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 | { |
David Woodhouse | 297d597 | 2021-11-15 16:50:24 +0000 | [diff] [blame] | 673 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 674 | struct gfn_to_hva_cache *ghc = &vmx->nested.shadow_vmcs12_cache; |
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 | |
David Woodhouse | 297d597 | 2021-11-15 16:50:24 +0000 | [diff] [blame] | 680 | if (ghc->gpa != vmcs12->vmcs_link_pointer && |
| 681 | kvm_gfn_to_hva_cache_init(vcpu->kvm, ghc, |
| 682 | vmcs12->vmcs_link_pointer, VMCS12_SIZE)) |
KarimAllah Ahmed | 8892530 | 2019-01-31 21:24:41 +0100 | [diff] [blame] | 683 | return; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 684 | |
David Woodhouse | 297d597 | 2021-11-15 16:50:24 +0000 | [diff] [blame] | 685 | kvm_read_guest_cached(vmx->vcpu.kvm, ghc, get_shadow_vmcs12(vcpu), |
| 686 | VMCS12_SIZE); |
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); |
David Woodhouse | 297d597 | 2021-11-15 16:50:24 +0000 | [diff] [blame] | 693 | struct gfn_to_hva_cache *ghc = &vmx->nested.shadow_vmcs12_cache; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 694 | |
| 695 | if (!nested_cpu_has_shadow_vmcs(vmcs12) || |
Yu Zhang | 64c7850 | 2021-09-30 01:51:53 +0800 | [diff] [blame] | 696 | vmcs12->vmcs_link_pointer == INVALID_GPA) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 697 | return; |
| 698 | |
David Woodhouse | 297d597 | 2021-11-15 16:50:24 +0000 | [diff] [blame] | 699 | if (ghc->gpa != vmcs12->vmcs_link_pointer && |
| 700 | kvm_gfn_to_hva_cache_init(vcpu->kvm, ghc, |
| 701 | vmcs12->vmcs_link_pointer, VMCS12_SIZE)) |
| 702 | return; |
| 703 | |
| 704 | kvm_write_guest_cached(vmx->vcpu.kvm, ghc, get_shadow_vmcs12(vcpu), |
| 705 | VMCS12_SIZE); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 706 | } |
| 707 | |
| 708 | /* |
| 709 | * In nested virtualization, check if L1 has set |
| 710 | * VM_EXIT_ACK_INTR_ON_EXIT |
| 711 | */ |
| 712 | static bool nested_exit_intr_ack_set(struct kvm_vcpu *vcpu) |
| 713 | { |
| 714 | return get_vmcs12(vcpu)->vm_exit_controls & |
| 715 | VM_EXIT_ACK_INTR_ON_EXIT; |
| 716 | } |
| 717 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 718 | static int nested_vmx_check_apic_access_controls(struct kvm_vcpu *vcpu, |
| 719 | struct vmcs12 *vmcs12) |
| 720 | { |
| 721 | if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES) && |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 722 | CC(!page_address_valid(vcpu, vmcs12->apic_access_addr))) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 723 | return -EINVAL; |
| 724 | else |
| 725 | return 0; |
| 726 | } |
| 727 | |
| 728 | static int nested_vmx_check_apicv_controls(struct kvm_vcpu *vcpu, |
| 729 | struct vmcs12 *vmcs12) |
| 730 | { |
| 731 | if (!nested_cpu_has_virt_x2apic_mode(vmcs12) && |
| 732 | !nested_cpu_has_apic_reg_virt(vmcs12) && |
| 733 | !nested_cpu_has_vid(vmcs12) && |
| 734 | !nested_cpu_has_posted_intr(vmcs12)) |
| 735 | return 0; |
| 736 | |
| 737 | /* |
| 738 | * If virtualize x2apic mode is enabled, |
| 739 | * virtualize apic access must be disabled. |
| 740 | */ |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 741 | if (CC(nested_cpu_has_virt_x2apic_mode(vmcs12) && |
| 742 | nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES))) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 743 | return -EINVAL; |
| 744 | |
| 745 | /* |
| 746 | * If virtual interrupt delivery is enabled, |
| 747 | * we must exit on external interrupts. |
| 748 | */ |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 749 | if (CC(nested_cpu_has_vid(vmcs12) && !nested_exit_on_intr(vcpu))) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 750 | return -EINVAL; |
| 751 | |
| 752 | /* |
| 753 | * bits 15:8 should be zero in posted_intr_nv, |
| 754 | * the descriptor address has been already checked |
| 755 | * in nested_get_vmcs12_pages. |
| 756 | * |
| 757 | * bits 5:0 of posted_intr_desc_addr should be zero. |
| 758 | */ |
| 759 | if (nested_cpu_has_posted_intr(vmcs12) && |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 760 | (CC(!nested_cpu_has_vid(vmcs12)) || |
| 761 | CC(!nested_exit_intr_ack_set(vcpu)) || |
| 762 | CC((vmcs12->posted_intr_nv & 0xff00)) || |
Sean Christopherson | 636e8b7 | 2021-02-03 16:01:10 -0800 | [diff] [blame] | 763 | 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] | 764 | return -EINVAL; |
| 765 | |
| 766 | /* tpr shadow is needed by all apicv features. */ |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 767 | if (CC(!nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW))) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 768 | return -EINVAL; |
| 769 | |
| 770 | return 0; |
| 771 | } |
| 772 | |
| 773 | static int nested_vmx_check_msr_switch(struct kvm_vcpu *vcpu, |
Sean Christopherson | f9b245e | 2018-12-12 13:30:08 -0500 | [diff] [blame] | 774 | u32 count, u64 addr) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 775 | { |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 776 | if (count == 0) |
| 777 | return 0; |
Sean Christopherson | 636e8b7 | 2021-02-03 16:01:10 -0800 | [diff] [blame] | 778 | |
| 779 | if (!kvm_vcpu_is_legal_aligned_gpa(vcpu, addr, 16) || |
| 780 | !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] | 781 | return -EINVAL; |
Sean Christopherson | f9b245e | 2018-12-12 13:30:08 -0500 | [diff] [blame] | 782 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 783 | return 0; |
| 784 | } |
| 785 | |
Krish Sadhukhan | 61446ba | 2018-12-12 13:30:09 -0500 | [diff] [blame] | 786 | static int nested_vmx_check_exit_msr_switch_controls(struct kvm_vcpu *vcpu, |
| 787 | struct vmcs12 *vmcs12) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 788 | { |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 789 | if (CC(nested_vmx_check_msr_switch(vcpu, |
| 790 | vmcs12->vm_exit_msr_load_count, |
| 791 | vmcs12->vm_exit_msr_load_addr)) || |
| 792 | CC(nested_vmx_check_msr_switch(vcpu, |
| 793 | vmcs12->vm_exit_msr_store_count, |
| 794 | vmcs12->vm_exit_msr_store_addr))) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 795 | return -EINVAL; |
Sean Christopherson | f9b245e | 2018-12-12 13:30:08 -0500 | [diff] [blame] | 796 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 797 | return 0; |
| 798 | } |
| 799 | |
Krish Sadhukhan | 5fbf963 | 2018-12-12 13:30:10 -0500 | [diff] [blame] | 800 | static int nested_vmx_check_entry_msr_switch_controls(struct kvm_vcpu *vcpu, |
| 801 | struct vmcs12 *vmcs12) |
Krish Sadhukhan | 61446ba | 2018-12-12 13:30:09 -0500 | [diff] [blame] | 802 | { |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 803 | if (CC(nested_vmx_check_msr_switch(vcpu, |
| 804 | vmcs12->vm_entry_msr_load_count, |
| 805 | vmcs12->vm_entry_msr_load_addr))) |
Krish Sadhukhan | 61446ba | 2018-12-12 13:30:09 -0500 | [diff] [blame] | 806 | return -EINVAL; |
| 807 | |
| 808 | return 0; |
| 809 | } |
| 810 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 811 | static int nested_vmx_check_pml_controls(struct kvm_vcpu *vcpu, |
| 812 | struct vmcs12 *vmcs12) |
| 813 | { |
| 814 | if (!nested_cpu_has_pml(vmcs12)) |
| 815 | return 0; |
| 816 | |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 817 | if (CC(!nested_cpu_has_ept(vmcs12)) || |
| 818 | CC(!page_address_valid(vcpu, vmcs12->pml_address))) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 819 | return -EINVAL; |
| 820 | |
| 821 | return 0; |
| 822 | } |
| 823 | |
| 824 | static int nested_vmx_check_unrestricted_guest_controls(struct kvm_vcpu *vcpu, |
| 825 | struct vmcs12 *vmcs12) |
| 826 | { |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 827 | if (CC(nested_cpu_has2(vmcs12, SECONDARY_EXEC_UNRESTRICTED_GUEST) && |
| 828 | !nested_cpu_has_ept(vmcs12))) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 829 | return -EINVAL; |
| 830 | return 0; |
| 831 | } |
| 832 | |
| 833 | static int nested_vmx_check_mode_based_ept_exec_controls(struct kvm_vcpu *vcpu, |
| 834 | struct vmcs12 *vmcs12) |
| 835 | { |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 836 | if (CC(nested_cpu_has2(vmcs12, SECONDARY_EXEC_MODE_BASED_EPT_EXEC) && |
| 837 | !nested_cpu_has_ept(vmcs12))) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 838 | return -EINVAL; |
| 839 | return 0; |
| 840 | } |
| 841 | |
| 842 | static int nested_vmx_check_shadow_vmcs_controls(struct kvm_vcpu *vcpu, |
| 843 | struct vmcs12 *vmcs12) |
| 844 | { |
| 845 | if (!nested_cpu_has_shadow_vmcs(vmcs12)) |
| 846 | return 0; |
| 847 | |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 848 | if (CC(!page_address_valid(vcpu, vmcs12->vmread_bitmap)) || |
| 849 | CC(!page_address_valid(vcpu, vmcs12->vmwrite_bitmap))) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 850 | return -EINVAL; |
| 851 | |
| 852 | return 0; |
| 853 | } |
| 854 | |
| 855 | static int nested_vmx_msr_check_common(struct kvm_vcpu *vcpu, |
| 856 | struct vmx_msr_entry *e) |
| 857 | { |
| 858 | /* x2APIC MSR accesses are not allowed */ |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 859 | if (CC(vcpu->arch.apic_base & X2APIC_ENABLE && e->index >> 8 == 0x8)) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 860 | return -EINVAL; |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 861 | if (CC(e->index == MSR_IA32_UCODE_WRITE) || /* SDM Table 35-2 */ |
| 862 | CC(e->index == MSR_IA32_UCODE_REV)) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 863 | return -EINVAL; |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 864 | if (CC(e->reserved != 0)) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 865 | return -EINVAL; |
| 866 | return 0; |
| 867 | } |
| 868 | |
| 869 | static int nested_vmx_load_msr_check(struct kvm_vcpu *vcpu, |
| 870 | struct vmx_msr_entry *e) |
| 871 | { |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 872 | if (CC(e->index == MSR_FS_BASE) || |
| 873 | CC(e->index == MSR_GS_BASE) || |
| 874 | CC(e->index == MSR_IA32_SMM_MONITOR_CTL) || /* SMM is not supported */ |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 875 | nested_vmx_msr_check_common(vcpu, e)) |
| 876 | return -EINVAL; |
| 877 | return 0; |
| 878 | } |
| 879 | |
| 880 | static int nested_vmx_store_msr_check(struct kvm_vcpu *vcpu, |
| 881 | struct vmx_msr_entry *e) |
| 882 | { |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 883 | if (CC(e->index == MSR_IA32_SMBASE) || /* SMM is not supported */ |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 884 | nested_vmx_msr_check_common(vcpu, e)) |
| 885 | return -EINVAL; |
| 886 | return 0; |
| 887 | } |
| 888 | |
Marc Orr | f0b5105 | 2019-09-17 11:50:57 -0700 | [diff] [blame] | 889 | static u32 nested_vmx_max_atomic_switch_msrs(struct kvm_vcpu *vcpu) |
| 890 | { |
| 891 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 892 | u64 vmx_misc = vmx_control_msr(vmx->nested.msrs.misc_low, |
| 893 | vmx->nested.msrs.misc_high); |
| 894 | |
| 895 | return (vmx_misc_max_msr(vmx_misc) + 1) * VMX_MISC_MSR_LIST_MULTIPLIER; |
| 896 | } |
| 897 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 898 | /* |
| 899 | * Load guest's/host's msr at nested entry/exit. |
| 900 | * return 0 for success, entry index for failure. |
Marc Orr | f0b5105 | 2019-09-17 11:50:57 -0700 | [diff] [blame] | 901 | * |
| 902 | * One of the failure modes for MSR load/store is when a list exceeds the |
| 903 | * virtual hardware's capacity. To maintain compatibility with hardware inasmuch |
| 904 | * as possible, process all valid entries before failing rather than precheck |
| 905 | * for a capacity violation. |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 906 | */ |
| 907 | static u32 nested_vmx_load_msr(struct kvm_vcpu *vcpu, u64 gpa, u32 count) |
| 908 | { |
| 909 | u32 i; |
| 910 | struct vmx_msr_entry e; |
Marc Orr | f0b5105 | 2019-09-17 11:50:57 -0700 | [diff] [blame] | 911 | u32 max_msr_list_size = nested_vmx_max_atomic_switch_msrs(vcpu); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 912 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 913 | for (i = 0; i < count; i++) { |
Marc Orr | f0b5105 | 2019-09-17 11:50:57 -0700 | [diff] [blame] | 914 | if (unlikely(i >= max_msr_list_size)) |
| 915 | goto fail; |
| 916 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 917 | if (kvm_vcpu_read_guest(vcpu, gpa + i * sizeof(e), |
| 918 | &e, sizeof(e))) { |
| 919 | pr_debug_ratelimited( |
| 920 | "%s cannot read MSR entry (%u, 0x%08llx)\n", |
| 921 | __func__, i, gpa + i * sizeof(e)); |
| 922 | goto fail; |
| 923 | } |
| 924 | if (nested_vmx_load_msr_check(vcpu, &e)) { |
| 925 | pr_debug_ratelimited( |
| 926 | "%s check failed (%u, 0x%x, 0x%x)\n", |
| 927 | __func__, i, e.index, e.reserved); |
| 928 | goto fail; |
| 929 | } |
Sean Christopherson | f20935d | 2019-09-05 14:22:54 -0700 | [diff] [blame] | 930 | if (kvm_set_msr(vcpu, e.index, e.value)) { |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 931 | pr_debug_ratelimited( |
| 932 | "%s cannot write MSR (%u, 0x%x, 0x%llx)\n", |
| 933 | __func__, i, e.index, e.value); |
| 934 | goto fail; |
| 935 | } |
| 936 | } |
| 937 | return 0; |
| 938 | fail: |
Sean Christopherson | 68cda40 | 2020-05-11 15:05:29 -0700 | [diff] [blame] | 939 | /* 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] | 940 | return i + 1; |
| 941 | } |
| 942 | |
Aaron Lewis | 662f1d1 | 2019-11-07 21:14:39 -0800 | [diff] [blame] | 943 | static bool nested_vmx_get_vmexit_msr_value(struct kvm_vcpu *vcpu, |
| 944 | u32 msr_index, |
| 945 | u64 *data) |
| 946 | { |
| 947 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 948 | |
| 949 | /* |
| 950 | * If the L0 hypervisor stored a more accurate value for the TSC that |
| 951 | * does not include the time taken for emulation of the L2->L1 |
| 952 | * VM-exit in L0, use the more accurate value. |
| 953 | */ |
| 954 | if (msr_index == MSR_IA32_TSC) { |
Sean Christopherson | a128a93 | 2020-09-23 11:03:57 -0700 | [diff] [blame] | 955 | int i = vmx_find_loadstore_msr_slot(&vmx->msr_autostore.guest, |
| 956 | MSR_IA32_TSC); |
Aaron Lewis | 662f1d1 | 2019-11-07 21:14:39 -0800 | [diff] [blame] | 957 | |
Sean Christopherson | a128a93 | 2020-09-23 11:03:57 -0700 | [diff] [blame] | 958 | if (i >= 0) { |
| 959 | u64 val = vmx->msr_autostore.guest.val[i].value; |
Aaron Lewis | 662f1d1 | 2019-11-07 21:14:39 -0800 | [diff] [blame] | 960 | |
| 961 | *data = kvm_read_l1_tsc(vcpu, val); |
| 962 | return true; |
| 963 | } |
| 964 | } |
| 965 | |
| 966 | if (kvm_get_msr(vcpu, msr_index, data)) { |
| 967 | pr_debug_ratelimited("%s cannot read MSR (0x%x)\n", __func__, |
| 968 | msr_index); |
| 969 | return false; |
| 970 | } |
| 971 | return true; |
| 972 | } |
| 973 | |
Aaron Lewis | 365d3d5 | 2019-11-07 21:14:36 -0800 | [diff] [blame] | 974 | static bool read_and_check_msr_entry(struct kvm_vcpu *vcpu, u64 gpa, int i, |
| 975 | struct vmx_msr_entry *e) |
| 976 | { |
| 977 | if (kvm_vcpu_read_guest(vcpu, |
| 978 | gpa + i * sizeof(*e), |
| 979 | e, 2 * sizeof(u32))) { |
| 980 | pr_debug_ratelimited( |
| 981 | "%s cannot read MSR entry (%u, 0x%08llx)\n", |
| 982 | __func__, i, gpa + i * sizeof(*e)); |
| 983 | return false; |
| 984 | } |
| 985 | if (nested_vmx_store_msr_check(vcpu, e)) { |
| 986 | pr_debug_ratelimited( |
| 987 | "%s check failed (%u, 0x%x, 0x%x)\n", |
| 988 | __func__, i, e->index, e->reserved); |
| 989 | return false; |
| 990 | } |
| 991 | return true; |
| 992 | } |
| 993 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 994 | static int nested_vmx_store_msr(struct kvm_vcpu *vcpu, u64 gpa, u32 count) |
| 995 | { |
Sean Christopherson | f20935d | 2019-09-05 14:22:54 -0700 | [diff] [blame] | 996 | u64 data; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 997 | u32 i; |
| 998 | struct vmx_msr_entry e; |
Marc Orr | f0b5105 | 2019-09-17 11:50:57 -0700 | [diff] [blame] | 999 | u32 max_msr_list_size = nested_vmx_max_atomic_switch_msrs(vcpu); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1000 | |
| 1001 | for (i = 0; i < count; i++) { |
Marc Orr | f0b5105 | 2019-09-17 11:50:57 -0700 | [diff] [blame] | 1002 | if (unlikely(i >= max_msr_list_size)) |
| 1003 | return -EINVAL; |
| 1004 | |
Aaron Lewis | 365d3d5 | 2019-11-07 21:14:36 -0800 | [diff] [blame] | 1005 | if (!read_and_check_msr_entry(vcpu, gpa, i, &e)) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1006 | return -EINVAL; |
Aaron Lewis | 365d3d5 | 2019-11-07 21:14:36 -0800 | [diff] [blame] | 1007 | |
Aaron Lewis | 662f1d1 | 2019-11-07 21:14:39 -0800 | [diff] [blame] | 1008 | if (!nested_vmx_get_vmexit_msr_value(vcpu, e.index, &data)) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1009 | return -EINVAL; |
Aaron Lewis | 662f1d1 | 2019-11-07 21:14:39 -0800 | [diff] [blame] | 1010 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1011 | if (kvm_vcpu_write_guest(vcpu, |
| 1012 | gpa + i * sizeof(e) + |
| 1013 | offsetof(struct vmx_msr_entry, value), |
Sean Christopherson | f20935d | 2019-09-05 14:22:54 -0700 | [diff] [blame] | 1014 | &data, sizeof(data))) { |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1015 | pr_debug_ratelimited( |
| 1016 | "%s cannot write MSR (%u, 0x%x, 0x%llx)\n", |
Sean Christopherson | f20935d | 2019-09-05 14:22:54 -0700 | [diff] [blame] | 1017 | __func__, i, e.index, data); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1018 | return -EINVAL; |
| 1019 | } |
| 1020 | } |
| 1021 | return 0; |
| 1022 | } |
| 1023 | |
Aaron Lewis | 662f1d1 | 2019-11-07 21:14:39 -0800 | [diff] [blame] | 1024 | static bool nested_msr_store_list_has_msr(struct kvm_vcpu *vcpu, u32 msr_index) |
| 1025 | { |
| 1026 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
| 1027 | u32 count = vmcs12->vm_exit_msr_store_count; |
| 1028 | u64 gpa = vmcs12->vm_exit_msr_store_addr; |
| 1029 | struct vmx_msr_entry e; |
| 1030 | u32 i; |
| 1031 | |
| 1032 | for (i = 0; i < count; i++) { |
| 1033 | if (!read_and_check_msr_entry(vcpu, gpa, i, &e)) |
| 1034 | return false; |
| 1035 | |
| 1036 | if (e.index == msr_index) |
| 1037 | return true; |
| 1038 | } |
| 1039 | return false; |
| 1040 | } |
| 1041 | |
| 1042 | static void prepare_vmx_msr_autostore_list(struct kvm_vcpu *vcpu, |
| 1043 | u32 msr_index) |
| 1044 | { |
| 1045 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 1046 | struct vmx_msrs *autostore = &vmx->msr_autostore.guest; |
| 1047 | bool in_vmcs12_store_list; |
Sean Christopherson | a128a93 | 2020-09-23 11:03:57 -0700 | [diff] [blame] | 1048 | int msr_autostore_slot; |
Aaron Lewis | 662f1d1 | 2019-11-07 21:14:39 -0800 | [diff] [blame] | 1049 | bool in_autostore_list; |
| 1050 | int last; |
| 1051 | |
Sean Christopherson | a128a93 | 2020-09-23 11:03:57 -0700 | [diff] [blame] | 1052 | msr_autostore_slot = vmx_find_loadstore_msr_slot(autostore, msr_index); |
| 1053 | in_autostore_list = msr_autostore_slot >= 0; |
Aaron Lewis | 662f1d1 | 2019-11-07 21:14:39 -0800 | [diff] [blame] | 1054 | in_vmcs12_store_list = nested_msr_store_list_has_msr(vcpu, msr_index); |
| 1055 | |
| 1056 | if (in_vmcs12_store_list && !in_autostore_list) { |
Sean Christopherson | ce833b2 | 2020-09-23 11:03:56 -0700 | [diff] [blame] | 1057 | if (autostore->nr == MAX_NR_LOADSTORE_MSRS) { |
Aaron Lewis | 662f1d1 | 2019-11-07 21:14:39 -0800 | [diff] [blame] | 1058 | /* |
| 1059 | * Emulated VMEntry does not fail here. Instead a less |
| 1060 | * accurate value will be returned by |
| 1061 | * nested_vmx_get_vmexit_msr_value() using kvm_get_msr() |
| 1062 | * instead of reading the value from the vmcs02 VMExit |
| 1063 | * MSR-store area. |
| 1064 | */ |
| 1065 | pr_warn_ratelimited( |
| 1066 | "Not enough msr entries in msr_autostore. Can't add msr %x\n", |
| 1067 | msr_index); |
| 1068 | return; |
| 1069 | } |
| 1070 | last = autostore->nr++; |
| 1071 | autostore->val[last].index = msr_index; |
| 1072 | } else if (!in_vmcs12_store_list && in_autostore_list) { |
| 1073 | last = --autostore->nr; |
Sean Christopherson | a128a93 | 2020-09-23 11:03:57 -0700 | [diff] [blame] | 1074 | autostore->val[msr_autostore_slot] = autostore->val[last]; |
Aaron Lewis | 662f1d1 | 2019-11-07 21:14:39 -0800 | [diff] [blame] | 1075 | } |
| 1076 | } |
| 1077 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1078 | /* |
Sean Christopherson | ea79a75 | 2020-02-04 07:32:59 -0800 | [diff] [blame] | 1079 | * Load guest's/host's cr3 at nested entry/exit. @nested_ept is true if we are |
| 1080 | * emulating VM-Entry into a guest with EPT enabled. On failure, the expected |
| 1081 | * Exit Qualification (for a VM-Entry consistency check VM-Exit) is assigned to |
| 1082 | * @entry_failure_code. |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1083 | */ |
Maxim Levitsky | 0f85722 | 2021-06-07 12:02:00 +0300 | [diff] [blame] | 1084 | static int nested_vmx_load_cr3(struct kvm_vcpu *vcpu, unsigned long cr3, |
| 1085 | bool nested_ept, bool reload_pdptrs, |
Sean Christopherson | 68cda40 | 2020-05-11 15:05:29 -0700 | [diff] [blame] | 1086 | enum vm_entry_failure_code *entry_failure_code) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1087 | { |
Sean Christopherson | 636e8b7 | 2021-02-03 16:01:10 -0800 | [diff] [blame] | 1088 | if (CC(kvm_vcpu_is_illegal_gpa(vcpu, cr3))) { |
Sean Christopherson | 0cc6920 | 2020-05-01 21:32:26 -0700 | [diff] [blame] | 1089 | *entry_failure_code = ENTRY_FAIL_DEFAULT; |
| 1090 | return -EINVAL; |
| 1091 | } |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1092 | |
Sean Christopherson | 0cc6920 | 2020-05-01 21:32:26 -0700 | [diff] [blame] | 1093 | /* |
| 1094 | * If PAE paging and EPT are both on, CR3 is not used by the CPU and |
| 1095 | * must not be dereferenced. |
| 1096 | */ |
Maxim Levitsky | 0f85722 | 2021-06-07 12:02:00 +0300 | [diff] [blame] | 1097 | if (reload_pdptrs && !nested_ept && is_pae_paging(vcpu) && |
Sean Christopherson | bcb72d0 | 2021-06-07 12:01:56 +0300 | [diff] [blame] | 1098 | CC(!load_pdptrs(vcpu, vcpu->arch.walk_mmu, cr3))) { |
| 1099 | *entry_failure_code = ENTRY_FAIL_PDPTE; |
| 1100 | return -EINVAL; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1101 | } |
| 1102 | |
Sean Christopherson | 50a4179 | 2021-06-09 16:42:28 -0700 | [diff] [blame] | 1103 | if (!nested_ept) |
Sean Christopherson | b512910 | 2021-06-09 16:42:27 -0700 | [diff] [blame] | 1104 | kvm_mmu_new_pgd(vcpu, cr3); |
Sean Christopherson | 07ffaf3 | 2021-06-09 16:42:21 -0700 | [diff] [blame] | 1105 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1106 | vcpu->arch.cr3 = cr3; |
Sean Christopherson | cb3c1e2 | 2019-09-27 14:45:22 -0700 | [diff] [blame] | 1107 | kvm_register_mark_available(vcpu, VCPU_EXREG_CR3); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1108 | |
Sean Christopherson | 616007c | 2021-06-22 10:57:34 -0700 | [diff] [blame] | 1109 | /* 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] | 1110 | kvm_init_mmu(vcpu); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1111 | |
| 1112 | return 0; |
| 1113 | } |
| 1114 | |
| 1115 | /* |
| 1116 | * Returns if KVM is able to config CPU to tag TLB entries |
| 1117 | * populated by L2 differently than TLB entries populated |
| 1118 | * by L1. |
| 1119 | * |
Liran Alon | 992edea | 2019-11-20 14:24:52 +0200 | [diff] [blame] | 1120 | * If L0 uses EPT, L1 and L2 run with different EPTP because |
| 1121 | * guest_mode is part of kvm_mmu_page_role. Thus, TLB entries |
| 1122 | * are tagged with different EPTP. |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1123 | * |
| 1124 | * If L1 uses VPID and we allocated a vpid02, TLB entries are tagged |
| 1125 | * with different VPID (L1 entries are tagged with vmx->vpid |
| 1126 | * while L2 entries are tagged with vmx->nested.vpid02). |
| 1127 | */ |
| 1128 | static bool nested_has_guest_tlb_tag(struct kvm_vcpu *vcpu) |
| 1129 | { |
| 1130 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
| 1131 | |
Liran Alon | 992edea | 2019-11-20 14:24:52 +0200 | [diff] [blame] | 1132 | return enable_ept || |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1133 | (nested_cpu_has_vpid(vmcs12) && to_vmx(vcpu)->nested.vpid02); |
| 1134 | } |
| 1135 | |
Sean Christopherson | 50b265a | 2020-03-20 14:28:19 -0700 | [diff] [blame] | 1136 | static void nested_vmx_transition_tlb_flush(struct kvm_vcpu *vcpu, |
| 1137 | struct vmcs12 *vmcs12, |
| 1138 | bool is_vmenter) |
| 1139 | { |
| 1140 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 1141 | |
| 1142 | /* |
Sean Christopherson | 50a4179 | 2021-06-09 16:42:28 -0700 | [diff] [blame] | 1143 | * If vmcs12 doesn't use VPID, L1 expects linear and combined mappings |
| 1144 | * for *all* contexts to be flushed on VM-Enter/VM-Exit, i.e. it's a |
| 1145 | * full TLB flush from the guest's perspective. This is required even |
| 1146 | * if VPID is disabled in the host as KVM may need to synchronize the |
| 1147 | * MMU in response to the guest TLB flush. |
| 1148 | * |
| 1149 | * Note, using TLB_FLUSH_GUEST is correct even if nested EPT is in use. |
| 1150 | * EPT is a special snowflake, as guest-physical mappings aren't |
| 1151 | * flushed on VPID invalidations, including VM-Enter or VM-Exit with |
| 1152 | * VPID disabled. As a result, KVM _never_ needs to sync nEPT |
| 1153 | * entries on VM-Enter because L1 can't rely on VM-Enter to flush |
| 1154 | * those mappings. |
Sean Christopherson | 50b265a | 2020-03-20 14:28:19 -0700 | [diff] [blame] | 1155 | */ |
Sean Christopherson | 50a4179 | 2021-06-09 16:42:28 -0700 | [diff] [blame] | 1156 | if (!nested_cpu_has_vpid(vmcs12)) { |
| 1157 | kvm_make_request(KVM_REQ_TLB_FLUSH_GUEST, vcpu); |
Sean Christopherson | 50b265a | 2020-03-20 14:28:19 -0700 | [diff] [blame] | 1158 | return; |
Sean Christopherson | 50a4179 | 2021-06-09 16:42:28 -0700 | [diff] [blame] | 1159 | } |
| 1160 | |
| 1161 | /* L2 should never have a VPID if VPID is disabled. */ |
| 1162 | WARN_ON(!enable_vpid); |
Sean Christopherson | 50b265a | 2020-03-20 14:28:19 -0700 | [diff] [blame] | 1163 | |
| 1164 | /* |
Sean Christopherson | 50b265a | 2020-03-20 14:28:19 -0700 | [diff] [blame] | 1165 | * If VPID is enabled and used by vmc12, but L2 does not have a unique |
| 1166 | * 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] | 1167 | * a VPID for L2, flush the current context as the effective ASID is |
| 1168 | * common to both L1 and L2. |
Sean Christopherson | 50b265a | 2020-03-20 14:28:19 -0700 | [diff] [blame] | 1169 | * |
| 1170 | * Defer the flush so that it runs after vmcs02.EPTP has been set by |
| 1171 | * KVM_REQ_LOAD_MMU_PGD (if nested EPT is enabled) and to avoid |
| 1172 | * redundant flushes further down the nested pipeline. |
| 1173 | * |
| 1174 | * If a TLB flush isn't required due to any of the above, and vpid12 is |
| 1175 | * changing then the new "virtual" VPID (vpid12) will reuse the same |
Sean Christopherson | 50a4179 | 2021-06-09 16:42:28 -0700 | [diff] [blame] | 1176 | * "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] | 1177 | * mapping between vpid02 and vpid12, vpid02 is per-vCPU and reused for |
Sean Christopherson | 50a4179 | 2021-06-09 16:42:28 -0700 | [diff] [blame] | 1178 | * all nested vCPUs. Remember, a flush on VM-Enter does not invalidate |
| 1179 | * 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] | 1180 | */ |
Sean Christopherson | 50a4179 | 2021-06-09 16:42:28 -0700 | [diff] [blame] | 1181 | if (!nested_has_guest_tlb_tag(vcpu)) { |
Sean Christopherson | c51e1ff | 2020-03-20 14:28:22 -0700 | [diff] [blame] | 1182 | kvm_make_request(KVM_REQ_TLB_FLUSH_CURRENT, vcpu); |
Sean Christopherson | 50b265a | 2020-03-20 14:28:19 -0700 | [diff] [blame] | 1183 | } else if (is_vmenter && |
| 1184 | vmcs12->virtual_processor_id != vmx->nested.last_vpid) { |
| 1185 | vmx->nested.last_vpid = vmcs12->virtual_processor_id; |
| 1186 | vpid_sync_context(nested_get_vpid02(vcpu)); |
| 1187 | } |
| 1188 | } |
| 1189 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1190 | static bool is_bitwise_subset(u64 superset, u64 subset, u64 mask) |
| 1191 | { |
| 1192 | superset &= mask; |
| 1193 | subset &= mask; |
| 1194 | |
| 1195 | return (superset | subset) == superset; |
| 1196 | } |
| 1197 | |
| 1198 | static int vmx_restore_vmx_basic(struct vcpu_vmx *vmx, u64 data) |
| 1199 | { |
| 1200 | const u64 feature_and_reserved = |
| 1201 | /* feature (except bit 48; see below) */ |
| 1202 | BIT_ULL(49) | BIT_ULL(54) | BIT_ULL(55) | |
| 1203 | /* reserved */ |
| 1204 | BIT_ULL(31) | GENMASK_ULL(47, 45) | GENMASK_ULL(63, 56); |
| 1205 | u64 vmx_basic = vmx->nested.msrs.basic; |
| 1206 | |
| 1207 | if (!is_bitwise_subset(vmx_basic, data, feature_and_reserved)) |
| 1208 | return -EINVAL; |
| 1209 | |
| 1210 | /* |
| 1211 | * KVM does not emulate a version of VMX that constrains physical |
| 1212 | * addresses of VMX structures (e.g. VMCS) to 32-bits. |
| 1213 | */ |
| 1214 | if (data & BIT_ULL(48)) |
| 1215 | return -EINVAL; |
| 1216 | |
| 1217 | if (vmx_basic_vmcs_revision_id(vmx_basic) != |
| 1218 | vmx_basic_vmcs_revision_id(data)) |
| 1219 | return -EINVAL; |
| 1220 | |
| 1221 | if (vmx_basic_vmcs_size(vmx_basic) > vmx_basic_vmcs_size(data)) |
| 1222 | return -EINVAL; |
| 1223 | |
| 1224 | vmx->nested.msrs.basic = data; |
| 1225 | return 0; |
| 1226 | } |
| 1227 | |
| 1228 | static int |
| 1229 | vmx_restore_control_msr(struct vcpu_vmx *vmx, u32 msr_index, u64 data) |
| 1230 | { |
| 1231 | u64 supported; |
| 1232 | u32 *lowp, *highp; |
| 1233 | |
| 1234 | switch (msr_index) { |
| 1235 | case MSR_IA32_VMX_TRUE_PINBASED_CTLS: |
| 1236 | lowp = &vmx->nested.msrs.pinbased_ctls_low; |
| 1237 | highp = &vmx->nested.msrs.pinbased_ctls_high; |
| 1238 | break; |
| 1239 | case MSR_IA32_VMX_TRUE_PROCBASED_CTLS: |
| 1240 | lowp = &vmx->nested.msrs.procbased_ctls_low; |
| 1241 | highp = &vmx->nested.msrs.procbased_ctls_high; |
| 1242 | break; |
| 1243 | case MSR_IA32_VMX_TRUE_EXIT_CTLS: |
| 1244 | lowp = &vmx->nested.msrs.exit_ctls_low; |
| 1245 | highp = &vmx->nested.msrs.exit_ctls_high; |
| 1246 | break; |
| 1247 | case MSR_IA32_VMX_TRUE_ENTRY_CTLS: |
| 1248 | lowp = &vmx->nested.msrs.entry_ctls_low; |
| 1249 | highp = &vmx->nested.msrs.entry_ctls_high; |
| 1250 | break; |
| 1251 | case MSR_IA32_VMX_PROCBASED_CTLS2: |
| 1252 | lowp = &vmx->nested.msrs.secondary_ctls_low; |
| 1253 | highp = &vmx->nested.msrs.secondary_ctls_high; |
| 1254 | break; |
| 1255 | default: |
| 1256 | BUG(); |
| 1257 | } |
| 1258 | |
| 1259 | supported = vmx_control_msr(*lowp, *highp); |
| 1260 | |
| 1261 | /* Check must-be-1 bits are still 1. */ |
| 1262 | if (!is_bitwise_subset(data, supported, GENMASK_ULL(31, 0))) |
| 1263 | return -EINVAL; |
| 1264 | |
| 1265 | /* Check must-be-0 bits are still 0. */ |
| 1266 | if (!is_bitwise_subset(supported, data, GENMASK_ULL(63, 32))) |
| 1267 | return -EINVAL; |
| 1268 | |
| 1269 | *lowp = data; |
| 1270 | *highp = data >> 32; |
| 1271 | return 0; |
| 1272 | } |
| 1273 | |
| 1274 | static int vmx_restore_vmx_misc(struct vcpu_vmx *vmx, u64 data) |
| 1275 | { |
| 1276 | const u64 feature_and_reserved_bits = |
| 1277 | /* feature */ |
| 1278 | BIT_ULL(5) | GENMASK_ULL(8, 6) | BIT_ULL(14) | BIT_ULL(15) | |
| 1279 | BIT_ULL(28) | BIT_ULL(29) | BIT_ULL(30) | |
| 1280 | /* reserved */ |
| 1281 | GENMASK_ULL(13, 9) | BIT_ULL(31); |
| 1282 | u64 vmx_misc; |
| 1283 | |
| 1284 | vmx_misc = vmx_control_msr(vmx->nested.msrs.misc_low, |
| 1285 | vmx->nested.msrs.misc_high); |
| 1286 | |
| 1287 | if (!is_bitwise_subset(vmx_misc, data, feature_and_reserved_bits)) |
| 1288 | return -EINVAL; |
| 1289 | |
| 1290 | if ((vmx->nested.msrs.pinbased_ctls_high & |
| 1291 | PIN_BASED_VMX_PREEMPTION_TIMER) && |
| 1292 | vmx_misc_preemption_timer_rate(data) != |
| 1293 | vmx_misc_preemption_timer_rate(vmx_misc)) |
| 1294 | return -EINVAL; |
| 1295 | |
| 1296 | if (vmx_misc_cr3_count(data) > vmx_misc_cr3_count(vmx_misc)) |
| 1297 | return -EINVAL; |
| 1298 | |
| 1299 | if (vmx_misc_max_msr(data) > vmx_misc_max_msr(vmx_misc)) |
| 1300 | return -EINVAL; |
| 1301 | |
| 1302 | if (vmx_misc_mseg_revid(data) != vmx_misc_mseg_revid(vmx_misc)) |
| 1303 | return -EINVAL; |
| 1304 | |
| 1305 | vmx->nested.msrs.misc_low = data; |
| 1306 | vmx->nested.msrs.misc_high = data >> 32; |
| 1307 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1308 | return 0; |
| 1309 | } |
| 1310 | |
| 1311 | static int vmx_restore_vmx_ept_vpid_cap(struct vcpu_vmx *vmx, u64 data) |
| 1312 | { |
| 1313 | u64 vmx_ept_vpid_cap; |
| 1314 | |
| 1315 | vmx_ept_vpid_cap = vmx_control_msr(vmx->nested.msrs.ept_caps, |
| 1316 | vmx->nested.msrs.vpid_caps); |
| 1317 | |
| 1318 | /* Every bit is either reserved or a feature bit. */ |
| 1319 | if (!is_bitwise_subset(vmx_ept_vpid_cap, data, -1ULL)) |
| 1320 | return -EINVAL; |
| 1321 | |
| 1322 | vmx->nested.msrs.ept_caps = data; |
| 1323 | vmx->nested.msrs.vpid_caps = data >> 32; |
| 1324 | return 0; |
| 1325 | } |
| 1326 | |
| 1327 | static int vmx_restore_fixed0_msr(struct vcpu_vmx *vmx, u32 msr_index, u64 data) |
| 1328 | { |
| 1329 | u64 *msr; |
| 1330 | |
| 1331 | switch (msr_index) { |
| 1332 | case MSR_IA32_VMX_CR0_FIXED0: |
| 1333 | msr = &vmx->nested.msrs.cr0_fixed0; |
| 1334 | break; |
| 1335 | case MSR_IA32_VMX_CR4_FIXED0: |
| 1336 | msr = &vmx->nested.msrs.cr4_fixed0; |
| 1337 | break; |
| 1338 | default: |
| 1339 | BUG(); |
| 1340 | } |
| 1341 | |
| 1342 | /* |
| 1343 | * 1 bits (which indicates bits which "must-be-1" during VMX operation) |
| 1344 | * must be 1 in the restored value. |
| 1345 | */ |
| 1346 | if (!is_bitwise_subset(data, *msr, -1ULL)) |
| 1347 | return -EINVAL; |
| 1348 | |
| 1349 | *msr = data; |
| 1350 | return 0; |
| 1351 | } |
| 1352 | |
| 1353 | /* |
| 1354 | * Called when userspace is restoring VMX MSRs. |
| 1355 | * |
| 1356 | * Returns 0 on success, non-0 otherwise. |
| 1357 | */ |
| 1358 | int vmx_set_vmx_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 data) |
| 1359 | { |
| 1360 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 1361 | |
| 1362 | /* |
| 1363 | * Don't allow changes to the VMX capability MSRs while the vCPU |
| 1364 | * is in VMX operation. |
| 1365 | */ |
| 1366 | if (vmx->nested.vmxon) |
| 1367 | return -EBUSY; |
| 1368 | |
| 1369 | switch (msr_index) { |
| 1370 | case MSR_IA32_VMX_BASIC: |
| 1371 | return vmx_restore_vmx_basic(vmx, data); |
| 1372 | case MSR_IA32_VMX_PINBASED_CTLS: |
| 1373 | case MSR_IA32_VMX_PROCBASED_CTLS: |
| 1374 | case MSR_IA32_VMX_EXIT_CTLS: |
| 1375 | case MSR_IA32_VMX_ENTRY_CTLS: |
| 1376 | /* |
| 1377 | * The "non-true" VMX capability MSRs are generated from the |
| 1378 | * "true" MSRs, so we do not support restoring them directly. |
| 1379 | * |
| 1380 | * If userspace wants to emulate VMX_BASIC[55]=0, userspace |
| 1381 | * should restore the "true" MSRs with the must-be-1 bits |
| 1382 | * set according to the SDM Vol 3. A.2 "RESERVED CONTROLS AND |
| 1383 | * DEFAULT SETTINGS". |
| 1384 | */ |
| 1385 | return -EINVAL; |
| 1386 | case MSR_IA32_VMX_TRUE_PINBASED_CTLS: |
| 1387 | case MSR_IA32_VMX_TRUE_PROCBASED_CTLS: |
| 1388 | case MSR_IA32_VMX_TRUE_EXIT_CTLS: |
| 1389 | case MSR_IA32_VMX_TRUE_ENTRY_CTLS: |
| 1390 | case MSR_IA32_VMX_PROCBASED_CTLS2: |
| 1391 | return vmx_restore_control_msr(vmx, msr_index, data); |
| 1392 | case MSR_IA32_VMX_MISC: |
| 1393 | return vmx_restore_vmx_misc(vmx, data); |
| 1394 | case MSR_IA32_VMX_CR0_FIXED0: |
| 1395 | case MSR_IA32_VMX_CR4_FIXED0: |
| 1396 | return vmx_restore_fixed0_msr(vmx, msr_index, data); |
| 1397 | case MSR_IA32_VMX_CR0_FIXED1: |
| 1398 | case MSR_IA32_VMX_CR4_FIXED1: |
| 1399 | /* |
| 1400 | * These MSRs are generated based on the vCPU's CPUID, so we |
| 1401 | * do not support restoring them directly. |
| 1402 | */ |
| 1403 | return -EINVAL; |
| 1404 | case MSR_IA32_VMX_EPT_VPID_CAP: |
| 1405 | return vmx_restore_vmx_ept_vpid_cap(vmx, data); |
| 1406 | case MSR_IA32_VMX_VMCS_ENUM: |
| 1407 | vmx->nested.msrs.vmcs_enum = data; |
| 1408 | return 0; |
Paolo Bonzini | e8a70bd | 2019-07-02 14:40:40 +0200 | [diff] [blame] | 1409 | case MSR_IA32_VMX_VMFUNC: |
| 1410 | if (data & ~vmx->nested.msrs.vmfunc_controls) |
| 1411 | return -EINVAL; |
| 1412 | vmx->nested.msrs.vmfunc_controls = data; |
| 1413 | return 0; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1414 | default: |
| 1415 | /* |
| 1416 | * The rest of the VMX capability MSRs do not support restore. |
| 1417 | */ |
| 1418 | return -EINVAL; |
| 1419 | } |
| 1420 | } |
| 1421 | |
| 1422 | /* Returns 0 on success, non-0 otherwise. */ |
| 1423 | int vmx_get_vmx_msr(struct nested_vmx_msrs *msrs, u32 msr_index, u64 *pdata) |
| 1424 | { |
| 1425 | switch (msr_index) { |
| 1426 | case MSR_IA32_VMX_BASIC: |
| 1427 | *pdata = msrs->basic; |
| 1428 | break; |
| 1429 | case MSR_IA32_VMX_TRUE_PINBASED_CTLS: |
| 1430 | case MSR_IA32_VMX_PINBASED_CTLS: |
| 1431 | *pdata = vmx_control_msr( |
| 1432 | msrs->pinbased_ctls_low, |
| 1433 | msrs->pinbased_ctls_high); |
| 1434 | if (msr_index == MSR_IA32_VMX_PINBASED_CTLS) |
| 1435 | *pdata |= PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR; |
| 1436 | break; |
| 1437 | case MSR_IA32_VMX_TRUE_PROCBASED_CTLS: |
| 1438 | case MSR_IA32_VMX_PROCBASED_CTLS: |
| 1439 | *pdata = vmx_control_msr( |
| 1440 | msrs->procbased_ctls_low, |
| 1441 | msrs->procbased_ctls_high); |
| 1442 | if (msr_index == MSR_IA32_VMX_PROCBASED_CTLS) |
| 1443 | *pdata |= CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR; |
| 1444 | break; |
| 1445 | case MSR_IA32_VMX_TRUE_EXIT_CTLS: |
| 1446 | case MSR_IA32_VMX_EXIT_CTLS: |
| 1447 | *pdata = vmx_control_msr( |
| 1448 | msrs->exit_ctls_low, |
| 1449 | msrs->exit_ctls_high); |
| 1450 | if (msr_index == MSR_IA32_VMX_EXIT_CTLS) |
| 1451 | *pdata |= VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR; |
| 1452 | break; |
| 1453 | case MSR_IA32_VMX_TRUE_ENTRY_CTLS: |
| 1454 | case MSR_IA32_VMX_ENTRY_CTLS: |
| 1455 | *pdata = vmx_control_msr( |
| 1456 | msrs->entry_ctls_low, |
| 1457 | msrs->entry_ctls_high); |
| 1458 | if (msr_index == MSR_IA32_VMX_ENTRY_CTLS) |
| 1459 | *pdata |= VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR; |
| 1460 | break; |
| 1461 | case MSR_IA32_VMX_MISC: |
| 1462 | *pdata = vmx_control_msr( |
| 1463 | msrs->misc_low, |
| 1464 | msrs->misc_high); |
| 1465 | break; |
| 1466 | case MSR_IA32_VMX_CR0_FIXED0: |
| 1467 | *pdata = msrs->cr0_fixed0; |
| 1468 | break; |
| 1469 | case MSR_IA32_VMX_CR0_FIXED1: |
| 1470 | *pdata = msrs->cr0_fixed1; |
| 1471 | break; |
| 1472 | case MSR_IA32_VMX_CR4_FIXED0: |
| 1473 | *pdata = msrs->cr4_fixed0; |
| 1474 | break; |
| 1475 | case MSR_IA32_VMX_CR4_FIXED1: |
| 1476 | *pdata = msrs->cr4_fixed1; |
| 1477 | break; |
| 1478 | case MSR_IA32_VMX_VMCS_ENUM: |
| 1479 | *pdata = msrs->vmcs_enum; |
| 1480 | break; |
| 1481 | case MSR_IA32_VMX_PROCBASED_CTLS2: |
| 1482 | *pdata = vmx_control_msr( |
| 1483 | msrs->secondary_ctls_low, |
| 1484 | msrs->secondary_ctls_high); |
| 1485 | break; |
| 1486 | case MSR_IA32_VMX_EPT_VPID_CAP: |
| 1487 | *pdata = msrs->ept_caps | |
| 1488 | ((u64)msrs->vpid_caps << 32); |
| 1489 | break; |
| 1490 | case MSR_IA32_VMX_VMFUNC: |
| 1491 | *pdata = msrs->vmfunc_controls; |
| 1492 | break; |
| 1493 | default: |
| 1494 | return 1; |
| 1495 | } |
| 1496 | |
| 1497 | return 0; |
| 1498 | } |
| 1499 | |
| 1500 | /* |
Sean Christopherson | fadcead | 2019-05-07 08:36:23 -0700 | [diff] [blame] | 1501 | * Copy the writable VMCS shadow fields back to the VMCS12, in case they have |
| 1502 | * been modified by the L1 guest. Note, "writable" in this context means |
| 1503 | * "writable by the guest", i.e. tagged SHADOW_FIELD_RW; the set of |
| 1504 | * fields tagged SHADOW_FIELD_RO may or may not align with the "read-only" |
| 1505 | * VM-exit information fields (which are actually writable if the vCPU is |
| 1506 | * configured to support "VMWRITE to any supported field in the VMCS"). |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1507 | */ |
| 1508 | static void copy_shadow_to_vmcs12(struct vcpu_vmx *vmx) |
| 1509 | { |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1510 | struct vmcs *shadow_vmcs = vmx->vmcs01.shadow_vmcs; |
Sean Christopherson | fadcead | 2019-05-07 08:36:23 -0700 | [diff] [blame] | 1511 | struct vmcs12 *vmcs12 = get_vmcs12(&vmx->vcpu); |
Sean Christopherson | 1c6f0b4 | 2019-05-07 08:36:25 -0700 | [diff] [blame] | 1512 | struct shadow_vmcs_field field; |
| 1513 | unsigned long val; |
Sean Christopherson | fadcead | 2019-05-07 08:36:23 -0700 | [diff] [blame] | 1514 | int i; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1515 | |
Paolo Bonzini | 88dddc1 | 2019-07-19 18:41:10 +0200 | [diff] [blame] | 1516 | if (WARN_ON(!shadow_vmcs)) |
| 1517 | return; |
| 1518 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1519 | preempt_disable(); |
| 1520 | |
| 1521 | vmcs_load(shadow_vmcs); |
| 1522 | |
Sean Christopherson | fadcead | 2019-05-07 08:36:23 -0700 | [diff] [blame] | 1523 | for (i = 0; i < max_shadow_read_write_fields; i++) { |
| 1524 | field = shadow_read_write_fields[i]; |
Sean Christopherson | 1c6f0b4 | 2019-05-07 08:36:25 -0700 | [diff] [blame] | 1525 | val = __vmcs_readl(field.encoding); |
| 1526 | vmcs12_write_any(vmcs12, field.encoding, field.offset, val); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1527 | } |
| 1528 | |
| 1529 | vmcs_clear(shadow_vmcs); |
| 1530 | vmcs_load(vmx->loaded_vmcs->vmcs); |
| 1531 | |
| 1532 | preempt_enable(); |
| 1533 | } |
| 1534 | |
| 1535 | static void copy_vmcs12_to_shadow(struct vcpu_vmx *vmx) |
| 1536 | { |
Sean Christopherson | 1c6f0b4 | 2019-05-07 08:36:25 -0700 | [diff] [blame] | 1537 | const struct shadow_vmcs_field *fields[] = { |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1538 | shadow_read_write_fields, |
| 1539 | shadow_read_only_fields |
| 1540 | }; |
| 1541 | const int max_fields[] = { |
| 1542 | max_shadow_read_write_fields, |
| 1543 | max_shadow_read_only_fields |
| 1544 | }; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1545 | struct vmcs *shadow_vmcs = vmx->vmcs01.shadow_vmcs; |
Sean Christopherson | 1c6f0b4 | 2019-05-07 08:36:25 -0700 | [diff] [blame] | 1546 | struct vmcs12 *vmcs12 = get_vmcs12(&vmx->vcpu); |
| 1547 | struct shadow_vmcs_field field; |
| 1548 | unsigned long val; |
| 1549 | int i, q; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1550 | |
Paolo Bonzini | 88dddc1 | 2019-07-19 18:41:10 +0200 | [diff] [blame] | 1551 | if (WARN_ON(!shadow_vmcs)) |
| 1552 | return; |
| 1553 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1554 | vmcs_load(shadow_vmcs); |
| 1555 | |
| 1556 | for (q = 0; q < ARRAY_SIZE(fields); q++) { |
| 1557 | for (i = 0; i < max_fields[q]; i++) { |
| 1558 | field = fields[q][i]; |
Sean Christopherson | 1c6f0b4 | 2019-05-07 08:36:25 -0700 | [diff] [blame] | 1559 | val = vmcs12_read_any(vmcs12, field.encoding, |
| 1560 | field.offset); |
| 1561 | __vmcs_writel(field.encoding, val); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1562 | } |
| 1563 | } |
| 1564 | |
| 1565 | vmcs_clear(shadow_vmcs); |
| 1566 | vmcs_load(vmx->loaded_vmcs->vmcs); |
| 1567 | } |
| 1568 | |
Vitaly Kuznetsov | d6bf71a | 2021-05-26 15:20:22 +0200 | [diff] [blame] | 1569 | 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] | 1570 | { |
| 1571 | struct vmcs12 *vmcs12 = vmx->nested.cached_vmcs12; |
| 1572 | struct hv_enlightened_vmcs *evmcs = vmx->nested.hv_evmcs; |
| 1573 | |
| 1574 | /* HV_VMX_ENLIGHTENED_CLEAN_FIELD_NONE */ |
| 1575 | vmcs12->tpr_threshold = evmcs->tpr_threshold; |
| 1576 | vmcs12->guest_rip = evmcs->guest_rip; |
| 1577 | |
Vitaly Kuznetsov | d6bf71a | 2021-05-26 15:20:22 +0200 | [diff] [blame] | 1578 | if (unlikely(!(hv_clean_fields & |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1579 | HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_BASIC))) { |
| 1580 | vmcs12->guest_rsp = evmcs->guest_rsp; |
| 1581 | vmcs12->guest_rflags = evmcs->guest_rflags; |
| 1582 | vmcs12->guest_interruptibility_info = |
| 1583 | evmcs->guest_interruptibility_info; |
| 1584 | } |
| 1585 | |
Vitaly Kuznetsov | d6bf71a | 2021-05-26 15:20:22 +0200 | [diff] [blame] | 1586 | if (unlikely(!(hv_clean_fields & |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1587 | HV_VMX_ENLIGHTENED_CLEAN_FIELD_CONTROL_PROC))) { |
| 1588 | vmcs12->cpu_based_vm_exec_control = |
| 1589 | evmcs->cpu_based_vm_exec_control; |
| 1590 | } |
| 1591 | |
Vitaly Kuznetsov | d6bf71a | 2021-05-26 15:20:22 +0200 | [diff] [blame] | 1592 | if (unlikely(!(hv_clean_fields & |
Vitaly Kuznetsov | f9bc522 | 2019-06-13 13:35:02 +0200 | [diff] [blame] | 1593 | HV_VMX_ENLIGHTENED_CLEAN_FIELD_CONTROL_EXCPN))) { |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1594 | vmcs12->exception_bitmap = evmcs->exception_bitmap; |
| 1595 | } |
| 1596 | |
Vitaly Kuznetsov | d6bf71a | 2021-05-26 15:20:22 +0200 | [diff] [blame] | 1597 | if (unlikely(!(hv_clean_fields & |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1598 | HV_VMX_ENLIGHTENED_CLEAN_FIELD_CONTROL_ENTRY))) { |
| 1599 | vmcs12->vm_entry_controls = evmcs->vm_entry_controls; |
| 1600 | } |
| 1601 | |
Vitaly Kuznetsov | d6bf71a | 2021-05-26 15:20:22 +0200 | [diff] [blame] | 1602 | if (unlikely(!(hv_clean_fields & |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1603 | HV_VMX_ENLIGHTENED_CLEAN_FIELD_CONTROL_EVENT))) { |
| 1604 | vmcs12->vm_entry_intr_info_field = |
| 1605 | evmcs->vm_entry_intr_info_field; |
| 1606 | vmcs12->vm_entry_exception_error_code = |
| 1607 | evmcs->vm_entry_exception_error_code; |
| 1608 | vmcs12->vm_entry_instruction_len = |
| 1609 | evmcs->vm_entry_instruction_len; |
| 1610 | } |
| 1611 | |
Vitaly Kuznetsov | d6bf71a | 2021-05-26 15:20:22 +0200 | [diff] [blame] | 1612 | if (unlikely(!(hv_clean_fields & |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1613 | HV_VMX_ENLIGHTENED_CLEAN_FIELD_HOST_GRP1))) { |
| 1614 | vmcs12->host_ia32_pat = evmcs->host_ia32_pat; |
| 1615 | vmcs12->host_ia32_efer = evmcs->host_ia32_efer; |
| 1616 | vmcs12->host_cr0 = evmcs->host_cr0; |
| 1617 | vmcs12->host_cr3 = evmcs->host_cr3; |
| 1618 | vmcs12->host_cr4 = evmcs->host_cr4; |
| 1619 | vmcs12->host_ia32_sysenter_esp = evmcs->host_ia32_sysenter_esp; |
| 1620 | vmcs12->host_ia32_sysenter_eip = evmcs->host_ia32_sysenter_eip; |
| 1621 | vmcs12->host_rip = evmcs->host_rip; |
| 1622 | vmcs12->host_ia32_sysenter_cs = evmcs->host_ia32_sysenter_cs; |
| 1623 | vmcs12->host_es_selector = evmcs->host_es_selector; |
| 1624 | vmcs12->host_cs_selector = evmcs->host_cs_selector; |
| 1625 | vmcs12->host_ss_selector = evmcs->host_ss_selector; |
| 1626 | vmcs12->host_ds_selector = evmcs->host_ds_selector; |
| 1627 | vmcs12->host_fs_selector = evmcs->host_fs_selector; |
| 1628 | vmcs12->host_gs_selector = evmcs->host_gs_selector; |
| 1629 | vmcs12->host_tr_selector = evmcs->host_tr_selector; |
| 1630 | } |
| 1631 | |
Vitaly Kuznetsov | d6bf71a | 2021-05-26 15:20:22 +0200 | [diff] [blame] | 1632 | if (unlikely(!(hv_clean_fields & |
Vitaly Kuznetsov | f9bc522 | 2019-06-13 13:35:02 +0200 | [diff] [blame] | 1633 | HV_VMX_ENLIGHTENED_CLEAN_FIELD_CONTROL_GRP1))) { |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1634 | vmcs12->pin_based_vm_exec_control = |
| 1635 | evmcs->pin_based_vm_exec_control; |
| 1636 | vmcs12->vm_exit_controls = evmcs->vm_exit_controls; |
| 1637 | vmcs12->secondary_vm_exec_control = |
| 1638 | evmcs->secondary_vm_exec_control; |
| 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_IO_BITMAP))) { |
| 1643 | vmcs12->io_bitmap_a = evmcs->io_bitmap_a; |
| 1644 | vmcs12->io_bitmap_b = evmcs->io_bitmap_b; |
| 1645 | } |
| 1646 | |
Vitaly Kuznetsov | d6bf71a | 2021-05-26 15:20:22 +0200 | [diff] [blame] | 1647 | if (unlikely(!(hv_clean_fields & |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1648 | HV_VMX_ENLIGHTENED_CLEAN_FIELD_MSR_BITMAP))) { |
| 1649 | vmcs12->msr_bitmap = evmcs->msr_bitmap; |
| 1650 | } |
| 1651 | |
Vitaly Kuznetsov | d6bf71a | 2021-05-26 15:20:22 +0200 | [diff] [blame] | 1652 | if (unlikely(!(hv_clean_fields & |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1653 | HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP2))) { |
| 1654 | vmcs12->guest_es_base = evmcs->guest_es_base; |
| 1655 | vmcs12->guest_cs_base = evmcs->guest_cs_base; |
| 1656 | vmcs12->guest_ss_base = evmcs->guest_ss_base; |
| 1657 | vmcs12->guest_ds_base = evmcs->guest_ds_base; |
| 1658 | vmcs12->guest_fs_base = evmcs->guest_fs_base; |
| 1659 | vmcs12->guest_gs_base = evmcs->guest_gs_base; |
| 1660 | vmcs12->guest_ldtr_base = evmcs->guest_ldtr_base; |
| 1661 | vmcs12->guest_tr_base = evmcs->guest_tr_base; |
| 1662 | vmcs12->guest_gdtr_base = evmcs->guest_gdtr_base; |
| 1663 | vmcs12->guest_idtr_base = evmcs->guest_idtr_base; |
| 1664 | vmcs12->guest_es_limit = evmcs->guest_es_limit; |
| 1665 | vmcs12->guest_cs_limit = evmcs->guest_cs_limit; |
| 1666 | vmcs12->guest_ss_limit = evmcs->guest_ss_limit; |
| 1667 | vmcs12->guest_ds_limit = evmcs->guest_ds_limit; |
| 1668 | vmcs12->guest_fs_limit = evmcs->guest_fs_limit; |
| 1669 | vmcs12->guest_gs_limit = evmcs->guest_gs_limit; |
| 1670 | vmcs12->guest_ldtr_limit = evmcs->guest_ldtr_limit; |
| 1671 | vmcs12->guest_tr_limit = evmcs->guest_tr_limit; |
| 1672 | vmcs12->guest_gdtr_limit = evmcs->guest_gdtr_limit; |
| 1673 | vmcs12->guest_idtr_limit = evmcs->guest_idtr_limit; |
| 1674 | vmcs12->guest_es_ar_bytes = evmcs->guest_es_ar_bytes; |
| 1675 | vmcs12->guest_cs_ar_bytes = evmcs->guest_cs_ar_bytes; |
| 1676 | vmcs12->guest_ss_ar_bytes = evmcs->guest_ss_ar_bytes; |
| 1677 | vmcs12->guest_ds_ar_bytes = evmcs->guest_ds_ar_bytes; |
| 1678 | vmcs12->guest_fs_ar_bytes = evmcs->guest_fs_ar_bytes; |
| 1679 | vmcs12->guest_gs_ar_bytes = evmcs->guest_gs_ar_bytes; |
| 1680 | vmcs12->guest_ldtr_ar_bytes = evmcs->guest_ldtr_ar_bytes; |
| 1681 | vmcs12->guest_tr_ar_bytes = evmcs->guest_tr_ar_bytes; |
| 1682 | vmcs12->guest_es_selector = evmcs->guest_es_selector; |
| 1683 | vmcs12->guest_cs_selector = evmcs->guest_cs_selector; |
| 1684 | vmcs12->guest_ss_selector = evmcs->guest_ss_selector; |
| 1685 | vmcs12->guest_ds_selector = evmcs->guest_ds_selector; |
| 1686 | vmcs12->guest_fs_selector = evmcs->guest_fs_selector; |
| 1687 | vmcs12->guest_gs_selector = evmcs->guest_gs_selector; |
| 1688 | vmcs12->guest_ldtr_selector = evmcs->guest_ldtr_selector; |
| 1689 | vmcs12->guest_tr_selector = evmcs->guest_tr_selector; |
| 1690 | } |
| 1691 | |
Vitaly Kuznetsov | d6bf71a | 2021-05-26 15:20:22 +0200 | [diff] [blame] | 1692 | if (unlikely(!(hv_clean_fields & |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1693 | HV_VMX_ENLIGHTENED_CLEAN_FIELD_CONTROL_GRP2))) { |
| 1694 | vmcs12->tsc_offset = evmcs->tsc_offset; |
| 1695 | vmcs12->virtual_apic_page_addr = evmcs->virtual_apic_page_addr; |
| 1696 | vmcs12->xss_exit_bitmap = evmcs->xss_exit_bitmap; |
| 1697 | } |
| 1698 | |
Vitaly Kuznetsov | d6bf71a | 2021-05-26 15:20:22 +0200 | [diff] [blame] | 1699 | if (unlikely(!(hv_clean_fields & |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1700 | HV_VMX_ENLIGHTENED_CLEAN_FIELD_CRDR))) { |
| 1701 | vmcs12->cr0_guest_host_mask = evmcs->cr0_guest_host_mask; |
| 1702 | vmcs12->cr4_guest_host_mask = evmcs->cr4_guest_host_mask; |
| 1703 | vmcs12->cr0_read_shadow = evmcs->cr0_read_shadow; |
| 1704 | vmcs12->cr4_read_shadow = evmcs->cr4_read_shadow; |
| 1705 | vmcs12->guest_cr0 = evmcs->guest_cr0; |
| 1706 | vmcs12->guest_cr3 = evmcs->guest_cr3; |
| 1707 | vmcs12->guest_cr4 = evmcs->guest_cr4; |
| 1708 | vmcs12->guest_dr7 = evmcs->guest_dr7; |
| 1709 | } |
| 1710 | |
Vitaly Kuznetsov | d6bf71a | 2021-05-26 15:20:22 +0200 | [diff] [blame] | 1711 | if (unlikely(!(hv_clean_fields & |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1712 | HV_VMX_ENLIGHTENED_CLEAN_FIELD_HOST_POINTER))) { |
| 1713 | vmcs12->host_fs_base = evmcs->host_fs_base; |
| 1714 | vmcs12->host_gs_base = evmcs->host_gs_base; |
| 1715 | vmcs12->host_tr_base = evmcs->host_tr_base; |
| 1716 | vmcs12->host_gdtr_base = evmcs->host_gdtr_base; |
| 1717 | vmcs12->host_idtr_base = evmcs->host_idtr_base; |
| 1718 | vmcs12->host_rsp = evmcs->host_rsp; |
| 1719 | } |
| 1720 | |
Vitaly Kuznetsov | d6bf71a | 2021-05-26 15:20:22 +0200 | [diff] [blame] | 1721 | if (unlikely(!(hv_clean_fields & |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1722 | HV_VMX_ENLIGHTENED_CLEAN_FIELD_CONTROL_XLAT))) { |
| 1723 | vmcs12->ept_pointer = evmcs->ept_pointer; |
| 1724 | vmcs12->virtual_processor_id = evmcs->virtual_processor_id; |
| 1725 | } |
| 1726 | |
Vitaly Kuznetsov | d6bf71a | 2021-05-26 15:20:22 +0200 | [diff] [blame] | 1727 | if (unlikely(!(hv_clean_fields & |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1728 | HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP1))) { |
| 1729 | vmcs12->vmcs_link_pointer = evmcs->vmcs_link_pointer; |
| 1730 | vmcs12->guest_ia32_debugctl = evmcs->guest_ia32_debugctl; |
| 1731 | vmcs12->guest_ia32_pat = evmcs->guest_ia32_pat; |
| 1732 | vmcs12->guest_ia32_efer = evmcs->guest_ia32_efer; |
| 1733 | vmcs12->guest_pdptr0 = evmcs->guest_pdptr0; |
| 1734 | vmcs12->guest_pdptr1 = evmcs->guest_pdptr1; |
| 1735 | vmcs12->guest_pdptr2 = evmcs->guest_pdptr2; |
| 1736 | vmcs12->guest_pdptr3 = evmcs->guest_pdptr3; |
| 1737 | vmcs12->guest_pending_dbg_exceptions = |
| 1738 | evmcs->guest_pending_dbg_exceptions; |
| 1739 | vmcs12->guest_sysenter_esp = evmcs->guest_sysenter_esp; |
| 1740 | vmcs12->guest_sysenter_eip = evmcs->guest_sysenter_eip; |
| 1741 | vmcs12->guest_bndcfgs = evmcs->guest_bndcfgs; |
| 1742 | vmcs12->guest_activity_state = evmcs->guest_activity_state; |
| 1743 | vmcs12->guest_sysenter_cs = evmcs->guest_sysenter_cs; |
| 1744 | } |
| 1745 | |
| 1746 | /* |
| 1747 | * Not used? |
| 1748 | * vmcs12->vm_exit_msr_store_addr = evmcs->vm_exit_msr_store_addr; |
| 1749 | * vmcs12->vm_exit_msr_load_addr = evmcs->vm_exit_msr_load_addr; |
| 1750 | * vmcs12->vm_entry_msr_load_addr = evmcs->vm_entry_msr_load_addr; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1751 | * vmcs12->page_fault_error_code_mask = |
| 1752 | * evmcs->page_fault_error_code_mask; |
| 1753 | * vmcs12->page_fault_error_code_match = |
| 1754 | * evmcs->page_fault_error_code_match; |
| 1755 | * vmcs12->cr3_target_count = evmcs->cr3_target_count; |
| 1756 | * vmcs12->vm_exit_msr_store_count = evmcs->vm_exit_msr_store_count; |
| 1757 | * vmcs12->vm_exit_msr_load_count = evmcs->vm_exit_msr_load_count; |
| 1758 | * vmcs12->vm_entry_msr_load_count = evmcs->vm_entry_msr_load_count; |
| 1759 | */ |
| 1760 | |
| 1761 | /* |
| 1762 | * Read only fields: |
| 1763 | * vmcs12->guest_physical_address = evmcs->guest_physical_address; |
| 1764 | * vmcs12->vm_instruction_error = evmcs->vm_instruction_error; |
| 1765 | * vmcs12->vm_exit_reason = evmcs->vm_exit_reason; |
| 1766 | * vmcs12->vm_exit_intr_info = evmcs->vm_exit_intr_info; |
| 1767 | * vmcs12->vm_exit_intr_error_code = evmcs->vm_exit_intr_error_code; |
| 1768 | * vmcs12->idt_vectoring_info_field = evmcs->idt_vectoring_info_field; |
| 1769 | * vmcs12->idt_vectoring_error_code = evmcs->idt_vectoring_error_code; |
| 1770 | * vmcs12->vm_exit_instruction_len = evmcs->vm_exit_instruction_len; |
| 1771 | * vmcs12->vmx_instruction_info = evmcs->vmx_instruction_info; |
| 1772 | * vmcs12->exit_qualification = evmcs->exit_qualification; |
| 1773 | * vmcs12->guest_linear_address = evmcs->guest_linear_address; |
| 1774 | * |
| 1775 | * Not present in struct vmcs12: |
| 1776 | * vmcs12->exit_io_instruction_ecx = evmcs->exit_io_instruction_ecx; |
| 1777 | * vmcs12->exit_io_instruction_esi = evmcs->exit_io_instruction_esi; |
| 1778 | * vmcs12->exit_io_instruction_edi = evmcs->exit_io_instruction_edi; |
| 1779 | * vmcs12->exit_io_instruction_eip = evmcs->exit_io_instruction_eip; |
| 1780 | */ |
| 1781 | |
Vitaly Kuznetsov | 25641ca | 2021-05-26 15:20:19 +0200 | [diff] [blame] | 1782 | return; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1783 | } |
| 1784 | |
Vitaly Kuznetsov | 25641ca | 2021-05-26 15:20:19 +0200 | [diff] [blame] | 1785 | static void copy_vmcs12_to_enlightened(struct vcpu_vmx *vmx) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1786 | { |
| 1787 | struct vmcs12 *vmcs12 = vmx->nested.cached_vmcs12; |
| 1788 | struct hv_enlightened_vmcs *evmcs = vmx->nested.hv_evmcs; |
| 1789 | |
| 1790 | /* |
| 1791 | * Should not be changed by KVM: |
| 1792 | * |
| 1793 | * evmcs->host_es_selector = vmcs12->host_es_selector; |
| 1794 | * evmcs->host_cs_selector = vmcs12->host_cs_selector; |
| 1795 | * evmcs->host_ss_selector = vmcs12->host_ss_selector; |
| 1796 | * evmcs->host_ds_selector = vmcs12->host_ds_selector; |
| 1797 | * evmcs->host_fs_selector = vmcs12->host_fs_selector; |
| 1798 | * evmcs->host_gs_selector = vmcs12->host_gs_selector; |
| 1799 | * evmcs->host_tr_selector = vmcs12->host_tr_selector; |
| 1800 | * evmcs->host_ia32_pat = vmcs12->host_ia32_pat; |
| 1801 | * evmcs->host_ia32_efer = vmcs12->host_ia32_efer; |
| 1802 | * evmcs->host_cr0 = vmcs12->host_cr0; |
| 1803 | * evmcs->host_cr3 = vmcs12->host_cr3; |
| 1804 | * evmcs->host_cr4 = vmcs12->host_cr4; |
| 1805 | * evmcs->host_ia32_sysenter_esp = vmcs12->host_ia32_sysenter_esp; |
| 1806 | * evmcs->host_ia32_sysenter_eip = vmcs12->host_ia32_sysenter_eip; |
| 1807 | * evmcs->host_rip = vmcs12->host_rip; |
| 1808 | * evmcs->host_ia32_sysenter_cs = vmcs12->host_ia32_sysenter_cs; |
| 1809 | * evmcs->host_fs_base = vmcs12->host_fs_base; |
| 1810 | * evmcs->host_gs_base = vmcs12->host_gs_base; |
| 1811 | * evmcs->host_tr_base = vmcs12->host_tr_base; |
| 1812 | * evmcs->host_gdtr_base = vmcs12->host_gdtr_base; |
| 1813 | * evmcs->host_idtr_base = vmcs12->host_idtr_base; |
| 1814 | * evmcs->host_rsp = vmcs12->host_rsp; |
Sean Christopherson | 3731905ef | 2019-05-07 08:36:27 -0700 | [diff] [blame] | 1815 | * sync_vmcs02_to_vmcs12() doesn't read these: |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1816 | * evmcs->io_bitmap_a = vmcs12->io_bitmap_a; |
| 1817 | * evmcs->io_bitmap_b = vmcs12->io_bitmap_b; |
| 1818 | * evmcs->msr_bitmap = vmcs12->msr_bitmap; |
| 1819 | * evmcs->ept_pointer = vmcs12->ept_pointer; |
| 1820 | * evmcs->xss_exit_bitmap = vmcs12->xss_exit_bitmap; |
| 1821 | * evmcs->vm_exit_msr_store_addr = vmcs12->vm_exit_msr_store_addr; |
| 1822 | * evmcs->vm_exit_msr_load_addr = vmcs12->vm_exit_msr_load_addr; |
| 1823 | * evmcs->vm_entry_msr_load_addr = vmcs12->vm_entry_msr_load_addr; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1824 | * evmcs->tpr_threshold = vmcs12->tpr_threshold; |
| 1825 | * evmcs->virtual_processor_id = vmcs12->virtual_processor_id; |
| 1826 | * evmcs->exception_bitmap = vmcs12->exception_bitmap; |
| 1827 | * evmcs->vmcs_link_pointer = vmcs12->vmcs_link_pointer; |
| 1828 | * evmcs->pin_based_vm_exec_control = vmcs12->pin_based_vm_exec_control; |
| 1829 | * evmcs->vm_exit_controls = vmcs12->vm_exit_controls; |
| 1830 | * evmcs->secondary_vm_exec_control = vmcs12->secondary_vm_exec_control; |
| 1831 | * evmcs->page_fault_error_code_mask = |
| 1832 | * vmcs12->page_fault_error_code_mask; |
| 1833 | * evmcs->page_fault_error_code_match = |
| 1834 | * vmcs12->page_fault_error_code_match; |
| 1835 | * evmcs->cr3_target_count = vmcs12->cr3_target_count; |
| 1836 | * evmcs->virtual_apic_page_addr = vmcs12->virtual_apic_page_addr; |
| 1837 | * evmcs->tsc_offset = vmcs12->tsc_offset; |
| 1838 | * evmcs->guest_ia32_debugctl = vmcs12->guest_ia32_debugctl; |
| 1839 | * evmcs->cr0_guest_host_mask = vmcs12->cr0_guest_host_mask; |
| 1840 | * evmcs->cr4_guest_host_mask = vmcs12->cr4_guest_host_mask; |
| 1841 | * evmcs->cr0_read_shadow = vmcs12->cr0_read_shadow; |
| 1842 | * evmcs->cr4_read_shadow = vmcs12->cr4_read_shadow; |
| 1843 | * evmcs->vm_exit_msr_store_count = vmcs12->vm_exit_msr_store_count; |
| 1844 | * evmcs->vm_exit_msr_load_count = vmcs12->vm_exit_msr_load_count; |
| 1845 | * evmcs->vm_entry_msr_load_count = vmcs12->vm_entry_msr_load_count; |
| 1846 | * |
| 1847 | * Not present in struct vmcs12: |
| 1848 | * evmcs->exit_io_instruction_ecx = vmcs12->exit_io_instruction_ecx; |
| 1849 | * evmcs->exit_io_instruction_esi = vmcs12->exit_io_instruction_esi; |
| 1850 | * evmcs->exit_io_instruction_edi = vmcs12->exit_io_instruction_edi; |
| 1851 | * evmcs->exit_io_instruction_eip = vmcs12->exit_io_instruction_eip; |
| 1852 | */ |
| 1853 | |
| 1854 | evmcs->guest_es_selector = vmcs12->guest_es_selector; |
| 1855 | evmcs->guest_cs_selector = vmcs12->guest_cs_selector; |
| 1856 | evmcs->guest_ss_selector = vmcs12->guest_ss_selector; |
| 1857 | evmcs->guest_ds_selector = vmcs12->guest_ds_selector; |
| 1858 | evmcs->guest_fs_selector = vmcs12->guest_fs_selector; |
| 1859 | evmcs->guest_gs_selector = vmcs12->guest_gs_selector; |
| 1860 | evmcs->guest_ldtr_selector = vmcs12->guest_ldtr_selector; |
| 1861 | evmcs->guest_tr_selector = vmcs12->guest_tr_selector; |
| 1862 | |
| 1863 | evmcs->guest_es_limit = vmcs12->guest_es_limit; |
| 1864 | evmcs->guest_cs_limit = vmcs12->guest_cs_limit; |
| 1865 | evmcs->guest_ss_limit = vmcs12->guest_ss_limit; |
| 1866 | evmcs->guest_ds_limit = vmcs12->guest_ds_limit; |
| 1867 | evmcs->guest_fs_limit = vmcs12->guest_fs_limit; |
| 1868 | evmcs->guest_gs_limit = vmcs12->guest_gs_limit; |
| 1869 | evmcs->guest_ldtr_limit = vmcs12->guest_ldtr_limit; |
| 1870 | evmcs->guest_tr_limit = vmcs12->guest_tr_limit; |
| 1871 | evmcs->guest_gdtr_limit = vmcs12->guest_gdtr_limit; |
| 1872 | evmcs->guest_idtr_limit = vmcs12->guest_idtr_limit; |
| 1873 | |
| 1874 | evmcs->guest_es_ar_bytes = vmcs12->guest_es_ar_bytes; |
| 1875 | evmcs->guest_cs_ar_bytes = vmcs12->guest_cs_ar_bytes; |
| 1876 | evmcs->guest_ss_ar_bytes = vmcs12->guest_ss_ar_bytes; |
| 1877 | evmcs->guest_ds_ar_bytes = vmcs12->guest_ds_ar_bytes; |
| 1878 | evmcs->guest_fs_ar_bytes = vmcs12->guest_fs_ar_bytes; |
| 1879 | evmcs->guest_gs_ar_bytes = vmcs12->guest_gs_ar_bytes; |
| 1880 | evmcs->guest_ldtr_ar_bytes = vmcs12->guest_ldtr_ar_bytes; |
| 1881 | evmcs->guest_tr_ar_bytes = vmcs12->guest_tr_ar_bytes; |
| 1882 | |
| 1883 | evmcs->guest_es_base = vmcs12->guest_es_base; |
| 1884 | evmcs->guest_cs_base = vmcs12->guest_cs_base; |
| 1885 | evmcs->guest_ss_base = vmcs12->guest_ss_base; |
| 1886 | evmcs->guest_ds_base = vmcs12->guest_ds_base; |
| 1887 | evmcs->guest_fs_base = vmcs12->guest_fs_base; |
| 1888 | evmcs->guest_gs_base = vmcs12->guest_gs_base; |
| 1889 | evmcs->guest_ldtr_base = vmcs12->guest_ldtr_base; |
| 1890 | evmcs->guest_tr_base = vmcs12->guest_tr_base; |
| 1891 | evmcs->guest_gdtr_base = vmcs12->guest_gdtr_base; |
| 1892 | evmcs->guest_idtr_base = vmcs12->guest_idtr_base; |
| 1893 | |
| 1894 | evmcs->guest_ia32_pat = vmcs12->guest_ia32_pat; |
| 1895 | evmcs->guest_ia32_efer = vmcs12->guest_ia32_efer; |
| 1896 | |
| 1897 | evmcs->guest_pdptr0 = vmcs12->guest_pdptr0; |
| 1898 | evmcs->guest_pdptr1 = vmcs12->guest_pdptr1; |
| 1899 | evmcs->guest_pdptr2 = vmcs12->guest_pdptr2; |
| 1900 | evmcs->guest_pdptr3 = vmcs12->guest_pdptr3; |
| 1901 | |
| 1902 | evmcs->guest_pending_dbg_exceptions = |
| 1903 | vmcs12->guest_pending_dbg_exceptions; |
| 1904 | evmcs->guest_sysenter_esp = vmcs12->guest_sysenter_esp; |
| 1905 | evmcs->guest_sysenter_eip = vmcs12->guest_sysenter_eip; |
| 1906 | |
| 1907 | evmcs->guest_activity_state = vmcs12->guest_activity_state; |
| 1908 | evmcs->guest_sysenter_cs = vmcs12->guest_sysenter_cs; |
| 1909 | |
| 1910 | evmcs->guest_cr0 = vmcs12->guest_cr0; |
| 1911 | evmcs->guest_cr3 = vmcs12->guest_cr3; |
| 1912 | evmcs->guest_cr4 = vmcs12->guest_cr4; |
| 1913 | evmcs->guest_dr7 = vmcs12->guest_dr7; |
| 1914 | |
| 1915 | evmcs->guest_physical_address = vmcs12->guest_physical_address; |
| 1916 | |
| 1917 | evmcs->vm_instruction_error = vmcs12->vm_instruction_error; |
| 1918 | evmcs->vm_exit_reason = vmcs12->vm_exit_reason; |
| 1919 | evmcs->vm_exit_intr_info = vmcs12->vm_exit_intr_info; |
| 1920 | evmcs->vm_exit_intr_error_code = vmcs12->vm_exit_intr_error_code; |
| 1921 | evmcs->idt_vectoring_info_field = vmcs12->idt_vectoring_info_field; |
| 1922 | evmcs->idt_vectoring_error_code = vmcs12->idt_vectoring_error_code; |
| 1923 | evmcs->vm_exit_instruction_len = vmcs12->vm_exit_instruction_len; |
| 1924 | evmcs->vmx_instruction_info = vmcs12->vmx_instruction_info; |
| 1925 | |
| 1926 | evmcs->exit_qualification = vmcs12->exit_qualification; |
| 1927 | |
| 1928 | evmcs->guest_linear_address = vmcs12->guest_linear_address; |
| 1929 | evmcs->guest_rsp = vmcs12->guest_rsp; |
| 1930 | evmcs->guest_rflags = vmcs12->guest_rflags; |
| 1931 | |
| 1932 | evmcs->guest_interruptibility_info = |
| 1933 | vmcs12->guest_interruptibility_info; |
| 1934 | evmcs->cpu_based_vm_exec_control = vmcs12->cpu_based_vm_exec_control; |
| 1935 | evmcs->vm_entry_controls = vmcs12->vm_entry_controls; |
| 1936 | evmcs->vm_entry_intr_info_field = vmcs12->vm_entry_intr_info_field; |
| 1937 | evmcs->vm_entry_exception_error_code = |
| 1938 | vmcs12->vm_entry_exception_error_code; |
| 1939 | evmcs->vm_entry_instruction_len = vmcs12->vm_entry_instruction_len; |
| 1940 | |
| 1941 | evmcs->guest_rip = vmcs12->guest_rip; |
| 1942 | |
| 1943 | evmcs->guest_bndcfgs = vmcs12->guest_bndcfgs; |
| 1944 | |
Vitaly Kuznetsov | 25641ca | 2021-05-26 15:20:19 +0200 | [diff] [blame] | 1945 | return; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1946 | } |
| 1947 | |
| 1948 | /* |
| 1949 | * This is an equivalent of the nested hypervisor executing the vmptrld |
| 1950 | * instruction. |
| 1951 | */ |
Vitaly Kuznetsov | b6a0653 | 2020-03-09 16:52:13 +0100 | [diff] [blame] | 1952 | static enum nested_evmptrld_status nested_vmx_handle_enlightened_vmptrld( |
| 1953 | struct kvm_vcpu *vcpu, bool from_launch) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1954 | { |
| 1955 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Vitaly Kuznetsov | a21a39c | 2019-06-28 13:23:32 +0200 | [diff] [blame] | 1956 | bool evmcs_gpa_changed = false; |
Vitaly Kuznetsov | 11e3491 | 2019-06-28 13:23:33 +0200 | [diff] [blame] | 1957 | u64 evmcs_gpa; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1958 | |
| 1959 | if (likely(!vmx->nested.enlightened_vmcs_enabled)) |
Vitaly Kuznetsov | b6a0653 | 2020-03-09 16:52:13 +0100 | [diff] [blame] | 1960 | return EVMPTRLD_DISABLED; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1961 | |
Vitaly Kuznetsov | 0276171 | 2021-05-26 15:20:18 +0200 | [diff] [blame] | 1962 | if (!nested_enlightened_vmentry(vcpu, &evmcs_gpa)) { |
| 1963 | nested_release_evmcs(vcpu); |
Vitaly Kuznetsov | b6a0653 | 2020-03-09 16:52:13 +0100 | [diff] [blame] | 1964 | return EVMPTRLD_DISABLED; |
Vitaly Kuznetsov | 0276171 | 2021-05-26 15:20:18 +0200 | [diff] [blame] | 1965 | } |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1966 | |
Vitaly Kuznetsov | 1e9dfbd | 2021-05-26 15:20:16 +0200 | [diff] [blame] | 1967 | if (unlikely(evmcs_gpa != vmx->nested.hv_evmcs_vmptr)) { |
Yu Zhang | 64c7850 | 2021-09-30 01:51:53 +0800 | [diff] [blame] | 1968 | vmx->nested.current_vmptr = INVALID_GPA; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1969 | |
| 1970 | nested_release_evmcs(vcpu); |
| 1971 | |
Vitaly Kuznetsov | 11e3491 | 2019-06-28 13:23:33 +0200 | [diff] [blame] | 1972 | if (kvm_vcpu_map(vcpu, gpa_to_gfn(evmcs_gpa), |
KarimAllah Ahmed | dee9c04 | 2019-01-31 21:24:42 +0100 | [diff] [blame] | 1973 | &vmx->nested.hv_evmcs_map)) |
Vitaly Kuznetsov | b6a0653 | 2020-03-09 16:52:13 +0100 | [diff] [blame] | 1974 | return EVMPTRLD_ERROR; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1975 | |
KarimAllah Ahmed | dee9c04 | 2019-01-31 21:24:42 +0100 | [diff] [blame] | 1976 | vmx->nested.hv_evmcs = vmx->nested.hv_evmcs_map.hva; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1977 | |
| 1978 | /* |
| 1979 | * Currently, KVM only supports eVMCS version 1 |
| 1980 | * (== KVM_EVMCS_VERSION) and thus we expect guest to set this |
| 1981 | * value to first u32 field of eVMCS which should specify eVMCS |
| 1982 | * VersionNumber. |
| 1983 | * |
| 1984 | * Guest should be aware of supported eVMCS versions by host by |
| 1985 | * examining CPUID.0x4000000A.EAX[0:15]. Host userspace VMM is |
| 1986 | * expected to set this CPUID leaf according to the value |
| 1987 | * returned in vmcs_version from nested_enable_evmcs(). |
| 1988 | * |
| 1989 | * However, it turns out that Microsoft Hyper-V fails to comply |
| 1990 | * to their own invented interface: When Hyper-V use eVMCS, it |
| 1991 | * just sets first u32 field of eVMCS to revision_id specified |
| 1992 | * in MSR_IA32_VMX_BASIC. Instead of used eVMCS version number |
| 1993 | * which is one of the supported versions specified in |
| 1994 | * CPUID.0x4000000A.EAX[0:15]. |
| 1995 | * |
| 1996 | * To overcome Hyper-V bug, we accept here either a supported |
| 1997 | * eVMCS version or VMCS12 revision_id as valid values for first |
| 1998 | * u32 field of eVMCS. |
| 1999 | */ |
| 2000 | if ((vmx->nested.hv_evmcs->revision_id != KVM_EVMCS_VERSION) && |
| 2001 | (vmx->nested.hv_evmcs->revision_id != VMCS12_REVISION)) { |
| 2002 | nested_release_evmcs(vcpu); |
Vitaly Kuznetsov | b6a0653 | 2020-03-09 16:52:13 +0100 | [diff] [blame] | 2003 | return EVMPTRLD_VMFAIL; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2004 | } |
| 2005 | |
Vitaly Kuznetsov | 11e3491 | 2019-06-28 13:23:33 +0200 | [diff] [blame] | 2006 | vmx->nested.hv_evmcs_vmptr = evmcs_gpa; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2007 | |
Vitaly Kuznetsov | a21a39c | 2019-06-28 13:23:32 +0200 | [diff] [blame] | 2008 | evmcs_gpa_changed = true; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2009 | /* |
| 2010 | * Unlike normal vmcs12, enlightened vmcs12 is not fully |
| 2011 | * reloaded from guest's memory (read only fields, fields not |
| 2012 | * present in struct hv_enlightened_vmcs, ...). Make sure there |
| 2013 | * are no leftovers. |
| 2014 | */ |
| 2015 | if (from_launch) { |
| 2016 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
| 2017 | memset(vmcs12, 0, sizeof(*vmcs12)); |
| 2018 | vmcs12->hdr.revision_id = VMCS12_REVISION; |
| 2019 | } |
| 2020 | |
| 2021 | } |
Vitaly Kuznetsov | a21a39c | 2019-06-28 13:23:32 +0200 | [diff] [blame] | 2022 | |
| 2023 | /* |
Miaohe Lin | ffdbd50 | 2020-02-07 23:22:45 +0800 | [diff] [blame] | 2024 | * 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] | 2025 | * between different L2 guests as KVM keeps a single VMCS12 per L1. |
| 2026 | */ |
| 2027 | if (from_launch || evmcs_gpa_changed) |
| 2028 | vmx->nested.hv_evmcs->hv_clean_fields &= |
| 2029 | ~HV_VMX_ENLIGHTENED_CLEAN_FIELD_ALL; |
| 2030 | |
Vitaly Kuznetsov | b6a0653 | 2020-03-09 16:52:13 +0100 | [diff] [blame] | 2031 | return EVMPTRLD_SUCCEEDED; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2032 | } |
| 2033 | |
Sean Christopherson | 3731905ef | 2019-05-07 08:36:27 -0700 | [diff] [blame] | 2034 | void nested_sync_vmcs12_to_shadow(struct kvm_vcpu *vcpu) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2035 | { |
| 2036 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 2037 | |
Vitaly Kuznetsov | dc31338 | 2021-05-26 15:20:24 +0200 | [diff] [blame] | 2038 | if (evmptr_is_valid(vmx->nested.hv_evmcs_vmptr)) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2039 | copy_vmcs12_to_enlightened(vmx); |
Vitaly Kuznetsov | dc31338 | 2021-05-26 15:20:24 +0200 | [diff] [blame] | 2040 | else |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2041 | copy_vmcs12_to_shadow(vmx); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2042 | |
Sean Christopherson | 3731905ef | 2019-05-07 08:36:27 -0700 | [diff] [blame] | 2043 | vmx->nested.need_vmcs12_to_shadow_sync = false; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2044 | } |
| 2045 | |
| 2046 | static enum hrtimer_restart vmx_preemption_timer_fn(struct hrtimer *timer) |
| 2047 | { |
| 2048 | struct vcpu_vmx *vmx = |
| 2049 | container_of(timer, struct vcpu_vmx, nested.preemption_timer); |
| 2050 | |
| 2051 | vmx->nested.preemption_timer_expired = true; |
| 2052 | kvm_make_request(KVM_REQ_EVENT, &vmx->vcpu); |
| 2053 | kvm_vcpu_kick(&vmx->vcpu); |
| 2054 | |
| 2055 | return HRTIMER_NORESTART; |
| 2056 | } |
| 2057 | |
Peter Shier | 850448f | 2020-05-26 14:51:06 -0700 | [diff] [blame] | 2058 | static u64 vmx_calc_preemption_timer_value(struct kvm_vcpu *vcpu) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2059 | { |
Peter Shier | 850448f | 2020-05-26 14:51:06 -0700 | [diff] [blame] | 2060 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 2061 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
Peter Shier | 850448f | 2020-05-26 14:51:06 -0700 | [diff] [blame] | 2062 | |
| 2063 | u64 l1_scaled_tsc = kvm_read_l1_tsc(vcpu, rdtsc()) >> |
| 2064 | VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE; |
| 2065 | |
| 2066 | if (!vmx->nested.has_preemption_timer_deadline) { |
Makarand Sonare | 8d7fbf0 | 2020-05-26 14:51:07 -0700 | [diff] [blame] | 2067 | vmx->nested.preemption_timer_deadline = |
| 2068 | vmcs12->vmx_preemption_timer_value + l1_scaled_tsc; |
Peter Shier | 850448f | 2020-05-26 14:51:06 -0700 | [diff] [blame] | 2069 | vmx->nested.has_preemption_timer_deadline = true; |
Makarand Sonare | 8d7fbf0 | 2020-05-26 14:51:07 -0700 | [diff] [blame] | 2070 | } |
| 2071 | return vmx->nested.preemption_timer_deadline - l1_scaled_tsc; |
Peter Shier | 850448f | 2020-05-26 14:51:06 -0700 | [diff] [blame] | 2072 | } |
| 2073 | |
| 2074 | static void vmx_start_preemption_timer(struct kvm_vcpu *vcpu, |
| 2075 | u64 preemption_timeout) |
| 2076 | { |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2077 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 2078 | |
| 2079 | /* |
| 2080 | * A timer value of zero is architecturally guaranteed to cause |
| 2081 | * a VMExit prior to executing any instructions in the guest. |
| 2082 | */ |
| 2083 | if (preemption_timeout == 0) { |
| 2084 | vmx_preemption_timer_fn(&vmx->nested.preemption_timer); |
| 2085 | return; |
| 2086 | } |
| 2087 | |
| 2088 | if (vcpu->arch.virtual_tsc_khz == 0) |
| 2089 | return; |
| 2090 | |
| 2091 | preemption_timeout <<= VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE; |
| 2092 | preemption_timeout *= 1000000; |
| 2093 | do_div(preemption_timeout, vcpu->arch.virtual_tsc_khz); |
| 2094 | hrtimer_start(&vmx->nested.preemption_timer, |
Jim Mattson | ada0098 | 2020-05-08 13:36:42 -0700 | [diff] [blame] | 2095 | ktime_add_ns(ktime_get(), preemption_timeout), |
| 2096 | HRTIMER_MODE_ABS_PINNED); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2097 | } |
| 2098 | |
| 2099 | static u64 nested_vmx_calc_efer(struct vcpu_vmx *vmx, struct vmcs12 *vmcs12) |
| 2100 | { |
| 2101 | if (vmx->nested.nested_run_pending && |
| 2102 | (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_EFER)) |
| 2103 | return vmcs12->guest_ia32_efer; |
| 2104 | else if (vmcs12->vm_entry_controls & VM_ENTRY_IA32E_MODE) |
| 2105 | return vmx->vcpu.arch.efer | (EFER_LMA | EFER_LME); |
| 2106 | else |
| 2107 | return vmx->vcpu.arch.efer & ~(EFER_LMA | EFER_LME); |
| 2108 | } |
| 2109 | |
| 2110 | static void prepare_vmcs02_constant_state(struct vcpu_vmx *vmx) |
| 2111 | { |
| 2112 | /* |
| 2113 | * If vmcs02 hasn't been initialized, set the constant vmcs02 state |
| 2114 | * according to L0's settings (vmcs12 is irrelevant here). Host |
| 2115 | * fields that come from L0 and are not constant, e.g. HOST_CR3, |
| 2116 | * will be set as needed prior to VMLAUNCH/VMRESUME. |
| 2117 | */ |
| 2118 | if (vmx->nested.vmcs02_initialized) |
| 2119 | return; |
| 2120 | vmx->nested.vmcs02_initialized = true; |
| 2121 | |
| 2122 | /* |
| 2123 | * We don't care what the EPTP value is we just need to guarantee |
| 2124 | * it's valid so we don't get a false positive when doing early |
| 2125 | * consistency checks. |
| 2126 | */ |
| 2127 | if (enable_ept && nested_early_check) |
Sean Christopherson | 2a40b90 | 2020-07-15 20:41:18 -0700 | [diff] [blame] | 2128 | vmcs_write64(EPT_POINTER, |
| 2129 | construct_eptp(&vmx->vcpu, 0, PT64_ROOT_4LEVEL)); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2130 | |
| 2131 | /* All VMFUNCs are currently emulated through L0 vmexits. */ |
| 2132 | if (cpu_has_vmx_vmfunc()) |
| 2133 | vmcs_write64(VM_FUNCTION_CONTROL, 0); |
| 2134 | |
| 2135 | if (cpu_has_vmx_posted_intr()) |
| 2136 | vmcs_write16(POSTED_INTR_NV, POSTED_INTR_NESTED_VECTOR); |
| 2137 | |
| 2138 | if (cpu_has_vmx_msr_bitmap()) |
| 2139 | vmcs_write64(MSR_BITMAP, __pa(vmx->nested.vmcs02.msr_bitmap)); |
| 2140 | |
Sean Christopherson | 4d6c989 | 2019-05-07 09:06:30 -0700 | [diff] [blame] | 2141 | /* |
Sean Christopherson | c3bb9a2 | 2021-02-12 16:50:07 -0800 | [diff] [blame] | 2142 | * PML is emulated for L2, but never enabled in hardware as the MMU |
| 2143 | * handles A/D emulation. Disabling PML for L2 also avoids having to |
| 2144 | * deal with filtering out L2 GPAs from the buffer. |
Sean Christopherson | 4d6c989 | 2019-05-07 09:06:30 -0700 | [diff] [blame] | 2145 | */ |
| 2146 | if (enable_pml) { |
Sean Christopherson | c3bb9a2 | 2021-02-12 16:50:07 -0800 | [diff] [blame] | 2147 | vmcs_write64(PML_ADDRESS, 0); |
| 2148 | vmcs_write16(GUEST_PML_INDEX, -1); |
Sean Christopherson | 4d6c989 | 2019-05-07 09:06:30 -0700 | [diff] [blame] | 2149 | } |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2150 | |
Sean Christopherson | c538d57 | 2019-05-07 09:06:29 -0700 | [diff] [blame] | 2151 | if (cpu_has_vmx_encls_vmexit()) |
Yu Zhang | 64c7850 | 2021-09-30 01:51:53 +0800 | [diff] [blame] | 2152 | vmcs_write64(ENCLS_EXITING_BITMAP, INVALID_GPA); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2153 | |
| 2154 | /* |
| 2155 | * Set the MSR load/store lists to match L0's settings. Only the |
| 2156 | * addresses are constant (for vmcs02), the counts can change based |
| 2157 | * on L2's behavior, e.g. switching to/from long mode. |
| 2158 | */ |
Aaron Lewis | 662f1d1 | 2019-11-07 21:14:39 -0800 | [diff] [blame] | 2159 | 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] | 2160 | vmcs_write64(VM_EXIT_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.host.val)); |
| 2161 | vmcs_write64(VM_ENTRY_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.guest.val)); |
| 2162 | |
| 2163 | vmx_set_constant_host_state(vmx); |
| 2164 | } |
| 2165 | |
Paolo Bonzini | b1346ab | 2019-06-06 17:24:00 +0200 | [diff] [blame] | 2166 | static void prepare_vmcs02_early_rare(struct vcpu_vmx *vmx, |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2167 | struct vmcs12 *vmcs12) |
| 2168 | { |
| 2169 | prepare_vmcs02_constant_state(vmx); |
| 2170 | |
Yu Zhang | 64c7850 | 2021-09-30 01:51:53 +0800 | [diff] [blame] | 2171 | vmcs_write64(VMCS_LINK_POINTER, INVALID_GPA); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2172 | |
| 2173 | if (enable_vpid) { |
| 2174 | if (nested_cpu_has_vpid(vmcs12) && vmx->nested.vpid02) |
| 2175 | vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->nested.vpid02); |
| 2176 | else |
| 2177 | vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid); |
| 2178 | } |
| 2179 | } |
| 2180 | |
Sean Christopherson | 389ab25 | 2021-08-10 10:19:50 -0700 | [diff] [blame] | 2181 | static void prepare_vmcs02_early(struct vcpu_vmx *vmx, struct loaded_vmcs *vmcs01, |
| 2182 | struct vmcs12 *vmcs12) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2183 | { |
Sean Christopherson | c3bb9a2 | 2021-02-12 16:50:07 -0800 | [diff] [blame] | 2184 | u32 exec_control; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2185 | u64 guest_efer = nested_vmx_calc_efer(vmx, vmcs12); |
| 2186 | |
Vitaly Kuznetsov | 1e9dfbd | 2021-05-26 15:20:16 +0200 | [diff] [blame] | 2187 | 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] | 2188 | prepare_vmcs02_early_rare(vmx, vmcs12); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2189 | |
| 2190 | /* |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2191 | * PIN CONTROLS |
| 2192 | */ |
Sean Christopherson | 389ab25 | 2021-08-10 10:19:50 -0700 | [diff] [blame] | 2193 | exec_control = __pin_controls_get(vmcs01); |
Sean Christopherson | 804939e | 2019-05-07 12:18:05 -0700 | [diff] [blame] | 2194 | exec_control |= (vmcs12->pin_based_vm_exec_control & |
| 2195 | ~PIN_BASED_VMX_PREEMPTION_TIMER); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2196 | |
| 2197 | /* Posted interrupts setting is only taken from vmcs12. */ |
Sean Christopherson | f7782bb8 | 2021-08-10 07:45:26 -0700 | [diff] [blame] | 2198 | vmx->nested.pi_pending = false; |
| 2199 | if (nested_cpu_has_posted_intr(vmcs12)) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2200 | vmx->nested.posted_intr_nv = vmcs12->posted_intr_nv; |
Sean Christopherson | f7782bb8 | 2021-08-10 07:45:26 -0700 | [diff] [blame] | 2201 | else |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2202 | exec_control &= ~PIN_BASED_POSTED_INTR; |
Sean Christopherson | 3af80fe | 2019-05-07 12:18:00 -0700 | [diff] [blame] | 2203 | pin_controls_set(vmx, exec_control); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2204 | |
| 2205 | /* |
| 2206 | * EXEC CONTROLS |
| 2207 | */ |
Sean Christopherson | 389ab25 | 2021-08-10 10:19:50 -0700 | [diff] [blame] | 2208 | exec_control = __exec_controls_get(vmcs01); /* L0's desires */ |
Xiaoyao Li | 9dadc2f | 2019-12-06 16:45:24 +0800 | [diff] [blame] | 2209 | exec_control &= ~CPU_BASED_INTR_WINDOW_EXITING; |
Xiaoyao Li | 4e2a0bc | 2019-12-06 16:45:25 +0800 | [diff] [blame] | 2210 | exec_control &= ~CPU_BASED_NMI_WINDOW_EXITING; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2211 | exec_control &= ~CPU_BASED_TPR_SHADOW; |
| 2212 | exec_control |= vmcs12->cpu_based_vm_exec_control; |
| 2213 | |
Liran Alon | 02d496cf | 2019-11-11 14:30:55 +0200 | [diff] [blame] | 2214 | vmx->nested.l1_tpr_threshold = -1; |
Sean Christopherson | ca2f546 | 2019-05-07 09:06:33 -0700 | [diff] [blame] | 2215 | if (exec_control & CPU_BASED_TPR_SHADOW) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2216 | vmcs_write32(TPR_THRESHOLD, vmcs12->tpr_threshold); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2217 | #ifdef CONFIG_X86_64 |
Sean Christopherson | ca2f546 | 2019-05-07 09:06:33 -0700 | [diff] [blame] | 2218 | else |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2219 | exec_control |= CPU_BASED_CR8_LOAD_EXITING | |
| 2220 | CPU_BASED_CR8_STORE_EXITING; |
| 2221 | #endif |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2222 | |
| 2223 | /* |
| 2224 | * A vmexit (to either L1 hypervisor or L0 userspace) is always needed |
| 2225 | * for I/O port accesses. |
| 2226 | */ |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2227 | exec_control |= CPU_BASED_UNCOND_IO_EXITING; |
Sean Christopherson | de0286b | 2019-05-07 12:18:01 -0700 | [diff] [blame] | 2228 | exec_control &= ~CPU_BASED_USE_IO_BITMAPS; |
| 2229 | |
| 2230 | /* |
| 2231 | * This bit will be computed in nested_get_vmcs12_pages, because |
| 2232 | * we do not have access to L1's MSR bitmap yet. For now, keep |
| 2233 | * the same bit as before, hoping to avoid multiple VMWRITEs that |
| 2234 | * only set/clear this bit. |
| 2235 | */ |
| 2236 | exec_control &= ~CPU_BASED_USE_MSR_BITMAPS; |
| 2237 | exec_control |= exec_controls_get(vmx) & CPU_BASED_USE_MSR_BITMAPS; |
| 2238 | |
Sean Christopherson | 3af80fe | 2019-05-07 12:18:00 -0700 | [diff] [blame] | 2239 | exec_controls_set(vmx, exec_control); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2240 | |
| 2241 | /* |
| 2242 | * SECONDARY EXEC CONTROLS |
| 2243 | */ |
| 2244 | if (cpu_has_secondary_exec_ctrls()) { |
Sean Christopherson | 389ab25 | 2021-08-10 10:19:50 -0700 | [diff] [blame] | 2245 | exec_control = __secondary_exec_controls_get(vmcs01); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2246 | |
| 2247 | /* Take the following fields only from vmcs12 */ |
| 2248 | exec_control &= ~(SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES | |
Sean Christopherson | 389ab25 | 2021-08-10 10:19:50 -0700 | [diff] [blame] | 2249 | SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2250 | SECONDARY_EXEC_ENABLE_INVPCID | |
Sean Christopherson | 7f3603b | 2020-09-23 09:50:47 -0700 | [diff] [blame] | 2251 | SECONDARY_EXEC_ENABLE_RDTSCP | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2252 | SECONDARY_EXEC_XSAVES | |
Tao Xu | e69e72fa | 2019-07-16 14:55:49 +0800 | [diff] [blame] | 2253 | SECONDARY_EXEC_ENABLE_USR_WAIT_PAUSE | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2254 | SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY | |
| 2255 | SECONDARY_EXEC_APIC_REGISTER_VIRT | |
Ilias Stamatis | d041b5e | 2021-05-26 19:44:17 +0100 | [diff] [blame] | 2256 | SECONDARY_EXEC_ENABLE_VMFUNC | |
Sean Christopherson | 389ab25 | 2021-08-10 10:19:50 -0700 | [diff] [blame] | 2257 | SECONDARY_EXEC_TSC_SCALING | |
| 2258 | SECONDARY_EXEC_DESC); |
| 2259 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2260 | if (nested_cpu_has(vmcs12, |
Sean Christopherson | c3bb9a2 | 2021-02-12 16:50:07 -0800 | [diff] [blame] | 2261 | CPU_BASED_ACTIVATE_SECONDARY_CONTROLS)) |
| 2262 | exec_control |= vmcs12->secondary_vm_exec_control; |
| 2263 | |
| 2264 | /* PML is emulated and never enabled in hardware for L2. */ |
| 2265 | exec_control &= ~SECONDARY_EXEC_ENABLE_PML; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2266 | |
| 2267 | /* VMCS shadowing for L2 is emulated for now */ |
| 2268 | exec_control &= ~SECONDARY_EXEC_SHADOW_VMCS; |
| 2269 | |
Sean Christopherson | 469debd | 2019-05-07 12:18:02 -0700 | [diff] [blame] | 2270 | /* |
| 2271 | * Preset *DT exiting when emulating UMIP, so that vmx_set_cr4() |
| 2272 | * will not have to rewrite the controls just for this bit. |
| 2273 | */ |
| 2274 | if (!boot_cpu_has(X86_FEATURE_UMIP) && vmx_umip_emulated() && |
| 2275 | (vmcs12->guest_cr4 & X86_CR4_UMIP)) |
| 2276 | exec_control |= SECONDARY_EXEC_DESC; |
| 2277 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2278 | if (exec_control & SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY) |
| 2279 | vmcs_write16(GUEST_INTR_STATUS, |
| 2280 | vmcs12->guest_intr_status); |
| 2281 | |
Krish Sadhukhan | bddd82d | 2020-09-21 08:10:25 +0000 | [diff] [blame] | 2282 | if (!nested_cpu_has2(vmcs12, SECONDARY_EXEC_UNRESTRICTED_GUEST)) |
| 2283 | exec_control &= ~SECONDARY_EXEC_UNRESTRICTED_GUEST; |
| 2284 | |
Sean Christopherson | 72add91 | 2021-04-12 16:21:42 +1200 | [diff] [blame] | 2285 | if (exec_control & SECONDARY_EXEC_ENCLS_EXITING) |
| 2286 | vmx_write_encls_bitmap(&vmx->vcpu, vmcs12); |
| 2287 | |
Sean Christopherson | 3af80fe | 2019-05-07 12:18:00 -0700 | [diff] [blame] | 2288 | secondary_exec_controls_set(vmx, exec_control); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2289 | } |
| 2290 | |
| 2291 | /* |
| 2292 | * ENTRY CONTROLS |
| 2293 | * |
| 2294 | * vmcs12's VM_{ENTRY,EXIT}_LOAD_IA32_EFER and VM_ENTRY_IA32E_MODE |
| 2295 | * are emulated by vmx_set_efer() in prepare_vmcs02(), but speculate |
| 2296 | * on the related bits (if supported by the CPU) in the hope that |
| 2297 | * we can avoid VMWrites during vmx_set_efer(). |
| 2298 | */ |
Sean Christopherson | 389ab25 | 2021-08-10 10:19:50 -0700 | [diff] [blame] | 2299 | exec_control = __vm_entry_controls_get(vmcs01); |
| 2300 | exec_control |= vmcs12->vm_entry_controls; |
| 2301 | exec_control &= ~(VM_ENTRY_IA32E_MODE | VM_ENTRY_LOAD_IA32_EFER); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2302 | if (cpu_has_load_ia32_efer()) { |
| 2303 | if (guest_efer & EFER_LMA) |
| 2304 | exec_control |= VM_ENTRY_IA32E_MODE; |
| 2305 | if (guest_efer != host_efer) |
| 2306 | exec_control |= VM_ENTRY_LOAD_IA32_EFER; |
| 2307 | } |
Sean Christopherson | 3af80fe | 2019-05-07 12:18:00 -0700 | [diff] [blame] | 2308 | vm_entry_controls_set(vmx, exec_control); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2309 | |
| 2310 | /* |
| 2311 | * EXIT CONTROLS |
| 2312 | * |
| 2313 | * L2->L1 exit controls are emulated - the hardware exit is to L0 so |
| 2314 | * we should use its exit controls. Note that VM_EXIT_LOAD_IA32_EFER |
| 2315 | * bits may be modified by vmx_set_efer() in prepare_vmcs02(). |
| 2316 | */ |
Sean Christopherson | 389ab25 | 2021-08-10 10:19:50 -0700 | [diff] [blame] | 2317 | exec_control = __vm_exit_controls_get(vmcs01); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2318 | if (cpu_has_load_ia32_efer() && guest_efer != host_efer) |
| 2319 | exec_control |= VM_EXIT_LOAD_IA32_EFER; |
Sean Christopherson | 389ab25 | 2021-08-10 10:19:50 -0700 | [diff] [blame] | 2320 | else |
| 2321 | exec_control &= ~VM_EXIT_LOAD_IA32_EFER; |
Sean Christopherson | 3af80fe | 2019-05-07 12:18:00 -0700 | [diff] [blame] | 2322 | vm_exit_controls_set(vmx, exec_control); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2323 | |
| 2324 | /* |
| 2325 | * Interrupt/Exception Fields |
| 2326 | */ |
| 2327 | if (vmx->nested.nested_run_pending) { |
| 2328 | vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, |
| 2329 | vmcs12->vm_entry_intr_info_field); |
| 2330 | vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE, |
| 2331 | vmcs12->vm_entry_exception_error_code); |
| 2332 | vmcs_write32(VM_ENTRY_INSTRUCTION_LEN, |
| 2333 | vmcs12->vm_entry_instruction_len); |
| 2334 | vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, |
| 2335 | vmcs12->guest_interruptibility_info); |
| 2336 | vmx->loaded_vmcs->nmi_known_unmasked = |
| 2337 | !(vmcs12->guest_interruptibility_info & GUEST_INTR_STATE_NMI); |
| 2338 | } else { |
| 2339 | vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0); |
| 2340 | } |
| 2341 | } |
| 2342 | |
Paolo Bonzini | b1346ab | 2019-06-06 17:24:00 +0200 | [diff] [blame] | 2343 | static void prepare_vmcs02_rare(struct vcpu_vmx *vmx, struct vmcs12 *vmcs12) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2344 | { |
| 2345 | struct hv_enlightened_vmcs *hv_evmcs = vmx->nested.hv_evmcs; |
| 2346 | |
| 2347 | if (!hv_evmcs || !(hv_evmcs->hv_clean_fields & |
| 2348 | HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP2)) { |
| 2349 | vmcs_write16(GUEST_ES_SELECTOR, vmcs12->guest_es_selector); |
| 2350 | vmcs_write16(GUEST_CS_SELECTOR, vmcs12->guest_cs_selector); |
| 2351 | vmcs_write16(GUEST_SS_SELECTOR, vmcs12->guest_ss_selector); |
| 2352 | vmcs_write16(GUEST_DS_SELECTOR, vmcs12->guest_ds_selector); |
| 2353 | vmcs_write16(GUEST_FS_SELECTOR, vmcs12->guest_fs_selector); |
| 2354 | vmcs_write16(GUEST_GS_SELECTOR, vmcs12->guest_gs_selector); |
| 2355 | vmcs_write16(GUEST_LDTR_SELECTOR, vmcs12->guest_ldtr_selector); |
| 2356 | vmcs_write16(GUEST_TR_SELECTOR, vmcs12->guest_tr_selector); |
| 2357 | vmcs_write32(GUEST_ES_LIMIT, vmcs12->guest_es_limit); |
| 2358 | vmcs_write32(GUEST_CS_LIMIT, vmcs12->guest_cs_limit); |
| 2359 | vmcs_write32(GUEST_SS_LIMIT, vmcs12->guest_ss_limit); |
| 2360 | vmcs_write32(GUEST_DS_LIMIT, vmcs12->guest_ds_limit); |
| 2361 | vmcs_write32(GUEST_FS_LIMIT, vmcs12->guest_fs_limit); |
| 2362 | vmcs_write32(GUEST_GS_LIMIT, vmcs12->guest_gs_limit); |
| 2363 | vmcs_write32(GUEST_LDTR_LIMIT, vmcs12->guest_ldtr_limit); |
| 2364 | vmcs_write32(GUEST_TR_LIMIT, vmcs12->guest_tr_limit); |
| 2365 | vmcs_write32(GUEST_GDTR_LIMIT, vmcs12->guest_gdtr_limit); |
| 2366 | vmcs_write32(GUEST_IDTR_LIMIT, vmcs12->guest_idtr_limit); |
Sean Christopherson | 1c6f0b4 | 2019-05-07 08:36:25 -0700 | [diff] [blame] | 2367 | vmcs_write32(GUEST_CS_AR_BYTES, vmcs12->guest_cs_ar_bytes); |
| 2368 | vmcs_write32(GUEST_SS_AR_BYTES, vmcs12->guest_ss_ar_bytes); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2369 | vmcs_write32(GUEST_ES_AR_BYTES, vmcs12->guest_es_ar_bytes); |
| 2370 | vmcs_write32(GUEST_DS_AR_BYTES, vmcs12->guest_ds_ar_bytes); |
| 2371 | vmcs_write32(GUEST_FS_AR_BYTES, vmcs12->guest_fs_ar_bytes); |
| 2372 | vmcs_write32(GUEST_GS_AR_BYTES, vmcs12->guest_gs_ar_bytes); |
| 2373 | vmcs_write32(GUEST_LDTR_AR_BYTES, vmcs12->guest_ldtr_ar_bytes); |
| 2374 | vmcs_write32(GUEST_TR_AR_BYTES, vmcs12->guest_tr_ar_bytes); |
| 2375 | vmcs_writel(GUEST_ES_BASE, vmcs12->guest_es_base); |
| 2376 | vmcs_writel(GUEST_CS_BASE, vmcs12->guest_cs_base); |
| 2377 | vmcs_writel(GUEST_SS_BASE, vmcs12->guest_ss_base); |
| 2378 | vmcs_writel(GUEST_DS_BASE, vmcs12->guest_ds_base); |
| 2379 | vmcs_writel(GUEST_FS_BASE, vmcs12->guest_fs_base); |
| 2380 | vmcs_writel(GUEST_GS_BASE, vmcs12->guest_gs_base); |
| 2381 | vmcs_writel(GUEST_LDTR_BASE, vmcs12->guest_ldtr_base); |
| 2382 | vmcs_writel(GUEST_TR_BASE, vmcs12->guest_tr_base); |
| 2383 | vmcs_writel(GUEST_GDTR_BASE, vmcs12->guest_gdtr_base); |
| 2384 | vmcs_writel(GUEST_IDTR_BASE, vmcs12->guest_idtr_base); |
Sean Christopherson | fc387d8 | 2020-09-23 11:44:46 -0700 | [diff] [blame] | 2385 | |
| 2386 | vmx->segment_cache.bitmask = 0; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2387 | } |
| 2388 | |
| 2389 | if (!hv_evmcs || !(hv_evmcs->hv_clean_fields & |
| 2390 | HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP1)) { |
| 2391 | vmcs_write32(GUEST_SYSENTER_CS, vmcs12->guest_sysenter_cs); |
| 2392 | vmcs_writel(GUEST_PENDING_DBG_EXCEPTIONS, |
| 2393 | vmcs12->guest_pending_dbg_exceptions); |
| 2394 | vmcs_writel(GUEST_SYSENTER_ESP, vmcs12->guest_sysenter_esp); |
| 2395 | vmcs_writel(GUEST_SYSENTER_EIP, vmcs12->guest_sysenter_eip); |
| 2396 | |
| 2397 | /* |
| 2398 | * L1 may access the L2's PDPTR, so save them to construct |
| 2399 | * vmcs12 |
| 2400 | */ |
| 2401 | if (enable_ept) { |
| 2402 | vmcs_write64(GUEST_PDPTR0, vmcs12->guest_pdptr0); |
| 2403 | vmcs_write64(GUEST_PDPTR1, vmcs12->guest_pdptr1); |
| 2404 | vmcs_write64(GUEST_PDPTR2, vmcs12->guest_pdptr2); |
| 2405 | vmcs_write64(GUEST_PDPTR3, vmcs12->guest_pdptr3); |
| 2406 | } |
Sean Christopherson | c27e5b0 | 2019-05-07 09:06:39 -0700 | [diff] [blame] | 2407 | |
| 2408 | if (kvm_mpx_supported() && vmx->nested.nested_run_pending && |
| 2409 | (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_BNDCFGS)) |
| 2410 | vmcs_write64(GUEST_BNDCFGS, vmcs12->guest_bndcfgs); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2411 | } |
| 2412 | |
| 2413 | if (nested_cpu_has_xsaves(vmcs12)) |
| 2414 | vmcs_write64(XSS_EXIT_BITMAP, vmcs12->xss_exit_bitmap); |
| 2415 | |
| 2416 | /* |
| 2417 | * Whether page-faults are trapped is determined by a combination of |
Paolo Bonzini | a0c1343 | 2020-07-10 17:48:08 +0200 | [diff] [blame] | 2418 | * 3 settings: PFEC_MASK, PFEC_MATCH and EXCEPTION_BITMAP.PF. If L0 |
| 2419 | * doesn't care about page faults then we should set all of these to |
| 2420 | * L1's desires. However, if L0 does care about (some) page faults, it |
| 2421 | * is not easy (if at all possible?) to merge L0 and L1's desires, we |
| 2422 | * simply ask to exit on each and every L2 page fault. This is done by |
| 2423 | * setting MASK=MATCH=0 and (see below) EB.PF=1. |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2424 | * Note that below we don't need special code to set EB.PF beyond the |
| 2425 | * "or"ing of the EB of vmcs01 and vmcs12, because when enable_ept, |
| 2426 | * vmcs01's EB.PF is 0 so the "or" will take vmcs12's value, and when |
| 2427 | * !enable_ept, EB.PF is 1, so the "or" will always be 1. |
| 2428 | */ |
Paolo Bonzini | a0c1343 | 2020-07-10 17:48:08 +0200 | [diff] [blame] | 2429 | if (vmx_need_pf_intercept(&vmx->vcpu)) { |
| 2430 | /* |
| 2431 | * TODO: if both L0 and L1 need the same MASK and MATCH, |
| 2432 | * go ahead and use it? |
| 2433 | */ |
| 2434 | vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK, 0); |
| 2435 | vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH, 0); |
| 2436 | } else { |
| 2437 | vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK, vmcs12->page_fault_error_code_mask); |
| 2438 | vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH, vmcs12->page_fault_error_code_match); |
| 2439 | } |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2440 | |
| 2441 | if (cpu_has_vmx_apicv()) { |
| 2442 | vmcs_write64(EOI_EXIT_BITMAP0, vmcs12->eoi_exit_bitmap0); |
| 2443 | vmcs_write64(EOI_EXIT_BITMAP1, vmcs12->eoi_exit_bitmap1); |
| 2444 | vmcs_write64(EOI_EXIT_BITMAP2, vmcs12->eoi_exit_bitmap2); |
| 2445 | vmcs_write64(EOI_EXIT_BITMAP3, vmcs12->eoi_exit_bitmap3); |
| 2446 | } |
| 2447 | |
Aaron Lewis | 662f1d1 | 2019-11-07 21:14:39 -0800 | [diff] [blame] | 2448 | /* |
| 2449 | * Make sure the msr_autostore list is up to date before we set the |
| 2450 | * count in the vmcs02. |
| 2451 | */ |
| 2452 | prepare_vmx_msr_autostore_list(&vmx->vcpu, MSR_IA32_TSC); |
| 2453 | |
| 2454 | vmcs_write32(VM_EXIT_MSR_STORE_COUNT, vmx->msr_autostore.guest.nr); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2455 | vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, vmx->msr_autoload.host.nr); |
| 2456 | vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, vmx->msr_autoload.guest.nr); |
| 2457 | |
| 2458 | set_cr4_guest_host_mask(vmx); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2459 | } |
| 2460 | |
| 2461 | /* |
| 2462 | * prepare_vmcs02 is called when the L1 guest hypervisor runs its nested |
| 2463 | * L2 guest. L1 has a vmcs for L2 (vmcs12), and this function "merges" it |
| 2464 | * with L0's requirements for its guest (a.k.a. vmcs01), so we can run the L2 |
| 2465 | * guest in a way that will both be appropriate to L1's requests, and our |
| 2466 | * needs. In addition to modifying the active vmcs (which is vmcs02), this |
| 2467 | * function also has additional necessary side-effects, like setting various |
| 2468 | * vcpu->arch fields. |
| 2469 | * Returns 0 on success, 1 on failure. Invalid state exit qualification code |
| 2470 | * is assigned to entry_failure_code on failure. |
| 2471 | */ |
| 2472 | static int prepare_vmcs02(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12, |
Maxim Levitsky | 0f85722 | 2021-06-07 12:02:00 +0300 | [diff] [blame] | 2473 | bool from_vmentry, |
Sean Christopherson | 68cda40 | 2020-05-11 15:05:29 -0700 | [diff] [blame] | 2474 | enum vm_entry_failure_code *entry_failure_code) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2475 | { |
| 2476 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Sean Christopherson | c7554efc | 2019-05-07 09:06:40 -0700 | [diff] [blame] | 2477 | bool load_guest_pdptrs_vmcs12 = false; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2478 | |
Vitaly Kuznetsov | 1e9dfbd | 2021-05-26 15:20:16 +0200 | [diff] [blame] | 2479 | 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] | 2480 | prepare_vmcs02_rare(vmx, vmcs12); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2481 | vmx->nested.dirty_vmcs12 = false; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2482 | |
Vitaly Kuznetsov | 1e9dfbd | 2021-05-26 15:20:16 +0200 | [diff] [blame] | 2483 | load_guest_pdptrs_vmcs12 = !evmptr_is_valid(vmx->nested.hv_evmcs_vmptr) || |
| 2484 | !(vmx->nested.hv_evmcs->hv_clean_fields & |
Sean Christopherson | c7554efc | 2019-05-07 09:06:40 -0700 | [diff] [blame] | 2485 | HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP1); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2486 | } |
| 2487 | |
| 2488 | if (vmx->nested.nested_run_pending && |
| 2489 | (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS)) { |
| 2490 | kvm_set_dr(vcpu, 7, vmcs12->guest_dr7); |
| 2491 | vmcs_write64(GUEST_IA32_DEBUGCTL, vmcs12->guest_ia32_debugctl); |
| 2492 | } else { |
| 2493 | kvm_set_dr(vcpu, 7, vcpu->arch.dr7); |
| 2494 | vmcs_write64(GUEST_IA32_DEBUGCTL, vmx->nested.vmcs01_debugctl); |
| 2495 | } |
Sean Christopherson | 3b013a2 | 2019-05-07 09:06:28 -0700 | [diff] [blame] | 2496 | if (kvm_mpx_supported() && (!vmx->nested.nested_run_pending || |
| 2497 | !(vmcs12->vm_entry_controls & VM_ENTRY_LOAD_BNDCFGS))) |
| 2498 | vmcs_write64(GUEST_BNDCFGS, vmx->nested.vmcs01_guest_bndcfgs); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2499 | vmx_set_rflags(vcpu, vmcs12->guest_rflags); |
| 2500 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2501 | /* EXCEPTION_BITMAP and CR0_GUEST_HOST_MASK should basically be the |
| 2502 | * bitwise-or of what L1 wants to trap for L2, and what we want to |
| 2503 | * trap. Note that CR0.TS also needs updating - we do this later. |
| 2504 | */ |
Jason Baron | b6a7cc3 | 2021-01-14 22:27:54 -0500 | [diff] [blame] | 2505 | vmx_update_exception_bitmap(vcpu); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2506 | vcpu->arch.cr0_guest_owned_bits &= ~vmcs12->cr0_guest_host_mask; |
| 2507 | vmcs_writel(CR0_GUEST_HOST_MASK, ~vcpu->arch.cr0_guest_owned_bits); |
| 2508 | |
| 2509 | if (vmx->nested.nested_run_pending && |
| 2510 | (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_PAT)) { |
| 2511 | vmcs_write64(GUEST_IA32_PAT, vmcs12->guest_ia32_pat); |
| 2512 | vcpu->arch.pat = vmcs12->guest_ia32_pat; |
| 2513 | } else if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) { |
| 2514 | vmcs_write64(GUEST_IA32_PAT, vmx->vcpu.arch.pat); |
| 2515 | } |
| 2516 | |
Ilias Stamatis | d041b5e | 2021-05-26 19:44:17 +0100 | [diff] [blame] | 2517 | vcpu->arch.tsc_offset = kvm_calc_nested_tsc_offset( |
| 2518 | vcpu->arch.l1_tsc_offset, |
| 2519 | vmx_get_l2_tsc_offset(vcpu), |
| 2520 | vmx_get_l2_tsc_multiplier(vcpu)); |
| 2521 | |
| 2522 | vcpu->arch.tsc_scaling_ratio = kvm_calc_nested_tsc_multiplier( |
| 2523 | vcpu->arch.l1_tsc_scaling_ratio, |
| 2524 | vmx_get_l2_tsc_multiplier(vcpu)); |
| 2525 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2526 | vmcs_write64(TSC_OFFSET, vcpu->arch.tsc_offset); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2527 | if (kvm_has_tsc_control) |
Ilias Stamatis | 1ab9287 | 2021-06-07 11:54:38 +0100 | [diff] [blame] | 2528 | vmcs_write64(TSC_MULTIPLIER, vcpu->arch.tsc_scaling_ratio); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2529 | |
Sean Christopherson | 50b265a | 2020-03-20 14:28:19 -0700 | [diff] [blame] | 2530 | nested_vmx_transition_tlb_flush(vcpu, vmcs12, true); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2531 | |
| 2532 | if (nested_cpu_has_ept(vmcs12)) |
| 2533 | nested_ept_init_mmu_context(vcpu); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2534 | |
| 2535 | /* |
| 2536 | * This sets GUEST_CR0 to vmcs12->guest_cr0, possibly modifying those |
| 2537 | * bits which we consider mandatory enabled. |
| 2538 | * The CR0_READ_SHADOW is what L2 should have expected to read given |
| 2539 | * the specifications by L1; It's not enough to take |
| 2540 | * vmcs12->cr0_read_shadow because on our cr0_guest_host_mask we we |
| 2541 | * have more bits than L1 expected. |
| 2542 | */ |
| 2543 | vmx_set_cr0(vcpu, vmcs12->guest_cr0); |
| 2544 | vmcs_writel(CR0_READ_SHADOW, nested_read_cr0(vmcs12)); |
| 2545 | |
| 2546 | vmx_set_cr4(vcpu, vmcs12->guest_cr4); |
| 2547 | vmcs_writel(CR4_READ_SHADOW, nested_read_cr4(vmcs12)); |
| 2548 | |
| 2549 | vcpu->arch.efer = nested_vmx_calc_efer(vmx, vmcs12); |
| 2550 | /* Note: may modify VM_ENTRY/EXIT_CONTROLS and GUEST/HOST_IA32_EFER */ |
| 2551 | vmx_set_efer(vcpu, vcpu->arch.efer); |
| 2552 | |
| 2553 | /* |
| 2554 | * Guest state is invalid and unrestricted guest is disabled, |
| 2555 | * which means L1 attempted VMEntry to L2 with invalid state. |
| 2556 | * Fail the VMEntry. |
Maxim Levitsky | c8607e4 | 2021-09-13 17:09:53 +0300 | [diff] [blame] | 2557 | * |
| 2558 | * However when force loading the guest state (SMM exit or |
| 2559 | * loading nested state after migration, it is possible to |
| 2560 | * have invalid guest state now, which will be later fixed by |
| 2561 | * restoring L2 register state |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2562 | */ |
Maxim Levitsky | c8607e4 | 2021-09-13 17:09:53 +0300 | [diff] [blame] | 2563 | if (CC(from_vmentry && !vmx_guest_state_valid(vcpu))) { |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2564 | *entry_failure_code = ENTRY_FAIL_DEFAULT; |
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 | } |
| 2567 | |
| 2568 | /* Shadow page tables on either EPT or shadow page tables. */ |
| 2569 | 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] | 2570 | from_vmentry, entry_failure_code)) |
Sean Christopherson | c80add0 | 2019-04-11 12:18:09 -0700 | [diff] [blame] | 2571 | return -EINVAL; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2572 | |
Sean Christopherson | 04f11ef | 2019-09-27 14:45:16 -0700 | [diff] [blame] | 2573 | /* |
| 2574 | * Immediately write vmcs02.GUEST_CR3. It will be propagated to vmcs12 |
| 2575 | * on nested VM-Exit, which can occur without actually running L2 and |
Paolo Bonzini | 727a7e2 | 2020-03-05 03:52:50 -0500 | [diff] [blame] | 2576 | * 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] | 2577 | * vmcs12.GUEST_ACTIVITYSTATE=HLT, in which case KVM will intercept the |
| 2578 | * transition to HLT instead of running L2. |
| 2579 | */ |
| 2580 | if (enable_ept) |
| 2581 | vmcs_writel(GUEST_CR3, vmcs12->guest_cr3); |
| 2582 | |
Sean Christopherson | c7554efc | 2019-05-07 09:06:40 -0700 | [diff] [blame] | 2583 | /* Late preparation of GUEST_PDPTRs now that EFER and CRs are set. */ |
| 2584 | if (load_guest_pdptrs_vmcs12 && nested_cpu_has_ept(vmcs12) && |
| 2585 | is_pae_paging(vcpu)) { |
| 2586 | vmcs_write64(GUEST_PDPTR0, vmcs12->guest_pdptr0); |
| 2587 | vmcs_write64(GUEST_PDPTR1, vmcs12->guest_pdptr1); |
| 2588 | vmcs_write64(GUEST_PDPTR2, vmcs12->guest_pdptr2); |
| 2589 | vmcs_write64(GUEST_PDPTR3, vmcs12->guest_pdptr3); |
| 2590 | } |
| 2591 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2592 | if (!enable_ept) |
| 2593 | vcpu->arch.walk_mmu->inject_page_fault = vmx_inject_page_fault_nested; |
| 2594 | |
Oliver Upton | 71f7347 | 2019-11-13 16:17:19 -0800 | [diff] [blame] | 2595 | if ((vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL) && |
Oliver Upton | d196842 | 2019-12-13 16:33:58 -0800 | [diff] [blame] | 2596 | WARN_ON_ONCE(kvm_set_msr(vcpu, MSR_CORE_PERF_GLOBAL_CTRL, |
| 2597 | vmcs12->guest_ia32_perf_global_ctrl))) |
Oliver Upton | 71f7347 | 2019-11-13 16:17:19 -0800 | [diff] [blame] | 2598 | return -EINVAL; |
| 2599 | |
Paolo Bonzini | e9c16c7 | 2019-04-30 22:07:26 +0200 | [diff] [blame] | 2600 | kvm_rsp_write(vcpu, vmcs12->guest_rsp); |
| 2601 | kvm_rip_write(vcpu, vmcs12->guest_rip); |
Vitaly Kuznetsov | dc31338 | 2021-05-26 15:20:24 +0200 | [diff] [blame] | 2602 | |
| 2603 | /* |
| 2604 | * It was observed that genuine Hyper-V running in L1 doesn't reset |
| 2605 | * 'hv_clean_fields' by itself, it only sets the corresponding dirty |
| 2606 | * bits when it changes a field in eVMCS. Mark all fields as clean |
| 2607 | * here. |
| 2608 | */ |
| 2609 | if (evmptr_is_valid(vmx->nested.hv_evmcs_vmptr)) |
| 2610 | vmx->nested.hv_evmcs->hv_clean_fields |= |
| 2611 | HV_VMX_ENLIGHTENED_CLEAN_FIELD_ALL; |
| 2612 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2613 | return 0; |
| 2614 | } |
| 2615 | |
| 2616 | static int nested_vmx_check_nmi_controls(struct vmcs12 *vmcs12) |
| 2617 | { |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 2618 | if (CC(!nested_cpu_has_nmi_exiting(vmcs12) && |
| 2619 | nested_cpu_has_virtual_nmis(vmcs12))) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2620 | return -EINVAL; |
| 2621 | |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 2622 | if (CC(!nested_cpu_has_virtual_nmis(vmcs12) && |
Xiaoyao Li | 4e2a0bc | 2019-12-06 16:45:25 +0800 | [diff] [blame] | 2623 | nested_cpu_has(vmcs12, CPU_BASED_NMI_WINDOW_EXITING))) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2624 | return -EINVAL; |
| 2625 | |
| 2626 | return 0; |
| 2627 | } |
| 2628 | |
Sean Christopherson | ac6389a | 2020-03-02 18:02:38 -0800 | [diff] [blame] | 2629 | 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] | 2630 | { |
| 2631 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2632 | |
| 2633 | /* Check for memory type validity */ |
Sean Christopherson | ac6389a | 2020-03-02 18:02:38 -0800 | [diff] [blame] | 2634 | switch (new_eptp & VMX_EPTP_MT_MASK) { |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2635 | case VMX_EPTP_MT_UC: |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 2636 | if (CC(!(vmx->nested.msrs.ept_caps & VMX_EPTP_UC_BIT))) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2637 | return false; |
| 2638 | break; |
| 2639 | case VMX_EPTP_MT_WB: |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 2640 | if (CC(!(vmx->nested.msrs.ept_caps & VMX_EPTP_WB_BIT))) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2641 | return false; |
| 2642 | break; |
| 2643 | default: |
| 2644 | return false; |
| 2645 | } |
| 2646 | |
Sean Christopherson | bb1fcc7 | 2020-03-02 18:02:36 -0800 | [diff] [blame] | 2647 | /* Page-walk levels validity. */ |
Sean Christopherson | ac6389a | 2020-03-02 18:02:38 -0800 | [diff] [blame] | 2648 | switch (new_eptp & VMX_EPTP_PWL_MASK) { |
Sean Christopherson | bb1fcc7 | 2020-03-02 18:02:36 -0800 | [diff] [blame] | 2649 | case VMX_EPTP_PWL_5: |
| 2650 | if (CC(!(vmx->nested.msrs.ept_caps & VMX_EPT_PAGE_WALK_5_BIT))) |
| 2651 | return false; |
| 2652 | break; |
| 2653 | case VMX_EPTP_PWL_4: |
| 2654 | if (CC(!(vmx->nested.msrs.ept_caps & VMX_EPT_PAGE_WALK_4_BIT))) |
| 2655 | return false; |
| 2656 | break; |
| 2657 | default: |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2658 | return false; |
Sean Christopherson | bb1fcc7 | 2020-03-02 18:02:36 -0800 | [diff] [blame] | 2659 | } |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2660 | |
| 2661 | /* Reserved bits should not be set */ |
Sean Christopherson | 636e8b7 | 2021-02-03 16:01:10 -0800 | [diff] [blame] | 2662 | 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] | 2663 | return false; |
| 2664 | |
| 2665 | /* AD, if set, should be supported */ |
Sean Christopherson | ac6389a | 2020-03-02 18:02:38 -0800 | [diff] [blame] | 2666 | if (new_eptp & VMX_EPTP_AD_ENABLE_BIT) { |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 2667 | if (CC(!(vmx->nested.msrs.ept_caps & VMX_EPT_AD_BIT))) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2668 | return false; |
| 2669 | } |
| 2670 | |
| 2671 | return true; |
| 2672 | } |
| 2673 | |
Krish Sadhukhan | 461b4ba | 2018-12-12 13:30:07 -0500 | [diff] [blame] | 2674 | /* |
| 2675 | * Checks related to VM-Execution Control Fields |
| 2676 | */ |
| 2677 | static int nested_check_vm_execution_controls(struct kvm_vcpu *vcpu, |
| 2678 | struct vmcs12 *vmcs12) |
| 2679 | { |
| 2680 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 2681 | |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 2682 | if (CC(!vmx_control_verify(vmcs12->pin_based_vm_exec_control, |
| 2683 | vmx->nested.msrs.pinbased_ctls_low, |
| 2684 | vmx->nested.msrs.pinbased_ctls_high)) || |
| 2685 | CC(!vmx_control_verify(vmcs12->cpu_based_vm_exec_control, |
| 2686 | vmx->nested.msrs.procbased_ctls_low, |
| 2687 | vmx->nested.msrs.procbased_ctls_high))) |
Krish Sadhukhan | 461b4ba | 2018-12-12 13:30:07 -0500 | [diff] [blame] | 2688 | return -EINVAL; |
| 2689 | |
| 2690 | if (nested_cpu_has(vmcs12, CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) && |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 2691 | CC(!vmx_control_verify(vmcs12->secondary_vm_exec_control, |
| 2692 | vmx->nested.msrs.secondary_ctls_low, |
| 2693 | vmx->nested.msrs.secondary_ctls_high))) |
Krish Sadhukhan | 461b4ba | 2018-12-12 13:30:07 -0500 | [diff] [blame] | 2694 | return -EINVAL; |
| 2695 | |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 2696 | 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] | 2697 | nested_vmx_check_io_bitmap_controls(vcpu, vmcs12) || |
| 2698 | nested_vmx_check_msr_bitmap_controls(vcpu, vmcs12) || |
| 2699 | nested_vmx_check_tpr_shadow_controls(vcpu, vmcs12) || |
| 2700 | nested_vmx_check_apic_access_controls(vcpu, vmcs12) || |
| 2701 | nested_vmx_check_apicv_controls(vcpu, vmcs12) || |
| 2702 | nested_vmx_check_nmi_controls(vmcs12) || |
| 2703 | nested_vmx_check_pml_controls(vcpu, vmcs12) || |
| 2704 | nested_vmx_check_unrestricted_guest_controls(vcpu, vmcs12) || |
| 2705 | nested_vmx_check_mode_based_ept_exec_controls(vcpu, vmcs12) || |
| 2706 | nested_vmx_check_shadow_vmcs_controls(vcpu, vmcs12) || |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 2707 | CC(nested_cpu_has_vpid(vmcs12) && !vmcs12->virtual_processor_id)) |
Krish Sadhukhan | 461b4ba | 2018-12-12 13:30:07 -0500 | [diff] [blame] | 2708 | return -EINVAL; |
| 2709 | |
Sean Christopherson | bc44121 | 2019-02-12 16:42:23 -0800 | [diff] [blame] | 2710 | if (!nested_cpu_has_preemption_timer(vmcs12) && |
| 2711 | nested_cpu_has_save_preemption_timer(vmcs12)) |
| 2712 | return -EINVAL; |
| 2713 | |
Krish Sadhukhan | 461b4ba | 2018-12-12 13:30:07 -0500 | [diff] [blame] | 2714 | if (nested_cpu_has_ept(vmcs12) && |
Sean Christopherson | ac6389a | 2020-03-02 18:02:38 -0800 | [diff] [blame] | 2715 | CC(!nested_vmx_check_eptp(vcpu, vmcs12->ept_pointer))) |
Krish Sadhukhan | 461b4ba | 2018-12-12 13:30:07 -0500 | [diff] [blame] | 2716 | return -EINVAL; |
| 2717 | |
| 2718 | if (nested_cpu_has_vmfunc(vmcs12)) { |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 2719 | if (CC(vmcs12->vm_function_control & |
| 2720 | ~vmx->nested.msrs.vmfunc_controls)) |
Krish Sadhukhan | 461b4ba | 2018-12-12 13:30:07 -0500 | [diff] [blame] | 2721 | return -EINVAL; |
| 2722 | |
| 2723 | if (nested_cpu_has_eptp_switching(vmcs12)) { |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 2724 | if (CC(!nested_cpu_has_ept(vmcs12)) || |
| 2725 | CC(!page_address_valid(vcpu, vmcs12->eptp_list_address))) |
Krish Sadhukhan | 461b4ba | 2018-12-12 13:30:07 -0500 | [diff] [blame] | 2726 | return -EINVAL; |
| 2727 | } |
| 2728 | } |
| 2729 | |
| 2730 | return 0; |
| 2731 | } |
| 2732 | |
Krish Sadhukhan | 61446ba | 2018-12-12 13:30:09 -0500 | [diff] [blame] | 2733 | /* |
| 2734 | * Checks related to VM-Exit Control Fields |
| 2735 | */ |
| 2736 | static int nested_check_vm_exit_controls(struct kvm_vcpu *vcpu, |
| 2737 | struct vmcs12 *vmcs12) |
| 2738 | { |
| 2739 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 2740 | |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 2741 | if (CC(!vmx_control_verify(vmcs12->vm_exit_controls, |
| 2742 | vmx->nested.msrs.exit_ctls_low, |
| 2743 | vmx->nested.msrs.exit_ctls_high)) || |
| 2744 | CC(nested_vmx_check_exit_msr_switch_controls(vcpu, vmcs12))) |
Krish Sadhukhan | 61446ba | 2018-12-12 13:30:09 -0500 | [diff] [blame] | 2745 | return -EINVAL; |
| 2746 | |
| 2747 | return 0; |
| 2748 | } |
| 2749 | |
Krish Sadhukhan | 5fbf963 | 2018-12-12 13:30:10 -0500 | [diff] [blame] | 2750 | /* |
| 2751 | * Checks related to VM-Entry Control Fields |
| 2752 | */ |
| 2753 | static int nested_check_vm_entry_controls(struct kvm_vcpu *vcpu, |
| 2754 | struct vmcs12 *vmcs12) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2755 | { |
| 2756 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2757 | |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 2758 | if (CC(!vmx_control_verify(vmcs12->vm_entry_controls, |
| 2759 | vmx->nested.msrs.entry_ctls_low, |
| 2760 | vmx->nested.msrs.entry_ctls_high))) |
Krish Sadhukhan | 5fbf963 | 2018-12-12 13:30:10 -0500 | [diff] [blame] | 2761 | return -EINVAL; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2762 | |
| 2763 | /* |
| 2764 | * From the Intel SDM, volume 3: |
| 2765 | * Fields relevant to VM-entry event injection must be set properly. |
| 2766 | * These fields are the VM-entry interruption-information field, the |
| 2767 | * VM-entry exception error code, and the VM-entry instruction length. |
| 2768 | */ |
| 2769 | if (vmcs12->vm_entry_intr_info_field & INTR_INFO_VALID_MASK) { |
| 2770 | u32 intr_info = vmcs12->vm_entry_intr_info_field; |
| 2771 | u8 vector = intr_info & INTR_INFO_VECTOR_MASK; |
| 2772 | u32 intr_type = intr_info & INTR_INFO_INTR_TYPE_MASK; |
| 2773 | bool has_error_code = intr_info & INTR_INFO_DELIVER_CODE_MASK; |
| 2774 | bool should_have_error_code; |
| 2775 | bool urg = nested_cpu_has2(vmcs12, |
| 2776 | SECONDARY_EXEC_UNRESTRICTED_GUEST); |
| 2777 | bool prot_mode = !urg || vmcs12->guest_cr0 & X86_CR0_PE; |
| 2778 | |
| 2779 | /* VM-entry interruption-info field: interruption type */ |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 2780 | if (CC(intr_type == INTR_TYPE_RESERVED) || |
| 2781 | CC(intr_type == INTR_TYPE_OTHER_EVENT && |
| 2782 | !nested_cpu_supports_monitor_trap_flag(vcpu))) |
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: vector */ |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 2786 | if (CC(intr_type == INTR_TYPE_NMI_INTR && vector != NMI_VECTOR) || |
| 2787 | CC(intr_type == INTR_TYPE_HARD_EXCEPTION && vector > 31) || |
| 2788 | CC(intr_type == INTR_TYPE_OTHER_EVENT && vector != 0)) |
Krish Sadhukhan | 5fbf963 | 2018-12-12 13:30:10 -0500 | [diff] [blame] | 2789 | return -EINVAL; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2790 | |
| 2791 | /* VM-entry interruption-info field: deliver error code */ |
| 2792 | should_have_error_code = |
| 2793 | intr_type == INTR_TYPE_HARD_EXCEPTION && prot_mode && |
| 2794 | x86_exception_has_error_code(vector); |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 2795 | if (CC(has_error_code != should_have_error_code)) |
Krish Sadhukhan | 5fbf963 | 2018-12-12 13:30:10 -0500 | [diff] [blame] | 2796 | return -EINVAL; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2797 | |
| 2798 | /* VM-entry exception error code */ |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 2799 | if (CC(has_error_code && |
Sean Christopherson | 567926c | 2019-10-01 09:21:23 -0700 | [diff] [blame] | 2800 | vmcs12->vm_entry_exception_error_code & GENMASK(31, 16))) |
Krish Sadhukhan | 5fbf963 | 2018-12-12 13:30:10 -0500 | [diff] [blame] | 2801 | return -EINVAL; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2802 | |
| 2803 | /* VM-entry interruption-info field: reserved bits */ |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 2804 | if (CC(intr_info & INTR_INFO_RESVD_BITS_MASK)) |
Krish Sadhukhan | 5fbf963 | 2018-12-12 13:30:10 -0500 | [diff] [blame] | 2805 | return -EINVAL; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2806 | |
| 2807 | /* VM-entry instruction length */ |
| 2808 | switch (intr_type) { |
| 2809 | case INTR_TYPE_SOFT_EXCEPTION: |
| 2810 | case INTR_TYPE_SOFT_INTR: |
| 2811 | case INTR_TYPE_PRIV_SW_EXCEPTION: |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 2812 | if (CC(vmcs12->vm_entry_instruction_len > 15) || |
| 2813 | CC(vmcs12->vm_entry_instruction_len == 0 && |
| 2814 | CC(!nested_cpu_has_zero_length_injection(vcpu)))) |
Krish Sadhukhan | 5fbf963 | 2018-12-12 13:30:10 -0500 | [diff] [blame] | 2815 | return -EINVAL; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2816 | } |
| 2817 | } |
| 2818 | |
Krish Sadhukhan | 5fbf963 | 2018-12-12 13:30:10 -0500 | [diff] [blame] | 2819 | if (nested_vmx_check_entry_msr_switch_controls(vcpu, vmcs12)) |
| 2820 | return -EINVAL; |
| 2821 | |
| 2822 | return 0; |
| 2823 | } |
| 2824 | |
Sean Christopherson | 5478ba3 | 2019-04-11 12:18:06 -0700 | [diff] [blame] | 2825 | static int nested_vmx_check_controls(struct kvm_vcpu *vcpu, |
| 2826 | struct vmcs12 *vmcs12) |
| 2827 | { |
| 2828 | if (nested_check_vm_execution_controls(vcpu, vmcs12) || |
| 2829 | nested_check_vm_exit_controls(vcpu, vmcs12) || |
| 2830 | nested_check_vm_entry_controls(vcpu, vmcs12)) |
Paolo Bonzini | 98d9e85 | 2019-04-12 10:19:57 +0200 | [diff] [blame] | 2831 | return -EINVAL; |
Sean Christopherson | 5478ba3 | 2019-04-11 12:18:06 -0700 | [diff] [blame] | 2832 | |
Vitaly Kuznetsov | a835023 | 2020-02-05 13:30:34 +0100 | [diff] [blame] | 2833 | if (to_vmx(vcpu)->nested.enlightened_vmcs_enabled) |
| 2834 | return nested_evmcs_check_controls(vmcs12); |
| 2835 | |
Sean Christopherson | 5478ba3 | 2019-04-11 12:18:06 -0700 | [diff] [blame] | 2836 | return 0; |
| 2837 | } |
| 2838 | |
Maxim Levitsky | af957ee | 2021-11-15 15:18:36 +0200 | [diff] [blame] | 2839 | static int nested_vmx_check_address_space_size(struct kvm_vcpu *vcpu, |
| 2840 | struct vmcs12 *vmcs12) |
| 2841 | { |
| 2842 | #ifdef CONFIG_X86_64 |
| 2843 | if (CC(!!(vmcs12->vm_exit_controls & VM_EXIT_HOST_ADDR_SPACE_SIZE) != |
| 2844 | !!(vcpu->arch.efer & EFER_LMA))) |
| 2845 | return -EINVAL; |
| 2846 | #endif |
| 2847 | return 0; |
| 2848 | } |
| 2849 | |
Paolo Bonzini | 98d9e85 | 2019-04-12 10:19:57 +0200 | [diff] [blame] | 2850 | static int nested_vmx_check_host_state(struct kvm_vcpu *vcpu, |
| 2851 | struct vmcs12 *vmcs12) |
Krish Sadhukhan | 5fbf963 | 2018-12-12 13:30:10 -0500 | [diff] [blame] | 2852 | { |
| 2853 | bool ia32e; |
| 2854 | |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 2855 | if (CC(!nested_host_cr0_valid(vcpu, vmcs12->host_cr0)) || |
| 2856 | CC(!nested_host_cr4_valid(vcpu, vmcs12->host_cr4)) || |
Sean Christopherson | 636e8b7 | 2021-02-03 16:01:10 -0800 | [diff] [blame] | 2857 | CC(kvm_vcpu_is_illegal_gpa(vcpu, vmcs12->host_cr3))) |
Krish Sadhukhan | 254b2f3 | 2018-12-12 13:30:11 -0500 | [diff] [blame] | 2858 | return -EINVAL; |
Krish Sadhukhan | 711eff3 | 2019-02-07 14:05:30 -0500 | [diff] [blame] | 2859 | |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 2860 | if (CC(is_noncanonical_address(vmcs12->host_ia32_sysenter_esp, vcpu)) || |
| 2861 | CC(is_noncanonical_address(vmcs12->host_ia32_sysenter_eip, vcpu))) |
Krish Sadhukhan | 711eff3 | 2019-02-07 14:05:30 -0500 | [diff] [blame] | 2862 | return -EINVAL; |
| 2863 | |
Krish Sadhukhan | f6b0db1f | 2019-04-08 17:35:11 -0400 | [diff] [blame] | 2864 | if ((vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PAT) && |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 2865 | CC(!kvm_pat_valid(vmcs12->host_ia32_pat))) |
Krish Sadhukhan | f6b0db1f | 2019-04-08 17:35:11 -0400 | [diff] [blame] | 2866 | return -EINVAL; |
| 2867 | |
Oliver Upton | c547cb6 | 2019-11-13 16:17:17 -0800 | [diff] [blame] | 2868 | if ((vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL) && |
| 2869 | CC(!kvm_valid_perf_global_ctrl(vcpu_to_pmu(vcpu), |
| 2870 | vmcs12->host_ia32_perf_global_ctrl))) |
| 2871 | return -EINVAL; |
| 2872 | |
Paolo Bonzini | fd3edd4 | 2019-09-25 18:33:53 +0200 | [diff] [blame] | 2873 | #ifdef CONFIG_X86_64 |
Maxim Levitsky | af957ee | 2021-11-15 15:18:36 +0200 | [diff] [blame] | 2874 | ia32e = !!(vmcs12->vm_exit_controls & VM_EXIT_HOST_ADDR_SPACE_SIZE); |
Paolo Bonzini | fd3edd4 | 2019-09-25 18:33:53 +0200 | [diff] [blame] | 2875 | #else |
| 2876 | ia32e = false; |
| 2877 | #endif |
| 2878 | |
| 2879 | if (ia32e) { |
Maxim Levitsky | af957ee | 2021-11-15 15:18:36 +0200 | [diff] [blame] | 2880 | if (CC(!(vmcs12->host_cr4 & X86_CR4_PAE))) |
Paolo Bonzini | fd3edd4 | 2019-09-25 18:33:53 +0200 | [diff] [blame] | 2881 | return -EINVAL; |
| 2882 | } else { |
Maxim Levitsky | af957ee | 2021-11-15 15:18:36 +0200 | [diff] [blame] | 2883 | if (CC(vmcs12->vm_entry_controls & VM_ENTRY_IA32E_MODE) || |
Paolo Bonzini | fd3edd4 | 2019-09-25 18:33:53 +0200 | [diff] [blame] | 2884 | CC(vmcs12->host_cr4 & X86_CR4_PCIDE) || |
| 2885 | CC((vmcs12->host_rip) >> 32)) |
| 2886 | return -EINVAL; |
| 2887 | } |
Krish Sadhukhan | 1ef23e1 | 2019-07-03 19:54:35 -0400 | [diff] [blame] | 2888 | |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 2889 | if (CC(vmcs12->host_cs_selector & (SEGMENT_RPL_MASK | SEGMENT_TI_MASK)) || |
| 2890 | CC(vmcs12->host_ss_selector & (SEGMENT_RPL_MASK | SEGMENT_TI_MASK)) || |
| 2891 | CC(vmcs12->host_ds_selector & (SEGMENT_RPL_MASK | SEGMENT_TI_MASK)) || |
| 2892 | CC(vmcs12->host_es_selector & (SEGMENT_RPL_MASK | SEGMENT_TI_MASK)) || |
| 2893 | CC(vmcs12->host_fs_selector & (SEGMENT_RPL_MASK | SEGMENT_TI_MASK)) || |
| 2894 | CC(vmcs12->host_gs_selector & (SEGMENT_RPL_MASK | SEGMENT_TI_MASK)) || |
| 2895 | CC(vmcs12->host_tr_selector & (SEGMENT_RPL_MASK | SEGMENT_TI_MASK)) || |
| 2896 | CC(vmcs12->host_cs_selector == 0) || |
| 2897 | CC(vmcs12->host_tr_selector == 0) || |
| 2898 | CC(vmcs12->host_ss_selector == 0 && !ia32e)) |
Krish Sadhukhan | 1ef23e1 | 2019-07-03 19:54:35 -0400 | [diff] [blame] | 2899 | return -EINVAL; |
| 2900 | |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 2901 | if (CC(is_noncanonical_address(vmcs12->host_fs_base, vcpu)) || |
| 2902 | CC(is_noncanonical_address(vmcs12->host_gs_base, vcpu)) || |
| 2903 | CC(is_noncanonical_address(vmcs12->host_gdtr_base, vcpu)) || |
| 2904 | CC(is_noncanonical_address(vmcs12->host_idtr_base, vcpu)) || |
Paolo Bonzini | fd3edd4 | 2019-09-25 18:33:53 +0200 | [diff] [blame] | 2905 | CC(is_noncanonical_address(vmcs12->host_tr_base, vcpu)) || |
| 2906 | CC(is_noncanonical_address(vmcs12->host_rip, vcpu))) |
Krish Sadhukhan | 5845038 | 2019-08-09 12:26:19 -0700 | [diff] [blame] | 2907 | return -EINVAL; |
Krish Sadhukhan | 1ef23e1 | 2019-07-03 19:54:35 -0400 | [diff] [blame] | 2908 | |
Krish Sadhukhan | 5fbf963 | 2018-12-12 13:30:10 -0500 | [diff] [blame] | 2909 | /* |
| 2910 | * If the load IA32_EFER VM-exit control is 1, bits reserved in the |
| 2911 | * IA32_EFER MSR must be 0 in the field for that register. In addition, |
| 2912 | * the values of the LMA and LME bits in the field must each be that of |
| 2913 | * the host address-space size VM-exit control. |
| 2914 | */ |
| 2915 | if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_EFER) { |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 2916 | if (CC(!kvm_valid_efer(vcpu, vmcs12->host_ia32_efer)) || |
| 2917 | CC(ia32e != !!(vmcs12->host_ia32_efer & EFER_LMA)) || |
| 2918 | CC(ia32e != !!(vmcs12->host_ia32_efer & EFER_LME))) |
Krish Sadhukhan | 254b2f3 | 2018-12-12 13:30:11 -0500 | [diff] [blame] | 2919 | return -EINVAL; |
Krish Sadhukhan | 5fbf963 | 2018-12-12 13:30:10 -0500 | [diff] [blame] | 2920 | } |
| 2921 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2922 | return 0; |
| 2923 | } |
| 2924 | |
| 2925 | static int nested_vmx_check_vmcs_link_ptr(struct kvm_vcpu *vcpu, |
| 2926 | struct vmcs12 *vmcs12) |
| 2927 | { |
David Woodhouse | 7d0172b | 2021-11-15 16:50:25 +0000 | [diff] [blame] | 2928 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 2929 | struct gfn_to_hva_cache *ghc = &vmx->nested.shadow_vmcs12_cache; |
| 2930 | struct vmcs_hdr hdr; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2931 | |
Yu Zhang | 64c7850 | 2021-09-30 01:51:53 +0800 | [diff] [blame] | 2932 | if (vmcs12->vmcs_link_pointer == INVALID_GPA) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2933 | return 0; |
| 2934 | |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 2935 | if (CC(!page_address_valid(vcpu, vmcs12->vmcs_link_pointer))) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2936 | return -EINVAL; |
| 2937 | |
David Woodhouse | 7d0172b | 2021-11-15 16:50:25 +0000 | [diff] [blame] | 2938 | if (ghc->gpa != vmcs12->vmcs_link_pointer && |
| 2939 | CC(kvm_gfn_to_hva_cache_init(vcpu->kvm, ghc, |
| 2940 | vmcs12->vmcs_link_pointer, VMCS12_SIZE))) |
| 2941 | return -EINVAL; |
| 2942 | |
| 2943 | if (CC(kvm_read_guest_offset_cached(vcpu->kvm, ghc, &hdr, |
| 2944 | offsetof(struct vmcs12, hdr), |
| 2945 | sizeof(hdr)))) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2946 | return -EINVAL; |
| 2947 | |
David Woodhouse | 7d0172b | 2021-11-15 16:50:25 +0000 | [diff] [blame] | 2948 | if (CC(hdr.revision_id != VMCS12_REVISION) || |
| 2949 | CC(hdr.shadow_vmcs != nested_cpu_has_shadow_vmcs(vmcs12))) |
| 2950 | return -EINVAL; |
KarimAllah Ahmed | 8892530 | 2019-01-31 21:24:41 +0100 | [diff] [blame] | 2951 | |
David Woodhouse | 7d0172b | 2021-11-15 16:50:25 +0000 | [diff] [blame] | 2952 | return 0; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2953 | } |
| 2954 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2955 | /* |
| 2956 | * Checks related to Guest Non-register State |
| 2957 | */ |
| 2958 | static int nested_check_guest_non_reg_state(struct vmcs12 *vmcs12) |
| 2959 | { |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 2960 | if (CC(vmcs12->guest_activity_state != GUEST_ACTIVITY_ACTIVE && |
Yadong Qi | bf0cd88 | 2020-11-06 14:51:22 +0800 | [diff] [blame] | 2961 | vmcs12->guest_activity_state != GUEST_ACTIVITY_HLT && |
| 2962 | vmcs12->guest_activity_state != GUEST_ACTIVITY_WAIT_SIPI)) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2963 | return -EINVAL; |
| 2964 | |
| 2965 | return 0; |
| 2966 | } |
| 2967 | |
Sean Christopherson | 5478ba3 | 2019-04-11 12:18:06 -0700 | [diff] [blame] | 2968 | static int nested_vmx_check_guest_state(struct kvm_vcpu *vcpu, |
| 2969 | struct vmcs12 *vmcs12, |
Sean Christopherson | 68cda40 | 2020-05-11 15:05:29 -0700 | [diff] [blame] | 2970 | enum vm_entry_failure_code *entry_failure_code) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2971 | { |
| 2972 | bool ia32e; |
| 2973 | |
Sean Christopherson | 68cda40 | 2020-05-11 15:05:29 -0700 | [diff] [blame] | 2974 | *entry_failure_code = ENTRY_FAIL_DEFAULT; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2975 | |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 2976 | if (CC(!nested_guest_cr0_valid(vcpu, vmcs12->guest_cr0)) || |
| 2977 | CC(!nested_guest_cr4_valid(vcpu, vmcs12->guest_cr4))) |
Sean Christopherson | c80add0 | 2019-04-11 12:18:09 -0700 | [diff] [blame] | 2978 | return -EINVAL; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2979 | |
Krish Sadhukhan | b91991b | 2020-01-15 19:54:32 -0500 | [diff] [blame] | 2980 | if ((vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS) && |
| 2981 | CC(!kvm_dr7_valid(vmcs12->guest_dr7))) |
| 2982 | return -EINVAL; |
| 2983 | |
Krish Sadhukhan | de2bc2b | 2019-04-08 17:35:12 -0400 | [diff] [blame] | 2984 | if ((vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_PAT) && |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 2985 | CC(!kvm_pat_valid(vmcs12->guest_ia32_pat))) |
Sean Christopherson | c80add0 | 2019-04-11 12:18:09 -0700 | [diff] [blame] | 2986 | return -EINVAL; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2987 | |
| 2988 | if (nested_vmx_check_vmcs_link_ptr(vcpu, vmcs12)) { |
Sean Christopherson | 68cda40 | 2020-05-11 15:05:29 -0700 | [diff] [blame] | 2989 | *entry_failure_code = ENTRY_FAIL_VMCS_LINK_PTR; |
Sean Christopherson | c80add0 | 2019-04-11 12:18:09 -0700 | [diff] [blame] | 2990 | return -EINVAL; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2991 | } |
| 2992 | |
Oliver Upton | bfc6ad6 | 2019-11-13 16:17:16 -0800 | [diff] [blame] | 2993 | if ((vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL) && |
| 2994 | CC(!kvm_valid_perf_global_ctrl(vcpu_to_pmu(vcpu), |
| 2995 | vmcs12->guest_ia32_perf_global_ctrl))) |
| 2996 | return -EINVAL; |
| 2997 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2998 | /* |
| 2999 | * If the load IA32_EFER VM-entry control is 1, the following checks |
| 3000 | * are performed on the field for the IA32_EFER MSR: |
| 3001 | * - Bits reserved in the IA32_EFER MSR must be 0. |
| 3002 | * - Bit 10 (corresponding to IA32_EFER.LMA) must equal the value of |
| 3003 | * the IA-32e mode guest VM-exit control. It must also be identical |
| 3004 | * to bit 8 (LME) if bit 31 in the CR0 field (corresponding to |
| 3005 | * CR0.PG) is 1. |
| 3006 | */ |
| 3007 | if (to_vmx(vcpu)->nested.nested_run_pending && |
| 3008 | (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_EFER)) { |
| 3009 | ia32e = (vmcs12->vm_entry_controls & VM_ENTRY_IA32E_MODE) != 0; |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 3010 | if (CC(!kvm_valid_efer(vcpu, vmcs12->guest_ia32_efer)) || |
| 3011 | CC(ia32e != !!(vmcs12->guest_ia32_efer & EFER_LMA)) || |
| 3012 | CC(((vmcs12->guest_cr0 & X86_CR0_PG) && |
| 3013 | ia32e != !!(vmcs12->guest_ia32_efer & EFER_LME)))) |
Sean Christopherson | c80add0 | 2019-04-11 12:18:09 -0700 | [diff] [blame] | 3014 | return -EINVAL; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3015 | } |
| 3016 | |
| 3017 | if ((vmcs12->vm_entry_controls & VM_ENTRY_LOAD_BNDCFGS) && |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 3018 | (CC(is_noncanonical_address(vmcs12->guest_bndcfgs & PAGE_MASK, vcpu)) || |
| 3019 | CC((vmcs12->guest_bndcfgs & MSR_IA32_BNDCFGS_RSVD)))) |
Sean Christopherson | c80add0 | 2019-04-11 12:18:09 -0700 | [diff] [blame] | 3020 | return -EINVAL; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3021 | |
Sean Christopherson | 9c3e922 | 2019-04-11 12:18:05 -0700 | [diff] [blame] | 3022 | if (nested_check_guest_non_reg_state(vmcs12)) |
Sean Christopherson | c80add0 | 2019-04-11 12:18:09 -0700 | [diff] [blame] | 3023 | return -EINVAL; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3024 | |
| 3025 | return 0; |
| 3026 | } |
| 3027 | |
Sean Christopherson | 453eafb | 2018-12-20 12:25:17 -0800 | [diff] [blame] | 3028 | static int nested_vmx_check_vmentry_hw(struct kvm_vcpu *vcpu) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3029 | { |
| 3030 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 3031 | unsigned long cr3, cr4; |
Sean Christopherson | f1727b4 | 2019-01-25 07:40:58 -0800 | [diff] [blame] | 3032 | bool vm_fail; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3033 | |
| 3034 | if (!nested_early_check) |
| 3035 | return 0; |
| 3036 | |
| 3037 | if (vmx->msr_autoload.host.nr) |
| 3038 | vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, 0); |
| 3039 | if (vmx->msr_autoload.guest.nr) |
| 3040 | vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, 0); |
| 3041 | |
| 3042 | preempt_disable(); |
| 3043 | |
| 3044 | vmx_prepare_switch_to_guest(vcpu); |
| 3045 | |
| 3046 | /* |
| 3047 | * Induce a consistency check VMExit by clearing bit 1 in GUEST_RFLAGS, |
| 3048 | * which is reserved to '1' by hardware. GUEST_RFLAGS is guaranteed to |
Miaohe Lin | 49f933d | 2020-02-27 11:20:54 +0800 | [diff] [blame] | 3049 | * be written (by prepare_vmcs02()) before the "real" VMEnter, i.e. |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3050 | * there is no need to preserve other bits or save/restore the field. |
| 3051 | */ |
| 3052 | vmcs_writel(GUEST_RFLAGS, 0); |
| 3053 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3054 | cr3 = __get_current_cr3_fast(); |
| 3055 | if (unlikely(cr3 != vmx->loaded_vmcs->host_state.cr3)) { |
| 3056 | vmcs_writel(HOST_CR3, cr3); |
| 3057 | vmx->loaded_vmcs->host_state.cr3 = cr3; |
| 3058 | } |
| 3059 | |
| 3060 | cr4 = cr4_read_shadow(); |
| 3061 | if (unlikely(cr4 != vmx->loaded_vmcs->host_state.cr4)) { |
| 3062 | vmcs_writel(HOST_CR4, cr4); |
| 3063 | vmx->loaded_vmcs->host_state.cr4 = cr4; |
| 3064 | } |
| 3065 | |
Uros Bizjak | 150f17b | 2020-12-30 16:26:57 -0800 | [diff] [blame] | 3066 | vm_fail = __vmx_vcpu_run(vmx, (unsigned long *)&vcpu->arch.regs, |
| 3067 | vmx->loaded_vmcs->launched); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3068 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3069 | if (vmx->msr_autoload.host.nr) |
| 3070 | vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, vmx->msr_autoload.host.nr); |
| 3071 | if (vmx->msr_autoload.guest.nr) |
| 3072 | vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, vmx->msr_autoload.guest.nr); |
| 3073 | |
Sean Christopherson | f1727b4 | 2019-01-25 07:40:58 -0800 | [diff] [blame] | 3074 | if (vm_fail) { |
Sean Christopherson | 380e005 | 2019-07-11 08:58:30 -0700 | [diff] [blame] | 3075 | u32 error = vmcs_read32(VM_INSTRUCTION_ERROR); |
| 3076 | |
Wanpeng Li | 541e886 | 2019-05-17 16:49:50 +0800 | [diff] [blame] | 3077 | preempt_enable(); |
Sean Christopherson | 380e005 | 2019-07-11 08:58:30 -0700 | [diff] [blame] | 3078 | |
| 3079 | trace_kvm_nested_vmenter_failed( |
| 3080 | "early hardware check VM-instruction error: ", error); |
| 3081 | WARN_ON_ONCE(error != VMXERR_ENTRY_INVALID_CONTROL_FIELD); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3082 | return 1; |
| 3083 | } |
| 3084 | |
| 3085 | /* |
| 3086 | * VMExit clears RFLAGS.IF and DR7, even on a consistency check. |
| 3087 | */ |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3088 | if (hw_breakpoint_active()) |
| 3089 | set_debugreg(__this_cpu_read(cpu_dr7), 7); |
Peter Zijlstra | 84b6a34 | 2020-05-29 23:27:36 +0200 | [diff] [blame] | 3090 | local_irq_enable(); |
Wanpeng Li | 541e886 | 2019-05-17 16:49:50 +0800 | [diff] [blame] | 3091 | preempt_enable(); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3092 | |
| 3093 | /* |
| 3094 | * A non-failing VMEntry means we somehow entered guest mode with |
| 3095 | * an illegal RIP, and that's just the tip of the iceberg. There |
| 3096 | * is no telling what memory has been modified or what state has |
| 3097 | * been exposed to unknown code. Hitting this all but guarantees |
| 3098 | * a (very critical) hardware issue. |
| 3099 | */ |
| 3100 | WARN_ON(!(vmcs_read32(VM_EXIT_REASON) & |
| 3101 | VMX_EXIT_REASONS_FAILED_VMENTRY)); |
| 3102 | |
| 3103 | return 0; |
| 3104 | } |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3105 | |
Paolo Bonzini | 9a78e15 | 2021-01-08 11:43:08 -0500 | [diff] [blame] | 3106 | static bool nested_get_evmcs_page(struct kvm_vcpu *vcpu) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3107 | { |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3108 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3109 | |
Vitaly Kuznetsov | e942dbf | 2020-03-09 16:52:12 +0100 | [diff] [blame] | 3110 | /* |
| 3111 | * hv_evmcs may end up being not mapped after migration (when |
| 3112 | * L2 was running), map it here to make sure vmcs12 changes are |
| 3113 | * properly reflected. |
| 3114 | */ |
Vitaly Kuznetsov | 1e9dfbd | 2021-05-26 15:20:16 +0200 | [diff] [blame] | 3115 | if (vmx->nested.enlightened_vmcs_enabled && |
Vitaly Kuznetsov | 2784996 | 2021-05-26 15:20:20 +0200 | [diff] [blame] | 3116 | vmx->nested.hv_evmcs_vmptr == EVMPTR_MAP_PENDING) { |
Vitaly Kuznetsov | b6a0653 | 2020-03-09 16:52:13 +0100 | [diff] [blame] | 3117 | enum nested_evmptrld_status evmptrld_status = |
| 3118 | nested_vmx_handle_enlightened_vmptrld(vcpu, false); |
| 3119 | |
| 3120 | if (evmptrld_status == EVMPTRLD_VMFAIL || |
Vitaly Kuznetsov | f5c7e84 | 2021-05-03 17:08:51 +0200 | [diff] [blame] | 3121 | evmptrld_status == EVMPTRLD_ERROR) |
Vitaly Kuznetsov | b6a0653 | 2020-03-09 16:52:13 +0100 | [diff] [blame] | 3122 | return false; |
Vitaly Kuznetsov | 8629b62 | 2021-05-26 15:20:25 +0200 | [diff] [blame] | 3123 | |
| 3124 | /* |
| 3125 | * Post migration VMCS12 always provides the most actual |
| 3126 | * information, copy it to eVMCS upon entry. |
| 3127 | */ |
| 3128 | vmx->nested.need_vmcs12_to_shadow_sync = true; |
Vitaly Kuznetsov | b6a0653 | 2020-03-09 16:52:13 +0100 | [diff] [blame] | 3129 | } |
Vitaly Kuznetsov | e942dbf | 2020-03-09 16:52:12 +0100 | [diff] [blame] | 3130 | |
Paolo Bonzini | 9a78e15 | 2021-01-08 11:43:08 -0500 | [diff] [blame] | 3131 | return true; |
| 3132 | } |
| 3133 | |
| 3134 | static bool nested_get_vmcs12_pages(struct kvm_vcpu *vcpu) |
| 3135 | { |
| 3136 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
| 3137 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 3138 | struct kvm_host_map *map; |
| 3139 | struct page *page; |
| 3140 | u64 hpa; |
| 3141 | |
Maxim Levitsky | 158a48e | 2021-06-07 12:02:03 +0300 | [diff] [blame] | 3142 | if (!vcpu->arch.pdptrs_from_userspace && |
| 3143 | !nested_cpu_has_ept(vmcs12) && is_pae_paging(vcpu)) { |
Maxim Levitsky | 0f85722 | 2021-06-07 12:02:00 +0300 | [diff] [blame] | 3144 | /* |
| 3145 | * Reload the guest's PDPTRs since after a migration |
| 3146 | * the guest CR3 might be restored prior to setting the nested |
| 3147 | * state which can lead to a load of wrong PDPTRs. |
| 3148 | */ |
| 3149 | if (CC(!load_pdptrs(vcpu, vcpu->arch.walk_mmu, vcpu->arch.cr3))) |
| 3150 | return false; |
| 3151 | } |
| 3152 | |
| 3153 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3154 | if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) { |
| 3155 | /* |
| 3156 | * Translate L1 physical address to host physical |
| 3157 | * address for vmcs02. Keep the page pinned, so this |
| 3158 | * physical address remains valid. We keep a reference |
| 3159 | * to it so we can release it later. |
| 3160 | */ |
| 3161 | if (vmx->nested.apic_access_page) { /* shouldn't happen */ |
Liran Alon | b11494b | 2019-11-21 00:31:47 +0200 | [diff] [blame] | 3162 | kvm_release_page_clean(vmx->nested.apic_access_page); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3163 | vmx->nested.apic_access_page = NULL; |
| 3164 | } |
| 3165 | page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->apic_access_addr); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3166 | if (!is_error_page(page)) { |
| 3167 | vmx->nested.apic_access_page = page; |
| 3168 | hpa = page_to_phys(vmx->nested.apic_access_page); |
| 3169 | vmcs_write64(APIC_ACCESS_ADDR, hpa); |
| 3170 | } else { |
Jim Mattson | 671ddc7 | 2019-10-15 10:44:05 -0700 | [diff] [blame] | 3171 | pr_debug_ratelimited("%s: no backing 'struct page' for APIC-access address in vmcs12\n", |
| 3172 | __func__); |
| 3173 | vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR; |
| 3174 | vcpu->run->internal.suberror = |
| 3175 | KVM_INTERNAL_ERROR_EMULATION; |
| 3176 | vcpu->run->internal.ndata = 0; |
| 3177 | return false; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3178 | } |
| 3179 | } |
| 3180 | |
| 3181 | if (nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW)) { |
KarimAllah Ahmed | 96c66e8 | 2019-01-31 21:24:37 +0100 | [diff] [blame] | 3182 | map = &vmx->nested.virtual_apic_map; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3183 | |
KarimAllah Ahmed | 96c66e8 | 2019-01-31 21:24:37 +0100 | [diff] [blame] | 3184 | if (!kvm_vcpu_map(vcpu, gpa_to_gfn(vmcs12->virtual_apic_page_addr), map)) { |
| 3185 | vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, pfn_to_hpa(map->pfn)); |
Paolo Bonzini | 6909081 | 2019-04-15 15:16:17 +0200 | [diff] [blame] | 3186 | } else if (nested_cpu_has(vmcs12, CPU_BASED_CR8_LOAD_EXITING) && |
| 3187 | nested_cpu_has(vmcs12, CPU_BASED_CR8_STORE_EXITING) && |
| 3188 | !nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) { |
| 3189 | /* |
| 3190 | * The processor will never use the TPR shadow, simply |
| 3191 | * clear the bit from the execution control. Such a |
| 3192 | * configuration is useless, but it happens in tests. |
| 3193 | * For any other configuration, failing the vm entry is |
| 3194 | * _not_ what the processor does but it's basically the |
| 3195 | * only possibility we have. |
| 3196 | */ |
Sean Christopherson | 2183f56 | 2019-05-07 12:17:56 -0700 | [diff] [blame] | 3197 | exec_controls_clearbit(vmx, CPU_BASED_TPR_SHADOW); |
Paolo Bonzini | 6909081 | 2019-04-15 15:16:17 +0200 | [diff] [blame] | 3198 | } else { |
Sean Christopherson | ca2f546 | 2019-05-07 09:06:33 -0700 | [diff] [blame] | 3199 | /* |
| 3200 | * Write an illegal value to VIRTUAL_APIC_PAGE_ADDR to |
| 3201 | * force VM-Entry to fail. |
| 3202 | */ |
Yu Zhang | 64c7850 | 2021-09-30 01:51:53 +0800 | [diff] [blame] | 3203 | vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, INVALID_GPA); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3204 | } |
| 3205 | } |
| 3206 | |
| 3207 | if (nested_cpu_has_posted_intr(vmcs12)) { |
KarimAllah Ahmed | 3278e04 | 2019-01-31 21:24:38 +0100 | [diff] [blame] | 3208 | map = &vmx->nested.pi_desc_map; |
| 3209 | |
| 3210 | if (!kvm_vcpu_map(vcpu, gpa_to_gfn(vmcs12->posted_intr_desc_addr), map)) { |
| 3211 | vmx->nested.pi_desc = |
| 3212 | (struct pi_desc *)(((void *)map->hva) + |
| 3213 | offset_in_page(vmcs12->posted_intr_desc_addr)); |
| 3214 | vmcs_write64(POSTED_INTR_DESC_ADDR, |
| 3215 | 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] | 3216 | } else { |
| 3217 | /* |
| 3218 | * Defer the KVM_INTERNAL_EXIT until KVM tries to |
| 3219 | * access the contents of the VMCS12 posted interrupt |
| 3220 | * descriptor. (Note that KVM may do this when it |
| 3221 | * should not, per the architectural specification.) |
| 3222 | */ |
| 3223 | vmx->nested.pi_desc = NULL; |
| 3224 | pin_controls_clearbit(vmx, PIN_BASED_POSTED_INTR); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3225 | } |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3226 | } |
| 3227 | if (nested_vmx_prepare_msr_bitmap(vcpu, vmcs12)) |
Sean Christopherson | 2183f56 | 2019-05-07 12:17:56 -0700 | [diff] [blame] | 3228 | exec_controls_setbit(vmx, CPU_BASED_USE_MSR_BITMAPS); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3229 | else |
Sean Christopherson | 2183f56 | 2019-05-07 12:17:56 -0700 | [diff] [blame] | 3230 | exec_controls_clearbit(vmx, CPU_BASED_USE_MSR_BITMAPS); |
Paolo Bonzini | 9a78e15 | 2021-01-08 11:43:08 -0500 | [diff] [blame] | 3231 | |
| 3232 | return true; |
| 3233 | } |
| 3234 | |
| 3235 | static bool vmx_get_nested_state_pages(struct kvm_vcpu *vcpu) |
| 3236 | { |
Vitaly Kuznetsov | f5c7e84 | 2021-05-03 17:08:51 +0200 | [diff] [blame] | 3237 | if (!nested_get_evmcs_page(vcpu)) { |
| 3238 | pr_debug_ratelimited("%s: enlightened vmptrld failed\n", |
| 3239 | __func__); |
| 3240 | vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR; |
| 3241 | vcpu->run->internal.suberror = |
| 3242 | KVM_INTERNAL_ERROR_EMULATION; |
| 3243 | vcpu->run->internal.ndata = 0; |
| 3244 | |
Paolo Bonzini | 9a78e15 | 2021-01-08 11:43:08 -0500 | [diff] [blame] | 3245 | return false; |
Vitaly Kuznetsov | f5c7e84 | 2021-05-03 17:08:51 +0200 | [diff] [blame] | 3246 | } |
Paolo Bonzini | 9a78e15 | 2021-01-08 11:43:08 -0500 | [diff] [blame] | 3247 | |
| 3248 | if (is_guest_mode(vcpu) && !nested_get_vmcs12_pages(vcpu)) |
| 3249 | return false; |
| 3250 | |
Jim Mattson | 671ddc7 | 2019-10-15 10:44:05 -0700 | [diff] [blame] | 3251 | return true; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3252 | } |
| 3253 | |
Sean Christopherson | 02f5fb2 | 2020-06-22 14:58:32 -0700 | [diff] [blame] | 3254 | static int nested_vmx_write_pml_buffer(struct kvm_vcpu *vcpu, gpa_t gpa) |
| 3255 | { |
| 3256 | struct vmcs12 *vmcs12; |
| 3257 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 3258 | gpa_t dst; |
| 3259 | |
| 3260 | if (WARN_ON_ONCE(!is_guest_mode(vcpu))) |
| 3261 | return 0; |
| 3262 | |
| 3263 | if (WARN_ON_ONCE(vmx->nested.pml_full)) |
| 3264 | return 1; |
| 3265 | |
| 3266 | /* |
| 3267 | * Check if PML is enabled for the nested guest. Whether eptp bit 6 is |
| 3268 | * set is already checked as part of A/D emulation. |
| 3269 | */ |
| 3270 | vmcs12 = get_vmcs12(vcpu); |
| 3271 | if (!nested_cpu_has_pml(vmcs12)) |
| 3272 | return 0; |
| 3273 | |
| 3274 | if (vmcs12->guest_pml_index >= PML_ENTITY_NUM) { |
| 3275 | vmx->nested.pml_full = true; |
| 3276 | return 1; |
| 3277 | } |
| 3278 | |
| 3279 | gpa &= ~0xFFFull; |
| 3280 | dst = vmcs12->pml_address + sizeof(u64) * vmcs12->guest_pml_index; |
| 3281 | |
| 3282 | if (kvm_write_guest_page(vcpu->kvm, gpa_to_gfn(dst), &gpa, |
| 3283 | offset_in_page(dst), sizeof(gpa))) |
| 3284 | return 0; |
| 3285 | |
| 3286 | vmcs12->guest_pml_index--; |
| 3287 | |
| 3288 | return 0; |
| 3289 | } |
| 3290 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3291 | /* |
| 3292 | * Intel's VMX Instruction Reference specifies a common set of prerequisites |
| 3293 | * for running VMX instructions (except VMXON, whose prerequisites are |
| 3294 | * slightly different). It also specifies what exception to inject otherwise. |
| 3295 | * Note that many of these exceptions have priority over VM exits, so they |
| 3296 | * don't have to be checked again here. |
| 3297 | */ |
| 3298 | static int nested_vmx_check_permission(struct kvm_vcpu *vcpu) |
| 3299 | { |
| 3300 | if (!to_vmx(vcpu)->nested.vmxon) { |
| 3301 | kvm_queue_exception(vcpu, UD_VECTOR); |
| 3302 | return 0; |
| 3303 | } |
| 3304 | |
| 3305 | if (vmx_get_cpl(vcpu)) { |
| 3306 | kvm_inject_gp(vcpu, 0); |
| 3307 | return 0; |
| 3308 | } |
| 3309 | |
| 3310 | return 1; |
| 3311 | } |
| 3312 | |
| 3313 | static u8 vmx_has_apicv_interrupt(struct kvm_vcpu *vcpu) |
| 3314 | { |
| 3315 | u8 rvi = vmx_get_rvi(); |
| 3316 | u8 vppr = kvm_lapic_get_reg(vcpu->arch.apic, APIC_PROCPRI); |
| 3317 | |
| 3318 | return ((rvi & 0xf0) > (vppr & 0xf0)); |
| 3319 | } |
| 3320 | |
| 3321 | static void load_vmcs12_host_state(struct kvm_vcpu *vcpu, |
| 3322 | struct vmcs12 *vmcs12); |
| 3323 | |
| 3324 | /* |
| 3325 | * If from_vmentry is false, this is being called from state restore (either RSM |
| 3326 | * or KVM_SET_NESTED_STATE). Otherwise it's called from vmlaunch/vmresume. |
Jim Mattson | 671ddc7 | 2019-10-15 10:44:05 -0700 | [diff] [blame] | 3327 | * |
| 3328 | * Returns: |
Miaohe Lin | 463bfee | 2020-02-14 10:44:05 +0800 | [diff] [blame] | 3329 | * NVMX_VMENTRY_SUCCESS: Entered VMX non-root mode |
| 3330 | * NVMX_VMENTRY_VMFAIL: Consistency check VMFail |
| 3331 | * NVMX_VMENTRY_VMEXIT: Consistency check VMExit |
| 3332 | * NVMX_VMENTRY_KVM_INTERNAL_ERROR: KVM internal error |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3333 | */ |
Jim Mattson | 671ddc7 | 2019-10-15 10:44:05 -0700 | [diff] [blame] | 3334 | enum nvmx_vmentry_status nested_vmx_enter_non_root_mode(struct kvm_vcpu *vcpu, |
| 3335 | bool from_vmentry) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3336 | { |
| 3337 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 3338 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
Sean Christopherson | 68cda40 | 2020-05-11 15:05:29 -0700 | [diff] [blame] | 3339 | enum vm_entry_failure_code entry_failure_code; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3340 | bool evaluate_pending_interrupts; |
Sean Christopherson | 8e53324 | 2020-11-06 17:03:12 +0800 | [diff] [blame] | 3341 | union vmx_exit_reason exit_reason = { |
| 3342 | .basic = EXIT_REASON_INVALID_STATE, |
| 3343 | .failed_vmentry = 1, |
| 3344 | }; |
| 3345 | u32 failed_index; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3346 | |
Sean Christopherson | eeeb4f6 | 2020-03-20 14:28:20 -0700 | [diff] [blame] | 3347 | if (kvm_check_request(KVM_REQ_TLB_FLUSH_CURRENT, vcpu)) |
| 3348 | kvm_vcpu_flush_tlb_current(vcpu); |
| 3349 | |
Sean Christopherson | 2183f56 | 2019-05-07 12:17:56 -0700 | [diff] [blame] | 3350 | evaluate_pending_interrupts = exec_controls_get(vmx) & |
Xiaoyao Li | 4e2a0bc | 2019-12-06 16:45:25 +0800 | [diff] [blame] | 3351 | (CPU_BASED_INTR_WINDOW_EXITING | CPU_BASED_NMI_WINDOW_EXITING); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3352 | if (likely(!evaluate_pending_interrupts) && kvm_vcpu_apicv_active(vcpu)) |
| 3353 | evaluate_pending_interrupts |= vmx_has_apicv_interrupt(vcpu); |
| 3354 | |
| 3355 | if (!(vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS)) |
| 3356 | vmx->nested.vmcs01_debugctl = vmcs_read64(GUEST_IA32_DEBUGCTL); |
| 3357 | if (kvm_mpx_supported() && |
| 3358 | !(vmcs12->vm_entry_controls & VM_ENTRY_LOAD_BNDCFGS)) |
| 3359 | vmx->nested.vmcs01_guest_bndcfgs = vmcs_read64(GUEST_BNDCFGS); |
| 3360 | |
Sean Christopherson | f087a02 | 2019-06-07 11:55:34 -0700 | [diff] [blame] | 3361 | /* |
| 3362 | * Overwrite vmcs01.GUEST_CR3 with L1's CR3 if EPT is disabled *and* |
| 3363 | * nested early checks are disabled. In the event of a "late" VM-Fail, |
| 3364 | * i.e. a VM-Fail detected by hardware but not KVM, KVM must unwind its |
| 3365 | * software model to the pre-VMEntry host state. When EPT is disabled, |
| 3366 | * GUEST_CR3 holds KVM's shadow CR3, not L1's "real" CR3, which causes |
| 3367 | * nested_vmx_restore_host_state() to corrupt vcpu->arch.cr3. Stuffing |
| 3368 | * vmcs01.GUEST_CR3 results in the unwind naturally setting arch.cr3 to |
| 3369 | * the correct value. Smashing vmcs01.GUEST_CR3 is safe because nested |
| 3370 | * VM-Exits, and the unwind, reset KVM's MMU, i.e. vmcs01.GUEST_CR3 is |
| 3371 | * guaranteed to be overwritten with a shadow CR3 prior to re-entering |
| 3372 | * L1. Don't stuff vmcs01.GUEST_CR3 when using nested early checks as |
| 3373 | * KVM modifies vcpu->arch.cr3 if and only if the early hardware checks |
| 3374 | * pass, and early VM-Fails do not reset KVM's MMU, i.e. the VM-Fail |
| 3375 | * path would need to manually save/restore vmcs01.GUEST_CR3. |
| 3376 | */ |
| 3377 | if (!enable_ept && !nested_early_check) |
| 3378 | vmcs_writel(GUEST_CR3, vcpu->arch.cr3); |
| 3379 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3380 | vmx_switch_vmcs(vcpu, &vmx->nested.vmcs02); |
| 3381 | |
Sean Christopherson | 389ab25 | 2021-08-10 10:19:50 -0700 | [diff] [blame] | 3382 | prepare_vmcs02_early(vmx, &vmx->vmcs01, vmcs12); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3383 | |
| 3384 | if (from_vmentry) { |
Sean Christopherson | b89d5ad | 2020-09-23 11:44:47 -0700 | [diff] [blame] | 3385 | if (unlikely(!nested_get_vmcs12_pages(vcpu))) { |
| 3386 | vmx_switch_vmcs(vcpu, &vmx->vmcs01); |
Jim Mattson | 671ddc7 | 2019-10-15 10:44:05 -0700 | [diff] [blame] | 3387 | return NVMX_VMENTRY_KVM_INTERNAL_ERROR; |
Sean Christopherson | b89d5ad | 2020-09-23 11:44:47 -0700 | [diff] [blame] | 3388 | } |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3389 | |
| 3390 | if (nested_vmx_check_vmentry_hw(vcpu)) { |
| 3391 | vmx_switch_vmcs(vcpu, &vmx->vmcs01); |
Jim Mattson | 671ddc7 | 2019-10-15 10:44:05 -0700 | [diff] [blame] | 3392 | return NVMX_VMENTRY_VMFAIL; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3393 | } |
| 3394 | |
Sean Christopherson | 68cda40 | 2020-05-11 15:05:29 -0700 | [diff] [blame] | 3395 | if (nested_vmx_check_guest_state(vcpu, vmcs12, |
| 3396 | &entry_failure_code)) { |
Sean Christopherson | 8e53324 | 2020-11-06 17:03:12 +0800 | [diff] [blame] | 3397 | exit_reason.basic = EXIT_REASON_INVALID_STATE; |
Sean Christopherson | 68cda40 | 2020-05-11 15:05:29 -0700 | [diff] [blame] | 3398 | vmcs12->exit_qualification = entry_failure_code; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3399 | goto vmentry_fail_vmexit; |
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 | } |
| 3402 | |
| 3403 | enter_guest_mode(vcpu); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3404 | |
Maxim Levitsky | 0f85722 | 2021-06-07 12:02:00 +0300 | [diff] [blame] | 3405 | if (prepare_vmcs02(vcpu, vmcs12, from_vmentry, &entry_failure_code)) { |
Sean Christopherson | 8e53324 | 2020-11-06 17:03:12 +0800 | [diff] [blame] | 3406 | exit_reason.basic = EXIT_REASON_INVALID_STATE; |
Sean Christopherson | 68cda40 | 2020-05-11 15:05:29 -0700 | [diff] [blame] | 3407 | vmcs12->exit_qualification = entry_failure_code; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3408 | goto vmentry_fail_vmexit_guest_mode; |
Sean Christopherson | 68cda40 | 2020-05-11 15:05:29 -0700 | [diff] [blame] | 3409 | } |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3410 | |
| 3411 | if (from_vmentry) { |
Sean Christopherson | 68cda40 | 2020-05-11 15:05:29 -0700 | [diff] [blame] | 3412 | failed_index = nested_vmx_load_msr(vcpu, |
| 3413 | vmcs12->vm_entry_msr_load_addr, |
| 3414 | vmcs12->vm_entry_msr_load_count); |
| 3415 | if (failed_index) { |
Sean Christopherson | 8e53324 | 2020-11-06 17:03:12 +0800 | [diff] [blame] | 3416 | exit_reason.basic = EXIT_REASON_MSR_LOAD_FAIL; |
Sean Christopherson | 68cda40 | 2020-05-11 15:05:29 -0700 | [diff] [blame] | 3417 | vmcs12->exit_qualification = failed_index; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3418 | goto vmentry_fail_vmexit_guest_mode; |
Sean Christopherson | 68cda40 | 2020-05-11 15:05:29 -0700 | [diff] [blame] | 3419 | } |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3420 | } else { |
| 3421 | /* |
| 3422 | * The MMU is not initialized to point at the right entities yet and |
| 3423 | * "get pages" would need to read data from the guest (i.e. we will |
| 3424 | * need to perform gpa to hpa translation). Request a call |
| 3425 | * to nested_get_vmcs12_pages before the next VM-entry. The MSRs |
| 3426 | * have already been set at vmentry time and should not be reset. |
| 3427 | */ |
Paolo Bonzini | 729c15c | 2020-09-22 06:53:57 -0400 | [diff] [blame] | 3428 | kvm_make_request(KVM_REQ_GET_NESTED_STATE_PAGES, vcpu); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3429 | } |
| 3430 | |
| 3431 | /* |
| 3432 | * If L1 had a pending IRQ/NMI until it executed |
| 3433 | * VMLAUNCH/VMRESUME which wasn't delivered because it was |
| 3434 | * disallowed (e.g. interrupts disabled), L0 needs to |
| 3435 | * evaluate if this pending event should cause an exit from L2 |
| 3436 | * to L1 or delivered directly to L2 (e.g. In case L1 don't |
| 3437 | * intercept EXTERNAL_INTERRUPT). |
| 3438 | * |
| 3439 | * Usually this would be handled by the processor noticing an |
| 3440 | * IRQ/NMI window request, or checking RVI during evaluation of |
| 3441 | * pending virtual interrupts. However, this setting was done |
| 3442 | * on VMCS01 and now VMCS02 is active instead. Thus, we force L0 |
| 3443 | * to perform pending event evaluation by requesting a KVM_REQ_EVENT. |
| 3444 | */ |
| 3445 | if (unlikely(evaluate_pending_interrupts)) |
| 3446 | kvm_make_request(KVM_REQ_EVENT, vcpu); |
| 3447 | |
| 3448 | /* |
Paolo Bonzini | 359a6c3 | 2019-01-29 19:14:46 +0100 | [diff] [blame] | 3449 | * Do not start the preemption timer hrtimer until after we know |
| 3450 | * we are successful, so that only nested_vmx_vmexit needs to cancel |
| 3451 | * the timer. |
| 3452 | */ |
| 3453 | vmx->nested.preemption_timer_expired = false; |
Peter Shier | 850448f | 2020-05-26 14:51:06 -0700 | [diff] [blame] | 3454 | if (nested_cpu_has_preemption_timer(vmcs12)) { |
| 3455 | u64 timer_value = vmx_calc_preemption_timer_value(vcpu); |
| 3456 | vmx_start_preemption_timer(vcpu, timer_value); |
| 3457 | } |
Paolo Bonzini | 359a6c3 | 2019-01-29 19:14:46 +0100 | [diff] [blame] | 3458 | |
| 3459 | /* |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3460 | * Note no nested_vmx_succeed or nested_vmx_fail here. At this point |
| 3461 | * we are no longer running L1, and VMLAUNCH/VMRESUME has not yet |
| 3462 | * returned as far as L1 is concerned. It will only return (and set |
| 3463 | * the success flag) when L2 exits (see nested_vmx_vmexit()). |
| 3464 | */ |
Jim Mattson | 671ddc7 | 2019-10-15 10:44:05 -0700 | [diff] [blame] | 3465 | return NVMX_VMENTRY_SUCCESS; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3466 | |
| 3467 | /* |
| 3468 | * A failed consistency check that leads to a VMExit during L1's |
| 3469 | * VMEnter to L2 is a variation of a normal VMexit, as explained in |
| 3470 | * 26.7 "VM-entry failures during or after loading guest state". |
| 3471 | */ |
| 3472 | vmentry_fail_vmexit_guest_mode: |
Xiaoyao Li | 5e3d394 | 2019-12-06 16:45:26 +0800 | [diff] [blame] | 3473 | if (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETTING) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3474 | vcpu->arch.tsc_offset -= vmcs12->tsc_offset; |
| 3475 | leave_guest_mode(vcpu); |
| 3476 | |
| 3477 | vmentry_fail_vmexit: |
| 3478 | vmx_switch_vmcs(vcpu, &vmx->vmcs01); |
| 3479 | |
| 3480 | if (!from_vmentry) |
Jim Mattson | 671ddc7 | 2019-10-15 10:44:05 -0700 | [diff] [blame] | 3481 | return NVMX_VMENTRY_VMEXIT; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3482 | |
| 3483 | load_vmcs12_host_state(vcpu, vmcs12); |
Sean Christopherson | 8e53324 | 2020-11-06 17:03:12 +0800 | [diff] [blame] | 3484 | vmcs12->vm_exit_reason = exit_reason.full; |
Vitaly Kuznetsov | 1e9dfbd | 2021-05-26 15:20:16 +0200 | [diff] [blame] | 3485 | if (enable_shadow_vmcs || evmptr_is_valid(vmx->nested.hv_evmcs_vmptr)) |
Sean Christopherson | 3731905ef | 2019-05-07 08:36:27 -0700 | [diff] [blame] | 3486 | vmx->nested.need_vmcs12_to_shadow_sync = true; |
Jim Mattson | 671ddc7 | 2019-10-15 10:44:05 -0700 | [diff] [blame] | 3487 | return NVMX_VMENTRY_VMEXIT; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3488 | } |
| 3489 | |
| 3490 | /* |
| 3491 | * nested_vmx_run() handles a nested entry, i.e., a VMLAUNCH or VMRESUME on L1 |
| 3492 | * for running an L2 nested guest. |
| 3493 | */ |
| 3494 | static int nested_vmx_run(struct kvm_vcpu *vcpu, bool launch) |
| 3495 | { |
| 3496 | struct vmcs12 *vmcs12; |
Jim Mattson | 671ddc7 | 2019-10-15 10:44:05 -0700 | [diff] [blame] | 3497 | enum nvmx_vmentry_status status; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3498 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 3499 | u32 interrupt_shadow = vmx_get_interrupt_shadow(vcpu); |
Vitaly Kuznetsov | b6a0653 | 2020-03-09 16:52:13 +0100 | [diff] [blame] | 3500 | enum nested_evmptrld_status evmptrld_status; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3501 | |
| 3502 | if (!nested_vmx_check_permission(vcpu)) |
| 3503 | return 1; |
| 3504 | |
Vitaly Kuznetsov | b6a0653 | 2020-03-09 16:52:13 +0100 | [diff] [blame] | 3505 | evmptrld_status = nested_vmx_handle_enlightened_vmptrld(vcpu, launch); |
| 3506 | if (evmptrld_status == EVMPTRLD_ERROR) { |
| 3507 | kvm_queue_exception(vcpu, UD_VECTOR); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3508 | return 1; |
Sean Christopherson | fc595f3 | 2020-08-12 11:06:15 -0700 | [diff] [blame] | 3509 | } else if (CC(evmptrld_status == EVMPTRLD_VMFAIL)) { |
Vitaly Kuznetsov | b6a0653 | 2020-03-09 16:52:13 +0100 | [diff] [blame] | 3510 | return nested_vmx_failInvalid(vcpu); |
| 3511 | } |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3512 | |
Vitaly Kuznetsov | 1e9dfbd | 2021-05-26 15:20:16 +0200 | [diff] [blame] | 3513 | if (CC(!evmptr_is_valid(vmx->nested.hv_evmcs_vmptr) && |
Yu Zhang | 64c7850 | 2021-09-30 01:51:53 +0800 | [diff] [blame] | 3514 | vmx->nested.current_vmptr == INVALID_GPA)) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3515 | return nested_vmx_failInvalid(vcpu); |
| 3516 | |
| 3517 | vmcs12 = get_vmcs12(vcpu); |
| 3518 | |
| 3519 | /* |
| 3520 | * Can't VMLAUNCH or VMRESUME a shadow VMCS. Despite the fact |
| 3521 | * that there *is* a valid VMCS pointer, RFLAGS.CF is set |
| 3522 | * rather than RFLAGS.ZF, and no error number is stored to the |
| 3523 | * VM-instruction error field. |
| 3524 | */ |
Sean Christopherson | fc595f3 | 2020-08-12 11:06:15 -0700 | [diff] [blame] | 3525 | if (CC(vmcs12->hdr.shadow_vmcs)) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3526 | return nested_vmx_failInvalid(vcpu); |
| 3527 | |
Vitaly Kuznetsov | 1e9dfbd | 2021-05-26 15:20:16 +0200 | [diff] [blame] | 3528 | if (evmptr_is_valid(vmx->nested.hv_evmcs_vmptr)) { |
Vitaly Kuznetsov | d6bf71a | 2021-05-26 15:20:22 +0200 | [diff] [blame] | 3529 | copy_enlightened_to_vmcs12(vmx, vmx->nested.hv_evmcs->hv_clean_fields); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3530 | /* Enlightened VMCS doesn't have launch state */ |
| 3531 | vmcs12->launch_state = !launch; |
| 3532 | } else if (enable_shadow_vmcs) { |
| 3533 | copy_shadow_to_vmcs12(vmx); |
| 3534 | } |
| 3535 | |
| 3536 | /* |
| 3537 | * The nested entry process starts with enforcing various prerequisites |
| 3538 | * on vmcs12 as required by the Intel SDM, and act appropriately when |
| 3539 | * they fail: As the SDM explains, some conditions should cause the |
| 3540 | * instruction to fail, while others will cause the instruction to seem |
| 3541 | * to succeed, but return an EXIT_REASON_INVALID_STATE. |
| 3542 | * To speed up the normal (success) code path, we should avoid checking |
| 3543 | * for misconfigurations which will anyway be caught by the processor |
| 3544 | * when using the merged vmcs02. |
| 3545 | */ |
Sean Christopherson | fc595f3 | 2020-08-12 11:06:15 -0700 | [diff] [blame] | 3546 | if (CC(interrupt_shadow & KVM_X86_SHADOW_INT_MOV_SS)) |
Sean Christopherson | b2656e4 | 2020-06-08 18:56:07 -0700 | [diff] [blame] | 3547 | return nested_vmx_fail(vcpu, VMXERR_ENTRY_EVENTS_BLOCKED_BY_MOV_SS); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3548 | |
Sean Christopherson | fc595f3 | 2020-08-12 11:06:15 -0700 | [diff] [blame] | 3549 | if (CC(vmcs12->launch_state == launch)) |
Sean Christopherson | b2656e4 | 2020-06-08 18:56:07 -0700 | [diff] [blame] | 3550 | return nested_vmx_fail(vcpu, |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3551 | launch ? VMXERR_VMLAUNCH_NONCLEAR_VMCS |
| 3552 | : VMXERR_VMRESUME_NONLAUNCHED_VMCS); |
| 3553 | |
Paolo Bonzini | 98d9e85 | 2019-04-12 10:19:57 +0200 | [diff] [blame] | 3554 | if (nested_vmx_check_controls(vcpu, vmcs12)) |
Sean Christopherson | b2656e4 | 2020-06-08 18:56:07 -0700 | [diff] [blame] | 3555 | return nested_vmx_fail(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD); |
Sean Christopherson | 5478ba3 | 2019-04-11 12:18:06 -0700 | [diff] [blame] | 3556 | |
Maxim Levitsky | af957ee | 2021-11-15 15:18:36 +0200 | [diff] [blame] | 3557 | if (nested_vmx_check_address_space_size(vcpu, vmcs12)) |
| 3558 | return nested_vmx_fail(vcpu, VMXERR_ENTRY_INVALID_HOST_STATE_FIELD); |
| 3559 | |
Paolo Bonzini | 98d9e85 | 2019-04-12 10:19:57 +0200 | [diff] [blame] | 3560 | if (nested_vmx_check_host_state(vcpu, vmcs12)) |
Sean Christopherson | b2656e4 | 2020-06-08 18:56:07 -0700 | [diff] [blame] | 3561 | return nested_vmx_fail(vcpu, VMXERR_ENTRY_INVALID_HOST_STATE_FIELD); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3562 | |
| 3563 | /* |
| 3564 | * We're finally done with prerequisite checking, and can start with |
| 3565 | * the nested entry. |
| 3566 | */ |
| 3567 | vmx->nested.nested_run_pending = 1; |
Peter Shier | 850448f | 2020-05-26 14:51:06 -0700 | [diff] [blame] | 3568 | vmx->nested.has_preemption_timer_deadline = false; |
Jim Mattson | 671ddc7 | 2019-10-15 10:44:05 -0700 | [diff] [blame] | 3569 | status = nested_vmx_enter_non_root_mode(vcpu, true); |
| 3570 | if (unlikely(status != NVMX_VMENTRY_SUCCESS)) |
| 3571 | goto vmentry_failed; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3572 | |
Sean Christopherson | 25bb2cf | 2020-08-12 10:51:29 -0700 | [diff] [blame] | 3573 | /* Emulate processing of posted interrupts on VM-Enter. */ |
| 3574 | if (nested_cpu_has_posted_intr(vmcs12) && |
| 3575 | kvm_apic_has_interrupt(vcpu) == vmx->nested.posted_intr_nv) { |
| 3576 | vmx->nested.pi_pending = true; |
| 3577 | kvm_make_request(KVM_REQ_EVENT, vcpu); |
| 3578 | kvm_apic_clear_irr(vcpu, vmx->nested.posted_intr_nv); |
| 3579 | } |
| 3580 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3581 | /* Hide L1D cache contents from the nested guest. */ |
| 3582 | vmx->vcpu.arch.l1tf_flush_l1d = true; |
| 3583 | |
| 3584 | /* |
| 3585 | * Must happen outside of nested_vmx_enter_non_root_mode() as it will |
| 3586 | * also be used as part of restoring nVMX state for |
| 3587 | * snapshot restore (migration). |
| 3588 | * |
| 3589 | * In this flow, it is assumed that vmcs12 cache was |
Ingo Molnar | 163b099 | 2021-03-21 22:28:53 +0100 | [diff] [blame] | 3590 | * transferred as part of captured nVMX state and should |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3591 | * therefore not be read from guest memory (which may not |
| 3592 | * exist on destination host yet). |
| 3593 | */ |
| 3594 | nested_cache_shadow_vmcs12(vcpu, vmcs12); |
| 3595 | |
Yadong Qi | bf0cd88 | 2020-11-06 14:51:22 +0800 | [diff] [blame] | 3596 | switch (vmcs12->guest_activity_state) { |
| 3597 | case GUEST_ACTIVITY_HLT: |
| 3598 | /* |
| 3599 | * If we're entering a halted L2 vcpu and the L2 vcpu won't be |
| 3600 | * awakened by event injection or by an NMI-window VM-exit or |
| 3601 | * by an interrupt-window VM-exit, halt the vcpu. |
| 3602 | */ |
| 3603 | if (!(vmcs12->vm_entry_intr_info_field & INTR_INFO_VALID_MASK) && |
| 3604 | !nested_cpu_has(vmcs12, CPU_BASED_NMI_WINDOW_EXITING) && |
| 3605 | !(nested_cpu_has(vmcs12, CPU_BASED_INTR_WINDOW_EXITING) && |
| 3606 | (vmcs12->guest_rflags & X86_EFLAGS_IF))) { |
| 3607 | vmx->nested.nested_run_pending = 0; |
| 3608 | return kvm_vcpu_halt(vcpu); |
| 3609 | } |
| 3610 | break; |
| 3611 | case GUEST_ACTIVITY_WAIT_SIPI: |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3612 | vmx->nested.nested_run_pending = 0; |
Yadong Qi | bf0cd88 | 2020-11-06 14:51:22 +0800 | [diff] [blame] | 3613 | vcpu->arch.mp_state = KVM_MP_STATE_INIT_RECEIVED; |
| 3614 | break; |
| 3615 | default: |
| 3616 | break; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3617 | } |
Yadong Qi | bf0cd88 | 2020-11-06 14:51:22 +0800 | [diff] [blame] | 3618 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3619 | return 1; |
Jim Mattson | 671ddc7 | 2019-10-15 10:44:05 -0700 | [diff] [blame] | 3620 | |
| 3621 | vmentry_failed: |
| 3622 | vmx->nested.nested_run_pending = 0; |
| 3623 | if (status == NVMX_VMENTRY_KVM_INTERNAL_ERROR) |
| 3624 | return 0; |
| 3625 | if (status == NVMX_VMENTRY_VMEXIT) |
| 3626 | return 1; |
| 3627 | WARN_ON_ONCE(status != NVMX_VMENTRY_VMFAIL); |
Sean Christopherson | b2656e4 | 2020-06-08 18:56:07 -0700 | [diff] [blame] | 3628 | return nested_vmx_fail(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3629 | } |
| 3630 | |
| 3631 | /* |
| 3632 | * 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] | 3633 | * 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] | 3634 | * This function returns the new value we should put in vmcs12.guest_cr0. |
| 3635 | * It's not enough to just return the vmcs02 GUEST_CR0. Rather, |
| 3636 | * 1. Bits that neither L0 nor L1 trapped, were set directly by L2 and are now |
| 3637 | * available in vmcs02 GUEST_CR0. (Note: It's enough to check that L0 |
| 3638 | * didn't trap the bit, because if L1 did, so would L0). |
| 3639 | * 2. Bits that L1 asked to trap (and therefore L0 also did) could not have |
| 3640 | * been modified by L2, and L1 knows it. So just leave the old value of |
| 3641 | * the bit from vmcs12.guest_cr0. Note that the bit from vmcs02 GUEST_CR0 |
| 3642 | * isn't relevant, because if L0 traps this bit it can set it to anything. |
| 3643 | * 3. Bits that L1 didn't trap, but L0 did. L1 believes the guest could have |
| 3644 | * changed these bits, and therefore they need to be updated, but L0 |
| 3645 | * didn't necessarily allow them to be changed in GUEST_CR0 - and rather |
| 3646 | * put them in vmcs02 CR0_READ_SHADOW. So take these bits from there. |
| 3647 | */ |
| 3648 | static inline unsigned long |
| 3649 | vmcs12_guest_cr0(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12) |
| 3650 | { |
| 3651 | return |
| 3652 | /*1*/ (vmcs_readl(GUEST_CR0) & vcpu->arch.cr0_guest_owned_bits) | |
| 3653 | /*2*/ (vmcs12->guest_cr0 & vmcs12->cr0_guest_host_mask) | |
| 3654 | /*3*/ (vmcs_readl(CR0_READ_SHADOW) & ~(vmcs12->cr0_guest_host_mask | |
| 3655 | vcpu->arch.cr0_guest_owned_bits)); |
| 3656 | } |
| 3657 | |
| 3658 | static inline unsigned long |
| 3659 | vmcs12_guest_cr4(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12) |
| 3660 | { |
| 3661 | return |
| 3662 | /*1*/ (vmcs_readl(GUEST_CR4) & vcpu->arch.cr4_guest_owned_bits) | |
| 3663 | /*2*/ (vmcs12->guest_cr4 & vmcs12->cr4_guest_host_mask) | |
| 3664 | /*3*/ (vmcs_readl(CR4_READ_SHADOW) & ~(vmcs12->cr4_guest_host_mask | |
| 3665 | vcpu->arch.cr4_guest_owned_bits)); |
| 3666 | } |
| 3667 | |
| 3668 | static void vmcs12_save_pending_event(struct kvm_vcpu *vcpu, |
| 3669 | struct vmcs12 *vmcs12) |
| 3670 | { |
| 3671 | u32 idt_vectoring; |
| 3672 | unsigned int nr; |
| 3673 | |
| 3674 | if (vcpu->arch.exception.injected) { |
| 3675 | nr = vcpu->arch.exception.nr; |
| 3676 | idt_vectoring = nr | VECTORING_INFO_VALID_MASK; |
| 3677 | |
| 3678 | if (kvm_exception_is_soft(nr)) { |
| 3679 | vmcs12->vm_exit_instruction_len = |
| 3680 | vcpu->arch.event_exit_inst_len; |
| 3681 | idt_vectoring |= INTR_TYPE_SOFT_EXCEPTION; |
| 3682 | } else |
| 3683 | idt_vectoring |= INTR_TYPE_HARD_EXCEPTION; |
| 3684 | |
| 3685 | if (vcpu->arch.exception.has_error_code) { |
| 3686 | idt_vectoring |= VECTORING_INFO_DELIVER_CODE_MASK; |
| 3687 | vmcs12->idt_vectoring_error_code = |
| 3688 | vcpu->arch.exception.error_code; |
| 3689 | } |
| 3690 | |
| 3691 | vmcs12->idt_vectoring_info_field = idt_vectoring; |
| 3692 | } else if (vcpu->arch.nmi_injected) { |
| 3693 | vmcs12->idt_vectoring_info_field = |
| 3694 | INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK | NMI_VECTOR; |
| 3695 | } else if (vcpu->arch.interrupt.injected) { |
| 3696 | nr = vcpu->arch.interrupt.nr; |
| 3697 | idt_vectoring = nr | VECTORING_INFO_VALID_MASK; |
| 3698 | |
| 3699 | if (vcpu->arch.interrupt.soft) { |
| 3700 | idt_vectoring |= INTR_TYPE_SOFT_INTR; |
| 3701 | vmcs12->vm_entry_instruction_len = |
| 3702 | vcpu->arch.event_exit_inst_len; |
| 3703 | } else |
| 3704 | idt_vectoring |= INTR_TYPE_EXT_INTR; |
| 3705 | |
| 3706 | vmcs12->idt_vectoring_info_field = idt_vectoring; |
| 3707 | } |
| 3708 | } |
| 3709 | |
| 3710 | |
Paolo Bonzini | 96b100c | 2020-03-17 18:32:50 +0100 | [diff] [blame] | 3711 | void nested_mark_vmcs12_pages_dirty(struct kvm_vcpu *vcpu) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3712 | { |
| 3713 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
| 3714 | gfn_t gfn; |
| 3715 | |
| 3716 | /* |
| 3717 | * Don't need to mark the APIC access page dirty; it is never |
| 3718 | * written to by the CPU during APIC virtualization. |
| 3719 | */ |
| 3720 | |
| 3721 | if (nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW)) { |
| 3722 | gfn = vmcs12->virtual_apic_page_addr >> PAGE_SHIFT; |
| 3723 | kvm_vcpu_mark_page_dirty(vcpu, gfn); |
| 3724 | } |
| 3725 | |
| 3726 | if (nested_cpu_has_posted_intr(vmcs12)) { |
| 3727 | gfn = vmcs12->posted_intr_desc_addr >> PAGE_SHIFT; |
| 3728 | kvm_vcpu_mark_page_dirty(vcpu, gfn); |
| 3729 | } |
| 3730 | } |
| 3731 | |
Jim Mattson | 650293c | 2021-06-04 10:26:02 -0700 | [diff] [blame] | 3732 | static int vmx_complete_nested_posted_interrupt(struct kvm_vcpu *vcpu) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3733 | { |
| 3734 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 3735 | int max_irr; |
| 3736 | void *vapic_page; |
| 3737 | u16 status; |
| 3738 | |
Jim Mattson | 966eefb | 2021-06-04 10:26:06 -0700 | [diff] [blame] | 3739 | if (!vmx->nested.pi_pending) |
Jim Mattson | 650293c | 2021-06-04 10:26:02 -0700 | [diff] [blame] | 3740 | return 0; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3741 | |
Jim Mattson | 966eefb | 2021-06-04 10:26:06 -0700 | [diff] [blame] | 3742 | if (!vmx->nested.pi_desc) |
| 3743 | goto mmio_needed; |
| 3744 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3745 | vmx->nested.pi_pending = false; |
Jim Mattson | 966eefb | 2021-06-04 10:26:06 -0700 | [diff] [blame] | 3746 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3747 | if (!pi_test_and_clear_on(vmx->nested.pi_desc)) |
Jim Mattson | 650293c | 2021-06-04 10:26:02 -0700 | [diff] [blame] | 3748 | return 0; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3749 | |
| 3750 | max_irr = find_last_bit((unsigned long *)vmx->nested.pi_desc->pir, 256); |
| 3751 | if (max_irr != 256) { |
KarimAllah Ahmed | 96c66e8 | 2019-01-31 21:24:37 +0100 | [diff] [blame] | 3752 | vapic_page = vmx->nested.virtual_apic_map.hva; |
| 3753 | if (!vapic_page) |
Jim Mattson | 0fe998b | 2021-06-04 10:26:05 -0700 | [diff] [blame] | 3754 | goto mmio_needed; |
KarimAllah Ahmed | 96c66e8 | 2019-01-31 21:24:37 +0100 | [diff] [blame] | 3755 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3756 | __kvm_apic_update_irr(vmx->nested.pi_desc->pir, |
| 3757 | vapic_page, &max_irr); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3758 | status = vmcs_read16(GUEST_INTR_STATUS); |
| 3759 | if ((u8)max_irr > ((u8)status & 0xff)) { |
| 3760 | status &= ~0xff; |
| 3761 | status |= (u8)max_irr; |
| 3762 | vmcs_write16(GUEST_INTR_STATUS, status); |
| 3763 | } |
| 3764 | } |
| 3765 | |
| 3766 | nested_mark_vmcs12_pages_dirty(vcpu); |
Jim Mattson | 650293c | 2021-06-04 10:26:02 -0700 | [diff] [blame] | 3767 | return 0; |
Jim Mattson | 0fe998b | 2021-06-04 10:26:05 -0700 | [diff] [blame] | 3768 | |
| 3769 | mmio_needed: |
| 3770 | kvm_handle_memory_failure(vcpu, X86EMUL_IO_NEEDED, NULL); |
| 3771 | return -ENXIO; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3772 | } |
| 3773 | |
| 3774 | static void nested_vmx_inject_exception_vmexit(struct kvm_vcpu *vcpu, |
| 3775 | unsigned long exit_qual) |
| 3776 | { |
| 3777 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
| 3778 | unsigned int nr = vcpu->arch.exception.nr; |
| 3779 | u32 intr_info = nr | INTR_INFO_VALID_MASK; |
| 3780 | |
| 3781 | if (vcpu->arch.exception.has_error_code) { |
| 3782 | vmcs12->vm_exit_intr_error_code = vcpu->arch.exception.error_code; |
| 3783 | intr_info |= INTR_INFO_DELIVER_CODE_MASK; |
| 3784 | } |
| 3785 | |
| 3786 | if (kvm_exception_is_soft(nr)) |
| 3787 | intr_info |= INTR_TYPE_SOFT_EXCEPTION; |
| 3788 | else |
| 3789 | intr_info |= INTR_TYPE_HARD_EXCEPTION; |
| 3790 | |
| 3791 | if (!(vmcs12->idt_vectoring_info_field & VECTORING_INFO_VALID_MASK) && |
| 3792 | vmx_get_nmi_mask(vcpu)) |
| 3793 | intr_info |= INTR_INFO_UNBLOCK_NMI; |
| 3794 | |
| 3795 | nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI, intr_info, exit_qual); |
| 3796 | } |
| 3797 | |
Oliver Upton | 684c042 | 2020-02-07 02:36:05 -0800 | [diff] [blame] | 3798 | /* |
| 3799 | * Returns true if a debug trap is pending delivery. |
| 3800 | * |
| 3801 | * In KVM, debug traps bear an exception payload. As such, the class of a #DB |
| 3802 | * exception may be inferred from the presence of an exception payload. |
| 3803 | */ |
| 3804 | static inline bool vmx_pending_dbg_trap(struct kvm_vcpu *vcpu) |
| 3805 | { |
| 3806 | return vcpu->arch.exception.pending && |
| 3807 | vcpu->arch.exception.nr == DB_VECTOR && |
| 3808 | vcpu->arch.exception.payload; |
| 3809 | } |
| 3810 | |
| 3811 | /* |
| 3812 | * Certain VM-exits set the 'pending debug exceptions' field to indicate a |
| 3813 | * recognized #DB (data or single-step) that has yet to be delivered. Since KVM |
| 3814 | * represents these debug traps with a payload that is said to be compatible |
| 3815 | * with the 'pending debug exceptions' field, write the payload to the VMCS |
| 3816 | * field if a VM-exit is delivered before the debug trap. |
| 3817 | */ |
| 3818 | static void nested_vmx_update_pending_dbg(struct kvm_vcpu *vcpu) |
| 3819 | { |
| 3820 | if (vmx_pending_dbg_trap(vcpu)) |
| 3821 | vmcs_writel(GUEST_PENDING_DBG_EXCEPTIONS, |
| 3822 | vcpu->arch.exception.payload); |
| 3823 | } |
| 3824 | |
Sean Christopherson | d2060bd | 2020-04-22 19:25:39 -0700 | [diff] [blame] | 3825 | static bool nested_vmx_preemption_timer_pending(struct kvm_vcpu *vcpu) |
| 3826 | { |
| 3827 | return nested_cpu_has_preemption_timer(get_vmcs12(vcpu)) && |
| 3828 | to_vmx(vcpu)->nested.preemption_timer_expired; |
| 3829 | } |
| 3830 | |
Sean Christopherson | a1c77ab | 2020-03-02 22:27:35 -0800 | [diff] [blame] | 3831 | static int vmx_check_nested_events(struct kvm_vcpu *vcpu) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3832 | { |
| 3833 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 3834 | unsigned long exit_qual; |
| 3835 | bool block_nested_events = |
| 3836 | vmx->nested.nested_run_pending || kvm_event_needs_reinjection(vcpu); |
Oliver Upton | 5ef8acb | 2020-02-07 02:36:07 -0800 | [diff] [blame] | 3837 | bool mtf_pending = vmx->nested.mtf_pending; |
Liran Alon | 4b9852f | 2019-08-26 13:24:49 +0300 | [diff] [blame] | 3838 | struct kvm_lapic *apic = vcpu->arch.apic; |
| 3839 | |
Oliver Upton | 5ef8acb | 2020-02-07 02:36:07 -0800 | [diff] [blame] | 3840 | /* |
| 3841 | * Clear the MTF state. If a higher priority VM-exit is delivered first, |
| 3842 | * this state is discarded. |
| 3843 | */ |
Oliver Upton | 5c8beb4 | 2020-04-06 20:12:37 +0000 | [diff] [blame] | 3844 | if (!block_nested_events) |
| 3845 | vmx->nested.mtf_pending = false; |
Oliver Upton | 5ef8acb | 2020-02-07 02:36:07 -0800 | [diff] [blame] | 3846 | |
Liran Alon | 4b9852f | 2019-08-26 13:24:49 +0300 | [diff] [blame] | 3847 | if (lapic_in_kernel(vcpu) && |
| 3848 | test_bit(KVM_APIC_INIT, &apic->pending_events)) { |
| 3849 | if (block_nested_events) |
| 3850 | return -EBUSY; |
Oliver Upton | 684c042 | 2020-02-07 02:36:05 -0800 | [diff] [blame] | 3851 | nested_vmx_update_pending_dbg(vcpu); |
Liran Alon | e64a850 | 2019-11-11 14:16:05 +0200 | [diff] [blame] | 3852 | clear_bit(KVM_APIC_INIT, &apic->pending_events); |
Yadong Qi | bf0cd88 | 2020-11-06 14:51:22 +0800 | [diff] [blame] | 3853 | if (vcpu->arch.mp_state != KVM_MP_STATE_INIT_RECEIVED) |
| 3854 | nested_vmx_vmexit(vcpu, EXIT_REASON_INIT_SIGNAL, 0, 0); |
| 3855 | return 0; |
| 3856 | } |
| 3857 | |
| 3858 | if (lapic_in_kernel(vcpu) && |
| 3859 | test_bit(KVM_APIC_SIPI, &apic->pending_events)) { |
| 3860 | if (block_nested_events) |
| 3861 | return -EBUSY; |
| 3862 | |
| 3863 | clear_bit(KVM_APIC_SIPI, &apic->pending_events); |
| 3864 | if (vcpu->arch.mp_state == KVM_MP_STATE_INIT_RECEIVED) |
| 3865 | nested_vmx_vmexit(vcpu, EXIT_REASON_SIPI_SIGNAL, 0, |
| 3866 | apic->sipi_vector & 0xFFUL); |
Liran Alon | 4b9852f | 2019-08-26 13:24:49 +0300 | [diff] [blame] | 3867 | return 0; |
| 3868 | } |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3869 | |
Oliver Upton | 5ef8acb | 2020-02-07 02:36:07 -0800 | [diff] [blame] | 3870 | /* |
| 3871 | * Process any exceptions that are not debug traps before MTF. |
Maxim Levitsky | 4020da3 | 2021-04-01 17:38:14 +0300 | [diff] [blame] | 3872 | * |
| 3873 | * Note that only a pending nested run can block a pending exception. |
| 3874 | * Otherwise an injected NMI/interrupt should either be |
| 3875 | * lost or delivered to the nested hypervisor in the IDT_VECTORING_INFO, |
| 3876 | * while delivering the pending exception. |
Oliver Upton | 5ef8acb | 2020-02-07 02:36:07 -0800 | [diff] [blame] | 3877 | */ |
Maxim Levitsky | 4020da3 | 2021-04-01 17:38:14 +0300 | [diff] [blame] | 3878 | |
Sean Christopherson | 6ce347a | 2020-04-22 19:25:38 -0700 | [diff] [blame] | 3879 | if (vcpu->arch.exception.pending && !vmx_pending_dbg_trap(vcpu)) { |
Maxim Levitsky | 4020da3 | 2021-04-01 17:38:14 +0300 | [diff] [blame] | 3880 | if (vmx->nested.nested_run_pending) |
Oliver Upton | 5ef8acb | 2020-02-07 02:36:07 -0800 | [diff] [blame] | 3881 | return -EBUSY; |
Sean Christopherson | 6ce347a | 2020-04-22 19:25:38 -0700 | [diff] [blame] | 3882 | if (!nested_vmx_check_exception(vcpu, &exit_qual)) |
| 3883 | goto no_vmexit; |
Oliver Upton | 5ef8acb | 2020-02-07 02:36:07 -0800 | [diff] [blame] | 3884 | nested_vmx_inject_exception_vmexit(vcpu, exit_qual); |
| 3885 | return 0; |
| 3886 | } |
| 3887 | |
| 3888 | if (mtf_pending) { |
| 3889 | if (block_nested_events) |
| 3890 | return -EBUSY; |
| 3891 | nested_vmx_update_pending_dbg(vcpu); |
| 3892 | nested_vmx_vmexit(vcpu, EXIT_REASON_MONITOR_TRAP_FLAG, 0, 0); |
| 3893 | return 0; |
| 3894 | } |
| 3895 | |
Sean Christopherson | 6ce347a | 2020-04-22 19:25:38 -0700 | [diff] [blame] | 3896 | if (vcpu->arch.exception.pending) { |
Maxim Levitsky | 4020da3 | 2021-04-01 17:38:14 +0300 | [diff] [blame] | 3897 | if (vmx->nested.nested_run_pending) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3898 | return -EBUSY; |
Sean Christopherson | 6ce347a | 2020-04-22 19:25:38 -0700 | [diff] [blame] | 3899 | if (!nested_vmx_check_exception(vcpu, &exit_qual)) |
| 3900 | goto no_vmexit; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3901 | nested_vmx_inject_exception_vmexit(vcpu, exit_qual); |
| 3902 | return 0; |
| 3903 | } |
| 3904 | |
Sean Christopherson | d2060bd | 2020-04-22 19:25:39 -0700 | [diff] [blame] | 3905 | if (nested_vmx_preemption_timer_pending(vcpu)) { |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3906 | if (block_nested_events) |
| 3907 | return -EBUSY; |
| 3908 | nested_vmx_vmexit(vcpu, EXIT_REASON_PREEMPTION_TIMER, 0, 0); |
| 3909 | return 0; |
| 3910 | } |
| 3911 | |
Sean Christopherson | 1cd2f0b | 2020-04-22 19:25:46 -0700 | [diff] [blame] | 3912 | if (vcpu->arch.smi_pending && !is_smm(vcpu)) { |
| 3913 | if (block_nested_events) |
| 3914 | return -EBUSY; |
| 3915 | goto no_vmexit; |
| 3916 | } |
| 3917 | |
Sean Christopherson | 15ff0b4 | 2020-04-22 19:25:45 -0700 | [diff] [blame] | 3918 | if (vcpu->arch.nmi_pending && !vmx_nmi_blocked(vcpu)) { |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3919 | if (block_nested_events) |
| 3920 | return -EBUSY; |
Sean Christopherson | 15ff0b4 | 2020-04-22 19:25:45 -0700 | [diff] [blame] | 3921 | if (!nested_exit_on_nmi(vcpu)) |
| 3922 | goto no_vmexit; |
| 3923 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3924 | nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI, |
| 3925 | NMI_VECTOR | INTR_TYPE_NMI_INTR | |
| 3926 | INTR_INFO_VALID_MASK, 0); |
| 3927 | /* |
| 3928 | * The NMI-triggered VM exit counts as injection: |
| 3929 | * clear this one and block further NMIs. |
| 3930 | */ |
| 3931 | vcpu->arch.nmi_pending = 0; |
| 3932 | vmx_set_nmi_mask(vcpu, true); |
| 3933 | return 0; |
| 3934 | } |
| 3935 | |
Sean Christopherson | 15ff0b4 | 2020-04-22 19:25:45 -0700 | [diff] [blame] | 3936 | if (kvm_cpu_has_interrupt(vcpu) && !vmx_interrupt_blocked(vcpu)) { |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3937 | if (block_nested_events) |
| 3938 | return -EBUSY; |
Sean Christopherson | 15ff0b4 | 2020-04-22 19:25:45 -0700 | [diff] [blame] | 3939 | if (!nested_exit_on_intr(vcpu)) |
| 3940 | goto no_vmexit; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3941 | nested_vmx_vmexit(vcpu, EXIT_REASON_EXTERNAL_INTERRUPT, 0, 0); |
| 3942 | return 0; |
| 3943 | } |
| 3944 | |
Sean Christopherson | 6ce347a | 2020-04-22 19:25:38 -0700 | [diff] [blame] | 3945 | no_vmexit: |
Jim Mattson | 650293c | 2021-06-04 10:26:02 -0700 | [diff] [blame] | 3946 | return vmx_complete_nested_posted_interrupt(vcpu); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3947 | } |
| 3948 | |
| 3949 | static u32 vmx_get_preemption_timer_value(struct kvm_vcpu *vcpu) |
| 3950 | { |
| 3951 | ktime_t remaining = |
| 3952 | hrtimer_get_remaining(&to_vmx(vcpu)->nested.preemption_timer); |
| 3953 | u64 value; |
| 3954 | |
| 3955 | if (ktime_to_ns(remaining) <= 0) |
| 3956 | return 0; |
| 3957 | |
| 3958 | value = ktime_to_ns(remaining) * vcpu->arch.virtual_tsc_khz; |
| 3959 | do_div(value, 1000000); |
| 3960 | return value >> VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE; |
| 3961 | } |
| 3962 | |
Sean Christopherson | 7952d76 | 2019-05-07 08:36:29 -0700 | [diff] [blame] | 3963 | static bool is_vmcs12_ext_field(unsigned long field) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3964 | { |
Sean Christopherson | 7952d76 | 2019-05-07 08:36:29 -0700 | [diff] [blame] | 3965 | switch (field) { |
| 3966 | case GUEST_ES_SELECTOR: |
| 3967 | case GUEST_CS_SELECTOR: |
| 3968 | case GUEST_SS_SELECTOR: |
| 3969 | case GUEST_DS_SELECTOR: |
| 3970 | case GUEST_FS_SELECTOR: |
| 3971 | case GUEST_GS_SELECTOR: |
| 3972 | case GUEST_LDTR_SELECTOR: |
| 3973 | case GUEST_TR_SELECTOR: |
| 3974 | case GUEST_ES_LIMIT: |
| 3975 | case GUEST_CS_LIMIT: |
| 3976 | case GUEST_SS_LIMIT: |
| 3977 | case GUEST_DS_LIMIT: |
| 3978 | case GUEST_FS_LIMIT: |
| 3979 | case GUEST_GS_LIMIT: |
| 3980 | case GUEST_LDTR_LIMIT: |
| 3981 | case GUEST_TR_LIMIT: |
| 3982 | case GUEST_GDTR_LIMIT: |
| 3983 | case GUEST_IDTR_LIMIT: |
| 3984 | case GUEST_ES_AR_BYTES: |
| 3985 | case GUEST_DS_AR_BYTES: |
| 3986 | case GUEST_FS_AR_BYTES: |
| 3987 | case GUEST_GS_AR_BYTES: |
| 3988 | case GUEST_LDTR_AR_BYTES: |
| 3989 | case GUEST_TR_AR_BYTES: |
| 3990 | case GUEST_ES_BASE: |
| 3991 | case GUEST_CS_BASE: |
| 3992 | case GUEST_SS_BASE: |
| 3993 | case GUEST_DS_BASE: |
| 3994 | case GUEST_FS_BASE: |
| 3995 | case GUEST_GS_BASE: |
| 3996 | case GUEST_LDTR_BASE: |
| 3997 | case GUEST_TR_BASE: |
| 3998 | case GUEST_GDTR_BASE: |
| 3999 | case GUEST_IDTR_BASE: |
| 4000 | case GUEST_PENDING_DBG_EXCEPTIONS: |
| 4001 | case GUEST_BNDCFGS: |
| 4002 | return true; |
| 4003 | default: |
| 4004 | break; |
| 4005 | } |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4006 | |
Sean Christopherson | 7952d76 | 2019-05-07 08:36:29 -0700 | [diff] [blame] | 4007 | return false; |
| 4008 | } |
| 4009 | |
| 4010 | static void sync_vmcs02_to_vmcs12_rare(struct kvm_vcpu *vcpu, |
| 4011 | struct vmcs12 *vmcs12) |
| 4012 | { |
| 4013 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4014 | |
| 4015 | vmcs12->guest_es_selector = vmcs_read16(GUEST_ES_SELECTOR); |
| 4016 | vmcs12->guest_cs_selector = vmcs_read16(GUEST_CS_SELECTOR); |
| 4017 | vmcs12->guest_ss_selector = vmcs_read16(GUEST_SS_SELECTOR); |
| 4018 | vmcs12->guest_ds_selector = vmcs_read16(GUEST_DS_SELECTOR); |
| 4019 | vmcs12->guest_fs_selector = vmcs_read16(GUEST_FS_SELECTOR); |
| 4020 | vmcs12->guest_gs_selector = vmcs_read16(GUEST_GS_SELECTOR); |
| 4021 | vmcs12->guest_ldtr_selector = vmcs_read16(GUEST_LDTR_SELECTOR); |
| 4022 | vmcs12->guest_tr_selector = vmcs_read16(GUEST_TR_SELECTOR); |
| 4023 | vmcs12->guest_es_limit = vmcs_read32(GUEST_ES_LIMIT); |
| 4024 | vmcs12->guest_cs_limit = vmcs_read32(GUEST_CS_LIMIT); |
| 4025 | vmcs12->guest_ss_limit = vmcs_read32(GUEST_SS_LIMIT); |
| 4026 | vmcs12->guest_ds_limit = vmcs_read32(GUEST_DS_LIMIT); |
| 4027 | vmcs12->guest_fs_limit = vmcs_read32(GUEST_FS_LIMIT); |
| 4028 | vmcs12->guest_gs_limit = vmcs_read32(GUEST_GS_LIMIT); |
| 4029 | vmcs12->guest_ldtr_limit = vmcs_read32(GUEST_LDTR_LIMIT); |
| 4030 | vmcs12->guest_tr_limit = vmcs_read32(GUEST_TR_LIMIT); |
| 4031 | vmcs12->guest_gdtr_limit = vmcs_read32(GUEST_GDTR_LIMIT); |
| 4032 | vmcs12->guest_idtr_limit = vmcs_read32(GUEST_IDTR_LIMIT); |
| 4033 | vmcs12->guest_es_ar_bytes = vmcs_read32(GUEST_ES_AR_BYTES); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4034 | vmcs12->guest_ds_ar_bytes = vmcs_read32(GUEST_DS_AR_BYTES); |
| 4035 | vmcs12->guest_fs_ar_bytes = vmcs_read32(GUEST_FS_AR_BYTES); |
| 4036 | vmcs12->guest_gs_ar_bytes = vmcs_read32(GUEST_GS_AR_BYTES); |
| 4037 | vmcs12->guest_ldtr_ar_bytes = vmcs_read32(GUEST_LDTR_AR_BYTES); |
| 4038 | vmcs12->guest_tr_ar_bytes = vmcs_read32(GUEST_TR_AR_BYTES); |
| 4039 | vmcs12->guest_es_base = vmcs_readl(GUEST_ES_BASE); |
| 4040 | vmcs12->guest_cs_base = vmcs_readl(GUEST_CS_BASE); |
| 4041 | vmcs12->guest_ss_base = vmcs_readl(GUEST_SS_BASE); |
| 4042 | vmcs12->guest_ds_base = vmcs_readl(GUEST_DS_BASE); |
| 4043 | vmcs12->guest_fs_base = vmcs_readl(GUEST_FS_BASE); |
| 4044 | vmcs12->guest_gs_base = vmcs_readl(GUEST_GS_BASE); |
| 4045 | vmcs12->guest_ldtr_base = vmcs_readl(GUEST_LDTR_BASE); |
| 4046 | vmcs12->guest_tr_base = vmcs_readl(GUEST_TR_BASE); |
| 4047 | vmcs12->guest_gdtr_base = vmcs_readl(GUEST_GDTR_BASE); |
| 4048 | vmcs12->guest_idtr_base = vmcs_readl(GUEST_IDTR_BASE); |
Sean Christopherson | 7952d76 | 2019-05-07 08:36:29 -0700 | [diff] [blame] | 4049 | vmcs12->guest_pending_dbg_exceptions = |
| 4050 | vmcs_readl(GUEST_PENDING_DBG_EXCEPTIONS); |
| 4051 | if (kvm_mpx_supported()) |
| 4052 | vmcs12->guest_bndcfgs = vmcs_read64(GUEST_BNDCFGS); |
| 4053 | |
| 4054 | vmx->nested.need_sync_vmcs02_to_vmcs12_rare = false; |
| 4055 | } |
| 4056 | |
| 4057 | static void copy_vmcs02_to_vmcs12_rare(struct kvm_vcpu *vcpu, |
| 4058 | struct vmcs12 *vmcs12) |
| 4059 | { |
| 4060 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 4061 | int cpu; |
| 4062 | |
| 4063 | if (!vmx->nested.need_sync_vmcs02_to_vmcs12_rare) |
| 4064 | return; |
| 4065 | |
| 4066 | |
| 4067 | WARN_ON_ONCE(vmx->loaded_vmcs != &vmx->vmcs01); |
| 4068 | |
| 4069 | cpu = get_cpu(); |
| 4070 | vmx->loaded_vmcs = &vmx->nested.vmcs02; |
Sean Christopherson | 1af1bb0 | 2020-05-06 16:58:50 -0700 | [diff] [blame] | 4071 | vmx_vcpu_load_vmcs(vcpu, cpu, &vmx->vmcs01); |
Sean Christopherson | 7952d76 | 2019-05-07 08:36:29 -0700 | [diff] [blame] | 4072 | |
| 4073 | sync_vmcs02_to_vmcs12_rare(vcpu, vmcs12); |
| 4074 | |
| 4075 | vmx->loaded_vmcs = &vmx->vmcs01; |
Sean Christopherson | 1af1bb0 | 2020-05-06 16:58:50 -0700 | [diff] [blame] | 4076 | vmx_vcpu_load_vmcs(vcpu, cpu, &vmx->nested.vmcs02); |
Sean Christopherson | 7952d76 | 2019-05-07 08:36:29 -0700 | [diff] [blame] | 4077 | put_cpu(); |
| 4078 | } |
| 4079 | |
| 4080 | /* |
| 4081 | * Update the guest state fields of vmcs12 to reflect changes that |
| 4082 | * occurred while L2 was running. (The "IA-32e mode guest" bit of the |
| 4083 | * VM-entry controls is also updated, since this is really a guest |
| 4084 | * state bit.) |
| 4085 | */ |
| 4086 | static void sync_vmcs02_to_vmcs12(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12) |
| 4087 | { |
| 4088 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 4089 | |
Vitaly Kuznetsov | 1e9dfbd | 2021-05-26 15:20:16 +0200 | [diff] [blame] | 4090 | if (evmptr_is_valid(vmx->nested.hv_evmcs_vmptr)) |
Sean Christopherson | 7952d76 | 2019-05-07 08:36:29 -0700 | [diff] [blame] | 4091 | sync_vmcs02_to_vmcs12_rare(vcpu, vmcs12); |
| 4092 | |
Vitaly Kuznetsov | 1e9dfbd | 2021-05-26 15:20:16 +0200 | [diff] [blame] | 4093 | vmx->nested.need_sync_vmcs02_to_vmcs12_rare = |
| 4094 | !evmptr_is_valid(vmx->nested.hv_evmcs_vmptr); |
Sean Christopherson | 7952d76 | 2019-05-07 08:36:29 -0700 | [diff] [blame] | 4095 | |
| 4096 | vmcs12->guest_cr0 = vmcs12_guest_cr0(vcpu, vmcs12); |
| 4097 | vmcs12->guest_cr4 = vmcs12_guest_cr4(vcpu, vmcs12); |
| 4098 | |
| 4099 | vmcs12->guest_rsp = kvm_rsp_read(vcpu); |
| 4100 | vmcs12->guest_rip = kvm_rip_read(vcpu); |
| 4101 | vmcs12->guest_rflags = vmcs_readl(GUEST_RFLAGS); |
| 4102 | |
| 4103 | vmcs12->guest_cs_ar_bytes = vmcs_read32(GUEST_CS_AR_BYTES); |
| 4104 | vmcs12->guest_ss_ar_bytes = vmcs_read32(GUEST_SS_AR_BYTES); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4105 | |
| 4106 | vmcs12->guest_interruptibility_info = |
| 4107 | vmcs_read32(GUEST_INTERRUPTIBILITY_INFO); |
Sean Christopherson | 7952d76 | 2019-05-07 08:36:29 -0700 | [diff] [blame] | 4108 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4109 | if (vcpu->arch.mp_state == KVM_MP_STATE_HALTED) |
| 4110 | vmcs12->guest_activity_state = GUEST_ACTIVITY_HLT; |
Yadong Qi | bf0cd88 | 2020-11-06 14:51:22 +0800 | [diff] [blame] | 4111 | else if (vcpu->arch.mp_state == KVM_MP_STATE_INIT_RECEIVED) |
| 4112 | vmcs12->guest_activity_state = GUEST_ACTIVITY_WAIT_SIPI; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4113 | else |
| 4114 | vmcs12->guest_activity_state = GUEST_ACTIVITY_ACTIVE; |
| 4115 | |
Paolo Bonzini | b4b65b5 | 2019-01-29 19:12:35 +0100 | [diff] [blame] | 4116 | if (nested_cpu_has_preemption_timer(vmcs12) && |
Peter Shier | 850448f | 2020-05-26 14:51:06 -0700 | [diff] [blame] | 4117 | vmcs12->vm_exit_controls & VM_EXIT_SAVE_VMX_PREEMPTION_TIMER && |
| 4118 | !vmx->nested.nested_run_pending) |
| 4119 | vmcs12->vmx_preemption_timer_value = |
| 4120 | vmx_get_preemption_timer_value(vcpu); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4121 | |
| 4122 | /* |
| 4123 | * In some cases (usually, nested EPT), L2 is allowed to change its |
| 4124 | * own CR3 without exiting. If it has changed it, we must keep it. |
| 4125 | * Of course, if L0 is using shadow page tables, GUEST_CR3 was defined |
| 4126 | * by L0, not L1 or L2, so we mustn't unconditionally copy it to vmcs12. |
| 4127 | * |
| 4128 | * Additionally, restore L2's PDPTR to vmcs12. |
| 4129 | */ |
| 4130 | if (enable_ept) { |
| 4131 | vmcs12->guest_cr3 = vmcs_readl(GUEST_CR3); |
Sean Christopherson | c7554efc | 2019-05-07 09:06:40 -0700 | [diff] [blame] | 4132 | if (nested_cpu_has_ept(vmcs12) && is_pae_paging(vcpu)) { |
| 4133 | vmcs12->guest_pdptr0 = vmcs_read64(GUEST_PDPTR0); |
| 4134 | vmcs12->guest_pdptr1 = vmcs_read64(GUEST_PDPTR1); |
| 4135 | vmcs12->guest_pdptr2 = vmcs_read64(GUEST_PDPTR2); |
| 4136 | vmcs12->guest_pdptr3 = vmcs_read64(GUEST_PDPTR3); |
| 4137 | } |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4138 | } |
| 4139 | |
| 4140 | vmcs12->guest_linear_address = vmcs_readl(GUEST_LINEAR_ADDRESS); |
| 4141 | |
| 4142 | if (nested_cpu_has_vid(vmcs12)) |
| 4143 | vmcs12->guest_intr_status = vmcs_read16(GUEST_INTR_STATUS); |
| 4144 | |
| 4145 | vmcs12->vm_entry_controls = |
| 4146 | (vmcs12->vm_entry_controls & ~VM_ENTRY_IA32E_MODE) | |
| 4147 | (vm_entry_controls_get(to_vmx(vcpu)) & VM_ENTRY_IA32E_MODE); |
| 4148 | |
Sean Christopherson | 699a1ac | 2019-05-07 09:06:37 -0700 | [diff] [blame] | 4149 | if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_DEBUG_CONTROLS) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4150 | kvm_get_dr(vcpu, 7, (unsigned long *)&vmcs12->guest_dr7); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4151 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4152 | if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_IA32_EFER) |
| 4153 | vmcs12->guest_ia32_efer = vcpu->arch.efer; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4154 | } |
| 4155 | |
| 4156 | /* |
| 4157 | * prepare_vmcs12 is part of what we need to do when the nested L2 guest exits |
| 4158 | * and we want to prepare to run its L1 parent. L1 keeps a vmcs for L2 (vmcs12), |
| 4159 | * and this function updates it to reflect the changes to the guest state while |
| 4160 | * L2 was running (and perhaps made some exits which were handled directly by L0 |
| 4161 | * without going back to L1), and to reflect the exit reason. |
| 4162 | * Note that we do not have to copy here all VMCS fields, just those that |
| 4163 | * could have changed by the L2 guest or the exit - i.e., the guest-state and |
| 4164 | * exit-information fields only. Other fields are modified by L1 with VMWRITE, |
| 4165 | * which already writes to vmcs12 directly. |
| 4166 | */ |
| 4167 | static void prepare_vmcs12(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12, |
Sean Christopherson | 4dcefa3 | 2020-04-15 10:55:18 -0700 | [diff] [blame] | 4168 | u32 vm_exit_reason, u32 exit_intr_info, |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4169 | unsigned long exit_qualification) |
| 4170 | { |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4171 | /* update exit information fields: */ |
Sean Christopherson | 4dcefa3 | 2020-04-15 10:55:18 -0700 | [diff] [blame] | 4172 | vmcs12->vm_exit_reason = vm_exit_reason; |
Sean Christopherson | 3c0c2ad | 2021-04-12 16:21:37 +1200 | [diff] [blame] | 4173 | if (to_vmx(vcpu)->exit_reason.enclave_mode) |
| 4174 | vmcs12->vm_exit_reason |= VMX_EXIT_REASONS_SGX_ENCLAVE_MODE; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4175 | vmcs12->exit_qualification = exit_qualification; |
| 4176 | vmcs12->vm_exit_intr_info = exit_intr_info; |
| 4177 | |
| 4178 | vmcs12->idt_vectoring_info_field = 0; |
| 4179 | vmcs12->vm_exit_instruction_len = vmcs_read32(VM_EXIT_INSTRUCTION_LEN); |
| 4180 | vmcs12->vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO); |
| 4181 | |
| 4182 | if (!(vmcs12->vm_exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY)) { |
| 4183 | vmcs12->launch_state = 1; |
| 4184 | |
| 4185 | /* vm_entry_intr_info_field is cleared on exit. Emulate this |
| 4186 | * instead of reading the real value. */ |
| 4187 | vmcs12->vm_entry_intr_info_field &= ~INTR_INFO_VALID_MASK; |
| 4188 | |
| 4189 | /* |
| 4190 | * Transfer the event that L0 or L1 may wanted to inject into |
| 4191 | * L2 to IDT_VECTORING_INFO_FIELD. |
| 4192 | */ |
| 4193 | vmcs12_save_pending_event(vcpu, vmcs12); |
Krish Sadhukhan | a0d4f80 | 2018-12-04 19:00:13 -0500 | [diff] [blame] | 4194 | |
| 4195 | /* |
| 4196 | * According to spec, there's no need to store the guest's |
| 4197 | * MSRs if the exit is due to a VM-entry failure that occurs |
| 4198 | * during or after loading the guest state. Since this exit |
| 4199 | * does not fall in that category, we need to save the MSRs. |
| 4200 | */ |
| 4201 | if (nested_vmx_store_msr(vcpu, |
| 4202 | vmcs12->vm_exit_msr_store_addr, |
| 4203 | vmcs12->vm_exit_msr_store_count)) |
| 4204 | nested_vmx_abort(vcpu, |
| 4205 | VMX_ABORT_SAVE_GUEST_MSR_FAIL); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4206 | } |
| 4207 | |
| 4208 | /* |
| 4209 | * Drop what we picked up for L2 via vmx_complete_interrupts. It is |
| 4210 | * preserved above and would only end up incorrectly in L1. |
| 4211 | */ |
| 4212 | vcpu->arch.nmi_injected = false; |
| 4213 | kvm_clear_exception_queue(vcpu); |
| 4214 | kvm_clear_interrupt_queue(vcpu); |
| 4215 | } |
| 4216 | |
| 4217 | /* |
| 4218 | * A part of what we need to when the nested L2 guest exits and we want to |
| 4219 | * run its L1 parent, is to reset L1's guest state to the host state specified |
| 4220 | * in vmcs12. |
| 4221 | * This function is to be called not only on normal nested exit, but also on |
| 4222 | * a nested entry failure, as explained in Intel's spec, 3B.23.7 ("VM-Entry |
| 4223 | * Failures During or After Loading Guest State"). |
| 4224 | * This function should be called when the active VMCS is L1's (vmcs01). |
| 4225 | */ |
| 4226 | static void load_vmcs12_host_state(struct kvm_vcpu *vcpu, |
| 4227 | struct vmcs12 *vmcs12) |
| 4228 | { |
Sean Christopherson | 68cda40 | 2020-05-11 15:05:29 -0700 | [diff] [blame] | 4229 | enum vm_entry_failure_code ignored; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4230 | struct kvm_segment seg; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4231 | |
| 4232 | if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_EFER) |
| 4233 | vcpu->arch.efer = vmcs12->host_ia32_efer; |
| 4234 | else if (vmcs12->vm_exit_controls & VM_EXIT_HOST_ADDR_SPACE_SIZE) |
| 4235 | vcpu->arch.efer |= (EFER_LMA | EFER_LME); |
| 4236 | else |
| 4237 | vcpu->arch.efer &= ~(EFER_LMA | EFER_LME); |
| 4238 | vmx_set_efer(vcpu, vcpu->arch.efer); |
| 4239 | |
Paolo Bonzini | e9c16c7 | 2019-04-30 22:07:26 +0200 | [diff] [blame] | 4240 | kvm_rsp_write(vcpu, vmcs12->host_rsp); |
| 4241 | kvm_rip_write(vcpu, vmcs12->host_rip); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4242 | vmx_set_rflags(vcpu, X86_EFLAGS_FIXED); |
| 4243 | vmx_set_interrupt_shadow(vcpu, 0); |
| 4244 | |
| 4245 | /* |
| 4246 | * Note that calling vmx_set_cr0 is important, even if cr0 hasn't |
| 4247 | * actually changed, because vmx_set_cr0 refers to efer set above. |
| 4248 | * |
| 4249 | * CR0_GUEST_HOST_MASK is already set in the original vmcs01 |
| 4250 | * (KVM doesn't change it); |
| 4251 | */ |
Sean Christopherson | fa71e95 | 2020-07-02 21:04:22 -0700 | [diff] [blame] | 4252 | vcpu->arch.cr0_guest_owned_bits = KVM_POSSIBLE_CR0_GUEST_BITS; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4253 | vmx_set_cr0(vcpu, vmcs12->host_cr0); |
| 4254 | |
| 4255 | /* Same as above - no reason to call set_cr4_guest_host_mask(). */ |
| 4256 | vcpu->arch.cr4_guest_owned_bits = ~vmcs_readl(CR4_GUEST_HOST_MASK); |
| 4257 | vmx_set_cr4(vcpu, vmcs12->host_cr4); |
| 4258 | |
| 4259 | nested_ept_uninit_mmu_context(vcpu); |
| 4260 | |
| 4261 | /* |
| 4262 | * Only PDPTE load can fail as the value of cr3 was checked on entry and |
| 4263 | * couldn't have changed. |
| 4264 | */ |
Maxim Levitsky | 0f85722 | 2021-06-07 12:02:00 +0300 | [diff] [blame] | 4265 | if (nested_vmx_load_cr3(vcpu, vmcs12->host_cr3, false, true, &ignored)) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4266 | nested_vmx_abort(vcpu, VMX_ABORT_LOAD_HOST_PDPTE_FAIL); |
| 4267 | |
Sean Christopherson | 50b265a | 2020-03-20 14:28:19 -0700 | [diff] [blame] | 4268 | nested_vmx_transition_tlb_flush(vcpu, vmcs12, false); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4269 | |
| 4270 | vmcs_write32(GUEST_SYSENTER_CS, vmcs12->host_ia32_sysenter_cs); |
| 4271 | vmcs_writel(GUEST_SYSENTER_ESP, vmcs12->host_ia32_sysenter_esp); |
| 4272 | vmcs_writel(GUEST_SYSENTER_EIP, vmcs12->host_ia32_sysenter_eip); |
| 4273 | vmcs_writel(GUEST_IDTR_BASE, vmcs12->host_idtr_base); |
| 4274 | vmcs_writel(GUEST_GDTR_BASE, vmcs12->host_gdtr_base); |
| 4275 | vmcs_write32(GUEST_IDTR_LIMIT, 0xFFFF); |
| 4276 | vmcs_write32(GUEST_GDTR_LIMIT, 0xFFFF); |
| 4277 | |
| 4278 | /* If not VM_EXIT_CLEAR_BNDCFGS, the L2 value propagates to L1. */ |
| 4279 | if (vmcs12->vm_exit_controls & VM_EXIT_CLEAR_BNDCFGS) |
| 4280 | vmcs_write64(GUEST_BNDCFGS, 0); |
| 4281 | |
| 4282 | if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PAT) { |
| 4283 | vmcs_write64(GUEST_IA32_PAT, vmcs12->host_ia32_pat); |
| 4284 | vcpu->arch.pat = vmcs12->host_ia32_pat; |
| 4285 | } |
| 4286 | if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL) |
Oliver Upton | d196842 | 2019-12-13 16:33:58 -0800 | [diff] [blame] | 4287 | WARN_ON_ONCE(kvm_set_msr(vcpu, MSR_CORE_PERF_GLOBAL_CTRL, |
| 4288 | vmcs12->host_ia32_perf_global_ctrl)); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4289 | |
| 4290 | /* Set L1 segment info according to Intel SDM |
| 4291 | 27.5.2 Loading Host Segment and Descriptor-Table Registers */ |
| 4292 | seg = (struct kvm_segment) { |
| 4293 | .base = 0, |
| 4294 | .limit = 0xFFFFFFFF, |
| 4295 | .selector = vmcs12->host_cs_selector, |
| 4296 | .type = 11, |
| 4297 | .present = 1, |
| 4298 | .s = 1, |
| 4299 | .g = 1 |
| 4300 | }; |
| 4301 | if (vmcs12->vm_exit_controls & VM_EXIT_HOST_ADDR_SPACE_SIZE) |
| 4302 | seg.l = 1; |
| 4303 | else |
| 4304 | seg.db = 1; |
Sean Christopherson | 816be9e | 2021-07-13 09:33:07 -0700 | [diff] [blame] | 4305 | __vmx_set_segment(vcpu, &seg, VCPU_SREG_CS); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4306 | seg = (struct kvm_segment) { |
| 4307 | .base = 0, |
| 4308 | .limit = 0xFFFFFFFF, |
| 4309 | .type = 3, |
| 4310 | .present = 1, |
| 4311 | .s = 1, |
| 4312 | .db = 1, |
| 4313 | .g = 1 |
| 4314 | }; |
| 4315 | seg.selector = vmcs12->host_ds_selector; |
Sean Christopherson | 816be9e | 2021-07-13 09:33:07 -0700 | [diff] [blame] | 4316 | __vmx_set_segment(vcpu, &seg, VCPU_SREG_DS); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4317 | seg.selector = vmcs12->host_es_selector; |
Sean Christopherson | 816be9e | 2021-07-13 09:33:07 -0700 | [diff] [blame] | 4318 | __vmx_set_segment(vcpu, &seg, VCPU_SREG_ES); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4319 | seg.selector = vmcs12->host_ss_selector; |
Sean Christopherson | 816be9e | 2021-07-13 09:33:07 -0700 | [diff] [blame] | 4320 | __vmx_set_segment(vcpu, &seg, VCPU_SREG_SS); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4321 | seg.selector = vmcs12->host_fs_selector; |
| 4322 | seg.base = vmcs12->host_fs_base; |
Sean Christopherson | 816be9e | 2021-07-13 09:33:07 -0700 | [diff] [blame] | 4323 | __vmx_set_segment(vcpu, &seg, VCPU_SREG_FS); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4324 | seg.selector = vmcs12->host_gs_selector; |
| 4325 | seg.base = vmcs12->host_gs_base; |
Sean Christopherson | 816be9e | 2021-07-13 09:33:07 -0700 | [diff] [blame] | 4326 | __vmx_set_segment(vcpu, &seg, VCPU_SREG_GS); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4327 | seg = (struct kvm_segment) { |
| 4328 | .base = vmcs12->host_tr_base, |
| 4329 | .limit = 0x67, |
| 4330 | .selector = vmcs12->host_tr_selector, |
| 4331 | .type = 11, |
| 4332 | .present = 1 |
| 4333 | }; |
Sean Christopherson | 816be9e | 2021-07-13 09:33:07 -0700 | [diff] [blame] | 4334 | __vmx_set_segment(vcpu, &seg, VCPU_SREG_TR); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4335 | |
Sean Christopherson | afc8de0 | 2021-07-13 09:32:40 -0700 | [diff] [blame] | 4336 | memset(&seg, 0, sizeof(seg)); |
| 4337 | seg.unusable = 1; |
Sean Christopherson | 816be9e | 2021-07-13 09:33:07 -0700 | [diff] [blame] | 4338 | __vmx_set_segment(vcpu, &seg, VCPU_SREG_LDTR); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4339 | |
| 4340 | kvm_set_dr(vcpu, 7, 0x400); |
| 4341 | vmcs_write64(GUEST_IA32_DEBUGCTL, 0); |
| 4342 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4343 | if (nested_vmx_load_msr(vcpu, vmcs12->vm_exit_msr_load_addr, |
| 4344 | vmcs12->vm_exit_msr_load_count)) |
| 4345 | nested_vmx_abort(vcpu, VMX_ABORT_LOAD_HOST_MSR_FAIL); |
Maxim Levitsky | dbab610 | 2021-09-13 17:09:54 +0300 | [diff] [blame] | 4346 | |
| 4347 | to_vmx(vcpu)->emulation_required = vmx_emulation_required(vcpu); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4348 | } |
| 4349 | |
| 4350 | static inline u64 nested_vmx_get_vmcs01_guest_efer(struct vcpu_vmx *vmx) |
| 4351 | { |
Sean Christopherson | eb3db1b | 2020-09-23 11:03:58 -0700 | [diff] [blame] | 4352 | struct vmx_uret_msr *efer_msr; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4353 | unsigned int i; |
| 4354 | |
| 4355 | if (vm_entry_controls_get(vmx) & VM_ENTRY_LOAD_IA32_EFER) |
| 4356 | return vmcs_read64(GUEST_IA32_EFER); |
| 4357 | |
| 4358 | if (cpu_has_load_ia32_efer()) |
| 4359 | return host_efer; |
| 4360 | |
| 4361 | for (i = 0; i < vmx->msr_autoload.guest.nr; ++i) { |
| 4362 | if (vmx->msr_autoload.guest.val[i].index == MSR_EFER) |
| 4363 | return vmx->msr_autoload.guest.val[i].value; |
| 4364 | } |
| 4365 | |
Sean Christopherson | d85a803 | 2020-09-23 11:04:06 -0700 | [diff] [blame] | 4366 | efer_msr = vmx_find_uret_msr(vmx, MSR_EFER); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4367 | if (efer_msr) |
| 4368 | return efer_msr->data; |
| 4369 | |
| 4370 | return host_efer; |
| 4371 | } |
| 4372 | |
| 4373 | static void nested_vmx_restore_host_state(struct kvm_vcpu *vcpu) |
| 4374 | { |
| 4375 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
| 4376 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 4377 | struct vmx_msr_entry g, h; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4378 | gpa_t gpa; |
| 4379 | u32 i, j; |
| 4380 | |
| 4381 | vcpu->arch.pat = vmcs_read64(GUEST_IA32_PAT); |
| 4382 | |
| 4383 | if (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS) { |
| 4384 | /* |
| 4385 | * L1's host DR7 is lost if KVM_GUESTDBG_USE_HW_BP is set |
| 4386 | * as vmcs01.GUEST_DR7 contains a userspace defined value |
| 4387 | * and vcpu->arch.dr7 is not squirreled away before the |
| 4388 | * nested VMENTER (not worth adding a variable in nested_vmx). |
| 4389 | */ |
| 4390 | if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP) |
| 4391 | kvm_set_dr(vcpu, 7, DR7_FIXED_1); |
| 4392 | else |
| 4393 | WARN_ON(kvm_set_dr(vcpu, 7, vmcs_readl(GUEST_DR7))); |
| 4394 | } |
| 4395 | |
| 4396 | /* |
| 4397 | * Note that calling vmx_set_{efer,cr0,cr4} is important as they |
| 4398 | * handle a variety of side effects to KVM's software model. |
| 4399 | */ |
| 4400 | vmx_set_efer(vcpu, nested_vmx_get_vmcs01_guest_efer(vmx)); |
| 4401 | |
Sean Christopherson | fa71e95 | 2020-07-02 21:04:22 -0700 | [diff] [blame] | 4402 | vcpu->arch.cr0_guest_owned_bits = KVM_POSSIBLE_CR0_GUEST_BITS; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4403 | vmx_set_cr0(vcpu, vmcs_readl(CR0_READ_SHADOW)); |
| 4404 | |
| 4405 | vcpu->arch.cr4_guest_owned_bits = ~vmcs_readl(CR4_GUEST_HOST_MASK); |
| 4406 | vmx_set_cr4(vcpu, vmcs_readl(CR4_READ_SHADOW)); |
| 4407 | |
| 4408 | nested_ept_uninit_mmu_context(vcpu); |
Sean Christopherson | f087a02 | 2019-06-07 11:55:34 -0700 | [diff] [blame] | 4409 | vcpu->arch.cr3 = vmcs_readl(GUEST_CR3); |
Sean Christopherson | cb3c1e2 | 2019-09-27 14:45:22 -0700 | [diff] [blame] | 4410 | kvm_register_mark_available(vcpu, VCPU_EXREG_CR3); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4411 | |
| 4412 | /* |
| 4413 | * Use ept_save_pdptrs(vcpu) to load the MMU's cached PDPTRs |
| 4414 | * from vmcs01 (if necessary). The PDPTRs are not loaded on |
| 4415 | * VMFail, like everything else we just need to ensure our |
| 4416 | * software model is up-to-date. |
| 4417 | */ |
Sean Christopherson | 9932b49 | 2020-04-15 13:34:50 -0700 | [diff] [blame] | 4418 | if (enable_ept && is_pae_paging(vcpu)) |
Sean Christopherson | f087a02 | 2019-06-07 11:55:34 -0700 | [diff] [blame] | 4419 | ept_save_pdptrs(vcpu); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4420 | |
| 4421 | kvm_mmu_reset_context(vcpu); |
| 4422 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4423 | /* |
| 4424 | * This nasty bit of open coding is a compromise between blindly |
| 4425 | * loading L1's MSRs using the exit load lists (incorrect emulation |
| 4426 | * of VMFail), leaving the nested VM's MSRs in the software model |
| 4427 | * (incorrect behavior) and snapshotting the modified MSRs (too |
| 4428 | * expensive since the lists are unbound by hardware). For each |
| 4429 | * MSR that was (prematurely) loaded from the nested VMEntry load |
| 4430 | * list, reload it from the exit load list if it exists and differs |
| 4431 | * from the guest value. The intent is to stuff host state as |
| 4432 | * silently as possible, not to fully process the exit load list. |
| 4433 | */ |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4434 | for (i = 0; i < vmcs12->vm_entry_msr_load_count; i++) { |
| 4435 | gpa = vmcs12->vm_entry_msr_load_addr + (i * sizeof(g)); |
| 4436 | if (kvm_vcpu_read_guest(vcpu, gpa, &g, sizeof(g))) { |
| 4437 | pr_debug_ratelimited( |
| 4438 | "%s read MSR index failed (%u, 0x%08llx)\n", |
| 4439 | __func__, i, gpa); |
| 4440 | goto vmabort; |
| 4441 | } |
| 4442 | |
| 4443 | for (j = 0; j < vmcs12->vm_exit_msr_load_count; j++) { |
| 4444 | gpa = vmcs12->vm_exit_msr_load_addr + (j * sizeof(h)); |
| 4445 | if (kvm_vcpu_read_guest(vcpu, gpa, &h, sizeof(h))) { |
| 4446 | pr_debug_ratelimited( |
| 4447 | "%s read MSR failed (%u, 0x%08llx)\n", |
| 4448 | __func__, j, gpa); |
| 4449 | goto vmabort; |
| 4450 | } |
| 4451 | if (h.index != g.index) |
| 4452 | continue; |
| 4453 | if (h.value == g.value) |
| 4454 | break; |
| 4455 | |
| 4456 | if (nested_vmx_load_msr_check(vcpu, &h)) { |
| 4457 | pr_debug_ratelimited( |
| 4458 | "%s check failed (%u, 0x%x, 0x%x)\n", |
| 4459 | __func__, j, h.index, h.reserved); |
| 4460 | goto vmabort; |
| 4461 | } |
| 4462 | |
Sean Christopherson | f20935d | 2019-09-05 14:22:54 -0700 | [diff] [blame] | 4463 | if (kvm_set_msr(vcpu, h.index, h.value)) { |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4464 | pr_debug_ratelimited( |
| 4465 | "%s WRMSR failed (%u, 0x%x, 0x%llx)\n", |
| 4466 | __func__, j, h.index, h.value); |
| 4467 | goto vmabort; |
| 4468 | } |
| 4469 | } |
| 4470 | } |
| 4471 | |
| 4472 | return; |
| 4473 | |
| 4474 | vmabort: |
| 4475 | nested_vmx_abort(vcpu, VMX_ABORT_LOAD_HOST_MSR_FAIL); |
| 4476 | } |
| 4477 | |
| 4478 | /* |
| 4479 | * Emulate an exit from nested guest (L2) to L1, i.e., prepare to run L1 |
| 4480 | * and modify vmcs12 to make it see what it would expect to see there if |
| 4481 | * L2 was its real guest. Must only be called when in L2 (is_guest_mode()) |
| 4482 | */ |
Sean Christopherson | 4dcefa3 | 2020-04-15 10:55:18 -0700 | [diff] [blame] | 4483 | void nested_vmx_vmexit(struct kvm_vcpu *vcpu, u32 vm_exit_reason, |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4484 | u32 exit_intr_info, unsigned long exit_qualification) |
| 4485 | { |
| 4486 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 4487 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
| 4488 | |
| 4489 | /* trying to cancel vmlaunch/vmresume is a bug */ |
| 4490 | WARN_ON_ONCE(vmx->nested.nested_run_pending); |
| 4491 | |
Sean Christopherson | cb6a32c | 2021-03-02 09:45:14 -0800 | [diff] [blame] | 4492 | /* Similarly, triple faults in L2 should never escape. */ |
| 4493 | WARN_ON_ONCE(kvm_check_request(KVM_REQ_TRIPLE_FAULT, vcpu)); |
| 4494 | |
Vitaly Kuznetsov | f5c7e84 | 2021-05-03 17:08:51 +0200 | [diff] [blame] | 4495 | if (kvm_check_request(KVM_REQ_GET_NESTED_STATE_PAGES, vcpu)) { |
| 4496 | /* |
| 4497 | * KVM_REQ_GET_NESTED_STATE_PAGES is also used to map |
| 4498 | * Enlightened VMCS after migration and we still need to |
| 4499 | * do that when something is forcing L2->L1 exit prior to |
| 4500 | * the first L2 run. |
| 4501 | */ |
| 4502 | (void)nested_get_evmcs_page(vcpu); |
| 4503 | } |
Maxim Levitsky | f2c7ef3 | 2021-01-07 11:38:51 +0200 | [diff] [blame] | 4504 | |
Sean Christopherson | eeeb4f6 | 2020-03-20 14:28:20 -0700 | [diff] [blame] | 4505 | /* Service the TLB flush request for L2 before switching to L1. */ |
| 4506 | if (kvm_check_request(KVM_REQ_TLB_FLUSH_CURRENT, vcpu)) |
| 4507 | kvm_vcpu_flush_tlb_current(vcpu); |
| 4508 | |
Peter Shier | 43fea4e | 2020-08-20 16:05:45 -0700 | [diff] [blame] | 4509 | /* |
| 4510 | * VCPU_EXREG_PDPTR will be clobbered in arch/x86/kvm/vmx/vmx.h between |
| 4511 | * now and the new vmentry. Ensure that the VMCS02 PDPTR fields are |
| 4512 | * up-to-date before switching to L1. |
| 4513 | */ |
| 4514 | if (enable_ept && is_pae_paging(vcpu)) |
| 4515 | vmx_ept_load_pdptrs(vcpu); |
| 4516 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4517 | leave_guest_mode(vcpu); |
| 4518 | |
Paolo Bonzini | b4b65b5 | 2019-01-29 19:12:35 +0100 | [diff] [blame] | 4519 | if (nested_cpu_has_preemption_timer(vmcs12)) |
| 4520 | hrtimer_cancel(&to_vmx(vcpu)->nested.preemption_timer); |
| 4521 | |
Ilias Stamatis | d041b5e | 2021-05-26 19:44:17 +0100 | [diff] [blame] | 4522 | if (nested_cpu_has(vmcs12, CPU_BASED_USE_TSC_OFFSETTING)) { |
| 4523 | vcpu->arch.tsc_offset = vcpu->arch.l1_tsc_offset; |
| 4524 | if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_TSC_SCALING)) |
| 4525 | vcpu->arch.tsc_scaling_ratio = vcpu->arch.l1_tsc_scaling_ratio; |
| 4526 | } |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4527 | |
| 4528 | if (likely(!vmx->fail)) { |
Sean Christopherson | 3731905ef | 2019-05-07 08:36:27 -0700 | [diff] [blame] | 4529 | sync_vmcs02_to_vmcs12(vcpu, vmcs12); |
Sean Christopherson | f4f8316 | 2019-05-07 08:36:26 -0700 | [diff] [blame] | 4530 | |
Sean Christopherson | 4dcefa3 | 2020-04-15 10:55:18 -0700 | [diff] [blame] | 4531 | if (vm_exit_reason != -1) |
| 4532 | prepare_vmcs12(vcpu, vmcs12, vm_exit_reason, |
| 4533 | exit_intr_info, exit_qualification); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4534 | |
| 4535 | /* |
Sean Christopherson | 3731905ef | 2019-05-07 08:36:27 -0700 | [diff] [blame] | 4536 | * Must happen outside of sync_vmcs02_to_vmcs12() as it will |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4537 | * also be used to capture vmcs12 cache as part of |
| 4538 | * capturing nVMX state for snapshot (migration). |
| 4539 | * |
| 4540 | * Otherwise, this flush will dirty guest memory at a |
| 4541 | * point it is already assumed by user-space to be |
| 4542 | * immutable. |
| 4543 | */ |
| 4544 | nested_flush_cached_shadow_vmcs12(vcpu, vmcs12); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4545 | } else { |
| 4546 | /* |
| 4547 | * The only expected VM-instruction error is "VM entry with |
| 4548 | * invalid control field(s)." Anything else indicates a |
| 4549 | * problem with L0. And we should never get here with a |
| 4550 | * VMFail of any type if early consistency checks are enabled. |
| 4551 | */ |
| 4552 | WARN_ON_ONCE(vmcs_read32(VM_INSTRUCTION_ERROR) != |
| 4553 | VMXERR_ENTRY_INVALID_CONTROL_FIELD); |
| 4554 | WARN_ON_ONCE(nested_early_check); |
| 4555 | } |
| 4556 | |
| 4557 | vmx_switch_vmcs(vcpu, &vmx->vmcs01); |
| 4558 | |
| 4559 | /* Update any VMCS fields that might have changed while L2 ran */ |
| 4560 | vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, vmx->msr_autoload.host.nr); |
| 4561 | vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, vmx->msr_autoload.guest.nr); |
| 4562 | vmcs_write64(TSC_OFFSET, vcpu->arch.tsc_offset); |
Ilias Stamatis | 1ab9287 | 2021-06-07 11:54:38 +0100 | [diff] [blame] | 4563 | if (kvm_has_tsc_control) |
| 4564 | vmcs_write64(TSC_MULTIPLIER, vcpu->arch.tsc_scaling_ratio); |
| 4565 | |
Liran Alon | 02d496cf | 2019-11-11 14:30:55 +0200 | [diff] [blame] | 4566 | if (vmx->nested.l1_tpr_threshold != -1) |
| 4567 | vmcs_write32(TPR_THRESHOLD, vmx->nested.l1_tpr_threshold); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4568 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4569 | if (vmx->nested.change_vmcs01_virtual_apic_mode) { |
| 4570 | vmx->nested.change_vmcs01_virtual_apic_mode = false; |
| 4571 | vmx_set_virtual_apic_mode(vcpu); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4572 | } |
| 4573 | |
Makarand Sonare | a85863c | 2021-02-12 16:50:12 -0800 | [diff] [blame] | 4574 | if (vmx->nested.update_vmcs01_cpu_dirty_logging) { |
| 4575 | vmx->nested.update_vmcs01_cpu_dirty_logging = false; |
| 4576 | vmx_update_cpu_dirty_logging(vcpu); |
| 4577 | } |
| 4578 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4579 | /* Unpin physical memory we referred to in vmcs02 */ |
| 4580 | if (vmx->nested.apic_access_page) { |
Liran Alon | b11494b | 2019-11-21 00:31:47 +0200 | [diff] [blame] | 4581 | kvm_release_page_clean(vmx->nested.apic_access_page); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4582 | vmx->nested.apic_access_page = NULL; |
| 4583 | } |
KarimAllah Ahmed | 96c66e8 | 2019-01-31 21:24:37 +0100 | [diff] [blame] | 4584 | kvm_vcpu_unmap(vcpu, &vmx->nested.virtual_apic_map, true); |
KarimAllah Ahmed | 3278e04 | 2019-01-31 21:24:38 +0100 | [diff] [blame] | 4585 | kvm_vcpu_unmap(vcpu, &vmx->nested.pi_desc_map, true); |
| 4586 | vmx->nested.pi_desc = NULL; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4587 | |
Sean Christopherson | 1196cb9 | 2020-03-20 14:28:23 -0700 | [diff] [blame] | 4588 | if (vmx->nested.reload_vmcs01_apic_access_page) { |
| 4589 | vmx->nested.reload_vmcs01_apic_access_page = false; |
| 4590 | kvm_make_request(KVM_REQ_APIC_PAGE_RELOAD, vcpu); |
| 4591 | } |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4592 | |
Sean Christopherson | 4dcefa3 | 2020-04-15 10:55:18 -0700 | [diff] [blame] | 4593 | if ((vm_exit_reason != -1) && |
Vitaly Kuznetsov | 1e9dfbd | 2021-05-26 15:20:16 +0200 | [diff] [blame] | 4594 | (enable_shadow_vmcs || evmptr_is_valid(vmx->nested.hv_evmcs_vmptr))) |
Sean Christopherson | 3731905ef | 2019-05-07 08:36:27 -0700 | [diff] [blame] | 4595 | vmx->nested.need_vmcs12_to_shadow_sync = true; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4596 | |
| 4597 | /* in case we halted in L2 */ |
| 4598 | vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE; |
| 4599 | |
| 4600 | if (likely(!vmx->fail)) { |
Sean Christopherson | 4dcefa3 | 2020-04-15 10:55:18 -0700 | [diff] [blame] | 4601 | if ((u16)vm_exit_reason == EXIT_REASON_EXTERNAL_INTERRUPT && |
Sean Christopherson | a1c77ab | 2020-03-02 22:27:35 -0800 | [diff] [blame] | 4602 | nested_exit_intr_ack_set(vcpu)) { |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4603 | int irq = kvm_cpu_get_interrupt(vcpu); |
| 4604 | WARN_ON(irq < 0); |
| 4605 | vmcs12->vm_exit_intr_info = irq | |
| 4606 | INTR_INFO_VALID_MASK | INTR_TYPE_EXT_INTR; |
| 4607 | } |
| 4608 | |
Sean Christopherson | 4dcefa3 | 2020-04-15 10:55:18 -0700 | [diff] [blame] | 4609 | if (vm_exit_reason != -1) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4610 | trace_kvm_nested_vmexit_inject(vmcs12->vm_exit_reason, |
| 4611 | vmcs12->exit_qualification, |
| 4612 | vmcs12->idt_vectoring_info_field, |
| 4613 | vmcs12->vm_exit_intr_info, |
| 4614 | vmcs12->vm_exit_intr_error_code, |
| 4615 | KVM_ISA_VMX); |
| 4616 | |
| 4617 | load_vmcs12_host_state(vcpu, vmcs12); |
| 4618 | |
| 4619 | return; |
| 4620 | } |
| 4621 | |
| 4622 | /* |
| 4623 | * After an early L2 VM-entry failure, we're now back |
| 4624 | * in L1 which thinks it just finished a VMLAUNCH or |
| 4625 | * VMRESUME instruction, so we need to set the failure |
| 4626 | * flag and the VM-instruction error field of the VMCS |
| 4627 | * accordingly, and skip the emulated instruction. |
| 4628 | */ |
Sean Christopherson | b2656e4 | 2020-06-08 18:56:07 -0700 | [diff] [blame] | 4629 | (void)nested_vmx_fail(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4630 | |
| 4631 | /* |
| 4632 | * Restore L1's host state to KVM's software model. We're here |
| 4633 | * because a consistency check was caught by hardware, which |
| 4634 | * means some amount of guest state has been propagated to KVM's |
| 4635 | * model and needs to be unwound to the host's state. |
| 4636 | */ |
| 4637 | nested_vmx_restore_host_state(vcpu); |
| 4638 | |
| 4639 | vmx->fail = 0; |
| 4640 | } |
| 4641 | |
Sean Christopherson | cb6a32c | 2021-03-02 09:45:14 -0800 | [diff] [blame] | 4642 | static void nested_vmx_triple_fault(struct kvm_vcpu *vcpu) |
| 4643 | { |
| 4644 | nested_vmx_vmexit(vcpu, EXIT_REASON_TRIPLE_FAULT, 0, 0); |
| 4645 | } |
| 4646 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4647 | /* |
| 4648 | * Decode the memory-address operand of a vmx instruction, as recorded on an |
| 4649 | * exit caused by such an instruction (run by a guest hypervisor). |
| 4650 | * 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] | 4651 | * #UD, #GP, or #SS. |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4652 | */ |
| 4653 | 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] | 4654 | u32 vmx_instruction_info, bool wr, int len, gva_t *ret) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4655 | { |
| 4656 | gva_t off; |
| 4657 | bool exn; |
| 4658 | struct kvm_segment s; |
| 4659 | |
| 4660 | /* |
| 4661 | * According to Vol. 3B, "Information for VM Exits Due to Instruction |
| 4662 | * Execution", on an exit, vmx_instruction_info holds most of the |
| 4663 | * addressing components of the operand. Only the displacement part |
| 4664 | * is put in exit_qualification (see 3B, "Basic VM-Exit Information"). |
| 4665 | * For how an actual address is calculated from all these components, |
| 4666 | * refer to Vol. 1, "Operand Addressing". |
| 4667 | */ |
| 4668 | int scaling = vmx_instruction_info & 3; |
| 4669 | int addr_size = (vmx_instruction_info >> 7) & 7; |
| 4670 | bool is_reg = vmx_instruction_info & (1u << 10); |
| 4671 | int seg_reg = (vmx_instruction_info >> 15) & 7; |
| 4672 | int index_reg = (vmx_instruction_info >> 18) & 0xf; |
| 4673 | bool index_is_valid = !(vmx_instruction_info & (1u << 22)); |
| 4674 | int base_reg = (vmx_instruction_info >> 23) & 0xf; |
| 4675 | bool base_is_valid = !(vmx_instruction_info & (1u << 27)); |
| 4676 | |
| 4677 | if (is_reg) { |
| 4678 | kvm_queue_exception(vcpu, UD_VECTOR); |
| 4679 | return 1; |
| 4680 | } |
| 4681 | |
| 4682 | /* Addr = segment_base + offset */ |
| 4683 | /* offset = base + [index * scale] + displacement */ |
| 4684 | off = exit_qualification; /* holds the displacement */ |
Sean Christopherson | 946c522 | 2019-01-23 14:39:23 -0800 | [diff] [blame] | 4685 | if (addr_size == 1) |
| 4686 | off = (gva_t)sign_extend64(off, 31); |
| 4687 | else if (addr_size == 0) |
| 4688 | off = (gva_t)sign_extend64(off, 15); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4689 | if (base_is_valid) |
| 4690 | off += kvm_register_read(vcpu, base_reg); |
| 4691 | if (index_is_valid) |
Miaohe Lin | e630269 | 2020-02-15 10:44:22 +0800 | [diff] [blame] | 4692 | off += kvm_register_read(vcpu, index_reg) << scaling; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4693 | vmx_get_segment(vcpu, &s, seg_reg); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4694 | |
Sean Christopherson | 8570f9e | 2019-01-23 14:39:24 -0800 | [diff] [blame] | 4695 | /* |
| 4696 | * The effective address, i.e. @off, of a memory operand is truncated |
| 4697 | * based on the address size of the instruction. Note that this is |
| 4698 | * the *effective address*, i.e. the address prior to accounting for |
| 4699 | * the segment's base. |
| 4700 | */ |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4701 | if (addr_size == 1) /* 32 bit */ |
Sean Christopherson | 8570f9e | 2019-01-23 14:39:24 -0800 | [diff] [blame] | 4702 | off &= 0xffffffff; |
| 4703 | else if (addr_size == 0) /* 16 bit */ |
| 4704 | off &= 0xffff; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4705 | |
| 4706 | /* Checks for #GP/#SS exceptions. */ |
| 4707 | exn = false; |
| 4708 | if (is_long_mode(vcpu)) { |
Sean Christopherson | 8570f9e | 2019-01-23 14:39:24 -0800 | [diff] [blame] | 4709 | /* |
| 4710 | * The virtual/linear address is never truncated in 64-bit |
| 4711 | * mode, e.g. a 32-bit address size can yield a 64-bit virtual |
| 4712 | * address when using FS/GS with a non-zero base. |
| 4713 | */ |
Liran Alon | 6694e48 | 2019-07-15 18:47:44 +0300 | [diff] [blame] | 4714 | if (seg_reg == VCPU_SREG_FS || seg_reg == VCPU_SREG_GS) |
| 4715 | *ret = s.base + off; |
| 4716 | else |
| 4717 | *ret = off; |
Sean Christopherson | 8570f9e | 2019-01-23 14:39:24 -0800 | [diff] [blame] | 4718 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4719 | /* Long mode: #GP(0)/#SS(0) if the memory address is in a |
| 4720 | * non-canonical form. This is the only check on the memory |
| 4721 | * destination for long mode! |
| 4722 | */ |
| 4723 | exn = is_noncanonical_address(*ret, vcpu); |
Paolo Bonzini | e0dfacb | 2019-01-30 17:25:38 +0100 | [diff] [blame] | 4724 | } else { |
Sean Christopherson | 8570f9e | 2019-01-23 14:39:24 -0800 | [diff] [blame] | 4725 | /* |
| 4726 | * When not in long mode, the virtual/linear address is |
| 4727 | * unconditionally truncated to 32 bits regardless of the |
| 4728 | * address size. |
| 4729 | */ |
| 4730 | *ret = (s.base + off) & 0xffffffff; |
| 4731 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4732 | /* Protected mode: apply checks for segment validity in the |
| 4733 | * following order: |
| 4734 | * - segment type check (#GP(0) may be thrown) |
| 4735 | * - usability check (#GP(0)/#SS(0)) |
| 4736 | * - limit check (#GP(0)/#SS(0)) |
| 4737 | */ |
| 4738 | if (wr) |
| 4739 | /* #GP(0) if the destination operand is located in a |
| 4740 | * read-only data segment or any code segment. |
| 4741 | */ |
| 4742 | exn = ((s.type & 0xa) == 0 || (s.type & 8)); |
| 4743 | else |
| 4744 | /* #GP(0) if the source operand is located in an |
| 4745 | * execute-only code segment |
| 4746 | */ |
| 4747 | exn = ((s.type & 0xa) == 8); |
| 4748 | if (exn) { |
| 4749 | kvm_queue_exception_e(vcpu, GP_VECTOR, 0); |
| 4750 | return 1; |
| 4751 | } |
| 4752 | /* Protected mode: #GP(0)/#SS(0) if the segment is unusable. |
| 4753 | */ |
| 4754 | exn = (s.unusable != 0); |
Sean Christopherson | 34333cc | 2019-01-23 14:39:25 -0800 | [diff] [blame] | 4755 | |
| 4756 | /* |
| 4757 | * Protected mode: #GP(0)/#SS(0) if the memory operand is |
| 4758 | * outside the segment limit. All CPUs that support VMX ignore |
| 4759 | * limit checks for flat segments, i.e. segments with base==0, |
| 4760 | * limit==0xffffffff and of type expand-up data or code. |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4761 | */ |
Sean Christopherson | 34333cc | 2019-01-23 14:39:25 -0800 | [diff] [blame] | 4762 | if (!(s.base == 0 && s.limit == 0xffffffff && |
| 4763 | ((s.type & 8) || !(s.type & 4)))) |
Eugene Korenevsky | fdb2861 | 2019-06-06 00:19:16 +0300 | [diff] [blame] | 4764 | exn = exn || ((u64)off + len - 1 > s.limit); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4765 | } |
| 4766 | if (exn) { |
| 4767 | kvm_queue_exception_e(vcpu, |
| 4768 | seg_reg == VCPU_SREG_SS ? |
| 4769 | SS_VECTOR : GP_VECTOR, |
| 4770 | 0); |
| 4771 | return 1; |
| 4772 | } |
| 4773 | |
| 4774 | return 0; |
| 4775 | } |
| 4776 | |
Oliver Upton | 03a8871a | 2019-11-13 16:17:20 -0800 | [diff] [blame] | 4777 | void nested_vmx_pmu_entry_exit_ctls_update(struct kvm_vcpu *vcpu) |
| 4778 | { |
| 4779 | struct vcpu_vmx *vmx; |
| 4780 | |
| 4781 | if (!nested_vmx_allowed(vcpu)) |
| 4782 | return; |
| 4783 | |
| 4784 | vmx = to_vmx(vcpu); |
Sean Christopherson | afaf0b2 | 2020-03-21 13:26:00 -0700 | [diff] [blame] | 4785 | 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] | 4786 | vmx->nested.msrs.entry_ctls_high |= |
| 4787 | VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL; |
| 4788 | vmx->nested.msrs.exit_ctls_high |= |
| 4789 | VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL; |
| 4790 | } else { |
| 4791 | vmx->nested.msrs.entry_ctls_high &= |
| 4792 | ~VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL; |
| 4793 | vmx->nested.msrs.exit_ctls_high &= |
Chenyi Qiang | c6b177a | 2020-08-28 16:56:21 +0800 | [diff] [blame] | 4794 | ~VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL; |
Oliver Upton | 03a8871a | 2019-11-13 16:17:20 -0800 | [diff] [blame] | 4795 | } |
| 4796 | } |
| 4797 | |
Vitaly Kuznetsov | 7a35e51 | 2020-06-05 13:59:05 +0200 | [diff] [blame] | 4798 | static int nested_vmx_get_vmptr(struct kvm_vcpu *vcpu, gpa_t *vmpointer, |
| 4799 | int *ret) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4800 | { |
| 4801 | gva_t gva; |
| 4802 | struct x86_exception e; |
Vitaly Kuznetsov | 7a35e51 | 2020-06-05 13:59:05 +0200 | [diff] [blame] | 4803 | int r; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4804 | |
Sean Christopherson | 5addc23 | 2020-04-15 13:34:53 -0700 | [diff] [blame] | 4805 | if (get_vmx_mem_address(vcpu, vmx_get_exit_qual(vcpu), |
Eugene Korenevsky | fdb2861 | 2019-06-06 00:19:16 +0300 | [diff] [blame] | 4806 | vmcs_read32(VMX_INSTRUCTION_INFO), false, |
Vitaly Kuznetsov | 7a35e51 | 2020-06-05 13:59:05 +0200 | [diff] [blame] | 4807 | sizeof(*vmpointer), &gva)) { |
| 4808 | *ret = 1; |
| 4809 | return -EINVAL; |
| 4810 | } |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4811 | |
Vitaly Kuznetsov | 7a35e51 | 2020-06-05 13:59:05 +0200 | [diff] [blame] | 4812 | r = kvm_read_guest_virt(vcpu, gva, vmpointer, sizeof(*vmpointer), &e); |
| 4813 | if (r != X86EMUL_CONTINUE) { |
Babu Moger | 3f3393b | 2020-09-11 14:29:05 -0500 | [diff] [blame] | 4814 | *ret = kvm_handle_memory_failure(vcpu, r, &e); |
Vitaly Kuznetsov | 7a35e51 | 2020-06-05 13:59:05 +0200 | [diff] [blame] | 4815 | return -EINVAL; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4816 | } |
| 4817 | |
| 4818 | return 0; |
| 4819 | } |
| 4820 | |
| 4821 | /* |
| 4822 | * Allocate a shadow VMCS and associate it with the currently loaded |
| 4823 | * VMCS, unless such a shadow VMCS already exists. The newly allocated |
| 4824 | * VMCS is also VMCLEARed, so that it is ready for use. |
| 4825 | */ |
| 4826 | static struct vmcs *alloc_shadow_vmcs(struct kvm_vcpu *vcpu) |
| 4827 | { |
| 4828 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 4829 | struct loaded_vmcs *loaded_vmcs = vmx->loaded_vmcs; |
| 4830 | |
| 4831 | /* |
| 4832 | * We should allocate a shadow vmcs for vmcs01 only when L1 |
| 4833 | * executes VMXON and free it when L1 executes VMXOFF. |
| 4834 | * As it is invalid to execute VMXON twice, we shouldn't reach |
| 4835 | * here when vmcs01 already have an allocated shadow vmcs. |
| 4836 | */ |
| 4837 | WARN_ON(loaded_vmcs == &vmx->vmcs01 && loaded_vmcs->shadow_vmcs); |
| 4838 | |
| 4839 | if (!loaded_vmcs->shadow_vmcs) { |
| 4840 | loaded_vmcs->shadow_vmcs = alloc_vmcs(true); |
| 4841 | if (loaded_vmcs->shadow_vmcs) |
| 4842 | vmcs_clear(loaded_vmcs->shadow_vmcs); |
| 4843 | } |
| 4844 | return loaded_vmcs->shadow_vmcs; |
| 4845 | } |
| 4846 | |
| 4847 | static int enter_vmx_operation(struct kvm_vcpu *vcpu) |
| 4848 | { |
| 4849 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 4850 | int r; |
| 4851 | |
| 4852 | r = alloc_loaded_vmcs(&vmx->nested.vmcs02); |
| 4853 | if (r < 0) |
| 4854 | goto out_vmcs02; |
| 4855 | |
Ben Gardon | 4183683 | 2019-02-11 11:02:52 -0800 | [diff] [blame] | 4856 | vmx->nested.cached_vmcs12 = kzalloc(VMCS12_SIZE, GFP_KERNEL_ACCOUNT); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4857 | if (!vmx->nested.cached_vmcs12) |
| 4858 | goto out_cached_vmcs12; |
| 4859 | |
Paolo Bonzini | 8503fea | 2021-11-22 18:20:16 -0500 | [diff] [blame^] | 4860 | vmx->nested.shadow_vmcs12_cache.gpa = INVALID_GPA; |
Ben Gardon | 4183683 | 2019-02-11 11:02:52 -0800 | [diff] [blame] | 4861 | vmx->nested.cached_shadow_vmcs12 = kzalloc(VMCS12_SIZE, GFP_KERNEL_ACCOUNT); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4862 | if (!vmx->nested.cached_shadow_vmcs12) |
| 4863 | goto out_cached_shadow_vmcs12; |
| 4864 | |
| 4865 | if (enable_shadow_vmcs && !alloc_shadow_vmcs(vcpu)) |
| 4866 | goto out_shadow_vmcs; |
| 4867 | |
| 4868 | hrtimer_init(&vmx->nested.preemption_timer, CLOCK_MONOTONIC, |
Jim Mattson | ada0098 | 2020-05-08 13:36:42 -0700 | [diff] [blame] | 4869 | HRTIMER_MODE_ABS_PINNED); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4870 | vmx->nested.preemption_timer.function = vmx_preemption_timer_fn; |
| 4871 | |
| 4872 | vmx->nested.vpid02 = allocate_vpid(); |
| 4873 | |
| 4874 | vmx->nested.vmcs02_initialized = false; |
| 4875 | vmx->nested.vmxon = true; |
Luwei Kang | ee85dec | 2018-10-24 16:05:16 +0800 | [diff] [blame] | 4876 | |
Sean Christopherson | 2ef7619 | 2020-03-02 15:56:22 -0800 | [diff] [blame] | 4877 | if (vmx_pt_mode_is_host_guest()) { |
Luwei Kang | ee85dec | 2018-10-24 16:05:16 +0800 | [diff] [blame] | 4878 | vmx->pt_desc.guest.ctl = 0; |
Aaron Lewis | 476c9bd | 2020-09-25 16:34:18 +0200 | [diff] [blame] | 4879 | pt_update_intercept_for_msr(vcpu); |
Luwei Kang | ee85dec | 2018-10-24 16:05:16 +0800 | [diff] [blame] | 4880 | } |
| 4881 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4882 | return 0; |
| 4883 | |
| 4884 | out_shadow_vmcs: |
| 4885 | kfree(vmx->nested.cached_shadow_vmcs12); |
| 4886 | |
| 4887 | out_cached_shadow_vmcs12: |
| 4888 | kfree(vmx->nested.cached_vmcs12); |
| 4889 | |
| 4890 | out_cached_vmcs12: |
| 4891 | free_loaded_vmcs(&vmx->nested.vmcs02); |
| 4892 | |
| 4893 | out_vmcs02: |
| 4894 | return -ENOMEM; |
| 4895 | } |
| 4896 | |
Yu Zhang | ed7023a | 2021-09-09 01:17:31 +0800 | [diff] [blame] | 4897 | /* Emulate the VMXON instruction. */ |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4898 | static int handle_vmon(struct kvm_vcpu *vcpu) |
| 4899 | { |
| 4900 | int ret; |
| 4901 | gpa_t vmptr; |
KarimAllah Ahmed | 2e40893 | 2019-01-31 21:24:31 +0100 | [diff] [blame] | 4902 | uint32_t revision; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4903 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Sean Christopherson | 32ad73d | 2019-12-20 20:44:55 -0800 | [diff] [blame] | 4904 | const u64 VMXON_NEEDED_FEATURES = FEAT_CTL_LOCKED |
| 4905 | | FEAT_CTL_VMX_ENABLED_OUTSIDE_SMX; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4906 | |
| 4907 | /* |
| 4908 | * The Intel VMX Instruction Reference lists a bunch of bits that are |
| 4909 | * prerequisite to running VMXON, most notably cr4.VMXE must be set to |
Sean Christopherson | c2fe3cd | 2020-10-06 18:44:15 -0700 | [diff] [blame] | 4910 | * 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] | 4911 | * Otherwise, we should fail with #UD. But most faulting conditions |
| 4912 | * have already been checked by hardware, prior to the VM-exit for |
| 4913 | * VMXON. We do test guest cr4.VMXE because processor CR4 always has |
| 4914 | * that bit set to 1 in non-root mode. |
| 4915 | */ |
| 4916 | if (!kvm_read_cr4_bits(vcpu, X86_CR4_VMXE)) { |
| 4917 | kvm_queue_exception(vcpu, UD_VECTOR); |
| 4918 | return 1; |
| 4919 | } |
| 4920 | |
| 4921 | /* CPL=0 must be checked manually. */ |
| 4922 | if (vmx_get_cpl(vcpu)) { |
| 4923 | kvm_inject_gp(vcpu, 0); |
| 4924 | return 1; |
| 4925 | } |
| 4926 | |
| 4927 | if (vmx->nested.vmxon) |
Sean Christopherson | b2656e4 | 2020-06-08 18:56:07 -0700 | [diff] [blame] | 4928 | return nested_vmx_fail(vcpu, VMXERR_VMXON_IN_VMX_ROOT_OPERATION); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4929 | |
| 4930 | if ((vmx->msr_ia32_feature_control & VMXON_NEEDED_FEATURES) |
| 4931 | != VMXON_NEEDED_FEATURES) { |
| 4932 | kvm_inject_gp(vcpu, 0); |
| 4933 | return 1; |
| 4934 | } |
| 4935 | |
Vitaly Kuznetsov | 7a35e51 | 2020-06-05 13:59:05 +0200 | [diff] [blame] | 4936 | if (nested_vmx_get_vmptr(vcpu, &vmptr, &ret)) |
| 4937 | return ret; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4938 | |
| 4939 | /* |
| 4940 | * SDM 3: 24.11.5 |
| 4941 | * The first 4 bytes of VMXON region contain the supported |
| 4942 | * VMCS revision identifier |
| 4943 | * |
| 4944 | * Note - IA32_VMX_BASIC[48] will never be 1 for the nested case; |
| 4945 | * which replaces physical address width with 32 |
| 4946 | */ |
KarimAllah Ahmed | e0bf266 | 2019-01-31 21:24:43 +0100 | [diff] [blame] | 4947 | if (!page_address_valid(vcpu, vmptr)) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4948 | return nested_vmx_failInvalid(vcpu); |
| 4949 | |
KarimAllah Ahmed | 2e40893 | 2019-01-31 21:24:31 +0100 | [diff] [blame] | 4950 | if (kvm_read_guest(vcpu->kvm, vmptr, &revision, sizeof(revision)) || |
| 4951 | revision != VMCS12_REVISION) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4952 | return nested_vmx_failInvalid(vcpu); |
| 4953 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4954 | vmx->nested.vmxon_ptr = vmptr; |
| 4955 | ret = enter_vmx_operation(vcpu); |
| 4956 | if (ret) |
| 4957 | return ret; |
| 4958 | |
| 4959 | return nested_vmx_succeed(vcpu); |
| 4960 | } |
| 4961 | |
| 4962 | static inline void nested_release_vmcs12(struct kvm_vcpu *vcpu) |
| 4963 | { |
| 4964 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 4965 | |
Yu Zhang | 64c7850 | 2021-09-30 01:51:53 +0800 | [diff] [blame] | 4966 | if (vmx->nested.current_vmptr == INVALID_GPA) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4967 | return; |
| 4968 | |
Sean Christopherson | 7952d76 | 2019-05-07 08:36:29 -0700 | [diff] [blame] | 4969 | copy_vmcs02_to_vmcs12_rare(vcpu, get_vmcs12(vcpu)); |
| 4970 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4971 | if (enable_shadow_vmcs) { |
| 4972 | /* copy to memory all shadowed fields in case |
| 4973 | they were modified */ |
| 4974 | copy_shadow_to_vmcs12(vmx); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4975 | vmx_disable_shadow_vmcs(vmx); |
| 4976 | } |
| 4977 | vmx->nested.posted_intr_nv = -1; |
| 4978 | |
| 4979 | /* Flush VMCS12 to guest memory */ |
| 4980 | kvm_vcpu_write_guest_page(vcpu, |
| 4981 | vmx->nested.current_vmptr >> PAGE_SHIFT, |
| 4982 | vmx->nested.cached_vmcs12, 0, VMCS12_SIZE); |
| 4983 | |
| 4984 | kvm_mmu_free_roots(vcpu, &vcpu->arch.guest_mmu, KVM_MMU_ROOTS_ALL); |
| 4985 | |
Yu Zhang | 64c7850 | 2021-09-30 01:51:53 +0800 | [diff] [blame] | 4986 | vmx->nested.current_vmptr = INVALID_GPA; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4987 | } |
| 4988 | |
| 4989 | /* Emulate the VMXOFF instruction */ |
| 4990 | static int handle_vmoff(struct kvm_vcpu *vcpu) |
| 4991 | { |
| 4992 | if (!nested_vmx_check_permission(vcpu)) |
| 4993 | return 1; |
Liran Alon | 4b9852f | 2019-08-26 13:24:49 +0300 | [diff] [blame] | 4994 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4995 | free_nested(vcpu); |
Liran Alon | 4b9852f | 2019-08-26 13:24:49 +0300 | [diff] [blame] | 4996 | |
| 4997 | /* Process a latched INIT during time CPU was in VMX operation */ |
| 4998 | kvm_make_request(KVM_REQ_EVENT, vcpu); |
| 4999 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5000 | return nested_vmx_succeed(vcpu); |
| 5001 | } |
| 5002 | |
| 5003 | /* Emulate the VMCLEAR instruction */ |
| 5004 | static int handle_vmclear(struct kvm_vcpu *vcpu) |
| 5005 | { |
| 5006 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 5007 | u32 zero = 0; |
| 5008 | gpa_t vmptr; |
Vitaly Kuznetsov | 11e3491 | 2019-06-28 13:23:33 +0200 | [diff] [blame] | 5009 | u64 evmcs_gpa; |
Vitaly Kuznetsov | 7a35e51 | 2020-06-05 13:59:05 +0200 | [diff] [blame] | 5010 | int r; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5011 | |
| 5012 | if (!nested_vmx_check_permission(vcpu)) |
| 5013 | return 1; |
| 5014 | |
Vitaly Kuznetsov | 7a35e51 | 2020-06-05 13:59:05 +0200 | [diff] [blame] | 5015 | if (nested_vmx_get_vmptr(vcpu, &vmptr, &r)) |
| 5016 | return r; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5017 | |
KarimAllah Ahmed | e0bf266 | 2019-01-31 21:24:43 +0100 | [diff] [blame] | 5018 | if (!page_address_valid(vcpu, vmptr)) |
Sean Christopherson | b2656e4 | 2020-06-08 18:56:07 -0700 | [diff] [blame] | 5019 | return nested_vmx_fail(vcpu, VMXERR_VMCLEAR_INVALID_ADDRESS); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5020 | |
| 5021 | if (vmptr == vmx->nested.vmxon_ptr) |
Sean Christopherson | b2656e4 | 2020-06-08 18:56:07 -0700 | [diff] [blame] | 5022 | return nested_vmx_fail(vcpu, VMXERR_VMCLEAR_VMXON_POINTER); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5023 | |
Vitaly Kuznetsov | 11e3491 | 2019-06-28 13:23:33 +0200 | [diff] [blame] | 5024 | /* |
| 5025 | * When Enlightened VMEntry is enabled on the calling CPU we treat |
| 5026 | * memory area pointer by vmptr as Enlightened VMCS (as there's no good |
| 5027 | * way to distinguish it from VMCS12) and we must not corrupt it by |
| 5028 | * writing to the non-existent 'launch_state' field. The area doesn't |
| 5029 | * have to be the currently active EVMCS on the calling CPU and there's |
| 5030 | * nothing KVM has to do to transition it from 'active' to 'non-active' |
| 5031 | * state. It is possible that the area will stay mapped as |
| 5032 | * vmx->nested.hv_evmcs but this shouldn't be a problem. |
| 5033 | */ |
| 5034 | if (likely(!vmx->nested.enlightened_vmcs_enabled || |
| 5035 | !nested_enlightened_vmentry(vcpu, &evmcs_gpa))) { |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5036 | if (vmptr == vmx->nested.current_vmptr) |
| 5037 | nested_release_vmcs12(vcpu); |
| 5038 | |
| 5039 | kvm_vcpu_write_guest(vcpu, |
| 5040 | vmptr + offsetof(struct vmcs12, |
| 5041 | launch_state), |
| 5042 | &zero, sizeof(zero)); |
Vitaly Kuznetsov | 3b19b81 | 2021-05-26 15:20:21 +0200 | [diff] [blame] | 5043 | } else if (vmx->nested.hv_evmcs && vmptr == vmx->nested.hv_evmcs_vmptr) { |
| 5044 | nested_release_evmcs(vcpu); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5045 | } |
| 5046 | |
| 5047 | return nested_vmx_succeed(vcpu); |
| 5048 | } |
| 5049 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5050 | /* Emulate the VMLAUNCH instruction */ |
| 5051 | static int handle_vmlaunch(struct kvm_vcpu *vcpu) |
| 5052 | { |
| 5053 | return nested_vmx_run(vcpu, true); |
| 5054 | } |
| 5055 | |
| 5056 | /* Emulate the VMRESUME instruction */ |
| 5057 | static int handle_vmresume(struct kvm_vcpu *vcpu) |
| 5058 | { |
| 5059 | |
| 5060 | return nested_vmx_run(vcpu, false); |
| 5061 | } |
| 5062 | |
| 5063 | static int handle_vmread(struct kvm_vcpu *vcpu) |
| 5064 | { |
Jim Mattson | dd2d604 | 2019-12-06 15:46:35 -0800 | [diff] [blame] | 5065 | struct vmcs12 *vmcs12 = is_guest_mode(vcpu) ? get_shadow_vmcs12(vcpu) |
| 5066 | : get_vmcs12(vcpu); |
Sean Christopherson | 5addc23 | 2020-04-15 13:34:53 -0700 | [diff] [blame] | 5067 | unsigned long exit_qualification = vmx_get_exit_qual(vcpu); |
Jim Mattson | c90f4d0 | 2019-12-06 15:46:37 -0800 | [diff] [blame] | 5068 | u32 instr_info = vmcs_read32(VMX_INSTRUCTION_INFO); |
| 5069 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Paolo Bonzini | f7eea63 | 2019-09-14 00:26:27 +0200 | [diff] [blame] | 5070 | struct x86_exception e; |
Jim Mattson | c90f4d0 | 2019-12-06 15:46:37 -0800 | [diff] [blame] | 5071 | unsigned long field; |
| 5072 | u64 value; |
| 5073 | gva_t gva = 0; |
Sean Christopherson | 1c6f0b4 | 2019-05-07 08:36:25 -0700 | [diff] [blame] | 5074 | short offset; |
Vitaly Kuznetsov | 7a35e51 | 2020-06-05 13:59:05 +0200 | [diff] [blame] | 5075 | int len, r; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5076 | |
| 5077 | if (!nested_vmx_check_permission(vcpu)) |
| 5078 | return 1; |
| 5079 | |
Jim Mattson | dd2d604 | 2019-12-06 15:46:35 -0800 | [diff] [blame] | 5080 | /* |
Yu Zhang | 64c7850 | 2021-09-30 01:51:53 +0800 | [diff] [blame] | 5081 | * 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] | 5082 | * any VMREAD sets the ALU flags for VMfailInvalid. |
| 5083 | */ |
Yu Zhang | 64c7850 | 2021-09-30 01:51:53 +0800 | [diff] [blame] | 5084 | if (vmx->nested.current_vmptr == INVALID_GPA || |
Jim Mattson | dd2d604 | 2019-12-06 15:46:35 -0800 | [diff] [blame] | 5085 | (is_guest_mode(vcpu) && |
Yu Zhang | 64c7850 | 2021-09-30 01:51:53 +0800 | [diff] [blame] | 5086 | get_vmcs12(vcpu)->vmcs_link_pointer == INVALID_GPA)) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5087 | return nested_vmx_failInvalid(vcpu); |
| 5088 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5089 | /* Decode instruction info and find the field to read */ |
Sean Christopherson | 27b4a9c4 | 2021-04-21 19:21:28 -0700 | [diff] [blame] | 5090 | field = kvm_register_read(vcpu, (((instr_info) >> 28) & 0xf)); |
Sean Christopherson | 1c6f0b4 | 2019-05-07 08:36:25 -0700 | [diff] [blame] | 5091 | |
| 5092 | offset = vmcs_field_to_offset(field); |
| 5093 | if (offset < 0) |
Sean Christopherson | b2656e4 | 2020-06-08 18:56:07 -0700 | [diff] [blame] | 5094 | return nested_vmx_fail(vcpu, VMXERR_UNSUPPORTED_VMCS_COMPONENT); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5095 | |
Sean Christopherson | 7952d76 | 2019-05-07 08:36:29 -0700 | [diff] [blame] | 5096 | if (!is_guest_mode(vcpu) && is_vmcs12_ext_field(field)) |
| 5097 | copy_vmcs02_to_vmcs12_rare(vcpu, vmcs12); |
| 5098 | |
Jim Mattson | c90f4d0 | 2019-12-06 15:46:37 -0800 | [diff] [blame] | 5099 | /* Read the field, zero-extended to a u64 value */ |
| 5100 | value = vmcs12_read_any(vmcs12, field, offset); |
Sean Christopherson | 1c6f0b4 | 2019-05-07 08:36:25 -0700 | [diff] [blame] | 5101 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5102 | /* |
| 5103 | * Now copy part of this value to register or memory, as requested. |
| 5104 | * Note that the number of bits actually copied is 32 or 64 depending |
| 5105 | * on the guest's mode (32 or 64 bit), not on the given field's length. |
| 5106 | */ |
Jim Mattson | c90f4d0 | 2019-12-06 15:46:37 -0800 | [diff] [blame] | 5107 | if (instr_info & BIT(10)) { |
Sean Christopherson | 27b4a9c4 | 2021-04-21 19:21:28 -0700 | [diff] [blame] | 5108 | kvm_register_write(vcpu, (((instr_info) >> 3) & 0xf), value); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5109 | } else { |
Eugene Korenevsky | fdb2861 | 2019-06-06 00:19:16 +0300 | [diff] [blame] | 5110 | len = is_64_bit_mode(vcpu) ? 8 : 4; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5111 | if (get_vmx_mem_address(vcpu, exit_qualification, |
Jim Mattson | c90f4d0 | 2019-12-06 15:46:37 -0800 | [diff] [blame] | 5112 | instr_info, true, len, &gva)) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5113 | return 1; |
| 5114 | /* _system ok, nested_vmx_check_permission has verified cpl=0 */ |
Vitaly Kuznetsov | 7a35e51 | 2020-06-05 13:59:05 +0200 | [diff] [blame] | 5115 | r = kvm_write_guest_virt_system(vcpu, gva, &value, len, &e); |
| 5116 | if (r != X86EMUL_CONTINUE) |
Babu Moger | 3f3393b | 2020-09-11 14:29:05 -0500 | [diff] [blame] | 5117 | return kvm_handle_memory_failure(vcpu, r, &e); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5118 | } |
| 5119 | |
| 5120 | return nested_vmx_succeed(vcpu); |
| 5121 | } |
| 5122 | |
Sean Christopherson | e217429 | 2019-05-07 08:36:28 -0700 | [diff] [blame] | 5123 | static bool is_shadow_field_rw(unsigned long field) |
| 5124 | { |
| 5125 | switch (field) { |
| 5126 | #define SHADOW_FIELD_RW(x, y) case x: |
| 5127 | #include "vmcs_shadow_fields.h" |
| 5128 | return true; |
| 5129 | default: |
| 5130 | break; |
| 5131 | } |
| 5132 | return false; |
| 5133 | } |
| 5134 | |
| 5135 | static bool is_shadow_field_ro(unsigned long field) |
| 5136 | { |
| 5137 | switch (field) { |
| 5138 | #define SHADOW_FIELD_RO(x, y) case x: |
| 5139 | #include "vmcs_shadow_fields.h" |
| 5140 | return true; |
| 5141 | default: |
| 5142 | break; |
| 5143 | } |
| 5144 | return false; |
| 5145 | } |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5146 | |
| 5147 | static int handle_vmwrite(struct kvm_vcpu *vcpu) |
| 5148 | { |
Jim Mattson | dd2d604 | 2019-12-06 15:46:35 -0800 | [diff] [blame] | 5149 | struct vmcs12 *vmcs12 = is_guest_mode(vcpu) ? get_shadow_vmcs12(vcpu) |
| 5150 | : get_vmcs12(vcpu); |
Sean Christopherson | 5addc23 | 2020-04-15 13:34:53 -0700 | [diff] [blame] | 5151 | unsigned long exit_qualification = vmx_get_exit_qual(vcpu); |
Jim Mattson | c90f4d0 | 2019-12-06 15:46:37 -0800 | [diff] [blame] | 5152 | u32 instr_info = vmcs_read32(VMX_INSTRUCTION_INFO); |
| 5153 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 5154 | struct x86_exception e; |
| 5155 | unsigned long field; |
Sean Christopherson | 1c6f0b4 | 2019-05-07 08:36:25 -0700 | [diff] [blame] | 5156 | short offset; |
Jim Mattson | c90f4d0 | 2019-12-06 15:46:37 -0800 | [diff] [blame] | 5157 | gva_t gva; |
Vitaly Kuznetsov | 7a35e51 | 2020-06-05 13:59:05 +0200 | [diff] [blame] | 5158 | int len, r; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5159 | |
Jim Mattson | c90f4d0 | 2019-12-06 15:46:37 -0800 | [diff] [blame] | 5160 | /* |
| 5161 | * 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] | 5162 | * mode, and eventually we need to write that into a field of several |
| 5163 | * possible lengths. The code below first zero-extends the value to 64 |
Jim Mattson | c90f4d0 | 2019-12-06 15:46:37 -0800 | [diff] [blame] | 5164 | * bit (value), and then copies only the appropriate number of |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5165 | * bits into the vmcs12 field. |
| 5166 | */ |
Jim Mattson | c90f4d0 | 2019-12-06 15:46:37 -0800 | [diff] [blame] | 5167 | u64 value = 0; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5168 | |
| 5169 | if (!nested_vmx_check_permission(vcpu)) |
| 5170 | return 1; |
| 5171 | |
Jim Mattson | dd2d604 | 2019-12-06 15:46:35 -0800 | [diff] [blame] | 5172 | /* |
Yu Zhang | 64c7850 | 2021-09-30 01:51:53 +0800 | [diff] [blame] | 5173 | * 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] | 5174 | * any VMWRITE sets the ALU flags for VMfailInvalid. |
| 5175 | */ |
Yu Zhang | 64c7850 | 2021-09-30 01:51:53 +0800 | [diff] [blame] | 5176 | if (vmx->nested.current_vmptr == INVALID_GPA || |
Jim Mattson | dd2d604 | 2019-12-06 15:46:35 -0800 | [diff] [blame] | 5177 | (is_guest_mode(vcpu) && |
Yu Zhang | 64c7850 | 2021-09-30 01:51:53 +0800 | [diff] [blame] | 5178 | get_vmcs12(vcpu)->vmcs_link_pointer == INVALID_GPA)) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5179 | return nested_vmx_failInvalid(vcpu); |
| 5180 | |
Jim Mattson | c90f4d0 | 2019-12-06 15:46:37 -0800 | [diff] [blame] | 5181 | if (instr_info & BIT(10)) |
Sean Christopherson | 27b4a9c4 | 2021-04-21 19:21:28 -0700 | [diff] [blame] | 5182 | value = kvm_register_read(vcpu, (((instr_info) >> 3) & 0xf)); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5183 | else { |
Eugene Korenevsky | fdb2861 | 2019-06-06 00:19:16 +0300 | [diff] [blame] | 5184 | len = is_64_bit_mode(vcpu) ? 8 : 4; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5185 | if (get_vmx_mem_address(vcpu, exit_qualification, |
Jim Mattson | c90f4d0 | 2019-12-06 15:46:37 -0800 | [diff] [blame] | 5186 | instr_info, false, len, &gva)) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5187 | return 1; |
Vitaly Kuznetsov | 7a35e51 | 2020-06-05 13:59:05 +0200 | [diff] [blame] | 5188 | r = kvm_read_guest_virt(vcpu, gva, &value, len, &e); |
| 5189 | if (r != X86EMUL_CONTINUE) |
Babu Moger | 3f3393b | 2020-09-11 14:29:05 -0500 | [diff] [blame] | 5190 | return kvm_handle_memory_failure(vcpu, r, &e); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5191 | } |
| 5192 | |
Sean Christopherson | 27b4a9c4 | 2021-04-21 19:21:28 -0700 | [diff] [blame] | 5193 | field = kvm_register_read(vcpu, (((instr_info) >> 28) & 0xf)); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5194 | |
Jim Mattson | 693e02c | 2019-12-06 15:46:36 -0800 | [diff] [blame] | 5195 | offset = vmcs_field_to_offset(field); |
| 5196 | if (offset < 0) |
Sean Christopherson | b2656e4 | 2020-06-08 18:56:07 -0700 | [diff] [blame] | 5197 | return nested_vmx_fail(vcpu, VMXERR_UNSUPPORTED_VMCS_COMPONENT); |
Jim Mattson | 693e02c | 2019-12-06 15:46:36 -0800 | [diff] [blame] | 5198 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5199 | /* |
| 5200 | * If the vCPU supports "VMWRITE to any supported field in the |
| 5201 | * VMCS," then the "read-only" fields are actually read/write. |
| 5202 | */ |
| 5203 | if (vmcs_field_readonly(field) && |
| 5204 | !nested_cpu_has_vmwrite_any_field(vcpu)) |
Sean Christopherson | b2656e4 | 2020-06-08 18:56:07 -0700 | [diff] [blame] | 5205 | return nested_vmx_fail(vcpu, VMXERR_VMWRITE_READ_ONLY_VMCS_COMPONENT); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5206 | |
Jim Mattson | dd2d604 | 2019-12-06 15:46:35 -0800 | [diff] [blame] | 5207 | /* |
| 5208 | * Ensure vmcs12 is up-to-date before any VMWRITE that dirties |
| 5209 | * vmcs12, else we may crush a field or consume a stale value. |
| 5210 | */ |
| 5211 | if (!is_guest_mode(vcpu) && !is_shadow_field_rw(field)) |
| 5212 | copy_vmcs02_to_vmcs12_rare(vcpu, vmcs12); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5213 | |
| 5214 | /* |
Sean Christopherson | b643780 | 2019-05-07 08:36:24 -0700 | [diff] [blame] | 5215 | * Some Intel CPUs intentionally drop the reserved bits of the AR byte |
| 5216 | * fields on VMWRITE. Emulate this behavior to ensure consistent KVM |
| 5217 | * behavior regardless of the underlying hardware, e.g. if an AR_BYTE |
| 5218 | * field is intercepted for VMWRITE but not VMREAD (in L1), then VMREAD |
| 5219 | * from L1 will return a different value than VMREAD from L2 (L1 sees |
| 5220 | * 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] | 5221 | */ |
Sean Christopherson | b643780 | 2019-05-07 08:36:24 -0700 | [diff] [blame] | 5222 | if (field >= GUEST_ES_AR_BYTES && field <= GUEST_TR_AR_BYTES) |
Jim Mattson | c90f4d0 | 2019-12-06 15:46:37 -0800 | [diff] [blame] | 5223 | value &= 0x1f0ff; |
Sean Christopherson | b643780 | 2019-05-07 08:36:24 -0700 | [diff] [blame] | 5224 | |
Jim Mattson | c90f4d0 | 2019-12-06 15:46:37 -0800 | [diff] [blame] | 5225 | vmcs12_write_any(vmcs12, field, offset, value); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5226 | |
| 5227 | /* |
Sean Christopherson | e217429 | 2019-05-07 08:36:28 -0700 | [diff] [blame] | 5228 | * Do not track vmcs12 dirty-state if in guest-mode as we actually |
| 5229 | * dirty shadow vmcs12 instead of vmcs12. Fields that can be updated |
| 5230 | * by L1 without a vmexit are always updated in the vmcs02, i.e. don't |
| 5231 | * "dirty" vmcs12, all others go down the prepare_vmcs02() slow path. |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5232 | */ |
Sean Christopherson | e217429 | 2019-05-07 08:36:28 -0700 | [diff] [blame] | 5233 | if (!is_guest_mode(vcpu) && !is_shadow_field_rw(field)) { |
| 5234 | /* |
| 5235 | * L1 can read these fields without exiting, ensure the |
| 5236 | * shadow VMCS is up-to-date. |
| 5237 | */ |
| 5238 | if (enable_shadow_vmcs && is_shadow_field_ro(field)) { |
| 5239 | preempt_disable(); |
| 5240 | vmcs_load(vmx->vmcs01.shadow_vmcs); |
Sean Christopherson | fadcead | 2019-05-07 08:36:23 -0700 | [diff] [blame] | 5241 | |
Jim Mattson | c90f4d0 | 2019-12-06 15:46:37 -0800 | [diff] [blame] | 5242 | __vmcs_writel(field, value); |
Sean Christopherson | fadcead | 2019-05-07 08:36:23 -0700 | [diff] [blame] | 5243 | |
Sean Christopherson | e217429 | 2019-05-07 08:36:28 -0700 | [diff] [blame] | 5244 | vmcs_clear(vmx->vmcs01.shadow_vmcs); |
| 5245 | vmcs_load(vmx->loaded_vmcs->vmcs); |
| 5246 | preempt_enable(); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5247 | } |
Sean Christopherson | e217429 | 2019-05-07 08:36:28 -0700 | [diff] [blame] | 5248 | vmx->nested.dirty_vmcs12 = true; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5249 | } |
| 5250 | |
| 5251 | return nested_vmx_succeed(vcpu); |
| 5252 | } |
| 5253 | |
| 5254 | static void set_current_vmptr(struct vcpu_vmx *vmx, gpa_t vmptr) |
| 5255 | { |
| 5256 | vmx->nested.current_vmptr = vmptr; |
| 5257 | if (enable_shadow_vmcs) { |
Sean Christopherson | fe7f895d | 2019-05-07 12:17:57 -0700 | [diff] [blame] | 5258 | secondary_exec_controls_setbit(vmx, SECONDARY_EXEC_SHADOW_VMCS); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5259 | vmcs_write64(VMCS_LINK_POINTER, |
| 5260 | __pa(vmx->vmcs01.shadow_vmcs)); |
Sean Christopherson | 3731905ef | 2019-05-07 08:36:27 -0700 | [diff] [blame] | 5261 | vmx->nested.need_vmcs12_to_shadow_sync = true; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5262 | } |
| 5263 | vmx->nested.dirty_vmcs12 = true; |
| 5264 | } |
| 5265 | |
| 5266 | /* Emulate the VMPTRLD instruction */ |
| 5267 | static int handle_vmptrld(struct kvm_vcpu *vcpu) |
| 5268 | { |
| 5269 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 5270 | gpa_t vmptr; |
Vitaly Kuznetsov | 7a35e51 | 2020-06-05 13:59:05 +0200 | [diff] [blame] | 5271 | int r; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5272 | |
| 5273 | if (!nested_vmx_check_permission(vcpu)) |
| 5274 | return 1; |
| 5275 | |
Vitaly Kuznetsov | 7a35e51 | 2020-06-05 13:59:05 +0200 | [diff] [blame] | 5276 | if (nested_vmx_get_vmptr(vcpu, &vmptr, &r)) |
| 5277 | return r; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5278 | |
KarimAllah Ahmed | e0bf266 | 2019-01-31 21:24:43 +0100 | [diff] [blame] | 5279 | if (!page_address_valid(vcpu, vmptr)) |
Sean Christopherson | b2656e4 | 2020-06-08 18:56:07 -0700 | [diff] [blame] | 5280 | return nested_vmx_fail(vcpu, VMXERR_VMPTRLD_INVALID_ADDRESS); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5281 | |
| 5282 | if (vmptr == vmx->nested.vmxon_ptr) |
Sean Christopherson | b2656e4 | 2020-06-08 18:56:07 -0700 | [diff] [blame] | 5283 | return nested_vmx_fail(vcpu, VMXERR_VMPTRLD_VMXON_POINTER); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5284 | |
| 5285 | /* Forbid normal VMPTRLD if Enlightened version was used */ |
Vitaly Kuznetsov | 1e9dfbd | 2021-05-26 15:20:16 +0200 | [diff] [blame] | 5286 | if (evmptr_is_valid(vmx->nested.hv_evmcs_vmptr)) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5287 | return 1; |
| 5288 | |
| 5289 | if (vmx->nested.current_vmptr != vmptr) { |
David Woodhouse | cee6666 | 2021-11-15 16:50:26 +0000 | [diff] [blame] | 5290 | struct gfn_to_hva_cache *ghc = &vmx->nested.vmcs12_cache; |
| 5291 | struct vmcs_hdr hdr; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5292 | |
Paolo Bonzini | 8503fea | 2021-11-22 18:20:16 -0500 | [diff] [blame^] | 5293 | if (kvm_gfn_to_hva_cache_init(vcpu->kvm, ghc, vmptr, VMCS12_SIZE)) { |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5294 | /* |
| 5295 | * Reads from an unbacked page return all 1s, |
| 5296 | * which means that the 32 bits located at the |
| 5297 | * given physical address won't match the required |
| 5298 | * VMCS12_REVISION identifier. |
| 5299 | */ |
Sean Christopherson | b2656e4 | 2020-06-08 18:56:07 -0700 | [diff] [blame] | 5300 | return nested_vmx_fail(vcpu, |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5301 | VMXERR_VMPTRLD_INCORRECT_VMCS_REVISION_ID); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5302 | } |
KarimAllah Ahmed | b146b83 | 2019-01-31 21:24:35 +0100 | [diff] [blame] | 5303 | |
David Woodhouse | cee6666 | 2021-11-15 16:50:26 +0000 | [diff] [blame] | 5304 | if (kvm_read_guest_offset_cached(vcpu->kvm, ghc, &hdr, |
| 5305 | offsetof(struct vmcs12, hdr), |
| 5306 | sizeof(hdr))) { |
| 5307 | return nested_vmx_fail(vcpu, |
| 5308 | VMXERR_VMPTRLD_INCORRECT_VMCS_REVISION_ID); |
| 5309 | } |
KarimAllah Ahmed | b146b83 | 2019-01-31 21:24:35 +0100 | [diff] [blame] | 5310 | |
David Woodhouse | cee6666 | 2021-11-15 16:50:26 +0000 | [diff] [blame] | 5311 | if (hdr.revision_id != VMCS12_REVISION || |
| 5312 | (hdr.shadow_vmcs && |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5313 | !nested_cpu_has_vmx_shadow_vmcs(vcpu))) { |
Sean Christopherson | b2656e4 | 2020-06-08 18:56:07 -0700 | [diff] [blame] | 5314 | return nested_vmx_fail(vcpu, |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5315 | VMXERR_VMPTRLD_INCORRECT_VMCS_REVISION_ID); |
| 5316 | } |
| 5317 | |
| 5318 | nested_release_vmcs12(vcpu); |
| 5319 | |
| 5320 | /* |
| 5321 | * Load VMCS12 from guest memory since it is not already |
| 5322 | * cached. |
| 5323 | */ |
David Woodhouse | cee6666 | 2021-11-15 16:50:26 +0000 | [diff] [blame] | 5324 | if (kvm_read_guest_cached(vcpu->kvm, ghc, vmx->nested.cached_vmcs12, |
| 5325 | VMCS12_SIZE)) { |
| 5326 | return nested_vmx_fail(vcpu, |
| 5327 | VMXERR_VMPTRLD_INCORRECT_VMCS_REVISION_ID); |
| 5328 | } |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5329 | |
| 5330 | set_current_vmptr(vmx, vmptr); |
| 5331 | } |
| 5332 | |
| 5333 | return nested_vmx_succeed(vcpu); |
| 5334 | } |
| 5335 | |
| 5336 | /* Emulate the VMPTRST instruction */ |
| 5337 | static int handle_vmptrst(struct kvm_vcpu *vcpu) |
| 5338 | { |
Sean Christopherson | 5addc23 | 2020-04-15 13:34:53 -0700 | [diff] [blame] | 5339 | unsigned long exit_qual = vmx_get_exit_qual(vcpu); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5340 | u32 instr_info = vmcs_read32(VMX_INSTRUCTION_INFO); |
| 5341 | gpa_t current_vmptr = to_vmx(vcpu)->nested.current_vmptr; |
| 5342 | struct x86_exception e; |
| 5343 | gva_t gva; |
Vitaly Kuznetsov | 7a35e51 | 2020-06-05 13:59:05 +0200 | [diff] [blame] | 5344 | int r; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5345 | |
| 5346 | if (!nested_vmx_check_permission(vcpu)) |
| 5347 | return 1; |
| 5348 | |
Vitaly Kuznetsov | 1e9dfbd | 2021-05-26 15:20:16 +0200 | [diff] [blame] | 5349 | if (unlikely(evmptr_is_valid(to_vmx(vcpu)->nested.hv_evmcs_vmptr))) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5350 | return 1; |
| 5351 | |
Eugene Korenevsky | fdb2861 | 2019-06-06 00:19:16 +0300 | [diff] [blame] | 5352 | if (get_vmx_mem_address(vcpu, exit_qual, instr_info, |
| 5353 | true, sizeof(gpa_t), &gva)) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5354 | return 1; |
| 5355 | /* *_system ok, nested_vmx_check_permission has verified cpl=0 */ |
Vitaly Kuznetsov | 7a35e51 | 2020-06-05 13:59:05 +0200 | [diff] [blame] | 5356 | r = kvm_write_guest_virt_system(vcpu, gva, (void *)¤t_vmptr, |
| 5357 | sizeof(gpa_t), &e); |
| 5358 | if (r != X86EMUL_CONTINUE) |
Babu Moger | 3f3393b | 2020-09-11 14:29:05 -0500 | [diff] [blame] | 5359 | return kvm_handle_memory_failure(vcpu, r, &e); |
Vitaly Kuznetsov | 7a35e51 | 2020-06-05 13:59:05 +0200 | [diff] [blame] | 5360 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5361 | return nested_vmx_succeed(vcpu); |
| 5362 | } |
| 5363 | |
| 5364 | /* Emulate the INVEPT instruction */ |
| 5365 | static int handle_invept(struct kvm_vcpu *vcpu) |
| 5366 | { |
| 5367 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 5368 | u32 vmx_instruction_info, types; |
Sean Christopherson | ce8fe7b | 2020-03-20 14:28:31 -0700 | [diff] [blame] | 5369 | unsigned long type, roots_to_free; |
| 5370 | struct kvm_mmu *mmu; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5371 | gva_t gva; |
| 5372 | struct x86_exception e; |
| 5373 | struct { |
| 5374 | u64 eptp, gpa; |
| 5375 | } operand; |
Vipin Sharma | 329bd56 | 2021-11-09 17:44:25 +0000 | [diff] [blame] | 5376 | int i, r, gpr_index; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5377 | |
| 5378 | if (!(vmx->nested.msrs.secondary_ctls_high & |
| 5379 | SECONDARY_EXEC_ENABLE_EPT) || |
| 5380 | !(vmx->nested.msrs.ept_caps & VMX_EPT_INVEPT_BIT)) { |
| 5381 | kvm_queue_exception(vcpu, UD_VECTOR); |
| 5382 | return 1; |
| 5383 | } |
| 5384 | |
| 5385 | if (!nested_vmx_check_permission(vcpu)) |
| 5386 | return 1; |
| 5387 | |
| 5388 | vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO); |
Vipin Sharma | 329bd56 | 2021-11-09 17:44:25 +0000 | [diff] [blame] | 5389 | gpr_index = vmx_get_instr_info_reg2(vmx_instruction_info); |
| 5390 | type = kvm_register_read(vcpu, gpr_index); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5391 | |
| 5392 | types = (vmx->nested.msrs.ept_caps >> VMX_EPT_EXTENT_SHIFT) & 6; |
| 5393 | |
| 5394 | if (type >= 32 || !(types & (1 << type))) |
Sean Christopherson | b2656e4 | 2020-06-08 18:56:07 -0700 | [diff] [blame] | 5395 | return nested_vmx_fail(vcpu, VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5396 | |
| 5397 | /* According to the Intel VMX instruction reference, the memory |
| 5398 | * operand is read even if it isn't needed (e.g., for type==global) |
| 5399 | */ |
Sean Christopherson | 5addc23 | 2020-04-15 13:34:53 -0700 | [diff] [blame] | 5400 | if (get_vmx_mem_address(vcpu, vmx_get_exit_qual(vcpu), |
Eugene Korenevsky | fdb2861 | 2019-06-06 00:19:16 +0300 | [diff] [blame] | 5401 | vmx_instruction_info, false, sizeof(operand), &gva)) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5402 | return 1; |
Vitaly Kuznetsov | 7a35e51 | 2020-06-05 13:59:05 +0200 | [diff] [blame] | 5403 | r = kvm_read_guest_virt(vcpu, gva, &operand, sizeof(operand), &e); |
| 5404 | if (r != X86EMUL_CONTINUE) |
Babu Moger | 3f3393b | 2020-09-11 14:29:05 -0500 | [diff] [blame] | 5405 | return kvm_handle_memory_failure(vcpu, r, &e); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5406 | |
Sean Christopherson | ce8fe7b | 2020-03-20 14:28:31 -0700 | [diff] [blame] | 5407 | /* |
| 5408 | * Nested EPT roots are always held through guest_mmu, |
| 5409 | * not root_mmu. |
| 5410 | */ |
| 5411 | mmu = &vcpu->arch.guest_mmu; |
| 5412 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5413 | switch (type) { |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5414 | case VMX_EPT_EXTENT_CONTEXT: |
Sean Christopherson | eed0030 | 2020-03-20 14:27:58 -0700 | [diff] [blame] | 5415 | if (!nested_vmx_check_eptp(vcpu, operand.eptp)) |
Sean Christopherson | b2656e4 | 2020-06-08 18:56:07 -0700 | [diff] [blame] | 5416 | return nested_vmx_fail(vcpu, |
Sean Christopherson | eed0030 | 2020-03-20 14:27:58 -0700 | [diff] [blame] | 5417 | VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID); |
Sean Christopherson | f8aa7e3 | 2020-03-20 14:27:59 -0700 | [diff] [blame] | 5418 | |
Sean Christopherson | ce8fe7b | 2020-03-20 14:28:31 -0700 | [diff] [blame] | 5419 | roots_to_free = 0; |
Sean Christopherson | be01e8e | 2020-03-20 14:28:32 -0700 | [diff] [blame] | 5420 | if (nested_ept_root_matches(mmu->root_hpa, mmu->root_pgd, |
Sean Christopherson | ce8fe7b | 2020-03-20 14:28:31 -0700 | [diff] [blame] | 5421 | operand.eptp)) |
| 5422 | roots_to_free |= KVM_MMU_ROOT_CURRENT; |
| 5423 | |
| 5424 | for (i = 0; i < KVM_MMU_NUM_PREV_ROOTS; i++) { |
| 5425 | if (nested_ept_root_matches(mmu->prev_roots[i].hpa, |
Sean Christopherson | be01e8e | 2020-03-20 14:28:32 -0700 | [diff] [blame] | 5426 | mmu->prev_roots[i].pgd, |
Sean Christopherson | ce8fe7b | 2020-03-20 14:28:31 -0700 | [diff] [blame] | 5427 | operand.eptp)) |
| 5428 | roots_to_free |= KVM_MMU_ROOT_PREVIOUS(i); |
| 5429 | } |
| 5430 | break; |
Sean Christopherson | eed0030 | 2020-03-20 14:27:58 -0700 | [diff] [blame] | 5431 | case VMX_EPT_EXTENT_GLOBAL: |
Sean Christopherson | ce8fe7b | 2020-03-20 14:28:31 -0700 | [diff] [blame] | 5432 | roots_to_free = KVM_MMU_ROOTS_ALL; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5433 | break; |
| 5434 | default: |
Sean Christopherson | f9336e3 | 2020-05-04 08:35:06 -0700 | [diff] [blame] | 5435 | BUG(); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5436 | break; |
| 5437 | } |
| 5438 | |
Sean Christopherson | ce8fe7b | 2020-03-20 14:28:31 -0700 | [diff] [blame] | 5439 | if (roots_to_free) |
| 5440 | kvm_mmu_free_roots(vcpu, mmu, roots_to_free); |
| 5441 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5442 | return nested_vmx_succeed(vcpu); |
| 5443 | } |
| 5444 | |
| 5445 | static int handle_invvpid(struct kvm_vcpu *vcpu) |
| 5446 | { |
| 5447 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 5448 | u32 vmx_instruction_info; |
| 5449 | unsigned long type, types; |
| 5450 | gva_t gva; |
| 5451 | struct x86_exception e; |
| 5452 | struct { |
| 5453 | u64 vpid; |
| 5454 | u64 gla; |
| 5455 | } operand; |
| 5456 | u16 vpid02; |
Vipin Sharma | 329bd56 | 2021-11-09 17:44:25 +0000 | [diff] [blame] | 5457 | int r, gpr_index; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5458 | |
| 5459 | if (!(vmx->nested.msrs.secondary_ctls_high & |
| 5460 | SECONDARY_EXEC_ENABLE_VPID) || |
| 5461 | !(vmx->nested.msrs.vpid_caps & VMX_VPID_INVVPID_BIT)) { |
| 5462 | kvm_queue_exception(vcpu, UD_VECTOR); |
| 5463 | return 1; |
| 5464 | } |
| 5465 | |
| 5466 | if (!nested_vmx_check_permission(vcpu)) |
| 5467 | return 1; |
| 5468 | |
| 5469 | vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO); |
Vipin Sharma | 329bd56 | 2021-11-09 17:44:25 +0000 | [diff] [blame] | 5470 | gpr_index = vmx_get_instr_info_reg2(vmx_instruction_info); |
| 5471 | type = kvm_register_read(vcpu, gpr_index); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5472 | |
| 5473 | types = (vmx->nested.msrs.vpid_caps & |
| 5474 | VMX_VPID_EXTENT_SUPPORTED_MASK) >> 8; |
| 5475 | |
| 5476 | if (type >= 32 || !(types & (1 << type))) |
Sean Christopherson | b2656e4 | 2020-06-08 18:56:07 -0700 | [diff] [blame] | 5477 | return nested_vmx_fail(vcpu, |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5478 | VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID); |
| 5479 | |
| 5480 | /* according to the intel vmx instruction reference, the memory |
| 5481 | * operand is read even if it isn't needed (e.g., for type==global) |
| 5482 | */ |
Sean Christopherson | 5addc23 | 2020-04-15 13:34:53 -0700 | [diff] [blame] | 5483 | if (get_vmx_mem_address(vcpu, vmx_get_exit_qual(vcpu), |
Eugene Korenevsky | fdb2861 | 2019-06-06 00:19:16 +0300 | [diff] [blame] | 5484 | vmx_instruction_info, false, sizeof(operand), &gva)) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5485 | return 1; |
Vitaly Kuznetsov | 7a35e51 | 2020-06-05 13:59:05 +0200 | [diff] [blame] | 5486 | r = kvm_read_guest_virt(vcpu, gva, &operand, sizeof(operand), &e); |
| 5487 | if (r != X86EMUL_CONTINUE) |
Babu Moger | 3f3393b | 2020-09-11 14:29:05 -0500 | [diff] [blame] | 5488 | return kvm_handle_memory_failure(vcpu, r, &e); |
Vitaly Kuznetsov | 7a35e51 | 2020-06-05 13:59:05 +0200 | [diff] [blame] | 5489 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5490 | if (operand.vpid >> 16) |
Sean Christopherson | b2656e4 | 2020-06-08 18:56:07 -0700 | [diff] [blame] | 5491 | return nested_vmx_fail(vcpu, |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5492 | VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID); |
| 5493 | |
| 5494 | vpid02 = nested_get_vpid02(vcpu); |
| 5495 | switch (type) { |
| 5496 | case VMX_VPID_EXTENT_INDIVIDUAL_ADDR: |
| 5497 | if (!operand.vpid || |
| 5498 | is_noncanonical_address(operand.gla, vcpu)) |
Sean Christopherson | b2656e4 | 2020-06-08 18:56:07 -0700 | [diff] [blame] | 5499 | return nested_vmx_fail(vcpu, |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5500 | VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID); |
Sean Christopherson | bc41d0c | 2020-03-20 14:28:09 -0700 | [diff] [blame] | 5501 | vpid_sync_vcpu_addr(vpid02, operand.gla); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5502 | break; |
| 5503 | case VMX_VPID_EXTENT_SINGLE_CONTEXT: |
| 5504 | case VMX_VPID_EXTENT_SINGLE_NON_GLOBAL: |
| 5505 | if (!operand.vpid) |
Sean Christopherson | b2656e4 | 2020-06-08 18:56:07 -0700 | [diff] [blame] | 5506 | return nested_vmx_fail(vcpu, |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5507 | VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID); |
Sean Christopherson | 446ace4 | 2020-03-20 14:28:05 -0700 | [diff] [blame] | 5508 | vpid_sync_context(vpid02); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5509 | break; |
| 5510 | case VMX_VPID_EXTENT_ALL_CONTEXT: |
Sean Christopherson | 446ace4 | 2020-03-20 14:28:05 -0700 | [diff] [blame] | 5511 | vpid_sync_context(vpid02); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5512 | break; |
| 5513 | default: |
| 5514 | WARN_ON_ONCE(1); |
| 5515 | return kvm_skip_emulated_instruction(vcpu); |
| 5516 | } |
| 5517 | |
Junaid Shahid | d6e3f83 | 2020-03-20 14:28:00 -0700 | [diff] [blame] | 5518 | /* |
| 5519 | * Sync the shadow page tables if EPT is disabled, L1 is invalidating |
Sean Christopherson | 25b62c6 | 2021-06-09 16:42:29 -0700 | [diff] [blame] | 5520 | * linear mappings for L2 (tagged with L2's VPID). Free all guest |
| 5521 | * roots as VPIDs are not tracked in the MMU role. |
Junaid Shahid | d6e3f83 | 2020-03-20 14:28:00 -0700 | [diff] [blame] | 5522 | * |
| 5523 | * Note, this operates on root_mmu, not guest_mmu, as L1 and L2 share |
| 5524 | * an MMU when EPT is disabled. |
| 5525 | * |
| 5526 | * TODO: sync only the affected SPTEs for INVDIVIDUAL_ADDR. |
| 5527 | */ |
| 5528 | if (!enable_ept) |
Sean Christopherson | 25b62c6 | 2021-06-09 16:42:29 -0700 | [diff] [blame] | 5529 | kvm_mmu_free_guest_mode_roots(vcpu, &vcpu->arch.root_mmu); |
Junaid Shahid | d6e3f83 | 2020-03-20 14:28:00 -0700 | [diff] [blame] | 5530 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5531 | return nested_vmx_succeed(vcpu); |
| 5532 | } |
| 5533 | |
| 5534 | static int nested_vmx_eptp_switching(struct kvm_vcpu *vcpu, |
| 5535 | struct vmcs12 *vmcs12) |
| 5536 | { |
Sean Christopherson | 2b3eaf8 | 2019-04-30 10:36:19 -0700 | [diff] [blame] | 5537 | u32 index = kvm_rcx_read(vcpu); |
Sean Christopherson | ac6389a | 2020-03-02 18:02:38 -0800 | [diff] [blame] | 5538 | u64 new_eptp; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5539 | |
Sean Christopherson | c5ffd40 | 2021-06-09 16:42:35 -0700 | [diff] [blame] | 5540 | if (WARN_ON_ONCE(!nested_cpu_has_ept(vmcs12))) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5541 | return 1; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5542 | if (index >= VMFUNC_EPTP_ENTRIES) |
| 5543 | return 1; |
| 5544 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5545 | 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] | 5546 | &new_eptp, index * 8, 8)) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5547 | return 1; |
| 5548 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5549 | /* |
| 5550 | * If the (L2) guest does a vmfunc to the currently |
| 5551 | * active ept pointer, we don't have to do anything else |
| 5552 | */ |
Sean Christopherson | ac6389a | 2020-03-02 18:02:38 -0800 | [diff] [blame] | 5553 | if (vmcs12->ept_pointer != new_eptp) { |
| 5554 | if (!nested_vmx_check_eptp(vcpu, new_eptp)) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5555 | return 1; |
| 5556 | |
Sean Christopherson | ac6389a | 2020-03-02 18:02:38 -0800 | [diff] [blame] | 5557 | vmcs12->ept_pointer = new_eptp; |
Sean Christopherson | 39353ab | 2021-06-09 16:42:31 -0700 | [diff] [blame] | 5558 | nested_ept_new_eptp(vcpu); |
Sean Christopherson | c805f5d | 2021-03-04 17:10:57 -0800 | [diff] [blame] | 5559 | |
Sean Christopherson | 39353ab | 2021-06-09 16:42:31 -0700 | [diff] [blame] | 5560 | if (!nested_cpu_has_vpid(vmcs12)) |
| 5561 | kvm_make_request(KVM_REQ_TLB_FLUSH_GUEST, vcpu); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5562 | } |
| 5563 | |
| 5564 | return 0; |
| 5565 | } |
| 5566 | |
| 5567 | static int handle_vmfunc(struct kvm_vcpu *vcpu) |
| 5568 | { |
| 5569 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 5570 | struct vmcs12 *vmcs12; |
Sean Christopherson | 2b3eaf8 | 2019-04-30 10:36:19 -0700 | [diff] [blame] | 5571 | u32 function = kvm_rax_read(vcpu); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5572 | |
| 5573 | /* |
| 5574 | * VMFUNC is only supported for nested guests, but we always enable the |
| 5575 | * secondary control for simplicity; for non-nested mode, fake that we |
| 5576 | * didn't by injecting #UD. |
| 5577 | */ |
| 5578 | if (!is_guest_mode(vcpu)) { |
| 5579 | kvm_queue_exception(vcpu, UD_VECTOR); |
| 5580 | return 1; |
| 5581 | } |
| 5582 | |
| 5583 | vmcs12 = get_vmcs12(vcpu); |
Sean Christopherson | 546e839 | 2021-06-09 16:42:34 -0700 | [diff] [blame] | 5584 | |
| 5585 | /* |
| 5586 | * #UD on out-of-bounds function has priority over VM-Exit, and VMFUNC |
| 5587 | * is enabled in vmcs02 if and only if it's enabled in vmcs12. |
| 5588 | */ |
| 5589 | if (WARN_ON_ONCE((function > 63) || !nested_cpu_has_vmfunc(vmcs12))) { |
| 5590 | kvm_queue_exception(vcpu, UD_VECTOR); |
| 5591 | return 1; |
| 5592 | } |
| 5593 | |
Sean Christopherson | 0e75225 | 2021-06-09 16:42:22 -0700 | [diff] [blame] | 5594 | if (!(vmcs12->vm_function_control & BIT_ULL(function))) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5595 | goto fail; |
| 5596 | |
| 5597 | switch (function) { |
| 5598 | case 0: |
| 5599 | if (nested_vmx_eptp_switching(vcpu, vmcs12)) |
| 5600 | goto fail; |
| 5601 | break; |
| 5602 | default: |
| 5603 | goto fail; |
| 5604 | } |
| 5605 | return kvm_skip_emulated_instruction(vcpu); |
| 5606 | |
| 5607 | fail: |
Sean Christopherson | 8e53324 | 2020-11-06 17:03:12 +0800 | [diff] [blame] | 5608 | /* |
| 5609 | * This is effectively a reflected VM-Exit, as opposed to a synthesized |
| 5610 | * nested VM-Exit. Pass the original exit reason, i.e. don't hardcode |
| 5611 | * EXIT_REASON_VMFUNC as the exit reason. |
| 5612 | */ |
| 5613 | nested_vmx_vmexit(vcpu, vmx->exit_reason.full, |
Sean Christopherson | 8791585 | 2020-04-15 13:34:54 -0700 | [diff] [blame] | 5614 | vmx_get_intr_info(vcpu), |
Sean Christopherson | 5addc23 | 2020-04-15 13:34:53 -0700 | [diff] [blame] | 5615 | vmx_get_exit_qual(vcpu)); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5616 | return 1; |
| 5617 | } |
| 5618 | |
Oliver Upton | e71237d | 2020-02-04 15:26:30 -0800 | [diff] [blame] | 5619 | /* |
| 5620 | * Return true if an IO instruction with the specified port and size should cause |
| 5621 | * a VM-exit into L1. |
| 5622 | */ |
| 5623 | bool nested_vmx_check_io_bitmaps(struct kvm_vcpu *vcpu, unsigned int port, |
| 5624 | int size) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5625 | { |
Oliver Upton | e71237d | 2020-02-04 15:26:30 -0800 | [diff] [blame] | 5626 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5627 | gpa_t bitmap, last_bitmap; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5628 | u8 b; |
| 5629 | |
Yu Zhang | 64c7850 | 2021-09-30 01:51:53 +0800 | [diff] [blame] | 5630 | last_bitmap = INVALID_GPA; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5631 | b = -1; |
| 5632 | |
| 5633 | while (size > 0) { |
| 5634 | if (port < 0x8000) |
| 5635 | bitmap = vmcs12->io_bitmap_a; |
| 5636 | else if (port < 0x10000) |
| 5637 | bitmap = vmcs12->io_bitmap_b; |
| 5638 | else |
| 5639 | return true; |
| 5640 | bitmap += (port & 0x7fff) / 8; |
| 5641 | |
| 5642 | if (last_bitmap != bitmap) |
| 5643 | if (kvm_vcpu_read_guest(vcpu, bitmap, &b, 1)) |
| 5644 | return true; |
| 5645 | if (b & (1 << (port & 7))) |
| 5646 | return true; |
| 5647 | |
| 5648 | port++; |
| 5649 | size--; |
| 5650 | last_bitmap = bitmap; |
| 5651 | } |
| 5652 | |
| 5653 | return false; |
| 5654 | } |
| 5655 | |
Oliver Upton | e71237d | 2020-02-04 15:26:30 -0800 | [diff] [blame] | 5656 | static bool nested_vmx_exit_handled_io(struct kvm_vcpu *vcpu, |
| 5657 | struct vmcs12 *vmcs12) |
| 5658 | { |
| 5659 | unsigned long exit_qualification; |
Oliver Upton | 35a5713 | 2020-02-04 15:26:31 -0800 | [diff] [blame] | 5660 | unsigned short port; |
Oliver Upton | e71237d | 2020-02-04 15:26:30 -0800 | [diff] [blame] | 5661 | int size; |
| 5662 | |
| 5663 | if (!nested_cpu_has(vmcs12, CPU_BASED_USE_IO_BITMAPS)) |
| 5664 | return nested_cpu_has(vmcs12, CPU_BASED_UNCOND_IO_EXITING); |
| 5665 | |
Sean Christopherson | 5addc23 | 2020-04-15 13:34:53 -0700 | [diff] [blame] | 5666 | exit_qualification = vmx_get_exit_qual(vcpu); |
Oliver Upton | e71237d | 2020-02-04 15:26:30 -0800 | [diff] [blame] | 5667 | |
| 5668 | port = exit_qualification >> 16; |
| 5669 | size = (exit_qualification & 7) + 1; |
| 5670 | |
| 5671 | return nested_vmx_check_io_bitmaps(vcpu, port, size); |
| 5672 | } |
| 5673 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5674 | /* |
Miaohe Lin | 463bfee | 2020-02-14 10:44:05 +0800 | [diff] [blame] | 5675 | * 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] | 5676 | * rather than handle it ourselves in L0. I.e., check whether L1 expressed |
| 5677 | * disinterest in the current event (read or write a specific MSR) by using an |
| 5678 | * MSR bitmap. This may be the case even when L0 doesn't use MSR bitmaps. |
| 5679 | */ |
| 5680 | static bool nested_vmx_exit_handled_msr(struct kvm_vcpu *vcpu, |
Sean Christopherson | 8e53324 | 2020-11-06 17:03:12 +0800 | [diff] [blame] | 5681 | struct vmcs12 *vmcs12, |
| 5682 | union vmx_exit_reason exit_reason) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5683 | { |
Sean Christopherson | 2b3eaf8 | 2019-04-30 10:36:19 -0700 | [diff] [blame] | 5684 | u32 msr_index = kvm_rcx_read(vcpu); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5685 | gpa_t bitmap; |
| 5686 | |
| 5687 | if (!nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS)) |
| 5688 | return true; |
| 5689 | |
| 5690 | /* |
| 5691 | * The MSR_BITMAP page is divided into four 1024-byte bitmaps, |
| 5692 | * for the four combinations of read/write and low/high MSR numbers. |
| 5693 | * First we need to figure out which of the four to use: |
| 5694 | */ |
| 5695 | bitmap = vmcs12->msr_bitmap; |
Sean Christopherson | 8e53324 | 2020-11-06 17:03:12 +0800 | [diff] [blame] | 5696 | if (exit_reason.basic == EXIT_REASON_MSR_WRITE) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5697 | bitmap += 2048; |
| 5698 | if (msr_index >= 0xc0000000) { |
| 5699 | msr_index -= 0xc0000000; |
| 5700 | bitmap += 1024; |
| 5701 | } |
| 5702 | |
| 5703 | /* Then read the msr_index'th bit from this bitmap: */ |
| 5704 | if (msr_index < 1024*8) { |
| 5705 | unsigned char b; |
| 5706 | if (kvm_vcpu_read_guest(vcpu, bitmap + msr_index/8, &b, 1)) |
| 5707 | return true; |
| 5708 | return 1 & (b >> (msr_index & 7)); |
| 5709 | } else |
| 5710 | return true; /* let L1 handle the wrong parameter */ |
| 5711 | } |
| 5712 | |
| 5713 | /* |
| 5714 | * Return 1 if we should exit from L2 to L1 to handle a CR access exit, |
| 5715 | * rather than handle it ourselves in L0. I.e., check if L1 wanted to |
| 5716 | * intercept (via guest_host_mask etc.) the current event. |
| 5717 | */ |
| 5718 | static bool nested_vmx_exit_handled_cr(struct kvm_vcpu *vcpu, |
| 5719 | struct vmcs12 *vmcs12) |
| 5720 | { |
Sean Christopherson | 5addc23 | 2020-04-15 13:34:53 -0700 | [diff] [blame] | 5721 | unsigned long exit_qualification = vmx_get_exit_qual(vcpu); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5722 | int cr = exit_qualification & 15; |
| 5723 | int reg; |
| 5724 | unsigned long val; |
| 5725 | |
| 5726 | switch ((exit_qualification >> 4) & 3) { |
| 5727 | case 0: /* mov to cr */ |
| 5728 | reg = (exit_qualification >> 8) & 15; |
Sean Christopherson | 27b4a9c4 | 2021-04-21 19:21:28 -0700 | [diff] [blame] | 5729 | val = kvm_register_read(vcpu, reg); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5730 | switch (cr) { |
| 5731 | case 0: |
| 5732 | if (vmcs12->cr0_guest_host_mask & |
| 5733 | (val ^ vmcs12->cr0_read_shadow)) |
| 5734 | return true; |
| 5735 | break; |
| 5736 | case 3: |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5737 | if (nested_cpu_has(vmcs12, CPU_BASED_CR3_LOAD_EXITING)) |
| 5738 | return true; |
| 5739 | break; |
| 5740 | case 4: |
| 5741 | if (vmcs12->cr4_guest_host_mask & |
| 5742 | (vmcs12->cr4_read_shadow ^ val)) |
| 5743 | return true; |
| 5744 | break; |
| 5745 | case 8: |
| 5746 | if (nested_cpu_has(vmcs12, CPU_BASED_CR8_LOAD_EXITING)) |
| 5747 | return true; |
| 5748 | break; |
| 5749 | } |
| 5750 | break; |
| 5751 | case 2: /* clts */ |
| 5752 | if ((vmcs12->cr0_guest_host_mask & X86_CR0_TS) && |
| 5753 | (vmcs12->cr0_read_shadow & X86_CR0_TS)) |
| 5754 | return true; |
| 5755 | break; |
| 5756 | case 1: /* mov from cr */ |
| 5757 | switch (cr) { |
| 5758 | case 3: |
| 5759 | if (vmcs12->cpu_based_vm_exec_control & |
| 5760 | CPU_BASED_CR3_STORE_EXITING) |
| 5761 | return true; |
| 5762 | break; |
| 5763 | case 8: |
| 5764 | if (vmcs12->cpu_based_vm_exec_control & |
| 5765 | CPU_BASED_CR8_STORE_EXITING) |
| 5766 | return true; |
| 5767 | break; |
| 5768 | } |
| 5769 | break; |
| 5770 | case 3: /* lmsw */ |
| 5771 | /* |
| 5772 | * lmsw can change bits 1..3 of cr0, and only set bit 0 of |
| 5773 | * cr0. Other attempted changes are ignored, with no exit. |
| 5774 | */ |
| 5775 | val = (exit_qualification >> LMSW_SOURCE_DATA_SHIFT) & 0x0f; |
| 5776 | if (vmcs12->cr0_guest_host_mask & 0xe & |
| 5777 | (val ^ vmcs12->cr0_read_shadow)) |
| 5778 | return true; |
| 5779 | if ((vmcs12->cr0_guest_host_mask & 0x1) && |
| 5780 | !(vmcs12->cr0_read_shadow & 0x1) && |
| 5781 | (val & 0x1)) |
| 5782 | return true; |
| 5783 | break; |
| 5784 | } |
| 5785 | return false; |
| 5786 | } |
| 5787 | |
Sean Christopherson | 72add91 | 2021-04-12 16:21:42 +1200 | [diff] [blame] | 5788 | static bool nested_vmx_exit_handled_encls(struct kvm_vcpu *vcpu, |
| 5789 | struct vmcs12 *vmcs12) |
| 5790 | { |
| 5791 | u32 encls_leaf; |
| 5792 | |
| 5793 | if (!guest_cpuid_has(vcpu, X86_FEATURE_SGX) || |
| 5794 | !nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENCLS_EXITING)) |
| 5795 | return false; |
| 5796 | |
| 5797 | encls_leaf = kvm_rax_read(vcpu); |
| 5798 | if (encls_leaf > 62) |
| 5799 | encls_leaf = 63; |
| 5800 | return vmcs12->encls_exiting_bitmap & BIT_ULL(encls_leaf); |
| 5801 | } |
| 5802 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5803 | static bool nested_vmx_exit_handled_vmcs_access(struct kvm_vcpu *vcpu, |
| 5804 | struct vmcs12 *vmcs12, gpa_t bitmap) |
| 5805 | { |
| 5806 | u32 vmx_instruction_info; |
| 5807 | unsigned long field; |
| 5808 | u8 b; |
| 5809 | |
| 5810 | if (!nested_cpu_has_shadow_vmcs(vmcs12)) |
| 5811 | return true; |
| 5812 | |
| 5813 | /* Decode instruction info and find the field to access */ |
| 5814 | vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO); |
| 5815 | field = kvm_register_read(vcpu, (((vmx_instruction_info) >> 28) & 0xf)); |
| 5816 | |
| 5817 | /* Out-of-range fields always cause a VM exit from L2 to L1 */ |
| 5818 | if (field >> 15) |
| 5819 | return true; |
| 5820 | |
| 5821 | if (kvm_vcpu_read_guest(vcpu, bitmap + field/8, &b, 1)) |
| 5822 | return true; |
| 5823 | |
| 5824 | return 1 & (b >> (field & 7)); |
| 5825 | } |
| 5826 | |
Oliver Upton | b045ae9 | 2020-04-14 22:47:45 +0000 | [diff] [blame] | 5827 | static bool nested_vmx_exit_handled_mtf(struct vmcs12 *vmcs12) |
| 5828 | { |
| 5829 | u32 entry_intr_info = vmcs12->vm_entry_intr_info_field; |
| 5830 | |
| 5831 | if (nested_cpu_has_mtf(vmcs12)) |
| 5832 | return true; |
| 5833 | |
| 5834 | /* |
| 5835 | * An MTF VM-exit may be injected into the guest by setting the |
| 5836 | * interruption-type to 7 (other event) and the vector field to 0. Such |
| 5837 | * is the case regardless of the 'monitor trap flag' VM-execution |
| 5838 | * control. |
| 5839 | */ |
| 5840 | return entry_intr_info == (INTR_INFO_VALID_MASK |
| 5841 | | INTR_TYPE_OTHER_EVENT); |
| 5842 | } |
| 5843 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5844 | /* |
Sean Christopherson | 2c1f332 | 2020-04-15 10:55:14 -0700 | [diff] [blame] | 5845 | * Return true if L0 wants to handle an exit from L2 regardless of whether or not |
| 5846 | * 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] | 5847 | */ |
Sean Christopherson | 8e53324 | 2020-11-06 17:03:12 +0800 | [diff] [blame] | 5848 | static bool nested_vmx_l0_wants_exit(struct kvm_vcpu *vcpu, |
| 5849 | union vmx_exit_reason exit_reason) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5850 | { |
Sean Christopherson | 2c1f332 | 2020-04-15 10:55:14 -0700 | [diff] [blame] | 5851 | u32 intr_info; |
| 5852 | |
Sean Christopherson | 8e53324 | 2020-11-06 17:03:12 +0800 | [diff] [blame] | 5853 | switch ((u16)exit_reason.basic) { |
Sean Christopherson | 2c1f332 | 2020-04-15 10:55:14 -0700 | [diff] [blame] | 5854 | case EXIT_REASON_EXCEPTION_NMI: |
Sean Christopherson | 8791585 | 2020-04-15 13:34:54 -0700 | [diff] [blame] | 5855 | intr_info = vmx_get_intr_info(vcpu); |
Sean Christopherson | 2c1f332 | 2020-04-15 10:55:14 -0700 | [diff] [blame] | 5856 | if (is_nmi(intr_info)) |
| 5857 | return true; |
| 5858 | else if (is_page_fault(intr_info)) |
Sean Christopherson | 18712c1 | 2021-08-11 21:56:15 -0700 | [diff] [blame] | 5859 | return vcpu->arch.apf.host_apf_flags || |
| 5860 | vmx_need_pf_intercept(vcpu); |
Sean Christopherson | 2c1f332 | 2020-04-15 10:55:14 -0700 | [diff] [blame] | 5861 | else if (is_debug(intr_info) && |
| 5862 | vcpu->guest_debug & |
| 5863 | (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP)) |
| 5864 | return true; |
| 5865 | else if (is_breakpoint(intr_info) && |
| 5866 | vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP) |
| 5867 | return true; |
Sean Christopherson | b33bb78 | 2021-06-22 10:22:44 -0700 | [diff] [blame] | 5868 | else if (is_alignment_check(intr_info) && |
| 5869 | !vmx_guest_inject_ac(vcpu)) |
| 5870 | return true; |
Sean Christopherson | 2c1f332 | 2020-04-15 10:55:14 -0700 | [diff] [blame] | 5871 | return false; |
| 5872 | case EXIT_REASON_EXTERNAL_INTERRUPT: |
| 5873 | return true; |
| 5874 | case EXIT_REASON_MCE_DURING_VMENTRY: |
| 5875 | return true; |
| 5876 | case EXIT_REASON_EPT_VIOLATION: |
| 5877 | /* |
| 5878 | * L0 always deals with the EPT violation. If nested EPT is |
| 5879 | * used, and the nested mmu code discovers that the address is |
| 5880 | * missing in the guest EPT table (EPT12), the EPT violation |
| 5881 | * will be injected with nested_ept_inject_page_fault() |
| 5882 | */ |
| 5883 | return true; |
| 5884 | case EXIT_REASON_EPT_MISCONFIG: |
| 5885 | /* |
| 5886 | * L2 never uses directly L1's EPT, but rather L0's own EPT |
| 5887 | * table (shadow on EPT) or a merged EPT table that L0 built |
| 5888 | * (EPT on EPT). So any problems with the structure of the |
| 5889 | * table is L0's fault. |
| 5890 | */ |
| 5891 | return true; |
| 5892 | case EXIT_REASON_PREEMPTION_TIMER: |
| 5893 | return true; |
| 5894 | case EXIT_REASON_PML_FULL: |
Sean Christopherson | c3bb9a2 | 2021-02-12 16:50:07 -0800 | [diff] [blame] | 5895 | /* |
| 5896 | * PML is emulated for an L1 VMM and should never be enabled in |
| 5897 | * vmcs02, always "handle" PML_FULL by exiting to userspace. |
| 5898 | */ |
Sean Christopherson | 2c1f332 | 2020-04-15 10:55:14 -0700 | [diff] [blame] | 5899 | return true; |
| 5900 | case EXIT_REASON_VMFUNC: |
| 5901 | /* VM functions are emulated through L2->L0 vmexits. */ |
| 5902 | return true; |
Chenyi Qiang | 24a996a | 2021-09-14 17:50:41 +0800 | [diff] [blame] | 5903 | case EXIT_REASON_BUS_LOCK: |
| 5904 | /* |
| 5905 | * At present, bus lock VM exit is never exposed to L1. |
| 5906 | * Handle L2's bus locks in L0 directly. |
| 5907 | */ |
| 5908 | return true; |
Sean Christopherson | 2c1f332 | 2020-04-15 10:55:14 -0700 | [diff] [blame] | 5909 | default: |
| 5910 | break; |
| 5911 | } |
| 5912 | return false; |
| 5913 | } |
| 5914 | |
| 5915 | /* |
| 5916 | * Return 1 if L1 wants to intercept an exit from L2. Only call this when in |
| 5917 | * is_guest_mode (L2). |
| 5918 | */ |
Sean Christopherson | 8e53324 | 2020-11-06 17:03:12 +0800 | [diff] [blame] | 5919 | static bool nested_vmx_l1_wants_exit(struct kvm_vcpu *vcpu, |
| 5920 | union vmx_exit_reason exit_reason) |
Sean Christopherson | 2c1f332 | 2020-04-15 10:55:14 -0700 | [diff] [blame] | 5921 | { |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5922 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
Sean Christopherson | 9bd4af2 | 2020-04-21 00:53:27 -0700 | [diff] [blame] | 5923 | u32 intr_info; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5924 | |
Sean Christopherson | 8e53324 | 2020-11-06 17:03:12 +0800 | [diff] [blame] | 5925 | switch ((u16)exit_reason.basic) { |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5926 | case EXIT_REASON_EXCEPTION_NMI: |
Sean Christopherson | 8791585 | 2020-04-15 13:34:54 -0700 | [diff] [blame] | 5927 | intr_info = vmx_get_intr_info(vcpu); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5928 | if (is_nmi(intr_info)) |
Sean Christopherson | 2c1f332 | 2020-04-15 10:55:14 -0700 | [diff] [blame] | 5929 | return true; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5930 | else if (is_page_fault(intr_info)) |
Sean Christopherson | 2c1f332 | 2020-04-15 10:55:14 -0700 | [diff] [blame] | 5931 | return true; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5932 | return vmcs12->exception_bitmap & |
| 5933 | (1u << (intr_info & INTR_INFO_VECTOR_MASK)); |
| 5934 | case EXIT_REASON_EXTERNAL_INTERRUPT: |
Sean Christopherson | 2c1f332 | 2020-04-15 10:55:14 -0700 | [diff] [blame] | 5935 | return nested_exit_on_intr(vcpu); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5936 | case EXIT_REASON_TRIPLE_FAULT: |
| 5937 | return true; |
Xiaoyao Li | 9dadc2f | 2019-12-06 16:45:24 +0800 | [diff] [blame] | 5938 | case EXIT_REASON_INTERRUPT_WINDOW: |
| 5939 | return nested_cpu_has(vmcs12, CPU_BASED_INTR_WINDOW_EXITING); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5940 | case EXIT_REASON_NMI_WINDOW: |
Xiaoyao Li | 4e2a0bc | 2019-12-06 16:45:25 +0800 | [diff] [blame] | 5941 | return nested_cpu_has(vmcs12, CPU_BASED_NMI_WINDOW_EXITING); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5942 | case EXIT_REASON_TASK_SWITCH: |
| 5943 | return true; |
| 5944 | case EXIT_REASON_CPUID: |
| 5945 | return true; |
| 5946 | case EXIT_REASON_HLT: |
| 5947 | return nested_cpu_has(vmcs12, CPU_BASED_HLT_EXITING); |
| 5948 | case EXIT_REASON_INVD: |
| 5949 | return true; |
| 5950 | case EXIT_REASON_INVLPG: |
| 5951 | return nested_cpu_has(vmcs12, CPU_BASED_INVLPG_EXITING); |
| 5952 | case EXIT_REASON_RDPMC: |
| 5953 | return nested_cpu_has(vmcs12, CPU_BASED_RDPMC_EXITING); |
| 5954 | case EXIT_REASON_RDRAND: |
| 5955 | return nested_cpu_has2(vmcs12, SECONDARY_EXEC_RDRAND_EXITING); |
| 5956 | case EXIT_REASON_RDSEED: |
| 5957 | return nested_cpu_has2(vmcs12, SECONDARY_EXEC_RDSEED_EXITING); |
| 5958 | case EXIT_REASON_RDTSC: case EXIT_REASON_RDTSCP: |
| 5959 | return nested_cpu_has(vmcs12, CPU_BASED_RDTSC_EXITING); |
| 5960 | case EXIT_REASON_VMREAD: |
| 5961 | return nested_vmx_exit_handled_vmcs_access(vcpu, vmcs12, |
| 5962 | vmcs12->vmread_bitmap); |
| 5963 | case EXIT_REASON_VMWRITE: |
| 5964 | return nested_vmx_exit_handled_vmcs_access(vcpu, vmcs12, |
| 5965 | vmcs12->vmwrite_bitmap); |
| 5966 | case EXIT_REASON_VMCALL: case EXIT_REASON_VMCLEAR: |
| 5967 | case EXIT_REASON_VMLAUNCH: case EXIT_REASON_VMPTRLD: |
| 5968 | case EXIT_REASON_VMPTRST: case EXIT_REASON_VMRESUME: |
| 5969 | case EXIT_REASON_VMOFF: case EXIT_REASON_VMON: |
| 5970 | case EXIT_REASON_INVEPT: case EXIT_REASON_INVVPID: |
| 5971 | /* |
| 5972 | * VMX instructions trap unconditionally. This allows L1 to |
| 5973 | * emulate them for its L2 guest, i.e., allows 3-level nesting! |
| 5974 | */ |
| 5975 | return true; |
| 5976 | case EXIT_REASON_CR_ACCESS: |
| 5977 | return nested_vmx_exit_handled_cr(vcpu, vmcs12); |
| 5978 | case EXIT_REASON_DR_ACCESS: |
| 5979 | return nested_cpu_has(vmcs12, CPU_BASED_MOV_DR_EXITING); |
| 5980 | case EXIT_REASON_IO_INSTRUCTION: |
| 5981 | return nested_vmx_exit_handled_io(vcpu, vmcs12); |
| 5982 | case EXIT_REASON_GDTR_IDTR: case EXIT_REASON_LDTR_TR: |
| 5983 | return nested_cpu_has2(vmcs12, SECONDARY_EXEC_DESC); |
| 5984 | case EXIT_REASON_MSR_READ: |
| 5985 | case EXIT_REASON_MSR_WRITE: |
| 5986 | return nested_vmx_exit_handled_msr(vcpu, vmcs12, exit_reason); |
| 5987 | case EXIT_REASON_INVALID_STATE: |
| 5988 | return true; |
| 5989 | case EXIT_REASON_MWAIT_INSTRUCTION: |
| 5990 | return nested_cpu_has(vmcs12, CPU_BASED_MWAIT_EXITING); |
| 5991 | case EXIT_REASON_MONITOR_TRAP_FLAG: |
Oliver Upton | b045ae9 | 2020-04-14 22:47:45 +0000 | [diff] [blame] | 5992 | return nested_vmx_exit_handled_mtf(vmcs12); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5993 | case EXIT_REASON_MONITOR_INSTRUCTION: |
| 5994 | return nested_cpu_has(vmcs12, CPU_BASED_MONITOR_EXITING); |
| 5995 | case EXIT_REASON_PAUSE_INSTRUCTION: |
| 5996 | return nested_cpu_has(vmcs12, CPU_BASED_PAUSE_EXITING) || |
| 5997 | nested_cpu_has2(vmcs12, |
| 5998 | SECONDARY_EXEC_PAUSE_LOOP_EXITING); |
| 5999 | case EXIT_REASON_MCE_DURING_VMENTRY: |
Sean Christopherson | 2c1f332 | 2020-04-15 10:55:14 -0700 | [diff] [blame] | 6000 | return true; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6001 | case EXIT_REASON_TPR_BELOW_THRESHOLD: |
| 6002 | return nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW); |
| 6003 | case EXIT_REASON_APIC_ACCESS: |
| 6004 | case EXIT_REASON_APIC_WRITE: |
| 6005 | case EXIT_REASON_EOI_INDUCED: |
| 6006 | /* |
| 6007 | * The controls for "virtualize APIC accesses," "APIC- |
| 6008 | * register virtualization," and "virtual-interrupt |
| 6009 | * delivery" only come from vmcs12. |
| 6010 | */ |
| 6011 | return true; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6012 | case EXIT_REASON_INVPCID: |
| 6013 | return |
| 6014 | nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_INVPCID) && |
| 6015 | nested_cpu_has(vmcs12, CPU_BASED_INVLPG_EXITING); |
| 6016 | case EXIT_REASON_WBINVD: |
| 6017 | return nested_cpu_has2(vmcs12, SECONDARY_EXEC_WBINVD_EXITING); |
| 6018 | case EXIT_REASON_XSETBV: |
| 6019 | return true; |
| 6020 | case EXIT_REASON_XSAVES: case EXIT_REASON_XRSTORS: |
| 6021 | /* |
| 6022 | * This should never happen, since it is not possible to |
| 6023 | * set XSS to a non-zero value---neither in L1 nor in L2. |
| 6024 | * If if it were, XSS would have to be checked against |
| 6025 | * the XSS exit bitmap in vmcs12. |
| 6026 | */ |
| 6027 | return nested_cpu_has2(vmcs12, SECONDARY_EXEC_XSAVES); |
Tao Xu | bf653b7 | 2019-07-16 14:55:51 +0800 | [diff] [blame] | 6028 | case EXIT_REASON_UMWAIT: |
| 6029 | case EXIT_REASON_TPAUSE: |
| 6030 | return nested_cpu_has2(vmcs12, |
| 6031 | SECONDARY_EXEC_ENABLE_USR_WAIT_PAUSE); |
Sean Christopherson | 72add91 | 2021-04-12 16:21:42 +1200 | [diff] [blame] | 6032 | case EXIT_REASON_ENCLS: |
| 6033 | return nested_vmx_exit_handled_encls(vcpu, vmcs12); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6034 | default: |
| 6035 | return true; |
| 6036 | } |
| 6037 | } |
| 6038 | |
Sean Christopherson | 7b7bd87 | 2020-04-15 10:55:11 -0700 | [diff] [blame] | 6039 | /* |
| 6040 | * Conditionally reflect a VM-Exit into L1. Returns %true if the VM-Exit was |
| 6041 | * reflected into L1. |
| 6042 | */ |
Sean Christopherson | f47baae | 2020-04-15 10:55:16 -0700 | [diff] [blame] | 6043 | bool nested_vmx_reflect_vmexit(struct kvm_vcpu *vcpu) |
Sean Christopherson | 7b7bd87 | 2020-04-15 10:55:11 -0700 | [diff] [blame] | 6044 | { |
Sean Christopherson | fbdd502 | 2020-04-15 10:55:12 -0700 | [diff] [blame] | 6045 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Sean Christopherson | 8e53324 | 2020-11-06 17:03:12 +0800 | [diff] [blame] | 6046 | union vmx_exit_reason exit_reason = vmx->exit_reason; |
Sean Christopherson | 8779655 | 2020-04-22 17:11:27 -0700 | [diff] [blame] | 6047 | unsigned long exit_qual; |
| 6048 | u32 exit_intr_info; |
Sean Christopherson | fbdd502 | 2020-04-15 10:55:12 -0700 | [diff] [blame] | 6049 | |
| 6050 | WARN_ON_ONCE(vmx->nested.nested_run_pending); |
| 6051 | |
| 6052 | /* |
| 6053 | * Late nested VM-Fail shares the same flow as nested VM-Exit since KVM |
| 6054 | * has already loaded L2's state. |
| 6055 | */ |
| 6056 | if (unlikely(vmx->fail)) { |
| 6057 | trace_kvm_nested_vmenter_failed( |
| 6058 | "hardware VM-instruction error: ", |
| 6059 | vmcs_read32(VM_INSTRUCTION_ERROR)); |
| 6060 | exit_intr_info = 0; |
| 6061 | exit_qual = 0; |
| 6062 | goto reflect_vmexit; |
| 6063 | } |
Sean Christopherson | 7b7bd87 | 2020-04-15 10:55:11 -0700 | [diff] [blame] | 6064 | |
David Edmondson | 0a62a03 | 2021-09-20 11:37:35 +0100 | [diff] [blame] | 6065 | trace_kvm_nested_vmexit(vcpu, KVM_ISA_VMX); |
Sean Christopherson | 236871b | 2020-04-15 10:55:13 -0700 | [diff] [blame] | 6066 | |
Sean Christopherson | 2c1f332 | 2020-04-15 10:55:14 -0700 | [diff] [blame] | 6067 | /* If L0 (KVM) wants the exit, it trumps L1's desires. */ |
| 6068 | if (nested_vmx_l0_wants_exit(vcpu, exit_reason)) |
| 6069 | return false; |
| 6070 | |
| 6071 | /* If L1 doesn't want the exit, handle it in L0. */ |
| 6072 | if (!nested_vmx_l1_wants_exit(vcpu, exit_reason)) |
Sean Christopherson | 7b7bd87 | 2020-04-15 10:55:11 -0700 | [diff] [blame] | 6073 | return false; |
| 6074 | |
| 6075 | /* |
Sean Christopherson | 1d28306 | 2020-04-15 10:55:15 -0700 | [diff] [blame] | 6076 | * vmcs.VM_EXIT_INTR_INFO is only valid for EXCEPTION_NMI exits. For |
| 6077 | * EXTERNAL_INTERRUPT, the value for vmcs12->vm_exit_intr_info would |
| 6078 | * need to be synthesized by querying the in-kernel LAPIC, but external |
| 6079 | * interrupts are never reflected to L1 so it's a non-issue. |
Sean Christopherson | 7b7bd87 | 2020-04-15 10:55:11 -0700 | [diff] [blame] | 6080 | */ |
Sean Christopherson | 02f1965 | 2020-09-23 13:13:49 -0700 | [diff] [blame] | 6081 | exit_intr_info = vmx_get_intr_info(vcpu); |
Sean Christopherson | f315f2b | 2020-09-23 13:13:45 -0700 | [diff] [blame] | 6082 | if (is_exception_with_error_code(exit_intr_info)) { |
Sean Christopherson | 7b7bd87 | 2020-04-15 10:55:11 -0700 | [diff] [blame] | 6083 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
| 6084 | |
| 6085 | vmcs12->vm_exit_intr_error_code = |
| 6086 | vmcs_read32(VM_EXIT_INTR_ERROR_CODE); |
| 6087 | } |
Sean Christopherson | 02f1965 | 2020-09-23 13:13:49 -0700 | [diff] [blame] | 6088 | exit_qual = vmx_get_exit_qual(vcpu); |
Sean Christopherson | 7b7bd87 | 2020-04-15 10:55:11 -0700 | [diff] [blame] | 6089 | |
Sean Christopherson | fbdd502 | 2020-04-15 10:55:12 -0700 | [diff] [blame] | 6090 | reflect_vmexit: |
Sean Christopherson | 8e53324 | 2020-11-06 17:03:12 +0800 | [diff] [blame] | 6091 | nested_vmx_vmexit(vcpu, exit_reason.full, exit_intr_info, exit_qual); |
Sean Christopherson | 7b7bd87 | 2020-04-15 10:55:11 -0700 | [diff] [blame] | 6092 | return true; |
| 6093 | } |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6094 | |
| 6095 | static int vmx_get_nested_state(struct kvm_vcpu *vcpu, |
| 6096 | struct kvm_nested_state __user *user_kvm_nested_state, |
| 6097 | u32 user_data_size) |
| 6098 | { |
| 6099 | struct vcpu_vmx *vmx; |
| 6100 | struct vmcs12 *vmcs12; |
| 6101 | struct kvm_nested_state kvm_state = { |
| 6102 | .flags = 0, |
Liran Alon | 6ca00df | 2019-06-16 15:03:10 +0300 | [diff] [blame] | 6103 | .format = KVM_STATE_NESTED_FORMAT_VMX, |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6104 | .size = sizeof(kvm_state), |
Peter Shier | 850448f | 2020-05-26 14:51:06 -0700 | [diff] [blame] | 6105 | .hdr.vmx.flags = 0, |
Yu Zhang | 64c7850 | 2021-09-30 01:51:53 +0800 | [diff] [blame] | 6106 | .hdr.vmx.vmxon_pa = INVALID_GPA, |
| 6107 | .hdr.vmx.vmcs12_pa = INVALID_GPA, |
Peter Shier | 850448f | 2020-05-26 14:51:06 -0700 | [diff] [blame] | 6108 | .hdr.vmx.preemption_timer_deadline = 0, |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6109 | }; |
Liran Alon | 6ca00df | 2019-06-16 15:03:10 +0300 | [diff] [blame] | 6110 | struct kvm_vmx_nested_state_data __user *user_vmx_nested_state = |
| 6111 | &user_kvm_nested_state->data.vmx[0]; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6112 | |
| 6113 | if (!vcpu) |
Liran Alon | 6ca00df | 2019-06-16 15:03:10 +0300 | [diff] [blame] | 6114 | return kvm_state.size + sizeof(*user_vmx_nested_state); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6115 | |
| 6116 | vmx = to_vmx(vcpu); |
| 6117 | vmcs12 = get_vmcs12(vcpu); |
| 6118 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6119 | if (nested_vmx_allowed(vcpu) && |
| 6120 | (vmx->nested.vmxon || vmx->nested.smm.vmxon)) { |
Liran Alon | 6ca00df | 2019-06-16 15:03:10 +0300 | [diff] [blame] | 6121 | kvm_state.hdr.vmx.vmxon_pa = vmx->nested.vmxon_ptr; |
| 6122 | kvm_state.hdr.vmx.vmcs12_pa = vmx->nested.current_vmptr; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6123 | |
| 6124 | if (vmx_has_valid_vmcs12(vcpu)) { |
Liran Alon | 6ca00df | 2019-06-16 15:03:10 +0300 | [diff] [blame] | 6125 | kvm_state.size += sizeof(user_vmx_nested_state->vmcs12); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6126 | |
Vitaly Kuznetsov | 2784996 | 2021-05-26 15:20:20 +0200 | [diff] [blame] | 6127 | /* 'hv_evmcs_vmptr' can also be EVMPTR_MAP_PENDING here */ |
| 6128 | if (vmx->nested.hv_evmcs_vmptr != EVMPTR_INVALID) |
Liran Alon | 323d73a | 2019-06-26 16:09:27 +0300 | [diff] [blame] | 6129 | kvm_state.flags |= KVM_STATE_NESTED_EVMCS; |
| 6130 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6131 | if (is_guest_mode(vcpu) && |
| 6132 | nested_cpu_has_shadow_vmcs(vmcs12) && |
Yu Zhang | 64c7850 | 2021-09-30 01:51:53 +0800 | [diff] [blame] | 6133 | vmcs12->vmcs_link_pointer != INVALID_GPA) |
Liran Alon | 6ca00df | 2019-06-16 15:03:10 +0300 | [diff] [blame] | 6134 | kvm_state.size += sizeof(user_vmx_nested_state->shadow_vmcs12); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6135 | } |
| 6136 | |
| 6137 | if (vmx->nested.smm.vmxon) |
Liran Alon | 6ca00df | 2019-06-16 15:03:10 +0300 | [diff] [blame] | 6138 | kvm_state.hdr.vmx.smm.flags |= KVM_STATE_NESTED_SMM_VMXON; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6139 | |
| 6140 | if (vmx->nested.smm.guest_mode) |
Liran Alon | 6ca00df | 2019-06-16 15:03:10 +0300 | [diff] [blame] | 6141 | kvm_state.hdr.vmx.smm.flags |= KVM_STATE_NESTED_SMM_GUEST_MODE; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6142 | |
| 6143 | if (is_guest_mode(vcpu)) { |
| 6144 | kvm_state.flags |= KVM_STATE_NESTED_GUEST_MODE; |
| 6145 | |
| 6146 | if (vmx->nested.nested_run_pending) |
| 6147 | kvm_state.flags |= KVM_STATE_NESTED_RUN_PENDING; |
Oliver Upton | 5ef8acb | 2020-02-07 02:36:07 -0800 | [diff] [blame] | 6148 | |
| 6149 | if (vmx->nested.mtf_pending) |
| 6150 | kvm_state.flags |= KVM_STATE_NESTED_MTF_PENDING; |
Peter Shier | 850448f | 2020-05-26 14:51:06 -0700 | [diff] [blame] | 6151 | |
| 6152 | if (nested_cpu_has_preemption_timer(vmcs12) && |
| 6153 | vmx->nested.has_preemption_timer_deadline) { |
| 6154 | kvm_state.hdr.vmx.flags |= |
| 6155 | KVM_STATE_VMX_PREEMPTION_TIMER_DEADLINE; |
| 6156 | kvm_state.hdr.vmx.preemption_timer_deadline = |
| 6157 | vmx->nested.preemption_timer_deadline; |
| 6158 | } |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6159 | } |
| 6160 | } |
| 6161 | |
| 6162 | if (user_data_size < kvm_state.size) |
| 6163 | goto out; |
| 6164 | |
| 6165 | if (copy_to_user(user_kvm_nested_state, &kvm_state, sizeof(kvm_state))) |
| 6166 | return -EFAULT; |
| 6167 | |
| 6168 | if (!vmx_has_valid_vmcs12(vcpu)) |
| 6169 | goto out; |
| 6170 | |
| 6171 | /* |
| 6172 | * When running L2, the authoritative vmcs12 state is in the |
| 6173 | * vmcs02. When running L1, the authoritative vmcs12 state is |
| 6174 | * in the shadow or enlightened vmcs linked to vmcs01, unless |
Sean Christopherson | 3731905ef | 2019-05-07 08:36:27 -0700 | [diff] [blame] | 6175 | * need_vmcs12_to_shadow_sync is set, in which case, the authoritative |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6176 | * vmcs12 state is in the vmcs12 already. |
| 6177 | */ |
| 6178 | if (is_guest_mode(vcpu)) { |
Sean Christopherson | 3731905ef | 2019-05-07 08:36:27 -0700 | [diff] [blame] | 6179 | sync_vmcs02_to_vmcs12(vcpu, vmcs12); |
Sean Christopherson | 7952d76 | 2019-05-07 08:36:29 -0700 | [diff] [blame] | 6180 | sync_vmcs02_to_vmcs12_rare(vcpu, vmcs12); |
Maxim Levitsky | d51e1d3 | 2021-01-14 22:54:47 +0200 | [diff] [blame] | 6181 | } else { |
| 6182 | copy_vmcs02_to_vmcs12_rare(vcpu, get_vmcs12(vcpu)); |
| 6183 | if (!vmx->nested.need_vmcs12_to_shadow_sync) { |
Vitaly Kuznetsov | 1e9dfbd | 2021-05-26 15:20:16 +0200 | [diff] [blame] | 6184 | if (evmptr_is_valid(vmx->nested.hv_evmcs_vmptr)) |
Vitaly Kuznetsov | d6bf71a | 2021-05-26 15:20:22 +0200 | [diff] [blame] | 6185 | /* |
| 6186 | * L1 hypervisor is not obliged to keep eVMCS |
| 6187 | * clean fields data always up-to-date while |
| 6188 | * not in guest mode, 'hv_clean_fields' is only |
| 6189 | * supposed to be actual upon vmentry so we need |
| 6190 | * to ignore it here and do full copy. |
| 6191 | */ |
| 6192 | copy_enlightened_to_vmcs12(vmx, 0); |
Maxim Levitsky | d51e1d3 | 2021-01-14 22:54:47 +0200 | [diff] [blame] | 6193 | else if (enable_shadow_vmcs) |
| 6194 | copy_shadow_to_vmcs12(vmx); |
| 6195 | } |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6196 | } |
| 6197 | |
Liran Alon | 6ca00df | 2019-06-16 15:03:10 +0300 | [diff] [blame] | 6198 | BUILD_BUG_ON(sizeof(user_vmx_nested_state->vmcs12) < VMCS12_SIZE); |
| 6199 | BUILD_BUG_ON(sizeof(user_vmx_nested_state->shadow_vmcs12) < VMCS12_SIZE); |
| 6200 | |
Tom Roeder | 3a33d03 | 2019-01-24 13:48:20 -0800 | [diff] [blame] | 6201 | /* |
| 6202 | * Copy over the full allocated size of vmcs12 rather than just the size |
| 6203 | * of the struct. |
| 6204 | */ |
Liran Alon | 6ca00df | 2019-06-16 15:03:10 +0300 | [diff] [blame] | 6205 | if (copy_to_user(user_vmx_nested_state->vmcs12, vmcs12, VMCS12_SIZE)) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6206 | return -EFAULT; |
| 6207 | |
| 6208 | if (nested_cpu_has_shadow_vmcs(vmcs12) && |
Yu Zhang | 64c7850 | 2021-09-30 01:51:53 +0800 | [diff] [blame] | 6209 | vmcs12->vmcs_link_pointer != INVALID_GPA) { |
Liran Alon | 6ca00df | 2019-06-16 15:03:10 +0300 | [diff] [blame] | 6210 | if (copy_to_user(user_vmx_nested_state->shadow_vmcs12, |
Tom Roeder | 3a33d03 | 2019-01-24 13:48:20 -0800 | [diff] [blame] | 6211 | get_shadow_vmcs12(vcpu), VMCS12_SIZE)) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6212 | return -EFAULT; |
| 6213 | } |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6214 | out: |
| 6215 | return kvm_state.size; |
| 6216 | } |
| 6217 | |
| 6218 | /* |
| 6219 | * Forcibly leave nested mode in order to be able to reset the VCPU later on. |
| 6220 | */ |
| 6221 | void vmx_leave_nested(struct kvm_vcpu *vcpu) |
| 6222 | { |
| 6223 | if (is_guest_mode(vcpu)) { |
| 6224 | to_vmx(vcpu)->nested.nested_run_pending = 0; |
| 6225 | nested_vmx_vmexit(vcpu, -1, 0, 0); |
| 6226 | } |
| 6227 | free_nested(vcpu); |
| 6228 | } |
| 6229 | |
| 6230 | static int vmx_set_nested_state(struct kvm_vcpu *vcpu, |
| 6231 | struct kvm_nested_state __user *user_kvm_nested_state, |
| 6232 | struct kvm_nested_state *kvm_state) |
| 6233 | { |
| 6234 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 6235 | struct vmcs12 *vmcs12; |
Sean Christopherson | 68cda40 | 2020-05-11 15:05:29 -0700 | [diff] [blame] | 6236 | enum vm_entry_failure_code ignored; |
Liran Alon | 6ca00df | 2019-06-16 15:03:10 +0300 | [diff] [blame] | 6237 | struct kvm_vmx_nested_state_data __user *user_vmx_nested_state = |
| 6238 | &user_kvm_nested_state->data.vmx[0]; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6239 | int ret; |
| 6240 | |
Liran Alon | 6ca00df | 2019-06-16 15:03:10 +0300 | [diff] [blame] | 6241 | if (kvm_state->format != KVM_STATE_NESTED_FORMAT_VMX) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6242 | return -EINVAL; |
| 6243 | |
Yu Zhang | 64c7850 | 2021-09-30 01:51:53 +0800 | [diff] [blame] | 6244 | if (kvm_state->hdr.vmx.vmxon_pa == INVALID_GPA) { |
Liran Alon | 6ca00df | 2019-06-16 15:03:10 +0300 | [diff] [blame] | 6245 | if (kvm_state->hdr.vmx.smm.flags) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6246 | return -EINVAL; |
| 6247 | |
Yu Zhang | 64c7850 | 2021-09-30 01:51:53 +0800 | [diff] [blame] | 6248 | if (kvm_state->hdr.vmx.vmcs12_pa != INVALID_GPA) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6249 | return -EINVAL; |
| 6250 | |
Liran Alon | 323d73a | 2019-06-26 16:09:27 +0300 | [diff] [blame] | 6251 | /* |
| 6252 | * KVM_STATE_NESTED_EVMCS used to signal that KVM should |
| 6253 | * enable eVMCS capability on vCPU. However, since then |
| 6254 | * code was changed such that flag signals vmcs12 should |
| 6255 | * be copied into eVMCS in guest memory. |
| 6256 | * |
| 6257 | * To preserve backwards compatability, allow user |
| 6258 | * to set this flag even when there is no VMXON region. |
| 6259 | */ |
Paolo Bonzini | 9fd5887 | 2019-06-19 16:52:27 +0200 | [diff] [blame] | 6260 | if (kvm_state->flags & ~KVM_STATE_NESTED_EVMCS) |
| 6261 | return -EINVAL; |
| 6262 | } else { |
| 6263 | if (!nested_vmx_allowed(vcpu)) |
| 6264 | return -EINVAL; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6265 | |
Paolo Bonzini | 9fd5887 | 2019-06-19 16:52:27 +0200 | [diff] [blame] | 6266 | if (!page_address_valid(vcpu, kvm_state->hdr.vmx.vmxon_pa)) |
| 6267 | return -EINVAL; |
Liran Alon | 323d73a | 2019-06-26 16:09:27 +0300 | [diff] [blame] | 6268 | } |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6269 | |
Liran Alon | 6ca00df | 2019-06-16 15:03:10 +0300 | [diff] [blame] | 6270 | 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] | 6271 | (kvm_state->flags & KVM_STATE_NESTED_GUEST_MODE)) |
| 6272 | return -EINVAL; |
| 6273 | |
Liran Alon | 6ca00df | 2019-06-16 15:03:10 +0300 | [diff] [blame] | 6274 | if (kvm_state->hdr.vmx.smm.flags & |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6275 | ~(KVM_STATE_NESTED_SMM_GUEST_MODE | KVM_STATE_NESTED_SMM_VMXON)) |
| 6276 | return -EINVAL; |
| 6277 | |
Paolo Bonzini | 5e105c8 | 2020-07-27 08:55:09 -0400 | [diff] [blame] | 6278 | if (kvm_state->hdr.vmx.flags & ~KVM_STATE_VMX_PREEMPTION_TIMER_DEADLINE) |
| 6279 | return -EINVAL; |
| 6280 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6281 | /* |
| 6282 | * SMM temporarily disables VMX, so we cannot be in guest mode, |
| 6283 | * nor can VMLAUNCH/VMRESUME be pending. Outside SMM, SMM flags |
| 6284 | * must be zero. |
| 6285 | */ |
Liran Alon | 65b712f1 | 2019-06-25 14:26:42 +0300 | [diff] [blame] | 6286 | if (is_smm(vcpu) ? |
| 6287 | (kvm_state->flags & |
| 6288 | (KVM_STATE_NESTED_GUEST_MODE | KVM_STATE_NESTED_RUN_PENDING)) |
| 6289 | : kvm_state->hdr.vmx.smm.flags) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6290 | return -EINVAL; |
| 6291 | |
Liran Alon | 6ca00df | 2019-06-16 15:03:10 +0300 | [diff] [blame] | 6292 | if ((kvm_state->hdr.vmx.smm.flags & KVM_STATE_NESTED_SMM_GUEST_MODE) && |
| 6293 | !(kvm_state->hdr.vmx.smm.flags & KVM_STATE_NESTED_SMM_VMXON)) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6294 | return -EINVAL; |
| 6295 | |
Liran Alon | 323d73a | 2019-06-26 16:09:27 +0300 | [diff] [blame] | 6296 | if ((kvm_state->flags & KVM_STATE_NESTED_EVMCS) && |
| 6297 | (!nested_vmx_allowed(vcpu) || !vmx->nested.enlightened_vmcs_enabled)) |
Paolo Bonzini | 9fd5887 | 2019-06-19 16:52:27 +0200 | [diff] [blame] | 6298 | return -EINVAL; |
| 6299 | |
Liran Alon | 323d73a | 2019-06-26 16:09:27 +0300 | [diff] [blame] | 6300 | vmx_leave_nested(vcpu); |
Paolo Bonzini | 9fd5887 | 2019-06-19 16:52:27 +0200 | [diff] [blame] | 6301 | |
Yu Zhang | 64c7850 | 2021-09-30 01:51:53 +0800 | [diff] [blame] | 6302 | if (kvm_state->hdr.vmx.vmxon_pa == INVALID_GPA) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6303 | return 0; |
| 6304 | |
Liran Alon | 6ca00df | 2019-06-16 15:03:10 +0300 | [diff] [blame] | 6305 | vmx->nested.vmxon_ptr = kvm_state->hdr.vmx.vmxon_pa; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6306 | ret = enter_vmx_operation(vcpu); |
| 6307 | if (ret) |
| 6308 | return ret; |
| 6309 | |
Paolo Bonzini | 0f02bd0 | 2020-07-27 09:00:37 -0400 | [diff] [blame] | 6310 | /* Empty 'VMXON' state is permitted if no VMCS loaded */ |
| 6311 | if (kvm_state->size < sizeof(*kvm_state) + sizeof(*vmcs12)) { |
| 6312 | /* See vmx_has_valid_vmcs12. */ |
| 6313 | if ((kvm_state->flags & KVM_STATE_NESTED_GUEST_MODE) || |
| 6314 | (kvm_state->flags & KVM_STATE_NESTED_EVMCS) || |
Yu Zhang | 64c7850 | 2021-09-30 01:51:53 +0800 | [diff] [blame] | 6315 | (kvm_state->hdr.vmx.vmcs12_pa != INVALID_GPA)) |
Paolo Bonzini | 0f02bd0 | 2020-07-27 09:00:37 -0400 | [diff] [blame] | 6316 | return -EINVAL; |
| 6317 | else |
| 6318 | return 0; |
| 6319 | } |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6320 | |
Yu Zhang | 64c7850 | 2021-09-30 01:51:53 +0800 | [diff] [blame] | 6321 | if (kvm_state->hdr.vmx.vmcs12_pa != INVALID_GPA) { |
Liran Alon | 6ca00df | 2019-06-16 15:03:10 +0300 | [diff] [blame] | 6322 | if (kvm_state->hdr.vmx.vmcs12_pa == kvm_state->hdr.vmx.vmxon_pa || |
| 6323 | !page_address_valid(vcpu, kvm_state->hdr.vmx.vmcs12_pa)) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6324 | return -EINVAL; |
| 6325 | |
Liran Alon | 6ca00df | 2019-06-16 15:03:10 +0300 | [diff] [blame] | 6326 | set_current_vmptr(vmx, kvm_state->hdr.vmx.vmcs12_pa); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6327 | } else if (kvm_state->flags & KVM_STATE_NESTED_EVMCS) { |
| 6328 | /* |
Vitaly Kuznetsov | e942dbf | 2020-03-09 16:52:12 +0100 | [diff] [blame] | 6329 | * nested_vmx_handle_enlightened_vmptrld() cannot be called |
| 6330 | * directly from here as HV_X64_MSR_VP_ASSIST_PAGE may not be |
| 6331 | * restored yet. EVMCS will be mapped from |
| 6332 | * nested_get_vmcs12_pages(). |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6333 | */ |
Vitaly Kuznetsov | 2784996 | 2021-05-26 15:20:20 +0200 | [diff] [blame] | 6334 | vmx->nested.hv_evmcs_vmptr = EVMPTR_MAP_PENDING; |
Paolo Bonzini | 729c15c | 2020-09-22 06:53:57 -0400 | [diff] [blame] | 6335 | kvm_make_request(KVM_REQ_GET_NESTED_STATE_PAGES, vcpu); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6336 | } else { |
| 6337 | return -EINVAL; |
| 6338 | } |
| 6339 | |
Liran Alon | 6ca00df | 2019-06-16 15:03:10 +0300 | [diff] [blame] | 6340 | if (kvm_state->hdr.vmx.smm.flags & KVM_STATE_NESTED_SMM_VMXON) { |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6341 | vmx->nested.smm.vmxon = true; |
| 6342 | vmx->nested.vmxon = false; |
| 6343 | |
Liran Alon | 6ca00df | 2019-06-16 15:03:10 +0300 | [diff] [blame] | 6344 | 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] | 6345 | vmx->nested.smm.guest_mode = true; |
| 6346 | } |
| 6347 | |
| 6348 | vmcs12 = get_vmcs12(vcpu); |
Liran Alon | 6ca00df | 2019-06-16 15:03:10 +0300 | [diff] [blame] | 6349 | if (copy_from_user(vmcs12, user_vmx_nested_state->vmcs12, sizeof(*vmcs12))) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6350 | return -EFAULT; |
| 6351 | |
| 6352 | if (vmcs12->hdr.revision_id != VMCS12_REVISION) |
| 6353 | return -EINVAL; |
| 6354 | |
| 6355 | if (!(kvm_state->flags & KVM_STATE_NESTED_GUEST_MODE)) |
| 6356 | return 0; |
| 6357 | |
Sean Christopherson | 21be4ca | 2019-05-08 11:04:32 -0700 | [diff] [blame] | 6358 | vmx->nested.nested_run_pending = |
| 6359 | !!(kvm_state->flags & KVM_STATE_NESTED_RUN_PENDING); |
| 6360 | |
Oliver Upton | 5ef8acb | 2020-02-07 02:36:07 -0800 | [diff] [blame] | 6361 | vmx->nested.mtf_pending = |
| 6362 | !!(kvm_state->flags & KVM_STATE_NESTED_MTF_PENDING); |
| 6363 | |
Sean Christopherson | 21be4ca | 2019-05-08 11:04:32 -0700 | [diff] [blame] | 6364 | ret = -EINVAL; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6365 | if (nested_cpu_has_shadow_vmcs(vmcs12) && |
Yu Zhang | 64c7850 | 2021-09-30 01:51:53 +0800 | [diff] [blame] | 6366 | vmcs12->vmcs_link_pointer != INVALID_GPA) { |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6367 | struct vmcs12 *shadow_vmcs12 = get_shadow_vmcs12(vcpu); |
| 6368 | |
Liran Alon | 6ca00df | 2019-06-16 15:03:10 +0300 | [diff] [blame] | 6369 | if (kvm_state->size < |
| 6370 | sizeof(*kvm_state) + |
| 6371 | sizeof(user_vmx_nested_state->vmcs12) + sizeof(*shadow_vmcs12)) |
Sean Christopherson | 21be4ca | 2019-05-08 11:04:32 -0700 | [diff] [blame] | 6372 | goto error_guest_mode; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6373 | |
| 6374 | if (copy_from_user(shadow_vmcs12, |
Liran Alon | 6ca00df | 2019-06-16 15:03:10 +0300 | [diff] [blame] | 6375 | user_vmx_nested_state->shadow_vmcs12, |
| 6376 | sizeof(*shadow_vmcs12))) { |
Sean Christopherson | 21be4ca | 2019-05-08 11:04:32 -0700 | [diff] [blame] | 6377 | ret = -EFAULT; |
| 6378 | goto error_guest_mode; |
| 6379 | } |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6380 | |
| 6381 | if (shadow_vmcs12->hdr.revision_id != VMCS12_REVISION || |
| 6382 | !shadow_vmcs12->hdr.shadow_vmcs) |
Sean Christopherson | 21be4ca | 2019-05-08 11:04:32 -0700 | [diff] [blame] | 6383 | goto error_guest_mode; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6384 | } |
| 6385 | |
Paolo Bonzini | 83d31e5 | 2020-07-09 13:12:09 -0400 | [diff] [blame] | 6386 | vmx->nested.has_preemption_timer_deadline = false; |
Peter Shier | 850448f | 2020-05-26 14:51:06 -0700 | [diff] [blame] | 6387 | if (kvm_state->hdr.vmx.flags & KVM_STATE_VMX_PREEMPTION_TIMER_DEADLINE) { |
| 6388 | vmx->nested.has_preemption_timer_deadline = true; |
| 6389 | vmx->nested.preemption_timer_deadline = |
| 6390 | kvm_state->hdr.vmx.preemption_timer_deadline; |
| 6391 | } |
| 6392 | |
Sean Christopherson | 5478ba3 | 2019-04-11 12:18:06 -0700 | [diff] [blame] | 6393 | if (nested_vmx_check_controls(vcpu, vmcs12) || |
| 6394 | nested_vmx_check_host_state(vcpu, vmcs12) || |
Sean Christopherson | 68cda40 | 2020-05-11 15:05:29 -0700 | [diff] [blame] | 6395 | nested_vmx_check_guest_state(vcpu, vmcs12, &ignored)) |
Sean Christopherson | 21be4ca | 2019-05-08 11:04:32 -0700 | [diff] [blame] | 6396 | goto error_guest_mode; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6397 | |
| 6398 | vmx->nested.dirty_vmcs12 = true; |
| 6399 | ret = nested_vmx_enter_non_root_mode(vcpu, false); |
Sean Christopherson | 21be4ca | 2019-05-08 11:04:32 -0700 | [diff] [blame] | 6400 | if (ret) |
| 6401 | goto error_guest_mode; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6402 | |
| 6403 | return 0; |
Sean Christopherson | 21be4ca | 2019-05-08 11:04:32 -0700 | [diff] [blame] | 6404 | |
| 6405 | error_guest_mode: |
| 6406 | vmx->nested.nested_run_pending = 0; |
| 6407 | return ret; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6408 | } |
| 6409 | |
Xiaoyao Li | 1b84292 | 2019-10-20 17:11:01 +0800 | [diff] [blame] | 6410 | void nested_vmx_set_vmcs_shadowing_bitmap(void) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6411 | { |
| 6412 | if (enable_shadow_vmcs) { |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6413 | vmcs_write64(VMREAD_BITMAP, __pa(vmx_vmread_bitmap)); |
Sean Christopherson | fadcead | 2019-05-07 08:36:23 -0700 | [diff] [blame] | 6414 | vmcs_write64(VMWRITE_BITMAP, __pa(vmx_vmwrite_bitmap)); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6415 | } |
| 6416 | } |
| 6417 | |
| 6418 | /* |
Sean Christopherson | ba1f824 | 2021-06-18 14:46:58 -0700 | [diff] [blame] | 6419 | * Indexing into the vmcs12 uses the VMCS encoding rotated left by 6. Undo |
| 6420 | * that madness to get the encoding for comparison. |
| 6421 | */ |
| 6422 | #define VMCS12_IDX_TO_ENC(idx) ((u16)(((u16)(idx) >> 6) | ((u16)(idx) << 10))) |
| 6423 | |
| 6424 | static u64 nested_vmx_calc_vmcs_enum_msr(void) |
| 6425 | { |
| 6426 | /* |
| 6427 | * Note these are the so called "index" of the VMCS field encoding, not |
| 6428 | * the index into vmcs12. |
| 6429 | */ |
| 6430 | unsigned int max_idx, idx; |
| 6431 | int i; |
| 6432 | |
| 6433 | /* |
| 6434 | * For better or worse, KVM allows VMREAD/VMWRITE to all fields in |
| 6435 | * vmcs12, regardless of whether or not the associated feature is |
| 6436 | * exposed to L1. Simply find the field with the highest index. |
| 6437 | */ |
| 6438 | max_idx = 0; |
| 6439 | for (i = 0; i < nr_vmcs12_fields; i++) { |
| 6440 | /* The vmcs12 table is very, very sparsely populated. */ |
| 6441 | if (!vmcs_field_to_offset_table[i]) |
| 6442 | continue; |
| 6443 | |
| 6444 | idx = vmcs_field_index(VMCS12_IDX_TO_ENC(i)); |
| 6445 | if (idx > max_idx) |
| 6446 | max_idx = idx; |
| 6447 | } |
| 6448 | |
| 6449 | return (u64)max_idx << VMCS_FIELD_INDEX_SHIFT; |
| 6450 | } |
| 6451 | |
| 6452 | /* |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6453 | * nested_vmx_setup_ctls_msrs() sets up variables containing the values to be |
| 6454 | * returned for the various VMX controls MSRs when nested VMX is enabled. |
| 6455 | * The same values should also be used to verify that vmcs12 control fields are |
| 6456 | * valid during nested entry from L1 to L2. |
| 6457 | * Each of these control msrs has a low and high 32-bit half: A low bit is on |
| 6458 | * if the corresponding bit in the (32-bit) control field *must* be on, and a |
| 6459 | * bit in the high half is on if the corresponding bit in the control field |
| 6460 | * may be on. See also vmx_control_verify(). |
| 6461 | */ |
Vitaly Kuznetsov | a444326 | 2020-02-20 18:22:04 +0100 | [diff] [blame] | 6462 | 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] | 6463 | { |
| 6464 | /* |
| 6465 | * Note that as a general rule, the high half of the MSRs (bits in |
| 6466 | * the control fields which may be 1) should be initialized by the |
| 6467 | * intersection of the underlying hardware's MSR (i.e., features which |
| 6468 | * can be supported) and the list of features we want to expose - |
| 6469 | * because they are known to be properly supported in our code. |
| 6470 | * Also, usually, the low half of the MSRs (bits which must be 1) can |
| 6471 | * be set to 0, meaning that L1 may turn off any of these bits. The |
| 6472 | * reason is that if one of these bits is necessary, it will appear |
| 6473 | * in vmcs01 and prepare_vmcs02, when it bitwise-or's the control |
| 6474 | * fields of vmcs01 and vmcs02, will turn these bits off - and |
Sean Christopherson | 2c1f332 | 2020-04-15 10:55:14 -0700 | [diff] [blame] | 6475 | * nested_vmx_l1_wants_exit() will not pass related exits to L1. |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6476 | * These rules have exceptions below. |
| 6477 | */ |
| 6478 | |
| 6479 | /* pin-based controls */ |
| 6480 | rdmsr(MSR_IA32_VMX_PINBASED_CTLS, |
| 6481 | msrs->pinbased_ctls_low, |
| 6482 | msrs->pinbased_ctls_high); |
| 6483 | msrs->pinbased_ctls_low |= |
| 6484 | PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR; |
| 6485 | msrs->pinbased_ctls_high &= |
| 6486 | PIN_BASED_EXT_INTR_MASK | |
| 6487 | PIN_BASED_NMI_EXITING | |
| 6488 | PIN_BASED_VIRTUAL_NMIS | |
Vitaly Kuznetsov | a444326 | 2020-02-20 18:22:04 +0100 | [diff] [blame] | 6489 | (enable_apicv ? PIN_BASED_POSTED_INTR : 0); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6490 | msrs->pinbased_ctls_high |= |
| 6491 | PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR | |
| 6492 | PIN_BASED_VMX_PREEMPTION_TIMER; |
| 6493 | |
| 6494 | /* exit controls */ |
| 6495 | rdmsr(MSR_IA32_VMX_EXIT_CTLS, |
| 6496 | msrs->exit_ctls_low, |
| 6497 | msrs->exit_ctls_high); |
| 6498 | msrs->exit_ctls_low = |
| 6499 | VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR; |
| 6500 | |
| 6501 | msrs->exit_ctls_high &= |
| 6502 | #ifdef CONFIG_X86_64 |
| 6503 | VM_EXIT_HOST_ADDR_SPACE_SIZE | |
| 6504 | #endif |
Chenyi Qiang | efc8313 | 2020-08-28 16:56:18 +0800 | [diff] [blame] | 6505 | VM_EXIT_LOAD_IA32_PAT | VM_EXIT_SAVE_IA32_PAT | |
| 6506 | VM_EXIT_CLEAR_BNDCFGS | VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6507 | msrs->exit_ctls_high |= |
| 6508 | VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR | |
| 6509 | VM_EXIT_LOAD_IA32_EFER | VM_EXIT_SAVE_IA32_EFER | |
| 6510 | VM_EXIT_SAVE_VMX_PREEMPTION_TIMER | VM_EXIT_ACK_INTR_ON_EXIT; |
| 6511 | |
| 6512 | /* We support free control of debug control saving. */ |
| 6513 | msrs->exit_ctls_low &= ~VM_EXIT_SAVE_DEBUG_CONTROLS; |
| 6514 | |
| 6515 | /* entry controls */ |
| 6516 | rdmsr(MSR_IA32_VMX_ENTRY_CTLS, |
| 6517 | msrs->entry_ctls_low, |
| 6518 | msrs->entry_ctls_high); |
| 6519 | msrs->entry_ctls_low = |
| 6520 | VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR; |
| 6521 | msrs->entry_ctls_high &= |
| 6522 | #ifdef CONFIG_X86_64 |
| 6523 | VM_ENTRY_IA32E_MODE | |
| 6524 | #endif |
Chenyi Qiang | efc8313 | 2020-08-28 16:56:18 +0800 | [diff] [blame] | 6525 | VM_ENTRY_LOAD_IA32_PAT | VM_ENTRY_LOAD_BNDCFGS | |
| 6526 | VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6527 | msrs->entry_ctls_high |= |
| 6528 | (VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR | VM_ENTRY_LOAD_IA32_EFER); |
| 6529 | |
| 6530 | /* We support free control of debug control loading. */ |
| 6531 | msrs->entry_ctls_low &= ~VM_ENTRY_LOAD_DEBUG_CONTROLS; |
| 6532 | |
| 6533 | /* cpu-based controls */ |
| 6534 | rdmsr(MSR_IA32_VMX_PROCBASED_CTLS, |
| 6535 | msrs->procbased_ctls_low, |
| 6536 | msrs->procbased_ctls_high); |
| 6537 | msrs->procbased_ctls_low = |
| 6538 | CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR; |
| 6539 | msrs->procbased_ctls_high &= |
Xiaoyao Li | 9dadc2f | 2019-12-06 16:45:24 +0800 | [diff] [blame] | 6540 | CPU_BASED_INTR_WINDOW_EXITING | |
Xiaoyao Li | 5e3d394 | 2019-12-06 16:45:26 +0800 | [diff] [blame] | 6541 | CPU_BASED_NMI_WINDOW_EXITING | CPU_BASED_USE_TSC_OFFSETTING | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6542 | CPU_BASED_HLT_EXITING | CPU_BASED_INVLPG_EXITING | |
| 6543 | CPU_BASED_MWAIT_EXITING | CPU_BASED_CR3_LOAD_EXITING | |
| 6544 | CPU_BASED_CR3_STORE_EXITING | |
| 6545 | #ifdef CONFIG_X86_64 |
| 6546 | CPU_BASED_CR8_LOAD_EXITING | CPU_BASED_CR8_STORE_EXITING | |
| 6547 | #endif |
| 6548 | CPU_BASED_MOV_DR_EXITING | CPU_BASED_UNCOND_IO_EXITING | |
| 6549 | CPU_BASED_USE_IO_BITMAPS | CPU_BASED_MONITOR_TRAP_FLAG | |
| 6550 | CPU_BASED_MONITOR_EXITING | CPU_BASED_RDPMC_EXITING | |
| 6551 | CPU_BASED_RDTSC_EXITING | CPU_BASED_PAUSE_EXITING | |
| 6552 | CPU_BASED_TPR_SHADOW | CPU_BASED_ACTIVATE_SECONDARY_CONTROLS; |
| 6553 | /* |
| 6554 | * We can allow some features even when not supported by the |
| 6555 | * hardware. For example, L1 can specify an MSR bitmap - and we |
| 6556 | * can use it to avoid exits to L1 - even when L0 runs L2 |
| 6557 | * without MSR bitmaps. |
| 6558 | */ |
| 6559 | msrs->procbased_ctls_high |= |
| 6560 | CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR | |
| 6561 | CPU_BASED_USE_MSR_BITMAPS; |
| 6562 | |
| 6563 | /* We support free control of CR3 access interception. */ |
| 6564 | msrs->procbased_ctls_low &= |
| 6565 | ~(CPU_BASED_CR3_LOAD_EXITING | CPU_BASED_CR3_STORE_EXITING); |
| 6566 | |
| 6567 | /* |
| 6568 | * secondary cpu-based controls. Do not include those that |
Xiaoyao Li | 7c1b761 | 2020-07-09 12:34:25 +0800 | [diff] [blame] | 6569 | * depend on CPUID bits, they are added later by |
| 6570 | * vmx_vcpu_after_set_cpuid. |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6571 | */ |
Vitaly Kuznetsov | 6b1971c | 2019-02-07 11:42:14 +0100 | [diff] [blame] | 6572 | if (msrs->procbased_ctls_high & CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) |
| 6573 | rdmsr(MSR_IA32_VMX_PROCBASED_CTLS2, |
| 6574 | msrs->secondary_ctls_low, |
| 6575 | msrs->secondary_ctls_high); |
| 6576 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6577 | msrs->secondary_ctls_low = 0; |
| 6578 | msrs->secondary_ctls_high &= |
| 6579 | SECONDARY_EXEC_DESC | |
Sean Christopherson | 7f3603b | 2020-09-23 09:50:47 -0700 | [diff] [blame] | 6580 | SECONDARY_EXEC_ENABLE_RDTSCP | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6581 | SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE | |
Paolo Bonzini | 6defc59 | 2019-07-02 14:39:29 +0200 | [diff] [blame] | 6582 | SECONDARY_EXEC_WBINVD_EXITING | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6583 | SECONDARY_EXEC_APIC_REGISTER_VIRT | |
| 6584 | SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY | |
Paolo Bonzini | 6defc59 | 2019-07-02 14:39:29 +0200 | [diff] [blame] | 6585 | SECONDARY_EXEC_RDRAND_EXITING | |
| 6586 | SECONDARY_EXEC_ENABLE_INVPCID | |
| 6587 | SECONDARY_EXEC_RDSEED_EXITING | |
Ilias Stamatis | d041b5e | 2021-05-26 19:44:17 +0100 | [diff] [blame] | 6588 | SECONDARY_EXEC_XSAVES | |
| 6589 | SECONDARY_EXEC_TSC_SCALING; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6590 | |
| 6591 | /* |
| 6592 | * We can emulate "VMCS shadowing," even if the hardware |
| 6593 | * doesn't support it. |
| 6594 | */ |
| 6595 | msrs->secondary_ctls_high |= |
| 6596 | SECONDARY_EXEC_SHADOW_VMCS; |
| 6597 | |
| 6598 | if (enable_ept) { |
| 6599 | /* nested EPT: emulate EPT also to L1 */ |
| 6600 | msrs->secondary_ctls_high |= |
| 6601 | SECONDARY_EXEC_ENABLE_EPT; |
Sean Christopherson | bb1fcc7 | 2020-03-02 18:02:36 -0800 | [diff] [blame] | 6602 | msrs->ept_caps = |
| 6603 | VMX_EPT_PAGE_WALK_4_BIT | |
| 6604 | VMX_EPT_PAGE_WALK_5_BIT | |
| 6605 | VMX_EPTP_WB_BIT | |
Sean Christopherson | 96d4701 | 2020-03-02 18:02:40 -0800 | [diff] [blame] | 6606 | VMX_EPT_INVEPT_BIT | |
| 6607 | VMX_EPT_EXECUTE_ONLY_BIT; |
| 6608 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6609 | msrs->ept_caps &= ept_caps; |
| 6610 | msrs->ept_caps |= VMX_EPT_EXTENT_GLOBAL_BIT | |
| 6611 | VMX_EPT_EXTENT_CONTEXT_BIT | VMX_EPT_2MB_PAGE_BIT | |
| 6612 | VMX_EPT_1GB_PAGE_BIT; |
| 6613 | if (enable_ept_ad_bits) { |
| 6614 | msrs->secondary_ctls_high |= |
| 6615 | SECONDARY_EXEC_ENABLE_PML; |
| 6616 | msrs->ept_caps |= VMX_EPT_AD_BIT; |
| 6617 | } |
| 6618 | } |
| 6619 | |
| 6620 | if (cpu_has_vmx_vmfunc()) { |
| 6621 | msrs->secondary_ctls_high |= |
| 6622 | SECONDARY_EXEC_ENABLE_VMFUNC; |
| 6623 | /* |
| 6624 | * Advertise EPTP switching unconditionally |
| 6625 | * since we emulate it |
| 6626 | */ |
| 6627 | if (enable_ept) |
| 6628 | msrs->vmfunc_controls = |
| 6629 | VMX_VMFUNC_EPTP_SWITCHING; |
| 6630 | } |
| 6631 | |
| 6632 | /* |
| 6633 | * Old versions of KVM use the single-context version without |
| 6634 | * checking for support, so declare that it is supported even |
| 6635 | * though it is treated as global context. The alternative is |
| 6636 | * not failing the single-context invvpid, and it is worse. |
| 6637 | */ |
| 6638 | if (enable_vpid) { |
| 6639 | msrs->secondary_ctls_high |= |
| 6640 | SECONDARY_EXEC_ENABLE_VPID; |
| 6641 | msrs->vpid_caps = VMX_VPID_INVVPID_BIT | |
| 6642 | VMX_VPID_EXTENT_SUPPORTED_MASK; |
| 6643 | } |
| 6644 | |
| 6645 | if (enable_unrestricted_guest) |
| 6646 | msrs->secondary_ctls_high |= |
| 6647 | SECONDARY_EXEC_UNRESTRICTED_GUEST; |
| 6648 | |
| 6649 | if (flexpriority_enabled) |
| 6650 | msrs->secondary_ctls_high |= |
| 6651 | SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES; |
| 6652 | |
Sean Christopherson | 72add91 | 2021-04-12 16:21:42 +1200 | [diff] [blame] | 6653 | if (enable_sgx) |
| 6654 | msrs->secondary_ctls_high |= SECONDARY_EXEC_ENCLS_EXITING; |
| 6655 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6656 | /* miscellaneous data */ |
| 6657 | rdmsr(MSR_IA32_VMX_MISC, |
| 6658 | msrs->misc_low, |
| 6659 | msrs->misc_high); |
| 6660 | msrs->misc_low &= VMX_MISC_SAVE_EFER_LMA; |
| 6661 | msrs->misc_low |= |
| 6662 | MSR_IA32_VMX_MISC_VMWRITE_SHADOW_RO_FIELDS | |
| 6663 | VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE | |
Yadong Qi | bf0cd88 | 2020-11-06 14:51:22 +0800 | [diff] [blame] | 6664 | VMX_MISC_ACTIVITY_HLT | |
| 6665 | VMX_MISC_ACTIVITY_WAIT_SIPI; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6666 | msrs->misc_high = 0; |
| 6667 | |
| 6668 | /* |
| 6669 | * This MSR reports some information about VMX support. We |
| 6670 | * should return information about the VMX we emulate for the |
| 6671 | * guest, and the VMCS structure we give it - not about the |
| 6672 | * VMX support of the underlying hardware. |
| 6673 | */ |
| 6674 | msrs->basic = |
| 6675 | VMCS12_REVISION | |
| 6676 | VMX_BASIC_TRUE_CTLS | |
| 6677 | ((u64)VMCS12_SIZE << VMX_BASIC_VMCS_SIZE_SHIFT) | |
| 6678 | (VMX_BASIC_MEM_TYPE_WB << VMX_BASIC_MEM_TYPE_SHIFT); |
| 6679 | |
| 6680 | if (cpu_has_vmx_basic_inout()) |
| 6681 | msrs->basic |= VMX_BASIC_INOUT; |
| 6682 | |
| 6683 | /* |
| 6684 | * These MSRs specify bits which the guest must keep fixed on |
| 6685 | * while L1 is in VMXON mode (in L1's root mode, or running an L2). |
| 6686 | * We picked the standard core2 setting. |
| 6687 | */ |
| 6688 | #define VMXON_CR0_ALWAYSON (X86_CR0_PE | X86_CR0_PG | X86_CR0_NE) |
| 6689 | #define VMXON_CR4_ALWAYSON X86_CR4_VMXE |
| 6690 | msrs->cr0_fixed0 = VMXON_CR0_ALWAYSON; |
| 6691 | msrs->cr4_fixed0 = VMXON_CR4_ALWAYSON; |
| 6692 | |
| 6693 | /* These MSRs specify bits which the guest must keep fixed off. */ |
| 6694 | rdmsrl(MSR_IA32_VMX_CR0_FIXED1, msrs->cr0_fixed1); |
| 6695 | rdmsrl(MSR_IA32_VMX_CR4_FIXED1, msrs->cr4_fixed1); |
| 6696 | |
Sean Christopherson | ba1f824 | 2021-06-18 14:46:58 -0700 | [diff] [blame] | 6697 | msrs->vmcs_enum = nested_vmx_calc_vmcs_enum_msr(); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6698 | } |
| 6699 | |
| 6700 | void nested_vmx_hardware_unsetup(void) |
| 6701 | { |
| 6702 | int i; |
| 6703 | |
| 6704 | if (enable_shadow_vmcs) { |
| 6705 | for (i = 0; i < VMX_BITMAP_NR; i++) |
| 6706 | free_page((unsigned long)vmx_bitmap[i]); |
| 6707 | } |
| 6708 | } |
| 6709 | |
Sean Christopherson | 6c1c6e5 | 2020-05-06 13:46:53 -0700 | [diff] [blame] | 6710 | __init int nested_vmx_hardware_setup(int (*exit_handlers[])(struct kvm_vcpu *)) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6711 | { |
| 6712 | int i; |
| 6713 | |
| 6714 | if (!cpu_has_vmx_shadow_vmcs()) |
| 6715 | enable_shadow_vmcs = 0; |
| 6716 | if (enable_shadow_vmcs) { |
| 6717 | for (i = 0; i < VMX_BITMAP_NR; i++) { |
Ben Gardon | 4183683 | 2019-02-11 11:02:52 -0800 | [diff] [blame] | 6718 | /* |
| 6719 | * The vmx_bitmap is not tied to a VM and so should |
| 6720 | * not be charged to a memcg. |
| 6721 | */ |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6722 | vmx_bitmap[i] = (unsigned long *) |
| 6723 | __get_free_page(GFP_KERNEL); |
| 6724 | if (!vmx_bitmap[i]) { |
| 6725 | nested_vmx_hardware_unsetup(); |
| 6726 | return -ENOMEM; |
| 6727 | } |
| 6728 | } |
| 6729 | |
| 6730 | init_vmcs_shadow_fields(); |
| 6731 | } |
| 6732 | |
Liran Alon | cc87767 | 2019-11-18 21:11:21 +0200 | [diff] [blame] | 6733 | exit_handlers[EXIT_REASON_VMCLEAR] = handle_vmclear; |
| 6734 | exit_handlers[EXIT_REASON_VMLAUNCH] = handle_vmlaunch; |
| 6735 | exit_handlers[EXIT_REASON_VMPTRLD] = handle_vmptrld; |
| 6736 | exit_handlers[EXIT_REASON_VMPTRST] = handle_vmptrst; |
| 6737 | exit_handlers[EXIT_REASON_VMREAD] = handle_vmread; |
| 6738 | exit_handlers[EXIT_REASON_VMRESUME] = handle_vmresume; |
| 6739 | exit_handlers[EXIT_REASON_VMWRITE] = handle_vmwrite; |
| 6740 | exit_handlers[EXIT_REASON_VMOFF] = handle_vmoff; |
| 6741 | exit_handlers[EXIT_REASON_VMON] = handle_vmon; |
| 6742 | exit_handlers[EXIT_REASON_INVEPT] = handle_invept; |
| 6743 | exit_handlers[EXIT_REASON_INVVPID] = handle_invvpid; |
| 6744 | exit_handlers[EXIT_REASON_VMFUNC] = handle_vmfunc; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6745 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6746 | return 0; |
| 6747 | } |
Paolo Bonzini | 33b2217 | 2020-04-17 10:24:18 -0400 | [diff] [blame] | 6748 | |
| 6749 | struct kvm_x86_nested_ops vmx_nested_ops = { |
| 6750 | .check_events = vmx_check_nested_events, |
Sean Christopherson | d2060bd | 2020-04-22 19:25:39 -0700 | [diff] [blame] | 6751 | .hv_timer_pending = nested_vmx_preemption_timer_pending, |
Sean Christopherson | cb6a32c | 2021-03-02 09:45:14 -0800 | [diff] [blame] | 6752 | .triple_fault = nested_vmx_triple_fault, |
Paolo Bonzini | 33b2217 | 2020-04-17 10:24:18 -0400 | [diff] [blame] | 6753 | .get_state = vmx_get_nested_state, |
| 6754 | .set_state = vmx_set_nested_state, |
Paolo Bonzini | 9a78e15 | 2021-01-08 11:43:08 -0500 | [diff] [blame] | 6755 | .get_nested_state_pages = vmx_get_nested_state_pages, |
Sean Christopherson | 02f5fb2 | 2020-06-22 14:58:32 -0700 | [diff] [blame] | 6756 | .write_log_dirty = nested_vmx_write_pml_buffer, |
Paolo Bonzini | 33b2217 | 2020-04-17 10:24:18 -0400 | [diff] [blame] | 6757 | .enable_evmcs = nested_enable_evmcs, |
| 6758 | .get_evmcs_version = nested_get_evmcs_version, |
| 6759 | }; |