2 * Kernel-based Virtual Machine driver for Linux
4 * This module enables machines with Intel VT-x extensions to run virtual
5 * machines without emulation or binary translation.
7 * Copyright (C) 2006 Qumranet, Inc.
8 * Copyright 2010 Red Hat, Inc. and/or its affiliates.
11 * Avi Kivity <avi@qumranet.com>
12 * Yaniv Kamay <yaniv@qumranet.com>
14 * This work is licensed under the terms of the GNU GPL, version 2. See
15 * the COPYING file in the top-level directory.
24 #include <linux/kvm_host.h>
25 #include <linux/module.h>
26 #include <linux/kernel.h>
28 #include <linux/highmem.h>
29 #include <linux/sched.h>
30 #include <linux/moduleparam.h>
31 #include <linux/mod_devicetable.h>
32 #include <linux/trace_events.h>
33 #include <linux/slab.h>
34 #include <linux/tboot.h>
35 #include <linux/hrtimer.h>
36 #include <linux/frame.h>
37 #include <linux/nospec.h>
38 #include "kvm_cache_regs.h"
45 #include <asm/virtext.h>
47 #include <asm/fpu/internal.h>
48 #include <asm/perf_event.h>
49 #include <asm/debugreg.h>
50 #include <asm/kexec.h>
52 #include <asm/irq_remapping.h>
53 #include <asm/mmu_context.h>
54 #include <asm/spec-ctrl.h>
55 #include <asm/mshyperv.h>
59 #include "vmx_evmcs.h"
61 #define __ex(x) __kvm_handle_fault_on_reboot(x)
62 #define __ex_clear(x, reg) \
63 ____kvm_handle_fault_on_reboot(x, "xor " reg " , " reg)
65 MODULE_AUTHOR("Qumranet");
66 MODULE_LICENSE("GPL");
68 static const struct x86_cpu_id vmx_cpu_id[] = {
69 X86_FEATURE_MATCH(X86_FEATURE_VMX),
72 MODULE_DEVICE_TABLE(x86cpu, vmx_cpu_id);
74 static bool __read_mostly enable_vpid = 1;
75 module_param_named(vpid, enable_vpid, bool, 0444);
77 static bool __read_mostly enable_vnmi = 1;
78 module_param_named(vnmi, enable_vnmi, bool, S_IRUGO);
80 static bool __read_mostly flexpriority_enabled = 1;
81 module_param_named(flexpriority, flexpriority_enabled, bool, S_IRUGO);
83 static bool __read_mostly enable_ept = 1;
84 module_param_named(ept, enable_ept, bool, S_IRUGO);
86 static bool __read_mostly enable_unrestricted_guest = 1;
87 module_param_named(unrestricted_guest,
88 enable_unrestricted_guest, bool, S_IRUGO);
90 static bool __read_mostly enable_ept_ad_bits = 1;
91 module_param_named(eptad, enable_ept_ad_bits, bool, S_IRUGO);
93 static bool __read_mostly emulate_invalid_guest_state = true;
94 module_param(emulate_invalid_guest_state, bool, S_IRUGO);
96 static bool __read_mostly fasteoi = 1;
97 module_param(fasteoi, bool, S_IRUGO);
99 static bool __read_mostly enable_apicv = 1;
100 module_param(enable_apicv, bool, S_IRUGO);
102 static bool __read_mostly enable_shadow_vmcs = 1;
103 module_param_named(enable_shadow_vmcs, enable_shadow_vmcs, bool, S_IRUGO);
105 * If nested=1, nested virtualization is supported, i.e., guests may use
106 * VMX and be a hypervisor for its own guests. If nested=0, guests may not
107 * use VMX instructions.
109 static bool __read_mostly nested = 0;
110 module_param(nested, bool, S_IRUGO);
112 static u64 __read_mostly host_xss;
114 static bool __read_mostly enable_pml = 1;
115 module_param_named(pml, enable_pml, bool, S_IRUGO);
119 #define MSR_TYPE_RW 3
121 #define MSR_BITMAP_MODE_X2APIC 1
122 #define MSR_BITMAP_MODE_X2APIC_APICV 2
123 #define MSR_BITMAP_MODE_LM 4
125 #define KVM_VMX_TSC_MULTIPLIER_MAX 0xffffffffffffffffULL
127 /* Guest_tsc -> host_tsc conversion requires 64-bit division. */
128 static int __read_mostly cpu_preemption_timer_multi;
129 static bool __read_mostly enable_preemption_timer = 1;
131 module_param_named(preemption_timer, enable_preemption_timer, bool, S_IRUGO);
134 #define KVM_GUEST_CR0_MASK (X86_CR0_NW | X86_CR0_CD)
135 #define KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST X86_CR0_NE
136 #define KVM_VM_CR0_ALWAYS_ON \
137 (KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST | \
138 X86_CR0_WP | X86_CR0_PG | X86_CR0_PE)
139 #define KVM_CR4_GUEST_OWNED_BITS \
140 (X86_CR4_PVI | X86_CR4_DE | X86_CR4_PCE | X86_CR4_OSFXSR \
141 | X86_CR4_OSXMMEXCPT | X86_CR4_LA57 | X86_CR4_TSD)
143 #define KVM_VM_CR4_ALWAYS_ON_UNRESTRICTED_GUEST X86_CR4_VMXE
144 #define KVM_PMODE_VM_CR4_ALWAYS_ON (X86_CR4_PAE | X86_CR4_VMXE)
145 #define KVM_RMODE_VM_CR4_ALWAYS_ON (X86_CR4_VME | X86_CR4_PAE | X86_CR4_VMXE)
147 #define RMODE_GUEST_OWNED_EFLAGS_BITS (~(X86_EFLAGS_IOPL | X86_EFLAGS_VM))
149 #define VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE 5
152 * Hyper-V requires all of these, so mark them as supported even though
153 * they are just treated the same as all-context.
155 #define VMX_VPID_EXTENT_SUPPORTED_MASK \
156 (VMX_VPID_EXTENT_INDIVIDUAL_ADDR_BIT | \
157 VMX_VPID_EXTENT_SINGLE_CONTEXT_BIT | \
158 VMX_VPID_EXTENT_GLOBAL_CONTEXT_BIT | \
159 VMX_VPID_EXTENT_SINGLE_NON_GLOBAL_BIT)
162 * These 2 parameters are used to config the controls for Pause-Loop Exiting:
163 * ple_gap: upper bound on the amount of time between two successive
164 * executions of PAUSE in a loop. Also indicate if ple enabled.
165 * According to test, this time is usually smaller than 128 cycles.
166 * ple_window: upper bound on the amount of time a guest is allowed to execute
167 * in a PAUSE loop. Tests indicate that most spinlocks are held for
168 * less than 2^12 cycles
169 * Time is measured based on a counter that runs at the same rate as the TSC,
170 * refer SDM volume 3b section 21.6.13 & 22.1.3.
172 static unsigned int ple_gap = KVM_DEFAULT_PLE_GAP;
174 static unsigned int ple_window = KVM_VMX_DEFAULT_PLE_WINDOW;
175 module_param(ple_window, uint, 0444);
177 /* Default doubles per-vcpu window every exit. */
178 static unsigned int ple_window_grow = KVM_DEFAULT_PLE_WINDOW_GROW;
179 module_param(ple_window_grow, uint, 0444);
181 /* Default resets per-vcpu window every exit to ple_window. */
182 static unsigned int ple_window_shrink = KVM_DEFAULT_PLE_WINDOW_SHRINK;
183 module_param(ple_window_shrink, uint, 0444);
185 /* Default is to compute the maximum so we can never overflow. */
186 static unsigned int ple_window_max = KVM_VMX_DEFAULT_PLE_WINDOW_MAX;
187 module_param(ple_window_max, uint, 0444);
189 extern const ulong vmx_return;
194 unsigned int tss_addr;
195 bool ept_identity_pagetable_done;
196 gpa_t ept_identity_map_addr;
199 #define NR_AUTOLOAD_MSRS 8
208 * Track a VMCS that may be loaded on a certain CPU. If it is (cpu!=-1), also
209 * remember whether it was VMLAUNCHed, and maintain a linked list of all VMCSs
210 * loaded on this CPU (so we can clear them if the CPU goes down).
214 struct vmcs *shadow_vmcs;
217 bool nmi_known_unmasked;
218 unsigned long vmcs_host_cr3; /* May not match real cr3 */
219 unsigned long vmcs_host_cr4; /* May not match real cr4 */
220 /* Support for vnmi-less CPUs */
221 int soft_vnmi_blocked;
223 s64 vnmi_blocked_time;
224 unsigned long *msr_bitmap;
225 struct list_head loaded_vmcss_on_cpu_link;
228 struct shared_msr_entry {
235 * struct vmcs12 describes the state that our guest hypervisor (L1) keeps for a
236 * single nested guest (L2), hence the name vmcs12. Any VMX implementation has
237 * a VMCS structure, and vmcs12 is our emulated VMX's VMCS. This structure is
238 * stored in guest memory specified by VMPTRLD, but is opaque to the guest,
239 * which must access it using VMREAD/VMWRITE/VMCLEAR instructions.
240 * More than one of these structures may exist, if L1 runs multiple L2 guests.
241 * nested_vmx_run() will use the data here to build the vmcs02: a VMCS for the
242 * underlying hardware which will be used to run L2.
243 * This structure is packed to ensure that its layout is identical across
244 * machines (necessary for live migration).
245 * If there are changes in this struct, VMCS12_REVISION must be changed.
247 typedef u64 natural_width;
248 struct __packed vmcs12 {
249 /* According to the Intel spec, a VMCS region must start with the
250 * following two fields. Then follow implementation-specific data.
255 u32 launch_state; /* set to 0 by VMCLEAR, to 1 by VMLAUNCH */
256 u32 padding[7]; /* room for future expansion */
261 u64 vm_exit_msr_store_addr;
262 u64 vm_exit_msr_load_addr;
263 u64 vm_entry_msr_load_addr;
265 u64 virtual_apic_page_addr;
266 u64 apic_access_addr;
267 u64 posted_intr_desc_addr;
268 u64 vm_function_control;
270 u64 eoi_exit_bitmap0;
271 u64 eoi_exit_bitmap1;
272 u64 eoi_exit_bitmap2;
273 u64 eoi_exit_bitmap3;
274 u64 eptp_list_address;
276 u64 guest_physical_address;
277 u64 vmcs_link_pointer;
279 u64 guest_ia32_debugctl;
282 u64 guest_ia32_perf_global_ctrl;
290 u64 host_ia32_perf_global_ctrl;
291 u64 padding64[8]; /* room for future expansion */
293 * To allow migration of L1 (complete with its L2 guests) between
294 * machines of different natural widths (32 or 64 bit), we cannot have
295 * unsigned long fields with no explict size. We use u64 (aliased
296 * natural_width) instead. Luckily, x86 is little-endian.
298 natural_width cr0_guest_host_mask;
299 natural_width cr4_guest_host_mask;
300 natural_width cr0_read_shadow;
301 natural_width cr4_read_shadow;
302 natural_width cr3_target_value0;
303 natural_width cr3_target_value1;
304 natural_width cr3_target_value2;
305 natural_width cr3_target_value3;
306 natural_width exit_qualification;
307 natural_width guest_linear_address;
308 natural_width guest_cr0;
309 natural_width guest_cr3;
310 natural_width guest_cr4;
311 natural_width guest_es_base;
312 natural_width guest_cs_base;
313 natural_width guest_ss_base;
314 natural_width guest_ds_base;
315 natural_width guest_fs_base;
316 natural_width guest_gs_base;
317 natural_width guest_ldtr_base;
318 natural_width guest_tr_base;
319 natural_width guest_gdtr_base;
320 natural_width guest_idtr_base;
321 natural_width guest_dr7;
322 natural_width guest_rsp;
323 natural_width guest_rip;
324 natural_width guest_rflags;
325 natural_width guest_pending_dbg_exceptions;
326 natural_width guest_sysenter_esp;
327 natural_width guest_sysenter_eip;
328 natural_width host_cr0;
329 natural_width host_cr3;
330 natural_width host_cr4;
331 natural_width host_fs_base;
332 natural_width host_gs_base;
333 natural_width host_tr_base;
334 natural_width host_gdtr_base;
335 natural_width host_idtr_base;
336 natural_width host_ia32_sysenter_esp;
337 natural_width host_ia32_sysenter_eip;
338 natural_width host_rsp;
339 natural_width host_rip;
340 natural_width paddingl[8]; /* room for future expansion */
341 u32 pin_based_vm_exec_control;
342 u32 cpu_based_vm_exec_control;
343 u32 exception_bitmap;
344 u32 page_fault_error_code_mask;
345 u32 page_fault_error_code_match;
346 u32 cr3_target_count;
347 u32 vm_exit_controls;
348 u32 vm_exit_msr_store_count;
349 u32 vm_exit_msr_load_count;
350 u32 vm_entry_controls;
351 u32 vm_entry_msr_load_count;
352 u32 vm_entry_intr_info_field;
353 u32 vm_entry_exception_error_code;
354 u32 vm_entry_instruction_len;
356 u32 secondary_vm_exec_control;
357 u32 vm_instruction_error;
359 u32 vm_exit_intr_info;
360 u32 vm_exit_intr_error_code;
361 u32 idt_vectoring_info_field;
362 u32 idt_vectoring_error_code;
363 u32 vm_exit_instruction_len;
364 u32 vmx_instruction_info;
371 u32 guest_ldtr_limit;
373 u32 guest_gdtr_limit;
374 u32 guest_idtr_limit;
375 u32 guest_es_ar_bytes;
376 u32 guest_cs_ar_bytes;
377 u32 guest_ss_ar_bytes;
378 u32 guest_ds_ar_bytes;
379 u32 guest_fs_ar_bytes;
380 u32 guest_gs_ar_bytes;
381 u32 guest_ldtr_ar_bytes;
382 u32 guest_tr_ar_bytes;
383 u32 guest_interruptibility_info;
384 u32 guest_activity_state;
385 u32 guest_sysenter_cs;
386 u32 host_ia32_sysenter_cs;
387 u32 vmx_preemption_timer_value;
388 u32 padding32[7]; /* room for future expansion */
389 u16 virtual_processor_id;
391 u16 guest_es_selector;
392 u16 guest_cs_selector;
393 u16 guest_ss_selector;
394 u16 guest_ds_selector;
395 u16 guest_fs_selector;
396 u16 guest_gs_selector;
397 u16 guest_ldtr_selector;
398 u16 guest_tr_selector;
399 u16 guest_intr_status;
401 u16 host_es_selector;
402 u16 host_cs_selector;
403 u16 host_ss_selector;
404 u16 host_ds_selector;
405 u16 host_fs_selector;
406 u16 host_gs_selector;
407 u16 host_tr_selector;
411 * VMCS12_REVISION is an arbitrary id that should be changed if the content or
412 * layout of struct vmcs12 is changed. MSR_IA32_VMX_BASIC returns this id, and
413 * VMPTRLD verifies that the VMCS region that L1 is loading contains this id.
415 #define VMCS12_REVISION 0x11e57ed0
418 * VMCS12_SIZE is the number of bytes L1 should allocate for the VMXON region
419 * and any VMCS region. Although only sizeof(struct vmcs12) are used by the
420 * current implementation, 4K are reserved to avoid future complications.
422 #define VMCS12_SIZE 0x1000
425 * VMCS12_MAX_FIELD_INDEX is the highest index value used in any
426 * supported VMCS12 field encoding.
428 #define VMCS12_MAX_FIELD_INDEX 0x17
430 struct nested_vmx_msrs {
432 * We only store the "true" versions of the VMX capability MSRs. We
433 * generate the "non-true" versions by setting the must-be-1 bits
434 * according to the SDM.
436 u32 procbased_ctls_low;
437 u32 procbased_ctls_high;
438 u32 secondary_ctls_low;
439 u32 secondary_ctls_high;
440 u32 pinbased_ctls_low;
441 u32 pinbased_ctls_high;
460 * The nested_vmx structure is part of vcpu_vmx, and holds information we need
461 * for correct emulation of VMX (i.e., nested VMX) on this vcpu.
464 /* Has the level1 guest done vmxon? */
469 /* The guest-physical address of the current VMCS L1 keeps for L2 */
472 * Cache of the guest's VMCS, existing outside of guest memory.
473 * Loaded from guest memory during VMPTRLD. Flushed to guest
474 * memory during VMCLEAR and VMPTRLD.
476 struct vmcs12 *cached_vmcs12;
478 * Indicates if the shadow vmcs must be updated with the
479 * data hold by vmcs12
481 bool sync_shadow_vmcs;
484 bool change_vmcs01_virtual_x2apic_mode;
485 /* L2 must run next, and mustn't decide to exit to L1. */
486 bool nested_run_pending;
488 struct loaded_vmcs vmcs02;
491 * Guest pages referred to in the vmcs02 with host-physical
492 * pointers, so we must keep them pinned while L2 runs.
494 struct page *apic_access_page;
495 struct page *virtual_apic_page;
496 struct page *pi_desc_page;
497 struct pi_desc *pi_desc;
501 struct hrtimer preemption_timer;
502 bool preemption_timer_expired;
504 /* to migrate it to L2 if VM_ENTRY_LOAD_DEBUG_CONTROLS is off */
510 struct nested_vmx_msrs msrs;
512 /* SMM related state */
514 /* in VMX operation on SMM entry? */
516 /* in guest mode on SMM entry? */
521 #define POSTED_INTR_ON 0
522 #define POSTED_INTR_SN 1
524 /* Posted-Interrupt Descriptor */
526 u32 pir[8]; /* Posted interrupt requested */
529 /* bit 256 - Outstanding Notification */
531 /* bit 257 - Suppress Notification */
533 /* bit 271:258 - Reserved */
535 /* bit 279:272 - Notification Vector */
537 /* bit 287:280 - Reserved */
539 /* bit 319:288 - Notification Destination */
547 static bool pi_test_and_set_on(struct pi_desc *pi_desc)
549 return test_and_set_bit(POSTED_INTR_ON,
550 (unsigned long *)&pi_desc->control);
553 static bool pi_test_and_clear_on(struct pi_desc *pi_desc)
555 return test_and_clear_bit(POSTED_INTR_ON,
556 (unsigned long *)&pi_desc->control);
559 static int pi_test_and_set_pir(int vector, struct pi_desc *pi_desc)
561 return test_and_set_bit(vector, (unsigned long *)pi_desc->pir);
564 static inline void pi_clear_sn(struct pi_desc *pi_desc)
566 return clear_bit(POSTED_INTR_SN,
567 (unsigned long *)&pi_desc->control);
570 static inline void pi_set_sn(struct pi_desc *pi_desc)
572 return set_bit(POSTED_INTR_SN,
573 (unsigned long *)&pi_desc->control);
576 static inline void pi_clear_on(struct pi_desc *pi_desc)
578 clear_bit(POSTED_INTR_ON,
579 (unsigned long *)&pi_desc->control);
582 static inline int pi_test_on(struct pi_desc *pi_desc)
584 return test_bit(POSTED_INTR_ON,
585 (unsigned long *)&pi_desc->control);
588 static inline int pi_test_sn(struct pi_desc *pi_desc)
590 return test_bit(POSTED_INTR_SN,
591 (unsigned long *)&pi_desc->control);
595 struct kvm_vcpu vcpu;
596 unsigned long host_rsp;
600 u32 idt_vectoring_info;
602 struct shared_msr_entry *guest_msrs;
605 unsigned long host_idt_base;
607 u64 msr_host_kernel_gs_base;
608 u64 msr_guest_kernel_gs_base;
611 u64 arch_capabilities;
614 u32 vm_entry_controls_shadow;
615 u32 vm_exit_controls_shadow;
616 u32 secondary_exec_control;
619 * loaded_vmcs points to the VMCS currently used in this vcpu. For a
620 * non-nested (L1) guest, it always points to vmcs01. For a nested
621 * guest (L2), it points to a different VMCS.
623 struct loaded_vmcs vmcs01;
624 struct loaded_vmcs *loaded_vmcs;
625 bool __launched; /* temporary, used in vmx_vcpu_run */
626 struct msr_autoload {
628 struct vmx_msr_entry guest[NR_AUTOLOAD_MSRS];
629 struct vmx_msr_entry host[NR_AUTOLOAD_MSRS];
633 u16 fs_sel, gs_sel, ldt_sel;
637 int gs_ldt_reload_needed;
638 int fs_reload_needed;
639 u64 msr_host_bndcfgs;
644 struct kvm_segment segs[8];
647 u32 bitmask; /* 4 bits per segment (1 bit per field) */
648 struct kvm_save_segment {
656 bool emulation_required;
660 /* Posted interrupt descriptor */
661 struct pi_desc pi_desc;
663 /* Support for a guest hypervisor (nested VMX) */
664 struct nested_vmx nested;
666 /* Dynamic PLE window. */
668 bool ple_window_dirty;
670 /* Support for PML */
671 #define PML_ENTITY_NUM 512
674 /* apic deadline value in host tsc */
677 u64 current_tsc_ratio;
681 unsigned long host_debugctlmsr;
684 * Only bits masked by msr_ia32_feature_control_valid_bits can be set in
685 * msr_ia32_feature_control. FEATURE_CONTROL_LOCKED is always included
686 * in msr_ia32_feature_control_valid_bits.
688 u64 msr_ia32_feature_control;
689 u64 msr_ia32_feature_control_valid_bits;
692 enum segment_cache_field {
701 static inline struct kvm_vmx *to_kvm_vmx(struct kvm *kvm)
703 return container_of(kvm, struct kvm_vmx, kvm);
706 static inline struct vcpu_vmx *to_vmx(struct kvm_vcpu *vcpu)
708 return container_of(vcpu, struct vcpu_vmx, vcpu);
711 static struct pi_desc *vcpu_to_pi_desc(struct kvm_vcpu *vcpu)
713 return &(to_vmx(vcpu)->pi_desc);
716 #define ROL16(val, n) ((u16)(((u16)(val) << (n)) | ((u16)(val) >> (16 - (n)))))
717 #define VMCS12_OFFSET(x) offsetof(struct vmcs12, x)
718 #define FIELD(number, name) [ROL16(number, 6)] = VMCS12_OFFSET(name)
719 #define FIELD64(number, name) \
720 FIELD(number, name), \
721 [ROL16(number##_HIGH, 6)] = VMCS12_OFFSET(name) + sizeof(u32)
724 static u16 shadow_read_only_fields[] = {
725 #define SHADOW_FIELD_RO(x) x,
726 #include "vmx_shadow_fields.h"
728 static int max_shadow_read_only_fields =
729 ARRAY_SIZE(shadow_read_only_fields);
731 static u16 shadow_read_write_fields[] = {
732 #define SHADOW_FIELD_RW(x) x,
733 #include "vmx_shadow_fields.h"
735 static int max_shadow_read_write_fields =
736 ARRAY_SIZE(shadow_read_write_fields);
738 static const unsigned short vmcs_field_to_offset_table[] = {
739 FIELD(VIRTUAL_PROCESSOR_ID, virtual_processor_id),
740 FIELD(POSTED_INTR_NV, posted_intr_nv),
741 FIELD(GUEST_ES_SELECTOR, guest_es_selector),
742 FIELD(GUEST_CS_SELECTOR, guest_cs_selector),
743 FIELD(GUEST_SS_SELECTOR, guest_ss_selector),
744 FIELD(GUEST_DS_SELECTOR, guest_ds_selector),
745 FIELD(GUEST_FS_SELECTOR, guest_fs_selector),
746 FIELD(GUEST_GS_SELECTOR, guest_gs_selector),
747 FIELD(GUEST_LDTR_SELECTOR, guest_ldtr_selector),
748 FIELD(GUEST_TR_SELECTOR, guest_tr_selector),
749 FIELD(GUEST_INTR_STATUS, guest_intr_status),
750 FIELD(GUEST_PML_INDEX, guest_pml_index),
751 FIELD(HOST_ES_SELECTOR, host_es_selector),
752 FIELD(HOST_CS_SELECTOR, host_cs_selector),
753 FIELD(HOST_SS_SELECTOR, host_ss_selector),
754 FIELD(HOST_DS_SELECTOR, host_ds_selector),
755 FIELD(HOST_FS_SELECTOR, host_fs_selector),
756 FIELD(HOST_GS_SELECTOR, host_gs_selector),
757 FIELD(HOST_TR_SELECTOR, host_tr_selector),
758 FIELD64(IO_BITMAP_A, io_bitmap_a),
759 FIELD64(IO_BITMAP_B, io_bitmap_b),
760 FIELD64(MSR_BITMAP, msr_bitmap),
761 FIELD64(VM_EXIT_MSR_STORE_ADDR, vm_exit_msr_store_addr),
762 FIELD64(VM_EXIT_MSR_LOAD_ADDR, vm_exit_msr_load_addr),
763 FIELD64(VM_ENTRY_MSR_LOAD_ADDR, vm_entry_msr_load_addr),
764 FIELD64(TSC_OFFSET, tsc_offset),
765 FIELD64(VIRTUAL_APIC_PAGE_ADDR, virtual_apic_page_addr),
766 FIELD64(APIC_ACCESS_ADDR, apic_access_addr),
767 FIELD64(POSTED_INTR_DESC_ADDR, posted_intr_desc_addr),
768 FIELD64(VM_FUNCTION_CONTROL, vm_function_control),
769 FIELD64(EPT_POINTER, ept_pointer),
770 FIELD64(EOI_EXIT_BITMAP0, eoi_exit_bitmap0),
771 FIELD64(EOI_EXIT_BITMAP1, eoi_exit_bitmap1),
772 FIELD64(EOI_EXIT_BITMAP2, eoi_exit_bitmap2),
773 FIELD64(EOI_EXIT_BITMAP3, eoi_exit_bitmap3),
774 FIELD64(EPTP_LIST_ADDRESS, eptp_list_address),
775 FIELD64(XSS_EXIT_BITMAP, xss_exit_bitmap),
776 FIELD64(GUEST_PHYSICAL_ADDRESS, guest_physical_address),
777 FIELD64(VMCS_LINK_POINTER, vmcs_link_pointer),
778 FIELD64(PML_ADDRESS, pml_address),
779 FIELD64(GUEST_IA32_DEBUGCTL, guest_ia32_debugctl),
780 FIELD64(GUEST_IA32_PAT, guest_ia32_pat),
781 FIELD64(GUEST_IA32_EFER, guest_ia32_efer),
782 FIELD64(GUEST_IA32_PERF_GLOBAL_CTRL, guest_ia32_perf_global_ctrl),
783 FIELD64(GUEST_PDPTR0, guest_pdptr0),
784 FIELD64(GUEST_PDPTR1, guest_pdptr1),
785 FIELD64(GUEST_PDPTR2, guest_pdptr2),
786 FIELD64(GUEST_PDPTR3, guest_pdptr3),
787 FIELD64(GUEST_BNDCFGS, guest_bndcfgs),
788 FIELD64(HOST_IA32_PAT, host_ia32_pat),
789 FIELD64(HOST_IA32_EFER, host_ia32_efer),
790 FIELD64(HOST_IA32_PERF_GLOBAL_CTRL, host_ia32_perf_global_ctrl),
791 FIELD(PIN_BASED_VM_EXEC_CONTROL, pin_based_vm_exec_control),
792 FIELD(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control),
793 FIELD(EXCEPTION_BITMAP, exception_bitmap),
794 FIELD(PAGE_FAULT_ERROR_CODE_MASK, page_fault_error_code_mask),
795 FIELD(PAGE_FAULT_ERROR_CODE_MATCH, page_fault_error_code_match),
796 FIELD(CR3_TARGET_COUNT, cr3_target_count),
797 FIELD(VM_EXIT_CONTROLS, vm_exit_controls),
798 FIELD(VM_EXIT_MSR_STORE_COUNT, vm_exit_msr_store_count),
799 FIELD(VM_EXIT_MSR_LOAD_COUNT, vm_exit_msr_load_count),
800 FIELD(VM_ENTRY_CONTROLS, vm_entry_controls),
801 FIELD(VM_ENTRY_MSR_LOAD_COUNT, vm_entry_msr_load_count),
802 FIELD(VM_ENTRY_INTR_INFO_FIELD, vm_entry_intr_info_field),
803 FIELD(VM_ENTRY_EXCEPTION_ERROR_CODE, vm_entry_exception_error_code),
804 FIELD(VM_ENTRY_INSTRUCTION_LEN, vm_entry_instruction_len),
805 FIELD(TPR_THRESHOLD, tpr_threshold),
806 FIELD(SECONDARY_VM_EXEC_CONTROL, secondary_vm_exec_control),
807 FIELD(VM_INSTRUCTION_ERROR, vm_instruction_error),
808 FIELD(VM_EXIT_REASON, vm_exit_reason),
809 FIELD(VM_EXIT_INTR_INFO, vm_exit_intr_info),
810 FIELD(VM_EXIT_INTR_ERROR_CODE, vm_exit_intr_error_code),
811 FIELD(IDT_VECTORING_INFO_FIELD, idt_vectoring_info_field),
812 FIELD(IDT_VECTORING_ERROR_CODE, idt_vectoring_error_code),
813 FIELD(VM_EXIT_INSTRUCTION_LEN, vm_exit_instruction_len),
814 FIELD(VMX_INSTRUCTION_INFO, vmx_instruction_info),
815 FIELD(GUEST_ES_LIMIT, guest_es_limit),
816 FIELD(GUEST_CS_LIMIT, guest_cs_limit),
817 FIELD(GUEST_SS_LIMIT, guest_ss_limit),
818 FIELD(GUEST_DS_LIMIT, guest_ds_limit),
819 FIELD(GUEST_FS_LIMIT, guest_fs_limit),
820 FIELD(GUEST_GS_LIMIT, guest_gs_limit),
821 FIELD(GUEST_LDTR_LIMIT, guest_ldtr_limit),
822 FIELD(GUEST_TR_LIMIT, guest_tr_limit),
823 FIELD(GUEST_GDTR_LIMIT, guest_gdtr_limit),
824 FIELD(GUEST_IDTR_LIMIT, guest_idtr_limit),
825 FIELD(GUEST_ES_AR_BYTES, guest_es_ar_bytes),
826 FIELD(GUEST_CS_AR_BYTES, guest_cs_ar_bytes),
827 FIELD(GUEST_SS_AR_BYTES, guest_ss_ar_bytes),
828 FIELD(GUEST_DS_AR_BYTES, guest_ds_ar_bytes),
829 FIELD(GUEST_FS_AR_BYTES, guest_fs_ar_bytes),
830 FIELD(GUEST_GS_AR_BYTES, guest_gs_ar_bytes),
831 FIELD(GUEST_LDTR_AR_BYTES, guest_ldtr_ar_bytes),
832 FIELD(GUEST_TR_AR_BYTES, guest_tr_ar_bytes),
833 FIELD(GUEST_INTERRUPTIBILITY_INFO, guest_interruptibility_info),
834 FIELD(GUEST_ACTIVITY_STATE, guest_activity_state),
835 FIELD(GUEST_SYSENTER_CS, guest_sysenter_cs),
836 FIELD(HOST_IA32_SYSENTER_CS, host_ia32_sysenter_cs),
837 FIELD(VMX_PREEMPTION_TIMER_VALUE, vmx_preemption_timer_value),
838 FIELD(CR0_GUEST_HOST_MASK, cr0_guest_host_mask),
839 FIELD(CR4_GUEST_HOST_MASK, cr4_guest_host_mask),
840 FIELD(CR0_READ_SHADOW, cr0_read_shadow),
841 FIELD(CR4_READ_SHADOW, cr4_read_shadow),
842 FIELD(CR3_TARGET_VALUE0, cr3_target_value0),
843 FIELD(CR3_TARGET_VALUE1, cr3_target_value1),
844 FIELD(CR3_TARGET_VALUE2, cr3_target_value2),
845 FIELD(CR3_TARGET_VALUE3, cr3_target_value3),
846 FIELD(EXIT_QUALIFICATION, exit_qualification),
847 FIELD(GUEST_LINEAR_ADDRESS, guest_linear_address),
848 FIELD(GUEST_CR0, guest_cr0),
849 FIELD(GUEST_CR3, guest_cr3),
850 FIELD(GUEST_CR4, guest_cr4),
851 FIELD(GUEST_ES_BASE, guest_es_base),
852 FIELD(GUEST_CS_BASE, guest_cs_base),
853 FIELD(GUEST_SS_BASE, guest_ss_base),
854 FIELD(GUEST_DS_BASE, guest_ds_base),
855 FIELD(GUEST_FS_BASE, guest_fs_base),
856 FIELD(GUEST_GS_BASE, guest_gs_base),
857 FIELD(GUEST_LDTR_BASE, guest_ldtr_base),
858 FIELD(GUEST_TR_BASE, guest_tr_base),
859 FIELD(GUEST_GDTR_BASE, guest_gdtr_base),
860 FIELD(GUEST_IDTR_BASE, guest_idtr_base),
861 FIELD(GUEST_DR7, guest_dr7),
862 FIELD(GUEST_RSP, guest_rsp),
863 FIELD(GUEST_RIP, guest_rip),
864 FIELD(GUEST_RFLAGS, guest_rflags),
865 FIELD(GUEST_PENDING_DBG_EXCEPTIONS, guest_pending_dbg_exceptions),
866 FIELD(GUEST_SYSENTER_ESP, guest_sysenter_esp),
867 FIELD(GUEST_SYSENTER_EIP, guest_sysenter_eip),
868 FIELD(HOST_CR0, host_cr0),
869 FIELD(HOST_CR3, host_cr3),
870 FIELD(HOST_CR4, host_cr4),
871 FIELD(HOST_FS_BASE, host_fs_base),
872 FIELD(HOST_GS_BASE, host_gs_base),
873 FIELD(HOST_TR_BASE, host_tr_base),
874 FIELD(HOST_GDTR_BASE, host_gdtr_base),
875 FIELD(HOST_IDTR_BASE, host_idtr_base),
876 FIELD(HOST_IA32_SYSENTER_ESP, host_ia32_sysenter_esp),
877 FIELD(HOST_IA32_SYSENTER_EIP, host_ia32_sysenter_eip),
878 FIELD(HOST_RSP, host_rsp),
879 FIELD(HOST_RIP, host_rip),
882 static inline short vmcs_field_to_offset(unsigned long field)
884 const size_t size = ARRAY_SIZE(vmcs_field_to_offset_table);
885 unsigned short offset;
891 index = ROL16(field, 6);
895 index = array_index_nospec(index, size);
896 offset = vmcs_field_to_offset_table[index];
902 static inline struct vmcs12 *get_vmcs12(struct kvm_vcpu *vcpu)
904 return to_vmx(vcpu)->nested.cached_vmcs12;
907 static bool nested_ept_ad_enabled(struct kvm_vcpu *vcpu);
908 static unsigned long nested_ept_get_cr3(struct kvm_vcpu *vcpu);
909 static u64 construct_eptp(struct kvm_vcpu *vcpu, unsigned long root_hpa);
910 static bool vmx_xsaves_supported(void);
911 static void vmx_set_segment(struct kvm_vcpu *vcpu,
912 struct kvm_segment *var, int seg);
913 static void vmx_get_segment(struct kvm_vcpu *vcpu,
914 struct kvm_segment *var, int seg);
915 static bool guest_state_valid(struct kvm_vcpu *vcpu);
916 static u32 vmx_segment_access_rights(struct kvm_segment *var);
917 static void copy_shadow_to_vmcs12(struct vcpu_vmx *vmx);
918 static bool vmx_get_nmi_mask(struct kvm_vcpu *vcpu);
919 static void vmx_set_nmi_mask(struct kvm_vcpu *vcpu, bool masked);
920 static bool nested_vmx_is_page_fault_vmexit(struct vmcs12 *vmcs12,
922 static void vmx_update_msr_bitmap(struct kvm_vcpu *vcpu);
923 static void __always_inline vmx_disable_intercept_for_msr(unsigned long *msr_bitmap,
926 static DEFINE_PER_CPU(struct vmcs *, vmxarea);
927 static DEFINE_PER_CPU(struct vmcs *, current_vmcs);
929 * We maintain a per-CPU linked-list of VMCS loaded on that CPU. This is needed
930 * when a CPU is brought down, and we need to VMCLEAR all VMCSs loaded on it.
932 static DEFINE_PER_CPU(struct list_head, loaded_vmcss_on_cpu);
935 * We maintian a per-CPU linked-list of vCPU, so in wakeup_handler() we
936 * can find which vCPU should be waken up.
938 static DEFINE_PER_CPU(struct list_head, blocked_vcpu_on_cpu);
939 static DEFINE_PER_CPU(spinlock_t, blocked_vcpu_on_cpu_lock);
947 static unsigned long *vmx_bitmap[VMX_BITMAP_NR];
949 #define vmx_vmread_bitmap (vmx_bitmap[VMX_VMREAD_BITMAP])
950 #define vmx_vmwrite_bitmap (vmx_bitmap[VMX_VMWRITE_BITMAP])
952 static bool cpu_has_load_ia32_efer;
953 static bool cpu_has_load_perf_global_ctrl;
955 static DECLARE_BITMAP(vmx_vpid_bitmap, VMX_NR_VPIDS);
956 static DEFINE_SPINLOCK(vmx_vpid_lock);
958 static struct vmcs_config {
963 u32 pin_based_exec_ctrl;
964 u32 cpu_based_exec_ctrl;
965 u32 cpu_based_2nd_exec_ctrl;
968 struct nested_vmx_msrs nested;
971 static struct vmx_capability {
976 #define VMX_SEGMENT_FIELD(seg) \
977 [VCPU_SREG_##seg] = { \
978 .selector = GUEST_##seg##_SELECTOR, \
979 .base = GUEST_##seg##_BASE, \
980 .limit = GUEST_##seg##_LIMIT, \
981 .ar_bytes = GUEST_##seg##_AR_BYTES, \
984 static const struct kvm_vmx_segment_field {
989 } kvm_vmx_segment_fields[] = {
990 VMX_SEGMENT_FIELD(CS),
991 VMX_SEGMENT_FIELD(DS),
992 VMX_SEGMENT_FIELD(ES),
993 VMX_SEGMENT_FIELD(FS),
994 VMX_SEGMENT_FIELD(GS),
995 VMX_SEGMENT_FIELD(SS),
996 VMX_SEGMENT_FIELD(TR),
997 VMX_SEGMENT_FIELD(LDTR),
1000 static u64 host_efer;
1002 static void ept_save_pdptrs(struct kvm_vcpu *vcpu);
1005 * Keep MSR_STAR at the end, as setup_msrs() will try to optimize it
1006 * away by decrementing the array size.
1008 static const u32 vmx_msr_index[] = {
1009 #ifdef CONFIG_X86_64
1010 MSR_SYSCALL_MASK, MSR_LSTAR, MSR_CSTAR,
1012 MSR_EFER, MSR_TSC_AUX, MSR_STAR,
1015 DEFINE_STATIC_KEY_FALSE(enable_evmcs);
1017 #define current_evmcs ((struct hv_enlightened_vmcs *)this_cpu_read(current_vmcs))
1019 #define KVM_EVMCS_VERSION 1
1021 #if IS_ENABLED(CONFIG_HYPERV)
1022 static bool __read_mostly enlightened_vmcs = true;
1023 module_param(enlightened_vmcs, bool, 0444);
1025 static inline void evmcs_write64(unsigned long field, u64 value)
1028 int offset = get_evmcs_offset(field, &clean_field);
1033 *(u64 *)((char *)current_evmcs + offset) = value;
1035 current_evmcs->hv_clean_fields &= ~clean_field;
1038 static inline void evmcs_write32(unsigned long field, u32 value)
1041 int offset = get_evmcs_offset(field, &clean_field);
1046 *(u32 *)((char *)current_evmcs + offset) = value;
1047 current_evmcs->hv_clean_fields &= ~clean_field;
1050 static inline void evmcs_write16(unsigned long field, u16 value)
1053 int offset = get_evmcs_offset(field, &clean_field);
1058 *(u16 *)((char *)current_evmcs + offset) = value;
1059 current_evmcs->hv_clean_fields &= ~clean_field;
1062 static inline u64 evmcs_read64(unsigned long field)
1064 int offset = get_evmcs_offset(field, NULL);
1069 return *(u64 *)((char *)current_evmcs + offset);
1072 static inline u32 evmcs_read32(unsigned long field)
1074 int offset = get_evmcs_offset(field, NULL);
1079 return *(u32 *)((char *)current_evmcs + offset);
1082 static inline u16 evmcs_read16(unsigned long field)
1084 int offset = get_evmcs_offset(field, NULL);
1089 return *(u16 *)((char *)current_evmcs + offset);
1092 static void evmcs_load(u64 phys_addr)
1094 struct hv_vp_assist_page *vp_ap =
1095 hv_get_vp_assist_page(smp_processor_id());
1097 vp_ap->current_nested_vmcs = phys_addr;
1098 vp_ap->enlighten_vmentry = 1;
1101 static void evmcs_sanitize_exec_ctrls(struct vmcs_config *vmcs_conf)
1104 * Enlightened VMCSv1 doesn't support these:
1106 * POSTED_INTR_NV = 0x00000002,
1107 * GUEST_INTR_STATUS = 0x00000810,
1108 * APIC_ACCESS_ADDR = 0x00002014,
1109 * POSTED_INTR_DESC_ADDR = 0x00002016,
1110 * EOI_EXIT_BITMAP0 = 0x0000201c,
1111 * EOI_EXIT_BITMAP1 = 0x0000201e,
1112 * EOI_EXIT_BITMAP2 = 0x00002020,
1113 * EOI_EXIT_BITMAP3 = 0x00002022,
1115 vmcs_conf->pin_based_exec_ctrl &= ~PIN_BASED_POSTED_INTR;
1116 vmcs_conf->cpu_based_2nd_exec_ctrl &=
1117 ~SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY;
1118 vmcs_conf->cpu_based_2nd_exec_ctrl &=
1119 ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
1120 vmcs_conf->cpu_based_2nd_exec_ctrl &=
1121 ~SECONDARY_EXEC_APIC_REGISTER_VIRT;
1124 * GUEST_PML_INDEX = 0x00000812,
1125 * PML_ADDRESS = 0x0000200e,
1127 vmcs_conf->cpu_based_2nd_exec_ctrl &= ~SECONDARY_EXEC_ENABLE_PML;
1129 /* VM_FUNCTION_CONTROL = 0x00002018, */
1130 vmcs_conf->cpu_based_2nd_exec_ctrl &= ~SECONDARY_EXEC_ENABLE_VMFUNC;
1133 * EPTP_LIST_ADDRESS = 0x00002024,
1134 * VMREAD_BITMAP = 0x00002026,
1135 * VMWRITE_BITMAP = 0x00002028,
1137 vmcs_conf->cpu_based_2nd_exec_ctrl &= ~SECONDARY_EXEC_SHADOW_VMCS;
1140 * TSC_MULTIPLIER = 0x00002032,
1142 vmcs_conf->cpu_based_2nd_exec_ctrl &= ~SECONDARY_EXEC_TSC_SCALING;
1145 * PLE_GAP = 0x00004020,
1146 * PLE_WINDOW = 0x00004022,
1148 vmcs_conf->cpu_based_2nd_exec_ctrl &= ~SECONDARY_EXEC_PAUSE_LOOP_EXITING;
1151 * VMX_PREEMPTION_TIMER_VALUE = 0x0000482E,
1153 vmcs_conf->pin_based_exec_ctrl &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
1156 * GUEST_IA32_PERF_GLOBAL_CTRL = 0x00002808,
1157 * HOST_IA32_PERF_GLOBAL_CTRL = 0x00002c04,
1159 vmcs_conf->vmexit_ctrl &= ~VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL;
1160 vmcs_conf->vmentry_ctrl &= ~VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL;
1163 * Currently unsupported in KVM:
1164 * GUEST_IA32_RTIT_CTL = 0x00002814,
1167 #else /* !IS_ENABLED(CONFIG_HYPERV) */
1168 static inline void evmcs_write64(unsigned long field, u64 value) {}
1169 static inline void evmcs_write32(unsigned long field, u32 value) {}
1170 static inline void evmcs_write16(unsigned long field, u16 value) {}
1171 static inline u64 evmcs_read64(unsigned long field) { return 0; }
1172 static inline u32 evmcs_read32(unsigned long field) { return 0; }
1173 static inline u16 evmcs_read16(unsigned long field) { return 0; }
1174 static inline void evmcs_load(u64 phys_addr) {}
1175 static inline void evmcs_sanitize_exec_ctrls(struct vmcs_config *vmcs_conf) {}
1176 #endif /* IS_ENABLED(CONFIG_HYPERV) */
1178 static inline bool is_exception_n(u32 intr_info, u8 vector)
1180 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
1181 INTR_INFO_VALID_MASK)) ==
1182 (INTR_TYPE_HARD_EXCEPTION | vector | INTR_INFO_VALID_MASK);
1185 static inline bool is_debug(u32 intr_info)
1187 return is_exception_n(intr_info, DB_VECTOR);
1190 static inline bool is_breakpoint(u32 intr_info)
1192 return is_exception_n(intr_info, BP_VECTOR);
1195 static inline bool is_page_fault(u32 intr_info)
1197 return is_exception_n(intr_info, PF_VECTOR);
1200 static inline bool is_no_device(u32 intr_info)
1202 return is_exception_n(intr_info, NM_VECTOR);
1205 static inline bool is_invalid_opcode(u32 intr_info)
1207 return is_exception_n(intr_info, UD_VECTOR);
1210 static inline bool is_gp_fault(u32 intr_info)
1212 return is_exception_n(intr_info, GP_VECTOR);
1215 static inline bool is_external_interrupt(u32 intr_info)
1217 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK))
1218 == (INTR_TYPE_EXT_INTR | INTR_INFO_VALID_MASK);
1221 static inline bool is_machine_check(u32 intr_info)
1223 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
1224 INTR_INFO_VALID_MASK)) ==
1225 (INTR_TYPE_HARD_EXCEPTION | MC_VECTOR | INTR_INFO_VALID_MASK);
1228 /* Undocumented: icebp/int1 */
1229 static inline bool is_icebp(u32 intr_info)
1231 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK))
1232 == (INTR_TYPE_PRIV_SW_EXCEPTION | INTR_INFO_VALID_MASK);
1235 static inline bool cpu_has_vmx_msr_bitmap(void)
1237 return vmcs_config.cpu_based_exec_ctrl & CPU_BASED_USE_MSR_BITMAPS;
1240 static inline bool cpu_has_vmx_tpr_shadow(void)
1242 return vmcs_config.cpu_based_exec_ctrl & CPU_BASED_TPR_SHADOW;
1245 static inline bool cpu_need_tpr_shadow(struct kvm_vcpu *vcpu)
1247 return cpu_has_vmx_tpr_shadow() && lapic_in_kernel(vcpu);
1250 static inline bool cpu_has_secondary_exec_ctrls(void)
1252 return vmcs_config.cpu_based_exec_ctrl &
1253 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
1256 static inline bool cpu_has_vmx_virtualize_apic_accesses(void)
1258 return vmcs_config.cpu_based_2nd_exec_ctrl &
1259 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
1262 static inline bool cpu_has_vmx_virtualize_x2apic_mode(void)
1264 return vmcs_config.cpu_based_2nd_exec_ctrl &
1265 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
1268 static inline bool cpu_has_vmx_apic_register_virt(void)
1270 return vmcs_config.cpu_based_2nd_exec_ctrl &
1271 SECONDARY_EXEC_APIC_REGISTER_VIRT;
1274 static inline bool cpu_has_vmx_virtual_intr_delivery(void)
1276 return vmcs_config.cpu_based_2nd_exec_ctrl &
1277 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY;
1281 * Comment's format: document - errata name - stepping - processor name.
1283 * https://www.virtualbox.org/svn/vbox/trunk/src/VBox/VMM/VMMR0/HMR0.cpp
1285 static u32 vmx_preemption_cpu_tfms[] = {
1286 /* 323344.pdf - BA86 - D0 - Xeon 7500 Series */
1288 /* 323056.pdf - AAX65 - C2 - Xeon L3406 */
1289 /* 322814.pdf - AAT59 - C2 - i7-600, i5-500, i5-400 and i3-300 Mobile */
1290 /* 322911.pdf - AAU65 - C2 - i5-600, i3-500 Desktop and Pentium G6950 */
1292 /* 322911.pdf - AAU65 - K0 - i5-600, i3-500 Desktop and Pentium G6950 */
1294 /* 322373.pdf - AAO95 - B1 - Xeon 3400 Series */
1295 /* 322166.pdf - AAN92 - B1 - i7-800 and i5-700 Desktop */
1297 * 320767.pdf - AAP86 - B1 -
1298 * i7-900 Mobile Extreme, i7-800 and i7-700 Mobile
1301 /* 321333.pdf - AAM126 - C0 - Xeon 3500 */
1303 /* 321333.pdf - AAM126 - C1 - Xeon 3500 */
1305 /* 320836.pdf - AAJ124 - C0 - i7-900 Desktop Extreme and i7-900 Desktop */
1307 /* 321333.pdf - AAM126 - D0 - Xeon 3500 */
1308 /* 321324.pdf - AAK139 - D0 - Xeon 5500 */
1309 /* 320836.pdf - AAJ124 - D0 - i7-900 Extreme and i7-900 Desktop */
1313 static inline bool cpu_has_broken_vmx_preemption_timer(void)
1315 u32 eax = cpuid_eax(0x00000001), i;
1317 /* Clear the reserved bits */
1318 eax &= ~(0x3U << 14 | 0xfU << 28);
1319 for (i = 0; i < ARRAY_SIZE(vmx_preemption_cpu_tfms); i++)
1320 if (eax == vmx_preemption_cpu_tfms[i])
1326 static inline bool cpu_has_vmx_preemption_timer(void)
1328 return vmcs_config.pin_based_exec_ctrl &
1329 PIN_BASED_VMX_PREEMPTION_TIMER;
1332 static inline bool cpu_has_vmx_posted_intr(void)
1334 return IS_ENABLED(CONFIG_X86_LOCAL_APIC) &&
1335 vmcs_config.pin_based_exec_ctrl & PIN_BASED_POSTED_INTR;
1338 static inline bool cpu_has_vmx_apicv(void)
1340 return cpu_has_vmx_apic_register_virt() &&
1341 cpu_has_vmx_virtual_intr_delivery() &&
1342 cpu_has_vmx_posted_intr();
1345 static inline bool cpu_has_vmx_flexpriority(void)
1347 return cpu_has_vmx_tpr_shadow() &&
1348 cpu_has_vmx_virtualize_apic_accesses();
1351 static inline bool cpu_has_vmx_ept_execute_only(void)
1353 return vmx_capability.ept & VMX_EPT_EXECUTE_ONLY_BIT;
1356 static inline bool cpu_has_vmx_ept_2m_page(void)
1358 return vmx_capability.ept & VMX_EPT_2MB_PAGE_BIT;
1361 static inline bool cpu_has_vmx_ept_1g_page(void)
1363 return vmx_capability.ept & VMX_EPT_1GB_PAGE_BIT;
1366 static inline bool cpu_has_vmx_ept_4levels(void)
1368 return vmx_capability.ept & VMX_EPT_PAGE_WALK_4_BIT;
1371 static inline bool cpu_has_vmx_ept_mt_wb(void)
1373 return vmx_capability.ept & VMX_EPTP_WB_BIT;
1376 static inline bool cpu_has_vmx_ept_5levels(void)
1378 return vmx_capability.ept & VMX_EPT_PAGE_WALK_5_BIT;
1381 static inline bool cpu_has_vmx_ept_ad_bits(void)
1383 return vmx_capability.ept & VMX_EPT_AD_BIT;
1386 static inline bool cpu_has_vmx_invept_context(void)
1388 return vmx_capability.ept & VMX_EPT_EXTENT_CONTEXT_BIT;
1391 static inline bool cpu_has_vmx_invept_global(void)
1393 return vmx_capability.ept & VMX_EPT_EXTENT_GLOBAL_BIT;
1396 static inline bool cpu_has_vmx_invvpid_single(void)
1398 return vmx_capability.vpid & VMX_VPID_EXTENT_SINGLE_CONTEXT_BIT;
1401 static inline bool cpu_has_vmx_invvpid_global(void)
1403 return vmx_capability.vpid & VMX_VPID_EXTENT_GLOBAL_CONTEXT_BIT;
1406 static inline bool cpu_has_vmx_invvpid(void)
1408 return vmx_capability.vpid & VMX_VPID_INVVPID_BIT;
1411 static inline bool cpu_has_vmx_ept(void)
1413 return vmcs_config.cpu_based_2nd_exec_ctrl &
1414 SECONDARY_EXEC_ENABLE_EPT;
1417 static inline bool cpu_has_vmx_unrestricted_guest(void)
1419 return vmcs_config.cpu_based_2nd_exec_ctrl &
1420 SECONDARY_EXEC_UNRESTRICTED_GUEST;
1423 static inline bool cpu_has_vmx_ple(void)
1425 return vmcs_config.cpu_based_2nd_exec_ctrl &
1426 SECONDARY_EXEC_PAUSE_LOOP_EXITING;
1429 static inline bool cpu_has_vmx_basic_inout(void)
1431 return (((u64)vmcs_config.basic_cap << 32) & VMX_BASIC_INOUT);
1434 static inline bool cpu_need_virtualize_apic_accesses(struct kvm_vcpu *vcpu)
1436 return flexpriority_enabled && lapic_in_kernel(vcpu);
1439 static inline bool cpu_has_vmx_vpid(void)
1441 return vmcs_config.cpu_based_2nd_exec_ctrl &
1442 SECONDARY_EXEC_ENABLE_VPID;
1445 static inline bool cpu_has_vmx_rdtscp(void)
1447 return vmcs_config.cpu_based_2nd_exec_ctrl &
1448 SECONDARY_EXEC_RDTSCP;
1451 static inline bool cpu_has_vmx_invpcid(void)
1453 return vmcs_config.cpu_based_2nd_exec_ctrl &
1454 SECONDARY_EXEC_ENABLE_INVPCID;
1457 static inline bool cpu_has_virtual_nmis(void)
1459 return vmcs_config.pin_based_exec_ctrl & PIN_BASED_VIRTUAL_NMIS;
1462 static inline bool cpu_has_vmx_wbinvd_exit(void)
1464 return vmcs_config.cpu_based_2nd_exec_ctrl &
1465 SECONDARY_EXEC_WBINVD_EXITING;
1468 static inline bool cpu_has_vmx_shadow_vmcs(void)
1471 rdmsrl(MSR_IA32_VMX_MISC, vmx_msr);
1472 /* check if the cpu supports writing r/o exit information fields */
1473 if (!(vmx_msr & MSR_IA32_VMX_MISC_VMWRITE_SHADOW_RO_FIELDS))
1476 return vmcs_config.cpu_based_2nd_exec_ctrl &
1477 SECONDARY_EXEC_SHADOW_VMCS;
1480 static inline bool cpu_has_vmx_pml(void)
1482 return vmcs_config.cpu_based_2nd_exec_ctrl & SECONDARY_EXEC_ENABLE_PML;
1485 static inline bool cpu_has_vmx_tsc_scaling(void)
1487 return vmcs_config.cpu_based_2nd_exec_ctrl &
1488 SECONDARY_EXEC_TSC_SCALING;
1491 static inline bool cpu_has_vmx_vmfunc(void)
1493 return vmcs_config.cpu_based_2nd_exec_ctrl &
1494 SECONDARY_EXEC_ENABLE_VMFUNC;
1497 static bool vmx_umip_emulated(void)
1499 return vmcs_config.cpu_based_2nd_exec_ctrl &
1500 SECONDARY_EXEC_DESC;
1503 static inline bool report_flexpriority(void)
1505 return flexpriority_enabled;
1508 static inline unsigned nested_cpu_vmx_misc_cr3_count(struct kvm_vcpu *vcpu)
1510 return vmx_misc_cr3_count(to_vmx(vcpu)->nested.msrs.misc_low);
1513 static inline bool nested_cpu_has(struct vmcs12 *vmcs12, u32 bit)
1515 return vmcs12->cpu_based_vm_exec_control & bit;
1518 static inline bool nested_cpu_has2(struct vmcs12 *vmcs12, u32 bit)
1520 return (vmcs12->cpu_based_vm_exec_control &
1521 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) &&
1522 (vmcs12->secondary_vm_exec_control & bit);
1525 static inline bool nested_cpu_has_preemption_timer(struct vmcs12 *vmcs12)
1527 return vmcs12->pin_based_vm_exec_control &
1528 PIN_BASED_VMX_PREEMPTION_TIMER;
1531 static inline bool nested_cpu_has_nmi_exiting(struct vmcs12 *vmcs12)
1533 return vmcs12->pin_based_vm_exec_control & PIN_BASED_NMI_EXITING;
1536 static inline bool nested_cpu_has_virtual_nmis(struct vmcs12 *vmcs12)
1538 return vmcs12->pin_based_vm_exec_control & PIN_BASED_VIRTUAL_NMIS;
1541 static inline int nested_cpu_has_ept(struct vmcs12 *vmcs12)
1543 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_EPT);
1546 static inline bool nested_cpu_has_xsaves(struct vmcs12 *vmcs12)
1548 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_XSAVES);
1551 static inline bool nested_cpu_has_pml(struct vmcs12 *vmcs12)
1553 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_PML);
1556 static inline bool nested_cpu_has_virt_x2apic_mode(struct vmcs12 *vmcs12)
1558 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE);
1561 static inline bool nested_cpu_has_vpid(struct vmcs12 *vmcs12)
1563 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_VPID);
1566 static inline bool nested_cpu_has_apic_reg_virt(struct vmcs12 *vmcs12)
1568 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_APIC_REGISTER_VIRT);
1571 static inline bool nested_cpu_has_vid(struct vmcs12 *vmcs12)
1573 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
1576 static inline bool nested_cpu_has_posted_intr(struct vmcs12 *vmcs12)
1578 return vmcs12->pin_based_vm_exec_control & PIN_BASED_POSTED_INTR;
1581 static inline bool nested_cpu_has_vmfunc(struct vmcs12 *vmcs12)
1583 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_VMFUNC);
1586 static inline bool nested_cpu_has_eptp_switching(struct vmcs12 *vmcs12)
1588 return nested_cpu_has_vmfunc(vmcs12) &&
1589 (vmcs12->vm_function_control &
1590 VMX_VMFUNC_EPTP_SWITCHING);
1593 static inline bool is_nmi(u32 intr_info)
1595 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK))
1596 == (INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK);
1599 static void nested_vmx_vmexit(struct kvm_vcpu *vcpu, u32 exit_reason,
1601 unsigned long exit_qualification);
1602 static void nested_vmx_entry_failure(struct kvm_vcpu *vcpu,
1603 struct vmcs12 *vmcs12,
1604 u32 reason, unsigned long qualification);
1606 static int __find_msr_index(struct vcpu_vmx *vmx, u32 msr)
1610 for (i = 0; i < vmx->nmsrs; ++i)
1611 if (vmx_msr_index[vmx->guest_msrs[i].index] == msr)
1616 static inline void __invvpid(int ext, u16 vpid, gva_t gva)
1622 } operand = { vpid, 0, gva };
1624 asm volatile (__ex(ASM_VMX_INVVPID)
1625 /* CF==1 or ZF==1 --> rc = -1 */
1626 "; ja 1f ; ud2 ; 1:"
1627 : : "a"(&operand), "c"(ext) : "cc", "memory");
1630 static inline void __invept(int ext, u64 eptp, gpa_t gpa)
1634 } operand = {eptp, gpa};
1636 asm volatile (__ex(ASM_VMX_INVEPT)
1637 /* CF==1 or ZF==1 --> rc = -1 */
1638 "; ja 1f ; ud2 ; 1:\n"
1639 : : "a" (&operand), "c" (ext) : "cc", "memory");
1642 static struct shared_msr_entry *find_msr_entry(struct vcpu_vmx *vmx, u32 msr)
1646 i = __find_msr_index(vmx, msr);
1648 return &vmx->guest_msrs[i];
1652 static void vmcs_clear(struct vmcs *vmcs)
1654 u64 phys_addr = __pa(vmcs);
1657 asm volatile (__ex(ASM_VMX_VMCLEAR_RAX) "; setna %0"
1658 : "=qm"(error) : "a"(&phys_addr), "m"(phys_addr)
1661 printk(KERN_ERR "kvm: vmclear fail: %p/%llx\n",
1665 static inline void loaded_vmcs_init(struct loaded_vmcs *loaded_vmcs)
1667 vmcs_clear(loaded_vmcs->vmcs);
1668 if (loaded_vmcs->shadow_vmcs && loaded_vmcs->launched)
1669 vmcs_clear(loaded_vmcs->shadow_vmcs);
1670 loaded_vmcs->cpu = -1;
1671 loaded_vmcs->launched = 0;
1674 static void vmcs_load(struct vmcs *vmcs)
1676 u64 phys_addr = __pa(vmcs);
1679 if (static_branch_unlikely(&enable_evmcs))
1680 return evmcs_load(phys_addr);
1682 asm volatile (__ex(ASM_VMX_VMPTRLD_RAX) "; setna %0"
1683 : "=qm"(error) : "a"(&phys_addr), "m"(phys_addr)
1686 printk(KERN_ERR "kvm: vmptrld %p/%llx failed\n",
1690 #ifdef CONFIG_KEXEC_CORE
1692 * This bitmap is used to indicate whether the vmclear
1693 * operation is enabled on all cpus. All disabled by
1696 static cpumask_t crash_vmclear_enabled_bitmap = CPU_MASK_NONE;
1698 static inline void crash_enable_local_vmclear(int cpu)
1700 cpumask_set_cpu(cpu, &crash_vmclear_enabled_bitmap);
1703 static inline void crash_disable_local_vmclear(int cpu)
1705 cpumask_clear_cpu(cpu, &crash_vmclear_enabled_bitmap);
1708 static inline int crash_local_vmclear_enabled(int cpu)
1710 return cpumask_test_cpu(cpu, &crash_vmclear_enabled_bitmap);
1713 static void crash_vmclear_local_loaded_vmcss(void)
1715 int cpu = raw_smp_processor_id();
1716 struct loaded_vmcs *v;
1718 if (!crash_local_vmclear_enabled(cpu))
1721 list_for_each_entry(v, &per_cpu(loaded_vmcss_on_cpu, cpu),
1722 loaded_vmcss_on_cpu_link)
1723 vmcs_clear(v->vmcs);
1726 static inline void crash_enable_local_vmclear(int cpu) { }
1727 static inline void crash_disable_local_vmclear(int cpu) { }
1728 #endif /* CONFIG_KEXEC_CORE */
1730 static void __loaded_vmcs_clear(void *arg)
1732 struct loaded_vmcs *loaded_vmcs = arg;
1733 int cpu = raw_smp_processor_id();
1735 if (loaded_vmcs->cpu != cpu)
1736 return; /* vcpu migration can race with cpu offline */
1737 if (per_cpu(current_vmcs, cpu) == loaded_vmcs->vmcs)
1738 per_cpu(current_vmcs, cpu) = NULL;
1739 crash_disable_local_vmclear(cpu);
1740 list_del(&loaded_vmcs->loaded_vmcss_on_cpu_link);
1743 * we should ensure updating loaded_vmcs->loaded_vmcss_on_cpu_link
1744 * is before setting loaded_vmcs->vcpu to -1 which is done in
1745 * loaded_vmcs_init. Otherwise, other cpu can see vcpu = -1 fist
1746 * then adds the vmcs into percpu list before it is deleted.
1750 loaded_vmcs_init(loaded_vmcs);
1751 crash_enable_local_vmclear(cpu);
1754 static void loaded_vmcs_clear(struct loaded_vmcs *loaded_vmcs)
1756 int cpu = loaded_vmcs->cpu;
1759 smp_call_function_single(cpu,
1760 __loaded_vmcs_clear, loaded_vmcs, 1);
1763 static inline void vpid_sync_vcpu_single(int vpid)
1768 if (cpu_has_vmx_invvpid_single())
1769 __invvpid(VMX_VPID_EXTENT_SINGLE_CONTEXT, vpid, 0);
1772 static inline void vpid_sync_vcpu_global(void)
1774 if (cpu_has_vmx_invvpid_global())
1775 __invvpid(VMX_VPID_EXTENT_ALL_CONTEXT, 0, 0);
1778 static inline void vpid_sync_context(int vpid)
1780 if (cpu_has_vmx_invvpid_single())
1781 vpid_sync_vcpu_single(vpid);
1783 vpid_sync_vcpu_global();
1786 static inline void ept_sync_global(void)
1788 __invept(VMX_EPT_EXTENT_GLOBAL, 0, 0);
1791 static inline void ept_sync_context(u64 eptp)
1793 if (cpu_has_vmx_invept_context())
1794 __invept(VMX_EPT_EXTENT_CONTEXT, eptp, 0);
1799 static __always_inline void vmcs_check16(unsigned long field)
1801 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2000,
1802 "16-bit accessor invalid for 64-bit field");
1803 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2001,
1804 "16-bit accessor invalid for 64-bit high field");
1805 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x4000,
1806 "16-bit accessor invalid for 32-bit high field");
1807 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x6000,
1808 "16-bit accessor invalid for natural width field");
1811 static __always_inline void vmcs_check32(unsigned long field)
1813 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0,
1814 "32-bit accessor invalid for 16-bit field");
1815 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x6000,
1816 "32-bit accessor invalid for natural width field");
1819 static __always_inline void vmcs_check64(unsigned long field)
1821 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0,
1822 "64-bit accessor invalid for 16-bit field");
1823 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2001,
1824 "64-bit accessor invalid for 64-bit high field");
1825 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x4000,
1826 "64-bit accessor invalid for 32-bit field");
1827 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x6000,
1828 "64-bit accessor invalid for natural width field");
1831 static __always_inline void vmcs_checkl(unsigned long field)
1833 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0,
1834 "Natural width accessor invalid for 16-bit field");
1835 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2000,
1836 "Natural width accessor invalid for 64-bit field");
1837 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2001,
1838 "Natural width accessor invalid for 64-bit high field");
1839 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x4000,
1840 "Natural width accessor invalid for 32-bit field");
1843 static __always_inline unsigned long __vmcs_readl(unsigned long field)
1845 unsigned long value;
1847 asm volatile (__ex_clear(ASM_VMX_VMREAD_RDX_RAX, "%0")
1848 : "=a"(value) : "d"(field) : "cc");
1852 static __always_inline u16 vmcs_read16(unsigned long field)
1854 vmcs_check16(field);
1855 if (static_branch_unlikely(&enable_evmcs))
1856 return evmcs_read16(field);
1857 return __vmcs_readl(field);
1860 static __always_inline u32 vmcs_read32(unsigned long field)
1862 vmcs_check32(field);
1863 if (static_branch_unlikely(&enable_evmcs))
1864 return evmcs_read32(field);
1865 return __vmcs_readl(field);
1868 static __always_inline u64 vmcs_read64(unsigned long field)
1870 vmcs_check64(field);
1871 if (static_branch_unlikely(&enable_evmcs))
1872 return evmcs_read64(field);
1873 #ifdef CONFIG_X86_64
1874 return __vmcs_readl(field);
1876 return __vmcs_readl(field) | ((u64)__vmcs_readl(field+1) << 32);
1880 static __always_inline unsigned long vmcs_readl(unsigned long field)
1883 if (static_branch_unlikely(&enable_evmcs))
1884 return evmcs_read64(field);
1885 return __vmcs_readl(field);
1888 static noinline void vmwrite_error(unsigned long field, unsigned long value)
1890 printk(KERN_ERR "vmwrite error: reg %lx value %lx (err %d)\n",
1891 field, value, vmcs_read32(VM_INSTRUCTION_ERROR));
1895 static __always_inline void __vmcs_writel(unsigned long field, unsigned long value)
1899 asm volatile (__ex(ASM_VMX_VMWRITE_RAX_RDX) "; setna %0"
1900 : "=q"(error) : "a"(value), "d"(field) : "cc");
1901 if (unlikely(error))
1902 vmwrite_error(field, value);
1905 static __always_inline void vmcs_write16(unsigned long field, u16 value)
1907 vmcs_check16(field);
1908 if (static_branch_unlikely(&enable_evmcs))
1909 return evmcs_write16(field, value);
1911 __vmcs_writel(field, value);
1914 static __always_inline void vmcs_write32(unsigned long field, u32 value)
1916 vmcs_check32(field);
1917 if (static_branch_unlikely(&enable_evmcs))
1918 return evmcs_write32(field, value);
1920 __vmcs_writel(field, value);
1923 static __always_inline void vmcs_write64(unsigned long field, u64 value)
1925 vmcs_check64(field);
1926 if (static_branch_unlikely(&enable_evmcs))
1927 return evmcs_write64(field, value);
1929 __vmcs_writel(field, value);
1930 #ifndef CONFIG_X86_64
1932 __vmcs_writel(field+1, value >> 32);
1936 static __always_inline void vmcs_writel(unsigned long field, unsigned long value)
1939 if (static_branch_unlikely(&enable_evmcs))
1940 return evmcs_write64(field, value);
1942 __vmcs_writel(field, value);
1945 static __always_inline void vmcs_clear_bits(unsigned long field, u32 mask)
1947 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x2000,
1948 "vmcs_clear_bits does not support 64-bit fields");
1949 if (static_branch_unlikely(&enable_evmcs))
1950 return evmcs_write32(field, evmcs_read32(field) & ~mask);
1952 __vmcs_writel(field, __vmcs_readl(field) & ~mask);
1955 static __always_inline void vmcs_set_bits(unsigned long field, u32 mask)
1957 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x2000,
1958 "vmcs_set_bits does not support 64-bit fields");
1959 if (static_branch_unlikely(&enable_evmcs))
1960 return evmcs_write32(field, evmcs_read32(field) | mask);
1962 __vmcs_writel(field, __vmcs_readl(field) | mask);
1965 static inline void vm_entry_controls_reset_shadow(struct vcpu_vmx *vmx)
1967 vmx->vm_entry_controls_shadow = vmcs_read32(VM_ENTRY_CONTROLS);
1970 static inline void vm_entry_controls_init(struct vcpu_vmx *vmx, u32 val)
1972 vmcs_write32(VM_ENTRY_CONTROLS, val);
1973 vmx->vm_entry_controls_shadow = val;
1976 static inline void vm_entry_controls_set(struct vcpu_vmx *vmx, u32 val)
1978 if (vmx->vm_entry_controls_shadow != val)
1979 vm_entry_controls_init(vmx, val);
1982 static inline u32 vm_entry_controls_get(struct vcpu_vmx *vmx)
1984 return vmx->vm_entry_controls_shadow;
1988 static inline void vm_entry_controls_setbit(struct vcpu_vmx *vmx, u32 val)
1990 vm_entry_controls_set(vmx, vm_entry_controls_get(vmx) | val);
1993 static inline void vm_entry_controls_clearbit(struct vcpu_vmx *vmx, u32 val)
1995 vm_entry_controls_set(vmx, vm_entry_controls_get(vmx) & ~val);
1998 static inline void vm_exit_controls_reset_shadow(struct vcpu_vmx *vmx)
2000 vmx->vm_exit_controls_shadow = vmcs_read32(VM_EXIT_CONTROLS);
2003 static inline void vm_exit_controls_init(struct vcpu_vmx *vmx, u32 val)
2005 vmcs_write32(VM_EXIT_CONTROLS, val);
2006 vmx->vm_exit_controls_shadow = val;
2009 static inline void vm_exit_controls_set(struct vcpu_vmx *vmx, u32 val)
2011 if (vmx->vm_exit_controls_shadow != val)
2012 vm_exit_controls_init(vmx, val);
2015 static inline u32 vm_exit_controls_get(struct vcpu_vmx *vmx)
2017 return vmx->vm_exit_controls_shadow;
2021 static inline void vm_exit_controls_setbit(struct vcpu_vmx *vmx, u32 val)
2023 vm_exit_controls_set(vmx, vm_exit_controls_get(vmx) | val);
2026 static inline void vm_exit_controls_clearbit(struct vcpu_vmx *vmx, u32 val)
2028 vm_exit_controls_set(vmx, vm_exit_controls_get(vmx) & ~val);
2031 static void vmx_segment_cache_clear(struct vcpu_vmx *vmx)
2033 vmx->segment_cache.bitmask = 0;
2036 static bool vmx_segment_cache_test_set(struct vcpu_vmx *vmx, unsigned seg,
2040 u32 mask = 1 << (seg * SEG_FIELD_NR + field);
2042 if (!(vmx->vcpu.arch.regs_avail & (1 << VCPU_EXREG_SEGMENTS))) {
2043 vmx->vcpu.arch.regs_avail |= (1 << VCPU_EXREG_SEGMENTS);
2044 vmx->segment_cache.bitmask = 0;
2046 ret = vmx->segment_cache.bitmask & mask;
2047 vmx->segment_cache.bitmask |= mask;
2051 static u16 vmx_read_guest_seg_selector(struct vcpu_vmx *vmx, unsigned seg)
2053 u16 *p = &vmx->segment_cache.seg[seg].selector;
2055 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_SEL))
2056 *p = vmcs_read16(kvm_vmx_segment_fields[seg].selector);
2060 static ulong vmx_read_guest_seg_base(struct vcpu_vmx *vmx, unsigned seg)
2062 ulong *p = &vmx->segment_cache.seg[seg].base;
2064 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_BASE))
2065 *p = vmcs_readl(kvm_vmx_segment_fields[seg].base);
2069 static u32 vmx_read_guest_seg_limit(struct vcpu_vmx *vmx, unsigned seg)
2071 u32 *p = &vmx->segment_cache.seg[seg].limit;
2073 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_LIMIT))
2074 *p = vmcs_read32(kvm_vmx_segment_fields[seg].limit);
2078 static u32 vmx_read_guest_seg_ar(struct vcpu_vmx *vmx, unsigned seg)
2080 u32 *p = &vmx->segment_cache.seg[seg].ar;
2082 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_AR))
2083 *p = vmcs_read32(kvm_vmx_segment_fields[seg].ar_bytes);
2087 static void update_exception_bitmap(struct kvm_vcpu *vcpu)
2091 eb = (1u << PF_VECTOR) | (1u << UD_VECTOR) | (1u << MC_VECTOR) |
2092 (1u << DB_VECTOR) | (1u << AC_VECTOR);
2094 * Guest access to VMware backdoor ports could legitimately
2095 * trigger #GP because of TSS I/O permission bitmap.
2096 * We intercept those #GP and allow access to them anyway
2099 if (enable_vmware_backdoor)
2100 eb |= (1u << GP_VECTOR);
2101 if ((vcpu->guest_debug &
2102 (KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_SW_BP)) ==
2103 (KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_SW_BP))
2104 eb |= 1u << BP_VECTOR;
2105 if (to_vmx(vcpu)->rmode.vm86_active)
2108 eb &= ~(1u << PF_VECTOR); /* bypass_guest_pf = 0 */
2110 /* When we are running a nested L2 guest and L1 specified for it a
2111 * certain exception bitmap, we must trap the same exceptions and pass
2112 * them to L1. When running L2, we will only handle the exceptions
2113 * specified above if L1 did not want them.
2115 if (is_guest_mode(vcpu))
2116 eb |= get_vmcs12(vcpu)->exception_bitmap;
2118 vmcs_write32(EXCEPTION_BITMAP, eb);
2122 * Check if MSR is intercepted for currently loaded MSR bitmap.
2124 static bool msr_write_intercepted(struct kvm_vcpu *vcpu, u32 msr)
2126 unsigned long *msr_bitmap;
2127 int f = sizeof(unsigned long);
2129 if (!cpu_has_vmx_msr_bitmap())
2132 msr_bitmap = to_vmx(vcpu)->loaded_vmcs->msr_bitmap;
2134 if (msr <= 0x1fff) {
2135 return !!test_bit(msr, msr_bitmap + 0x800 / f);
2136 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
2138 return !!test_bit(msr, msr_bitmap + 0xc00 / f);
2145 * Check if MSR is intercepted for L01 MSR bitmap.
2147 static bool msr_write_intercepted_l01(struct kvm_vcpu *vcpu, u32 msr)
2149 unsigned long *msr_bitmap;
2150 int f = sizeof(unsigned long);
2152 if (!cpu_has_vmx_msr_bitmap())
2155 msr_bitmap = to_vmx(vcpu)->vmcs01.msr_bitmap;
2157 if (msr <= 0x1fff) {
2158 return !!test_bit(msr, msr_bitmap + 0x800 / f);
2159 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
2161 return !!test_bit(msr, msr_bitmap + 0xc00 / f);
2167 static void clear_atomic_switch_msr_special(struct vcpu_vmx *vmx,
2168 unsigned long entry, unsigned long exit)
2170 vm_entry_controls_clearbit(vmx, entry);
2171 vm_exit_controls_clearbit(vmx, exit);
2174 static void clear_atomic_switch_msr(struct vcpu_vmx *vmx, unsigned msr)
2177 struct msr_autoload *m = &vmx->msr_autoload;
2181 if (cpu_has_load_ia32_efer) {
2182 clear_atomic_switch_msr_special(vmx,
2183 VM_ENTRY_LOAD_IA32_EFER,
2184 VM_EXIT_LOAD_IA32_EFER);
2188 case MSR_CORE_PERF_GLOBAL_CTRL:
2189 if (cpu_has_load_perf_global_ctrl) {
2190 clear_atomic_switch_msr_special(vmx,
2191 VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL,
2192 VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL);
2198 for (i = 0; i < m->nr; ++i)
2199 if (m->guest[i].index == msr)
2205 m->guest[i] = m->guest[m->nr];
2206 m->host[i] = m->host[m->nr];
2207 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, m->nr);
2208 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, m->nr);
2211 static void add_atomic_switch_msr_special(struct vcpu_vmx *vmx,
2212 unsigned long entry, unsigned long exit,
2213 unsigned long guest_val_vmcs, unsigned long host_val_vmcs,
2214 u64 guest_val, u64 host_val)
2216 vmcs_write64(guest_val_vmcs, guest_val);
2217 vmcs_write64(host_val_vmcs, host_val);
2218 vm_entry_controls_setbit(vmx, entry);
2219 vm_exit_controls_setbit(vmx, exit);
2222 static void add_atomic_switch_msr(struct vcpu_vmx *vmx, unsigned msr,
2223 u64 guest_val, u64 host_val)
2226 struct msr_autoload *m = &vmx->msr_autoload;
2230 if (cpu_has_load_ia32_efer) {
2231 add_atomic_switch_msr_special(vmx,
2232 VM_ENTRY_LOAD_IA32_EFER,
2233 VM_EXIT_LOAD_IA32_EFER,
2236 guest_val, host_val);
2240 case MSR_CORE_PERF_GLOBAL_CTRL:
2241 if (cpu_has_load_perf_global_ctrl) {
2242 add_atomic_switch_msr_special(vmx,
2243 VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL,
2244 VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL,
2245 GUEST_IA32_PERF_GLOBAL_CTRL,
2246 HOST_IA32_PERF_GLOBAL_CTRL,
2247 guest_val, host_val);
2251 case MSR_IA32_PEBS_ENABLE:
2252 /* PEBS needs a quiescent period after being disabled (to write
2253 * a record). Disabling PEBS through VMX MSR swapping doesn't
2254 * provide that period, so a CPU could write host's record into
2257 wrmsrl(MSR_IA32_PEBS_ENABLE, 0);
2260 for (i = 0; i < m->nr; ++i)
2261 if (m->guest[i].index == msr)
2264 if (i == NR_AUTOLOAD_MSRS) {
2265 printk_once(KERN_WARNING "Not enough msr switch entries. "
2266 "Can't add msr %x\n", msr);
2268 } else if (i == m->nr) {
2270 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, m->nr);
2271 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, m->nr);
2274 m->guest[i].index = msr;
2275 m->guest[i].value = guest_val;
2276 m->host[i].index = msr;
2277 m->host[i].value = host_val;
2280 static bool update_transition_efer(struct vcpu_vmx *vmx, int efer_offset)
2282 u64 guest_efer = vmx->vcpu.arch.efer;
2283 u64 ignore_bits = 0;
2287 * NX is needed to handle CR0.WP=1, CR4.SMEP=1. Testing
2288 * host CPUID is more efficient than testing guest CPUID
2289 * or CR4. Host SMEP is anyway a requirement for guest SMEP.
2291 if (boot_cpu_has(X86_FEATURE_SMEP))
2292 guest_efer |= EFER_NX;
2293 else if (!(guest_efer & EFER_NX))
2294 ignore_bits |= EFER_NX;
2298 * LMA and LME handled by hardware; SCE meaningless outside long mode.
2300 ignore_bits |= EFER_SCE;
2301 #ifdef CONFIG_X86_64
2302 ignore_bits |= EFER_LMA | EFER_LME;
2303 /* SCE is meaningful only in long mode on Intel */
2304 if (guest_efer & EFER_LMA)
2305 ignore_bits &= ~(u64)EFER_SCE;
2308 clear_atomic_switch_msr(vmx, MSR_EFER);
2311 * On EPT, we can't emulate NX, so we must switch EFER atomically.
2312 * On CPUs that support "load IA32_EFER", always switch EFER
2313 * atomically, since it's faster than switching it manually.
2315 if (cpu_has_load_ia32_efer ||
2316 (enable_ept && ((vmx->vcpu.arch.efer ^ host_efer) & EFER_NX))) {
2317 if (!(guest_efer & EFER_LMA))
2318 guest_efer &= ~EFER_LME;
2319 if (guest_efer != host_efer)
2320 add_atomic_switch_msr(vmx, MSR_EFER,
2321 guest_efer, host_efer);
2324 guest_efer &= ~ignore_bits;
2325 guest_efer |= host_efer & ignore_bits;
2327 vmx->guest_msrs[efer_offset].data = guest_efer;
2328 vmx->guest_msrs[efer_offset].mask = ~ignore_bits;
2334 #ifdef CONFIG_X86_32
2336 * On 32-bit kernels, VM exits still load the FS and GS bases from the
2337 * VMCS rather than the segment table. KVM uses this helper to figure
2338 * out the current bases to poke them into the VMCS before entry.
2340 static unsigned long segment_base(u16 selector)
2342 struct desc_struct *table;
2345 if (!(selector & ~SEGMENT_RPL_MASK))
2348 table = get_current_gdt_ro();
2350 if ((selector & SEGMENT_TI_MASK) == SEGMENT_LDT) {
2351 u16 ldt_selector = kvm_read_ldt();
2353 if (!(ldt_selector & ~SEGMENT_RPL_MASK))
2356 table = (struct desc_struct *)segment_base(ldt_selector);
2358 v = get_desc_base(&table[selector >> 3]);
2363 static void vmx_save_host_state(struct kvm_vcpu *vcpu)
2365 struct vcpu_vmx *vmx = to_vmx(vcpu);
2366 #ifdef CONFIG_X86_64
2367 int cpu = raw_smp_processor_id();
2371 if (vmx->host_state.loaded)
2374 vmx->host_state.loaded = 1;
2376 * Set host fs and gs selectors. Unfortunately, 22.2.3 does not
2377 * allow segment selectors with cpl > 0 or ti == 1.
2379 vmx->host_state.ldt_sel = kvm_read_ldt();
2380 vmx->host_state.gs_ldt_reload_needed = vmx->host_state.ldt_sel;
2382 #ifdef CONFIG_X86_64
2383 save_fsgs_for_kvm();
2384 vmx->host_state.fs_sel = current->thread.fsindex;
2385 vmx->host_state.gs_sel = current->thread.gsindex;
2387 savesegment(fs, vmx->host_state.fs_sel);
2388 savesegment(gs, vmx->host_state.gs_sel);
2390 if (!(vmx->host_state.fs_sel & 7)) {
2391 vmcs_write16(HOST_FS_SELECTOR, vmx->host_state.fs_sel);
2392 vmx->host_state.fs_reload_needed = 0;
2394 vmcs_write16(HOST_FS_SELECTOR, 0);
2395 vmx->host_state.fs_reload_needed = 1;
2397 if (!(vmx->host_state.gs_sel & 7))
2398 vmcs_write16(HOST_GS_SELECTOR, vmx->host_state.gs_sel);
2400 vmcs_write16(HOST_GS_SELECTOR, 0);
2401 vmx->host_state.gs_ldt_reload_needed = 1;
2404 #ifdef CONFIG_X86_64
2405 savesegment(ds, vmx->host_state.ds_sel);
2406 savesegment(es, vmx->host_state.es_sel);
2408 vmcs_writel(HOST_FS_BASE, current->thread.fsbase);
2409 vmcs_writel(HOST_GS_BASE, cpu_kernelmode_gs_base(cpu));
2411 vmx->msr_host_kernel_gs_base = current->thread.gsbase;
2412 if (is_long_mode(&vmx->vcpu))
2413 wrmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base);
2415 vmcs_writel(HOST_FS_BASE, segment_base(vmx->host_state.fs_sel));
2416 vmcs_writel(HOST_GS_BASE, segment_base(vmx->host_state.gs_sel));
2418 if (boot_cpu_has(X86_FEATURE_MPX))
2419 rdmsrl(MSR_IA32_BNDCFGS, vmx->host_state.msr_host_bndcfgs);
2420 for (i = 0; i < vmx->save_nmsrs; ++i)
2421 kvm_set_shared_msr(vmx->guest_msrs[i].index,
2422 vmx->guest_msrs[i].data,
2423 vmx->guest_msrs[i].mask);
2426 static void __vmx_load_host_state(struct vcpu_vmx *vmx)
2428 if (!vmx->host_state.loaded)
2431 ++vmx->vcpu.stat.host_state_reload;
2432 vmx->host_state.loaded = 0;
2433 #ifdef CONFIG_X86_64
2434 if (is_long_mode(&vmx->vcpu))
2435 rdmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base);
2437 if (vmx->host_state.gs_ldt_reload_needed) {
2438 kvm_load_ldt(vmx->host_state.ldt_sel);
2439 #ifdef CONFIG_X86_64
2440 load_gs_index(vmx->host_state.gs_sel);
2442 loadsegment(gs, vmx->host_state.gs_sel);
2445 if (vmx->host_state.fs_reload_needed)
2446 loadsegment(fs, vmx->host_state.fs_sel);
2447 #ifdef CONFIG_X86_64
2448 if (unlikely(vmx->host_state.ds_sel | vmx->host_state.es_sel)) {
2449 loadsegment(ds, vmx->host_state.ds_sel);
2450 loadsegment(es, vmx->host_state.es_sel);
2453 invalidate_tss_limit();
2454 #ifdef CONFIG_X86_64
2455 wrmsrl(MSR_KERNEL_GS_BASE, vmx->msr_host_kernel_gs_base);
2457 if (vmx->host_state.msr_host_bndcfgs)
2458 wrmsrl(MSR_IA32_BNDCFGS, vmx->host_state.msr_host_bndcfgs);
2459 load_fixmap_gdt(raw_smp_processor_id());
2462 static void vmx_load_host_state(struct vcpu_vmx *vmx)
2465 __vmx_load_host_state(vmx);
2469 static void vmx_vcpu_pi_load(struct kvm_vcpu *vcpu, int cpu)
2471 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
2472 struct pi_desc old, new;
2476 * In case of hot-plug or hot-unplug, we may have to undo
2477 * vmx_vcpu_pi_put even if there is no assigned device. And we
2478 * always keep PI.NDST up to date for simplicity: it makes the
2479 * code easier, and CPU migration is not a fast path.
2481 if (!pi_test_sn(pi_desc) && vcpu->cpu == cpu)
2485 * First handle the simple case where no cmpxchg is necessary; just
2486 * allow posting non-urgent interrupts.
2488 * If the 'nv' field is POSTED_INTR_WAKEUP_VECTOR, do not change
2489 * PI.NDST: pi_post_block will do it for us and the wakeup_handler
2490 * expects the VCPU to be on the blocked_vcpu_list that matches
2493 if (pi_desc->nv == POSTED_INTR_WAKEUP_VECTOR ||
2495 pi_clear_sn(pi_desc);
2499 /* The full case. */
2501 old.control = new.control = pi_desc->control;
2503 dest = cpu_physical_id(cpu);
2505 if (x2apic_enabled())
2508 new.ndst = (dest << 8) & 0xFF00;
2511 } while (cmpxchg64(&pi_desc->control, old.control,
2512 new.control) != old.control);
2515 static void decache_tsc_multiplier(struct vcpu_vmx *vmx)
2517 vmx->current_tsc_ratio = vmx->vcpu.arch.tsc_scaling_ratio;
2518 vmcs_write64(TSC_MULTIPLIER, vmx->current_tsc_ratio);
2522 * Switches to specified vcpu, until a matching vcpu_put(), but assumes
2523 * vcpu mutex is already taken.
2525 static void vmx_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
2527 struct vcpu_vmx *vmx = to_vmx(vcpu);
2528 bool already_loaded = vmx->loaded_vmcs->cpu == cpu;
2530 if (!already_loaded) {
2531 loaded_vmcs_clear(vmx->loaded_vmcs);
2532 local_irq_disable();
2533 crash_disable_local_vmclear(cpu);
2536 * Read loaded_vmcs->cpu should be before fetching
2537 * loaded_vmcs->loaded_vmcss_on_cpu_link.
2538 * See the comments in __loaded_vmcs_clear().
2542 list_add(&vmx->loaded_vmcs->loaded_vmcss_on_cpu_link,
2543 &per_cpu(loaded_vmcss_on_cpu, cpu));
2544 crash_enable_local_vmclear(cpu);
2548 if (per_cpu(current_vmcs, cpu) != vmx->loaded_vmcs->vmcs) {
2549 per_cpu(current_vmcs, cpu) = vmx->loaded_vmcs->vmcs;
2550 vmcs_load(vmx->loaded_vmcs->vmcs);
2551 indirect_branch_prediction_barrier();
2554 if (!already_loaded) {
2555 void *gdt = get_current_gdt_ro();
2556 unsigned long sysenter_esp;
2558 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
2561 * Linux uses per-cpu TSS and GDT, so set these when switching
2562 * processors. See 22.2.4.
2564 vmcs_writel(HOST_TR_BASE,
2565 (unsigned long)&get_cpu_entry_area(cpu)->tss.x86_tss);
2566 vmcs_writel(HOST_GDTR_BASE, (unsigned long)gdt); /* 22.2.4 */
2569 * VM exits change the host TR limit to 0x67 after a VM
2570 * exit. This is okay, since 0x67 covers everything except
2571 * the IO bitmap and have have code to handle the IO bitmap
2572 * being lost after a VM exit.
2574 BUILD_BUG_ON(IO_BITMAP_OFFSET - 1 != 0x67);
2576 rdmsrl(MSR_IA32_SYSENTER_ESP, sysenter_esp);
2577 vmcs_writel(HOST_IA32_SYSENTER_ESP, sysenter_esp); /* 22.2.3 */
2579 vmx->loaded_vmcs->cpu = cpu;
2582 /* Setup TSC multiplier */
2583 if (kvm_has_tsc_control &&
2584 vmx->current_tsc_ratio != vcpu->arch.tsc_scaling_ratio)
2585 decache_tsc_multiplier(vmx);
2587 vmx_vcpu_pi_load(vcpu, cpu);
2588 vmx->host_pkru = read_pkru();
2589 vmx->host_debugctlmsr = get_debugctlmsr();
2592 static void vmx_vcpu_pi_put(struct kvm_vcpu *vcpu)
2594 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
2596 if (!kvm_arch_has_assigned_device(vcpu->kvm) ||
2597 !irq_remapping_cap(IRQ_POSTING_CAP) ||
2598 !kvm_vcpu_apicv_active(vcpu))
2601 /* Set SN when the vCPU is preempted */
2602 if (vcpu->preempted)
2606 static void vmx_vcpu_put(struct kvm_vcpu *vcpu)
2608 vmx_vcpu_pi_put(vcpu);
2610 __vmx_load_host_state(to_vmx(vcpu));
2613 static bool emulation_required(struct kvm_vcpu *vcpu)
2615 return emulate_invalid_guest_state && !guest_state_valid(vcpu);
2618 static void vmx_decache_cr0_guest_bits(struct kvm_vcpu *vcpu);
2621 * Return the cr0 value that a nested guest would read. This is a combination
2622 * of the real cr0 used to run the guest (guest_cr0), and the bits shadowed by
2623 * its hypervisor (cr0_read_shadow).
2625 static inline unsigned long nested_read_cr0(struct vmcs12 *fields)
2627 return (fields->guest_cr0 & ~fields->cr0_guest_host_mask) |
2628 (fields->cr0_read_shadow & fields->cr0_guest_host_mask);
2630 static inline unsigned long nested_read_cr4(struct vmcs12 *fields)
2632 return (fields->guest_cr4 & ~fields->cr4_guest_host_mask) |
2633 (fields->cr4_read_shadow & fields->cr4_guest_host_mask);
2636 static unsigned long vmx_get_rflags(struct kvm_vcpu *vcpu)
2638 unsigned long rflags, save_rflags;
2640 if (!test_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail)) {
2641 __set_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail);
2642 rflags = vmcs_readl(GUEST_RFLAGS);
2643 if (to_vmx(vcpu)->rmode.vm86_active) {
2644 rflags &= RMODE_GUEST_OWNED_EFLAGS_BITS;
2645 save_rflags = to_vmx(vcpu)->rmode.save_rflags;
2646 rflags |= save_rflags & ~RMODE_GUEST_OWNED_EFLAGS_BITS;
2648 to_vmx(vcpu)->rflags = rflags;
2650 return to_vmx(vcpu)->rflags;
2653 static void vmx_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags)
2655 unsigned long old_rflags = vmx_get_rflags(vcpu);
2657 __set_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail);
2658 to_vmx(vcpu)->rflags = rflags;
2659 if (to_vmx(vcpu)->rmode.vm86_active) {
2660 to_vmx(vcpu)->rmode.save_rflags = rflags;
2661 rflags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM;
2663 vmcs_writel(GUEST_RFLAGS, rflags);
2665 if ((old_rflags ^ to_vmx(vcpu)->rflags) & X86_EFLAGS_VM)
2666 to_vmx(vcpu)->emulation_required = emulation_required(vcpu);
2669 static u32 vmx_get_interrupt_shadow(struct kvm_vcpu *vcpu)
2671 u32 interruptibility = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
2674 if (interruptibility & GUEST_INTR_STATE_STI)
2675 ret |= KVM_X86_SHADOW_INT_STI;
2676 if (interruptibility & GUEST_INTR_STATE_MOV_SS)
2677 ret |= KVM_X86_SHADOW_INT_MOV_SS;
2682 static void vmx_set_interrupt_shadow(struct kvm_vcpu *vcpu, int mask)
2684 u32 interruptibility_old = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
2685 u32 interruptibility = interruptibility_old;
2687 interruptibility &= ~(GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS);
2689 if (mask & KVM_X86_SHADOW_INT_MOV_SS)
2690 interruptibility |= GUEST_INTR_STATE_MOV_SS;
2691 else if (mask & KVM_X86_SHADOW_INT_STI)
2692 interruptibility |= GUEST_INTR_STATE_STI;
2694 if ((interruptibility != interruptibility_old))
2695 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, interruptibility);
2698 static void skip_emulated_instruction(struct kvm_vcpu *vcpu)
2702 rip = kvm_rip_read(vcpu);
2703 rip += vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
2704 kvm_rip_write(vcpu, rip);
2706 /* skipping an emulated instruction also counts */
2707 vmx_set_interrupt_shadow(vcpu, 0);
2710 static void nested_vmx_inject_exception_vmexit(struct kvm_vcpu *vcpu,
2711 unsigned long exit_qual)
2713 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
2714 unsigned int nr = vcpu->arch.exception.nr;
2715 u32 intr_info = nr | INTR_INFO_VALID_MASK;
2717 if (vcpu->arch.exception.has_error_code) {
2718 vmcs12->vm_exit_intr_error_code = vcpu->arch.exception.error_code;
2719 intr_info |= INTR_INFO_DELIVER_CODE_MASK;
2722 if (kvm_exception_is_soft(nr))
2723 intr_info |= INTR_TYPE_SOFT_EXCEPTION;
2725 intr_info |= INTR_TYPE_HARD_EXCEPTION;
2727 if (!(vmcs12->idt_vectoring_info_field & VECTORING_INFO_VALID_MASK) &&
2728 vmx_get_nmi_mask(vcpu))
2729 intr_info |= INTR_INFO_UNBLOCK_NMI;
2731 nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI, intr_info, exit_qual);
2735 * KVM wants to inject page-faults which it got to the guest. This function
2736 * checks whether in a nested guest, we need to inject them to L1 or L2.
2738 static int nested_vmx_check_exception(struct kvm_vcpu *vcpu, unsigned long *exit_qual)
2740 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
2741 unsigned int nr = vcpu->arch.exception.nr;
2743 if (nr == PF_VECTOR) {
2744 if (vcpu->arch.exception.nested_apf) {
2745 *exit_qual = vcpu->arch.apf.nested_apf_token;
2749 * FIXME: we must not write CR2 when L1 intercepts an L2 #PF exception.
2750 * The fix is to add the ancillary datum (CR2 or DR6) to structs
2751 * kvm_queued_exception and kvm_vcpu_events, so that CR2 and DR6
2752 * can be written only when inject_pending_event runs. This should be
2753 * conditional on a new capability---if the capability is disabled,
2754 * kvm_multiple_exception would write the ancillary information to
2755 * CR2 or DR6, for backwards ABI-compatibility.
2757 if (nested_vmx_is_page_fault_vmexit(vmcs12,
2758 vcpu->arch.exception.error_code)) {
2759 *exit_qual = vcpu->arch.cr2;
2763 if (vmcs12->exception_bitmap & (1u << nr)) {
2764 if (nr == DB_VECTOR)
2765 *exit_qual = vcpu->arch.dr6;
2775 static void vmx_clear_hlt(struct kvm_vcpu *vcpu)
2778 * Ensure that we clear the HLT state in the VMCS. We don't need to
2779 * explicitly skip the instruction because if the HLT state is set,
2780 * then the instruction is already executing and RIP has already been
2783 if (kvm_hlt_in_guest(vcpu->kvm) &&
2784 vmcs_read32(GUEST_ACTIVITY_STATE) == GUEST_ACTIVITY_HLT)
2785 vmcs_write32(GUEST_ACTIVITY_STATE, GUEST_ACTIVITY_ACTIVE);
2788 static void vmx_queue_exception(struct kvm_vcpu *vcpu)
2790 struct vcpu_vmx *vmx = to_vmx(vcpu);
2791 unsigned nr = vcpu->arch.exception.nr;
2792 bool has_error_code = vcpu->arch.exception.has_error_code;
2793 u32 error_code = vcpu->arch.exception.error_code;
2794 u32 intr_info = nr | INTR_INFO_VALID_MASK;
2796 if (has_error_code) {
2797 vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE, error_code);
2798 intr_info |= INTR_INFO_DELIVER_CODE_MASK;
2801 if (vmx->rmode.vm86_active) {
2803 if (kvm_exception_is_soft(nr))
2804 inc_eip = vcpu->arch.event_exit_inst_len;
2805 if (kvm_inject_realmode_interrupt(vcpu, nr, inc_eip) != EMULATE_DONE)
2806 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
2810 WARN_ON_ONCE(vmx->emulation_required);
2812 if (kvm_exception_is_soft(nr)) {
2813 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
2814 vmx->vcpu.arch.event_exit_inst_len);
2815 intr_info |= INTR_TYPE_SOFT_EXCEPTION;
2817 intr_info |= INTR_TYPE_HARD_EXCEPTION;
2819 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr_info);
2821 vmx_clear_hlt(vcpu);
2824 static bool vmx_rdtscp_supported(void)
2826 return cpu_has_vmx_rdtscp();
2829 static bool vmx_invpcid_supported(void)
2831 return cpu_has_vmx_invpcid() && enable_ept;
2835 * Swap MSR entry in host/guest MSR entry array.
2837 static void move_msr_up(struct vcpu_vmx *vmx, int from, int to)
2839 struct shared_msr_entry tmp;
2841 tmp = vmx->guest_msrs[to];
2842 vmx->guest_msrs[to] = vmx->guest_msrs[from];
2843 vmx->guest_msrs[from] = tmp;
2847 * Set up the vmcs to automatically save and restore system
2848 * msrs. Don't touch the 64-bit msrs if the guest is in legacy
2849 * mode, as fiddling with msrs is very expensive.
2851 static void setup_msrs(struct vcpu_vmx *vmx)
2853 int save_nmsrs, index;
2856 #ifdef CONFIG_X86_64
2857 if (is_long_mode(&vmx->vcpu)) {
2858 index = __find_msr_index(vmx, MSR_SYSCALL_MASK);
2860 move_msr_up(vmx, index, save_nmsrs++);
2861 index = __find_msr_index(vmx, MSR_LSTAR);
2863 move_msr_up(vmx, index, save_nmsrs++);
2864 index = __find_msr_index(vmx, MSR_CSTAR);
2866 move_msr_up(vmx, index, save_nmsrs++);
2867 index = __find_msr_index(vmx, MSR_TSC_AUX);
2868 if (index >= 0 && guest_cpuid_has(&vmx->vcpu, X86_FEATURE_RDTSCP))
2869 move_msr_up(vmx, index, save_nmsrs++);
2871 * MSR_STAR is only needed on long mode guests, and only
2872 * if efer.sce is enabled.
2874 index = __find_msr_index(vmx, MSR_STAR);
2875 if ((index >= 0) && (vmx->vcpu.arch.efer & EFER_SCE))
2876 move_msr_up(vmx, index, save_nmsrs++);
2879 index = __find_msr_index(vmx, MSR_EFER);
2880 if (index >= 0 && update_transition_efer(vmx, index))
2881 move_msr_up(vmx, index, save_nmsrs++);
2883 vmx->save_nmsrs = save_nmsrs;
2885 if (cpu_has_vmx_msr_bitmap())
2886 vmx_update_msr_bitmap(&vmx->vcpu);
2889 static u64 vmx_read_l1_tsc_offset(struct kvm_vcpu *vcpu)
2891 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
2893 if (is_guest_mode(vcpu) &&
2894 (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING))
2895 return vcpu->arch.tsc_offset - vmcs12->tsc_offset;
2897 return vcpu->arch.tsc_offset;
2901 * writes 'offset' into guest's timestamp counter offset register
2903 static void vmx_write_tsc_offset(struct kvm_vcpu *vcpu, u64 offset)
2905 if (is_guest_mode(vcpu)) {
2907 * We're here if L1 chose not to trap WRMSR to TSC. According
2908 * to the spec, this should set L1's TSC; The offset that L1
2909 * set for L2 remains unchanged, and still needs to be added
2910 * to the newly set TSC to get L2's TSC.
2912 struct vmcs12 *vmcs12;
2913 /* recalculate vmcs02.TSC_OFFSET: */
2914 vmcs12 = get_vmcs12(vcpu);
2915 vmcs_write64(TSC_OFFSET, offset +
2916 (nested_cpu_has(vmcs12, CPU_BASED_USE_TSC_OFFSETING) ?
2917 vmcs12->tsc_offset : 0));
2919 trace_kvm_write_tsc_offset(vcpu->vcpu_id,
2920 vmcs_read64(TSC_OFFSET), offset);
2921 vmcs_write64(TSC_OFFSET, offset);
2926 * nested_vmx_allowed() checks whether a guest should be allowed to use VMX
2927 * instructions and MSRs (i.e., nested VMX). Nested VMX is disabled for
2928 * all guests if the "nested" module option is off, and can also be disabled
2929 * for a single guest by disabling its VMX cpuid bit.
2931 static inline bool nested_vmx_allowed(struct kvm_vcpu *vcpu)
2933 return nested && guest_cpuid_has(vcpu, X86_FEATURE_VMX);
2937 * nested_vmx_setup_ctls_msrs() sets up variables containing the values to be
2938 * returned for the various VMX controls MSRs when nested VMX is enabled.
2939 * The same values should also be used to verify that vmcs12 control fields are
2940 * valid during nested entry from L1 to L2.
2941 * Each of these control msrs has a low and high 32-bit half: A low bit is on
2942 * if the corresponding bit in the (32-bit) control field *must* be on, and a
2943 * bit in the high half is on if the corresponding bit in the control field
2944 * may be on. See also vmx_control_verify().
2946 static void nested_vmx_setup_ctls_msrs(struct nested_vmx_msrs *msrs, bool apicv)
2949 memset(msrs, 0, sizeof(*msrs));
2954 * Note that as a general rule, the high half of the MSRs (bits in
2955 * the control fields which may be 1) should be initialized by the
2956 * intersection of the underlying hardware's MSR (i.e., features which
2957 * can be supported) and the list of features we want to expose -
2958 * because they are known to be properly supported in our code.
2959 * Also, usually, the low half of the MSRs (bits which must be 1) can
2960 * be set to 0, meaning that L1 may turn off any of these bits. The
2961 * reason is that if one of these bits is necessary, it will appear
2962 * in vmcs01 and prepare_vmcs02, when it bitwise-or's the control
2963 * fields of vmcs01 and vmcs02, will turn these bits off - and
2964 * nested_vmx_exit_reflected() will not pass related exits to L1.
2965 * These rules have exceptions below.
2968 /* pin-based controls */
2969 rdmsr(MSR_IA32_VMX_PINBASED_CTLS,
2970 msrs->pinbased_ctls_low,
2971 msrs->pinbased_ctls_high);
2972 msrs->pinbased_ctls_low |=
2973 PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
2974 msrs->pinbased_ctls_high &=
2975 PIN_BASED_EXT_INTR_MASK |
2976 PIN_BASED_NMI_EXITING |
2977 PIN_BASED_VIRTUAL_NMIS |
2978 (apicv ? PIN_BASED_POSTED_INTR : 0);
2979 msrs->pinbased_ctls_high |=
2980 PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR |
2981 PIN_BASED_VMX_PREEMPTION_TIMER;
2984 rdmsr(MSR_IA32_VMX_EXIT_CTLS,
2985 msrs->exit_ctls_low,
2986 msrs->exit_ctls_high);
2987 msrs->exit_ctls_low =
2988 VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR;
2990 msrs->exit_ctls_high &=
2991 #ifdef CONFIG_X86_64
2992 VM_EXIT_HOST_ADDR_SPACE_SIZE |
2994 VM_EXIT_LOAD_IA32_PAT | VM_EXIT_SAVE_IA32_PAT;
2995 msrs->exit_ctls_high |=
2996 VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR |
2997 VM_EXIT_LOAD_IA32_EFER | VM_EXIT_SAVE_IA32_EFER |
2998 VM_EXIT_SAVE_VMX_PREEMPTION_TIMER | VM_EXIT_ACK_INTR_ON_EXIT;
3000 if (kvm_mpx_supported())
3001 msrs->exit_ctls_high |= VM_EXIT_CLEAR_BNDCFGS;
3003 /* We support free control of debug control saving. */
3004 msrs->exit_ctls_low &= ~VM_EXIT_SAVE_DEBUG_CONTROLS;
3006 /* entry controls */
3007 rdmsr(MSR_IA32_VMX_ENTRY_CTLS,
3008 msrs->entry_ctls_low,
3009 msrs->entry_ctls_high);
3010 msrs->entry_ctls_low =
3011 VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR;
3012 msrs->entry_ctls_high &=
3013 #ifdef CONFIG_X86_64
3014 VM_ENTRY_IA32E_MODE |
3016 VM_ENTRY_LOAD_IA32_PAT;
3017 msrs->entry_ctls_high |=
3018 (VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR | VM_ENTRY_LOAD_IA32_EFER);
3019 if (kvm_mpx_supported())
3020 msrs->entry_ctls_high |= VM_ENTRY_LOAD_BNDCFGS;
3022 /* We support free control of debug control loading. */
3023 msrs->entry_ctls_low &= ~VM_ENTRY_LOAD_DEBUG_CONTROLS;
3025 /* cpu-based controls */
3026 rdmsr(MSR_IA32_VMX_PROCBASED_CTLS,
3027 msrs->procbased_ctls_low,
3028 msrs->procbased_ctls_high);
3029 msrs->procbased_ctls_low =
3030 CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
3031 msrs->procbased_ctls_high &=
3032 CPU_BASED_VIRTUAL_INTR_PENDING |
3033 CPU_BASED_VIRTUAL_NMI_PENDING | CPU_BASED_USE_TSC_OFFSETING |
3034 CPU_BASED_HLT_EXITING | CPU_BASED_INVLPG_EXITING |
3035 CPU_BASED_MWAIT_EXITING | CPU_BASED_CR3_LOAD_EXITING |
3036 CPU_BASED_CR3_STORE_EXITING |
3037 #ifdef CONFIG_X86_64
3038 CPU_BASED_CR8_LOAD_EXITING | CPU_BASED_CR8_STORE_EXITING |
3040 CPU_BASED_MOV_DR_EXITING | CPU_BASED_UNCOND_IO_EXITING |
3041 CPU_BASED_USE_IO_BITMAPS | CPU_BASED_MONITOR_TRAP_FLAG |
3042 CPU_BASED_MONITOR_EXITING | CPU_BASED_RDPMC_EXITING |
3043 CPU_BASED_RDTSC_EXITING | CPU_BASED_PAUSE_EXITING |
3044 CPU_BASED_TPR_SHADOW | CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
3046 * We can allow some features even when not supported by the
3047 * hardware. For example, L1 can specify an MSR bitmap - and we
3048 * can use it to avoid exits to L1 - even when L0 runs L2
3049 * without MSR bitmaps.
3051 msrs->procbased_ctls_high |=
3052 CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR |
3053 CPU_BASED_USE_MSR_BITMAPS;
3055 /* We support free control of CR3 access interception. */
3056 msrs->procbased_ctls_low &=
3057 ~(CPU_BASED_CR3_LOAD_EXITING | CPU_BASED_CR3_STORE_EXITING);
3060 * secondary cpu-based controls. Do not include those that
3061 * depend on CPUID bits, they are added later by vmx_cpuid_update.
3063 rdmsr(MSR_IA32_VMX_PROCBASED_CTLS2,
3064 msrs->secondary_ctls_low,
3065 msrs->secondary_ctls_high);
3066 msrs->secondary_ctls_low = 0;
3067 msrs->secondary_ctls_high &=
3068 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
3069 SECONDARY_EXEC_DESC |
3070 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
3071 SECONDARY_EXEC_APIC_REGISTER_VIRT |
3072 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY |
3073 SECONDARY_EXEC_WBINVD_EXITING;
3076 /* nested EPT: emulate EPT also to L1 */
3077 msrs->secondary_ctls_high |=
3078 SECONDARY_EXEC_ENABLE_EPT;
3079 msrs->ept_caps = VMX_EPT_PAGE_WALK_4_BIT |
3080 VMX_EPTP_WB_BIT | VMX_EPT_INVEPT_BIT;
3081 if (cpu_has_vmx_ept_execute_only())
3083 VMX_EPT_EXECUTE_ONLY_BIT;
3084 msrs->ept_caps &= vmx_capability.ept;
3085 msrs->ept_caps |= VMX_EPT_EXTENT_GLOBAL_BIT |
3086 VMX_EPT_EXTENT_CONTEXT_BIT | VMX_EPT_2MB_PAGE_BIT |
3087 VMX_EPT_1GB_PAGE_BIT;
3088 if (enable_ept_ad_bits) {
3089 msrs->secondary_ctls_high |=
3090 SECONDARY_EXEC_ENABLE_PML;
3091 msrs->ept_caps |= VMX_EPT_AD_BIT;
3095 if (cpu_has_vmx_vmfunc()) {
3096 msrs->secondary_ctls_high |=
3097 SECONDARY_EXEC_ENABLE_VMFUNC;
3099 * Advertise EPTP switching unconditionally
3100 * since we emulate it
3103 msrs->vmfunc_controls =
3104 VMX_VMFUNC_EPTP_SWITCHING;
3108 * Old versions of KVM use the single-context version without
3109 * checking for support, so declare that it is supported even
3110 * though it is treated as global context. The alternative is
3111 * not failing the single-context invvpid, and it is worse.
3114 msrs->secondary_ctls_high |=
3115 SECONDARY_EXEC_ENABLE_VPID;
3116 msrs->vpid_caps = VMX_VPID_INVVPID_BIT |
3117 VMX_VPID_EXTENT_SUPPORTED_MASK;
3120 if (enable_unrestricted_guest)
3121 msrs->secondary_ctls_high |=
3122 SECONDARY_EXEC_UNRESTRICTED_GUEST;
3124 /* miscellaneous data */
3125 rdmsr(MSR_IA32_VMX_MISC,
3128 msrs->misc_low &= VMX_MISC_SAVE_EFER_LMA;
3130 VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE |
3131 VMX_MISC_ACTIVITY_HLT;
3132 msrs->misc_high = 0;
3135 * This MSR reports some information about VMX support. We
3136 * should return information about the VMX we emulate for the
3137 * guest, and the VMCS structure we give it - not about the
3138 * VMX support of the underlying hardware.
3142 VMX_BASIC_TRUE_CTLS |
3143 ((u64)VMCS12_SIZE << VMX_BASIC_VMCS_SIZE_SHIFT) |
3144 (VMX_BASIC_MEM_TYPE_WB << VMX_BASIC_MEM_TYPE_SHIFT);
3146 if (cpu_has_vmx_basic_inout())
3147 msrs->basic |= VMX_BASIC_INOUT;
3150 * These MSRs specify bits which the guest must keep fixed on
3151 * while L1 is in VMXON mode (in L1's root mode, or running an L2).
3152 * We picked the standard core2 setting.
3154 #define VMXON_CR0_ALWAYSON (X86_CR0_PE | X86_CR0_PG | X86_CR0_NE)
3155 #define VMXON_CR4_ALWAYSON X86_CR4_VMXE
3156 msrs->cr0_fixed0 = VMXON_CR0_ALWAYSON;
3157 msrs->cr4_fixed0 = VMXON_CR4_ALWAYSON;
3159 /* These MSRs specify bits which the guest must keep fixed off. */
3160 rdmsrl(MSR_IA32_VMX_CR0_FIXED1, msrs->cr0_fixed1);
3161 rdmsrl(MSR_IA32_VMX_CR4_FIXED1, msrs->cr4_fixed1);
3163 /* highest index: VMX_PREEMPTION_TIMER_VALUE */
3164 msrs->vmcs_enum = VMCS12_MAX_FIELD_INDEX << 1;
3168 * if fixed0[i] == 1: val[i] must be 1
3169 * if fixed1[i] == 0: val[i] must be 0
3171 static inline bool fixed_bits_valid(u64 val, u64 fixed0, u64 fixed1)
3173 return ((val & fixed1) | fixed0) == val;
3176 static inline bool vmx_control_verify(u32 control, u32 low, u32 high)
3178 return fixed_bits_valid(control, low, high);
3181 static inline u64 vmx_control_msr(u32 low, u32 high)
3183 return low | ((u64)high << 32);
3186 static bool is_bitwise_subset(u64 superset, u64 subset, u64 mask)
3191 return (superset | subset) == superset;
3194 static int vmx_restore_vmx_basic(struct vcpu_vmx *vmx, u64 data)
3196 const u64 feature_and_reserved =
3197 /* feature (except bit 48; see below) */
3198 BIT_ULL(49) | BIT_ULL(54) | BIT_ULL(55) |
3200 BIT_ULL(31) | GENMASK_ULL(47, 45) | GENMASK_ULL(63, 56);
3201 u64 vmx_basic = vmx->nested.msrs.basic;
3203 if (!is_bitwise_subset(vmx_basic, data, feature_and_reserved))
3207 * KVM does not emulate a version of VMX that constrains physical
3208 * addresses of VMX structures (e.g. VMCS) to 32-bits.
3210 if (data & BIT_ULL(48))
3213 if (vmx_basic_vmcs_revision_id(vmx_basic) !=
3214 vmx_basic_vmcs_revision_id(data))
3217 if (vmx_basic_vmcs_size(vmx_basic) > vmx_basic_vmcs_size(data))
3220 vmx->nested.msrs.basic = data;
3225 vmx_restore_control_msr(struct vcpu_vmx *vmx, u32 msr_index, u64 data)
3230 switch (msr_index) {
3231 case MSR_IA32_VMX_TRUE_PINBASED_CTLS:
3232 lowp = &vmx->nested.msrs.pinbased_ctls_low;
3233 highp = &vmx->nested.msrs.pinbased_ctls_high;
3235 case MSR_IA32_VMX_TRUE_PROCBASED_CTLS:
3236 lowp = &vmx->nested.msrs.procbased_ctls_low;
3237 highp = &vmx->nested.msrs.procbased_ctls_high;
3239 case MSR_IA32_VMX_TRUE_EXIT_CTLS:
3240 lowp = &vmx->nested.msrs.exit_ctls_low;
3241 highp = &vmx->nested.msrs.exit_ctls_high;
3243 case MSR_IA32_VMX_TRUE_ENTRY_CTLS:
3244 lowp = &vmx->nested.msrs.entry_ctls_low;
3245 highp = &vmx->nested.msrs.entry_ctls_high;
3247 case MSR_IA32_VMX_PROCBASED_CTLS2:
3248 lowp = &vmx->nested.msrs.secondary_ctls_low;
3249 highp = &vmx->nested.msrs.secondary_ctls_high;
3255 supported = vmx_control_msr(*lowp, *highp);
3257 /* Check must-be-1 bits are still 1. */
3258 if (!is_bitwise_subset(data, supported, GENMASK_ULL(31, 0)))
3261 /* Check must-be-0 bits are still 0. */
3262 if (!is_bitwise_subset(supported, data, GENMASK_ULL(63, 32)))
3266 *highp = data >> 32;
3270 static int vmx_restore_vmx_misc(struct vcpu_vmx *vmx, u64 data)
3272 const u64 feature_and_reserved_bits =
3274 BIT_ULL(5) | GENMASK_ULL(8, 6) | BIT_ULL(14) | BIT_ULL(15) |
3275 BIT_ULL(28) | BIT_ULL(29) | BIT_ULL(30) |
3277 GENMASK_ULL(13, 9) | BIT_ULL(31);
3280 vmx_misc = vmx_control_msr(vmx->nested.msrs.misc_low,
3281 vmx->nested.msrs.misc_high);
3283 if (!is_bitwise_subset(vmx_misc, data, feature_and_reserved_bits))
3286 if ((vmx->nested.msrs.pinbased_ctls_high &
3287 PIN_BASED_VMX_PREEMPTION_TIMER) &&
3288 vmx_misc_preemption_timer_rate(data) !=
3289 vmx_misc_preemption_timer_rate(vmx_misc))
3292 if (vmx_misc_cr3_count(data) > vmx_misc_cr3_count(vmx_misc))
3295 if (vmx_misc_max_msr(data) > vmx_misc_max_msr(vmx_misc))
3298 if (vmx_misc_mseg_revid(data) != vmx_misc_mseg_revid(vmx_misc))
3301 vmx->nested.msrs.misc_low = data;
3302 vmx->nested.msrs.misc_high = data >> 32;
3306 static int vmx_restore_vmx_ept_vpid_cap(struct vcpu_vmx *vmx, u64 data)
3308 u64 vmx_ept_vpid_cap;
3310 vmx_ept_vpid_cap = vmx_control_msr(vmx->nested.msrs.ept_caps,
3311 vmx->nested.msrs.vpid_caps);
3313 /* Every bit is either reserved or a feature bit. */
3314 if (!is_bitwise_subset(vmx_ept_vpid_cap, data, -1ULL))
3317 vmx->nested.msrs.ept_caps = data;
3318 vmx->nested.msrs.vpid_caps = data >> 32;
3322 static int vmx_restore_fixed0_msr(struct vcpu_vmx *vmx, u32 msr_index, u64 data)
3326 switch (msr_index) {
3327 case MSR_IA32_VMX_CR0_FIXED0:
3328 msr = &vmx->nested.msrs.cr0_fixed0;
3330 case MSR_IA32_VMX_CR4_FIXED0:
3331 msr = &vmx->nested.msrs.cr4_fixed0;
3338 * 1 bits (which indicates bits which "must-be-1" during VMX operation)
3339 * must be 1 in the restored value.
3341 if (!is_bitwise_subset(data, *msr, -1ULL))
3349 * Called when userspace is restoring VMX MSRs.
3351 * Returns 0 on success, non-0 otherwise.
3353 static int vmx_set_vmx_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 data)
3355 struct vcpu_vmx *vmx = to_vmx(vcpu);
3357 switch (msr_index) {
3358 case MSR_IA32_VMX_BASIC:
3359 return vmx_restore_vmx_basic(vmx, data);
3360 case MSR_IA32_VMX_PINBASED_CTLS:
3361 case MSR_IA32_VMX_PROCBASED_CTLS:
3362 case MSR_IA32_VMX_EXIT_CTLS:
3363 case MSR_IA32_VMX_ENTRY_CTLS:
3365 * The "non-true" VMX capability MSRs are generated from the
3366 * "true" MSRs, so we do not support restoring them directly.
3368 * If userspace wants to emulate VMX_BASIC[55]=0, userspace
3369 * should restore the "true" MSRs with the must-be-1 bits
3370 * set according to the SDM Vol 3. A.2 "RESERVED CONTROLS AND
3371 * DEFAULT SETTINGS".
3374 case MSR_IA32_VMX_TRUE_PINBASED_CTLS:
3375 case MSR_IA32_VMX_TRUE_PROCBASED_CTLS:
3376 case MSR_IA32_VMX_TRUE_EXIT_CTLS:
3377 case MSR_IA32_VMX_TRUE_ENTRY_CTLS:
3378 case MSR_IA32_VMX_PROCBASED_CTLS2:
3379 return vmx_restore_control_msr(vmx, msr_index, data);
3380 case MSR_IA32_VMX_MISC:
3381 return vmx_restore_vmx_misc(vmx, data);
3382 case MSR_IA32_VMX_CR0_FIXED0:
3383 case MSR_IA32_VMX_CR4_FIXED0:
3384 return vmx_restore_fixed0_msr(vmx, msr_index, data);
3385 case MSR_IA32_VMX_CR0_FIXED1:
3386 case MSR_IA32_VMX_CR4_FIXED1:
3388 * These MSRs are generated based on the vCPU's CPUID, so we
3389 * do not support restoring them directly.
3392 case MSR_IA32_VMX_EPT_VPID_CAP:
3393 return vmx_restore_vmx_ept_vpid_cap(vmx, data);
3394 case MSR_IA32_VMX_VMCS_ENUM:
3395 vmx->nested.msrs.vmcs_enum = data;
3399 * The rest of the VMX capability MSRs do not support restore.
3405 /* Returns 0 on success, non-0 otherwise. */
3406 static int vmx_get_vmx_msr(struct nested_vmx_msrs *msrs, u32 msr_index, u64 *pdata)
3408 switch (msr_index) {
3409 case MSR_IA32_VMX_BASIC:
3410 *pdata = msrs->basic;
3412 case MSR_IA32_VMX_TRUE_PINBASED_CTLS:
3413 case MSR_IA32_VMX_PINBASED_CTLS:
3414 *pdata = vmx_control_msr(
3415 msrs->pinbased_ctls_low,
3416 msrs->pinbased_ctls_high);
3417 if (msr_index == MSR_IA32_VMX_PINBASED_CTLS)
3418 *pdata |= PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
3420 case MSR_IA32_VMX_TRUE_PROCBASED_CTLS:
3421 case MSR_IA32_VMX_PROCBASED_CTLS:
3422 *pdata = vmx_control_msr(
3423 msrs->procbased_ctls_low,
3424 msrs->procbased_ctls_high);
3425 if (msr_index == MSR_IA32_VMX_PROCBASED_CTLS)
3426 *pdata |= CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
3428 case MSR_IA32_VMX_TRUE_EXIT_CTLS:
3429 case MSR_IA32_VMX_EXIT_CTLS:
3430 *pdata = vmx_control_msr(
3431 msrs->exit_ctls_low,
3432 msrs->exit_ctls_high);
3433 if (msr_index == MSR_IA32_VMX_EXIT_CTLS)
3434 *pdata |= VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR;
3436 case MSR_IA32_VMX_TRUE_ENTRY_CTLS:
3437 case MSR_IA32_VMX_ENTRY_CTLS:
3438 *pdata = vmx_control_msr(
3439 msrs->entry_ctls_low,
3440 msrs->entry_ctls_high);
3441 if (msr_index == MSR_IA32_VMX_ENTRY_CTLS)
3442 *pdata |= VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR;
3444 case MSR_IA32_VMX_MISC:
3445 *pdata = vmx_control_msr(
3449 case MSR_IA32_VMX_CR0_FIXED0:
3450 *pdata = msrs->cr0_fixed0;
3452 case MSR_IA32_VMX_CR0_FIXED1:
3453 *pdata = msrs->cr0_fixed1;
3455 case MSR_IA32_VMX_CR4_FIXED0:
3456 *pdata = msrs->cr4_fixed0;
3458 case MSR_IA32_VMX_CR4_FIXED1:
3459 *pdata = msrs->cr4_fixed1;
3461 case MSR_IA32_VMX_VMCS_ENUM:
3462 *pdata = msrs->vmcs_enum;
3464 case MSR_IA32_VMX_PROCBASED_CTLS2:
3465 *pdata = vmx_control_msr(
3466 msrs->secondary_ctls_low,
3467 msrs->secondary_ctls_high);
3469 case MSR_IA32_VMX_EPT_VPID_CAP:
3470 *pdata = msrs->ept_caps |
3471 ((u64)msrs->vpid_caps << 32);
3473 case MSR_IA32_VMX_VMFUNC:
3474 *pdata = msrs->vmfunc_controls;
3483 static inline bool vmx_feature_control_msr_valid(struct kvm_vcpu *vcpu,
3486 uint64_t valid_bits = to_vmx(vcpu)->msr_ia32_feature_control_valid_bits;
3488 return !(val & ~valid_bits);
3491 static int vmx_get_msr_feature(struct kvm_msr_entry *msr)
3493 switch (msr->index) {
3494 case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC:
3497 return vmx_get_vmx_msr(&vmcs_config.nested, msr->index, &msr->data);
3506 * Reads an msr value (of 'msr_index') into 'pdata'.
3507 * Returns 0 on success, non-0 otherwise.
3508 * Assumes vcpu_load() was already called.
3510 static int vmx_get_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
3512 struct vcpu_vmx *vmx = to_vmx(vcpu);
3513 struct shared_msr_entry *msr;
3515 switch (msr_info->index) {
3516 #ifdef CONFIG_X86_64
3518 msr_info->data = vmcs_readl(GUEST_FS_BASE);
3521 msr_info->data = vmcs_readl(GUEST_GS_BASE);
3523 case MSR_KERNEL_GS_BASE:
3524 vmx_load_host_state(vmx);
3525 msr_info->data = vmx->msr_guest_kernel_gs_base;
3529 return kvm_get_msr_common(vcpu, msr_info);
3530 case MSR_IA32_SPEC_CTRL:
3531 if (!msr_info->host_initiated &&
3532 !guest_cpuid_has(vcpu, X86_FEATURE_SPEC_CTRL))
3535 msr_info->data = to_vmx(vcpu)->spec_ctrl;
3537 case MSR_IA32_ARCH_CAPABILITIES:
3538 if (!msr_info->host_initiated &&
3539 !guest_cpuid_has(vcpu, X86_FEATURE_ARCH_CAPABILITIES))
3541 msr_info->data = to_vmx(vcpu)->arch_capabilities;
3543 case MSR_IA32_SYSENTER_CS:
3544 msr_info->data = vmcs_read32(GUEST_SYSENTER_CS);
3546 case MSR_IA32_SYSENTER_EIP:
3547 msr_info->data = vmcs_readl(GUEST_SYSENTER_EIP);
3549 case MSR_IA32_SYSENTER_ESP:
3550 msr_info->data = vmcs_readl(GUEST_SYSENTER_ESP);
3552 case MSR_IA32_BNDCFGS:
3553 if (!kvm_mpx_supported() ||
3554 (!msr_info->host_initiated &&
3555 !guest_cpuid_has(vcpu, X86_FEATURE_MPX)))
3557 msr_info->data = vmcs_read64(GUEST_BNDCFGS);
3559 case MSR_IA32_MCG_EXT_CTL:
3560 if (!msr_info->host_initiated &&
3561 !(vmx->msr_ia32_feature_control &
3562 FEATURE_CONTROL_LMCE))
3564 msr_info->data = vcpu->arch.mcg_ext_ctl;
3566 case MSR_IA32_FEATURE_CONTROL:
3567 msr_info->data = vmx->msr_ia32_feature_control;
3569 case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC:
3570 if (!nested_vmx_allowed(vcpu))
3572 return vmx_get_vmx_msr(&vmx->nested.msrs, msr_info->index,
3575 if (!vmx_xsaves_supported())
3577 msr_info->data = vcpu->arch.ia32_xss;
3580 if (!msr_info->host_initiated &&
3581 !guest_cpuid_has(vcpu, X86_FEATURE_RDTSCP))
3583 /* Otherwise falls through */
3585 msr = find_msr_entry(vmx, msr_info->index);
3587 msr_info->data = msr->data;
3590 return kvm_get_msr_common(vcpu, msr_info);
3596 static void vmx_leave_nested(struct kvm_vcpu *vcpu);
3599 * Writes msr value into into the appropriate "register".
3600 * Returns 0 on success, non-0 otherwise.
3601 * Assumes vcpu_load() was already called.
3603 static int vmx_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
3605 struct vcpu_vmx *vmx = to_vmx(vcpu);
3606 struct shared_msr_entry *msr;
3608 u32 msr_index = msr_info->index;
3609 u64 data = msr_info->data;
3611 switch (msr_index) {
3613 ret = kvm_set_msr_common(vcpu, msr_info);
3615 #ifdef CONFIG_X86_64
3617 vmx_segment_cache_clear(vmx);
3618 vmcs_writel(GUEST_FS_BASE, data);
3621 vmx_segment_cache_clear(vmx);
3622 vmcs_writel(GUEST_GS_BASE, data);
3624 case MSR_KERNEL_GS_BASE:
3625 vmx_load_host_state(vmx);
3626 vmx->msr_guest_kernel_gs_base = data;
3629 case MSR_IA32_SYSENTER_CS:
3630 vmcs_write32(GUEST_SYSENTER_CS, data);
3632 case MSR_IA32_SYSENTER_EIP:
3633 vmcs_writel(GUEST_SYSENTER_EIP, data);
3635 case MSR_IA32_SYSENTER_ESP:
3636 vmcs_writel(GUEST_SYSENTER_ESP, data);
3638 case MSR_IA32_BNDCFGS:
3639 if (!kvm_mpx_supported() ||
3640 (!msr_info->host_initiated &&
3641 !guest_cpuid_has(vcpu, X86_FEATURE_MPX)))
3643 if (is_noncanonical_address(data & PAGE_MASK, vcpu) ||
3644 (data & MSR_IA32_BNDCFGS_RSVD))
3646 vmcs_write64(GUEST_BNDCFGS, data);
3648 case MSR_IA32_SPEC_CTRL:
3649 if (!msr_info->host_initiated &&
3650 !guest_cpuid_has(vcpu, X86_FEATURE_SPEC_CTRL))
3653 /* The STIBP bit doesn't fault even if it's not advertised */
3654 if (data & ~(SPEC_CTRL_IBRS | SPEC_CTRL_STIBP | SPEC_CTRL_SSBD))
3657 vmx->spec_ctrl = data;
3664 * When it's written (to non-zero) for the first time, pass
3668 * The handling of the MSR bitmap for L2 guests is done in
3669 * nested_vmx_merge_msr_bitmap. We should not touch the
3670 * vmcs02.msr_bitmap here since it gets completely overwritten
3671 * in the merging. We update the vmcs01 here for L1 as well
3672 * since it will end up touching the MSR anyway now.
3674 vmx_disable_intercept_for_msr(vmx->vmcs01.msr_bitmap,
3678 case MSR_IA32_PRED_CMD:
3679 if (!msr_info->host_initiated &&
3680 !guest_cpuid_has(vcpu, X86_FEATURE_SPEC_CTRL))
3683 if (data & ~PRED_CMD_IBPB)
3689 wrmsrl(MSR_IA32_PRED_CMD, PRED_CMD_IBPB);
3693 * When it's written (to non-zero) for the first time, pass
3697 * The handling of the MSR bitmap for L2 guests is done in
3698 * nested_vmx_merge_msr_bitmap. We should not touch the
3699 * vmcs02.msr_bitmap here since it gets completely overwritten
3702 vmx_disable_intercept_for_msr(vmx->vmcs01.msr_bitmap, MSR_IA32_PRED_CMD,
3705 case MSR_IA32_ARCH_CAPABILITIES:
3706 if (!msr_info->host_initiated)
3708 vmx->arch_capabilities = data;
3710 case MSR_IA32_CR_PAT:
3711 if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) {
3712 if (!kvm_mtrr_valid(vcpu, MSR_IA32_CR_PAT, data))
3714 vmcs_write64(GUEST_IA32_PAT, data);
3715 vcpu->arch.pat = data;
3718 ret = kvm_set_msr_common(vcpu, msr_info);
3720 case MSR_IA32_TSC_ADJUST:
3721 ret = kvm_set_msr_common(vcpu, msr_info);
3723 case MSR_IA32_MCG_EXT_CTL:
3724 if ((!msr_info->host_initiated &&
3725 !(to_vmx(vcpu)->msr_ia32_feature_control &
3726 FEATURE_CONTROL_LMCE)) ||
3727 (data & ~MCG_EXT_CTL_LMCE_EN))
3729 vcpu->arch.mcg_ext_ctl = data;
3731 case MSR_IA32_FEATURE_CONTROL:
3732 if (!vmx_feature_control_msr_valid(vcpu, data) ||
3733 (to_vmx(vcpu)->msr_ia32_feature_control &
3734 FEATURE_CONTROL_LOCKED && !msr_info->host_initiated))
3736 vmx->msr_ia32_feature_control = data;
3737 if (msr_info->host_initiated && data == 0)
3738 vmx_leave_nested(vcpu);
3740 case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC:
3741 if (!msr_info->host_initiated)
3742 return 1; /* they are read-only */
3743 if (!nested_vmx_allowed(vcpu))
3745 return vmx_set_vmx_msr(vcpu, msr_index, data);
3747 if (!vmx_xsaves_supported())
3750 * The only supported bit as of Skylake is bit 8, but
3751 * it is not supported on KVM.
3755 vcpu->arch.ia32_xss = data;
3756 if (vcpu->arch.ia32_xss != host_xss)
3757 add_atomic_switch_msr(vmx, MSR_IA32_XSS,
3758 vcpu->arch.ia32_xss, host_xss);
3760 clear_atomic_switch_msr(vmx, MSR_IA32_XSS);
3763 if (!msr_info->host_initiated &&
3764 !guest_cpuid_has(vcpu, X86_FEATURE_RDTSCP))
3766 /* Check reserved bit, higher 32 bits should be zero */
3767 if ((data >> 32) != 0)
3769 /* Otherwise falls through */
3771 msr = find_msr_entry(vmx, msr_index);
3773 u64 old_msr_data = msr->data;
3775 if (msr - vmx->guest_msrs < vmx->save_nmsrs) {
3777 ret = kvm_set_shared_msr(msr->index, msr->data,
3781 msr->data = old_msr_data;
3785 ret = kvm_set_msr_common(vcpu, msr_info);
3791 static void vmx_cache_reg(struct kvm_vcpu *vcpu, enum kvm_reg reg)
3793 __set_bit(reg, (unsigned long *)&vcpu->arch.regs_avail);
3796 vcpu->arch.regs[VCPU_REGS_RSP] = vmcs_readl(GUEST_RSP);
3799 vcpu->arch.regs[VCPU_REGS_RIP] = vmcs_readl(GUEST_RIP);
3801 case VCPU_EXREG_PDPTR:
3803 ept_save_pdptrs(vcpu);
3810 static __init int cpu_has_kvm_support(void)
3812 return cpu_has_vmx();
3815 static __init int vmx_disabled_by_bios(void)
3819 rdmsrl(MSR_IA32_FEATURE_CONTROL, msr);
3820 if (msr & FEATURE_CONTROL_LOCKED) {
3821 /* launched w/ TXT and VMX disabled */
3822 if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX)
3825 /* launched w/o TXT and VMX only enabled w/ TXT */
3826 if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX)
3827 && (msr & FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX)
3828 && !tboot_enabled()) {
3829 printk(KERN_WARNING "kvm: disable TXT in the BIOS or "
3830 "activate TXT before enabling KVM\n");
3833 /* launched w/o TXT and VMX disabled */
3834 if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX)
3835 && !tboot_enabled())
3842 static void kvm_cpu_vmxon(u64 addr)
3844 cr4_set_bits(X86_CR4_VMXE);
3845 intel_pt_handle_vmx(1);
3847 asm volatile (ASM_VMX_VMXON_RAX
3848 : : "a"(&addr), "m"(addr)
3852 static int hardware_enable(void)
3854 int cpu = raw_smp_processor_id();
3855 u64 phys_addr = __pa(per_cpu(vmxarea, cpu));
3858 if (cr4_read_shadow() & X86_CR4_VMXE)
3862 * This can happen if we hot-added a CPU but failed to allocate
3863 * VP assist page for it.
3865 if (static_branch_unlikely(&enable_evmcs) &&
3866 !hv_get_vp_assist_page(cpu))
3869 INIT_LIST_HEAD(&per_cpu(loaded_vmcss_on_cpu, cpu));
3870 INIT_LIST_HEAD(&per_cpu(blocked_vcpu_on_cpu, cpu));
3871 spin_lock_init(&per_cpu(blocked_vcpu_on_cpu_lock, cpu));
3874 * Now we can enable the vmclear operation in kdump
3875 * since the loaded_vmcss_on_cpu list on this cpu
3876 * has been initialized.
3878 * Though the cpu is not in VMX operation now, there
3879 * is no problem to enable the vmclear operation
3880 * for the loaded_vmcss_on_cpu list is empty!
3882 crash_enable_local_vmclear(cpu);
3884 rdmsrl(MSR_IA32_FEATURE_CONTROL, old);
3886 test_bits = FEATURE_CONTROL_LOCKED;
3887 test_bits |= FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
3888 if (tboot_enabled())
3889 test_bits |= FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX;
3891 if ((old & test_bits) != test_bits) {
3892 /* enable and lock */
3893 wrmsrl(MSR_IA32_FEATURE_CONTROL, old | test_bits);
3895 kvm_cpu_vmxon(phys_addr);
3902 static void vmclear_local_loaded_vmcss(void)
3904 int cpu = raw_smp_processor_id();
3905 struct loaded_vmcs *v, *n;
3907 list_for_each_entry_safe(v, n, &per_cpu(loaded_vmcss_on_cpu, cpu),
3908 loaded_vmcss_on_cpu_link)
3909 __loaded_vmcs_clear(v);
3913 /* Just like cpu_vmxoff(), but with the __kvm_handle_fault_on_reboot()
3916 static void kvm_cpu_vmxoff(void)
3918 asm volatile (__ex(ASM_VMX_VMXOFF) : : : "cc");
3920 intel_pt_handle_vmx(0);
3921 cr4_clear_bits(X86_CR4_VMXE);
3924 static void hardware_disable(void)
3926 vmclear_local_loaded_vmcss();
3930 static __init int adjust_vmx_controls(u32 ctl_min, u32 ctl_opt,
3931 u32 msr, u32 *result)
3933 u32 vmx_msr_low, vmx_msr_high;
3934 u32 ctl = ctl_min | ctl_opt;
3936 rdmsr(msr, vmx_msr_low, vmx_msr_high);
3938 ctl &= vmx_msr_high; /* bit == 0 in high word ==> must be zero */
3939 ctl |= vmx_msr_low; /* bit == 1 in low word ==> must be one */
3941 /* Ensure minimum (required) set of control bits are supported. */
3949 static __init bool allow_1_setting(u32 msr, u32 ctl)
3951 u32 vmx_msr_low, vmx_msr_high;
3953 rdmsr(msr, vmx_msr_low, vmx_msr_high);
3954 return vmx_msr_high & ctl;
3957 static __init int setup_vmcs_config(struct vmcs_config *vmcs_conf)
3959 u32 vmx_msr_low, vmx_msr_high;
3960 u32 min, opt, min2, opt2;
3961 u32 _pin_based_exec_control = 0;
3962 u32 _cpu_based_exec_control = 0;
3963 u32 _cpu_based_2nd_exec_control = 0;
3964 u32 _vmexit_control = 0;
3965 u32 _vmentry_control = 0;
3967 memset(vmcs_conf, 0, sizeof(*vmcs_conf));
3968 min = CPU_BASED_HLT_EXITING |
3969 #ifdef CONFIG_X86_64
3970 CPU_BASED_CR8_LOAD_EXITING |
3971 CPU_BASED_CR8_STORE_EXITING |
3973 CPU_BASED_CR3_LOAD_EXITING |
3974 CPU_BASED_CR3_STORE_EXITING |
3975 CPU_BASED_UNCOND_IO_EXITING |
3976 CPU_BASED_MOV_DR_EXITING |
3977 CPU_BASED_USE_TSC_OFFSETING |
3978 CPU_BASED_MWAIT_EXITING |
3979 CPU_BASED_MONITOR_EXITING |
3980 CPU_BASED_INVLPG_EXITING |
3981 CPU_BASED_RDPMC_EXITING;
3983 opt = CPU_BASED_TPR_SHADOW |
3984 CPU_BASED_USE_MSR_BITMAPS |
3985 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
3986 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PROCBASED_CTLS,
3987 &_cpu_based_exec_control) < 0)
3989 #ifdef CONFIG_X86_64
3990 if ((_cpu_based_exec_control & CPU_BASED_TPR_SHADOW))
3991 _cpu_based_exec_control &= ~CPU_BASED_CR8_LOAD_EXITING &
3992 ~CPU_BASED_CR8_STORE_EXITING;
3994 if (_cpu_based_exec_control & CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) {
3996 opt2 = SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
3997 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
3998 SECONDARY_EXEC_WBINVD_EXITING |
3999 SECONDARY_EXEC_ENABLE_VPID |
4000 SECONDARY_EXEC_ENABLE_EPT |
4001 SECONDARY_EXEC_UNRESTRICTED_GUEST |
4002 SECONDARY_EXEC_PAUSE_LOOP_EXITING |
4003 SECONDARY_EXEC_DESC |
4004 SECONDARY_EXEC_RDTSCP |
4005 SECONDARY_EXEC_ENABLE_INVPCID |
4006 SECONDARY_EXEC_APIC_REGISTER_VIRT |
4007 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY |
4008 SECONDARY_EXEC_SHADOW_VMCS |
4009 SECONDARY_EXEC_XSAVES |
4010 SECONDARY_EXEC_RDSEED_EXITING |
4011 SECONDARY_EXEC_RDRAND_EXITING |
4012 SECONDARY_EXEC_ENABLE_PML |
4013 SECONDARY_EXEC_TSC_SCALING |
4014 SECONDARY_EXEC_ENABLE_VMFUNC;
4015 if (adjust_vmx_controls(min2, opt2,
4016 MSR_IA32_VMX_PROCBASED_CTLS2,
4017 &_cpu_based_2nd_exec_control) < 0)
4020 #ifndef CONFIG_X86_64
4021 if (!(_cpu_based_2nd_exec_control &
4022 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES))
4023 _cpu_based_exec_control &= ~CPU_BASED_TPR_SHADOW;
4026 if (!(_cpu_based_exec_control & CPU_BASED_TPR_SHADOW))
4027 _cpu_based_2nd_exec_control &= ~(
4028 SECONDARY_EXEC_APIC_REGISTER_VIRT |
4029 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
4030 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
4032 rdmsr_safe(MSR_IA32_VMX_EPT_VPID_CAP,
4033 &vmx_capability.ept, &vmx_capability.vpid);
4035 if (_cpu_based_2nd_exec_control & SECONDARY_EXEC_ENABLE_EPT) {
4036 /* CR3 accesses and invlpg don't need to cause VM Exits when EPT
4038 _cpu_based_exec_control &= ~(CPU_BASED_CR3_LOAD_EXITING |
4039 CPU_BASED_CR3_STORE_EXITING |
4040 CPU_BASED_INVLPG_EXITING);
4041 } else if (vmx_capability.ept) {
4042 vmx_capability.ept = 0;
4043 pr_warn_once("EPT CAP should not exist if not support "
4044 "1-setting enable EPT VM-execution control\n");
4046 if (!(_cpu_based_2nd_exec_control & SECONDARY_EXEC_ENABLE_VPID) &&
4047 vmx_capability.vpid) {
4048 vmx_capability.vpid = 0;
4049 pr_warn_once("VPID CAP should not exist if not support "
4050 "1-setting enable VPID VM-execution control\n");
4053 min = VM_EXIT_SAVE_DEBUG_CONTROLS | VM_EXIT_ACK_INTR_ON_EXIT;
4054 #ifdef CONFIG_X86_64
4055 min |= VM_EXIT_HOST_ADDR_SPACE_SIZE;
4057 opt = VM_EXIT_SAVE_IA32_PAT | VM_EXIT_LOAD_IA32_PAT |
4058 VM_EXIT_CLEAR_BNDCFGS;
4059 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_EXIT_CTLS,
4060 &_vmexit_control) < 0)
4063 min = PIN_BASED_EXT_INTR_MASK | PIN_BASED_NMI_EXITING;
4064 opt = PIN_BASED_VIRTUAL_NMIS | PIN_BASED_POSTED_INTR |
4065 PIN_BASED_VMX_PREEMPTION_TIMER;
4066 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PINBASED_CTLS,
4067 &_pin_based_exec_control) < 0)
4070 if (cpu_has_broken_vmx_preemption_timer())
4071 _pin_based_exec_control &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
4072 if (!(_cpu_based_2nd_exec_control &
4073 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY))
4074 _pin_based_exec_control &= ~PIN_BASED_POSTED_INTR;
4076 min = VM_ENTRY_LOAD_DEBUG_CONTROLS;
4077 opt = VM_ENTRY_LOAD_IA32_PAT | VM_ENTRY_LOAD_BNDCFGS;
4078 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_ENTRY_CTLS,
4079 &_vmentry_control) < 0)
4082 rdmsr(MSR_IA32_VMX_BASIC, vmx_msr_low, vmx_msr_high);
4084 /* IA-32 SDM Vol 3B: VMCS size is never greater than 4kB. */
4085 if ((vmx_msr_high & 0x1fff) > PAGE_SIZE)
4088 #ifdef CONFIG_X86_64
4089 /* IA-32 SDM Vol 3B: 64-bit CPUs always have VMX_BASIC_MSR[48]==0. */
4090 if (vmx_msr_high & (1u<<16))
4094 /* Require Write-Back (WB) memory type for VMCS accesses. */
4095 if (((vmx_msr_high >> 18) & 15) != 6)
4098 vmcs_conf->size = vmx_msr_high & 0x1fff;
4099 vmcs_conf->order = get_order(vmcs_conf->size);
4100 vmcs_conf->basic_cap = vmx_msr_high & ~0x1fff;
4102 /* KVM supports Enlightened VMCS v1 only */
4103 if (static_branch_unlikely(&enable_evmcs))
4104 vmcs_conf->revision_id = KVM_EVMCS_VERSION;
4106 vmcs_conf->revision_id = vmx_msr_low;
4108 vmcs_conf->pin_based_exec_ctrl = _pin_based_exec_control;
4109 vmcs_conf->cpu_based_exec_ctrl = _cpu_based_exec_control;
4110 vmcs_conf->cpu_based_2nd_exec_ctrl = _cpu_based_2nd_exec_control;
4111 vmcs_conf->vmexit_ctrl = _vmexit_control;
4112 vmcs_conf->vmentry_ctrl = _vmentry_control;
4114 if (static_branch_unlikely(&enable_evmcs))
4115 evmcs_sanitize_exec_ctrls(vmcs_conf);
4117 cpu_has_load_ia32_efer =
4118 allow_1_setting(MSR_IA32_VMX_ENTRY_CTLS,
4119 VM_ENTRY_LOAD_IA32_EFER)
4120 && allow_1_setting(MSR_IA32_VMX_EXIT_CTLS,
4121 VM_EXIT_LOAD_IA32_EFER);
4123 cpu_has_load_perf_global_ctrl =
4124 allow_1_setting(MSR_IA32_VMX_ENTRY_CTLS,
4125 VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL)
4126 && allow_1_setting(MSR_IA32_VMX_EXIT_CTLS,
4127 VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL);
4130 * Some cpus support VM_ENTRY_(LOAD|SAVE)_IA32_PERF_GLOBAL_CTRL
4131 * but due to errata below it can't be used. Workaround is to use
4132 * msr load mechanism to switch IA32_PERF_GLOBAL_CTRL.
4134 * VM Exit May Incorrectly Clear IA32_PERF_GLOBAL_CTRL [34:32]
4139 * BC86,AAY89,BD102 (model 44)
4143 if (cpu_has_load_perf_global_ctrl && boot_cpu_data.x86 == 0x6) {
4144 switch (boot_cpu_data.x86_model) {
4150 cpu_has_load_perf_global_ctrl = false;
4151 printk_once(KERN_WARNING"kvm: VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL "
4152 "does not work properly. Using workaround\n");
4159 if (boot_cpu_has(X86_FEATURE_XSAVES))
4160 rdmsrl(MSR_IA32_XSS, host_xss);
4165 static struct vmcs *alloc_vmcs_cpu(int cpu)
4167 int node = cpu_to_node(cpu);
4171 pages = __alloc_pages_node(node, GFP_KERNEL, vmcs_config.order);
4174 vmcs = page_address(pages);
4175 memset(vmcs, 0, vmcs_config.size);
4176 vmcs->revision_id = vmcs_config.revision_id; /* vmcs revision id */
4180 static void free_vmcs(struct vmcs *vmcs)
4182 free_pages((unsigned long)vmcs, vmcs_config.order);
4186 * Free a VMCS, but before that VMCLEAR it on the CPU where it was last loaded
4188 static void free_loaded_vmcs(struct loaded_vmcs *loaded_vmcs)
4190 if (!loaded_vmcs->vmcs)
4192 loaded_vmcs_clear(loaded_vmcs);
4193 free_vmcs(loaded_vmcs->vmcs);
4194 loaded_vmcs->vmcs = NULL;
4195 if (loaded_vmcs->msr_bitmap)
4196 free_page((unsigned long)loaded_vmcs->msr_bitmap);
4197 WARN_ON(loaded_vmcs->shadow_vmcs != NULL);
4200 static struct vmcs *alloc_vmcs(void)
4202 return alloc_vmcs_cpu(raw_smp_processor_id());
4205 static int alloc_loaded_vmcs(struct loaded_vmcs *loaded_vmcs)
4207 loaded_vmcs->vmcs = alloc_vmcs();
4208 if (!loaded_vmcs->vmcs)
4211 loaded_vmcs->shadow_vmcs = NULL;
4212 loaded_vmcs_init(loaded_vmcs);
4214 if (cpu_has_vmx_msr_bitmap()) {
4215 loaded_vmcs->msr_bitmap = (unsigned long *)__get_free_page(GFP_KERNEL);
4216 if (!loaded_vmcs->msr_bitmap)
4218 memset(loaded_vmcs->msr_bitmap, 0xff, PAGE_SIZE);
4223 free_loaded_vmcs(loaded_vmcs);
4227 static void free_kvm_area(void)
4231 for_each_possible_cpu(cpu) {
4232 free_vmcs(per_cpu(vmxarea, cpu));
4233 per_cpu(vmxarea, cpu) = NULL;
4237 enum vmcs_field_width {
4238 VMCS_FIELD_WIDTH_U16 = 0,
4239 VMCS_FIELD_WIDTH_U64 = 1,
4240 VMCS_FIELD_WIDTH_U32 = 2,
4241 VMCS_FIELD_WIDTH_NATURAL_WIDTH = 3
4244 static inline int vmcs_field_width(unsigned long field)
4246 if (0x1 & field) /* the *_HIGH fields are all 32 bit */
4247 return VMCS_FIELD_WIDTH_U32;
4248 return (field >> 13) & 0x3 ;
4251 static inline int vmcs_field_readonly(unsigned long field)
4253 return (((field >> 10) & 0x3) == 1);
4256 static void init_vmcs_shadow_fields(void)
4260 for (i = j = 0; i < max_shadow_read_only_fields; i++) {
4261 u16 field = shadow_read_only_fields[i];
4262 if (vmcs_field_width(field) == VMCS_FIELD_WIDTH_U64 &&
4263 (i + 1 == max_shadow_read_only_fields ||
4264 shadow_read_only_fields[i + 1] != field + 1))
4265 pr_err("Missing field from shadow_read_only_field %x\n",
4268 clear_bit(field, vmx_vmread_bitmap);
4269 #ifdef CONFIG_X86_64
4274 shadow_read_only_fields[j] = field;
4277 max_shadow_read_only_fields = j;
4279 for (i = j = 0; i < max_shadow_read_write_fields; i++) {
4280 u16 field = shadow_read_write_fields[i];
4281 if (vmcs_field_width(field) == VMCS_FIELD_WIDTH_U64 &&
4282 (i + 1 == max_shadow_read_write_fields ||
4283 shadow_read_write_fields[i + 1] != field + 1))
4284 pr_err("Missing field from shadow_read_write_field %x\n",
4288 * PML and the preemption timer can be emulated, but the
4289 * processor cannot vmwrite to fields that don't exist
4293 case GUEST_PML_INDEX:
4294 if (!cpu_has_vmx_pml())
4297 case VMX_PREEMPTION_TIMER_VALUE:
4298 if (!cpu_has_vmx_preemption_timer())
4301 case GUEST_INTR_STATUS:
4302 if (!cpu_has_vmx_apicv())
4309 clear_bit(field, vmx_vmwrite_bitmap);
4310 clear_bit(field, vmx_vmread_bitmap);
4311 #ifdef CONFIG_X86_64
4316 shadow_read_write_fields[j] = field;
4319 max_shadow_read_write_fields = j;
4322 static __init int alloc_kvm_area(void)
4326 for_each_possible_cpu(cpu) {
4329 vmcs = alloc_vmcs_cpu(cpu);
4335 per_cpu(vmxarea, cpu) = vmcs;
4340 static void fix_pmode_seg(struct kvm_vcpu *vcpu, int seg,
4341 struct kvm_segment *save)
4343 if (!emulate_invalid_guest_state) {
4345 * CS and SS RPL should be equal during guest entry according
4346 * to VMX spec, but in reality it is not always so. Since vcpu
4347 * is in the middle of the transition from real mode to
4348 * protected mode it is safe to assume that RPL 0 is a good
4351 if (seg == VCPU_SREG_CS || seg == VCPU_SREG_SS)
4352 save->selector &= ~SEGMENT_RPL_MASK;
4353 save->dpl = save->selector & SEGMENT_RPL_MASK;
4356 vmx_set_segment(vcpu, save, seg);
4359 static void enter_pmode(struct kvm_vcpu *vcpu)
4361 unsigned long flags;
4362 struct vcpu_vmx *vmx = to_vmx(vcpu);
4365 * Update real mode segment cache. It may be not up-to-date if sement
4366 * register was written while vcpu was in a guest mode.
4368 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_ES], VCPU_SREG_ES);
4369 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_DS], VCPU_SREG_DS);
4370 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_FS], VCPU_SREG_FS);
4371 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_GS], VCPU_SREG_GS);
4372 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_SS], VCPU_SREG_SS);
4373 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_CS], VCPU_SREG_CS);
4375 vmx->rmode.vm86_active = 0;
4377 vmx_segment_cache_clear(vmx);
4379 vmx_set_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_TR], VCPU_SREG_TR);
4381 flags = vmcs_readl(GUEST_RFLAGS);
4382 flags &= RMODE_GUEST_OWNED_EFLAGS_BITS;
4383 flags |= vmx->rmode.save_rflags & ~RMODE_GUEST_OWNED_EFLAGS_BITS;
4384 vmcs_writel(GUEST_RFLAGS, flags);
4386 vmcs_writel(GUEST_CR4, (vmcs_readl(GUEST_CR4) & ~X86_CR4_VME) |
4387 (vmcs_readl(CR4_READ_SHADOW) & X86_CR4_VME));
4389 update_exception_bitmap(vcpu);
4391 fix_pmode_seg(vcpu, VCPU_SREG_CS, &vmx->rmode.segs[VCPU_SREG_CS]);
4392 fix_pmode_seg(vcpu, VCPU_SREG_SS, &vmx->rmode.segs[VCPU_SREG_SS]);
4393 fix_pmode_seg(vcpu, VCPU_SREG_ES, &vmx->rmode.segs[VCPU_SREG_ES]);
4394 fix_pmode_seg(vcpu, VCPU_SREG_DS, &vmx->rmode.segs[VCPU_SREG_DS]);
4395 fix_pmode_seg(vcpu, VCPU_SREG_FS, &vmx->rmode.segs[VCPU_SREG_FS]);
4396 fix_pmode_seg(vcpu, VCPU_SREG_GS, &vmx->rmode.segs[VCPU_SREG_GS]);
4399 static void fix_rmode_seg(int seg, struct kvm_segment *save)
4401 const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
4402 struct kvm_segment var = *save;
4405 if (seg == VCPU_SREG_CS)
4408 if (!emulate_invalid_guest_state) {
4409 var.selector = var.base >> 4;
4410 var.base = var.base & 0xffff0;
4420 if (save->base & 0xf)
4421 printk_once(KERN_WARNING "kvm: segment base is not "
4422 "paragraph aligned when entering "
4423 "protected mode (seg=%d)", seg);
4426 vmcs_write16(sf->selector, var.selector);
4427 vmcs_writel(sf->base, var.base);
4428 vmcs_write32(sf->limit, var.limit);
4429 vmcs_write32(sf->ar_bytes, vmx_segment_access_rights(&var));
4432 static void enter_rmode(struct kvm_vcpu *vcpu)
4434 unsigned long flags;
4435 struct vcpu_vmx *vmx = to_vmx(vcpu);
4436 struct kvm_vmx *kvm_vmx = to_kvm_vmx(vcpu->kvm);
4438 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_TR], VCPU_SREG_TR);
4439 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_ES], VCPU_SREG_ES);
4440 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_DS], VCPU_SREG_DS);
4441 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_FS], VCPU_SREG_FS);
4442 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_GS], VCPU_SREG_GS);
4443 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_SS], VCPU_SREG_SS);
4444 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_CS], VCPU_SREG_CS);
4446 vmx->rmode.vm86_active = 1;
4449 * Very old userspace does not call KVM_SET_TSS_ADDR before entering
4450 * vcpu. Warn the user that an update is overdue.
4452 if (!kvm_vmx->tss_addr)
4453 printk_once(KERN_WARNING "kvm: KVM_SET_TSS_ADDR need to be "
4454 "called before entering vcpu\n");
4456 vmx_segment_cache_clear(vmx);
4458 vmcs_writel(GUEST_TR_BASE, kvm_vmx->tss_addr);
4459 vmcs_write32(GUEST_TR_LIMIT, RMODE_TSS_SIZE - 1);
4460 vmcs_write32(GUEST_TR_AR_BYTES, 0x008b);
4462 flags = vmcs_readl(GUEST_RFLAGS);
4463 vmx->rmode.save_rflags = flags;
4465 flags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM;
4467 vmcs_writel(GUEST_RFLAGS, flags);
4468 vmcs_writel(GUEST_CR4, vmcs_readl(GUEST_CR4) | X86_CR4_VME);
4469 update_exception_bitmap(vcpu);
4471 fix_rmode_seg(VCPU_SREG_SS, &vmx->rmode.segs[VCPU_SREG_SS]);
4472 fix_rmode_seg(VCPU_SREG_CS, &vmx->rmode.segs[VCPU_SREG_CS]);
4473 fix_rmode_seg(VCPU_SREG_ES, &vmx->rmode.segs[VCPU_SREG_ES]);
4474 fix_rmode_seg(VCPU_SREG_DS, &vmx->rmode.segs[VCPU_SREG_DS]);
4475 fix_rmode_seg(VCPU_SREG_GS, &vmx->rmode.segs[VCPU_SREG_GS]);
4476 fix_rmode_seg(VCPU_SREG_FS, &vmx->rmode.segs[VCPU_SREG_FS]);
4478 kvm_mmu_reset_context(vcpu);
4481 static void vmx_set_efer(struct kvm_vcpu *vcpu, u64 efer)
4483 struct vcpu_vmx *vmx = to_vmx(vcpu);
4484 struct shared_msr_entry *msr = find_msr_entry(vmx, MSR_EFER);
4490 * Force kernel_gs_base reloading before EFER changes, as control
4491 * of this msr depends on is_long_mode().
4493 vmx_load_host_state(to_vmx(vcpu));
4494 vcpu->arch.efer = efer;
4495 if (efer & EFER_LMA) {
4496 vm_entry_controls_setbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
4499 vm_entry_controls_clearbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
4501 msr->data = efer & ~EFER_LME;
4506 #ifdef CONFIG_X86_64
4508 static void enter_lmode(struct kvm_vcpu *vcpu)
4512 vmx_segment_cache_clear(to_vmx(vcpu));
4514 guest_tr_ar = vmcs_read32(GUEST_TR_AR_BYTES);
4515 if ((guest_tr_ar & VMX_AR_TYPE_MASK) != VMX_AR_TYPE_BUSY_64_TSS) {
4516 pr_debug_ratelimited("%s: tss fixup for long mode. \n",
4518 vmcs_write32(GUEST_TR_AR_BYTES,
4519 (guest_tr_ar & ~VMX_AR_TYPE_MASK)
4520 | VMX_AR_TYPE_BUSY_64_TSS);
4522 vmx_set_efer(vcpu, vcpu->arch.efer | EFER_LMA);
4525 static void exit_lmode(struct kvm_vcpu *vcpu)
4527 vm_entry_controls_clearbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
4528 vmx_set_efer(vcpu, vcpu->arch.efer & ~EFER_LMA);
4533 static inline void __vmx_flush_tlb(struct kvm_vcpu *vcpu, int vpid,
4534 bool invalidate_gpa)
4536 if (enable_ept && (invalidate_gpa || !enable_vpid)) {
4537 if (!VALID_PAGE(vcpu->arch.mmu.root_hpa))
4539 ept_sync_context(construct_eptp(vcpu, vcpu->arch.mmu.root_hpa));
4541 vpid_sync_context(vpid);
4545 static void vmx_flush_tlb(struct kvm_vcpu *vcpu, bool invalidate_gpa)
4547 __vmx_flush_tlb(vcpu, to_vmx(vcpu)->vpid, invalidate_gpa);
4550 static void vmx_decache_cr0_guest_bits(struct kvm_vcpu *vcpu)
4552 ulong cr0_guest_owned_bits = vcpu->arch.cr0_guest_owned_bits;
4554 vcpu->arch.cr0 &= ~cr0_guest_owned_bits;
4555 vcpu->arch.cr0 |= vmcs_readl(GUEST_CR0) & cr0_guest_owned_bits;
4558 static void vmx_decache_cr3(struct kvm_vcpu *vcpu)
4560 if (enable_unrestricted_guest || (enable_ept && is_paging(vcpu)))
4561 vcpu->arch.cr3 = vmcs_readl(GUEST_CR3);
4562 __set_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail);
4565 static void vmx_decache_cr4_guest_bits(struct kvm_vcpu *vcpu)
4567 ulong cr4_guest_owned_bits = vcpu->arch.cr4_guest_owned_bits;
4569 vcpu->arch.cr4 &= ~cr4_guest_owned_bits;
4570 vcpu->arch.cr4 |= vmcs_readl(GUEST_CR4) & cr4_guest_owned_bits;
4573 static void ept_load_pdptrs(struct kvm_vcpu *vcpu)
4575 struct kvm_mmu *mmu = vcpu->arch.walk_mmu;
4577 if (!test_bit(VCPU_EXREG_PDPTR,
4578 (unsigned long *)&vcpu->arch.regs_dirty))
4581 if (is_paging(vcpu) && is_pae(vcpu) && !is_long_mode(vcpu)) {
4582 vmcs_write64(GUEST_PDPTR0, mmu->pdptrs[0]);
4583 vmcs_write64(GUEST_PDPTR1, mmu->pdptrs[1]);
4584 vmcs_write64(GUEST_PDPTR2, mmu->pdptrs[2]);
4585 vmcs_write64(GUEST_PDPTR3, mmu->pdptrs[3]);
4589 static void ept_save_pdptrs(struct kvm_vcpu *vcpu)
4591 struct kvm_mmu *mmu = vcpu->arch.walk_mmu;
4593 if (is_paging(vcpu) && is_pae(vcpu) && !is_long_mode(vcpu)) {
4594 mmu->pdptrs[0] = vmcs_read64(GUEST_PDPTR0);
4595 mmu->pdptrs[1] = vmcs_read64(GUEST_PDPTR1);
4596 mmu->pdptrs[2] = vmcs_read64(GUEST_PDPTR2);
4597 mmu->pdptrs[3] = vmcs_read64(GUEST_PDPTR3);
4600 __set_bit(VCPU_EXREG_PDPTR,
4601 (unsigned long *)&vcpu->arch.regs_avail);
4602 __set_bit(VCPU_EXREG_PDPTR,
4603 (unsigned long *)&vcpu->arch.regs_dirty);
4606 static bool nested_guest_cr0_valid(struct kvm_vcpu *vcpu, unsigned long val)
4608 u64 fixed0 = to_vmx(vcpu)->nested.msrs.cr0_fixed0;
4609 u64 fixed1 = to_vmx(vcpu)->nested.msrs.cr0_fixed1;
4610 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
4612 if (to_vmx(vcpu)->nested.msrs.secondary_ctls_high &
4613 SECONDARY_EXEC_UNRESTRICTED_GUEST &&
4614 nested_cpu_has2(vmcs12, SECONDARY_EXEC_UNRESTRICTED_GUEST))
4615 fixed0 &= ~(X86_CR0_PE | X86_CR0_PG);
4617 return fixed_bits_valid(val, fixed0, fixed1);
4620 static bool nested_host_cr0_valid(struct kvm_vcpu *vcpu, unsigned long val)
4622 u64 fixed0 = to_vmx(vcpu)->nested.msrs.cr0_fixed0;
4623 u64 fixed1 = to_vmx(vcpu)->nested.msrs.cr0_fixed1;
4625 return fixed_bits_valid(val, fixed0, fixed1);
4628 static bool nested_cr4_valid(struct kvm_vcpu *vcpu, unsigned long val)
4630 u64 fixed0 = to_vmx(vcpu)->nested.msrs.cr4_fixed0;
4631 u64 fixed1 = to_vmx(vcpu)->nested.msrs.cr4_fixed1;
4633 return fixed_bits_valid(val, fixed0, fixed1);
4636 /* No difference in the restrictions on guest and host CR4 in VMX operation. */
4637 #define nested_guest_cr4_valid nested_cr4_valid
4638 #define nested_host_cr4_valid nested_cr4_valid
4640 static int vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4);
4642 static void ept_update_paging_mode_cr0(unsigned long *hw_cr0,
4644 struct kvm_vcpu *vcpu)
4646 if (!test_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail))
4647 vmx_decache_cr3(vcpu);
4648 if (!(cr0 & X86_CR0_PG)) {
4649 /* From paging/starting to nonpaging */
4650 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL,
4651 vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) |
4652 (CPU_BASED_CR3_LOAD_EXITING |
4653 CPU_BASED_CR3_STORE_EXITING));
4654 vcpu->arch.cr0 = cr0;
4655 vmx_set_cr4(vcpu, kvm_read_cr4(vcpu));
4656 } else if (!is_paging(vcpu)) {
4657 /* From nonpaging to paging */
4658 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL,
4659 vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) &
4660 ~(CPU_BASED_CR3_LOAD_EXITING |
4661 CPU_BASED_CR3_STORE_EXITING));
4662 vcpu->arch.cr0 = cr0;
4663 vmx_set_cr4(vcpu, kvm_read_cr4(vcpu));
4666 if (!(cr0 & X86_CR0_WP))
4667 *hw_cr0 &= ~X86_CR0_WP;
4670 static void vmx_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
4672 struct vcpu_vmx *vmx = to_vmx(vcpu);
4673 unsigned long hw_cr0;
4675 hw_cr0 = (cr0 & ~KVM_GUEST_CR0_MASK);
4676 if (enable_unrestricted_guest)
4677 hw_cr0 |= KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST;
4679 hw_cr0 |= KVM_VM_CR0_ALWAYS_ON;
4681 if (vmx->rmode.vm86_active && (cr0 & X86_CR0_PE))
4684 if (!vmx->rmode.vm86_active && !(cr0 & X86_CR0_PE))
4688 #ifdef CONFIG_X86_64
4689 if (vcpu->arch.efer & EFER_LME) {
4690 if (!is_paging(vcpu) && (cr0 & X86_CR0_PG))
4692 if (is_paging(vcpu) && !(cr0 & X86_CR0_PG))
4697 if (enable_ept && !enable_unrestricted_guest)
4698 ept_update_paging_mode_cr0(&hw_cr0, cr0, vcpu);
4700 vmcs_writel(CR0_READ_SHADOW, cr0);
4701 vmcs_writel(GUEST_CR0, hw_cr0);
4702 vcpu->arch.cr0 = cr0;
4704 /* depends on vcpu->arch.cr0 to be set to a new value */
4705 vmx->emulation_required = emulation_required(vcpu);
4708 static int get_ept_level(struct kvm_vcpu *vcpu)
4710 if (cpu_has_vmx_ept_5levels() && (cpuid_maxphyaddr(vcpu) > 48))
4715 static u64 construct_eptp(struct kvm_vcpu *vcpu, unsigned long root_hpa)
4717 u64 eptp = VMX_EPTP_MT_WB;
4719 eptp |= (get_ept_level(vcpu) == 5) ? VMX_EPTP_PWL_5 : VMX_EPTP_PWL_4;
4721 if (enable_ept_ad_bits &&
4722 (!is_guest_mode(vcpu) || nested_ept_ad_enabled(vcpu)))
4723 eptp |= VMX_EPTP_AD_ENABLE_BIT;
4724 eptp |= (root_hpa & PAGE_MASK);
4729 static void vmx_set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3)
4731 unsigned long guest_cr3;
4736 eptp = construct_eptp(vcpu, cr3);
4737 vmcs_write64(EPT_POINTER, eptp);
4738 if (enable_unrestricted_guest || is_paging(vcpu) ||
4739 is_guest_mode(vcpu))
4740 guest_cr3 = kvm_read_cr3(vcpu);
4742 guest_cr3 = to_kvm_vmx(vcpu->kvm)->ept_identity_map_addr;
4743 ept_load_pdptrs(vcpu);
4746 vmx_flush_tlb(vcpu, true);
4747 vmcs_writel(GUEST_CR3, guest_cr3);
4750 static int vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
4753 * Pass through host's Machine Check Enable value to hw_cr4, which
4754 * is in force while we are in guest mode. Do not let guests control
4755 * this bit, even if host CR4.MCE == 0.
4757 unsigned long hw_cr4;
4759 hw_cr4 = (cr4_read_shadow() & X86_CR4_MCE) | (cr4 & ~X86_CR4_MCE);
4760 if (enable_unrestricted_guest)
4761 hw_cr4 |= KVM_VM_CR4_ALWAYS_ON_UNRESTRICTED_GUEST;
4762 else if (to_vmx(vcpu)->rmode.vm86_active)
4763 hw_cr4 |= KVM_RMODE_VM_CR4_ALWAYS_ON;
4765 hw_cr4 |= KVM_PMODE_VM_CR4_ALWAYS_ON;
4767 if (!boot_cpu_has(X86_FEATURE_UMIP) && vmx_umip_emulated()) {
4768 if (cr4 & X86_CR4_UMIP) {
4769 vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL,
4770 SECONDARY_EXEC_DESC);
4771 hw_cr4 &= ~X86_CR4_UMIP;
4772 } else if (!is_guest_mode(vcpu) ||
4773 !nested_cpu_has2(get_vmcs12(vcpu), SECONDARY_EXEC_DESC))
4774 vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL,
4775 SECONDARY_EXEC_DESC);
4778 if (cr4 & X86_CR4_VMXE) {
4780 * To use VMXON (and later other VMX instructions), a guest
4781 * must first be able to turn on cr4.VMXE (see handle_vmon()).
4782 * So basically the check on whether to allow nested VMX
4785 if (!nested_vmx_allowed(vcpu))
4789 if (to_vmx(vcpu)->nested.vmxon && !nested_cr4_valid(vcpu, cr4))
4792 vcpu->arch.cr4 = cr4;
4794 if (!enable_unrestricted_guest) {
4796 if (!is_paging(vcpu)) {
4797 hw_cr4 &= ~X86_CR4_PAE;
4798 hw_cr4 |= X86_CR4_PSE;
4799 } else if (!(cr4 & X86_CR4_PAE)) {
4800 hw_cr4 &= ~X86_CR4_PAE;
4805 * SMEP/SMAP/PKU is disabled if CPU is in non-paging mode in
4806 * hardware. To emulate this behavior, SMEP/SMAP/PKU needs
4807 * to be manually disabled when guest switches to non-paging
4810 * If !enable_unrestricted_guest, the CPU is always running
4811 * with CR0.PG=1 and CR4 needs to be modified.
4812 * If enable_unrestricted_guest, the CPU automatically
4813 * disables SMEP/SMAP/PKU when the guest sets CR0.PG=0.
4815 if (!is_paging(vcpu))
4816 hw_cr4 &= ~(X86_CR4_SMEP | X86_CR4_SMAP | X86_CR4_PKE);
4819 vmcs_writel(CR4_READ_SHADOW, cr4);
4820 vmcs_writel(GUEST_CR4, hw_cr4);
4824 static void vmx_get_segment(struct kvm_vcpu *vcpu,
4825 struct kvm_segment *var, int seg)
4827 struct vcpu_vmx *vmx = to_vmx(vcpu);
4830 if (vmx->rmode.vm86_active && seg != VCPU_SREG_LDTR) {
4831 *var = vmx->rmode.segs[seg];
4832 if (seg == VCPU_SREG_TR
4833 || var->selector == vmx_read_guest_seg_selector(vmx, seg))
4835 var->base = vmx_read_guest_seg_base(vmx, seg);
4836 var->selector = vmx_read_guest_seg_selector(vmx, seg);
4839 var->base = vmx_read_guest_seg_base(vmx, seg);
4840 var->limit = vmx_read_guest_seg_limit(vmx, seg);
4841 var->selector = vmx_read_guest_seg_selector(vmx, seg);
4842 ar = vmx_read_guest_seg_ar(vmx, seg);
4843 var->unusable = (ar >> 16) & 1;
4844 var->type = ar & 15;
4845 var->s = (ar >> 4) & 1;
4846 var->dpl = (ar >> 5) & 3;
4848 * Some userspaces do not preserve unusable property. Since usable
4849 * segment has to be present according to VMX spec we can use present
4850 * property to amend userspace bug by making unusable segment always
4851 * nonpresent. vmx_segment_access_rights() already marks nonpresent
4852 * segment as unusable.
4854 var->present = !var->unusable;
4855 var->avl = (ar >> 12) & 1;
4856 var->l = (ar >> 13) & 1;
4857 var->db = (ar >> 14) & 1;
4858 var->g = (ar >> 15) & 1;
4861 static u64 vmx_get_segment_base(struct kvm_vcpu *vcpu, int seg)
4863 struct kvm_segment s;
4865 if (to_vmx(vcpu)->rmode.vm86_active) {
4866 vmx_get_segment(vcpu, &s, seg);
4869 return vmx_read_guest_seg_base(to_vmx(vcpu), seg);
4872 static int vmx_get_cpl(struct kvm_vcpu *vcpu)
4874 struct vcpu_vmx *vmx = to_vmx(vcpu);
4876 if (unlikely(vmx->rmode.vm86_active))
4879 int ar = vmx_read_guest_seg_ar(vmx, VCPU_SREG_SS);
4880 return VMX_AR_DPL(ar);
4884 static u32 vmx_segment_access_rights(struct kvm_segment *var)
4888 if (var->unusable || !var->present)
4891 ar = var->type & 15;
4892 ar |= (var->s & 1) << 4;
4893 ar |= (var->dpl & 3) << 5;
4894 ar |= (var->present & 1) << 7;
4895 ar |= (var->avl & 1) << 12;
4896 ar |= (var->l & 1) << 13;
4897 ar |= (var->db & 1) << 14;
4898 ar |= (var->g & 1) << 15;
4904 static void vmx_set_segment(struct kvm_vcpu *vcpu,
4905 struct kvm_segment *var, int seg)
4907 struct vcpu_vmx *vmx = to_vmx(vcpu);
4908 const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
4910 vmx_segment_cache_clear(vmx);
4912 if (vmx->rmode.vm86_active && seg != VCPU_SREG_LDTR) {
4913 vmx->rmode.segs[seg] = *var;
4914 if (seg == VCPU_SREG_TR)
4915 vmcs_write16(sf->selector, var->selector);
4917 fix_rmode_seg(seg, &vmx->rmode.segs[seg]);
4921 vmcs_writel(sf->base, var->base);
4922 vmcs_write32(sf->limit, var->limit);
4923 vmcs_write16(sf->selector, var->selector);
4926 * Fix the "Accessed" bit in AR field of segment registers for older
4928 * IA32 arch specifies that at the time of processor reset the
4929 * "Accessed" bit in the AR field of segment registers is 1. And qemu
4930 * is setting it to 0 in the userland code. This causes invalid guest
4931 * state vmexit when "unrestricted guest" mode is turned on.
4932 * Fix for this setup issue in cpu_reset is being pushed in the qemu
4933 * tree. Newer qemu binaries with that qemu fix would not need this
4936 if (enable_unrestricted_guest && (seg != VCPU_SREG_LDTR))
4937 var->type |= 0x1; /* Accessed */
4939 vmcs_write32(sf->ar_bytes, vmx_segment_access_rights(var));
4942 vmx->emulation_required = emulation_required(vcpu);
4945 static void vmx_get_cs_db_l_bits(struct kvm_vcpu *vcpu, int *db, int *l)
4947 u32 ar = vmx_read_guest_seg_ar(to_vmx(vcpu), VCPU_SREG_CS);
4949 *db = (ar >> 14) & 1;
4950 *l = (ar >> 13) & 1;
4953 static void vmx_get_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
4955 dt->size = vmcs_read32(GUEST_IDTR_LIMIT);
4956 dt->address = vmcs_readl(GUEST_IDTR_BASE);
4959 static void vmx_set_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
4961 vmcs_write32(GUEST_IDTR_LIMIT, dt->size);
4962 vmcs_writel(GUEST_IDTR_BASE, dt->address);
4965 static void vmx_get_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
4967 dt->size = vmcs_read32(GUEST_GDTR_LIMIT);
4968 dt->address = vmcs_readl(GUEST_GDTR_BASE);
4971 static void vmx_set_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
4973 vmcs_write32(GUEST_GDTR_LIMIT, dt->size);
4974 vmcs_writel(GUEST_GDTR_BASE, dt->address);
4977 static bool rmode_segment_valid(struct kvm_vcpu *vcpu, int seg)
4979 struct kvm_segment var;
4982 vmx_get_segment(vcpu, &var, seg);
4984 if (seg == VCPU_SREG_CS)
4986 ar = vmx_segment_access_rights(&var);
4988 if (var.base != (var.selector << 4))
4990 if (var.limit != 0xffff)
4998 static bool code_segment_valid(struct kvm_vcpu *vcpu)
5000 struct kvm_segment cs;
5001 unsigned int cs_rpl;
5003 vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
5004 cs_rpl = cs.selector & SEGMENT_RPL_MASK;
5008 if (~cs.type & (VMX_AR_TYPE_CODE_MASK|VMX_AR_TYPE_ACCESSES_MASK))
5012 if (cs.type & VMX_AR_TYPE_WRITEABLE_MASK) {
5013 if (cs.dpl > cs_rpl)
5016 if (cs.dpl != cs_rpl)
5022 /* TODO: Add Reserved field check, this'll require a new member in the kvm_segment_field structure */
5026 static bool stack_segment_valid(struct kvm_vcpu *vcpu)
5028 struct kvm_segment ss;
5029 unsigned int ss_rpl;
5031 vmx_get_segment(vcpu, &ss, VCPU_SREG_SS);
5032 ss_rpl = ss.selector & SEGMENT_RPL_MASK;
5036 if (ss.type != 3 && ss.type != 7)
5040 if (ss.dpl != ss_rpl) /* DPL != RPL */
5048 static bool data_segment_valid(struct kvm_vcpu *vcpu, int seg)
5050 struct kvm_segment var;
5053 vmx_get_segment(vcpu, &var, seg);
5054 rpl = var.selector & SEGMENT_RPL_MASK;
5062 if (~var.type & (VMX_AR_TYPE_CODE_MASK|VMX_AR_TYPE_WRITEABLE_MASK)) {
5063 if (var.dpl < rpl) /* DPL < RPL */
5067 /* TODO: Add other members to kvm_segment_field to allow checking for other access
5073 static bool tr_valid(struct kvm_vcpu *vcpu)
5075 struct kvm_segment tr;
5077 vmx_get_segment(vcpu, &tr, VCPU_SREG_TR);
5081 if (tr.selector & SEGMENT_TI_MASK) /* TI = 1 */
5083 if (tr.type != 3 && tr.type != 11) /* TODO: Check if guest is in IA32e mode */
5091 static bool ldtr_valid(struct kvm_vcpu *vcpu)
5093 struct kvm_segment ldtr;
5095 vmx_get_segment(vcpu, &ldtr, VCPU_SREG_LDTR);
5099 if (ldtr.selector & SEGMENT_TI_MASK) /* TI = 1 */
5109 static bool cs_ss_rpl_check(struct kvm_vcpu *vcpu)
5111 struct kvm_segment cs, ss;
5113 vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
5114 vmx_get_segment(vcpu, &ss, VCPU_SREG_SS);
5116 return ((cs.selector & SEGMENT_RPL_MASK) ==
5117 (ss.selector & SEGMENT_RPL_MASK));
5121 * Check if guest state is valid. Returns true if valid, false if
5123 * We assume that registers are always usable
5125 static bool guest_state_valid(struct kvm_vcpu *vcpu)
5127 if (enable_unrestricted_guest)
5130 /* real mode guest state checks */
5131 if (!is_protmode(vcpu) || (vmx_get_rflags(vcpu) & X86_EFLAGS_VM)) {
5132 if (!rmode_segment_valid(vcpu, VCPU_SREG_CS))
5134 if (!rmode_segment_valid(vcpu, VCPU_SREG_SS))
5136 if (!rmode_segment_valid(vcpu, VCPU_SREG_DS))
5138 if (!rmode_segment_valid(vcpu, VCPU_SREG_ES))
5140 if (!rmode_segment_valid(vcpu, VCPU_SREG_FS))
5142 if (!rmode_segment_valid(vcpu, VCPU_SREG_GS))
5145 /* protected mode guest state checks */
5146 if (!cs_ss_rpl_check(vcpu))
5148 if (!code_segment_valid(vcpu))
5150 if (!stack_segment_valid(vcpu))
5152 if (!data_segment_valid(vcpu, VCPU_SREG_DS))
5154 if (!data_segment_valid(vcpu, VCPU_SREG_ES))
5156 if (!data_segment_valid(vcpu, VCPU_SREG_FS))
5158 if (!data_segment_valid(vcpu, VCPU_SREG_GS))
5160 if (!tr_valid(vcpu))
5162 if (!ldtr_valid(vcpu))
5166 * - Add checks on RIP
5167 * - Add checks on RFLAGS
5173 static bool page_address_valid(struct kvm_vcpu *vcpu, gpa_t gpa)
5175 return PAGE_ALIGNED(gpa) && !(gpa >> cpuid_maxphyaddr(vcpu));
5178 static int init_rmode_tss(struct kvm *kvm)
5184 idx = srcu_read_lock(&kvm->srcu);
5185 fn = to_kvm_vmx(kvm)->tss_addr >> PAGE_SHIFT;
5186 r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE);
5189 data = TSS_BASE_SIZE + TSS_REDIRECTION_SIZE;
5190 r = kvm_write_guest_page(kvm, fn++, &data,
5191 TSS_IOPB_BASE_OFFSET, sizeof(u16));
5194 r = kvm_clear_guest_page(kvm, fn++, 0, PAGE_SIZE);
5197 r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE);
5201 r = kvm_write_guest_page(kvm, fn, &data,
5202 RMODE_TSS_SIZE - 2 * PAGE_SIZE - 1,
5205 srcu_read_unlock(&kvm->srcu, idx);
5209 static int init_rmode_identity_map(struct kvm *kvm)
5211 struct kvm_vmx *kvm_vmx = to_kvm_vmx(kvm);
5213 kvm_pfn_t identity_map_pfn;
5216 /* Protect kvm_vmx->ept_identity_pagetable_done. */
5217 mutex_lock(&kvm->slots_lock);
5219 if (likely(kvm_vmx->ept_identity_pagetable_done))
5222 if (!kvm_vmx->ept_identity_map_addr)
5223 kvm_vmx->ept_identity_map_addr = VMX_EPT_IDENTITY_PAGETABLE_ADDR;
5224 identity_map_pfn = kvm_vmx->ept_identity_map_addr >> PAGE_SHIFT;
5226 r = __x86_set_memory_region(kvm, IDENTITY_PAGETABLE_PRIVATE_MEMSLOT,
5227 kvm_vmx->ept_identity_map_addr, PAGE_SIZE);
5231 idx = srcu_read_lock(&kvm->srcu);
5232 r = kvm_clear_guest_page(kvm, identity_map_pfn, 0, PAGE_SIZE);
5235 /* Set up identity-mapping pagetable for EPT in real mode */
5236 for (i = 0; i < PT32_ENT_PER_PAGE; i++) {
5237 tmp = (i << 22) + (_PAGE_PRESENT | _PAGE_RW | _PAGE_USER |
5238 _PAGE_ACCESSED | _PAGE_DIRTY | _PAGE_PSE);
5239 r = kvm_write_guest_page(kvm, identity_map_pfn,
5240 &tmp, i * sizeof(tmp), sizeof(tmp));
5244 kvm_vmx->ept_identity_pagetable_done = true;
5247 srcu_read_unlock(&kvm->srcu, idx);
5250 mutex_unlock(&kvm->slots_lock);
5254 static void seg_setup(int seg)
5256 const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
5259 vmcs_write16(sf->selector, 0);
5260 vmcs_writel(sf->base, 0);
5261 vmcs_write32(sf->limit, 0xffff);
5263 if (seg == VCPU_SREG_CS)
5264 ar |= 0x08; /* code segment */
5266 vmcs_write32(sf->ar_bytes, ar);
5269 static int alloc_apic_access_page(struct kvm *kvm)
5274 mutex_lock(&kvm->slots_lock);
5275 if (kvm->arch.apic_access_page_done)
5277 r = __x86_set_memory_region(kvm, APIC_ACCESS_PAGE_PRIVATE_MEMSLOT,
5278 APIC_DEFAULT_PHYS_BASE, PAGE_SIZE);
5282 page = gfn_to_page(kvm, APIC_DEFAULT_PHYS_BASE >> PAGE_SHIFT);
5283 if (is_error_page(page)) {
5289 * Do not pin the page in memory, so that memory hot-unplug
5290 * is able to migrate it.
5293 kvm->arch.apic_access_page_done = true;
5295 mutex_unlock(&kvm->slots_lock);
5299 static int allocate_vpid(void)
5305 spin_lock(&vmx_vpid_lock);
5306 vpid = find_first_zero_bit(vmx_vpid_bitmap, VMX_NR_VPIDS);
5307 if (vpid < VMX_NR_VPIDS)
5308 __set_bit(vpid, vmx_vpid_bitmap);
5311 spin_unlock(&vmx_vpid_lock);
5315 static void free_vpid(int vpid)
5317 if (!enable_vpid || vpid == 0)
5319 spin_lock(&vmx_vpid_lock);
5320 __clear_bit(vpid, vmx_vpid_bitmap);
5321 spin_unlock(&vmx_vpid_lock);
5324 static void __always_inline vmx_disable_intercept_for_msr(unsigned long *msr_bitmap,
5327 int f = sizeof(unsigned long);
5329 if (!cpu_has_vmx_msr_bitmap())
5333 * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
5334 * have the write-low and read-high bitmap offsets the wrong way round.
5335 * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
5337 if (msr <= 0x1fff) {
5338 if (type & MSR_TYPE_R)
5340 __clear_bit(msr, msr_bitmap + 0x000 / f);
5342 if (type & MSR_TYPE_W)
5344 __clear_bit(msr, msr_bitmap + 0x800 / f);
5346 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
5348 if (type & MSR_TYPE_R)
5350 __clear_bit(msr, msr_bitmap + 0x400 / f);
5352 if (type & MSR_TYPE_W)
5354 __clear_bit(msr, msr_bitmap + 0xc00 / f);
5359 static void __always_inline vmx_enable_intercept_for_msr(unsigned long *msr_bitmap,
5362 int f = sizeof(unsigned long);
5364 if (!cpu_has_vmx_msr_bitmap())
5368 * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
5369 * have the write-low and read-high bitmap offsets the wrong way round.
5370 * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
5372 if (msr <= 0x1fff) {
5373 if (type & MSR_TYPE_R)
5375 __set_bit(msr, msr_bitmap + 0x000 / f);
5377 if (type & MSR_TYPE_W)
5379 __set_bit(msr, msr_bitmap + 0x800 / f);
5381 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
5383 if (type & MSR_TYPE_R)
5385 __set_bit(msr, msr_bitmap + 0x400 / f);
5387 if (type & MSR_TYPE_W)
5389 __set_bit(msr, msr_bitmap + 0xc00 / f);
5394 static void __always_inline vmx_set_intercept_for_msr(unsigned long *msr_bitmap,
5395 u32 msr, int type, bool value)
5398 vmx_enable_intercept_for_msr(msr_bitmap, msr, type);
5400 vmx_disable_intercept_for_msr(msr_bitmap, msr, type);
5404 * If a msr is allowed by L0, we should check whether it is allowed by L1.
5405 * The corresponding bit will be cleared unless both of L0 and L1 allow it.
5407 static void nested_vmx_disable_intercept_for_msr(unsigned long *msr_bitmap_l1,
5408 unsigned long *msr_bitmap_nested,
5411 int f = sizeof(unsigned long);
5414 * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
5415 * have the write-low and read-high bitmap offsets the wrong way round.
5416 * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
5418 if (msr <= 0x1fff) {
5419 if (type & MSR_TYPE_R &&
5420 !test_bit(msr, msr_bitmap_l1 + 0x000 / f))
5422 __clear_bit(msr, msr_bitmap_nested + 0x000 / f);
5424 if (type & MSR_TYPE_W &&
5425 !test_bit(msr, msr_bitmap_l1 + 0x800 / f))
5427 __clear_bit(msr, msr_bitmap_nested + 0x800 / f);
5429 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
5431 if (type & MSR_TYPE_R &&
5432 !test_bit(msr, msr_bitmap_l1 + 0x400 / f))
5434 __clear_bit(msr, msr_bitmap_nested + 0x400 / f);
5436 if (type & MSR_TYPE_W &&
5437 !test_bit(msr, msr_bitmap_l1 + 0xc00 / f))
5439 __clear_bit(msr, msr_bitmap_nested + 0xc00 / f);
5444 static u8 vmx_msr_bitmap_mode(struct kvm_vcpu *vcpu)
5448 if (cpu_has_secondary_exec_ctrls() &&
5449 (vmcs_read32(SECONDARY_VM_EXEC_CONTROL) &
5450 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE)) {
5451 mode |= MSR_BITMAP_MODE_X2APIC;
5452 if (enable_apicv && kvm_vcpu_apicv_active(vcpu))
5453 mode |= MSR_BITMAP_MODE_X2APIC_APICV;
5456 if (is_long_mode(vcpu))
5457 mode |= MSR_BITMAP_MODE_LM;
5462 #define X2APIC_MSR(r) (APIC_BASE_MSR + ((r) >> 4))
5464 static void vmx_update_msr_bitmap_x2apic(unsigned long *msr_bitmap,
5469 for (msr = 0x800; msr <= 0x8ff; msr += BITS_PER_LONG) {
5470 unsigned word = msr / BITS_PER_LONG;
5471 msr_bitmap[word] = (mode & MSR_BITMAP_MODE_X2APIC_APICV) ? 0 : ~0;
5472 msr_bitmap[word + (0x800 / sizeof(long))] = ~0;
5475 if (mode & MSR_BITMAP_MODE_X2APIC) {
5477 * TPR reads and writes can be virtualized even if virtual interrupt
5478 * delivery is not in use.
5480 vmx_disable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_TASKPRI), MSR_TYPE_RW);
5481 if (mode & MSR_BITMAP_MODE_X2APIC_APICV) {
5482 vmx_enable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_TMCCT), MSR_TYPE_R);
5483 vmx_disable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_EOI), MSR_TYPE_W);
5484 vmx_disable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_SELF_IPI), MSR_TYPE_W);
5489 static void vmx_update_msr_bitmap(struct kvm_vcpu *vcpu)
5491 struct vcpu_vmx *vmx = to_vmx(vcpu);
5492 unsigned long *msr_bitmap = vmx->vmcs01.msr_bitmap;
5493 u8 mode = vmx_msr_bitmap_mode(vcpu);
5494 u8 changed = mode ^ vmx->msr_bitmap_mode;
5499 vmx_set_intercept_for_msr(msr_bitmap, MSR_KERNEL_GS_BASE, MSR_TYPE_RW,
5500 !(mode & MSR_BITMAP_MODE_LM));
5502 if (changed & (MSR_BITMAP_MODE_X2APIC | MSR_BITMAP_MODE_X2APIC_APICV))
5503 vmx_update_msr_bitmap_x2apic(msr_bitmap, mode);
5505 vmx->msr_bitmap_mode = mode;
5508 static bool vmx_get_enable_apicv(struct kvm_vcpu *vcpu)
5510 return enable_apicv;
5513 static void nested_mark_vmcs12_pages_dirty(struct kvm_vcpu *vcpu)
5515 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
5519 * Don't need to mark the APIC access page dirty; it is never
5520 * written to by the CPU during APIC virtualization.
5523 if (nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW)) {
5524 gfn = vmcs12->virtual_apic_page_addr >> PAGE_SHIFT;
5525 kvm_vcpu_mark_page_dirty(vcpu, gfn);
5528 if (nested_cpu_has_posted_intr(vmcs12)) {
5529 gfn = vmcs12->posted_intr_desc_addr >> PAGE_SHIFT;
5530 kvm_vcpu_mark_page_dirty(vcpu, gfn);
5535 static void vmx_complete_nested_posted_interrupt(struct kvm_vcpu *vcpu)
5537 struct vcpu_vmx *vmx = to_vmx(vcpu);
5542 if (!vmx->nested.pi_desc || !vmx->nested.pi_pending)
5545 vmx->nested.pi_pending = false;
5546 if (!pi_test_and_clear_on(vmx->nested.pi_desc))
5549 max_irr = find_last_bit((unsigned long *)vmx->nested.pi_desc->pir, 256);
5550 if (max_irr != 256) {
5551 vapic_page = kmap(vmx->nested.virtual_apic_page);
5552 __kvm_apic_update_irr(vmx->nested.pi_desc->pir,
5553 vapic_page, &max_irr);
5554 kunmap(vmx->nested.virtual_apic_page);
5556 status = vmcs_read16(GUEST_INTR_STATUS);
5557 if ((u8)max_irr > ((u8)status & 0xff)) {
5559 status |= (u8)max_irr;
5560 vmcs_write16(GUEST_INTR_STATUS, status);
5564 nested_mark_vmcs12_pages_dirty(vcpu);
5567 static inline bool kvm_vcpu_trigger_posted_interrupt(struct kvm_vcpu *vcpu,
5571 int pi_vec = nested ? POSTED_INTR_NESTED_VECTOR : POSTED_INTR_VECTOR;
5573 if (vcpu->mode == IN_GUEST_MODE) {
5575 * The vector of interrupt to be delivered to vcpu had
5576 * been set in PIR before this function.
5578 * Following cases will be reached in this block, and
5579 * we always send a notification event in all cases as
5582 * Case 1: vcpu keeps in non-root mode. Sending a
5583 * notification event posts the interrupt to vcpu.
5585 * Case 2: vcpu exits to root mode and is still
5586 * runnable. PIR will be synced to vIRR before the
5587 * next vcpu entry. Sending a notification event in
5588 * this case has no effect, as vcpu is not in root
5591 * Case 3: vcpu exits to root mode and is blocked.
5592 * vcpu_block() has already synced PIR to vIRR and
5593 * never blocks vcpu if vIRR is not cleared. Therefore,
5594 * a blocked vcpu here does not wait for any requested
5595 * interrupts in PIR, and sending a notification event
5596 * which has no effect is safe here.
5599 apic->send_IPI_mask(get_cpu_mask(vcpu->cpu), pi_vec);
5606 static int vmx_deliver_nested_posted_interrupt(struct kvm_vcpu *vcpu,
5609 struct vcpu_vmx *vmx = to_vmx(vcpu);
5611 if (is_guest_mode(vcpu) &&
5612 vector == vmx->nested.posted_intr_nv) {
5614 * If a posted intr is not recognized by hardware,
5615 * we will accomplish it in the next vmentry.
5617 vmx->nested.pi_pending = true;
5618 kvm_make_request(KVM_REQ_EVENT, vcpu);
5619 /* the PIR and ON have been set by L1. */
5620 if (!kvm_vcpu_trigger_posted_interrupt(vcpu, true))
5621 kvm_vcpu_kick(vcpu);
5627 * Send interrupt to vcpu via posted interrupt way.
5628 * 1. If target vcpu is running(non-root mode), send posted interrupt
5629 * notification to vcpu and hardware will sync PIR to vIRR atomically.
5630 * 2. If target vcpu isn't running(root mode), kick it to pick up the
5631 * interrupt from PIR in next vmentry.
5633 static void vmx_deliver_posted_interrupt(struct kvm_vcpu *vcpu, int vector)
5635 struct vcpu_vmx *vmx = to_vmx(vcpu);
5638 r = vmx_deliver_nested_posted_interrupt(vcpu, vector);
5642 if (pi_test_and_set_pir(vector, &vmx->pi_desc))
5645 /* If a previous notification has sent the IPI, nothing to do. */
5646 if (pi_test_and_set_on(&vmx->pi_desc))
5649 if (!kvm_vcpu_trigger_posted_interrupt(vcpu, false))
5650 kvm_vcpu_kick(vcpu);
5654 * Set up the vmcs's constant host-state fields, i.e., host-state fields that
5655 * will not change in the lifetime of the guest.
5656 * Note that host-state that does change is set elsewhere. E.g., host-state
5657 * that is set differently for each CPU is set in vmx_vcpu_load(), not here.
5659 static void vmx_set_constant_host_state(struct vcpu_vmx *vmx)
5664 unsigned long cr0, cr3, cr4;
5667 WARN_ON(cr0 & X86_CR0_TS);
5668 vmcs_writel(HOST_CR0, cr0); /* 22.2.3 */
5671 * Save the most likely value for this task's CR3 in the VMCS.
5672 * We can't use __get_current_cr3_fast() because we're not atomic.
5675 vmcs_writel(HOST_CR3, cr3); /* 22.2.3 FIXME: shadow tables */
5676 vmx->loaded_vmcs->vmcs_host_cr3 = cr3;
5678 /* Save the most likely value for this task's CR4 in the VMCS. */
5679 cr4 = cr4_read_shadow();
5680 vmcs_writel(HOST_CR4, cr4); /* 22.2.3, 22.2.5 */
5681 vmx->loaded_vmcs->vmcs_host_cr4 = cr4;
5683 vmcs_write16(HOST_CS_SELECTOR, __KERNEL_CS); /* 22.2.4 */
5684 #ifdef CONFIG_X86_64
5686 * Load null selectors, so we can avoid reloading them in
5687 * __vmx_load_host_state(), in case userspace uses the null selectors
5688 * too (the expected case).
5690 vmcs_write16(HOST_DS_SELECTOR, 0);
5691 vmcs_write16(HOST_ES_SELECTOR, 0);
5693 vmcs_write16(HOST_DS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
5694 vmcs_write16(HOST_ES_SELECTOR, __KERNEL_DS); /* 22.2.4 */
5696 vmcs_write16(HOST_SS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
5697 vmcs_write16(HOST_TR_SELECTOR, GDT_ENTRY_TSS*8); /* 22.2.4 */
5700 vmcs_writel(HOST_IDTR_BASE, dt.address); /* 22.2.4 */
5701 vmx->host_idt_base = dt.address;
5703 vmcs_writel(HOST_RIP, vmx_return); /* 22.2.5 */
5705 rdmsr(MSR_IA32_SYSENTER_CS, low32, high32);
5706 vmcs_write32(HOST_IA32_SYSENTER_CS, low32);
5707 rdmsrl(MSR_IA32_SYSENTER_EIP, tmpl);
5708 vmcs_writel(HOST_IA32_SYSENTER_EIP, tmpl); /* 22.2.3 */
5710 if (vmcs_config.vmexit_ctrl & VM_EXIT_LOAD_IA32_PAT) {
5711 rdmsr(MSR_IA32_CR_PAT, low32, high32);
5712 vmcs_write64(HOST_IA32_PAT, low32 | ((u64) high32 << 32));
5716 static void set_cr4_guest_host_mask(struct vcpu_vmx *vmx)
5718 vmx->vcpu.arch.cr4_guest_owned_bits = KVM_CR4_GUEST_OWNED_BITS;
5720 vmx->vcpu.arch.cr4_guest_owned_bits |= X86_CR4_PGE;
5721 if (is_guest_mode(&vmx->vcpu))
5722 vmx->vcpu.arch.cr4_guest_owned_bits &=
5723 ~get_vmcs12(&vmx->vcpu)->cr4_guest_host_mask;
5724 vmcs_writel(CR4_GUEST_HOST_MASK, ~vmx->vcpu.arch.cr4_guest_owned_bits);
5727 static u32 vmx_pin_based_exec_ctrl(struct vcpu_vmx *vmx)
5729 u32 pin_based_exec_ctrl = vmcs_config.pin_based_exec_ctrl;
5731 if (!kvm_vcpu_apicv_active(&vmx->vcpu))
5732 pin_based_exec_ctrl &= ~PIN_BASED_POSTED_INTR;
5735 pin_based_exec_ctrl &= ~PIN_BASED_VIRTUAL_NMIS;
5737 /* Enable the preemption timer dynamically */
5738 pin_based_exec_ctrl &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
5739 return pin_based_exec_ctrl;
5742 static void vmx_refresh_apicv_exec_ctrl(struct kvm_vcpu *vcpu)
5744 struct vcpu_vmx *vmx = to_vmx(vcpu);
5746 vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, vmx_pin_based_exec_ctrl(vmx));
5747 if (cpu_has_secondary_exec_ctrls()) {
5748 if (kvm_vcpu_apicv_active(vcpu))
5749 vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL,
5750 SECONDARY_EXEC_APIC_REGISTER_VIRT |
5751 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
5753 vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL,
5754 SECONDARY_EXEC_APIC_REGISTER_VIRT |
5755 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
5758 if (cpu_has_vmx_msr_bitmap())
5759 vmx_update_msr_bitmap(vcpu);
5762 static u32 vmx_exec_control(struct vcpu_vmx *vmx)
5764 u32 exec_control = vmcs_config.cpu_based_exec_ctrl;
5766 if (vmx->vcpu.arch.switch_db_regs & KVM_DEBUGREG_WONT_EXIT)
5767 exec_control &= ~CPU_BASED_MOV_DR_EXITING;
5769 if (!cpu_need_tpr_shadow(&vmx->vcpu)) {
5770 exec_control &= ~CPU_BASED_TPR_SHADOW;
5771 #ifdef CONFIG_X86_64
5772 exec_control |= CPU_BASED_CR8_STORE_EXITING |
5773 CPU_BASED_CR8_LOAD_EXITING;
5777 exec_control |= CPU_BASED_CR3_STORE_EXITING |
5778 CPU_BASED_CR3_LOAD_EXITING |
5779 CPU_BASED_INVLPG_EXITING;
5780 if (kvm_mwait_in_guest(vmx->vcpu.kvm))
5781 exec_control &= ~(CPU_BASED_MWAIT_EXITING |
5782 CPU_BASED_MONITOR_EXITING);
5783 if (kvm_hlt_in_guest(vmx->vcpu.kvm))
5784 exec_control &= ~CPU_BASED_HLT_EXITING;
5785 return exec_control;
5788 static bool vmx_rdrand_supported(void)
5790 return vmcs_config.cpu_based_2nd_exec_ctrl &
5791 SECONDARY_EXEC_RDRAND_EXITING;
5794 static bool vmx_rdseed_supported(void)
5796 return vmcs_config.cpu_based_2nd_exec_ctrl &
5797 SECONDARY_EXEC_RDSEED_EXITING;
5800 static void vmx_compute_secondary_exec_control(struct vcpu_vmx *vmx)
5802 struct kvm_vcpu *vcpu = &vmx->vcpu;
5804 u32 exec_control = vmcs_config.cpu_based_2nd_exec_ctrl;
5806 if (!cpu_need_virtualize_apic_accesses(vcpu))
5807 exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
5809 exec_control &= ~SECONDARY_EXEC_ENABLE_VPID;
5811 exec_control &= ~SECONDARY_EXEC_ENABLE_EPT;
5812 enable_unrestricted_guest = 0;
5813 /* Enable INVPCID for non-ept guests may cause performance regression. */
5814 exec_control &= ~SECONDARY_EXEC_ENABLE_INVPCID;
5816 if (!enable_unrestricted_guest)
5817 exec_control &= ~SECONDARY_EXEC_UNRESTRICTED_GUEST;
5818 if (kvm_pause_in_guest(vmx->vcpu.kvm))
5819 exec_control &= ~SECONDARY_EXEC_PAUSE_LOOP_EXITING;
5820 if (!kvm_vcpu_apicv_active(vcpu))
5821 exec_control &= ~(SECONDARY_EXEC_APIC_REGISTER_VIRT |
5822 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
5823 exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
5825 /* SECONDARY_EXEC_DESC is enabled/disabled on writes to CR4.UMIP,
5826 * in vmx_set_cr4. */
5827 exec_control &= ~SECONDARY_EXEC_DESC;
5829 /* SECONDARY_EXEC_SHADOW_VMCS is enabled when L1 executes VMPTRLD
5831 We can NOT enable shadow_vmcs here because we don't have yet
5834 exec_control &= ~SECONDARY_EXEC_SHADOW_VMCS;
5837 exec_control &= ~SECONDARY_EXEC_ENABLE_PML;
5839 if (vmx_xsaves_supported()) {
5840 /* Exposing XSAVES only when XSAVE is exposed */
5841 bool xsaves_enabled =
5842 guest_cpuid_has(vcpu, X86_FEATURE_XSAVE) &&
5843 guest_cpuid_has(vcpu, X86_FEATURE_XSAVES);
5845 if (!xsaves_enabled)
5846 exec_control &= ~SECONDARY_EXEC_XSAVES;
5850 vmx->nested.msrs.secondary_ctls_high |=
5851 SECONDARY_EXEC_XSAVES;
5853 vmx->nested.msrs.secondary_ctls_high &=
5854 ~SECONDARY_EXEC_XSAVES;
5858 if (vmx_rdtscp_supported()) {
5859 bool rdtscp_enabled = guest_cpuid_has(vcpu, X86_FEATURE_RDTSCP);
5860 if (!rdtscp_enabled)
5861 exec_control &= ~SECONDARY_EXEC_RDTSCP;
5865 vmx->nested.msrs.secondary_ctls_high |=
5866 SECONDARY_EXEC_RDTSCP;
5868 vmx->nested.msrs.secondary_ctls_high &=
5869 ~SECONDARY_EXEC_RDTSCP;
5873 if (vmx_invpcid_supported()) {
5874 /* Exposing INVPCID only when PCID is exposed */
5875 bool invpcid_enabled =
5876 guest_cpuid_has(vcpu, X86_FEATURE_INVPCID) &&
5877 guest_cpuid_has(vcpu, X86_FEATURE_PCID);
5879 if (!invpcid_enabled) {
5880 exec_control &= ~SECONDARY_EXEC_ENABLE_INVPCID;
5881 guest_cpuid_clear(vcpu, X86_FEATURE_INVPCID);
5885 if (invpcid_enabled)
5886 vmx->nested.msrs.secondary_ctls_high |=
5887 SECONDARY_EXEC_ENABLE_INVPCID;
5889 vmx->nested.msrs.secondary_ctls_high &=
5890 ~SECONDARY_EXEC_ENABLE_INVPCID;
5894 if (vmx_rdrand_supported()) {
5895 bool rdrand_enabled = guest_cpuid_has(vcpu, X86_FEATURE_RDRAND);
5897 exec_control &= ~SECONDARY_EXEC_RDRAND_EXITING;
5901 vmx->nested.msrs.secondary_ctls_high |=
5902 SECONDARY_EXEC_RDRAND_EXITING;
5904 vmx->nested.msrs.secondary_ctls_high &=
5905 ~SECONDARY_EXEC_RDRAND_EXITING;
5909 if (vmx_rdseed_supported()) {
5910 bool rdseed_enabled = guest_cpuid_has(vcpu, X86_FEATURE_RDSEED);
5912 exec_control &= ~SECONDARY_EXEC_RDSEED_EXITING;
5916 vmx->nested.msrs.secondary_ctls_high |=
5917 SECONDARY_EXEC_RDSEED_EXITING;
5919 vmx->nested.msrs.secondary_ctls_high &=
5920 ~SECONDARY_EXEC_RDSEED_EXITING;
5924 vmx->secondary_exec_control = exec_control;
5927 static void ept_set_mmio_spte_mask(void)
5930 * EPT Misconfigurations can be generated if the value of bits 2:0
5931 * of an EPT paging-structure entry is 110b (write/execute).
5933 kvm_mmu_set_mmio_spte_mask(VMX_EPT_RWX_MASK,
5934 VMX_EPT_MISCONFIG_WX_VALUE);
5937 #define VMX_XSS_EXIT_BITMAP 0
5939 * Sets up the vmcs for emulated real mode.
5941 static void vmx_vcpu_setup(struct vcpu_vmx *vmx)
5943 #ifdef CONFIG_X86_64
5948 if (enable_shadow_vmcs) {
5949 vmcs_write64(VMREAD_BITMAP, __pa(vmx_vmread_bitmap));
5950 vmcs_write64(VMWRITE_BITMAP, __pa(vmx_vmwrite_bitmap));
5952 if (cpu_has_vmx_msr_bitmap())
5953 vmcs_write64(MSR_BITMAP, __pa(vmx->vmcs01.msr_bitmap));
5955 vmcs_write64(VMCS_LINK_POINTER, -1ull); /* 22.3.1.5 */
5958 vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, vmx_pin_based_exec_ctrl(vmx));
5959 vmx->hv_deadline_tsc = -1;
5961 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, vmx_exec_control(vmx));
5963 if (cpu_has_secondary_exec_ctrls()) {
5964 vmx_compute_secondary_exec_control(vmx);
5965 vmcs_write32(SECONDARY_VM_EXEC_CONTROL,
5966 vmx->secondary_exec_control);
5969 if (kvm_vcpu_apicv_active(&vmx->vcpu)) {
5970 vmcs_write64(EOI_EXIT_BITMAP0, 0);
5971 vmcs_write64(EOI_EXIT_BITMAP1, 0);
5972 vmcs_write64(EOI_EXIT_BITMAP2, 0);
5973 vmcs_write64(EOI_EXIT_BITMAP3, 0);
5975 vmcs_write16(GUEST_INTR_STATUS, 0);
5977 vmcs_write16(POSTED_INTR_NV, POSTED_INTR_VECTOR);
5978 vmcs_write64(POSTED_INTR_DESC_ADDR, __pa((&vmx->pi_desc)));
5981 if (!kvm_pause_in_guest(vmx->vcpu.kvm)) {
5982 vmcs_write32(PLE_GAP, ple_gap);
5983 vmx->ple_window = ple_window;
5984 vmx->ple_window_dirty = true;
5987 vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK, 0);
5988 vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH, 0);
5989 vmcs_write32(CR3_TARGET_COUNT, 0); /* 22.2.1 */
5991 vmcs_write16(HOST_FS_SELECTOR, 0); /* 22.2.4 */
5992 vmcs_write16(HOST_GS_SELECTOR, 0); /* 22.2.4 */
5993 vmx_set_constant_host_state(vmx);
5994 #ifdef CONFIG_X86_64
5995 rdmsrl(MSR_FS_BASE, a);
5996 vmcs_writel(HOST_FS_BASE, a); /* 22.2.4 */
5997 rdmsrl(MSR_GS_BASE, a);
5998 vmcs_writel(HOST_GS_BASE, a); /* 22.2.4 */
6000 vmcs_writel(HOST_FS_BASE, 0); /* 22.2.4 */
6001 vmcs_writel(HOST_GS_BASE, 0); /* 22.2.4 */
6004 if (cpu_has_vmx_vmfunc())
6005 vmcs_write64(VM_FUNCTION_CONTROL, 0);
6007 vmcs_write32(VM_EXIT_MSR_STORE_COUNT, 0);
6008 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, 0);
6009 vmcs_write64(VM_EXIT_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.host));
6010 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, 0);
6011 vmcs_write64(VM_ENTRY_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.guest));
6013 if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT)
6014 vmcs_write64(GUEST_IA32_PAT, vmx->vcpu.arch.pat);
6016 for (i = 0; i < ARRAY_SIZE(vmx_msr_index); ++i) {
6017 u32 index = vmx_msr_index[i];
6018 u32 data_low, data_high;
6021 if (rdmsr_safe(index, &data_low, &data_high) < 0)
6023 if (wrmsr_safe(index, data_low, data_high) < 0)
6025 vmx->guest_msrs[j].index = i;
6026 vmx->guest_msrs[j].data = 0;
6027 vmx->guest_msrs[j].mask = -1ull;
6031 if (boot_cpu_has(X86_FEATURE_ARCH_CAPABILITIES))
6032 rdmsrl(MSR_IA32_ARCH_CAPABILITIES, vmx->arch_capabilities);
6034 vm_exit_controls_init(vmx, vmcs_config.vmexit_ctrl);
6036 /* 22.2.1, 20.8.1 */
6037 vm_entry_controls_init(vmx, vmcs_config.vmentry_ctrl);
6039 vmx->vcpu.arch.cr0_guest_owned_bits = X86_CR0_TS;
6040 vmcs_writel(CR0_GUEST_HOST_MASK, ~X86_CR0_TS);
6042 set_cr4_guest_host_mask(vmx);
6044 if (vmx_xsaves_supported())
6045 vmcs_write64(XSS_EXIT_BITMAP, VMX_XSS_EXIT_BITMAP);
6048 ASSERT(vmx->pml_pg);
6049 vmcs_write64(PML_ADDRESS, page_to_phys(vmx->pml_pg));
6050 vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1);
6054 static void vmx_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event)
6056 struct vcpu_vmx *vmx = to_vmx(vcpu);
6057 struct msr_data apic_base_msr;
6060 vmx->rmode.vm86_active = 0;
6063 vcpu->arch.microcode_version = 0x100000000ULL;
6064 vmx->vcpu.arch.regs[VCPU_REGS_RDX] = get_rdx_init_val();
6065 kvm_set_cr8(vcpu, 0);
6068 apic_base_msr.data = APIC_DEFAULT_PHYS_BASE |
6069 MSR_IA32_APICBASE_ENABLE;
6070 if (kvm_vcpu_is_reset_bsp(vcpu))
6071 apic_base_msr.data |= MSR_IA32_APICBASE_BSP;
6072 apic_base_msr.host_initiated = true;
6073 kvm_set_apic_base(vcpu, &apic_base_msr);
6076 vmx_segment_cache_clear(vmx);
6078 seg_setup(VCPU_SREG_CS);
6079 vmcs_write16(GUEST_CS_SELECTOR, 0xf000);
6080 vmcs_writel(GUEST_CS_BASE, 0xffff0000ul);
6082 seg_setup(VCPU_SREG_DS);
6083 seg_setup(VCPU_SREG_ES);
6084 seg_setup(VCPU_SREG_FS);
6085 seg_setup(VCPU_SREG_GS);
6086 seg_setup(VCPU_SREG_SS);
6088 vmcs_write16(GUEST_TR_SELECTOR, 0);
6089 vmcs_writel(GUEST_TR_BASE, 0);
6090 vmcs_write32(GUEST_TR_LIMIT, 0xffff);
6091 vmcs_write32(GUEST_TR_AR_BYTES, 0x008b);
6093 vmcs_write16(GUEST_LDTR_SELECTOR, 0);
6094 vmcs_writel(GUEST_LDTR_BASE, 0);
6095 vmcs_write32(GUEST_LDTR_LIMIT, 0xffff);
6096 vmcs_write32(GUEST_LDTR_AR_BYTES, 0x00082);
6099 vmcs_write32(GUEST_SYSENTER_CS, 0);
6100 vmcs_writel(GUEST_SYSENTER_ESP, 0);
6101 vmcs_writel(GUEST_SYSENTER_EIP, 0);
6102 vmcs_write64(GUEST_IA32_DEBUGCTL, 0);
6105 kvm_set_rflags(vcpu, X86_EFLAGS_FIXED);
6106 kvm_rip_write(vcpu, 0xfff0);
6108 vmcs_writel(GUEST_GDTR_BASE, 0);
6109 vmcs_write32(GUEST_GDTR_LIMIT, 0xffff);
6111 vmcs_writel(GUEST_IDTR_BASE, 0);
6112 vmcs_write32(GUEST_IDTR_LIMIT, 0xffff);
6114 vmcs_write32(GUEST_ACTIVITY_STATE, GUEST_ACTIVITY_ACTIVE);
6115 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, 0);
6116 vmcs_writel(GUEST_PENDING_DBG_EXCEPTIONS, 0);
6117 if (kvm_mpx_supported())
6118 vmcs_write64(GUEST_BNDCFGS, 0);
6122 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0); /* 22.2.1 */
6124 if (cpu_has_vmx_tpr_shadow() && !init_event) {
6125 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, 0);
6126 if (cpu_need_tpr_shadow(vcpu))
6127 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR,
6128 __pa(vcpu->arch.apic->regs));
6129 vmcs_write32(TPR_THRESHOLD, 0);
6132 kvm_make_request(KVM_REQ_APIC_PAGE_RELOAD, vcpu);
6135 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid);
6137 cr0 = X86_CR0_NW | X86_CR0_CD | X86_CR0_ET;
6138 vmx->vcpu.arch.cr0 = cr0;
6139 vmx_set_cr0(vcpu, cr0); /* enter rmode */
6140 vmx_set_cr4(vcpu, 0);
6141 vmx_set_efer(vcpu, 0);
6143 update_exception_bitmap(vcpu);
6145 vpid_sync_context(vmx->vpid);
6147 vmx_clear_hlt(vcpu);
6151 * In nested virtualization, check if L1 asked to exit on external interrupts.
6152 * For most existing hypervisors, this will always return true.
6154 static bool nested_exit_on_intr(struct kvm_vcpu *vcpu)
6156 return get_vmcs12(vcpu)->pin_based_vm_exec_control &
6157 PIN_BASED_EXT_INTR_MASK;
6161 * In nested virtualization, check if L1 has set
6162 * VM_EXIT_ACK_INTR_ON_EXIT
6164 static bool nested_exit_intr_ack_set(struct kvm_vcpu *vcpu)
6166 return get_vmcs12(vcpu)->vm_exit_controls &
6167 VM_EXIT_ACK_INTR_ON_EXIT;
6170 static bool nested_exit_on_nmi(struct kvm_vcpu *vcpu)
6172 return nested_cpu_has_nmi_exiting(get_vmcs12(vcpu));
6175 static void enable_irq_window(struct kvm_vcpu *vcpu)
6177 vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL,
6178 CPU_BASED_VIRTUAL_INTR_PENDING);
6181 static void enable_nmi_window(struct kvm_vcpu *vcpu)
6184 vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & GUEST_INTR_STATE_STI) {
6185 enable_irq_window(vcpu);
6189 vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL,
6190 CPU_BASED_VIRTUAL_NMI_PENDING);
6193 static void vmx_inject_irq(struct kvm_vcpu *vcpu)
6195 struct vcpu_vmx *vmx = to_vmx(vcpu);
6197 int irq = vcpu->arch.interrupt.nr;
6199 trace_kvm_inj_virq(irq);
6201 ++vcpu->stat.irq_injections;
6202 if (vmx->rmode.vm86_active) {
6204 if (vcpu->arch.interrupt.soft)
6205 inc_eip = vcpu->arch.event_exit_inst_len;
6206 if (kvm_inject_realmode_interrupt(vcpu, irq, inc_eip) != EMULATE_DONE)
6207 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
6210 intr = irq | INTR_INFO_VALID_MASK;
6211 if (vcpu->arch.interrupt.soft) {
6212 intr |= INTR_TYPE_SOFT_INTR;
6213 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
6214 vmx->vcpu.arch.event_exit_inst_len);
6216 intr |= INTR_TYPE_EXT_INTR;
6217 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr);
6219 vmx_clear_hlt(vcpu);
6222 static void vmx_inject_nmi(struct kvm_vcpu *vcpu)
6224 struct vcpu_vmx *vmx = to_vmx(vcpu);
6228 * Tracking the NMI-blocked state in software is built upon
6229 * finding the next open IRQ window. This, in turn, depends on
6230 * well-behaving guests: They have to keep IRQs disabled at
6231 * least as long as the NMI handler runs. Otherwise we may
6232 * cause NMI nesting, maybe breaking the guest. But as this is
6233 * highly unlikely, we can live with the residual risk.
6235 vmx->loaded_vmcs->soft_vnmi_blocked = 1;
6236 vmx->loaded_vmcs->vnmi_blocked_time = 0;
6239 ++vcpu->stat.nmi_injections;
6240 vmx->loaded_vmcs->nmi_known_unmasked = false;
6242 if (vmx->rmode.vm86_active) {
6243 if (kvm_inject_realmode_interrupt(vcpu, NMI_VECTOR, 0) != EMULATE_DONE)
6244 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
6248 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
6249 INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK | NMI_VECTOR);
6251 vmx_clear_hlt(vcpu);
6254 static bool vmx_get_nmi_mask(struct kvm_vcpu *vcpu)
6256 struct vcpu_vmx *vmx = to_vmx(vcpu);
6260 return vmx->loaded_vmcs->soft_vnmi_blocked;
6261 if (vmx->loaded_vmcs->nmi_known_unmasked)
6263 masked = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & GUEST_INTR_STATE_NMI;
6264 vmx->loaded_vmcs->nmi_known_unmasked = !masked;
6268 static void vmx_set_nmi_mask(struct kvm_vcpu *vcpu, bool masked)
6270 struct vcpu_vmx *vmx = to_vmx(vcpu);
6273 if (vmx->loaded_vmcs->soft_vnmi_blocked != masked) {
6274 vmx->loaded_vmcs->soft_vnmi_blocked = masked;
6275 vmx->loaded_vmcs->vnmi_blocked_time = 0;
6278 vmx->loaded_vmcs->nmi_known_unmasked = !masked;
6280 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
6281 GUEST_INTR_STATE_NMI);
6283 vmcs_clear_bits(GUEST_INTERRUPTIBILITY_INFO,
6284 GUEST_INTR_STATE_NMI);
6288 static int vmx_nmi_allowed(struct kvm_vcpu *vcpu)
6290 if (to_vmx(vcpu)->nested.nested_run_pending)
6294 to_vmx(vcpu)->loaded_vmcs->soft_vnmi_blocked)
6297 return !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) &
6298 (GUEST_INTR_STATE_MOV_SS | GUEST_INTR_STATE_STI
6299 | GUEST_INTR_STATE_NMI));
6302 static int vmx_interrupt_allowed(struct kvm_vcpu *vcpu)
6304 return (!to_vmx(vcpu)->nested.nested_run_pending &&
6305 vmcs_readl(GUEST_RFLAGS) & X86_EFLAGS_IF) &&
6306 !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) &
6307 (GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS));
6310 static int vmx_set_tss_addr(struct kvm *kvm, unsigned int addr)
6314 if (enable_unrestricted_guest)
6317 ret = x86_set_memory_region(kvm, TSS_PRIVATE_MEMSLOT, addr,
6321 to_kvm_vmx(kvm)->tss_addr = addr;
6322 return init_rmode_tss(kvm);
6325 static int vmx_set_identity_map_addr(struct kvm *kvm, u64 ident_addr)
6327 to_kvm_vmx(kvm)->ept_identity_map_addr = ident_addr;
6331 static bool rmode_exception(struct kvm_vcpu *vcpu, int vec)
6336 * Update instruction length as we may reinject the exception
6337 * from user space while in guest debugging mode.
6339 to_vmx(vcpu)->vcpu.arch.event_exit_inst_len =
6340 vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
6341 if (vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
6345 if (vcpu->guest_debug &
6346 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))
6363 static int handle_rmode_exception(struct kvm_vcpu *vcpu,
6364 int vec, u32 err_code)
6367 * Instruction with address size override prefix opcode 0x67
6368 * Cause the #SS fault with 0 error code in VM86 mode.
6370 if (((vec == GP_VECTOR) || (vec == SS_VECTOR)) && err_code == 0) {
6371 if (emulate_instruction(vcpu, 0) == EMULATE_DONE) {
6372 if (vcpu->arch.halt_request) {
6373 vcpu->arch.halt_request = 0;
6374 return kvm_vcpu_halt(vcpu);
6382 * Forward all other exceptions that are valid in real mode.
6383 * FIXME: Breaks guest debugging in real mode, needs to be fixed with
6384 * the required debugging infrastructure rework.
6386 kvm_queue_exception(vcpu, vec);
6391 * Trigger machine check on the host. We assume all the MSRs are already set up
6392 * by the CPU and that we still run on the same CPU as the MCE occurred on.
6393 * We pass a fake environment to the machine check handler because we want
6394 * the guest to be always treated like user space, no matter what context
6395 * it used internally.
6397 static void kvm_machine_check(void)
6399 #if defined(CONFIG_X86_MCE) && defined(CONFIG_X86_64)
6400 struct pt_regs regs = {
6401 .cs = 3, /* Fake ring 3 no matter what the guest ran on */
6402 .flags = X86_EFLAGS_IF,
6405 do_machine_check(®s, 0);
6409 static int handle_machine_check(struct kvm_vcpu *vcpu)
6411 /* already handled by vcpu_run */
6415 static int handle_exception(struct kvm_vcpu *vcpu)
6417 struct vcpu_vmx *vmx = to_vmx(vcpu);
6418 struct kvm_run *kvm_run = vcpu->run;
6419 u32 intr_info, ex_no, error_code;
6420 unsigned long cr2, rip, dr6;
6422 enum emulation_result er;
6424 vect_info = vmx->idt_vectoring_info;
6425 intr_info = vmx->exit_intr_info;
6427 if (is_machine_check(intr_info))
6428 return handle_machine_check(vcpu);
6430 if (is_nmi(intr_info))
6431 return 1; /* already handled by vmx_vcpu_run() */
6433 if (is_invalid_opcode(intr_info))
6434 return handle_ud(vcpu);
6437 if (intr_info & INTR_INFO_DELIVER_CODE_MASK)
6438 error_code = vmcs_read32(VM_EXIT_INTR_ERROR_CODE);
6440 if (!vmx->rmode.vm86_active && is_gp_fault(intr_info)) {
6441 WARN_ON_ONCE(!enable_vmware_backdoor);
6442 er = emulate_instruction(vcpu,
6443 EMULTYPE_VMWARE | EMULTYPE_NO_UD_ON_FAIL);
6444 if (er == EMULATE_USER_EXIT)
6446 else if (er != EMULATE_DONE)
6447 kvm_queue_exception_e(vcpu, GP_VECTOR, error_code);
6452 * The #PF with PFEC.RSVD = 1 indicates the guest is accessing
6453 * MMIO, it is better to report an internal error.
6454 * See the comments in vmx_handle_exit.
6456 if ((vect_info & VECTORING_INFO_VALID_MASK) &&
6457 !(is_page_fault(intr_info) && !(error_code & PFERR_RSVD_MASK))) {
6458 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
6459 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_SIMUL_EX;
6460 vcpu->run->internal.ndata = 3;
6461 vcpu->run->internal.data[0] = vect_info;
6462 vcpu->run->internal.data[1] = intr_info;
6463 vcpu->run->internal.data[2] = error_code;
6467 if (is_page_fault(intr_info)) {
6468 cr2 = vmcs_readl(EXIT_QUALIFICATION);
6469 /* EPT won't cause page fault directly */
6470 WARN_ON_ONCE(!vcpu->arch.apf.host_apf_reason && enable_ept);
6471 return kvm_handle_page_fault(vcpu, error_code, cr2, NULL, 0);
6474 ex_no = intr_info & INTR_INFO_VECTOR_MASK;
6476 if (vmx->rmode.vm86_active && rmode_exception(vcpu, ex_no))
6477 return handle_rmode_exception(vcpu, ex_no, error_code);
6481 kvm_queue_exception_e(vcpu, AC_VECTOR, error_code);
6484 dr6 = vmcs_readl(EXIT_QUALIFICATION);
6485 if (!(vcpu->guest_debug &
6486 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))) {
6487 vcpu->arch.dr6 &= ~15;
6488 vcpu->arch.dr6 |= dr6 | DR6_RTM;
6489 if (is_icebp(intr_info))
6490 skip_emulated_instruction(vcpu);
6492 kvm_queue_exception(vcpu, DB_VECTOR);
6495 kvm_run->debug.arch.dr6 = dr6 | DR6_FIXED_1;
6496 kvm_run->debug.arch.dr7 = vmcs_readl(GUEST_DR7);
6500 * Update instruction length as we may reinject #BP from
6501 * user space while in guest debugging mode. Reading it for
6502 * #DB as well causes no harm, it is not used in that case.
6504 vmx->vcpu.arch.event_exit_inst_len =
6505 vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
6506 kvm_run->exit_reason = KVM_EXIT_DEBUG;
6507 rip = kvm_rip_read(vcpu);
6508 kvm_run->debug.arch.pc = vmcs_readl(GUEST_CS_BASE) + rip;
6509 kvm_run->debug.arch.exception = ex_no;
6512 kvm_run->exit_reason = KVM_EXIT_EXCEPTION;
6513 kvm_run->ex.exception = ex_no;
6514 kvm_run->ex.error_code = error_code;
6520 static int handle_external_interrupt(struct kvm_vcpu *vcpu)
6522 ++vcpu->stat.irq_exits;
6526 static int handle_triple_fault(struct kvm_vcpu *vcpu)
6528 vcpu->run->exit_reason = KVM_EXIT_SHUTDOWN;
6529 vcpu->mmio_needed = 0;
6533 static int handle_io(struct kvm_vcpu *vcpu)
6535 unsigned long exit_qualification;
6536 int size, in, string;
6539 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
6540 string = (exit_qualification & 16) != 0;
6542 ++vcpu->stat.io_exits;
6545 return emulate_instruction(vcpu, 0) == EMULATE_DONE;
6547 port = exit_qualification >> 16;
6548 size = (exit_qualification & 7) + 1;
6549 in = (exit_qualification & 8) != 0;
6551 return kvm_fast_pio(vcpu, size, port, in);
6555 vmx_patch_hypercall(struct kvm_vcpu *vcpu, unsigned char *hypercall)
6558 * Patch in the VMCALL instruction:
6560 hypercall[0] = 0x0f;
6561 hypercall[1] = 0x01;
6562 hypercall[2] = 0xc1;
6565 /* called to set cr0 as appropriate for a mov-to-cr0 exit. */
6566 static int handle_set_cr0(struct kvm_vcpu *vcpu, unsigned long val)
6568 if (is_guest_mode(vcpu)) {
6569 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
6570 unsigned long orig_val = val;
6573 * We get here when L2 changed cr0 in a way that did not change
6574 * any of L1's shadowed bits (see nested_vmx_exit_handled_cr),
6575 * but did change L0 shadowed bits. So we first calculate the
6576 * effective cr0 value that L1 would like to write into the
6577 * hardware. It consists of the L2-owned bits from the new
6578 * value combined with the L1-owned bits from L1's guest_cr0.
6580 val = (val & ~vmcs12->cr0_guest_host_mask) |
6581 (vmcs12->guest_cr0 & vmcs12->cr0_guest_host_mask);
6583 if (!nested_guest_cr0_valid(vcpu, val))
6586 if (kvm_set_cr0(vcpu, val))
6588 vmcs_writel(CR0_READ_SHADOW, orig_val);
6591 if (to_vmx(vcpu)->nested.vmxon &&
6592 !nested_host_cr0_valid(vcpu, val))
6595 return kvm_set_cr0(vcpu, val);
6599 static int handle_set_cr4(struct kvm_vcpu *vcpu, unsigned long val)
6601 if (is_guest_mode(vcpu)) {
6602 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
6603 unsigned long orig_val = val;
6605 /* analogously to handle_set_cr0 */
6606 val = (val & ~vmcs12->cr4_guest_host_mask) |
6607 (vmcs12->guest_cr4 & vmcs12->cr4_guest_host_mask);
6608 if (kvm_set_cr4(vcpu, val))
6610 vmcs_writel(CR4_READ_SHADOW, orig_val);
6613 return kvm_set_cr4(vcpu, val);
6616 static int handle_desc(struct kvm_vcpu *vcpu)
6618 WARN_ON(!(vcpu->arch.cr4 & X86_CR4_UMIP));
6619 return emulate_instruction(vcpu, 0) == EMULATE_DONE;
6622 static int handle_cr(struct kvm_vcpu *vcpu)
6624 unsigned long exit_qualification, val;
6630 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
6631 cr = exit_qualification & 15;
6632 reg = (exit_qualification >> 8) & 15;
6633 switch ((exit_qualification >> 4) & 3) {
6634 case 0: /* mov to cr */
6635 val = kvm_register_readl(vcpu, reg);
6636 trace_kvm_cr_write(cr, val);
6639 err = handle_set_cr0(vcpu, val);
6640 return kvm_complete_insn_gp(vcpu, err);
6642 WARN_ON_ONCE(enable_unrestricted_guest);
6643 err = kvm_set_cr3(vcpu, val);
6644 return kvm_complete_insn_gp(vcpu, err);
6646 err = handle_set_cr4(vcpu, val);
6647 return kvm_complete_insn_gp(vcpu, err);
6649 u8 cr8_prev = kvm_get_cr8(vcpu);
6651 err = kvm_set_cr8(vcpu, cr8);
6652 ret = kvm_complete_insn_gp(vcpu, err);
6653 if (lapic_in_kernel(vcpu))
6655 if (cr8_prev <= cr8)
6658 * TODO: we might be squashing a
6659 * KVM_GUESTDBG_SINGLESTEP-triggered
6660 * KVM_EXIT_DEBUG here.
6662 vcpu->run->exit_reason = KVM_EXIT_SET_TPR;
6668 WARN_ONCE(1, "Guest should always own CR0.TS");
6669 vmx_set_cr0(vcpu, kvm_read_cr0_bits(vcpu, ~X86_CR0_TS));
6670 trace_kvm_cr_write(0, kvm_read_cr0(vcpu));
6671 return kvm_skip_emulated_instruction(vcpu);
6672 case 1: /*mov from cr*/
6675 WARN_ON_ONCE(enable_unrestricted_guest);
6676 val = kvm_read_cr3(vcpu);
6677 kvm_register_write(vcpu, reg, val);
6678 trace_kvm_cr_read(cr, val);
6679 return kvm_skip_emulated_instruction(vcpu);
6681 val = kvm_get_cr8(vcpu);
6682 kvm_register_write(vcpu, reg, val);
6683 trace_kvm_cr_read(cr, val);
6684 return kvm_skip_emulated_instruction(vcpu);
6688 val = (exit_qualification >> LMSW_SOURCE_DATA_SHIFT) & 0x0f;
6689 trace_kvm_cr_write(0, (kvm_read_cr0(vcpu) & ~0xful) | val);
6690 kvm_lmsw(vcpu, val);
6692 return kvm_skip_emulated_instruction(vcpu);
6696 vcpu->run->exit_reason = 0;
6697 vcpu_unimpl(vcpu, "unhandled control register: op %d cr %d\n",
6698 (int)(exit_qualification >> 4) & 3, cr);
6702 static int handle_dr(struct kvm_vcpu *vcpu)
6704 unsigned long exit_qualification;
6707 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
6708 dr = exit_qualification & DEBUG_REG_ACCESS_NUM;
6710 /* First, if DR does not exist, trigger UD */
6711 if (!kvm_require_dr(vcpu, dr))
6714 /* Do not handle if the CPL > 0, will trigger GP on re-entry */
6715 if (!kvm_require_cpl(vcpu, 0))
6717 dr7 = vmcs_readl(GUEST_DR7);
6720 * As the vm-exit takes precedence over the debug trap, we
6721 * need to emulate the latter, either for the host or the
6722 * guest debugging itself.
6724 if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP) {
6725 vcpu->run->debug.arch.dr6 = vcpu->arch.dr6;
6726 vcpu->run->debug.arch.dr7 = dr7;
6727 vcpu->run->debug.arch.pc = kvm_get_linear_rip(vcpu);
6728 vcpu->run->debug.arch.exception = DB_VECTOR;
6729 vcpu->run->exit_reason = KVM_EXIT_DEBUG;
6732 vcpu->arch.dr6 &= ~15;
6733 vcpu->arch.dr6 |= DR6_BD | DR6_RTM;
6734 kvm_queue_exception(vcpu, DB_VECTOR);
6739 if (vcpu->guest_debug == 0) {
6740 vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL,
6741 CPU_BASED_MOV_DR_EXITING);
6744 * No more DR vmexits; force a reload of the debug registers
6745 * and reenter on this instruction. The next vmexit will
6746 * retrieve the full state of the debug registers.
6748 vcpu->arch.switch_db_regs |= KVM_DEBUGREG_WONT_EXIT;
6752 reg = DEBUG_REG_ACCESS_REG(exit_qualification);
6753 if (exit_qualification & TYPE_MOV_FROM_DR) {
6756 if (kvm_get_dr(vcpu, dr, &val))
6758 kvm_register_write(vcpu, reg, val);
6760 if (kvm_set_dr(vcpu, dr, kvm_register_readl(vcpu, reg)))
6763 return kvm_skip_emulated_instruction(vcpu);
6766 static u64 vmx_get_dr6(struct kvm_vcpu *vcpu)
6768 return vcpu->arch.dr6;
6771 static void vmx_set_dr6(struct kvm_vcpu *vcpu, unsigned long val)
6775 static void vmx_sync_dirty_debug_regs(struct kvm_vcpu *vcpu)
6777 get_debugreg(vcpu->arch.db[0], 0);
6778 get_debugreg(vcpu->arch.db[1], 1);
6779 get_debugreg(vcpu->arch.db[2], 2);
6780 get_debugreg(vcpu->arch.db[3], 3);
6781 get_debugreg(vcpu->arch.dr6, 6);
6782 vcpu->arch.dr7 = vmcs_readl(GUEST_DR7);
6784 vcpu->arch.switch_db_regs &= ~KVM_DEBUGREG_WONT_EXIT;
6785 vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL, CPU_BASED_MOV_DR_EXITING);
6788 static void vmx_set_dr7(struct kvm_vcpu *vcpu, unsigned long val)
6790 vmcs_writel(GUEST_DR7, val);
6793 static int handle_cpuid(struct kvm_vcpu *vcpu)
6795 return kvm_emulate_cpuid(vcpu);
6798 static int handle_rdmsr(struct kvm_vcpu *vcpu)
6800 u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX];
6801 struct msr_data msr_info;
6803 msr_info.index = ecx;
6804 msr_info.host_initiated = false;
6805 if (vmx_get_msr(vcpu, &msr_info)) {
6806 trace_kvm_msr_read_ex(ecx);
6807 kvm_inject_gp(vcpu, 0);
6811 trace_kvm_msr_read(ecx, msr_info.data);
6813 /* FIXME: handling of bits 32:63 of rax, rdx */
6814 vcpu->arch.regs[VCPU_REGS_RAX] = msr_info.data & -1u;
6815 vcpu->arch.regs[VCPU_REGS_RDX] = (msr_info.data >> 32) & -1u;
6816 return kvm_skip_emulated_instruction(vcpu);
6819 static int handle_wrmsr(struct kvm_vcpu *vcpu)
6821 struct msr_data msr;
6822 u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX];
6823 u64 data = (vcpu->arch.regs[VCPU_REGS_RAX] & -1u)
6824 | ((u64)(vcpu->arch.regs[VCPU_REGS_RDX] & -1u) << 32);
6828 msr.host_initiated = false;
6829 if (kvm_set_msr(vcpu, &msr) != 0) {
6830 trace_kvm_msr_write_ex(ecx, data);
6831 kvm_inject_gp(vcpu, 0);
6835 trace_kvm_msr_write(ecx, data);
6836 return kvm_skip_emulated_instruction(vcpu);
6839 static int handle_tpr_below_threshold(struct kvm_vcpu *vcpu)
6841 kvm_apic_update_ppr(vcpu);
6845 static int handle_interrupt_window(struct kvm_vcpu *vcpu)
6847 vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL,
6848 CPU_BASED_VIRTUAL_INTR_PENDING);
6850 kvm_make_request(KVM_REQ_EVENT, vcpu);
6852 ++vcpu->stat.irq_window_exits;
6856 static int handle_halt(struct kvm_vcpu *vcpu)
6858 return kvm_emulate_halt(vcpu);
6861 static int handle_vmcall(struct kvm_vcpu *vcpu)
6863 return kvm_emulate_hypercall(vcpu);
6866 static int handle_invd(struct kvm_vcpu *vcpu)
6868 return emulate_instruction(vcpu, 0) == EMULATE_DONE;
6871 static int handle_invlpg(struct kvm_vcpu *vcpu)
6873 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
6875 kvm_mmu_invlpg(vcpu, exit_qualification);
6876 return kvm_skip_emulated_instruction(vcpu);
6879 static int handle_rdpmc(struct kvm_vcpu *vcpu)
6883 err = kvm_rdpmc(vcpu);
6884 return kvm_complete_insn_gp(vcpu, err);
6887 static int handle_wbinvd(struct kvm_vcpu *vcpu)
6889 return kvm_emulate_wbinvd(vcpu);
6892 static int handle_xsetbv(struct kvm_vcpu *vcpu)
6894 u64 new_bv = kvm_read_edx_eax(vcpu);
6895 u32 index = kvm_register_read(vcpu, VCPU_REGS_RCX);
6897 if (kvm_set_xcr(vcpu, index, new_bv) == 0)
6898 return kvm_skip_emulated_instruction(vcpu);
6902 static int handle_xsaves(struct kvm_vcpu *vcpu)
6904 kvm_skip_emulated_instruction(vcpu);
6905 WARN(1, "this should never happen\n");
6909 static int handle_xrstors(struct kvm_vcpu *vcpu)
6911 kvm_skip_emulated_instruction(vcpu);
6912 WARN(1, "this should never happen\n");
6916 static int handle_apic_access(struct kvm_vcpu *vcpu)
6918 if (likely(fasteoi)) {
6919 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
6920 int access_type, offset;
6922 access_type = exit_qualification & APIC_ACCESS_TYPE;
6923 offset = exit_qualification & APIC_ACCESS_OFFSET;
6925 * Sane guest uses MOV to write EOI, with written value
6926 * not cared. So make a short-circuit here by avoiding
6927 * heavy instruction emulation.
6929 if ((access_type == TYPE_LINEAR_APIC_INST_WRITE) &&
6930 (offset == APIC_EOI)) {
6931 kvm_lapic_set_eoi(vcpu);
6932 return kvm_skip_emulated_instruction(vcpu);
6935 return emulate_instruction(vcpu, 0) == EMULATE_DONE;
6938 static int handle_apic_eoi_induced(struct kvm_vcpu *vcpu)
6940 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
6941 int vector = exit_qualification & 0xff;
6943 /* EOI-induced VM exit is trap-like and thus no need to adjust IP */
6944 kvm_apic_set_eoi_accelerated(vcpu, vector);
6948 static int handle_apic_write(struct kvm_vcpu *vcpu)
6950 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
6951 u32 offset = exit_qualification & 0xfff;
6953 /* APIC-write VM exit is trap-like and thus no need to adjust IP */
6954 kvm_apic_write_nodecode(vcpu, offset);
6958 static int handle_task_switch(struct kvm_vcpu *vcpu)
6960 struct vcpu_vmx *vmx = to_vmx(vcpu);
6961 unsigned long exit_qualification;
6962 bool has_error_code = false;
6965 int reason, type, idt_v, idt_index;
6967 idt_v = (vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK);
6968 idt_index = (vmx->idt_vectoring_info & VECTORING_INFO_VECTOR_MASK);
6969 type = (vmx->idt_vectoring_info & VECTORING_INFO_TYPE_MASK);
6971 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
6973 reason = (u32)exit_qualification >> 30;
6974 if (reason == TASK_SWITCH_GATE && idt_v) {
6976 case INTR_TYPE_NMI_INTR:
6977 vcpu->arch.nmi_injected = false;
6978 vmx_set_nmi_mask(vcpu, true);
6980 case INTR_TYPE_EXT_INTR:
6981 case INTR_TYPE_SOFT_INTR:
6982 kvm_clear_interrupt_queue(vcpu);
6984 case INTR_TYPE_HARD_EXCEPTION:
6985 if (vmx->idt_vectoring_info &
6986 VECTORING_INFO_DELIVER_CODE_MASK) {
6987 has_error_code = true;
6989 vmcs_read32(IDT_VECTORING_ERROR_CODE);
6992 case INTR_TYPE_SOFT_EXCEPTION:
6993 kvm_clear_exception_queue(vcpu);
6999 tss_selector = exit_qualification;
7001 if (!idt_v || (type != INTR_TYPE_HARD_EXCEPTION &&
7002 type != INTR_TYPE_EXT_INTR &&
7003 type != INTR_TYPE_NMI_INTR))
7004 skip_emulated_instruction(vcpu);
7006 if (kvm_task_switch(vcpu, tss_selector,
7007 type == INTR_TYPE_SOFT_INTR ? idt_index : -1, reason,
7008 has_error_code, error_code) == EMULATE_FAIL) {
7009 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
7010 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
7011 vcpu->run->internal.ndata = 0;
7016 * TODO: What about debug traps on tss switch?
7017 * Are we supposed to inject them and update dr6?
7023 static int handle_ept_violation(struct kvm_vcpu *vcpu)
7025 unsigned long exit_qualification;
7029 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7032 * EPT violation happened while executing iret from NMI,
7033 * "blocked by NMI" bit has to be set before next VM entry.
7034 * There are errata that may cause this bit to not be set:
7037 if (!(to_vmx(vcpu)->idt_vectoring_info & VECTORING_INFO_VALID_MASK) &&
7039 (exit_qualification & INTR_INFO_UNBLOCK_NMI))
7040 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO, GUEST_INTR_STATE_NMI);
7042 gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS);
7043 trace_kvm_page_fault(gpa, exit_qualification);
7045 /* Is it a read fault? */
7046 error_code = (exit_qualification & EPT_VIOLATION_ACC_READ)
7047 ? PFERR_USER_MASK : 0;
7048 /* Is it a write fault? */
7049 error_code |= (exit_qualification & EPT_VIOLATION_ACC_WRITE)
7050 ? PFERR_WRITE_MASK : 0;
7051 /* Is it a fetch fault? */
7052 error_code |= (exit_qualification & EPT_VIOLATION_ACC_INSTR)
7053 ? PFERR_FETCH_MASK : 0;
7054 /* ept page table entry is present? */
7055 error_code |= (exit_qualification &
7056 (EPT_VIOLATION_READABLE | EPT_VIOLATION_WRITABLE |
7057 EPT_VIOLATION_EXECUTABLE))
7058 ? PFERR_PRESENT_MASK : 0;
7060 error_code |= (exit_qualification & 0x100) != 0 ?
7061 PFERR_GUEST_FINAL_MASK : PFERR_GUEST_PAGE_MASK;
7063 vcpu->arch.exit_qualification = exit_qualification;
7064 return kvm_mmu_page_fault(vcpu, gpa, error_code, NULL, 0);
7067 static int handle_ept_misconfig(struct kvm_vcpu *vcpu)
7072 * A nested guest cannot optimize MMIO vmexits, because we have an
7073 * nGPA here instead of the required GPA.
7075 gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS);
7076 if (!is_guest_mode(vcpu) &&
7077 !kvm_io_bus_write(vcpu, KVM_FAST_MMIO_BUS, gpa, 0, NULL)) {
7078 trace_kvm_fast_mmio(gpa);
7080 * Doing kvm_skip_emulated_instruction() depends on undefined
7081 * behavior: Intel's manual doesn't mandate
7082 * VM_EXIT_INSTRUCTION_LEN to be set in VMCS when EPT MISCONFIG
7083 * occurs and while on real hardware it was observed to be set,
7084 * other hypervisors (namely Hyper-V) don't set it, we end up
7085 * advancing IP with some random value. Disable fast mmio when
7086 * running nested and keep it for real hardware in hope that
7087 * VM_EXIT_INSTRUCTION_LEN will always be set correctly.
7089 if (!static_cpu_has(X86_FEATURE_HYPERVISOR))
7090 return kvm_skip_emulated_instruction(vcpu);
7092 return x86_emulate_instruction(vcpu, gpa, EMULTYPE_SKIP,
7093 NULL, 0) == EMULATE_DONE;
7096 return kvm_mmu_page_fault(vcpu, gpa, PFERR_RSVD_MASK, NULL, 0);
7099 static int handle_nmi_window(struct kvm_vcpu *vcpu)
7101 WARN_ON_ONCE(!enable_vnmi);
7102 vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL,
7103 CPU_BASED_VIRTUAL_NMI_PENDING);
7104 ++vcpu->stat.nmi_window_exits;
7105 kvm_make_request(KVM_REQ_EVENT, vcpu);
7110 static int handle_invalid_guest_state(struct kvm_vcpu *vcpu)
7112 struct vcpu_vmx *vmx = to_vmx(vcpu);
7113 enum emulation_result err = EMULATE_DONE;
7116 bool intr_window_requested;
7117 unsigned count = 130;
7120 * We should never reach the point where we are emulating L2
7121 * due to invalid guest state as that means we incorrectly
7122 * allowed a nested VMEntry with an invalid vmcs12.
7124 WARN_ON_ONCE(vmx->emulation_required && vmx->nested.nested_run_pending);
7126 cpu_exec_ctrl = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
7127 intr_window_requested = cpu_exec_ctrl & CPU_BASED_VIRTUAL_INTR_PENDING;
7129 while (vmx->emulation_required && count-- != 0) {
7130 if (intr_window_requested && vmx_interrupt_allowed(vcpu))
7131 return handle_interrupt_window(&vmx->vcpu);
7133 if (kvm_test_request(KVM_REQ_EVENT, vcpu))
7136 err = emulate_instruction(vcpu, 0);
7138 if (err == EMULATE_USER_EXIT) {
7139 ++vcpu->stat.mmio_exits;
7144 if (err != EMULATE_DONE)
7145 goto emulation_error;
7147 if (vmx->emulation_required && !vmx->rmode.vm86_active &&
7148 vcpu->arch.exception.pending)
7149 goto emulation_error;
7151 if (vcpu->arch.halt_request) {
7152 vcpu->arch.halt_request = 0;
7153 ret = kvm_vcpu_halt(vcpu);
7157 if (signal_pending(current))
7167 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
7168 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
7169 vcpu->run->internal.ndata = 0;
7173 static void grow_ple_window(struct kvm_vcpu *vcpu)
7175 struct vcpu_vmx *vmx = to_vmx(vcpu);
7176 int old = vmx->ple_window;
7178 vmx->ple_window = __grow_ple_window(old, ple_window,
7182 if (vmx->ple_window != old)
7183 vmx->ple_window_dirty = true;
7185 trace_kvm_ple_window_grow(vcpu->vcpu_id, vmx->ple_window, old);
7188 static void shrink_ple_window(struct kvm_vcpu *vcpu)
7190 struct vcpu_vmx *vmx = to_vmx(vcpu);
7191 int old = vmx->ple_window;
7193 vmx->ple_window = __shrink_ple_window(old, ple_window,
7197 if (vmx->ple_window != old)
7198 vmx->ple_window_dirty = true;
7200 trace_kvm_ple_window_shrink(vcpu->vcpu_id, vmx->ple_window, old);
7204 * Handler for POSTED_INTERRUPT_WAKEUP_VECTOR.
7206 static void wakeup_handler(void)
7208 struct kvm_vcpu *vcpu;
7209 int cpu = smp_processor_id();
7211 spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, cpu));
7212 list_for_each_entry(vcpu, &per_cpu(blocked_vcpu_on_cpu, cpu),
7213 blocked_vcpu_list) {
7214 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
7216 if (pi_test_on(pi_desc) == 1)
7217 kvm_vcpu_kick(vcpu);
7219 spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, cpu));
7222 static void vmx_enable_tdp(void)
7224 kvm_mmu_set_mask_ptes(VMX_EPT_READABLE_MASK,
7225 enable_ept_ad_bits ? VMX_EPT_ACCESS_BIT : 0ull,
7226 enable_ept_ad_bits ? VMX_EPT_DIRTY_BIT : 0ull,
7227 0ull, VMX_EPT_EXECUTABLE_MASK,
7228 cpu_has_vmx_ept_execute_only() ? 0ull : VMX_EPT_READABLE_MASK,
7229 VMX_EPT_RWX_MASK, 0ull);
7231 ept_set_mmio_spte_mask();
7235 static __init int hardware_setup(void)
7239 rdmsrl_safe(MSR_EFER, &host_efer);
7241 for (i = 0; i < ARRAY_SIZE(vmx_msr_index); ++i)
7242 kvm_define_shared_msr(i, vmx_msr_index[i]);
7244 for (i = 0; i < VMX_BITMAP_NR; i++) {
7245 vmx_bitmap[i] = (unsigned long *)__get_free_page(GFP_KERNEL);
7250 memset(vmx_vmread_bitmap, 0xff, PAGE_SIZE);
7251 memset(vmx_vmwrite_bitmap, 0xff, PAGE_SIZE);
7253 if (setup_vmcs_config(&vmcs_config) < 0) {
7258 if (boot_cpu_has(X86_FEATURE_NX))
7259 kvm_enable_efer_bits(EFER_NX);
7261 if (!cpu_has_vmx_vpid() || !cpu_has_vmx_invvpid() ||
7262 !(cpu_has_vmx_invvpid_single() || cpu_has_vmx_invvpid_global()))
7265 if (!cpu_has_vmx_ept() ||
7266 !cpu_has_vmx_ept_4levels() ||
7267 !cpu_has_vmx_ept_mt_wb() ||
7268 !cpu_has_vmx_invept_global())
7271 if (!cpu_has_vmx_ept_ad_bits() || !enable_ept)
7272 enable_ept_ad_bits = 0;
7274 if (!cpu_has_vmx_unrestricted_guest() || !enable_ept)
7275 enable_unrestricted_guest = 0;
7277 if (!cpu_has_vmx_flexpriority())
7278 flexpriority_enabled = 0;
7280 if (!cpu_has_virtual_nmis())
7284 * set_apic_access_page_addr() is used to reload apic access
7285 * page upon invalidation. No need to do anything if not
7286 * using the APIC_ACCESS_ADDR VMCS field.
7288 if (!flexpriority_enabled)
7289 kvm_x86_ops->set_apic_access_page_addr = NULL;
7291 if (!cpu_has_vmx_tpr_shadow())
7292 kvm_x86_ops->update_cr8_intercept = NULL;
7294 if (enable_ept && !cpu_has_vmx_ept_2m_page())
7295 kvm_disable_largepages();
7297 if (!cpu_has_vmx_ple()) {
7300 ple_window_grow = 0;
7302 ple_window_shrink = 0;
7305 if (!cpu_has_vmx_apicv()) {
7307 kvm_x86_ops->sync_pir_to_irr = NULL;
7310 if (cpu_has_vmx_tsc_scaling()) {
7311 kvm_has_tsc_control = true;
7312 kvm_max_tsc_scaling_ratio = KVM_VMX_TSC_MULTIPLIER_MAX;
7313 kvm_tsc_scaling_ratio_frac_bits = 48;
7316 set_bit(0, vmx_vpid_bitmap); /* 0 is reserved for host */
7324 * Only enable PML when hardware supports PML feature, and both EPT
7325 * and EPT A/D bit features are enabled -- PML depends on them to work.
7327 if (!enable_ept || !enable_ept_ad_bits || !cpu_has_vmx_pml())
7331 kvm_x86_ops->slot_enable_log_dirty = NULL;
7332 kvm_x86_ops->slot_disable_log_dirty = NULL;
7333 kvm_x86_ops->flush_log_dirty = NULL;
7334 kvm_x86_ops->enable_log_dirty_pt_masked = NULL;
7337 if (cpu_has_vmx_preemption_timer() && enable_preemption_timer) {
7340 rdmsrl(MSR_IA32_VMX_MISC, vmx_msr);
7341 cpu_preemption_timer_multi =
7342 vmx_msr & VMX_MISC_PREEMPTION_TIMER_RATE_MASK;
7344 kvm_x86_ops->set_hv_timer = NULL;
7345 kvm_x86_ops->cancel_hv_timer = NULL;
7348 if (!cpu_has_vmx_shadow_vmcs())
7349 enable_shadow_vmcs = 0;
7350 if (enable_shadow_vmcs)
7351 init_vmcs_shadow_fields();
7353 kvm_set_posted_intr_wakeup_handler(wakeup_handler);
7354 nested_vmx_setup_ctls_msrs(&vmcs_config.nested, enable_apicv);
7356 kvm_mce_cap_supported |= MCG_LMCE_P;
7358 return alloc_kvm_area();
7361 for (i = 0; i < VMX_BITMAP_NR; i++)
7362 free_page((unsigned long)vmx_bitmap[i]);
7367 static __exit void hardware_unsetup(void)
7371 for (i = 0; i < VMX_BITMAP_NR; i++)
7372 free_page((unsigned long)vmx_bitmap[i]);
7378 * Indicate a busy-waiting vcpu in spinlock. We do not enable the PAUSE
7379 * exiting, so only get here on cpu with PAUSE-Loop-Exiting.
7381 static int handle_pause(struct kvm_vcpu *vcpu)
7383 if (!kvm_pause_in_guest(vcpu->kvm))
7384 grow_ple_window(vcpu);
7387 * Intel sdm vol3 ch-25.1.3 says: The "PAUSE-loop exiting"
7388 * VM-execution control is ignored if CPL > 0. OTOH, KVM
7389 * never set PAUSE_EXITING and just set PLE if supported,
7390 * so the vcpu must be CPL=0 if it gets a PAUSE exit.
7392 kvm_vcpu_on_spin(vcpu, true);
7393 return kvm_skip_emulated_instruction(vcpu);
7396 static int handle_nop(struct kvm_vcpu *vcpu)
7398 return kvm_skip_emulated_instruction(vcpu);
7401 static int handle_mwait(struct kvm_vcpu *vcpu)
7403 printk_once(KERN_WARNING "kvm: MWAIT instruction emulated as NOP!\n");
7404 return handle_nop(vcpu);
7407 static int handle_invalid_op(struct kvm_vcpu *vcpu)
7409 kvm_queue_exception(vcpu, UD_VECTOR);
7413 static int handle_monitor_trap(struct kvm_vcpu *vcpu)
7418 static int handle_monitor(struct kvm_vcpu *vcpu)
7420 printk_once(KERN_WARNING "kvm: MONITOR instruction emulated as NOP!\n");
7421 return handle_nop(vcpu);
7425 * The following 3 functions, nested_vmx_succeed()/failValid()/failInvalid(),
7426 * set the success or error code of an emulated VMX instruction, as specified
7427 * by Vol 2B, VMX Instruction Reference, "Conventions".
7429 static void nested_vmx_succeed(struct kvm_vcpu *vcpu)
7431 vmx_set_rflags(vcpu, vmx_get_rflags(vcpu)
7432 & ~(X86_EFLAGS_CF | X86_EFLAGS_PF | X86_EFLAGS_AF |
7433 X86_EFLAGS_ZF | X86_EFLAGS_SF | X86_EFLAGS_OF));
7436 static void nested_vmx_failInvalid(struct kvm_vcpu *vcpu)
7438 vmx_set_rflags(vcpu, (vmx_get_rflags(vcpu)
7439 & ~(X86_EFLAGS_PF | X86_EFLAGS_AF | X86_EFLAGS_ZF |
7440 X86_EFLAGS_SF | X86_EFLAGS_OF))
7444 static void nested_vmx_failValid(struct kvm_vcpu *vcpu,
7445 u32 vm_instruction_error)
7447 if (to_vmx(vcpu)->nested.current_vmptr == -1ull) {
7449 * failValid writes the error number to the current VMCS, which
7450 * can't be done there isn't a current VMCS.
7452 nested_vmx_failInvalid(vcpu);
7455 vmx_set_rflags(vcpu, (vmx_get_rflags(vcpu)
7456 & ~(X86_EFLAGS_CF | X86_EFLAGS_PF | X86_EFLAGS_AF |
7457 X86_EFLAGS_SF | X86_EFLAGS_OF))
7459 get_vmcs12(vcpu)->vm_instruction_error = vm_instruction_error;
7461 * We don't need to force a shadow sync because
7462 * VM_INSTRUCTION_ERROR is not shadowed
7466 static void nested_vmx_abort(struct kvm_vcpu *vcpu, u32 indicator)
7468 /* TODO: not to reset guest simply here. */
7469 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
7470 pr_debug_ratelimited("kvm: nested vmx abort, indicator %d\n", indicator);
7473 static enum hrtimer_restart vmx_preemption_timer_fn(struct hrtimer *timer)
7475 struct vcpu_vmx *vmx =
7476 container_of(timer, struct vcpu_vmx, nested.preemption_timer);
7478 vmx->nested.preemption_timer_expired = true;
7479 kvm_make_request(KVM_REQ_EVENT, &vmx->vcpu);
7480 kvm_vcpu_kick(&vmx->vcpu);
7482 return HRTIMER_NORESTART;
7486 * Decode the memory-address operand of a vmx instruction, as recorded on an
7487 * exit caused by such an instruction (run by a guest hypervisor).
7488 * On success, returns 0. When the operand is invalid, returns 1 and throws
7491 static int get_vmx_mem_address(struct kvm_vcpu *vcpu,
7492 unsigned long exit_qualification,
7493 u32 vmx_instruction_info, bool wr, gva_t *ret)
7497 struct kvm_segment s;
7500 * According to Vol. 3B, "Information for VM Exits Due to Instruction
7501 * Execution", on an exit, vmx_instruction_info holds most of the
7502 * addressing components of the operand. Only the displacement part
7503 * is put in exit_qualification (see 3B, "Basic VM-Exit Information").
7504 * For how an actual address is calculated from all these components,
7505 * refer to Vol. 1, "Operand Addressing".
7507 int scaling = vmx_instruction_info & 3;
7508 int addr_size = (vmx_instruction_info >> 7) & 7;
7509 bool is_reg = vmx_instruction_info & (1u << 10);
7510 int seg_reg = (vmx_instruction_info >> 15) & 7;
7511 int index_reg = (vmx_instruction_info >> 18) & 0xf;
7512 bool index_is_valid = !(vmx_instruction_info & (1u << 22));
7513 int base_reg = (vmx_instruction_info >> 23) & 0xf;
7514 bool base_is_valid = !(vmx_instruction_info & (1u << 27));
7517 kvm_queue_exception(vcpu, UD_VECTOR);
7521 /* Addr = segment_base + offset */
7522 /* offset = base + [index * scale] + displacement */
7523 off = exit_qualification; /* holds the displacement */
7525 off += kvm_register_read(vcpu, base_reg);
7527 off += kvm_register_read(vcpu, index_reg)<<scaling;
7528 vmx_get_segment(vcpu, &s, seg_reg);
7529 *ret = s.base + off;
7531 if (addr_size == 1) /* 32 bit */
7534 /* Checks for #GP/#SS exceptions. */
7536 if (is_long_mode(vcpu)) {
7537 /* Long mode: #GP(0)/#SS(0) if the memory address is in a
7538 * non-canonical form. This is the only check on the memory
7539 * destination for long mode!
7541 exn = is_noncanonical_address(*ret, vcpu);
7542 } else if (is_protmode(vcpu)) {
7543 /* Protected mode: apply checks for segment validity in the
7545 * - segment type check (#GP(0) may be thrown)
7546 * - usability check (#GP(0)/#SS(0))
7547 * - limit check (#GP(0)/#SS(0))
7550 /* #GP(0) if the destination operand is located in a
7551 * read-only data segment or any code segment.
7553 exn = ((s.type & 0xa) == 0 || (s.type & 8));
7555 /* #GP(0) if the source operand is located in an
7556 * execute-only code segment
7558 exn = ((s.type & 0xa) == 8);
7560 kvm_queue_exception_e(vcpu, GP_VECTOR, 0);
7563 /* Protected mode: #GP(0)/#SS(0) if the segment is unusable.
7565 exn = (s.unusable != 0);
7566 /* Protected mode: #GP(0)/#SS(0) if the memory
7567 * operand is outside the segment limit.
7569 exn = exn || (off + sizeof(u64) > s.limit);
7572 kvm_queue_exception_e(vcpu,
7573 seg_reg == VCPU_SREG_SS ?
7574 SS_VECTOR : GP_VECTOR,
7582 static int nested_vmx_get_vmptr(struct kvm_vcpu *vcpu, gpa_t *vmpointer)
7585 struct x86_exception e;
7587 if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
7588 vmcs_read32(VMX_INSTRUCTION_INFO), false, &gva))
7591 if (kvm_read_guest_virt(&vcpu->arch.emulate_ctxt, gva, vmpointer,
7592 sizeof(*vmpointer), &e)) {
7593 kvm_inject_page_fault(vcpu, &e);
7600 static int enter_vmx_operation(struct kvm_vcpu *vcpu)
7602 struct vcpu_vmx *vmx = to_vmx(vcpu);
7603 struct vmcs *shadow_vmcs;
7606 r = alloc_loaded_vmcs(&vmx->nested.vmcs02);
7610 vmx->nested.cached_vmcs12 = kmalloc(VMCS12_SIZE, GFP_KERNEL);
7611 if (!vmx->nested.cached_vmcs12)
7612 goto out_cached_vmcs12;
7614 if (enable_shadow_vmcs) {
7615 shadow_vmcs = alloc_vmcs();
7617 goto out_shadow_vmcs;
7618 /* mark vmcs as shadow */
7619 shadow_vmcs->revision_id |= (1u << 31);
7620 /* init shadow vmcs */
7621 vmcs_clear(shadow_vmcs);
7622 vmx->vmcs01.shadow_vmcs = shadow_vmcs;
7625 hrtimer_init(&vmx->nested.preemption_timer, CLOCK_MONOTONIC,
7626 HRTIMER_MODE_REL_PINNED);
7627 vmx->nested.preemption_timer.function = vmx_preemption_timer_fn;
7629 vmx->nested.vmxon = true;
7633 kfree(vmx->nested.cached_vmcs12);
7636 free_loaded_vmcs(&vmx->nested.vmcs02);
7643 * Emulate the VMXON instruction.
7644 * Currently, we just remember that VMX is active, and do not save or even
7645 * inspect the argument to VMXON (the so-called "VMXON pointer") because we
7646 * do not currently need to store anything in that guest-allocated memory
7647 * region. Consequently, VMCLEAR and VMPTRLD also do not verify that the their
7648 * argument is different from the VMXON pointer (which the spec says they do).
7650 static int handle_vmon(struct kvm_vcpu *vcpu)
7655 struct vcpu_vmx *vmx = to_vmx(vcpu);
7656 const u64 VMXON_NEEDED_FEATURES = FEATURE_CONTROL_LOCKED
7657 | FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
7660 * The Intel VMX Instruction Reference lists a bunch of bits that are
7661 * prerequisite to running VMXON, most notably cr4.VMXE must be set to
7662 * 1 (see vmx_set_cr4() for when we allow the guest to set this).
7663 * Otherwise, we should fail with #UD. But most faulting conditions
7664 * have already been checked by hardware, prior to the VM-exit for
7665 * VMXON. We do test guest cr4.VMXE because processor CR4 always has
7666 * that bit set to 1 in non-root mode.
7668 if (!kvm_read_cr4_bits(vcpu, X86_CR4_VMXE)) {
7669 kvm_queue_exception(vcpu, UD_VECTOR);
7673 if (vmx->nested.vmxon) {
7674 nested_vmx_failValid(vcpu, VMXERR_VMXON_IN_VMX_ROOT_OPERATION);
7675 return kvm_skip_emulated_instruction(vcpu);
7678 if ((vmx->msr_ia32_feature_control & VMXON_NEEDED_FEATURES)
7679 != VMXON_NEEDED_FEATURES) {
7680 kvm_inject_gp(vcpu, 0);
7684 if (nested_vmx_get_vmptr(vcpu, &vmptr))
7689 * The first 4 bytes of VMXON region contain the supported
7690 * VMCS revision identifier
7692 * Note - IA32_VMX_BASIC[48] will never be 1 for the nested case;
7693 * which replaces physical address width with 32
7695 if (!PAGE_ALIGNED(vmptr) || (vmptr >> cpuid_maxphyaddr(vcpu))) {
7696 nested_vmx_failInvalid(vcpu);
7697 return kvm_skip_emulated_instruction(vcpu);
7700 page = kvm_vcpu_gpa_to_page(vcpu, vmptr);
7701 if (is_error_page(page)) {
7702 nested_vmx_failInvalid(vcpu);
7703 return kvm_skip_emulated_instruction(vcpu);
7705 if (*(u32 *)kmap(page) != VMCS12_REVISION) {
7707 kvm_release_page_clean(page);
7708 nested_vmx_failInvalid(vcpu);
7709 return kvm_skip_emulated_instruction(vcpu);
7712 kvm_release_page_clean(page);
7714 vmx->nested.vmxon_ptr = vmptr;
7715 ret = enter_vmx_operation(vcpu);
7719 nested_vmx_succeed(vcpu);
7720 return kvm_skip_emulated_instruction(vcpu);
7724 * Intel's VMX Instruction Reference specifies a common set of prerequisites
7725 * for running VMX instructions (except VMXON, whose prerequisites are
7726 * slightly different). It also specifies what exception to inject otherwise.
7727 * Note that many of these exceptions have priority over VM exits, so they
7728 * don't have to be checked again here.
7730 static int nested_vmx_check_permission(struct kvm_vcpu *vcpu)
7732 if (!to_vmx(vcpu)->nested.vmxon) {
7733 kvm_queue_exception(vcpu, UD_VECTOR);
7739 static void vmx_disable_shadow_vmcs(struct vcpu_vmx *vmx)
7741 vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL, SECONDARY_EXEC_SHADOW_VMCS);
7742 vmcs_write64(VMCS_LINK_POINTER, -1ull);
7745 static inline void nested_release_vmcs12(struct vcpu_vmx *vmx)
7747 if (vmx->nested.current_vmptr == -1ull)
7750 if (enable_shadow_vmcs) {
7751 /* copy to memory all shadowed fields in case
7752 they were modified */
7753 copy_shadow_to_vmcs12(vmx);
7754 vmx->nested.sync_shadow_vmcs = false;
7755 vmx_disable_shadow_vmcs(vmx);
7757 vmx->nested.posted_intr_nv = -1;
7759 /* Flush VMCS12 to guest memory */
7760 kvm_vcpu_write_guest_page(&vmx->vcpu,
7761 vmx->nested.current_vmptr >> PAGE_SHIFT,
7762 vmx->nested.cached_vmcs12, 0, VMCS12_SIZE);
7764 vmx->nested.current_vmptr = -1ull;
7768 * Free whatever needs to be freed from vmx->nested when L1 goes down, or
7769 * just stops using VMX.
7771 static void free_nested(struct vcpu_vmx *vmx)
7773 if (!vmx->nested.vmxon && !vmx->nested.smm.vmxon)
7776 vmx->nested.vmxon = false;
7777 vmx->nested.smm.vmxon = false;
7778 free_vpid(vmx->nested.vpid02);
7779 vmx->nested.posted_intr_nv = -1;
7780 vmx->nested.current_vmptr = -1ull;
7781 if (enable_shadow_vmcs) {
7782 vmx_disable_shadow_vmcs(vmx);
7783 vmcs_clear(vmx->vmcs01.shadow_vmcs);
7784 free_vmcs(vmx->vmcs01.shadow_vmcs);
7785 vmx->vmcs01.shadow_vmcs = NULL;
7787 kfree(vmx->nested.cached_vmcs12);
7788 /* Unpin physical memory we referred to in the vmcs02 */
7789 if (vmx->nested.apic_access_page) {
7790 kvm_release_page_dirty(vmx->nested.apic_access_page);
7791 vmx->nested.apic_access_page = NULL;
7793 if (vmx->nested.virtual_apic_page) {
7794 kvm_release_page_dirty(vmx->nested.virtual_apic_page);
7795 vmx->nested.virtual_apic_page = NULL;
7797 if (vmx->nested.pi_desc_page) {
7798 kunmap(vmx->nested.pi_desc_page);
7799 kvm_release_page_dirty(vmx->nested.pi_desc_page);
7800 vmx->nested.pi_desc_page = NULL;
7801 vmx->nested.pi_desc = NULL;
7804 free_loaded_vmcs(&vmx->nested.vmcs02);
7807 /* Emulate the VMXOFF instruction */
7808 static int handle_vmoff(struct kvm_vcpu *vcpu)
7810 if (!nested_vmx_check_permission(vcpu))
7812 free_nested(to_vmx(vcpu));
7813 nested_vmx_succeed(vcpu);
7814 return kvm_skip_emulated_instruction(vcpu);
7817 /* Emulate the VMCLEAR instruction */
7818 static int handle_vmclear(struct kvm_vcpu *vcpu)
7820 struct vcpu_vmx *vmx = to_vmx(vcpu);
7824 if (!nested_vmx_check_permission(vcpu))
7827 if (nested_vmx_get_vmptr(vcpu, &vmptr))
7830 if (!PAGE_ALIGNED(vmptr) || (vmptr >> cpuid_maxphyaddr(vcpu))) {
7831 nested_vmx_failValid(vcpu, VMXERR_VMCLEAR_INVALID_ADDRESS);
7832 return kvm_skip_emulated_instruction(vcpu);
7835 if (vmptr == vmx->nested.vmxon_ptr) {
7836 nested_vmx_failValid(vcpu, VMXERR_VMCLEAR_VMXON_POINTER);
7837 return kvm_skip_emulated_instruction(vcpu);
7840 if (vmptr == vmx->nested.current_vmptr)
7841 nested_release_vmcs12(vmx);
7843 kvm_vcpu_write_guest(vcpu,
7844 vmptr + offsetof(struct vmcs12, launch_state),
7845 &zero, sizeof(zero));
7847 nested_vmx_succeed(vcpu);
7848 return kvm_skip_emulated_instruction(vcpu);
7851 static int nested_vmx_run(struct kvm_vcpu *vcpu, bool launch);
7853 /* Emulate the VMLAUNCH instruction */
7854 static int handle_vmlaunch(struct kvm_vcpu *vcpu)
7856 return nested_vmx_run(vcpu, true);
7859 /* Emulate the VMRESUME instruction */
7860 static int handle_vmresume(struct kvm_vcpu *vcpu)
7863 return nested_vmx_run(vcpu, false);
7867 * Read a vmcs12 field. Since these can have varying lengths and we return
7868 * one type, we chose the biggest type (u64) and zero-extend the return value
7869 * to that size. Note that the caller, handle_vmread, might need to use only
7870 * some of the bits we return here (e.g., on 32-bit guests, only 32 bits of
7871 * 64-bit fields are to be returned).
7873 static inline int vmcs12_read_any(struct kvm_vcpu *vcpu,
7874 unsigned long field, u64 *ret)
7876 short offset = vmcs_field_to_offset(field);
7882 p = ((char *)(get_vmcs12(vcpu))) + offset;
7884 switch (vmcs_field_width(field)) {
7885 case VMCS_FIELD_WIDTH_NATURAL_WIDTH:
7886 *ret = *((natural_width *)p);
7888 case VMCS_FIELD_WIDTH_U16:
7891 case VMCS_FIELD_WIDTH_U32:
7894 case VMCS_FIELD_WIDTH_U64:
7904 static inline int vmcs12_write_any(struct kvm_vcpu *vcpu,
7905 unsigned long field, u64 field_value){
7906 short offset = vmcs_field_to_offset(field);
7907 char *p = ((char *) get_vmcs12(vcpu)) + offset;
7911 switch (vmcs_field_width(field)) {
7912 case VMCS_FIELD_WIDTH_U16:
7913 *(u16 *)p = field_value;
7915 case VMCS_FIELD_WIDTH_U32:
7916 *(u32 *)p = field_value;
7918 case VMCS_FIELD_WIDTH_U64:
7919 *(u64 *)p = field_value;
7921 case VMCS_FIELD_WIDTH_NATURAL_WIDTH:
7922 *(natural_width *)p = field_value;
7931 static void copy_shadow_to_vmcs12(struct vcpu_vmx *vmx)
7934 unsigned long field;
7936 struct vmcs *shadow_vmcs = vmx->vmcs01.shadow_vmcs;
7937 const u16 *fields = shadow_read_write_fields;
7938 const int num_fields = max_shadow_read_write_fields;
7942 vmcs_load(shadow_vmcs);
7944 for (i = 0; i < num_fields; i++) {
7946 field_value = __vmcs_readl(field);
7947 vmcs12_write_any(&vmx->vcpu, field, field_value);
7950 vmcs_clear(shadow_vmcs);
7951 vmcs_load(vmx->loaded_vmcs->vmcs);
7956 static void copy_vmcs12_to_shadow(struct vcpu_vmx *vmx)
7958 const u16 *fields[] = {
7959 shadow_read_write_fields,
7960 shadow_read_only_fields
7962 const int max_fields[] = {
7963 max_shadow_read_write_fields,
7964 max_shadow_read_only_fields
7967 unsigned long field;
7968 u64 field_value = 0;
7969 struct vmcs *shadow_vmcs = vmx->vmcs01.shadow_vmcs;
7971 vmcs_load(shadow_vmcs);
7973 for (q = 0; q < ARRAY_SIZE(fields); q++) {
7974 for (i = 0; i < max_fields[q]; i++) {
7975 field = fields[q][i];
7976 vmcs12_read_any(&vmx->vcpu, field, &field_value);
7977 __vmcs_writel(field, field_value);
7981 vmcs_clear(shadow_vmcs);
7982 vmcs_load(vmx->loaded_vmcs->vmcs);
7986 * VMX instructions which assume a current vmcs12 (i.e., that VMPTRLD was
7987 * used before) all generate the same failure when it is missing.
7989 static int nested_vmx_check_vmcs12(struct kvm_vcpu *vcpu)
7991 struct vcpu_vmx *vmx = to_vmx(vcpu);
7992 if (vmx->nested.current_vmptr == -1ull) {
7993 nested_vmx_failInvalid(vcpu);
7999 static int handle_vmread(struct kvm_vcpu *vcpu)
8001 unsigned long field;
8003 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
8004 u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
8007 if (!nested_vmx_check_permission(vcpu))
8010 if (!nested_vmx_check_vmcs12(vcpu))
8011 return kvm_skip_emulated_instruction(vcpu);
8013 /* Decode instruction info and find the field to read */
8014 field = kvm_register_readl(vcpu, (((vmx_instruction_info) >> 28) & 0xf));
8015 /* Read the field, zero-extended to a u64 field_value */
8016 if (vmcs12_read_any(vcpu, field, &field_value) < 0) {
8017 nested_vmx_failValid(vcpu, VMXERR_UNSUPPORTED_VMCS_COMPONENT);
8018 return kvm_skip_emulated_instruction(vcpu);
8021 * Now copy part of this value to register or memory, as requested.
8022 * Note that the number of bits actually copied is 32 or 64 depending
8023 * on the guest's mode (32 or 64 bit), not on the given field's length.
8025 if (vmx_instruction_info & (1u << 10)) {
8026 kvm_register_writel(vcpu, (((vmx_instruction_info) >> 3) & 0xf),
8029 if (get_vmx_mem_address(vcpu, exit_qualification,
8030 vmx_instruction_info, true, &gva))
8032 /* _system ok, as hardware has verified cpl=0 */
8033 kvm_write_guest_virt_system(&vcpu->arch.emulate_ctxt, gva,
8034 &field_value, (is_long_mode(vcpu) ? 8 : 4), NULL);
8037 nested_vmx_succeed(vcpu);
8038 return kvm_skip_emulated_instruction(vcpu);
8042 static int handle_vmwrite(struct kvm_vcpu *vcpu)
8044 unsigned long field;
8046 struct vcpu_vmx *vmx = to_vmx(vcpu);
8047 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
8048 u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
8050 /* The value to write might be 32 or 64 bits, depending on L1's long
8051 * mode, and eventually we need to write that into a field of several
8052 * possible lengths. The code below first zero-extends the value to 64
8053 * bit (field_value), and then copies only the appropriate number of
8054 * bits into the vmcs12 field.
8056 u64 field_value = 0;
8057 struct x86_exception e;
8059 if (!nested_vmx_check_permission(vcpu))
8062 if (!nested_vmx_check_vmcs12(vcpu))
8063 return kvm_skip_emulated_instruction(vcpu);
8065 if (vmx_instruction_info & (1u << 10))
8066 field_value = kvm_register_readl(vcpu,
8067 (((vmx_instruction_info) >> 3) & 0xf));
8069 if (get_vmx_mem_address(vcpu, exit_qualification,
8070 vmx_instruction_info, false, &gva))
8072 if (kvm_read_guest_virt(&vcpu->arch.emulate_ctxt, gva,
8073 &field_value, (is_64_bit_mode(vcpu) ? 8 : 4), &e)) {
8074 kvm_inject_page_fault(vcpu, &e);
8080 field = kvm_register_readl(vcpu, (((vmx_instruction_info) >> 28) & 0xf));
8081 if (vmcs_field_readonly(field)) {
8082 nested_vmx_failValid(vcpu,
8083 VMXERR_VMWRITE_READ_ONLY_VMCS_COMPONENT);
8084 return kvm_skip_emulated_instruction(vcpu);
8087 if (vmcs12_write_any(vcpu, field, field_value) < 0) {
8088 nested_vmx_failValid(vcpu, VMXERR_UNSUPPORTED_VMCS_COMPONENT);
8089 return kvm_skip_emulated_instruction(vcpu);
8093 #define SHADOW_FIELD_RW(x) case x:
8094 #include "vmx_shadow_fields.h"
8096 * The fields that can be updated by L1 without a vmexit are
8097 * always updated in the vmcs02, the others go down the slow
8098 * path of prepare_vmcs02.
8102 vmx->nested.dirty_vmcs12 = true;
8106 nested_vmx_succeed(vcpu);
8107 return kvm_skip_emulated_instruction(vcpu);
8110 static void set_current_vmptr(struct vcpu_vmx *vmx, gpa_t vmptr)
8112 vmx->nested.current_vmptr = vmptr;
8113 if (enable_shadow_vmcs) {
8114 vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL,
8115 SECONDARY_EXEC_SHADOW_VMCS);
8116 vmcs_write64(VMCS_LINK_POINTER,
8117 __pa(vmx->vmcs01.shadow_vmcs));
8118 vmx->nested.sync_shadow_vmcs = true;
8120 vmx->nested.dirty_vmcs12 = true;
8123 /* Emulate the VMPTRLD instruction */
8124 static int handle_vmptrld(struct kvm_vcpu *vcpu)
8126 struct vcpu_vmx *vmx = to_vmx(vcpu);
8129 if (!nested_vmx_check_permission(vcpu))
8132 if (nested_vmx_get_vmptr(vcpu, &vmptr))
8135 if (!PAGE_ALIGNED(vmptr) || (vmptr >> cpuid_maxphyaddr(vcpu))) {
8136 nested_vmx_failValid(vcpu, VMXERR_VMPTRLD_INVALID_ADDRESS);
8137 return kvm_skip_emulated_instruction(vcpu);
8140 if (vmptr == vmx->nested.vmxon_ptr) {
8141 nested_vmx_failValid(vcpu, VMXERR_VMPTRLD_VMXON_POINTER);
8142 return kvm_skip_emulated_instruction(vcpu);
8145 if (vmx->nested.current_vmptr != vmptr) {
8146 struct vmcs12 *new_vmcs12;
8148 page = kvm_vcpu_gpa_to_page(vcpu, vmptr);
8149 if (is_error_page(page)) {
8150 nested_vmx_failInvalid(vcpu);
8151 return kvm_skip_emulated_instruction(vcpu);
8153 new_vmcs12 = kmap(page);
8154 if (new_vmcs12->revision_id != VMCS12_REVISION) {
8156 kvm_release_page_clean(page);
8157 nested_vmx_failValid(vcpu,
8158 VMXERR_VMPTRLD_INCORRECT_VMCS_REVISION_ID);
8159 return kvm_skip_emulated_instruction(vcpu);
8162 nested_release_vmcs12(vmx);
8164 * Load VMCS12 from guest memory since it is not already
8167 memcpy(vmx->nested.cached_vmcs12, new_vmcs12, VMCS12_SIZE);
8169 kvm_release_page_clean(page);
8171 set_current_vmptr(vmx, vmptr);
8174 nested_vmx_succeed(vcpu);
8175 return kvm_skip_emulated_instruction(vcpu);
8178 /* Emulate the VMPTRST instruction */
8179 static int handle_vmptrst(struct kvm_vcpu *vcpu)
8181 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
8182 u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
8184 struct x86_exception e;
8186 if (!nested_vmx_check_permission(vcpu))
8189 if (get_vmx_mem_address(vcpu, exit_qualification,
8190 vmx_instruction_info, true, &vmcs_gva))
8192 /* ok to use *_system, as hardware has verified cpl=0 */
8193 if (kvm_write_guest_virt_system(&vcpu->arch.emulate_ctxt, vmcs_gva,
8194 (void *)&to_vmx(vcpu)->nested.current_vmptr,
8196 kvm_inject_page_fault(vcpu, &e);
8199 nested_vmx_succeed(vcpu);
8200 return kvm_skip_emulated_instruction(vcpu);
8203 /* Emulate the INVEPT instruction */
8204 static int handle_invept(struct kvm_vcpu *vcpu)
8206 struct vcpu_vmx *vmx = to_vmx(vcpu);
8207 u32 vmx_instruction_info, types;
8210 struct x86_exception e;
8215 if (!(vmx->nested.msrs.secondary_ctls_high &
8216 SECONDARY_EXEC_ENABLE_EPT) ||
8217 !(vmx->nested.msrs.ept_caps & VMX_EPT_INVEPT_BIT)) {
8218 kvm_queue_exception(vcpu, UD_VECTOR);
8222 if (!nested_vmx_check_permission(vcpu))
8225 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
8226 type = kvm_register_readl(vcpu, (vmx_instruction_info >> 28) & 0xf);
8228 types = (vmx->nested.msrs.ept_caps >> VMX_EPT_EXTENT_SHIFT) & 6;
8230 if (type >= 32 || !(types & (1 << type))) {
8231 nested_vmx_failValid(vcpu,
8232 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
8233 return kvm_skip_emulated_instruction(vcpu);
8236 /* According to the Intel VMX instruction reference, the memory
8237 * operand is read even if it isn't needed (e.g., for type==global)
8239 if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
8240 vmx_instruction_info, false, &gva))
8242 if (kvm_read_guest_virt(&vcpu->arch.emulate_ctxt, gva, &operand,
8243 sizeof(operand), &e)) {
8244 kvm_inject_page_fault(vcpu, &e);
8249 case VMX_EPT_EXTENT_GLOBAL:
8251 * TODO: track mappings and invalidate
8252 * single context requests appropriately
8254 case VMX_EPT_EXTENT_CONTEXT:
8255 kvm_mmu_sync_roots(vcpu);
8256 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
8257 nested_vmx_succeed(vcpu);
8264 return kvm_skip_emulated_instruction(vcpu);
8267 static int handle_invvpid(struct kvm_vcpu *vcpu)
8269 struct vcpu_vmx *vmx = to_vmx(vcpu);
8270 u32 vmx_instruction_info;
8271 unsigned long type, types;
8273 struct x86_exception e;
8279 if (!(vmx->nested.msrs.secondary_ctls_high &
8280 SECONDARY_EXEC_ENABLE_VPID) ||
8281 !(vmx->nested.msrs.vpid_caps & VMX_VPID_INVVPID_BIT)) {
8282 kvm_queue_exception(vcpu, UD_VECTOR);
8286 if (!nested_vmx_check_permission(vcpu))
8289 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
8290 type = kvm_register_readl(vcpu, (vmx_instruction_info >> 28) & 0xf);
8292 types = (vmx->nested.msrs.vpid_caps &
8293 VMX_VPID_EXTENT_SUPPORTED_MASK) >> 8;
8295 if (type >= 32 || !(types & (1 << type))) {
8296 nested_vmx_failValid(vcpu,
8297 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
8298 return kvm_skip_emulated_instruction(vcpu);
8301 /* according to the intel vmx instruction reference, the memory
8302 * operand is read even if it isn't needed (e.g., for type==global)
8304 if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
8305 vmx_instruction_info, false, &gva))
8307 if (kvm_read_guest_virt(&vcpu->arch.emulate_ctxt, gva, &operand,
8308 sizeof(operand), &e)) {
8309 kvm_inject_page_fault(vcpu, &e);
8312 if (operand.vpid >> 16) {
8313 nested_vmx_failValid(vcpu,
8314 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
8315 return kvm_skip_emulated_instruction(vcpu);
8319 case VMX_VPID_EXTENT_INDIVIDUAL_ADDR:
8320 if (is_noncanonical_address(operand.gla, vcpu)) {
8321 nested_vmx_failValid(vcpu,
8322 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
8323 return kvm_skip_emulated_instruction(vcpu);
8326 case VMX_VPID_EXTENT_SINGLE_CONTEXT:
8327 case VMX_VPID_EXTENT_SINGLE_NON_GLOBAL:
8328 if (!operand.vpid) {
8329 nested_vmx_failValid(vcpu,
8330 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
8331 return kvm_skip_emulated_instruction(vcpu);
8334 case VMX_VPID_EXTENT_ALL_CONTEXT:
8338 return kvm_skip_emulated_instruction(vcpu);
8341 __vmx_flush_tlb(vcpu, vmx->nested.vpid02, true);
8342 nested_vmx_succeed(vcpu);
8344 return kvm_skip_emulated_instruction(vcpu);
8347 static int handle_pml_full(struct kvm_vcpu *vcpu)
8349 unsigned long exit_qualification;
8351 trace_kvm_pml_full(vcpu->vcpu_id);
8353 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
8356 * PML buffer FULL happened while executing iret from NMI,
8357 * "blocked by NMI" bit has to be set before next VM entry.
8359 if (!(to_vmx(vcpu)->idt_vectoring_info & VECTORING_INFO_VALID_MASK) &&
8361 (exit_qualification & INTR_INFO_UNBLOCK_NMI))
8362 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
8363 GUEST_INTR_STATE_NMI);
8366 * PML buffer already flushed at beginning of VMEXIT. Nothing to do
8367 * here.., and there's no userspace involvement needed for PML.
8372 static int handle_preemption_timer(struct kvm_vcpu *vcpu)
8374 kvm_lapic_expired_hv_timer(vcpu);
8378 static bool valid_ept_address(struct kvm_vcpu *vcpu, u64 address)
8380 struct vcpu_vmx *vmx = to_vmx(vcpu);
8381 int maxphyaddr = cpuid_maxphyaddr(vcpu);
8383 /* Check for memory type validity */
8384 switch (address & VMX_EPTP_MT_MASK) {
8385 case VMX_EPTP_MT_UC:
8386 if (!(vmx->nested.msrs.ept_caps & VMX_EPTP_UC_BIT))
8389 case VMX_EPTP_MT_WB:
8390 if (!(vmx->nested.msrs.ept_caps & VMX_EPTP_WB_BIT))
8397 /* only 4 levels page-walk length are valid */
8398 if ((address & VMX_EPTP_PWL_MASK) != VMX_EPTP_PWL_4)
8401 /* Reserved bits should not be set */
8402 if (address >> maxphyaddr || ((address >> 7) & 0x1f))
8405 /* AD, if set, should be supported */
8406 if (address & VMX_EPTP_AD_ENABLE_BIT) {
8407 if (!(vmx->nested.msrs.ept_caps & VMX_EPT_AD_BIT))
8414 static int nested_vmx_eptp_switching(struct kvm_vcpu *vcpu,
8415 struct vmcs12 *vmcs12)
8417 u32 index = vcpu->arch.regs[VCPU_REGS_RCX];
8419 bool accessed_dirty;
8420 struct kvm_mmu *mmu = vcpu->arch.walk_mmu;
8422 if (!nested_cpu_has_eptp_switching(vmcs12) ||
8423 !nested_cpu_has_ept(vmcs12))
8426 if (index >= VMFUNC_EPTP_ENTRIES)
8430 if (kvm_vcpu_read_guest_page(vcpu, vmcs12->eptp_list_address >> PAGE_SHIFT,
8431 &address, index * 8, 8))
8434 accessed_dirty = !!(address & VMX_EPTP_AD_ENABLE_BIT);
8437 * If the (L2) guest does a vmfunc to the currently
8438 * active ept pointer, we don't have to do anything else
8440 if (vmcs12->ept_pointer != address) {
8441 if (!valid_ept_address(vcpu, address))
8444 kvm_mmu_unload(vcpu);
8445 mmu->ept_ad = accessed_dirty;
8446 mmu->base_role.ad_disabled = !accessed_dirty;
8447 vmcs12->ept_pointer = address;
8449 * TODO: Check what's the correct approach in case
8450 * mmu reload fails. Currently, we just let the next
8451 * reload potentially fail
8453 kvm_mmu_reload(vcpu);
8459 static int handle_vmfunc(struct kvm_vcpu *vcpu)
8461 struct vcpu_vmx *vmx = to_vmx(vcpu);
8462 struct vmcs12 *vmcs12;
8463 u32 function = vcpu->arch.regs[VCPU_REGS_RAX];
8466 * VMFUNC is only supported for nested guests, but we always enable the
8467 * secondary control for simplicity; for non-nested mode, fake that we
8468 * didn't by injecting #UD.
8470 if (!is_guest_mode(vcpu)) {
8471 kvm_queue_exception(vcpu, UD_VECTOR);
8475 vmcs12 = get_vmcs12(vcpu);
8476 if ((vmcs12->vm_function_control & (1 << function)) == 0)
8481 if (nested_vmx_eptp_switching(vcpu, vmcs12))
8487 return kvm_skip_emulated_instruction(vcpu);
8490 nested_vmx_vmexit(vcpu, vmx->exit_reason,
8491 vmcs_read32(VM_EXIT_INTR_INFO),
8492 vmcs_readl(EXIT_QUALIFICATION));
8497 * The exit handlers return 1 if the exit was handled fully and guest execution
8498 * may resume. Otherwise they set the kvm_run parameter to indicate what needs
8499 * to be done to userspace and return 0.
8501 static int (*const kvm_vmx_exit_handlers[])(struct kvm_vcpu *vcpu) = {
8502 [EXIT_REASON_EXCEPTION_NMI] = handle_exception,
8503 [EXIT_REASON_EXTERNAL_INTERRUPT] = handle_external_interrupt,
8504 [EXIT_REASON_TRIPLE_FAULT] = handle_triple_fault,
8505 [EXIT_REASON_NMI_WINDOW] = handle_nmi_window,
8506 [EXIT_REASON_IO_INSTRUCTION] = handle_io,
8507 [EXIT_REASON_CR_ACCESS] = handle_cr,
8508 [EXIT_REASON_DR_ACCESS] = handle_dr,
8509 [EXIT_REASON_CPUID] = handle_cpuid,
8510 [EXIT_REASON_MSR_READ] = handle_rdmsr,
8511 [EXIT_REASON_MSR_WRITE] = handle_wrmsr,
8512 [EXIT_REASON_PENDING_INTERRUPT] = handle_interrupt_window,
8513 [EXIT_REASON_HLT] = handle_halt,
8514 [EXIT_REASON_INVD] = handle_invd,
8515 [EXIT_REASON_INVLPG] = handle_invlpg,
8516 [EXIT_REASON_RDPMC] = handle_rdpmc,
8517 [EXIT_REASON_VMCALL] = handle_vmcall,
8518 [EXIT_REASON_VMCLEAR] = handle_vmclear,
8519 [EXIT_REASON_VMLAUNCH] = handle_vmlaunch,
8520 [EXIT_REASON_VMPTRLD] = handle_vmptrld,
8521 [EXIT_REASON_VMPTRST] = handle_vmptrst,
8522 [EXIT_REASON_VMREAD] = handle_vmread,
8523 [EXIT_REASON_VMRESUME] = handle_vmresume,
8524 [EXIT_REASON_VMWRITE] = handle_vmwrite,
8525 [EXIT_REASON_VMOFF] = handle_vmoff,
8526 [EXIT_REASON_VMON] = handle_vmon,
8527 [EXIT_REASON_TPR_BELOW_THRESHOLD] = handle_tpr_below_threshold,
8528 [EXIT_REASON_APIC_ACCESS] = handle_apic_access,
8529 [EXIT_REASON_APIC_WRITE] = handle_apic_write,
8530 [EXIT_REASON_EOI_INDUCED] = handle_apic_eoi_induced,
8531 [EXIT_REASON_WBINVD] = handle_wbinvd,
8532 [EXIT_REASON_XSETBV] = handle_xsetbv,
8533 [EXIT_REASON_TASK_SWITCH] = handle_task_switch,
8534 [EXIT_REASON_MCE_DURING_VMENTRY] = handle_machine_check,
8535 [EXIT_REASON_GDTR_IDTR] = handle_desc,
8536 [EXIT_REASON_LDTR_TR] = handle_desc,
8537 [EXIT_REASON_EPT_VIOLATION] = handle_ept_violation,
8538 [EXIT_REASON_EPT_MISCONFIG] = handle_ept_misconfig,
8539 [EXIT_REASON_PAUSE_INSTRUCTION] = handle_pause,
8540 [EXIT_REASON_MWAIT_INSTRUCTION] = handle_mwait,
8541 [EXIT_REASON_MONITOR_TRAP_FLAG] = handle_monitor_trap,
8542 [EXIT_REASON_MONITOR_INSTRUCTION] = handle_monitor,
8543 [EXIT_REASON_INVEPT] = handle_invept,
8544 [EXIT_REASON_INVVPID] = handle_invvpid,
8545 [EXIT_REASON_RDRAND] = handle_invalid_op,
8546 [EXIT_REASON_RDSEED] = handle_invalid_op,
8547 [EXIT_REASON_XSAVES] = handle_xsaves,
8548 [EXIT_REASON_XRSTORS] = handle_xrstors,
8549 [EXIT_REASON_PML_FULL] = handle_pml_full,
8550 [EXIT_REASON_VMFUNC] = handle_vmfunc,
8551 [EXIT_REASON_PREEMPTION_TIMER] = handle_preemption_timer,
8554 static const int kvm_vmx_max_exit_handlers =
8555 ARRAY_SIZE(kvm_vmx_exit_handlers);
8557 static bool nested_vmx_exit_handled_io(struct kvm_vcpu *vcpu,
8558 struct vmcs12 *vmcs12)
8560 unsigned long exit_qualification;
8561 gpa_t bitmap, last_bitmap;
8566 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_IO_BITMAPS))
8567 return nested_cpu_has(vmcs12, CPU_BASED_UNCOND_IO_EXITING);
8569 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
8571 port = exit_qualification >> 16;
8572 size = (exit_qualification & 7) + 1;
8574 last_bitmap = (gpa_t)-1;
8579 bitmap = vmcs12->io_bitmap_a;
8580 else if (port < 0x10000)
8581 bitmap = vmcs12->io_bitmap_b;
8584 bitmap += (port & 0x7fff) / 8;
8586 if (last_bitmap != bitmap)
8587 if (kvm_vcpu_read_guest(vcpu, bitmap, &b, 1))
8589 if (b & (1 << (port & 7)))
8594 last_bitmap = bitmap;
8601 * Return 1 if we should exit from L2 to L1 to handle an MSR access access,
8602 * rather than handle it ourselves in L0. I.e., check whether L1 expressed
8603 * disinterest in the current event (read or write a specific MSR) by using an
8604 * MSR bitmap. This may be the case even when L0 doesn't use MSR bitmaps.
8606 static bool nested_vmx_exit_handled_msr(struct kvm_vcpu *vcpu,
8607 struct vmcs12 *vmcs12, u32 exit_reason)
8609 u32 msr_index = vcpu->arch.regs[VCPU_REGS_RCX];
8612 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS))
8616 * The MSR_BITMAP page is divided into four 1024-byte bitmaps,
8617 * for the four combinations of read/write and low/high MSR numbers.
8618 * First we need to figure out which of the four to use:
8620 bitmap = vmcs12->msr_bitmap;
8621 if (exit_reason == EXIT_REASON_MSR_WRITE)
8623 if (msr_index >= 0xc0000000) {
8624 msr_index -= 0xc0000000;
8628 /* Then read the msr_index'th bit from this bitmap: */
8629 if (msr_index < 1024*8) {
8631 if (kvm_vcpu_read_guest(vcpu, bitmap + msr_index/8, &b, 1))
8633 return 1 & (b >> (msr_index & 7));
8635 return true; /* let L1 handle the wrong parameter */
8639 * Return 1 if we should exit from L2 to L1 to handle a CR access exit,
8640 * rather than handle it ourselves in L0. I.e., check if L1 wanted to
8641 * intercept (via guest_host_mask etc.) the current event.
8643 static bool nested_vmx_exit_handled_cr(struct kvm_vcpu *vcpu,
8644 struct vmcs12 *vmcs12)
8646 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
8647 int cr = exit_qualification & 15;
8651 switch ((exit_qualification >> 4) & 3) {
8652 case 0: /* mov to cr */
8653 reg = (exit_qualification >> 8) & 15;
8654 val = kvm_register_readl(vcpu, reg);
8657 if (vmcs12->cr0_guest_host_mask &
8658 (val ^ vmcs12->cr0_read_shadow))
8662 if ((vmcs12->cr3_target_count >= 1 &&
8663 vmcs12->cr3_target_value0 == val) ||
8664 (vmcs12->cr3_target_count >= 2 &&
8665 vmcs12->cr3_target_value1 == val) ||
8666 (vmcs12->cr3_target_count >= 3 &&
8667 vmcs12->cr3_target_value2 == val) ||
8668 (vmcs12->cr3_target_count >= 4 &&
8669 vmcs12->cr3_target_value3 == val))
8671 if (nested_cpu_has(vmcs12, CPU_BASED_CR3_LOAD_EXITING))
8675 if (vmcs12->cr4_guest_host_mask &
8676 (vmcs12->cr4_read_shadow ^ val))
8680 if (nested_cpu_has(vmcs12, CPU_BASED_CR8_LOAD_EXITING))
8686 if ((vmcs12->cr0_guest_host_mask & X86_CR0_TS) &&
8687 (vmcs12->cr0_read_shadow & X86_CR0_TS))
8690 case 1: /* mov from cr */
8693 if (vmcs12->cpu_based_vm_exec_control &
8694 CPU_BASED_CR3_STORE_EXITING)
8698 if (vmcs12->cpu_based_vm_exec_control &
8699 CPU_BASED_CR8_STORE_EXITING)
8706 * lmsw can change bits 1..3 of cr0, and only set bit 0 of
8707 * cr0. Other attempted changes are ignored, with no exit.
8709 val = (exit_qualification >> LMSW_SOURCE_DATA_SHIFT) & 0x0f;
8710 if (vmcs12->cr0_guest_host_mask & 0xe &
8711 (val ^ vmcs12->cr0_read_shadow))
8713 if ((vmcs12->cr0_guest_host_mask & 0x1) &&
8714 !(vmcs12->cr0_read_shadow & 0x1) &&
8723 * Return 1 if we should exit from L2 to L1 to handle an exit, or 0 if we
8724 * should handle it ourselves in L0 (and then continue L2). Only call this
8725 * when in is_guest_mode (L2).
8727 static bool nested_vmx_exit_reflected(struct kvm_vcpu *vcpu, u32 exit_reason)
8729 u32 intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
8730 struct vcpu_vmx *vmx = to_vmx(vcpu);
8731 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
8733 if (vmx->nested.nested_run_pending)
8736 if (unlikely(vmx->fail)) {
8737 pr_info_ratelimited("%s failed vm entry %x\n", __func__,
8738 vmcs_read32(VM_INSTRUCTION_ERROR));
8743 * The host physical addresses of some pages of guest memory
8744 * are loaded into the vmcs02 (e.g. vmcs12's Virtual APIC
8745 * Page). The CPU may write to these pages via their host
8746 * physical address while L2 is running, bypassing any
8747 * address-translation-based dirty tracking (e.g. EPT write
8750 * Mark them dirty on every exit from L2 to prevent them from
8751 * getting out of sync with dirty tracking.
8753 nested_mark_vmcs12_pages_dirty(vcpu);
8755 trace_kvm_nested_vmexit(kvm_rip_read(vcpu), exit_reason,
8756 vmcs_readl(EXIT_QUALIFICATION),
8757 vmx->idt_vectoring_info,
8759 vmcs_read32(VM_EXIT_INTR_ERROR_CODE),
8762 switch (exit_reason) {
8763 case EXIT_REASON_EXCEPTION_NMI:
8764 if (is_nmi(intr_info))
8766 else if (is_page_fault(intr_info))
8767 return !vmx->vcpu.arch.apf.host_apf_reason && enable_ept;
8768 else if (is_no_device(intr_info) &&
8769 !(vmcs12->guest_cr0 & X86_CR0_TS))
8771 else if (is_debug(intr_info) &&
8773 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))
8775 else if (is_breakpoint(intr_info) &&
8776 vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
8778 return vmcs12->exception_bitmap &
8779 (1u << (intr_info & INTR_INFO_VECTOR_MASK));
8780 case EXIT_REASON_EXTERNAL_INTERRUPT:
8782 case EXIT_REASON_TRIPLE_FAULT:
8784 case EXIT_REASON_PENDING_INTERRUPT:
8785 return nested_cpu_has(vmcs12, CPU_BASED_VIRTUAL_INTR_PENDING);
8786 case EXIT_REASON_NMI_WINDOW:
8787 return nested_cpu_has(vmcs12, CPU_BASED_VIRTUAL_NMI_PENDING);
8788 case EXIT_REASON_TASK_SWITCH:
8790 case EXIT_REASON_CPUID:
8792 case EXIT_REASON_HLT:
8793 return nested_cpu_has(vmcs12, CPU_BASED_HLT_EXITING);
8794 case EXIT_REASON_INVD:
8796 case EXIT_REASON_INVLPG:
8797 return nested_cpu_has(vmcs12, CPU_BASED_INVLPG_EXITING);
8798 case EXIT_REASON_RDPMC:
8799 return nested_cpu_has(vmcs12, CPU_BASED_RDPMC_EXITING);
8800 case EXIT_REASON_RDRAND:
8801 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_RDRAND_EXITING);
8802 case EXIT_REASON_RDSEED:
8803 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_RDSEED_EXITING);
8804 case EXIT_REASON_RDTSC: case EXIT_REASON_RDTSCP:
8805 return nested_cpu_has(vmcs12, CPU_BASED_RDTSC_EXITING);
8806 case EXIT_REASON_VMCALL: case EXIT_REASON_VMCLEAR:
8807 case EXIT_REASON_VMLAUNCH: case EXIT_REASON_VMPTRLD:
8808 case EXIT_REASON_VMPTRST: case EXIT_REASON_VMREAD:
8809 case EXIT_REASON_VMRESUME: case EXIT_REASON_VMWRITE:
8810 case EXIT_REASON_VMOFF: case EXIT_REASON_VMON:
8811 case EXIT_REASON_INVEPT: case EXIT_REASON_INVVPID:
8813 * VMX instructions trap unconditionally. This allows L1 to
8814 * emulate them for its L2 guest, i.e., allows 3-level nesting!
8817 case EXIT_REASON_CR_ACCESS:
8818 return nested_vmx_exit_handled_cr(vcpu, vmcs12);
8819 case EXIT_REASON_DR_ACCESS:
8820 return nested_cpu_has(vmcs12, CPU_BASED_MOV_DR_EXITING);
8821 case EXIT_REASON_IO_INSTRUCTION:
8822 return nested_vmx_exit_handled_io(vcpu, vmcs12);
8823 case EXIT_REASON_GDTR_IDTR: case EXIT_REASON_LDTR_TR:
8824 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_DESC);
8825 case EXIT_REASON_MSR_READ:
8826 case EXIT_REASON_MSR_WRITE:
8827 return nested_vmx_exit_handled_msr(vcpu, vmcs12, exit_reason);
8828 case EXIT_REASON_INVALID_STATE:
8830 case EXIT_REASON_MWAIT_INSTRUCTION:
8831 return nested_cpu_has(vmcs12, CPU_BASED_MWAIT_EXITING);
8832 case EXIT_REASON_MONITOR_TRAP_FLAG:
8833 return nested_cpu_has(vmcs12, CPU_BASED_MONITOR_TRAP_FLAG);
8834 case EXIT_REASON_MONITOR_INSTRUCTION:
8835 return nested_cpu_has(vmcs12, CPU_BASED_MONITOR_EXITING);
8836 case EXIT_REASON_PAUSE_INSTRUCTION:
8837 return nested_cpu_has(vmcs12, CPU_BASED_PAUSE_EXITING) ||
8838 nested_cpu_has2(vmcs12,
8839 SECONDARY_EXEC_PAUSE_LOOP_EXITING);
8840 case EXIT_REASON_MCE_DURING_VMENTRY:
8842 case EXIT_REASON_TPR_BELOW_THRESHOLD:
8843 return nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW);
8844 case EXIT_REASON_APIC_ACCESS:
8845 return nested_cpu_has2(vmcs12,
8846 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES);
8847 case EXIT_REASON_APIC_WRITE:
8848 case EXIT_REASON_EOI_INDUCED:
8849 /* apic_write and eoi_induced should exit unconditionally. */
8851 case EXIT_REASON_EPT_VIOLATION:
8853 * L0 always deals with the EPT violation. If nested EPT is
8854 * used, and the nested mmu code discovers that the address is
8855 * missing in the guest EPT table (EPT12), the EPT violation
8856 * will be injected with nested_ept_inject_page_fault()
8859 case EXIT_REASON_EPT_MISCONFIG:
8861 * L2 never uses directly L1's EPT, but rather L0's own EPT
8862 * table (shadow on EPT) or a merged EPT table that L0 built
8863 * (EPT on EPT). So any problems with the structure of the
8864 * table is L0's fault.
8867 case EXIT_REASON_INVPCID:
8869 nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_INVPCID) &&
8870 nested_cpu_has(vmcs12, CPU_BASED_INVLPG_EXITING);
8871 case EXIT_REASON_WBINVD:
8872 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_WBINVD_EXITING);
8873 case EXIT_REASON_XSETBV:
8875 case EXIT_REASON_XSAVES: case EXIT_REASON_XRSTORS:
8877 * This should never happen, since it is not possible to
8878 * set XSS to a non-zero value---neither in L1 nor in L2.
8879 * If if it were, XSS would have to be checked against
8880 * the XSS exit bitmap in vmcs12.
8882 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_XSAVES);
8883 case EXIT_REASON_PREEMPTION_TIMER:
8885 case EXIT_REASON_PML_FULL:
8886 /* We emulate PML support to L1. */
8888 case EXIT_REASON_VMFUNC:
8889 /* VM functions are emulated through L2->L0 vmexits. */
8896 static int nested_vmx_reflect_vmexit(struct kvm_vcpu *vcpu, u32 exit_reason)
8898 u32 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
8901 * At this point, the exit interruption info in exit_intr_info
8902 * is only valid for EXCEPTION_NMI exits. For EXTERNAL_INTERRUPT
8903 * we need to query the in-kernel LAPIC.
8905 WARN_ON(exit_reason == EXIT_REASON_EXTERNAL_INTERRUPT);
8906 if ((exit_intr_info &
8907 (INTR_INFO_VALID_MASK | INTR_INFO_DELIVER_CODE_MASK)) ==
8908 (INTR_INFO_VALID_MASK | INTR_INFO_DELIVER_CODE_MASK)) {
8909 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
8910 vmcs12->vm_exit_intr_error_code =
8911 vmcs_read32(VM_EXIT_INTR_ERROR_CODE);
8914 nested_vmx_vmexit(vcpu, exit_reason, exit_intr_info,
8915 vmcs_readl(EXIT_QUALIFICATION));
8919 static void vmx_get_exit_info(struct kvm_vcpu *vcpu, u64 *info1, u64 *info2)
8921 *info1 = vmcs_readl(EXIT_QUALIFICATION);
8922 *info2 = vmcs_read32(VM_EXIT_INTR_INFO);
8925 static void vmx_destroy_pml_buffer(struct vcpu_vmx *vmx)
8928 __free_page(vmx->pml_pg);
8933 static void vmx_flush_pml_buffer(struct kvm_vcpu *vcpu)
8935 struct vcpu_vmx *vmx = to_vmx(vcpu);
8939 pml_idx = vmcs_read16(GUEST_PML_INDEX);
8941 /* Do nothing if PML buffer is empty */
8942 if (pml_idx == (PML_ENTITY_NUM - 1))
8945 /* PML index always points to next available PML buffer entity */
8946 if (pml_idx >= PML_ENTITY_NUM)
8951 pml_buf = page_address(vmx->pml_pg);
8952 for (; pml_idx < PML_ENTITY_NUM; pml_idx++) {
8955 gpa = pml_buf[pml_idx];
8956 WARN_ON(gpa & (PAGE_SIZE - 1));
8957 kvm_vcpu_mark_page_dirty(vcpu, gpa >> PAGE_SHIFT);
8960 /* reset PML index */
8961 vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1);
8965 * Flush all vcpus' PML buffer and update logged GPAs to dirty_bitmap.
8966 * Called before reporting dirty_bitmap to userspace.
8968 static void kvm_flush_pml_buffers(struct kvm *kvm)
8971 struct kvm_vcpu *vcpu;
8973 * We only need to kick vcpu out of guest mode here, as PML buffer
8974 * is flushed at beginning of all VMEXITs, and it's obvious that only
8975 * vcpus running in guest are possible to have unflushed GPAs in PML
8978 kvm_for_each_vcpu(i, vcpu, kvm)
8979 kvm_vcpu_kick(vcpu);
8982 static void vmx_dump_sel(char *name, uint32_t sel)
8984 pr_err("%s sel=0x%04x, attr=0x%05x, limit=0x%08x, base=0x%016lx\n",
8985 name, vmcs_read16(sel),
8986 vmcs_read32(sel + GUEST_ES_AR_BYTES - GUEST_ES_SELECTOR),
8987 vmcs_read32(sel + GUEST_ES_LIMIT - GUEST_ES_SELECTOR),
8988 vmcs_readl(sel + GUEST_ES_BASE - GUEST_ES_SELECTOR));
8991 static void vmx_dump_dtsel(char *name, uint32_t limit)
8993 pr_err("%s limit=0x%08x, base=0x%016lx\n",
8994 name, vmcs_read32(limit),
8995 vmcs_readl(limit + GUEST_GDTR_BASE - GUEST_GDTR_LIMIT));
8998 static void dump_vmcs(void)
9000 u32 vmentry_ctl = vmcs_read32(VM_ENTRY_CONTROLS);
9001 u32 vmexit_ctl = vmcs_read32(VM_EXIT_CONTROLS);
9002 u32 cpu_based_exec_ctrl = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
9003 u32 pin_based_exec_ctrl = vmcs_read32(PIN_BASED_VM_EXEC_CONTROL);
9004 u32 secondary_exec_control = 0;
9005 unsigned long cr4 = vmcs_readl(GUEST_CR4);
9006 u64 efer = vmcs_read64(GUEST_IA32_EFER);
9009 if (cpu_has_secondary_exec_ctrls())
9010 secondary_exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
9012 pr_err("*** Guest State ***\n");
9013 pr_err("CR0: actual=0x%016lx, shadow=0x%016lx, gh_mask=%016lx\n",
9014 vmcs_readl(GUEST_CR0), vmcs_readl(CR0_READ_SHADOW),
9015 vmcs_readl(CR0_GUEST_HOST_MASK));
9016 pr_err("CR4: actual=0x%016lx, shadow=0x%016lx, gh_mask=%016lx\n",
9017 cr4, vmcs_readl(CR4_READ_SHADOW), vmcs_readl(CR4_GUEST_HOST_MASK));
9018 pr_err("CR3 = 0x%016lx\n", vmcs_readl(GUEST_CR3));
9019 if ((secondary_exec_control & SECONDARY_EXEC_ENABLE_EPT) &&
9020 (cr4 & X86_CR4_PAE) && !(efer & EFER_LMA))
9022 pr_err("PDPTR0 = 0x%016llx PDPTR1 = 0x%016llx\n",
9023 vmcs_read64(GUEST_PDPTR0), vmcs_read64(GUEST_PDPTR1));
9024 pr_err("PDPTR2 = 0x%016llx PDPTR3 = 0x%016llx\n",
9025 vmcs_read64(GUEST_PDPTR2), vmcs_read64(GUEST_PDPTR3));
9027 pr_err("RSP = 0x%016lx RIP = 0x%016lx\n",
9028 vmcs_readl(GUEST_RSP), vmcs_readl(GUEST_RIP));
9029 pr_err("RFLAGS=0x%08lx DR7 = 0x%016lx\n",
9030 vmcs_readl(GUEST_RFLAGS), vmcs_readl(GUEST_DR7));
9031 pr_err("Sysenter RSP=%016lx CS:RIP=%04x:%016lx\n",
9032 vmcs_readl(GUEST_SYSENTER_ESP),
9033 vmcs_read32(GUEST_SYSENTER_CS), vmcs_readl(GUEST_SYSENTER_EIP));
9034 vmx_dump_sel("CS: ", GUEST_CS_SELECTOR);
9035 vmx_dump_sel("DS: ", GUEST_DS_SELECTOR);
9036 vmx_dump_sel("SS: ", GUEST_SS_SELECTOR);
9037 vmx_dump_sel("ES: ", GUEST_ES_SELECTOR);
9038 vmx_dump_sel("FS: ", GUEST_FS_SELECTOR);
9039 vmx_dump_sel("GS: ", GUEST_GS_SELECTOR);
9040 vmx_dump_dtsel("GDTR:", GUEST_GDTR_LIMIT);
9041 vmx_dump_sel("LDTR:", GUEST_LDTR_SELECTOR);
9042 vmx_dump_dtsel("IDTR:", GUEST_IDTR_LIMIT);
9043 vmx_dump_sel("TR: ", GUEST_TR_SELECTOR);
9044 if ((vmexit_ctl & (VM_EXIT_SAVE_IA32_PAT | VM_EXIT_SAVE_IA32_EFER)) ||
9045 (vmentry_ctl & (VM_ENTRY_LOAD_IA32_PAT | VM_ENTRY_LOAD_IA32_EFER)))
9046 pr_err("EFER = 0x%016llx PAT = 0x%016llx\n",
9047 efer, vmcs_read64(GUEST_IA32_PAT));
9048 pr_err("DebugCtl = 0x%016llx DebugExceptions = 0x%016lx\n",
9049 vmcs_read64(GUEST_IA32_DEBUGCTL),
9050 vmcs_readl(GUEST_PENDING_DBG_EXCEPTIONS));
9051 if (cpu_has_load_perf_global_ctrl &&
9052 vmentry_ctl & VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL)
9053 pr_err("PerfGlobCtl = 0x%016llx\n",
9054 vmcs_read64(GUEST_IA32_PERF_GLOBAL_CTRL));
9055 if (vmentry_ctl & VM_ENTRY_LOAD_BNDCFGS)
9056 pr_err("BndCfgS = 0x%016llx\n", vmcs_read64(GUEST_BNDCFGS));
9057 pr_err("Interruptibility = %08x ActivityState = %08x\n",
9058 vmcs_read32(GUEST_INTERRUPTIBILITY_INFO),
9059 vmcs_read32(GUEST_ACTIVITY_STATE));
9060 if (secondary_exec_control & SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY)
9061 pr_err("InterruptStatus = %04x\n",
9062 vmcs_read16(GUEST_INTR_STATUS));
9064 pr_err("*** Host State ***\n");
9065 pr_err("RIP = 0x%016lx RSP = 0x%016lx\n",
9066 vmcs_readl(HOST_RIP), vmcs_readl(HOST_RSP));
9067 pr_err("CS=%04x SS=%04x DS=%04x ES=%04x FS=%04x GS=%04x TR=%04x\n",
9068 vmcs_read16(HOST_CS_SELECTOR), vmcs_read16(HOST_SS_SELECTOR),
9069 vmcs_read16(HOST_DS_SELECTOR), vmcs_read16(HOST_ES_SELECTOR),
9070 vmcs_read16(HOST_FS_SELECTOR), vmcs_read16(HOST_GS_SELECTOR),
9071 vmcs_read16(HOST_TR_SELECTOR));
9072 pr_err("FSBase=%016lx GSBase=%016lx TRBase=%016lx\n",
9073 vmcs_readl(HOST_FS_BASE), vmcs_readl(HOST_GS_BASE),
9074 vmcs_readl(HOST_TR_BASE));
9075 pr_err("GDTBase=%016lx IDTBase=%016lx\n",
9076 vmcs_readl(HOST_GDTR_BASE), vmcs_readl(HOST_IDTR_BASE));
9077 pr_err("CR0=%016lx CR3=%016lx CR4=%016lx\n",
9078 vmcs_readl(HOST_CR0), vmcs_readl(HOST_CR3),
9079 vmcs_readl(HOST_CR4));
9080 pr_err("Sysenter RSP=%016lx CS:RIP=%04x:%016lx\n",
9081 vmcs_readl(HOST_IA32_SYSENTER_ESP),
9082 vmcs_read32(HOST_IA32_SYSENTER_CS),
9083 vmcs_readl(HOST_IA32_SYSENTER_EIP));
9084 if (vmexit_ctl & (VM_EXIT_LOAD_IA32_PAT | VM_EXIT_LOAD_IA32_EFER))
9085 pr_err("EFER = 0x%016llx PAT = 0x%016llx\n",
9086 vmcs_read64(HOST_IA32_EFER),
9087 vmcs_read64(HOST_IA32_PAT));
9088 if (cpu_has_load_perf_global_ctrl &&
9089 vmexit_ctl & VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL)
9090 pr_err("PerfGlobCtl = 0x%016llx\n",
9091 vmcs_read64(HOST_IA32_PERF_GLOBAL_CTRL));
9093 pr_err("*** Control State ***\n");
9094 pr_err("PinBased=%08x CPUBased=%08x SecondaryExec=%08x\n",
9095 pin_based_exec_ctrl, cpu_based_exec_ctrl, secondary_exec_control);
9096 pr_err("EntryControls=%08x ExitControls=%08x\n", vmentry_ctl, vmexit_ctl);
9097 pr_err("ExceptionBitmap=%08x PFECmask=%08x PFECmatch=%08x\n",
9098 vmcs_read32(EXCEPTION_BITMAP),
9099 vmcs_read32(PAGE_FAULT_ERROR_CODE_MASK),
9100 vmcs_read32(PAGE_FAULT_ERROR_CODE_MATCH));
9101 pr_err("VMEntry: intr_info=%08x errcode=%08x ilen=%08x\n",
9102 vmcs_read32(VM_ENTRY_INTR_INFO_FIELD),
9103 vmcs_read32(VM_ENTRY_EXCEPTION_ERROR_CODE),
9104 vmcs_read32(VM_ENTRY_INSTRUCTION_LEN));
9105 pr_err("VMExit: intr_info=%08x errcode=%08x ilen=%08x\n",
9106 vmcs_read32(VM_EXIT_INTR_INFO),
9107 vmcs_read32(VM_EXIT_INTR_ERROR_CODE),
9108 vmcs_read32(VM_EXIT_INSTRUCTION_LEN));
9109 pr_err(" reason=%08x qualification=%016lx\n",
9110 vmcs_read32(VM_EXIT_REASON), vmcs_readl(EXIT_QUALIFICATION));
9111 pr_err("IDTVectoring: info=%08x errcode=%08x\n",
9112 vmcs_read32(IDT_VECTORING_INFO_FIELD),
9113 vmcs_read32(IDT_VECTORING_ERROR_CODE));
9114 pr_err("TSC Offset = 0x%016llx\n", vmcs_read64(TSC_OFFSET));
9115 if (secondary_exec_control & SECONDARY_EXEC_TSC_SCALING)
9116 pr_err("TSC Multiplier = 0x%016llx\n",
9117 vmcs_read64(TSC_MULTIPLIER));
9118 if (cpu_based_exec_ctrl & CPU_BASED_TPR_SHADOW)
9119 pr_err("TPR Threshold = 0x%02x\n", vmcs_read32(TPR_THRESHOLD));
9120 if (pin_based_exec_ctrl & PIN_BASED_POSTED_INTR)
9121 pr_err("PostedIntrVec = 0x%02x\n", vmcs_read16(POSTED_INTR_NV));
9122 if ((secondary_exec_control & SECONDARY_EXEC_ENABLE_EPT))
9123 pr_err("EPT pointer = 0x%016llx\n", vmcs_read64(EPT_POINTER));
9124 n = vmcs_read32(CR3_TARGET_COUNT);
9125 for (i = 0; i + 1 < n; i += 4)
9126 pr_err("CR3 target%u=%016lx target%u=%016lx\n",
9127 i, vmcs_readl(CR3_TARGET_VALUE0 + i * 2),
9128 i + 1, vmcs_readl(CR3_TARGET_VALUE0 + i * 2 + 2));
9130 pr_err("CR3 target%u=%016lx\n",
9131 i, vmcs_readl(CR3_TARGET_VALUE0 + i * 2));
9132 if (secondary_exec_control & SECONDARY_EXEC_PAUSE_LOOP_EXITING)
9133 pr_err("PLE Gap=%08x Window=%08x\n",
9134 vmcs_read32(PLE_GAP), vmcs_read32(PLE_WINDOW));
9135 if (secondary_exec_control & SECONDARY_EXEC_ENABLE_VPID)
9136 pr_err("Virtual processor ID = 0x%04x\n",
9137 vmcs_read16(VIRTUAL_PROCESSOR_ID));
9141 * The guest has exited. See if we can fix it or if we need userspace
9144 static int vmx_handle_exit(struct kvm_vcpu *vcpu)
9146 struct vcpu_vmx *vmx = to_vmx(vcpu);
9147 u32 exit_reason = vmx->exit_reason;
9148 u32 vectoring_info = vmx->idt_vectoring_info;
9150 trace_kvm_exit(exit_reason, vcpu, KVM_ISA_VMX);
9153 * Flush logged GPAs PML buffer, this will make dirty_bitmap more
9154 * updated. Another good is, in kvm_vm_ioctl_get_dirty_log, before
9155 * querying dirty_bitmap, we only need to kick all vcpus out of guest
9156 * mode as if vcpus is in root mode, the PML buffer must has been
9160 vmx_flush_pml_buffer(vcpu);
9162 /* If guest state is invalid, start emulating */
9163 if (vmx->emulation_required)
9164 return handle_invalid_guest_state(vcpu);
9166 if (is_guest_mode(vcpu) && nested_vmx_exit_reflected(vcpu, exit_reason))
9167 return nested_vmx_reflect_vmexit(vcpu, exit_reason);
9169 if (exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY) {
9171 vcpu->run->exit_reason = KVM_EXIT_FAIL_ENTRY;
9172 vcpu->run->fail_entry.hardware_entry_failure_reason
9177 if (unlikely(vmx->fail)) {
9178 vcpu->run->exit_reason = KVM_EXIT_FAIL_ENTRY;
9179 vcpu->run->fail_entry.hardware_entry_failure_reason
9180 = vmcs_read32(VM_INSTRUCTION_ERROR);
9186 * Do not try to fix EXIT_REASON_EPT_MISCONFIG if it caused by
9187 * delivery event since it indicates guest is accessing MMIO.
9188 * The vm-exit can be triggered again after return to guest that
9189 * will cause infinite loop.
9191 if ((vectoring_info & VECTORING_INFO_VALID_MASK) &&
9192 (exit_reason != EXIT_REASON_EXCEPTION_NMI &&
9193 exit_reason != EXIT_REASON_EPT_VIOLATION &&
9194 exit_reason != EXIT_REASON_PML_FULL &&
9195 exit_reason != EXIT_REASON_TASK_SWITCH)) {
9196 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
9197 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_DELIVERY_EV;
9198 vcpu->run->internal.ndata = 3;
9199 vcpu->run->internal.data[0] = vectoring_info;
9200 vcpu->run->internal.data[1] = exit_reason;
9201 vcpu->run->internal.data[2] = vcpu->arch.exit_qualification;
9202 if (exit_reason == EXIT_REASON_EPT_MISCONFIG) {
9203 vcpu->run->internal.ndata++;
9204 vcpu->run->internal.data[3] =
9205 vmcs_read64(GUEST_PHYSICAL_ADDRESS);
9210 if (unlikely(!enable_vnmi &&
9211 vmx->loaded_vmcs->soft_vnmi_blocked)) {
9212 if (vmx_interrupt_allowed(vcpu)) {
9213 vmx->loaded_vmcs->soft_vnmi_blocked = 0;
9214 } else if (vmx->loaded_vmcs->vnmi_blocked_time > 1000000000LL &&
9215 vcpu->arch.nmi_pending) {
9217 * This CPU don't support us in finding the end of an
9218 * NMI-blocked window if the guest runs with IRQs
9219 * disabled. So we pull the trigger after 1 s of
9220 * futile waiting, but inform the user about this.
9222 printk(KERN_WARNING "%s: Breaking out of NMI-blocked "
9223 "state on VCPU %d after 1 s timeout\n",
9224 __func__, vcpu->vcpu_id);
9225 vmx->loaded_vmcs->soft_vnmi_blocked = 0;
9229 if (exit_reason < kvm_vmx_max_exit_handlers
9230 && kvm_vmx_exit_handlers[exit_reason])
9231 return kvm_vmx_exit_handlers[exit_reason](vcpu);
9233 vcpu_unimpl(vcpu, "vmx: unexpected exit reason 0x%x\n",
9235 kvm_queue_exception(vcpu, UD_VECTOR);
9240 static void update_cr8_intercept(struct kvm_vcpu *vcpu, int tpr, int irr)
9242 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
9244 if (is_guest_mode(vcpu) &&
9245 nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW))
9248 if (irr == -1 || tpr < irr) {
9249 vmcs_write32(TPR_THRESHOLD, 0);
9253 vmcs_write32(TPR_THRESHOLD, irr);
9256 static void vmx_set_virtual_x2apic_mode(struct kvm_vcpu *vcpu, bool set)
9258 u32 sec_exec_control;
9260 /* Postpone execution until vmcs01 is the current VMCS. */
9261 if (is_guest_mode(vcpu)) {
9262 to_vmx(vcpu)->nested.change_vmcs01_virtual_x2apic_mode = true;
9266 if (!cpu_has_vmx_virtualize_x2apic_mode())
9269 if (!cpu_need_tpr_shadow(vcpu))
9272 sec_exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
9275 sec_exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
9276 sec_exec_control |= SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
9278 sec_exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
9279 sec_exec_control |= SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
9280 vmx_flush_tlb(vcpu, true);
9282 vmcs_write32(SECONDARY_VM_EXEC_CONTROL, sec_exec_control);
9284 vmx_update_msr_bitmap(vcpu);
9287 static void vmx_set_apic_access_page_addr(struct kvm_vcpu *vcpu, hpa_t hpa)
9289 struct vcpu_vmx *vmx = to_vmx(vcpu);
9292 * Currently we do not handle the nested case where L2 has an
9293 * APIC access page of its own; that page is still pinned.
9294 * Hence, we skip the case where the VCPU is in guest mode _and_
9295 * L1 prepared an APIC access page for L2.
9297 * For the case where L1 and L2 share the same APIC access page
9298 * (flexpriority=Y but SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES clear
9299 * in the vmcs12), this function will only update either the vmcs01
9300 * or the vmcs02. If the former, the vmcs02 will be updated by
9301 * prepare_vmcs02. If the latter, the vmcs01 will be updated in
9302 * the next L2->L1 exit.
9304 if (!is_guest_mode(vcpu) ||
9305 !nested_cpu_has2(get_vmcs12(&vmx->vcpu),
9306 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) {
9307 vmcs_write64(APIC_ACCESS_ADDR, hpa);
9308 vmx_flush_tlb(vcpu, true);
9312 static void vmx_hwapic_isr_update(struct kvm_vcpu *vcpu, int max_isr)
9320 status = vmcs_read16(GUEST_INTR_STATUS);
9322 if (max_isr != old) {
9324 status |= max_isr << 8;
9325 vmcs_write16(GUEST_INTR_STATUS, status);
9329 static void vmx_set_rvi(int vector)
9337 status = vmcs_read16(GUEST_INTR_STATUS);
9338 old = (u8)status & 0xff;
9339 if ((u8)vector != old) {
9341 status |= (u8)vector;
9342 vmcs_write16(GUEST_INTR_STATUS, status);
9346 static void vmx_hwapic_irr_update(struct kvm_vcpu *vcpu, int max_irr)
9349 * When running L2, updating RVI is only relevant when
9350 * vmcs12 virtual-interrupt-delivery enabled.
9351 * However, it can be enabled only when L1 also
9352 * intercepts external-interrupts and in that case
9353 * we should not update vmcs02 RVI but instead intercept
9354 * interrupt. Therefore, do nothing when running L2.
9356 if (!is_guest_mode(vcpu))
9357 vmx_set_rvi(max_irr);
9360 static int vmx_sync_pir_to_irr(struct kvm_vcpu *vcpu)
9362 struct vcpu_vmx *vmx = to_vmx(vcpu);
9364 bool max_irr_updated;
9366 WARN_ON(!vcpu->arch.apicv_active);
9367 if (pi_test_on(&vmx->pi_desc)) {
9368 pi_clear_on(&vmx->pi_desc);
9370 * IOMMU can write to PIR.ON, so the barrier matters even on UP.
9371 * But on x86 this is just a compiler barrier anyway.
9373 smp_mb__after_atomic();
9375 kvm_apic_update_irr(vcpu, vmx->pi_desc.pir, &max_irr);
9378 * If we are running L2 and L1 has a new pending interrupt
9379 * which can be injected, we should re-evaluate
9380 * what should be done with this new L1 interrupt.
9381 * If L1 intercepts external-interrupts, we should
9382 * exit from L2 to L1. Otherwise, interrupt should be
9383 * delivered directly to L2.
9385 if (is_guest_mode(vcpu) && max_irr_updated) {
9386 if (nested_exit_on_intr(vcpu))
9387 kvm_vcpu_exiting_guest_mode(vcpu);
9389 kvm_make_request(KVM_REQ_EVENT, vcpu);
9392 max_irr = kvm_lapic_find_highest_irr(vcpu);
9394 vmx_hwapic_irr_update(vcpu, max_irr);
9398 static void vmx_load_eoi_exitmap(struct kvm_vcpu *vcpu, u64 *eoi_exit_bitmap)
9400 if (!kvm_vcpu_apicv_active(vcpu))
9403 vmcs_write64(EOI_EXIT_BITMAP0, eoi_exit_bitmap[0]);
9404 vmcs_write64(EOI_EXIT_BITMAP1, eoi_exit_bitmap[1]);
9405 vmcs_write64(EOI_EXIT_BITMAP2, eoi_exit_bitmap[2]);
9406 vmcs_write64(EOI_EXIT_BITMAP3, eoi_exit_bitmap[3]);
9409 static void vmx_apicv_post_state_restore(struct kvm_vcpu *vcpu)
9411 struct vcpu_vmx *vmx = to_vmx(vcpu);
9413 pi_clear_on(&vmx->pi_desc);
9414 memset(vmx->pi_desc.pir, 0, sizeof(vmx->pi_desc.pir));
9417 static void vmx_complete_atomic_exit(struct vcpu_vmx *vmx)
9419 u32 exit_intr_info = 0;
9420 u16 basic_exit_reason = (u16)vmx->exit_reason;
9422 if (!(basic_exit_reason == EXIT_REASON_MCE_DURING_VMENTRY
9423 || basic_exit_reason == EXIT_REASON_EXCEPTION_NMI))
9426 if (!(vmx->exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY))
9427 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
9428 vmx->exit_intr_info = exit_intr_info;
9430 /* if exit due to PF check for async PF */
9431 if (is_page_fault(exit_intr_info))
9432 vmx->vcpu.arch.apf.host_apf_reason = kvm_read_and_reset_pf_reason();
9434 /* Handle machine checks before interrupts are enabled */
9435 if (basic_exit_reason == EXIT_REASON_MCE_DURING_VMENTRY ||
9436 is_machine_check(exit_intr_info))
9437 kvm_machine_check();
9439 /* We need to handle NMIs before interrupts are enabled */
9440 if (is_nmi(exit_intr_info)) {
9441 kvm_before_interrupt(&vmx->vcpu);
9443 kvm_after_interrupt(&vmx->vcpu);
9447 static void vmx_handle_external_intr(struct kvm_vcpu *vcpu)
9449 u32 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
9451 if ((exit_intr_info & (INTR_INFO_VALID_MASK | INTR_INFO_INTR_TYPE_MASK))
9452 == (INTR_INFO_VALID_MASK | INTR_TYPE_EXT_INTR)) {
9453 unsigned int vector;
9454 unsigned long entry;
9456 struct vcpu_vmx *vmx = to_vmx(vcpu);
9457 #ifdef CONFIG_X86_64
9461 vector = exit_intr_info & INTR_INFO_VECTOR_MASK;
9462 desc = (gate_desc *)vmx->host_idt_base + vector;
9463 entry = gate_offset(desc);
9465 #ifdef CONFIG_X86_64
9466 "mov %%" _ASM_SP ", %[sp]\n\t"
9467 "and $0xfffffffffffffff0, %%" _ASM_SP "\n\t"
9472 __ASM_SIZE(push) " $%c[cs]\n\t"
9475 #ifdef CONFIG_X86_64
9480 THUNK_TARGET(entry),
9481 [ss]"i"(__KERNEL_DS),
9482 [cs]"i"(__KERNEL_CS)
9486 STACK_FRAME_NON_STANDARD(vmx_handle_external_intr);
9488 static bool vmx_has_emulated_msr(int index)
9491 case MSR_IA32_SMBASE:
9493 * We cannot do SMM unless we can run the guest in big
9496 return enable_unrestricted_guest || emulate_invalid_guest_state;
9497 case MSR_AMD64_VIRT_SPEC_CTRL:
9498 /* This is AMD only. */
9505 static bool vmx_mpx_supported(void)
9507 return (vmcs_config.vmexit_ctrl & VM_EXIT_CLEAR_BNDCFGS) &&
9508 (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_BNDCFGS);
9511 static bool vmx_xsaves_supported(void)
9513 return vmcs_config.cpu_based_2nd_exec_ctrl &
9514 SECONDARY_EXEC_XSAVES;
9517 static void vmx_recover_nmi_blocking(struct vcpu_vmx *vmx)
9522 bool idtv_info_valid;
9524 idtv_info_valid = vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK;
9527 if (vmx->loaded_vmcs->nmi_known_unmasked)
9530 * Can't use vmx->exit_intr_info since we're not sure what
9531 * the exit reason is.
9533 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
9534 unblock_nmi = (exit_intr_info & INTR_INFO_UNBLOCK_NMI) != 0;
9535 vector = exit_intr_info & INTR_INFO_VECTOR_MASK;
9537 * SDM 3: 27.7.1.2 (September 2008)
9538 * Re-set bit "block by NMI" before VM entry if vmexit caused by
9539 * a guest IRET fault.
9540 * SDM 3: 23.2.2 (September 2008)
9541 * Bit 12 is undefined in any of the following cases:
9542 * If the VM exit sets the valid bit in the IDT-vectoring
9543 * information field.
9544 * If the VM exit is due to a double fault.
9546 if ((exit_intr_info & INTR_INFO_VALID_MASK) && unblock_nmi &&
9547 vector != DF_VECTOR && !idtv_info_valid)
9548 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
9549 GUEST_INTR_STATE_NMI);
9551 vmx->loaded_vmcs->nmi_known_unmasked =
9552 !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO)
9553 & GUEST_INTR_STATE_NMI);
9554 } else if (unlikely(vmx->loaded_vmcs->soft_vnmi_blocked))
9555 vmx->loaded_vmcs->vnmi_blocked_time +=
9556 ktime_to_ns(ktime_sub(ktime_get(),
9557 vmx->loaded_vmcs->entry_time));
9560 static void __vmx_complete_interrupts(struct kvm_vcpu *vcpu,
9561 u32 idt_vectoring_info,
9562 int instr_len_field,
9563 int error_code_field)
9567 bool idtv_info_valid;
9569 idtv_info_valid = idt_vectoring_info & VECTORING_INFO_VALID_MASK;
9571 vcpu->arch.nmi_injected = false;
9572 kvm_clear_exception_queue(vcpu);
9573 kvm_clear_interrupt_queue(vcpu);
9575 if (!idtv_info_valid)
9578 kvm_make_request(KVM_REQ_EVENT, vcpu);
9580 vector = idt_vectoring_info & VECTORING_INFO_VECTOR_MASK;
9581 type = idt_vectoring_info & VECTORING_INFO_TYPE_MASK;
9584 case INTR_TYPE_NMI_INTR:
9585 vcpu->arch.nmi_injected = true;
9587 * SDM 3: 27.7.1.2 (September 2008)
9588 * Clear bit "block by NMI" before VM entry if a NMI
9591 vmx_set_nmi_mask(vcpu, false);
9593 case INTR_TYPE_SOFT_EXCEPTION:
9594 vcpu->arch.event_exit_inst_len = vmcs_read32(instr_len_field);
9596 case INTR_TYPE_HARD_EXCEPTION:
9597 if (idt_vectoring_info & VECTORING_INFO_DELIVER_CODE_MASK) {
9598 u32 err = vmcs_read32(error_code_field);
9599 kvm_requeue_exception_e(vcpu, vector, err);
9601 kvm_requeue_exception(vcpu, vector);
9603 case INTR_TYPE_SOFT_INTR:
9604 vcpu->arch.event_exit_inst_len = vmcs_read32(instr_len_field);
9606 case INTR_TYPE_EXT_INTR:
9607 kvm_queue_interrupt(vcpu, vector, type == INTR_TYPE_SOFT_INTR);
9614 static void vmx_complete_interrupts(struct vcpu_vmx *vmx)
9616 __vmx_complete_interrupts(&vmx->vcpu, vmx->idt_vectoring_info,
9617 VM_EXIT_INSTRUCTION_LEN,
9618 IDT_VECTORING_ERROR_CODE);
9621 static void vmx_cancel_injection(struct kvm_vcpu *vcpu)
9623 __vmx_complete_interrupts(vcpu,
9624 vmcs_read32(VM_ENTRY_INTR_INFO_FIELD),
9625 VM_ENTRY_INSTRUCTION_LEN,
9626 VM_ENTRY_EXCEPTION_ERROR_CODE);
9628 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0);
9631 static void atomic_switch_perf_msrs(struct vcpu_vmx *vmx)
9634 struct perf_guest_switch_msr *msrs;
9636 msrs = perf_guest_get_msrs(&nr_msrs);
9641 for (i = 0; i < nr_msrs; i++)
9642 if (msrs[i].host == msrs[i].guest)
9643 clear_atomic_switch_msr(vmx, msrs[i].msr);
9645 add_atomic_switch_msr(vmx, msrs[i].msr, msrs[i].guest,
9649 static void vmx_arm_hv_timer(struct kvm_vcpu *vcpu)
9651 struct vcpu_vmx *vmx = to_vmx(vcpu);
9655 if (vmx->hv_deadline_tsc == -1)
9659 if (vmx->hv_deadline_tsc > tscl)
9660 /* sure to be 32 bit only because checked on set_hv_timer */
9661 delta_tsc = (u32)((vmx->hv_deadline_tsc - tscl) >>
9662 cpu_preemption_timer_multi);
9666 vmcs_write32(VMX_PREEMPTION_TIMER_VALUE, delta_tsc);
9669 static void __noclone vmx_vcpu_run(struct kvm_vcpu *vcpu)
9671 struct vcpu_vmx *vmx = to_vmx(vcpu);
9672 unsigned long cr3, cr4, evmcs_rsp;
9674 /* Record the guest's net vcpu time for enforced NMI injections. */
9675 if (unlikely(!enable_vnmi &&
9676 vmx->loaded_vmcs->soft_vnmi_blocked))
9677 vmx->loaded_vmcs->entry_time = ktime_get();
9679 /* Don't enter VMX if guest state is invalid, let the exit handler
9680 start emulation until we arrive back to a valid state */
9681 if (vmx->emulation_required)
9684 if (vmx->ple_window_dirty) {
9685 vmx->ple_window_dirty = false;
9686 vmcs_write32(PLE_WINDOW, vmx->ple_window);
9689 if (vmx->nested.sync_shadow_vmcs) {
9690 copy_vmcs12_to_shadow(vmx);
9691 vmx->nested.sync_shadow_vmcs = false;
9694 if (test_bit(VCPU_REGS_RSP, (unsigned long *)&vcpu->arch.regs_dirty))
9695 vmcs_writel(GUEST_RSP, vcpu->arch.regs[VCPU_REGS_RSP]);
9696 if (test_bit(VCPU_REGS_RIP, (unsigned long *)&vcpu->arch.regs_dirty))
9697 vmcs_writel(GUEST_RIP, vcpu->arch.regs[VCPU_REGS_RIP]);
9699 cr3 = __get_current_cr3_fast();
9700 if (unlikely(cr3 != vmx->loaded_vmcs->vmcs_host_cr3)) {
9701 vmcs_writel(HOST_CR3, cr3);
9702 vmx->loaded_vmcs->vmcs_host_cr3 = cr3;
9705 cr4 = cr4_read_shadow();
9706 if (unlikely(cr4 != vmx->loaded_vmcs->vmcs_host_cr4)) {
9707 vmcs_writel(HOST_CR4, cr4);
9708 vmx->loaded_vmcs->vmcs_host_cr4 = cr4;
9711 /* When single-stepping over STI and MOV SS, we must clear the
9712 * corresponding interruptibility bits in the guest state. Otherwise
9713 * vmentry fails as it then expects bit 14 (BS) in pending debug
9714 * exceptions being set, but that's not correct for the guest debugging
9716 if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP)
9717 vmx_set_interrupt_shadow(vcpu, 0);
9719 if (static_cpu_has(X86_FEATURE_PKU) &&
9720 kvm_read_cr4_bits(vcpu, X86_CR4_PKE) &&
9721 vcpu->arch.pkru != vmx->host_pkru)
9722 __write_pkru(vcpu->arch.pkru);
9724 atomic_switch_perf_msrs(vmx);
9726 vmx_arm_hv_timer(vcpu);
9729 * If this vCPU has touched SPEC_CTRL, restore the guest's value if
9730 * it's non-zero. Since vmentry is serialising on affected CPUs, there
9731 * is no need to worry about the conditional branch over the wrmsr
9732 * being speculatively taken.
9734 x86_spec_ctrl_set_guest(vmx->spec_ctrl, 0);
9736 vmx->__launched = vmx->loaded_vmcs->launched;
9738 evmcs_rsp = static_branch_unlikely(&enable_evmcs) ?
9739 (unsigned long)¤t_evmcs->host_rsp : 0;
9742 /* Store host registers */
9743 "push %%" _ASM_DX "; push %%" _ASM_BP ";"
9744 "push %%" _ASM_CX " \n\t" /* placeholder for guest rcx */
9745 "push %%" _ASM_CX " \n\t"
9746 "cmp %%" _ASM_SP ", %c[host_rsp](%0) \n\t"
9748 "mov %%" _ASM_SP ", %c[host_rsp](%0) \n\t"
9749 /* Avoid VMWRITE when Enlightened VMCS is in use */
9750 "test %%" _ASM_SI ", %%" _ASM_SI " \n\t"
9752 "mov %%" _ASM_SP ", (%%" _ASM_SI ") \n\t"
9755 __ex(ASM_VMX_VMWRITE_RSP_RDX) "\n\t"
9757 /* Reload cr2 if changed */
9758 "mov %c[cr2](%0), %%" _ASM_AX " \n\t"
9759 "mov %%cr2, %%" _ASM_DX " \n\t"
9760 "cmp %%" _ASM_AX ", %%" _ASM_DX " \n\t"
9762 "mov %%" _ASM_AX", %%cr2 \n\t"
9764 /* Check if vmlaunch of vmresume is needed */
9765 "cmpl $0, %c[launched](%0) \n\t"
9766 /* Load guest registers. Don't clobber flags. */
9767 "mov %c[rax](%0), %%" _ASM_AX " \n\t"
9768 "mov %c[rbx](%0), %%" _ASM_BX " \n\t"
9769 "mov %c[rdx](%0), %%" _ASM_DX " \n\t"
9770 "mov %c[rsi](%0), %%" _ASM_SI " \n\t"
9771 "mov %c[rdi](%0), %%" _ASM_DI " \n\t"
9772 "mov %c[rbp](%0), %%" _ASM_BP " \n\t"
9773 #ifdef CONFIG_X86_64
9774 "mov %c[r8](%0), %%r8 \n\t"
9775 "mov %c[r9](%0), %%r9 \n\t"
9776 "mov %c[r10](%0), %%r10 \n\t"
9777 "mov %c[r11](%0), %%r11 \n\t"
9778 "mov %c[r12](%0), %%r12 \n\t"
9779 "mov %c[r13](%0), %%r13 \n\t"
9780 "mov %c[r14](%0), %%r14 \n\t"
9781 "mov %c[r15](%0), %%r15 \n\t"
9783 "mov %c[rcx](%0), %%" _ASM_CX " \n\t" /* kills %0 (ecx) */
9785 /* Enter guest mode */
9787 __ex(ASM_VMX_VMLAUNCH) "\n\t"
9789 "1: " __ex(ASM_VMX_VMRESUME) "\n\t"
9791 /* Save guest registers, load host registers, keep flags */
9792 "mov %0, %c[wordsize](%%" _ASM_SP ") \n\t"
9794 "setbe %c[fail](%0)\n\t"
9795 "mov %%" _ASM_AX ", %c[rax](%0) \n\t"
9796 "mov %%" _ASM_BX ", %c[rbx](%0) \n\t"
9797 __ASM_SIZE(pop) " %c[rcx](%0) \n\t"
9798 "mov %%" _ASM_DX ", %c[rdx](%0) \n\t"
9799 "mov %%" _ASM_SI ", %c[rsi](%0) \n\t"
9800 "mov %%" _ASM_DI ", %c[rdi](%0) \n\t"
9801 "mov %%" _ASM_BP ", %c[rbp](%0) \n\t"
9802 #ifdef CONFIG_X86_64
9803 "mov %%r8, %c[r8](%0) \n\t"
9804 "mov %%r9, %c[r9](%0) \n\t"
9805 "mov %%r10, %c[r10](%0) \n\t"
9806 "mov %%r11, %c[r11](%0) \n\t"
9807 "mov %%r12, %c[r12](%0) \n\t"
9808 "mov %%r13, %c[r13](%0) \n\t"
9809 "mov %%r14, %c[r14](%0) \n\t"
9810 "mov %%r15, %c[r15](%0) \n\t"
9811 "xor %%r8d, %%r8d \n\t"
9812 "xor %%r9d, %%r9d \n\t"
9813 "xor %%r10d, %%r10d \n\t"
9814 "xor %%r11d, %%r11d \n\t"
9815 "xor %%r12d, %%r12d \n\t"
9816 "xor %%r13d, %%r13d \n\t"
9817 "xor %%r14d, %%r14d \n\t"
9818 "xor %%r15d, %%r15d \n\t"
9820 "mov %%cr2, %%" _ASM_AX " \n\t"
9821 "mov %%" _ASM_AX ", %c[cr2](%0) \n\t"
9823 "xor %%eax, %%eax \n\t"
9824 "xor %%ebx, %%ebx \n\t"
9825 "xor %%esi, %%esi \n\t"
9826 "xor %%edi, %%edi \n\t"
9827 "pop %%" _ASM_BP "; pop %%" _ASM_DX " \n\t"
9828 ".pushsection .rodata \n\t"
9829 ".global vmx_return \n\t"
9830 "vmx_return: " _ASM_PTR " 2b \n\t"
9832 : : "c"(vmx), "d"((unsigned long)HOST_RSP), "S"(evmcs_rsp),
9833 [launched]"i"(offsetof(struct vcpu_vmx, __launched)),
9834 [fail]"i"(offsetof(struct vcpu_vmx, fail)),
9835 [host_rsp]"i"(offsetof(struct vcpu_vmx, host_rsp)),
9836 [rax]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RAX])),
9837 [rbx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBX])),
9838 [rcx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RCX])),
9839 [rdx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDX])),
9840 [rsi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RSI])),
9841 [rdi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDI])),
9842 [rbp]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBP])),
9843 #ifdef CONFIG_X86_64
9844 [r8]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R8])),
9845 [r9]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R9])),
9846 [r10]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R10])),
9847 [r11]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R11])),
9848 [r12]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R12])),
9849 [r13]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R13])),
9850 [r14]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R14])),
9851 [r15]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R15])),
9853 [cr2]"i"(offsetof(struct vcpu_vmx, vcpu.arch.cr2)),
9854 [wordsize]"i"(sizeof(ulong))
9856 #ifdef CONFIG_X86_64
9857 , "rax", "rbx", "rdi"
9858 , "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15"
9860 , "eax", "ebx", "edi"
9865 * We do not use IBRS in the kernel. If this vCPU has used the
9866 * SPEC_CTRL MSR it may have left it on; save the value and
9867 * turn it off. This is much more efficient than blindly adding
9868 * it to the atomic save/restore list. Especially as the former
9869 * (Saving guest MSRs on vmexit) doesn't even exist in KVM.
9871 * For non-nested case:
9872 * If the L01 MSR bitmap does not intercept the MSR, then we need to
9876 * If the L02 MSR bitmap does not intercept the MSR, then we need to
9879 if (unlikely(!msr_write_intercepted(vcpu, MSR_IA32_SPEC_CTRL)))
9880 vmx->spec_ctrl = native_read_msr(MSR_IA32_SPEC_CTRL);
9882 x86_spec_ctrl_restore_host(vmx->spec_ctrl, 0);
9884 /* Eliminate branch target predictions from guest mode */
9887 /* All fields are clean at this point */
9888 if (static_branch_unlikely(&enable_evmcs))
9889 current_evmcs->hv_clean_fields |=
9890 HV_VMX_ENLIGHTENED_CLEAN_FIELD_ALL;
9892 /* MSR_IA32_DEBUGCTLMSR is zeroed on vmexit. Restore it if needed */
9893 if (vmx->host_debugctlmsr)
9894 update_debugctlmsr(vmx->host_debugctlmsr);
9896 #ifndef CONFIG_X86_64
9898 * The sysexit path does not restore ds/es, so we must set them to
9899 * a reasonable value ourselves.
9901 * We can't defer this to vmx_load_host_state() since that function
9902 * may be executed in interrupt context, which saves and restore segments
9903 * around it, nullifying its effect.
9905 loadsegment(ds, __USER_DS);
9906 loadsegment(es, __USER_DS);
9909 vcpu->arch.regs_avail = ~((1 << VCPU_REGS_RIP) | (1 << VCPU_REGS_RSP)
9910 | (1 << VCPU_EXREG_RFLAGS)
9911 | (1 << VCPU_EXREG_PDPTR)
9912 | (1 << VCPU_EXREG_SEGMENTS)
9913 | (1 << VCPU_EXREG_CR3));
9914 vcpu->arch.regs_dirty = 0;
9917 * eager fpu is enabled if PKEY is supported and CR4 is switched
9918 * back on host, so it is safe to read guest PKRU from current
9921 if (static_cpu_has(X86_FEATURE_PKU) &&
9922 kvm_read_cr4_bits(vcpu, X86_CR4_PKE)) {
9923 vcpu->arch.pkru = __read_pkru();
9924 if (vcpu->arch.pkru != vmx->host_pkru)
9925 __write_pkru(vmx->host_pkru);
9928 vmx->nested.nested_run_pending = 0;
9929 vmx->idt_vectoring_info = 0;
9931 vmx->exit_reason = vmx->fail ? 0xdead : vmcs_read32(VM_EXIT_REASON);
9932 if (vmx->fail || (vmx->exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY))
9935 vmx->loaded_vmcs->launched = 1;
9936 vmx->idt_vectoring_info = vmcs_read32(IDT_VECTORING_INFO_FIELD);
9938 vmx_complete_atomic_exit(vmx);
9939 vmx_recover_nmi_blocking(vmx);
9940 vmx_complete_interrupts(vmx);
9942 STACK_FRAME_NON_STANDARD(vmx_vcpu_run);
9944 static struct kvm *vmx_vm_alloc(void)
9946 struct kvm_vmx *kvm_vmx = kzalloc(sizeof(struct kvm_vmx), GFP_KERNEL);
9947 return &kvm_vmx->kvm;
9950 static void vmx_vm_free(struct kvm *kvm)
9952 kfree(to_kvm_vmx(kvm));
9955 static void vmx_switch_vmcs(struct kvm_vcpu *vcpu, struct loaded_vmcs *vmcs)
9957 struct vcpu_vmx *vmx = to_vmx(vcpu);
9960 if (vmx->loaded_vmcs == vmcs)
9964 vmx->loaded_vmcs = vmcs;
9966 vmx_vcpu_load(vcpu, cpu);
9971 * Ensure that the current vmcs of the logical processor is the
9972 * vmcs01 of the vcpu before calling free_nested().
9974 static void vmx_free_vcpu_nested(struct kvm_vcpu *vcpu)
9976 struct vcpu_vmx *vmx = to_vmx(vcpu);
9979 vmx_switch_vmcs(vcpu, &vmx->vmcs01);
9984 static void vmx_free_vcpu(struct kvm_vcpu *vcpu)
9986 struct vcpu_vmx *vmx = to_vmx(vcpu);
9989 vmx_destroy_pml_buffer(vmx);
9990 free_vpid(vmx->vpid);
9991 leave_guest_mode(vcpu);
9992 vmx_free_vcpu_nested(vcpu);
9993 free_loaded_vmcs(vmx->loaded_vmcs);
9994 kfree(vmx->guest_msrs);
9995 kvm_vcpu_uninit(vcpu);
9996 kmem_cache_free(kvm_vcpu_cache, vmx);
9999 static struct kvm_vcpu *vmx_create_vcpu(struct kvm *kvm, unsigned int id)
10002 struct vcpu_vmx *vmx = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
10003 unsigned long *msr_bitmap;
10007 return ERR_PTR(-ENOMEM);
10009 vmx->vpid = allocate_vpid();
10011 err = kvm_vcpu_init(&vmx->vcpu, kvm, id);
10018 * If PML is turned on, failure on enabling PML just results in failure
10019 * of creating the vcpu, therefore we can simplify PML logic (by
10020 * avoiding dealing with cases, such as enabling PML partially on vcpus
10021 * for the guest, etc.
10024 vmx->pml_pg = alloc_page(GFP_KERNEL | __GFP_ZERO);
10029 vmx->guest_msrs = kmalloc(PAGE_SIZE, GFP_KERNEL);
10030 BUILD_BUG_ON(ARRAY_SIZE(vmx_msr_index) * sizeof(vmx->guest_msrs[0])
10033 if (!vmx->guest_msrs)
10036 err = alloc_loaded_vmcs(&vmx->vmcs01);
10040 msr_bitmap = vmx->vmcs01.msr_bitmap;
10041 vmx_disable_intercept_for_msr(msr_bitmap, MSR_FS_BASE, MSR_TYPE_RW);
10042 vmx_disable_intercept_for_msr(msr_bitmap, MSR_GS_BASE, MSR_TYPE_RW);
10043 vmx_disable_intercept_for_msr(msr_bitmap, MSR_KERNEL_GS_BASE, MSR_TYPE_RW);
10044 vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_SYSENTER_CS, MSR_TYPE_RW);
10045 vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_SYSENTER_ESP, MSR_TYPE_RW);
10046 vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_SYSENTER_EIP, MSR_TYPE_RW);
10047 vmx->msr_bitmap_mode = 0;
10049 vmx->loaded_vmcs = &vmx->vmcs01;
10051 vmx_vcpu_load(&vmx->vcpu, cpu);
10052 vmx->vcpu.cpu = cpu;
10053 vmx_vcpu_setup(vmx);
10054 vmx_vcpu_put(&vmx->vcpu);
10056 if (cpu_need_virtualize_apic_accesses(&vmx->vcpu)) {
10057 err = alloc_apic_access_page(kvm);
10062 if (enable_ept && !enable_unrestricted_guest) {
10063 err = init_rmode_identity_map(kvm);
10069 nested_vmx_setup_ctls_msrs(&vmx->nested.msrs,
10070 kvm_vcpu_apicv_active(&vmx->vcpu));
10071 vmx->nested.vpid02 = allocate_vpid();
10074 vmx->nested.posted_intr_nv = -1;
10075 vmx->nested.current_vmptr = -1ull;
10077 vmx->msr_ia32_feature_control_valid_bits = FEATURE_CONTROL_LOCKED;
10080 * Enforce invariant: pi_desc.nv is always either POSTED_INTR_VECTOR
10081 * or POSTED_INTR_WAKEUP_VECTOR.
10083 vmx->pi_desc.nv = POSTED_INTR_VECTOR;
10084 vmx->pi_desc.sn = 1;
10089 free_vpid(vmx->nested.vpid02);
10090 free_loaded_vmcs(vmx->loaded_vmcs);
10092 kfree(vmx->guest_msrs);
10094 vmx_destroy_pml_buffer(vmx);
10096 kvm_vcpu_uninit(&vmx->vcpu);
10098 free_vpid(vmx->vpid);
10099 kmem_cache_free(kvm_vcpu_cache, vmx);
10100 return ERR_PTR(err);
10103 static int vmx_vm_init(struct kvm *kvm)
10106 kvm->arch.pause_in_guest = true;
10110 static void __init vmx_check_processor_compat(void *rtn)
10112 struct vmcs_config vmcs_conf;
10115 if (setup_vmcs_config(&vmcs_conf) < 0)
10116 *(int *)rtn = -EIO;
10117 nested_vmx_setup_ctls_msrs(&vmcs_conf.nested, enable_apicv);
10118 if (memcmp(&vmcs_config, &vmcs_conf, sizeof(struct vmcs_config)) != 0) {
10119 printk(KERN_ERR "kvm: CPU %d feature inconsistency!\n",
10120 smp_processor_id());
10121 *(int *)rtn = -EIO;
10125 static u64 vmx_get_mt_mask(struct kvm_vcpu *vcpu, gfn_t gfn, bool is_mmio)
10130 /* For VT-d and EPT combination
10131 * 1. MMIO: always map as UC
10132 * 2. EPT with VT-d:
10133 * a. VT-d without snooping control feature: can't guarantee the
10134 * result, try to trust guest.
10135 * b. VT-d with snooping control feature: snooping control feature of
10136 * VT-d engine can guarantee the cache correctness. Just set it
10137 * to WB to keep consistent with host. So the same as item 3.
10138 * 3. EPT without VT-d: always map as WB and set IPAT=1 to keep
10139 * consistent with host MTRR
10142 cache = MTRR_TYPE_UNCACHABLE;
10146 if (!kvm_arch_has_noncoherent_dma(vcpu->kvm)) {
10147 ipat = VMX_EPT_IPAT_BIT;
10148 cache = MTRR_TYPE_WRBACK;
10152 if (kvm_read_cr0(vcpu) & X86_CR0_CD) {
10153 ipat = VMX_EPT_IPAT_BIT;
10154 if (kvm_check_has_quirk(vcpu->kvm, KVM_X86_QUIRK_CD_NW_CLEARED))
10155 cache = MTRR_TYPE_WRBACK;
10157 cache = MTRR_TYPE_UNCACHABLE;
10161 cache = kvm_mtrr_get_guest_memory_type(vcpu, gfn);
10164 return (cache << VMX_EPT_MT_EPTE_SHIFT) | ipat;
10167 static int vmx_get_lpage_level(void)
10169 if (enable_ept && !cpu_has_vmx_ept_1g_page())
10170 return PT_DIRECTORY_LEVEL;
10172 /* For shadow and EPT supported 1GB page */
10173 return PT_PDPE_LEVEL;
10176 static void vmcs_set_secondary_exec_control(u32 new_ctl)
10179 * These bits in the secondary execution controls field
10180 * are dynamic, the others are mostly based on the hypervisor
10181 * architecture and the guest's CPUID. Do not touch the
10185 SECONDARY_EXEC_SHADOW_VMCS |
10186 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
10187 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
10188 SECONDARY_EXEC_DESC;
10190 u32 cur_ctl = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
10192 vmcs_write32(SECONDARY_VM_EXEC_CONTROL,
10193 (new_ctl & ~mask) | (cur_ctl & mask));
10197 * Generate MSR_IA32_VMX_CR{0,4}_FIXED1 according to CPUID. Only set bits
10198 * (indicating "allowed-1") if they are supported in the guest's CPUID.
10200 static void nested_vmx_cr_fixed1_bits_update(struct kvm_vcpu *vcpu)
10202 struct vcpu_vmx *vmx = to_vmx(vcpu);
10203 struct kvm_cpuid_entry2 *entry;
10205 vmx->nested.msrs.cr0_fixed1 = 0xffffffff;
10206 vmx->nested.msrs.cr4_fixed1 = X86_CR4_PCE;
10208 #define cr4_fixed1_update(_cr4_mask, _reg, _cpuid_mask) do { \
10209 if (entry && (entry->_reg & (_cpuid_mask))) \
10210 vmx->nested.msrs.cr4_fixed1 |= (_cr4_mask); \
10213 entry = kvm_find_cpuid_entry(vcpu, 0x1, 0);
10214 cr4_fixed1_update(X86_CR4_VME, edx, bit(X86_FEATURE_VME));
10215 cr4_fixed1_update(X86_CR4_PVI, edx, bit(X86_FEATURE_VME));
10216 cr4_fixed1_update(X86_CR4_TSD, edx, bit(X86_FEATURE_TSC));
10217 cr4_fixed1_update(X86_CR4_DE, edx, bit(X86_FEATURE_DE));
10218 cr4_fixed1_update(X86_CR4_PSE, edx, bit(X86_FEATURE_PSE));
10219 cr4_fixed1_update(X86_CR4_PAE, edx, bit(X86_FEATURE_PAE));
10220 cr4_fixed1_update(X86_CR4_MCE, edx, bit(X86_FEATURE_MCE));
10221 cr4_fixed1_update(X86_CR4_PGE, edx, bit(X86_FEATURE_PGE));
10222 cr4_fixed1_update(X86_CR4_OSFXSR, edx, bit(X86_FEATURE_FXSR));
10223 cr4_fixed1_update(X86_CR4_OSXMMEXCPT, edx, bit(X86_FEATURE_XMM));
10224 cr4_fixed1_update(X86_CR4_VMXE, ecx, bit(X86_FEATURE_VMX));
10225 cr4_fixed1_update(X86_CR4_SMXE, ecx, bit(X86_FEATURE_SMX));
10226 cr4_fixed1_update(X86_CR4_PCIDE, ecx, bit(X86_FEATURE_PCID));
10227 cr4_fixed1_update(X86_CR4_OSXSAVE, ecx, bit(X86_FEATURE_XSAVE));
10229 entry = kvm_find_cpuid_entry(vcpu, 0x7, 0);
10230 cr4_fixed1_update(X86_CR4_FSGSBASE, ebx, bit(X86_FEATURE_FSGSBASE));
10231 cr4_fixed1_update(X86_CR4_SMEP, ebx, bit(X86_FEATURE_SMEP));
10232 cr4_fixed1_update(X86_CR4_SMAP, ebx, bit(X86_FEATURE_SMAP));
10233 cr4_fixed1_update(X86_CR4_PKE, ecx, bit(X86_FEATURE_PKU));
10234 cr4_fixed1_update(X86_CR4_UMIP, ecx, bit(X86_FEATURE_UMIP));
10236 #undef cr4_fixed1_update
10239 static void vmx_cpuid_update(struct kvm_vcpu *vcpu)
10241 struct vcpu_vmx *vmx = to_vmx(vcpu);
10243 if (cpu_has_secondary_exec_ctrls()) {
10244 vmx_compute_secondary_exec_control(vmx);
10245 vmcs_set_secondary_exec_control(vmx->secondary_exec_control);
10248 if (nested_vmx_allowed(vcpu))
10249 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits |=
10250 FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
10252 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits &=
10253 ~FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
10255 if (nested_vmx_allowed(vcpu))
10256 nested_vmx_cr_fixed1_bits_update(vcpu);
10259 static void vmx_set_supported_cpuid(u32 func, struct kvm_cpuid_entry2 *entry)
10261 if (func == 1 && nested)
10262 entry->ecx |= bit(X86_FEATURE_VMX);
10265 static void nested_ept_inject_page_fault(struct kvm_vcpu *vcpu,
10266 struct x86_exception *fault)
10268 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
10269 struct vcpu_vmx *vmx = to_vmx(vcpu);
10271 unsigned long exit_qualification = vcpu->arch.exit_qualification;
10273 if (vmx->nested.pml_full) {
10274 exit_reason = EXIT_REASON_PML_FULL;
10275 vmx->nested.pml_full = false;
10276 exit_qualification &= INTR_INFO_UNBLOCK_NMI;
10277 } else if (fault->error_code & PFERR_RSVD_MASK)
10278 exit_reason = EXIT_REASON_EPT_MISCONFIG;
10280 exit_reason = EXIT_REASON_EPT_VIOLATION;
10282 nested_vmx_vmexit(vcpu, exit_reason, 0, exit_qualification);
10283 vmcs12->guest_physical_address = fault->address;
10286 static bool nested_ept_ad_enabled(struct kvm_vcpu *vcpu)
10288 return nested_ept_get_cr3(vcpu) & VMX_EPTP_AD_ENABLE_BIT;
10291 /* Callbacks for nested_ept_init_mmu_context: */
10293 static unsigned long nested_ept_get_cr3(struct kvm_vcpu *vcpu)
10295 /* return the page table to be shadowed - in our case, EPT12 */
10296 return get_vmcs12(vcpu)->ept_pointer;
10299 static int nested_ept_init_mmu_context(struct kvm_vcpu *vcpu)
10301 WARN_ON(mmu_is_nested(vcpu));
10302 if (!valid_ept_address(vcpu, nested_ept_get_cr3(vcpu)))
10305 kvm_mmu_unload(vcpu);
10306 kvm_init_shadow_ept_mmu(vcpu,
10307 to_vmx(vcpu)->nested.msrs.ept_caps &
10308 VMX_EPT_EXECUTE_ONLY_BIT,
10309 nested_ept_ad_enabled(vcpu));
10310 vcpu->arch.mmu.set_cr3 = vmx_set_cr3;
10311 vcpu->arch.mmu.get_cr3 = nested_ept_get_cr3;
10312 vcpu->arch.mmu.inject_page_fault = nested_ept_inject_page_fault;
10314 vcpu->arch.walk_mmu = &vcpu->arch.nested_mmu;
10318 static void nested_ept_uninit_mmu_context(struct kvm_vcpu *vcpu)
10320 vcpu->arch.walk_mmu = &vcpu->arch.mmu;
10323 static bool nested_vmx_is_page_fault_vmexit(struct vmcs12 *vmcs12,
10326 bool inequality, bit;
10328 bit = (vmcs12->exception_bitmap & (1u << PF_VECTOR)) != 0;
10330 (error_code & vmcs12->page_fault_error_code_mask) !=
10331 vmcs12->page_fault_error_code_match;
10332 return inequality ^ bit;
10335 static void vmx_inject_page_fault_nested(struct kvm_vcpu *vcpu,
10336 struct x86_exception *fault)
10338 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
10340 WARN_ON(!is_guest_mode(vcpu));
10342 if (nested_vmx_is_page_fault_vmexit(vmcs12, fault->error_code) &&
10343 !to_vmx(vcpu)->nested.nested_run_pending) {
10344 vmcs12->vm_exit_intr_error_code = fault->error_code;
10345 nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI,
10346 PF_VECTOR | INTR_TYPE_HARD_EXCEPTION |
10347 INTR_INFO_DELIVER_CODE_MASK | INTR_INFO_VALID_MASK,
10350 kvm_inject_page_fault(vcpu, fault);
10354 static inline bool nested_vmx_prepare_msr_bitmap(struct kvm_vcpu *vcpu,
10355 struct vmcs12 *vmcs12);
10357 static void nested_get_vmcs12_pages(struct kvm_vcpu *vcpu,
10358 struct vmcs12 *vmcs12)
10360 struct vcpu_vmx *vmx = to_vmx(vcpu);
10364 if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) {
10366 * Translate L1 physical address to host physical
10367 * address for vmcs02. Keep the page pinned, so this
10368 * physical address remains valid. We keep a reference
10369 * to it so we can release it later.
10371 if (vmx->nested.apic_access_page) { /* shouldn't happen */
10372 kvm_release_page_dirty(vmx->nested.apic_access_page);
10373 vmx->nested.apic_access_page = NULL;
10375 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->apic_access_addr);
10377 * If translation failed, no matter: This feature asks
10378 * to exit when accessing the given address, and if it
10379 * can never be accessed, this feature won't do
10382 if (!is_error_page(page)) {
10383 vmx->nested.apic_access_page = page;
10384 hpa = page_to_phys(vmx->nested.apic_access_page);
10385 vmcs_write64(APIC_ACCESS_ADDR, hpa);
10387 vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL,
10388 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES);
10390 } else if (!(nested_cpu_has_virt_x2apic_mode(vmcs12)) &&
10391 cpu_need_virtualize_apic_accesses(&vmx->vcpu)) {
10392 vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL,
10393 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES);
10394 kvm_vcpu_reload_apic_access_page(vcpu);
10397 if (nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW)) {
10398 if (vmx->nested.virtual_apic_page) { /* shouldn't happen */
10399 kvm_release_page_dirty(vmx->nested.virtual_apic_page);
10400 vmx->nested.virtual_apic_page = NULL;
10402 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->virtual_apic_page_addr);
10405 * If translation failed, VM entry will fail because
10406 * prepare_vmcs02 set VIRTUAL_APIC_PAGE_ADDR to -1ull.
10407 * Failing the vm entry is _not_ what the processor
10408 * does but it's basically the only possibility we
10409 * have. We could still enter the guest if CR8 load
10410 * exits are enabled, CR8 store exits are enabled, and
10411 * virtualize APIC access is disabled; in this case
10412 * the processor would never use the TPR shadow and we
10413 * could simply clear the bit from the execution
10414 * control. But such a configuration is useless, so
10415 * let's keep the code simple.
10417 if (!is_error_page(page)) {
10418 vmx->nested.virtual_apic_page = page;
10419 hpa = page_to_phys(vmx->nested.virtual_apic_page);
10420 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, hpa);
10424 if (nested_cpu_has_posted_intr(vmcs12)) {
10425 if (vmx->nested.pi_desc_page) { /* shouldn't happen */
10426 kunmap(vmx->nested.pi_desc_page);
10427 kvm_release_page_dirty(vmx->nested.pi_desc_page);
10428 vmx->nested.pi_desc_page = NULL;
10430 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->posted_intr_desc_addr);
10431 if (is_error_page(page))
10433 vmx->nested.pi_desc_page = page;
10434 vmx->nested.pi_desc = kmap(vmx->nested.pi_desc_page);
10435 vmx->nested.pi_desc =
10436 (struct pi_desc *)((void *)vmx->nested.pi_desc +
10437 (unsigned long)(vmcs12->posted_intr_desc_addr &
10439 vmcs_write64(POSTED_INTR_DESC_ADDR,
10440 page_to_phys(vmx->nested.pi_desc_page) +
10441 (unsigned long)(vmcs12->posted_intr_desc_addr &
10444 if (nested_vmx_prepare_msr_bitmap(vcpu, vmcs12))
10445 vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL,
10446 CPU_BASED_USE_MSR_BITMAPS);
10448 vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL,
10449 CPU_BASED_USE_MSR_BITMAPS);
10452 static void vmx_start_preemption_timer(struct kvm_vcpu *vcpu)
10454 u64 preemption_timeout = get_vmcs12(vcpu)->vmx_preemption_timer_value;
10455 struct vcpu_vmx *vmx = to_vmx(vcpu);
10457 if (vcpu->arch.virtual_tsc_khz == 0)
10460 /* Make sure short timeouts reliably trigger an immediate vmexit.
10461 * hrtimer_start does not guarantee this. */
10462 if (preemption_timeout <= 1) {
10463 vmx_preemption_timer_fn(&vmx->nested.preemption_timer);
10467 preemption_timeout <<= VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE;
10468 preemption_timeout *= 1000000;
10469 do_div(preemption_timeout, vcpu->arch.virtual_tsc_khz);
10470 hrtimer_start(&vmx->nested.preemption_timer,
10471 ns_to_ktime(preemption_timeout), HRTIMER_MODE_REL);
10474 static int nested_vmx_check_io_bitmap_controls(struct kvm_vcpu *vcpu,
10475 struct vmcs12 *vmcs12)
10477 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_IO_BITMAPS))
10480 if (!page_address_valid(vcpu, vmcs12->io_bitmap_a) ||
10481 !page_address_valid(vcpu, vmcs12->io_bitmap_b))
10487 static int nested_vmx_check_msr_bitmap_controls(struct kvm_vcpu *vcpu,
10488 struct vmcs12 *vmcs12)
10490 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS))
10493 if (!page_address_valid(vcpu, vmcs12->msr_bitmap))
10499 static int nested_vmx_check_tpr_shadow_controls(struct kvm_vcpu *vcpu,
10500 struct vmcs12 *vmcs12)
10502 if (!nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW))
10505 if (!page_address_valid(vcpu, vmcs12->virtual_apic_page_addr))
10512 * Merge L0's and L1's MSR bitmap, return false to indicate that
10513 * we do not use the hardware.
10515 static inline bool nested_vmx_prepare_msr_bitmap(struct kvm_vcpu *vcpu,
10516 struct vmcs12 *vmcs12)
10520 unsigned long *msr_bitmap_l1;
10521 unsigned long *msr_bitmap_l0 = to_vmx(vcpu)->nested.vmcs02.msr_bitmap;
10523 * pred_cmd & spec_ctrl are trying to verify two things:
10525 * 1. L0 gave a permission to L1 to actually passthrough the MSR. This
10526 * ensures that we do not accidentally generate an L02 MSR bitmap
10527 * from the L12 MSR bitmap that is too permissive.
10528 * 2. That L1 or L2s have actually used the MSR. This avoids
10529 * unnecessarily merging of the bitmap if the MSR is unused. This
10530 * works properly because we only update the L01 MSR bitmap lazily.
10531 * So even if L0 should pass L1 these MSRs, the L01 bitmap is only
10532 * updated to reflect this when L1 (or its L2s) actually write to
10535 bool pred_cmd = !msr_write_intercepted_l01(vcpu, MSR_IA32_PRED_CMD);
10536 bool spec_ctrl = !msr_write_intercepted_l01(vcpu, MSR_IA32_SPEC_CTRL);
10538 /* Nothing to do if the MSR bitmap is not in use. */
10539 if (!cpu_has_vmx_msr_bitmap() ||
10540 !nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS))
10543 if (!nested_cpu_has_virt_x2apic_mode(vmcs12) &&
10544 !pred_cmd && !spec_ctrl)
10547 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->msr_bitmap);
10548 if (is_error_page(page))
10551 msr_bitmap_l1 = (unsigned long *)kmap(page);
10552 if (nested_cpu_has_apic_reg_virt(vmcs12)) {
10554 * L0 need not intercept reads for MSRs between 0x800 and 0x8ff, it
10555 * just lets the processor take the value from the virtual-APIC page;
10556 * take those 256 bits directly from the L1 bitmap.
10558 for (msr = 0x800; msr <= 0x8ff; msr += BITS_PER_LONG) {
10559 unsigned word = msr / BITS_PER_LONG;
10560 msr_bitmap_l0[word] = msr_bitmap_l1[word];
10561 msr_bitmap_l0[word + (0x800 / sizeof(long))] = ~0;
10564 for (msr = 0x800; msr <= 0x8ff; msr += BITS_PER_LONG) {
10565 unsigned word = msr / BITS_PER_LONG;
10566 msr_bitmap_l0[word] = ~0;
10567 msr_bitmap_l0[word + (0x800 / sizeof(long))] = ~0;
10571 nested_vmx_disable_intercept_for_msr(
10572 msr_bitmap_l1, msr_bitmap_l0,
10573 X2APIC_MSR(APIC_TASKPRI),
10576 if (nested_cpu_has_vid(vmcs12)) {
10577 nested_vmx_disable_intercept_for_msr(
10578 msr_bitmap_l1, msr_bitmap_l0,
10579 X2APIC_MSR(APIC_EOI),
10581 nested_vmx_disable_intercept_for_msr(
10582 msr_bitmap_l1, msr_bitmap_l0,
10583 X2APIC_MSR(APIC_SELF_IPI),
10588 nested_vmx_disable_intercept_for_msr(
10589 msr_bitmap_l1, msr_bitmap_l0,
10590 MSR_IA32_SPEC_CTRL,
10591 MSR_TYPE_R | MSR_TYPE_W);
10594 nested_vmx_disable_intercept_for_msr(
10595 msr_bitmap_l1, msr_bitmap_l0,
10600 kvm_release_page_clean(page);
10605 static int nested_vmx_check_apic_access_controls(struct kvm_vcpu *vcpu,
10606 struct vmcs12 *vmcs12)
10608 if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES) &&
10609 !page_address_valid(vcpu, vmcs12->apic_access_addr))
10615 static int nested_vmx_check_apicv_controls(struct kvm_vcpu *vcpu,
10616 struct vmcs12 *vmcs12)
10618 if (!nested_cpu_has_virt_x2apic_mode(vmcs12) &&
10619 !nested_cpu_has_apic_reg_virt(vmcs12) &&
10620 !nested_cpu_has_vid(vmcs12) &&
10621 !nested_cpu_has_posted_intr(vmcs12))
10625 * If virtualize x2apic mode is enabled,
10626 * virtualize apic access must be disabled.
10628 if (nested_cpu_has_virt_x2apic_mode(vmcs12) &&
10629 nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES))
10633 * If virtual interrupt delivery is enabled,
10634 * we must exit on external interrupts.
10636 if (nested_cpu_has_vid(vmcs12) &&
10637 !nested_exit_on_intr(vcpu))
10641 * bits 15:8 should be zero in posted_intr_nv,
10642 * the descriptor address has been already checked
10643 * in nested_get_vmcs12_pages.
10645 if (nested_cpu_has_posted_intr(vmcs12) &&
10646 (!nested_cpu_has_vid(vmcs12) ||
10647 !nested_exit_intr_ack_set(vcpu) ||
10648 vmcs12->posted_intr_nv & 0xff00))
10651 /* tpr shadow is needed by all apicv features. */
10652 if (!nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW))
10658 static int nested_vmx_check_msr_switch(struct kvm_vcpu *vcpu,
10659 unsigned long count_field,
10660 unsigned long addr_field)
10665 if (vmcs12_read_any(vcpu, count_field, &count) ||
10666 vmcs12_read_any(vcpu, addr_field, &addr)) {
10672 maxphyaddr = cpuid_maxphyaddr(vcpu);
10673 if (!IS_ALIGNED(addr, 16) || addr >> maxphyaddr ||
10674 (addr + count * sizeof(struct vmx_msr_entry) - 1) >> maxphyaddr) {
10675 pr_debug_ratelimited(
10676 "nVMX: invalid MSR switch (0x%lx, %d, %llu, 0x%08llx)",
10677 addr_field, maxphyaddr, count, addr);
10683 static int nested_vmx_check_msr_switch_controls(struct kvm_vcpu *vcpu,
10684 struct vmcs12 *vmcs12)
10686 if (vmcs12->vm_exit_msr_load_count == 0 &&
10687 vmcs12->vm_exit_msr_store_count == 0 &&
10688 vmcs12->vm_entry_msr_load_count == 0)
10689 return 0; /* Fast path */
10690 if (nested_vmx_check_msr_switch(vcpu, VM_EXIT_MSR_LOAD_COUNT,
10691 VM_EXIT_MSR_LOAD_ADDR) ||
10692 nested_vmx_check_msr_switch(vcpu, VM_EXIT_MSR_STORE_COUNT,
10693 VM_EXIT_MSR_STORE_ADDR) ||
10694 nested_vmx_check_msr_switch(vcpu, VM_ENTRY_MSR_LOAD_COUNT,
10695 VM_ENTRY_MSR_LOAD_ADDR))
10700 static int nested_vmx_check_pml_controls(struct kvm_vcpu *vcpu,
10701 struct vmcs12 *vmcs12)
10703 u64 address = vmcs12->pml_address;
10704 int maxphyaddr = cpuid_maxphyaddr(vcpu);
10706 if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_PML)) {
10707 if (!nested_cpu_has_ept(vmcs12) ||
10708 !IS_ALIGNED(address, 4096) ||
10709 address >> maxphyaddr)
10716 static int nested_vmx_msr_check_common(struct kvm_vcpu *vcpu,
10717 struct vmx_msr_entry *e)
10719 /* x2APIC MSR accesses are not allowed */
10720 if (vcpu->arch.apic_base & X2APIC_ENABLE && e->index >> 8 == 0x8)
10722 if (e->index == MSR_IA32_UCODE_WRITE || /* SDM Table 35-2 */
10723 e->index == MSR_IA32_UCODE_REV)
10725 if (e->reserved != 0)
10730 static int nested_vmx_load_msr_check(struct kvm_vcpu *vcpu,
10731 struct vmx_msr_entry *e)
10733 if (e->index == MSR_FS_BASE ||
10734 e->index == MSR_GS_BASE ||
10735 e->index == MSR_IA32_SMM_MONITOR_CTL || /* SMM is not supported */
10736 nested_vmx_msr_check_common(vcpu, e))
10741 static int nested_vmx_store_msr_check(struct kvm_vcpu *vcpu,
10742 struct vmx_msr_entry *e)
10744 if (e->index == MSR_IA32_SMBASE || /* SMM is not supported */
10745 nested_vmx_msr_check_common(vcpu, e))
10751 * Load guest's/host's msr at nested entry/exit.
10752 * return 0 for success, entry index for failure.
10754 static u32 nested_vmx_load_msr(struct kvm_vcpu *vcpu, u64 gpa, u32 count)
10757 struct vmx_msr_entry e;
10758 struct msr_data msr;
10760 msr.host_initiated = false;
10761 for (i = 0; i < count; i++) {
10762 if (kvm_vcpu_read_guest(vcpu, gpa + i * sizeof(e),
10764 pr_debug_ratelimited(
10765 "%s cannot read MSR entry (%u, 0x%08llx)\n",
10766 __func__, i, gpa + i * sizeof(e));
10769 if (nested_vmx_load_msr_check(vcpu, &e)) {
10770 pr_debug_ratelimited(
10771 "%s check failed (%u, 0x%x, 0x%x)\n",
10772 __func__, i, e.index, e.reserved);
10775 msr.index = e.index;
10776 msr.data = e.value;
10777 if (kvm_set_msr(vcpu, &msr)) {
10778 pr_debug_ratelimited(
10779 "%s cannot write MSR (%u, 0x%x, 0x%llx)\n",
10780 __func__, i, e.index, e.value);
10789 static int nested_vmx_store_msr(struct kvm_vcpu *vcpu, u64 gpa, u32 count)
10792 struct vmx_msr_entry e;
10794 for (i = 0; i < count; i++) {
10795 struct msr_data msr_info;
10796 if (kvm_vcpu_read_guest(vcpu,
10797 gpa + i * sizeof(e),
10798 &e, 2 * sizeof(u32))) {
10799 pr_debug_ratelimited(
10800 "%s cannot read MSR entry (%u, 0x%08llx)\n",
10801 __func__, i, gpa + i * sizeof(e));
10804 if (nested_vmx_store_msr_check(vcpu, &e)) {
10805 pr_debug_ratelimited(
10806 "%s check failed (%u, 0x%x, 0x%x)\n",
10807 __func__, i, e.index, e.reserved);
10810 msr_info.host_initiated = false;
10811 msr_info.index = e.index;
10812 if (kvm_get_msr(vcpu, &msr_info)) {
10813 pr_debug_ratelimited(
10814 "%s cannot read MSR (%u, 0x%x)\n",
10815 __func__, i, e.index);
10818 if (kvm_vcpu_write_guest(vcpu,
10819 gpa + i * sizeof(e) +
10820 offsetof(struct vmx_msr_entry, value),
10821 &msr_info.data, sizeof(msr_info.data))) {
10822 pr_debug_ratelimited(
10823 "%s cannot write MSR (%u, 0x%x, 0x%llx)\n",
10824 __func__, i, e.index, msr_info.data);
10831 static bool nested_cr3_valid(struct kvm_vcpu *vcpu, unsigned long val)
10833 unsigned long invalid_mask;
10835 invalid_mask = (~0ULL) << cpuid_maxphyaddr(vcpu);
10836 return (val & invalid_mask) == 0;
10840 * Load guest's/host's cr3 at nested entry/exit. nested_ept is true if we are
10841 * emulating VM entry into a guest with EPT enabled.
10842 * Returns 0 on success, 1 on failure. Invalid state exit qualification code
10843 * is assigned to entry_failure_code on failure.
10845 static int nested_vmx_load_cr3(struct kvm_vcpu *vcpu, unsigned long cr3, bool nested_ept,
10846 u32 *entry_failure_code)
10848 if (cr3 != kvm_read_cr3(vcpu) || (!nested_ept && pdptrs_changed(vcpu))) {
10849 if (!nested_cr3_valid(vcpu, cr3)) {
10850 *entry_failure_code = ENTRY_FAIL_DEFAULT;
10855 * If PAE paging and EPT are both on, CR3 is not used by the CPU and
10856 * must not be dereferenced.
10858 if (!is_long_mode(vcpu) && is_pae(vcpu) && is_paging(vcpu) &&
10860 if (!load_pdptrs(vcpu, vcpu->arch.walk_mmu, cr3)) {
10861 *entry_failure_code = ENTRY_FAIL_PDPTE;
10866 vcpu->arch.cr3 = cr3;
10867 __set_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail);
10870 kvm_mmu_reset_context(vcpu);
10874 static void prepare_vmcs02_full(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12,
10877 struct vcpu_vmx *vmx = to_vmx(vcpu);
10879 vmcs_write16(GUEST_ES_SELECTOR, vmcs12->guest_es_selector);
10880 vmcs_write16(GUEST_SS_SELECTOR, vmcs12->guest_ss_selector);
10881 vmcs_write16(GUEST_DS_SELECTOR, vmcs12->guest_ds_selector);
10882 vmcs_write16(GUEST_FS_SELECTOR, vmcs12->guest_fs_selector);
10883 vmcs_write16(GUEST_GS_SELECTOR, vmcs12->guest_gs_selector);
10884 vmcs_write16(GUEST_LDTR_SELECTOR, vmcs12->guest_ldtr_selector);
10885 vmcs_write16(GUEST_TR_SELECTOR, vmcs12->guest_tr_selector);
10886 vmcs_write32(GUEST_ES_LIMIT, vmcs12->guest_es_limit);
10887 vmcs_write32(GUEST_SS_LIMIT, vmcs12->guest_ss_limit);
10888 vmcs_write32(GUEST_DS_LIMIT, vmcs12->guest_ds_limit);
10889 vmcs_write32(GUEST_FS_LIMIT, vmcs12->guest_fs_limit);
10890 vmcs_write32(GUEST_GS_LIMIT, vmcs12->guest_gs_limit);
10891 vmcs_write32(GUEST_LDTR_LIMIT, vmcs12->guest_ldtr_limit);
10892 vmcs_write32(GUEST_TR_LIMIT, vmcs12->guest_tr_limit);
10893 vmcs_write32(GUEST_GDTR_LIMIT, vmcs12->guest_gdtr_limit);
10894 vmcs_write32(GUEST_IDTR_LIMIT, vmcs12->guest_idtr_limit);
10895 vmcs_write32(GUEST_ES_AR_BYTES, vmcs12->guest_es_ar_bytes);
10896 vmcs_write32(GUEST_SS_AR_BYTES, vmcs12->guest_ss_ar_bytes);
10897 vmcs_write32(GUEST_DS_AR_BYTES, vmcs12->guest_ds_ar_bytes);
10898 vmcs_write32(GUEST_FS_AR_BYTES, vmcs12->guest_fs_ar_bytes);
10899 vmcs_write32(GUEST_GS_AR_BYTES, vmcs12->guest_gs_ar_bytes);
10900 vmcs_write32(GUEST_LDTR_AR_BYTES, vmcs12->guest_ldtr_ar_bytes);
10901 vmcs_write32(GUEST_TR_AR_BYTES, vmcs12->guest_tr_ar_bytes);
10902 vmcs_writel(GUEST_SS_BASE, vmcs12->guest_ss_base);
10903 vmcs_writel(GUEST_DS_BASE, vmcs12->guest_ds_base);
10904 vmcs_writel(GUEST_FS_BASE, vmcs12->guest_fs_base);
10905 vmcs_writel(GUEST_GS_BASE, vmcs12->guest_gs_base);
10906 vmcs_writel(GUEST_LDTR_BASE, vmcs12->guest_ldtr_base);
10907 vmcs_writel(GUEST_TR_BASE, vmcs12->guest_tr_base);
10908 vmcs_writel(GUEST_GDTR_BASE, vmcs12->guest_gdtr_base);
10909 vmcs_writel(GUEST_IDTR_BASE, vmcs12->guest_idtr_base);
10911 vmcs_write32(GUEST_SYSENTER_CS, vmcs12->guest_sysenter_cs);
10912 vmcs_writel(GUEST_PENDING_DBG_EXCEPTIONS,
10913 vmcs12->guest_pending_dbg_exceptions);
10914 vmcs_writel(GUEST_SYSENTER_ESP, vmcs12->guest_sysenter_esp);
10915 vmcs_writel(GUEST_SYSENTER_EIP, vmcs12->guest_sysenter_eip);
10917 if (nested_cpu_has_xsaves(vmcs12))
10918 vmcs_write64(XSS_EXIT_BITMAP, vmcs12->xss_exit_bitmap);
10919 vmcs_write64(VMCS_LINK_POINTER, -1ull);
10921 if (cpu_has_vmx_posted_intr())
10922 vmcs_write16(POSTED_INTR_NV, POSTED_INTR_NESTED_VECTOR);
10925 * Whether page-faults are trapped is determined by a combination of
10926 * 3 settings: PFEC_MASK, PFEC_MATCH and EXCEPTION_BITMAP.PF.
10927 * If enable_ept, L0 doesn't care about page faults and we should
10928 * set all of these to L1's desires. However, if !enable_ept, L0 does
10929 * care about (at least some) page faults, and because it is not easy
10930 * (if at all possible?) to merge L0 and L1's desires, we simply ask
10931 * to exit on each and every L2 page fault. This is done by setting
10932 * MASK=MATCH=0 and (see below) EB.PF=1.
10933 * Note that below we don't need special code to set EB.PF beyond the
10934 * "or"ing of the EB of vmcs01 and vmcs12, because when enable_ept,
10935 * vmcs01's EB.PF is 0 so the "or" will take vmcs12's value, and when
10936 * !enable_ept, EB.PF is 1, so the "or" will always be 1.
10938 vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK,
10939 enable_ept ? vmcs12->page_fault_error_code_mask : 0);
10940 vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH,
10941 enable_ept ? vmcs12->page_fault_error_code_match : 0);
10943 /* All VMFUNCs are currently emulated through L0 vmexits. */
10944 if (cpu_has_vmx_vmfunc())
10945 vmcs_write64(VM_FUNCTION_CONTROL, 0);
10947 if (cpu_has_vmx_apicv()) {
10948 vmcs_write64(EOI_EXIT_BITMAP0, vmcs12->eoi_exit_bitmap0);
10949 vmcs_write64(EOI_EXIT_BITMAP1, vmcs12->eoi_exit_bitmap1);
10950 vmcs_write64(EOI_EXIT_BITMAP2, vmcs12->eoi_exit_bitmap2);
10951 vmcs_write64(EOI_EXIT_BITMAP3, vmcs12->eoi_exit_bitmap3);
10955 * Set host-state according to L0's settings (vmcs12 is irrelevant here)
10956 * Some constant fields are set here by vmx_set_constant_host_state().
10957 * Other fields are different per CPU, and will be set later when
10958 * vmx_vcpu_load() is called, and when vmx_save_host_state() is called.
10960 vmx_set_constant_host_state(vmx);
10963 * Set the MSR load/store lists to match L0's settings.
10965 vmcs_write32(VM_EXIT_MSR_STORE_COUNT, 0);
10966 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, vmx->msr_autoload.nr);
10967 vmcs_write64(VM_EXIT_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.host));
10968 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, vmx->msr_autoload.nr);
10969 vmcs_write64(VM_ENTRY_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.guest));
10971 set_cr4_guest_host_mask(vmx);
10973 if (vmx_mpx_supported())
10974 vmcs_write64(GUEST_BNDCFGS, vmcs12->guest_bndcfgs);
10977 if (nested_cpu_has_vpid(vmcs12) && vmx->nested.vpid02)
10978 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->nested.vpid02);
10980 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid);
10984 * L1 may access the L2's PDPTR, so save them to construct vmcs12
10987 vmcs_write64(GUEST_PDPTR0, vmcs12->guest_pdptr0);
10988 vmcs_write64(GUEST_PDPTR1, vmcs12->guest_pdptr1);
10989 vmcs_write64(GUEST_PDPTR2, vmcs12->guest_pdptr2);
10990 vmcs_write64(GUEST_PDPTR3, vmcs12->guest_pdptr3);
10993 if (cpu_has_vmx_msr_bitmap())
10994 vmcs_write64(MSR_BITMAP, __pa(vmx->nested.vmcs02.msr_bitmap));
10998 * prepare_vmcs02 is called when the L1 guest hypervisor runs its nested
10999 * L2 guest. L1 has a vmcs for L2 (vmcs12), and this function "merges" it
11000 * with L0's requirements for its guest (a.k.a. vmcs01), so we can run the L2
11001 * guest in a way that will both be appropriate to L1's requests, and our
11002 * needs. In addition to modifying the active vmcs (which is vmcs02), this
11003 * function also has additional necessary side-effects, like setting various
11004 * vcpu->arch fields.
11005 * Returns 0 on success, 1 on failure. Invalid state exit qualification code
11006 * is assigned to entry_failure_code on failure.
11008 static int prepare_vmcs02(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12,
11009 bool from_vmentry, u32 *entry_failure_code)
11011 struct vcpu_vmx *vmx = to_vmx(vcpu);
11012 u32 exec_control, vmcs12_exec_ctrl;
11014 if (vmx->nested.dirty_vmcs12) {
11015 prepare_vmcs02_full(vcpu, vmcs12, from_vmentry);
11016 vmx->nested.dirty_vmcs12 = false;
11020 * First, the fields that are shadowed. This must be kept in sync
11021 * with vmx_shadow_fields.h.
11024 vmcs_write16(GUEST_CS_SELECTOR, vmcs12->guest_cs_selector);
11025 vmcs_write32(GUEST_CS_LIMIT, vmcs12->guest_cs_limit);
11026 vmcs_write32(GUEST_CS_AR_BYTES, vmcs12->guest_cs_ar_bytes);
11027 vmcs_writel(GUEST_ES_BASE, vmcs12->guest_es_base);
11028 vmcs_writel(GUEST_CS_BASE, vmcs12->guest_cs_base);
11031 * Not in vmcs02: GUEST_PML_INDEX, HOST_FS_SELECTOR, HOST_GS_SELECTOR,
11032 * HOST_FS_BASE, HOST_GS_BASE.
11035 if (from_vmentry &&
11036 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS)) {
11037 kvm_set_dr(vcpu, 7, vmcs12->guest_dr7);
11038 vmcs_write64(GUEST_IA32_DEBUGCTL, vmcs12->guest_ia32_debugctl);
11040 kvm_set_dr(vcpu, 7, vcpu->arch.dr7);
11041 vmcs_write64(GUEST_IA32_DEBUGCTL, vmx->nested.vmcs01_debugctl);
11043 if (from_vmentry) {
11044 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
11045 vmcs12->vm_entry_intr_info_field);
11046 vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE,
11047 vmcs12->vm_entry_exception_error_code);
11048 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
11049 vmcs12->vm_entry_instruction_len);
11050 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO,
11051 vmcs12->guest_interruptibility_info);
11052 vmx->loaded_vmcs->nmi_known_unmasked =
11053 !(vmcs12->guest_interruptibility_info & GUEST_INTR_STATE_NMI);
11055 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0);
11057 vmx_set_rflags(vcpu, vmcs12->guest_rflags);
11059 exec_control = vmcs12->pin_based_vm_exec_control;
11061 /* Preemption timer setting is only taken from vmcs01. */
11062 exec_control &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
11063 exec_control |= vmcs_config.pin_based_exec_ctrl;
11064 if (vmx->hv_deadline_tsc == -1)
11065 exec_control &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
11067 /* Posted interrupts setting is only taken from vmcs12. */
11068 if (nested_cpu_has_posted_intr(vmcs12)) {
11069 vmx->nested.posted_intr_nv = vmcs12->posted_intr_nv;
11070 vmx->nested.pi_pending = false;
11072 exec_control &= ~PIN_BASED_POSTED_INTR;
11075 vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, exec_control);
11077 vmx->nested.preemption_timer_expired = false;
11078 if (nested_cpu_has_preemption_timer(vmcs12))
11079 vmx_start_preemption_timer(vcpu);
11081 if (cpu_has_secondary_exec_ctrls()) {
11082 exec_control = vmx->secondary_exec_control;
11084 /* Take the following fields only from vmcs12 */
11085 exec_control &= ~(SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
11086 SECONDARY_EXEC_ENABLE_INVPCID |
11087 SECONDARY_EXEC_RDTSCP |
11088 SECONDARY_EXEC_XSAVES |
11089 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY |
11090 SECONDARY_EXEC_APIC_REGISTER_VIRT |
11091 SECONDARY_EXEC_ENABLE_VMFUNC);
11092 if (nested_cpu_has(vmcs12,
11093 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS)) {
11094 vmcs12_exec_ctrl = vmcs12->secondary_vm_exec_control &
11095 ~SECONDARY_EXEC_ENABLE_PML;
11096 exec_control |= vmcs12_exec_ctrl;
11099 if (exec_control & SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY)
11100 vmcs_write16(GUEST_INTR_STATUS,
11101 vmcs12->guest_intr_status);
11104 * Write an illegal value to APIC_ACCESS_ADDR. Later,
11105 * nested_get_vmcs12_pages will either fix it up or
11106 * remove the VM execution control.
11108 if (exec_control & SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)
11109 vmcs_write64(APIC_ACCESS_ADDR, -1ull);
11111 vmcs_write32(SECONDARY_VM_EXEC_CONTROL, exec_control);
11115 * HOST_RSP is normally set correctly in vmx_vcpu_run() just before
11116 * entry, but only if the current (host) sp changed from the value
11117 * we wrote last (vmx->host_rsp). This cache is no longer relevant
11118 * if we switch vmcs, and rather than hold a separate cache per vmcs,
11119 * here we just force the write to happen on entry.
11123 exec_control = vmx_exec_control(vmx); /* L0's desires */
11124 exec_control &= ~CPU_BASED_VIRTUAL_INTR_PENDING;
11125 exec_control &= ~CPU_BASED_VIRTUAL_NMI_PENDING;
11126 exec_control &= ~CPU_BASED_TPR_SHADOW;
11127 exec_control |= vmcs12->cpu_based_vm_exec_control;
11130 * Write an illegal value to VIRTUAL_APIC_PAGE_ADDR. Later, if
11131 * nested_get_vmcs12_pages can't fix it up, the illegal value
11132 * will result in a VM entry failure.
11134 if (exec_control & CPU_BASED_TPR_SHADOW) {
11135 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, -1ull);
11136 vmcs_write32(TPR_THRESHOLD, vmcs12->tpr_threshold);
11138 #ifdef CONFIG_X86_64
11139 exec_control |= CPU_BASED_CR8_LOAD_EXITING |
11140 CPU_BASED_CR8_STORE_EXITING;
11145 * A vmexit (to either L1 hypervisor or L0 userspace) is always needed
11146 * for I/O port accesses.
11148 exec_control &= ~CPU_BASED_USE_IO_BITMAPS;
11149 exec_control |= CPU_BASED_UNCOND_IO_EXITING;
11151 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, exec_control);
11153 /* EXCEPTION_BITMAP and CR0_GUEST_HOST_MASK should basically be the
11154 * bitwise-or of what L1 wants to trap for L2, and what we want to
11155 * trap. Note that CR0.TS also needs updating - we do this later.
11157 update_exception_bitmap(vcpu);
11158 vcpu->arch.cr0_guest_owned_bits &= ~vmcs12->cr0_guest_host_mask;
11159 vmcs_writel(CR0_GUEST_HOST_MASK, ~vcpu->arch.cr0_guest_owned_bits);
11161 /* L2->L1 exit controls are emulated - the hardware exit is to L0 so
11162 * we should use its exit controls. Note that VM_EXIT_LOAD_IA32_EFER
11163 * bits are further modified by vmx_set_efer() below.
11165 vmcs_write32(VM_EXIT_CONTROLS, vmcs_config.vmexit_ctrl);
11167 /* vmcs12's VM_ENTRY_LOAD_IA32_EFER and VM_ENTRY_IA32E_MODE are
11168 * emulated by vmx_set_efer(), below.
11170 vm_entry_controls_init(vmx,
11171 (vmcs12->vm_entry_controls & ~VM_ENTRY_LOAD_IA32_EFER &
11172 ~VM_ENTRY_IA32E_MODE) |
11173 (vmcs_config.vmentry_ctrl & ~VM_ENTRY_IA32E_MODE));
11175 if (from_vmentry &&
11176 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_PAT)) {
11177 vmcs_write64(GUEST_IA32_PAT, vmcs12->guest_ia32_pat);
11178 vcpu->arch.pat = vmcs12->guest_ia32_pat;
11179 } else if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) {
11180 vmcs_write64(GUEST_IA32_PAT, vmx->vcpu.arch.pat);
11183 vmcs_write64(TSC_OFFSET, vcpu->arch.tsc_offset);
11185 if (kvm_has_tsc_control)
11186 decache_tsc_multiplier(vmx);
11190 * There is no direct mapping between vpid02 and vpid12, the
11191 * vpid02 is per-vCPU for L0 and reused while the value of
11192 * vpid12 is changed w/ one invvpid during nested vmentry.
11193 * The vpid12 is allocated by L1 for L2, so it will not
11194 * influence global bitmap(for vpid01 and vpid02 allocation)
11195 * even if spawn a lot of nested vCPUs.
11197 if (nested_cpu_has_vpid(vmcs12) && vmx->nested.vpid02) {
11198 if (vmcs12->virtual_processor_id != vmx->nested.last_vpid) {
11199 vmx->nested.last_vpid = vmcs12->virtual_processor_id;
11200 __vmx_flush_tlb(vcpu, to_vmx(vcpu)->nested.vpid02, true);
11203 vmx_flush_tlb(vcpu, true);
11209 * Conceptually we want to copy the PML address and index from
11210 * vmcs01 here, and then back to vmcs01 on nested vmexit. But,
11211 * since we always flush the log on each vmexit, this happens
11212 * to be equivalent to simply resetting the fields in vmcs02.
11214 ASSERT(vmx->pml_pg);
11215 vmcs_write64(PML_ADDRESS, page_to_phys(vmx->pml_pg));
11216 vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1);
11219 if (nested_cpu_has_ept(vmcs12)) {
11220 if (nested_ept_init_mmu_context(vcpu)) {
11221 *entry_failure_code = ENTRY_FAIL_DEFAULT;
11224 } else if (nested_cpu_has2(vmcs12,
11225 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) {
11226 vmx_flush_tlb(vcpu, true);
11230 * This sets GUEST_CR0 to vmcs12->guest_cr0, possibly modifying those
11231 * bits which we consider mandatory enabled.
11232 * The CR0_READ_SHADOW is what L2 should have expected to read given
11233 * the specifications by L1; It's not enough to take
11234 * vmcs12->cr0_read_shadow because on our cr0_guest_host_mask we we
11235 * have more bits than L1 expected.
11237 vmx_set_cr0(vcpu, vmcs12->guest_cr0);
11238 vmcs_writel(CR0_READ_SHADOW, nested_read_cr0(vmcs12));
11240 vmx_set_cr4(vcpu, vmcs12->guest_cr4);
11241 vmcs_writel(CR4_READ_SHADOW, nested_read_cr4(vmcs12));
11243 if (from_vmentry &&
11244 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_EFER))
11245 vcpu->arch.efer = vmcs12->guest_ia32_efer;
11246 else if (vmcs12->vm_entry_controls & VM_ENTRY_IA32E_MODE)
11247 vcpu->arch.efer |= (EFER_LMA | EFER_LME);
11249 vcpu->arch.efer &= ~(EFER_LMA | EFER_LME);
11250 /* Note: modifies VM_ENTRY/EXIT_CONTROLS and GUEST/HOST_IA32_EFER */
11251 vmx_set_efer(vcpu, vcpu->arch.efer);
11254 * Guest state is invalid and unrestricted guest is disabled,
11255 * which means L1 attempted VMEntry to L2 with invalid state.
11256 * Fail the VMEntry.
11258 if (vmx->emulation_required) {
11259 *entry_failure_code = ENTRY_FAIL_DEFAULT;
11263 /* Shadow page tables on either EPT or shadow page tables. */
11264 if (nested_vmx_load_cr3(vcpu, vmcs12->guest_cr3, nested_cpu_has_ept(vmcs12),
11265 entry_failure_code))
11269 vcpu->arch.walk_mmu->inject_page_fault = vmx_inject_page_fault_nested;
11271 kvm_register_write(vcpu, VCPU_REGS_RSP, vmcs12->guest_rsp);
11272 kvm_register_write(vcpu, VCPU_REGS_RIP, vmcs12->guest_rip);
11276 static int nested_vmx_check_nmi_controls(struct vmcs12 *vmcs12)
11278 if (!nested_cpu_has_nmi_exiting(vmcs12) &&
11279 nested_cpu_has_virtual_nmis(vmcs12))
11282 if (!nested_cpu_has_virtual_nmis(vmcs12) &&
11283 nested_cpu_has(vmcs12, CPU_BASED_VIRTUAL_NMI_PENDING))
11289 static int check_vmentry_prereqs(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
11291 struct vcpu_vmx *vmx = to_vmx(vcpu);
11293 if (vmcs12->guest_activity_state != GUEST_ACTIVITY_ACTIVE &&
11294 vmcs12->guest_activity_state != GUEST_ACTIVITY_HLT)
11295 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11297 if (nested_vmx_check_io_bitmap_controls(vcpu, vmcs12))
11298 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11300 if (nested_vmx_check_msr_bitmap_controls(vcpu, vmcs12))
11301 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11303 if (nested_vmx_check_apic_access_controls(vcpu, vmcs12))
11304 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11306 if (nested_vmx_check_tpr_shadow_controls(vcpu, vmcs12))
11307 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11309 if (nested_vmx_check_apicv_controls(vcpu, vmcs12))
11310 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11312 if (nested_vmx_check_msr_switch_controls(vcpu, vmcs12))
11313 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11315 if (nested_vmx_check_pml_controls(vcpu, vmcs12))
11316 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11318 if (!vmx_control_verify(vmcs12->cpu_based_vm_exec_control,
11319 vmx->nested.msrs.procbased_ctls_low,
11320 vmx->nested.msrs.procbased_ctls_high) ||
11321 (nested_cpu_has(vmcs12, CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) &&
11322 !vmx_control_verify(vmcs12->secondary_vm_exec_control,
11323 vmx->nested.msrs.secondary_ctls_low,
11324 vmx->nested.msrs.secondary_ctls_high)) ||
11325 !vmx_control_verify(vmcs12->pin_based_vm_exec_control,
11326 vmx->nested.msrs.pinbased_ctls_low,
11327 vmx->nested.msrs.pinbased_ctls_high) ||
11328 !vmx_control_verify(vmcs12->vm_exit_controls,
11329 vmx->nested.msrs.exit_ctls_low,
11330 vmx->nested.msrs.exit_ctls_high) ||
11331 !vmx_control_verify(vmcs12->vm_entry_controls,
11332 vmx->nested.msrs.entry_ctls_low,
11333 vmx->nested.msrs.entry_ctls_high))
11334 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11336 if (nested_vmx_check_nmi_controls(vmcs12))
11337 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11339 if (nested_cpu_has_vmfunc(vmcs12)) {
11340 if (vmcs12->vm_function_control &
11341 ~vmx->nested.msrs.vmfunc_controls)
11342 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11344 if (nested_cpu_has_eptp_switching(vmcs12)) {
11345 if (!nested_cpu_has_ept(vmcs12) ||
11346 !page_address_valid(vcpu, vmcs12->eptp_list_address))
11347 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11351 if (vmcs12->cr3_target_count > nested_cpu_vmx_misc_cr3_count(vcpu))
11352 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11354 if (!nested_host_cr0_valid(vcpu, vmcs12->host_cr0) ||
11355 !nested_host_cr4_valid(vcpu, vmcs12->host_cr4) ||
11356 !nested_cr3_valid(vcpu, vmcs12->host_cr3))
11357 return VMXERR_ENTRY_INVALID_HOST_STATE_FIELD;
11362 static int check_vmentry_postreqs(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12,
11367 *exit_qual = ENTRY_FAIL_DEFAULT;
11369 if (!nested_guest_cr0_valid(vcpu, vmcs12->guest_cr0) ||
11370 !nested_guest_cr4_valid(vcpu, vmcs12->guest_cr4))
11373 if (!nested_cpu_has2(vmcs12, SECONDARY_EXEC_SHADOW_VMCS) &&
11374 vmcs12->vmcs_link_pointer != -1ull) {
11375 *exit_qual = ENTRY_FAIL_VMCS_LINK_PTR;
11380 * If the load IA32_EFER VM-entry control is 1, the following checks
11381 * are performed on the field for the IA32_EFER MSR:
11382 * - Bits reserved in the IA32_EFER MSR must be 0.
11383 * - Bit 10 (corresponding to IA32_EFER.LMA) must equal the value of
11384 * the IA-32e mode guest VM-exit control. It must also be identical
11385 * to bit 8 (LME) if bit 31 in the CR0 field (corresponding to
11388 if (to_vmx(vcpu)->nested.nested_run_pending &&
11389 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_EFER)) {
11390 ia32e = (vmcs12->vm_entry_controls & VM_ENTRY_IA32E_MODE) != 0;
11391 if (!kvm_valid_efer(vcpu, vmcs12->guest_ia32_efer) ||
11392 ia32e != !!(vmcs12->guest_ia32_efer & EFER_LMA) ||
11393 ((vmcs12->guest_cr0 & X86_CR0_PG) &&
11394 ia32e != !!(vmcs12->guest_ia32_efer & EFER_LME)))
11399 * If the load IA32_EFER VM-exit control is 1, bits reserved in the
11400 * IA32_EFER MSR must be 0 in the field for that register. In addition,
11401 * the values of the LMA and LME bits in the field must each be that of
11402 * the host address-space size VM-exit control.
11404 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_EFER) {
11405 ia32e = (vmcs12->vm_exit_controls &
11406 VM_EXIT_HOST_ADDR_SPACE_SIZE) != 0;
11407 if (!kvm_valid_efer(vcpu, vmcs12->host_ia32_efer) ||
11408 ia32e != !!(vmcs12->host_ia32_efer & EFER_LMA) ||
11409 ia32e != !!(vmcs12->host_ia32_efer & EFER_LME))
11413 if ((vmcs12->vm_entry_controls & VM_ENTRY_LOAD_BNDCFGS) &&
11414 (is_noncanonical_address(vmcs12->guest_bndcfgs & PAGE_MASK, vcpu) ||
11415 (vmcs12->guest_bndcfgs & MSR_IA32_BNDCFGS_RSVD)))
11421 static int enter_vmx_non_root_mode(struct kvm_vcpu *vcpu, bool from_vmentry)
11423 struct vcpu_vmx *vmx = to_vmx(vcpu);
11424 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
11429 enter_guest_mode(vcpu);
11431 if (!(vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS))
11432 vmx->nested.vmcs01_debugctl = vmcs_read64(GUEST_IA32_DEBUGCTL);
11434 vmx_switch_vmcs(vcpu, &vmx->nested.vmcs02);
11435 vmx_segment_cache_clear(vmx);
11437 if (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING)
11438 vcpu->arch.tsc_offset += vmcs12->tsc_offset;
11440 r = EXIT_REASON_INVALID_STATE;
11441 if (prepare_vmcs02(vcpu, vmcs12, from_vmentry, &exit_qual))
11444 nested_get_vmcs12_pages(vcpu, vmcs12);
11446 r = EXIT_REASON_MSR_LOAD_FAIL;
11447 msr_entry_idx = nested_vmx_load_msr(vcpu,
11448 vmcs12->vm_entry_msr_load_addr,
11449 vmcs12->vm_entry_msr_load_count);
11454 * Note no nested_vmx_succeed or nested_vmx_fail here. At this point
11455 * we are no longer running L1, and VMLAUNCH/VMRESUME has not yet
11456 * returned as far as L1 is concerned. It will only return (and set
11457 * the success flag) when L2 exits (see nested_vmx_vmexit()).
11462 if (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING)
11463 vcpu->arch.tsc_offset -= vmcs12->tsc_offset;
11464 leave_guest_mode(vcpu);
11465 vmx_switch_vmcs(vcpu, &vmx->vmcs01);
11466 nested_vmx_entry_failure(vcpu, vmcs12, r, exit_qual);
11471 * nested_vmx_run() handles a nested entry, i.e., a VMLAUNCH or VMRESUME on L1
11472 * for running an L2 nested guest.
11474 static int nested_vmx_run(struct kvm_vcpu *vcpu, bool launch)
11476 struct vmcs12 *vmcs12;
11477 struct vcpu_vmx *vmx = to_vmx(vcpu);
11478 u32 interrupt_shadow = vmx_get_interrupt_shadow(vcpu);
11482 if (!nested_vmx_check_permission(vcpu))
11485 if (!nested_vmx_check_vmcs12(vcpu))
11488 vmcs12 = get_vmcs12(vcpu);
11490 if (enable_shadow_vmcs)
11491 copy_shadow_to_vmcs12(vmx);
11494 * The nested entry process starts with enforcing various prerequisites
11495 * on vmcs12 as required by the Intel SDM, and act appropriately when
11496 * they fail: As the SDM explains, some conditions should cause the
11497 * instruction to fail, while others will cause the instruction to seem
11498 * to succeed, but return an EXIT_REASON_INVALID_STATE.
11499 * To speed up the normal (success) code path, we should avoid checking
11500 * for misconfigurations which will anyway be caught by the processor
11501 * when using the merged vmcs02.
11503 if (interrupt_shadow & KVM_X86_SHADOW_INT_MOV_SS) {
11504 nested_vmx_failValid(vcpu,
11505 VMXERR_ENTRY_EVENTS_BLOCKED_BY_MOV_SS);
11509 if (vmcs12->launch_state == launch) {
11510 nested_vmx_failValid(vcpu,
11511 launch ? VMXERR_VMLAUNCH_NONCLEAR_VMCS
11512 : VMXERR_VMRESUME_NONLAUNCHED_VMCS);
11516 ret = check_vmentry_prereqs(vcpu, vmcs12);
11518 nested_vmx_failValid(vcpu, ret);
11523 * After this point, the trap flag no longer triggers a singlestep trap
11524 * on the vm entry instructions; don't call kvm_skip_emulated_instruction.
11525 * This is not 100% correct; for performance reasons, we delegate most
11526 * of the checks on host state to the processor. If those fail,
11527 * the singlestep trap is missed.
11529 skip_emulated_instruction(vcpu);
11531 ret = check_vmentry_postreqs(vcpu, vmcs12, &exit_qual);
11533 nested_vmx_entry_failure(vcpu, vmcs12,
11534 EXIT_REASON_INVALID_STATE, exit_qual);
11539 * We're finally done with prerequisite checking, and can start with
11540 * the nested entry.
11543 ret = enter_vmx_non_root_mode(vcpu, true);
11548 * If we're entering a halted L2 vcpu and the L2 vcpu won't be woken
11549 * by event injection, halt vcpu.
11551 if ((vmcs12->guest_activity_state == GUEST_ACTIVITY_HLT) &&
11552 !(vmcs12->vm_entry_intr_info_field & INTR_INFO_VALID_MASK))
11553 return kvm_vcpu_halt(vcpu);
11555 vmx->nested.nested_run_pending = 1;
11560 return kvm_skip_emulated_instruction(vcpu);
11564 * On a nested exit from L2 to L1, vmcs12.guest_cr0 might not be up-to-date
11565 * because L2 may have changed some cr0 bits directly (CRO_GUEST_HOST_MASK).
11566 * This function returns the new value we should put in vmcs12.guest_cr0.
11567 * It's not enough to just return the vmcs02 GUEST_CR0. Rather,
11568 * 1. Bits that neither L0 nor L1 trapped, were set directly by L2 and are now
11569 * available in vmcs02 GUEST_CR0. (Note: It's enough to check that L0
11570 * didn't trap the bit, because if L1 did, so would L0).
11571 * 2. Bits that L1 asked to trap (and therefore L0 also did) could not have
11572 * been modified by L2, and L1 knows it. So just leave the old value of
11573 * the bit from vmcs12.guest_cr0. Note that the bit from vmcs02 GUEST_CR0
11574 * isn't relevant, because if L0 traps this bit it can set it to anything.
11575 * 3. Bits that L1 didn't trap, but L0 did. L1 believes the guest could have
11576 * changed these bits, and therefore they need to be updated, but L0
11577 * didn't necessarily allow them to be changed in GUEST_CR0 - and rather
11578 * put them in vmcs02 CR0_READ_SHADOW. So take these bits from there.
11580 static inline unsigned long
11581 vmcs12_guest_cr0(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
11584 /*1*/ (vmcs_readl(GUEST_CR0) & vcpu->arch.cr0_guest_owned_bits) |
11585 /*2*/ (vmcs12->guest_cr0 & vmcs12->cr0_guest_host_mask) |
11586 /*3*/ (vmcs_readl(CR0_READ_SHADOW) & ~(vmcs12->cr0_guest_host_mask |
11587 vcpu->arch.cr0_guest_owned_bits));
11590 static inline unsigned long
11591 vmcs12_guest_cr4(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
11594 /*1*/ (vmcs_readl(GUEST_CR4) & vcpu->arch.cr4_guest_owned_bits) |
11595 /*2*/ (vmcs12->guest_cr4 & vmcs12->cr4_guest_host_mask) |
11596 /*3*/ (vmcs_readl(CR4_READ_SHADOW) & ~(vmcs12->cr4_guest_host_mask |
11597 vcpu->arch.cr4_guest_owned_bits));
11600 static void vmcs12_save_pending_event(struct kvm_vcpu *vcpu,
11601 struct vmcs12 *vmcs12)
11606 if (vcpu->arch.exception.injected) {
11607 nr = vcpu->arch.exception.nr;
11608 idt_vectoring = nr | VECTORING_INFO_VALID_MASK;
11610 if (kvm_exception_is_soft(nr)) {
11611 vmcs12->vm_exit_instruction_len =
11612 vcpu->arch.event_exit_inst_len;
11613 idt_vectoring |= INTR_TYPE_SOFT_EXCEPTION;
11615 idt_vectoring |= INTR_TYPE_HARD_EXCEPTION;
11617 if (vcpu->arch.exception.has_error_code) {
11618 idt_vectoring |= VECTORING_INFO_DELIVER_CODE_MASK;
11619 vmcs12->idt_vectoring_error_code =
11620 vcpu->arch.exception.error_code;
11623 vmcs12->idt_vectoring_info_field = idt_vectoring;
11624 } else if (vcpu->arch.nmi_injected) {
11625 vmcs12->idt_vectoring_info_field =
11626 INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK | NMI_VECTOR;
11627 } else if (vcpu->arch.interrupt.injected) {
11628 nr = vcpu->arch.interrupt.nr;
11629 idt_vectoring = nr | VECTORING_INFO_VALID_MASK;
11631 if (vcpu->arch.interrupt.soft) {
11632 idt_vectoring |= INTR_TYPE_SOFT_INTR;
11633 vmcs12->vm_entry_instruction_len =
11634 vcpu->arch.event_exit_inst_len;
11636 idt_vectoring |= INTR_TYPE_EXT_INTR;
11638 vmcs12->idt_vectoring_info_field = idt_vectoring;
11642 static int vmx_check_nested_events(struct kvm_vcpu *vcpu, bool external_intr)
11644 struct vcpu_vmx *vmx = to_vmx(vcpu);
11645 unsigned long exit_qual;
11646 bool block_nested_events =
11647 vmx->nested.nested_run_pending || kvm_event_needs_reinjection(vcpu);
11649 if (vcpu->arch.exception.pending &&
11650 nested_vmx_check_exception(vcpu, &exit_qual)) {
11651 if (block_nested_events)
11653 nested_vmx_inject_exception_vmexit(vcpu, exit_qual);
11657 if (nested_cpu_has_preemption_timer(get_vmcs12(vcpu)) &&
11658 vmx->nested.preemption_timer_expired) {
11659 if (block_nested_events)
11661 nested_vmx_vmexit(vcpu, EXIT_REASON_PREEMPTION_TIMER, 0, 0);
11665 if (vcpu->arch.nmi_pending && nested_exit_on_nmi(vcpu)) {
11666 if (block_nested_events)
11668 nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI,
11669 NMI_VECTOR | INTR_TYPE_NMI_INTR |
11670 INTR_INFO_VALID_MASK, 0);
11672 * The NMI-triggered VM exit counts as injection:
11673 * clear this one and block further NMIs.
11675 vcpu->arch.nmi_pending = 0;
11676 vmx_set_nmi_mask(vcpu, true);
11680 if ((kvm_cpu_has_interrupt(vcpu) || external_intr) &&
11681 nested_exit_on_intr(vcpu)) {
11682 if (block_nested_events)
11684 nested_vmx_vmexit(vcpu, EXIT_REASON_EXTERNAL_INTERRUPT, 0, 0);
11688 vmx_complete_nested_posted_interrupt(vcpu);
11692 static u32 vmx_get_preemption_timer_value(struct kvm_vcpu *vcpu)
11694 ktime_t remaining =
11695 hrtimer_get_remaining(&to_vmx(vcpu)->nested.preemption_timer);
11698 if (ktime_to_ns(remaining) <= 0)
11701 value = ktime_to_ns(remaining) * vcpu->arch.virtual_tsc_khz;
11702 do_div(value, 1000000);
11703 return value >> VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE;
11707 * Update the guest state fields of vmcs12 to reflect changes that
11708 * occurred while L2 was running. (The "IA-32e mode guest" bit of the
11709 * VM-entry controls is also updated, since this is really a guest
11712 static void sync_vmcs12(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
11714 vmcs12->guest_cr0 = vmcs12_guest_cr0(vcpu, vmcs12);
11715 vmcs12->guest_cr4 = vmcs12_guest_cr4(vcpu, vmcs12);
11717 vmcs12->guest_rsp = kvm_register_read(vcpu, VCPU_REGS_RSP);
11718 vmcs12->guest_rip = kvm_register_read(vcpu, VCPU_REGS_RIP);
11719 vmcs12->guest_rflags = vmcs_readl(GUEST_RFLAGS);
11721 vmcs12->guest_es_selector = vmcs_read16(GUEST_ES_SELECTOR);
11722 vmcs12->guest_cs_selector = vmcs_read16(GUEST_CS_SELECTOR);
11723 vmcs12->guest_ss_selector = vmcs_read16(GUEST_SS_SELECTOR);
11724 vmcs12->guest_ds_selector = vmcs_read16(GUEST_DS_SELECTOR);
11725 vmcs12->guest_fs_selector = vmcs_read16(GUEST_FS_SELECTOR);
11726 vmcs12->guest_gs_selector = vmcs_read16(GUEST_GS_SELECTOR);
11727 vmcs12->guest_ldtr_selector = vmcs_read16(GUEST_LDTR_SELECTOR);
11728 vmcs12->guest_tr_selector = vmcs_read16(GUEST_TR_SELECTOR);
11729 vmcs12->guest_es_limit = vmcs_read32(GUEST_ES_LIMIT);
11730 vmcs12->guest_cs_limit = vmcs_read32(GUEST_CS_LIMIT);
11731 vmcs12->guest_ss_limit = vmcs_read32(GUEST_SS_LIMIT);
11732 vmcs12->guest_ds_limit = vmcs_read32(GUEST_DS_LIMIT);
11733 vmcs12->guest_fs_limit = vmcs_read32(GUEST_FS_LIMIT);
11734 vmcs12->guest_gs_limit = vmcs_read32(GUEST_GS_LIMIT);
11735 vmcs12->guest_ldtr_limit = vmcs_read32(GUEST_LDTR_LIMIT);
11736 vmcs12->guest_tr_limit = vmcs_read32(GUEST_TR_LIMIT);
11737 vmcs12->guest_gdtr_limit = vmcs_read32(GUEST_GDTR_LIMIT);
11738 vmcs12->guest_idtr_limit = vmcs_read32(GUEST_IDTR_LIMIT);
11739 vmcs12->guest_es_ar_bytes = vmcs_read32(GUEST_ES_AR_BYTES);
11740 vmcs12->guest_cs_ar_bytes = vmcs_read32(GUEST_CS_AR_BYTES);
11741 vmcs12->guest_ss_ar_bytes = vmcs_read32(GUEST_SS_AR_BYTES);
11742 vmcs12->guest_ds_ar_bytes = vmcs_read32(GUEST_DS_AR_BYTES);
11743 vmcs12->guest_fs_ar_bytes = vmcs_read32(GUEST_FS_AR_BYTES);
11744 vmcs12->guest_gs_ar_bytes = vmcs_read32(GUEST_GS_AR_BYTES);
11745 vmcs12->guest_ldtr_ar_bytes = vmcs_read32(GUEST_LDTR_AR_BYTES);
11746 vmcs12->guest_tr_ar_bytes = vmcs_read32(GUEST_TR_AR_BYTES);
11747 vmcs12->guest_es_base = vmcs_readl(GUEST_ES_BASE);
11748 vmcs12->guest_cs_base = vmcs_readl(GUEST_CS_BASE);
11749 vmcs12->guest_ss_base = vmcs_readl(GUEST_SS_BASE);
11750 vmcs12->guest_ds_base = vmcs_readl(GUEST_DS_BASE);
11751 vmcs12->guest_fs_base = vmcs_readl(GUEST_FS_BASE);
11752 vmcs12->guest_gs_base = vmcs_readl(GUEST_GS_BASE);
11753 vmcs12->guest_ldtr_base = vmcs_readl(GUEST_LDTR_BASE);
11754 vmcs12->guest_tr_base = vmcs_readl(GUEST_TR_BASE);
11755 vmcs12->guest_gdtr_base = vmcs_readl(GUEST_GDTR_BASE);
11756 vmcs12->guest_idtr_base = vmcs_readl(GUEST_IDTR_BASE);
11758 vmcs12->guest_interruptibility_info =
11759 vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
11760 vmcs12->guest_pending_dbg_exceptions =
11761 vmcs_readl(GUEST_PENDING_DBG_EXCEPTIONS);
11762 if (vcpu->arch.mp_state == KVM_MP_STATE_HALTED)
11763 vmcs12->guest_activity_state = GUEST_ACTIVITY_HLT;
11765 vmcs12->guest_activity_state = GUEST_ACTIVITY_ACTIVE;
11767 if (nested_cpu_has_preemption_timer(vmcs12)) {
11768 if (vmcs12->vm_exit_controls &
11769 VM_EXIT_SAVE_VMX_PREEMPTION_TIMER)
11770 vmcs12->vmx_preemption_timer_value =
11771 vmx_get_preemption_timer_value(vcpu);
11772 hrtimer_cancel(&to_vmx(vcpu)->nested.preemption_timer);
11776 * In some cases (usually, nested EPT), L2 is allowed to change its
11777 * own CR3 without exiting. If it has changed it, we must keep it.
11778 * Of course, if L0 is using shadow page tables, GUEST_CR3 was defined
11779 * by L0, not L1 or L2, so we mustn't unconditionally copy it to vmcs12.
11781 * Additionally, restore L2's PDPTR to vmcs12.
11784 vmcs12->guest_cr3 = vmcs_readl(GUEST_CR3);
11785 vmcs12->guest_pdptr0 = vmcs_read64(GUEST_PDPTR0);
11786 vmcs12->guest_pdptr1 = vmcs_read64(GUEST_PDPTR1);
11787 vmcs12->guest_pdptr2 = vmcs_read64(GUEST_PDPTR2);
11788 vmcs12->guest_pdptr3 = vmcs_read64(GUEST_PDPTR3);
11791 vmcs12->guest_linear_address = vmcs_readl(GUEST_LINEAR_ADDRESS);
11793 if (nested_cpu_has_vid(vmcs12))
11794 vmcs12->guest_intr_status = vmcs_read16(GUEST_INTR_STATUS);
11796 vmcs12->vm_entry_controls =
11797 (vmcs12->vm_entry_controls & ~VM_ENTRY_IA32E_MODE) |
11798 (vm_entry_controls_get(to_vmx(vcpu)) & VM_ENTRY_IA32E_MODE);
11800 if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_DEBUG_CONTROLS) {
11801 kvm_get_dr(vcpu, 7, (unsigned long *)&vmcs12->guest_dr7);
11802 vmcs12->guest_ia32_debugctl = vmcs_read64(GUEST_IA32_DEBUGCTL);
11805 /* TODO: These cannot have changed unless we have MSR bitmaps and
11806 * the relevant bit asks not to trap the change */
11807 if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_IA32_PAT)
11808 vmcs12->guest_ia32_pat = vmcs_read64(GUEST_IA32_PAT);
11809 if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_IA32_EFER)
11810 vmcs12->guest_ia32_efer = vcpu->arch.efer;
11811 vmcs12->guest_sysenter_cs = vmcs_read32(GUEST_SYSENTER_CS);
11812 vmcs12->guest_sysenter_esp = vmcs_readl(GUEST_SYSENTER_ESP);
11813 vmcs12->guest_sysenter_eip = vmcs_readl(GUEST_SYSENTER_EIP);
11814 if (kvm_mpx_supported())
11815 vmcs12->guest_bndcfgs = vmcs_read64(GUEST_BNDCFGS);
11819 * prepare_vmcs12 is part of what we need to do when the nested L2 guest exits
11820 * and we want to prepare to run its L1 parent. L1 keeps a vmcs for L2 (vmcs12),
11821 * and this function updates it to reflect the changes to the guest state while
11822 * L2 was running (and perhaps made some exits which were handled directly by L0
11823 * without going back to L1), and to reflect the exit reason.
11824 * Note that we do not have to copy here all VMCS fields, just those that
11825 * could have changed by the L2 guest or the exit - i.e., the guest-state and
11826 * exit-information fields only. Other fields are modified by L1 with VMWRITE,
11827 * which already writes to vmcs12 directly.
11829 static void prepare_vmcs12(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12,
11830 u32 exit_reason, u32 exit_intr_info,
11831 unsigned long exit_qualification)
11833 /* update guest state fields: */
11834 sync_vmcs12(vcpu, vmcs12);
11836 /* update exit information fields: */
11838 vmcs12->vm_exit_reason = exit_reason;
11839 vmcs12->exit_qualification = exit_qualification;
11840 vmcs12->vm_exit_intr_info = exit_intr_info;
11842 vmcs12->idt_vectoring_info_field = 0;
11843 vmcs12->vm_exit_instruction_len = vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
11844 vmcs12->vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
11846 if (!(vmcs12->vm_exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY)) {
11847 vmcs12->launch_state = 1;
11849 /* vm_entry_intr_info_field is cleared on exit. Emulate this
11850 * instead of reading the real value. */
11851 vmcs12->vm_entry_intr_info_field &= ~INTR_INFO_VALID_MASK;
11854 * Transfer the event that L0 or L1 may wanted to inject into
11855 * L2 to IDT_VECTORING_INFO_FIELD.
11857 vmcs12_save_pending_event(vcpu, vmcs12);
11861 * Drop what we picked up for L2 via vmx_complete_interrupts. It is
11862 * preserved above and would only end up incorrectly in L1.
11864 vcpu->arch.nmi_injected = false;
11865 kvm_clear_exception_queue(vcpu);
11866 kvm_clear_interrupt_queue(vcpu);
11869 static void load_vmcs12_mmu_host_state(struct kvm_vcpu *vcpu,
11870 struct vmcs12 *vmcs12)
11872 u32 entry_failure_code;
11874 nested_ept_uninit_mmu_context(vcpu);
11877 * Only PDPTE load can fail as the value of cr3 was checked on entry and
11878 * couldn't have changed.
11880 if (nested_vmx_load_cr3(vcpu, vmcs12->host_cr3, false, &entry_failure_code))
11881 nested_vmx_abort(vcpu, VMX_ABORT_LOAD_HOST_PDPTE_FAIL);
11884 vcpu->arch.walk_mmu->inject_page_fault = kvm_inject_page_fault;
11888 * A part of what we need to when the nested L2 guest exits and we want to
11889 * run its L1 parent, is to reset L1's guest state to the host state specified
11891 * This function is to be called not only on normal nested exit, but also on
11892 * a nested entry failure, as explained in Intel's spec, 3B.23.7 ("VM-Entry
11893 * Failures During or After Loading Guest State").
11894 * This function should be called when the active VMCS is L1's (vmcs01).
11896 static void load_vmcs12_host_state(struct kvm_vcpu *vcpu,
11897 struct vmcs12 *vmcs12)
11899 struct kvm_segment seg;
11901 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_EFER)
11902 vcpu->arch.efer = vmcs12->host_ia32_efer;
11903 else if (vmcs12->vm_exit_controls & VM_EXIT_HOST_ADDR_SPACE_SIZE)
11904 vcpu->arch.efer |= (EFER_LMA | EFER_LME);
11906 vcpu->arch.efer &= ~(EFER_LMA | EFER_LME);
11907 vmx_set_efer(vcpu, vcpu->arch.efer);
11909 kvm_register_write(vcpu, VCPU_REGS_RSP, vmcs12->host_rsp);
11910 kvm_register_write(vcpu, VCPU_REGS_RIP, vmcs12->host_rip);
11911 vmx_set_rflags(vcpu, X86_EFLAGS_FIXED);
11913 * Note that calling vmx_set_cr0 is important, even if cr0 hasn't
11914 * actually changed, because vmx_set_cr0 refers to efer set above.
11916 * CR0_GUEST_HOST_MASK is already set in the original vmcs01
11917 * (KVM doesn't change it);
11919 vcpu->arch.cr0_guest_owned_bits = X86_CR0_TS;
11920 vmx_set_cr0(vcpu, vmcs12->host_cr0);
11922 /* Same as above - no reason to call set_cr4_guest_host_mask(). */
11923 vcpu->arch.cr4_guest_owned_bits = ~vmcs_readl(CR4_GUEST_HOST_MASK);
11924 vmx_set_cr4(vcpu, vmcs12->host_cr4);
11926 load_vmcs12_mmu_host_state(vcpu, vmcs12);
11930 * Trivially support vpid by letting L2s share their parent
11931 * L1's vpid. TODO: move to a more elaborate solution, giving
11932 * each L2 its own vpid and exposing the vpid feature to L1.
11934 vmx_flush_tlb(vcpu, true);
11937 vmcs_write32(GUEST_SYSENTER_CS, vmcs12->host_ia32_sysenter_cs);
11938 vmcs_writel(GUEST_SYSENTER_ESP, vmcs12->host_ia32_sysenter_esp);
11939 vmcs_writel(GUEST_SYSENTER_EIP, vmcs12->host_ia32_sysenter_eip);
11940 vmcs_writel(GUEST_IDTR_BASE, vmcs12->host_idtr_base);
11941 vmcs_writel(GUEST_GDTR_BASE, vmcs12->host_gdtr_base);
11942 vmcs_write32(GUEST_IDTR_LIMIT, 0xFFFF);
11943 vmcs_write32(GUEST_GDTR_LIMIT, 0xFFFF);
11945 /* If not VM_EXIT_CLEAR_BNDCFGS, the L2 value propagates to L1. */
11946 if (vmcs12->vm_exit_controls & VM_EXIT_CLEAR_BNDCFGS)
11947 vmcs_write64(GUEST_BNDCFGS, 0);
11949 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PAT) {
11950 vmcs_write64(GUEST_IA32_PAT, vmcs12->host_ia32_pat);
11951 vcpu->arch.pat = vmcs12->host_ia32_pat;
11953 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL)
11954 vmcs_write64(GUEST_IA32_PERF_GLOBAL_CTRL,
11955 vmcs12->host_ia32_perf_global_ctrl);
11957 /* Set L1 segment info according to Intel SDM
11958 27.5.2 Loading Host Segment and Descriptor-Table Registers */
11959 seg = (struct kvm_segment) {
11961 .limit = 0xFFFFFFFF,
11962 .selector = vmcs12->host_cs_selector,
11968 if (vmcs12->vm_exit_controls & VM_EXIT_HOST_ADDR_SPACE_SIZE)
11972 vmx_set_segment(vcpu, &seg, VCPU_SREG_CS);
11973 seg = (struct kvm_segment) {
11975 .limit = 0xFFFFFFFF,
11982 seg.selector = vmcs12->host_ds_selector;
11983 vmx_set_segment(vcpu, &seg, VCPU_SREG_DS);
11984 seg.selector = vmcs12->host_es_selector;
11985 vmx_set_segment(vcpu, &seg, VCPU_SREG_ES);
11986 seg.selector = vmcs12->host_ss_selector;
11987 vmx_set_segment(vcpu, &seg, VCPU_SREG_SS);
11988 seg.selector = vmcs12->host_fs_selector;
11989 seg.base = vmcs12->host_fs_base;
11990 vmx_set_segment(vcpu, &seg, VCPU_SREG_FS);
11991 seg.selector = vmcs12->host_gs_selector;
11992 seg.base = vmcs12->host_gs_base;
11993 vmx_set_segment(vcpu, &seg, VCPU_SREG_GS);
11994 seg = (struct kvm_segment) {
11995 .base = vmcs12->host_tr_base,
11997 .selector = vmcs12->host_tr_selector,
12001 vmx_set_segment(vcpu, &seg, VCPU_SREG_TR);
12003 kvm_set_dr(vcpu, 7, 0x400);
12004 vmcs_write64(GUEST_IA32_DEBUGCTL, 0);
12006 if (cpu_has_vmx_msr_bitmap())
12007 vmx_update_msr_bitmap(vcpu);
12009 if (nested_vmx_load_msr(vcpu, vmcs12->vm_exit_msr_load_addr,
12010 vmcs12->vm_exit_msr_load_count))
12011 nested_vmx_abort(vcpu, VMX_ABORT_LOAD_HOST_MSR_FAIL);
12015 * Emulate an exit from nested guest (L2) to L1, i.e., prepare to run L1
12016 * and modify vmcs12 to make it see what it would expect to see there if
12017 * L2 was its real guest. Must only be called when in L2 (is_guest_mode())
12019 static void nested_vmx_vmexit(struct kvm_vcpu *vcpu, u32 exit_reason,
12020 u32 exit_intr_info,
12021 unsigned long exit_qualification)
12023 struct vcpu_vmx *vmx = to_vmx(vcpu);
12024 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
12026 /* trying to cancel vmlaunch/vmresume is a bug */
12027 WARN_ON_ONCE(vmx->nested.nested_run_pending);
12030 * The only expected VM-instruction error is "VM entry with
12031 * invalid control field(s)." Anything else indicates a
12034 WARN_ON_ONCE(vmx->fail && (vmcs_read32(VM_INSTRUCTION_ERROR) !=
12035 VMXERR_ENTRY_INVALID_CONTROL_FIELD));
12037 leave_guest_mode(vcpu);
12039 if (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING)
12040 vcpu->arch.tsc_offset -= vmcs12->tsc_offset;
12042 if (likely(!vmx->fail)) {
12043 if (exit_reason == -1)
12044 sync_vmcs12(vcpu, vmcs12);
12046 prepare_vmcs12(vcpu, vmcs12, exit_reason, exit_intr_info,
12047 exit_qualification);
12049 if (nested_vmx_store_msr(vcpu, vmcs12->vm_exit_msr_store_addr,
12050 vmcs12->vm_exit_msr_store_count))
12051 nested_vmx_abort(vcpu, VMX_ABORT_SAVE_GUEST_MSR_FAIL);
12054 vmx_switch_vmcs(vcpu, &vmx->vmcs01);
12055 vm_entry_controls_reset_shadow(vmx);
12056 vm_exit_controls_reset_shadow(vmx);
12057 vmx_segment_cache_clear(vmx);
12059 /* Update any VMCS fields that might have changed while L2 ran */
12060 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, vmx->msr_autoload.nr);
12061 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, vmx->msr_autoload.nr);
12062 vmcs_write64(TSC_OFFSET, vcpu->arch.tsc_offset);
12063 if (vmx->hv_deadline_tsc == -1)
12064 vmcs_clear_bits(PIN_BASED_VM_EXEC_CONTROL,
12065 PIN_BASED_VMX_PREEMPTION_TIMER);
12067 vmcs_set_bits(PIN_BASED_VM_EXEC_CONTROL,
12068 PIN_BASED_VMX_PREEMPTION_TIMER);
12069 if (kvm_has_tsc_control)
12070 decache_tsc_multiplier(vmx);
12072 if (vmx->nested.change_vmcs01_virtual_x2apic_mode) {
12073 vmx->nested.change_vmcs01_virtual_x2apic_mode = false;
12074 vmx_set_virtual_x2apic_mode(vcpu,
12075 vcpu->arch.apic_base & X2APIC_ENABLE);
12076 } else if (!nested_cpu_has_ept(vmcs12) &&
12077 nested_cpu_has2(vmcs12,
12078 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) {
12079 vmx_flush_tlb(vcpu, true);
12082 /* This is needed for same reason as it was needed in prepare_vmcs02 */
12085 /* Unpin physical memory we referred to in vmcs02 */
12086 if (vmx->nested.apic_access_page) {
12087 kvm_release_page_dirty(vmx->nested.apic_access_page);
12088 vmx->nested.apic_access_page = NULL;
12090 if (vmx->nested.virtual_apic_page) {
12091 kvm_release_page_dirty(vmx->nested.virtual_apic_page);
12092 vmx->nested.virtual_apic_page = NULL;
12094 if (vmx->nested.pi_desc_page) {
12095 kunmap(vmx->nested.pi_desc_page);
12096 kvm_release_page_dirty(vmx->nested.pi_desc_page);
12097 vmx->nested.pi_desc_page = NULL;
12098 vmx->nested.pi_desc = NULL;
12102 * We are now running in L2, mmu_notifier will force to reload the
12103 * page's hpa for L2 vmcs. Need to reload it for L1 before entering L1.
12105 kvm_make_request(KVM_REQ_APIC_PAGE_RELOAD, vcpu);
12107 if (enable_shadow_vmcs && exit_reason != -1)
12108 vmx->nested.sync_shadow_vmcs = true;
12110 /* in case we halted in L2 */
12111 vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
12113 if (likely(!vmx->fail)) {
12115 * TODO: SDM says that with acknowledge interrupt on
12116 * exit, bit 31 of the VM-exit interrupt information
12117 * (valid interrupt) is always set to 1 on
12118 * EXIT_REASON_EXTERNAL_INTERRUPT, so we shouldn't
12119 * need kvm_cpu_has_interrupt(). See the commit
12120 * message for details.
12122 if (nested_exit_intr_ack_set(vcpu) &&
12123 exit_reason == EXIT_REASON_EXTERNAL_INTERRUPT &&
12124 kvm_cpu_has_interrupt(vcpu)) {
12125 int irq = kvm_cpu_get_interrupt(vcpu);
12127 vmcs12->vm_exit_intr_info = irq |
12128 INTR_INFO_VALID_MASK | INTR_TYPE_EXT_INTR;
12131 if (exit_reason != -1)
12132 trace_kvm_nested_vmexit_inject(vmcs12->vm_exit_reason,
12133 vmcs12->exit_qualification,
12134 vmcs12->idt_vectoring_info_field,
12135 vmcs12->vm_exit_intr_info,
12136 vmcs12->vm_exit_intr_error_code,
12139 load_vmcs12_host_state(vcpu, vmcs12);
12145 * After an early L2 VM-entry failure, we're now back
12146 * in L1 which thinks it just finished a VMLAUNCH or
12147 * VMRESUME instruction, so we need to set the failure
12148 * flag and the VM-instruction error field of the VMCS
12151 nested_vmx_failValid(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD);
12153 load_vmcs12_mmu_host_state(vcpu, vmcs12);
12156 * The emulated instruction was already skipped in
12157 * nested_vmx_run, but the updated RIP was never
12158 * written back to the vmcs01.
12160 skip_emulated_instruction(vcpu);
12165 * Forcibly leave nested mode in order to be able to reset the VCPU later on.
12167 static void vmx_leave_nested(struct kvm_vcpu *vcpu)
12169 if (is_guest_mode(vcpu)) {
12170 to_vmx(vcpu)->nested.nested_run_pending = 0;
12171 nested_vmx_vmexit(vcpu, -1, 0, 0);
12173 free_nested(to_vmx(vcpu));
12177 * L1's failure to enter L2 is a subset of a normal exit, as explained in
12178 * 23.7 "VM-entry failures during or after loading guest state" (this also
12179 * lists the acceptable exit-reason and exit-qualification parameters).
12180 * It should only be called before L2 actually succeeded to run, and when
12181 * vmcs01 is current (it doesn't leave_guest_mode() or switch vmcss).
12183 static void nested_vmx_entry_failure(struct kvm_vcpu *vcpu,
12184 struct vmcs12 *vmcs12,
12185 u32 reason, unsigned long qualification)
12187 load_vmcs12_host_state(vcpu, vmcs12);
12188 vmcs12->vm_exit_reason = reason | VMX_EXIT_REASONS_FAILED_VMENTRY;
12189 vmcs12->exit_qualification = qualification;
12190 nested_vmx_succeed(vcpu);
12191 if (enable_shadow_vmcs)
12192 to_vmx(vcpu)->nested.sync_shadow_vmcs = true;
12195 static int vmx_check_intercept(struct kvm_vcpu *vcpu,
12196 struct x86_instruction_info *info,
12197 enum x86_intercept_stage stage)
12199 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
12200 struct x86_emulate_ctxt *ctxt = &vcpu->arch.emulate_ctxt;
12203 * RDPID causes #UD if disabled through secondary execution controls.
12204 * Because it is marked as EmulateOnUD, we need to intercept it here.
12206 if (info->intercept == x86_intercept_rdtscp &&
12207 !nested_cpu_has2(vmcs12, SECONDARY_EXEC_RDTSCP)) {
12208 ctxt->exception.vector = UD_VECTOR;
12209 ctxt->exception.error_code_valid = false;
12210 return X86EMUL_PROPAGATE_FAULT;
12213 /* TODO: check more intercepts... */
12214 return X86EMUL_CONTINUE;
12217 #ifdef CONFIG_X86_64
12218 /* (a << shift) / divisor, return 1 if overflow otherwise 0 */
12219 static inline int u64_shl_div_u64(u64 a, unsigned int shift,
12220 u64 divisor, u64 *result)
12222 u64 low = a << shift, high = a >> (64 - shift);
12224 /* To avoid the overflow on divq */
12225 if (high >= divisor)
12228 /* Low hold the result, high hold rem which is discarded */
12229 asm("divq %2\n\t" : "=a" (low), "=d" (high) :
12230 "rm" (divisor), "0" (low), "1" (high));
12236 static int vmx_set_hv_timer(struct kvm_vcpu *vcpu, u64 guest_deadline_tsc)
12238 struct vcpu_vmx *vmx;
12239 u64 tscl, guest_tscl, delta_tsc;
12241 if (kvm_mwait_in_guest(vcpu->kvm))
12242 return -EOPNOTSUPP;
12244 vmx = to_vmx(vcpu);
12246 guest_tscl = kvm_read_l1_tsc(vcpu, tscl);
12247 delta_tsc = max(guest_deadline_tsc, guest_tscl) - guest_tscl;
12249 /* Convert to host delta tsc if tsc scaling is enabled */
12250 if (vcpu->arch.tsc_scaling_ratio != kvm_default_tsc_scaling_ratio &&
12251 u64_shl_div_u64(delta_tsc,
12252 kvm_tsc_scaling_ratio_frac_bits,
12253 vcpu->arch.tsc_scaling_ratio,
12258 * If the delta tsc can't fit in the 32 bit after the multi shift,
12259 * we can't use the preemption timer.
12260 * It's possible that it fits on later vmentries, but checking
12261 * on every vmentry is costly so we just use an hrtimer.
12263 if (delta_tsc >> (cpu_preemption_timer_multi + 32))
12266 vmx->hv_deadline_tsc = tscl + delta_tsc;
12267 vmcs_set_bits(PIN_BASED_VM_EXEC_CONTROL,
12268 PIN_BASED_VMX_PREEMPTION_TIMER);
12270 return delta_tsc == 0;
12273 static void vmx_cancel_hv_timer(struct kvm_vcpu *vcpu)
12275 struct vcpu_vmx *vmx = to_vmx(vcpu);
12276 vmx->hv_deadline_tsc = -1;
12277 vmcs_clear_bits(PIN_BASED_VM_EXEC_CONTROL,
12278 PIN_BASED_VMX_PREEMPTION_TIMER);
12282 static void vmx_sched_in(struct kvm_vcpu *vcpu, int cpu)
12284 if (!kvm_pause_in_guest(vcpu->kvm))
12285 shrink_ple_window(vcpu);
12288 static void vmx_slot_enable_log_dirty(struct kvm *kvm,
12289 struct kvm_memory_slot *slot)
12291 kvm_mmu_slot_leaf_clear_dirty(kvm, slot);
12292 kvm_mmu_slot_largepage_remove_write_access(kvm, slot);
12295 static void vmx_slot_disable_log_dirty(struct kvm *kvm,
12296 struct kvm_memory_slot *slot)
12298 kvm_mmu_slot_set_dirty(kvm, slot);
12301 static void vmx_flush_log_dirty(struct kvm *kvm)
12303 kvm_flush_pml_buffers(kvm);
12306 static int vmx_write_pml_buffer(struct kvm_vcpu *vcpu)
12308 struct vmcs12 *vmcs12;
12309 struct vcpu_vmx *vmx = to_vmx(vcpu);
12311 struct page *page = NULL;
12314 if (is_guest_mode(vcpu)) {
12315 WARN_ON_ONCE(vmx->nested.pml_full);
12318 * Check if PML is enabled for the nested guest.
12319 * Whether eptp bit 6 is set is already checked
12320 * as part of A/D emulation.
12322 vmcs12 = get_vmcs12(vcpu);
12323 if (!nested_cpu_has_pml(vmcs12))
12326 if (vmcs12->guest_pml_index >= PML_ENTITY_NUM) {
12327 vmx->nested.pml_full = true;
12331 gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS) & ~0xFFFull;
12333 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->pml_address);
12334 if (is_error_page(page))
12337 pml_address = kmap(page);
12338 pml_address[vmcs12->guest_pml_index--] = gpa;
12340 kvm_release_page_clean(page);
12346 static void vmx_enable_log_dirty_pt_masked(struct kvm *kvm,
12347 struct kvm_memory_slot *memslot,
12348 gfn_t offset, unsigned long mask)
12350 kvm_mmu_clear_dirty_pt_masked(kvm, memslot, offset, mask);
12353 static void __pi_post_block(struct kvm_vcpu *vcpu)
12355 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
12356 struct pi_desc old, new;
12360 old.control = new.control = pi_desc->control;
12361 WARN(old.nv != POSTED_INTR_WAKEUP_VECTOR,
12362 "Wakeup handler not enabled while the VCPU is blocked\n");
12364 dest = cpu_physical_id(vcpu->cpu);
12366 if (x2apic_enabled())
12369 new.ndst = (dest << 8) & 0xFF00;
12371 /* set 'NV' to 'notification vector' */
12372 new.nv = POSTED_INTR_VECTOR;
12373 } while (cmpxchg64(&pi_desc->control, old.control,
12374 new.control) != old.control);
12376 if (!WARN_ON_ONCE(vcpu->pre_pcpu == -1)) {
12377 spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
12378 list_del(&vcpu->blocked_vcpu_list);
12379 spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
12380 vcpu->pre_pcpu = -1;
12385 * This routine does the following things for vCPU which is going
12386 * to be blocked if VT-d PI is enabled.
12387 * - Store the vCPU to the wakeup list, so when interrupts happen
12388 * we can find the right vCPU to wake up.
12389 * - Change the Posted-interrupt descriptor as below:
12390 * 'NDST' <-- vcpu->pre_pcpu
12391 * 'NV' <-- POSTED_INTR_WAKEUP_VECTOR
12392 * - If 'ON' is set during this process, which means at least one
12393 * interrupt is posted for this vCPU, we cannot block it, in
12394 * this case, return 1, otherwise, return 0.
12397 static int pi_pre_block(struct kvm_vcpu *vcpu)
12400 struct pi_desc old, new;
12401 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
12403 if (!kvm_arch_has_assigned_device(vcpu->kvm) ||
12404 !irq_remapping_cap(IRQ_POSTING_CAP) ||
12405 !kvm_vcpu_apicv_active(vcpu))
12408 WARN_ON(irqs_disabled());
12409 local_irq_disable();
12410 if (!WARN_ON_ONCE(vcpu->pre_pcpu != -1)) {
12411 vcpu->pre_pcpu = vcpu->cpu;
12412 spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
12413 list_add_tail(&vcpu->blocked_vcpu_list,
12414 &per_cpu(blocked_vcpu_on_cpu,
12416 spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
12420 old.control = new.control = pi_desc->control;
12422 WARN((pi_desc->sn == 1),
12423 "Warning: SN field of posted-interrupts "
12424 "is set before blocking\n");
12427 * Since vCPU can be preempted during this process,
12428 * vcpu->cpu could be different with pre_pcpu, we
12429 * need to set pre_pcpu as the destination of wakeup
12430 * notification event, then we can find the right vCPU
12431 * to wakeup in wakeup handler if interrupts happen
12432 * when the vCPU is in blocked state.
12434 dest = cpu_physical_id(vcpu->pre_pcpu);
12436 if (x2apic_enabled())
12439 new.ndst = (dest << 8) & 0xFF00;
12441 /* set 'NV' to 'wakeup vector' */
12442 new.nv = POSTED_INTR_WAKEUP_VECTOR;
12443 } while (cmpxchg64(&pi_desc->control, old.control,
12444 new.control) != old.control);
12446 /* We should not block the vCPU if an interrupt is posted for it. */
12447 if (pi_test_on(pi_desc) == 1)
12448 __pi_post_block(vcpu);
12450 local_irq_enable();
12451 return (vcpu->pre_pcpu == -1);
12454 static int vmx_pre_block(struct kvm_vcpu *vcpu)
12456 if (pi_pre_block(vcpu))
12459 if (kvm_lapic_hv_timer_in_use(vcpu))
12460 kvm_lapic_switch_to_sw_timer(vcpu);
12465 static void pi_post_block(struct kvm_vcpu *vcpu)
12467 if (vcpu->pre_pcpu == -1)
12470 WARN_ON(irqs_disabled());
12471 local_irq_disable();
12472 __pi_post_block(vcpu);
12473 local_irq_enable();
12476 static void vmx_post_block(struct kvm_vcpu *vcpu)
12478 if (kvm_x86_ops->set_hv_timer)
12479 kvm_lapic_switch_to_hv_timer(vcpu);
12481 pi_post_block(vcpu);
12485 * vmx_update_pi_irte - set IRTE for Posted-Interrupts
12488 * @host_irq: host irq of the interrupt
12489 * @guest_irq: gsi of the interrupt
12490 * @set: set or unset PI
12491 * returns 0 on success, < 0 on failure
12493 static int vmx_update_pi_irte(struct kvm *kvm, unsigned int host_irq,
12494 uint32_t guest_irq, bool set)
12496 struct kvm_kernel_irq_routing_entry *e;
12497 struct kvm_irq_routing_table *irq_rt;
12498 struct kvm_lapic_irq irq;
12499 struct kvm_vcpu *vcpu;
12500 struct vcpu_data vcpu_info;
12503 if (!kvm_arch_has_assigned_device(kvm) ||
12504 !irq_remapping_cap(IRQ_POSTING_CAP) ||
12505 !kvm_vcpu_apicv_active(kvm->vcpus[0]))
12508 idx = srcu_read_lock(&kvm->irq_srcu);
12509 irq_rt = srcu_dereference(kvm->irq_routing, &kvm->irq_srcu);
12510 if (guest_irq >= irq_rt->nr_rt_entries ||
12511 hlist_empty(&irq_rt->map[guest_irq])) {
12512 pr_warn_once("no route for guest_irq %u/%u (broken user space?)\n",
12513 guest_irq, irq_rt->nr_rt_entries);
12517 hlist_for_each_entry(e, &irq_rt->map[guest_irq], link) {
12518 if (e->type != KVM_IRQ_ROUTING_MSI)
12521 * VT-d PI cannot support posting multicast/broadcast
12522 * interrupts to a vCPU, we still use interrupt remapping
12523 * for these kind of interrupts.
12525 * For lowest-priority interrupts, we only support
12526 * those with single CPU as the destination, e.g. user
12527 * configures the interrupts via /proc/irq or uses
12528 * irqbalance to make the interrupts single-CPU.
12530 * We will support full lowest-priority interrupt later.
12533 kvm_set_msi_irq(kvm, e, &irq);
12534 if (!kvm_intr_is_single_vcpu(kvm, &irq, &vcpu)) {
12536 * Make sure the IRTE is in remapped mode if
12537 * we don't handle it in posted mode.
12539 ret = irq_set_vcpu_affinity(host_irq, NULL);
12542 "failed to back to remapped mode, irq: %u\n",
12550 vcpu_info.pi_desc_addr = __pa(vcpu_to_pi_desc(vcpu));
12551 vcpu_info.vector = irq.vector;
12553 trace_kvm_pi_irte_update(host_irq, vcpu->vcpu_id, e->gsi,
12554 vcpu_info.vector, vcpu_info.pi_desc_addr, set);
12557 ret = irq_set_vcpu_affinity(host_irq, &vcpu_info);
12559 ret = irq_set_vcpu_affinity(host_irq, NULL);
12562 printk(KERN_INFO "%s: failed to update PI IRTE\n",
12570 srcu_read_unlock(&kvm->irq_srcu, idx);
12574 static void vmx_setup_mce(struct kvm_vcpu *vcpu)
12576 if (vcpu->arch.mcg_cap & MCG_LMCE_P)
12577 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits |=
12578 FEATURE_CONTROL_LMCE;
12580 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits &=
12581 ~FEATURE_CONTROL_LMCE;
12584 static int vmx_smi_allowed(struct kvm_vcpu *vcpu)
12586 /* we need a nested vmexit to enter SMM, postpone if run is pending */
12587 if (to_vmx(vcpu)->nested.nested_run_pending)
12592 static int vmx_pre_enter_smm(struct kvm_vcpu *vcpu, char *smstate)
12594 struct vcpu_vmx *vmx = to_vmx(vcpu);
12596 vmx->nested.smm.guest_mode = is_guest_mode(vcpu);
12597 if (vmx->nested.smm.guest_mode)
12598 nested_vmx_vmexit(vcpu, -1, 0, 0);
12600 vmx->nested.smm.vmxon = vmx->nested.vmxon;
12601 vmx->nested.vmxon = false;
12602 vmx_clear_hlt(vcpu);
12606 static int vmx_pre_leave_smm(struct kvm_vcpu *vcpu, u64 smbase)
12608 struct vcpu_vmx *vmx = to_vmx(vcpu);
12611 if (vmx->nested.smm.vmxon) {
12612 vmx->nested.vmxon = true;
12613 vmx->nested.smm.vmxon = false;
12616 if (vmx->nested.smm.guest_mode) {
12617 vcpu->arch.hflags &= ~HF_SMM_MASK;
12618 ret = enter_vmx_non_root_mode(vcpu, false);
12619 vcpu->arch.hflags |= HF_SMM_MASK;
12623 vmx->nested.smm.guest_mode = false;
12628 static int enable_smi_window(struct kvm_vcpu *vcpu)
12633 static struct kvm_x86_ops vmx_x86_ops __ro_after_init = {
12634 .cpu_has_kvm_support = cpu_has_kvm_support,
12635 .disabled_by_bios = vmx_disabled_by_bios,
12636 .hardware_setup = hardware_setup,
12637 .hardware_unsetup = hardware_unsetup,
12638 .check_processor_compatibility = vmx_check_processor_compat,
12639 .hardware_enable = hardware_enable,
12640 .hardware_disable = hardware_disable,
12641 .cpu_has_accelerated_tpr = report_flexpriority,
12642 .has_emulated_msr = vmx_has_emulated_msr,
12644 .vm_init = vmx_vm_init,
12645 .vm_alloc = vmx_vm_alloc,
12646 .vm_free = vmx_vm_free,
12648 .vcpu_create = vmx_create_vcpu,
12649 .vcpu_free = vmx_free_vcpu,
12650 .vcpu_reset = vmx_vcpu_reset,
12652 .prepare_guest_switch = vmx_save_host_state,
12653 .vcpu_load = vmx_vcpu_load,
12654 .vcpu_put = vmx_vcpu_put,
12656 .update_bp_intercept = update_exception_bitmap,
12657 .get_msr_feature = vmx_get_msr_feature,
12658 .get_msr = vmx_get_msr,
12659 .set_msr = vmx_set_msr,
12660 .get_segment_base = vmx_get_segment_base,
12661 .get_segment = vmx_get_segment,
12662 .set_segment = vmx_set_segment,
12663 .get_cpl = vmx_get_cpl,
12664 .get_cs_db_l_bits = vmx_get_cs_db_l_bits,
12665 .decache_cr0_guest_bits = vmx_decache_cr0_guest_bits,
12666 .decache_cr3 = vmx_decache_cr3,
12667 .decache_cr4_guest_bits = vmx_decache_cr4_guest_bits,
12668 .set_cr0 = vmx_set_cr0,
12669 .set_cr3 = vmx_set_cr3,
12670 .set_cr4 = vmx_set_cr4,
12671 .set_efer = vmx_set_efer,
12672 .get_idt = vmx_get_idt,
12673 .set_idt = vmx_set_idt,
12674 .get_gdt = vmx_get_gdt,
12675 .set_gdt = vmx_set_gdt,
12676 .get_dr6 = vmx_get_dr6,
12677 .set_dr6 = vmx_set_dr6,
12678 .set_dr7 = vmx_set_dr7,
12679 .sync_dirty_debug_regs = vmx_sync_dirty_debug_regs,
12680 .cache_reg = vmx_cache_reg,
12681 .get_rflags = vmx_get_rflags,
12682 .set_rflags = vmx_set_rflags,
12684 .tlb_flush = vmx_flush_tlb,
12686 .run = vmx_vcpu_run,
12687 .handle_exit = vmx_handle_exit,
12688 .skip_emulated_instruction = skip_emulated_instruction,
12689 .set_interrupt_shadow = vmx_set_interrupt_shadow,
12690 .get_interrupt_shadow = vmx_get_interrupt_shadow,
12691 .patch_hypercall = vmx_patch_hypercall,
12692 .set_irq = vmx_inject_irq,
12693 .set_nmi = vmx_inject_nmi,
12694 .queue_exception = vmx_queue_exception,
12695 .cancel_injection = vmx_cancel_injection,
12696 .interrupt_allowed = vmx_interrupt_allowed,
12697 .nmi_allowed = vmx_nmi_allowed,
12698 .get_nmi_mask = vmx_get_nmi_mask,
12699 .set_nmi_mask = vmx_set_nmi_mask,
12700 .enable_nmi_window = enable_nmi_window,
12701 .enable_irq_window = enable_irq_window,
12702 .update_cr8_intercept = update_cr8_intercept,
12703 .set_virtual_x2apic_mode = vmx_set_virtual_x2apic_mode,
12704 .set_apic_access_page_addr = vmx_set_apic_access_page_addr,
12705 .get_enable_apicv = vmx_get_enable_apicv,
12706 .refresh_apicv_exec_ctrl = vmx_refresh_apicv_exec_ctrl,
12707 .load_eoi_exitmap = vmx_load_eoi_exitmap,
12708 .apicv_post_state_restore = vmx_apicv_post_state_restore,
12709 .hwapic_irr_update = vmx_hwapic_irr_update,
12710 .hwapic_isr_update = vmx_hwapic_isr_update,
12711 .sync_pir_to_irr = vmx_sync_pir_to_irr,
12712 .deliver_posted_interrupt = vmx_deliver_posted_interrupt,
12714 .set_tss_addr = vmx_set_tss_addr,
12715 .set_identity_map_addr = vmx_set_identity_map_addr,
12716 .get_tdp_level = get_ept_level,
12717 .get_mt_mask = vmx_get_mt_mask,
12719 .get_exit_info = vmx_get_exit_info,
12721 .get_lpage_level = vmx_get_lpage_level,
12723 .cpuid_update = vmx_cpuid_update,
12725 .rdtscp_supported = vmx_rdtscp_supported,
12726 .invpcid_supported = vmx_invpcid_supported,
12728 .set_supported_cpuid = vmx_set_supported_cpuid,
12730 .has_wbinvd_exit = cpu_has_vmx_wbinvd_exit,
12732 .read_l1_tsc_offset = vmx_read_l1_tsc_offset,
12733 .write_tsc_offset = vmx_write_tsc_offset,
12735 .set_tdp_cr3 = vmx_set_cr3,
12737 .check_intercept = vmx_check_intercept,
12738 .handle_external_intr = vmx_handle_external_intr,
12739 .mpx_supported = vmx_mpx_supported,
12740 .xsaves_supported = vmx_xsaves_supported,
12741 .umip_emulated = vmx_umip_emulated,
12743 .check_nested_events = vmx_check_nested_events,
12745 .sched_in = vmx_sched_in,
12747 .slot_enable_log_dirty = vmx_slot_enable_log_dirty,
12748 .slot_disable_log_dirty = vmx_slot_disable_log_dirty,
12749 .flush_log_dirty = vmx_flush_log_dirty,
12750 .enable_log_dirty_pt_masked = vmx_enable_log_dirty_pt_masked,
12751 .write_log_dirty = vmx_write_pml_buffer,
12753 .pre_block = vmx_pre_block,
12754 .post_block = vmx_post_block,
12756 .pmu_ops = &intel_pmu_ops,
12758 .update_pi_irte = vmx_update_pi_irte,
12760 #ifdef CONFIG_X86_64
12761 .set_hv_timer = vmx_set_hv_timer,
12762 .cancel_hv_timer = vmx_cancel_hv_timer,
12765 .setup_mce = vmx_setup_mce,
12767 .smi_allowed = vmx_smi_allowed,
12768 .pre_enter_smm = vmx_pre_enter_smm,
12769 .pre_leave_smm = vmx_pre_leave_smm,
12770 .enable_smi_window = enable_smi_window,
12773 static int __init vmx_init(void)
12777 #if IS_ENABLED(CONFIG_HYPERV)
12779 * Enlightened VMCS usage should be recommended and the host needs
12780 * to support eVMCS v1 or above. We can also disable eVMCS support
12781 * with module parameter.
12783 if (enlightened_vmcs &&
12784 ms_hyperv.hints & HV_X64_ENLIGHTENED_VMCS_RECOMMENDED &&
12785 (ms_hyperv.nested_features & HV_X64_ENLIGHTENED_VMCS_VERSION) >=
12786 KVM_EVMCS_VERSION) {
12789 /* Check that we have assist pages on all online CPUs */
12790 for_each_online_cpu(cpu) {
12791 if (!hv_get_vp_assist_page(cpu)) {
12792 enlightened_vmcs = false;
12797 if (enlightened_vmcs) {
12798 pr_info("KVM: vmx: using Hyper-V Enlightened VMCS\n");
12799 static_branch_enable(&enable_evmcs);
12802 enlightened_vmcs = false;
12806 r = kvm_init(&vmx_x86_ops, sizeof(struct vcpu_vmx),
12807 __alignof__(struct vcpu_vmx), THIS_MODULE);
12811 #ifdef CONFIG_KEXEC_CORE
12812 rcu_assign_pointer(crash_vmclear_loaded_vmcss,
12813 crash_vmclear_local_loaded_vmcss);
12819 static void __exit vmx_exit(void)
12821 #ifdef CONFIG_KEXEC_CORE
12822 RCU_INIT_POINTER(crash_vmclear_loaded_vmcss, NULL);
12828 #if IS_ENABLED(CONFIG_HYPERV)
12829 if (static_branch_unlikely(&enable_evmcs)) {
12831 struct hv_vp_assist_page *vp_ap;
12833 * Reset everything to support using non-enlightened VMCS
12834 * access later (e.g. when we reload the module with
12835 * enlightened_vmcs=0)
12837 for_each_online_cpu(cpu) {
12838 vp_ap = hv_get_vp_assist_page(cpu);
12843 vp_ap->current_nested_vmcs = 0;
12844 vp_ap->enlighten_vmentry = 0;
12847 static_branch_disable(&enable_evmcs);
12852 module_init(vmx_init)
12853 module_exit(vmx_exit)