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 | */ |
Yu Zhang | 64c7850 | 2021-09-30 01:51:53 +0800 | [diff] [blame] | 194 | if (vmx->nested.current_vmptr == INVALID_GPA && |
Vitaly Kuznetsov | 1e9dfbd | 2021-05-26 15:20:16 +0200 | [diff] [blame] | 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); |
Yu Zhang | 64c7850 | 2021-09-30 01:51:53 +0800 | [diff] [blame] | 221 | vmcs_write64(VMCS_LINK_POINTER, INVALID_GPA); |
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 | |
Paolo Bonzini | 41e68b6 | 2021-11-26 07:00:15 -0500 | [diff] [blame] | 272 | vcpu->arch.regs_avail = ~VMX_REGS_LAZY_LOAD_SET; |
| 273 | |
| 274 | /* |
| 275 | * All lazily updated registers will be reloaded from VMCS12 on both |
| 276 | * vmentry and vmexit. |
| 277 | */ |
| 278 | vcpu->arch.regs_dirty = 0; |
Sean Christopherson | c61ca2f | 2020-09-23 11:44:49 -0700 | [diff] [blame] | 279 | } |
| 280 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 281 | /* |
| 282 | * Free whatever needs to be freed from vmx->nested when L1 goes down, or |
| 283 | * just stops using VMX. |
| 284 | */ |
| 285 | static void free_nested(struct kvm_vcpu *vcpu) |
| 286 | { |
| 287 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 288 | |
Sean Christopherson | df82a24 | 2020-09-23 11:44:50 -0700 | [diff] [blame] | 289 | if (WARN_ON_ONCE(vmx->loaded_vmcs != &vmx->vmcs01)) |
| 290 | vmx_switch_vmcs(vcpu, &vmx->vmcs01); |
| 291 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 292 | if (!vmx->nested.vmxon && !vmx->nested.smm.vmxon) |
| 293 | return; |
| 294 | |
Paolo Bonzini | 729c15c | 2020-09-22 06:53:57 -0400 | [diff] [blame] | 295 | kvm_clear_request(KVM_REQ_GET_NESTED_STATE_PAGES, vcpu); |
Jan Kiszka | cf64527 | 2019-07-21 13:52:18 +0200 | [diff] [blame] | 296 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 297 | vmx->nested.vmxon = false; |
| 298 | vmx->nested.smm.vmxon = false; |
Vitaly Kuznetsov | feb3162 | 2021-09-30 01:51:54 +0800 | [diff] [blame] | 299 | vmx->nested.vmxon_ptr = INVALID_GPA; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 300 | free_vpid(vmx->nested.vpid02); |
| 301 | vmx->nested.posted_intr_nv = -1; |
Yu Zhang | 64c7850 | 2021-09-30 01:51:53 +0800 | [diff] [blame] | 302 | vmx->nested.current_vmptr = INVALID_GPA; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 303 | if (enable_shadow_vmcs) { |
| 304 | vmx_disable_shadow_vmcs(vmx); |
| 305 | vmcs_clear(vmx->vmcs01.shadow_vmcs); |
| 306 | free_vmcs(vmx->vmcs01.shadow_vmcs); |
| 307 | vmx->vmcs01.shadow_vmcs = NULL; |
| 308 | } |
| 309 | kfree(vmx->nested.cached_vmcs12); |
Jan Kiszka | c6bf2ae | 2019-07-21 16:01:36 +0200 | [diff] [blame] | 310 | vmx->nested.cached_vmcs12 = NULL; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 311 | kfree(vmx->nested.cached_shadow_vmcs12); |
Jan Kiszka | c6bf2ae | 2019-07-21 16:01:36 +0200 | [diff] [blame] | 312 | vmx->nested.cached_shadow_vmcs12 = NULL; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 313 | /* Unpin physical memory we referred to in the vmcs02 */ |
| 314 | if (vmx->nested.apic_access_page) { |
Liran Alon | b11494b | 2019-11-21 00:31:47 +0200 | [diff] [blame] | 315 | kvm_release_page_clean(vmx->nested.apic_access_page); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 316 | vmx->nested.apic_access_page = NULL; |
| 317 | } |
KarimAllah Ahmed | 96c66e8 | 2019-01-31 21:24:37 +0100 | [diff] [blame] | 318 | kvm_vcpu_unmap(vcpu, &vmx->nested.virtual_apic_map, true); |
KarimAllah Ahmed | 3278e04 | 2019-01-31 21:24:38 +0100 | [diff] [blame] | 319 | kvm_vcpu_unmap(vcpu, &vmx->nested.pi_desc_map, true); |
| 320 | vmx->nested.pi_desc = NULL; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 321 | |
| 322 | kvm_mmu_free_roots(vcpu, &vcpu->arch.guest_mmu, KVM_MMU_ROOTS_ALL); |
| 323 | |
| 324 | nested_release_evmcs(vcpu); |
| 325 | |
| 326 | free_loaded_vmcs(&vmx->nested.vmcs02); |
| 327 | } |
| 328 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 329 | /* |
| 330 | * Ensure that the current vmcs of the logical processor is the |
| 331 | * vmcs01 of the vcpu before calling free_nested(). |
| 332 | */ |
| 333 | void nested_vmx_free_vcpu(struct kvm_vcpu *vcpu) |
| 334 | { |
| 335 | vcpu_load(vcpu); |
Paolo Bonzini | b4b65b5 | 2019-01-29 19:12:35 +0100 | [diff] [blame] | 336 | vmx_leave_nested(vcpu); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 337 | vcpu_put(vcpu); |
| 338 | } |
| 339 | |
Junaid Shahid | 85aa888 | 2021-08-06 15:22:29 -0700 | [diff] [blame] | 340 | #define EPTP_PA_MASK GENMASK_ULL(51, 12) |
| 341 | |
| 342 | static bool nested_ept_root_matches(hpa_t root_hpa, u64 root_eptp, u64 eptp) |
| 343 | { |
| 344 | return VALID_PAGE(root_hpa) && |
| 345 | ((root_eptp & EPTP_PA_MASK) == (eptp & EPTP_PA_MASK)); |
| 346 | } |
| 347 | |
| 348 | static void nested_ept_invalidate_addr(struct kvm_vcpu *vcpu, gpa_t eptp, |
| 349 | gpa_t addr) |
| 350 | { |
| 351 | uint i; |
| 352 | struct kvm_mmu_root_info *cached_root; |
| 353 | |
| 354 | WARN_ON_ONCE(!mmu_is_nested(vcpu)); |
| 355 | |
| 356 | for (i = 0; i < KVM_MMU_NUM_PREV_ROOTS; i++) { |
| 357 | cached_root = &vcpu->arch.mmu->prev_roots[i]; |
| 358 | |
| 359 | if (nested_ept_root_matches(cached_root->hpa, cached_root->pgd, |
| 360 | eptp)) |
| 361 | vcpu->arch.mmu->invlpg(vcpu, addr, cached_root->hpa); |
| 362 | } |
| 363 | } |
| 364 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 365 | static void nested_ept_inject_page_fault(struct kvm_vcpu *vcpu, |
| 366 | struct x86_exception *fault) |
| 367 | { |
| 368 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
| 369 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Sean Christopherson | 4dcefa3 | 2020-04-15 10:55:18 -0700 | [diff] [blame] | 370 | u32 vm_exit_reason; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 371 | unsigned long exit_qualification = vcpu->arch.exit_qualification; |
| 372 | |
| 373 | if (vmx->nested.pml_full) { |
Sean Christopherson | 4dcefa3 | 2020-04-15 10:55:18 -0700 | [diff] [blame] | 374 | vm_exit_reason = EXIT_REASON_PML_FULL; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 375 | vmx->nested.pml_full = false; |
| 376 | exit_qualification &= INTR_INFO_UNBLOCK_NMI; |
Junaid Shahid | 85aa888 | 2021-08-06 15:22:29 -0700 | [diff] [blame] | 377 | } else { |
| 378 | if (fault->error_code & PFERR_RSVD_MASK) |
| 379 | vm_exit_reason = EXIT_REASON_EPT_MISCONFIG; |
| 380 | else |
| 381 | vm_exit_reason = EXIT_REASON_EPT_VIOLATION; |
| 382 | |
| 383 | /* |
| 384 | * Although the caller (kvm_inject_emulated_page_fault) would |
| 385 | * have already synced the faulting address in the shadow EPT |
| 386 | * tables for the current EPTP12, we also need to sync it for |
| 387 | * any other cached EPTP02s based on the same EP4TA, since the |
| 388 | * TLB associates mappings to the EP4TA rather than the full EPTP. |
| 389 | */ |
| 390 | nested_ept_invalidate_addr(vcpu, vmcs12->ept_pointer, |
| 391 | fault->address); |
| 392 | } |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 393 | |
Sean Christopherson | 4dcefa3 | 2020-04-15 10:55:18 -0700 | [diff] [blame] | 394 | nested_vmx_vmexit(vcpu, vm_exit_reason, 0, exit_qualification); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 395 | vmcs12->guest_physical_address = fault->address; |
| 396 | } |
| 397 | |
Sean Christopherson | 39353ab | 2021-06-09 16:42:31 -0700 | [diff] [blame] | 398 | static void nested_ept_new_eptp(struct kvm_vcpu *vcpu) |
| 399 | { |
Lai Jiangshan | cc022ae | 2021-11-24 20:20:49 +0800 | [diff] [blame] | 400 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 401 | bool execonly = vmx->nested.msrs.ept_caps & VMX_EPT_EXECUTE_ONLY_BIT; |
| 402 | int ept_lpage_level = ept_caps_to_lpage_level(vmx->nested.msrs.ept_caps); |
| 403 | |
| 404 | kvm_init_shadow_ept_mmu(vcpu, execonly, ept_lpage_level, |
Sean Christopherson | 39353ab | 2021-06-09 16:42:31 -0700 | [diff] [blame] | 405 | nested_ept_ad_enabled(vcpu), |
| 406 | nested_ept_get_eptp(vcpu)); |
| 407 | } |
| 408 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 409 | static void nested_ept_init_mmu_context(struct kvm_vcpu *vcpu) |
| 410 | { |
| 411 | WARN_ON(mmu_is_nested(vcpu)); |
| 412 | |
| 413 | vcpu->arch.mmu = &vcpu->arch.guest_mmu; |
Sean Christopherson | 39353ab | 2021-06-09 16:42:31 -0700 | [diff] [blame] | 414 | nested_ept_new_eptp(vcpu); |
Sean Christopherson | d8dd54e | 2020-03-02 18:02:39 -0800 | [diff] [blame] | 415 | vcpu->arch.mmu->get_guest_pgd = nested_ept_get_eptp; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 416 | vcpu->arch.mmu->inject_page_fault = nested_ept_inject_page_fault; |
| 417 | vcpu->arch.mmu->get_pdptr = kvm_pdptr_read; |
| 418 | |
| 419 | vcpu->arch.walk_mmu = &vcpu->arch.nested_mmu; |
| 420 | } |
| 421 | |
| 422 | static void nested_ept_uninit_mmu_context(struct kvm_vcpu *vcpu) |
| 423 | { |
| 424 | vcpu->arch.mmu = &vcpu->arch.root_mmu; |
| 425 | vcpu->arch.walk_mmu = &vcpu->arch.root_mmu; |
| 426 | } |
| 427 | |
| 428 | static bool nested_vmx_is_page_fault_vmexit(struct vmcs12 *vmcs12, |
| 429 | u16 error_code) |
| 430 | { |
| 431 | bool inequality, bit; |
| 432 | |
| 433 | bit = (vmcs12->exception_bitmap & (1u << PF_VECTOR)) != 0; |
| 434 | inequality = |
| 435 | (error_code & vmcs12->page_fault_error_code_mask) != |
| 436 | vmcs12->page_fault_error_code_match; |
| 437 | return inequality ^ bit; |
| 438 | } |
| 439 | |
| 440 | |
| 441 | /* |
| 442 | * KVM wants to inject page-faults which it got to the guest. This function |
| 443 | * checks whether in a nested guest, we need to inject them to L1 or L2. |
| 444 | */ |
| 445 | static int nested_vmx_check_exception(struct kvm_vcpu *vcpu, unsigned long *exit_qual) |
| 446 | { |
| 447 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
| 448 | unsigned int nr = vcpu->arch.exception.nr; |
| 449 | bool has_payload = vcpu->arch.exception.has_payload; |
| 450 | unsigned long payload = vcpu->arch.exception.payload; |
| 451 | |
| 452 | if (nr == PF_VECTOR) { |
| 453 | if (vcpu->arch.exception.nested_apf) { |
| 454 | *exit_qual = vcpu->arch.apf.nested_apf_token; |
| 455 | return 1; |
| 456 | } |
| 457 | if (nested_vmx_is_page_fault_vmexit(vmcs12, |
| 458 | vcpu->arch.exception.error_code)) { |
| 459 | *exit_qual = has_payload ? payload : vcpu->arch.cr2; |
| 460 | return 1; |
| 461 | } |
| 462 | } else if (vmcs12->exception_bitmap & (1u << nr)) { |
| 463 | if (nr == DB_VECTOR) { |
| 464 | if (!has_payload) { |
| 465 | payload = vcpu->arch.dr6; |
Chenyi Qiang | 9a3ecd5 | 2021-02-02 17:04:31 +0800 | [diff] [blame] | 466 | payload &= ~DR6_BT; |
| 467 | payload ^= DR6_ACTIVE_LOW; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 468 | } |
| 469 | *exit_qual = payload; |
| 470 | } else |
| 471 | *exit_qual = 0; |
| 472 | return 1; |
| 473 | } |
| 474 | |
| 475 | return 0; |
| 476 | } |
| 477 | |
| 478 | |
| 479 | static void vmx_inject_page_fault_nested(struct kvm_vcpu *vcpu, |
| 480 | struct x86_exception *fault) |
| 481 | { |
| 482 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
| 483 | |
| 484 | WARN_ON(!is_guest_mode(vcpu)); |
| 485 | |
| 486 | if (nested_vmx_is_page_fault_vmexit(vmcs12, fault->error_code) && |
| 487 | !to_vmx(vcpu)->nested.nested_run_pending) { |
| 488 | vmcs12->vm_exit_intr_error_code = fault->error_code; |
| 489 | nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI, |
| 490 | PF_VECTOR | INTR_TYPE_HARD_EXCEPTION | |
| 491 | INTR_INFO_DELIVER_CODE_MASK | INTR_INFO_VALID_MASK, |
| 492 | fault->address); |
| 493 | } else { |
| 494 | kvm_inject_page_fault(vcpu, fault); |
| 495 | } |
| 496 | } |
| 497 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 498 | static int nested_vmx_check_io_bitmap_controls(struct kvm_vcpu *vcpu, |
| 499 | struct vmcs12 *vmcs12) |
| 500 | { |
| 501 | if (!nested_cpu_has(vmcs12, CPU_BASED_USE_IO_BITMAPS)) |
| 502 | return 0; |
| 503 | |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 504 | if (CC(!page_address_valid(vcpu, vmcs12->io_bitmap_a)) || |
| 505 | CC(!page_address_valid(vcpu, vmcs12->io_bitmap_b))) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 506 | return -EINVAL; |
| 507 | |
| 508 | return 0; |
| 509 | } |
| 510 | |
| 511 | static int nested_vmx_check_msr_bitmap_controls(struct kvm_vcpu *vcpu, |
| 512 | struct vmcs12 *vmcs12) |
| 513 | { |
| 514 | if (!nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS)) |
| 515 | return 0; |
| 516 | |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 517 | if (CC(!page_address_valid(vcpu, vmcs12->msr_bitmap))) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 518 | return -EINVAL; |
| 519 | |
| 520 | return 0; |
| 521 | } |
| 522 | |
| 523 | static int nested_vmx_check_tpr_shadow_controls(struct kvm_vcpu *vcpu, |
| 524 | struct vmcs12 *vmcs12) |
| 525 | { |
| 526 | if (!nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW)) |
| 527 | return 0; |
| 528 | |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 529 | if (CC(!page_address_valid(vcpu, vmcs12->virtual_apic_page_addr))) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 530 | return -EINVAL; |
| 531 | |
| 532 | return 0; |
| 533 | } |
| 534 | |
| 535 | /* |
Sean Christopherson | a5e0c25 | 2021-11-09 01:30:47 +0000 | [diff] [blame] | 536 | * For x2APIC MSRs, ignore the vmcs01 bitmap. L1 can enable x2APIC without L1 |
| 537 | * itself utilizing x2APIC. All MSRs were previously set to be intercepted, |
| 538 | * only the "disable intercept" case needs to be handled. |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 539 | */ |
Sean Christopherson | a5e0c25 | 2021-11-09 01:30:47 +0000 | [diff] [blame] | 540 | static void nested_vmx_disable_intercept_for_x2apic_msr(unsigned long *msr_bitmap_l1, |
| 541 | unsigned long *msr_bitmap_l0, |
| 542 | u32 msr, int type) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 543 | { |
Sean Christopherson | a5e0c25 | 2021-11-09 01:30:47 +0000 | [diff] [blame] | 544 | if (type & MSR_TYPE_R && !vmx_test_msr_bitmap_read(msr_bitmap_l1, msr)) |
| 545 | vmx_clear_msr_bitmap_read(msr_bitmap_l0, msr); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 546 | |
Sean Christopherson | a5e0c25 | 2021-11-09 01:30:47 +0000 | [diff] [blame] | 547 | if (type & MSR_TYPE_W && !vmx_test_msr_bitmap_write(msr_bitmap_l1, msr)) |
| 548 | vmx_clear_msr_bitmap_write(msr_bitmap_l0, msr); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 549 | } |
| 550 | |
Miaohe Lin | ffdbd50 | 2020-02-07 23:22:45 +0800 | [diff] [blame] | 551 | static inline void enable_x2apic_msr_intercepts(unsigned long *msr_bitmap) |
| 552 | { |
Marc Orr | acff784 | 2019-04-01 23:55:59 -0700 | [diff] [blame] | 553 | int msr; |
| 554 | |
| 555 | for (msr = 0x800; msr <= 0x8ff; msr += BITS_PER_LONG) { |
| 556 | unsigned word = msr / BITS_PER_LONG; |
| 557 | |
| 558 | msr_bitmap[word] = ~0; |
| 559 | msr_bitmap[word + (0x800 / sizeof(long))] = ~0; |
| 560 | } |
| 561 | } |
| 562 | |
Sean Christopherson | 67f4b99 | 2021-11-09 01:30:45 +0000 | [diff] [blame] | 563 | #define BUILD_NVMX_MSR_INTERCEPT_HELPER(rw) \ |
| 564 | static inline \ |
| 565 | void nested_vmx_set_msr_##rw##_intercept(struct vcpu_vmx *vmx, \ |
| 566 | unsigned long *msr_bitmap_l1, \ |
| 567 | unsigned long *msr_bitmap_l0, u32 msr) \ |
| 568 | { \ |
| 569 | if (vmx_test_msr_bitmap_##rw(vmx->vmcs01.msr_bitmap, msr) || \ |
| 570 | vmx_test_msr_bitmap_##rw(msr_bitmap_l1, msr)) \ |
| 571 | vmx_set_msr_bitmap_##rw(msr_bitmap_l0, msr); \ |
| 572 | else \ |
| 573 | vmx_clear_msr_bitmap_##rw(msr_bitmap_l0, msr); \ |
| 574 | } |
| 575 | BUILD_NVMX_MSR_INTERCEPT_HELPER(read) |
| 576 | BUILD_NVMX_MSR_INTERCEPT_HELPER(write) |
| 577 | |
| 578 | static inline void nested_vmx_set_intercept_for_msr(struct vcpu_vmx *vmx, |
| 579 | unsigned long *msr_bitmap_l1, |
| 580 | unsigned long *msr_bitmap_l0, |
| 581 | u32 msr, int types) |
| 582 | { |
| 583 | if (types & MSR_TYPE_R) |
| 584 | nested_vmx_set_msr_read_intercept(vmx, msr_bitmap_l1, |
| 585 | msr_bitmap_l0, msr); |
| 586 | if (types & MSR_TYPE_W) |
| 587 | nested_vmx_set_msr_write_intercept(vmx, msr_bitmap_l1, |
| 588 | msr_bitmap_l0, msr); |
| 589 | } |
| 590 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 591 | /* |
| 592 | * Merge L0's and L1's MSR bitmap, return false to indicate that |
| 593 | * we do not use the hardware. |
| 594 | */ |
| 595 | static inline bool nested_vmx_prepare_msr_bitmap(struct kvm_vcpu *vcpu, |
| 596 | struct vmcs12 *vmcs12) |
| 597 | { |
Sean Christopherson | 67f4b99 | 2021-11-09 01:30:45 +0000 | [diff] [blame] | 598 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 599 | int msr; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 600 | unsigned long *msr_bitmap_l1; |
Sean Christopherson | 67f4b99 | 2021-11-09 01:30:45 +0000 | [diff] [blame] | 601 | unsigned long *msr_bitmap_l0 = vmx->nested.vmcs02.msr_bitmap; |
| 602 | struct kvm_host_map *map = &vmx->nested.msr_bitmap_map; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 603 | |
| 604 | /* Nothing to do if the MSR bitmap is not in use. */ |
| 605 | if (!cpu_has_vmx_msr_bitmap() || |
| 606 | !nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS)) |
| 607 | return false; |
| 608 | |
KarimAllah Ahmed | 31f0b6c | 2019-01-31 21:24:36 +0100 | [diff] [blame] | 609 | if (kvm_vcpu_map(vcpu, gpa_to_gfn(vmcs12->msr_bitmap), map)) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 610 | return false; |
| 611 | |
KarimAllah Ahmed | 31f0b6c | 2019-01-31 21:24:36 +0100 | [diff] [blame] | 612 | msr_bitmap_l1 = (unsigned long *)map->hva; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 613 | |
Marc Orr | acff784 | 2019-04-01 23:55:59 -0700 | [diff] [blame] | 614 | /* |
| 615 | * To keep the control flow simple, pay eight 8-byte writes (sixteen |
| 616 | * 4-byte writes on 32-bit systems) up front to enable intercepts for |
Sean Christopherson | a5e0c25 | 2021-11-09 01:30:47 +0000 | [diff] [blame] | 617 | * the x2APIC MSR range and selectively toggle those relevant to L2. |
Marc Orr | acff784 | 2019-04-01 23:55:59 -0700 | [diff] [blame] | 618 | */ |
| 619 | enable_x2apic_msr_intercepts(msr_bitmap_l0); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 620 | |
Marc Orr | acff784 | 2019-04-01 23:55:59 -0700 | [diff] [blame] | 621 | if (nested_cpu_has_virt_x2apic_mode(vmcs12)) { |
| 622 | if (nested_cpu_has_apic_reg_virt(vmcs12)) { |
| 623 | /* |
| 624 | * L0 need not intercept reads for MSRs between 0x800 |
| 625 | * and 0x8ff, it just lets the processor take the value |
| 626 | * from the virtual-APIC page; take those 256 bits |
| 627 | * directly from the L1 bitmap. |
| 628 | */ |
| 629 | for (msr = 0x800; msr <= 0x8ff; msr += BITS_PER_LONG) { |
| 630 | unsigned word = msr / BITS_PER_LONG; |
| 631 | |
| 632 | msr_bitmap_l0[word] = msr_bitmap_l1[word]; |
| 633 | } |
| 634 | } |
| 635 | |
Sean Christopherson | a5e0c25 | 2021-11-09 01:30:47 +0000 | [diff] [blame] | 636 | nested_vmx_disable_intercept_for_x2apic_msr( |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 637 | msr_bitmap_l1, msr_bitmap_l0, |
Marc Orr | acff784 | 2019-04-01 23:55:59 -0700 | [diff] [blame] | 638 | X2APIC_MSR(APIC_TASKPRI), |
Marc Orr | c73f4c9 | 2019-04-01 23:56:00 -0700 | [diff] [blame] | 639 | MSR_TYPE_R | MSR_TYPE_W); |
Marc Orr | acff784 | 2019-04-01 23:55:59 -0700 | [diff] [blame] | 640 | |
| 641 | if (nested_cpu_has_vid(vmcs12)) { |
Sean Christopherson | a5e0c25 | 2021-11-09 01:30:47 +0000 | [diff] [blame] | 642 | nested_vmx_disable_intercept_for_x2apic_msr( |
Marc Orr | acff784 | 2019-04-01 23:55:59 -0700 | [diff] [blame] | 643 | msr_bitmap_l1, msr_bitmap_l0, |
| 644 | X2APIC_MSR(APIC_EOI), |
| 645 | MSR_TYPE_W); |
Sean Christopherson | a5e0c25 | 2021-11-09 01:30:47 +0000 | [diff] [blame] | 646 | nested_vmx_disable_intercept_for_x2apic_msr( |
Marc Orr | acff784 | 2019-04-01 23:55:59 -0700 | [diff] [blame] | 647 | msr_bitmap_l1, msr_bitmap_l0, |
| 648 | X2APIC_MSR(APIC_SELF_IPI), |
| 649 | MSR_TYPE_W); |
| 650 | } |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 651 | } |
| 652 | |
Sean Christopherson | d69129b | 2019-05-08 07:32:15 -0700 | [diff] [blame] | 653 | /* |
Sean Christopherson | 67f4b99 | 2021-11-09 01:30:45 +0000 | [diff] [blame] | 654 | * Always check vmcs01's bitmap to honor userspace MSR filters and any |
| 655 | * other runtime changes to vmcs01's bitmap, e.g. dynamic pass-through. |
Sean Christopherson | d69129b | 2019-05-08 07:32:15 -0700 | [diff] [blame] | 656 | */ |
Sean Christopherson | 67f4b99 | 2021-11-09 01:30:45 +0000 | [diff] [blame] | 657 | #ifdef CONFIG_X86_64 |
| 658 | nested_vmx_set_intercept_for_msr(vmx, msr_bitmap_l1, msr_bitmap_l0, |
| 659 | MSR_FS_BASE, MSR_TYPE_RW); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 660 | |
Sean Christopherson | 67f4b99 | 2021-11-09 01:30:45 +0000 | [diff] [blame] | 661 | nested_vmx_set_intercept_for_msr(vmx, msr_bitmap_l1, msr_bitmap_l0, |
| 662 | MSR_GS_BASE, MSR_TYPE_RW); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 663 | |
Sean Christopherson | 67f4b99 | 2021-11-09 01:30:45 +0000 | [diff] [blame] | 664 | nested_vmx_set_intercept_for_msr(vmx, msr_bitmap_l1, msr_bitmap_l0, |
| 665 | MSR_KERNEL_GS_BASE, MSR_TYPE_RW); |
| 666 | #endif |
| 667 | nested_vmx_set_intercept_for_msr(vmx, msr_bitmap_l1, msr_bitmap_l0, |
| 668 | MSR_IA32_SPEC_CTRL, MSR_TYPE_RW); |
| 669 | |
| 670 | nested_vmx_set_intercept_for_msr(vmx, msr_bitmap_l1, msr_bitmap_l0, |
| 671 | MSR_IA32_PRED_CMD, MSR_TYPE_W); |
| 672 | |
| 673 | kvm_vcpu_unmap(vcpu, &vmx->nested.msr_bitmap_map, false); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 674 | |
Vitaly Kuznetsov | ed2a480 | 2021-11-29 10:47:03 +0100 | [diff] [blame^] | 675 | vmx->nested.force_msr_bitmap_recalc = false; |
| 676 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 677 | return true; |
| 678 | } |
| 679 | |
| 680 | static void nested_cache_shadow_vmcs12(struct kvm_vcpu *vcpu, |
| 681 | struct vmcs12 *vmcs12) |
| 682 | { |
David Woodhouse | 297d597 | 2021-11-15 16:50:24 +0000 | [diff] [blame] | 683 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 684 | struct gfn_to_hva_cache *ghc = &vmx->nested.shadow_vmcs12_cache; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 685 | |
| 686 | if (!nested_cpu_has_shadow_vmcs(vmcs12) || |
Yu Zhang | 64c7850 | 2021-09-30 01:51:53 +0800 | [diff] [blame] | 687 | vmcs12->vmcs_link_pointer == INVALID_GPA) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 688 | return; |
| 689 | |
David Woodhouse | 297d597 | 2021-11-15 16:50:24 +0000 | [diff] [blame] | 690 | if (ghc->gpa != vmcs12->vmcs_link_pointer && |
| 691 | kvm_gfn_to_hva_cache_init(vcpu->kvm, ghc, |
| 692 | vmcs12->vmcs_link_pointer, VMCS12_SIZE)) |
KarimAllah Ahmed | 8892530 | 2019-01-31 21:24:41 +0100 | [diff] [blame] | 693 | return; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 694 | |
David Woodhouse | 297d597 | 2021-11-15 16:50:24 +0000 | [diff] [blame] | 695 | kvm_read_guest_cached(vmx->vcpu.kvm, ghc, get_shadow_vmcs12(vcpu), |
| 696 | VMCS12_SIZE); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 697 | } |
| 698 | |
| 699 | static void nested_flush_cached_shadow_vmcs12(struct kvm_vcpu *vcpu, |
| 700 | struct vmcs12 *vmcs12) |
| 701 | { |
| 702 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
David Woodhouse | 297d597 | 2021-11-15 16:50:24 +0000 | [diff] [blame] | 703 | struct gfn_to_hva_cache *ghc = &vmx->nested.shadow_vmcs12_cache; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 704 | |
| 705 | if (!nested_cpu_has_shadow_vmcs(vmcs12) || |
Yu Zhang | 64c7850 | 2021-09-30 01:51:53 +0800 | [diff] [blame] | 706 | vmcs12->vmcs_link_pointer == INVALID_GPA) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 707 | return; |
| 708 | |
David Woodhouse | 297d597 | 2021-11-15 16:50:24 +0000 | [diff] [blame] | 709 | if (ghc->gpa != vmcs12->vmcs_link_pointer && |
| 710 | kvm_gfn_to_hva_cache_init(vcpu->kvm, ghc, |
| 711 | vmcs12->vmcs_link_pointer, VMCS12_SIZE)) |
| 712 | return; |
| 713 | |
| 714 | kvm_write_guest_cached(vmx->vcpu.kvm, ghc, get_shadow_vmcs12(vcpu), |
| 715 | VMCS12_SIZE); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 716 | } |
| 717 | |
| 718 | /* |
| 719 | * In nested virtualization, check if L1 has set |
| 720 | * VM_EXIT_ACK_INTR_ON_EXIT |
| 721 | */ |
| 722 | static bool nested_exit_intr_ack_set(struct kvm_vcpu *vcpu) |
| 723 | { |
| 724 | return get_vmcs12(vcpu)->vm_exit_controls & |
| 725 | VM_EXIT_ACK_INTR_ON_EXIT; |
| 726 | } |
| 727 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 728 | static int nested_vmx_check_apic_access_controls(struct kvm_vcpu *vcpu, |
| 729 | struct vmcs12 *vmcs12) |
| 730 | { |
| 731 | if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES) && |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 732 | CC(!page_address_valid(vcpu, vmcs12->apic_access_addr))) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 733 | return -EINVAL; |
| 734 | else |
| 735 | return 0; |
| 736 | } |
| 737 | |
| 738 | static int nested_vmx_check_apicv_controls(struct kvm_vcpu *vcpu, |
| 739 | struct vmcs12 *vmcs12) |
| 740 | { |
| 741 | if (!nested_cpu_has_virt_x2apic_mode(vmcs12) && |
| 742 | !nested_cpu_has_apic_reg_virt(vmcs12) && |
| 743 | !nested_cpu_has_vid(vmcs12) && |
| 744 | !nested_cpu_has_posted_intr(vmcs12)) |
| 745 | return 0; |
| 746 | |
| 747 | /* |
| 748 | * If virtualize x2apic mode is enabled, |
| 749 | * virtualize apic access must be disabled. |
| 750 | */ |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 751 | if (CC(nested_cpu_has_virt_x2apic_mode(vmcs12) && |
| 752 | nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES))) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 753 | return -EINVAL; |
| 754 | |
| 755 | /* |
| 756 | * If virtual interrupt delivery is enabled, |
| 757 | * we must exit on external interrupts. |
| 758 | */ |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 759 | if (CC(nested_cpu_has_vid(vmcs12) && !nested_exit_on_intr(vcpu))) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 760 | return -EINVAL; |
| 761 | |
| 762 | /* |
| 763 | * bits 15:8 should be zero in posted_intr_nv, |
| 764 | * the descriptor address has been already checked |
| 765 | * in nested_get_vmcs12_pages. |
| 766 | * |
| 767 | * bits 5:0 of posted_intr_desc_addr should be zero. |
| 768 | */ |
| 769 | if (nested_cpu_has_posted_intr(vmcs12) && |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 770 | (CC(!nested_cpu_has_vid(vmcs12)) || |
| 771 | CC(!nested_exit_intr_ack_set(vcpu)) || |
| 772 | CC((vmcs12->posted_intr_nv & 0xff00)) || |
Sean Christopherson | 636e8b7 | 2021-02-03 16:01:10 -0800 | [diff] [blame] | 773 | 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] | 774 | return -EINVAL; |
| 775 | |
| 776 | /* tpr shadow is needed by all apicv features. */ |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 777 | if (CC(!nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW))) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 778 | return -EINVAL; |
| 779 | |
| 780 | return 0; |
| 781 | } |
| 782 | |
| 783 | static int nested_vmx_check_msr_switch(struct kvm_vcpu *vcpu, |
Sean Christopherson | f9b245e | 2018-12-12 13:30:08 -0500 | [diff] [blame] | 784 | u32 count, u64 addr) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 785 | { |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 786 | if (count == 0) |
| 787 | return 0; |
Sean Christopherson | 636e8b7 | 2021-02-03 16:01:10 -0800 | [diff] [blame] | 788 | |
| 789 | if (!kvm_vcpu_is_legal_aligned_gpa(vcpu, addr, 16) || |
| 790 | !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] | 791 | return -EINVAL; |
Sean Christopherson | f9b245e | 2018-12-12 13:30:08 -0500 | [diff] [blame] | 792 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 793 | return 0; |
| 794 | } |
| 795 | |
Krish Sadhukhan | 61446ba | 2018-12-12 13:30:09 -0500 | [diff] [blame] | 796 | static int nested_vmx_check_exit_msr_switch_controls(struct kvm_vcpu *vcpu, |
| 797 | struct vmcs12 *vmcs12) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 798 | { |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 799 | if (CC(nested_vmx_check_msr_switch(vcpu, |
| 800 | vmcs12->vm_exit_msr_load_count, |
| 801 | vmcs12->vm_exit_msr_load_addr)) || |
| 802 | CC(nested_vmx_check_msr_switch(vcpu, |
| 803 | vmcs12->vm_exit_msr_store_count, |
| 804 | vmcs12->vm_exit_msr_store_addr))) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 805 | return -EINVAL; |
Sean Christopherson | f9b245e | 2018-12-12 13:30:08 -0500 | [diff] [blame] | 806 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 807 | return 0; |
| 808 | } |
| 809 | |
Krish Sadhukhan | 5fbf963 | 2018-12-12 13:30:10 -0500 | [diff] [blame] | 810 | static int nested_vmx_check_entry_msr_switch_controls(struct kvm_vcpu *vcpu, |
| 811 | struct vmcs12 *vmcs12) |
Krish Sadhukhan | 61446ba | 2018-12-12 13:30:09 -0500 | [diff] [blame] | 812 | { |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 813 | if (CC(nested_vmx_check_msr_switch(vcpu, |
| 814 | vmcs12->vm_entry_msr_load_count, |
| 815 | vmcs12->vm_entry_msr_load_addr))) |
Krish Sadhukhan | 61446ba | 2018-12-12 13:30:09 -0500 | [diff] [blame] | 816 | return -EINVAL; |
| 817 | |
| 818 | return 0; |
| 819 | } |
| 820 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 821 | static int nested_vmx_check_pml_controls(struct kvm_vcpu *vcpu, |
| 822 | struct vmcs12 *vmcs12) |
| 823 | { |
| 824 | if (!nested_cpu_has_pml(vmcs12)) |
| 825 | return 0; |
| 826 | |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 827 | if (CC(!nested_cpu_has_ept(vmcs12)) || |
| 828 | CC(!page_address_valid(vcpu, vmcs12->pml_address))) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 829 | return -EINVAL; |
| 830 | |
| 831 | return 0; |
| 832 | } |
| 833 | |
| 834 | static int nested_vmx_check_unrestricted_guest_controls(struct kvm_vcpu *vcpu, |
| 835 | struct vmcs12 *vmcs12) |
| 836 | { |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 837 | if (CC(nested_cpu_has2(vmcs12, SECONDARY_EXEC_UNRESTRICTED_GUEST) && |
| 838 | !nested_cpu_has_ept(vmcs12))) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 839 | return -EINVAL; |
| 840 | return 0; |
| 841 | } |
| 842 | |
| 843 | static int nested_vmx_check_mode_based_ept_exec_controls(struct kvm_vcpu *vcpu, |
| 844 | struct vmcs12 *vmcs12) |
| 845 | { |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 846 | if (CC(nested_cpu_has2(vmcs12, SECONDARY_EXEC_MODE_BASED_EPT_EXEC) && |
| 847 | !nested_cpu_has_ept(vmcs12))) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 848 | return -EINVAL; |
| 849 | return 0; |
| 850 | } |
| 851 | |
| 852 | static int nested_vmx_check_shadow_vmcs_controls(struct kvm_vcpu *vcpu, |
| 853 | struct vmcs12 *vmcs12) |
| 854 | { |
| 855 | if (!nested_cpu_has_shadow_vmcs(vmcs12)) |
| 856 | return 0; |
| 857 | |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 858 | if (CC(!page_address_valid(vcpu, vmcs12->vmread_bitmap)) || |
| 859 | CC(!page_address_valid(vcpu, vmcs12->vmwrite_bitmap))) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 860 | return -EINVAL; |
| 861 | |
| 862 | return 0; |
| 863 | } |
| 864 | |
| 865 | static int nested_vmx_msr_check_common(struct kvm_vcpu *vcpu, |
| 866 | struct vmx_msr_entry *e) |
| 867 | { |
| 868 | /* x2APIC MSR accesses are not allowed */ |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 869 | if (CC(vcpu->arch.apic_base & X2APIC_ENABLE && e->index >> 8 == 0x8)) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 870 | return -EINVAL; |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 871 | if (CC(e->index == MSR_IA32_UCODE_WRITE) || /* SDM Table 35-2 */ |
| 872 | CC(e->index == MSR_IA32_UCODE_REV)) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 873 | return -EINVAL; |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 874 | if (CC(e->reserved != 0)) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 875 | return -EINVAL; |
| 876 | return 0; |
| 877 | } |
| 878 | |
| 879 | static int nested_vmx_load_msr_check(struct kvm_vcpu *vcpu, |
| 880 | struct vmx_msr_entry *e) |
| 881 | { |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 882 | if (CC(e->index == MSR_FS_BASE) || |
| 883 | CC(e->index == MSR_GS_BASE) || |
| 884 | CC(e->index == MSR_IA32_SMM_MONITOR_CTL) || /* SMM is not supported */ |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 885 | nested_vmx_msr_check_common(vcpu, e)) |
| 886 | return -EINVAL; |
| 887 | return 0; |
| 888 | } |
| 889 | |
| 890 | static int nested_vmx_store_msr_check(struct kvm_vcpu *vcpu, |
| 891 | struct vmx_msr_entry *e) |
| 892 | { |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 893 | if (CC(e->index == MSR_IA32_SMBASE) || /* SMM is not supported */ |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 894 | nested_vmx_msr_check_common(vcpu, e)) |
| 895 | return -EINVAL; |
| 896 | return 0; |
| 897 | } |
| 898 | |
Marc Orr | f0b5105 | 2019-09-17 11:50:57 -0700 | [diff] [blame] | 899 | static u32 nested_vmx_max_atomic_switch_msrs(struct kvm_vcpu *vcpu) |
| 900 | { |
| 901 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 902 | u64 vmx_misc = vmx_control_msr(vmx->nested.msrs.misc_low, |
| 903 | vmx->nested.msrs.misc_high); |
| 904 | |
| 905 | return (vmx_misc_max_msr(vmx_misc) + 1) * VMX_MISC_MSR_LIST_MULTIPLIER; |
| 906 | } |
| 907 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 908 | /* |
| 909 | * Load guest's/host's msr at nested entry/exit. |
| 910 | * return 0 for success, entry index for failure. |
Marc Orr | f0b5105 | 2019-09-17 11:50:57 -0700 | [diff] [blame] | 911 | * |
| 912 | * One of the failure modes for MSR load/store is when a list exceeds the |
| 913 | * virtual hardware's capacity. To maintain compatibility with hardware inasmuch |
| 914 | * as possible, process all valid entries before failing rather than precheck |
| 915 | * for a capacity violation. |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 916 | */ |
| 917 | static u32 nested_vmx_load_msr(struct kvm_vcpu *vcpu, u64 gpa, u32 count) |
| 918 | { |
| 919 | u32 i; |
| 920 | struct vmx_msr_entry e; |
Marc Orr | f0b5105 | 2019-09-17 11:50:57 -0700 | [diff] [blame] | 921 | u32 max_msr_list_size = nested_vmx_max_atomic_switch_msrs(vcpu); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 922 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 923 | for (i = 0; i < count; i++) { |
Marc Orr | f0b5105 | 2019-09-17 11:50:57 -0700 | [diff] [blame] | 924 | if (unlikely(i >= max_msr_list_size)) |
| 925 | goto fail; |
| 926 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 927 | if (kvm_vcpu_read_guest(vcpu, gpa + i * sizeof(e), |
| 928 | &e, sizeof(e))) { |
| 929 | pr_debug_ratelimited( |
| 930 | "%s cannot read MSR entry (%u, 0x%08llx)\n", |
| 931 | __func__, i, gpa + i * sizeof(e)); |
| 932 | goto fail; |
| 933 | } |
| 934 | if (nested_vmx_load_msr_check(vcpu, &e)) { |
| 935 | pr_debug_ratelimited( |
| 936 | "%s check failed (%u, 0x%x, 0x%x)\n", |
| 937 | __func__, i, e.index, e.reserved); |
| 938 | goto fail; |
| 939 | } |
Sean Christopherson | f20935d | 2019-09-05 14:22:54 -0700 | [diff] [blame] | 940 | if (kvm_set_msr(vcpu, e.index, e.value)) { |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 941 | pr_debug_ratelimited( |
| 942 | "%s cannot write MSR (%u, 0x%x, 0x%llx)\n", |
| 943 | __func__, i, e.index, e.value); |
| 944 | goto fail; |
| 945 | } |
| 946 | } |
| 947 | return 0; |
| 948 | fail: |
Sean Christopherson | 68cda40 | 2020-05-11 15:05:29 -0700 | [diff] [blame] | 949 | /* 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] | 950 | return i + 1; |
| 951 | } |
| 952 | |
Aaron Lewis | 662f1d1 | 2019-11-07 21:14:39 -0800 | [diff] [blame] | 953 | static bool nested_vmx_get_vmexit_msr_value(struct kvm_vcpu *vcpu, |
| 954 | u32 msr_index, |
| 955 | u64 *data) |
| 956 | { |
| 957 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 958 | |
| 959 | /* |
| 960 | * If the L0 hypervisor stored a more accurate value for the TSC that |
| 961 | * does not include the time taken for emulation of the L2->L1 |
| 962 | * VM-exit in L0, use the more accurate value. |
| 963 | */ |
| 964 | if (msr_index == MSR_IA32_TSC) { |
Sean Christopherson | a128a93 | 2020-09-23 11:03:57 -0700 | [diff] [blame] | 965 | int i = vmx_find_loadstore_msr_slot(&vmx->msr_autostore.guest, |
| 966 | MSR_IA32_TSC); |
Aaron Lewis | 662f1d1 | 2019-11-07 21:14:39 -0800 | [diff] [blame] | 967 | |
Sean Christopherson | a128a93 | 2020-09-23 11:03:57 -0700 | [diff] [blame] | 968 | if (i >= 0) { |
| 969 | u64 val = vmx->msr_autostore.guest.val[i].value; |
Aaron Lewis | 662f1d1 | 2019-11-07 21:14:39 -0800 | [diff] [blame] | 970 | |
| 971 | *data = kvm_read_l1_tsc(vcpu, val); |
| 972 | return true; |
| 973 | } |
| 974 | } |
| 975 | |
| 976 | if (kvm_get_msr(vcpu, msr_index, data)) { |
| 977 | pr_debug_ratelimited("%s cannot read MSR (0x%x)\n", __func__, |
| 978 | msr_index); |
| 979 | return false; |
| 980 | } |
| 981 | return true; |
| 982 | } |
| 983 | |
Aaron Lewis | 365d3d5 | 2019-11-07 21:14:36 -0800 | [diff] [blame] | 984 | static bool read_and_check_msr_entry(struct kvm_vcpu *vcpu, u64 gpa, int i, |
| 985 | struct vmx_msr_entry *e) |
| 986 | { |
| 987 | if (kvm_vcpu_read_guest(vcpu, |
| 988 | gpa + i * sizeof(*e), |
| 989 | e, 2 * sizeof(u32))) { |
| 990 | pr_debug_ratelimited( |
| 991 | "%s cannot read MSR entry (%u, 0x%08llx)\n", |
| 992 | __func__, i, gpa + i * sizeof(*e)); |
| 993 | return false; |
| 994 | } |
| 995 | if (nested_vmx_store_msr_check(vcpu, e)) { |
| 996 | pr_debug_ratelimited( |
| 997 | "%s check failed (%u, 0x%x, 0x%x)\n", |
| 998 | __func__, i, e->index, e->reserved); |
| 999 | return false; |
| 1000 | } |
| 1001 | return true; |
| 1002 | } |
| 1003 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1004 | static int nested_vmx_store_msr(struct kvm_vcpu *vcpu, u64 gpa, u32 count) |
| 1005 | { |
Sean Christopherson | f20935d | 2019-09-05 14:22:54 -0700 | [diff] [blame] | 1006 | u64 data; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1007 | u32 i; |
| 1008 | struct vmx_msr_entry e; |
Marc Orr | f0b5105 | 2019-09-17 11:50:57 -0700 | [diff] [blame] | 1009 | u32 max_msr_list_size = nested_vmx_max_atomic_switch_msrs(vcpu); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1010 | |
| 1011 | for (i = 0; i < count; i++) { |
Marc Orr | f0b5105 | 2019-09-17 11:50:57 -0700 | [diff] [blame] | 1012 | if (unlikely(i >= max_msr_list_size)) |
| 1013 | return -EINVAL; |
| 1014 | |
Aaron Lewis | 365d3d5 | 2019-11-07 21:14:36 -0800 | [diff] [blame] | 1015 | if (!read_and_check_msr_entry(vcpu, gpa, i, &e)) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1016 | return -EINVAL; |
Aaron Lewis | 365d3d5 | 2019-11-07 21:14:36 -0800 | [diff] [blame] | 1017 | |
Aaron Lewis | 662f1d1 | 2019-11-07 21:14:39 -0800 | [diff] [blame] | 1018 | if (!nested_vmx_get_vmexit_msr_value(vcpu, e.index, &data)) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1019 | return -EINVAL; |
Aaron Lewis | 662f1d1 | 2019-11-07 21:14:39 -0800 | [diff] [blame] | 1020 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1021 | if (kvm_vcpu_write_guest(vcpu, |
| 1022 | gpa + i * sizeof(e) + |
| 1023 | offsetof(struct vmx_msr_entry, value), |
Sean Christopherson | f20935d | 2019-09-05 14:22:54 -0700 | [diff] [blame] | 1024 | &data, sizeof(data))) { |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1025 | pr_debug_ratelimited( |
| 1026 | "%s cannot write MSR (%u, 0x%x, 0x%llx)\n", |
Sean Christopherson | f20935d | 2019-09-05 14:22:54 -0700 | [diff] [blame] | 1027 | __func__, i, e.index, data); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1028 | return -EINVAL; |
| 1029 | } |
| 1030 | } |
| 1031 | return 0; |
| 1032 | } |
| 1033 | |
Aaron Lewis | 662f1d1 | 2019-11-07 21:14:39 -0800 | [diff] [blame] | 1034 | static bool nested_msr_store_list_has_msr(struct kvm_vcpu *vcpu, u32 msr_index) |
| 1035 | { |
| 1036 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
| 1037 | u32 count = vmcs12->vm_exit_msr_store_count; |
| 1038 | u64 gpa = vmcs12->vm_exit_msr_store_addr; |
| 1039 | struct vmx_msr_entry e; |
| 1040 | u32 i; |
| 1041 | |
| 1042 | for (i = 0; i < count; i++) { |
| 1043 | if (!read_and_check_msr_entry(vcpu, gpa, i, &e)) |
| 1044 | return false; |
| 1045 | |
| 1046 | if (e.index == msr_index) |
| 1047 | return true; |
| 1048 | } |
| 1049 | return false; |
| 1050 | } |
| 1051 | |
| 1052 | static void prepare_vmx_msr_autostore_list(struct kvm_vcpu *vcpu, |
| 1053 | u32 msr_index) |
| 1054 | { |
| 1055 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 1056 | struct vmx_msrs *autostore = &vmx->msr_autostore.guest; |
| 1057 | bool in_vmcs12_store_list; |
Sean Christopherson | a128a93 | 2020-09-23 11:03:57 -0700 | [diff] [blame] | 1058 | int msr_autostore_slot; |
Aaron Lewis | 662f1d1 | 2019-11-07 21:14:39 -0800 | [diff] [blame] | 1059 | bool in_autostore_list; |
| 1060 | int last; |
| 1061 | |
Sean Christopherson | a128a93 | 2020-09-23 11:03:57 -0700 | [diff] [blame] | 1062 | msr_autostore_slot = vmx_find_loadstore_msr_slot(autostore, msr_index); |
| 1063 | in_autostore_list = msr_autostore_slot >= 0; |
Aaron Lewis | 662f1d1 | 2019-11-07 21:14:39 -0800 | [diff] [blame] | 1064 | in_vmcs12_store_list = nested_msr_store_list_has_msr(vcpu, msr_index); |
| 1065 | |
| 1066 | if (in_vmcs12_store_list && !in_autostore_list) { |
Sean Christopherson | ce833b2 | 2020-09-23 11:03:56 -0700 | [diff] [blame] | 1067 | if (autostore->nr == MAX_NR_LOADSTORE_MSRS) { |
Aaron Lewis | 662f1d1 | 2019-11-07 21:14:39 -0800 | [diff] [blame] | 1068 | /* |
| 1069 | * Emulated VMEntry does not fail here. Instead a less |
| 1070 | * accurate value will be returned by |
| 1071 | * nested_vmx_get_vmexit_msr_value() using kvm_get_msr() |
| 1072 | * instead of reading the value from the vmcs02 VMExit |
| 1073 | * MSR-store area. |
| 1074 | */ |
| 1075 | pr_warn_ratelimited( |
| 1076 | "Not enough msr entries in msr_autostore. Can't add msr %x\n", |
| 1077 | msr_index); |
| 1078 | return; |
| 1079 | } |
| 1080 | last = autostore->nr++; |
| 1081 | autostore->val[last].index = msr_index; |
| 1082 | } else if (!in_vmcs12_store_list && in_autostore_list) { |
| 1083 | last = --autostore->nr; |
Sean Christopherson | a128a93 | 2020-09-23 11:03:57 -0700 | [diff] [blame] | 1084 | autostore->val[msr_autostore_slot] = autostore->val[last]; |
Aaron Lewis | 662f1d1 | 2019-11-07 21:14:39 -0800 | [diff] [blame] | 1085 | } |
| 1086 | } |
| 1087 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1088 | /* |
Sean Christopherson | ea79a75 | 2020-02-04 07:32:59 -0800 | [diff] [blame] | 1089 | * Load guest's/host's cr3 at nested entry/exit. @nested_ept is true if we are |
| 1090 | * emulating VM-Entry into a guest with EPT enabled. On failure, the expected |
| 1091 | * Exit Qualification (for a VM-Entry consistency check VM-Exit) is assigned to |
| 1092 | * @entry_failure_code. |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1093 | */ |
Maxim Levitsky | 0f85722 | 2021-06-07 12:02:00 +0300 | [diff] [blame] | 1094 | static int nested_vmx_load_cr3(struct kvm_vcpu *vcpu, unsigned long cr3, |
| 1095 | bool nested_ept, bool reload_pdptrs, |
Sean Christopherson | 68cda40 | 2020-05-11 15:05:29 -0700 | [diff] [blame] | 1096 | enum vm_entry_failure_code *entry_failure_code) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1097 | { |
Sean Christopherson | 636e8b7 | 2021-02-03 16:01:10 -0800 | [diff] [blame] | 1098 | if (CC(kvm_vcpu_is_illegal_gpa(vcpu, cr3))) { |
Sean Christopherson | 0cc6920 | 2020-05-01 21:32:26 -0700 | [diff] [blame] | 1099 | *entry_failure_code = ENTRY_FAIL_DEFAULT; |
| 1100 | return -EINVAL; |
| 1101 | } |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1102 | |
Sean Christopherson | 0cc6920 | 2020-05-01 21:32:26 -0700 | [diff] [blame] | 1103 | /* |
| 1104 | * If PAE paging and EPT are both on, CR3 is not used by the CPU and |
| 1105 | * must not be dereferenced. |
| 1106 | */ |
Maxim Levitsky | 0f85722 | 2021-06-07 12:02:00 +0300 | [diff] [blame] | 1107 | if (reload_pdptrs && !nested_ept && is_pae_paging(vcpu) && |
Lai Jiangshan | 2df4a5e | 2021-11-24 20:20:52 +0800 | [diff] [blame] | 1108 | CC(!load_pdptrs(vcpu, cr3))) { |
Sean Christopherson | bcb72d0 | 2021-06-07 12:01:56 +0300 | [diff] [blame] | 1109 | *entry_failure_code = ENTRY_FAIL_PDPTE; |
| 1110 | return -EINVAL; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1111 | } |
| 1112 | |
Sean Christopherson | 50a4179 | 2021-06-09 16:42:28 -0700 | [diff] [blame] | 1113 | if (!nested_ept) |
Sean Christopherson | b512910 | 2021-06-09 16:42:27 -0700 | [diff] [blame] | 1114 | kvm_mmu_new_pgd(vcpu, cr3); |
Sean Christopherson | 07ffaf3 | 2021-06-09 16:42:21 -0700 | [diff] [blame] | 1115 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1116 | vcpu->arch.cr3 = cr3; |
Lai Jiangshan | 3883bc9d | 2021-11-08 20:44:02 +0800 | [diff] [blame] | 1117 | kvm_register_mark_dirty(vcpu, VCPU_EXREG_CR3); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1118 | |
Sean Christopherson | 616007c | 2021-06-22 10:57:34 -0700 | [diff] [blame] | 1119 | /* 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] | 1120 | kvm_init_mmu(vcpu); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1121 | |
| 1122 | return 0; |
| 1123 | } |
| 1124 | |
| 1125 | /* |
| 1126 | * Returns if KVM is able to config CPU to tag TLB entries |
| 1127 | * populated by L2 differently than TLB entries populated |
| 1128 | * by L1. |
| 1129 | * |
Liran Alon | 992edea | 2019-11-20 14:24:52 +0200 | [diff] [blame] | 1130 | * If L0 uses EPT, L1 and L2 run with different EPTP because |
| 1131 | * guest_mode is part of kvm_mmu_page_role. Thus, TLB entries |
| 1132 | * are tagged with different EPTP. |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1133 | * |
| 1134 | * If L1 uses VPID and we allocated a vpid02, TLB entries are tagged |
| 1135 | * with different VPID (L1 entries are tagged with vmx->vpid |
| 1136 | * while L2 entries are tagged with vmx->nested.vpid02). |
| 1137 | */ |
| 1138 | static bool nested_has_guest_tlb_tag(struct kvm_vcpu *vcpu) |
| 1139 | { |
| 1140 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
| 1141 | |
Liran Alon | 992edea | 2019-11-20 14:24:52 +0200 | [diff] [blame] | 1142 | return enable_ept || |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1143 | (nested_cpu_has_vpid(vmcs12) && to_vmx(vcpu)->nested.vpid02); |
| 1144 | } |
| 1145 | |
Sean Christopherson | 50b265a | 2020-03-20 14:28:19 -0700 | [diff] [blame] | 1146 | static void nested_vmx_transition_tlb_flush(struct kvm_vcpu *vcpu, |
| 1147 | struct vmcs12 *vmcs12, |
| 1148 | bool is_vmenter) |
| 1149 | { |
| 1150 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 1151 | |
| 1152 | /* |
Sean Christopherson | 50a4179 | 2021-06-09 16:42:28 -0700 | [diff] [blame] | 1153 | * If vmcs12 doesn't use VPID, L1 expects linear and combined mappings |
| 1154 | * for *all* contexts to be flushed on VM-Enter/VM-Exit, i.e. it's a |
| 1155 | * full TLB flush from the guest's perspective. This is required even |
| 1156 | * if VPID is disabled in the host as KVM may need to synchronize the |
| 1157 | * MMU in response to the guest TLB flush. |
| 1158 | * |
| 1159 | * Note, using TLB_FLUSH_GUEST is correct even if nested EPT is in use. |
| 1160 | * EPT is a special snowflake, as guest-physical mappings aren't |
| 1161 | * flushed on VPID invalidations, including VM-Enter or VM-Exit with |
| 1162 | * VPID disabled. As a result, KVM _never_ needs to sync nEPT |
| 1163 | * entries on VM-Enter because L1 can't rely on VM-Enter to flush |
| 1164 | * those mappings. |
Sean Christopherson | 50b265a | 2020-03-20 14:28:19 -0700 | [diff] [blame] | 1165 | */ |
Sean Christopherson | 50a4179 | 2021-06-09 16:42:28 -0700 | [diff] [blame] | 1166 | if (!nested_cpu_has_vpid(vmcs12)) { |
| 1167 | kvm_make_request(KVM_REQ_TLB_FLUSH_GUEST, vcpu); |
Sean Christopherson | 50b265a | 2020-03-20 14:28:19 -0700 | [diff] [blame] | 1168 | return; |
Sean Christopherson | 50a4179 | 2021-06-09 16:42:28 -0700 | [diff] [blame] | 1169 | } |
| 1170 | |
| 1171 | /* L2 should never have a VPID if VPID is disabled. */ |
| 1172 | WARN_ON(!enable_vpid); |
Sean Christopherson | 50b265a | 2020-03-20 14:28:19 -0700 | [diff] [blame] | 1173 | |
| 1174 | /* |
Sean Christopherson | 712494d | 2021-11-25 01:49:44 +0000 | [diff] [blame] | 1175 | * VPID is enabled and in use by vmcs12. If vpid12 is changing, then |
| 1176 | * emulate a guest TLB flush as KVM does not track vpid12 history nor |
| 1177 | * is the VPID incorporated into the MMU context. I.e. KVM must assume |
| 1178 | * that the new vpid12 has never been used and thus represents a new |
| 1179 | * guest ASID that cannot have entries in the TLB. |
Sean Christopherson | 50b265a | 2020-03-20 14:28:19 -0700 | [diff] [blame] | 1180 | */ |
Sean Christopherson | 712494d | 2021-11-25 01:49:44 +0000 | [diff] [blame] | 1181 | if (is_vmenter && vmcs12->virtual_processor_id != vmx->nested.last_vpid) { |
Sean Christopherson | 50b265a | 2020-03-20 14:28:19 -0700 | [diff] [blame] | 1182 | vmx->nested.last_vpid = vmcs12->virtual_processor_id; |
Sean Christopherson | 712494d | 2021-11-25 01:49:44 +0000 | [diff] [blame] | 1183 | kvm_make_request(KVM_REQ_TLB_FLUSH_GUEST, vcpu); |
| 1184 | return; |
Sean Christopherson | 50b265a | 2020-03-20 14:28:19 -0700 | [diff] [blame] | 1185 | } |
Sean Christopherson | 712494d | 2021-11-25 01:49:44 +0000 | [diff] [blame] | 1186 | |
| 1187 | /* |
| 1188 | * If VPID is enabled, used by vmc12, and vpid12 is not changing but |
| 1189 | * does not have a unique TLB tag (ASID), i.e. EPT is disabled and |
| 1190 | * KVM was unable to allocate a VPID for L2, flush the current context |
| 1191 | * as the effective ASID is common to both L1 and L2. |
| 1192 | */ |
| 1193 | if (!nested_has_guest_tlb_tag(vcpu)) |
| 1194 | kvm_make_request(KVM_REQ_TLB_FLUSH_CURRENT, vcpu); |
Sean Christopherson | 50b265a | 2020-03-20 14:28:19 -0700 | [diff] [blame] | 1195 | } |
| 1196 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1197 | static bool is_bitwise_subset(u64 superset, u64 subset, u64 mask) |
| 1198 | { |
| 1199 | superset &= mask; |
| 1200 | subset &= mask; |
| 1201 | |
| 1202 | return (superset | subset) == superset; |
| 1203 | } |
| 1204 | |
| 1205 | static int vmx_restore_vmx_basic(struct vcpu_vmx *vmx, u64 data) |
| 1206 | { |
| 1207 | const u64 feature_and_reserved = |
| 1208 | /* feature (except bit 48; see below) */ |
| 1209 | BIT_ULL(49) | BIT_ULL(54) | BIT_ULL(55) | |
| 1210 | /* reserved */ |
| 1211 | BIT_ULL(31) | GENMASK_ULL(47, 45) | GENMASK_ULL(63, 56); |
| 1212 | u64 vmx_basic = vmx->nested.msrs.basic; |
| 1213 | |
| 1214 | if (!is_bitwise_subset(vmx_basic, data, feature_and_reserved)) |
| 1215 | return -EINVAL; |
| 1216 | |
| 1217 | /* |
| 1218 | * KVM does not emulate a version of VMX that constrains physical |
| 1219 | * addresses of VMX structures (e.g. VMCS) to 32-bits. |
| 1220 | */ |
| 1221 | if (data & BIT_ULL(48)) |
| 1222 | return -EINVAL; |
| 1223 | |
| 1224 | if (vmx_basic_vmcs_revision_id(vmx_basic) != |
| 1225 | vmx_basic_vmcs_revision_id(data)) |
| 1226 | return -EINVAL; |
| 1227 | |
| 1228 | if (vmx_basic_vmcs_size(vmx_basic) > vmx_basic_vmcs_size(data)) |
| 1229 | return -EINVAL; |
| 1230 | |
| 1231 | vmx->nested.msrs.basic = data; |
| 1232 | return 0; |
| 1233 | } |
| 1234 | |
| 1235 | static int |
| 1236 | vmx_restore_control_msr(struct vcpu_vmx *vmx, u32 msr_index, u64 data) |
| 1237 | { |
| 1238 | u64 supported; |
| 1239 | u32 *lowp, *highp; |
| 1240 | |
| 1241 | switch (msr_index) { |
| 1242 | case MSR_IA32_VMX_TRUE_PINBASED_CTLS: |
| 1243 | lowp = &vmx->nested.msrs.pinbased_ctls_low; |
| 1244 | highp = &vmx->nested.msrs.pinbased_ctls_high; |
| 1245 | break; |
| 1246 | case MSR_IA32_VMX_TRUE_PROCBASED_CTLS: |
| 1247 | lowp = &vmx->nested.msrs.procbased_ctls_low; |
| 1248 | highp = &vmx->nested.msrs.procbased_ctls_high; |
| 1249 | break; |
| 1250 | case MSR_IA32_VMX_TRUE_EXIT_CTLS: |
| 1251 | lowp = &vmx->nested.msrs.exit_ctls_low; |
| 1252 | highp = &vmx->nested.msrs.exit_ctls_high; |
| 1253 | break; |
| 1254 | case MSR_IA32_VMX_TRUE_ENTRY_CTLS: |
| 1255 | lowp = &vmx->nested.msrs.entry_ctls_low; |
| 1256 | highp = &vmx->nested.msrs.entry_ctls_high; |
| 1257 | break; |
| 1258 | case MSR_IA32_VMX_PROCBASED_CTLS2: |
| 1259 | lowp = &vmx->nested.msrs.secondary_ctls_low; |
| 1260 | highp = &vmx->nested.msrs.secondary_ctls_high; |
| 1261 | break; |
| 1262 | default: |
| 1263 | BUG(); |
| 1264 | } |
| 1265 | |
| 1266 | supported = vmx_control_msr(*lowp, *highp); |
| 1267 | |
| 1268 | /* Check must-be-1 bits are still 1. */ |
| 1269 | if (!is_bitwise_subset(data, supported, GENMASK_ULL(31, 0))) |
| 1270 | return -EINVAL; |
| 1271 | |
| 1272 | /* Check must-be-0 bits are still 0. */ |
| 1273 | if (!is_bitwise_subset(supported, data, GENMASK_ULL(63, 32))) |
| 1274 | return -EINVAL; |
| 1275 | |
| 1276 | *lowp = data; |
| 1277 | *highp = data >> 32; |
| 1278 | return 0; |
| 1279 | } |
| 1280 | |
| 1281 | static int vmx_restore_vmx_misc(struct vcpu_vmx *vmx, u64 data) |
| 1282 | { |
| 1283 | const u64 feature_and_reserved_bits = |
| 1284 | /* feature */ |
| 1285 | BIT_ULL(5) | GENMASK_ULL(8, 6) | BIT_ULL(14) | BIT_ULL(15) | |
| 1286 | BIT_ULL(28) | BIT_ULL(29) | BIT_ULL(30) | |
| 1287 | /* reserved */ |
| 1288 | GENMASK_ULL(13, 9) | BIT_ULL(31); |
| 1289 | u64 vmx_misc; |
| 1290 | |
| 1291 | vmx_misc = vmx_control_msr(vmx->nested.msrs.misc_low, |
| 1292 | vmx->nested.msrs.misc_high); |
| 1293 | |
| 1294 | if (!is_bitwise_subset(vmx_misc, data, feature_and_reserved_bits)) |
| 1295 | return -EINVAL; |
| 1296 | |
| 1297 | if ((vmx->nested.msrs.pinbased_ctls_high & |
| 1298 | PIN_BASED_VMX_PREEMPTION_TIMER) && |
| 1299 | vmx_misc_preemption_timer_rate(data) != |
| 1300 | vmx_misc_preemption_timer_rate(vmx_misc)) |
| 1301 | return -EINVAL; |
| 1302 | |
| 1303 | if (vmx_misc_cr3_count(data) > vmx_misc_cr3_count(vmx_misc)) |
| 1304 | return -EINVAL; |
| 1305 | |
| 1306 | if (vmx_misc_max_msr(data) > vmx_misc_max_msr(vmx_misc)) |
| 1307 | return -EINVAL; |
| 1308 | |
| 1309 | if (vmx_misc_mseg_revid(data) != vmx_misc_mseg_revid(vmx_misc)) |
| 1310 | return -EINVAL; |
| 1311 | |
| 1312 | vmx->nested.msrs.misc_low = data; |
| 1313 | vmx->nested.msrs.misc_high = data >> 32; |
| 1314 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1315 | return 0; |
| 1316 | } |
| 1317 | |
| 1318 | static int vmx_restore_vmx_ept_vpid_cap(struct vcpu_vmx *vmx, u64 data) |
| 1319 | { |
| 1320 | u64 vmx_ept_vpid_cap; |
| 1321 | |
| 1322 | vmx_ept_vpid_cap = vmx_control_msr(vmx->nested.msrs.ept_caps, |
| 1323 | vmx->nested.msrs.vpid_caps); |
| 1324 | |
| 1325 | /* Every bit is either reserved or a feature bit. */ |
| 1326 | if (!is_bitwise_subset(vmx_ept_vpid_cap, data, -1ULL)) |
| 1327 | return -EINVAL; |
| 1328 | |
| 1329 | vmx->nested.msrs.ept_caps = data; |
| 1330 | vmx->nested.msrs.vpid_caps = data >> 32; |
| 1331 | return 0; |
| 1332 | } |
| 1333 | |
| 1334 | static int vmx_restore_fixed0_msr(struct vcpu_vmx *vmx, u32 msr_index, u64 data) |
| 1335 | { |
| 1336 | u64 *msr; |
| 1337 | |
| 1338 | switch (msr_index) { |
| 1339 | case MSR_IA32_VMX_CR0_FIXED0: |
| 1340 | msr = &vmx->nested.msrs.cr0_fixed0; |
| 1341 | break; |
| 1342 | case MSR_IA32_VMX_CR4_FIXED0: |
| 1343 | msr = &vmx->nested.msrs.cr4_fixed0; |
| 1344 | break; |
| 1345 | default: |
| 1346 | BUG(); |
| 1347 | } |
| 1348 | |
| 1349 | /* |
| 1350 | * 1 bits (which indicates bits which "must-be-1" during VMX operation) |
| 1351 | * must be 1 in the restored value. |
| 1352 | */ |
| 1353 | if (!is_bitwise_subset(data, *msr, -1ULL)) |
| 1354 | return -EINVAL; |
| 1355 | |
| 1356 | *msr = data; |
| 1357 | return 0; |
| 1358 | } |
| 1359 | |
| 1360 | /* |
| 1361 | * Called when userspace is restoring VMX MSRs. |
| 1362 | * |
| 1363 | * Returns 0 on success, non-0 otherwise. |
| 1364 | */ |
| 1365 | int vmx_set_vmx_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 data) |
| 1366 | { |
| 1367 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 1368 | |
| 1369 | /* |
| 1370 | * Don't allow changes to the VMX capability MSRs while the vCPU |
| 1371 | * is in VMX operation. |
| 1372 | */ |
| 1373 | if (vmx->nested.vmxon) |
| 1374 | return -EBUSY; |
| 1375 | |
| 1376 | switch (msr_index) { |
| 1377 | case MSR_IA32_VMX_BASIC: |
| 1378 | return vmx_restore_vmx_basic(vmx, data); |
| 1379 | case MSR_IA32_VMX_PINBASED_CTLS: |
| 1380 | case MSR_IA32_VMX_PROCBASED_CTLS: |
| 1381 | case MSR_IA32_VMX_EXIT_CTLS: |
| 1382 | case MSR_IA32_VMX_ENTRY_CTLS: |
| 1383 | /* |
| 1384 | * The "non-true" VMX capability MSRs are generated from the |
| 1385 | * "true" MSRs, so we do not support restoring them directly. |
| 1386 | * |
| 1387 | * If userspace wants to emulate VMX_BASIC[55]=0, userspace |
| 1388 | * should restore the "true" MSRs with the must-be-1 bits |
| 1389 | * set according to the SDM Vol 3. A.2 "RESERVED CONTROLS AND |
| 1390 | * DEFAULT SETTINGS". |
| 1391 | */ |
| 1392 | return -EINVAL; |
| 1393 | case MSR_IA32_VMX_TRUE_PINBASED_CTLS: |
| 1394 | case MSR_IA32_VMX_TRUE_PROCBASED_CTLS: |
| 1395 | case MSR_IA32_VMX_TRUE_EXIT_CTLS: |
| 1396 | case MSR_IA32_VMX_TRUE_ENTRY_CTLS: |
| 1397 | case MSR_IA32_VMX_PROCBASED_CTLS2: |
| 1398 | return vmx_restore_control_msr(vmx, msr_index, data); |
| 1399 | case MSR_IA32_VMX_MISC: |
| 1400 | return vmx_restore_vmx_misc(vmx, data); |
| 1401 | case MSR_IA32_VMX_CR0_FIXED0: |
| 1402 | case MSR_IA32_VMX_CR4_FIXED0: |
| 1403 | return vmx_restore_fixed0_msr(vmx, msr_index, data); |
| 1404 | case MSR_IA32_VMX_CR0_FIXED1: |
| 1405 | case MSR_IA32_VMX_CR4_FIXED1: |
| 1406 | /* |
| 1407 | * These MSRs are generated based on the vCPU's CPUID, so we |
| 1408 | * do not support restoring them directly. |
| 1409 | */ |
| 1410 | return -EINVAL; |
| 1411 | case MSR_IA32_VMX_EPT_VPID_CAP: |
| 1412 | return vmx_restore_vmx_ept_vpid_cap(vmx, data); |
| 1413 | case MSR_IA32_VMX_VMCS_ENUM: |
| 1414 | vmx->nested.msrs.vmcs_enum = data; |
| 1415 | return 0; |
Paolo Bonzini | e8a70bd | 2019-07-02 14:40:40 +0200 | [diff] [blame] | 1416 | case MSR_IA32_VMX_VMFUNC: |
| 1417 | if (data & ~vmx->nested.msrs.vmfunc_controls) |
| 1418 | return -EINVAL; |
| 1419 | vmx->nested.msrs.vmfunc_controls = data; |
| 1420 | return 0; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1421 | default: |
| 1422 | /* |
| 1423 | * The rest of the VMX capability MSRs do not support restore. |
| 1424 | */ |
| 1425 | return -EINVAL; |
| 1426 | } |
| 1427 | } |
| 1428 | |
| 1429 | /* Returns 0 on success, non-0 otherwise. */ |
| 1430 | int vmx_get_vmx_msr(struct nested_vmx_msrs *msrs, u32 msr_index, u64 *pdata) |
| 1431 | { |
| 1432 | switch (msr_index) { |
| 1433 | case MSR_IA32_VMX_BASIC: |
| 1434 | *pdata = msrs->basic; |
| 1435 | break; |
| 1436 | case MSR_IA32_VMX_TRUE_PINBASED_CTLS: |
| 1437 | case MSR_IA32_VMX_PINBASED_CTLS: |
| 1438 | *pdata = vmx_control_msr( |
| 1439 | msrs->pinbased_ctls_low, |
| 1440 | msrs->pinbased_ctls_high); |
| 1441 | if (msr_index == MSR_IA32_VMX_PINBASED_CTLS) |
| 1442 | *pdata |= PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR; |
| 1443 | break; |
| 1444 | case MSR_IA32_VMX_TRUE_PROCBASED_CTLS: |
| 1445 | case MSR_IA32_VMX_PROCBASED_CTLS: |
| 1446 | *pdata = vmx_control_msr( |
| 1447 | msrs->procbased_ctls_low, |
| 1448 | msrs->procbased_ctls_high); |
| 1449 | if (msr_index == MSR_IA32_VMX_PROCBASED_CTLS) |
| 1450 | *pdata |= CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR; |
| 1451 | break; |
| 1452 | case MSR_IA32_VMX_TRUE_EXIT_CTLS: |
| 1453 | case MSR_IA32_VMX_EXIT_CTLS: |
| 1454 | *pdata = vmx_control_msr( |
| 1455 | msrs->exit_ctls_low, |
| 1456 | msrs->exit_ctls_high); |
| 1457 | if (msr_index == MSR_IA32_VMX_EXIT_CTLS) |
| 1458 | *pdata |= VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR; |
| 1459 | break; |
| 1460 | case MSR_IA32_VMX_TRUE_ENTRY_CTLS: |
| 1461 | case MSR_IA32_VMX_ENTRY_CTLS: |
| 1462 | *pdata = vmx_control_msr( |
| 1463 | msrs->entry_ctls_low, |
| 1464 | msrs->entry_ctls_high); |
| 1465 | if (msr_index == MSR_IA32_VMX_ENTRY_CTLS) |
| 1466 | *pdata |= VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR; |
| 1467 | break; |
| 1468 | case MSR_IA32_VMX_MISC: |
| 1469 | *pdata = vmx_control_msr( |
| 1470 | msrs->misc_low, |
| 1471 | msrs->misc_high); |
| 1472 | break; |
| 1473 | case MSR_IA32_VMX_CR0_FIXED0: |
| 1474 | *pdata = msrs->cr0_fixed0; |
| 1475 | break; |
| 1476 | case MSR_IA32_VMX_CR0_FIXED1: |
| 1477 | *pdata = msrs->cr0_fixed1; |
| 1478 | break; |
| 1479 | case MSR_IA32_VMX_CR4_FIXED0: |
| 1480 | *pdata = msrs->cr4_fixed0; |
| 1481 | break; |
| 1482 | case MSR_IA32_VMX_CR4_FIXED1: |
| 1483 | *pdata = msrs->cr4_fixed1; |
| 1484 | break; |
| 1485 | case MSR_IA32_VMX_VMCS_ENUM: |
| 1486 | *pdata = msrs->vmcs_enum; |
| 1487 | break; |
| 1488 | case MSR_IA32_VMX_PROCBASED_CTLS2: |
| 1489 | *pdata = vmx_control_msr( |
| 1490 | msrs->secondary_ctls_low, |
| 1491 | msrs->secondary_ctls_high); |
| 1492 | break; |
| 1493 | case MSR_IA32_VMX_EPT_VPID_CAP: |
| 1494 | *pdata = msrs->ept_caps | |
| 1495 | ((u64)msrs->vpid_caps << 32); |
| 1496 | break; |
| 1497 | case MSR_IA32_VMX_VMFUNC: |
| 1498 | *pdata = msrs->vmfunc_controls; |
| 1499 | break; |
| 1500 | default: |
| 1501 | return 1; |
| 1502 | } |
| 1503 | |
| 1504 | return 0; |
| 1505 | } |
| 1506 | |
| 1507 | /* |
Sean Christopherson | fadcead | 2019-05-07 08:36:23 -0700 | [diff] [blame] | 1508 | * Copy the writable VMCS shadow fields back to the VMCS12, in case they have |
| 1509 | * been modified by the L1 guest. Note, "writable" in this context means |
| 1510 | * "writable by the guest", i.e. tagged SHADOW_FIELD_RW; the set of |
| 1511 | * fields tagged SHADOW_FIELD_RO may or may not align with the "read-only" |
| 1512 | * VM-exit information fields (which are actually writable if the vCPU is |
| 1513 | * configured to support "VMWRITE to any supported field in the VMCS"). |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1514 | */ |
| 1515 | static void copy_shadow_to_vmcs12(struct vcpu_vmx *vmx) |
| 1516 | { |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1517 | struct vmcs *shadow_vmcs = vmx->vmcs01.shadow_vmcs; |
Sean Christopherson | fadcead | 2019-05-07 08:36:23 -0700 | [diff] [blame] | 1518 | struct vmcs12 *vmcs12 = get_vmcs12(&vmx->vcpu); |
Sean Christopherson | 1c6f0b4 | 2019-05-07 08:36:25 -0700 | [diff] [blame] | 1519 | struct shadow_vmcs_field field; |
| 1520 | unsigned long val; |
Sean Christopherson | fadcead | 2019-05-07 08:36:23 -0700 | [diff] [blame] | 1521 | int i; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1522 | |
Paolo Bonzini | 88dddc1 | 2019-07-19 18:41:10 +0200 | [diff] [blame] | 1523 | if (WARN_ON(!shadow_vmcs)) |
| 1524 | return; |
| 1525 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1526 | preempt_disable(); |
| 1527 | |
| 1528 | vmcs_load(shadow_vmcs); |
| 1529 | |
Sean Christopherson | fadcead | 2019-05-07 08:36:23 -0700 | [diff] [blame] | 1530 | for (i = 0; i < max_shadow_read_write_fields; i++) { |
| 1531 | field = shadow_read_write_fields[i]; |
Sean Christopherson | 1c6f0b4 | 2019-05-07 08:36:25 -0700 | [diff] [blame] | 1532 | val = __vmcs_readl(field.encoding); |
| 1533 | vmcs12_write_any(vmcs12, field.encoding, field.offset, val); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1534 | } |
| 1535 | |
| 1536 | vmcs_clear(shadow_vmcs); |
| 1537 | vmcs_load(vmx->loaded_vmcs->vmcs); |
| 1538 | |
| 1539 | preempt_enable(); |
| 1540 | } |
| 1541 | |
| 1542 | static void copy_vmcs12_to_shadow(struct vcpu_vmx *vmx) |
| 1543 | { |
Sean Christopherson | 1c6f0b4 | 2019-05-07 08:36:25 -0700 | [diff] [blame] | 1544 | const struct shadow_vmcs_field *fields[] = { |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1545 | shadow_read_write_fields, |
| 1546 | shadow_read_only_fields |
| 1547 | }; |
| 1548 | const int max_fields[] = { |
| 1549 | max_shadow_read_write_fields, |
| 1550 | max_shadow_read_only_fields |
| 1551 | }; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1552 | struct vmcs *shadow_vmcs = vmx->vmcs01.shadow_vmcs; |
Sean Christopherson | 1c6f0b4 | 2019-05-07 08:36:25 -0700 | [diff] [blame] | 1553 | struct vmcs12 *vmcs12 = get_vmcs12(&vmx->vcpu); |
| 1554 | struct shadow_vmcs_field field; |
| 1555 | unsigned long val; |
| 1556 | int i, q; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1557 | |
Paolo Bonzini | 88dddc1 | 2019-07-19 18:41:10 +0200 | [diff] [blame] | 1558 | if (WARN_ON(!shadow_vmcs)) |
| 1559 | return; |
| 1560 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1561 | vmcs_load(shadow_vmcs); |
| 1562 | |
| 1563 | for (q = 0; q < ARRAY_SIZE(fields); q++) { |
| 1564 | for (i = 0; i < max_fields[q]; i++) { |
| 1565 | field = fields[q][i]; |
Sean Christopherson | 1c6f0b4 | 2019-05-07 08:36:25 -0700 | [diff] [blame] | 1566 | val = vmcs12_read_any(vmcs12, field.encoding, |
| 1567 | field.offset); |
| 1568 | __vmcs_writel(field.encoding, val); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1569 | } |
| 1570 | } |
| 1571 | |
| 1572 | vmcs_clear(shadow_vmcs); |
| 1573 | vmcs_load(vmx->loaded_vmcs->vmcs); |
| 1574 | } |
| 1575 | |
Vitaly Kuznetsov | d6bf71a | 2021-05-26 15:20:22 +0200 | [diff] [blame] | 1576 | 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] | 1577 | { |
| 1578 | struct vmcs12 *vmcs12 = vmx->nested.cached_vmcs12; |
| 1579 | struct hv_enlightened_vmcs *evmcs = vmx->nested.hv_evmcs; |
| 1580 | |
| 1581 | /* HV_VMX_ENLIGHTENED_CLEAN_FIELD_NONE */ |
| 1582 | vmcs12->tpr_threshold = evmcs->tpr_threshold; |
| 1583 | vmcs12->guest_rip = evmcs->guest_rip; |
| 1584 | |
Vitaly Kuznetsov | d6bf71a | 2021-05-26 15:20:22 +0200 | [diff] [blame] | 1585 | if (unlikely(!(hv_clean_fields & |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1586 | HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_BASIC))) { |
| 1587 | vmcs12->guest_rsp = evmcs->guest_rsp; |
| 1588 | vmcs12->guest_rflags = evmcs->guest_rflags; |
| 1589 | vmcs12->guest_interruptibility_info = |
| 1590 | evmcs->guest_interruptibility_info; |
| 1591 | } |
| 1592 | |
Vitaly Kuznetsov | d6bf71a | 2021-05-26 15:20:22 +0200 | [diff] [blame] | 1593 | if (unlikely(!(hv_clean_fields & |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1594 | HV_VMX_ENLIGHTENED_CLEAN_FIELD_CONTROL_PROC))) { |
| 1595 | vmcs12->cpu_based_vm_exec_control = |
| 1596 | evmcs->cpu_based_vm_exec_control; |
| 1597 | } |
| 1598 | |
Vitaly Kuznetsov | d6bf71a | 2021-05-26 15:20:22 +0200 | [diff] [blame] | 1599 | if (unlikely(!(hv_clean_fields & |
Vitaly Kuznetsov | f9bc522 | 2019-06-13 13:35:02 +0200 | [diff] [blame] | 1600 | HV_VMX_ENLIGHTENED_CLEAN_FIELD_CONTROL_EXCPN))) { |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1601 | vmcs12->exception_bitmap = evmcs->exception_bitmap; |
| 1602 | } |
| 1603 | |
Vitaly Kuznetsov | d6bf71a | 2021-05-26 15:20:22 +0200 | [diff] [blame] | 1604 | if (unlikely(!(hv_clean_fields & |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1605 | HV_VMX_ENLIGHTENED_CLEAN_FIELD_CONTROL_ENTRY))) { |
| 1606 | vmcs12->vm_entry_controls = evmcs->vm_entry_controls; |
| 1607 | } |
| 1608 | |
Vitaly Kuznetsov | d6bf71a | 2021-05-26 15:20:22 +0200 | [diff] [blame] | 1609 | if (unlikely(!(hv_clean_fields & |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1610 | HV_VMX_ENLIGHTENED_CLEAN_FIELD_CONTROL_EVENT))) { |
| 1611 | vmcs12->vm_entry_intr_info_field = |
| 1612 | evmcs->vm_entry_intr_info_field; |
| 1613 | vmcs12->vm_entry_exception_error_code = |
| 1614 | evmcs->vm_entry_exception_error_code; |
| 1615 | vmcs12->vm_entry_instruction_len = |
| 1616 | evmcs->vm_entry_instruction_len; |
| 1617 | } |
| 1618 | |
Vitaly Kuznetsov | d6bf71a | 2021-05-26 15:20:22 +0200 | [diff] [blame] | 1619 | if (unlikely(!(hv_clean_fields & |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1620 | HV_VMX_ENLIGHTENED_CLEAN_FIELD_HOST_GRP1))) { |
| 1621 | vmcs12->host_ia32_pat = evmcs->host_ia32_pat; |
| 1622 | vmcs12->host_ia32_efer = evmcs->host_ia32_efer; |
| 1623 | vmcs12->host_cr0 = evmcs->host_cr0; |
| 1624 | vmcs12->host_cr3 = evmcs->host_cr3; |
| 1625 | vmcs12->host_cr4 = evmcs->host_cr4; |
| 1626 | vmcs12->host_ia32_sysenter_esp = evmcs->host_ia32_sysenter_esp; |
| 1627 | vmcs12->host_ia32_sysenter_eip = evmcs->host_ia32_sysenter_eip; |
| 1628 | vmcs12->host_rip = evmcs->host_rip; |
| 1629 | vmcs12->host_ia32_sysenter_cs = evmcs->host_ia32_sysenter_cs; |
| 1630 | vmcs12->host_es_selector = evmcs->host_es_selector; |
| 1631 | vmcs12->host_cs_selector = evmcs->host_cs_selector; |
| 1632 | vmcs12->host_ss_selector = evmcs->host_ss_selector; |
| 1633 | vmcs12->host_ds_selector = evmcs->host_ds_selector; |
| 1634 | vmcs12->host_fs_selector = evmcs->host_fs_selector; |
| 1635 | vmcs12->host_gs_selector = evmcs->host_gs_selector; |
| 1636 | vmcs12->host_tr_selector = evmcs->host_tr_selector; |
| 1637 | } |
| 1638 | |
Vitaly Kuznetsov | d6bf71a | 2021-05-26 15:20:22 +0200 | [diff] [blame] | 1639 | if (unlikely(!(hv_clean_fields & |
Vitaly Kuznetsov | f9bc522 | 2019-06-13 13:35:02 +0200 | [diff] [blame] | 1640 | HV_VMX_ENLIGHTENED_CLEAN_FIELD_CONTROL_GRP1))) { |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1641 | vmcs12->pin_based_vm_exec_control = |
| 1642 | evmcs->pin_based_vm_exec_control; |
| 1643 | vmcs12->vm_exit_controls = evmcs->vm_exit_controls; |
| 1644 | vmcs12->secondary_vm_exec_control = |
| 1645 | evmcs->secondary_vm_exec_control; |
| 1646 | } |
| 1647 | |
Vitaly Kuznetsov | d6bf71a | 2021-05-26 15:20:22 +0200 | [diff] [blame] | 1648 | if (unlikely(!(hv_clean_fields & |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1649 | HV_VMX_ENLIGHTENED_CLEAN_FIELD_IO_BITMAP))) { |
| 1650 | vmcs12->io_bitmap_a = evmcs->io_bitmap_a; |
| 1651 | vmcs12->io_bitmap_b = evmcs->io_bitmap_b; |
| 1652 | } |
| 1653 | |
Vitaly Kuznetsov | d6bf71a | 2021-05-26 15:20:22 +0200 | [diff] [blame] | 1654 | if (unlikely(!(hv_clean_fields & |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1655 | HV_VMX_ENLIGHTENED_CLEAN_FIELD_MSR_BITMAP))) { |
| 1656 | vmcs12->msr_bitmap = evmcs->msr_bitmap; |
| 1657 | } |
| 1658 | |
Vitaly Kuznetsov | d6bf71a | 2021-05-26 15:20:22 +0200 | [diff] [blame] | 1659 | if (unlikely(!(hv_clean_fields & |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1660 | HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP2))) { |
| 1661 | vmcs12->guest_es_base = evmcs->guest_es_base; |
| 1662 | vmcs12->guest_cs_base = evmcs->guest_cs_base; |
| 1663 | vmcs12->guest_ss_base = evmcs->guest_ss_base; |
| 1664 | vmcs12->guest_ds_base = evmcs->guest_ds_base; |
| 1665 | vmcs12->guest_fs_base = evmcs->guest_fs_base; |
| 1666 | vmcs12->guest_gs_base = evmcs->guest_gs_base; |
| 1667 | vmcs12->guest_ldtr_base = evmcs->guest_ldtr_base; |
| 1668 | vmcs12->guest_tr_base = evmcs->guest_tr_base; |
| 1669 | vmcs12->guest_gdtr_base = evmcs->guest_gdtr_base; |
| 1670 | vmcs12->guest_idtr_base = evmcs->guest_idtr_base; |
| 1671 | vmcs12->guest_es_limit = evmcs->guest_es_limit; |
| 1672 | vmcs12->guest_cs_limit = evmcs->guest_cs_limit; |
| 1673 | vmcs12->guest_ss_limit = evmcs->guest_ss_limit; |
| 1674 | vmcs12->guest_ds_limit = evmcs->guest_ds_limit; |
| 1675 | vmcs12->guest_fs_limit = evmcs->guest_fs_limit; |
| 1676 | vmcs12->guest_gs_limit = evmcs->guest_gs_limit; |
| 1677 | vmcs12->guest_ldtr_limit = evmcs->guest_ldtr_limit; |
| 1678 | vmcs12->guest_tr_limit = evmcs->guest_tr_limit; |
| 1679 | vmcs12->guest_gdtr_limit = evmcs->guest_gdtr_limit; |
| 1680 | vmcs12->guest_idtr_limit = evmcs->guest_idtr_limit; |
| 1681 | vmcs12->guest_es_ar_bytes = evmcs->guest_es_ar_bytes; |
| 1682 | vmcs12->guest_cs_ar_bytes = evmcs->guest_cs_ar_bytes; |
| 1683 | vmcs12->guest_ss_ar_bytes = evmcs->guest_ss_ar_bytes; |
| 1684 | vmcs12->guest_ds_ar_bytes = evmcs->guest_ds_ar_bytes; |
| 1685 | vmcs12->guest_fs_ar_bytes = evmcs->guest_fs_ar_bytes; |
| 1686 | vmcs12->guest_gs_ar_bytes = evmcs->guest_gs_ar_bytes; |
| 1687 | vmcs12->guest_ldtr_ar_bytes = evmcs->guest_ldtr_ar_bytes; |
| 1688 | vmcs12->guest_tr_ar_bytes = evmcs->guest_tr_ar_bytes; |
| 1689 | vmcs12->guest_es_selector = evmcs->guest_es_selector; |
| 1690 | vmcs12->guest_cs_selector = evmcs->guest_cs_selector; |
| 1691 | vmcs12->guest_ss_selector = evmcs->guest_ss_selector; |
| 1692 | vmcs12->guest_ds_selector = evmcs->guest_ds_selector; |
| 1693 | vmcs12->guest_fs_selector = evmcs->guest_fs_selector; |
| 1694 | vmcs12->guest_gs_selector = evmcs->guest_gs_selector; |
| 1695 | vmcs12->guest_ldtr_selector = evmcs->guest_ldtr_selector; |
| 1696 | vmcs12->guest_tr_selector = evmcs->guest_tr_selector; |
| 1697 | } |
| 1698 | |
Vitaly Kuznetsov | d6bf71a | 2021-05-26 15:20:22 +0200 | [diff] [blame] | 1699 | if (unlikely(!(hv_clean_fields & |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1700 | HV_VMX_ENLIGHTENED_CLEAN_FIELD_CONTROL_GRP2))) { |
| 1701 | vmcs12->tsc_offset = evmcs->tsc_offset; |
| 1702 | vmcs12->virtual_apic_page_addr = evmcs->virtual_apic_page_addr; |
| 1703 | vmcs12->xss_exit_bitmap = evmcs->xss_exit_bitmap; |
| 1704 | } |
| 1705 | |
Vitaly Kuznetsov | d6bf71a | 2021-05-26 15:20:22 +0200 | [diff] [blame] | 1706 | if (unlikely(!(hv_clean_fields & |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1707 | HV_VMX_ENLIGHTENED_CLEAN_FIELD_CRDR))) { |
| 1708 | vmcs12->cr0_guest_host_mask = evmcs->cr0_guest_host_mask; |
| 1709 | vmcs12->cr4_guest_host_mask = evmcs->cr4_guest_host_mask; |
| 1710 | vmcs12->cr0_read_shadow = evmcs->cr0_read_shadow; |
| 1711 | vmcs12->cr4_read_shadow = evmcs->cr4_read_shadow; |
| 1712 | vmcs12->guest_cr0 = evmcs->guest_cr0; |
| 1713 | vmcs12->guest_cr3 = evmcs->guest_cr3; |
| 1714 | vmcs12->guest_cr4 = evmcs->guest_cr4; |
| 1715 | vmcs12->guest_dr7 = evmcs->guest_dr7; |
| 1716 | } |
| 1717 | |
Vitaly Kuznetsov | d6bf71a | 2021-05-26 15:20:22 +0200 | [diff] [blame] | 1718 | if (unlikely(!(hv_clean_fields & |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1719 | HV_VMX_ENLIGHTENED_CLEAN_FIELD_HOST_POINTER))) { |
| 1720 | vmcs12->host_fs_base = evmcs->host_fs_base; |
| 1721 | vmcs12->host_gs_base = evmcs->host_gs_base; |
| 1722 | vmcs12->host_tr_base = evmcs->host_tr_base; |
| 1723 | vmcs12->host_gdtr_base = evmcs->host_gdtr_base; |
| 1724 | vmcs12->host_idtr_base = evmcs->host_idtr_base; |
| 1725 | vmcs12->host_rsp = evmcs->host_rsp; |
| 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_CONTROL_XLAT))) { |
| 1730 | vmcs12->ept_pointer = evmcs->ept_pointer; |
| 1731 | vmcs12->virtual_processor_id = evmcs->virtual_processor_id; |
| 1732 | } |
| 1733 | |
Vitaly Kuznetsov | d6bf71a | 2021-05-26 15:20:22 +0200 | [diff] [blame] | 1734 | if (unlikely(!(hv_clean_fields & |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1735 | HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP1))) { |
| 1736 | vmcs12->vmcs_link_pointer = evmcs->vmcs_link_pointer; |
| 1737 | vmcs12->guest_ia32_debugctl = evmcs->guest_ia32_debugctl; |
| 1738 | vmcs12->guest_ia32_pat = evmcs->guest_ia32_pat; |
| 1739 | vmcs12->guest_ia32_efer = evmcs->guest_ia32_efer; |
| 1740 | vmcs12->guest_pdptr0 = evmcs->guest_pdptr0; |
| 1741 | vmcs12->guest_pdptr1 = evmcs->guest_pdptr1; |
| 1742 | vmcs12->guest_pdptr2 = evmcs->guest_pdptr2; |
| 1743 | vmcs12->guest_pdptr3 = evmcs->guest_pdptr3; |
| 1744 | vmcs12->guest_pending_dbg_exceptions = |
| 1745 | evmcs->guest_pending_dbg_exceptions; |
| 1746 | vmcs12->guest_sysenter_esp = evmcs->guest_sysenter_esp; |
| 1747 | vmcs12->guest_sysenter_eip = evmcs->guest_sysenter_eip; |
| 1748 | vmcs12->guest_bndcfgs = evmcs->guest_bndcfgs; |
| 1749 | vmcs12->guest_activity_state = evmcs->guest_activity_state; |
| 1750 | vmcs12->guest_sysenter_cs = evmcs->guest_sysenter_cs; |
| 1751 | } |
| 1752 | |
| 1753 | /* |
| 1754 | * Not used? |
| 1755 | * vmcs12->vm_exit_msr_store_addr = evmcs->vm_exit_msr_store_addr; |
| 1756 | * vmcs12->vm_exit_msr_load_addr = evmcs->vm_exit_msr_load_addr; |
| 1757 | * vmcs12->vm_entry_msr_load_addr = evmcs->vm_entry_msr_load_addr; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1758 | * vmcs12->page_fault_error_code_mask = |
| 1759 | * evmcs->page_fault_error_code_mask; |
| 1760 | * vmcs12->page_fault_error_code_match = |
| 1761 | * evmcs->page_fault_error_code_match; |
| 1762 | * vmcs12->cr3_target_count = evmcs->cr3_target_count; |
| 1763 | * vmcs12->vm_exit_msr_store_count = evmcs->vm_exit_msr_store_count; |
| 1764 | * vmcs12->vm_exit_msr_load_count = evmcs->vm_exit_msr_load_count; |
| 1765 | * vmcs12->vm_entry_msr_load_count = evmcs->vm_entry_msr_load_count; |
| 1766 | */ |
| 1767 | |
| 1768 | /* |
| 1769 | * Read only fields: |
| 1770 | * vmcs12->guest_physical_address = evmcs->guest_physical_address; |
| 1771 | * vmcs12->vm_instruction_error = evmcs->vm_instruction_error; |
| 1772 | * vmcs12->vm_exit_reason = evmcs->vm_exit_reason; |
| 1773 | * vmcs12->vm_exit_intr_info = evmcs->vm_exit_intr_info; |
| 1774 | * vmcs12->vm_exit_intr_error_code = evmcs->vm_exit_intr_error_code; |
| 1775 | * vmcs12->idt_vectoring_info_field = evmcs->idt_vectoring_info_field; |
| 1776 | * vmcs12->idt_vectoring_error_code = evmcs->idt_vectoring_error_code; |
| 1777 | * vmcs12->vm_exit_instruction_len = evmcs->vm_exit_instruction_len; |
| 1778 | * vmcs12->vmx_instruction_info = evmcs->vmx_instruction_info; |
| 1779 | * vmcs12->exit_qualification = evmcs->exit_qualification; |
| 1780 | * vmcs12->guest_linear_address = evmcs->guest_linear_address; |
| 1781 | * |
| 1782 | * Not present in struct vmcs12: |
| 1783 | * vmcs12->exit_io_instruction_ecx = evmcs->exit_io_instruction_ecx; |
| 1784 | * vmcs12->exit_io_instruction_esi = evmcs->exit_io_instruction_esi; |
| 1785 | * vmcs12->exit_io_instruction_edi = evmcs->exit_io_instruction_edi; |
| 1786 | * vmcs12->exit_io_instruction_eip = evmcs->exit_io_instruction_eip; |
| 1787 | */ |
| 1788 | |
Vitaly Kuznetsov | 25641ca | 2021-05-26 15:20:19 +0200 | [diff] [blame] | 1789 | return; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1790 | } |
| 1791 | |
Vitaly Kuznetsov | 25641ca | 2021-05-26 15:20:19 +0200 | [diff] [blame] | 1792 | static void copy_vmcs12_to_enlightened(struct vcpu_vmx *vmx) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1793 | { |
| 1794 | struct vmcs12 *vmcs12 = vmx->nested.cached_vmcs12; |
| 1795 | struct hv_enlightened_vmcs *evmcs = vmx->nested.hv_evmcs; |
| 1796 | |
| 1797 | /* |
| 1798 | * Should not be changed by KVM: |
| 1799 | * |
| 1800 | * evmcs->host_es_selector = vmcs12->host_es_selector; |
| 1801 | * evmcs->host_cs_selector = vmcs12->host_cs_selector; |
| 1802 | * evmcs->host_ss_selector = vmcs12->host_ss_selector; |
| 1803 | * evmcs->host_ds_selector = vmcs12->host_ds_selector; |
| 1804 | * evmcs->host_fs_selector = vmcs12->host_fs_selector; |
| 1805 | * evmcs->host_gs_selector = vmcs12->host_gs_selector; |
| 1806 | * evmcs->host_tr_selector = vmcs12->host_tr_selector; |
| 1807 | * evmcs->host_ia32_pat = vmcs12->host_ia32_pat; |
| 1808 | * evmcs->host_ia32_efer = vmcs12->host_ia32_efer; |
| 1809 | * evmcs->host_cr0 = vmcs12->host_cr0; |
| 1810 | * evmcs->host_cr3 = vmcs12->host_cr3; |
| 1811 | * evmcs->host_cr4 = vmcs12->host_cr4; |
| 1812 | * evmcs->host_ia32_sysenter_esp = vmcs12->host_ia32_sysenter_esp; |
| 1813 | * evmcs->host_ia32_sysenter_eip = vmcs12->host_ia32_sysenter_eip; |
| 1814 | * evmcs->host_rip = vmcs12->host_rip; |
| 1815 | * evmcs->host_ia32_sysenter_cs = vmcs12->host_ia32_sysenter_cs; |
| 1816 | * evmcs->host_fs_base = vmcs12->host_fs_base; |
| 1817 | * evmcs->host_gs_base = vmcs12->host_gs_base; |
| 1818 | * evmcs->host_tr_base = vmcs12->host_tr_base; |
| 1819 | * evmcs->host_gdtr_base = vmcs12->host_gdtr_base; |
| 1820 | * evmcs->host_idtr_base = vmcs12->host_idtr_base; |
| 1821 | * evmcs->host_rsp = vmcs12->host_rsp; |
Sean Christopherson | 3731905ef | 2019-05-07 08:36:27 -0700 | [diff] [blame] | 1822 | * sync_vmcs02_to_vmcs12() doesn't read these: |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1823 | * evmcs->io_bitmap_a = vmcs12->io_bitmap_a; |
| 1824 | * evmcs->io_bitmap_b = vmcs12->io_bitmap_b; |
| 1825 | * evmcs->msr_bitmap = vmcs12->msr_bitmap; |
| 1826 | * evmcs->ept_pointer = vmcs12->ept_pointer; |
| 1827 | * evmcs->xss_exit_bitmap = vmcs12->xss_exit_bitmap; |
| 1828 | * evmcs->vm_exit_msr_store_addr = vmcs12->vm_exit_msr_store_addr; |
| 1829 | * evmcs->vm_exit_msr_load_addr = vmcs12->vm_exit_msr_load_addr; |
| 1830 | * evmcs->vm_entry_msr_load_addr = vmcs12->vm_entry_msr_load_addr; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1831 | * evmcs->tpr_threshold = vmcs12->tpr_threshold; |
| 1832 | * evmcs->virtual_processor_id = vmcs12->virtual_processor_id; |
| 1833 | * evmcs->exception_bitmap = vmcs12->exception_bitmap; |
| 1834 | * evmcs->vmcs_link_pointer = vmcs12->vmcs_link_pointer; |
| 1835 | * evmcs->pin_based_vm_exec_control = vmcs12->pin_based_vm_exec_control; |
| 1836 | * evmcs->vm_exit_controls = vmcs12->vm_exit_controls; |
| 1837 | * evmcs->secondary_vm_exec_control = vmcs12->secondary_vm_exec_control; |
| 1838 | * evmcs->page_fault_error_code_mask = |
| 1839 | * vmcs12->page_fault_error_code_mask; |
| 1840 | * evmcs->page_fault_error_code_match = |
| 1841 | * vmcs12->page_fault_error_code_match; |
| 1842 | * evmcs->cr3_target_count = vmcs12->cr3_target_count; |
| 1843 | * evmcs->virtual_apic_page_addr = vmcs12->virtual_apic_page_addr; |
| 1844 | * evmcs->tsc_offset = vmcs12->tsc_offset; |
| 1845 | * evmcs->guest_ia32_debugctl = vmcs12->guest_ia32_debugctl; |
| 1846 | * evmcs->cr0_guest_host_mask = vmcs12->cr0_guest_host_mask; |
| 1847 | * evmcs->cr4_guest_host_mask = vmcs12->cr4_guest_host_mask; |
| 1848 | * evmcs->cr0_read_shadow = vmcs12->cr0_read_shadow; |
| 1849 | * evmcs->cr4_read_shadow = vmcs12->cr4_read_shadow; |
| 1850 | * evmcs->vm_exit_msr_store_count = vmcs12->vm_exit_msr_store_count; |
| 1851 | * evmcs->vm_exit_msr_load_count = vmcs12->vm_exit_msr_load_count; |
| 1852 | * evmcs->vm_entry_msr_load_count = vmcs12->vm_entry_msr_load_count; |
| 1853 | * |
| 1854 | * Not present in struct vmcs12: |
| 1855 | * evmcs->exit_io_instruction_ecx = vmcs12->exit_io_instruction_ecx; |
| 1856 | * evmcs->exit_io_instruction_esi = vmcs12->exit_io_instruction_esi; |
| 1857 | * evmcs->exit_io_instruction_edi = vmcs12->exit_io_instruction_edi; |
| 1858 | * evmcs->exit_io_instruction_eip = vmcs12->exit_io_instruction_eip; |
| 1859 | */ |
| 1860 | |
| 1861 | evmcs->guest_es_selector = vmcs12->guest_es_selector; |
| 1862 | evmcs->guest_cs_selector = vmcs12->guest_cs_selector; |
| 1863 | evmcs->guest_ss_selector = vmcs12->guest_ss_selector; |
| 1864 | evmcs->guest_ds_selector = vmcs12->guest_ds_selector; |
| 1865 | evmcs->guest_fs_selector = vmcs12->guest_fs_selector; |
| 1866 | evmcs->guest_gs_selector = vmcs12->guest_gs_selector; |
| 1867 | evmcs->guest_ldtr_selector = vmcs12->guest_ldtr_selector; |
| 1868 | evmcs->guest_tr_selector = vmcs12->guest_tr_selector; |
| 1869 | |
| 1870 | evmcs->guest_es_limit = vmcs12->guest_es_limit; |
| 1871 | evmcs->guest_cs_limit = vmcs12->guest_cs_limit; |
| 1872 | evmcs->guest_ss_limit = vmcs12->guest_ss_limit; |
| 1873 | evmcs->guest_ds_limit = vmcs12->guest_ds_limit; |
| 1874 | evmcs->guest_fs_limit = vmcs12->guest_fs_limit; |
| 1875 | evmcs->guest_gs_limit = vmcs12->guest_gs_limit; |
| 1876 | evmcs->guest_ldtr_limit = vmcs12->guest_ldtr_limit; |
| 1877 | evmcs->guest_tr_limit = vmcs12->guest_tr_limit; |
| 1878 | evmcs->guest_gdtr_limit = vmcs12->guest_gdtr_limit; |
| 1879 | evmcs->guest_idtr_limit = vmcs12->guest_idtr_limit; |
| 1880 | |
| 1881 | evmcs->guest_es_ar_bytes = vmcs12->guest_es_ar_bytes; |
| 1882 | evmcs->guest_cs_ar_bytes = vmcs12->guest_cs_ar_bytes; |
| 1883 | evmcs->guest_ss_ar_bytes = vmcs12->guest_ss_ar_bytes; |
| 1884 | evmcs->guest_ds_ar_bytes = vmcs12->guest_ds_ar_bytes; |
| 1885 | evmcs->guest_fs_ar_bytes = vmcs12->guest_fs_ar_bytes; |
| 1886 | evmcs->guest_gs_ar_bytes = vmcs12->guest_gs_ar_bytes; |
| 1887 | evmcs->guest_ldtr_ar_bytes = vmcs12->guest_ldtr_ar_bytes; |
| 1888 | evmcs->guest_tr_ar_bytes = vmcs12->guest_tr_ar_bytes; |
| 1889 | |
| 1890 | evmcs->guest_es_base = vmcs12->guest_es_base; |
| 1891 | evmcs->guest_cs_base = vmcs12->guest_cs_base; |
| 1892 | evmcs->guest_ss_base = vmcs12->guest_ss_base; |
| 1893 | evmcs->guest_ds_base = vmcs12->guest_ds_base; |
| 1894 | evmcs->guest_fs_base = vmcs12->guest_fs_base; |
| 1895 | evmcs->guest_gs_base = vmcs12->guest_gs_base; |
| 1896 | evmcs->guest_ldtr_base = vmcs12->guest_ldtr_base; |
| 1897 | evmcs->guest_tr_base = vmcs12->guest_tr_base; |
| 1898 | evmcs->guest_gdtr_base = vmcs12->guest_gdtr_base; |
| 1899 | evmcs->guest_idtr_base = vmcs12->guest_idtr_base; |
| 1900 | |
| 1901 | evmcs->guest_ia32_pat = vmcs12->guest_ia32_pat; |
| 1902 | evmcs->guest_ia32_efer = vmcs12->guest_ia32_efer; |
| 1903 | |
| 1904 | evmcs->guest_pdptr0 = vmcs12->guest_pdptr0; |
| 1905 | evmcs->guest_pdptr1 = vmcs12->guest_pdptr1; |
| 1906 | evmcs->guest_pdptr2 = vmcs12->guest_pdptr2; |
| 1907 | evmcs->guest_pdptr3 = vmcs12->guest_pdptr3; |
| 1908 | |
| 1909 | evmcs->guest_pending_dbg_exceptions = |
| 1910 | vmcs12->guest_pending_dbg_exceptions; |
| 1911 | evmcs->guest_sysenter_esp = vmcs12->guest_sysenter_esp; |
| 1912 | evmcs->guest_sysenter_eip = vmcs12->guest_sysenter_eip; |
| 1913 | |
| 1914 | evmcs->guest_activity_state = vmcs12->guest_activity_state; |
| 1915 | evmcs->guest_sysenter_cs = vmcs12->guest_sysenter_cs; |
| 1916 | |
| 1917 | evmcs->guest_cr0 = vmcs12->guest_cr0; |
| 1918 | evmcs->guest_cr3 = vmcs12->guest_cr3; |
| 1919 | evmcs->guest_cr4 = vmcs12->guest_cr4; |
| 1920 | evmcs->guest_dr7 = vmcs12->guest_dr7; |
| 1921 | |
| 1922 | evmcs->guest_physical_address = vmcs12->guest_physical_address; |
| 1923 | |
| 1924 | evmcs->vm_instruction_error = vmcs12->vm_instruction_error; |
| 1925 | evmcs->vm_exit_reason = vmcs12->vm_exit_reason; |
| 1926 | evmcs->vm_exit_intr_info = vmcs12->vm_exit_intr_info; |
| 1927 | evmcs->vm_exit_intr_error_code = vmcs12->vm_exit_intr_error_code; |
| 1928 | evmcs->idt_vectoring_info_field = vmcs12->idt_vectoring_info_field; |
| 1929 | evmcs->idt_vectoring_error_code = vmcs12->idt_vectoring_error_code; |
| 1930 | evmcs->vm_exit_instruction_len = vmcs12->vm_exit_instruction_len; |
| 1931 | evmcs->vmx_instruction_info = vmcs12->vmx_instruction_info; |
| 1932 | |
| 1933 | evmcs->exit_qualification = vmcs12->exit_qualification; |
| 1934 | |
| 1935 | evmcs->guest_linear_address = vmcs12->guest_linear_address; |
| 1936 | evmcs->guest_rsp = vmcs12->guest_rsp; |
| 1937 | evmcs->guest_rflags = vmcs12->guest_rflags; |
| 1938 | |
| 1939 | evmcs->guest_interruptibility_info = |
| 1940 | vmcs12->guest_interruptibility_info; |
| 1941 | evmcs->cpu_based_vm_exec_control = vmcs12->cpu_based_vm_exec_control; |
| 1942 | evmcs->vm_entry_controls = vmcs12->vm_entry_controls; |
| 1943 | evmcs->vm_entry_intr_info_field = vmcs12->vm_entry_intr_info_field; |
| 1944 | evmcs->vm_entry_exception_error_code = |
| 1945 | vmcs12->vm_entry_exception_error_code; |
| 1946 | evmcs->vm_entry_instruction_len = vmcs12->vm_entry_instruction_len; |
| 1947 | |
| 1948 | evmcs->guest_rip = vmcs12->guest_rip; |
| 1949 | |
| 1950 | evmcs->guest_bndcfgs = vmcs12->guest_bndcfgs; |
| 1951 | |
Vitaly Kuznetsov | 25641ca | 2021-05-26 15:20:19 +0200 | [diff] [blame] | 1952 | return; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1953 | } |
| 1954 | |
| 1955 | /* |
| 1956 | * This is an equivalent of the nested hypervisor executing the vmptrld |
| 1957 | * instruction. |
| 1958 | */ |
Vitaly Kuznetsov | b6a0653 | 2020-03-09 16:52:13 +0100 | [diff] [blame] | 1959 | static enum nested_evmptrld_status nested_vmx_handle_enlightened_vmptrld( |
| 1960 | struct kvm_vcpu *vcpu, bool from_launch) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1961 | { |
| 1962 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Vitaly Kuznetsov | a21a39c | 2019-06-28 13:23:32 +0200 | [diff] [blame] | 1963 | bool evmcs_gpa_changed = false; |
Vitaly Kuznetsov | 11e3491 | 2019-06-28 13:23:33 +0200 | [diff] [blame] | 1964 | u64 evmcs_gpa; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1965 | |
| 1966 | if (likely(!vmx->nested.enlightened_vmcs_enabled)) |
Vitaly Kuznetsov | b6a0653 | 2020-03-09 16:52:13 +0100 | [diff] [blame] | 1967 | return EVMPTRLD_DISABLED; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1968 | |
Vitaly Kuznetsov | 0276171 | 2021-05-26 15:20:18 +0200 | [diff] [blame] | 1969 | if (!nested_enlightened_vmentry(vcpu, &evmcs_gpa)) { |
| 1970 | nested_release_evmcs(vcpu); |
Vitaly Kuznetsov | b6a0653 | 2020-03-09 16:52:13 +0100 | [diff] [blame] | 1971 | return EVMPTRLD_DISABLED; |
Vitaly Kuznetsov | 0276171 | 2021-05-26 15:20:18 +0200 | [diff] [blame] | 1972 | } |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1973 | |
Vitaly Kuznetsov | 1e9dfbd | 2021-05-26 15:20:16 +0200 | [diff] [blame] | 1974 | if (unlikely(evmcs_gpa != vmx->nested.hv_evmcs_vmptr)) { |
Yu Zhang | 64c7850 | 2021-09-30 01:51:53 +0800 | [diff] [blame] | 1975 | vmx->nested.current_vmptr = INVALID_GPA; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1976 | |
| 1977 | nested_release_evmcs(vcpu); |
| 1978 | |
Vitaly Kuznetsov | 11e3491 | 2019-06-28 13:23:33 +0200 | [diff] [blame] | 1979 | if (kvm_vcpu_map(vcpu, gpa_to_gfn(evmcs_gpa), |
KarimAllah Ahmed | dee9c04 | 2019-01-31 21:24:42 +0100 | [diff] [blame] | 1980 | &vmx->nested.hv_evmcs_map)) |
Vitaly Kuznetsov | b6a0653 | 2020-03-09 16:52:13 +0100 | [diff] [blame] | 1981 | return EVMPTRLD_ERROR; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1982 | |
KarimAllah Ahmed | dee9c04 | 2019-01-31 21:24:42 +0100 | [diff] [blame] | 1983 | vmx->nested.hv_evmcs = vmx->nested.hv_evmcs_map.hva; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1984 | |
| 1985 | /* |
| 1986 | * Currently, KVM only supports eVMCS version 1 |
| 1987 | * (== KVM_EVMCS_VERSION) and thus we expect guest to set this |
| 1988 | * value to first u32 field of eVMCS which should specify eVMCS |
| 1989 | * VersionNumber. |
| 1990 | * |
| 1991 | * Guest should be aware of supported eVMCS versions by host by |
| 1992 | * examining CPUID.0x4000000A.EAX[0:15]. Host userspace VMM is |
| 1993 | * expected to set this CPUID leaf according to the value |
| 1994 | * returned in vmcs_version from nested_enable_evmcs(). |
| 1995 | * |
| 1996 | * However, it turns out that Microsoft Hyper-V fails to comply |
| 1997 | * to their own invented interface: When Hyper-V use eVMCS, it |
| 1998 | * just sets first u32 field of eVMCS to revision_id specified |
| 1999 | * in MSR_IA32_VMX_BASIC. Instead of used eVMCS version number |
| 2000 | * which is one of the supported versions specified in |
| 2001 | * CPUID.0x4000000A.EAX[0:15]. |
| 2002 | * |
| 2003 | * To overcome Hyper-V bug, we accept here either a supported |
| 2004 | * eVMCS version or VMCS12 revision_id as valid values for first |
| 2005 | * u32 field of eVMCS. |
| 2006 | */ |
| 2007 | if ((vmx->nested.hv_evmcs->revision_id != KVM_EVMCS_VERSION) && |
| 2008 | (vmx->nested.hv_evmcs->revision_id != VMCS12_REVISION)) { |
| 2009 | nested_release_evmcs(vcpu); |
Vitaly Kuznetsov | b6a0653 | 2020-03-09 16:52:13 +0100 | [diff] [blame] | 2010 | return EVMPTRLD_VMFAIL; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2011 | } |
| 2012 | |
Vitaly Kuznetsov | 11e3491 | 2019-06-28 13:23:33 +0200 | [diff] [blame] | 2013 | vmx->nested.hv_evmcs_vmptr = evmcs_gpa; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2014 | |
Vitaly Kuznetsov | a21a39c | 2019-06-28 13:23:32 +0200 | [diff] [blame] | 2015 | evmcs_gpa_changed = true; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2016 | /* |
| 2017 | * Unlike normal vmcs12, enlightened vmcs12 is not fully |
| 2018 | * reloaded from guest's memory (read only fields, fields not |
| 2019 | * present in struct hv_enlightened_vmcs, ...). Make sure there |
| 2020 | * are no leftovers. |
| 2021 | */ |
| 2022 | if (from_launch) { |
| 2023 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
| 2024 | memset(vmcs12, 0, sizeof(*vmcs12)); |
| 2025 | vmcs12->hdr.revision_id = VMCS12_REVISION; |
| 2026 | } |
| 2027 | |
| 2028 | } |
Vitaly Kuznetsov | a21a39c | 2019-06-28 13:23:32 +0200 | [diff] [blame] | 2029 | |
| 2030 | /* |
Miaohe Lin | ffdbd50 | 2020-02-07 23:22:45 +0800 | [diff] [blame] | 2031 | * 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] | 2032 | * between different L2 guests as KVM keeps a single VMCS12 per L1. |
| 2033 | */ |
Vitaly Kuznetsov | ed2a480 | 2021-11-29 10:47:03 +0100 | [diff] [blame^] | 2034 | if (from_launch || evmcs_gpa_changed) { |
Vitaly Kuznetsov | a21a39c | 2019-06-28 13:23:32 +0200 | [diff] [blame] | 2035 | vmx->nested.hv_evmcs->hv_clean_fields &= |
| 2036 | ~HV_VMX_ENLIGHTENED_CLEAN_FIELD_ALL; |
| 2037 | |
Vitaly Kuznetsov | ed2a480 | 2021-11-29 10:47:03 +0100 | [diff] [blame^] | 2038 | vmx->nested.force_msr_bitmap_recalc = true; |
| 2039 | } |
| 2040 | |
Vitaly Kuznetsov | b6a0653 | 2020-03-09 16:52:13 +0100 | [diff] [blame] | 2041 | return EVMPTRLD_SUCCEEDED; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2042 | } |
| 2043 | |
Sean Christopherson | 3731905ef | 2019-05-07 08:36:27 -0700 | [diff] [blame] | 2044 | void nested_sync_vmcs12_to_shadow(struct kvm_vcpu *vcpu) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2045 | { |
| 2046 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 2047 | |
Vitaly Kuznetsov | dc31338 | 2021-05-26 15:20:24 +0200 | [diff] [blame] | 2048 | if (evmptr_is_valid(vmx->nested.hv_evmcs_vmptr)) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2049 | copy_vmcs12_to_enlightened(vmx); |
Vitaly Kuznetsov | dc31338 | 2021-05-26 15:20:24 +0200 | [diff] [blame] | 2050 | else |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2051 | copy_vmcs12_to_shadow(vmx); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2052 | |
Sean Christopherson | 3731905ef | 2019-05-07 08:36:27 -0700 | [diff] [blame] | 2053 | vmx->nested.need_vmcs12_to_shadow_sync = false; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2054 | } |
| 2055 | |
| 2056 | static enum hrtimer_restart vmx_preemption_timer_fn(struct hrtimer *timer) |
| 2057 | { |
| 2058 | struct vcpu_vmx *vmx = |
| 2059 | container_of(timer, struct vcpu_vmx, nested.preemption_timer); |
| 2060 | |
| 2061 | vmx->nested.preemption_timer_expired = true; |
| 2062 | kvm_make_request(KVM_REQ_EVENT, &vmx->vcpu); |
| 2063 | kvm_vcpu_kick(&vmx->vcpu); |
| 2064 | |
| 2065 | return HRTIMER_NORESTART; |
| 2066 | } |
| 2067 | |
Peter Shier | 850448f | 2020-05-26 14:51:06 -0700 | [diff] [blame] | 2068 | static u64 vmx_calc_preemption_timer_value(struct kvm_vcpu *vcpu) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2069 | { |
Peter Shier | 850448f | 2020-05-26 14:51:06 -0700 | [diff] [blame] | 2070 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 2071 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
Peter Shier | 850448f | 2020-05-26 14:51:06 -0700 | [diff] [blame] | 2072 | |
| 2073 | u64 l1_scaled_tsc = kvm_read_l1_tsc(vcpu, rdtsc()) >> |
| 2074 | VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE; |
| 2075 | |
| 2076 | if (!vmx->nested.has_preemption_timer_deadline) { |
Makarand Sonare | 8d7fbf0 | 2020-05-26 14:51:07 -0700 | [diff] [blame] | 2077 | vmx->nested.preemption_timer_deadline = |
| 2078 | vmcs12->vmx_preemption_timer_value + l1_scaled_tsc; |
Peter Shier | 850448f | 2020-05-26 14:51:06 -0700 | [diff] [blame] | 2079 | vmx->nested.has_preemption_timer_deadline = true; |
Makarand Sonare | 8d7fbf0 | 2020-05-26 14:51:07 -0700 | [diff] [blame] | 2080 | } |
| 2081 | return vmx->nested.preemption_timer_deadline - l1_scaled_tsc; |
Peter Shier | 850448f | 2020-05-26 14:51:06 -0700 | [diff] [blame] | 2082 | } |
| 2083 | |
| 2084 | static void vmx_start_preemption_timer(struct kvm_vcpu *vcpu, |
| 2085 | u64 preemption_timeout) |
| 2086 | { |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2087 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 2088 | |
| 2089 | /* |
| 2090 | * A timer value of zero is architecturally guaranteed to cause |
| 2091 | * a VMExit prior to executing any instructions in the guest. |
| 2092 | */ |
| 2093 | if (preemption_timeout == 0) { |
| 2094 | vmx_preemption_timer_fn(&vmx->nested.preemption_timer); |
| 2095 | return; |
| 2096 | } |
| 2097 | |
| 2098 | if (vcpu->arch.virtual_tsc_khz == 0) |
| 2099 | return; |
| 2100 | |
| 2101 | preemption_timeout <<= VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE; |
| 2102 | preemption_timeout *= 1000000; |
| 2103 | do_div(preemption_timeout, vcpu->arch.virtual_tsc_khz); |
| 2104 | hrtimer_start(&vmx->nested.preemption_timer, |
Jim Mattson | ada0098 | 2020-05-08 13:36:42 -0700 | [diff] [blame] | 2105 | ktime_add_ns(ktime_get(), preemption_timeout), |
| 2106 | HRTIMER_MODE_ABS_PINNED); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2107 | } |
| 2108 | |
| 2109 | static u64 nested_vmx_calc_efer(struct vcpu_vmx *vmx, struct vmcs12 *vmcs12) |
| 2110 | { |
| 2111 | if (vmx->nested.nested_run_pending && |
| 2112 | (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_EFER)) |
| 2113 | return vmcs12->guest_ia32_efer; |
| 2114 | else if (vmcs12->vm_entry_controls & VM_ENTRY_IA32E_MODE) |
| 2115 | return vmx->vcpu.arch.efer | (EFER_LMA | EFER_LME); |
| 2116 | else |
| 2117 | return vmx->vcpu.arch.efer & ~(EFER_LMA | EFER_LME); |
| 2118 | } |
| 2119 | |
| 2120 | static void prepare_vmcs02_constant_state(struct vcpu_vmx *vmx) |
| 2121 | { |
| 2122 | /* |
| 2123 | * If vmcs02 hasn't been initialized, set the constant vmcs02 state |
| 2124 | * according to L0's settings (vmcs12 is irrelevant here). Host |
| 2125 | * fields that come from L0 and are not constant, e.g. HOST_CR3, |
| 2126 | * will be set as needed prior to VMLAUNCH/VMRESUME. |
| 2127 | */ |
| 2128 | if (vmx->nested.vmcs02_initialized) |
| 2129 | return; |
| 2130 | vmx->nested.vmcs02_initialized = true; |
| 2131 | |
| 2132 | /* |
| 2133 | * We don't care what the EPTP value is we just need to guarantee |
| 2134 | * it's valid so we don't get a false positive when doing early |
| 2135 | * consistency checks. |
| 2136 | */ |
| 2137 | if (enable_ept && nested_early_check) |
Sean Christopherson | 2a40b90 | 2020-07-15 20:41:18 -0700 | [diff] [blame] | 2138 | vmcs_write64(EPT_POINTER, |
| 2139 | construct_eptp(&vmx->vcpu, 0, PT64_ROOT_4LEVEL)); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2140 | |
| 2141 | /* All VMFUNCs are currently emulated through L0 vmexits. */ |
| 2142 | if (cpu_has_vmx_vmfunc()) |
| 2143 | vmcs_write64(VM_FUNCTION_CONTROL, 0); |
| 2144 | |
| 2145 | if (cpu_has_vmx_posted_intr()) |
| 2146 | vmcs_write16(POSTED_INTR_NV, POSTED_INTR_NESTED_VECTOR); |
| 2147 | |
| 2148 | if (cpu_has_vmx_msr_bitmap()) |
| 2149 | vmcs_write64(MSR_BITMAP, __pa(vmx->nested.vmcs02.msr_bitmap)); |
| 2150 | |
Sean Christopherson | 4d6c989 | 2019-05-07 09:06:30 -0700 | [diff] [blame] | 2151 | /* |
Sean Christopherson | c3bb9a2 | 2021-02-12 16:50:07 -0800 | [diff] [blame] | 2152 | * PML is emulated for L2, but never enabled in hardware as the MMU |
| 2153 | * handles A/D emulation. Disabling PML for L2 also avoids having to |
| 2154 | * deal with filtering out L2 GPAs from the buffer. |
Sean Christopherson | 4d6c989 | 2019-05-07 09:06:30 -0700 | [diff] [blame] | 2155 | */ |
| 2156 | if (enable_pml) { |
Sean Christopherson | c3bb9a2 | 2021-02-12 16:50:07 -0800 | [diff] [blame] | 2157 | vmcs_write64(PML_ADDRESS, 0); |
| 2158 | vmcs_write16(GUEST_PML_INDEX, -1); |
Sean Christopherson | 4d6c989 | 2019-05-07 09:06:30 -0700 | [diff] [blame] | 2159 | } |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2160 | |
Sean Christopherson | c538d57 | 2019-05-07 09:06:29 -0700 | [diff] [blame] | 2161 | if (cpu_has_vmx_encls_vmexit()) |
Yu Zhang | 64c7850 | 2021-09-30 01:51:53 +0800 | [diff] [blame] | 2162 | vmcs_write64(ENCLS_EXITING_BITMAP, INVALID_GPA); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2163 | |
| 2164 | /* |
| 2165 | * Set the MSR load/store lists to match L0's settings. Only the |
| 2166 | * addresses are constant (for vmcs02), the counts can change based |
| 2167 | * on L2's behavior, e.g. switching to/from long mode. |
| 2168 | */ |
Aaron Lewis | 662f1d1 | 2019-11-07 21:14:39 -0800 | [diff] [blame] | 2169 | 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] | 2170 | vmcs_write64(VM_EXIT_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.host.val)); |
| 2171 | vmcs_write64(VM_ENTRY_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.guest.val)); |
| 2172 | |
| 2173 | vmx_set_constant_host_state(vmx); |
| 2174 | } |
| 2175 | |
Paolo Bonzini | b1346ab | 2019-06-06 17:24:00 +0200 | [diff] [blame] | 2176 | static void prepare_vmcs02_early_rare(struct vcpu_vmx *vmx, |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2177 | struct vmcs12 *vmcs12) |
| 2178 | { |
| 2179 | prepare_vmcs02_constant_state(vmx); |
| 2180 | |
Yu Zhang | 64c7850 | 2021-09-30 01:51:53 +0800 | [diff] [blame] | 2181 | vmcs_write64(VMCS_LINK_POINTER, INVALID_GPA); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2182 | |
| 2183 | if (enable_vpid) { |
| 2184 | if (nested_cpu_has_vpid(vmcs12) && vmx->nested.vpid02) |
| 2185 | vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->nested.vpid02); |
| 2186 | else |
| 2187 | vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid); |
| 2188 | } |
| 2189 | } |
| 2190 | |
Sean Christopherson | 389ab25 | 2021-08-10 10:19:50 -0700 | [diff] [blame] | 2191 | static void prepare_vmcs02_early(struct vcpu_vmx *vmx, struct loaded_vmcs *vmcs01, |
| 2192 | struct vmcs12 *vmcs12) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2193 | { |
Sean Christopherson | c3bb9a2 | 2021-02-12 16:50:07 -0800 | [diff] [blame] | 2194 | u32 exec_control; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2195 | u64 guest_efer = nested_vmx_calc_efer(vmx, vmcs12); |
| 2196 | |
Vitaly Kuznetsov | 1e9dfbd | 2021-05-26 15:20:16 +0200 | [diff] [blame] | 2197 | 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] | 2198 | prepare_vmcs02_early_rare(vmx, vmcs12); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2199 | |
| 2200 | /* |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2201 | * PIN CONTROLS |
| 2202 | */ |
Sean Christopherson | 389ab25 | 2021-08-10 10:19:50 -0700 | [diff] [blame] | 2203 | exec_control = __pin_controls_get(vmcs01); |
Sean Christopherson | 804939e | 2019-05-07 12:18:05 -0700 | [diff] [blame] | 2204 | exec_control |= (vmcs12->pin_based_vm_exec_control & |
| 2205 | ~PIN_BASED_VMX_PREEMPTION_TIMER); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2206 | |
| 2207 | /* Posted interrupts setting is only taken from vmcs12. */ |
Sean Christopherson | f7782bb8 | 2021-08-10 07:45:26 -0700 | [diff] [blame] | 2208 | vmx->nested.pi_pending = false; |
| 2209 | if (nested_cpu_has_posted_intr(vmcs12)) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2210 | vmx->nested.posted_intr_nv = vmcs12->posted_intr_nv; |
Sean Christopherson | f7782bb8 | 2021-08-10 07:45:26 -0700 | [diff] [blame] | 2211 | else |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2212 | exec_control &= ~PIN_BASED_POSTED_INTR; |
Sean Christopherson | 3af80fe | 2019-05-07 12:18:00 -0700 | [diff] [blame] | 2213 | pin_controls_set(vmx, exec_control); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2214 | |
| 2215 | /* |
| 2216 | * EXEC CONTROLS |
| 2217 | */ |
Sean Christopherson | 389ab25 | 2021-08-10 10:19:50 -0700 | [diff] [blame] | 2218 | exec_control = __exec_controls_get(vmcs01); /* L0's desires */ |
Xiaoyao Li | 9dadc2f | 2019-12-06 16:45:24 +0800 | [diff] [blame] | 2219 | exec_control &= ~CPU_BASED_INTR_WINDOW_EXITING; |
Xiaoyao Li | 4e2a0bc | 2019-12-06 16:45:25 +0800 | [diff] [blame] | 2220 | exec_control &= ~CPU_BASED_NMI_WINDOW_EXITING; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2221 | exec_control &= ~CPU_BASED_TPR_SHADOW; |
| 2222 | exec_control |= vmcs12->cpu_based_vm_exec_control; |
| 2223 | |
Liran Alon | 02d496cf | 2019-11-11 14:30:55 +0200 | [diff] [blame] | 2224 | vmx->nested.l1_tpr_threshold = -1; |
Sean Christopherson | ca2f546 | 2019-05-07 09:06:33 -0700 | [diff] [blame] | 2225 | if (exec_control & CPU_BASED_TPR_SHADOW) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2226 | vmcs_write32(TPR_THRESHOLD, vmcs12->tpr_threshold); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2227 | #ifdef CONFIG_X86_64 |
Sean Christopherson | ca2f546 | 2019-05-07 09:06:33 -0700 | [diff] [blame] | 2228 | else |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2229 | exec_control |= CPU_BASED_CR8_LOAD_EXITING | |
| 2230 | CPU_BASED_CR8_STORE_EXITING; |
| 2231 | #endif |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2232 | |
| 2233 | /* |
| 2234 | * A vmexit (to either L1 hypervisor or L0 userspace) is always needed |
| 2235 | * for I/O port accesses. |
| 2236 | */ |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2237 | exec_control |= CPU_BASED_UNCOND_IO_EXITING; |
Sean Christopherson | de0286b | 2019-05-07 12:18:01 -0700 | [diff] [blame] | 2238 | exec_control &= ~CPU_BASED_USE_IO_BITMAPS; |
| 2239 | |
| 2240 | /* |
| 2241 | * This bit will be computed in nested_get_vmcs12_pages, because |
| 2242 | * we do not have access to L1's MSR bitmap yet. For now, keep |
| 2243 | * the same bit as before, hoping to avoid multiple VMWRITEs that |
| 2244 | * only set/clear this bit. |
| 2245 | */ |
| 2246 | exec_control &= ~CPU_BASED_USE_MSR_BITMAPS; |
| 2247 | exec_control |= exec_controls_get(vmx) & CPU_BASED_USE_MSR_BITMAPS; |
| 2248 | |
Sean Christopherson | 3af80fe | 2019-05-07 12:18:00 -0700 | [diff] [blame] | 2249 | exec_controls_set(vmx, exec_control); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2250 | |
| 2251 | /* |
| 2252 | * SECONDARY EXEC CONTROLS |
| 2253 | */ |
| 2254 | if (cpu_has_secondary_exec_ctrls()) { |
Sean Christopherson | 389ab25 | 2021-08-10 10:19:50 -0700 | [diff] [blame] | 2255 | exec_control = __secondary_exec_controls_get(vmcs01); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2256 | |
| 2257 | /* Take the following fields only from vmcs12 */ |
| 2258 | exec_control &= ~(SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES | |
Sean Christopherson | 389ab25 | 2021-08-10 10:19:50 -0700 | [diff] [blame] | 2259 | SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2260 | SECONDARY_EXEC_ENABLE_INVPCID | |
Sean Christopherson | 7f3603b | 2020-09-23 09:50:47 -0700 | [diff] [blame] | 2261 | SECONDARY_EXEC_ENABLE_RDTSCP | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2262 | SECONDARY_EXEC_XSAVES | |
Tao Xu | e69e72fa | 2019-07-16 14:55:49 +0800 | [diff] [blame] | 2263 | SECONDARY_EXEC_ENABLE_USR_WAIT_PAUSE | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2264 | SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY | |
| 2265 | SECONDARY_EXEC_APIC_REGISTER_VIRT | |
Ilias Stamatis | d041b5e | 2021-05-26 19:44:17 +0100 | [diff] [blame] | 2266 | SECONDARY_EXEC_ENABLE_VMFUNC | |
Sean Christopherson | 389ab25 | 2021-08-10 10:19:50 -0700 | [diff] [blame] | 2267 | SECONDARY_EXEC_TSC_SCALING | |
| 2268 | SECONDARY_EXEC_DESC); |
| 2269 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2270 | if (nested_cpu_has(vmcs12, |
Sean Christopherson | c3bb9a2 | 2021-02-12 16:50:07 -0800 | [diff] [blame] | 2271 | CPU_BASED_ACTIVATE_SECONDARY_CONTROLS)) |
| 2272 | exec_control |= vmcs12->secondary_vm_exec_control; |
| 2273 | |
| 2274 | /* PML is emulated and never enabled in hardware for L2. */ |
| 2275 | exec_control &= ~SECONDARY_EXEC_ENABLE_PML; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2276 | |
| 2277 | /* VMCS shadowing for L2 is emulated for now */ |
| 2278 | exec_control &= ~SECONDARY_EXEC_SHADOW_VMCS; |
| 2279 | |
Sean Christopherson | 469debd | 2019-05-07 12:18:02 -0700 | [diff] [blame] | 2280 | /* |
| 2281 | * Preset *DT exiting when emulating UMIP, so that vmx_set_cr4() |
| 2282 | * will not have to rewrite the controls just for this bit. |
| 2283 | */ |
| 2284 | if (!boot_cpu_has(X86_FEATURE_UMIP) && vmx_umip_emulated() && |
| 2285 | (vmcs12->guest_cr4 & X86_CR4_UMIP)) |
| 2286 | exec_control |= SECONDARY_EXEC_DESC; |
| 2287 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2288 | if (exec_control & SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY) |
| 2289 | vmcs_write16(GUEST_INTR_STATUS, |
| 2290 | vmcs12->guest_intr_status); |
| 2291 | |
Krish Sadhukhan | bddd82d | 2020-09-21 08:10:25 +0000 | [diff] [blame] | 2292 | if (!nested_cpu_has2(vmcs12, SECONDARY_EXEC_UNRESTRICTED_GUEST)) |
| 2293 | exec_control &= ~SECONDARY_EXEC_UNRESTRICTED_GUEST; |
| 2294 | |
Sean Christopherson | 72add91 | 2021-04-12 16:21:42 +1200 | [diff] [blame] | 2295 | if (exec_control & SECONDARY_EXEC_ENCLS_EXITING) |
| 2296 | vmx_write_encls_bitmap(&vmx->vcpu, vmcs12); |
| 2297 | |
Sean Christopherson | 3af80fe | 2019-05-07 12:18:00 -0700 | [diff] [blame] | 2298 | secondary_exec_controls_set(vmx, exec_control); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2299 | } |
| 2300 | |
| 2301 | /* |
| 2302 | * ENTRY CONTROLS |
| 2303 | * |
| 2304 | * vmcs12's VM_{ENTRY,EXIT}_LOAD_IA32_EFER and VM_ENTRY_IA32E_MODE |
| 2305 | * are emulated by vmx_set_efer() in prepare_vmcs02(), but speculate |
| 2306 | * on the related bits (if supported by the CPU) in the hope that |
| 2307 | * we can avoid VMWrites during vmx_set_efer(). |
| 2308 | */ |
Sean Christopherson | 389ab25 | 2021-08-10 10:19:50 -0700 | [diff] [blame] | 2309 | exec_control = __vm_entry_controls_get(vmcs01); |
| 2310 | exec_control |= vmcs12->vm_entry_controls; |
| 2311 | exec_control &= ~(VM_ENTRY_IA32E_MODE | VM_ENTRY_LOAD_IA32_EFER); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2312 | if (cpu_has_load_ia32_efer()) { |
| 2313 | if (guest_efer & EFER_LMA) |
| 2314 | exec_control |= VM_ENTRY_IA32E_MODE; |
| 2315 | if (guest_efer != host_efer) |
| 2316 | exec_control |= VM_ENTRY_LOAD_IA32_EFER; |
| 2317 | } |
Sean Christopherson | 3af80fe | 2019-05-07 12:18:00 -0700 | [diff] [blame] | 2318 | vm_entry_controls_set(vmx, exec_control); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2319 | |
| 2320 | /* |
| 2321 | * EXIT CONTROLS |
| 2322 | * |
| 2323 | * L2->L1 exit controls are emulated - the hardware exit is to L0 so |
| 2324 | * we should use its exit controls. Note that VM_EXIT_LOAD_IA32_EFER |
| 2325 | * bits may be modified by vmx_set_efer() in prepare_vmcs02(). |
| 2326 | */ |
Sean Christopherson | 389ab25 | 2021-08-10 10:19:50 -0700 | [diff] [blame] | 2327 | exec_control = __vm_exit_controls_get(vmcs01); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2328 | if (cpu_has_load_ia32_efer() && guest_efer != host_efer) |
| 2329 | exec_control |= VM_EXIT_LOAD_IA32_EFER; |
Sean Christopherson | 389ab25 | 2021-08-10 10:19:50 -0700 | [diff] [blame] | 2330 | else |
| 2331 | exec_control &= ~VM_EXIT_LOAD_IA32_EFER; |
Sean Christopherson | 3af80fe | 2019-05-07 12:18:00 -0700 | [diff] [blame] | 2332 | vm_exit_controls_set(vmx, exec_control); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2333 | |
| 2334 | /* |
| 2335 | * Interrupt/Exception Fields |
| 2336 | */ |
| 2337 | if (vmx->nested.nested_run_pending) { |
| 2338 | vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, |
| 2339 | vmcs12->vm_entry_intr_info_field); |
| 2340 | vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE, |
| 2341 | vmcs12->vm_entry_exception_error_code); |
| 2342 | vmcs_write32(VM_ENTRY_INSTRUCTION_LEN, |
| 2343 | vmcs12->vm_entry_instruction_len); |
| 2344 | vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, |
| 2345 | vmcs12->guest_interruptibility_info); |
| 2346 | vmx->loaded_vmcs->nmi_known_unmasked = |
| 2347 | !(vmcs12->guest_interruptibility_info & GUEST_INTR_STATE_NMI); |
| 2348 | } else { |
| 2349 | vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0); |
| 2350 | } |
| 2351 | } |
| 2352 | |
Paolo Bonzini | b1346ab | 2019-06-06 17:24:00 +0200 | [diff] [blame] | 2353 | static void prepare_vmcs02_rare(struct vcpu_vmx *vmx, struct vmcs12 *vmcs12) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2354 | { |
| 2355 | struct hv_enlightened_vmcs *hv_evmcs = vmx->nested.hv_evmcs; |
| 2356 | |
| 2357 | if (!hv_evmcs || !(hv_evmcs->hv_clean_fields & |
| 2358 | HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP2)) { |
| 2359 | vmcs_write16(GUEST_ES_SELECTOR, vmcs12->guest_es_selector); |
| 2360 | vmcs_write16(GUEST_CS_SELECTOR, vmcs12->guest_cs_selector); |
| 2361 | vmcs_write16(GUEST_SS_SELECTOR, vmcs12->guest_ss_selector); |
| 2362 | vmcs_write16(GUEST_DS_SELECTOR, vmcs12->guest_ds_selector); |
| 2363 | vmcs_write16(GUEST_FS_SELECTOR, vmcs12->guest_fs_selector); |
| 2364 | vmcs_write16(GUEST_GS_SELECTOR, vmcs12->guest_gs_selector); |
| 2365 | vmcs_write16(GUEST_LDTR_SELECTOR, vmcs12->guest_ldtr_selector); |
| 2366 | vmcs_write16(GUEST_TR_SELECTOR, vmcs12->guest_tr_selector); |
| 2367 | vmcs_write32(GUEST_ES_LIMIT, vmcs12->guest_es_limit); |
| 2368 | vmcs_write32(GUEST_CS_LIMIT, vmcs12->guest_cs_limit); |
| 2369 | vmcs_write32(GUEST_SS_LIMIT, vmcs12->guest_ss_limit); |
| 2370 | vmcs_write32(GUEST_DS_LIMIT, vmcs12->guest_ds_limit); |
| 2371 | vmcs_write32(GUEST_FS_LIMIT, vmcs12->guest_fs_limit); |
| 2372 | vmcs_write32(GUEST_GS_LIMIT, vmcs12->guest_gs_limit); |
| 2373 | vmcs_write32(GUEST_LDTR_LIMIT, vmcs12->guest_ldtr_limit); |
| 2374 | vmcs_write32(GUEST_TR_LIMIT, vmcs12->guest_tr_limit); |
| 2375 | vmcs_write32(GUEST_GDTR_LIMIT, vmcs12->guest_gdtr_limit); |
| 2376 | vmcs_write32(GUEST_IDTR_LIMIT, vmcs12->guest_idtr_limit); |
Sean Christopherson | 1c6f0b4 | 2019-05-07 08:36:25 -0700 | [diff] [blame] | 2377 | vmcs_write32(GUEST_CS_AR_BYTES, vmcs12->guest_cs_ar_bytes); |
| 2378 | vmcs_write32(GUEST_SS_AR_BYTES, vmcs12->guest_ss_ar_bytes); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2379 | vmcs_write32(GUEST_ES_AR_BYTES, vmcs12->guest_es_ar_bytes); |
| 2380 | vmcs_write32(GUEST_DS_AR_BYTES, vmcs12->guest_ds_ar_bytes); |
| 2381 | vmcs_write32(GUEST_FS_AR_BYTES, vmcs12->guest_fs_ar_bytes); |
| 2382 | vmcs_write32(GUEST_GS_AR_BYTES, vmcs12->guest_gs_ar_bytes); |
| 2383 | vmcs_write32(GUEST_LDTR_AR_BYTES, vmcs12->guest_ldtr_ar_bytes); |
| 2384 | vmcs_write32(GUEST_TR_AR_BYTES, vmcs12->guest_tr_ar_bytes); |
| 2385 | vmcs_writel(GUEST_ES_BASE, vmcs12->guest_es_base); |
| 2386 | vmcs_writel(GUEST_CS_BASE, vmcs12->guest_cs_base); |
| 2387 | vmcs_writel(GUEST_SS_BASE, vmcs12->guest_ss_base); |
| 2388 | vmcs_writel(GUEST_DS_BASE, vmcs12->guest_ds_base); |
| 2389 | vmcs_writel(GUEST_FS_BASE, vmcs12->guest_fs_base); |
| 2390 | vmcs_writel(GUEST_GS_BASE, vmcs12->guest_gs_base); |
| 2391 | vmcs_writel(GUEST_LDTR_BASE, vmcs12->guest_ldtr_base); |
| 2392 | vmcs_writel(GUEST_TR_BASE, vmcs12->guest_tr_base); |
| 2393 | vmcs_writel(GUEST_GDTR_BASE, vmcs12->guest_gdtr_base); |
| 2394 | vmcs_writel(GUEST_IDTR_BASE, vmcs12->guest_idtr_base); |
Sean Christopherson | fc387d8 | 2020-09-23 11:44:46 -0700 | [diff] [blame] | 2395 | |
| 2396 | vmx->segment_cache.bitmask = 0; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2397 | } |
| 2398 | |
| 2399 | if (!hv_evmcs || !(hv_evmcs->hv_clean_fields & |
| 2400 | HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP1)) { |
| 2401 | vmcs_write32(GUEST_SYSENTER_CS, vmcs12->guest_sysenter_cs); |
| 2402 | vmcs_writel(GUEST_PENDING_DBG_EXCEPTIONS, |
| 2403 | vmcs12->guest_pending_dbg_exceptions); |
| 2404 | vmcs_writel(GUEST_SYSENTER_ESP, vmcs12->guest_sysenter_esp); |
| 2405 | vmcs_writel(GUEST_SYSENTER_EIP, vmcs12->guest_sysenter_eip); |
| 2406 | |
| 2407 | /* |
| 2408 | * L1 may access the L2's PDPTR, so save them to construct |
| 2409 | * vmcs12 |
| 2410 | */ |
| 2411 | if (enable_ept) { |
| 2412 | vmcs_write64(GUEST_PDPTR0, vmcs12->guest_pdptr0); |
| 2413 | vmcs_write64(GUEST_PDPTR1, vmcs12->guest_pdptr1); |
| 2414 | vmcs_write64(GUEST_PDPTR2, vmcs12->guest_pdptr2); |
| 2415 | vmcs_write64(GUEST_PDPTR3, vmcs12->guest_pdptr3); |
| 2416 | } |
Sean Christopherson | c27e5b0 | 2019-05-07 09:06:39 -0700 | [diff] [blame] | 2417 | |
| 2418 | if (kvm_mpx_supported() && vmx->nested.nested_run_pending && |
| 2419 | (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_BNDCFGS)) |
| 2420 | vmcs_write64(GUEST_BNDCFGS, vmcs12->guest_bndcfgs); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2421 | } |
| 2422 | |
| 2423 | if (nested_cpu_has_xsaves(vmcs12)) |
| 2424 | vmcs_write64(XSS_EXIT_BITMAP, vmcs12->xss_exit_bitmap); |
| 2425 | |
| 2426 | /* |
| 2427 | * Whether page-faults are trapped is determined by a combination of |
Paolo Bonzini | a0c1343 | 2020-07-10 17:48:08 +0200 | [diff] [blame] | 2428 | * 3 settings: PFEC_MASK, PFEC_MATCH and EXCEPTION_BITMAP.PF. If L0 |
| 2429 | * doesn't care about page faults then we should set all of these to |
| 2430 | * L1's desires. However, if L0 does care about (some) page faults, it |
| 2431 | * is not easy (if at all possible?) to merge L0 and L1's desires, we |
| 2432 | * simply ask to exit on each and every L2 page fault. This is done by |
| 2433 | * setting MASK=MATCH=0 and (see below) EB.PF=1. |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2434 | * Note that below we don't need special code to set EB.PF beyond the |
| 2435 | * "or"ing of the EB of vmcs01 and vmcs12, because when enable_ept, |
| 2436 | * vmcs01's EB.PF is 0 so the "or" will take vmcs12's value, and when |
| 2437 | * !enable_ept, EB.PF is 1, so the "or" will always be 1. |
| 2438 | */ |
Paolo Bonzini | a0c1343 | 2020-07-10 17:48:08 +0200 | [diff] [blame] | 2439 | if (vmx_need_pf_intercept(&vmx->vcpu)) { |
| 2440 | /* |
| 2441 | * TODO: if both L0 and L1 need the same MASK and MATCH, |
| 2442 | * go ahead and use it? |
| 2443 | */ |
| 2444 | vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK, 0); |
| 2445 | vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH, 0); |
| 2446 | } else { |
| 2447 | vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK, vmcs12->page_fault_error_code_mask); |
| 2448 | vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH, vmcs12->page_fault_error_code_match); |
| 2449 | } |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2450 | |
| 2451 | if (cpu_has_vmx_apicv()) { |
| 2452 | vmcs_write64(EOI_EXIT_BITMAP0, vmcs12->eoi_exit_bitmap0); |
| 2453 | vmcs_write64(EOI_EXIT_BITMAP1, vmcs12->eoi_exit_bitmap1); |
| 2454 | vmcs_write64(EOI_EXIT_BITMAP2, vmcs12->eoi_exit_bitmap2); |
| 2455 | vmcs_write64(EOI_EXIT_BITMAP3, vmcs12->eoi_exit_bitmap3); |
| 2456 | } |
| 2457 | |
Aaron Lewis | 662f1d1 | 2019-11-07 21:14:39 -0800 | [diff] [blame] | 2458 | /* |
| 2459 | * Make sure the msr_autostore list is up to date before we set the |
| 2460 | * count in the vmcs02. |
| 2461 | */ |
| 2462 | prepare_vmx_msr_autostore_list(&vmx->vcpu, MSR_IA32_TSC); |
| 2463 | |
| 2464 | vmcs_write32(VM_EXIT_MSR_STORE_COUNT, vmx->msr_autostore.guest.nr); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2465 | vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, vmx->msr_autoload.host.nr); |
| 2466 | vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, vmx->msr_autoload.guest.nr); |
| 2467 | |
| 2468 | set_cr4_guest_host_mask(vmx); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2469 | } |
| 2470 | |
| 2471 | /* |
| 2472 | * prepare_vmcs02 is called when the L1 guest hypervisor runs its nested |
| 2473 | * L2 guest. L1 has a vmcs for L2 (vmcs12), and this function "merges" it |
| 2474 | * with L0's requirements for its guest (a.k.a. vmcs01), so we can run the L2 |
| 2475 | * guest in a way that will both be appropriate to L1's requests, and our |
| 2476 | * needs. In addition to modifying the active vmcs (which is vmcs02), this |
| 2477 | * function also has additional necessary side-effects, like setting various |
| 2478 | * vcpu->arch fields. |
| 2479 | * Returns 0 on success, 1 on failure. Invalid state exit qualification code |
| 2480 | * is assigned to entry_failure_code on failure. |
| 2481 | */ |
| 2482 | static int prepare_vmcs02(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12, |
Maxim Levitsky | 0f85722 | 2021-06-07 12:02:00 +0300 | [diff] [blame] | 2483 | bool from_vmentry, |
Sean Christopherson | 68cda40 | 2020-05-11 15:05:29 -0700 | [diff] [blame] | 2484 | enum vm_entry_failure_code *entry_failure_code) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2485 | { |
| 2486 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Sean Christopherson | c7554efc | 2019-05-07 09:06:40 -0700 | [diff] [blame] | 2487 | bool load_guest_pdptrs_vmcs12 = false; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2488 | |
Vitaly Kuznetsov | 1e9dfbd | 2021-05-26 15:20:16 +0200 | [diff] [blame] | 2489 | 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] | 2490 | prepare_vmcs02_rare(vmx, vmcs12); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2491 | vmx->nested.dirty_vmcs12 = false; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2492 | |
Vitaly Kuznetsov | 1e9dfbd | 2021-05-26 15:20:16 +0200 | [diff] [blame] | 2493 | load_guest_pdptrs_vmcs12 = !evmptr_is_valid(vmx->nested.hv_evmcs_vmptr) || |
| 2494 | !(vmx->nested.hv_evmcs->hv_clean_fields & |
Sean Christopherson | c7554efc | 2019-05-07 09:06:40 -0700 | [diff] [blame] | 2495 | HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP1); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2496 | } |
| 2497 | |
| 2498 | if (vmx->nested.nested_run_pending && |
| 2499 | (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS)) { |
| 2500 | kvm_set_dr(vcpu, 7, vmcs12->guest_dr7); |
| 2501 | vmcs_write64(GUEST_IA32_DEBUGCTL, vmcs12->guest_ia32_debugctl); |
| 2502 | } else { |
| 2503 | kvm_set_dr(vcpu, 7, vcpu->arch.dr7); |
| 2504 | vmcs_write64(GUEST_IA32_DEBUGCTL, vmx->nested.vmcs01_debugctl); |
| 2505 | } |
Sean Christopherson | 3b013a2 | 2019-05-07 09:06:28 -0700 | [diff] [blame] | 2506 | if (kvm_mpx_supported() && (!vmx->nested.nested_run_pending || |
| 2507 | !(vmcs12->vm_entry_controls & VM_ENTRY_LOAD_BNDCFGS))) |
| 2508 | vmcs_write64(GUEST_BNDCFGS, vmx->nested.vmcs01_guest_bndcfgs); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2509 | vmx_set_rflags(vcpu, vmcs12->guest_rflags); |
| 2510 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2511 | /* EXCEPTION_BITMAP and CR0_GUEST_HOST_MASK should basically be the |
| 2512 | * bitwise-or of what L1 wants to trap for L2, and what we want to |
| 2513 | * trap. Note that CR0.TS also needs updating - we do this later. |
| 2514 | */ |
Jason Baron | b6a7cc3 | 2021-01-14 22:27:54 -0500 | [diff] [blame] | 2515 | vmx_update_exception_bitmap(vcpu); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2516 | vcpu->arch.cr0_guest_owned_bits &= ~vmcs12->cr0_guest_host_mask; |
| 2517 | vmcs_writel(CR0_GUEST_HOST_MASK, ~vcpu->arch.cr0_guest_owned_bits); |
| 2518 | |
| 2519 | if (vmx->nested.nested_run_pending && |
| 2520 | (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_PAT)) { |
| 2521 | vmcs_write64(GUEST_IA32_PAT, vmcs12->guest_ia32_pat); |
| 2522 | vcpu->arch.pat = vmcs12->guest_ia32_pat; |
| 2523 | } else if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) { |
| 2524 | vmcs_write64(GUEST_IA32_PAT, vmx->vcpu.arch.pat); |
| 2525 | } |
| 2526 | |
Ilias Stamatis | d041b5e | 2021-05-26 19:44:17 +0100 | [diff] [blame] | 2527 | vcpu->arch.tsc_offset = kvm_calc_nested_tsc_offset( |
| 2528 | vcpu->arch.l1_tsc_offset, |
| 2529 | vmx_get_l2_tsc_offset(vcpu), |
| 2530 | vmx_get_l2_tsc_multiplier(vcpu)); |
| 2531 | |
| 2532 | vcpu->arch.tsc_scaling_ratio = kvm_calc_nested_tsc_multiplier( |
| 2533 | vcpu->arch.l1_tsc_scaling_ratio, |
| 2534 | vmx_get_l2_tsc_multiplier(vcpu)); |
| 2535 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2536 | vmcs_write64(TSC_OFFSET, vcpu->arch.tsc_offset); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2537 | if (kvm_has_tsc_control) |
Ilias Stamatis | 1ab9287 | 2021-06-07 11:54:38 +0100 | [diff] [blame] | 2538 | vmcs_write64(TSC_MULTIPLIER, vcpu->arch.tsc_scaling_ratio); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2539 | |
Sean Christopherson | 50b265a | 2020-03-20 14:28:19 -0700 | [diff] [blame] | 2540 | nested_vmx_transition_tlb_flush(vcpu, vmcs12, true); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2541 | |
| 2542 | if (nested_cpu_has_ept(vmcs12)) |
| 2543 | nested_ept_init_mmu_context(vcpu); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2544 | |
| 2545 | /* |
| 2546 | * This sets GUEST_CR0 to vmcs12->guest_cr0, possibly modifying those |
| 2547 | * bits which we consider mandatory enabled. |
| 2548 | * The CR0_READ_SHADOW is what L2 should have expected to read given |
| 2549 | * the specifications by L1; It's not enough to take |
| 2550 | * vmcs12->cr0_read_shadow because on our cr0_guest_host_mask we we |
| 2551 | * have more bits than L1 expected. |
| 2552 | */ |
| 2553 | vmx_set_cr0(vcpu, vmcs12->guest_cr0); |
| 2554 | vmcs_writel(CR0_READ_SHADOW, nested_read_cr0(vmcs12)); |
| 2555 | |
| 2556 | vmx_set_cr4(vcpu, vmcs12->guest_cr4); |
| 2557 | vmcs_writel(CR4_READ_SHADOW, nested_read_cr4(vmcs12)); |
| 2558 | |
| 2559 | vcpu->arch.efer = nested_vmx_calc_efer(vmx, vmcs12); |
| 2560 | /* Note: may modify VM_ENTRY/EXIT_CONTROLS and GUEST/HOST_IA32_EFER */ |
| 2561 | vmx_set_efer(vcpu, vcpu->arch.efer); |
| 2562 | |
| 2563 | /* |
| 2564 | * Guest state is invalid and unrestricted guest is disabled, |
| 2565 | * which means L1 attempted VMEntry to L2 with invalid state. |
| 2566 | * Fail the VMEntry. |
Maxim Levitsky | c8607e4 | 2021-09-13 17:09:53 +0300 | [diff] [blame] | 2567 | * |
| 2568 | * However when force loading the guest state (SMM exit or |
| 2569 | * loading nested state after migration, it is possible to |
| 2570 | * have invalid guest state now, which will be later fixed by |
| 2571 | * restoring L2 register state |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2572 | */ |
Maxim Levitsky | c8607e4 | 2021-09-13 17:09:53 +0300 | [diff] [blame] | 2573 | if (CC(from_vmentry && !vmx_guest_state_valid(vcpu))) { |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2574 | *entry_failure_code = ENTRY_FAIL_DEFAULT; |
Sean Christopherson | c80add0 | 2019-04-11 12:18:09 -0700 | [diff] [blame] | 2575 | return -EINVAL; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2576 | } |
| 2577 | |
| 2578 | /* Shadow page tables on either EPT or shadow page tables. */ |
| 2579 | 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] | 2580 | from_vmentry, entry_failure_code)) |
Sean Christopherson | c80add0 | 2019-04-11 12:18:09 -0700 | [diff] [blame] | 2581 | return -EINVAL; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2582 | |
Sean Christopherson | 04f11ef | 2019-09-27 14:45:16 -0700 | [diff] [blame] | 2583 | /* |
| 2584 | * Immediately write vmcs02.GUEST_CR3. It will be propagated to vmcs12 |
| 2585 | * on nested VM-Exit, which can occur without actually running L2 and |
Paolo Bonzini | 727a7e2 | 2020-03-05 03:52:50 -0500 | [diff] [blame] | 2586 | * 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] | 2587 | * vmcs12.GUEST_ACTIVITYSTATE=HLT, in which case KVM will intercept the |
| 2588 | * transition to HLT instead of running L2. |
| 2589 | */ |
| 2590 | if (enable_ept) |
| 2591 | vmcs_writel(GUEST_CR3, vmcs12->guest_cr3); |
| 2592 | |
Sean Christopherson | c7554efc | 2019-05-07 09:06:40 -0700 | [diff] [blame] | 2593 | /* Late preparation of GUEST_PDPTRs now that EFER and CRs are set. */ |
| 2594 | if (load_guest_pdptrs_vmcs12 && nested_cpu_has_ept(vmcs12) && |
| 2595 | is_pae_paging(vcpu)) { |
| 2596 | vmcs_write64(GUEST_PDPTR0, vmcs12->guest_pdptr0); |
| 2597 | vmcs_write64(GUEST_PDPTR1, vmcs12->guest_pdptr1); |
| 2598 | vmcs_write64(GUEST_PDPTR2, vmcs12->guest_pdptr2); |
| 2599 | vmcs_write64(GUEST_PDPTR3, vmcs12->guest_pdptr3); |
| 2600 | } |
| 2601 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2602 | if (!enable_ept) |
| 2603 | vcpu->arch.walk_mmu->inject_page_fault = vmx_inject_page_fault_nested; |
| 2604 | |
Oliver Upton | 71f7347 | 2019-11-13 16:17:19 -0800 | [diff] [blame] | 2605 | if ((vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL) && |
Oliver Upton | d196842 | 2019-12-13 16:33:58 -0800 | [diff] [blame] | 2606 | WARN_ON_ONCE(kvm_set_msr(vcpu, MSR_CORE_PERF_GLOBAL_CTRL, |
Dan Carpenter | bfbb307 | 2021-11-30 15:53:37 +0300 | [diff] [blame] | 2607 | vmcs12->guest_ia32_perf_global_ctrl))) { |
| 2608 | *entry_failure_code = ENTRY_FAIL_DEFAULT; |
Oliver Upton | 71f7347 | 2019-11-13 16:17:19 -0800 | [diff] [blame] | 2609 | return -EINVAL; |
Dan Carpenter | bfbb307 | 2021-11-30 15:53:37 +0300 | [diff] [blame] | 2610 | } |
Oliver Upton | 71f7347 | 2019-11-13 16:17:19 -0800 | [diff] [blame] | 2611 | |
Paolo Bonzini | e9c16c7 | 2019-04-30 22:07:26 +0200 | [diff] [blame] | 2612 | kvm_rsp_write(vcpu, vmcs12->guest_rsp); |
| 2613 | kvm_rip_write(vcpu, vmcs12->guest_rip); |
Vitaly Kuznetsov | dc31338 | 2021-05-26 15:20:24 +0200 | [diff] [blame] | 2614 | |
| 2615 | /* |
| 2616 | * It was observed that genuine Hyper-V running in L1 doesn't reset |
| 2617 | * 'hv_clean_fields' by itself, it only sets the corresponding dirty |
| 2618 | * bits when it changes a field in eVMCS. Mark all fields as clean |
| 2619 | * here. |
| 2620 | */ |
| 2621 | if (evmptr_is_valid(vmx->nested.hv_evmcs_vmptr)) |
| 2622 | vmx->nested.hv_evmcs->hv_clean_fields |= |
| 2623 | HV_VMX_ENLIGHTENED_CLEAN_FIELD_ALL; |
| 2624 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2625 | return 0; |
| 2626 | } |
| 2627 | |
| 2628 | static int nested_vmx_check_nmi_controls(struct vmcs12 *vmcs12) |
| 2629 | { |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 2630 | if (CC(!nested_cpu_has_nmi_exiting(vmcs12) && |
| 2631 | nested_cpu_has_virtual_nmis(vmcs12))) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2632 | return -EINVAL; |
| 2633 | |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 2634 | if (CC(!nested_cpu_has_virtual_nmis(vmcs12) && |
Xiaoyao Li | 4e2a0bc | 2019-12-06 16:45:25 +0800 | [diff] [blame] | 2635 | nested_cpu_has(vmcs12, CPU_BASED_NMI_WINDOW_EXITING))) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2636 | return -EINVAL; |
| 2637 | |
| 2638 | return 0; |
| 2639 | } |
| 2640 | |
Sean Christopherson | ac6389a | 2020-03-02 18:02:38 -0800 | [diff] [blame] | 2641 | 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] | 2642 | { |
| 2643 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2644 | |
| 2645 | /* Check for memory type validity */ |
Sean Christopherson | ac6389a | 2020-03-02 18:02:38 -0800 | [diff] [blame] | 2646 | switch (new_eptp & VMX_EPTP_MT_MASK) { |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2647 | case VMX_EPTP_MT_UC: |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 2648 | if (CC(!(vmx->nested.msrs.ept_caps & VMX_EPTP_UC_BIT))) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2649 | return false; |
| 2650 | break; |
| 2651 | case VMX_EPTP_MT_WB: |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 2652 | if (CC(!(vmx->nested.msrs.ept_caps & VMX_EPTP_WB_BIT))) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2653 | return false; |
| 2654 | break; |
| 2655 | default: |
| 2656 | return false; |
| 2657 | } |
| 2658 | |
Sean Christopherson | bb1fcc7 | 2020-03-02 18:02:36 -0800 | [diff] [blame] | 2659 | /* Page-walk levels validity. */ |
Sean Christopherson | ac6389a | 2020-03-02 18:02:38 -0800 | [diff] [blame] | 2660 | switch (new_eptp & VMX_EPTP_PWL_MASK) { |
Sean Christopherson | bb1fcc7 | 2020-03-02 18:02:36 -0800 | [diff] [blame] | 2661 | case VMX_EPTP_PWL_5: |
| 2662 | if (CC(!(vmx->nested.msrs.ept_caps & VMX_EPT_PAGE_WALK_5_BIT))) |
| 2663 | return false; |
| 2664 | break; |
| 2665 | case VMX_EPTP_PWL_4: |
| 2666 | if (CC(!(vmx->nested.msrs.ept_caps & VMX_EPT_PAGE_WALK_4_BIT))) |
| 2667 | return false; |
| 2668 | break; |
| 2669 | default: |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2670 | return false; |
Sean Christopherson | bb1fcc7 | 2020-03-02 18:02:36 -0800 | [diff] [blame] | 2671 | } |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2672 | |
| 2673 | /* Reserved bits should not be set */ |
Sean Christopherson | 636e8b7 | 2021-02-03 16:01:10 -0800 | [diff] [blame] | 2674 | 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] | 2675 | return false; |
| 2676 | |
| 2677 | /* AD, if set, should be supported */ |
Sean Christopherson | ac6389a | 2020-03-02 18:02:38 -0800 | [diff] [blame] | 2678 | if (new_eptp & VMX_EPTP_AD_ENABLE_BIT) { |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 2679 | if (CC(!(vmx->nested.msrs.ept_caps & VMX_EPT_AD_BIT))) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2680 | return false; |
| 2681 | } |
| 2682 | |
| 2683 | return true; |
| 2684 | } |
| 2685 | |
Krish Sadhukhan | 461b4ba | 2018-12-12 13:30:07 -0500 | [diff] [blame] | 2686 | /* |
| 2687 | * Checks related to VM-Execution Control Fields |
| 2688 | */ |
| 2689 | static int nested_check_vm_execution_controls(struct kvm_vcpu *vcpu, |
| 2690 | struct vmcs12 *vmcs12) |
| 2691 | { |
| 2692 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 2693 | |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 2694 | if (CC(!vmx_control_verify(vmcs12->pin_based_vm_exec_control, |
| 2695 | vmx->nested.msrs.pinbased_ctls_low, |
| 2696 | vmx->nested.msrs.pinbased_ctls_high)) || |
| 2697 | CC(!vmx_control_verify(vmcs12->cpu_based_vm_exec_control, |
| 2698 | vmx->nested.msrs.procbased_ctls_low, |
| 2699 | vmx->nested.msrs.procbased_ctls_high))) |
Krish Sadhukhan | 461b4ba | 2018-12-12 13:30:07 -0500 | [diff] [blame] | 2700 | return -EINVAL; |
| 2701 | |
| 2702 | if (nested_cpu_has(vmcs12, CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) && |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 2703 | CC(!vmx_control_verify(vmcs12->secondary_vm_exec_control, |
| 2704 | vmx->nested.msrs.secondary_ctls_low, |
| 2705 | vmx->nested.msrs.secondary_ctls_high))) |
Krish Sadhukhan | 461b4ba | 2018-12-12 13:30:07 -0500 | [diff] [blame] | 2706 | return -EINVAL; |
| 2707 | |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 2708 | 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] | 2709 | nested_vmx_check_io_bitmap_controls(vcpu, vmcs12) || |
| 2710 | nested_vmx_check_msr_bitmap_controls(vcpu, vmcs12) || |
| 2711 | nested_vmx_check_tpr_shadow_controls(vcpu, vmcs12) || |
| 2712 | nested_vmx_check_apic_access_controls(vcpu, vmcs12) || |
| 2713 | nested_vmx_check_apicv_controls(vcpu, vmcs12) || |
| 2714 | nested_vmx_check_nmi_controls(vmcs12) || |
| 2715 | nested_vmx_check_pml_controls(vcpu, vmcs12) || |
| 2716 | nested_vmx_check_unrestricted_guest_controls(vcpu, vmcs12) || |
| 2717 | nested_vmx_check_mode_based_ept_exec_controls(vcpu, vmcs12) || |
| 2718 | nested_vmx_check_shadow_vmcs_controls(vcpu, vmcs12) || |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 2719 | CC(nested_cpu_has_vpid(vmcs12) && !vmcs12->virtual_processor_id)) |
Krish Sadhukhan | 461b4ba | 2018-12-12 13:30:07 -0500 | [diff] [blame] | 2720 | return -EINVAL; |
| 2721 | |
Sean Christopherson | bc44121 | 2019-02-12 16:42:23 -0800 | [diff] [blame] | 2722 | if (!nested_cpu_has_preemption_timer(vmcs12) && |
| 2723 | nested_cpu_has_save_preemption_timer(vmcs12)) |
| 2724 | return -EINVAL; |
| 2725 | |
Krish Sadhukhan | 461b4ba | 2018-12-12 13:30:07 -0500 | [diff] [blame] | 2726 | if (nested_cpu_has_ept(vmcs12) && |
Sean Christopherson | ac6389a | 2020-03-02 18:02:38 -0800 | [diff] [blame] | 2727 | CC(!nested_vmx_check_eptp(vcpu, vmcs12->ept_pointer))) |
Krish Sadhukhan | 461b4ba | 2018-12-12 13:30:07 -0500 | [diff] [blame] | 2728 | return -EINVAL; |
| 2729 | |
| 2730 | if (nested_cpu_has_vmfunc(vmcs12)) { |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 2731 | if (CC(vmcs12->vm_function_control & |
| 2732 | ~vmx->nested.msrs.vmfunc_controls)) |
Krish Sadhukhan | 461b4ba | 2018-12-12 13:30:07 -0500 | [diff] [blame] | 2733 | return -EINVAL; |
| 2734 | |
| 2735 | if (nested_cpu_has_eptp_switching(vmcs12)) { |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 2736 | if (CC(!nested_cpu_has_ept(vmcs12)) || |
| 2737 | CC(!page_address_valid(vcpu, vmcs12->eptp_list_address))) |
Krish Sadhukhan | 461b4ba | 2018-12-12 13:30:07 -0500 | [diff] [blame] | 2738 | return -EINVAL; |
| 2739 | } |
| 2740 | } |
| 2741 | |
| 2742 | return 0; |
| 2743 | } |
| 2744 | |
Krish Sadhukhan | 61446ba | 2018-12-12 13:30:09 -0500 | [diff] [blame] | 2745 | /* |
| 2746 | * Checks related to VM-Exit Control Fields |
| 2747 | */ |
| 2748 | static int nested_check_vm_exit_controls(struct kvm_vcpu *vcpu, |
| 2749 | struct vmcs12 *vmcs12) |
| 2750 | { |
| 2751 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 2752 | |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 2753 | if (CC(!vmx_control_verify(vmcs12->vm_exit_controls, |
| 2754 | vmx->nested.msrs.exit_ctls_low, |
| 2755 | vmx->nested.msrs.exit_ctls_high)) || |
| 2756 | CC(nested_vmx_check_exit_msr_switch_controls(vcpu, vmcs12))) |
Krish Sadhukhan | 61446ba | 2018-12-12 13:30:09 -0500 | [diff] [blame] | 2757 | return -EINVAL; |
| 2758 | |
| 2759 | return 0; |
| 2760 | } |
| 2761 | |
Krish Sadhukhan | 5fbf963 | 2018-12-12 13:30:10 -0500 | [diff] [blame] | 2762 | /* |
| 2763 | * Checks related to VM-Entry Control Fields |
| 2764 | */ |
| 2765 | static int nested_check_vm_entry_controls(struct kvm_vcpu *vcpu, |
| 2766 | struct vmcs12 *vmcs12) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2767 | { |
| 2768 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2769 | |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 2770 | if (CC(!vmx_control_verify(vmcs12->vm_entry_controls, |
| 2771 | vmx->nested.msrs.entry_ctls_low, |
| 2772 | vmx->nested.msrs.entry_ctls_high))) |
Krish Sadhukhan | 5fbf963 | 2018-12-12 13:30:10 -0500 | [diff] [blame] | 2773 | return -EINVAL; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2774 | |
| 2775 | /* |
| 2776 | * From the Intel SDM, volume 3: |
| 2777 | * Fields relevant to VM-entry event injection must be set properly. |
| 2778 | * These fields are the VM-entry interruption-information field, the |
| 2779 | * VM-entry exception error code, and the VM-entry instruction length. |
| 2780 | */ |
| 2781 | if (vmcs12->vm_entry_intr_info_field & INTR_INFO_VALID_MASK) { |
| 2782 | u32 intr_info = vmcs12->vm_entry_intr_info_field; |
| 2783 | u8 vector = intr_info & INTR_INFO_VECTOR_MASK; |
| 2784 | u32 intr_type = intr_info & INTR_INFO_INTR_TYPE_MASK; |
| 2785 | bool has_error_code = intr_info & INTR_INFO_DELIVER_CODE_MASK; |
| 2786 | bool should_have_error_code; |
| 2787 | bool urg = nested_cpu_has2(vmcs12, |
| 2788 | SECONDARY_EXEC_UNRESTRICTED_GUEST); |
| 2789 | bool prot_mode = !urg || vmcs12->guest_cr0 & X86_CR0_PE; |
| 2790 | |
| 2791 | /* VM-entry interruption-info field: interruption type */ |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 2792 | if (CC(intr_type == INTR_TYPE_RESERVED) || |
| 2793 | CC(intr_type == INTR_TYPE_OTHER_EVENT && |
| 2794 | !nested_cpu_supports_monitor_trap_flag(vcpu))) |
Krish Sadhukhan | 5fbf963 | 2018-12-12 13:30:10 -0500 | [diff] [blame] | 2795 | return -EINVAL; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2796 | |
| 2797 | /* VM-entry interruption-info field: vector */ |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 2798 | if (CC(intr_type == INTR_TYPE_NMI_INTR && vector != NMI_VECTOR) || |
| 2799 | CC(intr_type == INTR_TYPE_HARD_EXCEPTION && vector > 31) || |
| 2800 | CC(intr_type == INTR_TYPE_OTHER_EVENT && vector != 0)) |
Krish Sadhukhan | 5fbf963 | 2018-12-12 13:30:10 -0500 | [diff] [blame] | 2801 | return -EINVAL; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2802 | |
| 2803 | /* VM-entry interruption-info field: deliver error code */ |
| 2804 | should_have_error_code = |
| 2805 | intr_type == INTR_TYPE_HARD_EXCEPTION && prot_mode && |
| 2806 | x86_exception_has_error_code(vector); |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 2807 | if (CC(has_error_code != should_have_error_code)) |
Krish Sadhukhan | 5fbf963 | 2018-12-12 13:30:10 -0500 | [diff] [blame] | 2808 | return -EINVAL; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2809 | |
| 2810 | /* VM-entry exception error code */ |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 2811 | if (CC(has_error_code && |
Sean Christopherson | 567926c | 2019-10-01 09:21:23 -0700 | [diff] [blame] | 2812 | vmcs12->vm_entry_exception_error_code & GENMASK(31, 16))) |
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: reserved bits */ |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 2816 | if (CC(intr_info & INTR_INFO_RESVD_BITS_MASK)) |
Krish Sadhukhan | 5fbf963 | 2018-12-12 13:30:10 -0500 | [diff] [blame] | 2817 | return -EINVAL; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2818 | |
| 2819 | /* VM-entry instruction length */ |
| 2820 | switch (intr_type) { |
| 2821 | case INTR_TYPE_SOFT_EXCEPTION: |
| 2822 | case INTR_TYPE_SOFT_INTR: |
| 2823 | case INTR_TYPE_PRIV_SW_EXCEPTION: |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 2824 | if (CC(vmcs12->vm_entry_instruction_len > 15) || |
| 2825 | CC(vmcs12->vm_entry_instruction_len == 0 && |
| 2826 | CC(!nested_cpu_has_zero_length_injection(vcpu)))) |
Krish Sadhukhan | 5fbf963 | 2018-12-12 13:30:10 -0500 | [diff] [blame] | 2827 | return -EINVAL; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2828 | } |
| 2829 | } |
| 2830 | |
Krish Sadhukhan | 5fbf963 | 2018-12-12 13:30:10 -0500 | [diff] [blame] | 2831 | if (nested_vmx_check_entry_msr_switch_controls(vcpu, vmcs12)) |
| 2832 | return -EINVAL; |
| 2833 | |
| 2834 | return 0; |
| 2835 | } |
| 2836 | |
Sean Christopherson | 5478ba3 | 2019-04-11 12:18:06 -0700 | [diff] [blame] | 2837 | static int nested_vmx_check_controls(struct kvm_vcpu *vcpu, |
| 2838 | struct vmcs12 *vmcs12) |
| 2839 | { |
| 2840 | if (nested_check_vm_execution_controls(vcpu, vmcs12) || |
| 2841 | nested_check_vm_exit_controls(vcpu, vmcs12) || |
| 2842 | nested_check_vm_entry_controls(vcpu, vmcs12)) |
Paolo Bonzini | 98d9e85 | 2019-04-12 10:19:57 +0200 | [diff] [blame] | 2843 | return -EINVAL; |
Sean Christopherson | 5478ba3 | 2019-04-11 12:18:06 -0700 | [diff] [blame] | 2844 | |
Vitaly Kuznetsov | a835023 | 2020-02-05 13:30:34 +0100 | [diff] [blame] | 2845 | if (to_vmx(vcpu)->nested.enlightened_vmcs_enabled) |
| 2846 | return nested_evmcs_check_controls(vmcs12); |
| 2847 | |
Sean Christopherson | 5478ba3 | 2019-04-11 12:18:06 -0700 | [diff] [blame] | 2848 | return 0; |
| 2849 | } |
| 2850 | |
Maxim Levitsky | af957ee | 2021-11-15 15:18:36 +0200 | [diff] [blame] | 2851 | static int nested_vmx_check_address_space_size(struct kvm_vcpu *vcpu, |
| 2852 | struct vmcs12 *vmcs12) |
| 2853 | { |
| 2854 | #ifdef CONFIG_X86_64 |
| 2855 | if (CC(!!(vmcs12->vm_exit_controls & VM_EXIT_HOST_ADDR_SPACE_SIZE) != |
| 2856 | !!(vcpu->arch.efer & EFER_LMA))) |
| 2857 | return -EINVAL; |
| 2858 | #endif |
| 2859 | return 0; |
| 2860 | } |
| 2861 | |
Paolo Bonzini | 98d9e85 | 2019-04-12 10:19:57 +0200 | [diff] [blame] | 2862 | static int nested_vmx_check_host_state(struct kvm_vcpu *vcpu, |
| 2863 | struct vmcs12 *vmcs12) |
Krish Sadhukhan | 5fbf963 | 2018-12-12 13:30:10 -0500 | [diff] [blame] | 2864 | { |
| 2865 | bool ia32e; |
| 2866 | |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 2867 | if (CC(!nested_host_cr0_valid(vcpu, vmcs12->host_cr0)) || |
| 2868 | CC(!nested_host_cr4_valid(vcpu, vmcs12->host_cr4)) || |
Sean Christopherson | 636e8b7 | 2021-02-03 16:01:10 -0800 | [diff] [blame] | 2869 | CC(kvm_vcpu_is_illegal_gpa(vcpu, vmcs12->host_cr3))) |
Krish Sadhukhan | 254b2f3 | 2018-12-12 13:30:11 -0500 | [diff] [blame] | 2870 | return -EINVAL; |
Krish Sadhukhan | 711eff3 | 2019-02-07 14:05:30 -0500 | [diff] [blame] | 2871 | |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 2872 | if (CC(is_noncanonical_address(vmcs12->host_ia32_sysenter_esp, vcpu)) || |
| 2873 | CC(is_noncanonical_address(vmcs12->host_ia32_sysenter_eip, vcpu))) |
Krish Sadhukhan | 711eff3 | 2019-02-07 14:05:30 -0500 | [diff] [blame] | 2874 | return -EINVAL; |
| 2875 | |
Krish Sadhukhan | f6b0db1f | 2019-04-08 17:35:11 -0400 | [diff] [blame] | 2876 | if ((vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PAT) && |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 2877 | CC(!kvm_pat_valid(vmcs12->host_ia32_pat))) |
Krish Sadhukhan | f6b0db1f | 2019-04-08 17:35:11 -0400 | [diff] [blame] | 2878 | return -EINVAL; |
| 2879 | |
Oliver Upton | c547cb6 | 2019-11-13 16:17:17 -0800 | [diff] [blame] | 2880 | if ((vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL) && |
| 2881 | CC(!kvm_valid_perf_global_ctrl(vcpu_to_pmu(vcpu), |
| 2882 | vmcs12->host_ia32_perf_global_ctrl))) |
| 2883 | return -EINVAL; |
| 2884 | |
Paolo Bonzini | fd3edd4 | 2019-09-25 18:33:53 +0200 | [diff] [blame] | 2885 | #ifdef CONFIG_X86_64 |
Maxim Levitsky | af957ee | 2021-11-15 15:18:36 +0200 | [diff] [blame] | 2886 | ia32e = !!(vmcs12->vm_exit_controls & VM_EXIT_HOST_ADDR_SPACE_SIZE); |
Paolo Bonzini | fd3edd4 | 2019-09-25 18:33:53 +0200 | [diff] [blame] | 2887 | #else |
| 2888 | ia32e = false; |
| 2889 | #endif |
| 2890 | |
| 2891 | if (ia32e) { |
Maxim Levitsky | af957ee | 2021-11-15 15:18:36 +0200 | [diff] [blame] | 2892 | if (CC(!(vmcs12->host_cr4 & X86_CR4_PAE))) |
Paolo Bonzini | fd3edd4 | 2019-09-25 18:33:53 +0200 | [diff] [blame] | 2893 | return -EINVAL; |
| 2894 | } else { |
Maxim Levitsky | af957ee | 2021-11-15 15:18:36 +0200 | [diff] [blame] | 2895 | if (CC(vmcs12->vm_entry_controls & VM_ENTRY_IA32E_MODE) || |
Paolo Bonzini | fd3edd4 | 2019-09-25 18:33:53 +0200 | [diff] [blame] | 2896 | CC(vmcs12->host_cr4 & X86_CR4_PCIDE) || |
| 2897 | CC((vmcs12->host_rip) >> 32)) |
| 2898 | return -EINVAL; |
| 2899 | } |
Krish Sadhukhan | 1ef23e1 | 2019-07-03 19:54:35 -0400 | [diff] [blame] | 2900 | |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 2901 | if (CC(vmcs12->host_cs_selector & (SEGMENT_RPL_MASK | SEGMENT_TI_MASK)) || |
| 2902 | CC(vmcs12->host_ss_selector & (SEGMENT_RPL_MASK | SEGMENT_TI_MASK)) || |
| 2903 | CC(vmcs12->host_ds_selector & (SEGMENT_RPL_MASK | SEGMENT_TI_MASK)) || |
| 2904 | CC(vmcs12->host_es_selector & (SEGMENT_RPL_MASK | SEGMENT_TI_MASK)) || |
| 2905 | CC(vmcs12->host_fs_selector & (SEGMENT_RPL_MASK | SEGMENT_TI_MASK)) || |
| 2906 | CC(vmcs12->host_gs_selector & (SEGMENT_RPL_MASK | SEGMENT_TI_MASK)) || |
| 2907 | CC(vmcs12->host_tr_selector & (SEGMENT_RPL_MASK | SEGMENT_TI_MASK)) || |
| 2908 | CC(vmcs12->host_cs_selector == 0) || |
| 2909 | CC(vmcs12->host_tr_selector == 0) || |
| 2910 | CC(vmcs12->host_ss_selector == 0 && !ia32e)) |
Krish Sadhukhan | 1ef23e1 | 2019-07-03 19:54:35 -0400 | [diff] [blame] | 2911 | return -EINVAL; |
| 2912 | |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 2913 | if (CC(is_noncanonical_address(vmcs12->host_fs_base, vcpu)) || |
| 2914 | CC(is_noncanonical_address(vmcs12->host_gs_base, vcpu)) || |
| 2915 | CC(is_noncanonical_address(vmcs12->host_gdtr_base, vcpu)) || |
| 2916 | CC(is_noncanonical_address(vmcs12->host_idtr_base, vcpu)) || |
Paolo Bonzini | fd3edd4 | 2019-09-25 18:33:53 +0200 | [diff] [blame] | 2917 | CC(is_noncanonical_address(vmcs12->host_tr_base, vcpu)) || |
| 2918 | CC(is_noncanonical_address(vmcs12->host_rip, vcpu))) |
Krish Sadhukhan | 5845038 | 2019-08-09 12:26:19 -0700 | [diff] [blame] | 2919 | return -EINVAL; |
Krish Sadhukhan | 1ef23e1 | 2019-07-03 19:54:35 -0400 | [diff] [blame] | 2920 | |
Krish Sadhukhan | 5fbf963 | 2018-12-12 13:30:10 -0500 | [diff] [blame] | 2921 | /* |
| 2922 | * If the load IA32_EFER VM-exit control is 1, bits reserved in the |
| 2923 | * IA32_EFER MSR must be 0 in the field for that register. In addition, |
| 2924 | * the values of the LMA and LME bits in the field must each be that of |
| 2925 | * the host address-space size VM-exit control. |
| 2926 | */ |
| 2927 | if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_EFER) { |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 2928 | if (CC(!kvm_valid_efer(vcpu, vmcs12->host_ia32_efer)) || |
| 2929 | CC(ia32e != !!(vmcs12->host_ia32_efer & EFER_LMA)) || |
| 2930 | CC(ia32e != !!(vmcs12->host_ia32_efer & EFER_LME))) |
Krish Sadhukhan | 254b2f3 | 2018-12-12 13:30:11 -0500 | [diff] [blame] | 2931 | return -EINVAL; |
Krish Sadhukhan | 5fbf963 | 2018-12-12 13:30:10 -0500 | [diff] [blame] | 2932 | } |
| 2933 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2934 | return 0; |
| 2935 | } |
| 2936 | |
| 2937 | static int nested_vmx_check_vmcs_link_ptr(struct kvm_vcpu *vcpu, |
| 2938 | struct vmcs12 *vmcs12) |
| 2939 | { |
David Woodhouse | 7d0172b | 2021-11-15 16:50:25 +0000 | [diff] [blame] | 2940 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 2941 | struct gfn_to_hva_cache *ghc = &vmx->nested.shadow_vmcs12_cache; |
| 2942 | struct vmcs_hdr hdr; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2943 | |
Yu Zhang | 64c7850 | 2021-09-30 01:51:53 +0800 | [diff] [blame] | 2944 | if (vmcs12->vmcs_link_pointer == INVALID_GPA) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2945 | return 0; |
| 2946 | |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 2947 | if (CC(!page_address_valid(vcpu, vmcs12->vmcs_link_pointer))) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2948 | return -EINVAL; |
| 2949 | |
David Woodhouse | 7d0172b | 2021-11-15 16:50:25 +0000 | [diff] [blame] | 2950 | if (ghc->gpa != vmcs12->vmcs_link_pointer && |
| 2951 | CC(kvm_gfn_to_hva_cache_init(vcpu->kvm, ghc, |
| 2952 | vmcs12->vmcs_link_pointer, VMCS12_SIZE))) |
| 2953 | return -EINVAL; |
| 2954 | |
| 2955 | if (CC(kvm_read_guest_offset_cached(vcpu->kvm, ghc, &hdr, |
| 2956 | offsetof(struct vmcs12, hdr), |
| 2957 | sizeof(hdr)))) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2958 | return -EINVAL; |
| 2959 | |
David Woodhouse | 7d0172b | 2021-11-15 16:50:25 +0000 | [diff] [blame] | 2960 | if (CC(hdr.revision_id != VMCS12_REVISION) || |
| 2961 | CC(hdr.shadow_vmcs != nested_cpu_has_shadow_vmcs(vmcs12))) |
| 2962 | return -EINVAL; |
KarimAllah Ahmed | 8892530 | 2019-01-31 21:24:41 +0100 | [diff] [blame] | 2963 | |
David Woodhouse | 7d0172b | 2021-11-15 16:50:25 +0000 | [diff] [blame] | 2964 | return 0; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2965 | } |
| 2966 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2967 | /* |
| 2968 | * Checks related to Guest Non-register State |
| 2969 | */ |
| 2970 | static int nested_check_guest_non_reg_state(struct vmcs12 *vmcs12) |
| 2971 | { |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 2972 | if (CC(vmcs12->guest_activity_state != GUEST_ACTIVITY_ACTIVE && |
Yadong Qi | bf0cd88 | 2020-11-06 14:51:22 +0800 | [diff] [blame] | 2973 | vmcs12->guest_activity_state != GUEST_ACTIVITY_HLT && |
| 2974 | vmcs12->guest_activity_state != GUEST_ACTIVITY_WAIT_SIPI)) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2975 | return -EINVAL; |
| 2976 | |
| 2977 | return 0; |
| 2978 | } |
| 2979 | |
Sean Christopherson | 5478ba3 | 2019-04-11 12:18:06 -0700 | [diff] [blame] | 2980 | static int nested_vmx_check_guest_state(struct kvm_vcpu *vcpu, |
| 2981 | struct vmcs12 *vmcs12, |
Sean Christopherson | 68cda40 | 2020-05-11 15:05:29 -0700 | [diff] [blame] | 2982 | enum vm_entry_failure_code *entry_failure_code) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2983 | { |
| 2984 | bool ia32e; |
| 2985 | |
Sean Christopherson | 68cda40 | 2020-05-11 15:05:29 -0700 | [diff] [blame] | 2986 | *entry_failure_code = ENTRY_FAIL_DEFAULT; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2987 | |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 2988 | if (CC(!nested_guest_cr0_valid(vcpu, vmcs12->guest_cr0)) || |
| 2989 | CC(!nested_guest_cr4_valid(vcpu, vmcs12->guest_cr4))) |
Sean Christopherson | c80add0 | 2019-04-11 12:18:09 -0700 | [diff] [blame] | 2990 | return -EINVAL; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2991 | |
Krish Sadhukhan | b91991b | 2020-01-15 19:54:32 -0500 | [diff] [blame] | 2992 | if ((vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS) && |
| 2993 | CC(!kvm_dr7_valid(vmcs12->guest_dr7))) |
| 2994 | return -EINVAL; |
| 2995 | |
Krish Sadhukhan | de2bc2b | 2019-04-08 17:35:12 -0400 | [diff] [blame] | 2996 | if ((vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_PAT) && |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 2997 | CC(!kvm_pat_valid(vmcs12->guest_ia32_pat))) |
Sean Christopherson | c80add0 | 2019-04-11 12:18:09 -0700 | [diff] [blame] | 2998 | return -EINVAL; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2999 | |
| 3000 | if (nested_vmx_check_vmcs_link_ptr(vcpu, vmcs12)) { |
Sean Christopherson | 68cda40 | 2020-05-11 15:05:29 -0700 | [diff] [blame] | 3001 | *entry_failure_code = ENTRY_FAIL_VMCS_LINK_PTR; |
Sean Christopherson | c80add0 | 2019-04-11 12:18:09 -0700 | [diff] [blame] | 3002 | return -EINVAL; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3003 | } |
| 3004 | |
Oliver Upton | bfc6ad6 | 2019-11-13 16:17:16 -0800 | [diff] [blame] | 3005 | if ((vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL) && |
| 3006 | CC(!kvm_valid_perf_global_ctrl(vcpu_to_pmu(vcpu), |
| 3007 | vmcs12->guest_ia32_perf_global_ctrl))) |
| 3008 | return -EINVAL; |
| 3009 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3010 | /* |
| 3011 | * If the load IA32_EFER VM-entry control is 1, the following checks |
| 3012 | * are performed on the field for the IA32_EFER MSR: |
| 3013 | * - Bits reserved in the IA32_EFER MSR must be 0. |
| 3014 | * - Bit 10 (corresponding to IA32_EFER.LMA) must equal the value of |
| 3015 | * the IA-32e mode guest VM-exit control. It must also be identical |
| 3016 | * to bit 8 (LME) if bit 31 in the CR0 field (corresponding to |
| 3017 | * CR0.PG) is 1. |
| 3018 | */ |
| 3019 | if (to_vmx(vcpu)->nested.nested_run_pending && |
| 3020 | (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_EFER)) { |
| 3021 | ia32e = (vmcs12->vm_entry_controls & VM_ENTRY_IA32E_MODE) != 0; |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 3022 | if (CC(!kvm_valid_efer(vcpu, vmcs12->guest_ia32_efer)) || |
| 3023 | CC(ia32e != !!(vmcs12->guest_ia32_efer & EFER_LMA)) || |
| 3024 | CC(((vmcs12->guest_cr0 & X86_CR0_PG) && |
| 3025 | ia32e != !!(vmcs12->guest_ia32_efer & EFER_LME)))) |
Sean Christopherson | c80add0 | 2019-04-11 12:18:09 -0700 | [diff] [blame] | 3026 | return -EINVAL; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3027 | } |
| 3028 | |
| 3029 | if ((vmcs12->vm_entry_controls & VM_ENTRY_LOAD_BNDCFGS) && |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 3030 | (CC(is_noncanonical_address(vmcs12->guest_bndcfgs & PAGE_MASK, vcpu)) || |
| 3031 | CC((vmcs12->guest_bndcfgs & MSR_IA32_BNDCFGS_RSVD)))) |
Sean Christopherson | c80add0 | 2019-04-11 12:18:09 -0700 | [diff] [blame] | 3032 | return -EINVAL; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3033 | |
Sean Christopherson | 9c3e922 | 2019-04-11 12:18:05 -0700 | [diff] [blame] | 3034 | if (nested_check_guest_non_reg_state(vmcs12)) |
Sean Christopherson | c80add0 | 2019-04-11 12:18:09 -0700 | [diff] [blame] | 3035 | return -EINVAL; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3036 | |
| 3037 | return 0; |
| 3038 | } |
| 3039 | |
Sean Christopherson | 453eafb | 2018-12-20 12:25:17 -0800 | [diff] [blame] | 3040 | static int nested_vmx_check_vmentry_hw(struct kvm_vcpu *vcpu) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3041 | { |
| 3042 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Lai Jiangshan | 15ad976 | 2021-11-18 19:08:03 +0800 | [diff] [blame] | 3043 | unsigned long cr4; |
Sean Christopherson | f1727b4 | 2019-01-25 07:40:58 -0800 | [diff] [blame] | 3044 | bool vm_fail; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3045 | |
| 3046 | if (!nested_early_check) |
| 3047 | return 0; |
| 3048 | |
| 3049 | if (vmx->msr_autoload.host.nr) |
| 3050 | vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, 0); |
| 3051 | if (vmx->msr_autoload.guest.nr) |
| 3052 | vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, 0); |
| 3053 | |
| 3054 | preempt_disable(); |
| 3055 | |
| 3056 | vmx_prepare_switch_to_guest(vcpu); |
| 3057 | |
| 3058 | /* |
| 3059 | * Induce a consistency check VMExit by clearing bit 1 in GUEST_RFLAGS, |
| 3060 | * which is reserved to '1' by hardware. GUEST_RFLAGS is guaranteed to |
Miaohe Lin | 49f933d | 2020-02-27 11:20:54 +0800 | [diff] [blame] | 3061 | * be written (by prepare_vmcs02()) before the "real" VMEnter, i.e. |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3062 | * there is no need to preserve other bits or save/restore the field. |
| 3063 | */ |
| 3064 | vmcs_writel(GUEST_RFLAGS, 0); |
| 3065 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3066 | cr4 = cr4_read_shadow(); |
| 3067 | if (unlikely(cr4 != vmx->loaded_vmcs->host_state.cr4)) { |
| 3068 | vmcs_writel(HOST_CR4, cr4); |
| 3069 | vmx->loaded_vmcs->host_state.cr4 = cr4; |
| 3070 | } |
| 3071 | |
Uros Bizjak | 150f17b | 2020-12-30 16:26:57 -0800 | [diff] [blame] | 3072 | vm_fail = __vmx_vcpu_run(vmx, (unsigned long *)&vcpu->arch.regs, |
| 3073 | vmx->loaded_vmcs->launched); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3074 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3075 | if (vmx->msr_autoload.host.nr) |
| 3076 | vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, vmx->msr_autoload.host.nr); |
| 3077 | if (vmx->msr_autoload.guest.nr) |
| 3078 | vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, vmx->msr_autoload.guest.nr); |
| 3079 | |
Sean Christopherson | f1727b4 | 2019-01-25 07:40:58 -0800 | [diff] [blame] | 3080 | if (vm_fail) { |
Sean Christopherson | 380e005 | 2019-07-11 08:58:30 -0700 | [diff] [blame] | 3081 | u32 error = vmcs_read32(VM_INSTRUCTION_ERROR); |
| 3082 | |
Wanpeng Li | 541e886 | 2019-05-17 16:49:50 +0800 | [diff] [blame] | 3083 | preempt_enable(); |
Sean Christopherson | 380e005 | 2019-07-11 08:58:30 -0700 | [diff] [blame] | 3084 | |
| 3085 | trace_kvm_nested_vmenter_failed( |
| 3086 | "early hardware check VM-instruction error: ", error); |
| 3087 | WARN_ON_ONCE(error != VMXERR_ENTRY_INVALID_CONTROL_FIELD); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3088 | return 1; |
| 3089 | } |
| 3090 | |
| 3091 | /* |
| 3092 | * VMExit clears RFLAGS.IF and DR7, even on a consistency check. |
| 3093 | */ |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3094 | if (hw_breakpoint_active()) |
| 3095 | set_debugreg(__this_cpu_read(cpu_dr7), 7); |
Peter Zijlstra | 84b6a34 | 2020-05-29 23:27:36 +0200 | [diff] [blame] | 3096 | local_irq_enable(); |
Wanpeng Li | 541e886 | 2019-05-17 16:49:50 +0800 | [diff] [blame] | 3097 | preempt_enable(); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3098 | |
| 3099 | /* |
| 3100 | * A non-failing VMEntry means we somehow entered guest mode with |
| 3101 | * an illegal RIP, and that's just the tip of the iceberg. There |
| 3102 | * is no telling what memory has been modified or what state has |
| 3103 | * been exposed to unknown code. Hitting this all but guarantees |
| 3104 | * a (very critical) hardware issue. |
| 3105 | */ |
| 3106 | WARN_ON(!(vmcs_read32(VM_EXIT_REASON) & |
| 3107 | VMX_EXIT_REASONS_FAILED_VMENTRY)); |
| 3108 | |
| 3109 | return 0; |
| 3110 | } |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3111 | |
Paolo Bonzini | 9a78e15 | 2021-01-08 11:43:08 -0500 | [diff] [blame] | 3112 | static bool nested_get_evmcs_page(struct kvm_vcpu *vcpu) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3113 | { |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3114 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3115 | |
Vitaly Kuznetsov | e942dbf | 2020-03-09 16:52:12 +0100 | [diff] [blame] | 3116 | /* |
| 3117 | * hv_evmcs may end up being not mapped after migration (when |
| 3118 | * L2 was running), map it here to make sure vmcs12 changes are |
| 3119 | * properly reflected. |
| 3120 | */ |
Vitaly Kuznetsov | 1e9dfbd | 2021-05-26 15:20:16 +0200 | [diff] [blame] | 3121 | if (vmx->nested.enlightened_vmcs_enabled && |
Vitaly Kuznetsov | 2784996 | 2021-05-26 15:20:20 +0200 | [diff] [blame] | 3122 | vmx->nested.hv_evmcs_vmptr == EVMPTR_MAP_PENDING) { |
Vitaly Kuznetsov | b6a0653 | 2020-03-09 16:52:13 +0100 | [diff] [blame] | 3123 | enum nested_evmptrld_status evmptrld_status = |
| 3124 | nested_vmx_handle_enlightened_vmptrld(vcpu, false); |
| 3125 | |
| 3126 | if (evmptrld_status == EVMPTRLD_VMFAIL || |
Vitaly Kuznetsov | f5c7e84 | 2021-05-03 17:08:51 +0200 | [diff] [blame] | 3127 | evmptrld_status == EVMPTRLD_ERROR) |
Vitaly Kuznetsov | b6a0653 | 2020-03-09 16:52:13 +0100 | [diff] [blame] | 3128 | return false; |
Vitaly Kuznetsov | 8629b62 | 2021-05-26 15:20:25 +0200 | [diff] [blame] | 3129 | |
| 3130 | /* |
| 3131 | * Post migration VMCS12 always provides the most actual |
| 3132 | * information, copy it to eVMCS upon entry. |
| 3133 | */ |
| 3134 | vmx->nested.need_vmcs12_to_shadow_sync = true; |
Vitaly Kuznetsov | b6a0653 | 2020-03-09 16:52:13 +0100 | [diff] [blame] | 3135 | } |
Vitaly Kuznetsov | e942dbf | 2020-03-09 16:52:12 +0100 | [diff] [blame] | 3136 | |
Paolo Bonzini | 9a78e15 | 2021-01-08 11:43:08 -0500 | [diff] [blame] | 3137 | return true; |
| 3138 | } |
| 3139 | |
| 3140 | static bool nested_get_vmcs12_pages(struct kvm_vcpu *vcpu) |
| 3141 | { |
| 3142 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
| 3143 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 3144 | struct kvm_host_map *map; |
| 3145 | struct page *page; |
| 3146 | u64 hpa; |
| 3147 | |
Maxim Levitsky | 158a48e | 2021-06-07 12:02:03 +0300 | [diff] [blame] | 3148 | if (!vcpu->arch.pdptrs_from_userspace && |
| 3149 | !nested_cpu_has_ept(vmcs12) && is_pae_paging(vcpu)) { |
Maxim Levitsky | 0f85722 | 2021-06-07 12:02:00 +0300 | [diff] [blame] | 3150 | /* |
| 3151 | * Reload the guest's PDPTRs since after a migration |
| 3152 | * the guest CR3 might be restored prior to setting the nested |
| 3153 | * state which can lead to a load of wrong PDPTRs. |
| 3154 | */ |
Lai Jiangshan | 2df4a5e | 2021-11-24 20:20:52 +0800 | [diff] [blame] | 3155 | if (CC(!load_pdptrs(vcpu, vcpu->arch.cr3))) |
Maxim Levitsky | 0f85722 | 2021-06-07 12:02:00 +0300 | [diff] [blame] | 3156 | return false; |
| 3157 | } |
| 3158 | |
| 3159 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3160 | if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) { |
| 3161 | /* |
| 3162 | * Translate L1 physical address to host physical |
| 3163 | * address for vmcs02. Keep the page pinned, so this |
| 3164 | * physical address remains valid. We keep a reference |
| 3165 | * to it so we can release it later. |
| 3166 | */ |
| 3167 | if (vmx->nested.apic_access_page) { /* shouldn't happen */ |
Liran Alon | b11494b | 2019-11-21 00:31:47 +0200 | [diff] [blame] | 3168 | kvm_release_page_clean(vmx->nested.apic_access_page); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3169 | vmx->nested.apic_access_page = NULL; |
| 3170 | } |
| 3171 | page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->apic_access_addr); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3172 | if (!is_error_page(page)) { |
| 3173 | vmx->nested.apic_access_page = page; |
| 3174 | hpa = page_to_phys(vmx->nested.apic_access_page); |
| 3175 | vmcs_write64(APIC_ACCESS_ADDR, hpa); |
| 3176 | } else { |
Jim Mattson | 671ddc7 | 2019-10-15 10:44:05 -0700 | [diff] [blame] | 3177 | pr_debug_ratelimited("%s: no backing 'struct page' for APIC-access address in vmcs12\n", |
| 3178 | __func__); |
| 3179 | vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR; |
| 3180 | vcpu->run->internal.suberror = |
| 3181 | KVM_INTERNAL_ERROR_EMULATION; |
| 3182 | vcpu->run->internal.ndata = 0; |
| 3183 | return false; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3184 | } |
| 3185 | } |
| 3186 | |
| 3187 | if (nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW)) { |
KarimAllah Ahmed | 96c66e8 | 2019-01-31 21:24:37 +0100 | [diff] [blame] | 3188 | map = &vmx->nested.virtual_apic_map; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3189 | |
KarimAllah Ahmed | 96c66e8 | 2019-01-31 21:24:37 +0100 | [diff] [blame] | 3190 | if (!kvm_vcpu_map(vcpu, gpa_to_gfn(vmcs12->virtual_apic_page_addr), map)) { |
| 3191 | vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, pfn_to_hpa(map->pfn)); |
Paolo Bonzini | 6909081 | 2019-04-15 15:16:17 +0200 | [diff] [blame] | 3192 | } else if (nested_cpu_has(vmcs12, CPU_BASED_CR8_LOAD_EXITING) && |
| 3193 | nested_cpu_has(vmcs12, CPU_BASED_CR8_STORE_EXITING) && |
| 3194 | !nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) { |
| 3195 | /* |
| 3196 | * The processor will never use the TPR shadow, simply |
| 3197 | * clear the bit from the execution control. Such a |
| 3198 | * configuration is useless, but it happens in tests. |
| 3199 | * For any other configuration, failing the vm entry is |
| 3200 | * _not_ what the processor does but it's basically the |
| 3201 | * only possibility we have. |
| 3202 | */ |
Sean Christopherson | 2183f56 | 2019-05-07 12:17:56 -0700 | [diff] [blame] | 3203 | exec_controls_clearbit(vmx, CPU_BASED_TPR_SHADOW); |
Paolo Bonzini | 6909081 | 2019-04-15 15:16:17 +0200 | [diff] [blame] | 3204 | } else { |
Sean Christopherson | ca2f546 | 2019-05-07 09:06:33 -0700 | [diff] [blame] | 3205 | /* |
| 3206 | * Write an illegal value to VIRTUAL_APIC_PAGE_ADDR to |
| 3207 | * force VM-Entry to fail. |
| 3208 | */ |
Yu Zhang | 64c7850 | 2021-09-30 01:51:53 +0800 | [diff] [blame] | 3209 | vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, INVALID_GPA); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3210 | } |
| 3211 | } |
| 3212 | |
| 3213 | if (nested_cpu_has_posted_intr(vmcs12)) { |
KarimAllah Ahmed | 3278e04 | 2019-01-31 21:24:38 +0100 | [diff] [blame] | 3214 | map = &vmx->nested.pi_desc_map; |
| 3215 | |
| 3216 | if (!kvm_vcpu_map(vcpu, gpa_to_gfn(vmcs12->posted_intr_desc_addr), map)) { |
| 3217 | vmx->nested.pi_desc = |
| 3218 | (struct pi_desc *)(((void *)map->hva) + |
| 3219 | offset_in_page(vmcs12->posted_intr_desc_addr)); |
| 3220 | vmcs_write64(POSTED_INTR_DESC_ADDR, |
| 3221 | 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] | 3222 | } else { |
| 3223 | /* |
| 3224 | * Defer the KVM_INTERNAL_EXIT until KVM tries to |
| 3225 | * access the contents of the VMCS12 posted interrupt |
| 3226 | * descriptor. (Note that KVM may do this when it |
| 3227 | * should not, per the architectural specification.) |
| 3228 | */ |
| 3229 | vmx->nested.pi_desc = NULL; |
| 3230 | pin_controls_clearbit(vmx, PIN_BASED_POSTED_INTR); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3231 | } |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3232 | } |
| 3233 | if (nested_vmx_prepare_msr_bitmap(vcpu, vmcs12)) |
Sean Christopherson | 2183f56 | 2019-05-07 12:17:56 -0700 | [diff] [blame] | 3234 | exec_controls_setbit(vmx, CPU_BASED_USE_MSR_BITMAPS); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3235 | else |
Sean Christopherson | 2183f56 | 2019-05-07 12:17:56 -0700 | [diff] [blame] | 3236 | exec_controls_clearbit(vmx, CPU_BASED_USE_MSR_BITMAPS); |
Paolo Bonzini | 9a78e15 | 2021-01-08 11:43:08 -0500 | [diff] [blame] | 3237 | |
| 3238 | return true; |
| 3239 | } |
| 3240 | |
| 3241 | static bool vmx_get_nested_state_pages(struct kvm_vcpu *vcpu) |
| 3242 | { |
Vitaly Kuznetsov | f5c7e84 | 2021-05-03 17:08:51 +0200 | [diff] [blame] | 3243 | if (!nested_get_evmcs_page(vcpu)) { |
| 3244 | pr_debug_ratelimited("%s: enlightened vmptrld failed\n", |
| 3245 | __func__); |
| 3246 | vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR; |
| 3247 | vcpu->run->internal.suberror = |
| 3248 | KVM_INTERNAL_ERROR_EMULATION; |
| 3249 | vcpu->run->internal.ndata = 0; |
| 3250 | |
Paolo Bonzini | 9a78e15 | 2021-01-08 11:43:08 -0500 | [diff] [blame] | 3251 | return false; |
Vitaly Kuznetsov | f5c7e84 | 2021-05-03 17:08:51 +0200 | [diff] [blame] | 3252 | } |
Paolo Bonzini | 9a78e15 | 2021-01-08 11:43:08 -0500 | [diff] [blame] | 3253 | |
| 3254 | if (is_guest_mode(vcpu) && !nested_get_vmcs12_pages(vcpu)) |
| 3255 | return false; |
| 3256 | |
Jim Mattson | 671ddc7 | 2019-10-15 10:44:05 -0700 | [diff] [blame] | 3257 | return true; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3258 | } |
| 3259 | |
Sean Christopherson | 02f5fb2 | 2020-06-22 14:58:32 -0700 | [diff] [blame] | 3260 | static int nested_vmx_write_pml_buffer(struct kvm_vcpu *vcpu, gpa_t gpa) |
| 3261 | { |
| 3262 | struct vmcs12 *vmcs12; |
| 3263 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 3264 | gpa_t dst; |
| 3265 | |
| 3266 | if (WARN_ON_ONCE(!is_guest_mode(vcpu))) |
| 3267 | return 0; |
| 3268 | |
| 3269 | if (WARN_ON_ONCE(vmx->nested.pml_full)) |
| 3270 | return 1; |
| 3271 | |
| 3272 | /* |
| 3273 | * Check if PML is enabled for the nested guest. Whether eptp bit 6 is |
| 3274 | * set is already checked as part of A/D emulation. |
| 3275 | */ |
| 3276 | vmcs12 = get_vmcs12(vcpu); |
| 3277 | if (!nested_cpu_has_pml(vmcs12)) |
| 3278 | return 0; |
| 3279 | |
| 3280 | if (vmcs12->guest_pml_index >= PML_ENTITY_NUM) { |
| 3281 | vmx->nested.pml_full = true; |
| 3282 | return 1; |
| 3283 | } |
| 3284 | |
| 3285 | gpa &= ~0xFFFull; |
| 3286 | dst = vmcs12->pml_address + sizeof(u64) * vmcs12->guest_pml_index; |
| 3287 | |
| 3288 | if (kvm_write_guest_page(vcpu->kvm, gpa_to_gfn(dst), &gpa, |
| 3289 | offset_in_page(dst), sizeof(gpa))) |
| 3290 | return 0; |
| 3291 | |
| 3292 | vmcs12->guest_pml_index--; |
| 3293 | |
| 3294 | return 0; |
| 3295 | } |
| 3296 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3297 | /* |
| 3298 | * Intel's VMX Instruction Reference specifies a common set of prerequisites |
| 3299 | * for running VMX instructions (except VMXON, whose prerequisites are |
| 3300 | * slightly different). It also specifies what exception to inject otherwise. |
| 3301 | * Note that many of these exceptions have priority over VM exits, so they |
| 3302 | * don't have to be checked again here. |
| 3303 | */ |
| 3304 | static int nested_vmx_check_permission(struct kvm_vcpu *vcpu) |
| 3305 | { |
| 3306 | if (!to_vmx(vcpu)->nested.vmxon) { |
| 3307 | kvm_queue_exception(vcpu, UD_VECTOR); |
| 3308 | return 0; |
| 3309 | } |
| 3310 | |
| 3311 | if (vmx_get_cpl(vcpu)) { |
| 3312 | kvm_inject_gp(vcpu, 0); |
| 3313 | return 0; |
| 3314 | } |
| 3315 | |
| 3316 | return 1; |
| 3317 | } |
| 3318 | |
| 3319 | static u8 vmx_has_apicv_interrupt(struct kvm_vcpu *vcpu) |
| 3320 | { |
| 3321 | u8 rvi = vmx_get_rvi(); |
| 3322 | u8 vppr = kvm_lapic_get_reg(vcpu->arch.apic, APIC_PROCPRI); |
| 3323 | |
| 3324 | return ((rvi & 0xf0) > (vppr & 0xf0)); |
| 3325 | } |
| 3326 | |
| 3327 | static void load_vmcs12_host_state(struct kvm_vcpu *vcpu, |
| 3328 | struct vmcs12 *vmcs12); |
| 3329 | |
| 3330 | /* |
| 3331 | * If from_vmentry is false, this is being called from state restore (either RSM |
| 3332 | * or KVM_SET_NESTED_STATE). Otherwise it's called from vmlaunch/vmresume. |
Jim Mattson | 671ddc7 | 2019-10-15 10:44:05 -0700 | [diff] [blame] | 3333 | * |
| 3334 | * Returns: |
Miaohe Lin | 463bfee | 2020-02-14 10:44:05 +0800 | [diff] [blame] | 3335 | * NVMX_VMENTRY_SUCCESS: Entered VMX non-root mode |
| 3336 | * NVMX_VMENTRY_VMFAIL: Consistency check VMFail |
| 3337 | * NVMX_VMENTRY_VMEXIT: Consistency check VMExit |
| 3338 | * NVMX_VMENTRY_KVM_INTERNAL_ERROR: KVM internal error |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3339 | */ |
Jim Mattson | 671ddc7 | 2019-10-15 10:44:05 -0700 | [diff] [blame] | 3340 | enum nvmx_vmentry_status nested_vmx_enter_non_root_mode(struct kvm_vcpu *vcpu, |
| 3341 | bool from_vmentry) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3342 | { |
| 3343 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 3344 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
Sean Christopherson | 68cda40 | 2020-05-11 15:05:29 -0700 | [diff] [blame] | 3345 | enum vm_entry_failure_code entry_failure_code; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3346 | bool evaluate_pending_interrupts; |
Sean Christopherson | 8e53324 | 2020-11-06 17:03:12 +0800 | [diff] [blame] | 3347 | union vmx_exit_reason exit_reason = { |
| 3348 | .basic = EXIT_REASON_INVALID_STATE, |
| 3349 | .failed_vmentry = 1, |
| 3350 | }; |
| 3351 | u32 failed_index; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3352 | |
Sean Christopherson | 40e5f908 | 2021-11-25 01:49:43 +0000 | [diff] [blame] | 3353 | kvm_service_local_tlb_flush_requests(vcpu); |
Sean Christopherson | eeeb4f6 | 2020-03-20 14:28:20 -0700 | [diff] [blame] | 3354 | |
Sean Christopherson | 2183f56 | 2019-05-07 12:17:56 -0700 | [diff] [blame] | 3355 | evaluate_pending_interrupts = exec_controls_get(vmx) & |
Xiaoyao Li | 4e2a0bc | 2019-12-06 16:45:25 +0800 | [diff] [blame] | 3356 | (CPU_BASED_INTR_WINDOW_EXITING | CPU_BASED_NMI_WINDOW_EXITING); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3357 | if (likely(!evaluate_pending_interrupts) && kvm_vcpu_apicv_active(vcpu)) |
| 3358 | evaluate_pending_interrupts |= vmx_has_apicv_interrupt(vcpu); |
| 3359 | |
| 3360 | if (!(vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS)) |
| 3361 | vmx->nested.vmcs01_debugctl = vmcs_read64(GUEST_IA32_DEBUGCTL); |
| 3362 | if (kvm_mpx_supported() && |
| 3363 | !(vmcs12->vm_entry_controls & VM_ENTRY_LOAD_BNDCFGS)) |
| 3364 | vmx->nested.vmcs01_guest_bndcfgs = vmcs_read64(GUEST_BNDCFGS); |
| 3365 | |
Sean Christopherson | f087a02 | 2019-06-07 11:55:34 -0700 | [diff] [blame] | 3366 | /* |
| 3367 | * Overwrite vmcs01.GUEST_CR3 with L1's CR3 if EPT is disabled *and* |
| 3368 | * nested early checks are disabled. In the event of a "late" VM-Fail, |
| 3369 | * i.e. a VM-Fail detected by hardware but not KVM, KVM must unwind its |
| 3370 | * software model to the pre-VMEntry host state. When EPT is disabled, |
| 3371 | * GUEST_CR3 holds KVM's shadow CR3, not L1's "real" CR3, which causes |
| 3372 | * nested_vmx_restore_host_state() to corrupt vcpu->arch.cr3. Stuffing |
| 3373 | * vmcs01.GUEST_CR3 results in the unwind naturally setting arch.cr3 to |
| 3374 | * the correct value. Smashing vmcs01.GUEST_CR3 is safe because nested |
| 3375 | * VM-Exits, and the unwind, reset KVM's MMU, i.e. vmcs01.GUEST_CR3 is |
| 3376 | * guaranteed to be overwritten with a shadow CR3 prior to re-entering |
| 3377 | * L1. Don't stuff vmcs01.GUEST_CR3 when using nested early checks as |
| 3378 | * KVM modifies vcpu->arch.cr3 if and only if the early hardware checks |
| 3379 | * pass, and early VM-Fails do not reset KVM's MMU, i.e. the VM-Fail |
| 3380 | * path would need to manually save/restore vmcs01.GUEST_CR3. |
| 3381 | */ |
| 3382 | if (!enable_ept && !nested_early_check) |
| 3383 | vmcs_writel(GUEST_CR3, vcpu->arch.cr3); |
| 3384 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3385 | vmx_switch_vmcs(vcpu, &vmx->nested.vmcs02); |
| 3386 | |
Sean Christopherson | 389ab25 | 2021-08-10 10:19:50 -0700 | [diff] [blame] | 3387 | prepare_vmcs02_early(vmx, &vmx->vmcs01, vmcs12); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3388 | |
| 3389 | if (from_vmentry) { |
Sean Christopherson | b89d5ad | 2020-09-23 11:44:47 -0700 | [diff] [blame] | 3390 | if (unlikely(!nested_get_vmcs12_pages(vcpu))) { |
| 3391 | vmx_switch_vmcs(vcpu, &vmx->vmcs01); |
Jim Mattson | 671ddc7 | 2019-10-15 10:44:05 -0700 | [diff] [blame] | 3392 | return NVMX_VMENTRY_KVM_INTERNAL_ERROR; |
Sean Christopherson | b89d5ad | 2020-09-23 11:44:47 -0700 | [diff] [blame] | 3393 | } |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3394 | |
| 3395 | if (nested_vmx_check_vmentry_hw(vcpu)) { |
| 3396 | vmx_switch_vmcs(vcpu, &vmx->vmcs01); |
Jim Mattson | 671ddc7 | 2019-10-15 10:44:05 -0700 | [diff] [blame] | 3397 | return NVMX_VMENTRY_VMFAIL; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3398 | } |
| 3399 | |
Sean Christopherson | 68cda40 | 2020-05-11 15:05:29 -0700 | [diff] [blame] | 3400 | if (nested_vmx_check_guest_state(vcpu, vmcs12, |
| 3401 | &entry_failure_code)) { |
Sean Christopherson | 8e53324 | 2020-11-06 17:03:12 +0800 | [diff] [blame] | 3402 | exit_reason.basic = EXIT_REASON_INVALID_STATE; |
Sean Christopherson | 68cda40 | 2020-05-11 15:05:29 -0700 | [diff] [blame] | 3403 | vmcs12->exit_qualification = entry_failure_code; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3404 | goto vmentry_fail_vmexit; |
Sean Christopherson | 68cda40 | 2020-05-11 15:05:29 -0700 | [diff] [blame] | 3405 | } |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3406 | } |
| 3407 | |
| 3408 | enter_guest_mode(vcpu); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3409 | |
Maxim Levitsky | 0f85722 | 2021-06-07 12:02:00 +0300 | [diff] [blame] | 3410 | if (prepare_vmcs02(vcpu, vmcs12, from_vmentry, &entry_failure_code)) { |
Sean Christopherson | 8e53324 | 2020-11-06 17:03:12 +0800 | [diff] [blame] | 3411 | exit_reason.basic = EXIT_REASON_INVALID_STATE; |
Sean Christopherson | 68cda40 | 2020-05-11 15:05:29 -0700 | [diff] [blame] | 3412 | vmcs12->exit_qualification = entry_failure_code; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3413 | goto vmentry_fail_vmexit_guest_mode; |
Sean Christopherson | 68cda40 | 2020-05-11 15:05:29 -0700 | [diff] [blame] | 3414 | } |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3415 | |
| 3416 | if (from_vmentry) { |
Sean Christopherson | 68cda40 | 2020-05-11 15:05:29 -0700 | [diff] [blame] | 3417 | failed_index = nested_vmx_load_msr(vcpu, |
| 3418 | vmcs12->vm_entry_msr_load_addr, |
| 3419 | vmcs12->vm_entry_msr_load_count); |
| 3420 | if (failed_index) { |
Sean Christopherson | 8e53324 | 2020-11-06 17:03:12 +0800 | [diff] [blame] | 3421 | exit_reason.basic = EXIT_REASON_MSR_LOAD_FAIL; |
Sean Christopherson | 68cda40 | 2020-05-11 15:05:29 -0700 | [diff] [blame] | 3422 | vmcs12->exit_qualification = failed_index; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3423 | goto vmentry_fail_vmexit_guest_mode; |
Sean Christopherson | 68cda40 | 2020-05-11 15:05:29 -0700 | [diff] [blame] | 3424 | } |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3425 | } else { |
| 3426 | /* |
| 3427 | * The MMU is not initialized to point at the right entities yet and |
| 3428 | * "get pages" would need to read data from the guest (i.e. we will |
| 3429 | * need to perform gpa to hpa translation). Request a call |
| 3430 | * to nested_get_vmcs12_pages before the next VM-entry. The MSRs |
| 3431 | * have already been set at vmentry time and should not be reset. |
| 3432 | */ |
Paolo Bonzini | 729c15c | 2020-09-22 06:53:57 -0400 | [diff] [blame] | 3433 | kvm_make_request(KVM_REQ_GET_NESTED_STATE_PAGES, vcpu); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3434 | } |
| 3435 | |
| 3436 | /* |
| 3437 | * If L1 had a pending IRQ/NMI until it executed |
| 3438 | * VMLAUNCH/VMRESUME which wasn't delivered because it was |
| 3439 | * disallowed (e.g. interrupts disabled), L0 needs to |
| 3440 | * evaluate if this pending event should cause an exit from L2 |
| 3441 | * to L1 or delivered directly to L2 (e.g. In case L1 don't |
| 3442 | * intercept EXTERNAL_INTERRUPT). |
| 3443 | * |
| 3444 | * Usually this would be handled by the processor noticing an |
| 3445 | * IRQ/NMI window request, or checking RVI during evaluation of |
| 3446 | * pending virtual interrupts. However, this setting was done |
| 3447 | * on VMCS01 and now VMCS02 is active instead. Thus, we force L0 |
| 3448 | * to perform pending event evaluation by requesting a KVM_REQ_EVENT. |
| 3449 | */ |
| 3450 | if (unlikely(evaluate_pending_interrupts)) |
| 3451 | kvm_make_request(KVM_REQ_EVENT, vcpu); |
| 3452 | |
| 3453 | /* |
Paolo Bonzini | 359a6c3 | 2019-01-29 19:14:46 +0100 | [diff] [blame] | 3454 | * Do not start the preemption timer hrtimer until after we know |
| 3455 | * we are successful, so that only nested_vmx_vmexit needs to cancel |
| 3456 | * the timer. |
| 3457 | */ |
| 3458 | vmx->nested.preemption_timer_expired = false; |
Peter Shier | 850448f | 2020-05-26 14:51:06 -0700 | [diff] [blame] | 3459 | if (nested_cpu_has_preemption_timer(vmcs12)) { |
| 3460 | u64 timer_value = vmx_calc_preemption_timer_value(vcpu); |
| 3461 | vmx_start_preemption_timer(vcpu, timer_value); |
| 3462 | } |
Paolo Bonzini | 359a6c3 | 2019-01-29 19:14:46 +0100 | [diff] [blame] | 3463 | |
| 3464 | /* |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3465 | * Note no nested_vmx_succeed or nested_vmx_fail here. At this point |
| 3466 | * we are no longer running L1, and VMLAUNCH/VMRESUME has not yet |
| 3467 | * returned as far as L1 is concerned. It will only return (and set |
| 3468 | * the success flag) when L2 exits (see nested_vmx_vmexit()). |
| 3469 | */ |
Jim Mattson | 671ddc7 | 2019-10-15 10:44:05 -0700 | [diff] [blame] | 3470 | return NVMX_VMENTRY_SUCCESS; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3471 | |
| 3472 | /* |
| 3473 | * A failed consistency check that leads to a VMExit during L1's |
| 3474 | * VMEnter to L2 is a variation of a normal VMexit, as explained in |
| 3475 | * 26.7 "VM-entry failures during or after loading guest state". |
| 3476 | */ |
| 3477 | vmentry_fail_vmexit_guest_mode: |
Xiaoyao Li | 5e3d394 | 2019-12-06 16:45:26 +0800 | [diff] [blame] | 3478 | if (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETTING) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3479 | vcpu->arch.tsc_offset -= vmcs12->tsc_offset; |
| 3480 | leave_guest_mode(vcpu); |
| 3481 | |
| 3482 | vmentry_fail_vmexit: |
| 3483 | vmx_switch_vmcs(vcpu, &vmx->vmcs01); |
| 3484 | |
| 3485 | if (!from_vmentry) |
Jim Mattson | 671ddc7 | 2019-10-15 10:44:05 -0700 | [diff] [blame] | 3486 | return NVMX_VMENTRY_VMEXIT; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3487 | |
| 3488 | load_vmcs12_host_state(vcpu, vmcs12); |
Sean Christopherson | 8e53324 | 2020-11-06 17:03:12 +0800 | [diff] [blame] | 3489 | vmcs12->vm_exit_reason = exit_reason.full; |
Vitaly Kuznetsov | 1e9dfbd | 2021-05-26 15:20:16 +0200 | [diff] [blame] | 3490 | if (enable_shadow_vmcs || evmptr_is_valid(vmx->nested.hv_evmcs_vmptr)) |
Sean Christopherson | 3731905ef | 2019-05-07 08:36:27 -0700 | [diff] [blame] | 3491 | vmx->nested.need_vmcs12_to_shadow_sync = true; |
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 | |
| 3495 | /* |
| 3496 | * nested_vmx_run() handles a nested entry, i.e., a VMLAUNCH or VMRESUME on L1 |
| 3497 | * for running an L2 nested guest. |
| 3498 | */ |
| 3499 | static int nested_vmx_run(struct kvm_vcpu *vcpu, bool launch) |
| 3500 | { |
| 3501 | struct vmcs12 *vmcs12; |
Jim Mattson | 671ddc7 | 2019-10-15 10:44:05 -0700 | [diff] [blame] | 3502 | enum nvmx_vmentry_status status; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3503 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 3504 | u32 interrupt_shadow = vmx_get_interrupt_shadow(vcpu); |
Vitaly Kuznetsov | b6a0653 | 2020-03-09 16:52:13 +0100 | [diff] [blame] | 3505 | enum nested_evmptrld_status evmptrld_status; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3506 | |
| 3507 | if (!nested_vmx_check_permission(vcpu)) |
| 3508 | return 1; |
| 3509 | |
Vitaly Kuznetsov | b6a0653 | 2020-03-09 16:52:13 +0100 | [diff] [blame] | 3510 | evmptrld_status = nested_vmx_handle_enlightened_vmptrld(vcpu, launch); |
| 3511 | if (evmptrld_status == EVMPTRLD_ERROR) { |
| 3512 | kvm_queue_exception(vcpu, UD_VECTOR); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3513 | return 1; |
Sean Christopherson | fc595f3 | 2020-08-12 11:06:15 -0700 | [diff] [blame] | 3514 | } else if (CC(evmptrld_status == EVMPTRLD_VMFAIL)) { |
Vitaly Kuznetsov | b6a0653 | 2020-03-09 16:52:13 +0100 | [diff] [blame] | 3515 | return nested_vmx_failInvalid(vcpu); |
| 3516 | } |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3517 | |
Vitaly Kuznetsov | 1e9dfbd | 2021-05-26 15:20:16 +0200 | [diff] [blame] | 3518 | if (CC(!evmptr_is_valid(vmx->nested.hv_evmcs_vmptr) && |
Yu Zhang | 64c7850 | 2021-09-30 01:51:53 +0800 | [diff] [blame] | 3519 | vmx->nested.current_vmptr == INVALID_GPA)) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3520 | return nested_vmx_failInvalid(vcpu); |
| 3521 | |
| 3522 | vmcs12 = get_vmcs12(vcpu); |
| 3523 | |
| 3524 | /* |
| 3525 | * Can't VMLAUNCH or VMRESUME a shadow VMCS. Despite the fact |
| 3526 | * that there *is* a valid VMCS pointer, RFLAGS.CF is set |
| 3527 | * rather than RFLAGS.ZF, and no error number is stored to the |
| 3528 | * VM-instruction error field. |
| 3529 | */ |
Sean Christopherson | fc595f3 | 2020-08-12 11:06:15 -0700 | [diff] [blame] | 3530 | if (CC(vmcs12->hdr.shadow_vmcs)) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3531 | return nested_vmx_failInvalid(vcpu); |
| 3532 | |
Vitaly Kuznetsov | 1e9dfbd | 2021-05-26 15:20:16 +0200 | [diff] [blame] | 3533 | if (evmptr_is_valid(vmx->nested.hv_evmcs_vmptr)) { |
Vitaly Kuznetsov | d6bf71a | 2021-05-26 15:20:22 +0200 | [diff] [blame] | 3534 | copy_enlightened_to_vmcs12(vmx, vmx->nested.hv_evmcs->hv_clean_fields); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3535 | /* Enlightened VMCS doesn't have launch state */ |
| 3536 | vmcs12->launch_state = !launch; |
| 3537 | } else if (enable_shadow_vmcs) { |
| 3538 | copy_shadow_to_vmcs12(vmx); |
| 3539 | } |
| 3540 | |
| 3541 | /* |
| 3542 | * The nested entry process starts with enforcing various prerequisites |
| 3543 | * on vmcs12 as required by the Intel SDM, and act appropriately when |
| 3544 | * they fail: As the SDM explains, some conditions should cause the |
| 3545 | * instruction to fail, while others will cause the instruction to seem |
| 3546 | * to succeed, but return an EXIT_REASON_INVALID_STATE. |
| 3547 | * To speed up the normal (success) code path, we should avoid checking |
| 3548 | * for misconfigurations which will anyway be caught by the processor |
| 3549 | * when using the merged vmcs02. |
| 3550 | */ |
Sean Christopherson | fc595f3 | 2020-08-12 11:06:15 -0700 | [diff] [blame] | 3551 | if (CC(interrupt_shadow & KVM_X86_SHADOW_INT_MOV_SS)) |
Sean Christopherson | b2656e4 | 2020-06-08 18:56:07 -0700 | [diff] [blame] | 3552 | return nested_vmx_fail(vcpu, VMXERR_ENTRY_EVENTS_BLOCKED_BY_MOV_SS); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3553 | |
Sean Christopherson | fc595f3 | 2020-08-12 11:06:15 -0700 | [diff] [blame] | 3554 | if (CC(vmcs12->launch_state == launch)) |
Sean Christopherson | b2656e4 | 2020-06-08 18:56:07 -0700 | [diff] [blame] | 3555 | return nested_vmx_fail(vcpu, |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3556 | launch ? VMXERR_VMLAUNCH_NONCLEAR_VMCS |
| 3557 | : VMXERR_VMRESUME_NONLAUNCHED_VMCS); |
| 3558 | |
Paolo Bonzini | 98d9e85 | 2019-04-12 10:19:57 +0200 | [diff] [blame] | 3559 | if (nested_vmx_check_controls(vcpu, vmcs12)) |
Sean Christopherson | b2656e4 | 2020-06-08 18:56:07 -0700 | [diff] [blame] | 3560 | return nested_vmx_fail(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD); |
Sean Christopherson | 5478ba3 | 2019-04-11 12:18:06 -0700 | [diff] [blame] | 3561 | |
Maxim Levitsky | af957ee | 2021-11-15 15:18:36 +0200 | [diff] [blame] | 3562 | if (nested_vmx_check_address_space_size(vcpu, vmcs12)) |
| 3563 | return nested_vmx_fail(vcpu, VMXERR_ENTRY_INVALID_HOST_STATE_FIELD); |
| 3564 | |
Paolo Bonzini | 98d9e85 | 2019-04-12 10:19:57 +0200 | [diff] [blame] | 3565 | if (nested_vmx_check_host_state(vcpu, vmcs12)) |
Sean Christopherson | b2656e4 | 2020-06-08 18:56:07 -0700 | [diff] [blame] | 3566 | return nested_vmx_fail(vcpu, VMXERR_ENTRY_INVALID_HOST_STATE_FIELD); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3567 | |
| 3568 | /* |
| 3569 | * We're finally done with prerequisite checking, and can start with |
| 3570 | * the nested entry. |
| 3571 | */ |
| 3572 | vmx->nested.nested_run_pending = 1; |
Peter Shier | 850448f | 2020-05-26 14:51:06 -0700 | [diff] [blame] | 3573 | vmx->nested.has_preemption_timer_deadline = false; |
Jim Mattson | 671ddc7 | 2019-10-15 10:44:05 -0700 | [diff] [blame] | 3574 | status = nested_vmx_enter_non_root_mode(vcpu, true); |
| 3575 | if (unlikely(status != NVMX_VMENTRY_SUCCESS)) |
| 3576 | goto vmentry_failed; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3577 | |
Sean Christopherson | 25bb2cf | 2020-08-12 10:51:29 -0700 | [diff] [blame] | 3578 | /* Emulate processing of posted interrupts on VM-Enter. */ |
| 3579 | if (nested_cpu_has_posted_intr(vmcs12) && |
| 3580 | kvm_apic_has_interrupt(vcpu) == vmx->nested.posted_intr_nv) { |
| 3581 | vmx->nested.pi_pending = true; |
| 3582 | kvm_make_request(KVM_REQ_EVENT, vcpu); |
| 3583 | kvm_apic_clear_irr(vcpu, vmx->nested.posted_intr_nv); |
| 3584 | } |
| 3585 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3586 | /* Hide L1D cache contents from the nested guest. */ |
| 3587 | vmx->vcpu.arch.l1tf_flush_l1d = true; |
| 3588 | |
| 3589 | /* |
| 3590 | * Must happen outside of nested_vmx_enter_non_root_mode() as it will |
| 3591 | * also be used as part of restoring nVMX state for |
| 3592 | * snapshot restore (migration). |
| 3593 | * |
| 3594 | * In this flow, it is assumed that vmcs12 cache was |
Ingo Molnar | 163b099 | 2021-03-21 22:28:53 +0100 | [diff] [blame] | 3595 | * transferred as part of captured nVMX state and should |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3596 | * therefore not be read from guest memory (which may not |
| 3597 | * exist on destination host yet). |
| 3598 | */ |
| 3599 | nested_cache_shadow_vmcs12(vcpu, vmcs12); |
| 3600 | |
Yadong Qi | bf0cd88 | 2020-11-06 14:51:22 +0800 | [diff] [blame] | 3601 | switch (vmcs12->guest_activity_state) { |
| 3602 | case GUEST_ACTIVITY_HLT: |
| 3603 | /* |
| 3604 | * If we're entering a halted L2 vcpu and the L2 vcpu won't be |
| 3605 | * awakened by event injection or by an NMI-window VM-exit or |
| 3606 | * by an interrupt-window VM-exit, halt the vcpu. |
| 3607 | */ |
| 3608 | if (!(vmcs12->vm_entry_intr_info_field & INTR_INFO_VALID_MASK) && |
| 3609 | !nested_cpu_has(vmcs12, CPU_BASED_NMI_WINDOW_EXITING) && |
| 3610 | !(nested_cpu_has(vmcs12, CPU_BASED_INTR_WINDOW_EXITING) && |
| 3611 | (vmcs12->guest_rflags & X86_EFLAGS_IF))) { |
| 3612 | vmx->nested.nested_run_pending = 0; |
Sean Christopherson | 1460179 | 2021-10-08 19:12:05 -0700 | [diff] [blame] | 3613 | return kvm_emulate_halt_noskip(vcpu); |
Yadong Qi | bf0cd88 | 2020-11-06 14:51:22 +0800 | [diff] [blame] | 3614 | } |
| 3615 | break; |
| 3616 | case GUEST_ACTIVITY_WAIT_SIPI: |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3617 | vmx->nested.nested_run_pending = 0; |
Yadong Qi | bf0cd88 | 2020-11-06 14:51:22 +0800 | [diff] [blame] | 3618 | vcpu->arch.mp_state = KVM_MP_STATE_INIT_RECEIVED; |
| 3619 | break; |
| 3620 | default: |
| 3621 | break; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3622 | } |
Yadong Qi | bf0cd88 | 2020-11-06 14:51:22 +0800 | [diff] [blame] | 3623 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3624 | return 1; |
Jim Mattson | 671ddc7 | 2019-10-15 10:44:05 -0700 | [diff] [blame] | 3625 | |
| 3626 | vmentry_failed: |
| 3627 | vmx->nested.nested_run_pending = 0; |
| 3628 | if (status == NVMX_VMENTRY_KVM_INTERNAL_ERROR) |
| 3629 | return 0; |
| 3630 | if (status == NVMX_VMENTRY_VMEXIT) |
| 3631 | return 1; |
| 3632 | WARN_ON_ONCE(status != NVMX_VMENTRY_VMFAIL); |
Sean Christopherson | b2656e4 | 2020-06-08 18:56:07 -0700 | [diff] [blame] | 3633 | return nested_vmx_fail(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3634 | } |
| 3635 | |
| 3636 | /* |
| 3637 | * 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] | 3638 | * 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] | 3639 | * This function returns the new value we should put in vmcs12.guest_cr0. |
| 3640 | * It's not enough to just return the vmcs02 GUEST_CR0. Rather, |
| 3641 | * 1. Bits that neither L0 nor L1 trapped, were set directly by L2 and are now |
| 3642 | * available in vmcs02 GUEST_CR0. (Note: It's enough to check that L0 |
| 3643 | * didn't trap the bit, because if L1 did, so would L0). |
| 3644 | * 2. Bits that L1 asked to trap (and therefore L0 also did) could not have |
| 3645 | * been modified by L2, and L1 knows it. So just leave the old value of |
| 3646 | * the bit from vmcs12.guest_cr0. Note that the bit from vmcs02 GUEST_CR0 |
| 3647 | * isn't relevant, because if L0 traps this bit it can set it to anything. |
| 3648 | * 3. Bits that L1 didn't trap, but L0 did. L1 believes the guest could have |
| 3649 | * changed these bits, and therefore they need to be updated, but L0 |
| 3650 | * didn't necessarily allow them to be changed in GUEST_CR0 - and rather |
| 3651 | * put them in vmcs02 CR0_READ_SHADOW. So take these bits from there. |
| 3652 | */ |
| 3653 | static inline unsigned long |
| 3654 | vmcs12_guest_cr0(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12) |
| 3655 | { |
| 3656 | return |
| 3657 | /*1*/ (vmcs_readl(GUEST_CR0) & vcpu->arch.cr0_guest_owned_bits) | |
| 3658 | /*2*/ (vmcs12->guest_cr0 & vmcs12->cr0_guest_host_mask) | |
| 3659 | /*3*/ (vmcs_readl(CR0_READ_SHADOW) & ~(vmcs12->cr0_guest_host_mask | |
| 3660 | vcpu->arch.cr0_guest_owned_bits)); |
| 3661 | } |
| 3662 | |
| 3663 | static inline unsigned long |
| 3664 | vmcs12_guest_cr4(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12) |
| 3665 | { |
| 3666 | return |
| 3667 | /*1*/ (vmcs_readl(GUEST_CR4) & vcpu->arch.cr4_guest_owned_bits) | |
| 3668 | /*2*/ (vmcs12->guest_cr4 & vmcs12->cr4_guest_host_mask) | |
| 3669 | /*3*/ (vmcs_readl(CR4_READ_SHADOW) & ~(vmcs12->cr4_guest_host_mask | |
| 3670 | vcpu->arch.cr4_guest_owned_bits)); |
| 3671 | } |
| 3672 | |
| 3673 | static void vmcs12_save_pending_event(struct kvm_vcpu *vcpu, |
| 3674 | struct vmcs12 *vmcs12) |
| 3675 | { |
| 3676 | u32 idt_vectoring; |
| 3677 | unsigned int nr; |
| 3678 | |
| 3679 | if (vcpu->arch.exception.injected) { |
| 3680 | nr = vcpu->arch.exception.nr; |
| 3681 | idt_vectoring = nr | VECTORING_INFO_VALID_MASK; |
| 3682 | |
| 3683 | if (kvm_exception_is_soft(nr)) { |
| 3684 | vmcs12->vm_exit_instruction_len = |
| 3685 | vcpu->arch.event_exit_inst_len; |
| 3686 | idt_vectoring |= INTR_TYPE_SOFT_EXCEPTION; |
| 3687 | } else |
| 3688 | idt_vectoring |= INTR_TYPE_HARD_EXCEPTION; |
| 3689 | |
| 3690 | if (vcpu->arch.exception.has_error_code) { |
| 3691 | idt_vectoring |= VECTORING_INFO_DELIVER_CODE_MASK; |
| 3692 | vmcs12->idt_vectoring_error_code = |
| 3693 | vcpu->arch.exception.error_code; |
| 3694 | } |
| 3695 | |
| 3696 | vmcs12->idt_vectoring_info_field = idt_vectoring; |
| 3697 | } else if (vcpu->arch.nmi_injected) { |
| 3698 | vmcs12->idt_vectoring_info_field = |
| 3699 | INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK | NMI_VECTOR; |
| 3700 | } else if (vcpu->arch.interrupt.injected) { |
| 3701 | nr = vcpu->arch.interrupt.nr; |
| 3702 | idt_vectoring = nr | VECTORING_INFO_VALID_MASK; |
| 3703 | |
| 3704 | if (vcpu->arch.interrupt.soft) { |
| 3705 | idt_vectoring |= INTR_TYPE_SOFT_INTR; |
| 3706 | vmcs12->vm_entry_instruction_len = |
| 3707 | vcpu->arch.event_exit_inst_len; |
| 3708 | } else |
| 3709 | idt_vectoring |= INTR_TYPE_EXT_INTR; |
| 3710 | |
| 3711 | vmcs12->idt_vectoring_info_field = idt_vectoring; |
| 3712 | } |
| 3713 | } |
| 3714 | |
| 3715 | |
Paolo Bonzini | 96b100c | 2020-03-17 18:32:50 +0100 | [diff] [blame] | 3716 | void nested_mark_vmcs12_pages_dirty(struct kvm_vcpu *vcpu) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3717 | { |
| 3718 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
| 3719 | gfn_t gfn; |
| 3720 | |
| 3721 | /* |
| 3722 | * Don't need to mark the APIC access page dirty; it is never |
| 3723 | * written to by the CPU during APIC virtualization. |
| 3724 | */ |
| 3725 | |
| 3726 | if (nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW)) { |
| 3727 | gfn = vmcs12->virtual_apic_page_addr >> PAGE_SHIFT; |
| 3728 | kvm_vcpu_mark_page_dirty(vcpu, gfn); |
| 3729 | } |
| 3730 | |
| 3731 | if (nested_cpu_has_posted_intr(vmcs12)) { |
| 3732 | gfn = vmcs12->posted_intr_desc_addr >> PAGE_SHIFT; |
| 3733 | kvm_vcpu_mark_page_dirty(vcpu, gfn); |
| 3734 | } |
| 3735 | } |
| 3736 | |
Jim Mattson | 650293c | 2021-06-04 10:26:02 -0700 | [diff] [blame] | 3737 | static int vmx_complete_nested_posted_interrupt(struct kvm_vcpu *vcpu) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3738 | { |
| 3739 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 3740 | int max_irr; |
| 3741 | void *vapic_page; |
| 3742 | u16 status; |
| 3743 | |
Jim Mattson | 966eefb | 2021-06-04 10:26:06 -0700 | [diff] [blame] | 3744 | if (!vmx->nested.pi_pending) |
Jim Mattson | 650293c | 2021-06-04 10:26:02 -0700 | [diff] [blame] | 3745 | return 0; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3746 | |
Jim Mattson | 966eefb | 2021-06-04 10:26:06 -0700 | [diff] [blame] | 3747 | if (!vmx->nested.pi_desc) |
| 3748 | goto mmio_needed; |
| 3749 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3750 | vmx->nested.pi_pending = false; |
Jim Mattson | 966eefb | 2021-06-04 10:26:06 -0700 | [diff] [blame] | 3751 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3752 | if (!pi_test_and_clear_on(vmx->nested.pi_desc)) |
Jim Mattson | 650293c | 2021-06-04 10:26:02 -0700 | [diff] [blame] | 3753 | return 0; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3754 | |
| 3755 | max_irr = find_last_bit((unsigned long *)vmx->nested.pi_desc->pir, 256); |
| 3756 | if (max_irr != 256) { |
KarimAllah Ahmed | 96c66e8 | 2019-01-31 21:24:37 +0100 | [diff] [blame] | 3757 | vapic_page = vmx->nested.virtual_apic_map.hva; |
| 3758 | if (!vapic_page) |
Jim Mattson | 0fe998b | 2021-06-04 10:26:05 -0700 | [diff] [blame] | 3759 | goto mmio_needed; |
KarimAllah Ahmed | 96c66e8 | 2019-01-31 21:24:37 +0100 | [diff] [blame] | 3760 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3761 | __kvm_apic_update_irr(vmx->nested.pi_desc->pir, |
| 3762 | vapic_page, &max_irr); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3763 | status = vmcs_read16(GUEST_INTR_STATUS); |
| 3764 | if ((u8)max_irr > ((u8)status & 0xff)) { |
| 3765 | status &= ~0xff; |
| 3766 | status |= (u8)max_irr; |
| 3767 | vmcs_write16(GUEST_INTR_STATUS, status); |
| 3768 | } |
| 3769 | } |
| 3770 | |
| 3771 | nested_mark_vmcs12_pages_dirty(vcpu); |
Jim Mattson | 650293c | 2021-06-04 10:26:02 -0700 | [diff] [blame] | 3772 | return 0; |
Jim Mattson | 0fe998b | 2021-06-04 10:26:05 -0700 | [diff] [blame] | 3773 | |
| 3774 | mmio_needed: |
| 3775 | kvm_handle_memory_failure(vcpu, X86EMUL_IO_NEEDED, NULL); |
| 3776 | return -ENXIO; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3777 | } |
| 3778 | |
| 3779 | static void nested_vmx_inject_exception_vmexit(struct kvm_vcpu *vcpu, |
| 3780 | unsigned long exit_qual) |
| 3781 | { |
| 3782 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
| 3783 | unsigned int nr = vcpu->arch.exception.nr; |
| 3784 | u32 intr_info = nr | INTR_INFO_VALID_MASK; |
| 3785 | |
| 3786 | if (vcpu->arch.exception.has_error_code) { |
| 3787 | vmcs12->vm_exit_intr_error_code = vcpu->arch.exception.error_code; |
| 3788 | intr_info |= INTR_INFO_DELIVER_CODE_MASK; |
| 3789 | } |
| 3790 | |
| 3791 | if (kvm_exception_is_soft(nr)) |
| 3792 | intr_info |= INTR_TYPE_SOFT_EXCEPTION; |
| 3793 | else |
| 3794 | intr_info |= INTR_TYPE_HARD_EXCEPTION; |
| 3795 | |
| 3796 | if (!(vmcs12->idt_vectoring_info_field & VECTORING_INFO_VALID_MASK) && |
| 3797 | vmx_get_nmi_mask(vcpu)) |
| 3798 | intr_info |= INTR_INFO_UNBLOCK_NMI; |
| 3799 | |
| 3800 | nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI, intr_info, exit_qual); |
| 3801 | } |
| 3802 | |
Oliver Upton | 684c042 | 2020-02-07 02:36:05 -0800 | [diff] [blame] | 3803 | /* |
| 3804 | * Returns true if a debug trap is pending delivery. |
| 3805 | * |
| 3806 | * In KVM, debug traps bear an exception payload. As such, the class of a #DB |
| 3807 | * exception may be inferred from the presence of an exception payload. |
| 3808 | */ |
| 3809 | static inline bool vmx_pending_dbg_trap(struct kvm_vcpu *vcpu) |
| 3810 | { |
| 3811 | return vcpu->arch.exception.pending && |
| 3812 | vcpu->arch.exception.nr == DB_VECTOR && |
| 3813 | vcpu->arch.exception.payload; |
| 3814 | } |
| 3815 | |
| 3816 | /* |
| 3817 | * Certain VM-exits set the 'pending debug exceptions' field to indicate a |
| 3818 | * recognized #DB (data or single-step) that has yet to be delivered. Since KVM |
| 3819 | * represents these debug traps with a payload that is said to be compatible |
| 3820 | * with the 'pending debug exceptions' field, write the payload to the VMCS |
| 3821 | * field if a VM-exit is delivered before the debug trap. |
| 3822 | */ |
| 3823 | static void nested_vmx_update_pending_dbg(struct kvm_vcpu *vcpu) |
| 3824 | { |
| 3825 | if (vmx_pending_dbg_trap(vcpu)) |
| 3826 | vmcs_writel(GUEST_PENDING_DBG_EXCEPTIONS, |
| 3827 | vcpu->arch.exception.payload); |
| 3828 | } |
| 3829 | |
Sean Christopherson | d2060bd | 2020-04-22 19:25:39 -0700 | [diff] [blame] | 3830 | static bool nested_vmx_preemption_timer_pending(struct kvm_vcpu *vcpu) |
| 3831 | { |
| 3832 | return nested_cpu_has_preemption_timer(get_vmcs12(vcpu)) && |
| 3833 | to_vmx(vcpu)->nested.preemption_timer_expired; |
| 3834 | } |
| 3835 | |
Sean Christopherson | a1c77ab | 2020-03-02 22:27:35 -0800 | [diff] [blame] | 3836 | static int vmx_check_nested_events(struct kvm_vcpu *vcpu) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3837 | { |
| 3838 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 3839 | unsigned long exit_qual; |
| 3840 | bool block_nested_events = |
| 3841 | vmx->nested.nested_run_pending || kvm_event_needs_reinjection(vcpu); |
Oliver Upton | 5ef8acb | 2020-02-07 02:36:07 -0800 | [diff] [blame] | 3842 | bool mtf_pending = vmx->nested.mtf_pending; |
Liran Alon | 4b9852f | 2019-08-26 13:24:49 +0300 | [diff] [blame] | 3843 | struct kvm_lapic *apic = vcpu->arch.apic; |
| 3844 | |
Oliver Upton | 5ef8acb | 2020-02-07 02:36:07 -0800 | [diff] [blame] | 3845 | /* |
| 3846 | * Clear the MTF state. If a higher priority VM-exit is delivered first, |
| 3847 | * this state is discarded. |
| 3848 | */ |
Oliver Upton | 5c8beb4 | 2020-04-06 20:12:37 +0000 | [diff] [blame] | 3849 | if (!block_nested_events) |
| 3850 | vmx->nested.mtf_pending = false; |
Oliver Upton | 5ef8acb | 2020-02-07 02:36:07 -0800 | [diff] [blame] | 3851 | |
Liran Alon | 4b9852f | 2019-08-26 13:24:49 +0300 | [diff] [blame] | 3852 | if (lapic_in_kernel(vcpu) && |
| 3853 | test_bit(KVM_APIC_INIT, &apic->pending_events)) { |
| 3854 | if (block_nested_events) |
| 3855 | return -EBUSY; |
Oliver Upton | 684c042 | 2020-02-07 02:36:05 -0800 | [diff] [blame] | 3856 | nested_vmx_update_pending_dbg(vcpu); |
Liran Alon | e64a850 | 2019-11-11 14:16:05 +0200 | [diff] [blame] | 3857 | clear_bit(KVM_APIC_INIT, &apic->pending_events); |
Yadong Qi | bf0cd88 | 2020-11-06 14:51:22 +0800 | [diff] [blame] | 3858 | if (vcpu->arch.mp_state != KVM_MP_STATE_INIT_RECEIVED) |
| 3859 | nested_vmx_vmexit(vcpu, EXIT_REASON_INIT_SIGNAL, 0, 0); |
| 3860 | return 0; |
| 3861 | } |
| 3862 | |
| 3863 | if (lapic_in_kernel(vcpu) && |
| 3864 | test_bit(KVM_APIC_SIPI, &apic->pending_events)) { |
| 3865 | if (block_nested_events) |
| 3866 | return -EBUSY; |
| 3867 | |
| 3868 | clear_bit(KVM_APIC_SIPI, &apic->pending_events); |
| 3869 | if (vcpu->arch.mp_state == KVM_MP_STATE_INIT_RECEIVED) |
| 3870 | nested_vmx_vmexit(vcpu, EXIT_REASON_SIPI_SIGNAL, 0, |
| 3871 | apic->sipi_vector & 0xFFUL); |
Liran Alon | 4b9852f | 2019-08-26 13:24:49 +0300 | [diff] [blame] | 3872 | return 0; |
| 3873 | } |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3874 | |
Oliver Upton | 5ef8acb | 2020-02-07 02:36:07 -0800 | [diff] [blame] | 3875 | /* |
| 3876 | * Process any exceptions that are not debug traps before MTF. |
Maxim Levitsky | 4020da3 | 2021-04-01 17:38:14 +0300 | [diff] [blame] | 3877 | * |
| 3878 | * Note that only a pending nested run can block a pending exception. |
| 3879 | * Otherwise an injected NMI/interrupt should either be |
| 3880 | * lost or delivered to the nested hypervisor in the IDT_VECTORING_INFO, |
| 3881 | * while delivering the pending exception. |
Oliver Upton | 5ef8acb | 2020-02-07 02:36:07 -0800 | [diff] [blame] | 3882 | */ |
Maxim Levitsky | 4020da3 | 2021-04-01 17:38:14 +0300 | [diff] [blame] | 3883 | |
Sean Christopherson | 6ce347a | 2020-04-22 19:25:38 -0700 | [diff] [blame] | 3884 | if (vcpu->arch.exception.pending && !vmx_pending_dbg_trap(vcpu)) { |
Maxim Levitsky | 4020da3 | 2021-04-01 17:38:14 +0300 | [diff] [blame] | 3885 | if (vmx->nested.nested_run_pending) |
Oliver Upton | 5ef8acb | 2020-02-07 02:36:07 -0800 | [diff] [blame] | 3886 | return -EBUSY; |
Sean Christopherson | 6ce347a | 2020-04-22 19:25:38 -0700 | [diff] [blame] | 3887 | if (!nested_vmx_check_exception(vcpu, &exit_qual)) |
| 3888 | goto no_vmexit; |
Oliver Upton | 5ef8acb | 2020-02-07 02:36:07 -0800 | [diff] [blame] | 3889 | nested_vmx_inject_exception_vmexit(vcpu, exit_qual); |
| 3890 | return 0; |
| 3891 | } |
| 3892 | |
| 3893 | if (mtf_pending) { |
| 3894 | if (block_nested_events) |
| 3895 | return -EBUSY; |
| 3896 | nested_vmx_update_pending_dbg(vcpu); |
| 3897 | nested_vmx_vmexit(vcpu, EXIT_REASON_MONITOR_TRAP_FLAG, 0, 0); |
| 3898 | return 0; |
| 3899 | } |
| 3900 | |
Sean Christopherson | 6ce347a | 2020-04-22 19:25:38 -0700 | [diff] [blame] | 3901 | if (vcpu->arch.exception.pending) { |
Maxim Levitsky | 4020da3 | 2021-04-01 17:38:14 +0300 | [diff] [blame] | 3902 | if (vmx->nested.nested_run_pending) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3903 | return -EBUSY; |
Sean Christopherson | 6ce347a | 2020-04-22 19:25:38 -0700 | [diff] [blame] | 3904 | if (!nested_vmx_check_exception(vcpu, &exit_qual)) |
| 3905 | goto no_vmexit; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3906 | nested_vmx_inject_exception_vmexit(vcpu, exit_qual); |
| 3907 | return 0; |
| 3908 | } |
| 3909 | |
Sean Christopherson | d2060bd | 2020-04-22 19:25:39 -0700 | [diff] [blame] | 3910 | if (nested_vmx_preemption_timer_pending(vcpu)) { |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3911 | if (block_nested_events) |
| 3912 | return -EBUSY; |
| 3913 | nested_vmx_vmexit(vcpu, EXIT_REASON_PREEMPTION_TIMER, 0, 0); |
| 3914 | return 0; |
| 3915 | } |
| 3916 | |
Sean Christopherson | 1cd2f0b | 2020-04-22 19:25:46 -0700 | [diff] [blame] | 3917 | if (vcpu->arch.smi_pending && !is_smm(vcpu)) { |
| 3918 | if (block_nested_events) |
| 3919 | return -EBUSY; |
| 3920 | goto no_vmexit; |
| 3921 | } |
| 3922 | |
Sean Christopherson | 15ff0b4 | 2020-04-22 19:25:45 -0700 | [diff] [blame] | 3923 | if (vcpu->arch.nmi_pending && !vmx_nmi_blocked(vcpu)) { |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3924 | if (block_nested_events) |
| 3925 | return -EBUSY; |
Sean Christopherson | 15ff0b4 | 2020-04-22 19:25:45 -0700 | [diff] [blame] | 3926 | if (!nested_exit_on_nmi(vcpu)) |
| 3927 | goto no_vmexit; |
| 3928 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3929 | nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI, |
| 3930 | NMI_VECTOR | INTR_TYPE_NMI_INTR | |
| 3931 | INTR_INFO_VALID_MASK, 0); |
| 3932 | /* |
| 3933 | * The NMI-triggered VM exit counts as injection: |
| 3934 | * clear this one and block further NMIs. |
| 3935 | */ |
| 3936 | vcpu->arch.nmi_pending = 0; |
| 3937 | vmx_set_nmi_mask(vcpu, true); |
| 3938 | return 0; |
| 3939 | } |
| 3940 | |
Sean Christopherson | 15ff0b4 | 2020-04-22 19:25:45 -0700 | [diff] [blame] | 3941 | if (kvm_cpu_has_interrupt(vcpu) && !vmx_interrupt_blocked(vcpu)) { |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3942 | if (block_nested_events) |
| 3943 | return -EBUSY; |
Sean Christopherson | 15ff0b4 | 2020-04-22 19:25:45 -0700 | [diff] [blame] | 3944 | if (!nested_exit_on_intr(vcpu)) |
| 3945 | goto no_vmexit; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3946 | nested_vmx_vmexit(vcpu, EXIT_REASON_EXTERNAL_INTERRUPT, 0, 0); |
| 3947 | return 0; |
| 3948 | } |
| 3949 | |
Sean Christopherson | 6ce347a | 2020-04-22 19:25:38 -0700 | [diff] [blame] | 3950 | no_vmexit: |
Jim Mattson | 650293c | 2021-06-04 10:26:02 -0700 | [diff] [blame] | 3951 | return vmx_complete_nested_posted_interrupt(vcpu); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3952 | } |
| 3953 | |
| 3954 | static u32 vmx_get_preemption_timer_value(struct kvm_vcpu *vcpu) |
| 3955 | { |
| 3956 | ktime_t remaining = |
| 3957 | hrtimer_get_remaining(&to_vmx(vcpu)->nested.preemption_timer); |
| 3958 | u64 value; |
| 3959 | |
| 3960 | if (ktime_to_ns(remaining) <= 0) |
| 3961 | return 0; |
| 3962 | |
| 3963 | value = ktime_to_ns(remaining) * vcpu->arch.virtual_tsc_khz; |
| 3964 | do_div(value, 1000000); |
| 3965 | return value >> VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE; |
| 3966 | } |
| 3967 | |
Sean Christopherson | 7952d76 | 2019-05-07 08:36:29 -0700 | [diff] [blame] | 3968 | static bool is_vmcs12_ext_field(unsigned long field) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3969 | { |
Sean Christopherson | 7952d76 | 2019-05-07 08:36:29 -0700 | [diff] [blame] | 3970 | switch (field) { |
| 3971 | case GUEST_ES_SELECTOR: |
| 3972 | case GUEST_CS_SELECTOR: |
| 3973 | case GUEST_SS_SELECTOR: |
| 3974 | case GUEST_DS_SELECTOR: |
| 3975 | case GUEST_FS_SELECTOR: |
| 3976 | case GUEST_GS_SELECTOR: |
| 3977 | case GUEST_LDTR_SELECTOR: |
| 3978 | case GUEST_TR_SELECTOR: |
| 3979 | case GUEST_ES_LIMIT: |
| 3980 | case GUEST_CS_LIMIT: |
| 3981 | case GUEST_SS_LIMIT: |
| 3982 | case GUEST_DS_LIMIT: |
| 3983 | case GUEST_FS_LIMIT: |
| 3984 | case GUEST_GS_LIMIT: |
| 3985 | case GUEST_LDTR_LIMIT: |
| 3986 | case GUEST_TR_LIMIT: |
| 3987 | case GUEST_GDTR_LIMIT: |
| 3988 | case GUEST_IDTR_LIMIT: |
| 3989 | case GUEST_ES_AR_BYTES: |
| 3990 | case GUEST_DS_AR_BYTES: |
| 3991 | case GUEST_FS_AR_BYTES: |
| 3992 | case GUEST_GS_AR_BYTES: |
| 3993 | case GUEST_LDTR_AR_BYTES: |
| 3994 | case GUEST_TR_AR_BYTES: |
| 3995 | case GUEST_ES_BASE: |
| 3996 | case GUEST_CS_BASE: |
| 3997 | case GUEST_SS_BASE: |
| 3998 | case GUEST_DS_BASE: |
| 3999 | case GUEST_FS_BASE: |
| 4000 | case GUEST_GS_BASE: |
| 4001 | case GUEST_LDTR_BASE: |
| 4002 | case GUEST_TR_BASE: |
| 4003 | case GUEST_GDTR_BASE: |
| 4004 | case GUEST_IDTR_BASE: |
| 4005 | case GUEST_PENDING_DBG_EXCEPTIONS: |
| 4006 | case GUEST_BNDCFGS: |
| 4007 | return true; |
| 4008 | default: |
| 4009 | break; |
| 4010 | } |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4011 | |
Sean Christopherson | 7952d76 | 2019-05-07 08:36:29 -0700 | [diff] [blame] | 4012 | return false; |
| 4013 | } |
| 4014 | |
| 4015 | static void sync_vmcs02_to_vmcs12_rare(struct kvm_vcpu *vcpu, |
| 4016 | struct vmcs12 *vmcs12) |
| 4017 | { |
| 4018 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4019 | |
| 4020 | vmcs12->guest_es_selector = vmcs_read16(GUEST_ES_SELECTOR); |
| 4021 | vmcs12->guest_cs_selector = vmcs_read16(GUEST_CS_SELECTOR); |
| 4022 | vmcs12->guest_ss_selector = vmcs_read16(GUEST_SS_SELECTOR); |
| 4023 | vmcs12->guest_ds_selector = vmcs_read16(GUEST_DS_SELECTOR); |
| 4024 | vmcs12->guest_fs_selector = vmcs_read16(GUEST_FS_SELECTOR); |
| 4025 | vmcs12->guest_gs_selector = vmcs_read16(GUEST_GS_SELECTOR); |
| 4026 | vmcs12->guest_ldtr_selector = vmcs_read16(GUEST_LDTR_SELECTOR); |
| 4027 | vmcs12->guest_tr_selector = vmcs_read16(GUEST_TR_SELECTOR); |
| 4028 | vmcs12->guest_es_limit = vmcs_read32(GUEST_ES_LIMIT); |
| 4029 | vmcs12->guest_cs_limit = vmcs_read32(GUEST_CS_LIMIT); |
| 4030 | vmcs12->guest_ss_limit = vmcs_read32(GUEST_SS_LIMIT); |
| 4031 | vmcs12->guest_ds_limit = vmcs_read32(GUEST_DS_LIMIT); |
| 4032 | vmcs12->guest_fs_limit = vmcs_read32(GUEST_FS_LIMIT); |
| 4033 | vmcs12->guest_gs_limit = vmcs_read32(GUEST_GS_LIMIT); |
| 4034 | vmcs12->guest_ldtr_limit = vmcs_read32(GUEST_LDTR_LIMIT); |
| 4035 | vmcs12->guest_tr_limit = vmcs_read32(GUEST_TR_LIMIT); |
| 4036 | vmcs12->guest_gdtr_limit = vmcs_read32(GUEST_GDTR_LIMIT); |
| 4037 | vmcs12->guest_idtr_limit = vmcs_read32(GUEST_IDTR_LIMIT); |
| 4038 | vmcs12->guest_es_ar_bytes = vmcs_read32(GUEST_ES_AR_BYTES); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4039 | vmcs12->guest_ds_ar_bytes = vmcs_read32(GUEST_DS_AR_BYTES); |
| 4040 | vmcs12->guest_fs_ar_bytes = vmcs_read32(GUEST_FS_AR_BYTES); |
| 4041 | vmcs12->guest_gs_ar_bytes = vmcs_read32(GUEST_GS_AR_BYTES); |
| 4042 | vmcs12->guest_ldtr_ar_bytes = vmcs_read32(GUEST_LDTR_AR_BYTES); |
| 4043 | vmcs12->guest_tr_ar_bytes = vmcs_read32(GUEST_TR_AR_BYTES); |
| 4044 | vmcs12->guest_es_base = vmcs_readl(GUEST_ES_BASE); |
| 4045 | vmcs12->guest_cs_base = vmcs_readl(GUEST_CS_BASE); |
| 4046 | vmcs12->guest_ss_base = vmcs_readl(GUEST_SS_BASE); |
| 4047 | vmcs12->guest_ds_base = vmcs_readl(GUEST_DS_BASE); |
| 4048 | vmcs12->guest_fs_base = vmcs_readl(GUEST_FS_BASE); |
| 4049 | vmcs12->guest_gs_base = vmcs_readl(GUEST_GS_BASE); |
| 4050 | vmcs12->guest_ldtr_base = vmcs_readl(GUEST_LDTR_BASE); |
| 4051 | vmcs12->guest_tr_base = vmcs_readl(GUEST_TR_BASE); |
| 4052 | vmcs12->guest_gdtr_base = vmcs_readl(GUEST_GDTR_BASE); |
| 4053 | vmcs12->guest_idtr_base = vmcs_readl(GUEST_IDTR_BASE); |
Sean Christopherson | 7952d76 | 2019-05-07 08:36:29 -0700 | [diff] [blame] | 4054 | vmcs12->guest_pending_dbg_exceptions = |
| 4055 | vmcs_readl(GUEST_PENDING_DBG_EXCEPTIONS); |
| 4056 | if (kvm_mpx_supported()) |
| 4057 | vmcs12->guest_bndcfgs = vmcs_read64(GUEST_BNDCFGS); |
| 4058 | |
| 4059 | vmx->nested.need_sync_vmcs02_to_vmcs12_rare = false; |
| 4060 | } |
| 4061 | |
| 4062 | static void copy_vmcs02_to_vmcs12_rare(struct kvm_vcpu *vcpu, |
| 4063 | struct vmcs12 *vmcs12) |
| 4064 | { |
| 4065 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 4066 | int cpu; |
| 4067 | |
| 4068 | if (!vmx->nested.need_sync_vmcs02_to_vmcs12_rare) |
| 4069 | return; |
| 4070 | |
| 4071 | |
| 4072 | WARN_ON_ONCE(vmx->loaded_vmcs != &vmx->vmcs01); |
| 4073 | |
| 4074 | cpu = get_cpu(); |
| 4075 | vmx->loaded_vmcs = &vmx->nested.vmcs02; |
Sean Christopherson | 1af1bb0 | 2020-05-06 16:58:50 -0700 | [diff] [blame] | 4076 | vmx_vcpu_load_vmcs(vcpu, cpu, &vmx->vmcs01); |
Sean Christopherson | 7952d76 | 2019-05-07 08:36:29 -0700 | [diff] [blame] | 4077 | |
| 4078 | sync_vmcs02_to_vmcs12_rare(vcpu, vmcs12); |
| 4079 | |
| 4080 | vmx->loaded_vmcs = &vmx->vmcs01; |
Sean Christopherson | 1af1bb0 | 2020-05-06 16:58:50 -0700 | [diff] [blame] | 4081 | vmx_vcpu_load_vmcs(vcpu, cpu, &vmx->nested.vmcs02); |
Sean Christopherson | 7952d76 | 2019-05-07 08:36:29 -0700 | [diff] [blame] | 4082 | put_cpu(); |
| 4083 | } |
| 4084 | |
| 4085 | /* |
| 4086 | * Update the guest state fields of vmcs12 to reflect changes that |
| 4087 | * occurred while L2 was running. (The "IA-32e mode guest" bit of the |
| 4088 | * VM-entry controls is also updated, since this is really a guest |
| 4089 | * state bit.) |
| 4090 | */ |
| 4091 | static void sync_vmcs02_to_vmcs12(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12) |
| 4092 | { |
| 4093 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 4094 | |
Vitaly Kuznetsov | 1e9dfbd | 2021-05-26 15:20:16 +0200 | [diff] [blame] | 4095 | if (evmptr_is_valid(vmx->nested.hv_evmcs_vmptr)) |
Sean Christopherson | 7952d76 | 2019-05-07 08:36:29 -0700 | [diff] [blame] | 4096 | sync_vmcs02_to_vmcs12_rare(vcpu, vmcs12); |
| 4097 | |
Vitaly Kuznetsov | 1e9dfbd | 2021-05-26 15:20:16 +0200 | [diff] [blame] | 4098 | vmx->nested.need_sync_vmcs02_to_vmcs12_rare = |
| 4099 | !evmptr_is_valid(vmx->nested.hv_evmcs_vmptr); |
Sean Christopherson | 7952d76 | 2019-05-07 08:36:29 -0700 | [diff] [blame] | 4100 | |
| 4101 | vmcs12->guest_cr0 = vmcs12_guest_cr0(vcpu, vmcs12); |
| 4102 | vmcs12->guest_cr4 = vmcs12_guest_cr4(vcpu, vmcs12); |
| 4103 | |
| 4104 | vmcs12->guest_rsp = kvm_rsp_read(vcpu); |
| 4105 | vmcs12->guest_rip = kvm_rip_read(vcpu); |
| 4106 | vmcs12->guest_rflags = vmcs_readl(GUEST_RFLAGS); |
| 4107 | |
| 4108 | vmcs12->guest_cs_ar_bytes = vmcs_read32(GUEST_CS_AR_BYTES); |
| 4109 | vmcs12->guest_ss_ar_bytes = vmcs_read32(GUEST_SS_AR_BYTES); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4110 | |
| 4111 | vmcs12->guest_interruptibility_info = |
| 4112 | vmcs_read32(GUEST_INTERRUPTIBILITY_INFO); |
Sean Christopherson | 7952d76 | 2019-05-07 08:36:29 -0700 | [diff] [blame] | 4113 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4114 | if (vcpu->arch.mp_state == KVM_MP_STATE_HALTED) |
| 4115 | vmcs12->guest_activity_state = GUEST_ACTIVITY_HLT; |
Yadong Qi | bf0cd88 | 2020-11-06 14:51:22 +0800 | [diff] [blame] | 4116 | else if (vcpu->arch.mp_state == KVM_MP_STATE_INIT_RECEIVED) |
| 4117 | vmcs12->guest_activity_state = GUEST_ACTIVITY_WAIT_SIPI; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4118 | else |
| 4119 | vmcs12->guest_activity_state = GUEST_ACTIVITY_ACTIVE; |
| 4120 | |
Paolo Bonzini | b4b65b5 | 2019-01-29 19:12:35 +0100 | [diff] [blame] | 4121 | if (nested_cpu_has_preemption_timer(vmcs12) && |
Peter Shier | 850448f | 2020-05-26 14:51:06 -0700 | [diff] [blame] | 4122 | vmcs12->vm_exit_controls & VM_EXIT_SAVE_VMX_PREEMPTION_TIMER && |
| 4123 | !vmx->nested.nested_run_pending) |
| 4124 | vmcs12->vmx_preemption_timer_value = |
| 4125 | vmx_get_preemption_timer_value(vcpu); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4126 | |
| 4127 | /* |
| 4128 | * In some cases (usually, nested EPT), L2 is allowed to change its |
| 4129 | * own CR3 without exiting. If it has changed it, we must keep it. |
| 4130 | * Of course, if L0 is using shadow page tables, GUEST_CR3 was defined |
| 4131 | * by L0, not L1 or L2, so we mustn't unconditionally copy it to vmcs12. |
| 4132 | * |
| 4133 | * Additionally, restore L2's PDPTR to vmcs12. |
| 4134 | */ |
| 4135 | if (enable_ept) { |
| 4136 | vmcs12->guest_cr3 = vmcs_readl(GUEST_CR3); |
Sean Christopherson | c7554efc | 2019-05-07 09:06:40 -0700 | [diff] [blame] | 4137 | if (nested_cpu_has_ept(vmcs12) && is_pae_paging(vcpu)) { |
| 4138 | vmcs12->guest_pdptr0 = vmcs_read64(GUEST_PDPTR0); |
| 4139 | vmcs12->guest_pdptr1 = vmcs_read64(GUEST_PDPTR1); |
| 4140 | vmcs12->guest_pdptr2 = vmcs_read64(GUEST_PDPTR2); |
| 4141 | vmcs12->guest_pdptr3 = vmcs_read64(GUEST_PDPTR3); |
| 4142 | } |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4143 | } |
| 4144 | |
| 4145 | vmcs12->guest_linear_address = vmcs_readl(GUEST_LINEAR_ADDRESS); |
| 4146 | |
| 4147 | if (nested_cpu_has_vid(vmcs12)) |
| 4148 | vmcs12->guest_intr_status = vmcs_read16(GUEST_INTR_STATUS); |
| 4149 | |
| 4150 | vmcs12->vm_entry_controls = |
| 4151 | (vmcs12->vm_entry_controls & ~VM_ENTRY_IA32E_MODE) | |
| 4152 | (vm_entry_controls_get(to_vmx(vcpu)) & VM_ENTRY_IA32E_MODE); |
| 4153 | |
Sean Christopherson | 699a1ac | 2019-05-07 09:06:37 -0700 | [diff] [blame] | 4154 | if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_DEBUG_CONTROLS) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4155 | kvm_get_dr(vcpu, 7, (unsigned long *)&vmcs12->guest_dr7); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4156 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4157 | if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_IA32_EFER) |
| 4158 | vmcs12->guest_ia32_efer = vcpu->arch.efer; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4159 | } |
| 4160 | |
| 4161 | /* |
| 4162 | * prepare_vmcs12 is part of what we need to do when the nested L2 guest exits |
| 4163 | * and we want to prepare to run its L1 parent. L1 keeps a vmcs for L2 (vmcs12), |
| 4164 | * and this function updates it to reflect the changes to the guest state while |
| 4165 | * L2 was running (and perhaps made some exits which were handled directly by L0 |
| 4166 | * without going back to L1), and to reflect the exit reason. |
| 4167 | * Note that we do not have to copy here all VMCS fields, just those that |
| 4168 | * could have changed by the L2 guest or the exit - i.e., the guest-state and |
| 4169 | * exit-information fields only. Other fields are modified by L1 with VMWRITE, |
| 4170 | * which already writes to vmcs12 directly. |
| 4171 | */ |
| 4172 | static void prepare_vmcs12(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12, |
Sean Christopherson | 4dcefa3 | 2020-04-15 10:55:18 -0700 | [diff] [blame] | 4173 | u32 vm_exit_reason, u32 exit_intr_info, |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4174 | unsigned long exit_qualification) |
| 4175 | { |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4176 | /* update exit information fields: */ |
Sean Christopherson | 4dcefa3 | 2020-04-15 10:55:18 -0700 | [diff] [blame] | 4177 | vmcs12->vm_exit_reason = vm_exit_reason; |
Sean Christopherson | 3c0c2ad | 2021-04-12 16:21:37 +1200 | [diff] [blame] | 4178 | if (to_vmx(vcpu)->exit_reason.enclave_mode) |
| 4179 | vmcs12->vm_exit_reason |= VMX_EXIT_REASONS_SGX_ENCLAVE_MODE; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4180 | vmcs12->exit_qualification = exit_qualification; |
| 4181 | vmcs12->vm_exit_intr_info = exit_intr_info; |
| 4182 | |
| 4183 | vmcs12->idt_vectoring_info_field = 0; |
| 4184 | vmcs12->vm_exit_instruction_len = vmcs_read32(VM_EXIT_INSTRUCTION_LEN); |
| 4185 | vmcs12->vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO); |
| 4186 | |
| 4187 | if (!(vmcs12->vm_exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY)) { |
| 4188 | vmcs12->launch_state = 1; |
| 4189 | |
| 4190 | /* vm_entry_intr_info_field is cleared on exit. Emulate this |
| 4191 | * instead of reading the real value. */ |
| 4192 | vmcs12->vm_entry_intr_info_field &= ~INTR_INFO_VALID_MASK; |
| 4193 | |
| 4194 | /* |
| 4195 | * Transfer the event that L0 or L1 may wanted to inject into |
| 4196 | * L2 to IDT_VECTORING_INFO_FIELD. |
| 4197 | */ |
| 4198 | vmcs12_save_pending_event(vcpu, vmcs12); |
Krish Sadhukhan | a0d4f80 | 2018-12-04 19:00:13 -0500 | [diff] [blame] | 4199 | |
| 4200 | /* |
| 4201 | * According to spec, there's no need to store the guest's |
| 4202 | * MSRs if the exit is due to a VM-entry failure that occurs |
| 4203 | * during or after loading the guest state. Since this exit |
| 4204 | * does not fall in that category, we need to save the MSRs. |
| 4205 | */ |
| 4206 | if (nested_vmx_store_msr(vcpu, |
| 4207 | vmcs12->vm_exit_msr_store_addr, |
| 4208 | vmcs12->vm_exit_msr_store_count)) |
| 4209 | nested_vmx_abort(vcpu, |
| 4210 | VMX_ABORT_SAVE_GUEST_MSR_FAIL); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4211 | } |
| 4212 | |
| 4213 | /* |
| 4214 | * Drop what we picked up for L2 via vmx_complete_interrupts. It is |
| 4215 | * preserved above and would only end up incorrectly in L1. |
| 4216 | */ |
| 4217 | vcpu->arch.nmi_injected = false; |
| 4218 | kvm_clear_exception_queue(vcpu); |
| 4219 | kvm_clear_interrupt_queue(vcpu); |
| 4220 | } |
| 4221 | |
| 4222 | /* |
| 4223 | * A part of what we need to when the nested L2 guest exits and we want to |
| 4224 | * run its L1 parent, is to reset L1's guest state to the host state specified |
| 4225 | * in vmcs12. |
| 4226 | * This function is to be called not only on normal nested exit, but also on |
| 4227 | * a nested entry failure, as explained in Intel's spec, 3B.23.7 ("VM-Entry |
| 4228 | * Failures During or After Loading Guest State"). |
| 4229 | * This function should be called when the active VMCS is L1's (vmcs01). |
| 4230 | */ |
| 4231 | static void load_vmcs12_host_state(struct kvm_vcpu *vcpu, |
| 4232 | struct vmcs12 *vmcs12) |
| 4233 | { |
Sean Christopherson | 68cda40 | 2020-05-11 15:05:29 -0700 | [diff] [blame] | 4234 | enum vm_entry_failure_code ignored; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4235 | struct kvm_segment seg; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4236 | |
| 4237 | if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_EFER) |
| 4238 | vcpu->arch.efer = vmcs12->host_ia32_efer; |
| 4239 | else if (vmcs12->vm_exit_controls & VM_EXIT_HOST_ADDR_SPACE_SIZE) |
| 4240 | vcpu->arch.efer |= (EFER_LMA | EFER_LME); |
| 4241 | else |
| 4242 | vcpu->arch.efer &= ~(EFER_LMA | EFER_LME); |
| 4243 | vmx_set_efer(vcpu, vcpu->arch.efer); |
| 4244 | |
Paolo Bonzini | e9c16c7 | 2019-04-30 22:07:26 +0200 | [diff] [blame] | 4245 | kvm_rsp_write(vcpu, vmcs12->host_rsp); |
| 4246 | kvm_rip_write(vcpu, vmcs12->host_rip); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4247 | vmx_set_rflags(vcpu, X86_EFLAGS_FIXED); |
| 4248 | vmx_set_interrupt_shadow(vcpu, 0); |
| 4249 | |
| 4250 | /* |
| 4251 | * Note that calling vmx_set_cr0 is important, even if cr0 hasn't |
| 4252 | * actually changed, because vmx_set_cr0 refers to efer set above. |
| 4253 | * |
| 4254 | * CR0_GUEST_HOST_MASK is already set in the original vmcs01 |
| 4255 | * (KVM doesn't change it); |
| 4256 | */ |
Sean Christopherson | fa71e95 | 2020-07-02 21:04:22 -0700 | [diff] [blame] | 4257 | vcpu->arch.cr0_guest_owned_bits = KVM_POSSIBLE_CR0_GUEST_BITS; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4258 | vmx_set_cr0(vcpu, vmcs12->host_cr0); |
| 4259 | |
| 4260 | /* Same as above - no reason to call set_cr4_guest_host_mask(). */ |
| 4261 | vcpu->arch.cr4_guest_owned_bits = ~vmcs_readl(CR4_GUEST_HOST_MASK); |
| 4262 | vmx_set_cr4(vcpu, vmcs12->host_cr4); |
| 4263 | |
| 4264 | nested_ept_uninit_mmu_context(vcpu); |
| 4265 | |
| 4266 | /* |
| 4267 | * Only PDPTE load can fail as the value of cr3 was checked on entry and |
| 4268 | * couldn't have changed. |
| 4269 | */ |
Maxim Levitsky | 0f85722 | 2021-06-07 12:02:00 +0300 | [diff] [blame] | 4270 | if (nested_vmx_load_cr3(vcpu, vmcs12->host_cr3, false, true, &ignored)) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4271 | nested_vmx_abort(vcpu, VMX_ABORT_LOAD_HOST_PDPTE_FAIL); |
| 4272 | |
Sean Christopherson | 50b265a | 2020-03-20 14:28:19 -0700 | [diff] [blame] | 4273 | nested_vmx_transition_tlb_flush(vcpu, vmcs12, false); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4274 | |
| 4275 | vmcs_write32(GUEST_SYSENTER_CS, vmcs12->host_ia32_sysenter_cs); |
| 4276 | vmcs_writel(GUEST_SYSENTER_ESP, vmcs12->host_ia32_sysenter_esp); |
| 4277 | vmcs_writel(GUEST_SYSENTER_EIP, vmcs12->host_ia32_sysenter_eip); |
| 4278 | vmcs_writel(GUEST_IDTR_BASE, vmcs12->host_idtr_base); |
| 4279 | vmcs_writel(GUEST_GDTR_BASE, vmcs12->host_gdtr_base); |
| 4280 | vmcs_write32(GUEST_IDTR_LIMIT, 0xFFFF); |
| 4281 | vmcs_write32(GUEST_GDTR_LIMIT, 0xFFFF); |
| 4282 | |
| 4283 | /* If not VM_EXIT_CLEAR_BNDCFGS, the L2 value propagates to L1. */ |
| 4284 | if (vmcs12->vm_exit_controls & VM_EXIT_CLEAR_BNDCFGS) |
| 4285 | vmcs_write64(GUEST_BNDCFGS, 0); |
| 4286 | |
| 4287 | if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PAT) { |
| 4288 | vmcs_write64(GUEST_IA32_PAT, vmcs12->host_ia32_pat); |
| 4289 | vcpu->arch.pat = vmcs12->host_ia32_pat; |
| 4290 | } |
| 4291 | if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL) |
Oliver Upton | d196842 | 2019-12-13 16:33:58 -0800 | [diff] [blame] | 4292 | WARN_ON_ONCE(kvm_set_msr(vcpu, MSR_CORE_PERF_GLOBAL_CTRL, |
| 4293 | vmcs12->host_ia32_perf_global_ctrl)); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4294 | |
| 4295 | /* Set L1 segment info according to Intel SDM |
| 4296 | 27.5.2 Loading Host Segment and Descriptor-Table Registers */ |
| 4297 | seg = (struct kvm_segment) { |
| 4298 | .base = 0, |
| 4299 | .limit = 0xFFFFFFFF, |
| 4300 | .selector = vmcs12->host_cs_selector, |
| 4301 | .type = 11, |
| 4302 | .present = 1, |
| 4303 | .s = 1, |
| 4304 | .g = 1 |
| 4305 | }; |
| 4306 | if (vmcs12->vm_exit_controls & VM_EXIT_HOST_ADDR_SPACE_SIZE) |
| 4307 | seg.l = 1; |
| 4308 | else |
| 4309 | seg.db = 1; |
Sean Christopherson | 816be9e | 2021-07-13 09:33:07 -0700 | [diff] [blame] | 4310 | __vmx_set_segment(vcpu, &seg, VCPU_SREG_CS); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4311 | seg = (struct kvm_segment) { |
| 4312 | .base = 0, |
| 4313 | .limit = 0xFFFFFFFF, |
| 4314 | .type = 3, |
| 4315 | .present = 1, |
| 4316 | .s = 1, |
| 4317 | .db = 1, |
| 4318 | .g = 1 |
| 4319 | }; |
| 4320 | seg.selector = vmcs12->host_ds_selector; |
Sean Christopherson | 816be9e | 2021-07-13 09:33:07 -0700 | [diff] [blame] | 4321 | __vmx_set_segment(vcpu, &seg, VCPU_SREG_DS); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4322 | seg.selector = vmcs12->host_es_selector; |
Sean Christopherson | 816be9e | 2021-07-13 09:33:07 -0700 | [diff] [blame] | 4323 | __vmx_set_segment(vcpu, &seg, VCPU_SREG_ES); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4324 | seg.selector = vmcs12->host_ss_selector; |
Sean Christopherson | 816be9e | 2021-07-13 09:33:07 -0700 | [diff] [blame] | 4325 | __vmx_set_segment(vcpu, &seg, VCPU_SREG_SS); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4326 | seg.selector = vmcs12->host_fs_selector; |
| 4327 | seg.base = vmcs12->host_fs_base; |
Sean Christopherson | 816be9e | 2021-07-13 09:33:07 -0700 | [diff] [blame] | 4328 | __vmx_set_segment(vcpu, &seg, VCPU_SREG_FS); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4329 | seg.selector = vmcs12->host_gs_selector; |
| 4330 | seg.base = vmcs12->host_gs_base; |
Sean Christopherson | 816be9e | 2021-07-13 09:33:07 -0700 | [diff] [blame] | 4331 | __vmx_set_segment(vcpu, &seg, VCPU_SREG_GS); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4332 | seg = (struct kvm_segment) { |
| 4333 | .base = vmcs12->host_tr_base, |
| 4334 | .limit = 0x67, |
| 4335 | .selector = vmcs12->host_tr_selector, |
| 4336 | .type = 11, |
| 4337 | .present = 1 |
| 4338 | }; |
Sean Christopherson | 816be9e | 2021-07-13 09:33:07 -0700 | [diff] [blame] | 4339 | __vmx_set_segment(vcpu, &seg, VCPU_SREG_TR); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4340 | |
Sean Christopherson | afc8de0 | 2021-07-13 09:32:40 -0700 | [diff] [blame] | 4341 | memset(&seg, 0, sizeof(seg)); |
| 4342 | seg.unusable = 1; |
Sean Christopherson | 816be9e | 2021-07-13 09:33:07 -0700 | [diff] [blame] | 4343 | __vmx_set_segment(vcpu, &seg, VCPU_SREG_LDTR); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4344 | |
| 4345 | kvm_set_dr(vcpu, 7, 0x400); |
| 4346 | vmcs_write64(GUEST_IA32_DEBUGCTL, 0); |
| 4347 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4348 | if (nested_vmx_load_msr(vcpu, vmcs12->vm_exit_msr_load_addr, |
| 4349 | vmcs12->vm_exit_msr_load_count)) |
| 4350 | nested_vmx_abort(vcpu, VMX_ABORT_LOAD_HOST_MSR_FAIL); |
Maxim Levitsky | dbab610 | 2021-09-13 17:09:54 +0300 | [diff] [blame] | 4351 | |
| 4352 | to_vmx(vcpu)->emulation_required = vmx_emulation_required(vcpu); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4353 | } |
| 4354 | |
| 4355 | static inline u64 nested_vmx_get_vmcs01_guest_efer(struct vcpu_vmx *vmx) |
| 4356 | { |
Sean Christopherson | eb3db1b | 2020-09-23 11:03:58 -0700 | [diff] [blame] | 4357 | struct vmx_uret_msr *efer_msr; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4358 | unsigned int i; |
| 4359 | |
| 4360 | if (vm_entry_controls_get(vmx) & VM_ENTRY_LOAD_IA32_EFER) |
| 4361 | return vmcs_read64(GUEST_IA32_EFER); |
| 4362 | |
| 4363 | if (cpu_has_load_ia32_efer()) |
| 4364 | return host_efer; |
| 4365 | |
| 4366 | for (i = 0; i < vmx->msr_autoload.guest.nr; ++i) { |
| 4367 | if (vmx->msr_autoload.guest.val[i].index == MSR_EFER) |
| 4368 | return vmx->msr_autoload.guest.val[i].value; |
| 4369 | } |
| 4370 | |
Sean Christopherson | d85a803 | 2020-09-23 11:04:06 -0700 | [diff] [blame] | 4371 | efer_msr = vmx_find_uret_msr(vmx, MSR_EFER); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4372 | if (efer_msr) |
| 4373 | return efer_msr->data; |
| 4374 | |
| 4375 | return host_efer; |
| 4376 | } |
| 4377 | |
| 4378 | static void nested_vmx_restore_host_state(struct kvm_vcpu *vcpu) |
| 4379 | { |
| 4380 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
| 4381 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 4382 | struct vmx_msr_entry g, h; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4383 | gpa_t gpa; |
| 4384 | u32 i, j; |
| 4385 | |
| 4386 | vcpu->arch.pat = vmcs_read64(GUEST_IA32_PAT); |
| 4387 | |
| 4388 | if (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS) { |
| 4389 | /* |
| 4390 | * L1's host DR7 is lost if KVM_GUESTDBG_USE_HW_BP is set |
| 4391 | * as vmcs01.GUEST_DR7 contains a userspace defined value |
| 4392 | * and vcpu->arch.dr7 is not squirreled away before the |
| 4393 | * nested VMENTER (not worth adding a variable in nested_vmx). |
| 4394 | */ |
| 4395 | if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP) |
| 4396 | kvm_set_dr(vcpu, 7, DR7_FIXED_1); |
| 4397 | else |
| 4398 | WARN_ON(kvm_set_dr(vcpu, 7, vmcs_readl(GUEST_DR7))); |
| 4399 | } |
| 4400 | |
| 4401 | /* |
| 4402 | * Note that calling vmx_set_{efer,cr0,cr4} is important as they |
| 4403 | * handle a variety of side effects to KVM's software model. |
| 4404 | */ |
| 4405 | vmx_set_efer(vcpu, nested_vmx_get_vmcs01_guest_efer(vmx)); |
| 4406 | |
Sean Christopherson | fa71e95 | 2020-07-02 21:04:22 -0700 | [diff] [blame] | 4407 | vcpu->arch.cr0_guest_owned_bits = KVM_POSSIBLE_CR0_GUEST_BITS; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4408 | vmx_set_cr0(vcpu, vmcs_readl(CR0_READ_SHADOW)); |
| 4409 | |
| 4410 | vcpu->arch.cr4_guest_owned_bits = ~vmcs_readl(CR4_GUEST_HOST_MASK); |
| 4411 | vmx_set_cr4(vcpu, vmcs_readl(CR4_READ_SHADOW)); |
| 4412 | |
| 4413 | nested_ept_uninit_mmu_context(vcpu); |
Sean Christopherson | f087a02 | 2019-06-07 11:55:34 -0700 | [diff] [blame] | 4414 | vcpu->arch.cr3 = vmcs_readl(GUEST_CR3); |
Sean Christopherson | cb3c1e2 | 2019-09-27 14:45:22 -0700 | [diff] [blame] | 4415 | kvm_register_mark_available(vcpu, VCPU_EXREG_CR3); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4416 | |
| 4417 | /* |
| 4418 | * Use ept_save_pdptrs(vcpu) to load the MMU's cached PDPTRs |
| 4419 | * from vmcs01 (if necessary). The PDPTRs are not loaded on |
| 4420 | * VMFail, like everything else we just need to ensure our |
| 4421 | * software model is up-to-date. |
| 4422 | */ |
Sean Christopherson | 9932b49 | 2020-04-15 13:34:50 -0700 | [diff] [blame] | 4423 | if (enable_ept && is_pae_paging(vcpu)) |
Sean Christopherson | f087a02 | 2019-06-07 11:55:34 -0700 | [diff] [blame] | 4424 | ept_save_pdptrs(vcpu); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4425 | |
| 4426 | kvm_mmu_reset_context(vcpu); |
| 4427 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4428 | /* |
| 4429 | * This nasty bit of open coding is a compromise between blindly |
| 4430 | * loading L1's MSRs using the exit load lists (incorrect emulation |
| 4431 | * of VMFail), leaving the nested VM's MSRs in the software model |
| 4432 | * (incorrect behavior) and snapshotting the modified MSRs (too |
| 4433 | * expensive since the lists are unbound by hardware). For each |
| 4434 | * MSR that was (prematurely) loaded from the nested VMEntry load |
| 4435 | * list, reload it from the exit load list if it exists and differs |
| 4436 | * from the guest value. The intent is to stuff host state as |
| 4437 | * silently as possible, not to fully process the exit load list. |
| 4438 | */ |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4439 | for (i = 0; i < vmcs12->vm_entry_msr_load_count; i++) { |
| 4440 | gpa = vmcs12->vm_entry_msr_load_addr + (i * sizeof(g)); |
| 4441 | if (kvm_vcpu_read_guest(vcpu, gpa, &g, sizeof(g))) { |
| 4442 | pr_debug_ratelimited( |
| 4443 | "%s read MSR index failed (%u, 0x%08llx)\n", |
| 4444 | __func__, i, gpa); |
| 4445 | goto vmabort; |
| 4446 | } |
| 4447 | |
| 4448 | for (j = 0; j < vmcs12->vm_exit_msr_load_count; j++) { |
| 4449 | gpa = vmcs12->vm_exit_msr_load_addr + (j * sizeof(h)); |
| 4450 | if (kvm_vcpu_read_guest(vcpu, gpa, &h, sizeof(h))) { |
| 4451 | pr_debug_ratelimited( |
| 4452 | "%s read MSR failed (%u, 0x%08llx)\n", |
| 4453 | __func__, j, gpa); |
| 4454 | goto vmabort; |
| 4455 | } |
| 4456 | if (h.index != g.index) |
| 4457 | continue; |
| 4458 | if (h.value == g.value) |
| 4459 | break; |
| 4460 | |
| 4461 | if (nested_vmx_load_msr_check(vcpu, &h)) { |
| 4462 | pr_debug_ratelimited( |
| 4463 | "%s check failed (%u, 0x%x, 0x%x)\n", |
| 4464 | __func__, j, h.index, h.reserved); |
| 4465 | goto vmabort; |
| 4466 | } |
| 4467 | |
Sean Christopherson | f20935d | 2019-09-05 14:22:54 -0700 | [diff] [blame] | 4468 | if (kvm_set_msr(vcpu, h.index, h.value)) { |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4469 | pr_debug_ratelimited( |
| 4470 | "%s WRMSR failed (%u, 0x%x, 0x%llx)\n", |
| 4471 | __func__, j, h.index, h.value); |
| 4472 | goto vmabort; |
| 4473 | } |
| 4474 | } |
| 4475 | } |
| 4476 | |
| 4477 | return; |
| 4478 | |
| 4479 | vmabort: |
| 4480 | nested_vmx_abort(vcpu, VMX_ABORT_LOAD_HOST_MSR_FAIL); |
| 4481 | } |
| 4482 | |
| 4483 | /* |
| 4484 | * Emulate an exit from nested guest (L2) to L1, i.e., prepare to run L1 |
| 4485 | * and modify vmcs12 to make it see what it would expect to see there if |
| 4486 | * L2 was its real guest. Must only be called when in L2 (is_guest_mode()) |
| 4487 | */ |
Sean Christopherson | 4dcefa3 | 2020-04-15 10:55:18 -0700 | [diff] [blame] | 4488 | void nested_vmx_vmexit(struct kvm_vcpu *vcpu, u32 vm_exit_reason, |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4489 | u32 exit_intr_info, unsigned long exit_qualification) |
| 4490 | { |
| 4491 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 4492 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
| 4493 | |
| 4494 | /* trying to cancel vmlaunch/vmresume is a bug */ |
| 4495 | WARN_ON_ONCE(vmx->nested.nested_run_pending); |
| 4496 | |
Sean Christopherson | cb6a32c | 2021-03-02 09:45:14 -0800 | [diff] [blame] | 4497 | /* Similarly, triple faults in L2 should never escape. */ |
| 4498 | WARN_ON_ONCE(kvm_check_request(KVM_REQ_TRIPLE_FAULT, vcpu)); |
| 4499 | |
Vitaly Kuznetsov | f5c7e84 | 2021-05-03 17:08:51 +0200 | [diff] [blame] | 4500 | if (kvm_check_request(KVM_REQ_GET_NESTED_STATE_PAGES, vcpu)) { |
| 4501 | /* |
| 4502 | * KVM_REQ_GET_NESTED_STATE_PAGES is also used to map |
| 4503 | * Enlightened VMCS after migration and we still need to |
| 4504 | * do that when something is forcing L2->L1 exit prior to |
| 4505 | * the first L2 run. |
| 4506 | */ |
| 4507 | (void)nested_get_evmcs_page(vcpu); |
| 4508 | } |
Maxim Levitsky | f2c7ef3 | 2021-01-07 11:38:51 +0200 | [diff] [blame] | 4509 | |
Sean Christopherson | 40e5f908 | 2021-11-25 01:49:43 +0000 | [diff] [blame] | 4510 | /* Service pending TLB flush requests for L2 before switching to L1. */ |
| 4511 | kvm_service_local_tlb_flush_requests(vcpu); |
Sean Christopherson | eeeb4f6 | 2020-03-20 14:28:20 -0700 | [diff] [blame] | 4512 | |
Peter Shier | 43fea4e | 2020-08-20 16:05:45 -0700 | [diff] [blame] | 4513 | /* |
| 4514 | * VCPU_EXREG_PDPTR will be clobbered in arch/x86/kvm/vmx/vmx.h between |
| 4515 | * now and the new vmentry. Ensure that the VMCS02 PDPTR fields are |
| 4516 | * up-to-date before switching to L1. |
| 4517 | */ |
| 4518 | if (enable_ept && is_pae_paging(vcpu)) |
| 4519 | vmx_ept_load_pdptrs(vcpu); |
| 4520 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4521 | leave_guest_mode(vcpu); |
| 4522 | |
Paolo Bonzini | b4b65b5 | 2019-01-29 19:12:35 +0100 | [diff] [blame] | 4523 | if (nested_cpu_has_preemption_timer(vmcs12)) |
| 4524 | hrtimer_cancel(&to_vmx(vcpu)->nested.preemption_timer); |
| 4525 | |
Ilias Stamatis | d041b5e | 2021-05-26 19:44:17 +0100 | [diff] [blame] | 4526 | if (nested_cpu_has(vmcs12, CPU_BASED_USE_TSC_OFFSETTING)) { |
| 4527 | vcpu->arch.tsc_offset = vcpu->arch.l1_tsc_offset; |
| 4528 | if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_TSC_SCALING)) |
| 4529 | vcpu->arch.tsc_scaling_ratio = vcpu->arch.l1_tsc_scaling_ratio; |
| 4530 | } |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4531 | |
| 4532 | if (likely(!vmx->fail)) { |
Sean Christopherson | 3731905ef | 2019-05-07 08:36:27 -0700 | [diff] [blame] | 4533 | sync_vmcs02_to_vmcs12(vcpu, vmcs12); |
Sean Christopherson | f4f8316 | 2019-05-07 08:36:26 -0700 | [diff] [blame] | 4534 | |
Sean Christopherson | 4dcefa3 | 2020-04-15 10:55:18 -0700 | [diff] [blame] | 4535 | if (vm_exit_reason != -1) |
| 4536 | prepare_vmcs12(vcpu, vmcs12, vm_exit_reason, |
| 4537 | exit_intr_info, exit_qualification); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4538 | |
| 4539 | /* |
Sean Christopherson | 3731905ef | 2019-05-07 08:36:27 -0700 | [diff] [blame] | 4540 | * Must happen outside of sync_vmcs02_to_vmcs12() as it will |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4541 | * also be used to capture vmcs12 cache as part of |
| 4542 | * capturing nVMX state for snapshot (migration). |
| 4543 | * |
| 4544 | * Otherwise, this flush will dirty guest memory at a |
| 4545 | * point it is already assumed by user-space to be |
| 4546 | * immutable. |
| 4547 | */ |
| 4548 | nested_flush_cached_shadow_vmcs12(vcpu, vmcs12); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4549 | } else { |
| 4550 | /* |
| 4551 | * The only expected VM-instruction error is "VM entry with |
| 4552 | * invalid control field(s)." Anything else indicates a |
| 4553 | * problem with L0. And we should never get here with a |
| 4554 | * VMFail of any type if early consistency checks are enabled. |
| 4555 | */ |
| 4556 | WARN_ON_ONCE(vmcs_read32(VM_INSTRUCTION_ERROR) != |
| 4557 | VMXERR_ENTRY_INVALID_CONTROL_FIELD); |
| 4558 | WARN_ON_ONCE(nested_early_check); |
| 4559 | } |
| 4560 | |
| 4561 | vmx_switch_vmcs(vcpu, &vmx->vmcs01); |
| 4562 | |
| 4563 | /* Update any VMCS fields that might have changed while L2 ran */ |
| 4564 | vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, vmx->msr_autoload.host.nr); |
| 4565 | vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, vmx->msr_autoload.guest.nr); |
| 4566 | vmcs_write64(TSC_OFFSET, vcpu->arch.tsc_offset); |
Ilias Stamatis | 1ab9287 | 2021-06-07 11:54:38 +0100 | [diff] [blame] | 4567 | if (kvm_has_tsc_control) |
| 4568 | vmcs_write64(TSC_MULTIPLIER, vcpu->arch.tsc_scaling_ratio); |
| 4569 | |
Liran Alon | 02d496cf | 2019-11-11 14:30:55 +0200 | [diff] [blame] | 4570 | if (vmx->nested.l1_tpr_threshold != -1) |
| 4571 | vmcs_write32(TPR_THRESHOLD, vmx->nested.l1_tpr_threshold); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4572 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4573 | if (vmx->nested.change_vmcs01_virtual_apic_mode) { |
| 4574 | vmx->nested.change_vmcs01_virtual_apic_mode = false; |
| 4575 | vmx_set_virtual_apic_mode(vcpu); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4576 | } |
| 4577 | |
Makarand Sonare | a85863c | 2021-02-12 16:50:12 -0800 | [diff] [blame] | 4578 | if (vmx->nested.update_vmcs01_cpu_dirty_logging) { |
| 4579 | vmx->nested.update_vmcs01_cpu_dirty_logging = false; |
| 4580 | vmx_update_cpu_dirty_logging(vcpu); |
| 4581 | } |
| 4582 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4583 | /* Unpin physical memory we referred to in vmcs02 */ |
| 4584 | if (vmx->nested.apic_access_page) { |
Liran Alon | b11494b | 2019-11-21 00:31:47 +0200 | [diff] [blame] | 4585 | kvm_release_page_clean(vmx->nested.apic_access_page); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4586 | vmx->nested.apic_access_page = NULL; |
| 4587 | } |
KarimAllah Ahmed | 96c66e8 | 2019-01-31 21:24:37 +0100 | [diff] [blame] | 4588 | kvm_vcpu_unmap(vcpu, &vmx->nested.virtual_apic_map, true); |
KarimAllah Ahmed | 3278e04 | 2019-01-31 21:24:38 +0100 | [diff] [blame] | 4589 | kvm_vcpu_unmap(vcpu, &vmx->nested.pi_desc_map, true); |
| 4590 | vmx->nested.pi_desc = NULL; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4591 | |
Sean Christopherson | 1196cb9 | 2020-03-20 14:28:23 -0700 | [diff] [blame] | 4592 | if (vmx->nested.reload_vmcs01_apic_access_page) { |
| 4593 | vmx->nested.reload_vmcs01_apic_access_page = false; |
| 4594 | kvm_make_request(KVM_REQ_APIC_PAGE_RELOAD, vcpu); |
| 4595 | } |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4596 | |
Sean Christopherson | 4dcefa3 | 2020-04-15 10:55:18 -0700 | [diff] [blame] | 4597 | if ((vm_exit_reason != -1) && |
Vitaly Kuznetsov | 1e9dfbd | 2021-05-26 15:20:16 +0200 | [diff] [blame] | 4598 | (enable_shadow_vmcs || evmptr_is_valid(vmx->nested.hv_evmcs_vmptr))) |
Sean Christopherson | 3731905ef | 2019-05-07 08:36:27 -0700 | [diff] [blame] | 4599 | vmx->nested.need_vmcs12_to_shadow_sync = true; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4600 | |
| 4601 | /* in case we halted in L2 */ |
| 4602 | vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE; |
| 4603 | |
| 4604 | if (likely(!vmx->fail)) { |
Sean Christopherson | 4dcefa3 | 2020-04-15 10:55:18 -0700 | [diff] [blame] | 4605 | if ((u16)vm_exit_reason == EXIT_REASON_EXTERNAL_INTERRUPT && |
Sean Christopherson | a1c77ab | 2020-03-02 22:27:35 -0800 | [diff] [blame] | 4606 | nested_exit_intr_ack_set(vcpu)) { |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4607 | int irq = kvm_cpu_get_interrupt(vcpu); |
| 4608 | WARN_ON(irq < 0); |
| 4609 | vmcs12->vm_exit_intr_info = irq | |
| 4610 | INTR_INFO_VALID_MASK | INTR_TYPE_EXT_INTR; |
| 4611 | } |
| 4612 | |
Sean Christopherson | 4dcefa3 | 2020-04-15 10:55:18 -0700 | [diff] [blame] | 4613 | if (vm_exit_reason != -1) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4614 | trace_kvm_nested_vmexit_inject(vmcs12->vm_exit_reason, |
| 4615 | vmcs12->exit_qualification, |
| 4616 | vmcs12->idt_vectoring_info_field, |
| 4617 | vmcs12->vm_exit_intr_info, |
| 4618 | vmcs12->vm_exit_intr_error_code, |
| 4619 | KVM_ISA_VMX); |
| 4620 | |
| 4621 | load_vmcs12_host_state(vcpu, vmcs12); |
| 4622 | |
| 4623 | return; |
| 4624 | } |
| 4625 | |
| 4626 | /* |
| 4627 | * After an early L2 VM-entry failure, we're now back |
| 4628 | * in L1 which thinks it just finished a VMLAUNCH or |
| 4629 | * VMRESUME instruction, so we need to set the failure |
| 4630 | * flag and the VM-instruction error field of the VMCS |
| 4631 | * accordingly, and skip the emulated instruction. |
| 4632 | */ |
Sean Christopherson | b2656e4 | 2020-06-08 18:56:07 -0700 | [diff] [blame] | 4633 | (void)nested_vmx_fail(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4634 | |
| 4635 | /* |
| 4636 | * Restore L1's host state to KVM's software model. We're here |
| 4637 | * because a consistency check was caught by hardware, which |
| 4638 | * means some amount of guest state has been propagated to KVM's |
| 4639 | * model and needs to be unwound to the host's state. |
| 4640 | */ |
| 4641 | nested_vmx_restore_host_state(vcpu); |
| 4642 | |
| 4643 | vmx->fail = 0; |
| 4644 | } |
| 4645 | |
Sean Christopherson | cb6a32c | 2021-03-02 09:45:14 -0800 | [diff] [blame] | 4646 | static void nested_vmx_triple_fault(struct kvm_vcpu *vcpu) |
| 4647 | { |
| 4648 | nested_vmx_vmexit(vcpu, EXIT_REASON_TRIPLE_FAULT, 0, 0); |
| 4649 | } |
| 4650 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4651 | /* |
| 4652 | * Decode the memory-address operand of a vmx instruction, as recorded on an |
| 4653 | * exit caused by such an instruction (run by a guest hypervisor). |
| 4654 | * 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] | 4655 | * #UD, #GP, or #SS. |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4656 | */ |
| 4657 | 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] | 4658 | u32 vmx_instruction_info, bool wr, int len, gva_t *ret) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4659 | { |
| 4660 | gva_t off; |
| 4661 | bool exn; |
| 4662 | struct kvm_segment s; |
| 4663 | |
| 4664 | /* |
| 4665 | * According to Vol. 3B, "Information for VM Exits Due to Instruction |
| 4666 | * Execution", on an exit, vmx_instruction_info holds most of the |
| 4667 | * addressing components of the operand. Only the displacement part |
| 4668 | * is put in exit_qualification (see 3B, "Basic VM-Exit Information"). |
| 4669 | * For how an actual address is calculated from all these components, |
| 4670 | * refer to Vol. 1, "Operand Addressing". |
| 4671 | */ |
| 4672 | int scaling = vmx_instruction_info & 3; |
| 4673 | int addr_size = (vmx_instruction_info >> 7) & 7; |
| 4674 | bool is_reg = vmx_instruction_info & (1u << 10); |
| 4675 | int seg_reg = (vmx_instruction_info >> 15) & 7; |
| 4676 | int index_reg = (vmx_instruction_info >> 18) & 0xf; |
| 4677 | bool index_is_valid = !(vmx_instruction_info & (1u << 22)); |
| 4678 | int base_reg = (vmx_instruction_info >> 23) & 0xf; |
| 4679 | bool base_is_valid = !(vmx_instruction_info & (1u << 27)); |
| 4680 | |
| 4681 | if (is_reg) { |
| 4682 | kvm_queue_exception(vcpu, UD_VECTOR); |
| 4683 | return 1; |
| 4684 | } |
| 4685 | |
| 4686 | /* Addr = segment_base + offset */ |
| 4687 | /* offset = base + [index * scale] + displacement */ |
| 4688 | off = exit_qualification; /* holds the displacement */ |
Sean Christopherson | 946c522 | 2019-01-23 14:39:23 -0800 | [diff] [blame] | 4689 | if (addr_size == 1) |
| 4690 | off = (gva_t)sign_extend64(off, 31); |
| 4691 | else if (addr_size == 0) |
| 4692 | off = (gva_t)sign_extend64(off, 15); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4693 | if (base_is_valid) |
| 4694 | off += kvm_register_read(vcpu, base_reg); |
| 4695 | if (index_is_valid) |
Miaohe Lin | e630269 | 2020-02-15 10:44:22 +0800 | [diff] [blame] | 4696 | off += kvm_register_read(vcpu, index_reg) << scaling; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4697 | vmx_get_segment(vcpu, &s, seg_reg); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4698 | |
Sean Christopherson | 8570f9e | 2019-01-23 14:39:24 -0800 | [diff] [blame] | 4699 | /* |
| 4700 | * The effective address, i.e. @off, of a memory operand is truncated |
| 4701 | * based on the address size of the instruction. Note that this is |
| 4702 | * the *effective address*, i.e. the address prior to accounting for |
| 4703 | * the segment's base. |
| 4704 | */ |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4705 | if (addr_size == 1) /* 32 bit */ |
Sean Christopherson | 8570f9e | 2019-01-23 14:39:24 -0800 | [diff] [blame] | 4706 | off &= 0xffffffff; |
| 4707 | else if (addr_size == 0) /* 16 bit */ |
| 4708 | off &= 0xffff; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4709 | |
| 4710 | /* Checks for #GP/#SS exceptions. */ |
| 4711 | exn = false; |
| 4712 | if (is_long_mode(vcpu)) { |
Sean Christopherson | 8570f9e | 2019-01-23 14:39:24 -0800 | [diff] [blame] | 4713 | /* |
| 4714 | * The virtual/linear address is never truncated in 64-bit |
| 4715 | * mode, e.g. a 32-bit address size can yield a 64-bit virtual |
| 4716 | * address when using FS/GS with a non-zero base. |
| 4717 | */ |
Liran Alon | 6694e48 | 2019-07-15 18:47:44 +0300 | [diff] [blame] | 4718 | if (seg_reg == VCPU_SREG_FS || seg_reg == VCPU_SREG_GS) |
| 4719 | *ret = s.base + off; |
| 4720 | else |
| 4721 | *ret = off; |
Sean Christopherson | 8570f9e | 2019-01-23 14:39:24 -0800 | [diff] [blame] | 4722 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4723 | /* Long mode: #GP(0)/#SS(0) if the memory address is in a |
| 4724 | * non-canonical form. This is the only check on the memory |
| 4725 | * destination for long mode! |
| 4726 | */ |
| 4727 | exn = is_noncanonical_address(*ret, vcpu); |
Paolo Bonzini | e0dfacb | 2019-01-30 17:25:38 +0100 | [diff] [blame] | 4728 | } else { |
Sean Christopherson | 8570f9e | 2019-01-23 14:39:24 -0800 | [diff] [blame] | 4729 | /* |
| 4730 | * When not in long mode, the virtual/linear address is |
| 4731 | * unconditionally truncated to 32 bits regardless of the |
| 4732 | * address size. |
| 4733 | */ |
| 4734 | *ret = (s.base + off) & 0xffffffff; |
| 4735 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4736 | /* Protected mode: apply checks for segment validity in the |
| 4737 | * following order: |
| 4738 | * - segment type check (#GP(0) may be thrown) |
| 4739 | * - usability check (#GP(0)/#SS(0)) |
| 4740 | * - limit check (#GP(0)/#SS(0)) |
| 4741 | */ |
| 4742 | if (wr) |
| 4743 | /* #GP(0) if the destination operand is located in a |
| 4744 | * read-only data segment or any code segment. |
| 4745 | */ |
| 4746 | exn = ((s.type & 0xa) == 0 || (s.type & 8)); |
| 4747 | else |
| 4748 | /* #GP(0) if the source operand is located in an |
| 4749 | * execute-only code segment |
| 4750 | */ |
| 4751 | exn = ((s.type & 0xa) == 8); |
| 4752 | if (exn) { |
| 4753 | kvm_queue_exception_e(vcpu, GP_VECTOR, 0); |
| 4754 | return 1; |
| 4755 | } |
| 4756 | /* Protected mode: #GP(0)/#SS(0) if the segment is unusable. |
| 4757 | */ |
| 4758 | exn = (s.unusable != 0); |
Sean Christopherson | 34333cc | 2019-01-23 14:39:25 -0800 | [diff] [blame] | 4759 | |
| 4760 | /* |
| 4761 | * Protected mode: #GP(0)/#SS(0) if the memory operand is |
| 4762 | * outside the segment limit. All CPUs that support VMX ignore |
| 4763 | * limit checks for flat segments, i.e. segments with base==0, |
| 4764 | * limit==0xffffffff and of type expand-up data or code. |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4765 | */ |
Sean Christopherson | 34333cc | 2019-01-23 14:39:25 -0800 | [diff] [blame] | 4766 | if (!(s.base == 0 && s.limit == 0xffffffff && |
| 4767 | ((s.type & 8) || !(s.type & 4)))) |
Eugene Korenevsky | fdb2861 | 2019-06-06 00:19:16 +0300 | [diff] [blame] | 4768 | exn = exn || ((u64)off + len - 1 > s.limit); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4769 | } |
| 4770 | if (exn) { |
| 4771 | kvm_queue_exception_e(vcpu, |
| 4772 | seg_reg == VCPU_SREG_SS ? |
| 4773 | SS_VECTOR : GP_VECTOR, |
| 4774 | 0); |
| 4775 | return 1; |
| 4776 | } |
| 4777 | |
| 4778 | return 0; |
| 4779 | } |
| 4780 | |
Oliver Upton | 03a8871a | 2019-11-13 16:17:20 -0800 | [diff] [blame] | 4781 | void nested_vmx_pmu_entry_exit_ctls_update(struct kvm_vcpu *vcpu) |
| 4782 | { |
| 4783 | struct vcpu_vmx *vmx; |
| 4784 | |
| 4785 | if (!nested_vmx_allowed(vcpu)) |
| 4786 | return; |
| 4787 | |
| 4788 | vmx = to_vmx(vcpu); |
Sean Christopherson | afaf0b2 | 2020-03-21 13:26:00 -0700 | [diff] [blame] | 4789 | 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] | 4790 | vmx->nested.msrs.entry_ctls_high |= |
| 4791 | VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL; |
| 4792 | vmx->nested.msrs.exit_ctls_high |= |
| 4793 | VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL; |
| 4794 | } else { |
| 4795 | vmx->nested.msrs.entry_ctls_high &= |
| 4796 | ~VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL; |
| 4797 | vmx->nested.msrs.exit_ctls_high &= |
Chenyi Qiang | c6b177a | 2020-08-28 16:56:21 +0800 | [diff] [blame] | 4798 | ~VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL; |
Oliver Upton | 03a8871a | 2019-11-13 16:17:20 -0800 | [diff] [blame] | 4799 | } |
| 4800 | } |
| 4801 | |
Vitaly Kuznetsov | 7a35e51 | 2020-06-05 13:59:05 +0200 | [diff] [blame] | 4802 | static int nested_vmx_get_vmptr(struct kvm_vcpu *vcpu, gpa_t *vmpointer, |
| 4803 | int *ret) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4804 | { |
| 4805 | gva_t gva; |
| 4806 | struct x86_exception e; |
Vitaly Kuznetsov | 7a35e51 | 2020-06-05 13:59:05 +0200 | [diff] [blame] | 4807 | int r; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4808 | |
Sean Christopherson | 5addc23 | 2020-04-15 13:34:53 -0700 | [diff] [blame] | 4809 | if (get_vmx_mem_address(vcpu, vmx_get_exit_qual(vcpu), |
Eugene Korenevsky | fdb2861 | 2019-06-06 00:19:16 +0300 | [diff] [blame] | 4810 | vmcs_read32(VMX_INSTRUCTION_INFO), false, |
Vitaly Kuznetsov | 7a35e51 | 2020-06-05 13:59:05 +0200 | [diff] [blame] | 4811 | sizeof(*vmpointer), &gva)) { |
| 4812 | *ret = 1; |
| 4813 | return -EINVAL; |
| 4814 | } |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4815 | |
Vitaly Kuznetsov | 7a35e51 | 2020-06-05 13:59:05 +0200 | [diff] [blame] | 4816 | r = kvm_read_guest_virt(vcpu, gva, vmpointer, sizeof(*vmpointer), &e); |
| 4817 | if (r != X86EMUL_CONTINUE) { |
Babu Moger | 3f3393b | 2020-09-11 14:29:05 -0500 | [diff] [blame] | 4818 | *ret = kvm_handle_memory_failure(vcpu, r, &e); |
Vitaly Kuznetsov | 7a35e51 | 2020-06-05 13:59:05 +0200 | [diff] [blame] | 4819 | return -EINVAL; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4820 | } |
| 4821 | |
| 4822 | return 0; |
| 4823 | } |
| 4824 | |
| 4825 | /* |
| 4826 | * Allocate a shadow VMCS and associate it with the currently loaded |
| 4827 | * VMCS, unless such a shadow VMCS already exists. The newly allocated |
| 4828 | * VMCS is also VMCLEARed, so that it is ready for use. |
| 4829 | */ |
| 4830 | static struct vmcs *alloc_shadow_vmcs(struct kvm_vcpu *vcpu) |
| 4831 | { |
| 4832 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 4833 | struct loaded_vmcs *loaded_vmcs = vmx->loaded_vmcs; |
| 4834 | |
| 4835 | /* |
| 4836 | * We should allocate a shadow vmcs for vmcs01 only when L1 |
| 4837 | * executes VMXON and free it when L1 executes VMXOFF. |
| 4838 | * As it is invalid to execute VMXON twice, we shouldn't reach |
| 4839 | * here when vmcs01 already have an allocated shadow vmcs. |
| 4840 | */ |
| 4841 | WARN_ON(loaded_vmcs == &vmx->vmcs01 && loaded_vmcs->shadow_vmcs); |
| 4842 | |
| 4843 | if (!loaded_vmcs->shadow_vmcs) { |
| 4844 | loaded_vmcs->shadow_vmcs = alloc_vmcs(true); |
| 4845 | if (loaded_vmcs->shadow_vmcs) |
| 4846 | vmcs_clear(loaded_vmcs->shadow_vmcs); |
| 4847 | } |
| 4848 | return loaded_vmcs->shadow_vmcs; |
| 4849 | } |
| 4850 | |
| 4851 | static int enter_vmx_operation(struct kvm_vcpu *vcpu) |
| 4852 | { |
| 4853 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 4854 | int r; |
| 4855 | |
| 4856 | r = alloc_loaded_vmcs(&vmx->nested.vmcs02); |
| 4857 | if (r < 0) |
| 4858 | goto out_vmcs02; |
| 4859 | |
Ben Gardon | 4183683 | 2019-02-11 11:02:52 -0800 | [diff] [blame] | 4860 | vmx->nested.cached_vmcs12 = kzalloc(VMCS12_SIZE, GFP_KERNEL_ACCOUNT); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4861 | if (!vmx->nested.cached_vmcs12) |
| 4862 | goto out_cached_vmcs12; |
| 4863 | |
Paolo Bonzini | 8503fea | 2021-11-22 18:20:16 -0500 | [diff] [blame] | 4864 | vmx->nested.shadow_vmcs12_cache.gpa = INVALID_GPA; |
Ben Gardon | 4183683 | 2019-02-11 11:02:52 -0800 | [diff] [blame] | 4865 | vmx->nested.cached_shadow_vmcs12 = kzalloc(VMCS12_SIZE, GFP_KERNEL_ACCOUNT); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4866 | if (!vmx->nested.cached_shadow_vmcs12) |
| 4867 | goto out_cached_shadow_vmcs12; |
| 4868 | |
| 4869 | if (enable_shadow_vmcs && !alloc_shadow_vmcs(vcpu)) |
| 4870 | goto out_shadow_vmcs; |
| 4871 | |
| 4872 | hrtimer_init(&vmx->nested.preemption_timer, CLOCK_MONOTONIC, |
Jim Mattson | ada0098 | 2020-05-08 13:36:42 -0700 | [diff] [blame] | 4873 | HRTIMER_MODE_ABS_PINNED); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4874 | vmx->nested.preemption_timer.function = vmx_preemption_timer_fn; |
| 4875 | |
| 4876 | vmx->nested.vpid02 = allocate_vpid(); |
| 4877 | |
| 4878 | vmx->nested.vmcs02_initialized = false; |
| 4879 | vmx->nested.vmxon = true; |
Luwei Kang | ee85dec | 2018-10-24 16:05:16 +0800 | [diff] [blame] | 4880 | |
Sean Christopherson | 2ef7619 | 2020-03-02 15:56:22 -0800 | [diff] [blame] | 4881 | if (vmx_pt_mode_is_host_guest()) { |
Luwei Kang | ee85dec | 2018-10-24 16:05:16 +0800 | [diff] [blame] | 4882 | vmx->pt_desc.guest.ctl = 0; |
Aaron Lewis | 476c9bd | 2020-09-25 16:34:18 +0200 | [diff] [blame] | 4883 | pt_update_intercept_for_msr(vcpu); |
Luwei Kang | ee85dec | 2018-10-24 16:05:16 +0800 | [diff] [blame] | 4884 | } |
| 4885 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4886 | return 0; |
| 4887 | |
| 4888 | out_shadow_vmcs: |
| 4889 | kfree(vmx->nested.cached_shadow_vmcs12); |
| 4890 | |
| 4891 | out_cached_shadow_vmcs12: |
| 4892 | kfree(vmx->nested.cached_vmcs12); |
| 4893 | |
| 4894 | out_cached_vmcs12: |
| 4895 | free_loaded_vmcs(&vmx->nested.vmcs02); |
| 4896 | |
| 4897 | out_vmcs02: |
| 4898 | return -ENOMEM; |
| 4899 | } |
| 4900 | |
Yu Zhang | ed7023a | 2021-09-09 01:17:31 +0800 | [diff] [blame] | 4901 | /* Emulate the VMXON instruction. */ |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4902 | static int handle_vmon(struct kvm_vcpu *vcpu) |
| 4903 | { |
| 4904 | int ret; |
| 4905 | gpa_t vmptr; |
KarimAllah Ahmed | 2e40893 | 2019-01-31 21:24:31 +0100 | [diff] [blame] | 4906 | uint32_t revision; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4907 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Sean Christopherson | 32ad73d | 2019-12-20 20:44:55 -0800 | [diff] [blame] | 4908 | const u64 VMXON_NEEDED_FEATURES = FEAT_CTL_LOCKED |
| 4909 | | FEAT_CTL_VMX_ENABLED_OUTSIDE_SMX; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4910 | |
| 4911 | /* |
| 4912 | * The Intel VMX Instruction Reference lists a bunch of bits that are |
| 4913 | * prerequisite to running VMXON, most notably cr4.VMXE must be set to |
Sean Christopherson | c2fe3cd | 2020-10-06 18:44:15 -0700 | [diff] [blame] | 4914 | * 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] | 4915 | * Otherwise, we should fail with #UD. But most faulting conditions |
| 4916 | * have already been checked by hardware, prior to the VM-exit for |
| 4917 | * VMXON. We do test guest cr4.VMXE because processor CR4 always has |
| 4918 | * that bit set to 1 in non-root mode. |
| 4919 | */ |
| 4920 | if (!kvm_read_cr4_bits(vcpu, X86_CR4_VMXE)) { |
| 4921 | kvm_queue_exception(vcpu, UD_VECTOR); |
| 4922 | return 1; |
| 4923 | } |
| 4924 | |
| 4925 | /* CPL=0 must be checked manually. */ |
| 4926 | if (vmx_get_cpl(vcpu)) { |
| 4927 | kvm_inject_gp(vcpu, 0); |
| 4928 | return 1; |
| 4929 | } |
| 4930 | |
| 4931 | if (vmx->nested.vmxon) |
Sean Christopherson | b2656e4 | 2020-06-08 18:56:07 -0700 | [diff] [blame] | 4932 | return nested_vmx_fail(vcpu, VMXERR_VMXON_IN_VMX_ROOT_OPERATION); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4933 | |
| 4934 | if ((vmx->msr_ia32_feature_control & VMXON_NEEDED_FEATURES) |
| 4935 | != VMXON_NEEDED_FEATURES) { |
| 4936 | kvm_inject_gp(vcpu, 0); |
| 4937 | return 1; |
| 4938 | } |
| 4939 | |
Vitaly Kuznetsov | 7a35e51 | 2020-06-05 13:59:05 +0200 | [diff] [blame] | 4940 | if (nested_vmx_get_vmptr(vcpu, &vmptr, &ret)) |
| 4941 | return ret; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4942 | |
| 4943 | /* |
| 4944 | * SDM 3: 24.11.5 |
| 4945 | * The first 4 bytes of VMXON region contain the supported |
| 4946 | * VMCS revision identifier |
| 4947 | * |
| 4948 | * Note - IA32_VMX_BASIC[48] will never be 1 for the nested case; |
| 4949 | * which replaces physical address width with 32 |
| 4950 | */ |
KarimAllah Ahmed | e0bf266 | 2019-01-31 21:24:43 +0100 | [diff] [blame] | 4951 | if (!page_address_valid(vcpu, vmptr)) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4952 | return nested_vmx_failInvalid(vcpu); |
| 4953 | |
KarimAllah Ahmed | 2e40893 | 2019-01-31 21:24:31 +0100 | [diff] [blame] | 4954 | if (kvm_read_guest(vcpu->kvm, vmptr, &revision, sizeof(revision)) || |
| 4955 | revision != VMCS12_REVISION) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4956 | return nested_vmx_failInvalid(vcpu); |
| 4957 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4958 | vmx->nested.vmxon_ptr = vmptr; |
| 4959 | ret = enter_vmx_operation(vcpu); |
| 4960 | if (ret) |
| 4961 | return ret; |
| 4962 | |
| 4963 | return nested_vmx_succeed(vcpu); |
| 4964 | } |
| 4965 | |
| 4966 | static inline void nested_release_vmcs12(struct kvm_vcpu *vcpu) |
| 4967 | { |
| 4968 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 4969 | |
Yu Zhang | 64c7850 | 2021-09-30 01:51:53 +0800 | [diff] [blame] | 4970 | if (vmx->nested.current_vmptr == INVALID_GPA) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4971 | return; |
| 4972 | |
Sean Christopherson | 7952d76 | 2019-05-07 08:36:29 -0700 | [diff] [blame] | 4973 | copy_vmcs02_to_vmcs12_rare(vcpu, get_vmcs12(vcpu)); |
| 4974 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4975 | if (enable_shadow_vmcs) { |
| 4976 | /* copy to memory all shadowed fields in case |
| 4977 | they were modified */ |
| 4978 | copy_shadow_to_vmcs12(vmx); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4979 | vmx_disable_shadow_vmcs(vmx); |
| 4980 | } |
| 4981 | vmx->nested.posted_intr_nv = -1; |
| 4982 | |
| 4983 | /* Flush VMCS12 to guest memory */ |
| 4984 | kvm_vcpu_write_guest_page(vcpu, |
| 4985 | vmx->nested.current_vmptr >> PAGE_SHIFT, |
| 4986 | vmx->nested.cached_vmcs12, 0, VMCS12_SIZE); |
| 4987 | |
| 4988 | kvm_mmu_free_roots(vcpu, &vcpu->arch.guest_mmu, KVM_MMU_ROOTS_ALL); |
| 4989 | |
Yu Zhang | 64c7850 | 2021-09-30 01:51:53 +0800 | [diff] [blame] | 4990 | vmx->nested.current_vmptr = INVALID_GPA; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4991 | } |
| 4992 | |
| 4993 | /* Emulate the VMXOFF instruction */ |
| 4994 | static int handle_vmoff(struct kvm_vcpu *vcpu) |
| 4995 | { |
| 4996 | if (!nested_vmx_check_permission(vcpu)) |
| 4997 | return 1; |
Liran Alon | 4b9852f | 2019-08-26 13:24:49 +0300 | [diff] [blame] | 4998 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4999 | free_nested(vcpu); |
Liran Alon | 4b9852f | 2019-08-26 13:24:49 +0300 | [diff] [blame] | 5000 | |
| 5001 | /* Process a latched INIT during time CPU was in VMX operation */ |
| 5002 | kvm_make_request(KVM_REQ_EVENT, vcpu); |
| 5003 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5004 | return nested_vmx_succeed(vcpu); |
| 5005 | } |
| 5006 | |
| 5007 | /* Emulate the VMCLEAR instruction */ |
| 5008 | static int handle_vmclear(struct kvm_vcpu *vcpu) |
| 5009 | { |
| 5010 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 5011 | u32 zero = 0; |
| 5012 | gpa_t vmptr; |
Vitaly Kuznetsov | 11e3491 | 2019-06-28 13:23:33 +0200 | [diff] [blame] | 5013 | u64 evmcs_gpa; |
Vitaly Kuznetsov | 7a35e51 | 2020-06-05 13:59:05 +0200 | [diff] [blame] | 5014 | int r; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5015 | |
| 5016 | if (!nested_vmx_check_permission(vcpu)) |
| 5017 | return 1; |
| 5018 | |
Vitaly Kuznetsov | 7a35e51 | 2020-06-05 13:59:05 +0200 | [diff] [blame] | 5019 | if (nested_vmx_get_vmptr(vcpu, &vmptr, &r)) |
| 5020 | return r; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5021 | |
KarimAllah Ahmed | e0bf266 | 2019-01-31 21:24:43 +0100 | [diff] [blame] | 5022 | if (!page_address_valid(vcpu, vmptr)) |
Sean Christopherson | b2656e4 | 2020-06-08 18:56:07 -0700 | [diff] [blame] | 5023 | return nested_vmx_fail(vcpu, VMXERR_VMCLEAR_INVALID_ADDRESS); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5024 | |
| 5025 | if (vmptr == vmx->nested.vmxon_ptr) |
Sean Christopherson | b2656e4 | 2020-06-08 18:56:07 -0700 | [diff] [blame] | 5026 | return nested_vmx_fail(vcpu, VMXERR_VMCLEAR_VMXON_POINTER); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5027 | |
Vitaly Kuznetsov | 11e3491 | 2019-06-28 13:23:33 +0200 | [diff] [blame] | 5028 | /* |
| 5029 | * When Enlightened VMEntry is enabled on the calling CPU we treat |
| 5030 | * memory area pointer by vmptr as Enlightened VMCS (as there's no good |
| 5031 | * way to distinguish it from VMCS12) and we must not corrupt it by |
| 5032 | * writing to the non-existent 'launch_state' field. The area doesn't |
| 5033 | * have to be the currently active EVMCS on the calling CPU and there's |
| 5034 | * nothing KVM has to do to transition it from 'active' to 'non-active' |
| 5035 | * state. It is possible that the area will stay mapped as |
| 5036 | * vmx->nested.hv_evmcs but this shouldn't be a problem. |
| 5037 | */ |
| 5038 | if (likely(!vmx->nested.enlightened_vmcs_enabled || |
| 5039 | !nested_enlightened_vmentry(vcpu, &evmcs_gpa))) { |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5040 | if (vmptr == vmx->nested.current_vmptr) |
| 5041 | nested_release_vmcs12(vcpu); |
| 5042 | |
| 5043 | kvm_vcpu_write_guest(vcpu, |
| 5044 | vmptr + offsetof(struct vmcs12, |
| 5045 | launch_state), |
| 5046 | &zero, sizeof(zero)); |
Vitaly Kuznetsov | 3b19b81 | 2021-05-26 15:20:21 +0200 | [diff] [blame] | 5047 | } else if (vmx->nested.hv_evmcs && vmptr == vmx->nested.hv_evmcs_vmptr) { |
| 5048 | nested_release_evmcs(vcpu); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5049 | } |
| 5050 | |
| 5051 | return nested_vmx_succeed(vcpu); |
| 5052 | } |
| 5053 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5054 | /* Emulate the VMLAUNCH instruction */ |
| 5055 | static int handle_vmlaunch(struct kvm_vcpu *vcpu) |
| 5056 | { |
| 5057 | return nested_vmx_run(vcpu, true); |
| 5058 | } |
| 5059 | |
| 5060 | /* Emulate the VMRESUME instruction */ |
| 5061 | static int handle_vmresume(struct kvm_vcpu *vcpu) |
| 5062 | { |
| 5063 | |
| 5064 | return nested_vmx_run(vcpu, false); |
| 5065 | } |
| 5066 | |
| 5067 | static int handle_vmread(struct kvm_vcpu *vcpu) |
| 5068 | { |
Jim Mattson | dd2d604 | 2019-12-06 15:46:35 -0800 | [diff] [blame] | 5069 | struct vmcs12 *vmcs12 = is_guest_mode(vcpu) ? get_shadow_vmcs12(vcpu) |
| 5070 | : get_vmcs12(vcpu); |
Sean Christopherson | 5addc23 | 2020-04-15 13:34:53 -0700 | [diff] [blame] | 5071 | unsigned long exit_qualification = vmx_get_exit_qual(vcpu); |
Jim Mattson | c90f4d0 | 2019-12-06 15:46:37 -0800 | [diff] [blame] | 5072 | u32 instr_info = vmcs_read32(VMX_INSTRUCTION_INFO); |
| 5073 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Paolo Bonzini | f7eea63 | 2019-09-14 00:26:27 +0200 | [diff] [blame] | 5074 | struct x86_exception e; |
Jim Mattson | c90f4d0 | 2019-12-06 15:46:37 -0800 | [diff] [blame] | 5075 | unsigned long field; |
| 5076 | u64 value; |
| 5077 | gva_t gva = 0; |
Sean Christopherson | 1c6f0b4 | 2019-05-07 08:36:25 -0700 | [diff] [blame] | 5078 | short offset; |
Vitaly Kuznetsov | 7a35e51 | 2020-06-05 13:59:05 +0200 | [diff] [blame] | 5079 | int len, r; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5080 | |
| 5081 | if (!nested_vmx_check_permission(vcpu)) |
| 5082 | return 1; |
| 5083 | |
Jim Mattson | dd2d604 | 2019-12-06 15:46:35 -0800 | [diff] [blame] | 5084 | /* |
Yu Zhang | 64c7850 | 2021-09-30 01:51:53 +0800 | [diff] [blame] | 5085 | * In VMX non-root operation, when the VMCS-link pointer is INVALID_GPA, |
Jim Mattson | dd2d604 | 2019-12-06 15:46:35 -0800 | [diff] [blame] | 5086 | * any VMREAD sets the ALU flags for VMfailInvalid. |
| 5087 | */ |
Yu Zhang | 64c7850 | 2021-09-30 01:51:53 +0800 | [diff] [blame] | 5088 | if (vmx->nested.current_vmptr == INVALID_GPA || |
Jim Mattson | dd2d604 | 2019-12-06 15:46:35 -0800 | [diff] [blame] | 5089 | (is_guest_mode(vcpu) && |
Yu Zhang | 64c7850 | 2021-09-30 01:51:53 +0800 | [diff] [blame] | 5090 | get_vmcs12(vcpu)->vmcs_link_pointer == INVALID_GPA)) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5091 | return nested_vmx_failInvalid(vcpu); |
| 5092 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5093 | /* Decode instruction info and find the field to read */ |
Sean Christopherson | 27b4a9c4 | 2021-04-21 19:21:28 -0700 | [diff] [blame] | 5094 | field = kvm_register_read(vcpu, (((instr_info) >> 28) & 0xf)); |
Sean Christopherson | 1c6f0b4 | 2019-05-07 08:36:25 -0700 | [diff] [blame] | 5095 | |
| 5096 | offset = vmcs_field_to_offset(field); |
| 5097 | if (offset < 0) |
Sean Christopherson | b2656e4 | 2020-06-08 18:56:07 -0700 | [diff] [blame] | 5098 | return nested_vmx_fail(vcpu, VMXERR_UNSUPPORTED_VMCS_COMPONENT); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5099 | |
Sean Christopherson | 7952d76 | 2019-05-07 08:36:29 -0700 | [diff] [blame] | 5100 | if (!is_guest_mode(vcpu) && is_vmcs12_ext_field(field)) |
| 5101 | copy_vmcs02_to_vmcs12_rare(vcpu, vmcs12); |
| 5102 | |
Jim Mattson | c90f4d0 | 2019-12-06 15:46:37 -0800 | [diff] [blame] | 5103 | /* Read the field, zero-extended to a u64 value */ |
| 5104 | value = vmcs12_read_any(vmcs12, field, offset); |
Sean Christopherson | 1c6f0b4 | 2019-05-07 08:36:25 -0700 | [diff] [blame] | 5105 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5106 | /* |
| 5107 | * Now copy part of this value to register or memory, as requested. |
| 5108 | * Note that the number of bits actually copied is 32 or 64 depending |
| 5109 | * on the guest's mode (32 or 64 bit), not on the given field's length. |
| 5110 | */ |
Jim Mattson | c90f4d0 | 2019-12-06 15:46:37 -0800 | [diff] [blame] | 5111 | if (instr_info & BIT(10)) { |
Sean Christopherson | 27b4a9c4 | 2021-04-21 19:21:28 -0700 | [diff] [blame] | 5112 | kvm_register_write(vcpu, (((instr_info) >> 3) & 0xf), value); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5113 | } else { |
Eugene Korenevsky | fdb2861 | 2019-06-06 00:19:16 +0300 | [diff] [blame] | 5114 | len = is_64_bit_mode(vcpu) ? 8 : 4; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5115 | if (get_vmx_mem_address(vcpu, exit_qualification, |
Jim Mattson | c90f4d0 | 2019-12-06 15:46:37 -0800 | [diff] [blame] | 5116 | instr_info, true, len, &gva)) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5117 | return 1; |
| 5118 | /* _system ok, nested_vmx_check_permission has verified cpl=0 */ |
Vitaly Kuznetsov | 7a35e51 | 2020-06-05 13:59:05 +0200 | [diff] [blame] | 5119 | r = kvm_write_guest_virt_system(vcpu, gva, &value, len, &e); |
| 5120 | if (r != X86EMUL_CONTINUE) |
Babu Moger | 3f3393b | 2020-09-11 14:29:05 -0500 | [diff] [blame] | 5121 | return kvm_handle_memory_failure(vcpu, r, &e); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5122 | } |
| 5123 | |
| 5124 | return nested_vmx_succeed(vcpu); |
| 5125 | } |
| 5126 | |
Sean Christopherson | e217429 | 2019-05-07 08:36:28 -0700 | [diff] [blame] | 5127 | static bool is_shadow_field_rw(unsigned long field) |
| 5128 | { |
| 5129 | switch (field) { |
| 5130 | #define SHADOW_FIELD_RW(x, y) case x: |
| 5131 | #include "vmcs_shadow_fields.h" |
| 5132 | return true; |
| 5133 | default: |
| 5134 | break; |
| 5135 | } |
| 5136 | return false; |
| 5137 | } |
| 5138 | |
| 5139 | static bool is_shadow_field_ro(unsigned long field) |
| 5140 | { |
| 5141 | switch (field) { |
| 5142 | #define SHADOW_FIELD_RO(x, y) case x: |
| 5143 | #include "vmcs_shadow_fields.h" |
| 5144 | return true; |
| 5145 | default: |
| 5146 | break; |
| 5147 | } |
| 5148 | return false; |
| 5149 | } |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5150 | |
| 5151 | static int handle_vmwrite(struct kvm_vcpu *vcpu) |
| 5152 | { |
Jim Mattson | dd2d604 | 2019-12-06 15:46:35 -0800 | [diff] [blame] | 5153 | struct vmcs12 *vmcs12 = is_guest_mode(vcpu) ? get_shadow_vmcs12(vcpu) |
| 5154 | : get_vmcs12(vcpu); |
Sean Christopherson | 5addc23 | 2020-04-15 13:34:53 -0700 | [diff] [blame] | 5155 | unsigned long exit_qualification = vmx_get_exit_qual(vcpu); |
Jim Mattson | c90f4d0 | 2019-12-06 15:46:37 -0800 | [diff] [blame] | 5156 | u32 instr_info = vmcs_read32(VMX_INSTRUCTION_INFO); |
| 5157 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 5158 | struct x86_exception e; |
| 5159 | unsigned long field; |
Sean Christopherson | 1c6f0b4 | 2019-05-07 08:36:25 -0700 | [diff] [blame] | 5160 | short offset; |
Jim Mattson | c90f4d0 | 2019-12-06 15:46:37 -0800 | [diff] [blame] | 5161 | gva_t gva; |
Vitaly Kuznetsov | 7a35e51 | 2020-06-05 13:59:05 +0200 | [diff] [blame] | 5162 | int len, r; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5163 | |
Jim Mattson | c90f4d0 | 2019-12-06 15:46:37 -0800 | [diff] [blame] | 5164 | /* |
| 5165 | * 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] | 5166 | * mode, and eventually we need to write that into a field of several |
| 5167 | * possible lengths. The code below first zero-extends the value to 64 |
Jim Mattson | c90f4d0 | 2019-12-06 15:46:37 -0800 | [diff] [blame] | 5168 | * bit (value), and then copies only the appropriate number of |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5169 | * bits into the vmcs12 field. |
| 5170 | */ |
Jim Mattson | c90f4d0 | 2019-12-06 15:46:37 -0800 | [diff] [blame] | 5171 | u64 value = 0; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5172 | |
| 5173 | if (!nested_vmx_check_permission(vcpu)) |
| 5174 | return 1; |
| 5175 | |
Jim Mattson | dd2d604 | 2019-12-06 15:46:35 -0800 | [diff] [blame] | 5176 | /* |
Yu Zhang | 64c7850 | 2021-09-30 01:51:53 +0800 | [diff] [blame] | 5177 | * In VMX non-root operation, when the VMCS-link pointer is INVALID_GPA, |
Jim Mattson | dd2d604 | 2019-12-06 15:46:35 -0800 | [diff] [blame] | 5178 | * any VMWRITE sets the ALU flags for VMfailInvalid. |
| 5179 | */ |
Yu Zhang | 64c7850 | 2021-09-30 01:51:53 +0800 | [diff] [blame] | 5180 | if (vmx->nested.current_vmptr == INVALID_GPA || |
Jim Mattson | dd2d604 | 2019-12-06 15:46:35 -0800 | [diff] [blame] | 5181 | (is_guest_mode(vcpu) && |
Yu Zhang | 64c7850 | 2021-09-30 01:51:53 +0800 | [diff] [blame] | 5182 | get_vmcs12(vcpu)->vmcs_link_pointer == INVALID_GPA)) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5183 | return nested_vmx_failInvalid(vcpu); |
| 5184 | |
Jim Mattson | c90f4d0 | 2019-12-06 15:46:37 -0800 | [diff] [blame] | 5185 | if (instr_info & BIT(10)) |
Sean Christopherson | 27b4a9c4 | 2021-04-21 19:21:28 -0700 | [diff] [blame] | 5186 | value = kvm_register_read(vcpu, (((instr_info) >> 3) & 0xf)); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5187 | else { |
Eugene Korenevsky | fdb2861 | 2019-06-06 00:19:16 +0300 | [diff] [blame] | 5188 | len = is_64_bit_mode(vcpu) ? 8 : 4; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5189 | if (get_vmx_mem_address(vcpu, exit_qualification, |
Jim Mattson | c90f4d0 | 2019-12-06 15:46:37 -0800 | [diff] [blame] | 5190 | instr_info, false, len, &gva)) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5191 | return 1; |
Vitaly Kuznetsov | 7a35e51 | 2020-06-05 13:59:05 +0200 | [diff] [blame] | 5192 | r = kvm_read_guest_virt(vcpu, gva, &value, len, &e); |
| 5193 | if (r != X86EMUL_CONTINUE) |
Babu Moger | 3f3393b | 2020-09-11 14:29:05 -0500 | [diff] [blame] | 5194 | return kvm_handle_memory_failure(vcpu, r, &e); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5195 | } |
| 5196 | |
Sean Christopherson | 27b4a9c4 | 2021-04-21 19:21:28 -0700 | [diff] [blame] | 5197 | field = kvm_register_read(vcpu, (((instr_info) >> 28) & 0xf)); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5198 | |
Jim Mattson | 693e02c | 2019-12-06 15:46:36 -0800 | [diff] [blame] | 5199 | offset = vmcs_field_to_offset(field); |
| 5200 | if (offset < 0) |
Sean Christopherson | b2656e4 | 2020-06-08 18:56:07 -0700 | [diff] [blame] | 5201 | return nested_vmx_fail(vcpu, VMXERR_UNSUPPORTED_VMCS_COMPONENT); |
Jim Mattson | 693e02c | 2019-12-06 15:46:36 -0800 | [diff] [blame] | 5202 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5203 | /* |
| 5204 | * If the vCPU supports "VMWRITE to any supported field in the |
| 5205 | * VMCS," then the "read-only" fields are actually read/write. |
| 5206 | */ |
| 5207 | if (vmcs_field_readonly(field) && |
| 5208 | !nested_cpu_has_vmwrite_any_field(vcpu)) |
Sean Christopherson | b2656e4 | 2020-06-08 18:56:07 -0700 | [diff] [blame] | 5209 | return nested_vmx_fail(vcpu, VMXERR_VMWRITE_READ_ONLY_VMCS_COMPONENT); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5210 | |
Jim Mattson | dd2d604 | 2019-12-06 15:46:35 -0800 | [diff] [blame] | 5211 | /* |
| 5212 | * Ensure vmcs12 is up-to-date before any VMWRITE that dirties |
| 5213 | * vmcs12, else we may crush a field or consume a stale value. |
| 5214 | */ |
| 5215 | if (!is_guest_mode(vcpu) && !is_shadow_field_rw(field)) |
| 5216 | copy_vmcs02_to_vmcs12_rare(vcpu, vmcs12); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5217 | |
| 5218 | /* |
Sean Christopherson | b643780 | 2019-05-07 08:36:24 -0700 | [diff] [blame] | 5219 | * Some Intel CPUs intentionally drop the reserved bits of the AR byte |
| 5220 | * fields on VMWRITE. Emulate this behavior to ensure consistent KVM |
| 5221 | * behavior regardless of the underlying hardware, e.g. if an AR_BYTE |
| 5222 | * field is intercepted for VMWRITE but not VMREAD (in L1), then VMREAD |
| 5223 | * from L1 will return a different value than VMREAD from L2 (L1 sees |
| 5224 | * 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] | 5225 | */ |
Sean Christopherson | b643780 | 2019-05-07 08:36:24 -0700 | [diff] [blame] | 5226 | if (field >= GUEST_ES_AR_BYTES && field <= GUEST_TR_AR_BYTES) |
Jim Mattson | c90f4d0 | 2019-12-06 15:46:37 -0800 | [diff] [blame] | 5227 | value &= 0x1f0ff; |
Sean Christopherson | b643780 | 2019-05-07 08:36:24 -0700 | [diff] [blame] | 5228 | |
Jim Mattson | c90f4d0 | 2019-12-06 15:46:37 -0800 | [diff] [blame] | 5229 | vmcs12_write_any(vmcs12, field, offset, value); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5230 | |
| 5231 | /* |
Sean Christopherson | e217429 | 2019-05-07 08:36:28 -0700 | [diff] [blame] | 5232 | * Do not track vmcs12 dirty-state if in guest-mode as we actually |
| 5233 | * dirty shadow vmcs12 instead of vmcs12. Fields that can be updated |
| 5234 | * by L1 without a vmexit are always updated in the vmcs02, i.e. don't |
| 5235 | * "dirty" vmcs12, all others go down the prepare_vmcs02() slow path. |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5236 | */ |
Sean Christopherson | e217429 | 2019-05-07 08:36:28 -0700 | [diff] [blame] | 5237 | if (!is_guest_mode(vcpu) && !is_shadow_field_rw(field)) { |
| 5238 | /* |
| 5239 | * L1 can read these fields without exiting, ensure the |
| 5240 | * shadow VMCS is up-to-date. |
| 5241 | */ |
| 5242 | if (enable_shadow_vmcs && is_shadow_field_ro(field)) { |
| 5243 | preempt_disable(); |
| 5244 | vmcs_load(vmx->vmcs01.shadow_vmcs); |
Sean Christopherson | fadcead | 2019-05-07 08:36:23 -0700 | [diff] [blame] | 5245 | |
Jim Mattson | c90f4d0 | 2019-12-06 15:46:37 -0800 | [diff] [blame] | 5246 | __vmcs_writel(field, value); |
Sean Christopherson | fadcead | 2019-05-07 08:36:23 -0700 | [diff] [blame] | 5247 | |
Sean Christopherson | e217429 | 2019-05-07 08:36:28 -0700 | [diff] [blame] | 5248 | vmcs_clear(vmx->vmcs01.shadow_vmcs); |
| 5249 | vmcs_load(vmx->loaded_vmcs->vmcs); |
| 5250 | preempt_enable(); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5251 | } |
Sean Christopherson | e217429 | 2019-05-07 08:36:28 -0700 | [diff] [blame] | 5252 | vmx->nested.dirty_vmcs12 = true; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5253 | } |
| 5254 | |
| 5255 | return nested_vmx_succeed(vcpu); |
| 5256 | } |
| 5257 | |
| 5258 | static void set_current_vmptr(struct vcpu_vmx *vmx, gpa_t vmptr) |
| 5259 | { |
| 5260 | vmx->nested.current_vmptr = vmptr; |
| 5261 | if (enable_shadow_vmcs) { |
Sean Christopherson | fe7f895d | 2019-05-07 12:17:57 -0700 | [diff] [blame] | 5262 | secondary_exec_controls_setbit(vmx, SECONDARY_EXEC_SHADOW_VMCS); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5263 | vmcs_write64(VMCS_LINK_POINTER, |
| 5264 | __pa(vmx->vmcs01.shadow_vmcs)); |
Sean Christopherson | 3731905ef | 2019-05-07 08:36:27 -0700 | [diff] [blame] | 5265 | vmx->nested.need_vmcs12_to_shadow_sync = true; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5266 | } |
| 5267 | vmx->nested.dirty_vmcs12 = true; |
Vitaly Kuznetsov | ed2a480 | 2021-11-29 10:47:03 +0100 | [diff] [blame^] | 5268 | vmx->nested.force_msr_bitmap_recalc = true; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 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) { |
David Woodhouse | cee6666 | 2021-11-15 16:50:26 +0000 | [diff] [blame] | 5295 | struct gfn_to_hva_cache *ghc = &vmx->nested.vmcs12_cache; |
| 5296 | struct vmcs_hdr hdr; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5297 | |
Paolo Bonzini | 8503fea | 2021-11-22 18:20:16 -0500 | [diff] [blame] | 5298 | if (kvm_gfn_to_hva_cache_init(vcpu->kvm, ghc, vmptr, VMCS12_SIZE)) { |
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 | |
David Woodhouse | cee6666 | 2021-11-15 16:50:26 +0000 | [diff] [blame] | 5309 | if (kvm_read_guest_offset_cached(vcpu->kvm, ghc, &hdr, |
| 5310 | offsetof(struct vmcs12, hdr), |
| 5311 | sizeof(hdr))) { |
| 5312 | return nested_vmx_fail(vcpu, |
| 5313 | VMXERR_VMPTRLD_INCORRECT_VMCS_REVISION_ID); |
| 5314 | } |
KarimAllah Ahmed | b146b83 | 2019-01-31 21:24:35 +0100 | [diff] [blame] | 5315 | |
David Woodhouse | cee6666 | 2021-11-15 16:50:26 +0000 | [diff] [blame] | 5316 | if (hdr.revision_id != VMCS12_REVISION || |
| 5317 | (hdr.shadow_vmcs && |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5318 | !nested_cpu_has_vmx_shadow_vmcs(vcpu))) { |
Sean Christopherson | b2656e4 | 2020-06-08 18:56:07 -0700 | [diff] [blame] | 5319 | return nested_vmx_fail(vcpu, |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5320 | VMXERR_VMPTRLD_INCORRECT_VMCS_REVISION_ID); |
| 5321 | } |
| 5322 | |
| 5323 | nested_release_vmcs12(vcpu); |
| 5324 | |
| 5325 | /* |
| 5326 | * Load VMCS12 from guest memory since it is not already |
| 5327 | * cached. |
| 5328 | */ |
David Woodhouse | cee6666 | 2021-11-15 16:50:26 +0000 | [diff] [blame] | 5329 | if (kvm_read_guest_cached(vcpu->kvm, ghc, vmx->nested.cached_vmcs12, |
| 5330 | VMCS12_SIZE)) { |
| 5331 | return nested_vmx_fail(vcpu, |
| 5332 | VMXERR_VMPTRLD_INCORRECT_VMCS_REVISION_ID); |
| 5333 | } |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5334 | |
| 5335 | set_current_vmptr(vmx, vmptr); |
| 5336 | } |
| 5337 | |
| 5338 | return nested_vmx_succeed(vcpu); |
| 5339 | } |
| 5340 | |
| 5341 | /* Emulate the VMPTRST instruction */ |
| 5342 | static int handle_vmptrst(struct kvm_vcpu *vcpu) |
| 5343 | { |
Sean Christopherson | 5addc23 | 2020-04-15 13:34:53 -0700 | [diff] [blame] | 5344 | unsigned long exit_qual = vmx_get_exit_qual(vcpu); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5345 | u32 instr_info = vmcs_read32(VMX_INSTRUCTION_INFO); |
| 5346 | gpa_t current_vmptr = to_vmx(vcpu)->nested.current_vmptr; |
| 5347 | struct x86_exception e; |
| 5348 | gva_t gva; |
Vitaly Kuznetsov | 7a35e51 | 2020-06-05 13:59:05 +0200 | [diff] [blame] | 5349 | int r; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5350 | |
| 5351 | if (!nested_vmx_check_permission(vcpu)) |
| 5352 | return 1; |
| 5353 | |
Vitaly Kuznetsov | 1e9dfbd | 2021-05-26 15:20:16 +0200 | [diff] [blame] | 5354 | if (unlikely(evmptr_is_valid(to_vmx(vcpu)->nested.hv_evmcs_vmptr))) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5355 | return 1; |
| 5356 | |
Eugene Korenevsky | fdb2861 | 2019-06-06 00:19:16 +0300 | [diff] [blame] | 5357 | if (get_vmx_mem_address(vcpu, exit_qual, instr_info, |
| 5358 | true, sizeof(gpa_t), &gva)) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5359 | return 1; |
| 5360 | /* *_system ok, nested_vmx_check_permission has verified cpl=0 */ |
Vitaly Kuznetsov | 7a35e51 | 2020-06-05 13:59:05 +0200 | [diff] [blame] | 5361 | r = kvm_write_guest_virt_system(vcpu, gva, (void *)¤t_vmptr, |
| 5362 | sizeof(gpa_t), &e); |
| 5363 | if (r != X86EMUL_CONTINUE) |
Babu Moger | 3f3393b | 2020-09-11 14:29:05 -0500 | [diff] [blame] | 5364 | return kvm_handle_memory_failure(vcpu, r, &e); |
Vitaly Kuznetsov | 7a35e51 | 2020-06-05 13:59:05 +0200 | [diff] [blame] | 5365 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5366 | return nested_vmx_succeed(vcpu); |
| 5367 | } |
| 5368 | |
| 5369 | /* Emulate the INVEPT instruction */ |
| 5370 | static int handle_invept(struct kvm_vcpu *vcpu) |
| 5371 | { |
| 5372 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 5373 | u32 vmx_instruction_info, types; |
Sean Christopherson | ce8fe7b | 2020-03-20 14:28:31 -0700 | [diff] [blame] | 5374 | unsigned long type, roots_to_free; |
| 5375 | struct kvm_mmu *mmu; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5376 | gva_t gva; |
| 5377 | struct x86_exception e; |
| 5378 | struct { |
| 5379 | u64 eptp, gpa; |
| 5380 | } operand; |
Vipin Sharma | 329bd56 | 2021-11-09 17:44:25 +0000 | [diff] [blame] | 5381 | int i, r, gpr_index; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5382 | |
| 5383 | if (!(vmx->nested.msrs.secondary_ctls_high & |
| 5384 | SECONDARY_EXEC_ENABLE_EPT) || |
| 5385 | !(vmx->nested.msrs.ept_caps & VMX_EPT_INVEPT_BIT)) { |
| 5386 | kvm_queue_exception(vcpu, UD_VECTOR); |
| 5387 | return 1; |
| 5388 | } |
| 5389 | |
| 5390 | if (!nested_vmx_check_permission(vcpu)) |
| 5391 | return 1; |
| 5392 | |
| 5393 | vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO); |
Vipin Sharma | 329bd56 | 2021-11-09 17:44:25 +0000 | [diff] [blame] | 5394 | gpr_index = vmx_get_instr_info_reg2(vmx_instruction_info); |
| 5395 | type = kvm_register_read(vcpu, gpr_index); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5396 | |
| 5397 | types = (vmx->nested.msrs.ept_caps >> VMX_EPT_EXTENT_SHIFT) & 6; |
| 5398 | |
| 5399 | if (type >= 32 || !(types & (1 << type))) |
Sean Christopherson | b2656e4 | 2020-06-08 18:56:07 -0700 | [diff] [blame] | 5400 | return nested_vmx_fail(vcpu, VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5401 | |
| 5402 | /* According to the Intel VMX instruction reference, the memory |
| 5403 | * operand is read even if it isn't needed (e.g., for type==global) |
| 5404 | */ |
Sean Christopherson | 5addc23 | 2020-04-15 13:34:53 -0700 | [diff] [blame] | 5405 | if (get_vmx_mem_address(vcpu, vmx_get_exit_qual(vcpu), |
Eugene Korenevsky | fdb2861 | 2019-06-06 00:19:16 +0300 | [diff] [blame] | 5406 | vmx_instruction_info, false, sizeof(operand), &gva)) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5407 | return 1; |
Vitaly Kuznetsov | 7a35e51 | 2020-06-05 13:59:05 +0200 | [diff] [blame] | 5408 | r = kvm_read_guest_virt(vcpu, gva, &operand, sizeof(operand), &e); |
| 5409 | if (r != X86EMUL_CONTINUE) |
Babu Moger | 3f3393b | 2020-09-11 14:29:05 -0500 | [diff] [blame] | 5410 | return kvm_handle_memory_failure(vcpu, r, &e); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5411 | |
Sean Christopherson | ce8fe7b | 2020-03-20 14:28:31 -0700 | [diff] [blame] | 5412 | /* |
| 5413 | * Nested EPT roots are always held through guest_mmu, |
| 5414 | * not root_mmu. |
| 5415 | */ |
| 5416 | mmu = &vcpu->arch.guest_mmu; |
| 5417 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5418 | switch (type) { |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5419 | case VMX_EPT_EXTENT_CONTEXT: |
Sean Christopherson | eed0030 | 2020-03-20 14:27:58 -0700 | [diff] [blame] | 5420 | if (!nested_vmx_check_eptp(vcpu, operand.eptp)) |
Sean Christopherson | b2656e4 | 2020-06-08 18:56:07 -0700 | [diff] [blame] | 5421 | return nested_vmx_fail(vcpu, |
Sean Christopherson | eed0030 | 2020-03-20 14:27:58 -0700 | [diff] [blame] | 5422 | VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID); |
Sean Christopherson | f8aa7e3 | 2020-03-20 14:27:59 -0700 | [diff] [blame] | 5423 | |
Sean Christopherson | ce8fe7b | 2020-03-20 14:28:31 -0700 | [diff] [blame] | 5424 | roots_to_free = 0; |
Sean Christopherson | be01e8e | 2020-03-20 14:28:32 -0700 | [diff] [blame] | 5425 | if (nested_ept_root_matches(mmu->root_hpa, mmu->root_pgd, |
Sean Christopherson | ce8fe7b | 2020-03-20 14:28:31 -0700 | [diff] [blame] | 5426 | operand.eptp)) |
| 5427 | roots_to_free |= KVM_MMU_ROOT_CURRENT; |
| 5428 | |
| 5429 | for (i = 0; i < KVM_MMU_NUM_PREV_ROOTS; i++) { |
| 5430 | if (nested_ept_root_matches(mmu->prev_roots[i].hpa, |
Sean Christopherson | be01e8e | 2020-03-20 14:28:32 -0700 | [diff] [blame] | 5431 | mmu->prev_roots[i].pgd, |
Sean Christopherson | ce8fe7b | 2020-03-20 14:28:31 -0700 | [diff] [blame] | 5432 | operand.eptp)) |
| 5433 | roots_to_free |= KVM_MMU_ROOT_PREVIOUS(i); |
| 5434 | } |
| 5435 | break; |
Sean Christopherson | eed0030 | 2020-03-20 14:27:58 -0700 | [diff] [blame] | 5436 | case VMX_EPT_EXTENT_GLOBAL: |
Sean Christopherson | ce8fe7b | 2020-03-20 14:28:31 -0700 | [diff] [blame] | 5437 | roots_to_free = KVM_MMU_ROOTS_ALL; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5438 | break; |
| 5439 | default: |
Sean Christopherson | f9336e3 | 2020-05-04 08:35:06 -0700 | [diff] [blame] | 5440 | BUG(); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5441 | break; |
| 5442 | } |
| 5443 | |
Sean Christopherson | ce8fe7b | 2020-03-20 14:28:31 -0700 | [diff] [blame] | 5444 | if (roots_to_free) |
| 5445 | kvm_mmu_free_roots(vcpu, mmu, roots_to_free); |
| 5446 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5447 | return nested_vmx_succeed(vcpu); |
| 5448 | } |
| 5449 | |
| 5450 | static int handle_invvpid(struct kvm_vcpu *vcpu) |
| 5451 | { |
| 5452 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 5453 | u32 vmx_instruction_info; |
| 5454 | unsigned long type, types; |
| 5455 | gva_t gva; |
| 5456 | struct x86_exception e; |
| 5457 | struct { |
| 5458 | u64 vpid; |
| 5459 | u64 gla; |
| 5460 | } operand; |
| 5461 | u16 vpid02; |
Vipin Sharma | 329bd56 | 2021-11-09 17:44:25 +0000 | [diff] [blame] | 5462 | int r, gpr_index; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5463 | |
| 5464 | if (!(vmx->nested.msrs.secondary_ctls_high & |
| 5465 | SECONDARY_EXEC_ENABLE_VPID) || |
| 5466 | !(vmx->nested.msrs.vpid_caps & VMX_VPID_INVVPID_BIT)) { |
| 5467 | kvm_queue_exception(vcpu, UD_VECTOR); |
| 5468 | return 1; |
| 5469 | } |
| 5470 | |
| 5471 | if (!nested_vmx_check_permission(vcpu)) |
| 5472 | return 1; |
| 5473 | |
| 5474 | vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO); |
Vipin Sharma | 329bd56 | 2021-11-09 17:44:25 +0000 | [diff] [blame] | 5475 | gpr_index = vmx_get_instr_info_reg2(vmx_instruction_info); |
| 5476 | type = kvm_register_read(vcpu, gpr_index); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5477 | |
| 5478 | types = (vmx->nested.msrs.vpid_caps & |
| 5479 | VMX_VPID_EXTENT_SUPPORTED_MASK) >> 8; |
| 5480 | |
| 5481 | if (type >= 32 || !(types & (1 << type))) |
Sean Christopherson | b2656e4 | 2020-06-08 18:56:07 -0700 | [diff] [blame] | 5482 | return nested_vmx_fail(vcpu, |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5483 | VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID); |
| 5484 | |
| 5485 | /* according to the intel vmx instruction reference, the memory |
| 5486 | * operand is read even if it isn't needed (e.g., for type==global) |
| 5487 | */ |
Sean Christopherson | 5addc23 | 2020-04-15 13:34:53 -0700 | [diff] [blame] | 5488 | if (get_vmx_mem_address(vcpu, vmx_get_exit_qual(vcpu), |
Eugene Korenevsky | fdb2861 | 2019-06-06 00:19:16 +0300 | [diff] [blame] | 5489 | vmx_instruction_info, false, sizeof(operand), &gva)) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5490 | return 1; |
Vitaly Kuznetsov | 7a35e51 | 2020-06-05 13:59:05 +0200 | [diff] [blame] | 5491 | r = kvm_read_guest_virt(vcpu, gva, &operand, sizeof(operand), &e); |
| 5492 | if (r != X86EMUL_CONTINUE) |
Babu Moger | 3f3393b | 2020-09-11 14:29:05 -0500 | [diff] [blame] | 5493 | return kvm_handle_memory_failure(vcpu, r, &e); |
Vitaly Kuznetsov | 7a35e51 | 2020-06-05 13:59:05 +0200 | [diff] [blame] | 5494 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5495 | if (operand.vpid >> 16) |
Sean Christopherson | b2656e4 | 2020-06-08 18:56:07 -0700 | [diff] [blame] | 5496 | return nested_vmx_fail(vcpu, |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5497 | VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID); |
| 5498 | |
| 5499 | vpid02 = nested_get_vpid02(vcpu); |
| 5500 | switch (type) { |
| 5501 | case VMX_VPID_EXTENT_INDIVIDUAL_ADDR: |
| 5502 | if (!operand.vpid || |
| 5503 | is_noncanonical_address(operand.gla, vcpu)) |
Sean Christopherson | b2656e4 | 2020-06-08 18:56:07 -0700 | [diff] [blame] | 5504 | return nested_vmx_fail(vcpu, |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5505 | VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID); |
Sean Christopherson | bc41d0c | 2020-03-20 14:28:09 -0700 | [diff] [blame] | 5506 | vpid_sync_vcpu_addr(vpid02, operand.gla); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5507 | break; |
| 5508 | case VMX_VPID_EXTENT_SINGLE_CONTEXT: |
| 5509 | case VMX_VPID_EXTENT_SINGLE_NON_GLOBAL: |
| 5510 | if (!operand.vpid) |
Sean Christopherson | b2656e4 | 2020-06-08 18:56:07 -0700 | [diff] [blame] | 5511 | return nested_vmx_fail(vcpu, |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5512 | VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID); |
Sean Christopherson | 446ace4 | 2020-03-20 14:28:05 -0700 | [diff] [blame] | 5513 | vpid_sync_context(vpid02); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5514 | break; |
| 5515 | case VMX_VPID_EXTENT_ALL_CONTEXT: |
Sean Christopherson | 446ace4 | 2020-03-20 14:28:05 -0700 | [diff] [blame] | 5516 | vpid_sync_context(vpid02); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5517 | break; |
| 5518 | default: |
| 5519 | WARN_ON_ONCE(1); |
| 5520 | return kvm_skip_emulated_instruction(vcpu); |
| 5521 | } |
| 5522 | |
Junaid Shahid | d6e3f83 | 2020-03-20 14:28:00 -0700 | [diff] [blame] | 5523 | /* |
| 5524 | * Sync the shadow page tables if EPT is disabled, L1 is invalidating |
Sean Christopherson | 25b62c6 | 2021-06-09 16:42:29 -0700 | [diff] [blame] | 5525 | * linear mappings for L2 (tagged with L2's VPID). Free all guest |
| 5526 | * roots as VPIDs are not tracked in the MMU role. |
Junaid Shahid | d6e3f83 | 2020-03-20 14:28:00 -0700 | [diff] [blame] | 5527 | * |
| 5528 | * Note, this operates on root_mmu, not guest_mmu, as L1 and L2 share |
| 5529 | * an MMU when EPT is disabled. |
| 5530 | * |
| 5531 | * TODO: sync only the affected SPTEs for INVDIVIDUAL_ADDR. |
| 5532 | */ |
| 5533 | if (!enable_ept) |
Sean Christopherson | 25b62c6 | 2021-06-09 16:42:29 -0700 | [diff] [blame] | 5534 | kvm_mmu_free_guest_mode_roots(vcpu, &vcpu->arch.root_mmu); |
Junaid Shahid | d6e3f83 | 2020-03-20 14:28:00 -0700 | [diff] [blame] | 5535 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5536 | return nested_vmx_succeed(vcpu); |
| 5537 | } |
| 5538 | |
| 5539 | static int nested_vmx_eptp_switching(struct kvm_vcpu *vcpu, |
| 5540 | struct vmcs12 *vmcs12) |
| 5541 | { |
Sean Christopherson | 2b3eaf8 | 2019-04-30 10:36:19 -0700 | [diff] [blame] | 5542 | u32 index = kvm_rcx_read(vcpu); |
Sean Christopherson | ac6389a | 2020-03-02 18:02:38 -0800 | [diff] [blame] | 5543 | u64 new_eptp; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5544 | |
Sean Christopherson | c5ffd40 | 2021-06-09 16:42:35 -0700 | [diff] [blame] | 5545 | if (WARN_ON_ONCE(!nested_cpu_has_ept(vmcs12))) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5546 | return 1; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5547 | if (index >= VMFUNC_EPTP_ENTRIES) |
| 5548 | return 1; |
| 5549 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5550 | 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] | 5551 | &new_eptp, index * 8, 8)) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5552 | return 1; |
| 5553 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5554 | /* |
| 5555 | * If the (L2) guest does a vmfunc to the currently |
| 5556 | * active ept pointer, we don't have to do anything else |
| 5557 | */ |
Sean Christopherson | ac6389a | 2020-03-02 18:02:38 -0800 | [diff] [blame] | 5558 | if (vmcs12->ept_pointer != new_eptp) { |
| 5559 | if (!nested_vmx_check_eptp(vcpu, new_eptp)) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5560 | return 1; |
| 5561 | |
Sean Christopherson | ac6389a | 2020-03-02 18:02:38 -0800 | [diff] [blame] | 5562 | vmcs12->ept_pointer = new_eptp; |
Sean Christopherson | 39353ab | 2021-06-09 16:42:31 -0700 | [diff] [blame] | 5563 | nested_ept_new_eptp(vcpu); |
Sean Christopherson | c805f5d | 2021-03-04 17:10:57 -0800 | [diff] [blame] | 5564 | |
Sean Christopherson | 39353ab | 2021-06-09 16:42:31 -0700 | [diff] [blame] | 5565 | if (!nested_cpu_has_vpid(vmcs12)) |
| 5566 | kvm_make_request(KVM_REQ_TLB_FLUSH_GUEST, vcpu); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5567 | } |
| 5568 | |
| 5569 | return 0; |
| 5570 | } |
| 5571 | |
| 5572 | static int handle_vmfunc(struct kvm_vcpu *vcpu) |
| 5573 | { |
| 5574 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 5575 | struct vmcs12 *vmcs12; |
Sean Christopherson | 2b3eaf8 | 2019-04-30 10:36:19 -0700 | [diff] [blame] | 5576 | u32 function = kvm_rax_read(vcpu); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5577 | |
| 5578 | /* |
| 5579 | * VMFUNC is only supported for nested guests, but we always enable the |
| 5580 | * secondary control for simplicity; for non-nested mode, fake that we |
| 5581 | * didn't by injecting #UD. |
| 5582 | */ |
| 5583 | if (!is_guest_mode(vcpu)) { |
| 5584 | kvm_queue_exception(vcpu, UD_VECTOR); |
| 5585 | return 1; |
| 5586 | } |
| 5587 | |
| 5588 | vmcs12 = get_vmcs12(vcpu); |
Sean Christopherson | 546e839 | 2021-06-09 16:42:34 -0700 | [diff] [blame] | 5589 | |
| 5590 | /* |
| 5591 | * #UD on out-of-bounds function has priority over VM-Exit, and VMFUNC |
| 5592 | * is enabled in vmcs02 if and only if it's enabled in vmcs12. |
| 5593 | */ |
| 5594 | if (WARN_ON_ONCE((function > 63) || !nested_cpu_has_vmfunc(vmcs12))) { |
| 5595 | kvm_queue_exception(vcpu, UD_VECTOR); |
| 5596 | return 1; |
| 5597 | } |
| 5598 | |
Sean Christopherson | 0e75225 | 2021-06-09 16:42:22 -0700 | [diff] [blame] | 5599 | if (!(vmcs12->vm_function_control & BIT_ULL(function))) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5600 | goto fail; |
| 5601 | |
| 5602 | switch (function) { |
| 5603 | case 0: |
| 5604 | if (nested_vmx_eptp_switching(vcpu, vmcs12)) |
| 5605 | goto fail; |
| 5606 | break; |
| 5607 | default: |
| 5608 | goto fail; |
| 5609 | } |
| 5610 | return kvm_skip_emulated_instruction(vcpu); |
| 5611 | |
| 5612 | fail: |
Sean Christopherson | 8e53324 | 2020-11-06 17:03:12 +0800 | [diff] [blame] | 5613 | /* |
| 5614 | * This is effectively a reflected VM-Exit, as opposed to a synthesized |
| 5615 | * nested VM-Exit. Pass the original exit reason, i.e. don't hardcode |
| 5616 | * EXIT_REASON_VMFUNC as the exit reason. |
| 5617 | */ |
| 5618 | nested_vmx_vmexit(vcpu, vmx->exit_reason.full, |
Sean Christopherson | 8791585 | 2020-04-15 13:34:54 -0700 | [diff] [blame] | 5619 | vmx_get_intr_info(vcpu), |
Sean Christopherson | 5addc23 | 2020-04-15 13:34:53 -0700 | [diff] [blame] | 5620 | vmx_get_exit_qual(vcpu)); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5621 | return 1; |
| 5622 | } |
| 5623 | |
Oliver Upton | e71237d | 2020-02-04 15:26:30 -0800 | [diff] [blame] | 5624 | /* |
| 5625 | * Return true if an IO instruction with the specified port and size should cause |
| 5626 | * a VM-exit into L1. |
| 5627 | */ |
| 5628 | bool nested_vmx_check_io_bitmaps(struct kvm_vcpu *vcpu, unsigned int port, |
| 5629 | int size) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5630 | { |
Oliver Upton | e71237d | 2020-02-04 15:26:30 -0800 | [diff] [blame] | 5631 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5632 | gpa_t bitmap, last_bitmap; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5633 | u8 b; |
| 5634 | |
Yu Zhang | 64c7850 | 2021-09-30 01:51:53 +0800 | [diff] [blame] | 5635 | last_bitmap = INVALID_GPA; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5636 | b = -1; |
| 5637 | |
| 5638 | while (size > 0) { |
| 5639 | if (port < 0x8000) |
| 5640 | bitmap = vmcs12->io_bitmap_a; |
| 5641 | else if (port < 0x10000) |
| 5642 | bitmap = vmcs12->io_bitmap_b; |
| 5643 | else |
| 5644 | return true; |
| 5645 | bitmap += (port & 0x7fff) / 8; |
| 5646 | |
| 5647 | if (last_bitmap != bitmap) |
| 5648 | if (kvm_vcpu_read_guest(vcpu, bitmap, &b, 1)) |
| 5649 | return true; |
| 5650 | if (b & (1 << (port & 7))) |
| 5651 | return true; |
| 5652 | |
| 5653 | port++; |
| 5654 | size--; |
| 5655 | last_bitmap = bitmap; |
| 5656 | } |
| 5657 | |
| 5658 | return false; |
| 5659 | } |
| 5660 | |
Oliver Upton | e71237d | 2020-02-04 15:26:30 -0800 | [diff] [blame] | 5661 | static bool nested_vmx_exit_handled_io(struct kvm_vcpu *vcpu, |
| 5662 | struct vmcs12 *vmcs12) |
| 5663 | { |
| 5664 | unsigned long exit_qualification; |
Oliver Upton | 35a5713 | 2020-02-04 15:26:31 -0800 | [diff] [blame] | 5665 | unsigned short port; |
Oliver Upton | e71237d | 2020-02-04 15:26:30 -0800 | [diff] [blame] | 5666 | int size; |
| 5667 | |
| 5668 | if (!nested_cpu_has(vmcs12, CPU_BASED_USE_IO_BITMAPS)) |
| 5669 | return nested_cpu_has(vmcs12, CPU_BASED_UNCOND_IO_EXITING); |
| 5670 | |
Sean Christopherson | 5addc23 | 2020-04-15 13:34:53 -0700 | [diff] [blame] | 5671 | exit_qualification = vmx_get_exit_qual(vcpu); |
Oliver Upton | e71237d | 2020-02-04 15:26:30 -0800 | [diff] [blame] | 5672 | |
| 5673 | port = exit_qualification >> 16; |
| 5674 | size = (exit_qualification & 7) + 1; |
| 5675 | |
| 5676 | return nested_vmx_check_io_bitmaps(vcpu, port, size); |
| 5677 | } |
| 5678 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5679 | /* |
Miaohe Lin | 463bfee | 2020-02-14 10:44:05 +0800 | [diff] [blame] | 5680 | * 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] | 5681 | * rather than handle it ourselves in L0. I.e., check whether L1 expressed |
| 5682 | * disinterest in the current event (read or write a specific MSR) by using an |
| 5683 | * MSR bitmap. This may be the case even when L0 doesn't use MSR bitmaps. |
| 5684 | */ |
| 5685 | static bool nested_vmx_exit_handled_msr(struct kvm_vcpu *vcpu, |
Sean Christopherson | 8e53324 | 2020-11-06 17:03:12 +0800 | [diff] [blame] | 5686 | struct vmcs12 *vmcs12, |
| 5687 | union vmx_exit_reason exit_reason) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5688 | { |
Sean Christopherson | 2b3eaf8 | 2019-04-30 10:36:19 -0700 | [diff] [blame] | 5689 | u32 msr_index = kvm_rcx_read(vcpu); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5690 | gpa_t bitmap; |
| 5691 | |
| 5692 | if (!nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS)) |
| 5693 | return true; |
| 5694 | |
| 5695 | /* |
| 5696 | * The MSR_BITMAP page is divided into four 1024-byte bitmaps, |
| 5697 | * for the four combinations of read/write and low/high MSR numbers. |
| 5698 | * First we need to figure out which of the four to use: |
| 5699 | */ |
| 5700 | bitmap = vmcs12->msr_bitmap; |
Sean Christopherson | 8e53324 | 2020-11-06 17:03:12 +0800 | [diff] [blame] | 5701 | if (exit_reason.basic == EXIT_REASON_MSR_WRITE) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5702 | bitmap += 2048; |
| 5703 | if (msr_index >= 0xc0000000) { |
| 5704 | msr_index -= 0xc0000000; |
| 5705 | bitmap += 1024; |
| 5706 | } |
| 5707 | |
| 5708 | /* Then read the msr_index'th bit from this bitmap: */ |
| 5709 | if (msr_index < 1024*8) { |
| 5710 | unsigned char b; |
| 5711 | if (kvm_vcpu_read_guest(vcpu, bitmap + msr_index/8, &b, 1)) |
| 5712 | return true; |
| 5713 | return 1 & (b >> (msr_index & 7)); |
| 5714 | } else |
| 5715 | return true; /* let L1 handle the wrong parameter */ |
| 5716 | } |
| 5717 | |
| 5718 | /* |
| 5719 | * Return 1 if we should exit from L2 to L1 to handle a CR access exit, |
| 5720 | * rather than handle it ourselves in L0. I.e., check if L1 wanted to |
| 5721 | * intercept (via guest_host_mask etc.) the current event. |
| 5722 | */ |
| 5723 | static bool nested_vmx_exit_handled_cr(struct kvm_vcpu *vcpu, |
| 5724 | struct vmcs12 *vmcs12) |
| 5725 | { |
Sean Christopherson | 5addc23 | 2020-04-15 13:34:53 -0700 | [diff] [blame] | 5726 | unsigned long exit_qualification = vmx_get_exit_qual(vcpu); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5727 | int cr = exit_qualification & 15; |
| 5728 | int reg; |
| 5729 | unsigned long val; |
| 5730 | |
| 5731 | switch ((exit_qualification >> 4) & 3) { |
| 5732 | case 0: /* mov to cr */ |
| 5733 | reg = (exit_qualification >> 8) & 15; |
Sean Christopherson | 27b4a9c4 | 2021-04-21 19:21:28 -0700 | [diff] [blame] | 5734 | val = kvm_register_read(vcpu, reg); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5735 | switch (cr) { |
| 5736 | case 0: |
| 5737 | if (vmcs12->cr0_guest_host_mask & |
| 5738 | (val ^ vmcs12->cr0_read_shadow)) |
| 5739 | return true; |
| 5740 | break; |
| 5741 | case 3: |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5742 | if (nested_cpu_has(vmcs12, CPU_BASED_CR3_LOAD_EXITING)) |
| 5743 | return true; |
| 5744 | break; |
| 5745 | case 4: |
| 5746 | if (vmcs12->cr4_guest_host_mask & |
| 5747 | (vmcs12->cr4_read_shadow ^ val)) |
| 5748 | return true; |
| 5749 | break; |
| 5750 | case 8: |
| 5751 | if (nested_cpu_has(vmcs12, CPU_BASED_CR8_LOAD_EXITING)) |
| 5752 | return true; |
| 5753 | break; |
| 5754 | } |
| 5755 | break; |
| 5756 | case 2: /* clts */ |
| 5757 | if ((vmcs12->cr0_guest_host_mask & X86_CR0_TS) && |
| 5758 | (vmcs12->cr0_read_shadow & X86_CR0_TS)) |
| 5759 | return true; |
| 5760 | break; |
| 5761 | case 1: /* mov from cr */ |
| 5762 | switch (cr) { |
| 5763 | case 3: |
| 5764 | if (vmcs12->cpu_based_vm_exec_control & |
| 5765 | CPU_BASED_CR3_STORE_EXITING) |
| 5766 | return true; |
| 5767 | break; |
| 5768 | case 8: |
| 5769 | if (vmcs12->cpu_based_vm_exec_control & |
| 5770 | CPU_BASED_CR8_STORE_EXITING) |
| 5771 | return true; |
| 5772 | break; |
| 5773 | } |
| 5774 | break; |
| 5775 | case 3: /* lmsw */ |
| 5776 | /* |
| 5777 | * lmsw can change bits 1..3 of cr0, and only set bit 0 of |
| 5778 | * cr0. Other attempted changes are ignored, with no exit. |
| 5779 | */ |
| 5780 | val = (exit_qualification >> LMSW_SOURCE_DATA_SHIFT) & 0x0f; |
| 5781 | if (vmcs12->cr0_guest_host_mask & 0xe & |
| 5782 | (val ^ vmcs12->cr0_read_shadow)) |
| 5783 | return true; |
| 5784 | if ((vmcs12->cr0_guest_host_mask & 0x1) && |
| 5785 | !(vmcs12->cr0_read_shadow & 0x1) && |
| 5786 | (val & 0x1)) |
| 5787 | return true; |
| 5788 | break; |
| 5789 | } |
| 5790 | return false; |
| 5791 | } |
| 5792 | |
Sean Christopherson | 72add91 | 2021-04-12 16:21:42 +1200 | [diff] [blame] | 5793 | static bool nested_vmx_exit_handled_encls(struct kvm_vcpu *vcpu, |
| 5794 | struct vmcs12 *vmcs12) |
| 5795 | { |
| 5796 | u32 encls_leaf; |
| 5797 | |
| 5798 | if (!guest_cpuid_has(vcpu, X86_FEATURE_SGX) || |
| 5799 | !nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENCLS_EXITING)) |
| 5800 | return false; |
| 5801 | |
| 5802 | encls_leaf = kvm_rax_read(vcpu); |
| 5803 | if (encls_leaf > 62) |
| 5804 | encls_leaf = 63; |
| 5805 | return vmcs12->encls_exiting_bitmap & BIT_ULL(encls_leaf); |
| 5806 | } |
| 5807 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5808 | static bool nested_vmx_exit_handled_vmcs_access(struct kvm_vcpu *vcpu, |
| 5809 | struct vmcs12 *vmcs12, gpa_t bitmap) |
| 5810 | { |
| 5811 | u32 vmx_instruction_info; |
| 5812 | unsigned long field; |
| 5813 | u8 b; |
| 5814 | |
| 5815 | if (!nested_cpu_has_shadow_vmcs(vmcs12)) |
| 5816 | return true; |
| 5817 | |
| 5818 | /* Decode instruction info and find the field to access */ |
| 5819 | vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO); |
| 5820 | field = kvm_register_read(vcpu, (((vmx_instruction_info) >> 28) & 0xf)); |
| 5821 | |
| 5822 | /* Out-of-range fields always cause a VM exit from L2 to L1 */ |
| 5823 | if (field >> 15) |
| 5824 | return true; |
| 5825 | |
| 5826 | if (kvm_vcpu_read_guest(vcpu, bitmap + field/8, &b, 1)) |
| 5827 | return true; |
| 5828 | |
| 5829 | return 1 & (b >> (field & 7)); |
| 5830 | } |
| 5831 | |
Oliver Upton | b045ae9 | 2020-04-14 22:47:45 +0000 | [diff] [blame] | 5832 | static bool nested_vmx_exit_handled_mtf(struct vmcs12 *vmcs12) |
| 5833 | { |
| 5834 | u32 entry_intr_info = vmcs12->vm_entry_intr_info_field; |
| 5835 | |
| 5836 | if (nested_cpu_has_mtf(vmcs12)) |
| 5837 | return true; |
| 5838 | |
| 5839 | /* |
| 5840 | * An MTF VM-exit may be injected into the guest by setting the |
| 5841 | * interruption-type to 7 (other event) and the vector field to 0. Such |
| 5842 | * is the case regardless of the 'monitor trap flag' VM-execution |
| 5843 | * control. |
| 5844 | */ |
| 5845 | return entry_intr_info == (INTR_INFO_VALID_MASK |
| 5846 | | INTR_TYPE_OTHER_EVENT); |
| 5847 | } |
| 5848 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5849 | /* |
Sean Christopherson | 2c1f332 | 2020-04-15 10:55:14 -0700 | [diff] [blame] | 5850 | * Return true if L0 wants to handle an exit from L2 regardless of whether or not |
| 5851 | * 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] | 5852 | */ |
Sean Christopherson | 8e53324 | 2020-11-06 17:03:12 +0800 | [diff] [blame] | 5853 | static bool nested_vmx_l0_wants_exit(struct kvm_vcpu *vcpu, |
| 5854 | union vmx_exit_reason exit_reason) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5855 | { |
Sean Christopherson | 2c1f332 | 2020-04-15 10:55:14 -0700 | [diff] [blame] | 5856 | u32 intr_info; |
| 5857 | |
Sean Christopherson | 8e53324 | 2020-11-06 17:03:12 +0800 | [diff] [blame] | 5858 | switch ((u16)exit_reason.basic) { |
Sean Christopherson | 2c1f332 | 2020-04-15 10:55:14 -0700 | [diff] [blame] | 5859 | case EXIT_REASON_EXCEPTION_NMI: |
Sean Christopherson | 8791585 | 2020-04-15 13:34:54 -0700 | [diff] [blame] | 5860 | intr_info = vmx_get_intr_info(vcpu); |
Sean Christopherson | 2c1f332 | 2020-04-15 10:55:14 -0700 | [diff] [blame] | 5861 | if (is_nmi(intr_info)) |
| 5862 | return true; |
| 5863 | else if (is_page_fault(intr_info)) |
Sean Christopherson | 18712c1 | 2021-08-11 21:56:15 -0700 | [diff] [blame] | 5864 | return vcpu->arch.apf.host_apf_flags || |
| 5865 | vmx_need_pf_intercept(vcpu); |
Sean Christopherson | 2c1f332 | 2020-04-15 10:55:14 -0700 | [diff] [blame] | 5866 | else if (is_debug(intr_info) && |
| 5867 | vcpu->guest_debug & |
| 5868 | (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP)) |
| 5869 | return true; |
| 5870 | else if (is_breakpoint(intr_info) && |
| 5871 | vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP) |
| 5872 | return true; |
Sean Christopherson | b33bb78 | 2021-06-22 10:22:44 -0700 | [diff] [blame] | 5873 | else if (is_alignment_check(intr_info) && |
| 5874 | !vmx_guest_inject_ac(vcpu)) |
| 5875 | return true; |
Sean Christopherson | 2c1f332 | 2020-04-15 10:55:14 -0700 | [diff] [blame] | 5876 | return false; |
| 5877 | case EXIT_REASON_EXTERNAL_INTERRUPT: |
| 5878 | return true; |
| 5879 | case EXIT_REASON_MCE_DURING_VMENTRY: |
| 5880 | return true; |
| 5881 | case EXIT_REASON_EPT_VIOLATION: |
| 5882 | /* |
| 5883 | * L0 always deals with the EPT violation. If nested EPT is |
| 5884 | * used, and the nested mmu code discovers that the address is |
| 5885 | * missing in the guest EPT table (EPT12), the EPT violation |
| 5886 | * will be injected with nested_ept_inject_page_fault() |
| 5887 | */ |
| 5888 | return true; |
| 5889 | case EXIT_REASON_EPT_MISCONFIG: |
| 5890 | /* |
| 5891 | * L2 never uses directly L1's EPT, but rather L0's own EPT |
| 5892 | * table (shadow on EPT) or a merged EPT table that L0 built |
| 5893 | * (EPT on EPT). So any problems with the structure of the |
| 5894 | * table is L0's fault. |
| 5895 | */ |
| 5896 | return true; |
| 5897 | case EXIT_REASON_PREEMPTION_TIMER: |
| 5898 | return true; |
| 5899 | case EXIT_REASON_PML_FULL: |
Sean Christopherson | c3bb9a2 | 2021-02-12 16:50:07 -0800 | [diff] [blame] | 5900 | /* |
| 5901 | * PML is emulated for an L1 VMM and should never be enabled in |
| 5902 | * vmcs02, always "handle" PML_FULL by exiting to userspace. |
| 5903 | */ |
Sean Christopherson | 2c1f332 | 2020-04-15 10:55:14 -0700 | [diff] [blame] | 5904 | return true; |
| 5905 | case EXIT_REASON_VMFUNC: |
| 5906 | /* VM functions are emulated through L2->L0 vmexits. */ |
| 5907 | return true; |
Chenyi Qiang | 24a996a | 2021-09-14 17:50:41 +0800 | [diff] [blame] | 5908 | case EXIT_REASON_BUS_LOCK: |
| 5909 | /* |
| 5910 | * At present, bus lock VM exit is never exposed to L1. |
| 5911 | * Handle L2's bus locks in L0 directly. |
| 5912 | */ |
| 5913 | return true; |
Sean Christopherson | 2c1f332 | 2020-04-15 10:55:14 -0700 | [diff] [blame] | 5914 | default: |
| 5915 | break; |
| 5916 | } |
| 5917 | return false; |
| 5918 | } |
| 5919 | |
| 5920 | /* |
| 5921 | * Return 1 if L1 wants to intercept an exit from L2. Only call this when in |
| 5922 | * is_guest_mode (L2). |
| 5923 | */ |
Sean Christopherson | 8e53324 | 2020-11-06 17:03:12 +0800 | [diff] [blame] | 5924 | static bool nested_vmx_l1_wants_exit(struct kvm_vcpu *vcpu, |
| 5925 | union vmx_exit_reason exit_reason) |
Sean Christopherson | 2c1f332 | 2020-04-15 10:55:14 -0700 | [diff] [blame] | 5926 | { |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5927 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
Sean Christopherson | 9bd4af2 | 2020-04-21 00:53:27 -0700 | [diff] [blame] | 5928 | u32 intr_info; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5929 | |
Sean Christopherson | 8e53324 | 2020-11-06 17:03:12 +0800 | [diff] [blame] | 5930 | switch ((u16)exit_reason.basic) { |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5931 | case EXIT_REASON_EXCEPTION_NMI: |
Sean Christopherson | 8791585 | 2020-04-15 13:34:54 -0700 | [diff] [blame] | 5932 | intr_info = vmx_get_intr_info(vcpu); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5933 | if (is_nmi(intr_info)) |
Sean Christopherson | 2c1f332 | 2020-04-15 10:55:14 -0700 | [diff] [blame] | 5934 | return true; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5935 | else if (is_page_fault(intr_info)) |
Sean Christopherson | 2c1f332 | 2020-04-15 10:55:14 -0700 | [diff] [blame] | 5936 | return true; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5937 | return vmcs12->exception_bitmap & |
| 5938 | (1u << (intr_info & INTR_INFO_VECTOR_MASK)); |
| 5939 | case EXIT_REASON_EXTERNAL_INTERRUPT: |
Sean Christopherson | 2c1f332 | 2020-04-15 10:55:14 -0700 | [diff] [blame] | 5940 | return nested_exit_on_intr(vcpu); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5941 | case EXIT_REASON_TRIPLE_FAULT: |
| 5942 | return true; |
Xiaoyao Li | 9dadc2f | 2019-12-06 16:45:24 +0800 | [diff] [blame] | 5943 | case EXIT_REASON_INTERRUPT_WINDOW: |
| 5944 | return nested_cpu_has(vmcs12, CPU_BASED_INTR_WINDOW_EXITING); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5945 | case EXIT_REASON_NMI_WINDOW: |
Xiaoyao Li | 4e2a0bc | 2019-12-06 16:45:25 +0800 | [diff] [blame] | 5946 | return nested_cpu_has(vmcs12, CPU_BASED_NMI_WINDOW_EXITING); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5947 | case EXIT_REASON_TASK_SWITCH: |
| 5948 | return true; |
| 5949 | case EXIT_REASON_CPUID: |
| 5950 | return true; |
| 5951 | case EXIT_REASON_HLT: |
| 5952 | return nested_cpu_has(vmcs12, CPU_BASED_HLT_EXITING); |
| 5953 | case EXIT_REASON_INVD: |
| 5954 | return true; |
| 5955 | case EXIT_REASON_INVLPG: |
| 5956 | return nested_cpu_has(vmcs12, CPU_BASED_INVLPG_EXITING); |
| 5957 | case EXIT_REASON_RDPMC: |
| 5958 | return nested_cpu_has(vmcs12, CPU_BASED_RDPMC_EXITING); |
| 5959 | case EXIT_REASON_RDRAND: |
| 5960 | return nested_cpu_has2(vmcs12, SECONDARY_EXEC_RDRAND_EXITING); |
| 5961 | case EXIT_REASON_RDSEED: |
| 5962 | return nested_cpu_has2(vmcs12, SECONDARY_EXEC_RDSEED_EXITING); |
| 5963 | case EXIT_REASON_RDTSC: case EXIT_REASON_RDTSCP: |
| 5964 | return nested_cpu_has(vmcs12, CPU_BASED_RDTSC_EXITING); |
| 5965 | case EXIT_REASON_VMREAD: |
| 5966 | return nested_vmx_exit_handled_vmcs_access(vcpu, vmcs12, |
| 5967 | vmcs12->vmread_bitmap); |
| 5968 | case EXIT_REASON_VMWRITE: |
| 5969 | return nested_vmx_exit_handled_vmcs_access(vcpu, vmcs12, |
| 5970 | vmcs12->vmwrite_bitmap); |
| 5971 | case EXIT_REASON_VMCALL: case EXIT_REASON_VMCLEAR: |
| 5972 | case EXIT_REASON_VMLAUNCH: case EXIT_REASON_VMPTRLD: |
| 5973 | case EXIT_REASON_VMPTRST: case EXIT_REASON_VMRESUME: |
| 5974 | case EXIT_REASON_VMOFF: case EXIT_REASON_VMON: |
| 5975 | case EXIT_REASON_INVEPT: case EXIT_REASON_INVVPID: |
| 5976 | /* |
| 5977 | * VMX instructions trap unconditionally. This allows L1 to |
| 5978 | * emulate them for its L2 guest, i.e., allows 3-level nesting! |
| 5979 | */ |
| 5980 | return true; |
| 5981 | case EXIT_REASON_CR_ACCESS: |
| 5982 | return nested_vmx_exit_handled_cr(vcpu, vmcs12); |
| 5983 | case EXIT_REASON_DR_ACCESS: |
| 5984 | return nested_cpu_has(vmcs12, CPU_BASED_MOV_DR_EXITING); |
| 5985 | case EXIT_REASON_IO_INSTRUCTION: |
| 5986 | return nested_vmx_exit_handled_io(vcpu, vmcs12); |
| 5987 | case EXIT_REASON_GDTR_IDTR: case EXIT_REASON_LDTR_TR: |
| 5988 | return nested_cpu_has2(vmcs12, SECONDARY_EXEC_DESC); |
| 5989 | case EXIT_REASON_MSR_READ: |
| 5990 | case EXIT_REASON_MSR_WRITE: |
| 5991 | return nested_vmx_exit_handled_msr(vcpu, vmcs12, exit_reason); |
| 5992 | case EXIT_REASON_INVALID_STATE: |
| 5993 | return true; |
| 5994 | case EXIT_REASON_MWAIT_INSTRUCTION: |
| 5995 | return nested_cpu_has(vmcs12, CPU_BASED_MWAIT_EXITING); |
| 5996 | case EXIT_REASON_MONITOR_TRAP_FLAG: |
Oliver Upton | b045ae9 | 2020-04-14 22:47:45 +0000 | [diff] [blame] | 5997 | return nested_vmx_exit_handled_mtf(vmcs12); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5998 | case EXIT_REASON_MONITOR_INSTRUCTION: |
| 5999 | return nested_cpu_has(vmcs12, CPU_BASED_MONITOR_EXITING); |
| 6000 | case EXIT_REASON_PAUSE_INSTRUCTION: |
| 6001 | return nested_cpu_has(vmcs12, CPU_BASED_PAUSE_EXITING) || |
| 6002 | nested_cpu_has2(vmcs12, |
| 6003 | SECONDARY_EXEC_PAUSE_LOOP_EXITING); |
| 6004 | case EXIT_REASON_MCE_DURING_VMENTRY: |
Sean Christopherson | 2c1f332 | 2020-04-15 10:55:14 -0700 | [diff] [blame] | 6005 | return true; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6006 | case EXIT_REASON_TPR_BELOW_THRESHOLD: |
| 6007 | return nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW); |
| 6008 | case EXIT_REASON_APIC_ACCESS: |
| 6009 | case EXIT_REASON_APIC_WRITE: |
| 6010 | case EXIT_REASON_EOI_INDUCED: |
| 6011 | /* |
| 6012 | * The controls for "virtualize APIC accesses," "APIC- |
| 6013 | * register virtualization," and "virtual-interrupt |
| 6014 | * delivery" only come from vmcs12. |
| 6015 | */ |
| 6016 | return true; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6017 | case EXIT_REASON_INVPCID: |
| 6018 | return |
| 6019 | nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_INVPCID) && |
| 6020 | nested_cpu_has(vmcs12, CPU_BASED_INVLPG_EXITING); |
| 6021 | case EXIT_REASON_WBINVD: |
| 6022 | return nested_cpu_has2(vmcs12, SECONDARY_EXEC_WBINVD_EXITING); |
| 6023 | case EXIT_REASON_XSETBV: |
| 6024 | return true; |
| 6025 | case EXIT_REASON_XSAVES: case EXIT_REASON_XRSTORS: |
| 6026 | /* |
| 6027 | * This should never happen, since it is not possible to |
| 6028 | * set XSS to a non-zero value---neither in L1 nor in L2. |
| 6029 | * If if it were, XSS would have to be checked against |
| 6030 | * the XSS exit bitmap in vmcs12. |
| 6031 | */ |
| 6032 | return nested_cpu_has2(vmcs12, SECONDARY_EXEC_XSAVES); |
Tao Xu | bf653b7 | 2019-07-16 14:55:51 +0800 | [diff] [blame] | 6033 | case EXIT_REASON_UMWAIT: |
| 6034 | case EXIT_REASON_TPAUSE: |
| 6035 | return nested_cpu_has2(vmcs12, |
| 6036 | SECONDARY_EXEC_ENABLE_USR_WAIT_PAUSE); |
Sean Christopherson | 72add91 | 2021-04-12 16:21:42 +1200 | [diff] [blame] | 6037 | case EXIT_REASON_ENCLS: |
| 6038 | return nested_vmx_exit_handled_encls(vcpu, vmcs12); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6039 | default: |
| 6040 | return true; |
| 6041 | } |
| 6042 | } |
| 6043 | |
Sean Christopherson | 7b7bd87 | 2020-04-15 10:55:11 -0700 | [diff] [blame] | 6044 | /* |
| 6045 | * Conditionally reflect a VM-Exit into L1. Returns %true if the VM-Exit was |
| 6046 | * reflected into L1. |
| 6047 | */ |
Sean Christopherson | f47baae | 2020-04-15 10:55:16 -0700 | [diff] [blame] | 6048 | bool nested_vmx_reflect_vmexit(struct kvm_vcpu *vcpu) |
Sean Christopherson | 7b7bd87 | 2020-04-15 10:55:11 -0700 | [diff] [blame] | 6049 | { |
Sean Christopherson | fbdd502 | 2020-04-15 10:55:12 -0700 | [diff] [blame] | 6050 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Sean Christopherson | 8e53324 | 2020-11-06 17:03:12 +0800 | [diff] [blame] | 6051 | union vmx_exit_reason exit_reason = vmx->exit_reason; |
Sean Christopherson | 8779655 | 2020-04-22 17:11:27 -0700 | [diff] [blame] | 6052 | unsigned long exit_qual; |
| 6053 | u32 exit_intr_info; |
Sean Christopherson | fbdd502 | 2020-04-15 10:55:12 -0700 | [diff] [blame] | 6054 | |
| 6055 | WARN_ON_ONCE(vmx->nested.nested_run_pending); |
| 6056 | |
| 6057 | /* |
| 6058 | * Late nested VM-Fail shares the same flow as nested VM-Exit since KVM |
| 6059 | * has already loaded L2's state. |
| 6060 | */ |
| 6061 | if (unlikely(vmx->fail)) { |
| 6062 | trace_kvm_nested_vmenter_failed( |
| 6063 | "hardware VM-instruction error: ", |
| 6064 | vmcs_read32(VM_INSTRUCTION_ERROR)); |
| 6065 | exit_intr_info = 0; |
| 6066 | exit_qual = 0; |
| 6067 | goto reflect_vmexit; |
| 6068 | } |
Sean Christopherson | 7b7bd87 | 2020-04-15 10:55:11 -0700 | [diff] [blame] | 6069 | |
David Edmondson | 0a62a03 | 2021-09-20 11:37:35 +0100 | [diff] [blame] | 6070 | trace_kvm_nested_vmexit(vcpu, KVM_ISA_VMX); |
Sean Christopherson | 236871b | 2020-04-15 10:55:13 -0700 | [diff] [blame] | 6071 | |
Sean Christopherson | 2c1f332 | 2020-04-15 10:55:14 -0700 | [diff] [blame] | 6072 | /* If L0 (KVM) wants the exit, it trumps L1's desires. */ |
| 6073 | if (nested_vmx_l0_wants_exit(vcpu, exit_reason)) |
| 6074 | return false; |
| 6075 | |
| 6076 | /* If L1 doesn't want the exit, handle it in L0. */ |
| 6077 | if (!nested_vmx_l1_wants_exit(vcpu, exit_reason)) |
Sean Christopherson | 7b7bd87 | 2020-04-15 10:55:11 -0700 | [diff] [blame] | 6078 | return false; |
| 6079 | |
| 6080 | /* |
Sean Christopherson | 1d28306 | 2020-04-15 10:55:15 -0700 | [diff] [blame] | 6081 | * vmcs.VM_EXIT_INTR_INFO is only valid for EXCEPTION_NMI exits. For |
| 6082 | * EXTERNAL_INTERRUPT, the value for vmcs12->vm_exit_intr_info would |
| 6083 | * need to be synthesized by querying the in-kernel LAPIC, but external |
| 6084 | * interrupts are never reflected to L1 so it's a non-issue. |
Sean Christopherson | 7b7bd87 | 2020-04-15 10:55:11 -0700 | [diff] [blame] | 6085 | */ |
Sean Christopherson | 02f1965 | 2020-09-23 13:13:49 -0700 | [diff] [blame] | 6086 | exit_intr_info = vmx_get_intr_info(vcpu); |
Sean Christopherson | f315f2b | 2020-09-23 13:13:45 -0700 | [diff] [blame] | 6087 | if (is_exception_with_error_code(exit_intr_info)) { |
Sean Christopherson | 7b7bd87 | 2020-04-15 10:55:11 -0700 | [diff] [blame] | 6088 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
| 6089 | |
| 6090 | vmcs12->vm_exit_intr_error_code = |
| 6091 | vmcs_read32(VM_EXIT_INTR_ERROR_CODE); |
| 6092 | } |
Sean Christopherson | 02f1965 | 2020-09-23 13:13:49 -0700 | [diff] [blame] | 6093 | exit_qual = vmx_get_exit_qual(vcpu); |
Sean Christopherson | 7b7bd87 | 2020-04-15 10:55:11 -0700 | [diff] [blame] | 6094 | |
Sean Christopherson | fbdd502 | 2020-04-15 10:55:12 -0700 | [diff] [blame] | 6095 | reflect_vmexit: |
Sean Christopherson | 8e53324 | 2020-11-06 17:03:12 +0800 | [diff] [blame] | 6096 | nested_vmx_vmexit(vcpu, exit_reason.full, exit_intr_info, exit_qual); |
Sean Christopherson | 7b7bd87 | 2020-04-15 10:55:11 -0700 | [diff] [blame] | 6097 | return true; |
| 6098 | } |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6099 | |
| 6100 | static int vmx_get_nested_state(struct kvm_vcpu *vcpu, |
| 6101 | struct kvm_nested_state __user *user_kvm_nested_state, |
| 6102 | u32 user_data_size) |
| 6103 | { |
| 6104 | struct vcpu_vmx *vmx; |
| 6105 | struct vmcs12 *vmcs12; |
| 6106 | struct kvm_nested_state kvm_state = { |
| 6107 | .flags = 0, |
Liran Alon | 6ca00df | 2019-06-16 15:03:10 +0300 | [diff] [blame] | 6108 | .format = KVM_STATE_NESTED_FORMAT_VMX, |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6109 | .size = sizeof(kvm_state), |
Peter Shier | 850448f | 2020-05-26 14:51:06 -0700 | [diff] [blame] | 6110 | .hdr.vmx.flags = 0, |
Yu Zhang | 64c7850 | 2021-09-30 01:51:53 +0800 | [diff] [blame] | 6111 | .hdr.vmx.vmxon_pa = INVALID_GPA, |
| 6112 | .hdr.vmx.vmcs12_pa = INVALID_GPA, |
Peter Shier | 850448f | 2020-05-26 14:51:06 -0700 | [diff] [blame] | 6113 | .hdr.vmx.preemption_timer_deadline = 0, |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6114 | }; |
Liran Alon | 6ca00df | 2019-06-16 15:03:10 +0300 | [diff] [blame] | 6115 | struct kvm_vmx_nested_state_data __user *user_vmx_nested_state = |
| 6116 | &user_kvm_nested_state->data.vmx[0]; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6117 | |
| 6118 | if (!vcpu) |
Liran Alon | 6ca00df | 2019-06-16 15:03:10 +0300 | [diff] [blame] | 6119 | return kvm_state.size + sizeof(*user_vmx_nested_state); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6120 | |
| 6121 | vmx = to_vmx(vcpu); |
| 6122 | vmcs12 = get_vmcs12(vcpu); |
| 6123 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6124 | if (nested_vmx_allowed(vcpu) && |
| 6125 | (vmx->nested.vmxon || vmx->nested.smm.vmxon)) { |
Liran Alon | 6ca00df | 2019-06-16 15:03:10 +0300 | [diff] [blame] | 6126 | kvm_state.hdr.vmx.vmxon_pa = vmx->nested.vmxon_ptr; |
| 6127 | kvm_state.hdr.vmx.vmcs12_pa = vmx->nested.current_vmptr; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6128 | |
| 6129 | if (vmx_has_valid_vmcs12(vcpu)) { |
Liran Alon | 6ca00df | 2019-06-16 15:03:10 +0300 | [diff] [blame] | 6130 | kvm_state.size += sizeof(user_vmx_nested_state->vmcs12); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6131 | |
Vitaly Kuznetsov | 2784996 | 2021-05-26 15:20:20 +0200 | [diff] [blame] | 6132 | /* 'hv_evmcs_vmptr' can also be EVMPTR_MAP_PENDING here */ |
| 6133 | if (vmx->nested.hv_evmcs_vmptr != EVMPTR_INVALID) |
Liran Alon | 323d73a | 2019-06-26 16:09:27 +0300 | [diff] [blame] | 6134 | kvm_state.flags |= KVM_STATE_NESTED_EVMCS; |
| 6135 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6136 | if (is_guest_mode(vcpu) && |
| 6137 | nested_cpu_has_shadow_vmcs(vmcs12) && |
Yu Zhang | 64c7850 | 2021-09-30 01:51:53 +0800 | [diff] [blame] | 6138 | vmcs12->vmcs_link_pointer != INVALID_GPA) |
Liran Alon | 6ca00df | 2019-06-16 15:03:10 +0300 | [diff] [blame] | 6139 | kvm_state.size += sizeof(user_vmx_nested_state->shadow_vmcs12); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6140 | } |
| 6141 | |
| 6142 | if (vmx->nested.smm.vmxon) |
Liran Alon | 6ca00df | 2019-06-16 15:03:10 +0300 | [diff] [blame] | 6143 | kvm_state.hdr.vmx.smm.flags |= KVM_STATE_NESTED_SMM_VMXON; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6144 | |
| 6145 | if (vmx->nested.smm.guest_mode) |
Liran Alon | 6ca00df | 2019-06-16 15:03:10 +0300 | [diff] [blame] | 6146 | kvm_state.hdr.vmx.smm.flags |= KVM_STATE_NESTED_SMM_GUEST_MODE; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6147 | |
| 6148 | if (is_guest_mode(vcpu)) { |
| 6149 | kvm_state.flags |= KVM_STATE_NESTED_GUEST_MODE; |
| 6150 | |
| 6151 | if (vmx->nested.nested_run_pending) |
| 6152 | kvm_state.flags |= KVM_STATE_NESTED_RUN_PENDING; |
Oliver Upton | 5ef8acb | 2020-02-07 02:36:07 -0800 | [diff] [blame] | 6153 | |
| 6154 | if (vmx->nested.mtf_pending) |
| 6155 | kvm_state.flags |= KVM_STATE_NESTED_MTF_PENDING; |
Peter Shier | 850448f | 2020-05-26 14:51:06 -0700 | [diff] [blame] | 6156 | |
| 6157 | if (nested_cpu_has_preemption_timer(vmcs12) && |
| 6158 | vmx->nested.has_preemption_timer_deadline) { |
| 6159 | kvm_state.hdr.vmx.flags |= |
| 6160 | KVM_STATE_VMX_PREEMPTION_TIMER_DEADLINE; |
| 6161 | kvm_state.hdr.vmx.preemption_timer_deadline = |
| 6162 | vmx->nested.preemption_timer_deadline; |
| 6163 | } |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6164 | } |
| 6165 | } |
| 6166 | |
| 6167 | if (user_data_size < kvm_state.size) |
| 6168 | goto out; |
| 6169 | |
| 6170 | if (copy_to_user(user_kvm_nested_state, &kvm_state, sizeof(kvm_state))) |
| 6171 | return -EFAULT; |
| 6172 | |
| 6173 | if (!vmx_has_valid_vmcs12(vcpu)) |
| 6174 | goto out; |
| 6175 | |
| 6176 | /* |
| 6177 | * When running L2, the authoritative vmcs12 state is in the |
| 6178 | * vmcs02. When running L1, the authoritative vmcs12 state is |
| 6179 | * in the shadow or enlightened vmcs linked to vmcs01, unless |
Sean Christopherson | 3731905ef | 2019-05-07 08:36:27 -0700 | [diff] [blame] | 6180 | * need_vmcs12_to_shadow_sync is set, in which case, the authoritative |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6181 | * vmcs12 state is in the vmcs12 already. |
| 6182 | */ |
| 6183 | if (is_guest_mode(vcpu)) { |
Sean Christopherson | 3731905ef | 2019-05-07 08:36:27 -0700 | [diff] [blame] | 6184 | sync_vmcs02_to_vmcs12(vcpu, vmcs12); |
Sean Christopherson | 7952d76 | 2019-05-07 08:36:29 -0700 | [diff] [blame] | 6185 | sync_vmcs02_to_vmcs12_rare(vcpu, vmcs12); |
Maxim Levitsky | d51e1d3 | 2021-01-14 22:54:47 +0200 | [diff] [blame] | 6186 | } else { |
| 6187 | copy_vmcs02_to_vmcs12_rare(vcpu, get_vmcs12(vcpu)); |
| 6188 | if (!vmx->nested.need_vmcs12_to_shadow_sync) { |
Vitaly Kuznetsov | 1e9dfbd | 2021-05-26 15:20:16 +0200 | [diff] [blame] | 6189 | if (evmptr_is_valid(vmx->nested.hv_evmcs_vmptr)) |
Vitaly Kuznetsov | d6bf71a | 2021-05-26 15:20:22 +0200 | [diff] [blame] | 6190 | /* |
| 6191 | * L1 hypervisor is not obliged to keep eVMCS |
| 6192 | * clean fields data always up-to-date while |
| 6193 | * not in guest mode, 'hv_clean_fields' is only |
| 6194 | * supposed to be actual upon vmentry so we need |
| 6195 | * to ignore it here and do full copy. |
| 6196 | */ |
| 6197 | copy_enlightened_to_vmcs12(vmx, 0); |
Maxim Levitsky | d51e1d3 | 2021-01-14 22:54:47 +0200 | [diff] [blame] | 6198 | else if (enable_shadow_vmcs) |
| 6199 | copy_shadow_to_vmcs12(vmx); |
| 6200 | } |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6201 | } |
| 6202 | |
Liran Alon | 6ca00df | 2019-06-16 15:03:10 +0300 | [diff] [blame] | 6203 | BUILD_BUG_ON(sizeof(user_vmx_nested_state->vmcs12) < VMCS12_SIZE); |
| 6204 | BUILD_BUG_ON(sizeof(user_vmx_nested_state->shadow_vmcs12) < VMCS12_SIZE); |
| 6205 | |
Tom Roeder | 3a33d03 | 2019-01-24 13:48:20 -0800 | [diff] [blame] | 6206 | /* |
| 6207 | * Copy over the full allocated size of vmcs12 rather than just the size |
| 6208 | * of the struct. |
| 6209 | */ |
Liran Alon | 6ca00df | 2019-06-16 15:03:10 +0300 | [diff] [blame] | 6210 | if (copy_to_user(user_vmx_nested_state->vmcs12, vmcs12, VMCS12_SIZE)) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6211 | return -EFAULT; |
| 6212 | |
| 6213 | if (nested_cpu_has_shadow_vmcs(vmcs12) && |
Yu Zhang | 64c7850 | 2021-09-30 01:51:53 +0800 | [diff] [blame] | 6214 | vmcs12->vmcs_link_pointer != INVALID_GPA) { |
Liran Alon | 6ca00df | 2019-06-16 15:03:10 +0300 | [diff] [blame] | 6215 | if (copy_to_user(user_vmx_nested_state->shadow_vmcs12, |
Tom Roeder | 3a33d03 | 2019-01-24 13:48:20 -0800 | [diff] [blame] | 6216 | get_shadow_vmcs12(vcpu), VMCS12_SIZE)) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6217 | return -EFAULT; |
| 6218 | } |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6219 | out: |
| 6220 | return kvm_state.size; |
| 6221 | } |
| 6222 | |
| 6223 | /* |
| 6224 | * Forcibly leave nested mode in order to be able to reset the VCPU later on. |
| 6225 | */ |
| 6226 | void vmx_leave_nested(struct kvm_vcpu *vcpu) |
| 6227 | { |
| 6228 | if (is_guest_mode(vcpu)) { |
| 6229 | to_vmx(vcpu)->nested.nested_run_pending = 0; |
| 6230 | nested_vmx_vmexit(vcpu, -1, 0, 0); |
| 6231 | } |
| 6232 | free_nested(vcpu); |
| 6233 | } |
| 6234 | |
| 6235 | static int vmx_set_nested_state(struct kvm_vcpu *vcpu, |
| 6236 | struct kvm_nested_state __user *user_kvm_nested_state, |
| 6237 | struct kvm_nested_state *kvm_state) |
| 6238 | { |
| 6239 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 6240 | struct vmcs12 *vmcs12; |
Sean Christopherson | 68cda40 | 2020-05-11 15:05:29 -0700 | [diff] [blame] | 6241 | enum vm_entry_failure_code ignored; |
Liran Alon | 6ca00df | 2019-06-16 15:03:10 +0300 | [diff] [blame] | 6242 | struct kvm_vmx_nested_state_data __user *user_vmx_nested_state = |
| 6243 | &user_kvm_nested_state->data.vmx[0]; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6244 | int ret; |
| 6245 | |
Liran Alon | 6ca00df | 2019-06-16 15:03:10 +0300 | [diff] [blame] | 6246 | if (kvm_state->format != KVM_STATE_NESTED_FORMAT_VMX) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6247 | return -EINVAL; |
| 6248 | |
Yu Zhang | 64c7850 | 2021-09-30 01:51:53 +0800 | [diff] [blame] | 6249 | if (kvm_state->hdr.vmx.vmxon_pa == INVALID_GPA) { |
Liran Alon | 6ca00df | 2019-06-16 15:03:10 +0300 | [diff] [blame] | 6250 | if (kvm_state->hdr.vmx.smm.flags) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6251 | return -EINVAL; |
| 6252 | |
Yu Zhang | 64c7850 | 2021-09-30 01:51:53 +0800 | [diff] [blame] | 6253 | if (kvm_state->hdr.vmx.vmcs12_pa != INVALID_GPA) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6254 | return -EINVAL; |
| 6255 | |
Liran Alon | 323d73a | 2019-06-26 16:09:27 +0300 | [diff] [blame] | 6256 | /* |
| 6257 | * KVM_STATE_NESTED_EVMCS used to signal that KVM should |
| 6258 | * enable eVMCS capability on vCPU. However, since then |
| 6259 | * code was changed such that flag signals vmcs12 should |
| 6260 | * be copied into eVMCS in guest memory. |
| 6261 | * |
| 6262 | * To preserve backwards compatability, allow user |
| 6263 | * to set this flag even when there is no VMXON region. |
| 6264 | */ |
Paolo Bonzini | 9fd5887 | 2019-06-19 16:52:27 +0200 | [diff] [blame] | 6265 | if (kvm_state->flags & ~KVM_STATE_NESTED_EVMCS) |
| 6266 | return -EINVAL; |
| 6267 | } else { |
| 6268 | if (!nested_vmx_allowed(vcpu)) |
| 6269 | return -EINVAL; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6270 | |
Paolo Bonzini | 9fd5887 | 2019-06-19 16:52:27 +0200 | [diff] [blame] | 6271 | if (!page_address_valid(vcpu, kvm_state->hdr.vmx.vmxon_pa)) |
| 6272 | return -EINVAL; |
Liran Alon | 323d73a | 2019-06-26 16:09:27 +0300 | [diff] [blame] | 6273 | } |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6274 | |
Liran Alon | 6ca00df | 2019-06-16 15:03:10 +0300 | [diff] [blame] | 6275 | 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] | 6276 | (kvm_state->flags & KVM_STATE_NESTED_GUEST_MODE)) |
| 6277 | return -EINVAL; |
| 6278 | |
Liran Alon | 6ca00df | 2019-06-16 15:03:10 +0300 | [diff] [blame] | 6279 | if (kvm_state->hdr.vmx.smm.flags & |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6280 | ~(KVM_STATE_NESTED_SMM_GUEST_MODE | KVM_STATE_NESTED_SMM_VMXON)) |
| 6281 | return -EINVAL; |
| 6282 | |
Paolo Bonzini | 5e105c8 | 2020-07-27 08:55:09 -0400 | [diff] [blame] | 6283 | if (kvm_state->hdr.vmx.flags & ~KVM_STATE_VMX_PREEMPTION_TIMER_DEADLINE) |
| 6284 | return -EINVAL; |
| 6285 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6286 | /* |
| 6287 | * SMM temporarily disables VMX, so we cannot be in guest mode, |
| 6288 | * nor can VMLAUNCH/VMRESUME be pending. Outside SMM, SMM flags |
| 6289 | * must be zero. |
| 6290 | */ |
Liran Alon | 65b712f1 | 2019-06-25 14:26:42 +0300 | [diff] [blame] | 6291 | if (is_smm(vcpu) ? |
| 6292 | (kvm_state->flags & |
| 6293 | (KVM_STATE_NESTED_GUEST_MODE | KVM_STATE_NESTED_RUN_PENDING)) |
| 6294 | : kvm_state->hdr.vmx.smm.flags) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6295 | return -EINVAL; |
| 6296 | |
Liran Alon | 6ca00df | 2019-06-16 15:03:10 +0300 | [diff] [blame] | 6297 | if ((kvm_state->hdr.vmx.smm.flags & KVM_STATE_NESTED_SMM_GUEST_MODE) && |
| 6298 | !(kvm_state->hdr.vmx.smm.flags & KVM_STATE_NESTED_SMM_VMXON)) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6299 | return -EINVAL; |
| 6300 | |
Liran Alon | 323d73a | 2019-06-26 16:09:27 +0300 | [diff] [blame] | 6301 | if ((kvm_state->flags & KVM_STATE_NESTED_EVMCS) && |
| 6302 | (!nested_vmx_allowed(vcpu) || !vmx->nested.enlightened_vmcs_enabled)) |
Paolo Bonzini | 9fd5887 | 2019-06-19 16:52:27 +0200 | [diff] [blame] | 6303 | return -EINVAL; |
| 6304 | |
Liran Alon | 323d73a | 2019-06-26 16:09:27 +0300 | [diff] [blame] | 6305 | vmx_leave_nested(vcpu); |
Paolo Bonzini | 9fd5887 | 2019-06-19 16:52:27 +0200 | [diff] [blame] | 6306 | |
Yu Zhang | 64c7850 | 2021-09-30 01:51:53 +0800 | [diff] [blame] | 6307 | if (kvm_state->hdr.vmx.vmxon_pa == INVALID_GPA) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6308 | return 0; |
| 6309 | |
Liran Alon | 6ca00df | 2019-06-16 15:03:10 +0300 | [diff] [blame] | 6310 | vmx->nested.vmxon_ptr = kvm_state->hdr.vmx.vmxon_pa; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6311 | ret = enter_vmx_operation(vcpu); |
| 6312 | if (ret) |
| 6313 | return ret; |
| 6314 | |
Paolo Bonzini | 0f02bd0 | 2020-07-27 09:00:37 -0400 | [diff] [blame] | 6315 | /* Empty 'VMXON' state is permitted if no VMCS loaded */ |
| 6316 | if (kvm_state->size < sizeof(*kvm_state) + sizeof(*vmcs12)) { |
| 6317 | /* See vmx_has_valid_vmcs12. */ |
| 6318 | if ((kvm_state->flags & KVM_STATE_NESTED_GUEST_MODE) || |
| 6319 | (kvm_state->flags & KVM_STATE_NESTED_EVMCS) || |
Yu Zhang | 64c7850 | 2021-09-30 01:51:53 +0800 | [diff] [blame] | 6320 | (kvm_state->hdr.vmx.vmcs12_pa != INVALID_GPA)) |
Paolo Bonzini | 0f02bd0 | 2020-07-27 09:00:37 -0400 | [diff] [blame] | 6321 | return -EINVAL; |
| 6322 | else |
| 6323 | return 0; |
| 6324 | } |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6325 | |
Yu Zhang | 64c7850 | 2021-09-30 01:51:53 +0800 | [diff] [blame] | 6326 | if (kvm_state->hdr.vmx.vmcs12_pa != INVALID_GPA) { |
Liran Alon | 6ca00df | 2019-06-16 15:03:10 +0300 | [diff] [blame] | 6327 | if (kvm_state->hdr.vmx.vmcs12_pa == kvm_state->hdr.vmx.vmxon_pa || |
| 6328 | !page_address_valid(vcpu, kvm_state->hdr.vmx.vmcs12_pa)) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6329 | return -EINVAL; |
| 6330 | |
Liran Alon | 6ca00df | 2019-06-16 15:03:10 +0300 | [diff] [blame] | 6331 | set_current_vmptr(vmx, kvm_state->hdr.vmx.vmcs12_pa); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6332 | } else if (kvm_state->flags & KVM_STATE_NESTED_EVMCS) { |
| 6333 | /* |
Vitaly Kuznetsov | e942dbf | 2020-03-09 16:52:12 +0100 | [diff] [blame] | 6334 | * nested_vmx_handle_enlightened_vmptrld() cannot be called |
| 6335 | * directly from here as HV_X64_MSR_VP_ASSIST_PAGE may not be |
| 6336 | * restored yet. EVMCS will be mapped from |
| 6337 | * nested_get_vmcs12_pages(). |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6338 | */ |
Vitaly Kuznetsov | 2784996 | 2021-05-26 15:20:20 +0200 | [diff] [blame] | 6339 | vmx->nested.hv_evmcs_vmptr = EVMPTR_MAP_PENDING; |
Paolo Bonzini | 729c15c | 2020-09-22 06:53:57 -0400 | [diff] [blame] | 6340 | kvm_make_request(KVM_REQ_GET_NESTED_STATE_PAGES, vcpu); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6341 | } else { |
| 6342 | return -EINVAL; |
| 6343 | } |
| 6344 | |
Liran Alon | 6ca00df | 2019-06-16 15:03:10 +0300 | [diff] [blame] | 6345 | if (kvm_state->hdr.vmx.smm.flags & KVM_STATE_NESTED_SMM_VMXON) { |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6346 | vmx->nested.smm.vmxon = true; |
| 6347 | vmx->nested.vmxon = false; |
| 6348 | |
Liran Alon | 6ca00df | 2019-06-16 15:03:10 +0300 | [diff] [blame] | 6349 | 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] | 6350 | vmx->nested.smm.guest_mode = true; |
| 6351 | } |
| 6352 | |
| 6353 | vmcs12 = get_vmcs12(vcpu); |
Liran Alon | 6ca00df | 2019-06-16 15:03:10 +0300 | [diff] [blame] | 6354 | if (copy_from_user(vmcs12, user_vmx_nested_state->vmcs12, sizeof(*vmcs12))) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6355 | return -EFAULT; |
| 6356 | |
| 6357 | if (vmcs12->hdr.revision_id != VMCS12_REVISION) |
| 6358 | return -EINVAL; |
| 6359 | |
| 6360 | if (!(kvm_state->flags & KVM_STATE_NESTED_GUEST_MODE)) |
| 6361 | return 0; |
| 6362 | |
Sean Christopherson | 21be4ca | 2019-05-08 11:04:32 -0700 | [diff] [blame] | 6363 | vmx->nested.nested_run_pending = |
| 6364 | !!(kvm_state->flags & KVM_STATE_NESTED_RUN_PENDING); |
| 6365 | |
Oliver Upton | 5ef8acb | 2020-02-07 02:36:07 -0800 | [diff] [blame] | 6366 | vmx->nested.mtf_pending = |
| 6367 | !!(kvm_state->flags & KVM_STATE_NESTED_MTF_PENDING); |
| 6368 | |
Sean Christopherson | 21be4ca | 2019-05-08 11:04:32 -0700 | [diff] [blame] | 6369 | ret = -EINVAL; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6370 | if (nested_cpu_has_shadow_vmcs(vmcs12) && |
Yu Zhang | 64c7850 | 2021-09-30 01:51:53 +0800 | [diff] [blame] | 6371 | vmcs12->vmcs_link_pointer != INVALID_GPA) { |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6372 | struct vmcs12 *shadow_vmcs12 = get_shadow_vmcs12(vcpu); |
| 6373 | |
Liran Alon | 6ca00df | 2019-06-16 15:03:10 +0300 | [diff] [blame] | 6374 | if (kvm_state->size < |
| 6375 | sizeof(*kvm_state) + |
| 6376 | sizeof(user_vmx_nested_state->vmcs12) + sizeof(*shadow_vmcs12)) |
Sean Christopherson | 21be4ca | 2019-05-08 11:04:32 -0700 | [diff] [blame] | 6377 | goto error_guest_mode; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6378 | |
| 6379 | if (copy_from_user(shadow_vmcs12, |
Liran Alon | 6ca00df | 2019-06-16 15:03:10 +0300 | [diff] [blame] | 6380 | user_vmx_nested_state->shadow_vmcs12, |
| 6381 | sizeof(*shadow_vmcs12))) { |
Sean Christopherson | 21be4ca | 2019-05-08 11:04:32 -0700 | [diff] [blame] | 6382 | ret = -EFAULT; |
| 6383 | goto error_guest_mode; |
| 6384 | } |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6385 | |
| 6386 | if (shadow_vmcs12->hdr.revision_id != VMCS12_REVISION || |
| 6387 | !shadow_vmcs12->hdr.shadow_vmcs) |
Sean Christopherson | 21be4ca | 2019-05-08 11:04:32 -0700 | [diff] [blame] | 6388 | goto error_guest_mode; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6389 | } |
| 6390 | |
Paolo Bonzini | 83d31e5 | 2020-07-09 13:12:09 -0400 | [diff] [blame] | 6391 | vmx->nested.has_preemption_timer_deadline = false; |
Peter Shier | 850448f | 2020-05-26 14:51:06 -0700 | [diff] [blame] | 6392 | if (kvm_state->hdr.vmx.flags & KVM_STATE_VMX_PREEMPTION_TIMER_DEADLINE) { |
| 6393 | vmx->nested.has_preemption_timer_deadline = true; |
| 6394 | vmx->nested.preemption_timer_deadline = |
| 6395 | kvm_state->hdr.vmx.preemption_timer_deadline; |
| 6396 | } |
| 6397 | |
Sean Christopherson | 5478ba3 | 2019-04-11 12:18:06 -0700 | [diff] [blame] | 6398 | if (nested_vmx_check_controls(vcpu, vmcs12) || |
| 6399 | nested_vmx_check_host_state(vcpu, vmcs12) || |
Sean Christopherson | 68cda40 | 2020-05-11 15:05:29 -0700 | [diff] [blame] | 6400 | nested_vmx_check_guest_state(vcpu, vmcs12, &ignored)) |
Sean Christopherson | 21be4ca | 2019-05-08 11:04:32 -0700 | [diff] [blame] | 6401 | goto error_guest_mode; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6402 | |
| 6403 | vmx->nested.dirty_vmcs12 = true; |
Vitaly Kuznetsov | ed2a480 | 2021-11-29 10:47:03 +0100 | [diff] [blame^] | 6404 | vmx->nested.force_msr_bitmap_recalc = true; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6405 | ret = nested_vmx_enter_non_root_mode(vcpu, false); |
Sean Christopherson | 21be4ca | 2019-05-08 11:04:32 -0700 | [diff] [blame] | 6406 | if (ret) |
| 6407 | goto error_guest_mode; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6408 | |
| 6409 | return 0; |
Sean Christopherson | 21be4ca | 2019-05-08 11:04:32 -0700 | [diff] [blame] | 6410 | |
| 6411 | error_guest_mode: |
| 6412 | vmx->nested.nested_run_pending = 0; |
| 6413 | return ret; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6414 | } |
| 6415 | |
Xiaoyao Li | 1b84292 | 2019-10-20 17:11:01 +0800 | [diff] [blame] | 6416 | void nested_vmx_set_vmcs_shadowing_bitmap(void) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6417 | { |
| 6418 | if (enable_shadow_vmcs) { |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6419 | vmcs_write64(VMREAD_BITMAP, __pa(vmx_vmread_bitmap)); |
Sean Christopherson | fadcead | 2019-05-07 08:36:23 -0700 | [diff] [blame] | 6420 | vmcs_write64(VMWRITE_BITMAP, __pa(vmx_vmwrite_bitmap)); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6421 | } |
| 6422 | } |
| 6423 | |
| 6424 | /* |
Sean Christopherson | ba1f824 | 2021-06-18 14:46:58 -0700 | [diff] [blame] | 6425 | * Indexing into the vmcs12 uses the VMCS encoding rotated left by 6. Undo |
| 6426 | * that madness to get the encoding for comparison. |
| 6427 | */ |
| 6428 | #define VMCS12_IDX_TO_ENC(idx) ((u16)(((u16)(idx) >> 6) | ((u16)(idx) << 10))) |
| 6429 | |
| 6430 | static u64 nested_vmx_calc_vmcs_enum_msr(void) |
| 6431 | { |
| 6432 | /* |
| 6433 | * Note these are the so called "index" of the VMCS field encoding, not |
| 6434 | * the index into vmcs12. |
| 6435 | */ |
| 6436 | unsigned int max_idx, idx; |
| 6437 | int i; |
| 6438 | |
| 6439 | /* |
| 6440 | * For better or worse, KVM allows VMREAD/VMWRITE to all fields in |
| 6441 | * vmcs12, regardless of whether or not the associated feature is |
| 6442 | * exposed to L1. Simply find the field with the highest index. |
| 6443 | */ |
| 6444 | max_idx = 0; |
| 6445 | for (i = 0; i < nr_vmcs12_fields; i++) { |
| 6446 | /* The vmcs12 table is very, very sparsely populated. */ |
| 6447 | if (!vmcs_field_to_offset_table[i]) |
| 6448 | continue; |
| 6449 | |
| 6450 | idx = vmcs_field_index(VMCS12_IDX_TO_ENC(i)); |
| 6451 | if (idx > max_idx) |
| 6452 | max_idx = idx; |
| 6453 | } |
| 6454 | |
| 6455 | return (u64)max_idx << VMCS_FIELD_INDEX_SHIFT; |
| 6456 | } |
| 6457 | |
| 6458 | /* |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6459 | * nested_vmx_setup_ctls_msrs() sets up variables containing the values to be |
| 6460 | * returned for the various VMX controls MSRs when nested VMX is enabled. |
| 6461 | * The same values should also be used to verify that vmcs12 control fields are |
| 6462 | * valid during nested entry from L1 to L2. |
| 6463 | * Each of these control msrs has a low and high 32-bit half: A low bit is on |
| 6464 | * if the corresponding bit in the (32-bit) control field *must* be on, and a |
| 6465 | * bit in the high half is on if the corresponding bit in the control field |
| 6466 | * may be on. See also vmx_control_verify(). |
| 6467 | */ |
Vitaly Kuznetsov | a444326 | 2020-02-20 18:22:04 +0100 | [diff] [blame] | 6468 | 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] | 6469 | { |
| 6470 | /* |
| 6471 | * Note that as a general rule, the high half of the MSRs (bits in |
| 6472 | * the control fields which may be 1) should be initialized by the |
| 6473 | * intersection of the underlying hardware's MSR (i.e., features which |
| 6474 | * can be supported) and the list of features we want to expose - |
| 6475 | * because they are known to be properly supported in our code. |
| 6476 | * Also, usually, the low half of the MSRs (bits which must be 1) can |
| 6477 | * be set to 0, meaning that L1 may turn off any of these bits. The |
| 6478 | * reason is that if one of these bits is necessary, it will appear |
| 6479 | * in vmcs01 and prepare_vmcs02, when it bitwise-or's the control |
| 6480 | * fields of vmcs01 and vmcs02, will turn these bits off - and |
Sean Christopherson | 2c1f332 | 2020-04-15 10:55:14 -0700 | [diff] [blame] | 6481 | * nested_vmx_l1_wants_exit() will not pass related exits to L1. |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6482 | * These rules have exceptions below. |
| 6483 | */ |
| 6484 | |
| 6485 | /* pin-based controls */ |
| 6486 | rdmsr(MSR_IA32_VMX_PINBASED_CTLS, |
| 6487 | msrs->pinbased_ctls_low, |
| 6488 | msrs->pinbased_ctls_high); |
| 6489 | msrs->pinbased_ctls_low |= |
| 6490 | PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR; |
| 6491 | msrs->pinbased_ctls_high &= |
| 6492 | PIN_BASED_EXT_INTR_MASK | |
| 6493 | PIN_BASED_NMI_EXITING | |
| 6494 | PIN_BASED_VIRTUAL_NMIS | |
Vitaly Kuznetsov | a444326 | 2020-02-20 18:22:04 +0100 | [diff] [blame] | 6495 | (enable_apicv ? PIN_BASED_POSTED_INTR : 0); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6496 | msrs->pinbased_ctls_high |= |
| 6497 | PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR | |
| 6498 | PIN_BASED_VMX_PREEMPTION_TIMER; |
| 6499 | |
| 6500 | /* exit controls */ |
| 6501 | rdmsr(MSR_IA32_VMX_EXIT_CTLS, |
| 6502 | msrs->exit_ctls_low, |
| 6503 | msrs->exit_ctls_high); |
| 6504 | msrs->exit_ctls_low = |
| 6505 | VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR; |
| 6506 | |
| 6507 | msrs->exit_ctls_high &= |
| 6508 | #ifdef CONFIG_X86_64 |
| 6509 | VM_EXIT_HOST_ADDR_SPACE_SIZE | |
| 6510 | #endif |
Chenyi Qiang | efc8313 | 2020-08-28 16:56:18 +0800 | [diff] [blame] | 6511 | VM_EXIT_LOAD_IA32_PAT | VM_EXIT_SAVE_IA32_PAT | |
| 6512 | VM_EXIT_CLEAR_BNDCFGS | VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6513 | msrs->exit_ctls_high |= |
| 6514 | VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR | |
| 6515 | VM_EXIT_LOAD_IA32_EFER | VM_EXIT_SAVE_IA32_EFER | |
| 6516 | VM_EXIT_SAVE_VMX_PREEMPTION_TIMER | VM_EXIT_ACK_INTR_ON_EXIT; |
| 6517 | |
| 6518 | /* We support free control of debug control saving. */ |
| 6519 | msrs->exit_ctls_low &= ~VM_EXIT_SAVE_DEBUG_CONTROLS; |
| 6520 | |
| 6521 | /* entry controls */ |
| 6522 | rdmsr(MSR_IA32_VMX_ENTRY_CTLS, |
| 6523 | msrs->entry_ctls_low, |
| 6524 | msrs->entry_ctls_high); |
| 6525 | msrs->entry_ctls_low = |
| 6526 | VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR; |
| 6527 | msrs->entry_ctls_high &= |
| 6528 | #ifdef CONFIG_X86_64 |
| 6529 | VM_ENTRY_IA32E_MODE | |
| 6530 | #endif |
Chenyi Qiang | efc8313 | 2020-08-28 16:56:18 +0800 | [diff] [blame] | 6531 | VM_ENTRY_LOAD_IA32_PAT | VM_ENTRY_LOAD_BNDCFGS | |
| 6532 | VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6533 | msrs->entry_ctls_high |= |
| 6534 | (VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR | VM_ENTRY_LOAD_IA32_EFER); |
| 6535 | |
| 6536 | /* We support free control of debug control loading. */ |
| 6537 | msrs->entry_ctls_low &= ~VM_ENTRY_LOAD_DEBUG_CONTROLS; |
| 6538 | |
| 6539 | /* cpu-based controls */ |
| 6540 | rdmsr(MSR_IA32_VMX_PROCBASED_CTLS, |
| 6541 | msrs->procbased_ctls_low, |
| 6542 | msrs->procbased_ctls_high); |
| 6543 | msrs->procbased_ctls_low = |
| 6544 | CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR; |
| 6545 | msrs->procbased_ctls_high &= |
Xiaoyao Li | 9dadc2f | 2019-12-06 16:45:24 +0800 | [diff] [blame] | 6546 | CPU_BASED_INTR_WINDOW_EXITING | |
Xiaoyao Li | 5e3d394 | 2019-12-06 16:45:26 +0800 | [diff] [blame] | 6547 | CPU_BASED_NMI_WINDOW_EXITING | CPU_BASED_USE_TSC_OFFSETTING | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6548 | CPU_BASED_HLT_EXITING | CPU_BASED_INVLPG_EXITING | |
| 6549 | CPU_BASED_MWAIT_EXITING | CPU_BASED_CR3_LOAD_EXITING | |
| 6550 | CPU_BASED_CR3_STORE_EXITING | |
| 6551 | #ifdef CONFIG_X86_64 |
| 6552 | CPU_BASED_CR8_LOAD_EXITING | CPU_BASED_CR8_STORE_EXITING | |
| 6553 | #endif |
| 6554 | CPU_BASED_MOV_DR_EXITING | CPU_BASED_UNCOND_IO_EXITING | |
| 6555 | CPU_BASED_USE_IO_BITMAPS | CPU_BASED_MONITOR_TRAP_FLAG | |
| 6556 | CPU_BASED_MONITOR_EXITING | CPU_BASED_RDPMC_EXITING | |
| 6557 | CPU_BASED_RDTSC_EXITING | CPU_BASED_PAUSE_EXITING | |
| 6558 | CPU_BASED_TPR_SHADOW | CPU_BASED_ACTIVATE_SECONDARY_CONTROLS; |
| 6559 | /* |
| 6560 | * We can allow some features even when not supported by the |
| 6561 | * hardware. For example, L1 can specify an MSR bitmap - and we |
| 6562 | * can use it to avoid exits to L1 - even when L0 runs L2 |
| 6563 | * without MSR bitmaps. |
| 6564 | */ |
| 6565 | msrs->procbased_ctls_high |= |
| 6566 | CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR | |
| 6567 | CPU_BASED_USE_MSR_BITMAPS; |
| 6568 | |
| 6569 | /* We support free control of CR3 access interception. */ |
| 6570 | msrs->procbased_ctls_low &= |
| 6571 | ~(CPU_BASED_CR3_LOAD_EXITING | CPU_BASED_CR3_STORE_EXITING); |
| 6572 | |
| 6573 | /* |
| 6574 | * secondary cpu-based controls. Do not include those that |
Xiaoyao Li | 7c1b761 | 2020-07-09 12:34:25 +0800 | [diff] [blame] | 6575 | * depend on CPUID bits, they are added later by |
| 6576 | * vmx_vcpu_after_set_cpuid. |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6577 | */ |
Vitaly Kuznetsov | 6b1971c | 2019-02-07 11:42:14 +0100 | [diff] [blame] | 6578 | if (msrs->procbased_ctls_high & CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) |
| 6579 | rdmsr(MSR_IA32_VMX_PROCBASED_CTLS2, |
| 6580 | msrs->secondary_ctls_low, |
| 6581 | msrs->secondary_ctls_high); |
| 6582 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6583 | msrs->secondary_ctls_low = 0; |
| 6584 | msrs->secondary_ctls_high &= |
| 6585 | SECONDARY_EXEC_DESC | |
Sean Christopherson | 7f3603b | 2020-09-23 09:50:47 -0700 | [diff] [blame] | 6586 | SECONDARY_EXEC_ENABLE_RDTSCP | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6587 | SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE | |
Paolo Bonzini | 6defc59 | 2019-07-02 14:39:29 +0200 | [diff] [blame] | 6588 | SECONDARY_EXEC_WBINVD_EXITING | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6589 | SECONDARY_EXEC_APIC_REGISTER_VIRT | |
| 6590 | SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY | |
Paolo Bonzini | 6defc59 | 2019-07-02 14:39:29 +0200 | [diff] [blame] | 6591 | SECONDARY_EXEC_RDRAND_EXITING | |
| 6592 | SECONDARY_EXEC_ENABLE_INVPCID | |
| 6593 | SECONDARY_EXEC_RDSEED_EXITING | |
Ilias Stamatis | d041b5e | 2021-05-26 19:44:17 +0100 | [diff] [blame] | 6594 | SECONDARY_EXEC_XSAVES | |
| 6595 | SECONDARY_EXEC_TSC_SCALING; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6596 | |
| 6597 | /* |
| 6598 | * We can emulate "VMCS shadowing," even if the hardware |
| 6599 | * doesn't support it. |
| 6600 | */ |
| 6601 | msrs->secondary_ctls_high |= |
| 6602 | SECONDARY_EXEC_SHADOW_VMCS; |
| 6603 | |
| 6604 | if (enable_ept) { |
| 6605 | /* nested EPT: emulate EPT also to L1 */ |
| 6606 | msrs->secondary_ctls_high |= |
| 6607 | SECONDARY_EXEC_ENABLE_EPT; |
Sean Christopherson | bb1fcc7 | 2020-03-02 18:02:36 -0800 | [diff] [blame] | 6608 | msrs->ept_caps = |
| 6609 | VMX_EPT_PAGE_WALK_4_BIT | |
| 6610 | VMX_EPT_PAGE_WALK_5_BIT | |
| 6611 | VMX_EPTP_WB_BIT | |
Sean Christopherson | 96d4701 | 2020-03-02 18:02:40 -0800 | [diff] [blame] | 6612 | VMX_EPT_INVEPT_BIT | |
| 6613 | VMX_EPT_EXECUTE_ONLY_BIT; |
| 6614 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6615 | msrs->ept_caps &= ept_caps; |
| 6616 | msrs->ept_caps |= VMX_EPT_EXTENT_GLOBAL_BIT | |
| 6617 | VMX_EPT_EXTENT_CONTEXT_BIT | VMX_EPT_2MB_PAGE_BIT | |
| 6618 | VMX_EPT_1GB_PAGE_BIT; |
| 6619 | if (enable_ept_ad_bits) { |
| 6620 | msrs->secondary_ctls_high |= |
| 6621 | SECONDARY_EXEC_ENABLE_PML; |
| 6622 | msrs->ept_caps |= VMX_EPT_AD_BIT; |
| 6623 | } |
| 6624 | } |
| 6625 | |
| 6626 | if (cpu_has_vmx_vmfunc()) { |
| 6627 | msrs->secondary_ctls_high |= |
| 6628 | SECONDARY_EXEC_ENABLE_VMFUNC; |
| 6629 | /* |
| 6630 | * Advertise EPTP switching unconditionally |
| 6631 | * since we emulate it |
| 6632 | */ |
| 6633 | if (enable_ept) |
| 6634 | msrs->vmfunc_controls = |
| 6635 | VMX_VMFUNC_EPTP_SWITCHING; |
| 6636 | } |
| 6637 | |
| 6638 | /* |
| 6639 | * Old versions of KVM use the single-context version without |
| 6640 | * checking for support, so declare that it is supported even |
| 6641 | * though it is treated as global context. The alternative is |
| 6642 | * not failing the single-context invvpid, and it is worse. |
| 6643 | */ |
| 6644 | if (enable_vpid) { |
| 6645 | msrs->secondary_ctls_high |= |
| 6646 | SECONDARY_EXEC_ENABLE_VPID; |
| 6647 | msrs->vpid_caps = VMX_VPID_INVVPID_BIT | |
| 6648 | VMX_VPID_EXTENT_SUPPORTED_MASK; |
| 6649 | } |
| 6650 | |
| 6651 | if (enable_unrestricted_guest) |
| 6652 | msrs->secondary_ctls_high |= |
| 6653 | SECONDARY_EXEC_UNRESTRICTED_GUEST; |
| 6654 | |
| 6655 | if (flexpriority_enabled) |
| 6656 | msrs->secondary_ctls_high |= |
| 6657 | SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES; |
| 6658 | |
Sean Christopherson | 72add91 | 2021-04-12 16:21:42 +1200 | [diff] [blame] | 6659 | if (enable_sgx) |
| 6660 | msrs->secondary_ctls_high |= SECONDARY_EXEC_ENCLS_EXITING; |
| 6661 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6662 | /* miscellaneous data */ |
| 6663 | rdmsr(MSR_IA32_VMX_MISC, |
| 6664 | msrs->misc_low, |
| 6665 | msrs->misc_high); |
| 6666 | msrs->misc_low &= VMX_MISC_SAVE_EFER_LMA; |
| 6667 | msrs->misc_low |= |
| 6668 | MSR_IA32_VMX_MISC_VMWRITE_SHADOW_RO_FIELDS | |
| 6669 | VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE | |
Yadong Qi | bf0cd88 | 2020-11-06 14:51:22 +0800 | [diff] [blame] | 6670 | VMX_MISC_ACTIVITY_HLT | |
| 6671 | VMX_MISC_ACTIVITY_WAIT_SIPI; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6672 | msrs->misc_high = 0; |
| 6673 | |
| 6674 | /* |
| 6675 | * This MSR reports some information about VMX support. We |
| 6676 | * should return information about the VMX we emulate for the |
| 6677 | * guest, and the VMCS structure we give it - not about the |
| 6678 | * VMX support of the underlying hardware. |
| 6679 | */ |
| 6680 | msrs->basic = |
| 6681 | VMCS12_REVISION | |
| 6682 | VMX_BASIC_TRUE_CTLS | |
| 6683 | ((u64)VMCS12_SIZE << VMX_BASIC_VMCS_SIZE_SHIFT) | |
| 6684 | (VMX_BASIC_MEM_TYPE_WB << VMX_BASIC_MEM_TYPE_SHIFT); |
| 6685 | |
| 6686 | if (cpu_has_vmx_basic_inout()) |
| 6687 | msrs->basic |= VMX_BASIC_INOUT; |
| 6688 | |
| 6689 | /* |
| 6690 | * These MSRs specify bits which the guest must keep fixed on |
| 6691 | * while L1 is in VMXON mode (in L1's root mode, or running an L2). |
| 6692 | * We picked the standard core2 setting. |
| 6693 | */ |
| 6694 | #define VMXON_CR0_ALWAYSON (X86_CR0_PE | X86_CR0_PG | X86_CR0_NE) |
| 6695 | #define VMXON_CR4_ALWAYSON X86_CR4_VMXE |
| 6696 | msrs->cr0_fixed0 = VMXON_CR0_ALWAYSON; |
| 6697 | msrs->cr4_fixed0 = VMXON_CR4_ALWAYSON; |
| 6698 | |
| 6699 | /* These MSRs specify bits which the guest must keep fixed off. */ |
| 6700 | rdmsrl(MSR_IA32_VMX_CR0_FIXED1, msrs->cr0_fixed1); |
| 6701 | rdmsrl(MSR_IA32_VMX_CR4_FIXED1, msrs->cr4_fixed1); |
| 6702 | |
Sean Christopherson | ba1f824 | 2021-06-18 14:46:58 -0700 | [diff] [blame] | 6703 | msrs->vmcs_enum = nested_vmx_calc_vmcs_enum_msr(); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6704 | } |
| 6705 | |
| 6706 | void nested_vmx_hardware_unsetup(void) |
| 6707 | { |
| 6708 | int i; |
| 6709 | |
| 6710 | if (enable_shadow_vmcs) { |
| 6711 | for (i = 0; i < VMX_BITMAP_NR; i++) |
| 6712 | free_page((unsigned long)vmx_bitmap[i]); |
| 6713 | } |
| 6714 | } |
| 6715 | |
Sean Christopherson | 6c1c6e5 | 2020-05-06 13:46:53 -0700 | [diff] [blame] | 6716 | __init int nested_vmx_hardware_setup(int (*exit_handlers[])(struct kvm_vcpu *)) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6717 | { |
| 6718 | int i; |
| 6719 | |
| 6720 | if (!cpu_has_vmx_shadow_vmcs()) |
| 6721 | enable_shadow_vmcs = 0; |
| 6722 | if (enable_shadow_vmcs) { |
| 6723 | for (i = 0; i < VMX_BITMAP_NR; i++) { |
Ben Gardon | 4183683 | 2019-02-11 11:02:52 -0800 | [diff] [blame] | 6724 | /* |
| 6725 | * The vmx_bitmap is not tied to a VM and so should |
| 6726 | * not be charged to a memcg. |
| 6727 | */ |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6728 | vmx_bitmap[i] = (unsigned long *) |
| 6729 | __get_free_page(GFP_KERNEL); |
| 6730 | if (!vmx_bitmap[i]) { |
| 6731 | nested_vmx_hardware_unsetup(); |
| 6732 | return -ENOMEM; |
| 6733 | } |
| 6734 | } |
| 6735 | |
| 6736 | init_vmcs_shadow_fields(); |
| 6737 | } |
| 6738 | |
Liran Alon | cc87767 | 2019-11-18 21:11:21 +0200 | [diff] [blame] | 6739 | exit_handlers[EXIT_REASON_VMCLEAR] = handle_vmclear; |
| 6740 | exit_handlers[EXIT_REASON_VMLAUNCH] = handle_vmlaunch; |
| 6741 | exit_handlers[EXIT_REASON_VMPTRLD] = handle_vmptrld; |
| 6742 | exit_handlers[EXIT_REASON_VMPTRST] = handle_vmptrst; |
| 6743 | exit_handlers[EXIT_REASON_VMREAD] = handle_vmread; |
| 6744 | exit_handlers[EXIT_REASON_VMRESUME] = handle_vmresume; |
| 6745 | exit_handlers[EXIT_REASON_VMWRITE] = handle_vmwrite; |
| 6746 | exit_handlers[EXIT_REASON_VMOFF] = handle_vmoff; |
| 6747 | exit_handlers[EXIT_REASON_VMON] = handle_vmon; |
| 6748 | exit_handlers[EXIT_REASON_INVEPT] = handle_invept; |
| 6749 | exit_handlers[EXIT_REASON_INVVPID] = handle_invvpid; |
| 6750 | exit_handlers[EXIT_REASON_VMFUNC] = handle_vmfunc; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6751 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6752 | return 0; |
| 6753 | } |
Paolo Bonzini | 33b2217 | 2020-04-17 10:24:18 -0400 | [diff] [blame] | 6754 | |
| 6755 | struct kvm_x86_nested_ops vmx_nested_ops = { |
| 6756 | .check_events = vmx_check_nested_events, |
Sean Christopherson | d2060bd | 2020-04-22 19:25:39 -0700 | [diff] [blame] | 6757 | .hv_timer_pending = nested_vmx_preemption_timer_pending, |
Sean Christopherson | cb6a32c | 2021-03-02 09:45:14 -0800 | [diff] [blame] | 6758 | .triple_fault = nested_vmx_triple_fault, |
Paolo Bonzini | 33b2217 | 2020-04-17 10:24:18 -0400 | [diff] [blame] | 6759 | .get_state = vmx_get_nested_state, |
| 6760 | .set_state = vmx_set_nested_state, |
Paolo Bonzini | 9a78e15 | 2021-01-08 11:43:08 -0500 | [diff] [blame] | 6761 | .get_nested_state_pages = vmx_get_nested_state_pages, |
Sean Christopherson | 02f5fb2 | 2020-06-22 14:58:32 -0700 | [diff] [blame] | 6762 | .write_log_dirty = nested_vmx_write_pml_buffer, |
Paolo Bonzini | 33b2217 | 2020-04-17 10:24:18 -0400 | [diff] [blame] | 6763 | .enable_evmcs = nested_enable_evmcs, |
| 6764 | .get_evmcs_version = nested_get_evmcs_version, |
| 6765 | }; |