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 | |
| 272 | vmx_register_cache_reset(vcpu); |
| 273 | } |
| 274 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 275 | /* |
| 276 | * Free whatever needs to be freed from vmx->nested when L1 goes down, or |
| 277 | * just stops using VMX. |
| 278 | */ |
| 279 | static void free_nested(struct kvm_vcpu *vcpu) |
| 280 | { |
| 281 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 282 | |
Sean Christopherson | df82a24 | 2020-09-23 11:44:50 -0700 | [diff] [blame] | 283 | if (WARN_ON_ONCE(vmx->loaded_vmcs != &vmx->vmcs01)) |
| 284 | vmx_switch_vmcs(vcpu, &vmx->vmcs01); |
| 285 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 286 | if (!vmx->nested.vmxon && !vmx->nested.smm.vmxon) |
| 287 | return; |
| 288 | |
Paolo Bonzini | 729c15c | 2020-09-22 06:53:57 -0400 | [diff] [blame] | 289 | kvm_clear_request(KVM_REQ_GET_NESTED_STATE_PAGES, vcpu); |
Jan Kiszka | cf64527 | 2019-07-21 13:52:18 +0200 | [diff] [blame] | 290 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 291 | vmx->nested.vmxon = false; |
| 292 | vmx->nested.smm.vmxon = false; |
Vitaly Kuznetsov | feb3162 | 2021-09-30 01:51:54 +0800 | [diff] [blame] | 293 | vmx->nested.vmxon_ptr = INVALID_GPA; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 294 | free_vpid(vmx->nested.vpid02); |
| 295 | vmx->nested.posted_intr_nv = -1; |
Yu Zhang | 64c7850 | 2021-09-30 01:51:53 +0800 | [diff] [blame] | 296 | vmx->nested.current_vmptr = INVALID_GPA; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 297 | if (enable_shadow_vmcs) { |
| 298 | vmx_disable_shadow_vmcs(vmx); |
| 299 | vmcs_clear(vmx->vmcs01.shadow_vmcs); |
| 300 | free_vmcs(vmx->vmcs01.shadow_vmcs); |
| 301 | vmx->vmcs01.shadow_vmcs = NULL; |
| 302 | } |
| 303 | kfree(vmx->nested.cached_vmcs12); |
Jan Kiszka | c6bf2ae | 2019-07-21 16:01:36 +0200 | [diff] [blame] | 304 | vmx->nested.cached_vmcs12 = NULL; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 305 | kfree(vmx->nested.cached_shadow_vmcs12); |
Jan Kiszka | c6bf2ae | 2019-07-21 16:01:36 +0200 | [diff] [blame] | 306 | vmx->nested.cached_shadow_vmcs12 = NULL; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 307 | /* Unpin physical memory we referred to in the vmcs02 */ |
| 308 | if (vmx->nested.apic_access_page) { |
Liran Alon | b11494b | 2019-11-21 00:31:47 +0200 | [diff] [blame] | 309 | kvm_release_page_clean(vmx->nested.apic_access_page); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 310 | vmx->nested.apic_access_page = NULL; |
| 311 | } |
KarimAllah Ahmed | 96c66e8 | 2019-01-31 21:24:37 +0100 | [diff] [blame] | 312 | kvm_vcpu_unmap(vcpu, &vmx->nested.virtual_apic_map, true); |
KarimAllah Ahmed | 3278e04 | 2019-01-31 21:24:38 +0100 | [diff] [blame] | 313 | kvm_vcpu_unmap(vcpu, &vmx->nested.pi_desc_map, true); |
| 314 | vmx->nested.pi_desc = NULL; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 315 | |
| 316 | kvm_mmu_free_roots(vcpu, &vcpu->arch.guest_mmu, KVM_MMU_ROOTS_ALL); |
| 317 | |
| 318 | nested_release_evmcs(vcpu); |
| 319 | |
| 320 | free_loaded_vmcs(&vmx->nested.vmcs02); |
| 321 | } |
| 322 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 323 | /* |
| 324 | * Ensure that the current vmcs of the logical processor is the |
| 325 | * vmcs01 of the vcpu before calling free_nested(). |
| 326 | */ |
| 327 | void nested_vmx_free_vcpu(struct kvm_vcpu *vcpu) |
| 328 | { |
| 329 | vcpu_load(vcpu); |
Paolo Bonzini | b4b65b5 | 2019-01-29 19:12:35 +0100 | [diff] [blame] | 330 | vmx_leave_nested(vcpu); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 331 | vcpu_put(vcpu); |
| 332 | } |
| 333 | |
Junaid Shahid | 85aa888 | 2021-08-06 15:22:29 -0700 | [diff] [blame] | 334 | #define EPTP_PA_MASK GENMASK_ULL(51, 12) |
| 335 | |
| 336 | static bool nested_ept_root_matches(hpa_t root_hpa, u64 root_eptp, u64 eptp) |
| 337 | { |
| 338 | return VALID_PAGE(root_hpa) && |
| 339 | ((root_eptp & EPTP_PA_MASK) == (eptp & EPTP_PA_MASK)); |
| 340 | } |
| 341 | |
| 342 | static void nested_ept_invalidate_addr(struct kvm_vcpu *vcpu, gpa_t eptp, |
| 343 | gpa_t addr) |
| 344 | { |
| 345 | uint i; |
| 346 | struct kvm_mmu_root_info *cached_root; |
| 347 | |
| 348 | WARN_ON_ONCE(!mmu_is_nested(vcpu)); |
| 349 | |
| 350 | for (i = 0; i < KVM_MMU_NUM_PREV_ROOTS; i++) { |
| 351 | cached_root = &vcpu->arch.mmu->prev_roots[i]; |
| 352 | |
| 353 | if (nested_ept_root_matches(cached_root->hpa, cached_root->pgd, |
| 354 | eptp)) |
| 355 | vcpu->arch.mmu->invlpg(vcpu, addr, cached_root->hpa); |
| 356 | } |
| 357 | } |
| 358 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 359 | static void nested_ept_inject_page_fault(struct kvm_vcpu *vcpu, |
| 360 | struct x86_exception *fault) |
| 361 | { |
| 362 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
| 363 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Sean Christopherson | 4dcefa3 | 2020-04-15 10:55:18 -0700 | [diff] [blame] | 364 | u32 vm_exit_reason; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 365 | unsigned long exit_qualification = vcpu->arch.exit_qualification; |
| 366 | |
| 367 | if (vmx->nested.pml_full) { |
Sean Christopherson | 4dcefa3 | 2020-04-15 10:55:18 -0700 | [diff] [blame] | 368 | vm_exit_reason = EXIT_REASON_PML_FULL; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 369 | vmx->nested.pml_full = false; |
| 370 | exit_qualification &= INTR_INFO_UNBLOCK_NMI; |
Junaid Shahid | 85aa888 | 2021-08-06 15:22:29 -0700 | [diff] [blame] | 371 | } else { |
| 372 | if (fault->error_code & PFERR_RSVD_MASK) |
| 373 | vm_exit_reason = EXIT_REASON_EPT_MISCONFIG; |
| 374 | else |
| 375 | vm_exit_reason = EXIT_REASON_EPT_VIOLATION; |
| 376 | |
| 377 | /* |
| 378 | * Although the caller (kvm_inject_emulated_page_fault) would |
| 379 | * have already synced the faulting address in the shadow EPT |
| 380 | * tables for the current EPTP12, we also need to sync it for |
| 381 | * any other cached EPTP02s based on the same EP4TA, since the |
| 382 | * TLB associates mappings to the EP4TA rather than the full EPTP. |
| 383 | */ |
| 384 | nested_ept_invalidate_addr(vcpu, vmcs12->ept_pointer, |
| 385 | fault->address); |
| 386 | } |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 387 | |
Sean Christopherson | 4dcefa3 | 2020-04-15 10:55:18 -0700 | [diff] [blame] | 388 | nested_vmx_vmexit(vcpu, vm_exit_reason, 0, exit_qualification); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 389 | vmcs12->guest_physical_address = fault->address; |
| 390 | } |
| 391 | |
Sean Christopherson | 39353ab | 2021-06-09 16:42:31 -0700 | [diff] [blame] | 392 | static void nested_ept_new_eptp(struct kvm_vcpu *vcpu) |
| 393 | { |
| 394 | kvm_init_shadow_ept_mmu(vcpu, |
| 395 | to_vmx(vcpu)->nested.msrs.ept_caps & |
| 396 | VMX_EPT_EXECUTE_ONLY_BIT, |
| 397 | nested_ept_ad_enabled(vcpu), |
| 398 | nested_ept_get_eptp(vcpu)); |
| 399 | } |
| 400 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 401 | static void nested_ept_init_mmu_context(struct kvm_vcpu *vcpu) |
| 402 | { |
| 403 | WARN_ON(mmu_is_nested(vcpu)); |
| 404 | |
| 405 | vcpu->arch.mmu = &vcpu->arch.guest_mmu; |
Sean Christopherson | 39353ab | 2021-06-09 16:42:31 -0700 | [diff] [blame] | 406 | nested_ept_new_eptp(vcpu); |
Sean Christopherson | d8dd54e | 2020-03-02 18:02:39 -0800 | [diff] [blame] | 407 | vcpu->arch.mmu->get_guest_pgd = nested_ept_get_eptp; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 408 | vcpu->arch.mmu->inject_page_fault = nested_ept_inject_page_fault; |
| 409 | vcpu->arch.mmu->get_pdptr = kvm_pdptr_read; |
| 410 | |
| 411 | vcpu->arch.walk_mmu = &vcpu->arch.nested_mmu; |
| 412 | } |
| 413 | |
| 414 | static void nested_ept_uninit_mmu_context(struct kvm_vcpu *vcpu) |
| 415 | { |
| 416 | vcpu->arch.mmu = &vcpu->arch.root_mmu; |
| 417 | vcpu->arch.walk_mmu = &vcpu->arch.root_mmu; |
| 418 | } |
| 419 | |
| 420 | static bool nested_vmx_is_page_fault_vmexit(struct vmcs12 *vmcs12, |
| 421 | u16 error_code) |
| 422 | { |
| 423 | bool inequality, bit; |
| 424 | |
| 425 | bit = (vmcs12->exception_bitmap & (1u << PF_VECTOR)) != 0; |
| 426 | inequality = |
| 427 | (error_code & vmcs12->page_fault_error_code_mask) != |
| 428 | vmcs12->page_fault_error_code_match; |
| 429 | return inequality ^ bit; |
| 430 | } |
| 431 | |
| 432 | |
| 433 | /* |
| 434 | * KVM wants to inject page-faults which it got to the guest. This function |
| 435 | * checks whether in a nested guest, we need to inject them to L1 or L2. |
| 436 | */ |
| 437 | static int nested_vmx_check_exception(struct kvm_vcpu *vcpu, unsigned long *exit_qual) |
| 438 | { |
| 439 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
| 440 | unsigned int nr = vcpu->arch.exception.nr; |
| 441 | bool has_payload = vcpu->arch.exception.has_payload; |
| 442 | unsigned long payload = vcpu->arch.exception.payload; |
| 443 | |
| 444 | if (nr == PF_VECTOR) { |
| 445 | if (vcpu->arch.exception.nested_apf) { |
| 446 | *exit_qual = vcpu->arch.apf.nested_apf_token; |
| 447 | return 1; |
| 448 | } |
| 449 | if (nested_vmx_is_page_fault_vmexit(vmcs12, |
| 450 | vcpu->arch.exception.error_code)) { |
| 451 | *exit_qual = has_payload ? payload : vcpu->arch.cr2; |
| 452 | return 1; |
| 453 | } |
| 454 | } else if (vmcs12->exception_bitmap & (1u << nr)) { |
| 455 | if (nr == DB_VECTOR) { |
| 456 | if (!has_payload) { |
| 457 | payload = vcpu->arch.dr6; |
Chenyi Qiang | 9a3ecd5 | 2021-02-02 17:04:31 +0800 | [diff] [blame] | 458 | payload &= ~DR6_BT; |
| 459 | payload ^= DR6_ACTIVE_LOW; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 460 | } |
| 461 | *exit_qual = payload; |
| 462 | } else |
| 463 | *exit_qual = 0; |
| 464 | return 1; |
| 465 | } |
| 466 | |
| 467 | return 0; |
| 468 | } |
| 469 | |
| 470 | |
| 471 | static void vmx_inject_page_fault_nested(struct kvm_vcpu *vcpu, |
| 472 | struct x86_exception *fault) |
| 473 | { |
| 474 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
| 475 | |
| 476 | WARN_ON(!is_guest_mode(vcpu)); |
| 477 | |
| 478 | if (nested_vmx_is_page_fault_vmexit(vmcs12, fault->error_code) && |
| 479 | !to_vmx(vcpu)->nested.nested_run_pending) { |
| 480 | vmcs12->vm_exit_intr_error_code = fault->error_code; |
| 481 | nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI, |
| 482 | PF_VECTOR | INTR_TYPE_HARD_EXCEPTION | |
| 483 | INTR_INFO_DELIVER_CODE_MASK | INTR_INFO_VALID_MASK, |
| 484 | fault->address); |
| 485 | } else { |
| 486 | kvm_inject_page_fault(vcpu, fault); |
| 487 | } |
| 488 | } |
| 489 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 490 | static int nested_vmx_check_io_bitmap_controls(struct kvm_vcpu *vcpu, |
| 491 | struct vmcs12 *vmcs12) |
| 492 | { |
| 493 | if (!nested_cpu_has(vmcs12, CPU_BASED_USE_IO_BITMAPS)) |
| 494 | return 0; |
| 495 | |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 496 | if (CC(!page_address_valid(vcpu, vmcs12->io_bitmap_a)) || |
| 497 | CC(!page_address_valid(vcpu, vmcs12->io_bitmap_b))) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 498 | return -EINVAL; |
| 499 | |
| 500 | return 0; |
| 501 | } |
| 502 | |
| 503 | static int nested_vmx_check_msr_bitmap_controls(struct kvm_vcpu *vcpu, |
| 504 | struct vmcs12 *vmcs12) |
| 505 | { |
| 506 | if (!nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS)) |
| 507 | return 0; |
| 508 | |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 509 | if (CC(!page_address_valid(vcpu, vmcs12->msr_bitmap))) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 510 | return -EINVAL; |
| 511 | |
| 512 | return 0; |
| 513 | } |
| 514 | |
| 515 | static int nested_vmx_check_tpr_shadow_controls(struct kvm_vcpu *vcpu, |
| 516 | struct vmcs12 *vmcs12) |
| 517 | { |
| 518 | if (!nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW)) |
| 519 | return 0; |
| 520 | |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 521 | if (CC(!page_address_valid(vcpu, vmcs12->virtual_apic_page_addr))) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 522 | return -EINVAL; |
| 523 | |
| 524 | return 0; |
| 525 | } |
| 526 | |
| 527 | /* |
Sean Christopherson | a5e0c25 | 2021-11-09 01:30:47 +0000 | [diff] [blame^] | 528 | * For x2APIC MSRs, ignore the vmcs01 bitmap. L1 can enable x2APIC without L1 |
| 529 | * itself utilizing x2APIC. All MSRs were previously set to be intercepted, |
| 530 | * only the "disable intercept" case needs to be handled. |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 531 | */ |
Sean Christopherson | a5e0c25 | 2021-11-09 01:30:47 +0000 | [diff] [blame^] | 532 | static void nested_vmx_disable_intercept_for_x2apic_msr(unsigned long *msr_bitmap_l1, |
| 533 | unsigned long *msr_bitmap_l0, |
| 534 | u32 msr, int type) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 535 | { |
Sean Christopherson | a5e0c25 | 2021-11-09 01:30:47 +0000 | [diff] [blame^] | 536 | if (type & MSR_TYPE_R && !vmx_test_msr_bitmap_read(msr_bitmap_l1, msr)) |
| 537 | vmx_clear_msr_bitmap_read(msr_bitmap_l0, msr); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 538 | |
Sean Christopherson | a5e0c25 | 2021-11-09 01:30:47 +0000 | [diff] [blame^] | 539 | if (type & MSR_TYPE_W && !vmx_test_msr_bitmap_write(msr_bitmap_l1, msr)) |
| 540 | vmx_clear_msr_bitmap_write(msr_bitmap_l0, msr); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 541 | } |
| 542 | |
Miaohe Lin | ffdbd50 | 2020-02-07 23:22:45 +0800 | [diff] [blame] | 543 | static inline void enable_x2apic_msr_intercepts(unsigned long *msr_bitmap) |
| 544 | { |
Marc Orr | acff784 | 2019-04-01 23:55:59 -0700 | [diff] [blame] | 545 | int msr; |
| 546 | |
| 547 | for (msr = 0x800; msr <= 0x8ff; msr += BITS_PER_LONG) { |
| 548 | unsigned word = msr / BITS_PER_LONG; |
| 549 | |
| 550 | msr_bitmap[word] = ~0; |
| 551 | msr_bitmap[word + (0x800 / sizeof(long))] = ~0; |
| 552 | } |
| 553 | } |
| 554 | |
Sean Christopherson | 67f4b99 | 2021-11-09 01:30:45 +0000 | [diff] [blame] | 555 | #define BUILD_NVMX_MSR_INTERCEPT_HELPER(rw) \ |
| 556 | static inline \ |
| 557 | void nested_vmx_set_msr_##rw##_intercept(struct vcpu_vmx *vmx, \ |
| 558 | unsigned long *msr_bitmap_l1, \ |
| 559 | unsigned long *msr_bitmap_l0, u32 msr) \ |
| 560 | { \ |
| 561 | if (vmx_test_msr_bitmap_##rw(vmx->vmcs01.msr_bitmap, msr) || \ |
| 562 | vmx_test_msr_bitmap_##rw(msr_bitmap_l1, msr)) \ |
| 563 | vmx_set_msr_bitmap_##rw(msr_bitmap_l0, msr); \ |
| 564 | else \ |
| 565 | vmx_clear_msr_bitmap_##rw(msr_bitmap_l0, msr); \ |
| 566 | } |
| 567 | BUILD_NVMX_MSR_INTERCEPT_HELPER(read) |
| 568 | BUILD_NVMX_MSR_INTERCEPT_HELPER(write) |
| 569 | |
| 570 | static inline void nested_vmx_set_intercept_for_msr(struct vcpu_vmx *vmx, |
| 571 | unsigned long *msr_bitmap_l1, |
| 572 | unsigned long *msr_bitmap_l0, |
| 573 | u32 msr, int types) |
| 574 | { |
| 575 | if (types & MSR_TYPE_R) |
| 576 | nested_vmx_set_msr_read_intercept(vmx, msr_bitmap_l1, |
| 577 | msr_bitmap_l0, msr); |
| 578 | if (types & MSR_TYPE_W) |
| 579 | nested_vmx_set_msr_write_intercept(vmx, msr_bitmap_l1, |
| 580 | msr_bitmap_l0, msr); |
| 581 | } |
| 582 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 583 | /* |
| 584 | * Merge L0's and L1's MSR bitmap, return false to indicate that |
| 585 | * we do not use the hardware. |
| 586 | */ |
| 587 | static inline bool nested_vmx_prepare_msr_bitmap(struct kvm_vcpu *vcpu, |
| 588 | struct vmcs12 *vmcs12) |
| 589 | { |
Sean Christopherson | 67f4b99 | 2021-11-09 01:30:45 +0000 | [diff] [blame] | 590 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 591 | int msr; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 592 | unsigned long *msr_bitmap_l1; |
Sean Christopherson | 67f4b99 | 2021-11-09 01:30:45 +0000 | [diff] [blame] | 593 | unsigned long *msr_bitmap_l0 = vmx->nested.vmcs02.msr_bitmap; |
| 594 | struct kvm_host_map *map = &vmx->nested.msr_bitmap_map; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 595 | |
| 596 | /* Nothing to do if the MSR bitmap is not in use. */ |
| 597 | if (!cpu_has_vmx_msr_bitmap() || |
| 598 | !nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS)) |
| 599 | return false; |
| 600 | |
KarimAllah Ahmed | 31f0b6c | 2019-01-31 21:24:36 +0100 | [diff] [blame] | 601 | if (kvm_vcpu_map(vcpu, gpa_to_gfn(vmcs12->msr_bitmap), map)) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 602 | return false; |
| 603 | |
KarimAllah Ahmed | 31f0b6c | 2019-01-31 21:24:36 +0100 | [diff] [blame] | 604 | msr_bitmap_l1 = (unsigned long *)map->hva; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 605 | |
Marc Orr | acff784 | 2019-04-01 23:55:59 -0700 | [diff] [blame] | 606 | /* |
| 607 | * To keep the control flow simple, pay eight 8-byte writes (sixteen |
| 608 | * 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^] | 609 | * the x2APIC MSR range and selectively toggle those relevant to L2. |
Marc Orr | acff784 | 2019-04-01 23:55:59 -0700 | [diff] [blame] | 610 | */ |
| 611 | enable_x2apic_msr_intercepts(msr_bitmap_l0); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 612 | |
Marc Orr | acff784 | 2019-04-01 23:55:59 -0700 | [diff] [blame] | 613 | if (nested_cpu_has_virt_x2apic_mode(vmcs12)) { |
| 614 | if (nested_cpu_has_apic_reg_virt(vmcs12)) { |
| 615 | /* |
| 616 | * L0 need not intercept reads for MSRs between 0x800 |
| 617 | * and 0x8ff, it just lets the processor take the value |
| 618 | * from the virtual-APIC page; take those 256 bits |
| 619 | * directly from the L1 bitmap. |
| 620 | */ |
| 621 | for (msr = 0x800; msr <= 0x8ff; msr += BITS_PER_LONG) { |
| 622 | unsigned word = msr / BITS_PER_LONG; |
| 623 | |
| 624 | msr_bitmap_l0[word] = msr_bitmap_l1[word]; |
| 625 | } |
| 626 | } |
| 627 | |
Sean Christopherson | a5e0c25 | 2021-11-09 01:30:47 +0000 | [diff] [blame^] | 628 | nested_vmx_disable_intercept_for_x2apic_msr( |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 629 | msr_bitmap_l1, msr_bitmap_l0, |
Marc Orr | acff784 | 2019-04-01 23:55:59 -0700 | [diff] [blame] | 630 | X2APIC_MSR(APIC_TASKPRI), |
Marc Orr | c73f4c9 | 2019-04-01 23:56:00 -0700 | [diff] [blame] | 631 | MSR_TYPE_R | MSR_TYPE_W); |
Marc Orr | acff784 | 2019-04-01 23:55:59 -0700 | [diff] [blame] | 632 | |
| 633 | if (nested_cpu_has_vid(vmcs12)) { |
Sean Christopherson | a5e0c25 | 2021-11-09 01:30:47 +0000 | [diff] [blame^] | 634 | nested_vmx_disable_intercept_for_x2apic_msr( |
Marc Orr | acff784 | 2019-04-01 23:55:59 -0700 | [diff] [blame] | 635 | msr_bitmap_l1, msr_bitmap_l0, |
| 636 | X2APIC_MSR(APIC_EOI), |
| 637 | MSR_TYPE_W); |
Sean Christopherson | a5e0c25 | 2021-11-09 01:30:47 +0000 | [diff] [blame^] | 638 | nested_vmx_disable_intercept_for_x2apic_msr( |
Marc Orr | acff784 | 2019-04-01 23:55:59 -0700 | [diff] [blame] | 639 | msr_bitmap_l1, msr_bitmap_l0, |
| 640 | X2APIC_MSR(APIC_SELF_IPI), |
| 641 | MSR_TYPE_W); |
| 642 | } |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 643 | } |
| 644 | |
Sean Christopherson | d69129b | 2019-05-08 07:32:15 -0700 | [diff] [blame] | 645 | /* |
Sean Christopherson | 67f4b99 | 2021-11-09 01:30:45 +0000 | [diff] [blame] | 646 | * Always check vmcs01's bitmap to honor userspace MSR filters and any |
| 647 | * other runtime changes to vmcs01's bitmap, e.g. dynamic pass-through. |
Sean Christopherson | d69129b | 2019-05-08 07:32:15 -0700 | [diff] [blame] | 648 | */ |
Sean Christopherson | 67f4b99 | 2021-11-09 01:30:45 +0000 | [diff] [blame] | 649 | #ifdef CONFIG_X86_64 |
| 650 | nested_vmx_set_intercept_for_msr(vmx, msr_bitmap_l1, msr_bitmap_l0, |
| 651 | MSR_FS_BASE, MSR_TYPE_RW); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 652 | |
Sean Christopherson | 67f4b99 | 2021-11-09 01:30:45 +0000 | [diff] [blame] | 653 | nested_vmx_set_intercept_for_msr(vmx, msr_bitmap_l1, msr_bitmap_l0, |
| 654 | MSR_GS_BASE, MSR_TYPE_RW); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 655 | |
Sean Christopherson | 67f4b99 | 2021-11-09 01:30:45 +0000 | [diff] [blame] | 656 | nested_vmx_set_intercept_for_msr(vmx, msr_bitmap_l1, msr_bitmap_l0, |
| 657 | MSR_KERNEL_GS_BASE, MSR_TYPE_RW); |
| 658 | #endif |
| 659 | nested_vmx_set_intercept_for_msr(vmx, msr_bitmap_l1, msr_bitmap_l0, |
| 660 | MSR_IA32_SPEC_CTRL, MSR_TYPE_RW); |
| 661 | |
| 662 | nested_vmx_set_intercept_for_msr(vmx, msr_bitmap_l1, msr_bitmap_l0, |
| 663 | MSR_IA32_PRED_CMD, MSR_TYPE_W); |
| 664 | |
| 665 | kvm_vcpu_unmap(vcpu, &vmx->nested.msr_bitmap_map, false); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 666 | |
| 667 | return true; |
| 668 | } |
| 669 | |
| 670 | static void nested_cache_shadow_vmcs12(struct kvm_vcpu *vcpu, |
| 671 | struct vmcs12 *vmcs12) |
| 672 | { |
KarimAllah Ahmed | 8892530 | 2019-01-31 21:24:41 +0100 | [diff] [blame] | 673 | struct kvm_host_map map; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 674 | struct vmcs12 *shadow; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 675 | |
| 676 | if (!nested_cpu_has_shadow_vmcs(vmcs12) || |
Yu Zhang | 64c7850 | 2021-09-30 01:51:53 +0800 | [diff] [blame] | 677 | vmcs12->vmcs_link_pointer == INVALID_GPA) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 678 | return; |
| 679 | |
| 680 | shadow = get_shadow_vmcs12(vcpu); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 681 | |
KarimAllah Ahmed | 8892530 | 2019-01-31 21:24:41 +0100 | [diff] [blame] | 682 | if (kvm_vcpu_map(vcpu, gpa_to_gfn(vmcs12->vmcs_link_pointer), &map)) |
| 683 | return; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 684 | |
KarimAllah Ahmed | 8892530 | 2019-01-31 21:24:41 +0100 | [diff] [blame] | 685 | memcpy(shadow, map.hva, VMCS12_SIZE); |
| 686 | kvm_vcpu_unmap(vcpu, &map, false); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 687 | } |
| 688 | |
| 689 | static void nested_flush_cached_shadow_vmcs12(struct kvm_vcpu *vcpu, |
| 690 | struct vmcs12 *vmcs12) |
| 691 | { |
| 692 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 693 | |
| 694 | if (!nested_cpu_has_shadow_vmcs(vmcs12) || |
Yu Zhang | 64c7850 | 2021-09-30 01:51:53 +0800 | [diff] [blame] | 695 | vmcs12->vmcs_link_pointer == INVALID_GPA) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 696 | return; |
| 697 | |
| 698 | kvm_write_guest(vmx->vcpu.kvm, vmcs12->vmcs_link_pointer, |
| 699 | get_shadow_vmcs12(vcpu), VMCS12_SIZE); |
| 700 | } |
| 701 | |
| 702 | /* |
| 703 | * In nested virtualization, check if L1 has set |
| 704 | * VM_EXIT_ACK_INTR_ON_EXIT |
| 705 | */ |
| 706 | static bool nested_exit_intr_ack_set(struct kvm_vcpu *vcpu) |
| 707 | { |
| 708 | return get_vmcs12(vcpu)->vm_exit_controls & |
| 709 | VM_EXIT_ACK_INTR_ON_EXIT; |
| 710 | } |
| 711 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 712 | static int nested_vmx_check_apic_access_controls(struct kvm_vcpu *vcpu, |
| 713 | struct vmcs12 *vmcs12) |
| 714 | { |
| 715 | if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES) && |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 716 | CC(!page_address_valid(vcpu, vmcs12->apic_access_addr))) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 717 | return -EINVAL; |
| 718 | else |
| 719 | return 0; |
| 720 | } |
| 721 | |
| 722 | static int nested_vmx_check_apicv_controls(struct kvm_vcpu *vcpu, |
| 723 | struct vmcs12 *vmcs12) |
| 724 | { |
| 725 | if (!nested_cpu_has_virt_x2apic_mode(vmcs12) && |
| 726 | !nested_cpu_has_apic_reg_virt(vmcs12) && |
| 727 | !nested_cpu_has_vid(vmcs12) && |
| 728 | !nested_cpu_has_posted_intr(vmcs12)) |
| 729 | return 0; |
| 730 | |
| 731 | /* |
| 732 | * If virtualize x2apic mode is enabled, |
| 733 | * virtualize apic access must be disabled. |
| 734 | */ |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 735 | if (CC(nested_cpu_has_virt_x2apic_mode(vmcs12) && |
| 736 | nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES))) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 737 | return -EINVAL; |
| 738 | |
| 739 | /* |
| 740 | * If virtual interrupt delivery is enabled, |
| 741 | * we must exit on external interrupts. |
| 742 | */ |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 743 | if (CC(nested_cpu_has_vid(vmcs12) && !nested_exit_on_intr(vcpu))) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 744 | return -EINVAL; |
| 745 | |
| 746 | /* |
| 747 | * bits 15:8 should be zero in posted_intr_nv, |
| 748 | * the descriptor address has been already checked |
| 749 | * in nested_get_vmcs12_pages. |
| 750 | * |
| 751 | * bits 5:0 of posted_intr_desc_addr should be zero. |
| 752 | */ |
| 753 | if (nested_cpu_has_posted_intr(vmcs12) && |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 754 | (CC(!nested_cpu_has_vid(vmcs12)) || |
| 755 | CC(!nested_exit_intr_ack_set(vcpu)) || |
| 756 | CC((vmcs12->posted_intr_nv & 0xff00)) || |
Sean Christopherson | 636e8b7 | 2021-02-03 16:01:10 -0800 | [diff] [blame] | 757 | 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] | 758 | return -EINVAL; |
| 759 | |
| 760 | /* tpr shadow is needed by all apicv features. */ |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 761 | if (CC(!nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW))) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 762 | return -EINVAL; |
| 763 | |
| 764 | return 0; |
| 765 | } |
| 766 | |
| 767 | static int nested_vmx_check_msr_switch(struct kvm_vcpu *vcpu, |
Sean Christopherson | f9b245e | 2018-12-12 13:30:08 -0500 | [diff] [blame] | 768 | u32 count, u64 addr) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 769 | { |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 770 | if (count == 0) |
| 771 | return 0; |
Sean Christopherson | 636e8b7 | 2021-02-03 16:01:10 -0800 | [diff] [blame] | 772 | |
| 773 | if (!kvm_vcpu_is_legal_aligned_gpa(vcpu, addr, 16) || |
| 774 | !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] | 775 | return -EINVAL; |
Sean Christopherson | f9b245e | 2018-12-12 13:30:08 -0500 | [diff] [blame] | 776 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 777 | return 0; |
| 778 | } |
| 779 | |
Krish Sadhukhan | 61446ba | 2018-12-12 13:30:09 -0500 | [diff] [blame] | 780 | static int nested_vmx_check_exit_msr_switch_controls(struct kvm_vcpu *vcpu, |
| 781 | struct vmcs12 *vmcs12) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 782 | { |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 783 | if (CC(nested_vmx_check_msr_switch(vcpu, |
| 784 | vmcs12->vm_exit_msr_load_count, |
| 785 | vmcs12->vm_exit_msr_load_addr)) || |
| 786 | CC(nested_vmx_check_msr_switch(vcpu, |
| 787 | vmcs12->vm_exit_msr_store_count, |
| 788 | vmcs12->vm_exit_msr_store_addr))) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 789 | return -EINVAL; |
Sean Christopherson | f9b245e | 2018-12-12 13:30:08 -0500 | [diff] [blame] | 790 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 791 | return 0; |
| 792 | } |
| 793 | |
Krish Sadhukhan | 5fbf963 | 2018-12-12 13:30:10 -0500 | [diff] [blame] | 794 | static int nested_vmx_check_entry_msr_switch_controls(struct kvm_vcpu *vcpu, |
| 795 | struct vmcs12 *vmcs12) |
Krish Sadhukhan | 61446ba | 2018-12-12 13:30:09 -0500 | [diff] [blame] | 796 | { |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 797 | if (CC(nested_vmx_check_msr_switch(vcpu, |
| 798 | vmcs12->vm_entry_msr_load_count, |
| 799 | vmcs12->vm_entry_msr_load_addr))) |
Krish Sadhukhan | 61446ba | 2018-12-12 13:30:09 -0500 | [diff] [blame] | 800 | return -EINVAL; |
| 801 | |
| 802 | return 0; |
| 803 | } |
| 804 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 805 | static int nested_vmx_check_pml_controls(struct kvm_vcpu *vcpu, |
| 806 | struct vmcs12 *vmcs12) |
| 807 | { |
| 808 | if (!nested_cpu_has_pml(vmcs12)) |
| 809 | return 0; |
| 810 | |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 811 | if (CC(!nested_cpu_has_ept(vmcs12)) || |
| 812 | CC(!page_address_valid(vcpu, vmcs12->pml_address))) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 813 | return -EINVAL; |
| 814 | |
| 815 | return 0; |
| 816 | } |
| 817 | |
| 818 | static int nested_vmx_check_unrestricted_guest_controls(struct kvm_vcpu *vcpu, |
| 819 | struct vmcs12 *vmcs12) |
| 820 | { |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 821 | if (CC(nested_cpu_has2(vmcs12, SECONDARY_EXEC_UNRESTRICTED_GUEST) && |
| 822 | !nested_cpu_has_ept(vmcs12))) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 823 | return -EINVAL; |
| 824 | return 0; |
| 825 | } |
| 826 | |
| 827 | static int nested_vmx_check_mode_based_ept_exec_controls(struct kvm_vcpu *vcpu, |
| 828 | struct vmcs12 *vmcs12) |
| 829 | { |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 830 | if (CC(nested_cpu_has2(vmcs12, SECONDARY_EXEC_MODE_BASED_EPT_EXEC) && |
| 831 | !nested_cpu_has_ept(vmcs12))) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 832 | return -EINVAL; |
| 833 | return 0; |
| 834 | } |
| 835 | |
| 836 | static int nested_vmx_check_shadow_vmcs_controls(struct kvm_vcpu *vcpu, |
| 837 | struct vmcs12 *vmcs12) |
| 838 | { |
| 839 | if (!nested_cpu_has_shadow_vmcs(vmcs12)) |
| 840 | return 0; |
| 841 | |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 842 | if (CC(!page_address_valid(vcpu, vmcs12->vmread_bitmap)) || |
| 843 | CC(!page_address_valid(vcpu, vmcs12->vmwrite_bitmap))) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 844 | return -EINVAL; |
| 845 | |
| 846 | return 0; |
| 847 | } |
| 848 | |
| 849 | static int nested_vmx_msr_check_common(struct kvm_vcpu *vcpu, |
| 850 | struct vmx_msr_entry *e) |
| 851 | { |
| 852 | /* x2APIC MSR accesses are not allowed */ |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 853 | if (CC(vcpu->arch.apic_base & X2APIC_ENABLE && e->index >> 8 == 0x8)) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 854 | return -EINVAL; |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 855 | if (CC(e->index == MSR_IA32_UCODE_WRITE) || /* SDM Table 35-2 */ |
| 856 | CC(e->index == MSR_IA32_UCODE_REV)) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 857 | return -EINVAL; |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 858 | if (CC(e->reserved != 0)) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 859 | return -EINVAL; |
| 860 | return 0; |
| 861 | } |
| 862 | |
| 863 | static int nested_vmx_load_msr_check(struct kvm_vcpu *vcpu, |
| 864 | struct vmx_msr_entry *e) |
| 865 | { |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 866 | if (CC(e->index == MSR_FS_BASE) || |
| 867 | CC(e->index == MSR_GS_BASE) || |
| 868 | CC(e->index == MSR_IA32_SMM_MONITOR_CTL) || /* SMM is not supported */ |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 869 | nested_vmx_msr_check_common(vcpu, e)) |
| 870 | return -EINVAL; |
| 871 | return 0; |
| 872 | } |
| 873 | |
| 874 | static int nested_vmx_store_msr_check(struct kvm_vcpu *vcpu, |
| 875 | struct vmx_msr_entry *e) |
| 876 | { |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 877 | if (CC(e->index == MSR_IA32_SMBASE) || /* SMM is not supported */ |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 878 | nested_vmx_msr_check_common(vcpu, e)) |
| 879 | return -EINVAL; |
| 880 | return 0; |
| 881 | } |
| 882 | |
Marc Orr | f0b5105 | 2019-09-17 11:50:57 -0700 | [diff] [blame] | 883 | static u32 nested_vmx_max_atomic_switch_msrs(struct kvm_vcpu *vcpu) |
| 884 | { |
| 885 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 886 | u64 vmx_misc = vmx_control_msr(vmx->nested.msrs.misc_low, |
| 887 | vmx->nested.msrs.misc_high); |
| 888 | |
| 889 | return (vmx_misc_max_msr(vmx_misc) + 1) * VMX_MISC_MSR_LIST_MULTIPLIER; |
| 890 | } |
| 891 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 892 | /* |
| 893 | * Load guest's/host's msr at nested entry/exit. |
| 894 | * return 0 for success, entry index for failure. |
Marc Orr | f0b5105 | 2019-09-17 11:50:57 -0700 | [diff] [blame] | 895 | * |
| 896 | * One of the failure modes for MSR load/store is when a list exceeds the |
| 897 | * virtual hardware's capacity. To maintain compatibility with hardware inasmuch |
| 898 | * as possible, process all valid entries before failing rather than precheck |
| 899 | * for a capacity violation. |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 900 | */ |
| 901 | static u32 nested_vmx_load_msr(struct kvm_vcpu *vcpu, u64 gpa, u32 count) |
| 902 | { |
| 903 | u32 i; |
| 904 | struct vmx_msr_entry e; |
Marc Orr | f0b5105 | 2019-09-17 11:50:57 -0700 | [diff] [blame] | 905 | u32 max_msr_list_size = nested_vmx_max_atomic_switch_msrs(vcpu); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 906 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 907 | for (i = 0; i < count; i++) { |
Marc Orr | f0b5105 | 2019-09-17 11:50:57 -0700 | [diff] [blame] | 908 | if (unlikely(i >= max_msr_list_size)) |
| 909 | goto fail; |
| 910 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 911 | if (kvm_vcpu_read_guest(vcpu, gpa + i * sizeof(e), |
| 912 | &e, sizeof(e))) { |
| 913 | pr_debug_ratelimited( |
| 914 | "%s cannot read MSR entry (%u, 0x%08llx)\n", |
| 915 | __func__, i, gpa + i * sizeof(e)); |
| 916 | goto fail; |
| 917 | } |
| 918 | if (nested_vmx_load_msr_check(vcpu, &e)) { |
| 919 | pr_debug_ratelimited( |
| 920 | "%s check failed (%u, 0x%x, 0x%x)\n", |
| 921 | __func__, i, e.index, e.reserved); |
| 922 | goto fail; |
| 923 | } |
Sean Christopherson | f20935d | 2019-09-05 14:22:54 -0700 | [diff] [blame] | 924 | if (kvm_set_msr(vcpu, e.index, e.value)) { |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 925 | pr_debug_ratelimited( |
| 926 | "%s cannot write MSR (%u, 0x%x, 0x%llx)\n", |
| 927 | __func__, i, e.index, e.value); |
| 928 | goto fail; |
| 929 | } |
| 930 | } |
| 931 | return 0; |
| 932 | fail: |
Sean Christopherson | 68cda40 | 2020-05-11 15:05:29 -0700 | [diff] [blame] | 933 | /* 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] | 934 | return i + 1; |
| 935 | } |
| 936 | |
Aaron Lewis | 662f1d1 | 2019-11-07 21:14:39 -0800 | [diff] [blame] | 937 | static bool nested_vmx_get_vmexit_msr_value(struct kvm_vcpu *vcpu, |
| 938 | u32 msr_index, |
| 939 | u64 *data) |
| 940 | { |
| 941 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 942 | |
| 943 | /* |
| 944 | * If the L0 hypervisor stored a more accurate value for the TSC that |
| 945 | * does not include the time taken for emulation of the L2->L1 |
| 946 | * VM-exit in L0, use the more accurate value. |
| 947 | */ |
| 948 | if (msr_index == MSR_IA32_TSC) { |
Sean Christopherson | a128a93 | 2020-09-23 11:03:57 -0700 | [diff] [blame] | 949 | int i = vmx_find_loadstore_msr_slot(&vmx->msr_autostore.guest, |
| 950 | MSR_IA32_TSC); |
Aaron Lewis | 662f1d1 | 2019-11-07 21:14:39 -0800 | [diff] [blame] | 951 | |
Sean Christopherson | a128a93 | 2020-09-23 11:03:57 -0700 | [diff] [blame] | 952 | if (i >= 0) { |
| 953 | u64 val = vmx->msr_autostore.guest.val[i].value; |
Aaron Lewis | 662f1d1 | 2019-11-07 21:14:39 -0800 | [diff] [blame] | 954 | |
| 955 | *data = kvm_read_l1_tsc(vcpu, val); |
| 956 | return true; |
| 957 | } |
| 958 | } |
| 959 | |
| 960 | if (kvm_get_msr(vcpu, msr_index, data)) { |
| 961 | pr_debug_ratelimited("%s cannot read MSR (0x%x)\n", __func__, |
| 962 | msr_index); |
| 963 | return false; |
| 964 | } |
| 965 | return true; |
| 966 | } |
| 967 | |
Aaron Lewis | 365d3d5 | 2019-11-07 21:14:36 -0800 | [diff] [blame] | 968 | static bool read_and_check_msr_entry(struct kvm_vcpu *vcpu, u64 gpa, int i, |
| 969 | struct vmx_msr_entry *e) |
| 970 | { |
| 971 | if (kvm_vcpu_read_guest(vcpu, |
| 972 | gpa + i * sizeof(*e), |
| 973 | e, 2 * sizeof(u32))) { |
| 974 | pr_debug_ratelimited( |
| 975 | "%s cannot read MSR entry (%u, 0x%08llx)\n", |
| 976 | __func__, i, gpa + i * sizeof(*e)); |
| 977 | return false; |
| 978 | } |
| 979 | if (nested_vmx_store_msr_check(vcpu, e)) { |
| 980 | pr_debug_ratelimited( |
| 981 | "%s check failed (%u, 0x%x, 0x%x)\n", |
| 982 | __func__, i, e->index, e->reserved); |
| 983 | return false; |
| 984 | } |
| 985 | return true; |
| 986 | } |
| 987 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 988 | static int nested_vmx_store_msr(struct kvm_vcpu *vcpu, u64 gpa, u32 count) |
| 989 | { |
Sean Christopherson | f20935d | 2019-09-05 14:22:54 -0700 | [diff] [blame] | 990 | u64 data; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 991 | u32 i; |
| 992 | struct vmx_msr_entry e; |
Marc Orr | f0b5105 | 2019-09-17 11:50:57 -0700 | [diff] [blame] | 993 | u32 max_msr_list_size = nested_vmx_max_atomic_switch_msrs(vcpu); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 994 | |
| 995 | for (i = 0; i < count; i++) { |
Marc Orr | f0b5105 | 2019-09-17 11:50:57 -0700 | [diff] [blame] | 996 | if (unlikely(i >= max_msr_list_size)) |
| 997 | return -EINVAL; |
| 998 | |
Aaron Lewis | 365d3d5 | 2019-11-07 21:14:36 -0800 | [diff] [blame] | 999 | if (!read_and_check_msr_entry(vcpu, gpa, i, &e)) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1000 | return -EINVAL; |
Aaron Lewis | 365d3d5 | 2019-11-07 21:14:36 -0800 | [diff] [blame] | 1001 | |
Aaron Lewis | 662f1d1 | 2019-11-07 21:14:39 -0800 | [diff] [blame] | 1002 | if (!nested_vmx_get_vmexit_msr_value(vcpu, e.index, &data)) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1003 | return -EINVAL; |
Aaron Lewis | 662f1d1 | 2019-11-07 21:14:39 -0800 | [diff] [blame] | 1004 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1005 | if (kvm_vcpu_write_guest(vcpu, |
| 1006 | gpa + i * sizeof(e) + |
| 1007 | offsetof(struct vmx_msr_entry, value), |
Sean Christopherson | f20935d | 2019-09-05 14:22:54 -0700 | [diff] [blame] | 1008 | &data, sizeof(data))) { |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1009 | pr_debug_ratelimited( |
| 1010 | "%s cannot write MSR (%u, 0x%x, 0x%llx)\n", |
Sean Christopherson | f20935d | 2019-09-05 14:22:54 -0700 | [diff] [blame] | 1011 | __func__, i, e.index, data); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1012 | return -EINVAL; |
| 1013 | } |
| 1014 | } |
| 1015 | return 0; |
| 1016 | } |
| 1017 | |
Aaron Lewis | 662f1d1 | 2019-11-07 21:14:39 -0800 | [diff] [blame] | 1018 | static bool nested_msr_store_list_has_msr(struct kvm_vcpu *vcpu, u32 msr_index) |
| 1019 | { |
| 1020 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
| 1021 | u32 count = vmcs12->vm_exit_msr_store_count; |
| 1022 | u64 gpa = vmcs12->vm_exit_msr_store_addr; |
| 1023 | struct vmx_msr_entry e; |
| 1024 | u32 i; |
| 1025 | |
| 1026 | for (i = 0; i < count; i++) { |
| 1027 | if (!read_and_check_msr_entry(vcpu, gpa, i, &e)) |
| 1028 | return false; |
| 1029 | |
| 1030 | if (e.index == msr_index) |
| 1031 | return true; |
| 1032 | } |
| 1033 | return false; |
| 1034 | } |
| 1035 | |
| 1036 | static void prepare_vmx_msr_autostore_list(struct kvm_vcpu *vcpu, |
| 1037 | u32 msr_index) |
| 1038 | { |
| 1039 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 1040 | struct vmx_msrs *autostore = &vmx->msr_autostore.guest; |
| 1041 | bool in_vmcs12_store_list; |
Sean Christopherson | a128a93 | 2020-09-23 11:03:57 -0700 | [diff] [blame] | 1042 | int msr_autostore_slot; |
Aaron Lewis | 662f1d1 | 2019-11-07 21:14:39 -0800 | [diff] [blame] | 1043 | bool in_autostore_list; |
| 1044 | int last; |
| 1045 | |
Sean Christopherson | a128a93 | 2020-09-23 11:03:57 -0700 | [diff] [blame] | 1046 | msr_autostore_slot = vmx_find_loadstore_msr_slot(autostore, msr_index); |
| 1047 | in_autostore_list = msr_autostore_slot >= 0; |
Aaron Lewis | 662f1d1 | 2019-11-07 21:14:39 -0800 | [diff] [blame] | 1048 | in_vmcs12_store_list = nested_msr_store_list_has_msr(vcpu, msr_index); |
| 1049 | |
| 1050 | if (in_vmcs12_store_list && !in_autostore_list) { |
Sean Christopherson | ce833b2 | 2020-09-23 11:03:56 -0700 | [diff] [blame] | 1051 | if (autostore->nr == MAX_NR_LOADSTORE_MSRS) { |
Aaron Lewis | 662f1d1 | 2019-11-07 21:14:39 -0800 | [diff] [blame] | 1052 | /* |
| 1053 | * Emulated VMEntry does not fail here. Instead a less |
| 1054 | * accurate value will be returned by |
| 1055 | * nested_vmx_get_vmexit_msr_value() using kvm_get_msr() |
| 1056 | * instead of reading the value from the vmcs02 VMExit |
| 1057 | * MSR-store area. |
| 1058 | */ |
| 1059 | pr_warn_ratelimited( |
| 1060 | "Not enough msr entries in msr_autostore. Can't add msr %x\n", |
| 1061 | msr_index); |
| 1062 | return; |
| 1063 | } |
| 1064 | last = autostore->nr++; |
| 1065 | autostore->val[last].index = msr_index; |
| 1066 | } else if (!in_vmcs12_store_list && in_autostore_list) { |
| 1067 | last = --autostore->nr; |
Sean Christopherson | a128a93 | 2020-09-23 11:03:57 -0700 | [diff] [blame] | 1068 | autostore->val[msr_autostore_slot] = autostore->val[last]; |
Aaron Lewis | 662f1d1 | 2019-11-07 21:14:39 -0800 | [diff] [blame] | 1069 | } |
| 1070 | } |
| 1071 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1072 | /* |
Sean Christopherson | ea79a75 | 2020-02-04 07:32:59 -0800 | [diff] [blame] | 1073 | * Load guest's/host's cr3 at nested entry/exit. @nested_ept is true if we are |
| 1074 | * emulating VM-Entry into a guest with EPT enabled. On failure, the expected |
| 1075 | * Exit Qualification (for a VM-Entry consistency check VM-Exit) is assigned to |
| 1076 | * @entry_failure_code. |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1077 | */ |
Maxim Levitsky | 0f85722 | 2021-06-07 12:02:00 +0300 | [diff] [blame] | 1078 | static int nested_vmx_load_cr3(struct kvm_vcpu *vcpu, unsigned long cr3, |
| 1079 | bool nested_ept, bool reload_pdptrs, |
Sean Christopherson | 68cda40 | 2020-05-11 15:05:29 -0700 | [diff] [blame] | 1080 | enum vm_entry_failure_code *entry_failure_code) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1081 | { |
Sean Christopherson | 636e8b7 | 2021-02-03 16:01:10 -0800 | [diff] [blame] | 1082 | if (CC(kvm_vcpu_is_illegal_gpa(vcpu, cr3))) { |
Sean Christopherson | 0cc6920 | 2020-05-01 21:32:26 -0700 | [diff] [blame] | 1083 | *entry_failure_code = ENTRY_FAIL_DEFAULT; |
| 1084 | return -EINVAL; |
| 1085 | } |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1086 | |
Sean Christopherson | 0cc6920 | 2020-05-01 21:32:26 -0700 | [diff] [blame] | 1087 | /* |
| 1088 | * If PAE paging and EPT are both on, CR3 is not used by the CPU and |
| 1089 | * must not be dereferenced. |
| 1090 | */ |
Maxim Levitsky | 0f85722 | 2021-06-07 12:02:00 +0300 | [diff] [blame] | 1091 | if (reload_pdptrs && !nested_ept && is_pae_paging(vcpu) && |
Sean Christopherson | bcb72d0 | 2021-06-07 12:01:56 +0300 | [diff] [blame] | 1092 | CC(!load_pdptrs(vcpu, vcpu->arch.walk_mmu, cr3))) { |
| 1093 | *entry_failure_code = ENTRY_FAIL_PDPTE; |
| 1094 | return -EINVAL; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1095 | } |
| 1096 | |
Sean Christopherson | 50a4179 | 2021-06-09 16:42:28 -0700 | [diff] [blame] | 1097 | if (!nested_ept) |
Sean Christopherson | b512910 | 2021-06-09 16:42:27 -0700 | [diff] [blame] | 1098 | kvm_mmu_new_pgd(vcpu, cr3); |
Sean Christopherson | 07ffaf3 | 2021-06-09 16:42:21 -0700 | [diff] [blame] | 1099 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1100 | vcpu->arch.cr3 = cr3; |
Sean Christopherson | cb3c1e2 | 2019-09-27 14:45:22 -0700 | [diff] [blame] | 1101 | kvm_register_mark_available(vcpu, VCPU_EXREG_CR3); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1102 | |
Sean Christopherson | 616007c | 2021-06-22 10:57:34 -0700 | [diff] [blame] | 1103 | /* 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] | 1104 | kvm_init_mmu(vcpu); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1105 | |
| 1106 | return 0; |
| 1107 | } |
| 1108 | |
| 1109 | /* |
| 1110 | * Returns if KVM is able to config CPU to tag TLB entries |
| 1111 | * populated by L2 differently than TLB entries populated |
| 1112 | * by L1. |
| 1113 | * |
Liran Alon | 992edea | 2019-11-20 14:24:52 +0200 | [diff] [blame] | 1114 | * If L0 uses EPT, L1 and L2 run with different EPTP because |
| 1115 | * guest_mode is part of kvm_mmu_page_role. Thus, TLB entries |
| 1116 | * are tagged with different EPTP. |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1117 | * |
| 1118 | * If L1 uses VPID and we allocated a vpid02, TLB entries are tagged |
| 1119 | * with different VPID (L1 entries are tagged with vmx->vpid |
| 1120 | * while L2 entries are tagged with vmx->nested.vpid02). |
| 1121 | */ |
| 1122 | static bool nested_has_guest_tlb_tag(struct kvm_vcpu *vcpu) |
| 1123 | { |
| 1124 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
| 1125 | |
Liran Alon | 992edea | 2019-11-20 14:24:52 +0200 | [diff] [blame] | 1126 | return enable_ept || |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1127 | (nested_cpu_has_vpid(vmcs12) && to_vmx(vcpu)->nested.vpid02); |
| 1128 | } |
| 1129 | |
Sean Christopherson | 50b265a | 2020-03-20 14:28:19 -0700 | [diff] [blame] | 1130 | static void nested_vmx_transition_tlb_flush(struct kvm_vcpu *vcpu, |
| 1131 | struct vmcs12 *vmcs12, |
| 1132 | bool is_vmenter) |
| 1133 | { |
| 1134 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 1135 | |
| 1136 | /* |
Sean Christopherson | 50a4179 | 2021-06-09 16:42:28 -0700 | [diff] [blame] | 1137 | * If vmcs12 doesn't use VPID, L1 expects linear and combined mappings |
| 1138 | * for *all* contexts to be flushed on VM-Enter/VM-Exit, i.e. it's a |
| 1139 | * full TLB flush from the guest's perspective. This is required even |
| 1140 | * if VPID is disabled in the host as KVM may need to synchronize the |
| 1141 | * MMU in response to the guest TLB flush. |
| 1142 | * |
| 1143 | * Note, using TLB_FLUSH_GUEST is correct even if nested EPT is in use. |
| 1144 | * EPT is a special snowflake, as guest-physical mappings aren't |
| 1145 | * flushed on VPID invalidations, including VM-Enter or VM-Exit with |
| 1146 | * VPID disabled. As a result, KVM _never_ needs to sync nEPT |
| 1147 | * entries on VM-Enter because L1 can't rely on VM-Enter to flush |
| 1148 | * those mappings. |
Sean Christopherson | 50b265a | 2020-03-20 14:28:19 -0700 | [diff] [blame] | 1149 | */ |
Sean Christopherson | 50a4179 | 2021-06-09 16:42:28 -0700 | [diff] [blame] | 1150 | if (!nested_cpu_has_vpid(vmcs12)) { |
| 1151 | kvm_make_request(KVM_REQ_TLB_FLUSH_GUEST, vcpu); |
Sean Christopherson | 50b265a | 2020-03-20 14:28:19 -0700 | [diff] [blame] | 1152 | return; |
Sean Christopherson | 50a4179 | 2021-06-09 16:42:28 -0700 | [diff] [blame] | 1153 | } |
| 1154 | |
| 1155 | /* L2 should never have a VPID if VPID is disabled. */ |
| 1156 | WARN_ON(!enable_vpid); |
Sean Christopherson | 50b265a | 2020-03-20 14:28:19 -0700 | [diff] [blame] | 1157 | |
| 1158 | /* |
Sean Christopherson | 50b265a | 2020-03-20 14:28:19 -0700 | [diff] [blame] | 1159 | * If VPID is enabled and used by vmc12, but L2 does not have a unique |
| 1160 | * TLB tag (ASID), i.e. EPT is disabled and KVM was unable to allocate |
Sean Christopherson | c51e1ff | 2020-03-20 14:28:22 -0700 | [diff] [blame] | 1161 | * a VPID for L2, flush the current context as the effective ASID is |
| 1162 | * common to both L1 and L2. |
Sean Christopherson | 50b265a | 2020-03-20 14:28:19 -0700 | [diff] [blame] | 1163 | * |
| 1164 | * Defer the flush so that it runs after vmcs02.EPTP has been set by |
| 1165 | * KVM_REQ_LOAD_MMU_PGD (if nested EPT is enabled) and to avoid |
| 1166 | * redundant flushes further down the nested pipeline. |
| 1167 | * |
| 1168 | * If a TLB flush isn't required due to any of the above, and vpid12 is |
| 1169 | * changing then the new "virtual" VPID (vpid12) will reuse the same |
Sean Christopherson | 50a4179 | 2021-06-09 16:42:28 -0700 | [diff] [blame] | 1170 | * "real" VPID (vpid02), and so needs to be flushed. There's no direct |
Sean Christopherson | 50b265a | 2020-03-20 14:28:19 -0700 | [diff] [blame] | 1171 | * mapping between vpid02 and vpid12, vpid02 is per-vCPU and reused for |
Sean Christopherson | 50a4179 | 2021-06-09 16:42:28 -0700 | [diff] [blame] | 1172 | * all nested vCPUs. Remember, a flush on VM-Enter does not invalidate |
| 1173 | * guest-physical mappings, so there is no need to sync the nEPT MMU. |
Sean Christopherson | 50b265a | 2020-03-20 14:28:19 -0700 | [diff] [blame] | 1174 | */ |
Sean Christopherson | 50a4179 | 2021-06-09 16:42:28 -0700 | [diff] [blame] | 1175 | if (!nested_has_guest_tlb_tag(vcpu)) { |
Sean Christopherson | c51e1ff | 2020-03-20 14:28:22 -0700 | [diff] [blame] | 1176 | kvm_make_request(KVM_REQ_TLB_FLUSH_CURRENT, vcpu); |
Sean Christopherson | 50b265a | 2020-03-20 14:28:19 -0700 | [diff] [blame] | 1177 | } else if (is_vmenter && |
| 1178 | vmcs12->virtual_processor_id != vmx->nested.last_vpid) { |
| 1179 | vmx->nested.last_vpid = vmcs12->virtual_processor_id; |
| 1180 | vpid_sync_context(nested_get_vpid02(vcpu)); |
| 1181 | } |
| 1182 | } |
| 1183 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1184 | static bool is_bitwise_subset(u64 superset, u64 subset, u64 mask) |
| 1185 | { |
| 1186 | superset &= mask; |
| 1187 | subset &= mask; |
| 1188 | |
| 1189 | return (superset | subset) == superset; |
| 1190 | } |
| 1191 | |
| 1192 | static int vmx_restore_vmx_basic(struct vcpu_vmx *vmx, u64 data) |
| 1193 | { |
| 1194 | const u64 feature_and_reserved = |
| 1195 | /* feature (except bit 48; see below) */ |
| 1196 | BIT_ULL(49) | BIT_ULL(54) | BIT_ULL(55) | |
| 1197 | /* reserved */ |
| 1198 | BIT_ULL(31) | GENMASK_ULL(47, 45) | GENMASK_ULL(63, 56); |
| 1199 | u64 vmx_basic = vmx->nested.msrs.basic; |
| 1200 | |
| 1201 | if (!is_bitwise_subset(vmx_basic, data, feature_and_reserved)) |
| 1202 | return -EINVAL; |
| 1203 | |
| 1204 | /* |
| 1205 | * KVM does not emulate a version of VMX that constrains physical |
| 1206 | * addresses of VMX structures (e.g. VMCS) to 32-bits. |
| 1207 | */ |
| 1208 | if (data & BIT_ULL(48)) |
| 1209 | return -EINVAL; |
| 1210 | |
| 1211 | if (vmx_basic_vmcs_revision_id(vmx_basic) != |
| 1212 | vmx_basic_vmcs_revision_id(data)) |
| 1213 | return -EINVAL; |
| 1214 | |
| 1215 | if (vmx_basic_vmcs_size(vmx_basic) > vmx_basic_vmcs_size(data)) |
| 1216 | return -EINVAL; |
| 1217 | |
| 1218 | vmx->nested.msrs.basic = data; |
| 1219 | return 0; |
| 1220 | } |
| 1221 | |
| 1222 | static int |
| 1223 | vmx_restore_control_msr(struct vcpu_vmx *vmx, u32 msr_index, u64 data) |
| 1224 | { |
| 1225 | u64 supported; |
| 1226 | u32 *lowp, *highp; |
| 1227 | |
| 1228 | switch (msr_index) { |
| 1229 | case MSR_IA32_VMX_TRUE_PINBASED_CTLS: |
| 1230 | lowp = &vmx->nested.msrs.pinbased_ctls_low; |
| 1231 | highp = &vmx->nested.msrs.pinbased_ctls_high; |
| 1232 | break; |
| 1233 | case MSR_IA32_VMX_TRUE_PROCBASED_CTLS: |
| 1234 | lowp = &vmx->nested.msrs.procbased_ctls_low; |
| 1235 | highp = &vmx->nested.msrs.procbased_ctls_high; |
| 1236 | break; |
| 1237 | case MSR_IA32_VMX_TRUE_EXIT_CTLS: |
| 1238 | lowp = &vmx->nested.msrs.exit_ctls_low; |
| 1239 | highp = &vmx->nested.msrs.exit_ctls_high; |
| 1240 | break; |
| 1241 | case MSR_IA32_VMX_TRUE_ENTRY_CTLS: |
| 1242 | lowp = &vmx->nested.msrs.entry_ctls_low; |
| 1243 | highp = &vmx->nested.msrs.entry_ctls_high; |
| 1244 | break; |
| 1245 | case MSR_IA32_VMX_PROCBASED_CTLS2: |
| 1246 | lowp = &vmx->nested.msrs.secondary_ctls_low; |
| 1247 | highp = &vmx->nested.msrs.secondary_ctls_high; |
| 1248 | break; |
| 1249 | default: |
| 1250 | BUG(); |
| 1251 | } |
| 1252 | |
| 1253 | supported = vmx_control_msr(*lowp, *highp); |
| 1254 | |
| 1255 | /* Check must-be-1 bits are still 1. */ |
| 1256 | if (!is_bitwise_subset(data, supported, GENMASK_ULL(31, 0))) |
| 1257 | return -EINVAL; |
| 1258 | |
| 1259 | /* Check must-be-0 bits are still 0. */ |
| 1260 | if (!is_bitwise_subset(supported, data, GENMASK_ULL(63, 32))) |
| 1261 | return -EINVAL; |
| 1262 | |
| 1263 | *lowp = data; |
| 1264 | *highp = data >> 32; |
| 1265 | return 0; |
| 1266 | } |
| 1267 | |
| 1268 | static int vmx_restore_vmx_misc(struct vcpu_vmx *vmx, u64 data) |
| 1269 | { |
| 1270 | const u64 feature_and_reserved_bits = |
| 1271 | /* feature */ |
| 1272 | BIT_ULL(5) | GENMASK_ULL(8, 6) | BIT_ULL(14) | BIT_ULL(15) | |
| 1273 | BIT_ULL(28) | BIT_ULL(29) | BIT_ULL(30) | |
| 1274 | /* reserved */ |
| 1275 | GENMASK_ULL(13, 9) | BIT_ULL(31); |
| 1276 | u64 vmx_misc; |
| 1277 | |
| 1278 | vmx_misc = vmx_control_msr(vmx->nested.msrs.misc_low, |
| 1279 | vmx->nested.msrs.misc_high); |
| 1280 | |
| 1281 | if (!is_bitwise_subset(vmx_misc, data, feature_and_reserved_bits)) |
| 1282 | return -EINVAL; |
| 1283 | |
| 1284 | if ((vmx->nested.msrs.pinbased_ctls_high & |
| 1285 | PIN_BASED_VMX_PREEMPTION_TIMER) && |
| 1286 | vmx_misc_preemption_timer_rate(data) != |
| 1287 | vmx_misc_preemption_timer_rate(vmx_misc)) |
| 1288 | return -EINVAL; |
| 1289 | |
| 1290 | if (vmx_misc_cr3_count(data) > vmx_misc_cr3_count(vmx_misc)) |
| 1291 | return -EINVAL; |
| 1292 | |
| 1293 | if (vmx_misc_max_msr(data) > vmx_misc_max_msr(vmx_misc)) |
| 1294 | return -EINVAL; |
| 1295 | |
| 1296 | if (vmx_misc_mseg_revid(data) != vmx_misc_mseg_revid(vmx_misc)) |
| 1297 | return -EINVAL; |
| 1298 | |
| 1299 | vmx->nested.msrs.misc_low = data; |
| 1300 | vmx->nested.msrs.misc_high = data >> 32; |
| 1301 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1302 | return 0; |
| 1303 | } |
| 1304 | |
| 1305 | static int vmx_restore_vmx_ept_vpid_cap(struct vcpu_vmx *vmx, u64 data) |
| 1306 | { |
| 1307 | u64 vmx_ept_vpid_cap; |
| 1308 | |
| 1309 | vmx_ept_vpid_cap = vmx_control_msr(vmx->nested.msrs.ept_caps, |
| 1310 | vmx->nested.msrs.vpid_caps); |
| 1311 | |
| 1312 | /* Every bit is either reserved or a feature bit. */ |
| 1313 | if (!is_bitwise_subset(vmx_ept_vpid_cap, data, -1ULL)) |
| 1314 | return -EINVAL; |
| 1315 | |
| 1316 | vmx->nested.msrs.ept_caps = data; |
| 1317 | vmx->nested.msrs.vpid_caps = data >> 32; |
| 1318 | return 0; |
| 1319 | } |
| 1320 | |
| 1321 | static int vmx_restore_fixed0_msr(struct vcpu_vmx *vmx, u32 msr_index, u64 data) |
| 1322 | { |
| 1323 | u64 *msr; |
| 1324 | |
| 1325 | switch (msr_index) { |
| 1326 | case MSR_IA32_VMX_CR0_FIXED0: |
| 1327 | msr = &vmx->nested.msrs.cr0_fixed0; |
| 1328 | break; |
| 1329 | case MSR_IA32_VMX_CR4_FIXED0: |
| 1330 | msr = &vmx->nested.msrs.cr4_fixed0; |
| 1331 | break; |
| 1332 | default: |
| 1333 | BUG(); |
| 1334 | } |
| 1335 | |
| 1336 | /* |
| 1337 | * 1 bits (which indicates bits which "must-be-1" during VMX operation) |
| 1338 | * must be 1 in the restored value. |
| 1339 | */ |
| 1340 | if (!is_bitwise_subset(data, *msr, -1ULL)) |
| 1341 | return -EINVAL; |
| 1342 | |
| 1343 | *msr = data; |
| 1344 | return 0; |
| 1345 | } |
| 1346 | |
| 1347 | /* |
| 1348 | * Called when userspace is restoring VMX MSRs. |
| 1349 | * |
| 1350 | * Returns 0 on success, non-0 otherwise. |
| 1351 | */ |
| 1352 | int vmx_set_vmx_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 data) |
| 1353 | { |
| 1354 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 1355 | |
| 1356 | /* |
| 1357 | * Don't allow changes to the VMX capability MSRs while the vCPU |
| 1358 | * is in VMX operation. |
| 1359 | */ |
| 1360 | if (vmx->nested.vmxon) |
| 1361 | return -EBUSY; |
| 1362 | |
| 1363 | switch (msr_index) { |
| 1364 | case MSR_IA32_VMX_BASIC: |
| 1365 | return vmx_restore_vmx_basic(vmx, data); |
| 1366 | case MSR_IA32_VMX_PINBASED_CTLS: |
| 1367 | case MSR_IA32_VMX_PROCBASED_CTLS: |
| 1368 | case MSR_IA32_VMX_EXIT_CTLS: |
| 1369 | case MSR_IA32_VMX_ENTRY_CTLS: |
| 1370 | /* |
| 1371 | * The "non-true" VMX capability MSRs are generated from the |
| 1372 | * "true" MSRs, so we do not support restoring them directly. |
| 1373 | * |
| 1374 | * If userspace wants to emulate VMX_BASIC[55]=0, userspace |
| 1375 | * should restore the "true" MSRs with the must-be-1 bits |
| 1376 | * set according to the SDM Vol 3. A.2 "RESERVED CONTROLS AND |
| 1377 | * DEFAULT SETTINGS". |
| 1378 | */ |
| 1379 | return -EINVAL; |
| 1380 | case MSR_IA32_VMX_TRUE_PINBASED_CTLS: |
| 1381 | case MSR_IA32_VMX_TRUE_PROCBASED_CTLS: |
| 1382 | case MSR_IA32_VMX_TRUE_EXIT_CTLS: |
| 1383 | case MSR_IA32_VMX_TRUE_ENTRY_CTLS: |
| 1384 | case MSR_IA32_VMX_PROCBASED_CTLS2: |
| 1385 | return vmx_restore_control_msr(vmx, msr_index, data); |
| 1386 | case MSR_IA32_VMX_MISC: |
| 1387 | return vmx_restore_vmx_misc(vmx, data); |
| 1388 | case MSR_IA32_VMX_CR0_FIXED0: |
| 1389 | case MSR_IA32_VMX_CR4_FIXED0: |
| 1390 | return vmx_restore_fixed0_msr(vmx, msr_index, data); |
| 1391 | case MSR_IA32_VMX_CR0_FIXED1: |
| 1392 | case MSR_IA32_VMX_CR4_FIXED1: |
| 1393 | /* |
| 1394 | * These MSRs are generated based on the vCPU's CPUID, so we |
| 1395 | * do not support restoring them directly. |
| 1396 | */ |
| 1397 | return -EINVAL; |
| 1398 | case MSR_IA32_VMX_EPT_VPID_CAP: |
| 1399 | return vmx_restore_vmx_ept_vpid_cap(vmx, data); |
| 1400 | case MSR_IA32_VMX_VMCS_ENUM: |
| 1401 | vmx->nested.msrs.vmcs_enum = data; |
| 1402 | return 0; |
Paolo Bonzini | e8a70bd | 2019-07-02 14:40:40 +0200 | [diff] [blame] | 1403 | case MSR_IA32_VMX_VMFUNC: |
| 1404 | if (data & ~vmx->nested.msrs.vmfunc_controls) |
| 1405 | return -EINVAL; |
| 1406 | vmx->nested.msrs.vmfunc_controls = data; |
| 1407 | return 0; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1408 | default: |
| 1409 | /* |
| 1410 | * The rest of the VMX capability MSRs do not support restore. |
| 1411 | */ |
| 1412 | return -EINVAL; |
| 1413 | } |
| 1414 | } |
| 1415 | |
| 1416 | /* Returns 0 on success, non-0 otherwise. */ |
| 1417 | int vmx_get_vmx_msr(struct nested_vmx_msrs *msrs, u32 msr_index, u64 *pdata) |
| 1418 | { |
| 1419 | switch (msr_index) { |
| 1420 | case MSR_IA32_VMX_BASIC: |
| 1421 | *pdata = msrs->basic; |
| 1422 | break; |
| 1423 | case MSR_IA32_VMX_TRUE_PINBASED_CTLS: |
| 1424 | case MSR_IA32_VMX_PINBASED_CTLS: |
| 1425 | *pdata = vmx_control_msr( |
| 1426 | msrs->pinbased_ctls_low, |
| 1427 | msrs->pinbased_ctls_high); |
| 1428 | if (msr_index == MSR_IA32_VMX_PINBASED_CTLS) |
| 1429 | *pdata |= PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR; |
| 1430 | break; |
| 1431 | case MSR_IA32_VMX_TRUE_PROCBASED_CTLS: |
| 1432 | case MSR_IA32_VMX_PROCBASED_CTLS: |
| 1433 | *pdata = vmx_control_msr( |
| 1434 | msrs->procbased_ctls_low, |
| 1435 | msrs->procbased_ctls_high); |
| 1436 | if (msr_index == MSR_IA32_VMX_PROCBASED_CTLS) |
| 1437 | *pdata |= CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR; |
| 1438 | break; |
| 1439 | case MSR_IA32_VMX_TRUE_EXIT_CTLS: |
| 1440 | case MSR_IA32_VMX_EXIT_CTLS: |
| 1441 | *pdata = vmx_control_msr( |
| 1442 | msrs->exit_ctls_low, |
| 1443 | msrs->exit_ctls_high); |
| 1444 | if (msr_index == MSR_IA32_VMX_EXIT_CTLS) |
| 1445 | *pdata |= VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR; |
| 1446 | break; |
| 1447 | case MSR_IA32_VMX_TRUE_ENTRY_CTLS: |
| 1448 | case MSR_IA32_VMX_ENTRY_CTLS: |
| 1449 | *pdata = vmx_control_msr( |
| 1450 | msrs->entry_ctls_low, |
| 1451 | msrs->entry_ctls_high); |
| 1452 | if (msr_index == MSR_IA32_VMX_ENTRY_CTLS) |
| 1453 | *pdata |= VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR; |
| 1454 | break; |
| 1455 | case MSR_IA32_VMX_MISC: |
| 1456 | *pdata = vmx_control_msr( |
| 1457 | msrs->misc_low, |
| 1458 | msrs->misc_high); |
| 1459 | break; |
| 1460 | case MSR_IA32_VMX_CR0_FIXED0: |
| 1461 | *pdata = msrs->cr0_fixed0; |
| 1462 | break; |
| 1463 | case MSR_IA32_VMX_CR0_FIXED1: |
| 1464 | *pdata = msrs->cr0_fixed1; |
| 1465 | break; |
| 1466 | case MSR_IA32_VMX_CR4_FIXED0: |
| 1467 | *pdata = msrs->cr4_fixed0; |
| 1468 | break; |
| 1469 | case MSR_IA32_VMX_CR4_FIXED1: |
| 1470 | *pdata = msrs->cr4_fixed1; |
| 1471 | break; |
| 1472 | case MSR_IA32_VMX_VMCS_ENUM: |
| 1473 | *pdata = msrs->vmcs_enum; |
| 1474 | break; |
| 1475 | case MSR_IA32_VMX_PROCBASED_CTLS2: |
| 1476 | *pdata = vmx_control_msr( |
| 1477 | msrs->secondary_ctls_low, |
| 1478 | msrs->secondary_ctls_high); |
| 1479 | break; |
| 1480 | case MSR_IA32_VMX_EPT_VPID_CAP: |
| 1481 | *pdata = msrs->ept_caps | |
| 1482 | ((u64)msrs->vpid_caps << 32); |
| 1483 | break; |
| 1484 | case MSR_IA32_VMX_VMFUNC: |
| 1485 | *pdata = msrs->vmfunc_controls; |
| 1486 | break; |
| 1487 | default: |
| 1488 | return 1; |
| 1489 | } |
| 1490 | |
| 1491 | return 0; |
| 1492 | } |
| 1493 | |
| 1494 | /* |
Sean Christopherson | fadcead | 2019-05-07 08:36:23 -0700 | [diff] [blame] | 1495 | * Copy the writable VMCS shadow fields back to the VMCS12, in case they have |
| 1496 | * been modified by the L1 guest. Note, "writable" in this context means |
| 1497 | * "writable by the guest", i.e. tagged SHADOW_FIELD_RW; the set of |
| 1498 | * fields tagged SHADOW_FIELD_RO may or may not align with the "read-only" |
| 1499 | * VM-exit information fields (which are actually writable if the vCPU is |
| 1500 | * configured to support "VMWRITE to any supported field in the VMCS"). |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1501 | */ |
| 1502 | static void copy_shadow_to_vmcs12(struct vcpu_vmx *vmx) |
| 1503 | { |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1504 | struct vmcs *shadow_vmcs = vmx->vmcs01.shadow_vmcs; |
Sean Christopherson | fadcead | 2019-05-07 08:36:23 -0700 | [diff] [blame] | 1505 | struct vmcs12 *vmcs12 = get_vmcs12(&vmx->vcpu); |
Sean Christopherson | 1c6f0b4 | 2019-05-07 08:36:25 -0700 | [diff] [blame] | 1506 | struct shadow_vmcs_field field; |
| 1507 | unsigned long val; |
Sean Christopherson | fadcead | 2019-05-07 08:36:23 -0700 | [diff] [blame] | 1508 | int i; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1509 | |
Paolo Bonzini | 88dddc1 | 2019-07-19 18:41:10 +0200 | [diff] [blame] | 1510 | if (WARN_ON(!shadow_vmcs)) |
| 1511 | return; |
| 1512 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1513 | preempt_disable(); |
| 1514 | |
| 1515 | vmcs_load(shadow_vmcs); |
| 1516 | |
Sean Christopherson | fadcead | 2019-05-07 08:36:23 -0700 | [diff] [blame] | 1517 | for (i = 0; i < max_shadow_read_write_fields; i++) { |
| 1518 | field = shadow_read_write_fields[i]; |
Sean Christopherson | 1c6f0b4 | 2019-05-07 08:36:25 -0700 | [diff] [blame] | 1519 | val = __vmcs_readl(field.encoding); |
| 1520 | vmcs12_write_any(vmcs12, field.encoding, field.offset, val); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1521 | } |
| 1522 | |
| 1523 | vmcs_clear(shadow_vmcs); |
| 1524 | vmcs_load(vmx->loaded_vmcs->vmcs); |
| 1525 | |
| 1526 | preempt_enable(); |
| 1527 | } |
| 1528 | |
| 1529 | static void copy_vmcs12_to_shadow(struct vcpu_vmx *vmx) |
| 1530 | { |
Sean Christopherson | 1c6f0b4 | 2019-05-07 08:36:25 -0700 | [diff] [blame] | 1531 | const struct shadow_vmcs_field *fields[] = { |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1532 | shadow_read_write_fields, |
| 1533 | shadow_read_only_fields |
| 1534 | }; |
| 1535 | const int max_fields[] = { |
| 1536 | max_shadow_read_write_fields, |
| 1537 | max_shadow_read_only_fields |
| 1538 | }; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1539 | struct vmcs *shadow_vmcs = vmx->vmcs01.shadow_vmcs; |
Sean Christopherson | 1c6f0b4 | 2019-05-07 08:36:25 -0700 | [diff] [blame] | 1540 | struct vmcs12 *vmcs12 = get_vmcs12(&vmx->vcpu); |
| 1541 | struct shadow_vmcs_field field; |
| 1542 | unsigned long val; |
| 1543 | int i, q; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1544 | |
Paolo Bonzini | 88dddc1 | 2019-07-19 18:41:10 +0200 | [diff] [blame] | 1545 | if (WARN_ON(!shadow_vmcs)) |
| 1546 | return; |
| 1547 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1548 | vmcs_load(shadow_vmcs); |
| 1549 | |
| 1550 | for (q = 0; q < ARRAY_SIZE(fields); q++) { |
| 1551 | for (i = 0; i < max_fields[q]; i++) { |
| 1552 | field = fields[q][i]; |
Sean Christopherson | 1c6f0b4 | 2019-05-07 08:36:25 -0700 | [diff] [blame] | 1553 | val = vmcs12_read_any(vmcs12, field.encoding, |
| 1554 | field.offset); |
| 1555 | __vmcs_writel(field.encoding, val); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1556 | } |
| 1557 | } |
| 1558 | |
| 1559 | vmcs_clear(shadow_vmcs); |
| 1560 | vmcs_load(vmx->loaded_vmcs->vmcs); |
| 1561 | } |
| 1562 | |
Vitaly Kuznetsov | d6bf71a | 2021-05-26 15:20:22 +0200 | [diff] [blame] | 1563 | 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] | 1564 | { |
| 1565 | struct vmcs12 *vmcs12 = vmx->nested.cached_vmcs12; |
| 1566 | struct hv_enlightened_vmcs *evmcs = vmx->nested.hv_evmcs; |
| 1567 | |
| 1568 | /* HV_VMX_ENLIGHTENED_CLEAN_FIELD_NONE */ |
| 1569 | vmcs12->tpr_threshold = evmcs->tpr_threshold; |
| 1570 | vmcs12->guest_rip = evmcs->guest_rip; |
| 1571 | |
Vitaly Kuznetsov | d6bf71a | 2021-05-26 15:20:22 +0200 | [diff] [blame] | 1572 | if (unlikely(!(hv_clean_fields & |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1573 | HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_BASIC))) { |
| 1574 | vmcs12->guest_rsp = evmcs->guest_rsp; |
| 1575 | vmcs12->guest_rflags = evmcs->guest_rflags; |
| 1576 | vmcs12->guest_interruptibility_info = |
| 1577 | evmcs->guest_interruptibility_info; |
| 1578 | } |
| 1579 | |
Vitaly Kuznetsov | d6bf71a | 2021-05-26 15:20:22 +0200 | [diff] [blame] | 1580 | if (unlikely(!(hv_clean_fields & |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1581 | HV_VMX_ENLIGHTENED_CLEAN_FIELD_CONTROL_PROC))) { |
| 1582 | vmcs12->cpu_based_vm_exec_control = |
| 1583 | evmcs->cpu_based_vm_exec_control; |
| 1584 | } |
| 1585 | |
Vitaly Kuznetsov | d6bf71a | 2021-05-26 15:20:22 +0200 | [diff] [blame] | 1586 | if (unlikely(!(hv_clean_fields & |
Vitaly Kuznetsov | f9bc522 | 2019-06-13 13:35:02 +0200 | [diff] [blame] | 1587 | HV_VMX_ENLIGHTENED_CLEAN_FIELD_CONTROL_EXCPN))) { |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1588 | vmcs12->exception_bitmap = evmcs->exception_bitmap; |
| 1589 | } |
| 1590 | |
Vitaly Kuznetsov | d6bf71a | 2021-05-26 15:20:22 +0200 | [diff] [blame] | 1591 | if (unlikely(!(hv_clean_fields & |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1592 | HV_VMX_ENLIGHTENED_CLEAN_FIELD_CONTROL_ENTRY))) { |
| 1593 | vmcs12->vm_entry_controls = evmcs->vm_entry_controls; |
| 1594 | } |
| 1595 | |
Vitaly Kuznetsov | d6bf71a | 2021-05-26 15:20:22 +0200 | [diff] [blame] | 1596 | if (unlikely(!(hv_clean_fields & |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1597 | HV_VMX_ENLIGHTENED_CLEAN_FIELD_CONTROL_EVENT))) { |
| 1598 | vmcs12->vm_entry_intr_info_field = |
| 1599 | evmcs->vm_entry_intr_info_field; |
| 1600 | vmcs12->vm_entry_exception_error_code = |
| 1601 | evmcs->vm_entry_exception_error_code; |
| 1602 | vmcs12->vm_entry_instruction_len = |
| 1603 | evmcs->vm_entry_instruction_len; |
| 1604 | } |
| 1605 | |
Vitaly Kuznetsov | d6bf71a | 2021-05-26 15:20:22 +0200 | [diff] [blame] | 1606 | if (unlikely(!(hv_clean_fields & |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1607 | HV_VMX_ENLIGHTENED_CLEAN_FIELD_HOST_GRP1))) { |
| 1608 | vmcs12->host_ia32_pat = evmcs->host_ia32_pat; |
| 1609 | vmcs12->host_ia32_efer = evmcs->host_ia32_efer; |
| 1610 | vmcs12->host_cr0 = evmcs->host_cr0; |
| 1611 | vmcs12->host_cr3 = evmcs->host_cr3; |
| 1612 | vmcs12->host_cr4 = evmcs->host_cr4; |
| 1613 | vmcs12->host_ia32_sysenter_esp = evmcs->host_ia32_sysenter_esp; |
| 1614 | vmcs12->host_ia32_sysenter_eip = evmcs->host_ia32_sysenter_eip; |
| 1615 | vmcs12->host_rip = evmcs->host_rip; |
| 1616 | vmcs12->host_ia32_sysenter_cs = evmcs->host_ia32_sysenter_cs; |
| 1617 | vmcs12->host_es_selector = evmcs->host_es_selector; |
| 1618 | vmcs12->host_cs_selector = evmcs->host_cs_selector; |
| 1619 | vmcs12->host_ss_selector = evmcs->host_ss_selector; |
| 1620 | vmcs12->host_ds_selector = evmcs->host_ds_selector; |
| 1621 | vmcs12->host_fs_selector = evmcs->host_fs_selector; |
| 1622 | vmcs12->host_gs_selector = evmcs->host_gs_selector; |
| 1623 | vmcs12->host_tr_selector = evmcs->host_tr_selector; |
| 1624 | } |
| 1625 | |
Vitaly Kuznetsov | d6bf71a | 2021-05-26 15:20:22 +0200 | [diff] [blame] | 1626 | if (unlikely(!(hv_clean_fields & |
Vitaly Kuznetsov | f9bc522 | 2019-06-13 13:35:02 +0200 | [diff] [blame] | 1627 | HV_VMX_ENLIGHTENED_CLEAN_FIELD_CONTROL_GRP1))) { |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1628 | vmcs12->pin_based_vm_exec_control = |
| 1629 | evmcs->pin_based_vm_exec_control; |
| 1630 | vmcs12->vm_exit_controls = evmcs->vm_exit_controls; |
| 1631 | vmcs12->secondary_vm_exec_control = |
| 1632 | evmcs->secondary_vm_exec_control; |
| 1633 | } |
| 1634 | |
Vitaly Kuznetsov | d6bf71a | 2021-05-26 15:20:22 +0200 | [diff] [blame] | 1635 | if (unlikely(!(hv_clean_fields & |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1636 | HV_VMX_ENLIGHTENED_CLEAN_FIELD_IO_BITMAP))) { |
| 1637 | vmcs12->io_bitmap_a = evmcs->io_bitmap_a; |
| 1638 | vmcs12->io_bitmap_b = evmcs->io_bitmap_b; |
| 1639 | } |
| 1640 | |
Vitaly Kuznetsov | d6bf71a | 2021-05-26 15:20:22 +0200 | [diff] [blame] | 1641 | if (unlikely(!(hv_clean_fields & |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1642 | HV_VMX_ENLIGHTENED_CLEAN_FIELD_MSR_BITMAP))) { |
| 1643 | vmcs12->msr_bitmap = evmcs->msr_bitmap; |
| 1644 | } |
| 1645 | |
Vitaly Kuznetsov | d6bf71a | 2021-05-26 15:20:22 +0200 | [diff] [blame] | 1646 | if (unlikely(!(hv_clean_fields & |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1647 | HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP2))) { |
| 1648 | vmcs12->guest_es_base = evmcs->guest_es_base; |
| 1649 | vmcs12->guest_cs_base = evmcs->guest_cs_base; |
| 1650 | vmcs12->guest_ss_base = evmcs->guest_ss_base; |
| 1651 | vmcs12->guest_ds_base = evmcs->guest_ds_base; |
| 1652 | vmcs12->guest_fs_base = evmcs->guest_fs_base; |
| 1653 | vmcs12->guest_gs_base = evmcs->guest_gs_base; |
| 1654 | vmcs12->guest_ldtr_base = evmcs->guest_ldtr_base; |
| 1655 | vmcs12->guest_tr_base = evmcs->guest_tr_base; |
| 1656 | vmcs12->guest_gdtr_base = evmcs->guest_gdtr_base; |
| 1657 | vmcs12->guest_idtr_base = evmcs->guest_idtr_base; |
| 1658 | vmcs12->guest_es_limit = evmcs->guest_es_limit; |
| 1659 | vmcs12->guest_cs_limit = evmcs->guest_cs_limit; |
| 1660 | vmcs12->guest_ss_limit = evmcs->guest_ss_limit; |
| 1661 | vmcs12->guest_ds_limit = evmcs->guest_ds_limit; |
| 1662 | vmcs12->guest_fs_limit = evmcs->guest_fs_limit; |
| 1663 | vmcs12->guest_gs_limit = evmcs->guest_gs_limit; |
| 1664 | vmcs12->guest_ldtr_limit = evmcs->guest_ldtr_limit; |
| 1665 | vmcs12->guest_tr_limit = evmcs->guest_tr_limit; |
| 1666 | vmcs12->guest_gdtr_limit = evmcs->guest_gdtr_limit; |
| 1667 | vmcs12->guest_idtr_limit = evmcs->guest_idtr_limit; |
| 1668 | vmcs12->guest_es_ar_bytes = evmcs->guest_es_ar_bytes; |
| 1669 | vmcs12->guest_cs_ar_bytes = evmcs->guest_cs_ar_bytes; |
| 1670 | vmcs12->guest_ss_ar_bytes = evmcs->guest_ss_ar_bytes; |
| 1671 | vmcs12->guest_ds_ar_bytes = evmcs->guest_ds_ar_bytes; |
| 1672 | vmcs12->guest_fs_ar_bytes = evmcs->guest_fs_ar_bytes; |
| 1673 | vmcs12->guest_gs_ar_bytes = evmcs->guest_gs_ar_bytes; |
| 1674 | vmcs12->guest_ldtr_ar_bytes = evmcs->guest_ldtr_ar_bytes; |
| 1675 | vmcs12->guest_tr_ar_bytes = evmcs->guest_tr_ar_bytes; |
| 1676 | vmcs12->guest_es_selector = evmcs->guest_es_selector; |
| 1677 | vmcs12->guest_cs_selector = evmcs->guest_cs_selector; |
| 1678 | vmcs12->guest_ss_selector = evmcs->guest_ss_selector; |
| 1679 | vmcs12->guest_ds_selector = evmcs->guest_ds_selector; |
| 1680 | vmcs12->guest_fs_selector = evmcs->guest_fs_selector; |
| 1681 | vmcs12->guest_gs_selector = evmcs->guest_gs_selector; |
| 1682 | vmcs12->guest_ldtr_selector = evmcs->guest_ldtr_selector; |
| 1683 | vmcs12->guest_tr_selector = evmcs->guest_tr_selector; |
| 1684 | } |
| 1685 | |
Vitaly Kuznetsov | d6bf71a | 2021-05-26 15:20:22 +0200 | [diff] [blame] | 1686 | if (unlikely(!(hv_clean_fields & |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1687 | HV_VMX_ENLIGHTENED_CLEAN_FIELD_CONTROL_GRP2))) { |
| 1688 | vmcs12->tsc_offset = evmcs->tsc_offset; |
| 1689 | vmcs12->virtual_apic_page_addr = evmcs->virtual_apic_page_addr; |
| 1690 | vmcs12->xss_exit_bitmap = evmcs->xss_exit_bitmap; |
| 1691 | } |
| 1692 | |
Vitaly Kuznetsov | d6bf71a | 2021-05-26 15:20:22 +0200 | [diff] [blame] | 1693 | if (unlikely(!(hv_clean_fields & |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1694 | HV_VMX_ENLIGHTENED_CLEAN_FIELD_CRDR))) { |
| 1695 | vmcs12->cr0_guest_host_mask = evmcs->cr0_guest_host_mask; |
| 1696 | vmcs12->cr4_guest_host_mask = evmcs->cr4_guest_host_mask; |
| 1697 | vmcs12->cr0_read_shadow = evmcs->cr0_read_shadow; |
| 1698 | vmcs12->cr4_read_shadow = evmcs->cr4_read_shadow; |
| 1699 | vmcs12->guest_cr0 = evmcs->guest_cr0; |
| 1700 | vmcs12->guest_cr3 = evmcs->guest_cr3; |
| 1701 | vmcs12->guest_cr4 = evmcs->guest_cr4; |
| 1702 | vmcs12->guest_dr7 = evmcs->guest_dr7; |
| 1703 | } |
| 1704 | |
Vitaly Kuznetsov | d6bf71a | 2021-05-26 15:20:22 +0200 | [diff] [blame] | 1705 | if (unlikely(!(hv_clean_fields & |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1706 | HV_VMX_ENLIGHTENED_CLEAN_FIELD_HOST_POINTER))) { |
| 1707 | vmcs12->host_fs_base = evmcs->host_fs_base; |
| 1708 | vmcs12->host_gs_base = evmcs->host_gs_base; |
| 1709 | vmcs12->host_tr_base = evmcs->host_tr_base; |
| 1710 | vmcs12->host_gdtr_base = evmcs->host_gdtr_base; |
| 1711 | vmcs12->host_idtr_base = evmcs->host_idtr_base; |
| 1712 | vmcs12->host_rsp = evmcs->host_rsp; |
| 1713 | } |
| 1714 | |
Vitaly Kuznetsov | d6bf71a | 2021-05-26 15:20:22 +0200 | [diff] [blame] | 1715 | if (unlikely(!(hv_clean_fields & |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1716 | HV_VMX_ENLIGHTENED_CLEAN_FIELD_CONTROL_XLAT))) { |
| 1717 | vmcs12->ept_pointer = evmcs->ept_pointer; |
| 1718 | vmcs12->virtual_processor_id = evmcs->virtual_processor_id; |
| 1719 | } |
| 1720 | |
Vitaly Kuznetsov | d6bf71a | 2021-05-26 15:20:22 +0200 | [diff] [blame] | 1721 | if (unlikely(!(hv_clean_fields & |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1722 | HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP1))) { |
| 1723 | vmcs12->vmcs_link_pointer = evmcs->vmcs_link_pointer; |
| 1724 | vmcs12->guest_ia32_debugctl = evmcs->guest_ia32_debugctl; |
| 1725 | vmcs12->guest_ia32_pat = evmcs->guest_ia32_pat; |
| 1726 | vmcs12->guest_ia32_efer = evmcs->guest_ia32_efer; |
| 1727 | vmcs12->guest_pdptr0 = evmcs->guest_pdptr0; |
| 1728 | vmcs12->guest_pdptr1 = evmcs->guest_pdptr1; |
| 1729 | vmcs12->guest_pdptr2 = evmcs->guest_pdptr2; |
| 1730 | vmcs12->guest_pdptr3 = evmcs->guest_pdptr3; |
| 1731 | vmcs12->guest_pending_dbg_exceptions = |
| 1732 | evmcs->guest_pending_dbg_exceptions; |
| 1733 | vmcs12->guest_sysenter_esp = evmcs->guest_sysenter_esp; |
| 1734 | vmcs12->guest_sysenter_eip = evmcs->guest_sysenter_eip; |
| 1735 | vmcs12->guest_bndcfgs = evmcs->guest_bndcfgs; |
| 1736 | vmcs12->guest_activity_state = evmcs->guest_activity_state; |
| 1737 | vmcs12->guest_sysenter_cs = evmcs->guest_sysenter_cs; |
| 1738 | } |
| 1739 | |
| 1740 | /* |
| 1741 | * Not used? |
| 1742 | * vmcs12->vm_exit_msr_store_addr = evmcs->vm_exit_msr_store_addr; |
| 1743 | * vmcs12->vm_exit_msr_load_addr = evmcs->vm_exit_msr_load_addr; |
| 1744 | * vmcs12->vm_entry_msr_load_addr = evmcs->vm_entry_msr_load_addr; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1745 | * vmcs12->page_fault_error_code_mask = |
| 1746 | * evmcs->page_fault_error_code_mask; |
| 1747 | * vmcs12->page_fault_error_code_match = |
| 1748 | * evmcs->page_fault_error_code_match; |
| 1749 | * vmcs12->cr3_target_count = evmcs->cr3_target_count; |
| 1750 | * vmcs12->vm_exit_msr_store_count = evmcs->vm_exit_msr_store_count; |
| 1751 | * vmcs12->vm_exit_msr_load_count = evmcs->vm_exit_msr_load_count; |
| 1752 | * vmcs12->vm_entry_msr_load_count = evmcs->vm_entry_msr_load_count; |
| 1753 | */ |
| 1754 | |
| 1755 | /* |
| 1756 | * Read only fields: |
| 1757 | * vmcs12->guest_physical_address = evmcs->guest_physical_address; |
| 1758 | * vmcs12->vm_instruction_error = evmcs->vm_instruction_error; |
| 1759 | * vmcs12->vm_exit_reason = evmcs->vm_exit_reason; |
| 1760 | * vmcs12->vm_exit_intr_info = evmcs->vm_exit_intr_info; |
| 1761 | * vmcs12->vm_exit_intr_error_code = evmcs->vm_exit_intr_error_code; |
| 1762 | * vmcs12->idt_vectoring_info_field = evmcs->idt_vectoring_info_field; |
| 1763 | * vmcs12->idt_vectoring_error_code = evmcs->idt_vectoring_error_code; |
| 1764 | * vmcs12->vm_exit_instruction_len = evmcs->vm_exit_instruction_len; |
| 1765 | * vmcs12->vmx_instruction_info = evmcs->vmx_instruction_info; |
| 1766 | * vmcs12->exit_qualification = evmcs->exit_qualification; |
| 1767 | * vmcs12->guest_linear_address = evmcs->guest_linear_address; |
| 1768 | * |
| 1769 | * Not present in struct vmcs12: |
| 1770 | * vmcs12->exit_io_instruction_ecx = evmcs->exit_io_instruction_ecx; |
| 1771 | * vmcs12->exit_io_instruction_esi = evmcs->exit_io_instruction_esi; |
| 1772 | * vmcs12->exit_io_instruction_edi = evmcs->exit_io_instruction_edi; |
| 1773 | * vmcs12->exit_io_instruction_eip = evmcs->exit_io_instruction_eip; |
| 1774 | */ |
| 1775 | |
Vitaly Kuznetsov | 25641ca | 2021-05-26 15:20:19 +0200 | [diff] [blame] | 1776 | return; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1777 | } |
| 1778 | |
Vitaly Kuznetsov | 25641ca | 2021-05-26 15:20:19 +0200 | [diff] [blame] | 1779 | static void copy_vmcs12_to_enlightened(struct vcpu_vmx *vmx) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1780 | { |
| 1781 | struct vmcs12 *vmcs12 = vmx->nested.cached_vmcs12; |
| 1782 | struct hv_enlightened_vmcs *evmcs = vmx->nested.hv_evmcs; |
| 1783 | |
| 1784 | /* |
| 1785 | * Should not be changed by KVM: |
| 1786 | * |
| 1787 | * evmcs->host_es_selector = vmcs12->host_es_selector; |
| 1788 | * evmcs->host_cs_selector = vmcs12->host_cs_selector; |
| 1789 | * evmcs->host_ss_selector = vmcs12->host_ss_selector; |
| 1790 | * evmcs->host_ds_selector = vmcs12->host_ds_selector; |
| 1791 | * evmcs->host_fs_selector = vmcs12->host_fs_selector; |
| 1792 | * evmcs->host_gs_selector = vmcs12->host_gs_selector; |
| 1793 | * evmcs->host_tr_selector = vmcs12->host_tr_selector; |
| 1794 | * evmcs->host_ia32_pat = vmcs12->host_ia32_pat; |
| 1795 | * evmcs->host_ia32_efer = vmcs12->host_ia32_efer; |
| 1796 | * evmcs->host_cr0 = vmcs12->host_cr0; |
| 1797 | * evmcs->host_cr3 = vmcs12->host_cr3; |
| 1798 | * evmcs->host_cr4 = vmcs12->host_cr4; |
| 1799 | * evmcs->host_ia32_sysenter_esp = vmcs12->host_ia32_sysenter_esp; |
| 1800 | * evmcs->host_ia32_sysenter_eip = vmcs12->host_ia32_sysenter_eip; |
| 1801 | * evmcs->host_rip = vmcs12->host_rip; |
| 1802 | * evmcs->host_ia32_sysenter_cs = vmcs12->host_ia32_sysenter_cs; |
| 1803 | * evmcs->host_fs_base = vmcs12->host_fs_base; |
| 1804 | * evmcs->host_gs_base = vmcs12->host_gs_base; |
| 1805 | * evmcs->host_tr_base = vmcs12->host_tr_base; |
| 1806 | * evmcs->host_gdtr_base = vmcs12->host_gdtr_base; |
| 1807 | * evmcs->host_idtr_base = vmcs12->host_idtr_base; |
| 1808 | * evmcs->host_rsp = vmcs12->host_rsp; |
Sean Christopherson | 3731905ef | 2019-05-07 08:36:27 -0700 | [diff] [blame] | 1809 | * sync_vmcs02_to_vmcs12() doesn't read these: |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1810 | * evmcs->io_bitmap_a = vmcs12->io_bitmap_a; |
| 1811 | * evmcs->io_bitmap_b = vmcs12->io_bitmap_b; |
| 1812 | * evmcs->msr_bitmap = vmcs12->msr_bitmap; |
| 1813 | * evmcs->ept_pointer = vmcs12->ept_pointer; |
| 1814 | * evmcs->xss_exit_bitmap = vmcs12->xss_exit_bitmap; |
| 1815 | * evmcs->vm_exit_msr_store_addr = vmcs12->vm_exit_msr_store_addr; |
| 1816 | * evmcs->vm_exit_msr_load_addr = vmcs12->vm_exit_msr_load_addr; |
| 1817 | * evmcs->vm_entry_msr_load_addr = vmcs12->vm_entry_msr_load_addr; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1818 | * evmcs->tpr_threshold = vmcs12->tpr_threshold; |
| 1819 | * evmcs->virtual_processor_id = vmcs12->virtual_processor_id; |
| 1820 | * evmcs->exception_bitmap = vmcs12->exception_bitmap; |
| 1821 | * evmcs->vmcs_link_pointer = vmcs12->vmcs_link_pointer; |
| 1822 | * evmcs->pin_based_vm_exec_control = vmcs12->pin_based_vm_exec_control; |
| 1823 | * evmcs->vm_exit_controls = vmcs12->vm_exit_controls; |
| 1824 | * evmcs->secondary_vm_exec_control = vmcs12->secondary_vm_exec_control; |
| 1825 | * evmcs->page_fault_error_code_mask = |
| 1826 | * vmcs12->page_fault_error_code_mask; |
| 1827 | * evmcs->page_fault_error_code_match = |
| 1828 | * vmcs12->page_fault_error_code_match; |
| 1829 | * evmcs->cr3_target_count = vmcs12->cr3_target_count; |
| 1830 | * evmcs->virtual_apic_page_addr = vmcs12->virtual_apic_page_addr; |
| 1831 | * evmcs->tsc_offset = vmcs12->tsc_offset; |
| 1832 | * evmcs->guest_ia32_debugctl = vmcs12->guest_ia32_debugctl; |
| 1833 | * evmcs->cr0_guest_host_mask = vmcs12->cr0_guest_host_mask; |
| 1834 | * evmcs->cr4_guest_host_mask = vmcs12->cr4_guest_host_mask; |
| 1835 | * evmcs->cr0_read_shadow = vmcs12->cr0_read_shadow; |
| 1836 | * evmcs->cr4_read_shadow = vmcs12->cr4_read_shadow; |
| 1837 | * evmcs->vm_exit_msr_store_count = vmcs12->vm_exit_msr_store_count; |
| 1838 | * evmcs->vm_exit_msr_load_count = vmcs12->vm_exit_msr_load_count; |
| 1839 | * evmcs->vm_entry_msr_load_count = vmcs12->vm_entry_msr_load_count; |
| 1840 | * |
| 1841 | * Not present in struct vmcs12: |
| 1842 | * evmcs->exit_io_instruction_ecx = vmcs12->exit_io_instruction_ecx; |
| 1843 | * evmcs->exit_io_instruction_esi = vmcs12->exit_io_instruction_esi; |
| 1844 | * evmcs->exit_io_instruction_edi = vmcs12->exit_io_instruction_edi; |
| 1845 | * evmcs->exit_io_instruction_eip = vmcs12->exit_io_instruction_eip; |
| 1846 | */ |
| 1847 | |
| 1848 | evmcs->guest_es_selector = vmcs12->guest_es_selector; |
| 1849 | evmcs->guest_cs_selector = vmcs12->guest_cs_selector; |
| 1850 | evmcs->guest_ss_selector = vmcs12->guest_ss_selector; |
| 1851 | evmcs->guest_ds_selector = vmcs12->guest_ds_selector; |
| 1852 | evmcs->guest_fs_selector = vmcs12->guest_fs_selector; |
| 1853 | evmcs->guest_gs_selector = vmcs12->guest_gs_selector; |
| 1854 | evmcs->guest_ldtr_selector = vmcs12->guest_ldtr_selector; |
| 1855 | evmcs->guest_tr_selector = vmcs12->guest_tr_selector; |
| 1856 | |
| 1857 | evmcs->guest_es_limit = vmcs12->guest_es_limit; |
| 1858 | evmcs->guest_cs_limit = vmcs12->guest_cs_limit; |
| 1859 | evmcs->guest_ss_limit = vmcs12->guest_ss_limit; |
| 1860 | evmcs->guest_ds_limit = vmcs12->guest_ds_limit; |
| 1861 | evmcs->guest_fs_limit = vmcs12->guest_fs_limit; |
| 1862 | evmcs->guest_gs_limit = vmcs12->guest_gs_limit; |
| 1863 | evmcs->guest_ldtr_limit = vmcs12->guest_ldtr_limit; |
| 1864 | evmcs->guest_tr_limit = vmcs12->guest_tr_limit; |
| 1865 | evmcs->guest_gdtr_limit = vmcs12->guest_gdtr_limit; |
| 1866 | evmcs->guest_idtr_limit = vmcs12->guest_idtr_limit; |
| 1867 | |
| 1868 | evmcs->guest_es_ar_bytes = vmcs12->guest_es_ar_bytes; |
| 1869 | evmcs->guest_cs_ar_bytes = vmcs12->guest_cs_ar_bytes; |
| 1870 | evmcs->guest_ss_ar_bytes = vmcs12->guest_ss_ar_bytes; |
| 1871 | evmcs->guest_ds_ar_bytes = vmcs12->guest_ds_ar_bytes; |
| 1872 | evmcs->guest_fs_ar_bytes = vmcs12->guest_fs_ar_bytes; |
| 1873 | evmcs->guest_gs_ar_bytes = vmcs12->guest_gs_ar_bytes; |
| 1874 | evmcs->guest_ldtr_ar_bytes = vmcs12->guest_ldtr_ar_bytes; |
| 1875 | evmcs->guest_tr_ar_bytes = vmcs12->guest_tr_ar_bytes; |
| 1876 | |
| 1877 | evmcs->guest_es_base = vmcs12->guest_es_base; |
| 1878 | evmcs->guest_cs_base = vmcs12->guest_cs_base; |
| 1879 | evmcs->guest_ss_base = vmcs12->guest_ss_base; |
| 1880 | evmcs->guest_ds_base = vmcs12->guest_ds_base; |
| 1881 | evmcs->guest_fs_base = vmcs12->guest_fs_base; |
| 1882 | evmcs->guest_gs_base = vmcs12->guest_gs_base; |
| 1883 | evmcs->guest_ldtr_base = vmcs12->guest_ldtr_base; |
| 1884 | evmcs->guest_tr_base = vmcs12->guest_tr_base; |
| 1885 | evmcs->guest_gdtr_base = vmcs12->guest_gdtr_base; |
| 1886 | evmcs->guest_idtr_base = vmcs12->guest_idtr_base; |
| 1887 | |
| 1888 | evmcs->guest_ia32_pat = vmcs12->guest_ia32_pat; |
| 1889 | evmcs->guest_ia32_efer = vmcs12->guest_ia32_efer; |
| 1890 | |
| 1891 | evmcs->guest_pdptr0 = vmcs12->guest_pdptr0; |
| 1892 | evmcs->guest_pdptr1 = vmcs12->guest_pdptr1; |
| 1893 | evmcs->guest_pdptr2 = vmcs12->guest_pdptr2; |
| 1894 | evmcs->guest_pdptr3 = vmcs12->guest_pdptr3; |
| 1895 | |
| 1896 | evmcs->guest_pending_dbg_exceptions = |
| 1897 | vmcs12->guest_pending_dbg_exceptions; |
| 1898 | evmcs->guest_sysenter_esp = vmcs12->guest_sysenter_esp; |
| 1899 | evmcs->guest_sysenter_eip = vmcs12->guest_sysenter_eip; |
| 1900 | |
| 1901 | evmcs->guest_activity_state = vmcs12->guest_activity_state; |
| 1902 | evmcs->guest_sysenter_cs = vmcs12->guest_sysenter_cs; |
| 1903 | |
| 1904 | evmcs->guest_cr0 = vmcs12->guest_cr0; |
| 1905 | evmcs->guest_cr3 = vmcs12->guest_cr3; |
| 1906 | evmcs->guest_cr4 = vmcs12->guest_cr4; |
| 1907 | evmcs->guest_dr7 = vmcs12->guest_dr7; |
| 1908 | |
| 1909 | evmcs->guest_physical_address = vmcs12->guest_physical_address; |
| 1910 | |
| 1911 | evmcs->vm_instruction_error = vmcs12->vm_instruction_error; |
| 1912 | evmcs->vm_exit_reason = vmcs12->vm_exit_reason; |
| 1913 | evmcs->vm_exit_intr_info = vmcs12->vm_exit_intr_info; |
| 1914 | evmcs->vm_exit_intr_error_code = vmcs12->vm_exit_intr_error_code; |
| 1915 | evmcs->idt_vectoring_info_field = vmcs12->idt_vectoring_info_field; |
| 1916 | evmcs->idt_vectoring_error_code = vmcs12->idt_vectoring_error_code; |
| 1917 | evmcs->vm_exit_instruction_len = vmcs12->vm_exit_instruction_len; |
| 1918 | evmcs->vmx_instruction_info = vmcs12->vmx_instruction_info; |
| 1919 | |
| 1920 | evmcs->exit_qualification = vmcs12->exit_qualification; |
| 1921 | |
| 1922 | evmcs->guest_linear_address = vmcs12->guest_linear_address; |
| 1923 | evmcs->guest_rsp = vmcs12->guest_rsp; |
| 1924 | evmcs->guest_rflags = vmcs12->guest_rflags; |
| 1925 | |
| 1926 | evmcs->guest_interruptibility_info = |
| 1927 | vmcs12->guest_interruptibility_info; |
| 1928 | evmcs->cpu_based_vm_exec_control = vmcs12->cpu_based_vm_exec_control; |
| 1929 | evmcs->vm_entry_controls = vmcs12->vm_entry_controls; |
| 1930 | evmcs->vm_entry_intr_info_field = vmcs12->vm_entry_intr_info_field; |
| 1931 | evmcs->vm_entry_exception_error_code = |
| 1932 | vmcs12->vm_entry_exception_error_code; |
| 1933 | evmcs->vm_entry_instruction_len = vmcs12->vm_entry_instruction_len; |
| 1934 | |
| 1935 | evmcs->guest_rip = vmcs12->guest_rip; |
| 1936 | |
| 1937 | evmcs->guest_bndcfgs = vmcs12->guest_bndcfgs; |
| 1938 | |
Vitaly Kuznetsov | 25641ca | 2021-05-26 15:20:19 +0200 | [diff] [blame] | 1939 | return; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1940 | } |
| 1941 | |
| 1942 | /* |
| 1943 | * This is an equivalent of the nested hypervisor executing the vmptrld |
| 1944 | * instruction. |
| 1945 | */ |
Vitaly Kuznetsov | b6a0653 | 2020-03-09 16:52:13 +0100 | [diff] [blame] | 1946 | static enum nested_evmptrld_status nested_vmx_handle_enlightened_vmptrld( |
| 1947 | struct kvm_vcpu *vcpu, bool from_launch) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1948 | { |
| 1949 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Vitaly Kuznetsov | a21a39c | 2019-06-28 13:23:32 +0200 | [diff] [blame] | 1950 | bool evmcs_gpa_changed = false; |
Vitaly Kuznetsov | 11e3491 | 2019-06-28 13:23:33 +0200 | [diff] [blame] | 1951 | u64 evmcs_gpa; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1952 | |
| 1953 | if (likely(!vmx->nested.enlightened_vmcs_enabled)) |
Vitaly Kuznetsov | b6a0653 | 2020-03-09 16:52:13 +0100 | [diff] [blame] | 1954 | return EVMPTRLD_DISABLED; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1955 | |
Vitaly Kuznetsov | 0276171 | 2021-05-26 15:20:18 +0200 | [diff] [blame] | 1956 | if (!nested_enlightened_vmentry(vcpu, &evmcs_gpa)) { |
| 1957 | nested_release_evmcs(vcpu); |
Vitaly Kuznetsov | b6a0653 | 2020-03-09 16:52:13 +0100 | [diff] [blame] | 1958 | return EVMPTRLD_DISABLED; |
Vitaly Kuznetsov | 0276171 | 2021-05-26 15:20:18 +0200 | [diff] [blame] | 1959 | } |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1960 | |
Vitaly Kuznetsov | 1e9dfbd | 2021-05-26 15:20:16 +0200 | [diff] [blame] | 1961 | if (unlikely(evmcs_gpa != vmx->nested.hv_evmcs_vmptr)) { |
Yu Zhang | 64c7850 | 2021-09-30 01:51:53 +0800 | [diff] [blame] | 1962 | vmx->nested.current_vmptr = INVALID_GPA; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1963 | |
| 1964 | nested_release_evmcs(vcpu); |
| 1965 | |
Vitaly Kuznetsov | 11e3491 | 2019-06-28 13:23:33 +0200 | [diff] [blame] | 1966 | if (kvm_vcpu_map(vcpu, gpa_to_gfn(evmcs_gpa), |
KarimAllah Ahmed | dee9c04 | 2019-01-31 21:24:42 +0100 | [diff] [blame] | 1967 | &vmx->nested.hv_evmcs_map)) |
Vitaly Kuznetsov | b6a0653 | 2020-03-09 16:52:13 +0100 | [diff] [blame] | 1968 | return EVMPTRLD_ERROR; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1969 | |
KarimAllah Ahmed | dee9c04 | 2019-01-31 21:24:42 +0100 | [diff] [blame] | 1970 | vmx->nested.hv_evmcs = vmx->nested.hv_evmcs_map.hva; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1971 | |
| 1972 | /* |
| 1973 | * Currently, KVM only supports eVMCS version 1 |
| 1974 | * (== KVM_EVMCS_VERSION) and thus we expect guest to set this |
| 1975 | * value to first u32 field of eVMCS which should specify eVMCS |
| 1976 | * VersionNumber. |
| 1977 | * |
| 1978 | * Guest should be aware of supported eVMCS versions by host by |
| 1979 | * examining CPUID.0x4000000A.EAX[0:15]. Host userspace VMM is |
| 1980 | * expected to set this CPUID leaf according to the value |
| 1981 | * returned in vmcs_version from nested_enable_evmcs(). |
| 1982 | * |
| 1983 | * However, it turns out that Microsoft Hyper-V fails to comply |
| 1984 | * to their own invented interface: When Hyper-V use eVMCS, it |
| 1985 | * just sets first u32 field of eVMCS to revision_id specified |
| 1986 | * in MSR_IA32_VMX_BASIC. Instead of used eVMCS version number |
| 1987 | * which is one of the supported versions specified in |
| 1988 | * CPUID.0x4000000A.EAX[0:15]. |
| 1989 | * |
| 1990 | * To overcome Hyper-V bug, we accept here either a supported |
| 1991 | * eVMCS version or VMCS12 revision_id as valid values for first |
| 1992 | * u32 field of eVMCS. |
| 1993 | */ |
| 1994 | if ((vmx->nested.hv_evmcs->revision_id != KVM_EVMCS_VERSION) && |
| 1995 | (vmx->nested.hv_evmcs->revision_id != VMCS12_REVISION)) { |
| 1996 | nested_release_evmcs(vcpu); |
Vitaly Kuznetsov | b6a0653 | 2020-03-09 16:52:13 +0100 | [diff] [blame] | 1997 | return EVMPTRLD_VMFAIL; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1998 | } |
| 1999 | |
Vitaly Kuznetsov | 11e3491 | 2019-06-28 13:23:33 +0200 | [diff] [blame] | 2000 | vmx->nested.hv_evmcs_vmptr = evmcs_gpa; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2001 | |
Vitaly Kuznetsov | a21a39c | 2019-06-28 13:23:32 +0200 | [diff] [blame] | 2002 | evmcs_gpa_changed = true; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2003 | /* |
| 2004 | * Unlike normal vmcs12, enlightened vmcs12 is not fully |
| 2005 | * reloaded from guest's memory (read only fields, fields not |
| 2006 | * present in struct hv_enlightened_vmcs, ...). Make sure there |
| 2007 | * are no leftovers. |
| 2008 | */ |
| 2009 | if (from_launch) { |
| 2010 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
| 2011 | memset(vmcs12, 0, sizeof(*vmcs12)); |
| 2012 | vmcs12->hdr.revision_id = VMCS12_REVISION; |
| 2013 | } |
| 2014 | |
| 2015 | } |
Vitaly Kuznetsov | a21a39c | 2019-06-28 13:23:32 +0200 | [diff] [blame] | 2016 | |
| 2017 | /* |
Miaohe Lin | ffdbd50 | 2020-02-07 23:22:45 +0800 | [diff] [blame] | 2018 | * 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] | 2019 | * between different L2 guests as KVM keeps a single VMCS12 per L1. |
| 2020 | */ |
| 2021 | if (from_launch || evmcs_gpa_changed) |
| 2022 | vmx->nested.hv_evmcs->hv_clean_fields &= |
| 2023 | ~HV_VMX_ENLIGHTENED_CLEAN_FIELD_ALL; |
| 2024 | |
Vitaly Kuznetsov | b6a0653 | 2020-03-09 16:52:13 +0100 | [diff] [blame] | 2025 | return EVMPTRLD_SUCCEEDED; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2026 | } |
| 2027 | |
Sean Christopherson | 3731905ef | 2019-05-07 08:36:27 -0700 | [diff] [blame] | 2028 | void nested_sync_vmcs12_to_shadow(struct kvm_vcpu *vcpu) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2029 | { |
| 2030 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 2031 | |
Vitaly Kuznetsov | dc31338 | 2021-05-26 15:20:24 +0200 | [diff] [blame] | 2032 | if (evmptr_is_valid(vmx->nested.hv_evmcs_vmptr)) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2033 | copy_vmcs12_to_enlightened(vmx); |
Vitaly Kuznetsov | dc31338 | 2021-05-26 15:20:24 +0200 | [diff] [blame] | 2034 | else |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2035 | copy_vmcs12_to_shadow(vmx); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2036 | |
Sean Christopherson | 3731905ef | 2019-05-07 08:36:27 -0700 | [diff] [blame] | 2037 | vmx->nested.need_vmcs12_to_shadow_sync = false; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2038 | } |
| 2039 | |
| 2040 | static enum hrtimer_restart vmx_preemption_timer_fn(struct hrtimer *timer) |
| 2041 | { |
| 2042 | struct vcpu_vmx *vmx = |
| 2043 | container_of(timer, struct vcpu_vmx, nested.preemption_timer); |
| 2044 | |
| 2045 | vmx->nested.preemption_timer_expired = true; |
| 2046 | kvm_make_request(KVM_REQ_EVENT, &vmx->vcpu); |
| 2047 | kvm_vcpu_kick(&vmx->vcpu); |
| 2048 | |
| 2049 | return HRTIMER_NORESTART; |
| 2050 | } |
| 2051 | |
Peter Shier | 850448f | 2020-05-26 14:51:06 -0700 | [diff] [blame] | 2052 | static u64 vmx_calc_preemption_timer_value(struct kvm_vcpu *vcpu) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2053 | { |
Peter Shier | 850448f | 2020-05-26 14:51:06 -0700 | [diff] [blame] | 2054 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 2055 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
Peter Shier | 850448f | 2020-05-26 14:51:06 -0700 | [diff] [blame] | 2056 | |
| 2057 | u64 l1_scaled_tsc = kvm_read_l1_tsc(vcpu, rdtsc()) >> |
| 2058 | VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE; |
| 2059 | |
| 2060 | if (!vmx->nested.has_preemption_timer_deadline) { |
Makarand Sonare | 8d7fbf0 | 2020-05-26 14:51:07 -0700 | [diff] [blame] | 2061 | vmx->nested.preemption_timer_deadline = |
| 2062 | vmcs12->vmx_preemption_timer_value + l1_scaled_tsc; |
Peter Shier | 850448f | 2020-05-26 14:51:06 -0700 | [diff] [blame] | 2063 | vmx->nested.has_preemption_timer_deadline = true; |
Makarand Sonare | 8d7fbf0 | 2020-05-26 14:51:07 -0700 | [diff] [blame] | 2064 | } |
| 2065 | return vmx->nested.preemption_timer_deadline - l1_scaled_tsc; |
Peter Shier | 850448f | 2020-05-26 14:51:06 -0700 | [diff] [blame] | 2066 | } |
| 2067 | |
| 2068 | static void vmx_start_preemption_timer(struct kvm_vcpu *vcpu, |
| 2069 | u64 preemption_timeout) |
| 2070 | { |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2071 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 2072 | |
| 2073 | /* |
| 2074 | * A timer value of zero is architecturally guaranteed to cause |
| 2075 | * a VMExit prior to executing any instructions in the guest. |
| 2076 | */ |
| 2077 | if (preemption_timeout == 0) { |
| 2078 | vmx_preemption_timer_fn(&vmx->nested.preemption_timer); |
| 2079 | return; |
| 2080 | } |
| 2081 | |
| 2082 | if (vcpu->arch.virtual_tsc_khz == 0) |
| 2083 | return; |
| 2084 | |
| 2085 | preemption_timeout <<= VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE; |
| 2086 | preemption_timeout *= 1000000; |
| 2087 | do_div(preemption_timeout, vcpu->arch.virtual_tsc_khz); |
| 2088 | hrtimer_start(&vmx->nested.preemption_timer, |
Jim Mattson | ada0098 | 2020-05-08 13:36:42 -0700 | [diff] [blame] | 2089 | ktime_add_ns(ktime_get(), preemption_timeout), |
| 2090 | HRTIMER_MODE_ABS_PINNED); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2091 | } |
| 2092 | |
| 2093 | static u64 nested_vmx_calc_efer(struct vcpu_vmx *vmx, struct vmcs12 *vmcs12) |
| 2094 | { |
| 2095 | if (vmx->nested.nested_run_pending && |
| 2096 | (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_EFER)) |
| 2097 | return vmcs12->guest_ia32_efer; |
| 2098 | else if (vmcs12->vm_entry_controls & VM_ENTRY_IA32E_MODE) |
| 2099 | return vmx->vcpu.arch.efer | (EFER_LMA | EFER_LME); |
| 2100 | else |
| 2101 | return vmx->vcpu.arch.efer & ~(EFER_LMA | EFER_LME); |
| 2102 | } |
| 2103 | |
| 2104 | static void prepare_vmcs02_constant_state(struct vcpu_vmx *vmx) |
| 2105 | { |
| 2106 | /* |
| 2107 | * If vmcs02 hasn't been initialized, set the constant vmcs02 state |
| 2108 | * according to L0's settings (vmcs12 is irrelevant here). Host |
| 2109 | * fields that come from L0 and are not constant, e.g. HOST_CR3, |
| 2110 | * will be set as needed prior to VMLAUNCH/VMRESUME. |
| 2111 | */ |
| 2112 | if (vmx->nested.vmcs02_initialized) |
| 2113 | return; |
| 2114 | vmx->nested.vmcs02_initialized = true; |
| 2115 | |
| 2116 | /* |
| 2117 | * We don't care what the EPTP value is we just need to guarantee |
| 2118 | * it's valid so we don't get a false positive when doing early |
| 2119 | * consistency checks. |
| 2120 | */ |
| 2121 | if (enable_ept && nested_early_check) |
Sean Christopherson | 2a40b90 | 2020-07-15 20:41:18 -0700 | [diff] [blame] | 2122 | vmcs_write64(EPT_POINTER, |
| 2123 | construct_eptp(&vmx->vcpu, 0, PT64_ROOT_4LEVEL)); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2124 | |
| 2125 | /* All VMFUNCs are currently emulated through L0 vmexits. */ |
| 2126 | if (cpu_has_vmx_vmfunc()) |
| 2127 | vmcs_write64(VM_FUNCTION_CONTROL, 0); |
| 2128 | |
| 2129 | if (cpu_has_vmx_posted_intr()) |
| 2130 | vmcs_write16(POSTED_INTR_NV, POSTED_INTR_NESTED_VECTOR); |
| 2131 | |
| 2132 | if (cpu_has_vmx_msr_bitmap()) |
| 2133 | vmcs_write64(MSR_BITMAP, __pa(vmx->nested.vmcs02.msr_bitmap)); |
| 2134 | |
Sean Christopherson | 4d6c989 | 2019-05-07 09:06:30 -0700 | [diff] [blame] | 2135 | /* |
Sean Christopherson | c3bb9a2 | 2021-02-12 16:50:07 -0800 | [diff] [blame] | 2136 | * PML is emulated for L2, but never enabled in hardware as the MMU |
| 2137 | * handles A/D emulation. Disabling PML for L2 also avoids having to |
| 2138 | * deal with filtering out L2 GPAs from the buffer. |
Sean Christopherson | 4d6c989 | 2019-05-07 09:06:30 -0700 | [diff] [blame] | 2139 | */ |
| 2140 | if (enable_pml) { |
Sean Christopherson | c3bb9a2 | 2021-02-12 16:50:07 -0800 | [diff] [blame] | 2141 | vmcs_write64(PML_ADDRESS, 0); |
| 2142 | vmcs_write16(GUEST_PML_INDEX, -1); |
Sean Christopherson | 4d6c989 | 2019-05-07 09:06:30 -0700 | [diff] [blame] | 2143 | } |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2144 | |
Sean Christopherson | c538d57 | 2019-05-07 09:06:29 -0700 | [diff] [blame] | 2145 | if (cpu_has_vmx_encls_vmexit()) |
Yu Zhang | 64c7850 | 2021-09-30 01:51:53 +0800 | [diff] [blame] | 2146 | vmcs_write64(ENCLS_EXITING_BITMAP, INVALID_GPA); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2147 | |
| 2148 | /* |
| 2149 | * Set the MSR load/store lists to match L0's settings. Only the |
| 2150 | * addresses are constant (for vmcs02), the counts can change based |
| 2151 | * on L2's behavior, e.g. switching to/from long mode. |
| 2152 | */ |
Aaron Lewis | 662f1d1 | 2019-11-07 21:14:39 -0800 | [diff] [blame] | 2153 | 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] | 2154 | vmcs_write64(VM_EXIT_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.host.val)); |
| 2155 | vmcs_write64(VM_ENTRY_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.guest.val)); |
| 2156 | |
| 2157 | vmx_set_constant_host_state(vmx); |
| 2158 | } |
| 2159 | |
Paolo Bonzini | b1346ab | 2019-06-06 17:24:00 +0200 | [diff] [blame] | 2160 | static void prepare_vmcs02_early_rare(struct vcpu_vmx *vmx, |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2161 | struct vmcs12 *vmcs12) |
| 2162 | { |
| 2163 | prepare_vmcs02_constant_state(vmx); |
| 2164 | |
Yu Zhang | 64c7850 | 2021-09-30 01:51:53 +0800 | [diff] [blame] | 2165 | vmcs_write64(VMCS_LINK_POINTER, INVALID_GPA); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2166 | |
| 2167 | if (enable_vpid) { |
| 2168 | if (nested_cpu_has_vpid(vmcs12) && vmx->nested.vpid02) |
| 2169 | vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->nested.vpid02); |
| 2170 | else |
| 2171 | vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid); |
| 2172 | } |
| 2173 | } |
| 2174 | |
Sean Christopherson | 389ab25 | 2021-08-10 10:19:50 -0700 | [diff] [blame] | 2175 | static void prepare_vmcs02_early(struct vcpu_vmx *vmx, struct loaded_vmcs *vmcs01, |
| 2176 | struct vmcs12 *vmcs12) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2177 | { |
Sean Christopherson | c3bb9a2 | 2021-02-12 16:50:07 -0800 | [diff] [blame] | 2178 | u32 exec_control; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2179 | u64 guest_efer = nested_vmx_calc_efer(vmx, vmcs12); |
| 2180 | |
Vitaly Kuznetsov | 1e9dfbd | 2021-05-26 15:20:16 +0200 | [diff] [blame] | 2181 | 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] | 2182 | prepare_vmcs02_early_rare(vmx, vmcs12); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2183 | |
| 2184 | /* |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2185 | * PIN CONTROLS |
| 2186 | */ |
Sean Christopherson | 389ab25 | 2021-08-10 10:19:50 -0700 | [diff] [blame] | 2187 | exec_control = __pin_controls_get(vmcs01); |
Sean Christopherson | 804939e | 2019-05-07 12:18:05 -0700 | [diff] [blame] | 2188 | exec_control |= (vmcs12->pin_based_vm_exec_control & |
| 2189 | ~PIN_BASED_VMX_PREEMPTION_TIMER); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2190 | |
| 2191 | /* Posted interrupts setting is only taken from vmcs12. */ |
Sean Christopherson | f7782bb8 | 2021-08-10 07:45:26 -0700 | [diff] [blame] | 2192 | vmx->nested.pi_pending = false; |
| 2193 | if (nested_cpu_has_posted_intr(vmcs12)) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2194 | vmx->nested.posted_intr_nv = vmcs12->posted_intr_nv; |
Sean Christopherson | f7782bb8 | 2021-08-10 07:45:26 -0700 | [diff] [blame] | 2195 | else |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2196 | exec_control &= ~PIN_BASED_POSTED_INTR; |
Sean Christopherson | 3af80fe | 2019-05-07 12:18:00 -0700 | [diff] [blame] | 2197 | pin_controls_set(vmx, exec_control); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2198 | |
| 2199 | /* |
| 2200 | * EXEC CONTROLS |
| 2201 | */ |
Sean Christopherson | 389ab25 | 2021-08-10 10:19:50 -0700 | [diff] [blame] | 2202 | exec_control = __exec_controls_get(vmcs01); /* L0's desires */ |
Xiaoyao Li | 9dadc2f | 2019-12-06 16:45:24 +0800 | [diff] [blame] | 2203 | exec_control &= ~CPU_BASED_INTR_WINDOW_EXITING; |
Xiaoyao Li | 4e2a0bc | 2019-12-06 16:45:25 +0800 | [diff] [blame] | 2204 | exec_control &= ~CPU_BASED_NMI_WINDOW_EXITING; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2205 | exec_control &= ~CPU_BASED_TPR_SHADOW; |
| 2206 | exec_control |= vmcs12->cpu_based_vm_exec_control; |
| 2207 | |
Liran Alon | 02d496cf | 2019-11-11 14:30:55 +0200 | [diff] [blame] | 2208 | vmx->nested.l1_tpr_threshold = -1; |
Sean Christopherson | ca2f546 | 2019-05-07 09:06:33 -0700 | [diff] [blame] | 2209 | if (exec_control & CPU_BASED_TPR_SHADOW) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2210 | vmcs_write32(TPR_THRESHOLD, vmcs12->tpr_threshold); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2211 | #ifdef CONFIG_X86_64 |
Sean Christopherson | ca2f546 | 2019-05-07 09:06:33 -0700 | [diff] [blame] | 2212 | else |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2213 | exec_control |= CPU_BASED_CR8_LOAD_EXITING | |
| 2214 | CPU_BASED_CR8_STORE_EXITING; |
| 2215 | #endif |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2216 | |
| 2217 | /* |
| 2218 | * A vmexit (to either L1 hypervisor or L0 userspace) is always needed |
| 2219 | * for I/O port accesses. |
| 2220 | */ |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2221 | exec_control |= CPU_BASED_UNCOND_IO_EXITING; |
Sean Christopherson | de0286b | 2019-05-07 12:18:01 -0700 | [diff] [blame] | 2222 | exec_control &= ~CPU_BASED_USE_IO_BITMAPS; |
| 2223 | |
| 2224 | /* |
| 2225 | * This bit will be computed in nested_get_vmcs12_pages, because |
| 2226 | * we do not have access to L1's MSR bitmap yet. For now, keep |
| 2227 | * the same bit as before, hoping to avoid multiple VMWRITEs that |
| 2228 | * only set/clear this bit. |
| 2229 | */ |
| 2230 | exec_control &= ~CPU_BASED_USE_MSR_BITMAPS; |
| 2231 | exec_control |= exec_controls_get(vmx) & CPU_BASED_USE_MSR_BITMAPS; |
| 2232 | |
Sean Christopherson | 3af80fe | 2019-05-07 12:18:00 -0700 | [diff] [blame] | 2233 | exec_controls_set(vmx, exec_control); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2234 | |
| 2235 | /* |
| 2236 | * SECONDARY EXEC CONTROLS |
| 2237 | */ |
| 2238 | if (cpu_has_secondary_exec_ctrls()) { |
Sean Christopherson | 389ab25 | 2021-08-10 10:19:50 -0700 | [diff] [blame] | 2239 | exec_control = __secondary_exec_controls_get(vmcs01); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2240 | |
| 2241 | /* Take the following fields only from vmcs12 */ |
| 2242 | exec_control &= ~(SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES | |
Sean Christopherson | 389ab25 | 2021-08-10 10:19:50 -0700 | [diff] [blame] | 2243 | SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2244 | SECONDARY_EXEC_ENABLE_INVPCID | |
Sean Christopherson | 7f3603b | 2020-09-23 09:50:47 -0700 | [diff] [blame] | 2245 | SECONDARY_EXEC_ENABLE_RDTSCP | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2246 | SECONDARY_EXEC_XSAVES | |
Tao Xu | e69e72fa | 2019-07-16 14:55:49 +0800 | [diff] [blame] | 2247 | SECONDARY_EXEC_ENABLE_USR_WAIT_PAUSE | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2248 | SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY | |
| 2249 | SECONDARY_EXEC_APIC_REGISTER_VIRT | |
Ilias Stamatis | d041b5e | 2021-05-26 19:44:17 +0100 | [diff] [blame] | 2250 | SECONDARY_EXEC_ENABLE_VMFUNC | |
Sean Christopherson | 389ab25 | 2021-08-10 10:19:50 -0700 | [diff] [blame] | 2251 | SECONDARY_EXEC_TSC_SCALING | |
| 2252 | SECONDARY_EXEC_DESC); |
| 2253 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2254 | if (nested_cpu_has(vmcs12, |
Sean Christopherson | c3bb9a2 | 2021-02-12 16:50:07 -0800 | [diff] [blame] | 2255 | CPU_BASED_ACTIVATE_SECONDARY_CONTROLS)) |
| 2256 | exec_control |= vmcs12->secondary_vm_exec_control; |
| 2257 | |
| 2258 | /* PML is emulated and never enabled in hardware for L2. */ |
| 2259 | exec_control &= ~SECONDARY_EXEC_ENABLE_PML; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2260 | |
| 2261 | /* VMCS shadowing for L2 is emulated for now */ |
| 2262 | exec_control &= ~SECONDARY_EXEC_SHADOW_VMCS; |
| 2263 | |
Sean Christopherson | 469debd | 2019-05-07 12:18:02 -0700 | [diff] [blame] | 2264 | /* |
| 2265 | * Preset *DT exiting when emulating UMIP, so that vmx_set_cr4() |
| 2266 | * will not have to rewrite the controls just for this bit. |
| 2267 | */ |
| 2268 | if (!boot_cpu_has(X86_FEATURE_UMIP) && vmx_umip_emulated() && |
| 2269 | (vmcs12->guest_cr4 & X86_CR4_UMIP)) |
| 2270 | exec_control |= SECONDARY_EXEC_DESC; |
| 2271 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2272 | if (exec_control & SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY) |
| 2273 | vmcs_write16(GUEST_INTR_STATUS, |
| 2274 | vmcs12->guest_intr_status); |
| 2275 | |
Krish Sadhukhan | bddd82d | 2020-09-21 08:10:25 +0000 | [diff] [blame] | 2276 | if (!nested_cpu_has2(vmcs12, SECONDARY_EXEC_UNRESTRICTED_GUEST)) |
| 2277 | exec_control &= ~SECONDARY_EXEC_UNRESTRICTED_GUEST; |
| 2278 | |
Sean Christopherson | 72add91 | 2021-04-12 16:21:42 +1200 | [diff] [blame] | 2279 | if (exec_control & SECONDARY_EXEC_ENCLS_EXITING) |
| 2280 | vmx_write_encls_bitmap(&vmx->vcpu, vmcs12); |
| 2281 | |
Sean Christopherson | 3af80fe | 2019-05-07 12:18:00 -0700 | [diff] [blame] | 2282 | secondary_exec_controls_set(vmx, exec_control); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2283 | } |
| 2284 | |
| 2285 | /* |
| 2286 | * ENTRY CONTROLS |
| 2287 | * |
| 2288 | * vmcs12's VM_{ENTRY,EXIT}_LOAD_IA32_EFER and VM_ENTRY_IA32E_MODE |
| 2289 | * are emulated by vmx_set_efer() in prepare_vmcs02(), but speculate |
| 2290 | * on the related bits (if supported by the CPU) in the hope that |
| 2291 | * we can avoid VMWrites during vmx_set_efer(). |
| 2292 | */ |
Sean Christopherson | 389ab25 | 2021-08-10 10:19:50 -0700 | [diff] [blame] | 2293 | exec_control = __vm_entry_controls_get(vmcs01); |
| 2294 | exec_control |= vmcs12->vm_entry_controls; |
| 2295 | exec_control &= ~(VM_ENTRY_IA32E_MODE | VM_ENTRY_LOAD_IA32_EFER); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2296 | if (cpu_has_load_ia32_efer()) { |
| 2297 | if (guest_efer & EFER_LMA) |
| 2298 | exec_control |= VM_ENTRY_IA32E_MODE; |
| 2299 | if (guest_efer != host_efer) |
| 2300 | exec_control |= VM_ENTRY_LOAD_IA32_EFER; |
| 2301 | } |
Sean Christopherson | 3af80fe | 2019-05-07 12:18:00 -0700 | [diff] [blame] | 2302 | vm_entry_controls_set(vmx, exec_control); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2303 | |
| 2304 | /* |
| 2305 | * EXIT CONTROLS |
| 2306 | * |
| 2307 | * L2->L1 exit controls are emulated - the hardware exit is to L0 so |
| 2308 | * we should use its exit controls. Note that VM_EXIT_LOAD_IA32_EFER |
| 2309 | * bits may be modified by vmx_set_efer() in prepare_vmcs02(). |
| 2310 | */ |
Sean Christopherson | 389ab25 | 2021-08-10 10:19:50 -0700 | [diff] [blame] | 2311 | exec_control = __vm_exit_controls_get(vmcs01); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2312 | if (cpu_has_load_ia32_efer() && guest_efer != host_efer) |
| 2313 | exec_control |= VM_EXIT_LOAD_IA32_EFER; |
Sean Christopherson | 389ab25 | 2021-08-10 10:19:50 -0700 | [diff] [blame] | 2314 | else |
| 2315 | exec_control &= ~VM_EXIT_LOAD_IA32_EFER; |
Sean Christopherson | 3af80fe | 2019-05-07 12:18:00 -0700 | [diff] [blame] | 2316 | vm_exit_controls_set(vmx, exec_control); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2317 | |
| 2318 | /* |
| 2319 | * Interrupt/Exception Fields |
| 2320 | */ |
| 2321 | if (vmx->nested.nested_run_pending) { |
| 2322 | vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, |
| 2323 | vmcs12->vm_entry_intr_info_field); |
| 2324 | vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE, |
| 2325 | vmcs12->vm_entry_exception_error_code); |
| 2326 | vmcs_write32(VM_ENTRY_INSTRUCTION_LEN, |
| 2327 | vmcs12->vm_entry_instruction_len); |
| 2328 | vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, |
| 2329 | vmcs12->guest_interruptibility_info); |
| 2330 | vmx->loaded_vmcs->nmi_known_unmasked = |
| 2331 | !(vmcs12->guest_interruptibility_info & GUEST_INTR_STATE_NMI); |
| 2332 | } else { |
| 2333 | vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0); |
| 2334 | } |
| 2335 | } |
| 2336 | |
Paolo Bonzini | b1346ab | 2019-06-06 17:24:00 +0200 | [diff] [blame] | 2337 | static void prepare_vmcs02_rare(struct vcpu_vmx *vmx, struct vmcs12 *vmcs12) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2338 | { |
| 2339 | struct hv_enlightened_vmcs *hv_evmcs = vmx->nested.hv_evmcs; |
| 2340 | |
| 2341 | if (!hv_evmcs || !(hv_evmcs->hv_clean_fields & |
| 2342 | HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP2)) { |
| 2343 | vmcs_write16(GUEST_ES_SELECTOR, vmcs12->guest_es_selector); |
| 2344 | vmcs_write16(GUEST_CS_SELECTOR, vmcs12->guest_cs_selector); |
| 2345 | vmcs_write16(GUEST_SS_SELECTOR, vmcs12->guest_ss_selector); |
| 2346 | vmcs_write16(GUEST_DS_SELECTOR, vmcs12->guest_ds_selector); |
| 2347 | vmcs_write16(GUEST_FS_SELECTOR, vmcs12->guest_fs_selector); |
| 2348 | vmcs_write16(GUEST_GS_SELECTOR, vmcs12->guest_gs_selector); |
| 2349 | vmcs_write16(GUEST_LDTR_SELECTOR, vmcs12->guest_ldtr_selector); |
| 2350 | vmcs_write16(GUEST_TR_SELECTOR, vmcs12->guest_tr_selector); |
| 2351 | vmcs_write32(GUEST_ES_LIMIT, vmcs12->guest_es_limit); |
| 2352 | vmcs_write32(GUEST_CS_LIMIT, vmcs12->guest_cs_limit); |
| 2353 | vmcs_write32(GUEST_SS_LIMIT, vmcs12->guest_ss_limit); |
| 2354 | vmcs_write32(GUEST_DS_LIMIT, vmcs12->guest_ds_limit); |
| 2355 | vmcs_write32(GUEST_FS_LIMIT, vmcs12->guest_fs_limit); |
| 2356 | vmcs_write32(GUEST_GS_LIMIT, vmcs12->guest_gs_limit); |
| 2357 | vmcs_write32(GUEST_LDTR_LIMIT, vmcs12->guest_ldtr_limit); |
| 2358 | vmcs_write32(GUEST_TR_LIMIT, vmcs12->guest_tr_limit); |
| 2359 | vmcs_write32(GUEST_GDTR_LIMIT, vmcs12->guest_gdtr_limit); |
| 2360 | vmcs_write32(GUEST_IDTR_LIMIT, vmcs12->guest_idtr_limit); |
Sean Christopherson | 1c6f0b4 | 2019-05-07 08:36:25 -0700 | [diff] [blame] | 2361 | vmcs_write32(GUEST_CS_AR_BYTES, vmcs12->guest_cs_ar_bytes); |
| 2362 | vmcs_write32(GUEST_SS_AR_BYTES, vmcs12->guest_ss_ar_bytes); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2363 | vmcs_write32(GUEST_ES_AR_BYTES, vmcs12->guest_es_ar_bytes); |
| 2364 | vmcs_write32(GUEST_DS_AR_BYTES, vmcs12->guest_ds_ar_bytes); |
| 2365 | vmcs_write32(GUEST_FS_AR_BYTES, vmcs12->guest_fs_ar_bytes); |
| 2366 | vmcs_write32(GUEST_GS_AR_BYTES, vmcs12->guest_gs_ar_bytes); |
| 2367 | vmcs_write32(GUEST_LDTR_AR_BYTES, vmcs12->guest_ldtr_ar_bytes); |
| 2368 | vmcs_write32(GUEST_TR_AR_BYTES, vmcs12->guest_tr_ar_bytes); |
| 2369 | vmcs_writel(GUEST_ES_BASE, vmcs12->guest_es_base); |
| 2370 | vmcs_writel(GUEST_CS_BASE, vmcs12->guest_cs_base); |
| 2371 | vmcs_writel(GUEST_SS_BASE, vmcs12->guest_ss_base); |
| 2372 | vmcs_writel(GUEST_DS_BASE, vmcs12->guest_ds_base); |
| 2373 | vmcs_writel(GUEST_FS_BASE, vmcs12->guest_fs_base); |
| 2374 | vmcs_writel(GUEST_GS_BASE, vmcs12->guest_gs_base); |
| 2375 | vmcs_writel(GUEST_LDTR_BASE, vmcs12->guest_ldtr_base); |
| 2376 | vmcs_writel(GUEST_TR_BASE, vmcs12->guest_tr_base); |
| 2377 | vmcs_writel(GUEST_GDTR_BASE, vmcs12->guest_gdtr_base); |
| 2378 | vmcs_writel(GUEST_IDTR_BASE, vmcs12->guest_idtr_base); |
Sean Christopherson | fc387d8 | 2020-09-23 11:44:46 -0700 | [diff] [blame] | 2379 | |
| 2380 | vmx->segment_cache.bitmask = 0; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2381 | } |
| 2382 | |
| 2383 | if (!hv_evmcs || !(hv_evmcs->hv_clean_fields & |
| 2384 | HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP1)) { |
| 2385 | vmcs_write32(GUEST_SYSENTER_CS, vmcs12->guest_sysenter_cs); |
| 2386 | vmcs_writel(GUEST_PENDING_DBG_EXCEPTIONS, |
| 2387 | vmcs12->guest_pending_dbg_exceptions); |
| 2388 | vmcs_writel(GUEST_SYSENTER_ESP, vmcs12->guest_sysenter_esp); |
| 2389 | vmcs_writel(GUEST_SYSENTER_EIP, vmcs12->guest_sysenter_eip); |
| 2390 | |
| 2391 | /* |
| 2392 | * L1 may access the L2's PDPTR, so save them to construct |
| 2393 | * vmcs12 |
| 2394 | */ |
| 2395 | if (enable_ept) { |
| 2396 | vmcs_write64(GUEST_PDPTR0, vmcs12->guest_pdptr0); |
| 2397 | vmcs_write64(GUEST_PDPTR1, vmcs12->guest_pdptr1); |
| 2398 | vmcs_write64(GUEST_PDPTR2, vmcs12->guest_pdptr2); |
| 2399 | vmcs_write64(GUEST_PDPTR3, vmcs12->guest_pdptr3); |
| 2400 | } |
Sean Christopherson | c27e5b0 | 2019-05-07 09:06:39 -0700 | [diff] [blame] | 2401 | |
| 2402 | if (kvm_mpx_supported() && vmx->nested.nested_run_pending && |
| 2403 | (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_BNDCFGS)) |
| 2404 | vmcs_write64(GUEST_BNDCFGS, vmcs12->guest_bndcfgs); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2405 | } |
| 2406 | |
| 2407 | if (nested_cpu_has_xsaves(vmcs12)) |
| 2408 | vmcs_write64(XSS_EXIT_BITMAP, vmcs12->xss_exit_bitmap); |
| 2409 | |
| 2410 | /* |
| 2411 | * Whether page-faults are trapped is determined by a combination of |
Paolo Bonzini | a0c1343 | 2020-07-10 17:48:08 +0200 | [diff] [blame] | 2412 | * 3 settings: PFEC_MASK, PFEC_MATCH and EXCEPTION_BITMAP.PF. If L0 |
| 2413 | * doesn't care about page faults then we should set all of these to |
| 2414 | * L1's desires. However, if L0 does care about (some) page faults, it |
| 2415 | * is not easy (if at all possible?) to merge L0 and L1's desires, we |
| 2416 | * simply ask to exit on each and every L2 page fault. This is done by |
| 2417 | * setting MASK=MATCH=0 and (see below) EB.PF=1. |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2418 | * Note that below we don't need special code to set EB.PF beyond the |
| 2419 | * "or"ing of the EB of vmcs01 and vmcs12, because when enable_ept, |
| 2420 | * vmcs01's EB.PF is 0 so the "or" will take vmcs12's value, and when |
| 2421 | * !enable_ept, EB.PF is 1, so the "or" will always be 1. |
| 2422 | */ |
Paolo Bonzini | a0c1343 | 2020-07-10 17:48:08 +0200 | [diff] [blame] | 2423 | if (vmx_need_pf_intercept(&vmx->vcpu)) { |
| 2424 | /* |
| 2425 | * TODO: if both L0 and L1 need the same MASK and MATCH, |
| 2426 | * go ahead and use it? |
| 2427 | */ |
| 2428 | vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK, 0); |
| 2429 | vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH, 0); |
| 2430 | } else { |
| 2431 | vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK, vmcs12->page_fault_error_code_mask); |
| 2432 | vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH, vmcs12->page_fault_error_code_match); |
| 2433 | } |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2434 | |
| 2435 | if (cpu_has_vmx_apicv()) { |
| 2436 | vmcs_write64(EOI_EXIT_BITMAP0, vmcs12->eoi_exit_bitmap0); |
| 2437 | vmcs_write64(EOI_EXIT_BITMAP1, vmcs12->eoi_exit_bitmap1); |
| 2438 | vmcs_write64(EOI_EXIT_BITMAP2, vmcs12->eoi_exit_bitmap2); |
| 2439 | vmcs_write64(EOI_EXIT_BITMAP3, vmcs12->eoi_exit_bitmap3); |
| 2440 | } |
| 2441 | |
Aaron Lewis | 662f1d1 | 2019-11-07 21:14:39 -0800 | [diff] [blame] | 2442 | /* |
| 2443 | * Make sure the msr_autostore list is up to date before we set the |
| 2444 | * count in the vmcs02. |
| 2445 | */ |
| 2446 | prepare_vmx_msr_autostore_list(&vmx->vcpu, MSR_IA32_TSC); |
| 2447 | |
| 2448 | vmcs_write32(VM_EXIT_MSR_STORE_COUNT, vmx->msr_autostore.guest.nr); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2449 | vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, vmx->msr_autoload.host.nr); |
| 2450 | vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, vmx->msr_autoload.guest.nr); |
| 2451 | |
| 2452 | set_cr4_guest_host_mask(vmx); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2453 | } |
| 2454 | |
| 2455 | /* |
| 2456 | * prepare_vmcs02 is called when the L1 guest hypervisor runs its nested |
| 2457 | * L2 guest. L1 has a vmcs for L2 (vmcs12), and this function "merges" it |
| 2458 | * with L0's requirements for its guest (a.k.a. vmcs01), so we can run the L2 |
| 2459 | * guest in a way that will both be appropriate to L1's requests, and our |
| 2460 | * needs. In addition to modifying the active vmcs (which is vmcs02), this |
| 2461 | * function also has additional necessary side-effects, like setting various |
| 2462 | * vcpu->arch fields. |
| 2463 | * Returns 0 on success, 1 on failure. Invalid state exit qualification code |
| 2464 | * is assigned to entry_failure_code on failure. |
| 2465 | */ |
| 2466 | static int prepare_vmcs02(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12, |
Maxim Levitsky | 0f85722 | 2021-06-07 12:02:00 +0300 | [diff] [blame] | 2467 | bool from_vmentry, |
Sean Christopherson | 68cda40 | 2020-05-11 15:05:29 -0700 | [diff] [blame] | 2468 | enum vm_entry_failure_code *entry_failure_code) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2469 | { |
| 2470 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Sean Christopherson | c7554efc | 2019-05-07 09:06:40 -0700 | [diff] [blame] | 2471 | bool load_guest_pdptrs_vmcs12 = false; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2472 | |
Vitaly Kuznetsov | 1e9dfbd | 2021-05-26 15:20:16 +0200 | [diff] [blame] | 2473 | 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] | 2474 | prepare_vmcs02_rare(vmx, vmcs12); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2475 | vmx->nested.dirty_vmcs12 = false; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2476 | |
Vitaly Kuznetsov | 1e9dfbd | 2021-05-26 15:20:16 +0200 | [diff] [blame] | 2477 | load_guest_pdptrs_vmcs12 = !evmptr_is_valid(vmx->nested.hv_evmcs_vmptr) || |
| 2478 | !(vmx->nested.hv_evmcs->hv_clean_fields & |
Sean Christopherson | c7554efc | 2019-05-07 09:06:40 -0700 | [diff] [blame] | 2479 | HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP1); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2480 | } |
| 2481 | |
| 2482 | if (vmx->nested.nested_run_pending && |
| 2483 | (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS)) { |
| 2484 | kvm_set_dr(vcpu, 7, vmcs12->guest_dr7); |
| 2485 | vmcs_write64(GUEST_IA32_DEBUGCTL, vmcs12->guest_ia32_debugctl); |
| 2486 | } else { |
| 2487 | kvm_set_dr(vcpu, 7, vcpu->arch.dr7); |
| 2488 | vmcs_write64(GUEST_IA32_DEBUGCTL, vmx->nested.vmcs01_debugctl); |
| 2489 | } |
Sean Christopherson | 3b013a2 | 2019-05-07 09:06:28 -0700 | [diff] [blame] | 2490 | if (kvm_mpx_supported() && (!vmx->nested.nested_run_pending || |
| 2491 | !(vmcs12->vm_entry_controls & VM_ENTRY_LOAD_BNDCFGS))) |
| 2492 | vmcs_write64(GUEST_BNDCFGS, vmx->nested.vmcs01_guest_bndcfgs); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2493 | vmx_set_rflags(vcpu, vmcs12->guest_rflags); |
| 2494 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2495 | /* EXCEPTION_BITMAP and CR0_GUEST_HOST_MASK should basically be the |
| 2496 | * bitwise-or of what L1 wants to trap for L2, and what we want to |
| 2497 | * trap. Note that CR0.TS also needs updating - we do this later. |
| 2498 | */ |
Jason Baron | b6a7cc3 | 2021-01-14 22:27:54 -0500 | [diff] [blame] | 2499 | vmx_update_exception_bitmap(vcpu); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2500 | vcpu->arch.cr0_guest_owned_bits &= ~vmcs12->cr0_guest_host_mask; |
| 2501 | vmcs_writel(CR0_GUEST_HOST_MASK, ~vcpu->arch.cr0_guest_owned_bits); |
| 2502 | |
| 2503 | if (vmx->nested.nested_run_pending && |
| 2504 | (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_PAT)) { |
| 2505 | vmcs_write64(GUEST_IA32_PAT, vmcs12->guest_ia32_pat); |
| 2506 | vcpu->arch.pat = vmcs12->guest_ia32_pat; |
| 2507 | } else if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) { |
| 2508 | vmcs_write64(GUEST_IA32_PAT, vmx->vcpu.arch.pat); |
| 2509 | } |
| 2510 | |
Ilias Stamatis | d041b5e | 2021-05-26 19:44:17 +0100 | [diff] [blame] | 2511 | vcpu->arch.tsc_offset = kvm_calc_nested_tsc_offset( |
| 2512 | vcpu->arch.l1_tsc_offset, |
| 2513 | vmx_get_l2_tsc_offset(vcpu), |
| 2514 | vmx_get_l2_tsc_multiplier(vcpu)); |
| 2515 | |
| 2516 | vcpu->arch.tsc_scaling_ratio = kvm_calc_nested_tsc_multiplier( |
| 2517 | vcpu->arch.l1_tsc_scaling_ratio, |
| 2518 | vmx_get_l2_tsc_multiplier(vcpu)); |
| 2519 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2520 | vmcs_write64(TSC_OFFSET, vcpu->arch.tsc_offset); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2521 | if (kvm_has_tsc_control) |
Ilias Stamatis | 1ab9287 | 2021-06-07 11:54:38 +0100 | [diff] [blame] | 2522 | vmcs_write64(TSC_MULTIPLIER, vcpu->arch.tsc_scaling_ratio); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2523 | |
Sean Christopherson | 50b265a | 2020-03-20 14:28:19 -0700 | [diff] [blame] | 2524 | nested_vmx_transition_tlb_flush(vcpu, vmcs12, true); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2525 | |
| 2526 | if (nested_cpu_has_ept(vmcs12)) |
| 2527 | nested_ept_init_mmu_context(vcpu); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2528 | |
| 2529 | /* |
| 2530 | * This sets GUEST_CR0 to vmcs12->guest_cr0, possibly modifying those |
| 2531 | * bits which we consider mandatory enabled. |
| 2532 | * The CR0_READ_SHADOW is what L2 should have expected to read given |
| 2533 | * the specifications by L1; It's not enough to take |
| 2534 | * vmcs12->cr0_read_shadow because on our cr0_guest_host_mask we we |
| 2535 | * have more bits than L1 expected. |
| 2536 | */ |
| 2537 | vmx_set_cr0(vcpu, vmcs12->guest_cr0); |
| 2538 | vmcs_writel(CR0_READ_SHADOW, nested_read_cr0(vmcs12)); |
| 2539 | |
| 2540 | vmx_set_cr4(vcpu, vmcs12->guest_cr4); |
| 2541 | vmcs_writel(CR4_READ_SHADOW, nested_read_cr4(vmcs12)); |
| 2542 | |
| 2543 | vcpu->arch.efer = nested_vmx_calc_efer(vmx, vmcs12); |
| 2544 | /* Note: may modify VM_ENTRY/EXIT_CONTROLS and GUEST/HOST_IA32_EFER */ |
| 2545 | vmx_set_efer(vcpu, vcpu->arch.efer); |
| 2546 | |
| 2547 | /* |
| 2548 | * Guest state is invalid and unrestricted guest is disabled, |
| 2549 | * which means L1 attempted VMEntry to L2 with invalid state. |
| 2550 | * Fail the VMEntry. |
Maxim Levitsky | c8607e4 | 2021-09-13 17:09:53 +0300 | [diff] [blame] | 2551 | * |
| 2552 | * However when force loading the guest state (SMM exit or |
| 2553 | * loading nested state after migration, it is possible to |
| 2554 | * have invalid guest state now, which will be later fixed by |
| 2555 | * restoring L2 register state |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2556 | */ |
Maxim Levitsky | c8607e4 | 2021-09-13 17:09:53 +0300 | [diff] [blame] | 2557 | if (CC(from_vmentry && !vmx_guest_state_valid(vcpu))) { |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2558 | *entry_failure_code = ENTRY_FAIL_DEFAULT; |
Sean Christopherson | c80add0 | 2019-04-11 12:18:09 -0700 | [diff] [blame] | 2559 | return -EINVAL; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2560 | } |
| 2561 | |
| 2562 | /* Shadow page tables on either EPT or shadow page tables. */ |
| 2563 | 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] | 2564 | from_vmentry, entry_failure_code)) |
Sean Christopherson | c80add0 | 2019-04-11 12:18:09 -0700 | [diff] [blame] | 2565 | return -EINVAL; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2566 | |
Sean Christopherson | 04f11ef | 2019-09-27 14:45:16 -0700 | [diff] [blame] | 2567 | /* |
| 2568 | * Immediately write vmcs02.GUEST_CR3. It will be propagated to vmcs12 |
| 2569 | * on nested VM-Exit, which can occur without actually running L2 and |
Paolo Bonzini | 727a7e2 | 2020-03-05 03:52:50 -0500 | [diff] [blame] | 2570 | * 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] | 2571 | * vmcs12.GUEST_ACTIVITYSTATE=HLT, in which case KVM will intercept the |
| 2572 | * transition to HLT instead of running L2. |
| 2573 | */ |
| 2574 | if (enable_ept) |
| 2575 | vmcs_writel(GUEST_CR3, vmcs12->guest_cr3); |
| 2576 | |
Sean Christopherson | c7554efc | 2019-05-07 09:06:40 -0700 | [diff] [blame] | 2577 | /* Late preparation of GUEST_PDPTRs now that EFER and CRs are set. */ |
| 2578 | if (load_guest_pdptrs_vmcs12 && nested_cpu_has_ept(vmcs12) && |
| 2579 | is_pae_paging(vcpu)) { |
| 2580 | vmcs_write64(GUEST_PDPTR0, vmcs12->guest_pdptr0); |
| 2581 | vmcs_write64(GUEST_PDPTR1, vmcs12->guest_pdptr1); |
| 2582 | vmcs_write64(GUEST_PDPTR2, vmcs12->guest_pdptr2); |
| 2583 | vmcs_write64(GUEST_PDPTR3, vmcs12->guest_pdptr3); |
| 2584 | } |
| 2585 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2586 | if (!enable_ept) |
| 2587 | vcpu->arch.walk_mmu->inject_page_fault = vmx_inject_page_fault_nested; |
| 2588 | |
Oliver Upton | 71f7347 | 2019-11-13 16:17:19 -0800 | [diff] [blame] | 2589 | if ((vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL) && |
Oliver Upton | d196842 | 2019-12-13 16:33:58 -0800 | [diff] [blame] | 2590 | WARN_ON_ONCE(kvm_set_msr(vcpu, MSR_CORE_PERF_GLOBAL_CTRL, |
| 2591 | vmcs12->guest_ia32_perf_global_ctrl))) |
Oliver Upton | 71f7347 | 2019-11-13 16:17:19 -0800 | [diff] [blame] | 2592 | return -EINVAL; |
| 2593 | |
Paolo Bonzini | e9c16c7 | 2019-04-30 22:07:26 +0200 | [diff] [blame] | 2594 | kvm_rsp_write(vcpu, vmcs12->guest_rsp); |
| 2595 | kvm_rip_write(vcpu, vmcs12->guest_rip); |
Vitaly Kuznetsov | dc31338 | 2021-05-26 15:20:24 +0200 | [diff] [blame] | 2596 | |
| 2597 | /* |
| 2598 | * It was observed that genuine Hyper-V running in L1 doesn't reset |
| 2599 | * 'hv_clean_fields' by itself, it only sets the corresponding dirty |
| 2600 | * bits when it changes a field in eVMCS. Mark all fields as clean |
| 2601 | * here. |
| 2602 | */ |
| 2603 | if (evmptr_is_valid(vmx->nested.hv_evmcs_vmptr)) |
| 2604 | vmx->nested.hv_evmcs->hv_clean_fields |= |
| 2605 | HV_VMX_ENLIGHTENED_CLEAN_FIELD_ALL; |
| 2606 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2607 | return 0; |
| 2608 | } |
| 2609 | |
| 2610 | static int nested_vmx_check_nmi_controls(struct vmcs12 *vmcs12) |
| 2611 | { |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 2612 | if (CC(!nested_cpu_has_nmi_exiting(vmcs12) && |
| 2613 | nested_cpu_has_virtual_nmis(vmcs12))) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2614 | return -EINVAL; |
| 2615 | |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 2616 | if (CC(!nested_cpu_has_virtual_nmis(vmcs12) && |
Xiaoyao Li | 4e2a0bc | 2019-12-06 16:45:25 +0800 | [diff] [blame] | 2617 | nested_cpu_has(vmcs12, CPU_BASED_NMI_WINDOW_EXITING))) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2618 | return -EINVAL; |
| 2619 | |
| 2620 | return 0; |
| 2621 | } |
| 2622 | |
Sean Christopherson | ac6389a | 2020-03-02 18:02:38 -0800 | [diff] [blame] | 2623 | 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] | 2624 | { |
| 2625 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2626 | |
| 2627 | /* Check for memory type validity */ |
Sean Christopherson | ac6389a | 2020-03-02 18:02:38 -0800 | [diff] [blame] | 2628 | switch (new_eptp & VMX_EPTP_MT_MASK) { |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2629 | case VMX_EPTP_MT_UC: |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 2630 | if (CC(!(vmx->nested.msrs.ept_caps & VMX_EPTP_UC_BIT))) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2631 | return false; |
| 2632 | break; |
| 2633 | case VMX_EPTP_MT_WB: |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 2634 | if (CC(!(vmx->nested.msrs.ept_caps & VMX_EPTP_WB_BIT))) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2635 | return false; |
| 2636 | break; |
| 2637 | default: |
| 2638 | return false; |
| 2639 | } |
| 2640 | |
Sean Christopherson | bb1fcc7 | 2020-03-02 18:02:36 -0800 | [diff] [blame] | 2641 | /* Page-walk levels validity. */ |
Sean Christopherson | ac6389a | 2020-03-02 18:02:38 -0800 | [diff] [blame] | 2642 | switch (new_eptp & VMX_EPTP_PWL_MASK) { |
Sean Christopherson | bb1fcc7 | 2020-03-02 18:02:36 -0800 | [diff] [blame] | 2643 | case VMX_EPTP_PWL_5: |
| 2644 | if (CC(!(vmx->nested.msrs.ept_caps & VMX_EPT_PAGE_WALK_5_BIT))) |
| 2645 | return false; |
| 2646 | break; |
| 2647 | case VMX_EPTP_PWL_4: |
| 2648 | if (CC(!(vmx->nested.msrs.ept_caps & VMX_EPT_PAGE_WALK_4_BIT))) |
| 2649 | return false; |
| 2650 | break; |
| 2651 | default: |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2652 | return false; |
Sean Christopherson | bb1fcc7 | 2020-03-02 18:02:36 -0800 | [diff] [blame] | 2653 | } |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2654 | |
| 2655 | /* Reserved bits should not be set */ |
Sean Christopherson | 636e8b7 | 2021-02-03 16:01:10 -0800 | [diff] [blame] | 2656 | 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] | 2657 | return false; |
| 2658 | |
| 2659 | /* AD, if set, should be supported */ |
Sean Christopherson | ac6389a | 2020-03-02 18:02:38 -0800 | [diff] [blame] | 2660 | if (new_eptp & VMX_EPTP_AD_ENABLE_BIT) { |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 2661 | if (CC(!(vmx->nested.msrs.ept_caps & VMX_EPT_AD_BIT))) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2662 | return false; |
| 2663 | } |
| 2664 | |
| 2665 | return true; |
| 2666 | } |
| 2667 | |
Krish Sadhukhan | 461b4ba | 2018-12-12 13:30:07 -0500 | [diff] [blame] | 2668 | /* |
| 2669 | * Checks related to VM-Execution Control Fields |
| 2670 | */ |
| 2671 | static int nested_check_vm_execution_controls(struct kvm_vcpu *vcpu, |
| 2672 | struct vmcs12 *vmcs12) |
| 2673 | { |
| 2674 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 2675 | |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 2676 | if (CC(!vmx_control_verify(vmcs12->pin_based_vm_exec_control, |
| 2677 | vmx->nested.msrs.pinbased_ctls_low, |
| 2678 | vmx->nested.msrs.pinbased_ctls_high)) || |
| 2679 | CC(!vmx_control_verify(vmcs12->cpu_based_vm_exec_control, |
| 2680 | vmx->nested.msrs.procbased_ctls_low, |
| 2681 | vmx->nested.msrs.procbased_ctls_high))) |
Krish Sadhukhan | 461b4ba | 2018-12-12 13:30:07 -0500 | [diff] [blame] | 2682 | return -EINVAL; |
| 2683 | |
| 2684 | if (nested_cpu_has(vmcs12, CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) && |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 2685 | CC(!vmx_control_verify(vmcs12->secondary_vm_exec_control, |
| 2686 | vmx->nested.msrs.secondary_ctls_low, |
| 2687 | vmx->nested.msrs.secondary_ctls_high))) |
Krish Sadhukhan | 461b4ba | 2018-12-12 13:30:07 -0500 | [diff] [blame] | 2688 | return -EINVAL; |
| 2689 | |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 2690 | 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] | 2691 | nested_vmx_check_io_bitmap_controls(vcpu, vmcs12) || |
| 2692 | nested_vmx_check_msr_bitmap_controls(vcpu, vmcs12) || |
| 2693 | nested_vmx_check_tpr_shadow_controls(vcpu, vmcs12) || |
| 2694 | nested_vmx_check_apic_access_controls(vcpu, vmcs12) || |
| 2695 | nested_vmx_check_apicv_controls(vcpu, vmcs12) || |
| 2696 | nested_vmx_check_nmi_controls(vmcs12) || |
| 2697 | nested_vmx_check_pml_controls(vcpu, vmcs12) || |
| 2698 | nested_vmx_check_unrestricted_guest_controls(vcpu, vmcs12) || |
| 2699 | nested_vmx_check_mode_based_ept_exec_controls(vcpu, vmcs12) || |
| 2700 | nested_vmx_check_shadow_vmcs_controls(vcpu, vmcs12) || |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 2701 | CC(nested_cpu_has_vpid(vmcs12) && !vmcs12->virtual_processor_id)) |
Krish Sadhukhan | 461b4ba | 2018-12-12 13:30:07 -0500 | [diff] [blame] | 2702 | return -EINVAL; |
| 2703 | |
Sean Christopherson | bc44121 | 2019-02-12 16:42:23 -0800 | [diff] [blame] | 2704 | if (!nested_cpu_has_preemption_timer(vmcs12) && |
| 2705 | nested_cpu_has_save_preemption_timer(vmcs12)) |
| 2706 | return -EINVAL; |
| 2707 | |
Krish Sadhukhan | 461b4ba | 2018-12-12 13:30:07 -0500 | [diff] [blame] | 2708 | if (nested_cpu_has_ept(vmcs12) && |
Sean Christopherson | ac6389a | 2020-03-02 18:02:38 -0800 | [diff] [blame] | 2709 | CC(!nested_vmx_check_eptp(vcpu, vmcs12->ept_pointer))) |
Krish Sadhukhan | 461b4ba | 2018-12-12 13:30:07 -0500 | [diff] [blame] | 2710 | return -EINVAL; |
| 2711 | |
| 2712 | if (nested_cpu_has_vmfunc(vmcs12)) { |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 2713 | if (CC(vmcs12->vm_function_control & |
| 2714 | ~vmx->nested.msrs.vmfunc_controls)) |
Krish Sadhukhan | 461b4ba | 2018-12-12 13:30:07 -0500 | [diff] [blame] | 2715 | return -EINVAL; |
| 2716 | |
| 2717 | if (nested_cpu_has_eptp_switching(vmcs12)) { |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 2718 | if (CC(!nested_cpu_has_ept(vmcs12)) || |
| 2719 | CC(!page_address_valid(vcpu, vmcs12->eptp_list_address))) |
Krish Sadhukhan | 461b4ba | 2018-12-12 13:30:07 -0500 | [diff] [blame] | 2720 | return -EINVAL; |
| 2721 | } |
| 2722 | } |
| 2723 | |
| 2724 | return 0; |
| 2725 | } |
| 2726 | |
Krish Sadhukhan | 61446ba | 2018-12-12 13:30:09 -0500 | [diff] [blame] | 2727 | /* |
| 2728 | * Checks related to VM-Exit Control Fields |
| 2729 | */ |
| 2730 | static int nested_check_vm_exit_controls(struct kvm_vcpu *vcpu, |
| 2731 | struct vmcs12 *vmcs12) |
| 2732 | { |
| 2733 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 2734 | |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 2735 | if (CC(!vmx_control_verify(vmcs12->vm_exit_controls, |
| 2736 | vmx->nested.msrs.exit_ctls_low, |
| 2737 | vmx->nested.msrs.exit_ctls_high)) || |
| 2738 | CC(nested_vmx_check_exit_msr_switch_controls(vcpu, vmcs12))) |
Krish Sadhukhan | 61446ba | 2018-12-12 13:30:09 -0500 | [diff] [blame] | 2739 | return -EINVAL; |
| 2740 | |
| 2741 | return 0; |
| 2742 | } |
| 2743 | |
Krish Sadhukhan | 5fbf963 | 2018-12-12 13:30:10 -0500 | [diff] [blame] | 2744 | /* |
| 2745 | * Checks related to VM-Entry Control Fields |
| 2746 | */ |
| 2747 | static int nested_check_vm_entry_controls(struct kvm_vcpu *vcpu, |
| 2748 | struct vmcs12 *vmcs12) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2749 | { |
| 2750 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2751 | |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 2752 | if (CC(!vmx_control_verify(vmcs12->vm_entry_controls, |
| 2753 | vmx->nested.msrs.entry_ctls_low, |
| 2754 | vmx->nested.msrs.entry_ctls_high))) |
Krish Sadhukhan | 5fbf963 | 2018-12-12 13:30:10 -0500 | [diff] [blame] | 2755 | return -EINVAL; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2756 | |
| 2757 | /* |
| 2758 | * From the Intel SDM, volume 3: |
| 2759 | * Fields relevant to VM-entry event injection must be set properly. |
| 2760 | * These fields are the VM-entry interruption-information field, the |
| 2761 | * VM-entry exception error code, and the VM-entry instruction length. |
| 2762 | */ |
| 2763 | if (vmcs12->vm_entry_intr_info_field & INTR_INFO_VALID_MASK) { |
| 2764 | u32 intr_info = vmcs12->vm_entry_intr_info_field; |
| 2765 | u8 vector = intr_info & INTR_INFO_VECTOR_MASK; |
| 2766 | u32 intr_type = intr_info & INTR_INFO_INTR_TYPE_MASK; |
| 2767 | bool has_error_code = intr_info & INTR_INFO_DELIVER_CODE_MASK; |
| 2768 | bool should_have_error_code; |
| 2769 | bool urg = nested_cpu_has2(vmcs12, |
| 2770 | SECONDARY_EXEC_UNRESTRICTED_GUEST); |
| 2771 | bool prot_mode = !urg || vmcs12->guest_cr0 & X86_CR0_PE; |
| 2772 | |
| 2773 | /* VM-entry interruption-info field: interruption type */ |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 2774 | if (CC(intr_type == INTR_TYPE_RESERVED) || |
| 2775 | CC(intr_type == INTR_TYPE_OTHER_EVENT && |
| 2776 | !nested_cpu_supports_monitor_trap_flag(vcpu))) |
Krish Sadhukhan | 5fbf963 | 2018-12-12 13:30:10 -0500 | [diff] [blame] | 2777 | return -EINVAL; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2778 | |
| 2779 | /* VM-entry interruption-info field: vector */ |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 2780 | if (CC(intr_type == INTR_TYPE_NMI_INTR && vector != NMI_VECTOR) || |
| 2781 | CC(intr_type == INTR_TYPE_HARD_EXCEPTION && vector > 31) || |
| 2782 | CC(intr_type == INTR_TYPE_OTHER_EVENT && vector != 0)) |
Krish Sadhukhan | 5fbf963 | 2018-12-12 13:30:10 -0500 | [diff] [blame] | 2783 | return -EINVAL; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2784 | |
| 2785 | /* VM-entry interruption-info field: deliver error code */ |
| 2786 | should_have_error_code = |
| 2787 | intr_type == INTR_TYPE_HARD_EXCEPTION && prot_mode && |
| 2788 | x86_exception_has_error_code(vector); |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 2789 | if (CC(has_error_code != should_have_error_code)) |
Krish Sadhukhan | 5fbf963 | 2018-12-12 13:30:10 -0500 | [diff] [blame] | 2790 | return -EINVAL; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2791 | |
| 2792 | /* VM-entry exception error code */ |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 2793 | if (CC(has_error_code && |
Sean Christopherson | 567926c | 2019-10-01 09:21:23 -0700 | [diff] [blame] | 2794 | vmcs12->vm_entry_exception_error_code & GENMASK(31, 16))) |
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: reserved bits */ |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 2798 | if (CC(intr_info & INTR_INFO_RESVD_BITS_MASK)) |
Krish Sadhukhan | 5fbf963 | 2018-12-12 13:30:10 -0500 | [diff] [blame] | 2799 | return -EINVAL; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2800 | |
| 2801 | /* VM-entry instruction length */ |
| 2802 | switch (intr_type) { |
| 2803 | case INTR_TYPE_SOFT_EXCEPTION: |
| 2804 | case INTR_TYPE_SOFT_INTR: |
| 2805 | case INTR_TYPE_PRIV_SW_EXCEPTION: |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 2806 | if (CC(vmcs12->vm_entry_instruction_len > 15) || |
| 2807 | CC(vmcs12->vm_entry_instruction_len == 0 && |
| 2808 | CC(!nested_cpu_has_zero_length_injection(vcpu)))) |
Krish Sadhukhan | 5fbf963 | 2018-12-12 13:30:10 -0500 | [diff] [blame] | 2809 | return -EINVAL; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2810 | } |
| 2811 | } |
| 2812 | |
Krish Sadhukhan | 5fbf963 | 2018-12-12 13:30:10 -0500 | [diff] [blame] | 2813 | if (nested_vmx_check_entry_msr_switch_controls(vcpu, vmcs12)) |
| 2814 | return -EINVAL; |
| 2815 | |
| 2816 | return 0; |
| 2817 | } |
| 2818 | |
Sean Christopherson | 5478ba3 | 2019-04-11 12:18:06 -0700 | [diff] [blame] | 2819 | static int nested_vmx_check_controls(struct kvm_vcpu *vcpu, |
| 2820 | struct vmcs12 *vmcs12) |
| 2821 | { |
| 2822 | if (nested_check_vm_execution_controls(vcpu, vmcs12) || |
| 2823 | nested_check_vm_exit_controls(vcpu, vmcs12) || |
| 2824 | nested_check_vm_entry_controls(vcpu, vmcs12)) |
Paolo Bonzini | 98d9e85 | 2019-04-12 10:19:57 +0200 | [diff] [blame] | 2825 | return -EINVAL; |
Sean Christopherson | 5478ba3 | 2019-04-11 12:18:06 -0700 | [diff] [blame] | 2826 | |
Vitaly Kuznetsov | a835023 | 2020-02-05 13:30:34 +0100 | [diff] [blame] | 2827 | if (to_vmx(vcpu)->nested.enlightened_vmcs_enabled) |
| 2828 | return nested_evmcs_check_controls(vmcs12); |
| 2829 | |
Sean Christopherson | 5478ba3 | 2019-04-11 12:18:06 -0700 | [diff] [blame] | 2830 | return 0; |
| 2831 | } |
| 2832 | |
Paolo Bonzini | 98d9e85 | 2019-04-12 10:19:57 +0200 | [diff] [blame] | 2833 | static int nested_vmx_check_host_state(struct kvm_vcpu *vcpu, |
| 2834 | struct vmcs12 *vmcs12) |
Krish Sadhukhan | 5fbf963 | 2018-12-12 13:30:10 -0500 | [diff] [blame] | 2835 | { |
| 2836 | bool ia32e; |
| 2837 | |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 2838 | if (CC(!nested_host_cr0_valid(vcpu, vmcs12->host_cr0)) || |
| 2839 | CC(!nested_host_cr4_valid(vcpu, vmcs12->host_cr4)) || |
Sean Christopherson | 636e8b7 | 2021-02-03 16:01:10 -0800 | [diff] [blame] | 2840 | CC(kvm_vcpu_is_illegal_gpa(vcpu, vmcs12->host_cr3))) |
Krish Sadhukhan | 254b2f3 | 2018-12-12 13:30:11 -0500 | [diff] [blame] | 2841 | return -EINVAL; |
Krish Sadhukhan | 711eff3 | 2019-02-07 14:05:30 -0500 | [diff] [blame] | 2842 | |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 2843 | if (CC(is_noncanonical_address(vmcs12->host_ia32_sysenter_esp, vcpu)) || |
| 2844 | CC(is_noncanonical_address(vmcs12->host_ia32_sysenter_eip, vcpu))) |
Krish Sadhukhan | 711eff3 | 2019-02-07 14:05:30 -0500 | [diff] [blame] | 2845 | return -EINVAL; |
| 2846 | |
Krish Sadhukhan | f6b0db1f | 2019-04-08 17:35:11 -0400 | [diff] [blame] | 2847 | if ((vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PAT) && |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 2848 | CC(!kvm_pat_valid(vmcs12->host_ia32_pat))) |
Krish Sadhukhan | f6b0db1f | 2019-04-08 17:35:11 -0400 | [diff] [blame] | 2849 | return -EINVAL; |
| 2850 | |
Oliver Upton | c547cb6 | 2019-11-13 16:17:17 -0800 | [diff] [blame] | 2851 | if ((vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL) && |
| 2852 | CC(!kvm_valid_perf_global_ctrl(vcpu_to_pmu(vcpu), |
| 2853 | vmcs12->host_ia32_perf_global_ctrl))) |
| 2854 | return -EINVAL; |
| 2855 | |
Paolo Bonzini | fd3edd4 | 2019-09-25 18:33:53 +0200 | [diff] [blame] | 2856 | #ifdef CONFIG_X86_64 |
| 2857 | ia32e = !!(vcpu->arch.efer & EFER_LMA); |
| 2858 | #else |
| 2859 | ia32e = false; |
| 2860 | #endif |
| 2861 | |
| 2862 | if (ia32e) { |
| 2863 | if (CC(!(vmcs12->vm_exit_controls & VM_EXIT_HOST_ADDR_SPACE_SIZE)) || |
| 2864 | CC(!(vmcs12->host_cr4 & X86_CR4_PAE))) |
| 2865 | return -EINVAL; |
| 2866 | } else { |
| 2867 | if (CC(vmcs12->vm_exit_controls & VM_EXIT_HOST_ADDR_SPACE_SIZE) || |
| 2868 | CC(vmcs12->vm_entry_controls & VM_ENTRY_IA32E_MODE) || |
| 2869 | CC(vmcs12->host_cr4 & X86_CR4_PCIDE) || |
| 2870 | CC((vmcs12->host_rip) >> 32)) |
| 2871 | return -EINVAL; |
| 2872 | } |
Krish Sadhukhan | 1ef23e1 | 2019-07-03 19:54:35 -0400 | [diff] [blame] | 2873 | |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 2874 | if (CC(vmcs12->host_cs_selector & (SEGMENT_RPL_MASK | SEGMENT_TI_MASK)) || |
| 2875 | CC(vmcs12->host_ss_selector & (SEGMENT_RPL_MASK | SEGMENT_TI_MASK)) || |
| 2876 | CC(vmcs12->host_ds_selector & (SEGMENT_RPL_MASK | SEGMENT_TI_MASK)) || |
| 2877 | CC(vmcs12->host_es_selector & (SEGMENT_RPL_MASK | SEGMENT_TI_MASK)) || |
| 2878 | CC(vmcs12->host_fs_selector & (SEGMENT_RPL_MASK | SEGMENT_TI_MASK)) || |
| 2879 | CC(vmcs12->host_gs_selector & (SEGMENT_RPL_MASK | SEGMENT_TI_MASK)) || |
| 2880 | CC(vmcs12->host_tr_selector & (SEGMENT_RPL_MASK | SEGMENT_TI_MASK)) || |
| 2881 | CC(vmcs12->host_cs_selector == 0) || |
| 2882 | CC(vmcs12->host_tr_selector == 0) || |
| 2883 | CC(vmcs12->host_ss_selector == 0 && !ia32e)) |
Krish Sadhukhan | 1ef23e1 | 2019-07-03 19:54:35 -0400 | [diff] [blame] | 2884 | return -EINVAL; |
| 2885 | |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 2886 | if (CC(is_noncanonical_address(vmcs12->host_fs_base, vcpu)) || |
| 2887 | CC(is_noncanonical_address(vmcs12->host_gs_base, vcpu)) || |
| 2888 | CC(is_noncanonical_address(vmcs12->host_gdtr_base, vcpu)) || |
| 2889 | CC(is_noncanonical_address(vmcs12->host_idtr_base, vcpu)) || |
Paolo Bonzini | fd3edd4 | 2019-09-25 18:33:53 +0200 | [diff] [blame] | 2890 | CC(is_noncanonical_address(vmcs12->host_tr_base, vcpu)) || |
| 2891 | CC(is_noncanonical_address(vmcs12->host_rip, vcpu))) |
Krish Sadhukhan | 5845038 | 2019-08-09 12:26:19 -0700 | [diff] [blame] | 2892 | return -EINVAL; |
Krish Sadhukhan | 1ef23e1 | 2019-07-03 19:54:35 -0400 | [diff] [blame] | 2893 | |
Krish Sadhukhan | 5fbf963 | 2018-12-12 13:30:10 -0500 | [diff] [blame] | 2894 | /* |
| 2895 | * If the load IA32_EFER VM-exit control is 1, bits reserved in the |
| 2896 | * IA32_EFER MSR must be 0 in the field for that register. In addition, |
| 2897 | * the values of the LMA and LME bits in the field must each be that of |
| 2898 | * the host address-space size VM-exit control. |
| 2899 | */ |
| 2900 | if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_EFER) { |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 2901 | if (CC(!kvm_valid_efer(vcpu, vmcs12->host_ia32_efer)) || |
| 2902 | CC(ia32e != !!(vmcs12->host_ia32_efer & EFER_LMA)) || |
| 2903 | CC(ia32e != !!(vmcs12->host_ia32_efer & EFER_LME))) |
Krish Sadhukhan | 254b2f3 | 2018-12-12 13:30:11 -0500 | [diff] [blame] | 2904 | return -EINVAL; |
Krish Sadhukhan | 5fbf963 | 2018-12-12 13:30:10 -0500 | [diff] [blame] | 2905 | } |
| 2906 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2907 | return 0; |
| 2908 | } |
| 2909 | |
| 2910 | static int nested_vmx_check_vmcs_link_ptr(struct kvm_vcpu *vcpu, |
| 2911 | struct vmcs12 *vmcs12) |
| 2912 | { |
KarimAllah Ahmed | 8892530 | 2019-01-31 21:24:41 +0100 | [diff] [blame] | 2913 | int r = 0; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2914 | struct vmcs12 *shadow; |
KarimAllah Ahmed | 8892530 | 2019-01-31 21:24:41 +0100 | [diff] [blame] | 2915 | struct kvm_host_map map; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2916 | |
Yu Zhang | 64c7850 | 2021-09-30 01:51:53 +0800 | [diff] [blame] | 2917 | if (vmcs12->vmcs_link_pointer == INVALID_GPA) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2918 | return 0; |
| 2919 | |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 2920 | if (CC(!page_address_valid(vcpu, vmcs12->vmcs_link_pointer))) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2921 | return -EINVAL; |
| 2922 | |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 2923 | if (CC(kvm_vcpu_map(vcpu, gpa_to_gfn(vmcs12->vmcs_link_pointer), &map))) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2924 | return -EINVAL; |
| 2925 | |
KarimAllah Ahmed | 8892530 | 2019-01-31 21:24:41 +0100 | [diff] [blame] | 2926 | shadow = map.hva; |
| 2927 | |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 2928 | if (CC(shadow->hdr.revision_id != VMCS12_REVISION) || |
| 2929 | CC(shadow->hdr.shadow_vmcs != nested_cpu_has_shadow_vmcs(vmcs12))) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2930 | r = -EINVAL; |
KarimAllah Ahmed | 8892530 | 2019-01-31 21:24:41 +0100 | [diff] [blame] | 2931 | |
| 2932 | kvm_vcpu_unmap(vcpu, &map, false); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2933 | return r; |
| 2934 | } |
| 2935 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2936 | /* |
| 2937 | * Checks related to Guest Non-register State |
| 2938 | */ |
| 2939 | static int nested_check_guest_non_reg_state(struct vmcs12 *vmcs12) |
| 2940 | { |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 2941 | if (CC(vmcs12->guest_activity_state != GUEST_ACTIVITY_ACTIVE && |
Yadong Qi | bf0cd88 | 2020-11-06 14:51:22 +0800 | [diff] [blame] | 2942 | vmcs12->guest_activity_state != GUEST_ACTIVITY_HLT && |
| 2943 | vmcs12->guest_activity_state != GUEST_ACTIVITY_WAIT_SIPI)) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2944 | return -EINVAL; |
| 2945 | |
| 2946 | return 0; |
| 2947 | } |
| 2948 | |
Sean Christopherson | 5478ba3 | 2019-04-11 12:18:06 -0700 | [diff] [blame] | 2949 | static int nested_vmx_check_guest_state(struct kvm_vcpu *vcpu, |
| 2950 | struct vmcs12 *vmcs12, |
Sean Christopherson | 68cda40 | 2020-05-11 15:05:29 -0700 | [diff] [blame] | 2951 | enum vm_entry_failure_code *entry_failure_code) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2952 | { |
| 2953 | bool ia32e; |
| 2954 | |
Sean Christopherson | 68cda40 | 2020-05-11 15:05:29 -0700 | [diff] [blame] | 2955 | *entry_failure_code = ENTRY_FAIL_DEFAULT; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2956 | |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 2957 | if (CC(!nested_guest_cr0_valid(vcpu, vmcs12->guest_cr0)) || |
| 2958 | CC(!nested_guest_cr4_valid(vcpu, vmcs12->guest_cr4))) |
Sean Christopherson | c80add0 | 2019-04-11 12:18:09 -0700 | [diff] [blame] | 2959 | return -EINVAL; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2960 | |
Krish Sadhukhan | b91991b | 2020-01-15 19:54:32 -0500 | [diff] [blame] | 2961 | if ((vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS) && |
| 2962 | CC(!kvm_dr7_valid(vmcs12->guest_dr7))) |
| 2963 | return -EINVAL; |
| 2964 | |
Krish Sadhukhan | de2bc2b | 2019-04-08 17:35:12 -0400 | [diff] [blame] | 2965 | if ((vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_PAT) && |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 2966 | CC(!kvm_pat_valid(vmcs12->guest_ia32_pat))) |
Sean Christopherson | c80add0 | 2019-04-11 12:18:09 -0700 | [diff] [blame] | 2967 | return -EINVAL; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2968 | |
| 2969 | if (nested_vmx_check_vmcs_link_ptr(vcpu, vmcs12)) { |
Sean Christopherson | 68cda40 | 2020-05-11 15:05:29 -0700 | [diff] [blame] | 2970 | *entry_failure_code = ENTRY_FAIL_VMCS_LINK_PTR; |
Sean Christopherson | c80add0 | 2019-04-11 12:18:09 -0700 | [diff] [blame] | 2971 | return -EINVAL; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2972 | } |
| 2973 | |
Oliver Upton | bfc6ad6 | 2019-11-13 16:17:16 -0800 | [diff] [blame] | 2974 | if ((vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL) && |
| 2975 | CC(!kvm_valid_perf_global_ctrl(vcpu_to_pmu(vcpu), |
| 2976 | vmcs12->guest_ia32_perf_global_ctrl))) |
| 2977 | return -EINVAL; |
| 2978 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2979 | /* |
| 2980 | * If the load IA32_EFER VM-entry control is 1, the following checks |
| 2981 | * are performed on the field for the IA32_EFER MSR: |
| 2982 | * - Bits reserved in the IA32_EFER MSR must be 0. |
| 2983 | * - Bit 10 (corresponding to IA32_EFER.LMA) must equal the value of |
| 2984 | * the IA-32e mode guest VM-exit control. It must also be identical |
| 2985 | * to bit 8 (LME) if bit 31 in the CR0 field (corresponding to |
| 2986 | * CR0.PG) is 1. |
| 2987 | */ |
| 2988 | if (to_vmx(vcpu)->nested.nested_run_pending && |
| 2989 | (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_EFER)) { |
| 2990 | ia32e = (vmcs12->vm_entry_controls & VM_ENTRY_IA32E_MODE) != 0; |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 2991 | if (CC(!kvm_valid_efer(vcpu, vmcs12->guest_ia32_efer)) || |
| 2992 | CC(ia32e != !!(vmcs12->guest_ia32_efer & EFER_LMA)) || |
| 2993 | CC(((vmcs12->guest_cr0 & X86_CR0_PG) && |
| 2994 | ia32e != !!(vmcs12->guest_ia32_efer & EFER_LME)))) |
Sean Christopherson | c80add0 | 2019-04-11 12:18:09 -0700 | [diff] [blame] | 2995 | return -EINVAL; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2996 | } |
| 2997 | |
| 2998 | if ((vmcs12->vm_entry_controls & VM_ENTRY_LOAD_BNDCFGS) && |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 2999 | (CC(is_noncanonical_address(vmcs12->guest_bndcfgs & PAGE_MASK, vcpu)) || |
| 3000 | CC((vmcs12->guest_bndcfgs & MSR_IA32_BNDCFGS_RSVD)))) |
Sean Christopherson | c80add0 | 2019-04-11 12:18:09 -0700 | [diff] [blame] | 3001 | return -EINVAL; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3002 | |
Sean Christopherson | 9c3e922 | 2019-04-11 12:18:05 -0700 | [diff] [blame] | 3003 | if (nested_check_guest_non_reg_state(vmcs12)) |
Sean Christopherson | c80add0 | 2019-04-11 12:18:09 -0700 | [diff] [blame] | 3004 | return -EINVAL; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3005 | |
| 3006 | return 0; |
| 3007 | } |
| 3008 | |
Sean Christopherson | 453eafb | 2018-12-20 12:25:17 -0800 | [diff] [blame] | 3009 | static int nested_vmx_check_vmentry_hw(struct kvm_vcpu *vcpu) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3010 | { |
| 3011 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 3012 | unsigned long cr3, cr4; |
Sean Christopherson | f1727b4 | 2019-01-25 07:40:58 -0800 | [diff] [blame] | 3013 | bool vm_fail; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3014 | |
| 3015 | if (!nested_early_check) |
| 3016 | return 0; |
| 3017 | |
| 3018 | if (vmx->msr_autoload.host.nr) |
| 3019 | vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, 0); |
| 3020 | if (vmx->msr_autoload.guest.nr) |
| 3021 | vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, 0); |
| 3022 | |
| 3023 | preempt_disable(); |
| 3024 | |
| 3025 | vmx_prepare_switch_to_guest(vcpu); |
| 3026 | |
| 3027 | /* |
| 3028 | * Induce a consistency check VMExit by clearing bit 1 in GUEST_RFLAGS, |
| 3029 | * which is reserved to '1' by hardware. GUEST_RFLAGS is guaranteed to |
Miaohe Lin | 49f933d | 2020-02-27 11:20:54 +0800 | [diff] [blame] | 3030 | * be written (by prepare_vmcs02()) before the "real" VMEnter, i.e. |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3031 | * there is no need to preserve other bits or save/restore the field. |
| 3032 | */ |
| 3033 | vmcs_writel(GUEST_RFLAGS, 0); |
| 3034 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3035 | cr3 = __get_current_cr3_fast(); |
| 3036 | if (unlikely(cr3 != vmx->loaded_vmcs->host_state.cr3)) { |
| 3037 | vmcs_writel(HOST_CR3, cr3); |
| 3038 | vmx->loaded_vmcs->host_state.cr3 = cr3; |
| 3039 | } |
| 3040 | |
| 3041 | cr4 = cr4_read_shadow(); |
| 3042 | if (unlikely(cr4 != vmx->loaded_vmcs->host_state.cr4)) { |
| 3043 | vmcs_writel(HOST_CR4, cr4); |
| 3044 | vmx->loaded_vmcs->host_state.cr4 = cr4; |
| 3045 | } |
| 3046 | |
Uros Bizjak | 150f17b | 2020-12-30 16:26:57 -0800 | [diff] [blame] | 3047 | vm_fail = __vmx_vcpu_run(vmx, (unsigned long *)&vcpu->arch.regs, |
| 3048 | vmx->loaded_vmcs->launched); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3049 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3050 | if (vmx->msr_autoload.host.nr) |
| 3051 | vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, vmx->msr_autoload.host.nr); |
| 3052 | if (vmx->msr_autoload.guest.nr) |
| 3053 | vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, vmx->msr_autoload.guest.nr); |
| 3054 | |
Sean Christopherson | f1727b4 | 2019-01-25 07:40:58 -0800 | [diff] [blame] | 3055 | if (vm_fail) { |
Sean Christopherson | 380e005 | 2019-07-11 08:58:30 -0700 | [diff] [blame] | 3056 | u32 error = vmcs_read32(VM_INSTRUCTION_ERROR); |
| 3057 | |
Wanpeng Li | 541e886 | 2019-05-17 16:49:50 +0800 | [diff] [blame] | 3058 | preempt_enable(); |
Sean Christopherson | 380e005 | 2019-07-11 08:58:30 -0700 | [diff] [blame] | 3059 | |
| 3060 | trace_kvm_nested_vmenter_failed( |
| 3061 | "early hardware check VM-instruction error: ", error); |
| 3062 | WARN_ON_ONCE(error != VMXERR_ENTRY_INVALID_CONTROL_FIELD); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3063 | return 1; |
| 3064 | } |
| 3065 | |
| 3066 | /* |
| 3067 | * VMExit clears RFLAGS.IF and DR7, even on a consistency check. |
| 3068 | */ |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3069 | if (hw_breakpoint_active()) |
| 3070 | set_debugreg(__this_cpu_read(cpu_dr7), 7); |
Peter Zijlstra | 84b6a34 | 2020-05-29 23:27:36 +0200 | [diff] [blame] | 3071 | local_irq_enable(); |
Wanpeng Li | 541e886 | 2019-05-17 16:49:50 +0800 | [diff] [blame] | 3072 | preempt_enable(); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3073 | |
| 3074 | /* |
| 3075 | * A non-failing VMEntry means we somehow entered guest mode with |
| 3076 | * an illegal RIP, and that's just the tip of the iceberg. There |
| 3077 | * is no telling what memory has been modified or what state has |
| 3078 | * been exposed to unknown code. Hitting this all but guarantees |
| 3079 | * a (very critical) hardware issue. |
| 3080 | */ |
| 3081 | WARN_ON(!(vmcs_read32(VM_EXIT_REASON) & |
| 3082 | VMX_EXIT_REASONS_FAILED_VMENTRY)); |
| 3083 | |
| 3084 | return 0; |
| 3085 | } |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3086 | |
Paolo Bonzini | 9a78e15 | 2021-01-08 11:43:08 -0500 | [diff] [blame] | 3087 | static bool nested_get_evmcs_page(struct kvm_vcpu *vcpu) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3088 | { |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3089 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3090 | |
Vitaly Kuznetsov | e942dbf | 2020-03-09 16:52:12 +0100 | [diff] [blame] | 3091 | /* |
| 3092 | * hv_evmcs may end up being not mapped after migration (when |
| 3093 | * L2 was running), map it here to make sure vmcs12 changes are |
| 3094 | * properly reflected. |
| 3095 | */ |
Vitaly Kuznetsov | 1e9dfbd | 2021-05-26 15:20:16 +0200 | [diff] [blame] | 3096 | if (vmx->nested.enlightened_vmcs_enabled && |
Vitaly Kuznetsov | 2784996 | 2021-05-26 15:20:20 +0200 | [diff] [blame] | 3097 | vmx->nested.hv_evmcs_vmptr == EVMPTR_MAP_PENDING) { |
Vitaly Kuznetsov | b6a0653 | 2020-03-09 16:52:13 +0100 | [diff] [blame] | 3098 | enum nested_evmptrld_status evmptrld_status = |
| 3099 | nested_vmx_handle_enlightened_vmptrld(vcpu, false); |
| 3100 | |
| 3101 | if (evmptrld_status == EVMPTRLD_VMFAIL || |
Vitaly Kuznetsov | f5c7e84 | 2021-05-03 17:08:51 +0200 | [diff] [blame] | 3102 | evmptrld_status == EVMPTRLD_ERROR) |
Vitaly Kuznetsov | b6a0653 | 2020-03-09 16:52:13 +0100 | [diff] [blame] | 3103 | return false; |
Vitaly Kuznetsov | 8629b62 | 2021-05-26 15:20:25 +0200 | [diff] [blame] | 3104 | |
| 3105 | /* |
| 3106 | * Post migration VMCS12 always provides the most actual |
| 3107 | * information, copy it to eVMCS upon entry. |
| 3108 | */ |
| 3109 | vmx->nested.need_vmcs12_to_shadow_sync = true; |
Vitaly Kuznetsov | b6a0653 | 2020-03-09 16:52:13 +0100 | [diff] [blame] | 3110 | } |
Vitaly Kuznetsov | e942dbf | 2020-03-09 16:52:12 +0100 | [diff] [blame] | 3111 | |
Paolo Bonzini | 9a78e15 | 2021-01-08 11:43:08 -0500 | [diff] [blame] | 3112 | return true; |
| 3113 | } |
| 3114 | |
| 3115 | static bool nested_get_vmcs12_pages(struct kvm_vcpu *vcpu) |
| 3116 | { |
| 3117 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
| 3118 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 3119 | struct kvm_host_map *map; |
| 3120 | struct page *page; |
| 3121 | u64 hpa; |
| 3122 | |
Maxim Levitsky | 158a48e | 2021-06-07 12:02:03 +0300 | [diff] [blame] | 3123 | if (!vcpu->arch.pdptrs_from_userspace && |
| 3124 | !nested_cpu_has_ept(vmcs12) && is_pae_paging(vcpu)) { |
Maxim Levitsky | 0f85722 | 2021-06-07 12:02:00 +0300 | [diff] [blame] | 3125 | /* |
| 3126 | * Reload the guest's PDPTRs since after a migration |
| 3127 | * the guest CR3 might be restored prior to setting the nested |
| 3128 | * state which can lead to a load of wrong PDPTRs. |
| 3129 | */ |
| 3130 | if (CC(!load_pdptrs(vcpu, vcpu->arch.walk_mmu, vcpu->arch.cr3))) |
| 3131 | return false; |
| 3132 | } |
| 3133 | |
| 3134 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3135 | if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) { |
| 3136 | /* |
| 3137 | * Translate L1 physical address to host physical |
| 3138 | * address for vmcs02. Keep the page pinned, so this |
| 3139 | * physical address remains valid. We keep a reference |
| 3140 | * to it so we can release it later. |
| 3141 | */ |
| 3142 | if (vmx->nested.apic_access_page) { /* shouldn't happen */ |
Liran Alon | b11494b | 2019-11-21 00:31:47 +0200 | [diff] [blame] | 3143 | kvm_release_page_clean(vmx->nested.apic_access_page); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3144 | vmx->nested.apic_access_page = NULL; |
| 3145 | } |
| 3146 | page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->apic_access_addr); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3147 | if (!is_error_page(page)) { |
| 3148 | vmx->nested.apic_access_page = page; |
| 3149 | hpa = page_to_phys(vmx->nested.apic_access_page); |
| 3150 | vmcs_write64(APIC_ACCESS_ADDR, hpa); |
| 3151 | } else { |
Jim Mattson | 671ddc7 | 2019-10-15 10:44:05 -0700 | [diff] [blame] | 3152 | pr_debug_ratelimited("%s: no backing 'struct page' for APIC-access address in vmcs12\n", |
| 3153 | __func__); |
| 3154 | vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR; |
| 3155 | vcpu->run->internal.suberror = |
| 3156 | KVM_INTERNAL_ERROR_EMULATION; |
| 3157 | vcpu->run->internal.ndata = 0; |
| 3158 | return false; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3159 | } |
| 3160 | } |
| 3161 | |
| 3162 | if (nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW)) { |
KarimAllah Ahmed | 96c66e8 | 2019-01-31 21:24:37 +0100 | [diff] [blame] | 3163 | map = &vmx->nested.virtual_apic_map; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3164 | |
KarimAllah Ahmed | 96c66e8 | 2019-01-31 21:24:37 +0100 | [diff] [blame] | 3165 | if (!kvm_vcpu_map(vcpu, gpa_to_gfn(vmcs12->virtual_apic_page_addr), map)) { |
| 3166 | vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, pfn_to_hpa(map->pfn)); |
Paolo Bonzini | 6909081 | 2019-04-15 15:16:17 +0200 | [diff] [blame] | 3167 | } else if (nested_cpu_has(vmcs12, CPU_BASED_CR8_LOAD_EXITING) && |
| 3168 | nested_cpu_has(vmcs12, CPU_BASED_CR8_STORE_EXITING) && |
| 3169 | !nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) { |
| 3170 | /* |
| 3171 | * The processor will never use the TPR shadow, simply |
| 3172 | * clear the bit from the execution control. Such a |
| 3173 | * configuration is useless, but it happens in tests. |
| 3174 | * For any other configuration, failing the vm entry is |
| 3175 | * _not_ what the processor does but it's basically the |
| 3176 | * only possibility we have. |
| 3177 | */ |
Sean Christopherson | 2183f56 | 2019-05-07 12:17:56 -0700 | [diff] [blame] | 3178 | exec_controls_clearbit(vmx, CPU_BASED_TPR_SHADOW); |
Paolo Bonzini | 6909081 | 2019-04-15 15:16:17 +0200 | [diff] [blame] | 3179 | } else { |
Sean Christopherson | ca2f546 | 2019-05-07 09:06:33 -0700 | [diff] [blame] | 3180 | /* |
| 3181 | * Write an illegal value to VIRTUAL_APIC_PAGE_ADDR to |
| 3182 | * force VM-Entry to fail. |
| 3183 | */ |
Yu Zhang | 64c7850 | 2021-09-30 01:51:53 +0800 | [diff] [blame] | 3184 | vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, INVALID_GPA); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3185 | } |
| 3186 | } |
| 3187 | |
| 3188 | if (nested_cpu_has_posted_intr(vmcs12)) { |
KarimAllah Ahmed | 3278e04 | 2019-01-31 21:24:38 +0100 | [diff] [blame] | 3189 | map = &vmx->nested.pi_desc_map; |
| 3190 | |
| 3191 | if (!kvm_vcpu_map(vcpu, gpa_to_gfn(vmcs12->posted_intr_desc_addr), map)) { |
| 3192 | vmx->nested.pi_desc = |
| 3193 | (struct pi_desc *)(((void *)map->hva) + |
| 3194 | offset_in_page(vmcs12->posted_intr_desc_addr)); |
| 3195 | vmcs_write64(POSTED_INTR_DESC_ADDR, |
| 3196 | 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] | 3197 | } else { |
| 3198 | /* |
| 3199 | * Defer the KVM_INTERNAL_EXIT until KVM tries to |
| 3200 | * access the contents of the VMCS12 posted interrupt |
| 3201 | * descriptor. (Note that KVM may do this when it |
| 3202 | * should not, per the architectural specification.) |
| 3203 | */ |
| 3204 | vmx->nested.pi_desc = NULL; |
| 3205 | pin_controls_clearbit(vmx, PIN_BASED_POSTED_INTR); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3206 | } |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3207 | } |
| 3208 | if (nested_vmx_prepare_msr_bitmap(vcpu, vmcs12)) |
Sean Christopherson | 2183f56 | 2019-05-07 12:17:56 -0700 | [diff] [blame] | 3209 | exec_controls_setbit(vmx, CPU_BASED_USE_MSR_BITMAPS); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3210 | else |
Sean Christopherson | 2183f56 | 2019-05-07 12:17:56 -0700 | [diff] [blame] | 3211 | exec_controls_clearbit(vmx, CPU_BASED_USE_MSR_BITMAPS); |
Paolo Bonzini | 9a78e15 | 2021-01-08 11:43:08 -0500 | [diff] [blame] | 3212 | |
| 3213 | return true; |
| 3214 | } |
| 3215 | |
| 3216 | static bool vmx_get_nested_state_pages(struct kvm_vcpu *vcpu) |
| 3217 | { |
Vitaly Kuznetsov | f5c7e84 | 2021-05-03 17:08:51 +0200 | [diff] [blame] | 3218 | if (!nested_get_evmcs_page(vcpu)) { |
| 3219 | pr_debug_ratelimited("%s: enlightened vmptrld failed\n", |
| 3220 | __func__); |
| 3221 | vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR; |
| 3222 | vcpu->run->internal.suberror = |
| 3223 | KVM_INTERNAL_ERROR_EMULATION; |
| 3224 | vcpu->run->internal.ndata = 0; |
| 3225 | |
Paolo Bonzini | 9a78e15 | 2021-01-08 11:43:08 -0500 | [diff] [blame] | 3226 | return false; |
Vitaly Kuznetsov | f5c7e84 | 2021-05-03 17:08:51 +0200 | [diff] [blame] | 3227 | } |
Paolo Bonzini | 9a78e15 | 2021-01-08 11:43:08 -0500 | [diff] [blame] | 3228 | |
| 3229 | if (is_guest_mode(vcpu) && !nested_get_vmcs12_pages(vcpu)) |
| 3230 | return false; |
| 3231 | |
Jim Mattson | 671ddc7 | 2019-10-15 10:44:05 -0700 | [diff] [blame] | 3232 | return true; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3233 | } |
| 3234 | |
Sean Christopherson | 02f5fb2 | 2020-06-22 14:58:32 -0700 | [diff] [blame] | 3235 | static int nested_vmx_write_pml_buffer(struct kvm_vcpu *vcpu, gpa_t gpa) |
| 3236 | { |
| 3237 | struct vmcs12 *vmcs12; |
| 3238 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 3239 | gpa_t dst; |
| 3240 | |
| 3241 | if (WARN_ON_ONCE(!is_guest_mode(vcpu))) |
| 3242 | return 0; |
| 3243 | |
| 3244 | if (WARN_ON_ONCE(vmx->nested.pml_full)) |
| 3245 | return 1; |
| 3246 | |
| 3247 | /* |
| 3248 | * Check if PML is enabled for the nested guest. Whether eptp bit 6 is |
| 3249 | * set is already checked as part of A/D emulation. |
| 3250 | */ |
| 3251 | vmcs12 = get_vmcs12(vcpu); |
| 3252 | if (!nested_cpu_has_pml(vmcs12)) |
| 3253 | return 0; |
| 3254 | |
| 3255 | if (vmcs12->guest_pml_index >= PML_ENTITY_NUM) { |
| 3256 | vmx->nested.pml_full = true; |
| 3257 | return 1; |
| 3258 | } |
| 3259 | |
| 3260 | gpa &= ~0xFFFull; |
| 3261 | dst = vmcs12->pml_address + sizeof(u64) * vmcs12->guest_pml_index; |
| 3262 | |
| 3263 | if (kvm_write_guest_page(vcpu->kvm, gpa_to_gfn(dst), &gpa, |
| 3264 | offset_in_page(dst), sizeof(gpa))) |
| 3265 | return 0; |
| 3266 | |
| 3267 | vmcs12->guest_pml_index--; |
| 3268 | |
| 3269 | return 0; |
| 3270 | } |
| 3271 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3272 | /* |
| 3273 | * Intel's VMX Instruction Reference specifies a common set of prerequisites |
| 3274 | * for running VMX instructions (except VMXON, whose prerequisites are |
| 3275 | * slightly different). It also specifies what exception to inject otherwise. |
| 3276 | * Note that many of these exceptions have priority over VM exits, so they |
| 3277 | * don't have to be checked again here. |
| 3278 | */ |
| 3279 | static int nested_vmx_check_permission(struct kvm_vcpu *vcpu) |
| 3280 | { |
| 3281 | if (!to_vmx(vcpu)->nested.vmxon) { |
| 3282 | kvm_queue_exception(vcpu, UD_VECTOR); |
| 3283 | return 0; |
| 3284 | } |
| 3285 | |
| 3286 | if (vmx_get_cpl(vcpu)) { |
| 3287 | kvm_inject_gp(vcpu, 0); |
| 3288 | return 0; |
| 3289 | } |
| 3290 | |
| 3291 | return 1; |
| 3292 | } |
| 3293 | |
| 3294 | static u8 vmx_has_apicv_interrupt(struct kvm_vcpu *vcpu) |
| 3295 | { |
| 3296 | u8 rvi = vmx_get_rvi(); |
| 3297 | u8 vppr = kvm_lapic_get_reg(vcpu->arch.apic, APIC_PROCPRI); |
| 3298 | |
| 3299 | return ((rvi & 0xf0) > (vppr & 0xf0)); |
| 3300 | } |
| 3301 | |
| 3302 | static void load_vmcs12_host_state(struct kvm_vcpu *vcpu, |
| 3303 | struct vmcs12 *vmcs12); |
| 3304 | |
| 3305 | /* |
| 3306 | * If from_vmentry is false, this is being called from state restore (either RSM |
| 3307 | * or KVM_SET_NESTED_STATE). Otherwise it's called from vmlaunch/vmresume. |
Jim Mattson | 671ddc7 | 2019-10-15 10:44:05 -0700 | [diff] [blame] | 3308 | * |
| 3309 | * Returns: |
Miaohe Lin | 463bfee | 2020-02-14 10:44:05 +0800 | [diff] [blame] | 3310 | * NVMX_VMENTRY_SUCCESS: Entered VMX non-root mode |
| 3311 | * NVMX_VMENTRY_VMFAIL: Consistency check VMFail |
| 3312 | * NVMX_VMENTRY_VMEXIT: Consistency check VMExit |
| 3313 | * NVMX_VMENTRY_KVM_INTERNAL_ERROR: KVM internal error |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3314 | */ |
Jim Mattson | 671ddc7 | 2019-10-15 10:44:05 -0700 | [diff] [blame] | 3315 | enum nvmx_vmentry_status nested_vmx_enter_non_root_mode(struct kvm_vcpu *vcpu, |
| 3316 | bool from_vmentry) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3317 | { |
| 3318 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 3319 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
Sean Christopherson | 68cda40 | 2020-05-11 15:05:29 -0700 | [diff] [blame] | 3320 | enum vm_entry_failure_code entry_failure_code; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3321 | bool evaluate_pending_interrupts; |
Sean Christopherson | 8e53324 | 2020-11-06 17:03:12 +0800 | [diff] [blame] | 3322 | union vmx_exit_reason exit_reason = { |
| 3323 | .basic = EXIT_REASON_INVALID_STATE, |
| 3324 | .failed_vmentry = 1, |
| 3325 | }; |
| 3326 | u32 failed_index; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3327 | |
Sean Christopherson | eeeb4f6 | 2020-03-20 14:28:20 -0700 | [diff] [blame] | 3328 | if (kvm_check_request(KVM_REQ_TLB_FLUSH_CURRENT, vcpu)) |
| 3329 | kvm_vcpu_flush_tlb_current(vcpu); |
| 3330 | |
Sean Christopherson | 2183f56 | 2019-05-07 12:17:56 -0700 | [diff] [blame] | 3331 | evaluate_pending_interrupts = exec_controls_get(vmx) & |
Xiaoyao Li | 4e2a0bc | 2019-12-06 16:45:25 +0800 | [diff] [blame] | 3332 | (CPU_BASED_INTR_WINDOW_EXITING | CPU_BASED_NMI_WINDOW_EXITING); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3333 | if (likely(!evaluate_pending_interrupts) && kvm_vcpu_apicv_active(vcpu)) |
| 3334 | evaluate_pending_interrupts |= vmx_has_apicv_interrupt(vcpu); |
| 3335 | |
| 3336 | if (!(vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS)) |
| 3337 | vmx->nested.vmcs01_debugctl = vmcs_read64(GUEST_IA32_DEBUGCTL); |
| 3338 | if (kvm_mpx_supported() && |
| 3339 | !(vmcs12->vm_entry_controls & VM_ENTRY_LOAD_BNDCFGS)) |
| 3340 | vmx->nested.vmcs01_guest_bndcfgs = vmcs_read64(GUEST_BNDCFGS); |
| 3341 | |
Sean Christopherson | f087a02 | 2019-06-07 11:55:34 -0700 | [diff] [blame] | 3342 | /* |
| 3343 | * Overwrite vmcs01.GUEST_CR3 with L1's CR3 if EPT is disabled *and* |
| 3344 | * nested early checks are disabled. In the event of a "late" VM-Fail, |
| 3345 | * i.e. a VM-Fail detected by hardware but not KVM, KVM must unwind its |
| 3346 | * software model to the pre-VMEntry host state. When EPT is disabled, |
| 3347 | * GUEST_CR3 holds KVM's shadow CR3, not L1's "real" CR3, which causes |
| 3348 | * nested_vmx_restore_host_state() to corrupt vcpu->arch.cr3. Stuffing |
| 3349 | * vmcs01.GUEST_CR3 results in the unwind naturally setting arch.cr3 to |
| 3350 | * the correct value. Smashing vmcs01.GUEST_CR3 is safe because nested |
| 3351 | * VM-Exits, and the unwind, reset KVM's MMU, i.e. vmcs01.GUEST_CR3 is |
| 3352 | * guaranteed to be overwritten with a shadow CR3 prior to re-entering |
| 3353 | * L1. Don't stuff vmcs01.GUEST_CR3 when using nested early checks as |
| 3354 | * KVM modifies vcpu->arch.cr3 if and only if the early hardware checks |
| 3355 | * pass, and early VM-Fails do not reset KVM's MMU, i.e. the VM-Fail |
| 3356 | * path would need to manually save/restore vmcs01.GUEST_CR3. |
| 3357 | */ |
| 3358 | if (!enable_ept && !nested_early_check) |
| 3359 | vmcs_writel(GUEST_CR3, vcpu->arch.cr3); |
| 3360 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3361 | vmx_switch_vmcs(vcpu, &vmx->nested.vmcs02); |
| 3362 | |
Sean Christopherson | 389ab25 | 2021-08-10 10:19:50 -0700 | [diff] [blame] | 3363 | prepare_vmcs02_early(vmx, &vmx->vmcs01, vmcs12); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3364 | |
| 3365 | if (from_vmentry) { |
Sean Christopherson | b89d5ad | 2020-09-23 11:44:47 -0700 | [diff] [blame] | 3366 | if (unlikely(!nested_get_vmcs12_pages(vcpu))) { |
| 3367 | vmx_switch_vmcs(vcpu, &vmx->vmcs01); |
Jim Mattson | 671ddc7 | 2019-10-15 10:44:05 -0700 | [diff] [blame] | 3368 | return NVMX_VMENTRY_KVM_INTERNAL_ERROR; |
Sean Christopherson | b89d5ad | 2020-09-23 11:44:47 -0700 | [diff] [blame] | 3369 | } |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3370 | |
| 3371 | if (nested_vmx_check_vmentry_hw(vcpu)) { |
| 3372 | vmx_switch_vmcs(vcpu, &vmx->vmcs01); |
Jim Mattson | 671ddc7 | 2019-10-15 10:44:05 -0700 | [diff] [blame] | 3373 | return NVMX_VMENTRY_VMFAIL; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3374 | } |
| 3375 | |
Sean Christopherson | 68cda40 | 2020-05-11 15:05:29 -0700 | [diff] [blame] | 3376 | if (nested_vmx_check_guest_state(vcpu, vmcs12, |
| 3377 | &entry_failure_code)) { |
Sean Christopherson | 8e53324 | 2020-11-06 17:03:12 +0800 | [diff] [blame] | 3378 | exit_reason.basic = EXIT_REASON_INVALID_STATE; |
Sean Christopherson | 68cda40 | 2020-05-11 15:05:29 -0700 | [diff] [blame] | 3379 | vmcs12->exit_qualification = entry_failure_code; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3380 | goto vmentry_fail_vmexit; |
Sean Christopherson | 68cda40 | 2020-05-11 15:05:29 -0700 | [diff] [blame] | 3381 | } |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3382 | } |
| 3383 | |
| 3384 | enter_guest_mode(vcpu); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3385 | |
Maxim Levitsky | 0f85722 | 2021-06-07 12:02:00 +0300 | [diff] [blame] | 3386 | if (prepare_vmcs02(vcpu, vmcs12, from_vmentry, &entry_failure_code)) { |
Sean Christopherson | 8e53324 | 2020-11-06 17:03:12 +0800 | [diff] [blame] | 3387 | exit_reason.basic = EXIT_REASON_INVALID_STATE; |
Sean Christopherson | 68cda40 | 2020-05-11 15:05:29 -0700 | [diff] [blame] | 3388 | vmcs12->exit_qualification = entry_failure_code; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3389 | goto vmentry_fail_vmexit_guest_mode; |
Sean Christopherson | 68cda40 | 2020-05-11 15:05:29 -0700 | [diff] [blame] | 3390 | } |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3391 | |
| 3392 | if (from_vmentry) { |
Sean Christopherson | 68cda40 | 2020-05-11 15:05:29 -0700 | [diff] [blame] | 3393 | failed_index = nested_vmx_load_msr(vcpu, |
| 3394 | vmcs12->vm_entry_msr_load_addr, |
| 3395 | vmcs12->vm_entry_msr_load_count); |
| 3396 | if (failed_index) { |
Sean Christopherson | 8e53324 | 2020-11-06 17:03:12 +0800 | [diff] [blame] | 3397 | exit_reason.basic = EXIT_REASON_MSR_LOAD_FAIL; |
Sean Christopherson | 68cda40 | 2020-05-11 15:05:29 -0700 | [diff] [blame] | 3398 | vmcs12->exit_qualification = failed_index; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3399 | goto vmentry_fail_vmexit_guest_mode; |
Sean Christopherson | 68cda40 | 2020-05-11 15:05:29 -0700 | [diff] [blame] | 3400 | } |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3401 | } else { |
| 3402 | /* |
| 3403 | * The MMU is not initialized to point at the right entities yet and |
| 3404 | * "get pages" would need to read data from the guest (i.e. we will |
| 3405 | * need to perform gpa to hpa translation). Request a call |
| 3406 | * to nested_get_vmcs12_pages before the next VM-entry. The MSRs |
| 3407 | * have already been set at vmentry time and should not be reset. |
| 3408 | */ |
Paolo Bonzini | 729c15c | 2020-09-22 06:53:57 -0400 | [diff] [blame] | 3409 | kvm_make_request(KVM_REQ_GET_NESTED_STATE_PAGES, vcpu); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3410 | } |
| 3411 | |
| 3412 | /* |
| 3413 | * If L1 had a pending IRQ/NMI until it executed |
| 3414 | * VMLAUNCH/VMRESUME which wasn't delivered because it was |
| 3415 | * disallowed (e.g. interrupts disabled), L0 needs to |
| 3416 | * evaluate if this pending event should cause an exit from L2 |
| 3417 | * to L1 or delivered directly to L2 (e.g. In case L1 don't |
| 3418 | * intercept EXTERNAL_INTERRUPT). |
| 3419 | * |
| 3420 | * Usually this would be handled by the processor noticing an |
| 3421 | * IRQ/NMI window request, or checking RVI during evaluation of |
| 3422 | * pending virtual interrupts. However, this setting was done |
| 3423 | * on VMCS01 and now VMCS02 is active instead. Thus, we force L0 |
| 3424 | * to perform pending event evaluation by requesting a KVM_REQ_EVENT. |
| 3425 | */ |
| 3426 | if (unlikely(evaluate_pending_interrupts)) |
| 3427 | kvm_make_request(KVM_REQ_EVENT, vcpu); |
| 3428 | |
| 3429 | /* |
Paolo Bonzini | 359a6c3 | 2019-01-29 19:14:46 +0100 | [diff] [blame] | 3430 | * Do not start the preemption timer hrtimer until after we know |
| 3431 | * we are successful, so that only nested_vmx_vmexit needs to cancel |
| 3432 | * the timer. |
| 3433 | */ |
| 3434 | vmx->nested.preemption_timer_expired = false; |
Peter Shier | 850448f | 2020-05-26 14:51:06 -0700 | [diff] [blame] | 3435 | if (nested_cpu_has_preemption_timer(vmcs12)) { |
| 3436 | u64 timer_value = vmx_calc_preemption_timer_value(vcpu); |
| 3437 | vmx_start_preemption_timer(vcpu, timer_value); |
| 3438 | } |
Paolo Bonzini | 359a6c3 | 2019-01-29 19:14:46 +0100 | [diff] [blame] | 3439 | |
| 3440 | /* |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3441 | * Note no nested_vmx_succeed or nested_vmx_fail here. At this point |
| 3442 | * we are no longer running L1, and VMLAUNCH/VMRESUME has not yet |
| 3443 | * returned as far as L1 is concerned. It will only return (and set |
| 3444 | * the success flag) when L2 exits (see nested_vmx_vmexit()). |
| 3445 | */ |
Jim Mattson | 671ddc7 | 2019-10-15 10:44:05 -0700 | [diff] [blame] | 3446 | return NVMX_VMENTRY_SUCCESS; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3447 | |
| 3448 | /* |
| 3449 | * A failed consistency check that leads to a VMExit during L1's |
| 3450 | * VMEnter to L2 is a variation of a normal VMexit, as explained in |
| 3451 | * 26.7 "VM-entry failures during or after loading guest state". |
| 3452 | */ |
| 3453 | vmentry_fail_vmexit_guest_mode: |
Xiaoyao Li | 5e3d394 | 2019-12-06 16:45:26 +0800 | [diff] [blame] | 3454 | if (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETTING) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3455 | vcpu->arch.tsc_offset -= vmcs12->tsc_offset; |
| 3456 | leave_guest_mode(vcpu); |
| 3457 | |
| 3458 | vmentry_fail_vmexit: |
| 3459 | vmx_switch_vmcs(vcpu, &vmx->vmcs01); |
| 3460 | |
| 3461 | if (!from_vmentry) |
Jim Mattson | 671ddc7 | 2019-10-15 10:44:05 -0700 | [diff] [blame] | 3462 | return NVMX_VMENTRY_VMEXIT; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3463 | |
| 3464 | load_vmcs12_host_state(vcpu, vmcs12); |
Sean Christopherson | 8e53324 | 2020-11-06 17:03:12 +0800 | [diff] [blame] | 3465 | vmcs12->vm_exit_reason = exit_reason.full; |
Vitaly Kuznetsov | 1e9dfbd | 2021-05-26 15:20:16 +0200 | [diff] [blame] | 3466 | if (enable_shadow_vmcs || evmptr_is_valid(vmx->nested.hv_evmcs_vmptr)) |
Sean Christopherson | 3731905ef | 2019-05-07 08:36:27 -0700 | [diff] [blame] | 3467 | vmx->nested.need_vmcs12_to_shadow_sync = true; |
Jim Mattson | 671ddc7 | 2019-10-15 10:44:05 -0700 | [diff] [blame] | 3468 | return NVMX_VMENTRY_VMEXIT; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3469 | } |
| 3470 | |
| 3471 | /* |
| 3472 | * nested_vmx_run() handles a nested entry, i.e., a VMLAUNCH or VMRESUME on L1 |
| 3473 | * for running an L2 nested guest. |
| 3474 | */ |
| 3475 | static int nested_vmx_run(struct kvm_vcpu *vcpu, bool launch) |
| 3476 | { |
| 3477 | struct vmcs12 *vmcs12; |
Jim Mattson | 671ddc7 | 2019-10-15 10:44:05 -0700 | [diff] [blame] | 3478 | enum nvmx_vmentry_status status; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3479 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 3480 | u32 interrupt_shadow = vmx_get_interrupt_shadow(vcpu); |
Vitaly Kuznetsov | b6a0653 | 2020-03-09 16:52:13 +0100 | [diff] [blame] | 3481 | enum nested_evmptrld_status evmptrld_status; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3482 | |
| 3483 | if (!nested_vmx_check_permission(vcpu)) |
| 3484 | return 1; |
| 3485 | |
Vitaly Kuznetsov | b6a0653 | 2020-03-09 16:52:13 +0100 | [diff] [blame] | 3486 | evmptrld_status = nested_vmx_handle_enlightened_vmptrld(vcpu, launch); |
| 3487 | if (evmptrld_status == EVMPTRLD_ERROR) { |
| 3488 | kvm_queue_exception(vcpu, UD_VECTOR); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3489 | return 1; |
Sean Christopherson | fc595f3 | 2020-08-12 11:06:15 -0700 | [diff] [blame] | 3490 | } else if (CC(evmptrld_status == EVMPTRLD_VMFAIL)) { |
Vitaly Kuznetsov | b6a0653 | 2020-03-09 16:52:13 +0100 | [diff] [blame] | 3491 | return nested_vmx_failInvalid(vcpu); |
| 3492 | } |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3493 | |
Vitaly Kuznetsov | 1e9dfbd | 2021-05-26 15:20:16 +0200 | [diff] [blame] | 3494 | if (CC(!evmptr_is_valid(vmx->nested.hv_evmcs_vmptr) && |
Yu Zhang | 64c7850 | 2021-09-30 01:51:53 +0800 | [diff] [blame] | 3495 | vmx->nested.current_vmptr == INVALID_GPA)) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3496 | return nested_vmx_failInvalid(vcpu); |
| 3497 | |
| 3498 | vmcs12 = get_vmcs12(vcpu); |
| 3499 | |
| 3500 | /* |
| 3501 | * Can't VMLAUNCH or VMRESUME a shadow VMCS. Despite the fact |
| 3502 | * that there *is* a valid VMCS pointer, RFLAGS.CF is set |
| 3503 | * rather than RFLAGS.ZF, and no error number is stored to the |
| 3504 | * VM-instruction error field. |
| 3505 | */ |
Sean Christopherson | fc595f3 | 2020-08-12 11:06:15 -0700 | [diff] [blame] | 3506 | if (CC(vmcs12->hdr.shadow_vmcs)) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3507 | return nested_vmx_failInvalid(vcpu); |
| 3508 | |
Vitaly Kuznetsov | 1e9dfbd | 2021-05-26 15:20:16 +0200 | [diff] [blame] | 3509 | if (evmptr_is_valid(vmx->nested.hv_evmcs_vmptr)) { |
Vitaly Kuznetsov | d6bf71a | 2021-05-26 15:20:22 +0200 | [diff] [blame] | 3510 | copy_enlightened_to_vmcs12(vmx, vmx->nested.hv_evmcs->hv_clean_fields); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3511 | /* Enlightened VMCS doesn't have launch state */ |
| 3512 | vmcs12->launch_state = !launch; |
| 3513 | } else if (enable_shadow_vmcs) { |
| 3514 | copy_shadow_to_vmcs12(vmx); |
| 3515 | } |
| 3516 | |
| 3517 | /* |
| 3518 | * The nested entry process starts with enforcing various prerequisites |
| 3519 | * on vmcs12 as required by the Intel SDM, and act appropriately when |
| 3520 | * they fail: As the SDM explains, some conditions should cause the |
| 3521 | * instruction to fail, while others will cause the instruction to seem |
| 3522 | * to succeed, but return an EXIT_REASON_INVALID_STATE. |
| 3523 | * To speed up the normal (success) code path, we should avoid checking |
| 3524 | * for misconfigurations which will anyway be caught by the processor |
| 3525 | * when using the merged vmcs02. |
| 3526 | */ |
Sean Christopherson | fc595f3 | 2020-08-12 11:06:15 -0700 | [diff] [blame] | 3527 | if (CC(interrupt_shadow & KVM_X86_SHADOW_INT_MOV_SS)) |
Sean Christopherson | b2656e4 | 2020-06-08 18:56:07 -0700 | [diff] [blame] | 3528 | return nested_vmx_fail(vcpu, VMXERR_ENTRY_EVENTS_BLOCKED_BY_MOV_SS); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3529 | |
Sean Christopherson | fc595f3 | 2020-08-12 11:06:15 -0700 | [diff] [blame] | 3530 | if (CC(vmcs12->launch_state == launch)) |
Sean Christopherson | b2656e4 | 2020-06-08 18:56:07 -0700 | [diff] [blame] | 3531 | return nested_vmx_fail(vcpu, |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3532 | launch ? VMXERR_VMLAUNCH_NONCLEAR_VMCS |
| 3533 | : VMXERR_VMRESUME_NONLAUNCHED_VMCS); |
| 3534 | |
Paolo Bonzini | 98d9e85 | 2019-04-12 10:19:57 +0200 | [diff] [blame] | 3535 | if (nested_vmx_check_controls(vcpu, vmcs12)) |
Sean Christopherson | b2656e4 | 2020-06-08 18:56:07 -0700 | [diff] [blame] | 3536 | return nested_vmx_fail(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD); |
Sean Christopherson | 5478ba3 | 2019-04-11 12:18:06 -0700 | [diff] [blame] | 3537 | |
Paolo Bonzini | 98d9e85 | 2019-04-12 10:19:57 +0200 | [diff] [blame] | 3538 | if (nested_vmx_check_host_state(vcpu, vmcs12)) |
Sean Christopherson | b2656e4 | 2020-06-08 18:56:07 -0700 | [diff] [blame] | 3539 | return nested_vmx_fail(vcpu, VMXERR_ENTRY_INVALID_HOST_STATE_FIELD); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3540 | |
| 3541 | /* |
| 3542 | * We're finally done with prerequisite checking, and can start with |
| 3543 | * the nested entry. |
| 3544 | */ |
| 3545 | vmx->nested.nested_run_pending = 1; |
Peter Shier | 850448f | 2020-05-26 14:51:06 -0700 | [diff] [blame] | 3546 | vmx->nested.has_preemption_timer_deadline = false; |
Jim Mattson | 671ddc7 | 2019-10-15 10:44:05 -0700 | [diff] [blame] | 3547 | status = nested_vmx_enter_non_root_mode(vcpu, true); |
| 3548 | if (unlikely(status != NVMX_VMENTRY_SUCCESS)) |
| 3549 | goto vmentry_failed; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3550 | |
Sean Christopherson | 25bb2cf | 2020-08-12 10:51:29 -0700 | [diff] [blame] | 3551 | /* Emulate processing of posted interrupts on VM-Enter. */ |
| 3552 | if (nested_cpu_has_posted_intr(vmcs12) && |
| 3553 | kvm_apic_has_interrupt(vcpu) == vmx->nested.posted_intr_nv) { |
| 3554 | vmx->nested.pi_pending = true; |
| 3555 | kvm_make_request(KVM_REQ_EVENT, vcpu); |
| 3556 | kvm_apic_clear_irr(vcpu, vmx->nested.posted_intr_nv); |
| 3557 | } |
| 3558 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3559 | /* Hide L1D cache contents from the nested guest. */ |
| 3560 | vmx->vcpu.arch.l1tf_flush_l1d = true; |
| 3561 | |
| 3562 | /* |
| 3563 | * Must happen outside of nested_vmx_enter_non_root_mode() as it will |
| 3564 | * also be used as part of restoring nVMX state for |
| 3565 | * snapshot restore (migration). |
| 3566 | * |
| 3567 | * In this flow, it is assumed that vmcs12 cache was |
Ingo Molnar | 163b099 | 2021-03-21 22:28:53 +0100 | [diff] [blame] | 3568 | * transferred as part of captured nVMX state and should |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3569 | * therefore not be read from guest memory (which may not |
| 3570 | * exist on destination host yet). |
| 3571 | */ |
| 3572 | nested_cache_shadow_vmcs12(vcpu, vmcs12); |
| 3573 | |
Yadong Qi | bf0cd88 | 2020-11-06 14:51:22 +0800 | [diff] [blame] | 3574 | switch (vmcs12->guest_activity_state) { |
| 3575 | case GUEST_ACTIVITY_HLT: |
| 3576 | /* |
| 3577 | * If we're entering a halted L2 vcpu and the L2 vcpu won't be |
| 3578 | * awakened by event injection or by an NMI-window VM-exit or |
| 3579 | * by an interrupt-window VM-exit, halt the vcpu. |
| 3580 | */ |
| 3581 | if (!(vmcs12->vm_entry_intr_info_field & INTR_INFO_VALID_MASK) && |
| 3582 | !nested_cpu_has(vmcs12, CPU_BASED_NMI_WINDOW_EXITING) && |
| 3583 | !(nested_cpu_has(vmcs12, CPU_BASED_INTR_WINDOW_EXITING) && |
| 3584 | (vmcs12->guest_rflags & X86_EFLAGS_IF))) { |
| 3585 | vmx->nested.nested_run_pending = 0; |
| 3586 | return kvm_vcpu_halt(vcpu); |
| 3587 | } |
| 3588 | break; |
| 3589 | case GUEST_ACTIVITY_WAIT_SIPI: |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3590 | vmx->nested.nested_run_pending = 0; |
Yadong Qi | bf0cd88 | 2020-11-06 14:51:22 +0800 | [diff] [blame] | 3591 | vcpu->arch.mp_state = KVM_MP_STATE_INIT_RECEIVED; |
| 3592 | break; |
| 3593 | default: |
| 3594 | break; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3595 | } |
Yadong Qi | bf0cd88 | 2020-11-06 14:51:22 +0800 | [diff] [blame] | 3596 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3597 | return 1; |
Jim Mattson | 671ddc7 | 2019-10-15 10:44:05 -0700 | [diff] [blame] | 3598 | |
| 3599 | vmentry_failed: |
| 3600 | vmx->nested.nested_run_pending = 0; |
| 3601 | if (status == NVMX_VMENTRY_KVM_INTERNAL_ERROR) |
| 3602 | return 0; |
| 3603 | if (status == NVMX_VMENTRY_VMEXIT) |
| 3604 | return 1; |
| 3605 | WARN_ON_ONCE(status != NVMX_VMENTRY_VMFAIL); |
Sean Christopherson | b2656e4 | 2020-06-08 18:56:07 -0700 | [diff] [blame] | 3606 | return nested_vmx_fail(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3607 | } |
| 3608 | |
| 3609 | /* |
| 3610 | * 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] | 3611 | * 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] | 3612 | * This function returns the new value we should put in vmcs12.guest_cr0. |
| 3613 | * It's not enough to just return the vmcs02 GUEST_CR0. Rather, |
| 3614 | * 1. Bits that neither L0 nor L1 trapped, were set directly by L2 and are now |
| 3615 | * available in vmcs02 GUEST_CR0. (Note: It's enough to check that L0 |
| 3616 | * didn't trap the bit, because if L1 did, so would L0). |
| 3617 | * 2. Bits that L1 asked to trap (and therefore L0 also did) could not have |
| 3618 | * been modified by L2, and L1 knows it. So just leave the old value of |
| 3619 | * the bit from vmcs12.guest_cr0. Note that the bit from vmcs02 GUEST_CR0 |
| 3620 | * isn't relevant, because if L0 traps this bit it can set it to anything. |
| 3621 | * 3. Bits that L1 didn't trap, but L0 did. L1 believes the guest could have |
| 3622 | * changed these bits, and therefore they need to be updated, but L0 |
| 3623 | * didn't necessarily allow them to be changed in GUEST_CR0 - and rather |
| 3624 | * put them in vmcs02 CR0_READ_SHADOW. So take these bits from there. |
| 3625 | */ |
| 3626 | static inline unsigned long |
| 3627 | vmcs12_guest_cr0(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12) |
| 3628 | { |
| 3629 | return |
| 3630 | /*1*/ (vmcs_readl(GUEST_CR0) & vcpu->arch.cr0_guest_owned_bits) | |
| 3631 | /*2*/ (vmcs12->guest_cr0 & vmcs12->cr0_guest_host_mask) | |
| 3632 | /*3*/ (vmcs_readl(CR0_READ_SHADOW) & ~(vmcs12->cr0_guest_host_mask | |
| 3633 | vcpu->arch.cr0_guest_owned_bits)); |
| 3634 | } |
| 3635 | |
| 3636 | static inline unsigned long |
| 3637 | vmcs12_guest_cr4(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12) |
| 3638 | { |
| 3639 | return |
| 3640 | /*1*/ (vmcs_readl(GUEST_CR4) & vcpu->arch.cr4_guest_owned_bits) | |
| 3641 | /*2*/ (vmcs12->guest_cr4 & vmcs12->cr4_guest_host_mask) | |
| 3642 | /*3*/ (vmcs_readl(CR4_READ_SHADOW) & ~(vmcs12->cr4_guest_host_mask | |
| 3643 | vcpu->arch.cr4_guest_owned_bits)); |
| 3644 | } |
| 3645 | |
| 3646 | static void vmcs12_save_pending_event(struct kvm_vcpu *vcpu, |
| 3647 | struct vmcs12 *vmcs12) |
| 3648 | { |
| 3649 | u32 idt_vectoring; |
| 3650 | unsigned int nr; |
| 3651 | |
| 3652 | if (vcpu->arch.exception.injected) { |
| 3653 | nr = vcpu->arch.exception.nr; |
| 3654 | idt_vectoring = nr | VECTORING_INFO_VALID_MASK; |
| 3655 | |
| 3656 | if (kvm_exception_is_soft(nr)) { |
| 3657 | vmcs12->vm_exit_instruction_len = |
| 3658 | vcpu->arch.event_exit_inst_len; |
| 3659 | idt_vectoring |= INTR_TYPE_SOFT_EXCEPTION; |
| 3660 | } else |
| 3661 | idt_vectoring |= INTR_TYPE_HARD_EXCEPTION; |
| 3662 | |
| 3663 | if (vcpu->arch.exception.has_error_code) { |
| 3664 | idt_vectoring |= VECTORING_INFO_DELIVER_CODE_MASK; |
| 3665 | vmcs12->idt_vectoring_error_code = |
| 3666 | vcpu->arch.exception.error_code; |
| 3667 | } |
| 3668 | |
| 3669 | vmcs12->idt_vectoring_info_field = idt_vectoring; |
| 3670 | } else if (vcpu->arch.nmi_injected) { |
| 3671 | vmcs12->idt_vectoring_info_field = |
| 3672 | INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK | NMI_VECTOR; |
| 3673 | } else if (vcpu->arch.interrupt.injected) { |
| 3674 | nr = vcpu->arch.interrupt.nr; |
| 3675 | idt_vectoring = nr | VECTORING_INFO_VALID_MASK; |
| 3676 | |
| 3677 | if (vcpu->arch.interrupt.soft) { |
| 3678 | idt_vectoring |= INTR_TYPE_SOFT_INTR; |
| 3679 | vmcs12->vm_entry_instruction_len = |
| 3680 | vcpu->arch.event_exit_inst_len; |
| 3681 | } else |
| 3682 | idt_vectoring |= INTR_TYPE_EXT_INTR; |
| 3683 | |
| 3684 | vmcs12->idt_vectoring_info_field = idt_vectoring; |
| 3685 | } |
| 3686 | } |
| 3687 | |
| 3688 | |
Paolo Bonzini | 96b100c | 2020-03-17 18:32:50 +0100 | [diff] [blame] | 3689 | void nested_mark_vmcs12_pages_dirty(struct kvm_vcpu *vcpu) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3690 | { |
| 3691 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
| 3692 | gfn_t gfn; |
| 3693 | |
| 3694 | /* |
| 3695 | * Don't need to mark the APIC access page dirty; it is never |
| 3696 | * written to by the CPU during APIC virtualization. |
| 3697 | */ |
| 3698 | |
| 3699 | if (nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW)) { |
| 3700 | gfn = vmcs12->virtual_apic_page_addr >> PAGE_SHIFT; |
| 3701 | kvm_vcpu_mark_page_dirty(vcpu, gfn); |
| 3702 | } |
| 3703 | |
| 3704 | if (nested_cpu_has_posted_intr(vmcs12)) { |
| 3705 | gfn = vmcs12->posted_intr_desc_addr >> PAGE_SHIFT; |
| 3706 | kvm_vcpu_mark_page_dirty(vcpu, gfn); |
| 3707 | } |
| 3708 | } |
| 3709 | |
Jim Mattson | 650293c | 2021-06-04 10:26:02 -0700 | [diff] [blame] | 3710 | static int vmx_complete_nested_posted_interrupt(struct kvm_vcpu *vcpu) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3711 | { |
| 3712 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 3713 | int max_irr; |
| 3714 | void *vapic_page; |
| 3715 | u16 status; |
| 3716 | |
Jim Mattson | 966eefb | 2021-06-04 10:26:06 -0700 | [diff] [blame] | 3717 | if (!vmx->nested.pi_pending) |
Jim Mattson | 650293c | 2021-06-04 10:26:02 -0700 | [diff] [blame] | 3718 | return 0; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3719 | |
Jim Mattson | 966eefb | 2021-06-04 10:26:06 -0700 | [diff] [blame] | 3720 | if (!vmx->nested.pi_desc) |
| 3721 | goto mmio_needed; |
| 3722 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3723 | vmx->nested.pi_pending = false; |
Jim Mattson | 966eefb | 2021-06-04 10:26:06 -0700 | [diff] [blame] | 3724 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3725 | if (!pi_test_and_clear_on(vmx->nested.pi_desc)) |
Jim Mattson | 650293c | 2021-06-04 10:26:02 -0700 | [diff] [blame] | 3726 | return 0; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3727 | |
| 3728 | max_irr = find_last_bit((unsigned long *)vmx->nested.pi_desc->pir, 256); |
| 3729 | if (max_irr != 256) { |
KarimAllah Ahmed | 96c66e8 | 2019-01-31 21:24:37 +0100 | [diff] [blame] | 3730 | vapic_page = vmx->nested.virtual_apic_map.hva; |
| 3731 | if (!vapic_page) |
Jim Mattson | 0fe998b | 2021-06-04 10:26:05 -0700 | [diff] [blame] | 3732 | goto mmio_needed; |
KarimAllah Ahmed | 96c66e8 | 2019-01-31 21:24:37 +0100 | [diff] [blame] | 3733 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3734 | __kvm_apic_update_irr(vmx->nested.pi_desc->pir, |
| 3735 | vapic_page, &max_irr); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3736 | status = vmcs_read16(GUEST_INTR_STATUS); |
| 3737 | if ((u8)max_irr > ((u8)status & 0xff)) { |
| 3738 | status &= ~0xff; |
| 3739 | status |= (u8)max_irr; |
| 3740 | vmcs_write16(GUEST_INTR_STATUS, status); |
| 3741 | } |
| 3742 | } |
| 3743 | |
| 3744 | nested_mark_vmcs12_pages_dirty(vcpu); |
Jim Mattson | 650293c | 2021-06-04 10:26:02 -0700 | [diff] [blame] | 3745 | return 0; |
Jim Mattson | 0fe998b | 2021-06-04 10:26:05 -0700 | [diff] [blame] | 3746 | |
| 3747 | mmio_needed: |
| 3748 | kvm_handle_memory_failure(vcpu, X86EMUL_IO_NEEDED, NULL); |
| 3749 | return -ENXIO; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3750 | } |
| 3751 | |
| 3752 | static void nested_vmx_inject_exception_vmexit(struct kvm_vcpu *vcpu, |
| 3753 | unsigned long exit_qual) |
| 3754 | { |
| 3755 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
| 3756 | unsigned int nr = vcpu->arch.exception.nr; |
| 3757 | u32 intr_info = nr | INTR_INFO_VALID_MASK; |
| 3758 | |
| 3759 | if (vcpu->arch.exception.has_error_code) { |
| 3760 | vmcs12->vm_exit_intr_error_code = vcpu->arch.exception.error_code; |
| 3761 | intr_info |= INTR_INFO_DELIVER_CODE_MASK; |
| 3762 | } |
| 3763 | |
| 3764 | if (kvm_exception_is_soft(nr)) |
| 3765 | intr_info |= INTR_TYPE_SOFT_EXCEPTION; |
| 3766 | else |
| 3767 | intr_info |= INTR_TYPE_HARD_EXCEPTION; |
| 3768 | |
| 3769 | if (!(vmcs12->idt_vectoring_info_field & VECTORING_INFO_VALID_MASK) && |
| 3770 | vmx_get_nmi_mask(vcpu)) |
| 3771 | intr_info |= INTR_INFO_UNBLOCK_NMI; |
| 3772 | |
| 3773 | nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI, intr_info, exit_qual); |
| 3774 | } |
| 3775 | |
Oliver Upton | 684c042 | 2020-02-07 02:36:05 -0800 | [diff] [blame] | 3776 | /* |
| 3777 | * Returns true if a debug trap is pending delivery. |
| 3778 | * |
| 3779 | * In KVM, debug traps bear an exception payload. As such, the class of a #DB |
| 3780 | * exception may be inferred from the presence of an exception payload. |
| 3781 | */ |
| 3782 | static inline bool vmx_pending_dbg_trap(struct kvm_vcpu *vcpu) |
| 3783 | { |
| 3784 | return vcpu->arch.exception.pending && |
| 3785 | vcpu->arch.exception.nr == DB_VECTOR && |
| 3786 | vcpu->arch.exception.payload; |
| 3787 | } |
| 3788 | |
| 3789 | /* |
| 3790 | * Certain VM-exits set the 'pending debug exceptions' field to indicate a |
| 3791 | * recognized #DB (data or single-step) that has yet to be delivered. Since KVM |
| 3792 | * represents these debug traps with a payload that is said to be compatible |
| 3793 | * with the 'pending debug exceptions' field, write the payload to the VMCS |
| 3794 | * field if a VM-exit is delivered before the debug trap. |
| 3795 | */ |
| 3796 | static void nested_vmx_update_pending_dbg(struct kvm_vcpu *vcpu) |
| 3797 | { |
| 3798 | if (vmx_pending_dbg_trap(vcpu)) |
| 3799 | vmcs_writel(GUEST_PENDING_DBG_EXCEPTIONS, |
| 3800 | vcpu->arch.exception.payload); |
| 3801 | } |
| 3802 | |
Sean Christopherson | d2060bd | 2020-04-22 19:25:39 -0700 | [diff] [blame] | 3803 | static bool nested_vmx_preemption_timer_pending(struct kvm_vcpu *vcpu) |
| 3804 | { |
| 3805 | return nested_cpu_has_preemption_timer(get_vmcs12(vcpu)) && |
| 3806 | to_vmx(vcpu)->nested.preemption_timer_expired; |
| 3807 | } |
| 3808 | |
Sean Christopherson | a1c77ab | 2020-03-02 22:27:35 -0800 | [diff] [blame] | 3809 | static int vmx_check_nested_events(struct kvm_vcpu *vcpu) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3810 | { |
| 3811 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 3812 | unsigned long exit_qual; |
| 3813 | bool block_nested_events = |
| 3814 | vmx->nested.nested_run_pending || kvm_event_needs_reinjection(vcpu); |
Oliver Upton | 5ef8acb | 2020-02-07 02:36:07 -0800 | [diff] [blame] | 3815 | bool mtf_pending = vmx->nested.mtf_pending; |
Liran Alon | 4b9852f | 2019-08-26 13:24:49 +0300 | [diff] [blame] | 3816 | struct kvm_lapic *apic = vcpu->arch.apic; |
| 3817 | |
Oliver Upton | 5ef8acb | 2020-02-07 02:36:07 -0800 | [diff] [blame] | 3818 | /* |
| 3819 | * Clear the MTF state. If a higher priority VM-exit is delivered first, |
| 3820 | * this state is discarded. |
| 3821 | */ |
Oliver Upton | 5c8beb4 | 2020-04-06 20:12:37 +0000 | [diff] [blame] | 3822 | if (!block_nested_events) |
| 3823 | vmx->nested.mtf_pending = false; |
Oliver Upton | 5ef8acb | 2020-02-07 02:36:07 -0800 | [diff] [blame] | 3824 | |
Liran Alon | 4b9852f | 2019-08-26 13:24:49 +0300 | [diff] [blame] | 3825 | if (lapic_in_kernel(vcpu) && |
| 3826 | test_bit(KVM_APIC_INIT, &apic->pending_events)) { |
| 3827 | if (block_nested_events) |
| 3828 | return -EBUSY; |
Oliver Upton | 684c042 | 2020-02-07 02:36:05 -0800 | [diff] [blame] | 3829 | nested_vmx_update_pending_dbg(vcpu); |
Liran Alon | e64a850 | 2019-11-11 14:16:05 +0200 | [diff] [blame] | 3830 | clear_bit(KVM_APIC_INIT, &apic->pending_events); |
Yadong Qi | bf0cd88 | 2020-11-06 14:51:22 +0800 | [diff] [blame] | 3831 | if (vcpu->arch.mp_state != KVM_MP_STATE_INIT_RECEIVED) |
| 3832 | nested_vmx_vmexit(vcpu, EXIT_REASON_INIT_SIGNAL, 0, 0); |
| 3833 | return 0; |
| 3834 | } |
| 3835 | |
| 3836 | if (lapic_in_kernel(vcpu) && |
| 3837 | test_bit(KVM_APIC_SIPI, &apic->pending_events)) { |
| 3838 | if (block_nested_events) |
| 3839 | return -EBUSY; |
| 3840 | |
| 3841 | clear_bit(KVM_APIC_SIPI, &apic->pending_events); |
| 3842 | if (vcpu->arch.mp_state == KVM_MP_STATE_INIT_RECEIVED) |
| 3843 | nested_vmx_vmexit(vcpu, EXIT_REASON_SIPI_SIGNAL, 0, |
| 3844 | apic->sipi_vector & 0xFFUL); |
Liran Alon | 4b9852f | 2019-08-26 13:24:49 +0300 | [diff] [blame] | 3845 | return 0; |
| 3846 | } |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3847 | |
Oliver Upton | 5ef8acb | 2020-02-07 02:36:07 -0800 | [diff] [blame] | 3848 | /* |
| 3849 | * Process any exceptions that are not debug traps before MTF. |
Maxim Levitsky | 4020da3 | 2021-04-01 17:38:14 +0300 | [diff] [blame] | 3850 | * |
| 3851 | * Note that only a pending nested run can block a pending exception. |
| 3852 | * Otherwise an injected NMI/interrupt should either be |
| 3853 | * lost or delivered to the nested hypervisor in the IDT_VECTORING_INFO, |
| 3854 | * while delivering the pending exception. |
Oliver Upton | 5ef8acb | 2020-02-07 02:36:07 -0800 | [diff] [blame] | 3855 | */ |
Maxim Levitsky | 4020da3 | 2021-04-01 17:38:14 +0300 | [diff] [blame] | 3856 | |
Sean Christopherson | 6ce347a | 2020-04-22 19:25:38 -0700 | [diff] [blame] | 3857 | if (vcpu->arch.exception.pending && !vmx_pending_dbg_trap(vcpu)) { |
Maxim Levitsky | 4020da3 | 2021-04-01 17:38:14 +0300 | [diff] [blame] | 3858 | if (vmx->nested.nested_run_pending) |
Oliver Upton | 5ef8acb | 2020-02-07 02:36:07 -0800 | [diff] [blame] | 3859 | return -EBUSY; |
Sean Christopherson | 6ce347a | 2020-04-22 19:25:38 -0700 | [diff] [blame] | 3860 | if (!nested_vmx_check_exception(vcpu, &exit_qual)) |
| 3861 | goto no_vmexit; |
Oliver Upton | 5ef8acb | 2020-02-07 02:36:07 -0800 | [diff] [blame] | 3862 | nested_vmx_inject_exception_vmexit(vcpu, exit_qual); |
| 3863 | return 0; |
| 3864 | } |
| 3865 | |
| 3866 | if (mtf_pending) { |
| 3867 | if (block_nested_events) |
| 3868 | return -EBUSY; |
| 3869 | nested_vmx_update_pending_dbg(vcpu); |
| 3870 | nested_vmx_vmexit(vcpu, EXIT_REASON_MONITOR_TRAP_FLAG, 0, 0); |
| 3871 | return 0; |
| 3872 | } |
| 3873 | |
Sean Christopherson | 6ce347a | 2020-04-22 19:25:38 -0700 | [diff] [blame] | 3874 | if (vcpu->arch.exception.pending) { |
Maxim Levitsky | 4020da3 | 2021-04-01 17:38:14 +0300 | [diff] [blame] | 3875 | if (vmx->nested.nested_run_pending) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3876 | return -EBUSY; |
Sean Christopherson | 6ce347a | 2020-04-22 19:25:38 -0700 | [diff] [blame] | 3877 | if (!nested_vmx_check_exception(vcpu, &exit_qual)) |
| 3878 | goto no_vmexit; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3879 | nested_vmx_inject_exception_vmexit(vcpu, exit_qual); |
| 3880 | return 0; |
| 3881 | } |
| 3882 | |
Sean Christopherson | d2060bd | 2020-04-22 19:25:39 -0700 | [diff] [blame] | 3883 | if (nested_vmx_preemption_timer_pending(vcpu)) { |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3884 | if (block_nested_events) |
| 3885 | return -EBUSY; |
| 3886 | nested_vmx_vmexit(vcpu, EXIT_REASON_PREEMPTION_TIMER, 0, 0); |
| 3887 | return 0; |
| 3888 | } |
| 3889 | |
Sean Christopherson | 1cd2f0b | 2020-04-22 19:25:46 -0700 | [diff] [blame] | 3890 | if (vcpu->arch.smi_pending && !is_smm(vcpu)) { |
| 3891 | if (block_nested_events) |
| 3892 | return -EBUSY; |
| 3893 | goto no_vmexit; |
| 3894 | } |
| 3895 | |
Sean Christopherson | 15ff0b4 | 2020-04-22 19:25:45 -0700 | [diff] [blame] | 3896 | if (vcpu->arch.nmi_pending && !vmx_nmi_blocked(vcpu)) { |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3897 | if (block_nested_events) |
| 3898 | return -EBUSY; |
Sean Christopherson | 15ff0b4 | 2020-04-22 19:25:45 -0700 | [diff] [blame] | 3899 | if (!nested_exit_on_nmi(vcpu)) |
| 3900 | goto no_vmexit; |
| 3901 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3902 | nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI, |
| 3903 | NMI_VECTOR | INTR_TYPE_NMI_INTR | |
| 3904 | INTR_INFO_VALID_MASK, 0); |
| 3905 | /* |
| 3906 | * The NMI-triggered VM exit counts as injection: |
| 3907 | * clear this one and block further NMIs. |
| 3908 | */ |
| 3909 | vcpu->arch.nmi_pending = 0; |
| 3910 | vmx_set_nmi_mask(vcpu, true); |
| 3911 | return 0; |
| 3912 | } |
| 3913 | |
Sean Christopherson | 15ff0b4 | 2020-04-22 19:25:45 -0700 | [diff] [blame] | 3914 | if (kvm_cpu_has_interrupt(vcpu) && !vmx_interrupt_blocked(vcpu)) { |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3915 | if (block_nested_events) |
| 3916 | return -EBUSY; |
Sean Christopherson | 15ff0b4 | 2020-04-22 19:25:45 -0700 | [diff] [blame] | 3917 | if (!nested_exit_on_intr(vcpu)) |
| 3918 | goto no_vmexit; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3919 | nested_vmx_vmexit(vcpu, EXIT_REASON_EXTERNAL_INTERRUPT, 0, 0); |
| 3920 | return 0; |
| 3921 | } |
| 3922 | |
Sean Christopherson | 6ce347a | 2020-04-22 19:25:38 -0700 | [diff] [blame] | 3923 | no_vmexit: |
Jim Mattson | 650293c | 2021-06-04 10:26:02 -0700 | [diff] [blame] | 3924 | return vmx_complete_nested_posted_interrupt(vcpu); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3925 | } |
| 3926 | |
| 3927 | static u32 vmx_get_preemption_timer_value(struct kvm_vcpu *vcpu) |
| 3928 | { |
| 3929 | ktime_t remaining = |
| 3930 | hrtimer_get_remaining(&to_vmx(vcpu)->nested.preemption_timer); |
| 3931 | u64 value; |
| 3932 | |
| 3933 | if (ktime_to_ns(remaining) <= 0) |
| 3934 | return 0; |
| 3935 | |
| 3936 | value = ktime_to_ns(remaining) * vcpu->arch.virtual_tsc_khz; |
| 3937 | do_div(value, 1000000); |
| 3938 | return value >> VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE; |
| 3939 | } |
| 3940 | |
Sean Christopherson | 7952d76 | 2019-05-07 08:36:29 -0700 | [diff] [blame] | 3941 | static bool is_vmcs12_ext_field(unsigned long field) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3942 | { |
Sean Christopherson | 7952d76 | 2019-05-07 08:36:29 -0700 | [diff] [blame] | 3943 | switch (field) { |
| 3944 | case GUEST_ES_SELECTOR: |
| 3945 | case GUEST_CS_SELECTOR: |
| 3946 | case GUEST_SS_SELECTOR: |
| 3947 | case GUEST_DS_SELECTOR: |
| 3948 | case GUEST_FS_SELECTOR: |
| 3949 | case GUEST_GS_SELECTOR: |
| 3950 | case GUEST_LDTR_SELECTOR: |
| 3951 | case GUEST_TR_SELECTOR: |
| 3952 | case GUEST_ES_LIMIT: |
| 3953 | case GUEST_CS_LIMIT: |
| 3954 | case GUEST_SS_LIMIT: |
| 3955 | case GUEST_DS_LIMIT: |
| 3956 | case GUEST_FS_LIMIT: |
| 3957 | case GUEST_GS_LIMIT: |
| 3958 | case GUEST_LDTR_LIMIT: |
| 3959 | case GUEST_TR_LIMIT: |
| 3960 | case GUEST_GDTR_LIMIT: |
| 3961 | case GUEST_IDTR_LIMIT: |
| 3962 | case GUEST_ES_AR_BYTES: |
| 3963 | case GUEST_DS_AR_BYTES: |
| 3964 | case GUEST_FS_AR_BYTES: |
| 3965 | case GUEST_GS_AR_BYTES: |
| 3966 | case GUEST_LDTR_AR_BYTES: |
| 3967 | case GUEST_TR_AR_BYTES: |
| 3968 | case GUEST_ES_BASE: |
| 3969 | case GUEST_CS_BASE: |
| 3970 | case GUEST_SS_BASE: |
| 3971 | case GUEST_DS_BASE: |
| 3972 | case GUEST_FS_BASE: |
| 3973 | case GUEST_GS_BASE: |
| 3974 | case GUEST_LDTR_BASE: |
| 3975 | case GUEST_TR_BASE: |
| 3976 | case GUEST_GDTR_BASE: |
| 3977 | case GUEST_IDTR_BASE: |
| 3978 | case GUEST_PENDING_DBG_EXCEPTIONS: |
| 3979 | case GUEST_BNDCFGS: |
| 3980 | return true; |
| 3981 | default: |
| 3982 | break; |
| 3983 | } |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3984 | |
Sean Christopherson | 7952d76 | 2019-05-07 08:36:29 -0700 | [diff] [blame] | 3985 | return false; |
| 3986 | } |
| 3987 | |
| 3988 | static void sync_vmcs02_to_vmcs12_rare(struct kvm_vcpu *vcpu, |
| 3989 | struct vmcs12 *vmcs12) |
| 3990 | { |
| 3991 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3992 | |
| 3993 | vmcs12->guest_es_selector = vmcs_read16(GUEST_ES_SELECTOR); |
| 3994 | vmcs12->guest_cs_selector = vmcs_read16(GUEST_CS_SELECTOR); |
| 3995 | vmcs12->guest_ss_selector = vmcs_read16(GUEST_SS_SELECTOR); |
| 3996 | vmcs12->guest_ds_selector = vmcs_read16(GUEST_DS_SELECTOR); |
| 3997 | vmcs12->guest_fs_selector = vmcs_read16(GUEST_FS_SELECTOR); |
| 3998 | vmcs12->guest_gs_selector = vmcs_read16(GUEST_GS_SELECTOR); |
| 3999 | vmcs12->guest_ldtr_selector = vmcs_read16(GUEST_LDTR_SELECTOR); |
| 4000 | vmcs12->guest_tr_selector = vmcs_read16(GUEST_TR_SELECTOR); |
| 4001 | vmcs12->guest_es_limit = vmcs_read32(GUEST_ES_LIMIT); |
| 4002 | vmcs12->guest_cs_limit = vmcs_read32(GUEST_CS_LIMIT); |
| 4003 | vmcs12->guest_ss_limit = vmcs_read32(GUEST_SS_LIMIT); |
| 4004 | vmcs12->guest_ds_limit = vmcs_read32(GUEST_DS_LIMIT); |
| 4005 | vmcs12->guest_fs_limit = vmcs_read32(GUEST_FS_LIMIT); |
| 4006 | vmcs12->guest_gs_limit = vmcs_read32(GUEST_GS_LIMIT); |
| 4007 | vmcs12->guest_ldtr_limit = vmcs_read32(GUEST_LDTR_LIMIT); |
| 4008 | vmcs12->guest_tr_limit = vmcs_read32(GUEST_TR_LIMIT); |
| 4009 | vmcs12->guest_gdtr_limit = vmcs_read32(GUEST_GDTR_LIMIT); |
| 4010 | vmcs12->guest_idtr_limit = vmcs_read32(GUEST_IDTR_LIMIT); |
| 4011 | vmcs12->guest_es_ar_bytes = vmcs_read32(GUEST_ES_AR_BYTES); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4012 | vmcs12->guest_ds_ar_bytes = vmcs_read32(GUEST_DS_AR_BYTES); |
| 4013 | vmcs12->guest_fs_ar_bytes = vmcs_read32(GUEST_FS_AR_BYTES); |
| 4014 | vmcs12->guest_gs_ar_bytes = vmcs_read32(GUEST_GS_AR_BYTES); |
| 4015 | vmcs12->guest_ldtr_ar_bytes = vmcs_read32(GUEST_LDTR_AR_BYTES); |
| 4016 | vmcs12->guest_tr_ar_bytes = vmcs_read32(GUEST_TR_AR_BYTES); |
| 4017 | vmcs12->guest_es_base = vmcs_readl(GUEST_ES_BASE); |
| 4018 | vmcs12->guest_cs_base = vmcs_readl(GUEST_CS_BASE); |
| 4019 | vmcs12->guest_ss_base = vmcs_readl(GUEST_SS_BASE); |
| 4020 | vmcs12->guest_ds_base = vmcs_readl(GUEST_DS_BASE); |
| 4021 | vmcs12->guest_fs_base = vmcs_readl(GUEST_FS_BASE); |
| 4022 | vmcs12->guest_gs_base = vmcs_readl(GUEST_GS_BASE); |
| 4023 | vmcs12->guest_ldtr_base = vmcs_readl(GUEST_LDTR_BASE); |
| 4024 | vmcs12->guest_tr_base = vmcs_readl(GUEST_TR_BASE); |
| 4025 | vmcs12->guest_gdtr_base = vmcs_readl(GUEST_GDTR_BASE); |
| 4026 | vmcs12->guest_idtr_base = vmcs_readl(GUEST_IDTR_BASE); |
Sean Christopherson | 7952d76 | 2019-05-07 08:36:29 -0700 | [diff] [blame] | 4027 | vmcs12->guest_pending_dbg_exceptions = |
| 4028 | vmcs_readl(GUEST_PENDING_DBG_EXCEPTIONS); |
| 4029 | if (kvm_mpx_supported()) |
| 4030 | vmcs12->guest_bndcfgs = vmcs_read64(GUEST_BNDCFGS); |
| 4031 | |
| 4032 | vmx->nested.need_sync_vmcs02_to_vmcs12_rare = false; |
| 4033 | } |
| 4034 | |
| 4035 | static void copy_vmcs02_to_vmcs12_rare(struct kvm_vcpu *vcpu, |
| 4036 | struct vmcs12 *vmcs12) |
| 4037 | { |
| 4038 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 4039 | int cpu; |
| 4040 | |
| 4041 | if (!vmx->nested.need_sync_vmcs02_to_vmcs12_rare) |
| 4042 | return; |
| 4043 | |
| 4044 | |
| 4045 | WARN_ON_ONCE(vmx->loaded_vmcs != &vmx->vmcs01); |
| 4046 | |
| 4047 | cpu = get_cpu(); |
| 4048 | vmx->loaded_vmcs = &vmx->nested.vmcs02; |
Sean Christopherson | 1af1bb0 | 2020-05-06 16:58:50 -0700 | [diff] [blame] | 4049 | vmx_vcpu_load_vmcs(vcpu, cpu, &vmx->vmcs01); |
Sean Christopherson | 7952d76 | 2019-05-07 08:36:29 -0700 | [diff] [blame] | 4050 | |
| 4051 | sync_vmcs02_to_vmcs12_rare(vcpu, vmcs12); |
| 4052 | |
| 4053 | vmx->loaded_vmcs = &vmx->vmcs01; |
Sean Christopherson | 1af1bb0 | 2020-05-06 16:58:50 -0700 | [diff] [blame] | 4054 | vmx_vcpu_load_vmcs(vcpu, cpu, &vmx->nested.vmcs02); |
Sean Christopherson | 7952d76 | 2019-05-07 08:36:29 -0700 | [diff] [blame] | 4055 | put_cpu(); |
| 4056 | } |
| 4057 | |
| 4058 | /* |
| 4059 | * Update the guest state fields of vmcs12 to reflect changes that |
| 4060 | * occurred while L2 was running. (The "IA-32e mode guest" bit of the |
| 4061 | * VM-entry controls is also updated, since this is really a guest |
| 4062 | * state bit.) |
| 4063 | */ |
| 4064 | static void sync_vmcs02_to_vmcs12(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12) |
| 4065 | { |
| 4066 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 4067 | |
Vitaly Kuznetsov | 1e9dfbd | 2021-05-26 15:20:16 +0200 | [diff] [blame] | 4068 | if (evmptr_is_valid(vmx->nested.hv_evmcs_vmptr)) |
Sean Christopherson | 7952d76 | 2019-05-07 08:36:29 -0700 | [diff] [blame] | 4069 | sync_vmcs02_to_vmcs12_rare(vcpu, vmcs12); |
| 4070 | |
Vitaly Kuznetsov | 1e9dfbd | 2021-05-26 15:20:16 +0200 | [diff] [blame] | 4071 | vmx->nested.need_sync_vmcs02_to_vmcs12_rare = |
| 4072 | !evmptr_is_valid(vmx->nested.hv_evmcs_vmptr); |
Sean Christopherson | 7952d76 | 2019-05-07 08:36:29 -0700 | [diff] [blame] | 4073 | |
| 4074 | vmcs12->guest_cr0 = vmcs12_guest_cr0(vcpu, vmcs12); |
| 4075 | vmcs12->guest_cr4 = vmcs12_guest_cr4(vcpu, vmcs12); |
| 4076 | |
| 4077 | vmcs12->guest_rsp = kvm_rsp_read(vcpu); |
| 4078 | vmcs12->guest_rip = kvm_rip_read(vcpu); |
| 4079 | vmcs12->guest_rflags = vmcs_readl(GUEST_RFLAGS); |
| 4080 | |
| 4081 | vmcs12->guest_cs_ar_bytes = vmcs_read32(GUEST_CS_AR_BYTES); |
| 4082 | vmcs12->guest_ss_ar_bytes = vmcs_read32(GUEST_SS_AR_BYTES); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4083 | |
| 4084 | vmcs12->guest_interruptibility_info = |
| 4085 | vmcs_read32(GUEST_INTERRUPTIBILITY_INFO); |
Sean Christopherson | 7952d76 | 2019-05-07 08:36:29 -0700 | [diff] [blame] | 4086 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4087 | if (vcpu->arch.mp_state == KVM_MP_STATE_HALTED) |
| 4088 | vmcs12->guest_activity_state = GUEST_ACTIVITY_HLT; |
Yadong Qi | bf0cd88 | 2020-11-06 14:51:22 +0800 | [diff] [blame] | 4089 | else if (vcpu->arch.mp_state == KVM_MP_STATE_INIT_RECEIVED) |
| 4090 | vmcs12->guest_activity_state = GUEST_ACTIVITY_WAIT_SIPI; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4091 | else |
| 4092 | vmcs12->guest_activity_state = GUEST_ACTIVITY_ACTIVE; |
| 4093 | |
Paolo Bonzini | b4b65b5 | 2019-01-29 19:12:35 +0100 | [diff] [blame] | 4094 | if (nested_cpu_has_preemption_timer(vmcs12) && |
Peter Shier | 850448f | 2020-05-26 14:51:06 -0700 | [diff] [blame] | 4095 | vmcs12->vm_exit_controls & VM_EXIT_SAVE_VMX_PREEMPTION_TIMER && |
| 4096 | !vmx->nested.nested_run_pending) |
| 4097 | vmcs12->vmx_preemption_timer_value = |
| 4098 | vmx_get_preemption_timer_value(vcpu); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4099 | |
| 4100 | /* |
| 4101 | * In some cases (usually, nested EPT), L2 is allowed to change its |
| 4102 | * own CR3 without exiting. If it has changed it, we must keep it. |
| 4103 | * Of course, if L0 is using shadow page tables, GUEST_CR3 was defined |
| 4104 | * by L0, not L1 or L2, so we mustn't unconditionally copy it to vmcs12. |
| 4105 | * |
| 4106 | * Additionally, restore L2's PDPTR to vmcs12. |
| 4107 | */ |
| 4108 | if (enable_ept) { |
| 4109 | vmcs12->guest_cr3 = vmcs_readl(GUEST_CR3); |
Sean Christopherson | c7554efc | 2019-05-07 09:06:40 -0700 | [diff] [blame] | 4110 | if (nested_cpu_has_ept(vmcs12) && is_pae_paging(vcpu)) { |
| 4111 | vmcs12->guest_pdptr0 = vmcs_read64(GUEST_PDPTR0); |
| 4112 | vmcs12->guest_pdptr1 = vmcs_read64(GUEST_PDPTR1); |
| 4113 | vmcs12->guest_pdptr2 = vmcs_read64(GUEST_PDPTR2); |
| 4114 | vmcs12->guest_pdptr3 = vmcs_read64(GUEST_PDPTR3); |
| 4115 | } |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4116 | } |
| 4117 | |
| 4118 | vmcs12->guest_linear_address = vmcs_readl(GUEST_LINEAR_ADDRESS); |
| 4119 | |
| 4120 | if (nested_cpu_has_vid(vmcs12)) |
| 4121 | vmcs12->guest_intr_status = vmcs_read16(GUEST_INTR_STATUS); |
| 4122 | |
| 4123 | vmcs12->vm_entry_controls = |
| 4124 | (vmcs12->vm_entry_controls & ~VM_ENTRY_IA32E_MODE) | |
| 4125 | (vm_entry_controls_get(to_vmx(vcpu)) & VM_ENTRY_IA32E_MODE); |
| 4126 | |
Sean Christopherson | 699a1ac | 2019-05-07 09:06:37 -0700 | [diff] [blame] | 4127 | if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_DEBUG_CONTROLS) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4128 | kvm_get_dr(vcpu, 7, (unsigned long *)&vmcs12->guest_dr7); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4129 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4130 | if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_IA32_EFER) |
| 4131 | vmcs12->guest_ia32_efer = vcpu->arch.efer; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4132 | } |
| 4133 | |
| 4134 | /* |
| 4135 | * prepare_vmcs12 is part of what we need to do when the nested L2 guest exits |
| 4136 | * and we want to prepare to run its L1 parent. L1 keeps a vmcs for L2 (vmcs12), |
| 4137 | * and this function updates it to reflect the changes to the guest state while |
| 4138 | * L2 was running (and perhaps made some exits which were handled directly by L0 |
| 4139 | * without going back to L1), and to reflect the exit reason. |
| 4140 | * Note that we do not have to copy here all VMCS fields, just those that |
| 4141 | * could have changed by the L2 guest or the exit - i.e., the guest-state and |
| 4142 | * exit-information fields only. Other fields are modified by L1 with VMWRITE, |
| 4143 | * which already writes to vmcs12 directly. |
| 4144 | */ |
| 4145 | static void prepare_vmcs12(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12, |
Sean Christopherson | 4dcefa3 | 2020-04-15 10:55:18 -0700 | [diff] [blame] | 4146 | u32 vm_exit_reason, u32 exit_intr_info, |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4147 | unsigned long exit_qualification) |
| 4148 | { |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4149 | /* update exit information fields: */ |
Sean Christopherson | 4dcefa3 | 2020-04-15 10:55:18 -0700 | [diff] [blame] | 4150 | vmcs12->vm_exit_reason = vm_exit_reason; |
Sean Christopherson | 3c0c2ad | 2021-04-12 16:21:37 +1200 | [diff] [blame] | 4151 | if (to_vmx(vcpu)->exit_reason.enclave_mode) |
| 4152 | vmcs12->vm_exit_reason |= VMX_EXIT_REASONS_SGX_ENCLAVE_MODE; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4153 | vmcs12->exit_qualification = exit_qualification; |
| 4154 | vmcs12->vm_exit_intr_info = exit_intr_info; |
| 4155 | |
| 4156 | vmcs12->idt_vectoring_info_field = 0; |
| 4157 | vmcs12->vm_exit_instruction_len = vmcs_read32(VM_EXIT_INSTRUCTION_LEN); |
| 4158 | vmcs12->vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO); |
| 4159 | |
| 4160 | if (!(vmcs12->vm_exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY)) { |
| 4161 | vmcs12->launch_state = 1; |
| 4162 | |
| 4163 | /* vm_entry_intr_info_field is cleared on exit. Emulate this |
| 4164 | * instead of reading the real value. */ |
| 4165 | vmcs12->vm_entry_intr_info_field &= ~INTR_INFO_VALID_MASK; |
| 4166 | |
| 4167 | /* |
| 4168 | * Transfer the event that L0 or L1 may wanted to inject into |
| 4169 | * L2 to IDT_VECTORING_INFO_FIELD. |
| 4170 | */ |
| 4171 | vmcs12_save_pending_event(vcpu, vmcs12); |
Krish Sadhukhan | a0d4f80 | 2018-12-04 19:00:13 -0500 | [diff] [blame] | 4172 | |
| 4173 | /* |
| 4174 | * According to spec, there's no need to store the guest's |
| 4175 | * MSRs if the exit is due to a VM-entry failure that occurs |
| 4176 | * during or after loading the guest state. Since this exit |
| 4177 | * does not fall in that category, we need to save the MSRs. |
| 4178 | */ |
| 4179 | if (nested_vmx_store_msr(vcpu, |
| 4180 | vmcs12->vm_exit_msr_store_addr, |
| 4181 | vmcs12->vm_exit_msr_store_count)) |
| 4182 | nested_vmx_abort(vcpu, |
| 4183 | VMX_ABORT_SAVE_GUEST_MSR_FAIL); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4184 | } |
| 4185 | |
| 4186 | /* |
| 4187 | * Drop what we picked up for L2 via vmx_complete_interrupts. It is |
| 4188 | * preserved above and would only end up incorrectly in L1. |
| 4189 | */ |
| 4190 | vcpu->arch.nmi_injected = false; |
| 4191 | kvm_clear_exception_queue(vcpu); |
| 4192 | kvm_clear_interrupt_queue(vcpu); |
| 4193 | } |
| 4194 | |
| 4195 | /* |
| 4196 | * A part of what we need to when the nested L2 guest exits and we want to |
| 4197 | * run its L1 parent, is to reset L1's guest state to the host state specified |
| 4198 | * in vmcs12. |
| 4199 | * This function is to be called not only on normal nested exit, but also on |
| 4200 | * a nested entry failure, as explained in Intel's spec, 3B.23.7 ("VM-Entry |
| 4201 | * Failures During or After Loading Guest State"). |
| 4202 | * This function should be called when the active VMCS is L1's (vmcs01). |
| 4203 | */ |
| 4204 | static void load_vmcs12_host_state(struct kvm_vcpu *vcpu, |
| 4205 | struct vmcs12 *vmcs12) |
| 4206 | { |
Sean Christopherson | 68cda40 | 2020-05-11 15:05:29 -0700 | [diff] [blame] | 4207 | enum vm_entry_failure_code ignored; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4208 | struct kvm_segment seg; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4209 | |
| 4210 | if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_EFER) |
| 4211 | vcpu->arch.efer = vmcs12->host_ia32_efer; |
| 4212 | else if (vmcs12->vm_exit_controls & VM_EXIT_HOST_ADDR_SPACE_SIZE) |
| 4213 | vcpu->arch.efer |= (EFER_LMA | EFER_LME); |
| 4214 | else |
| 4215 | vcpu->arch.efer &= ~(EFER_LMA | EFER_LME); |
| 4216 | vmx_set_efer(vcpu, vcpu->arch.efer); |
| 4217 | |
Paolo Bonzini | e9c16c7 | 2019-04-30 22:07:26 +0200 | [diff] [blame] | 4218 | kvm_rsp_write(vcpu, vmcs12->host_rsp); |
| 4219 | kvm_rip_write(vcpu, vmcs12->host_rip); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4220 | vmx_set_rflags(vcpu, X86_EFLAGS_FIXED); |
| 4221 | vmx_set_interrupt_shadow(vcpu, 0); |
| 4222 | |
| 4223 | /* |
| 4224 | * Note that calling vmx_set_cr0 is important, even if cr0 hasn't |
| 4225 | * actually changed, because vmx_set_cr0 refers to efer set above. |
| 4226 | * |
| 4227 | * CR0_GUEST_HOST_MASK is already set in the original vmcs01 |
| 4228 | * (KVM doesn't change it); |
| 4229 | */ |
Sean Christopherson | fa71e95 | 2020-07-02 21:04:22 -0700 | [diff] [blame] | 4230 | vcpu->arch.cr0_guest_owned_bits = KVM_POSSIBLE_CR0_GUEST_BITS; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4231 | vmx_set_cr0(vcpu, vmcs12->host_cr0); |
| 4232 | |
| 4233 | /* Same as above - no reason to call set_cr4_guest_host_mask(). */ |
| 4234 | vcpu->arch.cr4_guest_owned_bits = ~vmcs_readl(CR4_GUEST_HOST_MASK); |
| 4235 | vmx_set_cr4(vcpu, vmcs12->host_cr4); |
| 4236 | |
| 4237 | nested_ept_uninit_mmu_context(vcpu); |
| 4238 | |
| 4239 | /* |
| 4240 | * Only PDPTE load can fail as the value of cr3 was checked on entry and |
| 4241 | * couldn't have changed. |
| 4242 | */ |
Maxim Levitsky | 0f85722 | 2021-06-07 12:02:00 +0300 | [diff] [blame] | 4243 | if (nested_vmx_load_cr3(vcpu, vmcs12->host_cr3, false, true, &ignored)) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4244 | nested_vmx_abort(vcpu, VMX_ABORT_LOAD_HOST_PDPTE_FAIL); |
| 4245 | |
Sean Christopherson | 50b265a | 2020-03-20 14:28:19 -0700 | [diff] [blame] | 4246 | nested_vmx_transition_tlb_flush(vcpu, vmcs12, false); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4247 | |
| 4248 | vmcs_write32(GUEST_SYSENTER_CS, vmcs12->host_ia32_sysenter_cs); |
| 4249 | vmcs_writel(GUEST_SYSENTER_ESP, vmcs12->host_ia32_sysenter_esp); |
| 4250 | vmcs_writel(GUEST_SYSENTER_EIP, vmcs12->host_ia32_sysenter_eip); |
| 4251 | vmcs_writel(GUEST_IDTR_BASE, vmcs12->host_idtr_base); |
| 4252 | vmcs_writel(GUEST_GDTR_BASE, vmcs12->host_gdtr_base); |
| 4253 | vmcs_write32(GUEST_IDTR_LIMIT, 0xFFFF); |
| 4254 | vmcs_write32(GUEST_GDTR_LIMIT, 0xFFFF); |
| 4255 | |
| 4256 | /* If not VM_EXIT_CLEAR_BNDCFGS, the L2 value propagates to L1. */ |
| 4257 | if (vmcs12->vm_exit_controls & VM_EXIT_CLEAR_BNDCFGS) |
| 4258 | vmcs_write64(GUEST_BNDCFGS, 0); |
| 4259 | |
| 4260 | if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PAT) { |
| 4261 | vmcs_write64(GUEST_IA32_PAT, vmcs12->host_ia32_pat); |
| 4262 | vcpu->arch.pat = vmcs12->host_ia32_pat; |
| 4263 | } |
| 4264 | if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL) |
Oliver Upton | d196842 | 2019-12-13 16:33:58 -0800 | [diff] [blame] | 4265 | WARN_ON_ONCE(kvm_set_msr(vcpu, MSR_CORE_PERF_GLOBAL_CTRL, |
| 4266 | vmcs12->host_ia32_perf_global_ctrl)); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4267 | |
| 4268 | /* Set L1 segment info according to Intel SDM |
| 4269 | 27.5.2 Loading Host Segment and Descriptor-Table Registers */ |
| 4270 | seg = (struct kvm_segment) { |
| 4271 | .base = 0, |
| 4272 | .limit = 0xFFFFFFFF, |
| 4273 | .selector = vmcs12->host_cs_selector, |
| 4274 | .type = 11, |
| 4275 | .present = 1, |
| 4276 | .s = 1, |
| 4277 | .g = 1 |
| 4278 | }; |
| 4279 | if (vmcs12->vm_exit_controls & VM_EXIT_HOST_ADDR_SPACE_SIZE) |
| 4280 | seg.l = 1; |
| 4281 | else |
| 4282 | seg.db = 1; |
Sean Christopherson | 816be9e | 2021-07-13 09:33:07 -0700 | [diff] [blame] | 4283 | __vmx_set_segment(vcpu, &seg, VCPU_SREG_CS); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4284 | seg = (struct kvm_segment) { |
| 4285 | .base = 0, |
| 4286 | .limit = 0xFFFFFFFF, |
| 4287 | .type = 3, |
| 4288 | .present = 1, |
| 4289 | .s = 1, |
| 4290 | .db = 1, |
| 4291 | .g = 1 |
| 4292 | }; |
| 4293 | seg.selector = vmcs12->host_ds_selector; |
Sean Christopherson | 816be9e | 2021-07-13 09:33:07 -0700 | [diff] [blame] | 4294 | __vmx_set_segment(vcpu, &seg, VCPU_SREG_DS); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4295 | seg.selector = vmcs12->host_es_selector; |
Sean Christopherson | 816be9e | 2021-07-13 09:33:07 -0700 | [diff] [blame] | 4296 | __vmx_set_segment(vcpu, &seg, VCPU_SREG_ES); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4297 | seg.selector = vmcs12->host_ss_selector; |
Sean Christopherson | 816be9e | 2021-07-13 09:33:07 -0700 | [diff] [blame] | 4298 | __vmx_set_segment(vcpu, &seg, VCPU_SREG_SS); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4299 | seg.selector = vmcs12->host_fs_selector; |
| 4300 | seg.base = vmcs12->host_fs_base; |
Sean Christopherson | 816be9e | 2021-07-13 09:33:07 -0700 | [diff] [blame] | 4301 | __vmx_set_segment(vcpu, &seg, VCPU_SREG_FS); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4302 | seg.selector = vmcs12->host_gs_selector; |
| 4303 | seg.base = vmcs12->host_gs_base; |
Sean Christopherson | 816be9e | 2021-07-13 09:33:07 -0700 | [diff] [blame] | 4304 | __vmx_set_segment(vcpu, &seg, VCPU_SREG_GS); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4305 | seg = (struct kvm_segment) { |
| 4306 | .base = vmcs12->host_tr_base, |
| 4307 | .limit = 0x67, |
| 4308 | .selector = vmcs12->host_tr_selector, |
| 4309 | .type = 11, |
| 4310 | .present = 1 |
| 4311 | }; |
Sean Christopherson | 816be9e | 2021-07-13 09:33:07 -0700 | [diff] [blame] | 4312 | __vmx_set_segment(vcpu, &seg, VCPU_SREG_TR); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4313 | |
Sean Christopherson | afc8de0 | 2021-07-13 09:32:40 -0700 | [diff] [blame] | 4314 | memset(&seg, 0, sizeof(seg)); |
| 4315 | seg.unusable = 1; |
Sean Christopherson | 816be9e | 2021-07-13 09:33:07 -0700 | [diff] [blame] | 4316 | __vmx_set_segment(vcpu, &seg, VCPU_SREG_LDTR); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4317 | |
| 4318 | kvm_set_dr(vcpu, 7, 0x400); |
| 4319 | vmcs_write64(GUEST_IA32_DEBUGCTL, 0); |
| 4320 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4321 | if (nested_vmx_load_msr(vcpu, vmcs12->vm_exit_msr_load_addr, |
| 4322 | vmcs12->vm_exit_msr_load_count)) |
| 4323 | nested_vmx_abort(vcpu, VMX_ABORT_LOAD_HOST_MSR_FAIL); |
Maxim Levitsky | dbab610 | 2021-09-13 17:09:54 +0300 | [diff] [blame] | 4324 | |
| 4325 | to_vmx(vcpu)->emulation_required = vmx_emulation_required(vcpu); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4326 | } |
| 4327 | |
| 4328 | static inline u64 nested_vmx_get_vmcs01_guest_efer(struct vcpu_vmx *vmx) |
| 4329 | { |
Sean Christopherson | eb3db1b | 2020-09-23 11:03:58 -0700 | [diff] [blame] | 4330 | struct vmx_uret_msr *efer_msr; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4331 | unsigned int i; |
| 4332 | |
| 4333 | if (vm_entry_controls_get(vmx) & VM_ENTRY_LOAD_IA32_EFER) |
| 4334 | return vmcs_read64(GUEST_IA32_EFER); |
| 4335 | |
| 4336 | if (cpu_has_load_ia32_efer()) |
| 4337 | return host_efer; |
| 4338 | |
| 4339 | for (i = 0; i < vmx->msr_autoload.guest.nr; ++i) { |
| 4340 | if (vmx->msr_autoload.guest.val[i].index == MSR_EFER) |
| 4341 | return vmx->msr_autoload.guest.val[i].value; |
| 4342 | } |
| 4343 | |
Sean Christopherson | d85a803 | 2020-09-23 11:04:06 -0700 | [diff] [blame] | 4344 | efer_msr = vmx_find_uret_msr(vmx, MSR_EFER); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4345 | if (efer_msr) |
| 4346 | return efer_msr->data; |
| 4347 | |
| 4348 | return host_efer; |
| 4349 | } |
| 4350 | |
| 4351 | static void nested_vmx_restore_host_state(struct kvm_vcpu *vcpu) |
| 4352 | { |
| 4353 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
| 4354 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 4355 | struct vmx_msr_entry g, h; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4356 | gpa_t gpa; |
| 4357 | u32 i, j; |
| 4358 | |
| 4359 | vcpu->arch.pat = vmcs_read64(GUEST_IA32_PAT); |
| 4360 | |
| 4361 | if (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS) { |
| 4362 | /* |
| 4363 | * L1's host DR7 is lost if KVM_GUESTDBG_USE_HW_BP is set |
| 4364 | * as vmcs01.GUEST_DR7 contains a userspace defined value |
| 4365 | * and vcpu->arch.dr7 is not squirreled away before the |
| 4366 | * nested VMENTER (not worth adding a variable in nested_vmx). |
| 4367 | */ |
| 4368 | if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP) |
| 4369 | kvm_set_dr(vcpu, 7, DR7_FIXED_1); |
| 4370 | else |
| 4371 | WARN_ON(kvm_set_dr(vcpu, 7, vmcs_readl(GUEST_DR7))); |
| 4372 | } |
| 4373 | |
| 4374 | /* |
| 4375 | * Note that calling vmx_set_{efer,cr0,cr4} is important as they |
| 4376 | * handle a variety of side effects to KVM's software model. |
| 4377 | */ |
| 4378 | vmx_set_efer(vcpu, nested_vmx_get_vmcs01_guest_efer(vmx)); |
| 4379 | |
Sean Christopherson | fa71e95 | 2020-07-02 21:04:22 -0700 | [diff] [blame] | 4380 | vcpu->arch.cr0_guest_owned_bits = KVM_POSSIBLE_CR0_GUEST_BITS; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4381 | vmx_set_cr0(vcpu, vmcs_readl(CR0_READ_SHADOW)); |
| 4382 | |
| 4383 | vcpu->arch.cr4_guest_owned_bits = ~vmcs_readl(CR4_GUEST_HOST_MASK); |
| 4384 | vmx_set_cr4(vcpu, vmcs_readl(CR4_READ_SHADOW)); |
| 4385 | |
| 4386 | nested_ept_uninit_mmu_context(vcpu); |
Sean Christopherson | f087a02 | 2019-06-07 11:55:34 -0700 | [diff] [blame] | 4387 | vcpu->arch.cr3 = vmcs_readl(GUEST_CR3); |
Sean Christopherson | cb3c1e2 | 2019-09-27 14:45:22 -0700 | [diff] [blame] | 4388 | kvm_register_mark_available(vcpu, VCPU_EXREG_CR3); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4389 | |
| 4390 | /* |
| 4391 | * Use ept_save_pdptrs(vcpu) to load the MMU's cached PDPTRs |
| 4392 | * from vmcs01 (if necessary). The PDPTRs are not loaded on |
| 4393 | * VMFail, like everything else we just need to ensure our |
| 4394 | * software model is up-to-date. |
| 4395 | */ |
Sean Christopherson | 9932b49 | 2020-04-15 13:34:50 -0700 | [diff] [blame] | 4396 | if (enable_ept && is_pae_paging(vcpu)) |
Sean Christopherson | f087a02 | 2019-06-07 11:55:34 -0700 | [diff] [blame] | 4397 | ept_save_pdptrs(vcpu); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4398 | |
| 4399 | kvm_mmu_reset_context(vcpu); |
| 4400 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4401 | /* |
| 4402 | * This nasty bit of open coding is a compromise between blindly |
| 4403 | * loading L1's MSRs using the exit load lists (incorrect emulation |
| 4404 | * of VMFail), leaving the nested VM's MSRs in the software model |
| 4405 | * (incorrect behavior) and snapshotting the modified MSRs (too |
| 4406 | * expensive since the lists are unbound by hardware). For each |
| 4407 | * MSR that was (prematurely) loaded from the nested VMEntry load |
| 4408 | * list, reload it from the exit load list if it exists and differs |
| 4409 | * from the guest value. The intent is to stuff host state as |
| 4410 | * silently as possible, not to fully process the exit load list. |
| 4411 | */ |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4412 | for (i = 0; i < vmcs12->vm_entry_msr_load_count; i++) { |
| 4413 | gpa = vmcs12->vm_entry_msr_load_addr + (i * sizeof(g)); |
| 4414 | if (kvm_vcpu_read_guest(vcpu, gpa, &g, sizeof(g))) { |
| 4415 | pr_debug_ratelimited( |
| 4416 | "%s read MSR index failed (%u, 0x%08llx)\n", |
| 4417 | __func__, i, gpa); |
| 4418 | goto vmabort; |
| 4419 | } |
| 4420 | |
| 4421 | for (j = 0; j < vmcs12->vm_exit_msr_load_count; j++) { |
| 4422 | gpa = vmcs12->vm_exit_msr_load_addr + (j * sizeof(h)); |
| 4423 | if (kvm_vcpu_read_guest(vcpu, gpa, &h, sizeof(h))) { |
| 4424 | pr_debug_ratelimited( |
| 4425 | "%s read MSR failed (%u, 0x%08llx)\n", |
| 4426 | __func__, j, gpa); |
| 4427 | goto vmabort; |
| 4428 | } |
| 4429 | if (h.index != g.index) |
| 4430 | continue; |
| 4431 | if (h.value == g.value) |
| 4432 | break; |
| 4433 | |
| 4434 | if (nested_vmx_load_msr_check(vcpu, &h)) { |
| 4435 | pr_debug_ratelimited( |
| 4436 | "%s check failed (%u, 0x%x, 0x%x)\n", |
| 4437 | __func__, j, h.index, h.reserved); |
| 4438 | goto vmabort; |
| 4439 | } |
| 4440 | |
Sean Christopherson | f20935d | 2019-09-05 14:22:54 -0700 | [diff] [blame] | 4441 | if (kvm_set_msr(vcpu, h.index, h.value)) { |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4442 | pr_debug_ratelimited( |
| 4443 | "%s WRMSR failed (%u, 0x%x, 0x%llx)\n", |
| 4444 | __func__, j, h.index, h.value); |
| 4445 | goto vmabort; |
| 4446 | } |
| 4447 | } |
| 4448 | } |
| 4449 | |
| 4450 | return; |
| 4451 | |
| 4452 | vmabort: |
| 4453 | nested_vmx_abort(vcpu, VMX_ABORT_LOAD_HOST_MSR_FAIL); |
| 4454 | } |
| 4455 | |
| 4456 | /* |
| 4457 | * Emulate an exit from nested guest (L2) to L1, i.e., prepare to run L1 |
| 4458 | * and modify vmcs12 to make it see what it would expect to see there if |
| 4459 | * L2 was its real guest. Must only be called when in L2 (is_guest_mode()) |
| 4460 | */ |
Sean Christopherson | 4dcefa3 | 2020-04-15 10:55:18 -0700 | [diff] [blame] | 4461 | void nested_vmx_vmexit(struct kvm_vcpu *vcpu, u32 vm_exit_reason, |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4462 | u32 exit_intr_info, unsigned long exit_qualification) |
| 4463 | { |
| 4464 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 4465 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
| 4466 | |
| 4467 | /* trying to cancel vmlaunch/vmresume is a bug */ |
| 4468 | WARN_ON_ONCE(vmx->nested.nested_run_pending); |
| 4469 | |
Sean Christopherson | cb6a32c | 2021-03-02 09:45:14 -0800 | [diff] [blame] | 4470 | /* Similarly, triple faults in L2 should never escape. */ |
| 4471 | WARN_ON_ONCE(kvm_check_request(KVM_REQ_TRIPLE_FAULT, vcpu)); |
| 4472 | |
Vitaly Kuznetsov | f5c7e84 | 2021-05-03 17:08:51 +0200 | [diff] [blame] | 4473 | if (kvm_check_request(KVM_REQ_GET_NESTED_STATE_PAGES, vcpu)) { |
| 4474 | /* |
| 4475 | * KVM_REQ_GET_NESTED_STATE_PAGES is also used to map |
| 4476 | * Enlightened VMCS after migration and we still need to |
| 4477 | * do that when something is forcing L2->L1 exit prior to |
| 4478 | * the first L2 run. |
| 4479 | */ |
| 4480 | (void)nested_get_evmcs_page(vcpu); |
| 4481 | } |
Maxim Levitsky | f2c7ef3 | 2021-01-07 11:38:51 +0200 | [diff] [blame] | 4482 | |
Sean Christopherson | eeeb4f6 | 2020-03-20 14:28:20 -0700 | [diff] [blame] | 4483 | /* Service the TLB flush request for L2 before switching to L1. */ |
| 4484 | if (kvm_check_request(KVM_REQ_TLB_FLUSH_CURRENT, vcpu)) |
| 4485 | kvm_vcpu_flush_tlb_current(vcpu); |
| 4486 | |
Peter Shier | 43fea4e | 2020-08-20 16:05:45 -0700 | [diff] [blame] | 4487 | /* |
| 4488 | * VCPU_EXREG_PDPTR will be clobbered in arch/x86/kvm/vmx/vmx.h between |
| 4489 | * now and the new vmentry. Ensure that the VMCS02 PDPTR fields are |
| 4490 | * up-to-date before switching to L1. |
| 4491 | */ |
| 4492 | if (enable_ept && is_pae_paging(vcpu)) |
| 4493 | vmx_ept_load_pdptrs(vcpu); |
| 4494 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4495 | leave_guest_mode(vcpu); |
| 4496 | |
Paolo Bonzini | b4b65b5 | 2019-01-29 19:12:35 +0100 | [diff] [blame] | 4497 | if (nested_cpu_has_preemption_timer(vmcs12)) |
| 4498 | hrtimer_cancel(&to_vmx(vcpu)->nested.preemption_timer); |
| 4499 | |
Ilias Stamatis | d041b5e | 2021-05-26 19:44:17 +0100 | [diff] [blame] | 4500 | if (nested_cpu_has(vmcs12, CPU_BASED_USE_TSC_OFFSETTING)) { |
| 4501 | vcpu->arch.tsc_offset = vcpu->arch.l1_tsc_offset; |
| 4502 | if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_TSC_SCALING)) |
| 4503 | vcpu->arch.tsc_scaling_ratio = vcpu->arch.l1_tsc_scaling_ratio; |
| 4504 | } |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4505 | |
| 4506 | if (likely(!vmx->fail)) { |
Sean Christopherson | 3731905ef | 2019-05-07 08:36:27 -0700 | [diff] [blame] | 4507 | sync_vmcs02_to_vmcs12(vcpu, vmcs12); |
Sean Christopherson | f4f8316 | 2019-05-07 08:36:26 -0700 | [diff] [blame] | 4508 | |
Sean Christopherson | 4dcefa3 | 2020-04-15 10:55:18 -0700 | [diff] [blame] | 4509 | if (vm_exit_reason != -1) |
| 4510 | prepare_vmcs12(vcpu, vmcs12, vm_exit_reason, |
| 4511 | exit_intr_info, exit_qualification); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4512 | |
| 4513 | /* |
Sean Christopherson | 3731905ef | 2019-05-07 08:36:27 -0700 | [diff] [blame] | 4514 | * Must happen outside of sync_vmcs02_to_vmcs12() as it will |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4515 | * also be used to capture vmcs12 cache as part of |
| 4516 | * capturing nVMX state for snapshot (migration). |
| 4517 | * |
| 4518 | * Otherwise, this flush will dirty guest memory at a |
| 4519 | * point it is already assumed by user-space to be |
| 4520 | * immutable. |
| 4521 | */ |
| 4522 | nested_flush_cached_shadow_vmcs12(vcpu, vmcs12); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4523 | } else { |
| 4524 | /* |
| 4525 | * The only expected VM-instruction error is "VM entry with |
| 4526 | * invalid control field(s)." Anything else indicates a |
| 4527 | * problem with L0. And we should never get here with a |
| 4528 | * VMFail of any type if early consistency checks are enabled. |
| 4529 | */ |
| 4530 | WARN_ON_ONCE(vmcs_read32(VM_INSTRUCTION_ERROR) != |
| 4531 | VMXERR_ENTRY_INVALID_CONTROL_FIELD); |
| 4532 | WARN_ON_ONCE(nested_early_check); |
| 4533 | } |
| 4534 | |
| 4535 | vmx_switch_vmcs(vcpu, &vmx->vmcs01); |
| 4536 | |
| 4537 | /* Update any VMCS fields that might have changed while L2 ran */ |
| 4538 | vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, vmx->msr_autoload.host.nr); |
| 4539 | vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, vmx->msr_autoload.guest.nr); |
| 4540 | vmcs_write64(TSC_OFFSET, vcpu->arch.tsc_offset); |
Ilias Stamatis | 1ab9287 | 2021-06-07 11:54:38 +0100 | [diff] [blame] | 4541 | if (kvm_has_tsc_control) |
| 4542 | vmcs_write64(TSC_MULTIPLIER, vcpu->arch.tsc_scaling_ratio); |
| 4543 | |
Liran Alon | 02d496cf | 2019-11-11 14:30:55 +0200 | [diff] [blame] | 4544 | if (vmx->nested.l1_tpr_threshold != -1) |
| 4545 | vmcs_write32(TPR_THRESHOLD, vmx->nested.l1_tpr_threshold); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4546 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4547 | if (vmx->nested.change_vmcs01_virtual_apic_mode) { |
| 4548 | vmx->nested.change_vmcs01_virtual_apic_mode = false; |
| 4549 | vmx_set_virtual_apic_mode(vcpu); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4550 | } |
| 4551 | |
Makarand Sonare | a85863c | 2021-02-12 16:50:12 -0800 | [diff] [blame] | 4552 | if (vmx->nested.update_vmcs01_cpu_dirty_logging) { |
| 4553 | vmx->nested.update_vmcs01_cpu_dirty_logging = false; |
| 4554 | vmx_update_cpu_dirty_logging(vcpu); |
| 4555 | } |
| 4556 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4557 | /* Unpin physical memory we referred to in vmcs02 */ |
| 4558 | if (vmx->nested.apic_access_page) { |
Liran Alon | b11494b | 2019-11-21 00:31:47 +0200 | [diff] [blame] | 4559 | kvm_release_page_clean(vmx->nested.apic_access_page); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4560 | vmx->nested.apic_access_page = NULL; |
| 4561 | } |
KarimAllah Ahmed | 96c66e8 | 2019-01-31 21:24:37 +0100 | [diff] [blame] | 4562 | kvm_vcpu_unmap(vcpu, &vmx->nested.virtual_apic_map, true); |
KarimAllah Ahmed | 3278e04 | 2019-01-31 21:24:38 +0100 | [diff] [blame] | 4563 | kvm_vcpu_unmap(vcpu, &vmx->nested.pi_desc_map, true); |
| 4564 | vmx->nested.pi_desc = NULL; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4565 | |
Sean Christopherson | 1196cb9 | 2020-03-20 14:28:23 -0700 | [diff] [blame] | 4566 | if (vmx->nested.reload_vmcs01_apic_access_page) { |
| 4567 | vmx->nested.reload_vmcs01_apic_access_page = false; |
| 4568 | kvm_make_request(KVM_REQ_APIC_PAGE_RELOAD, vcpu); |
| 4569 | } |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4570 | |
Sean Christopherson | 4dcefa3 | 2020-04-15 10:55:18 -0700 | [diff] [blame] | 4571 | if ((vm_exit_reason != -1) && |
Vitaly Kuznetsov | 1e9dfbd | 2021-05-26 15:20:16 +0200 | [diff] [blame] | 4572 | (enable_shadow_vmcs || evmptr_is_valid(vmx->nested.hv_evmcs_vmptr))) |
Sean Christopherson | 3731905ef | 2019-05-07 08:36:27 -0700 | [diff] [blame] | 4573 | vmx->nested.need_vmcs12_to_shadow_sync = true; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4574 | |
| 4575 | /* in case we halted in L2 */ |
| 4576 | vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE; |
| 4577 | |
| 4578 | if (likely(!vmx->fail)) { |
Sean Christopherson | 4dcefa3 | 2020-04-15 10:55:18 -0700 | [diff] [blame] | 4579 | if ((u16)vm_exit_reason == EXIT_REASON_EXTERNAL_INTERRUPT && |
Sean Christopherson | a1c77ab | 2020-03-02 22:27:35 -0800 | [diff] [blame] | 4580 | nested_exit_intr_ack_set(vcpu)) { |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4581 | int irq = kvm_cpu_get_interrupt(vcpu); |
| 4582 | WARN_ON(irq < 0); |
| 4583 | vmcs12->vm_exit_intr_info = irq | |
| 4584 | INTR_INFO_VALID_MASK | INTR_TYPE_EXT_INTR; |
| 4585 | } |
| 4586 | |
Sean Christopherson | 4dcefa3 | 2020-04-15 10:55:18 -0700 | [diff] [blame] | 4587 | if (vm_exit_reason != -1) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4588 | trace_kvm_nested_vmexit_inject(vmcs12->vm_exit_reason, |
| 4589 | vmcs12->exit_qualification, |
| 4590 | vmcs12->idt_vectoring_info_field, |
| 4591 | vmcs12->vm_exit_intr_info, |
| 4592 | vmcs12->vm_exit_intr_error_code, |
| 4593 | KVM_ISA_VMX); |
| 4594 | |
| 4595 | load_vmcs12_host_state(vcpu, vmcs12); |
| 4596 | |
| 4597 | return; |
| 4598 | } |
| 4599 | |
| 4600 | /* |
| 4601 | * After an early L2 VM-entry failure, we're now back |
| 4602 | * in L1 which thinks it just finished a VMLAUNCH or |
| 4603 | * VMRESUME instruction, so we need to set the failure |
| 4604 | * flag and the VM-instruction error field of the VMCS |
| 4605 | * accordingly, and skip the emulated instruction. |
| 4606 | */ |
Sean Christopherson | b2656e4 | 2020-06-08 18:56:07 -0700 | [diff] [blame] | 4607 | (void)nested_vmx_fail(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4608 | |
| 4609 | /* |
| 4610 | * Restore L1's host state to KVM's software model. We're here |
| 4611 | * because a consistency check was caught by hardware, which |
| 4612 | * means some amount of guest state has been propagated to KVM's |
| 4613 | * model and needs to be unwound to the host's state. |
| 4614 | */ |
| 4615 | nested_vmx_restore_host_state(vcpu); |
| 4616 | |
| 4617 | vmx->fail = 0; |
| 4618 | } |
| 4619 | |
Sean Christopherson | cb6a32c | 2021-03-02 09:45:14 -0800 | [diff] [blame] | 4620 | static void nested_vmx_triple_fault(struct kvm_vcpu *vcpu) |
| 4621 | { |
| 4622 | nested_vmx_vmexit(vcpu, EXIT_REASON_TRIPLE_FAULT, 0, 0); |
| 4623 | } |
| 4624 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4625 | /* |
| 4626 | * Decode the memory-address operand of a vmx instruction, as recorded on an |
| 4627 | * exit caused by such an instruction (run by a guest hypervisor). |
| 4628 | * 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] | 4629 | * #UD, #GP, or #SS. |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4630 | */ |
| 4631 | 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] | 4632 | u32 vmx_instruction_info, bool wr, int len, gva_t *ret) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4633 | { |
| 4634 | gva_t off; |
| 4635 | bool exn; |
| 4636 | struct kvm_segment s; |
| 4637 | |
| 4638 | /* |
| 4639 | * According to Vol. 3B, "Information for VM Exits Due to Instruction |
| 4640 | * Execution", on an exit, vmx_instruction_info holds most of the |
| 4641 | * addressing components of the operand. Only the displacement part |
| 4642 | * is put in exit_qualification (see 3B, "Basic VM-Exit Information"). |
| 4643 | * For how an actual address is calculated from all these components, |
| 4644 | * refer to Vol. 1, "Operand Addressing". |
| 4645 | */ |
| 4646 | int scaling = vmx_instruction_info & 3; |
| 4647 | int addr_size = (vmx_instruction_info >> 7) & 7; |
| 4648 | bool is_reg = vmx_instruction_info & (1u << 10); |
| 4649 | int seg_reg = (vmx_instruction_info >> 15) & 7; |
| 4650 | int index_reg = (vmx_instruction_info >> 18) & 0xf; |
| 4651 | bool index_is_valid = !(vmx_instruction_info & (1u << 22)); |
| 4652 | int base_reg = (vmx_instruction_info >> 23) & 0xf; |
| 4653 | bool base_is_valid = !(vmx_instruction_info & (1u << 27)); |
| 4654 | |
| 4655 | if (is_reg) { |
| 4656 | kvm_queue_exception(vcpu, UD_VECTOR); |
| 4657 | return 1; |
| 4658 | } |
| 4659 | |
| 4660 | /* Addr = segment_base + offset */ |
| 4661 | /* offset = base + [index * scale] + displacement */ |
| 4662 | off = exit_qualification; /* holds the displacement */ |
Sean Christopherson | 946c522 | 2019-01-23 14:39:23 -0800 | [diff] [blame] | 4663 | if (addr_size == 1) |
| 4664 | off = (gva_t)sign_extend64(off, 31); |
| 4665 | else if (addr_size == 0) |
| 4666 | off = (gva_t)sign_extend64(off, 15); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4667 | if (base_is_valid) |
| 4668 | off += kvm_register_read(vcpu, base_reg); |
| 4669 | if (index_is_valid) |
Miaohe Lin | e630269 | 2020-02-15 10:44:22 +0800 | [diff] [blame] | 4670 | off += kvm_register_read(vcpu, index_reg) << scaling; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4671 | vmx_get_segment(vcpu, &s, seg_reg); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4672 | |
Sean Christopherson | 8570f9e | 2019-01-23 14:39:24 -0800 | [diff] [blame] | 4673 | /* |
| 4674 | * The effective address, i.e. @off, of a memory operand is truncated |
| 4675 | * based on the address size of the instruction. Note that this is |
| 4676 | * the *effective address*, i.e. the address prior to accounting for |
| 4677 | * the segment's base. |
| 4678 | */ |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4679 | if (addr_size == 1) /* 32 bit */ |
Sean Christopherson | 8570f9e | 2019-01-23 14:39:24 -0800 | [diff] [blame] | 4680 | off &= 0xffffffff; |
| 4681 | else if (addr_size == 0) /* 16 bit */ |
| 4682 | off &= 0xffff; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4683 | |
| 4684 | /* Checks for #GP/#SS exceptions. */ |
| 4685 | exn = false; |
| 4686 | if (is_long_mode(vcpu)) { |
Sean Christopherson | 8570f9e | 2019-01-23 14:39:24 -0800 | [diff] [blame] | 4687 | /* |
| 4688 | * The virtual/linear address is never truncated in 64-bit |
| 4689 | * mode, e.g. a 32-bit address size can yield a 64-bit virtual |
| 4690 | * address when using FS/GS with a non-zero base. |
| 4691 | */ |
Liran Alon | 6694e48 | 2019-07-15 18:47:44 +0300 | [diff] [blame] | 4692 | if (seg_reg == VCPU_SREG_FS || seg_reg == VCPU_SREG_GS) |
| 4693 | *ret = s.base + off; |
| 4694 | else |
| 4695 | *ret = off; |
Sean Christopherson | 8570f9e | 2019-01-23 14:39:24 -0800 | [diff] [blame] | 4696 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4697 | /* Long mode: #GP(0)/#SS(0) if the memory address is in a |
| 4698 | * non-canonical form. This is the only check on the memory |
| 4699 | * destination for long mode! |
| 4700 | */ |
| 4701 | exn = is_noncanonical_address(*ret, vcpu); |
Paolo Bonzini | e0dfacb | 2019-01-30 17:25:38 +0100 | [diff] [blame] | 4702 | } else { |
Sean Christopherson | 8570f9e | 2019-01-23 14:39:24 -0800 | [diff] [blame] | 4703 | /* |
| 4704 | * When not in long mode, the virtual/linear address is |
| 4705 | * unconditionally truncated to 32 bits regardless of the |
| 4706 | * address size. |
| 4707 | */ |
| 4708 | *ret = (s.base + off) & 0xffffffff; |
| 4709 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4710 | /* Protected mode: apply checks for segment validity in the |
| 4711 | * following order: |
| 4712 | * - segment type check (#GP(0) may be thrown) |
| 4713 | * - usability check (#GP(0)/#SS(0)) |
| 4714 | * - limit check (#GP(0)/#SS(0)) |
| 4715 | */ |
| 4716 | if (wr) |
| 4717 | /* #GP(0) if the destination operand is located in a |
| 4718 | * read-only data segment or any code segment. |
| 4719 | */ |
| 4720 | exn = ((s.type & 0xa) == 0 || (s.type & 8)); |
| 4721 | else |
| 4722 | /* #GP(0) if the source operand is located in an |
| 4723 | * execute-only code segment |
| 4724 | */ |
| 4725 | exn = ((s.type & 0xa) == 8); |
| 4726 | if (exn) { |
| 4727 | kvm_queue_exception_e(vcpu, GP_VECTOR, 0); |
| 4728 | return 1; |
| 4729 | } |
| 4730 | /* Protected mode: #GP(0)/#SS(0) if the segment is unusable. |
| 4731 | */ |
| 4732 | exn = (s.unusable != 0); |
Sean Christopherson | 34333cc | 2019-01-23 14:39:25 -0800 | [diff] [blame] | 4733 | |
| 4734 | /* |
| 4735 | * Protected mode: #GP(0)/#SS(0) if the memory operand is |
| 4736 | * outside the segment limit. All CPUs that support VMX ignore |
| 4737 | * limit checks for flat segments, i.e. segments with base==0, |
| 4738 | * limit==0xffffffff and of type expand-up data or code. |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4739 | */ |
Sean Christopherson | 34333cc | 2019-01-23 14:39:25 -0800 | [diff] [blame] | 4740 | if (!(s.base == 0 && s.limit == 0xffffffff && |
| 4741 | ((s.type & 8) || !(s.type & 4)))) |
Eugene Korenevsky | fdb2861 | 2019-06-06 00:19:16 +0300 | [diff] [blame] | 4742 | exn = exn || ((u64)off + len - 1 > s.limit); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4743 | } |
| 4744 | if (exn) { |
| 4745 | kvm_queue_exception_e(vcpu, |
| 4746 | seg_reg == VCPU_SREG_SS ? |
| 4747 | SS_VECTOR : GP_VECTOR, |
| 4748 | 0); |
| 4749 | return 1; |
| 4750 | } |
| 4751 | |
| 4752 | return 0; |
| 4753 | } |
| 4754 | |
Oliver Upton | 03a8871a | 2019-11-13 16:17:20 -0800 | [diff] [blame] | 4755 | void nested_vmx_pmu_entry_exit_ctls_update(struct kvm_vcpu *vcpu) |
| 4756 | { |
| 4757 | struct vcpu_vmx *vmx; |
| 4758 | |
| 4759 | if (!nested_vmx_allowed(vcpu)) |
| 4760 | return; |
| 4761 | |
| 4762 | vmx = to_vmx(vcpu); |
Sean Christopherson | afaf0b2 | 2020-03-21 13:26:00 -0700 | [diff] [blame] | 4763 | 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] | 4764 | vmx->nested.msrs.entry_ctls_high |= |
| 4765 | VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL; |
| 4766 | vmx->nested.msrs.exit_ctls_high |= |
| 4767 | VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL; |
| 4768 | } else { |
| 4769 | vmx->nested.msrs.entry_ctls_high &= |
| 4770 | ~VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL; |
| 4771 | vmx->nested.msrs.exit_ctls_high &= |
Chenyi Qiang | c6b177a | 2020-08-28 16:56:21 +0800 | [diff] [blame] | 4772 | ~VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL; |
Oliver Upton | 03a8871a | 2019-11-13 16:17:20 -0800 | [diff] [blame] | 4773 | } |
| 4774 | } |
| 4775 | |
Vitaly Kuznetsov | 7a35e51 | 2020-06-05 13:59:05 +0200 | [diff] [blame] | 4776 | static int nested_vmx_get_vmptr(struct kvm_vcpu *vcpu, gpa_t *vmpointer, |
| 4777 | int *ret) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4778 | { |
| 4779 | gva_t gva; |
| 4780 | struct x86_exception e; |
Vitaly Kuznetsov | 7a35e51 | 2020-06-05 13:59:05 +0200 | [diff] [blame] | 4781 | int r; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4782 | |
Sean Christopherson | 5addc23 | 2020-04-15 13:34:53 -0700 | [diff] [blame] | 4783 | if (get_vmx_mem_address(vcpu, vmx_get_exit_qual(vcpu), |
Eugene Korenevsky | fdb2861 | 2019-06-06 00:19:16 +0300 | [diff] [blame] | 4784 | vmcs_read32(VMX_INSTRUCTION_INFO), false, |
Vitaly Kuznetsov | 7a35e51 | 2020-06-05 13:59:05 +0200 | [diff] [blame] | 4785 | sizeof(*vmpointer), &gva)) { |
| 4786 | *ret = 1; |
| 4787 | return -EINVAL; |
| 4788 | } |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4789 | |
Vitaly Kuznetsov | 7a35e51 | 2020-06-05 13:59:05 +0200 | [diff] [blame] | 4790 | r = kvm_read_guest_virt(vcpu, gva, vmpointer, sizeof(*vmpointer), &e); |
| 4791 | if (r != X86EMUL_CONTINUE) { |
Babu Moger | 3f3393b | 2020-09-11 14:29:05 -0500 | [diff] [blame] | 4792 | *ret = kvm_handle_memory_failure(vcpu, r, &e); |
Vitaly Kuznetsov | 7a35e51 | 2020-06-05 13:59:05 +0200 | [diff] [blame] | 4793 | return -EINVAL; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4794 | } |
| 4795 | |
| 4796 | return 0; |
| 4797 | } |
| 4798 | |
| 4799 | /* |
| 4800 | * Allocate a shadow VMCS and associate it with the currently loaded |
| 4801 | * VMCS, unless such a shadow VMCS already exists. The newly allocated |
| 4802 | * VMCS is also VMCLEARed, so that it is ready for use. |
| 4803 | */ |
| 4804 | static struct vmcs *alloc_shadow_vmcs(struct kvm_vcpu *vcpu) |
| 4805 | { |
| 4806 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 4807 | struct loaded_vmcs *loaded_vmcs = vmx->loaded_vmcs; |
| 4808 | |
| 4809 | /* |
| 4810 | * We should allocate a shadow vmcs for vmcs01 only when L1 |
| 4811 | * executes VMXON and free it when L1 executes VMXOFF. |
| 4812 | * As it is invalid to execute VMXON twice, we shouldn't reach |
| 4813 | * here when vmcs01 already have an allocated shadow vmcs. |
| 4814 | */ |
| 4815 | WARN_ON(loaded_vmcs == &vmx->vmcs01 && loaded_vmcs->shadow_vmcs); |
| 4816 | |
| 4817 | if (!loaded_vmcs->shadow_vmcs) { |
| 4818 | loaded_vmcs->shadow_vmcs = alloc_vmcs(true); |
| 4819 | if (loaded_vmcs->shadow_vmcs) |
| 4820 | vmcs_clear(loaded_vmcs->shadow_vmcs); |
| 4821 | } |
| 4822 | return loaded_vmcs->shadow_vmcs; |
| 4823 | } |
| 4824 | |
| 4825 | static int enter_vmx_operation(struct kvm_vcpu *vcpu) |
| 4826 | { |
| 4827 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 4828 | int r; |
| 4829 | |
| 4830 | r = alloc_loaded_vmcs(&vmx->nested.vmcs02); |
| 4831 | if (r < 0) |
| 4832 | goto out_vmcs02; |
| 4833 | |
Ben Gardon | 4183683 | 2019-02-11 11:02:52 -0800 | [diff] [blame] | 4834 | vmx->nested.cached_vmcs12 = kzalloc(VMCS12_SIZE, GFP_KERNEL_ACCOUNT); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4835 | if (!vmx->nested.cached_vmcs12) |
| 4836 | goto out_cached_vmcs12; |
| 4837 | |
Ben Gardon | 4183683 | 2019-02-11 11:02:52 -0800 | [diff] [blame] | 4838 | vmx->nested.cached_shadow_vmcs12 = kzalloc(VMCS12_SIZE, GFP_KERNEL_ACCOUNT); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4839 | if (!vmx->nested.cached_shadow_vmcs12) |
| 4840 | goto out_cached_shadow_vmcs12; |
| 4841 | |
| 4842 | if (enable_shadow_vmcs && !alloc_shadow_vmcs(vcpu)) |
| 4843 | goto out_shadow_vmcs; |
| 4844 | |
| 4845 | hrtimer_init(&vmx->nested.preemption_timer, CLOCK_MONOTONIC, |
Jim Mattson | ada0098 | 2020-05-08 13:36:42 -0700 | [diff] [blame] | 4846 | HRTIMER_MODE_ABS_PINNED); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4847 | vmx->nested.preemption_timer.function = vmx_preemption_timer_fn; |
| 4848 | |
| 4849 | vmx->nested.vpid02 = allocate_vpid(); |
| 4850 | |
| 4851 | vmx->nested.vmcs02_initialized = false; |
| 4852 | vmx->nested.vmxon = true; |
Luwei Kang | ee85dec | 2018-10-24 16:05:16 +0800 | [diff] [blame] | 4853 | |
Sean Christopherson | 2ef7619 | 2020-03-02 15:56:22 -0800 | [diff] [blame] | 4854 | if (vmx_pt_mode_is_host_guest()) { |
Luwei Kang | ee85dec | 2018-10-24 16:05:16 +0800 | [diff] [blame] | 4855 | vmx->pt_desc.guest.ctl = 0; |
Aaron Lewis | 476c9bd | 2020-09-25 16:34:18 +0200 | [diff] [blame] | 4856 | pt_update_intercept_for_msr(vcpu); |
Luwei Kang | ee85dec | 2018-10-24 16:05:16 +0800 | [diff] [blame] | 4857 | } |
| 4858 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4859 | return 0; |
| 4860 | |
| 4861 | out_shadow_vmcs: |
| 4862 | kfree(vmx->nested.cached_shadow_vmcs12); |
| 4863 | |
| 4864 | out_cached_shadow_vmcs12: |
| 4865 | kfree(vmx->nested.cached_vmcs12); |
| 4866 | |
| 4867 | out_cached_vmcs12: |
| 4868 | free_loaded_vmcs(&vmx->nested.vmcs02); |
| 4869 | |
| 4870 | out_vmcs02: |
| 4871 | return -ENOMEM; |
| 4872 | } |
| 4873 | |
Yu Zhang | ed7023a | 2021-09-09 01:17:31 +0800 | [diff] [blame] | 4874 | /* Emulate the VMXON instruction. */ |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4875 | static int handle_vmon(struct kvm_vcpu *vcpu) |
| 4876 | { |
| 4877 | int ret; |
| 4878 | gpa_t vmptr; |
KarimAllah Ahmed | 2e40893 | 2019-01-31 21:24:31 +0100 | [diff] [blame] | 4879 | uint32_t revision; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4880 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Sean Christopherson | 32ad73d | 2019-12-20 20:44:55 -0800 | [diff] [blame] | 4881 | const u64 VMXON_NEEDED_FEATURES = FEAT_CTL_LOCKED |
| 4882 | | FEAT_CTL_VMX_ENABLED_OUTSIDE_SMX; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4883 | |
| 4884 | /* |
| 4885 | * The Intel VMX Instruction Reference lists a bunch of bits that are |
| 4886 | * prerequisite to running VMXON, most notably cr4.VMXE must be set to |
Sean Christopherson | c2fe3cd | 2020-10-06 18:44:15 -0700 | [diff] [blame] | 4887 | * 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] | 4888 | * Otherwise, we should fail with #UD. But most faulting conditions |
| 4889 | * have already been checked by hardware, prior to the VM-exit for |
| 4890 | * VMXON. We do test guest cr4.VMXE because processor CR4 always has |
| 4891 | * that bit set to 1 in non-root mode. |
| 4892 | */ |
| 4893 | if (!kvm_read_cr4_bits(vcpu, X86_CR4_VMXE)) { |
| 4894 | kvm_queue_exception(vcpu, UD_VECTOR); |
| 4895 | return 1; |
| 4896 | } |
| 4897 | |
| 4898 | /* CPL=0 must be checked manually. */ |
| 4899 | if (vmx_get_cpl(vcpu)) { |
| 4900 | kvm_inject_gp(vcpu, 0); |
| 4901 | return 1; |
| 4902 | } |
| 4903 | |
| 4904 | if (vmx->nested.vmxon) |
Sean Christopherson | b2656e4 | 2020-06-08 18:56:07 -0700 | [diff] [blame] | 4905 | return nested_vmx_fail(vcpu, VMXERR_VMXON_IN_VMX_ROOT_OPERATION); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4906 | |
| 4907 | if ((vmx->msr_ia32_feature_control & VMXON_NEEDED_FEATURES) |
| 4908 | != VMXON_NEEDED_FEATURES) { |
| 4909 | kvm_inject_gp(vcpu, 0); |
| 4910 | return 1; |
| 4911 | } |
| 4912 | |
Vitaly Kuznetsov | 7a35e51 | 2020-06-05 13:59:05 +0200 | [diff] [blame] | 4913 | if (nested_vmx_get_vmptr(vcpu, &vmptr, &ret)) |
| 4914 | return ret; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4915 | |
| 4916 | /* |
| 4917 | * SDM 3: 24.11.5 |
| 4918 | * The first 4 bytes of VMXON region contain the supported |
| 4919 | * VMCS revision identifier |
| 4920 | * |
| 4921 | * Note - IA32_VMX_BASIC[48] will never be 1 for the nested case; |
| 4922 | * which replaces physical address width with 32 |
| 4923 | */ |
KarimAllah Ahmed | e0bf266 | 2019-01-31 21:24:43 +0100 | [diff] [blame] | 4924 | if (!page_address_valid(vcpu, vmptr)) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4925 | return nested_vmx_failInvalid(vcpu); |
| 4926 | |
KarimAllah Ahmed | 2e40893 | 2019-01-31 21:24:31 +0100 | [diff] [blame] | 4927 | if (kvm_read_guest(vcpu->kvm, vmptr, &revision, sizeof(revision)) || |
| 4928 | revision != VMCS12_REVISION) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4929 | return nested_vmx_failInvalid(vcpu); |
| 4930 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4931 | vmx->nested.vmxon_ptr = vmptr; |
| 4932 | ret = enter_vmx_operation(vcpu); |
| 4933 | if (ret) |
| 4934 | return ret; |
| 4935 | |
| 4936 | return nested_vmx_succeed(vcpu); |
| 4937 | } |
| 4938 | |
| 4939 | static inline void nested_release_vmcs12(struct kvm_vcpu *vcpu) |
| 4940 | { |
| 4941 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 4942 | |
Yu Zhang | 64c7850 | 2021-09-30 01:51:53 +0800 | [diff] [blame] | 4943 | if (vmx->nested.current_vmptr == INVALID_GPA) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4944 | return; |
| 4945 | |
Sean Christopherson | 7952d76 | 2019-05-07 08:36:29 -0700 | [diff] [blame] | 4946 | copy_vmcs02_to_vmcs12_rare(vcpu, get_vmcs12(vcpu)); |
| 4947 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4948 | if (enable_shadow_vmcs) { |
| 4949 | /* copy to memory all shadowed fields in case |
| 4950 | they were modified */ |
| 4951 | copy_shadow_to_vmcs12(vmx); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4952 | vmx_disable_shadow_vmcs(vmx); |
| 4953 | } |
| 4954 | vmx->nested.posted_intr_nv = -1; |
| 4955 | |
| 4956 | /* Flush VMCS12 to guest memory */ |
| 4957 | kvm_vcpu_write_guest_page(vcpu, |
| 4958 | vmx->nested.current_vmptr >> PAGE_SHIFT, |
| 4959 | vmx->nested.cached_vmcs12, 0, VMCS12_SIZE); |
| 4960 | |
| 4961 | kvm_mmu_free_roots(vcpu, &vcpu->arch.guest_mmu, KVM_MMU_ROOTS_ALL); |
| 4962 | |
Yu Zhang | 64c7850 | 2021-09-30 01:51:53 +0800 | [diff] [blame] | 4963 | vmx->nested.current_vmptr = INVALID_GPA; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4964 | } |
| 4965 | |
| 4966 | /* Emulate the VMXOFF instruction */ |
| 4967 | static int handle_vmoff(struct kvm_vcpu *vcpu) |
| 4968 | { |
| 4969 | if (!nested_vmx_check_permission(vcpu)) |
| 4970 | return 1; |
Liran Alon | 4b9852f | 2019-08-26 13:24:49 +0300 | [diff] [blame] | 4971 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4972 | free_nested(vcpu); |
Liran Alon | 4b9852f | 2019-08-26 13:24:49 +0300 | [diff] [blame] | 4973 | |
| 4974 | /* Process a latched INIT during time CPU was in VMX operation */ |
| 4975 | kvm_make_request(KVM_REQ_EVENT, vcpu); |
| 4976 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4977 | return nested_vmx_succeed(vcpu); |
| 4978 | } |
| 4979 | |
| 4980 | /* Emulate the VMCLEAR instruction */ |
| 4981 | static int handle_vmclear(struct kvm_vcpu *vcpu) |
| 4982 | { |
| 4983 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 4984 | u32 zero = 0; |
| 4985 | gpa_t vmptr; |
Vitaly Kuznetsov | 11e3491 | 2019-06-28 13:23:33 +0200 | [diff] [blame] | 4986 | u64 evmcs_gpa; |
Vitaly Kuznetsov | 7a35e51 | 2020-06-05 13:59:05 +0200 | [diff] [blame] | 4987 | int r; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4988 | |
| 4989 | if (!nested_vmx_check_permission(vcpu)) |
| 4990 | return 1; |
| 4991 | |
Vitaly Kuznetsov | 7a35e51 | 2020-06-05 13:59:05 +0200 | [diff] [blame] | 4992 | if (nested_vmx_get_vmptr(vcpu, &vmptr, &r)) |
| 4993 | return r; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4994 | |
KarimAllah Ahmed | e0bf266 | 2019-01-31 21:24:43 +0100 | [diff] [blame] | 4995 | if (!page_address_valid(vcpu, vmptr)) |
Sean Christopherson | b2656e4 | 2020-06-08 18:56:07 -0700 | [diff] [blame] | 4996 | return nested_vmx_fail(vcpu, VMXERR_VMCLEAR_INVALID_ADDRESS); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4997 | |
| 4998 | if (vmptr == vmx->nested.vmxon_ptr) |
Sean Christopherson | b2656e4 | 2020-06-08 18:56:07 -0700 | [diff] [blame] | 4999 | return nested_vmx_fail(vcpu, VMXERR_VMCLEAR_VMXON_POINTER); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5000 | |
Vitaly Kuznetsov | 11e3491 | 2019-06-28 13:23:33 +0200 | [diff] [blame] | 5001 | /* |
| 5002 | * When Enlightened VMEntry is enabled on the calling CPU we treat |
| 5003 | * memory area pointer by vmptr as Enlightened VMCS (as there's no good |
| 5004 | * way to distinguish it from VMCS12) and we must not corrupt it by |
| 5005 | * writing to the non-existent 'launch_state' field. The area doesn't |
| 5006 | * have to be the currently active EVMCS on the calling CPU and there's |
| 5007 | * nothing KVM has to do to transition it from 'active' to 'non-active' |
| 5008 | * state. It is possible that the area will stay mapped as |
| 5009 | * vmx->nested.hv_evmcs but this shouldn't be a problem. |
| 5010 | */ |
| 5011 | if (likely(!vmx->nested.enlightened_vmcs_enabled || |
| 5012 | !nested_enlightened_vmentry(vcpu, &evmcs_gpa))) { |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5013 | if (vmptr == vmx->nested.current_vmptr) |
| 5014 | nested_release_vmcs12(vcpu); |
| 5015 | |
| 5016 | kvm_vcpu_write_guest(vcpu, |
| 5017 | vmptr + offsetof(struct vmcs12, |
| 5018 | launch_state), |
| 5019 | &zero, sizeof(zero)); |
Vitaly Kuznetsov | 3b19b81 | 2021-05-26 15:20:21 +0200 | [diff] [blame] | 5020 | } else if (vmx->nested.hv_evmcs && vmptr == vmx->nested.hv_evmcs_vmptr) { |
| 5021 | nested_release_evmcs(vcpu); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5022 | } |
| 5023 | |
| 5024 | return nested_vmx_succeed(vcpu); |
| 5025 | } |
| 5026 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5027 | /* Emulate the VMLAUNCH instruction */ |
| 5028 | static int handle_vmlaunch(struct kvm_vcpu *vcpu) |
| 5029 | { |
| 5030 | return nested_vmx_run(vcpu, true); |
| 5031 | } |
| 5032 | |
| 5033 | /* Emulate the VMRESUME instruction */ |
| 5034 | static int handle_vmresume(struct kvm_vcpu *vcpu) |
| 5035 | { |
| 5036 | |
| 5037 | return nested_vmx_run(vcpu, false); |
| 5038 | } |
| 5039 | |
| 5040 | static int handle_vmread(struct kvm_vcpu *vcpu) |
| 5041 | { |
Jim Mattson | dd2d604 | 2019-12-06 15:46:35 -0800 | [diff] [blame] | 5042 | struct vmcs12 *vmcs12 = is_guest_mode(vcpu) ? get_shadow_vmcs12(vcpu) |
| 5043 | : get_vmcs12(vcpu); |
Sean Christopherson | 5addc23 | 2020-04-15 13:34:53 -0700 | [diff] [blame] | 5044 | unsigned long exit_qualification = vmx_get_exit_qual(vcpu); |
Jim Mattson | c90f4d0 | 2019-12-06 15:46:37 -0800 | [diff] [blame] | 5045 | u32 instr_info = vmcs_read32(VMX_INSTRUCTION_INFO); |
| 5046 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Paolo Bonzini | f7eea63 | 2019-09-14 00:26:27 +0200 | [diff] [blame] | 5047 | struct x86_exception e; |
Jim Mattson | c90f4d0 | 2019-12-06 15:46:37 -0800 | [diff] [blame] | 5048 | unsigned long field; |
| 5049 | u64 value; |
| 5050 | gva_t gva = 0; |
Sean Christopherson | 1c6f0b4 | 2019-05-07 08:36:25 -0700 | [diff] [blame] | 5051 | short offset; |
Vitaly Kuznetsov | 7a35e51 | 2020-06-05 13:59:05 +0200 | [diff] [blame] | 5052 | int len, r; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5053 | |
| 5054 | if (!nested_vmx_check_permission(vcpu)) |
| 5055 | return 1; |
| 5056 | |
Jim Mattson | dd2d604 | 2019-12-06 15:46:35 -0800 | [diff] [blame] | 5057 | /* |
Yu Zhang | 64c7850 | 2021-09-30 01:51:53 +0800 | [diff] [blame] | 5058 | * 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] | 5059 | * any VMREAD sets the ALU flags for VMfailInvalid. |
| 5060 | */ |
Yu Zhang | 64c7850 | 2021-09-30 01:51:53 +0800 | [diff] [blame] | 5061 | if (vmx->nested.current_vmptr == INVALID_GPA || |
Jim Mattson | dd2d604 | 2019-12-06 15:46:35 -0800 | [diff] [blame] | 5062 | (is_guest_mode(vcpu) && |
Yu Zhang | 64c7850 | 2021-09-30 01:51:53 +0800 | [diff] [blame] | 5063 | get_vmcs12(vcpu)->vmcs_link_pointer == INVALID_GPA)) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5064 | return nested_vmx_failInvalid(vcpu); |
| 5065 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5066 | /* Decode instruction info and find the field to read */ |
Sean Christopherson | 27b4a9c4 | 2021-04-21 19:21:28 -0700 | [diff] [blame] | 5067 | field = kvm_register_read(vcpu, (((instr_info) >> 28) & 0xf)); |
Sean Christopherson | 1c6f0b4 | 2019-05-07 08:36:25 -0700 | [diff] [blame] | 5068 | |
| 5069 | offset = vmcs_field_to_offset(field); |
| 5070 | if (offset < 0) |
Sean Christopherson | b2656e4 | 2020-06-08 18:56:07 -0700 | [diff] [blame] | 5071 | return nested_vmx_fail(vcpu, VMXERR_UNSUPPORTED_VMCS_COMPONENT); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5072 | |
Sean Christopherson | 7952d76 | 2019-05-07 08:36:29 -0700 | [diff] [blame] | 5073 | if (!is_guest_mode(vcpu) && is_vmcs12_ext_field(field)) |
| 5074 | copy_vmcs02_to_vmcs12_rare(vcpu, vmcs12); |
| 5075 | |
Jim Mattson | c90f4d0 | 2019-12-06 15:46:37 -0800 | [diff] [blame] | 5076 | /* Read the field, zero-extended to a u64 value */ |
| 5077 | value = vmcs12_read_any(vmcs12, field, offset); |
Sean Christopherson | 1c6f0b4 | 2019-05-07 08:36:25 -0700 | [diff] [blame] | 5078 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5079 | /* |
| 5080 | * Now copy part of this value to register or memory, as requested. |
| 5081 | * Note that the number of bits actually copied is 32 or 64 depending |
| 5082 | * on the guest's mode (32 or 64 bit), not on the given field's length. |
| 5083 | */ |
Jim Mattson | c90f4d0 | 2019-12-06 15:46:37 -0800 | [diff] [blame] | 5084 | if (instr_info & BIT(10)) { |
Sean Christopherson | 27b4a9c4 | 2021-04-21 19:21:28 -0700 | [diff] [blame] | 5085 | kvm_register_write(vcpu, (((instr_info) >> 3) & 0xf), value); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5086 | } else { |
Eugene Korenevsky | fdb2861 | 2019-06-06 00:19:16 +0300 | [diff] [blame] | 5087 | len = is_64_bit_mode(vcpu) ? 8 : 4; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5088 | if (get_vmx_mem_address(vcpu, exit_qualification, |
Jim Mattson | c90f4d0 | 2019-12-06 15:46:37 -0800 | [diff] [blame] | 5089 | instr_info, true, len, &gva)) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5090 | return 1; |
| 5091 | /* _system ok, nested_vmx_check_permission has verified cpl=0 */ |
Vitaly Kuznetsov | 7a35e51 | 2020-06-05 13:59:05 +0200 | [diff] [blame] | 5092 | r = kvm_write_guest_virt_system(vcpu, gva, &value, len, &e); |
| 5093 | if (r != X86EMUL_CONTINUE) |
Babu Moger | 3f3393b | 2020-09-11 14:29:05 -0500 | [diff] [blame] | 5094 | return kvm_handle_memory_failure(vcpu, r, &e); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5095 | } |
| 5096 | |
| 5097 | return nested_vmx_succeed(vcpu); |
| 5098 | } |
| 5099 | |
Sean Christopherson | e217429 | 2019-05-07 08:36:28 -0700 | [diff] [blame] | 5100 | static bool is_shadow_field_rw(unsigned long field) |
| 5101 | { |
| 5102 | switch (field) { |
| 5103 | #define SHADOW_FIELD_RW(x, y) case x: |
| 5104 | #include "vmcs_shadow_fields.h" |
| 5105 | return true; |
| 5106 | default: |
| 5107 | break; |
| 5108 | } |
| 5109 | return false; |
| 5110 | } |
| 5111 | |
| 5112 | static bool is_shadow_field_ro(unsigned long field) |
| 5113 | { |
| 5114 | switch (field) { |
| 5115 | #define SHADOW_FIELD_RO(x, y) case x: |
| 5116 | #include "vmcs_shadow_fields.h" |
| 5117 | return true; |
| 5118 | default: |
| 5119 | break; |
| 5120 | } |
| 5121 | return false; |
| 5122 | } |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5123 | |
| 5124 | static int handle_vmwrite(struct kvm_vcpu *vcpu) |
| 5125 | { |
Jim Mattson | dd2d604 | 2019-12-06 15:46:35 -0800 | [diff] [blame] | 5126 | struct vmcs12 *vmcs12 = is_guest_mode(vcpu) ? get_shadow_vmcs12(vcpu) |
| 5127 | : get_vmcs12(vcpu); |
Sean Christopherson | 5addc23 | 2020-04-15 13:34:53 -0700 | [diff] [blame] | 5128 | unsigned long exit_qualification = vmx_get_exit_qual(vcpu); |
Jim Mattson | c90f4d0 | 2019-12-06 15:46:37 -0800 | [diff] [blame] | 5129 | u32 instr_info = vmcs_read32(VMX_INSTRUCTION_INFO); |
| 5130 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 5131 | struct x86_exception e; |
| 5132 | unsigned long field; |
Sean Christopherson | 1c6f0b4 | 2019-05-07 08:36:25 -0700 | [diff] [blame] | 5133 | short offset; |
Jim Mattson | c90f4d0 | 2019-12-06 15:46:37 -0800 | [diff] [blame] | 5134 | gva_t gva; |
Vitaly Kuznetsov | 7a35e51 | 2020-06-05 13:59:05 +0200 | [diff] [blame] | 5135 | int len, r; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5136 | |
Jim Mattson | c90f4d0 | 2019-12-06 15:46:37 -0800 | [diff] [blame] | 5137 | /* |
| 5138 | * 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] | 5139 | * mode, and eventually we need to write that into a field of several |
| 5140 | * possible lengths. The code below first zero-extends the value to 64 |
Jim Mattson | c90f4d0 | 2019-12-06 15:46:37 -0800 | [diff] [blame] | 5141 | * bit (value), and then copies only the appropriate number of |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5142 | * bits into the vmcs12 field. |
| 5143 | */ |
Jim Mattson | c90f4d0 | 2019-12-06 15:46:37 -0800 | [diff] [blame] | 5144 | u64 value = 0; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5145 | |
| 5146 | if (!nested_vmx_check_permission(vcpu)) |
| 5147 | return 1; |
| 5148 | |
Jim Mattson | dd2d604 | 2019-12-06 15:46:35 -0800 | [diff] [blame] | 5149 | /* |
Yu Zhang | 64c7850 | 2021-09-30 01:51:53 +0800 | [diff] [blame] | 5150 | * 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] | 5151 | * any VMWRITE sets the ALU flags for VMfailInvalid. |
| 5152 | */ |
Yu Zhang | 64c7850 | 2021-09-30 01:51:53 +0800 | [diff] [blame] | 5153 | if (vmx->nested.current_vmptr == INVALID_GPA || |
Jim Mattson | dd2d604 | 2019-12-06 15:46:35 -0800 | [diff] [blame] | 5154 | (is_guest_mode(vcpu) && |
Yu Zhang | 64c7850 | 2021-09-30 01:51:53 +0800 | [diff] [blame] | 5155 | get_vmcs12(vcpu)->vmcs_link_pointer == INVALID_GPA)) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5156 | return nested_vmx_failInvalid(vcpu); |
| 5157 | |
Jim Mattson | c90f4d0 | 2019-12-06 15:46:37 -0800 | [diff] [blame] | 5158 | if (instr_info & BIT(10)) |
Sean Christopherson | 27b4a9c4 | 2021-04-21 19:21:28 -0700 | [diff] [blame] | 5159 | value = kvm_register_read(vcpu, (((instr_info) >> 3) & 0xf)); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5160 | else { |
Eugene Korenevsky | fdb2861 | 2019-06-06 00:19:16 +0300 | [diff] [blame] | 5161 | len = is_64_bit_mode(vcpu) ? 8 : 4; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5162 | if (get_vmx_mem_address(vcpu, exit_qualification, |
Jim Mattson | c90f4d0 | 2019-12-06 15:46:37 -0800 | [diff] [blame] | 5163 | instr_info, false, len, &gva)) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5164 | return 1; |
Vitaly Kuznetsov | 7a35e51 | 2020-06-05 13:59:05 +0200 | [diff] [blame] | 5165 | r = kvm_read_guest_virt(vcpu, gva, &value, len, &e); |
| 5166 | if (r != X86EMUL_CONTINUE) |
Babu Moger | 3f3393b | 2020-09-11 14:29:05 -0500 | [diff] [blame] | 5167 | return kvm_handle_memory_failure(vcpu, r, &e); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5168 | } |
| 5169 | |
Sean Christopherson | 27b4a9c4 | 2021-04-21 19:21:28 -0700 | [diff] [blame] | 5170 | field = kvm_register_read(vcpu, (((instr_info) >> 28) & 0xf)); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5171 | |
Jim Mattson | 693e02c | 2019-12-06 15:46:36 -0800 | [diff] [blame] | 5172 | offset = vmcs_field_to_offset(field); |
| 5173 | if (offset < 0) |
Sean Christopherson | b2656e4 | 2020-06-08 18:56:07 -0700 | [diff] [blame] | 5174 | return nested_vmx_fail(vcpu, VMXERR_UNSUPPORTED_VMCS_COMPONENT); |
Jim Mattson | 693e02c | 2019-12-06 15:46:36 -0800 | [diff] [blame] | 5175 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5176 | /* |
| 5177 | * If the vCPU supports "VMWRITE to any supported field in the |
| 5178 | * VMCS," then the "read-only" fields are actually read/write. |
| 5179 | */ |
| 5180 | if (vmcs_field_readonly(field) && |
| 5181 | !nested_cpu_has_vmwrite_any_field(vcpu)) |
Sean Christopherson | b2656e4 | 2020-06-08 18:56:07 -0700 | [diff] [blame] | 5182 | return nested_vmx_fail(vcpu, VMXERR_VMWRITE_READ_ONLY_VMCS_COMPONENT); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5183 | |
Jim Mattson | dd2d604 | 2019-12-06 15:46:35 -0800 | [diff] [blame] | 5184 | /* |
| 5185 | * Ensure vmcs12 is up-to-date before any VMWRITE that dirties |
| 5186 | * vmcs12, else we may crush a field or consume a stale value. |
| 5187 | */ |
| 5188 | if (!is_guest_mode(vcpu) && !is_shadow_field_rw(field)) |
| 5189 | copy_vmcs02_to_vmcs12_rare(vcpu, vmcs12); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5190 | |
| 5191 | /* |
Sean Christopherson | b643780 | 2019-05-07 08:36:24 -0700 | [diff] [blame] | 5192 | * Some Intel CPUs intentionally drop the reserved bits of the AR byte |
| 5193 | * fields on VMWRITE. Emulate this behavior to ensure consistent KVM |
| 5194 | * behavior regardless of the underlying hardware, e.g. if an AR_BYTE |
| 5195 | * field is intercepted for VMWRITE but not VMREAD (in L1), then VMREAD |
| 5196 | * from L1 will return a different value than VMREAD from L2 (L1 sees |
| 5197 | * 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] | 5198 | */ |
Sean Christopherson | b643780 | 2019-05-07 08:36:24 -0700 | [diff] [blame] | 5199 | if (field >= GUEST_ES_AR_BYTES && field <= GUEST_TR_AR_BYTES) |
Jim Mattson | c90f4d0 | 2019-12-06 15:46:37 -0800 | [diff] [blame] | 5200 | value &= 0x1f0ff; |
Sean Christopherson | b643780 | 2019-05-07 08:36:24 -0700 | [diff] [blame] | 5201 | |
Jim Mattson | c90f4d0 | 2019-12-06 15:46:37 -0800 | [diff] [blame] | 5202 | vmcs12_write_any(vmcs12, field, offset, value); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5203 | |
| 5204 | /* |
Sean Christopherson | e217429 | 2019-05-07 08:36:28 -0700 | [diff] [blame] | 5205 | * Do not track vmcs12 dirty-state if in guest-mode as we actually |
| 5206 | * dirty shadow vmcs12 instead of vmcs12. Fields that can be updated |
| 5207 | * by L1 without a vmexit are always updated in the vmcs02, i.e. don't |
| 5208 | * "dirty" vmcs12, all others go down the prepare_vmcs02() slow path. |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5209 | */ |
Sean Christopherson | e217429 | 2019-05-07 08:36:28 -0700 | [diff] [blame] | 5210 | if (!is_guest_mode(vcpu) && !is_shadow_field_rw(field)) { |
| 5211 | /* |
| 5212 | * L1 can read these fields without exiting, ensure the |
| 5213 | * shadow VMCS is up-to-date. |
| 5214 | */ |
| 5215 | if (enable_shadow_vmcs && is_shadow_field_ro(field)) { |
| 5216 | preempt_disable(); |
| 5217 | vmcs_load(vmx->vmcs01.shadow_vmcs); |
Sean Christopherson | fadcead | 2019-05-07 08:36:23 -0700 | [diff] [blame] | 5218 | |
Jim Mattson | c90f4d0 | 2019-12-06 15:46:37 -0800 | [diff] [blame] | 5219 | __vmcs_writel(field, value); |
Sean Christopherson | fadcead | 2019-05-07 08:36:23 -0700 | [diff] [blame] | 5220 | |
Sean Christopherson | e217429 | 2019-05-07 08:36:28 -0700 | [diff] [blame] | 5221 | vmcs_clear(vmx->vmcs01.shadow_vmcs); |
| 5222 | vmcs_load(vmx->loaded_vmcs->vmcs); |
| 5223 | preempt_enable(); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5224 | } |
Sean Christopherson | e217429 | 2019-05-07 08:36:28 -0700 | [diff] [blame] | 5225 | vmx->nested.dirty_vmcs12 = true; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5226 | } |
| 5227 | |
| 5228 | return nested_vmx_succeed(vcpu); |
| 5229 | } |
| 5230 | |
| 5231 | static void set_current_vmptr(struct vcpu_vmx *vmx, gpa_t vmptr) |
| 5232 | { |
| 5233 | vmx->nested.current_vmptr = vmptr; |
| 5234 | if (enable_shadow_vmcs) { |
Sean Christopherson | fe7f895d | 2019-05-07 12:17:57 -0700 | [diff] [blame] | 5235 | secondary_exec_controls_setbit(vmx, SECONDARY_EXEC_SHADOW_VMCS); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5236 | vmcs_write64(VMCS_LINK_POINTER, |
| 5237 | __pa(vmx->vmcs01.shadow_vmcs)); |
Sean Christopherson | 3731905ef | 2019-05-07 08:36:27 -0700 | [diff] [blame] | 5238 | vmx->nested.need_vmcs12_to_shadow_sync = true; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5239 | } |
| 5240 | vmx->nested.dirty_vmcs12 = true; |
| 5241 | } |
| 5242 | |
| 5243 | /* Emulate the VMPTRLD instruction */ |
| 5244 | static int handle_vmptrld(struct kvm_vcpu *vcpu) |
| 5245 | { |
| 5246 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 5247 | gpa_t vmptr; |
Vitaly Kuznetsov | 7a35e51 | 2020-06-05 13:59:05 +0200 | [diff] [blame] | 5248 | int r; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5249 | |
| 5250 | if (!nested_vmx_check_permission(vcpu)) |
| 5251 | return 1; |
| 5252 | |
Vitaly Kuznetsov | 7a35e51 | 2020-06-05 13:59:05 +0200 | [diff] [blame] | 5253 | if (nested_vmx_get_vmptr(vcpu, &vmptr, &r)) |
| 5254 | return r; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5255 | |
KarimAllah Ahmed | e0bf266 | 2019-01-31 21:24:43 +0100 | [diff] [blame] | 5256 | if (!page_address_valid(vcpu, vmptr)) |
Sean Christopherson | b2656e4 | 2020-06-08 18:56:07 -0700 | [diff] [blame] | 5257 | return nested_vmx_fail(vcpu, VMXERR_VMPTRLD_INVALID_ADDRESS); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5258 | |
| 5259 | if (vmptr == vmx->nested.vmxon_ptr) |
Sean Christopherson | b2656e4 | 2020-06-08 18:56:07 -0700 | [diff] [blame] | 5260 | return nested_vmx_fail(vcpu, VMXERR_VMPTRLD_VMXON_POINTER); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5261 | |
| 5262 | /* Forbid normal VMPTRLD if Enlightened version was used */ |
Vitaly Kuznetsov | 1e9dfbd | 2021-05-26 15:20:16 +0200 | [diff] [blame] | 5263 | if (evmptr_is_valid(vmx->nested.hv_evmcs_vmptr)) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5264 | return 1; |
| 5265 | |
| 5266 | if (vmx->nested.current_vmptr != vmptr) { |
KarimAllah Ahmed | b146b83 | 2019-01-31 21:24:35 +0100 | [diff] [blame] | 5267 | struct kvm_host_map map; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5268 | struct vmcs12 *new_vmcs12; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5269 | |
KarimAllah Ahmed | b146b83 | 2019-01-31 21:24:35 +0100 | [diff] [blame] | 5270 | if (kvm_vcpu_map(vcpu, gpa_to_gfn(vmptr), &map)) { |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5271 | /* |
| 5272 | * Reads from an unbacked page return all 1s, |
| 5273 | * which means that the 32 bits located at the |
| 5274 | * given physical address won't match the required |
| 5275 | * VMCS12_REVISION identifier. |
| 5276 | */ |
Sean Christopherson | b2656e4 | 2020-06-08 18:56:07 -0700 | [diff] [blame] | 5277 | return nested_vmx_fail(vcpu, |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5278 | VMXERR_VMPTRLD_INCORRECT_VMCS_REVISION_ID); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5279 | } |
KarimAllah Ahmed | b146b83 | 2019-01-31 21:24:35 +0100 | [diff] [blame] | 5280 | |
| 5281 | new_vmcs12 = map.hva; |
| 5282 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5283 | if (new_vmcs12->hdr.revision_id != VMCS12_REVISION || |
| 5284 | (new_vmcs12->hdr.shadow_vmcs && |
| 5285 | !nested_cpu_has_vmx_shadow_vmcs(vcpu))) { |
KarimAllah Ahmed | b146b83 | 2019-01-31 21:24:35 +0100 | [diff] [blame] | 5286 | kvm_vcpu_unmap(vcpu, &map, false); |
Sean Christopherson | b2656e4 | 2020-06-08 18:56:07 -0700 | [diff] [blame] | 5287 | return nested_vmx_fail(vcpu, |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5288 | VMXERR_VMPTRLD_INCORRECT_VMCS_REVISION_ID); |
| 5289 | } |
| 5290 | |
| 5291 | nested_release_vmcs12(vcpu); |
| 5292 | |
| 5293 | /* |
| 5294 | * Load VMCS12 from guest memory since it is not already |
| 5295 | * cached. |
| 5296 | */ |
| 5297 | memcpy(vmx->nested.cached_vmcs12, new_vmcs12, VMCS12_SIZE); |
KarimAllah Ahmed | b146b83 | 2019-01-31 21:24:35 +0100 | [diff] [blame] | 5298 | kvm_vcpu_unmap(vcpu, &map, false); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5299 | |
| 5300 | set_current_vmptr(vmx, vmptr); |
| 5301 | } |
| 5302 | |
| 5303 | return nested_vmx_succeed(vcpu); |
| 5304 | } |
| 5305 | |
| 5306 | /* Emulate the VMPTRST instruction */ |
| 5307 | static int handle_vmptrst(struct kvm_vcpu *vcpu) |
| 5308 | { |
Sean Christopherson | 5addc23 | 2020-04-15 13:34:53 -0700 | [diff] [blame] | 5309 | unsigned long exit_qual = vmx_get_exit_qual(vcpu); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5310 | u32 instr_info = vmcs_read32(VMX_INSTRUCTION_INFO); |
| 5311 | gpa_t current_vmptr = to_vmx(vcpu)->nested.current_vmptr; |
| 5312 | struct x86_exception e; |
| 5313 | gva_t gva; |
Vitaly Kuznetsov | 7a35e51 | 2020-06-05 13:59:05 +0200 | [diff] [blame] | 5314 | int r; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5315 | |
| 5316 | if (!nested_vmx_check_permission(vcpu)) |
| 5317 | return 1; |
| 5318 | |
Vitaly Kuznetsov | 1e9dfbd | 2021-05-26 15:20:16 +0200 | [diff] [blame] | 5319 | if (unlikely(evmptr_is_valid(to_vmx(vcpu)->nested.hv_evmcs_vmptr))) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5320 | return 1; |
| 5321 | |
Eugene Korenevsky | fdb2861 | 2019-06-06 00:19:16 +0300 | [diff] [blame] | 5322 | if (get_vmx_mem_address(vcpu, exit_qual, instr_info, |
| 5323 | true, sizeof(gpa_t), &gva)) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5324 | return 1; |
| 5325 | /* *_system ok, nested_vmx_check_permission has verified cpl=0 */ |
Vitaly Kuznetsov | 7a35e51 | 2020-06-05 13:59:05 +0200 | [diff] [blame] | 5326 | r = kvm_write_guest_virt_system(vcpu, gva, (void *)¤t_vmptr, |
| 5327 | sizeof(gpa_t), &e); |
| 5328 | if (r != X86EMUL_CONTINUE) |
Babu Moger | 3f3393b | 2020-09-11 14:29:05 -0500 | [diff] [blame] | 5329 | return kvm_handle_memory_failure(vcpu, r, &e); |
Vitaly Kuznetsov | 7a35e51 | 2020-06-05 13:59:05 +0200 | [diff] [blame] | 5330 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5331 | return nested_vmx_succeed(vcpu); |
| 5332 | } |
| 5333 | |
| 5334 | /* Emulate the INVEPT instruction */ |
| 5335 | static int handle_invept(struct kvm_vcpu *vcpu) |
| 5336 | { |
| 5337 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 5338 | u32 vmx_instruction_info, types; |
Sean Christopherson | ce8fe7b | 2020-03-20 14:28:31 -0700 | [diff] [blame] | 5339 | unsigned long type, roots_to_free; |
| 5340 | struct kvm_mmu *mmu; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5341 | gva_t gva; |
| 5342 | struct x86_exception e; |
| 5343 | struct { |
| 5344 | u64 eptp, gpa; |
| 5345 | } operand; |
Vitaly Kuznetsov | 7a35e51 | 2020-06-05 13:59:05 +0200 | [diff] [blame] | 5346 | int i, r; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5347 | |
| 5348 | if (!(vmx->nested.msrs.secondary_ctls_high & |
| 5349 | SECONDARY_EXEC_ENABLE_EPT) || |
| 5350 | !(vmx->nested.msrs.ept_caps & VMX_EPT_INVEPT_BIT)) { |
| 5351 | kvm_queue_exception(vcpu, UD_VECTOR); |
| 5352 | return 1; |
| 5353 | } |
| 5354 | |
| 5355 | if (!nested_vmx_check_permission(vcpu)) |
| 5356 | return 1; |
| 5357 | |
| 5358 | vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO); |
Sean Christopherson | 27b4a9c4 | 2021-04-21 19:21:28 -0700 | [diff] [blame] | 5359 | type = kvm_register_read(vcpu, (vmx_instruction_info >> 28) & 0xf); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5360 | |
| 5361 | types = (vmx->nested.msrs.ept_caps >> VMX_EPT_EXTENT_SHIFT) & 6; |
| 5362 | |
| 5363 | if (type >= 32 || !(types & (1 << type))) |
Sean Christopherson | b2656e4 | 2020-06-08 18:56:07 -0700 | [diff] [blame] | 5364 | return nested_vmx_fail(vcpu, VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5365 | |
| 5366 | /* According to the Intel VMX instruction reference, the memory |
| 5367 | * operand is read even if it isn't needed (e.g., for type==global) |
| 5368 | */ |
Sean Christopherson | 5addc23 | 2020-04-15 13:34:53 -0700 | [diff] [blame] | 5369 | if (get_vmx_mem_address(vcpu, vmx_get_exit_qual(vcpu), |
Eugene Korenevsky | fdb2861 | 2019-06-06 00:19:16 +0300 | [diff] [blame] | 5370 | vmx_instruction_info, false, sizeof(operand), &gva)) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5371 | return 1; |
Vitaly Kuznetsov | 7a35e51 | 2020-06-05 13:59:05 +0200 | [diff] [blame] | 5372 | r = kvm_read_guest_virt(vcpu, gva, &operand, sizeof(operand), &e); |
| 5373 | if (r != X86EMUL_CONTINUE) |
Babu Moger | 3f3393b | 2020-09-11 14:29:05 -0500 | [diff] [blame] | 5374 | return kvm_handle_memory_failure(vcpu, r, &e); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5375 | |
Sean Christopherson | ce8fe7b | 2020-03-20 14:28:31 -0700 | [diff] [blame] | 5376 | /* |
| 5377 | * Nested EPT roots are always held through guest_mmu, |
| 5378 | * not root_mmu. |
| 5379 | */ |
| 5380 | mmu = &vcpu->arch.guest_mmu; |
| 5381 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5382 | switch (type) { |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5383 | case VMX_EPT_EXTENT_CONTEXT: |
Sean Christopherson | eed0030 | 2020-03-20 14:27:58 -0700 | [diff] [blame] | 5384 | if (!nested_vmx_check_eptp(vcpu, operand.eptp)) |
Sean Christopherson | b2656e4 | 2020-06-08 18:56:07 -0700 | [diff] [blame] | 5385 | return nested_vmx_fail(vcpu, |
Sean Christopherson | eed0030 | 2020-03-20 14:27:58 -0700 | [diff] [blame] | 5386 | VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID); |
Sean Christopherson | f8aa7e3 | 2020-03-20 14:27:59 -0700 | [diff] [blame] | 5387 | |
Sean Christopherson | ce8fe7b | 2020-03-20 14:28:31 -0700 | [diff] [blame] | 5388 | roots_to_free = 0; |
Sean Christopherson | be01e8e | 2020-03-20 14:28:32 -0700 | [diff] [blame] | 5389 | if (nested_ept_root_matches(mmu->root_hpa, mmu->root_pgd, |
Sean Christopherson | ce8fe7b | 2020-03-20 14:28:31 -0700 | [diff] [blame] | 5390 | operand.eptp)) |
| 5391 | roots_to_free |= KVM_MMU_ROOT_CURRENT; |
| 5392 | |
| 5393 | for (i = 0; i < KVM_MMU_NUM_PREV_ROOTS; i++) { |
| 5394 | if (nested_ept_root_matches(mmu->prev_roots[i].hpa, |
Sean Christopherson | be01e8e | 2020-03-20 14:28:32 -0700 | [diff] [blame] | 5395 | mmu->prev_roots[i].pgd, |
Sean Christopherson | ce8fe7b | 2020-03-20 14:28:31 -0700 | [diff] [blame] | 5396 | operand.eptp)) |
| 5397 | roots_to_free |= KVM_MMU_ROOT_PREVIOUS(i); |
| 5398 | } |
| 5399 | break; |
Sean Christopherson | eed0030 | 2020-03-20 14:27:58 -0700 | [diff] [blame] | 5400 | case VMX_EPT_EXTENT_GLOBAL: |
Sean Christopherson | ce8fe7b | 2020-03-20 14:28:31 -0700 | [diff] [blame] | 5401 | roots_to_free = KVM_MMU_ROOTS_ALL; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5402 | break; |
| 5403 | default: |
Sean Christopherson | f9336e3 | 2020-05-04 08:35:06 -0700 | [diff] [blame] | 5404 | BUG(); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5405 | break; |
| 5406 | } |
| 5407 | |
Sean Christopherson | ce8fe7b | 2020-03-20 14:28:31 -0700 | [diff] [blame] | 5408 | if (roots_to_free) |
| 5409 | kvm_mmu_free_roots(vcpu, mmu, roots_to_free); |
| 5410 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5411 | return nested_vmx_succeed(vcpu); |
| 5412 | } |
| 5413 | |
| 5414 | static int handle_invvpid(struct kvm_vcpu *vcpu) |
| 5415 | { |
| 5416 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 5417 | u32 vmx_instruction_info; |
| 5418 | unsigned long type, types; |
| 5419 | gva_t gva; |
| 5420 | struct x86_exception e; |
| 5421 | struct { |
| 5422 | u64 vpid; |
| 5423 | u64 gla; |
| 5424 | } operand; |
| 5425 | u16 vpid02; |
Vitaly Kuznetsov | 7a35e51 | 2020-06-05 13:59:05 +0200 | [diff] [blame] | 5426 | int r; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5427 | |
| 5428 | if (!(vmx->nested.msrs.secondary_ctls_high & |
| 5429 | SECONDARY_EXEC_ENABLE_VPID) || |
| 5430 | !(vmx->nested.msrs.vpid_caps & VMX_VPID_INVVPID_BIT)) { |
| 5431 | kvm_queue_exception(vcpu, UD_VECTOR); |
| 5432 | return 1; |
| 5433 | } |
| 5434 | |
| 5435 | if (!nested_vmx_check_permission(vcpu)) |
| 5436 | return 1; |
| 5437 | |
| 5438 | vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO); |
Sean Christopherson | 27b4a9c4 | 2021-04-21 19:21:28 -0700 | [diff] [blame] | 5439 | type = kvm_register_read(vcpu, (vmx_instruction_info >> 28) & 0xf); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5440 | |
| 5441 | types = (vmx->nested.msrs.vpid_caps & |
| 5442 | VMX_VPID_EXTENT_SUPPORTED_MASK) >> 8; |
| 5443 | |
| 5444 | if (type >= 32 || !(types & (1 << type))) |
Sean Christopherson | b2656e4 | 2020-06-08 18:56:07 -0700 | [diff] [blame] | 5445 | return nested_vmx_fail(vcpu, |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5446 | VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID); |
| 5447 | |
| 5448 | /* according to the intel vmx instruction reference, the memory |
| 5449 | * operand is read even if it isn't needed (e.g., for type==global) |
| 5450 | */ |
Sean Christopherson | 5addc23 | 2020-04-15 13:34:53 -0700 | [diff] [blame] | 5451 | if (get_vmx_mem_address(vcpu, vmx_get_exit_qual(vcpu), |
Eugene Korenevsky | fdb2861 | 2019-06-06 00:19:16 +0300 | [diff] [blame] | 5452 | vmx_instruction_info, false, sizeof(operand), &gva)) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5453 | return 1; |
Vitaly Kuznetsov | 7a35e51 | 2020-06-05 13:59:05 +0200 | [diff] [blame] | 5454 | r = kvm_read_guest_virt(vcpu, gva, &operand, sizeof(operand), &e); |
| 5455 | if (r != X86EMUL_CONTINUE) |
Babu Moger | 3f3393b | 2020-09-11 14:29:05 -0500 | [diff] [blame] | 5456 | return kvm_handle_memory_failure(vcpu, r, &e); |
Vitaly Kuznetsov | 7a35e51 | 2020-06-05 13:59:05 +0200 | [diff] [blame] | 5457 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5458 | if (operand.vpid >> 16) |
Sean Christopherson | b2656e4 | 2020-06-08 18:56:07 -0700 | [diff] [blame] | 5459 | return nested_vmx_fail(vcpu, |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5460 | VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID); |
| 5461 | |
| 5462 | vpid02 = nested_get_vpid02(vcpu); |
| 5463 | switch (type) { |
| 5464 | case VMX_VPID_EXTENT_INDIVIDUAL_ADDR: |
| 5465 | if (!operand.vpid || |
| 5466 | is_noncanonical_address(operand.gla, vcpu)) |
Sean Christopherson | b2656e4 | 2020-06-08 18:56:07 -0700 | [diff] [blame] | 5467 | return nested_vmx_fail(vcpu, |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5468 | VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID); |
Sean Christopherson | bc41d0c | 2020-03-20 14:28:09 -0700 | [diff] [blame] | 5469 | vpid_sync_vcpu_addr(vpid02, operand.gla); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5470 | break; |
| 5471 | case VMX_VPID_EXTENT_SINGLE_CONTEXT: |
| 5472 | case VMX_VPID_EXTENT_SINGLE_NON_GLOBAL: |
| 5473 | if (!operand.vpid) |
Sean Christopherson | b2656e4 | 2020-06-08 18:56:07 -0700 | [diff] [blame] | 5474 | return nested_vmx_fail(vcpu, |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5475 | VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID); |
Sean Christopherson | 446ace4 | 2020-03-20 14:28:05 -0700 | [diff] [blame] | 5476 | vpid_sync_context(vpid02); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5477 | break; |
| 5478 | case VMX_VPID_EXTENT_ALL_CONTEXT: |
Sean Christopherson | 446ace4 | 2020-03-20 14:28:05 -0700 | [diff] [blame] | 5479 | vpid_sync_context(vpid02); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5480 | break; |
| 5481 | default: |
| 5482 | WARN_ON_ONCE(1); |
| 5483 | return kvm_skip_emulated_instruction(vcpu); |
| 5484 | } |
| 5485 | |
Junaid Shahid | d6e3f83 | 2020-03-20 14:28:00 -0700 | [diff] [blame] | 5486 | /* |
| 5487 | * Sync the shadow page tables if EPT is disabled, L1 is invalidating |
Sean Christopherson | 25b62c6 | 2021-06-09 16:42:29 -0700 | [diff] [blame] | 5488 | * linear mappings for L2 (tagged with L2's VPID). Free all guest |
| 5489 | * roots as VPIDs are not tracked in the MMU role. |
Junaid Shahid | d6e3f83 | 2020-03-20 14:28:00 -0700 | [diff] [blame] | 5490 | * |
| 5491 | * Note, this operates on root_mmu, not guest_mmu, as L1 and L2 share |
| 5492 | * an MMU when EPT is disabled. |
| 5493 | * |
| 5494 | * TODO: sync only the affected SPTEs for INVDIVIDUAL_ADDR. |
| 5495 | */ |
| 5496 | if (!enable_ept) |
Sean Christopherson | 25b62c6 | 2021-06-09 16:42:29 -0700 | [diff] [blame] | 5497 | kvm_mmu_free_guest_mode_roots(vcpu, &vcpu->arch.root_mmu); |
Junaid Shahid | d6e3f83 | 2020-03-20 14:28:00 -0700 | [diff] [blame] | 5498 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5499 | return nested_vmx_succeed(vcpu); |
| 5500 | } |
| 5501 | |
| 5502 | static int nested_vmx_eptp_switching(struct kvm_vcpu *vcpu, |
| 5503 | struct vmcs12 *vmcs12) |
| 5504 | { |
Sean Christopherson | 2b3eaf8 | 2019-04-30 10:36:19 -0700 | [diff] [blame] | 5505 | u32 index = kvm_rcx_read(vcpu); |
Sean Christopherson | ac6389a | 2020-03-02 18:02:38 -0800 | [diff] [blame] | 5506 | u64 new_eptp; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5507 | |
Sean Christopherson | c5ffd40 | 2021-06-09 16:42:35 -0700 | [diff] [blame] | 5508 | if (WARN_ON_ONCE(!nested_cpu_has_ept(vmcs12))) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5509 | return 1; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5510 | if (index >= VMFUNC_EPTP_ENTRIES) |
| 5511 | return 1; |
| 5512 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5513 | 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] | 5514 | &new_eptp, index * 8, 8)) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5515 | return 1; |
| 5516 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5517 | /* |
| 5518 | * If the (L2) guest does a vmfunc to the currently |
| 5519 | * active ept pointer, we don't have to do anything else |
| 5520 | */ |
Sean Christopherson | ac6389a | 2020-03-02 18:02:38 -0800 | [diff] [blame] | 5521 | if (vmcs12->ept_pointer != new_eptp) { |
| 5522 | if (!nested_vmx_check_eptp(vcpu, new_eptp)) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5523 | return 1; |
| 5524 | |
Sean Christopherson | ac6389a | 2020-03-02 18:02:38 -0800 | [diff] [blame] | 5525 | vmcs12->ept_pointer = new_eptp; |
Sean Christopherson | 39353ab | 2021-06-09 16:42:31 -0700 | [diff] [blame] | 5526 | nested_ept_new_eptp(vcpu); |
Sean Christopherson | c805f5d | 2021-03-04 17:10:57 -0800 | [diff] [blame] | 5527 | |
Sean Christopherson | 39353ab | 2021-06-09 16:42:31 -0700 | [diff] [blame] | 5528 | if (!nested_cpu_has_vpid(vmcs12)) |
| 5529 | kvm_make_request(KVM_REQ_TLB_FLUSH_GUEST, vcpu); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5530 | } |
| 5531 | |
| 5532 | return 0; |
| 5533 | } |
| 5534 | |
| 5535 | static int handle_vmfunc(struct kvm_vcpu *vcpu) |
| 5536 | { |
| 5537 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 5538 | struct vmcs12 *vmcs12; |
Sean Christopherson | 2b3eaf8 | 2019-04-30 10:36:19 -0700 | [diff] [blame] | 5539 | u32 function = kvm_rax_read(vcpu); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5540 | |
| 5541 | /* |
| 5542 | * VMFUNC is only supported for nested guests, but we always enable the |
| 5543 | * secondary control for simplicity; for non-nested mode, fake that we |
| 5544 | * didn't by injecting #UD. |
| 5545 | */ |
| 5546 | if (!is_guest_mode(vcpu)) { |
| 5547 | kvm_queue_exception(vcpu, UD_VECTOR); |
| 5548 | return 1; |
| 5549 | } |
| 5550 | |
| 5551 | vmcs12 = get_vmcs12(vcpu); |
Sean Christopherson | 546e839 | 2021-06-09 16:42:34 -0700 | [diff] [blame] | 5552 | |
| 5553 | /* |
| 5554 | * #UD on out-of-bounds function has priority over VM-Exit, and VMFUNC |
| 5555 | * is enabled in vmcs02 if and only if it's enabled in vmcs12. |
| 5556 | */ |
| 5557 | if (WARN_ON_ONCE((function > 63) || !nested_cpu_has_vmfunc(vmcs12))) { |
| 5558 | kvm_queue_exception(vcpu, UD_VECTOR); |
| 5559 | return 1; |
| 5560 | } |
| 5561 | |
Sean Christopherson | 0e75225 | 2021-06-09 16:42:22 -0700 | [diff] [blame] | 5562 | if (!(vmcs12->vm_function_control & BIT_ULL(function))) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5563 | goto fail; |
| 5564 | |
| 5565 | switch (function) { |
| 5566 | case 0: |
| 5567 | if (nested_vmx_eptp_switching(vcpu, vmcs12)) |
| 5568 | goto fail; |
| 5569 | break; |
| 5570 | default: |
| 5571 | goto fail; |
| 5572 | } |
| 5573 | return kvm_skip_emulated_instruction(vcpu); |
| 5574 | |
| 5575 | fail: |
Sean Christopherson | 8e53324 | 2020-11-06 17:03:12 +0800 | [diff] [blame] | 5576 | /* |
| 5577 | * This is effectively a reflected VM-Exit, as opposed to a synthesized |
| 5578 | * nested VM-Exit. Pass the original exit reason, i.e. don't hardcode |
| 5579 | * EXIT_REASON_VMFUNC as the exit reason. |
| 5580 | */ |
| 5581 | nested_vmx_vmexit(vcpu, vmx->exit_reason.full, |
Sean Christopherson | 8791585 | 2020-04-15 13:34:54 -0700 | [diff] [blame] | 5582 | vmx_get_intr_info(vcpu), |
Sean Christopherson | 5addc23 | 2020-04-15 13:34:53 -0700 | [diff] [blame] | 5583 | vmx_get_exit_qual(vcpu)); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5584 | return 1; |
| 5585 | } |
| 5586 | |
Oliver Upton | e71237d | 2020-02-04 15:26:30 -0800 | [diff] [blame] | 5587 | /* |
| 5588 | * Return true if an IO instruction with the specified port and size should cause |
| 5589 | * a VM-exit into L1. |
| 5590 | */ |
| 5591 | bool nested_vmx_check_io_bitmaps(struct kvm_vcpu *vcpu, unsigned int port, |
| 5592 | int size) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5593 | { |
Oliver Upton | e71237d | 2020-02-04 15:26:30 -0800 | [diff] [blame] | 5594 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5595 | gpa_t bitmap, last_bitmap; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5596 | u8 b; |
| 5597 | |
Yu Zhang | 64c7850 | 2021-09-30 01:51:53 +0800 | [diff] [blame] | 5598 | last_bitmap = INVALID_GPA; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5599 | b = -1; |
| 5600 | |
| 5601 | while (size > 0) { |
| 5602 | if (port < 0x8000) |
| 5603 | bitmap = vmcs12->io_bitmap_a; |
| 5604 | else if (port < 0x10000) |
| 5605 | bitmap = vmcs12->io_bitmap_b; |
| 5606 | else |
| 5607 | return true; |
| 5608 | bitmap += (port & 0x7fff) / 8; |
| 5609 | |
| 5610 | if (last_bitmap != bitmap) |
| 5611 | if (kvm_vcpu_read_guest(vcpu, bitmap, &b, 1)) |
| 5612 | return true; |
| 5613 | if (b & (1 << (port & 7))) |
| 5614 | return true; |
| 5615 | |
| 5616 | port++; |
| 5617 | size--; |
| 5618 | last_bitmap = bitmap; |
| 5619 | } |
| 5620 | |
| 5621 | return false; |
| 5622 | } |
| 5623 | |
Oliver Upton | e71237d | 2020-02-04 15:26:30 -0800 | [diff] [blame] | 5624 | static bool nested_vmx_exit_handled_io(struct kvm_vcpu *vcpu, |
| 5625 | struct vmcs12 *vmcs12) |
| 5626 | { |
| 5627 | unsigned long exit_qualification; |
Oliver Upton | 35a5713 | 2020-02-04 15:26:31 -0800 | [diff] [blame] | 5628 | unsigned short port; |
Oliver Upton | e71237d | 2020-02-04 15:26:30 -0800 | [diff] [blame] | 5629 | int size; |
| 5630 | |
| 5631 | if (!nested_cpu_has(vmcs12, CPU_BASED_USE_IO_BITMAPS)) |
| 5632 | return nested_cpu_has(vmcs12, CPU_BASED_UNCOND_IO_EXITING); |
| 5633 | |
Sean Christopherson | 5addc23 | 2020-04-15 13:34:53 -0700 | [diff] [blame] | 5634 | exit_qualification = vmx_get_exit_qual(vcpu); |
Oliver Upton | e71237d | 2020-02-04 15:26:30 -0800 | [diff] [blame] | 5635 | |
| 5636 | port = exit_qualification >> 16; |
| 5637 | size = (exit_qualification & 7) + 1; |
| 5638 | |
| 5639 | return nested_vmx_check_io_bitmaps(vcpu, port, size); |
| 5640 | } |
| 5641 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5642 | /* |
Miaohe Lin | 463bfee | 2020-02-14 10:44:05 +0800 | [diff] [blame] | 5643 | * 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] | 5644 | * rather than handle it ourselves in L0. I.e., check whether L1 expressed |
| 5645 | * disinterest in the current event (read or write a specific MSR) by using an |
| 5646 | * MSR bitmap. This may be the case even when L0 doesn't use MSR bitmaps. |
| 5647 | */ |
| 5648 | static bool nested_vmx_exit_handled_msr(struct kvm_vcpu *vcpu, |
Sean Christopherson | 8e53324 | 2020-11-06 17:03:12 +0800 | [diff] [blame] | 5649 | struct vmcs12 *vmcs12, |
| 5650 | union vmx_exit_reason exit_reason) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5651 | { |
Sean Christopherson | 2b3eaf8 | 2019-04-30 10:36:19 -0700 | [diff] [blame] | 5652 | u32 msr_index = kvm_rcx_read(vcpu); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5653 | gpa_t bitmap; |
| 5654 | |
| 5655 | if (!nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS)) |
| 5656 | return true; |
| 5657 | |
| 5658 | /* |
| 5659 | * The MSR_BITMAP page is divided into four 1024-byte bitmaps, |
| 5660 | * for the four combinations of read/write and low/high MSR numbers. |
| 5661 | * First we need to figure out which of the four to use: |
| 5662 | */ |
| 5663 | bitmap = vmcs12->msr_bitmap; |
Sean Christopherson | 8e53324 | 2020-11-06 17:03:12 +0800 | [diff] [blame] | 5664 | if (exit_reason.basic == EXIT_REASON_MSR_WRITE) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5665 | bitmap += 2048; |
| 5666 | if (msr_index >= 0xc0000000) { |
| 5667 | msr_index -= 0xc0000000; |
| 5668 | bitmap += 1024; |
| 5669 | } |
| 5670 | |
| 5671 | /* Then read the msr_index'th bit from this bitmap: */ |
| 5672 | if (msr_index < 1024*8) { |
| 5673 | unsigned char b; |
| 5674 | if (kvm_vcpu_read_guest(vcpu, bitmap + msr_index/8, &b, 1)) |
| 5675 | return true; |
| 5676 | return 1 & (b >> (msr_index & 7)); |
| 5677 | } else |
| 5678 | return true; /* let L1 handle the wrong parameter */ |
| 5679 | } |
| 5680 | |
| 5681 | /* |
| 5682 | * Return 1 if we should exit from L2 to L1 to handle a CR access exit, |
| 5683 | * rather than handle it ourselves in L0. I.e., check if L1 wanted to |
| 5684 | * intercept (via guest_host_mask etc.) the current event. |
| 5685 | */ |
| 5686 | static bool nested_vmx_exit_handled_cr(struct kvm_vcpu *vcpu, |
| 5687 | struct vmcs12 *vmcs12) |
| 5688 | { |
Sean Christopherson | 5addc23 | 2020-04-15 13:34:53 -0700 | [diff] [blame] | 5689 | unsigned long exit_qualification = vmx_get_exit_qual(vcpu); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5690 | int cr = exit_qualification & 15; |
| 5691 | int reg; |
| 5692 | unsigned long val; |
| 5693 | |
| 5694 | switch ((exit_qualification >> 4) & 3) { |
| 5695 | case 0: /* mov to cr */ |
| 5696 | reg = (exit_qualification >> 8) & 15; |
Sean Christopherson | 27b4a9c4 | 2021-04-21 19:21:28 -0700 | [diff] [blame] | 5697 | val = kvm_register_read(vcpu, reg); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5698 | switch (cr) { |
| 5699 | case 0: |
| 5700 | if (vmcs12->cr0_guest_host_mask & |
| 5701 | (val ^ vmcs12->cr0_read_shadow)) |
| 5702 | return true; |
| 5703 | break; |
| 5704 | case 3: |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5705 | if (nested_cpu_has(vmcs12, CPU_BASED_CR3_LOAD_EXITING)) |
| 5706 | return true; |
| 5707 | break; |
| 5708 | case 4: |
| 5709 | if (vmcs12->cr4_guest_host_mask & |
| 5710 | (vmcs12->cr4_read_shadow ^ val)) |
| 5711 | return true; |
| 5712 | break; |
| 5713 | case 8: |
| 5714 | if (nested_cpu_has(vmcs12, CPU_BASED_CR8_LOAD_EXITING)) |
| 5715 | return true; |
| 5716 | break; |
| 5717 | } |
| 5718 | break; |
| 5719 | case 2: /* clts */ |
| 5720 | if ((vmcs12->cr0_guest_host_mask & X86_CR0_TS) && |
| 5721 | (vmcs12->cr0_read_shadow & X86_CR0_TS)) |
| 5722 | return true; |
| 5723 | break; |
| 5724 | case 1: /* mov from cr */ |
| 5725 | switch (cr) { |
| 5726 | case 3: |
| 5727 | if (vmcs12->cpu_based_vm_exec_control & |
| 5728 | CPU_BASED_CR3_STORE_EXITING) |
| 5729 | return true; |
| 5730 | break; |
| 5731 | case 8: |
| 5732 | if (vmcs12->cpu_based_vm_exec_control & |
| 5733 | CPU_BASED_CR8_STORE_EXITING) |
| 5734 | return true; |
| 5735 | break; |
| 5736 | } |
| 5737 | break; |
| 5738 | case 3: /* lmsw */ |
| 5739 | /* |
| 5740 | * lmsw can change bits 1..3 of cr0, and only set bit 0 of |
| 5741 | * cr0. Other attempted changes are ignored, with no exit. |
| 5742 | */ |
| 5743 | val = (exit_qualification >> LMSW_SOURCE_DATA_SHIFT) & 0x0f; |
| 5744 | if (vmcs12->cr0_guest_host_mask & 0xe & |
| 5745 | (val ^ vmcs12->cr0_read_shadow)) |
| 5746 | return true; |
| 5747 | if ((vmcs12->cr0_guest_host_mask & 0x1) && |
| 5748 | !(vmcs12->cr0_read_shadow & 0x1) && |
| 5749 | (val & 0x1)) |
| 5750 | return true; |
| 5751 | break; |
| 5752 | } |
| 5753 | return false; |
| 5754 | } |
| 5755 | |
Sean Christopherson | 72add91 | 2021-04-12 16:21:42 +1200 | [diff] [blame] | 5756 | static bool nested_vmx_exit_handled_encls(struct kvm_vcpu *vcpu, |
| 5757 | struct vmcs12 *vmcs12) |
| 5758 | { |
| 5759 | u32 encls_leaf; |
| 5760 | |
| 5761 | if (!guest_cpuid_has(vcpu, X86_FEATURE_SGX) || |
| 5762 | !nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENCLS_EXITING)) |
| 5763 | return false; |
| 5764 | |
| 5765 | encls_leaf = kvm_rax_read(vcpu); |
| 5766 | if (encls_leaf > 62) |
| 5767 | encls_leaf = 63; |
| 5768 | return vmcs12->encls_exiting_bitmap & BIT_ULL(encls_leaf); |
| 5769 | } |
| 5770 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5771 | static bool nested_vmx_exit_handled_vmcs_access(struct kvm_vcpu *vcpu, |
| 5772 | struct vmcs12 *vmcs12, gpa_t bitmap) |
| 5773 | { |
| 5774 | u32 vmx_instruction_info; |
| 5775 | unsigned long field; |
| 5776 | u8 b; |
| 5777 | |
| 5778 | if (!nested_cpu_has_shadow_vmcs(vmcs12)) |
| 5779 | return true; |
| 5780 | |
| 5781 | /* Decode instruction info and find the field to access */ |
| 5782 | vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO); |
| 5783 | field = kvm_register_read(vcpu, (((vmx_instruction_info) >> 28) & 0xf)); |
| 5784 | |
| 5785 | /* Out-of-range fields always cause a VM exit from L2 to L1 */ |
| 5786 | if (field >> 15) |
| 5787 | return true; |
| 5788 | |
| 5789 | if (kvm_vcpu_read_guest(vcpu, bitmap + field/8, &b, 1)) |
| 5790 | return true; |
| 5791 | |
| 5792 | return 1 & (b >> (field & 7)); |
| 5793 | } |
| 5794 | |
Oliver Upton | b045ae9 | 2020-04-14 22:47:45 +0000 | [diff] [blame] | 5795 | static bool nested_vmx_exit_handled_mtf(struct vmcs12 *vmcs12) |
| 5796 | { |
| 5797 | u32 entry_intr_info = vmcs12->vm_entry_intr_info_field; |
| 5798 | |
| 5799 | if (nested_cpu_has_mtf(vmcs12)) |
| 5800 | return true; |
| 5801 | |
| 5802 | /* |
| 5803 | * An MTF VM-exit may be injected into the guest by setting the |
| 5804 | * interruption-type to 7 (other event) and the vector field to 0. Such |
| 5805 | * is the case regardless of the 'monitor trap flag' VM-execution |
| 5806 | * control. |
| 5807 | */ |
| 5808 | return entry_intr_info == (INTR_INFO_VALID_MASK |
| 5809 | | INTR_TYPE_OTHER_EVENT); |
| 5810 | } |
| 5811 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5812 | /* |
Sean Christopherson | 2c1f332 | 2020-04-15 10:55:14 -0700 | [diff] [blame] | 5813 | * Return true if L0 wants to handle an exit from L2 regardless of whether or not |
| 5814 | * 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] | 5815 | */ |
Sean Christopherson | 8e53324 | 2020-11-06 17:03:12 +0800 | [diff] [blame] | 5816 | static bool nested_vmx_l0_wants_exit(struct kvm_vcpu *vcpu, |
| 5817 | union vmx_exit_reason exit_reason) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5818 | { |
Sean Christopherson | 2c1f332 | 2020-04-15 10:55:14 -0700 | [diff] [blame] | 5819 | u32 intr_info; |
| 5820 | |
Sean Christopherson | 8e53324 | 2020-11-06 17:03:12 +0800 | [diff] [blame] | 5821 | switch ((u16)exit_reason.basic) { |
Sean Christopherson | 2c1f332 | 2020-04-15 10:55:14 -0700 | [diff] [blame] | 5822 | case EXIT_REASON_EXCEPTION_NMI: |
Sean Christopherson | 8791585 | 2020-04-15 13:34:54 -0700 | [diff] [blame] | 5823 | intr_info = vmx_get_intr_info(vcpu); |
Sean Christopherson | 2c1f332 | 2020-04-15 10:55:14 -0700 | [diff] [blame] | 5824 | if (is_nmi(intr_info)) |
| 5825 | return true; |
| 5826 | else if (is_page_fault(intr_info)) |
Sean Christopherson | 18712c1 | 2021-08-11 21:56:15 -0700 | [diff] [blame] | 5827 | return vcpu->arch.apf.host_apf_flags || |
| 5828 | vmx_need_pf_intercept(vcpu); |
Sean Christopherson | 2c1f332 | 2020-04-15 10:55:14 -0700 | [diff] [blame] | 5829 | else if (is_debug(intr_info) && |
| 5830 | vcpu->guest_debug & |
| 5831 | (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP)) |
| 5832 | return true; |
| 5833 | else if (is_breakpoint(intr_info) && |
| 5834 | vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP) |
| 5835 | return true; |
Sean Christopherson | b33bb78 | 2021-06-22 10:22:44 -0700 | [diff] [blame] | 5836 | else if (is_alignment_check(intr_info) && |
| 5837 | !vmx_guest_inject_ac(vcpu)) |
| 5838 | return true; |
Sean Christopherson | 2c1f332 | 2020-04-15 10:55:14 -0700 | [diff] [blame] | 5839 | return false; |
| 5840 | case EXIT_REASON_EXTERNAL_INTERRUPT: |
| 5841 | return true; |
| 5842 | case EXIT_REASON_MCE_DURING_VMENTRY: |
| 5843 | return true; |
| 5844 | case EXIT_REASON_EPT_VIOLATION: |
| 5845 | /* |
| 5846 | * L0 always deals with the EPT violation. If nested EPT is |
| 5847 | * used, and the nested mmu code discovers that the address is |
| 5848 | * missing in the guest EPT table (EPT12), the EPT violation |
| 5849 | * will be injected with nested_ept_inject_page_fault() |
| 5850 | */ |
| 5851 | return true; |
| 5852 | case EXIT_REASON_EPT_MISCONFIG: |
| 5853 | /* |
| 5854 | * L2 never uses directly L1's EPT, but rather L0's own EPT |
| 5855 | * table (shadow on EPT) or a merged EPT table that L0 built |
| 5856 | * (EPT on EPT). So any problems with the structure of the |
| 5857 | * table is L0's fault. |
| 5858 | */ |
| 5859 | return true; |
| 5860 | case EXIT_REASON_PREEMPTION_TIMER: |
| 5861 | return true; |
| 5862 | case EXIT_REASON_PML_FULL: |
Sean Christopherson | c3bb9a2 | 2021-02-12 16:50:07 -0800 | [diff] [blame] | 5863 | /* |
| 5864 | * PML is emulated for an L1 VMM and should never be enabled in |
| 5865 | * vmcs02, always "handle" PML_FULL by exiting to userspace. |
| 5866 | */ |
Sean Christopherson | 2c1f332 | 2020-04-15 10:55:14 -0700 | [diff] [blame] | 5867 | return true; |
| 5868 | case EXIT_REASON_VMFUNC: |
| 5869 | /* VM functions are emulated through L2->L0 vmexits. */ |
| 5870 | return true; |
Chenyi Qiang | 24a996a | 2021-09-14 17:50:41 +0800 | [diff] [blame] | 5871 | case EXIT_REASON_BUS_LOCK: |
| 5872 | /* |
| 5873 | * At present, bus lock VM exit is never exposed to L1. |
| 5874 | * Handle L2's bus locks in L0 directly. |
| 5875 | */ |
| 5876 | return true; |
Sean Christopherson | 2c1f332 | 2020-04-15 10:55:14 -0700 | [diff] [blame] | 5877 | default: |
| 5878 | break; |
| 5879 | } |
| 5880 | return false; |
| 5881 | } |
| 5882 | |
| 5883 | /* |
| 5884 | * Return 1 if L1 wants to intercept an exit from L2. Only call this when in |
| 5885 | * is_guest_mode (L2). |
| 5886 | */ |
Sean Christopherson | 8e53324 | 2020-11-06 17:03:12 +0800 | [diff] [blame] | 5887 | static bool nested_vmx_l1_wants_exit(struct kvm_vcpu *vcpu, |
| 5888 | union vmx_exit_reason exit_reason) |
Sean Christopherson | 2c1f332 | 2020-04-15 10:55:14 -0700 | [diff] [blame] | 5889 | { |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5890 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
Sean Christopherson | 9bd4af2 | 2020-04-21 00:53:27 -0700 | [diff] [blame] | 5891 | u32 intr_info; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5892 | |
Sean Christopherson | 8e53324 | 2020-11-06 17:03:12 +0800 | [diff] [blame] | 5893 | switch ((u16)exit_reason.basic) { |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5894 | case EXIT_REASON_EXCEPTION_NMI: |
Sean Christopherson | 8791585 | 2020-04-15 13:34:54 -0700 | [diff] [blame] | 5895 | intr_info = vmx_get_intr_info(vcpu); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5896 | if (is_nmi(intr_info)) |
Sean Christopherson | 2c1f332 | 2020-04-15 10:55:14 -0700 | [diff] [blame] | 5897 | return true; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5898 | else if (is_page_fault(intr_info)) |
Sean Christopherson | 2c1f332 | 2020-04-15 10:55:14 -0700 | [diff] [blame] | 5899 | return true; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5900 | return vmcs12->exception_bitmap & |
| 5901 | (1u << (intr_info & INTR_INFO_VECTOR_MASK)); |
| 5902 | case EXIT_REASON_EXTERNAL_INTERRUPT: |
Sean Christopherson | 2c1f332 | 2020-04-15 10:55:14 -0700 | [diff] [blame] | 5903 | return nested_exit_on_intr(vcpu); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5904 | case EXIT_REASON_TRIPLE_FAULT: |
| 5905 | return true; |
Xiaoyao Li | 9dadc2f | 2019-12-06 16:45:24 +0800 | [diff] [blame] | 5906 | case EXIT_REASON_INTERRUPT_WINDOW: |
| 5907 | return nested_cpu_has(vmcs12, CPU_BASED_INTR_WINDOW_EXITING); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5908 | case EXIT_REASON_NMI_WINDOW: |
Xiaoyao Li | 4e2a0bc | 2019-12-06 16:45:25 +0800 | [diff] [blame] | 5909 | return nested_cpu_has(vmcs12, CPU_BASED_NMI_WINDOW_EXITING); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5910 | case EXIT_REASON_TASK_SWITCH: |
| 5911 | return true; |
| 5912 | case EXIT_REASON_CPUID: |
| 5913 | return true; |
| 5914 | case EXIT_REASON_HLT: |
| 5915 | return nested_cpu_has(vmcs12, CPU_BASED_HLT_EXITING); |
| 5916 | case EXIT_REASON_INVD: |
| 5917 | return true; |
| 5918 | case EXIT_REASON_INVLPG: |
| 5919 | return nested_cpu_has(vmcs12, CPU_BASED_INVLPG_EXITING); |
| 5920 | case EXIT_REASON_RDPMC: |
| 5921 | return nested_cpu_has(vmcs12, CPU_BASED_RDPMC_EXITING); |
| 5922 | case EXIT_REASON_RDRAND: |
| 5923 | return nested_cpu_has2(vmcs12, SECONDARY_EXEC_RDRAND_EXITING); |
| 5924 | case EXIT_REASON_RDSEED: |
| 5925 | return nested_cpu_has2(vmcs12, SECONDARY_EXEC_RDSEED_EXITING); |
| 5926 | case EXIT_REASON_RDTSC: case EXIT_REASON_RDTSCP: |
| 5927 | return nested_cpu_has(vmcs12, CPU_BASED_RDTSC_EXITING); |
| 5928 | case EXIT_REASON_VMREAD: |
| 5929 | return nested_vmx_exit_handled_vmcs_access(vcpu, vmcs12, |
| 5930 | vmcs12->vmread_bitmap); |
| 5931 | case EXIT_REASON_VMWRITE: |
| 5932 | return nested_vmx_exit_handled_vmcs_access(vcpu, vmcs12, |
| 5933 | vmcs12->vmwrite_bitmap); |
| 5934 | case EXIT_REASON_VMCALL: case EXIT_REASON_VMCLEAR: |
| 5935 | case EXIT_REASON_VMLAUNCH: case EXIT_REASON_VMPTRLD: |
| 5936 | case EXIT_REASON_VMPTRST: case EXIT_REASON_VMRESUME: |
| 5937 | case EXIT_REASON_VMOFF: case EXIT_REASON_VMON: |
| 5938 | case EXIT_REASON_INVEPT: case EXIT_REASON_INVVPID: |
| 5939 | /* |
| 5940 | * VMX instructions trap unconditionally. This allows L1 to |
| 5941 | * emulate them for its L2 guest, i.e., allows 3-level nesting! |
| 5942 | */ |
| 5943 | return true; |
| 5944 | case EXIT_REASON_CR_ACCESS: |
| 5945 | return nested_vmx_exit_handled_cr(vcpu, vmcs12); |
| 5946 | case EXIT_REASON_DR_ACCESS: |
| 5947 | return nested_cpu_has(vmcs12, CPU_BASED_MOV_DR_EXITING); |
| 5948 | case EXIT_REASON_IO_INSTRUCTION: |
| 5949 | return nested_vmx_exit_handled_io(vcpu, vmcs12); |
| 5950 | case EXIT_REASON_GDTR_IDTR: case EXIT_REASON_LDTR_TR: |
| 5951 | return nested_cpu_has2(vmcs12, SECONDARY_EXEC_DESC); |
| 5952 | case EXIT_REASON_MSR_READ: |
| 5953 | case EXIT_REASON_MSR_WRITE: |
| 5954 | return nested_vmx_exit_handled_msr(vcpu, vmcs12, exit_reason); |
| 5955 | case EXIT_REASON_INVALID_STATE: |
| 5956 | return true; |
| 5957 | case EXIT_REASON_MWAIT_INSTRUCTION: |
| 5958 | return nested_cpu_has(vmcs12, CPU_BASED_MWAIT_EXITING); |
| 5959 | case EXIT_REASON_MONITOR_TRAP_FLAG: |
Oliver Upton | b045ae9 | 2020-04-14 22:47:45 +0000 | [diff] [blame] | 5960 | return nested_vmx_exit_handled_mtf(vmcs12); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5961 | case EXIT_REASON_MONITOR_INSTRUCTION: |
| 5962 | return nested_cpu_has(vmcs12, CPU_BASED_MONITOR_EXITING); |
| 5963 | case EXIT_REASON_PAUSE_INSTRUCTION: |
| 5964 | return nested_cpu_has(vmcs12, CPU_BASED_PAUSE_EXITING) || |
| 5965 | nested_cpu_has2(vmcs12, |
| 5966 | SECONDARY_EXEC_PAUSE_LOOP_EXITING); |
| 5967 | case EXIT_REASON_MCE_DURING_VMENTRY: |
Sean Christopherson | 2c1f332 | 2020-04-15 10:55:14 -0700 | [diff] [blame] | 5968 | return true; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5969 | case EXIT_REASON_TPR_BELOW_THRESHOLD: |
| 5970 | return nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW); |
| 5971 | case EXIT_REASON_APIC_ACCESS: |
| 5972 | case EXIT_REASON_APIC_WRITE: |
| 5973 | case EXIT_REASON_EOI_INDUCED: |
| 5974 | /* |
| 5975 | * The controls for "virtualize APIC accesses," "APIC- |
| 5976 | * register virtualization," and "virtual-interrupt |
| 5977 | * delivery" only come from vmcs12. |
| 5978 | */ |
| 5979 | return true; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5980 | case EXIT_REASON_INVPCID: |
| 5981 | return |
| 5982 | nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_INVPCID) && |
| 5983 | nested_cpu_has(vmcs12, CPU_BASED_INVLPG_EXITING); |
| 5984 | case EXIT_REASON_WBINVD: |
| 5985 | return nested_cpu_has2(vmcs12, SECONDARY_EXEC_WBINVD_EXITING); |
| 5986 | case EXIT_REASON_XSETBV: |
| 5987 | return true; |
| 5988 | case EXIT_REASON_XSAVES: case EXIT_REASON_XRSTORS: |
| 5989 | /* |
| 5990 | * This should never happen, since it is not possible to |
| 5991 | * set XSS to a non-zero value---neither in L1 nor in L2. |
| 5992 | * If if it were, XSS would have to be checked against |
| 5993 | * the XSS exit bitmap in vmcs12. |
| 5994 | */ |
| 5995 | return nested_cpu_has2(vmcs12, SECONDARY_EXEC_XSAVES); |
Tao Xu | bf653b7 | 2019-07-16 14:55:51 +0800 | [diff] [blame] | 5996 | case EXIT_REASON_UMWAIT: |
| 5997 | case EXIT_REASON_TPAUSE: |
| 5998 | return nested_cpu_has2(vmcs12, |
| 5999 | SECONDARY_EXEC_ENABLE_USR_WAIT_PAUSE); |
Sean Christopherson | 72add91 | 2021-04-12 16:21:42 +1200 | [diff] [blame] | 6000 | case EXIT_REASON_ENCLS: |
| 6001 | return nested_vmx_exit_handled_encls(vcpu, vmcs12); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6002 | default: |
| 6003 | return true; |
| 6004 | } |
| 6005 | } |
| 6006 | |
Sean Christopherson | 7b7bd87 | 2020-04-15 10:55:11 -0700 | [diff] [blame] | 6007 | /* |
| 6008 | * Conditionally reflect a VM-Exit into L1. Returns %true if the VM-Exit was |
| 6009 | * reflected into L1. |
| 6010 | */ |
Sean Christopherson | f47baae | 2020-04-15 10:55:16 -0700 | [diff] [blame] | 6011 | bool nested_vmx_reflect_vmexit(struct kvm_vcpu *vcpu) |
Sean Christopherson | 7b7bd87 | 2020-04-15 10:55:11 -0700 | [diff] [blame] | 6012 | { |
Sean Christopherson | fbdd502 | 2020-04-15 10:55:12 -0700 | [diff] [blame] | 6013 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Sean Christopherson | 8e53324 | 2020-11-06 17:03:12 +0800 | [diff] [blame] | 6014 | union vmx_exit_reason exit_reason = vmx->exit_reason; |
Sean Christopherson | 8779655 | 2020-04-22 17:11:27 -0700 | [diff] [blame] | 6015 | unsigned long exit_qual; |
| 6016 | u32 exit_intr_info; |
Sean Christopherson | fbdd502 | 2020-04-15 10:55:12 -0700 | [diff] [blame] | 6017 | |
| 6018 | WARN_ON_ONCE(vmx->nested.nested_run_pending); |
| 6019 | |
| 6020 | /* |
| 6021 | * Late nested VM-Fail shares the same flow as nested VM-Exit since KVM |
| 6022 | * has already loaded L2's state. |
| 6023 | */ |
| 6024 | if (unlikely(vmx->fail)) { |
| 6025 | trace_kvm_nested_vmenter_failed( |
| 6026 | "hardware VM-instruction error: ", |
| 6027 | vmcs_read32(VM_INSTRUCTION_ERROR)); |
| 6028 | exit_intr_info = 0; |
| 6029 | exit_qual = 0; |
| 6030 | goto reflect_vmexit; |
| 6031 | } |
Sean Christopherson | 7b7bd87 | 2020-04-15 10:55:11 -0700 | [diff] [blame] | 6032 | |
David Edmondson | 0a62a03 | 2021-09-20 11:37:35 +0100 | [diff] [blame] | 6033 | trace_kvm_nested_vmexit(vcpu, KVM_ISA_VMX); |
Sean Christopherson | 236871b | 2020-04-15 10:55:13 -0700 | [diff] [blame] | 6034 | |
Sean Christopherson | 2c1f332 | 2020-04-15 10:55:14 -0700 | [diff] [blame] | 6035 | /* If L0 (KVM) wants the exit, it trumps L1's desires. */ |
| 6036 | if (nested_vmx_l0_wants_exit(vcpu, exit_reason)) |
| 6037 | return false; |
| 6038 | |
| 6039 | /* If L1 doesn't want the exit, handle it in L0. */ |
| 6040 | if (!nested_vmx_l1_wants_exit(vcpu, exit_reason)) |
Sean Christopherson | 7b7bd87 | 2020-04-15 10:55:11 -0700 | [diff] [blame] | 6041 | return false; |
| 6042 | |
| 6043 | /* |
Sean Christopherson | 1d28306 | 2020-04-15 10:55:15 -0700 | [diff] [blame] | 6044 | * vmcs.VM_EXIT_INTR_INFO is only valid for EXCEPTION_NMI exits. For |
| 6045 | * EXTERNAL_INTERRUPT, the value for vmcs12->vm_exit_intr_info would |
| 6046 | * need to be synthesized by querying the in-kernel LAPIC, but external |
| 6047 | * interrupts are never reflected to L1 so it's a non-issue. |
Sean Christopherson | 7b7bd87 | 2020-04-15 10:55:11 -0700 | [diff] [blame] | 6048 | */ |
Sean Christopherson | 02f1965 | 2020-09-23 13:13:49 -0700 | [diff] [blame] | 6049 | exit_intr_info = vmx_get_intr_info(vcpu); |
Sean Christopherson | f315f2b | 2020-09-23 13:13:45 -0700 | [diff] [blame] | 6050 | if (is_exception_with_error_code(exit_intr_info)) { |
Sean Christopherson | 7b7bd87 | 2020-04-15 10:55:11 -0700 | [diff] [blame] | 6051 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
| 6052 | |
| 6053 | vmcs12->vm_exit_intr_error_code = |
| 6054 | vmcs_read32(VM_EXIT_INTR_ERROR_CODE); |
| 6055 | } |
Sean Christopherson | 02f1965 | 2020-09-23 13:13:49 -0700 | [diff] [blame] | 6056 | exit_qual = vmx_get_exit_qual(vcpu); |
Sean Christopherson | 7b7bd87 | 2020-04-15 10:55:11 -0700 | [diff] [blame] | 6057 | |
Sean Christopherson | fbdd502 | 2020-04-15 10:55:12 -0700 | [diff] [blame] | 6058 | reflect_vmexit: |
Sean Christopherson | 8e53324 | 2020-11-06 17:03:12 +0800 | [diff] [blame] | 6059 | nested_vmx_vmexit(vcpu, exit_reason.full, exit_intr_info, exit_qual); |
Sean Christopherson | 7b7bd87 | 2020-04-15 10:55:11 -0700 | [diff] [blame] | 6060 | return true; |
| 6061 | } |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6062 | |
| 6063 | static int vmx_get_nested_state(struct kvm_vcpu *vcpu, |
| 6064 | struct kvm_nested_state __user *user_kvm_nested_state, |
| 6065 | u32 user_data_size) |
| 6066 | { |
| 6067 | struct vcpu_vmx *vmx; |
| 6068 | struct vmcs12 *vmcs12; |
| 6069 | struct kvm_nested_state kvm_state = { |
| 6070 | .flags = 0, |
Liran Alon | 6ca00df | 2019-06-16 15:03:10 +0300 | [diff] [blame] | 6071 | .format = KVM_STATE_NESTED_FORMAT_VMX, |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6072 | .size = sizeof(kvm_state), |
Peter Shier | 850448f | 2020-05-26 14:51:06 -0700 | [diff] [blame] | 6073 | .hdr.vmx.flags = 0, |
Yu Zhang | 64c7850 | 2021-09-30 01:51:53 +0800 | [diff] [blame] | 6074 | .hdr.vmx.vmxon_pa = INVALID_GPA, |
| 6075 | .hdr.vmx.vmcs12_pa = INVALID_GPA, |
Peter Shier | 850448f | 2020-05-26 14:51:06 -0700 | [diff] [blame] | 6076 | .hdr.vmx.preemption_timer_deadline = 0, |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6077 | }; |
Liran Alon | 6ca00df | 2019-06-16 15:03:10 +0300 | [diff] [blame] | 6078 | struct kvm_vmx_nested_state_data __user *user_vmx_nested_state = |
| 6079 | &user_kvm_nested_state->data.vmx[0]; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6080 | |
| 6081 | if (!vcpu) |
Liran Alon | 6ca00df | 2019-06-16 15:03:10 +0300 | [diff] [blame] | 6082 | return kvm_state.size + sizeof(*user_vmx_nested_state); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6083 | |
| 6084 | vmx = to_vmx(vcpu); |
| 6085 | vmcs12 = get_vmcs12(vcpu); |
| 6086 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6087 | if (nested_vmx_allowed(vcpu) && |
| 6088 | (vmx->nested.vmxon || vmx->nested.smm.vmxon)) { |
Liran Alon | 6ca00df | 2019-06-16 15:03:10 +0300 | [diff] [blame] | 6089 | kvm_state.hdr.vmx.vmxon_pa = vmx->nested.vmxon_ptr; |
| 6090 | kvm_state.hdr.vmx.vmcs12_pa = vmx->nested.current_vmptr; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6091 | |
| 6092 | if (vmx_has_valid_vmcs12(vcpu)) { |
Liran Alon | 6ca00df | 2019-06-16 15:03:10 +0300 | [diff] [blame] | 6093 | kvm_state.size += sizeof(user_vmx_nested_state->vmcs12); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6094 | |
Vitaly Kuznetsov | 2784996 | 2021-05-26 15:20:20 +0200 | [diff] [blame] | 6095 | /* 'hv_evmcs_vmptr' can also be EVMPTR_MAP_PENDING here */ |
| 6096 | if (vmx->nested.hv_evmcs_vmptr != EVMPTR_INVALID) |
Liran Alon | 323d73a | 2019-06-26 16:09:27 +0300 | [diff] [blame] | 6097 | kvm_state.flags |= KVM_STATE_NESTED_EVMCS; |
| 6098 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6099 | if (is_guest_mode(vcpu) && |
| 6100 | nested_cpu_has_shadow_vmcs(vmcs12) && |
Yu Zhang | 64c7850 | 2021-09-30 01:51:53 +0800 | [diff] [blame] | 6101 | vmcs12->vmcs_link_pointer != INVALID_GPA) |
Liran Alon | 6ca00df | 2019-06-16 15:03:10 +0300 | [diff] [blame] | 6102 | kvm_state.size += sizeof(user_vmx_nested_state->shadow_vmcs12); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6103 | } |
| 6104 | |
| 6105 | if (vmx->nested.smm.vmxon) |
Liran Alon | 6ca00df | 2019-06-16 15:03:10 +0300 | [diff] [blame] | 6106 | kvm_state.hdr.vmx.smm.flags |= KVM_STATE_NESTED_SMM_VMXON; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6107 | |
| 6108 | if (vmx->nested.smm.guest_mode) |
Liran Alon | 6ca00df | 2019-06-16 15:03:10 +0300 | [diff] [blame] | 6109 | kvm_state.hdr.vmx.smm.flags |= KVM_STATE_NESTED_SMM_GUEST_MODE; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6110 | |
| 6111 | if (is_guest_mode(vcpu)) { |
| 6112 | kvm_state.flags |= KVM_STATE_NESTED_GUEST_MODE; |
| 6113 | |
| 6114 | if (vmx->nested.nested_run_pending) |
| 6115 | kvm_state.flags |= KVM_STATE_NESTED_RUN_PENDING; |
Oliver Upton | 5ef8acb | 2020-02-07 02:36:07 -0800 | [diff] [blame] | 6116 | |
| 6117 | if (vmx->nested.mtf_pending) |
| 6118 | kvm_state.flags |= KVM_STATE_NESTED_MTF_PENDING; |
Peter Shier | 850448f | 2020-05-26 14:51:06 -0700 | [diff] [blame] | 6119 | |
| 6120 | if (nested_cpu_has_preemption_timer(vmcs12) && |
| 6121 | vmx->nested.has_preemption_timer_deadline) { |
| 6122 | kvm_state.hdr.vmx.flags |= |
| 6123 | KVM_STATE_VMX_PREEMPTION_TIMER_DEADLINE; |
| 6124 | kvm_state.hdr.vmx.preemption_timer_deadline = |
| 6125 | vmx->nested.preemption_timer_deadline; |
| 6126 | } |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6127 | } |
| 6128 | } |
| 6129 | |
| 6130 | if (user_data_size < kvm_state.size) |
| 6131 | goto out; |
| 6132 | |
| 6133 | if (copy_to_user(user_kvm_nested_state, &kvm_state, sizeof(kvm_state))) |
| 6134 | return -EFAULT; |
| 6135 | |
| 6136 | if (!vmx_has_valid_vmcs12(vcpu)) |
| 6137 | goto out; |
| 6138 | |
| 6139 | /* |
| 6140 | * When running L2, the authoritative vmcs12 state is in the |
| 6141 | * vmcs02. When running L1, the authoritative vmcs12 state is |
| 6142 | * in the shadow or enlightened vmcs linked to vmcs01, unless |
Sean Christopherson | 3731905ef | 2019-05-07 08:36:27 -0700 | [diff] [blame] | 6143 | * need_vmcs12_to_shadow_sync is set, in which case, the authoritative |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6144 | * vmcs12 state is in the vmcs12 already. |
| 6145 | */ |
| 6146 | if (is_guest_mode(vcpu)) { |
Sean Christopherson | 3731905ef | 2019-05-07 08:36:27 -0700 | [diff] [blame] | 6147 | sync_vmcs02_to_vmcs12(vcpu, vmcs12); |
Sean Christopherson | 7952d76 | 2019-05-07 08:36:29 -0700 | [diff] [blame] | 6148 | sync_vmcs02_to_vmcs12_rare(vcpu, vmcs12); |
Maxim Levitsky | d51e1d3 | 2021-01-14 22:54:47 +0200 | [diff] [blame] | 6149 | } else { |
| 6150 | copy_vmcs02_to_vmcs12_rare(vcpu, get_vmcs12(vcpu)); |
| 6151 | if (!vmx->nested.need_vmcs12_to_shadow_sync) { |
Vitaly Kuznetsov | 1e9dfbd | 2021-05-26 15:20:16 +0200 | [diff] [blame] | 6152 | if (evmptr_is_valid(vmx->nested.hv_evmcs_vmptr)) |
Vitaly Kuznetsov | d6bf71a | 2021-05-26 15:20:22 +0200 | [diff] [blame] | 6153 | /* |
| 6154 | * L1 hypervisor is not obliged to keep eVMCS |
| 6155 | * clean fields data always up-to-date while |
| 6156 | * not in guest mode, 'hv_clean_fields' is only |
| 6157 | * supposed to be actual upon vmentry so we need |
| 6158 | * to ignore it here and do full copy. |
| 6159 | */ |
| 6160 | copy_enlightened_to_vmcs12(vmx, 0); |
Maxim Levitsky | d51e1d3 | 2021-01-14 22:54:47 +0200 | [diff] [blame] | 6161 | else if (enable_shadow_vmcs) |
| 6162 | copy_shadow_to_vmcs12(vmx); |
| 6163 | } |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6164 | } |
| 6165 | |
Liran Alon | 6ca00df | 2019-06-16 15:03:10 +0300 | [diff] [blame] | 6166 | BUILD_BUG_ON(sizeof(user_vmx_nested_state->vmcs12) < VMCS12_SIZE); |
| 6167 | BUILD_BUG_ON(sizeof(user_vmx_nested_state->shadow_vmcs12) < VMCS12_SIZE); |
| 6168 | |
Tom Roeder | 3a33d03 | 2019-01-24 13:48:20 -0800 | [diff] [blame] | 6169 | /* |
| 6170 | * Copy over the full allocated size of vmcs12 rather than just the size |
| 6171 | * of the struct. |
| 6172 | */ |
Liran Alon | 6ca00df | 2019-06-16 15:03:10 +0300 | [diff] [blame] | 6173 | if (copy_to_user(user_vmx_nested_state->vmcs12, vmcs12, VMCS12_SIZE)) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6174 | return -EFAULT; |
| 6175 | |
| 6176 | if (nested_cpu_has_shadow_vmcs(vmcs12) && |
Yu Zhang | 64c7850 | 2021-09-30 01:51:53 +0800 | [diff] [blame] | 6177 | vmcs12->vmcs_link_pointer != INVALID_GPA) { |
Liran Alon | 6ca00df | 2019-06-16 15:03:10 +0300 | [diff] [blame] | 6178 | if (copy_to_user(user_vmx_nested_state->shadow_vmcs12, |
Tom Roeder | 3a33d03 | 2019-01-24 13:48:20 -0800 | [diff] [blame] | 6179 | get_shadow_vmcs12(vcpu), VMCS12_SIZE)) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6180 | return -EFAULT; |
| 6181 | } |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6182 | out: |
| 6183 | return kvm_state.size; |
| 6184 | } |
| 6185 | |
| 6186 | /* |
| 6187 | * Forcibly leave nested mode in order to be able to reset the VCPU later on. |
| 6188 | */ |
| 6189 | void vmx_leave_nested(struct kvm_vcpu *vcpu) |
| 6190 | { |
| 6191 | if (is_guest_mode(vcpu)) { |
| 6192 | to_vmx(vcpu)->nested.nested_run_pending = 0; |
| 6193 | nested_vmx_vmexit(vcpu, -1, 0, 0); |
| 6194 | } |
| 6195 | free_nested(vcpu); |
| 6196 | } |
| 6197 | |
| 6198 | static int vmx_set_nested_state(struct kvm_vcpu *vcpu, |
| 6199 | struct kvm_nested_state __user *user_kvm_nested_state, |
| 6200 | struct kvm_nested_state *kvm_state) |
| 6201 | { |
| 6202 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 6203 | struct vmcs12 *vmcs12; |
Sean Christopherson | 68cda40 | 2020-05-11 15:05:29 -0700 | [diff] [blame] | 6204 | enum vm_entry_failure_code ignored; |
Liran Alon | 6ca00df | 2019-06-16 15:03:10 +0300 | [diff] [blame] | 6205 | struct kvm_vmx_nested_state_data __user *user_vmx_nested_state = |
| 6206 | &user_kvm_nested_state->data.vmx[0]; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6207 | int ret; |
| 6208 | |
Liran Alon | 6ca00df | 2019-06-16 15:03:10 +0300 | [diff] [blame] | 6209 | if (kvm_state->format != KVM_STATE_NESTED_FORMAT_VMX) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6210 | return -EINVAL; |
| 6211 | |
Yu Zhang | 64c7850 | 2021-09-30 01:51:53 +0800 | [diff] [blame] | 6212 | if (kvm_state->hdr.vmx.vmxon_pa == INVALID_GPA) { |
Liran Alon | 6ca00df | 2019-06-16 15:03:10 +0300 | [diff] [blame] | 6213 | if (kvm_state->hdr.vmx.smm.flags) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6214 | return -EINVAL; |
| 6215 | |
Yu Zhang | 64c7850 | 2021-09-30 01:51:53 +0800 | [diff] [blame] | 6216 | if (kvm_state->hdr.vmx.vmcs12_pa != INVALID_GPA) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6217 | return -EINVAL; |
| 6218 | |
Liran Alon | 323d73a | 2019-06-26 16:09:27 +0300 | [diff] [blame] | 6219 | /* |
| 6220 | * KVM_STATE_NESTED_EVMCS used to signal that KVM should |
| 6221 | * enable eVMCS capability on vCPU. However, since then |
| 6222 | * code was changed such that flag signals vmcs12 should |
| 6223 | * be copied into eVMCS in guest memory. |
| 6224 | * |
| 6225 | * To preserve backwards compatability, allow user |
| 6226 | * to set this flag even when there is no VMXON region. |
| 6227 | */ |
Paolo Bonzini | 9fd5887 | 2019-06-19 16:52:27 +0200 | [diff] [blame] | 6228 | if (kvm_state->flags & ~KVM_STATE_NESTED_EVMCS) |
| 6229 | return -EINVAL; |
| 6230 | } else { |
| 6231 | if (!nested_vmx_allowed(vcpu)) |
| 6232 | return -EINVAL; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6233 | |
Paolo Bonzini | 9fd5887 | 2019-06-19 16:52:27 +0200 | [diff] [blame] | 6234 | if (!page_address_valid(vcpu, kvm_state->hdr.vmx.vmxon_pa)) |
| 6235 | return -EINVAL; |
Liran Alon | 323d73a | 2019-06-26 16:09:27 +0300 | [diff] [blame] | 6236 | } |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6237 | |
Liran Alon | 6ca00df | 2019-06-16 15:03:10 +0300 | [diff] [blame] | 6238 | if ((kvm_state->hdr.vmx.smm.flags & KVM_STATE_NESTED_SMM_GUEST_MODE) && |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6239 | (kvm_state->flags & KVM_STATE_NESTED_GUEST_MODE)) |
| 6240 | return -EINVAL; |
| 6241 | |
Liran Alon | 6ca00df | 2019-06-16 15:03:10 +0300 | [diff] [blame] | 6242 | if (kvm_state->hdr.vmx.smm.flags & |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6243 | ~(KVM_STATE_NESTED_SMM_GUEST_MODE | KVM_STATE_NESTED_SMM_VMXON)) |
| 6244 | return -EINVAL; |
| 6245 | |
Paolo Bonzini | 5e105c8 | 2020-07-27 08:55:09 -0400 | [diff] [blame] | 6246 | if (kvm_state->hdr.vmx.flags & ~KVM_STATE_VMX_PREEMPTION_TIMER_DEADLINE) |
| 6247 | return -EINVAL; |
| 6248 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6249 | /* |
| 6250 | * SMM temporarily disables VMX, so we cannot be in guest mode, |
| 6251 | * nor can VMLAUNCH/VMRESUME be pending. Outside SMM, SMM flags |
| 6252 | * must be zero. |
| 6253 | */ |
Liran Alon | 65b712f1 | 2019-06-25 14:26:42 +0300 | [diff] [blame] | 6254 | if (is_smm(vcpu) ? |
| 6255 | (kvm_state->flags & |
| 6256 | (KVM_STATE_NESTED_GUEST_MODE | KVM_STATE_NESTED_RUN_PENDING)) |
| 6257 | : kvm_state->hdr.vmx.smm.flags) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6258 | return -EINVAL; |
| 6259 | |
Liran Alon | 6ca00df | 2019-06-16 15:03:10 +0300 | [diff] [blame] | 6260 | if ((kvm_state->hdr.vmx.smm.flags & KVM_STATE_NESTED_SMM_GUEST_MODE) && |
| 6261 | !(kvm_state->hdr.vmx.smm.flags & KVM_STATE_NESTED_SMM_VMXON)) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6262 | return -EINVAL; |
| 6263 | |
Liran Alon | 323d73a | 2019-06-26 16:09:27 +0300 | [diff] [blame] | 6264 | if ((kvm_state->flags & KVM_STATE_NESTED_EVMCS) && |
| 6265 | (!nested_vmx_allowed(vcpu) || !vmx->nested.enlightened_vmcs_enabled)) |
Paolo Bonzini | 9fd5887 | 2019-06-19 16:52:27 +0200 | [diff] [blame] | 6266 | return -EINVAL; |
| 6267 | |
Liran Alon | 323d73a | 2019-06-26 16:09:27 +0300 | [diff] [blame] | 6268 | vmx_leave_nested(vcpu); |
Paolo Bonzini | 9fd5887 | 2019-06-19 16:52:27 +0200 | [diff] [blame] | 6269 | |
Yu Zhang | 64c7850 | 2021-09-30 01:51:53 +0800 | [diff] [blame] | 6270 | if (kvm_state->hdr.vmx.vmxon_pa == INVALID_GPA) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6271 | return 0; |
| 6272 | |
Liran Alon | 6ca00df | 2019-06-16 15:03:10 +0300 | [diff] [blame] | 6273 | vmx->nested.vmxon_ptr = kvm_state->hdr.vmx.vmxon_pa; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6274 | ret = enter_vmx_operation(vcpu); |
| 6275 | if (ret) |
| 6276 | return ret; |
| 6277 | |
Paolo Bonzini | 0f02bd0 | 2020-07-27 09:00:37 -0400 | [diff] [blame] | 6278 | /* Empty 'VMXON' state is permitted if no VMCS loaded */ |
| 6279 | if (kvm_state->size < sizeof(*kvm_state) + sizeof(*vmcs12)) { |
| 6280 | /* See vmx_has_valid_vmcs12. */ |
| 6281 | if ((kvm_state->flags & KVM_STATE_NESTED_GUEST_MODE) || |
| 6282 | (kvm_state->flags & KVM_STATE_NESTED_EVMCS) || |
Yu Zhang | 64c7850 | 2021-09-30 01:51:53 +0800 | [diff] [blame] | 6283 | (kvm_state->hdr.vmx.vmcs12_pa != INVALID_GPA)) |
Paolo Bonzini | 0f02bd0 | 2020-07-27 09:00:37 -0400 | [diff] [blame] | 6284 | return -EINVAL; |
| 6285 | else |
| 6286 | return 0; |
| 6287 | } |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6288 | |
Yu Zhang | 64c7850 | 2021-09-30 01:51:53 +0800 | [diff] [blame] | 6289 | if (kvm_state->hdr.vmx.vmcs12_pa != INVALID_GPA) { |
Liran Alon | 6ca00df | 2019-06-16 15:03:10 +0300 | [diff] [blame] | 6290 | if (kvm_state->hdr.vmx.vmcs12_pa == kvm_state->hdr.vmx.vmxon_pa || |
| 6291 | !page_address_valid(vcpu, kvm_state->hdr.vmx.vmcs12_pa)) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6292 | return -EINVAL; |
| 6293 | |
Liran Alon | 6ca00df | 2019-06-16 15:03:10 +0300 | [diff] [blame] | 6294 | set_current_vmptr(vmx, kvm_state->hdr.vmx.vmcs12_pa); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6295 | } else if (kvm_state->flags & KVM_STATE_NESTED_EVMCS) { |
| 6296 | /* |
Vitaly Kuznetsov | e942dbf | 2020-03-09 16:52:12 +0100 | [diff] [blame] | 6297 | * nested_vmx_handle_enlightened_vmptrld() cannot be called |
| 6298 | * directly from here as HV_X64_MSR_VP_ASSIST_PAGE may not be |
| 6299 | * restored yet. EVMCS will be mapped from |
| 6300 | * nested_get_vmcs12_pages(). |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6301 | */ |
Vitaly Kuznetsov | 2784996 | 2021-05-26 15:20:20 +0200 | [diff] [blame] | 6302 | vmx->nested.hv_evmcs_vmptr = EVMPTR_MAP_PENDING; |
Paolo Bonzini | 729c15c | 2020-09-22 06:53:57 -0400 | [diff] [blame] | 6303 | kvm_make_request(KVM_REQ_GET_NESTED_STATE_PAGES, vcpu); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6304 | } else { |
| 6305 | return -EINVAL; |
| 6306 | } |
| 6307 | |
Liran Alon | 6ca00df | 2019-06-16 15:03:10 +0300 | [diff] [blame] | 6308 | if (kvm_state->hdr.vmx.smm.flags & KVM_STATE_NESTED_SMM_VMXON) { |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6309 | vmx->nested.smm.vmxon = true; |
| 6310 | vmx->nested.vmxon = false; |
| 6311 | |
Liran Alon | 6ca00df | 2019-06-16 15:03:10 +0300 | [diff] [blame] | 6312 | 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] | 6313 | vmx->nested.smm.guest_mode = true; |
| 6314 | } |
| 6315 | |
| 6316 | vmcs12 = get_vmcs12(vcpu); |
Liran Alon | 6ca00df | 2019-06-16 15:03:10 +0300 | [diff] [blame] | 6317 | if (copy_from_user(vmcs12, user_vmx_nested_state->vmcs12, sizeof(*vmcs12))) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6318 | return -EFAULT; |
| 6319 | |
| 6320 | if (vmcs12->hdr.revision_id != VMCS12_REVISION) |
| 6321 | return -EINVAL; |
| 6322 | |
| 6323 | if (!(kvm_state->flags & KVM_STATE_NESTED_GUEST_MODE)) |
| 6324 | return 0; |
| 6325 | |
Sean Christopherson | 21be4ca | 2019-05-08 11:04:32 -0700 | [diff] [blame] | 6326 | vmx->nested.nested_run_pending = |
| 6327 | !!(kvm_state->flags & KVM_STATE_NESTED_RUN_PENDING); |
| 6328 | |
Oliver Upton | 5ef8acb | 2020-02-07 02:36:07 -0800 | [diff] [blame] | 6329 | vmx->nested.mtf_pending = |
| 6330 | !!(kvm_state->flags & KVM_STATE_NESTED_MTF_PENDING); |
| 6331 | |
Sean Christopherson | 21be4ca | 2019-05-08 11:04:32 -0700 | [diff] [blame] | 6332 | ret = -EINVAL; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6333 | if (nested_cpu_has_shadow_vmcs(vmcs12) && |
Yu Zhang | 64c7850 | 2021-09-30 01:51:53 +0800 | [diff] [blame] | 6334 | vmcs12->vmcs_link_pointer != INVALID_GPA) { |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6335 | struct vmcs12 *shadow_vmcs12 = get_shadow_vmcs12(vcpu); |
| 6336 | |
Liran Alon | 6ca00df | 2019-06-16 15:03:10 +0300 | [diff] [blame] | 6337 | if (kvm_state->size < |
| 6338 | sizeof(*kvm_state) + |
| 6339 | sizeof(user_vmx_nested_state->vmcs12) + sizeof(*shadow_vmcs12)) |
Sean Christopherson | 21be4ca | 2019-05-08 11:04:32 -0700 | [diff] [blame] | 6340 | goto error_guest_mode; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6341 | |
| 6342 | if (copy_from_user(shadow_vmcs12, |
Liran Alon | 6ca00df | 2019-06-16 15:03:10 +0300 | [diff] [blame] | 6343 | user_vmx_nested_state->shadow_vmcs12, |
| 6344 | sizeof(*shadow_vmcs12))) { |
Sean Christopherson | 21be4ca | 2019-05-08 11:04:32 -0700 | [diff] [blame] | 6345 | ret = -EFAULT; |
| 6346 | goto error_guest_mode; |
| 6347 | } |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6348 | |
| 6349 | if (shadow_vmcs12->hdr.revision_id != VMCS12_REVISION || |
| 6350 | !shadow_vmcs12->hdr.shadow_vmcs) |
Sean Christopherson | 21be4ca | 2019-05-08 11:04:32 -0700 | [diff] [blame] | 6351 | goto error_guest_mode; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6352 | } |
| 6353 | |
Paolo Bonzini | 83d31e5 | 2020-07-09 13:12:09 -0400 | [diff] [blame] | 6354 | vmx->nested.has_preemption_timer_deadline = false; |
Peter Shier | 850448f | 2020-05-26 14:51:06 -0700 | [diff] [blame] | 6355 | if (kvm_state->hdr.vmx.flags & KVM_STATE_VMX_PREEMPTION_TIMER_DEADLINE) { |
| 6356 | vmx->nested.has_preemption_timer_deadline = true; |
| 6357 | vmx->nested.preemption_timer_deadline = |
| 6358 | kvm_state->hdr.vmx.preemption_timer_deadline; |
| 6359 | } |
| 6360 | |
Sean Christopherson | 5478ba3 | 2019-04-11 12:18:06 -0700 | [diff] [blame] | 6361 | if (nested_vmx_check_controls(vcpu, vmcs12) || |
| 6362 | nested_vmx_check_host_state(vcpu, vmcs12) || |
Sean Christopherson | 68cda40 | 2020-05-11 15:05:29 -0700 | [diff] [blame] | 6363 | nested_vmx_check_guest_state(vcpu, vmcs12, &ignored)) |
Sean Christopherson | 21be4ca | 2019-05-08 11:04:32 -0700 | [diff] [blame] | 6364 | goto error_guest_mode; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6365 | |
| 6366 | vmx->nested.dirty_vmcs12 = true; |
| 6367 | ret = nested_vmx_enter_non_root_mode(vcpu, false); |
Sean Christopherson | 21be4ca | 2019-05-08 11:04:32 -0700 | [diff] [blame] | 6368 | if (ret) |
| 6369 | goto error_guest_mode; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6370 | |
| 6371 | return 0; |
Sean Christopherson | 21be4ca | 2019-05-08 11:04:32 -0700 | [diff] [blame] | 6372 | |
| 6373 | error_guest_mode: |
| 6374 | vmx->nested.nested_run_pending = 0; |
| 6375 | return ret; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6376 | } |
| 6377 | |
Xiaoyao Li | 1b84292 | 2019-10-20 17:11:01 +0800 | [diff] [blame] | 6378 | void nested_vmx_set_vmcs_shadowing_bitmap(void) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6379 | { |
| 6380 | if (enable_shadow_vmcs) { |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6381 | vmcs_write64(VMREAD_BITMAP, __pa(vmx_vmread_bitmap)); |
Sean Christopherson | fadcead | 2019-05-07 08:36:23 -0700 | [diff] [blame] | 6382 | vmcs_write64(VMWRITE_BITMAP, __pa(vmx_vmwrite_bitmap)); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6383 | } |
| 6384 | } |
| 6385 | |
| 6386 | /* |
Sean Christopherson | ba1f824 | 2021-06-18 14:46:58 -0700 | [diff] [blame] | 6387 | * Indexing into the vmcs12 uses the VMCS encoding rotated left by 6. Undo |
| 6388 | * that madness to get the encoding for comparison. |
| 6389 | */ |
| 6390 | #define VMCS12_IDX_TO_ENC(idx) ((u16)(((u16)(idx) >> 6) | ((u16)(idx) << 10))) |
| 6391 | |
| 6392 | static u64 nested_vmx_calc_vmcs_enum_msr(void) |
| 6393 | { |
| 6394 | /* |
| 6395 | * Note these are the so called "index" of the VMCS field encoding, not |
| 6396 | * the index into vmcs12. |
| 6397 | */ |
| 6398 | unsigned int max_idx, idx; |
| 6399 | int i; |
| 6400 | |
| 6401 | /* |
| 6402 | * For better or worse, KVM allows VMREAD/VMWRITE to all fields in |
| 6403 | * vmcs12, regardless of whether or not the associated feature is |
| 6404 | * exposed to L1. Simply find the field with the highest index. |
| 6405 | */ |
| 6406 | max_idx = 0; |
| 6407 | for (i = 0; i < nr_vmcs12_fields; i++) { |
| 6408 | /* The vmcs12 table is very, very sparsely populated. */ |
| 6409 | if (!vmcs_field_to_offset_table[i]) |
| 6410 | continue; |
| 6411 | |
| 6412 | idx = vmcs_field_index(VMCS12_IDX_TO_ENC(i)); |
| 6413 | if (idx > max_idx) |
| 6414 | max_idx = idx; |
| 6415 | } |
| 6416 | |
| 6417 | return (u64)max_idx << VMCS_FIELD_INDEX_SHIFT; |
| 6418 | } |
| 6419 | |
| 6420 | /* |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6421 | * nested_vmx_setup_ctls_msrs() sets up variables containing the values to be |
| 6422 | * returned for the various VMX controls MSRs when nested VMX is enabled. |
| 6423 | * The same values should also be used to verify that vmcs12 control fields are |
| 6424 | * valid during nested entry from L1 to L2. |
| 6425 | * Each of these control msrs has a low and high 32-bit half: A low bit is on |
| 6426 | * if the corresponding bit in the (32-bit) control field *must* be on, and a |
| 6427 | * bit in the high half is on if the corresponding bit in the control field |
| 6428 | * may be on. See also vmx_control_verify(). |
| 6429 | */ |
Vitaly Kuznetsov | a444326 | 2020-02-20 18:22:04 +0100 | [diff] [blame] | 6430 | 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] | 6431 | { |
| 6432 | /* |
| 6433 | * Note that as a general rule, the high half of the MSRs (bits in |
| 6434 | * the control fields which may be 1) should be initialized by the |
| 6435 | * intersection of the underlying hardware's MSR (i.e., features which |
| 6436 | * can be supported) and the list of features we want to expose - |
| 6437 | * because they are known to be properly supported in our code. |
| 6438 | * Also, usually, the low half of the MSRs (bits which must be 1) can |
| 6439 | * be set to 0, meaning that L1 may turn off any of these bits. The |
| 6440 | * reason is that if one of these bits is necessary, it will appear |
| 6441 | * in vmcs01 and prepare_vmcs02, when it bitwise-or's the control |
| 6442 | * fields of vmcs01 and vmcs02, will turn these bits off - and |
Sean Christopherson | 2c1f332 | 2020-04-15 10:55:14 -0700 | [diff] [blame] | 6443 | * nested_vmx_l1_wants_exit() will not pass related exits to L1. |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6444 | * These rules have exceptions below. |
| 6445 | */ |
| 6446 | |
| 6447 | /* pin-based controls */ |
| 6448 | rdmsr(MSR_IA32_VMX_PINBASED_CTLS, |
| 6449 | msrs->pinbased_ctls_low, |
| 6450 | msrs->pinbased_ctls_high); |
| 6451 | msrs->pinbased_ctls_low |= |
| 6452 | PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR; |
| 6453 | msrs->pinbased_ctls_high &= |
| 6454 | PIN_BASED_EXT_INTR_MASK | |
| 6455 | PIN_BASED_NMI_EXITING | |
| 6456 | PIN_BASED_VIRTUAL_NMIS | |
Vitaly Kuznetsov | a444326 | 2020-02-20 18:22:04 +0100 | [diff] [blame] | 6457 | (enable_apicv ? PIN_BASED_POSTED_INTR : 0); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6458 | msrs->pinbased_ctls_high |= |
| 6459 | PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR | |
| 6460 | PIN_BASED_VMX_PREEMPTION_TIMER; |
| 6461 | |
| 6462 | /* exit controls */ |
| 6463 | rdmsr(MSR_IA32_VMX_EXIT_CTLS, |
| 6464 | msrs->exit_ctls_low, |
| 6465 | msrs->exit_ctls_high); |
| 6466 | msrs->exit_ctls_low = |
| 6467 | VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR; |
| 6468 | |
| 6469 | msrs->exit_ctls_high &= |
| 6470 | #ifdef CONFIG_X86_64 |
| 6471 | VM_EXIT_HOST_ADDR_SPACE_SIZE | |
| 6472 | #endif |
Chenyi Qiang | efc8313 | 2020-08-28 16:56:18 +0800 | [diff] [blame] | 6473 | VM_EXIT_LOAD_IA32_PAT | VM_EXIT_SAVE_IA32_PAT | |
| 6474 | VM_EXIT_CLEAR_BNDCFGS | VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6475 | msrs->exit_ctls_high |= |
| 6476 | VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR | |
| 6477 | VM_EXIT_LOAD_IA32_EFER | VM_EXIT_SAVE_IA32_EFER | |
| 6478 | VM_EXIT_SAVE_VMX_PREEMPTION_TIMER | VM_EXIT_ACK_INTR_ON_EXIT; |
| 6479 | |
| 6480 | /* We support free control of debug control saving. */ |
| 6481 | msrs->exit_ctls_low &= ~VM_EXIT_SAVE_DEBUG_CONTROLS; |
| 6482 | |
| 6483 | /* entry controls */ |
| 6484 | rdmsr(MSR_IA32_VMX_ENTRY_CTLS, |
| 6485 | msrs->entry_ctls_low, |
| 6486 | msrs->entry_ctls_high); |
| 6487 | msrs->entry_ctls_low = |
| 6488 | VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR; |
| 6489 | msrs->entry_ctls_high &= |
| 6490 | #ifdef CONFIG_X86_64 |
| 6491 | VM_ENTRY_IA32E_MODE | |
| 6492 | #endif |
Chenyi Qiang | efc8313 | 2020-08-28 16:56:18 +0800 | [diff] [blame] | 6493 | VM_ENTRY_LOAD_IA32_PAT | VM_ENTRY_LOAD_BNDCFGS | |
| 6494 | VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6495 | msrs->entry_ctls_high |= |
| 6496 | (VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR | VM_ENTRY_LOAD_IA32_EFER); |
| 6497 | |
| 6498 | /* We support free control of debug control loading. */ |
| 6499 | msrs->entry_ctls_low &= ~VM_ENTRY_LOAD_DEBUG_CONTROLS; |
| 6500 | |
| 6501 | /* cpu-based controls */ |
| 6502 | rdmsr(MSR_IA32_VMX_PROCBASED_CTLS, |
| 6503 | msrs->procbased_ctls_low, |
| 6504 | msrs->procbased_ctls_high); |
| 6505 | msrs->procbased_ctls_low = |
| 6506 | CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR; |
| 6507 | msrs->procbased_ctls_high &= |
Xiaoyao Li | 9dadc2f | 2019-12-06 16:45:24 +0800 | [diff] [blame] | 6508 | CPU_BASED_INTR_WINDOW_EXITING | |
Xiaoyao Li | 5e3d394 | 2019-12-06 16:45:26 +0800 | [diff] [blame] | 6509 | CPU_BASED_NMI_WINDOW_EXITING | CPU_BASED_USE_TSC_OFFSETTING | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6510 | CPU_BASED_HLT_EXITING | CPU_BASED_INVLPG_EXITING | |
| 6511 | CPU_BASED_MWAIT_EXITING | CPU_BASED_CR3_LOAD_EXITING | |
| 6512 | CPU_BASED_CR3_STORE_EXITING | |
| 6513 | #ifdef CONFIG_X86_64 |
| 6514 | CPU_BASED_CR8_LOAD_EXITING | CPU_BASED_CR8_STORE_EXITING | |
| 6515 | #endif |
| 6516 | CPU_BASED_MOV_DR_EXITING | CPU_BASED_UNCOND_IO_EXITING | |
| 6517 | CPU_BASED_USE_IO_BITMAPS | CPU_BASED_MONITOR_TRAP_FLAG | |
| 6518 | CPU_BASED_MONITOR_EXITING | CPU_BASED_RDPMC_EXITING | |
| 6519 | CPU_BASED_RDTSC_EXITING | CPU_BASED_PAUSE_EXITING | |
| 6520 | CPU_BASED_TPR_SHADOW | CPU_BASED_ACTIVATE_SECONDARY_CONTROLS; |
| 6521 | /* |
| 6522 | * We can allow some features even when not supported by the |
| 6523 | * hardware. For example, L1 can specify an MSR bitmap - and we |
| 6524 | * can use it to avoid exits to L1 - even when L0 runs L2 |
| 6525 | * without MSR bitmaps. |
| 6526 | */ |
| 6527 | msrs->procbased_ctls_high |= |
| 6528 | CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR | |
| 6529 | CPU_BASED_USE_MSR_BITMAPS; |
| 6530 | |
| 6531 | /* We support free control of CR3 access interception. */ |
| 6532 | msrs->procbased_ctls_low &= |
| 6533 | ~(CPU_BASED_CR3_LOAD_EXITING | CPU_BASED_CR3_STORE_EXITING); |
| 6534 | |
| 6535 | /* |
| 6536 | * secondary cpu-based controls. Do not include those that |
Xiaoyao Li | 7c1b761 | 2020-07-09 12:34:25 +0800 | [diff] [blame] | 6537 | * depend on CPUID bits, they are added later by |
| 6538 | * vmx_vcpu_after_set_cpuid. |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6539 | */ |
Vitaly Kuznetsov | 6b1971c | 2019-02-07 11:42:14 +0100 | [diff] [blame] | 6540 | if (msrs->procbased_ctls_high & CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) |
| 6541 | rdmsr(MSR_IA32_VMX_PROCBASED_CTLS2, |
| 6542 | msrs->secondary_ctls_low, |
| 6543 | msrs->secondary_ctls_high); |
| 6544 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6545 | msrs->secondary_ctls_low = 0; |
| 6546 | msrs->secondary_ctls_high &= |
| 6547 | SECONDARY_EXEC_DESC | |
Sean Christopherson | 7f3603b | 2020-09-23 09:50:47 -0700 | [diff] [blame] | 6548 | SECONDARY_EXEC_ENABLE_RDTSCP | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6549 | SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE | |
Paolo Bonzini | 6defc59 | 2019-07-02 14:39:29 +0200 | [diff] [blame] | 6550 | SECONDARY_EXEC_WBINVD_EXITING | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6551 | SECONDARY_EXEC_APIC_REGISTER_VIRT | |
| 6552 | SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY | |
Paolo Bonzini | 6defc59 | 2019-07-02 14:39:29 +0200 | [diff] [blame] | 6553 | SECONDARY_EXEC_RDRAND_EXITING | |
| 6554 | SECONDARY_EXEC_ENABLE_INVPCID | |
| 6555 | SECONDARY_EXEC_RDSEED_EXITING | |
Ilias Stamatis | d041b5e | 2021-05-26 19:44:17 +0100 | [diff] [blame] | 6556 | SECONDARY_EXEC_XSAVES | |
| 6557 | SECONDARY_EXEC_TSC_SCALING; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6558 | |
| 6559 | /* |
| 6560 | * We can emulate "VMCS shadowing," even if the hardware |
| 6561 | * doesn't support it. |
| 6562 | */ |
| 6563 | msrs->secondary_ctls_high |= |
| 6564 | SECONDARY_EXEC_SHADOW_VMCS; |
| 6565 | |
| 6566 | if (enable_ept) { |
| 6567 | /* nested EPT: emulate EPT also to L1 */ |
| 6568 | msrs->secondary_ctls_high |= |
| 6569 | SECONDARY_EXEC_ENABLE_EPT; |
Sean Christopherson | bb1fcc7 | 2020-03-02 18:02:36 -0800 | [diff] [blame] | 6570 | msrs->ept_caps = |
| 6571 | VMX_EPT_PAGE_WALK_4_BIT | |
| 6572 | VMX_EPT_PAGE_WALK_5_BIT | |
| 6573 | VMX_EPTP_WB_BIT | |
Sean Christopherson | 96d4701 | 2020-03-02 18:02:40 -0800 | [diff] [blame] | 6574 | VMX_EPT_INVEPT_BIT | |
| 6575 | VMX_EPT_EXECUTE_ONLY_BIT; |
| 6576 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6577 | msrs->ept_caps &= ept_caps; |
| 6578 | msrs->ept_caps |= VMX_EPT_EXTENT_GLOBAL_BIT | |
| 6579 | VMX_EPT_EXTENT_CONTEXT_BIT | VMX_EPT_2MB_PAGE_BIT | |
| 6580 | VMX_EPT_1GB_PAGE_BIT; |
| 6581 | if (enable_ept_ad_bits) { |
| 6582 | msrs->secondary_ctls_high |= |
| 6583 | SECONDARY_EXEC_ENABLE_PML; |
| 6584 | msrs->ept_caps |= VMX_EPT_AD_BIT; |
| 6585 | } |
| 6586 | } |
| 6587 | |
| 6588 | if (cpu_has_vmx_vmfunc()) { |
| 6589 | msrs->secondary_ctls_high |= |
| 6590 | SECONDARY_EXEC_ENABLE_VMFUNC; |
| 6591 | /* |
| 6592 | * Advertise EPTP switching unconditionally |
| 6593 | * since we emulate it |
| 6594 | */ |
| 6595 | if (enable_ept) |
| 6596 | msrs->vmfunc_controls = |
| 6597 | VMX_VMFUNC_EPTP_SWITCHING; |
| 6598 | } |
| 6599 | |
| 6600 | /* |
| 6601 | * Old versions of KVM use the single-context version without |
| 6602 | * checking for support, so declare that it is supported even |
| 6603 | * though it is treated as global context. The alternative is |
| 6604 | * not failing the single-context invvpid, and it is worse. |
| 6605 | */ |
| 6606 | if (enable_vpid) { |
| 6607 | msrs->secondary_ctls_high |= |
| 6608 | SECONDARY_EXEC_ENABLE_VPID; |
| 6609 | msrs->vpid_caps = VMX_VPID_INVVPID_BIT | |
| 6610 | VMX_VPID_EXTENT_SUPPORTED_MASK; |
| 6611 | } |
| 6612 | |
| 6613 | if (enable_unrestricted_guest) |
| 6614 | msrs->secondary_ctls_high |= |
| 6615 | SECONDARY_EXEC_UNRESTRICTED_GUEST; |
| 6616 | |
| 6617 | if (flexpriority_enabled) |
| 6618 | msrs->secondary_ctls_high |= |
| 6619 | SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES; |
| 6620 | |
Sean Christopherson | 72add91 | 2021-04-12 16:21:42 +1200 | [diff] [blame] | 6621 | if (enable_sgx) |
| 6622 | msrs->secondary_ctls_high |= SECONDARY_EXEC_ENCLS_EXITING; |
| 6623 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6624 | /* miscellaneous data */ |
| 6625 | rdmsr(MSR_IA32_VMX_MISC, |
| 6626 | msrs->misc_low, |
| 6627 | msrs->misc_high); |
| 6628 | msrs->misc_low &= VMX_MISC_SAVE_EFER_LMA; |
| 6629 | msrs->misc_low |= |
| 6630 | MSR_IA32_VMX_MISC_VMWRITE_SHADOW_RO_FIELDS | |
| 6631 | VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE | |
Yadong Qi | bf0cd88 | 2020-11-06 14:51:22 +0800 | [diff] [blame] | 6632 | VMX_MISC_ACTIVITY_HLT | |
| 6633 | VMX_MISC_ACTIVITY_WAIT_SIPI; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6634 | msrs->misc_high = 0; |
| 6635 | |
| 6636 | /* |
| 6637 | * This MSR reports some information about VMX support. We |
| 6638 | * should return information about the VMX we emulate for the |
| 6639 | * guest, and the VMCS structure we give it - not about the |
| 6640 | * VMX support of the underlying hardware. |
| 6641 | */ |
| 6642 | msrs->basic = |
| 6643 | VMCS12_REVISION | |
| 6644 | VMX_BASIC_TRUE_CTLS | |
| 6645 | ((u64)VMCS12_SIZE << VMX_BASIC_VMCS_SIZE_SHIFT) | |
| 6646 | (VMX_BASIC_MEM_TYPE_WB << VMX_BASIC_MEM_TYPE_SHIFT); |
| 6647 | |
| 6648 | if (cpu_has_vmx_basic_inout()) |
| 6649 | msrs->basic |= VMX_BASIC_INOUT; |
| 6650 | |
| 6651 | /* |
| 6652 | * These MSRs specify bits which the guest must keep fixed on |
| 6653 | * while L1 is in VMXON mode (in L1's root mode, or running an L2). |
| 6654 | * We picked the standard core2 setting. |
| 6655 | */ |
| 6656 | #define VMXON_CR0_ALWAYSON (X86_CR0_PE | X86_CR0_PG | X86_CR0_NE) |
| 6657 | #define VMXON_CR4_ALWAYSON X86_CR4_VMXE |
| 6658 | msrs->cr0_fixed0 = VMXON_CR0_ALWAYSON; |
| 6659 | msrs->cr4_fixed0 = VMXON_CR4_ALWAYSON; |
| 6660 | |
| 6661 | /* These MSRs specify bits which the guest must keep fixed off. */ |
| 6662 | rdmsrl(MSR_IA32_VMX_CR0_FIXED1, msrs->cr0_fixed1); |
| 6663 | rdmsrl(MSR_IA32_VMX_CR4_FIXED1, msrs->cr4_fixed1); |
| 6664 | |
Sean Christopherson | ba1f824 | 2021-06-18 14:46:58 -0700 | [diff] [blame] | 6665 | msrs->vmcs_enum = nested_vmx_calc_vmcs_enum_msr(); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6666 | } |
| 6667 | |
| 6668 | void nested_vmx_hardware_unsetup(void) |
| 6669 | { |
| 6670 | int i; |
| 6671 | |
| 6672 | if (enable_shadow_vmcs) { |
| 6673 | for (i = 0; i < VMX_BITMAP_NR; i++) |
| 6674 | free_page((unsigned long)vmx_bitmap[i]); |
| 6675 | } |
| 6676 | } |
| 6677 | |
Sean Christopherson | 6c1c6e5 | 2020-05-06 13:46:53 -0700 | [diff] [blame] | 6678 | __init int nested_vmx_hardware_setup(int (*exit_handlers[])(struct kvm_vcpu *)) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6679 | { |
| 6680 | int i; |
| 6681 | |
| 6682 | if (!cpu_has_vmx_shadow_vmcs()) |
| 6683 | enable_shadow_vmcs = 0; |
| 6684 | if (enable_shadow_vmcs) { |
| 6685 | for (i = 0; i < VMX_BITMAP_NR; i++) { |
Ben Gardon | 4183683 | 2019-02-11 11:02:52 -0800 | [diff] [blame] | 6686 | /* |
| 6687 | * The vmx_bitmap is not tied to a VM and so should |
| 6688 | * not be charged to a memcg. |
| 6689 | */ |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6690 | vmx_bitmap[i] = (unsigned long *) |
| 6691 | __get_free_page(GFP_KERNEL); |
| 6692 | if (!vmx_bitmap[i]) { |
| 6693 | nested_vmx_hardware_unsetup(); |
| 6694 | return -ENOMEM; |
| 6695 | } |
| 6696 | } |
| 6697 | |
| 6698 | init_vmcs_shadow_fields(); |
| 6699 | } |
| 6700 | |
Liran Alon | cc87767 | 2019-11-18 21:11:21 +0200 | [diff] [blame] | 6701 | exit_handlers[EXIT_REASON_VMCLEAR] = handle_vmclear; |
| 6702 | exit_handlers[EXIT_REASON_VMLAUNCH] = handle_vmlaunch; |
| 6703 | exit_handlers[EXIT_REASON_VMPTRLD] = handle_vmptrld; |
| 6704 | exit_handlers[EXIT_REASON_VMPTRST] = handle_vmptrst; |
| 6705 | exit_handlers[EXIT_REASON_VMREAD] = handle_vmread; |
| 6706 | exit_handlers[EXIT_REASON_VMRESUME] = handle_vmresume; |
| 6707 | exit_handlers[EXIT_REASON_VMWRITE] = handle_vmwrite; |
| 6708 | exit_handlers[EXIT_REASON_VMOFF] = handle_vmoff; |
| 6709 | exit_handlers[EXIT_REASON_VMON] = handle_vmon; |
| 6710 | exit_handlers[EXIT_REASON_INVEPT] = handle_invept; |
| 6711 | exit_handlers[EXIT_REASON_INVVPID] = handle_invvpid; |
| 6712 | exit_handlers[EXIT_REASON_VMFUNC] = handle_vmfunc; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6713 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6714 | return 0; |
| 6715 | } |
Paolo Bonzini | 33b2217 | 2020-04-17 10:24:18 -0400 | [diff] [blame] | 6716 | |
| 6717 | struct kvm_x86_nested_ops vmx_nested_ops = { |
| 6718 | .check_events = vmx_check_nested_events, |
Sean Christopherson | d2060bd | 2020-04-22 19:25:39 -0700 | [diff] [blame] | 6719 | .hv_timer_pending = nested_vmx_preemption_timer_pending, |
Sean Christopherson | cb6a32c | 2021-03-02 09:45:14 -0800 | [diff] [blame] | 6720 | .triple_fault = nested_vmx_triple_fault, |
Paolo Bonzini | 33b2217 | 2020-04-17 10:24:18 -0400 | [diff] [blame] | 6721 | .get_state = vmx_get_nested_state, |
| 6722 | .set_state = vmx_set_nested_state, |
Paolo Bonzini | 9a78e15 | 2021-01-08 11:43:08 -0500 | [diff] [blame] | 6723 | .get_nested_state_pages = vmx_get_nested_state_pages, |
Sean Christopherson | 02f5fb2 | 2020-06-22 14:58:32 -0700 | [diff] [blame] | 6724 | .write_log_dirty = nested_vmx_write_pml_buffer, |
Paolo Bonzini | 33b2217 | 2020-04-17 10:24:18 -0400 | [diff] [blame] | 6725 | .enable_evmcs = nested_enable_evmcs, |
| 6726 | .get_evmcs_version = nested_get_evmcs_version, |
| 6727 | }; |