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 | |
Junaid Shahid | 85aa888 | 2021-08-06 15:22:29 -0700 | [diff] [blame] | 333 | #define EPTP_PA_MASK GENMASK_ULL(51, 12) |
| 334 | |
| 335 | static bool nested_ept_root_matches(hpa_t root_hpa, u64 root_eptp, u64 eptp) |
| 336 | { |
| 337 | return VALID_PAGE(root_hpa) && |
| 338 | ((root_eptp & EPTP_PA_MASK) == (eptp & EPTP_PA_MASK)); |
| 339 | } |
| 340 | |
| 341 | static void nested_ept_invalidate_addr(struct kvm_vcpu *vcpu, gpa_t eptp, |
| 342 | gpa_t addr) |
| 343 | { |
| 344 | uint i; |
| 345 | struct kvm_mmu_root_info *cached_root; |
| 346 | |
| 347 | WARN_ON_ONCE(!mmu_is_nested(vcpu)); |
| 348 | |
| 349 | for (i = 0; i < KVM_MMU_NUM_PREV_ROOTS; i++) { |
| 350 | cached_root = &vcpu->arch.mmu->prev_roots[i]; |
| 351 | |
| 352 | if (nested_ept_root_matches(cached_root->hpa, cached_root->pgd, |
| 353 | eptp)) |
| 354 | vcpu->arch.mmu->invlpg(vcpu, addr, cached_root->hpa); |
| 355 | } |
| 356 | } |
| 357 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 358 | static void nested_ept_inject_page_fault(struct kvm_vcpu *vcpu, |
| 359 | struct x86_exception *fault) |
| 360 | { |
| 361 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
| 362 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Sean Christopherson | 4dcefa3 | 2020-04-15 10:55:18 -0700 | [diff] [blame] | 363 | u32 vm_exit_reason; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 364 | unsigned long exit_qualification = vcpu->arch.exit_qualification; |
| 365 | |
| 366 | if (vmx->nested.pml_full) { |
Sean Christopherson | 4dcefa3 | 2020-04-15 10:55:18 -0700 | [diff] [blame] | 367 | vm_exit_reason = EXIT_REASON_PML_FULL; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 368 | vmx->nested.pml_full = false; |
| 369 | exit_qualification &= INTR_INFO_UNBLOCK_NMI; |
Junaid Shahid | 85aa888 | 2021-08-06 15:22:29 -0700 | [diff] [blame] | 370 | } else { |
| 371 | if (fault->error_code & PFERR_RSVD_MASK) |
| 372 | vm_exit_reason = EXIT_REASON_EPT_MISCONFIG; |
| 373 | else |
| 374 | vm_exit_reason = EXIT_REASON_EPT_VIOLATION; |
| 375 | |
| 376 | /* |
| 377 | * Although the caller (kvm_inject_emulated_page_fault) would |
| 378 | * have already synced the faulting address in the shadow EPT |
| 379 | * tables for the current EPTP12, we also need to sync it for |
| 380 | * any other cached EPTP02s based on the same EP4TA, since the |
| 381 | * TLB associates mappings to the EP4TA rather than the full EPTP. |
| 382 | */ |
| 383 | nested_ept_invalidate_addr(vcpu, vmcs12->ept_pointer, |
| 384 | fault->address); |
| 385 | } |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 386 | |
Sean Christopherson | 4dcefa3 | 2020-04-15 10:55:18 -0700 | [diff] [blame] | 387 | nested_vmx_vmexit(vcpu, vm_exit_reason, 0, exit_qualification); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 388 | vmcs12->guest_physical_address = fault->address; |
| 389 | } |
| 390 | |
Sean Christopherson | 39353ab | 2021-06-09 16:42:31 -0700 | [diff] [blame] | 391 | static void nested_ept_new_eptp(struct kvm_vcpu *vcpu) |
| 392 | { |
| 393 | kvm_init_shadow_ept_mmu(vcpu, |
| 394 | to_vmx(vcpu)->nested.msrs.ept_caps & |
| 395 | VMX_EPT_EXECUTE_ONLY_BIT, |
| 396 | nested_ept_ad_enabled(vcpu), |
| 397 | nested_ept_get_eptp(vcpu)); |
| 398 | } |
| 399 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 400 | static void nested_ept_init_mmu_context(struct kvm_vcpu *vcpu) |
| 401 | { |
| 402 | WARN_ON(mmu_is_nested(vcpu)); |
| 403 | |
| 404 | vcpu->arch.mmu = &vcpu->arch.guest_mmu; |
Sean Christopherson | 39353ab | 2021-06-09 16:42:31 -0700 | [diff] [blame] | 405 | nested_ept_new_eptp(vcpu); |
Sean Christopherson | d8dd54e | 2020-03-02 18:02:39 -0800 | [diff] [blame] | 406 | vcpu->arch.mmu->get_guest_pgd = nested_ept_get_eptp; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 407 | vcpu->arch.mmu->inject_page_fault = nested_ept_inject_page_fault; |
| 408 | vcpu->arch.mmu->get_pdptr = kvm_pdptr_read; |
| 409 | |
| 410 | vcpu->arch.walk_mmu = &vcpu->arch.nested_mmu; |
| 411 | } |
| 412 | |
| 413 | static void nested_ept_uninit_mmu_context(struct kvm_vcpu *vcpu) |
| 414 | { |
| 415 | vcpu->arch.mmu = &vcpu->arch.root_mmu; |
| 416 | vcpu->arch.walk_mmu = &vcpu->arch.root_mmu; |
| 417 | } |
| 418 | |
| 419 | static bool nested_vmx_is_page_fault_vmexit(struct vmcs12 *vmcs12, |
| 420 | u16 error_code) |
| 421 | { |
| 422 | bool inequality, bit; |
| 423 | |
| 424 | bit = (vmcs12->exception_bitmap & (1u << PF_VECTOR)) != 0; |
| 425 | inequality = |
| 426 | (error_code & vmcs12->page_fault_error_code_mask) != |
| 427 | vmcs12->page_fault_error_code_match; |
| 428 | return inequality ^ bit; |
| 429 | } |
| 430 | |
| 431 | |
| 432 | /* |
| 433 | * KVM wants to inject page-faults which it got to the guest. This function |
| 434 | * checks whether in a nested guest, we need to inject them to L1 or L2. |
| 435 | */ |
| 436 | static int nested_vmx_check_exception(struct kvm_vcpu *vcpu, unsigned long *exit_qual) |
| 437 | { |
| 438 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
| 439 | unsigned int nr = vcpu->arch.exception.nr; |
| 440 | bool has_payload = vcpu->arch.exception.has_payload; |
| 441 | unsigned long payload = vcpu->arch.exception.payload; |
| 442 | |
| 443 | if (nr == PF_VECTOR) { |
| 444 | if (vcpu->arch.exception.nested_apf) { |
| 445 | *exit_qual = vcpu->arch.apf.nested_apf_token; |
| 446 | return 1; |
| 447 | } |
| 448 | if (nested_vmx_is_page_fault_vmexit(vmcs12, |
| 449 | vcpu->arch.exception.error_code)) { |
| 450 | *exit_qual = has_payload ? payload : vcpu->arch.cr2; |
| 451 | return 1; |
| 452 | } |
| 453 | } else if (vmcs12->exception_bitmap & (1u << nr)) { |
| 454 | if (nr == DB_VECTOR) { |
| 455 | if (!has_payload) { |
| 456 | payload = vcpu->arch.dr6; |
Chenyi Qiang | 9a3ecd5 | 2021-02-02 17:04:31 +0800 | [diff] [blame] | 457 | payload &= ~DR6_BT; |
| 458 | payload ^= DR6_ACTIVE_LOW; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 459 | } |
| 460 | *exit_qual = payload; |
| 461 | } else |
| 462 | *exit_qual = 0; |
| 463 | return 1; |
| 464 | } |
| 465 | |
| 466 | return 0; |
| 467 | } |
| 468 | |
| 469 | |
| 470 | static void vmx_inject_page_fault_nested(struct kvm_vcpu *vcpu, |
| 471 | struct x86_exception *fault) |
| 472 | { |
| 473 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
| 474 | |
| 475 | WARN_ON(!is_guest_mode(vcpu)); |
| 476 | |
| 477 | if (nested_vmx_is_page_fault_vmexit(vmcs12, fault->error_code) && |
| 478 | !to_vmx(vcpu)->nested.nested_run_pending) { |
| 479 | vmcs12->vm_exit_intr_error_code = fault->error_code; |
| 480 | nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI, |
| 481 | PF_VECTOR | INTR_TYPE_HARD_EXCEPTION | |
| 482 | INTR_INFO_DELIVER_CODE_MASK | INTR_INFO_VALID_MASK, |
| 483 | fault->address); |
| 484 | } else { |
| 485 | kvm_inject_page_fault(vcpu, fault); |
| 486 | } |
| 487 | } |
| 488 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 489 | static int nested_vmx_check_io_bitmap_controls(struct kvm_vcpu *vcpu, |
| 490 | struct vmcs12 *vmcs12) |
| 491 | { |
| 492 | if (!nested_cpu_has(vmcs12, CPU_BASED_USE_IO_BITMAPS)) |
| 493 | return 0; |
| 494 | |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 495 | if (CC(!page_address_valid(vcpu, vmcs12->io_bitmap_a)) || |
| 496 | CC(!page_address_valid(vcpu, vmcs12->io_bitmap_b))) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 497 | return -EINVAL; |
| 498 | |
| 499 | return 0; |
| 500 | } |
| 501 | |
| 502 | static int nested_vmx_check_msr_bitmap_controls(struct kvm_vcpu *vcpu, |
| 503 | struct vmcs12 *vmcs12) |
| 504 | { |
| 505 | if (!nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS)) |
| 506 | return 0; |
| 507 | |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 508 | if (CC(!page_address_valid(vcpu, vmcs12->msr_bitmap))) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 509 | return -EINVAL; |
| 510 | |
| 511 | return 0; |
| 512 | } |
| 513 | |
| 514 | static int nested_vmx_check_tpr_shadow_controls(struct kvm_vcpu *vcpu, |
| 515 | struct vmcs12 *vmcs12) |
| 516 | { |
| 517 | if (!nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW)) |
| 518 | return 0; |
| 519 | |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 520 | if (CC(!page_address_valid(vcpu, vmcs12->virtual_apic_page_addr))) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 521 | return -EINVAL; |
| 522 | |
| 523 | return 0; |
| 524 | } |
| 525 | |
| 526 | /* |
| 527 | * Check if MSR is intercepted for L01 MSR bitmap. |
| 528 | */ |
| 529 | static bool msr_write_intercepted_l01(struct kvm_vcpu *vcpu, u32 msr) |
| 530 | { |
| 531 | unsigned long *msr_bitmap; |
| 532 | int f = sizeof(unsigned long); |
| 533 | |
| 534 | if (!cpu_has_vmx_msr_bitmap()) |
| 535 | return true; |
| 536 | |
| 537 | msr_bitmap = to_vmx(vcpu)->vmcs01.msr_bitmap; |
| 538 | |
| 539 | if (msr <= 0x1fff) { |
| 540 | return !!test_bit(msr, msr_bitmap + 0x800 / f); |
| 541 | } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) { |
| 542 | msr &= 0x1fff; |
| 543 | return !!test_bit(msr, msr_bitmap + 0xc00 / f); |
| 544 | } |
| 545 | |
| 546 | return true; |
| 547 | } |
| 548 | |
| 549 | /* |
| 550 | * If a msr is allowed by L0, we should check whether it is allowed by L1. |
| 551 | * The corresponding bit will be cleared unless both of L0 and L1 allow it. |
| 552 | */ |
| 553 | static void nested_vmx_disable_intercept_for_msr(unsigned long *msr_bitmap_l1, |
| 554 | unsigned long *msr_bitmap_nested, |
| 555 | u32 msr, int type) |
| 556 | { |
| 557 | int f = sizeof(unsigned long); |
| 558 | |
| 559 | /* |
| 560 | * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals |
| 561 | * have the write-low and read-high bitmap offsets the wrong way round. |
| 562 | * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff. |
| 563 | */ |
| 564 | if (msr <= 0x1fff) { |
| 565 | if (type & MSR_TYPE_R && |
| 566 | !test_bit(msr, msr_bitmap_l1 + 0x000 / f)) |
| 567 | /* read-low */ |
| 568 | __clear_bit(msr, msr_bitmap_nested + 0x000 / f); |
| 569 | |
| 570 | if (type & MSR_TYPE_W && |
| 571 | !test_bit(msr, msr_bitmap_l1 + 0x800 / f)) |
| 572 | /* write-low */ |
| 573 | __clear_bit(msr, msr_bitmap_nested + 0x800 / f); |
| 574 | |
| 575 | } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) { |
| 576 | msr &= 0x1fff; |
| 577 | if (type & MSR_TYPE_R && |
| 578 | !test_bit(msr, msr_bitmap_l1 + 0x400 / f)) |
| 579 | /* read-high */ |
| 580 | __clear_bit(msr, msr_bitmap_nested + 0x400 / f); |
| 581 | |
| 582 | if (type & MSR_TYPE_W && |
| 583 | !test_bit(msr, msr_bitmap_l1 + 0xc00 / f)) |
| 584 | /* write-high */ |
| 585 | __clear_bit(msr, msr_bitmap_nested + 0xc00 / f); |
| 586 | |
| 587 | } |
| 588 | } |
| 589 | |
Miaohe Lin | ffdbd50 | 2020-02-07 23:22:45 +0800 | [diff] [blame] | 590 | static inline void enable_x2apic_msr_intercepts(unsigned long *msr_bitmap) |
| 591 | { |
Marc Orr | acff784 | 2019-04-01 23:55:59 -0700 | [diff] [blame] | 592 | int msr; |
| 593 | |
| 594 | for (msr = 0x800; msr <= 0x8ff; msr += BITS_PER_LONG) { |
| 595 | unsigned word = msr / BITS_PER_LONG; |
| 596 | |
| 597 | msr_bitmap[word] = ~0; |
| 598 | msr_bitmap[word + (0x800 / sizeof(long))] = ~0; |
| 599 | } |
| 600 | } |
| 601 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 602 | /* |
| 603 | * Merge L0's and L1's MSR bitmap, return false to indicate that |
| 604 | * we do not use the hardware. |
| 605 | */ |
| 606 | static inline bool nested_vmx_prepare_msr_bitmap(struct kvm_vcpu *vcpu, |
| 607 | struct vmcs12 *vmcs12) |
| 608 | { |
| 609 | int msr; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 610 | unsigned long *msr_bitmap_l1; |
| 611 | unsigned long *msr_bitmap_l0 = to_vmx(vcpu)->nested.vmcs02.msr_bitmap; |
KarimAllah Ahmed | 31f0b6c | 2019-01-31 21:24:36 +0100 | [diff] [blame] | 612 | struct kvm_host_map *map = &to_vmx(vcpu)->nested.msr_bitmap_map; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 613 | |
| 614 | /* Nothing to do if the MSR bitmap is not in use. */ |
| 615 | if (!cpu_has_vmx_msr_bitmap() || |
| 616 | !nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS)) |
| 617 | return false; |
| 618 | |
KarimAllah Ahmed | 31f0b6c | 2019-01-31 21:24:36 +0100 | [diff] [blame] | 619 | if (kvm_vcpu_map(vcpu, gpa_to_gfn(vmcs12->msr_bitmap), map)) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 620 | return false; |
| 621 | |
KarimAllah Ahmed | 31f0b6c | 2019-01-31 21:24:36 +0100 | [diff] [blame] | 622 | msr_bitmap_l1 = (unsigned long *)map->hva; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 623 | |
Marc Orr | acff784 | 2019-04-01 23:55:59 -0700 | [diff] [blame] | 624 | /* |
| 625 | * To keep the control flow simple, pay eight 8-byte writes (sixteen |
| 626 | * 4-byte writes on 32-bit systems) up front to enable intercepts for |
| 627 | * the x2APIC MSR range and selectively disable them below. |
| 628 | */ |
| 629 | enable_x2apic_msr_intercepts(msr_bitmap_l0); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 630 | |
Marc Orr | acff784 | 2019-04-01 23:55:59 -0700 | [diff] [blame] | 631 | if (nested_cpu_has_virt_x2apic_mode(vmcs12)) { |
| 632 | if (nested_cpu_has_apic_reg_virt(vmcs12)) { |
| 633 | /* |
| 634 | * L0 need not intercept reads for MSRs between 0x800 |
| 635 | * and 0x8ff, it just lets the processor take the value |
| 636 | * from the virtual-APIC page; take those 256 bits |
| 637 | * directly from the L1 bitmap. |
| 638 | */ |
| 639 | for (msr = 0x800; msr <= 0x8ff; msr += BITS_PER_LONG) { |
| 640 | unsigned word = msr / BITS_PER_LONG; |
| 641 | |
| 642 | msr_bitmap_l0[word] = msr_bitmap_l1[word]; |
| 643 | } |
| 644 | } |
| 645 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 646 | nested_vmx_disable_intercept_for_msr( |
| 647 | msr_bitmap_l1, msr_bitmap_l0, |
Marc Orr | acff784 | 2019-04-01 23:55:59 -0700 | [diff] [blame] | 648 | X2APIC_MSR(APIC_TASKPRI), |
Marc Orr | c73f4c9 | 2019-04-01 23:56:00 -0700 | [diff] [blame] | 649 | MSR_TYPE_R | MSR_TYPE_W); |
Marc Orr | acff784 | 2019-04-01 23:55:59 -0700 | [diff] [blame] | 650 | |
| 651 | if (nested_cpu_has_vid(vmcs12)) { |
| 652 | nested_vmx_disable_intercept_for_msr( |
| 653 | msr_bitmap_l1, msr_bitmap_l0, |
| 654 | X2APIC_MSR(APIC_EOI), |
| 655 | MSR_TYPE_W); |
| 656 | nested_vmx_disable_intercept_for_msr( |
| 657 | msr_bitmap_l1, msr_bitmap_l0, |
| 658 | X2APIC_MSR(APIC_SELF_IPI), |
| 659 | MSR_TYPE_W); |
| 660 | } |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 661 | } |
| 662 | |
Sean Christopherson | d69129b | 2019-05-08 07:32:15 -0700 | [diff] [blame] | 663 | /* KVM unconditionally exposes the FS/GS base MSRs to L1. */ |
Sean Christopherson | dbdd096 | 2021-04-21 19:38:31 -0700 | [diff] [blame] | 664 | #ifdef CONFIG_X86_64 |
Sean Christopherson | d69129b | 2019-05-08 07:32:15 -0700 | [diff] [blame] | 665 | nested_vmx_disable_intercept_for_msr(msr_bitmap_l1, msr_bitmap_l0, |
| 666 | MSR_FS_BASE, MSR_TYPE_RW); |
| 667 | |
| 668 | nested_vmx_disable_intercept_for_msr(msr_bitmap_l1, msr_bitmap_l0, |
| 669 | MSR_GS_BASE, MSR_TYPE_RW); |
| 670 | |
| 671 | nested_vmx_disable_intercept_for_msr(msr_bitmap_l1, msr_bitmap_l0, |
| 672 | MSR_KERNEL_GS_BASE, MSR_TYPE_RW); |
Sean Christopherson | dbdd096 | 2021-04-21 19:38:31 -0700 | [diff] [blame] | 673 | #endif |
Sean Christopherson | d69129b | 2019-05-08 07:32:15 -0700 | [diff] [blame] | 674 | |
| 675 | /* |
| 676 | * Checking the L0->L1 bitmap is trying to verify two things: |
| 677 | * |
| 678 | * 1. L0 gave a permission to L1 to actually passthrough the MSR. This |
| 679 | * ensures that we do not accidentally generate an L02 MSR bitmap |
| 680 | * from the L12 MSR bitmap that is too permissive. |
| 681 | * 2. That L1 or L2s have actually used the MSR. This avoids |
| 682 | * unnecessarily merging of the bitmap if the MSR is unused. This |
| 683 | * works properly because we only update the L01 MSR bitmap lazily. |
| 684 | * So even if L0 should pass L1 these MSRs, the L01 bitmap is only |
| 685 | * updated to reflect this when L1 (or its L2s) actually write to |
| 686 | * the MSR. |
| 687 | */ |
| 688 | if (!msr_write_intercepted_l01(vcpu, MSR_IA32_SPEC_CTRL)) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 689 | nested_vmx_disable_intercept_for_msr( |
| 690 | msr_bitmap_l1, msr_bitmap_l0, |
| 691 | MSR_IA32_SPEC_CTRL, |
| 692 | MSR_TYPE_R | MSR_TYPE_W); |
| 693 | |
Sean Christopherson | d69129b | 2019-05-08 07:32:15 -0700 | [diff] [blame] | 694 | if (!msr_write_intercepted_l01(vcpu, MSR_IA32_PRED_CMD)) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 695 | nested_vmx_disable_intercept_for_msr( |
| 696 | msr_bitmap_l1, msr_bitmap_l0, |
| 697 | MSR_IA32_PRED_CMD, |
| 698 | MSR_TYPE_W); |
| 699 | |
KarimAllah Ahmed | 31f0b6c | 2019-01-31 21:24:36 +0100 | [diff] [blame] | 700 | kvm_vcpu_unmap(vcpu, &to_vmx(vcpu)->nested.msr_bitmap_map, false); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 701 | |
| 702 | return true; |
| 703 | } |
| 704 | |
| 705 | static void nested_cache_shadow_vmcs12(struct kvm_vcpu *vcpu, |
| 706 | struct vmcs12 *vmcs12) |
| 707 | { |
KarimAllah Ahmed | 8892530 | 2019-01-31 21:24:41 +0100 | [diff] [blame] | 708 | struct kvm_host_map map; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 709 | struct vmcs12 *shadow; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 710 | |
| 711 | if (!nested_cpu_has_shadow_vmcs(vmcs12) || |
| 712 | vmcs12->vmcs_link_pointer == -1ull) |
| 713 | return; |
| 714 | |
| 715 | shadow = get_shadow_vmcs12(vcpu); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 716 | |
KarimAllah Ahmed | 8892530 | 2019-01-31 21:24:41 +0100 | [diff] [blame] | 717 | if (kvm_vcpu_map(vcpu, gpa_to_gfn(vmcs12->vmcs_link_pointer), &map)) |
| 718 | return; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 719 | |
KarimAllah Ahmed | 8892530 | 2019-01-31 21:24:41 +0100 | [diff] [blame] | 720 | memcpy(shadow, map.hva, VMCS12_SIZE); |
| 721 | kvm_vcpu_unmap(vcpu, &map, false); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 722 | } |
| 723 | |
| 724 | static void nested_flush_cached_shadow_vmcs12(struct kvm_vcpu *vcpu, |
| 725 | struct vmcs12 *vmcs12) |
| 726 | { |
| 727 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 728 | |
| 729 | if (!nested_cpu_has_shadow_vmcs(vmcs12) || |
| 730 | vmcs12->vmcs_link_pointer == -1ull) |
| 731 | return; |
| 732 | |
| 733 | kvm_write_guest(vmx->vcpu.kvm, vmcs12->vmcs_link_pointer, |
| 734 | get_shadow_vmcs12(vcpu), VMCS12_SIZE); |
| 735 | } |
| 736 | |
| 737 | /* |
| 738 | * In nested virtualization, check if L1 has set |
| 739 | * VM_EXIT_ACK_INTR_ON_EXIT |
| 740 | */ |
| 741 | static bool nested_exit_intr_ack_set(struct kvm_vcpu *vcpu) |
| 742 | { |
| 743 | return get_vmcs12(vcpu)->vm_exit_controls & |
| 744 | VM_EXIT_ACK_INTR_ON_EXIT; |
| 745 | } |
| 746 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 747 | static int nested_vmx_check_apic_access_controls(struct kvm_vcpu *vcpu, |
| 748 | struct vmcs12 *vmcs12) |
| 749 | { |
| 750 | if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES) && |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 751 | CC(!page_address_valid(vcpu, vmcs12->apic_access_addr))) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 752 | return -EINVAL; |
| 753 | else |
| 754 | return 0; |
| 755 | } |
| 756 | |
| 757 | static int nested_vmx_check_apicv_controls(struct kvm_vcpu *vcpu, |
| 758 | struct vmcs12 *vmcs12) |
| 759 | { |
| 760 | if (!nested_cpu_has_virt_x2apic_mode(vmcs12) && |
| 761 | !nested_cpu_has_apic_reg_virt(vmcs12) && |
| 762 | !nested_cpu_has_vid(vmcs12) && |
| 763 | !nested_cpu_has_posted_intr(vmcs12)) |
| 764 | return 0; |
| 765 | |
| 766 | /* |
| 767 | * If virtualize x2apic mode is enabled, |
| 768 | * virtualize apic access must be disabled. |
| 769 | */ |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 770 | if (CC(nested_cpu_has_virt_x2apic_mode(vmcs12) && |
| 771 | nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES))) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 772 | return -EINVAL; |
| 773 | |
| 774 | /* |
| 775 | * If virtual interrupt delivery is enabled, |
| 776 | * we must exit on external interrupts. |
| 777 | */ |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 778 | if (CC(nested_cpu_has_vid(vmcs12) && !nested_exit_on_intr(vcpu))) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 779 | return -EINVAL; |
| 780 | |
| 781 | /* |
| 782 | * bits 15:8 should be zero in posted_intr_nv, |
| 783 | * the descriptor address has been already checked |
| 784 | * in nested_get_vmcs12_pages. |
| 785 | * |
| 786 | * bits 5:0 of posted_intr_desc_addr should be zero. |
| 787 | */ |
| 788 | if (nested_cpu_has_posted_intr(vmcs12) && |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 789 | (CC(!nested_cpu_has_vid(vmcs12)) || |
| 790 | CC(!nested_exit_intr_ack_set(vcpu)) || |
| 791 | CC((vmcs12->posted_intr_nv & 0xff00)) || |
Sean Christopherson | 636e8b7 | 2021-02-03 16:01:10 -0800 | [diff] [blame] | 792 | 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] | 793 | return -EINVAL; |
| 794 | |
| 795 | /* tpr shadow is needed by all apicv features. */ |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 796 | if (CC(!nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW))) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 797 | return -EINVAL; |
| 798 | |
| 799 | return 0; |
| 800 | } |
| 801 | |
| 802 | static int nested_vmx_check_msr_switch(struct kvm_vcpu *vcpu, |
Sean Christopherson | f9b245e | 2018-12-12 13:30:08 -0500 | [diff] [blame] | 803 | u32 count, u64 addr) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 804 | { |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 805 | if (count == 0) |
| 806 | return 0; |
Sean Christopherson | 636e8b7 | 2021-02-03 16:01:10 -0800 | [diff] [blame] | 807 | |
| 808 | if (!kvm_vcpu_is_legal_aligned_gpa(vcpu, addr, 16) || |
| 809 | !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] | 810 | return -EINVAL; |
Sean Christopherson | f9b245e | 2018-12-12 13:30:08 -0500 | [diff] [blame] | 811 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 812 | return 0; |
| 813 | } |
| 814 | |
Krish Sadhukhan | 61446ba | 2018-12-12 13:30:09 -0500 | [diff] [blame] | 815 | static int nested_vmx_check_exit_msr_switch_controls(struct kvm_vcpu *vcpu, |
| 816 | struct vmcs12 *vmcs12) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 817 | { |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 818 | if (CC(nested_vmx_check_msr_switch(vcpu, |
| 819 | vmcs12->vm_exit_msr_load_count, |
| 820 | vmcs12->vm_exit_msr_load_addr)) || |
| 821 | CC(nested_vmx_check_msr_switch(vcpu, |
| 822 | vmcs12->vm_exit_msr_store_count, |
| 823 | vmcs12->vm_exit_msr_store_addr))) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 824 | return -EINVAL; |
Sean Christopherson | f9b245e | 2018-12-12 13:30:08 -0500 | [diff] [blame] | 825 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 826 | return 0; |
| 827 | } |
| 828 | |
Krish Sadhukhan | 5fbf963 | 2018-12-12 13:30:10 -0500 | [diff] [blame] | 829 | static int nested_vmx_check_entry_msr_switch_controls(struct kvm_vcpu *vcpu, |
| 830 | struct vmcs12 *vmcs12) |
Krish Sadhukhan | 61446ba | 2018-12-12 13:30:09 -0500 | [diff] [blame] | 831 | { |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 832 | if (CC(nested_vmx_check_msr_switch(vcpu, |
| 833 | vmcs12->vm_entry_msr_load_count, |
| 834 | vmcs12->vm_entry_msr_load_addr))) |
Krish Sadhukhan | 61446ba | 2018-12-12 13:30:09 -0500 | [diff] [blame] | 835 | return -EINVAL; |
| 836 | |
| 837 | return 0; |
| 838 | } |
| 839 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 840 | static int nested_vmx_check_pml_controls(struct kvm_vcpu *vcpu, |
| 841 | struct vmcs12 *vmcs12) |
| 842 | { |
| 843 | if (!nested_cpu_has_pml(vmcs12)) |
| 844 | return 0; |
| 845 | |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 846 | if (CC(!nested_cpu_has_ept(vmcs12)) || |
| 847 | CC(!page_address_valid(vcpu, vmcs12->pml_address))) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 848 | return -EINVAL; |
| 849 | |
| 850 | return 0; |
| 851 | } |
| 852 | |
| 853 | static int nested_vmx_check_unrestricted_guest_controls(struct kvm_vcpu *vcpu, |
| 854 | struct vmcs12 *vmcs12) |
| 855 | { |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 856 | if (CC(nested_cpu_has2(vmcs12, SECONDARY_EXEC_UNRESTRICTED_GUEST) && |
| 857 | !nested_cpu_has_ept(vmcs12))) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 858 | return -EINVAL; |
| 859 | return 0; |
| 860 | } |
| 861 | |
| 862 | static int nested_vmx_check_mode_based_ept_exec_controls(struct kvm_vcpu *vcpu, |
| 863 | struct vmcs12 *vmcs12) |
| 864 | { |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 865 | if (CC(nested_cpu_has2(vmcs12, SECONDARY_EXEC_MODE_BASED_EPT_EXEC) && |
| 866 | !nested_cpu_has_ept(vmcs12))) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 867 | return -EINVAL; |
| 868 | return 0; |
| 869 | } |
| 870 | |
| 871 | static int nested_vmx_check_shadow_vmcs_controls(struct kvm_vcpu *vcpu, |
| 872 | struct vmcs12 *vmcs12) |
| 873 | { |
| 874 | if (!nested_cpu_has_shadow_vmcs(vmcs12)) |
| 875 | return 0; |
| 876 | |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 877 | if (CC(!page_address_valid(vcpu, vmcs12->vmread_bitmap)) || |
| 878 | CC(!page_address_valid(vcpu, vmcs12->vmwrite_bitmap))) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 879 | return -EINVAL; |
| 880 | |
| 881 | return 0; |
| 882 | } |
| 883 | |
| 884 | static int nested_vmx_msr_check_common(struct kvm_vcpu *vcpu, |
| 885 | struct vmx_msr_entry *e) |
| 886 | { |
| 887 | /* x2APIC MSR accesses are not allowed */ |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 888 | if (CC(vcpu->arch.apic_base & X2APIC_ENABLE && e->index >> 8 == 0x8)) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 889 | return -EINVAL; |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 890 | if (CC(e->index == MSR_IA32_UCODE_WRITE) || /* SDM Table 35-2 */ |
| 891 | CC(e->index == MSR_IA32_UCODE_REV)) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 892 | return -EINVAL; |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 893 | if (CC(e->reserved != 0)) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 894 | return -EINVAL; |
| 895 | return 0; |
| 896 | } |
| 897 | |
| 898 | static int nested_vmx_load_msr_check(struct kvm_vcpu *vcpu, |
| 899 | struct vmx_msr_entry *e) |
| 900 | { |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 901 | if (CC(e->index == MSR_FS_BASE) || |
| 902 | CC(e->index == MSR_GS_BASE) || |
| 903 | CC(e->index == MSR_IA32_SMM_MONITOR_CTL) || /* SMM is not supported */ |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 904 | nested_vmx_msr_check_common(vcpu, e)) |
| 905 | return -EINVAL; |
| 906 | return 0; |
| 907 | } |
| 908 | |
| 909 | static int nested_vmx_store_msr_check(struct kvm_vcpu *vcpu, |
| 910 | struct vmx_msr_entry *e) |
| 911 | { |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 912 | if (CC(e->index == MSR_IA32_SMBASE) || /* SMM is not supported */ |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 913 | nested_vmx_msr_check_common(vcpu, e)) |
| 914 | return -EINVAL; |
| 915 | return 0; |
| 916 | } |
| 917 | |
Marc Orr | f0b5105 | 2019-09-17 11:50:57 -0700 | [diff] [blame] | 918 | static u32 nested_vmx_max_atomic_switch_msrs(struct kvm_vcpu *vcpu) |
| 919 | { |
| 920 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 921 | u64 vmx_misc = vmx_control_msr(vmx->nested.msrs.misc_low, |
| 922 | vmx->nested.msrs.misc_high); |
| 923 | |
| 924 | return (vmx_misc_max_msr(vmx_misc) + 1) * VMX_MISC_MSR_LIST_MULTIPLIER; |
| 925 | } |
| 926 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 927 | /* |
| 928 | * Load guest's/host's msr at nested entry/exit. |
| 929 | * return 0 for success, entry index for failure. |
Marc Orr | f0b5105 | 2019-09-17 11:50:57 -0700 | [diff] [blame] | 930 | * |
| 931 | * One of the failure modes for MSR load/store is when a list exceeds the |
| 932 | * virtual hardware's capacity. To maintain compatibility with hardware inasmuch |
| 933 | * as possible, process all valid entries before failing rather than precheck |
| 934 | * for a capacity violation. |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 935 | */ |
| 936 | static u32 nested_vmx_load_msr(struct kvm_vcpu *vcpu, u64 gpa, u32 count) |
| 937 | { |
| 938 | u32 i; |
| 939 | struct vmx_msr_entry e; |
Marc Orr | f0b5105 | 2019-09-17 11:50:57 -0700 | [diff] [blame] | 940 | u32 max_msr_list_size = nested_vmx_max_atomic_switch_msrs(vcpu); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 941 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 942 | for (i = 0; i < count; i++) { |
Marc Orr | f0b5105 | 2019-09-17 11:50:57 -0700 | [diff] [blame] | 943 | if (unlikely(i >= max_msr_list_size)) |
| 944 | goto fail; |
| 945 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 946 | if (kvm_vcpu_read_guest(vcpu, gpa + i * sizeof(e), |
| 947 | &e, sizeof(e))) { |
| 948 | pr_debug_ratelimited( |
| 949 | "%s cannot read MSR entry (%u, 0x%08llx)\n", |
| 950 | __func__, i, gpa + i * sizeof(e)); |
| 951 | goto fail; |
| 952 | } |
| 953 | if (nested_vmx_load_msr_check(vcpu, &e)) { |
| 954 | pr_debug_ratelimited( |
| 955 | "%s check failed (%u, 0x%x, 0x%x)\n", |
| 956 | __func__, i, e.index, e.reserved); |
| 957 | goto fail; |
| 958 | } |
Sean Christopherson | f20935d | 2019-09-05 14:22:54 -0700 | [diff] [blame] | 959 | if (kvm_set_msr(vcpu, e.index, e.value)) { |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 960 | pr_debug_ratelimited( |
| 961 | "%s cannot write MSR (%u, 0x%x, 0x%llx)\n", |
| 962 | __func__, i, e.index, e.value); |
| 963 | goto fail; |
| 964 | } |
| 965 | } |
| 966 | return 0; |
| 967 | fail: |
Sean Christopherson | 68cda40 | 2020-05-11 15:05:29 -0700 | [diff] [blame] | 968 | /* 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] | 969 | return i + 1; |
| 970 | } |
| 971 | |
Aaron Lewis | 662f1d1 | 2019-11-07 21:14:39 -0800 | [diff] [blame] | 972 | static bool nested_vmx_get_vmexit_msr_value(struct kvm_vcpu *vcpu, |
| 973 | u32 msr_index, |
| 974 | u64 *data) |
| 975 | { |
| 976 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 977 | |
| 978 | /* |
| 979 | * If the L0 hypervisor stored a more accurate value for the TSC that |
| 980 | * does not include the time taken for emulation of the L2->L1 |
| 981 | * VM-exit in L0, use the more accurate value. |
| 982 | */ |
| 983 | if (msr_index == MSR_IA32_TSC) { |
Sean Christopherson | a128a93 | 2020-09-23 11:03:57 -0700 | [diff] [blame] | 984 | int i = vmx_find_loadstore_msr_slot(&vmx->msr_autostore.guest, |
| 985 | MSR_IA32_TSC); |
Aaron Lewis | 662f1d1 | 2019-11-07 21:14:39 -0800 | [diff] [blame] | 986 | |
Sean Christopherson | a128a93 | 2020-09-23 11:03:57 -0700 | [diff] [blame] | 987 | if (i >= 0) { |
| 988 | u64 val = vmx->msr_autostore.guest.val[i].value; |
Aaron Lewis | 662f1d1 | 2019-11-07 21:14:39 -0800 | [diff] [blame] | 989 | |
| 990 | *data = kvm_read_l1_tsc(vcpu, val); |
| 991 | return true; |
| 992 | } |
| 993 | } |
| 994 | |
| 995 | if (kvm_get_msr(vcpu, msr_index, data)) { |
| 996 | pr_debug_ratelimited("%s cannot read MSR (0x%x)\n", __func__, |
| 997 | msr_index); |
| 998 | return false; |
| 999 | } |
| 1000 | return true; |
| 1001 | } |
| 1002 | |
Aaron Lewis | 365d3d5 | 2019-11-07 21:14:36 -0800 | [diff] [blame] | 1003 | static bool read_and_check_msr_entry(struct kvm_vcpu *vcpu, u64 gpa, int i, |
| 1004 | struct vmx_msr_entry *e) |
| 1005 | { |
| 1006 | if (kvm_vcpu_read_guest(vcpu, |
| 1007 | gpa + i * sizeof(*e), |
| 1008 | e, 2 * sizeof(u32))) { |
| 1009 | pr_debug_ratelimited( |
| 1010 | "%s cannot read MSR entry (%u, 0x%08llx)\n", |
| 1011 | __func__, i, gpa + i * sizeof(*e)); |
| 1012 | return false; |
| 1013 | } |
| 1014 | if (nested_vmx_store_msr_check(vcpu, e)) { |
| 1015 | pr_debug_ratelimited( |
| 1016 | "%s check failed (%u, 0x%x, 0x%x)\n", |
| 1017 | __func__, i, e->index, e->reserved); |
| 1018 | return false; |
| 1019 | } |
| 1020 | return true; |
| 1021 | } |
| 1022 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1023 | static int nested_vmx_store_msr(struct kvm_vcpu *vcpu, u64 gpa, u32 count) |
| 1024 | { |
Sean Christopherson | f20935d | 2019-09-05 14:22:54 -0700 | [diff] [blame] | 1025 | u64 data; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1026 | u32 i; |
| 1027 | struct vmx_msr_entry e; |
Marc Orr | f0b5105 | 2019-09-17 11:50:57 -0700 | [diff] [blame] | 1028 | u32 max_msr_list_size = nested_vmx_max_atomic_switch_msrs(vcpu); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1029 | |
| 1030 | for (i = 0; i < count; i++) { |
Marc Orr | f0b5105 | 2019-09-17 11:50:57 -0700 | [diff] [blame] | 1031 | if (unlikely(i >= max_msr_list_size)) |
| 1032 | return -EINVAL; |
| 1033 | |
Aaron Lewis | 365d3d5 | 2019-11-07 21:14:36 -0800 | [diff] [blame] | 1034 | if (!read_and_check_msr_entry(vcpu, gpa, i, &e)) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1035 | return -EINVAL; |
Aaron Lewis | 365d3d5 | 2019-11-07 21:14:36 -0800 | [diff] [blame] | 1036 | |
Aaron Lewis | 662f1d1 | 2019-11-07 21:14:39 -0800 | [diff] [blame] | 1037 | if (!nested_vmx_get_vmexit_msr_value(vcpu, e.index, &data)) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1038 | return -EINVAL; |
Aaron Lewis | 662f1d1 | 2019-11-07 21:14:39 -0800 | [diff] [blame] | 1039 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1040 | if (kvm_vcpu_write_guest(vcpu, |
| 1041 | gpa + i * sizeof(e) + |
| 1042 | offsetof(struct vmx_msr_entry, value), |
Sean Christopherson | f20935d | 2019-09-05 14:22:54 -0700 | [diff] [blame] | 1043 | &data, sizeof(data))) { |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1044 | pr_debug_ratelimited( |
| 1045 | "%s cannot write MSR (%u, 0x%x, 0x%llx)\n", |
Sean Christopherson | f20935d | 2019-09-05 14:22:54 -0700 | [diff] [blame] | 1046 | __func__, i, e.index, data); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1047 | return -EINVAL; |
| 1048 | } |
| 1049 | } |
| 1050 | return 0; |
| 1051 | } |
| 1052 | |
Aaron Lewis | 662f1d1 | 2019-11-07 21:14:39 -0800 | [diff] [blame] | 1053 | static bool nested_msr_store_list_has_msr(struct kvm_vcpu *vcpu, u32 msr_index) |
| 1054 | { |
| 1055 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
| 1056 | u32 count = vmcs12->vm_exit_msr_store_count; |
| 1057 | u64 gpa = vmcs12->vm_exit_msr_store_addr; |
| 1058 | struct vmx_msr_entry e; |
| 1059 | u32 i; |
| 1060 | |
| 1061 | for (i = 0; i < count; i++) { |
| 1062 | if (!read_and_check_msr_entry(vcpu, gpa, i, &e)) |
| 1063 | return false; |
| 1064 | |
| 1065 | if (e.index == msr_index) |
| 1066 | return true; |
| 1067 | } |
| 1068 | return false; |
| 1069 | } |
| 1070 | |
| 1071 | static void prepare_vmx_msr_autostore_list(struct kvm_vcpu *vcpu, |
| 1072 | u32 msr_index) |
| 1073 | { |
| 1074 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 1075 | struct vmx_msrs *autostore = &vmx->msr_autostore.guest; |
| 1076 | bool in_vmcs12_store_list; |
Sean Christopherson | a128a93 | 2020-09-23 11:03:57 -0700 | [diff] [blame] | 1077 | int msr_autostore_slot; |
Aaron Lewis | 662f1d1 | 2019-11-07 21:14:39 -0800 | [diff] [blame] | 1078 | bool in_autostore_list; |
| 1079 | int last; |
| 1080 | |
Sean Christopherson | a128a93 | 2020-09-23 11:03:57 -0700 | [diff] [blame] | 1081 | msr_autostore_slot = vmx_find_loadstore_msr_slot(autostore, msr_index); |
| 1082 | in_autostore_list = msr_autostore_slot >= 0; |
Aaron Lewis | 662f1d1 | 2019-11-07 21:14:39 -0800 | [diff] [blame] | 1083 | in_vmcs12_store_list = nested_msr_store_list_has_msr(vcpu, msr_index); |
| 1084 | |
| 1085 | if (in_vmcs12_store_list && !in_autostore_list) { |
Sean Christopherson | ce833b2 | 2020-09-23 11:03:56 -0700 | [diff] [blame] | 1086 | if (autostore->nr == MAX_NR_LOADSTORE_MSRS) { |
Aaron Lewis | 662f1d1 | 2019-11-07 21:14:39 -0800 | [diff] [blame] | 1087 | /* |
| 1088 | * Emulated VMEntry does not fail here. Instead a less |
| 1089 | * accurate value will be returned by |
| 1090 | * nested_vmx_get_vmexit_msr_value() using kvm_get_msr() |
| 1091 | * instead of reading the value from the vmcs02 VMExit |
| 1092 | * MSR-store area. |
| 1093 | */ |
| 1094 | pr_warn_ratelimited( |
| 1095 | "Not enough msr entries in msr_autostore. Can't add msr %x\n", |
| 1096 | msr_index); |
| 1097 | return; |
| 1098 | } |
| 1099 | last = autostore->nr++; |
| 1100 | autostore->val[last].index = msr_index; |
| 1101 | } else if (!in_vmcs12_store_list && in_autostore_list) { |
| 1102 | last = --autostore->nr; |
Sean Christopherson | a128a93 | 2020-09-23 11:03:57 -0700 | [diff] [blame] | 1103 | autostore->val[msr_autostore_slot] = autostore->val[last]; |
Aaron Lewis | 662f1d1 | 2019-11-07 21:14:39 -0800 | [diff] [blame] | 1104 | } |
| 1105 | } |
| 1106 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1107 | /* |
Sean Christopherson | ea79a75 | 2020-02-04 07:32:59 -0800 | [diff] [blame] | 1108 | * Load guest's/host's cr3 at nested entry/exit. @nested_ept is true if we are |
| 1109 | * emulating VM-Entry into a guest with EPT enabled. On failure, the expected |
| 1110 | * Exit Qualification (for a VM-Entry consistency check VM-Exit) is assigned to |
| 1111 | * @entry_failure_code. |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1112 | */ |
Maxim Levitsky | 0f85722 | 2021-06-07 12:02:00 +0300 | [diff] [blame] | 1113 | static int nested_vmx_load_cr3(struct kvm_vcpu *vcpu, unsigned long cr3, |
| 1114 | bool nested_ept, bool reload_pdptrs, |
Sean Christopherson | 68cda40 | 2020-05-11 15:05:29 -0700 | [diff] [blame] | 1115 | enum vm_entry_failure_code *entry_failure_code) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1116 | { |
Sean Christopherson | 636e8b7 | 2021-02-03 16:01:10 -0800 | [diff] [blame] | 1117 | if (CC(kvm_vcpu_is_illegal_gpa(vcpu, cr3))) { |
Sean Christopherson | 0cc6920 | 2020-05-01 21:32:26 -0700 | [diff] [blame] | 1118 | *entry_failure_code = ENTRY_FAIL_DEFAULT; |
| 1119 | return -EINVAL; |
| 1120 | } |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1121 | |
Sean Christopherson | 0cc6920 | 2020-05-01 21:32:26 -0700 | [diff] [blame] | 1122 | /* |
| 1123 | * If PAE paging and EPT are both on, CR3 is not used by the CPU and |
| 1124 | * must not be dereferenced. |
| 1125 | */ |
Maxim Levitsky | 0f85722 | 2021-06-07 12:02:00 +0300 | [diff] [blame] | 1126 | if (reload_pdptrs && !nested_ept && is_pae_paging(vcpu) && |
Sean Christopherson | bcb72d0 | 2021-06-07 12:01:56 +0300 | [diff] [blame] | 1127 | CC(!load_pdptrs(vcpu, vcpu->arch.walk_mmu, cr3))) { |
| 1128 | *entry_failure_code = ENTRY_FAIL_PDPTE; |
| 1129 | return -EINVAL; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1130 | } |
| 1131 | |
Sean Christopherson | 50a4179 | 2021-06-09 16:42:28 -0700 | [diff] [blame] | 1132 | if (!nested_ept) |
Sean Christopherson | b512910 | 2021-06-09 16:42:27 -0700 | [diff] [blame] | 1133 | kvm_mmu_new_pgd(vcpu, cr3); |
Sean Christopherson | 07ffaf3 | 2021-06-09 16:42:21 -0700 | [diff] [blame] | 1134 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1135 | vcpu->arch.cr3 = cr3; |
Sean Christopherson | cb3c1e2 | 2019-09-27 14:45:22 -0700 | [diff] [blame] | 1136 | kvm_register_mark_available(vcpu, VCPU_EXREG_CR3); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1137 | |
Sean Christopherson | 616007c | 2021-06-22 10:57:34 -0700 | [diff] [blame] | 1138 | /* Re-initialize the MMU, e.g. to pick up CR4 MMU role changes. */ |
Sean Christopherson | c906066 | 2021-06-09 16:42:33 -0700 | [diff] [blame] | 1139 | kvm_init_mmu(vcpu); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1140 | |
| 1141 | return 0; |
| 1142 | } |
| 1143 | |
| 1144 | /* |
| 1145 | * Returns if KVM is able to config CPU to tag TLB entries |
| 1146 | * populated by L2 differently than TLB entries populated |
| 1147 | * by L1. |
| 1148 | * |
Liran Alon | 992edea | 2019-11-20 14:24:52 +0200 | [diff] [blame] | 1149 | * If L0 uses EPT, L1 and L2 run with different EPTP because |
| 1150 | * guest_mode is part of kvm_mmu_page_role. Thus, TLB entries |
| 1151 | * are tagged with different EPTP. |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1152 | * |
| 1153 | * If L1 uses VPID and we allocated a vpid02, TLB entries are tagged |
| 1154 | * with different VPID (L1 entries are tagged with vmx->vpid |
| 1155 | * while L2 entries are tagged with vmx->nested.vpid02). |
| 1156 | */ |
| 1157 | static bool nested_has_guest_tlb_tag(struct kvm_vcpu *vcpu) |
| 1158 | { |
| 1159 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
| 1160 | |
Liran Alon | 992edea | 2019-11-20 14:24:52 +0200 | [diff] [blame] | 1161 | return enable_ept || |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1162 | (nested_cpu_has_vpid(vmcs12) && to_vmx(vcpu)->nested.vpid02); |
| 1163 | } |
| 1164 | |
Sean Christopherson | 50b265a | 2020-03-20 14:28:19 -0700 | [diff] [blame] | 1165 | static void nested_vmx_transition_tlb_flush(struct kvm_vcpu *vcpu, |
| 1166 | struct vmcs12 *vmcs12, |
| 1167 | bool is_vmenter) |
| 1168 | { |
| 1169 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 1170 | |
| 1171 | /* |
Sean Christopherson | 50a4179 | 2021-06-09 16:42:28 -0700 | [diff] [blame] | 1172 | * If vmcs12 doesn't use VPID, L1 expects linear and combined mappings |
| 1173 | * for *all* contexts to be flushed on VM-Enter/VM-Exit, i.e. it's a |
| 1174 | * full TLB flush from the guest's perspective. This is required even |
| 1175 | * if VPID is disabled in the host as KVM may need to synchronize the |
| 1176 | * MMU in response to the guest TLB flush. |
| 1177 | * |
| 1178 | * Note, using TLB_FLUSH_GUEST is correct even if nested EPT is in use. |
| 1179 | * EPT is a special snowflake, as guest-physical mappings aren't |
| 1180 | * flushed on VPID invalidations, including VM-Enter or VM-Exit with |
| 1181 | * VPID disabled. As a result, KVM _never_ needs to sync nEPT |
| 1182 | * entries on VM-Enter because L1 can't rely on VM-Enter to flush |
| 1183 | * those mappings. |
Sean Christopherson | 50b265a | 2020-03-20 14:28:19 -0700 | [diff] [blame] | 1184 | */ |
Sean Christopherson | 50a4179 | 2021-06-09 16:42:28 -0700 | [diff] [blame] | 1185 | if (!nested_cpu_has_vpid(vmcs12)) { |
| 1186 | kvm_make_request(KVM_REQ_TLB_FLUSH_GUEST, vcpu); |
Sean Christopherson | 50b265a | 2020-03-20 14:28:19 -0700 | [diff] [blame] | 1187 | return; |
Sean Christopherson | 50a4179 | 2021-06-09 16:42:28 -0700 | [diff] [blame] | 1188 | } |
| 1189 | |
| 1190 | /* L2 should never have a VPID if VPID is disabled. */ |
| 1191 | WARN_ON(!enable_vpid); |
Sean Christopherson | 50b265a | 2020-03-20 14:28:19 -0700 | [diff] [blame] | 1192 | |
| 1193 | /* |
Sean Christopherson | 50b265a | 2020-03-20 14:28:19 -0700 | [diff] [blame] | 1194 | * If VPID is enabled and used by vmc12, but L2 does not have a unique |
| 1195 | * 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] | 1196 | * a VPID for L2, flush the current context as the effective ASID is |
| 1197 | * common to both L1 and L2. |
Sean Christopherson | 50b265a | 2020-03-20 14:28:19 -0700 | [diff] [blame] | 1198 | * |
| 1199 | * Defer the flush so that it runs after vmcs02.EPTP has been set by |
| 1200 | * KVM_REQ_LOAD_MMU_PGD (if nested EPT is enabled) and to avoid |
| 1201 | * redundant flushes further down the nested pipeline. |
| 1202 | * |
| 1203 | * If a TLB flush isn't required due to any of the above, and vpid12 is |
| 1204 | * changing then the new "virtual" VPID (vpid12) will reuse the same |
Sean Christopherson | 50a4179 | 2021-06-09 16:42:28 -0700 | [diff] [blame] | 1205 | * "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] | 1206 | * mapping between vpid02 and vpid12, vpid02 is per-vCPU and reused for |
Sean Christopherson | 50a4179 | 2021-06-09 16:42:28 -0700 | [diff] [blame] | 1207 | * all nested vCPUs. Remember, a flush on VM-Enter does not invalidate |
| 1208 | * 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] | 1209 | */ |
Sean Christopherson | 50a4179 | 2021-06-09 16:42:28 -0700 | [diff] [blame] | 1210 | if (!nested_has_guest_tlb_tag(vcpu)) { |
Sean Christopherson | c51e1ff | 2020-03-20 14:28:22 -0700 | [diff] [blame] | 1211 | kvm_make_request(KVM_REQ_TLB_FLUSH_CURRENT, vcpu); |
Sean Christopherson | 50b265a | 2020-03-20 14:28:19 -0700 | [diff] [blame] | 1212 | } else if (is_vmenter && |
| 1213 | vmcs12->virtual_processor_id != vmx->nested.last_vpid) { |
| 1214 | vmx->nested.last_vpid = vmcs12->virtual_processor_id; |
| 1215 | vpid_sync_context(nested_get_vpid02(vcpu)); |
| 1216 | } |
| 1217 | } |
| 1218 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1219 | static bool is_bitwise_subset(u64 superset, u64 subset, u64 mask) |
| 1220 | { |
| 1221 | superset &= mask; |
| 1222 | subset &= mask; |
| 1223 | |
| 1224 | return (superset | subset) == superset; |
| 1225 | } |
| 1226 | |
| 1227 | static int vmx_restore_vmx_basic(struct vcpu_vmx *vmx, u64 data) |
| 1228 | { |
| 1229 | const u64 feature_and_reserved = |
| 1230 | /* feature (except bit 48; see below) */ |
| 1231 | BIT_ULL(49) | BIT_ULL(54) | BIT_ULL(55) | |
| 1232 | /* reserved */ |
| 1233 | BIT_ULL(31) | GENMASK_ULL(47, 45) | GENMASK_ULL(63, 56); |
| 1234 | u64 vmx_basic = vmx->nested.msrs.basic; |
| 1235 | |
| 1236 | if (!is_bitwise_subset(vmx_basic, data, feature_and_reserved)) |
| 1237 | return -EINVAL; |
| 1238 | |
| 1239 | /* |
| 1240 | * KVM does not emulate a version of VMX that constrains physical |
| 1241 | * addresses of VMX structures (e.g. VMCS) to 32-bits. |
| 1242 | */ |
| 1243 | if (data & BIT_ULL(48)) |
| 1244 | return -EINVAL; |
| 1245 | |
| 1246 | if (vmx_basic_vmcs_revision_id(vmx_basic) != |
| 1247 | vmx_basic_vmcs_revision_id(data)) |
| 1248 | return -EINVAL; |
| 1249 | |
| 1250 | if (vmx_basic_vmcs_size(vmx_basic) > vmx_basic_vmcs_size(data)) |
| 1251 | return -EINVAL; |
| 1252 | |
| 1253 | vmx->nested.msrs.basic = data; |
| 1254 | return 0; |
| 1255 | } |
| 1256 | |
| 1257 | static int |
| 1258 | vmx_restore_control_msr(struct vcpu_vmx *vmx, u32 msr_index, u64 data) |
| 1259 | { |
| 1260 | u64 supported; |
| 1261 | u32 *lowp, *highp; |
| 1262 | |
| 1263 | switch (msr_index) { |
| 1264 | case MSR_IA32_VMX_TRUE_PINBASED_CTLS: |
| 1265 | lowp = &vmx->nested.msrs.pinbased_ctls_low; |
| 1266 | highp = &vmx->nested.msrs.pinbased_ctls_high; |
| 1267 | break; |
| 1268 | case MSR_IA32_VMX_TRUE_PROCBASED_CTLS: |
| 1269 | lowp = &vmx->nested.msrs.procbased_ctls_low; |
| 1270 | highp = &vmx->nested.msrs.procbased_ctls_high; |
| 1271 | break; |
| 1272 | case MSR_IA32_VMX_TRUE_EXIT_CTLS: |
| 1273 | lowp = &vmx->nested.msrs.exit_ctls_low; |
| 1274 | highp = &vmx->nested.msrs.exit_ctls_high; |
| 1275 | break; |
| 1276 | case MSR_IA32_VMX_TRUE_ENTRY_CTLS: |
| 1277 | lowp = &vmx->nested.msrs.entry_ctls_low; |
| 1278 | highp = &vmx->nested.msrs.entry_ctls_high; |
| 1279 | break; |
| 1280 | case MSR_IA32_VMX_PROCBASED_CTLS2: |
| 1281 | lowp = &vmx->nested.msrs.secondary_ctls_low; |
| 1282 | highp = &vmx->nested.msrs.secondary_ctls_high; |
| 1283 | break; |
| 1284 | default: |
| 1285 | BUG(); |
| 1286 | } |
| 1287 | |
| 1288 | supported = vmx_control_msr(*lowp, *highp); |
| 1289 | |
| 1290 | /* Check must-be-1 bits are still 1. */ |
| 1291 | if (!is_bitwise_subset(data, supported, GENMASK_ULL(31, 0))) |
| 1292 | return -EINVAL; |
| 1293 | |
| 1294 | /* Check must-be-0 bits are still 0. */ |
| 1295 | if (!is_bitwise_subset(supported, data, GENMASK_ULL(63, 32))) |
| 1296 | return -EINVAL; |
| 1297 | |
| 1298 | *lowp = data; |
| 1299 | *highp = data >> 32; |
| 1300 | return 0; |
| 1301 | } |
| 1302 | |
| 1303 | static int vmx_restore_vmx_misc(struct vcpu_vmx *vmx, u64 data) |
| 1304 | { |
| 1305 | const u64 feature_and_reserved_bits = |
| 1306 | /* feature */ |
| 1307 | BIT_ULL(5) | GENMASK_ULL(8, 6) | BIT_ULL(14) | BIT_ULL(15) | |
| 1308 | BIT_ULL(28) | BIT_ULL(29) | BIT_ULL(30) | |
| 1309 | /* reserved */ |
| 1310 | GENMASK_ULL(13, 9) | BIT_ULL(31); |
| 1311 | u64 vmx_misc; |
| 1312 | |
| 1313 | vmx_misc = vmx_control_msr(vmx->nested.msrs.misc_low, |
| 1314 | vmx->nested.msrs.misc_high); |
| 1315 | |
| 1316 | if (!is_bitwise_subset(vmx_misc, data, feature_and_reserved_bits)) |
| 1317 | return -EINVAL; |
| 1318 | |
| 1319 | if ((vmx->nested.msrs.pinbased_ctls_high & |
| 1320 | PIN_BASED_VMX_PREEMPTION_TIMER) && |
| 1321 | vmx_misc_preemption_timer_rate(data) != |
| 1322 | vmx_misc_preemption_timer_rate(vmx_misc)) |
| 1323 | return -EINVAL; |
| 1324 | |
| 1325 | if (vmx_misc_cr3_count(data) > vmx_misc_cr3_count(vmx_misc)) |
| 1326 | return -EINVAL; |
| 1327 | |
| 1328 | if (vmx_misc_max_msr(data) > vmx_misc_max_msr(vmx_misc)) |
| 1329 | return -EINVAL; |
| 1330 | |
| 1331 | if (vmx_misc_mseg_revid(data) != vmx_misc_mseg_revid(vmx_misc)) |
| 1332 | return -EINVAL; |
| 1333 | |
| 1334 | vmx->nested.msrs.misc_low = data; |
| 1335 | vmx->nested.msrs.misc_high = data >> 32; |
| 1336 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1337 | return 0; |
| 1338 | } |
| 1339 | |
| 1340 | static int vmx_restore_vmx_ept_vpid_cap(struct vcpu_vmx *vmx, u64 data) |
| 1341 | { |
| 1342 | u64 vmx_ept_vpid_cap; |
| 1343 | |
| 1344 | vmx_ept_vpid_cap = vmx_control_msr(vmx->nested.msrs.ept_caps, |
| 1345 | vmx->nested.msrs.vpid_caps); |
| 1346 | |
| 1347 | /* Every bit is either reserved or a feature bit. */ |
| 1348 | if (!is_bitwise_subset(vmx_ept_vpid_cap, data, -1ULL)) |
| 1349 | return -EINVAL; |
| 1350 | |
| 1351 | vmx->nested.msrs.ept_caps = data; |
| 1352 | vmx->nested.msrs.vpid_caps = data >> 32; |
| 1353 | return 0; |
| 1354 | } |
| 1355 | |
| 1356 | static int vmx_restore_fixed0_msr(struct vcpu_vmx *vmx, u32 msr_index, u64 data) |
| 1357 | { |
| 1358 | u64 *msr; |
| 1359 | |
| 1360 | switch (msr_index) { |
| 1361 | case MSR_IA32_VMX_CR0_FIXED0: |
| 1362 | msr = &vmx->nested.msrs.cr0_fixed0; |
| 1363 | break; |
| 1364 | case MSR_IA32_VMX_CR4_FIXED0: |
| 1365 | msr = &vmx->nested.msrs.cr4_fixed0; |
| 1366 | break; |
| 1367 | default: |
| 1368 | BUG(); |
| 1369 | } |
| 1370 | |
| 1371 | /* |
| 1372 | * 1 bits (which indicates bits which "must-be-1" during VMX operation) |
| 1373 | * must be 1 in the restored value. |
| 1374 | */ |
| 1375 | if (!is_bitwise_subset(data, *msr, -1ULL)) |
| 1376 | return -EINVAL; |
| 1377 | |
| 1378 | *msr = data; |
| 1379 | return 0; |
| 1380 | } |
| 1381 | |
| 1382 | /* |
| 1383 | * Called when userspace is restoring VMX MSRs. |
| 1384 | * |
| 1385 | * Returns 0 on success, non-0 otherwise. |
| 1386 | */ |
| 1387 | int vmx_set_vmx_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 data) |
| 1388 | { |
| 1389 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 1390 | |
| 1391 | /* |
| 1392 | * Don't allow changes to the VMX capability MSRs while the vCPU |
| 1393 | * is in VMX operation. |
| 1394 | */ |
| 1395 | if (vmx->nested.vmxon) |
| 1396 | return -EBUSY; |
| 1397 | |
| 1398 | switch (msr_index) { |
| 1399 | case MSR_IA32_VMX_BASIC: |
| 1400 | return vmx_restore_vmx_basic(vmx, data); |
| 1401 | case MSR_IA32_VMX_PINBASED_CTLS: |
| 1402 | case MSR_IA32_VMX_PROCBASED_CTLS: |
| 1403 | case MSR_IA32_VMX_EXIT_CTLS: |
| 1404 | case MSR_IA32_VMX_ENTRY_CTLS: |
| 1405 | /* |
| 1406 | * The "non-true" VMX capability MSRs are generated from the |
| 1407 | * "true" MSRs, so we do not support restoring them directly. |
| 1408 | * |
| 1409 | * If userspace wants to emulate VMX_BASIC[55]=0, userspace |
| 1410 | * should restore the "true" MSRs with the must-be-1 bits |
| 1411 | * set according to the SDM Vol 3. A.2 "RESERVED CONTROLS AND |
| 1412 | * DEFAULT SETTINGS". |
| 1413 | */ |
| 1414 | return -EINVAL; |
| 1415 | case MSR_IA32_VMX_TRUE_PINBASED_CTLS: |
| 1416 | case MSR_IA32_VMX_TRUE_PROCBASED_CTLS: |
| 1417 | case MSR_IA32_VMX_TRUE_EXIT_CTLS: |
| 1418 | case MSR_IA32_VMX_TRUE_ENTRY_CTLS: |
| 1419 | case MSR_IA32_VMX_PROCBASED_CTLS2: |
| 1420 | return vmx_restore_control_msr(vmx, msr_index, data); |
| 1421 | case MSR_IA32_VMX_MISC: |
| 1422 | return vmx_restore_vmx_misc(vmx, data); |
| 1423 | case MSR_IA32_VMX_CR0_FIXED0: |
| 1424 | case MSR_IA32_VMX_CR4_FIXED0: |
| 1425 | return vmx_restore_fixed0_msr(vmx, msr_index, data); |
| 1426 | case MSR_IA32_VMX_CR0_FIXED1: |
| 1427 | case MSR_IA32_VMX_CR4_FIXED1: |
| 1428 | /* |
| 1429 | * These MSRs are generated based on the vCPU's CPUID, so we |
| 1430 | * do not support restoring them directly. |
| 1431 | */ |
| 1432 | return -EINVAL; |
| 1433 | case MSR_IA32_VMX_EPT_VPID_CAP: |
| 1434 | return vmx_restore_vmx_ept_vpid_cap(vmx, data); |
| 1435 | case MSR_IA32_VMX_VMCS_ENUM: |
| 1436 | vmx->nested.msrs.vmcs_enum = data; |
| 1437 | return 0; |
Paolo Bonzini | e8a70bd | 2019-07-02 14:40:40 +0200 | [diff] [blame] | 1438 | case MSR_IA32_VMX_VMFUNC: |
| 1439 | if (data & ~vmx->nested.msrs.vmfunc_controls) |
| 1440 | return -EINVAL; |
| 1441 | vmx->nested.msrs.vmfunc_controls = data; |
| 1442 | return 0; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1443 | default: |
| 1444 | /* |
| 1445 | * The rest of the VMX capability MSRs do not support restore. |
| 1446 | */ |
| 1447 | return -EINVAL; |
| 1448 | } |
| 1449 | } |
| 1450 | |
| 1451 | /* Returns 0 on success, non-0 otherwise. */ |
| 1452 | int vmx_get_vmx_msr(struct nested_vmx_msrs *msrs, u32 msr_index, u64 *pdata) |
| 1453 | { |
| 1454 | switch (msr_index) { |
| 1455 | case MSR_IA32_VMX_BASIC: |
| 1456 | *pdata = msrs->basic; |
| 1457 | break; |
| 1458 | case MSR_IA32_VMX_TRUE_PINBASED_CTLS: |
| 1459 | case MSR_IA32_VMX_PINBASED_CTLS: |
| 1460 | *pdata = vmx_control_msr( |
| 1461 | msrs->pinbased_ctls_low, |
| 1462 | msrs->pinbased_ctls_high); |
| 1463 | if (msr_index == MSR_IA32_VMX_PINBASED_CTLS) |
| 1464 | *pdata |= PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR; |
| 1465 | break; |
| 1466 | case MSR_IA32_VMX_TRUE_PROCBASED_CTLS: |
| 1467 | case MSR_IA32_VMX_PROCBASED_CTLS: |
| 1468 | *pdata = vmx_control_msr( |
| 1469 | msrs->procbased_ctls_low, |
| 1470 | msrs->procbased_ctls_high); |
| 1471 | if (msr_index == MSR_IA32_VMX_PROCBASED_CTLS) |
| 1472 | *pdata |= CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR; |
| 1473 | break; |
| 1474 | case MSR_IA32_VMX_TRUE_EXIT_CTLS: |
| 1475 | case MSR_IA32_VMX_EXIT_CTLS: |
| 1476 | *pdata = vmx_control_msr( |
| 1477 | msrs->exit_ctls_low, |
| 1478 | msrs->exit_ctls_high); |
| 1479 | if (msr_index == MSR_IA32_VMX_EXIT_CTLS) |
| 1480 | *pdata |= VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR; |
| 1481 | break; |
| 1482 | case MSR_IA32_VMX_TRUE_ENTRY_CTLS: |
| 1483 | case MSR_IA32_VMX_ENTRY_CTLS: |
| 1484 | *pdata = vmx_control_msr( |
| 1485 | msrs->entry_ctls_low, |
| 1486 | msrs->entry_ctls_high); |
| 1487 | if (msr_index == MSR_IA32_VMX_ENTRY_CTLS) |
| 1488 | *pdata |= VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR; |
| 1489 | break; |
| 1490 | case MSR_IA32_VMX_MISC: |
| 1491 | *pdata = vmx_control_msr( |
| 1492 | msrs->misc_low, |
| 1493 | msrs->misc_high); |
| 1494 | break; |
| 1495 | case MSR_IA32_VMX_CR0_FIXED0: |
| 1496 | *pdata = msrs->cr0_fixed0; |
| 1497 | break; |
| 1498 | case MSR_IA32_VMX_CR0_FIXED1: |
| 1499 | *pdata = msrs->cr0_fixed1; |
| 1500 | break; |
| 1501 | case MSR_IA32_VMX_CR4_FIXED0: |
| 1502 | *pdata = msrs->cr4_fixed0; |
| 1503 | break; |
| 1504 | case MSR_IA32_VMX_CR4_FIXED1: |
| 1505 | *pdata = msrs->cr4_fixed1; |
| 1506 | break; |
| 1507 | case MSR_IA32_VMX_VMCS_ENUM: |
| 1508 | *pdata = msrs->vmcs_enum; |
| 1509 | break; |
| 1510 | case MSR_IA32_VMX_PROCBASED_CTLS2: |
| 1511 | *pdata = vmx_control_msr( |
| 1512 | msrs->secondary_ctls_low, |
| 1513 | msrs->secondary_ctls_high); |
| 1514 | break; |
| 1515 | case MSR_IA32_VMX_EPT_VPID_CAP: |
| 1516 | *pdata = msrs->ept_caps | |
| 1517 | ((u64)msrs->vpid_caps << 32); |
| 1518 | break; |
| 1519 | case MSR_IA32_VMX_VMFUNC: |
| 1520 | *pdata = msrs->vmfunc_controls; |
| 1521 | break; |
| 1522 | default: |
| 1523 | return 1; |
| 1524 | } |
| 1525 | |
| 1526 | return 0; |
| 1527 | } |
| 1528 | |
| 1529 | /* |
Sean Christopherson | fadcead | 2019-05-07 08:36:23 -0700 | [diff] [blame] | 1530 | * Copy the writable VMCS shadow fields back to the VMCS12, in case they have |
| 1531 | * been modified by the L1 guest. Note, "writable" in this context means |
| 1532 | * "writable by the guest", i.e. tagged SHADOW_FIELD_RW; the set of |
| 1533 | * fields tagged SHADOW_FIELD_RO may or may not align with the "read-only" |
| 1534 | * VM-exit information fields (which are actually writable if the vCPU is |
| 1535 | * configured to support "VMWRITE to any supported field in the VMCS"). |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1536 | */ |
| 1537 | static void copy_shadow_to_vmcs12(struct vcpu_vmx *vmx) |
| 1538 | { |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1539 | struct vmcs *shadow_vmcs = vmx->vmcs01.shadow_vmcs; |
Sean Christopherson | fadcead | 2019-05-07 08:36:23 -0700 | [diff] [blame] | 1540 | struct vmcs12 *vmcs12 = get_vmcs12(&vmx->vcpu); |
Sean Christopherson | 1c6f0b4 | 2019-05-07 08:36:25 -0700 | [diff] [blame] | 1541 | struct shadow_vmcs_field field; |
| 1542 | unsigned long val; |
Sean Christopherson | fadcead | 2019-05-07 08:36:23 -0700 | [diff] [blame] | 1543 | int i; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1544 | |
Paolo Bonzini | 88dddc1 | 2019-07-19 18:41:10 +0200 | [diff] [blame] | 1545 | if (WARN_ON(!shadow_vmcs)) |
| 1546 | return; |
| 1547 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1548 | preempt_disable(); |
| 1549 | |
| 1550 | vmcs_load(shadow_vmcs); |
| 1551 | |
Sean Christopherson | fadcead | 2019-05-07 08:36:23 -0700 | [diff] [blame] | 1552 | for (i = 0; i < max_shadow_read_write_fields; i++) { |
| 1553 | field = shadow_read_write_fields[i]; |
Sean Christopherson | 1c6f0b4 | 2019-05-07 08:36:25 -0700 | [diff] [blame] | 1554 | val = __vmcs_readl(field.encoding); |
| 1555 | vmcs12_write_any(vmcs12, field.encoding, field.offset, val); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1556 | } |
| 1557 | |
| 1558 | vmcs_clear(shadow_vmcs); |
| 1559 | vmcs_load(vmx->loaded_vmcs->vmcs); |
| 1560 | |
| 1561 | preempt_enable(); |
| 1562 | } |
| 1563 | |
| 1564 | static void copy_vmcs12_to_shadow(struct vcpu_vmx *vmx) |
| 1565 | { |
Sean Christopherson | 1c6f0b4 | 2019-05-07 08:36:25 -0700 | [diff] [blame] | 1566 | const struct shadow_vmcs_field *fields[] = { |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1567 | shadow_read_write_fields, |
| 1568 | shadow_read_only_fields |
| 1569 | }; |
| 1570 | const int max_fields[] = { |
| 1571 | max_shadow_read_write_fields, |
| 1572 | max_shadow_read_only_fields |
| 1573 | }; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1574 | struct vmcs *shadow_vmcs = vmx->vmcs01.shadow_vmcs; |
Sean Christopherson | 1c6f0b4 | 2019-05-07 08:36:25 -0700 | [diff] [blame] | 1575 | struct vmcs12 *vmcs12 = get_vmcs12(&vmx->vcpu); |
| 1576 | struct shadow_vmcs_field field; |
| 1577 | unsigned long val; |
| 1578 | int i, q; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1579 | |
Paolo Bonzini | 88dddc1 | 2019-07-19 18:41:10 +0200 | [diff] [blame] | 1580 | if (WARN_ON(!shadow_vmcs)) |
| 1581 | return; |
| 1582 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1583 | vmcs_load(shadow_vmcs); |
| 1584 | |
| 1585 | for (q = 0; q < ARRAY_SIZE(fields); q++) { |
| 1586 | for (i = 0; i < max_fields[q]; i++) { |
| 1587 | field = fields[q][i]; |
Sean Christopherson | 1c6f0b4 | 2019-05-07 08:36:25 -0700 | [diff] [blame] | 1588 | val = vmcs12_read_any(vmcs12, field.encoding, |
| 1589 | field.offset); |
| 1590 | __vmcs_writel(field.encoding, val); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1591 | } |
| 1592 | } |
| 1593 | |
| 1594 | vmcs_clear(shadow_vmcs); |
| 1595 | vmcs_load(vmx->loaded_vmcs->vmcs); |
| 1596 | } |
| 1597 | |
Vitaly Kuznetsov | d6bf71a | 2021-05-26 15:20:22 +0200 | [diff] [blame] | 1598 | 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] | 1599 | { |
| 1600 | struct vmcs12 *vmcs12 = vmx->nested.cached_vmcs12; |
| 1601 | struct hv_enlightened_vmcs *evmcs = vmx->nested.hv_evmcs; |
| 1602 | |
| 1603 | /* HV_VMX_ENLIGHTENED_CLEAN_FIELD_NONE */ |
| 1604 | vmcs12->tpr_threshold = evmcs->tpr_threshold; |
| 1605 | vmcs12->guest_rip = evmcs->guest_rip; |
| 1606 | |
Vitaly Kuznetsov | d6bf71a | 2021-05-26 15:20:22 +0200 | [diff] [blame] | 1607 | if (unlikely(!(hv_clean_fields & |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1608 | HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_BASIC))) { |
| 1609 | vmcs12->guest_rsp = evmcs->guest_rsp; |
| 1610 | vmcs12->guest_rflags = evmcs->guest_rflags; |
| 1611 | vmcs12->guest_interruptibility_info = |
| 1612 | evmcs->guest_interruptibility_info; |
| 1613 | } |
| 1614 | |
Vitaly Kuznetsov | d6bf71a | 2021-05-26 15:20:22 +0200 | [diff] [blame] | 1615 | if (unlikely(!(hv_clean_fields & |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1616 | HV_VMX_ENLIGHTENED_CLEAN_FIELD_CONTROL_PROC))) { |
| 1617 | vmcs12->cpu_based_vm_exec_control = |
| 1618 | evmcs->cpu_based_vm_exec_control; |
| 1619 | } |
| 1620 | |
Vitaly Kuznetsov | d6bf71a | 2021-05-26 15:20:22 +0200 | [diff] [blame] | 1621 | if (unlikely(!(hv_clean_fields & |
Vitaly Kuznetsov | f9bc522 | 2019-06-13 13:35:02 +0200 | [diff] [blame] | 1622 | HV_VMX_ENLIGHTENED_CLEAN_FIELD_CONTROL_EXCPN))) { |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1623 | vmcs12->exception_bitmap = evmcs->exception_bitmap; |
| 1624 | } |
| 1625 | |
Vitaly Kuznetsov | d6bf71a | 2021-05-26 15:20:22 +0200 | [diff] [blame] | 1626 | if (unlikely(!(hv_clean_fields & |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1627 | HV_VMX_ENLIGHTENED_CLEAN_FIELD_CONTROL_ENTRY))) { |
| 1628 | vmcs12->vm_entry_controls = evmcs->vm_entry_controls; |
| 1629 | } |
| 1630 | |
Vitaly Kuznetsov | d6bf71a | 2021-05-26 15:20:22 +0200 | [diff] [blame] | 1631 | if (unlikely(!(hv_clean_fields & |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1632 | HV_VMX_ENLIGHTENED_CLEAN_FIELD_CONTROL_EVENT))) { |
| 1633 | vmcs12->vm_entry_intr_info_field = |
| 1634 | evmcs->vm_entry_intr_info_field; |
| 1635 | vmcs12->vm_entry_exception_error_code = |
| 1636 | evmcs->vm_entry_exception_error_code; |
| 1637 | vmcs12->vm_entry_instruction_len = |
| 1638 | evmcs->vm_entry_instruction_len; |
| 1639 | } |
| 1640 | |
Vitaly Kuznetsov | d6bf71a | 2021-05-26 15:20:22 +0200 | [diff] [blame] | 1641 | if (unlikely(!(hv_clean_fields & |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1642 | HV_VMX_ENLIGHTENED_CLEAN_FIELD_HOST_GRP1))) { |
| 1643 | vmcs12->host_ia32_pat = evmcs->host_ia32_pat; |
| 1644 | vmcs12->host_ia32_efer = evmcs->host_ia32_efer; |
| 1645 | vmcs12->host_cr0 = evmcs->host_cr0; |
| 1646 | vmcs12->host_cr3 = evmcs->host_cr3; |
| 1647 | vmcs12->host_cr4 = evmcs->host_cr4; |
| 1648 | vmcs12->host_ia32_sysenter_esp = evmcs->host_ia32_sysenter_esp; |
| 1649 | vmcs12->host_ia32_sysenter_eip = evmcs->host_ia32_sysenter_eip; |
| 1650 | vmcs12->host_rip = evmcs->host_rip; |
| 1651 | vmcs12->host_ia32_sysenter_cs = evmcs->host_ia32_sysenter_cs; |
| 1652 | vmcs12->host_es_selector = evmcs->host_es_selector; |
| 1653 | vmcs12->host_cs_selector = evmcs->host_cs_selector; |
| 1654 | vmcs12->host_ss_selector = evmcs->host_ss_selector; |
| 1655 | vmcs12->host_ds_selector = evmcs->host_ds_selector; |
| 1656 | vmcs12->host_fs_selector = evmcs->host_fs_selector; |
| 1657 | vmcs12->host_gs_selector = evmcs->host_gs_selector; |
| 1658 | vmcs12->host_tr_selector = evmcs->host_tr_selector; |
| 1659 | } |
| 1660 | |
Vitaly Kuznetsov | d6bf71a | 2021-05-26 15:20:22 +0200 | [diff] [blame] | 1661 | if (unlikely(!(hv_clean_fields & |
Vitaly Kuznetsov | f9bc522 | 2019-06-13 13:35:02 +0200 | [diff] [blame] | 1662 | HV_VMX_ENLIGHTENED_CLEAN_FIELD_CONTROL_GRP1))) { |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1663 | vmcs12->pin_based_vm_exec_control = |
| 1664 | evmcs->pin_based_vm_exec_control; |
| 1665 | vmcs12->vm_exit_controls = evmcs->vm_exit_controls; |
| 1666 | vmcs12->secondary_vm_exec_control = |
| 1667 | evmcs->secondary_vm_exec_control; |
| 1668 | } |
| 1669 | |
Vitaly Kuznetsov | d6bf71a | 2021-05-26 15:20:22 +0200 | [diff] [blame] | 1670 | if (unlikely(!(hv_clean_fields & |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1671 | HV_VMX_ENLIGHTENED_CLEAN_FIELD_IO_BITMAP))) { |
| 1672 | vmcs12->io_bitmap_a = evmcs->io_bitmap_a; |
| 1673 | vmcs12->io_bitmap_b = evmcs->io_bitmap_b; |
| 1674 | } |
| 1675 | |
Vitaly Kuznetsov | d6bf71a | 2021-05-26 15:20:22 +0200 | [diff] [blame] | 1676 | if (unlikely(!(hv_clean_fields & |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1677 | HV_VMX_ENLIGHTENED_CLEAN_FIELD_MSR_BITMAP))) { |
| 1678 | vmcs12->msr_bitmap = evmcs->msr_bitmap; |
| 1679 | } |
| 1680 | |
Vitaly Kuznetsov | d6bf71a | 2021-05-26 15:20:22 +0200 | [diff] [blame] | 1681 | if (unlikely(!(hv_clean_fields & |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1682 | HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP2))) { |
| 1683 | vmcs12->guest_es_base = evmcs->guest_es_base; |
| 1684 | vmcs12->guest_cs_base = evmcs->guest_cs_base; |
| 1685 | vmcs12->guest_ss_base = evmcs->guest_ss_base; |
| 1686 | vmcs12->guest_ds_base = evmcs->guest_ds_base; |
| 1687 | vmcs12->guest_fs_base = evmcs->guest_fs_base; |
| 1688 | vmcs12->guest_gs_base = evmcs->guest_gs_base; |
| 1689 | vmcs12->guest_ldtr_base = evmcs->guest_ldtr_base; |
| 1690 | vmcs12->guest_tr_base = evmcs->guest_tr_base; |
| 1691 | vmcs12->guest_gdtr_base = evmcs->guest_gdtr_base; |
| 1692 | vmcs12->guest_idtr_base = evmcs->guest_idtr_base; |
| 1693 | vmcs12->guest_es_limit = evmcs->guest_es_limit; |
| 1694 | vmcs12->guest_cs_limit = evmcs->guest_cs_limit; |
| 1695 | vmcs12->guest_ss_limit = evmcs->guest_ss_limit; |
| 1696 | vmcs12->guest_ds_limit = evmcs->guest_ds_limit; |
| 1697 | vmcs12->guest_fs_limit = evmcs->guest_fs_limit; |
| 1698 | vmcs12->guest_gs_limit = evmcs->guest_gs_limit; |
| 1699 | vmcs12->guest_ldtr_limit = evmcs->guest_ldtr_limit; |
| 1700 | vmcs12->guest_tr_limit = evmcs->guest_tr_limit; |
| 1701 | vmcs12->guest_gdtr_limit = evmcs->guest_gdtr_limit; |
| 1702 | vmcs12->guest_idtr_limit = evmcs->guest_idtr_limit; |
| 1703 | vmcs12->guest_es_ar_bytes = evmcs->guest_es_ar_bytes; |
| 1704 | vmcs12->guest_cs_ar_bytes = evmcs->guest_cs_ar_bytes; |
| 1705 | vmcs12->guest_ss_ar_bytes = evmcs->guest_ss_ar_bytes; |
| 1706 | vmcs12->guest_ds_ar_bytes = evmcs->guest_ds_ar_bytes; |
| 1707 | vmcs12->guest_fs_ar_bytes = evmcs->guest_fs_ar_bytes; |
| 1708 | vmcs12->guest_gs_ar_bytes = evmcs->guest_gs_ar_bytes; |
| 1709 | vmcs12->guest_ldtr_ar_bytes = evmcs->guest_ldtr_ar_bytes; |
| 1710 | vmcs12->guest_tr_ar_bytes = evmcs->guest_tr_ar_bytes; |
| 1711 | vmcs12->guest_es_selector = evmcs->guest_es_selector; |
| 1712 | vmcs12->guest_cs_selector = evmcs->guest_cs_selector; |
| 1713 | vmcs12->guest_ss_selector = evmcs->guest_ss_selector; |
| 1714 | vmcs12->guest_ds_selector = evmcs->guest_ds_selector; |
| 1715 | vmcs12->guest_fs_selector = evmcs->guest_fs_selector; |
| 1716 | vmcs12->guest_gs_selector = evmcs->guest_gs_selector; |
| 1717 | vmcs12->guest_ldtr_selector = evmcs->guest_ldtr_selector; |
| 1718 | vmcs12->guest_tr_selector = evmcs->guest_tr_selector; |
| 1719 | } |
| 1720 | |
Vitaly Kuznetsov | d6bf71a | 2021-05-26 15:20:22 +0200 | [diff] [blame] | 1721 | if (unlikely(!(hv_clean_fields & |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1722 | HV_VMX_ENLIGHTENED_CLEAN_FIELD_CONTROL_GRP2))) { |
| 1723 | vmcs12->tsc_offset = evmcs->tsc_offset; |
| 1724 | vmcs12->virtual_apic_page_addr = evmcs->virtual_apic_page_addr; |
| 1725 | vmcs12->xss_exit_bitmap = evmcs->xss_exit_bitmap; |
| 1726 | } |
| 1727 | |
Vitaly Kuznetsov | d6bf71a | 2021-05-26 15:20:22 +0200 | [diff] [blame] | 1728 | if (unlikely(!(hv_clean_fields & |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1729 | HV_VMX_ENLIGHTENED_CLEAN_FIELD_CRDR))) { |
| 1730 | vmcs12->cr0_guest_host_mask = evmcs->cr0_guest_host_mask; |
| 1731 | vmcs12->cr4_guest_host_mask = evmcs->cr4_guest_host_mask; |
| 1732 | vmcs12->cr0_read_shadow = evmcs->cr0_read_shadow; |
| 1733 | vmcs12->cr4_read_shadow = evmcs->cr4_read_shadow; |
| 1734 | vmcs12->guest_cr0 = evmcs->guest_cr0; |
| 1735 | vmcs12->guest_cr3 = evmcs->guest_cr3; |
| 1736 | vmcs12->guest_cr4 = evmcs->guest_cr4; |
| 1737 | vmcs12->guest_dr7 = evmcs->guest_dr7; |
| 1738 | } |
| 1739 | |
Vitaly Kuznetsov | d6bf71a | 2021-05-26 15:20:22 +0200 | [diff] [blame] | 1740 | if (unlikely(!(hv_clean_fields & |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1741 | HV_VMX_ENLIGHTENED_CLEAN_FIELD_HOST_POINTER))) { |
| 1742 | vmcs12->host_fs_base = evmcs->host_fs_base; |
| 1743 | vmcs12->host_gs_base = evmcs->host_gs_base; |
| 1744 | vmcs12->host_tr_base = evmcs->host_tr_base; |
| 1745 | vmcs12->host_gdtr_base = evmcs->host_gdtr_base; |
| 1746 | vmcs12->host_idtr_base = evmcs->host_idtr_base; |
| 1747 | vmcs12->host_rsp = evmcs->host_rsp; |
| 1748 | } |
| 1749 | |
Vitaly Kuznetsov | d6bf71a | 2021-05-26 15:20:22 +0200 | [diff] [blame] | 1750 | if (unlikely(!(hv_clean_fields & |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1751 | HV_VMX_ENLIGHTENED_CLEAN_FIELD_CONTROL_XLAT))) { |
| 1752 | vmcs12->ept_pointer = evmcs->ept_pointer; |
| 1753 | vmcs12->virtual_processor_id = evmcs->virtual_processor_id; |
| 1754 | } |
| 1755 | |
Vitaly Kuznetsov | d6bf71a | 2021-05-26 15:20:22 +0200 | [diff] [blame] | 1756 | if (unlikely(!(hv_clean_fields & |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1757 | HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP1))) { |
| 1758 | vmcs12->vmcs_link_pointer = evmcs->vmcs_link_pointer; |
| 1759 | vmcs12->guest_ia32_debugctl = evmcs->guest_ia32_debugctl; |
| 1760 | vmcs12->guest_ia32_pat = evmcs->guest_ia32_pat; |
| 1761 | vmcs12->guest_ia32_efer = evmcs->guest_ia32_efer; |
| 1762 | vmcs12->guest_pdptr0 = evmcs->guest_pdptr0; |
| 1763 | vmcs12->guest_pdptr1 = evmcs->guest_pdptr1; |
| 1764 | vmcs12->guest_pdptr2 = evmcs->guest_pdptr2; |
| 1765 | vmcs12->guest_pdptr3 = evmcs->guest_pdptr3; |
| 1766 | vmcs12->guest_pending_dbg_exceptions = |
| 1767 | evmcs->guest_pending_dbg_exceptions; |
| 1768 | vmcs12->guest_sysenter_esp = evmcs->guest_sysenter_esp; |
| 1769 | vmcs12->guest_sysenter_eip = evmcs->guest_sysenter_eip; |
| 1770 | vmcs12->guest_bndcfgs = evmcs->guest_bndcfgs; |
| 1771 | vmcs12->guest_activity_state = evmcs->guest_activity_state; |
| 1772 | vmcs12->guest_sysenter_cs = evmcs->guest_sysenter_cs; |
| 1773 | } |
| 1774 | |
| 1775 | /* |
| 1776 | * Not used? |
| 1777 | * vmcs12->vm_exit_msr_store_addr = evmcs->vm_exit_msr_store_addr; |
| 1778 | * vmcs12->vm_exit_msr_load_addr = evmcs->vm_exit_msr_load_addr; |
| 1779 | * vmcs12->vm_entry_msr_load_addr = evmcs->vm_entry_msr_load_addr; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1780 | * vmcs12->page_fault_error_code_mask = |
| 1781 | * evmcs->page_fault_error_code_mask; |
| 1782 | * vmcs12->page_fault_error_code_match = |
| 1783 | * evmcs->page_fault_error_code_match; |
| 1784 | * vmcs12->cr3_target_count = evmcs->cr3_target_count; |
| 1785 | * vmcs12->vm_exit_msr_store_count = evmcs->vm_exit_msr_store_count; |
| 1786 | * vmcs12->vm_exit_msr_load_count = evmcs->vm_exit_msr_load_count; |
| 1787 | * vmcs12->vm_entry_msr_load_count = evmcs->vm_entry_msr_load_count; |
| 1788 | */ |
| 1789 | |
| 1790 | /* |
| 1791 | * Read only fields: |
| 1792 | * vmcs12->guest_physical_address = evmcs->guest_physical_address; |
| 1793 | * vmcs12->vm_instruction_error = evmcs->vm_instruction_error; |
| 1794 | * vmcs12->vm_exit_reason = evmcs->vm_exit_reason; |
| 1795 | * vmcs12->vm_exit_intr_info = evmcs->vm_exit_intr_info; |
| 1796 | * vmcs12->vm_exit_intr_error_code = evmcs->vm_exit_intr_error_code; |
| 1797 | * vmcs12->idt_vectoring_info_field = evmcs->idt_vectoring_info_field; |
| 1798 | * vmcs12->idt_vectoring_error_code = evmcs->idt_vectoring_error_code; |
| 1799 | * vmcs12->vm_exit_instruction_len = evmcs->vm_exit_instruction_len; |
| 1800 | * vmcs12->vmx_instruction_info = evmcs->vmx_instruction_info; |
| 1801 | * vmcs12->exit_qualification = evmcs->exit_qualification; |
| 1802 | * vmcs12->guest_linear_address = evmcs->guest_linear_address; |
| 1803 | * |
| 1804 | * Not present in struct vmcs12: |
| 1805 | * vmcs12->exit_io_instruction_ecx = evmcs->exit_io_instruction_ecx; |
| 1806 | * vmcs12->exit_io_instruction_esi = evmcs->exit_io_instruction_esi; |
| 1807 | * vmcs12->exit_io_instruction_edi = evmcs->exit_io_instruction_edi; |
| 1808 | * vmcs12->exit_io_instruction_eip = evmcs->exit_io_instruction_eip; |
| 1809 | */ |
| 1810 | |
Vitaly Kuznetsov | 25641ca | 2021-05-26 15:20:19 +0200 | [diff] [blame] | 1811 | return; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1812 | } |
| 1813 | |
Vitaly Kuznetsov | 25641ca | 2021-05-26 15:20:19 +0200 | [diff] [blame] | 1814 | static void copy_vmcs12_to_enlightened(struct vcpu_vmx *vmx) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1815 | { |
| 1816 | struct vmcs12 *vmcs12 = vmx->nested.cached_vmcs12; |
| 1817 | struct hv_enlightened_vmcs *evmcs = vmx->nested.hv_evmcs; |
| 1818 | |
| 1819 | /* |
| 1820 | * Should not be changed by KVM: |
| 1821 | * |
| 1822 | * evmcs->host_es_selector = vmcs12->host_es_selector; |
| 1823 | * evmcs->host_cs_selector = vmcs12->host_cs_selector; |
| 1824 | * evmcs->host_ss_selector = vmcs12->host_ss_selector; |
| 1825 | * evmcs->host_ds_selector = vmcs12->host_ds_selector; |
| 1826 | * evmcs->host_fs_selector = vmcs12->host_fs_selector; |
| 1827 | * evmcs->host_gs_selector = vmcs12->host_gs_selector; |
| 1828 | * evmcs->host_tr_selector = vmcs12->host_tr_selector; |
| 1829 | * evmcs->host_ia32_pat = vmcs12->host_ia32_pat; |
| 1830 | * evmcs->host_ia32_efer = vmcs12->host_ia32_efer; |
| 1831 | * evmcs->host_cr0 = vmcs12->host_cr0; |
| 1832 | * evmcs->host_cr3 = vmcs12->host_cr3; |
| 1833 | * evmcs->host_cr4 = vmcs12->host_cr4; |
| 1834 | * evmcs->host_ia32_sysenter_esp = vmcs12->host_ia32_sysenter_esp; |
| 1835 | * evmcs->host_ia32_sysenter_eip = vmcs12->host_ia32_sysenter_eip; |
| 1836 | * evmcs->host_rip = vmcs12->host_rip; |
| 1837 | * evmcs->host_ia32_sysenter_cs = vmcs12->host_ia32_sysenter_cs; |
| 1838 | * evmcs->host_fs_base = vmcs12->host_fs_base; |
| 1839 | * evmcs->host_gs_base = vmcs12->host_gs_base; |
| 1840 | * evmcs->host_tr_base = vmcs12->host_tr_base; |
| 1841 | * evmcs->host_gdtr_base = vmcs12->host_gdtr_base; |
| 1842 | * evmcs->host_idtr_base = vmcs12->host_idtr_base; |
| 1843 | * evmcs->host_rsp = vmcs12->host_rsp; |
Sean Christopherson | 3731905ef | 2019-05-07 08:36:27 -0700 | [diff] [blame] | 1844 | * sync_vmcs02_to_vmcs12() doesn't read these: |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1845 | * evmcs->io_bitmap_a = vmcs12->io_bitmap_a; |
| 1846 | * evmcs->io_bitmap_b = vmcs12->io_bitmap_b; |
| 1847 | * evmcs->msr_bitmap = vmcs12->msr_bitmap; |
| 1848 | * evmcs->ept_pointer = vmcs12->ept_pointer; |
| 1849 | * evmcs->xss_exit_bitmap = vmcs12->xss_exit_bitmap; |
| 1850 | * evmcs->vm_exit_msr_store_addr = vmcs12->vm_exit_msr_store_addr; |
| 1851 | * evmcs->vm_exit_msr_load_addr = vmcs12->vm_exit_msr_load_addr; |
| 1852 | * evmcs->vm_entry_msr_load_addr = vmcs12->vm_entry_msr_load_addr; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1853 | * evmcs->tpr_threshold = vmcs12->tpr_threshold; |
| 1854 | * evmcs->virtual_processor_id = vmcs12->virtual_processor_id; |
| 1855 | * evmcs->exception_bitmap = vmcs12->exception_bitmap; |
| 1856 | * evmcs->vmcs_link_pointer = vmcs12->vmcs_link_pointer; |
| 1857 | * evmcs->pin_based_vm_exec_control = vmcs12->pin_based_vm_exec_control; |
| 1858 | * evmcs->vm_exit_controls = vmcs12->vm_exit_controls; |
| 1859 | * evmcs->secondary_vm_exec_control = vmcs12->secondary_vm_exec_control; |
| 1860 | * evmcs->page_fault_error_code_mask = |
| 1861 | * vmcs12->page_fault_error_code_mask; |
| 1862 | * evmcs->page_fault_error_code_match = |
| 1863 | * vmcs12->page_fault_error_code_match; |
| 1864 | * evmcs->cr3_target_count = vmcs12->cr3_target_count; |
| 1865 | * evmcs->virtual_apic_page_addr = vmcs12->virtual_apic_page_addr; |
| 1866 | * evmcs->tsc_offset = vmcs12->tsc_offset; |
| 1867 | * evmcs->guest_ia32_debugctl = vmcs12->guest_ia32_debugctl; |
| 1868 | * evmcs->cr0_guest_host_mask = vmcs12->cr0_guest_host_mask; |
| 1869 | * evmcs->cr4_guest_host_mask = vmcs12->cr4_guest_host_mask; |
| 1870 | * evmcs->cr0_read_shadow = vmcs12->cr0_read_shadow; |
| 1871 | * evmcs->cr4_read_shadow = vmcs12->cr4_read_shadow; |
| 1872 | * evmcs->vm_exit_msr_store_count = vmcs12->vm_exit_msr_store_count; |
| 1873 | * evmcs->vm_exit_msr_load_count = vmcs12->vm_exit_msr_load_count; |
| 1874 | * evmcs->vm_entry_msr_load_count = vmcs12->vm_entry_msr_load_count; |
| 1875 | * |
| 1876 | * Not present in struct vmcs12: |
| 1877 | * evmcs->exit_io_instruction_ecx = vmcs12->exit_io_instruction_ecx; |
| 1878 | * evmcs->exit_io_instruction_esi = vmcs12->exit_io_instruction_esi; |
| 1879 | * evmcs->exit_io_instruction_edi = vmcs12->exit_io_instruction_edi; |
| 1880 | * evmcs->exit_io_instruction_eip = vmcs12->exit_io_instruction_eip; |
| 1881 | */ |
| 1882 | |
| 1883 | evmcs->guest_es_selector = vmcs12->guest_es_selector; |
| 1884 | evmcs->guest_cs_selector = vmcs12->guest_cs_selector; |
| 1885 | evmcs->guest_ss_selector = vmcs12->guest_ss_selector; |
| 1886 | evmcs->guest_ds_selector = vmcs12->guest_ds_selector; |
| 1887 | evmcs->guest_fs_selector = vmcs12->guest_fs_selector; |
| 1888 | evmcs->guest_gs_selector = vmcs12->guest_gs_selector; |
| 1889 | evmcs->guest_ldtr_selector = vmcs12->guest_ldtr_selector; |
| 1890 | evmcs->guest_tr_selector = vmcs12->guest_tr_selector; |
| 1891 | |
| 1892 | evmcs->guest_es_limit = vmcs12->guest_es_limit; |
| 1893 | evmcs->guest_cs_limit = vmcs12->guest_cs_limit; |
| 1894 | evmcs->guest_ss_limit = vmcs12->guest_ss_limit; |
| 1895 | evmcs->guest_ds_limit = vmcs12->guest_ds_limit; |
| 1896 | evmcs->guest_fs_limit = vmcs12->guest_fs_limit; |
| 1897 | evmcs->guest_gs_limit = vmcs12->guest_gs_limit; |
| 1898 | evmcs->guest_ldtr_limit = vmcs12->guest_ldtr_limit; |
| 1899 | evmcs->guest_tr_limit = vmcs12->guest_tr_limit; |
| 1900 | evmcs->guest_gdtr_limit = vmcs12->guest_gdtr_limit; |
| 1901 | evmcs->guest_idtr_limit = vmcs12->guest_idtr_limit; |
| 1902 | |
| 1903 | evmcs->guest_es_ar_bytes = vmcs12->guest_es_ar_bytes; |
| 1904 | evmcs->guest_cs_ar_bytes = vmcs12->guest_cs_ar_bytes; |
| 1905 | evmcs->guest_ss_ar_bytes = vmcs12->guest_ss_ar_bytes; |
| 1906 | evmcs->guest_ds_ar_bytes = vmcs12->guest_ds_ar_bytes; |
| 1907 | evmcs->guest_fs_ar_bytes = vmcs12->guest_fs_ar_bytes; |
| 1908 | evmcs->guest_gs_ar_bytes = vmcs12->guest_gs_ar_bytes; |
| 1909 | evmcs->guest_ldtr_ar_bytes = vmcs12->guest_ldtr_ar_bytes; |
| 1910 | evmcs->guest_tr_ar_bytes = vmcs12->guest_tr_ar_bytes; |
| 1911 | |
| 1912 | evmcs->guest_es_base = vmcs12->guest_es_base; |
| 1913 | evmcs->guest_cs_base = vmcs12->guest_cs_base; |
| 1914 | evmcs->guest_ss_base = vmcs12->guest_ss_base; |
| 1915 | evmcs->guest_ds_base = vmcs12->guest_ds_base; |
| 1916 | evmcs->guest_fs_base = vmcs12->guest_fs_base; |
| 1917 | evmcs->guest_gs_base = vmcs12->guest_gs_base; |
| 1918 | evmcs->guest_ldtr_base = vmcs12->guest_ldtr_base; |
| 1919 | evmcs->guest_tr_base = vmcs12->guest_tr_base; |
| 1920 | evmcs->guest_gdtr_base = vmcs12->guest_gdtr_base; |
| 1921 | evmcs->guest_idtr_base = vmcs12->guest_idtr_base; |
| 1922 | |
| 1923 | evmcs->guest_ia32_pat = vmcs12->guest_ia32_pat; |
| 1924 | evmcs->guest_ia32_efer = vmcs12->guest_ia32_efer; |
| 1925 | |
| 1926 | evmcs->guest_pdptr0 = vmcs12->guest_pdptr0; |
| 1927 | evmcs->guest_pdptr1 = vmcs12->guest_pdptr1; |
| 1928 | evmcs->guest_pdptr2 = vmcs12->guest_pdptr2; |
| 1929 | evmcs->guest_pdptr3 = vmcs12->guest_pdptr3; |
| 1930 | |
| 1931 | evmcs->guest_pending_dbg_exceptions = |
| 1932 | vmcs12->guest_pending_dbg_exceptions; |
| 1933 | evmcs->guest_sysenter_esp = vmcs12->guest_sysenter_esp; |
| 1934 | evmcs->guest_sysenter_eip = vmcs12->guest_sysenter_eip; |
| 1935 | |
| 1936 | evmcs->guest_activity_state = vmcs12->guest_activity_state; |
| 1937 | evmcs->guest_sysenter_cs = vmcs12->guest_sysenter_cs; |
| 1938 | |
| 1939 | evmcs->guest_cr0 = vmcs12->guest_cr0; |
| 1940 | evmcs->guest_cr3 = vmcs12->guest_cr3; |
| 1941 | evmcs->guest_cr4 = vmcs12->guest_cr4; |
| 1942 | evmcs->guest_dr7 = vmcs12->guest_dr7; |
| 1943 | |
| 1944 | evmcs->guest_physical_address = vmcs12->guest_physical_address; |
| 1945 | |
| 1946 | evmcs->vm_instruction_error = vmcs12->vm_instruction_error; |
| 1947 | evmcs->vm_exit_reason = vmcs12->vm_exit_reason; |
| 1948 | evmcs->vm_exit_intr_info = vmcs12->vm_exit_intr_info; |
| 1949 | evmcs->vm_exit_intr_error_code = vmcs12->vm_exit_intr_error_code; |
| 1950 | evmcs->idt_vectoring_info_field = vmcs12->idt_vectoring_info_field; |
| 1951 | evmcs->idt_vectoring_error_code = vmcs12->idt_vectoring_error_code; |
| 1952 | evmcs->vm_exit_instruction_len = vmcs12->vm_exit_instruction_len; |
| 1953 | evmcs->vmx_instruction_info = vmcs12->vmx_instruction_info; |
| 1954 | |
| 1955 | evmcs->exit_qualification = vmcs12->exit_qualification; |
| 1956 | |
| 1957 | evmcs->guest_linear_address = vmcs12->guest_linear_address; |
| 1958 | evmcs->guest_rsp = vmcs12->guest_rsp; |
| 1959 | evmcs->guest_rflags = vmcs12->guest_rflags; |
| 1960 | |
| 1961 | evmcs->guest_interruptibility_info = |
| 1962 | vmcs12->guest_interruptibility_info; |
| 1963 | evmcs->cpu_based_vm_exec_control = vmcs12->cpu_based_vm_exec_control; |
| 1964 | evmcs->vm_entry_controls = vmcs12->vm_entry_controls; |
| 1965 | evmcs->vm_entry_intr_info_field = vmcs12->vm_entry_intr_info_field; |
| 1966 | evmcs->vm_entry_exception_error_code = |
| 1967 | vmcs12->vm_entry_exception_error_code; |
| 1968 | evmcs->vm_entry_instruction_len = vmcs12->vm_entry_instruction_len; |
| 1969 | |
| 1970 | evmcs->guest_rip = vmcs12->guest_rip; |
| 1971 | |
| 1972 | evmcs->guest_bndcfgs = vmcs12->guest_bndcfgs; |
| 1973 | |
Vitaly Kuznetsov | 25641ca | 2021-05-26 15:20:19 +0200 | [diff] [blame] | 1974 | return; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1975 | } |
| 1976 | |
| 1977 | /* |
| 1978 | * This is an equivalent of the nested hypervisor executing the vmptrld |
| 1979 | * instruction. |
| 1980 | */ |
Vitaly Kuznetsov | b6a0653 | 2020-03-09 16:52:13 +0100 | [diff] [blame] | 1981 | static enum nested_evmptrld_status nested_vmx_handle_enlightened_vmptrld( |
| 1982 | struct kvm_vcpu *vcpu, bool from_launch) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1983 | { |
| 1984 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Vitaly Kuznetsov | a21a39c | 2019-06-28 13:23:32 +0200 | [diff] [blame] | 1985 | bool evmcs_gpa_changed = false; |
Vitaly Kuznetsov | 11e3491 | 2019-06-28 13:23:33 +0200 | [diff] [blame] | 1986 | u64 evmcs_gpa; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1987 | |
| 1988 | if (likely(!vmx->nested.enlightened_vmcs_enabled)) |
Vitaly Kuznetsov | b6a0653 | 2020-03-09 16:52:13 +0100 | [diff] [blame] | 1989 | return EVMPTRLD_DISABLED; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1990 | |
Vitaly Kuznetsov | 0276171 | 2021-05-26 15:20:18 +0200 | [diff] [blame] | 1991 | if (!nested_enlightened_vmentry(vcpu, &evmcs_gpa)) { |
| 1992 | nested_release_evmcs(vcpu); |
Vitaly Kuznetsov | b6a0653 | 2020-03-09 16:52:13 +0100 | [diff] [blame] | 1993 | return EVMPTRLD_DISABLED; |
Vitaly Kuznetsov | 0276171 | 2021-05-26 15:20:18 +0200 | [diff] [blame] | 1994 | } |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1995 | |
Vitaly Kuznetsov | 1e9dfbd | 2021-05-26 15:20:16 +0200 | [diff] [blame] | 1996 | if (unlikely(evmcs_gpa != vmx->nested.hv_evmcs_vmptr)) { |
| 1997 | vmx->nested.current_vmptr = -1ull; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1998 | |
| 1999 | nested_release_evmcs(vcpu); |
| 2000 | |
Vitaly Kuznetsov | 11e3491 | 2019-06-28 13:23:33 +0200 | [diff] [blame] | 2001 | if (kvm_vcpu_map(vcpu, gpa_to_gfn(evmcs_gpa), |
KarimAllah Ahmed | dee9c04 | 2019-01-31 21:24:42 +0100 | [diff] [blame] | 2002 | &vmx->nested.hv_evmcs_map)) |
Vitaly Kuznetsov | b6a0653 | 2020-03-09 16:52:13 +0100 | [diff] [blame] | 2003 | return EVMPTRLD_ERROR; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2004 | |
KarimAllah Ahmed | dee9c04 | 2019-01-31 21:24:42 +0100 | [diff] [blame] | 2005 | vmx->nested.hv_evmcs = vmx->nested.hv_evmcs_map.hva; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2006 | |
| 2007 | /* |
| 2008 | * Currently, KVM only supports eVMCS version 1 |
| 2009 | * (== KVM_EVMCS_VERSION) and thus we expect guest to set this |
| 2010 | * value to first u32 field of eVMCS which should specify eVMCS |
| 2011 | * VersionNumber. |
| 2012 | * |
| 2013 | * Guest should be aware of supported eVMCS versions by host by |
| 2014 | * examining CPUID.0x4000000A.EAX[0:15]. Host userspace VMM is |
| 2015 | * expected to set this CPUID leaf according to the value |
| 2016 | * returned in vmcs_version from nested_enable_evmcs(). |
| 2017 | * |
| 2018 | * However, it turns out that Microsoft Hyper-V fails to comply |
| 2019 | * to their own invented interface: When Hyper-V use eVMCS, it |
| 2020 | * just sets first u32 field of eVMCS to revision_id specified |
| 2021 | * in MSR_IA32_VMX_BASIC. Instead of used eVMCS version number |
| 2022 | * which is one of the supported versions specified in |
| 2023 | * CPUID.0x4000000A.EAX[0:15]. |
| 2024 | * |
| 2025 | * To overcome Hyper-V bug, we accept here either a supported |
| 2026 | * eVMCS version or VMCS12 revision_id as valid values for first |
| 2027 | * u32 field of eVMCS. |
| 2028 | */ |
| 2029 | if ((vmx->nested.hv_evmcs->revision_id != KVM_EVMCS_VERSION) && |
| 2030 | (vmx->nested.hv_evmcs->revision_id != VMCS12_REVISION)) { |
| 2031 | nested_release_evmcs(vcpu); |
Vitaly Kuznetsov | b6a0653 | 2020-03-09 16:52:13 +0100 | [diff] [blame] | 2032 | return EVMPTRLD_VMFAIL; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2033 | } |
| 2034 | |
Vitaly Kuznetsov | 11e3491 | 2019-06-28 13:23:33 +0200 | [diff] [blame] | 2035 | vmx->nested.hv_evmcs_vmptr = evmcs_gpa; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2036 | |
Vitaly Kuznetsov | a21a39c | 2019-06-28 13:23:32 +0200 | [diff] [blame] | 2037 | evmcs_gpa_changed = true; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2038 | /* |
| 2039 | * Unlike normal vmcs12, enlightened vmcs12 is not fully |
| 2040 | * reloaded from guest's memory (read only fields, fields not |
| 2041 | * present in struct hv_enlightened_vmcs, ...). Make sure there |
| 2042 | * are no leftovers. |
| 2043 | */ |
| 2044 | if (from_launch) { |
| 2045 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
| 2046 | memset(vmcs12, 0, sizeof(*vmcs12)); |
| 2047 | vmcs12->hdr.revision_id = VMCS12_REVISION; |
| 2048 | } |
| 2049 | |
| 2050 | } |
Vitaly Kuznetsov | a21a39c | 2019-06-28 13:23:32 +0200 | [diff] [blame] | 2051 | |
| 2052 | /* |
Miaohe Lin | ffdbd50 | 2020-02-07 23:22:45 +0800 | [diff] [blame] | 2053 | * 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] | 2054 | * between different L2 guests as KVM keeps a single VMCS12 per L1. |
| 2055 | */ |
| 2056 | if (from_launch || evmcs_gpa_changed) |
| 2057 | vmx->nested.hv_evmcs->hv_clean_fields &= |
| 2058 | ~HV_VMX_ENLIGHTENED_CLEAN_FIELD_ALL; |
| 2059 | |
Vitaly Kuznetsov | b6a0653 | 2020-03-09 16:52:13 +0100 | [diff] [blame] | 2060 | return EVMPTRLD_SUCCEEDED; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2061 | } |
| 2062 | |
Sean Christopherson | 3731905ef | 2019-05-07 08:36:27 -0700 | [diff] [blame] | 2063 | void nested_sync_vmcs12_to_shadow(struct kvm_vcpu *vcpu) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2064 | { |
| 2065 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 2066 | |
Vitaly Kuznetsov | dc31338 | 2021-05-26 15:20:24 +0200 | [diff] [blame] | 2067 | if (evmptr_is_valid(vmx->nested.hv_evmcs_vmptr)) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2068 | copy_vmcs12_to_enlightened(vmx); |
Vitaly Kuznetsov | dc31338 | 2021-05-26 15:20:24 +0200 | [diff] [blame] | 2069 | else |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2070 | copy_vmcs12_to_shadow(vmx); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2071 | |
Sean Christopherson | 3731905ef | 2019-05-07 08:36:27 -0700 | [diff] [blame] | 2072 | vmx->nested.need_vmcs12_to_shadow_sync = false; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2073 | } |
| 2074 | |
| 2075 | static enum hrtimer_restart vmx_preemption_timer_fn(struct hrtimer *timer) |
| 2076 | { |
| 2077 | struct vcpu_vmx *vmx = |
| 2078 | container_of(timer, struct vcpu_vmx, nested.preemption_timer); |
| 2079 | |
| 2080 | vmx->nested.preemption_timer_expired = true; |
| 2081 | kvm_make_request(KVM_REQ_EVENT, &vmx->vcpu); |
| 2082 | kvm_vcpu_kick(&vmx->vcpu); |
| 2083 | |
| 2084 | return HRTIMER_NORESTART; |
| 2085 | } |
| 2086 | |
Peter Shier | 850448f | 2020-05-26 14:51:06 -0700 | [diff] [blame] | 2087 | static u64 vmx_calc_preemption_timer_value(struct kvm_vcpu *vcpu) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2088 | { |
Peter Shier | 850448f | 2020-05-26 14:51:06 -0700 | [diff] [blame] | 2089 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 2090 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
Peter Shier | 850448f | 2020-05-26 14:51:06 -0700 | [diff] [blame] | 2091 | |
| 2092 | u64 l1_scaled_tsc = kvm_read_l1_tsc(vcpu, rdtsc()) >> |
| 2093 | VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE; |
| 2094 | |
| 2095 | if (!vmx->nested.has_preemption_timer_deadline) { |
Makarand Sonare | 8d7fbf0 | 2020-05-26 14:51:07 -0700 | [diff] [blame] | 2096 | vmx->nested.preemption_timer_deadline = |
| 2097 | vmcs12->vmx_preemption_timer_value + l1_scaled_tsc; |
Peter Shier | 850448f | 2020-05-26 14:51:06 -0700 | [diff] [blame] | 2098 | vmx->nested.has_preemption_timer_deadline = true; |
Makarand Sonare | 8d7fbf0 | 2020-05-26 14:51:07 -0700 | [diff] [blame] | 2099 | } |
| 2100 | return vmx->nested.preemption_timer_deadline - l1_scaled_tsc; |
Peter Shier | 850448f | 2020-05-26 14:51:06 -0700 | [diff] [blame] | 2101 | } |
| 2102 | |
| 2103 | static void vmx_start_preemption_timer(struct kvm_vcpu *vcpu, |
| 2104 | u64 preemption_timeout) |
| 2105 | { |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2106 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 2107 | |
| 2108 | /* |
| 2109 | * A timer value of zero is architecturally guaranteed to cause |
| 2110 | * a VMExit prior to executing any instructions in the guest. |
| 2111 | */ |
| 2112 | if (preemption_timeout == 0) { |
| 2113 | vmx_preemption_timer_fn(&vmx->nested.preemption_timer); |
| 2114 | return; |
| 2115 | } |
| 2116 | |
| 2117 | if (vcpu->arch.virtual_tsc_khz == 0) |
| 2118 | return; |
| 2119 | |
| 2120 | preemption_timeout <<= VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE; |
| 2121 | preemption_timeout *= 1000000; |
| 2122 | do_div(preemption_timeout, vcpu->arch.virtual_tsc_khz); |
| 2123 | hrtimer_start(&vmx->nested.preemption_timer, |
Jim Mattson | ada0098 | 2020-05-08 13:36:42 -0700 | [diff] [blame] | 2124 | ktime_add_ns(ktime_get(), preemption_timeout), |
| 2125 | HRTIMER_MODE_ABS_PINNED); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2126 | } |
| 2127 | |
| 2128 | static u64 nested_vmx_calc_efer(struct vcpu_vmx *vmx, struct vmcs12 *vmcs12) |
| 2129 | { |
| 2130 | if (vmx->nested.nested_run_pending && |
| 2131 | (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_EFER)) |
| 2132 | return vmcs12->guest_ia32_efer; |
| 2133 | else if (vmcs12->vm_entry_controls & VM_ENTRY_IA32E_MODE) |
| 2134 | return vmx->vcpu.arch.efer | (EFER_LMA | EFER_LME); |
| 2135 | else |
| 2136 | return vmx->vcpu.arch.efer & ~(EFER_LMA | EFER_LME); |
| 2137 | } |
| 2138 | |
| 2139 | static void prepare_vmcs02_constant_state(struct vcpu_vmx *vmx) |
| 2140 | { |
| 2141 | /* |
| 2142 | * If vmcs02 hasn't been initialized, set the constant vmcs02 state |
| 2143 | * according to L0's settings (vmcs12 is irrelevant here). Host |
| 2144 | * fields that come from L0 and are not constant, e.g. HOST_CR3, |
| 2145 | * will be set as needed prior to VMLAUNCH/VMRESUME. |
| 2146 | */ |
| 2147 | if (vmx->nested.vmcs02_initialized) |
| 2148 | return; |
| 2149 | vmx->nested.vmcs02_initialized = true; |
| 2150 | |
| 2151 | /* |
| 2152 | * We don't care what the EPTP value is we just need to guarantee |
| 2153 | * it's valid so we don't get a false positive when doing early |
| 2154 | * consistency checks. |
| 2155 | */ |
| 2156 | if (enable_ept && nested_early_check) |
Sean Christopherson | 2a40b90 | 2020-07-15 20:41:18 -0700 | [diff] [blame] | 2157 | vmcs_write64(EPT_POINTER, |
| 2158 | construct_eptp(&vmx->vcpu, 0, PT64_ROOT_4LEVEL)); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2159 | |
| 2160 | /* All VMFUNCs are currently emulated through L0 vmexits. */ |
| 2161 | if (cpu_has_vmx_vmfunc()) |
| 2162 | vmcs_write64(VM_FUNCTION_CONTROL, 0); |
| 2163 | |
| 2164 | if (cpu_has_vmx_posted_intr()) |
| 2165 | vmcs_write16(POSTED_INTR_NV, POSTED_INTR_NESTED_VECTOR); |
| 2166 | |
| 2167 | if (cpu_has_vmx_msr_bitmap()) |
| 2168 | vmcs_write64(MSR_BITMAP, __pa(vmx->nested.vmcs02.msr_bitmap)); |
| 2169 | |
Sean Christopherson | 4d6c989 | 2019-05-07 09:06:30 -0700 | [diff] [blame] | 2170 | /* |
Sean Christopherson | c3bb9a2 | 2021-02-12 16:50:07 -0800 | [diff] [blame] | 2171 | * PML is emulated for L2, but never enabled in hardware as the MMU |
| 2172 | * handles A/D emulation. Disabling PML for L2 also avoids having to |
| 2173 | * deal with filtering out L2 GPAs from the buffer. |
Sean Christopherson | 4d6c989 | 2019-05-07 09:06:30 -0700 | [diff] [blame] | 2174 | */ |
| 2175 | if (enable_pml) { |
Sean Christopherson | c3bb9a2 | 2021-02-12 16:50:07 -0800 | [diff] [blame] | 2176 | vmcs_write64(PML_ADDRESS, 0); |
| 2177 | vmcs_write16(GUEST_PML_INDEX, -1); |
Sean Christopherson | 4d6c989 | 2019-05-07 09:06:30 -0700 | [diff] [blame] | 2178 | } |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2179 | |
Sean Christopherson | c538d57 | 2019-05-07 09:06:29 -0700 | [diff] [blame] | 2180 | if (cpu_has_vmx_encls_vmexit()) |
| 2181 | vmcs_write64(ENCLS_EXITING_BITMAP, -1ull); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2182 | |
| 2183 | /* |
| 2184 | * Set the MSR load/store lists to match L0's settings. Only the |
| 2185 | * addresses are constant (for vmcs02), the counts can change based |
| 2186 | * on L2's behavior, e.g. switching to/from long mode. |
| 2187 | */ |
Aaron Lewis | 662f1d1 | 2019-11-07 21:14:39 -0800 | [diff] [blame] | 2188 | 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] | 2189 | vmcs_write64(VM_EXIT_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.host.val)); |
| 2190 | vmcs_write64(VM_ENTRY_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.guest.val)); |
| 2191 | |
| 2192 | vmx_set_constant_host_state(vmx); |
| 2193 | } |
| 2194 | |
Paolo Bonzini | b1346ab | 2019-06-06 17:24:00 +0200 | [diff] [blame] | 2195 | static void prepare_vmcs02_early_rare(struct vcpu_vmx *vmx, |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2196 | struct vmcs12 *vmcs12) |
| 2197 | { |
| 2198 | prepare_vmcs02_constant_state(vmx); |
| 2199 | |
| 2200 | vmcs_write64(VMCS_LINK_POINTER, -1ull); |
| 2201 | |
| 2202 | if (enable_vpid) { |
| 2203 | if (nested_cpu_has_vpid(vmcs12) && vmx->nested.vpid02) |
| 2204 | vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->nested.vpid02); |
| 2205 | else |
| 2206 | vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid); |
| 2207 | } |
| 2208 | } |
| 2209 | |
Sean Christopherson | 389ab25 | 2021-08-10 10:19:50 -0700 | [diff] [blame] | 2210 | static void prepare_vmcs02_early(struct vcpu_vmx *vmx, struct loaded_vmcs *vmcs01, |
| 2211 | struct vmcs12 *vmcs12) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2212 | { |
Sean Christopherson | c3bb9a2 | 2021-02-12 16:50:07 -0800 | [diff] [blame] | 2213 | u32 exec_control; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2214 | u64 guest_efer = nested_vmx_calc_efer(vmx, vmcs12); |
| 2215 | |
Vitaly Kuznetsov | 1e9dfbd | 2021-05-26 15:20:16 +0200 | [diff] [blame] | 2216 | 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] | 2217 | prepare_vmcs02_early_rare(vmx, vmcs12); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2218 | |
| 2219 | /* |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2220 | * PIN CONTROLS |
| 2221 | */ |
Sean Christopherson | 389ab25 | 2021-08-10 10:19:50 -0700 | [diff] [blame] | 2222 | exec_control = __pin_controls_get(vmcs01); |
Sean Christopherson | 804939e | 2019-05-07 12:18:05 -0700 | [diff] [blame] | 2223 | exec_control |= (vmcs12->pin_based_vm_exec_control & |
| 2224 | ~PIN_BASED_VMX_PREEMPTION_TIMER); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2225 | |
| 2226 | /* Posted interrupts setting is only taken from vmcs12. */ |
Sean Christopherson | f7782bb8 | 2021-08-10 07:45:26 -0700 | [diff] [blame] | 2227 | vmx->nested.pi_pending = false; |
| 2228 | if (nested_cpu_has_posted_intr(vmcs12)) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2229 | vmx->nested.posted_intr_nv = vmcs12->posted_intr_nv; |
Sean Christopherson | f7782bb8 | 2021-08-10 07:45:26 -0700 | [diff] [blame] | 2230 | else |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2231 | exec_control &= ~PIN_BASED_POSTED_INTR; |
Sean Christopherson | 3af80fe | 2019-05-07 12:18:00 -0700 | [diff] [blame] | 2232 | pin_controls_set(vmx, exec_control); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2233 | |
| 2234 | /* |
| 2235 | * EXEC CONTROLS |
| 2236 | */ |
Sean Christopherson | 389ab25 | 2021-08-10 10:19:50 -0700 | [diff] [blame] | 2237 | exec_control = __exec_controls_get(vmcs01); /* L0's desires */ |
Xiaoyao Li | 9dadc2f | 2019-12-06 16:45:24 +0800 | [diff] [blame] | 2238 | exec_control &= ~CPU_BASED_INTR_WINDOW_EXITING; |
Xiaoyao Li | 4e2a0bc | 2019-12-06 16:45:25 +0800 | [diff] [blame] | 2239 | exec_control &= ~CPU_BASED_NMI_WINDOW_EXITING; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2240 | exec_control &= ~CPU_BASED_TPR_SHADOW; |
| 2241 | exec_control |= vmcs12->cpu_based_vm_exec_control; |
| 2242 | |
Liran Alon | 02d496cf | 2019-11-11 14:30:55 +0200 | [diff] [blame] | 2243 | vmx->nested.l1_tpr_threshold = -1; |
Sean Christopherson | ca2f546 | 2019-05-07 09:06:33 -0700 | [diff] [blame] | 2244 | if (exec_control & CPU_BASED_TPR_SHADOW) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2245 | vmcs_write32(TPR_THRESHOLD, vmcs12->tpr_threshold); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2246 | #ifdef CONFIG_X86_64 |
Sean Christopherson | ca2f546 | 2019-05-07 09:06:33 -0700 | [diff] [blame] | 2247 | else |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2248 | exec_control |= CPU_BASED_CR8_LOAD_EXITING | |
| 2249 | CPU_BASED_CR8_STORE_EXITING; |
| 2250 | #endif |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2251 | |
| 2252 | /* |
| 2253 | * A vmexit (to either L1 hypervisor or L0 userspace) is always needed |
| 2254 | * for I/O port accesses. |
| 2255 | */ |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2256 | exec_control |= CPU_BASED_UNCOND_IO_EXITING; |
Sean Christopherson | de0286b | 2019-05-07 12:18:01 -0700 | [diff] [blame] | 2257 | exec_control &= ~CPU_BASED_USE_IO_BITMAPS; |
| 2258 | |
| 2259 | /* |
| 2260 | * This bit will be computed in nested_get_vmcs12_pages, because |
| 2261 | * we do not have access to L1's MSR bitmap yet. For now, keep |
| 2262 | * the same bit as before, hoping to avoid multiple VMWRITEs that |
| 2263 | * only set/clear this bit. |
| 2264 | */ |
| 2265 | exec_control &= ~CPU_BASED_USE_MSR_BITMAPS; |
| 2266 | exec_control |= exec_controls_get(vmx) & CPU_BASED_USE_MSR_BITMAPS; |
| 2267 | |
Sean Christopherson | 3af80fe | 2019-05-07 12:18:00 -0700 | [diff] [blame] | 2268 | exec_controls_set(vmx, exec_control); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2269 | |
| 2270 | /* |
| 2271 | * SECONDARY EXEC CONTROLS |
| 2272 | */ |
| 2273 | if (cpu_has_secondary_exec_ctrls()) { |
Sean Christopherson | 389ab25 | 2021-08-10 10:19:50 -0700 | [diff] [blame] | 2274 | exec_control = __secondary_exec_controls_get(vmcs01); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2275 | |
| 2276 | /* Take the following fields only from vmcs12 */ |
| 2277 | exec_control &= ~(SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES | |
Sean Christopherson | 389ab25 | 2021-08-10 10:19:50 -0700 | [diff] [blame] | 2278 | SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2279 | SECONDARY_EXEC_ENABLE_INVPCID | |
Sean Christopherson | 7f3603b | 2020-09-23 09:50:47 -0700 | [diff] [blame] | 2280 | SECONDARY_EXEC_ENABLE_RDTSCP | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2281 | SECONDARY_EXEC_XSAVES | |
Tao Xu | e69e72fa | 2019-07-16 14:55:49 +0800 | [diff] [blame] | 2282 | SECONDARY_EXEC_ENABLE_USR_WAIT_PAUSE | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2283 | SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY | |
| 2284 | SECONDARY_EXEC_APIC_REGISTER_VIRT | |
Ilias Stamatis | d041b5e | 2021-05-26 19:44:17 +0100 | [diff] [blame] | 2285 | SECONDARY_EXEC_ENABLE_VMFUNC | |
Sean Christopherson | 389ab25 | 2021-08-10 10:19:50 -0700 | [diff] [blame] | 2286 | SECONDARY_EXEC_TSC_SCALING | |
| 2287 | SECONDARY_EXEC_DESC); |
| 2288 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2289 | if (nested_cpu_has(vmcs12, |
Sean Christopherson | c3bb9a2 | 2021-02-12 16:50:07 -0800 | [diff] [blame] | 2290 | CPU_BASED_ACTIVATE_SECONDARY_CONTROLS)) |
| 2291 | exec_control |= vmcs12->secondary_vm_exec_control; |
| 2292 | |
| 2293 | /* PML is emulated and never enabled in hardware for L2. */ |
| 2294 | exec_control &= ~SECONDARY_EXEC_ENABLE_PML; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2295 | |
| 2296 | /* VMCS shadowing for L2 is emulated for now */ |
| 2297 | exec_control &= ~SECONDARY_EXEC_SHADOW_VMCS; |
| 2298 | |
Sean Christopherson | 469debd | 2019-05-07 12:18:02 -0700 | [diff] [blame] | 2299 | /* |
| 2300 | * Preset *DT exiting when emulating UMIP, so that vmx_set_cr4() |
| 2301 | * will not have to rewrite the controls just for this bit. |
| 2302 | */ |
| 2303 | if (!boot_cpu_has(X86_FEATURE_UMIP) && vmx_umip_emulated() && |
| 2304 | (vmcs12->guest_cr4 & X86_CR4_UMIP)) |
| 2305 | exec_control |= SECONDARY_EXEC_DESC; |
| 2306 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2307 | if (exec_control & SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY) |
| 2308 | vmcs_write16(GUEST_INTR_STATUS, |
| 2309 | vmcs12->guest_intr_status); |
| 2310 | |
Krish Sadhukhan | bddd82d | 2020-09-21 08:10:25 +0000 | [diff] [blame] | 2311 | if (!nested_cpu_has2(vmcs12, SECONDARY_EXEC_UNRESTRICTED_GUEST)) |
| 2312 | exec_control &= ~SECONDARY_EXEC_UNRESTRICTED_GUEST; |
| 2313 | |
Sean Christopherson | 72add91 | 2021-04-12 16:21:42 +1200 | [diff] [blame] | 2314 | if (exec_control & SECONDARY_EXEC_ENCLS_EXITING) |
| 2315 | vmx_write_encls_bitmap(&vmx->vcpu, vmcs12); |
| 2316 | |
Sean Christopherson | 3af80fe | 2019-05-07 12:18:00 -0700 | [diff] [blame] | 2317 | secondary_exec_controls_set(vmx, exec_control); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2318 | } |
| 2319 | |
| 2320 | /* |
| 2321 | * ENTRY CONTROLS |
| 2322 | * |
| 2323 | * vmcs12's VM_{ENTRY,EXIT}_LOAD_IA32_EFER and VM_ENTRY_IA32E_MODE |
| 2324 | * are emulated by vmx_set_efer() in prepare_vmcs02(), but speculate |
| 2325 | * on the related bits (if supported by the CPU) in the hope that |
| 2326 | * we can avoid VMWrites during vmx_set_efer(). |
| 2327 | */ |
Sean Christopherson | 389ab25 | 2021-08-10 10:19:50 -0700 | [diff] [blame] | 2328 | exec_control = __vm_entry_controls_get(vmcs01); |
| 2329 | exec_control |= vmcs12->vm_entry_controls; |
| 2330 | exec_control &= ~(VM_ENTRY_IA32E_MODE | VM_ENTRY_LOAD_IA32_EFER); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2331 | if (cpu_has_load_ia32_efer()) { |
| 2332 | if (guest_efer & EFER_LMA) |
| 2333 | exec_control |= VM_ENTRY_IA32E_MODE; |
| 2334 | if (guest_efer != host_efer) |
| 2335 | exec_control |= VM_ENTRY_LOAD_IA32_EFER; |
| 2336 | } |
Sean Christopherson | 3af80fe | 2019-05-07 12:18:00 -0700 | [diff] [blame] | 2337 | vm_entry_controls_set(vmx, exec_control); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2338 | |
| 2339 | /* |
| 2340 | * EXIT CONTROLS |
| 2341 | * |
| 2342 | * L2->L1 exit controls are emulated - the hardware exit is to L0 so |
| 2343 | * we should use its exit controls. Note that VM_EXIT_LOAD_IA32_EFER |
| 2344 | * bits may be modified by vmx_set_efer() in prepare_vmcs02(). |
| 2345 | */ |
Sean Christopherson | 389ab25 | 2021-08-10 10:19:50 -0700 | [diff] [blame] | 2346 | exec_control = __vm_exit_controls_get(vmcs01); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2347 | if (cpu_has_load_ia32_efer() && guest_efer != host_efer) |
| 2348 | exec_control |= VM_EXIT_LOAD_IA32_EFER; |
Sean Christopherson | 389ab25 | 2021-08-10 10:19:50 -0700 | [diff] [blame] | 2349 | else |
| 2350 | exec_control &= ~VM_EXIT_LOAD_IA32_EFER; |
Sean Christopherson | 3af80fe | 2019-05-07 12:18:00 -0700 | [diff] [blame] | 2351 | vm_exit_controls_set(vmx, exec_control); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2352 | |
| 2353 | /* |
| 2354 | * Interrupt/Exception Fields |
| 2355 | */ |
| 2356 | if (vmx->nested.nested_run_pending) { |
| 2357 | vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, |
| 2358 | vmcs12->vm_entry_intr_info_field); |
| 2359 | vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE, |
| 2360 | vmcs12->vm_entry_exception_error_code); |
| 2361 | vmcs_write32(VM_ENTRY_INSTRUCTION_LEN, |
| 2362 | vmcs12->vm_entry_instruction_len); |
| 2363 | vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, |
| 2364 | vmcs12->guest_interruptibility_info); |
| 2365 | vmx->loaded_vmcs->nmi_known_unmasked = |
| 2366 | !(vmcs12->guest_interruptibility_info & GUEST_INTR_STATE_NMI); |
| 2367 | } else { |
| 2368 | vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0); |
| 2369 | } |
| 2370 | } |
| 2371 | |
Paolo Bonzini | b1346ab | 2019-06-06 17:24:00 +0200 | [diff] [blame] | 2372 | static void prepare_vmcs02_rare(struct vcpu_vmx *vmx, struct vmcs12 *vmcs12) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2373 | { |
| 2374 | struct hv_enlightened_vmcs *hv_evmcs = vmx->nested.hv_evmcs; |
| 2375 | |
| 2376 | if (!hv_evmcs || !(hv_evmcs->hv_clean_fields & |
| 2377 | HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP2)) { |
| 2378 | vmcs_write16(GUEST_ES_SELECTOR, vmcs12->guest_es_selector); |
| 2379 | vmcs_write16(GUEST_CS_SELECTOR, vmcs12->guest_cs_selector); |
| 2380 | vmcs_write16(GUEST_SS_SELECTOR, vmcs12->guest_ss_selector); |
| 2381 | vmcs_write16(GUEST_DS_SELECTOR, vmcs12->guest_ds_selector); |
| 2382 | vmcs_write16(GUEST_FS_SELECTOR, vmcs12->guest_fs_selector); |
| 2383 | vmcs_write16(GUEST_GS_SELECTOR, vmcs12->guest_gs_selector); |
| 2384 | vmcs_write16(GUEST_LDTR_SELECTOR, vmcs12->guest_ldtr_selector); |
| 2385 | vmcs_write16(GUEST_TR_SELECTOR, vmcs12->guest_tr_selector); |
| 2386 | vmcs_write32(GUEST_ES_LIMIT, vmcs12->guest_es_limit); |
| 2387 | vmcs_write32(GUEST_CS_LIMIT, vmcs12->guest_cs_limit); |
| 2388 | vmcs_write32(GUEST_SS_LIMIT, vmcs12->guest_ss_limit); |
| 2389 | vmcs_write32(GUEST_DS_LIMIT, vmcs12->guest_ds_limit); |
| 2390 | vmcs_write32(GUEST_FS_LIMIT, vmcs12->guest_fs_limit); |
| 2391 | vmcs_write32(GUEST_GS_LIMIT, vmcs12->guest_gs_limit); |
| 2392 | vmcs_write32(GUEST_LDTR_LIMIT, vmcs12->guest_ldtr_limit); |
| 2393 | vmcs_write32(GUEST_TR_LIMIT, vmcs12->guest_tr_limit); |
| 2394 | vmcs_write32(GUEST_GDTR_LIMIT, vmcs12->guest_gdtr_limit); |
| 2395 | vmcs_write32(GUEST_IDTR_LIMIT, vmcs12->guest_idtr_limit); |
Sean Christopherson | 1c6f0b4 | 2019-05-07 08:36:25 -0700 | [diff] [blame] | 2396 | vmcs_write32(GUEST_CS_AR_BYTES, vmcs12->guest_cs_ar_bytes); |
| 2397 | vmcs_write32(GUEST_SS_AR_BYTES, vmcs12->guest_ss_ar_bytes); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2398 | vmcs_write32(GUEST_ES_AR_BYTES, vmcs12->guest_es_ar_bytes); |
| 2399 | vmcs_write32(GUEST_DS_AR_BYTES, vmcs12->guest_ds_ar_bytes); |
| 2400 | vmcs_write32(GUEST_FS_AR_BYTES, vmcs12->guest_fs_ar_bytes); |
| 2401 | vmcs_write32(GUEST_GS_AR_BYTES, vmcs12->guest_gs_ar_bytes); |
| 2402 | vmcs_write32(GUEST_LDTR_AR_BYTES, vmcs12->guest_ldtr_ar_bytes); |
| 2403 | vmcs_write32(GUEST_TR_AR_BYTES, vmcs12->guest_tr_ar_bytes); |
| 2404 | vmcs_writel(GUEST_ES_BASE, vmcs12->guest_es_base); |
| 2405 | vmcs_writel(GUEST_CS_BASE, vmcs12->guest_cs_base); |
| 2406 | vmcs_writel(GUEST_SS_BASE, vmcs12->guest_ss_base); |
| 2407 | vmcs_writel(GUEST_DS_BASE, vmcs12->guest_ds_base); |
| 2408 | vmcs_writel(GUEST_FS_BASE, vmcs12->guest_fs_base); |
| 2409 | vmcs_writel(GUEST_GS_BASE, vmcs12->guest_gs_base); |
| 2410 | vmcs_writel(GUEST_LDTR_BASE, vmcs12->guest_ldtr_base); |
| 2411 | vmcs_writel(GUEST_TR_BASE, vmcs12->guest_tr_base); |
| 2412 | vmcs_writel(GUEST_GDTR_BASE, vmcs12->guest_gdtr_base); |
| 2413 | vmcs_writel(GUEST_IDTR_BASE, vmcs12->guest_idtr_base); |
Sean Christopherson | fc387d8 | 2020-09-23 11:44:46 -0700 | [diff] [blame] | 2414 | |
| 2415 | vmx->segment_cache.bitmask = 0; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2416 | } |
| 2417 | |
| 2418 | if (!hv_evmcs || !(hv_evmcs->hv_clean_fields & |
| 2419 | HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP1)) { |
| 2420 | vmcs_write32(GUEST_SYSENTER_CS, vmcs12->guest_sysenter_cs); |
| 2421 | vmcs_writel(GUEST_PENDING_DBG_EXCEPTIONS, |
| 2422 | vmcs12->guest_pending_dbg_exceptions); |
| 2423 | vmcs_writel(GUEST_SYSENTER_ESP, vmcs12->guest_sysenter_esp); |
| 2424 | vmcs_writel(GUEST_SYSENTER_EIP, vmcs12->guest_sysenter_eip); |
| 2425 | |
| 2426 | /* |
| 2427 | * L1 may access the L2's PDPTR, so save them to construct |
| 2428 | * vmcs12 |
| 2429 | */ |
| 2430 | if (enable_ept) { |
| 2431 | vmcs_write64(GUEST_PDPTR0, vmcs12->guest_pdptr0); |
| 2432 | vmcs_write64(GUEST_PDPTR1, vmcs12->guest_pdptr1); |
| 2433 | vmcs_write64(GUEST_PDPTR2, vmcs12->guest_pdptr2); |
| 2434 | vmcs_write64(GUEST_PDPTR3, vmcs12->guest_pdptr3); |
| 2435 | } |
Sean Christopherson | c27e5b0 | 2019-05-07 09:06:39 -0700 | [diff] [blame] | 2436 | |
| 2437 | if (kvm_mpx_supported() && vmx->nested.nested_run_pending && |
| 2438 | (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_BNDCFGS)) |
| 2439 | vmcs_write64(GUEST_BNDCFGS, vmcs12->guest_bndcfgs); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2440 | } |
| 2441 | |
| 2442 | if (nested_cpu_has_xsaves(vmcs12)) |
| 2443 | vmcs_write64(XSS_EXIT_BITMAP, vmcs12->xss_exit_bitmap); |
| 2444 | |
| 2445 | /* |
| 2446 | * Whether page-faults are trapped is determined by a combination of |
Paolo Bonzini | a0c1343 | 2020-07-10 17:48:08 +0200 | [diff] [blame] | 2447 | * 3 settings: PFEC_MASK, PFEC_MATCH and EXCEPTION_BITMAP.PF. If L0 |
| 2448 | * doesn't care about page faults then we should set all of these to |
| 2449 | * L1's desires. However, if L0 does care about (some) page faults, it |
| 2450 | * is not easy (if at all possible?) to merge L0 and L1's desires, we |
| 2451 | * simply ask to exit on each and every L2 page fault. This is done by |
| 2452 | * setting MASK=MATCH=0 and (see below) EB.PF=1. |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2453 | * Note that below we don't need special code to set EB.PF beyond the |
| 2454 | * "or"ing of the EB of vmcs01 and vmcs12, because when enable_ept, |
| 2455 | * vmcs01's EB.PF is 0 so the "or" will take vmcs12's value, and when |
| 2456 | * !enable_ept, EB.PF is 1, so the "or" will always be 1. |
| 2457 | */ |
Paolo Bonzini | a0c1343 | 2020-07-10 17:48:08 +0200 | [diff] [blame] | 2458 | if (vmx_need_pf_intercept(&vmx->vcpu)) { |
| 2459 | /* |
| 2460 | * TODO: if both L0 and L1 need the same MASK and MATCH, |
| 2461 | * go ahead and use it? |
| 2462 | */ |
| 2463 | vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK, 0); |
| 2464 | vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH, 0); |
| 2465 | } else { |
| 2466 | vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK, vmcs12->page_fault_error_code_mask); |
| 2467 | vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH, vmcs12->page_fault_error_code_match); |
| 2468 | } |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2469 | |
| 2470 | if (cpu_has_vmx_apicv()) { |
| 2471 | vmcs_write64(EOI_EXIT_BITMAP0, vmcs12->eoi_exit_bitmap0); |
| 2472 | vmcs_write64(EOI_EXIT_BITMAP1, vmcs12->eoi_exit_bitmap1); |
| 2473 | vmcs_write64(EOI_EXIT_BITMAP2, vmcs12->eoi_exit_bitmap2); |
| 2474 | vmcs_write64(EOI_EXIT_BITMAP3, vmcs12->eoi_exit_bitmap3); |
| 2475 | } |
| 2476 | |
Aaron Lewis | 662f1d1 | 2019-11-07 21:14:39 -0800 | [diff] [blame] | 2477 | /* |
| 2478 | * Make sure the msr_autostore list is up to date before we set the |
| 2479 | * count in the vmcs02. |
| 2480 | */ |
| 2481 | prepare_vmx_msr_autostore_list(&vmx->vcpu, MSR_IA32_TSC); |
| 2482 | |
| 2483 | vmcs_write32(VM_EXIT_MSR_STORE_COUNT, vmx->msr_autostore.guest.nr); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2484 | vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, vmx->msr_autoload.host.nr); |
| 2485 | vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, vmx->msr_autoload.guest.nr); |
| 2486 | |
| 2487 | set_cr4_guest_host_mask(vmx); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2488 | } |
| 2489 | |
| 2490 | /* |
| 2491 | * prepare_vmcs02 is called when the L1 guest hypervisor runs its nested |
| 2492 | * L2 guest. L1 has a vmcs for L2 (vmcs12), and this function "merges" it |
| 2493 | * with L0's requirements for its guest (a.k.a. vmcs01), so we can run the L2 |
| 2494 | * guest in a way that will both be appropriate to L1's requests, and our |
| 2495 | * needs. In addition to modifying the active vmcs (which is vmcs02), this |
| 2496 | * function also has additional necessary side-effects, like setting various |
| 2497 | * vcpu->arch fields. |
| 2498 | * Returns 0 on success, 1 on failure. Invalid state exit qualification code |
| 2499 | * is assigned to entry_failure_code on failure. |
| 2500 | */ |
| 2501 | static int prepare_vmcs02(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12, |
Maxim Levitsky | 0f85722 | 2021-06-07 12:02:00 +0300 | [diff] [blame] | 2502 | bool from_vmentry, |
Sean Christopherson | 68cda40 | 2020-05-11 15:05:29 -0700 | [diff] [blame] | 2503 | enum vm_entry_failure_code *entry_failure_code) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2504 | { |
| 2505 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Sean Christopherson | c7554efc | 2019-05-07 09:06:40 -0700 | [diff] [blame] | 2506 | bool load_guest_pdptrs_vmcs12 = false; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2507 | |
Vitaly Kuznetsov | 1e9dfbd | 2021-05-26 15:20:16 +0200 | [diff] [blame] | 2508 | 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] | 2509 | prepare_vmcs02_rare(vmx, vmcs12); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2510 | vmx->nested.dirty_vmcs12 = false; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2511 | |
Vitaly Kuznetsov | 1e9dfbd | 2021-05-26 15:20:16 +0200 | [diff] [blame] | 2512 | load_guest_pdptrs_vmcs12 = !evmptr_is_valid(vmx->nested.hv_evmcs_vmptr) || |
| 2513 | !(vmx->nested.hv_evmcs->hv_clean_fields & |
Sean Christopherson | c7554efc | 2019-05-07 09:06:40 -0700 | [diff] [blame] | 2514 | HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP1); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2515 | } |
| 2516 | |
| 2517 | if (vmx->nested.nested_run_pending && |
| 2518 | (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS)) { |
| 2519 | kvm_set_dr(vcpu, 7, vmcs12->guest_dr7); |
| 2520 | vmcs_write64(GUEST_IA32_DEBUGCTL, vmcs12->guest_ia32_debugctl); |
| 2521 | } else { |
| 2522 | kvm_set_dr(vcpu, 7, vcpu->arch.dr7); |
| 2523 | vmcs_write64(GUEST_IA32_DEBUGCTL, vmx->nested.vmcs01_debugctl); |
| 2524 | } |
Sean Christopherson | 3b013a2 | 2019-05-07 09:06:28 -0700 | [diff] [blame] | 2525 | if (kvm_mpx_supported() && (!vmx->nested.nested_run_pending || |
| 2526 | !(vmcs12->vm_entry_controls & VM_ENTRY_LOAD_BNDCFGS))) |
| 2527 | vmcs_write64(GUEST_BNDCFGS, vmx->nested.vmcs01_guest_bndcfgs); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2528 | vmx_set_rflags(vcpu, vmcs12->guest_rflags); |
| 2529 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2530 | /* EXCEPTION_BITMAP and CR0_GUEST_HOST_MASK should basically be the |
| 2531 | * bitwise-or of what L1 wants to trap for L2, and what we want to |
| 2532 | * trap. Note that CR0.TS also needs updating - we do this later. |
| 2533 | */ |
Jason Baron | b6a7cc3 | 2021-01-14 22:27:54 -0500 | [diff] [blame] | 2534 | vmx_update_exception_bitmap(vcpu); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2535 | vcpu->arch.cr0_guest_owned_bits &= ~vmcs12->cr0_guest_host_mask; |
| 2536 | vmcs_writel(CR0_GUEST_HOST_MASK, ~vcpu->arch.cr0_guest_owned_bits); |
| 2537 | |
| 2538 | if (vmx->nested.nested_run_pending && |
| 2539 | (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_PAT)) { |
| 2540 | vmcs_write64(GUEST_IA32_PAT, vmcs12->guest_ia32_pat); |
| 2541 | vcpu->arch.pat = vmcs12->guest_ia32_pat; |
| 2542 | } else if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) { |
| 2543 | vmcs_write64(GUEST_IA32_PAT, vmx->vcpu.arch.pat); |
| 2544 | } |
| 2545 | |
Ilias Stamatis | d041b5e | 2021-05-26 19:44:17 +0100 | [diff] [blame] | 2546 | vcpu->arch.tsc_offset = kvm_calc_nested_tsc_offset( |
| 2547 | vcpu->arch.l1_tsc_offset, |
| 2548 | vmx_get_l2_tsc_offset(vcpu), |
| 2549 | vmx_get_l2_tsc_multiplier(vcpu)); |
| 2550 | |
| 2551 | vcpu->arch.tsc_scaling_ratio = kvm_calc_nested_tsc_multiplier( |
| 2552 | vcpu->arch.l1_tsc_scaling_ratio, |
| 2553 | vmx_get_l2_tsc_multiplier(vcpu)); |
| 2554 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2555 | vmcs_write64(TSC_OFFSET, vcpu->arch.tsc_offset); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2556 | if (kvm_has_tsc_control) |
Ilias Stamatis | 1ab9287 | 2021-06-07 11:54:38 +0100 | [diff] [blame] | 2557 | vmcs_write64(TSC_MULTIPLIER, vcpu->arch.tsc_scaling_ratio); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2558 | |
Sean Christopherson | 50b265a | 2020-03-20 14:28:19 -0700 | [diff] [blame] | 2559 | nested_vmx_transition_tlb_flush(vcpu, vmcs12, true); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2560 | |
| 2561 | if (nested_cpu_has_ept(vmcs12)) |
| 2562 | nested_ept_init_mmu_context(vcpu); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2563 | |
| 2564 | /* |
| 2565 | * This sets GUEST_CR0 to vmcs12->guest_cr0, possibly modifying those |
| 2566 | * bits which we consider mandatory enabled. |
| 2567 | * The CR0_READ_SHADOW is what L2 should have expected to read given |
| 2568 | * the specifications by L1; It's not enough to take |
| 2569 | * vmcs12->cr0_read_shadow because on our cr0_guest_host_mask we we |
| 2570 | * have more bits than L1 expected. |
| 2571 | */ |
| 2572 | vmx_set_cr0(vcpu, vmcs12->guest_cr0); |
| 2573 | vmcs_writel(CR0_READ_SHADOW, nested_read_cr0(vmcs12)); |
| 2574 | |
| 2575 | vmx_set_cr4(vcpu, vmcs12->guest_cr4); |
| 2576 | vmcs_writel(CR4_READ_SHADOW, nested_read_cr4(vmcs12)); |
| 2577 | |
| 2578 | vcpu->arch.efer = nested_vmx_calc_efer(vmx, vmcs12); |
| 2579 | /* Note: may modify VM_ENTRY/EXIT_CONTROLS and GUEST/HOST_IA32_EFER */ |
| 2580 | vmx_set_efer(vcpu, vcpu->arch.efer); |
| 2581 | |
| 2582 | /* |
| 2583 | * Guest state is invalid and unrestricted guest is disabled, |
| 2584 | * which means L1 attempted VMEntry to L2 with invalid state. |
| 2585 | * Fail the VMEntry. |
| 2586 | */ |
Sean Christopherson | 2ba4493 | 2020-09-23 11:44:48 -0700 | [diff] [blame] | 2587 | if (CC(!vmx_guest_state_valid(vcpu))) { |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2588 | *entry_failure_code = ENTRY_FAIL_DEFAULT; |
Sean Christopherson | c80add0 | 2019-04-11 12:18:09 -0700 | [diff] [blame] | 2589 | return -EINVAL; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2590 | } |
| 2591 | |
| 2592 | /* Shadow page tables on either EPT or shadow page tables. */ |
| 2593 | 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] | 2594 | from_vmentry, entry_failure_code)) |
Sean Christopherson | c80add0 | 2019-04-11 12:18:09 -0700 | [diff] [blame] | 2595 | return -EINVAL; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2596 | |
Sean Christopherson | 04f11ef | 2019-09-27 14:45:16 -0700 | [diff] [blame] | 2597 | /* |
| 2598 | * Immediately write vmcs02.GUEST_CR3. It will be propagated to vmcs12 |
| 2599 | * on nested VM-Exit, which can occur without actually running L2 and |
Paolo Bonzini | 727a7e2 | 2020-03-05 03:52:50 -0500 | [diff] [blame] | 2600 | * 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] | 2601 | * vmcs12.GUEST_ACTIVITYSTATE=HLT, in which case KVM will intercept the |
| 2602 | * transition to HLT instead of running L2. |
| 2603 | */ |
| 2604 | if (enable_ept) |
| 2605 | vmcs_writel(GUEST_CR3, vmcs12->guest_cr3); |
| 2606 | |
Sean Christopherson | c7554efc | 2019-05-07 09:06:40 -0700 | [diff] [blame] | 2607 | /* Late preparation of GUEST_PDPTRs now that EFER and CRs are set. */ |
| 2608 | if (load_guest_pdptrs_vmcs12 && nested_cpu_has_ept(vmcs12) && |
| 2609 | is_pae_paging(vcpu)) { |
| 2610 | vmcs_write64(GUEST_PDPTR0, vmcs12->guest_pdptr0); |
| 2611 | vmcs_write64(GUEST_PDPTR1, vmcs12->guest_pdptr1); |
| 2612 | vmcs_write64(GUEST_PDPTR2, vmcs12->guest_pdptr2); |
| 2613 | vmcs_write64(GUEST_PDPTR3, vmcs12->guest_pdptr3); |
| 2614 | } |
| 2615 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2616 | if (!enable_ept) |
| 2617 | vcpu->arch.walk_mmu->inject_page_fault = vmx_inject_page_fault_nested; |
| 2618 | |
Oliver Upton | 71f7347 | 2019-11-13 16:17:19 -0800 | [diff] [blame] | 2619 | if ((vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL) && |
Oliver Upton | d196842 | 2019-12-13 16:33:58 -0800 | [diff] [blame] | 2620 | WARN_ON_ONCE(kvm_set_msr(vcpu, MSR_CORE_PERF_GLOBAL_CTRL, |
| 2621 | vmcs12->guest_ia32_perf_global_ctrl))) |
Oliver Upton | 71f7347 | 2019-11-13 16:17:19 -0800 | [diff] [blame] | 2622 | return -EINVAL; |
| 2623 | |
Paolo Bonzini | e9c16c7 | 2019-04-30 22:07:26 +0200 | [diff] [blame] | 2624 | kvm_rsp_write(vcpu, vmcs12->guest_rsp); |
| 2625 | kvm_rip_write(vcpu, vmcs12->guest_rip); |
Vitaly Kuznetsov | dc31338 | 2021-05-26 15:20:24 +0200 | [diff] [blame] | 2626 | |
| 2627 | /* |
| 2628 | * It was observed that genuine Hyper-V running in L1 doesn't reset |
| 2629 | * 'hv_clean_fields' by itself, it only sets the corresponding dirty |
| 2630 | * bits when it changes a field in eVMCS. Mark all fields as clean |
| 2631 | * here. |
| 2632 | */ |
| 2633 | if (evmptr_is_valid(vmx->nested.hv_evmcs_vmptr)) |
| 2634 | vmx->nested.hv_evmcs->hv_clean_fields |= |
| 2635 | HV_VMX_ENLIGHTENED_CLEAN_FIELD_ALL; |
| 2636 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2637 | return 0; |
| 2638 | } |
| 2639 | |
| 2640 | static int nested_vmx_check_nmi_controls(struct vmcs12 *vmcs12) |
| 2641 | { |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 2642 | if (CC(!nested_cpu_has_nmi_exiting(vmcs12) && |
| 2643 | nested_cpu_has_virtual_nmis(vmcs12))) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2644 | return -EINVAL; |
| 2645 | |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 2646 | if (CC(!nested_cpu_has_virtual_nmis(vmcs12) && |
Xiaoyao Li | 4e2a0bc | 2019-12-06 16:45:25 +0800 | [diff] [blame] | 2647 | nested_cpu_has(vmcs12, CPU_BASED_NMI_WINDOW_EXITING))) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2648 | return -EINVAL; |
| 2649 | |
| 2650 | return 0; |
| 2651 | } |
| 2652 | |
Sean Christopherson | ac6389a | 2020-03-02 18:02:38 -0800 | [diff] [blame] | 2653 | 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] | 2654 | { |
| 2655 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2656 | |
| 2657 | /* Check for memory type validity */ |
Sean Christopherson | ac6389a | 2020-03-02 18:02:38 -0800 | [diff] [blame] | 2658 | switch (new_eptp & VMX_EPTP_MT_MASK) { |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2659 | case VMX_EPTP_MT_UC: |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 2660 | if (CC(!(vmx->nested.msrs.ept_caps & VMX_EPTP_UC_BIT))) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2661 | return false; |
| 2662 | break; |
| 2663 | case VMX_EPTP_MT_WB: |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 2664 | if (CC(!(vmx->nested.msrs.ept_caps & VMX_EPTP_WB_BIT))) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2665 | return false; |
| 2666 | break; |
| 2667 | default: |
| 2668 | return false; |
| 2669 | } |
| 2670 | |
Sean Christopherson | bb1fcc7 | 2020-03-02 18:02:36 -0800 | [diff] [blame] | 2671 | /* Page-walk levels validity. */ |
Sean Christopherson | ac6389a | 2020-03-02 18:02:38 -0800 | [diff] [blame] | 2672 | switch (new_eptp & VMX_EPTP_PWL_MASK) { |
Sean Christopherson | bb1fcc7 | 2020-03-02 18:02:36 -0800 | [diff] [blame] | 2673 | case VMX_EPTP_PWL_5: |
| 2674 | if (CC(!(vmx->nested.msrs.ept_caps & VMX_EPT_PAGE_WALK_5_BIT))) |
| 2675 | return false; |
| 2676 | break; |
| 2677 | case VMX_EPTP_PWL_4: |
| 2678 | if (CC(!(vmx->nested.msrs.ept_caps & VMX_EPT_PAGE_WALK_4_BIT))) |
| 2679 | return false; |
| 2680 | break; |
| 2681 | default: |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2682 | return false; |
Sean Christopherson | bb1fcc7 | 2020-03-02 18:02:36 -0800 | [diff] [blame] | 2683 | } |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2684 | |
| 2685 | /* Reserved bits should not be set */ |
Sean Christopherson | 636e8b7 | 2021-02-03 16:01:10 -0800 | [diff] [blame] | 2686 | 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] | 2687 | return false; |
| 2688 | |
| 2689 | /* AD, if set, should be supported */ |
Sean Christopherson | ac6389a | 2020-03-02 18:02:38 -0800 | [diff] [blame] | 2690 | if (new_eptp & VMX_EPTP_AD_ENABLE_BIT) { |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 2691 | if (CC(!(vmx->nested.msrs.ept_caps & VMX_EPT_AD_BIT))) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2692 | return false; |
| 2693 | } |
| 2694 | |
| 2695 | return true; |
| 2696 | } |
| 2697 | |
Krish Sadhukhan | 461b4ba | 2018-12-12 13:30:07 -0500 | [diff] [blame] | 2698 | /* |
| 2699 | * Checks related to VM-Execution Control Fields |
| 2700 | */ |
| 2701 | static int nested_check_vm_execution_controls(struct kvm_vcpu *vcpu, |
| 2702 | struct vmcs12 *vmcs12) |
| 2703 | { |
| 2704 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 2705 | |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 2706 | if (CC(!vmx_control_verify(vmcs12->pin_based_vm_exec_control, |
| 2707 | vmx->nested.msrs.pinbased_ctls_low, |
| 2708 | vmx->nested.msrs.pinbased_ctls_high)) || |
| 2709 | CC(!vmx_control_verify(vmcs12->cpu_based_vm_exec_control, |
| 2710 | vmx->nested.msrs.procbased_ctls_low, |
| 2711 | vmx->nested.msrs.procbased_ctls_high))) |
Krish Sadhukhan | 461b4ba | 2018-12-12 13:30:07 -0500 | [diff] [blame] | 2712 | return -EINVAL; |
| 2713 | |
| 2714 | if (nested_cpu_has(vmcs12, CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) && |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 2715 | CC(!vmx_control_verify(vmcs12->secondary_vm_exec_control, |
| 2716 | vmx->nested.msrs.secondary_ctls_low, |
| 2717 | vmx->nested.msrs.secondary_ctls_high))) |
Krish Sadhukhan | 461b4ba | 2018-12-12 13:30:07 -0500 | [diff] [blame] | 2718 | return -EINVAL; |
| 2719 | |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 2720 | 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] | 2721 | nested_vmx_check_io_bitmap_controls(vcpu, vmcs12) || |
| 2722 | nested_vmx_check_msr_bitmap_controls(vcpu, vmcs12) || |
| 2723 | nested_vmx_check_tpr_shadow_controls(vcpu, vmcs12) || |
| 2724 | nested_vmx_check_apic_access_controls(vcpu, vmcs12) || |
| 2725 | nested_vmx_check_apicv_controls(vcpu, vmcs12) || |
| 2726 | nested_vmx_check_nmi_controls(vmcs12) || |
| 2727 | nested_vmx_check_pml_controls(vcpu, vmcs12) || |
| 2728 | nested_vmx_check_unrestricted_guest_controls(vcpu, vmcs12) || |
| 2729 | nested_vmx_check_mode_based_ept_exec_controls(vcpu, vmcs12) || |
| 2730 | nested_vmx_check_shadow_vmcs_controls(vcpu, vmcs12) || |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 2731 | CC(nested_cpu_has_vpid(vmcs12) && !vmcs12->virtual_processor_id)) |
Krish Sadhukhan | 461b4ba | 2018-12-12 13:30:07 -0500 | [diff] [blame] | 2732 | return -EINVAL; |
| 2733 | |
Sean Christopherson | bc44121 | 2019-02-12 16:42:23 -0800 | [diff] [blame] | 2734 | if (!nested_cpu_has_preemption_timer(vmcs12) && |
| 2735 | nested_cpu_has_save_preemption_timer(vmcs12)) |
| 2736 | return -EINVAL; |
| 2737 | |
Krish Sadhukhan | 461b4ba | 2018-12-12 13:30:07 -0500 | [diff] [blame] | 2738 | if (nested_cpu_has_ept(vmcs12) && |
Sean Christopherson | ac6389a | 2020-03-02 18:02:38 -0800 | [diff] [blame] | 2739 | CC(!nested_vmx_check_eptp(vcpu, vmcs12->ept_pointer))) |
Krish Sadhukhan | 461b4ba | 2018-12-12 13:30:07 -0500 | [diff] [blame] | 2740 | return -EINVAL; |
| 2741 | |
| 2742 | if (nested_cpu_has_vmfunc(vmcs12)) { |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 2743 | if (CC(vmcs12->vm_function_control & |
| 2744 | ~vmx->nested.msrs.vmfunc_controls)) |
Krish Sadhukhan | 461b4ba | 2018-12-12 13:30:07 -0500 | [diff] [blame] | 2745 | return -EINVAL; |
| 2746 | |
| 2747 | if (nested_cpu_has_eptp_switching(vmcs12)) { |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 2748 | if (CC(!nested_cpu_has_ept(vmcs12)) || |
| 2749 | CC(!page_address_valid(vcpu, vmcs12->eptp_list_address))) |
Krish Sadhukhan | 461b4ba | 2018-12-12 13:30:07 -0500 | [diff] [blame] | 2750 | return -EINVAL; |
| 2751 | } |
| 2752 | } |
| 2753 | |
| 2754 | return 0; |
| 2755 | } |
| 2756 | |
Krish Sadhukhan | 61446ba | 2018-12-12 13:30:09 -0500 | [diff] [blame] | 2757 | /* |
| 2758 | * Checks related to VM-Exit Control Fields |
| 2759 | */ |
| 2760 | static int nested_check_vm_exit_controls(struct kvm_vcpu *vcpu, |
| 2761 | struct vmcs12 *vmcs12) |
| 2762 | { |
| 2763 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 2764 | |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 2765 | if (CC(!vmx_control_verify(vmcs12->vm_exit_controls, |
| 2766 | vmx->nested.msrs.exit_ctls_low, |
| 2767 | vmx->nested.msrs.exit_ctls_high)) || |
| 2768 | CC(nested_vmx_check_exit_msr_switch_controls(vcpu, vmcs12))) |
Krish Sadhukhan | 61446ba | 2018-12-12 13:30:09 -0500 | [diff] [blame] | 2769 | return -EINVAL; |
| 2770 | |
| 2771 | return 0; |
| 2772 | } |
| 2773 | |
Krish Sadhukhan | 5fbf963 | 2018-12-12 13:30:10 -0500 | [diff] [blame] | 2774 | /* |
| 2775 | * Checks related to VM-Entry Control Fields |
| 2776 | */ |
| 2777 | static int nested_check_vm_entry_controls(struct kvm_vcpu *vcpu, |
| 2778 | struct vmcs12 *vmcs12) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2779 | { |
| 2780 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2781 | |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 2782 | if (CC(!vmx_control_verify(vmcs12->vm_entry_controls, |
| 2783 | vmx->nested.msrs.entry_ctls_low, |
| 2784 | vmx->nested.msrs.entry_ctls_high))) |
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 | /* |
| 2788 | * From the Intel SDM, volume 3: |
| 2789 | * Fields relevant to VM-entry event injection must be set properly. |
| 2790 | * These fields are the VM-entry interruption-information field, the |
| 2791 | * VM-entry exception error code, and the VM-entry instruction length. |
| 2792 | */ |
| 2793 | if (vmcs12->vm_entry_intr_info_field & INTR_INFO_VALID_MASK) { |
| 2794 | u32 intr_info = vmcs12->vm_entry_intr_info_field; |
| 2795 | u8 vector = intr_info & INTR_INFO_VECTOR_MASK; |
| 2796 | u32 intr_type = intr_info & INTR_INFO_INTR_TYPE_MASK; |
| 2797 | bool has_error_code = intr_info & INTR_INFO_DELIVER_CODE_MASK; |
| 2798 | bool should_have_error_code; |
| 2799 | bool urg = nested_cpu_has2(vmcs12, |
| 2800 | SECONDARY_EXEC_UNRESTRICTED_GUEST); |
| 2801 | bool prot_mode = !urg || vmcs12->guest_cr0 & X86_CR0_PE; |
| 2802 | |
| 2803 | /* VM-entry interruption-info field: interruption type */ |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 2804 | if (CC(intr_type == INTR_TYPE_RESERVED) || |
| 2805 | CC(intr_type == INTR_TYPE_OTHER_EVENT && |
| 2806 | !nested_cpu_supports_monitor_trap_flag(vcpu))) |
Krish Sadhukhan | 5fbf963 | 2018-12-12 13:30:10 -0500 | [diff] [blame] | 2807 | return -EINVAL; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2808 | |
| 2809 | /* VM-entry interruption-info field: vector */ |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 2810 | if (CC(intr_type == INTR_TYPE_NMI_INTR && vector != NMI_VECTOR) || |
| 2811 | CC(intr_type == INTR_TYPE_HARD_EXCEPTION && vector > 31) || |
| 2812 | CC(intr_type == INTR_TYPE_OTHER_EVENT && vector != 0)) |
Krish Sadhukhan | 5fbf963 | 2018-12-12 13:30:10 -0500 | [diff] [blame] | 2813 | return -EINVAL; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2814 | |
| 2815 | /* VM-entry interruption-info field: deliver error code */ |
| 2816 | should_have_error_code = |
| 2817 | intr_type == INTR_TYPE_HARD_EXCEPTION && prot_mode && |
| 2818 | x86_exception_has_error_code(vector); |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 2819 | if (CC(has_error_code != should_have_error_code)) |
Krish Sadhukhan | 5fbf963 | 2018-12-12 13:30:10 -0500 | [diff] [blame] | 2820 | return -EINVAL; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2821 | |
| 2822 | /* VM-entry exception error code */ |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 2823 | if (CC(has_error_code && |
Sean Christopherson | 567926c | 2019-10-01 09:21:23 -0700 | [diff] [blame] | 2824 | vmcs12->vm_entry_exception_error_code & GENMASK(31, 16))) |
Krish Sadhukhan | 5fbf963 | 2018-12-12 13:30:10 -0500 | [diff] [blame] | 2825 | return -EINVAL; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2826 | |
| 2827 | /* VM-entry interruption-info field: reserved bits */ |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 2828 | if (CC(intr_info & INTR_INFO_RESVD_BITS_MASK)) |
Krish Sadhukhan | 5fbf963 | 2018-12-12 13:30:10 -0500 | [diff] [blame] | 2829 | return -EINVAL; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2830 | |
| 2831 | /* VM-entry instruction length */ |
| 2832 | switch (intr_type) { |
| 2833 | case INTR_TYPE_SOFT_EXCEPTION: |
| 2834 | case INTR_TYPE_SOFT_INTR: |
| 2835 | case INTR_TYPE_PRIV_SW_EXCEPTION: |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 2836 | if (CC(vmcs12->vm_entry_instruction_len > 15) || |
| 2837 | CC(vmcs12->vm_entry_instruction_len == 0 && |
| 2838 | CC(!nested_cpu_has_zero_length_injection(vcpu)))) |
Krish Sadhukhan | 5fbf963 | 2018-12-12 13:30:10 -0500 | [diff] [blame] | 2839 | return -EINVAL; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2840 | } |
| 2841 | } |
| 2842 | |
Krish Sadhukhan | 5fbf963 | 2018-12-12 13:30:10 -0500 | [diff] [blame] | 2843 | if (nested_vmx_check_entry_msr_switch_controls(vcpu, vmcs12)) |
| 2844 | return -EINVAL; |
| 2845 | |
| 2846 | return 0; |
| 2847 | } |
| 2848 | |
Sean Christopherson | 5478ba3 | 2019-04-11 12:18:06 -0700 | [diff] [blame] | 2849 | static int nested_vmx_check_controls(struct kvm_vcpu *vcpu, |
| 2850 | struct vmcs12 *vmcs12) |
| 2851 | { |
| 2852 | if (nested_check_vm_execution_controls(vcpu, vmcs12) || |
| 2853 | nested_check_vm_exit_controls(vcpu, vmcs12) || |
| 2854 | nested_check_vm_entry_controls(vcpu, vmcs12)) |
Paolo Bonzini | 98d9e85 | 2019-04-12 10:19:57 +0200 | [diff] [blame] | 2855 | return -EINVAL; |
Sean Christopherson | 5478ba3 | 2019-04-11 12:18:06 -0700 | [diff] [blame] | 2856 | |
Vitaly Kuznetsov | a835023 | 2020-02-05 13:30:34 +0100 | [diff] [blame] | 2857 | if (to_vmx(vcpu)->nested.enlightened_vmcs_enabled) |
| 2858 | return nested_evmcs_check_controls(vmcs12); |
| 2859 | |
Sean Christopherson | 5478ba3 | 2019-04-11 12:18:06 -0700 | [diff] [blame] | 2860 | return 0; |
| 2861 | } |
| 2862 | |
Paolo Bonzini | 98d9e85 | 2019-04-12 10:19:57 +0200 | [diff] [blame] | 2863 | static int nested_vmx_check_host_state(struct kvm_vcpu *vcpu, |
| 2864 | struct vmcs12 *vmcs12) |
Krish Sadhukhan | 5fbf963 | 2018-12-12 13:30:10 -0500 | [diff] [blame] | 2865 | { |
| 2866 | bool ia32e; |
| 2867 | |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 2868 | if (CC(!nested_host_cr0_valid(vcpu, vmcs12->host_cr0)) || |
| 2869 | CC(!nested_host_cr4_valid(vcpu, vmcs12->host_cr4)) || |
Sean Christopherson | 636e8b7 | 2021-02-03 16:01:10 -0800 | [diff] [blame] | 2870 | CC(kvm_vcpu_is_illegal_gpa(vcpu, vmcs12->host_cr3))) |
Krish Sadhukhan | 254b2f3 | 2018-12-12 13:30:11 -0500 | [diff] [blame] | 2871 | return -EINVAL; |
Krish Sadhukhan | 711eff3 | 2019-02-07 14:05:30 -0500 | [diff] [blame] | 2872 | |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 2873 | if (CC(is_noncanonical_address(vmcs12->host_ia32_sysenter_esp, vcpu)) || |
| 2874 | CC(is_noncanonical_address(vmcs12->host_ia32_sysenter_eip, vcpu))) |
Krish Sadhukhan | 711eff3 | 2019-02-07 14:05:30 -0500 | [diff] [blame] | 2875 | return -EINVAL; |
| 2876 | |
Krish Sadhukhan | f6b0db1f | 2019-04-08 17:35:11 -0400 | [diff] [blame] | 2877 | if ((vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PAT) && |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 2878 | CC(!kvm_pat_valid(vmcs12->host_ia32_pat))) |
Krish Sadhukhan | f6b0db1f | 2019-04-08 17:35:11 -0400 | [diff] [blame] | 2879 | return -EINVAL; |
| 2880 | |
Oliver Upton | c547cb6 | 2019-11-13 16:17:17 -0800 | [diff] [blame] | 2881 | if ((vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL) && |
| 2882 | CC(!kvm_valid_perf_global_ctrl(vcpu_to_pmu(vcpu), |
| 2883 | vmcs12->host_ia32_perf_global_ctrl))) |
| 2884 | return -EINVAL; |
| 2885 | |
Paolo Bonzini | fd3edd4 | 2019-09-25 18:33:53 +0200 | [diff] [blame] | 2886 | #ifdef CONFIG_X86_64 |
| 2887 | ia32e = !!(vcpu->arch.efer & EFER_LMA); |
| 2888 | #else |
| 2889 | ia32e = false; |
| 2890 | #endif |
| 2891 | |
| 2892 | if (ia32e) { |
| 2893 | if (CC(!(vmcs12->vm_exit_controls & VM_EXIT_HOST_ADDR_SPACE_SIZE)) || |
| 2894 | CC(!(vmcs12->host_cr4 & X86_CR4_PAE))) |
| 2895 | return -EINVAL; |
| 2896 | } else { |
| 2897 | if (CC(vmcs12->vm_exit_controls & VM_EXIT_HOST_ADDR_SPACE_SIZE) || |
| 2898 | CC(vmcs12->vm_entry_controls & VM_ENTRY_IA32E_MODE) || |
| 2899 | CC(vmcs12->host_cr4 & X86_CR4_PCIDE) || |
| 2900 | CC((vmcs12->host_rip) >> 32)) |
| 2901 | return -EINVAL; |
| 2902 | } |
Krish Sadhukhan | 1ef23e1 | 2019-07-03 19:54:35 -0400 | [diff] [blame] | 2903 | |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 2904 | if (CC(vmcs12->host_cs_selector & (SEGMENT_RPL_MASK | SEGMENT_TI_MASK)) || |
| 2905 | CC(vmcs12->host_ss_selector & (SEGMENT_RPL_MASK | SEGMENT_TI_MASK)) || |
| 2906 | CC(vmcs12->host_ds_selector & (SEGMENT_RPL_MASK | SEGMENT_TI_MASK)) || |
| 2907 | CC(vmcs12->host_es_selector & (SEGMENT_RPL_MASK | SEGMENT_TI_MASK)) || |
| 2908 | CC(vmcs12->host_fs_selector & (SEGMENT_RPL_MASK | SEGMENT_TI_MASK)) || |
| 2909 | CC(vmcs12->host_gs_selector & (SEGMENT_RPL_MASK | SEGMENT_TI_MASK)) || |
| 2910 | CC(vmcs12->host_tr_selector & (SEGMENT_RPL_MASK | SEGMENT_TI_MASK)) || |
| 2911 | CC(vmcs12->host_cs_selector == 0) || |
| 2912 | CC(vmcs12->host_tr_selector == 0) || |
| 2913 | CC(vmcs12->host_ss_selector == 0 && !ia32e)) |
Krish Sadhukhan | 1ef23e1 | 2019-07-03 19:54:35 -0400 | [diff] [blame] | 2914 | return -EINVAL; |
| 2915 | |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 2916 | if (CC(is_noncanonical_address(vmcs12->host_fs_base, vcpu)) || |
| 2917 | CC(is_noncanonical_address(vmcs12->host_gs_base, vcpu)) || |
| 2918 | CC(is_noncanonical_address(vmcs12->host_gdtr_base, vcpu)) || |
| 2919 | CC(is_noncanonical_address(vmcs12->host_idtr_base, vcpu)) || |
Paolo Bonzini | fd3edd4 | 2019-09-25 18:33:53 +0200 | [diff] [blame] | 2920 | CC(is_noncanonical_address(vmcs12->host_tr_base, vcpu)) || |
| 2921 | CC(is_noncanonical_address(vmcs12->host_rip, vcpu))) |
Krish Sadhukhan | 5845038 | 2019-08-09 12:26:19 -0700 | [diff] [blame] | 2922 | return -EINVAL; |
Krish Sadhukhan | 1ef23e1 | 2019-07-03 19:54:35 -0400 | [diff] [blame] | 2923 | |
Krish Sadhukhan | 5fbf963 | 2018-12-12 13:30:10 -0500 | [diff] [blame] | 2924 | /* |
| 2925 | * If the load IA32_EFER VM-exit control is 1, bits reserved in the |
| 2926 | * IA32_EFER MSR must be 0 in the field for that register. In addition, |
| 2927 | * the values of the LMA and LME bits in the field must each be that of |
| 2928 | * the host address-space size VM-exit control. |
| 2929 | */ |
| 2930 | if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_EFER) { |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 2931 | if (CC(!kvm_valid_efer(vcpu, vmcs12->host_ia32_efer)) || |
| 2932 | CC(ia32e != !!(vmcs12->host_ia32_efer & EFER_LMA)) || |
| 2933 | CC(ia32e != !!(vmcs12->host_ia32_efer & EFER_LME))) |
Krish Sadhukhan | 254b2f3 | 2018-12-12 13:30:11 -0500 | [diff] [blame] | 2934 | return -EINVAL; |
Krish Sadhukhan | 5fbf963 | 2018-12-12 13:30:10 -0500 | [diff] [blame] | 2935 | } |
| 2936 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2937 | return 0; |
| 2938 | } |
| 2939 | |
| 2940 | static int nested_vmx_check_vmcs_link_ptr(struct kvm_vcpu *vcpu, |
| 2941 | struct vmcs12 *vmcs12) |
| 2942 | { |
KarimAllah Ahmed | 8892530 | 2019-01-31 21:24:41 +0100 | [diff] [blame] | 2943 | int r = 0; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2944 | struct vmcs12 *shadow; |
KarimAllah Ahmed | 8892530 | 2019-01-31 21:24:41 +0100 | [diff] [blame] | 2945 | struct kvm_host_map map; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2946 | |
| 2947 | if (vmcs12->vmcs_link_pointer == -1ull) |
| 2948 | return 0; |
| 2949 | |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 2950 | if (CC(!page_address_valid(vcpu, vmcs12->vmcs_link_pointer))) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2951 | return -EINVAL; |
| 2952 | |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 2953 | 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] | 2954 | return -EINVAL; |
| 2955 | |
KarimAllah Ahmed | 8892530 | 2019-01-31 21:24:41 +0100 | [diff] [blame] | 2956 | shadow = map.hva; |
| 2957 | |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 2958 | if (CC(shadow->hdr.revision_id != VMCS12_REVISION) || |
| 2959 | CC(shadow->hdr.shadow_vmcs != nested_cpu_has_shadow_vmcs(vmcs12))) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2960 | r = -EINVAL; |
KarimAllah Ahmed | 8892530 | 2019-01-31 21:24:41 +0100 | [diff] [blame] | 2961 | |
| 2962 | kvm_vcpu_unmap(vcpu, &map, false); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2963 | return r; |
| 2964 | } |
| 2965 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2966 | /* |
| 2967 | * Checks related to Guest Non-register State |
| 2968 | */ |
| 2969 | static int nested_check_guest_non_reg_state(struct vmcs12 *vmcs12) |
| 2970 | { |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 2971 | if (CC(vmcs12->guest_activity_state != GUEST_ACTIVITY_ACTIVE && |
Yadong Qi | bf0cd88 | 2020-11-06 14:51:22 +0800 | [diff] [blame] | 2972 | vmcs12->guest_activity_state != GUEST_ACTIVITY_HLT && |
| 2973 | vmcs12->guest_activity_state != GUEST_ACTIVITY_WAIT_SIPI)) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2974 | return -EINVAL; |
| 2975 | |
| 2976 | return 0; |
| 2977 | } |
| 2978 | |
Sean Christopherson | 5478ba3 | 2019-04-11 12:18:06 -0700 | [diff] [blame] | 2979 | static int nested_vmx_check_guest_state(struct kvm_vcpu *vcpu, |
| 2980 | struct vmcs12 *vmcs12, |
Sean Christopherson | 68cda40 | 2020-05-11 15:05:29 -0700 | [diff] [blame] | 2981 | enum vm_entry_failure_code *entry_failure_code) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2982 | { |
| 2983 | bool ia32e; |
| 2984 | |
Sean Christopherson | 68cda40 | 2020-05-11 15:05:29 -0700 | [diff] [blame] | 2985 | *entry_failure_code = ENTRY_FAIL_DEFAULT; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2986 | |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 2987 | if (CC(!nested_guest_cr0_valid(vcpu, vmcs12->guest_cr0)) || |
| 2988 | CC(!nested_guest_cr4_valid(vcpu, vmcs12->guest_cr4))) |
Sean Christopherson | c80add0 | 2019-04-11 12:18:09 -0700 | [diff] [blame] | 2989 | return -EINVAL; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2990 | |
Krish Sadhukhan | b91991b | 2020-01-15 19:54:32 -0500 | [diff] [blame] | 2991 | if ((vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS) && |
| 2992 | CC(!kvm_dr7_valid(vmcs12->guest_dr7))) |
| 2993 | return -EINVAL; |
| 2994 | |
Krish Sadhukhan | de2bc2b | 2019-04-08 17:35:12 -0400 | [diff] [blame] | 2995 | if ((vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_PAT) && |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 2996 | CC(!kvm_pat_valid(vmcs12->guest_ia32_pat))) |
Sean Christopherson | c80add0 | 2019-04-11 12:18:09 -0700 | [diff] [blame] | 2997 | return -EINVAL; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2998 | |
| 2999 | if (nested_vmx_check_vmcs_link_ptr(vcpu, vmcs12)) { |
Sean Christopherson | 68cda40 | 2020-05-11 15:05:29 -0700 | [diff] [blame] | 3000 | *entry_failure_code = ENTRY_FAIL_VMCS_LINK_PTR; |
Sean Christopherson | c80add0 | 2019-04-11 12:18:09 -0700 | [diff] [blame] | 3001 | return -EINVAL; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3002 | } |
| 3003 | |
Oliver Upton | bfc6ad6 | 2019-11-13 16:17:16 -0800 | [diff] [blame] | 3004 | if ((vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL) && |
| 3005 | CC(!kvm_valid_perf_global_ctrl(vcpu_to_pmu(vcpu), |
| 3006 | vmcs12->guest_ia32_perf_global_ctrl))) |
| 3007 | return -EINVAL; |
| 3008 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3009 | /* |
| 3010 | * If the load IA32_EFER VM-entry control is 1, the following checks |
| 3011 | * are performed on the field for the IA32_EFER MSR: |
| 3012 | * - Bits reserved in the IA32_EFER MSR must be 0. |
| 3013 | * - Bit 10 (corresponding to IA32_EFER.LMA) must equal the value of |
| 3014 | * the IA-32e mode guest VM-exit control. It must also be identical |
| 3015 | * to bit 8 (LME) if bit 31 in the CR0 field (corresponding to |
| 3016 | * CR0.PG) is 1. |
| 3017 | */ |
| 3018 | if (to_vmx(vcpu)->nested.nested_run_pending && |
| 3019 | (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_EFER)) { |
| 3020 | ia32e = (vmcs12->vm_entry_controls & VM_ENTRY_IA32E_MODE) != 0; |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 3021 | if (CC(!kvm_valid_efer(vcpu, vmcs12->guest_ia32_efer)) || |
| 3022 | CC(ia32e != !!(vmcs12->guest_ia32_efer & EFER_LMA)) || |
| 3023 | CC(((vmcs12->guest_cr0 & X86_CR0_PG) && |
| 3024 | ia32e != !!(vmcs12->guest_ia32_efer & EFER_LME)))) |
Sean Christopherson | c80add0 | 2019-04-11 12:18:09 -0700 | [diff] [blame] | 3025 | return -EINVAL; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3026 | } |
| 3027 | |
| 3028 | if ((vmcs12->vm_entry_controls & VM_ENTRY_LOAD_BNDCFGS) && |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 3029 | (CC(is_noncanonical_address(vmcs12->guest_bndcfgs & PAGE_MASK, vcpu)) || |
| 3030 | CC((vmcs12->guest_bndcfgs & MSR_IA32_BNDCFGS_RSVD)))) |
Sean Christopherson | c80add0 | 2019-04-11 12:18:09 -0700 | [diff] [blame] | 3031 | return -EINVAL; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3032 | |
Sean Christopherson | 9c3e922 | 2019-04-11 12:18:05 -0700 | [diff] [blame] | 3033 | if (nested_check_guest_non_reg_state(vmcs12)) |
Sean Christopherson | c80add0 | 2019-04-11 12:18:09 -0700 | [diff] [blame] | 3034 | return -EINVAL; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3035 | |
| 3036 | return 0; |
| 3037 | } |
| 3038 | |
Sean Christopherson | 453eafb | 2018-12-20 12:25:17 -0800 | [diff] [blame] | 3039 | static int nested_vmx_check_vmentry_hw(struct kvm_vcpu *vcpu) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3040 | { |
| 3041 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 3042 | unsigned long cr3, cr4; |
Sean Christopherson | f1727b4 | 2019-01-25 07:40:58 -0800 | [diff] [blame] | 3043 | bool vm_fail; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3044 | |
| 3045 | if (!nested_early_check) |
| 3046 | return 0; |
| 3047 | |
| 3048 | if (vmx->msr_autoload.host.nr) |
| 3049 | vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, 0); |
| 3050 | if (vmx->msr_autoload.guest.nr) |
| 3051 | vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, 0); |
| 3052 | |
| 3053 | preempt_disable(); |
| 3054 | |
| 3055 | vmx_prepare_switch_to_guest(vcpu); |
| 3056 | |
| 3057 | /* |
| 3058 | * Induce a consistency check VMExit by clearing bit 1 in GUEST_RFLAGS, |
| 3059 | * which is reserved to '1' by hardware. GUEST_RFLAGS is guaranteed to |
Miaohe Lin | 49f933d | 2020-02-27 11:20:54 +0800 | [diff] [blame] | 3060 | * be written (by prepare_vmcs02()) before the "real" VMEnter, i.e. |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3061 | * there is no need to preserve other bits or save/restore the field. |
| 3062 | */ |
| 3063 | vmcs_writel(GUEST_RFLAGS, 0); |
| 3064 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3065 | cr3 = __get_current_cr3_fast(); |
| 3066 | if (unlikely(cr3 != vmx->loaded_vmcs->host_state.cr3)) { |
| 3067 | vmcs_writel(HOST_CR3, cr3); |
| 3068 | vmx->loaded_vmcs->host_state.cr3 = cr3; |
| 3069 | } |
| 3070 | |
| 3071 | cr4 = cr4_read_shadow(); |
| 3072 | if (unlikely(cr4 != vmx->loaded_vmcs->host_state.cr4)) { |
| 3073 | vmcs_writel(HOST_CR4, cr4); |
| 3074 | vmx->loaded_vmcs->host_state.cr4 = cr4; |
| 3075 | } |
| 3076 | |
Uros Bizjak | 150f17b | 2020-12-30 16:26:57 -0800 | [diff] [blame] | 3077 | vm_fail = __vmx_vcpu_run(vmx, (unsigned long *)&vcpu->arch.regs, |
| 3078 | vmx->loaded_vmcs->launched); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3079 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3080 | if (vmx->msr_autoload.host.nr) |
| 3081 | vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, vmx->msr_autoload.host.nr); |
| 3082 | if (vmx->msr_autoload.guest.nr) |
| 3083 | vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, vmx->msr_autoload.guest.nr); |
| 3084 | |
Sean Christopherson | f1727b4 | 2019-01-25 07:40:58 -0800 | [diff] [blame] | 3085 | if (vm_fail) { |
Sean Christopherson | 380e005 | 2019-07-11 08:58:30 -0700 | [diff] [blame] | 3086 | u32 error = vmcs_read32(VM_INSTRUCTION_ERROR); |
| 3087 | |
Wanpeng Li | 541e886 | 2019-05-17 16:49:50 +0800 | [diff] [blame] | 3088 | preempt_enable(); |
Sean Christopherson | 380e005 | 2019-07-11 08:58:30 -0700 | [diff] [blame] | 3089 | |
| 3090 | trace_kvm_nested_vmenter_failed( |
| 3091 | "early hardware check VM-instruction error: ", error); |
| 3092 | WARN_ON_ONCE(error != VMXERR_ENTRY_INVALID_CONTROL_FIELD); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3093 | return 1; |
| 3094 | } |
| 3095 | |
| 3096 | /* |
| 3097 | * VMExit clears RFLAGS.IF and DR7, even on a consistency check. |
| 3098 | */ |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3099 | if (hw_breakpoint_active()) |
| 3100 | set_debugreg(__this_cpu_read(cpu_dr7), 7); |
Peter Zijlstra | 84b6a34 | 2020-05-29 23:27:36 +0200 | [diff] [blame] | 3101 | local_irq_enable(); |
Wanpeng Li | 541e886 | 2019-05-17 16:49:50 +0800 | [diff] [blame] | 3102 | preempt_enable(); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3103 | |
| 3104 | /* |
| 3105 | * A non-failing VMEntry means we somehow entered guest mode with |
| 3106 | * an illegal RIP, and that's just the tip of the iceberg. There |
| 3107 | * is no telling what memory has been modified or what state has |
| 3108 | * been exposed to unknown code. Hitting this all but guarantees |
| 3109 | * a (very critical) hardware issue. |
| 3110 | */ |
| 3111 | WARN_ON(!(vmcs_read32(VM_EXIT_REASON) & |
| 3112 | VMX_EXIT_REASONS_FAILED_VMENTRY)); |
| 3113 | |
| 3114 | return 0; |
| 3115 | } |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3116 | |
Paolo Bonzini | 9a78e15 | 2021-01-08 11:43:08 -0500 | [diff] [blame] | 3117 | static bool nested_get_evmcs_page(struct kvm_vcpu *vcpu) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3118 | { |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3119 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3120 | |
Vitaly Kuznetsov | e942dbf | 2020-03-09 16:52:12 +0100 | [diff] [blame] | 3121 | /* |
| 3122 | * hv_evmcs may end up being not mapped after migration (when |
| 3123 | * L2 was running), map it here to make sure vmcs12 changes are |
| 3124 | * properly reflected. |
| 3125 | */ |
Vitaly Kuznetsov | 1e9dfbd | 2021-05-26 15:20:16 +0200 | [diff] [blame] | 3126 | if (vmx->nested.enlightened_vmcs_enabled && |
Vitaly Kuznetsov | 2784996 | 2021-05-26 15:20:20 +0200 | [diff] [blame] | 3127 | vmx->nested.hv_evmcs_vmptr == EVMPTR_MAP_PENDING) { |
Vitaly Kuznetsov | b6a0653 | 2020-03-09 16:52:13 +0100 | [diff] [blame] | 3128 | enum nested_evmptrld_status evmptrld_status = |
| 3129 | nested_vmx_handle_enlightened_vmptrld(vcpu, false); |
| 3130 | |
| 3131 | if (evmptrld_status == EVMPTRLD_VMFAIL || |
Vitaly Kuznetsov | f5c7e84 | 2021-05-03 17:08:51 +0200 | [diff] [blame] | 3132 | evmptrld_status == EVMPTRLD_ERROR) |
Vitaly Kuznetsov | b6a0653 | 2020-03-09 16:52:13 +0100 | [diff] [blame] | 3133 | return false; |
Vitaly Kuznetsov | 8629b62 | 2021-05-26 15:20:25 +0200 | [diff] [blame] | 3134 | |
| 3135 | /* |
| 3136 | * Post migration VMCS12 always provides the most actual |
| 3137 | * information, copy it to eVMCS upon entry. |
| 3138 | */ |
| 3139 | vmx->nested.need_vmcs12_to_shadow_sync = true; |
Vitaly Kuznetsov | b6a0653 | 2020-03-09 16:52:13 +0100 | [diff] [blame] | 3140 | } |
Vitaly Kuznetsov | e942dbf | 2020-03-09 16:52:12 +0100 | [diff] [blame] | 3141 | |
Paolo Bonzini | 9a78e15 | 2021-01-08 11:43:08 -0500 | [diff] [blame] | 3142 | return true; |
| 3143 | } |
| 3144 | |
| 3145 | static bool nested_get_vmcs12_pages(struct kvm_vcpu *vcpu) |
| 3146 | { |
| 3147 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
| 3148 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 3149 | struct kvm_host_map *map; |
| 3150 | struct page *page; |
| 3151 | u64 hpa; |
| 3152 | |
Maxim Levitsky | 158a48e | 2021-06-07 12:02:03 +0300 | [diff] [blame] | 3153 | if (!vcpu->arch.pdptrs_from_userspace && |
| 3154 | !nested_cpu_has_ept(vmcs12) && is_pae_paging(vcpu)) { |
Maxim Levitsky | 0f85722 | 2021-06-07 12:02:00 +0300 | [diff] [blame] | 3155 | /* |
| 3156 | * Reload the guest's PDPTRs since after a migration |
| 3157 | * the guest CR3 might be restored prior to setting the nested |
| 3158 | * state which can lead to a load of wrong PDPTRs. |
| 3159 | */ |
| 3160 | if (CC(!load_pdptrs(vcpu, vcpu->arch.walk_mmu, vcpu->arch.cr3))) |
| 3161 | return false; |
| 3162 | } |
| 3163 | |
| 3164 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3165 | if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) { |
| 3166 | /* |
| 3167 | * Translate L1 physical address to host physical |
| 3168 | * address for vmcs02. Keep the page pinned, so this |
| 3169 | * physical address remains valid. We keep a reference |
| 3170 | * to it so we can release it later. |
| 3171 | */ |
| 3172 | if (vmx->nested.apic_access_page) { /* shouldn't happen */ |
Liran Alon | b11494b | 2019-11-21 00:31:47 +0200 | [diff] [blame] | 3173 | kvm_release_page_clean(vmx->nested.apic_access_page); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3174 | vmx->nested.apic_access_page = NULL; |
| 3175 | } |
| 3176 | page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->apic_access_addr); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3177 | if (!is_error_page(page)) { |
| 3178 | vmx->nested.apic_access_page = page; |
| 3179 | hpa = page_to_phys(vmx->nested.apic_access_page); |
| 3180 | vmcs_write64(APIC_ACCESS_ADDR, hpa); |
| 3181 | } else { |
Jim Mattson | 671ddc7 | 2019-10-15 10:44:05 -0700 | [diff] [blame] | 3182 | pr_debug_ratelimited("%s: no backing 'struct page' for APIC-access address in vmcs12\n", |
| 3183 | __func__); |
| 3184 | vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR; |
| 3185 | vcpu->run->internal.suberror = |
| 3186 | KVM_INTERNAL_ERROR_EMULATION; |
| 3187 | vcpu->run->internal.ndata = 0; |
| 3188 | return false; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3189 | } |
| 3190 | } |
| 3191 | |
| 3192 | if (nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW)) { |
KarimAllah Ahmed | 96c66e8 | 2019-01-31 21:24:37 +0100 | [diff] [blame] | 3193 | map = &vmx->nested.virtual_apic_map; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3194 | |
KarimAllah Ahmed | 96c66e8 | 2019-01-31 21:24:37 +0100 | [diff] [blame] | 3195 | if (!kvm_vcpu_map(vcpu, gpa_to_gfn(vmcs12->virtual_apic_page_addr), map)) { |
| 3196 | vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, pfn_to_hpa(map->pfn)); |
Paolo Bonzini | 6909081 | 2019-04-15 15:16:17 +0200 | [diff] [blame] | 3197 | } else if (nested_cpu_has(vmcs12, CPU_BASED_CR8_LOAD_EXITING) && |
| 3198 | nested_cpu_has(vmcs12, CPU_BASED_CR8_STORE_EXITING) && |
| 3199 | !nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) { |
| 3200 | /* |
| 3201 | * The processor will never use the TPR shadow, simply |
| 3202 | * clear the bit from the execution control. Such a |
| 3203 | * configuration is useless, but it happens in tests. |
| 3204 | * For any other configuration, failing the vm entry is |
| 3205 | * _not_ what the processor does but it's basically the |
| 3206 | * only possibility we have. |
| 3207 | */ |
Sean Christopherson | 2183f56 | 2019-05-07 12:17:56 -0700 | [diff] [blame] | 3208 | exec_controls_clearbit(vmx, CPU_BASED_TPR_SHADOW); |
Paolo Bonzini | 6909081 | 2019-04-15 15:16:17 +0200 | [diff] [blame] | 3209 | } else { |
Sean Christopherson | ca2f546 | 2019-05-07 09:06:33 -0700 | [diff] [blame] | 3210 | /* |
| 3211 | * Write an illegal value to VIRTUAL_APIC_PAGE_ADDR to |
| 3212 | * force VM-Entry to fail. |
| 3213 | */ |
| 3214 | vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, -1ull); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3215 | } |
| 3216 | } |
| 3217 | |
| 3218 | if (nested_cpu_has_posted_intr(vmcs12)) { |
KarimAllah Ahmed | 3278e04 | 2019-01-31 21:24:38 +0100 | [diff] [blame] | 3219 | map = &vmx->nested.pi_desc_map; |
| 3220 | |
| 3221 | if (!kvm_vcpu_map(vcpu, gpa_to_gfn(vmcs12->posted_intr_desc_addr), map)) { |
| 3222 | vmx->nested.pi_desc = |
| 3223 | (struct pi_desc *)(((void *)map->hva) + |
| 3224 | offset_in_page(vmcs12->posted_intr_desc_addr)); |
| 3225 | vmcs_write64(POSTED_INTR_DESC_ADDR, |
| 3226 | 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] | 3227 | } else { |
| 3228 | /* |
| 3229 | * Defer the KVM_INTERNAL_EXIT until KVM tries to |
| 3230 | * access the contents of the VMCS12 posted interrupt |
| 3231 | * descriptor. (Note that KVM may do this when it |
| 3232 | * should not, per the architectural specification.) |
| 3233 | */ |
| 3234 | vmx->nested.pi_desc = NULL; |
| 3235 | pin_controls_clearbit(vmx, PIN_BASED_POSTED_INTR); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3236 | } |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3237 | } |
| 3238 | if (nested_vmx_prepare_msr_bitmap(vcpu, vmcs12)) |
Sean Christopherson | 2183f56 | 2019-05-07 12:17:56 -0700 | [diff] [blame] | 3239 | exec_controls_setbit(vmx, CPU_BASED_USE_MSR_BITMAPS); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3240 | else |
Sean Christopherson | 2183f56 | 2019-05-07 12:17:56 -0700 | [diff] [blame] | 3241 | exec_controls_clearbit(vmx, CPU_BASED_USE_MSR_BITMAPS); |
Paolo Bonzini | 9a78e15 | 2021-01-08 11:43:08 -0500 | [diff] [blame] | 3242 | |
| 3243 | return true; |
| 3244 | } |
| 3245 | |
| 3246 | static bool vmx_get_nested_state_pages(struct kvm_vcpu *vcpu) |
| 3247 | { |
Vitaly Kuznetsov | f5c7e84 | 2021-05-03 17:08:51 +0200 | [diff] [blame] | 3248 | if (!nested_get_evmcs_page(vcpu)) { |
| 3249 | pr_debug_ratelimited("%s: enlightened vmptrld failed\n", |
| 3250 | __func__); |
| 3251 | vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR; |
| 3252 | vcpu->run->internal.suberror = |
| 3253 | KVM_INTERNAL_ERROR_EMULATION; |
| 3254 | vcpu->run->internal.ndata = 0; |
| 3255 | |
Paolo Bonzini | 9a78e15 | 2021-01-08 11:43:08 -0500 | [diff] [blame] | 3256 | return false; |
Vitaly Kuznetsov | f5c7e84 | 2021-05-03 17:08:51 +0200 | [diff] [blame] | 3257 | } |
Paolo Bonzini | 9a78e15 | 2021-01-08 11:43:08 -0500 | [diff] [blame] | 3258 | |
| 3259 | if (is_guest_mode(vcpu) && !nested_get_vmcs12_pages(vcpu)) |
| 3260 | return false; |
| 3261 | |
Jim Mattson | 671ddc7 | 2019-10-15 10:44:05 -0700 | [diff] [blame] | 3262 | return true; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3263 | } |
| 3264 | |
Sean Christopherson | 02f5fb2 | 2020-06-22 14:58:32 -0700 | [diff] [blame] | 3265 | static int nested_vmx_write_pml_buffer(struct kvm_vcpu *vcpu, gpa_t gpa) |
| 3266 | { |
| 3267 | struct vmcs12 *vmcs12; |
| 3268 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 3269 | gpa_t dst; |
| 3270 | |
| 3271 | if (WARN_ON_ONCE(!is_guest_mode(vcpu))) |
| 3272 | return 0; |
| 3273 | |
| 3274 | if (WARN_ON_ONCE(vmx->nested.pml_full)) |
| 3275 | return 1; |
| 3276 | |
| 3277 | /* |
| 3278 | * Check if PML is enabled for the nested guest. Whether eptp bit 6 is |
| 3279 | * set is already checked as part of A/D emulation. |
| 3280 | */ |
| 3281 | vmcs12 = get_vmcs12(vcpu); |
| 3282 | if (!nested_cpu_has_pml(vmcs12)) |
| 3283 | return 0; |
| 3284 | |
| 3285 | if (vmcs12->guest_pml_index >= PML_ENTITY_NUM) { |
| 3286 | vmx->nested.pml_full = true; |
| 3287 | return 1; |
| 3288 | } |
| 3289 | |
| 3290 | gpa &= ~0xFFFull; |
| 3291 | dst = vmcs12->pml_address + sizeof(u64) * vmcs12->guest_pml_index; |
| 3292 | |
| 3293 | if (kvm_write_guest_page(vcpu->kvm, gpa_to_gfn(dst), &gpa, |
| 3294 | offset_in_page(dst), sizeof(gpa))) |
| 3295 | return 0; |
| 3296 | |
| 3297 | vmcs12->guest_pml_index--; |
| 3298 | |
| 3299 | return 0; |
| 3300 | } |
| 3301 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3302 | /* |
| 3303 | * Intel's VMX Instruction Reference specifies a common set of prerequisites |
| 3304 | * for running VMX instructions (except VMXON, whose prerequisites are |
| 3305 | * slightly different). It also specifies what exception to inject otherwise. |
| 3306 | * Note that many of these exceptions have priority over VM exits, so they |
| 3307 | * don't have to be checked again here. |
| 3308 | */ |
| 3309 | static int nested_vmx_check_permission(struct kvm_vcpu *vcpu) |
| 3310 | { |
| 3311 | if (!to_vmx(vcpu)->nested.vmxon) { |
| 3312 | kvm_queue_exception(vcpu, UD_VECTOR); |
| 3313 | return 0; |
| 3314 | } |
| 3315 | |
| 3316 | if (vmx_get_cpl(vcpu)) { |
| 3317 | kvm_inject_gp(vcpu, 0); |
| 3318 | return 0; |
| 3319 | } |
| 3320 | |
| 3321 | return 1; |
| 3322 | } |
| 3323 | |
| 3324 | static u8 vmx_has_apicv_interrupt(struct kvm_vcpu *vcpu) |
| 3325 | { |
| 3326 | u8 rvi = vmx_get_rvi(); |
| 3327 | u8 vppr = kvm_lapic_get_reg(vcpu->arch.apic, APIC_PROCPRI); |
| 3328 | |
| 3329 | return ((rvi & 0xf0) > (vppr & 0xf0)); |
| 3330 | } |
| 3331 | |
| 3332 | static void load_vmcs12_host_state(struct kvm_vcpu *vcpu, |
| 3333 | struct vmcs12 *vmcs12); |
| 3334 | |
| 3335 | /* |
| 3336 | * If from_vmentry is false, this is being called from state restore (either RSM |
| 3337 | * or KVM_SET_NESTED_STATE). Otherwise it's called from vmlaunch/vmresume. |
Jim Mattson | 671ddc7 | 2019-10-15 10:44:05 -0700 | [diff] [blame] | 3338 | * |
| 3339 | * Returns: |
Miaohe Lin | 463bfee | 2020-02-14 10:44:05 +0800 | [diff] [blame] | 3340 | * NVMX_VMENTRY_SUCCESS: Entered VMX non-root mode |
| 3341 | * NVMX_VMENTRY_VMFAIL: Consistency check VMFail |
| 3342 | * NVMX_VMENTRY_VMEXIT: Consistency check VMExit |
| 3343 | * NVMX_VMENTRY_KVM_INTERNAL_ERROR: KVM internal error |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3344 | */ |
Jim Mattson | 671ddc7 | 2019-10-15 10:44:05 -0700 | [diff] [blame] | 3345 | enum nvmx_vmentry_status nested_vmx_enter_non_root_mode(struct kvm_vcpu *vcpu, |
| 3346 | bool from_vmentry) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3347 | { |
| 3348 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 3349 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
Sean Christopherson | 68cda40 | 2020-05-11 15:05:29 -0700 | [diff] [blame] | 3350 | enum vm_entry_failure_code entry_failure_code; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3351 | bool evaluate_pending_interrupts; |
Sean Christopherson | 8e53324 | 2020-11-06 17:03:12 +0800 | [diff] [blame] | 3352 | union vmx_exit_reason exit_reason = { |
| 3353 | .basic = EXIT_REASON_INVALID_STATE, |
| 3354 | .failed_vmentry = 1, |
| 3355 | }; |
| 3356 | u32 failed_index; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3357 | |
Sean Christopherson | eeeb4f6 | 2020-03-20 14:28:20 -0700 | [diff] [blame] | 3358 | if (kvm_check_request(KVM_REQ_TLB_FLUSH_CURRENT, vcpu)) |
| 3359 | kvm_vcpu_flush_tlb_current(vcpu); |
| 3360 | |
Sean Christopherson | 2183f56 | 2019-05-07 12:17:56 -0700 | [diff] [blame] | 3361 | evaluate_pending_interrupts = exec_controls_get(vmx) & |
Xiaoyao Li | 4e2a0bc | 2019-12-06 16:45:25 +0800 | [diff] [blame] | 3362 | (CPU_BASED_INTR_WINDOW_EXITING | CPU_BASED_NMI_WINDOW_EXITING); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3363 | if (likely(!evaluate_pending_interrupts) && kvm_vcpu_apicv_active(vcpu)) |
| 3364 | evaluate_pending_interrupts |= vmx_has_apicv_interrupt(vcpu); |
| 3365 | |
| 3366 | if (!(vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS)) |
| 3367 | vmx->nested.vmcs01_debugctl = vmcs_read64(GUEST_IA32_DEBUGCTL); |
| 3368 | if (kvm_mpx_supported() && |
| 3369 | !(vmcs12->vm_entry_controls & VM_ENTRY_LOAD_BNDCFGS)) |
| 3370 | vmx->nested.vmcs01_guest_bndcfgs = vmcs_read64(GUEST_BNDCFGS); |
| 3371 | |
Sean Christopherson | f087a02 | 2019-06-07 11:55:34 -0700 | [diff] [blame] | 3372 | /* |
| 3373 | * Overwrite vmcs01.GUEST_CR3 with L1's CR3 if EPT is disabled *and* |
| 3374 | * nested early checks are disabled. In the event of a "late" VM-Fail, |
| 3375 | * i.e. a VM-Fail detected by hardware but not KVM, KVM must unwind its |
| 3376 | * software model to the pre-VMEntry host state. When EPT is disabled, |
| 3377 | * GUEST_CR3 holds KVM's shadow CR3, not L1's "real" CR3, which causes |
| 3378 | * nested_vmx_restore_host_state() to corrupt vcpu->arch.cr3. Stuffing |
| 3379 | * vmcs01.GUEST_CR3 results in the unwind naturally setting arch.cr3 to |
| 3380 | * the correct value. Smashing vmcs01.GUEST_CR3 is safe because nested |
| 3381 | * VM-Exits, and the unwind, reset KVM's MMU, i.e. vmcs01.GUEST_CR3 is |
| 3382 | * guaranteed to be overwritten with a shadow CR3 prior to re-entering |
| 3383 | * L1. Don't stuff vmcs01.GUEST_CR3 when using nested early checks as |
| 3384 | * KVM modifies vcpu->arch.cr3 if and only if the early hardware checks |
| 3385 | * pass, and early VM-Fails do not reset KVM's MMU, i.e. the VM-Fail |
| 3386 | * path would need to manually save/restore vmcs01.GUEST_CR3. |
| 3387 | */ |
| 3388 | if (!enable_ept && !nested_early_check) |
| 3389 | vmcs_writel(GUEST_CR3, vcpu->arch.cr3); |
| 3390 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3391 | vmx_switch_vmcs(vcpu, &vmx->nested.vmcs02); |
| 3392 | |
Sean Christopherson | 389ab25 | 2021-08-10 10:19:50 -0700 | [diff] [blame] | 3393 | prepare_vmcs02_early(vmx, &vmx->vmcs01, vmcs12); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3394 | |
| 3395 | if (from_vmentry) { |
Sean Christopherson | b89d5ad | 2020-09-23 11:44:47 -0700 | [diff] [blame] | 3396 | if (unlikely(!nested_get_vmcs12_pages(vcpu))) { |
| 3397 | vmx_switch_vmcs(vcpu, &vmx->vmcs01); |
Jim Mattson | 671ddc7 | 2019-10-15 10:44:05 -0700 | [diff] [blame] | 3398 | return NVMX_VMENTRY_KVM_INTERNAL_ERROR; |
Sean Christopherson | b89d5ad | 2020-09-23 11:44:47 -0700 | [diff] [blame] | 3399 | } |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3400 | |
| 3401 | if (nested_vmx_check_vmentry_hw(vcpu)) { |
| 3402 | vmx_switch_vmcs(vcpu, &vmx->vmcs01); |
Jim Mattson | 671ddc7 | 2019-10-15 10:44:05 -0700 | [diff] [blame] | 3403 | return NVMX_VMENTRY_VMFAIL; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3404 | } |
| 3405 | |
Sean Christopherson | 68cda40 | 2020-05-11 15:05:29 -0700 | [diff] [blame] | 3406 | if (nested_vmx_check_guest_state(vcpu, vmcs12, |
| 3407 | &entry_failure_code)) { |
Sean Christopherson | 8e53324 | 2020-11-06 17:03:12 +0800 | [diff] [blame] | 3408 | exit_reason.basic = EXIT_REASON_INVALID_STATE; |
Sean Christopherson | 68cda40 | 2020-05-11 15:05:29 -0700 | [diff] [blame] | 3409 | vmcs12->exit_qualification = entry_failure_code; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3410 | goto vmentry_fail_vmexit; |
Sean Christopherson | 68cda40 | 2020-05-11 15:05:29 -0700 | [diff] [blame] | 3411 | } |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3412 | } |
| 3413 | |
| 3414 | enter_guest_mode(vcpu); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3415 | |
Maxim Levitsky | 0f85722 | 2021-06-07 12:02:00 +0300 | [diff] [blame] | 3416 | if (prepare_vmcs02(vcpu, vmcs12, from_vmentry, &entry_failure_code)) { |
Sean Christopherson | 8e53324 | 2020-11-06 17:03:12 +0800 | [diff] [blame] | 3417 | exit_reason.basic = EXIT_REASON_INVALID_STATE; |
Sean Christopherson | 68cda40 | 2020-05-11 15:05:29 -0700 | [diff] [blame] | 3418 | vmcs12->exit_qualification = entry_failure_code; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3419 | goto vmentry_fail_vmexit_guest_mode; |
Sean Christopherson | 68cda40 | 2020-05-11 15:05:29 -0700 | [diff] [blame] | 3420 | } |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3421 | |
| 3422 | if (from_vmentry) { |
Sean Christopherson | 68cda40 | 2020-05-11 15:05:29 -0700 | [diff] [blame] | 3423 | failed_index = nested_vmx_load_msr(vcpu, |
| 3424 | vmcs12->vm_entry_msr_load_addr, |
| 3425 | vmcs12->vm_entry_msr_load_count); |
| 3426 | if (failed_index) { |
Sean Christopherson | 8e53324 | 2020-11-06 17:03:12 +0800 | [diff] [blame] | 3427 | exit_reason.basic = EXIT_REASON_MSR_LOAD_FAIL; |
Sean Christopherson | 68cda40 | 2020-05-11 15:05:29 -0700 | [diff] [blame] | 3428 | vmcs12->exit_qualification = failed_index; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3429 | goto vmentry_fail_vmexit_guest_mode; |
Sean Christopherson | 68cda40 | 2020-05-11 15:05:29 -0700 | [diff] [blame] | 3430 | } |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3431 | } else { |
| 3432 | /* |
| 3433 | * The MMU is not initialized to point at the right entities yet and |
| 3434 | * "get pages" would need to read data from the guest (i.e. we will |
| 3435 | * need to perform gpa to hpa translation). Request a call |
| 3436 | * to nested_get_vmcs12_pages before the next VM-entry. The MSRs |
| 3437 | * have already been set at vmentry time and should not be reset. |
| 3438 | */ |
Paolo Bonzini | 729c15c | 2020-09-22 06:53:57 -0400 | [diff] [blame] | 3439 | kvm_make_request(KVM_REQ_GET_NESTED_STATE_PAGES, vcpu); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3440 | } |
| 3441 | |
| 3442 | /* |
| 3443 | * If L1 had a pending IRQ/NMI until it executed |
| 3444 | * VMLAUNCH/VMRESUME which wasn't delivered because it was |
| 3445 | * disallowed (e.g. interrupts disabled), L0 needs to |
| 3446 | * evaluate if this pending event should cause an exit from L2 |
| 3447 | * to L1 or delivered directly to L2 (e.g. In case L1 don't |
| 3448 | * intercept EXTERNAL_INTERRUPT). |
| 3449 | * |
| 3450 | * Usually this would be handled by the processor noticing an |
| 3451 | * IRQ/NMI window request, or checking RVI during evaluation of |
| 3452 | * pending virtual interrupts. However, this setting was done |
| 3453 | * on VMCS01 and now VMCS02 is active instead. Thus, we force L0 |
| 3454 | * to perform pending event evaluation by requesting a KVM_REQ_EVENT. |
| 3455 | */ |
| 3456 | if (unlikely(evaluate_pending_interrupts)) |
| 3457 | kvm_make_request(KVM_REQ_EVENT, vcpu); |
| 3458 | |
| 3459 | /* |
Paolo Bonzini | 359a6c3 | 2019-01-29 19:14:46 +0100 | [diff] [blame] | 3460 | * Do not start the preemption timer hrtimer until after we know |
| 3461 | * we are successful, so that only nested_vmx_vmexit needs to cancel |
| 3462 | * the timer. |
| 3463 | */ |
| 3464 | vmx->nested.preemption_timer_expired = false; |
Peter Shier | 850448f | 2020-05-26 14:51:06 -0700 | [diff] [blame] | 3465 | if (nested_cpu_has_preemption_timer(vmcs12)) { |
| 3466 | u64 timer_value = vmx_calc_preemption_timer_value(vcpu); |
| 3467 | vmx_start_preemption_timer(vcpu, timer_value); |
| 3468 | } |
Paolo Bonzini | 359a6c3 | 2019-01-29 19:14:46 +0100 | [diff] [blame] | 3469 | |
| 3470 | /* |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3471 | * Note no nested_vmx_succeed or nested_vmx_fail here. At this point |
| 3472 | * we are no longer running L1, and VMLAUNCH/VMRESUME has not yet |
| 3473 | * returned as far as L1 is concerned. It will only return (and set |
| 3474 | * the success flag) when L2 exits (see nested_vmx_vmexit()). |
| 3475 | */ |
Jim Mattson | 671ddc7 | 2019-10-15 10:44:05 -0700 | [diff] [blame] | 3476 | return NVMX_VMENTRY_SUCCESS; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3477 | |
| 3478 | /* |
| 3479 | * A failed consistency check that leads to a VMExit during L1's |
| 3480 | * VMEnter to L2 is a variation of a normal VMexit, as explained in |
| 3481 | * 26.7 "VM-entry failures during or after loading guest state". |
| 3482 | */ |
| 3483 | vmentry_fail_vmexit_guest_mode: |
Xiaoyao Li | 5e3d394 | 2019-12-06 16:45:26 +0800 | [diff] [blame] | 3484 | if (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETTING) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3485 | vcpu->arch.tsc_offset -= vmcs12->tsc_offset; |
| 3486 | leave_guest_mode(vcpu); |
| 3487 | |
| 3488 | vmentry_fail_vmexit: |
| 3489 | vmx_switch_vmcs(vcpu, &vmx->vmcs01); |
| 3490 | |
| 3491 | if (!from_vmentry) |
Jim Mattson | 671ddc7 | 2019-10-15 10:44:05 -0700 | [diff] [blame] | 3492 | return NVMX_VMENTRY_VMEXIT; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3493 | |
| 3494 | load_vmcs12_host_state(vcpu, vmcs12); |
Sean Christopherson | 8e53324 | 2020-11-06 17:03:12 +0800 | [diff] [blame] | 3495 | vmcs12->vm_exit_reason = exit_reason.full; |
Vitaly Kuznetsov | 1e9dfbd | 2021-05-26 15:20:16 +0200 | [diff] [blame] | 3496 | if (enable_shadow_vmcs || evmptr_is_valid(vmx->nested.hv_evmcs_vmptr)) |
Sean Christopherson | 3731905ef | 2019-05-07 08:36:27 -0700 | [diff] [blame] | 3497 | vmx->nested.need_vmcs12_to_shadow_sync = true; |
Jim Mattson | 671ddc7 | 2019-10-15 10:44:05 -0700 | [diff] [blame] | 3498 | return NVMX_VMENTRY_VMEXIT; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3499 | } |
| 3500 | |
| 3501 | /* |
| 3502 | * nested_vmx_run() handles a nested entry, i.e., a VMLAUNCH or VMRESUME on L1 |
| 3503 | * for running an L2 nested guest. |
| 3504 | */ |
| 3505 | static int nested_vmx_run(struct kvm_vcpu *vcpu, bool launch) |
| 3506 | { |
| 3507 | struct vmcs12 *vmcs12; |
Jim Mattson | 671ddc7 | 2019-10-15 10:44:05 -0700 | [diff] [blame] | 3508 | enum nvmx_vmentry_status status; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3509 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 3510 | u32 interrupt_shadow = vmx_get_interrupt_shadow(vcpu); |
Vitaly Kuznetsov | b6a0653 | 2020-03-09 16:52:13 +0100 | [diff] [blame] | 3511 | enum nested_evmptrld_status evmptrld_status; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3512 | |
| 3513 | if (!nested_vmx_check_permission(vcpu)) |
| 3514 | return 1; |
| 3515 | |
Vitaly Kuznetsov | b6a0653 | 2020-03-09 16:52:13 +0100 | [diff] [blame] | 3516 | evmptrld_status = nested_vmx_handle_enlightened_vmptrld(vcpu, launch); |
| 3517 | if (evmptrld_status == EVMPTRLD_ERROR) { |
| 3518 | kvm_queue_exception(vcpu, UD_VECTOR); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3519 | return 1; |
Sean Christopherson | fc595f3 | 2020-08-12 11:06:15 -0700 | [diff] [blame] | 3520 | } else if (CC(evmptrld_status == EVMPTRLD_VMFAIL)) { |
Vitaly Kuznetsov | b6a0653 | 2020-03-09 16:52:13 +0100 | [diff] [blame] | 3521 | return nested_vmx_failInvalid(vcpu); |
| 3522 | } |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3523 | |
Vitaly Kuznetsov | 1e9dfbd | 2021-05-26 15:20:16 +0200 | [diff] [blame] | 3524 | if (CC(!evmptr_is_valid(vmx->nested.hv_evmcs_vmptr) && |
| 3525 | vmx->nested.current_vmptr == -1ull)) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3526 | return nested_vmx_failInvalid(vcpu); |
| 3527 | |
| 3528 | vmcs12 = get_vmcs12(vcpu); |
| 3529 | |
| 3530 | /* |
| 3531 | * Can't VMLAUNCH or VMRESUME a shadow VMCS. Despite the fact |
| 3532 | * that there *is* a valid VMCS pointer, RFLAGS.CF is set |
| 3533 | * rather than RFLAGS.ZF, and no error number is stored to the |
| 3534 | * VM-instruction error field. |
| 3535 | */ |
Sean Christopherson | fc595f3 | 2020-08-12 11:06:15 -0700 | [diff] [blame] | 3536 | if (CC(vmcs12->hdr.shadow_vmcs)) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3537 | return nested_vmx_failInvalid(vcpu); |
| 3538 | |
Vitaly Kuznetsov | 1e9dfbd | 2021-05-26 15:20:16 +0200 | [diff] [blame] | 3539 | if (evmptr_is_valid(vmx->nested.hv_evmcs_vmptr)) { |
Vitaly Kuznetsov | d6bf71a | 2021-05-26 15:20:22 +0200 | [diff] [blame] | 3540 | copy_enlightened_to_vmcs12(vmx, vmx->nested.hv_evmcs->hv_clean_fields); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3541 | /* Enlightened VMCS doesn't have launch state */ |
| 3542 | vmcs12->launch_state = !launch; |
| 3543 | } else if (enable_shadow_vmcs) { |
| 3544 | copy_shadow_to_vmcs12(vmx); |
| 3545 | } |
| 3546 | |
| 3547 | /* |
| 3548 | * The nested entry process starts with enforcing various prerequisites |
| 3549 | * on vmcs12 as required by the Intel SDM, and act appropriately when |
| 3550 | * they fail: As the SDM explains, some conditions should cause the |
| 3551 | * instruction to fail, while others will cause the instruction to seem |
| 3552 | * to succeed, but return an EXIT_REASON_INVALID_STATE. |
| 3553 | * To speed up the normal (success) code path, we should avoid checking |
| 3554 | * for misconfigurations which will anyway be caught by the processor |
| 3555 | * when using the merged vmcs02. |
| 3556 | */ |
Sean Christopherson | fc595f3 | 2020-08-12 11:06:15 -0700 | [diff] [blame] | 3557 | if (CC(interrupt_shadow & KVM_X86_SHADOW_INT_MOV_SS)) |
Sean Christopherson | b2656e4 | 2020-06-08 18:56:07 -0700 | [diff] [blame] | 3558 | return nested_vmx_fail(vcpu, VMXERR_ENTRY_EVENTS_BLOCKED_BY_MOV_SS); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3559 | |
Sean Christopherson | fc595f3 | 2020-08-12 11:06:15 -0700 | [diff] [blame] | 3560 | if (CC(vmcs12->launch_state == launch)) |
Sean Christopherson | b2656e4 | 2020-06-08 18:56:07 -0700 | [diff] [blame] | 3561 | return nested_vmx_fail(vcpu, |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3562 | launch ? VMXERR_VMLAUNCH_NONCLEAR_VMCS |
| 3563 | : VMXERR_VMRESUME_NONLAUNCHED_VMCS); |
| 3564 | |
Paolo Bonzini | 98d9e85 | 2019-04-12 10:19:57 +0200 | [diff] [blame] | 3565 | if (nested_vmx_check_controls(vcpu, vmcs12)) |
Sean Christopherson | b2656e4 | 2020-06-08 18:56:07 -0700 | [diff] [blame] | 3566 | return nested_vmx_fail(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD); |
Sean Christopherson | 5478ba3 | 2019-04-11 12:18:06 -0700 | [diff] [blame] | 3567 | |
Paolo Bonzini | 98d9e85 | 2019-04-12 10:19:57 +0200 | [diff] [blame] | 3568 | if (nested_vmx_check_host_state(vcpu, vmcs12)) |
Sean Christopherson | b2656e4 | 2020-06-08 18:56:07 -0700 | [diff] [blame] | 3569 | return nested_vmx_fail(vcpu, VMXERR_ENTRY_INVALID_HOST_STATE_FIELD); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3570 | |
| 3571 | /* |
| 3572 | * We're finally done with prerequisite checking, and can start with |
| 3573 | * the nested entry. |
| 3574 | */ |
| 3575 | vmx->nested.nested_run_pending = 1; |
Peter Shier | 850448f | 2020-05-26 14:51:06 -0700 | [diff] [blame] | 3576 | vmx->nested.has_preemption_timer_deadline = false; |
Jim Mattson | 671ddc7 | 2019-10-15 10:44:05 -0700 | [diff] [blame] | 3577 | status = nested_vmx_enter_non_root_mode(vcpu, true); |
| 3578 | if (unlikely(status != NVMX_VMENTRY_SUCCESS)) |
| 3579 | goto vmentry_failed; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3580 | |
Sean Christopherson | 25bb2cf | 2020-08-12 10:51:29 -0700 | [diff] [blame] | 3581 | /* Emulate processing of posted interrupts on VM-Enter. */ |
| 3582 | if (nested_cpu_has_posted_intr(vmcs12) && |
| 3583 | kvm_apic_has_interrupt(vcpu) == vmx->nested.posted_intr_nv) { |
| 3584 | vmx->nested.pi_pending = true; |
| 3585 | kvm_make_request(KVM_REQ_EVENT, vcpu); |
| 3586 | kvm_apic_clear_irr(vcpu, vmx->nested.posted_intr_nv); |
| 3587 | } |
| 3588 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3589 | /* Hide L1D cache contents from the nested guest. */ |
| 3590 | vmx->vcpu.arch.l1tf_flush_l1d = true; |
| 3591 | |
| 3592 | /* |
| 3593 | * Must happen outside of nested_vmx_enter_non_root_mode() as it will |
| 3594 | * also be used as part of restoring nVMX state for |
| 3595 | * snapshot restore (migration). |
| 3596 | * |
| 3597 | * In this flow, it is assumed that vmcs12 cache was |
Ingo Molnar | 163b099 | 2021-03-21 22:28:53 +0100 | [diff] [blame] | 3598 | * transferred as part of captured nVMX state and should |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3599 | * therefore not be read from guest memory (which may not |
| 3600 | * exist on destination host yet). |
| 3601 | */ |
| 3602 | nested_cache_shadow_vmcs12(vcpu, vmcs12); |
| 3603 | |
Yadong Qi | bf0cd88 | 2020-11-06 14:51:22 +0800 | [diff] [blame] | 3604 | switch (vmcs12->guest_activity_state) { |
| 3605 | case GUEST_ACTIVITY_HLT: |
| 3606 | /* |
| 3607 | * If we're entering a halted L2 vcpu and the L2 vcpu won't be |
| 3608 | * awakened by event injection or by an NMI-window VM-exit or |
| 3609 | * by an interrupt-window VM-exit, halt the vcpu. |
| 3610 | */ |
| 3611 | if (!(vmcs12->vm_entry_intr_info_field & INTR_INFO_VALID_MASK) && |
| 3612 | !nested_cpu_has(vmcs12, CPU_BASED_NMI_WINDOW_EXITING) && |
| 3613 | !(nested_cpu_has(vmcs12, CPU_BASED_INTR_WINDOW_EXITING) && |
| 3614 | (vmcs12->guest_rflags & X86_EFLAGS_IF))) { |
| 3615 | vmx->nested.nested_run_pending = 0; |
| 3616 | return kvm_vcpu_halt(vcpu); |
| 3617 | } |
| 3618 | break; |
| 3619 | case GUEST_ACTIVITY_WAIT_SIPI: |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3620 | vmx->nested.nested_run_pending = 0; |
Yadong Qi | bf0cd88 | 2020-11-06 14:51:22 +0800 | [diff] [blame] | 3621 | vcpu->arch.mp_state = KVM_MP_STATE_INIT_RECEIVED; |
| 3622 | break; |
| 3623 | default: |
| 3624 | break; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3625 | } |
Yadong Qi | bf0cd88 | 2020-11-06 14:51:22 +0800 | [diff] [blame] | 3626 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3627 | return 1; |
Jim Mattson | 671ddc7 | 2019-10-15 10:44:05 -0700 | [diff] [blame] | 3628 | |
| 3629 | vmentry_failed: |
| 3630 | vmx->nested.nested_run_pending = 0; |
| 3631 | if (status == NVMX_VMENTRY_KVM_INTERNAL_ERROR) |
| 3632 | return 0; |
| 3633 | if (status == NVMX_VMENTRY_VMEXIT) |
| 3634 | return 1; |
| 3635 | WARN_ON_ONCE(status != NVMX_VMENTRY_VMFAIL); |
Sean Christopherson | b2656e4 | 2020-06-08 18:56:07 -0700 | [diff] [blame] | 3636 | return nested_vmx_fail(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3637 | } |
| 3638 | |
| 3639 | /* |
| 3640 | * 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] | 3641 | * 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] | 3642 | * This function returns the new value we should put in vmcs12.guest_cr0. |
| 3643 | * It's not enough to just return the vmcs02 GUEST_CR0. Rather, |
| 3644 | * 1. Bits that neither L0 nor L1 trapped, were set directly by L2 and are now |
| 3645 | * available in vmcs02 GUEST_CR0. (Note: It's enough to check that L0 |
| 3646 | * didn't trap the bit, because if L1 did, so would L0). |
| 3647 | * 2. Bits that L1 asked to trap (and therefore L0 also did) could not have |
| 3648 | * been modified by L2, and L1 knows it. So just leave the old value of |
| 3649 | * the bit from vmcs12.guest_cr0. Note that the bit from vmcs02 GUEST_CR0 |
| 3650 | * isn't relevant, because if L0 traps this bit it can set it to anything. |
| 3651 | * 3. Bits that L1 didn't trap, but L0 did. L1 believes the guest could have |
| 3652 | * changed these bits, and therefore they need to be updated, but L0 |
| 3653 | * didn't necessarily allow them to be changed in GUEST_CR0 - and rather |
| 3654 | * put them in vmcs02 CR0_READ_SHADOW. So take these bits from there. |
| 3655 | */ |
| 3656 | static inline unsigned long |
| 3657 | vmcs12_guest_cr0(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12) |
| 3658 | { |
| 3659 | return |
| 3660 | /*1*/ (vmcs_readl(GUEST_CR0) & vcpu->arch.cr0_guest_owned_bits) | |
| 3661 | /*2*/ (vmcs12->guest_cr0 & vmcs12->cr0_guest_host_mask) | |
| 3662 | /*3*/ (vmcs_readl(CR0_READ_SHADOW) & ~(vmcs12->cr0_guest_host_mask | |
| 3663 | vcpu->arch.cr0_guest_owned_bits)); |
| 3664 | } |
| 3665 | |
| 3666 | static inline unsigned long |
| 3667 | vmcs12_guest_cr4(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12) |
| 3668 | { |
| 3669 | return |
| 3670 | /*1*/ (vmcs_readl(GUEST_CR4) & vcpu->arch.cr4_guest_owned_bits) | |
| 3671 | /*2*/ (vmcs12->guest_cr4 & vmcs12->cr4_guest_host_mask) | |
| 3672 | /*3*/ (vmcs_readl(CR4_READ_SHADOW) & ~(vmcs12->cr4_guest_host_mask | |
| 3673 | vcpu->arch.cr4_guest_owned_bits)); |
| 3674 | } |
| 3675 | |
| 3676 | static void vmcs12_save_pending_event(struct kvm_vcpu *vcpu, |
| 3677 | struct vmcs12 *vmcs12) |
| 3678 | { |
| 3679 | u32 idt_vectoring; |
| 3680 | unsigned int nr; |
| 3681 | |
| 3682 | if (vcpu->arch.exception.injected) { |
| 3683 | nr = vcpu->arch.exception.nr; |
| 3684 | idt_vectoring = nr | VECTORING_INFO_VALID_MASK; |
| 3685 | |
| 3686 | if (kvm_exception_is_soft(nr)) { |
| 3687 | vmcs12->vm_exit_instruction_len = |
| 3688 | vcpu->arch.event_exit_inst_len; |
| 3689 | idt_vectoring |= INTR_TYPE_SOFT_EXCEPTION; |
| 3690 | } else |
| 3691 | idt_vectoring |= INTR_TYPE_HARD_EXCEPTION; |
| 3692 | |
| 3693 | if (vcpu->arch.exception.has_error_code) { |
| 3694 | idt_vectoring |= VECTORING_INFO_DELIVER_CODE_MASK; |
| 3695 | vmcs12->idt_vectoring_error_code = |
| 3696 | vcpu->arch.exception.error_code; |
| 3697 | } |
| 3698 | |
| 3699 | vmcs12->idt_vectoring_info_field = idt_vectoring; |
| 3700 | } else if (vcpu->arch.nmi_injected) { |
| 3701 | vmcs12->idt_vectoring_info_field = |
| 3702 | INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK | NMI_VECTOR; |
| 3703 | } else if (vcpu->arch.interrupt.injected) { |
| 3704 | nr = vcpu->arch.interrupt.nr; |
| 3705 | idt_vectoring = nr | VECTORING_INFO_VALID_MASK; |
| 3706 | |
| 3707 | if (vcpu->arch.interrupt.soft) { |
| 3708 | idt_vectoring |= INTR_TYPE_SOFT_INTR; |
| 3709 | vmcs12->vm_entry_instruction_len = |
| 3710 | vcpu->arch.event_exit_inst_len; |
| 3711 | } else |
| 3712 | idt_vectoring |= INTR_TYPE_EXT_INTR; |
| 3713 | |
| 3714 | vmcs12->idt_vectoring_info_field = idt_vectoring; |
| 3715 | } |
| 3716 | } |
| 3717 | |
| 3718 | |
Paolo Bonzini | 96b100c | 2020-03-17 18:32:50 +0100 | [diff] [blame] | 3719 | void nested_mark_vmcs12_pages_dirty(struct kvm_vcpu *vcpu) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3720 | { |
| 3721 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
| 3722 | gfn_t gfn; |
| 3723 | |
| 3724 | /* |
| 3725 | * Don't need to mark the APIC access page dirty; it is never |
| 3726 | * written to by the CPU during APIC virtualization. |
| 3727 | */ |
| 3728 | |
| 3729 | if (nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW)) { |
| 3730 | gfn = vmcs12->virtual_apic_page_addr >> PAGE_SHIFT; |
| 3731 | kvm_vcpu_mark_page_dirty(vcpu, gfn); |
| 3732 | } |
| 3733 | |
| 3734 | if (nested_cpu_has_posted_intr(vmcs12)) { |
| 3735 | gfn = vmcs12->posted_intr_desc_addr >> PAGE_SHIFT; |
| 3736 | kvm_vcpu_mark_page_dirty(vcpu, gfn); |
| 3737 | } |
| 3738 | } |
| 3739 | |
Jim Mattson | 650293c | 2021-06-04 10:26:02 -0700 | [diff] [blame] | 3740 | static int vmx_complete_nested_posted_interrupt(struct kvm_vcpu *vcpu) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3741 | { |
| 3742 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 3743 | int max_irr; |
| 3744 | void *vapic_page; |
| 3745 | u16 status; |
| 3746 | |
Jim Mattson | 966eefb | 2021-06-04 10:26:06 -0700 | [diff] [blame] | 3747 | if (!vmx->nested.pi_pending) |
Jim Mattson | 650293c | 2021-06-04 10:26:02 -0700 | [diff] [blame] | 3748 | return 0; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3749 | |
Jim Mattson | 966eefb | 2021-06-04 10:26:06 -0700 | [diff] [blame] | 3750 | if (!vmx->nested.pi_desc) |
| 3751 | goto mmio_needed; |
| 3752 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3753 | vmx->nested.pi_pending = false; |
Jim Mattson | 966eefb | 2021-06-04 10:26:06 -0700 | [diff] [blame] | 3754 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3755 | if (!pi_test_and_clear_on(vmx->nested.pi_desc)) |
Jim Mattson | 650293c | 2021-06-04 10:26:02 -0700 | [diff] [blame] | 3756 | return 0; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3757 | |
| 3758 | max_irr = find_last_bit((unsigned long *)vmx->nested.pi_desc->pir, 256); |
| 3759 | if (max_irr != 256) { |
KarimAllah Ahmed | 96c66e8 | 2019-01-31 21:24:37 +0100 | [diff] [blame] | 3760 | vapic_page = vmx->nested.virtual_apic_map.hva; |
| 3761 | if (!vapic_page) |
Jim Mattson | 0fe998b | 2021-06-04 10:26:05 -0700 | [diff] [blame] | 3762 | goto mmio_needed; |
KarimAllah Ahmed | 96c66e8 | 2019-01-31 21:24:37 +0100 | [diff] [blame] | 3763 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3764 | __kvm_apic_update_irr(vmx->nested.pi_desc->pir, |
| 3765 | vapic_page, &max_irr); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3766 | status = vmcs_read16(GUEST_INTR_STATUS); |
| 3767 | if ((u8)max_irr > ((u8)status & 0xff)) { |
| 3768 | status &= ~0xff; |
| 3769 | status |= (u8)max_irr; |
| 3770 | vmcs_write16(GUEST_INTR_STATUS, status); |
| 3771 | } |
| 3772 | } |
| 3773 | |
| 3774 | nested_mark_vmcs12_pages_dirty(vcpu); |
Jim Mattson | 650293c | 2021-06-04 10:26:02 -0700 | [diff] [blame] | 3775 | return 0; |
Jim Mattson | 0fe998b | 2021-06-04 10:26:05 -0700 | [diff] [blame] | 3776 | |
| 3777 | mmio_needed: |
| 3778 | kvm_handle_memory_failure(vcpu, X86EMUL_IO_NEEDED, NULL); |
| 3779 | return -ENXIO; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3780 | } |
| 3781 | |
| 3782 | static void nested_vmx_inject_exception_vmexit(struct kvm_vcpu *vcpu, |
| 3783 | unsigned long exit_qual) |
| 3784 | { |
| 3785 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
| 3786 | unsigned int nr = vcpu->arch.exception.nr; |
| 3787 | u32 intr_info = nr | INTR_INFO_VALID_MASK; |
| 3788 | |
| 3789 | if (vcpu->arch.exception.has_error_code) { |
| 3790 | vmcs12->vm_exit_intr_error_code = vcpu->arch.exception.error_code; |
| 3791 | intr_info |= INTR_INFO_DELIVER_CODE_MASK; |
| 3792 | } |
| 3793 | |
| 3794 | if (kvm_exception_is_soft(nr)) |
| 3795 | intr_info |= INTR_TYPE_SOFT_EXCEPTION; |
| 3796 | else |
| 3797 | intr_info |= INTR_TYPE_HARD_EXCEPTION; |
| 3798 | |
| 3799 | if (!(vmcs12->idt_vectoring_info_field & VECTORING_INFO_VALID_MASK) && |
| 3800 | vmx_get_nmi_mask(vcpu)) |
| 3801 | intr_info |= INTR_INFO_UNBLOCK_NMI; |
| 3802 | |
| 3803 | nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI, intr_info, exit_qual); |
| 3804 | } |
| 3805 | |
Oliver Upton | 684c042 | 2020-02-07 02:36:05 -0800 | [diff] [blame] | 3806 | /* |
| 3807 | * Returns true if a debug trap is pending delivery. |
| 3808 | * |
| 3809 | * In KVM, debug traps bear an exception payload. As such, the class of a #DB |
| 3810 | * exception may be inferred from the presence of an exception payload. |
| 3811 | */ |
| 3812 | static inline bool vmx_pending_dbg_trap(struct kvm_vcpu *vcpu) |
| 3813 | { |
| 3814 | return vcpu->arch.exception.pending && |
| 3815 | vcpu->arch.exception.nr == DB_VECTOR && |
| 3816 | vcpu->arch.exception.payload; |
| 3817 | } |
| 3818 | |
| 3819 | /* |
| 3820 | * Certain VM-exits set the 'pending debug exceptions' field to indicate a |
| 3821 | * recognized #DB (data or single-step) that has yet to be delivered. Since KVM |
| 3822 | * represents these debug traps with a payload that is said to be compatible |
| 3823 | * with the 'pending debug exceptions' field, write the payload to the VMCS |
| 3824 | * field if a VM-exit is delivered before the debug trap. |
| 3825 | */ |
| 3826 | static void nested_vmx_update_pending_dbg(struct kvm_vcpu *vcpu) |
| 3827 | { |
| 3828 | if (vmx_pending_dbg_trap(vcpu)) |
| 3829 | vmcs_writel(GUEST_PENDING_DBG_EXCEPTIONS, |
| 3830 | vcpu->arch.exception.payload); |
| 3831 | } |
| 3832 | |
Sean Christopherson | d2060bd | 2020-04-22 19:25:39 -0700 | [diff] [blame] | 3833 | static bool nested_vmx_preemption_timer_pending(struct kvm_vcpu *vcpu) |
| 3834 | { |
| 3835 | return nested_cpu_has_preemption_timer(get_vmcs12(vcpu)) && |
| 3836 | to_vmx(vcpu)->nested.preemption_timer_expired; |
| 3837 | } |
| 3838 | |
Sean Christopherson | a1c77ab | 2020-03-02 22:27:35 -0800 | [diff] [blame] | 3839 | static int vmx_check_nested_events(struct kvm_vcpu *vcpu) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3840 | { |
| 3841 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 3842 | unsigned long exit_qual; |
| 3843 | bool block_nested_events = |
| 3844 | vmx->nested.nested_run_pending || kvm_event_needs_reinjection(vcpu); |
Oliver Upton | 5ef8acb | 2020-02-07 02:36:07 -0800 | [diff] [blame] | 3845 | bool mtf_pending = vmx->nested.mtf_pending; |
Liran Alon | 4b9852f | 2019-08-26 13:24:49 +0300 | [diff] [blame] | 3846 | struct kvm_lapic *apic = vcpu->arch.apic; |
| 3847 | |
Oliver Upton | 5ef8acb | 2020-02-07 02:36:07 -0800 | [diff] [blame] | 3848 | /* |
| 3849 | * Clear the MTF state. If a higher priority VM-exit is delivered first, |
| 3850 | * this state is discarded. |
| 3851 | */ |
Oliver Upton | 5c8beb4 | 2020-04-06 20:12:37 +0000 | [diff] [blame] | 3852 | if (!block_nested_events) |
| 3853 | vmx->nested.mtf_pending = false; |
Oliver Upton | 5ef8acb | 2020-02-07 02:36:07 -0800 | [diff] [blame] | 3854 | |
Liran Alon | 4b9852f | 2019-08-26 13:24:49 +0300 | [diff] [blame] | 3855 | if (lapic_in_kernel(vcpu) && |
| 3856 | test_bit(KVM_APIC_INIT, &apic->pending_events)) { |
| 3857 | if (block_nested_events) |
| 3858 | return -EBUSY; |
Oliver Upton | 684c042 | 2020-02-07 02:36:05 -0800 | [diff] [blame] | 3859 | nested_vmx_update_pending_dbg(vcpu); |
Liran Alon | e64a850 | 2019-11-11 14:16:05 +0200 | [diff] [blame] | 3860 | clear_bit(KVM_APIC_INIT, &apic->pending_events); |
Yadong Qi | bf0cd88 | 2020-11-06 14:51:22 +0800 | [diff] [blame] | 3861 | if (vcpu->arch.mp_state != KVM_MP_STATE_INIT_RECEIVED) |
| 3862 | nested_vmx_vmexit(vcpu, EXIT_REASON_INIT_SIGNAL, 0, 0); |
| 3863 | return 0; |
| 3864 | } |
| 3865 | |
| 3866 | if (lapic_in_kernel(vcpu) && |
| 3867 | test_bit(KVM_APIC_SIPI, &apic->pending_events)) { |
| 3868 | if (block_nested_events) |
| 3869 | return -EBUSY; |
| 3870 | |
| 3871 | clear_bit(KVM_APIC_SIPI, &apic->pending_events); |
| 3872 | if (vcpu->arch.mp_state == KVM_MP_STATE_INIT_RECEIVED) |
| 3873 | nested_vmx_vmexit(vcpu, EXIT_REASON_SIPI_SIGNAL, 0, |
| 3874 | apic->sipi_vector & 0xFFUL); |
Liran Alon | 4b9852f | 2019-08-26 13:24:49 +0300 | [diff] [blame] | 3875 | return 0; |
| 3876 | } |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3877 | |
Oliver Upton | 5ef8acb | 2020-02-07 02:36:07 -0800 | [diff] [blame] | 3878 | /* |
| 3879 | * Process any exceptions that are not debug traps before MTF. |
Maxim Levitsky | 4020da3 | 2021-04-01 17:38:14 +0300 | [diff] [blame] | 3880 | * |
| 3881 | * Note that only a pending nested run can block a pending exception. |
| 3882 | * Otherwise an injected NMI/interrupt should either be |
| 3883 | * lost or delivered to the nested hypervisor in the IDT_VECTORING_INFO, |
| 3884 | * while delivering the pending exception. |
Oliver Upton | 5ef8acb | 2020-02-07 02:36:07 -0800 | [diff] [blame] | 3885 | */ |
Maxim Levitsky | 4020da3 | 2021-04-01 17:38:14 +0300 | [diff] [blame] | 3886 | |
Sean Christopherson | 6ce347a | 2020-04-22 19:25:38 -0700 | [diff] [blame] | 3887 | if (vcpu->arch.exception.pending && !vmx_pending_dbg_trap(vcpu)) { |
Maxim Levitsky | 4020da3 | 2021-04-01 17:38:14 +0300 | [diff] [blame] | 3888 | if (vmx->nested.nested_run_pending) |
Oliver Upton | 5ef8acb | 2020-02-07 02:36:07 -0800 | [diff] [blame] | 3889 | return -EBUSY; |
Sean Christopherson | 6ce347a | 2020-04-22 19:25:38 -0700 | [diff] [blame] | 3890 | if (!nested_vmx_check_exception(vcpu, &exit_qual)) |
| 3891 | goto no_vmexit; |
Oliver Upton | 5ef8acb | 2020-02-07 02:36:07 -0800 | [diff] [blame] | 3892 | nested_vmx_inject_exception_vmexit(vcpu, exit_qual); |
| 3893 | return 0; |
| 3894 | } |
| 3895 | |
| 3896 | if (mtf_pending) { |
| 3897 | if (block_nested_events) |
| 3898 | return -EBUSY; |
| 3899 | nested_vmx_update_pending_dbg(vcpu); |
| 3900 | nested_vmx_vmexit(vcpu, EXIT_REASON_MONITOR_TRAP_FLAG, 0, 0); |
| 3901 | return 0; |
| 3902 | } |
| 3903 | |
Sean Christopherson | 6ce347a | 2020-04-22 19:25:38 -0700 | [diff] [blame] | 3904 | if (vcpu->arch.exception.pending) { |
Maxim Levitsky | 4020da3 | 2021-04-01 17:38:14 +0300 | [diff] [blame] | 3905 | if (vmx->nested.nested_run_pending) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3906 | return -EBUSY; |
Sean Christopherson | 6ce347a | 2020-04-22 19:25:38 -0700 | [diff] [blame] | 3907 | if (!nested_vmx_check_exception(vcpu, &exit_qual)) |
| 3908 | goto no_vmexit; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3909 | nested_vmx_inject_exception_vmexit(vcpu, exit_qual); |
| 3910 | return 0; |
| 3911 | } |
| 3912 | |
Sean Christopherson | d2060bd | 2020-04-22 19:25:39 -0700 | [diff] [blame] | 3913 | if (nested_vmx_preemption_timer_pending(vcpu)) { |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3914 | if (block_nested_events) |
| 3915 | return -EBUSY; |
| 3916 | nested_vmx_vmexit(vcpu, EXIT_REASON_PREEMPTION_TIMER, 0, 0); |
| 3917 | return 0; |
| 3918 | } |
| 3919 | |
Sean Christopherson | 1cd2f0b | 2020-04-22 19:25:46 -0700 | [diff] [blame] | 3920 | if (vcpu->arch.smi_pending && !is_smm(vcpu)) { |
| 3921 | if (block_nested_events) |
| 3922 | return -EBUSY; |
| 3923 | goto no_vmexit; |
| 3924 | } |
| 3925 | |
Sean Christopherson | 15ff0b4 | 2020-04-22 19:25:45 -0700 | [diff] [blame] | 3926 | if (vcpu->arch.nmi_pending && !vmx_nmi_blocked(vcpu)) { |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3927 | if (block_nested_events) |
| 3928 | return -EBUSY; |
Sean Christopherson | 15ff0b4 | 2020-04-22 19:25:45 -0700 | [diff] [blame] | 3929 | if (!nested_exit_on_nmi(vcpu)) |
| 3930 | goto no_vmexit; |
| 3931 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3932 | nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI, |
| 3933 | NMI_VECTOR | INTR_TYPE_NMI_INTR | |
| 3934 | INTR_INFO_VALID_MASK, 0); |
| 3935 | /* |
| 3936 | * The NMI-triggered VM exit counts as injection: |
| 3937 | * clear this one and block further NMIs. |
| 3938 | */ |
| 3939 | vcpu->arch.nmi_pending = 0; |
| 3940 | vmx_set_nmi_mask(vcpu, true); |
| 3941 | return 0; |
| 3942 | } |
| 3943 | |
Sean Christopherson | 15ff0b4 | 2020-04-22 19:25:45 -0700 | [diff] [blame] | 3944 | if (kvm_cpu_has_interrupt(vcpu) && !vmx_interrupt_blocked(vcpu)) { |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3945 | if (block_nested_events) |
| 3946 | return -EBUSY; |
Sean Christopherson | 15ff0b4 | 2020-04-22 19:25:45 -0700 | [diff] [blame] | 3947 | if (!nested_exit_on_intr(vcpu)) |
| 3948 | goto no_vmexit; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3949 | nested_vmx_vmexit(vcpu, EXIT_REASON_EXTERNAL_INTERRUPT, 0, 0); |
| 3950 | return 0; |
| 3951 | } |
| 3952 | |
Sean Christopherson | 6ce347a | 2020-04-22 19:25:38 -0700 | [diff] [blame] | 3953 | no_vmexit: |
Jim Mattson | 650293c | 2021-06-04 10:26:02 -0700 | [diff] [blame] | 3954 | return vmx_complete_nested_posted_interrupt(vcpu); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3955 | } |
| 3956 | |
| 3957 | static u32 vmx_get_preemption_timer_value(struct kvm_vcpu *vcpu) |
| 3958 | { |
| 3959 | ktime_t remaining = |
| 3960 | hrtimer_get_remaining(&to_vmx(vcpu)->nested.preemption_timer); |
| 3961 | u64 value; |
| 3962 | |
| 3963 | if (ktime_to_ns(remaining) <= 0) |
| 3964 | return 0; |
| 3965 | |
| 3966 | value = ktime_to_ns(remaining) * vcpu->arch.virtual_tsc_khz; |
| 3967 | do_div(value, 1000000); |
| 3968 | return value >> VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE; |
| 3969 | } |
| 3970 | |
Sean Christopherson | 7952d76 | 2019-05-07 08:36:29 -0700 | [diff] [blame] | 3971 | static bool is_vmcs12_ext_field(unsigned long field) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3972 | { |
Sean Christopherson | 7952d76 | 2019-05-07 08:36:29 -0700 | [diff] [blame] | 3973 | switch (field) { |
| 3974 | case GUEST_ES_SELECTOR: |
| 3975 | case GUEST_CS_SELECTOR: |
| 3976 | case GUEST_SS_SELECTOR: |
| 3977 | case GUEST_DS_SELECTOR: |
| 3978 | case GUEST_FS_SELECTOR: |
| 3979 | case GUEST_GS_SELECTOR: |
| 3980 | case GUEST_LDTR_SELECTOR: |
| 3981 | case GUEST_TR_SELECTOR: |
| 3982 | case GUEST_ES_LIMIT: |
| 3983 | case GUEST_CS_LIMIT: |
| 3984 | case GUEST_SS_LIMIT: |
| 3985 | case GUEST_DS_LIMIT: |
| 3986 | case GUEST_FS_LIMIT: |
| 3987 | case GUEST_GS_LIMIT: |
| 3988 | case GUEST_LDTR_LIMIT: |
| 3989 | case GUEST_TR_LIMIT: |
| 3990 | case GUEST_GDTR_LIMIT: |
| 3991 | case GUEST_IDTR_LIMIT: |
| 3992 | case GUEST_ES_AR_BYTES: |
| 3993 | case GUEST_DS_AR_BYTES: |
| 3994 | case GUEST_FS_AR_BYTES: |
| 3995 | case GUEST_GS_AR_BYTES: |
| 3996 | case GUEST_LDTR_AR_BYTES: |
| 3997 | case GUEST_TR_AR_BYTES: |
| 3998 | case GUEST_ES_BASE: |
| 3999 | case GUEST_CS_BASE: |
| 4000 | case GUEST_SS_BASE: |
| 4001 | case GUEST_DS_BASE: |
| 4002 | case GUEST_FS_BASE: |
| 4003 | case GUEST_GS_BASE: |
| 4004 | case GUEST_LDTR_BASE: |
| 4005 | case GUEST_TR_BASE: |
| 4006 | case GUEST_GDTR_BASE: |
| 4007 | case GUEST_IDTR_BASE: |
| 4008 | case GUEST_PENDING_DBG_EXCEPTIONS: |
| 4009 | case GUEST_BNDCFGS: |
| 4010 | return true; |
| 4011 | default: |
| 4012 | break; |
| 4013 | } |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4014 | |
Sean Christopherson | 7952d76 | 2019-05-07 08:36:29 -0700 | [diff] [blame] | 4015 | return false; |
| 4016 | } |
| 4017 | |
| 4018 | static void sync_vmcs02_to_vmcs12_rare(struct kvm_vcpu *vcpu, |
| 4019 | struct vmcs12 *vmcs12) |
| 4020 | { |
| 4021 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4022 | |
| 4023 | vmcs12->guest_es_selector = vmcs_read16(GUEST_ES_SELECTOR); |
| 4024 | vmcs12->guest_cs_selector = vmcs_read16(GUEST_CS_SELECTOR); |
| 4025 | vmcs12->guest_ss_selector = vmcs_read16(GUEST_SS_SELECTOR); |
| 4026 | vmcs12->guest_ds_selector = vmcs_read16(GUEST_DS_SELECTOR); |
| 4027 | vmcs12->guest_fs_selector = vmcs_read16(GUEST_FS_SELECTOR); |
| 4028 | vmcs12->guest_gs_selector = vmcs_read16(GUEST_GS_SELECTOR); |
| 4029 | vmcs12->guest_ldtr_selector = vmcs_read16(GUEST_LDTR_SELECTOR); |
| 4030 | vmcs12->guest_tr_selector = vmcs_read16(GUEST_TR_SELECTOR); |
| 4031 | vmcs12->guest_es_limit = vmcs_read32(GUEST_ES_LIMIT); |
| 4032 | vmcs12->guest_cs_limit = vmcs_read32(GUEST_CS_LIMIT); |
| 4033 | vmcs12->guest_ss_limit = vmcs_read32(GUEST_SS_LIMIT); |
| 4034 | vmcs12->guest_ds_limit = vmcs_read32(GUEST_DS_LIMIT); |
| 4035 | vmcs12->guest_fs_limit = vmcs_read32(GUEST_FS_LIMIT); |
| 4036 | vmcs12->guest_gs_limit = vmcs_read32(GUEST_GS_LIMIT); |
| 4037 | vmcs12->guest_ldtr_limit = vmcs_read32(GUEST_LDTR_LIMIT); |
| 4038 | vmcs12->guest_tr_limit = vmcs_read32(GUEST_TR_LIMIT); |
| 4039 | vmcs12->guest_gdtr_limit = vmcs_read32(GUEST_GDTR_LIMIT); |
| 4040 | vmcs12->guest_idtr_limit = vmcs_read32(GUEST_IDTR_LIMIT); |
| 4041 | vmcs12->guest_es_ar_bytes = vmcs_read32(GUEST_ES_AR_BYTES); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4042 | vmcs12->guest_ds_ar_bytes = vmcs_read32(GUEST_DS_AR_BYTES); |
| 4043 | vmcs12->guest_fs_ar_bytes = vmcs_read32(GUEST_FS_AR_BYTES); |
| 4044 | vmcs12->guest_gs_ar_bytes = vmcs_read32(GUEST_GS_AR_BYTES); |
| 4045 | vmcs12->guest_ldtr_ar_bytes = vmcs_read32(GUEST_LDTR_AR_BYTES); |
| 4046 | vmcs12->guest_tr_ar_bytes = vmcs_read32(GUEST_TR_AR_BYTES); |
| 4047 | vmcs12->guest_es_base = vmcs_readl(GUEST_ES_BASE); |
| 4048 | vmcs12->guest_cs_base = vmcs_readl(GUEST_CS_BASE); |
| 4049 | vmcs12->guest_ss_base = vmcs_readl(GUEST_SS_BASE); |
| 4050 | vmcs12->guest_ds_base = vmcs_readl(GUEST_DS_BASE); |
| 4051 | vmcs12->guest_fs_base = vmcs_readl(GUEST_FS_BASE); |
| 4052 | vmcs12->guest_gs_base = vmcs_readl(GUEST_GS_BASE); |
| 4053 | vmcs12->guest_ldtr_base = vmcs_readl(GUEST_LDTR_BASE); |
| 4054 | vmcs12->guest_tr_base = vmcs_readl(GUEST_TR_BASE); |
| 4055 | vmcs12->guest_gdtr_base = vmcs_readl(GUEST_GDTR_BASE); |
| 4056 | vmcs12->guest_idtr_base = vmcs_readl(GUEST_IDTR_BASE); |
Sean Christopherson | 7952d76 | 2019-05-07 08:36:29 -0700 | [diff] [blame] | 4057 | vmcs12->guest_pending_dbg_exceptions = |
| 4058 | vmcs_readl(GUEST_PENDING_DBG_EXCEPTIONS); |
| 4059 | if (kvm_mpx_supported()) |
| 4060 | vmcs12->guest_bndcfgs = vmcs_read64(GUEST_BNDCFGS); |
| 4061 | |
| 4062 | vmx->nested.need_sync_vmcs02_to_vmcs12_rare = false; |
| 4063 | } |
| 4064 | |
| 4065 | static void copy_vmcs02_to_vmcs12_rare(struct kvm_vcpu *vcpu, |
| 4066 | struct vmcs12 *vmcs12) |
| 4067 | { |
| 4068 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 4069 | int cpu; |
| 4070 | |
| 4071 | if (!vmx->nested.need_sync_vmcs02_to_vmcs12_rare) |
| 4072 | return; |
| 4073 | |
| 4074 | |
| 4075 | WARN_ON_ONCE(vmx->loaded_vmcs != &vmx->vmcs01); |
| 4076 | |
| 4077 | cpu = get_cpu(); |
| 4078 | vmx->loaded_vmcs = &vmx->nested.vmcs02; |
Sean Christopherson | 1af1bb0 | 2020-05-06 16:58:50 -0700 | [diff] [blame] | 4079 | vmx_vcpu_load_vmcs(vcpu, cpu, &vmx->vmcs01); |
Sean Christopherson | 7952d76 | 2019-05-07 08:36:29 -0700 | [diff] [blame] | 4080 | |
| 4081 | sync_vmcs02_to_vmcs12_rare(vcpu, vmcs12); |
| 4082 | |
| 4083 | vmx->loaded_vmcs = &vmx->vmcs01; |
Sean Christopherson | 1af1bb0 | 2020-05-06 16:58:50 -0700 | [diff] [blame] | 4084 | vmx_vcpu_load_vmcs(vcpu, cpu, &vmx->nested.vmcs02); |
Sean Christopherson | 7952d76 | 2019-05-07 08:36:29 -0700 | [diff] [blame] | 4085 | put_cpu(); |
| 4086 | } |
| 4087 | |
| 4088 | /* |
| 4089 | * Update the guest state fields of vmcs12 to reflect changes that |
| 4090 | * occurred while L2 was running. (The "IA-32e mode guest" bit of the |
| 4091 | * VM-entry controls is also updated, since this is really a guest |
| 4092 | * state bit.) |
| 4093 | */ |
| 4094 | static void sync_vmcs02_to_vmcs12(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12) |
| 4095 | { |
| 4096 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 4097 | |
Vitaly Kuznetsov | 1e9dfbd | 2021-05-26 15:20:16 +0200 | [diff] [blame] | 4098 | if (evmptr_is_valid(vmx->nested.hv_evmcs_vmptr)) |
Sean Christopherson | 7952d76 | 2019-05-07 08:36:29 -0700 | [diff] [blame] | 4099 | sync_vmcs02_to_vmcs12_rare(vcpu, vmcs12); |
| 4100 | |
Vitaly Kuznetsov | 1e9dfbd | 2021-05-26 15:20:16 +0200 | [diff] [blame] | 4101 | vmx->nested.need_sync_vmcs02_to_vmcs12_rare = |
| 4102 | !evmptr_is_valid(vmx->nested.hv_evmcs_vmptr); |
Sean Christopherson | 7952d76 | 2019-05-07 08:36:29 -0700 | [diff] [blame] | 4103 | |
| 4104 | vmcs12->guest_cr0 = vmcs12_guest_cr0(vcpu, vmcs12); |
| 4105 | vmcs12->guest_cr4 = vmcs12_guest_cr4(vcpu, vmcs12); |
| 4106 | |
| 4107 | vmcs12->guest_rsp = kvm_rsp_read(vcpu); |
| 4108 | vmcs12->guest_rip = kvm_rip_read(vcpu); |
| 4109 | vmcs12->guest_rflags = vmcs_readl(GUEST_RFLAGS); |
| 4110 | |
| 4111 | vmcs12->guest_cs_ar_bytes = vmcs_read32(GUEST_CS_AR_BYTES); |
| 4112 | vmcs12->guest_ss_ar_bytes = vmcs_read32(GUEST_SS_AR_BYTES); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4113 | |
| 4114 | vmcs12->guest_interruptibility_info = |
| 4115 | vmcs_read32(GUEST_INTERRUPTIBILITY_INFO); |
Sean Christopherson | 7952d76 | 2019-05-07 08:36:29 -0700 | [diff] [blame] | 4116 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4117 | if (vcpu->arch.mp_state == KVM_MP_STATE_HALTED) |
| 4118 | vmcs12->guest_activity_state = GUEST_ACTIVITY_HLT; |
Yadong Qi | bf0cd88 | 2020-11-06 14:51:22 +0800 | [diff] [blame] | 4119 | else if (vcpu->arch.mp_state == KVM_MP_STATE_INIT_RECEIVED) |
| 4120 | vmcs12->guest_activity_state = GUEST_ACTIVITY_WAIT_SIPI; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4121 | else |
| 4122 | vmcs12->guest_activity_state = GUEST_ACTIVITY_ACTIVE; |
| 4123 | |
Paolo Bonzini | b4b65b5 | 2019-01-29 19:12:35 +0100 | [diff] [blame] | 4124 | if (nested_cpu_has_preemption_timer(vmcs12) && |
Peter Shier | 850448f | 2020-05-26 14:51:06 -0700 | [diff] [blame] | 4125 | vmcs12->vm_exit_controls & VM_EXIT_SAVE_VMX_PREEMPTION_TIMER && |
| 4126 | !vmx->nested.nested_run_pending) |
| 4127 | vmcs12->vmx_preemption_timer_value = |
| 4128 | vmx_get_preemption_timer_value(vcpu); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4129 | |
| 4130 | /* |
| 4131 | * In some cases (usually, nested EPT), L2 is allowed to change its |
| 4132 | * own CR3 without exiting. If it has changed it, we must keep it. |
| 4133 | * Of course, if L0 is using shadow page tables, GUEST_CR3 was defined |
| 4134 | * by L0, not L1 or L2, so we mustn't unconditionally copy it to vmcs12. |
| 4135 | * |
| 4136 | * Additionally, restore L2's PDPTR to vmcs12. |
| 4137 | */ |
| 4138 | if (enable_ept) { |
| 4139 | vmcs12->guest_cr3 = vmcs_readl(GUEST_CR3); |
Sean Christopherson | c7554efc | 2019-05-07 09:06:40 -0700 | [diff] [blame] | 4140 | if (nested_cpu_has_ept(vmcs12) && is_pae_paging(vcpu)) { |
| 4141 | vmcs12->guest_pdptr0 = vmcs_read64(GUEST_PDPTR0); |
| 4142 | vmcs12->guest_pdptr1 = vmcs_read64(GUEST_PDPTR1); |
| 4143 | vmcs12->guest_pdptr2 = vmcs_read64(GUEST_PDPTR2); |
| 4144 | vmcs12->guest_pdptr3 = vmcs_read64(GUEST_PDPTR3); |
| 4145 | } |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4146 | } |
| 4147 | |
| 4148 | vmcs12->guest_linear_address = vmcs_readl(GUEST_LINEAR_ADDRESS); |
| 4149 | |
| 4150 | if (nested_cpu_has_vid(vmcs12)) |
| 4151 | vmcs12->guest_intr_status = vmcs_read16(GUEST_INTR_STATUS); |
| 4152 | |
| 4153 | vmcs12->vm_entry_controls = |
| 4154 | (vmcs12->vm_entry_controls & ~VM_ENTRY_IA32E_MODE) | |
| 4155 | (vm_entry_controls_get(to_vmx(vcpu)) & VM_ENTRY_IA32E_MODE); |
| 4156 | |
Sean Christopherson | 699a1ac | 2019-05-07 09:06:37 -0700 | [diff] [blame] | 4157 | if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_DEBUG_CONTROLS) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4158 | kvm_get_dr(vcpu, 7, (unsigned long *)&vmcs12->guest_dr7); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4159 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4160 | if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_IA32_EFER) |
| 4161 | vmcs12->guest_ia32_efer = vcpu->arch.efer; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4162 | } |
| 4163 | |
| 4164 | /* |
| 4165 | * prepare_vmcs12 is part of what we need to do when the nested L2 guest exits |
| 4166 | * and we want to prepare to run its L1 parent. L1 keeps a vmcs for L2 (vmcs12), |
| 4167 | * and this function updates it to reflect the changes to the guest state while |
| 4168 | * L2 was running (and perhaps made some exits which were handled directly by L0 |
| 4169 | * without going back to L1), and to reflect the exit reason. |
| 4170 | * Note that we do not have to copy here all VMCS fields, just those that |
| 4171 | * could have changed by the L2 guest or the exit - i.e., the guest-state and |
| 4172 | * exit-information fields only. Other fields are modified by L1 with VMWRITE, |
| 4173 | * which already writes to vmcs12 directly. |
| 4174 | */ |
| 4175 | static void prepare_vmcs12(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12, |
Sean Christopherson | 4dcefa3 | 2020-04-15 10:55:18 -0700 | [diff] [blame] | 4176 | u32 vm_exit_reason, u32 exit_intr_info, |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4177 | unsigned long exit_qualification) |
| 4178 | { |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4179 | /* update exit information fields: */ |
Sean Christopherson | 4dcefa3 | 2020-04-15 10:55:18 -0700 | [diff] [blame] | 4180 | vmcs12->vm_exit_reason = vm_exit_reason; |
Sean Christopherson | 3c0c2ad | 2021-04-12 16:21:37 +1200 | [diff] [blame] | 4181 | if (to_vmx(vcpu)->exit_reason.enclave_mode) |
| 4182 | vmcs12->vm_exit_reason |= VMX_EXIT_REASONS_SGX_ENCLAVE_MODE; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4183 | vmcs12->exit_qualification = exit_qualification; |
| 4184 | vmcs12->vm_exit_intr_info = exit_intr_info; |
| 4185 | |
| 4186 | vmcs12->idt_vectoring_info_field = 0; |
| 4187 | vmcs12->vm_exit_instruction_len = vmcs_read32(VM_EXIT_INSTRUCTION_LEN); |
| 4188 | vmcs12->vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO); |
| 4189 | |
| 4190 | if (!(vmcs12->vm_exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY)) { |
| 4191 | vmcs12->launch_state = 1; |
| 4192 | |
| 4193 | /* vm_entry_intr_info_field is cleared on exit. Emulate this |
| 4194 | * instead of reading the real value. */ |
| 4195 | vmcs12->vm_entry_intr_info_field &= ~INTR_INFO_VALID_MASK; |
| 4196 | |
| 4197 | /* |
| 4198 | * Transfer the event that L0 or L1 may wanted to inject into |
| 4199 | * L2 to IDT_VECTORING_INFO_FIELD. |
| 4200 | */ |
| 4201 | vmcs12_save_pending_event(vcpu, vmcs12); |
Krish Sadhukhan | a0d4f80 | 2018-12-04 19:00:13 -0500 | [diff] [blame] | 4202 | |
| 4203 | /* |
| 4204 | * According to spec, there's no need to store the guest's |
| 4205 | * MSRs if the exit is due to a VM-entry failure that occurs |
| 4206 | * during or after loading the guest state. Since this exit |
| 4207 | * does not fall in that category, we need to save the MSRs. |
| 4208 | */ |
| 4209 | if (nested_vmx_store_msr(vcpu, |
| 4210 | vmcs12->vm_exit_msr_store_addr, |
| 4211 | vmcs12->vm_exit_msr_store_count)) |
| 4212 | nested_vmx_abort(vcpu, |
| 4213 | VMX_ABORT_SAVE_GUEST_MSR_FAIL); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4214 | } |
| 4215 | |
| 4216 | /* |
| 4217 | * Drop what we picked up for L2 via vmx_complete_interrupts. It is |
| 4218 | * preserved above and would only end up incorrectly in L1. |
| 4219 | */ |
| 4220 | vcpu->arch.nmi_injected = false; |
| 4221 | kvm_clear_exception_queue(vcpu); |
| 4222 | kvm_clear_interrupt_queue(vcpu); |
| 4223 | } |
| 4224 | |
| 4225 | /* |
| 4226 | * A part of what we need to when the nested L2 guest exits and we want to |
| 4227 | * run its L1 parent, is to reset L1's guest state to the host state specified |
| 4228 | * in vmcs12. |
| 4229 | * This function is to be called not only on normal nested exit, but also on |
| 4230 | * a nested entry failure, as explained in Intel's spec, 3B.23.7 ("VM-Entry |
| 4231 | * Failures During or After Loading Guest State"). |
| 4232 | * This function should be called when the active VMCS is L1's (vmcs01). |
| 4233 | */ |
| 4234 | static void load_vmcs12_host_state(struct kvm_vcpu *vcpu, |
| 4235 | struct vmcs12 *vmcs12) |
| 4236 | { |
Sean Christopherson | 68cda40 | 2020-05-11 15:05:29 -0700 | [diff] [blame] | 4237 | enum vm_entry_failure_code ignored; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4238 | struct kvm_segment seg; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4239 | |
| 4240 | if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_EFER) |
| 4241 | vcpu->arch.efer = vmcs12->host_ia32_efer; |
| 4242 | else if (vmcs12->vm_exit_controls & VM_EXIT_HOST_ADDR_SPACE_SIZE) |
| 4243 | vcpu->arch.efer |= (EFER_LMA | EFER_LME); |
| 4244 | else |
| 4245 | vcpu->arch.efer &= ~(EFER_LMA | EFER_LME); |
| 4246 | vmx_set_efer(vcpu, vcpu->arch.efer); |
| 4247 | |
Paolo Bonzini | e9c16c7 | 2019-04-30 22:07:26 +0200 | [diff] [blame] | 4248 | kvm_rsp_write(vcpu, vmcs12->host_rsp); |
| 4249 | kvm_rip_write(vcpu, vmcs12->host_rip); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4250 | vmx_set_rflags(vcpu, X86_EFLAGS_FIXED); |
| 4251 | vmx_set_interrupt_shadow(vcpu, 0); |
| 4252 | |
| 4253 | /* |
| 4254 | * Note that calling vmx_set_cr0 is important, even if cr0 hasn't |
| 4255 | * actually changed, because vmx_set_cr0 refers to efer set above. |
| 4256 | * |
| 4257 | * CR0_GUEST_HOST_MASK is already set in the original vmcs01 |
| 4258 | * (KVM doesn't change it); |
| 4259 | */ |
Sean Christopherson | fa71e95 | 2020-07-02 21:04:22 -0700 | [diff] [blame] | 4260 | vcpu->arch.cr0_guest_owned_bits = KVM_POSSIBLE_CR0_GUEST_BITS; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4261 | vmx_set_cr0(vcpu, vmcs12->host_cr0); |
| 4262 | |
| 4263 | /* Same as above - no reason to call set_cr4_guest_host_mask(). */ |
| 4264 | vcpu->arch.cr4_guest_owned_bits = ~vmcs_readl(CR4_GUEST_HOST_MASK); |
| 4265 | vmx_set_cr4(vcpu, vmcs12->host_cr4); |
| 4266 | |
| 4267 | nested_ept_uninit_mmu_context(vcpu); |
| 4268 | |
| 4269 | /* |
| 4270 | * Only PDPTE load can fail as the value of cr3 was checked on entry and |
| 4271 | * couldn't have changed. |
| 4272 | */ |
Maxim Levitsky | 0f85722 | 2021-06-07 12:02:00 +0300 | [diff] [blame] | 4273 | if (nested_vmx_load_cr3(vcpu, vmcs12->host_cr3, false, true, &ignored)) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4274 | nested_vmx_abort(vcpu, VMX_ABORT_LOAD_HOST_PDPTE_FAIL); |
| 4275 | |
Sean Christopherson | 50b265a | 2020-03-20 14:28:19 -0700 | [diff] [blame] | 4276 | nested_vmx_transition_tlb_flush(vcpu, vmcs12, false); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4277 | |
| 4278 | vmcs_write32(GUEST_SYSENTER_CS, vmcs12->host_ia32_sysenter_cs); |
| 4279 | vmcs_writel(GUEST_SYSENTER_ESP, vmcs12->host_ia32_sysenter_esp); |
| 4280 | vmcs_writel(GUEST_SYSENTER_EIP, vmcs12->host_ia32_sysenter_eip); |
| 4281 | vmcs_writel(GUEST_IDTR_BASE, vmcs12->host_idtr_base); |
| 4282 | vmcs_writel(GUEST_GDTR_BASE, vmcs12->host_gdtr_base); |
| 4283 | vmcs_write32(GUEST_IDTR_LIMIT, 0xFFFF); |
| 4284 | vmcs_write32(GUEST_GDTR_LIMIT, 0xFFFF); |
| 4285 | |
| 4286 | /* If not VM_EXIT_CLEAR_BNDCFGS, the L2 value propagates to L1. */ |
| 4287 | if (vmcs12->vm_exit_controls & VM_EXIT_CLEAR_BNDCFGS) |
| 4288 | vmcs_write64(GUEST_BNDCFGS, 0); |
| 4289 | |
| 4290 | if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PAT) { |
| 4291 | vmcs_write64(GUEST_IA32_PAT, vmcs12->host_ia32_pat); |
| 4292 | vcpu->arch.pat = vmcs12->host_ia32_pat; |
| 4293 | } |
| 4294 | if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL) |
Oliver Upton | d196842 | 2019-12-13 16:33:58 -0800 | [diff] [blame] | 4295 | WARN_ON_ONCE(kvm_set_msr(vcpu, MSR_CORE_PERF_GLOBAL_CTRL, |
| 4296 | vmcs12->host_ia32_perf_global_ctrl)); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4297 | |
| 4298 | /* Set L1 segment info according to Intel SDM |
| 4299 | 27.5.2 Loading Host Segment and Descriptor-Table Registers */ |
| 4300 | seg = (struct kvm_segment) { |
| 4301 | .base = 0, |
| 4302 | .limit = 0xFFFFFFFF, |
| 4303 | .selector = vmcs12->host_cs_selector, |
| 4304 | .type = 11, |
| 4305 | .present = 1, |
| 4306 | .s = 1, |
| 4307 | .g = 1 |
| 4308 | }; |
| 4309 | if (vmcs12->vm_exit_controls & VM_EXIT_HOST_ADDR_SPACE_SIZE) |
| 4310 | seg.l = 1; |
| 4311 | else |
| 4312 | seg.db = 1; |
Sean Christopherson | 816be9e | 2021-07-13 09:33:07 -0700 | [diff] [blame] | 4313 | __vmx_set_segment(vcpu, &seg, VCPU_SREG_CS); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4314 | seg = (struct kvm_segment) { |
| 4315 | .base = 0, |
| 4316 | .limit = 0xFFFFFFFF, |
| 4317 | .type = 3, |
| 4318 | .present = 1, |
| 4319 | .s = 1, |
| 4320 | .db = 1, |
| 4321 | .g = 1 |
| 4322 | }; |
| 4323 | seg.selector = vmcs12->host_ds_selector; |
Sean Christopherson | 816be9e | 2021-07-13 09:33:07 -0700 | [diff] [blame] | 4324 | __vmx_set_segment(vcpu, &seg, VCPU_SREG_DS); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4325 | seg.selector = vmcs12->host_es_selector; |
Sean Christopherson | 816be9e | 2021-07-13 09:33:07 -0700 | [diff] [blame] | 4326 | __vmx_set_segment(vcpu, &seg, VCPU_SREG_ES); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4327 | seg.selector = vmcs12->host_ss_selector; |
Sean Christopherson | 816be9e | 2021-07-13 09:33:07 -0700 | [diff] [blame] | 4328 | __vmx_set_segment(vcpu, &seg, VCPU_SREG_SS); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4329 | seg.selector = vmcs12->host_fs_selector; |
| 4330 | seg.base = vmcs12->host_fs_base; |
Sean Christopherson | 816be9e | 2021-07-13 09:33:07 -0700 | [diff] [blame] | 4331 | __vmx_set_segment(vcpu, &seg, VCPU_SREG_FS); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4332 | seg.selector = vmcs12->host_gs_selector; |
| 4333 | seg.base = vmcs12->host_gs_base; |
Sean Christopherson | 816be9e | 2021-07-13 09:33:07 -0700 | [diff] [blame] | 4334 | __vmx_set_segment(vcpu, &seg, VCPU_SREG_GS); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4335 | seg = (struct kvm_segment) { |
| 4336 | .base = vmcs12->host_tr_base, |
| 4337 | .limit = 0x67, |
| 4338 | .selector = vmcs12->host_tr_selector, |
| 4339 | .type = 11, |
| 4340 | .present = 1 |
| 4341 | }; |
Sean Christopherson | 816be9e | 2021-07-13 09:33:07 -0700 | [diff] [blame] | 4342 | __vmx_set_segment(vcpu, &seg, VCPU_SREG_TR); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4343 | |
Sean Christopherson | afc8de0 | 2021-07-13 09:32:40 -0700 | [diff] [blame] | 4344 | memset(&seg, 0, sizeof(seg)); |
| 4345 | seg.unusable = 1; |
Sean Christopherson | 816be9e | 2021-07-13 09:33:07 -0700 | [diff] [blame] | 4346 | __vmx_set_segment(vcpu, &seg, VCPU_SREG_LDTR); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4347 | |
| 4348 | kvm_set_dr(vcpu, 7, 0x400); |
| 4349 | vmcs_write64(GUEST_IA32_DEBUGCTL, 0); |
| 4350 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4351 | if (nested_vmx_load_msr(vcpu, vmcs12->vm_exit_msr_load_addr, |
| 4352 | vmcs12->vm_exit_msr_load_count)) |
| 4353 | nested_vmx_abort(vcpu, VMX_ABORT_LOAD_HOST_MSR_FAIL); |
| 4354 | } |
| 4355 | |
| 4356 | static inline u64 nested_vmx_get_vmcs01_guest_efer(struct vcpu_vmx *vmx) |
| 4357 | { |
Sean Christopherson | eb3db1b | 2020-09-23 11:03:58 -0700 | [diff] [blame] | 4358 | struct vmx_uret_msr *efer_msr; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4359 | unsigned int i; |
| 4360 | |
| 4361 | if (vm_entry_controls_get(vmx) & VM_ENTRY_LOAD_IA32_EFER) |
| 4362 | return vmcs_read64(GUEST_IA32_EFER); |
| 4363 | |
| 4364 | if (cpu_has_load_ia32_efer()) |
| 4365 | return host_efer; |
| 4366 | |
| 4367 | for (i = 0; i < vmx->msr_autoload.guest.nr; ++i) { |
| 4368 | if (vmx->msr_autoload.guest.val[i].index == MSR_EFER) |
| 4369 | return vmx->msr_autoload.guest.val[i].value; |
| 4370 | } |
| 4371 | |
Sean Christopherson | d85a803 | 2020-09-23 11:04:06 -0700 | [diff] [blame] | 4372 | efer_msr = vmx_find_uret_msr(vmx, MSR_EFER); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4373 | if (efer_msr) |
| 4374 | return efer_msr->data; |
| 4375 | |
| 4376 | return host_efer; |
| 4377 | } |
| 4378 | |
| 4379 | static void nested_vmx_restore_host_state(struct kvm_vcpu *vcpu) |
| 4380 | { |
| 4381 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
| 4382 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 4383 | struct vmx_msr_entry g, h; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4384 | gpa_t gpa; |
| 4385 | u32 i, j; |
| 4386 | |
| 4387 | vcpu->arch.pat = vmcs_read64(GUEST_IA32_PAT); |
| 4388 | |
| 4389 | if (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS) { |
| 4390 | /* |
| 4391 | * L1's host DR7 is lost if KVM_GUESTDBG_USE_HW_BP is set |
| 4392 | * as vmcs01.GUEST_DR7 contains a userspace defined value |
| 4393 | * and vcpu->arch.dr7 is not squirreled away before the |
| 4394 | * nested VMENTER (not worth adding a variable in nested_vmx). |
| 4395 | */ |
| 4396 | if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP) |
| 4397 | kvm_set_dr(vcpu, 7, DR7_FIXED_1); |
| 4398 | else |
| 4399 | WARN_ON(kvm_set_dr(vcpu, 7, vmcs_readl(GUEST_DR7))); |
| 4400 | } |
| 4401 | |
| 4402 | /* |
| 4403 | * Note that calling vmx_set_{efer,cr0,cr4} is important as they |
| 4404 | * handle a variety of side effects to KVM's software model. |
| 4405 | */ |
| 4406 | vmx_set_efer(vcpu, nested_vmx_get_vmcs01_guest_efer(vmx)); |
| 4407 | |
Sean Christopherson | fa71e95 | 2020-07-02 21:04:22 -0700 | [diff] [blame] | 4408 | vcpu->arch.cr0_guest_owned_bits = KVM_POSSIBLE_CR0_GUEST_BITS; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4409 | vmx_set_cr0(vcpu, vmcs_readl(CR0_READ_SHADOW)); |
| 4410 | |
| 4411 | vcpu->arch.cr4_guest_owned_bits = ~vmcs_readl(CR4_GUEST_HOST_MASK); |
| 4412 | vmx_set_cr4(vcpu, vmcs_readl(CR4_READ_SHADOW)); |
| 4413 | |
| 4414 | nested_ept_uninit_mmu_context(vcpu); |
Sean Christopherson | f087a02 | 2019-06-07 11:55:34 -0700 | [diff] [blame] | 4415 | vcpu->arch.cr3 = vmcs_readl(GUEST_CR3); |
Sean Christopherson | cb3c1e2 | 2019-09-27 14:45:22 -0700 | [diff] [blame] | 4416 | kvm_register_mark_available(vcpu, VCPU_EXREG_CR3); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4417 | |
| 4418 | /* |
| 4419 | * Use ept_save_pdptrs(vcpu) to load the MMU's cached PDPTRs |
| 4420 | * from vmcs01 (if necessary). The PDPTRs are not loaded on |
| 4421 | * VMFail, like everything else we just need to ensure our |
| 4422 | * software model is up-to-date. |
| 4423 | */ |
Sean Christopherson | 9932b49 | 2020-04-15 13:34:50 -0700 | [diff] [blame] | 4424 | if (enable_ept && is_pae_paging(vcpu)) |
Sean Christopherson | f087a02 | 2019-06-07 11:55:34 -0700 | [diff] [blame] | 4425 | ept_save_pdptrs(vcpu); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4426 | |
| 4427 | kvm_mmu_reset_context(vcpu); |
| 4428 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4429 | /* |
| 4430 | * This nasty bit of open coding is a compromise between blindly |
| 4431 | * loading L1's MSRs using the exit load lists (incorrect emulation |
| 4432 | * of VMFail), leaving the nested VM's MSRs in the software model |
| 4433 | * (incorrect behavior) and snapshotting the modified MSRs (too |
| 4434 | * expensive since the lists are unbound by hardware). For each |
| 4435 | * MSR that was (prematurely) loaded from the nested VMEntry load |
| 4436 | * list, reload it from the exit load list if it exists and differs |
| 4437 | * from the guest value. The intent is to stuff host state as |
| 4438 | * silently as possible, not to fully process the exit load list. |
| 4439 | */ |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4440 | for (i = 0; i < vmcs12->vm_entry_msr_load_count; i++) { |
| 4441 | gpa = vmcs12->vm_entry_msr_load_addr + (i * sizeof(g)); |
| 4442 | if (kvm_vcpu_read_guest(vcpu, gpa, &g, sizeof(g))) { |
| 4443 | pr_debug_ratelimited( |
| 4444 | "%s read MSR index failed (%u, 0x%08llx)\n", |
| 4445 | __func__, i, gpa); |
| 4446 | goto vmabort; |
| 4447 | } |
| 4448 | |
| 4449 | for (j = 0; j < vmcs12->vm_exit_msr_load_count; j++) { |
| 4450 | gpa = vmcs12->vm_exit_msr_load_addr + (j * sizeof(h)); |
| 4451 | if (kvm_vcpu_read_guest(vcpu, gpa, &h, sizeof(h))) { |
| 4452 | pr_debug_ratelimited( |
| 4453 | "%s read MSR failed (%u, 0x%08llx)\n", |
| 4454 | __func__, j, gpa); |
| 4455 | goto vmabort; |
| 4456 | } |
| 4457 | if (h.index != g.index) |
| 4458 | continue; |
| 4459 | if (h.value == g.value) |
| 4460 | break; |
| 4461 | |
| 4462 | if (nested_vmx_load_msr_check(vcpu, &h)) { |
| 4463 | pr_debug_ratelimited( |
| 4464 | "%s check failed (%u, 0x%x, 0x%x)\n", |
| 4465 | __func__, j, h.index, h.reserved); |
| 4466 | goto vmabort; |
| 4467 | } |
| 4468 | |
Sean Christopherson | f20935d | 2019-09-05 14:22:54 -0700 | [diff] [blame] | 4469 | if (kvm_set_msr(vcpu, h.index, h.value)) { |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4470 | pr_debug_ratelimited( |
| 4471 | "%s WRMSR failed (%u, 0x%x, 0x%llx)\n", |
| 4472 | __func__, j, h.index, h.value); |
| 4473 | goto vmabort; |
| 4474 | } |
| 4475 | } |
| 4476 | } |
| 4477 | |
| 4478 | return; |
| 4479 | |
| 4480 | vmabort: |
| 4481 | nested_vmx_abort(vcpu, VMX_ABORT_LOAD_HOST_MSR_FAIL); |
| 4482 | } |
| 4483 | |
| 4484 | /* |
| 4485 | * Emulate an exit from nested guest (L2) to L1, i.e., prepare to run L1 |
| 4486 | * and modify vmcs12 to make it see what it would expect to see there if |
| 4487 | * L2 was its real guest. Must only be called when in L2 (is_guest_mode()) |
| 4488 | */ |
Sean Christopherson | 4dcefa3 | 2020-04-15 10:55:18 -0700 | [diff] [blame] | 4489 | void nested_vmx_vmexit(struct kvm_vcpu *vcpu, u32 vm_exit_reason, |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4490 | u32 exit_intr_info, unsigned long exit_qualification) |
| 4491 | { |
| 4492 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 4493 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
| 4494 | |
| 4495 | /* trying to cancel vmlaunch/vmresume is a bug */ |
| 4496 | WARN_ON_ONCE(vmx->nested.nested_run_pending); |
| 4497 | |
Sean Christopherson | cb6a32c | 2021-03-02 09:45:14 -0800 | [diff] [blame] | 4498 | /* Similarly, triple faults in L2 should never escape. */ |
| 4499 | WARN_ON_ONCE(kvm_check_request(KVM_REQ_TRIPLE_FAULT, vcpu)); |
| 4500 | |
Vitaly Kuznetsov | f5c7e84 | 2021-05-03 17:08:51 +0200 | [diff] [blame] | 4501 | if (kvm_check_request(KVM_REQ_GET_NESTED_STATE_PAGES, vcpu)) { |
| 4502 | /* |
| 4503 | * KVM_REQ_GET_NESTED_STATE_PAGES is also used to map |
| 4504 | * Enlightened VMCS after migration and we still need to |
| 4505 | * do that when something is forcing L2->L1 exit prior to |
| 4506 | * the first L2 run. |
| 4507 | */ |
| 4508 | (void)nested_get_evmcs_page(vcpu); |
| 4509 | } |
Maxim Levitsky | f2c7ef3 | 2021-01-07 11:38:51 +0200 | [diff] [blame] | 4510 | |
Sean Christopherson | eeeb4f6 | 2020-03-20 14:28:20 -0700 | [diff] [blame] | 4511 | /* Service the TLB flush request for L2 before switching to L1. */ |
| 4512 | if (kvm_check_request(KVM_REQ_TLB_FLUSH_CURRENT, vcpu)) |
| 4513 | kvm_vcpu_flush_tlb_current(vcpu); |
| 4514 | |
Peter Shier | 43fea4e | 2020-08-20 16:05:45 -0700 | [diff] [blame] | 4515 | /* |
| 4516 | * VCPU_EXREG_PDPTR will be clobbered in arch/x86/kvm/vmx/vmx.h between |
| 4517 | * now and the new vmentry. Ensure that the VMCS02 PDPTR fields are |
| 4518 | * up-to-date before switching to L1. |
| 4519 | */ |
| 4520 | if (enable_ept && is_pae_paging(vcpu)) |
| 4521 | vmx_ept_load_pdptrs(vcpu); |
| 4522 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4523 | leave_guest_mode(vcpu); |
| 4524 | |
Paolo Bonzini | b4b65b5 | 2019-01-29 19:12:35 +0100 | [diff] [blame] | 4525 | if (nested_cpu_has_preemption_timer(vmcs12)) |
| 4526 | hrtimer_cancel(&to_vmx(vcpu)->nested.preemption_timer); |
| 4527 | |
Ilias Stamatis | d041b5e | 2021-05-26 19:44:17 +0100 | [diff] [blame] | 4528 | if (nested_cpu_has(vmcs12, CPU_BASED_USE_TSC_OFFSETTING)) { |
| 4529 | vcpu->arch.tsc_offset = vcpu->arch.l1_tsc_offset; |
| 4530 | if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_TSC_SCALING)) |
| 4531 | vcpu->arch.tsc_scaling_ratio = vcpu->arch.l1_tsc_scaling_ratio; |
| 4532 | } |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4533 | |
| 4534 | if (likely(!vmx->fail)) { |
Sean Christopherson | 3731905ef | 2019-05-07 08:36:27 -0700 | [diff] [blame] | 4535 | sync_vmcs02_to_vmcs12(vcpu, vmcs12); |
Sean Christopherson | f4f8316 | 2019-05-07 08:36:26 -0700 | [diff] [blame] | 4536 | |
Sean Christopherson | 4dcefa3 | 2020-04-15 10:55:18 -0700 | [diff] [blame] | 4537 | if (vm_exit_reason != -1) |
| 4538 | prepare_vmcs12(vcpu, vmcs12, vm_exit_reason, |
| 4539 | exit_intr_info, exit_qualification); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4540 | |
| 4541 | /* |
Sean Christopherson | 3731905ef | 2019-05-07 08:36:27 -0700 | [diff] [blame] | 4542 | * Must happen outside of sync_vmcs02_to_vmcs12() as it will |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4543 | * also be used to capture vmcs12 cache as part of |
| 4544 | * capturing nVMX state for snapshot (migration). |
| 4545 | * |
| 4546 | * Otherwise, this flush will dirty guest memory at a |
| 4547 | * point it is already assumed by user-space to be |
| 4548 | * immutable. |
| 4549 | */ |
| 4550 | nested_flush_cached_shadow_vmcs12(vcpu, vmcs12); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4551 | } else { |
| 4552 | /* |
| 4553 | * The only expected VM-instruction error is "VM entry with |
| 4554 | * invalid control field(s)." Anything else indicates a |
| 4555 | * problem with L0. And we should never get here with a |
| 4556 | * VMFail of any type if early consistency checks are enabled. |
| 4557 | */ |
| 4558 | WARN_ON_ONCE(vmcs_read32(VM_INSTRUCTION_ERROR) != |
| 4559 | VMXERR_ENTRY_INVALID_CONTROL_FIELD); |
| 4560 | WARN_ON_ONCE(nested_early_check); |
| 4561 | } |
| 4562 | |
| 4563 | vmx_switch_vmcs(vcpu, &vmx->vmcs01); |
| 4564 | |
| 4565 | /* Update any VMCS fields that might have changed while L2 ran */ |
| 4566 | vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, vmx->msr_autoload.host.nr); |
| 4567 | vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, vmx->msr_autoload.guest.nr); |
| 4568 | vmcs_write64(TSC_OFFSET, vcpu->arch.tsc_offset); |
Ilias Stamatis | 1ab9287 | 2021-06-07 11:54:38 +0100 | [diff] [blame] | 4569 | if (kvm_has_tsc_control) |
| 4570 | vmcs_write64(TSC_MULTIPLIER, vcpu->arch.tsc_scaling_ratio); |
| 4571 | |
Liran Alon | 02d496cf | 2019-11-11 14:30:55 +0200 | [diff] [blame] | 4572 | if (vmx->nested.l1_tpr_threshold != -1) |
| 4573 | vmcs_write32(TPR_THRESHOLD, vmx->nested.l1_tpr_threshold); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4574 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4575 | if (vmx->nested.change_vmcs01_virtual_apic_mode) { |
| 4576 | vmx->nested.change_vmcs01_virtual_apic_mode = false; |
| 4577 | vmx_set_virtual_apic_mode(vcpu); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4578 | } |
| 4579 | |
Makarand Sonare | a85863c | 2021-02-12 16:50:12 -0800 | [diff] [blame] | 4580 | if (vmx->nested.update_vmcs01_cpu_dirty_logging) { |
| 4581 | vmx->nested.update_vmcs01_cpu_dirty_logging = false; |
| 4582 | vmx_update_cpu_dirty_logging(vcpu); |
| 4583 | } |
| 4584 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4585 | /* Unpin physical memory we referred to in vmcs02 */ |
| 4586 | if (vmx->nested.apic_access_page) { |
Liran Alon | b11494b | 2019-11-21 00:31:47 +0200 | [diff] [blame] | 4587 | kvm_release_page_clean(vmx->nested.apic_access_page); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4588 | vmx->nested.apic_access_page = NULL; |
| 4589 | } |
KarimAllah Ahmed | 96c66e8 | 2019-01-31 21:24:37 +0100 | [diff] [blame] | 4590 | kvm_vcpu_unmap(vcpu, &vmx->nested.virtual_apic_map, true); |
KarimAllah Ahmed | 3278e04 | 2019-01-31 21:24:38 +0100 | [diff] [blame] | 4591 | kvm_vcpu_unmap(vcpu, &vmx->nested.pi_desc_map, true); |
| 4592 | vmx->nested.pi_desc = NULL; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4593 | |
Sean Christopherson | 1196cb9 | 2020-03-20 14:28:23 -0700 | [diff] [blame] | 4594 | if (vmx->nested.reload_vmcs01_apic_access_page) { |
| 4595 | vmx->nested.reload_vmcs01_apic_access_page = false; |
| 4596 | kvm_make_request(KVM_REQ_APIC_PAGE_RELOAD, vcpu); |
| 4597 | } |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4598 | |
Sean Christopherson | 4dcefa3 | 2020-04-15 10:55:18 -0700 | [diff] [blame] | 4599 | if ((vm_exit_reason != -1) && |
Vitaly Kuznetsov | 1e9dfbd | 2021-05-26 15:20:16 +0200 | [diff] [blame] | 4600 | (enable_shadow_vmcs || evmptr_is_valid(vmx->nested.hv_evmcs_vmptr))) |
Sean Christopherson | 3731905ef | 2019-05-07 08:36:27 -0700 | [diff] [blame] | 4601 | vmx->nested.need_vmcs12_to_shadow_sync = true; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4602 | |
| 4603 | /* in case we halted in L2 */ |
| 4604 | vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE; |
| 4605 | |
| 4606 | if (likely(!vmx->fail)) { |
Sean Christopherson | 4dcefa3 | 2020-04-15 10:55:18 -0700 | [diff] [blame] | 4607 | if ((u16)vm_exit_reason == EXIT_REASON_EXTERNAL_INTERRUPT && |
Sean Christopherson | a1c77ab | 2020-03-02 22:27:35 -0800 | [diff] [blame] | 4608 | nested_exit_intr_ack_set(vcpu)) { |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4609 | int irq = kvm_cpu_get_interrupt(vcpu); |
| 4610 | WARN_ON(irq < 0); |
| 4611 | vmcs12->vm_exit_intr_info = irq | |
| 4612 | INTR_INFO_VALID_MASK | INTR_TYPE_EXT_INTR; |
| 4613 | } |
| 4614 | |
Sean Christopherson | 4dcefa3 | 2020-04-15 10:55:18 -0700 | [diff] [blame] | 4615 | if (vm_exit_reason != -1) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4616 | trace_kvm_nested_vmexit_inject(vmcs12->vm_exit_reason, |
| 4617 | vmcs12->exit_qualification, |
| 4618 | vmcs12->idt_vectoring_info_field, |
| 4619 | vmcs12->vm_exit_intr_info, |
| 4620 | vmcs12->vm_exit_intr_error_code, |
| 4621 | KVM_ISA_VMX); |
| 4622 | |
| 4623 | load_vmcs12_host_state(vcpu, vmcs12); |
| 4624 | |
| 4625 | return; |
| 4626 | } |
| 4627 | |
| 4628 | /* |
| 4629 | * After an early L2 VM-entry failure, we're now back |
| 4630 | * in L1 which thinks it just finished a VMLAUNCH or |
| 4631 | * VMRESUME instruction, so we need to set the failure |
| 4632 | * flag and the VM-instruction error field of the VMCS |
| 4633 | * accordingly, and skip the emulated instruction. |
| 4634 | */ |
Sean Christopherson | b2656e4 | 2020-06-08 18:56:07 -0700 | [diff] [blame] | 4635 | (void)nested_vmx_fail(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4636 | |
| 4637 | /* |
| 4638 | * Restore L1's host state to KVM's software model. We're here |
| 4639 | * because a consistency check was caught by hardware, which |
| 4640 | * means some amount of guest state has been propagated to KVM's |
| 4641 | * model and needs to be unwound to the host's state. |
| 4642 | */ |
| 4643 | nested_vmx_restore_host_state(vcpu); |
| 4644 | |
| 4645 | vmx->fail = 0; |
| 4646 | } |
| 4647 | |
Sean Christopherson | cb6a32c | 2021-03-02 09:45:14 -0800 | [diff] [blame] | 4648 | static void nested_vmx_triple_fault(struct kvm_vcpu *vcpu) |
| 4649 | { |
| 4650 | nested_vmx_vmexit(vcpu, EXIT_REASON_TRIPLE_FAULT, 0, 0); |
| 4651 | } |
| 4652 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4653 | /* |
| 4654 | * Decode the memory-address operand of a vmx instruction, as recorded on an |
| 4655 | * exit caused by such an instruction (run by a guest hypervisor). |
| 4656 | * 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] | 4657 | * #UD, #GP, or #SS. |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4658 | */ |
| 4659 | 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] | 4660 | u32 vmx_instruction_info, bool wr, int len, gva_t *ret) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4661 | { |
| 4662 | gva_t off; |
| 4663 | bool exn; |
| 4664 | struct kvm_segment s; |
| 4665 | |
| 4666 | /* |
| 4667 | * According to Vol. 3B, "Information for VM Exits Due to Instruction |
| 4668 | * Execution", on an exit, vmx_instruction_info holds most of the |
| 4669 | * addressing components of the operand. Only the displacement part |
| 4670 | * is put in exit_qualification (see 3B, "Basic VM-Exit Information"). |
| 4671 | * For how an actual address is calculated from all these components, |
| 4672 | * refer to Vol. 1, "Operand Addressing". |
| 4673 | */ |
| 4674 | int scaling = vmx_instruction_info & 3; |
| 4675 | int addr_size = (vmx_instruction_info >> 7) & 7; |
| 4676 | bool is_reg = vmx_instruction_info & (1u << 10); |
| 4677 | int seg_reg = (vmx_instruction_info >> 15) & 7; |
| 4678 | int index_reg = (vmx_instruction_info >> 18) & 0xf; |
| 4679 | bool index_is_valid = !(vmx_instruction_info & (1u << 22)); |
| 4680 | int base_reg = (vmx_instruction_info >> 23) & 0xf; |
| 4681 | bool base_is_valid = !(vmx_instruction_info & (1u << 27)); |
| 4682 | |
| 4683 | if (is_reg) { |
| 4684 | kvm_queue_exception(vcpu, UD_VECTOR); |
| 4685 | return 1; |
| 4686 | } |
| 4687 | |
| 4688 | /* Addr = segment_base + offset */ |
| 4689 | /* offset = base + [index * scale] + displacement */ |
| 4690 | off = exit_qualification; /* holds the displacement */ |
Sean Christopherson | 946c522 | 2019-01-23 14:39:23 -0800 | [diff] [blame] | 4691 | if (addr_size == 1) |
| 4692 | off = (gva_t)sign_extend64(off, 31); |
| 4693 | else if (addr_size == 0) |
| 4694 | off = (gva_t)sign_extend64(off, 15); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4695 | if (base_is_valid) |
| 4696 | off += kvm_register_read(vcpu, base_reg); |
| 4697 | if (index_is_valid) |
Miaohe Lin | e630269 | 2020-02-15 10:44:22 +0800 | [diff] [blame] | 4698 | off += kvm_register_read(vcpu, index_reg) << scaling; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4699 | vmx_get_segment(vcpu, &s, seg_reg); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4700 | |
Sean Christopherson | 8570f9e | 2019-01-23 14:39:24 -0800 | [diff] [blame] | 4701 | /* |
| 4702 | * The effective address, i.e. @off, of a memory operand is truncated |
| 4703 | * based on the address size of the instruction. Note that this is |
| 4704 | * the *effective address*, i.e. the address prior to accounting for |
| 4705 | * the segment's base. |
| 4706 | */ |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4707 | if (addr_size == 1) /* 32 bit */ |
Sean Christopherson | 8570f9e | 2019-01-23 14:39:24 -0800 | [diff] [blame] | 4708 | off &= 0xffffffff; |
| 4709 | else if (addr_size == 0) /* 16 bit */ |
| 4710 | off &= 0xffff; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4711 | |
| 4712 | /* Checks for #GP/#SS exceptions. */ |
| 4713 | exn = false; |
| 4714 | if (is_long_mode(vcpu)) { |
Sean Christopherson | 8570f9e | 2019-01-23 14:39:24 -0800 | [diff] [blame] | 4715 | /* |
| 4716 | * The virtual/linear address is never truncated in 64-bit |
| 4717 | * mode, e.g. a 32-bit address size can yield a 64-bit virtual |
| 4718 | * address when using FS/GS with a non-zero base. |
| 4719 | */ |
Liran Alon | 6694e48 | 2019-07-15 18:47:44 +0300 | [diff] [blame] | 4720 | if (seg_reg == VCPU_SREG_FS || seg_reg == VCPU_SREG_GS) |
| 4721 | *ret = s.base + off; |
| 4722 | else |
| 4723 | *ret = off; |
Sean Christopherson | 8570f9e | 2019-01-23 14:39:24 -0800 | [diff] [blame] | 4724 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4725 | /* Long mode: #GP(0)/#SS(0) if the memory address is in a |
| 4726 | * non-canonical form. This is the only check on the memory |
| 4727 | * destination for long mode! |
| 4728 | */ |
| 4729 | exn = is_noncanonical_address(*ret, vcpu); |
Paolo Bonzini | e0dfacb | 2019-01-30 17:25:38 +0100 | [diff] [blame] | 4730 | } else { |
Sean Christopherson | 8570f9e | 2019-01-23 14:39:24 -0800 | [diff] [blame] | 4731 | /* |
| 4732 | * When not in long mode, the virtual/linear address is |
| 4733 | * unconditionally truncated to 32 bits regardless of the |
| 4734 | * address size. |
| 4735 | */ |
| 4736 | *ret = (s.base + off) & 0xffffffff; |
| 4737 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4738 | /* Protected mode: apply checks for segment validity in the |
| 4739 | * following order: |
| 4740 | * - segment type check (#GP(0) may be thrown) |
| 4741 | * - usability check (#GP(0)/#SS(0)) |
| 4742 | * - limit check (#GP(0)/#SS(0)) |
| 4743 | */ |
| 4744 | if (wr) |
| 4745 | /* #GP(0) if the destination operand is located in a |
| 4746 | * read-only data segment or any code segment. |
| 4747 | */ |
| 4748 | exn = ((s.type & 0xa) == 0 || (s.type & 8)); |
| 4749 | else |
| 4750 | /* #GP(0) if the source operand is located in an |
| 4751 | * execute-only code segment |
| 4752 | */ |
| 4753 | exn = ((s.type & 0xa) == 8); |
| 4754 | if (exn) { |
| 4755 | kvm_queue_exception_e(vcpu, GP_VECTOR, 0); |
| 4756 | return 1; |
| 4757 | } |
| 4758 | /* Protected mode: #GP(0)/#SS(0) if the segment is unusable. |
| 4759 | */ |
| 4760 | exn = (s.unusable != 0); |
Sean Christopherson | 34333cc | 2019-01-23 14:39:25 -0800 | [diff] [blame] | 4761 | |
| 4762 | /* |
| 4763 | * Protected mode: #GP(0)/#SS(0) if the memory operand is |
| 4764 | * outside the segment limit. All CPUs that support VMX ignore |
| 4765 | * limit checks for flat segments, i.e. segments with base==0, |
| 4766 | * limit==0xffffffff and of type expand-up data or code. |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4767 | */ |
Sean Christopherson | 34333cc | 2019-01-23 14:39:25 -0800 | [diff] [blame] | 4768 | if (!(s.base == 0 && s.limit == 0xffffffff && |
| 4769 | ((s.type & 8) || !(s.type & 4)))) |
Eugene Korenevsky | fdb2861 | 2019-06-06 00:19:16 +0300 | [diff] [blame] | 4770 | exn = exn || ((u64)off + len - 1 > s.limit); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4771 | } |
| 4772 | if (exn) { |
| 4773 | kvm_queue_exception_e(vcpu, |
| 4774 | seg_reg == VCPU_SREG_SS ? |
| 4775 | SS_VECTOR : GP_VECTOR, |
| 4776 | 0); |
| 4777 | return 1; |
| 4778 | } |
| 4779 | |
| 4780 | return 0; |
| 4781 | } |
| 4782 | |
Oliver Upton | 03a8871a | 2019-11-13 16:17:20 -0800 | [diff] [blame] | 4783 | void nested_vmx_pmu_entry_exit_ctls_update(struct kvm_vcpu *vcpu) |
| 4784 | { |
| 4785 | struct vcpu_vmx *vmx; |
| 4786 | |
| 4787 | if (!nested_vmx_allowed(vcpu)) |
| 4788 | return; |
| 4789 | |
| 4790 | vmx = to_vmx(vcpu); |
Sean Christopherson | afaf0b2 | 2020-03-21 13:26:00 -0700 | [diff] [blame] | 4791 | 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] | 4792 | vmx->nested.msrs.entry_ctls_high |= |
| 4793 | VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL; |
| 4794 | vmx->nested.msrs.exit_ctls_high |= |
| 4795 | VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL; |
| 4796 | } else { |
| 4797 | vmx->nested.msrs.entry_ctls_high &= |
| 4798 | ~VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL; |
| 4799 | vmx->nested.msrs.exit_ctls_high &= |
Chenyi Qiang | c6b177a | 2020-08-28 16:56:21 +0800 | [diff] [blame] | 4800 | ~VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL; |
Oliver Upton | 03a8871a | 2019-11-13 16:17:20 -0800 | [diff] [blame] | 4801 | } |
| 4802 | } |
| 4803 | |
Vitaly Kuznetsov | 7a35e51 | 2020-06-05 13:59:05 +0200 | [diff] [blame] | 4804 | static int nested_vmx_get_vmptr(struct kvm_vcpu *vcpu, gpa_t *vmpointer, |
| 4805 | int *ret) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4806 | { |
| 4807 | gva_t gva; |
| 4808 | struct x86_exception e; |
Vitaly Kuznetsov | 7a35e51 | 2020-06-05 13:59:05 +0200 | [diff] [blame] | 4809 | int r; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4810 | |
Sean Christopherson | 5addc23 | 2020-04-15 13:34:53 -0700 | [diff] [blame] | 4811 | if (get_vmx_mem_address(vcpu, vmx_get_exit_qual(vcpu), |
Eugene Korenevsky | fdb2861 | 2019-06-06 00:19:16 +0300 | [diff] [blame] | 4812 | vmcs_read32(VMX_INSTRUCTION_INFO), false, |
Vitaly Kuznetsov | 7a35e51 | 2020-06-05 13:59:05 +0200 | [diff] [blame] | 4813 | sizeof(*vmpointer), &gva)) { |
| 4814 | *ret = 1; |
| 4815 | return -EINVAL; |
| 4816 | } |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4817 | |
Vitaly Kuznetsov | 7a35e51 | 2020-06-05 13:59:05 +0200 | [diff] [blame] | 4818 | r = kvm_read_guest_virt(vcpu, gva, vmpointer, sizeof(*vmpointer), &e); |
| 4819 | if (r != X86EMUL_CONTINUE) { |
Babu Moger | 3f3393b | 2020-09-11 14:29:05 -0500 | [diff] [blame] | 4820 | *ret = kvm_handle_memory_failure(vcpu, r, &e); |
Vitaly Kuznetsov | 7a35e51 | 2020-06-05 13:59:05 +0200 | [diff] [blame] | 4821 | return -EINVAL; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4822 | } |
| 4823 | |
| 4824 | return 0; |
| 4825 | } |
| 4826 | |
| 4827 | /* |
| 4828 | * Allocate a shadow VMCS and associate it with the currently loaded |
| 4829 | * VMCS, unless such a shadow VMCS already exists. The newly allocated |
| 4830 | * VMCS is also VMCLEARed, so that it is ready for use. |
| 4831 | */ |
| 4832 | static struct vmcs *alloc_shadow_vmcs(struct kvm_vcpu *vcpu) |
| 4833 | { |
| 4834 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 4835 | struct loaded_vmcs *loaded_vmcs = vmx->loaded_vmcs; |
| 4836 | |
| 4837 | /* |
| 4838 | * We should allocate a shadow vmcs for vmcs01 only when L1 |
| 4839 | * executes VMXON and free it when L1 executes VMXOFF. |
| 4840 | * As it is invalid to execute VMXON twice, we shouldn't reach |
| 4841 | * here when vmcs01 already have an allocated shadow vmcs. |
| 4842 | */ |
| 4843 | WARN_ON(loaded_vmcs == &vmx->vmcs01 && loaded_vmcs->shadow_vmcs); |
| 4844 | |
| 4845 | if (!loaded_vmcs->shadow_vmcs) { |
| 4846 | loaded_vmcs->shadow_vmcs = alloc_vmcs(true); |
| 4847 | if (loaded_vmcs->shadow_vmcs) |
| 4848 | vmcs_clear(loaded_vmcs->shadow_vmcs); |
| 4849 | } |
| 4850 | return loaded_vmcs->shadow_vmcs; |
| 4851 | } |
| 4852 | |
| 4853 | static int enter_vmx_operation(struct kvm_vcpu *vcpu) |
| 4854 | { |
| 4855 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 4856 | int r; |
| 4857 | |
| 4858 | r = alloc_loaded_vmcs(&vmx->nested.vmcs02); |
| 4859 | if (r < 0) |
| 4860 | goto out_vmcs02; |
| 4861 | |
Ben Gardon | 4183683 | 2019-02-11 11:02:52 -0800 | [diff] [blame] | 4862 | vmx->nested.cached_vmcs12 = kzalloc(VMCS12_SIZE, GFP_KERNEL_ACCOUNT); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4863 | if (!vmx->nested.cached_vmcs12) |
| 4864 | goto out_cached_vmcs12; |
| 4865 | |
Ben Gardon | 4183683 | 2019-02-11 11:02:52 -0800 | [diff] [blame] | 4866 | vmx->nested.cached_shadow_vmcs12 = kzalloc(VMCS12_SIZE, GFP_KERNEL_ACCOUNT); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4867 | if (!vmx->nested.cached_shadow_vmcs12) |
| 4868 | goto out_cached_shadow_vmcs12; |
| 4869 | |
| 4870 | if (enable_shadow_vmcs && !alloc_shadow_vmcs(vcpu)) |
| 4871 | goto out_shadow_vmcs; |
| 4872 | |
| 4873 | hrtimer_init(&vmx->nested.preemption_timer, CLOCK_MONOTONIC, |
Jim Mattson | ada0098 | 2020-05-08 13:36:42 -0700 | [diff] [blame] | 4874 | HRTIMER_MODE_ABS_PINNED); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4875 | vmx->nested.preemption_timer.function = vmx_preemption_timer_fn; |
| 4876 | |
| 4877 | vmx->nested.vpid02 = allocate_vpid(); |
| 4878 | |
| 4879 | vmx->nested.vmcs02_initialized = false; |
| 4880 | vmx->nested.vmxon = true; |
Luwei Kang | ee85dec | 2018-10-24 16:05:16 +0800 | [diff] [blame] | 4881 | |
Sean Christopherson | 2ef7619 | 2020-03-02 15:56:22 -0800 | [diff] [blame] | 4882 | if (vmx_pt_mode_is_host_guest()) { |
Luwei Kang | ee85dec | 2018-10-24 16:05:16 +0800 | [diff] [blame] | 4883 | vmx->pt_desc.guest.ctl = 0; |
Aaron Lewis | 476c9bd | 2020-09-25 16:34:18 +0200 | [diff] [blame] | 4884 | pt_update_intercept_for_msr(vcpu); |
Luwei Kang | ee85dec | 2018-10-24 16:05:16 +0800 | [diff] [blame] | 4885 | } |
| 4886 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4887 | return 0; |
| 4888 | |
| 4889 | out_shadow_vmcs: |
| 4890 | kfree(vmx->nested.cached_shadow_vmcs12); |
| 4891 | |
| 4892 | out_cached_shadow_vmcs12: |
| 4893 | kfree(vmx->nested.cached_vmcs12); |
| 4894 | |
| 4895 | out_cached_vmcs12: |
| 4896 | free_loaded_vmcs(&vmx->nested.vmcs02); |
| 4897 | |
| 4898 | out_vmcs02: |
| 4899 | return -ENOMEM; |
| 4900 | } |
| 4901 | |
Yu Zhang | ed7023a | 2021-09-09 01:17:31 +0800 | [diff] [blame] | 4902 | /* Emulate the VMXON instruction. */ |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4903 | static int handle_vmon(struct kvm_vcpu *vcpu) |
| 4904 | { |
| 4905 | int ret; |
| 4906 | gpa_t vmptr; |
KarimAllah Ahmed | 2e40893 | 2019-01-31 21:24:31 +0100 | [diff] [blame] | 4907 | uint32_t revision; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4908 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Sean Christopherson | 32ad73d | 2019-12-20 20:44:55 -0800 | [diff] [blame] | 4909 | const u64 VMXON_NEEDED_FEATURES = FEAT_CTL_LOCKED |
| 4910 | | FEAT_CTL_VMX_ENABLED_OUTSIDE_SMX; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4911 | |
| 4912 | /* |
| 4913 | * The Intel VMX Instruction Reference lists a bunch of bits that are |
| 4914 | * prerequisite to running VMXON, most notably cr4.VMXE must be set to |
Sean Christopherson | c2fe3cd | 2020-10-06 18:44:15 -0700 | [diff] [blame] | 4915 | * 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] | 4916 | * Otherwise, we should fail with #UD. But most faulting conditions |
| 4917 | * have already been checked by hardware, prior to the VM-exit for |
| 4918 | * VMXON. We do test guest cr4.VMXE because processor CR4 always has |
| 4919 | * that bit set to 1 in non-root mode. |
| 4920 | */ |
| 4921 | if (!kvm_read_cr4_bits(vcpu, X86_CR4_VMXE)) { |
| 4922 | kvm_queue_exception(vcpu, UD_VECTOR); |
| 4923 | return 1; |
| 4924 | } |
| 4925 | |
| 4926 | /* CPL=0 must be checked manually. */ |
| 4927 | if (vmx_get_cpl(vcpu)) { |
| 4928 | kvm_inject_gp(vcpu, 0); |
| 4929 | return 1; |
| 4930 | } |
| 4931 | |
| 4932 | if (vmx->nested.vmxon) |
Sean Christopherson | b2656e4 | 2020-06-08 18:56:07 -0700 | [diff] [blame] | 4933 | return nested_vmx_fail(vcpu, VMXERR_VMXON_IN_VMX_ROOT_OPERATION); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4934 | |
| 4935 | if ((vmx->msr_ia32_feature_control & VMXON_NEEDED_FEATURES) |
| 4936 | != VMXON_NEEDED_FEATURES) { |
| 4937 | kvm_inject_gp(vcpu, 0); |
| 4938 | return 1; |
| 4939 | } |
| 4940 | |
Vitaly Kuznetsov | 7a35e51 | 2020-06-05 13:59:05 +0200 | [diff] [blame] | 4941 | if (nested_vmx_get_vmptr(vcpu, &vmptr, &ret)) |
| 4942 | return ret; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4943 | |
| 4944 | /* |
| 4945 | * SDM 3: 24.11.5 |
| 4946 | * The first 4 bytes of VMXON region contain the supported |
| 4947 | * VMCS revision identifier |
| 4948 | * |
| 4949 | * Note - IA32_VMX_BASIC[48] will never be 1 for the nested case; |
| 4950 | * which replaces physical address width with 32 |
| 4951 | */ |
KarimAllah Ahmed | e0bf266 | 2019-01-31 21:24:43 +0100 | [diff] [blame] | 4952 | if (!page_address_valid(vcpu, vmptr)) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4953 | return nested_vmx_failInvalid(vcpu); |
| 4954 | |
KarimAllah Ahmed | 2e40893 | 2019-01-31 21:24:31 +0100 | [diff] [blame] | 4955 | if (kvm_read_guest(vcpu->kvm, vmptr, &revision, sizeof(revision)) || |
| 4956 | revision != VMCS12_REVISION) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4957 | return nested_vmx_failInvalid(vcpu); |
| 4958 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4959 | vmx->nested.vmxon_ptr = vmptr; |
| 4960 | ret = enter_vmx_operation(vcpu); |
| 4961 | if (ret) |
| 4962 | return ret; |
| 4963 | |
| 4964 | return nested_vmx_succeed(vcpu); |
| 4965 | } |
| 4966 | |
| 4967 | static inline void nested_release_vmcs12(struct kvm_vcpu *vcpu) |
| 4968 | { |
| 4969 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 4970 | |
| 4971 | if (vmx->nested.current_vmptr == -1ull) |
| 4972 | return; |
| 4973 | |
Sean Christopherson | 7952d76 | 2019-05-07 08:36:29 -0700 | [diff] [blame] | 4974 | copy_vmcs02_to_vmcs12_rare(vcpu, get_vmcs12(vcpu)); |
| 4975 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4976 | if (enable_shadow_vmcs) { |
| 4977 | /* copy to memory all shadowed fields in case |
| 4978 | they were modified */ |
| 4979 | copy_shadow_to_vmcs12(vmx); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4980 | vmx_disable_shadow_vmcs(vmx); |
| 4981 | } |
| 4982 | vmx->nested.posted_intr_nv = -1; |
| 4983 | |
| 4984 | /* Flush VMCS12 to guest memory */ |
| 4985 | kvm_vcpu_write_guest_page(vcpu, |
| 4986 | vmx->nested.current_vmptr >> PAGE_SHIFT, |
| 4987 | vmx->nested.cached_vmcs12, 0, VMCS12_SIZE); |
| 4988 | |
| 4989 | kvm_mmu_free_roots(vcpu, &vcpu->arch.guest_mmu, KVM_MMU_ROOTS_ALL); |
| 4990 | |
| 4991 | vmx->nested.current_vmptr = -1ull; |
| 4992 | } |
| 4993 | |
| 4994 | /* Emulate the VMXOFF instruction */ |
| 4995 | static int handle_vmoff(struct kvm_vcpu *vcpu) |
| 4996 | { |
| 4997 | if (!nested_vmx_check_permission(vcpu)) |
| 4998 | return 1; |
Liran Alon | 4b9852f | 2019-08-26 13:24:49 +0300 | [diff] [blame] | 4999 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5000 | free_nested(vcpu); |
Liran Alon | 4b9852f | 2019-08-26 13:24:49 +0300 | [diff] [blame] | 5001 | |
| 5002 | /* Process a latched INIT during time CPU was in VMX operation */ |
| 5003 | kvm_make_request(KVM_REQ_EVENT, vcpu); |
| 5004 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5005 | return nested_vmx_succeed(vcpu); |
| 5006 | } |
| 5007 | |
| 5008 | /* Emulate the VMCLEAR instruction */ |
| 5009 | static int handle_vmclear(struct kvm_vcpu *vcpu) |
| 5010 | { |
| 5011 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 5012 | u32 zero = 0; |
| 5013 | gpa_t vmptr; |
Vitaly Kuznetsov | 11e3491 | 2019-06-28 13:23:33 +0200 | [diff] [blame] | 5014 | u64 evmcs_gpa; |
Vitaly Kuznetsov | 7a35e51 | 2020-06-05 13:59:05 +0200 | [diff] [blame] | 5015 | int r; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5016 | |
| 5017 | if (!nested_vmx_check_permission(vcpu)) |
| 5018 | return 1; |
| 5019 | |
Vitaly Kuznetsov | 7a35e51 | 2020-06-05 13:59:05 +0200 | [diff] [blame] | 5020 | if (nested_vmx_get_vmptr(vcpu, &vmptr, &r)) |
| 5021 | return r; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5022 | |
KarimAllah Ahmed | e0bf266 | 2019-01-31 21:24:43 +0100 | [diff] [blame] | 5023 | if (!page_address_valid(vcpu, vmptr)) |
Sean Christopherson | b2656e4 | 2020-06-08 18:56:07 -0700 | [diff] [blame] | 5024 | return nested_vmx_fail(vcpu, VMXERR_VMCLEAR_INVALID_ADDRESS); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5025 | |
| 5026 | if (vmptr == vmx->nested.vmxon_ptr) |
Sean Christopherson | b2656e4 | 2020-06-08 18:56:07 -0700 | [diff] [blame] | 5027 | return nested_vmx_fail(vcpu, VMXERR_VMCLEAR_VMXON_POINTER); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5028 | |
Vitaly Kuznetsov | 11e3491 | 2019-06-28 13:23:33 +0200 | [diff] [blame] | 5029 | /* |
| 5030 | * When Enlightened VMEntry is enabled on the calling CPU we treat |
| 5031 | * memory area pointer by vmptr as Enlightened VMCS (as there's no good |
| 5032 | * way to distinguish it from VMCS12) and we must not corrupt it by |
| 5033 | * writing to the non-existent 'launch_state' field. The area doesn't |
| 5034 | * have to be the currently active EVMCS on the calling CPU and there's |
| 5035 | * nothing KVM has to do to transition it from 'active' to 'non-active' |
| 5036 | * state. It is possible that the area will stay mapped as |
| 5037 | * vmx->nested.hv_evmcs but this shouldn't be a problem. |
| 5038 | */ |
| 5039 | if (likely(!vmx->nested.enlightened_vmcs_enabled || |
| 5040 | !nested_enlightened_vmentry(vcpu, &evmcs_gpa))) { |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5041 | if (vmptr == vmx->nested.current_vmptr) |
| 5042 | nested_release_vmcs12(vcpu); |
| 5043 | |
| 5044 | kvm_vcpu_write_guest(vcpu, |
| 5045 | vmptr + offsetof(struct vmcs12, |
| 5046 | launch_state), |
| 5047 | &zero, sizeof(zero)); |
Vitaly Kuznetsov | 3b19b81 | 2021-05-26 15:20:21 +0200 | [diff] [blame] | 5048 | } else if (vmx->nested.hv_evmcs && vmptr == vmx->nested.hv_evmcs_vmptr) { |
| 5049 | nested_release_evmcs(vcpu); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5050 | } |
| 5051 | |
| 5052 | return nested_vmx_succeed(vcpu); |
| 5053 | } |
| 5054 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5055 | /* Emulate the VMLAUNCH instruction */ |
| 5056 | static int handle_vmlaunch(struct kvm_vcpu *vcpu) |
| 5057 | { |
| 5058 | return nested_vmx_run(vcpu, true); |
| 5059 | } |
| 5060 | |
| 5061 | /* Emulate the VMRESUME instruction */ |
| 5062 | static int handle_vmresume(struct kvm_vcpu *vcpu) |
| 5063 | { |
| 5064 | |
| 5065 | return nested_vmx_run(vcpu, false); |
| 5066 | } |
| 5067 | |
| 5068 | static int handle_vmread(struct kvm_vcpu *vcpu) |
| 5069 | { |
Jim Mattson | dd2d604 | 2019-12-06 15:46:35 -0800 | [diff] [blame] | 5070 | struct vmcs12 *vmcs12 = is_guest_mode(vcpu) ? get_shadow_vmcs12(vcpu) |
| 5071 | : get_vmcs12(vcpu); |
Sean Christopherson | 5addc23 | 2020-04-15 13:34:53 -0700 | [diff] [blame] | 5072 | unsigned long exit_qualification = vmx_get_exit_qual(vcpu); |
Jim Mattson | c90f4d0 | 2019-12-06 15:46:37 -0800 | [diff] [blame] | 5073 | u32 instr_info = vmcs_read32(VMX_INSTRUCTION_INFO); |
| 5074 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Paolo Bonzini | f7eea63 | 2019-09-14 00:26:27 +0200 | [diff] [blame] | 5075 | struct x86_exception e; |
Jim Mattson | c90f4d0 | 2019-12-06 15:46:37 -0800 | [diff] [blame] | 5076 | unsigned long field; |
| 5077 | u64 value; |
| 5078 | gva_t gva = 0; |
Sean Christopherson | 1c6f0b4 | 2019-05-07 08:36:25 -0700 | [diff] [blame] | 5079 | short offset; |
Vitaly Kuznetsov | 7a35e51 | 2020-06-05 13:59:05 +0200 | [diff] [blame] | 5080 | int len, r; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5081 | |
| 5082 | if (!nested_vmx_check_permission(vcpu)) |
| 5083 | return 1; |
| 5084 | |
Jim Mattson | dd2d604 | 2019-12-06 15:46:35 -0800 | [diff] [blame] | 5085 | /* |
| 5086 | * In VMX non-root operation, when the VMCS-link pointer is -1ull, |
| 5087 | * any VMREAD sets the ALU flags for VMfailInvalid. |
| 5088 | */ |
| 5089 | if (vmx->nested.current_vmptr == -1ull || |
| 5090 | (is_guest_mode(vcpu) && |
| 5091 | get_vmcs12(vcpu)->vmcs_link_pointer == -1ull)) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5092 | return nested_vmx_failInvalid(vcpu); |
| 5093 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5094 | /* Decode instruction info and find the field to read */ |
Sean Christopherson | 27b4a9c4 | 2021-04-21 19:21:28 -0700 | [diff] [blame] | 5095 | field = kvm_register_read(vcpu, (((instr_info) >> 28) & 0xf)); |
Sean Christopherson | 1c6f0b4 | 2019-05-07 08:36:25 -0700 | [diff] [blame] | 5096 | |
| 5097 | offset = vmcs_field_to_offset(field); |
| 5098 | if (offset < 0) |
Sean Christopherson | b2656e4 | 2020-06-08 18:56:07 -0700 | [diff] [blame] | 5099 | return nested_vmx_fail(vcpu, VMXERR_UNSUPPORTED_VMCS_COMPONENT); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5100 | |
Sean Christopherson | 7952d76 | 2019-05-07 08:36:29 -0700 | [diff] [blame] | 5101 | if (!is_guest_mode(vcpu) && is_vmcs12_ext_field(field)) |
| 5102 | copy_vmcs02_to_vmcs12_rare(vcpu, vmcs12); |
| 5103 | |
Jim Mattson | c90f4d0 | 2019-12-06 15:46:37 -0800 | [diff] [blame] | 5104 | /* Read the field, zero-extended to a u64 value */ |
| 5105 | value = vmcs12_read_any(vmcs12, field, offset); |
Sean Christopherson | 1c6f0b4 | 2019-05-07 08:36:25 -0700 | [diff] [blame] | 5106 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5107 | /* |
| 5108 | * Now copy part of this value to register or memory, as requested. |
| 5109 | * Note that the number of bits actually copied is 32 or 64 depending |
| 5110 | * on the guest's mode (32 or 64 bit), not on the given field's length. |
| 5111 | */ |
Jim Mattson | c90f4d0 | 2019-12-06 15:46:37 -0800 | [diff] [blame] | 5112 | if (instr_info & BIT(10)) { |
Sean Christopherson | 27b4a9c4 | 2021-04-21 19:21:28 -0700 | [diff] [blame] | 5113 | kvm_register_write(vcpu, (((instr_info) >> 3) & 0xf), value); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5114 | } else { |
Eugene Korenevsky | fdb2861 | 2019-06-06 00:19:16 +0300 | [diff] [blame] | 5115 | len = is_64_bit_mode(vcpu) ? 8 : 4; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5116 | if (get_vmx_mem_address(vcpu, exit_qualification, |
Jim Mattson | c90f4d0 | 2019-12-06 15:46:37 -0800 | [diff] [blame] | 5117 | instr_info, true, len, &gva)) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5118 | return 1; |
| 5119 | /* _system ok, nested_vmx_check_permission has verified cpl=0 */ |
Vitaly Kuznetsov | 7a35e51 | 2020-06-05 13:59:05 +0200 | [diff] [blame] | 5120 | r = kvm_write_guest_virt_system(vcpu, gva, &value, len, &e); |
| 5121 | if (r != X86EMUL_CONTINUE) |
Babu Moger | 3f3393b | 2020-09-11 14:29:05 -0500 | [diff] [blame] | 5122 | return kvm_handle_memory_failure(vcpu, r, &e); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5123 | } |
| 5124 | |
| 5125 | return nested_vmx_succeed(vcpu); |
| 5126 | } |
| 5127 | |
Sean Christopherson | e217429 | 2019-05-07 08:36:28 -0700 | [diff] [blame] | 5128 | static bool is_shadow_field_rw(unsigned long field) |
| 5129 | { |
| 5130 | switch (field) { |
| 5131 | #define SHADOW_FIELD_RW(x, y) case x: |
| 5132 | #include "vmcs_shadow_fields.h" |
| 5133 | return true; |
| 5134 | default: |
| 5135 | break; |
| 5136 | } |
| 5137 | return false; |
| 5138 | } |
| 5139 | |
| 5140 | static bool is_shadow_field_ro(unsigned long field) |
| 5141 | { |
| 5142 | switch (field) { |
| 5143 | #define SHADOW_FIELD_RO(x, y) case x: |
| 5144 | #include "vmcs_shadow_fields.h" |
| 5145 | return true; |
| 5146 | default: |
| 5147 | break; |
| 5148 | } |
| 5149 | return false; |
| 5150 | } |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5151 | |
| 5152 | static int handle_vmwrite(struct kvm_vcpu *vcpu) |
| 5153 | { |
Jim Mattson | dd2d604 | 2019-12-06 15:46:35 -0800 | [diff] [blame] | 5154 | struct vmcs12 *vmcs12 = is_guest_mode(vcpu) ? get_shadow_vmcs12(vcpu) |
| 5155 | : get_vmcs12(vcpu); |
Sean Christopherson | 5addc23 | 2020-04-15 13:34:53 -0700 | [diff] [blame] | 5156 | unsigned long exit_qualification = vmx_get_exit_qual(vcpu); |
Jim Mattson | c90f4d0 | 2019-12-06 15:46:37 -0800 | [diff] [blame] | 5157 | u32 instr_info = vmcs_read32(VMX_INSTRUCTION_INFO); |
| 5158 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 5159 | struct x86_exception e; |
| 5160 | unsigned long field; |
Sean Christopherson | 1c6f0b4 | 2019-05-07 08:36:25 -0700 | [diff] [blame] | 5161 | short offset; |
Jim Mattson | c90f4d0 | 2019-12-06 15:46:37 -0800 | [diff] [blame] | 5162 | gva_t gva; |
Vitaly Kuznetsov | 7a35e51 | 2020-06-05 13:59:05 +0200 | [diff] [blame] | 5163 | int len, r; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5164 | |
Jim Mattson | c90f4d0 | 2019-12-06 15:46:37 -0800 | [diff] [blame] | 5165 | /* |
| 5166 | * 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] | 5167 | * mode, and eventually we need to write that into a field of several |
| 5168 | * possible lengths. The code below first zero-extends the value to 64 |
Jim Mattson | c90f4d0 | 2019-12-06 15:46:37 -0800 | [diff] [blame] | 5169 | * bit (value), and then copies only the appropriate number of |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5170 | * bits into the vmcs12 field. |
| 5171 | */ |
Jim Mattson | c90f4d0 | 2019-12-06 15:46:37 -0800 | [diff] [blame] | 5172 | u64 value = 0; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5173 | |
| 5174 | if (!nested_vmx_check_permission(vcpu)) |
| 5175 | return 1; |
| 5176 | |
Jim Mattson | dd2d604 | 2019-12-06 15:46:35 -0800 | [diff] [blame] | 5177 | /* |
| 5178 | * In VMX non-root operation, when the VMCS-link pointer is -1ull, |
| 5179 | * any VMWRITE sets the ALU flags for VMfailInvalid. |
| 5180 | */ |
| 5181 | if (vmx->nested.current_vmptr == -1ull || |
| 5182 | (is_guest_mode(vcpu) && |
| 5183 | get_vmcs12(vcpu)->vmcs_link_pointer == -1ull)) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5184 | return nested_vmx_failInvalid(vcpu); |
| 5185 | |
Jim Mattson | c90f4d0 | 2019-12-06 15:46:37 -0800 | [diff] [blame] | 5186 | if (instr_info & BIT(10)) |
Sean Christopherson | 27b4a9c4 | 2021-04-21 19:21:28 -0700 | [diff] [blame] | 5187 | value = kvm_register_read(vcpu, (((instr_info) >> 3) & 0xf)); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5188 | else { |
Eugene Korenevsky | fdb2861 | 2019-06-06 00:19:16 +0300 | [diff] [blame] | 5189 | len = is_64_bit_mode(vcpu) ? 8 : 4; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5190 | if (get_vmx_mem_address(vcpu, exit_qualification, |
Jim Mattson | c90f4d0 | 2019-12-06 15:46:37 -0800 | [diff] [blame] | 5191 | instr_info, false, len, &gva)) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5192 | return 1; |
Vitaly Kuznetsov | 7a35e51 | 2020-06-05 13:59:05 +0200 | [diff] [blame] | 5193 | r = kvm_read_guest_virt(vcpu, gva, &value, len, &e); |
| 5194 | if (r != X86EMUL_CONTINUE) |
Babu Moger | 3f3393b | 2020-09-11 14:29:05 -0500 | [diff] [blame] | 5195 | return kvm_handle_memory_failure(vcpu, r, &e); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5196 | } |
| 5197 | |
Sean Christopherson | 27b4a9c4 | 2021-04-21 19:21:28 -0700 | [diff] [blame] | 5198 | field = kvm_register_read(vcpu, (((instr_info) >> 28) & 0xf)); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5199 | |
Jim Mattson | 693e02c | 2019-12-06 15:46:36 -0800 | [diff] [blame] | 5200 | offset = vmcs_field_to_offset(field); |
| 5201 | if (offset < 0) |
Sean Christopherson | b2656e4 | 2020-06-08 18:56:07 -0700 | [diff] [blame] | 5202 | return nested_vmx_fail(vcpu, VMXERR_UNSUPPORTED_VMCS_COMPONENT); |
Jim Mattson | 693e02c | 2019-12-06 15:46:36 -0800 | [diff] [blame] | 5203 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5204 | /* |
| 5205 | * If the vCPU supports "VMWRITE to any supported field in the |
| 5206 | * VMCS," then the "read-only" fields are actually read/write. |
| 5207 | */ |
| 5208 | if (vmcs_field_readonly(field) && |
| 5209 | !nested_cpu_has_vmwrite_any_field(vcpu)) |
Sean Christopherson | b2656e4 | 2020-06-08 18:56:07 -0700 | [diff] [blame] | 5210 | return nested_vmx_fail(vcpu, VMXERR_VMWRITE_READ_ONLY_VMCS_COMPONENT); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5211 | |
Jim Mattson | dd2d604 | 2019-12-06 15:46:35 -0800 | [diff] [blame] | 5212 | /* |
| 5213 | * Ensure vmcs12 is up-to-date before any VMWRITE that dirties |
| 5214 | * vmcs12, else we may crush a field or consume a stale value. |
| 5215 | */ |
| 5216 | if (!is_guest_mode(vcpu) && !is_shadow_field_rw(field)) |
| 5217 | copy_vmcs02_to_vmcs12_rare(vcpu, vmcs12); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5218 | |
| 5219 | /* |
Sean Christopherson | b643780 | 2019-05-07 08:36:24 -0700 | [diff] [blame] | 5220 | * Some Intel CPUs intentionally drop the reserved bits of the AR byte |
| 5221 | * fields on VMWRITE. Emulate this behavior to ensure consistent KVM |
| 5222 | * behavior regardless of the underlying hardware, e.g. if an AR_BYTE |
| 5223 | * field is intercepted for VMWRITE but not VMREAD (in L1), then VMREAD |
| 5224 | * from L1 will return a different value than VMREAD from L2 (L1 sees |
| 5225 | * 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] | 5226 | */ |
Sean Christopherson | b643780 | 2019-05-07 08:36:24 -0700 | [diff] [blame] | 5227 | if (field >= GUEST_ES_AR_BYTES && field <= GUEST_TR_AR_BYTES) |
Jim Mattson | c90f4d0 | 2019-12-06 15:46:37 -0800 | [diff] [blame] | 5228 | value &= 0x1f0ff; |
Sean Christopherson | b643780 | 2019-05-07 08:36:24 -0700 | [diff] [blame] | 5229 | |
Jim Mattson | c90f4d0 | 2019-12-06 15:46:37 -0800 | [diff] [blame] | 5230 | vmcs12_write_any(vmcs12, field, offset, value); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5231 | |
| 5232 | /* |
Sean Christopherson | e217429 | 2019-05-07 08:36:28 -0700 | [diff] [blame] | 5233 | * Do not track vmcs12 dirty-state if in guest-mode as we actually |
| 5234 | * dirty shadow vmcs12 instead of vmcs12. Fields that can be updated |
| 5235 | * by L1 without a vmexit are always updated in the vmcs02, i.e. don't |
| 5236 | * "dirty" vmcs12, all others go down the prepare_vmcs02() slow path. |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5237 | */ |
Sean Christopherson | e217429 | 2019-05-07 08:36:28 -0700 | [diff] [blame] | 5238 | if (!is_guest_mode(vcpu) && !is_shadow_field_rw(field)) { |
| 5239 | /* |
| 5240 | * L1 can read these fields without exiting, ensure the |
| 5241 | * shadow VMCS is up-to-date. |
| 5242 | */ |
| 5243 | if (enable_shadow_vmcs && is_shadow_field_ro(field)) { |
| 5244 | preempt_disable(); |
| 5245 | vmcs_load(vmx->vmcs01.shadow_vmcs); |
Sean Christopherson | fadcead | 2019-05-07 08:36:23 -0700 | [diff] [blame] | 5246 | |
Jim Mattson | c90f4d0 | 2019-12-06 15:46:37 -0800 | [diff] [blame] | 5247 | __vmcs_writel(field, value); |
Sean Christopherson | fadcead | 2019-05-07 08:36:23 -0700 | [diff] [blame] | 5248 | |
Sean Christopherson | e217429 | 2019-05-07 08:36:28 -0700 | [diff] [blame] | 5249 | vmcs_clear(vmx->vmcs01.shadow_vmcs); |
| 5250 | vmcs_load(vmx->loaded_vmcs->vmcs); |
| 5251 | preempt_enable(); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5252 | } |
Sean Christopherson | e217429 | 2019-05-07 08:36:28 -0700 | [diff] [blame] | 5253 | vmx->nested.dirty_vmcs12 = true; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5254 | } |
| 5255 | |
| 5256 | return nested_vmx_succeed(vcpu); |
| 5257 | } |
| 5258 | |
| 5259 | static void set_current_vmptr(struct vcpu_vmx *vmx, gpa_t vmptr) |
| 5260 | { |
| 5261 | vmx->nested.current_vmptr = vmptr; |
| 5262 | if (enable_shadow_vmcs) { |
Sean Christopherson | fe7f895d | 2019-05-07 12:17:57 -0700 | [diff] [blame] | 5263 | secondary_exec_controls_setbit(vmx, SECONDARY_EXEC_SHADOW_VMCS); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5264 | vmcs_write64(VMCS_LINK_POINTER, |
| 5265 | __pa(vmx->vmcs01.shadow_vmcs)); |
Sean Christopherson | 3731905ef | 2019-05-07 08:36:27 -0700 | [diff] [blame] | 5266 | vmx->nested.need_vmcs12_to_shadow_sync = true; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5267 | } |
| 5268 | vmx->nested.dirty_vmcs12 = true; |
| 5269 | } |
| 5270 | |
| 5271 | /* Emulate the VMPTRLD instruction */ |
| 5272 | static int handle_vmptrld(struct kvm_vcpu *vcpu) |
| 5273 | { |
| 5274 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 5275 | gpa_t vmptr; |
Vitaly Kuznetsov | 7a35e51 | 2020-06-05 13:59:05 +0200 | [diff] [blame] | 5276 | int r; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5277 | |
| 5278 | if (!nested_vmx_check_permission(vcpu)) |
| 5279 | return 1; |
| 5280 | |
Vitaly Kuznetsov | 7a35e51 | 2020-06-05 13:59:05 +0200 | [diff] [blame] | 5281 | if (nested_vmx_get_vmptr(vcpu, &vmptr, &r)) |
| 5282 | return r; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5283 | |
KarimAllah Ahmed | e0bf266 | 2019-01-31 21:24:43 +0100 | [diff] [blame] | 5284 | if (!page_address_valid(vcpu, vmptr)) |
Sean Christopherson | b2656e4 | 2020-06-08 18:56:07 -0700 | [diff] [blame] | 5285 | return nested_vmx_fail(vcpu, VMXERR_VMPTRLD_INVALID_ADDRESS); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5286 | |
| 5287 | if (vmptr == vmx->nested.vmxon_ptr) |
Sean Christopherson | b2656e4 | 2020-06-08 18:56:07 -0700 | [diff] [blame] | 5288 | return nested_vmx_fail(vcpu, VMXERR_VMPTRLD_VMXON_POINTER); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5289 | |
| 5290 | /* Forbid normal VMPTRLD if Enlightened version was used */ |
Vitaly Kuznetsov | 1e9dfbd | 2021-05-26 15:20:16 +0200 | [diff] [blame] | 5291 | if (evmptr_is_valid(vmx->nested.hv_evmcs_vmptr)) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5292 | return 1; |
| 5293 | |
| 5294 | if (vmx->nested.current_vmptr != vmptr) { |
KarimAllah Ahmed | b146b83 | 2019-01-31 21:24:35 +0100 | [diff] [blame] | 5295 | struct kvm_host_map map; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5296 | struct vmcs12 *new_vmcs12; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5297 | |
KarimAllah Ahmed | b146b83 | 2019-01-31 21:24:35 +0100 | [diff] [blame] | 5298 | if (kvm_vcpu_map(vcpu, gpa_to_gfn(vmptr), &map)) { |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5299 | /* |
| 5300 | * Reads from an unbacked page return all 1s, |
| 5301 | * which means that the 32 bits located at the |
| 5302 | * given physical address won't match the required |
| 5303 | * VMCS12_REVISION identifier. |
| 5304 | */ |
Sean Christopherson | b2656e4 | 2020-06-08 18:56:07 -0700 | [diff] [blame] | 5305 | return nested_vmx_fail(vcpu, |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5306 | VMXERR_VMPTRLD_INCORRECT_VMCS_REVISION_ID); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5307 | } |
KarimAllah Ahmed | b146b83 | 2019-01-31 21:24:35 +0100 | [diff] [blame] | 5308 | |
| 5309 | new_vmcs12 = map.hva; |
| 5310 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5311 | if (new_vmcs12->hdr.revision_id != VMCS12_REVISION || |
| 5312 | (new_vmcs12->hdr.shadow_vmcs && |
| 5313 | !nested_cpu_has_vmx_shadow_vmcs(vcpu))) { |
KarimAllah Ahmed | b146b83 | 2019-01-31 21:24:35 +0100 | [diff] [blame] | 5314 | kvm_vcpu_unmap(vcpu, &map, false); |
Sean Christopherson | b2656e4 | 2020-06-08 18:56:07 -0700 | [diff] [blame] | 5315 | return nested_vmx_fail(vcpu, |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5316 | VMXERR_VMPTRLD_INCORRECT_VMCS_REVISION_ID); |
| 5317 | } |
| 5318 | |
| 5319 | nested_release_vmcs12(vcpu); |
| 5320 | |
| 5321 | /* |
| 5322 | * Load VMCS12 from guest memory since it is not already |
| 5323 | * cached. |
| 5324 | */ |
| 5325 | memcpy(vmx->nested.cached_vmcs12, new_vmcs12, VMCS12_SIZE); |
KarimAllah Ahmed | b146b83 | 2019-01-31 21:24:35 +0100 | [diff] [blame] | 5326 | kvm_vcpu_unmap(vcpu, &map, false); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5327 | |
| 5328 | set_current_vmptr(vmx, vmptr); |
| 5329 | } |
| 5330 | |
| 5331 | return nested_vmx_succeed(vcpu); |
| 5332 | } |
| 5333 | |
| 5334 | /* Emulate the VMPTRST instruction */ |
| 5335 | static int handle_vmptrst(struct kvm_vcpu *vcpu) |
| 5336 | { |
Sean Christopherson | 5addc23 | 2020-04-15 13:34:53 -0700 | [diff] [blame] | 5337 | unsigned long exit_qual = vmx_get_exit_qual(vcpu); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5338 | u32 instr_info = vmcs_read32(VMX_INSTRUCTION_INFO); |
| 5339 | gpa_t current_vmptr = to_vmx(vcpu)->nested.current_vmptr; |
| 5340 | struct x86_exception e; |
| 5341 | gva_t gva; |
Vitaly Kuznetsov | 7a35e51 | 2020-06-05 13:59:05 +0200 | [diff] [blame] | 5342 | int r; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5343 | |
| 5344 | if (!nested_vmx_check_permission(vcpu)) |
| 5345 | return 1; |
| 5346 | |
Vitaly Kuznetsov | 1e9dfbd | 2021-05-26 15:20:16 +0200 | [diff] [blame] | 5347 | if (unlikely(evmptr_is_valid(to_vmx(vcpu)->nested.hv_evmcs_vmptr))) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5348 | return 1; |
| 5349 | |
Eugene Korenevsky | fdb2861 | 2019-06-06 00:19:16 +0300 | [diff] [blame] | 5350 | if (get_vmx_mem_address(vcpu, exit_qual, instr_info, |
| 5351 | true, sizeof(gpa_t), &gva)) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5352 | return 1; |
| 5353 | /* *_system ok, nested_vmx_check_permission has verified cpl=0 */ |
Vitaly Kuznetsov | 7a35e51 | 2020-06-05 13:59:05 +0200 | [diff] [blame] | 5354 | r = kvm_write_guest_virt_system(vcpu, gva, (void *)¤t_vmptr, |
| 5355 | sizeof(gpa_t), &e); |
| 5356 | if (r != X86EMUL_CONTINUE) |
Babu Moger | 3f3393b | 2020-09-11 14:29:05 -0500 | [diff] [blame] | 5357 | return kvm_handle_memory_failure(vcpu, r, &e); |
Vitaly Kuznetsov | 7a35e51 | 2020-06-05 13:59:05 +0200 | [diff] [blame] | 5358 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5359 | return nested_vmx_succeed(vcpu); |
| 5360 | } |
| 5361 | |
| 5362 | /* Emulate the INVEPT instruction */ |
| 5363 | static int handle_invept(struct kvm_vcpu *vcpu) |
| 5364 | { |
| 5365 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 5366 | u32 vmx_instruction_info, types; |
Sean Christopherson | ce8fe7b | 2020-03-20 14:28:31 -0700 | [diff] [blame] | 5367 | unsigned long type, roots_to_free; |
| 5368 | struct kvm_mmu *mmu; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5369 | gva_t gva; |
| 5370 | struct x86_exception e; |
| 5371 | struct { |
| 5372 | u64 eptp, gpa; |
| 5373 | } operand; |
Vitaly Kuznetsov | 7a35e51 | 2020-06-05 13:59:05 +0200 | [diff] [blame] | 5374 | int i, r; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5375 | |
| 5376 | if (!(vmx->nested.msrs.secondary_ctls_high & |
| 5377 | SECONDARY_EXEC_ENABLE_EPT) || |
| 5378 | !(vmx->nested.msrs.ept_caps & VMX_EPT_INVEPT_BIT)) { |
| 5379 | kvm_queue_exception(vcpu, UD_VECTOR); |
| 5380 | return 1; |
| 5381 | } |
| 5382 | |
| 5383 | if (!nested_vmx_check_permission(vcpu)) |
| 5384 | return 1; |
| 5385 | |
| 5386 | vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO); |
Sean Christopherson | 27b4a9c4 | 2021-04-21 19:21:28 -0700 | [diff] [blame] | 5387 | type = kvm_register_read(vcpu, (vmx_instruction_info >> 28) & 0xf); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5388 | |
| 5389 | types = (vmx->nested.msrs.ept_caps >> VMX_EPT_EXTENT_SHIFT) & 6; |
| 5390 | |
| 5391 | if (type >= 32 || !(types & (1 << type))) |
Sean Christopherson | b2656e4 | 2020-06-08 18:56:07 -0700 | [diff] [blame] | 5392 | return nested_vmx_fail(vcpu, VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5393 | |
| 5394 | /* According to the Intel VMX instruction reference, the memory |
| 5395 | * operand is read even if it isn't needed (e.g., for type==global) |
| 5396 | */ |
Sean Christopherson | 5addc23 | 2020-04-15 13:34:53 -0700 | [diff] [blame] | 5397 | if (get_vmx_mem_address(vcpu, vmx_get_exit_qual(vcpu), |
Eugene Korenevsky | fdb2861 | 2019-06-06 00:19:16 +0300 | [diff] [blame] | 5398 | vmx_instruction_info, false, sizeof(operand), &gva)) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5399 | return 1; |
Vitaly Kuznetsov | 7a35e51 | 2020-06-05 13:59:05 +0200 | [diff] [blame] | 5400 | r = kvm_read_guest_virt(vcpu, gva, &operand, sizeof(operand), &e); |
| 5401 | if (r != X86EMUL_CONTINUE) |
Babu Moger | 3f3393b | 2020-09-11 14:29:05 -0500 | [diff] [blame] | 5402 | return kvm_handle_memory_failure(vcpu, r, &e); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5403 | |
Sean Christopherson | ce8fe7b | 2020-03-20 14:28:31 -0700 | [diff] [blame] | 5404 | /* |
| 5405 | * Nested EPT roots are always held through guest_mmu, |
| 5406 | * not root_mmu. |
| 5407 | */ |
| 5408 | mmu = &vcpu->arch.guest_mmu; |
| 5409 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5410 | switch (type) { |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5411 | case VMX_EPT_EXTENT_CONTEXT: |
Sean Christopherson | eed0030 | 2020-03-20 14:27:58 -0700 | [diff] [blame] | 5412 | if (!nested_vmx_check_eptp(vcpu, operand.eptp)) |
Sean Christopherson | b2656e4 | 2020-06-08 18:56:07 -0700 | [diff] [blame] | 5413 | return nested_vmx_fail(vcpu, |
Sean Christopherson | eed0030 | 2020-03-20 14:27:58 -0700 | [diff] [blame] | 5414 | VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID); |
Sean Christopherson | f8aa7e3 | 2020-03-20 14:27:59 -0700 | [diff] [blame] | 5415 | |
Sean Christopherson | ce8fe7b | 2020-03-20 14:28:31 -0700 | [diff] [blame] | 5416 | roots_to_free = 0; |
Sean Christopherson | be01e8e | 2020-03-20 14:28:32 -0700 | [diff] [blame] | 5417 | if (nested_ept_root_matches(mmu->root_hpa, mmu->root_pgd, |
Sean Christopherson | ce8fe7b | 2020-03-20 14:28:31 -0700 | [diff] [blame] | 5418 | operand.eptp)) |
| 5419 | roots_to_free |= KVM_MMU_ROOT_CURRENT; |
| 5420 | |
| 5421 | for (i = 0; i < KVM_MMU_NUM_PREV_ROOTS; i++) { |
| 5422 | if (nested_ept_root_matches(mmu->prev_roots[i].hpa, |
Sean Christopherson | be01e8e | 2020-03-20 14:28:32 -0700 | [diff] [blame] | 5423 | mmu->prev_roots[i].pgd, |
Sean Christopherson | ce8fe7b | 2020-03-20 14:28:31 -0700 | [diff] [blame] | 5424 | operand.eptp)) |
| 5425 | roots_to_free |= KVM_MMU_ROOT_PREVIOUS(i); |
| 5426 | } |
| 5427 | break; |
Sean Christopherson | eed0030 | 2020-03-20 14:27:58 -0700 | [diff] [blame] | 5428 | case VMX_EPT_EXTENT_GLOBAL: |
Sean Christopherson | ce8fe7b | 2020-03-20 14:28:31 -0700 | [diff] [blame] | 5429 | roots_to_free = KVM_MMU_ROOTS_ALL; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5430 | break; |
| 5431 | default: |
Sean Christopherson | f9336e3 | 2020-05-04 08:35:06 -0700 | [diff] [blame] | 5432 | BUG(); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5433 | break; |
| 5434 | } |
| 5435 | |
Sean Christopherson | ce8fe7b | 2020-03-20 14:28:31 -0700 | [diff] [blame] | 5436 | if (roots_to_free) |
| 5437 | kvm_mmu_free_roots(vcpu, mmu, roots_to_free); |
| 5438 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5439 | return nested_vmx_succeed(vcpu); |
| 5440 | } |
| 5441 | |
| 5442 | static int handle_invvpid(struct kvm_vcpu *vcpu) |
| 5443 | { |
| 5444 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 5445 | u32 vmx_instruction_info; |
| 5446 | unsigned long type, types; |
| 5447 | gva_t gva; |
| 5448 | struct x86_exception e; |
| 5449 | struct { |
| 5450 | u64 vpid; |
| 5451 | u64 gla; |
| 5452 | } operand; |
| 5453 | u16 vpid02; |
Vitaly Kuznetsov | 7a35e51 | 2020-06-05 13:59:05 +0200 | [diff] [blame] | 5454 | int r; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5455 | |
| 5456 | if (!(vmx->nested.msrs.secondary_ctls_high & |
| 5457 | SECONDARY_EXEC_ENABLE_VPID) || |
| 5458 | !(vmx->nested.msrs.vpid_caps & VMX_VPID_INVVPID_BIT)) { |
| 5459 | kvm_queue_exception(vcpu, UD_VECTOR); |
| 5460 | return 1; |
| 5461 | } |
| 5462 | |
| 5463 | if (!nested_vmx_check_permission(vcpu)) |
| 5464 | return 1; |
| 5465 | |
| 5466 | vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO); |
Sean Christopherson | 27b4a9c4 | 2021-04-21 19:21:28 -0700 | [diff] [blame] | 5467 | type = kvm_register_read(vcpu, (vmx_instruction_info >> 28) & 0xf); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5468 | |
| 5469 | types = (vmx->nested.msrs.vpid_caps & |
| 5470 | VMX_VPID_EXTENT_SUPPORTED_MASK) >> 8; |
| 5471 | |
| 5472 | if (type >= 32 || !(types & (1 << type))) |
Sean Christopherson | b2656e4 | 2020-06-08 18:56:07 -0700 | [diff] [blame] | 5473 | return nested_vmx_fail(vcpu, |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5474 | VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID); |
| 5475 | |
| 5476 | /* according to the intel vmx instruction reference, the memory |
| 5477 | * operand is read even if it isn't needed (e.g., for type==global) |
| 5478 | */ |
Sean Christopherson | 5addc23 | 2020-04-15 13:34:53 -0700 | [diff] [blame] | 5479 | if (get_vmx_mem_address(vcpu, vmx_get_exit_qual(vcpu), |
Eugene Korenevsky | fdb2861 | 2019-06-06 00:19:16 +0300 | [diff] [blame] | 5480 | vmx_instruction_info, false, sizeof(operand), &gva)) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5481 | return 1; |
Vitaly Kuznetsov | 7a35e51 | 2020-06-05 13:59:05 +0200 | [diff] [blame] | 5482 | r = kvm_read_guest_virt(vcpu, gva, &operand, sizeof(operand), &e); |
| 5483 | if (r != X86EMUL_CONTINUE) |
Babu Moger | 3f3393b | 2020-09-11 14:29:05 -0500 | [diff] [blame] | 5484 | return kvm_handle_memory_failure(vcpu, r, &e); |
Vitaly Kuznetsov | 7a35e51 | 2020-06-05 13:59:05 +0200 | [diff] [blame] | 5485 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5486 | if (operand.vpid >> 16) |
Sean Christopherson | b2656e4 | 2020-06-08 18:56:07 -0700 | [diff] [blame] | 5487 | return nested_vmx_fail(vcpu, |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5488 | VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID); |
| 5489 | |
| 5490 | vpid02 = nested_get_vpid02(vcpu); |
| 5491 | switch (type) { |
| 5492 | case VMX_VPID_EXTENT_INDIVIDUAL_ADDR: |
| 5493 | if (!operand.vpid || |
| 5494 | is_noncanonical_address(operand.gla, vcpu)) |
Sean Christopherson | b2656e4 | 2020-06-08 18:56:07 -0700 | [diff] [blame] | 5495 | return nested_vmx_fail(vcpu, |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5496 | VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID); |
Sean Christopherson | bc41d0c | 2020-03-20 14:28:09 -0700 | [diff] [blame] | 5497 | vpid_sync_vcpu_addr(vpid02, operand.gla); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5498 | break; |
| 5499 | case VMX_VPID_EXTENT_SINGLE_CONTEXT: |
| 5500 | case VMX_VPID_EXTENT_SINGLE_NON_GLOBAL: |
| 5501 | if (!operand.vpid) |
Sean Christopherson | b2656e4 | 2020-06-08 18:56:07 -0700 | [diff] [blame] | 5502 | return nested_vmx_fail(vcpu, |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5503 | VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID); |
Sean Christopherson | 446ace4 | 2020-03-20 14:28:05 -0700 | [diff] [blame] | 5504 | vpid_sync_context(vpid02); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5505 | break; |
| 5506 | case VMX_VPID_EXTENT_ALL_CONTEXT: |
Sean Christopherson | 446ace4 | 2020-03-20 14:28:05 -0700 | [diff] [blame] | 5507 | vpid_sync_context(vpid02); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5508 | break; |
| 5509 | default: |
| 5510 | WARN_ON_ONCE(1); |
| 5511 | return kvm_skip_emulated_instruction(vcpu); |
| 5512 | } |
| 5513 | |
Junaid Shahid | d6e3f83 | 2020-03-20 14:28:00 -0700 | [diff] [blame] | 5514 | /* |
| 5515 | * Sync the shadow page tables if EPT is disabled, L1 is invalidating |
Sean Christopherson | 25b62c6 | 2021-06-09 16:42:29 -0700 | [diff] [blame] | 5516 | * linear mappings for L2 (tagged with L2's VPID). Free all guest |
| 5517 | * roots as VPIDs are not tracked in the MMU role. |
Junaid Shahid | d6e3f83 | 2020-03-20 14:28:00 -0700 | [diff] [blame] | 5518 | * |
| 5519 | * Note, this operates on root_mmu, not guest_mmu, as L1 and L2 share |
| 5520 | * an MMU when EPT is disabled. |
| 5521 | * |
| 5522 | * TODO: sync only the affected SPTEs for INVDIVIDUAL_ADDR. |
| 5523 | */ |
| 5524 | if (!enable_ept) |
Sean Christopherson | 25b62c6 | 2021-06-09 16:42:29 -0700 | [diff] [blame] | 5525 | kvm_mmu_free_guest_mode_roots(vcpu, &vcpu->arch.root_mmu); |
Junaid Shahid | d6e3f83 | 2020-03-20 14:28:00 -0700 | [diff] [blame] | 5526 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5527 | return nested_vmx_succeed(vcpu); |
| 5528 | } |
| 5529 | |
| 5530 | static int nested_vmx_eptp_switching(struct kvm_vcpu *vcpu, |
| 5531 | struct vmcs12 *vmcs12) |
| 5532 | { |
Sean Christopherson | 2b3eaf8 | 2019-04-30 10:36:19 -0700 | [diff] [blame] | 5533 | u32 index = kvm_rcx_read(vcpu); |
Sean Christopherson | ac6389a | 2020-03-02 18:02:38 -0800 | [diff] [blame] | 5534 | u64 new_eptp; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5535 | |
Sean Christopherson | c5ffd40 | 2021-06-09 16:42:35 -0700 | [diff] [blame] | 5536 | if (WARN_ON_ONCE(!nested_cpu_has_ept(vmcs12))) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5537 | return 1; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5538 | if (index >= VMFUNC_EPTP_ENTRIES) |
| 5539 | return 1; |
| 5540 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5541 | 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] | 5542 | &new_eptp, index * 8, 8)) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5543 | return 1; |
| 5544 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5545 | /* |
| 5546 | * If the (L2) guest does a vmfunc to the currently |
| 5547 | * active ept pointer, we don't have to do anything else |
| 5548 | */ |
Sean Christopherson | ac6389a | 2020-03-02 18:02:38 -0800 | [diff] [blame] | 5549 | if (vmcs12->ept_pointer != new_eptp) { |
| 5550 | if (!nested_vmx_check_eptp(vcpu, new_eptp)) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5551 | return 1; |
| 5552 | |
Sean Christopherson | ac6389a | 2020-03-02 18:02:38 -0800 | [diff] [blame] | 5553 | vmcs12->ept_pointer = new_eptp; |
Sean Christopherson | 39353ab | 2021-06-09 16:42:31 -0700 | [diff] [blame] | 5554 | nested_ept_new_eptp(vcpu); |
Sean Christopherson | c805f5d | 2021-03-04 17:10:57 -0800 | [diff] [blame] | 5555 | |
Sean Christopherson | 39353ab | 2021-06-09 16:42:31 -0700 | [diff] [blame] | 5556 | if (!nested_cpu_has_vpid(vmcs12)) |
| 5557 | kvm_make_request(KVM_REQ_TLB_FLUSH_GUEST, vcpu); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5558 | } |
| 5559 | |
| 5560 | return 0; |
| 5561 | } |
| 5562 | |
| 5563 | static int handle_vmfunc(struct kvm_vcpu *vcpu) |
| 5564 | { |
| 5565 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 5566 | struct vmcs12 *vmcs12; |
Sean Christopherson | 2b3eaf8 | 2019-04-30 10:36:19 -0700 | [diff] [blame] | 5567 | u32 function = kvm_rax_read(vcpu); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5568 | |
| 5569 | /* |
| 5570 | * VMFUNC is only supported for nested guests, but we always enable the |
| 5571 | * secondary control for simplicity; for non-nested mode, fake that we |
| 5572 | * didn't by injecting #UD. |
| 5573 | */ |
| 5574 | if (!is_guest_mode(vcpu)) { |
| 5575 | kvm_queue_exception(vcpu, UD_VECTOR); |
| 5576 | return 1; |
| 5577 | } |
| 5578 | |
| 5579 | vmcs12 = get_vmcs12(vcpu); |
Sean Christopherson | 546e839 | 2021-06-09 16:42:34 -0700 | [diff] [blame] | 5580 | |
| 5581 | /* |
| 5582 | * #UD on out-of-bounds function has priority over VM-Exit, and VMFUNC |
| 5583 | * is enabled in vmcs02 if and only if it's enabled in vmcs12. |
| 5584 | */ |
| 5585 | if (WARN_ON_ONCE((function > 63) || !nested_cpu_has_vmfunc(vmcs12))) { |
| 5586 | kvm_queue_exception(vcpu, UD_VECTOR); |
| 5587 | return 1; |
| 5588 | } |
| 5589 | |
Sean Christopherson | 0e75225 | 2021-06-09 16:42:22 -0700 | [diff] [blame] | 5590 | if (!(vmcs12->vm_function_control & BIT_ULL(function))) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5591 | goto fail; |
| 5592 | |
| 5593 | switch (function) { |
| 5594 | case 0: |
| 5595 | if (nested_vmx_eptp_switching(vcpu, vmcs12)) |
| 5596 | goto fail; |
| 5597 | break; |
| 5598 | default: |
| 5599 | goto fail; |
| 5600 | } |
| 5601 | return kvm_skip_emulated_instruction(vcpu); |
| 5602 | |
| 5603 | fail: |
Sean Christopherson | 8e53324 | 2020-11-06 17:03:12 +0800 | [diff] [blame] | 5604 | /* |
| 5605 | * This is effectively a reflected VM-Exit, as opposed to a synthesized |
| 5606 | * nested VM-Exit. Pass the original exit reason, i.e. don't hardcode |
| 5607 | * EXIT_REASON_VMFUNC as the exit reason. |
| 5608 | */ |
| 5609 | nested_vmx_vmexit(vcpu, vmx->exit_reason.full, |
Sean Christopherson | 8791585 | 2020-04-15 13:34:54 -0700 | [diff] [blame] | 5610 | vmx_get_intr_info(vcpu), |
Sean Christopherson | 5addc23 | 2020-04-15 13:34:53 -0700 | [diff] [blame] | 5611 | vmx_get_exit_qual(vcpu)); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5612 | return 1; |
| 5613 | } |
| 5614 | |
Oliver Upton | e71237d | 2020-02-04 15:26:30 -0800 | [diff] [blame] | 5615 | /* |
| 5616 | * Return true if an IO instruction with the specified port and size should cause |
| 5617 | * a VM-exit into L1. |
| 5618 | */ |
| 5619 | bool nested_vmx_check_io_bitmaps(struct kvm_vcpu *vcpu, unsigned int port, |
| 5620 | int size) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5621 | { |
Oliver Upton | e71237d | 2020-02-04 15:26:30 -0800 | [diff] [blame] | 5622 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5623 | gpa_t bitmap, last_bitmap; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5624 | u8 b; |
| 5625 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5626 | last_bitmap = (gpa_t)-1; |
| 5627 | b = -1; |
| 5628 | |
| 5629 | while (size > 0) { |
| 5630 | if (port < 0x8000) |
| 5631 | bitmap = vmcs12->io_bitmap_a; |
| 5632 | else if (port < 0x10000) |
| 5633 | bitmap = vmcs12->io_bitmap_b; |
| 5634 | else |
| 5635 | return true; |
| 5636 | bitmap += (port & 0x7fff) / 8; |
| 5637 | |
| 5638 | if (last_bitmap != bitmap) |
| 5639 | if (kvm_vcpu_read_guest(vcpu, bitmap, &b, 1)) |
| 5640 | return true; |
| 5641 | if (b & (1 << (port & 7))) |
| 5642 | return true; |
| 5643 | |
| 5644 | port++; |
| 5645 | size--; |
| 5646 | last_bitmap = bitmap; |
| 5647 | } |
| 5648 | |
| 5649 | return false; |
| 5650 | } |
| 5651 | |
Oliver Upton | e71237d | 2020-02-04 15:26:30 -0800 | [diff] [blame] | 5652 | static bool nested_vmx_exit_handled_io(struct kvm_vcpu *vcpu, |
| 5653 | struct vmcs12 *vmcs12) |
| 5654 | { |
| 5655 | unsigned long exit_qualification; |
Oliver Upton | 35a5713 | 2020-02-04 15:26:31 -0800 | [diff] [blame] | 5656 | unsigned short port; |
Oliver Upton | e71237d | 2020-02-04 15:26:30 -0800 | [diff] [blame] | 5657 | int size; |
| 5658 | |
| 5659 | if (!nested_cpu_has(vmcs12, CPU_BASED_USE_IO_BITMAPS)) |
| 5660 | return nested_cpu_has(vmcs12, CPU_BASED_UNCOND_IO_EXITING); |
| 5661 | |
Sean Christopherson | 5addc23 | 2020-04-15 13:34:53 -0700 | [diff] [blame] | 5662 | exit_qualification = vmx_get_exit_qual(vcpu); |
Oliver Upton | e71237d | 2020-02-04 15:26:30 -0800 | [diff] [blame] | 5663 | |
| 5664 | port = exit_qualification >> 16; |
| 5665 | size = (exit_qualification & 7) + 1; |
| 5666 | |
| 5667 | return nested_vmx_check_io_bitmaps(vcpu, port, size); |
| 5668 | } |
| 5669 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5670 | /* |
Miaohe Lin | 463bfee | 2020-02-14 10:44:05 +0800 | [diff] [blame] | 5671 | * 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] | 5672 | * rather than handle it ourselves in L0. I.e., check whether L1 expressed |
| 5673 | * disinterest in the current event (read or write a specific MSR) by using an |
| 5674 | * MSR bitmap. This may be the case even when L0 doesn't use MSR bitmaps. |
| 5675 | */ |
| 5676 | static bool nested_vmx_exit_handled_msr(struct kvm_vcpu *vcpu, |
Sean Christopherson | 8e53324 | 2020-11-06 17:03:12 +0800 | [diff] [blame] | 5677 | struct vmcs12 *vmcs12, |
| 5678 | union vmx_exit_reason exit_reason) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5679 | { |
Sean Christopherson | 2b3eaf8 | 2019-04-30 10:36:19 -0700 | [diff] [blame] | 5680 | u32 msr_index = kvm_rcx_read(vcpu); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5681 | gpa_t bitmap; |
| 5682 | |
| 5683 | if (!nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS)) |
| 5684 | return true; |
| 5685 | |
| 5686 | /* |
| 5687 | * The MSR_BITMAP page is divided into four 1024-byte bitmaps, |
| 5688 | * for the four combinations of read/write and low/high MSR numbers. |
| 5689 | * First we need to figure out which of the four to use: |
| 5690 | */ |
| 5691 | bitmap = vmcs12->msr_bitmap; |
Sean Christopherson | 8e53324 | 2020-11-06 17:03:12 +0800 | [diff] [blame] | 5692 | if (exit_reason.basic == EXIT_REASON_MSR_WRITE) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5693 | bitmap += 2048; |
| 5694 | if (msr_index >= 0xc0000000) { |
| 5695 | msr_index -= 0xc0000000; |
| 5696 | bitmap += 1024; |
| 5697 | } |
| 5698 | |
| 5699 | /* Then read the msr_index'th bit from this bitmap: */ |
| 5700 | if (msr_index < 1024*8) { |
| 5701 | unsigned char b; |
| 5702 | if (kvm_vcpu_read_guest(vcpu, bitmap + msr_index/8, &b, 1)) |
| 5703 | return true; |
| 5704 | return 1 & (b >> (msr_index & 7)); |
| 5705 | } else |
| 5706 | return true; /* let L1 handle the wrong parameter */ |
| 5707 | } |
| 5708 | |
| 5709 | /* |
| 5710 | * Return 1 if we should exit from L2 to L1 to handle a CR access exit, |
| 5711 | * rather than handle it ourselves in L0. I.e., check if L1 wanted to |
| 5712 | * intercept (via guest_host_mask etc.) the current event. |
| 5713 | */ |
| 5714 | static bool nested_vmx_exit_handled_cr(struct kvm_vcpu *vcpu, |
| 5715 | struct vmcs12 *vmcs12) |
| 5716 | { |
Sean Christopherson | 5addc23 | 2020-04-15 13:34:53 -0700 | [diff] [blame] | 5717 | unsigned long exit_qualification = vmx_get_exit_qual(vcpu); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5718 | int cr = exit_qualification & 15; |
| 5719 | int reg; |
| 5720 | unsigned long val; |
| 5721 | |
| 5722 | switch ((exit_qualification >> 4) & 3) { |
| 5723 | case 0: /* mov to cr */ |
| 5724 | reg = (exit_qualification >> 8) & 15; |
Sean Christopherson | 27b4a9c4 | 2021-04-21 19:21:28 -0700 | [diff] [blame] | 5725 | val = kvm_register_read(vcpu, reg); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5726 | switch (cr) { |
| 5727 | case 0: |
| 5728 | if (vmcs12->cr0_guest_host_mask & |
| 5729 | (val ^ vmcs12->cr0_read_shadow)) |
| 5730 | return true; |
| 5731 | break; |
| 5732 | case 3: |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5733 | if (nested_cpu_has(vmcs12, CPU_BASED_CR3_LOAD_EXITING)) |
| 5734 | return true; |
| 5735 | break; |
| 5736 | case 4: |
| 5737 | if (vmcs12->cr4_guest_host_mask & |
| 5738 | (vmcs12->cr4_read_shadow ^ val)) |
| 5739 | return true; |
| 5740 | break; |
| 5741 | case 8: |
| 5742 | if (nested_cpu_has(vmcs12, CPU_BASED_CR8_LOAD_EXITING)) |
| 5743 | return true; |
| 5744 | break; |
| 5745 | } |
| 5746 | break; |
| 5747 | case 2: /* clts */ |
| 5748 | if ((vmcs12->cr0_guest_host_mask & X86_CR0_TS) && |
| 5749 | (vmcs12->cr0_read_shadow & X86_CR0_TS)) |
| 5750 | return true; |
| 5751 | break; |
| 5752 | case 1: /* mov from cr */ |
| 5753 | switch (cr) { |
| 5754 | case 3: |
| 5755 | if (vmcs12->cpu_based_vm_exec_control & |
| 5756 | CPU_BASED_CR3_STORE_EXITING) |
| 5757 | return true; |
| 5758 | break; |
| 5759 | case 8: |
| 5760 | if (vmcs12->cpu_based_vm_exec_control & |
| 5761 | CPU_BASED_CR8_STORE_EXITING) |
| 5762 | return true; |
| 5763 | break; |
| 5764 | } |
| 5765 | break; |
| 5766 | case 3: /* lmsw */ |
| 5767 | /* |
| 5768 | * lmsw can change bits 1..3 of cr0, and only set bit 0 of |
| 5769 | * cr0. Other attempted changes are ignored, with no exit. |
| 5770 | */ |
| 5771 | val = (exit_qualification >> LMSW_SOURCE_DATA_SHIFT) & 0x0f; |
| 5772 | if (vmcs12->cr0_guest_host_mask & 0xe & |
| 5773 | (val ^ vmcs12->cr0_read_shadow)) |
| 5774 | return true; |
| 5775 | if ((vmcs12->cr0_guest_host_mask & 0x1) && |
| 5776 | !(vmcs12->cr0_read_shadow & 0x1) && |
| 5777 | (val & 0x1)) |
| 5778 | return true; |
| 5779 | break; |
| 5780 | } |
| 5781 | return false; |
| 5782 | } |
| 5783 | |
Sean Christopherson | 72add91 | 2021-04-12 16:21:42 +1200 | [diff] [blame] | 5784 | static bool nested_vmx_exit_handled_encls(struct kvm_vcpu *vcpu, |
| 5785 | struct vmcs12 *vmcs12) |
| 5786 | { |
| 5787 | u32 encls_leaf; |
| 5788 | |
| 5789 | if (!guest_cpuid_has(vcpu, X86_FEATURE_SGX) || |
| 5790 | !nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENCLS_EXITING)) |
| 5791 | return false; |
| 5792 | |
| 5793 | encls_leaf = kvm_rax_read(vcpu); |
| 5794 | if (encls_leaf > 62) |
| 5795 | encls_leaf = 63; |
| 5796 | return vmcs12->encls_exiting_bitmap & BIT_ULL(encls_leaf); |
| 5797 | } |
| 5798 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5799 | static bool nested_vmx_exit_handled_vmcs_access(struct kvm_vcpu *vcpu, |
| 5800 | struct vmcs12 *vmcs12, gpa_t bitmap) |
| 5801 | { |
| 5802 | u32 vmx_instruction_info; |
| 5803 | unsigned long field; |
| 5804 | u8 b; |
| 5805 | |
| 5806 | if (!nested_cpu_has_shadow_vmcs(vmcs12)) |
| 5807 | return true; |
| 5808 | |
| 5809 | /* Decode instruction info and find the field to access */ |
| 5810 | vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO); |
| 5811 | field = kvm_register_read(vcpu, (((vmx_instruction_info) >> 28) & 0xf)); |
| 5812 | |
| 5813 | /* Out-of-range fields always cause a VM exit from L2 to L1 */ |
| 5814 | if (field >> 15) |
| 5815 | return true; |
| 5816 | |
| 5817 | if (kvm_vcpu_read_guest(vcpu, bitmap + field/8, &b, 1)) |
| 5818 | return true; |
| 5819 | |
| 5820 | return 1 & (b >> (field & 7)); |
| 5821 | } |
| 5822 | |
Oliver Upton | b045ae9 | 2020-04-14 22:47:45 +0000 | [diff] [blame] | 5823 | static bool nested_vmx_exit_handled_mtf(struct vmcs12 *vmcs12) |
| 5824 | { |
| 5825 | u32 entry_intr_info = vmcs12->vm_entry_intr_info_field; |
| 5826 | |
| 5827 | if (nested_cpu_has_mtf(vmcs12)) |
| 5828 | return true; |
| 5829 | |
| 5830 | /* |
| 5831 | * An MTF VM-exit may be injected into the guest by setting the |
| 5832 | * interruption-type to 7 (other event) and the vector field to 0. Such |
| 5833 | * is the case regardless of the 'monitor trap flag' VM-execution |
| 5834 | * control. |
| 5835 | */ |
| 5836 | return entry_intr_info == (INTR_INFO_VALID_MASK |
| 5837 | | INTR_TYPE_OTHER_EVENT); |
| 5838 | } |
| 5839 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5840 | /* |
Sean Christopherson | 2c1f332 | 2020-04-15 10:55:14 -0700 | [diff] [blame] | 5841 | * Return true if L0 wants to handle an exit from L2 regardless of whether or not |
| 5842 | * 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] | 5843 | */ |
Sean Christopherson | 8e53324 | 2020-11-06 17:03:12 +0800 | [diff] [blame] | 5844 | static bool nested_vmx_l0_wants_exit(struct kvm_vcpu *vcpu, |
| 5845 | union vmx_exit_reason exit_reason) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5846 | { |
Sean Christopherson | 2c1f332 | 2020-04-15 10:55:14 -0700 | [diff] [blame] | 5847 | u32 intr_info; |
| 5848 | |
Sean Christopherson | 8e53324 | 2020-11-06 17:03:12 +0800 | [diff] [blame] | 5849 | switch ((u16)exit_reason.basic) { |
Sean Christopherson | 2c1f332 | 2020-04-15 10:55:14 -0700 | [diff] [blame] | 5850 | case EXIT_REASON_EXCEPTION_NMI: |
Sean Christopherson | 8791585 | 2020-04-15 13:34:54 -0700 | [diff] [blame] | 5851 | intr_info = vmx_get_intr_info(vcpu); |
Sean Christopherson | 2c1f332 | 2020-04-15 10:55:14 -0700 | [diff] [blame] | 5852 | if (is_nmi(intr_info)) |
| 5853 | return true; |
| 5854 | else if (is_page_fault(intr_info)) |
Sean Christopherson | 18712c1 | 2021-08-11 21:56:15 -0700 | [diff] [blame] | 5855 | return vcpu->arch.apf.host_apf_flags || |
| 5856 | vmx_need_pf_intercept(vcpu); |
Sean Christopherson | 2c1f332 | 2020-04-15 10:55:14 -0700 | [diff] [blame] | 5857 | else if (is_debug(intr_info) && |
| 5858 | vcpu->guest_debug & |
| 5859 | (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP)) |
| 5860 | return true; |
| 5861 | else if (is_breakpoint(intr_info) && |
| 5862 | vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP) |
| 5863 | return true; |
Sean Christopherson | b33bb78 | 2021-06-22 10:22:44 -0700 | [diff] [blame] | 5864 | else if (is_alignment_check(intr_info) && |
| 5865 | !vmx_guest_inject_ac(vcpu)) |
| 5866 | return true; |
Sean Christopherson | 2c1f332 | 2020-04-15 10:55:14 -0700 | [diff] [blame] | 5867 | return false; |
| 5868 | case EXIT_REASON_EXTERNAL_INTERRUPT: |
| 5869 | return true; |
| 5870 | case EXIT_REASON_MCE_DURING_VMENTRY: |
| 5871 | return true; |
| 5872 | case EXIT_REASON_EPT_VIOLATION: |
| 5873 | /* |
| 5874 | * L0 always deals with the EPT violation. If nested EPT is |
| 5875 | * used, and the nested mmu code discovers that the address is |
| 5876 | * missing in the guest EPT table (EPT12), the EPT violation |
| 5877 | * will be injected with nested_ept_inject_page_fault() |
| 5878 | */ |
| 5879 | return true; |
| 5880 | case EXIT_REASON_EPT_MISCONFIG: |
| 5881 | /* |
| 5882 | * L2 never uses directly L1's EPT, but rather L0's own EPT |
| 5883 | * table (shadow on EPT) or a merged EPT table that L0 built |
| 5884 | * (EPT on EPT). So any problems with the structure of the |
| 5885 | * table is L0's fault. |
| 5886 | */ |
| 5887 | return true; |
| 5888 | case EXIT_REASON_PREEMPTION_TIMER: |
| 5889 | return true; |
| 5890 | case EXIT_REASON_PML_FULL: |
Sean Christopherson | c3bb9a2 | 2021-02-12 16:50:07 -0800 | [diff] [blame] | 5891 | /* |
| 5892 | * PML is emulated for an L1 VMM and should never be enabled in |
| 5893 | * vmcs02, always "handle" PML_FULL by exiting to userspace. |
| 5894 | */ |
Sean Christopherson | 2c1f332 | 2020-04-15 10:55:14 -0700 | [diff] [blame] | 5895 | return true; |
| 5896 | case EXIT_REASON_VMFUNC: |
| 5897 | /* VM functions are emulated through L2->L0 vmexits. */ |
| 5898 | return true; |
Chenyi Qiang | 24a996a | 2021-09-14 17:50:41 +0800 | [diff] [blame^] | 5899 | case EXIT_REASON_BUS_LOCK: |
| 5900 | /* |
| 5901 | * At present, bus lock VM exit is never exposed to L1. |
| 5902 | * Handle L2's bus locks in L0 directly. |
| 5903 | */ |
| 5904 | return true; |
Sean Christopherson | 2c1f332 | 2020-04-15 10:55:14 -0700 | [diff] [blame] | 5905 | default: |
| 5906 | break; |
| 5907 | } |
| 5908 | return false; |
| 5909 | } |
| 5910 | |
| 5911 | /* |
| 5912 | * Return 1 if L1 wants to intercept an exit from L2. Only call this when in |
| 5913 | * is_guest_mode (L2). |
| 5914 | */ |
Sean Christopherson | 8e53324 | 2020-11-06 17:03:12 +0800 | [diff] [blame] | 5915 | static bool nested_vmx_l1_wants_exit(struct kvm_vcpu *vcpu, |
| 5916 | union vmx_exit_reason exit_reason) |
Sean Christopherson | 2c1f332 | 2020-04-15 10:55:14 -0700 | [diff] [blame] | 5917 | { |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5918 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
Sean Christopherson | 9bd4af2 | 2020-04-21 00:53:27 -0700 | [diff] [blame] | 5919 | u32 intr_info; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5920 | |
Sean Christopherson | 8e53324 | 2020-11-06 17:03:12 +0800 | [diff] [blame] | 5921 | switch ((u16)exit_reason.basic) { |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5922 | case EXIT_REASON_EXCEPTION_NMI: |
Sean Christopherson | 8791585 | 2020-04-15 13:34:54 -0700 | [diff] [blame] | 5923 | intr_info = vmx_get_intr_info(vcpu); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5924 | if (is_nmi(intr_info)) |
Sean Christopherson | 2c1f332 | 2020-04-15 10:55:14 -0700 | [diff] [blame] | 5925 | return true; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5926 | else if (is_page_fault(intr_info)) |
Sean Christopherson | 2c1f332 | 2020-04-15 10:55:14 -0700 | [diff] [blame] | 5927 | return true; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5928 | return vmcs12->exception_bitmap & |
| 5929 | (1u << (intr_info & INTR_INFO_VECTOR_MASK)); |
| 5930 | case EXIT_REASON_EXTERNAL_INTERRUPT: |
Sean Christopherson | 2c1f332 | 2020-04-15 10:55:14 -0700 | [diff] [blame] | 5931 | return nested_exit_on_intr(vcpu); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5932 | case EXIT_REASON_TRIPLE_FAULT: |
| 5933 | return true; |
Xiaoyao Li | 9dadc2f | 2019-12-06 16:45:24 +0800 | [diff] [blame] | 5934 | case EXIT_REASON_INTERRUPT_WINDOW: |
| 5935 | return nested_cpu_has(vmcs12, CPU_BASED_INTR_WINDOW_EXITING); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5936 | case EXIT_REASON_NMI_WINDOW: |
Xiaoyao Li | 4e2a0bc | 2019-12-06 16:45:25 +0800 | [diff] [blame] | 5937 | return nested_cpu_has(vmcs12, CPU_BASED_NMI_WINDOW_EXITING); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5938 | case EXIT_REASON_TASK_SWITCH: |
| 5939 | return true; |
| 5940 | case EXIT_REASON_CPUID: |
| 5941 | return true; |
| 5942 | case EXIT_REASON_HLT: |
| 5943 | return nested_cpu_has(vmcs12, CPU_BASED_HLT_EXITING); |
| 5944 | case EXIT_REASON_INVD: |
| 5945 | return true; |
| 5946 | case EXIT_REASON_INVLPG: |
| 5947 | return nested_cpu_has(vmcs12, CPU_BASED_INVLPG_EXITING); |
| 5948 | case EXIT_REASON_RDPMC: |
| 5949 | return nested_cpu_has(vmcs12, CPU_BASED_RDPMC_EXITING); |
| 5950 | case EXIT_REASON_RDRAND: |
| 5951 | return nested_cpu_has2(vmcs12, SECONDARY_EXEC_RDRAND_EXITING); |
| 5952 | case EXIT_REASON_RDSEED: |
| 5953 | return nested_cpu_has2(vmcs12, SECONDARY_EXEC_RDSEED_EXITING); |
| 5954 | case EXIT_REASON_RDTSC: case EXIT_REASON_RDTSCP: |
| 5955 | return nested_cpu_has(vmcs12, CPU_BASED_RDTSC_EXITING); |
| 5956 | case EXIT_REASON_VMREAD: |
| 5957 | return nested_vmx_exit_handled_vmcs_access(vcpu, vmcs12, |
| 5958 | vmcs12->vmread_bitmap); |
| 5959 | case EXIT_REASON_VMWRITE: |
| 5960 | return nested_vmx_exit_handled_vmcs_access(vcpu, vmcs12, |
| 5961 | vmcs12->vmwrite_bitmap); |
| 5962 | case EXIT_REASON_VMCALL: case EXIT_REASON_VMCLEAR: |
| 5963 | case EXIT_REASON_VMLAUNCH: case EXIT_REASON_VMPTRLD: |
| 5964 | case EXIT_REASON_VMPTRST: case EXIT_REASON_VMRESUME: |
| 5965 | case EXIT_REASON_VMOFF: case EXIT_REASON_VMON: |
| 5966 | case EXIT_REASON_INVEPT: case EXIT_REASON_INVVPID: |
| 5967 | /* |
| 5968 | * VMX instructions trap unconditionally. This allows L1 to |
| 5969 | * emulate them for its L2 guest, i.e., allows 3-level nesting! |
| 5970 | */ |
| 5971 | return true; |
| 5972 | case EXIT_REASON_CR_ACCESS: |
| 5973 | return nested_vmx_exit_handled_cr(vcpu, vmcs12); |
| 5974 | case EXIT_REASON_DR_ACCESS: |
| 5975 | return nested_cpu_has(vmcs12, CPU_BASED_MOV_DR_EXITING); |
| 5976 | case EXIT_REASON_IO_INSTRUCTION: |
| 5977 | return nested_vmx_exit_handled_io(vcpu, vmcs12); |
| 5978 | case EXIT_REASON_GDTR_IDTR: case EXIT_REASON_LDTR_TR: |
| 5979 | return nested_cpu_has2(vmcs12, SECONDARY_EXEC_DESC); |
| 5980 | case EXIT_REASON_MSR_READ: |
| 5981 | case EXIT_REASON_MSR_WRITE: |
| 5982 | return nested_vmx_exit_handled_msr(vcpu, vmcs12, exit_reason); |
| 5983 | case EXIT_REASON_INVALID_STATE: |
| 5984 | return true; |
| 5985 | case EXIT_REASON_MWAIT_INSTRUCTION: |
| 5986 | return nested_cpu_has(vmcs12, CPU_BASED_MWAIT_EXITING); |
| 5987 | case EXIT_REASON_MONITOR_TRAP_FLAG: |
Oliver Upton | b045ae9 | 2020-04-14 22:47:45 +0000 | [diff] [blame] | 5988 | return nested_vmx_exit_handled_mtf(vmcs12); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5989 | case EXIT_REASON_MONITOR_INSTRUCTION: |
| 5990 | return nested_cpu_has(vmcs12, CPU_BASED_MONITOR_EXITING); |
| 5991 | case EXIT_REASON_PAUSE_INSTRUCTION: |
| 5992 | return nested_cpu_has(vmcs12, CPU_BASED_PAUSE_EXITING) || |
| 5993 | nested_cpu_has2(vmcs12, |
| 5994 | SECONDARY_EXEC_PAUSE_LOOP_EXITING); |
| 5995 | case EXIT_REASON_MCE_DURING_VMENTRY: |
Sean Christopherson | 2c1f332 | 2020-04-15 10:55:14 -0700 | [diff] [blame] | 5996 | return true; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5997 | case EXIT_REASON_TPR_BELOW_THRESHOLD: |
| 5998 | return nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW); |
| 5999 | case EXIT_REASON_APIC_ACCESS: |
| 6000 | case EXIT_REASON_APIC_WRITE: |
| 6001 | case EXIT_REASON_EOI_INDUCED: |
| 6002 | /* |
| 6003 | * The controls for "virtualize APIC accesses," "APIC- |
| 6004 | * register virtualization," and "virtual-interrupt |
| 6005 | * delivery" only come from vmcs12. |
| 6006 | */ |
| 6007 | return true; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6008 | case EXIT_REASON_INVPCID: |
| 6009 | return |
| 6010 | nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_INVPCID) && |
| 6011 | nested_cpu_has(vmcs12, CPU_BASED_INVLPG_EXITING); |
| 6012 | case EXIT_REASON_WBINVD: |
| 6013 | return nested_cpu_has2(vmcs12, SECONDARY_EXEC_WBINVD_EXITING); |
| 6014 | case EXIT_REASON_XSETBV: |
| 6015 | return true; |
| 6016 | case EXIT_REASON_XSAVES: case EXIT_REASON_XRSTORS: |
| 6017 | /* |
| 6018 | * This should never happen, since it is not possible to |
| 6019 | * set XSS to a non-zero value---neither in L1 nor in L2. |
| 6020 | * If if it were, XSS would have to be checked against |
| 6021 | * the XSS exit bitmap in vmcs12. |
| 6022 | */ |
| 6023 | return nested_cpu_has2(vmcs12, SECONDARY_EXEC_XSAVES); |
Tao Xu | bf653b7 | 2019-07-16 14:55:51 +0800 | [diff] [blame] | 6024 | case EXIT_REASON_UMWAIT: |
| 6025 | case EXIT_REASON_TPAUSE: |
| 6026 | return nested_cpu_has2(vmcs12, |
| 6027 | SECONDARY_EXEC_ENABLE_USR_WAIT_PAUSE); |
Sean Christopherson | 72add91 | 2021-04-12 16:21:42 +1200 | [diff] [blame] | 6028 | case EXIT_REASON_ENCLS: |
| 6029 | return nested_vmx_exit_handled_encls(vcpu, vmcs12); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6030 | default: |
| 6031 | return true; |
| 6032 | } |
| 6033 | } |
| 6034 | |
Sean Christopherson | 7b7bd87 | 2020-04-15 10:55:11 -0700 | [diff] [blame] | 6035 | /* |
| 6036 | * Conditionally reflect a VM-Exit into L1. Returns %true if the VM-Exit was |
| 6037 | * reflected into L1. |
| 6038 | */ |
Sean Christopherson | f47baae | 2020-04-15 10:55:16 -0700 | [diff] [blame] | 6039 | bool nested_vmx_reflect_vmexit(struct kvm_vcpu *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 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Sean Christopherson | 8e53324 | 2020-11-06 17:03:12 +0800 | [diff] [blame] | 6042 | union vmx_exit_reason exit_reason = vmx->exit_reason; |
Sean Christopherson | 8779655 | 2020-04-22 17:11:27 -0700 | [diff] [blame] | 6043 | unsigned long exit_qual; |
| 6044 | u32 exit_intr_info; |
Sean Christopherson | fbdd502 | 2020-04-15 10:55:12 -0700 | [diff] [blame] | 6045 | |
| 6046 | WARN_ON_ONCE(vmx->nested.nested_run_pending); |
| 6047 | |
| 6048 | /* |
| 6049 | * Late nested VM-Fail shares the same flow as nested VM-Exit since KVM |
| 6050 | * has already loaded L2's state. |
| 6051 | */ |
| 6052 | if (unlikely(vmx->fail)) { |
| 6053 | trace_kvm_nested_vmenter_failed( |
| 6054 | "hardware VM-instruction error: ", |
| 6055 | vmcs_read32(VM_INSTRUCTION_ERROR)); |
| 6056 | exit_intr_info = 0; |
| 6057 | exit_qual = 0; |
| 6058 | goto reflect_vmexit; |
| 6059 | } |
Sean Christopherson | 7b7bd87 | 2020-04-15 10:55:11 -0700 | [diff] [blame] | 6060 | |
Sean Christopherson | 8e53324 | 2020-11-06 17:03:12 +0800 | [diff] [blame] | 6061 | trace_kvm_nested_vmexit(exit_reason.full, vcpu, KVM_ISA_VMX); |
Sean Christopherson | 236871b | 2020-04-15 10:55:13 -0700 | [diff] [blame] | 6062 | |
Sean Christopherson | 2c1f332 | 2020-04-15 10:55:14 -0700 | [diff] [blame] | 6063 | /* If L0 (KVM) wants the exit, it trumps L1's desires. */ |
| 6064 | if (nested_vmx_l0_wants_exit(vcpu, exit_reason)) |
| 6065 | return false; |
| 6066 | |
| 6067 | /* If L1 doesn't want the exit, handle it in L0. */ |
| 6068 | if (!nested_vmx_l1_wants_exit(vcpu, exit_reason)) |
Sean Christopherson | 7b7bd87 | 2020-04-15 10:55:11 -0700 | [diff] [blame] | 6069 | return false; |
| 6070 | |
| 6071 | /* |
Sean Christopherson | 1d28306 | 2020-04-15 10:55:15 -0700 | [diff] [blame] | 6072 | * vmcs.VM_EXIT_INTR_INFO is only valid for EXCEPTION_NMI exits. For |
| 6073 | * EXTERNAL_INTERRUPT, the value for vmcs12->vm_exit_intr_info would |
| 6074 | * need to be synthesized by querying the in-kernel LAPIC, but external |
| 6075 | * interrupts are never reflected to L1 so it's a non-issue. |
Sean Christopherson | 7b7bd87 | 2020-04-15 10:55:11 -0700 | [diff] [blame] | 6076 | */ |
Sean Christopherson | 02f1965 | 2020-09-23 13:13:49 -0700 | [diff] [blame] | 6077 | exit_intr_info = vmx_get_intr_info(vcpu); |
Sean Christopherson | f315f2b | 2020-09-23 13:13:45 -0700 | [diff] [blame] | 6078 | if (is_exception_with_error_code(exit_intr_info)) { |
Sean Christopherson | 7b7bd87 | 2020-04-15 10:55:11 -0700 | [diff] [blame] | 6079 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
| 6080 | |
| 6081 | vmcs12->vm_exit_intr_error_code = |
| 6082 | vmcs_read32(VM_EXIT_INTR_ERROR_CODE); |
| 6083 | } |
Sean Christopherson | 02f1965 | 2020-09-23 13:13:49 -0700 | [diff] [blame] | 6084 | exit_qual = vmx_get_exit_qual(vcpu); |
Sean Christopherson | 7b7bd87 | 2020-04-15 10:55:11 -0700 | [diff] [blame] | 6085 | |
Sean Christopherson | fbdd502 | 2020-04-15 10:55:12 -0700 | [diff] [blame] | 6086 | reflect_vmexit: |
Sean Christopherson | 8e53324 | 2020-11-06 17:03:12 +0800 | [diff] [blame] | 6087 | nested_vmx_vmexit(vcpu, exit_reason.full, exit_intr_info, exit_qual); |
Sean Christopherson | 7b7bd87 | 2020-04-15 10:55:11 -0700 | [diff] [blame] | 6088 | return true; |
| 6089 | } |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6090 | |
| 6091 | static int vmx_get_nested_state(struct kvm_vcpu *vcpu, |
| 6092 | struct kvm_nested_state __user *user_kvm_nested_state, |
| 6093 | u32 user_data_size) |
| 6094 | { |
| 6095 | struct vcpu_vmx *vmx; |
| 6096 | struct vmcs12 *vmcs12; |
| 6097 | struct kvm_nested_state kvm_state = { |
| 6098 | .flags = 0, |
Liran Alon | 6ca00df | 2019-06-16 15:03:10 +0300 | [diff] [blame] | 6099 | .format = KVM_STATE_NESTED_FORMAT_VMX, |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6100 | .size = sizeof(kvm_state), |
Peter Shier | 850448f | 2020-05-26 14:51:06 -0700 | [diff] [blame] | 6101 | .hdr.vmx.flags = 0, |
Liran Alon | 6ca00df | 2019-06-16 15:03:10 +0300 | [diff] [blame] | 6102 | .hdr.vmx.vmxon_pa = -1ull, |
| 6103 | .hdr.vmx.vmcs12_pa = -1ull, |
Peter Shier | 850448f | 2020-05-26 14:51:06 -0700 | [diff] [blame] | 6104 | .hdr.vmx.preemption_timer_deadline = 0, |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6105 | }; |
Liran Alon | 6ca00df | 2019-06-16 15:03:10 +0300 | [diff] [blame] | 6106 | struct kvm_vmx_nested_state_data __user *user_vmx_nested_state = |
| 6107 | &user_kvm_nested_state->data.vmx[0]; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6108 | |
| 6109 | if (!vcpu) |
Liran Alon | 6ca00df | 2019-06-16 15:03:10 +0300 | [diff] [blame] | 6110 | return kvm_state.size + sizeof(*user_vmx_nested_state); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6111 | |
| 6112 | vmx = to_vmx(vcpu); |
| 6113 | vmcs12 = get_vmcs12(vcpu); |
| 6114 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6115 | if (nested_vmx_allowed(vcpu) && |
| 6116 | (vmx->nested.vmxon || vmx->nested.smm.vmxon)) { |
Liran Alon | 6ca00df | 2019-06-16 15:03:10 +0300 | [diff] [blame] | 6117 | kvm_state.hdr.vmx.vmxon_pa = vmx->nested.vmxon_ptr; |
| 6118 | kvm_state.hdr.vmx.vmcs12_pa = vmx->nested.current_vmptr; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6119 | |
| 6120 | if (vmx_has_valid_vmcs12(vcpu)) { |
Liran Alon | 6ca00df | 2019-06-16 15:03:10 +0300 | [diff] [blame] | 6121 | kvm_state.size += sizeof(user_vmx_nested_state->vmcs12); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6122 | |
Vitaly Kuznetsov | 2784996 | 2021-05-26 15:20:20 +0200 | [diff] [blame] | 6123 | /* 'hv_evmcs_vmptr' can also be EVMPTR_MAP_PENDING here */ |
| 6124 | if (vmx->nested.hv_evmcs_vmptr != EVMPTR_INVALID) |
Liran Alon | 323d73a | 2019-06-26 16:09:27 +0300 | [diff] [blame] | 6125 | kvm_state.flags |= KVM_STATE_NESTED_EVMCS; |
| 6126 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6127 | if (is_guest_mode(vcpu) && |
| 6128 | nested_cpu_has_shadow_vmcs(vmcs12) && |
| 6129 | vmcs12->vmcs_link_pointer != -1ull) |
Liran Alon | 6ca00df | 2019-06-16 15:03:10 +0300 | [diff] [blame] | 6130 | kvm_state.size += sizeof(user_vmx_nested_state->shadow_vmcs12); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6131 | } |
| 6132 | |
| 6133 | if (vmx->nested.smm.vmxon) |
Liran Alon | 6ca00df | 2019-06-16 15:03:10 +0300 | [diff] [blame] | 6134 | kvm_state.hdr.vmx.smm.flags |= KVM_STATE_NESTED_SMM_VMXON; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6135 | |
| 6136 | if (vmx->nested.smm.guest_mode) |
Liran Alon | 6ca00df | 2019-06-16 15:03:10 +0300 | [diff] [blame] | 6137 | kvm_state.hdr.vmx.smm.flags |= KVM_STATE_NESTED_SMM_GUEST_MODE; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6138 | |
| 6139 | if (is_guest_mode(vcpu)) { |
| 6140 | kvm_state.flags |= KVM_STATE_NESTED_GUEST_MODE; |
| 6141 | |
| 6142 | if (vmx->nested.nested_run_pending) |
| 6143 | kvm_state.flags |= KVM_STATE_NESTED_RUN_PENDING; |
Oliver Upton | 5ef8acb | 2020-02-07 02:36:07 -0800 | [diff] [blame] | 6144 | |
| 6145 | if (vmx->nested.mtf_pending) |
| 6146 | kvm_state.flags |= KVM_STATE_NESTED_MTF_PENDING; |
Peter Shier | 850448f | 2020-05-26 14:51:06 -0700 | [diff] [blame] | 6147 | |
| 6148 | if (nested_cpu_has_preemption_timer(vmcs12) && |
| 6149 | vmx->nested.has_preemption_timer_deadline) { |
| 6150 | kvm_state.hdr.vmx.flags |= |
| 6151 | KVM_STATE_VMX_PREEMPTION_TIMER_DEADLINE; |
| 6152 | kvm_state.hdr.vmx.preemption_timer_deadline = |
| 6153 | vmx->nested.preemption_timer_deadline; |
| 6154 | } |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6155 | } |
| 6156 | } |
| 6157 | |
| 6158 | if (user_data_size < kvm_state.size) |
| 6159 | goto out; |
| 6160 | |
| 6161 | if (copy_to_user(user_kvm_nested_state, &kvm_state, sizeof(kvm_state))) |
| 6162 | return -EFAULT; |
| 6163 | |
| 6164 | if (!vmx_has_valid_vmcs12(vcpu)) |
| 6165 | goto out; |
| 6166 | |
| 6167 | /* |
| 6168 | * When running L2, the authoritative vmcs12 state is in the |
| 6169 | * vmcs02. When running L1, the authoritative vmcs12 state is |
| 6170 | * in the shadow or enlightened vmcs linked to vmcs01, unless |
Sean Christopherson | 3731905ef | 2019-05-07 08:36:27 -0700 | [diff] [blame] | 6171 | * need_vmcs12_to_shadow_sync is set, in which case, the authoritative |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6172 | * vmcs12 state is in the vmcs12 already. |
| 6173 | */ |
| 6174 | if (is_guest_mode(vcpu)) { |
Sean Christopherson | 3731905ef | 2019-05-07 08:36:27 -0700 | [diff] [blame] | 6175 | sync_vmcs02_to_vmcs12(vcpu, vmcs12); |
Sean Christopherson | 7952d76 | 2019-05-07 08:36:29 -0700 | [diff] [blame] | 6176 | sync_vmcs02_to_vmcs12_rare(vcpu, vmcs12); |
Maxim Levitsky | d51e1d3 | 2021-01-14 22:54:47 +0200 | [diff] [blame] | 6177 | } else { |
| 6178 | copy_vmcs02_to_vmcs12_rare(vcpu, get_vmcs12(vcpu)); |
| 6179 | if (!vmx->nested.need_vmcs12_to_shadow_sync) { |
Vitaly Kuznetsov | 1e9dfbd | 2021-05-26 15:20:16 +0200 | [diff] [blame] | 6180 | if (evmptr_is_valid(vmx->nested.hv_evmcs_vmptr)) |
Vitaly Kuznetsov | d6bf71a | 2021-05-26 15:20:22 +0200 | [diff] [blame] | 6181 | /* |
| 6182 | * L1 hypervisor is not obliged to keep eVMCS |
| 6183 | * clean fields data always up-to-date while |
| 6184 | * not in guest mode, 'hv_clean_fields' is only |
| 6185 | * supposed to be actual upon vmentry so we need |
| 6186 | * to ignore it here and do full copy. |
| 6187 | */ |
| 6188 | copy_enlightened_to_vmcs12(vmx, 0); |
Maxim Levitsky | d51e1d3 | 2021-01-14 22:54:47 +0200 | [diff] [blame] | 6189 | else if (enable_shadow_vmcs) |
| 6190 | copy_shadow_to_vmcs12(vmx); |
| 6191 | } |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6192 | } |
| 6193 | |
Liran Alon | 6ca00df | 2019-06-16 15:03:10 +0300 | [diff] [blame] | 6194 | BUILD_BUG_ON(sizeof(user_vmx_nested_state->vmcs12) < VMCS12_SIZE); |
| 6195 | BUILD_BUG_ON(sizeof(user_vmx_nested_state->shadow_vmcs12) < VMCS12_SIZE); |
| 6196 | |
Tom Roeder | 3a33d03 | 2019-01-24 13:48:20 -0800 | [diff] [blame] | 6197 | /* |
| 6198 | * Copy over the full allocated size of vmcs12 rather than just the size |
| 6199 | * of the struct. |
| 6200 | */ |
Liran Alon | 6ca00df | 2019-06-16 15:03:10 +0300 | [diff] [blame] | 6201 | if (copy_to_user(user_vmx_nested_state->vmcs12, vmcs12, VMCS12_SIZE)) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6202 | return -EFAULT; |
| 6203 | |
| 6204 | if (nested_cpu_has_shadow_vmcs(vmcs12) && |
| 6205 | vmcs12->vmcs_link_pointer != -1ull) { |
Liran Alon | 6ca00df | 2019-06-16 15:03:10 +0300 | [diff] [blame] | 6206 | if (copy_to_user(user_vmx_nested_state->shadow_vmcs12, |
Tom Roeder | 3a33d03 | 2019-01-24 13:48:20 -0800 | [diff] [blame] | 6207 | get_shadow_vmcs12(vcpu), VMCS12_SIZE)) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6208 | return -EFAULT; |
| 6209 | } |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6210 | out: |
| 6211 | return kvm_state.size; |
| 6212 | } |
| 6213 | |
| 6214 | /* |
| 6215 | * Forcibly leave nested mode in order to be able to reset the VCPU later on. |
| 6216 | */ |
| 6217 | void vmx_leave_nested(struct kvm_vcpu *vcpu) |
| 6218 | { |
| 6219 | if (is_guest_mode(vcpu)) { |
| 6220 | to_vmx(vcpu)->nested.nested_run_pending = 0; |
| 6221 | nested_vmx_vmexit(vcpu, -1, 0, 0); |
| 6222 | } |
| 6223 | free_nested(vcpu); |
| 6224 | } |
| 6225 | |
| 6226 | static int vmx_set_nested_state(struct kvm_vcpu *vcpu, |
| 6227 | struct kvm_nested_state __user *user_kvm_nested_state, |
| 6228 | struct kvm_nested_state *kvm_state) |
| 6229 | { |
| 6230 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 6231 | struct vmcs12 *vmcs12; |
Sean Christopherson | 68cda40 | 2020-05-11 15:05:29 -0700 | [diff] [blame] | 6232 | enum vm_entry_failure_code ignored; |
Liran Alon | 6ca00df | 2019-06-16 15:03:10 +0300 | [diff] [blame] | 6233 | struct kvm_vmx_nested_state_data __user *user_vmx_nested_state = |
| 6234 | &user_kvm_nested_state->data.vmx[0]; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6235 | int ret; |
| 6236 | |
Liran Alon | 6ca00df | 2019-06-16 15:03:10 +0300 | [diff] [blame] | 6237 | if (kvm_state->format != KVM_STATE_NESTED_FORMAT_VMX) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6238 | return -EINVAL; |
| 6239 | |
Liran Alon | 6ca00df | 2019-06-16 15:03:10 +0300 | [diff] [blame] | 6240 | if (kvm_state->hdr.vmx.vmxon_pa == -1ull) { |
| 6241 | if (kvm_state->hdr.vmx.smm.flags) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6242 | return -EINVAL; |
| 6243 | |
Liran Alon | 6ca00df | 2019-06-16 15:03:10 +0300 | [diff] [blame] | 6244 | if (kvm_state->hdr.vmx.vmcs12_pa != -1ull) |
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 | /* |
| 6248 | * KVM_STATE_NESTED_EVMCS used to signal that KVM should |
| 6249 | * enable eVMCS capability on vCPU. However, since then |
| 6250 | * code was changed such that flag signals vmcs12 should |
| 6251 | * be copied into eVMCS in guest memory. |
| 6252 | * |
| 6253 | * To preserve backwards compatability, allow user |
| 6254 | * to set this flag even when there is no VMXON region. |
| 6255 | */ |
Paolo Bonzini | 9fd5887 | 2019-06-19 16:52:27 +0200 | [diff] [blame] | 6256 | if (kvm_state->flags & ~KVM_STATE_NESTED_EVMCS) |
| 6257 | return -EINVAL; |
| 6258 | } else { |
| 6259 | if (!nested_vmx_allowed(vcpu)) |
| 6260 | return -EINVAL; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6261 | |
Paolo Bonzini | 9fd5887 | 2019-06-19 16:52:27 +0200 | [diff] [blame] | 6262 | if (!page_address_valid(vcpu, kvm_state->hdr.vmx.vmxon_pa)) |
| 6263 | return -EINVAL; |
Liran Alon | 323d73a | 2019-06-26 16:09:27 +0300 | [diff] [blame] | 6264 | } |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6265 | |
Liran Alon | 6ca00df | 2019-06-16 15:03:10 +0300 | [diff] [blame] | 6266 | 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] | 6267 | (kvm_state->flags & KVM_STATE_NESTED_GUEST_MODE)) |
| 6268 | return -EINVAL; |
| 6269 | |
Liran Alon | 6ca00df | 2019-06-16 15:03:10 +0300 | [diff] [blame] | 6270 | if (kvm_state->hdr.vmx.smm.flags & |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6271 | ~(KVM_STATE_NESTED_SMM_GUEST_MODE | KVM_STATE_NESTED_SMM_VMXON)) |
| 6272 | return -EINVAL; |
| 6273 | |
Paolo Bonzini | 5e105c8 | 2020-07-27 08:55:09 -0400 | [diff] [blame] | 6274 | if (kvm_state->hdr.vmx.flags & ~KVM_STATE_VMX_PREEMPTION_TIMER_DEADLINE) |
| 6275 | return -EINVAL; |
| 6276 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6277 | /* |
| 6278 | * SMM temporarily disables VMX, so we cannot be in guest mode, |
| 6279 | * nor can VMLAUNCH/VMRESUME be pending. Outside SMM, SMM flags |
| 6280 | * must be zero. |
| 6281 | */ |
Liran Alon | 65b712f1 | 2019-06-25 14:26:42 +0300 | [diff] [blame] | 6282 | if (is_smm(vcpu) ? |
| 6283 | (kvm_state->flags & |
| 6284 | (KVM_STATE_NESTED_GUEST_MODE | KVM_STATE_NESTED_RUN_PENDING)) |
| 6285 | : kvm_state->hdr.vmx.smm.flags) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6286 | return -EINVAL; |
| 6287 | |
Liran Alon | 6ca00df | 2019-06-16 15:03:10 +0300 | [diff] [blame] | 6288 | if ((kvm_state->hdr.vmx.smm.flags & KVM_STATE_NESTED_SMM_GUEST_MODE) && |
| 6289 | !(kvm_state->hdr.vmx.smm.flags & KVM_STATE_NESTED_SMM_VMXON)) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6290 | return -EINVAL; |
| 6291 | |
Liran Alon | 323d73a | 2019-06-26 16:09:27 +0300 | [diff] [blame] | 6292 | if ((kvm_state->flags & KVM_STATE_NESTED_EVMCS) && |
| 6293 | (!nested_vmx_allowed(vcpu) || !vmx->nested.enlightened_vmcs_enabled)) |
Paolo Bonzini | 9fd5887 | 2019-06-19 16:52:27 +0200 | [diff] [blame] | 6294 | return -EINVAL; |
| 6295 | |
Liran Alon | 323d73a | 2019-06-26 16:09:27 +0300 | [diff] [blame] | 6296 | vmx_leave_nested(vcpu); |
Paolo Bonzini | 9fd5887 | 2019-06-19 16:52:27 +0200 | [diff] [blame] | 6297 | |
Liran Alon | 6ca00df | 2019-06-16 15:03:10 +0300 | [diff] [blame] | 6298 | if (kvm_state->hdr.vmx.vmxon_pa == -1ull) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6299 | return 0; |
| 6300 | |
Liran Alon | 6ca00df | 2019-06-16 15:03:10 +0300 | [diff] [blame] | 6301 | vmx->nested.vmxon_ptr = kvm_state->hdr.vmx.vmxon_pa; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6302 | ret = enter_vmx_operation(vcpu); |
| 6303 | if (ret) |
| 6304 | return ret; |
| 6305 | |
Paolo Bonzini | 0f02bd0 | 2020-07-27 09:00:37 -0400 | [diff] [blame] | 6306 | /* Empty 'VMXON' state is permitted if no VMCS loaded */ |
| 6307 | if (kvm_state->size < sizeof(*kvm_state) + sizeof(*vmcs12)) { |
| 6308 | /* See vmx_has_valid_vmcs12. */ |
| 6309 | if ((kvm_state->flags & KVM_STATE_NESTED_GUEST_MODE) || |
| 6310 | (kvm_state->flags & KVM_STATE_NESTED_EVMCS) || |
| 6311 | (kvm_state->hdr.vmx.vmcs12_pa != -1ull)) |
| 6312 | return -EINVAL; |
| 6313 | else |
| 6314 | return 0; |
| 6315 | } |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6316 | |
Liran Alon | 6ca00df | 2019-06-16 15:03:10 +0300 | [diff] [blame] | 6317 | if (kvm_state->hdr.vmx.vmcs12_pa != -1ull) { |
| 6318 | if (kvm_state->hdr.vmx.vmcs12_pa == kvm_state->hdr.vmx.vmxon_pa || |
| 6319 | !page_address_valid(vcpu, kvm_state->hdr.vmx.vmcs12_pa)) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6320 | return -EINVAL; |
| 6321 | |
Liran Alon | 6ca00df | 2019-06-16 15:03:10 +0300 | [diff] [blame] | 6322 | set_current_vmptr(vmx, kvm_state->hdr.vmx.vmcs12_pa); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6323 | } else if (kvm_state->flags & KVM_STATE_NESTED_EVMCS) { |
| 6324 | /* |
Vitaly Kuznetsov | e942dbf | 2020-03-09 16:52:12 +0100 | [diff] [blame] | 6325 | * nested_vmx_handle_enlightened_vmptrld() cannot be called |
| 6326 | * directly from here as HV_X64_MSR_VP_ASSIST_PAGE may not be |
| 6327 | * restored yet. EVMCS will be mapped from |
| 6328 | * nested_get_vmcs12_pages(). |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6329 | */ |
Vitaly Kuznetsov | 2784996 | 2021-05-26 15:20:20 +0200 | [diff] [blame] | 6330 | vmx->nested.hv_evmcs_vmptr = EVMPTR_MAP_PENDING; |
Paolo Bonzini | 729c15c | 2020-09-22 06:53:57 -0400 | [diff] [blame] | 6331 | kvm_make_request(KVM_REQ_GET_NESTED_STATE_PAGES, vcpu); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6332 | } else { |
| 6333 | return -EINVAL; |
| 6334 | } |
| 6335 | |
Liran Alon | 6ca00df | 2019-06-16 15:03:10 +0300 | [diff] [blame] | 6336 | if (kvm_state->hdr.vmx.smm.flags & KVM_STATE_NESTED_SMM_VMXON) { |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6337 | vmx->nested.smm.vmxon = true; |
| 6338 | vmx->nested.vmxon = false; |
| 6339 | |
Liran Alon | 6ca00df | 2019-06-16 15:03:10 +0300 | [diff] [blame] | 6340 | 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] | 6341 | vmx->nested.smm.guest_mode = true; |
| 6342 | } |
| 6343 | |
| 6344 | vmcs12 = get_vmcs12(vcpu); |
Liran Alon | 6ca00df | 2019-06-16 15:03:10 +0300 | [diff] [blame] | 6345 | if (copy_from_user(vmcs12, user_vmx_nested_state->vmcs12, sizeof(*vmcs12))) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6346 | return -EFAULT; |
| 6347 | |
| 6348 | if (vmcs12->hdr.revision_id != VMCS12_REVISION) |
| 6349 | return -EINVAL; |
| 6350 | |
| 6351 | if (!(kvm_state->flags & KVM_STATE_NESTED_GUEST_MODE)) |
| 6352 | return 0; |
| 6353 | |
Sean Christopherson | 21be4ca | 2019-05-08 11:04:32 -0700 | [diff] [blame] | 6354 | vmx->nested.nested_run_pending = |
| 6355 | !!(kvm_state->flags & KVM_STATE_NESTED_RUN_PENDING); |
| 6356 | |
Oliver Upton | 5ef8acb | 2020-02-07 02:36:07 -0800 | [diff] [blame] | 6357 | vmx->nested.mtf_pending = |
| 6358 | !!(kvm_state->flags & KVM_STATE_NESTED_MTF_PENDING); |
| 6359 | |
Sean Christopherson | 21be4ca | 2019-05-08 11:04:32 -0700 | [diff] [blame] | 6360 | ret = -EINVAL; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6361 | if (nested_cpu_has_shadow_vmcs(vmcs12) && |
| 6362 | vmcs12->vmcs_link_pointer != -1ull) { |
| 6363 | struct vmcs12 *shadow_vmcs12 = get_shadow_vmcs12(vcpu); |
| 6364 | |
Liran Alon | 6ca00df | 2019-06-16 15:03:10 +0300 | [diff] [blame] | 6365 | if (kvm_state->size < |
| 6366 | sizeof(*kvm_state) + |
| 6367 | sizeof(user_vmx_nested_state->vmcs12) + sizeof(*shadow_vmcs12)) |
Sean Christopherson | 21be4ca | 2019-05-08 11:04:32 -0700 | [diff] [blame] | 6368 | goto error_guest_mode; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6369 | |
| 6370 | if (copy_from_user(shadow_vmcs12, |
Liran Alon | 6ca00df | 2019-06-16 15:03:10 +0300 | [diff] [blame] | 6371 | user_vmx_nested_state->shadow_vmcs12, |
| 6372 | sizeof(*shadow_vmcs12))) { |
Sean Christopherson | 21be4ca | 2019-05-08 11:04:32 -0700 | [diff] [blame] | 6373 | ret = -EFAULT; |
| 6374 | goto error_guest_mode; |
| 6375 | } |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6376 | |
| 6377 | if (shadow_vmcs12->hdr.revision_id != VMCS12_REVISION || |
| 6378 | !shadow_vmcs12->hdr.shadow_vmcs) |
Sean Christopherson | 21be4ca | 2019-05-08 11:04:32 -0700 | [diff] [blame] | 6379 | goto error_guest_mode; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6380 | } |
| 6381 | |
Paolo Bonzini | 83d31e5 | 2020-07-09 13:12:09 -0400 | [diff] [blame] | 6382 | vmx->nested.has_preemption_timer_deadline = false; |
Peter Shier | 850448f | 2020-05-26 14:51:06 -0700 | [diff] [blame] | 6383 | if (kvm_state->hdr.vmx.flags & KVM_STATE_VMX_PREEMPTION_TIMER_DEADLINE) { |
| 6384 | vmx->nested.has_preemption_timer_deadline = true; |
| 6385 | vmx->nested.preemption_timer_deadline = |
| 6386 | kvm_state->hdr.vmx.preemption_timer_deadline; |
| 6387 | } |
| 6388 | |
Sean Christopherson | 5478ba3 | 2019-04-11 12:18:06 -0700 | [diff] [blame] | 6389 | if (nested_vmx_check_controls(vcpu, vmcs12) || |
| 6390 | nested_vmx_check_host_state(vcpu, vmcs12) || |
Sean Christopherson | 68cda40 | 2020-05-11 15:05:29 -0700 | [diff] [blame] | 6391 | nested_vmx_check_guest_state(vcpu, vmcs12, &ignored)) |
Sean Christopherson | 21be4ca | 2019-05-08 11:04:32 -0700 | [diff] [blame] | 6392 | goto error_guest_mode; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6393 | |
| 6394 | vmx->nested.dirty_vmcs12 = true; |
| 6395 | ret = nested_vmx_enter_non_root_mode(vcpu, false); |
Sean Christopherson | 21be4ca | 2019-05-08 11:04:32 -0700 | [diff] [blame] | 6396 | if (ret) |
| 6397 | goto error_guest_mode; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6398 | |
| 6399 | return 0; |
Sean Christopherson | 21be4ca | 2019-05-08 11:04:32 -0700 | [diff] [blame] | 6400 | |
| 6401 | error_guest_mode: |
| 6402 | vmx->nested.nested_run_pending = 0; |
| 6403 | return ret; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6404 | } |
| 6405 | |
Xiaoyao Li | 1b84292 | 2019-10-20 17:11:01 +0800 | [diff] [blame] | 6406 | void nested_vmx_set_vmcs_shadowing_bitmap(void) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6407 | { |
| 6408 | if (enable_shadow_vmcs) { |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6409 | vmcs_write64(VMREAD_BITMAP, __pa(vmx_vmread_bitmap)); |
Sean Christopherson | fadcead | 2019-05-07 08:36:23 -0700 | [diff] [blame] | 6410 | vmcs_write64(VMWRITE_BITMAP, __pa(vmx_vmwrite_bitmap)); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6411 | } |
| 6412 | } |
| 6413 | |
| 6414 | /* |
Sean Christopherson | ba1f824 | 2021-06-18 14:46:58 -0700 | [diff] [blame] | 6415 | * Indexing into the vmcs12 uses the VMCS encoding rotated left by 6. Undo |
| 6416 | * that madness to get the encoding for comparison. |
| 6417 | */ |
| 6418 | #define VMCS12_IDX_TO_ENC(idx) ((u16)(((u16)(idx) >> 6) | ((u16)(idx) << 10))) |
| 6419 | |
| 6420 | static u64 nested_vmx_calc_vmcs_enum_msr(void) |
| 6421 | { |
| 6422 | /* |
| 6423 | * Note these are the so called "index" of the VMCS field encoding, not |
| 6424 | * the index into vmcs12. |
| 6425 | */ |
| 6426 | unsigned int max_idx, idx; |
| 6427 | int i; |
| 6428 | |
| 6429 | /* |
| 6430 | * For better or worse, KVM allows VMREAD/VMWRITE to all fields in |
| 6431 | * vmcs12, regardless of whether or not the associated feature is |
| 6432 | * exposed to L1. Simply find the field with the highest index. |
| 6433 | */ |
| 6434 | max_idx = 0; |
| 6435 | for (i = 0; i < nr_vmcs12_fields; i++) { |
| 6436 | /* The vmcs12 table is very, very sparsely populated. */ |
| 6437 | if (!vmcs_field_to_offset_table[i]) |
| 6438 | continue; |
| 6439 | |
| 6440 | idx = vmcs_field_index(VMCS12_IDX_TO_ENC(i)); |
| 6441 | if (idx > max_idx) |
| 6442 | max_idx = idx; |
| 6443 | } |
| 6444 | |
| 6445 | return (u64)max_idx << VMCS_FIELD_INDEX_SHIFT; |
| 6446 | } |
| 6447 | |
| 6448 | /* |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6449 | * nested_vmx_setup_ctls_msrs() sets up variables containing the values to be |
| 6450 | * returned for the various VMX controls MSRs when nested VMX is enabled. |
| 6451 | * The same values should also be used to verify that vmcs12 control fields are |
| 6452 | * valid during nested entry from L1 to L2. |
| 6453 | * Each of these control msrs has a low and high 32-bit half: A low bit is on |
| 6454 | * if the corresponding bit in the (32-bit) control field *must* be on, and a |
| 6455 | * bit in the high half is on if the corresponding bit in the control field |
| 6456 | * may be on. See also vmx_control_verify(). |
| 6457 | */ |
Vitaly Kuznetsov | a444326 | 2020-02-20 18:22:04 +0100 | [diff] [blame] | 6458 | 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] | 6459 | { |
| 6460 | /* |
| 6461 | * Note that as a general rule, the high half of the MSRs (bits in |
| 6462 | * the control fields which may be 1) should be initialized by the |
| 6463 | * intersection of the underlying hardware's MSR (i.e., features which |
| 6464 | * can be supported) and the list of features we want to expose - |
| 6465 | * because they are known to be properly supported in our code. |
| 6466 | * Also, usually, the low half of the MSRs (bits which must be 1) can |
| 6467 | * be set to 0, meaning that L1 may turn off any of these bits. The |
| 6468 | * reason is that if one of these bits is necessary, it will appear |
| 6469 | * in vmcs01 and prepare_vmcs02, when it bitwise-or's the control |
| 6470 | * fields of vmcs01 and vmcs02, will turn these bits off - and |
Sean Christopherson | 2c1f332 | 2020-04-15 10:55:14 -0700 | [diff] [blame] | 6471 | * nested_vmx_l1_wants_exit() will not pass related exits to L1. |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6472 | * These rules have exceptions below. |
| 6473 | */ |
| 6474 | |
| 6475 | /* pin-based controls */ |
| 6476 | rdmsr(MSR_IA32_VMX_PINBASED_CTLS, |
| 6477 | msrs->pinbased_ctls_low, |
| 6478 | msrs->pinbased_ctls_high); |
| 6479 | msrs->pinbased_ctls_low |= |
| 6480 | PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR; |
| 6481 | msrs->pinbased_ctls_high &= |
| 6482 | PIN_BASED_EXT_INTR_MASK | |
| 6483 | PIN_BASED_NMI_EXITING | |
| 6484 | PIN_BASED_VIRTUAL_NMIS | |
Vitaly Kuznetsov | a444326 | 2020-02-20 18:22:04 +0100 | [diff] [blame] | 6485 | (enable_apicv ? PIN_BASED_POSTED_INTR : 0); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6486 | msrs->pinbased_ctls_high |= |
| 6487 | PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR | |
| 6488 | PIN_BASED_VMX_PREEMPTION_TIMER; |
| 6489 | |
| 6490 | /* exit controls */ |
| 6491 | rdmsr(MSR_IA32_VMX_EXIT_CTLS, |
| 6492 | msrs->exit_ctls_low, |
| 6493 | msrs->exit_ctls_high); |
| 6494 | msrs->exit_ctls_low = |
| 6495 | VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR; |
| 6496 | |
| 6497 | msrs->exit_ctls_high &= |
| 6498 | #ifdef CONFIG_X86_64 |
| 6499 | VM_EXIT_HOST_ADDR_SPACE_SIZE | |
| 6500 | #endif |
Chenyi Qiang | efc8313 | 2020-08-28 16:56:18 +0800 | [diff] [blame] | 6501 | VM_EXIT_LOAD_IA32_PAT | VM_EXIT_SAVE_IA32_PAT | |
| 6502 | VM_EXIT_CLEAR_BNDCFGS | VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6503 | msrs->exit_ctls_high |= |
| 6504 | VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR | |
| 6505 | VM_EXIT_LOAD_IA32_EFER | VM_EXIT_SAVE_IA32_EFER | |
| 6506 | VM_EXIT_SAVE_VMX_PREEMPTION_TIMER | VM_EXIT_ACK_INTR_ON_EXIT; |
| 6507 | |
| 6508 | /* We support free control of debug control saving. */ |
| 6509 | msrs->exit_ctls_low &= ~VM_EXIT_SAVE_DEBUG_CONTROLS; |
| 6510 | |
| 6511 | /* entry controls */ |
| 6512 | rdmsr(MSR_IA32_VMX_ENTRY_CTLS, |
| 6513 | msrs->entry_ctls_low, |
| 6514 | msrs->entry_ctls_high); |
| 6515 | msrs->entry_ctls_low = |
| 6516 | VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR; |
| 6517 | msrs->entry_ctls_high &= |
| 6518 | #ifdef CONFIG_X86_64 |
| 6519 | VM_ENTRY_IA32E_MODE | |
| 6520 | #endif |
Chenyi Qiang | efc8313 | 2020-08-28 16:56:18 +0800 | [diff] [blame] | 6521 | VM_ENTRY_LOAD_IA32_PAT | VM_ENTRY_LOAD_BNDCFGS | |
| 6522 | VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6523 | msrs->entry_ctls_high |= |
| 6524 | (VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR | VM_ENTRY_LOAD_IA32_EFER); |
| 6525 | |
| 6526 | /* We support free control of debug control loading. */ |
| 6527 | msrs->entry_ctls_low &= ~VM_ENTRY_LOAD_DEBUG_CONTROLS; |
| 6528 | |
| 6529 | /* cpu-based controls */ |
| 6530 | rdmsr(MSR_IA32_VMX_PROCBASED_CTLS, |
| 6531 | msrs->procbased_ctls_low, |
| 6532 | msrs->procbased_ctls_high); |
| 6533 | msrs->procbased_ctls_low = |
| 6534 | CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR; |
| 6535 | msrs->procbased_ctls_high &= |
Xiaoyao Li | 9dadc2f | 2019-12-06 16:45:24 +0800 | [diff] [blame] | 6536 | CPU_BASED_INTR_WINDOW_EXITING | |
Xiaoyao Li | 5e3d394 | 2019-12-06 16:45:26 +0800 | [diff] [blame] | 6537 | CPU_BASED_NMI_WINDOW_EXITING | CPU_BASED_USE_TSC_OFFSETTING | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6538 | CPU_BASED_HLT_EXITING | CPU_BASED_INVLPG_EXITING | |
| 6539 | CPU_BASED_MWAIT_EXITING | CPU_BASED_CR3_LOAD_EXITING | |
| 6540 | CPU_BASED_CR3_STORE_EXITING | |
| 6541 | #ifdef CONFIG_X86_64 |
| 6542 | CPU_BASED_CR8_LOAD_EXITING | CPU_BASED_CR8_STORE_EXITING | |
| 6543 | #endif |
| 6544 | CPU_BASED_MOV_DR_EXITING | CPU_BASED_UNCOND_IO_EXITING | |
| 6545 | CPU_BASED_USE_IO_BITMAPS | CPU_BASED_MONITOR_TRAP_FLAG | |
| 6546 | CPU_BASED_MONITOR_EXITING | CPU_BASED_RDPMC_EXITING | |
| 6547 | CPU_BASED_RDTSC_EXITING | CPU_BASED_PAUSE_EXITING | |
| 6548 | CPU_BASED_TPR_SHADOW | CPU_BASED_ACTIVATE_SECONDARY_CONTROLS; |
| 6549 | /* |
| 6550 | * We can allow some features even when not supported by the |
| 6551 | * hardware. For example, L1 can specify an MSR bitmap - and we |
| 6552 | * can use it to avoid exits to L1 - even when L0 runs L2 |
| 6553 | * without MSR bitmaps. |
| 6554 | */ |
| 6555 | msrs->procbased_ctls_high |= |
| 6556 | CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR | |
| 6557 | CPU_BASED_USE_MSR_BITMAPS; |
| 6558 | |
| 6559 | /* We support free control of CR3 access interception. */ |
| 6560 | msrs->procbased_ctls_low &= |
| 6561 | ~(CPU_BASED_CR3_LOAD_EXITING | CPU_BASED_CR3_STORE_EXITING); |
| 6562 | |
| 6563 | /* |
| 6564 | * secondary cpu-based controls. Do not include those that |
Xiaoyao Li | 7c1b761 | 2020-07-09 12:34:25 +0800 | [diff] [blame] | 6565 | * depend on CPUID bits, they are added later by |
| 6566 | * vmx_vcpu_after_set_cpuid. |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6567 | */ |
Vitaly Kuznetsov | 6b1971c | 2019-02-07 11:42:14 +0100 | [diff] [blame] | 6568 | if (msrs->procbased_ctls_high & CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) |
| 6569 | rdmsr(MSR_IA32_VMX_PROCBASED_CTLS2, |
| 6570 | msrs->secondary_ctls_low, |
| 6571 | msrs->secondary_ctls_high); |
| 6572 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6573 | msrs->secondary_ctls_low = 0; |
| 6574 | msrs->secondary_ctls_high &= |
| 6575 | SECONDARY_EXEC_DESC | |
Sean Christopherson | 7f3603b | 2020-09-23 09:50:47 -0700 | [diff] [blame] | 6576 | SECONDARY_EXEC_ENABLE_RDTSCP | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6577 | SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE | |
Paolo Bonzini | 6defc59 | 2019-07-02 14:39:29 +0200 | [diff] [blame] | 6578 | SECONDARY_EXEC_WBINVD_EXITING | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6579 | SECONDARY_EXEC_APIC_REGISTER_VIRT | |
| 6580 | SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY | |
Paolo Bonzini | 6defc59 | 2019-07-02 14:39:29 +0200 | [diff] [blame] | 6581 | SECONDARY_EXEC_RDRAND_EXITING | |
| 6582 | SECONDARY_EXEC_ENABLE_INVPCID | |
| 6583 | SECONDARY_EXEC_RDSEED_EXITING | |
Ilias Stamatis | d041b5e | 2021-05-26 19:44:17 +0100 | [diff] [blame] | 6584 | SECONDARY_EXEC_XSAVES | |
| 6585 | SECONDARY_EXEC_TSC_SCALING; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6586 | |
| 6587 | /* |
| 6588 | * We can emulate "VMCS shadowing," even if the hardware |
| 6589 | * doesn't support it. |
| 6590 | */ |
| 6591 | msrs->secondary_ctls_high |= |
| 6592 | SECONDARY_EXEC_SHADOW_VMCS; |
| 6593 | |
| 6594 | if (enable_ept) { |
| 6595 | /* nested EPT: emulate EPT also to L1 */ |
| 6596 | msrs->secondary_ctls_high |= |
| 6597 | SECONDARY_EXEC_ENABLE_EPT; |
Sean Christopherson | bb1fcc7 | 2020-03-02 18:02:36 -0800 | [diff] [blame] | 6598 | msrs->ept_caps = |
| 6599 | VMX_EPT_PAGE_WALK_4_BIT | |
| 6600 | VMX_EPT_PAGE_WALK_5_BIT | |
| 6601 | VMX_EPTP_WB_BIT | |
Sean Christopherson | 96d4701 | 2020-03-02 18:02:40 -0800 | [diff] [blame] | 6602 | VMX_EPT_INVEPT_BIT | |
| 6603 | VMX_EPT_EXECUTE_ONLY_BIT; |
| 6604 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6605 | msrs->ept_caps &= ept_caps; |
| 6606 | msrs->ept_caps |= VMX_EPT_EXTENT_GLOBAL_BIT | |
| 6607 | VMX_EPT_EXTENT_CONTEXT_BIT | VMX_EPT_2MB_PAGE_BIT | |
| 6608 | VMX_EPT_1GB_PAGE_BIT; |
| 6609 | if (enable_ept_ad_bits) { |
| 6610 | msrs->secondary_ctls_high |= |
| 6611 | SECONDARY_EXEC_ENABLE_PML; |
| 6612 | msrs->ept_caps |= VMX_EPT_AD_BIT; |
| 6613 | } |
| 6614 | } |
| 6615 | |
| 6616 | if (cpu_has_vmx_vmfunc()) { |
| 6617 | msrs->secondary_ctls_high |= |
| 6618 | SECONDARY_EXEC_ENABLE_VMFUNC; |
| 6619 | /* |
| 6620 | * Advertise EPTP switching unconditionally |
| 6621 | * since we emulate it |
| 6622 | */ |
| 6623 | if (enable_ept) |
| 6624 | msrs->vmfunc_controls = |
| 6625 | VMX_VMFUNC_EPTP_SWITCHING; |
| 6626 | } |
| 6627 | |
| 6628 | /* |
| 6629 | * Old versions of KVM use the single-context version without |
| 6630 | * checking for support, so declare that it is supported even |
| 6631 | * though it is treated as global context. The alternative is |
| 6632 | * not failing the single-context invvpid, and it is worse. |
| 6633 | */ |
| 6634 | if (enable_vpid) { |
| 6635 | msrs->secondary_ctls_high |= |
| 6636 | SECONDARY_EXEC_ENABLE_VPID; |
| 6637 | msrs->vpid_caps = VMX_VPID_INVVPID_BIT | |
| 6638 | VMX_VPID_EXTENT_SUPPORTED_MASK; |
| 6639 | } |
| 6640 | |
| 6641 | if (enable_unrestricted_guest) |
| 6642 | msrs->secondary_ctls_high |= |
| 6643 | SECONDARY_EXEC_UNRESTRICTED_GUEST; |
| 6644 | |
| 6645 | if (flexpriority_enabled) |
| 6646 | msrs->secondary_ctls_high |= |
| 6647 | SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES; |
| 6648 | |
Sean Christopherson | 72add91 | 2021-04-12 16:21:42 +1200 | [diff] [blame] | 6649 | if (enable_sgx) |
| 6650 | msrs->secondary_ctls_high |= SECONDARY_EXEC_ENCLS_EXITING; |
| 6651 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6652 | /* miscellaneous data */ |
| 6653 | rdmsr(MSR_IA32_VMX_MISC, |
| 6654 | msrs->misc_low, |
| 6655 | msrs->misc_high); |
| 6656 | msrs->misc_low &= VMX_MISC_SAVE_EFER_LMA; |
| 6657 | msrs->misc_low |= |
| 6658 | MSR_IA32_VMX_MISC_VMWRITE_SHADOW_RO_FIELDS | |
| 6659 | VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE | |
Yadong Qi | bf0cd88 | 2020-11-06 14:51:22 +0800 | [diff] [blame] | 6660 | VMX_MISC_ACTIVITY_HLT | |
| 6661 | VMX_MISC_ACTIVITY_WAIT_SIPI; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6662 | msrs->misc_high = 0; |
| 6663 | |
| 6664 | /* |
| 6665 | * This MSR reports some information about VMX support. We |
| 6666 | * should return information about the VMX we emulate for the |
| 6667 | * guest, and the VMCS structure we give it - not about the |
| 6668 | * VMX support of the underlying hardware. |
| 6669 | */ |
| 6670 | msrs->basic = |
| 6671 | VMCS12_REVISION | |
| 6672 | VMX_BASIC_TRUE_CTLS | |
| 6673 | ((u64)VMCS12_SIZE << VMX_BASIC_VMCS_SIZE_SHIFT) | |
| 6674 | (VMX_BASIC_MEM_TYPE_WB << VMX_BASIC_MEM_TYPE_SHIFT); |
| 6675 | |
| 6676 | if (cpu_has_vmx_basic_inout()) |
| 6677 | msrs->basic |= VMX_BASIC_INOUT; |
| 6678 | |
| 6679 | /* |
| 6680 | * These MSRs specify bits which the guest must keep fixed on |
| 6681 | * while L1 is in VMXON mode (in L1's root mode, or running an L2). |
| 6682 | * We picked the standard core2 setting. |
| 6683 | */ |
| 6684 | #define VMXON_CR0_ALWAYSON (X86_CR0_PE | X86_CR0_PG | X86_CR0_NE) |
| 6685 | #define VMXON_CR4_ALWAYSON X86_CR4_VMXE |
| 6686 | msrs->cr0_fixed0 = VMXON_CR0_ALWAYSON; |
| 6687 | msrs->cr4_fixed0 = VMXON_CR4_ALWAYSON; |
| 6688 | |
| 6689 | /* These MSRs specify bits which the guest must keep fixed off. */ |
| 6690 | rdmsrl(MSR_IA32_VMX_CR0_FIXED1, msrs->cr0_fixed1); |
| 6691 | rdmsrl(MSR_IA32_VMX_CR4_FIXED1, msrs->cr4_fixed1); |
| 6692 | |
Sean Christopherson | ba1f824 | 2021-06-18 14:46:58 -0700 | [diff] [blame] | 6693 | msrs->vmcs_enum = nested_vmx_calc_vmcs_enum_msr(); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6694 | } |
| 6695 | |
| 6696 | void nested_vmx_hardware_unsetup(void) |
| 6697 | { |
| 6698 | int i; |
| 6699 | |
| 6700 | if (enable_shadow_vmcs) { |
| 6701 | for (i = 0; i < VMX_BITMAP_NR; i++) |
| 6702 | free_page((unsigned long)vmx_bitmap[i]); |
| 6703 | } |
| 6704 | } |
| 6705 | |
Sean Christopherson | 6c1c6e5 | 2020-05-06 13:46:53 -0700 | [diff] [blame] | 6706 | __init int nested_vmx_hardware_setup(int (*exit_handlers[])(struct kvm_vcpu *)) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6707 | { |
| 6708 | int i; |
| 6709 | |
| 6710 | if (!cpu_has_vmx_shadow_vmcs()) |
| 6711 | enable_shadow_vmcs = 0; |
| 6712 | if (enable_shadow_vmcs) { |
| 6713 | for (i = 0; i < VMX_BITMAP_NR; i++) { |
Ben Gardon | 4183683 | 2019-02-11 11:02:52 -0800 | [diff] [blame] | 6714 | /* |
| 6715 | * The vmx_bitmap is not tied to a VM and so should |
| 6716 | * not be charged to a memcg. |
| 6717 | */ |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6718 | vmx_bitmap[i] = (unsigned long *) |
| 6719 | __get_free_page(GFP_KERNEL); |
| 6720 | if (!vmx_bitmap[i]) { |
| 6721 | nested_vmx_hardware_unsetup(); |
| 6722 | return -ENOMEM; |
| 6723 | } |
| 6724 | } |
| 6725 | |
| 6726 | init_vmcs_shadow_fields(); |
| 6727 | } |
| 6728 | |
Liran Alon | cc87767 | 2019-11-18 21:11:21 +0200 | [diff] [blame] | 6729 | exit_handlers[EXIT_REASON_VMCLEAR] = handle_vmclear; |
| 6730 | exit_handlers[EXIT_REASON_VMLAUNCH] = handle_vmlaunch; |
| 6731 | exit_handlers[EXIT_REASON_VMPTRLD] = handle_vmptrld; |
| 6732 | exit_handlers[EXIT_REASON_VMPTRST] = handle_vmptrst; |
| 6733 | exit_handlers[EXIT_REASON_VMREAD] = handle_vmread; |
| 6734 | exit_handlers[EXIT_REASON_VMRESUME] = handle_vmresume; |
| 6735 | exit_handlers[EXIT_REASON_VMWRITE] = handle_vmwrite; |
| 6736 | exit_handlers[EXIT_REASON_VMOFF] = handle_vmoff; |
| 6737 | exit_handlers[EXIT_REASON_VMON] = handle_vmon; |
| 6738 | exit_handlers[EXIT_REASON_INVEPT] = handle_invept; |
| 6739 | exit_handlers[EXIT_REASON_INVVPID] = handle_invvpid; |
| 6740 | exit_handlers[EXIT_REASON_VMFUNC] = handle_vmfunc; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6741 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6742 | return 0; |
| 6743 | } |
Paolo Bonzini | 33b2217 | 2020-04-17 10:24:18 -0400 | [diff] [blame] | 6744 | |
| 6745 | struct kvm_x86_nested_ops vmx_nested_ops = { |
| 6746 | .check_events = vmx_check_nested_events, |
Sean Christopherson | d2060bd | 2020-04-22 19:25:39 -0700 | [diff] [blame] | 6747 | .hv_timer_pending = nested_vmx_preemption_timer_pending, |
Sean Christopherson | cb6a32c | 2021-03-02 09:45:14 -0800 | [diff] [blame] | 6748 | .triple_fault = nested_vmx_triple_fault, |
Paolo Bonzini | 33b2217 | 2020-04-17 10:24:18 -0400 | [diff] [blame] | 6749 | .get_state = vmx_get_nested_state, |
| 6750 | .set_state = vmx_set_nested_state, |
Paolo Bonzini | 9a78e15 | 2021-01-08 11:43:08 -0500 | [diff] [blame] | 6751 | .get_nested_state_pages = vmx_get_nested_state_pages, |
Sean Christopherson | 02f5fb2 | 2020-06-22 14:58:32 -0700 | [diff] [blame] | 6752 | .write_log_dirty = nested_vmx_write_pml_buffer, |
Paolo Bonzini | 33b2217 | 2020-04-17 10:24:18 -0400 | [diff] [blame] | 6753 | .enable_evmcs = nested_enable_evmcs, |
| 6754 | .get_evmcs_version = nested_get_evmcs_version, |
| 6755 | }; |