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/nospec-branch.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 inline bool report_flexpriority(void)
1499 return flexpriority_enabled;
1502 static inline unsigned nested_cpu_vmx_misc_cr3_count(struct kvm_vcpu *vcpu)
1504 return vmx_misc_cr3_count(to_vmx(vcpu)->nested.msrs.misc_low);
1507 static inline bool nested_cpu_has(struct vmcs12 *vmcs12, u32 bit)
1509 return vmcs12->cpu_based_vm_exec_control & bit;
1512 static inline bool nested_cpu_has2(struct vmcs12 *vmcs12, u32 bit)
1514 return (vmcs12->cpu_based_vm_exec_control &
1515 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) &&
1516 (vmcs12->secondary_vm_exec_control & bit);
1519 static inline bool nested_cpu_has_preemption_timer(struct vmcs12 *vmcs12)
1521 return vmcs12->pin_based_vm_exec_control &
1522 PIN_BASED_VMX_PREEMPTION_TIMER;
1525 static inline bool nested_cpu_has_nmi_exiting(struct vmcs12 *vmcs12)
1527 return vmcs12->pin_based_vm_exec_control & PIN_BASED_NMI_EXITING;
1530 static inline bool nested_cpu_has_virtual_nmis(struct vmcs12 *vmcs12)
1532 return vmcs12->pin_based_vm_exec_control & PIN_BASED_VIRTUAL_NMIS;
1535 static inline int nested_cpu_has_ept(struct vmcs12 *vmcs12)
1537 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_EPT);
1540 static inline bool nested_cpu_has_xsaves(struct vmcs12 *vmcs12)
1542 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_XSAVES);
1545 static inline bool nested_cpu_has_pml(struct vmcs12 *vmcs12)
1547 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_PML);
1550 static inline bool nested_cpu_has_virt_x2apic_mode(struct vmcs12 *vmcs12)
1552 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE);
1555 static inline bool nested_cpu_has_vpid(struct vmcs12 *vmcs12)
1557 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_VPID);
1560 static inline bool nested_cpu_has_apic_reg_virt(struct vmcs12 *vmcs12)
1562 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_APIC_REGISTER_VIRT);
1565 static inline bool nested_cpu_has_vid(struct vmcs12 *vmcs12)
1567 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
1570 static inline bool nested_cpu_has_posted_intr(struct vmcs12 *vmcs12)
1572 return vmcs12->pin_based_vm_exec_control & PIN_BASED_POSTED_INTR;
1575 static inline bool nested_cpu_has_vmfunc(struct vmcs12 *vmcs12)
1577 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_VMFUNC);
1580 static inline bool nested_cpu_has_eptp_switching(struct vmcs12 *vmcs12)
1582 return nested_cpu_has_vmfunc(vmcs12) &&
1583 (vmcs12->vm_function_control &
1584 VMX_VMFUNC_EPTP_SWITCHING);
1587 static inline bool is_nmi(u32 intr_info)
1589 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK))
1590 == (INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK);
1593 static void nested_vmx_vmexit(struct kvm_vcpu *vcpu, u32 exit_reason,
1595 unsigned long exit_qualification);
1596 static void nested_vmx_entry_failure(struct kvm_vcpu *vcpu,
1597 struct vmcs12 *vmcs12,
1598 u32 reason, unsigned long qualification);
1600 static int __find_msr_index(struct vcpu_vmx *vmx, u32 msr)
1604 for (i = 0; i < vmx->nmsrs; ++i)
1605 if (vmx_msr_index[vmx->guest_msrs[i].index] == msr)
1610 static inline void __invvpid(int ext, u16 vpid, gva_t gva)
1616 } operand = { vpid, 0, gva };
1618 asm volatile (__ex(ASM_VMX_INVVPID)
1619 /* CF==1 or ZF==1 --> rc = -1 */
1620 "; ja 1f ; ud2 ; 1:"
1621 : : "a"(&operand), "c"(ext) : "cc", "memory");
1624 static inline void __invept(int ext, u64 eptp, gpa_t gpa)
1628 } operand = {eptp, gpa};
1630 asm volatile (__ex(ASM_VMX_INVEPT)
1631 /* CF==1 or ZF==1 --> rc = -1 */
1632 "; ja 1f ; ud2 ; 1:\n"
1633 : : "a" (&operand), "c" (ext) : "cc", "memory");
1636 static struct shared_msr_entry *find_msr_entry(struct vcpu_vmx *vmx, u32 msr)
1640 i = __find_msr_index(vmx, msr);
1642 return &vmx->guest_msrs[i];
1646 static void vmcs_clear(struct vmcs *vmcs)
1648 u64 phys_addr = __pa(vmcs);
1651 asm volatile (__ex(ASM_VMX_VMCLEAR_RAX) "; setna %0"
1652 : "=qm"(error) : "a"(&phys_addr), "m"(phys_addr)
1655 printk(KERN_ERR "kvm: vmclear fail: %p/%llx\n",
1659 static inline void loaded_vmcs_init(struct loaded_vmcs *loaded_vmcs)
1661 vmcs_clear(loaded_vmcs->vmcs);
1662 if (loaded_vmcs->shadow_vmcs && loaded_vmcs->launched)
1663 vmcs_clear(loaded_vmcs->shadow_vmcs);
1664 loaded_vmcs->cpu = -1;
1665 loaded_vmcs->launched = 0;
1668 static void vmcs_load(struct vmcs *vmcs)
1670 u64 phys_addr = __pa(vmcs);
1673 if (static_branch_unlikely(&enable_evmcs))
1674 return evmcs_load(phys_addr);
1676 asm volatile (__ex(ASM_VMX_VMPTRLD_RAX) "; setna %0"
1677 : "=qm"(error) : "a"(&phys_addr), "m"(phys_addr)
1680 printk(KERN_ERR "kvm: vmptrld %p/%llx failed\n",
1684 #ifdef CONFIG_KEXEC_CORE
1686 * This bitmap is used to indicate whether the vmclear
1687 * operation is enabled on all cpus. All disabled by
1690 static cpumask_t crash_vmclear_enabled_bitmap = CPU_MASK_NONE;
1692 static inline void crash_enable_local_vmclear(int cpu)
1694 cpumask_set_cpu(cpu, &crash_vmclear_enabled_bitmap);
1697 static inline void crash_disable_local_vmclear(int cpu)
1699 cpumask_clear_cpu(cpu, &crash_vmclear_enabled_bitmap);
1702 static inline int crash_local_vmclear_enabled(int cpu)
1704 return cpumask_test_cpu(cpu, &crash_vmclear_enabled_bitmap);
1707 static void crash_vmclear_local_loaded_vmcss(void)
1709 int cpu = raw_smp_processor_id();
1710 struct loaded_vmcs *v;
1712 if (!crash_local_vmclear_enabled(cpu))
1715 list_for_each_entry(v, &per_cpu(loaded_vmcss_on_cpu, cpu),
1716 loaded_vmcss_on_cpu_link)
1717 vmcs_clear(v->vmcs);
1720 static inline void crash_enable_local_vmclear(int cpu) { }
1721 static inline void crash_disable_local_vmclear(int cpu) { }
1722 #endif /* CONFIG_KEXEC_CORE */
1724 static void __loaded_vmcs_clear(void *arg)
1726 struct loaded_vmcs *loaded_vmcs = arg;
1727 int cpu = raw_smp_processor_id();
1729 if (loaded_vmcs->cpu != cpu)
1730 return; /* vcpu migration can race with cpu offline */
1731 if (per_cpu(current_vmcs, cpu) == loaded_vmcs->vmcs)
1732 per_cpu(current_vmcs, cpu) = NULL;
1733 crash_disable_local_vmclear(cpu);
1734 list_del(&loaded_vmcs->loaded_vmcss_on_cpu_link);
1737 * we should ensure updating loaded_vmcs->loaded_vmcss_on_cpu_link
1738 * is before setting loaded_vmcs->vcpu to -1 which is done in
1739 * loaded_vmcs_init. Otherwise, other cpu can see vcpu = -1 fist
1740 * then adds the vmcs into percpu list before it is deleted.
1744 loaded_vmcs_init(loaded_vmcs);
1745 crash_enable_local_vmclear(cpu);
1748 static void loaded_vmcs_clear(struct loaded_vmcs *loaded_vmcs)
1750 int cpu = loaded_vmcs->cpu;
1753 smp_call_function_single(cpu,
1754 __loaded_vmcs_clear, loaded_vmcs, 1);
1757 static inline void vpid_sync_vcpu_single(int vpid)
1762 if (cpu_has_vmx_invvpid_single())
1763 __invvpid(VMX_VPID_EXTENT_SINGLE_CONTEXT, vpid, 0);
1766 static inline void vpid_sync_vcpu_global(void)
1768 if (cpu_has_vmx_invvpid_global())
1769 __invvpid(VMX_VPID_EXTENT_ALL_CONTEXT, 0, 0);
1772 static inline void vpid_sync_context(int vpid)
1774 if (cpu_has_vmx_invvpid_single())
1775 vpid_sync_vcpu_single(vpid);
1777 vpid_sync_vcpu_global();
1780 static inline void ept_sync_global(void)
1782 __invept(VMX_EPT_EXTENT_GLOBAL, 0, 0);
1785 static inline void ept_sync_context(u64 eptp)
1787 if (cpu_has_vmx_invept_context())
1788 __invept(VMX_EPT_EXTENT_CONTEXT, eptp, 0);
1793 static __always_inline void vmcs_check16(unsigned long field)
1795 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2000,
1796 "16-bit accessor invalid for 64-bit field");
1797 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2001,
1798 "16-bit accessor invalid for 64-bit high field");
1799 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x4000,
1800 "16-bit accessor invalid for 32-bit high field");
1801 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x6000,
1802 "16-bit accessor invalid for natural width field");
1805 static __always_inline void vmcs_check32(unsigned long field)
1807 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0,
1808 "32-bit accessor invalid for 16-bit field");
1809 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x6000,
1810 "32-bit accessor invalid for natural width field");
1813 static __always_inline void vmcs_check64(unsigned long field)
1815 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0,
1816 "64-bit accessor invalid for 16-bit field");
1817 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2001,
1818 "64-bit accessor invalid for 64-bit high field");
1819 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x4000,
1820 "64-bit accessor invalid for 32-bit field");
1821 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x6000,
1822 "64-bit accessor invalid for natural width field");
1825 static __always_inline void vmcs_checkl(unsigned long field)
1827 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0,
1828 "Natural width accessor invalid for 16-bit field");
1829 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2000,
1830 "Natural width accessor invalid for 64-bit field");
1831 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2001,
1832 "Natural width accessor invalid for 64-bit high field");
1833 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x4000,
1834 "Natural width accessor invalid for 32-bit field");
1837 static __always_inline unsigned long __vmcs_readl(unsigned long field)
1839 unsigned long value;
1841 asm volatile (__ex_clear(ASM_VMX_VMREAD_RDX_RAX, "%0")
1842 : "=a"(value) : "d"(field) : "cc");
1846 static __always_inline u16 vmcs_read16(unsigned long field)
1848 vmcs_check16(field);
1849 if (static_branch_unlikely(&enable_evmcs))
1850 return evmcs_read16(field);
1851 return __vmcs_readl(field);
1854 static __always_inline u32 vmcs_read32(unsigned long field)
1856 vmcs_check32(field);
1857 if (static_branch_unlikely(&enable_evmcs))
1858 return evmcs_read32(field);
1859 return __vmcs_readl(field);
1862 static __always_inline u64 vmcs_read64(unsigned long field)
1864 vmcs_check64(field);
1865 if (static_branch_unlikely(&enable_evmcs))
1866 return evmcs_read64(field);
1867 #ifdef CONFIG_X86_64
1868 return __vmcs_readl(field);
1870 return __vmcs_readl(field) | ((u64)__vmcs_readl(field+1) << 32);
1874 static __always_inline unsigned long vmcs_readl(unsigned long field)
1877 if (static_branch_unlikely(&enable_evmcs))
1878 return evmcs_read64(field);
1879 return __vmcs_readl(field);
1882 static noinline void vmwrite_error(unsigned long field, unsigned long value)
1884 printk(KERN_ERR "vmwrite error: reg %lx value %lx (err %d)\n",
1885 field, value, vmcs_read32(VM_INSTRUCTION_ERROR));
1889 static __always_inline void __vmcs_writel(unsigned long field, unsigned long value)
1893 asm volatile (__ex(ASM_VMX_VMWRITE_RAX_RDX) "; setna %0"
1894 : "=q"(error) : "a"(value), "d"(field) : "cc");
1895 if (unlikely(error))
1896 vmwrite_error(field, value);
1899 static __always_inline void vmcs_write16(unsigned long field, u16 value)
1901 vmcs_check16(field);
1902 if (static_branch_unlikely(&enable_evmcs))
1903 return evmcs_write16(field, value);
1905 __vmcs_writel(field, value);
1908 static __always_inline void vmcs_write32(unsigned long field, u32 value)
1910 vmcs_check32(field);
1911 if (static_branch_unlikely(&enable_evmcs))
1912 return evmcs_write32(field, value);
1914 __vmcs_writel(field, value);
1917 static __always_inline void vmcs_write64(unsigned long field, u64 value)
1919 vmcs_check64(field);
1920 if (static_branch_unlikely(&enable_evmcs))
1921 return evmcs_write64(field, value);
1923 __vmcs_writel(field, value);
1924 #ifndef CONFIG_X86_64
1926 __vmcs_writel(field+1, value >> 32);
1930 static __always_inline void vmcs_writel(unsigned long field, unsigned long value)
1933 if (static_branch_unlikely(&enable_evmcs))
1934 return evmcs_write64(field, value);
1936 __vmcs_writel(field, value);
1939 static __always_inline void vmcs_clear_bits(unsigned long field, u32 mask)
1941 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x2000,
1942 "vmcs_clear_bits does not support 64-bit fields");
1943 if (static_branch_unlikely(&enable_evmcs))
1944 return evmcs_write32(field, evmcs_read32(field) & ~mask);
1946 __vmcs_writel(field, __vmcs_readl(field) & ~mask);
1949 static __always_inline void vmcs_set_bits(unsigned long field, u32 mask)
1951 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x2000,
1952 "vmcs_set_bits does not support 64-bit fields");
1953 if (static_branch_unlikely(&enable_evmcs))
1954 return evmcs_write32(field, evmcs_read32(field) | mask);
1956 __vmcs_writel(field, __vmcs_readl(field) | mask);
1959 static inline void vm_entry_controls_reset_shadow(struct vcpu_vmx *vmx)
1961 vmx->vm_entry_controls_shadow = vmcs_read32(VM_ENTRY_CONTROLS);
1964 static inline void vm_entry_controls_init(struct vcpu_vmx *vmx, u32 val)
1966 vmcs_write32(VM_ENTRY_CONTROLS, val);
1967 vmx->vm_entry_controls_shadow = val;
1970 static inline void vm_entry_controls_set(struct vcpu_vmx *vmx, u32 val)
1972 if (vmx->vm_entry_controls_shadow != val)
1973 vm_entry_controls_init(vmx, val);
1976 static inline u32 vm_entry_controls_get(struct vcpu_vmx *vmx)
1978 return vmx->vm_entry_controls_shadow;
1982 static inline void vm_entry_controls_setbit(struct vcpu_vmx *vmx, u32 val)
1984 vm_entry_controls_set(vmx, vm_entry_controls_get(vmx) | val);
1987 static inline void vm_entry_controls_clearbit(struct vcpu_vmx *vmx, u32 val)
1989 vm_entry_controls_set(vmx, vm_entry_controls_get(vmx) & ~val);
1992 static inline void vm_exit_controls_reset_shadow(struct vcpu_vmx *vmx)
1994 vmx->vm_exit_controls_shadow = vmcs_read32(VM_EXIT_CONTROLS);
1997 static inline void vm_exit_controls_init(struct vcpu_vmx *vmx, u32 val)
1999 vmcs_write32(VM_EXIT_CONTROLS, val);
2000 vmx->vm_exit_controls_shadow = val;
2003 static inline void vm_exit_controls_set(struct vcpu_vmx *vmx, u32 val)
2005 if (vmx->vm_exit_controls_shadow != val)
2006 vm_exit_controls_init(vmx, val);
2009 static inline u32 vm_exit_controls_get(struct vcpu_vmx *vmx)
2011 return vmx->vm_exit_controls_shadow;
2015 static inline void vm_exit_controls_setbit(struct vcpu_vmx *vmx, u32 val)
2017 vm_exit_controls_set(vmx, vm_exit_controls_get(vmx) | val);
2020 static inline void vm_exit_controls_clearbit(struct vcpu_vmx *vmx, u32 val)
2022 vm_exit_controls_set(vmx, vm_exit_controls_get(vmx) & ~val);
2025 static void vmx_segment_cache_clear(struct vcpu_vmx *vmx)
2027 vmx->segment_cache.bitmask = 0;
2030 static bool vmx_segment_cache_test_set(struct vcpu_vmx *vmx, unsigned seg,
2034 u32 mask = 1 << (seg * SEG_FIELD_NR + field);
2036 if (!(vmx->vcpu.arch.regs_avail & (1 << VCPU_EXREG_SEGMENTS))) {
2037 vmx->vcpu.arch.regs_avail |= (1 << VCPU_EXREG_SEGMENTS);
2038 vmx->segment_cache.bitmask = 0;
2040 ret = vmx->segment_cache.bitmask & mask;
2041 vmx->segment_cache.bitmask |= mask;
2045 static u16 vmx_read_guest_seg_selector(struct vcpu_vmx *vmx, unsigned seg)
2047 u16 *p = &vmx->segment_cache.seg[seg].selector;
2049 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_SEL))
2050 *p = vmcs_read16(kvm_vmx_segment_fields[seg].selector);
2054 static ulong vmx_read_guest_seg_base(struct vcpu_vmx *vmx, unsigned seg)
2056 ulong *p = &vmx->segment_cache.seg[seg].base;
2058 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_BASE))
2059 *p = vmcs_readl(kvm_vmx_segment_fields[seg].base);
2063 static u32 vmx_read_guest_seg_limit(struct vcpu_vmx *vmx, unsigned seg)
2065 u32 *p = &vmx->segment_cache.seg[seg].limit;
2067 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_LIMIT))
2068 *p = vmcs_read32(kvm_vmx_segment_fields[seg].limit);
2072 static u32 vmx_read_guest_seg_ar(struct vcpu_vmx *vmx, unsigned seg)
2074 u32 *p = &vmx->segment_cache.seg[seg].ar;
2076 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_AR))
2077 *p = vmcs_read32(kvm_vmx_segment_fields[seg].ar_bytes);
2081 static void update_exception_bitmap(struct kvm_vcpu *vcpu)
2085 eb = (1u << PF_VECTOR) | (1u << UD_VECTOR) | (1u << MC_VECTOR) |
2086 (1u << DB_VECTOR) | (1u << AC_VECTOR);
2088 * Guest access to VMware backdoor ports could legitimately
2089 * trigger #GP because of TSS I/O permission bitmap.
2090 * We intercept those #GP and allow access to them anyway
2093 if (enable_vmware_backdoor)
2094 eb |= (1u << GP_VECTOR);
2095 if ((vcpu->guest_debug &
2096 (KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_SW_BP)) ==
2097 (KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_SW_BP))
2098 eb |= 1u << BP_VECTOR;
2099 if (to_vmx(vcpu)->rmode.vm86_active)
2102 eb &= ~(1u << PF_VECTOR); /* bypass_guest_pf = 0 */
2104 /* When we are running a nested L2 guest and L1 specified for it a
2105 * certain exception bitmap, we must trap the same exceptions and pass
2106 * them to L1. When running L2, we will only handle the exceptions
2107 * specified above if L1 did not want them.
2109 if (is_guest_mode(vcpu))
2110 eb |= get_vmcs12(vcpu)->exception_bitmap;
2112 vmcs_write32(EXCEPTION_BITMAP, eb);
2116 * Check if MSR is intercepted for currently loaded MSR bitmap.
2118 static bool msr_write_intercepted(struct kvm_vcpu *vcpu, u32 msr)
2120 unsigned long *msr_bitmap;
2121 int f = sizeof(unsigned long);
2123 if (!cpu_has_vmx_msr_bitmap())
2126 msr_bitmap = to_vmx(vcpu)->loaded_vmcs->msr_bitmap;
2128 if (msr <= 0x1fff) {
2129 return !!test_bit(msr, msr_bitmap + 0x800 / f);
2130 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
2132 return !!test_bit(msr, msr_bitmap + 0xc00 / f);
2139 * Check if MSR is intercepted for L01 MSR bitmap.
2141 static bool msr_write_intercepted_l01(struct kvm_vcpu *vcpu, u32 msr)
2143 unsigned long *msr_bitmap;
2144 int f = sizeof(unsigned long);
2146 if (!cpu_has_vmx_msr_bitmap())
2149 msr_bitmap = to_vmx(vcpu)->vmcs01.msr_bitmap;
2151 if (msr <= 0x1fff) {
2152 return !!test_bit(msr, msr_bitmap + 0x800 / f);
2153 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
2155 return !!test_bit(msr, msr_bitmap + 0xc00 / f);
2161 static void clear_atomic_switch_msr_special(struct vcpu_vmx *vmx,
2162 unsigned long entry, unsigned long exit)
2164 vm_entry_controls_clearbit(vmx, entry);
2165 vm_exit_controls_clearbit(vmx, exit);
2168 static void clear_atomic_switch_msr(struct vcpu_vmx *vmx, unsigned msr)
2171 struct msr_autoload *m = &vmx->msr_autoload;
2175 if (cpu_has_load_ia32_efer) {
2176 clear_atomic_switch_msr_special(vmx,
2177 VM_ENTRY_LOAD_IA32_EFER,
2178 VM_EXIT_LOAD_IA32_EFER);
2182 case MSR_CORE_PERF_GLOBAL_CTRL:
2183 if (cpu_has_load_perf_global_ctrl) {
2184 clear_atomic_switch_msr_special(vmx,
2185 VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL,
2186 VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL);
2192 for (i = 0; i < m->nr; ++i)
2193 if (m->guest[i].index == msr)
2199 m->guest[i] = m->guest[m->nr];
2200 m->host[i] = m->host[m->nr];
2201 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, m->nr);
2202 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, m->nr);
2205 static void add_atomic_switch_msr_special(struct vcpu_vmx *vmx,
2206 unsigned long entry, unsigned long exit,
2207 unsigned long guest_val_vmcs, unsigned long host_val_vmcs,
2208 u64 guest_val, u64 host_val)
2210 vmcs_write64(guest_val_vmcs, guest_val);
2211 vmcs_write64(host_val_vmcs, host_val);
2212 vm_entry_controls_setbit(vmx, entry);
2213 vm_exit_controls_setbit(vmx, exit);
2216 static void add_atomic_switch_msr(struct vcpu_vmx *vmx, unsigned msr,
2217 u64 guest_val, u64 host_val)
2220 struct msr_autoload *m = &vmx->msr_autoload;
2224 if (cpu_has_load_ia32_efer) {
2225 add_atomic_switch_msr_special(vmx,
2226 VM_ENTRY_LOAD_IA32_EFER,
2227 VM_EXIT_LOAD_IA32_EFER,
2230 guest_val, host_val);
2234 case MSR_CORE_PERF_GLOBAL_CTRL:
2235 if (cpu_has_load_perf_global_ctrl) {
2236 add_atomic_switch_msr_special(vmx,
2237 VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL,
2238 VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL,
2239 GUEST_IA32_PERF_GLOBAL_CTRL,
2240 HOST_IA32_PERF_GLOBAL_CTRL,
2241 guest_val, host_val);
2245 case MSR_IA32_PEBS_ENABLE:
2246 /* PEBS needs a quiescent period after being disabled (to write
2247 * a record). Disabling PEBS through VMX MSR swapping doesn't
2248 * provide that period, so a CPU could write host's record into
2251 wrmsrl(MSR_IA32_PEBS_ENABLE, 0);
2254 for (i = 0; i < m->nr; ++i)
2255 if (m->guest[i].index == msr)
2258 if (i == NR_AUTOLOAD_MSRS) {
2259 printk_once(KERN_WARNING "Not enough msr switch entries. "
2260 "Can't add msr %x\n", msr);
2262 } else if (i == m->nr) {
2264 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, m->nr);
2265 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, m->nr);
2268 m->guest[i].index = msr;
2269 m->guest[i].value = guest_val;
2270 m->host[i].index = msr;
2271 m->host[i].value = host_val;
2274 static bool update_transition_efer(struct vcpu_vmx *vmx, int efer_offset)
2276 u64 guest_efer = vmx->vcpu.arch.efer;
2277 u64 ignore_bits = 0;
2281 * NX is needed to handle CR0.WP=1, CR4.SMEP=1. Testing
2282 * host CPUID is more efficient than testing guest CPUID
2283 * or CR4. Host SMEP is anyway a requirement for guest SMEP.
2285 if (boot_cpu_has(X86_FEATURE_SMEP))
2286 guest_efer |= EFER_NX;
2287 else if (!(guest_efer & EFER_NX))
2288 ignore_bits |= EFER_NX;
2292 * LMA and LME handled by hardware; SCE meaningless outside long mode.
2294 ignore_bits |= EFER_SCE;
2295 #ifdef CONFIG_X86_64
2296 ignore_bits |= EFER_LMA | EFER_LME;
2297 /* SCE is meaningful only in long mode on Intel */
2298 if (guest_efer & EFER_LMA)
2299 ignore_bits &= ~(u64)EFER_SCE;
2302 clear_atomic_switch_msr(vmx, MSR_EFER);
2305 * On EPT, we can't emulate NX, so we must switch EFER atomically.
2306 * On CPUs that support "load IA32_EFER", always switch EFER
2307 * atomically, since it's faster than switching it manually.
2309 if (cpu_has_load_ia32_efer ||
2310 (enable_ept && ((vmx->vcpu.arch.efer ^ host_efer) & EFER_NX))) {
2311 if (!(guest_efer & EFER_LMA))
2312 guest_efer &= ~EFER_LME;
2313 if (guest_efer != host_efer)
2314 add_atomic_switch_msr(vmx, MSR_EFER,
2315 guest_efer, host_efer);
2318 guest_efer &= ~ignore_bits;
2319 guest_efer |= host_efer & ignore_bits;
2321 vmx->guest_msrs[efer_offset].data = guest_efer;
2322 vmx->guest_msrs[efer_offset].mask = ~ignore_bits;
2328 #ifdef CONFIG_X86_32
2330 * On 32-bit kernels, VM exits still load the FS and GS bases from the
2331 * VMCS rather than the segment table. KVM uses this helper to figure
2332 * out the current bases to poke them into the VMCS before entry.
2334 static unsigned long segment_base(u16 selector)
2336 struct desc_struct *table;
2339 if (!(selector & ~SEGMENT_RPL_MASK))
2342 table = get_current_gdt_ro();
2344 if ((selector & SEGMENT_TI_MASK) == SEGMENT_LDT) {
2345 u16 ldt_selector = kvm_read_ldt();
2347 if (!(ldt_selector & ~SEGMENT_RPL_MASK))
2350 table = (struct desc_struct *)segment_base(ldt_selector);
2352 v = get_desc_base(&table[selector >> 3]);
2357 static void vmx_save_host_state(struct kvm_vcpu *vcpu)
2359 struct vcpu_vmx *vmx = to_vmx(vcpu);
2360 #ifdef CONFIG_X86_64
2361 int cpu = raw_smp_processor_id();
2365 if (vmx->host_state.loaded)
2368 vmx->host_state.loaded = 1;
2370 * Set host fs and gs selectors. Unfortunately, 22.2.3 does not
2371 * allow segment selectors with cpl > 0 or ti == 1.
2373 vmx->host_state.ldt_sel = kvm_read_ldt();
2374 vmx->host_state.gs_ldt_reload_needed = vmx->host_state.ldt_sel;
2376 #ifdef CONFIG_X86_64
2377 save_fsgs_for_kvm();
2378 vmx->host_state.fs_sel = current->thread.fsindex;
2379 vmx->host_state.gs_sel = current->thread.gsindex;
2381 savesegment(fs, vmx->host_state.fs_sel);
2382 savesegment(gs, vmx->host_state.gs_sel);
2384 if (!(vmx->host_state.fs_sel & 7)) {
2385 vmcs_write16(HOST_FS_SELECTOR, vmx->host_state.fs_sel);
2386 vmx->host_state.fs_reload_needed = 0;
2388 vmcs_write16(HOST_FS_SELECTOR, 0);
2389 vmx->host_state.fs_reload_needed = 1;
2391 if (!(vmx->host_state.gs_sel & 7))
2392 vmcs_write16(HOST_GS_SELECTOR, vmx->host_state.gs_sel);
2394 vmcs_write16(HOST_GS_SELECTOR, 0);
2395 vmx->host_state.gs_ldt_reload_needed = 1;
2398 #ifdef CONFIG_X86_64
2399 savesegment(ds, vmx->host_state.ds_sel);
2400 savesegment(es, vmx->host_state.es_sel);
2402 vmcs_writel(HOST_FS_BASE, current->thread.fsbase);
2403 vmcs_writel(HOST_GS_BASE, cpu_kernelmode_gs_base(cpu));
2405 vmx->msr_host_kernel_gs_base = current->thread.gsbase;
2406 if (is_long_mode(&vmx->vcpu))
2407 wrmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base);
2409 vmcs_writel(HOST_FS_BASE, segment_base(vmx->host_state.fs_sel));
2410 vmcs_writel(HOST_GS_BASE, segment_base(vmx->host_state.gs_sel));
2412 if (boot_cpu_has(X86_FEATURE_MPX))
2413 rdmsrl(MSR_IA32_BNDCFGS, vmx->host_state.msr_host_bndcfgs);
2414 for (i = 0; i < vmx->save_nmsrs; ++i)
2415 kvm_set_shared_msr(vmx->guest_msrs[i].index,
2416 vmx->guest_msrs[i].data,
2417 vmx->guest_msrs[i].mask);
2420 static void __vmx_load_host_state(struct vcpu_vmx *vmx)
2422 if (!vmx->host_state.loaded)
2425 ++vmx->vcpu.stat.host_state_reload;
2426 vmx->host_state.loaded = 0;
2427 #ifdef CONFIG_X86_64
2428 if (is_long_mode(&vmx->vcpu))
2429 rdmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base);
2431 if (vmx->host_state.gs_ldt_reload_needed) {
2432 kvm_load_ldt(vmx->host_state.ldt_sel);
2433 #ifdef CONFIG_X86_64
2434 load_gs_index(vmx->host_state.gs_sel);
2436 loadsegment(gs, vmx->host_state.gs_sel);
2439 if (vmx->host_state.fs_reload_needed)
2440 loadsegment(fs, vmx->host_state.fs_sel);
2441 #ifdef CONFIG_X86_64
2442 if (unlikely(vmx->host_state.ds_sel | vmx->host_state.es_sel)) {
2443 loadsegment(ds, vmx->host_state.ds_sel);
2444 loadsegment(es, vmx->host_state.es_sel);
2447 invalidate_tss_limit();
2448 #ifdef CONFIG_X86_64
2449 wrmsrl(MSR_KERNEL_GS_BASE, vmx->msr_host_kernel_gs_base);
2451 if (vmx->host_state.msr_host_bndcfgs)
2452 wrmsrl(MSR_IA32_BNDCFGS, vmx->host_state.msr_host_bndcfgs);
2453 load_fixmap_gdt(raw_smp_processor_id());
2456 static void vmx_load_host_state(struct vcpu_vmx *vmx)
2459 __vmx_load_host_state(vmx);
2463 static void vmx_vcpu_pi_load(struct kvm_vcpu *vcpu, int cpu)
2465 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
2466 struct pi_desc old, new;
2470 * In case of hot-plug or hot-unplug, we may have to undo
2471 * vmx_vcpu_pi_put even if there is no assigned device. And we
2472 * always keep PI.NDST up to date for simplicity: it makes the
2473 * code easier, and CPU migration is not a fast path.
2475 if (!pi_test_sn(pi_desc) && vcpu->cpu == cpu)
2479 * First handle the simple case where no cmpxchg is necessary; just
2480 * allow posting non-urgent interrupts.
2482 * If the 'nv' field is POSTED_INTR_WAKEUP_VECTOR, do not change
2483 * PI.NDST: pi_post_block will do it for us and the wakeup_handler
2484 * expects the VCPU to be on the blocked_vcpu_list that matches
2487 if (pi_desc->nv == POSTED_INTR_WAKEUP_VECTOR ||
2489 pi_clear_sn(pi_desc);
2493 /* The full case. */
2495 old.control = new.control = pi_desc->control;
2497 dest = cpu_physical_id(cpu);
2499 if (x2apic_enabled())
2502 new.ndst = (dest << 8) & 0xFF00;
2505 } while (cmpxchg64(&pi_desc->control, old.control,
2506 new.control) != old.control);
2509 static void decache_tsc_multiplier(struct vcpu_vmx *vmx)
2511 vmx->current_tsc_ratio = vmx->vcpu.arch.tsc_scaling_ratio;
2512 vmcs_write64(TSC_MULTIPLIER, vmx->current_tsc_ratio);
2516 * Switches to specified vcpu, until a matching vcpu_put(), but assumes
2517 * vcpu mutex is already taken.
2519 static void vmx_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
2521 struct vcpu_vmx *vmx = to_vmx(vcpu);
2522 bool already_loaded = vmx->loaded_vmcs->cpu == cpu;
2524 if (!already_loaded) {
2525 loaded_vmcs_clear(vmx->loaded_vmcs);
2526 local_irq_disable();
2527 crash_disable_local_vmclear(cpu);
2530 * Read loaded_vmcs->cpu should be before fetching
2531 * loaded_vmcs->loaded_vmcss_on_cpu_link.
2532 * See the comments in __loaded_vmcs_clear().
2536 list_add(&vmx->loaded_vmcs->loaded_vmcss_on_cpu_link,
2537 &per_cpu(loaded_vmcss_on_cpu, cpu));
2538 crash_enable_local_vmclear(cpu);
2542 if (per_cpu(current_vmcs, cpu) != vmx->loaded_vmcs->vmcs) {
2543 per_cpu(current_vmcs, cpu) = vmx->loaded_vmcs->vmcs;
2544 vmcs_load(vmx->loaded_vmcs->vmcs);
2545 indirect_branch_prediction_barrier();
2548 if (!already_loaded) {
2549 void *gdt = get_current_gdt_ro();
2550 unsigned long sysenter_esp;
2552 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
2555 * Linux uses per-cpu TSS and GDT, so set these when switching
2556 * processors. See 22.2.4.
2558 vmcs_writel(HOST_TR_BASE,
2559 (unsigned long)&get_cpu_entry_area(cpu)->tss.x86_tss);
2560 vmcs_writel(HOST_GDTR_BASE, (unsigned long)gdt); /* 22.2.4 */
2563 * VM exits change the host TR limit to 0x67 after a VM
2564 * exit. This is okay, since 0x67 covers everything except
2565 * the IO bitmap and have have code to handle the IO bitmap
2566 * being lost after a VM exit.
2568 BUILD_BUG_ON(IO_BITMAP_OFFSET - 1 != 0x67);
2570 rdmsrl(MSR_IA32_SYSENTER_ESP, sysenter_esp);
2571 vmcs_writel(HOST_IA32_SYSENTER_ESP, sysenter_esp); /* 22.2.3 */
2573 vmx->loaded_vmcs->cpu = cpu;
2576 /* Setup TSC multiplier */
2577 if (kvm_has_tsc_control &&
2578 vmx->current_tsc_ratio != vcpu->arch.tsc_scaling_ratio)
2579 decache_tsc_multiplier(vmx);
2581 vmx_vcpu_pi_load(vcpu, cpu);
2582 vmx->host_pkru = read_pkru();
2583 vmx->host_debugctlmsr = get_debugctlmsr();
2586 static void vmx_vcpu_pi_put(struct kvm_vcpu *vcpu)
2588 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
2590 if (!kvm_arch_has_assigned_device(vcpu->kvm) ||
2591 !irq_remapping_cap(IRQ_POSTING_CAP) ||
2592 !kvm_vcpu_apicv_active(vcpu))
2595 /* Set SN when the vCPU is preempted */
2596 if (vcpu->preempted)
2600 static void vmx_vcpu_put(struct kvm_vcpu *vcpu)
2602 vmx_vcpu_pi_put(vcpu);
2604 __vmx_load_host_state(to_vmx(vcpu));
2607 static bool emulation_required(struct kvm_vcpu *vcpu)
2609 return emulate_invalid_guest_state && !guest_state_valid(vcpu);
2612 static void vmx_decache_cr0_guest_bits(struct kvm_vcpu *vcpu);
2615 * Return the cr0 value that a nested guest would read. This is a combination
2616 * of the real cr0 used to run the guest (guest_cr0), and the bits shadowed by
2617 * its hypervisor (cr0_read_shadow).
2619 static inline unsigned long nested_read_cr0(struct vmcs12 *fields)
2621 return (fields->guest_cr0 & ~fields->cr0_guest_host_mask) |
2622 (fields->cr0_read_shadow & fields->cr0_guest_host_mask);
2624 static inline unsigned long nested_read_cr4(struct vmcs12 *fields)
2626 return (fields->guest_cr4 & ~fields->cr4_guest_host_mask) |
2627 (fields->cr4_read_shadow & fields->cr4_guest_host_mask);
2630 static unsigned long vmx_get_rflags(struct kvm_vcpu *vcpu)
2632 unsigned long rflags, save_rflags;
2634 if (!test_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail)) {
2635 __set_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail);
2636 rflags = vmcs_readl(GUEST_RFLAGS);
2637 if (to_vmx(vcpu)->rmode.vm86_active) {
2638 rflags &= RMODE_GUEST_OWNED_EFLAGS_BITS;
2639 save_rflags = to_vmx(vcpu)->rmode.save_rflags;
2640 rflags |= save_rflags & ~RMODE_GUEST_OWNED_EFLAGS_BITS;
2642 to_vmx(vcpu)->rflags = rflags;
2644 return to_vmx(vcpu)->rflags;
2647 static void vmx_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags)
2649 unsigned long old_rflags = vmx_get_rflags(vcpu);
2651 __set_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail);
2652 to_vmx(vcpu)->rflags = rflags;
2653 if (to_vmx(vcpu)->rmode.vm86_active) {
2654 to_vmx(vcpu)->rmode.save_rflags = rflags;
2655 rflags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM;
2657 vmcs_writel(GUEST_RFLAGS, rflags);
2659 if ((old_rflags ^ to_vmx(vcpu)->rflags) & X86_EFLAGS_VM)
2660 to_vmx(vcpu)->emulation_required = emulation_required(vcpu);
2663 static u32 vmx_get_interrupt_shadow(struct kvm_vcpu *vcpu)
2665 u32 interruptibility = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
2668 if (interruptibility & GUEST_INTR_STATE_STI)
2669 ret |= KVM_X86_SHADOW_INT_STI;
2670 if (interruptibility & GUEST_INTR_STATE_MOV_SS)
2671 ret |= KVM_X86_SHADOW_INT_MOV_SS;
2676 static void vmx_set_interrupt_shadow(struct kvm_vcpu *vcpu, int mask)
2678 u32 interruptibility_old = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
2679 u32 interruptibility = interruptibility_old;
2681 interruptibility &= ~(GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS);
2683 if (mask & KVM_X86_SHADOW_INT_MOV_SS)
2684 interruptibility |= GUEST_INTR_STATE_MOV_SS;
2685 else if (mask & KVM_X86_SHADOW_INT_STI)
2686 interruptibility |= GUEST_INTR_STATE_STI;
2688 if ((interruptibility != interruptibility_old))
2689 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, interruptibility);
2692 static void skip_emulated_instruction(struct kvm_vcpu *vcpu)
2696 rip = kvm_rip_read(vcpu);
2697 rip += vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
2698 kvm_rip_write(vcpu, rip);
2700 /* skipping an emulated instruction also counts */
2701 vmx_set_interrupt_shadow(vcpu, 0);
2704 static void nested_vmx_inject_exception_vmexit(struct kvm_vcpu *vcpu,
2705 unsigned long exit_qual)
2707 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
2708 unsigned int nr = vcpu->arch.exception.nr;
2709 u32 intr_info = nr | INTR_INFO_VALID_MASK;
2711 if (vcpu->arch.exception.has_error_code) {
2712 vmcs12->vm_exit_intr_error_code = vcpu->arch.exception.error_code;
2713 intr_info |= INTR_INFO_DELIVER_CODE_MASK;
2716 if (kvm_exception_is_soft(nr))
2717 intr_info |= INTR_TYPE_SOFT_EXCEPTION;
2719 intr_info |= INTR_TYPE_HARD_EXCEPTION;
2721 if (!(vmcs12->idt_vectoring_info_field & VECTORING_INFO_VALID_MASK) &&
2722 vmx_get_nmi_mask(vcpu))
2723 intr_info |= INTR_INFO_UNBLOCK_NMI;
2725 nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI, intr_info, exit_qual);
2729 * KVM wants to inject page-faults which it got to the guest. This function
2730 * checks whether in a nested guest, we need to inject them to L1 or L2.
2732 static int nested_vmx_check_exception(struct kvm_vcpu *vcpu, unsigned long *exit_qual)
2734 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
2735 unsigned int nr = vcpu->arch.exception.nr;
2737 if (nr == PF_VECTOR) {
2738 if (vcpu->arch.exception.nested_apf) {
2739 *exit_qual = vcpu->arch.apf.nested_apf_token;
2743 * FIXME: we must not write CR2 when L1 intercepts an L2 #PF exception.
2744 * The fix is to add the ancillary datum (CR2 or DR6) to structs
2745 * kvm_queued_exception and kvm_vcpu_events, so that CR2 and DR6
2746 * can be written only when inject_pending_event runs. This should be
2747 * conditional on a new capability---if the capability is disabled,
2748 * kvm_multiple_exception would write the ancillary information to
2749 * CR2 or DR6, for backwards ABI-compatibility.
2751 if (nested_vmx_is_page_fault_vmexit(vmcs12,
2752 vcpu->arch.exception.error_code)) {
2753 *exit_qual = vcpu->arch.cr2;
2757 if (vmcs12->exception_bitmap & (1u << nr)) {
2758 if (nr == DB_VECTOR)
2759 *exit_qual = vcpu->arch.dr6;
2769 static void vmx_clear_hlt(struct kvm_vcpu *vcpu)
2772 * Ensure that we clear the HLT state in the VMCS. We don't need to
2773 * explicitly skip the instruction because if the HLT state is set,
2774 * then the instruction is already executing and RIP has already been
2777 if (kvm_hlt_in_guest(vcpu->kvm) &&
2778 vmcs_read32(GUEST_ACTIVITY_STATE) == GUEST_ACTIVITY_HLT)
2779 vmcs_write32(GUEST_ACTIVITY_STATE, GUEST_ACTIVITY_ACTIVE);
2782 static void vmx_queue_exception(struct kvm_vcpu *vcpu)
2784 struct vcpu_vmx *vmx = to_vmx(vcpu);
2785 unsigned nr = vcpu->arch.exception.nr;
2786 bool has_error_code = vcpu->arch.exception.has_error_code;
2787 u32 error_code = vcpu->arch.exception.error_code;
2788 u32 intr_info = nr | INTR_INFO_VALID_MASK;
2790 if (has_error_code) {
2791 vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE, error_code);
2792 intr_info |= INTR_INFO_DELIVER_CODE_MASK;
2795 if (vmx->rmode.vm86_active) {
2797 if (kvm_exception_is_soft(nr))
2798 inc_eip = vcpu->arch.event_exit_inst_len;
2799 if (kvm_inject_realmode_interrupt(vcpu, nr, inc_eip) != EMULATE_DONE)
2800 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
2804 WARN_ON_ONCE(vmx->emulation_required);
2806 if (kvm_exception_is_soft(nr)) {
2807 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
2808 vmx->vcpu.arch.event_exit_inst_len);
2809 intr_info |= INTR_TYPE_SOFT_EXCEPTION;
2811 intr_info |= INTR_TYPE_HARD_EXCEPTION;
2813 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr_info);
2815 vmx_clear_hlt(vcpu);
2818 static bool vmx_rdtscp_supported(void)
2820 return cpu_has_vmx_rdtscp();
2823 static bool vmx_invpcid_supported(void)
2825 return cpu_has_vmx_invpcid() && enable_ept;
2829 * Swap MSR entry in host/guest MSR entry array.
2831 static void move_msr_up(struct vcpu_vmx *vmx, int from, int to)
2833 struct shared_msr_entry tmp;
2835 tmp = vmx->guest_msrs[to];
2836 vmx->guest_msrs[to] = vmx->guest_msrs[from];
2837 vmx->guest_msrs[from] = tmp;
2841 * Set up the vmcs to automatically save and restore system
2842 * msrs. Don't touch the 64-bit msrs if the guest is in legacy
2843 * mode, as fiddling with msrs is very expensive.
2845 static void setup_msrs(struct vcpu_vmx *vmx)
2847 int save_nmsrs, index;
2850 #ifdef CONFIG_X86_64
2851 if (is_long_mode(&vmx->vcpu)) {
2852 index = __find_msr_index(vmx, MSR_SYSCALL_MASK);
2854 move_msr_up(vmx, index, save_nmsrs++);
2855 index = __find_msr_index(vmx, MSR_LSTAR);
2857 move_msr_up(vmx, index, save_nmsrs++);
2858 index = __find_msr_index(vmx, MSR_CSTAR);
2860 move_msr_up(vmx, index, save_nmsrs++);
2861 index = __find_msr_index(vmx, MSR_TSC_AUX);
2862 if (index >= 0 && guest_cpuid_has(&vmx->vcpu, X86_FEATURE_RDTSCP))
2863 move_msr_up(vmx, index, save_nmsrs++);
2865 * MSR_STAR is only needed on long mode guests, and only
2866 * if efer.sce is enabled.
2868 index = __find_msr_index(vmx, MSR_STAR);
2869 if ((index >= 0) && (vmx->vcpu.arch.efer & EFER_SCE))
2870 move_msr_up(vmx, index, save_nmsrs++);
2873 index = __find_msr_index(vmx, MSR_EFER);
2874 if (index >= 0 && update_transition_efer(vmx, index))
2875 move_msr_up(vmx, index, save_nmsrs++);
2877 vmx->save_nmsrs = save_nmsrs;
2879 if (cpu_has_vmx_msr_bitmap())
2880 vmx_update_msr_bitmap(&vmx->vcpu);
2884 * reads and returns guest's timestamp counter "register"
2885 * guest_tsc = (host_tsc * tsc multiplier) >> 48 + tsc_offset
2886 * -- Intel TSC Scaling for Virtualization White Paper, sec 1.3
2888 static u64 guest_read_tsc(struct kvm_vcpu *vcpu)
2890 u64 host_tsc, tsc_offset;
2893 tsc_offset = vmcs_read64(TSC_OFFSET);
2894 return kvm_scale_tsc(vcpu, host_tsc) + tsc_offset;
2898 * writes 'offset' into guest's timestamp counter offset register
2900 static void vmx_write_tsc_offset(struct kvm_vcpu *vcpu, u64 offset)
2902 if (is_guest_mode(vcpu)) {
2904 * We're here if L1 chose not to trap WRMSR to TSC. According
2905 * to the spec, this should set L1's TSC; The offset that L1
2906 * set for L2 remains unchanged, and still needs to be added
2907 * to the newly set TSC to get L2's TSC.
2909 struct vmcs12 *vmcs12;
2910 /* recalculate vmcs02.TSC_OFFSET: */
2911 vmcs12 = get_vmcs12(vcpu);
2912 vmcs_write64(TSC_OFFSET, offset +
2913 (nested_cpu_has(vmcs12, CPU_BASED_USE_TSC_OFFSETING) ?
2914 vmcs12->tsc_offset : 0));
2916 trace_kvm_write_tsc_offset(vcpu->vcpu_id,
2917 vmcs_read64(TSC_OFFSET), offset);
2918 vmcs_write64(TSC_OFFSET, offset);
2923 * nested_vmx_allowed() checks whether a guest should be allowed to use VMX
2924 * instructions and MSRs (i.e., nested VMX). Nested VMX is disabled for
2925 * all guests if the "nested" module option is off, and can also be disabled
2926 * for a single guest by disabling its VMX cpuid bit.
2928 static inline bool nested_vmx_allowed(struct kvm_vcpu *vcpu)
2930 return nested && guest_cpuid_has(vcpu, X86_FEATURE_VMX);
2934 * nested_vmx_setup_ctls_msrs() sets up variables containing the values to be
2935 * returned for the various VMX controls MSRs when nested VMX is enabled.
2936 * The same values should also be used to verify that vmcs12 control fields are
2937 * valid during nested entry from L1 to L2.
2938 * Each of these control msrs has a low and high 32-bit half: A low bit is on
2939 * if the corresponding bit in the (32-bit) control field *must* be on, and a
2940 * bit in the high half is on if the corresponding bit in the control field
2941 * may be on. See also vmx_control_verify().
2943 static void nested_vmx_setup_ctls_msrs(struct nested_vmx_msrs *msrs, bool apicv)
2946 memset(msrs, 0, sizeof(*msrs));
2951 * Note that as a general rule, the high half of the MSRs (bits in
2952 * the control fields which may be 1) should be initialized by the
2953 * intersection of the underlying hardware's MSR (i.e., features which
2954 * can be supported) and the list of features we want to expose -
2955 * because they are known to be properly supported in our code.
2956 * Also, usually, the low half of the MSRs (bits which must be 1) can
2957 * be set to 0, meaning that L1 may turn off any of these bits. The
2958 * reason is that if one of these bits is necessary, it will appear
2959 * in vmcs01 and prepare_vmcs02, when it bitwise-or's the control
2960 * fields of vmcs01 and vmcs02, will turn these bits off - and
2961 * nested_vmx_exit_reflected() will not pass related exits to L1.
2962 * These rules have exceptions below.
2965 /* pin-based controls */
2966 rdmsr(MSR_IA32_VMX_PINBASED_CTLS,
2967 msrs->pinbased_ctls_low,
2968 msrs->pinbased_ctls_high);
2969 msrs->pinbased_ctls_low |=
2970 PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
2971 msrs->pinbased_ctls_high &=
2972 PIN_BASED_EXT_INTR_MASK |
2973 PIN_BASED_NMI_EXITING |
2974 PIN_BASED_VIRTUAL_NMIS |
2975 (apicv ? PIN_BASED_POSTED_INTR : 0);
2976 msrs->pinbased_ctls_high |=
2977 PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR |
2978 PIN_BASED_VMX_PREEMPTION_TIMER;
2981 rdmsr(MSR_IA32_VMX_EXIT_CTLS,
2982 msrs->exit_ctls_low,
2983 msrs->exit_ctls_high);
2984 msrs->exit_ctls_low =
2985 VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR;
2987 msrs->exit_ctls_high &=
2988 #ifdef CONFIG_X86_64
2989 VM_EXIT_HOST_ADDR_SPACE_SIZE |
2991 VM_EXIT_LOAD_IA32_PAT | VM_EXIT_SAVE_IA32_PAT;
2992 msrs->exit_ctls_high |=
2993 VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR |
2994 VM_EXIT_LOAD_IA32_EFER | VM_EXIT_SAVE_IA32_EFER |
2995 VM_EXIT_SAVE_VMX_PREEMPTION_TIMER | VM_EXIT_ACK_INTR_ON_EXIT;
2997 if (kvm_mpx_supported())
2998 msrs->exit_ctls_high |= VM_EXIT_CLEAR_BNDCFGS;
3000 /* We support free control of debug control saving. */
3001 msrs->exit_ctls_low &= ~VM_EXIT_SAVE_DEBUG_CONTROLS;
3003 /* entry controls */
3004 rdmsr(MSR_IA32_VMX_ENTRY_CTLS,
3005 msrs->entry_ctls_low,
3006 msrs->entry_ctls_high);
3007 msrs->entry_ctls_low =
3008 VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR;
3009 msrs->entry_ctls_high &=
3010 #ifdef CONFIG_X86_64
3011 VM_ENTRY_IA32E_MODE |
3013 VM_ENTRY_LOAD_IA32_PAT;
3014 msrs->entry_ctls_high |=
3015 (VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR | VM_ENTRY_LOAD_IA32_EFER);
3016 if (kvm_mpx_supported())
3017 msrs->entry_ctls_high |= VM_ENTRY_LOAD_BNDCFGS;
3019 /* We support free control of debug control loading. */
3020 msrs->entry_ctls_low &= ~VM_ENTRY_LOAD_DEBUG_CONTROLS;
3022 /* cpu-based controls */
3023 rdmsr(MSR_IA32_VMX_PROCBASED_CTLS,
3024 msrs->procbased_ctls_low,
3025 msrs->procbased_ctls_high);
3026 msrs->procbased_ctls_low =
3027 CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
3028 msrs->procbased_ctls_high &=
3029 CPU_BASED_VIRTUAL_INTR_PENDING |
3030 CPU_BASED_VIRTUAL_NMI_PENDING | CPU_BASED_USE_TSC_OFFSETING |
3031 CPU_BASED_HLT_EXITING | CPU_BASED_INVLPG_EXITING |
3032 CPU_BASED_MWAIT_EXITING | CPU_BASED_CR3_LOAD_EXITING |
3033 CPU_BASED_CR3_STORE_EXITING |
3034 #ifdef CONFIG_X86_64
3035 CPU_BASED_CR8_LOAD_EXITING | CPU_BASED_CR8_STORE_EXITING |
3037 CPU_BASED_MOV_DR_EXITING | CPU_BASED_UNCOND_IO_EXITING |
3038 CPU_BASED_USE_IO_BITMAPS | CPU_BASED_MONITOR_TRAP_FLAG |
3039 CPU_BASED_MONITOR_EXITING | CPU_BASED_RDPMC_EXITING |
3040 CPU_BASED_RDTSC_EXITING | CPU_BASED_PAUSE_EXITING |
3041 CPU_BASED_TPR_SHADOW | CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
3043 * We can allow some features even when not supported by the
3044 * hardware. For example, L1 can specify an MSR bitmap - and we
3045 * can use it to avoid exits to L1 - even when L0 runs L2
3046 * without MSR bitmaps.
3048 msrs->procbased_ctls_high |=
3049 CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR |
3050 CPU_BASED_USE_MSR_BITMAPS;
3052 /* We support free control of CR3 access interception. */
3053 msrs->procbased_ctls_low &=
3054 ~(CPU_BASED_CR3_LOAD_EXITING | CPU_BASED_CR3_STORE_EXITING);
3057 * secondary cpu-based controls. Do not include those that
3058 * depend on CPUID bits, they are added later by vmx_cpuid_update.
3060 rdmsr(MSR_IA32_VMX_PROCBASED_CTLS2,
3061 msrs->secondary_ctls_low,
3062 msrs->secondary_ctls_high);
3063 msrs->secondary_ctls_low = 0;
3064 msrs->secondary_ctls_high &=
3065 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
3066 SECONDARY_EXEC_DESC |
3067 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
3068 SECONDARY_EXEC_APIC_REGISTER_VIRT |
3069 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY |
3070 SECONDARY_EXEC_WBINVD_EXITING;
3073 /* nested EPT: emulate EPT also to L1 */
3074 msrs->secondary_ctls_high |=
3075 SECONDARY_EXEC_ENABLE_EPT;
3076 msrs->ept_caps = VMX_EPT_PAGE_WALK_4_BIT |
3077 VMX_EPTP_WB_BIT | VMX_EPT_INVEPT_BIT;
3078 if (cpu_has_vmx_ept_execute_only())
3080 VMX_EPT_EXECUTE_ONLY_BIT;
3081 msrs->ept_caps &= vmx_capability.ept;
3082 msrs->ept_caps |= VMX_EPT_EXTENT_GLOBAL_BIT |
3083 VMX_EPT_EXTENT_CONTEXT_BIT | VMX_EPT_2MB_PAGE_BIT |
3084 VMX_EPT_1GB_PAGE_BIT;
3085 if (enable_ept_ad_bits) {
3086 msrs->secondary_ctls_high |=
3087 SECONDARY_EXEC_ENABLE_PML;
3088 msrs->ept_caps |= VMX_EPT_AD_BIT;
3092 if (cpu_has_vmx_vmfunc()) {
3093 msrs->secondary_ctls_high |=
3094 SECONDARY_EXEC_ENABLE_VMFUNC;
3096 * Advertise EPTP switching unconditionally
3097 * since we emulate it
3100 msrs->vmfunc_controls =
3101 VMX_VMFUNC_EPTP_SWITCHING;
3105 * Old versions of KVM use the single-context version without
3106 * checking for support, so declare that it is supported even
3107 * though it is treated as global context. The alternative is
3108 * not failing the single-context invvpid, and it is worse.
3111 msrs->secondary_ctls_high |=
3112 SECONDARY_EXEC_ENABLE_VPID;
3113 msrs->vpid_caps = VMX_VPID_INVVPID_BIT |
3114 VMX_VPID_EXTENT_SUPPORTED_MASK;
3117 if (enable_unrestricted_guest)
3118 msrs->secondary_ctls_high |=
3119 SECONDARY_EXEC_UNRESTRICTED_GUEST;
3121 /* miscellaneous data */
3122 rdmsr(MSR_IA32_VMX_MISC,
3125 msrs->misc_low &= VMX_MISC_SAVE_EFER_LMA;
3127 VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE |
3128 VMX_MISC_ACTIVITY_HLT;
3129 msrs->misc_high = 0;
3132 * This MSR reports some information about VMX support. We
3133 * should return information about the VMX we emulate for the
3134 * guest, and the VMCS structure we give it - not about the
3135 * VMX support of the underlying hardware.
3139 VMX_BASIC_TRUE_CTLS |
3140 ((u64)VMCS12_SIZE << VMX_BASIC_VMCS_SIZE_SHIFT) |
3141 (VMX_BASIC_MEM_TYPE_WB << VMX_BASIC_MEM_TYPE_SHIFT);
3143 if (cpu_has_vmx_basic_inout())
3144 msrs->basic |= VMX_BASIC_INOUT;
3147 * These MSRs specify bits which the guest must keep fixed on
3148 * while L1 is in VMXON mode (in L1's root mode, or running an L2).
3149 * We picked the standard core2 setting.
3151 #define VMXON_CR0_ALWAYSON (X86_CR0_PE | X86_CR0_PG | X86_CR0_NE)
3152 #define VMXON_CR4_ALWAYSON X86_CR4_VMXE
3153 msrs->cr0_fixed0 = VMXON_CR0_ALWAYSON;
3154 msrs->cr4_fixed0 = VMXON_CR4_ALWAYSON;
3156 /* These MSRs specify bits which the guest must keep fixed off. */
3157 rdmsrl(MSR_IA32_VMX_CR0_FIXED1, msrs->cr0_fixed1);
3158 rdmsrl(MSR_IA32_VMX_CR4_FIXED1, msrs->cr4_fixed1);
3160 /* highest index: VMX_PREEMPTION_TIMER_VALUE */
3161 msrs->vmcs_enum = VMCS12_MAX_FIELD_INDEX << 1;
3165 * if fixed0[i] == 1: val[i] must be 1
3166 * if fixed1[i] == 0: val[i] must be 0
3168 static inline bool fixed_bits_valid(u64 val, u64 fixed0, u64 fixed1)
3170 return ((val & fixed1) | fixed0) == val;
3173 static inline bool vmx_control_verify(u32 control, u32 low, u32 high)
3175 return fixed_bits_valid(control, low, high);
3178 static inline u64 vmx_control_msr(u32 low, u32 high)
3180 return low | ((u64)high << 32);
3183 static bool is_bitwise_subset(u64 superset, u64 subset, u64 mask)
3188 return (superset | subset) == superset;
3191 static int vmx_restore_vmx_basic(struct vcpu_vmx *vmx, u64 data)
3193 const u64 feature_and_reserved =
3194 /* feature (except bit 48; see below) */
3195 BIT_ULL(49) | BIT_ULL(54) | BIT_ULL(55) |
3197 BIT_ULL(31) | GENMASK_ULL(47, 45) | GENMASK_ULL(63, 56);
3198 u64 vmx_basic = vmx->nested.msrs.basic;
3200 if (!is_bitwise_subset(vmx_basic, data, feature_and_reserved))
3204 * KVM does not emulate a version of VMX that constrains physical
3205 * addresses of VMX structures (e.g. VMCS) to 32-bits.
3207 if (data & BIT_ULL(48))
3210 if (vmx_basic_vmcs_revision_id(vmx_basic) !=
3211 vmx_basic_vmcs_revision_id(data))
3214 if (vmx_basic_vmcs_size(vmx_basic) > vmx_basic_vmcs_size(data))
3217 vmx->nested.msrs.basic = data;
3222 vmx_restore_control_msr(struct vcpu_vmx *vmx, u32 msr_index, u64 data)
3227 switch (msr_index) {
3228 case MSR_IA32_VMX_TRUE_PINBASED_CTLS:
3229 lowp = &vmx->nested.msrs.pinbased_ctls_low;
3230 highp = &vmx->nested.msrs.pinbased_ctls_high;
3232 case MSR_IA32_VMX_TRUE_PROCBASED_CTLS:
3233 lowp = &vmx->nested.msrs.procbased_ctls_low;
3234 highp = &vmx->nested.msrs.procbased_ctls_high;
3236 case MSR_IA32_VMX_TRUE_EXIT_CTLS:
3237 lowp = &vmx->nested.msrs.exit_ctls_low;
3238 highp = &vmx->nested.msrs.exit_ctls_high;
3240 case MSR_IA32_VMX_TRUE_ENTRY_CTLS:
3241 lowp = &vmx->nested.msrs.entry_ctls_low;
3242 highp = &vmx->nested.msrs.entry_ctls_high;
3244 case MSR_IA32_VMX_PROCBASED_CTLS2:
3245 lowp = &vmx->nested.msrs.secondary_ctls_low;
3246 highp = &vmx->nested.msrs.secondary_ctls_high;
3252 supported = vmx_control_msr(*lowp, *highp);
3254 /* Check must-be-1 bits are still 1. */
3255 if (!is_bitwise_subset(data, supported, GENMASK_ULL(31, 0)))
3258 /* Check must-be-0 bits are still 0. */
3259 if (!is_bitwise_subset(supported, data, GENMASK_ULL(63, 32)))
3263 *highp = data >> 32;
3267 static int vmx_restore_vmx_misc(struct vcpu_vmx *vmx, u64 data)
3269 const u64 feature_and_reserved_bits =
3271 BIT_ULL(5) | GENMASK_ULL(8, 6) | BIT_ULL(14) | BIT_ULL(15) |
3272 BIT_ULL(28) | BIT_ULL(29) | BIT_ULL(30) |
3274 GENMASK_ULL(13, 9) | BIT_ULL(31);
3277 vmx_misc = vmx_control_msr(vmx->nested.msrs.misc_low,
3278 vmx->nested.msrs.misc_high);
3280 if (!is_bitwise_subset(vmx_misc, data, feature_and_reserved_bits))
3283 if ((vmx->nested.msrs.pinbased_ctls_high &
3284 PIN_BASED_VMX_PREEMPTION_TIMER) &&
3285 vmx_misc_preemption_timer_rate(data) !=
3286 vmx_misc_preemption_timer_rate(vmx_misc))
3289 if (vmx_misc_cr3_count(data) > vmx_misc_cr3_count(vmx_misc))
3292 if (vmx_misc_max_msr(data) > vmx_misc_max_msr(vmx_misc))
3295 if (vmx_misc_mseg_revid(data) != vmx_misc_mseg_revid(vmx_misc))
3298 vmx->nested.msrs.misc_low = data;
3299 vmx->nested.msrs.misc_high = data >> 32;
3303 static int vmx_restore_vmx_ept_vpid_cap(struct vcpu_vmx *vmx, u64 data)
3305 u64 vmx_ept_vpid_cap;
3307 vmx_ept_vpid_cap = vmx_control_msr(vmx->nested.msrs.ept_caps,
3308 vmx->nested.msrs.vpid_caps);
3310 /* Every bit is either reserved or a feature bit. */
3311 if (!is_bitwise_subset(vmx_ept_vpid_cap, data, -1ULL))
3314 vmx->nested.msrs.ept_caps = data;
3315 vmx->nested.msrs.vpid_caps = data >> 32;
3319 static int vmx_restore_fixed0_msr(struct vcpu_vmx *vmx, u32 msr_index, u64 data)
3323 switch (msr_index) {
3324 case MSR_IA32_VMX_CR0_FIXED0:
3325 msr = &vmx->nested.msrs.cr0_fixed0;
3327 case MSR_IA32_VMX_CR4_FIXED0:
3328 msr = &vmx->nested.msrs.cr4_fixed0;
3335 * 1 bits (which indicates bits which "must-be-1" during VMX operation)
3336 * must be 1 in the restored value.
3338 if (!is_bitwise_subset(data, *msr, -1ULL))
3346 * Called when userspace is restoring VMX MSRs.
3348 * Returns 0 on success, non-0 otherwise.
3350 static int vmx_set_vmx_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 data)
3352 struct vcpu_vmx *vmx = to_vmx(vcpu);
3354 switch (msr_index) {
3355 case MSR_IA32_VMX_BASIC:
3356 return vmx_restore_vmx_basic(vmx, data);
3357 case MSR_IA32_VMX_PINBASED_CTLS:
3358 case MSR_IA32_VMX_PROCBASED_CTLS:
3359 case MSR_IA32_VMX_EXIT_CTLS:
3360 case MSR_IA32_VMX_ENTRY_CTLS:
3362 * The "non-true" VMX capability MSRs are generated from the
3363 * "true" MSRs, so we do not support restoring them directly.
3365 * If userspace wants to emulate VMX_BASIC[55]=0, userspace
3366 * should restore the "true" MSRs with the must-be-1 bits
3367 * set according to the SDM Vol 3. A.2 "RESERVED CONTROLS AND
3368 * DEFAULT SETTINGS".
3371 case MSR_IA32_VMX_TRUE_PINBASED_CTLS:
3372 case MSR_IA32_VMX_TRUE_PROCBASED_CTLS:
3373 case MSR_IA32_VMX_TRUE_EXIT_CTLS:
3374 case MSR_IA32_VMX_TRUE_ENTRY_CTLS:
3375 case MSR_IA32_VMX_PROCBASED_CTLS2:
3376 return vmx_restore_control_msr(vmx, msr_index, data);
3377 case MSR_IA32_VMX_MISC:
3378 return vmx_restore_vmx_misc(vmx, data);
3379 case MSR_IA32_VMX_CR0_FIXED0:
3380 case MSR_IA32_VMX_CR4_FIXED0:
3381 return vmx_restore_fixed0_msr(vmx, msr_index, data);
3382 case MSR_IA32_VMX_CR0_FIXED1:
3383 case MSR_IA32_VMX_CR4_FIXED1:
3385 * These MSRs are generated based on the vCPU's CPUID, so we
3386 * do not support restoring them directly.
3389 case MSR_IA32_VMX_EPT_VPID_CAP:
3390 return vmx_restore_vmx_ept_vpid_cap(vmx, data);
3391 case MSR_IA32_VMX_VMCS_ENUM:
3392 vmx->nested.msrs.vmcs_enum = data;
3396 * The rest of the VMX capability MSRs do not support restore.
3402 /* Returns 0 on success, non-0 otherwise. */
3403 static int vmx_get_vmx_msr(struct nested_vmx_msrs *msrs, u32 msr_index, u64 *pdata)
3405 switch (msr_index) {
3406 case MSR_IA32_VMX_BASIC:
3407 *pdata = msrs->basic;
3409 case MSR_IA32_VMX_TRUE_PINBASED_CTLS:
3410 case MSR_IA32_VMX_PINBASED_CTLS:
3411 *pdata = vmx_control_msr(
3412 msrs->pinbased_ctls_low,
3413 msrs->pinbased_ctls_high);
3414 if (msr_index == MSR_IA32_VMX_PINBASED_CTLS)
3415 *pdata |= PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
3417 case MSR_IA32_VMX_TRUE_PROCBASED_CTLS:
3418 case MSR_IA32_VMX_PROCBASED_CTLS:
3419 *pdata = vmx_control_msr(
3420 msrs->procbased_ctls_low,
3421 msrs->procbased_ctls_high);
3422 if (msr_index == MSR_IA32_VMX_PROCBASED_CTLS)
3423 *pdata |= CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
3425 case MSR_IA32_VMX_TRUE_EXIT_CTLS:
3426 case MSR_IA32_VMX_EXIT_CTLS:
3427 *pdata = vmx_control_msr(
3428 msrs->exit_ctls_low,
3429 msrs->exit_ctls_high);
3430 if (msr_index == MSR_IA32_VMX_EXIT_CTLS)
3431 *pdata |= VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR;
3433 case MSR_IA32_VMX_TRUE_ENTRY_CTLS:
3434 case MSR_IA32_VMX_ENTRY_CTLS:
3435 *pdata = vmx_control_msr(
3436 msrs->entry_ctls_low,
3437 msrs->entry_ctls_high);
3438 if (msr_index == MSR_IA32_VMX_ENTRY_CTLS)
3439 *pdata |= VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR;
3441 case MSR_IA32_VMX_MISC:
3442 *pdata = vmx_control_msr(
3446 case MSR_IA32_VMX_CR0_FIXED0:
3447 *pdata = msrs->cr0_fixed0;
3449 case MSR_IA32_VMX_CR0_FIXED1:
3450 *pdata = msrs->cr0_fixed1;
3452 case MSR_IA32_VMX_CR4_FIXED0:
3453 *pdata = msrs->cr4_fixed0;
3455 case MSR_IA32_VMX_CR4_FIXED1:
3456 *pdata = msrs->cr4_fixed1;
3458 case MSR_IA32_VMX_VMCS_ENUM:
3459 *pdata = msrs->vmcs_enum;
3461 case MSR_IA32_VMX_PROCBASED_CTLS2:
3462 *pdata = vmx_control_msr(
3463 msrs->secondary_ctls_low,
3464 msrs->secondary_ctls_high);
3466 case MSR_IA32_VMX_EPT_VPID_CAP:
3467 *pdata = msrs->ept_caps |
3468 ((u64)msrs->vpid_caps << 32);
3470 case MSR_IA32_VMX_VMFUNC:
3471 *pdata = msrs->vmfunc_controls;
3480 static inline bool vmx_feature_control_msr_valid(struct kvm_vcpu *vcpu,
3483 uint64_t valid_bits = to_vmx(vcpu)->msr_ia32_feature_control_valid_bits;
3485 return !(val & ~valid_bits);
3488 static int vmx_get_msr_feature(struct kvm_msr_entry *msr)
3490 switch (msr->index) {
3491 case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC:
3494 return vmx_get_vmx_msr(&vmcs_config.nested, msr->index, &msr->data);
3503 * Reads an msr value (of 'msr_index') into 'pdata'.
3504 * Returns 0 on success, non-0 otherwise.
3505 * Assumes vcpu_load() was already called.
3507 static int vmx_get_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
3509 struct vcpu_vmx *vmx = to_vmx(vcpu);
3510 struct shared_msr_entry *msr;
3512 switch (msr_info->index) {
3513 #ifdef CONFIG_X86_64
3515 msr_info->data = vmcs_readl(GUEST_FS_BASE);
3518 msr_info->data = vmcs_readl(GUEST_GS_BASE);
3520 case MSR_KERNEL_GS_BASE:
3521 vmx_load_host_state(vmx);
3522 msr_info->data = vmx->msr_guest_kernel_gs_base;
3526 return kvm_get_msr_common(vcpu, msr_info);
3528 msr_info->data = guest_read_tsc(vcpu);
3530 case MSR_IA32_SPEC_CTRL:
3531 if (!msr_info->host_initiated &&
3532 !guest_cpuid_has(vcpu, X86_FEATURE_IBRS) &&
3533 !guest_cpuid_has(vcpu, X86_FEATURE_SPEC_CTRL))
3536 msr_info->data = to_vmx(vcpu)->spec_ctrl;
3538 case MSR_IA32_ARCH_CAPABILITIES:
3539 if (!msr_info->host_initiated &&
3540 !guest_cpuid_has(vcpu, X86_FEATURE_ARCH_CAPABILITIES))
3542 msr_info->data = to_vmx(vcpu)->arch_capabilities;
3544 case MSR_IA32_SYSENTER_CS:
3545 msr_info->data = vmcs_read32(GUEST_SYSENTER_CS);
3547 case MSR_IA32_SYSENTER_EIP:
3548 msr_info->data = vmcs_readl(GUEST_SYSENTER_EIP);
3550 case MSR_IA32_SYSENTER_ESP:
3551 msr_info->data = vmcs_readl(GUEST_SYSENTER_ESP);
3553 case MSR_IA32_BNDCFGS:
3554 if (!kvm_mpx_supported() ||
3555 (!msr_info->host_initiated &&
3556 !guest_cpuid_has(vcpu, X86_FEATURE_MPX)))
3558 msr_info->data = vmcs_read64(GUEST_BNDCFGS);
3560 case MSR_IA32_MCG_EXT_CTL:
3561 if (!msr_info->host_initiated &&
3562 !(vmx->msr_ia32_feature_control &
3563 FEATURE_CONTROL_LMCE))
3565 msr_info->data = vcpu->arch.mcg_ext_ctl;
3567 case MSR_IA32_FEATURE_CONTROL:
3568 msr_info->data = vmx->msr_ia32_feature_control;
3570 case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC:
3571 if (!nested_vmx_allowed(vcpu))
3573 return vmx_get_vmx_msr(&vmx->nested.msrs, msr_info->index,
3576 if (!vmx_xsaves_supported())
3578 msr_info->data = vcpu->arch.ia32_xss;
3581 if (!msr_info->host_initiated &&
3582 !guest_cpuid_has(vcpu, X86_FEATURE_RDTSCP))
3584 /* Otherwise falls through */
3586 msr = find_msr_entry(vmx, msr_info->index);
3588 msr_info->data = msr->data;
3591 return kvm_get_msr_common(vcpu, msr_info);
3597 static void vmx_leave_nested(struct kvm_vcpu *vcpu);
3600 * Writes msr value into into the appropriate "register".
3601 * Returns 0 on success, non-0 otherwise.
3602 * Assumes vcpu_load() was already called.
3604 static int vmx_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
3606 struct vcpu_vmx *vmx = to_vmx(vcpu);
3607 struct shared_msr_entry *msr;
3609 u32 msr_index = msr_info->index;
3610 u64 data = msr_info->data;
3612 switch (msr_index) {
3614 ret = kvm_set_msr_common(vcpu, msr_info);
3616 #ifdef CONFIG_X86_64
3618 vmx_segment_cache_clear(vmx);
3619 vmcs_writel(GUEST_FS_BASE, data);
3622 vmx_segment_cache_clear(vmx);
3623 vmcs_writel(GUEST_GS_BASE, data);
3625 case MSR_KERNEL_GS_BASE:
3626 vmx_load_host_state(vmx);
3627 vmx->msr_guest_kernel_gs_base = data;
3630 case MSR_IA32_SYSENTER_CS:
3631 vmcs_write32(GUEST_SYSENTER_CS, data);
3633 case MSR_IA32_SYSENTER_EIP:
3634 vmcs_writel(GUEST_SYSENTER_EIP, data);
3636 case MSR_IA32_SYSENTER_ESP:
3637 vmcs_writel(GUEST_SYSENTER_ESP, data);
3639 case MSR_IA32_BNDCFGS:
3640 if (!kvm_mpx_supported() ||
3641 (!msr_info->host_initiated &&
3642 !guest_cpuid_has(vcpu, X86_FEATURE_MPX)))
3644 if (is_noncanonical_address(data & PAGE_MASK, vcpu) ||
3645 (data & MSR_IA32_BNDCFGS_RSVD))
3647 vmcs_write64(GUEST_BNDCFGS, data);
3650 kvm_write_tsc(vcpu, msr_info);
3652 case MSR_IA32_SPEC_CTRL:
3653 if (!msr_info->host_initiated &&
3654 !guest_cpuid_has(vcpu, X86_FEATURE_IBRS) &&
3655 !guest_cpuid_has(vcpu, X86_FEATURE_SPEC_CTRL))
3658 /* The STIBP bit doesn't fault even if it's not advertised */
3659 if (data & ~(SPEC_CTRL_IBRS | SPEC_CTRL_STIBP))
3662 vmx->spec_ctrl = data;
3669 * When it's written (to non-zero) for the first time, pass
3673 * The handling of the MSR bitmap for L2 guests is done in
3674 * nested_vmx_merge_msr_bitmap. We should not touch the
3675 * vmcs02.msr_bitmap here since it gets completely overwritten
3676 * in the merging. We update the vmcs01 here for L1 as well
3677 * since it will end up touching the MSR anyway now.
3679 vmx_disable_intercept_for_msr(vmx->vmcs01.msr_bitmap,
3683 case MSR_IA32_PRED_CMD:
3684 if (!msr_info->host_initiated &&
3685 !guest_cpuid_has(vcpu, X86_FEATURE_IBPB) &&
3686 !guest_cpuid_has(vcpu, X86_FEATURE_SPEC_CTRL))
3689 if (data & ~PRED_CMD_IBPB)
3695 wrmsrl(MSR_IA32_PRED_CMD, PRED_CMD_IBPB);
3699 * When it's written (to non-zero) for the first time, pass
3703 * The handling of the MSR bitmap for L2 guests is done in
3704 * nested_vmx_merge_msr_bitmap. We should not touch the
3705 * vmcs02.msr_bitmap here since it gets completely overwritten
3708 vmx_disable_intercept_for_msr(vmx->vmcs01.msr_bitmap, MSR_IA32_PRED_CMD,
3711 case MSR_IA32_ARCH_CAPABILITIES:
3712 if (!msr_info->host_initiated)
3714 vmx->arch_capabilities = data;
3716 case MSR_IA32_CR_PAT:
3717 if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) {
3718 if (!kvm_mtrr_valid(vcpu, MSR_IA32_CR_PAT, data))
3720 vmcs_write64(GUEST_IA32_PAT, data);
3721 vcpu->arch.pat = data;
3724 ret = kvm_set_msr_common(vcpu, msr_info);
3726 case MSR_IA32_TSC_ADJUST:
3727 ret = kvm_set_msr_common(vcpu, msr_info);
3729 case MSR_IA32_MCG_EXT_CTL:
3730 if ((!msr_info->host_initiated &&
3731 !(to_vmx(vcpu)->msr_ia32_feature_control &
3732 FEATURE_CONTROL_LMCE)) ||
3733 (data & ~MCG_EXT_CTL_LMCE_EN))
3735 vcpu->arch.mcg_ext_ctl = data;
3737 case MSR_IA32_FEATURE_CONTROL:
3738 if (!vmx_feature_control_msr_valid(vcpu, data) ||
3739 (to_vmx(vcpu)->msr_ia32_feature_control &
3740 FEATURE_CONTROL_LOCKED && !msr_info->host_initiated))
3742 vmx->msr_ia32_feature_control = data;
3743 if (msr_info->host_initiated && data == 0)
3744 vmx_leave_nested(vcpu);
3746 case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC:
3747 if (!msr_info->host_initiated)
3748 return 1; /* they are read-only */
3749 if (!nested_vmx_allowed(vcpu))
3751 return vmx_set_vmx_msr(vcpu, msr_index, data);
3753 if (!vmx_xsaves_supported())
3756 * The only supported bit as of Skylake is bit 8, but
3757 * it is not supported on KVM.
3761 vcpu->arch.ia32_xss = data;
3762 if (vcpu->arch.ia32_xss != host_xss)
3763 add_atomic_switch_msr(vmx, MSR_IA32_XSS,
3764 vcpu->arch.ia32_xss, host_xss);
3766 clear_atomic_switch_msr(vmx, MSR_IA32_XSS);
3769 if (!msr_info->host_initiated &&
3770 !guest_cpuid_has(vcpu, X86_FEATURE_RDTSCP))
3772 /* Check reserved bit, higher 32 bits should be zero */
3773 if ((data >> 32) != 0)
3775 /* Otherwise falls through */
3777 msr = find_msr_entry(vmx, msr_index);
3779 u64 old_msr_data = msr->data;
3781 if (msr - vmx->guest_msrs < vmx->save_nmsrs) {
3783 ret = kvm_set_shared_msr(msr->index, msr->data,
3787 msr->data = old_msr_data;
3791 ret = kvm_set_msr_common(vcpu, msr_info);
3797 static void vmx_cache_reg(struct kvm_vcpu *vcpu, enum kvm_reg reg)
3799 __set_bit(reg, (unsigned long *)&vcpu->arch.regs_avail);
3802 vcpu->arch.regs[VCPU_REGS_RSP] = vmcs_readl(GUEST_RSP);
3805 vcpu->arch.regs[VCPU_REGS_RIP] = vmcs_readl(GUEST_RIP);
3807 case VCPU_EXREG_PDPTR:
3809 ept_save_pdptrs(vcpu);
3816 static __init int cpu_has_kvm_support(void)
3818 return cpu_has_vmx();
3821 static __init int vmx_disabled_by_bios(void)
3825 rdmsrl(MSR_IA32_FEATURE_CONTROL, msr);
3826 if (msr & FEATURE_CONTROL_LOCKED) {
3827 /* launched w/ TXT and VMX disabled */
3828 if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX)
3831 /* launched w/o TXT and VMX only enabled w/ TXT */
3832 if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX)
3833 && (msr & FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX)
3834 && !tboot_enabled()) {
3835 printk(KERN_WARNING "kvm: disable TXT in the BIOS or "
3836 "activate TXT before enabling KVM\n");
3839 /* launched w/o TXT and VMX disabled */
3840 if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX)
3841 && !tboot_enabled())
3848 static void kvm_cpu_vmxon(u64 addr)
3850 cr4_set_bits(X86_CR4_VMXE);
3851 intel_pt_handle_vmx(1);
3853 asm volatile (ASM_VMX_VMXON_RAX
3854 : : "a"(&addr), "m"(addr)
3858 static int hardware_enable(void)
3860 int cpu = raw_smp_processor_id();
3861 u64 phys_addr = __pa(per_cpu(vmxarea, cpu));
3864 if (cr4_read_shadow() & X86_CR4_VMXE)
3868 * This can happen if we hot-added a CPU but failed to allocate
3869 * VP assist page for it.
3871 if (static_branch_unlikely(&enable_evmcs) &&
3872 !hv_get_vp_assist_page(cpu))
3875 INIT_LIST_HEAD(&per_cpu(loaded_vmcss_on_cpu, cpu));
3876 INIT_LIST_HEAD(&per_cpu(blocked_vcpu_on_cpu, cpu));
3877 spin_lock_init(&per_cpu(blocked_vcpu_on_cpu_lock, cpu));
3880 * Now we can enable the vmclear operation in kdump
3881 * since the loaded_vmcss_on_cpu list on this cpu
3882 * has been initialized.
3884 * Though the cpu is not in VMX operation now, there
3885 * is no problem to enable the vmclear operation
3886 * for the loaded_vmcss_on_cpu list is empty!
3888 crash_enable_local_vmclear(cpu);
3890 rdmsrl(MSR_IA32_FEATURE_CONTROL, old);
3892 test_bits = FEATURE_CONTROL_LOCKED;
3893 test_bits |= FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
3894 if (tboot_enabled())
3895 test_bits |= FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX;
3897 if ((old & test_bits) != test_bits) {
3898 /* enable and lock */
3899 wrmsrl(MSR_IA32_FEATURE_CONTROL, old | test_bits);
3901 kvm_cpu_vmxon(phys_addr);
3908 static void vmclear_local_loaded_vmcss(void)
3910 int cpu = raw_smp_processor_id();
3911 struct loaded_vmcs *v, *n;
3913 list_for_each_entry_safe(v, n, &per_cpu(loaded_vmcss_on_cpu, cpu),
3914 loaded_vmcss_on_cpu_link)
3915 __loaded_vmcs_clear(v);
3919 /* Just like cpu_vmxoff(), but with the __kvm_handle_fault_on_reboot()
3922 static void kvm_cpu_vmxoff(void)
3924 asm volatile (__ex(ASM_VMX_VMXOFF) : : : "cc");
3926 intel_pt_handle_vmx(0);
3927 cr4_clear_bits(X86_CR4_VMXE);
3930 static void hardware_disable(void)
3932 vmclear_local_loaded_vmcss();
3936 static __init int adjust_vmx_controls(u32 ctl_min, u32 ctl_opt,
3937 u32 msr, u32 *result)
3939 u32 vmx_msr_low, vmx_msr_high;
3940 u32 ctl = ctl_min | ctl_opt;
3942 rdmsr(msr, vmx_msr_low, vmx_msr_high);
3944 ctl &= vmx_msr_high; /* bit == 0 in high word ==> must be zero */
3945 ctl |= vmx_msr_low; /* bit == 1 in low word ==> must be one */
3947 /* Ensure minimum (required) set of control bits are supported. */
3955 static __init bool allow_1_setting(u32 msr, u32 ctl)
3957 u32 vmx_msr_low, vmx_msr_high;
3959 rdmsr(msr, vmx_msr_low, vmx_msr_high);
3960 return vmx_msr_high & ctl;
3963 static __init int setup_vmcs_config(struct vmcs_config *vmcs_conf)
3965 u32 vmx_msr_low, vmx_msr_high;
3966 u32 min, opt, min2, opt2;
3967 u32 _pin_based_exec_control = 0;
3968 u32 _cpu_based_exec_control = 0;
3969 u32 _cpu_based_2nd_exec_control = 0;
3970 u32 _vmexit_control = 0;
3971 u32 _vmentry_control = 0;
3973 memset(vmcs_conf, 0, sizeof(*vmcs_conf));
3974 min = CPU_BASED_HLT_EXITING |
3975 #ifdef CONFIG_X86_64
3976 CPU_BASED_CR8_LOAD_EXITING |
3977 CPU_BASED_CR8_STORE_EXITING |
3979 CPU_BASED_CR3_LOAD_EXITING |
3980 CPU_BASED_CR3_STORE_EXITING |
3981 CPU_BASED_UNCOND_IO_EXITING |
3982 CPU_BASED_MOV_DR_EXITING |
3983 CPU_BASED_USE_TSC_OFFSETING |
3984 CPU_BASED_MWAIT_EXITING |
3985 CPU_BASED_MONITOR_EXITING |
3986 CPU_BASED_INVLPG_EXITING |
3987 CPU_BASED_RDPMC_EXITING;
3989 opt = CPU_BASED_TPR_SHADOW |
3990 CPU_BASED_USE_MSR_BITMAPS |
3991 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
3992 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PROCBASED_CTLS,
3993 &_cpu_based_exec_control) < 0)
3995 #ifdef CONFIG_X86_64
3996 if ((_cpu_based_exec_control & CPU_BASED_TPR_SHADOW))
3997 _cpu_based_exec_control &= ~CPU_BASED_CR8_LOAD_EXITING &
3998 ~CPU_BASED_CR8_STORE_EXITING;
4000 if (_cpu_based_exec_control & CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) {
4002 opt2 = SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
4003 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
4004 SECONDARY_EXEC_WBINVD_EXITING |
4005 SECONDARY_EXEC_ENABLE_VPID |
4006 SECONDARY_EXEC_ENABLE_EPT |
4007 SECONDARY_EXEC_UNRESTRICTED_GUEST |
4008 SECONDARY_EXEC_PAUSE_LOOP_EXITING |
4009 SECONDARY_EXEC_DESC |
4010 SECONDARY_EXEC_RDTSCP |
4011 SECONDARY_EXEC_ENABLE_INVPCID |
4012 SECONDARY_EXEC_APIC_REGISTER_VIRT |
4013 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY |
4014 SECONDARY_EXEC_SHADOW_VMCS |
4015 SECONDARY_EXEC_XSAVES |
4016 SECONDARY_EXEC_RDSEED_EXITING |
4017 SECONDARY_EXEC_RDRAND_EXITING |
4018 SECONDARY_EXEC_ENABLE_PML |
4019 SECONDARY_EXEC_TSC_SCALING |
4020 SECONDARY_EXEC_ENABLE_VMFUNC;
4021 if (adjust_vmx_controls(min2, opt2,
4022 MSR_IA32_VMX_PROCBASED_CTLS2,
4023 &_cpu_based_2nd_exec_control) < 0)
4026 #ifndef CONFIG_X86_64
4027 if (!(_cpu_based_2nd_exec_control &
4028 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES))
4029 _cpu_based_exec_control &= ~CPU_BASED_TPR_SHADOW;
4032 if (!(_cpu_based_exec_control & CPU_BASED_TPR_SHADOW))
4033 _cpu_based_2nd_exec_control &= ~(
4034 SECONDARY_EXEC_APIC_REGISTER_VIRT |
4035 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
4036 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
4038 rdmsr_safe(MSR_IA32_VMX_EPT_VPID_CAP,
4039 &vmx_capability.ept, &vmx_capability.vpid);
4041 if (_cpu_based_2nd_exec_control & SECONDARY_EXEC_ENABLE_EPT) {
4042 /* CR3 accesses and invlpg don't need to cause VM Exits when EPT
4044 _cpu_based_exec_control &= ~(CPU_BASED_CR3_LOAD_EXITING |
4045 CPU_BASED_CR3_STORE_EXITING |
4046 CPU_BASED_INVLPG_EXITING);
4047 } else if (vmx_capability.ept) {
4048 vmx_capability.ept = 0;
4049 pr_warn_once("EPT CAP should not exist if not support "
4050 "1-setting enable EPT VM-execution control\n");
4052 if (!(_cpu_based_2nd_exec_control & SECONDARY_EXEC_ENABLE_VPID) &&
4053 vmx_capability.vpid) {
4054 vmx_capability.vpid = 0;
4055 pr_warn_once("VPID CAP should not exist if not support "
4056 "1-setting enable VPID VM-execution control\n");
4059 min = VM_EXIT_SAVE_DEBUG_CONTROLS | VM_EXIT_ACK_INTR_ON_EXIT;
4060 #ifdef CONFIG_X86_64
4061 min |= VM_EXIT_HOST_ADDR_SPACE_SIZE;
4063 opt = VM_EXIT_SAVE_IA32_PAT | VM_EXIT_LOAD_IA32_PAT |
4064 VM_EXIT_CLEAR_BNDCFGS;
4065 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_EXIT_CTLS,
4066 &_vmexit_control) < 0)
4069 min = PIN_BASED_EXT_INTR_MASK | PIN_BASED_NMI_EXITING;
4070 opt = PIN_BASED_VIRTUAL_NMIS | PIN_BASED_POSTED_INTR |
4071 PIN_BASED_VMX_PREEMPTION_TIMER;
4072 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PINBASED_CTLS,
4073 &_pin_based_exec_control) < 0)
4076 if (cpu_has_broken_vmx_preemption_timer())
4077 _pin_based_exec_control &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
4078 if (!(_cpu_based_2nd_exec_control &
4079 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY))
4080 _pin_based_exec_control &= ~PIN_BASED_POSTED_INTR;
4082 min = VM_ENTRY_LOAD_DEBUG_CONTROLS;
4083 opt = VM_ENTRY_LOAD_IA32_PAT | VM_ENTRY_LOAD_BNDCFGS;
4084 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_ENTRY_CTLS,
4085 &_vmentry_control) < 0)
4088 rdmsr(MSR_IA32_VMX_BASIC, vmx_msr_low, vmx_msr_high);
4090 /* IA-32 SDM Vol 3B: VMCS size is never greater than 4kB. */
4091 if ((vmx_msr_high & 0x1fff) > PAGE_SIZE)
4094 #ifdef CONFIG_X86_64
4095 /* IA-32 SDM Vol 3B: 64-bit CPUs always have VMX_BASIC_MSR[48]==0. */
4096 if (vmx_msr_high & (1u<<16))
4100 /* Require Write-Back (WB) memory type for VMCS accesses. */
4101 if (((vmx_msr_high >> 18) & 15) != 6)
4104 vmcs_conf->size = vmx_msr_high & 0x1fff;
4105 vmcs_conf->order = get_order(vmcs_conf->size);
4106 vmcs_conf->basic_cap = vmx_msr_high & ~0x1fff;
4108 /* KVM supports Enlightened VMCS v1 only */
4109 if (static_branch_unlikely(&enable_evmcs))
4110 vmcs_conf->revision_id = KVM_EVMCS_VERSION;
4112 vmcs_conf->revision_id = vmx_msr_low;
4114 vmcs_conf->pin_based_exec_ctrl = _pin_based_exec_control;
4115 vmcs_conf->cpu_based_exec_ctrl = _cpu_based_exec_control;
4116 vmcs_conf->cpu_based_2nd_exec_ctrl = _cpu_based_2nd_exec_control;
4117 vmcs_conf->vmexit_ctrl = _vmexit_control;
4118 vmcs_conf->vmentry_ctrl = _vmentry_control;
4120 if (static_branch_unlikely(&enable_evmcs))
4121 evmcs_sanitize_exec_ctrls(vmcs_conf);
4123 cpu_has_load_ia32_efer =
4124 allow_1_setting(MSR_IA32_VMX_ENTRY_CTLS,
4125 VM_ENTRY_LOAD_IA32_EFER)
4126 && allow_1_setting(MSR_IA32_VMX_EXIT_CTLS,
4127 VM_EXIT_LOAD_IA32_EFER);
4129 cpu_has_load_perf_global_ctrl =
4130 allow_1_setting(MSR_IA32_VMX_ENTRY_CTLS,
4131 VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL)
4132 && allow_1_setting(MSR_IA32_VMX_EXIT_CTLS,
4133 VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL);
4136 * Some cpus support VM_ENTRY_(LOAD|SAVE)_IA32_PERF_GLOBAL_CTRL
4137 * but due to errata below it can't be used. Workaround is to use
4138 * msr load mechanism to switch IA32_PERF_GLOBAL_CTRL.
4140 * VM Exit May Incorrectly Clear IA32_PERF_GLOBAL_CTRL [34:32]
4145 * BC86,AAY89,BD102 (model 44)
4149 if (cpu_has_load_perf_global_ctrl && boot_cpu_data.x86 == 0x6) {
4150 switch (boot_cpu_data.x86_model) {
4156 cpu_has_load_perf_global_ctrl = false;
4157 printk_once(KERN_WARNING"kvm: VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL "
4158 "does not work properly. Using workaround\n");
4165 if (boot_cpu_has(X86_FEATURE_XSAVES))
4166 rdmsrl(MSR_IA32_XSS, host_xss);
4171 static struct vmcs *alloc_vmcs_cpu(int cpu)
4173 int node = cpu_to_node(cpu);
4177 pages = __alloc_pages_node(node, GFP_KERNEL, vmcs_config.order);
4180 vmcs = page_address(pages);
4181 memset(vmcs, 0, vmcs_config.size);
4182 vmcs->revision_id = vmcs_config.revision_id; /* vmcs revision id */
4186 static void free_vmcs(struct vmcs *vmcs)
4188 free_pages((unsigned long)vmcs, vmcs_config.order);
4192 * Free a VMCS, but before that VMCLEAR it on the CPU where it was last loaded
4194 static void free_loaded_vmcs(struct loaded_vmcs *loaded_vmcs)
4196 if (!loaded_vmcs->vmcs)
4198 loaded_vmcs_clear(loaded_vmcs);
4199 free_vmcs(loaded_vmcs->vmcs);
4200 loaded_vmcs->vmcs = NULL;
4201 if (loaded_vmcs->msr_bitmap)
4202 free_page((unsigned long)loaded_vmcs->msr_bitmap);
4203 WARN_ON(loaded_vmcs->shadow_vmcs != NULL);
4206 static struct vmcs *alloc_vmcs(void)
4208 return alloc_vmcs_cpu(raw_smp_processor_id());
4211 static int alloc_loaded_vmcs(struct loaded_vmcs *loaded_vmcs)
4213 loaded_vmcs->vmcs = alloc_vmcs();
4214 if (!loaded_vmcs->vmcs)
4217 loaded_vmcs->shadow_vmcs = NULL;
4218 loaded_vmcs_init(loaded_vmcs);
4220 if (cpu_has_vmx_msr_bitmap()) {
4221 loaded_vmcs->msr_bitmap = (unsigned long *)__get_free_page(GFP_KERNEL);
4222 if (!loaded_vmcs->msr_bitmap)
4224 memset(loaded_vmcs->msr_bitmap, 0xff, PAGE_SIZE);
4229 free_loaded_vmcs(loaded_vmcs);
4233 static void free_kvm_area(void)
4237 for_each_possible_cpu(cpu) {
4238 free_vmcs(per_cpu(vmxarea, cpu));
4239 per_cpu(vmxarea, cpu) = NULL;
4243 enum vmcs_field_width {
4244 VMCS_FIELD_WIDTH_U16 = 0,
4245 VMCS_FIELD_WIDTH_U64 = 1,
4246 VMCS_FIELD_WIDTH_U32 = 2,
4247 VMCS_FIELD_WIDTH_NATURAL_WIDTH = 3
4250 static inline int vmcs_field_width(unsigned long field)
4252 if (0x1 & field) /* the *_HIGH fields are all 32 bit */
4253 return VMCS_FIELD_WIDTH_U32;
4254 return (field >> 13) & 0x3 ;
4257 static inline int vmcs_field_readonly(unsigned long field)
4259 return (((field >> 10) & 0x3) == 1);
4262 static void init_vmcs_shadow_fields(void)
4266 for (i = j = 0; i < max_shadow_read_only_fields; i++) {
4267 u16 field = shadow_read_only_fields[i];
4268 if (vmcs_field_width(field) == VMCS_FIELD_WIDTH_U64 &&
4269 (i + 1 == max_shadow_read_only_fields ||
4270 shadow_read_only_fields[i + 1] != field + 1))
4271 pr_err("Missing field from shadow_read_only_field %x\n",
4274 clear_bit(field, vmx_vmread_bitmap);
4275 #ifdef CONFIG_X86_64
4280 shadow_read_only_fields[j] = field;
4283 max_shadow_read_only_fields = j;
4285 for (i = j = 0; i < max_shadow_read_write_fields; i++) {
4286 u16 field = shadow_read_write_fields[i];
4287 if (vmcs_field_width(field) == VMCS_FIELD_WIDTH_U64 &&
4288 (i + 1 == max_shadow_read_write_fields ||
4289 shadow_read_write_fields[i + 1] != field + 1))
4290 pr_err("Missing field from shadow_read_write_field %x\n",
4294 * PML and the preemption timer can be emulated, but the
4295 * processor cannot vmwrite to fields that don't exist
4299 case GUEST_PML_INDEX:
4300 if (!cpu_has_vmx_pml())
4303 case VMX_PREEMPTION_TIMER_VALUE:
4304 if (!cpu_has_vmx_preemption_timer())
4307 case GUEST_INTR_STATUS:
4308 if (!cpu_has_vmx_apicv())
4315 clear_bit(field, vmx_vmwrite_bitmap);
4316 clear_bit(field, vmx_vmread_bitmap);
4317 #ifdef CONFIG_X86_64
4322 shadow_read_write_fields[j] = field;
4325 max_shadow_read_write_fields = j;
4328 static __init int alloc_kvm_area(void)
4332 for_each_possible_cpu(cpu) {
4335 vmcs = alloc_vmcs_cpu(cpu);
4341 per_cpu(vmxarea, cpu) = vmcs;
4346 static void fix_pmode_seg(struct kvm_vcpu *vcpu, int seg,
4347 struct kvm_segment *save)
4349 if (!emulate_invalid_guest_state) {
4351 * CS and SS RPL should be equal during guest entry according
4352 * to VMX spec, but in reality it is not always so. Since vcpu
4353 * is in the middle of the transition from real mode to
4354 * protected mode it is safe to assume that RPL 0 is a good
4357 if (seg == VCPU_SREG_CS || seg == VCPU_SREG_SS)
4358 save->selector &= ~SEGMENT_RPL_MASK;
4359 save->dpl = save->selector & SEGMENT_RPL_MASK;
4362 vmx_set_segment(vcpu, save, seg);
4365 static void enter_pmode(struct kvm_vcpu *vcpu)
4367 unsigned long flags;
4368 struct vcpu_vmx *vmx = to_vmx(vcpu);
4371 * Update real mode segment cache. It may be not up-to-date if sement
4372 * register was written while vcpu was in a guest mode.
4374 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_ES], VCPU_SREG_ES);
4375 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_DS], VCPU_SREG_DS);
4376 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_FS], VCPU_SREG_FS);
4377 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_GS], VCPU_SREG_GS);
4378 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_SS], VCPU_SREG_SS);
4379 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_CS], VCPU_SREG_CS);
4381 vmx->rmode.vm86_active = 0;
4383 vmx_segment_cache_clear(vmx);
4385 vmx_set_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_TR], VCPU_SREG_TR);
4387 flags = vmcs_readl(GUEST_RFLAGS);
4388 flags &= RMODE_GUEST_OWNED_EFLAGS_BITS;
4389 flags |= vmx->rmode.save_rflags & ~RMODE_GUEST_OWNED_EFLAGS_BITS;
4390 vmcs_writel(GUEST_RFLAGS, flags);
4392 vmcs_writel(GUEST_CR4, (vmcs_readl(GUEST_CR4) & ~X86_CR4_VME) |
4393 (vmcs_readl(CR4_READ_SHADOW) & X86_CR4_VME));
4395 update_exception_bitmap(vcpu);
4397 fix_pmode_seg(vcpu, VCPU_SREG_CS, &vmx->rmode.segs[VCPU_SREG_CS]);
4398 fix_pmode_seg(vcpu, VCPU_SREG_SS, &vmx->rmode.segs[VCPU_SREG_SS]);
4399 fix_pmode_seg(vcpu, VCPU_SREG_ES, &vmx->rmode.segs[VCPU_SREG_ES]);
4400 fix_pmode_seg(vcpu, VCPU_SREG_DS, &vmx->rmode.segs[VCPU_SREG_DS]);
4401 fix_pmode_seg(vcpu, VCPU_SREG_FS, &vmx->rmode.segs[VCPU_SREG_FS]);
4402 fix_pmode_seg(vcpu, VCPU_SREG_GS, &vmx->rmode.segs[VCPU_SREG_GS]);
4405 static void fix_rmode_seg(int seg, struct kvm_segment *save)
4407 const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
4408 struct kvm_segment var = *save;
4411 if (seg == VCPU_SREG_CS)
4414 if (!emulate_invalid_guest_state) {
4415 var.selector = var.base >> 4;
4416 var.base = var.base & 0xffff0;
4426 if (save->base & 0xf)
4427 printk_once(KERN_WARNING "kvm: segment base is not "
4428 "paragraph aligned when entering "
4429 "protected mode (seg=%d)", seg);
4432 vmcs_write16(sf->selector, var.selector);
4433 vmcs_writel(sf->base, var.base);
4434 vmcs_write32(sf->limit, var.limit);
4435 vmcs_write32(sf->ar_bytes, vmx_segment_access_rights(&var));
4438 static void enter_rmode(struct kvm_vcpu *vcpu)
4440 unsigned long flags;
4441 struct vcpu_vmx *vmx = to_vmx(vcpu);
4442 struct kvm_vmx *kvm_vmx = to_kvm_vmx(vcpu->kvm);
4444 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_TR], VCPU_SREG_TR);
4445 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_ES], VCPU_SREG_ES);
4446 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_DS], VCPU_SREG_DS);
4447 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_FS], VCPU_SREG_FS);
4448 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_GS], VCPU_SREG_GS);
4449 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_SS], VCPU_SREG_SS);
4450 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_CS], VCPU_SREG_CS);
4452 vmx->rmode.vm86_active = 1;
4455 * Very old userspace does not call KVM_SET_TSS_ADDR before entering
4456 * vcpu. Warn the user that an update is overdue.
4458 if (!kvm_vmx->tss_addr)
4459 printk_once(KERN_WARNING "kvm: KVM_SET_TSS_ADDR need to be "
4460 "called before entering vcpu\n");
4462 vmx_segment_cache_clear(vmx);
4464 vmcs_writel(GUEST_TR_BASE, kvm_vmx->tss_addr);
4465 vmcs_write32(GUEST_TR_LIMIT, RMODE_TSS_SIZE - 1);
4466 vmcs_write32(GUEST_TR_AR_BYTES, 0x008b);
4468 flags = vmcs_readl(GUEST_RFLAGS);
4469 vmx->rmode.save_rflags = flags;
4471 flags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM;
4473 vmcs_writel(GUEST_RFLAGS, flags);
4474 vmcs_writel(GUEST_CR4, vmcs_readl(GUEST_CR4) | X86_CR4_VME);
4475 update_exception_bitmap(vcpu);
4477 fix_rmode_seg(VCPU_SREG_SS, &vmx->rmode.segs[VCPU_SREG_SS]);
4478 fix_rmode_seg(VCPU_SREG_CS, &vmx->rmode.segs[VCPU_SREG_CS]);
4479 fix_rmode_seg(VCPU_SREG_ES, &vmx->rmode.segs[VCPU_SREG_ES]);
4480 fix_rmode_seg(VCPU_SREG_DS, &vmx->rmode.segs[VCPU_SREG_DS]);
4481 fix_rmode_seg(VCPU_SREG_GS, &vmx->rmode.segs[VCPU_SREG_GS]);
4482 fix_rmode_seg(VCPU_SREG_FS, &vmx->rmode.segs[VCPU_SREG_FS]);
4484 kvm_mmu_reset_context(vcpu);
4487 static void vmx_set_efer(struct kvm_vcpu *vcpu, u64 efer)
4489 struct vcpu_vmx *vmx = to_vmx(vcpu);
4490 struct shared_msr_entry *msr = find_msr_entry(vmx, MSR_EFER);
4496 * Force kernel_gs_base reloading before EFER changes, as control
4497 * of this msr depends on is_long_mode().
4499 vmx_load_host_state(to_vmx(vcpu));
4500 vcpu->arch.efer = efer;
4501 if (efer & EFER_LMA) {
4502 vm_entry_controls_setbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
4505 vm_entry_controls_clearbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
4507 msr->data = efer & ~EFER_LME;
4512 #ifdef CONFIG_X86_64
4514 static void enter_lmode(struct kvm_vcpu *vcpu)
4518 vmx_segment_cache_clear(to_vmx(vcpu));
4520 guest_tr_ar = vmcs_read32(GUEST_TR_AR_BYTES);
4521 if ((guest_tr_ar & VMX_AR_TYPE_MASK) != VMX_AR_TYPE_BUSY_64_TSS) {
4522 pr_debug_ratelimited("%s: tss fixup for long mode. \n",
4524 vmcs_write32(GUEST_TR_AR_BYTES,
4525 (guest_tr_ar & ~VMX_AR_TYPE_MASK)
4526 | VMX_AR_TYPE_BUSY_64_TSS);
4528 vmx_set_efer(vcpu, vcpu->arch.efer | EFER_LMA);
4531 static void exit_lmode(struct kvm_vcpu *vcpu)
4533 vm_entry_controls_clearbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
4534 vmx_set_efer(vcpu, vcpu->arch.efer & ~EFER_LMA);
4539 static inline void __vmx_flush_tlb(struct kvm_vcpu *vcpu, int vpid,
4540 bool invalidate_gpa)
4542 if (enable_ept && (invalidate_gpa || !enable_vpid)) {
4543 if (!VALID_PAGE(vcpu->arch.mmu.root_hpa))
4545 ept_sync_context(construct_eptp(vcpu, vcpu->arch.mmu.root_hpa));
4547 vpid_sync_context(vpid);
4551 static void vmx_flush_tlb(struct kvm_vcpu *vcpu, bool invalidate_gpa)
4553 __vmx_flush_tlb(vcpu, to_vmx(vcpu)->vpid, invalidate_gpa);
4556 static void vmx_flush_tlb_ept_only(struct kvm_vcpu *vcpu)
4559 vmx_flush_tlb(vcpu, true);
4562 static void vmx_decache_cr0_guest_bits(struct kvm_vcpu *vcpu)
4564 ulong cr0_guest_owned_bits = vcpu->arch.cr0_guest_owned_bits;
4566 vcpu->arch.cr0 &= ~cr0_guest_owned_bits;
4567 vcpu->arch.cr0 |= vmcs_readl(GUEST_CR0) & cr0_guest_owned_bits;
4570 static void vmx_decache_cr3(struct kvm_vcpu *vcpu)
4572 if (enable_unrestricted_guest || (enable_ept && is_paging(vcpu)))
4573 vcpu->arch.cr3 = vmcs_readl(GUEST_CR3);
4574 __set_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail);
4577 static void vmx_decache_cr4_guest_bits(struct kvm_vcpu *vcpu)
4579 ulong cr4_guest_owned_bits = vcpu->arch.cr4_guest_owned_bits;
4581 vcpu->arch.cr4 &= ~cr4_guest_owned_bits;
4582 vcpu->arch.cr4 |= vmcs_readl(GUEST_CR4) & cr4_guest_owned_bits;
4585 static void ept_load_pdptrs(struct kvm_vcpu *vcpu)
4587 struct kvm_mmu *mmu = vcpu->arch.walk_mmu;
4589 if (!test_bit(VCPU_EXREG_PDPTR,
4590 (unsigned long *)&vcpu->arch.regs_dirty))
4593 if (is_paging(vcpu) && is_pae(vcpu) && !is_long_mode(vcpu)) {
4594 vmcs_write64(GUEST_PDPTR0, mmu->pdptrs[0]);
4595 vmcs_write64(GUEST_PDPTR1, mmu->pdptrs[1]);
4596 vmcs_write64(GUEST_PDPTR2, mmu->pdptrs[2]);
4597 vmcs_write64(GUEST_PDPTR3, mmu->pdptrs[3]);
4601 static void ept_save_pdptrs(struct kvm_vcpu *vcpu)
4603 struct kvm_mmu *mmu = vcpu->arch.walk_mmu;
4605 if (is_paging(vcpu) && is_pae(vcpu) && !is_long_mode(vcpu)) {
4606 mmu->pdptrs[0] = vmcs_read64(GUEST_PDPTR0);
4607 mmu->pdptrs[1] = vmcs_read64(GUEST_PDPTR1);
4608 mmu->pdptrs[2] = vmcs_read64(GUEST_PDPTR2);
4609 mmu->pdptrs[3] = vmcs_read64(GUEST_PDPTR3);
4612 __set_bit(VCPU_EXREG_PDPTR,
4613 (unsigned long *)&vcpu->arch.regs_avail);
4614 __set_bit(VCPU_EXREG_PDPTR,
4615 (unsigned long *)&vcpu->arch.regs_dirty);
4618 static bool nested_guest_cr0_valid(struct kvm_vcpu *vcpu, unsigned long val)
4620 u64 fixed0 = to_vmx(vcpu)->nested.msrs.cr0_fixed0;
4621 u64 fixed1 = to_vmx(vcpu)->nested.msrs.cr0_fixed1;
4622 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
4624 if (to_vmx(vcpu)->nested.msrs.secondary_ctls_high &
4625 SECONDARY_EXEC_UNRESTRICTED_GUEST &&
4626 nested_cpu_has2(vmcs12, SECONDARY_EXEC_UNRESTRICTED_GUEST))
4627 fixed0 &= ~(X86_CR0_PE | X86_CR0_PG);
4629 return fixed_bits_valid(val, fixed0, fixed1);
4632 static bool nested_host_cr0_valid(struct kvm_vcpu *vcpu, unsigned long val)
4634 u64 fixed0 = to_vmx(vcpu)->nested.msrs.cr0_fixed0;
4635 u64 fixed1 = to_vmx(vcpu)->nested.msrs.cr0_fixed1;
4637 return fixed_bits_valid(val, fixed0, fixed1);
4640 static bool nested_cr4_valid(struct kvm_vcpu *vcpu, unsigned long val)
4642 u64 fixed0 = to_vmx(vcpu)->nested.msrs.cr4_fixed0;
4643 u64 fixed1 = to_vmx(vcpu)->nested.msrs.cr4_fixed1;
4645 return fixed_bits_valid(val, fixed0, fixed1);
4648 /* No difference in the restrictions on guest and host CR4 in VMX operation. */
4649 #define nested_guest_cr4_valid nested_cr4_valid
4650 #define nested_host_cr4_valid nested_cr4_valid
4652 static int vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4);
4654 static void ept_update_paging_mode_cr0(unsigned long *hw_cr0,
4656 struct kvm_vcpu *vcpu)
4658 if (!test_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail))
4659 vmx_decache_cr3(vcpu);
4660 if (!(cr0 & X86_CR0_PG)) {
4661 /* From paging/starting to nonpaging */
4662 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL,
4663 vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) |
4664 (CPU_BASED_CR3_LOAD_EXITING |
4665 CPU_BASED_CR3_STORE_EXITING));
4666 vcpu->arch.cr0 = cr0;
4667 vmx_set_cr4(vcpu, kvm_read_cr4(vcpu));
4668 } else if (!is_paging(vcpu)) {
4669 /* From nonpaging to paging */
4670 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL,
4671 vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) &
4672 ~(CPU_BASED_CR3_LOAD_EXITING |
4673 CPU_BASED_CR3_STORE_EXITING));
4674 vcpu->arch.cr0 = cr0;
4675 vmx_set_cr4(vcpu, kvm_read_cr4(vcpu));
4678 if (!(cr0 & X86_CR0_WP))
4679 *hw_cr0 &= ~X86_CR0_WP;
4682 static void vmx_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
4684 struct vcpu_vmx *vmx = to_vmx(vcpu);
4685 unsigned long hw_cr0;
4687 hw_cr0 = (cr0 & ~KVM_GUEST_CR0_MASK);
4688 if (enable_unrestricted_guest)
4689 hw_cr0 |= KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST;
4691 hw_cr0 |= KVM_VM_CR0_ALWAYS_ON;
4693 if (vmx->rmode.vm86_active && (cr0 & X86_CR0_PE))
4696 if (!vmx->rmode.vm86_active && !(cr0 & X86_CR0_PE))
4700 #ifdef CONFIG_X86_64
4701 if (vcpu->arch.efer & EFER_LME) {
4702 if (!is_paging(vcpu) && (cr0 & X86_CR0_PG))
4704 if (is_paging(vcpu) && !(cr0 & X86_CR0_PG))
4709 if (enable_ept && !enable_unrestricted_guest)
4710 ept_update_paging_mode_cr0(&hw_cr0, cr0, vcpu);
4712 vmcs_writel(CR0_READ_SHADOW, cr0);
4713 vmcs_writel(GUEST_CR0, hw_cr0);
4714 vcpu->arch.cr0 = cr0;
4716 /* depends on vcpu->arch.cr0 to be set to a new value */
4717 vmx->emulation_required = emulation_required(vcpu);
4720 static int get_ept_level(struct kvm_vcpu *vcpu)
4722 if (cpu_has_vmx_ept_5levels() && (cpuid_maxphyaddr(vcpu) > 48))
4727 static u64 construct_eptp(struct kvm_vcpu *vcpu, unsigned long root_hpa)
4729 u64 eptp = VMX_EPTP_MT_WB;
4731 eptp |= (get_ept_level(vcpu) == 5) ? VMX_EPTP_PWL_5 : VMX_EPTP_PWL_4;
4733 if (enable_ept_ad_bits &&
4734 (!is_guest_mode(vcpu) || nested_ept_ad_enabled(vcpu)))
4735 eptp |= VMX_EPTP_AD_ENABLE_BIT;
4736 eptp |= (root_hpa & PAGE_MASK);
4741 static void vmx_set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3)
4743 unsigned long guest_cr3;
4748 eptp = construct_eptp(vcpu, cr3);
4749 vmcs_write64(EPT_POINTER, eptp);
4750 if (enable_unrestricted_guest || is_paging(vcpu) ||
4751 is_guest_mode(vcpu))
4752 guest_cr3 = kvm_read_cr3(vcpu);
4754 guest_cr3 = to_kvm_vmx(vcpu->kvm)->ept_identity_map_addr;
4755 ept_load_pdptrs(vcpu);
4758 vmx_flush_tlb(vcpu, true);
4759 vmcs_writel(GUEST_CR3, guest_cr3);
4762 static int vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
4765 * Pass through host's Machine Check Enable value to hw_cr4, which
4766 * is in force while we are in guest mode. Do not let guests control
4767 * this bit, even if host CR4.MCE == 0.
4769 unsigned long hw_cr4;
4771 hw_cr4 = (cr4_read_shadow() & X86_CR4_MCE) | (cr4 & ~X86_CR4_MCE);
4772 if (enable_unrestricted_guest)
4773 hw_cr4 |= KVM_VM_CR4_ALWAYS_ON_UNRESTRICTED_GUEST;
4774 else if (to_vmx(vcpu)->rmode.vm86_active)
4775 hw_cr4 |= KVM_RMODE_VM_CR4_ALWAYS_ON;
4777 hw_cr4 |= KVM_PMODE_VM_CR4_ALWAYS_ON;
4779 if ((cr4 & X86_CR4_UMIP) && !boot_cpu_has(X86_FEATURE_UMIP)) {
4780 vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL,
4781 SECONDARY_EXEC_DESC);
4782 hw_cr4 &= ~X86_CR4_UMIP;
4783 } else if (!is_guest_mode(vcpu) ||
4784 !nested_cpu_has2(get_vmcs12(vcpu), SECONDARY_EXEC_DESC))
4785 vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL,
4786 SECONDARY_EXEC_DESC);
4788 if (cr4 & X86_CR4_VMXE) {
4790 * To use VMXON (and later other VMX instructions), a guest
4791 * must first be able to turn on cr4.VMXE (see handle_vmon()).
4792 * So basically the check on whether to allow nested VMX
4795 if (!nested_vmx_allowed(vcpu))
4799 if (to_vmx(vcpu)->nested.vmxon && !nested_cr4_valid(vcpu, cr4))
4802 vcpu->arch.cr4 = cr4;
4804 if (!enable_unrestricted_guest) {
4806 if (!is_paging(vcpu)) {
4807 hw_cr4 &= ~X86_CR4_PAE;
4808 hw_cr4 |= X86_CR4_PSE;
4809 } else if (!(cr4 & X86_CR4_PAE)) {
4810 hw_cr4 &= ~X86_CR4_PAE;
4815 * SMEP/SMAP/PKU is disabled if CPU is in non-paging mode in
4816 * hardware. To emulate this behavior, SMEP/SMAP/PKU needs
4817 * to be manually disabled when guest switches to non-paging
4820 * If !enable_unrestricted_guest, the CPU is always running
4821 * with CR0.PG=1 and CR4 needs to be modified.
4822 * If enable_unrestricted_guest, the CPU automatically
4823 * disables SMEP/SMAP/PKU when the guest sets CR0.PG=0.
4825 if (!is_paging(vcpu))
4826 hw_cr4 &= ~(X86_CR4_SMEP | X86_CR4_SMAP | X86_CR4_PKE);
4829 vmcs_writel(CR4_READ_SHADOW, cr4);
4830 vmcs_writel(GUEST_CR4, hw_cr4);
4834 static void vmx_get_segment(struct kvm_vcpu *vcpu,
4835 struct kvm_segment *var, int seg)
4837 struct vcpu_vmx *vmx = to_vmx(vcpu);
4840 if (vmx->rmode.vm86_active && seg != VCPU_SREG_LDTR) {
4841 *var = vmx->rmode.segs[seg];
4842 if (seg == VCPU_SREG_TR
4843 || var->selector == vmx_read_guest_seg_selector(vmx, seg))
4845 var->base = vmx_read_guest_seg_base(vmx, seg);
4846 var->selector = vmx_read_guest_seg_selector(vmx, seg);
4849 var->base = vmx_read_guest_seg_base(vmx, seg);
4850 var->limit = vmx_read_guest_seg_limit(vmx, seg);
4851 var->selector = vmx_read_guest_seg_selector(vmx, seg);
4852 ar = vmx_read_guest_seg_ar(vmx, seg);
4853 var->unusable = (ar >> 16) & 1;
4854 var->type = ar & 15;
4855 var->s = (ar >> 4) & 1;
4856 var->dpl = (ar >> 5) & 3;
4858 * Some userspaces do not preserve unusable property. Since usable
4859 * segment has to be present according to VMX spec we can use present
4860 * property to amend userspace bug by making unusable segment always
4861 * nonpresent. vmx_segment_access_rights() already marks nonpresent
4862 * segment as unusable.
4864 var->present = !var->unusable;
4865 var->avl = (ar >> 12) & 1;
4866 var->l = (ar >> 13) & 1;
4867 var->db = (ar >> 14) & 1;
4868 var->g = (ar >> 15) & 1;
4871 static u64 vmx_get_segment_base(struct kvm_vcpu *vcpu, int seg)
4873 struct kvm_segment s;
4875 if (to_vmx(vcpu)->rmode.vm86_active) {
4876 vmx_get_segment(vcpu, &s, seg);
4879 return vmx_read_guest_seg_base(to_vmx(vcpu), seg);
4882 static int vmx_get_cpl(struct kvm_vcpu *vcpu)
4884 struct vcpu_vmx *vmx = to_vmx(vcpu);
4886 if (unlikely(vmx->rmode.vm86_active))
4889 int ar = vmx_read_guest_seg_ar(vmx, VCPU_SREG_SS);
4890 return VMX_AR_DPL(ar);
4894 static u32 vmx_segment_access_rights(struct kvm_segment *var)
4898 if (var->unusable || !var->present)
4901 ar = var->type & 15;
4902 ar |= (var->s & 1) << 4;
4903 ar |= (var->dpl & 3) << 5;
4904 ar |= (var->present & 1) << 7;
4905 ar |= (var->avl & 1) << 12;
4906 ar |= (var->l & 1) << 13;
4907 ar |= (var->db & 1) << 14;
4908 ar |= (var->g & 1) << 15;
4914 static void vmx_set_segment(struct kvm_vcpu *vcpu,
4915 struct kvm_segment *var, int seg)
4917 struct vcpu_vmx *vmx = to_vmx(vcpu);
4918 const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
4920 vmx_segment_cache_clear(vmx);
4922 if (vmx->rmode.vm86_active && seg != VCPU_SREG_LDTR) {
4923 vmx->rmode.segs[seg] = *var;
4924 if (seg == VCPU_SREG_TR)
4925 vmcs_write16(sf->selector, var->selector);
4927 fix_rmode_seg(seg, &vmx->rmode.segs[seg]);
4931 vmcs_writel(sf->base, var->base);
4932 vmcs_write32(sf->limit, var->limit);
4933 vmcs_write16(sf->selector, var->selector);
4936 * Fix the "Accessed" bit in AR field of segment registers for older
4938 * IA32 arch specifies that at the time of processor reset the
4939 * "Accessed" bit in the AR field of segment registers is 1. And qemu
4940 * is setting it to 0 in the userland code. This causes invalid guest
4941 * state vmexit when "unrestricted guest" mode is turned on.
4942 * Fix for this setup issue in cpu_reset is being pushed in the qemu
4943 * tree. Newer qemu binaries with that qemu fix would not need this
4946 if (enable_unrestricted_guest && (seg != VCPU_SREG_LDTR))
4947 var->type |= 0x1; /* Accessed */
4949 vmcs_write32(sf->ar_bytes, vmx_segment_access_rights(var));
4952 vmx->emulation_required = emulation_required(vcpu);
4955 static void vmx_get_cs_db_l_bits(struct kvm_vcpu *vcpu, int *db, int *l)
4957 u32 ar = vmx_read_guest_seg_ar(to_vmx(vcpu), VCPU_SREG_CS);
4959 *db = (ar >> 14) & 1;
4960 *l = (ar >> 13) & 1;
4963 static void vmx_get_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
4965 dt->size = vmcs_read32(GUEST_IDTR_LIMIT);
4966 dt->address = vmcs_readl(GUEST_IDTR_BASE);
4969 static void vmx_set_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
4971 vmcs_write32(GUEST_IDTR_LIMIT, dt->size);
4972 vmcs_writel(GUEST_IDTR_BASE, dt->address);
4975 static void vmx_get_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
4977 dt->size = vmcs_read32(GUEST_GDTR_LIMIT);
4978 dt->address = vmcs_readl(GUEST_GDTR_BASE);
4981 static void vmx_set_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
4983 vmcs_write32(GUEST_GDTR_LIMIT, dt->size);
4984 vmcs_writel(GUEST_GDTR_BASE, dt->address);
4987 static bool rmode_segment_valid(struct kvm_vcpu *vcpu, int seg)
4989 struct kvm_segment var;
4992 vmx_get_segment(vcpu, &var, seg);
4994 if (seg == VCPU_SREG_CS)
4996 ar = vmx_segment_access_rights(&var);
4998 if (var.base != (var.selector << 4))
5000 if (var.limit != 0xffff)
5008 static bool code_segment_valid(struct kvm_vcpu *vcpu)
5010 struct kvm_segment cs;
5011 unsigned int cs_rpl;
5013 vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
5014 cs_rpl = cs.selector & SEGMENT_RPL_MASK;
5018 if (~cs.type & (VMX_AR_TYPE_CODE_MASK|VMX_AR_TYPE_ACCESSES_MASK))
5022 if (cs.type & VMX_AR_TYPE_WRITEABLE_MASK) {
5023 if (cs.dpl > cs_rpl)
5026 if (cs.dpl != cs_rpl)
5032 /* TODO: Add Reserved field check, this'll require a new member in the kvm_segment_field structure */
5036 static bool stack_segment_valid(struct kvm_vcpu *vcpu)
5038 struct kvm_segment ss;
5039 unsigned int ss_rpl;
5041 vmx_get_segment(vcpu, &ss, VCPU_SREG_SS);
5042 ss_rpl = ss.selector & SEGMENT_RPL_MASK;
5046 if (ss.type != 3 && ss.type != 7)
5050 if (ss.dpl != ss_rpl) /* DPL != RPL */
5058 static bool data_segment_valid(struct kvm_vcpu *vcpu, int seg)
5060 struct kvm_segment var;
5063 vmx_get_segment(vcpu, &var, seg);
5064 rpl = var.selector & SEGMENT_RPL_MASK;
5072 if (~var.type & (VMX_AR_TYPE_CODE_MASK|VMX_AR_TYPE_WRITEABLE_MASK)) {
5073 if (var.dpl < rpl) /* DPL < RPL */
5077 /* TODO: Add other members to kvm_segment_field to allow checking for other access
5083 static bool tr_valid(struct kvm_vcpu *vcpu)
5085 struct kvm_segment tr;
5087 vmx_get_segment(vcpu, &tr, VCPU_SREG_TR);
5091 if (tr.selector & SEGMENT_TI_MASK) /* TI = 1 */
5093 if (tr.type != 3 && tr.type != 11) /* TODO: Check if guest is in IA32e mode */
5101 static bool ldtr_valid(struct kvm_vcpu *vcpu)
5103 struct kvm_segment ldtr;
5105 vmx_get_segment(vcpu, &ldtr, VCPU_SREG_LDTR);
5109 if (ldtr.selector & SEGMENT_TI_MASK) /* TI = 1 */
5119 static bool cs_ss_rpl_check(struct kvm_vcpu *vcpu)
5121 struct kvm_segment cs, ss;
5123 vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
5124 vmx_get_segment(vcpu, &ss, VCPU_SREG_SS);
5126 return ((cs.selector & SEGMENT_RPL_MASK) ==
5127 (ss.selector & SEGMENT_RPL_MASK));
5131 * Check if guest state is valid. Returns true if valid, false if
5133 * We assume that registers are always usable
5135 static bool guest_state_valid(struct kvm_vcpu *vcpu)
5137 if (enable_unrestricted_guest)
5140 /* real mode guest state checks */
5141 if (!is_protmode(vcpu) || (vmx_get_rflags(vcpu) & X86_EFLAGS_VM)) {
5142 if (!rmode_segment_valid(vcpu, VCPU_SREG_CS))
5144 if (!rmode_segment_valid(vcpu, VCPU_SREG_SS))
5146 if (!rmode_segment_valid(vcpu, VCPU_SREG_DS))
5148 if (!rmode_segment_valid(vcpu, VCPU_SREG_ES))
5150 if (!rmode_segment_valid(vcpu, VCPU_SREG_FS))
5152 if (!rmode_segment_valid(vcpu, VCPU_SREG_GS))
5155 /* protected mode guest state checks */
5156 if (!cs_ss_rpl_check(vcpu))
5158 if (!code_segment_valid(vcpu))
5160 if (!stack_segment_valid(vcpu))
5162 if (!data_segment_valid(vcpu, VCPU_SREG_DS))
5164 if (!data_segment_valid(vcpu, VCPU_SREG_ES))
5166 if (!data_segment_valid(vcpu, VCPU_SREG_FS))
5168 if (!data_segment_valid(vcpu, VCPU_SREG_GS))
5170 if (!tr_valid(vcpu))
5172 if (!ldtr_valid(vcpu))
5176 * - Add checks on RIP
5177 * - Add checks on RFLAGS
5183 static bool page_address_valid(struct kvm_vcpu *vcpu, gpa_t gpa)
5185 return PAGE_ALIGNED(gpa) && !(gpa >> cpuid_maxphyaddr(vcpu));
5188 static int init_rmode_tss(struct kvm *kvm)
5194 idx = srcu_read_lock(&kvm->srcu);
5195 fn = to_kvm_vmx(kvm)->tss_addr >> PAGE_SHIFT;
5196 r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE);
5199 data = TSS_BASE_SIZE + TSS_REDIRECTION_SIZE;
5200 r = kvm_write_guest_page(kvm, fn++, &data,
5201 TSS_IOPB_BASE_OFFSET, sizeof(u16));
5204 r = kvm_clear_guest_page(kvm, fn++, 0, PAGE_SIZE);
5207 r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE);
5211 r = kvm_write_guest_page(kvm, fn, &data,
5212 RMODE_TSS_SIZE - 2 * PAGE_SIZE - 1,
5215 srcu_read_unlock(&kvm->srcu, idx);
5219 static int init_rmode_identity_map(struct kvm *kvm)
5221 struct kvm_vmx *kvm_vmx = to_kvm_vmx(kvm);
5223 kvm_pfn_t identity_map_pfn;
5226 /* Protect kvm_vmx->ept_identity_pagetable_done. */
5227 mutex_lock(&kvm->slots_lock);
5229 if (likely(kvm_vmx->ept_identity_pagetable_done))
5232 if (!kvm_vmx->ept_identity_map_addr)
5233 kvm_vmx->ept_identity_map_addr = VMX_EPT_IDENTITY_PAGETABLE_ADDR;
5234 identity_map_pfn = kvm_vmx->ept_identity_map_addr >> PAGE_SHIFT;
5236 r = __x86_set_memory_region(kvm, IDENTITY_PAGETABLE_PRIVATE_MEMSLOT,
5237 kvm_vmx->ept_identity_map_addr, PAGE_SIZE);
5241 idx = srcu_read_lock(&kvm->srcu);
5242 r = kvm_clear_guest_page(kvm, identity_map_pfn, 0, PAGE_SIZE);
5245 /* Set up identity-mapping pagetable for EPT in real mode */
5246 for (i = 0; i < PT32_ENT_PER_PAGE; i++) {
5247 tmp = (i << 22) + (_PAGE_PRESENT | _PAGE_RW | _PAGE_USER |
5248 _PAGE_ACCESSED | _PAGE_DIRTY | _PAGE_PSE);
5249 r = kvm_write_guest_page(kvm, identity_map_pfn,
5250 &tmp, i * sizeof(tmp), sizeof(tmp));
5254 kvm_vmx->ept_identity_pagetable_done = true;
5257 srcu_read_unlock(&kvm->srcu, idx);
5260 mutex_unlock(&kvm->slots_lock);
5264 static void seg_setup(int seg)
5266 const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
5269 vmcs_write16(sf->selector, 0);
5270 vmcs_writel(sf->base, 0);
5271 vmcs_write32(sf->limit, 0xffff);
5273 if (seg == VCPU_SREG_CS)
5274 ar |= 0x08; /* code segment */
5276 vmcs_write32(sf->ar_bytes, ar);
5279 static int alloc_apic_access_page(struct kvm *kvm)
5284 mutex_lock(&kvm->slots_lock);
5285 if (kvm->arch.apic_access_page_done)
5287 r = __x86_set_memory_region(kvm, APIC_ACCESS_PAGE_PRIVATE_MEMSLOT,
5288 APIC_DEFAULT_PHYS_BASE, PAGE_SIZE);
5292 page = gfn_to_page(kvm, APIC_DEFAULT_PHYS_BASE >> PAGE_SHIFT);
5293 if (is_error_page(page)) {
5299 * Do not pin the page in memory, so that memory hot-unplug
5300 * is able to migrate it.
5303 kvm->arch.apic_access_page_done = true;
5305 mutex_unlock(&kvm->slots_lock);
5309 static int allocate_vpid(void)
5315 spin_lock(&vmx_vpid_lock);
5316 vpid = find_first_zero_bit(vmx_vpid_bitmap, VMX_NR_VPIDS);
5317 if (vpid < VMX_NR_VPIDS)
5318 __set_bit(vpid, vmx_vpid_bitmap);
5321 spin_unlock(&vmx_vpid_lock);
5325 static void free_vpid(int vpid)
5327 if (!enable_vpid || vpid == 0)
5329 spin_lock(&vmx_vpid_lock);
5330 __clear_bit(vpid, vmx_vpid_bitmap);
5331 spin_unlock(&vmx_vpid_lock);
5334 static void __always_inline vmx_disable_intercept_for_msr(unsigned long *msr_bitmap,
5337 int f = sizeof(unsigned long);
5339 if (!cpu_has_vmx_msr_bitmap())
5343 * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
5344 * have the write-low and read-high bitmap offsets the wrong way round.
5345 * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
5347 if (msr <= 0x1fff) {
5348 if (type & MSR_TYPE_R)
5350 __clear_bit(msr, msr_bitmap + 0x000 / f);
5352 if (type & MSR_TYPE_W)
5354 __clear_bit(msr, msr_bitmap + 0x800 / f);
5356 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
5358 if (type & MSR_TYPE_R)
5360 __clear_bit(msr, msr_bitmap + 0x400 / f);
5362 if (type & MSR_TYPE_W)
5364 __clear_bit(msr, msr_bitmap + 0xc00 / f);
5369 static void __always_inline vmx_enable_intercept_for_msr(unsigned long *msr_bitmap,
5372 int f = sizeof(unsigned long);
5374 if (!cpu_has_vmx_msr_bitmap())
5378 * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
5379 * have the write-low and read-high bitmap offsets the wrong way round.
5380 * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
5382 if (msr <= 0x1fff) {
5383 if (type & MSR_TYPE_R)
5385 __set_bit(msr, msr_bitmap + 0x000 / f);
5387 if (type & MSR_TYPE_W)
5389 __set_bit(msr, msr_bitmap + 0x800 / f);
5391 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
5393 if (type & MSR_TYPE_R)
5395 __set_bit(msr, msr_bitmap + 0x400 / f);
5397 if (type & MSR_TYPE_W)
5399 __set_bit(msr, msr_bitmap + 0xc00 / f);
5404 static void __always_inline vmx_set_intercept_for_msr(unsigned long *msr_bitmap,
5405 u32 msr, int type, bool value)
5408 vmx_enable_intercept_for_msr(msr_bitmap, msr, type);
5410 vmx_disable_intercept_for_msr(msr_bitmap, msr, type);
5414 * If a msr is allowed by L0, we should check whether it is allowed by L1.
5415 * The corresponding bit will be cleared unless both of L0 and L1 allow it.
5417 static void nested_vmx_disable_intercept_for_msr(unsigned long *msr_bitmap_l1,
5418 unsigned long *msr_bitmap_nested,
5421 int f = sizeof(unsigned long);
5424 * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
5425 * have the write-low and read-high bitmap offsets the wrong way round.
5426 * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
5428 if (msr <= 0x1fff) {
5429 if (type & MSR_TYPE_R &&
5430 !test_bit(msr, msr_bitmap_l1 + 0x000 / f))
5432 __clear_bit(msr, msr_bitmap_nested + 0x000 / f);
5434 if (type & MSR_TYPE_W &&
5435 !test_bit(msr, msr_bitmap_l1 + 0x800 / f))
5437 __clear_bit(msr, msr_bitmap_nested + 0x800 / f);
5439 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
5441 if (type & MSR_TYPE_R &&
5442 !test_bit(msr, msr_bitmap_l1 + 0x400 / f))
5444 __clear_bit(msr, msr_bitmap_nested + 0x400 / f);
5446 if (type & MSR_TYPE_W &&
5447 !test_bit(msr, msr_bitmap_l1 + 0xc00 / f))
5449 __clear_bit(msr, msr_bitmap_nested + 0xc00 / f);
5454 static u8 vmx_msr_bitmap_mode(struct kvm_vcpu *vcpu)
5458 if (cpu_has_secondary_exec_ctrls() &&
5459 (vmcs_read32(SECONDARY_VM_EXEC_CONTROL) &
5460 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE)) {
5461 mode |= MSR_BITMAP_MODE_X2APIC;
5462 if (enable_apicv && kvm_vcpu_apicv_active(vcpu))
5463 mode |= MSR_BITMAP_MODE_X2APIC_APICV;
5466 if (is_long_mode(vcpu))
5467 mode |= MSR_BITMAP_MODE_LM;
5472 #define X2APIC_MSR(r) (APIC_BASE_MSR + ((r) >> 4))
5474 static void vmx_update_msr_bitmap_x2apic(unsigned long *msr_bitmap,
5479 for (msr = 0x800; msr <= 0x8ff; msr += BITS_PER_LONG) {
5480 unsigned word = msr / BITS_PER_LONG;
5481 msr_bitmap[word] = (mode & MSR_BITMAP_MODE_X2APIC_APICV) ? 0 : ~0;
5482 msr_bitmap[word + (0x800 / sizeof(long))] = ~0;
5485 if (mode & MSR_BITMAP_MODE_X2APIC) {
5487 * TPR reads and writes can be virtualized even if virtual interrupt
5488 * delivery is not in use.
5490 vmx_disable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_TASKPRI), MSR_TYPE_RW);
5491 if (mode & MSR_BITMAP_MODE_X2APIC_APICV) {
5492 vmx_enable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_TMCCT), MSR_TYPE_R);
5493 vmx_disable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_EOI), MSR_TYPE_W);
5494 vmx_disable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_SELF_IPI), MSR_TYPE_W);
5499 static void vmx_update_msr_bitmap(struct kvm_vcpu *vcpu)
5501 struct vcpu_vmx *vmx = to_vmx(vcpu);
5502 unsigned long *msr_bitmap = vmx->vmcs01.msr_bitmap;
5503 u8 mode = vmx_msr_bitmap_mode(vcpu);
5504 u8 changed = mode ^ vmx->msr_bitmap_mode;
5509 vmx_set_intercept_for_msr(msr_bitmap, MSR_KERNEL_GS_BASE, MSR_TYPE_RW,
5510 !(mode & MSR_BITMAP_MODE_LM));
5512 if (changed & (MSR_BITMAP_MODE_X2APIC | MSR_BITMAP_MODE_X2APIC_APICV))
5513 vmx_update_msr_bitmap_x2apic(msr_bitmap, mode);
5515 vmx->msr_bitmap_mode = mode;
5518 static bool vmx_get_enable_apicv(struct kvm_vcpu *vcpu)
5520 return enable_apicv;
5523 static void nested_mark_vmcs12_pages_dirty(struct kvm_vcpu *vcpu)
5525 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
5529 * Don't need to mark the APIC access page dirty; it is never
5530 * written to by the CPU during APIC virtualization.
5533 if (nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW)) {
5534 gfn = vmcs12->virtual_apic_page_addr >> PAGE_SHIFT;
5535 kvm_vcpu_mark_page_dirty(vcpu, gfn);
5538 if (nested_cpu_has_posted_intr(vmcs12)) {
5539 gfn = vmcs12->posted_intr_desc_addr >> PAGE_SHIFT;
5540 kvm_vcpu_mark_page_dirty(vcpu, gfn);
5545 static void vmx_complete_nested_posted_interrupt(struct kvm_vcpu *vcpu)
5547 struct vcpu_vmx *vmx = to_vmx(vcpu);
5552 if (!vmx->nested.pi_desc || !vmx->nested.pi_pending)
5555 vmx->nested.pi_pending = false;
5556 if (!pi_test_and_clear_on(vmx->nested.pi_desc))
5559 max_irr = find_last_bit((unsigned long *)vmx->nested.pi_desc->pir, 256);
5560 if (max_irr != 256) {
5561 vapic_page = kmap(vmx->nested.virtual_apic_page);
5562 __kvm_apic_update_irr(vmx->nested.pi_desc->pir,
5563 vapic_page, &max_irr);
5564 kunmap(vmx->nested.virtual_apic_page);
5566 status = vmcs_read16(GUEST_INTR_STATUS);
5567 if ((u8)max_irr > ((u8)status & 0xff)) {
5569 status |= (u8)max_irr;
5570 vmcs_write16(GUEST_INTR_STATUS, status);
5574 nested_mark_vmcs12_pages_dirty(vcpu);
5577 static inline bool kvm_vcpu_trigger_posted_interrupt(struct kvm_vcpu *vcpu,
5581 int pi_vec = nested ? POSTED_INTR_NESTED_VECTOR : POSTED_INTR_VECTOR;
5583 if (vcpu->mode == IN_GUEST_MODE) {
5585 * The vector of interrupt to be delivered to vcpu had
5586 * been set in PIR before this function.
5588 * Following cases will be reached in this block, and
5589 * we always send a notification event in all cases as
5592 * Case 1: vcpu keeps in non-root mode. Sending a
5593 * notification event posts the interrupt to vcpu.
5595 * Case 2: vcpu exits to root mode and is still
5596 * runnable. PIR will be synced to vIRR before the
5597 * next vcpu entry. Sending a notification event in
5598 * this case has no effect, as vcpu is not in root
5601 * Case 3: vcpu exits to root mode and is blocked.
5602 * vcpu_block() has already synced PIR to vIRR and
5603 * never blocks vcpu if vIRR is not cleared. Therefore,
5604 * a blocked vcpu here does not wait for any requested
5605 * interrupts in PIR, and sending a notification event
5606 * which has no effect is safe here.
5609 apic->send_IPI_mask(get_cpu_mask(vcpu->cpu), pi_vec);
5616 static int vmx_deliver_nested_posted_interrupt(struct kvm_vcpu *vcpu,
5619 struct vcpu_vmx *vmx = to_vmx(vcpu);
5621 if (is_guest_mode(vcpu) &&
5622 vector == vmx->nested.posted_intr_nv) {
5624 * If a posted intr is not recognized by hardware,
5625 * we will accomplish it in the next vmentry.
5627 vmx->nested.pi_pending = true;
5628 kvm_make_request(KVM_REQ_EVENT, vcpu);
5629 /* the PIR and ON have been set by L1. */
5630 if (!kvm_vcpu_trigger_posted_interrupt(vcpu, true))
5631 kvm_vcpu_kick(vcpu);
5637 * Send interrupt to vcpu via posted interrupt way.
5638 * 1. If target vcpu is running(non-root mode), send posted interrupt
5639 * notification to vcpu and hardware will sync PIR to vIRR atomically.
5640 * 2. If target vcpu isn't running(root mode), kick it to pick up the
5641 * interrupt from PIR in next vmentry.
5643 static void vmx_deliver_posted_interrupt(struct kvm_vcpu *vcpu, int vector)
5645 struct vcpu_vmx *vmx = to_vmx(vcpu);
5648 r = vmx_deliver_nested_posted_interrupt(vcpu, vector);
5652 if (pi_test_and_set_pir(vector, &vmx->pi_desc))
5655 /* If a previous notification has sent the IPI, nothing to do. */
5656 if (pi_test_and_set_on(&vmx->pi_desc))
5659 if (!kvm_vcpu_trigger_posted_interrupt(vcpu, false))
5660 kvm_vcpu_kick(vcpu);
5664 * Set up the vmcs's constant host-state fields, i.e., host-state fields that
5665 * will not change in the lifetime of the guest.
5666 * Note that host-state that does change is set elsewhere. E.g., host-state
5667 * that is set differently for each CPU is set in vmx_vcpu_load(), not here.
5669 static void vmx_set_constant_host_state(struct vcpu_vmx *vmx)
5674 unsigned long cr0, cr3, cr4;
5677 WARN_ON(cr0 & X86_CR0_TS);
5678 vmcs_writel(HOST_CR0, cr0); /* 22.2.3 */
5681 * Save the most likely value for this task's CR3 in the VMCS.
5682 * We can't use __get_current_cr3_fast() because we're not atomic.
5685 vmcs_writel(HOST_CR3, cr3); /* 22.2.3 FIXME: shadow tables */
5686 vmx->loaded_vmcs->vmcs_host_cr3 = cr3;
5688 /* Save the most likely value for this task's CR4 in the VMCS. */
5689 cr4 = cr4_read_shadow();
5690 vmcs_writel(HOST_CR4, cr4); /* 22.2.3, 22.2.5 */
5691 vmx->loaded_vmcs->vmcs_host_cr4 = cr4;
5693 vmcs_write16(HOST_CS_SELECTOR, __KERNEL_CS); /* 22.2.4 */
5694 #ifdef CONFIG_X86_64
5696 * Load null selectors, so we can avoid reloading them in
5697 * __vmx_load_host_state(), in case userspace uses the null selectors
5698 * too (the expected case).
5700 vmcs_write16(HOST_DS_SELECTOR, 0);
5701 vmcs_write16(HOST_ES_SELECTOR, 0);
5703 vmcs_write16(HOST_DS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
5704 vmcs_write16(HOST_ES_SELECTOR, __KERNEL_DS); /* 22.2.4 */
5706 vmcs_write16(HOST_SS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
5707 vmcs_write16(HOST_TR_SELECTOR, GDT_ENTRY_TSS*8); /* 22.2.4 */
5710 vmcs_writel(HOST_IDTR_BASE, dt.address); /* 22.2.4 */
5711 vmx->host_idt_base = dt.address;
5713 vmcs_writel(HOST_RIP, vmx_return); /* 22.2.5 */
5715 rdmsr(MSR_IA32_SYSENTER_CS, low32, high32);
5716 vmcs_write32(HOST_IA32_SYSENTER_CS, low32);
5717 rdmsrl(MSR_IA32_SYSENTER_EIP, tmpl);
5718 vmcs_writel(HOST_IA32_SYSENTER_EIP, tmpl); /* 22.2.3 */
5720 if (vmcs_config.vmexit_ctrl & VM_EXIT_LOAD_IA32_PAT) {
5721 rdmsr(MSR_IA32_CR_PAT, low32, high32);
5722 vmcs_write64(HOST_IA32_PAT, low32 | ((u64) high32 << 32));
5726 static void set_cr4_guest_host_mask(struct vcpu_vmx *vmx)
5728 vmx->vcpu.arch.cr4_guest_owned_bits = KVM_CR4_GUEST_OWNED_BITS;
5730 vmx->vcpu.arch.cr4_guest_owned_bits |= X86_CR4_PGE;
5731 if (is_guest_mode(&vmx->vcpu))
5732 vmx->vcpu.arch.cr4_guest_owned_bits &=
5733 ~get_vmcs12(&vmx->vcpu)->cr4_guest_host_mask;
5734 vmcs_writel(CR4_GUEST_HOST_MASK, ~vmx->vcpu.arch.cr4_guest_owned_bits);
5737 static u32 vmx_pin_based_exec_ctrl(struct vcpu_vmx *vmx)
5739 u32 pin_based_exec_ctrl = vmcs_config.pin_based_exec_ctrl;
5741 if (!kvm_vcpu_apicv_active(&vmx->vcpu))
5742 pin_based_exec_ctrl &= ~PIN_BASED_POSTED_INTR;
5745 pin_based_exec_ctrl &= ~PIN_BASED_VIRTUAL_NMIS;
5747 /* Enable the preemption timer dynamically */
5748 pin_based_exec_ctrl &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
5749 return pin_based_exec_ctrl;
5752 static void vmx_refresh_apicv_exec_ctrl(struct kvm_vcpu *vcpu)
5754 struct vcpu_vmx *vmx = to_vmx(vcpu);
5756 vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, vmx_pin_based_exec_ctrl(vmx));
5757 if (cpu_has_secondary_exec_ctrls()) {
5758 if (kvm_vcpu_apicv_active(vcpu))
5759 vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL,
5760 SECONDARY_EXEC_APIC_REGISTER_VIRT |
5761 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
5763 vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL,
5764 SECONDARY_EXEC_APIC_REGISTER_VIRT |
5765 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
5768 if (cpu_has_vmx_msr_bitmap())
5769 vmx_update_msr_bitmap(vcpu);
5772 static u32 vmx_exec_control(struct vcpu_vmx *vmx)
5774 u32 exec_control = vmcs_config.cpu_based_exec_ctrl;
5776 if (vmx->vcpu.arch.switch_db_regs & KVM_DEBUGREG_WONT_EXIT)
5777 exec_control &= ~CPU_BASED_MOV_DR_EXITING;
5779 if (!cpu_need_tpr_shadow(&vmx->vcpu)) {
5780 exec_control &= ~CPU_BASED_TPR_SHADOW;
5781 #ifdef CONFIG_X86_64
5782 exec_control |= CPU_BASED_CR8_STORE_EXITING |
5783 CPU_BASED_CR8_LOAD_EXITING;
5787 exec_control |= CPU_BASED_CR3_STORE_EXITING |
5788 CPU_BASED_CR3_LOAD_EXITING |
5789 CPU_BASED_INVLPG_EXITING;
5790 if (kvm_mwait_in_guest(vmx->vcpu.kvm))
5791 exec_control &= ~(CPU_BASED_MWAIT_EXITING |
5792 CPU_BASED_MONITOR_EXITING);
5793 if (kvm_hlt_in_guest(vmx->vcpu.kvm))
5794 exec_control &= ~CPU_BASED_HLT_EXITING;
5795 return exec_control;
5798 static bool vmx_rdrand_supported(void)
5800 return vmcs_config.cpu_based_2nd_exec_ctrl &
5801 SECONDARY_EXEC_RDRAND_EXITING;
5804 static bool vmx_rdseed_supported(void)
5806 return vmcs_config.cpu_based_2nd_exec_ctrl &
5807 SECONDARY_EXEC_RDSEED_EXITING;
5810 static void vmx_compute_secondary_exec_control(struct vcpu_vmx *vmx)
5812 struct kvm_vcpu *vcpu = &vmx->vcpu;
5814 u32 exec_control = vmcs_config.cpu_based_2nd_exec_ctrl;
5816 if (!cpu_need_virtualize_apic_accesses(vcpu))
5817 exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
5819 exec_control &= ~SECONDARY_EXEC_ENABLE_VPID;
5821 exec_control &= ~SECONDARY_EXEC_ENABLE_EPT;
5822 enable_unrestricted_guest = 0;
5823 /* Enable INVPCID for non-ept guests may cause performance regression. */
5824 exec_control &= ~SECONDARY_EXEC_ENABLE_INVPCID;
5826 if (!enable_unrestricted_guest)
5827 exec_control &= ~SECONDARY_EXEC_UNRESTRICTED_GUEST;
5828 if (kvm_pause_in_guest(vmx->vcpu.kvm))
5829 exec_control &= ~SECONDARY_EXEC_PAUSE_LOOP_EXITING;
5830 if (!kvm_vcpu_apicv_active(vcpu))
5831 exec_control &= ~(SECONDARY_EXEC_APIC_REGISTER_VIRT |
5832 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
5833 exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
5835 /* SECONDARY_EXEC_DESC is enabled/disabled on writes to CR4.UMIP,
5836 * in vmx_set_cr4. */
5837 exec_control &= ~SECONDARY_EXEC_DESC;
5839 /* SECONDARY_EXEC_SHADOW_VMCS is enabled when L1 executes VMPTRLD
5841 We can NOT enable shadow_vmcs here because we don't have yet
5844 exec_control &= ~SECONDARY_EXEC_SHADOW_VMCS;
5847 exec_control &= ~SECONDARY_EXEC_ENABLE_PML;
5849 if (vmx_xsaves_supported()) {
5850 /* Exposing XSAVES only when XSAVE is exposed */
5851 bool xsaves_enabled =
5852 guest_cpuid_has(vcpu, X86_FEATURE_XSAVE) &&
5853 guest_cpuid_has(vcpu, X86_FEATURE_XSAVES);
5855 if (!xsaves_enabled)
5856 exec_control &= ~SECONDARY_EXEC_XSAVES;
5860 vmx->nested.msrs.secondary_ctls_high |=
5861 SECONDARY_EXEC_XSAVES;
5863 vmx->nested.msrs.secondary_ctls_high &=
5864 ~SECONDARY_EXEC_XSAVES;
5868 if (vmx_rdtscp_supported()) {
5869 bool rdtscp_enabled = guest_cpuid_has(vcpu, X86_FEATURE_RDTSCP);
5870 if (!rdtscp_enabled)
5871 exec_control &= ~SECONDARY_EXEC_RDTSCP;
5875 vmx->nested.msrs.secondary_ctls_high |=
5876 SECONDARY_EXEC_RDTSCP;
5878 vmx->nested.msrs.secondary_ctls_high &=
5879 ~SECONDARY_EXEC_RDTSCP;
5883 if (vmx_invpcid_supported()) {
5884 /* Exposing INVPCID only when PCID is exposed */
5885 bool invpcid_enabled =
5886 guest_cpuid_has(vcpu, X86_FEATURE_INVPCID) &&
5887 guest_cpuid_has(vcpu, X86_FEATURE_PCID);
5889 if (!invpcid_enabled) {
5890 exec_control &= ~SECONDARY_EXEC_ENABLE_INVPCID;
5891 guest_cpuid_clear(vcpu, X86_FEATURE_INVPCID);
5895 if (invpcid_enabled)
5896 vmx->nested.msrs.secondary_ctls_high |=
5897 SECONDARY_EXEC_ENABLE_INVPCID;
5899 vmx->nested.msrs.secondary_ctls_high &=
5900 ~SECONDARY_EXEC_ENABLE_INVPCID;
5904 if (vmx_rdrand_supported()) {
5905 bool rdrand_enabled = guest_cpuid_has(vcpu, X86_FEATURE_RDRAND);
5907 exec_control &= ~SECONDARY_EXEC_RDRAND_EXITING;
5911 vmx->nested.msrs.secondary_ctls_high |=
5912 SECONDARY_EXEC_RDRAND_EXITING;
5914 vmx->nested.msrs.secondary_ctls_high &=
5915 ~SECONDARY_EXEC_RDRAND_EXITING;
5919 if (vmx_rdseed_supported()) {
5920 bool rdseed_enabled = guest_cpuid_has(vcpu, X86_FEATURE_RDSEED);
5922 exec_control &= ~SECONDARY_EXEC_RDSEED_EXITING;
5926 vmx->nested.msrs.secondary_ctls_high |=
5927 SECONDARY_EXEC_RDSEED_EXITING;
5929 vmx->nested.msrs.secondary_ctls_high &=
5930 ~SECONDARY_EXEC_RDSEED_EXITING;
5934 vmx->secondary_exec_control = exec_control;
5937 static void ept_set_mmio_spte_mask(void)
5940 * EPT Misconfigurations can be generated if the value of bits 2:0
5941 * of an EPT paging-structure entry is 110b (write/execute).
5943 kvm_mmu_set_mmio_spte_mask(VMX_EPT_RWX_MASK,
5944 VMX_EPT_MISCONFIG_WX_VALUE);
5947 #define VMX_XSS_EXIT_BITMAP 0
5949 * Sets up the vmcs for emulated real mode.
5951 static void vmx_vcpu_setup(struct vcpu_vmx *vmx)
5953 #ifdef CONFIG_X86_64
5958 if (enable_shadow_vmcs) {
5959 vmcs_write64(VMREAD_BITMAP, __pa(vmx_vmread_bitmap));
5960 vmcs_write64(VMWRITE_BITMAP, __pa(vmx_vmwrite_bitmap));
5962 if (cpu_has_vmx_msr_bitmap())
5963 vmcs_write64(MSR_BITMAP, __pa(vmx->vmcs01.msr_bitmap));
5965 vmcs_write64(VMCS_LINK_POINTER, -1ull); /* 22.3.1.5 */
5968 vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, vmx_pin_based_exec_ctrl(vmx));
5969 vmx->hv_deadline_tsc = -1;
5971 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, vmx_exec_control(vmx));
5973 if (cpu_has_secondary_exec_ctrls()) {
5974 vmx_compute_secondary_exec_control(vmx);
5975 vmcs_write32(SECONDARY_VM_EXEC_CONTROL,
5976 vmx->secondary_exec_control);
5979 if (kvm_vcpu_apicv_active(&vmx->vcpu)) {
5980 vmcs_write64(EOI_EXIT_BITMAP0, 0);
5981 vmcs_write64(EOI_EXIT_BITMAP1, 0);
5982 vmcs_write64(EOI_EXIT_BITMAP2, 0);
5983 vmcs_write64(EOI_EXIT_BITMAP3, 0);
5985 vmcs_write16(GUEST_INTR_STATUS, 0);
5987 vmcs_write16(POSTED_INTR_NV, POSTED_INTR_VECTOR);
5988 vmcs_write64(POSTED_INTR_DESC_ADDR, __pa((&vmx->pi_desc)));
5991 if (!kvm_pause_in_guest(vmx->vcpu.kvm)) {
5992 vmcs_write32(PLE_GAP, ple_gap);
5993 vmx->ple_window = ple_window;
5994 vmx->ple_window_dirty = true;
5997 vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK, 0);
5998 vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH, 0);
5999 vmcs_write32(CR3_TARGET_COUNT, 0); /* 22.2.1 */
6001 vmcs_write16(HOST_FS_SELECTOR, 0); /* 22.2.4 */
6002 vmcs_write16(HOST_GS_SELECTOR, 0); /* 22.2.4 */
6003 vmx_set_constant_host_state(vmx);
6004 #ifdef CONFIG_X86_64
6005 rdmsrl(MSR_FS_BASE, a);
6006 vmcs_writel(HOST_FS_BASE, a); /* 22.2.4 */
6007 rdmsrl(MSR_GS_BASE, a);
6008 vmcs_writel(HOST_GS_BASE, a); /* 22.2.4 */
6010 vmcs_writel(HOST_FS_BASE, 0); /* 22.2.4 */
6011 vmcs_writel(HOST_GS_BASE, 0); /* 22.2.4 */
6014 if (cpu_has_vmx_vmfunc())
6015 vmcs_write64(VM_FUNCTION_CONTROL, 0);
6017 vmcs_write32(VM_EXIT_MSR_STORE_COUNT, 0);
6018 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, 0);
6019 vmcs_write64(VM_EXIT_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.host));
6020 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, 0);
6021 vmcs_write64(VM_ENTRY_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.guest));
6023 if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT)
6024 vmcs_write64(GUEST_IA32_PAT, vmx->vcpu.arch.pat);
6026 for (i = 0; i < ARRAY_SIZE(vmx_msr_index); ++i) {
6027 u32 index = vmx_msr_index[i];
6028 u32 data_low, data_high;
6031 if (rdmsr_safe(index, &data_low, &data_high) < 0)
6033 if (wrmsr_safe(index, data_low, data_high) < 0)
6035 vmx->guest_msrs[j].index = i;
6036 vmx->guest_msrs[j].data = 0;
6037 vmx->guest_msrs[j].mask = -1ull;
6041 if (boot_cpu_has(X86_FEATURE_ARCH_CAPABILITIES))
6042 rdmsrl(MSR_IA32_ARCH_CAPABILITIES, vmx->arch_capabilities);
6044 vm_exit_controls_init(vmx, vmcs_config.vmexit_ctrl);
6046 /* 22.2.1, 20.8.1 */
6047 vm_entry_controls_init(vmx, vmcs_config.vmentry_ctrl);
6049 vmx->vcpu.arch.cr0_guest_owned_bits = X86_CR0_TS;
6050 vmcs_writel(CR0_GUEST_HOST_MASK, ~X86_CR0_TS);
6052 set_cr4_guest_host_mask(vmx);
6054 if (vmx_xsaves_supported())
6055 vmcs_write64(XSS_EXIT_BITMAP, VMX_XSS_EXIT_BITMAP);
6058 ASSERT(vmx->pml_pg);
6059 vmcs_write64(PML_ADDRESS, page_to_phys(vmx->pml_pg));
6060 vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1);
6064 static void vmx_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event)
6066 struct vcpu_vmx *vmx = to_vmx(vcpu);
6067 struct msr_data apic_base_msr;
6070 vmx->rmode.vm86_active = 0;
6073 vcpu->arch.microcode_version = 0x100000000ULL;
6074 vmx->vcpu.arch.regs[VCPU_REGS_RDX] = get_rdx_init_val();
6075 kvm_set_cr8(vcpu, 0);
6078 apic_base_msr.data = APIC_DEFAULT_PHYS_BASE |
6079 MSR_IA32_APICBASE_ENABLE;
6080 if (kvm_vcpu_is_reset_bsp(vcpu))
6081 apic_base_msr.data |= MSR_IA32_APICBASE_BSP;
6082 apic_base_msr.host_initiated = true;
6083 kvm_set_apic_base(vcpu, &apic_base_msr);
6086 vmx_segment_cache_clear(vmx);
6088 seg_setup(VCPU_SREG_CS);
6089 vmcs_write16(GUEST_CS_SELECTOR, 0xf000);
6090 vmcs_writel(GUEST_CS_BASE, 0xffff0000ul);
6092 seg_setup(VCPU_SREG_DS);
6093 seg_setup(VCPU_SREG_ES);
6094 seg_setup(VCPU_SREG_FS);
6095 seg_setup(VCPU_SREG_GS);
6096 seg_setup(VCPU_SREG_SS);
6098 vmcs_write16(GUEST_TR_SELECTOR, 0);
6099 vmcs_writel(GUEST_TR_BASE, 0);
6100 vmcs_write32(GUEST_TR_LIMIT, 0xffff);
6101 vmcs_write32(GUEST_TR_AR_BYTES, 0x008b);
6103 vmcs_write16(GUEST_LDTR_SELECTOR, 0);
6104 vmcs_writel(GUEST_LDTR_BASE, 0);
6105 vmcs_write32(GUEST_LDTR_LIMIT, 0xffff);
6106 vmcs_write32(GUEST_LDTR_AR_BYTES, 0x00082);
6109 vmcs_write32(GUEST_SYSENTER_CS, 0);
6110 vmcs_writel(GUEST_SYSENTER_ESP, 0);
6111 vmcs_writel(GUEST_SYSENTER_EIP, 0);
6112 vmcs_write64(GUEST_IA32_DEBUGCTL, 0);
6115 kvm_set_rflags(vcpu, X86_EFLAGS_FIXED);
6116 kvm_rip_write(vcpu, 0xfff0);
6118 vmcs_writel(GUEST_GDTR_BASE, 0);
6119 vmcs_write32(GUEST_GDTR_LIMIT, 0xffff);
6121 vmcs_writel(GUEST_IDTR_BASE, 0);
6122 vmcs_write32(GUEST_IDTR_LIMIT, 0xffff);
6124 vmcs_write32(GUEST_ACTIVITY_STATE, GUEST_ACTIVITY_ACTIVE);
6125 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, 0);
6126 vmcs_writel(GUEST_PENDING_DBG_EXCEPTIONS, 0);
6127 if (kvm_mpx_supported())
6128 vmcs_write64(GUEST_BNDCFGS, 0);
6132 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0); /* 22.2.1 */
6134 if (cpu_has_vmx_tpr_shadow() && !init_event) {
6135 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, 0);
6136 if (cpu_need_tpr_shadow(vcpu))
6137 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR,
6138 __pa(vcpu->arch.apic->regs));
6139 vmcs_write32(TPR_THRESHOLD, 0);
6142 kvm_make_request(KVM_REQ_APIC_PAGE_RELOAD, vcpu);
6145 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid);
6147 cr0 = X86_CR0_NW | X86_CR0_CD | X86_CR0_ET;
6148 vmx->vcpu.arch.cr0 = cr0;
6149 vmx_set_cr0(vcpu, cr0); /* enter rmode */
6150 vmx_set_cr4(vcpu, 0);
6151 vmx_set_efer(vcpu, 0);
6153 update_exception_bitmap(vcpu);
6155 vpid_sync_context(vmx->vpid);
6157 vmx_clear_hlt(vcpu);
6161 * In nested virtualization, check if L1 asked to exit on external interrupts.
6162 * For most existing hypervisors, this will always return true.
6164 static bool nested_exit_on_intr(struct kvm_vcpu *vcpu)
6166 return get_vmcs12(vcpu)->pin_based_vm_exec_control &
6167 PIN_BASED_EXT_INTR_MASK;
6171 * In nested virtualization, check if L1 has set
6172 * VM_EXIT_ACK_INTR_ON_EXIT
6174 static bool nested_exit_intr_ack_set(struct kvm_vcpu *vcpu)
6176 return get_vmcs12(vcpu)->vm_exit_controls &
6177 VM_EXIT_ACK_INTR_ON_EXIT;
6180 static bool nested_exit_on_nmi(struct kvm_vcpu *vcpu)
6182 return nested_cpu_has_nmi_exiting(get_vmcs12(vcpu));
6185 static void enable_irq_window(struct kvm_vcpu *vcpu)
6187 vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL,
6188 CPU_BASED_VIRTUAL_INTR_PENDING);
6191 static void enable_nmi_window(struct kvm_vcpu *vcpu)
6194 vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & GUEST_INTR_STATE_STI) {
6195 enable_irq_window(vcpu);
6199 vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL,
6200 CPU_BASED_VIRTUAL_NMI_PENDING);
6203 static void vmx_inject_irq(struct kvm_vcpu *vcpu)
6205 struct vcpu_vmx *vmx = to_vmx(vcpu);
6207 int irq = vcpu->arch.interrupt.nr;
6209 trace_kvm_inj_virq(irq);
6211 ++vcpu->stat.irq_injections;
6212 if (vmx->rmode.vm86_active) {
6214 if (vcpu->arch.interrupt.soft)
6215 inc_eip = vcpu->arch.event_exit_inst_len;
6216 if (kvm_inject_realmode_interrupt(vcpu, irq, inc_eip) != EMULATE_DONE)
6217 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
6220 intr = irq | INTR_INFO_VALID_MASK;
6221 if (vcpu->arch.interrupt.soft) {
6222 intr |= INTR_TYPE_SOFT_INTR;
6223 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
6224 vmx->vcpu.arch.event_exit_inst_len);
6226 intr |= INTR_TYPE_EXT_INTR;
6227 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr);
6229 vmx_clear_hlt(vcpu);
6232 static void vmx_inject_nmi(struct kvm_vcpu *vcpu)
6234 struct vcpu_vmx *vmx = to_vmx(vcpu);
6238 * Tracking the NMI-blocked state in software is built upon
6239 * finding the next open IRQ window. This, in turn, depends on
6240 * well-behaving guests: They have to keep IRQs disabled at
6241 * least as long as the NMI handler runs. Otherwise we may
6242 * cause NMI nesting, maybe breaking the guest. But as this is
6243 * highly unlikely, we can live with the residual risk.
6245 vmx->loaded_vmcs->soft_vnmi_blocked = 1;
6246 vmx->loaded_vmcs->vnmi_blocked_time = 0;
6249 ++vcpu->stat.nmi_injections;
6250 vmx->loaded_vmcs->nmi_known_unmasked = false;
6252 if (vmx->rmode.vm86_active) {
6253 if (kvm_inject_realmode_interrupt(vcpu, NMI_VECTOR, 0) != EMULATE_DONE)
6254 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
6258 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
6259 INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK | NMI_VECTOR);
6261 vmx_clear_hlt(vcpu);
6264 static bool vmx_get_nmi_mask(struct kvm_vcpu *vcpu)
6266 struct vcpu_vmx *vmx = to_vmx(vcpu);
6270 return vmx->loaded_vmcs->soft_vnmi_blocked;
6271 if (vmx->loaded_vmcs->nmi_known_unmasked)
6273 masked = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & GUEST_INTR_STATE_NMI;
6274 vmx->loaded_vmcs->nmi_known_unmasked = !masked;
6278 static void vmx_set_nmi_mask(struct kvm_vcpu *vcpu, bool masked)
6280 struct vcpu_vmx *vmx = to_vmx(vcpu);
6283 if (vmx->loaded_vmcs->soft_vnmi_blocked != masked) {
6284 vmx->loaded_vmcs->soft_vnmi_blocked = masked;
6285 vmx->loaded_vmcs->vnmi_blocked_time = 0;
6288 vmx->loaded_vmcs->nmi_known_unmasked = !masked;
6290 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
6291 GUEST_INTR_STATE_NMI);
6293 vmcs_clear_bits(GUEST_INTERRUPTIBILITY_INFO,
6294 GUEST_INTR_STATE_NMI);
6298 static int vmx_nmi_allowed(struct kvm_vcpu *vcpu)
6300 if (to_vmx(vcpu)->nested.nested_run_pending)
6304 to_vmx(vcpu)->loaded_vmcs->soft_vnmi_blocked)
6307 return !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) &
6308 (GUEST_INTR_STATE_MOV_SS | GUEST_INTR_STATE_STI
6309 | GUEST_INTR_STATE_NMI));
6312 static int vmx_interrupt_allowed(struct kvm_vcpu *vcpu)
6314 return (!to_vmx(vcpu)->nested.nested_run_pending &&
6315 vmcs_readl(GUEST_RFLAGS) & X86_EFLAGS_IF) &&
6316 !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) &
6317 (GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS));
6320 static int vmx_set_tss_addr(struct kvm *kvm, unsigned int addr)
6324 if (enable_unrestricted_guest)
6327 ret = x86_set_memory_region(kvm, TSS_PRIVATE_MEMSLOT, addr,
6331 to_kvm_vmx(kvm)->tss_addr = addr;
6332 return init_rmode_tss(kvm);
6335 static int vmx_set_identity_map_addr(struct kvm *kvm, u64 ident_addr)
6337 to_kvm_vmx(kvm)->ept_identity_map_addr = ident_addr;
6341 static bool rmode_exception(struct kvm_vcpu *vcpu, int vec)
6346 * Update instruction length as we may reinject the exception
6347 * from user space while in guest debugging mode.
6349 to_vmx(vcpu)->vcpu.arch.event_exit_inst_len =
6350 vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
6351 if (vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
6355 if (vcpu->guest_debug &
6356 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))
6373 static int handle_rmode_exception(struct kvm_vcpu *vcpu,
6374 int vec, u32 err_code)
6377 * Instruction with address size override prefix opcode 0x67
6378 * Cause the #SS fault with 0 error code in VM86 mode.
6380 if (((vec == GP_VECTOR) || (vec == SS_VECTOR)) && err_code == 0) {
6381 if (emulate_instruction(vcpu, 0) == EMULATE_DONE) {
6382 if (vcpu->arch.halt_request) {
6383 vcpu->arch.halt_request = 0;
6384 return kvm_vcpu_halt(vcpu);
6392 * Forward all other exceptions that are valid in real mode.
6393 * FIXME: Breaks guest debugging in real mode, needs to be fixed with
6394 * the required debugging infrastructure rework.
6396 kvm_queue_exception(vcpu, vec);
6401 * Trigger machine check on the host. We assume all the MSRs are already set up
6402 * by the CPU and that we still run on the same CPU as the MCE occurred on.
6403 * We pass a fake environment to the machine check handler because we want
6404 * the guest to be always treated like user space, no matter what context
6405 * it used internally.
6407 static void kvm_machine_check(void)
6409 #if defined(CONFIG_X86_MCE) && defined(CONFIG_X86_64)
6410 struct pt_regs regs = {
6411 .cs = 3, /* Fake ring 3 no matter what the guest ran on */
6412 .flags = X86_EFLAGS_IF,
6415 do_machine_check(®s, 0);
6419 static int handle_machine_check(struct kvm_vcpu *vcpu)
6421 /* already handled by vcpu_run */
6425 static int handle_exception(struct kvm_vcpu *vcpu)
6427 struct vcpu_vmx *vmx = to_vmx(vcpu);
6428 struct kvm_run *kvm_run = vcpu->run;
6429 u32 intr_info, ex_no, error_code;
6430 unsigned long cr2, rip, dr6;
6432 enum emulation_result er;
6434 vect_info = vmx->idt_vectoring_info;
6435 intr_info = vmx->exit_intr_info;
6437 if (is_machine_check(intr_info))
6438 return handle_machine_check(vcpu);
6440 if (is_nmi(intr_info))
6441 return 1; /* already handled by vmx_vcpu_run() */
6443 if (is_invalid_opcode(intr_info))
6444 return handle_ud(vcpu);
6447 if (intr_info & INTR_INFO_DELIVER_CODE_MASK)
6448 error_code = vmcs_read32(VM_EXIT_INTR_ERROR_CODE);
6450 if (!vmx->rmode.vm86_active && is_gp_fault(intr_info)) {
6451 WARN_ON_ONCE(!enable_vmware_backdoor);
6452 er = emulate_instruction(vcpu,
6453 EMULTYPE_VMWARE | EMULTYPE_NO_UD_ON_FAIL);
6454 if (er == EMULATE_USER_EXIT)
6456 else if (er != EMULATE_DONE)
6457 kvm_queue_exception_e(vcpu, GP_VECTOR, error_code);
6462 * The #PF with PFEC.RSVD = 1 indicates the guest is accessing
6463 * MMIO, it is better to report an internal error.
6464 * See the comments in vmx_handle_exit.
6466 if ((vect_info & VECTORING_INFO_VALID_MASK) &&
6467 !(is_page_fault(intr_info) && !(error_code & PFERR_RSVD_MASK))) {
6468 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
6469 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_SIMUL_EX;
6470 vcpu->run->internal.ndata = 3;
6471 vcpu->run->internal.data[0] = vect_info;
6472 vcpu->run->internal.data[1] = intr_info;
6473 vcpu->run->internal.data[2] = error_code;
6477 if (is_page_fault(intr_info)) {
6478 cr2 = vmcs_readl(EXIT_QUALIFICATION);
6479 /* EPT won't cause page fault directly */
6480 WARN_ON_ONCE(!vcpu->arch.apf.host_apf_reason && enable_ept);
6481 return kvm_handle_page_fault(vcpu, error_code, cr2, NULL, 0);
6484 ex_no = intr_info & INTR_INFO_VECTOR_MASK;
6486 if (vmx->rmode.vm86_active && rmode_exception(vcpu, ex_no))
6487 return handle_rmode_exception(vcpu, ex_no, error_code);
6491 kvm_queue_exception_e(vcpu, AC_VECTOR, error_code);
6494 dr6 = vmcs_readl(EXIT_QUALIFICATION);
6495 if (!(vcpu->guest_debug &
6496 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))) {
6497 vcpu->arch.dr6 &= ~15;
6498 vcpu->arch.dr6 |= dr6 | DR6_RTM;
6499 if (is_icebp(intr_info))
6500 skip_emulated_instruction(vcpu);
6502 kvm_queue_exception(vcpu, DB_VECTOR);
6505 kvm_run->debug.arch.dr6 = dr6 | DR6_FIXED_1;
6506 kvm_run->debug.arch.dr7 = vmcs_readl(GUEST_DR7);
6510 * Update instruction length as we may reinject #BP from
6511 * user space while in guest debugging mode. Reading it for
6512 * #DB as well causes no harm, it is not used in that case.
6514 vmx->vcpu.arch.event_exit_inst_len =
6515 vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
6516 kvm_run->exit_reason = KVM_EXIT_DEBUG;
6517 rip = kvm_rip_read(vcpu);
6518 kvm_run->debug.arch.pc = vmcs_readl(GUEST_CS_BASE) + rip;
6519 kvm_run->debug.arch.exception = ex_no;
6522 kvm_run->exit_reason = KVM_EXIT_EXCEPTION;
6523 kvm_run->ex.exception = ex_no;
6524 kvm_run->ex.error_code = error_code;
6530 static int handle_external_interrupt(struct kvm_vcpu *vcpu)
6532 ++vcpu->stat.irq_exits;
6536 static int handle_triple_fault(struct kvm_vcpu *vcpu)
6538 vcpu->run->exit_reason = KVM_EXIT_SHUTDOWN;
6539 vcpu->mmio_needed = 0;
6543 static int handle_io(struct kvm_vcpu *vcpu)
6545 unsigned long exit_qualification;
6546 int size, in, string;
6549 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
6550 string = (exit_qualification & 16) != 0;
6552 ++vcpu->stat.io_exits;
6555 return emulate_instruction(vcpu, 0) == EMULATE_DONE;
6557 port = exit_qualification >> 16;
6558 size = (exit_qualification & 7) + 1;
6559 in = (exit_qualification & 8) != 0;
6561 return kvm_fast_pio(vcpu, size, port, in);
6565 vmx_patch_hypercall(struct kvm_vcpu *vcpu, unsigned char *hypercall)
6568 * Patch in the VMCALL instruction:
6570 hypercall[0] = 0x0f;
6571 hypercall[1] = 0x01;
6572 hypercall[2] = 0xc1;
6575 /* called to set cr0 as appropriate for a mov-to-cr0 exit. */
6576 static int handle_set_cr0(struct kvm_vcpu *vcpu, unsigned long val)
6578 if (is_guest_mode(vcpu)) {
6579 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
6580 unsigned long orig_val = val;
6583 * We get here when L2 changed cr0 in a way that did not change
6584 * any of L1's shadowed bits (see nested_vmx_exit_handled_cr),
6585 * but did change L0 shadowed bits. So we first calculate the
6586 * effective cr0 value that L1 would like to write into the
6587 * hardware. It consists of the L2-owned bits from the new
6588 * value combined with the L1-owned bits from L1's guest_cr0.
6590 val = (val & ~vmcs12->cr0_guest_host_mask) |
6591 (vmcs12->guest_cr0 & vmcs12->cr0_guest_host_mask);
6593 if (!nested_guest_cr0_valid(vcpu, val))
6596 if (kvm_set_cr0(vcpu, val))
6598 vmcs_writel(CR0_READ_SHADOW, orig_val);
6601 if (to_vmx(vcpu)->nested.vmxon &&
6602 !nested_host_cr0_valid(vcpu, val))
6605 return kvm_set_cr0(vcpu, val);
6609 static int handle_set_cr4(struct kvm_vcpu *vcpu, unsigned long val)
6611 if (is_guest_mode(vcpu)) {
6612 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
6613 unsigned long orig_val = val;
6615 /* analogously to handle_set_cr0 */
6616 val = (val & ~vmcs12->cr4_guest_host_mask) |
6617 (vmcs12->guest_cr4 & vmcs12->cr4_guest_host_mask);
6618 if (kvm_set_cr4(vcpu, val))
6620 vmcs_writel(CR4_READ_SHADOW, orig_val);
6623 return kvm_set_cr4(vcpu, val);
6626 static int handle_desc(struct kvm_vcpu *vcpu)
6628 WARN_ON(!(vcpu->arch.cr4 & X86_CR4_UMIP));
6629 return emulate_instruction(vcpu, 0) == EMULATE_DONE;
6632 static int handle_cr(struct kvm_vcpu *vcpu)
6634 unsigned long exit_qualification, val;
6640 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
6641 cr = exit_qualification & 15;
6642 reg = (exit_qualification >> 8) & 15;
6643 switch ((exit_qualification >> 4) & 3) {
6644 case 0: /* mov to cr */
6645 val = kvm_register_readl(vcpu, reg);
6646 trace_kvm_cr_write(cr, val);
6649 err = handle_set_cr0(vcpu, val);
6650 return kvm_complete_insn_gp(vcpu, err);
6652 WARN_ON_ONCE(enable_unrestricted_guest);
6653 err = kvm_set_cr3(vcpu, val);
6654 return kvm_complete_insn_gp(vcpu, err);
6656 err = handle_set_cr4(vcpu, val);
6657 return kvm_complete_insn_gp(vcpu, err);
6659 u8 cr8_prev = kvm_get_cr8(vcpu);
6661 err = kvm_set_cr8(vcpu, cr8);
6662 ret = kvm_complete_insn_gp(vcpu, err);
6663 if (lapic_in_kernel(vcpu))
6665 if (cr8_prev <= cr8)
6668 * TODO: we might be squashing a
6669 * KVM_GUESTDBG_SINGLESTEP-triggered
6670 * KVM_EXIT_DEBUG here.
6672 vcpu->run->exit_reason = KVM_EXIT_SET_TPR;
6678 WARN_ONCE(1, "Guest should always own CR0.TS");
6679 vmx_set_cr0(vcpu, kvm_read_cr0_bits(vcpu, ~X86_CR0_TS));
6680 trace_kvm_cr_write(0, kvm_read_cr0(vcpu));
6681 return kvm_skip_emulated_instruction(vcpu);
6682 case 1: /*mov from cr*/
6685 WARN_ON_ONCE(enable_unrestricted_guest);
6686 val = kvm_read_cr3(vcpu);
6687 kvm_register_write(vcpu, reg, val);
6688 trace_kvm_cr_read(cr, val);
6689 return kvm_skip_emulated_instruction(vcpu);
6691 val = kvm_get_cr8(vcpu);
6692 kvm_register_write(vcpu, reg, val);
6693 trace_kvm_cr_read(cr, val);
6694 return kvm_skip_emulated_instruction(vcpu);
6698 val = (exit_qualification >> LMSW_SOURCE_DATA_SHIFT) & 0x0f;
6699 trace_kvm_cr_write(0, (kvm_read_cr0(vcpu) & ~0xful) | val);
6700 kvm_lmsw(vcpu, val);
6702 return kvm_skip_emulated_instruction(vcpu);
6706 vcpu->run->exit_reason = 0;
6707 vcpu_unimpl(vcpu, "unhandled control register: op %d cr %d\n",
6708 (int)(exit_qualification >> 4) & 3, cr);
6712 static int handle_dr(struct kvm_vcpu *vcpu)
6714 unsigned long exit_qualification;
6717 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
6718 dr = exit_qualification & DEBUG_REG_ACCESS_NUM;
6720 /* First, if DR does not exist, trigger UD */
6721 if (!kvm_require_dr(vcpu, dr))
6724 /* Do not handle if the CPL > 0, will trigger GP on re-entry */
6725 if (!kvm_require_cpl(vcpu, 0))
6727 dr7 = vmcs_readl(GUEST_DR7);
6730 * As the vm-exit takes precedence over the debug trap, we
6731 * need to emulate the latter, either for the host or the
6732 * guest debugging itself.
6734 if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP) {
6735 vcpu->run->debug.arch.dr6 = vcpu->arch.dr6;
6736 vcpu->run->debug.arch.dr7 = dr7;
6737 vcpu->run->debug.arch.pc = kvm_get_linear_rip(vcpu);
6738 vcpu->run->debug.arch.exception = DB_VECTOR;
6739 vcpu->run->exit_reason = KVM_EXIT_DEBUG;
6742 vcpu->arch.dr6 &= ~15;
6743 vcpu->arch.dr6 |= DR6_BD | DR6_RTM;
6744 kvm_queue_exception(vcpu, DB_VECTOR);
6749 if (vcpu->guest_debug == 0) {
6750 vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL,
6751 CPU_BASED_MOV_DR_EXITING);
6754 * No more DR vmexits; force a reload of the debug registers
6755 * and reenter on this instruction. The next vmexit will
6756 * retrieve the full state of the debug registers.
6758 vcpu->arch.switch_db_regs |= KVM_DEBUGREG_WONT_EXIT;
6762 reg = DEBUG_REG_ACCESS_REG(exit_qualification);
6763 if (exit_qualification & TYPE_MOV_FROM_DR) {
6766 if (kvm_get_dr(vcpu, dr, &val))
6768 kvm_register_write(vcpu, reg, val);
6770 if (kvm_set_dr(vcpu, dr, kvm_register_readl(vcpu, reg)))
6773 return kvm_skip_emulated_instruction(vcpu);
6776 static u64 vmx_get_dr6(struct kvm_vcpu *vcpu)
6778 return vcpu->arch.dr6;
6781 static void vmx_set_dr6(struct kvm_vcpu *vcpu, unsigned long val)
6785 static void vmx_sync_dirty_debug_regs(struct kvm_vcpu *vcpu)
6787 get_debugreg(vcpu->arch.db[0], 0);
6788 get_debugreg(vcpu->arch.db[1], 1);
6789 get_debugreg(vcpu->arch.db[2], 2);
6790 get_debugreg(vcpu->arch.db[3], 3);
6791 get_debugreg(vcpu->arch.dr6, 6);
6792 vcpu->arch.dr7 = vmcs_readl(GUEST_DR7);
6794 vcpu->arch.switch_db_regs &= ~KVM_DEBUGREG_WONT_EXIT;
6795 vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL, CPU_BASED_MOV_DR_EXITING);
6798 static void vmx_set_dr7(struct kvm_vcpu *vcpu, unsigned long val)
6800 vmcs_writel(GUEST_DR7, val);
6803 static int handle_cpuid(struct kvm_vcpu *vcpu)
6805 return kvm_emulate_cpuid(vcpu);
6808 static int handle_rdmsr(struct kvm_vcpu *vcpu)
6810 u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX];
6811 struct msr_data msr_info;
6813 msr_info.index = ecx;
6814 msr_info.host_initiated = false;
6815 if (vmx_get_msr(vcpu, &msr_info)) {
6816 trace_kvm_msr_read_ex(ecx);
6817 kvm_inject_gp(vcpu, 0);
6821 trace_kvm_msr_read(ecx, msr_info.data);
6823 /* FIXME: handling of bits 32:63 of rax, rdx */
6824 vcpu->arch.regs[VCPU_REGS_RAX] = msr_info.data & -1u;
6825 vcpu->arch.regs[VCPU_REGS_RDX] = (msr_info.data >> 32) & -1u;
6826 return kvm_skip_emulated_instruction(vcpu);
6829 static int handle_wrmsr(struct kvm_vcpu *vcpu)
6831 struct msr_data msr;
6832 u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX];
6833 u64 data = (vcpu->arch.regs[VCPU_REGS_RAX] & -1u)
6834 | ((u64)(vcpu->arch.regs[VCPU_REGS_RDX] & -1u) << 32);
6838 msr.host_initiated = false;
6839 if (kvm_set_msr(vcpu, &msr) != 0) {
6840 trace_kvm_msr_write_ex(ecx, data);
6841 kvm_inject_gp(vcpu, 0);
6845 trace_kvm_msr_write(ecx, data);
6846 return kvm_skip_emulated_instruction(vcpu);
6849 static int handle_tpr_below_threshold(struct kvm_vcpu *vcpu)
6851 kvm_apic_update_ppr(vcpu);
6855 static int handle_interrupt_window(struct kvm_vcpu *vcpu)
6857 vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL,
6858 CPU_BASED_VIRTUAL_INTR_PENDING);
6860 kvm_make_request(KVM_REQ_EVENT, vcpu);
6862 ++vcpu->stat.irq_window_exits;
6866 static int handle_halt(struct kvm_vcpu *vcpu)
6868 return kvm_emulate_halt(vcpu);
6871 static int handle_vmcall(struct kvm_vcpu *vcpu)
6873 return kvm_emulate_hypercall(vcpu);
6876 static int handle_invd(struct kvm_vcpu *vcpu)
6878 return emulate_instruction(vcpu, 0) == EMULATE_DONE;
6881 static int handle_invlpg(struct kvm_vcpu *vcpu)
6883 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
6885 kvm_mmu_invlpg(vcpu, exit_qualification);
6886 return kvm_skip_emulated_instruction(vcpu);
6889 static int handle_rdpmc(struct kvm_vcpu *vcpu)
6893 err = kvm_rdpmc(vcpu);
6894 return kvm_complete_insn_gp(vcpu, err);
6897 static int handle_wbinvd(struct kvm_vcpu *vcpu)
6899 return kvm_emulate_wbinvd(vcpu);
6902 static int handle_xsetbv(struct kvm_vcpu *vcpu)
6904 u64 new_bv = kvm_read_edx_eax(vcpu);
6905 u32 index = kvm_register_read(vcpu, VCPU_REGS_RCX);
6907 if (kvm_set_xcr(vcpu, index, new_bv) == 0)
6908 return kvm_skip_emulated_instruction(vcpu);
6912 static int handle_xsaves(struct kvm_vcpu *vcpu)
6914 kvm_skip_emulated_instruction(vcpu);
6915 WARN(1, "this should never happen\n");
6919 static int handle_xrstors(struct kvm_vcpu *vcpu)
6921 kvm_skip_emulated_instruction(vcpu);
6922 WARN(1, "this should never happen\n");
6926 static int handle_apic_access(struct kvm_vcpu *vcpu)
6928 if (likely(fasteoi)) {
6929 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
6930 int access_type, offset;
6932 access_type = exit_qualification & APIC_ACCESS_TYPE;
6933 offset = exit_qualification & APIC_ACCESS_OFFSET;
6935 * Sane guest uses MOV to write EOI, with written value
6936 * not cared. So make a short-circuit here by avoiding
6937 * heavy instruction emulation.
6939 if ((access_type == TYPE_LINEAR_APIC_INST_WRITE) &&
6940 (offset == APIC_EOI)) {
6941 kvm_lapic_set_eoi(vcpu);
6942 return kvm_skip_emulated_instruction(vcpu);
6945 return emulate_instruction(vcpu, 0) == EMULATE_DONE;
6948 static int handle_apic_eoi_induced(struct kvm_vcpu *vcpu)
6950 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
6951 int vector = exit_qualification & 0xff;
6953 /* EOI-induced VM exit is trap-like and thus no need to adjust IP */
6954 kvm_apic_set_eoi_accelerated(vcpu, vector);
6958 static int handle_apic_write(struct kvm_vcpu *vcpu)
6960 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
6961 u32 offset = exit_qualification & 0xfff;
6963 /* APIC-write VM exit is trap-like and thus no need to adjust IP */
6964 kvm_apic_write_nodecode(vcpu, offset);
6968 static int handle_task_switch(struct kvm_vcpu *vcpu)
6970 struct vcpu_vmx *vmx = to_vmx(vcpu);
6971 unsigned long exit_qualification;
6972 bool has_error_code = false;
6975 int reason, type, idt_v, idt_index;
6977 idt_v = (vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK);
6978 idt_index = (vmx->idt_vectoring_info & VECTORING_INFO_VECTOR_MASK);
6979 type = (vmx->idt_vectoring_info & VECTORING_INFO_TYPE_MASK);
6981 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
6983 reason = (u32)exit_qualification >> 30;
6984 if (reason == TASK_SWITCH_GATE && idt_v) {
6986 case INTR_TYPE_NMI_INTR:
6987 vcpu->arch.nmi_injected = false;
6988 vmx_set_nmi_mask(vcpu, true);
6990 case INTR_TYPE_EXT_INTR:
6991 case INTR_TYPE_SOFT_INTR:
6992 kvm_clear_interrupt_queue(vcpu);
6994 case INTR_TYPE_HARD_EXCEPTION:
6995 if (vmx->idt_vectoring_info &
6996 VECTORING_INFO_DELIVER_CODE_MASK) {
6997 has_error_code = true;
6999 vmcs_read32(IDT_VECTORING_ERROR_CODE);
7002 case INTR_TYPE_SOFT_EXCEPTION:
7003 kvm_clear_exception_queue(vcpu);
7009 tss_selector = exit_qualification;
7011 if (!idt_v || (type != INTR_TYPE_HARD_EXCEPTION &&
7012 type != INTR_TYPE_EXT_INTR &&
7013 type != INTR_TYPE_NMI_INTR))
7014 skip_emulated_instruction(vcpu);
7016 if (kvm_task_switch(vcpu, tss_selector,
7017 type == INTR_TYPE_SOFT_INTR ? idt_index : -1, reason,
7018 has_error_code, error_code) == EMULATE_FAIL) {
7019 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
7020 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
7021 vcpu->run->internal.ndata = 0;
7026 * TODO: What about debug traps on tss switch?
7027 * Are we supposed to inject them and update dr6?
7033 static int handle_ept_violation(struct kvm_vcpu *vcpu)
7035 unsigned long exit_qualification;
7039 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7042 * EPT violation happened while executing iret from NMI,
7043 * "blocked by NMI" bit has to be set before next VM entry.
7044 * There are errata that may cause this bit to not be set:
7047 if (!(to_vmx(vcpu)->idt_vectoring_info & VECTORING_INFO_VALID_MASK) &&
7049 (exit_qualification & INTR_INFO_UNBLOCK_NMI))
7050 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO, GUEST_INTR_STATE_NMI);
7052 gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS);
7053 trace_kvm_page_fault(gpa, exit_qualification);
7055 /* Is it a read fault? */
7056 error_code = (exit_qualification & EPT_VIOLATION_ACC_READ)
7057 ? PFERR_USER_MASK : 0;
7058 /* Is it a write fault? */
7059 error_code |= (exit_qualification & EPT_VIOLATION_ACC_WRITE)
7060 ? PFERR_WRITE_MASK : 0;
7061 /* Is it a fetch fault? */
7062 error_code |= (exit_qualification & EPT_VIOLATION_ACC_INSTR)
7063 ? PFERR_FETCH_MASK : 0;
7064 /* ept page table entry is present? */
7065 error_code |= (exit_qualification &
7066 (EPT_VIOLATION_READABLE | EPT_VIOLATION_WRITABLE |
7067 EPT_VIOLATION_EXECUTABLE))
7068 ? PFERR_PRESENT_MASK : 0;
7070 error_code |= (exit_qualification & 0x100) != 0 ?
7071 PFERR_GUEST_FINAL_MASK : PFERR_GUEST_PAGE_MASK;
7073 vcpu->arch.exit_qualification = exit_qualification;
7074 return kvm_mmu_page_fault(vcpu, gpa, error_code, NULL, 0);
7077 static int handle_ept_misconfig(struct kvm_vcpu *vcpu)
7082 * A nested guest cannot optimize MMIO vmexits, because we have an
7083 * nGPA here instead of the required GPA.
7085 gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS);
7086 if (!is_guest_mode(vcpu) &&
7087 !kvm_io_bus_write(vcpu, KVM_FAST_MMIO_BUS, gpa, 0, NULL)) {
7088 trace_kvm_fast_mmio(gpa);
7090 * Doing kvm_skip_emulated_instruction() depends on undefined
7091 * behavior: Intel's manual doesn't mandate
7092 * VM_EXIT_INSTRUCTION_LEN to be set in VMCS when EPT MISCONFIG
7093 * occurs and while on real hardware it was observed to be set,
7094 * other hypervisors (namely Hyper-V) don't set it, we end up
7095 * advancing IP with some random value. Disable fast mmio when
7096 * running nested and keep it for real hardware in hope that
7097 * VM_EXIT_INSTRUCTION_LEN will always be set correctly.
7099 if (!static_cpu_has(X86_FEATURE_HYPERVISOR))
7100 return kvm_skip_emulated_instruction(vcpu);
7102 return x86_emulate_instruction(vcpu, gpa, EMULTYPE_SKIP,
7103 NULL, 0) == EMULATE_DONE;
7106 return kvm_mmu_page_fault(vcpu, gpa, PFERR_RSVD_MASK, NULL, 0);
7109 static int handle_nmi_window(struct kvm_vcpu *vcpu)
7111 WARN_ON_ONCE(!enable_vnmi);
7112 vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL,
7113 CPU_BASED_VIRTUAL_NMI_PENDING);
7114 ++vcpu->stat.nmi_window_exits;
7115 kvm_make_request(KVM_REQ_EVENT, vcpu);
7120 static int handle_invalid_guest_state(struct kvm_vcpu *vcpu)
7122 struct vcpu_vmx *vmx = to_vmx(vcpu);
7123 enum emulation_result err = EMULATE_DONE;
7126 bool intr_window_requested;
7127 unsigned count = 130;
7130 * We should never reach the point where we are emulating L2
7131 * due to invalid guest state as that means we incorrectly
7132 * allowed a nested VMEntry with an invalid vmcs12.
7134 WARN_ON_ONCE(vmx->emulation_required && vmx->nested.nested_run_pending);
7136 cpu_exec_ctrl = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
7137 intr_window_requested = cpu_exec_ctrl & CPU_BASED_VIRTUAL_INTR_PENDING;
7139 while (vmx->emulation_required && count-- != 0) {
7140 if (intr_window_requested && vmx_interrupt_allowed(vcpu))
7141 return handle_interrupt_window(&vmx->vcpu);
7143 if (kvm_test_request(KVM_REQ_EVENT, vcpu))
7146 err = emulate_instruction(vcpu, 0);
7148 if (err == EMULATE_USER_EXIT) {
7149 ++vcpu->stat.mmio_exits;
7154 if (err != EMULATE_DONE)
7155 goto emulation_error;
7157 if (vmx->emulation_required && !vmx->rmode.vm86_active &&
7158 vcpu->arch.exception.pending)
7159 goto emulation_error;
7161 if (vcpu->arch.halt_request) {
7162 vcpu->arch.halt_request = 0;
7163 ret = kvm_vcpu_halt(vcpu);
7167 if (signal_pending(current))
7177 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
7178 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
7179 vcpu->run->internal.ndata = 0;
7183 static void grow_ple_window(struct kvm_vcpu *vcpu)
7185 struct vcpu_vmx *vmx = to_vmx(vcpu);
7186 int old = vmx->ple_window;
7188 vmx->ple_window = __grow_ple_window(old, ple_window,
7192 if (vmx->ple_window != old)
7193 vmx->ple_window_dirty = true;
7195 trace_kvm_ple_window_grow(vcpu->vcpu_id, vmx->ple_window, old);
7198 static void shrink_ple_window(struct kvm_vcpu *vcpu)
7200 struct vcpu_vmx *vmx = to_vmx(vcpu);
7201 int old = vmx->ple_window;
7203 vmx->ple_window = __shrink_ple_window(old, ple_window,
7207 if (vmx->ple_window != old)
7208 vmx->ple_window_dirty = true;
7210 trace_kvm_ple_window_shrink(vcpu->vcpu_id, vmx->ple_window, old);
7214 * Handler for POSTED_INTERRUPT_WAKEUP_VECTOR.
7216 static void wakeup_handler(void)
7218 struct kvm_vcpu *vcpu;
7219 int cpu = smp_processor_id();
7221 spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, cpu));
7222 list_for_each_entry(vcpu, &per_cpu(blocked_vcpu_on_cpu, cpu),
7223 blocked_vcpu_list) {
7224 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
7226 if (pi_test_on(pi_desc) == 1)
7227 kvm_vcpu_kick(vcpu);
7229 spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, cpu));
7232 static void vmx_enable_tdp(void)
7234 kvm_mmu_set_mask_ptes(VMX_EPT_READABLE_MASK,
7235 enable_ept_ad_bits ? VMX_EPT_ACCESS_BIT : 0ull,
7236 enable_ept_ad_bits ? VMX_EPT_DIRTY_BIT : 0ull,
7237 0ull, VMX_EPT_EXECUTABLE_MASK,
7238 cpu_has_vmx_ept_execute_only() ? 0ull : VMX_EPT_READABLE_MASK,
7239 VMX_EPT_RWX_MASK, 0ull);
7241 ept_set_mmio_spte_mask();
7245 static __init int hardware_setup(void)
7249 rdmsrl_safe(MSR_EFER, &host_efer);
7251 for (i = 0; i < ARRAY_SIZE(vmx_msr_index); ++i)
7252 kvm_define_shared_msr(i, vmx_msr_index[i]);
7254 for (i = 0; i < VMX_BITMAP_NR; i++) {
7255 vmx_bitmap[i] = (unsigned long *)__get_free_page(GFP_KERNEL);
7260 memset(vmx_vmread_bitmap, 0xff, PAGE_SIZE);
7261 memset(vmx_vmwrite_bitmap, 0xff, PAGE_SIZE);
7263 if (setup_vmcs_config(&vmcs_config) < 0) {
7268 if (boot_cpu_has(X86_FEATURE_NX))
7269 kvm_enable_efer_bits(EFER_NX);
7271 if (!cpu_has_vmx_vpid() || !cpu_has_vmx_invvpid() ||
7272 !(cpu_has_vmx_invvpid_single() || cpu_has_vmx_invvpid_global()))
7275 if (!cpu_has_vmx_ept() ||
7276 !cpu_has_vmx_ept_4levels() ||
7277 !cpu_has_vmx_ept_mt_wb() ||
7278 !cpu_has_vmx_invept_global())
7281 if (!cpu_has_vmx_ept_ad_bits() || !enable_ept)
7282 enable_ept_ad_bits = 0;
7284 if (!cpu_has_vmx_unrestricted_guest() || !enable_ept)
7285 enable_unrestricted_guest = 0;
7287 if (!cpu_has_vmx_flexpriority())
7288 flexpriority_enabled = 0;
7290 if (!cpu_has_virtual_nmis())
7294 * set_apic_access_page_addr() is used to reload apic access
7295 * page upon invalidation. No need to do anything if not
7296 * using the APIC_ACCESS_ADDR VMCS field.
7298 if (!flexpriority_enabled)
7299 kvm_x86_ops->set_apic_access_page_addr = NULL;
7301 if (!cpu_has_vmx_tpr_shadow())
7302 kvm_x86_ops->update_cr8_intercept = NULL;
7304 if (enable_ept && !cpu_has_vmx_ept_2m_page())
7305 kvm_disable_largepages();
7307 if (!cpu_has_vmx_ple()) {
7310 ple_window_grow = 0;
7312 ple_window_shrink = 0;
7315 if (!cpu_has_vmx_apicv()) {
7317 kvm_x86_ops->sync_pir_to_irr = NULL;
7320 if (cpu_has_vmx_tsc_scaling()) {
7321 kvm_has_tsc_control = true;
7322 kvm_max_tsc_scaling_ratio = KVM_VMX_TSC_MULTIPLIER_MAX;
7323 kvm_tsc_scaling_ratio_frac_bits = 48;
7326 set_bit(0, vmx_vpid_bitmap); /* 0 is reserved for host */
7334 * Only enable PML when hardware supports PML feature, and both EPT
7335 * and EPT A/D bit features are enabled -- PML depends on them to work.
7337 if (!enable_ept || !enable_ept_ad_bits || !cpu_has_vmx_pml())
7341 kvm_x86_ops->slot_enable_log_dirty = NULL;
7342 kvm_x86_ops->slot_disable_log_dirty = NULL;
7343 kvm_x86_ops->flush_log_dirty = NULL;
7344 kvm_x86_ops->enable_log_dirty_pt_masked = NULL;
7347 if (cpu_has_vmx_preemption_timer() && enable_preemption_timer) {
7350 rdmsrl(MSR_IA32_VMX_MISC, vmx_msr);
7351 cpu_preemption_timer_multi =
7352 vmx_msr & VMX_MISC_PREEMPTION_TIMER_RATE_MASK;
7354 kvm_x86_ops->set_hv_timer = NULL;
7355 kvm_x86_ops->cancel_hv_timer = NULL;
7358 if (!cpu_has_vmx_shadow_vmcs())
7359 enable_shadow_vmcs = 0;
7360 if (enable_shadow_vmcs)
7361 init_vmcs_shadow_fields();
7363 kvm_set_posted_intr_wakeup_handler(wakeup_handler);
7364 nested_vmx_setup_ctls_msrs(&vmcs_config.nested, enable_apicv);
7366 kvm_mce_cap_supported |= MCG_LMCE_P;
7368 return alloc_kvm_area();
7371 for (i = 0; i < VMX_BITMAP_NR; i++)
7372 free_page((unsigned long)vmx_bitmap[i]);
7377 static __exit void hardware_unsetup(void)
7381 for (i = 0; i < VMX_BITMAP_NR; i++)
7382 free_page((unsigned long)vmx_bitmap[i]);
7388 * Indicate a busy-waiting vcpu in spinlock. We do not enable the PAUSE
7389 * exiting, so only get here on cpu with PAUSE-Loop-Exiting.
7391 static int handle_pause(struct kvm_vcpu *vcpu)
7393 if (!kvm_pause_in_guest(vcpu->kvm))
7394 grow_ple_window(vcpu);
7397 * Intel sdm vol3 ch-25.1.3 says: The "PAUSE-loop exiting"
7398 * VM-execution control is ignored if CPL > 0. OTOH, KVM
7399 * never set PAUSE_EXITING and just set PLE if supported,
7400 * so the vcpu must be CPL=0 if it gets a PAUSE exit.
7402 kvm_vcpu_on_spin(vcpu, true);
7403 return kvm_skip_emulated_instruction(vcpu);
7406 static int handle_nop(struct kvm_vcpu *vcpu)
7408 return kvm_skip_emulated_instruction(vcpu);
7411 static int handle_mwait(struct kvm_vcpu *vcpu)
7413 printk_once(KERN_WARNING "kvm: MWAIT instruction emulated as NOP!\n");
7414 return handle_nop(vcpu);
7417 static int handle_invalid_op(struct kvm_vcpu *vcpu)
7419 kvm_queue_exception(vcpu, UD_VECTOR);
7423 static int handle_monitor_trap(struct kvm_vcpu *vcpu)
7428 static int handle_monitor(struct kvm_vcpu *vcpu)
7430 printk_once(KERN_WARNING "kvm: MONITOR instruction emulated as NOP!\n");
7431 return handle_nop(vcpu);
7435 * The following 3 functions, nested_vmx_succeed()/failValid()/failInvalid(),
7436 * set the success or error code of an emulated VMX instruction, as specified
7437 * by Vol 2B, VMX Instruction Reference, "Conventions".
7439 static void nested_vmx_succeed(struct kvm_vcpu *vcpu)
7441 vmx_set_rflags(vcpu, vmx_get_rflags(vcpu)
7442 & ~(X86_EFLAGS_CF | X86_EFLAGS_PF | X86_EFLAGS_AF |
7443 X86_EFLAGS_ZF | X86_EFLAGS_SF | X86_EFLAGS_OF));
7446 static void nested_vmx_failInvalid(struct kvm_vcpu *vcpu)
7448 vmx_set_rflags(vcpu, (vmx_get_rflags(vcpu)
7449 & ~(X86_EFLAGS_PF | X86_EFLAGS_AF | X86_EFLAGS_ZF |
7450 X86_EFLAGS_SF | X86_EFLAGS_OF))
7454 static void nested_vmx_failValid(struct kvm_vcpu *vcpu,
7455 u32 vm_instruction_error)
7457 if (to_vmx(vcpu)->nested.current_vmptr == -1ull) {
7459 * failValid writes the error number to the current VMCS, which
7460 * can't be done there isn't a current VMCS.
7462 nested_vmx_failInvalid(vcpu);
7465 vmx_set_rflags(vcpu, (vmx_get_rflags(vcpu)
7466 & ~(X86_EFLAGS_CF | X86_EFLAGS_PF | X86_EFLAGS_AF |
7467 X86_EFLAGS_SF | X86_EFLAGS_OF))
7469 get_vmcs12(vcpu)->vm_instruction_error = vm_instruction_error;
7471 * We don't need to force a shadow sync because
7472 * VM_INSTRUCTION_ERROR is not shadowed
7476 static void nested_vmx_abort(struct kvm_vcpu *vcpu, u32 indicator)
7478 /* TODO: not to reset guest simply here. */
7479 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
7480 pr_debug_ratelimited("kvm: nested vmx abort, indicator %d\n", indicator);
7483 static enum hrtimer_restart vmx_preemption_timer_fn(struct hrtimer *timer)
7485 struct vcpu_vmx *vmx =
7486 container_of(timer, struct vcpu_vmx, nested.preemption_timer);
7488 vmx->nested.preemption_timer_expired = true;
7489 kvm_make_request(KVM_REQ_EVENT, &vmx->vcpu);
7490 kvm_vcpu_kick(&vmx->vcpu);
7492 return HRTIMER_NORESTART;
7496 * Decode the memory-address operand of a vmx instruction, as recorded on an
7497 * exit caused by such an instruction (run by a guest hypervisor).
7498 * On success, returns 0. When the operand is invalid, returns 1 and throws
7501 static int get_vmx_mem_address(struct kvm_vcpu *vcpu,
7502 unsigned long exit_qualification,
7503 u32 vmx_instruction_info, bool wr, gva_t *ret)
7507 struct kvm_segment s;
7510 * According to Vol. 3B, "Information for VM Exits Due to Instruction
7511 * Execution", on an exit, vmx_instruction_info holds most of the
7512 * addressing components of the operand. Only the displacement part
7513 * is put in exit_qualification (see 3B, "Basic VM-Exit Information").
7514 * For how an actual address is calculated from all these components,
7515 * refer to Vol. 1, "Operand Addressing".
7517 int scaling = vmx_instruction_info & 3;
7518 int addr_size = (vmx_instruction_info >> 7) & 7;
7519 bool is_reg = vmx_instruction_info & (1u << 10);
7520 int seg_reg = (vmx_instruction_info >> 15) & 7;
7521 int index_reg = (vmx_instruction_info >> 18) & 0xf;
7522 bool index_is_valid = !(vmx_instruction_info & (1u << 22));
7523 int base_reg = (vmx_instruction_info >> 23) & 0xf;
7524 bool base_is_valid = !(vmx_instruction_info & (1u << 27));
7527 kvm_queue_exception(vcpu, UD_VECTOR);
7531 /* Addr = segment_base + offset */
7532 /* offset = base + [index * scale] + displacement */
7533 off = exit_qualification; /* holds the displacement */
7535 off += kvm_register_read(vcpu, base_reg);
7537 off += kvm_register_read(vcpu, index_reg)<<scaling;
7538 vmx_get_segment(vcpu, &s, seg_reg);
7539 *ret = s.base + off;
7541 if (addr_size == 1) /* 32 bit */
7544 /* Checks for #GP/#SS exceptions. */
7546 if (is_long_mode(vcpu)) {
7547 /* Long mode: #GP(0)/#SS(0) if the memory address is in a
7548 * non-canonical form. This is the only check on the memory
7549 * destination for long mode!
7551 exn = is_noncanonical_address(*ret, vcpu);
7552 } else if (is_protmode(vcpu)) {
7553 /* Protected mode: apply checks for segment validity in the
7555 * - segment type check (#GP(0) may be thrown)
7556 * - usability check (#GP(0)/#SS(0))
7557 * - limit check (#GP(0)/#SS(0))
7560 /* #GP(0) if the destination operand is located in a
7561 * read-only data segment or any code segment.
7563 exn = ((s.type & 0xa) == 0 || (s.type & 8));
7565 /* #GP(0) if the source operand is located in an
7566 * execute-only code segment
7568 exn = ((s.type & 0xa) == 8);
7570 kvm_queue_exception_e(vcpu, GP_VECTOR, 0);
7573 /* Protected mode: #GP(0)/#SS(0) if the segment is unusable.
7575 exn = (s.unusable != 0);
7576 /* Protected mode: #GP(0)/#SS(0) if the memory
7577 * operand is outside the segment limit.
7579 exn = exn || (off + sizeof(u64) > s.limit);
7582 kvm_queue_exception_e(vcpu,
7583 seg_reg == VCPU_SREG_SS ?
7584 SS_VECTOR : GP_VECTOR,
7592 static int nested_vmx_get_vmptr(struct kvm_vcpu *vcpu, gpa_t *vmpointer)
7595 struct x86_exception e;
7597 if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
7598 vmcs_read32(VMX_INSTRUCTION_INFO), false, &gva))
7601 if (kvm_read_guest_virt(&vcpu->arch.emulate_ctxt, gva, vmpointer,
7602 sizeof(*vmpointer), &e)) {
7603 kvm_inject_page_fault(vcpu, &e);
7610 static int enter_vmx_operation(struct kvm_vcpu *vcpu)
7612 struct vcpu_vmx *vmx = to_vmx(vcpu);
7613 struct vmcs *shadow_vmcs;
7616 r = alloc_loaded_vmcs(&vmx->nested.vmcs02);
7620 vmx->nested.cached_vmcs12 = kmalloc(VMCS12_SIZE, GFP_KERNEL);
7621 if (!vmx->nested.cached_vmcs12)
7622 goto out_cached_vmcs12;
7624 if (enable_shadow_vmcs) {
7625 shadow_vmcs = alloc_vmcs();
7627 goto out_shadow_vmcs;
7628 /* mark vmcs as shadow */
7629 shadow_vmcs->revision_id |= (1u << 31);
7630 /* init shadow vmcs */
7631 vmcs_clear(shadow_vmcs);
7632 vmx->vmcs01.shadow_vmcs = shadow_vmcs;
7635 hrtimer_init(&vmx->nested.preemption_timer, CLOCK_MONOTONIC,
7636 HRTIMER_MODE_REL_PINNED);
7637 vmx->nested.preemption_timer.function = vmx_preemption_timer_fn;
7639 vmx->nested.vmxon = true;
7643 kfree(vmx->nested.cached_vmcs12);
7646 free_loaded_vmcs(&vmx->nested.vmcs02);
7653 * Emulate the VMXON instruction.
7654 * Currently, we just remember that VMX is active, and do not save or even
7655 * inspect the argument to VMXON (the so-called "VMXON pointer") because we
7656 * do not currently need to store anything in that guest-allocated memory
7657 * region. Consequently, VMCLEAR and VMPTRLD also do not verify that the their
7658 * argument is different from the VMXON pointer (which the spec says they do).
7660 static int handle_vmon(struct kvm_vcpu *vcpu)
7665 struct vcpu_vmx *vmx = to_vmx(vcpu);
7666 const u64 VMXON_NEEDED_FEATURES = FEATURE_CONTROL_LOCKED
7667 | FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
7670 * The Intel VMX Instruction Reference lists a bunch of bits that are
7671 * prerequisite to running VMXON, most notably cr4.VMXE must be set to
7672 * 1 (see vmx_set_cr4() for when we allow the guest to set this).
7673 * Otherwise, we should fail with #UD. But most faulting conditions
7674 * have already been checked by hardware, prior to the VM-exit for
7675 * VMXON. We do test guest cr4.VMXE because processor CR4 always has
7676 * that bit set to 1 in non-root mode.
7678 if (!kvm_read_cr4_bits(vcpu, X86_CR4_VMXE)) {
7679 kvm_queue_exception(vcpu, UD_VECTOR);
7683 if (vmx->nested.vmxon) {
7684 nested_vmx_failValid(vcpu, VMXERR_VMXON_IN_VMX_ROOT_OPERATION);
7685 return kvm_skip_emulated_instruction(vcpu);
7688 if ((vmx->msr_ia32_feature_control & VMXON_NEEDED_FEATURES)
7689 != VMXON_NEEDED_FEATURES) {
7690 kvm_inject_gp(vcpu, 0);
7694 if (nested_vmx_get_vmptr(vcpu, &vmptr))
7699 * The first 4 bytes of VMXON region contain the supported
7700 * VMCS revision identifier
7702 * Note - IA32_VMX_BASIC[48] will never be 1 for the nested case;
7703 * which replaces physical address width with 32
7705 if (!PAGE_ALIGNED(vmptr) || (vmptr >> cpuid_maxphyaddr(vcpu))) {
7706 nested_vmx_failInvalid(vcpu);
7707 return kvm_skip_emulated_instruction(vcpu);
7710 page = kvm_vcpu_gpa_to_page(vcpu, vmptr);
7711 if (is_error_page(page)) {
7712 nested_vmx_failInvalid(vcpu);
7713 return kvm_skip_emulated_instruction(vcpu);
7715 if (*(u32 *)kmap(page) != VMCS12_REVISION) {
7717 kvm_release_page_clean(page);
7718 nested_vmx_failInvalid(vcpu);
7719 return kvm_skip_emulated_instruction(vcpu);
7722 kvm_release_page_clean(page);
7724 vmx->nested.vmxon_ptr = vmptr;
7725 ret = enter_vmx_operation(vcpu);
7729 nested_vmx_succeed(vcpu);
7730 return kvm_skip_emulated_instruction(vcpu);
7734 * Intel's VMX Instruction Reference specifies a common set of prerequisites
7735 * for running VMX instructions (except VMXON, whose prerequisites are
7736 * slightly different). It also specifies what exception to inject otherwise.
7737 * Note that many of these exceptions have priority over VM exits, so they
7738 * don't have to be checked again here.
7740 static int nested_vmx_check_permission(struct kvm_vcpu *vcpu)
7742 if (!to_vmx(vcpu)->nested.vmxon) {
7743 kvm_queue_exception(vcpu, UD_VECTOR);
7749 static void vmx_disable_shadow_vmcs(struct vcpu_vmx *vmx)
7751 vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL, SECONDARY_EXEC_SHADOW_VMCS);
7752 vmcs_write64(VMCS_LINK_POINTER, -1ull);
7755 static inline void nested_release_vmcs12(struct vcpu_vmx *vmx)
7757 if (vmx->nested.current_vmptr == -1ull)
7760 if (enable_shadow_vmcs) {
7761 /* copy to memory all shadowed fields in case
7762 they were modified */
7763 copy_shadow_to_vmcs12(vmx);
7764 vmx->nested.sync_shadow_vmcs = false;
7765 vmx_disable_shadow_vmcs(vmx);
7767 vmx->nested.posted_intr_nv = -1;
7769 /* Flush VMCS12 to guest memory */
7770 kvm_vcpu_write_guest_page(&vmx->vcpu,
7771 vmx->nested.current_vmptr >> PAGE_SHIFT,
7772 vmx->nested.cached_vmcs12, 0, VMCS12_SIZE);
7774 vmx->nested.current_vmptr = -1ull;
7778 * Free whatever needs to be freed from vmx->nested when L1 goes down, or
7779 * just stops using VMX.
7781 static void free_nested(struct vcpu_vmx *vmx)
7783 if (!vmx->nested.vmxon && !vmx->nested.smm.vmxon)
7786 vmx->nested.vmxon = false;
7787 vmx->nested.smm.vmxon = false;
7788 free_vpid(vmx->nested.vpid02);
7789 vmx->nested.posted_intr_nv = -1;
7790 vmx->nested.current_vmptr = -1ull;
7791 if (enable_shadow_vmcs) {
7792 vmx_disable_shadow_vmcs(vmx);
7793 vmcs_clear(vmx->vmcs01.shadow_vmcs);
7794 free_vmcs(vmx->vmcs01.shadow_vmcs);
7795 vmx->vmcs01.shadow_vmcs = NULL;
7797 kfree(vmx->nested.cached_vmcs12);
7798 /* Unpin physical memory we referred to in the vmcs02 */
7799 if (vmx->nested.apic_access_page) {
7800 kvm_release_page_dirty(vmx->nested.apic_access_page);
7801 vmx->nested.apic_access_page = NULL;
7803 if (vmx->nested.virtual_apic_page) {
7804 kvm_release_page_dirty(vmx->nested.virtual_apic_page);
7805 vmx->nested.virtual_apic_page = NULL;
7807 if (vmx->nested.pi_desc_page) {
7808 kunmap(vmx->nested.pi_desc_page);
7809 kvm_release_page_dirty(vmx->nested.pi_desc_page);
7810 vmx->nested.pi_desc_page = NULL;
7811 vmx->nested.pi_desc = NULL;
7814 free_loaded_vmcs(&vmx->nested.vmcs02);
7817 /* Emulate the VMXOFF instruction */
7818 static int handle_vmoff(struct kvm_vcpu *vcpu)
7820 if (!nested_vmx_check_permission(vcpu))
7822 free_nested(to_vmx(vcpu));
7823 nested_vmx_succeed(vcpu);
7824 return kvm_skip_emulated_instruction(vcpu);
7827 /* Emulate the VMCLEAR instruction */
7828 static int handle_vmclear(struct kvm_vcpu *vcpu)
7830 struct vcpu_vmx *vmx = to_vmx(vcpu);
7834 if (!nested_vmx_check_permission(vcpu))
7837 if (nested_vmx_get_vmptr(vcpu, &vmptr))
7840 if (!PAGE_ALIGNED(vmptr) || (vmptr >> cpuid_maxphyaddr(vcpu))) {
7841 nested_vmx_failValid(vcpu, VMXERR_VMCLEAR_INVALID_ADDRESS);
7842 return kvm_skip_emulated_instruction(vcpu);
7845 if (vmptr == vmx->nested.vmxon_ptr) {
7846 nested_vmx_failValid(vcpu, VMXERR_VMCLEAR_VMXON_POINTER);
7847 return kvm_skip_emulated_instruction(vcpu);
7850 if (vmptr == vmx->nested.current_vmptr)
7851 nested_release_vmcs12(vmx);
7853 kvm_vcpu_write_guest(vcpu,
7854 vmptr + offsetof(struct vmcs12, launch_state),
7855 &zero, sizeof(zero));
7857 nested_vmx_succeed(vcpu);
7858 return kvm_skip_emulated_instruction(vcpu);
7861 static int nested_vmx_run(struct kvm_vcpu *vcpu, bool launch);
7863 /* Emulate the VMLAUNCH instruction */
7864 static int handle_vmlaunch(struct kvm_vcpu *vcpu)
7866 return nested_vmx_run(vcpu, true);
7869 /* Emulate the VMRESUME instruction */
7870 static int handle_vmresume(struct kvm_vcpu *vcpu)
7873 return nested_vmx_run(vcpu, false);
7877 * Read a vmcs12 field. Since these can have varying lengths and we return
7878 * one type, we chose the biggest type (u64) and zero-extend the return value
7879 * to that size. Note that the caller, handle_vmread, might need to use only
7880 * some of the bits we return here (e.g., on 32-bit guests, only 32 bits of
7881 * 64-bit fields are to be returned).
7883 static inline int vmcs12_read_any(struct kvm_vcpu *vcpu,
7884 unsigned long field, u64 *ret)
7886 short offset = vmcs_field_to_offset(field);
7892 p = ((char *)(get_vmcs12(vcpu))) + offset;
7894 switch (vmcs_field_width(field)) {
7895 case VMCS_FIELD_WIDTH_NATURAL_WIDTH:
7896 *ret = *((natural_width *)p);
7898 case VMCS_FIELD_WIDTH_U16:
7901 case VMCS_FIELD_WIDTH_U32:
7904 case VMCS_FIELD_WIDTH_U64:
7914 static inline int vmcs12_write_any(struct kvm_vcpu *vcpu,
7915 unsigned long field, u64 field_value){
7916 short offset = vmcs_field_to_offset(field);
7917 char *p = ((char *) get_vmcs12(vcpu)) + offset;
7921 switch (vmcs_field_width(field)) {
7922 case VMCS_FIELD_WIDTH_U16:
7923 *(u16 *)p = field_value;
7925 case VMCS_FIELD_WIDTH_U32:
7926 *(u32 *)p = field_value;
7928 case VMCS_FIELD_WIDTH_U64:
7929 *(u64 *)p = field_value;
7931 case VMCS_FIELD_WIDTH_NATURAL_WIDTH:
7932 *(natural_width *)p = field_value;
7941 static void copy_shadow_to_vmcs12(struct vcpu_vmx *vmx)
7944 unsigned long field;
7946 struct vmcs *shadow_vmcs = vmx->vmcs01.shadow_vmcs;
7947 const u16 *fields = shadow_read_write_fields;
7948 const int num_fields = max_shadow_read_write_fields;
7952 vmcs_load(shadow_vmcs);
7954 for (i = 0; i < num_fields; i++) {
7956 field_value = __vmcs_readl(field);
7957 vmcs12_write_any(&vmx->vcpu, field, field_value);
7960 vmcs_clear(shadow_vmcs);
7961 vmcs_load(vmx->loaded_vmcs->vmcs);
7966 static void copy_vmcs12_to_shadow(struct vcpu_vmx *vmx)
7968 const u16 *fields[] = {
7969 shadow_read_write_fields,
7970 shadow_read_only_fields
7972 const int max_fields[] = {
7973 max_shadow_read_write_fields,
7974 max_shadow_read_only_fields
7977 unsigned long field;
7978 u64 field_value = 0;
7979 struct vmcs *shadow_vmcs = vmx->vmcs01.shadow_vmcs;
7981 vmcs_load(shadow_vmcs);
7983 for (q = 0; q < ARRAY_SIZE(fields); q++) {
7984 for (i = 0; i < max_fields[q]; i++) {
7985 field = fields[q][i];
7986 vmcs12_read_any(&vmx->vcpu, field, &field_value);
7987 __vmcs_writel(field, field_value);
7991 vmcs_clear(shadow_vmcs);
7992 vmcs_load(vmx->loaded_vmcs->vmcs);
7996 * VMX instructions which assume a current vmcs12 (i.e., that VMPTRLD was
7997 * used before) all generate the same failure when it is missing.
7999 static int nested_vmx_check_vmcs12(struct kvm_vcpu *vcpu)
8001 struct vcpu_vmx *vmx = to_vmx(vcpu);
8002 if (vmx->nested.current_vmptr == -1ull) {
8003 nested_vmx_failInvalid(vcpu);
8009 static int handle_vmread(struct kvm_vcpu *vcpu)
8011 unsigned long field;
8013 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
8014 u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
8017 if (!nested_vmx_check_permission(vcpu))
8020 if (!nested_vmx_check_vmcs12(vcpu))
8021 return kvm_skip_emulated_instruction(vcpu);
8023 /* Decode instruction info and find the field to read */
8024 field = kvm_register_readl(vcpu, (((vmx_instruction_info) >> 28) & 0xf));
8025 /* Read the field, zero-extended to a u64 field_value */
8026 if (vmcs12_read_any(vcpu, field, &field_value) < 0) {
8027 nested_vmx_failValid(vcpu, VMXERR_UNSUPPORTED_VMCS_COMPONENT);
8028 return kvm_skip_emulated_instruction(vcpu);
8031 * Now copy part of this value to register or memory, as requested.
8032 * Note that the number of bits actually copied is 32 or 64 depending
8033 * on the guest's mode (32 or 64 bit), not on the given field's length.
8035 if (vmx_instruction_info & (1u << 10)) {
8036 kvm_register_writel(vcpu, (((vmx_instruction_info) >> 3) & 0xf),
8039 if (get_vmx_mem_address(vcpu, exit_qualification,
8040 vmx_instruction_info, true, &gva))
8042 /* _system ok, as hardware has verified cpl=0 */
8043 kvm_write_guest_virt_system(&vcpu->arch.emulate_ctxt, gva,
8044 &field_value, (is_long_mode(vcpu) ? 8 : 4), NULL);
8047 nested_vmx_succeed(vcpu);
8048 return kvm_skip_emulated_instruction(vcpu);
8052 static int handle_vmwrite(struct kvm_vcpu *vcpu)
8054 unsigned long field;
8056 struct vcpu_vmx *vmx = to_vmx(vcpu);
8057 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
8058 u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
8060 /* The value to write might be 32 or 64 bits, depending on L1's long
8061 * mode, and eventually we need to write that into a field of several
8062 * possible lengths. The code below first zero-extends the value to 64
8063 * bit (field_value), and then copies only the appropriate number of
8064 * bits into the vmcs12 field.
8066 u64 field_value = 0;
8067 struct x86_exception e;
8069 if (!nested_vmx_check_permission(vcpu))
8072 if (!nested_vmx_check_vmcs12(vcpu))
8073 return kvm_skip_emulated_instruction(vcpu);
8075 if (vmx_instruction_info & (1u << 10))
8076 field_value = kvm_register_readl(vcpu,
8077 (((vmx_instruction_info) >> 3) & 0xf));
8079 if (get_vmx_mem_address(vcpu, exit_qualification,
8080 vmx_instruction_info, false, &gva))
8082 if (kvm_read_guest_virt(&vcpu->arch.emulate_ctxt, gva,
8083 &field_value, (is_64_bit_mode(vcpu) ? 8 : 4), &e)) {
8084 kvm_inject_page_fault(vcpu, &e);
8090 field = kvm_register_readl(vcpu, (((vmx_instruction_info) >> 28) & 0xf));
8091 if (vmcs_field_readonly(field)) {
8092 nested_vmx_failValid(vcpu,
8093 VMXERR_VMWRITE_READ_ONLY_VMCS_COMPONENT);
8094 return kvm_skip_emulated_instruction(vcpu);
8097 if (vmcs12_write_any(vcpu, field, field_value) < 0) {
8098 nested_vmx_failValid(vcpu, VMXERR_UNSUPPORTED_VMCS_COMPONENT);
8099 return kvm_skip_emulated_instruction(vcpu);
8103 #define SHADOW_FIELD_RW(x) case x:
8104 #include "vmx_shadow_fields.h"
8106 * The fields that can be updated by L1 without a vmexit are
8107 * always updated in the vmcs02, the others go down the slow
8108 * path of prepare_vmcs02.
8112 vmx->nested.dirty_vmcs12 = true;
8116 nested_vmx_succeed(vcpu);
8117 return kvm_skip_emulated_instruction(vcpu);
8120 static void set_current_vmptr(struct vcpu_vmx *vmx, gpa_t vmptr)
8122 vmx->nested.current_vmptr = vmptr;
8123 if (enable_shadow_vmcs) {
8124 vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL,
8125 SECONDARY_EXEC_SHADOW_VMCS);
8126 vmcs_write64(VMCS_LINK_POINTER,
8127 __pa(vmx->vmcs01.shadow_vmcs));
8128 vmx->nested.sync_shadow_vmcs = true;
8130 vmx->nested.dirty_vmcs12 = true;
8133 /* Emulate the VMPTRLD instruction */
8134 static int handle_vmptrld(struct kvm_vcpu *vcpu)
8136 struct vcpu_vmx *vmx = to_vmx(vcpu);
8139 if (!nested_vmx_check_permission(vcpu))
8142 if (nested_vmx_get_vmptr(vcpu, &vmptr))
8145 if (!PAGE_ALIGNED(vmptr) || (vmptr >> cpuid_maxphyaddr(vcpu))) {
8146 nested_vmx_failValid(vcpu, VMXERR_VMPTRLD_INVALID_ADDRESS);
8147 return kvm_skip_emulated_instruction(vcpu);
8150 if (vmptr == vmx->nested.vmxon_ptr) {
8151 nested_vmx_failValid(vcpu, VMXERR_VMPTRLD_VMXON_POINTER);
8152 return kvm_skip_emulated_instruction(vcpu);
8155 if (vmx->nested.current_vmptr != vmptr) {
8156 struct vmcs12 *new_vmcs12;
8158 page = kvm_vcpu_gpa_to_page(vcpu, vmptr);
8159 if (is_error_page(page)) {
8160 nested_vmx_failInvalid(vcpu);
8161 return kvm_skip_emulated_instruction(vcpu);
8163 new_vmcs12 = kmap(page);
8164 if (new_vmcs12->revision_id != VMCS12_REVISION) {
8166 kvm_release_page_clean(page);
8167 nested_vmx_failValid(vcpu,
8168 VMXERR_VMPTRLD_INCORRECT_VMCS_REVISION_ID);
8169 return kvm_skip_emulated_instruction(vcpu);
8172 nested_release_vmcs12(vmx);
8174 * Load VMCS12 from guest memory since it is not already
8177 memcpy(vmx->nested.cached_vmcs12, new_vmcs12, VMCS12_SIZE);
8179 kvm_release_page_clean(page);
8181 set_current_vmptr(vmx, vmptr);
8184 nested_vmx_succeed(vcpu);
8185 return kvm_skip_emulated_instruction(vcpu);
8188 /* Emulate the VMPTRST instruction */
8189 static int handle_vmptrst(struct kvm_vcpu *vcpu)
8191 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
8192 u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
8194 struct x86_exception e;
8196 if (!nested_vmx_check_permission(vcpu))
8199 if (get_vmx_mem_address(vcpu, exit_qualification,
8200 vmx_instruction_info, true, &vmcs_gva))
8202 /* ok to use *_system, as hardware has verified cpl=0 */
8203 if (kvm_write_guest_virt_system(&vcpu->arch.emulate_ctxt, vmcs_gva,
8204 (void *)&to_vmx(vcpu)->nested.current_vmptr,
8206 kvm_inject_page_fault(vcpu, &e);
8209 nested_vmx_succeed(vcpu);
8210 return kvm_skip_emulated_instruction(vcpu);
8213 /* Emulate the INVEPT instruction */
8214 static int handle_invept(struct kvm_vcpu *vcpu)
8216 struct vcpu_vmx *vmx = to_vmx(vcpu);
8217 u32 vmx_instruction_info, types;
8220 struct x86_exception e;
8225 if (!(vmx->nested.msrs.secondary_ctls_high &
8226 SECONDARY_EXEC_ENABLE_EPT) ||
8227 !(vmx->nested.msrs.ept_caps & VMX_EPT_INVEPT_BIT)) {
8228 kvm_queue_exception(vcpu, UD_VECTOR);
8232 if (!nested_vmx_check_permission(vcpu))
8235 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
8236 type = kvm_register_readl(vcpu, (vmx_instruction_info >> 28) & 0xf);
8238 types = (vmx->nested.msrs.ept_caps >> VMX_EPT_EXTENT_SHIFT) & 6;
8240 if (type >= 32 || !(types & (1 << type))) {
8241 nested_vmx_failValid(vcpu,
8242 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
8243 return kvm_skip_emulated_instruction(vcpu);
8246 /* According to the Intel VMX instruction reference, the memory
8247 * operand is read even if it isn't needed (e.g., for type==global)
8249 if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
8250 vmx_instruction_info, false, &gva))
8252 if (kvm_read_guest_virt(&vcpu->arch.emulate_ctxt, gva, &operand,
8253 sizeof(operand), &e)) {
8254 kvm_inject_page_fault(vcpu, &e);
8259 case VMX_EPT_EXTENT_GLOBAL:
8261 * TODO: track mappings and invalidate
8262 * single context requests appropriately
8264 case VMX_EPT_EXTENT_CONTEXT:
8265 kvm_mmu_sync_roots(vcpu);
8266 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
8267 nested_vmx_succeed(vcpu);
8274 return kvm_skip_emulated_instruction(vcpu);
8277 static int handle_invvpid(struct kvm_vcpu *vcpu)
8279 struct vcpu_vmx *vmx = to_vmx(vcpu);
8280 u32 vmx_instruction_info;
8281 unsigned long type, types;
8283 struct x86_exception e;
8289 if (!(vmx->nested.msrs.secondary_ctls_high &
8290 SECONDARY_EXEC_ENABLE_VPID) ||
8291 !(vmx->nested.msrs.vpid_caps & VMX_VPID_INVVPID_BIT)) {
8292 kvm_queue_exception(vcpu, UD_VECTOR);
8296 if (!nested_vmx_check_permission(vcpu))
8299 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
8300 type = kvm_register_readl(vcpu, (vmx_instruction_info >> 28) & 0xf);
8302 types = (vmx->nested.msrs.vpid_caps &
8303 VMX_VPID_EXTENT_SUPPORTED_MASK) >> 8;
8305 if (type >= 32 || !(types & (1 << type))) {
8306 nested_vmx_failValid(vcpu,
8307 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
8308 return kvm_skip_emulated_instruction(vcpu);
8311 /* according to the intel vmx instruction reference, the memory
8312 * operand is read even if it isn't needed (e.g., for type==global)
8314 if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
8315 vmx_instruction_info, false, &gva))
8317 if (kvm_read_guest_virt(&vcpu->arch.emulate_ctxt, gva, &operand,
8318 sizeof(operand), &e)) {
8319 kvm_inject_page_fault(vcpu, &e);
8322 if (operand.vpid >> 16) {
8323 nested_vmx_failValid(vcpu,
8324 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
8325 return kvm_skip_emulated_instruction(vcpu);
8329 case VMX_VPID_EXTENT_INDIVIDUAL_ADDR:
8330 if (is_noncanonical_address(operand.gla, vcpu)) {
8331 nested_vmx_failValid(vcpu,
8332 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
8333 return kvm_skip_emulated_instruction(vcpu);
8336 case VMX_VPID_EXTENT_SINGLE_CONTEXT:
8337 case VMX_VPID_EXTENT_SINGLE_NON_GLOBAL:
8338 if (!operand.vpid) {
8339 nested_vmx_failValid(vcpu,
8340 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
8341 return kvm_skip_emulated_instruction(vcpu);
8344 case VMX_VPID_EXTENT_ALL_CONTEXT:
8348 return kvm_skip_emulated_instruction(vcpu);
8351 __vmx_flush_tlb(vcpu, vmx->nested.vpid02, true);
8352 nested_vmx_succeed(vcpu);
8354 return kvm_skip_emulated_instruction(vcpu);
8357 static int handle_pml_full(struct kvm_vcpu *vcpu)
8359 unsigned long exit_qualification;
8361 trace_kvm_pml_full(vcpu->vcpu_id);
8363 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
8366 * PML buffer FULL happened while executing iret from NMI,
8367 * "blocked by NMI" bit has to be set before next VM entry.
8369 if (!(to_vmx(vcpu)->idt_vectoring_info & VECTORING_INFO_VALID_MASK) &&
8371 (exit_qualification & INTR_INFO_UNBLOCK_NMI))
8372 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
8373 GUEST_INTR_STATE_NMI);
8376 * PML buffer already flushed at beginning of VMEXIT. Nothing to do
8377 * here.., and there's no userspace involvement needed for PML.
8382 static int handle_preemption_timer(struct kvm_vcpu *vcpu)
8384 kvm_lapic_expired_hv_timer(vcpu);
8388 static bool valid_ept_address(struct kvm_vcpu *vcpu, u64 address)
8390 struct vcpu_vmx *vmx = to_vmx(vcpu);
8391 int maxphyaddr = cpuid_maxphyaddr(vcpu);
8393 /* Check for memory type validity */
8394 switch (address & VMX_EPTP_MT_MASK) {
8395 case VMX_EPTP_MT_UC:
8396 if (!(vmx->nested.msrs.ept_caps & VMX_EPTP_UC_BIT))
8399 case VMX_EPTP_MT_WB:
8400 if (!(vmx->nested.msrs.ept_caps & VMX_EPTP_WB_BIT))
8407 /* only 4 levels page-walk length are valid */
8408 if ((address & VMX_EPTP_PWL_MASK) != VMX_EPTP_PWL_4)
8411 /* Reserved bits should not be set */
8412 if (address >> maxphyaddr || ((address >> 7) & 0x1f))
8415 /* AD, if set, should be supported */
8416 if (address & VMX_EPTP_AD_ENABLE_BIT) {
8417 if (!(vmx->nested.msrs.ept_caps & VMX_EPT_AD_BIT))
8424 static int nested_vmx_eptp_switching(struct kvm_vcpu *vcpu,
8425 struct vmcs12 *vmcs12)
8427 u32 index = vcpu->arch.regs[VCPU_REGS_RCX];
8429 bool accessed_dirty;
8430 struct kvm_mmu *mmu = vcpu->arch.walk_mmu;
8432 if (!nested_cpu_has_eptp_switching(vmcs12) ||
8433 !nested_cpu_has_ept(vmcs12))
8436 if (index >= VMFUNC_EPTP_ENTRIES)
8440 if (kvm_vcpu_read_guest_page(vcpu, vmcs12->eptp_list_address >> PAGE_SHIFT,
8441 &address, index * 8, 8))
8444 accessed_dirty = !!(address & VMX_EPTP_AD_ENABLE_BIT);
8447 * If the (L2) guest does a vmfunc to the currently
8448 * active ept pointer, we don't have to do anything else
8450 if (vmcs12->ept_pointer != address) {
8451 if (!valid_ept_address(vcpu, address))
8454 kvm_mmu_unload(vcpu);
8455 mmu->ept_ad = accessed_dirty;
8456 mmu->base_role.ad_disabled = !accessed_dirty;
8457 vmcs12->ept_pointer = address;
8459 * TODO: Check what's the correct approach in case
8460 * mmu reload fails. Currently, we just let the next
8461 * reload potentially fail
8463 kvm_mmu_reload(vcpu);
8469 static int handle_vmfunc(struct kvm_vcpu *vcpu)
8471 struct vcpu_vmx *vmx = to_vmx(vcpu);
8472 struct vmcs12 *vmcs12;
8473 u32 function = vcpu->arch.regs[VCPU_REGS_RAX];
8476 * VMFUNC is only supported for nested guests, but we always enable the
8477 * secondary control for simplicity; for non-nested mode, fake that we
8478 * didn't by injecting #UD.
8480 if (!is_guest_mode(vcpu)) {
8481 kvm_queue_exception(vcpu, UD_VECTOR);
8485 vmcs12 = get_vmcs12(vcpu);
8486 if ((vmcs12->vm_function_control & (1 << function)) == 0)
8491 if (nested_vmx_eptp_switching(vcpu, vmcs12))
8497 return kvm_skip_emulated_instruction(vcpu);
8500 nested_vmx_vmexit(vcpu, vmx->exit_reason,
8501 vmcs_read32(VM_EXIT_INTR_INFO),
8502 vmcs_readl(EXIT_QUALIFICATION));
8507 * The exit handlers return 1 if the exit was handled fully and guest execution
8508 * may resume. Otherwise they set the kvm_run parameter to indicate what needs
8509 * to be done to userspace and return 0.
8511 static int (*const kvm_vmx_exit_handlers[])(struct kvm_vcpu *vcpu) = {
8512 [EXIT_REASON_EXCEPTION_NMI] = handle_exception,
8513 [EXIT_REASON_EXTERNAL_INTERRUPT] = handle_external_interrupt,
8514 [EXIT_REASON_TRIPLE_FAULT] = handle_triple_fault,
8515 [EXIT_REASON_NMI_WINDOW] = handle_nmi_window,
8516 [EXIT_REASON_IO_INSTRUCTION] = handle_io,
8517 [EXIT_REASON_CR_ACCESS] = handle_cr,
8518 [EXIT_REASON_DR_ACCESS] = handle_dr,
8519 [EXIT_REASON_CPUID] = handle_cpuid,
8520 [EXIT_REASON_MSR_READ] = handle_rdmsr,
8521 [EXIT_REASON_MSR_WRITE] = handle_wrmsr,
8522 [EXIT_REASON_PENDING_INTERRUPT] = handle_interrupt_window,
8523 [EXIT_REASON_HLT] = handle_halt,
8524 [EXIT_REASON_INVD] = handle_invd,
8525 [EXIT_REASON_INVLPG] = handle_invlpg,
8526 [EXIT_REASON_RDPMC] = handle_rdpmc,
8527 [EXIT_REASON_VMCALL] = handle_vmcall,
8528 [EXIT_REASON_VMCLEAR] = handle_vmclear,
8529 [EXIT_REASON_VMLAUNCH] = handle_vmlaunch,
8530 [EXIT_REASON_VMPTRLD] = handle_vmptrld,
8531 [EXIT_REASON_VMPTRST] = handle_vmptrst,
8532 [EXIT_REASON_VMREAD] = handle_vmread,
8533 [EXIT_REASON_VMRESUME] = handle_vmresume,
8534 [EXIT_REASON_VMWRITE] = handle_vmwrite,
8535 [EXIT_REASON_VMOFF] = handle_vmoff,
8536 [EXIT_REASON_VMON] = handle_vmon,
8537 [EXIT_REASON_TPR_BELOW_THRESHOLD] = handle_tpr_below_threshold,
8538 [EXIT_REASON_APIC_ACCESS] = handle_apic_access,
8539 [EXIT_REASON_APIC_WRITE] = handle_apic_write,
8540 [EXIT_REASON_EOI_INDUCED] = handle_apic_eoi_induced,
8541 [EXIT_REASON_WBINVD] = handle_wbinvd,
8542 [EXIT_REASON_XSETBV] = handle_xsetbv,
8543 [EXIT_REASON_TASK_SWITCH] = handle_task_switch,
8544 [EXIT_REASON_MCE_DURING_VMENTRY] = handle_machine_check,
8545 [EXIT_REASON_GDTR_IDTR] = handle_desc,
8546 [EXIT_REASON_LDTR_TR] = handle_desc,
8547 [EXIT_REASON_EPT_VIOLATION] = handle_ept_violation,
8548 [EXIT_REASON_EPT_MISCONFIG] = handle_ept_misconfig,
8549 [EXIT_REASON_PAUSE_INSTRUCTION] = handle_pause,
8550 [EXIT_REASON_MWAIT_INSTRUCTION] = handle_mwait,
8551 [EXIT_REASON_MONITOR_TRAP_FLAG] = handle_monitor_trap,
8552 [EXIT_REASON_MONITOR_INSTRUCTION] = handle_monitor,
8553 [EXIT_REASON_INVEPT] = handle_invept,
8554 [EXIT_REASON_INVVPID] = handle_invvpid,
8555 [EXIT_REASON_RDRAND] = handle_invalid_op,
8556 [EXIT_REASON_RDSEED] = handle_invalid_op,
8557 [EXIT_REASON_XSAVES] = handle_xsaves,
8558 [EXIT_REASON_XRSTORS] = handle_xrstors,
8559 [EXIT_REASON_PML_FULL] = handle_pml_full,
8560 [EXIT_REASON_VMFUNC] = handle_vmfunc,
8561 [EXIT_REASON_PREEMPTION_TIMER] = handle_preemption_timer,
8564 static const int kvm_vmx_max_exit_handlers =
8565 ARRAY_SIZE(kvm_vmx_exit_handlers);
8567 static bool nested_vmx_exit_handled_io(struct kvm_vcpu *vcpu,
8568 struct vmcs12 *vmcs12)
8570 unsigned long exit_qualification;
8571 gpa_t bitmap, last_bitmap;
8576 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_IO_BITMAPS))
8577 return nested_cpu_has(vmcs12, CPU_BASED_UNCOND_IO_EXITING);
8579 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
8581 port = exit_qualification >> 16;
8582 size = (exit_qualification & 7) + 1;
8584 last_bitmap = (gpa_t)-1;
8589 bitmap = vmcs12->io_bitmap_a;
8590 else if (port < 0x10000)
8591 bitmap = vmcs12->io_bitmap_b;
8594 bitmap += (port & 0x7fff) / 8;
8596 if (last_bitmap != bitmap)
8597 if (kvm_vcpu_read_guest(vcpu, bitmap, &b, 1))
8599 if (b & (1 << (port & 7)))
8604 last_bitmap = bitmap;
8611 * Return 1 if we should exit from L2 to L1 to handle an MSR access access,
8612 * rather than handle it ourselves in L0. I.e., check whether L1 expressed
8613 * disinterest in the current event (read or write a specific MSR) by using an
8614 * MSR bitmap. This may be the case even when L0 doesn't use MSR bitmaps.
8616 static bool nested_vmx_exit_handled_msr(struct kvm_vcpu *vcpu,
8617 struct vmcs12 *vmcs12, u32 exit_reason)
8619 u32 msr_index = vcpu->arch.regs[VCPU_REGS_RCX];
8622 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS))
8626 * The MSR_BITMAP page is divided into four 1024-byte bitmaps,
8627 * for the four combinations of read/write and low/high MSR numbers.
8628 * First we need to figure out which of the four to use:
8630 bitmap = vmcs12->msr_bitmap;
8631 if (exit_reason == EXIT_REASON_MSR_WRITE)
8633 if (msr_index >= 0xc0000000) {
8634 msr_index -= 0xc0000000;
8638 /* Then read the msr_index'th bit from this bitmap: */
8639 if (msr_index < 1024*8) {
8641 if (kvm_vcpu_read_guest(vcpu, bitmap + msr_index/8, &b, 1))
8643 return 1 & (b >> (msr_index & 7));
8645 return true; /* let L1 handle the wrong parameter */
8649 * Return 1 if we should exit from L2 to L1 to handle a CR access exit,
8650 * rather than handle it ourselves in L0. I.e., check if L1 wanted to
8651 * intercept (via guest_host_mask etc.) the current event.
8653 static bool nested_vmx_exit_handled_cr(struct kvm_vcpu *vcpu,
8654 struct vmcs12 *vmcs12)
8656 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
8657 int cr = exit_qualification & 15;
8661 switch ((exit_qualification >> 4) & 3) {
8662 case 0: /* mov to cr */
8663 reg = (exit_qualification >> 8) & 15;
8664 val = kvm_register_readl(vcpu, reg);
8667 if (vmcs12->cr0_guest_host_mask &
8668 (val ^ vmcs12->cr0_read_shadow))
8672 if ((vmcs12->cr3_target_count >= 1 &&
8673 vmcs12->cr3_target_value0 == val) ||
8674 (vmcs12->cr3_target_count >= 2 &&
8675 vmcs12->cr3_target_value1 == val) ||
8676 (vmcs12->cr3_target_count >= 3 &&
8677 vmcs12->cr3_target_value2 == val) ||
8678 (vmcs12->cr3_target_count >= 4 &&
8679 vmcs12->cr3_target_value3 == val))
8681 if (nested_cpu_has(vmcs12, CPU_BASED_CR3_LOAD_EXITING))
8685 if (vmcs12->cr4_guest_host_mask &
8686 (vmcs12->cr4_read_shadow ^ val))
8690 if (nested_cpu_has(vmcs12, CPU_BASED_CR8_LOAD_EXITING))
8696 if ((vmcs12->cr0_guest_host_mask & X86_CR0_TS) &&
8697 (vmcs12->cr0_read_shadow & X86_CR0_TS))
8700 case 1: /* mov from cr */
8703 if (vmcs12->cpu_based_vm_exec_control &
8704 CPU_BASED_CR3_STORE_EXITING)
8708 if (vmcs12->cpu_based_vm_exec_control &
8709 CPU_BASED_CR8_STORE_EXITING)
8716 * lmsw can change bits 1..3 of cr0, and only set bit 0 of
8717 * cr0. Other attempted changes are ignored, with no exit.
8719 val = (exit_qualification >> LMSW_SOURCE_DATA_SHIFT) & 0x0f;
8720 if (vmcs12->cr0_guest_host_mask & 0xe &
8721 (val ^ vmcs12->cr0_read_shadow))
8723 if ((vmcs12->cr0_guest_host_mask & 0x1) &&
8724 !(vmcs12->cr0_read_shadow & 0x1) &&
8733 * Return 1 if we should exit from L2 to L1 to handle an exit, or 0 if we
8734 * should handle it ourselves in L0 (and then continue L2). Only call this
8735 * when in is_guest_mode (L2).
8737 static bool nested_vmx_exit_reflected(struct kvm_vcpu *vcpu, u32 exit_reason)
8739 u32 intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
8740 struct vcpu_vmx *vmx = to_vmx(vcpu);
8741 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
8743 if (vmx->nested.nested_run_pending)
8746 if (unlikely(vmx->fail)) {
8747 pr_info_ratelimited("%s failed vm entry %x\n", __func__,
8748 vmcs_read32(VM_INSTRUCTION_ERROR));
8753 * The host physical addresses of some pages of guest memory
8754 * are loaded into the vmcs02 (e.g. vmcs12's Virtual APIC
8755 * Page). The CPU may write to these pages via their host
8756 * physical address while L2 is running, bypassing any
8757 * address-translation-based dirty tracking (e.g. EPT write
8760 * Mark them dirty on every exit from L2 to prevent them from
8761 * getting out of sync with dirty tracking.
8763 nested_mark_vmcs12_pages_dirty(vcpu);
8765 trace_kvm_nested_vmexit(kvm_rip_read(vcpu), exit_reason,
8766 vmcs_readl(EXIT_QUALIFICATION),
8767 vmx->idt_vectoring_info,
8769 vmcs_read32(VM_EXIT_INTR_ERROR_CODE),
8772 switch (exit_reason) {
8773 case EXIT_REASON_EXCEPTION_NMI:
8774 if (is_nmi(intr_info))
8776 else if (is_page_fault(intr_info))
8777 return !vmx->vcpu.arch.apf.host_apf_reason && enable_ept;
8778 else if (is_no_device(intr_info) &&
8779 !(vmcs12->guest_cr0 & X86_CR0_TS))
8781 else if (is_debug(intr_info) &&
8783 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))
8785 else if (is_breakpoint(intr_info) &&
8786 vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
8788 return vmcs12->exception_bitmap &
8789 (1u << (intr_info & INTR_INFO_VECTOR_MASK));
8790 case EXIT_REASON_EXTERNAL_INTERRUPT:
8792 case EXIT_REASON_TRIPLE_FAULT:
8794 case EXIT_REASON_PENDING_INTERRUPT:
8795 return nested_cpu_has(vmcs12, CPU_BASED_VIRTUAL_INTR_PENDING);
8796 case EXIT_REASON_NMI_WINDOW:
8797 return nested_cpu_has(vmcs12, CPU_BASED_VIRTUAL_NMI_PENDING);
8798 case EXIT_REASON_TASK_SWITCH:
8800 case EXIT_REASON_CPUID:
8802 case EXIT_REASON_HLT:
8803 return nested_cpu_has(vmcs12, CPU_BASED_HLT_EXITING);
8804 case EXIT_REASON_INVD:
8806 case EXIT_REASON_INVLPG:
8807 return nested_cpu_has(vmcs12, CPU_BASED_INVLPG_EXITING);
8808 case EXIT_REASON_RDPMC:
8809 return nested_cpu_has(vmcs12, CPU_BASED_RDPMC_EXITING);
8810 case EXIT_REASON_RDRAND:
8811 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_RDRAND_EXITING);
8812 case EXIT_REASON_RDSEED:
8813 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_RDSEED_EXITING);
8814 case EXIT_REASON_RDTSC: case EXIT_REASON_RDTSCP:
8815 return nested_cpu_has(vmcs12, CPU_BASED_RDTSC_EXITING);
8816 case EXIT_REASON_VMCALL: case EXIT_REASON_VMCLEAR:
8817 case EXIT_REASON_VMLAUNCH: case EXIT_REASON_VMPTRLD:
8818 case EXIT_REASON_VMPTRST: case EXIT_REASON_VMREAD:
8819 case EXIT_REASON_VMRESUME: case EXIT_REASON_VMWRITE:
8820 case EXIT_REASON_VMOFF: case EXIT_REASON_VMON:
8821 case EXIT_REASON_INVEPT: case EXIT_REASON_INVVPID:
8823 * VMX instructions trap unconditionally. This allows L1 to
8824 * emulate them for its L2 guest, i.e., allows 3-level nesting!
8827 case EXIT_REASON_CR_ACCESS:
8828 return nested_vmx_exit_handled_cr(vcpu, vmcs12);
8829 case EXIT_REASON_DR_ACCESS:
8830 return nested_cpu_has(vmcs12, CPU_BASED_MOV_DR_EXITING);
8831 case EXIT_REASON_IO_INSTRUCTION:
8832 return nested_vmx_exit_handled_io(vcpu, vmcs12);
8833 case EXIT_REASON_GDTR_IDTR: case EXIT_REASON_LDTR_TR:
8834 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_DESC);
8835 case EXIT_REASON_MSR_READ:
8836 case EXIT_REASON_MSR_WRITE:
8837 return nested_vmx_exit_handled_msr(vcpu, vmcs12, exit_reason);
8838 case EXIT_REASON_INVALID_STATE:
8840 case EXIT_REASON_MWAIT_INSTRUCTION:
8841 return nested_cpu_has(vmcs12, CPU_BASED_MWAIT_EXITING);
8842 case EXIT_REASON_MONITOR_TRAP_FLAG:
8843 return nested_cpu_has(vmcs12, CPU_BASED_MONITOR_TRAP_FLAG);
8844 case EXIT_REASON_MONITOR_INSTRUCTION:
8845 return nested_cpu_has(vmcs12, CPU_BASED_MONITOR_EXITING);
8846 case EXIT_REASON_PAUSE_INSTRUCTION:
8847 return nested_cpu_has(vmcs12, CPU_BASED_PAUSE_EXITING) ||
8848 nested_cpu_has2(vmcs12,
8849 SECONDARY_EXEC_PAUSE_LOOP_EXITING);
8850 case EXIT_REASON_MCE_DURING_VMENTRY:
8852 case EXIT_REASON_TPR_BELOW_THRESHOLD:
8853 return nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW);
8854 case EXIT_REASON_APIC_ACCESS:
8855 return nested_cpu_has2(vmcs12,
8856 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES);
8857 case EXIT_REASON_APIC_WRITE:
8858 case EXIT_REASON_EOI_INDUCED:
8859 /* apic_write and eoi_induced should exit unconditionally. */
8861 case EXIT_REASON_EPT_VIOLATION:
8863 * L0 always deals with the EPT violation. If nested EPT is
8864 * used, and the nested mmu code discovers that the address is
8865 * missing in the guest EPT table (EPT12), the EPT violation
8866 * will be injected with nested_ept_inject_page_fault()
8869 case EXIT_REASON_EPT_MISCONFIG:
8871 * L2 never uses directly L1's EPT, but rather L0's own EPT
8872 * table (shadow on EPT) or a merged EPT table that L0 built
8873 * (EPT on EPT). So any problems with the structure of the
8874 * table is L0's fault.
8877 case EXIT_REASON_INVPCID:
8879 nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_INVPCID) &&
8880 nested_cpu_has(vmcs12, CPU_BASED_INVLPG_EXITING);
8881 case EXIT_REASON_WBINVD:
8882 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_WBINVD_EXITING);
8883 case EXIT_REASON_XSETBV:
8885 case EXIT_REASON_XSAVES: case EXIT_REASON_XRSTORS:
8887 * This should never happen, since it is not possible to
8888 * set XSS to a non-zero value---neither in L1 nor in L2.
8889 * If if it were, XSS would have to be checked against
8890 * the XSS exit bitmap in vmcs12.
8892 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_XSAVES);
8893 case EXIT_REASON_PREEMPTION_TIMER:
8895 case EXIT_REASON_PML_FULL:
8896 /* We emulate PML support to L1. */
8898 case EXIT_REASON_VMFUNC:
8899 /* VM functions are emulated through L2->L0 vmexits. */
8906 static int nested_vmx_reflect_vmexit(struct kvm_vcpu *vcpu, u32 exit_reason)
8908 u32 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
8911 * At this point, the exit interruption info in exit_intr_info
8912 * is only valid for EXCEPTION_NMI exits. For EXTERNAL_INTERRUPT
8913 * we need to query the in-kernel LAPIC.
8915 WARN_ON(exit_reason == EXIT_REASON_EXTERNAL_INTERRUPT);
8916 if ((exit_intr_info &
8917 (INTR_INFO_VALID_MASK | INTR_INFO_DELIVER_CODE_MASK)) ==
8918 (INTR_INFO_VALID_MASK | INTR_INFO_DELIVER_CODE_MASK)) {
8919 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
8920 vmcs12->vm_exit_intr_error_code =
8921 vmcs_read32(VM_EXIT_INTR_ERROR_CODE);
8924 nested_vmx_vmexit(vcpu, exit_reason, exit_intr_info,
8925 vmcs_readl(EXIT_QUALIFICATION));
8929 static void vmx_get_exit_info(struct kvm_vcpu *vcpu, u64 *info1, u64 *info2)
8931 *info1 = vmcs_readl(EXIT_QUALIFICATION);
8932 *info2 = vmcs_read32(VM_EXIT_INTR_INFO);
8935 static void vmx_destroy_pml_buffer(struct vcpu_vmx *vmx)
8938 __free_page(vmx->pml_pg);
8943 static void vmx_flush_pml_buffer(struct kvm_vcpu *vcpu)
8945 struct vcpu_vmx *vmx = to_vmx(vcpu);
8949 pml_idx = vmcs_read16(GUEST_PML_INDEX);
8951 /* Do nothing if PML buffer is empty */
8952 if (pml_idx == (PML_ENTITY_NUM - 1))
8955 /* PML index always points to next available PML buffer entity */
8956 if (pml_idx >= PML_ENTITY_NUM)
8961 pml_buf = page_address(vmx->pml_pg);
8962 for (; pml_idx < PML_ENTITY_NUM; pml_idx++) {
8965 gpa = pml_buf[pml_idx];
8966 WARN_ON(gpa & (PAGE_SIZE - 1));
8967 kvm_vcpu_mark_page_dirty(vcpu, gpa >> PAGE_SHIFT);
8970 /* reset PML index */
8971 vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1);
8975 * Flush all vcpus' PML buffer and update logged GPAs to dirty_bitmap.
8976 * Called before reporting dirty_bitmap to userspace.
8978 static void kvm_flush_pml_buffers(struct kvm *kvm)
8981 struct kvm_vcpu *vcpu;
8983 * We only need to kick vcpu out of guest mode here, as PML buffer
8984 * is flushed at beginning of all VMEXITs, and it's obvious that only
8985 * vcpus running in guest are possible to have unflushed GPAs in PML
8988 kvm_for_each_vcpu(i, vcpu, kvm)
8989 kvm_vcpu_kick(vcpu);
8992 static void vmx_dump_sel(char *name, uint32_t sel)
8994 pr_err("%s sel=0x%04x, attr=0x%05x, limit=0x%08x, base=0x%016lx\n",
8995 name, vmcs_read16(sel),
8996 vmcs_read32(sel + GUEST_ES_AR_BYTES - GUEST_ES_SELECTOR),
8997 vmcs_read32(sel + GUEST_ES_LIMIT - GUEST_ES_SELECTOR),
8998 vmcs_readl(sel + GUEST_ES_BASE - GUEST_ES_SELECTOR));
9001 static void vmx_dump_dtsel(char *name, uint32_t limit)
9003 pr_err("%s limit=0x%08x, base=0x%016lx\n",
9004 name, vmcs_read32(limit),
9005 vmcs_readl(limit + GUEST_GDTR_BASE - GUEST_GDTR_LIMIT));
9008 static void dump_vmcs(void)
9010 u32 vmentry_ctl = vmcs_read32(VM_ENTRY_CONTROLS);
9011 u32 vmexit_ctl = vmcs_read32(VM_EXIT_CONTROLS);
9012 u32 cpu_based_exec_ctrl = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
9013 u32 pin_based_exec_ctrl = vmcs_read32(PIN_BASED_VM_EXEC_CONTROL);
9014 u32 secondary_exec_control = 0;
9015 unsigned long cr4 = vmcs_readl(GUEST_CR4);
9016 u64 efer = vmcs_read64(GUEST_IA32_EFER);
9019 if (cpu_has_secondary_exec_ctrls())
9020 secondary_exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
9022 pr_err("*** Guest State ***\n");
9023 pr_err("CR0: actual=0x%016lx, shadow=0x%016lx, gh_mask=%016lx\n",
9024 vmcs_readl(GUEST_CR0), vmcs_readl(CR0_READ_SHADOW),
9025 vmcs_readl(CR0_GUEST_HOST_MASK));
9026 pr_err("CR4: actual=0x%016lx, shadow=0x%016lx, gh_mask=%016lx\n",
9027 cr4, vmcs_readl(CR4_READ_SHADOW), vmcs_readl(CR4_GUEST_HOST_MASK));
9028 pr_err("CR3 = 0x%016lx\n", vmcs_readl(GUEST_CR3));
9029 if ((secondary_exec_control & SECONDARY_EXEC_ENABLE_EPT) &&
9030 (cr4 & X86_CR4_PAE) && !(efer & EFER_LMA))
9032 pr_err("PDPTR0 = 0x%016llx PDPTR1 = 0x%016llx\n",
9033 vmcs_read64(GUEST_PDPTR0), vmcs_read64(GUEST_PDPTR1));
9034 pr_err("PDPTR2 = 0x%016llx PDPTR3 = 0x%016llx\n",
9035 vmcs_read64(GUEST_PDPTR2), vmcs_read64(GUEST_PDPTR3));
9037 pr_err("RSP = 0x%016lx RIP = 0x%016lx\n",
9038 vmcs_readl(GUEST_RSP), vmcs_readl(GUEST_RIP));
9039 pr_err("RFLAGS=0x%08lx DR7 = 0x%016lx\n",
9040 vmcs_readl(GUEST_RFLAGS), vmcs_readl(GUEST_DR7));
9041 pr_err("Sysenter RSP=%016lx CS:RIP=%04x:%016lx\n",
9042 vmcs_readl(GUEST_SYSENTER_ESP),
9043 vmcs_read32(GUEST_SYSENTER_CS), vmcs_readl(GUEST_SYSENTER_EIP));
9044 vmx_dump_sel("CS: ", GUEST_CS_SELECTOR);
9045 vmx_dump_sel("DS: ", GUEST_DS_SELECTOR);
9046 vmx_dump_sel("SS: ", GUEST_SS_SELECTOR);
9047 vmx_dump_sel("ES: ", GUEST_ES_SELECTOR);
9048 vmx_dump_sel("FS: ", GUEST_FS_SELECTOR);
9049 vmx_dump_sel("GS: ", GUEST_GS_SELECTOR);
9050 vmx_dump_dtsel("GDTR:", GUEST_GDTR_LIMIT);
9051 vmx_dump_sel("LDTR:", GUEST_LDTR_SELECTOR);
9052 vmx_dump_dtsel("IDTR:", GUEST_IDTR_LIMIT);
9053 vmx_dump_sel("TR: ", GUEST_TR_SELECTOR);
9054 if ((vmexit_ctl & (VM_EXIT_SAVE_IA32_PAT | VM_EXIT_SAVE_IA32_EFER)) ||
9055 (vmentry_ctl & (VM_ENTRY_LOAD_IA32_PAT | VM_ENTRY_LOAD_IA32_EFER)))
9056 pr_err("EFER = 0x%016llx PAT = 0x%016llx\n",
9057 efer, vmcs_read64(GUEST_IA32_PAT));
9058 pr_err("DebugCtl = 0x%016llx DebugExceptions = 0x%016lx\n",
9059 vmcs_read64(GUEST_IA32_DEBUGCTL),
9060 vmcs_readl(GUEST_PENDING_DBG_EXCEPTIONS));
9061 if (cpu_has_load_perf_global_ctrl &&
9062 vmentry_ctl & VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL)
9063 pr_err("PerfGlobCtl = 0x%016llx\n",
9064 vmcs_read64(GUEST_IA32_PERF_GLOBAL_CTRL));
9065 if (vmentry_ctl & VM_ENTRY_LOAD_BNDCFGS)
9066 pr_err("BndCfgS = 0x%016llx\n", vmcs_read64(GUEST_BNDCFGS));
9067 pr_err("Interruptibility = %08x ActivityState = %08x\n",
9068 vmcs_read32(GUEST_INTERRUPTIBILITY_INFO),
9069 vmcs_read32(GUEST_ACTIVITY_STATE));
9070 if (secondary_exec_control & SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY)
9071 pr_err("InterruptStatus = %04x\n",
9072 vmcs_read16(GUEST_INTR_STATUS));
9074 pr_err("*** Host State ***\n");
9075 pr_err("RIP = 0x%016lx RSP = 0x%016lx\n",
9076 vmcs_readl(HOST_RIP), vmcs_readl(HOST_RSP));
9077 pr_err("CS=%04x SS=%04x DS=%04x ES=%04x FS=%04x GS=%04x TR=%04x\n",
9078 vmcs_read16(HOST_CS_SELECTOR), vmcs_read16(HOST_SS_SELECTOR),
9079 vmcs_read16(HOST_DS_SELECTOR), vmcs_read16(HOST_ES_SELECTOR),
9080 vmcs_read16(HOST_FS_SELECTOR), vmcs_read16(HOST_GS_SELECTOR),
9081 vmcs_read16(HOST_TR_SELECTOR));
9082 pr_err("FSBase=%016lx GSBase=%016lx TRBase=%016lx\n",
9083 vmcs_readl(HOST_FS_BASE), vmcs_readl(HOST_GS_BASE),
9084 vmcs_readl(HOST_TR_BASE));
9085 pr_err("GDTBase=%016lx IDTBase=%016lx\n",
9086 vmcs_readl(HOST_GDTR_BASE), vmcs_readl(HOST_IDTR_BASE));
9087 pr_err("CR0=%016lx CR3=%016lx CR4=%016lx\n",
9088 vmcs_readl(HOST_CR0), vmcs_readl(HOST_CR3),
9089 vmcs_readl(HOST_CR4));
9090 pr_err("Sysenter RSP=%016lx CS:RIP=%04x:%016lx\n",
9091 vmcs_readl(HOST_IA32_SYSENTER_ESP),
9092 vmcs_read32(HOST_IA32_SYSENTER_CS),
9093 vmcs_readl(HOST_IA32_SYSENTER_EIP));
9094 if (vmexit_ctl & (VM_EXIT_LOAD_IA32_PAT | VM_EXIT_LOAD_IA32_EFER))
9095 pr_err("EFER = 0x%016llx PAT = 0x%016llx\n",
9096 vmcs_read64(HOST_IA32_EFER),
9097 vmcs_read64(HOST_IA32_PAT));
9098 if (cpu_has_load_perf_global_ctrl &&
9099 vmexit_ctl & VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL)
9100 pr_err("PerfGlobCtl = 0x%016llx\n",
9101 vmcs_read64(HOST_IA32_PERF_GLOBAL_CTRL));
9103 pr_err("*** Control State ***\n");
9104 pr_err("PinBased=%08x CPUBased=%08x SecondaryExec=%08x\n",
9105 pin_based_exec_ctrl, cpu_based_exec_ctrl, secondary_exec_control);
9106 pr_err("EntryControls=%08x ExitControls=%08x\n", vmentry_ctl, vmexit_ctl);
9107 pr_err("ExceptionBitmap=%08x PFECmask=%08x PFECmatch=%08x\n",
9108 vmcs_read32(EXCEPTION_BITMAP),
9109 vmcs_read32(PAGE_FAULT_ERROR_CODE_MASK),
9110 vmcs_read32(PAGE_FAULT_ERROR_CODE_MATCH));
9111 pr_err("VMEntry: intr_info=%08x errcode=%08x ilen=%08x\n",
9112 vmcs_read32(VM_ENTRY_INTR_INFO_FIELD),
9113 vmcs_read32(VM_ENTRY_EXCEPTION_ERROR_CODE),
9114 vmcs_read32(VM_ENTRY_INSTRUCTION_LEN));
9115 pr_err("VMExit: intr_info=%08x errcode=%08x ilen=%08x\n",
9116 vmcs_read32(VM_EXIT_INTR_INFO),
9117 vmcs_read32(VM_EXIT_INTR_ERROR_CODE),
9118 vmcs_read32(VM_EXIT_INSTRUCTION_LEN));
9119 pr_err(" reason=%08x qualification=%016lx\n",
9120 vmcs_read32(VM_EXIT_REASON), vmcs_readl(EXIT_QUALIFICATION));
9121 pr_err("IDTVectoring: info=%08x errcode=%08x\n",
9122 vmcs_read32(IDT_VECTORING_INFO_FIELD),
9123 vmcs_read32(IDT_VECTORING_ERROR_CODE));
9124 pr_err("TSC Offset = 0x%016llx\n", vmcs_read64(TSC_OFFSET));
9125 if (secondary_exec_control & SECONDARY_EXEC_TSC_SCALING)
9126 pr_err("TSC Multiplier = 0x%016llx\n",
9127 vmcs_read64(TSC_MULTIPLIER));
9128 if (cpu_based_exec_ctrl & CPU_BASED_TPR_SHADOW)
9129 pr_err("TPR Threshold = 0x%02x\n", vmcs_read32(TPR_THRESHOLD));
9130 if (pin_based_exec_ctrl & PIN_BASED_POSTED_INTR)
9131 pr_err("PostedIntrVec = 0x%02x\n", vmcs_read16(POSTED_INTR_NV));
9132 if ((secondary_exec_control & SECONDARY_EXEC_ENABLE_EPT))
9133 pr_err("EPT pointer = 0x%016llx\n", vmcs_read64(EPT_POINTER));
9134 n = vmcs_read32(CR3_TARGET_COUNT);
9135 for (i = 0; i + 1 < n; i += 4)
9136 pr_err("CR3 target%u=%016lx target%u=%016lx\n",
9137 i, vmcs_readl(CR3_TARGET_VALUE0 + i * 2),
9138 i + 1, vmcs_readl(CR3_TARGET_VALUE0 + i * 2 + 2));
9140 pr_err("CR3 target%u=%016lx\n",
9141 i, vmcs_readl(CR3_TARGET_VALUE0 + i * 2));
9142 if (secondary_exec_control & SECONDARY_EXEC_PAUSE_LOOP_EXITING)
9143 pr_err("PLE Gap=%08x Window=%08x\n",
9144 vmcs_read32(PLE_GAP), vmcs_read32(PLE_WINDOW));
9145 if (secondary_exec_control & SECONDARY_EXEC_ENABLE_VPID)
9146 pr_err("Virtual processor ID = 0x%04x\n",
9147 vmcs_read16(VIRTUAL_PROCESSOR_ID));
9151 * The guest has exited. See if we can fix it or if we need userspace
9154 static int vmx_handle_exit(struct kvm_vcpu *vcpu)
9156 struct vcpu_vmx *vmx = to_vmx(vcpu);
9157 u32 exit_reason = vmx->exit_reason;
9158 u32 vectoring_info = vmx->idt_vectoring_info;
9160 trace_kvm_exit(exit_reason, vcpu, KVM_ISA_VMX);
9163 * Flush logged GPAs PML buffer, this will make dirty_bitmap more
9164 * updated. Another good is, in kvm_vm_ioctl_get_dirty_log, before
9165 * querying dirty_bitmap, we only need to kick all vcpus out of guest
9166 * mode as if vcpus is in root mode, the PML buffer must has been
9170 vmx_flush_pml_buffer(vcpu);
9172 /* If guest state is invalid, start emulating */
9173 if (vmx->emulation_required)
9174 return handle_invalid_guest_state(vcpu);
9176 if (is_guest_mode(vcpu) && nested_vmx_exit_reflected(vcpu, exit_reason))
9177 return nested_vmx_reflect_vmexit(vcpu, exit_reason);
9179 if (exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY) {
9181 vcpu->run->exit_reason = KVM_EXIT_FAIL_ENTRY;
9182 vcpu->run->fail_entry.hardware_entry_failure_reason
9187 if (unlikely(vmx->fail)) {
9188 vcpu->run->exit_reason = KVM_EXIT_FAIL_ENTRY;
9189 vcpu->run->fail_entry.hardware_entry_failure_reason
9190 = vmcs_read32(VM_INSTRUCTION_ERROR);
9196 * Do not try to fix EXIT_REASON_EPT_MISCONFIG if it caused by
9197 * delivery event since it indicates guest is accessing MMIO.
9198 * The vm-exit can be triggered again after return to guest that
9199 * will cause infinite loop.
9201 if ((vectoring_info & VECTORING_INFO_VALID_MASK) &&
9202 (exit_reason != EXIT_REASON_EXCEPTION_NMI &&
9203 exit_reason != EXIT_REASON_EPT_VIOLATION &&
9204 exit_reason != EXIT_REASON_PML_FULL &&
9205 exit_reason != EXIT_REASON_TASK_SWITCH)) {
9206 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
9207 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_DELIVERY_EV;
9208 vcpu->run->internal.ndata = 3;
9209 vcpu->run->internal.data[0] = vectoring_info;
9210 vcpu->run->internal.data[1] = exit_reason;
9211 vcpu->run->internal.data[2] = vcpu->arch.exit_qualification;
9212 if (exit_reason == EXIT_REASON_EPT_MISCONFIG) {
9213 vcpu->run->internal.ndata++;
9214 vcpu->run->internal.data[3] =
9215 vmcs_read64(GUEST_PHYSICAL_ADDRESS);
9220 if (unlikely(!enable_vnmi &&
9221 vmx->loaded_vmcs->soft_vnmi_blocked)) {
9222 if (vmx_interrupt_allowed(vcpu)) {
9223 vmx->loaded_vmcs->soft_vnmi_blocked = 0;
9224 } else if (vmx->loaded_vmcs->vnmi_blocked_time > 1000000000LL &&
9225 vcpu->arch.nmi_pending) {
9227 * This CPU don't support us in finding the end of an
9228 * NMI-blocked window if the guest runs with IRQs
9229 * disabled. So we pull the trigger after 1 s of
9230 * futile waiting, but inform the user about this.
9232 printk(KERN_WARNING "%s: Breaking out of NMI-blocked "
9233 "state on VCPU %d after 1 s timeout\n",
9234 __func__, vcpu->vcpu_id);
9235 vmx->loaded_vmcs->soft_vnmi_blocked = 0;
9239 if (exit_reason < kvm_vmx_max_exit_handlers
9240 && kvm_vmx_exit_handlers[exit_reason])
9241 return kvm_vmx_exit_handlers[exit_reason](vcpu);
9243 vcpu_unimpl(vcpu, "vmx: unexpected exit reason 0x%x\n",
9245 kvm_queue_exception(vcpu, UD_VECTOR);
9250 static void update_cr8_intercept(struct kvm_vcpu *vcpu, int tpr, int irr)
9252 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
9254 if (is_guest_mode(vcpu) &&
9255 nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW))
9258 if (irr == -1 || tpr < irr) {
9259 vmcs_write32(TPR_THRESHOLD, 0);
9263 vmcs_write32(TPR_THRESHOLD, irr);
9266 static void vmx_set_virtual_x2apic_mode(struct kvm_vcpu *vcpu, bool set)
9268 u32 sec_exec_control;
9270 /* Postpone execution until vmcs01 is the current VMCS. */
9271 if (is_guest_mode(vcpu)) {
9272 to_vmx(vcpu)->nested.change_vmcs01_virtual_x2apic_mode = true;
9276 if (!cpu_has_vmx_virtualize_x2apic_mode())
9279 if (!cpu_need_tpr_shadow(vcpu))
9282 sec_exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
9285 sec_exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
9286 sec_exec_control |= SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
9288 sec_exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
9289 sec_exec_control |= SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
9290 vmx_flush_tlb_ept_only(vcpu);
9292 vmcs_write32(SECONDARY_VM_EXEC_CONTROL, sec_exec_control);
9294 vmx_update_msr_bitmap(vcpu);
9297 static void vmx_set_apic_access_page_addr(struct kvm_vcpu *vcpu, hpa_t hpa)
9299 struct vcpu_vmx *vmx = to_vmx(vcpu);
9302 * Currently we do not handle the nested case where L2 has an
9303 * APIC access page of its own; that page is still pinned.
9304 * Hence, we skip the case where the VCPU is in guest mode _and_
9305 * L1 prepared an APIC access page for L2.
9307 * For the case where L1 and L2 share the same APIC access page
9308 * (flexpriority=Y but SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES clear
9309 * in the vmcs12), this function will only update either the vmcs01
9310 * or the vmcs02. If the former, the vmcs02 will be updated by
9311 * prepare_vmcs02. If the latter, the vmcs01 will be updated in
9312 * the next L2->L1 exit.
9314 if (!is_guest_mode(vcpu) ||
9315 !nested_cpu_has2(get_vmcs12(&vmx->vcpu),
9316 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) {
9317 vmcs_write64(APIC_ACCESS_ADDR, hpa);
9318 vmx_flush_tlb_ept_only(vcpu);
9322 static void vmx_hwapic_isr_update(struct kvm_vcpu *vcpu, int max_isr)
9330 status = vmcs_read16(GUEST_INTR_STATUS);
9332 if (max_isr != old) {
9334 status |= max_isr << 8;
9335 vmcs_write16(GUEST_INTR_STATUS, status);
9339 static void vmx_set_rvi(int vector)
9347 status = vmcs_read16(GUEST_INTR_STATUS);
9348 old = (u8)status & 0xff;
9349 if ((u8)vector != old) {
9351 status |= (u8)vector;
9352 vmcs_write16(GUEST_INTR_STATUS, status);
9356 static void vmx_hwapic_irr_update(struct kvm_vcpu *vcpu, int max_irr)
9359 * When running L2, updating RVI is only relevant when
9360 * vmcs12 virtual-interrupt-delivery enabled.
9361 * However, it can be enabled only when L1 also
9362 * intercepts external-interrupts and in that case
9363 * we should not update vmcs02 RVI but instead intercept
9364 * interrupt. Therefore, do nothing when running L2.
9366 if (!is_guest_mode(vcpu))
9367 vmx_set_rvi(max_irr);
9370 static int vmx_sync_pir_to_irr(struct kvm_vcpu *vcpu)
9372 struct vcpu_vmx *vmx = to_vmx(vcpu);
9374 bool max_irr_updated;
9376 WARN_ON(!vcpu->arch.apicv_active);
9377 if (pi_test_on(&vmx->pi_desc)) {
9378 pi_clear_on(&vmx->pi_desc);
9380 * IOMMU can write to PIR.ON, so the barrier matters even on UP.
9381 * But on x86 this is just a compiler barrier anyway.
9383 smp_mb__after_atomic();
9385 kvm_apic_update_irr(vcpu, vmx->pi_desc.pir, &max_irr);
9388 * If we are running L2 and L1 has a new pending interrupt
9389 * which can be injected, we should re-evaluate
9390 * what should be done with this new L1 interrupt.
9391 * If L1 intercepts external-interrupts, we should
9392 * exit from L2 to L1. Otherwise, interrupt should be
9393 * delivered directly to L2.
9395 if (is_guest_mode(vcpu) && max_irr_updated) {
9396 if (nested_exit_on_intr(vcpu))
9397 kvm_vcpu_exiting_guest_mode(vcpu);
9399 kvm_make_request(KVM_REQ_EVENT, vcpu);
9402 max_irr = kvm_lapic_find_highest_irr(vcpu);
9404 vmx_hwapic_irr_update(vcpu, max_irr);
9408 static void vmx_load_eoi_exitmap(struct kvm_vcpu *vcpu, u64 *eoi_exit_bitmap)
9410 if (!kvm_vcpu_apicv_active(vcpu))
9413 vmcs_write64(EOI_EXIT_BITMAP0, eoi_exit_bitmap[0]);
9414 vmcs_write64(EOI_EXIT_BITMAP1, eoi_exit_bitmap[1]);
9415 vmcs_write64(EOI_EXIT_BITMAP2, eoi_exit_bitmap[2]);
9416 vmcs_write64(EOI_EXIT_BITMAP3, eoi_exit_bitmap[3]);
9419 static void vmx_apicv_post_state_restore(struct kvm_vcpu *vcpu)
9421 struct vcpu_vmx *vmx = to_vmx(vcpu);
9423 pi_clear_on(&vmx->pi_desc);
9424 memset(vmx->pi_desc.pir, 0, sizeof(vmx->pi_desc.pir));
9427 static void vmx_complete_atomic_exit(struct vcpu_vmx *vmx)
9429 u32 exit_intr_info = 0;
9430 u16 basic_exit_reason = (u16)vmx->exit_reason;
9432 if (!(basic_exit_reason == EXIT_REASON_MCE_DURING_VMENTRY
9433 || basic_exit_reason == EXIT_REASON_EXCEPTION_NMI))
9436 if (!(vmx->exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY))
9437 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
9438 vmx->exit_intr_info = exit_intr_info;
9440 /* if exit due to PF check for async PF */
9441 if (is_page_fault(exit_intr_info))
9442 vmx->vcpu.arch.apf.host_apf_reason = kvm_read_and_reset_pf_reason();
9444 /* Handle machine checks before interrupts are enabled */
9445 if (basic_exit_reason == EXIT_REASON_MCE_DURING_VMENTRY ||
9446 is_machine_check(exit_intr_info))
9447 kvm_machine_check();
9449 /* We need to handle NMIs before interrupts are enabled */
9450 if (is_nmi(exit_intr_info)) {
9451 kvm_before_interrupt(&vmx->vcpu);
9453 kvm_after_interrupt(&vmx->vcpu);
9457 static void vmx_handle_external_intr(struct kvm_vcpu *vcpu)
9459 u32 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
9461 if ((exit_intr_info & (INTR_INFO_VALID_MASK | INTR_INFO_INTR_TYPE_MASK))
9462 == (INTR_INFO_VALID_MASK | INTR_TYPE_EXT_INTR)) {
9463 unsigned int vector;
9464 unsigned long entry;
9466 struct vcpu_vmx *vmx = to_vmx(vcpu);
9467 #ifdef CONFIG_X86_64
9471 vector = exit_intr_info & INTR_INFO_VECTOR_MASK;
9472 desc = (gate_desc *)vmx->host_idt_base + vector;
9473 entry = gate_offset(desc);
9475 #ifdef CONFIG_X86_64
9476 "mov %%" _ASM_SP ", %[sp]\n\t"
9477 "and $0xfffffffffffffff0, %%" _ASM_SP "\n\t"
9482 __ASM_SIZE(push) " $%c[cs]\n\t"
9485 #ifdef CONFIG_X86_64
9490 THUNK_TARGET(entry),
9491 [ss]"i"(__KERNEL_DS),
9492 [cs]"i"(__KERNEL_CS)
9496 STACK_FRAME_NON_STANDARD(vmx_handle_external_intr);
9498 static bool vmx_has_high_real_mode_segbase(void)
9500 return enable_unrestricted_guest || emulate_invalid_guest_state;
9503 static bool vmx_mpx_supported(void)
9505 return (vmcs_config.vmexit_ctrl & VM_EXIT_CLEAR_BNDCFGS) &&
9506 (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_BNDCFGS);
9509 static bool vmx_xsaves_supported(void)
9511 return vmcs_config.cpu_based_2nd_exec_ctrl &
9512 SECONDARY_EXEC_XSAVES;
9515 static bool vmx_umip_emulated(void)
9517 return vmcs_config.cpu_based_2nd_exec_ctrl &
9518 SECONDARY_EXEC_DESC;
9521 static void vmx_recover_nmi_blocking(struct vcpu_vmx *vmx)
9526 bool idtv_info_valid;
9528 idtv_info_valid = vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK;
9531 if (vmx->loaded_vmcs->nmi_known_unmasked)
9534 * Can't use vmx->exit_intr_info since we're not sure what
9535 * the exit reason is.
9537 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
9538 unblock_nmi = (exit_intr_info & INTR_INFO_UNBLOCK_NMI) != 0;
9539 vector = exit_intr_info & INTR_INFO_VECTOR_MASK;
9541 * SDM 3: 27.7.1.2 (September 2008)
9542 * Re-set bit "block by NMI" before VM entry if vmexit caused by
9543 * a guest IRET fault.
9544 * SDM 3: 23.2.2 (September 2008)
9545 * Bit 12 is undefined in any of the following cases:
9546 * If the VM exit sets the valid bit in the IDT-vectoring
9547 * information field.
9548 * If the VM exit is due to a double fault.
9550 if ((exit_intr_info & INTR_INFO_VALID_MASK) && unblock_nmi &&
9551 vector != DF_VECTOR && !idtv_info_valid)
9552 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
9553 GUEST_INTR_STATE_NMI);
9555 vmx->loaded_vmcs->nmi_known_unmasked =
9556 !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO)
9557 & GUEST_INTR_STATE_NMI);
9558 } else if (unlikely(vmx->loaded_vmcs->soft_vnmi_blocked))
9559 vmx->loaded_vmcs->vnmi_blocked_time +=
9560 ktime_to_ns(ktime_sub(ktime_get(),
9561 vmx->loaded_vmcs->entry_time));
9564 static void __vmx_complete_interrupts(struct kvm_vcpu *vcpu,
9565 u32 idt_vectoring_info,
9566 int instr_len_field,
9567 int error_code_field)
9571 bool idtv_info_valid;
9573 idtv_info_valid = idt_vectoring_info & VECTORING_INFO_VALID_MASK;
9575 vcpu->arch.nmi_injected = false;
9576 kvm_clear_exception_queue(vcpu);
9577 kvm_clear_interrupt_queue(vcpu);
9579 if (!idtv_info_valid)
9582 kvm_make_request(KVM_REQ_EVENT, vcpu);
9584 vector = idt_vectoring_info & VECTORING_INFO_VECTOR_MASK;
9585 type = idt_vectoring_info & VECTORING_INFO_TYPE_MASK;
9588 case INTR_TYPE_NMI_INTR:
9589 vcpu->arch.nmi_injected = true;
9591 * SDM 3: 27.7.1.2 (September 2008)
9592 * Clear bit "block by NMI" before VM entry if a NMI
9595 vmx_set_nmi_mask(vcpu, false);
9597 case INTR_TYPE_SOFT_EXCEPTION:
9598 vcpu->arch.event_exit_inst_len = vmcs_read32(instr_len_field);
9600 case INTR_TYPE_HARD_EXCEPTION:
9601 if (idt_vectoring_info & VECTORING_INFO_DELIVER_CODE_MASK) {
9602 u32 err = vmcs_read32(error_code_field);
9603 kvm_requeue_exception_e(vcpu, vector, err);
9605 kvm_requeue_exception(vcpu, vector);
9607 case INTR_TYPE_SOFT_INTR:
9608 vcpu->arch.event_exit_inst_len = vmcs_read32(instr_len_field);
9610 case INTR_TYPE_EXT_INTR:
9611 kvm_queue_interrupt(vcpu, vector, type == INTR_TYPE_SOFT_INTR);
9618 static void vmx_complete_interrupts(struct vcpu_vmx *vmx)
9620 __vmx_complete_interrupts(&vmx->vcpu, vmx->idt_vectoring_info,
9621 VM_EXIT_INSTRUCTION_LEN,
9622 IDT_VECTORING_ERROR_CODE);
9625 static void vmx_cancel_injection(struct kvm_vcpu *vcpu)
9627 __vmx_complete_interrupts(vcpu,
9628 vmcs_read32(VM_ENTRY_INTR_INFO_FIELD),
9629 VM_ENTRY_INSTRUCTION_LEN,
9630 VM_ENTRY_EXCEPTION_ERROR_CODE);
9632 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0);
9635 static void atomic_switch_perf_msrs(struct vcpu_vmx *vmx)
9638 struct perf_guest_switch_msr *msrs;
9640 msrs = perf_guest_get_msrs(&nr_msrs);
9645 for (i = 0; i < nr_msrs; i++)
9646 if (msrs[i].host == msrs[i].guest)
9647 clear_atomic_switch_msr(vmx, msrs[i].msr);
9649 add_atomic_switch_msr(vmx, msrs[i].msr, msrs[i].guest,
9653 static void vmx_arm_hv_timer(struct kvm_vcpu *vcpu)
9655 struct vcpu_vmx *vmx = to_vmx(vcpu);
9659 if (vmx->hv_deadline_tsc == -1)
9663 if (vmx->hv_deadline_tsc > tscl)
9664 /* sure to be 32 bit only because checked on set_hv_timer */
9665 delta_tsc = (u32)((vmx->hv_deadline_tsc - tscl) >>
9666 cpu_preemption_timer_multi);
9670 vmcs_write32(VMX_PREEMPTION_TIMER_VALUE, delta_tsc);
9673 static void __noclone vmx_vcpu_run(struct kvm_vcpu *vcpu)
9675 struct vcpu_vmx *vmx = to_vmx(vcpu);
9676 unsigned long cr3, cr4, evmcs_rsp;
9678 /* Record the guest's net vcpu time for enforced NMI injections. */
9679 if (unlikely(!enable_vnmi &&
9680 vmx->loaded_vmcs->soft_vnmi_blocked))
9681 vmx->loaded_vmcs->entry_time = ktime_get();
9683 /* Don't enter VMX if guest state is invalid, let the exit handler
9684 start emulation until we arrive back to a valid state */
9685 if (vmx->emulation_required)
9688 if (vmx->ple_window_dirty) {
9689 vmx->ple_window_dirty = false;
9690 vmcs_write32(PLE_WINDOW, vmx->ple_window);
9693 if (vmx->nested.sync_shadow_vmcs) {
9694 copy_vmcs12_to_shadow(vmx);
9695 vmx->nested.sync_shadow_vmcs = false;
9698 if (test_bit(VCPU_REGS_RSP, (unsigned long *)&vcpu->arch.regs_dirty))
9699 vmcs_writel(GUEST_RSP, vcpu->arch.regs[VCPU_REGS_RSP]);
9700 if (test_bit(VCPU_REGS_RIP, (unsigned long *)&vcpu->arch.regs_dirty))
9701 vmcs_writel(GUEST_RIP, vcpu->arch.regs[VCPU_REGS_RIP]);
9703 cr3 = __get_current_cr3_fast();
9704 if (unlikely(cr3 != vmx->loaded_vmcs->vmcs_host_cr3)) {
9705 vmcs_writel(HOST_CR3, cr3);
9706 vmx->loaded_vmcs->vmcs_host_cr3 = cr3;
9709 cr4 = cr4_read_shadow();
9710 if (unlikely(cr4 != vmx->loaded_vmcs->vmcs_host_cr4)) {
9711 vmcs_writel(HOST_CR4, cr4);
9712 vmx->loaded_vmcs->vmcs_host_cr4 = cr4;
9715 /* When single-stepping over STI and MOV SS, we must clear the
9716 * corresponding interruptibility bits in the guest state. Otherwise
9717 * vmentry fails as it then expects bit 14 (BS) in pending debug
9718 * exceptions being set, but that's not correct for the guest debugging
9720 if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP)
9721 vmx_set_interrupt_shadow(vcpu, 0);
9723 if (static_cpu_has(X86_FEATURE_PKU) &&
9724 kvm_read_cr4_bits(vcpu, X86_CR4_PKE) &&
9725 vcpu->arch.pkru != vmx->host_pkru)
9726 __write_pkru(vcpu->arch.pkru);
9728 atomic_switch_perf_msrs(vmx);
9730 vmx_arm_hv_timer(vcpu);
9733 * If this vCPU has touched SPEC_CTRL, restore the guest's value if
9734 * it's non-zero. Since vmentry is serialising on affected CPUs, there
9735 * is no need to worry about the conditional branch over the wrmsr
9736 * being speculatively taken.
9739 native_wrmsrl(MSR_IA32_SPEC_CTRL, vmx->spec_ctrl);
9741 vmx->__launched = vmx->loaded_vmcs->launched;
9743 evmcs_rsp = static_branch_unlikely(&enable_evmcs) ?
9744 (unsigned long)¤t_evmcs->host_rsp : 0;
9747 /* Store host registers */
9748 "push %%" _ASM_DX "; push %%" _ASM_BP ";"
9749 "push %%" _ASM_CX " \n\t" /* placeholder for guest rcx */
9750 "push %%" _ASM_CX " \n\t"
9751 "cmp %%" _ASM_SP ", %c[host_rsp](%0) \n\t"
9753 "mov %%" _ASM_SP ", %c[host_rsp](%0) \n\t"
9754 /* Avoid VMWRITE when Enlightened VMCS is in use */
9755 "test %%" _ASM_SI ", %%" _ASM_SI " \n\t"
9757 "mov %%" _ASM_SP ", (%%" _ASM_SI ") \n\t"
9760 __ex(ASM_VMX_VMWRITE_RSP_RDX) "\n\t"
9762 /* Reload cr2 if changed */
9763 "mov %c[cr2](%0), %%" _ASM_AX " \n\t"
9764 "mov %%cr2, %%" _ASM_DX " \n\t"
9765 "cmp %%" _ASM_AX ", %%" _ASM_DX " \n\t"
9767 "mov %%" _ASM_AX", %%cr2 \n\t"
9769 /* Check if vmlaunch of vmresume is needed */
9770 "cmpl $0, %c[launched](%0) \n\t"
9771 /* Load guest registers. Don't clobber flags. */
9772 "mov %c[rax](%0), %%" _ASM_AX " \n\t"
9773 "mov %c[rbx](%0), %%" _ASM_BX " \n\t"
9774 "mov %c[rdx](%0), %%" _ASM_DX " \n\t"
9775 "mov %c[rsi](%0), %%" _ASM_SI " \n\t"
9776 "mov %c[rdi](%0), %%" _ASM_DI " \n\t"
9777 "mov %c[rbp](%0), %%" _ASM_BP " \n\t"
9778 #ifdef CONFIG_X86_64
9779 "mov %c[r8](%0), %%r8 \n\t"
9780 "mov %c[r9](%0), %%r9 \n\t"
9781 "mov %c[r10](%0), %%r10 \n\t"
9782 "mov %c[r11](%0), %%r11 \n\t"
9783 "mov %c[r12](%0), %%r12 \n\t"
9784 "mov %c[r13](%0), %%r13 \n\t"
9785 "mov %c[r14](%0), %%r14 \n\t"
9786 "mov %c[r15](%0), %%r15 \n\t"
9788 "mov %c[rcx](%0), %%" _ASM_CX " \n\t" /* kills %0 (ecx) */
9790 /* Enter guest mode */
9792 __ex(ASM_VMX_VMLAUNCH) "\n\t"
9794 "1: " __ex(ASM_VMX_VMRESUME) "\n\t"
9796 /* Save guest registers, load host registers, keep flags */
9797 "mov %0, %c[wordsize](%%" _ASM_SP ") \n\t"
9799 "setbe %c[fail](%0)\n\t"
9800 "mov %%" _ASM_AX ", %c[rax](%0) \n\t"
9801 "mov %%" _ASM_BX ", %c[rbx](%0) \n\t"
9802 __ASM_SIZE(pop) " %c[rcx](%0) \n\t"
9803 "mov %%" _ASM_DX ", %c[rdx](%0) \n\t"
9804 "mov %%" _ASM_SI ", %c[rsi](%0) \n\t"
9805 "mov %%" _ASM_DI ", %c[rdi](%0) \n\t"
9806 "mov %%" _ASM_BP ", %c[rbp](%0) \n\t"
9807 #ifdef CONFIG_X86_64
9808 "mov %%r8, %c[r8](%0) \n\t"
9809 "mov %%r9, %c[r9](%0) \n\t"
9810 "mov %%r10, %c[r10](%0) \n\t"
9811 "mov %%r11, %c[r11](%0) \n\t"
9812 "mov %%r12, %c[r12](%0) \n\t"
9813 "mov %%r13, %c[r13](%0) \n\t"
9814 "mov %%r14, %c[r14](%0) \n\t"
9815 "mov %%r15, %c[r15](%0) \n\t"
9816 "xor %%r8d, %%r8d \n\t"
9817 "xor %%r9d, %%r9d \n\t"
9818 "xor %%r10d, %%r10d \n\t"
9819 "xor %%r11d, %%r11d \n\t"
9820 "xor %%r12d, %%r12d \n\t"
9821 "xor %%r13d, %%r13d \n\t"
9822 "xor %%r14d, %%r14d \n\t"
9823 "xor %%r15d, %%r15d \n\t"
9825 "mov %%cr2, %%" _ASM_AX " \n\t"
9826 "mov %%" _ASM_AX ", %c[cr2](%0) \n\t"
9828 "xor %%eax, %%eax \n\t"
9829 "xor %%ebx, %%ebx \n\t"
9830 "xor %%esi, %%esi \n\t"
9831 "xor %%edi, %%edi \n\t"
9832 "pop %%" _ASM_BP "; pop %%" _ASM_DX " \n\t"
9833 ".pushsection .rodata \n\t"
9834 ".global vmx_return \n\t"
9835 "vmx_return: " _ASM_PTR " 2b \n\t"
9837 : : "c"(vmx), "d"((unsigned long)HOST_RSP), "S"(evmcs_rsp),
9838 [launched]"i"(offsetof(struct vcpu_vmx, __launched)),
9839 [fail]"i"(offsetof(struct vcpu_vmx, fail)),
9840 [host_rsp]"i"(offsetof(struct vcpu_vmx, host_rsp)),
9841 [rax]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RAX])),
9842 [rbx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBX])),
9843 [rcx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RCX])),
9844 [rdx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDX])),
9845 [rsi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RSI])),
9846 [rdi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDI])),
9847 [rbp]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBP])),
9848 #ifdef CONFIG_X86_64
9849 [r8]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R8])),
9850 [r9]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R9])),
9851 [r10]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R10])),
9852 [r11]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R11])),
9853 [r12]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R12])),
9854 [r13]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R13])),
9855 [r14]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R14])),
9856 [r15]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R15])),
9858 [cr2]"i"(offsetof(struct vcpu_vmx, vcpu.arch.cr2)),
9859 [wordsize]"i"(sizeof(ulong))
9861 #ifdef CONFIG_X86_64
9862 , "rax", "rbx", "rdi"
9863 , "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15"
9865 , "eax", "ebx", "edi"
9870 * We do not use IBRS in the kernel. If this vCPU has used the
9871 * SPEC_CTRL MSR it may have left it on; save the value and
9872 * turn it off. This is much more efficient than blindly adding
9873 * it to the atomic save/restore list. Especially as the former
9874 * (Saving guest MSRs on vmexit) doesn't even exist in KVM.
9876 * For non-nested case:
9877 * If the L01 MSR bitmap does not intercept the MSR, then we need to
9881 * If the L02 MSR bitmap does not intercept the MSR, then we need to
9884 if (unlikely(!msr_write_intercepted(vcpu, MSR_IA32_SPEC_CTRL)))
9885 vmx->spec_ctrl = native_read_msr(MSR_IA32_SPEC_CTRL);
9888 native_wrmsrl(MSR_IA32_SPEC_CTRL, 0);
9890 /* Eliminate branch target predictions from guest mode */
9893 /* All fields are clean at this point */
9894 if (static_branch_unlikely(&enable_evmcs))
9895 current_evmcs->hv_clean_fields |=
9896 HV_VMX_ENLIGHTENED_CLEAN_FIELD_ALL;
9898 /* MSR_IA32_DEBUGCTLMSR is zeroed on vmexit. Restore it if needed */
9899 if (vmx->host_debugctlmsr)
9900 update_debugctlmsr(vmx->host_debugctlmsr);
9902 #ifndef CONFIG_X86_64
9904 * The sysexit path does not restore ds/es, so we must set them to
9905 * a reasonable value ourselves.
9907 * We can't defer this to vmx_load_host_state() since that function
9908 * may be executed in interrupt context, which saves and restore segments
9909 * around it, nullifying its effect.
9911 loadsegment(ds, __USER_DS);
9912 loadsegment(es, __USER_DS);
9915 vcpu->arch.regs_avail = ~((1 << VCPU_REGS_RIP) | (1 << VCPU_REGS_RSP)
9916 | (1 << VCPU_EXREG_RFLAGS)
9917 | (1 << VCPU_EXREG_PDPTR)
9918 | (1 << VCPU_EXREG_SEGMENTS)
9919 | (1 << VCPU_EXREG_CR3));
9920 vcpu->arch.regs_dirty = 0;
9923 * eager fpu is enabled if PKEY is supported and CR4 is switched
9924 * back on host, so it is safe to read guest PKRU from current
9927 if (static_cpu_has(X86_FEATURE_PKU) &&
9928 kvm_read_cr4_bits(vcpu, X86_CR4_PKE)) {
9929 vcpu->arch.pkru = __read_pkru();
9930 if (vcpu->arch.pkru != vmx->host_pkru)
9931 __write_pkru(vmx->host_pkru);
9934 vmx->nested.nested_run_pending = 0;
9935 vmx->idt_vectoring_info = 0;
9937 vmx->exit_reason = vmx->fail ? 0xdead : vmcs_read32(VM_EXIT_REASON);
9938 if (vmx->fail || (vmx->exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY))
9941 vmx->loaded_vmcs->launched = 1;
9942 vmx->idt_vectoring_info = vmcs_read32(IDT_VECTORING_INFO_FIELD);
9944 vmx_complete_atomic_exit(vmx);
9945 vmx_recover_nmi_blocking(vmx);
9946 vmx_complete_interrupts(vmx);
9948 STACK_FRAME_NON_STANDARD(vmx_vcpu_run);
9950 static struct kvm *vmx_vm_alloc(void)
9952 struct kvm_vmx *kvm_vmx = kzalloc(sizeof(struct kvm_vmx), GFP_KERNEL);
9953 return &kvm_vmx->kvm;
9956 static void vmx_vm_free(struct kvm *kvm)
9958 kfree(to_kvm_vmx(kvm));
9961 static void vmx_switch_vmcs(struct kvm_vcpu *vcpu, struct loaded_vmcs *vmcs)
9963 struct vcpu_vmx *vmx = to_vmx(vcpu);
9966 if (vmx->loaded_vmcs == vmcs)
9970 vmx->loaded_vmcs = vmcs;
9972 vmx_vcpu_load(vcpu, cpu);
9977 * Ensure that the current vmcs of the logical processor is the
9978 * vmcs01 of the vcpu before calling free_nested().
9980 static void vmx_free_vcpu_nested(struct kvm_vcpu *vcpu)
9982 struct vcpu_vmx *vmx = to_vmx(vcpu);
9985 vmx_switch_vmcs(vcpu, &vmx->vmcs01);
9990 static void vmx_free_vcpu(struct kvm_vcpu *vcpu)
9992 struct vcpu_vmx *vmx = to_vmx(vcpu);
9995 vmx_destroy_pml_buffer(vmx);
9996 free_vpid(vmx->vpid);
9997 leave_guest_mode(vcpu);
9998 vmx_free_vcpu_nested(vcpu);
9999 free_loaded_vmcs(vmx->loaded_vmcs);
10000 kfree(vmx->guest_msrs);
10001 kvm_vcpu_uninit(vcpu);
10002 kmem_cache_free(kvm_vcpu_cache, vmx);
10005 static struct kvm_vcpu *vmx_create_vcpu(struct kvm *kvm, unsigned int id)
10008 struct vcpu_vmx *vmx = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
10009 unsigned long *msr_bitmap;
10013 return ERR_PTR(-ENOMEM);
10015 vmx->vpid = allocate_vpid();
10017 err = kvm_vcpu_init(&vmx->vcpu, kvm, id);
10024 * If PML is turned on, failure on enabling PML just results in failure
10025 * of creating the vcpu, therefore we can simplify PML logic (by
10026 * avoiding dealing with cases, such as enabling PML partially on vcpus
10027 * for the guest, etc.
10030 vmx->pml_pg = alloc_page(GFP_KERNEL | __GFP_ZERO);
10035 vmx->guest_msrs = kmalloc(PAGE_SIZE, GFP_KERNEL);
10036 BUILD_BUG_ON(ARRAY_SIZE(vmx_msr_index) * sizeof(vmx->guest_msrs[0])
10039 if (!vmx->guest_msrs)
10042 err = alloc_loaded_vmcs(&vmx->vmcs01);
10046 msr_bitmap = vmx->vmcs01.msr_bitmap;
10047 vmx_disable_intercept_for_msr(msr_bitmap, MSR_FS_BASE, MSR_TYPE_RW);
10048 vmx_disable_intercept_for_msr(msr_bitmap, MSR_GS_BASE, MSR_TYPE_RW);
10049 vmx_disable_intercept_for_msr(msr_bitmap, MSR_KERNEL_GS_BASE, MSR_TYPE_RW);
10050 vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_SYSENTER_CS, MSR_TYPE_RW);
10051 vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_SYSENTER_ESP, MSR_TYPE_RW);
10052 vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_SYSENTER_EIP, MSR_TYPE_RW);
10053 vmx->msr_bitmap_mode = 0;
10055 vmx->loaded_vmcs = &vmx->vmcs01;
10057 vmx_vcpu_load(&vmx->vcpu, cpu);
10058 vmx->vcpu.cpu = cpu;
10059 vmx_vcpu_setup(vmx);
10060 vmx_vcpu_put(&vmx->vcpu);
10062 if (cpu_need_virtualize_apic_accesses(&vmx->vcpu)) {
10063 err = alloc_apic_access_page(kvm);
10068 if (enable_ept && !enable_unrestricted_guest) {
10069 err = init_rmode_identity_map(kvm);
10075 nested_vmx_setup_ctls_msrs(&vmx->nested.msrs,
10076 kvm_vcpu_apicv_active(&vmx->vcpu));
10077 vmx->nested.vpid02 = allocate_vpid();
10080 vmx->nested.posted_intr_nv = -1;
10081 vmx->nested.current_vmptr = -1ull;
10083 vmx->msr_ia32_feature_control_valid_bits = FEATURE_CONTROL_LOCKED;
10086 * Enforce invariant: pi_desc.nv is always either POSTED_INTR_VECTOR
10087 * or POSTED_INTR_WAKEUP_VECTOR.
10089 vmx->pi_desc.nv = POSTED_INTR_VECTOR;
10090 vmx->pi_desc.sn = 1;
10095 free_vpid(vmx->nested.vpid02);
10096 free_loaded_vmcs(vmx->loaded_vmcs);
10098 kfree(vmx->guest_msrs);
10100 vmx_destroy_pml_buffer(vmx);
10102 kvm_vcpu_uninit(&vmx->vcpu);
10104 free_vpid(vmx->vpid);
10105 kmem_cache_free(kvm_vcpu_cache, vmx);
10106 return ERR_PTR(err);
10109 static int vmx_vm_init(struct kvm *kvm)
10112 kvm->arch.pause_in_guest = true;
10116 static void __init vmx_check_processor_compat(void *rtn)
10118 struct vmcs_config vmcs_conf;
10121 if (setup_vmcs_config(&vmcs_conf) < 0)
10122 *(int *)rtn = -EIO;
10123 nested_vmx_setup_ctls_msrs(&vmcs_conf.nested, enable_apicv);
10124 if (memcmp(&vmcs_config, &vmcs_conf, sizeof(struct vmcs_config)) != 0) {
10125 printk(KERN_ERR "kvm: CPU %d feature inconsistency!\n",
10126 smp_processor_id());
10127 *(int *)rtn = -EIO;
10131 static u64 vmx_get_mt_mask(struct kvm_vcpu *vcpu, gfn_t gfn, bool is_mmio)
10136 /* For VT-d and EPT combination
10137 * 1. MMIO: always map as UC
10138 * 2. EPT with VT-d:
10139 * a. VT-d without snooping control feature: can't guarantee the
10140 * result, try to trust guest.
10141 * b. VT-d with snooping control feature: snooping control feature of
10142 * VT-d engine can guarantee the cache correctness. Just set it
10143 * to WB to keep consistent with host. So the same as item 3.
10144 * 3. EPT without VT-d: always map as WB and set IPAT=1 to keep
10145 * consistent with host MTRR
10148 cache = MTRR_TYPE_UNCACHABLE;
10152 if (!kvm_arch_has_noncoherent_dma(vcpu->kvm)) {
10153 ipat = VMX_EPT_IPAT_BIT;
10154 cache = MTRR_TYPE_WRBACK;
10158 if (kvm_read_cr0(vcpu) & X86_CR0_CD) {
10159 ipat = VMX_EPT_IPAT_BIT;
10160 if (kvm_check_has_quirk(vcpu->kvm, KVM_X86_QUIRK_CD_NW_CLEARED))
10161 cache = MTRR_TYPE_WRBACK;
10163 cache = MTRR_TYPE_UNCACHABLE;
10167 cache = kvm_mtrr_get_guest_memory_type(vcpu, gfn);
10170 return (cache << VMX_EPT_MT_EPTE_SHIFT) | ipat;
10173 static int vmx_get_lpage_level(void)
10175 if (enable_ept && !cpu_has_vmx_ept_1g_page())
10176 return PT_DIRECTORY_LEVEL;
10178 /* For shadow and EPT supported 1GB page */
10179 return PT_PDPE_LEVEL;
10182 static void vmcs_set_secondary_exec_control(u32 new_ctl)
10185 * These bits in the secondary execution controls field
10186 * are dynamic, the others are mostly based on the hypervisor
10187 * architecture and the guest's CPUID. Do not touch the
10191 SECONDARY_EXEC_SHADOW_VMCS |
10192 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
10193 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
10194 SECONDARY_EXEC_DESC;
10196 u32 cur_ctl = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
10198 vmcs_write32(SECONDARY_VM_EXEC_CONTROL,
10199 (new_ctl & ~mask) | (cur_ctl & mask));
10203 * Generate MSR_IA32_VMX_CR{0,4}_FIXED1 according to CPUID. Only set bits
10204 * (indicating "allowed-1") if they are supported in the guest's CPUID.
10206 static void nested_vmx_cr_fixed1_bits_update(struct kvm_vcpu *vcpu)
10208 struct vcpu_vmx *vmx = to_vmx(vcpu);
10209 struct kvm_cpuid_entry2 *entry;
10211 vmx->nested.msrs.cr0_fixed1 = 0xffffffff;
10212 vmx->nested.msrs.cr4_fixed1 = X86_CR4_PCE;
10214 #define cr4_fixed1_update(_cr4_mask, _reg, _cpuid_mask) do { \
10215 if (entry && (entry->_reg & (_cpuid_mask))) \
10216 vmx->nested.msrs.cr4_fixed1 |= (_cr4_mask); \
10219 entry = kvm_find_cpuid_entry(vcpu, 0x1, 0);
10220 cr4_fixed1_update(X86_CR4_VME, edx, bit(X86_FEATURE_VME));
10221 cr4_fixed1_update(X86_CR4_PVI, edx, bit(X86_FEATURE_VME));
10222 cr4_fixed1_update(X86_CR4_TSD, edx, bit(X86_FEATURE_TSC));
10223 cr4_fixed1_update(X86_CR4_DE, edx, bit(X86_FEATURE_DE));
10224 cr4_fixed1_update(X86_CR4_PSE, edx, bit(X86_FEATURE_PSE));
10225 cr4_fixed1_update(X86_CR4_PAE, edx, bit(X86_FEATURE_PAE));
10226 cr4_fixed1_update(X86_CR4_MCE, edx, bit(X86_FEATURE_MCE));
10227 cr4_fixed1_update(X86_CR4_PGE, edx, bit(X86_FEATURE_PGE));
10228 cr4_fixed1_update(X86_CR4_OSFXSR, edx, bit(X86_FEATURE_FXSR));
10229 cr4_fixed1_update(X86_CR4_OSXMMEXCPT, edx, bit(X86_FEATURE_XMM));
10230 cr4_fixed1_update(X86_CR4_VMXE, ecx, bit(X86_FEATURE_VMX));
10231 cr4_fixed1_update(X86_CR4_SMXE, ecx, bit(X86_FEATURE_SMX));
10232 cr4_fixed1_update(X86_CR4_PCIDE, ecx, bit(X86_FEATURE_PCID));
10233 cr4_fixed1_update(X86_CR4_OSXSAVE, ecx, bit(X86_FEATURE_XSAVE));
10235 entry = kvm_find_cpuid_entry(vcpu, 0x7, 0);
10236 cr4_fixed1_update(X86_CR4_FSGSBASE, ebx, bit(X86_FEATURE_FSGSBASE));
10237 cr4_fixed1_update(X86_CR4_SMEP, ebx, bit(X86_FEATURE_SMEP));
10238 cr4_fixed1_update(X86_CR4_SMAP, ebx, bit(X86_FEATURE_SMAP));
10239 cr4_fixed1_update(X86_CR4_PKE, ecx, bit(X86_FEATURE_PKU));
10240 cr4_fixed1_update(X86_CR4_UMIP, ecx, bit(X86_FEATURE_UMIP));
10242 #undef cr4_fixed1_update
10245 static void vmx_cpuid_update(struct kvm_vcpu *vcpu)
10247 struct vcpu_vmx *vmx = to_vmx(vcpu);
10249 if (cpu_has_secondary_exec_ctrls()) {
10250 vmx_compute_secondary_exec_control(vmx);
10251 vmcs_set_secondary_exec_control(vmx->secondary_exec_control);
10254 if (nested_vmx_allowed(vcpu))
10255 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits |=
10256 FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
10258 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits &=
10259 ~FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
10261 if (nested_vmx_allowed(vcpu))
10262 nested_vmx_cr_fixed1_bits_update(vcpu);
10265 static void vmx_set_supported_cpuid(u32 func, struct kvm_cpuid_entry2 *entry)
10267 if (func == 1 && nested)
10268 entry->ecx |= bit(X86_FEATURE_VMX);
10271 static void nested_ept_inject_page_fault(struct kvm_vcpu *vcpu,
10272 struct x86_exception *fault)
10274 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
10275 struct vcpu_vmx *vmx = to_vmx(vcpu);
10277 unsigned long exit_qualification = vcpu->arch.exit_qualification;
10279 if (vmx->nested.pml_full) {
10280 exit_reason = EXIT_REASON_PML_FULL;
10281 vmx->nested.pml_full = false;
10282 exit_qualification &= INTR_INFO_UNBLOCK_NMI;
10283 } else if (fault->error_code & PFERR_RSVD_MASK)
10284 exit_reason = EXIT_REASON_EPT_MISCONFIG;
10286 exit_reason = EXIT_REASON_EPT_VIOLATION;
10288 nested_vmx_vmexit(vcpu, exit_reason, 0, exit_qualification);
10289 vmcs12->guest_physical_address = fault->address;
10292 static bool nested_ept_ad_enabled(struct kvm_vcpu *vcpu)
10294 return nested_ept_get_cr3(vcpu) & VMX_EPTP_AD_ENABLE_BIT;
10297 /* Callbacks for nested_ept_init_mmu_context: */
10299 static unsigned long nested_ept_get_cr3(struct kvm_vcpu *vcpu)
10301 /* return the page table to be shadowed - in our case, EPT12 */
10302 return get_vmcs12(vcpu)->ept_pointer;
10305 static int nested_ept_init_mmu_context(struct kvm_vcpu *vcpu)
10307 WARN_ON(mmu_is_nested(vcpu));
10308 if (!valid_ept_address(vcpu, nested_ept_get_cr3(vcpu)))
10311 kvm_mmu_unload(vcpu);
10312 kvm_init_shadow_ept_mmu(vcpu,
10313 to_vmx(vcpu)->nested.msrs.ept_caps &
10314 VMX_EPT_EXECUTE_ONLY_BIT,
10315 nested_ept_ad_enabled(vcpu));
10316 vcpu->arch.mmu.set_cr3 = vmx_set_cr3;
10317 vcpu->arch.mmu.get_cr3 = nested_ept_get_cr3;
10318 vcpu->arch.mmu.inject_page_fault = nested_ept_inject_page_fault;
10320 vcpu->arch.walk_mmu = &vcpu->arch.nested_mmu;
10324 static void nested_ept_uninit_mmu_context(struct kvm_vcpu *vcpu)
10326 vcpu->arch.walk_mmu = &vcpu->arch.mmu;
10329 static bool nested_vmx_is_page_fault_vmexit(struct vmcs12 *vmcs12,
10332 bool inequality, bit;
10334 bit = (vmcs12->exception_bitmap & (1u << PF_VECTOR)) != 0;
10336 (error_code & vmcs12->page_fault_error_code_mask) !=
10337 vmcs12->page_fault_error_code_match;
10338 return inequality ^ bit;
10341 static void vmx_inject_page_fault_nested(struct kvm_vcpu *vcpu,
10342 struct x86_exception *fault)
10344 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
10346 WARN_ON(!is_guest_mode(vcpu));
10348 if (nested_vmx_is_page_fault_vmexit(vmcs12, fault->error_code) &&
10349 !to_vmx(vcpu)->nested.nested_run_pending) {
10350 vmcs12->vm_exit_intr_error_code = fault->error_code;
10351 nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI,
10352 PF_VECTOR | INTR_TYPE_HARD_EXCEPTION |
10353 INTR_INFO_DELIVER_CODE_MASK | INTR_INFO_VALID_MASK,
10356 kvm_inject_page_fault(vcpu, fault);
10360 static inline bool nested_vmx_prepare_msr_bitmap(struct kvm_vcpu *vcpu,
10361 struct vmcs12 *vmcs12);
10363 static void nested_get_vmcs12_pages(struct kvm_vcpu *vcpu,
10364 struct vmcs12 *vmcs12)
10366 struct vcpu_vmx *vmx = to_vmx(vcpu);
10370 if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) {
10372 * Translate L1 physical address to host physical
10373 * address for vmcs02. Keep the page pinned, so this
10374 * physical address remains valid. We keep a reference
10375 * to it so we can release it later.
10377 if (vmx->nested.apic_access_page) { /* shouldn't happen */
10378 kvm_release_page_dirty(vmx->nested.apic_access_page);
10379 vmx->nested.apic_access_page = NULL;
10381 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->apic_access_addr);
10383 * If translation failed, no matter: This feature asks
10384 * to exit when accessing the given address, and if it
10385 * can never be accessed, this feature won't do
10388 if (!is_error_page(page)) {
10389 vmx->nested.apic_access_page = page;
10390 hpa = page_to_phys(vmx->nested.apic_access_page);
10391 vmcs_write64(APIC_ACCESS_ADDR, hpa);
10393 vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL,
10394 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES);
10396 } else if (!(nested_cpu_has_virt_x2apic_mode(vmcs12)) &&
10397 cpu_need_virtualize_apic_accesses(&vmx->vcpu)) {
10398 vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL,
10399 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES);
10400 kvm_vcpu_reload_apic_access_page(vcpu);
10403 if (nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW)) {
10404 if (vmx->nested.virtual_apic_page) { /* shouldn't happen */
10405 kvm_release_page_dirty(vmx->nested.virtual_apic_page);
10406 vmx->nested.virtual_apic_page = NULL;
10408 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->virtual_apic_page_addr);
10411 * If translation failed, VM entry will fail because
10412 * prepare_vmcs02 set VIRTUAL_APIC_PAGE_ADDR to -1ull.
10413 * Failing the vm entry is _not_ what the processor
10414 * does but it's basically the only possibility we
10415 * have. We could still enter the guest if CR8 load
10416 * exits are enabled, CR8 store exits are enabled, and
10417 * virtualize APIC access is disabled; in this case
10418 * the processor would never use the TPR shadow and we
10419 * could simply clear the bit from the execution
10420 * control. But such a configuration is useless, so
10421 * let's keep the code simple.
10423 if (!is_error_page(page)) {
10424 vmx->nested.virtual_apic_page = page;
10425 hpa = page_to_phys(vmx->nested.virtual_apic_page);
10426 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, hpa);
10430 if (nested_cpu_has_posted_intr(vmcs12)) {
10431 if (vmx->nested.pi_desc_page) { /* shouldn't happen */
10432 kunmap(vmx->nested.pi_desc_page);
10433 kvm_release_page_dirty(vmx->nested.pi_desc_page);
10434 vmx->nested.pi_desc_page = NULL;
10436 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->posted_intr_desc_addr);
10437 if (is_error_page(page))
10439 vmx->nested.pi_desc_page = page;
10440 vmx->nested.pi_desc = kmap(vmx->nested.pi_desc_page);
10441 vmx->nested.pi_desc =
10442 (struct pi_desc *)((void *)vmx->nested.pi_desc +
10443 (unsigned long)(vmcs12->posted_intr_desc_addr &
10445 vmcs_write64(POSTED_INTR_DESC_ADDR,
10446 page_to_phys(vmx->nested.pi_desc_page) +
10447 (unsigned long)(vmcs12->posted_intr_desc_addr &
10450 if (nested_vmx_prepare_msr_bitmap(vcpu, vmcs12))
10451 vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL,
10452 CPU_BASED_USE_MSR_BITMAPS);
10454 vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL,
10455 CPU_BASED_USE_MSR_BITMAPS);
10458 static void vmx_start_preemption_timer(struct kvm_vcpu *vcpu)
10460 u64 preemption_timeout = get_vmcs12(vcpu)->vmx_preemption_timer_value;
10461 struct vcpu_vmx *vmx = to_vmx(vcpu);
10463 if (vcpu->arch.virtual_tsc_khz == 0)
10466 /* Make sure short timeouts reliably trigger an immediate vmexit.
10467 * hrtimer_start does not guarantee this. */
10468 if (preemption_timeout <= 1) {
10469 vmx_preemption_timer_fn(&vmx->nested.preemption_timer);
10473 preemption_timeout <<= VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE;
10474 preemption_timeout *= 1000000;
10475 do_div(preemption_timeout, vcpu->arch.virtual_tsc_khz);
10476 hrtimer_start(&vmx->nested.preemption_timer,
10477 ns_to_ktime(preemption_timeout), HRTIMER_MODE_REL);
10480 static int nested_vmx_check_io_bitmap_controls(struct kvm_vcpu *vcpu,
10481 struct vmcs12 *vmcs12)
10483 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_IO_BITMAPS))
10486 if (!page_address_valid(vcpu, vmcs12->io_bitmap_a) ||
10487 !page_address_valid(vcpu, vmcs12->io_bitmap_b))
10493 static int nested_vmx_check_msr_bitmap_controls(struct kvm_vcpu *vcpu,
10494 struct vmcs12 *vmcs12)
10496 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS))
10499 if (!page_address_valid(vcpu, vmcs12->msr_bitmap))
10505 static int nested_vmx_check_tpr_shadow_controls(struct kvm_vcpu *vcpu,
10506 struct vmcs12 *vmcs12)
10508 if (!nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW))
10511 if (!page_address_valid(vcpu, vmcs12->virtual_apic_page_addr))
10518 * Merge L0's and L1's MSR bitmap, return false to indicate that
10519 * we do not use the hardware.
10521 static inline bool nested_vmx_prepare_msr_bitmap(struct kvm_vcpu *vcpu,
10522 struct vmcs12 *vmcs12)
10526 unsigned long *msr_bitmap_l1;
10527 unsigned long *msr_bitmap_l0 = to_vmx(vcpu)->nested.vmcs02.msr_bitmap;
10529 * pred_cmd & spec_ctrl are trying to verify two things:
10531 * 1. L0 gave a permission to L1 to actually passthrough the MSR. This
10532 * ensures that we do not accidentally generate an L02 MSR bitmap
10533 * from the L12 MSR bitmap that is too permissive.
10534 * 2. That L1 or L2s have actually used the MSR. This avoids
10535 * unnecessarily merging of the bitmap if the MSR is unused. This
10536 * works properly because we only update the L01 MSR bitmap lazily.
10537 * So even if L0 should pass L1 these MSRs, the L01 bitmap is only
10538 * updated to reflect this when L1 (or its L2s) actually write to
10541 bool pred_cmd = !msr_write_intercepted_l01(vcpu, MSR_IA32_PRED_CMD);
10542 bool spec_ctrl = !msr_write_intercepted_l01(vcpu, MSR_IA32_SPEC_CTRL);
10544 /* Nothing to do if the MSR bitmap is not in use. */
10545 if (!cpu_has_vmx_msr_bitmap() ||
10546 !nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS))
10549 if (!nested_cpu_has_virt_x2apic_mode(vmcs12) &&
10550 !pred_cmd && !spec_ctrl)
10553 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->msr_bitmap);
10554 if (is_error_page(page))
10557 msr_bitmap_l1 = (unsigned long *)kmap(page);
10558 if (nested_cpu_has_apic_reg_virt(vmcs12)) {
10560 * L0 need not intercept reads for MSRs between 0x800 and 0x8ff, it
10561 * just lets the processor take the value from the virtual-APIC page;
10562 * take those 256 bits directly from the L1 bitmap.
10564 for (msr = 0x800; msr <= 0x8ff; msr += BITS_PER_LONG) {
10565 unsigned word = msr / BITS_PER_LONG;
10566 msr_bitmap_l0[word] = msr_bitmap_l1[word];
10567 msr_bitmap_l0[word + (0x800 / sizeof(long))] = ~0;
10570 for (msr = 0x800; msr <= 0x8ff; msr += BITS_PER_LONG) {
10571 unsigned word = msr / BITS_PER_LONG;
10572 msr_bitmap_l0[word] = ~0;
10573 msr_bitmap_l0[word + (0x800 / sizeof(long))] = ~0;
10577 nested_vmx_disable_intercept_for_msr(
10578 msr_bitmap_l1, msr_bitmap_l0,
10579 X2APIC_MSR(APIC_TASKPRI),
10582 if (nested_cpu_has_vid(vmcs12)) {
10583 nested_vmx_disable_intercept_for_msr(
10584 msr_bitmap_l1, msr_bitmap_l0,
10585 X2APIC_MSR(APIC_EOI),
10587 nested_vmx_disable_intercept_for_msr(
10588 msr_bitmap_l1, msr_bitmap_l0,
10589 X2APIC_MSR(APIC_SELF_IPI),
10594 nested_vmx_disable_intercept_for_msr(
10595 msr_bitmap_l1, msr_bitmap_l0,
10596 MSR_IA32_SPEC_CTRL,
10597 MSR_TYPE_R | MSR_TYPE_W);
10600 nested_vmx_disable_intercept_for_msr(
10601 msr_bitmap_l1, msr_bitmap_l0,
10606 kvm_release_page_clean(page);
10611 static int nested_vmx_check_apicv_controls(struct kvm_vcpu *vcpu,
10612 struct vmcs12 *vmcs12)
10614 if (!nested_cpu_has_virt_x2apic_mode(vmcs12) &&
10615 !nested_cpu_has_apic_reg_virt(vmcs12) &&
10616 !nested_cpu_has_vid(vmcs12) &&
10617 !nested_cpu_has_posted_intr(vmcs12))
10621 * If virtualize x2apic mode is enabled,
10622 * virtualize apic access must be disabled.
10624 if (nested_cpu_has_virt_x2apic_mode(vmcs12) &&
10625 nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES))
10629 * If virtual interrupt delivery is enabled,
10630 * we must exit on external interrupts.
10632 if (nested_cpu_has_vid(vmcs12) &&
10633 !nested_exit_on_intr(vcpu))
10637 * bits 15:8 should be zero in posted_intr_nv,
10638 * the descriptor address has been already checked
10639 * in nested_get_vmcs12_pages.
10641 if (nested_cpu_has_posted_intr(vmcs12) &&
10642 (!nested_cpu_has_vid(vmcs12) ||
10643 !nested_exit_intr_ack_set(vcpu) ||
10644 vmcs12->posted_intr_nv & 0xff00))
10647 /* tpr shadow is needed by all apicv features. */
10648 if (!nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW))
10654 static int nested_vmx_check_msr_switch(struct kvm_vcpu *vcpu,
10655 unsigned long count_field,
10656 unsigned long addr_field)
10661 if (vmcs12_read_any(vcpu, count_field, &count) ||
10662 vmcs12_read_any(vcpu, addr_field, &addr)) {
10668 maxphyaddr = cpuid_maxphyaddr(vcpu);
10669 if (!IS_ALIGNED(addr, 16) || addr >> maxphyaddr ||
10670 (addr + count * sizeof(struct vmx_msr_entry) - 1) >> maxphyaddr) {
10671 pr_debug_ratelimited(
10672 "nVMX: invalid MSR switch (0x%lx, %d, %llu, 0x%08llx)",
10673 addr_field, maxphyaddr, count, addr);
10679 static int nested_vmx_check_msr_switch_controls(struct kvm_vcpu *vcpu,
10680 struct vmcs12 *vmcs12)
10682 if (vmcs12->vm_exit_msr_load_count == 0 &&
10683 vmcs12->vm_exit_msr_store_count == 0 &&
10684 vmcs12->vm_entry_msr_load_count == 0)
10685 return 0; /* Fast path */
10686 if (nested_vmx_check_msr_switch(vcpu, VM_EXIT_MSR_LOAD_COUNT,
10687 VM_EXIT_MSR_LOAD_ADDR) ||
10688 nested_vmx_check_msr_switch(vcpu, VM_EXIT_MSR_STORE_COUNT,
10689 VM_EXIT_MSR_STORE_ADDR) ||
10690 nested_vmx_check_msr_switch(vcpu, VM_ENTRY_MSR_LOAD_COUNT,
10691 VM_ENTRY_MSR_LOAD_ADDR))
10696 static int nested_vmx_check_pml_controls(struct kvm_vcpu *vcpu,
10697 struct vmcs12 *vmcs12)
10699 u64 address = vmcs12->pml_address;
10700 int maxphyaddr = cpuid_maxphyaddr(vcpu);
10702 if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_PML)) {
10703 if (!nested_cpu_has_ept(vmcs12) ||
10704 !IS_ALIGNED(address, 4096) ||
10705 address >> maxphyaddr)
10712 static int nested_vmx_msr_check_common(struct kvm_vcpu *vcpu,
10713 struct vmx_msr_entry *e)
10715 /* x2APIC MSR accesses are not allowed */
10716 if (vcpu->arch.apic_base & X2APIC_ENABLE && e->index >> 8 == 0x8)
10718 if (e->index == MSR_IA32_UCODE_WRITE || /* SDM Table 35-2 */
10719 e->index == MSR_IA32_UCODE_REV)
10721 if (e->reserved != 0)
10726 static int nested_vmx_load_msr_check(struct kvm_vcpu *vcpu,
10727 struct vmx_msr_entry *e)
10729 if (e->index == MSR_FS_BASE ||
10730 e->index == MSR_GS_BASE ||
10731 e->index == MSR_IA32_SMM_MONITOR_CTL || /* SMM is not supported */
10732 nested_vmx_msr_check_common(vcpu, e))
10737 static int nested_vmx_store_msr_check(struct kvm_vcpu *vcpu,
10738 struct vmx_msr_entry *e)
10740 if (e->index == MSR_IA32_SMBASE || /* SMM is not supported */
10741 nested_vmx_msr_check_common(vcpu, e))
10747 * Load guest's/host's msr at nested entry/exit.
10748 * return 0 for success, entry index for failure.
10750 static u32 nested_vmx_load_msr(struct kvm_vcpu *vcpu, u64 gpa, u32 count)
10753 struct vmx_msr_entry e;
10754 struct msr_data msr;
10756 msr.host_initiated = false;
10757 for (i = 0; i < count; i++) {
10758 if (kvm_vcpu_read_guest(vcpu, gpa + i * sizeof(e),
10760 pr_debug_ratelimited(
10761 "%s cannot read MSR entry (%u, 0x%08llx)\n",
10762 __func__, i, gpa + i * sizeof(e));
10765 if (nested_vmx_load_msr_check(vcpu, &e)) {
10766 pr_debug_ratelimited(
10767 "%s check failed (%u, 0x%x, 0x%x)\n",
10768 __func__, i, e.index, e.reserved);
10771 msr.index = e.index;
10772 msr.data = e.value;
10773 if (kvm_set_msr(vcpu, &msr)) {
10774 pr_debug_ratelimited(
10775 "%s cannot write MSR (%u, 0x%x, 0x%llx)\n",
10776 __func__, i, e.index, e.value);
10785 static int nested_vmx_store_msr(struct kvm_vcpu *vcpu, u64 gpa, u32 count)
10788 struct vmx_msr_entry e;
10790 for (i = 0; i < count; i++) {
10791 struct msr_data msr_info;
10792 if (kvm_vcpu_read_guest(vcpu,
10793 gpa + i * sizeof(e),
10794 &e, 2 * sizeof(u32))) {
10795 pr_debug_ratelimited(
10796 "%s cannot read MSR entry (%u, 0x%08llx)\n",
10797 __func__, i, gpa + i * sizeof(e));
10800 if (nested_vmx_store_msr_check(vcpu, &e)) {
10801 pr_debug_ratelimited(
10802 "%s check failed (%u, 0x%x, 0x%x)\n",
10803 __func__, i, e.index, e.reserved);
10806 msr_info.host_initiated = false;
10807 msr_info.index = e.index;
10808 if (kvm_get_msr(vcpu, &msr_info)) {
10809 pr_debug_ratelimited(
10810 "%s cannot read MSR (%u, 0x%x)\n",
10811 __func__, i, e.index);
10814 if (kvm_vcpu_write_guest(vcpu,
10815 gpa + i * sizeof(e) +
10816 offsetof(struct vmx_msr_entry, value),
10817 &msr_info.data, sizeof(msr_info.data))) {
10818 pr_debug_ratelimited(
10819 "%s cannot write MSR (%u, 0x%x, 0x%llx)\n",
10820 __func__, i, e.index, msr_info.data);
10827 static bool nested_cr3_valid(struct kvm_vcpu *vcpu, unsigned long val)
10829 unsigned long invalid_mask;
10831 invalid_mask = (~0ULL) << cpuid_maxphyaddr(vcpu);
10832 return (val & invalid_mask) == 0;
10836 * Load guest's/host's cr3 at nested entry/exit. nested_ept is true if we are
10837 * emulating VM entry into a guest with EPT enabled.
10838 * Returns 0 on success, 1 on failure. Invalid state exit qualification code
10839 * is assigned to entry_failure_code on failure.
10841 static int nested_vmx_load_cr3(struct kvm_vcpu *vcpu, unsigned long cr3, bool nested_ept,
10842 u32 *entry_failure_code)
10844 if (cr3 != kvm_read_cr3(vcpu) || (!nested_ept && pdptrs_changed(vcpu))) {
10845 if (!nested_cr3_valid(vcpu, cr3)) {
10846 *entry_failure_code = ENTRY_FAIL_DEFAULT;
10851 * If PAE paging and EPT are both on, CR3 is not used by the CPU and
10852 * must not be dereferenced.
10854 if (!is_long_mode(vcpu) && is_pae(vcpu) && is_paging(vcpu) &&
10856 if (!load_pdptrs(vcpu, vcpu->arch.walk_mmu, cr3)) {
10857 *entry_failure_code = ENTRY_FAIL_PDPTE;
10862 vcpu->arch.cr3 = cr3;
10863 __set_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail);
10866 kvm_mmu_reset_context(vcpu);
10870 static void prepare_vmcs02_full(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12,
10873 struct vcpu_vmx *vmx = to_vmx(vcpu);
10875 vmcs_write16(GUEST_ES_SELECTOR, vmcs12->guest_es_selector);
10876 vmcs_write16(GUEST_SS_SELECTOR, vmcs12->guest_ss_selector);
10877 vmcs_write16(GUEST_DS_SELECTOR, vmcs12->guest_ds_selector);
10878 vmcs_write16(GUEST_FS_SELECTOR, vmcs12->guest_fs_selector);
10879 vmcs_write16(GUEST_GS_SELECTOR, vmcs12->guest_gs_selector);
10880 vmcs_write16(GUEST_LDTR_SELECTOR, vmcs12->guest_ldtr_selector);
10881 vmcs_write16(GUEST_TR_SELECTOR, vmcs12->guest_tr_selector);
10882 vmcs_write32(GUEST_ES_LIMIT, vmcs12->guest_es_limit);
10883 vmcs_write32(GUEST_SS_LIMIT, vmcs12->guest_ss_limit);
10884 vmcs_write32(GUEST_DS_LIMIT, vmcs12->guest_ds_limit);
10885 vmcs_write32(GUEST_FS_LIMIT, vmcs12->guest_fs_limit);
10886 vmcs_write32(GUEST_GS_LIMIT, vmcs12->guest_gs_limit);
10887 vmcs_write32(GUEST_LDTR_LIMIT, vmcs12->guest_ldtr_limit);
10888 vmcs_write32(GUEST_TR_LIMIT, vmcs12->guest_tr_limit);
10889 vmcs_write32(GUEST_GDTR_LIMIT, vmcs12->guest_gdtr_limit);
10890 vmcs_write32(GUEST_IDTR_LIMIT, vmcs12->guest_idtr_limit);
10891 vmcs_write32(GUEST_ES_AR_BYTES, vmcs12->guest_es_ar_bytes);
10892 vmcs_write32(GUEST_SS_AR_BYTES, vmcs12->guest_ss_ar_bytes);
10893 vmcs_write32(GUEST_DS_AR_BYTES, vmcs12->guest_ds_ar_bytes);
10894 vmcs_write32(GUEST_FS_AR_BYTES, vmcs12->guest_fs_ar_bytes);
10895 vmcs_write32(GUEST_GS_AR_BYTES, vmcs12->guest_gs_ar_bytes);
10896 vmcs_write32(GUEST_LDTR_AR_BYTES, vmcs12->guest_ldtr_ar_bytes);
10897 vmcs_write32(GUEST_TR_AR_BYTES, vmcs12->guest_tr_ar_bytes);
10898 vmcs_writel(GUEST_SS_BASE, vmcs12->guest_ss_base);
10899 vmcs_writel(GUEST_DS_BASE, vmcs12->guest_ds_base);
10900 vmcs_writel(GUEST_FS_BASE, vmcs12->guest_fs_base);
10901 vmcs_writel(GUEST_GS_BASE, vmcs12->guest_gs_base);
10902 vmcs_writel(GUEST_LDTR_BASE, vmcs12->guest_ldtr_base);
10903 vmcs_writel(GUEST_TR_BASE, vmcs12->guest_tr_base);
10904 vmcs_writel(GUEST_GDTR_BASE, vmcs12->guest_gdtr_base);
10905 vmcs_writel(GUEST_IDTR_BASE, vmcs12->guest_idtr_base);
10907 vmcs_write32(GUEST_SYSENTER_CS, vmcs12->guest_sysenter_cs);
10908 vmcs_writel(GUEST_PENDING_DBG_EXCEPTIONS,
10909 vmcs12->guest_pending_dbg_exceptions);
10910 vmcs_writel(GUEST_SYSENTER_ESP, vmcs12->guest_sysenter_esp);
10911 vmcs_writel(GUEST_SYSENTER_EIP, vmcs12->guest_sysenter_eip);
10913 if (nested_cpu_has_xsaves(vmcs12))
10914 vmcs_write64(XSS_EXIT_BITMAP, vmcs12->xss_exit_bitmap);
10915 vmcs_write64(VMCS_LINK_POINTER, -1ull);
10917 if (cpu_has_vmx_posted_intr())
10918 vmcs_write16(POSTED_INTR_NV, POSTED_INTR_NESTED_VECTOR);
10921 * Whether page-faults are trapped is determined by a combination of
10922 * 3 settings: PFEC_MASK, PFEC_MATCH and EXCEPTION_BITMAP.PF.
10923 * If enable_ept, L0 doesn't care about page faults and we should
10924 * set all of these to L1's desires. However, if !enable_ept, L0 does
10925 * care about (at least some) page faults, and because it is not easy
10926 * (if at all possible?) to merge L0 and L1's desires, we simply ask
10927 * to exit on each and every L2 page fault. This is done by setting
10928 * MASK=MATCH=0 and (see below) EB.PF=1.
10929 * Note that below we don't need special code to set EB.PF beyond the
10930 * "or"ing of the EB of vmcs01 and vmcs12, because when enable_ept,
10931 * vmcs01's EB.PF is 0 so the "or" will take vmcs12's value, and when
10932 * !enable_ept, EB.PF is 1, so the "or" will always be 1.
10934 vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK,
10935 enable_ept ? vmcs12->page_fault_error_code_mask : 0);
10936 vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH,
10937 enable_ept ? vmcs12->page_fault_error_code_match : 0);
10939 /* All VMFUNCs are currently emulated through L0 vmexits. */
10940 if (cpu_has_vmx_vmfunc())
10941 vmcs_write64(VM_FUNCTION_CONTROL, 0);
10943 if (cpu_has_vmx_apicv()) {
10944 vmcs_write64(EOI_EXIT_BITMAP0, vmcs12->eoi_exit_bitmap0);
10945 vmcs_write64(EOI_EXIT_BITMAP1, vmcs12->eoi_exit_bitmap1);
10946 vmcs_write64(EOI_EXIT_BITMAP2, vmcs12->eoi_exit_bitmap2);
10947 vmcs_write64(EOI_EXIT_BITMAP3, vmcs12->eoi_exit_bitmap3);
10951 * Set host-state according to L0's settings (vmcs12 is irrelevant here)
10952 * Some constant fields are set here by vmx_set_constant_host_state().
10953 * Other fields are different per CPU, and will be set later when
10954 * vmx_vcpu_load() is called, and when vmx_save_host_state() is called.
10956 vmx_set_constant_host_state(vmx);
10959 * Set the MSR load/store lists to match L0's settings.
10961 vmcs_write32(VM_EXIT_MSR_STORE_COUNT, 0);
10962 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, vmx->msr_autoload.nr);
10963 vmcs_write64(VM_EXIT_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.host));
10964 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, vmx->msr_autoload.nr);
10965 vmcs_write64(VM_ENTRY_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.guest));
10967 set_cr4_guest_host_mask(vmx);
10969 if (vmx_mpx_supported())
10970 vmcs_write64(GUEST_BNDCFGS, vmcs12->guest_bndcfgs);
10973 if (nested_cpu_has_vpid(vmcs12) && vmx->nested.vpid02)
10974 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->nested.vpid02);
10976 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid);
10980 * L1 may access the L2's PDPTR, so save them to construct vmcs12
10983 vmcs_write64(GUEST_PDPTR0, vmcs12->guest_pdptr0);
10984 vmcs_write64(GUEST_PDPTR1, vmcs12->guest_pdptr1);
10985 vmcs_write64(GUEST_PDPTR2, vmcs12->guest_pdptr2);
10986 vmcs_write64(GUEST_PDPTR3, vmcs12->guest_pdptr3);
10989 if (cpu_has_vmx_msr_bitmap())
10990 vmcs_write64(MSR_BITMAP, __pa(vmx->nested.vmcs02.msr_bitmap));
10994 * prepare_vmcs02 is called when the L1 guest hypervisor runs its nested
10995 * L2 guest. L1 has a vmcs for L2 (vmcs12), and this function "merges" it
10996 * with L0's requirements for its guest (a.k.a. vmcs01), so we can run the L2
10997 * guest in a way that will both be appropriate to L1's requests, and our
10998 * needs. In addition to modifying the active vmcs (which is vmcs02), this
10999 * function also has additional necessary side-effects, like setting various
11000 * vcpu->arch fields.
11001 * Returns 0 on success, 1 on failure. Invalid state exit qualification code
11002 * is assigned to entry_failure_code on failure.
11004 static int prepare_vmcs02(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12,
11005 bool from_vmentry, u32 *entry_failure_code)
11007 struct vcpu_vmx *vmx = to_vmx(vcpu);
11008 u32 exec_control, vmcs12_exec_ctrl;
11010 if (vmx->nested.dirty_vmcs12) {
11011 prepare_vmcs02_full(vcpu, vmcs12, from_vmentry);
11012 vmx->nested.dirty_vmcs12 = false;
11016 * First, the fields that are shadowed. This must be kept in sync
11017 * with vmx_shadow_fields.h.
11020 vmcs_write16(GUEST_CS_SELECTOR, vmcs12->guest_cs_selector);
11021 vmcs_write32(GUEST_CS_LIMIT, vmcs12->guest_cs_limit);
11022 vmcs_write32(GUEST_CS_AR_BYTES, vmcs12->guest_cs_ar_bytes);
11023 vmcs_writel(GUEST_ES_BASE, vmcs12->guest_es_base);
11024 vmcs_writel(GUEST_CS_BASE, vmcs12->guest_cs_base);
11027 * Not in vmcs02: GUEST_PML_INDEX, HOST_FS_SELECTOR, HOST_GS_SELECTOR,
11028 * HOST_FS_BASE, HOST_GS_BASE.
11031 if (from_vmentry &&
11032 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS)) {
11033 kvm_set_dr(vcpu, 7, vmcs12->guest_dr7);
11034 vmcs_write64(GUEST_IA32_DEBUGCTL, vmcs12->guest_ia32_debugctl);
11036 kvm_set_dr(vcpu, 7, vcpu->arch.dr7);
11037 vmcs_write64(GUEST_IA32_DEBUGCTL, vmx->nested.vmcs01_debugctl);
11039 if (from_vmentry) {
11040 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
11041 vmcs12->vm_entry_intr_info_field);
11042 vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE,
11043 vmcs12->vm_entry_exception_error_code);
11044 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
11045 vmcs12->vm_entry_instruction_len);
11046 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO,
11047 vmcs12->guest_interruptibility_info);
11048 vmx->loaded_vmcs->nmi_known_unmasked =
11049 !(vmcs12->guest_interruptibility_info & GUEST_INTR_STATE_NMI);
11051 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0);
11053 vmx_set_rflags(vcpu, vmcs12->guest_rflags);
11055 exec_control = vmcs12->pin_based_vm_exec_control;
11057 /* Preemption timer setting is only taken from vmcs01. */
11058 exec_control &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
11059 exec_control |= vmcs_config.pin_based_exec_ctrl;
11060 if (vmx->hv_deadline_tsc == -1)
11061 exec_control &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
11063 /* Posted interrupts setting is only taken from vmcs12. */
11064 if (nested_cpu_has_posted_intr(vmcs12)) {
11065 vmx->nested.posted_intr_nv = vmcs12->posted_intr_nv;
11066 vmx->nested.pi_pending = false;
11068 exec_control &= ~PIN_BASED_POSTED_INTR;
11071 vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, exec_control);
11073 vmx->nested.preemption_timer_expired = false;
11074 if (nested_cpu_has_preemption_timer(vmcs12))
11075 vmx_start_preemption_timer(vcpu);
11077 if (cpu_has_secondary_exec_ctrls()) {
11078 exec_control = vmx->secondary_exec_control;
11080 /* Take the following fields only from vmcs12 */
11081 exec_control &= ~(SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
11082 SECONDARY_EXEC_ENABLE_INVPCID |
11083 SECONDARY_EXEC_RDTSCP |
11084 SECONDARY_EXEC_XSAVES |
11085 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY |
11086 SECONDARY_EXEC_APIC_REGISTER_VIRT |
11087 SECONDARY_EXEC_ENABLE_VMFUNC);
11088 if (nested_cpu_has(vmcs12,
11089 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS)) {
11090 vmcs12_exec_ctrl = vmcs12->secondary_vm_exec_control &
11091 ~SECONDARY_EXEC_ENABLE_PML;
11092 exec_control |= vmcs12_exec_ctrl;
11095 if (exec_control & SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY)
11096 vmcs_write16(GUEST_INTR_STATUS,
11097 vmcs12->guest_intr_status);
11100 * Write an illegal value to APIC_ACCESS_ADDR. Later,
11101 * nested_get_vmcs12_pages will either fix it up or
11102 * remove the VM execution control.
11104 if (exec_control & SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)
11105 vmcs_write64(APIC_ACCESS_ADDR, -1ull);
11107 vmcs_write32(SECONDARY_VM_EXEC_CONTROL, exec_control);
11111 * HOST_RSP is normally set correctly in vmx_vcpu_run() just before
11112 * entry, but only if the current (host) sp changed from the value
11113 * we wrote last (vmx->host_rsp). This cache is no longer relevant
11114 * if we switch vmcs, and rather than hold a separate cache per vmcs,
11115 * here we just force the write to happen on entry.
11119 exec_control = vmx_exec_control(vmx); /* L0's desires */
11120 exec_control &= ~CPU_BASED_VIRTUAL_INTR_PENDING;
11121 exec_control &= ~CPU_BASED_VIRTUAL_NMI_PENDING;
11122 exec_control &= ~CPU_BASED_TPR_SHADOW;
11123 exec_control |= vmcs12->cpu_based_vm_exec_control;
11126 * Write an illegal value to VIRTUAL_APIC_PAGE_ADDR. Later, if
11127 * nested_get_vmcs12_pages can't fix it up, the illegal value
11128 * will result in a VM entry failure.
11130 if (exec_control & CPU_BASED_TPR_SHADOW) {
11131 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, -1ull);
11132 vmcs_write32(TPR_THRESHOLD, vmcs12->tpr_threshold);
11134 #ifdef CONFIG_X86_64
11135 exec_control |= CPU_BASED_CR8_LOAD_EXITING |
11136 CPU_BASED_CR8_STORE_EXITING;
11141 * A vmexit (to either L1 hypervisor or L0 userspace) is always needed
11142 * for I/O port accesses.
11144 exec_control &= ~CPU_BASED_USE_IO_BITMAPS;
11145 exec_control |= CPU_BASED_UNCOND_IO_EXITING;
11147 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, exec_control);
11149 /* EXCEPTION_BITMAP and CR0_GUEST_HOST_MASK should basically be the
11150 * bitwise-or of what L1 wants to trap for L2, and what we want to
11151 * trap. Note that CR0.TS also needs updating - we do this later.
11153 update_exception_bitmap(vcpu);
11154 vcpu->arch.cr0_guest_owned_bits &= ~vmcs12->cr0_guest_host_mask;
11155 vmcs_writel(CR0_GUEST_HOST_MASK, ~vcpu->arch.cr0_guest_owned_bits);
11157 /* L2->L1 exit controls are emulated - the hardware exit is to L0 so
11158 * we should use its exit controls. Note that VM_EXIT_LOAD_IA32_EFER
11159 * bits are further modified by vmx_set_efer() below.
11161 vmcs_write32(VM_EXIT_CONTROLS, vmcs_config.vmexit_ctrl);
11163 /* vmcs12's VM_ENTRY_LOAD_IA32_EFER and VM_ENTRY_IA32E_MODE are
11164 * emulated by vmx_set_efer(), below.
11166 vm_entry_controls_init(vmx,
11167 (vmcs12->vm_entry_controls & ~VM_ENTRY_LOAD_IA32_EFER &
11168 ~VM_ENTRY_IA32E_MODE) |
11169 (vmcs_config.vmentry_ctrl & ~VM_ENTRY_IA32E_MODE));
11171 if (from_vmentry &&
11172 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_PAT)) {
11173 vmcs_write64(GUEST_IA32_PAT, vmcs12->guest_ia32_pat);
11174 vcpu->arch.pat = vmcs12->guest_ia32_pat;
11175 } else if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) {
11176 vmcs_write64(GUEST_IA32_PAT, vmx->vcpu.arch.pat);
11179 if (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING)
11180 vmcs_write64(TSC_OFFSET,
11181 vcpu->arch.tsc_offset + vmcs12->tsc_offset);
11183 vmcs_write64(TSC_OFFSET, vcpu->arch.tsc_offset);
11184 if (kvm_has_tsc_control)
11185 decache_tsc_multiplier(vmx);
11189 * There is no direct mapping between vpid02 and vpid12, the
11190 * vpid02 is per-vCPU for L0 and reused while the value of
11191 * vpid12 is changed w/ one invvpid during nested vmentry.
11192 * The vpid12 is allocated by L1 for L2, so it will not
11193 * influence global bitmap(for vpid01 and vpid02 allocation)
11194 * even if spawn a lot of nested vCPUs.
11196 if (nested_cpu_has_vpid(vmcs12) && vmx->nested.vpid02) {
11197 if (vmcs12->virtual_processor_id != vmx->nested.last_vpid) {
11198 vmx->nested.last_vpid = vmcs12->virtual_processor_id;
11199 __vmx_flush_tlb(vcpu, to_vmx(vcpu)->nested.vpid02, true);
11202 vmx_flush_tlb(vcpu, true);
11208 * Conceptually we want to copy the PML address and index from
11209 * vmcs01 here, and then back to vmcs01 on nested vmexit. But,
11210 * since we always flush the log on each vmexit, this happens
11211 * to be equivalent to simply resetting the fields in vmcs02.
11213 ASSERT(vmx->pml_pg);
11214 vmcs_write64(PML_ADDRESS, page_to_phys(vmx->pml_pg));
11215 vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1);
11218 if (nested_cpu_has_ept(vmcs12)) {
11219 if (nested_ept_init_mmu_context(vcpu)) {
11220 *entry_failure_code = ENTRY_FAIL_DEFAULT;
11223 } else if (nested_cpu_has2(vmcs12,
11224 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) {
11225 vmx_flush_tlb_ept_only(vcpu);
11229 * This sets GUEST_CR0 to vmcs12->guest_cr0, possibly modifying those
11230 * bits which we consider mandatory enabled.
11231 * The CR0_READ_SHADOW is what L2 should have expected to read given
11232 * the specifications by L1; It's not enough to take
11233 * vmcs12->cr0_read_shadow because on our cr0_guest_host_mask we we
11234 * have more bits than L1 expected.
11236 vmx_set_cr0(vcpu, vmcs12->guest_cr0);
11237 vmcs_writel(CR0_READ_SHADOW, nested_read_cr0(vmcs12));
11239 vmx_set_cr4(vcpu, vmcs12->guest_cr4);
11240 vmcs_writel(CR4_READ_SHADOW, nested_read_cr4(vmcs12));
11242 if (from_vmentry &&
11243 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_EFER))
11244 vcpu->arch.efer = vmcs12->guest_ia32_efer;
11245 else if (vmcs12->vm_entry_controls & VM_ENTRY_IA32E_MODE)
11246 vcpu->arch.efer |= (EFER_LMA | EFER_LME);
11248 vcpu->arch.efer &= ~(EFER_LMA | EFER_LME);
11249 /* Note: modifies VM_ENTRY/EXIT_CONTROLS and GUEST/HOST_IA32_EFER */
11250 vmx_set_efer(vcpu, vcpu->arch.efer);
11253 * Guest state is invalid and unrestricted guest is disabled,
11254 * which means L1 attempted VMEntry to L2 with invalid state.
11255 * Fail the VMEntry.
11257 if (vmx->emulation_required) {
11258 *entry_failure_code = ENTRY_FAIL_DEFAULT;
11262 /* Shadow page tables on either EPT or shadow page tables. */
11263 if (nested_vmx_load_cr3(vcpu, vmcs12->guest_cr3, nested_cpu_has_ept(vmcs12),
11264 entry_failure_code))
11268 vcpu->arch.walk_mmu->inject_page_fault = vmx_inject_page_fault_nested;
11270 kvm_register_write(vcpu, VCPU_REGS_RSP, vmcs12->guest_rsp);
11271 kvm_register_write(vcpu, VCPU_REGS_RIP, vmcs12->guest_rip);
11275 static int nested_vmx_check_nmi_controls(struct vmcs12 *vmcs12)
11277 if (!nested_cpu_has_nmi_exiting(vmcs12) &&
11278 nested_cpu_has_virtual_nmis(vmcs12))
11281 if (!nested_cpu_has_virtual_nmis(vmcs12) &&
11282 nested_cpu_has(vmcs12, CPU_BASED_VIRTUAL_NMI_PENDING))
11288 static int check_vmentry_prereqs(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
11290 struct vcpu_vmx *vmx = to_vmx(vcpu);
11292 if (vmcs12->guest_activity_state != GUEST_ACTIVITY_ACTIVE &&
11293 vmcs12->guest_activity_state != GUEST_ACTIVITY_HLT)
11294 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11296 if (nested_vmx_check_io_bitmap_controls(vcpu, vmcs12))
11297 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11299 if (nested_vmx_check_msr_bitmap_controls(vcpu, vmcs12))
11300 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11302 if (nested_vmx_check_tpr_shadow_controls(vcpu, vmcs12))
11303 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11305 if (nested_vmx_check_apicv_controls(vcpu, vmcs12))
11306 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11308 if (nested_vmx_check_msr_switch_controls(vcpu, vmcs12))
11309 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11311 if (nested_vmx_check_pml_controls(vcpu, vmcs12))
11312 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11314 if (!vmx_control_verify(vmcs12->cpu_based_vm_exec_control,
11315 vmx->nested.msrs.procbased_ctls_low,
11316 vmx->nested.msrs.procbased_ctls_high) ||
11317 (nested_cpu_has(vmcs12, CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) &&
11318 !vmx_control_verify(vmcs12->secondary_vm_exec_control,
11319 vmx->nested.msrs.secondary_ctls_low,
11320 vmx->nested.msrs.secondary_ctls_high)) ||
11321 !vmx_control_verify(vmcs12->pin_based_vm_exec_control,
11322 vmx->nested.msrs.pinbased_ctls_low,
11323 vmx->nested.msrs.pinbased_ctls_high) ||
11324 !vmx_control_verify(vmcs12->vm_exit_controls,
11325 vmx->nested.msrs.exit_ctls_low,
11326 vmx->nested.msrs.exit_ctls_high) ||
11327 !vmx_control_verify(vmcs12->vm_entry_controls,
11328 vmx->nested.msrs.entry_ctls_low,
11329 vmx->nested.msrs.entry_ctls_high))
11330 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11332 if (nested_vmx_check_nmi_controls(vmcs12))
11333 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11335 if (nested_cpu_has_vmfunc(vmcs12)) {
11336 if (vmcs12->vm_function_control &
11337 ~vmx->nested.msrs.vmfunc_controls)
11338 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11340 if (nested_cpu_has_eptp_switching(vmcs12)) {
11341 if (!nested_cpu_has_ept(vmcs12) ||
11342 !page_address_valid(vcpu, vmcs12->eptp_list_address))
11343 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11347 if (vmcs12->cr3_target_count > nested_cpu_vmx_misc_cr3_count(vcpu))
11348 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11350 if (!nested_host_cr0_valid(vcpu, vmcs12->host_cr0) ||
11351 !nested_host_cr4_valid(vcpu, vmcs12->host_cr4) ||
11352 !nested_cr3_valid(vcpu, vmcs12->host_cr3))
11353 return VMXERR_ENTRY_INVALID_HOST_STATE_FIELD;
11358 static int check_vmentry_postreqs(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12,
11363 *exit_qual = ENTRY_FAIL_DEFAULT;
11365 if (!nested_guest_cr0_valid(vcpu, vmcs12->guest_cr0) ||
11366 !nested_guest_cr4_valid(vcpu, vmcs12->guest_cr4))
11369 if (!nested_cpu_has2(vmcs12, SECONDARY_EXEC_SHADOW_VMCS) &&
11370 vmcs12->vmcs_link_pointer != -1ull) {
11371 *exit_qual = ENTRY_FAIL_VMCS_LINK_PTR;
11376 * If the load IA32_EFER VM-entry control is 1, the following checks
11377 * are performed on the field for the IA32_EFER MSR:
11378 * - Bits reserved in the IA32_EFER MSR must be 0.
11379 * - Bit 10 (corresponding to IA32_EFER.LMA) must equal the value of
11380 * the IA-32e mode guest VM-exit control. It must also be identical
11381 * to bit 8 (LME) if bit 31 in the CR0 field (corresponding to
11384 if (to_vmx(vcpu)->nested.nested_run_pending &&
11385 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_EFER)) {
11386 ia32e = (vmcs12->vm_entry_controls & VM_ENTRY_IA32E_MODE) != 0;
11387 if (!kvm_valid_efer(vcpu, vmcs12->guest_ia32_efer) ||
11388 ia32e != !!(vmcs12->guest_ia32_efer & EFER_LMA) ||
11389 ((vmcs12->guest_cr0 & X86_CR0_PG) &&
11390 ia32e != !!(vmcs12->guest_ia32_efer & EFER_LME)))
11395 * If the load IA32_EFER VM-exit control is 1, bits reserved in the
11396 * IA32_EFER MSR must be 0 in the field for that register. In addition,
11397 * the values of the LMA and LME bits in the field must each be that of
11398 * the host address-space size VM-exit control.
11400 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_EFER) {
11401 ia32e = (vmcs12->vm_exit_controls &
11402 VM_EXIT_HOST_ADDR_SPACE_SIZE) != 0;
11403 if (!kvm_valid_efer(vcpu, vmcs12->host_ia32_efer) ||
11404 ia32e != !!(vmcs12->host_ia32_efer & EFER_LMA) ||
11405 ia32e != !!(vmcs12->host_ia32_efer & EFER_LME))
11409 if ((vmcs12->vm_entry_controls & VM_ENTRY_LOAD_BNDCFGS) &&
11410 (is_noncanonical_address(vmcs12->guest_bndcfgs & PAGE_MASK, vcpu) ||
11411 (vmcs12->guest_bndcfgs & MSR_IA32_BNDCFGS_RSVD)))
11417 static int enter_vmx_non_root_mode(struct kvm_vcpu *vcpu, bool from_vmentry)
11419 struct vcpu_vmx *vmx = to_vmx(vcpu);
11420 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
11424 enter_guest_mode(vcpu);
11426 if (!(vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS))
11427 vmx->nested.vmcs01_debugctl = vmcs_read64(GUEST_IA32_DEBUGCTL);
11429 vmx_switch_vmcs(vcpu, &vmx->nested.vmcs02);
11430 vmx_segment_cache_clear(vmx);
11432 if (prepare_vmcs02(vcpu, vmcs12, from_vmentry, &exit_qual)) {
11433 leave_guest_mode(vcpu);
11434 vmx_switch_vmcs(vcpu, &vmx->vmcs01);
11435 nested_vmx_entry_failure(vcpu, vmcs12,
11436 EXIT_REASON_INVALID_STATE, exit_qual);
11440 nested_get_vmcs12_pages(vcpu, vmcs12);
11442 msr_entry_idx = nested_vmx_load_msr(vcpu,
11443 vmcs12->vm_entry_msr_load_addr,
11444 vmcs12->vm_entry_msr_load_count);
11445 if (msr_entry_idx) {
11446 leave_guest_mode(vcpu);
11447 vmx_switch_vmcs(vcpu, &vmx->vmcs01);
11448 nested_vmx_entry_failure(vcpu, vmcs12,
11449 EXIT_REASON_MSR_LOAD_FAIL, msr_entry_idx);
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()).
11463 * nested_vmx_run() handles a nested entry, i.e., a VMLAUNCH or VMRESUME on L1
11464 * for running an L2 nested guest.
11466 static int nested_vmx_run(struct kvm_vcpu *vcpu, bool launch)
11468 struct vmcs12 *vmcs12;
11469 struct vcpu_vmx *vmx = to_vmx(vcpu);
11470 u32 interrupt_shadow = vmx_get_interrupt_shadow(vcpu);
11474 if (!nested_vmx_check_permission(vcpu))
11477 if (!nested_vmx_check_vmcs12(vcpu))
11480 vmcs12 = get_vmcs12(vcpu);
11482 if (enable_shadow_vmcs)
11483 copy_shadow_to_vmcs12(vmx);
11486 * The nested entry process starts with enforcing various prerequisites
11487 * on vmcs12 as required by the Intel SDM, and act appropriately when
11488 * they fail: As the SDM explains, some conditions should cause the
11489 * instruction to fail, while others will cause the instruction to seem
11490 * to succeed, but return an EXIT_REASON_INVALID_STATE.
11491 * To speed up the normal (success) code path, we should avoid checking
11492 * for misconfigurations which will anyway be caught by the processor
11493 * when using the merged vmcs02.
11495 if (interrupt_shadow & KVM_X86_SHADOW_INT_MOV_SS) {
11496 nested_vmx_failValid(vcpu,
11497 VMXERR_ENTRY_EVENTS_BLOCKED_BY_MOV_SS);
11501 if (vmcs12->launch_state == launch) {
11502 nested_vmx_failValid(vcpu,
11503 launch ? VMXERR_VMLAUNCH_NONCLEAR_VMCS
11504 : VMXERR_VMRESUME_NONLAUNCHED_VMCS);
11508 ret = check_vmentry_prereqs(vcpu, vmcs12);
11510 nested_vmx_failValid(vcpu, ret);
11515 * After this point, the trap flag no longer triggers a singlestep trap
11516 * on the vm entry instructions; don't call kvm_skip_emulated_instruction.
11517 * This is not 100% correct; for performance reasons, we delegate most
11518 * of the checks on host state to the processor. If those fail,
11519 * the singlestep trap is missed.
11521 skip_emulated_instruction(vcpu);
11523 ret = check_vmentry_postreqs(vcpu, vmcs12, &exit_qual);
11525 nested_vmx_entry_failure(vcpu, vmcs12,
11526 EXIT_REASON_INVALID_STATE, exit_qual);
11531 * We're finally done with prerequisite checking, and can start with
11532 * the nested entry.
11535 ret = enter_vmx_non_root_mode(vcpu, true);
11540 * If we're entering a halted L2 vcpu and the L2 vcpu won't be woken
11541 * by event injection, halt vcpu.
11543 if ((vmcs12->guest_activity_state == GUEST_ACTIVITY_HLT) &&
11544 !(vmcs12->vm_entry_intr_info_field & INTR_INFO_VALID_MASK))
11545 return kvm_vcpu_halt(vcpu);
11547 vmx->nested.nested_run_pending = 1;
11552 return kvm_skip_emulated_instruction(vcpu);
11556 * On a nested exit from L2 to L1, vmcs12.guest_cr0 might not be up-to-date
11557 * because L2 may have changed some cr0 bits directly (CRO_GUEST_HOST_MASK).
11558 * This function returns the new value we should put in vmcs12.guest_cr0.
11559 * It's not enough to just return the vmcs02 GUEST_CR0. Rather,
11560 * 1. Bits that neither L0 nor L1 trapped, were set directly by L2 and are now
11561 * available in vmcs02 GUEST_CR0. (Note: It's enough to check that L0
11562 * didn't trap the bit, because if L1 did, so would L0).
11563 * 2. Bits that L1 asked to trap (and therefore L0 also did) could not have
11564 * been modified by L2, and L1 knows it. So just leave the old value of
11565 * the bit from vmcs12.guest_cr0. Note that the bit from vmcs02 GUEST_CR0
11566 * isn't relevant, because if L0 traps this bit it can set it to anything.
11567 * 3. Bits that L1 didn't trap, but L0 did. L1 believes the guest could have
11568 * changed these bits, and therefore they need to be updated, but L0
11569 * didn't necessarily allow them to be changed in GUEST_CR0 - and rather
11570 * put them in vmcs02 CR0_READ_SHADOW. So take these bits from there.
11572 static inline unsigned long
11573 vmcs12_guest_cr0(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
11576 /*1*/ (vmcs_readl(GUEST_CR0) & vcpu->arch.cr0_guest_owned_bits) |
11577 /*2*/ (vmcs12->guest_cr0 & vmcs12->cr0_guest_host_mask) |
11578 /*3*/ (vmcs_readl(CR0_READ_SHADOW) & ~(vmcs12->cr0_guest_host_mask |
11579 vcpu->arch.cr0_guest_owned_bits));
11582 static inline unsigned long
11583 vmcs12_guest_cr4(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
11586 /*1*/ (vmcs_readl(GUEST_CR4) & vcpu->arch.cr4_guest_owned_bits) |
11587 /*2*/ (vmcs12->guest_cr4 & vmcs12->cr4_guest_host_mask) |
11588 /*3*/ (vmcs_readl(CR4_READ_SHADOW) & ~(vmcs12->cr4_guest_host_mask |
11589 vcpu->arch.cr4_guest_owned_bits));
11592 static void vmcs12_save_pending_event(struct kvm_vcpu *vcpu,
11593 struct vmcs12 *vmcs12)
11598 if (vcpu->arch.exception.injected) {
11599 nr = vcpu->arch.exception.nr;
11600 idt_vectoring = nr | VECTORING_INFO_VALID_MASK;
11602 if (kvm_exception_is_soft(nr)) {
11603 vmcs12->vm_exit_instruction_len =
11604 vcpu->arch.event_exit_inst_len;
11605 idt_vectoring |= INTR_TYPE_SOFT_EXCEPTION;
11607 idt_vectoring |= INTR_TYPE_HARD_EXCEPTION;
11609 if (vcpu->arch.exception.has_error_code) {
11610 idt_vectoring |= VECTORING_INFO_DELIVER_CODE_MASK;
11611 vmcs12->idt_vectoring_error_code =
11612 vcpu->arch.exception.error_code;
11615 vmcs12->idt_vectoring_info_field = idt_vectoring;
11616 } else if (vcpu->arch.nmi_injected) {
11617 vmcs12->idt_vectoring_info_field =
11618 INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK | NMI_VECTOR;
11619 } else if (vcpu->arch.interrupt.injected) {
11620 nr = vcpu->arch.interrupt.nr;
11621 idt_vectoring = nr | VECTORING_INFO_VALID_MASK;
11623 if (vcpu->arch.interrupt.soft) {
11624 idt_vectoring |= INTR_TYPE_SOFT_INTR;
11625 vmcs12->vm_entry_instruction_len =
11626 vcpu->arch.event_exit_inst_len;
11628 idt_vectoring |= INTR_TYPE_EXT_INTR;
11630 vmcs12->idt_vectoring_info_field = idt_vectoring;
11634 static int vmx_check_nested_events(struct kvm_vcpu *vcpu, bool external_intr)
11636 struct vcpu_vmx *vmx = to_vmx(vcpu);
11637 unsigned long exit_qual;
11638 bool block_nested_events =
11639 vmx->nested.nested_run_pending || kvm_event_needs_reinjection(vcpu);
11641 if (vcpu->arch.exception.pending &&
11642 nested_vmx_check_exception(vcpu, &exit_qual)) {
11643 if (block_nested_events)
11645 nested_vmx_inject_exception_vmexit(vcpu, exit_qual);
11649 if (nested_cpu_has_preemption_timer(get_vmcs12(vcpu)) &&
11650 vmx->nested.preemption_timer_expired) {
11651 if (block_nested_events)
11653 nested_vmx_vmexit(vcpu, EXIT_REASON_PREEMPTION_TIMER, 0, 0);
11657 if (vcpu->arch.nmi_pending && nested_exit_on_nmi(vcpu)) {
11658 if (block_nested_events)
11660 nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI,
11661 NMI_VECTOR | INTR_TYPE_NMI_INTR |
11662 INTR_INFO_VALID_MASK, 0);
11664 * The NMI-triggered VM exit counts as injection:
11665 * clear this one and block further NMIs.
11667 vcpu->arch.nmi_pending = 0;
11668 vmx_set_nmi_mask(vcpu, true);
11672 if ((kvm_cpu_has_interrupt(vcpu) || external_intr) &&
11673 nested_exit_on_intr(vcpu)) {
11674 if (block_nested_events)
11676 nested_vmx_vmexit(vcpu, EXIT_REASON_EXTERNAL_INTERRUPT, 0, 0);
11680 vmx_complete_nested_posted_interrupt(vcpu);
11684 static u32 vmx_get_preemption_timer_value(struct kvm_vcpu *vcpu)
11686 ktime_t remaining =
11687 hrtimer_get_remaining(&to_vmx(vcpu)->nested.preemption_timer);
11690 if (ktime_to_ns(remaining) <= 0)
11693 value = ktime_to_ns(remaining) * vcpu->arch.virtual_tsc_khz;
11694 do_div(value, 1000000);
11695 return value >> VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE;
11699 * Update the guest state fields of vmcs12 to reflect changes that
11700 * occurred while L2 was running. (The "IA-32e mode guest" bit of the
11701 * VM-entry controls is also updated, since this is really a guest
11704 static void sync_vmcs12(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
11706 vmcs12->guest_cr0 = vmcs12_guest_cr0(vcpu, vmcs12);
11707 vmcs12->guest_cr4 = vmcs12_guest_cr4(vcpu, vmcs12);
11709 vmcs12->guest_rsp = kvm_register_read(vcpu, VCPU_REGS_RSP);
11710 vmcs12->guest_rip = kvm_register_read(vcpu, VCPU_REGS_RIP);
11711 vmcs12->guest_rflags = vmcs_readl(GUEST_RFLAGS);
11713 vmcs12->guest_es_selector = vmcs_read16(GUEST_ES_SELECTOR);
11714 vmcs12->guest_cs_selector = vmcs_read16(GUEST_CS_SELECTOR);
11715 vmcs12->guest_ss_selector = vmcs_read16(GUEST_SS_SELECTOR);
11716 vmcs12->guest_ds_selector = vmcs_read16(GUEST_DS_SELECTOR);
11717 vmcs12->guest_fs_selector = vmcs_read16(GUEST_FS_SELECTOR);
11718 vmcs12->guest_gs_selector = vmcs_read16(GUEST_GS_SELECTOR);
11719 vmcs12->guest_ldtr_selector = vmcs_read16(GUEST_LDTR_SELECTOR);
11720 vmcs12->guest_tr_selector = vmcs_read16(GUEST_TR_SELECTOR);
11721 vmcs12->guest_es_limit = vmcs_read32(GUEST_ES_LIMIT);
11722 vmcs12->guest_cs_limit = vmcs_read32(GUEST_CS_LIMIT);
11723 vmcs12->guest_ss_limit = vmcs_read32(GUEST_SS_LIMIT);
11724 vmcs12->guest_ds_limit = vmcs_read32(GUEST_DS_LIMIT);
11725 vmcs12->guest_fs_limit = vmcs_read32(GUEST_FS_LIMIT);
11726 vmcs12->guest_gs_limit = vmcs_read32(GUEST_GS_LIMIT);
11727 vmcs12->guest_ldtr_limit = vmcs_read32(GUEST_LDTR_LIMIT);
11728 vmcs12->guest_tr_limit = vmcs_read32(GUEST_TR_LIMIT);
11729 vmcs12->guest_gdtr_limit = vmcs_read32(GUEST_GDTR_LIMIT);
11730 vmcs12->guest_idtr_limit = vmcs_read32(GUEST_IDTR_LIMIT);
11731 vmcs12->guest_es_ar_bytes = vmcs_read32(GUEST_ES_AR_BYTES);
11732 vmcs12->guest_cs_ar_bytes = vmcs_read32(GUEST_CS_AR_BYTES);
11733 vmcs12->guest_ss_ar_bytes = vmcs_read32(GUEST_SS_AR_BYTES);
11734 vmcs12->guest_ds_ar_bytes = vmcs_read32(GUEST_DS_AR_BYTES);
11735 vmcs12->guest_fs_ar_bytes = vmcs_read32(GUEST_FS_AR_BYTES);
11736 vmcs12->guest_gs_ar_bytes = vmcs_read32(GUEST_GS_AR_BYTES);
11737 vmcs12->guest_ldtr_ar_bytes = vmcs_read32(GUEST_LDTR_AR_BYTES);
11738 vmcs12->guest_tr_ar_bytes = vmcs_read32(GUEST_TR_AR_BYTES);
11739 vmcs12->guest_es_base = vmcs_readl(GUEST_ES_BASE);
11740 vmcs12->guest_cs_base = vmcs_readl(GUEST_CS_BASE);
11741 vmcs12->guest_ss_base = vmcs_readl(GUEST_SS_BASE);
11742 vmcs12->guest_ds_base = vmcs_readl(GUEST_DS_BASE);
11743 vmcs12->guest_fs_base = vmcs_readl(GUEST_FS_BASE);
11744 vmcs12->guest_gs_base = vmcs_readl(GUEST_GS_BASE);
11745 vmcs12->guest_ldtr_base = vmcs_readl(GUEST_LDTR_BASE);
11746 vmcs12->guest_tr_base = vmcs_readl(GUEST_TR_BASE);
11747 vmcs12->guest_gdtr_base = vmcs_readl(GUEST_GDTR_BASE);
11748 vmcs12->guest_idtr_base = vmcs_readl(GUEST_IDTR_BASE);
11750 vmcs12->guest_interruptibility_info =
11751 vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
11752 vmcs12->guest_pending_dbg_exceptions =
11753 vmcs_readl(GUEST_PENDING_DBG_EXCEPTIONS);
11754 if (vcpu->arch.mp_state == KVM_MP_STATE_HALTED)
11755 vmcs12->guest_activity_state = GUEST_ACTIVITY_HLT;
11757 vmcs12->guest_activity_state = GUEST_ACTIVITY_ACTIVE;
11759 if (nested_cpu_has_preemption_timer(vmcs12)) {
11760 if (vmcs12->vm_exit_controls &
11761 VM_EXIT_SAVE_VMX_PREEMPTION_TIMER)
11762 vmcs12->vmx_preemption_timer_value =
11763 vmx_get_preemption_timer_value(vcpu);
11764 hrtimer_cancel(&to_vmx(vcpu)->nested.preemption_timer);
11768 * In some cases (usually, nested EPT), L2 is allowed to change its
11769 * own CR3 without exiting. If it has changed it, we must keep it.
11770 * Of course, if L0 is using shadow page tables, GUEST_CR3 was defined
11771 * by L0, not L1 or L2, so we mustn't unconditionally copy it to vmcs12.
11773 * Additionally, restore L2's PDPTR to vmcs12.
11776 vmcs12->guest_cr3 = vmcs_readl(GUEST_CR3);
11777 vmcs12->guest_pdptr0 = vmcs_read64(GUEST_PDPTR0);
11778 vmcs12->guest_pdptr1 = vmcs_read64(GUEST_PDPTR1);
11779 vmcs12->guest_pdptr2 = vmcs_read64(GUEST_PDPTR2);
11780 vmcs12->guest_pdptr3 = vmcs_read64(GUEST_PDPTR3);
11783 vmcs12->guest_linear_address = vmcs_readl(GUEST_LINEAR_ADDRESS);
11785 if (nested_cpu_has_vid(vmcs12))
11786 vmcs12->guest_intr_status = vmcs_read16(GUEST_INTR_STATUS);
11788 vmcs12->vm_entry_controls =
11789 (vmcs12->vm_entry_controls & ~VM_ENTRY_IA32E_MODE) |
11790 (vm_entry_controls_get(to_vmx(vcpu)) & VM_ENTRY_IA32E_MODE);
11792 if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_DEBUG_CONTROLS) {
11793 kvm_get_dr(vcpu, 7, (unsigned long *)&vmcs12->guest_dr7);
11794 vmcs12->guest_ia32_debugctl = vmcs_read64(GUEST_IA32_DEBUGCTL);
11797 /* TODO: These cannot have changed unless we have MSR bitmaps and
11798 * the relevant bit asks not to trap the change */
11799 if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_IA32_PAT)
11800 vmcs12->guest_ia32_pat = vmcs_read64(GUEST_IA32_PAT);
11801 if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_IA32_EFER)
11802 vmcs12->guest_ia32_efer = vcpu->arch.efer;
11803 vmcs12->guest_sysenter_cs = vmcs_read32(GUEST_SYSENTER_CS);
11804 vmcs12->guest_sysenter_esp = vmcs_readl(GUEST_SYSENTER_ESP);
11805 vmcs12->guest_sysenter_eip = vmcs_readl(GUEST_SYSENTER_EIP);
11806 if (kvm_mpx_supported())
11807 vmcs12->guest_bndcfgs = vmcs_read64(GUEST_BNDCFGS);
11811 * prepare_vmcs12 is part of what we need to do when the nested L2 guest exits
11812 * and we want to prepare to run its L1 parent. L1 keeps a vmcs for L2 (vmcs12),
11813 * and this function updates it to reflect the changes to the guest state while
11814 * L2 was running (and perhaps made some exits which were handled directly by L0
11815 * without going back to L1), and to reflect the exit reason.
11816 * Note that we do not have to copy here all VMCS fields, just those that
11817 * could have changed by the L2 guest or the exit - i.e., the guest-state and
11818 * exit-information fields only. Other fields are modified by L1 with VMWRITE,
11819 * which already writes to vmcs12 directly.
11821 static void prepare_vmcs12(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12,
11822 u32 exit_reason, u32 exit_intr_info,
11823 unsigned long exit_qualification)
11825 /* update guest state fields: */
11826 sync_vmcs12(vcpu, vmcs12);
11828 /* update exit information fields: */
11830 vmcs12->vm_exit_reason = exit_reason;
11831 vmcs12->exit_qualification = exit_qualification;
11832 vmcs12->vm_exit_intr_info = exit_intr_info;
11834 vmcs12->idt_vectoring_info_field = 0;
11835 vmcs12->vm_exit_instruction_len = vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
11836 vmcs12->vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
11838 if (!(vmcs12->vm_exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY)) {
11839 vmcs12->launch_state = 1;
11841 /* vm_entry_intr_info_field is cleared on exit. Emulate this
11842 * instead of reading the real value. */
11843 vmcs12->vm_entry_intr_info_field &= ~INTR_INFO_VALID_MASK;
11846 * Transfer the event that L0 or L1 may wanted to inject into
11847 * L2 to IDT_VECTORING_INFO_FIELD.
11849 vmcs12_save_pending_event(vcpu, vmcs12);
11853 * Drop what we picked up for L2 via vmx_complete_interrupts. It is
11854 * preserved above and would only end up incorrectly in L1.
11856 vcpu->arch.nmi_injected = false;
11857 kvm_clear_exception_queue(vcpu);
11858 kvm_clear_interrupt_queue(vcpu);
11861 static void load_vmcs12_mmu_host_state(struct kvm_vcpu *vcpu,
11862 struct vmcs12 *vmcs12)
11864 u32 entry_failure_code;
11866 nested_ept_uninit_mmu_context(vcpu);
11869 * Only PDPTE load can fail as the value of cr3 was checked on entry and
11870 * couldn't have changed.
11872 if (nested_vmx_load_cr3(vcpu, vmcs12->host_cr3, false, &entry_failure_code))
11873 nested_vmx_abort(vcpu, VMX_ABORT_LOAD_HOST_PDPTE_FAIL);
11876 vcpu->arch.walk_mmu->inject_page_fault = kvm_inject_page_fault;
11880 * A part of what we need to when the nested L2 guest exits and we want to
11881 * run its L1 parent, is to reset L1's guest state to the host state specified
11883 * This function is to be called not only on normal nested exit, but also on
11884 * a nested entry failure, as explained in Intel's spec, 3B.23.7 ("VM-Entry
11885 * Failures During or After Loading Guest State").
11886 * This function should be called when the active VMCS is L1's (vmcs01).
11888 static void load_vmcs12_host_state(struct kvm_vcpu *vcpu,
11889 struct vmcs12 *vmcs12)
11891 struct kvm_segment seg;
11893 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_EFER)
11894 vcpu->arch.efer = vmcs12->host_ia32_efer;
11895 else if (vmcs12->vm_exit_controls & VM_EXIT_HOST_ADDR_SPACE_SIZE)
11896 vcpu->arch.efer |= (EFER_LMA | EFER_LME);
11898 vcpu->arch.efer &= ~(EFER_LMA | EFER_LME);
11899 vmx_set_efer(vcpu, vcpu->arch.efer);
11901 kvm_register_write(vcpu, VCPU_REGS_RSP, vmcs12->host_rsp);
11902 kvm_register_write(vcpu, VCPU_REGS_RIP, vmcs12->host_rip);
11903 vmx_set_rflags(vcpu, X86_EFLAGS_FIXED);
11905 * Note that calling vmx_set_cr0 is important, even if cr0 hasn't
11906 * actually changed, because vmx_set_cr0 refers to efer set above.
11908 * CR0_GUEST_HOST_MASK is already set in the original vmcs01
11909 * (KVM doesn't change it);
11911 vcpu->arch.cr0_guest_owned_bits = X86_CR0_TS;
11912 vmx_set_cr0(vcpu, vmcs12->host_cr0);
11914 /* Same as above - no reason to call set_cr4_guest_host_mask(). */
11915 vcpu->arch.cr4_guest_owned_bits = ~vmcs_readl(CR4_GUEST_HOST_MASK);
11916 vmx_set_cr4(vcpu, vmcs12->host_cr4);
11918 load_vmcs12_mmu_host_state(vcpu, vmcs12);
11922 * Trivially support vpid by letting L2s share their parent
11923 * L1's vpid. TODO: move to a more elaborate solution, giving
11924 * each L2 its own vpid and exposing the vpid feature to L1.
11926 vmx_flush_tlb(vcpu, true);
11929 vmcs_write32(GUEST_SYSENTER_CS, vmcs12->host_ia32_sysenter_cs);
11930 vmcs_writel(GUEST_SYSENTER_ESP, vmcs12->host_ia32_sysenter_esp);
11931 vmcs_writel(GUEST_SYSENTER_EIP, vmcs12->host_ia32_sysenter_eip);
11932 vmcs_writel(GUEST_IDTR_BASE, vmcs12->host_idtr_base);
11933 vmcs_writel(GUEST_GDTR_BASE, vmcs12->host_gdtr_base);
11934 vmcs_write32(GUEST_IDTR_LIMIT, 0xFFFF);
11935 vmcs_write32(GUEST_GDTR_LIMIT, 0xFFFF);
11937 /* If not VM_EXIT_CLEAR_BNDCFGS, the L2 value propagates to L1. */
11938 if (vmcs12->vm_exit_controls & VM_EXIT_CLEAR_BNDCFGS)
11939 vmcs_write64(GUEST_BNDCFGS, 0);
11941 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PAT) {
11942 vmcs_write64(GUEST_IA32_PAT, vmcs12->host_ia32_pat);
11943 vcpu->arch.pat = vmcs12->host_ia32_pat;
11945 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL)
11946 vmcs_write64(GUEST_IA32_PERF_GLOBAL_CTRL,
11947 vmcs12->host_ia32_perf_global_ctrl);
11949 /* Set L1 segment info according to Intel SDM
11950 27.5.2 Loading Host Segment and Descriptor-Table Registers */
11951 seg = (struct kvm_segment) {
11953 .limit = 0xFFFFFFFF,
11954 .selector = vmcs12->host_cs_selector,
11960 if (vmcs12->vm_exit_controls & VM_EXIT_HOST_ADDR_SPACE_SIZE)
11964 vmx_set_segment(vcpu, &seg, VCPU_SREG_CS);
11965 seg = (struct kvm_segment) {
11967 .limit = 0xFFFFFFFF,
11974 seg.selector = vmcs12->host_ds_selector;
11975 vmx_set_segment(vcpu, &seg, VCPU_SREG_DS);
11976 seg.selector = vmcs12->host_es_selector;
11977 vmx_set_segment(vcpu, &seg, VCPU_SREG_ES);
11978 seg.selector = vmcs12->host_ss_selector;
11979 vmx_set_segment(vcpu, &seg, VCPU_SREG_SS);
11980 seg.selector = vmcs12->host_fs_selector;
11981 seg.base = vmcs12->host_fs_base;
11982 vmx_set_segment(vcpu, &seg, VCPU_SREG_FS);
11983 seg.selector = vmcs12->host_gs_selector;
11984 seg.base = vmcs12->host_gs_base;
11985 vmx_set_segment(vcpu, &seg, VCPU_SREG_GS);
11986 seg = (struct kvm_segment) {
11987 .base = vmcs12->host_tr_base,
11989 .selector = vmcs12->host_tr_selector,
11993 vmx_set_segment(vcpu, &seg, VCPU_SREG_TR);
11995 kvm_set_dr(vcpu, 7, 0x400);
11996 vmcs_write64(GUEST_IA32_DEBUGCTL, 0);
11998 if (cpu_has_vmx_msr_bitmap())
11999 vmx_update_msr_bitmap(vcpu);
12001 if (nested_vmx_load_msr(vcpu, vmcs12->vm_exit_msr_load_addr,
12002 vmcs12->vm_exit_msr_load_count))
12003 nested_vmx_abort(vcpu, VMX_ABORT_LOAD_HOST_MSR_FAIL);
12007 * Emulate an exit from nested guest (L2) to L1, i.e., prepare to run L1
12008 * and modify vmcs12 to make it see what it would expect to see there if
12009 * L2 was its real guest. Must only be called when in L2 (is_guest_mode())
12011 static void nested_vmx_vmexit(struct kvm_vcpu *vcpu, u32 exit_reason,
12012 u32 exit_intr_info,
12013 unsigned long exit_qualification)
12015 struct vcpu_vmx *vmx = to_vmx(vcpu);
12016 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
12018 /* trying to cancel vmlaunch/vmresume is a bug */
12019 WARN_ON_ONCE(vmx->nested.nested_run_pending);
12022 * The only expected VM-instruction error is "VM entry with
12023 * invalid control field(s)." Anything else indicates a
12026 WARN_ON_ONCE(vmx->fail && (vmcs_read32(VM_INSTRUCTION_ERROR) !=
12027 VMXERR_ENTRY_INVALID_CONTROL_FIELD));
12029 leave_guest_mode(vcpu);
12031 if (likely(!vmx->fail)) {
12032 if (exit_reason == -1)
12033 sync_vmcs12(vcpu, vmcs12);
12035 prepare_vmcs12(vcpu, vmcs12, exit_reason, exit_intr_info,
12036 exit_qualification);
12038 if (nested_vmx_store_msr(vcpu, vmcs12->vm_exit_msr_store_addr,
12039 vmcs12->vm_exit_msr_store_count))
12040 nested_vmx_abort(vcpu, VMX_ABORT_SAVE_GUEST_MSR_FAIL);
12043 vmx_switch_vmcs(vcpu, &vmx->vmcs01);
12044 vm_entry_controls_reset_shadow(vmx);
12045 vm_exit_controls_reset_shadow(vmx);
12046 vmx_segment_cache_clear(vmx);
12048 /* Update any VMCS fields that might have changed while L2 ran */
12049 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, vmx->msr_autoload.nr);
12050 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, vmx->msr_autoload.nr);
12051 vmcs_write64(TSC_OFFSET, vcpu->arch.tsc_offset);
12052 if (vmx->hv_deadline_tsc == -1)
12053 vmcs_clear_bits(PIN_BASED_VM_EXEC_CONTROL,
12054 PIN_BASED_VMX_PREEMPTION_TIMER);
12056 vmcs_set_bits(PIN_BASED_VM_EXEC_CONTROL,
12057 PIN_BASED_VMX_PREEMPTION_TIMER);
12058 if (kvm_has_tsc_control)
12059 decache_tsc_multiplier(vmx);
12061 if (vmx->nested.change_vmcs01_virtual_x2apic_mode) {
12062 vmx->nested.change_vmcs01_virtual_x2apic_mode = false;
12063 vmx_set_virtual_x2apic_mode(vcpu,
12064 vcpu->arch.apic_base & X2APIC_ENABLE);
12065 } else if (!nested_cpu_has_ept(vmcs12) &&
12066 nested_cpu_has2(vmcs12,
12067 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) {
12068 vmx_flush_tlb_ept_only(vcpu);
12071 /* This is needed for same reason as it was needed in prepare_vmcs02 */
12074 /* Unpin physical memory we referred to in vmcs02 */
12075 if (vmx->nested.apic_access_page) {
12076 kvm_release_page_dirty(vmx->nested.apic_access_page);
12077 vmx->nested.apic_access_page = NULL;
12079 if (vmx->nested.virtual_apic_page) {
12080 kvm_release_page_dirty(vmx->nested.virtual_apic_page);
12081 vmx->nested.virtual_apic_page = NULL;
12083 if (vmx->nested.pi_desc_page) {
12084 kunmap(vmx->nested.pi_desc_page);
12085 kvm_release_page_dirty(vmx->nested.pi_desc_page);
12086 vmx->nested.pi_desc_page = NULL;
12087 vmx->nested.pi_desc = NULL;
12091 * We are now running in L2, mmu_notifier will force to reload the
12092 * page's hpa for L2 vmcs. Need to reload it for L1 before entering L1.
12094 kvm_make_request(KVM_REQ_APIC_PAGE_RELOAD, vcpu);
12096 if (enable_shadow_vmcs && exit_reason != -1)
12097 vmx->nested.sync_shadow_vmcs = true;
12099 /* in case we halted in L2 */
12100 vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
12102 if (likely(!vmx->fail)) {
12104 * TODO: SDM says that with acknowledge interrupt on
12105 * exit, bit 31 of the VM-exit interrupt information
12106 * (valid interrupt) is always set to 1 on
12107 * EXIT_REASON_EXTERNAL_INTERRUPT, so we shouldn't
12108 * need kvm_cpu_has_interrupt(). See the commit
12109 * message for details.
12111 if (nested_exit_intr_ack_set(vcpu) &&
12112 exit_reason == EXIT_REASON_EXTERNAL_INTERRUPT &&
12113 kvm_cpu_has_interrupt(vcpu)) {
12114 int irq = kvm_cpu_get_interrupt(vcpu);
12116 vmcs12->vm_exit_intr_info = irq |
12117 INTR_INFO_VALID_MASK | INTR_TYPE_EXT_INTR;
12120 if (exit_reason != -1)
12121 trace_kvm_nested_vmexit_inject(vmcs12->vm_exit_reason,
12122 vmcs12->exit_qualification,
12123 vmcs12->idt_vectoring_info_field,
12124 vmcs12->vm_exit_intr_info,
12125 vmcs12->vm_exit_intr_error_code,
12128 load_vmcs12_host_state(vcpu, vmcs12);
12134 * After an early L2 VM-entry failure, we're now back
12135 * in L1 which thinks it just finished a VMLAUNCH or
12136 * VMRESUME instruction, so we need to set the failure
12137 * flag and the VM-instruction error field of the VMCS
12140 nested_vmx_failValid(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD);
12142 load_vmcs12_mmu_host_state(vcpu, vmcs12);
12145 * The emulated instruction was already skipped in
12146 * nested_vmx_run, but the updated RIP was never
12147 * written back to the vmcs01.
12149 skip_emulated_instruction(vcpu);
12154 * Forcibly leave nested mode in order to be able to reset the VCPU later on.
12156 static void vmx_leave_nested(struct kvm_vcpu *vcpu)
12158 if (is_guest_mode(vcpu)) {
12159 to_vmx(vcpu)->nested.nested_run_pending = 0;
12160 nested_vmx_vmexit(vcpu, -1, 0, 0);
12162 free_nested(to_vmx(vcpu));
12166 * L1's failure to enter L2 is a subset of a normal exit, as explained in
12167 * 23.7 "VM-entry failures during or after loading guest state" (this also
12168 * lists the acceptable exit-reason and exit-qualification parameters).
12169 * It should only be called before L2 actually succeeded to run, and when
12170 * vmcs01 is current (it doesn't leave_guest_mode() or switch vmcss).
12172 static void nested_vmx_entry_failure(struct kvm_vcpu *vcpu,
12173 struct vmcs12 *vmcs12,
12174 u32 reason, unsigned long qualification)
12176 load_vmcs12_host_state(vcpu, vmcs12);
12177 vmcs12->vm_exit_reason = reason | VMX_EXIT_REASONS_FAILED_VMENTRY;
12178 vmcs12->exit_qualification = qualification;
12179 nested_vmx_succeed(vcpu);
12180 if (enable_shadow_vmcs)
12181 to_vmx(vcpu)->nested.sync_shadow_vmcs = true;
12184 static int vmx_check_intercept(struct kvm_vcpu *vcpu,
12185 struct x86_instruction_info *info,
12186 enum x86_intercept_stage stage)
12188 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
12189 struct x86_emulate_ctxt *ctxt = &vcpu->arch.emulate_ctxt;
12192 * RDPID causes #UD if disabled through secondary execution controls.
12193 * Because it is marked as EmulateOnUD, we need to intercept it here.
12195 if (info->intercept == x86_intercept_rdtscp &&
12196 !nested_cpu_has2(vmcs12, SECONDARY_EXEC_RDTSCP)) {
12197 ctxt->exception.vector = UD_VECTOR;
12198 ctxt->exception.error_code_valid = false;
12199 return X86EMUL_PROPAGATE_FAULT;
12202 /* TODO: check more intercepts... */
12203 return X86EMUL_CONTINUE;
12206 #ifdef CONFIG_X86_64
12207 /* (a << shift) / divisor, return 1 if overflow otherwise 0 */
12208 static inline int u64_shl_div_u64(u64 a, unsigned int shift,
12209 u64 divisor, u64 *result)
12211 u64 low = a << shift, high = a >> (64 - shift);
12213 /* To avoid the overflow on divq */
12214 if (high >= divisor)
12217 /* Low hold the result, high hold rem which is discarded */
12218 asm("divq %2\n\t" : "=a" (low), "=d" (high) :
12219 "rm" (divisor), "0" (low), "1" (high));
12225 static int vmx_set_hv_timer(struct kvm_vcpu *vcpu, u64 guest_deadline_tsc)
12227 struct vcpu_vmx *vmx = to_vmx(vcpu);
12228 u64 tscl = rdtsc();
12229 u64 guest_tscl = kvm_read_l1_tsc(vcpu, tscl);
12230 u64 delta_tsc = max(guest_deadline_tsc, guest_tscl) - guest_tscl;
12232 /* Convert to host delta tsc if tsc scaling is enabled */
12233 if (vcpu->arch.tsc_scaling_ratio != kvm_default_tsc_scaling_ratio &&
12234 u64_shl_div_u64(delta_tsc,
12235 kvm_tsc_scaling_ratio_frac_bits,
12236 vcpu->arch.tsc_scaling_ratio,
12241 * If the delta tsc can't fit in the 32 bit after the multi shift,
12242 * we can't use the preemption timer.
12243 * It's possible that it fits on later vmentries, but checking
12244 * on every vmentry is costly so we just use an hrtimer.
12246 if (delta_tsc >> (cpu_preemption_timer_multi + 32))
12249 vmx->hv_deadline_tsc = tscl + delta_tsc;
12250 vmcs_set_bits(PIN_BASED_VM_EXEC_CONTROL,
12251 PIN_BASED_VMX_PREEMPTION_TIMER);
12253 return delta_tsc == 0;
12256 static void vmx_cancel_hv_timer(struct kvm_vcpu *vcpu)
12258 struct vcpu_vmx *vmx = to_vmx(vcpu);
12259 vmx->hv_deadline_tsc = -1;
12260 vmcs_clear_bits(PIN_BASED_VM_EXEC_CONTROL,
12261 PIN_BASED_VMX_PREEMPTION_TIMER);
12265 static void vmx_sched_in(struct kvm_vcpu *vcpu, int cpu)
12267 if (!kvm_pause_in_guest(vcpu->kvm))
12268 shrink_ple_window(vcpu);
12271 static void vmx_slot_enable_log_dirty(struct kvm *kvm,
12272 struct kvm_memory_slot *slot)
12274 kvm_mmu_slot_leaf_clear_dirty(kvm, slot);
12275 kvm_mmu_slot_largepage_remove_write_access(kvm, slot);
12278 static void vmx_slot_disable_log_dirty(struct kvm *kvm,
12279 struct kvm_memory_slot *slot)
12281 kvm_mmu_slot_set_dirty(kvm, slot);
12284 static void vmx_flush_log_dirty(struct kvm *kvm)
12286 kvm_flush_pml_buffers(kvm);
12289 static int vmx_write_pml_buffer(struct kvm_vcpu *vcpu)
12291 struct vmcs12 *vmcs12;
12292 struct vcpu_vmx *vmx = to_vmx(vcpu);
12294 struct page *page = NULL;
12297 if (is_guest_mode(vcpu)) {
12298 WARN_ON_ONCE(vmx->nested.pml_full);
12301 * Check if PML is enabled for the nested guest.
12302 * Whether eptp bit 6 is set is already checked
12303 * as part of A/D emulation.
12305 vmcs12 = get_vmcs12(vcpu);
12306 if (!nested_cpu_has_pml(vmcs12))
12309 if (vmcs12->guest_pml_index >= PML_ENTITY_NUM) {
12310 vmx->nested.pml_full = true;
12314 gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS) & ~0xFFFull;
12316 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->pml_address);
12317 if (is_error_page(page))
12320 pml_address = kmap(page);
12321 pml_address[vmcs12->guest_pml_index--] = gpa;
12323 kvm_release_page_clean(page);
12329 static void vmx_enable_log_dirty_pt_masked(struct kvm *kvm,
12330 struct kvm_memory_slot *memslot,
12331 gfn_t offset, unsigned long mask)
12333 kvm_mmu_clear_dirty_pt_masked(kvm, memslot, offset, mask);
12336 static void __pi_post_block(struct kvm_vcpu *vcpu)
12338 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
12339 struct pi_desc old, new;
12343 old.control = new.control = pi_desc->control;
12344 WARN(old.nv != POSTED_INTR_WAKEUP_VECTOR,
12345 "Wakeup handler not enabled while the VCPU is blocked\n");
12347 dest = cpu_physical_id(vcpu->cpu);
12349 if (x2apic_enabled())
12352 new.ndst = (dest << 8) & 0xFF00;
12354 /* set 'NV' to 'notification vector' */
12355 new.nv = POSTED_INTR_VECTOR;
12356 } while (cmpxchg64(&pi_desc->control, old.control,
12357 new.control) != old.control);
12359 if (!WARN_ON_ONCE(vcpu->pre_pcpu == -1)) {
12360 spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
12361 list_del(&vcpu->blocked_vcpu_list);
12362 spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
12363 vcpu->pre_pcpu = -1;
12368 * This routine does the following things for vCPU which is going
12369 * to be blocked if VT-d PI is enabled.
12370 * - Store the vCPU to the wakeup list, so when interrupts happen
12371 * we can find the right vCPU to wake up.
12372 * - Change the Posted-interrupt descriptor as below:
12373 * 'NDST' <-- vcpu->pre_pcpu
12374 * 'NV' <-- POSTED_INTR_WAKEUP_VECTOR
12375 * - If 'ON' is set during this process, which means at least one
12376 * interrupt is posted for this vCPU, we cannot block it, in
12377 * this case, return 1, otherwise, return 0.
12380 static int pi_pre_block(struct kvm_vcpu *vcpu)
12383 struct pi_desc old, new;
12384 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
12386 if (!kvm_arch_has_assigned_device(vcpu->kvm) ||
12387 !irq_remapping_cap(IRQ_POSTING_CAP) ||
12388 !kvm_vcpu_apicv_active(vcpu))
12391 WARN_ON(irqs_disabled());
12392 local_irq_disable();
12393 if (!WARN_ON_ONCE(vcpu->pre_pcpu != -1)) {
12394 vcpu->pre_pcpu = vcpu->cpu;
12395 spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
12396 list_add_tail(&vcpu->blocked_vcpu_list,
12397 &per_cpu(blocked_vcpu_on_cpu,
12399 spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
12403 old.control = new.control = pi_desc->control;
12405 WARN((pi_desc->sn == 1),
12406 "Warning: SN field of posted-interrupts "
12407 "is set before blocking\n");
12410 * Since vCPU can be preempted during this process,
12411 * vcpu->cpu could be different with pre_pcpu, we
12412 * need to set pre_pcpu as the destination of wakeup
12413 * notification event, then we can find the right vCPU
12414 * to wakeup in wakeup handler if interrupts happen
12415 * when the vCPU is in blocked state.
12417 dest = cpu_physical_id(vcpu->pre_pcpu);
12419 if (x2apic_enabled())
12422 new.ndst = (dest << 8) & 0xFF00;
12424 /* set 'NV' to 'wakeup vector' */
12425 new.nv = POSTED_INTR_WAKEUP_VECTOR;
12426 } while (cmpxchg64(&pi_desc->control, old.control,
12427 new.control) != old.control);
12429 /* We should not block the vCPU if an interrupt is posted for it. */
12430 if (pi_test_on(pi_desc) == 1)
12431 __pi_post_block(vcpu);
12433 local_irq_enable();
12434 return (vcpu->pre_pcpu == -1);
12437 static int vmx_pre_block(struct kvm_vcpu *vcpu)
12439 if (pi_pre_block(vcpu))
12442 if (kvm_lapic_hv_timer_in_use(vcpu))
12443 kvm_lapic_switch_to_sw_timer(vcpu);
12448 static void pi_post_block(struct kvm_vcpu *vcpu)
12450 if (vcpu->pre_pcpu == -1)
12453 WARN_ON(irqs_disabled());
12454 local_irq_disable();
12455 __pi_post_block(vcpu);
12456 local_irq_enable();
12459 static void vmx_post_block(struct kvm_vcpu *vcpu)
12461 if (kvm_x86_ops->set_hv_timer)
12462 kvm_lapic_switch_to_hv_timer(vcpu);
12464 pi_post_block(vcpu);
12468 * vmx_update_pi_irte - set IRTE for Posted-Interrupts
12471 * @host_irq: host irq of the interrupt
12472 * @guest_irq: gsi of the interrupt
12473 * @set: set or unset PI
12474 * returns 0 on success, < 0 on failure
12476 static int vmx_update_pi_irte(struct kvm *kvm, unsigned int host_irq,
12477 uint32_t guest_irq, bool set)
12479 struct kvm_kernel_irq_routing_entry *e;
12480 struct kvm_irq_routing_table *irq_rt;
12481 struct kvm_lapic_irq irq;
12482 struct kvm_vcpu *vcpu;
12483 struct vcpu_data vcpu_info;
12486 if (!kvm_arch_has_assigned_device(kvm) ||
12487 !irq_remapping_cap(IRQ_POSTING_CAP) ||
12488 !kvm_vcpu_apicv_active(kvm->vcpus[0]))
12491 idx = srcu_read_lock(&kvm->irq_srcu);
12492 irq_rt = srcu_dereference(kvm->irq_routing, &kvm->irq_srcu);
12493 if (guest_irq >= irq_rt->nr_rt_entries ||
12494 hlist_empty(&irq_rt->map[guest_irq])) {
12495 pr_warn_once("no route for guest_irq %u/%u (broken user space?)\n",
12496 guest_irq, irq_rt->nr_rt_entries);
12500 hlist_for_each_entry(e, &irq_rt->map[guest_irq], link) {
12501 if (e->type != KVM_IRQ_ROUTING_MSI)
12504 * VT-d PI cannot support posting multicast/broadcast
12505 * interrupts to a vCPU, we still use interrupt remapping
12506 * for these kind of interrupts.
12508 * For lowest-priority interrupts, we only support
12509 * those with single CPU as the destination, e.g. user
12510 * configures the interrupts via /proc/irq or uses
12511 * irqbalance to make the interrupts single-CPU.
12513 * We will support full lowest-priority interrupt later.
12516 kvm_set_msi_irq(kvm, e, &irq);
12517 if (!kvm_intr_is_single_vcpu(kvm, &irq, &vcpu)) {
12519 * Make sure the IRTE is in remapped mode if
12520 * we don't handle it in posted mode.
12522 ret = irq_set_vcpu_affinity(host_irq, NULL);
12525 "failed to back to remapped mode, irq: %u\n",
12533 vcpu_info.pi_desc_addr = __pa(vcpu_to_pi_desc(vcpu));
12534 vcpu_info.vector = irq.vector;
12536 trace_kvm_pi_irte_update(vcpu->vcpu_id, host_irq, e->gsi,
12537 vcpu_info.vector, vcpu_info.pi_desc_addr, set);
12540 ret = irq_set_vcpu_affinity(host_irq, &vcpu_info);
12542 ret = irq_set_vcpu_affinity(host_irq, NULL);
12545 printk(KERN_INFO "%s: failed to update PI IRTE\n",
12553 srcu_read_unlock(&kvm->irq_srcu, idx);
12557 static void vmx_setup_mce(struct kvm_vcpu *vcpu)
12559 if (vcpu->arch.mcg_cap & MCG_LMCE_P)
12560 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits |=
12561 FEATURE_CONTROL_LMCE;
12563 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits &=
12564 ~FEATURE_CONTROL_LMCE;
12567 static int vmx_smi_allowed(struct kvm_vcpu *vcpu)
12569 /* we need a nested vmexit to enter SMM, postpone if run is pending */
12570 if (to_vmx(vcpu)->nested.nested_run_pending)
12575 static int vmx_pre_enter_smm(struct kvm_vcpu *vcpu, char *smstate)
12577 struct vcpu_vmx *vmx = to_vmx(vcpu);
12579 vmx->nested.smm.guest_mode = is_guest_mode(vcpu);
12580 if (vmx->nested.smm.guest_mode)
12581 nested_vmx_vmexit(vcpu, -1, 0, 0);
12583 vmx->nested.smm.vmxon = vmx->nested.vmxon;
12584 vmx->nested.vmxon = false;
12585 vmx_clear_hlt(vcpu);
12589 static int vmx_pre_leave_smm(struct kvm_vcpu *vcpu, u64 smbase)
12591 struct vcpu_vmx *vmx = to_vmx(vcpu);
12594 if (vmx->nested.smm.vmxon) {
12595 vmx->nested.vmxon = true;
12596 vmx->nested.smm.vmxon = false;
12599 if (vmx->nested.smm.guest_mode) {
12600 vcpu->arch.hflags &= ~HF_SMM_MASK;
12601 ret = enter_vmx_non_root_mode(vcpu, false);
12602 vcpu->arch.hflags |= HF_SMM_MASK;
12606 vmx->nested.smm.guest_mode = false;
12611 static int enable_smi_window(struct kvm_vcpu *vcpu)
12616 static struct kvm_x86_ops vmx_x86_ops __ro_after_init = {
12617 .cpu_has_kvm_support = cpu_has_kvm_support,
12618 .disabled_by_bios = vmx_disabled_by_bios,
12619 .hardware_setup = hardware_setup,
12620 .hardware_unsetup = hardware_unsetup,
12621 .check_processor_compatibility = vmx_check_processor_compat,
12622 .hardware_enable = hardware_enable,
12623 .hardware_disable = hardware_disable,
12624 .cpu_has_accelerated_tpr = report_flexpriority,
12625 .cpu_has_high_real_mode_segbase = vmx_has_high_real_mode_segbase,
12627 .vm_init = vmx_vm_init,
12628 .vm_alloc = vmx_vm_alloc,
12629 .vm_free = vmx_vm_free,
12631 .vcpu_create = vmx_create_vcpu,
12632 .vcpu_free = vmx_free_vcpu,
12633 .vcpu_reset = vmx_vcpu_reset,
12635 .prepare_guest_switch = vmx_save_host_state,
12636 .vcpu_load = vmx_vcpu_load,
12637 .vcpu_put = vmx_vcpu_put,
12639 .update_bp_intercept = update_exception_bitmap,
12640 .get_msr_feature = vmx_get_msr_feature,
12641 .get_msr = vmx_get_msr,
12642 .set_msr = vmx_set_msr,
12643 .get_segment_base = vmx_get_segment_base,
12644 .get_segment = vmx_get_segment,
12645 .set_segment = vmx_set_segment,
12646 .get_cpl = vmx_get_cpl,
12647 .get_cs_db_l_bits = vmx_get_cs_db_l_bits,
12648 .decache_cr0_guest_bits = vmx_decache_cr0_guest_bits,
12649 .decache_cr3 = vmx_decache_cr3,
12650 .decache_cr4_guest_bits = vmx_decache_cr4_guest_bits,
12651 .set_cr0 = vmx_set_cr0,
12652 .set_cr3 = vmx_set_cr3,
12653 .set_cr4 = vmx_set_cr4,
12654 .set_efer = vmx_set_efer,
12655 .get_idt = vmx_get_idt,
12656 .set_idt = vmx_set_idt,
12657 .get_gdt = vmx_get_gdt,
12658 .set_gdt = vmx_set_gdt,
12659 .get_dr6 = vmx_get_dr6,
12660 .set_dr6 = vmx_set_dr6,
12661 .set_dr7 = vmx_set_dr7,
12662 .sync_dirty_debug_regs = vmx_sync_dirty_debug_regs,
12663 .cache_reg = vmx_cache_reg,
12664 .get_rflags = vmx_get_rflags,
12665 .set_rflags = vmx_set_rflags,
12667 .tlb_flush = vmx_flush_tlb,
12669 .run = vmx_vcpu_run,
12670 .handle_exit = vmx_handle_exit,
12671 .skip_emulated_instruction = skip_emulated_instruction,
12672 .set_interrupt_shadow = vmx_set_interrupt_shadow,
12673 .get_interrupt_shadow = vmx_get_interrupt_shadow,
12674 .patch_hypercall = vmx_patch_hypercall,
12675 .set_irq = vmx_inject_irq,
12676 .set_nmi = vmx_inject_nmi,
12677 .queue_exception = vmx_queue_exception,
12678 .cancel_injection = vmx_cancel_injection,
12679 .interrupt_allowed = vmx_interrupt_allowed,
12680 .nmi_allowed = vmx_nmi_allowed,
12681 .get_nmi_mask = vmx_get_nmi_mask,
12682 .set_nmi_mask = vmx_set_nmi_mask,
12683 .enable_nmi_window = enable_nmi_window,
12684 .enable_irq_window = enable_irq_window,
12685 .update_cr8_intercept = update_cr8_intercept,
12686 .set_virtual_x2apic_mode = vmx_set_virtual_x2apic_mode,
12687 .set_apic_access_page_addr = vmx_set_apic_access_page_addr,
12688 .get_enable_apicv = vmx_get_enable_apicv,
12689 .refresh_apicv_exec_ctrl = vmx_refresh_apicv_exec_ctrl,
12690 .load_eoi_exitmap = vmx_load_eoi_exitmap,
12691 .apicv_post_state_restore = vmx_apicv_post_state_restore,
12692 .hwapic_irr_update = vmx_hwapic_irr_update,
12693 .hwapic_isr_update = vmx_hwapic_isr_update,
12694 .sync_pir_to_irr = vmx_sync_pir_to_irr,
12695 .deliver_posted_interrupt = vmx_deliver_posted_interrupt,
12697 .set_tss_addr = vmx_set_tss_addr,
12698 .set_identity_map_addr = vmx_set_identity_map_addr,
12699 .get_tdp_level = get_ept_level,
12700 .get_mt_mask = vmx_get_mt_mask,
12702 .get_exit_info = vmx_get_exit_info,
12704 .get_lpage_level = vmx_get_lpage_level,
12706 .cpuid_update = vmx_cpuid_update,
12708 .rdtscp_supported = vmx_rdtscp_supported,
12709 .invpcid_supported = vmx_invpcid_supported,
12711 .set_supported_cpuid = vmx_set_supported_cpuid,
12713 .has_wbinvd_exit = cpu_has_vmx_wbinvd_exit,
12715 .write_tsc_offset = vmx_write_tsc_offset,
12717 .set_tdp_cr3 = vmx_set_cr3,
12719 .check_intercept = vmx_check_intercept,
12720 .handle_external_intr = vmx_handle_external_intr,
12721 .mpx_supported = vmx_mpx_supported,
12722 .xsaves_supported = vmx_xsaves_supported,
12723 .umip_emulated = vmx_umip_emulated,
12725 .check_nested_events = vmx_check_nested_events,
12727 .sched_in = vmx_sched_in,
12729 .slot_enable_log_dirty = vmx_slot_enable_log_dirty,
12730 .slot_disable_log_dirty = vmx_slot_disable_log_dirty,
12731 .flush_log_dirty = vmx_flush_log_dirty,
12732 .enable_log_dirty_pt_masked = vmx_enable_log_dirty_pt_masked,
12733 .write_log_dirty = vmx_write_pml_buffer,
12735 .pre_block = vmx_pre_block,
12736 .post_block = vmx_post_block,
12738 .pmu_ops = &intel_pmu_ops,
12740 .update_pi_irte = vmx_update_pi_irte,
12742 #ifdef CONFIG_X86_64
12743 .set_hv_timer = vmx_set_hv_timer,
12744 .cancel_hv_timer = vmx_cancel_hv_timer,
12747 .setup_mce = vmx_setup_mce,
12749 .smi_allowed = vmx_smi_allowed,
12750 .pre_enter_smm = vmx_pre_enter_smm,
12751 .pre_leave_smm = vmx_pre_leave_smm,
12752 .enable_smi_window = enable_smi_window,
12755 static int __init vmx_init(void)
12759 #if IS_ENABLED(CONFIG_HYPERV)
12761 * Enlightened VMCS usage should be recommended and the host needs
12762 * to support eVMCS v1 or above. We can also disable eVMCS support
12763 * with module parameter.
12765 if (enlightened_vmcs &&
12766 ms_hyperv.hints & HV_X64_ENLIGHTENED_VMCS_RECOMMENDED &&
12767 (ms_hyperv.nested_features & HV_X64_ENLIGHTENED_VMCS_VERSION) >=
12768 KVM_EVMCS_VERSION) {
12771 /* Check that we have assist pages on all online CPUs */
12772 for_each_online_cpu(cpu) {
12773 if (!hv_get_vp_assist_page(cpu)) {
12774 enlightened_vmcs = false;
12779 if (enlightened_vmcs) {
12780 pr_info("KVM: vmx: using Hyper-V Enlightened VMCS\n");
12781 static_branch_enable(&enable_evmcs);
12784 enlightened_vmcs = false;
12788 r = kvm_init(&vmx_x86_ops, sizeof(struct vcpu_vmx),
12789 __alignof__(struct vcpu_vmx), THIS_MODULE);
12793 #ifdef CONFIG_KEXEC_CORE
12794 rcu_assign_pointer(crash_vmclear_loaded_vmcss,
12795 crash_vmclear_local_loaded_vmcss);
12801 static void __exit vmx_exit(void)
12803 #ifdef CONFIG_KEXEC_CORE
12804 RCU_INIT_POINTER(crash_vmclear_loaded_vmcss, NULL);
12810 #if IS_ENABLED(CONFIG_HYPERV)
12811 if (static_branch_unlikely(&enable_evmcs)) {
12813 struct hv_vp_assist_page *vp_ap;
12815 * Reset everything to support using non-enlightened VMCS
12816 * access later (e.g. when we reload the module with
12817 * enlightened_vmcs=0)
12819 for_each_online_cpu(cpu) {
12820 vp_ap = hv_get_vp_assist_page(cpu);
12825 vp_ap->current_nested_vmcs = 0;
12826 vp_ap->enlighten_vmentry = 0;
12829 static_branch_disable(&enable_evmcs);
12834 module_init(vmx_init)
12835 module_exit(vmx_exit)