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/microcode.h>
55 #include <asm/spec-ctrl.h>
60 #define __ex(x) __kvm_handle_fault_on_reboot(x)
61 #define __ex_clear(x, reg) \
62 ____kvm_handle_fault_on_reboot(x, "xor " reg " , " reg)
64 MODULE_AUTHOR("Qumranet");
65 MODULE_LICENSE("GPL");
67 static const struct x86_cpu_id vmx_cpu_id[] = {
68 X86_FEATURE_MATCH(X86_FEATURE_VMX),
71 MODULE_DEVICE_TABLE(x86cpu, vmx_cpu_id);
73 static bool __read_mostly nosmt;
74 module_param(nosmt, bool, S_IRUGO);
76 static bool __read_mostly enable_vpid = 1;
77 module_param_named(vpid, enable_vpid, bool, 0444);
79 static bool __read_mostly flexpriority_enabled = 1;
80 module_param_named(flexpriority, flexpriority_enabled, bool, S_IRUGO);
82 static bool __read_mostly enable_ept = 1;
83 module_param_named(ept, enable_ept, bool, S_IRUGO);
85 static bool __read_mostly enable_unrestricted_guest = 1;
86 module_param_named(unrestricted_guest,
87 enable_unrestricted_guest, bool, S_IRUGO);
89 static bool __read_mostly enable_ept_ad_bits = 1;
90 module_param_named(eptad, enable_ept_ad_bits, bool, S_IRUGO);
92 static bool __read_mostly emulate_invalid_guest_state = true;
93 module_param(emulate_invalid_guest_state, bool, S_IRUGO);
95 static bool __read_mostly fasteoi = 1;
96 module_param(fasteoi, bool, S_IRUGO);
98 static bool __read_mostly enable_apicv = 1;
99 module_param(enable_apicv, bool, S_IRUGO);
101 static bool __read_mostly enable_shadow_vmcs = 1;
102 module_param_named(enable_shadow_vmcs, enable_shadow_vmcs, bool, S_IRUGO);
104 * If nested=1, nested virtualization is supported, i.e., guests may use
105 * VMX and be a hypervisor for its own guests. If nested=0, guests may not
106 * use VMX instructions.
108 static bool __read_mostly nested = 0;
109 module_param(nested, bool, S_IRUGO);
111 static u64 __read_mostly host_xss;
113 static bool __read_mostly enable_pml = 1;
114 module_param_named(pml, enable_pml, bool, S_IRUGO);
118 #define MSR_TYPE_RW 3
120 #define MSR_BITMAP_MODE_X2APIC 1
121 #define MSR_BITMAP_MODE_X2APIC_APICV 2
122 #define MSR_BITMAP_MODE_LM 4
124 #define KVM_VMX_TSC_MULTIPLIER_MAX 0xffffffffffffffffULL
126 /* Guest_tsc -> host_tsc conversion requires 64-bit division. */
127 static int __read_mostly cpu_preemption_timer_multi;
128 static bool __read_mostly enable_preemption_timer = 1;
130 module_param_named(preemption_timer, enable_preemption_timer, bool, S_IRUGO);
133 #define KVM_GUEST_CR0_MASK (X86_CR0_NW | X86_CR0_CD)
134 #define KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST (X86_CR0_WP | X86_CR0_NE)
135 #define KVM_VM_CR0_ALWAYS_ON \
136 (KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST | X86_CR0_PG | X86_CR0_PE)
137 #define KVM_CR4_GUEST_OWNED_BITS \
138 (X86_CR4_PVI | X86_CR4_DE | X86_CR4_PCE | X86_CR4_OSFXSR \
139 | X86_CR4_OSXMMEXCPT | X86_CR4_LA57 | X86_CR4_TSD)
141 #define KVM_PMODE_VM_CR4_ALWAYS_ON (X86_CR4_PAE | X86_CR4_VMXE)
142 #define KVM_RMODE_VM_CR4_ALWAYS_ON (X86_CR4_VME | X86_CR4_PAE | X86_CR4_VMXE)
144 #define RMODE_GUEST_OWNED_EFLAGS_BITS (~(X86_EFLAGS_IOPL | X86_EFLAGS_VM))
146 #define VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE 5
149 * Hyper-V requires all of these, so mark them as supported even though
150 * they are just treated the same as all-context.
152 #define VMX_VPID_EXTENT_SUPPORTED_MASK \
153 (VMX_VPID_EXTENT_INDIVIDUAL_ADDR_BIT | \
154 VMX_VPID_EXTENT_SINGLE_CONTEXT_BIT | \
155 VMX_VPID_EXTENT_GLOBAL_CONTEXT_BIT | \
156 VMX_VPID_EXTENT_SINGLE_NON_GLOBAL_BIT)
159 * These 2 parameters are used to config the controls for Pause-Loop Exiting:
160 * ple_gap: upper bound on the amount of time between two successive
161 * executions of PAUSE in a loop. Also indicate if ple enabled.
162 * According to test, this time is usually smaller than 128 cycles.
163 * ple_window: upper bound on the amount of time a guest is allowed to execute
164 * in a PAUSE loop. Tests indicate that most spinlocks are held for
165 * less than 2^12 cycles
166 * Time is measured based on a counter that runs at the same rate as the TSC,
167 * refer SDM volume 3b section 21.6.13 & 22.1.3.
169 #define KVM_VMX_DEFAULT_PLE_GAP 128
170 #define KVM_VMX_DEFAULT_PLE_WINDOW 4096
171 #define KVM_VMX_DEFAULT_PLE_WINDOW_GROW 2
172 #define KVM_VMX_DEFAULT_PLE_WINDOW_SHRINK 0
173 #define KVM_VMX_DEFAULT_PLE_WINDOW_MAX \
174 INT_MAX / KVM_VMX_DEFAULT_PLE_WINDOW_GROW
176 static int ple_gap = KVM_VMX_DEFAULT_PLE_GAP;
177 module_param(ple_gap, int, S_IRUGO);
179 static int ple_window = KVM_VMX_DEFAULT_PLE_WINDOW;
180 module_param(ple_window, int, S_IRUGO);
182 /* Default doubles per-vcpu window every exit. */
183 static int ple_window_grow = KVM_VMX_DEFAULT_PLE_WINDOW_GROW;
184 module_param(ple_window_grow, int, S_IRUGO);
186 /* Default resets per-vcpu window every exit to ple_window. */
187 static int ple_window_shrink = KVM_VMX_DEFAULT_PLE_WINDOW_SHRINK;
188 module_param(ple_window_shrink, int, S_IRUGO);
190 /* Default is to compute the maximum so we can never overflow. */
191 static int ple_window_actual_max = KVM_VMX_DEFAULT_PLE_WINDOW_MAX;
192 static int ple_window_max = KVM_VMX_DEFAULT_PLE_WINDOW_MAX;
193 module_param(ple_window_max, int, S_IRUGO);
195 extern const ulong vmx_return;
197 static DEFINE_STATIC_KEY_FALSE(vmx_l1d_should_flush);
198 static DEFINE_STATIC_KEY_FALSE(vmx_l1d_flush_always);
200 /* Storage for pre module init parameter parsing */
201 static enum vmx_l1d_flush_state __read_mostly vmentry_l1d_flush_param = VMENTER_L1D_FLUSH_AUTO;
203 static const struct {
205 enum vmx_l1d_flush_state cmd;
206 } vmentry_l1d_param[] = {
207 {"auto", VMENTER_L1D_FLUSH_AUTO},
208 {"never", VMENTER_L1D_FLUSH_NEVER},
209 {"cond", VMENTER_L1D_FLUSH_COND},
210 {"always", VMENTER_L1D_FLUSH_ALWAYS},
213 #define L1D_CACHE_ORDER 4
214 static void *vmx_l1d_flush_pages;
216 static int vmx_setup_l1d_flush(enum vmx_l1d_flush_state l1tf)
220 /* If set to 'auto' select 'cond' */
221 if (l1tf == VMENTER_L1D_FLUSH_AUTO)
222 l1tf = VMENTER_L1D_FLUSH_COND;
225 l1tf_vmx_mitigation = VMENTER_L1D_FLUSH_EPT_DISABLED;
229 if (l1tf != VMENTER_L1D_FLUSH_NEVER && !vmx_l1d_flush_pages &&
230 !boot_cpu_has(X86_FEATURE_FLUSH_L1D)) {
231 page = alloc_pages(GFP_KERNEL, L1D_CACHE_ORDER);
234 vmx_l1d_flush_pages = page_address(page);
237 l1tf_vmx_mitigation = l1tf;
239 if (l1tf == VMENTER_L1D_FLUSH_NEVER)
242 static_branch_enable(&vmx_l1d_should_flush);
243 if (l1tf == VMENTER_L1D_FLUSH_ALWAYS)
244 static_branch_enable(&vmx_l1d_flush_always);
248 static int vmentry_l1d_flush_parse(const char *s)
253 for (i = 0; i < ARRAY_SIZE(vmentry_l1d_param); i++) {
254 if (!strcmp(s, vmentry_l1d_param[i].option))
255 return vmentry_l1d_param[i].cmd;
261 static int vmentry_l1d_flush_set(const char *s, const struct kernel_param *kp)
265 if (!boot_cpu_has(X86_BUG_L1TF))
268 l1tf = vmentry_l1d_flush_parse(s);
273 * Has vmx_init() run already? If not then this is the pre init
274 * parameter parsing. In that case just store the value and let
275 * vmx_init() do the proper setup after enable_ept has been
278 if (l1tf_vmx_mitigation == VMENTER_L1D_FLUSH_AUTO) {
279 vmentry_l1d_flush_param = l1tf;
283 return vmx_setup_l1d_flush(l1tf);
286 static int vmentry_l1d_flush_get(char *s, const struct kernel_param *kp)
288 return sprintf(s, "%s\n", vmentry_l1d_param[l1tf_vmx_mitigation].option);
291 static const struct kernel_param_ops vmentry_l1d_flush_ops = {
292 .set = vmentry_l1d_flush_set,
293 .get = vmentry_l1d_flush_get,
295 module_param_cb(vmentry_l1d_flush, &vmentry_l1d_flush_ops, NULL, S_IRUGO);
297 #define NR_AUTOLOAD_MSRS 8
306 * Track a VMCS that may be loaded on a certain CPU. If it is (cpu!=-1), also
307 * remember whether it was VMLAUNCHed, and maintain a linked list of all VMCSs
308 * loaded on this CPU (so we can clear them if the CPU goes down).
312 struct vmcs *shadow_vmcs;
315 bool nmi_known_unmasked;
316 unsigned long vmcs_host_cr3; /* May not match real cr3 */
317 unsigned long vmcs_host_cr4; /* May not match real cr4 */
318 /* Support for vnmi-less CPUs */
319 int soft_vnmi_blocked;
321 s64 vnmi_blocked_time;
322 unsigned long *msr_bitmap;
323 struct list_head loaded_vmcss_on_cpu_link;
326 struct shared_msr_entry {
333 * struct vmcs12 describes the state that our guest hypervisor (L1) keeps for a
334 * single nested guest (L2), hence the name vmcs12. Any VMX implementation has
335 * a VMCS structure, and vmcs12 is our emulated VMX's VMCS. This structure is
336 * stored in guest memory specified by VMPTRLD, but is opaque to the guest,
337 * which must access it using VMREAD/VMWRITE/VMCLEAR instructions.
338 * More than one of these structures may exist, if L1 runs multiple L2 guests.
339 * nested_vmx_run() will use the data here to build the vmcs02: a VMCS for the
340 * underlying hardware which will be used to run L2.
341 * This structure is packed to ensure that its layout is identical across
342 * machines (necessary for live migration).
343 * If there are changes in this struct, VMCS12_REVISION must be changed.
345 typedef u64 natural_width;
346 struct __packed vmcs12 {
347 /* According to the Intel spec, a VMCS region must start with the
348 * following two fields. Then follow implementation-specific data.
353 u32 launch_state; /* set to 0 by VMCLEAR, to 1 by VMLAUNCH */
354 u32 padding[7]; /* room for future expansion */
359 u64 vm_exit_msr_store_addr;
360 u64 vm_exit_msr_load_addr;
361 u64 vm_entry_msr_load_addr;
363 u64 virtual_apic_page_addr;
364 u64 apic_access_addr;
365 u64 posted_intr_desc_addr;
366 u64 vm_function_control;
368 u64 eoi_exit_bitmap0;
369 u64 eoi_exit_bitmap1;
370 u64 eoi_exit_bitmap2;
371 u64 eoi_exit_bitmap3;
372 u64 eptp_list_address;
374 u64 guest_physical_address;
375 u64 vmcs_link_pointer;
377 u64 guest_ia32_debugctl;
380 u64 guest_ia32_perf_global_ctrl;
388 u64 host_ia32_perf_global_ctrl;
389 u64 padding64[8]; /* room for future expansion */
391 * To allow migration of L1 (complete with its L2 guests) between
392 * machines of different natural widths (32 or 64 bit), we cannot have
393 * unsigned long fields with no explict size. We use u64 (aliased
394 * natural_width) instead. Luckily, x86 is little-endian.
396 natural_width cr0_guest_host_mask;
397 natural_width cr4_guest_host_mask;
398 natural_width cr0_read_shadow;
399 natural_width cr4_read_shadow;
400 natural_width cr3_target_value0;
401 natural_width cr3_target_value1;
402 natural_width cr3_target_value2;
403 natural_width cr3_target_value3;
404 natural_width exit_qualification;
405 natural_width guest_linear_address;
406 natural_width guest_cr0;
407 natural_width guest_cr3;
408 natural_width guest_cr4;
409 natural_width guest_es_base;
410 natural_width guest_cs_base;
411 natural_width guest_ss_base;
412 natural_width guest_ds_base;
413 natural_width guest_fs_base;
414 natural_width guest_gs_base;
415 natural_width guest_ldtr_base;
416 natural_width guest_tr_base;
417 natural_width guest_gdtr_base;
418 natural_width guest_idtr_base;
419 natural_width guest_dr7;
420 natural_width guest_rsp;
421 natural_width guest_rip;
422 natural_width guest_rflags;
423 natural_width guest_pending_dbg_exceptions;
424 natural_width guest_sysenter_esp;
425 natural_width guest_sysenter_eip;
426 natural_width host_cr0;
427 natural_width host_cr3;
428 natural_width host_cr4;
429 natural_width host_fs_base;
430 natural_width host_gs_base;
431 natural_width host_tr_base;
432 natural_width host_gdtr_base;
433 natural_width host_idtr_base;
434 natural_width host_ia32_sysenter_esp;
435 natural_width host_ia32_sysenter_eip;
436 natural_width host_rsp;
437 natural_width host_rip;
438 natural_width paddingl[8]; /* room for future expansion */
439 u32 pin_based_vm_exec_control;
440 u32 cpu_based_vm_exec_control;
441 u32 exception_bitmap;
442 u32 page_fault_error_code_mask;
443 u32 page_fault_error_code_match;
444 u32 cr3_target_count;
445 u32 vm_exit_controls;
446 u32 vm_exit_msr_store_count;
447 u32 vm_exit_msr_load_count;
448 u32 vm_entry_controls;
449 u32 vm_entry_msr_load_count;
450 u32 vm_entry_intr_info_field;
451 u32 vm_entry_exception_error_code;
452 u32 vm_entry_instruction_len;
454 u32 secondary_vm_exec_control;
455 u32 vm_instruction_error;
457 u32 vm_exit_intr_info;
458 u32 vm_exit_intr_error_code;
459 u32 idt_vectoring_info_field;
460 u32 idt_vectoring_error_code;
461 u32 vm_exit_instruction_len;
462 u32 vmx_instruction_info;
469 u32 guest_ldtr_limit;
471 u32 guest_gdtr_limit;
472 u32 guest_idtr_limit;
473 u32 guest_es_ar_bytes;
474 u32 guest_cs_ar_bytes;
475 u32 guest_ss_ar_bytes;
476 u32 guest_ds_ar_bytes;
477 u32 guest_fs_ar_bytes;
478 u32 guest_gs_ar_bytes;
479 u32 guest_ldtr_ar_bytes;
480 u32 guest_tr_ar_bytes;
481 u32 guest_interruptibility_info;
482 u32 guest_activity_state;
483 u32 guest_sysenter_cs;
484 u32 host_ia32_sysenter_cs;
485 u32 vmx_preemption_timer_value;
486 u32 padding32[7]; /* room for future expansion */
487 u16 virtual_processor_id;
489 u16 guest_es_selector;
490 u16 guest_cs_selector;
491 u16 guest_ss_selector;
492 u16 guest_ds_selector;
493 u16 guest_fs_selector;
494 u16 guest_gs_selector;
495 u16 guest_ldtr_selector;
496 u16 guest_tr_selector;
497 u16 guest_intr_status;
499 u16 host_es_selector;
500 u16 host_cs_selector;
501 u16 host_ss_selector;
502 u16 host_ds_selector;
503 u16 host_fs_selector;
504 u16 host_gs_selector;
505 u16 host_tr_selector;
509 * VMCS12_REVISION is an arbitrary id that should be changed if the content or
510 * layout of struct vmcs12 is changed. MSR_IA32_VMX_BASIC returns this id, and
511 * VMPTRLD verifies that the VMCS region that L1 is loading contains this id.
513 #define VMCS12_REVISION 0x11e57ed0
516 * VMCS12_SIZE is the number of bytes L1 should allocate for the VMXON region
517 * and any VMCS region. Although only sizeof(struct vmcs12) are used by the
518 * current implementation, 4K are reserved to avoid future complications.
520 #define VMCS12_SIZE 0x1000
523 * The nested_vmx structure is part of vcpu_vmx, and holds information we need
524 * for correct emulation of VMX (i.e., nested VMX) on this vcpu.
527 /* Has the level1 guest done vmxon? */
532 /* The guest-physical address of the current VMCS L1 keeps for L2 */
535 * Cache of the guest's VMCS, existing outside of guest memory.
536 * Loaded from guest memory during VMPTRLD. Flushed to guest
537 * memory during VMCLEAR and VMPTRLD.
539 struct vmcs12 *cached_vmcs12;
541 * Indicates if the shadow vmcs must be updated with the
542 * data hold by vmcs12
544 bool sync_shadow_vmcs;
546 bool change_vmcs01_virtual_x2apic_mode;
547 /* L2 must run next, and mustn't decide to exit to L1. */
548 bool nested_run_pending;
550 struct loaded_vmcs vmcs02;
553 * Guest pages referred to in the vmcs02 with host-physical
554 * pointers, so we must keep them pinned while L2 runs.
556 struct page *apic_access_page;
557 struct page *virtual_apic_page;
558 struct page *pi_desc_page;
559 struct pi_desc *pi_desc;
563 struct hrtimer preemption_timer;
564 bool preemption_timer_expired;
566 /* to migrate it to L2 if VM_ENTRY_LOAD_DEBUG_CONTROLS is off */
573 * We only store the "true" versions of the VMX capability MSRs. We
574 * generate the "non-true" versions by setting the must-be-1 bits
575 * according to the SDM.
577 u32 nested_vmx_procbased_ctls_low;
578 u32 nested_vmx_procbased_ctls_high;
579 u32 nested_vmx_secondary_ctls_low;
580 u32 nested_vmx_secondary_ctls_high;
581 u32 nested_vmx_pinbased_ctls_low;
582 u32 nested_vmx_pinbased_ctls_high;
583 u32 nested_vmx_exit_ctls_low;
584 u32 nested_vmx_exit_ctls_high;
585 u32 nested_vmx_entry_ctls_low;
586 u32 nested_vmx_entry_ctls_high;
587 u32 nested_vmx_misc_low;
588 u32 nested_vmx_misc_high;
589 u32 nested_vmx_ept_caps;
590 u32 nested_vmx_vpid_caps;
591 u64 nested_vmx_basic;
592 u64 nested_vmx_cr0_fixed0;
593 u64 nested_vmx_cr0_fixed1;
594 u64 nested_vmx_cr4_fixed0;
595 u64 nested_vmx_cr4_fixed1;
596 u64 nested_vmx_vmcs_enum;
597 u64 nested_vmx_vmfunc_controls;
600 #define POSTED_INTR_ON 0
601 #define POSTED_INTR_SN 1
603 /* Posted-Interrupt Descriptor */
605 u32 pir[8]; /* Posted interrupt requested */
608 /* bit 256 - Outstanding Notification */
610 /* bit 257 - Suppress Notification */
612 /* bit 271:258 - Reserved */
614 /* bit 279:272 - Notification Vector */
616 /* bit 287:280 - Reserved */
618 /* bit 319:288 - Notification Destination */
626 static bool pi_test_and_set_on(struct pi_desc *pi_desc)
628 return test_and_set_bit(POSTED_INTR_ON,
629 (unsigned long *)&pi_desc->control);
632 static bool pi_test_and_clear_on(struct pi_desc *pi_desc)
634 return test_and_clear_bit(POSTED_INTR_ON,
635 (unsigned long *)&pi_desc->control);
638 static int pi_test_and_set_pir(int vector, struct pi_desc *pi_desc)
640 return test_and_set_bit(vector, (unsigned long *)pi_desc->pir);
643 static inline void pi_clear_sn(struct pi_desc *pi_desc)
645 return clear_bit(POSTED_INTR_SN,
646 (unsigned long *)&pi_desc->control);
649 static inline void pi_set_sn(struct pi_desc *pi_desc)
651 return set_bit(POSTED_INTR_SN,
652 (unsigned long *)&pi_desc->control);
655 static inline void pi_clear_on(struct pi_desc *pi_desc)
657 clear_bit(POSTED_INTR_ON,
658 (unsigned long *)&pi_desc->control);
661 static inline int pi_test_on(struct pi_desc *pi_desc)
663 return test_bit(POSTED_INTR_ON,
664 (unsigned long *)&pi_desc->control);
667 static inline int pi_test_sn(struct pi_desc *pi_desc)
669 return test_bit(POSTED_INTR_SN,
670 (unsigned long *)&pi_desc->control);
675 struct vmx_msr_entry val[NR_AUTOLOAD_MSRS];
679 struct kvm_vcpu vcpu;
680 unsigned long host_rsp;
684 u32 idt_vectoring_info;
686 struct shared_msr_entry *guest_msrs;
689 unsigned long host_idt_base;
691 u64 msr_host_kernel_gs_base;
692 u64 msr_guest_kernel_gs_base;
695 u64 arch_capabilities;
698 u32 vm_entry_controls_shadow;
699 u32 vm_exit_controls_shadow;
700 u32 secondary_exec_control;
703 * loaded_vmcs points to the VMCS currently used in this vcpu. For a
704 * non-nested (L1) guest, it always points to vmcs01. For a nested
705 * guest (L2), it points to a different VMCS.
707 struct loaded_vmcs vmcs01;
708 struct loaded_vmcs *loaded_vmcs;
709 bool __launched; /* temporary, used in vmx_vcpu_run */
710 struct msr_autoload {
711 struct vmx_msrs guest;
712 struct vmx_msrs host;
716 u16 fs_sel, gs_sel, ldt_sel;
720 int gs_ldt_reload_needed;
721 int fs_reload_needed;
722 u64 msr_host_bndcfgs;
727 struct kvm_segment segs[8];
730 u32 bitmask; /* 4 bits per segment (1 bit per field) */
731 struct kvm_save_segment {
739 bool emulation_required;
743 /* Posted interrupt descriptor */
744 struct pi_desc pi_desc;
746 /* Support for a guest hypervisor (nested VMX) */
747 struct nested_vmx nested;
749 /* Dynamic PLE window. */
751 bool ple_window_dirty;
753 /* Support for PML */
754 #define PML_ENTITY_NUM 512
757 /* apic deadline value in host tsc */
760 u64 current_tsc_ratio;
765 * Only bits masked by msr_ia32_feature_control_valid_bits can be set in
766 * msr_ia32_feature_control. FEATURE_CONTROL_LOCKED is always included
767 * in msr_ia32_feature_control_valid_bits.
769 u64 msr_ia32_feature_control;
770 u64 msr_ia32_feature_control_valid_bits;
773 enum segment_cache_field {
782 static inline struct vcpu_vmx *to_vmx(struct kvm_vcpu *vcpu)
784 return container_of(vcpu, struct vcpu_vmx, vcpu);
787 static struct pi_desc *vcpu_to_pi_desc(struct kvm_vcpu *vcpu)
789 return &(to_vmx(vcpu)->pi_desc);
792 #define VMCS12_OFFSET(x) offsetof(struct vmcs12, x)
793 #define FIELD(number, name) [number] = VMCS12_OFFSET(name)
794 #define FIELD64(number, name) [number] = VMCS12_OFFSET(name), \
795 [number##_HIGH] = VMCS12_OFFSET(name)+4
798 static unsigned long shadow_read_only_fields[] = {
800 * We do NOT shadow fields that are modified when L0
801 * traps and emulates any vmx instruction (e.g. VMPTRLD,
802 * VMXON...) executed by L1.
803 * For example, VM_INSTRUCTION_ERROR is read
804 * by L1 if a vmx instruction fails (part of the error path).
805 * Note the code assumes this logic. If for some reason
806 * we start shadowing these fields then we need to
807 * force a shadow sync when L0 emulates vmx instructions
808 * (e.g. force a sync if VM_INSTRUCTION_ERROR is modified
809 * by nested_vmx_failValid)
813 VM_EXIT_INSTRUCTION_LEN,
814 IDT_VECTORING_INFO_FIELD,
815 IDT_VECTORING_ERROR_CODE,
816 VM_EXIT_INTR_ERROR_CODE,
818 GUEST_LINEAR_ADDRESS,
819 GUEST_PHYSICAL_ADDRESS
821 static int max_shadow_read_only_fields =
822 ARRAY_SIZE(shadow_read_only_fields);
824 static unsigned long shadow_read_write_fields[] = {
831 GUEST_INTERRUPTIBILITY_INFO,
844 CPU_BASED_VM_EXEC_CONTROL,
845 VM_ENTRY_EXCEPTION_ERROR_CODE,
846 VM_ENTRY_INTR_INFO_FIELD,
847 VM_ENTRY_INSTRUCTION_LEN,
848 VM_ENTRY_EXCEPTION_ERROR_CODE,
854 static int max_shadow_read_write_fields =
855 ARRAY_SIZE(shadow_read_write_fields);
857 static const unsigned short vmcs_field_to_offset_table[] = {
858 FIELD(VIRTUAL_PROCESSOR_ID, virtual_processor_id),
859 FIELD(POSTED_INTR_NV, posted_intr_nv),
860 FIELD(GUEST_ES_SELECTOR, guest_es_selector),
861 FIELD(GUEST_CS_SELECTOR, guest_cs_selector),
862 FIELD(GUEST_SS_SELECTOR, guest_ss_selector),
863 FIELD(GUEST_DS_SELECTOR, guest_ds_selector),
864 FIELD(GUEST_FS_SELECTOR, guest_fs_selector),
865 FIELD(GUEST_GS_SELECTOR, guest_gs_selector),
866 FIELD(GUEST_LDTR_SELECTOR, guest_ldtr_selector),
867 FIELD(GUEST_TR_SELECTOR, guest_tr_selector),
868 FIELD(GUEST_INTR_STATUS, guest_intr_status),
869 FIELD(GUEST_PML_INDEX, guest_pml_index),
870 FIELD(HOST_ES_SELECTOR, host_es_selector),
871 FIELD(HOST_CS_SELECTOR, host_cs_selector),
872 FIELD(HOST_SS_SELECTOR, host_ss_selector),
873 FIELD(HOST_DS_SELECTOR, host_ds_selector),
874 FIELD(HOST_FS_SELECTOR, host_fs_selector),
875 FIELD(HOST_GS_SELECTOR, host_gs_selector),
876 FIELD(HOST_TR_SELECTOR, host_tr_selector),
877 FIELD64(IO_BITMAP_A, io_bitmap_a),
878 FIELD64(IO_BITMAP_B, io_bitmap_b),
879 FIELD64(MSR_BITMAP, msr_bitmap),
880 FIELD64(VM_EXIT_MSR_STORE_ADDR, vm_exit_msr_store_addr),
881 FIELD64(VM_EXIT_MSR_LOAD_ADDR, vm_exit_msr_load_addr),
882 FIELD64(VM_ENTRY_MSR_LOAD_ADDR, vm_entry_msr_load_addr),
883 FIELD64(TSC_OFFSET, tsc_offset),
884 FIELD64(VIRTUAL_APIC_PAGE_ADDR, virtual_apic_page_addr),
885 FIELD64(APIC_ACCESS_ADDR, apic_access_addr),
886 FIELD64(POSTED_INTR_DESC_ADDR, posted_intr_desc_addr),
887 FIELD64(VM_FUNCTION_CONTROL, vm_function_control),
888 FIELD64(EPT_POINTER, ept_pointer),
889 FIELD64(EOI_EXIT_BITMAP0, eoi_exit_bitmap0),
890 FIELD64(EOI_EXIT_BITMAP1, eoi_exit_bitmap1),
891 FIELD64(EOI_EXIT_BITMAP2, eoi_exit_bitmap2),
892 FIELD64(EOI_EXIT_BITMAP3, eoi_exit_bitmap3),
893 FIELD64(EPTP_LIST_ADDRESS, eptp_list_address),
894 FIELD64(XSS_EXIT_BITMAP, xss_exit_bitmap),
895 FIELD64(GUEST_PHYSICAL_ADDRESS, guest_physical_address),
896 FIELD64(VMCS_LINK_POINTER, vmcs_link_pointer),
897 FIELD64(PML_ADDRESS, pml_address),
898 FIELD64(GUEST_IA32_DEBUGCTL, guest_ia32_debugctl),
899 FIELD64(GUEST_IA32_PAT, guest_ia32_pat),
900 FIELD64(GUEST_IA32_EFER, guest_ia32_efer),
901 FIELD64(GUEST_IA32_PERF_GLOBAL_CTRL, guest_ia32_perf_global_ctrl),
902 FIELD64(GUEST_PDPTR0, guest_pdptr0),
903 FIELD64(GUEST_PDPTR1, guest_pdptr1),
904 FIELD64(GUEST_PDPTR2, guest_pdptr2),
905 FIELD64(GUEST_PDPTR3, guest_pdptr3),
906 FIELD64(GUEST_BNDCFGS, guest_bndcfgs),
907 FIELD64(HOST_IA32_PAT, host_ia32_pat),
908 FIELD64(HOST_IA32_EFER, host_ia32_efer),
909 FIELD64(HOST_IA32_PERF_GLOBAL_CTRL, host_ia32_perf_global_ctrl),
910 FIELD(PIN_BASED_VM_EXEC_CONTROL, pin_based_vm_exec_control),
911 FIELD(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control),
912 FIELD(EXCEPTION_BITMAP, exception_bitmap),
913 FIELD(PAGE_FAULT_ERROR_CODE_MASK, page_fault_error_code_mask),
914 FIELD(PAGE_FAULT_ERROR_CODE_MATCH, page_fault_error_code_match),
915 FIELD(CR3_TARGET_COUNT, cr3_target_count),
916 FIELD(VM_EXIT_CONTROLS, vm_exit_controls),
917 FIELD(VM_EXIT_MSR_STORE_COUNT, vm_exit_msr_store_count),
918 FIELD(VM_EXIT_MSR_LOAD_COUNT, vm_exit_msr_load_count),
919 FIELD(VM_ENTRY_CONTROLS, vm_entry_controls),
920 FIELD(VM_ENTRY_MSR_LOAD_COUNT, vm_entry_msr_load_count),
921 FIELD(VM_ENTRY_INTR_INFO_FIELD, vm_entry_intr_info_field),
922 FIELD(VM_ENTRY_EXCEPTION_ERROR_CODE, vm_entry_exception_error_code),
923 FIELD(VM_ENTRY_INSTRUCTION_LEN, vm_entry_instruction_len),
924 FIELD(TPR_THRESHOLD, tpr_threshold),
925 FIELD(SECONDARY_VM_EXEC_CONTROL, secondary_vm_exec_control),
926 FIELD(VM_INSTRUCTION_ERROR, vm_instruction_error),
927 FIELD(VM_EXIT_REASON, vm_exit_reason),
928 FIELD(VM_EXIT_INTR_INFO, vm_exit_intr_info),
929 FIELD(VM_EXIT_INTR_ERROR_CODE, vm_exit_intr_error_code),
930 FIELD(IDT_VECTORING_INFO_FIELD, idt_vectoring_info_field),
931 FIELD(IDT_VECTORING_ERROR_CODE, idt_vectoring_error_code),
932 FIELD(VM_EXIT_INSTRUCTION_LEN, vm_exit_instruction_len),
933 FIELD(VMX_INSTRUCTION_INFO, vmx_instruction_info),
934 FIELD(GUEST_ES_LIMIT, guest_es_limit),
935 FIELD(GUEST_CS_LIMIT, guest_cs_limit),
936 FIELD(GUEST_SS_LIMIT, guest_ss_limit),
937 FIELD(GUEST_DS_LIMIT, guest_ds_limit),
938 FIELD(GUEST_FS_LIMIT, guest_fs_limit),
939 FIELD(GUEST_GS_LIMIT, guest_gs_limit),
940 FIELD(GUEST_LDTR_LIMIT, guest_ldtr_limit),
941 FIELD(GUEST_TR_LIMIT, guest_tr_limit),
942 FIELD(GUEST_GDTR_LIMIT, guest_gdtr_limit),
943 FIELD(GUEST_IDTR_LIMIT, guest_idtr_limit),
944 FIELD(GUEST_ES_AR_BYTES, guest_es_ar_bytes),
945 FIELD(GUEST_CS_AR_BYTES, guest_cs_ar_bytes),
946 FIELD(GUEST_SS_AR_BYTES, guest_ss_ar_bytes),
947 FIELD(GUEST_DS_AR_BYTES, guest_ds_ar_bytes),
948 FIELD(GUEST_FS_AR_BYTES, guest_fs_ar_bytes),
949 FIELD(GUEST_GS_AR_BYTES, guest_gs_ar_bytes),
950 FIELD(GUEST_LDTR_AR_BYTES, guest_ldtr_ar_bytes),
951 FIELD(GUEST_TR_AR_BYTES, guest_tr_ar_bytes),
952 FIELD(GUEST_INTERRUPTIBILITY_INFO, guest_interruptibility_info),
953 FIELD(GUEST_ACTIVITY_STATE, guest_activity_state),
954 FIELD(GUEST_SYSENTER_CS, guest_sysenter_cs),
955 FIELD(HOST_IA32_SYSENTER_CS, host_ia32_sysenter_cs),
956 FIELD(VMX_PREEMPTION_TIMER_VALUE, vmx_preemption_timer_value),
957 FIELD(CR0_GUEST_HOST_MASK, cr0_guest_host_mask),
958 FIELD(CR4_GUEST_HOST_MASK, cr4_guest_host_mask),
959 FIELD(CR0_READ_SHADOW, cr0_read_shadow),
960 FIELD(CR4_READ_SHADOW, cr4_read_shadow),
961 FIELD(CR3_TARGET_VALUE0, cr3_target_value0),
962 FIELD(CR3_TARGET_VALUE1, cr3_target_value1),
963 FIELD(CR3_TARGET_VALUE2, cr3_target_value2),
964 FIELD(CR3_TARGET_VALUE3, cr3_target_value3),
965 FIELD(EXIT_QUALIFICATION, exit_qualification),
966 FIELD(GUEST_LINEAR_ADDRESS, guest_linear_address),
967 FIELD(GUEST_CR0, guest_cr0),
968 FIELD(GUEST_CR3, guest_cr3),
969 FIELD(GUEST_CR4, guest_cr4),
970 FIELD(GUEST_ES_BASE, guest_es_base),
971 FIELD(GUEST_CS_BASE, guest_cs_base),
972 FIELD(GUEST_SS_BASE, guest_ss_base),
973 FIELD(GUEST_DS_BASE, guest_ds_base),
974 FIELD(GUEST_FS_BASE, guest_fs_base),
975 FIELD(GUEST_GS_BASE, guest_gs_base),
976 FIELD(GUEST_LDTR_BASE, guest_ldtr_base),
977 FIELD(GUEST_TR_BASE, guest_tr_base),
978 FIELD(GUEST_GDTR_BASE, guest_gdtr_base),
979 FIELD(GUEST_IDTR_BASE, guest_idtr_base),
980 FIELD(GUEST_DR7, guest_dr7),
981 FIELD(GUEST_RSP, guest_rsp),
982 FIELD(GUEST_RIP, guest_rip),
983 FIELD(GUEST_RFLAGS, guest_rflags),
984 FIELD(GUEST_PENDING_DBG_EXCEPTIONS, guest_pending_dbg_exceptions),
985 FIELD(GUEST_SYSENTER_ESP, guest_sysenter_esp),
986 FIELD(GUEST_SYSENTER_EIP, guest_sysenter_eip),
987 FIELD(HOST_CR0, host_cr0),
988 FIELD(HOST_CR3, host_cr3),
989 FIELD(HOST_CR4, host_cr4),
990 FIELD(HOST_FS_BASE, host_fs_base),
991 FIELD(HOST_GS_BASE, host_gs_base),
992 FIELD(HOST_TR_BASE, host_tr_base),
993 FIELD(HOST_GDTR_BASE, host_gdtr_base),
994 FIELD(HOST_IDTR_BASE, host_idtr_base),
995 FIELD(HOST_IA32_SYSENTER_ESP, host_ia32_sysenter_esp),
996 FIELD(HOST_IA32_SYSENTER_EIP, host_ia32_sysenter_eip),
997 FIELD(HOST_RSP, host_rsp),
998 FIELD(HOST_RIP, host_rip),
1001 static inline short vmcs_field_to_offset(unsigned long field)
1003 const size_t size = ARRAY_SIZE(vmcs_field_to_offset_table);
1004 unsigned short offset;
1006 BUILD_BUG_ON(size > SHRT_MAX);
1010 field = array_index_nospec(field, size);
1011 offset = vmcs_field_to_offset_table[field];
1017 static inline struct vmcs12 *get_vmcs12(struct kvm_vcpu *vcpu)
1019 return to_vmx(vcpu)->nested.cached_vmcs12;
1022 static bool nested_ept_ad_enabled(struct kvm_vcpu *vcpu);
1023 static unsigned long nested_ept_get_cr3(struct kvm_vcpu *vcpu);
1024 static u64 construct_eptp(struct kvm_vcpu *vcpu, unsigned long root_hpa);
1025 static bool vmx_xsaves_supported(void);
1026 static int vmx_set_tss_addr(struct kvm *kvm, unsigned int addr);
1027 static void vmx_set_segment(struct kvm_vcpu *vcpu,
1028 struct kvm_segment *var, int seg);
1029 static void vmx_get_segment(struct kvm_vcpu *vcpu,
1030 struct kvm_segment *var, int seg);
1031 static bool guest_state_valid(struct kvm_vcpu *vcpu);
1032 static u32 vmx_segment_access_rights(struct kvm_segment *var);
1033 static void copy_vmcs12_to_shadow(struct vcpu_vmx *vmx);
1034 static void copy_shadow_to_vmcs12(struct vcpu_vmx *vmx);
1035 static int alloc_identity_pagetable(struct kvm *kvm);
1036 static bool vmx_get_nmi_mask(struct kvm_vcpu *vcpu);
1037 static void vmx_set_nmi_mask(struct kvm_vcpu *vcpu, bool masked);
1038 static bool nested_vmx_is_page_fault_vmexit(struct vmcs12 *vmcs12,
1040 static void vmx_update_msr_bitmap(struct kvm_vcpu *vcpu);
1041 static void __always_inline vmx_disable_intercept_for_msr(unsigned long *msr_bitmap,
1044 static DEFINE_PER_CPU(struct vmcs *, vmxarea);
1045 static DEFINE_PER_CPU(struct vmcs *, current_vmcs);
1047 * We maintain a per-CPU linked-list of VMCS loaded on that CPU. This is needed
1048 * when a CPU is brought down, and we need to VMCLEAR all VMCSs loaded on it.
1050 static DEFINE_PER_CPU(struct list_head, loaded_vmcss_on_cpu);
1053 * We maintian a per-CPU linked-list of vCPU, so in wakeup_handler() we
1054 * can find which vCPU should be waken up.
1056 static DEFINE_PER_CPU(struct list_head, blocked_vcpu_on_cpu);
1057 static DEFINE_PER_CPU(spinlock_t, blocked_vcpu_on_cpu_lock);
1067 static unsigned long *vmx_bitmap[VMX_BITMAP_NR];
1069 #define vmx_io_bitmap_a (vmx_bitmap[VMX_IO_BITMAP_A])
1070 #define vmx_io_bitmap_b (vmx_bitmap[VMX_IO_BITMAP_B])
1071 #define vmx_vmread_bitmap (vmx_bitmap[VMX_VMREAD_BITMAP])
1072 #define vmx_vmwrite_bitmap (vmx_bitmap[VMX_VMWRITE_BITMAP])
1074 static bool cpu_has_load_ia32_efer;
1075 static bool cpu_has_load_perf_global_ctrl;
1077 static DECLARE_BITMAP(vmx_vpid_bitmap, VMX_NR_VPIDS);
1078 static DEFINE_SPINLOCK(vmx_vpid_lock);
1080 static struct vmcs_config {
1085 u32 pin_based_exec_ctrl;
1086 u32 cpu_based_exec_ctrl;
1087 u32 cpu_based_2nd_exec_ctrl;
1092 static struct vmx_capability {
1097 #define VMX_SEGMENT_FIELD(seg) \
1098 [VCPU_SREG_##seg] = { \
1099 .selector = GUEST_##seg##_SELECTOR, \
1100 .base = GUEST_##seg##_BASE, \
1101 .limit = GUEST_##seg##_LIMIT, \
1102 .ar_bytes = GUEST_##seg##_AR_BYTES, \
1105 static const struct kvm_vmx_segment_field {
1110 } kvm_vmx_segment_fields[] = {
1111 VMX_SEGMENT_FIELD(CS),
1112 VMX_SEGMENT_FIELD(DS),
1113 VMX_SEGMENT_FIELD(ES),
1114 VMX_SEGMENT_FIELD(FS),
1115 VMX_SEGMENT_FIELD(GS),
1116 VMX_SEGMENT_FIELD(SS),
1117 VMX_SEGMENT_FIELD(TR),
1118 VMX_SEGMENT_FIELD(LDTR),
1121 static u64 host_efer;
1123 static void ept_save_pdptrs(struct kvm_vcpu *vcpu);
1126 * Keep MSR_STAR at the end, as setup_msrs() will try to optimize it
1127 * away by decrementing the array size.
1129 static const u32 vmx_msr_index[] = {
1130 #ifdef CONFIG_X86_64
1131 MSR_SYSCALL_MASK, MSR_LSTAR, MSR_CSTAR,
1133 MSR_EFER, MSR_TSC_AUX, MSR_STAR,
1136 static inline bool is_exception_n(u32 intr_info, u8 vector)
1138 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
1139 INTR_INFO_VALID_MASK)) ==
1140 (INTR_TYPE_HARD_EXCEPTION | vector | INTR_INFO_VALID_MASK);
1143 static inline bool is_debug(u32 intr_info)
1145 return is_exception_n(intr_info, DB_VECTOR);
1148 static inline bool is_breakpoint(u32 intr_info)
1150 return is_exception_n(intr_info, BP_VECTOR);
1153 static inline bool is_page_fault(u32 intr_info)
1155 return is_exception_n(intr_info, PF_VECTOR);
1158 static inline bool is_no_device(u32 intr_info)
1160 return is_exception_n(intr_info, NM_VECTOR);
1163 static inline bool is_invalid_opcode(u32 intr_info)
1165 return is_exception_n(intr_info, UD_VECTOR);
1168 static inline bool is_external_interrupt(u32 intr_info)
1170 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK))
1171 == (INTR_TYPE_EXT_INTR | INTR_INFO_VALID_MASK);
1174 static inline bool is_machine_check(u32 intr_info)
1176 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
1177 INTR_INFO_VALID_MASK)) ==
1178 (INTR_TYPE_HARD_EXCEPTION | MC_VECTOR | INTR_INFO_VALID_MASK);
1181 /* Undocumented: icebp/int1 */
1182 static inline bool is_icebp(u32 intr_info)
1184 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK))
1185 == (INTR_TYPE_PRIV_SW_EXCEPTION | INTR_INFO_VALID_MASK);
1188 static inline bool cpu_has_vmx_msr_bitmap(void)
1190 return vmcs_config.cpu_based_exec_ctrl & CPU_BASED_USE_MSR_BITMAPS;
1193 static inline bool cpu_has_vmx_tpr_shadow(void)
1195 return vmcs_config.cpu_based_exec_ctrl & CPU_BASED_TPR_SHADOW;
1198 static inline bool cpu_need_tpr_shadow(struct kvm_vcpu *vcpu)
1200 return cpu_has_vmx_tpr_shadow() && lapic_in_kernel(vcpu);
1203 static inline bool cpu_has_secondary_exec_ctrls(void)
1205 return vmcs_config.cpu_based_exec_ctrl &
1206 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
1209 static inline bool cpu_has_vmx_virtualize_apic_accesses(void)
1211 return vmcs_config.cpu_based_2nd_exec_ctrl &
1212 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
1215 static inline bool cpu_has_vmx_virtualize_x2apic_mode(void)
1217 return vmcs_config.cpu_based_2nd_exec_ctrl &
1218 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
1221 static inline bool cpu_has_vmx_apic_register_virt(void)
1223 return vmcs_config.cpu_based_2nd_exec_ctrl &
1224 SECONDARY_EXEC_APIC_REGISTER_VIRT;
1227 static inline bool cpu_has_vmx_virtual_intr_delivery(void)
1229 return vmcs_config.cpu_based_2nd_exec_ctrl &
1230 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY;
1234 * Comment's format: document - errata name - stepping - processor name.
1236 * https://www.virtualbox.org/svn/vbox/trunk/src/VBox/VMM/VMMR0/HMR0.cpp
1238 static u32 vmx_preemption_cpu_tfms[] = {
1239 /* 323344.pdf - BA86 - D0 - Xeon 7500 Series */
1241 /* 323056.pdf - AAX65 - C2 - Xeon L3406 */
1242 /* 322814.pdf - AAT59 - C2 - i7-600, i5-500, i5-400 and i3-300 Mobile */
1243 /* 322911.pdf - AAU65 - C2 - i5-600, i3-500 Desktop and Pentium G6950 */
1245 /* 322911.pdf - AAU65 - K0 - i5-600, i3-500 Desktop and Pentium G6950 */
1247 /* 322373.pdf - AAO95 - B1 - Xeon 3400 Series */
1248 /* 322166.pdf - AAN92 - B1 - i7-800 and i5-700 Desktop */
1250 * 320767.pdf - AAP86 - B1 -
1251 * i7-900 Mobile Extreme, i7-800 and i7-700 Mobile
1254 /* 321333.pdf - AAM126 - C0 - Xeon 3500 */
1256 /* 321333.pdf - AAM126 - C1 - Xeon 3500 */
1258 /* 320836.pdf - AAJ124 - C0 - i7-900 Desktop Extreme and i7-900 Desktop */
1260 /* 321333.pdf - AAM126 - D0 - Xeon 3500 */
1261 /* 321324.pdf - AAK139 - D0 - Xeon 5500 */
1262 /* 320836.pdf - AAJ124 - D0 - i7-900 Extreme and i7-900 Desktop */
1266 static inline bool cpu_has_broken_vmx_preemption_timer(void)
1268 u32 eax = cpuid_eax(0x00000001), i;
1270 /* Clear the reserved bits */
1271 eax &= ~(0x3U << 14 | 0xfU << 28);
1272 for (i = 0; i < ARRAY_SIZE(vmx_preemption_cpu_tfms); i++)
1273 if (eax == vmx_preemption_cpu_tfms[i])
1279 static inline bool cpu_has_vmx_preemption_timer(void)
1281 return vmcs_config.pin_based_exec_ctrl &
1282 PIN_BASED_VMX_PREEMPTION_TIMER;
1285 static inline bool cpu_has_vmx_posted_intr(void)
1287 return IS_ENABLED(CONFIG_X86_LOCAL_APIC) &&
1288 vmcs_config.pin_based_exec_ctrl & PIN_BASED_POSTED_INTR;
1291 static inline bool cpu_has_vmx_apicv(void)
1293 return cpu_has_vmx_apic_register_virt() &&
1294 cpu_has_vmx_virtual_intr_delivery() &&
1295 cpu_has_vmx_posted_intr();
1298 static inline bool cpu_has_vmx_flexpriority(void)
1300 return cpu_has_vmx_tpr_shadow() &&
1301 cpu_has_vmx_virtualize_apic_accesses();
1304 static inline bool cpu_has_vmx_ept_execute_only(void)
1306 return vmx_capability.ept & VMX_EPT_EXECUTE_ONLY_BIT;
1309 static inline bool cpu_has_vmx_ept_2m_page(void)
1311 return vmx_capability.ept & VMX_EPT_2MB_PAGE_BIT;
1314 static inline bool cpu_has_vmx_ept_1g_page(void)
1316 return vmx_capability.ept & VMX_EPT_1GB_PAGE_BIT;
1319 static inline bool cpu_has_vmx_ept_4levels(void)
1321 return vmx_capability.ept & VMX_EPT_PAGE_WALK_4_BIT;
1324 static inline bool cpu_has_vmx_ept_mt_wb(void)
1326 return vmx_capability.ept & VMX_EPTP_WB_BIT;
1329 static inline bool cpu_has_vmx_ept_5levels(void)
1331 return vmx_capability.ept & VMX_EPT_PAGE_WALK_5_BIT;
1334 static inline bool cpu_has_vmx_ept_ad_bits(void)
1336 return vmx_capability.ept & VMX_EPT_AD_BIT;
1339 static inline bool cpu_has_vmx_invept_context(void)
1341 return vmx_capability.ept & VMX_EPT_EXTENT_CONTEXT_BIT;
1344 static inline bool cpu_has_vmx_invept_global(void)
1346 return vmx_capability.ept & VMX_EPT_EXTENT_GLOBAL_BIT;
1349 static inline bool cpu_has_vmx_invvpid_single(void)
1351 return vmx_capability.vpid & VMX_VPID_EXTENT_SINGLE_CONTEXT_BIT;
1354 static inline bool cpu_has_vmx_invvpid_global(void)
1356 return vmx_capability.vpid & VMX_VPID_EXTENT_GLOBAL_CONTEXT_BIT;
1359 static inline bool cpu_has_vmx_invvpid(void)
1361 return vmx_capability.vpid & VMX_VPID_INVVPID_BIT;
1364 static inline bool cpu_has_vmx_ept(void)
1366 return vmcs_config.cpu_based_2nd_exec_ctrl &
1367 SECONDARY_EXEC_ENABLE_EPT;
1370 static inline bool cpu_has_vmx_unrestricted_guest(void)
1372 return vmcs_config.cpu_based_2nd_exec_ctrl &
1373 SECONDARY_EXEC_UNRESTRICTED_GUEST;
1376 static inline bool cpu_has_vmx_ple(void)
1378 return vmcs_config.cpu_based_2nd_exec_ctrl &
1379 SECONDARY_EXEC_PAUSE_LOOP_EXITING;
1382 static inline bool cpu_has_vmx_basic_inout(void)
1384 return (((u64)vmcs_config.basic_cap << 32) & VMX_BASIC_INOUT);
1387 static inline bool cpu_need_virtualize_apic_accesses(struct kvm_vcpu *vcpu)
1389 return flexpriority_enabled && lapic_in_kernel(vcpu);
1392 static inline bool cpu_has_vmx_vpid(void)
1394 return vmcs_config.cpu_based_2nd_exec_ctrl &
1395 SECONDARY_EXEC_ENABLE_VPID;
1398 static inline bool cpu_has_vmx_rdtscp(void)
1400 return vmcs_config.cpu_based_2nd_exec_ctrl &
1401 SECONDARY_EXEC_RDTSCP;
1404 static inline bool cpu_has_vmx_invpcid(void)
1406 return vmcs_config.cpu_based_2nd_exec_ctrl &
1407 SECONDARY_EXEC_ENABLE_INVPCID;
1410 static inline bool cpu_has_virtual_nmis(void)
1412 return vmcs_config.pin_based_exec_ctrl & PIN_BASED_VIRTUAL_NMIS;
1415 static inline bool cpu_has_vmx_wbinvd_exit(void)
1417 return vmcs_config.cpu_based_2nd_exec_ctrl &
1418 SECONDARY_EXEC_WBINVD_EXITING;
1421 static inline bool cpu_has_vmx_shadow_vmcs(void)
1424 rdmsrl(MSR_IA32_VMX_MISC, vmx_msr);
1425 /* check if the cpu supports writing r/o exit information fields */
1426 if (!(vmx_msr & MSR_IA32_VMX_MISC_VMWRITE_SHADOW_RO_FIELDS))
1429 return vmcs_config.cpu_based_2nd_exec_ctrl &
1430 SECONDARY_EXEC_SHADOW_VMCS;
1433 static inline bool cpu_has_vmx_pml(void)
1435 return vmcs_config.cpu_based_2nd_exec_ctrl & SECONDARY_EXEC_ENABLE_PML;
1438 static inline bool cpu_has_vmx_tsc_scaling(void)
1440 return vmcs_config.cpu_based_2nd_exec_ctrl &
1441 SECONDARY_EXEC_TSC_SCALING;
1444 static inline bool cpu_has_vmx_vmfunc(void)
1446 return vmcs_config.cpu_based_2nd_exec_ctrl &
1447 SECONDARY_EXEC_ENABLE_VMFUNC;
1450 static inline bool report_flexpriority(void)
1452 return flexpriority_enabled;
1455 static inline unsigned nested_cpu_vmx_misc_cr3_count(struct kvm_vcpu *vcpu)
1457 return vmx_misc_cr3_count(to_vmx(vcpu)->nested.nested_vmx_misc_low);
1460 static inline bool nested_cpu_has(struct vmcs12 *vmcs12, u32 bit)
1462 return vmcs12->cpu_based_vm_exec_control & bit;
1465 static inline bool nested_cpu_has2(struct vmcs12 *vmcs12, u32 bit)
1467 return (vmcs12->cpu_based_vm_exec_control &
1468 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) &&
1469 (vmcs12->secondary_vm_exec_control & bit);
1472 static inline bool nested_cpu_has_preemption_timer(struct vmcs12 *vmcs12)
1474 return vmcs12->pin_based_vm_exec_control &
1475 PIN_BASED_VMX_PREEMPTION_TIMER;
1478 static inline int nested_cpu_has_ept(struct vmcs12 *vmcs12)
1480 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_EPT);
1483 static inline bool nested_cpu_has_xsaves(struct vmcs12 *vmcs12)
1485 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_XSAVES);
1488 static inline bool nested_cpu_has_pml(struct vmcs12 *vmcs12)
1490 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_PML);
1493 static inline bool nested_cpu_has_virt_x2apic_mode(struct vmcs12 *vmcs12)
1495 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE);
1498 static inline bool nested_cpu_has_vpid(struct vmcs12 *vmcs12)
1500 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_VPID);
1503 static inline bool nested_cpu_has_apic_reg_virt(struct vmcs12 *vmcs12)
1505 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_APIC_REGISTER_VIRT);
1508 static inline bool nested_cpu_has_vid(struct vmcs12 *vmcs12)
1510 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
1513 static inline bool nested_cpu_has_posted_intr(struct vmcs12 *vmcs12)
1515 return vmcs12->pin_based_vm_exec_control & PIN_BASED_POSTED_INTR;
1518 static inline bool nested_cpu_has_vmfunc(struct vmcs12 *vmcs12)
1520 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_VMFUNC);
1523 static inline bool nested_cpu_has_eptp_switching(struct vmcs12 *vmcs12)
1525 return nested_cpu_has_vmfunc(vmcs12) &&
1526 (vmcs12->vm_function_control &
1527 VMX_VMFUNC_EPTP_SWITCHING);
1530 static inline bool is_nmi(u32 intr_info)
1532 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK))
1533 == (INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK);
1536 static void nested_vmx_vmexit(struct kvm_vcpu *vcpu, u32 exit_reason,
1538 unsigned long exit_qualification);
1539 static void nested_vmx_entry_failure(struct kvm_vcpu *vcpu,
1540 struct vmcs12 *vmcs12,
1541 u32 reason, unsigned long qualification);
1543 static int __find_msr_index(struct vcpu_vmx *vmx, u32 msr)
1547 for (i = 0; i < vmx->nmsrs; ++i)
1548 if (vmx_msr_index[vmx->guest_msrs[i].index] == msr)
1553 static inline void __invvpid(int ext, u16 vpid, gva_t gva)
1559 } operand = { vpid, 0, gva };
1561 asm volatile (__ex(ASM_VMX_INVVPID)
1562 /* CF==1 or ZF==1 --> rc = -1 */
1563 "; ja 1f ; ud2 ; 1:"
1564 : : "a"(&operand), "c"(ext) : "cc", "memory");
1567 static inline void __invept(int ext, u64 eptp, gpa_t gpa)
1571 } operand = {eptp, gpa};
1573 asm volatile (__ex(ASM_VMX_INVEPT)
1574 /* CF==1 or ZF==1 --> rc = -1 */
1575 "; ja 1f ; ud2 ; 1:\n"
1576 : : "a" (&operand), "c" (ext) : "cc", "memory");
1579 static struct shared_msr_entry *find_msr_entry(struct vcpu_vmx *vmx, u32 msr)
1583 i = __find_msr_index(vmx, msr);
1585 return &vmx->guest_msrs[i];
1589 static void vmcs_clear(struct vmcs *vmcs)
1591 u64 phys_addr = __pa(vmcs);
1594 asm volatile (__ex(ASM_VMX_VMCLEAR_RAX) "; setna %0"
1595 : "=qm"(error) : "a"(&phys_addr), "m"(phys_addr)
1598 printk(KERN_ERR "kvm: vmclear fail: %p/%llx\n",
1602 static inline void loaded_vmcs_init(struct loaded_vmcs *loaded_vmcs)
1604 vmcs_clear(loaded_vmcs->vmcs);
1605 if (loaded_vmcs->shadow_vmcs && loaded_vmcs->launched)
1606 vmcs_clear(loaded_vmcs->shadow_vmcs);
1607 loaded_vmcs->cpu = -1;
1608 loaded_vmcs->launched = 0;
1611 static void vmcs_load(struct vmcs *vmcs)
1613 u64 phys_addr = __pa(vmcs);
1616 asm volatile (__ex(ASM_VMX_VMPTRLD_RAX) "; setna %0"
1617 : "=qm"(error) : "a"(&phys_addr), "m"(phys_addr)
1620 printk(KERN_ERR "kvm: vmptrld %p/%llx failed\n",
1624 #ifdef CONFIG_KEXEC_CORE
1626 * This bitmap is used to indicate whether the vmclear
1627 * operation is enabled on all cpus. All disabled by
1630 static cpumask_t crash_vmclear_enabled_bitmap = CPU_MASK_NONE;
1632 static inline void crash_enable_local_vmclear(int cpu)
1634 cpumask_set_cpu(cpu, &crash_vmclear_enabled_bitmap);
1637 static inline void crash_disable_local_vmclear(int cpu)
1639 cpumask_clear_cpu(cpu, &crash_vmclear_enabled_bitmap);
1642 static inline int crash_local_vmclear_enabled(int cpu)
1644 return cpumask_test_cpu(cpu, &crash_vmclear_enabled_bitmap);
1647 static void crash_vmclear_local_loaded_vmcss(void)
1649 int cpu = raw_smp_processor_id();
1650 struct loaded_vmcs *v;
1652 if (!crash_local_vmclear_enabled(cpu))
1655 list_for_each_entry(v, &per_cpu(loaded_vmcss_on_cpu, cpu),
1656 loaded_vmcss_on_cpu_link)
1657 vmcs_clear(v->vmcs);
1660 static inline void crash_enable_local_vmclear(int cpu) { }
1661 static inline void crash_disable_local_vmclear(int cpu) { }
1662 #endif /* CONFIG_KEXEC_CORE */
1664 static void __loaded_vmcs_clear(void *arg)
1666 struct loaded_vmcs *loaded_vmcs = arg;
1667 int cpu = raw_smp_processor_id();
1669 if (loaded_vmcs->cpu != cpu)
1670 return; /* vcpu migration can race with cpu offline */
1671 if (per_cpu(current_vmcs, cpu) == loaded_vmcs->vmcs)
1672 per_cpu(current_vmcs, cpu) = NULL;
1673 crash_disable_local_vmclear(cpu);
1674 list_del(&loaded_vmcs->loaded_vmcss_on_cpu_link);
1677 * we should ensure updating loaded_vmcs->loaded_vmcss_on_cpu_link
1678 * is before setting loaded_vmcs->vcpu to -1 which is done in
1679 * loaded_vmcs_init. Otherwise, other cpu can see vcpu = -1 fist
1680 * then adds the vmcs into percpu list before it is deleted.
1684 loaded_vmcs_init(loaded_vmcs);
1685 crash_enable_local_vmclear(cpu);
1688 static void loaded_vmcs_clear(struct loaded_vmcs *loaded_vmcs)
1690 int cpu = loaded_vmcs->cpu;
1693 smp_call_function_single(cpu,
1694 __loaded_vmcs_clear, loaded_vmcs, 1);
1697 static inline void vpid_sync_vcpu_single(int vpid)
1702 if (cpu_has_vmx_invvpid_single())
1703 __invvpid(VMX_VPID_EXTENT_SINGLE_CONTEXT, vpid, 0);
1706 static inline void vpid_sync_vcpu_global(void)
1708 if (cpu_has_vmx_invvpid_global())
1709 __invvpid(VMX_VPID_EXTENT_ALL_CONTEXT, 0, 0);
1712 static inline void vpid_sync_context(int vpid)
1714 if (cpu_has_vmx_invvpid_single())
1715 vpid_sync_vcpu_single(vpid);
1717 vpid_sync_vcpu_global();
1720 static inline void ept_sync_global(void)
1722 if (cpu_has_vmx_invept_global())
1723 __invept(VMX_EPT_EXTENT_GLOBAL, 0, 0);
1726 static inline void ept_sync_context(u64 eptp)
1729 if (cpu_has_vmx_invept_context())
1730 __invept(VMX_EPT_EXTENT_CONTEXT, eptp, 0);
1736 static __always_inline void vmcs_check16(unsigned long field)
1738 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2000,
1739 "16-bit accessor invalid for 64-bit field");
1740 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2001,
1741 "16-bit accessor invalid for 64-bit high field");
1742 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x4000,
1743 "16-bit accessor invalid for 32-bit high field");
1744 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x6000,
1745 "16-bit accessor invalid for natural width field");
1748 static __always_inline void vmcs_check32(unsigned long field)
1750 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0,
1751 "32-bit accessor invalid for 16-bit field");
1752 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x6000,
1753 "32-bit accessor invalid for natural width field");
1756 static __always_inline void vmcs_check64(unsigned long field)
1758 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0,
1759 "64-bit accessor invalid for 16-bit field");
1760 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2001,
1761 "64-bit accessor invalid for 64-bit high field");
1762 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x4000,
1763 "64-bit accessor invalid for 32-bit field");
1764 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x6000,
1765 "64-bit accessor invalid for natural width field");
1768 static __always_inline void vmcs_checkl(unsigned long field)
1770 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0,
1771 "Natural width accessor invalid for 16-bit field");
1772 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2000,
1773 "Natural width accessor invalid for 64-bit field");
1774 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2001,
1775 "Natural width accessor invalid for 64-bit high field");
1776 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x4000,
1777 "Natural width accessor invalid for 32-bit field");
1780 static __always_inline unsigned long __vmcs_readl(unsigned long field)
1782 unsigned long value;
1784 asm volatile (__ex_clear(ASM_VMX_VMREAD_RDX_RAX, "%0")
1785 : "=a"(value) : "d"(field) : "cc");
1789 static __always_inline u16 vmcs_read16(unsigned long field)
1791 vmcs_check16(field);
1792 return __vmcs_readl(field);
1795 static __always_inline u32 vmcs_read32(unsigned long field)
1797 vmcs_check32(field);
1798 return __vmcs_readl(field);
1801 static __always_inline u64 vmcs_read64(unsigned long field)
1803 vmcs_check64(field);
1804 #ifdef CONFIG_X86_64
1805 return __vmcs_readl(field);
1807 return __vmcs_readl(field) | ((u64)__vmcs_readl(field+1) << 32);
1811 static __always_inline unsigned long vmcs_readl(unsigned long field)
1814 return __vmcs_readl(field);
1817 static noinline void vmwrite_error(unsigned long field, unsigned long value)
1819 printk(KERN_ERR "vmwrite error: reg %lx value %lx (err %d)\n",
1820 field, value, vmcs_read32(VM_INSTRUCTION_ERROR));
1824 static __always_inline void __vmcs_writel(unsigned long field, unsigned long value)
1828 asm volatile (__ex(ASM_VMX_VMWRITE_RAX_RDX) "; setna %0"
1829 : "=q"(error) : "a"(value), "d"(field) : "cc");
1830 if (unlikely(error))
1831 vmwrite_error(field, value);
1834 static __always_inline void vmcs_write16(unsigned long field, u16 value)
1836 vmcs_check16(field);
1837 __vmcs_writel(field, value);
1840 static __always_inline void vmcs_write32(unsigned long field, u32 value)
1842 vmcs_check32(field);
1843 __vmcs_writel(field, value);
1846 static __always_inline void vmcs_write64(unsigned long field, u64 value)
1848 vmcs_check64(field);
1849 __vmcs_writel(field, value);
1850 #ifndef CONFIG_X86_64
1852 __vmcs_writel(field+1, value >> 32);
1856 static __always_inline void vmcs_writel(unsigned long field, unsigned long value)
1859 __vmcs_writel(field, value);
1862 static __always_inline void vmcs_clear_bits(unsigned long field, u32 mask)
1864 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x2000,
1865 "vmcs_clear_bits does not support 64-bit fields");
1866 __vmcs_writel(field, __vmcs_readl(field) & ~mask);
1869 static __always_inline void vmcs_set_bits(unsigned long field, u32 mask)
1871 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x2000,
1872 "vmcs_set_bits does not support 64-bit fields");
1873 __vmcs_writel(field, __vmcs_readl(field) | mask);
1876 static inline void vm_entry_controls_reset_shadow(struct vcpu_vmx *vmx)
1878 vmx->vm_entry_controls_shadow = vmcs_read32(VM_ENTRY_CONTROLS);
1881 static inline void vm_entry_controls_init(struct vcpu_vmx *vmx, u32 val)
1883 vmcs_write32(VM_ENTRY_CONTROLS, val);
1884 vmx->vm_entry_controls_shadow = val;
1887 static inline void vm_entry_controls_set(struct vcpu_vmx *vmx, u32 val)
1889 if (vmx->vm_entry_controls_shadow != val)
1890 vm_entry_controls_init(vmx, val);
1893 static inline u32 vm_entry_controls_get(struct vcpu_vmx *vmx)
1895 return vmx->vm_entry_controls_shadow;
1899 static inline void vm_entry_controls_setbit(struct vcpu_vmx *vmx, u32 val)
1901 vm_entry_controls_set(vmx, vm_entry_controls_get(vmx) | val);
1904 static inline void vm_entry_controls_clearbit(struct vcpu_vmx *vmx, u32 val)
1906 vm_entry_controls_set(vmx, vm_entry_controls_get(vmx) & ~val);
1909 static inline void vm_exit_controls_reset_shadow(struct vcpu_vmx *vmx)
1911 vmx->vm_exit_controls_shadow = vmcs_read32(VM_EXIT_CONTROLS);
1914 static inline void vm_exit_controls_init(struct vcpu_vmx *vmx, u32 val)
1916 vmcs_write32(VM_EXIT_CONTROLS, val);
1917 vmx->vm_exit_controls_shadow = val;
1920 static inline void vm_exit_controls_set(struct vcpu_vmx *vmx, u32 val)
1922 if (vmx->vm_exit_controls_shadow != val)
1923 vm_exit_controls_init(vmx, val);
1926 static inline u32 vm_exit_controls_get(struct vcpu_vmx *vmx)
1928 return vmx->vm_exit_controls_shadow;
1932 static inline void vm_exit_controls_setbit(struct vcpu_vmx *vmx, u32 val)
1934 vm_exit_controls_set(vmx, vm_exit_controls_get(vmx) | val);
1937 static inline void vm_exit_controls_clearbit(struct vcpu_vmx *vmx, u32 val)
1939 vm_exit_controls_set(vmx, vm_exit_controls_get(vmx) & ~val);
1942 static void vmx_segment_cache_clear(struct vcpu_vmx *vmx)
1944 vmx->segment_cache.bitmask = 0;
1947 static bool vmx_segment_cache_test_set(struct vcpu_vmx *vmx, unsigned seg,
1951 u32 mask = 1 << (seg * SEG_FIELD_NR + field);
1953 if (!(vmx->vcpu.arch.regs_avail & (1 << VCPU_EXREG_SEGMENTS))) {
1954 vmx->vcpu.arch.regs_avail |= (1 << VCPU_EXREG_SEGMENTS);
1955 vmx->segment_cache.bitmask = 0;
1957 ret = vmx->segment_cache.bitmask & mask;
1958 vmx->segment_cache.bitmask |= mask;
1962 static u16 vmx_read_guest_seg_selector(struct vcpu_vmx *vmx, unsigned seg)
1964 u16 *p = &vmx->segment_cache.seg[seg].selector;
1966 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_SEL))
1967 *p = vmcs_read16(kvm_vmx_segment_fields[seg].selector);
1971 static ulong vmx_read_guest_seg_base(struct vcpu_vmx *vmx, unsigned seg)
1973 ulong *p = &vmx->segment_cache.seg[seg].base;
1975 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_BASE))
1976 *p = vmcs_readl(kvm_vmx_segment_fields[seg].base);
1980 static u32 vmx_read_guest_seg_limit(struct vcpu_vmx *vmx, unsigned seg)
1982 u32 *p = &vmx->segment_cache.seg[seg].limit;
1984 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_LIMIT))
1985 *p = vmcs_read32(kvm_vmx_segment_fields[seg].limit);
1989 static u32 vmx_read_guest_seg_ar(struct vcpu_vmx *vmx, unsigned seg)
1991 u32 *p = &vmx->segment_cache.seg[seg].ar;
1993 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_AR))
1994 *p = vmcs_read32(kvm_vmx_segment_fields[seg].ar_bytes);
1998 static void update_exception_bitmap(struct kvm_vcpu *vcpu)
2002 eb = (1u << PF_VECTOR) | (1u << UD_VECTOR) | (1u << MC_VECTOR) |
2003 (1u << DB_VECTOR) | (1u << AC_VECTOR);
2004 if ((vcpu->guest_debug &
2005 (KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_SW_BP)) ==
2006 (KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_SW_BP))
2007 eb |= 1u << BP_VECTOR;
2008 if (to_vmx(vcpu)->rmode.vm86_active)
2011 eb &= ~(1u << PF_VECTOR); /* bypass_guest_pf = 0 */
2013 /* When we are running a nested L2 guest and L1 specified for it a
2014 * certain exception bitmap, we must trap the same exceptions and pass
2015 * them to L1. When running L2, we will only handle the exceptions
2016 * specified above if L1 did not want them.
2018 if (is_guest_mode(vcpu))
2019 eb |= get_vmcs12(vcpu)->exception_bitmap;
2021 vmcs_write32(EXCEPTION_BITMAP, eb);
2025 * Check if MSR is intercepted for currently loaded MSR bitmap.
2027 static bool msr_write_intercepted(struct kvm_vcpu *vcpu, u32 msr)
2029 unsigned long *msr_bitmap;
2030 int f = sizeof(unsigned long);
2032 if (!cpu_has_vmx_msr_bitmap())
2035 msr_bitmap = to_vmx(vcpu)->loaded_vmcs->msr_bitmap;
2037 if (msr <= 0x1fff) {
2038 return !!test_bit(msr, msr_bitmap + 0x800 / f);
2039 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
2041 return !!test_bit(msr, msr_bitmap + 0xc00 / f);
2048 * Check if MSR is intercepted for L01 MSR bitmap.
2050 static bool msr_write_intercepted_l01(struct kvm_vcpu *vcpu, u32 msr)
2052 unsigned long *msr_bitmap;
2053 int f = sizeof(unsigned long);
2055 if (!cpu_has_vmx_msr_bitmap())
2058 msr_bitmap = to_vmx(vcpu)->vmcs01.msr_bitmap;
2060 if (msr <= 0x1fff) {
2061 return !!test_bit(msr, msr_bitmap + 0x800 / f);
2062 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
2064 return !!test_bit(msr, msr_bitmap + 0xc00 / f);
2070 static void clear_atomic_switch_msr_special(struct vcpu_vmx *vmx,
2071 unsigned long entry, unsigned long exit)
2073 vm_entry_controls_clearbit(vmx, entry);
2074 vm_exit_controls_clearbit(vmx, exit);
2077 static int find_msr(struct vmx_msrs *m, unsigned int msr)
2081 for (i = 0; i < m->nr; ++i) {
2082 if (m->val[i].index == msr)
2088 static void clear_atomic_switch_msr(struct vcpu_vmx *vmx, unsigned msr)
2091 struct msr_autoload *m = &vmx->msr_autoload;
2095 if (cpu_has_load_ia32_efer) {
2096 clear_atomic_switch_msr_special(vmx,
2097 VM_ENTRY_LOAD_IA32_EFER,
2098 VM_EXIT_LOAD_IA32_EFER);
2102 case MSR_CORE_PERF_GLOBAL_CTRL:
2103 if (cpu_has_load_perf_global_ctrl) {
2104 clear_atomic_switch_msr_special(vmx,
2105 VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL,
2106 VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL);
2111 i = find_msr(&m->guest, msr);
2115 m->guest.val[i] = m->guest.val[m->guest.nr];
2116 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, m->guest.nr);
2119 i = find_msr(&m->host, msr);
2124 m->host.val[i] = m->host.val[m->host.nr];
2125 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, m->host.nr);
2128 static void add_atomic_switch_msr_special(struct vcpu_vmx *vmx,
2129 unsigned long entry, unsigned long exit,
2130 unsigned long guest_val_vmcs, unsigned long host_val_vmcs,
2131 u64 guest_val, u64 host_val)
2133 vmcs_write64(guest_val_vmcs, guest_val);
2134 vmcs_write64(host_val_vmcs, host_val);
2135 vm_entry_controls_setbit(vmx, entry);
2136 vm_exit_controls_setbit(vmx, exit);
2139 static void add_atomic_switch_msr(struct vcpu_vmx *vmx, unsigned msr,
2140 u64 guest_val, u64 host_val, bool entry_only)
2143 struct msr_autoload *m = &vmx->msr_autoload;
2147 if (cpu_has_load_ia32_efer) {
2148 add_atomic_switch_msr_special(vmx,
2149 VM_ENTRY_LOAD_IA32_EFER,
2150 VM_EXIT_LOAD_IA32_EFER,
2153 guest_val, host_val);
2157 case MSR_CORE_PERF_GLOBAL_CTRL:
2158 if (cpu_has_load_perf_global_ctrl) {
2159 add_atomic_switch_msr_special(vmx,
2160 VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL,
2161 VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL,
2162 GUEST_IA32_PERF_GLOBAL_CTRL,
2163 HOST_IA32_PERF_GLOBAL_CTRL,
2164 guest_val, host_val);
2168 case MSR_IA32_PEBS_ENABLE:
2169 /* PEBS needs a quiescent period after being disabled (to write
2170 * a record). Disabling PEBS through VMX MSR swapping doesn't
2171 * provide that period, so a CPU could write host's record into
2174 wrmsrl(MSR_IA32_PEBS_ENABLE, 0);
2177 i = find_msr(&m->guest, msr);
2179 j = find_msr(&m->host, msr);
2181 if (i == NR_AUTOLOAD_MSRS || j == NR_AUTOLOAD_MSRS) {
2182 printk_once(KERN_WARNING "Not enough msr switch entries. "
2183 "Can't add msr %x\n", msr);
2188 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, m->guest.nr);
2190 m->guest.val[i].index = msr;
2191 m->guest.val[i].value = guest_val;
2198 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, m->host.nr);
2200 m->host.val[j].index = msr;
2201 m->host.val[j].value = host_val;
2204 static bool update_transition_efer(struct vcpu_vmx *vmx, int efer_offset)
2206 u64 guest_efer = vmx->vcpu.arch.efer;
2207 u64 ignore_bits = 0;
2211 * NX is needed to handle CR0.WP=1, CR4.SMEP=1. Testing
2212 * host CPUID is more efficient than testing guest CPUID
2213 * or CR4. Host SMEP is anyway a requirement for guest SMEP.
2215 if (boot_cpu_has(X86_FEATURE_SMEP))
2216 guest_efer |= EFER_NX;
2217 else if (!(guest_efer & EFER_NX))
2218 ignore_bits |= EFER_NX;
2222 * LMA and LME handled by hardware; SCE meaningless outside long mode.
2224 ignore_bits |= EFER_SCE;
2225 #ifdef CONFIG_X86_64
2226 ignore_bits |= EFER_LMA | EFER_LME;
2227 /* SCE is meaningful only in long mode on Intel */
2228 if (guest_efer & EFER_LMA)
2229 ignore_bits &= ~(u64)EFER_SCE;
2232 clear_atomic_switch_msr(vmx, MSR_EFER);
2235 * On EPT, we can't emulate NX, so we must switch EFER atomically.
2236 * On CPUs that support "load IA32_EFER", always switch EFER
2237 * atomically, since it's faster than switching it manually.
2239 if (cpu_has_load_ia32_efer ||
2240 (enable_ept && ((vmx->vcpu.arch.efer ^ host_efer) & EFER_NX))) {
2241 if (!(guest_efer & EFER_LMA))
2242 guest_efer &= ~EFER_LME;
2243 if (guest_efer != host_efer)
2244 add_atomic_switch_msr(vmx, MSR_EFER,
2245 guest_efer, host_efer, false);
2248 guest_efer &= ~ignore_bits;
2249 guest_efer |= host_efer & ignore_bits;
2251 vmx->guest_msrs[efer_offset].data = guest_efer;
2252 vmx->guest_msrs[efer_offset].mask = ~ignore_bits;
2258 #ifdef CONFIG_X86_32
2260 * On 32-bit kernels, VM exits still load the FS and GS bases from the
2261 * VMCS rather than the segment table. KVM uses this helper to figure
2262 * out the current bases to poke them into the VMCS before entry.
2264 static unsigned long segment_base(u16 selector)
2266 struct desc_struct *table;
2269 if (!(selector & ~SEGMENT_RPL_MASK))
2272 table = get_current_gdt_ro();
2274 if ((selector & SEGMENT_TI_MASK) == SEGMENT_LDT) {
2275 u16 ldt_selector = kvm_read_ldt();
2277 if (!(ldt_selector & ~SEGMENT_RPL_MASK))
2280 table = (struct desc_struct *)segment_base(ldt_selector);
2282 v = get_desc_base(&table[selector >> 3]);
2287 static void vmx_save_host_state(struct kvm_vcpu *vcpu)
2289 struct vcpu_vmx *vmx = to_vmx(vcpu);
2292 if (vmx->host_state.loaded)
2295 vmx->host_state.loaded = 1;
2297 * Set host fs and gs selectors. Unfortunately, 22.2.3 does not
2298 * allow segment selectors with cpl > 0 or ti == 1.
2300 vmx->host_state.ldt_sel = kvm_read_ldt();
2301 vmx->host_state.gs_ldt_reload_needed = vmx->host_state.ldt_sel;
2302 savesegment(fs, vmx->host_state.fs_sel);
2303 if (!(vmx->host_state.fs_sel & 7)) {
2304 vmcs_write16(HOST_FS_SELECTOR, vmx->host_state.fs_sel);
2305 vmx->host_state.fs_reload_needed = 0;
2307 vmcs_write16(HOST_FS_SELECTOR, 0);
2308 vmx->host_state.fs_reload_needed = 1;
2310 savesegment(gs, vmx->host_state.gs_sel);
2311 if (!(vmx->host_state.gs_sel & 7))
2312 vmcs_write16(HOST_GS_SELECTOR, vmx->host_state.gs_sel);
2314 vmcs_write16(HOST_GS_SELECTOR, 0);
2315 vmx->host_state.gs_ldt_reload_needed = 1;
2318 #ifdef CONFIG_X86_64
2319 savesegment(ds, vmx->host_state.ds_sel);
2320 savesegment(es, vmx->host_state.es_sel);
2323 #ifdef CONFIG_X86_64
2324 vmcs_writel(HOST_FS_BASE, read_msr(MSR_FS_BASE));
2325 vmcs_writel(HOST_GS_BASE, read_msr(MSR_GS_BASE));
2327 vmcs_writel(HOST_FS_BASE, segment_base(vmx->host_state.fs_sel));
2328 vmcs_writel(HOST_GS_BASE, segment_base(vmx->host_state.gs_sel));
2331 #ifdef CONFIG_X86_64
2332 rdmsrl(MSR_KERNEL_GS_BASE, vmx->msr_host_kernel_gs_base);
2333 if (is_long_mode(&vmx->vcpu))
2334 wrmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base);
2336 if (boot_cpu_has(X86_FEATURE_MPX))
2337 rdmsrl(MSR_IA32_BNDCFGS, vmx->host_state.msr_host_bndcfgs);
2338 for (i = 0; i < vmx->save_nmsrs; ++i)
2339 kvm_set_shared_msr(vmx->guest_msrs[i].index,
2340 vmx->guest_msrs[i].data,
2341 vmx->guest_msrs[i].mask);
2344 static void __vmx_load_host_state(struct vcpu_vmx *vmx)
2346 if (!vmx->host_state.loaded)
2349 ++vmx->vcpu.stat.host_state_reload;
2350 vmx->host_state.loaded = 0;
2351 #ifdef CONFIG_X86_64
2352 if (is_long_mode(&vmx->vcpu))
2353 rdmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base);
2355 if (vmx->host_state.gs_ldt_reload_needed) {
2356 kvm_load_ldt(vmx->host_state.ldt_sel);
2357 #ifdef CONFIG_X86_64
2358 load_gs_index(vmx->host_state.gs_sel);
2360 loadsegment(gs, vmx->host_state.gs_sel);
2363 if (vmx->host_state.fs_reload_needed)
2364 loadsegment(fs, vmx->host_state.fs_sel);
2365 #ifdef CONFIG_X86_64
2366 if (unlikely(vmx->host_state.ds_sel | vmx->host_state.es_sel)) {
2367 loadsegment(ds, vmx->host_state.ds_sel);
2368 loadsegment(es, vmx->host_state.es_sel);
2371 invalidate_tss_limit();
2372 #ifdef CONFIG_X86_64
2373 wrmsrl(MSR_KERNEL_GS_BASE, vmx->msr_host_kernel_gs_base);
2375 if (vmx->host_state.msr_host_bndcfgs)
2376 wrmsrl(MSR_IA32_BNDCFGS, vmx->host_state.msr_host_bndcfgs);
2377 load_fixmap_gdt(raw_smp_processor_id());
2380 static void vmx_load_host_state(struct vcpu_vmx *vmx)
2383 __vmx_load_host_state(vmx);
2387 static void vmx_vcpu_pi_load(struct kvm_vcpu *vcpu, int cpu)
2389 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
2390 struct pi_desc old, new;
2394 * In case of hot-plug or hot-unplug, we may have to undo
2395 * vmx_vcpu_pi_put even if there is no assigned device. And we
2396 * always keep PI.NDST up to date for simplicity: it makes the
2397 * code easier, and CPU migration is not a fast path.
2399 if (!pi_test_sn(pi_desc) && vcpu->cpu == cpu)
2403 * First handle the simple case where no cmpxchg is necessary; just
2404 * allow posting non-urgent interrupts.
2406 * If the 'nv' field is POSTED_INTR_WAKEUP_VECTOR, do not change
2407 * PI.NDST: pi_post_block will do it for us and the wakeup_handler
2408 * expects the VCPU to be on the blocked_vcpu_list that matches
2411 if (pi_desc->nv == POSTED_INTR_WAKEUP_VECTOR ||
2413 pi_clear_sn(pi_desc);
2417 /* The full case. */
2419 old.control = new.control = pi_desc->control;
2421 dest = cpu_physical_id(cpu);
2423 if (x2apic_enabled())
2426 new.ndst = (dest << 8) & 0xFF00;
2429 } while (cmpxchg64(&pi_desc->control, old.control,
2430 new.control) != old.control);
2433 static void decache_tsc_multiplier(struct vcpu_vmx *vmx)
2435 vmx->current_tsc_ratio = vmx->vcpu.arch.tsc_scaling_ratio;
2436 vmcs_write64(TSC_MULTIPLIER, vmx->current_tsc_ratio);
2440 * Switches to specified vcpu, until a matching vcpu_put(), but assumes
2441 * vcpu mutex is already taken.
2443 static void vmx_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
2445 struct vcpu_vmx *vmx = to_vmx(vcpu);
2446 bool already_loaded = vmx->loaded_vmcs->cpu == cpu;
2448 if (!already_loaded) {
2449 loaded_vmcs_clear(vmx->loaded_vmcs);
2450 local_irq_disable();
2451 crash_disable_local_vmclear(cpu);
2454 * Read loaded_vmcs->cpu should be before fetching
2455 * loaded_vmcs->loaded_vmcss_on_cpu_link.
2456 * See the comments in __loaded_vmcs_clear().
2460 list_add(&vmx->loaded_vmcs->loaded_vmcss_on_cpu_link,
2461 &per_cpu(loaded_vmcss_on_cpu, cpu));
2462 crash_enable_local_vmclear(cpu);
2466 if (per_cpu(current_vmcs, cpu) != vmx->loaded_vmcs->vmcs) {
2467 per_cpu(current_vmcs, cpu) = vmx->loaded_vmcs->vmcs;
2468 vmcs_load(vmx->loaded_vmcs->vmcs);
2469 indirect_branch_prediction_barrier();
2472 if (!already_loaded) {
2473 void *gdt = get_current_gdt_ro();
2474 unsigned long sysenter_esp;
2476 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
2479 * Linux uses per-cpu TSS and GDT, so set these when switching
2480 * processors. See 22.2.4.
2482 vmcs_writel(HOST_TR_BASE,
2483 (unsigned long)&get_cpu_entry_area(cpu)->tss.x86_tss);
2484 vmcs_writel(HOST_GDTR_BASE, (unsigned long)gdt); /* 22.2.4 */
2487 * VM exits change the host TR limit to 0x67 after a VM
2488 * exit. This is okay, since 0x67 covers everything except
2489 * the IO bitmap and have have code to handle the IO bitmap
2490 * being lost after a VM exit.
2492 BUILD_BUG_ON(IO_BITMAP_OFFSET - 1 != 0x67);
2494 rdmsrl(MSR_IA32_SYSENTER_ESP, sysenter_esp);
2495 vmcs_writel(HOST_IA32_SYSENTER_ESP, sysenter_esp); /* 22.2.3 */
2497 vmx->loaded_vmcs->cpu = cpu;
2500 /* Setup TSC multiplier */
2501 if (kvm_has_tsc_control &&
2502 vmx->current_tsc_ratio != vcpu->arch.tsc_scaling_ratio)
2503 decache_tsc_multiplier(vmx);
2505 vmx_vcpu_pi_load(vcpu, cpu);
2506 vmx->host_pkru = read_pkru();
2509 static void vmx_vcpu_pi_put(struct kvm_vcpu *vcpu)
2511 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
2513 if (!kvm_arch_has_assigned_device(vcpu->kvm) ||
2514 !irq_remapping_cap(IRQ_POSTING_CAP) ||
2515 !kvm_vcpu_apicv_active(vcpu))
2518 /* Set SN when the vCPU is preempted */
2519 if (vcpu->preempted)
2523 static void vmx_vcpu_put(struct kvm_vcpu *vcpu)
2525 vmx_vcpu_pi_put(vcpu);
2527 __vmx_load_host_state(to_vmx(vcpu));
2530 static bool emulation_required(struct kvm_vcpu *vcpu)
2532 return emulate_invalid_guest_state && !guest_state_valid(vcpu);
2535 static void vmx_decache_cr0_guest_bits(struct kvm_vcpu *vcpu);
2538 * Return the cr0 value that a nested guest would read. This is a combination
2539 * of the real cr0 used to run the guest (guest_cr0), and the bits shadowed by
2540 * its hypervisor (cr0_read_shadow).
2542 static inline unsigned long nested_read_cr0(struct vmcs12 *fields)
2544 return (fields->guest_cr0 & ~fields->cr0_guest_host_mask) |
2545 (fields->cr0_read_shadow & fields->cr0_guest_host_mask);
2547 static inline unsigned long nested_read_cr4(struct vmcs12 *fields)
2549 return (fields->guest_cr4 & ~fields->cr4_guest_host_mask) |
2550 (fields->cr4_read_shadow & fields->cr4_guest_host_mask);
2553 static unsigned long vmx_get_rflags(struct kvm_vcpu *vcpu)
2555 unsigned long rflags, save_rflags;
2557 if (!test_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail)) {
2558 __set_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail);
2559 rflags = vmcs_readl(GUEST_RFLAGS);
2560 if (to_vmx(vcpu)->rmode.vm86_active) {
2561 rflags &= RMODE_GUEST_OWNED_EFLAGS_BITS;
2562 save_rflags = to_vmx(vcpu)->rmode.save_rflags;
2563 rflags |= save_rflags & ~RMODE_GUEST_OWNED_EFLAGS_BITS;
2565 to_vmx(vcpu)->rflags = rflags;
2567 return to_vmx(vcpu)->rflags;
2570 static void vmx_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags)
2572 unsigned long old_rflags = vmx_get_rflags(vcpu);
2574 __set_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail);
2575 to_vmx(vcpu)->rflags = rflags;
2576 if (to_vmx(vcpu)->rmode.vm86_active) {
2577 to_vmx(vcpu)->rmode.save_rflags = rflags;
2578 rflags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM;
2580 vmcs_writel(GUEST_RFLAGS, rflags);
2582 if ((old_rflags ^ to_vmx(vcpu)->rflags) & X86_EFLAGS_VM)
2583 to_vmx(vcpu)->emulation_required = emulation_required(vcpu);
2586 static u32 vmx_get_interrupt_shadow(struct kvm_vcpu *vcpu)
2588 u32 interruptibility = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
2591 if (interruptibility & GUEST_INTR_STATE_STI)
2592 ret |= KVM_X86_SHADOW_INT_STI;
2593 if (interruptibility & GUEST_INTR_STATE_MOV_SS)
2594 ret |= KVM_X86_SHADOW_INT_MOV_SS;
2599 static void vmx_set_interrupt_shadow(struct kvm_vcpu *vcpu, int mask)
2601 u32 interruptibility_old = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
2602 u32 interruptibility = interruptibility_old;
2604 interruptibility &= ~(GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS);
2606 if (mask & KVM_X86_SHADOW_INT_MOV_SS)
2607 interruptibility |= GUEST_INTR_STATE_MOV_SS;
2608 else if (mask & KVM_X86_SHADOW_INT_STI)
2609 interruptibility |= GUEST_INTR_STATE_STI;
2611 if ((interruptibility != interruptibility_old))
2612 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, interruptibility);
2615 static void skip_emulated_instruction(struct kvm_vcpu *vcpu)
2619 rip = kvm_rip_read(vcpu);
2620 rip += vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
2621 kvm_rip_write(vcpu, rip);
2623 /* skipping an emulated instruction also counts */
2624 vmx_set_interrupt_shadow(vcpu, 0);
2627 static void nested_vmx_inject_exception_vmexit(struct kvm_vcpu *vcpu,
2628 unsigned long exit_qual)
2630 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
2631 unsigned int nr = vcpu->arch.exception.nr;
2632 u32 intr_info = nr | INTR_INFO_VALID_MASK;
2634 if (vcpu->arch.exception.has_error_code) {
2635 vmcs12->vm_exit_intr_error_code = vcpu->arch.exception.error_code;
2636 intr_info |= INTR_INFO_DELIVER_CODE_MASK;
2639 if (kvm_exception_is_soft(nr))
2640 intr_info |= INTR_TYPE_SOFT_EXCEPTION;
2642 intr_info |= INTR_TYPE_HARD_EXCEPTION;
2644 if (!(vmcs12->idt_vectoring_info_field & VECTORING_INFO_VALID_MASK) &&
2645 vmx_get_nmi_mask(vcpu))
2646 intr_info |= INTR_INFO_UNBLOCK_NMI;
2648 nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI, intr_info, exit_qual);
2652 * KVM wants to inject page-faults which it got to the guest. This function
2653 * checks whether in a nested guest, we need to inject them to L1 or L2.
2655 static int nested_vmx_check_exception(struct kvm_vcpu *vcpu, unsigned long *exit_qual)
2657 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
2658 unsigned int nr = vcpu->arch.exception.nr;
2660 if (nr == PF_VECTOR) {
2661 if (vcpu->arch.exception.nested_apf) {
2662 *exit_qual = vcpu->arch.apf.nested_apf_token;
2666 * FIXME: we must not write CR2 when L1 intercepts an L2 #PF exception.
2667 * The fix is to add the ancillary datum (CR2 or DR6) to structs
2668 * kvm_queued_exception and kvm_vcpu_events, so that CR2 and DR6
2669 * can be written only when inject_pending_event runs. This should be
2670 * conditional on a new capability---if the capability is disabled,
2671 * kvm_multiple_exception would write the ancillary information to
2672 * CR2 or DR6, for backwards ABI-compatibility.
2674 if (nested_vmx_is_page_fault_vmexit(vmcs12,
2675 vcpu->arch.exception.error_code)) {
2676 *exit_qual = vcpu->arch.cr2;
2680 if (vmcs12->exception_bitmap & (1u << nr)) {
2681 if (nr == DB_VECTOR)
2682 *exit_qual = vcpu->arch.dr6;
2692 static void vmx_queue_exception(struct kvm_vcpu *vcpu)
2694 struct vcpu_vmx *vmx = to_vmx(vcpu);
2695 unsigned nr = vcpu->arch.exception.nr;
2696 bool has_error_code = vcpu->arch.exception.has_error_code;
2697 u32 error_code = vcpu->arch.exception.error_code;
2698 u32 intr_info = nr | INTR_INFO_VALID_MASK;
2700 if (has_error_code) {
2701 vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE, error_code);
2702 intr_info |= INTR_INFO_DELIVER_CODE_MASK;
2705 if (vmx->rmode.vm86_active) {
2707 if (kvm_exception_is_soft(nr))
2708 inc_eip = vcpu->arch.event_exit_inst_len;
2709 if (kvm_inject_realmode_interrupt(vcpu, nr, inc_eip) != EMULATE_DONE)
2710 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
2714 WARN_ON_ONCE(vmx->emulation_required);
2716 if (kvm_exception_is_soft(nr)) {
2717 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
2718 vmx->vcpu.arch.event_exit_inst_len);
2719 intr_info |= INTR_TYPE_SOFT_EXCEPTION;
2721 intr_info |= INTR_TYPE_HARD_EXCEPTION;
2723 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr_info);
2726 static bool vmx_rdtscp_supported(void)
2728 return cpu_has_vmx_rdtscp();
2731 static bool vmx_invpcid_supported(void)
2733 return cpu_has_vmx_invpcid() && enable_ept;
2737 * Swap MSR entry in host/guest MSR entry array.
2739 static void move_msr_up(struct vcpu_vmx *vmx, int from, int to)
2741 struct shared_msr_entry tmp;
2743 tmp = vmx->guest_msrs[to];
2744 vmx->guest_msrs[to] = vmx->guest_msrs[from];
2745 vmx->guest_msrs[from] = tmp;
2749 * Set up the vmcs to automatically save and restore system
2750 * msrs. Don't touch the 64-bit msrs if the guest is in legacy
2751 * mode, as fiddling with msrs is very expensive.
2753 static void setup_msrs(struct vcpu_vmx *vmx)
2755 int save_nmsrs, index;
2758 #ifdef CONFIG_X86_64
2759 if (is_long_mode(&vmx->vcpu)) {
2760 index = __find_msr_index(vmx, MSR_SYSCALL_MASK);
2762 move_msr_up(vmx, index, save_nmsrs++);
2763 index = __find_msr_index(vmx, MSR_LSTAR);
2765 move_msr_up(vmx, index, save_nmsrs++);
2766 index = __find_msr_index(vmx, MSR_CSTAR);
2768 move_msr_up(vmx, index, save_nmsrs++);
2769 index = __find_msr_index(vmx, MSR_TSC_AUX);
2770 if (index >= 0 && guest_cpuid_has(&vmx->vcpu, X86_FEATURE_RDTSCP))
2771 move_msr_up(vmx, index, save_nmsrs++);
2773 * MSR_STAR is only needed on long mode guests, and only
2774 * if efer.sce is enabled.
2776 index = __find_msr_index(vmx, MSR_STAR);
2777 if ((index >= 0) && (vmx->vcpu.arch.efer & EFER_SCE))
2778 move_msr_up(vmx, index, save_nmsrs++);
2781 index = __find_msr_index(vmx, MSR_EFER);
2782 if (index >= 0 && update_transition_efer(vmx, index))
2783 move_msr_up(vmx, index, save_nmsrs++);
2785 vmx->save_nmsrs = save_nmsrs;
2787 if (cpu_has_vmx_msr_bitmap())
2788 vmx_update_msr_bitmap(&vmx->vcpu);
2792 * reads and returns guest's timestamp counter "register"
2793 * guest_tsc = (host_tsc * tsc multiplier) >> 48 + tsc_offset
2794 * -- Intel TSC Scaling for Virtualization White Paper, sec 1.3
2796 static u64 guest_read_tsc(struct kvm_vcpu *vcpu)
2798 u64 host_tsc, tsc_offset;
2801 tsc_offset = vmcs_read64(TSC_OFFSET);
2802 return kvm_scale_tsc(vcpu, host_tsc) + tsc_offset;
2806 * writes 'offset' into guest's timestamp counter offset register
2808 static void vmx_write_tsc_offset(struct kvm_vcpu *vcpu, u64 offset)
2810 if (is_guest_mode(vcpu)) {
2812 * We're here if L1 chose not to trap WRMSR to TSC. According
2813 * to the spec, this should set L1's TSC; The offset that L1
2814 * set for L2 remains unchanged, and still needs to be added
2815 * to the newly set TSC to get L2's TSC.
2817 struct vmcs12 *vmcs12;
2818 /* recalculate vmcs02.TSC_OFFSET: */
2819 vmcs12 = get_vmcs12(vcpu);
2820 vmcs_write64(TSC_OFFSET, offset +
2821 (nested_cpu_has(vmcs12, CPU_BASED_USE_TSC_OFFSETING) ?
2822 vmcs12->tsc_offset : 0));
2824 trace_kvm_write_tsc_offset(vcpu->vcpu_id,
2825 vmcs_read64(TSC_OFFSET), offset);
2826 vmcs_write64(TSC_OFFSET, offset);
2831 * nested_vmx_allowed() checks whether a guest should be allowed to use VMX
2832 * instructions and MSRs (i.e., nested VMX). Nested VMX is disabled for
2833 * all guests if the "nested" module option is off, and can also be disabled
2834 * for a single guest by disabling its VMX cpuid bit.
2836 static inline bool nested_vmx_allowed(struct kvm_vcpu *vcpu)
2838 return nested && guest_cpuid_has(vcpu, X86_FEATURE_VMX);
2842 * nested_vmx_setup_ctls_msrs() sets up variables containing the values to be
2843 * returned for the various VMX controls MSRs when nested VMX is enabled.
2844 * The same values should also be used to verify that vmcs12 control fields are
2845 * valid during nested entry from L1 to L2.
2846 * Each of these control msrs has a low and high 32-bit half: A low bit is on
2847 * if the corresponding bit in the (32-bit) control field *must* be on, and a
2848 * bit in the high half is on if the corresponding bit in the control field
2849 * may be on. See also vmx_control_verify().
2851 static void nested_vmx_setup_ctls_msrs(struct vcpu_vmx *vmx)
2854 * Note that as a general rule, the high half of the MSRs (bits in
2855 * the control fields which may be 1) should be initialized by the
2856 * intersection of the underlying hardware's MSR (i.e., features which
2857 * can be supported) and the list of features we want to expose -
2858 * because they are known to be properly supported in our code.
2859 * Also, usually, the low half of the MSRs (bits which must be 1) can
2860 * be set to 0, meaning that L1 may turn off any of these bits. The
2861 * reason is that if one of these bits is necessary, it will appear
2862 * in vmcs01 and prepare_vmcs02, when it bitwise-or's the control
2863 * fields of vmcs01 and vmcs02, will turn these bits off - and
2864 * nested_vmx_exit_reflected() will not pass related exits to L1.
2865 * These rules have exceptions below.
2868 /* pin-based controls */
2869 rdmsr(MSR_IA32_VMX_PINBASED_CTLS,
2870 vmx->nested.nested_vmx_pinbased_ctls_low,
2871 vmx->nested.nested_vmx_pinbased_ctls_high);
2872 vmx->nested.nested_vmx_pinbased_ctls_low |=
2873 PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
2874 vmx->nested.nested_vmx_pinbased_ctls_high &=
2875 PIN_BASED_EXT_INTR_MASK |
2876 PIN_BASED_NMI_EXITING |
2877 PIN_BASED_VIRTUAL_NMIS;
2878 vmx->nested.nested_vmx_pinbased_ctls_high |=
2879 PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR |
2880 PIN_BASED_VMX_PREEMPTION_TIMER;
2881 if (kvm_vcpu_apicv_active(&vmx->vcpu))
2882 vmx->nested.nested_vmx_pinbased_ctls_high |=
2883 PIN_BASED_POSTED_INTR;
2886 rdmsr(MSR_IA32_VMX_EXIT_CTLS,
2887 vmx->nested.nested_vmx_exit_ctls_low,
2888 vmx->nested.nested_vmx_exit_ctls_high);
2889 vmx->nested.nested_vmx_exit_ctls_low =
2890 VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR;
2892 vmx->nested.nested_vmx_exit_ctls_high &=
2893 #ifdef CONFIG_X86_64
2894 VM_EXIT_HOST_ADDR_SPACE_SIZE |
2896 VM_EXIT_LOAD_IA32_PAT | VM_EXIT_SAVE_IA32_PAT;
2897 vmx->nested.nested_vmx_exit_ctls_high |=
2898 VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR |
2899 VM_EXIT_LOAD_IA32_EFER | VM_EXIT_SAVE_IA32_EFER |
2900 VM_EXIT_SAVE_VMX_PREEMPTION_TIMER | VM_EXIT_ACK_INTR_ON_EXIT;
2902 if (kvm_mpx_supported())
2903 vmx->nested.nested_vmx_exit_ctls_high |= VM_EXIT_CLEAR_BNDCFGS;
2905 /* We support free control of debug control saving. */
2906 vmx->nested.nested_vmx_exit_ctls_low &= ~VM_EXIT_SAVE_DEBUG_CONTROLS;
2908 /* entry controls */
2909 rdmsr(MSR_IA32_VMX_ENTRY_CTLS,
2910 vmx->nested.nested_vmx_entry_ctls_low,
2911 vmx->nested.nested_vmx_entry_ctls_high);
2912 vmx->nested.nested_vmx_entry_ctls_low =
2913 VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR;
2914 vmx->nested.nested_vmx_entry_ctls_high &=
2915 #ifdef CONFIG_X86_64
2916 VM_ENTRY_IA32E_MODE |
2918 VM_ENTRY_LOAD_IA32_PAT;
2919 vmx->nested.nested_vmx_entry_ctls_high |=
2920 (VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR | VM_ENTRY_LOAD_IA32_EFER);
2921 if (kvm_mpx_supported())
2922 vmx->nested.nested_vmx_entry_ctls_high |= VM_ENTRY_LOAD_BNDCFGS;
2924 /* We support free control of debug control loading. */
2925 vmx->nested.nested_vmx_entry_ctls_low &= ~VM_ENTRY_LOAD_DEBUG_CONTROLS;
2927 /* cpu-based controls */
2928 rdmsr(MSR_IA32_VMX_PROCBASED_CTLS,
2929 vmx->nested.nested_vmx_procbased_ctls_low,
2930 vmx->nested.nested_vmx_procbased_ctls_high);
2931 vmx->nested.nested_vmx_procbased_ctls_low =
2932 CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
2933 vmx->nested.nested_vmx_procbased_ctls_high &=
2934 CPU_BASED_VIRTUAL_INTR_PENDING |
2935 CPU_BASED_VIRTUAL_NMI_PENDING | CPU_BASED_USE_TSC_OFFSETING |
2936 CPU_BASED_HLT_EXITING | CPU_BASED_INVLPG_EXITING |
2937 CPU_BASED_MWAIT_EXITING | CPU_BASED_CR3_LOAD_EXITING |
2938 CPU_BASED_CR3_STORE_EXITING |
2939 #ifdef CONFIG_X86_64
2940 CPU_BASED_CR8_LOAD_EXITING | CPU_BASED_CR8_STORE_EXITING |
2942 CPU_BASED_MOV_DR_EXITING | CPU_BASED_UNCOND_IO_EXITING |
2943 CPU_BASED_USE_IO_BITMAPS | CPU_BASED_MONITOR_TRAP_FLAG |
2944 CPU_BASED_MONITOR_EXITING | CPU_BASED_RDPMC_EXITING |
2945 CPU_BASED_RDTSC_EXITING | CPU_BASED_PAUSE_EXITING |
2946 CPU_BASED_TPR_SHADOW | CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
2948 * We can allow some features even when not supported by the
2949 * hardware. For example, L1 can specify an MSR bitmap - and we
2950 * can use it to avoid exits to L1 - even when L0 runs L2
2951 * without MSR bitmaps.
2953 vmx->nested.nested_vmx_procbased_ctls_high |=
2954 CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR |
2955 CPU_BASED_USE_MSR_BITMAPS;
2957 /* We support free control of CR3 access interception. */
2958 vmx->nested.nested_vmx_procbased_ctls_low &=
2959 ~(CPU_BASED_CR3_LOAD_EXITING | CPU_BASED_CR3_STORE_EXITING);
2962 * secondary cpu-based controls. Do not include those that
2963 * depend on CPUID bits, they are added later by vmx_cpuid_update.
2965 rdmsr(MSR_IA32_VMX_PROCBASED_CTLS2,
2966 vmx->nested.nested_vmx_secondary_ctls_low,
2967 vmx->nested.nested_vmx_secondary_ctls_high);
2968 vmx->nested.nested_vmx_secondary_ctls_low = 0;
2969 vmx->nested.nested_vmx_secondary_ctls_high &=
2970 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
2971 SECONDARY_EXEC_DESC |
2972 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
2973 SECONDARY_EXEC_APIC_REGISTER_VIRT |
2974 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY |
2975 SECONDARY_EXEC_WBINVD_EXITING;
2978 /* nested EPT: emulate EPT also to L1 */
2979 vmx->nested.nested_vmx_secondary_ctls_high |=
2980 SECONDARY_EXEC_ENABLE_EPT;
2981 vmx->nested.nested_vmx_ept_caps = VMX_EPT_PAGE_WALK_4_BIT |
2982 VMX_EPTP_WB_BIT | VMX_EPT_INVEPT_BIT;
2983 if (cpu_has_vmx_ept_execute_only())
2984 vmx->nested.nested_vmx_ept_caps |=
2985 VMX_EPT_EXECUTE_ONLY_BIT;
2986 vmx->nested.nested_vmx_ept_caps &= vmx_capability.ept;
2987 vmx->nested.nested_vmx_ept_caps |= VMX_EPT_EXTENT_GLOBAL_BIT |
2988 VMX_EPT_EXTENT_CONTEXT_BIT | VMX_EPT_2MB_PAGE_BIT |
2989 VMX_EPT_1GB_PAGE_BIT;
2990 if (enable_ept_ad_bits) {
2991 vmx->nested.nested_vmx_secondary_ctls_high |=
2992 SECONDARY_EXEC_ENABLE_PML;
2993 vmx->nested.nested_vmx_ept_caps |= VMX_EPT_AD_BIT;
2996 vmx->nested.nested_vmx_ept_caps = 0;
2998 if (cpu_has_vmx_vmfunc()) {
2999 vmx->nested.nested_vmx_secondary_ctls_high |=
3000 SECONDARY_EXEC_ENABLE_VMFUNC;
3002 * Advertise EPTP switching unconditionally
3003 * since we emulate it
3006 vmx->nested.nested_vmx_vmfunc_controls =
3007 VMX_VMFUNC_EPTP_SWITCHING;
3011 * Old versions of KVM use the single-context version without
3012 * checking for support, so declare that it is supported even
3013 * though it is treated as global context. The alternative is
3014 * not failing the single-context invvpid, and it is worse.
3017 vmx->nested.nested_vmx_secondary_ctls_high |=
3018 SECONDARY_EXEC_ENABLE_VPID;
3019 vmx->nested.nested_vmx_vpid_caps = VMX_VPID_INVVPID_BIT |
3020 VMX_VPID_EXTENT_SUPPORTED_MASK;
3022 vmx->nested.nested_vmx_vpid_caps = 0;
3024 if (enable_unrestricted_guest)
3025 vmx->nested.nested_vmx_secondary_ctls_high |=
3026 SECONDARY_EXEC_UNRESTRICTED_GUEST;
3028 /* miscellaneous data */
3029 rdmsr(MSR_IA32_VMX_MISC,
3030 vmx->nested.nested_vmx_misc_low,
3031 vmx->nested.nested_vmx_misc_high);
3032 vmx->nested.nested_vmx_misc_low &= VMX_MISC_SAVE_EFER_LMA;
3033 vmx->nested.nested_vmx_misc_low |=
3034 VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE |
3035 VMX_MISC_ACTIVITY_HLT;
3036 vmx->nested.nested_vmx_misc_high = 0;
3039 * This MSR reports some information about VMX support. We
3040 * should return information about the VMX we emulate for the
3041 * guest, and the VMCS structure we give it - not about the
3042 * VMX support of the underlying hardware.
3044 vmx->nested.nested_vmx_basic =
3046 VMX_BASIC_TRUE_CTLS |
3047 ((u64)VMCS12_SIZE << VMX_BASIC_VMCS_SIZE_SHIFT) |
3048 (VMX_BASIC_MEM_TYPE_WB << VMX_BASIC_MEM_TYPE_SHIFT);
3050 if (cpu_has_vmx_basic_inout())
3051 vmx->nested.nested_vmx_basic |= VMX_BASIC_INOUT;
3054 * These MSRs specify bits which the guest must keep fixed on
3055 * while L1 is in VMXON mode (in L1's root mode, or running an L2).
3056 * We picked the standard core2 setting.
3058 #define VMXON_CR0_ALWAYSON (X86_CR0_PE | X86_CR0_PG | X86_CR0_NE)
3059 #define VMXON_CR4_ALWAYSON X86_CR4_VMXE
3060 vmx->nested.nested_vmx_cr0_fixed0 = VMXON_CR0_ALWAYSON;
3061 vmx->nested.nested_vmx_cr4_fixed0 = VMXON_CR4_ALWAYSON;
3063 /* These MSRs specify bits which the guest must keep fixed off. */
3064 rdmsrl(MSR_IA32_VMX_CR0_FIXED1, vmx->nested.nested_vmx_cr0_fixed1);
3065 rdmsrl(MSR_IA32_VMX_CR4_FIXED1, vmx->nested.nested_vmx_cr4_fixed1);
3067 /* highest index: VMX_PREEMPTION_TIMER_VALUE */
3068 vmx->nested.nested_vmx_vmcs_enum = 0x2e;
3072 * if fixed0[i] == 1: val[i] must be 1
3073 * if fixed1[i] == 0: val[i] must be 0
3075 static inline bool fixed_bits_valid(u64 val, u64 fixed0, u64 fixed1)
3077 return ((val & fixed1) | fixed0) == val;
3080 static inline bool vmx_control_verify(u32 control, u32 low, u32 high)
3082 return fixed_bits_valid(control, low, high);
3085 static inline u64 vmx_control_msr(u32 low, u32 high)
3087 return low | ((u64)high << 32);
3090 static bool is_bitwise_subset(u64 superset, u64 subset, u64 mask)
3095 return (superset | subset) == superset;
3098 static int vmx_restore_vmx_basic(struct vcpu_vmx *vmx, u64 data)
3100 const u64 feature_and_reserved =
3101 /* feature (except bit 48; see below) */
3102 BIT_ULL(49) | BIT_ULL(54) | BIT_ULL(55) |
3104 BIT_ULL(31) | GENMASK_ULL(47, 45) | GENMASK_ULL(63, 56);
3105 u64 vmx_basic = vmx->nested.nested_vmx_basic;
3107 if (!is_bitwise_subset(vmx_basic, data, feature_and_reserved))
3111 * KVM does not emulate a version of VMX that constrains physical
3112 * addresses of VMX structures (e.g. VMCS) to 32-bits.
3114 if (data & BIT_ULL(48))
3117 if (vmx_basic_vmcs_revision_id(vmx_basic) !=
3118 vmx_basic_vmcs_revision_id(data))
3121 if (vmx_basic_vmcs_size(vmx_basic) > vmx_basic_vmcs_size(data))
3124 vmx->nested.nested_vmx_basic = data;
3129 vmx_restore_control_msr(struct vcpu_vmx *vmx, u32 msr_index, u64 data)
3134 switch (msr_index) {
3135 case MSR_IA32_VMX_TRUE_PINBASED_CTLS:
3136 lowp = &vmx->nested.nested_vmx_pinbased_ctls_low;
3137 highp = &vmx->nested.nested_vmx_pinbased_ctls_high;
3139 case MSR_IA32_VMX_TRUE_PROCBASED_CTLS:
3140 lowp = &vmx->nested.nested_vmx_procbased_ctls_low;
3141 highp = &vmx->nested.nested_vmx_procbased_ctls_high;
3143 case MSR_IA32_VMX_TRUE_EXIT_CTLS:
3144 lowp = &vmx->nested.nested_vmx_exit_ctls_low;
3145 highp = &vmx->nested.nested_vmx_exit_ctls_high;
3147 case MSR_IA32_VMX_TRUE_ENTRY_CTLS:
3148 lowp = &vmx->nested.nested_vmx_entry_ctls_low;
3149 highp = &vmx->nested.nested_vmx_entry_ctls_high;
3151 case MSR_IA32_VMX_PROCBASED_CTLS2:
3152 lowp = &vmx->nested.nested_vmx_secondary_ctls_low;
3153 highp = &vmx->nested.nested_vmx_secondary_ctls_high;
3159 supported = vmx_control_msr(*lowp, *highp);
3161 /* Check must-be-1 bits are still 1. */
3162 if (!is_bitwise_subset(data, supported, GENMASK_ULL(31, 0)))
3165 /* Check must-be-0 bits are still 0. */
3166 if (!is_bitwise_subset(supported, data, GENMASK_ULL(63, 32)))
3170 *highp = data >> 32;
3174 static int vmx_restore_vmx_misc(struct vcpu_vmx *vmx, u64 data)
3176 const u64 feature_and_reserved_bits =
3178 BIT_ULL(5) | GENMASK_ULL(8, 6) | BIT_ULL(14) | BIT_ULL(15) |
3179 BIT_ULL(28) | BIT_ULL(29) | BIT_ULL(30) |
3181 GENMASK_ULL(13, 9) | BIT_ULL(31);
3184 vmx_misc = vmx_control_msr(vmx->nested.nested_vmx_misc_low,
3185 vmx->nested.nested_vmx_misc_high);
3187 if (!is_bitwise_subset(vmx_misc, data, feature_and_reserved_bits))
3190 if ((vmx->nested.nested_vmx_pinbased_ctls_high &
3191 PIN_BASED_VMX_PREEMPTION_TIMER) &&
3192 vmx_misc_preemption_timer_rate(data) !=
3193 vmx_misc_preemption_timer_rate(vmx_misc))
3196 if (vmx_misc_cr3_count(data) > vmx_misc_cr3_count(vmx_misc))
3199 if (vmx_misc_max_msr(data) > vmx_misc_max_msr(vmx_misc))
3202 if (vmx_misc_mseg_revid(data) != vmx_misc_mseg_revid(vmx_misc))
3205 vmx->nested.nested_vmx_misc_low = data;
3206 vmx->nested.nested_vmx_misc_high = data >> 32;
3210 static int vmx_restore_vmx_ept_vpid_cap(struct vcpu_vmx *vmx, u64 data)
3212 u64 vmx_ept_vpid_cap;
3214 vmx_ept_vpid_cap = vmx_control_msr(vmx->nested.nested_vmx_ept_caps,
3215 vmx->nested.nested_vmx_vpid_caps);
3217 /* Every bit is either reserved or a feature bit. */
3218 if (!is_bitwise_subset(vmx_ept_vpid_cap, data, -1ULL))
3221 vmx->nested.nested_vmx_ept_caps = data;
3222 vmx->nested.nested_vmx_vpid_caps = data >> 32;
3226 static int vmx_restore_fixed0_msr(struct vcpu_vmx *vmx, u32 msr_index, u64 data)
3230 switch (msr_index) {
3231 case MSR_IA32_VMX_CR0_FIXED0:
3232 msr = &vmx->nested.nested_vmx_cr0_fixed0;
3234 case MSR_IA32_VMX_CR4_FIXED0:
3235 msr = &vmx->nested.nested_vmx_cr4_fixed0;
3242 * 1 bits (which indicates bits which "must-be-1" during VMX operation)
3243 * must be 1 in the restored value.
3245 if (!is_bitwise_subset(data, *msr, -1ULL))
3253 * Called when userspace is restoring VMX MSRs.
3255 * Returns 0 on success, non-0 otherwise.
3257 static int vmx_set_vmx_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 data)
3259 struct vcpu_vmx *vmx = to_vmx(vcpu);
3261 switch (msr_index) {
3262 case MSR_IA32_VMX_BASIC:
3263 return vmx_restore_vmx_basic(vmx, data);
3264 case MSR_IA32_VMX_PINBASED_CTLS:
3265 case MSR_IA32_VMX_PROCBASED_CTLS:
3266 case MSR_IA32_VMX_EXIT_CTLS:
3267 case MSR_IA32_VMX_ENTRY_CTLS:
3269 * The "non-true" VMX capability MSRs are generated from the
3270 * "true" MSRs, so we do not support restoring them directly.
3272 * If userspace wants to emulate VMX_BASIC[55]=0, userspace
3273 * should restore the "true" MSRs with the must-be-1 bits
3274 * set according to the SDM Vol 3. A.2 "RESERVED CONTROLS AND
3275 * DEFAULT SETTINGS".
3278 case MSR_IA32_VMX_TRUE_PINBASED_CTLS:
3279 case MSR_IA32_VMX_TRUE_PROCBASED_CTLS:
3280 case MSR_IA32_VMX_TRUE_EXIT_CTLS:
3281 case MSR_IA32_VMX_TRUE_ENTRY_CTLS:
3282 case MSR_IA32_VMX_PROCBASED_CTLS2:
3283 return vmx_restore_control_msr(vmx, msr_index, data);
3284 case MSR_IA32_VMX_MISC:
3285 return vmx_restore_vmx_misc(vmx, data);
3286 case MSR_IA32_VMX_CR0_FIXED0:
3287 case MSR_IA32_VMX_CR4_FIXED0:
3288 return vmx_restore_fixed0_msr(vmx, msr_index, data);
3289 case MSR_IA32_VMX_CR0_FIXED1:
3290 case MSR_IA32_VMX_CR4_FIXED1:
3292 * These MSRs are generated based on the vCPU's CPUID, so we
3293 * do not support restoring them directly.
3296 case MSR_IA32_VMX_EPT_VPID_CAP:
3297 return vmx_restore_vmx_ept_vpid_cap(vmx, data);
3298 case MSR_IA32_VMX_VMCS_ENUM:
3299 vmx->nested.nested_vmx_vmcs_enum = data;
3303 * The rest of the VMX capability MSRs do not support restore.
3309 /* Returns 0 on success, non-0 otherwise. */
3310 static int vmx_get_vmx_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 *pdata)
3312 struct vcpu_vmx *vmx = to_vmx(vcpu);
3314 switch (msr_index) {
3315 case MSR_IA32_VMX_BASIC:
3316 *pdata = vmx->nested.nested_vmx_basic;
3318 case MSR_IA32_VMX_TRUE_PINBASED_CTLS:
3319 case MSR_IA32_VMX_PINBASED_CTLS:
3320 *pdata = vmx_control_msr(
3321 vmx->nested.nested_vmx_pinbased_ctls_low,
3322 vmx->nested.nested_vmx_pinbased_ctls_high);
3323 if (msr_index == MSR_IA32_VMX_PINBASED_CTLS)
3324 *pdata |= PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
3326 case MSR_IA32_VMX_TRUE_PROCBASED_CTLS:
3327 case MSR_IA32_VMX_PROCBASED_CTLS:
3328 *pdata = vmx_control_msr(
3329 vmx->nested.nested_vmx_procbased_ctls_low,
3330 vmx->nested.nested_vmx_procbased_ctls_high);
3331 if (msr_index == MSR_IA32_VMX_PROCBASED_CTLS)
3332 *pdata |= CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
3334 case MSR_IA32_VMX_TRUE_EXIT_CTLS:
3335 case MSR_IA32_VMX_EXIT_CTLS:
3336 *pdata = vmx_control_msr(
3337 vmx->nested.nested_vmx_exit_ctls_low,
3338 vmx->nested.nested_vmx_exit_ctls_high);
3339 if (msr_index == MSR_IA32_VMX_EXIT_CTLS)
3340 *pdata |= VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR;
3342 case MSR_IA32_VMX_TRUE_ENTRY_CTLS:
3343 case MSR_IA32_VMX_ENTRY_CTLS:
3344 *pdata = vmx_control_msr(
3345 vmx->nested.nested_vmx_entry_ctls_low,
3346 vmx->nested.nested_vmx_entry_ctls_high);
3347 if (msr_index == MSR_IA32_VMX_ENTRY_CTLS)
3348 *pdata |= VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR;
3350 case MSR_IA32_VMX_MISC:
3351 *pdata = vmx_control_msr(
3352 vmx->nested.nested_vmx_misc_low,
3353 vmx->nested.nested_vmx_misc_high);
3355 case MSR_IA32_VMX_CR0_FIXED0:
3356 *pdata = vmx->nested.nested_vmx_cr0_fixed0;
3358 case MSR_IA32_VMX_CR0_FIXED1:
3359 *pdata = vmx->nested.nested_vmx_cr0_fixed1;
3361 case MSR_IA32_VMX_CR4_FIXED0:
3362 *pdata = vmx->nested.nested_vmx_cr4_fixed0;
3364 case MSR_IA32_VMX_CR4_FIXED1:
3365 *pdata = vmx->nested.nested_vmx_cr4_fixed1;
3367 case MSR_IA32_VMX_VMCS_ENUM:
3368 *pdata = vmx->nested.nested_vmx_vmcs_enum;
3370 case MSR_IA32_VMX_PROCBASED_CTLS2:
3371 *pdata = vmx_control_msr(
3372 vmx->nested.nested_vmx_secondary_ctls_low,
3373 vmx->nested.nested_vmx_secondary_ctls_high);
3375 case MSR_IA32_VMX_EPT_VPID_CAP:
3376 *pdata = vmx->nested.nested_vmx_ept_caps |
3377 ((u64)vmx->nested.nested_vmx_vpid_caps << 32);
3379 case MSR_IA32_VMX_VMFUNC:
3380 *pdata = vmx->nested.nested_vmx_vmfunc_controls;
3389 static inline bool vmx_feature_control_msr_valid(struct kvm_vcpu *vcpu,
3392 uint64_t valid_bits = to_vmx(vcpu)->msr_ia32_feature_control_valid_bits;
3394 return !(val & ~valid_bits);
3398 * Reads an msr value (of 'msr_index') into 'pdata'.
3399 * Returns 0 on success, non-0 otherwise.
3400 * Assumes vcpu_load() was already called.
3402 static int vmx_get_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
3404 struct shared_msr_entry *msr;
3406 switch (msr_info->index) {
3407 #ifdef CONFIG_X86_64
3409 msr_info->data = vmcs_readl(GUEST_FS_BASE);
3412 msr_info->data = vmcs_readl(GUEST_GS_BASE);
3414 case MSR_KERNEL_GS_BASE:
3415 vmx_load_host_state(to_vmx(vcpu));
3416 msr_info->data = to_vmx(vcpu)->msr_guest_kernel_gs_base;
3420 return kvm_get_msr_common(vcpu, msr_info);
3422 msr_info->data = guest_read_tsc(vcpu);
3424 case MSR_IA32_SPEC_CTRL:
3425 if (!msr_info->host_initiated &&
3426 !guest_cpuid_has(vcpu, X86_FEATURE_SPEC_CTRL))
3429 msr_info->data = to_vmx(vcpu)->spec_ctrl;
3431 case MSR_IA32_ARCH_CAPABILITIES:
3432 if (!msr_info->host_initiated &&
3433 !guest_cpuid_has(vcpu, X86_FEATURE_ARCH_CAPABILITIES))
3435 msr_info->data = to_vmx(vcpu)->arch_capabilities;
3437 case MSR_IA32_SYSENTER_CS:
3438 msr_info->data = vmcs_read32(GUEST_SYSENTER_CS);
3440 case MSR_IA32_SYSENTER_EIP:
3441 msr_info->data = vmcs_readl(GUEST_SYSENTER_EIP);
3443 case MSR_IA32_SYSENTER_ESP:
3444 msr_info->data = vmcs_readl(GUEST_SYSENTER_ESP);
3446 case MSR_IA32_BNDCFGS:
3447 if (!kvm_mpx_supported() ||
3448 (!msr_info->host_initiated &&
3449 !guest_cpuid_has(vcpu, X86_FEATURE_MPX)))
3451 msr_info->data = vmcs_read64(GUEST_BNDCFGS);
3453 case MSR_IA32_MCG_EXT_CTL:
3454 if (!msr_info->host_initiated &&
3455 !(to_vmx(vcpu)->msr_ia32_feature_control &
3456 FEATURE_CONTROL_LMCE))
3458 msr_info->data = vcpu->arch.mcg_ext_ctl;
3460 case MSR_IA32_FEATURE_CONTROL:
3461 msr_info->data = to_vmx(vcpu)->msr_ia32_feature_control;
3463 case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC:
3464 if (!nested_vmx_allowed(vcpu))
3466 return vmx_get_vmx_msr(vcpu, msr_info->index, &msr_info->data);
3468 if (!vmx_xsaves_supported())
3470 msr_info->data = vcpu->arch.ia32_xss;
3473 if (!msr_info->host_initiated &&
3474 !guest_cpuid_has(vcpu, X86_FEATURE_RDTSCP))
3476 /* Otherwise falls through */
3478 msr = find_msr_entry(to_vmx(vcpu), msr_info->index);
3480 msr_info->data = msr->data;
3483 return kvm_get_msr_common(vcpu, msr_info);
3489 static void vmx_leave_nested(struct kvm_vcpu *vcpu);
3492 * Writes msr value into into the appropriate "register".
3493 * Returns 0 on success, non-0 otherwise.
3494 * Assumes vcpu_load() was already called.
3496 static int vmx_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
3498 struct vcpu_vmx *vmx = to_vmx(vcpu);
3499 struct shared_msr_entry *msr;
3501 u32 msr_index = msr_info->index;
3502 u64 data = msr_info->data;
3504 switch (msr_index) {
3506 ret = kvm_set_msr_common(vcpu, msr_info);
3508 #ifdef CONFIG_X86_64
3510 vmx_segment_cache_clear(vmx);
3511 vmcs_writel(GUEST_FS_BASE, data);
3514 vmx_segment_cache_clear(vmx);
3515 vmcs_writel(GUEST_GS_BASE, data);
3517 case MSR_KERNEL_GS_BASE:
3518 vmx_load_host_state(vmx);
3519 vmx->msr_guest_kernel_gs_base = data;
3522 case MSR_IA32_SYSENTER_CS:
3523 vmcs_write32(GUEST_SYSENTER_CS, data);
3525 case MSR_IA32_SYSENTER_EIP:
3526 vmcs_writel(GUEST_SYSENTER_EIP, data);
3528 case MSR_IA32_SYSENTER_ESP:
3529 vmcs_writel(GUEST_SYSENTER_ESP, data);
3531 case MSR_IA32_BNDCFGS:
3532 if (!kvm_mpx_supported() ||
3533 (!msr_info->host_initiated &&
3534 !guest_cpuid_has(vcpu, X86_FEATURE_MPX)))
3536 if (is_noncanonical_address(data & PAGE_MASK, vcpu) ||
3537 (data & MSR_IA32_BNDCFGS_RSVD))
3539 vmcs_write64(GUEST_BNDCFGS, data);
3542 kvm_write_tsc(vcpu, msr_info);
3544 case MSR_IA32_SPEC_CTRL:
3545 if (!msr_info->host_initiated &&
3546 !guest_cpuid_has(vcpu, X86_FEATURE_SPEC_CTRL))
3549 /* The STIBP bit doesn't fault even if it's not advertised */
3550 if (data & ~(SPEC_CTRL_IBRS | SPEC_CTRL_STIBP | SPEC_CTRL_SSBD))
3553 vmx->spec_ctrl = data;
3560 * When it's written (to non-zero) for the first time, pass
3564 * The handling of the MSR bitmap for L2 guests is done in
3565 * nested_vmx_merge_msr_bitmap. We should not touch the
3566 * vmcs02.msr_bitmap here since it gets completely overwritten
3567 * in the merging. We update the vmcs01 here for L1 as well
3568 * since it will end up touching the MSR anyway now.
3570 vmx_disable_intercept_for_msr(vmx->vmcs01.msr_bitmap,
3574 case MSR_IA32_PRED_CMD:
3575 if (!msr_info->host_initiated &&
3576 !guest_cpuid_has(vcpu, X86_FEATURE_SPEC_CTRL))
3579 if (data & ~PRED_CMD_IBPB)
3585 wrmsrl(MSR_IA32_PRED_CMD, PRED_CMD_IBPB);
3589 * When it's written (to non-zero) for the first time, pass
3593 * The handling of the MSR bitmap for L2 guests is done in
3594 * nested_vmx_merge_msr_bitmap. We should not touch the
3595 * vmcs02.msr_bitmap here since it gets completely overwritten
3598 vmx_disable_intercept_for_msr(vmx->vmcs01.msr_bitmap, MSR_IA32_PRED_CMD,
3601 case MSR_IA32_ARCH_CAPABILITIES:
3602 if (!msr_info->host_initiated)
3604 vmx->arch_capabilities = data;
3606 case MSR_IA32_CR_PAT:
3607 if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) {
3608 if (!kvm_mtrr_valid(vcpu, MSR_IA32_CR_PAT, data))
3610 vmcs_write64(GUEST_IA32_PAT, data);
3611 vcpu->arch.pat = data;
3614 ret = kvm_set_msr_common(vcpu, msr_info);
3616 case MSR_IA32_TSC_ADJUST:
3617 ret = kvm_set_msr_common(vcpu, msr_info);
3619 case MSR_IA32_MCG_EXT_CTL:
3620 if ((!msr_info->host_initiated &&
3621 !(to_vmx(vcpu)->msr_ia32_feature_control &
3622 FEATURE_CONTROL_LMCE)) ||
3623 (data & ~MCG_EXT_CTL_LMCE_EN))
3625 vcpu->arch.mcg_ext_ctl = data;
3627 case MSR_IA32_FEATURE_CONTROL:
3628 if (!vmx_feature_control_msr_valid(vcpu, data) ||
3629 (to_vmx(vcpu)->msr_ia32_feature_control &
3630 FEATURE_CONTROL_LOCKED && !msr_info->host_initiated))
3632 vmx->msr_ia32_feature_control = data;
3633 if (msr_info->host_initiated && data == 0)
3634 vmx_leave_nested(vcpu);
3636 case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC:
3637 if (!msr_info->host_initiated)
3638 return 1; /* they are read-only */
3639 if (!nested_vmx_allowed(vcpu))
3641 return vmx_set_vmx_msr(vcpu, msr_index, data);
3643 if (!vmx_xsaves_supported())
3646 * The only supported bit as of Skylake is bit 8, but
3647 * it is not supported on KVM.
3651 vcpu->arch.ia32_xss = data;
3652 if (vcpu->arch.ia32_xss != host_xss)
3653 add_atomic_switch_msr(vmx, MSR_IA32_XSS,
3654 vcpu->arch.ia32_xss, host_xss, false);
3656 clear_atomic_switch_msr(vmx, MSR_IA32_XSS);
3659 if (!msr_info->host_initiated &&
3660 !guest_cpuid_has(vcpu, X86_FEATURE_RDTSCP))
3662 /* Check reserved bit, higher 32 bits should be zero */
3663 if ((data >> 32) != 0)
3665 /* Otherwise falls through */
3667 msr = find_msr_entry(vmx, msr_index);
3669 u64 old_msr_data = msr->data;
3671 if (msr - vmx->guest_msrs < vmx->save_nmsrs) {
3673 ret = kvm_set_shared_msr(msr->index, msr->data,
3677 msr->data = old_msr_data;
3681 ret = kvm_set_msr_common(vcpu, msr_info);
3687 static void vmx_cache_reg(struct kvm_vcpu *vcpu, enum kvm_reg reg)
3689 __set_bit(reg, (unsigned long *)&vcpu->arch.regs_avail);
3692 vcpu->arch.regs[VCPU_REGS_RSP] = vmcs_readl(GUEST_RSP);
3695 vcpu->arch.regs[VCPU_REGS_RIP] = vmcs_readl(GUEST_RIP);
3697 case VCPU_EXREG_PDPTR:
3699 ept_save_pdptrs(vcpu);
3706 static __init int cpu_has_kvm_support(void)
3708 return cpu_has_vmx();
3711 static __init int vmx_disabled_by_bios(void)
3715 rdmsrl(MSR_IA32_FEATURE_CONTROL, msr);
3716 if (msr & FEATURE_CONTROL_LOCKED) {
3717 /* launched w/ TXT and VMX disabled */
3718 if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX)
3721 /* launched w/o TXT and VMX only enabled w/ TXT */
3722 if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX)
3723 && (msr & FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX)
3724 && !tboot_enabled()) {
3725 printk(KERN_WARNING "kvm: disable TXT in the BIOS or "
3726 "activate TXT before enabling KVM\n");
3729 /* launched w/o TXT and VMX disabled */
3730 if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX)
3731 && !tboot_enabled())
3738 static void kvm_cpu_vmxon(u64 addr)
3740 cr4_set_bits(X86_CR4_VMXE);
3741 intel_pt_handle_vmx(1);
3743 asm volatile (ASM_VMX_VMXON_RAX
3744 : : "a"(&addr), "m"(addr)
3748 static int hardware_enable(void)
3750 int cpu = raw_smp_processor_id();
3751 u64 phys_addr = __pa(per_cpu(vmxarea, cpu));
3754 if (cr4_read_shadow() & X86_CR4_VMXE)
3757 INIT_LIST_HEAD(&per_cpu(loaded_vmcss_on_cpu, cpu));
3758 INIT_LIST_HEAD(&per_cpu(blocked_vcpu_on_cpu, cpu));
3759 spin_lock_init(&per_cpu(blocked_vcpu_on_cpu_lock, cpu));
3762 * Now we can enable the vmclear operation in kdump
3763 * since the loaded_vmcss_on_cpu list on this cpu
3764 * has been initialized.
3766 * Though the cpu is not in VMX operation now, there
3767 * is no problem to enable the vmclear operation
3768 * for the loaded_vmcss_on_cpu list is empty!
3770 crash_enable_local_vmclear(cpu);
3772 rdmsrl(MSR_IA32_FEATURE_CONTROL, old);
3774 test_bits = FEATURE_CONTROL_LOCKED;
3775 test_bits |= FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
3776 if (tboot_enabled())
3777 test_bits |= FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX;
3779 if ((old & test_bits) != test_bits) {
3780 /* enable and lock */
3781 wrmsrl(MSR_IA32_FEATURE_CONTROL, old | test_bits);
3783 kvm_cpu_vmxon(phys_addr);
3789 static void vmclear_local_loaded_vmcss(void)
3791 int cpu = raw_smp_processor_id();
3792 struct loaded_vmcs *v, *n;
3794 list_for_each_entry_safe(v, n, &per_cpu(loaded_vmcss_on_cpu, cpu),
3795 loaded_vmcss_on_cpu_link)
3796 __loaded_vmcs_clear(v);
3800 /* Just like cpu_vmxoff(), but with the __kvm_handle_fault_on_reboot()
3803 static void kvm_cpu_vmxoff(void)
3805 asm volatile (__ex(ASM_VMX_VMXOFF) : : : "cc");
3807 intel_pt_handle_vmx(0);
3808 cr4_clear_bits(X86_CR4_VMXE);
3811 static void hardware_disable(void)
3813 vmclear_local_loaded_vmcss();
3817 static __init int adjust_vmx_controls(u32 ctl_min, u32 ctl_opt,
3818 u32 msr, u32 *result)
3820 u32 vmx_msr_low, vmx_msr_high;
3821 u32 ctl = ctl_min | ctl_opt;
3823 rdmsr(msr, vmx_msr_low, vmx_msr_high);
3825 ctl &= vmx_msr_high; /* bit == 0 in high word ==> must be zero */
3826 ctl |= vmx_msr_low; /* bit == 1 in low word ==> must be one */
3828 /* Ensure minimum (required) set of control bits are supported. */
3836 static __init bool allow_1_setting(u32 msr, u32 ctl)
3838 u32 vmx_msr_low, vmx_msr_high;
3840 rdmsr(msr, vmx_msr_low, vmx_msr_high);
3841 return vmx_msr_high & ctl;
3844 static __init int setup_vmcs_config(struct vmcs_config *vmcs_conf)
3846 u32 vmx_msr_low, vmx_msr_high;
3847 u32 min, opt, min2, opt2;
3848 u32 _pin_based_exec_control = 0;
3849 u32 _cpu_based_exec_control = 0;
3850 u32 _cpu_based_2nd_exec_control = 0;
3851 u32 _vmexit_control = 0;
3852 u32 _vmentry_control = 0;
3854 min = CPU_BASED_HLT_EXITING |
3855 #ifdef CONFIG_X86_64
3856 CPU_BASED_CR8_LOAD_EXITING |
3857 CPU_BASED_CR8_STORE_EXITING |
3859 CPU_BASED_CR3_LOAD_EXITING |
3860 CPU_BASED_CR3_STORE_EXITING |
3861 CPU_BASED_USE_IO_BITMAPS |
3862 CPU_BASED_MOV_DR_EXITING |
3863 CPU_BASED_USE_TSC_OFFSETING |
3864 CPU_BASED_INVLPG_EXITING |
3865 CPU_BASED_RDPMC_EXITING;
3867 if (!kvm_mwait_in_guest())
3868 min |= CPU_BASED_MWAIT_EXITING |
3869 CPU_BASED_MONITOR_EXITING;
3871 opt = CPU_BASED_TPR_SHADOW |
3872 CPU_BASED_USE_MSR_BITMAPS |
3873 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
3874 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PROCBASED_CTLS,
3875 &_cpu_based_exec_control) < 0)
3877 #ifdef CONFIG_X86_64
3878 if ((_cpu_based_exec_control & CPU_BASED_TPR_SHADOW))
3879 _cpu_based_exec_control &= ~CPU_BASED_CR8_LOAD_EXITING &
3880 ~CPU_BASED_CR8_STORE_EXITING;
3882 if (_cpu_based_exec_control & CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) {
3884 opt2 = SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
3885 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
3886 SECONDARY_EXEC_WBINVD_EXITING |
3887 SECONDARY_EXEC_ENABLE_VPID |
3888 SECONDARY_EXEC_ENABLE_EPT |
3889 SECONDARY_EXEC_UNRESTRICTED_GUEST |
3890 SECONDARY_EXEC_PAUSE_LOOP_EXITING |
3891 SECONDARY_EXEC_RDTSCP |
3892 SECONDARY_EXEC_ENABLE_INVPCID |
3893 SECONDARY_EXEC_APIC_REGISTER_VIRT |
3894 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY |
3895 SECONDARY_EXEC_SHADOW_VMCS |
3896 SECONDARY_EXEC_XSAVES |
3897 SECONDARY_EXEC_RDSEED |
3898 SECONDARY_EXEC_RDRAND |
3899 SECONDARY_EXEC_ENABLE_PML |
3900 SECONDARY_EXEC_TSC_SCALING |
3901 SECONDARY_EXEC_ENABLE_VMFUNC;
3902 if (adjust_vmx_controls(min2, opt2,
3903 MSR_IA32_VMX_PROCBASED_CTLS2,
3904 &_cpu_based_2nd_exec_control) < 0)
3907 #ifndef CONFIG_X86_64
3908 if (!(_cpu_based_2nd_exec_control &
3909 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES))
3910 _cpu_based_exec_control &= ~CPU_BASED_TPR_SHADOW;
3913 if (!(_cpu_based_exec_control & CPU_BASED_TPR_SHADOW))
3914 _cpu_based_2nd_exec_control &= ~(
3915 SECONDARY_EXEC_APIC_REGISTER_VIRT |
3916 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
3917 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
3919 if (_cpu_based_2nd_exec_control & SECONDARY_EXEC_ENABLE_EPT) {
3920 /* CR3 accesses and invlpg don't need to cause VM Exits when EPT
3922 _cpu_based_exec_control &= ~(CPU_BASED_CR3_LOAD_EXITING |
3923 CPU_BASED_CR3_STORE_EXITING |
3924 CPU_BASED_INVLPG_EXITING);
3925 rdmsr(MSR_IA32_VMX_EPT_VPID_CAP,
3926 vmx_capability.ept, vmx_capability.vpid);
3929 min = VM_EXIT_SAVE_DEBUG_CONTROLS | VM_EXIT_ACK_INTR_ON_EXIT;
3930 #ifdef CONFIG_X86_64
3931 min |= VM_EXIT_HOST_ADDR_SPACE_SIZE;
3933 opt = VM_EXIT_SAVE_IA32_PAT | VM_EXIT_LOAD_IA32_PAT |
3934 VM_EXIT_CLEAR_BNDCFGS;
3935 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_EXIT_CTLS,
3936 &_vmexit_control) < 0)
3939 min = PIN_BASED_EXT_INTR_MASK | PIN_BASED_NMI_EXITING;
3940 opt = PIN_BASED_VIRTUAL_NMIS | PIN_BASED_POSTED_INTR |
3941 PIN_BASED_VMX_PREEMPTION_TIMER;
3942 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PINBASED_CTLS,
3943 &_pin_based_exec_control) < 0)
3946 if (cpu_has_broken_vmx_preemption_timer())
3947 _pin_based_exec_control &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
3948 if (!(_cpu_based_2nd_exec_control &
3949 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY))
3950 _pin_based_exec_control &= ~PIN_BASED_POSTED_INTR;
3952 min = VM_ENTRY_LOAD_DEBUG_CONTROLS;
3953 opt = VM_ENTRY_LOAD_IA32_PAT | VM_ENTRY_LOAD_BNDCFGS;
3954 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_ENTRY_CTLS,
3955 &_vmentry_control) < 0)
3958 rdmsr(MSR_IA32_VMX_BASIC, vmx_msr_low, vmx_msr_high);
3960 /* IA-32 SDM Vol 3B: VMCS size is never greater than 4kB. */
3961 if ((vmx_msr_high & 0x1fff) > PAGE_SIZE)
3964 #ifdef CONFIG_X86_64
3965 /* IA-32 SDM Vol 3B: 64-bit CPUs always have VMX_BASIC_MSR[48]==0. */
3966 if (vmx_msr_high & (1u<<16))
3970 /* Require Write-Back (WB) memory type for VMCS accesses. */
3971 if (((vmx_msr_high >> 18) & 15) != 6)
3974 vmcs_conf->size = vmx_msr_high & 0x1fff;
3975 vmcs_conf->order = get_order(vmcs_conf->size);
3976 vmcs_conf->basic_cap = vmx_msr_high & ~0x1fff;
3977 vmcs_conf->revision_id = vmx_msr_low;
3979 vmcs_conf->pin_based_exec_ctrl = _pin_based_exec_control;
3980 vmcs_conf->cpu_based_exec_ctrl = _cpu_based_exec_control;
3981 vmcs_conf->cpu_based_2nd_exec_ctrl = _cpu_based_2nd_exec_control;
3982 vmcs_conf->vmexit_ctrl = _vmexit_control;
3983 vmcs_conf->vmentry_ctrl = _vmentry_control;
3985 cpu_has_load_ia32_efer =
3986 allow_1_setting(MSR_IA32_VMX_ENTRY_CTLS,
3987 VM_ENTRY_LOAD_IA32_EFER)
3988 && allow_1_setting(MSR_IA32_VMX_EXIT_CTLS,
3989 VM_EXIT_LOAD_IA32_EFER);
3991 cpu_has_load_perf_global_ctrl =
3992 allow_1_setting(MSR_IA32_VMX_ENTRY_CTLS,
3993 VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL)
3994 && allow_1_setting(MSR_IA32_VMX_EXIT_CTLS,
3995 VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL);
3998 * Some cpus support VM_ENTRY_(LOAD|SAVE)_IA32_PERF_GLOBAL_CTRL
3999 * but due to errata below it can't be used. Workaround is to use
4000 * msr load mechanism to switch IA32_PERF_GLOBAL_CTRL.
4002 * VM Exit May Incorrectly Clear IA32_PERF_GLOBAL_CTRL [34:32]
4007 * BC86,AAY89,BD102 (model 44)
4011 if (cpu_has_load_perf_global_ctrl && boot_cpu_data.x86 == 0x6) {
4012 switch (boot_cpu_data.x86_model) {
4018 cpu_has_load_perf_global_ctrl = false;
4019 printk_once(KERN_WARNING"kvm: VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL "
4020 "does not work properly. Using workaround\n");
4027 if (boot_cpu_has(X86_FEATURE_XSAVES))
4028 rdmsrl(MSR_IA32_XSS, host_xss);
4033 static struct vmcs *alloc_vmcs_cpu(int cpu)
4035 int node = cpu_to_node(cpu);
4039 pages = __alloc_pages_node(node, GFP_KERNEL, vmcs_config.order);
4042 vmcs = page_address(pages);
4043 memset(vmcs, 0, vmcs_config.size);
4044 vmcs->revision_id = vmcs_config.revision_id; /* vmcs revision id */
4048 static void free_vmcs(struct vmcs *vmcs)
4050 free_pages((unsigned long)vmcs, vmcs_config.order);
4054 * Free a VMCS, but before that VMCLEAR it on the CPU where it was last loaded
4056 static void free_loaded_vmcs(struct loaded_vmcs *loaded_vmcs)
4058 if (!loaded_vmcs->vmcs)
4060 loaded_vmcs_clear(loaded_vmcs);
4061 free_vmcs(loaded_vmcs->vmcs);
4062 loaded_vmcs->vmcs = NULL;
4063 if (loaded_vmcs->msr_bitmap)
4064 free_page((unsigned long)loaded_vmcs->msr_bitmap);
4065 WARN_ON(loaded_vmcs->shadow_vmcs != NULL);
4068 static struct vmcs *alloc_vmcs(void)
4070 return alloc_vmcs_cpu(raw_smp_processor_id());
4073 static int alloc_loaded_vmcs(struct loaded_vmcs *loaded_vmcs)
4075 loaded_vmcs->vmcs = alloc_vmcs();
4076 if (!loaded_vmcs->vmcs)
4079 loaded_vmcs->shadow_vmcs = NULL;
4080 loaded_vmcs_init(loaded_vmcs);
4082 if (cpu_has_vmx_msr_bitmap()) {
4083 loaded_vmcs->msr_bitmap = (unsigned long *)__get_free_page(GFP_KERNEL);
4084 if (!loaded_vmcs->msr_bitmap)
4086 memset(loaded_vmcs->msr_bitmap, 0xff, PAGE_SIZE);
4091 free_loaded_vmcs(loaded_vmcs);
4095 static void free_kvm_area(void)
4099 for_each_possible_cpu(cpu) {
4100 free_vmcs(per_cpu(vmxarea, cpu));
4101 per_cpu(vmxarea, cpu) = NULL;
4105 enum vmcs_field_type {
4106 VMCS_FIELD_TYPE_U16 = 0,
4107 VMCS_FIELD_TYPE_U64 = 1,
4108 VMCS_FIELD_TYPE_U32 = 2,
4109 VMCS_FIELD_TYPE_NATURAL_WIDTH = 3
4112 static inline int vmcs_field_type(unsigned long field)
4114 if (0x1 & field) /* the *_HIGH fields are all 32 bit */
4115 return VMCS_FIELD_TYPE_U32;
4116 return (field >> 13) & 0x3 ;
4119 static inline int vmcs_field_readonly(unsigned long field)
4121 return (((field >> 10) & 0x3) == 1);
4124 static void init_vmcs_shadow_fields(void)
4128 /* No checks for read only fields yet */
4130 for (i = j = 0; i < max_shadow_read_write_fields; i++) {
4131 switch (shadow_read_write_fields[i]) {
4133 if (!kvm_mpx_supported())
4141 shadow_read_write_fields[j] =
4142 shadow_read_write_fields[i];
4145 max_shadow_read_write_fields = j;
4147 /* shadowed fields guest access without vmexit */
4148 for (i = 0; i < max_shadow_read_write_fields; i++) {
4149 unsigned long field = shadow_read_write_fields[i];
4151 clear_bit(field, vmx_vmwrite_bitmap);
4152 clear_bit(field, vmx_vmread_bitmap);
4153 if (vmcs_field_type(field) == VMCS_FIELD_TYPE_U64) {
4154 clear_bit(field + 1, vmx_vmwrite_bitmap);
4155 clear_bit(field + 1, vmx_vmread_bitmap);
4158 for (i = 0; i < max_shadow_read_only_fields; i++) {
4159 unsigned long field = shadow_read_only_fields[i];
4161 clear_bit(field, vmx_vmread_bitmap);
4162 if (vmcs_field_type(field) == VMCS_FIELD_TYPE_U64)
4163 clear_bit(field + 1, vmx_vmread_bitmap);
4167 static __init int alloc_kvm_area(void)
4171 for_each_possible_cpu(cpu) {
4174 vmcs = alloc_vmcs_cpu(cpu);
4180 per_cpu(vmxarea, cpu) = vmcs;
4185 static void fix_pmode_seg(struct kvm_vcpu *vcpu, int seg,
4186 struct kvm_segment *save)
4188 if (!emulate_invalid_guest_state) {
4190 * CS and SS RPL should be equal during guest entry according
4191 * to VMX spec, but in reality it is not always so. Since vcpu
4192 * is in the middle of the transition from real mode to
4193 * protected mode it is safe to assume that RPL 0 is a good
4196 if (seg == VCPU_SREG_CS || seg == VCPU_SREG_SS)
4197 save->selector &= ~SEGMENT_RPL_MASK;
4198 save->dpl = save->selector & SEGMENT_RPL_MASK;
4201 vmx_set_segment(vcpu, save, seg);
4204 static void enter_pmode(struct kvm_vcpu *vcpu)
4206 unsigned long flags;
4207 struct vcpu_vmx *vmx = to_vmx(vcpu);
4210 * Update real mode segment cache. It may be not up-to-date if sement
4211 * register was written while vcpu was in a guest mode.
4213 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_ES], VCPU_SREG_ES);
4214 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_DS], VCPU_SREG_DS);
4215 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_FS], VCPU_SREG_FS);
4216 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_GS], VCPU_SREG_GS);
4217 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_SS], VCPU_SREG_SS);
4218 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_CS], VCPU_SREG_CS);
4220 vmx->rmode.vm86_active = 0;
4222 vmx_segment_cache_clear(vmx);
4224 vmx_set_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_TR], VCPU_SREG_TR);
4226 flags = vmcs_readl(GUEST_RFLAGS);
4227 flags &= RMODE_GUEST_OWNED_EFLAGS_BITS;
4228 flags |= vmx->rmode.save_rflags & ~RMODE_GUEST_OWNED_EFLAGS_BITS;
4229 vmcs_writel(GUEST_RFLAGS, flags);
4231 vmcs_writel(GUEST_CR4, (vmcs_readl(GUEST_CR4) & ~X86_CR4_VME) |
4232 (vmcs_readl(CR4_READ_SHADOW) & X86_CR4_VME));
4234 update_exception_bitmap(vcpu);
4236 fix_pmode_seg(vcpu, VCPU_SREG_CS, &vmx->rmode.segs[VCPU_SREG_CS]);
4237 fix_pmode_seg(vcpu, VCPU_SREG_SS, &vmx->rmode.segs[VCPU_SREG_SS]);
4238 fix_pmode_seg(vcpu, VCPU_SREG_ES, &vmx->rmode.segs[VCPU_SREG_ES]);
4239 fix_pmode_seg(vcpu, VCPU_SREG_DS, &vmx->rmode.segs[VCPU_SREG_DS]);
4240 fix_pmode_seg(vcpu, VCPU_SREG_FS, &vmx->rmode.segs[VCPU_SREG_FS]);
4241 fix_pmode_seg(vcpu, VCPU_SREG_GS, &vmx->rmode.segs[VCPU_SREG_GS]);
4244 static void fix_rmode_seg(int seg, struct kvm_segment *save)
4246 const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
4247 struct kvm_segment var = *save;
4250 if (seg == VCPU_SREG_CS)
4253 if (!emulate_invalid_guest_state) {
4254 var.selector = var.base >> 4;
4255 var.base = var.base & 0xffff0;
4265 if (save->base & 0xf)
4266 printk_once(KERN_WARNING "kvm: segment base is not "
4267 "paragraph aligned when entering "
4268 "protected mode (seg=%d)", seg);
4271 vmcs_write16(sf->selector, var.selector);
4272 vmcs_writel(sf->base, var.base);
4273 vmcs_write32(sf->limit, var.limit);
4274 vmcs_write32(sf->ar_bytes, vmx_segment_access_rights(&var));
4277 static void enter_rmode(struct kvm_vcpu *vcpu)
4279 unsigned long flags;
4280 struct vcpu_vmx *vmx = to_vmx(vcpu);
4282 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_TR], VCPU_SREG_TR);
4283 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_ES], VCPU_SREG_ES);
4284 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_DS], VCPU_SREG_DS);
4285 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_FS], VCPU_SREG_FS);
4286 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_GS], VCPU_SREG_GS);
4287 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_SS], VCPU_SREG_SS);
4288 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_CS], VCPU_SREG_CS);
4290 vmx->rmode.vm86_active = 1;
4293 * Very old userspace does not call KVM_SET_TSS_ADDR before entering
4294 * vcpu. Warn the user that an update is overdue.
4296 if (!vcpu->kvm->arch.tss_addr)
4297 printk_once(KERN_WARNING "kvm: KVM_SET_TSS_ADDR need to be "
4298 "called before entering vcpu\n");
4300 vmx_segment_cache_clear(vmx);
4302 vmcs_writel(GUEST_TR_BASE, vcpu->kvm->arch.tss_addr);
4303 vmcs_write32(GUEST_TR_LIMIT, RMODE_TSS_SIZE - 1);
4304 vmcs_write32(GUEST_TR_AR_BYTES, 0x008b);
4306 flags = vmcs_readl(GUEST_RFLAGS);
4307 vmx->rmode.save_rflags = flags;
4309 flags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM;
4311 vmcs_writel(GUEST_RFLAGS, flags);
4312 vmcs_writel(GUEST_CR4, vmcs_readl(GUEST_CR4) | X86_CR4_VME);
4313 update_exception_bitmap(vcpu);
4315 fix_rmode_seg(VCPU_SREG_SS, &vmx->rmode.segs[VCPU_SREG_SS]);
4316 fix_rmode_seg(VCPU_SREG_CS, &vmx->rmode.segs[VCPU_SREG_CS]);
4317 fix_rmode_seg(VCPU_SREG_ES, &vmx->rmode.segs[VCPU_SREG_ES]);
4318 fix_rmode_seg(VCPU_SREG_DS, &vmx->rmode.segs[VCPU_SREG_DS]);
4319 fix_rmode_seg(VCPU_SREG_GS, &vmx->rmode.segs[VCPU_SREG_GS]);
4320 fix_rmode_seg(VCPU_SREG_FS, &vmx->rmode.segs[VCPU_SREG_FS]);
4322 kvm_mmu_reset_context(vcpu);
4325 static void vmx_set_efer(struct kvm_vcpu *vcpu, u64 efer)
4327 struct vcpu_vmx *vmx = to_vmx(vcpu);
4328 struct shared_msr_entry *msr = find_msr_entry(vmx, MSR_EFER);
4334 * Force kernel_gs_base reloading before EFER changes, as control
4335 * of this msr depends on is_long_mode().
4337 vmx_load_host_state(to_vmx(vcpu));
4338 vcpu->arch.efer = efer;
4339 if (efer & EFER_LMA) {
4340 vm_entry_controls_setbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
4343 vm_entry_controls_clearbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
4345 msr->data = efer & ~EFER_LME;
4350 #ifdef CONFIG_X86_64
4352 static void enter_lmode(struct kvm_vcpu *vcpu)
4356 vmx_segment_cache_clear(to_vmx(vcpu));
4358 guest_tr_ar = vmcs_read32(GUEST_TR_AR_BYTES);
4359 if ((guest_tr_ar & VMX_AR_TYPE_MASK) != VMX_AR_TYPE_BUSY_64_TSS) {
4360 pr_debug_ratelimited("%s: tss fixup for long mode. \n",
4362 vmcs_write32(GUEST_TR_AR_BYTES,
4363 (guest_tr_ar & ~VMX_AR_TYPE_MASK)
4364 | VMX_AR_TYPE_BUSY_64_TSS);
4366 vmx_set_efer(vcpu, vcpu->arch.efer | EFER_LMA);
4369 static void exit_lmode(struct kvm_vcpu *vcpu)
4371 vm_entry_controls_clearbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
4372 vmx_set_efer(vcpu, vcpu->arch.efer & ~EFER_LMA);
4377 static inline void __vmx_flush_tlb(struct kvm_vcpu *vcpu, int vpid)
4380 if (!VALID_PAGE(vcpu->arch.mmu.root_hpa))
4382 ept_sync_context(construct_eptp(vcpu, vcpu->arch.mmu.root_hpa));
4384 vpid_sync_context(vpid);
4388 static void vmx_flush_tlb(struct kvm_vcpu *vcpu)
4390 __vmx_flush_tlb(vcpu, to_vmx(vcpu)->vpid);
4393 static void vmx_flush_tlb_ept_only(struct kvm_vcpu *vcpu)
4396 vmx_flush_tlb(vcpu);
4399 static void vmx_decache_cr0_guest_bits(struct kvm_vcpu *vcpu)
4401 ulong cr0_guest_owned_bits = vcpu->arch.cr0_guest_owned_bits;
4403 vcpu->arch.cr0 &= ~cr0_guest_owned_bits;
4404 vcpu->arch.cr0 |= vmcs_readl(GUEST_CR0) & cr0_guest_owned_bits;
4407 static void vmx_decache_cr3(struct kvm_vcpu *vcpu)
4409 if (enable_ept && is_paging(vcpu))
4410 vcpu->arch.cr3 = vmcs_readl(GUEST_CR3);
4411 __set_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail);
4414 static void vmx_decache_cr4_guest_bits(struct kvm_vcpu *vcpu)
4416 ulong cr4_guest_owned_bits = vcpu->arch.cr4_guest_owned_bits;
4418 vcpu->arch.cr4 &= ~cr4_guest_owned_bits;
4419 vcpu->arch.cr4 |= vmcs_readl(GUEST_CR4) & cr4_guest_owned_bits;
4422 static void ept_load_pdptrs(struct kvm_vcpu *vcpu)
4424 struct kvm_mmu *mmu = vcpu->arch.walk_mmu;
4426 if (!test_bit(VCPU_EXREG_PDPTR,
4427 (unsigned long *)&vcpu->arch.regs_dirty))
4430 if (is_paging(vcpu) && is_pae(vcpu) && !is_long_mode(vcpu)) {
4431 vmcs_write64(GUEST_PDPTR0, mmu->pdptrs[0]);
4432 vmcs_write64(GUEST_PDPTR1, mmu->pdptrs[1]);
4433 vmcs_write64(GUEST_PDPTR2, mmu->pdptrs[2]);
4434 vmcs_write64(GUEST_PDPTR3, mmu->pdptrs[3]);
4438 static void ept_save_pdptrs(struct kvm_vcpu *vcpu)
4440 struct kvm_mmu *mmu = vcpu->arch.walk_mmu;
4442 if (is_paging(vcpu) && is_pae(vcpu) && !is_long_mode(vcpu)) {
4443 mmu->pdptrs[0] = vmcs_read64(GUEST_PDPTR0);
4444 mmu->pdptrs[1] = vmcs_read64(GUEST_PDPTR1);
4445 mmu->pdptrs[2] = vmcs_read64(GUEST_PDPTR2);
4446 mmu->pdptrs[3] = vmcs_read64(GUEST_PDPTR3);
4449 __set_bit(VCPU_EXREG_PDPTR,
4450 (unsigned long *)&vcpu->arch.regs_avail);
4451 __set_bit(VCPU_EXREG_PDPTR,
4452 (unsigned long *)&vcpu->arch.regs_dirty);
4455 static bool nested_guest_cr0_valid(struct kvm_vcpu *vcpu, unsigned long val)
4457 u64 fixed0 = to_vmx(vcpu)->nested.nested_vmx_cr0_fixed0;
4458 u64 fixed1 = to_vmx(vcpu)->nested.nested_vmx_cr0_fixed1;
4459 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
4461 if (to_vmx(vcpu)->nested.nested_vmx_secondary_ctls_high &
4462 SECONDARY_EXEC_UNRESTRICTED_GUEST &&
4463 nested_cpu_has2(vmcs12, SECONDARY_EXEC_UNRESTRICTED_GUEST))
4464 fixed0 &= ~(X86_CR0_PE | X86_CR0_PG);
4466 return fixed_bits_valid(val, fixed0, fixed1);
4469 static bool nested_host_cr0_valid(struct kvm_vcpu *vcpu, unsigned long val)
4471 u64 fixed0 = to_vmx(vcpu)->nested.nested_vmx_cr0_fixed0;
4472 u64 fixed1 = to_vmx(vcpu)->nested.nested_vmx_cr0_fixed1;
4474 return fixed_bits_valid(val, fixed0, fixed1);
4477 static bool nested_cr4_valid(struct kvm_vcpu *vcpu, unsigned long val)
4479 u64 fixed0 = to_vmx(vcpu)->nested.nested_vmx_cr4_fixed0;
4480 u64 fixed1 = to_vmx(vcpu)->nested.nested_vmx_cr4_fixed1;
4482 return fixed_bits_valid(val, fixed0, fixed1);
4485 /* No difference in the restrictions on guest and host CR4 in VMX operation. */
4486 #define nested_guest_cr4_valid nested_cr4_valid
4487 #define nested_host_cr4_valid nested_cr4_valid
4489 static int vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4);
4491 static void ept_update_paging_mode_cr0(unsigned long *hw_cr0,
4493 struct kvm_vcpu *vcpu)
4495 if (!test_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail))
4496 vmx_decache_cr3(vcpu);
4497 if (!(cr0 & X86_CR0_PG)) {
4498 /* From paging/starting to nonpaging */
4499 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL,
4500 vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) |
4501 (CPU_BASED_CR3_LOAD_EXITING |
4502 CPU_BASED_CR3_STORE_EXITING));
4503 vcpu->arch.cr0 = cr0;
4504 vmx_set_cr4(vcpu, kvm_read_cr4(vcpu));
4505 } else if (!is_paging(vcpu)) {
4506 /* From nonpaging to paging */
4507 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL,
4508 vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) &
4509 ~(CPU_BASED_CR3_LOAD_EXITING |
4510 CPU_BASED_CR3_STORE_EXITING));
4511 vcpu->arch.cr0 = cr0;
4512 vmx_set_cr4(vcpu, kvm_read_cr4(vcpu));
4515 if (!(cr0 & X86_CR0_WP))
4516 *hw_cr0 &= ~X86_CR0_WP;
4519 static void vmx_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
4521 struct vcpu_vmx *vmx = to_vmx(vcpu);
4522 unsigned long hw_cr0;
4524 hw_cr0 = (cr0 & ~KVM_GUEST_CR0_MASK);
4525 if (enable_unrestricted_guest)
4526 hw_cr0 |= KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST;
4528 hw_cr0 |= KVM_VM_CR0_ALWAYS_ON;
4530 if (vmx->rmode.vm86_active && (cr0 & X86_CR0_PE))
4533 if (!vmx->rmode.vm86_active && !(cr0 & X86_CR0_PE))
4537 #ifdef CONFIG_X86_64
4538 if (vcpu->arch.efer & EFER_LME) {
4539 if (!is_paging(vcpu) && (cr0 & X86_CR0_PG))
4541 if (is_paging(vcpu) && !(cr0 & X86_CR0_PG))
4547 ept_update_paging_mode_cr0(&hw_cr0, cr0, vcpu);
4549 vmcs_writel(CR0_READ_SHADOW, cr0);
4550 vmcs_writel(GUEST_CR0, hw_cr0);
4551 vcpu->arch.cr0 = cr0;
4553 /* depends on vcpu->arch.cr0 to be set to a new value */
4554 vmx->emulation_required = emulation_required(vcpu);
4557 static int get_ept_level(struct kvm_vcpu *vcpu)
4559 if (cpu_has_vmx_ept_5levels() && (cpuid_maxphyaddr(vcpu) > 48))
4564 static u64 construct_eptp(struct kvm_vcpu *vcpu, unsigned long root_hpa)
4566 u64 eptp = VMX_EPTP_MT_WB;
4568 eptp |= (get_ept_level(vcpu) == 5) ? VMX_EPTP_PWL_5 : VMX_EPTP_PWL_4;
4570 if (enable_ept_ad_bits &&
4571 (!is_guest_mode(vcpu) || nested_ept_ad_enabled(vcpu)))
4572 eptp |= VMX_EPTP_AD_ENABLE_BIT;
4573 eptp |= (root_hpa & PAGE_MASK);
4578 static void vmx_set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3)
4580 unsigned long guest_cr3;
4585 eptp = construct_eptp(vcpu, cr3);
4586 vmcs_write64(EPT_POINTER, eptp);
4587 if (is_paging(vcpu) || is_guest_mode(vcpu))
4588 guest_cr3 = kvm_read_cr3(vcpu);
4590 guest_cr3 = vcpu->kvm->arch.ept_identity_map_addr;
4591 ept_load_pdptrs(vcpu);
4594 vmx_flush_tlb(vcpu);
4595 vmcs_writel(GUEST_CR3, guest_cr3);
4598 static int vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
4601 * Pass through host's Machine Check Enable value to hw_cr4, which
4602 * is in force while we are in guest mode. Do not let guests control
4603 * this bit, even if host CR4.MCE == 0.
4605 unsigned long hw_cr4 =
4606 (cr4_read_shadow() & X86_CR4_MCE) |
4607 (cr4 & ~X86_CR4_MCE) |
4608 (to_vmx(vcpu)->rmode.vm86_active ?
4609 KVM_RMODE_VM_CR4_ALWAYS_ON : KVM_PMODE_VM_CR4_ALWAYS_ON);
4611 if (cr4 & X86_CR4_VMXE) {
4613 * To use VMXON (and later other VMX instructions), a guest
4614 * must first be able to turn on cr4.VMXE (see handle_vmon()).
4615 * So basically the check on whether to allow nested VMX
4618 if (!nested_vmx_allowed(vcpu))
4622 if (to_vmx(vcpu)->nested.vmxon && !nested_cr4_valid(vcpu, cr4))
4625 vcpu->arch.cr4 = cr4;
4627 if (!is_paging(vcpu)) {
4628 hw_cr4 &= ~X86_CR4_PAE;
4629 hw_cr4 |= X86_CR4_PSE;
4630 } else if (!(cr4 & X86_CR4_PAE)) {
4631 hw_cr4 &= ~X86_CR4_PAE;
4635 if (!enable_unrestricted_guest && !is_paging(vcpu))
4637 * SMEP/SMAP/PKU is disabled if CPU is in non-paging mode in
4638 * hardware. To emulate this behavior, SMEP/SMAP/PKU needs
4639 * to be manually disabled when guest switches to non-paging
4642 * If !enable_unrestricted_guest, the CPU is always running
4643 * with CR0.PG=1 and CR4 needs to be modified.
4644 * If enable_unrestricted_guest, the CPU automatically
4645 * disables SMEP/SMAP/PKU when the guest sets CR0.PG=0.
4647 hw_cr4 &= ~(X86_CR4_SMEP | X86_CR4_SMAP | X86_CR4_PKE);
4649 vmcs_writel(CR4_READ_SHADOW, cr4);
4650 vmcs_writel(GUEST_CR4, hw_cr4);
4654 static void vmx_get_segment(struct kvm_vcpu *vcpu,
4655 struct kvm_segment *var, int seg)
4657 struct vcpu_vmx *vmx = to_vmx(vcpu);
4660 if (vmx->rmode.vm86_active && seg != VCPU_SREG_LDTR) {
4661 *var = vmx->rmode.segs[seg];
4662 if (seg == VCPU_SREG_TR
4663 || var->selector == vmx_read_guest_seg_selector(vmx, seg))
4665 var->base = vmx_read_guest_seg_base(vmx, seg);
4666 var->selector = vmx_read_guest_seg_selector(vmx, seg);
4669 var->base = vmx_read_guest_seg_base(vmx, seg);
4670 var->limit = vmx_read_guest_seg_limit(vmx, seg);
4671 var->selector = vmx_read_guest_seg_selector(vmx, seg);
4672 ar = vmx_read_guest_seg_ar(vmx, seg);
4673 var->unusable = (ar >> 16) & 1;
4674 var->type = ar & 15;
4675 var->s = (ar >> 4) & 1;
4676 var->dpl = (ar >> 5) & 3;
4678 * Some userspaces do not preserve unusable property. Since usable
4679 * segment has to be present according to VMX spec we can use present
4680 * property to amend userspace bug by making unusable segment always
4681 * nonpresent. vmx_segment_access_rights() already marks nonpresent
4682 * segment as unusable.
4684 var->present = !var->unusable;
4685 var->avl = (ar >> 12) & 1;
4686 var->l = (ar >> 13) & 1;
4687 var->db = (ar >> 14) & 1;
4688 var->g = (ar >> 15) & 1;
4691 static u64 vmx_get_segment_base(struct kvm_vcpu *vcpu, int seg)
4693 struct kvm_segment s;
4695 if (to_vmx(vcpu)->rmode.vm86_active) {
4696 vmx_get_segment(vcpu, &s, seg);
4699 return vmx_read_guest_seg_base(to_vmx(vcpu), seg);
4702 static int vmx_get_cpl(struct kvm_vcpu *vcpu)
4704 struct vcpu_vmx *vmx = to_vmx(vcpu);
4706 if (unlikely(vmx->rmode.vm86_active))
4709 int ar = vmx_read_guest_seg_ar(vmx, VCPU_SREG_SS);
4710 return VMX_AR_DPL(ar);
4714 static u32 vmx_segment_access_rights(struct kvm_segment *var)
4718 if (var->unusable || !var->present)
4721 ar = var->type & 15;
4722 ar |= (var->s & 1) << 4;
4723 ar |= (var->dpl & 3) << 5;
4724 ar |= (var->present & 1) << 7;
4725 ar |= (var->avl & 1) << 12;
4726 ar |= (var->l & 1) << 13;
4727 ar |= (var->db & 1) << 14;
4728 ar |= (var->g & 1) << 15;
4734 static void vmx_set_segment(struct kvm_vcpu *vcpu,
4735 struct kvm_segment *var, int seg)
4737 struct vcpu_vmx *vmx = to_vmx(vcpu);
4738 const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
4740 vmx_segment_cache_clear(vmx);
4742 if (vmx->rmode.vm86_active && seg != VCPU_SREG_LDTR) {
4743 vmx->rmode.segs[seg] = *var;
4744 if (seg == VCPU_SREG_TR)
4745 vmcs_write16(sf->selector, var->selector);
4747 fix_rmode_seg(seg, &vmx->rmode.segs[seg]);
4751 vmcs_writel(sf->base, var->base);
4752 vmcs_write32(sf->limit, var->limit);
4753 vmcs_write16(sf->selector, var->selector);
4756 * Fix the "Accessed" bit in AR field of segment registers for older
4758 * IA32 arch specifies that at the time of processor reset the
4759 * "Accessed" bit in the AR field of segment registers is 1. And qemu
4760 * is setting it to 0 in the userland code. This causes invalid guest
4761 * state vmexit when "unrestricted guest" mode is turned on.
4762 * Fix for this setup issue in cpu_reset is being pushed in the qemu
4763 * tree. Newer qemu binaries with that qemu fix would not need this
4766 if (enable_unrestricted_guest && (seg != VCPU_SREG_LDTR))
4767 var->type |= 0x1; /* Accessed */
4769 vmcs_write32(sf->ar_bytes, vmx_segment_access_rights(var));
4772 vmx->emulation_required = emulation_required(vcpu);
4775 static void vmx_get_cs_db_l_bits(struct kvm_vcpu *vcpu, int *db, int *l)
4777 u32 ar = vmx_read_guest_seg_ar(to_vmx(vcpu), VCPU_SREG_CS);
4779 *db = (ar >> 14) & 1;
4780 *l = (ar >> 13) & 1;
4783 static void vmx_get_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
4785 dt->size = vmcs_read32(GUEST_IDTR_LIMIT);
4786 dt->address = vmcs_readl(GUEST_IDTR_BASE);
4789 static void vmx_set_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
4791 vmcs_write32(GUEST_IDTR_LIMIT, dt->size);
4792 vmcs_writel(GUEST_IDTR_BASE, dt->address);
4795 static void vmx_get_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
4797 dt->size = vmcs_read32(GUEST_GDTR_LIMIT);
4798 dt->address = vmcs_readl(GUEST_GDTR_BASE);
4801 static void vmx_set_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
4803 vmcs_write32(GUEST_GDTR_LIMIT, dt->size);
4804 vmcs_writel(GUEST_GDTR_BASE, dt->address);
4807 static bool rmode_segment_valid(struct kvm_vcpu *vcpu, int seg)
4809 struct kvm_segment var;
4812 vmx_get_segment(vcpu, &var, seg);
4814 if (seg == VCPU_SREG_CS)
4816 ar = vmx_segment_access_rights(&var);
4818 if (var.base != (var.selector << 4))
4820 if (var.limit != 0xffff)
4828 static bool code_segment_valid(struct kvm_vcpu *vcpu)
4830 struct kvm_segment cs;
4831 unsigned int cs_rpl;
4833 vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
4834 cs_rpl = cs.selector & SEGMENT_RPL_MASK;
4838 if (~cs.type & (VMX_AR_TYPE_CODE_MASK|VMX_AR_TYPE_ACCESSES_MASK))
4842 if (cs.type & VMX_AR_TYPE_WRITEABLE_MASK) {
4843 if (cs.dpl > cs_rpl)
4846 if (cs.dpl != cs_rpl)
4852 /* TODO: Add Reserved field check, this'll require a new member in the kvm_segment_field structure */
4856 static bool stack_segment_valid(struct kvm_vcpu *vcpu)
4858 struct kvm_segment ss;
4859 unsigned int ss_rpl;
4861 vmx_get_segment(vcpu, &ss, VCPU_SREG_SS);
4862 ss_rpl = ss.selector & SEGMENT_RPL_MASK;
4866 if (ss.type != 3 && ss.type != 7)
4870 if (ss.dpl != ss_rpl) /* DPL != RPL */
4878 static bool data_segment_valid(struct kvm_vcpu *vcpu, int seg)
4880 struct kvm_segment var;
4883 vmx_get_segment(vcpu, &var, seg);
4884 rpl = var.selector & SEGMENT_RPL_MASK;
4892 if (~var.type & (VMX_AR_TYPE_CODE_MASK|VMX_AR_TYPE_WRITEABLE_MASK)) {
4893 if (var.dpl < rpl) /* DPL < RPL */
4897 /* TODO: Add other members to kvm_segment_field to allow checking for other access
4903 static bool tr_valid(struct kvm_vcpu *vcpu)
4905 struct kvm_segment tr;
4907 vmx_get_segment(vcpu, &tr, VCPU_SREG_TR);
4911 if (tr.selector & SEGMENT_TI_MASK) /* TI = 1 */
4913 if (tr.type != 3 && tr.type != 11) /* TODO: Check if guest is in IA32e mode */
4921 static bool ldtr_valid(struct kvm_vcpu *vcpu)
4923 struct kvm_segment ldtr;
4925 vmx_get_segment(vcpu, &ldtr, VCPU_SREG_LDTR);
4929 if (ldtr.selector & SEGMENT_TI_MASK) /* TI = 1 */
4939 static bool cs_ss_rpl_check(struct kvm_vcpu *vcpu)
4941 struct kvm_segment cs, ss;
4943 vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
4944 vmx_get_segment(vcpu, &ss, VCPU_SREG_SS);
4946 return ((cs.selector & SEGMENT_RPL_MASK) ==
4947 (ss.selector & SEGMENT_RPL_MASK));
4951 * Check if guest state is valid. Returns true if valid, false if
4953 * We assume that registers are always usable
4955 static bool guest_state_valid(struct kvm_vcpu *vcpu)
4957 if (enable_unrestricted_guest)
4960 /* real mode guest state checks */
4961 if (!is_protmode(vcpu) || (vmx_get_rflags(vcpu) & X86_EFLAGS_VM)) {
4962 if (!rmode_segment_valid(vcpu, VCPU_SREG_CS))
4964 if (!rmode_segment_valid(vcpu, VCPU_SREG_SS))
4966 if (!rmode_segment_valid(vcpu, VCPU_SREG_DS))
4968 if (!rmode_segment_valid(vcpu, VCPU_SREG_ES))
4970 if (!rmode_segment_valid(vcpu, VCPU_SREG_FS))
4972 if (!rmode_segment_valid(vcpu, VCPU_SREG_GS))
4975 /* protected mode guest state checks */
4976 if (!cs_ss_rpl_check(vcpu))
4978 if (!code_segment_valid(vcpu))
4980 if (!stack_segment_valid(vcpu))
4982 if (!data_segment_valid(vcpu, VCPU_SREG_DS))
4984 if (!data_segment_valid(vcpu, VCPU_SREG_ES))
4986 if (!data_segment_valid(vcpu, VCPU_SREG_FS))
4988 if (!data_segment_valid(vcpu, VCPU_SREG_GS))
4990 if (!tr_valid(vcpu))
4992 if (!ldtr_valid(vcpu))
4996 * - Add checks on RIP
4997 * - Add checks on RFLAGS
5003 static bool page_address_valid(struct kvm_vcpu *vcpu, gpa_t gpa)
5005 return PAGE_ALIGNED(gpa) && !(gpa >> cpuid_maxphyaddr(vcpu));
5008 static int init_rmode_tss(struct kvm *kvm)
5014 idx = srcu_read_lock(&kvm->srcu);
5015 fn = kvm->arch.tss_addr >> PAGE_SHIFT;
5016 r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE);
5019 data = TSS_BASE_SIZE + TSS_REDIRECTION_SIZE;
5020 r = kvm_write_guest_page(kvm, fn++, &data,
5021 TSS_IOPB_BASE_OFFSET, sizeof(u16));
5024 r = kvm_clear_guest_page(kvm, fn++, 0, PAGE_SIZE);
5027 r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE);
5031 r = kvm_write_guest_page(kvm, fn, &data,
5032 RMODE_TSS_SIZE - 2 * PAGE_SIZE - 1,
5035 srcu_read_unlock(&kvm->srcu, idx);
5039 static int init_rmode_identity_map(struct kvm *kvm)
5042 kvm_pfn_t identity_map_pfn;
5048 /* Protect kvm->arch.ept_identity_pagetable_done. */
5049 mutex_lock(&kvm->slots_lock);
5051 if (likely(kvm->arch.ept_identity_pagetable_done))
5054 identity_map_pfn = kvm->arch.ept_identity_map_addr >> PAGE_SHIFT;
5056 r = alloc_identity_pagetable(kvm);
5060 idx = srcu_read_lock(&kvm->srcu);
5061 r = kvm_clear_guest_page(kvm, identity_map_pfn, 0, PAGE_SIZE);
5064 /* Set up identity-mapping pagetable for EPT in real mode */
5065 for (i = 0; i < PT32_ENT_PER_PAGE; i++) {
5066 tmp = (i << 22) + (_PAGE_PRESENT | _PAGE_RW | _PAGE_USER |
5067 _PAGE_ACCESSED | _PAGE_DIRTY | _PAGE_PSE);
5068 r = kvm_write_guest_page(kvm, identity_map_pfn,
5069 &tmp, i * sizeof(tmp), sizeof(tmp));
5073 kvm->arch.ept_identity_pagetable_done = true;
5076 srcu_read_unlock(&kvm->srcu, idx);
5079 mutex_unlock(&kvm->slots_lock);
5083 static void seg_setup(int seg)
5085 const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
5088 vmcs_write16(sf->selector, 0);
5089 vmcs_writel(sf->base, 0);
5090 vmcs_write32(sf->limit, 0xffff);
5092 if (seg == VCPU_SREG_CS)
5093 ar |= 0x08; /* code segment */
5095 vmcs_write32(sf->ar_bytes, ar);
5098 static int alloc_apic_access_page(struct kvm *kvm)
5103 mutex_lock(&kvm->slots_lock);
5104 if (kvm->arch.apic_access_page_done)
5106 r = __x86_set_memory_region(kvm, APIC_ACCESS_PAGE_PRIVATE_MEMSLOT,
5107 APIC_DEFAULT_PHYS_BASE, PAGE_SIZE);
5111 page = gfn_to_page(kvm, APIC_DEFAULT_PHYS_BASE >> PAGE_SHIFT);
5112 if (is_error_page(page)) {
5118 * Do not pin the page in memory, so that memory hot-unplug
5119 * is able to migrate it.
5122 kvm->arch.apic_access_page_done = true;
5124 mutex_unlock(&kvm->slots_lock);
5128 static int alloc_identity_pagetable(struct kvm *kvm)
5130 /* Called with kvm->slots_lock held. */
5134 BUG_ON(kvm->arch.ept_identity_pagetable_done);
5136 r = __x86_set_memory_region(kvm, IDENTITY_PAGETABLE_PRIVATE_MEMSLOT,
5137 kvm->arch.ept_identity_map_addr, PAGE_SIZE);
5142 static int allocate_vpid(void)
5148 spin_lock(&vmx_vpid_lock);
5149 vpid = find_first_zero_bit(vmx_vpid_bitmap, VMX_NR_VPIDS);
5150 if (vpid < VMX_NR_VPIDS)
5151 __set_bit(vpid, vmx_vpid_bitmap);
5154 spin_unlock(&vmx_vpid_lock);
5158 static void free_vpid(int vpid)
5160 if (!enable_vpid || vpid == 0)
5162 spin_lock(&vmx_vpid_lock);
5163 __clear_bit(vpid, vmx_vpid_bitmap);
5164 spin_unlock(&vmx_vpid_lock);
5167 static void __always_inline vmx_disable_intercept_for_msr(unsigned long *msr_bitmap,
5170 int f = sizeof(unsigned long);
5172 if (!cpu_has_vmx_msr_bitmap())
5176 * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
5177 * have the write-low and read-high bitmap offsets the wrong way round.
5178 * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
5180 if (msr <= 0x1fff) {
5181 if (type & MSR_TYPE_R)
5183 __clear_bit(msr, msr_bitmap + 0x000 / f);
5185 if (type & MSR_TYPE_W)
5187 __clear_bit(msr, msr_bitmap + 0x800 / f);
5189 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
5191 if (type & MSR_TYPE_R)
5193 __clear_bit(msr, msr_bitmap + 0x400 / f);
5195 if (type & MSR_TYPE_W)
5197 __clear_bit(msr, msr_bitmap + 0xc00 / f);
5202 static void __always_inline vmx_enable_intercept_for_msr(unsigned long *msr_bitmap,
5205 int f = sizeof(unsigned long);
5207 if (!cpu_has_vmx_msr_bitmap())
5211 * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
5212 * have the write-low and read-high bitmap offsets the wrong way round.
5213 * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
5215 if (msr <= 0x1fff) {
5216 if (type & MSR_TYPE_R)
5218 __set_bit(msr, msr_bitmap + 0x000 / f);
5220 if (type & MSR_TYPE_W)
5222 __set_bit(msr, msr_bitmap + 0x800 / f);
5224 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
5226 if (type & MSR_TYPE_R)
5228 __set_bit(msr, msr_bitmap + 0x400 / f);
5230 if (type & MSR_TYPE_W)
5232 __set_bit(msr, msr_bitmap + 0xc00 / f);
5237 static void __always_inline vmx_set_intercept_for_msr(unsigned long *msr_bitmap,
5238 u32 msr, int type, bool value)
5241 vmx_enable_intercept_for_msr(msr_bitmap, msr, type);
5243 vmx_disable_intercept_for_msr(msr_bitmap, msr, type);
5247 * If a msr is allowed by L0, we should check whether it is allowed by L1.
5248 * The corresponding bit will be cleared unless both of L0 and L1 allow it.
5250 static void nested_vmx_disable_intercept_for_msr(unsigned long *msr_bitmap_l1,
5251 unsigned long *msr_bitmap_nested,
5254 int f = sizeof(unsigned long);
5256 if (!cpu_has_vmx_msr_bitmap()) {
5262 * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
5263 * have the write-low and read-high bitmap offsets the wrong way round.
5264 * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
5266 if (msr <= 0x1fff) {
5267 if (type & MSR_TYPE_R &&
5268 !test_bit(msr, msr_bitmap_l1 + 0x000 / f))
5270 __clear_bit(msr, msr_bitmap_nested + 0x000 / f);
5272 if (type & MSR_TYPE_W &&
5273 !test_bit(msr, msr_bitmap_l1 + 0x800 / f))
5275 __clear_bit(msr, msr_bitmap_nested + 0x800 / f);
5277 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
5279 if (type & MSR_TYPE_R &&
5280 !test_bit(msr, msr_bitmap_l1 + 0x400 / f))
5282 __clear_bit(msr, msr_bitmap_nested + 0x400 / f);
5284 if (type & MSR_TYPE_W &&
5285 !test_bit(msr, msr_bitmap_l1 + 0xc00 / f))
5287 __clear_bit(msr, msr_bitmap_nested + 0xc00 / f);
5292 static u8 vmx_msr_bitmap_mode(struct kvm_vcpu *vcpu)
5296 if (cpu_has_secondary_exec_ctrls() &&
5297 (vmcs_read32(SECONDARY_VM_EXEC_CONTROL) &
5298 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE)) {
5299 mode |= MSR_BITMAP_MODE_X2APIC;
5300 if (enable_apicv && kvm_vcpu_apicv_active(vcpu))
5301 mode |= MSR_BITMAP_MODE_X2APIC_APICV;
5304 if (is_long_mode(vcpu))
5305 mode |= MSR_BITMAP_MODE_LM;
5310 #define X2APIC_MSR(r) (APIC_BASE_MSR + ((r) >> 4))
5312 static void vmx_update_msr_bitmap_x2apic(unsigned long *msr_bitmap,
5317 for (msr = 0x800; msr <= 0x8ff; msr += BITS_PER_LONG) {
5318 unsigned word = msr / BITS_PER_LONG;
5319 msr_bitmap[word] = (mode & MSR_BITMAP_MODE_X2APIC_APICV) ? 0 : ~0;
5320 msr_bitmap[word + (0x800 / sizeof(long))] = ~0;
5323 if (mode & MSR_BITMAP_MODE_X2APIC) {
5325 * TPR reads and writes can be virtualized even if virtual interrupt
5326 * delivery is not in use.
5328 vmx_disable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_TASKPRI), MSR_TYPE_RW);
5329 if (mode & MSR_BITMAP_MODE_X2APIC_APICV) {
5330 vmx_enable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_TMCCT), MSR_TYPE_R);
5331 vmx_disable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_EOI), MSR_TYPE_W);
5332 vmx_disable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_SELF_IPI), MSR_TYPE_W);
5337 static void vmx_update_msr_bitmap(struct kvm_vcpu *vcpu)
5339 struct vcpu_vmx *vmx = to_vmx(vcpu);
5340 unsigned long *msr_bitmap = vmx->vmcs01.msr_bitmap;
5341 u8 mode = vmx_msr_bitmap_mode(vcpu);
5342 u8 changed = mode ^ vmx->msr_bitmap_mode;
5347 vmx_set_intercept_for_msr(msr_bitmap, MSR_KERNEL_GS_BASE, MSR_TYPE_RW,
5348 !(mode & MSR_BITMAP_MODE_LM));
5350 if (changed & (MSR_BITMAP_MODE_X2APIC | MSR_BITMAP_MODE_X2APIC_APICV))
5351 vmx_update_msr_bitmap_x2apic(msr_bitmap, mode);
5353 vmx->msr_bitmap_mode = mode;
5356 static bool vmx_get_enable_apicv(struct kvm_vcpu *vcpu)
5358 return enable_apicv;
5361 static void nested_mark_vmcs12_pages_dirty(struct kvm_vcpu *vcpu)
5363 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
5367 * Don't need to mark the APIC access page dirty; it is never
5368 * written to by the CPU during APIC virtualization.
5371 if (nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW)) {
5372 gfn = vmcs12->virtual_apic_page_addr >> PAGE_SHIFT;
5373 kvm_vcpu_mark_page_dirty(vcpu, gfn);
5376 if (nested_cpu_has_posted_intr(vmcs12)) {
5377 gfn = vmcs12->posted_intr_desc_addr >> PAGE_SHIFT;
5378 kvm_vcpu_mark_page_dirty(vcpu, gfn);
5383 static void vmx_complete_nested_posted_interrupt(struct kvm_vcpu *vcpu)
5385 struct vcpu_vmx *vmx = to_vmx(vcpu);
5390 if (!vmx->nested.pi_desc || !vmx->nested.pi_pending)
5393 vmx->nested.pi_pending = false;
5394 if (!pi_test_and_clear_on(vmx->nested.pi_desc))
5397 max_irr = find_last_bit((unsigned long *)vmx->nested.pi_desc->pir, 256);
5398 if (max_irr != 256) {
5399 vapic_page = kmap(vmx->nested.virtual_apic_page);
5400 __kvm_apic_update_irr(vmx->nested.pi_desc->pir, vapic_page);
5401 kunmap(vmx->nested.virtual_apic_page);
5403 status = vmcs_read16(GUEST_INTR_STATUS);
5404 if ((u8)max_irr > ((u8)status & 0xff)) {
5406 status |= (u8)max_irr;
5407 vmcs_write16(GUEST_INTR_STATUS, status);
5411 nested_mark_vmcs12_pages_dirty(vcpu);
5414 static inline bool kvm_vcpu_trigger_posted_interrupt(struct kvm_vcpu *vcpu,
5418 int pi_vec = nested ? POSTED_INTR_NESTED_VECTOR : POSTED_INTR_VECTOR;
5420 if (vcpu->mode == IN_GUEST_MODE) {
5422 * The vector of interrupt to be delivered to vcpu had
5423 * been set in PIR before this function.
5425 * Following cases will be reached in this block, and
5426 * we always send a notification event in all cases as
5429 * Case 1: vcpu keeps in non-root mode. Sending a
5430 * notification event posts the interrupt to vcpu.
5432 * Case 2: vcpu exits to root mode and is still
5433 * runnable. PIR will be synced to vIRR before the
5434 * next vcpu entry. Sending a notification event in
5435 * this case has no effect, as vcpu is not in root
5438 * Case 3: vcpu exits to root mode and is blocked.
5439 * vcpu_block() has already synced PIR to vIRR and
5440 * never blocks vcpu if vIRR is not cleared. Therefore,
5441 * a blocked vcpu here does not wait for any requested
5442 * interrupts in PIR, and sending a notification event
5443 * which has no effect is safe here.
5446 apic->send_IPI_mask(get_cpu_mask(vcpu->cpu), pi_vec);
5453 static int vmx_deliver_nested_posted_interrupt(struct kvm_vcpu *vcpu,
5456 struct vcpu_vmx *vmx = to_vmx(vcpu);
5458 if (is_guest_mode(vcpu) &&
5459 vector == vmx->nested.posted_intr_nv) {
5461 * If a posted intr is not recognized by hardware,
5462 * we will accomplish it in the next vmentry.
5464 vmx->nested.pi_pending = true;
5465 kvm_make_request(KVM_REQ_EVENT, vcpu);
5466 /* the PIR and ON have been set by L1. */
5467 if (!kvm_vcpu_trigger_posted_interrupt(vcpu, true))
5468 kvm_vcpu_kick(vcpu);
5474 * Send interrupt to vcpu via posted interrupt way.
5475 * 1. If target vcpu is running(non-root mode), send posted interrupt
5476 * notification to vcpu and hardware will sync PIR to vIRR atomically.
5477 * 2. If target vcpu isn't running(root mode), kick it to pick up the
5478 * interrupt from PIR in next vmentry.
5480 static void vmx_deliver_posted_interrupt(struct kvm_vcpu *vcpu, int vector)
5482 struct vcpu_vmx *vmx = to_vmx(vcpu);
5485 r = vmx_deliver_nested_posted_interrupt(vcpu, vector);
5489 if (pi_test_and_set_pir(vector, &vmx->pi_desc))
5492 /* If a previous notification has sent the IPI, nothing to do. */
5493 if (pi_test_and_set_on(&vmx->pi_desc))
5496 if (!kvm_vcpu_trigger_posted_interrupt(vcpu, false))
5497 kvm_vcpu_kick(vcpu);
5501 * Set up the vmcs's constant host-state fields, i.e., host-state fields that
5502 * will not change in the lifetime of the guest.
5503 * Note that host-state that does change is set elsewhere. E.g., host-state
5504 * that is set differently for each CPU is set in vmx_vcpu_load(), not here.
5506 static void vmx_set_constant_host_state(struct vcpu_vmx *vmx)
5511 unsigned long cr0, cr3, cr4;
5514 WARN_ON(cr0 & X86_CR0_TS);
5515 vmcs_writel(HOST_CR0, cr0); /* 22.2.3 */
5518 * Save the most likely value for this task's CR3 in the VMCS.
5519 * We can't use __get_current_cr3_fast() because we're not atomic.
5522 vmcs_writel(HOST_CR3, cr3); /* 22.2.3 FIXME: shadow tables */
5523 vmx->loaded_vmcs->vmcs_host_cr3 = cr3;
5525 /* Save the most likely value for this task's CR4 in the VMCS. */
5526 cr4 = cr4_read_shadow();
5527 vmcs_writel(HOST_CR4, cr4); /* 22.2.3, 22.2.5 */
5528 vmx->loaded_vmcs->vmcs_host_cr4 = cr4;
5530 vmcs_write16(HOST_CS_SELECTOR, __KERNEL_CS); /* 22.2.4 */
5531 #ifdef CONFIG_X86_64
5533 * Load null selectors, so we can avoid reloading them in
5534 * __vmx_load_host_state(), in case userspace uses the null selectors
5535 * too (the expected case).
5537 vmcs_write16(HOST_DS_SELECTOR, 0);
5538 vmcs_write16(HOST_ES_SELECTOR, 0);
5540 vmcs_write16(HOST_DS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
5541 vmcs_write16(HOST_ES_SELECTOR, __KERNEL_DS); /* 22.2.4 */
5543 vmcs_write16(HOST_SS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
5544 vmcs_write16(HOST_TR_SELECTOR, GDT_ENTRY_TSS*8); /* 22.2.4 */
5547 vmcs_writel(HOST_IDTR_BASE, dt.address); /* 22.2.4 */
5548 vmx->host_idt_base = dt.address;
5550 vmcs_writel(HOST_RIP, vmx_return); /* 22.2.5 */
5552 rdmsr(MSR_IA32_SYSENTER_CS, low32, high32);
5553 vmcs_write32(HOST_IA32_SYSENTER_CS, low32);
5554 rdmsrl(MSR_IA32_SYSENTER_EIP, tmpl);
5555 vmcs_writel(HOST_IA32_SYSENTER_EIP, tmpl); /* 22.2.3 */
5557 if (vmcs_config.vmexit_ctrl & VM_EXIT_LOAD_IA32_PAT) {
5558 rdmsr(MSR_IA32_CR_PAT, low32, high32);
5559 vmcs_write64(HOST_IA32_PAT, low32 | ((u64) high32 << 32));
5563 static void set_cr4_guest_host_mask(struct vcpu_vmx *vmx)
5565 vmx->vcpu.arch.cr4_guest_owned_bits = KVM_CR4_GUEST_OWNED_BITS;
5567 vmx->vcpu.arch.cr4_guest_owned_bits |= X86_CR4_PGE;
5568 if (is_guest_mode(&vmx->vcpu))
5569 vmx->vcpu.arch.cr4_guest_owned_bits &=
5570 ~get_vmcs12(&vmx->vcpu)->cr4_guest_host_mask;
5571 vmcs_writel(CR4_GUEST_HOST_MASK, ~vmx->vcpu.arch.cr4_guest_owned_bits);
5574 static u32 vmx_pin_based_exec_ctrl(struct vcpu_vmx *vmx)
5576 u32 pin_based_exec_ctrl = vmcs_config.pin_based_exec_ctrl;
5578 if (!kvm_vcpu_apicv_active(&vmx->vcpu))
5579 pin_based_exec_ctrl &= ~PIN_BASED_POSTED_INTR;
5580 /* Enable the preemption timer dynamically */
5581 pin_based_exec_ctrl &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
5582 return pin_based_exec_ctrl;
5585 static void vmx_refresh_apicv_exec_ctrl(struct kvm_vcpu *vcpu)
5587 struct vcpu_vmx *vmx = to_vmx(vcpu);
5589 vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, vmx_pin_based_exec_ctrl(vmx));
5590 if (cpu_has_secondary_exec_ctrls()) {
5591 if (kvm_vcpu_apicv_active(vcpu))
5592 vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL,
5593 SECONDARY_EXEC_APIC_REGISTER_VIRT |
5594 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
5596 vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL,
5597 SECONDARY_EXEC_APIC_REGISTER_VIRT |
5598 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
5601 if (cpu_has_vmx_msr_bitmap())
5602 vmx_update_msr_bitmap(vcpu);
5605 static u32 vmx_exec_control(struct vcpu_vmx *vmx)
5607 u32 exec_control = vmcs_config.cpu_based_exec_ctrl;
5609 if (vmx->vcpu.arch.switch_db_regs & KVM_DEBUGREG_WONT_EXIT)
5610 exec_control &= ~CPU_BASED_MOV_DR_EXITING;
5612 if (!cpu_need_tpr_shadow(&vmx->vcpu)) {
5613 exec_control &= ~CPU_BASED_TPR_SHADOW;
5614 #ifdef CONFIG_X86_64
5615 exec_control |= CPU_BASED_CR8_STORE_EXITING |
5616 CPU_BASED_CR8_LOAD_EXITING;
5620 exec_control |= CPU_BASED_CR3_STORE_EXITING |
5621 CPU_BASED_CR3_LOAD_EXITING |
5622 CPU_BASED_INVLPG_EXITING;
5623 return exec_control;
5626 static bool vmx_rdrand_supported(void)
5628 return vmcs_config.cpu_based_2nd_exec_ctrl &
5629 SECONDARY_EXEC_RDRAND;
5632 static bool vmx_rdseed_supported(void)
5634 return vmcs_config.cpu_based_2nd_exec_ctrl &
5635 SECONDARY_EXEC_RDSEED;
5638 static void vmx_compute_secondary_exec_control(struct vcpu_vmx *vmx)
5640 struct kvm_vcpu *vcpu = &vmx->vcpu;
5642 u32 exec_control = vmcs_config.cpu_based_2nd_exec_ctrl;
5643 if (!cpu_need_virtualize_apic_accesses(vcpu))
5644 exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
5646 exec_control &= ~SECONDARY_EXEC_ENABLE_VPID;
5648 exec_control &= ~SECONDARY_EXEC_ENABLE_EPT;
5649 enable_unrestricted_guest = 0;
5650 /* Enable INVPCID for non-ept guests may cause performance regression. */
5651 exec_control &= ~SECONDARY_EXEC_ENABLE_INVPCID;
5653 if (!enable_unrestricted_guest)
5654 exec_control &= ~SECONDARY_EXEC_UNRESTRICTED_GUEST;
5656 exec_control &= ~SECONDARY_EXEC_PAUSE_LOOP_EXITING;
5657 if (!kvm_vcpu_apicv_active(vcpu))
5658 exec_control &= ~(SECONDARY_EXEC_APIC_REGISTER_VIRT |
5659 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
5660 exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
5661 /* SECONDARY_EXEC_SHADOW_VMCS is enabled when L1 executes VMPTRLD
5663 We can NOT enable shadow_vmcs here because we don't have yet
5666 exec_control &= ~SECONDARY_EXEC_SHADOW_VMCS;
5669 exec_control &= ~SECONDARY_EXEC_ENABLE_PML;
5671 if (vmx_xsaves_supported()) {
5672 /* Exposing XSAVES only when XSAVE is exposed */
5673 bool xsaves_enabled =
5674 guest_cpuid_has(vcpu, X86_FEATURE_XSAVE) &&
5675 guest_cpuid_has(vcpu, X86_FEATURE_XSAVES);
5677 if (!xsaves_enabled)
5678 exec_control &= ~SECONDARY_EXEC_XSAVES;
5682 vmx->nested.nested_vmx_secondary_ctls_high |=
5683 SECONDARY_EXEC_XSAVES;
5685 vmx->nested.nested_vmx_secondary_ctls_high &=
5686 ~SECONDARY_EXEC_XSAVES;
5690 if (vmx_rdtscp_supported()) {
5691 bool rdtscp_enabled = guest_cpuid_has(vcpu, X86_FEATURE_RDTSCP);
5692 if (!rdtscp_enabled)
5693 exec_control &= ~SECONDARY_EXEC_RDTSCP;
5697 vmx->nested.nested_vmx_secondary_ctls_high |=
5698 SECONDARY_EXEC_RDTSCP;
5700 vmx->nested.nested_vmx_secondary_ctls_high &=
5701 ~SECONDARY_EXEC_RDTSCP;
5705 if (vmx_invpcid_supported()) {
5706 /* Exposing INVPCID only when PCID is exposed */
5707 bool invpcid_enabled =
5708 guest_cpuid_has(vcpu, X86_FEATURE_INVPCID) &&
5709 guest_cpuid_has(vcpu, X86_FEATURE_PCID);
5711 if (!invpcid_enabled) {
5712 exec_control &= ~SECONDARY_EXEC_ENABLE_INVPCID;
5713 guest_cpuid_clear(vcpu, X86_FEATURE_INVPCID);
5717 if (invpcid_enabled)
5718 vmx->nested.nested_vmx_secondary_ctls_high |=
5719 SECONDARY_EXEC_ENABLE_INVPCID;
5721 vmx->nested.nested_vmx_secondary_ctls_high &=
5722 ~SECONDARY_EXEC_ENABLE_INVPCID;
5726 if (vmx_rdrand_supported()) {
5727 bool rdrand_enabled = guest_cpuid_has(vcpu, X86_FEATURE_RDRAND);
5729 exec_control &= ~SECONDARY_EXEC_RDRAND;
5733 vmx->nested.nested_vmx_secondary_ctls_high |=
5734 SECONDARY_EXEC_RDRAND;
5736 vmx->nested.nested_vmx_secondary_ctls_high &=
5737 ~SECONDARY_EXEC_RDRAND;
5741 if (vmx_rdseed_supported()) {
5742 bool rdseed_enabled = guest_cpuid_has(vcpu, X86_FEATURE_RDSEED);
5744 exec_control &= ~SECONDARY_EXEC_RDSEED;
5748 vmx->nested.nested_vmx_secondary_ctls_high |=
5749 SECONDARY_EXEC_RDSEED;
5751 vmx->nested.nested_vmx_secondary_ctls_high &=
5752 ~SECONDARY_EXEC_RDSEED;
5756 vmx->secondary_exec_control = exec_control;
5759 static void ept_set_mmio_spte_mask(void)
5762 * EPT Misconfigurations can be generated if the value of bits 2:0
5763 * of an EPT paging-structure entry is 110b (write/execute).
5765 kvm_mmu_set_mmio_spte_mask(VMX_EPT_RWX_MASK,
5766 VMX_EPT_MISCONFIG_WX_VALUE);
5769 #define VMX_XSS_EXIT_BITMAP 0
5771 * Sets up the vmcs for emulated real mode.
5773 static int vmx_vcpu_setup(struct vcpu_vmx *vmx)
5775 #ifdef CONFIG_X86_64
5781 vmcs_write64(IO_BITMAP_A, __pa(vmx_io_bitmap_a));
5782 vmcs_write64(IO_BITMAP_B, __pa(vmx_io_bitmap_b));
5784 if (enable_shadow_vmcs) {
5785 vmcs_write64(VMREAD_BITMAP, __pa(vmx_vmread_bitmap));
5786 vmcs_write64(VMWRITE_BITMAP, __pa(vmx_vmwrite_bitmap));
5788 if (cpu_has_vmx_msr_bitmap())
5789 vmcs_write64(MSR_BITMAP, __pa(vmx->vmcs01.msr_bitmap));
5791 vmcs_write64(VMCS_LINK_POINTER, -1ull); /* 22.3.1.5 */
5794 vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, vmx_pin_based_exec_ctrl(vmx));
5795 vmx->hv_deadline_tsc = -1;
5797 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, vmx_exec_control(vmx));
5799 if (cpu_has_secondary_exec_ctrls()) {
5800 vmx_compute_secondary_exec_control(vmx);
5801 vmcs_write32(SECONDARY_VM_EXEC_CONTROL,
5802 vmx->secondary_exec_control);
5805 if (kvm_vcpu_apicv_active(&vmx->vcpu)) {
5806 vmcs_write64(EOI_EXIT_BITMAP0, 0);
5807 vmcs_write64(EOI_EXIT_BITMAP1, 0);
5808 vmcs_write64(EOI_EXIT_BITMAP2, 0);
5809 vmcs_write64(EOI_EXIT_BITMAP3, 0);
5811 vmcs_write16(GUEST_INTR_STATUS, 0);
5813 vmcs_write16(POSTED_INTR_NV, POSTED_INTR_VECTOR);
5814 vmcs_write64(POSTED_INTR_DESC_ADDR, __pa((&vmx->pi_desc)));
5818 vmcs_write32(PLE_GAP, ple_gap);
5819 vmx->ple_window = ple_window;
5820 vmx->ple_window_dirty = true;
5823 vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK, 0);
5824 vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH, 0);
5825 vmcs_write32(CR3_TARGET_COUNT, 0); /* 22.2.1 */
5827 vmcs_write16(HOST_FS_SELECTOR, 0); /* 22.2.4 */
5828 vmcs_write16(HOST_GS_SELECTOR, 0); /* 22.2.4 */
5829 vmx_set_constant_host_state(vmx);
5830 #ifdef CONFIG_X86_64
5831 rdmsrl(MSR_FS_BASE, a);
5832 vmcs_writel(HOST_FS_BASE, a); /* 22.2.4 */
5833 rdmsrl(MSR_GS_BASE, a);
5834 vmcs_writel(HOST_GS_BASE, a); /* 22.2.4 */
5836 vmcs_writel(HOST_FS_BASE, 0); /* 22.2.4 */
5837 vmcs_writel(HOST_GS_BASE, 0); /* 22.2.4 */
5840 if (cpu_has_vmx_vmfunc())
5841 vmcs_write64(VM_FUNCTION_CONTROL, 0);
5843 vmcs_write32(VM_EXIT_MSR_STORE_COUNT, 0);
5844 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, 0);
5845 vmcs_write64(VM_EXIT_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.host.val));
5846 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, 0);
5847 vmcs_write64(VM_ENTRY_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.guest.val));
5849 if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT)
5850 vmcs_write64(GUEST_IA32_PAT, vmx->vcpu.arch.pat);
5852 for (i = 0; i < ARRAY_SIZE(vmx_msr_index); ++i) {
5853 u32 index = vmx_msr_index[i];
5854 u32 data_low, data_high;
5857 if (rdmsr_safe(index, &data_low, &data_high) < 0)
5859 if (wrmsr_safe(index, data_low, data_high) < 0)
5861 vmx->guest_msrs[j].index = i;
5862 vmx->guest_msrs[j].data = 0;
5863 vmx->guest_msrs[j].mask = -1ull;
5867 if (boot_cpu_has(X86_FEATURE_ARCH_CAPABILITIES))
5868 rdmsrl(MSR_IA32_ARCH_CAPABILITIES, vmx->arch_capabilities);
5870 vm_exit_controls_init(vmx, vmcs_config.vmexit_ctrl);
5872 /* 22.2.1, 20.8.1 */
5873 vm_entry_controls_init(vmx, vmcs_config.vmentry_ctrl);
5875 vmx->vcpu.arch.cr0_guest_owned_bits = X86_CR0_TS;
5876 vmcs_writel(CR0_GUEST_HOST_MASK, ~X86_CR0_TS);
5878 set_cr4_guest_host_mask(vmx);
5880 if (vmx_xsaves_supported())
5881 vmcs_write64(XSS_EXIT_BITMAP, VMX_XSS_EXIT_BITMAP);
5884 ASSERT(vmx->pml_pg);
5885 vmcs_write64(PML_ADDRESS, page_to_phys(vmx->pml_pg));
5886 vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1);
5892 static void vmx_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event)
5894 struct vcpu_vmx *vmx = to_vmx(vcpu);
5895 struct msr_data apic_base_msr;
5898 vmx->rmode.vm86_active = 0;
5901 vmx->vcpu.arch.regs[VCPU_REGS_RDX] = get_rdx_init_val();
5902 kvm_set_cr8(vcpu, 0);
5905 apic_base_msr.data = APIC_DEFAULT_PHYS_BASE |
5906 MSR_IA32_APICBASE_ENABLE;
5907 if (kvm_vcpu_is_reset_bsp(vcpu))
5908 apic_base_msr.data |= MSR_IA32_APICBASE_BSP;
5909 apic_base_msr.host_initiated = true;
5910 kvm_set_apic_base(vcpu, &apic_base_msr);
5913 vmx_segment_cache_clear(vmx);
5915 seg_setup(VCPU_SREG_CS);
5916 vmcs_write16(GUEST_CS_SELECTOR, 0xf000);
5917 vmcs_writel(GUEST_CS_BASE, 0xffff0000ul);
5919 seg_setup(VCPU_SREG_DS);
5920 seg_setup(VCPU_SREG_ES);
5921 seg_setup(VCPU_SREG_FS);
5922 seg_setup(VCPU_SREG_GS);
5923 seg_setup(VCPU_SREG_SS);
5925 vmcs_write16(GUEST_TR_SELECTOR, 0);
5926 vmcs_writel(GUEST_TR_BASE, 0);
5927 vmcs_write32(GUEST_TR_LIMIT, 0xffff);
5928 vmcs_write32(GUEST_TR_AR_BYTES, 0x008b);
5930 vmcs_write16(GUEST_LDTR_SELECTOR, 0);
5931 vmcs_writel(GUEST_LDTR_BASE, 0);
5932 vmcs_write32(GUEST_LDTR_LIMIT, 0xffff);
5933 vmcs_write32(GUEST_LDTR_AR_BYTES, 0x00082);
5936 vmcs_write32(GUEST_SYSENTER_CS, 0);
5937 vmcs_writel(GUEST_SYSENTER_ESP, 0);
5938 vmcs_writel(GUEST_SYSENTER_EIP, 0);
5939 vmcs_write64(GUEST_IA32_DEBUGCTL, 0);
5942 kvm_set_rflags(vcpu, X86_EFLAGS_FIXED);
5943 kvm_rip_write(vcpu, 0xfff0);
5945 vmcs_writel(GUEST_GDTR_BASE, 0);
5946 vmcs_write32(GUEST_GDTR_LIMIT, 0xffff);
5948 vmcs_writel(GUEST_IDTR_BASE, 0);
5949 vmcs_write32(GUEST_IDTR_LIMIT, 0xffff);
5951 vmcs_write32(GUEST_ACTIVITY_STATE, GUEST_ACTIVITY_ACTIVE);
5952 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, 0);
5953 vmcs_writel(GUEST_PENDING_DBG_EXCEPTIONS, 0);
5957 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0); /* 22.2.1 */
5959 if (cpu_has_vmx_tpr_shadow() && !init_event) {
5960 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, 0);
5961 if (cpu_need_tpr_shadow(vcpu))
5962 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR,
5963 __pa(vcpu->arch.apic->regs));
5964 vmcs_write32(TPR_THRESHOLD, 0);
5967 kvm_make_request(KVM_REQ_APIC_PAGE_RELOAD, vcpu);
5970 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid);
5972 cr0 = X86_CR0_NW | X86_CR0_CD | X86_CR0_ET;
5973 vmx->vcpu.arch.cr0 = cr0;
5974 vmx_set_cr0(vcpu, cr0); /* enter rmode */
5975 vmx_set_cr4(vcpu, 0);
5976 vmx_set_efer(vcpu, 0);
5978 update_exception_bitmap(vcpu);
5980 vpid_sync_context(vmx->vpid);
5984 * In nested virtualization, check if L1 asked to exit on external interrupts.
5985 * For most existing hypervisors, this will always return true.
5987 static bool nested_exit_on_intr(struct kvm_vcpu *vcpu)
5989 return get_vmcs12(vcpu)->pin_based_vm_exec_control &
5990 PIN_BASED_EXT_INTR_MASK;
5994 * In nested virtualization, check if L1 has set
5995 * VM_EXIT_ACK_INTR_ON_EXIT
5997 static bool nested_exit_intr_ack_set(struct kvm_vcpu *vcpu)
5999 return get_vmcs12(vcpu)->vm_exit_controls &
6000 VM_EXIT_ACK_INTR_ON_EXIT;
6003 static bool nested_exit_on_nmi(struct kvm_vcpu *vcpu)
6005 return get_vmcs12(vcpu)->pin_based_vm_exec_control &
6006 PIN_BASED_NMI_EXITING;
6009 static void enable_irq_window(struct kvm_vcpu *vcpu)
6011 vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL,
6012 CPU_BASED_VIRTUAL_INTR_PENDING);
6015 static void enable_nmi_window(struct kvm_vcpu *vcpu)
6017 if (!cpu_has_virtual_nmis() ||
6018 vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & GUEST_INTR_STATE_STI) {
6019 enable_irq_window(vcpu);
6023 vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL,
6024 CPU_BASED_VIRTUAL_NMI_PENDING);
6027 static void vmx_inject_irq(struct kvm_vcpu *vcpu)
6029 struct vcpu_vmx *vmx = to_vmx(vcpu);
6031 int irq = vcpu->arch.interrupt.nr;
6033 trace_kvm_inj_virq(irq);
6035 ++vcpu->stat.irq_injections;
6036 if (vmx->rmode.vm86_active) {
6038 if (vcpu->arch.interrupt.soft)
6039 inc_eip = vcpu->arch.event_exit_inst_len;
6040 if (kvm_inject_realmode_interrupt(vcpu, irq, inc_eip) != EMULATE_DONE)
6041 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
6044 intr = irq | INTR_INFO_VALID_MASK;
6045 if (vcpu->arch.interrupt.soft) {
6046 intr |= INTR_TYPE_SOFT_INTR;
6047 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
6048 vmx->vcpu.arch.event_exit_inst_len);
6050 intr |= INTR_TYPE_EXT_INTR;
6051 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr);
6054 static void vmx_inject_nmi(struct kvm_vcpu *vcpu)
6056 struct vcpu_vmx *vmx = to_vmx(vcpu);
6058 if (!cpu_has_virtual_nmis()) {
6060 * Tracking the NMI-blocked state in software is built upon
6061 * finding the next open IRQ window. This, in turn, depends on
6062 * well-behaving guests: They have to keep IRQs disabled at
6063 * least as long as the NMI handler runs. Otherwise we may
6064 * cause NMI nesting, maybe breaking the guest. But as this is
6065 * highly unlikely, we can live with the residual risk.
6067 vmx->loaded_vmcs->soft_vnmi_blocked = 1;
6068 vmx->loaded_vmcs->vnmi_blocked_time = 0;
6071 ++vcpu->stat.nmi_injections;
6072 vmx->loaded_vmcs->nmi_known_unmasked = false;
6074 if (vmx->rmode.vm86_active) {
6075 if (kvm_inject_realmode_interrupt(vcpu, NMI_VECTOR, 0) != EMULATE_DONE)
6076 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
6080 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
6081 INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK | NMI_VECTOR);
6084 static bool vmx_get_nmi_mask(struct kvm_vcpu *vcpu)
6086 struct vcpu_vmx *vmx = to_vmx(vcpu);
6089 if (!cpu_has_virtual_nmis())
6090 return vmx->loaded_vmcs->soft_vnmi_blocked;
6091 if (vmx->loaded_vmcs->nmi_known_unmasked)
6093 masked = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & GUEST_INTR_STATE_NMI;
6094 vmx->loaded_vmcs->nmi_known_unmasked = !masked;
6098 static void vmx_set_nmi_mask(struct kvm_vcpu *vcpu, bool masked)
6100 struct vcpu_vmx *vmx = to_vmx(vcpu);
6102 if (!cpu_has_virtual_nmis()) {
6103 if (vmx->loaded_vmcs->soft_vnmi_blocked != masked) {
6104 vmx->loaded_vmcs->soft_vnmi_blocked = masked;
6105 vmx->loaded_vmcs->vnmi_blocked_time = 0;
6108 vmx->loaded_vmcs->nmi_known_unmasked = !masked;
6110 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
6111 GUEST_INTR_STATE_NMI);
6113 vmcs_clear_bits(GUEST_INTERRUPTIBILITY_INFO,
6114 GUEST_INTR_STATE_NMI);
6118 static int vmx_nmi_allowed(struct kvm_vcpu *vcpu)
6120 if (to_vmx(vcpu)->nested.nested_run_pending)
6123 if (!cpu_has_virtual_nmis() &&
6124 to_vmx(vcpu)->loaded_vmcs->soft_vnmi_blocked)
6127 return !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) &
6128 (GUEST_INTR_STATE_MOV_SS | GUEST_INTR_STATE_STI
6129 | GUEST_INTR_STATE_NMI));
6132 static int vmx_interrupt_allowed(struct kvm_vcpu *vcpu)
6134 return (!to_vmx(vcpu)->nested.nested_run_pending &&
6135 vmcs_readl(GUEST_RFLAGS) & X86_EFLAGS_IF) &&
6136 !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) &
6137 (GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS));
6140 static int vmx_set_tss_addr(struct kvm *kvm, unsigned int addr)
6144 ret = x86_set_memory_region(kvm, TSS_PRIVATE_MEMSLOT, addr,
6148 kvm->arch.tss_addr = addr;
6149 return init_rmode_tss(kvm);
6152 static bool rmode_exception(struct kvm_vcpu *vcpu, int vec)
6157 * Update instruction length as we may reinject the exception
6158 * from user space while in guest debugging mode.
6160 to_vmx(vcpu)->vcpu.arch.event_exit_inst_len =
6161 vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
6162 if (vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
6166 if (vcpu->guest_debug &
6167 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))
6184 static int handle_rmode_exception(struct kvm_vcpu *vcpu,
6185 int vec, u32 err_code)
6188 * Instruction with address size override prefix opcode 0x67
6189 * Cause the #SS fault with 0 error code in VM86 mode.
6191 if (((vec == GP_VECTOR) || (vec == SS_VECTOR)) && err_code == 0) {
6192 if (emulate_instruction(vcpu, 0) == EMULATE_DONE) {
6193 if (vcpu->arch.halt_request) {
6194 vcpu->arch.halt_request = 0;
6195 return kvm_vcpu_halt(vcpu);
6203 * Forward all other exceptions that are valid in real mode.
6204 * FIXME: Breaks guest debugging in real mode, needs to be fixed with
6205 * the required debugging infrastructure rework.
6207 kvm_queue_exception(vcpu, vec);
6212 * Trigger machine check on the host. We assume all the MSRs are already set up
6213 * by the CPU and that we still run on the same CPU as the MCE occurred on.
6214 * We pass a fake environment to the machine check handler because we want
6215 * the guest to be always treated like user space, no matter what context
6216 * it used internally.
6218 static void kvm_machine_check(void)
6220 #if defined(CONFIG_X86_MCE) && defined(CONFIG_X86_64)
6221 struct pt_regs regs = {
6222 .cs = 3, /* Fake ring 3 no matter what the guest ran on */
6223 .flags = X86_EFLAGS_IF,
6226 do_machine_check(®s, 0);
6230 static int handle_machine_check(struct kvm_vcpu *vcpu)
6232 /* already handled by vcpu_run */
6236 static int handle_exception(struct kvm_vcpu *vcpu)
6238 struct vcpu_vmx *vmx = to_vmx(vcpu);
6239 struct kvm_run *kvm_run = vcpu->run;
6240 u32 intr_info, ex_no, error_code;
6241 unsigned long cr2, rip, dr6;
6243 enum emulation_result er;
6245 vect_info = vmx->idt_vectoring_info;
6246 intr_info = vmx->exit_intr_info;
6248 if (is_machine_check(intr_info))
6249 return handle_machine_check(vcpu);
6251 if (is_nmi(intr_info))
6252 return 1; /* already handled by vmx_vcpu_run() */
6254 if (is_invalid_opcode(intr_info)) {
6255 er = emulate_instruction(vcpu, EMULTYPE_TRAP_UD);
6256 if (er == EMULATE_USER_EXIT)
6258 if (er != EMULATE_DONE)
6259 kvm_queue_exception(vcpu, UD_VECTOR);
6264 if (intr_info & INTR_INFO_DELIVER_CODE_MASK)
6265 error_code = vmcs_read32(VM_EXIT_INTR_ERROR_CODE);
6268 * The #PF with PFEC.RSVD = 1 indicates the guest is accessing
6269 * MMIO, it is better to report an internal error.
6270 * See the comments in vmx_handle_exit.
6272 if ((vect_info & VECTORING_INFO_VALID_MASK) &&
6273 !(is_page_fault(intr_info) && !(error_code & PFERR_RSVD_MASK))) {
6274 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
6275 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_SIMUL_EX;
6276 vcpu->run->internal.ndata = 3;
6277 vcpu->run->internal.data[0] = vect_info;
6278 vcpu->run->internal.data[1] = intr_info;
6279 vcpu->run->internal.data[2] = error_code;
6283 if (is_page_fault(intr_info)) {
6284 cr2 = vmcs_readl(EXIT_QUALIFICATION);
6285 /* EPT won't cause page fault directly */
6286 WARN_ON_ONCE(!vcpu->arch.apf.host_apf_reason && enable_ept);
6287 return kvm_handle_page_fault(vcpu, error_code, cr2, NULL, 0,
6291 ex_no = intr_info & INTR_INFO_VECTOR_MASK;
6293 if (vmx->rmode.vm86_active && rmode_exception(vcpu, ex_no))
6294 return handle_rmode_exception(vcpu, ex_no, error_code);
6298 kvm_queue_exception_e(vcpu, AC_VECTOR, error_code);
6301 dr6 = vmcs_readl(EXIT_QUALIFICATION);
6302 if (!(vcpu->guest_debug &
6303 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))) {
6304 vcpu->arch.dr6 &= ~15;
6305 vcpu->arch.dr6 |= dr6 | DR6_RTM;
6306 if (is_icebp(intr_info))
6307 skip_emulated_instruction(vcpu);
6309 kvm_queue_exception(vcpu, DB_VECTOR);
6312 kvm_run->debug.arch.dr6 = dr6 | DR6_FIXED_1;
6313 kvm_run->debug.arch.dr7 = vmcs_readl(GUEST_DR7);
6317 * Update instruction length as we may reinject #BP from
6318 * user space while in guest debugging mode. Reading it for
6319 * #DB as well causes no harm, it is not used in that case.
6321 vmx->vcpu.arch.event_exit_inst_len =
6322 vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
6323 kvm_run->exit_reason = KVM_EXIT_DEBUG;
6324 rip = kvm_rip_read(vcpu);
6325 kvm_run->debug.arch.pc = vmcs_readl(GUEST_CS_BASE) + rip;
6326 kvm_run->debug.arch.exception = ex_no;
6329 kvm_run->exit_reason = KVM_EXIT_EXCEPTION;
6330 kvm_run->ex.exception = ex_no;
6331 kvm_run->ex.error_code = error_code;
6337 static int handle_external_interrupt(struct kvm_vcpu *vcpu)
6339 ++vcpu->stat.irq_exits;
6343 static int handle_triple_fault(struct kvm_vcpu *vcpu)
6345 vcpu->run->exit_reason = KVM_EXIT_SHUTDOWN;
6346 vcpu->mmio_needed = 0;
6350 static int handle_io(struct kvm_vcpu *vcpu)
6352 unsigned long exit_qualification;
6353 int size, in, string, ret;
6356 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
6357 string = (exit_qualification & 16) != 0;
6358 in = (exit_qualification & 8) != 0;
6360 ++vcpu->stat.io_exits;
6363 return emulate_instruction(vcpu, 0) == EMULATE_DONE;
6365 port = exit_qualification >> 16;
6366 size = (exit_qualification & 7) + 1;
6368 ret = kvm_skip_emulated_instruction(vcpu);
6371 * TODO: we might be squashing a KVM_GUESTDBG_SINGLESTEP-triggered
6372 * KVM_EXIT_DEBUG here.
6374 return kvm_fast_pio_out(vcpu, size, port) && ret;
6378 vmx_patch_hypercall(struct kvm_vcpu *vcpu, unsigned char *hypercall)
6381 * Patch in the VMCALL instruction:
6383 hypercall[0] = 0x0f;
6384 hypercall[1] = 0x01;
6385 hypercall[2] = 0xc1;
6388 /* called to set cr0 as appropriate for a mov-to-cr0 exit. */
6389 static int handle_set_cr0(struct kvm_vcpu *vcpu, unsigned long val)
6391 if (is_guest_mode(vcpu)) {
6392 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
6393 unsigned long orig_val = val;
6396 * We get here when L2 changed cr0 in a way that did not change
6397 * any of L1's shadowed bits (see nested_vmx_exit_handled_cr),
6398 * but did change L0 shadowed bits. So we first calculate the
6399 * effective cr0 value that L1 would like to write into the
6400 * hardware. It consists of the L2-owned bits from the new
6401 * value combined with the L1-owned bits from L1's guest_cr0.
6403 val = (val & ~vmcs12->cr0_guest_host_mask) |
6404 (vmcs12->guest_cr0 & vmcs12->cr0_guest_host_mask);
6406 if (!nested_guest_cr0_valid(vcpu, val))
6409 if (kvm_set_cr0(vcpu, val))
6411 vmcs_writel(CR0_READ_SHADOW, orig_val);
6414 if (to_vmx(vcpu)->nested.vmxon &&
6415 !nested_host_cr0_valid(vcpu, val))
6418 return kvm_set_cr0(vcpu, val);
6422 static int handle_set_cr4(struct kvm_vcpu *vcpu, unsigned long val)
6424 if (is_guest_mode(vcpu)) {
6425 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
6426 unsigned long orig_val = val;
6428 /* analogously to handle_set_cr0 */
6429 val = (val & ~vmcs12->cr4_guest_host_mask) |
6430 (vmcs12->guest_cr4 & vmcs12->cr4_guest_host_mask);
6431 if (kvm_set_cr4(vcpu, val))
6433 vmcs_writel(CR4_READ_SHADOW, orig_val);
6436 return kvm_set_cr4(vcpu, val);
6439 static int handle_cr(struct kvm_vcpu *vcpu)
6441 unsigned long exit_qualification, val;
6447 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
6448 cr = exit_qualification & 15;
6449 reg = (exit_qualification >> 8) & 15;
6450 switch ((exit_qualification >> 4) & 3) {
6451 case 0: /* mov to cr */
6452 val = kvm_register_readl(vcpu, reg);
6453 trace_kvm_cr_write(cr, val);
6456 err = handle_set_cr0(vcpu, val);
6457 return kvm_complete_insn_gp(vcpu, err);
6459 err = kvm_set_cr3(vcpu, val);
6460 return kvm_complete_insn_gp(vcpu, err);
6462 err = handle_set_cr4(vcpu, val);
6463 return kvm_complete_insn_gp(vcpu, err);
6465 u8 cr8_prev = kvm_get_cr8(vcpu);
6467 err = kvm_set_cr8(vcpu, cr8);
6468 ret = kvm_complete_insn_gp(vcpu, err);
6469 if (lapic_in_kernel(vcpu))
6471 if (cr8_prev <= cr8)
6474 * TODO: we might be squashing a
6475 * KVM_GUESTDBG_SINGLESTEP-triggered
6476 * KVM_EXIT_DEBUG here.
6478 vcpu->run->exit_reason = KVM_EXIT_SET_TPR;
6484 WARN_ONCE(1, "Guest should always own CR0.TS");
6485 vmx_set_cr0(vcpu, kvm_read_cr0_bits(vcpu, ~X86_CR0_TS));
6486 trace_kvm_cr_write(0, kvm_read_cr0(vcpu));
6487 return kvm_skip_emulated_instruction(vcpu);
6488 case 1: /*mov from cr*/
6491 val = kvm_read_cr3(vcpu);
6492 kvm_register_write(vcpu, reg, val);
6493 trace_kvm_cr_read(cr, val);
6494 return kvm_skip_emulated_instruction(vcpu);
6496 val = kvm_get_cr8(vcpu);
6497 kvm_register_write(vcpu, reg, val);
6498 trace_kvm_cr_read(cr, val);
6499 return kvm_skip_emulated_instruction(vcpu);
6503 val = (exit_qualification >> LMSW_SOURCE_DATA_SHIFT) & 0x0f;
6504 trace_kvm_cr_write(0, (kvm_read_cr0(vcpu) & ~0xful) | val);
6505 kvm_lmsw(vcpu, val);
6507 return kvm_skip_emulated_instruction(vcpu);
6511 vcpu->run->exit_reason = 0;
6512 vcpu_unimpl(vcpu, "unhandled control register: op %d cr %d\n",
6513 (int)(exit_qualification >> 4) & 3, cr);
6517 static int handle_dr(struct kvm_vcpu *vcpu)
6519 unsigned long exit_qualification;
6522 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
6523 dr = exit_qualification & DEBUG_REG_ACCESS_NUM;
6525 /* First, if DR does not exist, trigger UD */
6526 if (!kvm_require_dr(vcpu, dr))
6529 /* Do not handle if the CPL > 0, will trigger GP on re-entry */
6530 if (!kvm_require_cpl(vcpu, 0))
6532 dr7 = vmcs_readl(GUEST_DR7);
6535 * As the vm-exit takes precedence over the debug trap, we
6536 * need to emulate the latter, either for the host or the
6537 * guest debugging itself.
6539 if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP) {
6540 vcpu->run->debug.arch.dr6 = vcpu->arch.dr6;
6541 vcpu->run->debug.arch.dr7 = dr7;
6542 vcpu->run->debug.arch.pc = kvm_get_linear_rip(vcpu);
6543 vcpu->run->debug.arch.exception = DB_VECTOR;
6544 vcpu->run->exit_reason = KVM_EXIT_DEBUG;
6547 vcpu->arch.dr6 &= ~15;
6548 vcpu->arch.dr6 |= DR6_BD | DR6_RTM;
6549 kvm_queue_exception(vcpu, DB_VECTOR);
6554 if (vcpu->guest_debug == 0) {
6555 vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL,
6556 CPU_BASED_MOV_DR_EXITING);
6559 * No more DR vmexits; force a reload of the debug registers
6560 * and reenter on this instruction. The next vmexit will
6561 * retrieve the full state of the debug registers.
6563 vcpu->arch.switch_db_regs |= KVM_DEBUGREG_WONT_EXIT;
6567 reg = DEBUG_REG_ACCESS_REG(exit_qualification);
6568 if (exit_qualification & TYPE_MOV_FROM_DR) {
6571 if (kvm_get_dr(vcpu, dr, &val))
6573 kvm_register_write(vcpu, reg, val);
6575 if (kvm_set_dr(vcpu, dr, kvm_register_readl(vcpu, reg)))
6578 return kvm_skip_emulated_instruction(vcpu);
6581 static u64 vmx_get_dr6(struct kvm_vcpu *vcpu)
6583 return vcpu->arch.dr6;
6586 static void vmx_set_dr6(struct kvm_vcpu *vcpu, unsigned long val)
6590 static void vmx_sync_dirty_debug_regs(struct kvm_vcpu *vcpu)
6592 get_debugreg(vcpu->arch.db[0], 0);
6593 get_debugreg(vcpu->arch.db[1], 1);
6594 get_debugreg(vcpu->arch.db[2], 2);
6595 get_debugreg(vcpu->arch.db[3], 3);
6596 get_debugreg(vcpu->arch.dr6, 6);
6597 vcpu->arch.dr7 = vmcs_readl(GUEST_DR7);
6599 vcpu->arch.switch_db_regs &= ~KVM_DEBUGREG_WONT_EXIT;
6600 vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL, CPU_BASED_MOV_DR_EXITING);
6603 static void vmx_set_dr7(struct kvm_vcpu *vcpu, unsigned long val)
6605 vmcs_writel(GUEST_DR7, val);
6608 static int handle_cpuid(struct kvm_vcpu *vcpu)
6610 return kvm_emulate_cpuid(vcpu);
6613 static int handle_rdmsr(struct kvm_vcpu *vcpu)
6615 u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX];
6616 struct msr_data msr_info;
6618 msr_info.index = ecx;
6619 msr_info.host_initiated = false;
6620 if (vmx_get_msr(vcpu, &msr_info)) {
6621 trace_kvm_msr_read_ex(ecx);
6622 kvm_inject_gp(vcpu, 0);
6626 trace_kvm_msr_read(ecx, msr_info.data);
6628 /* FIXME: handling of bits 32:63 of rax, rdx */
6629 vcpu->arch.regs[VCPU_REGS_RAX] = msr_info.data & -1u;
6630 vcpu->arch.regs[VCPU_REGS_RDX] = (msr_info.data >> 32) & -1u;
6631 return kvm_skip_emulated_instruction(vcpu);
6634 static int handle_wrmsr(struct kvm_vcpu *vcpu)
6636 struct msr_data msr;
6637 u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX];
6638 u64 data = (vcpu->arch.regs[VCPU_REGS_RAX] & -1u)
6639 | ((u64)(vcpu->arch.regs[VCPU_REGS_RDX] & -1u) << 32);
6643 msr.host_initiated = false;
6644 if (kvm_set_msr(vcpu, &msr) != 0) {
6645 trace_kvm_msr_write_ex(ecx, data);
6646 kvm_inject_gp(vcpu, 0);
6650 trace_kvm_msr_write(ecx, data);
6651 return kvm_skip_emulated_instruction(vcpu);
6654 static int handle_tpr_below_threshold(struct kvm_vcpu *vcpu)
6656 kvm_apic_update_ppr(vcpu);
6660 static int handle_interrupt_window(struct kvm_vcpu *vcpu)
6662 vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL,
6663 CPU_BASED_VIRTUAL_INTR_PENDING);
6665 kvm_make_request(KVM_REQ_EVENT, vcpu);
6667 ++vcpu->stat.irq_window_exits;
6671 static int handle_halt(struct kvm_vcpu *vcpu)
6673 return kvm_emulate_halt(vcpu);
6676 static int handle_vmcall(struct kvm_vcpu *vcpu)
6678 return kvm_emulate_hypercall(vcpu);
6681 static int handle_invd(struct kvm_vcpu *vcpu)
6683 return emulate_instruction(vcpu, 0) == EMULATE_DONE;
6686 static int handle_invlpg(struct kvm_vcpu *vcpu)
6688 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
6690 kvm_mmu_invlpg(vcpu, exit_qualification);
6691 return kvm_skip_emulated_instruction(vcpu);
6694 static int handle_rdpmc(struct kvm_vcpu *vcpu)
6698 err = kvm_rdpmc(vcpu);
6699 return kvm_complete_insn_gp(vcpu, err);
6702 static int handle_wbinvd(struct kvm_vcpu *vcpu)
6704 return kvm_emulate_wbinvd(vcpu);
6707 static int handle_xsetbv(struct kvm_vcpu *vcpu)
6709 u64 new_bv = kvm_read_edx_eax(vcpu);
6710 u32 index = kvm_register_read(vcpu, VCPU_REGS_RCX);
6712 if (kvm_set_xcr(vcpu, index, new_bv) == 0)
6713 return kvm_skip_emulated_instruction(vcpu);
6717 static int handle_xsaves(struct kvm_vcpu *vcpu)
6719 kvm_skip_emulated_instruction(vcpu);
6720 WARN(1, "this should never happen\n");
6724 static int handle_xrstors(struct kvm_vcpu *vcpu)
6726 kvm_skip_emulated_instruction(vcpu);
6727 WARN(1, "this should never happen\n");
6731 static int handle_apic_access(struct kvm_vcpu *vcpu)
6733 if (likely(fasteoi)) {
6734 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
6735 int access_type, offset;
6737 access_type = exit_qualification & APIC_ACCESS_TYPE;
6738 offset = exit_qualification & APIC_ACCESS_OFFSET;
6740 * Sane guest uses MOV to write EOI, with written value
6741 * not cared. So make a short-circuit here by avoiding
6742 * heavy instruction emulation.
6744 if ((access_type == TYPE_LINEAR_APIC_INST_WRITE) &&
6745 (offset == APIC_EOI)) {
6746 kvm_lapic_set_eoi(vcpu);
6747 return kvm_skip_emulated_instruction(vcpu);
6750 return emulate_instruction(vcpu, 0) == EMULATE_DONE;
6753 static int handle_apic_eoi_induced(struct kvm_vcpu *vcpu)
6755 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
6756 int vector = exit_qualification & 0xff;
6758 /* EOI-induced VM exit is trap-like and thus no need to adjust IP */
6759 kvm_apic_set_eoi_accelerated(vcpu, vector);
6763 static int handle_apic_write(struct kvm_vcpu *vcpu)
6765 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
6766 u32 offset = exit_qualification & 0xfff;
6768 /* APIC-write VM exit is trap-like and thus no need to adjust IP */
6769 kvm_apic_write_nodecode(vcpu, offset);
6773 static int handle_task_switch(struct kvm_vcpu *vcpu)
6775 struct vcpu_vmx *vmx = to_vmx(vcpu);
6776 unsigned long exit_qualification;
6777 bool has_error_code = false;
6780 int reason, type, idt_v, idt_index;
6782 idt_v = (vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK);
6783 idt_index = (vmx->idt_vectoring_info & VECTORING_INFO_VECTOR_MASK);
6784 type = (vmx->idt_vectoring_info & VECTORING_INFO_TYPE_MASK);
6786 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
6788 reason = (u32)exit_qualification >> 30;
6789 if (reason == TASK_SWITCH_GATE && idt_v) {
6791 case INTR_TYPE_NMI_INTR:
6792 vcpu->arch.nmi_injected = false;
6793 vmx_set_nmi_mask(vcpu, true);
6795 case INTR_TYPE_EXT_INTR:
6796 case INTR_TYPE_SOFT_INTR:
6797 kvm_clear_interrupt_queue(vcpu);
6799 case INTR_TYPE_HARD_EXCEPTION:
6800 if (vmx->idt_vectoring_info &
6801 VECTORING_INFO_DELIVER_CODE_MASK) {
6802 has_error_code = true;
6804 vmcs_read32(IDT_VECTORING_ERROR_CODE);
6807 case INTR_TYPE_SOFT_EXCEPTION:
6808 kvm_clear_exception_queue(vcpu);
6814 tss_selector = exit_qualification;
6816 if (!idt_v || (type != INTR_TYPE_HARD_EXCEPTION &&
6817 type != INTR_TYPE_EXT_INTR &&
6818 type != INTR_TYPE_NMI_INTR))
6819 skip_emulated_instruction(vcpu);
6821 if (kvm_task_switch(vcpu, tss_selector,
6822 type == INTR_TYPE_SOFT_INTR ? idt_index : -1, reason,
6823 has_error_code, error_code) == EMULATE_FAIL) {
6824 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
6825 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
6826 vcpu->run->internal.ndata = 0;
6831 * TODO: What about debug traps on tss switch?
6832 * Are we supposed to inject them and update dr6?
6838 static int handle_ept_violation(struct kvm_vcpu *vcpu)
6840 unsigned long exit_qualification;
6844 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
6847 * EPT violation happened while executing iret from NMI,
6848 * "blocked by NMI" bit has to be set before next VM entry.
6849 * There are errata that may cause this bit to not be set:
6852 if (!(to_vmx(vcpu)->idt_vectoring_info & VECTORING_INFO_VALID_MASK) &&
6853 cpu_has_virtual_nmis() &&
6854 (exit_qualification & INTR_INFO_UNBLOCK_NMI))
6855 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO, GUEST_INTR_STATE_NMI);
6857 gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS);
6858 trace_kvm_page_fault(gpa, exit_qualification);
6860 /* Is it a read fault? */
6861 error_code = (exit_qualification & EPT_VIOLATION_ACC_READ)
6862 ? PFERR_USER_MASK : 0;
6863 /* Is it a write fault? */
6864 error_code |= (exit_qualification & EPT_VIOLATION_ACC_WRITE)
6865 ? PFERR_WRITE_MASK : 0;
6866 /* Is it a fetch fault? */
6867 error_code |= (exit_qualification & EPT_VIOLATION_ACC_INSTR)
6868 ? PFERR_FETCH_MASK : 0;
6869 /* ept page table entry is present? */
6870 error_code |= (exit_qualification &
6871 (EPT_VIOLATION_READABLE | EPT_VIOLATION_WRITABLE |
6872 EPT_VIOLATION_EXECUTABLE))
6873 ? PFERR_PRESENT_MASK : 0;
6875 error_code |= (exit_qualification & 0x100) != 0 ?
6876 PFERR_GUEST_FINAL_MASK : PFERR_GUEST_PAGE_MASK;
6878 vcpu->arch.exit_qualification = exit_qualification;
6879 return kvm_mmu_page_fault(vcpu, gpa, error_code, NULL, 0);
6882 static int handle_ept_misconfig(struct kvm_vcpu *vcpu)
6888 * A nested guest cannot optimize MMIO vmexits, because we have an
6889 * nGPA here instead of the required GPA.
6891 gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS);
6892 if (!is_guest_mode(vcpu) &&
6893 !kvm_io_bus_write(vcpu, KVM_FAST_MMIO_BUS, gpa, 0, NULL)) {
6894 trace_kvm_fast_mmio(gpa);
6896 * Doing kvm_skip_emulated_instruction() depends on undefined
6897 * behavior: Intel's manual doesn't mandate
6898 * VM_EXIT_INSTRUCTION_LEN to be set in VMCS when EPT MISCONFIG
6899 * occurs and while on real hardware it was observed to be set,
6900 * other hypervisors (namely Hyper-V) don't set it, we end up
6901 * advancing IP with some random value. Disable fast mmio when
6902 * running nested and keep it for real hardware in hope that
6903 * VM_EXIT_INSTRUCTION_LEN will always be set correctly.
6905 if (!static_cpu_has(X86_FEATURE_HYPERVISOR))
6906 return kvm_skip_emulated_instruction(vcpu);
6908 return x86_emulate_instruction(vcpu, gpa, EMULTYPE_SKIP,
6909 NULL, 0) == EMULATE_DONE;
6912 ret = kvm_mmu_page_fault(vcpu, gpa, PFERR_RSVD_MASK, NULL, 0);
6916 /* It is the real ept misconfig */
6919 vcpu->run->exit_reason = KVM_EXIT_UNKNOWN;
6920 vcpu->run->hw.hardware_exit_reason = EXIT_REASON_EPT_MISCONFIG;
6925 static int handle_nmi_window(struct kvm_vcpu *vcpu)
6927 vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL,
6928 CPU_BASED_VIRTUAL_NMI_PENDING);
6929 ++vcpu->stat.nmi_window_exits;
6930 kvm_make_request(KVM_REQ_EVENT, vcpu);
6935 static int handle_invalid_guest_state(struct kvm_vcpu *vcpu)
6937 struct vcpu_vmx *vmx = to_vmx(vcpu);
6938 enum emulation_result err = EMULATE_DONE;
6941 bool intr_window_requested;
6942 unsigned count = 130;
6944 cpu_exec_ctrl = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
6945 intr_window_requested = cpu_exec_ctrl & CPU_BASED_VIRTUAL_INTR_PENDING;
6947 while (vmx->emulation_required && count-- != 0) {
6948 if (intr_window_requested && vmx_interrupt_allowed(vcpu))
6949 return handle_interrupt_window(&vmx->vcpu);
6951 if (kvm_test_request(KVM_REQ_EVENT, vcpu))
6954 err = emulate_instruction(vcpu, 0);
6956 if (err == EMULATE_USER_EXIT) {
6957 ++vcpu->stat.mmio_exits;
6962 if (err != EMULATE_DONE)
6963 goto emulation_error;
6965 if (vmx->emulation_required && !vmx->rmode.vm86_active &&
6966 vcpu->arch.exception.pending)
6967 goto emulation_error;
6969 if (vcpu->arch.halt_request) {
6970 vcpu->arch.halt_request = 0;
6971 ret = kvm_vcpu_halt(vcpu);
6975 if (signal_pending(current))
6985 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
6986 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
6987 vcpu->run->internal.ndata = 0;
6991 static int __grow_ple_window(int val)
6993 if (ple_window_grow < 1)
6996 val = min(val, ple_window_actual_max);
6998 if (ple_window_grow < ple_window)
6999 val *= ple_window_grow;
7001 val += ple_window_grow;
7006 static int __shrink_ple_window(int val, int modifier, int minimum)
7011 if (modifier < ple_window)
7016 return max(val, minimum);
7019 static void grow_ple_window(struct kvm_vcpu *vcpu)
7021 struct vcpu_vmx *vmx = to_vmx(vcpu);
7022 int old = vmx->ple_window;
7024 vmx->ple_window = __grow_ple_window(old);
7026 if (vmx->ple_window != old)
7027 vmx->ple_window_dirty = true;
7029 trace_kvm_ple_window_grow(vcpu->vcpu_id, vmx->ple_window, old);
7032 static void shrink_ple_window(struct kvm_vcpu *vcpu)
7034 struct vcpu_vmx *vmx = to_vmx(vcpu);
7035 int old = vmx->ple_window;
7037 vmx->ple_window = __shrink_ple_window(old,
7038 ple_window_shrink, ple_window);
7040 if (vmx->ple_window != old)
7041 vmx->ple_window_dirty = true;
7043 trace_kvm_ple_window_shrink(vcpu->vcpu_id, vmx->ple_window, old);
7047 * ple_window_actual_max is computed to be one grow_ple_window() below
7048 * ple_window_max. (See __grow_ple_window for the reason.)
7049 * This prevents overflows, because ple_window_max is int.
7050 * ple_window_max effectively rounded down to a multiple of ple_window_grow in
7052 * ple_window_max is also prevented from setting vmx->ple_window < ple_window.
7054 static void update_ple_window_actual_max(void)
7056 ple_window_actual_max =
7057 __shrink_ple_window(max(ple_window_max, ple_window),
7058 ple_window_grow, INT_MIN);
7062 * Handler for POSTED_INTERRUPT_WAKEUP_VECTOR.
7064 static void wakeup_handler(void)
7066 struct kvm_vcpu *vcpu;
7067 int cpu = smp_processor_id();
7069 spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, cpu));
7070 list_for_each_entry(vcpu, &per_cpu(blocked_vcpu_on_cpu, cpu),
7071 blocked_vcpu_list) {
7072 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
7074 if (pi_test_on(pi_desc) == 1)
7075 kvm_vcpu_kick(vcpu);
7077 spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, cpu));
7080 void vmx_enable_tdp(void)
7082 kvm_mmu_set_mask_ptes(VMX_EPT_READABLE_MASK,
7083 enable_ept_ad_bits ? VMX_EPT_ACCESS_BIT : 0ull,
7084 enable_ept_ad_bits ? VMX_EPT_DIRTY_BIT : 0ull,
7085 0ull, VMX_EPT_EXECUTABLE_MASK,
7086 cpu_has_vmx_ept_execute_only() ? 0ull : VMX_EPT_READABLE_MASK,
7087 VMX_EPT_RWX_MASK, 0ull);
7089 ept_set_mmio_spte_mask();
7093 static __init int hardware_setup(void)
7097 rdmsrl_safe(MSR_EFER, &host_efer);
7099 for (i = 0; i < ARRAY_SIZE(vmx_msr_index); ++i)
7100 kvm_define_shared_msr(i, vmx_msr_index[i]);
7102 for (i = 0; i < VMX_BITMAP_NR; i++) {
7103 vmx_bitmap[i] = (unsigned long *)__get_free_page(GFP_KERNEL);
7108 memset(vmx_vmread_bitmap, 0xff, PAGE_SIZE);
7109 memset(vmx_vmwrite_bitmap, 0xff, PAGE_SIZE);
7111 memset(vmx_io_bitmap_a, 0xff, PAGE_SIZE);
7113 memset(vmx_io_bitmap_b, 0xff, PAGE_SIZE);
7115 if (setup_vmcs_config(&vmcs_config) < 0) {
7120 if (boot_cpu_has(X86_FEATURE_NX))
7121 kvm_enable_efer_bits(EFER_NX);
7123 if (!cpu_has_vmx_vpid() || !cpu_has_vmx_invvpid() ||
7124 !(cpu_has_vmx_invvpid_single() || cpu_has_vmx_invvpid_global()))
7127 if (!cpu_has_vmx_shadow_vmcs())
7128 enable_shadow_vmcs = 0;
7129 if (enable_shadow_vmcs)
7130 init_vmcs_shadow_fields();
7132 if (!cpu_has_vmx_ept() ||
7133 !cpu_has_vmx_ept_4levels() ||
7134 !cpu_has_vmx_ept_mt_wb()) {
7136 enable_unrestricted_guest = 0;
7137 enable_ept_ad_bits = 0;
7140 if (!cpu_has_vmx_ept_ad_bits() || !enable_ept)
7141 enable_ept_ad_bits = 0;
7143 if (!cpu_has_vmx_unrestricted_guest())
7144 enable_unrestricted_guest = 0;
7146 if (!cpu_has_vmx_flexpriority())
7147 flexpriority_enabled = 0;
7150 * set_apic_access_page_addr() is used to reload apic access
7151 * page upon invalidation. No need to do anything if not
7152 * using the APIC_ACCESS_ADDR VMCS field.
7154 if (!flexpriority_enabled)
7155 kvm_x86_ops->set_apic_access_page_addr = NULL;
7157 if (!cpu_has_vmx_tpr_shadow())
7158 kvm_x86_ops->update_cr8_intercept = NULL;
7160 if (enable_ept && !cpu_has_vmx_ept_2m_page())
7161 kvm_disable_largepages();
7163 if (!cpu_has_vmx_ple())
7166 if (!cpu_has_vmx_apicv()) {
7168 kvm_x86_ops->sync_pir_to_irr = NULL;
7171 if (cpu_has_vmx_tsc_scaling()) {
7172 kvm_has_tsc_control = true;
7173 kvm_max_tsc_scaling_ratio = KVM_VMX_TSC_MULTIPLIER_MAX;
7174 kvm_tsc_scaling_ratio_frac_bits = 48;
7177 set_bit(0, vmx_vpid_bitmap); /* 0 is reserved for host */
7184 update_ple_window_actual_max();
7187 * Only enable PML when hardware supports PML feature, and both EPT
7188 * and EPT A/D bit features are enabled -- PML depends on them to work.
7190 if (!enable_ept || !enable_ept_ad_bits || !cpu_has_vmx_pml())
7194 kvm_x86_ops->slot_enable_log_dirty = NULL;
7195 kvm_x86_ops->slot_disable_log_dirty = NULL;
7196 kvm_x86_ops->flush_log_dirty = NULL;
7197 kvm_x86_ops->enable_log_dirty_pt_masked = NULL;
7200 if (cpu_has_vmx_preemption_timer() && enable_preemption_timer) {
7203 rdmsrl(MSR_IA32_VMX_MISC, vmx_msr);
7204 cpu_preemption_timer_multi =
7205 vmx_msr & VMX_MISC_PREEMPTION_TIMER_RATE_MASK;
7207 kvm_x86_ops->set_hv_timer = NULL;
7208 kvm_x86_ops->cancel_hv_timer = NULL;
7211 kvm_set_posted_intr_wakeup_handler(wakeup_handler);
7213 kvm_mce_cap_supported |= MCG_LMCE_P;
7215 return alloc_kvm_area();
7218 for (i = 0; i < VMX_BITMAP_NR; i++)
7219 free_page((unsigned long)vmx_bitmap[i]);
7224 static __exit void hardware_unsetup(void)
7228 for (i = 0; i < VMX_BITMAP_NR; i++)
7229 free_page((unsigned long)vmx_bitmap[i]);
7235 * Indicate a busy-waiting vcpu in spinlock. We do not enable the PAUSE
7236 * exiting, so only get here on cpu with PAUSE-Loop-Exiting.
7238 static int handle_pause(struct kvm_vcpu *vcpu)
7241 grow_ple_window(vcpu);
7244 * Intel sdm vol3 ch-25.1.3 says: The "PAUSE-loop exiting"
7245 * VM-execution control is ignored if CPL > 0. OTOH, KVM
7246 * never set PAUSE_EXITING and just set PLE if supported,
7247 * so the vcpu must be CPL=0 if it gets a PAUSE exit.
7249 kvm_vcpu_on_spin(vcpu, true);
7250 return kvm_skip_emulated_instruction(vcpu);
7253 static int handle_nop(struct kvm_vcpu *vcpu)
7255 return kvm_skip_emulated_instruction(vcpu);
7258 static int handle_mwait(struct kvm_vcpu *vcpu)
7260 printk_once(KERN_WARNING "kvm: MWAIT instruction emulated as NOP!\n");
7261 return handle_nop(vcpu);
7264 static int handle_invalid_op(struct kvm_vcpu *vcpu)
7266 kvm_queue_exception(vcpu, UD_VECTOR);
7270 static int handle_monitor_trap(struct kvm_vcpu *vcpu)
7275 static int handle_monitor(struct kvm_vcpu *vcpu)
7277 printk_once(KERN_WARNING "kvm: MONITOR instruction emulated as NOP!\n");
7278 return handle_nop(vcpu);
7282 * The following 3 functions, nested_vmx_succeed()/failValid()/failInvalid(),
7283 * set the success or error code of an emulated VMX instruction, as specified
7284 * by Vol 2B, VMX Instruction Reference, "Conventions".
7286 static void nested_vmx_succeed(struct kvm_vcpu *vcpu)
7288 vmx_set_rflags(vcpu, vmx_get_rflags(vcpu)
7289 & ~(X86_EFLAGS_CF | X86_EFLAGS_PF | X86_EFLAGS_AF |
7290 X86_EFLAGS_ZF | X86_EFLAGS_SF | X86_EFLAGS_OF));
7293 static void nested_vmx_failInvalid(struct kvm_vcpu *vcpu)
7295 vmx_set_rflags(vcpu, (vmx_get_rflags(vcpu)
7296 & ~(X86_EFLAGS_PF | X86_EFLAGS_AF | X86_EFLAGS_ZF |
7297 X86_EFLAGS_SF | X86_EFLAGS_OF))
7301 static void nested_vmx_failValid(struct kvm_vcpu *vcpu,
7302 u32 vm_instruction_error)
7304 if (to_vmx(vcpu)->nested.current_vmptr == -1ull) {
7306 * failValid writes the error number to the current VMCS, which
7307 * can't be done there isn't a current VMCS.
7309 nested_vmx_failInvalid(vcpu);
7312 vmx_set_rflags(vcpu, (vmx_get_rflags(vcpu)
7313 & ~(X86_EFLAGS_CF | X86_EFLAGS_PF | X86_EFLAGS_AF |
7314 X86_EFLAGS_SF | X86_EFLAGS_OF))
7316 get_vmcs12(vcpu)->vm_instruction_error = vm_instruction_error;
7318 * We don't need to force a shadow sync because
7319 * VM_INSTRUCTION_ERROR is not shadowed
7323 static void nested_vmx_abort(struct kvm_vcpu *vcpu, u32 indicator)
7325 /* TODO: not to reset guest simply here. */
7326 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
7327 pr_debug_ratelimited("kvm: nested vmx abort, indicator %d\n", indicator);
7330 static enum hrtimer_restart vmx_preemption_timer_fn(struct hrtimer *timer)
7332 struct vcpu_vmx *vmx =
7333 container_of(timer, struct vcpu_vmx, nested.preemption_timer);
7335 vmx->nested.preemption_timer_expired = true;
7336 kvm_make_request(KVM_REQ_EVENT, &vmx->vcpu);
7337 kvm_vcpu_kick(&vmx->vcpu);
7339 return HRTIMER_NORESTART;
7343 * Decode the memory-address operand of a vmx instruction, as recorded on an
7344 * exit caused by such an instruction (run by a guest hypervisor).
7345 * On success, returns 0. When the operand is invalid, returns 1 and throws
7348 static int get_vmx_mem_address(struct kvm_vcpu *vcpu,
7349 unsigned long exit_qualification,
7350 u32 vmx_instruction_info, bool wr, gva_t *ret)
7354 struct kvm_segment s;
7357 * According to Vol. 3B, "Information for VM Exits Due to Instruction
7358 * Execution", on an exit, vmx_instruction_info holds most of the
7359 * addressing components of the operand. Only the displacement part
7360 * is put in exit_qualification (see 3B, "Basic VM-Exit Information").
7361 * For how an actual address is calculated from all these components,
7362 * refer to Vol. 1, "Operand Addressing".
7364 int scaling = vmx_instruction_info & 3;
7365 int addr_size = (vmx_instruction_info >> 7) & 7;
7366 bool is_reg = vmx_instruction_info & (1u << 10);
7367 int seg_reg = (vmx_instruction_info >> 15) & 7;
7368 int index_reg = (vmx_instruction_info >> 18) & 0xf;
7369 bool index_is_valid = !(vmx_instruction_info & (1u << 22));
7370 int base_reg = (vmx_instruction_info >> 23) & 0xf;
7371 bool base_is_valid = !(vmx_instruction_info & (1u << 27));
7374 kvm_queue_exception(vcpu, UD_VECTOR);
7378 /* Addr = segment_base + offset */
7379 /* offset = base + [index * scale] + displacement */
7380 off = exit_qualification; /* holds the displacement */
7382 off += kvm_register_read(vcpu, base_reg);
7384 off += kvm_register_read(vcpu, index_reg)<<scaling;
7385 vmx_get_segment(vcpu, &s, seg_reg);
7386 *ret = s.base + off;
7388 if (addr_size == 1) /* 32 bit */
7391 /* Checks for #GP/#SS exceptions. */
7393 if (is_long_mode(vcpu)) {
7394 /* Long mode: #GP(0)/#SS(0) if the memory address is in a
7395 * non-canonical form. This is the only check on the memory
7396 * destination for long mode!
7398 exn = is_noncanonical_address(*ret, vcpu);
7399 } else if (is_protmode(vcpu)) {
7400 /* Protected mode: apply checks for segment validity in the
7402 * - segment type check (#GP(0) may be thrown)
7403 * - usability check (#GP(0)/#SS(0))
7404 * - limit check (#GP(0)/#SS(0))
7407 /* #GP(0) if the destination operand is located in a
7408 * read-only data segment or any code segment.
7410 exn = ((s.type & 0xa) == 0 || (s.type & 8));
7412 /* #GP(0) if the source operand is located in an
7413 * execute-only code segment
7415 exn = ((s.type & 0xa) == 8);
7417 kvm_queue_exception_e(vcpu, GP_VECTOR, 0);
7420 /* Protected mode: #GP(0)/#SS(0) if the segment is unusable.
7422 exn = (s.unusable != 0);
7423 /* Protected mode: #GP(0)/#SS(0) if the memory
7424 * operand is outside the segment limit.
7426 exn = exn || (off + sizeof(u64) > s.limit);
7429 kvm_queue_exception_e(vcpu,
7430 seg_reg == VCPU_SREG_SS ?
7431 SS_VECTOR : GP_VECTOR,
7439 static int nested_vmx_get_vmptr(struct kvm_vcpu *vcpu, gpa_t *vmpointer)
7442 struct x86_exception e;
7444 if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
7445 vmcs_read32(VMX_INSTRUCTION_INFO), false, &gva))
7448 if (kvm_read_guest_virt(vcpu, gva, vmpointer, sizeof(*vmpointer), &e)) {
7449 kvm_inject_page_fault(vcpu, &e);
7456 static int enter_vmx_operation(struct kvm_vcpu *vcpu)
7458 struct vcpu_vmx *vmx = to_vmx(vcpu);
7459 struct vmcs *shadow_vmcs;
7462 r = alloc_loaded_vmcs(&vmx->nested.vmcs02);
7466 vmx->nested.cached_vmcs12 = kmalloc(VMCS12_SIZE, GFP_KERNEL);
7467 if (!vmx->nested.cached_vmcs12)
7468 goto out_cached_vmcs12;
7470 if (enable_shadow_vmcs) {
7471 shadow_vmcs = alloc_vmcs();
7473 goto out_shadow_vmcs;
7474 /* mark vmcs as shadow */
7475 shadow_vmcs->revision_id |= (1u << 31);
7476 /* init shadow vmcs */
7477 vmcs_clear(shadow_vmcs);
7478 vmx->vmcs01.shadow_vmcs = shadow_vmcs;
7481 hrtimer_init(&vmx->nested.preemption_timer, CLOCK_MONOTONIC,
7482 HRTIMER_MODE_REL_PINNED);
7483 vmx->nested.preemption_timer.function = vmx_preemption_timer_fn;
7485 vmx->nested.vpid02 = allocate_vpid();
7487 vmx->nested.vmxon = true;
7491 kfree(vmx->nested.cached_vmcs12);
7494 free_loaded_vmcs(&vmx->nested.vmcs02);
7501 * Emulate the VMXON instruction.
7502 * Currently, we just remember that VMX is active, and do not save or even
7503 * inspect the argument to VMXON (the so-called "VMXON pointer") because we
7504 * do not currently need to store anything in that guest-allocated memory
7505 * region. Consequently, VMCLEAR and VMPTRLD also do not verify that the their
7506 * argument is different from the VMXON pointer (which the spec says they do).
7508 static int handle_vmon(struct kvm_vcpu *vcpu)
7513 struct vcpu_vmx *vmx = to_vmx(vcpu);
7514 const u64 VMXON_NEEDED_FEATURES = FEATURE_CONTROL_LOCKED
7515 | FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
7518 * The Intel VMX Instruction Reference lists a bunch of bits that are
7519 * prerequisite to running VMXON, most notably cr4.VMXE must be set to
7520 * 1 (see vmx_set_cr4() for when we allow the guest to set this).
7521 * Otherwise, we should fail with #UD. But most faulting conditions
7522 * have already been checked by hardware, prior to the VM-exit for
7523 * VMXON. We do test guest cr4.VMXE because processor CR4 always has
7524 * that bit set to 1 in non-root mode.
7526 if (!kvm_read_cr4_bits(vcpu, X86_CR4_VMXE)) {
7527 kvm_queue_exception(vcpu, UD_VECTOR);
7531 /* CPL=0 must be checked manually. */
7532 if (vmx_get_cpl(vcpu)) {
7533 kvm_queue_exception(vcpu, UD_VECTOR);
7537 if (vmx->nested.vmxon) {
7538 nested_vmx_failValid(vcpu, VMXERR_VMXON_IN_VMX_ROOT_OPERATION);
7539 return kvm_skip_emulated_instruction(vcpu);
7542 if ((vmx->msr_ia32_feature_control & VMXON_NEEDED_FEATURES)
7543 != VMXON_NEEDED_FEATURES) {
7544 kvm_inject_gp(vcpu, 0);
7548 if (nested_vmx_get_vmptr(vcpu, &vmptr))
7553 * The first 4 bytes of VMXON region contain the supported
7554 * VMCS revision identifier
7556 * Note - IA32_VMX_BASIC[48] will never be 1 for the nested case;
7557 * which replaces physical address width with 32
7559 if (!PAGE_ALIGNED(vmptr) || (vmptr >> cpuid_maxphyaddr(vcpu))) {
7560 nested_vmx_failInvalid(vcpu);
7561 return kvm_skip_emulated_instruction(vcpu);
7564 page = kvm_vcpu_gpa_to_page(vcpu, vmptr);
7565 if (is_error_page(page)) {
7566 nested_vmx_failInvalid(vcpu);
7567 return kvm_skip_emulated_instruction(vcpu);
7569 if (*(u32 *)kmap(page) != VMCS12_REVISION) {
7571 kvm_release_page_clean(page);
7572 nested_vmx_failInvalid(vcpu);
7573 return kvm_skip_emulated_instruction(vcpu);
7576 kvm_release_page_clean(page);
7578 vmx->nested.vmxon_ptr = vmptr;
7579 ret = enter_vmx_operation(vcpu);
7583 nested_vmx_succeed(vcpu);
7584 return kvm_skip_emulated_instruction(vcpu);
7588 * Intel's VMX Instruction Reference specifies a common set of prerequisites
7589 * for running VMX instructions (except VMXON, whose prerequisites are
7590 * slightly different). It also specifies what exception to inject otherwise.
7591 * Note that many of these exceptions have priority over VM exits, so they
7592 * don't have to be checked again here.
7594 static int nested_vmx_check_permission(struct kvm_vcpu *vcpu)
7596 if (vmx_get_cpl(vcpu)) {
7597 kvm_queue_exception(vcpu, UD_VECTOR);
7601 if (!to_vmx(vcpu)->nested.vmxon) {
7602 kvm_queue_exception(vcpu, UD_VECTOR);
7608 static void vmx_disable_shadow_vmcs(struct vcpu_vmx *vmx)
7610 vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL, SECONDARY_EXEC_SHADOW_VMCS);
7611 vmcs_write64(VMCS_LINK_POINTER, -1ull);
7614 static inline void nested_release_vmcs12(struct vcpu_vmx *vmx)
7616 if (vmx->nested.current_vmptr == -1ull)
7619 if (enable_shadow_vmcs) {
7620 /* copy to memory all shadowed fields in case
7621 they were modified */
7622 copy_shadow_to_vmcs12(vmx);
7623 vmx->nested.sync_shadow_vmcs = false;
7624 vmx_disable_shadow_vmcs(vmx);
7626 vmx->nested.posted_intr_nv = -1;
7628 /* Flush VMCS12 to guest memory */
7629 kvm_vcpu_write_guest_page(&vmx->vcpu,
7630 vmx->nested.current_vmptr >> PAGE_SHIFT,
7631 vmx->nested.cached_vmcs12, 0, VMCS12_SIZE);
7633 vmx->nested.current_vmptr = -1ull;
7637 * Free whatever needs to be freed from vmx->nested when L1 goes down, or
7638 * just stops using VMX.
7640 static void free_nested(struct vcpu_vmx *vmx)
7642 if (!vmx->nested.vmxon)
7645 vmx->nested.vmxon = false;
7646 free_vpid(vmx->nested.vpid02);
7647 vmx->nested.posted_intr_nv = -1;
7648 vmx->nested.current_vmptr = -1ull;
7649 if (enable_shadow_vmcs) {
7650 vmx_disable_shadow_vmcs(vmx);
7651 vmcs_clear(vmx->vmcs01.shadow_vmcs);
7652 free_vmcs(vmx->vmcs01.shadow_vmcs);
7653 vmx->vmcs01.shadow_vmcs = NULL;
7655 kfree(vmx->nested.cached_vmcs12);
7656 /* Unpin physical memory we referred to in the vmcs02 */
7657 if (vmx->nested.apic_access_page) {
7658 kvm_release_page_dirty(vmx->nested.apic_access_page);
7659 vmx->nested.apic_access_page = NULL;
7661 if (vmx->nested.virtual_apic_page) {
7662 kvm_release_page_dirty(vmx->nested.virtual_apic_page);
7663 vmx->nested.virtual_apic_page = NULL;
7665 if (vmx->nested.pi_desc_page) {
7666 kunmap(vmx->nested.pi_desc_page);
7667 kvm_release_page_dirty(vmx->nested.pi_desc_page);
7668 vmx->nested.pi_desc_page = NULL;
7669 vmx->nested.pi_desc = NULL;
7672 free_loaded_vmcs(&vmx->nested.vmcs02);
7675 /* Emulate the VMXOFF instruction */
7676 static int handle_vmoff(struct kvm_vcpu *vcpu)
7678 if (!nested_vmx_check_permission(vcpu))
7680 free_nested(to_vmx(vcpu));
7681 nested_vmx_succeed(vcpu);
7682 return kvm_skip_emulated_instruction(vcpu);
7685 /* Emulate the VMCLEAR instruction */
7686 static int handle_vmclear(struct kvm_vcpu *vcpu)
7688 struct vcpu_vmx *vmx = to_vmx(vcpu);
7692 if (!nested_vmx_check_permission(vcpu))
7695 if (nested_vmx_get_vmptr(vcpu, &vmptr))
7698 if (!PAGE_ALIGNED(vmptr) || (vmptr >> cpuid_maxphyaddr(vcpu))) {
7699 nested_vmx_failValid(vcpu, VMXERR_VMCLEAR_INVALID_ADDRESS);
7700 return kvm_skip_emulated_instruction(vcpu);
7703 if (vmptr == vmx->nested.vmxon_ptr) {
7704 nested_vmx_failValid(vcpu, VMXERR_VMCLEAR_VMXON_POINTER);
7705 return kvm_skip_emulated_instruction(vcpu);
7708 if (vmptr == vmx->nested.current_vmptr)
7709 nested_release_vmcs12(vmx);
7711 kvm_vcpu_write_guest(vcpu,
7712 vmptr + offsetof(struct vmcs12, launch_state),
7713 &zero, sizeof(zero));
7715 nested_vmx_succeed(vcpu);
7716 return kvm_skip_emulated_instruction(vcpu);
7719 static int nested_vmx_run(struct kvm_vcpu *vcpu, bool launch);
7721 /* Emulate the VMLAUNCH instruction */
7722 static int handle_vmlaunch(struct kvm_vcpu *vcpu)
7724 return nested_vmx_run(vcpu, true);
7727 /* Emulate the VMRESUME instruction */
7728 static int handle_vmresume(struct kvm_vcpu *vcpu)
7731 return nested_vmx_run(vcpu, false);
7735 * Read a vmcs12 field. Since these can have varying lengths and we return
7736 * one type, we chose the biggest type (u64) and zero-extend the return value
7737 * to that size. Note that the caller, handle_vmread, might need to use only
7738 * some of the bits we return here (e.g., on 32-bit guests, only 32 bits of
7739 * 64-bit fields are to be returned).
7741 static inline int vmcs12_read_any(struct kvm_vcpu *vcpu,
7742 unsigned long field, u64 *ret)
7744 short offset = vmcs_field_to_offset(field);
7750 p = ((char *)(get_vmcs12(vcpu))) + offset;
7752 switch (vmcs_field_type(field)) {
7753 case VMCS_FIELD_TYPE_NATURAL_WIDTH:
7754 *ret = *((natural_width *)p);
7756 case VMCS_FIELD_TYPE_U16:
7759 case VMCS_FIELD_TYPE_U32:
7762 case VMCS_FIELD_TYPE_U64:
7772 static inline int vmcs12_write_any(struct kvm_vcpu *vcpu,
7773 unsigned long field, u64 field_value){
7774 short offset = vmcs_field_to_offset(field);
7775 char *p = ((char *) get_vmcs12(vcpu)) + offset;
7779 switch (vmcs_field_type(field)) {
7780 case VMCS_FIELD_TYPE_U16:
7781 *(u16 *)p = field_value;
7783 case VMCS_FIELD_TYPE_U32:
7784 *(u32 *)p = field_value;
7786 case VMCS_FIELD_TYPE_U64:
7787 *(u64 *)p = field_value;
7789 case VMCS_FIELD_TYPE_NATURAL_WIDTH:
7790 *(natural_width *)p = field_value;
7799 static void copy_shadow_to_vmcs12(struct vcpu_vmx *vmx)
7802 unsigned long field;
7804 struct vmcs *shadow_vmcs = vmx->vmcs01.shadow_vmcs;
7805 const unsigned long *fields = shadow_read_write_fields;
7806 const int num_fields = max_shadow_read_write_fields;
7810 vmcs_load(shadow_vmcs);
7812 for (i = 0; i < num_fields; i++) {
7814 switch (vmcs_field_type(field)) {
7815 case VMCS_FIELD_TYPE_U16:
7816 field_value = vmcs_read16(field);
7818 case VMCS_FIELD_TYPE_U32:
7819 field_value = vmcs_read32(field);
7821 case VMCS_FIELD_TYPE_U64:
7822 field_value = vmcs_read64(field);
7824 case VMCS_FIELD_TYPE_NATURAL_WIDTH:
7825 field_value = vmcs_readl(field);
7831 vmcs12_write_any(&vmx->vcpu, field, field_value);
7834 vmcs_clear(shadow_vmcs);
7835 vmcs_load(vmx->loaded_vmcs->vmcs);
7840 static void copy_vmcs12_to_shadow(struct vcpu_vmx *vmx)
7842 const unsigned long *fields[] = {
7843 shadow_read_write_fields,
7844 shadow_read_only_fields
7846 const int max_fields[] = {
7847 max_shadow_read_write_fields,
7848 max_shadow_read_only_fields
7851 unsigned long field;
7852 u64 field_value = 0;
7853 struct vmcs *shadow_vmcs = vmx->vmcs01.shadow_vmcs;
7855 vmcs_load(shadow_vmcs);
7857 for (q = 0; q < ARRAY_SIZE(fields); q++) {
7858 for (i = 0; i < max_fields[q]; i++) {
7859 field = fields[q][i];
7860 vmcs12_read_any(&vmx->vcpu, field, &field_value);
7862 switch (vmcs_field_type(field)) {
7863 case VMCS_FIELD_TYPE_U16:
7864 vmcs_write16(field, (u16)field_value);
7866 case VMCS_FIELD_TYPE_U32:
7867 vmcs_write32(field, (u32)field_value);
7869 case VMCS_FIELD_TYPE_U64:
7870 vmcs_write64(field, (u64)field_value);
7872 case VMCS_FIELD_TYPE_NATURAL_WIDTH:
7873 vmcs_writel(field, (long)field_value);
7882 vmcs_clear(shadow_vmcs);
7883 vmcs_load(vmx->loaded_vmcs->vmcs);
7887 * VMX instructions which assume a current vmcs12 (i.e., that VMPTRLD was
7888 * used before) all generate the same failure when it is missing.
7890 static int nested_vmx_check_vmcs12(struct kvm_vcpu *vcpu)
7892 struct vcpu_vmx *vmx = to_vmx(vcpu);
7893 if (vmx->nested.current_vmptr == -1ull) {
7894 nested_vmx_failInvalid(vcpu);
7900 static int handle_vmread(struct kvm_vcpu *vcpu)
7902 unsigned long field;
7904 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7905 u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
7908 if (!nested_vmx_check_permission(vcpu))
7911 if (!nested_vmx_check_vmcs12(vcpu))
7912 return kvm_skip_emulated_instruction(vcpu);
7914 /* Decode instruction info and find the field to read */
7915 field = kvm_register_readl(vcpu, (((vmx_instruction_info) >> 28) & 0xf));
7916 /* Read the field, zero-extended to a u64 field_value */
7917 if (vmcs12_read_any(vcpu, field, &field_value) < 0) {
7918 nested_vmx_failValid(vcpu, VMXERR_UNSUPPORTED_VMCS_COMPONENT);
7919 return kvm_skip_emulated_instruction(vcpu);
7922 * Now copy part of this value to register or memory, as requested.
7923 * Note that the number of bits actually copied is 32 or 64 depending
7924 * on the guest's mode (32 or 64 bit), not on the given field's length.
7926 if (vmx_instruction_info & (1u << 10)) {
7927 kvm_register_writel(vcpu, (((vmx_instruction_info) >> 3) & 0xf),
7930 if (get_vmx_mem_address(vcpu, exit_qualification,
7931 vmx_instruction_info, true, &gva))
7933 /* _system ok, nested_vmx_check_permission has verified cpl=0 */
7934 kvm_write_guest_virt_system(vcpu, gva, &field_value,
7935 (is_long_mode(vcpu) ? 8 : 4), NULL);
7938 nested_vmx_succeed(vcpu);
7939 return kvm_skip_emulated_instruction(vcpu);
7943 static int handle_vmwrite(struct kvm_vcpu *vcpu)
7945 unsigned long field;
7947 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7948 u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
7949 /* The value to write might be 32 or 64 bits, depending on L1's long
7950 * mode, and eventually we need to write that into a field of several
7951 * possible lengths. The code below first zero-extends the value to 64
7952 * bit (field_value), and then copies only the appropriate number of
7953 * bits into the vmcs12 field.
7955 u64 field_value = 0;
7956 struct x86_exception e;
7958 if (!nested_vmx_check_permission(vcpu))
7961 if (!nested_vmx_check_vmcs12(vcpu))
7962 return kvm_skip_emulated_instruction(vcpu);
7964 if (vmx_instruction_info & (1u << 10))
7965 field_value = kvm_register_readl(vcpu,
7966 (((vmx_instruction_info) >> 3) & 0xf));
7968 if (get_vmx_mem_address(vcpu, exit_qualification,
7969 vmx_instruction_info, false, &gva))
7971 if (kvm_read_guest_virt(vcpu, gva, &field_value,
7972 (is_64_bit_mode(vcpu) ? 8 : 4), &e)) {
7973 kvm_inject_page_fault(vcpu, &e);
7979 field = kvm_register_readl(vcpu, (((vmx_instruction_info) >> 28) & 0xf));
7980 if (vmcs_field_readonly(field)) {
7981 nested_vmx_failValid(vcpu,
7982 VMXERR_VMWRITE_READ_ONLY_VMCS_COMPONENT);
7983 return kvm_skip_emulated_instruction(vcpu);
7986 if (vmcs12_write_any(vcpu, field, field_value) < 0) {
7987 nested_vmx_failValid(vcpu, VMXERR_UNSUPPORTED_VMCS_COMPONENT);
7988 return kvm_skip_emulated_instruction(vcpu);
7991 nested_vmx_succeed(vcpu);
7992 return kvm_skip_emulated_instruction(vcpu);
7995 static void set_current_vmptr(struct vcpu_vmx *vmx, gpa_t vmptr)
7997 vmx->nested.current_vmptr = vmptr;
7998 if (enable_shadow_vmcs) {
7999 vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL,
8000 SECONDARY_EXEC_SHADOW_VMCS);
8001 vmcs_write64(VMCS_LINK_POINTER,
8002 __pa(vmx->vmcs01.shadow_vmcs));
8003 vmx->nested.sync_shadow_vmcs = true;
8007 /* Emulate the VMPTRLD instruction */
8008 static int handle_vmptrld(struct kvm_vcpu *vcpu)
8010 struct vcpu_vmx *vmx = to_vmx(vcpu);
8013 if (!nested_vmx_check_permission(vcpu))
8016 if (nested_vmx_get_vmptr(vcpu, &vmptr))
8019 if (!PAGE_ALIGNED(vmptr) || (vmptr >> cpuid_maxphyaddr(vcpu))) {
8020 nested_vmx_failValid(vcpu, VMXERR_VMPTRLD_INVALID_ADDRESS);
8021 return kvm_skip_emulated_instruction(vcpu);
8024 if (vmptr == vmx->nested.vmxon_ptr) {
8025 nested_vmx_failValid(vcpu, VMXERR_VMPTRLD_VMXON_POINTER);
8026 return kvm_skip_emulated_instruction(vcpu);
8029 if (vmx->nested.current_vmptr != vmptr) {
8030 struct vmcs12 *new_vmcs12;
8032 page = kvm_vcpu_gpa_to_page(vcpu, vmptr);
8033 if (is_error_page(page)) {
8034 nested_vmx_failInvalid(vcpu);
8035 return kvm_skip_emulated_instruction(vcpu);
8037 new_vmcs12 = kmap(page);
8038 if (new_vmcs12->revision_id != VMCS12_REVISION) {
8040 kvm_release_page_clean(page);
8041 nested_vmx_failValid(vcpu,
8042 VMXERR_VMPTRLD_INCORRECT_VMCS_REVISION_ID);
8043 return kvm_skip_emulated_instruction(vcpu);
8046 nested_release_vmcs12(vmx);
8048 * Load VMCS12 from guest memory since it is not already
8051 memcpy(vmx->nested.cached_vmcs12, new_vmcs12, VMCS12_SIZE);
8053 kvm_release_page_clean(page);
8055 set_current_vmptr(vmx, vmptr);
8058 nested_vmx_succeed(vcpu);
8059 return kvm_skip_emulated_instruction(vcpu);
8062 /* Emulate the VMPTRST instruction */
8063 static int handle_vmptrst(struct kvm_vcpu *vcpu)
8065 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
8066 u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
8068 struct x86_exception e;
8070 if (!nested_vmx_check_permission(vcpu))
8073 if (get_vmx_mem_address(vcpu, exit_qualification,
8074 vmx_instruction_info, true, &vmcs_gva))
8076 /* *_system ok, nested_vmx_check_permission has verified cpl=0 */
8077 if (kvm_write_guest_virt_system(vcpu, vmcs_gva,
8078 (void *)&to_vmx(vcpu)->nested.current_vmptr,
8080 kvm_inject_page_fault(vcpu, &e);
8083 nested_vmx_succeed(vcpu);
8084 return kvm_skip_emulated_instruction(vcpu);
8087 /* Emulate the INVEPT instruction */
8088 static int handle_invept(struct kvm_vcpu *vcpu)
8090 struct vcpu_vmx *vmx = to_vmx(vcpu);
8091 u32 vmx_instruction_info, types;
8094 struct x86_exception e;
8099 if (!(vmx->nested.nested_vmx_secondary_ctls_high &
8100 SECONDARY_EXEC_ENABLE_EPT) ||
8101 !(vmx->nested.nested_vmx_ept_caps & VMX_EPT_INVEPT_BIT)) {
8102 kvm_queue_exception(vcpu, UD_VECTOR);
8106 if (!nested_vmx_check_permission(vcpu))
8109 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
8110 type = kvm_register_readl(vcpu, (vmx_instruction_info >> 28) & 0xf);
8112 types = (vmx->nested.nested_vmx_ept_caps >> VMX_EPT_EXTENT_SHIFT) & 6;
8114 if (type >= 32 || !(types & (1 << type))) {
8115 nested_vmx_failValid(vcpu,
8116 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
8117 return kvm_skip_emulated_instruction(vcpu);
8120 /* According to the Intel VMX instruction reference, the memory
8121 * operand is read even if it isn't needed (e.g., for type==global)
8123 if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
8124 vmx_instruction_info, false, &gva))
8126 if (kvm_read_guest_virt(vcpu, gva, &operand, sizeof(operand), &e)) {
8127 kvm_inject_page_fault(vcpu, &e);
8132 case VMX_EPT_EXTENT_GLOBAL:
8134 * TODO: track mappings and invalidate
8135 * single context requests appropriately
8137 case VMX_EPT_EXTENT_CONTEXT:
8138 kvm_mmu_sync_roots(vcpu);
8139 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
8140 nested_vmx_succeed(vcpu);
8147 return kvm_skip_emulated_instruction(vcpu);
8150 static int handle_invvpid(struct kvm_vcpu *vcpu)
8152 struct vcpu_vmx *vmx = to_vmx(vcpu);
8153 u32 vmx_instruction_info;
8154 unsigned long type, types;
8156 struct x86_exception e;
8162 if (!(vmx->nested.nested_vmx_secondary_ctls_high &
8163 SECONDARY_EXEC_ENABLE_VPID) ||
8164 !(vmx->nested.nested_vmx_vpid_caps & VMX_VPID_INVVPID_BIT)) {
8165 kvm_queue_exception(vcpu, UD_VECTOR);
8169 if (!nested_vmx_check_permission(vcpu))
8172 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
8173 type = kvm_register_readl(vcpu, (vmx_instruction_info >> 28) & 0xf);
8175 types = (vmx->nested.nested_vmx_vpid_caps &
8176 VMX_VPID_EXTENT_SUPPORTED_MASK) >> 8;
8178 if (type >= 32 || !(types & (1 << type))) {
8179 nested_vmx_failValid(vcpu,
8180 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
8181 return kvm_skip_emulated_instruction(vcpu);
8184 /* according to the intel vmx instruction reference, the memory
8185 * operand is read even if it isn't needed (e.g., for type==global)
8187 if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
8188 vmx_instruction_info, false, &gva))
8190 if (kvm_read_guest_virt(vcpu, gva, &operand, sizeof(operand), &e)) {
8191 kvm_inject_page_fault(vcpu, &e);
8194 if (operand.vpid >> 16) {
8195 nested_vmx_failValid(vcpu,
8196 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
8197 return kvm_skip_emulated_instruction(vcpu);
8201 case VMX_VPID_EXTENT_INDIVIDUAL_ADDR:
8202 if (is_noncanonical_address(operand.gla, vcpu)) {
8203 nested_vmx_failValid(vcpu,
8204 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
8205 return kvm_skip_emulated_instruction(vcpu);
8208 case VMX_VPID_EXTENT_SINGLE_CONTEXT:
8209 case VMX_VPID_EXTENT_SINGLE_NON_GLOBAL:
8210 if (!operand.vpid) {
8211 nested_vmx_failValid(vcpu,
8212 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
8213 return kvm_skip_emulated_instruction(vcpu);
8216 case VMX_VPID_EXTENT_ALL_CONTEXT:
8220 return kvm_skip_emulated_instruction(vcpu);
8223 __vmx_flush_tlb(vcpu, vmx->nested.vpid02);
8224 nested_vmx_succeed(vcpu);
8226 return kvm_skip_emulated_instruction(vcpu);
8229 static int handle_pml_full(struct kvm_vcpu *vcpu)
8231 unsigned long exit_qualification;
8233 trace_kvm_pml_full(vcpu->vcpu_id);
8235 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
8238 * PML buffer FULL happened while executing iret from NMI,
8239 * "blocked by NMI" bit has to be set before next VM entry.
8241 if (!(to_vmx(vcpu)->idt_vectoring_info & VECTORING_INFO_VALID_MASK) &&
8242 cpu_has_virtual_nmis() &&
8243 (exit_qualification & INTR_INFO_UNBLOCK_NMI))
8244 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
8245 GUEST_INTR_STATE_NMI);
8248 * PML buffer already flushed at beginning of VMEXIT. Nothing to do
8249 * here.., and there's no userspace involvement needed for PML.
8254 static int handle_preemption_timer(struct kvm_vcpu *vcpu)
8256 kvm_lapic_expired_hv_timer(vcpu);
8260 static bool valid_ept_address(struct kvm_vcpu *vcpu, u64 address)
8262 struct vcpu_vmx *vmx = to_vmx(vcpu);
8263 int maxphyaddr = cpuid_maxphyaddr(vcpu);
8265 /* Check for memory type validity */
8266 switch (address & VMX_EPTP_MT_MASK) {
8267 case VMX_EPTP_MT_UC:
8268 if (!(vmx->nested.nested_vmx_ept_caps & VMX_EPTP_UC_BIT))
8271 case VMX_EPTP_MT_WB:
8272 if (!(vmx->nested.nested_vmx_ept_caps & VMX_EPTP_WB_BIT))
8279 /* only 4 levels page-walk length are valid */
8280 if ((address & VMX_EPTP_PWL_MASK) != VMX_EPTP_PWL_4)
8283 /* Reserved bits should not be set */
8284 if (address >> maxphyaddr || ((address >> 7) & 0x1f))
8287 /* AD, if set, should be supported */
8288 if (address & VMX_EPTP_AD_ENABLE_BIT) {
8289 if (!(vmx->nested.nested_vmx_ept_caps & VMX_EPT_AD_BIT))
8296 static int nested_vmx_eptp_switching(struct kvm_vcpu *vcpu,
8297 struct vmcs12 *vmcs12)
8299 u32 index = vcpu->arch.regs[VCPU_REGS_RCX];
8301 bool accessed_dirty;
8302 struct kvm_mmu *mmu = vcpu->arch.walk_mmu;
8304 if (!nested_cpu_has_eptp_switching(vmcs12) ||
8305 !nested_cpu_has_ept(vmcs12))
8308 if (index >= VMFUNC_EPTP_ENTRIES)
8312 if (kvm_vcpu_read_guest_page(vcpu, vmcs12->eptp_list_address >> PAGE_SHIFT,
8313 &address, index * 8, 8))
8316 accessed_dirty = !!(address & VMX_EPTP_AD_ENABLE_BIT);
8319 * If the (L2) guest does a vmfunc to the currently
8320 * active ept pointer, we don't have to do anything else
8322 if (vmcs12->ept_pointer != address) {
8323 if (!valid_ept_address(vcpu, address))
8326 kvm_mmu_unload(vcpu);
8327 mmu->ept_ad = accessed_dirty;
8328 mmu->base_role.ad_disabled = !accessed_dirty;
8329 vmcs12->ept_pointer = address;
8331 * TODO: Check what's the correct approach in case
8332 * mmu reload fails. Currently, we just let the next
8333 * reload potentially fail
8335 kvm_mmu_reload(vcpu);
8341 static int handle_vmfunc(struct kvm_vcpu *vcpu)
8343 struct vcpu_vmx *vmx = to_vmx(vcpu);
8344 struct vmcs12 *vmcs12;
8345 u32 function = vcpu->arch.regs[VCPU_REGS_RAX];
8348 * VMFUNC is only supported for nested guests, but we always enable the
8349 * secondary control for simplicity; for non-nested mode, fake that we
8350 * didn't by injecting #UD.
8352 if (!is_guest_mode(vcpu)) {
8353 kvm_queue_exception(vcpu, UD_VECTOR);
8357 vmcs12 = get_vmcs12(vcpu);
8358 if ((vmcs12->vm_function_control & (1 << function)) == 0)
8363 if (nested_vmx_eptp_switching(vcpu, vmcs12))
8369 return kvm_skip_emulated_instruction(vcpu);
8372 nested_vmx_vmexit(vcpu, vmx->exit_reason,
8373 vmcs_read32(VM_EXIT_INTR_INFO),
8374 vmcs_readl(EXIT_QUALIFICATION));
8379 * The exit handlers return 1 if the exit was handled fully and guest execution
8380 * may resume. Otherwise they set the kvm_run parameter to indicate what needs
8381 * to be done to userspace and return 0.
8383 static int (*const kvm_vmx_exit_handlers[])(struct kvm_vcpu *vcpu) = {
8384 [EXIT_REASON_EXCEPTION_NMI] = handle_exception,
8385 [EXIT_REASON_EXTERNAL_INTERRUPT] = handle_external_interrupt,
8386 [EXIT_REASON_TRIPLE_FAULT] = handle_triple_fault,
8387 [EXIT_REASON_NMI_WINDOW] = handle_nmi_window,
8388 [EXIT_REASON_IO_INSTRUCTION] = handle_io,
8389 [EXIT_REASON_CR_ACCESS] = handle_cr,
8390 [EXIT_REASON_DR_ACCESS] = handle_dr,
8391 [EXIT_REASON_CPUID] = handle_cpuid,
8392 [EXIT_REASON_MSR_READ] = handle_rdmsr,
8393 [EXIT_REASON_MSR_WRITE] = handle_wrmsr,
8394 [EXIT_REASON_PENDING_INTERRUPT] = handle_interrupt_window,
8395 [EXIT_REASON_HLT] = handle_halt,
8396 [EXIT_REASON_INVD] = handle_invd,
8397 [EXIT_REASON_INVLPG] = handle_invlpg,
8398 [EXIT_REASON_RDPMC] = handle_rdpmc,
8399 [EXIT_REASON_VMCALL] = handle_vmcall,
8400 [EXIT_REASON_VMCLEAR] = handle_vmclear,
8401 [EXIT_REASON_VMLAUNCH] = handle_vmlaunch,
8402 [EXIT_REASON_VMPTRLD] = handle_vmptrld,
8403 [EXIT_REASON_VMPTRST] = handle_vmptrst,
8404 [EXIT_REASON_VMREAD] = handle_vmread,
8405 [EXIT_REASON_VMRESUME] = handle_vmresume,
8406 [EXIT_REASON_VMWRITE] = handle_vmwrite,
8407 [EXIT_REASON_VMOFF] = handle_vmoff,
8408 [EXIT_REASON_VMON] = handle_vmon,
8409 [EXIT_REASON_TPR_BELOW_THRESHOLD] = handle_tpr_below_threshold,
8410 [EXIT_REASON_APIC_ACCESS] = handle_apic_access,
8411 [EXIT_REASON_APIC_WRITE] = handle_apic_write,
8412 [EXIT_REASON_EOI_INDUCED] = handle_apic_eoi_induced,
8413 [EXIT_REASON_WBINVD] = handle_wbinvd,
8414 [EXIT_REASON_XSETBV] = handle_xsetbv,
8415 [EXIT_REASON_TASK_SWITCH] = handle_task_switch,
8416 [EXIT_REASON_MCE_DURING_VMENTRY] = handle_machine_check,
8417 [EXIT_REASON_EPT_VIOLATION] = handle_ept_violation,
8418 [EXIT_REASON_EPT_MISCONFIG] = handle_ept_misconfig,
8419 [EXIT_REASON_PAUSE_INSTRUCTION] = handle_pause,
8420 [EXIT_REASON_MWAIT_INSTRUCTION] = handle_mwait,
8421 [EXIT_REASON_MONITOR_TRAP_FLAG] = handle_monitor_trap,
8422 [EXIT_REASON_MONITOR_INSTRUCTION] = handle_monitor,
8423 [EXIT_REASON_INVEPT] = handle_invept,
8424 [EXIT_REASON_INVVPID] = handle_invvpid,
8425 [EXIT_REASON_RDRAND] = handle_invalid_op,
8426 [EXIT_REASON_RDSEED] = handle_invalid_op,
8427 [EXIT_REASON_XSAVES] = handle_xsaves,
8428 [EXIT_REASON_XRSTORS] = handle_xrstors,
8429 [EXIT_REASON_PML_FULL] = handle_pml_full,
8430 [EXIT_REASON_VMFUNC] = handle_vmfunc,
8431 [EXIT_REASON_PREEMPTION_TIMER] = handle_preemption_timer,
8434 static const int kvm_vmx_max_exit_handlers =
8435 ARRAY_SIZE(kvm_vmx_exit_handlers);
8437 static bool nested_vmx_exit_handled_io(struct kvm_vcpu *vcpu,
8438 struct vmcs12 *vmcs12)
8440 unsigned long exit_qualification;
8441 gpa_t bitmap, last_bitmap;
8446 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_IO_BITMAPS))
8447 return nested_cpu_has(vmcs12, CPU_BASED_UNCOND_IO_EXITING);
8449 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
8451 port = exit_qualification >> 16;
8452 size = (exit_qualification & 7) + 1;
8454 last_bitmap = (gpa_t)-1;
8459 bitmap = vmcs12->io_bitmap_a;
8460 else if (port < 0x10000)
8461 bitmap = vmcs12->io_bitmap_b;
8464 bitmap += (port & 0x7fff) / 8;
8466 if (last_bitmap != bitmap)
8467 if (kvm_vcpu_read_guest(vcpu, bitmap, &b, 1))
8469 if (b & (1 << (port & 7)))
8474 last_bitmap = bitmap;
8481 * Return 1 if we should exit from L2 to L1 to handle an MSR access access,
8482 * rather than handle it ourselves in L0. I.e., check whether L1 expressed
8483 * disinterest in the current event (read or write a specific MSR) by using an
8484 * MSR bitmap. This may be the case even when L0 doesn't use MSR bitmaps.
8486 static bool nested_vmx_exit_handled_msr(struct kvm_vcpu *vcpu,
8487 struct vmcs12 *vmcs12, u32 exit_reason)
8489 u32 msr_index = vcpu->arch.regs[VCPU_REGS_RCX];
8492 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS))
8496 * The MSR_BITMAP page is divided into four 1024-byte bitmaps,
8497 * for the four combinations of read/write and low/high MSR numbers.
8498 * First we need to figure out which of the four to use:
8500 bitmap = vmcs12->msr_bitmap;
8501 if (exit_reason == EXIT_REASON_MSR_WRITE)
8503 if (msr_index >= 0xc0000000) {
8504 msr_index -= 0xc0000000;
8508 /* Then read the msr_index'th bit from this bitmap: */
8509 if (msr_index < 1024*8) {
8511 if (kvm_vcpu_read_guest(vcpu, bitmap + msr_index/8, &b, 1))
8513 return 1 & (b >> (msr_index & 7));
8515 return true; /* let L1 handle the wrong parameter */
8519 * Return 1 if we should exit from L2 to L1 to handle a CR access exit,
8520 * rather than handle it ourselves in L0. I.e., check if L1 wanted to
8521 * intercept (via guest_host_mask etc.) the current event.
8523 static bool nested_vmx_exit_handled_cr(struct kvm_vcpu *vcpu,
8524 struct vmcs12 *vmcs12)
8526 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
8527 int cr = exit_qualification & 15;
8531 switch ((exit_qualification >> 4) & 3) {
8532 case 0: /* mov to cr */
8533 reg = (exit_qualification >> 8) & 15;
8534 val = kvm_register_readl(vcpu, reg);
8537 if (vmcs12->cr0_guest_host_mask &
8538 (val ^ vmcs12->cr0_read_shadow))
8542 if ((vmcs12->cr3_target_count >= 1 &&
8543 vmcs12->cr3_target_value0 == val) ||
8544 (vmcs12->cr3_target_count >= 2 &&
8545 vmcs12->cr3_target_value1 == val) ||
8546 (vmcs12->cr3_target_count >= 3 &&
8547 vmcs12->cr3_target_value2 == val) ||
8548 (vmcs12->cr3_target_count >= 4 &&
8549 vmcs12->cr3_target_value3 == val))
8551 if (nested_cpu_has(vmcs12, CPU_BASED_CR3_LOAD_EXITING))
8555 if (vmcs12->cr4_guest_host_mask &
8556 (vmcs12->cr4_read_shadow ^ val))
8560 if (nested_cpu_has(vmcs12, CPU_BASED_CR8_LOAD_EXITING))
8566 if ((vmcs12->cr0_guest_host_mask & X86_CR0_TS) &&
8567 (vmcs12->cr0_read_shadow & X86_CR0_TS))
8570 case 1: /* mov from cr */
8573 if (vmcs12->cpu_based_vm_exec_control &
8574 CPU_BASED_CR3_STORE_EXITING)
8578 if (vmcs12->cpu_based_vm_exec_control &
8579 CPU_BASED_CR8_STORE_EXITING)
8586 * lmsw can change bits 1..3 of cr0, and only set bit 0 of
8587 * cr0. Other attempted changes are ignored, with no exit.
8589 val = (exit_qualification >> LMSW_SOURCE_DATA_SHIFT) & 0x0f;
8590 if (vmcs12->cr0_guest_host_mask & 0xe &
8591 (val ^ vmcs12->cr0_read_shadow))
8593 if ((vmcs12->cr0_guest_host_mask & 0x1) &&
8594 !(vmcs12->cr0_read_shadow & 0x1) &&
8603 * Return 1 if we should exit from L2 to L1 to handle an exit, or 0 if we
8604 * should handle it ourselves in L0 (and then continue L2). Only call this
8605 * when in is_guest_mode (L2).
8607 static bool nested_vmx_exit_reflected(struct kvm_vcpu *vcpu, u32 exit_reason)
8609 u32 intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
8610 struct vcpu_vmx *vmx = to_vmx(vcpu);
8611 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
8613 if (vmx->nested.nested_run_pending)
8616 if (unlikely(vmx->fail)) {
8617 pr_info_ratelimited("%s failed vm entry %x\n", __func__,
8618 vmcs_read32(VM_INSTRUCTION_ERROR));
8623 * The host physical addresses of some pages of guest memory
8624 * are loaded into the vmcs02 (e.g. vmcs12's Virtual APIC
8625 * Page). The CPU may write to these pages via their host
8626 * physical address while L2 is running, bypassing any
8627 * address-translation-based dirty tracking (e.g. EPT write
8630 * Mark them dirty on every exit from L2 to prevent them from
8631 * getting out of sync with dirty tracking.
8633 nested_mark_vmcs12_pages_dirty(vcpu);
8635 trace_kvm_nested_vmexit(kvm_rip_read(vcpu), exit_reason,
8636 vmcs_readl(EXIT_QUALIFICATION),
8637 vmx->idt_vectoring_info,
8639 vmcs_read32(VM_EXIT_INTR_ERROR_CODE),
8642 switch (exit_reason) {
8643 case EXIT_REASON_EXCEPTION_NMI:
8644 if (is_nmi(intr_info))
8646 else if (is_page_fault(intr_info))
8647 return !vmx->vcpu.arch.apf.host_apf_reason && enable_ept;
8648 else if (is_no_device(intr_info) &&
8649 !(vmcs12->guest_cr0 & X86_CR0_TS))
8651 else if (is_debug(intr_info) &&
8653 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))
8655 else if (is_breakpoint(intr_info) &&
8656 vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
8658 return vmcs12->exception_bitmap &
8659 (1u << (intr_info & INTR_INFO_VECTOR_MASK));
8660 case EXIT_REASON_EXTERNAL_INTERRUPT:
8662 case EXIT_REASON_TRIPLE_FAULT:
8664 case EXIT_REASON_PENDING_INTERRUPT:
8665 return nested_cpu_has(vmcs12, CPU_BASED_VIRTUAL_INTR_PENDING);
8666 case EXIT_REASON_NMI_WINDOW:
8667 return nested_cpu_has(vmcs12, CPU_BASED_VIRTUAL_NMI_PENDING);
8668 case EXIT_REASON_TASK_SWITCH:
8670 case EXIT_REASON_CPUID:
8672 case EXIT_REASON_HLT:
8673 return nested_cpu_has(vmcs12, CPU_BASED_HLT_EXITING);
8674 case EXIT_REASON_INVD:
8676 case EXIT_REASON_INVLPG:
8677 return nested_cpu_has(vmcs12, CPU_BASED_INVLPG_EXITING);
8678 case EXIT_REASON_RDPMC:
8679 return nested_cpu_has(vmcs12, CPU_BASED_RDPMC_EXITING);
8680 case EXIT_REASON_RDRAND:
8681 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_RDRAND);
8682 case EXIT_REASON_RDSEED:
8683 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_RDSEED);
8684 case EXIT_REASON_RDTSC: case EXIT_REASON_RDTSCP:
8685 return nested_cpu_has(vmcs12, CPU_BASED_RDTSC_EXITING);
8686 case EXIT_REASON_VMCALL: case EXIT_REASON_VMCLEAR:
8687 case EXIT_REASON_VMLAUNCH: case EXIT_REASON_VMPTRLD:
8688 case EXIT_REASON_VMPTRST: case EXIT_REASON_VMREAD:
8689 case EXIT_REASON_VMRESUME: case EXIT_REASON_VMWRITE:
8690 case EXIT_REASON_VMOFF: case EXIT_REASON_VMON:
8691 case EXIT_REASON_INVEPT: case EXIT_REASON_INVVPID:
8693 * VMX instructions trap unconditionally. This allows L1 to
8694 * emulate them for its L2 guest, i.e., allows 3-level nesting!
8697 case EXIT_REASON_CR_ACCESS:
8698 return nested_vmx_exit_handled_cr(vcpu, vmcs12);
8699 case EXIT_REASON_DR_ACCESS:
8700 return nested_cpu_has(vmcs12, CPU_BASED_MOV_DR_EXITING);
8701 case EXIT_REASON_IO_INSTRUCTION:
8702 return nested_vmx_exit_handled_io(vcpu, vmcs12);
8703 case EXIT_REASON_GDTR_IDTR: case EXIT_REASON_LDTR_TR:
8704 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_DESC);
8705 case EXIT_REASON_MSR_READ:
8706 case EXIT_REASON_MSR_WRITE:
8707 return nested_vmx_exit_handled_msr(vcpu, vmcs12, exit_reason);
8708 case EXIT_REASON_INVALID_STATE:
8710 case EXIT_REASON_MWAIT_INSTRUCTION:
8711 return nested_cpu_has(vmcs12, CPU_BASED_MWAIT_EXITING);
8712 case EXIT_REASON_MONITOR_TRAP_FLAG:
8713 return nested_cpu_has(vmcs12, CPU_BASED_MONITOR_TRAP_FLAG);
8714 case EXIT_REASON_MONITOR_INSTRUCTION:
8715 return nested_cpu_has(vmcs12, CPU_BASED_MONITOR_EXITING);
8716 case EXIT_REASON_PAUSE_INSTRUCTION:
8717 return nested_cpu_has(vmcs12, CPU_BASED_PAUSE_EXITING) ||
8718 nested_cpu_has2(vmcs12,
8719 SECONDARY_EXEC_PAUSE_LOOP_EXITING);
8720 case EXIT_REASON_MCE_DURING_VMENTRY:
8722 case EXIT_REASON_TPR_BELOW_THRESHOLD:
8723 return nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW);
8724 case EXIT_REASON_APIC_ACCESS:
8725 return nested_cpu_has2(vmcs12,
8726 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES);
8727 case EXIT_REASON_APIC_WRITE:
8728 case EXIT_REASON_EOI_INDUCED:
8729 /* apic_write and eoi_induced should exit unconditionally. */
8731 case EXIT_REASON_EPT_VIOLATION:
8733 * L0 always deals with the EPT violation. If nested EPT is
8734 * used, and the nested mmu code discovers that the address is
8735 * missing in the guest EPT table (EPT12), the EPT violation
8736 * will be injected with nested_ept_inject_page_fault()
8739 case EXIT_REASON_EPT_MISCONFIG:
8741 * L2 never uses directly L1's EPT, but rather L0's own EPT
8742 * table (shadow on EPT) or a merged EPT table that L0 built
8743 * (EPT on EPT). So any problems with the structure of the
8744 * table is L0's fault.
8747 case EXIT_REASON_INVPCID:
8749 nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_INVPCID) &&
8750 nested_cpu_has(vmcs12, CPU_BASED_INVLPG_EXITING);
8751 case EXIT_REASON_WBINVD:
8752 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_WBINVD_EXITING);
8753 case EXIT_REASON_XSETBV:
8755 case EXIT_REASON_XSAVES: case EXIT_REASON_XRSTORS:
8757 * This should never happen, since it is not possible to
8758 * set XSS to a non-zero value---neither in L1 nor in L2.
8759 * If if it were, XSS would have to be checked against
8760 * the XSS exit bitmap in vmcs12.
8762 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_XSAVES);
8763 case EXIT_REASON_PREEMPTION_TIMER:
8765 case EXIT_REASON_PML_FULL:
8766 /* We emulate PML support to L1. */
8768 case EXIT_REASON_VMFUNC:
8769 /* VM functions are emulated through L2->L0 vmexits. */
8776 static int nested_vmx_reflect_vmexit(struct kvm_vcpu *vcpu, u32 exit_reason)
8778 u32 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
8781 * At this point, the exit interruption info in exit_intr_info
8782 * is only valid for EXCEPTION_NMI exits. For EXTERNAL_INTERRUPT
8783 * we need to query the in-kernel LAPIC.
8785 WARN_ON(exit_reason == EXIT_REASON_EXTERNAL_INTERRUPT);
8786 if ((exit_intr_info &
8787 (INTR_INFO_VALID_MASK | INTR_INFO_DELIVER_CODE_MASK)) ==
8788 (INTR_INFO_VALID_MASK | INTR_INFO_DELIVER_CODE_MASK)) {
8789 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
8790 vmcs12->vm_exit_intr_error_code =
8791 vmcs_read32(VM_EXIT_INTR_ERROR_CODE);
8794 nested_vmx_vmexit(vcpu, exit_reason, exit_intr_info,
8795 vmcs_readl(EXIT_QUALIFICATION));
8799 static void vmx_get_exit_info(struct kvm_vcpu *vcpu, u64 *info1, u64 *info2)
8801 *info1 = vmcs_readl(EXIT_QUALIFICATION);
8802 *info2 = vmcs_read32(VM_EXIT_INTR_INFO);
8805 static void vmx_destroy_pml_buffer(struct vcpu_vmx *vmx)
8808 __free_page(vmx->pml_pg);
8813 static void vmx_flush_pml_buffer(struct kvm_vcpu *vcpu)
8815 struct vcpu_vmx *vmx = to_vmx(vcpu);
8819 pml_idx = vmcs_read16(GUEST_PML_INDEX);
8821 /* Do nothing if PML buffer is empty */
8822 if (pml_idx == (PML_ENTITY_NUM - 1))
8825 /* PML index always points to next available PML buffer entity */
8826 if (pml_idx >= PML_ENTITY_NUM)
8831 pml_buf = page_address(vmx->pml_pg);
8832 for (; pml_idx < PML_ENTITY_NUM; pml_idx++) {
8835 gpa = pml_buf[pml_idx];
8836 WARN_ON(gpa & (PAGE_SIZE - 1));
8837 kvm_vcpu_mark_page_dirty(vcpu, gpa >> PAGE_SHIFT);
8840 /* reset PML index */
8841 vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1);
8845 * Flush all vcpus' PML buffer and update logged GPAs to dirty_bitmap.
8846 * Called before reporting dirty_bitmap to userspace.
8848 static void kvm_flush_pml_buffers(struct kvm *kvm)
8851 struct kvm_vcpu *vcpu;
8853 * We only need to kick vcpu out of guest mode here, as PML buffer
8854 * is flushed at beginning of all VMEXITs, and it's obvious that only
8855 * vcpus running in guest are possible to have unflushed GPAs in PML
8858 kvm_for_each_vcpu(i, vcpu, kvm)
8859 kvm_vcpu_kick(vcpu);
8862 static void vmx_dump_sel(char *name, uint32_t sel)
8864 pr_err("%s sel=0x%04x, attr=0x%05x, limit=0x%08x, base=0x%016lx\n",
8865 name, vmcs_read16(sel),
8866 vmcs_read32(sel + GUEST_ES_AR_BYTES - GUEST_ES_SELECTOR),
8867 vmcs_read32(sel + GUEST_ES_LIMIT - GUEST_ES_SELECTOR),
8868 vmcs_readl(sel + GUEST_ES_BASE - GUEST_ES_SELECTOR));
8871 static void vmx_dump_dtsel(char *name, uint32_t limit)
8873 pr_err("%s limit=0x%08x, base=0x%016lx\n",
8874 name, vmcs_read32(limit),
8875 vmcs_readl(limit + GUEST_GDTR_BASE - GUEST_GDTR_LIMIT));
8878 static void dump_vmcs(void)
8880 u32 vmentry_ctl = vmcs_read32(VM_ENTRY_CONTROLS);
8881 u32 vmexit_ctl = vmcs_read32(VM_EXIT_CONTROLS);
8882 u32 cpu_based_exec_ctrl = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
8883 u32 pin_based_exec_ctrl = vmcs_read32(PIN_BASED_VM_EXEC_CONTROL);
8884 u32 secondary_exec_control = 0;
8885 unsigned long cr4 = vmcs_readl(GUEST_CR4);
8886 u64 efer = vmcs_read64(GUEST_IA32_EFER);
8889 if (cpu_has_secondary_exec_ctrls())
8890 secondary_exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
8892 pr_err("*** Guest State ***\n");
8893 pr_err("CR0: actual=0x%016lx, shadow=0x%016lx, gh_mask=%016lx\n",
8894 vmcs_readl(GUEST_CR0), vmcs_readl(CR0_READ_SHADOW),
8895 vmcs_readl(CR0_GUEST_HOST_MASK));
8896 pr_err("CR4: actual=0x%016lx, shadow=0x%016lx, gh_mask=%016lx\n",
8897 cr4, vmcs_readl(CR4_READ_SHADOW), vmcs_readl(CR4_GUEST_HOST_MASK));
8898 pr_err("CR3 = 0x%016lx\n", vmcs_readl(GUEST_CR3));
8899 if ((secondary_exec_control & SECONDARY_EXEC_ENABLE_EPT) &&
8900 (cr4 & X86_CR4_PAE) && !(efer & EFER_LMA))
8902 pr_err("PDPTR0 = 0x%016llx PDPTR1 = 0x%016llx\n",
8903 vmcs_read64(GUEST_PDPTR0), vmcs_read64(GUEST_PDPTR1));
8904 pr_err("PDPTR2 = 0x%016llx PDPTR3 = 0x%016llx\n",
8905 vmcs_read64(GUEST_PDPTR2), vmcs_read64(GUEST_PDPTR3));
8907 pr_err("RSP = 0x%016lx RIP = 0x%016lx\n",
8908 vmcs_readl(GUEST_RSP), vmcs_readl(GUEST_RIP));
8909 pr_err("RFLAGS=0x%08lx DR7 = 0x%016lx\n",
8910 vmcs_readl(GUEST_RFLAGS), vmcs_readl(GUEST_DR7));
8911 pr_err("Sysenter RSP=%016lx CS:RIP=%04x:%016lx\n",
8912 vmcs_readl(GUEST_SYSENTER_ESP),
8913 vmcs_read32(GUEST_SYSENTER_CS), vmcs_readl(GUEST_SYSENTER_EIP));
8914 vmx_dump_sel("CS: ", GUEST_CS_SELECTOR);
8915 vmx_dump_sel("DS: ", GUEST_DS_SELECTOR);
8916 vmx_dump_sel("SS: ", GUEST_SS_SELECTOR);
8917 vmx_dump_sel("ES: ", GUEST_ES_SELECTOR);
8918 vmx_dump_sel("FS: ", GUEST_FS_SELECTOR);
8919 vmx_dump_sel("GS: ", GUEST_GS_SELECTOR);
8920 vmx_dump_dtsel("GDTR:", GUEST_GDTR_LIMIT);
8921 vmx_dump_sel("LDTR:", GUEST_LDTR_SELECTOR);
8922 vmx_dump_dtsel("IDTR:", GUEST_IDTR_LIMIT);
8923 vmx_dump_sel("TR: ", GUEST_TR_SELECTOR);
8924 if ((vmexit_ctl & (VM_EXIT_SAVE_IA32_PAT | VM_EXIT_SAVE_IA32_EFER)) ||
8925 (vmentry_ctl & (VM_ENTRY_LOAD_IA32_PAT | VM_ENTRY_LOAD_IA32_EFER)))
8926 pr_err("EFER = 0x%016llx PAT = 0x%016llx\n",
8927 efer, vmcs_read64(GUEST_IA32_PAT));
8928 pr_err("DebugCtl = 0x%016llx DebugExceptions = 0x%016lx\n",
8929 vmcs_read64(GUEST_IA32_DEBUGCTL),
8930 vmcs_readl(GUEST_PENDING_DBG_EXCEPTIONS));
8931 if (vmentry_ctl & VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL)
8932 pr_err("PerfGlobCtl = 0x%016llx\n",
8933 vmcs_read64(GUEST_IA32_PERF_GLOBAL_CTRL));
8934 if (vmentry_ctl & VM_ENTRY_LOAD_BNDCFGS)
8935 pr_err("BndCfgS = 0x%016llx\n", vmcs_read64(GUEST_BNDCFGS));
8936 pr_err("Interruptibility = %08x ActivityState = %08x\n",
8937 vmcs_read32(GUEST_INTERRUPTIBILITY_INFO),
8938 vmcs_read32(GUEST_ACTIVITY_STATE));
8939 if (secondary_exec_control & SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY)
8940 pr_err("InterruptStatus = %04x\n",
8941 vmcs_read16(GUEST_INTR_STATUS));
8943 pr_err("*** Host State ***\n");
8944 pr_err("RIP = 0x%016lx RSP = 0x%016lx\n",
8945 vmcs_readl(HOST_RIP), vmcs_readl(HOST_RSP));
8946 pr_err("CS=%04x SS=%04x DS=%04x ES=%04x FS=%04x GS=%04x TR=%04x\n",
8947 vmcs_read16(HOST_CS_SELECTOR), vmcs_read16(HOST_SS_SELECTOR),
8948 vmcs_read16(HOST_DS_SELECTOR), vmcs_read16(HOST_ES_SELECTOR),
8949 vmcs_read16(HOST_FS_SELECTOR), vmcs_read16(HOST_GS_SELECTOR),
8950 vmcs_read16(HOST_TR_SELECTOR));
8951 pr_err("FSBase=%016lx GSBase=%016lx TRBase=%016lx\n",
8952 vmcs_readl(HOST_FS_BASE), vmcs_readl(HOST_GS_BASE),
8953 vmcs_readl(HOST_TR_BASE));
8954 pr_err("GDTBase=%016lx IDTBase=%016lx\n",
8955 vmcs_readl(HOST_GDTR_BASE), vmcs_readl(HOST_IDTR_BASE));
8956 pr_err("CR0=%016lx CR3=%016lx CR4=%016lx\n",
8957 vmcs_readl(HOST_CR0), vmcs_readl(HOST_CR3),
8958 vmcs_readl(HOST_CR4));
8959 pr_err("Sysenter RSP=%016lx CS:RIP=%04x:%016lx\n",
8960 vmcs_readl(HOST_IA32_SYSENTER_ESP),
8961 vmcs_read32(HOST_IA32_SYSENTER_CS),
8962 vmcs_readl(HOST_IA32_SYSENTER_EIP));
8963 if (vmexit_ctl & (VM_EXIT_LOAD_IA32_PAT | VM_EXIT_LOAD_IA32_EFER))
8964 pr_err("EFER = 0x%016llx PAT = 0x%016llx\n",
8965 vmcs_read64(HOST_IA32_EFER),
8966 vmcs_read64(HOST_IA32_PAT));
8967 if (vmexit_ctl & VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL)
8968 pr_err("PerfGlobCtl = 0x%016llx\n",
8969 vmcs_read64(HOST_IA32_PERF_GLOBAL_CTRL));
8971 pr_err("*** Control State ***\n");
8972 pr_err("PinBased=%08x CPUBased=%08x SecondaryExec=%08x\n",
8973 pin_based_exec_ctrl, cpu_based_exec_ctrl, secondary_exec_control);
8974 pr_err("EntryControls=%08x ExitControls=%08x\n", vmentry_ctl, vmexit_ctl);
8975 pr_err("ExceptionBitmap=%08x PFECmask=%08x PFECmatch=%08x\n",
8976 vmcs_read32(EXCEPTION_BITMAP),
8977 vmcs_read32(PAGE_FAULT_ERROR_CODE_MASK),
8978 vmcs_read32(PAGE_FAULT_ERROR_CODE_MATCH));
8979 pr_err("VMEntry: intr_info=%08x errcode=%08x ilen=%08x\n",
8980 vmcs_read32(VM_ENTRY_INTR_INFO_FIELD),
8981 vmcs_read32(VM_ENTRY_EXCEPTION_ERROR_CODE),
8982 vmcs_read32(VM_ENTRY_INSTRUCTION_LEN));
8983 pr_err("VMExit: intr_info=%08x errcode=%08x ilen=%08x\n",
8984 vmcs_read32(VM_EXIT_INTR_INFO),
8985 vmcs_read32(VM_EXIT_INTR_ERROR_CODE),
8986 vmcs_read32(VM_EXIT_INSTRUCTION_LEN));
8987 pr_err(" reason=%08x qualification=%016lx\n",
8988 vmcs_read32(VM_EXIT_REASON), vmcs_readl(EXIT_QUALIFICATION));
8989 pr_err("IDTVectoring: info=%08x errcode=%08x\n",
8990 vmcs_read32(IDT_VECTORING_INFO_FIELD),
8991 vmcs_read32(IDT_VECTORING_ERROR_CODE));
8992 pr_err("TSC Offset = 0x%016llx\n", vmcs_read64(TSC_OFFSET));
8993 if (secondary_exec_control & SECONDARY_EXEC_TSC_SCALING)
8994 pr_err("TSC Multiplier = 0x%016llx\n",
8995 vmcs_read64(TSC_MULTIPLIER));
8996 if (cpu_based_exec_ctrl & CPU_BASED_TPR_SHADOW)
8997 pr_err("TPR Threshold = 0x%02x\n", vmcs_read32(TPR_THRESHOLD));
8998 if (pin_based_exec_ctrl & PIN_BASED_POSTED_INTR)
8999 pr_err("PostedIntrVec = 0x%02x\n", vmcs_read16(POSTED_INTR_NV));
9000 if ((secondary_exec_control & SECONDARY_EXEC_ENABLE_EPT))
9001 pr_err("EPT pointer = 0x%016llx\n", vmcs_read64(EPT_POINTER));
9002 n = vmcs_read32(CR3_TARGET_COUNT);
9003 for (i = 0; i + 1 < n; i += 4)
9004 pr_err("CR3 target%u=%016lx target%u=%016lx\n",
9005 i, vmcs_readl(CR3_TARGET_VALUE0 + i * 2),
9006 i + 1, vmcs_readl(CR3_TARGET_VALUE0 + i * 2 + 2));
9008 pr_err("CR3 target%u=%016lx\n",
9009 i, vmcs_readl(CR3_TARGET_VALUE0 + i * 2));
9010 if (secondary_exec_control & SECONDARY_EXEC_PAUSE_LOOP_EXITING)
9011 pr_err("PLE Gap=%08x Window=%08x\n",
9012 vmcs_read32(PLE_GAP), vmcs_read32(PLE_WINDOW));
9013 if (secondary_exec_control & SECONDARY_EXEC_ENABLE_VPID)
9014 pr_err("Virtual processor ID = 0x%04x\n",
9015 vmcs_read16(VIRTUAL_PROCESSOR_ID));
9019 * The guest has exited. See if we can fix it or if we need userspace
9022 static int vmx_handle_exit(struct kvm_vcpu *vcpu)
9024 struct vcpu_vmx *vmx = to_vmx(vcpu);
9025 u32 exit_reason = vmx->exit_reason;
9026 u32 vectoring_info = vmx->idt_vectoring_info;
9028 trace_kvm_exit(exit_reason, vcpu, KVM_ISA_VMX);
9031 * Flush logged GPAs PML buffer, this will make dirty_bitmap more
9032 * updated. Another good is, in kvm_vm_ioctl_get_dirty_log, before
9033 * querying dirty_bitmap, we only need to kick all vcpus out of guest
9034 * mode as if vcpus is in root mode, the PML buffer must has been
9038 vmx_flush_pml_buffer(vcpu);
9040 /* If guest state is invalid, start emulating */
9041 if (vmx->emulation_required)
9042 return handle_invalid_guest_state(vcpu);
9044 if (is_guest_mode(vcpu) && nested_vmx_exit_reflected(vcpu, exit_reason))
9045 return nested_vmx_reflect_vmexit(vcpu, exit_reason);
9047 if (exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY) {
9049 vcpu->run->exit_reason = KVM_EXIT_FAIL_ENTRY;
9050 vcpu->run->fail_entry.hardware_entry_failure_reason
9055 if (unlikely(vmx->fail)) {
9056 vcpu->run->exit_reason = KVM_EXIT_FAIL_ENTRY;
9057 vcpu->run->fail_entry.hardware_entry_failure_reason
9058 = vmcs_read32(VM_INSTRUCTION_ERROR);
9064 * Do not try to fix EXIT_REASON_EPT_MISCONFIG if it caused by
9065 * delivery event since it indicates guest is accessing MMIO.
9066 * The vm-exit can be triggered again after return to guest that
9067 * will cause infinite loop.
9069 if ((vectoring_info & VECTORING_INFO_VALID_MASK) &&
9070 (exit_reason != EXIT_REASON_EXCEPTION_NMI &&
9071 exit_reason != EXIT_REASON_EPT_VIOLATION &&
9072 exit_reason != EXIT_REASON_PML_FULL &&
9073 exit_reason != EXIT_REASON_TASK_SWITCH)) {
9074 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
9075 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_DELIVERY_EV;
9076 vcpu->run->internal.ndata = 3;
9077 vcpu->run->internal.data[0] = vectoring_info;
9078 vcpu->run->internal.data[1] = exit_reason;
9079 vcpu->run->internal.data[2] = vcpu->arch.exit_qualification;
9080 if (exit_reason == EXIT_REASON_EPT_MISCONFIG) {
9081 vcpu->run->internal.ndata++;
9082 vcpu->run->internal.data[3] =
9083 vmcs_read64(GUEST_PHYSICAL_ADDRESS);
9088 if (unlikely(!cpu_has_virtual_nmis() &&
9089 vmx->loaded_vmcs->soft_vnmi_blocked)) {
9090 if (vmx_interrupt_allowed(vcpu)) {
9091 vmx->loaded_vmcs->soft_vnmi_blocked = 0;
9092 } else if (vmx->loaded_vmcs->vnmi_blocked_time > 1000000000LL &&
9093 vcpu->arch.nmi_pending) {
9095 * This CPU don't support us in finding the end of an
9096 * NMI-blocked window if the guest runs with IRQs
9097 * disabled. So we pull the trigger after 1 s of
9098 * futile waiting, but inform the user about this.
9100 printk(KERN_WARNING "%s: Breaking out of NMI-blocked "
9101 "state on VCPU %d after 1 s timeout\n",
9102 __func__, vcpu->vcpu_id);
9103 vmx->loaded_vmcs->soft_vnmi_blocked = 0;
9107 if (exit_reason < kvm_vmx_max_exit_handlers
9108 && kvm_vmx_exit_handlers[exit_reason])
9109 return kvm_vmx_exit_handlers[exit_reason](vcpu);
9111 vcpu_unimpl(vcpu, "vmx: unexpected exit reason 0x%x\n",
9113 kvm_queue_exception(vcpu, UD_VECTOR);
9119 * Software based L1D cache flush which is used when microcode providing
9120 * the cache control MSR is not loaded.
9122 * The L1D cache is 32 KiB on Nehalem and later microarchitectures, but to
9123 * flush it is required to read in 64 KiB because the replacement algorithm
9124 * is not exactly LRU. This could be sized at runtime via topology
9125 * information but as all relevant affected CPUs have 32KiB L1D cache size
9126 * there is no point in doing so.
9128 #define L1D_CACHE_ORDER 4
9129 static void *vmx_l1d_flush_pages;
9131 static void vmx_l1d_flush(struct kvm_vcpu *vcpu)
9133 int size = PAGE_SIZE << L1D_CACHE_ORDER;
9136 * This code is only executed when the the flush mode is 'cond' or
9139 * If 'flush always', keep the flush bit set, otherwise clear
9140 * it. The flush bit gets set again either from vcpu_run() or from
9141 * one of the unsafe VMEXIT handlers.
9143 if (static_branch_unlikely(&vmx_l1d_flush_always))
9144 vcpu->arch.l1tf_flush_l1d = true;
9146 vcpu->arch.l1tf_flush_l1d = false;
9148 vcpu->stat.l1d_flush++;
9150 if (static_cpu_has(X86_FEATURE_FLUSH_L1D)) {
9151 wrmsrl(MSR_IA32_FLUSH_CMD, L1D_FLUSH);
9156 /* First ensure the pages are in the TLB */
9157 "xorl %%eax, %%eax\n"
9158 ".Lpopulate_tlb:\n\t"
9159 "movzbl (%[empty_zp], %%" _ASM_AX "), %%ecx\n\t"
9160 "addl $4096, %%eax\n\t"
9161 "cmpl %%eax, %[size]\n\t"
9162 "jne .Lpopulate_tlb\n\t"
9163 "xorl %%eax, %%eax\n\t"
9165 /* Now fill the cache */
9166 "xorl %%eax, %%eax\n"
9168 "movzbl (%[empty_zp], %%" _ASM_AX "), %%ecx\n\t"
9169 "addl $64, %%eax\n\t"
9170 "cmpl %%eax, %[size]\n\t"
9171 "jne .Lfill_cache\n\t"
9173 :: [empty_zp] "r" (vmx_l1d_flush_pages),
9175 : "eax", "ebx", "ecx", "edx");
9178 static void update_cr8_intercept(struct kvm_vcpu *vcpu, int tpr, int irr)
9180 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
9182 if (is_guest_mode(vcpu) &&
9183 nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW))
9186 if (irr == -1 || tpr < irr) {
9187 vmcs_write32(TPR_THRESHOLD, 0);
9191 vmcs_write32(TPR_THRESHOLD, irr);
9194 static void vmx_set_virtual_x2apic_mode(struct kvm_vcpu *vcpu, bool set)
9196 u32 sec_exec_control;
9198 /* Postpone execution until vmcs01 is the current VMCS. */
9199 if (is_guest_mode(vcpu)) {
9200 to_vmx(vcpu)->nested.change_vmcs01_virtual_x2apic_mode = true;
9204 if (!cpu_has_vmx_virtualize_x2apic_mode())
9207 if (!cpu_need_tpr_shadow(vcpu))
9210 sec_exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
9213 sec_exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
9214 sec_exec_control |= SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
9216 sec_exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
9217 sec_exec_control |= SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
9218 vmx_flush_tlb_ept_only(vcpu);
9220 vmcs_write32(SECONDARY_VM_EXEC_CONTROL, sec_exec_control);
9222 vmx_update_msr_bitmap(vcpu);
9225 static void vmx_set_apic_access_page_addr(struct kvm_vcpu *vcpu, hpa_t hpa)
9227 struct vcpu_vmx *vmx = to_vmx(vcpu);
9230 * Currently we do not handle the nested case where L2 has an
9231 * APIC access page of its own; that page is still pinned.
9232 * Hence, we skip the case where the VCPU is in guest mode _and_
9233 * L1 prepared an APIC access page for L2.
9235 * For the case where L1 and L2 share the same APIC access page
9236 * (flexpriority=Y but SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES clear
9237 * in the vmcs12), this function will only update either the vmcs01
9238 * or the vmcs02. If the former, the vmcs02 will be updated by
9239 * prepare_vmcs02. If the latter, the vmcs01 will be updated in
9240 * the next L2->L1 exit.
9242 if (!is_guest_mode(vcpu) ||
9243 !nested_cpu_has2(get_vmcs12(&vmx->vcpu),
9244 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) {
9245 vmcs_write64(APIC_ACCESS_ADDR, hpa);
9246 vmx_flush_tlb_ept_only(vcpu);
9250 static void vmx_hwapic_isr_update(struct kvm_vcpu *vcpu, int max_isr)
9258 status = vmcs_read16(GUEST_INTR_STATUS);
9260 if (max_isr != old) {
9262 status |= max_isr << 8;
9263 vmcs_write16(GUEST_INTR_STATUS, status);
9267 static void vmx_set_rvi(int vector)
9275 status = vmcs_read16(GUEST_INTR_STATUS);
9276 old = (u8)status & 0xff;
9277 if ((u8)vector != old) {
9279 status |= (u8)vector;
9280 vmcs_write16(GUEST_INTR_STATUS, status);
9284 static void vmx_hwapic_irr_update(struct kvm_vcpu *vcpu, int max_irr)
9286 if (!is_guest_mode(vcpu)) {
9287 vmx_set_rvi(max_irr);
9295 * In guest mode. If a vmexit is needed, vmx_check_nested_events
9298 if (nested_exit_on_intr(vcpu))
9302 * Else, fall back to pre-APICv interrupt injection since L2
9303 * is run without virtual interrupt delivery.
9305 if (!kvm_event_needs_reinjection(vcpu) &&
9306 vmx_interrupt_allowed(vcpu)) {
9307 kvm_queue_interrupt(vcpu, max_irr, false);
9308 vmx_inject_irq(vcpu);
9312 static int vmx_sync_pir_to_irr(struct kvm_vcpu *vcpu)
9314 struct vcpu_vmx *vmx = to_vmx(vcpu);
9317 WARN_ON(!vcpu->arch.apicv_active);
9318 if (pi_test_on(&vmx->pi_desc)) {
9319 pi_clear_on(&vmx->pi_desc);
9321 * IOMMU can write to PIR.ON, so the barrier matters even on UP.
9322 * But on x86 this is just a compiler barrier anyway.
9324 smp_mb__after_atomic();
9325 max_irr = kvm_apic_update_irr(vcpu, vmx->pi_desc.pir);
9327 max_irr = kvm_lapic_find_highest_irr(vcpu);
9329 vmx_hwapic_irr_update(vcpu, max_irr);
9333 static void vmx_load_eoi_exitmap(struct kvm_vcpu *vcpu, u64 *eoi_exit_bitmap)
9335 if (!kvm_vcpu_apicv_active(vcpu))
9338 vmcs_write64(EOI_EXIT_BITMAP0, eoi_exit_bitmap[0]);
9339 vmcs_write64(EOI_EXIT_BITMAP1, eoi_exit_bitmap[1]);
9340 vmcs_write64(EOI_EXIT_BITMAP2, eoi_exit_bitmap[2]);
9341 vmcs_write64(EOI_EXIT_BITMAP3, eoi_exit_bitmap[3]);
9344 static void vmx_apicv_post_state_restore(struct kvm_vcpu *vcpu)
9346 struct vcpu_vmx *vmx = to_vmx(vcpu);
9348 pi_clear_on(&vmx->pi_desc);
9349 memset(vmx->pi_desc.pir, 0, sizeof(vmx->pi_desc.pir));
9352 static void vmx_complete_atomic_exit(struct vcpu_vmx *vmx)
9354 u32 exit_intr_info = 0;
9355 u16 basic_exit_reason = (u16)vmx->exit_reason;
9357 if (!(basic_exit_reason == EXIT_REASON_MCE_DURING_VMENTRY
9358 || basic_exit_reason == EXIT_REASON_EXCEPTION_NMI))
9361 if (!(vmx->exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY))
9362 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
9363 vmx->exit_intr_info = exit_intr_info;
9365 /* if exit due to PF check for async PF */
9366 if (is_page_fault(exit_intr_info))
9367 vmx->vcpu.arch.apf.host_apf_reason = kvm_read_and_reset_pf_reason();
9369 /* Handle machine checks before interrupts are enabled */
9370 if (basic_exit_reason == EXIT_REASON_MCE_DURING_VMENTRY ||
9371 is_machine_check(exit_intr_info))
9372 kvm_machine_check();
9374 /* We need to handle NMIs before interrupts are enabled */
9375 if (is_nmi(exit_intr_info)) {
9376 kvm_before_handle_nmi(&vmx->vcpu);
9378 kvm_after_handle_nmi(&vmx->vcpu);
9382 static void vmx_handle_external_intr(struct kvm_vcpu *vcpu)
9384 u32 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
9386 if ((exit_intr_info & (INTR_INFO_VALID_MASK | INTR_INFO_INTR_TYPE_MASK))
9387 == (INTR_INFO_VALID_MASK | INTR_TYPE_EXT_INTR)) {
9388 unsigned int vector;
9389 unsigned long entry;
9391 struct vcpu_vmx *vmx = to_vmx(vcpu);
9392 #ifdef CONFIG_X86_64
9396 vector = exit_intr_info & INTR_INFO_VECTOR_MASK;
9397 desc = (gate_desc *)vmx->host_idt_base + vector;
9398 entry = gate_offset(desc);
9400 #ifdef CONFIG_X86_64
9401 "mov %%" _ASM_SP ", %[sp]\n\t"
9402 "and $0xfffffffffffffff0, %%" _ASM_SP "\n\t"
9407 __ASM_SIZE(push) " $%c[cs]\n\t"
9410 #ifdef CONFIG_X86_64
9415 THUNK_TARGET(entry),
9416 [ss]"i"(__KERNEL_DS),
9417 [cs]"i"(__KERNEL_CS)
9419 vcpu->arch.l1tf_flush_l1d = true;
9422 STACK_FRAME_NON_STANDARD(vmx_handle_external_intr);
9424 static bool vmx_has_emulated_msr(int index)
9427 case MSR_IA32_SMBASE:
9429 * We cannot do SMM unless we can run the guest in big
9432 return enable_unrestricted_guest || emulate_invalid_guest_state;
9433 case MSR_AMD64_VIRT_SPEC_CTRL:
9434 /* This is AMD only. */
9441 static bool vmx_mpx_supported(void)
9443 return (vmcs_config.vmexit_ctrl & VM_EXIT_CLEAR_BNDCFGS) &&
9444 (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_BNDCFGS);
9447 static bool vmx_xsaves_supported(void)
9449 return vmcs_config.cpu_based_2nd_exec_ctrl &
9450 SECONDARY_EXEC_XSAVES;
9453 static void vmx_recover_nmi_blocking(struct vcpu_vmx *vmx)
9458 bool idtv_info_valid;
9460 idtv_info_valid = vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK;
9462 if (cpu_has_virtual_nmis()) {
9463 if (vmx->loaded_vmcs->nmi_known_unmasked)
9466 * Can't use vmx->exit_intr_info since we're not sure what
9467 * the exit reason is.
9469 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
9470 unblock_nmi = (exit_intr_info & INTR_INFO_UNBLOCK_NMI) != 0;
9471 vector = exit_intr_info & INTR_INFO_VECTOR_MASK;
9473 * SDM 3: 27.7.1.2 (September 2008)
9474 * Re-set bit "block by NMI" before VM entry if vmexit caused by
9475 * a guest IRET fault.
9476 * SDM 3: 23.2.2 (September 2008)
9477 * Bit 12 is undefined in any of the following cases:
9478 * If the VM exit sets the valid bit in the IDT-vectoring
9479 * information field.
9480 * If the VM exit is due to a double fault.
9482 if ((exit_intr_info & INTR_INFO_VALID_MASK) && unblock_nmi &&
9483 vector != DF_VECTOR && !idtv_info_valid)
9484 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
9485 GUEST_INTR_STATE_NMI);
9487 vmx->loaded_vmcs->nmi_known_unmasked =
9488 !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO)
9489 & GUEST_INTR_STATE_NMI);
9490 } else if (unlikely(vmx->loaded_vmcs->soft_vnmi_blocked))
9491 vmx->loaded_vmcs->vnmi_blocked_time +=
9492 ktime_to_ns(ktime_sub(ktime_get(),
9493 vmx->loaded_vmcs->entry_time));
9496 static void __vmx_complete_interrupts(struct kvm_vcpu *vcpu,
9497 u32 idt_vectoring_info,
9498 int instr_len_field,
9499 int error_code_field)
9503 bool idtv_info_valid;
9505 idtv_info_valid = idt_vectoring_info & VECTORING_INFO_VALID_MASK;
9507 vcpu->arch.nmi_injected = false;
9508 kvm_clear_exception_queue(vcpu);
9509 kvm_clear_interrupt_queue(vcpu);
9511 if (!idtv_info_valid)
9514 kvm_make_request(KVM_REQ_EVENT, vcpu);
9516 vector = idt_vectoring_info & VECTORING_INFO_VECTOR_MASK;
9517 type = idt_vectoring_info & VECTORING_INFO_TYPE_MASK;
9520 case INTR_TYPE_NMI_INTR:
9521 vcpu->arch.nmi_injected = true;
9523 * SDM 3: 27.7.1.2 (September 2008)
9524 * Clear bit "block by NMI" before VM entry if a NMI
9527 vmx_set_nmi_mask(vcpu, false);
9529 case INTR_TYPE_SOFT_EXCEPTION:
9530 vcpu->arch.event_exit_inst_len = vmcs_read32(instr_len_field);
9532 case INTR_TYPE_HARD_EXCEPTION:
9533 if (idt_vectoring_info & VECTORING_INFO_DELIVER_CODE_MASK) {
9534 u32 err = vmcs_read32(error_code_field);
9535 kvm_requeue_exception_e(vcpu, vector, err);
9537 kvm_requeue_exception(vcpu, vector);
9539 case INTR_TYPE_SOFT_INTR:
9540 vcpu->arch.event_exit_inst_len = vmcs_read32(instr_len_field);
9542 case INTR_TYPE_EXT_INTR:
9543 kvm_queue_interrupt(vcpu, vector, type == INTR_TYPE_SOFT_INTR);
9550 static void vmx_complete_interrupts(struct vcpu_vmx *vmx)
9552 __vmx_complete_interrupts(&vmx->vcpu, vmx->idt_vectoring_info,
9553 VM_EXIT_INSTRUCTION_LEN,
9554 IDT_VECTORING_ERROR_CODE);
9557 static void vmx_cancel_injection(struct kvm_vcpu *vcpu)
9559 __vmx_complete_interrupts(vcpu,
9560 vmcs_read32(VM_ENTRY_INTR_INFO_FIELD),
9561 VM_ENTRY_INSTRUCTION_LEN,
9562 VM_ENTRY_EXCEPTION_ERROR_CODE);
9564 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0);
9567 static void atomic_switch_perf_msrs(struct vcpu_vmx *vmx)
9570 struct perf_guest_switch_msr *msrs;
9572 msrs = perf_guest_get_msrs(&nr_msrs);
9577 for (i = 0; i < nr_msrs; i++)
9578 if (msrs[i].host == msrs[i].guest)
9579 clear_atomic_switch_msr(vmx, msrs[i].msr);
9581 add_atomic_switch_msr(vmx, msrs[i].msr, msrs[i].guest,
9582 msrs[i].host, false);
9585 static void vmx_arm_hv_timer(struct kvm_vcpu *vcpu)
9587 struct vcpu_vmx *vmx = to_vmx(vcpu);
9591 if (vmx->hv_deadline_tsc == -1)
9595 if (vmx->hv_deadline_tsc > tscl)
9596 /* sure to be 32 bit only because checked on set_hv_timer */
9597 delta_tsc = (u32)((vmx->hv_deadline_tsc - tscl) >>
9598 cpu_preemption_timer_multi);
9602 vmcs_write32(VMX_PREEMPTION_TIMER_VALUE, delta_tsc);
9605 static void __noclone vmx_vcpu_run(struct kvm_vcpu *vcpu)
9607 struct vcpu_vmx *vmx = to_vmx(vcpu);
9608 unsigned long debugctlmsr, cr3, cr4;
9610 /* Record the guest's net vcpu time for enforced NMI injections. */
9611 if (unlikely(!cpu_has_virtual_nmis() &&
9612 vmx->loaded_vmcs->soft_vnmi_blocked))
9613 vmx->loaded_vmcs->entry_time = ktime_get();
9615 /* Don't enter VMX if guest state is invalid, let the exit handler
9616 start emulation until we arrive back to a valid state */
9617 if (vmx->emulation_required)
9620 if (vmx->ple_window_dirty) {
9621 vmx->ple_window_dirty = false;
9622 vmcs_write32(PLE_WINDOW, vmx->ple_window);
9625 if (vmx->nested.sync_shadow_vmcs) {
9626 copy_vmcs12_to_shadow(vmx);
9627 vmx->nested.sync_shadow_vmcs = false;
9630 if (test_bit(VCPU_REGS_RSP, (unsigned long *)&vcpu->arch.regs_dirty))
9631 vmcs_writel(GUEST_RSP, vcpu->arch.regs[VCPU_REGS_RSP]);
9632 if (test_bit(VCPU_REGS_RIP, (unsigned long *)&vcpu->arch.regs_dirty))
9633 vmcs_writel(GUEST_RIP, vcpu->arch.regs[VCPU_REGS_RIP]);
9635 cr3 = __get_current_cr3_fast();
9636 if (unlikely(cr3 != vmx->loaded_vmcs->vmcs_host_cr3)) {
9637 vmcs_writel(HOST_CR3, cr3);
9638 vmx->loaded_vmcs->vmcs_host_cr3 = cr3;
9641 cr4 = cr4_read_shadow();
9642 if (unlikely(cr4 != vmx->loaded_vmcs->vmcs_host_cr4)) {
9643 vmcs_writel(HOST_CR4, cr4);
9644 vmx->loaded_vmcs->vmcs_host_cr4 = cr4;
9647 /* When single-stepping over STI and MOV SS, we must clear the
9648 * corresponding interruptibility bits in the guest state. Otherwise
9649 * vmentry fails as it then expects bit 14 (BS) in pending debug
9650 * exceptions being set, but that's not correct for the guest debugging
9652 if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP)
9653 vmx_set_interrupt_shadow(vcpu, 0);
9655 if (static_cpu_has(X86_FEATURE_PKU) &&
9656 kvm_read_cr4_bits(vcpu, X86_CR4_PKE) &&
9657 vcpu->arch.pkru != vmx->host_pkru)
9658 __write_pkru(vcpu->arch.pkru);
9660 atomic_switch_perf_msrs(vmx);
9661 debugctlmsr = get_debugctlmsr();
9663 vmx_arm_hv_timer(vcpu);
9666 * If this vCPU has touched SPEC_CTRL, restore the guest's value if
9667 * it's non-zero. Since vmentry is serialising on affected CPUs, there
9668 * is no need to worry about the conditional branch over the wrmsr
9669 * being speculatively taken.
9671 x86_spec_ctrl_set_guest(vmx->spec_ctrl, 0);
9673 vmx->__launched = vmx->loaded_vmcs->launched;
9675 if (static_branch_unlikely(&vmx_l1d_should_flush)) {
9676 if (vcpu->arch.l1tf_flush_l1d)
9677 vmx_l1d_flush(vcpu);
9681 /* Store host registers */
9682 "push %%" _ASM_DX "; push %%" _ASM_BP ";"
9683 "push %%" _ASM_CX " \n\t" /* placeholder for guest rcx */
9684 "push %%" _ASM_CX " \n\t"
9685 "cmp %%" _ASM_SP ", %c[host_rsp](%0) \n\t"
9687 "mov %%" _ASM_SP ", %c[host_rsp](%0) \n\t"
9688 __ex(ASM_VMX_VMWRITE_RSP_RDX) "\n\t"
9690 /* Reload cr2 if changed */
9691 "mov %c[cr2](%0), %%" _ASM_AX " \n\t"
9692 "mov %%cr2, %%" _ASM_DX " \n\t"
9693 "cmp %%" _ASM_AX ", %%" _ASM_DX " \n\t"
9695 "mov %%" _ASM_AX", %%cr2 \n\t"
9697 /* Check if vmlaunch of vmresume is needed */
9698 "cmpl $0, %c[launched](%0) \n\t"
9699 /* Load guest registers. Don't clobber flags. */
9700 "mov %c[rax](%0), %%" _ASM_AX " \n\t"
9701 "mov %c[rbx](%0), %%" _ASM_BX " \n\t"
9702 "mov %c[rdx](%0), %%" _ASM_DX " \n\t"
9703 "mov %c[rsi](%0), %%" _ASM_SI " \n\t"
9704 "mov %c[rdi](%0), %%" _ASM_DI " \n\t"
9705 "mov %c[rbp](%0), %%" _ASM_BP " \n\t"
9706 #ifdef CONFIG_X86_64
9707 "mov %c[r8](%0), %%r8 \n\t"
9708 "mov %c[r9](%0), %%r9 \n\t"
9709 "mov %c[r10](%0), %%r10 \n\t"
9710 "mov %c[r11](%0), %%r11 \n\t"
9711 "mov %c[r12](%0), %%r12 \n\t"
9712 "mov %c[r13](%0), %%r13 \n\t"
9713 "mov %c[r14](%0), %%r14 \n\t"
9714 "mov %c[r15](%0), %%r15 \n\t"
9716 "mov %c[rcx](%0), %%" _ASM_CX " \n\t" /* kills %0 (ecx) */
9718 /* Enter guest mode */
9720 __ex(ASM_VMX_VMLAUNCH) "\n\t"
9722 "1: " __ex(ASM_VMX_VMRESUME) "\n\t"
9724 /* Save guest registers, load host registers, keep flags */
9725 "mov %0, %c[wordsize](%%" _ASM_SP ") \n\t"
9727 "setbe %c[fail](%0)\n\t"
9728 "mov %%" _ASM_AX ", %c[rax](%0) \n\t"
9729 "mov %%" _ASM_BX ", %c[rbx](%0) \n\t"
9730 __ASM_SIZE(pop) " %c[rcx](%0) \n\t"
9731 "mov %%" _ASM_DX ", %c[rdx](%0) \n\t"
9732 "mov %%" _ASM_SI ", %c[rsi](%0) \n\t"
9733 "mov %%" _ASM_DI ", %c[rdi](%0) \n\t"
9734 "mov %%" _ASM_BP ", %c[rbp](%0) \n\t"
9735 #ifdef CONFIG_X86_64
9736 "mov %%r8, %c[r8](%0) \n\t"
9737 "mov %%r9, %c[r9](%0) \n\t"
9738 "mov %%r10, %c[r10](%0) \n\t"
9739 "mov %%r11, %c[r11](%0) \n\t"
9740 "mov %%r12, %c[r12](%0) \n\t"
9741 "mov %%r13, %c[r13](%0) \n\t"
9742 "mov %%r14, %c[r14](%0) \n\t"
9743 "mov %%r15, %c[r15](%0) \n\t"
9744 "xor %%r8d, %%r8d \n\t"
9745 "xor %%r9d, %%r9d \n\t"
9746 "xor %%r10d, %%r10d \n\t"
9747 "xor %%r11d, %%r11d \n\t"
9748 "xor %%r12d, %%r12d \n\t"
9749 "xor %%r13d, %%r13d \n\t"
9750 "xor %%r14d, %%r14d \n\t"
9751 "xor %%r15d, %%r15d \n\t"
9753 "mov %%cr2, %%" _ASM_AX " \n\t"
9754 "mov %%" _ASM_AX ", %c[cr2](%0) \n\t"
9756 "xor %%eax, %%eax \n\t"
9757 "xor %%ebx, %%ebx \n\t"
9758 "xor %%esi, %%esi \n\t"
9759 "xor %%edi, %%edi \n\t"
9760 "pop %%" _ASM_BP "; pop %%" _ASM_DX " \n\t"
9761 ".pushsection .rodata \n\t"
9762 ".global vmx_return \n\t"
9763 "vmx_return: " _ASM_PTR " 2b \n\t"
9765 : : "c"(vmx), "d"((unsigned long)HOST_RSP),
9766 [launched]"i"(offsetof(struct vcpu_vmx, __launched)),
9767 [fail]"i"(offsetof(struct vcpu_vmx, fail)),
9768 [host_rsp]"i"(offsetof(struct vcpu_vmx, host_rsp)),
9769 [rax]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RAX])),
9770 [rbx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBX])),
9771 [rcx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RCX])),
9772 [rdx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDX])),
9773 [rsi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RSI])),
9774 [rdi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDI])),
9775 [rbp]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBP])),
9776 #ifdef CONFIG_X86_64
9777 [r8]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R8])),
9778 [r9]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R9])),
9779 [r10]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R10])),
9780 [r11]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R11])),
9781 [r12]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R12])),
9782 [r13]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R13])),
9783 [r14]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R14])),
9784 [r15]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R15])),
9786 [cr2]"i"(offsetof(struct vcpu_vmx, vcpu.arch.cr2)),
9787 [wordsize]"i"(sizeof(ulong))
9789 #ifdef CONFIG_X86_64
9790 , "rax", "rbx", "rdi", "rsi"
9791 , "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15"
9793 , "eax", "ebx", "edi", "esi"
9798 * We do not use IBRS in the kernel. If this vCPU has used the
9799 * SPEC_CTRL MSR it may have left it on; save the value and
9800 * turn it off. This is much more efficient than blindly adding
9801 * it to the atomic save/restore list. Especially as the former
9802 * (Saving guest MSRs on vmexit) doesn't even exist in KVM.
9804 * For non-nested case:
9805 * If the L01 MSR bitmap does not intercept the MSR, then we need to
9809 * If the L02 MSR bitmap does not intercept the MSR, then we need to
9812 if (unlikely(!msr_write_intercepted(vcpu, MSR_IA32_SPEC_CTRL)))
9813 vmx->spec_ctrl = native_read_msr(MSR_IA32_SPEC_CTRL);
9815 x86_spec_ctrl_restore_host(vmx->spec_ctrl, 0);
9817 /* Eliminate branch target predictions from guest mode */
9820 /* MSR_IA32_DEBUGCTLMSR is zeroed on vmexit. Restore it if needed */
9822 update_debugctlmsr(debugctlmsr);
9824 #ifndef CONFIG_X86_64
9826 * The sysexit path does not restore ds/es, so we must set them to
9827 * a reasonable value ourselves.
9829 * We can't defer this to vmx_load_host_state() since that function
9830 * may be executed in interrupt context, which saves and restore segments
9831 * around it, nullifying its effect.
9833 loadsegment(ds, __USER_DS);
9834 loadsegment(es, __USER_DS);
9837 vcpu->arch.regs_avail = ~((1 << VCPU_REGS_RIP) | (1 << VCPU_REGS_RSP)
9838 | (1 << VCPU_EXREG_RFLAGS)
9839 | (1 << VCPU_EXREG_PDPTR)
9840 | (1 << VCPU_EXREG_SEGMENTS)
9841 | (1 << VCPU_EXREG_CR3));
9842 vcpu->arch.regs_dirty = 0;
9845 * eager fpu is enabled if PKEY is supported and CR4 is switched
9846 * back on host, so it is safe to read guest PKRU from current
9849 if (static_cpu_has(X86_FEATURE_PKU) &&
9850 kvm_read_cr4_bits(vcpu, X86_CR4_PKE)) {
9851 vcpu->arch.pkru = __read_pkru();
9852 if (vcpu->arch.pkru != vmx->host_pkru)
9853 __write_pkru(vmx->host_pkru);
9857 * the KVM_REQ_EVENT optimization bit is only on for one entry, and if
9858 * we did not inject a still-pending event to L1 now because of
9859 * nested_run_pending, we need to re-enable this bit.
9861 if (vmx->nested.nested_run_pending)
9862 kvm_make_request(KVM_REQ_EVENT, vcpu);
9864 vmx->nested.nested_run_pending = 0;
9865 vmx->idt_vectoring_info = 0;
9867 vmx->exit_reason = vmx->fail ? 0xdead : vmcs_read32(VM_EXIT_REASON);
9868 if (vmx->fail || (vmx->exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY))
9871 vmx->loaded_vmcs->launched = 1;
9872 vmx->idt_vectoring_info = vmcs_read32(IDT_VECTORING_INFO_FIELD);
9874 vmx_complete_atomic_exit(vmx);
9875 vmx_recover_nmi_blocking(vmx);
9876 vmx_complete_interrupts(vmx);
9878 STACK_FRAME_NON_STANDARD(vmx_vcpu_run);
9880 static void vmx_switch_vmcs(struct kvm_vcpu *vcpu, struct loaded_vmcs *vmcs)
9882 struct vcpu_vmx *vmx = to_vmx(vcpu);
9885 if (vmx->loaded_vmcs == vmcs)
9889 vmx->loaded_vmcs = vmcs;
9891 vmx_vcpu_load(vcpu, cpu);
9897 * Ensure that the current vmcs of the logical processor is the
9898 * vmcs01 of the vcpu before calling free_nested().
9900 static void vmx_free_vcpu_nested(struct kvm_vcpu *vcpu)
9902 struct vcpu_vmx *vmx = to_vmx(vcpu);
9905 r = vcpu_load(vcpu);
9907 vmx_switch_vmcs(vcpu, &vmx->vmcs01);
9912 static void vmx_free_vcpu(struct kvm_vcpu *vcpu)
9914 struct vcpu_vmx *vmx = to_vmx(vcpu);
9917 vmx_destroy_pml_buffer(vmx);
9918 free_vpid(vmx->vpid);
9919 leave_guest_mode(vcpu);
9920 vmx_free_vcpu_nested(vcpu);
9921 free_loaded_vmcs(vmx->loaded_vmcs);
9922 kfree(vmx->guest_msrs);
9923 kvm_vcpu_uninit(vcpu);
9924 kmem_cache_free(kvm_vcpu_cache, vmx);
9927 static struct kvm_vcpu *vmx_create_vcpu(struct kvm *kvm, unsigned int id)
9930 struct vcpu_vmx *vmx = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
9931 unsigned long *msr_bitmap;
9935 return ERR_PTR(-ENOMEM);
9937 vmx->vpid = allocate_vpid();
9939 err = kvm_vcpu_init(&vmx->vcpu, kvm, id);
9946 * If PML is turned on, failure on enabling PML just results in failure
9947 * of creating the vcpu, therefore we can simplify PML logic (by
9948 * avoiding dealing with cases, such as enabling PML partially on vcpus
9949 * for the guest, etc.
9952 vmx->pml_pg = alloc_page(GFP_KERNEL | __GFP_ZERO);
9957 vmx->guest_msrs = kmalloc(PAGE_SIZE, GFP_KERNEL);
9958 BUILD_BUG_ON(ARRAY_SIZE(vmx_msr_index) * sizeof(vmx->guest_msrs[0])
9961 if (!vmx->guest_msrs)
9964 err = alloc_loaded_vmcs(&vmx->vmcs01);
9968 msr_bitmap = vmx->vmcs01.msr_bitmap;
9969 vmx_disable_intercept_for_msr(msr_bitmap, MSR_FS_BASE, MSR_TYPE_RW);
9970 vmx_disable_intercept_for_msr(msr_bitmap, MSR_GS_BASE, MSR_TYPE_RW);
9971 vmx_disable_intercept_for_msr(msr_bitmap, MSR_KERNEL_GS_BASE, MSR_TYPE_RW);
9972 vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_SYSENTER_CS, MSR_TYPE_RW);
9973 vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_SYSENTER_ESP, MSR_TYPE_RW);
9974 vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_SYSENTER_EIP, MSR_TYPE_RW);
9975 vmx->msr_bitmap_mode = 0;
9977 vmx->loaded_vmcs = &vmx->vmcs01;
9979 vmx_vcpu_load(&vmx->vcpu, cpu);
9980 vmx->vcpu.cpu = cpu;
9981 err = vmx_vcpu_setup(vmx);
9982 vmx_vcpu_put(&vmx->vcpu);
9986 if (cpu_need_virtualize_apic_accesses(&vmx->vcpu)) {
9987 err = alloc_apic_access_page(kvm);
9993 if (!kvm->arch.ept_identity_map_addr)
9994 kvm->arch.ept_identity_map_addr =
9995 VMX_EPT_IDENTITY_PAGETABLE_ADDR;
9996 err = init_rmode_identity_map(kvm);
10002 nested_vmx_setup_ctls_msrs(vmx);
10004 vmx->nested.posted_intr_nv = -1;
10005 vmx->nested.current_vmptr = -1ull;
10007 vmx->msr_ia32_feature_control_valid_bits = FEATURE_CONTROL_LOCKED;
10010 * Enforce invariant: pi_desc.nv is always either POSTED_INTR_VECTOR
10011 * or POSTED_INTR_WAKEUP_VECTOR.
10013 vmx->pi_desc.nv = POSTED_INTR_VECTOR;
10014 vmx->pi_desc.sn = 1;
10019 free_loaded_vmcs(vmx->loaded_vmcs);
10021 kfree(vmx->guest_msrs);
10023 vmx_destroy_pml_buffer(vmx);
10025 kvm_vcpu_uninit(&vmx->vcpu);
10027 free_vpid(vmx->vpid);
10028 kmem_cache_free(kvm_vcpu_cache, vmx);
10029 return ERR_PTR(err);
10032 #define L1TF_MSG "SMT enabled with L1TF CPU bug present. Refer to CVE-2018-3620 for details.\n"
10034 static int vmx_vm_init(struct kvm *kvm)
10036 if (boot_cpu_has(X86_BUG_L1TF) && cpu_smt_control == CPU_SMT_ENABLED) {
10039 return -EOPNOTSUPP;
10046 static void __init vmx_check_processor_compat(void *rtn)
10048 struct vmcs_config vmcs_conf;
10051 if (setup_vmcs_config(&vmcs_conf) < 0)
10052 *(int *)rtn = -EIO;
10053 if (memcmp(&vmcs_config, &vmcs_conf, sizeof(struct vmcs_config)) != 0) {
10054 printk(KERN_ERR "kvm: CPU %d feature inconsistency!\n",
10055 smp_processor_id());
10056 *(int *)rtn = -EIO;
10060 static u64 vmx_get_mt_mask(struct kvm_vcpu *vcpu, gfn_t gfn, bool is_mmio)
10065 /* For VT-d and EPT combination
10066 * 1. MMIO: always map as UC
10067 * 2. EPT with VT-d:
10068 * a. VT-d without snooping control feature: can't guarantee the
10069 * result, try to trust guest.
10070 * b. VT-d with snooping control feature: snooping control feature of
10071 * VT-d engine can guarantee the cache correctness. Just set it
10072 * to WB to keep consistent with host. So the same as item 3.
10073 * 3. EPT without VT-d: always map as WB and set IPAT=1 to keep
10074 * consistent with host MTRR
10077 cache = MTRR_TYPE_UNCACHABLE;
10081 if (!kvm_arch_has_noncoherent_dma(vcpu->kvm)) {
10082 ipat = VMX_EPT_IPAT_BIT;
10083 cache = MTRR_TYPE_WRBACK;
10087 if (kvm_read_cr0(vcpu) & X86_CR0_CD) {
10088 ipat = VMX_EPT_IPAT_BIT;
10089 if (kvm_check_has_quirk(vcpu->kvm, KVM_X86_QUIRK_CD_NW_CLEARED))
10090 cache = MTRR_TYPE_WRBACK;
10092 cache = MTRR_TYPE_UNCACHABLE;
10096 cache = kvm_mtrr_get_guest_memory_type(vcpu, gfn);
10099 return (cache << VMX_EPT_MT_EPTE_SHIFT) | ipat;
10102 static int vmx_get_lpage_level(void)
10104 if (enable_ept && !cpu_has_vmx_ept_1g_page())
10105 return PT_DIRECTORY_LEVEL;
10107 /* For shadow and EPT supported 1GB page */
10108 return PT_PDPE_LEVEL;
10111 static void vmcs_set_secondary_exec_control(u32 new_ctl)
10114 * These bits in the secondary execution controls field
10115 * are dynamic, the others are mostly based on the hypervisor
10116 * architecture and the guest's CPUID. Do not touch the
10120 SECONDARY_EXEC_SHADOW_VMCS |
10121 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
10122 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
10124 u32 cur_ctl = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
10126 vmcs_write32(SECONDARY_VM_EXEC_CONTROL,
10127 (new_ctl & ~mask) | (cur_ctl & mask));
10131 * Generate MSR_IA32_VMX_CR{0,4}_FIXED1 according to CPUID. Only set bits
10132 * (indicating "allowed-1") if they are supported in the guest's CPUID.
10134 static void nested_vmx_cr_fixed1_bits_update(struct kvm_vcpu *vcpu)
10136 struct vcpu_vmx *vmx = to_vmx(vcpu);
10137 struct kvm_cpuid_entry2 *entry;
10139 vmx->nested.nested_vmx_cr0_fixed1 = 0xffffffff;
10140 vmx->nested.nested_vmx_cr4_fixed1 = X86_CR4_PCE;
10142 #define cr4_fixed1_update(_cr4_mask, _reg, _cpuid_mask) do { \
10143 if (entry && (entry->_reg & (_cpuid_mask))) \
10144 vmx->nested.nested_vmx_cr4_fixed1 |= (_cr4_mask); \
10147 entry = kvm_find_cpuid_entry(vcpu, 0x1, 0);
10148 cr4_fixed1_update(X86_CR4_VME, edx, bit(X86_FEATURE_VME));
10149 cr4_fixed1_update(X86_CR4_PVI, edx, bit(X86_FEATURE_VME));
10150 cr4_fixed1_update(X86_CR4_TSD, edx, bit(X86_FEATURE_TSC));
10151 cr4_fixed1_update(X86_CR4_DE, edx, bit(X86_FEATURE_DE));
10152 cr4_fixed1_update(X86_CR4_PSE, edx, bit(X86_FEATURE_PSE));
10153 cr4_fixed1_update(X86_CR4_PAE, edx, bit(X86_FEATURE_PAE));
10154 cr4_fixed1_update(X86_CR4_MCE, edx, bit(X86_FEATURE_MCE));
10155 cr4_fixed1_update(X86_CR4_PGE, edx, bit(X86_FEATURE_PGE));
10156 cr4_fixed1_update(X86_CR4_OSFXSR, edx, bit(X86_FEATURE_FXSR));
10157 cr4_fixed1_update(X86_CR4_OSXMMEXCPT, edx, bit(X86_FEATURE_XMM));
10158 cr4_fixed1_update(X86_CR4_VMXE, ecx, bit(X86_FEATURE_VMX));
10159 cr4_fixed1_update(X86_CR4_SMXE, ecx, bit(X86_FEATURE_SMX));
10160 cr4_fixed1_update(X86_CR4_PCIDE, ecx, bit(X86_FEATURE_PCID));
10161 cr4_fixed1_update(X86_CR4_OSXSAVE, ecx, bit(X86_FEATURE_XSAVE));
10163 entry = kvm_find_cpuid_entry(vcpu, 0x7, 0);
10164 cr4_fixed1_update(X86_CR4_FSGSBASE, ebx, bit(X86_FEATURE_FSGSBASE));
10165 cr4_fixed1_update(X86_CR4_SMEP, ebx, bit(X86_FEATURE_SMEP));
10166 cr4_fixed1_update(X86_CR4_SMAP, ebx, bit(X86_FEATURE_SMAP));
10167 cr4_fixed1_update(X86_CR4_PKE, ecx, bit(X86_FEATURE_PKU));
10168 /* TODO: Use X86_CR4_UMIP and X86_FEATURE_UMIP macros */
10169 cr4_fixed1_update(bit(11), ecx, bit(2));
10171 #undef cr4_fixed1_update
10174 static void vmx_cpuid_update(struct kvm_vcpu *vcpu)
10176 struct vcpu_vmx *vmx = to_vmx(vcpu);
10178 if (cpu_has_secondary_exec_ctrls()) {
10179 vmx_compute_secondary_exec_control(vmx);
10180 vmcs_set_secondary_exec_control(vmx->secondary_exec_control);
10183 if (nested_vmx_allowed(vcpu))
10184 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits |=
10185 FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
10187 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits &=
10188 ~FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
10190 if (nested_vmx_allowed(vcpu))
10191 nested_vmx_cr_fixed1_bits_update(vcpu);
10194 static void vmx_set_supported_cpuid(u32 func, struct kvm_cpuid_entry2 *entry)
10196 if (func == 1 && nested)
10197 entry->ecx |= bit(X86_FEATURE_VMX);
10200 static void nested_ept_inject_page_fault(struct kvm_vcpu *vcpu,
10201 struct x86_exception *fault)
10203 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
10204 struct vcpu_vmx *vmx = to_vmx(vcpu);
10206 unsigned long exit_qualification = vcpu->arch.exit_qualification;
10208 if (vmx->nested.pml_full) {
10209 exit_reason = EXIT_REASON_PML_FULL;
10210 vmx->nested.pml_full = false;
10211 exit_qualification &= INTR_INFO_UNBLOCK_NMI;
10212 } else if (fault->error_code & PFERR_RSVD_MASK)
10213 exit_reason = EXIT_REASON_EPT_MISCONFIG;
10215 exit_reason = EXIT_REASON_EPT_VIOLATION;
10217 nested_vmx_vmexit(vcpu, exit_reason, 0, exit_qualification);
10218 vmcs12->guest_physical_address = fault->address;
10221 static bool nested_ept_ad_enabled(struct kvm_vcpu *vcpu)
10223 return nested_ept_get_cr3(vcpu) & VMX_EPTP_AD_ENABLE_BIT;
10226 /* Callbacks for nested_ept_init_mmu_context: */
10228 static unsigned long nested_ept_get_cr3(struct kvm_vcpu *vcpu)
10230 /* return the page table to be shadowed - in our case, EPT12 */
10231 return get_vmcs12(vcpu)->ept_pointer;
10234 static int nested_ept_init_mmu_context(struct kvm_vcpu *vcpu)
10236 WARN_ON(mmu_is_nested(vcpu));
10237 if (!valid_ept_address(vcpu, nested_ept_get_cr3(vcpu)))
10240 kvm_mmu_unload(vcpu);
10241 kvm_init_shadow_ept_mmu(vcpu,
10242 to_vmx(vcpu)->nested.nested_vmx_ept_caps &
10243 VMX_EPT_EXECUTE_ONLY_BIT,
10244 nested_ept_ad_enabled(vcpu));
10245 vcpu->arch.mmu.set_cr3 = vmx_set_cr3;
10246 vcpu->arch.mmu.get_cr3 = nested_ept_get_cr3;
10247 vcpu->arch.mmu.inject_page_fault = nested_ept_inject_page_fault;
10249 vcpu->arch.walk_mmu = &vcpu->arch.nested_mmu;
10253 static void nested_ept_uninit_mmu_context(struct kvm_vcpu *vcpu)
10255 vcpu->arch.walk_mmu = &vcpu->arch.mmu;
10258 static bool nested_vmx_is_page_fault_vmexit(struct vmcs12 *vmcs12,
10261 bool inequality, bit;
10263 bit = (vmcs12->exception_bitmap & (1u << PF_VECTOR)) != 0;
10265 (error_code & vmcs12->page_fault_error_code_mask) !=
10266 vmcs12->page_fault_error_code_match;
10267 return inequality ^ bit;
10270 static void vmx_inject_page_fault_nested(struct kvm_vcpu *vcpu,
10271 struct x86_exception *fault)
10273 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
10275 WARN_ON(!is_guest_mode(vcpu));
10277 if (nested_vmx_is_page_fault_vmexit(vmcs12, fault->error_code) &&
10278 !to_vmx(vcpu)->nested.nested_run_pending) {
10279 vmcs12->vm_exit_intr_error_code = fault->error_code;
10280 nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI,
10281 PF_VECTOR | INTR_TYPE_HARD_EXCEPTION |
10282 INTR_INFO_DELIVER_CODE_MASK | INTR_INFO_VALID_MASK,
10285 kvm_inject_page_fault(vcpu, fault);
10289 static inline bool nested_vmx_merge_msr_bitmap(struct kvm_vcpu *vcpu,
10290 struct vmcs12 *vmcs12);
10292 static void nested_get_vmcs12_pages(struct kvm_vcpu *vcpu,
10293 struct vmcs12 *vmcs12)
10295 struct vcpu_vmx *vmx = to_vmx(vcpu);
10299 if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) {
10301 * Translate L1 physical address to host physical
10302 * address for vmcs02. Keep the page pinned, so this
10303 * physical address remains valid. We keep a reference
10304 * to it so we can release it later.
10306 if (vmx->nested.apic_access_page) { /* shouldn't happen */
10307 kvm_release_page_dirty(vmx->nested.apic_access_page);
10308 vmx->nested.apic_access_page = NULL;
10310 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->apic_access_addr);
10312 * If translation failed, no matter: This feature asks
10313 * to exit when accessing the given address, and if it
10314 * can never be accessed, this feature won't do
10317 if (!is_error_page(page)) {
10318 vmx->nested.apic_access_page = page;
10319 hpa = page_to_phys(vmx->nested.apic_access_page);
10320 vmcs_write64(APIC_ACCESS_ADDR, hpa);
10322 vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL,
10323 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES);
10325 } else if (!(nested_cpu_has_virt_x2apic_mode(vmcs12)) &&
10326 cpu_need_virtualize_apic_accesses(&vmx->vcpu)) {
10327 vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL,
10328 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES);
10329 kvm_vcpu_reload_apic_access_page(vcpu);
10332 if (nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW)) {
10333 if (vmx->nested.virtual_apic_page) { /* shouldn't happen */
10334 kvm_release_page_dirty(vmx->nested.virtual_apic_page);
10335 vmx->nested.virtual_apic_page = NULL;
10337 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->virtual_apic_page_addr);
10340 * If translation failed, VM entry will fail because
10341 * prepare_vmcs02 set VIRTUAL_APIC_PAGE_ADDR to -1ull.
10342 * Failing the vm entry is _not_ what the processor
10343 * does but it's basically the only possibility we
10344 * have. We could still enter the guest if CR8 load
10345 * exits are enabled, CR8 store exits are enabled, and
10346 * virtualize APIC access is disabled; in this case
10347 * the processor would never use the TPR shadow and we
10348 * could simply clear the bit from the execution
10349 * control. But such a configuration is useless, so
10350 * let's keep the code simple.
10352 if (!is_error_page(page)) {
10353 vmx->nested.virtual_apic_page = page;
10354 hpa = page_to_phys(vmx->nested.virtual_apic_page);
10355 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, hpa);
10359 if (nested_cpu_has_posted_intr(vmcs12)) {
10360 if (vmx->nested.pi_desc_page) { /* shouldn't happen */
10361 kunmap(vmx->nested.pi_desc_page);
10362 kvm_release_page_dirty(vmx->nested.pi_desc_page);
10363 vmx->nested.pi_desc_page = NULL;
10365 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->posted_intr_desc_addr);
10366 if (is_error_page(page))
10368 vmx->nested.pi_desc_page = page;
10369 vmx->nested.pi_desc = kmap(vmx->nested.pi_desc_page);
10370 vmx->nested.pi_desc =
10371 (struct pi_desc *)((void *)vmx->nested.pi_desc +
10372 (unsigned long)(vmcs12->posted_intr_desc_addr &
10374 vmcs_write64(POSTED_INTR_DESC_ADDR,
10375 page_to_phys(vmx->nested.pi_desc_page) +
10376 (unsigned long)(vmcs12->posted_intr_desc_addr &
10379 if (cpu_has_vmx_msr_bitmap() &&
10380 nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS) &&
10381 nested_vmx_merge_msr_bitmap(vcpu, vmcs12))
10382 vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL,
10383 CPU_BASED_USE_MSR_BITMAPS);
10385 vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL,
10386 CPU_BASED_USE_MSR_BITMAPS);
10389 static void vmx_start_preemption_timer(struct kvm_vcpu *vcpu)
10391 u64 preemption_timeout = get_vmcs12(vcpu)->vmx_preemption_timer_value;
10392 struct vcpu_vmx *vmx = to_vmx(vcpu);
10394 if (vcpu->arch.virtual_tsc_khz == 0)
10397 /* Make sure short timeouts reliably trigger an immediate vmexit.
10398 * hrtimer_start does not guarantee this. */
10399 if (preemption_timeout <= 1) {
10400 vmx_preemption_timer_fn(&vmx->nested.preemption_timer);
10404 preemption_timeout <<= VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE;
10405 preemption_timeout *= 1000000;
10406 do_div(preemption_timeout, vcpu->arch.virtual_tsc_khz);
10407 hrtimer_start(&vmx->nested.preemption_timer,
10408 ns_to_ktime(preemption_timeout), HRTIMER_MODE_REL);
10411 static int nested_vmx_check_io_bitmap_controls(struct kvm_vcpu *vcpu,
10412 struct vmcs12 *vmcs12)
10414 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_IO_BITMAPS))
10417 if (!page_address_valid(vcpu, vmcs12->io_bitmap_a) ||
10418 !page_address_valid(vcpu, vmcs12->io_bitmap_b))
10424 static int nested_vmx_check_msr_bitmap_controls(struct kvm_vcpu *vcpu,
10425 struct vmcs12 *vmcs12)
10427 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS))
10430 if (!page_address_valid(vcpu, vmcs12->msr_bitmap))
10436 static int nested_vmx_check_tpr_shadow_controls(struct kvm_vcpu *vcpu,
10437 struct vmcs12 *vmcs12)
10439 if (!nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW))
10442 if (!page_address_valid(vcpu, vmcs12->virtual_apic_page_addr))
10449 * Merge L0's and L1's MSR bitmap, return false to indicate that
10450 * we do not use the hardware.
10452 static inline bool nested_vmx_merge_msr_bitmap(struct kvm_vcpu *vcpu,
10453 struct vmcs12 *vmcs12)
10457 unsigned long *msr_bitmap_l1;
10458 unsigned long *msr_bitmap_l0 = to_vmx(vcpu)->nested.vmcs02.msr_bitmap;
10460 * pred_cmd & spec_ctrl are trying to verify two things:
10462 * 1. L0 gave a permission to L1 to actually passthrough the MSR. This
10463 * ensures that we do not accidentally generate an L02 MSR bitmap
10464 * from the L12 MSR bitmap that is too permissive.
10465 * 2. That L1 or L2s have actually used the MSR. This avoids
10466 * unnecessarily merging of the bitmap if the MSR is unused. This
10467 * works properly because we only update the L01 MSR bitmap lazily.
10468 * So even if L0 should pass L1 these MSRs, the L01 bitmap is only
10469 * updated to reflect this when L1 (or its L2s) actually write to
10472 bool pred_cmd = !msr_write_intercepted_l01(vcpu, MSR_IA32_PRED_CMD);
10473 bool spec_ctrl = !msr_write_intercepted_l01(vcpu, MSR_IA32_SPEC_CTRL);
10475 if (!nested_cpu_has_virt_x2apic_mode(vmcs12) &&
10476 !pred_cmd && !spec_ctrl)
10479 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->msr_bitmap);
10480 if (is_error_page(page))
10482 msr_bitmap_l1 = (unsigned long *)kmap(page);
10484 memset(msr_bitmap_l0, 0xff, PAGE_SIZE);
10486 if (nested_cpu_has_virt_x2apic_mode(vmcs12)) {
10487 if (nested_cpu_has_apic_reg_virt(vmcs12))
10488 for (msr = 0x800; msr <= 0x8ff; msr++)
10489 nested_vmx_disable_intercept_for_msr(
10490 msr_bitmap_l1, msr_bitmap_l0,
10493 nested_vmx_disable_intercept_for_msr(
10494 msr_bitmap_l1, msr_bitmap_l0,
10495 APIC_BASE_MSR + (APIC_TASKPRI >> 4),
10496 MSR_TYPE_R | MSR_TYPE_W);
10498 if (nested_cpu_has_vid(vmcs12)) {
10499 nested_vmx_disable_intercept_for_msr(
10500 msr_bitmap_l1, msr_bitmap_l0,
10501 APIC_BASE_MSR + (APIC_EOI >> 4),
10503 nested_vmx_disable_intercept_for_msr(
10504 msr_bitmap_l1, msr_bitmap_l0,
10505 APIC_BASE_MSR + (APIC_SELF_IPI >> 4),
10511 nested_vmx_disable_intercept_for_msr(
10512 msr_bitmap_l1, msr_bitmap_l0,
10513 MSR_IA32_SPEC_CTRL,
10514 MSR_TYPE_R | MSR_TYPE_W);
10517 nested_vmx_disable_intercept_for_msr(
10518 msr_bitmap_l1, msr_bitmap_l0,
10523 kvm_release_page_clean(page);
10528 static int nested_vmx_check_apic_access_controls(struct kvm_vcpu *vcpu,
10529 struct vmcs12 *vmcs12)
10531 if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES) &&
10532 !page_address_valid(vcpu, vmcs12->apic_access_addr))
10538 static int nested_vmx_check_apicv_controls(struct kvm_vcpu *vcpu,
10539 struct vmcs12 *vmcs12)
10541 if (!nested_cpu_has_virt_x2apic_mode(vmcs12) &&
10542 !nested_cpu_has_apic_reg_virt(vmcs12) &&
10543 !nested_cpu_has_vid(vmcs12) &&
10544 !nested_cpu_has_posted_intr(vmcs12))
10548 * If virtualize x2apic mode is enabled,
10549 * virtualize apic access must be disabled.
10551 if (nested_cpu_has_virt_x2apic_mode(vmcs12) &&
10552 nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES))
10556 * If virtual interrupt delivery is enabled,
10557 * we must exit on external interrupts.
10559 if (nested_cpu_has_vid(vmcs12) &&
10560 !nested_exit_on_intr(vcpu))
10564 * bits 15:8 should be zero in posted_intr_nv,
10565 * the descriptor address has been already checked
10566 * in nested_get_vmcs12_pages.
10568 if (nested_cpu_has_posted_intr(vmcs12) &&
10569 (!nested_cpu_has_vid(vmcs12) ||
10570 !nested_exit_intr_ack_set(vcpu) ||
10571 vmcs12->posted_intr_nv & 0xff00))
10574 /* tpr shadow is needed by all apicv features. */
10575 if (!nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW))
10581 static int nested_vmx_check_msr_switch(struct kvm_vcpu *vcpu,
10582 unsigned long count_field,
10583 unsigned long addr_field)
10588 if (vmcs12_read_any(vcpu, count_field, &count) ||
10589 vmcs12_read_any(vcpu, addr_field, &addr)) {
10595 maxphyaddr = cpuid_maxphyaddr(vcpu);
10596 if (!IS_ALIGNED(addr, 16) || addr >> maxphyaddr ||
10597 (addr + count * sizeof(struct vmx_msr_entry) - 1) >> maxphyaddr) {
10598 pr_debug_ratelimited(
10599 "nVMX: invalid MSR switch (0x%lx, %d, %llu, 0x%08llx)",
10600 addr_field, maxphyaddr, count, addr);
10606 static int nested_vmx_check_msr_switch_controls(struct kvm_vcpu *vcpu,
10607 struct vmcs12 *vmcs12)
10609 if (vmcs12->vm_exit_msr_load_count == 0 &&
10610 vmcs12->vm_exit_msr_store_count == 0 &&
10611 vmcs12->vm_entry_msr_load_count == 0)
10612 return 0; /* Fast path */
10613 if (nested_vmx_check_msr_switch(vcpu, VM_EXIT_MSR_LOAD_COUNT,
10614 VM_EXIT_MSR_LOAD_ADDR) ||
10615 nested_vmx_check_msr_switch(vcpu, VM_EXIT_MSR_STORE_COUNT,
10616 VM_EXIT_MSR_STORE_ADDR) ||
10617 nested_vmx_check_msr_switch(vcpu, VM_ENTRY_MSR_LOAD_COUNT,
10618 VM_ENTRY_MSR_LOAD_ADDR))
10623 static int nested_vmx_check_pml_controls(struct kvm_vcpu *vcpu,
10624 struct vmcs12 *vmcs12)
10626 u64 address = vmcs12->pml_address;
10627 int maxphyaddr = cpuid_maxphyaddr(vcpu);
10629 if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_PML)) {
10630 if (!nested_cpu_has_ept(vmcs12) ||
10631 !IS_ALIGNED(address, 4096) ||
10632 address >> maxphyaddr)
10639 static int nested_vmx_msr_check_common(struct kvm_vcpu *vcpu,
10640 struct vmx_msr_entry *e)
10642 /* x2APIC MSR accesses are not allowed */
10643 if (vcpu->arch.apic_base & X2APIC_ENABLE && e->index >> 8 == 0x8)
10645 if (e->index == MSR_IA32_UCODE_WRITE || /* SDM Table 35-2 */
10646 e->index == MSR_IA32_UCODE_REV)
10648 if (e->reserved != 0)
10653 static int nested_vmx_load_msr_check(struct kvm_vcpu *vcpu,
10654 struct vmx_msr_entry *e)
10656 if (e->index == MSR_FS_BASE ||
10657 e->index == MSR_GS_BASE ||
10658 e->index == MSR_IA32_SMM_MONITOR_CTL || /* SMM is not supported */
10659 nested_vmx_msr_check_common(vcpu, e))
10664 static int nested_vmx_store_msr_check(struct kvm_vcpu *vcpu,
10665 struct vmx_msr_entry *e)
10667 if (e->index == MSR_IA32_SMBASE || /* SMM is not supported */
10668 nested_vmx_msr_check_common(vcpu, e))
10674 * Load guest's/host's msr at nested entry/exit.
10675 * return 0 for success, entry index for failure.
10677 static u32 nested_vmx_load_msr(struct kvm_vcpu *vcpu, u64 gpa, u32 count)
10680 struct vmx_msr_entry e;
10681 struct msr_data msr;
10683 msr.host_initiated = false;
10684 for (i = 0; i < count; i++) {
10685 if (kvm_vcpu_read_guest(vcpu, gpa + i * sizeof(e),
10687 pr_debug_ratelimited(
10688 "%s cannot read MSR entry (%u, 0x%08llx)\n",
10689 __func__, i, gpa + i * sizeof(e));
10692 if (nested_vmx_load_msr_check(vcpu, &e)) {
10693 pr_debug_ratelimited(
10694 "%s check failed (%u, 0x%x, 0x%x)\n",
10695 __func__, i, e.index, e.reserved);
10698 msr.index = e.index;
10699 msr.data = e.value;
10700 if (kvm_set_msr(vcpu, &msr)) {
10701 pr_debug_ratelimited(
10702 "%s cannot write MSR (%u, 0x%x, 0x%llx)\n",
10703 __func__, i, e.index, e.value);
10712 static int nested_vmx_store_msr(struct kvm_vcpu *vcpu, u64 gpa, u32 count)
10715 struct vmx_msr_entry e;
10717 for (i = 0; i < count; i++) {
10718 struct msr_data msr_info;
10719 if (kvm_vcpu_read_guest(vcpu,
10720 gpa + i * sizeof(e),
10721 &e, 2 * sizeof(u32))) {
10722 pr_debug_ratelimited(
10723 "%s cannot read MSR entry (%u, 0x%08llx)\n",
10724 __func__, i, gpa + i * sizeof(e));
10727 if (nested_vmx_store_msr_check(vcpu, &e)) {
10728 pr_debug_ratelimited(
10729 "%s check failed (%u, 0x%x, 0x%x)\n",
10730 __func__, i, e.index, e.reserved);
10733 msr_info.host_initiated = false;
10734 msr_info.index = e.index;
10735 if (kvm_get_msr(vcpu, &msr_info)) {
10736 pr_debug_ratelimited(
10737 "%s cannot read MSR (%u, 0x%x)\n",
10738 __func__, i, e.index);
10741 if (kvm_vcpu_write_guest(vcpu,
10742 gpa + i * sizeof(e) +
10743 offsetof(struct vmx_msr_entry, value),
10744 &msr_info.data, sizeof(msr_info.data))) {
10745 pr_debug_ratelimited(
10746 "%s cannot write MSR (%u, 0x%x, 0x%llx)\n",
10747 __func__, i, e.index, msr_info.data);
10754 static bool nested_cr3_valid(struct kvm_vcpu *vcpu, unsigned long val)
10756 unsigned long invalid_mask;
10758 invalid_mask = (~0ULL) << cpuid_maxphyaddr(vcpu);
10759 return (val & invalid_mask) == 0;
10763 * Load guest's/host's cr3 at nested entry/exit. nested_ept is true if we are
10764 * emulating VM entry into a guest with EPT enabled.
10765 * Returns 0 on success, 1 on failure. Invalid state exit qualification code
10766 * is assigned to entry_failure_code on failure.
10768 static int nested_vmx_load_cr3(struct kvm_vcpu *vcpu, unsigned long cr3, bool nested_ept,
10769 u32 *entry_failure_code)
10771 if (cr3 != kvm_read_cr3(vcpu) || (!nested_ept && pdptrs_changed(vcpu))) {
10772 if (!nested_cr3_valid(vcpu, cr3)) {
10773 *entry_failure_code = ENTRY_FAIL_DEFAULT;
10778 * If PAE paging and EPT are both on, CR3 is not used by the CPU and
10779 * must not be dereferenced.
10781 if (!is_long_mode(vcpu) && is_pae(vcpu) && is_paging(vcpu) &&
10783 if (!load_pdptrs(vcpu, vcpu->arch.walk_mmu, cr3)) {
10784 *entry_failure_code = ENTRY_FAIL_PDPTE;
10789 vcpu->arch.cr3 = cr3;
10790 __set_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail);
10793 kvm_mmu_reset_context(vcpu);
10798 * prepare_vmcs02 is called when the L1 guest hypervisor runs its nested
10799 * L2 guest. L1 has a vmcs for L2 (vmcs12), and this function "merges" it
10800 * with L0's requirements for its guest (a.k.a. vmcs01), so we can run the L2
10801 * guest in a way that will both be appropriate to L1's requests, and our
10802 * needs. In addition to modifying the active vmcs (which is vmcs02), this
10803 * function also has additional necessary side-effects, like setting various
10804 * vcpu->arch fields.
10805 * Returns 0 on success, 1 on failure. Invalid state exit qualification code
10806 * is assigned to entry_failure_code on failure.
10808 static int prepare_vmcs02(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12,
10809 bool from_vmentry, u32 *entry_failure_code)
10811 struct vcpu_vmx *vmx = to_vmx(vcpu);
10812 u32 exec_control, vmcs12_exec_ctrl;
10814 vmcs_write16(GUEST_ES_SELECTOR, vmcs12->guest_es_selector);
10815 vmcs_write16(GUEST_CS_SELECTOR, vmcs12->guest_cs_selector);
10816 vmcs_write16(GUEST_SS_SELECTOR, vmcs12->guest_ss_selector);
10817 vmcs_write16(GUEST_DS_SELECTOR, vmcs12->guest_ds_selector);
10818 vmcs_write16(GUEST_FS_SELECTOR, vmcs12->guest_fs_selector);
10819 vmcs_write16(GUEST_GS_SELECTOR, vmcs12->guest_gs_selector);
10820 vmcs_write16(GUEST_LDTR_SELECTOR, vmcs12->guest_ldtr_selector);
10821 vmcs_write16(GUEST_TR_SELECTOR, vmcs12->guest_tr_selector);
10822 vmcs_write32(GUEST_ES_LIMIT, vmcs12->guest_es_limit);
10823 vmcs_write32(GUEST_CS_LIMIT, vmcs12->guest_cs_limit);
10824 vmcs_write32(GUEST_SS_LIMIT, vmcs12->guest_ss_limit);
10825 vmcs_write32(GUEST_DS_LIMIT, vmcs12->guest_ds_limit);
10826 vmcs_write32(GUEST_FS_LIMIT, vmcs12->guest_fs_limit);
10827 vmcs_write32(GUEST_GS_LIMIT, vmcs12->guest_gs_limit);
10828 vmcs_write32(GUEST_LDTR_LIMIT, vmcs12->guest_ldtr_limit);
10829 vmcs_write32(GUEST_TR_LIMIT, vmcs12->guest_tr_limit);
10830 vmcs_write32(GUEST_GDTR_LIMIT, vmcs12->guest_gdtr_limit);
10831 vmcs_write32(GUEST_IDTR_LIMIT, vmcs12->guest_idtr_limit);
10832 vmcs_write32(GUEST_ES_AR_BYTES, vmcs12->guest_es_ar_bytes);
10833 vmcs_write32(GUEST_CS_AR_BYTES, vmcs12->guest_cs_ar_bytes);
10834 vmcs_write32(GUEST_SS_AR_BYTES, vmcs12->guest_ss_ar_bytes);
10835 vmcs_write32(GUEST_DS_AR_BYTES, vmcs12->guest_ds_ar_bytes);
10836 vmcs_write32(GUEST_FS_AR_BYTES, vmcs12->guest_fs_ar_bytes);
10837 vmcs_write32(GUEST_GS_AR_BYTES, vmcs12->guest_gs_ar_bytes);
10838 vmcs_write32(GUEST_LDTR_AR_BYTES, vmcs12->guest_ldtr_ar_bytes);
10839 vmcs_write32(GUEST_TR_AR_BYTES, vmcs12->guest_tr_ar_bytes);
10840 vmcs_writel(GUEST_ES_BASE, vmcs12->guest_es_base);
10841 vmcs_writel(GUEST_CS_BASE, vmcs12->guest_cs_base);
10842 vmcs_writel(GUEST_SS_BASE, vmcs12->guest_ss_base);
10843 vmcs_writel(GUEST_DS_BASE, vmcs12->guest_ds_base);
10844 vmcs_writel(GUEST_FS_BASE, vmcs12->guest_fs_base);
10845 vmcs_writel(GUEST_GS_BASE, vmcs12->guest_gs_base);
10846 vmcs_writel(GUEST_LDTR_BASE, vmcs12->guest_ldtr_base);
10847 vmcs_writel(GUEST_TR_BASE, vmcs12->guest_tr_base);
10848 vmcs_writel(GUEST_GDTR_BASE, vmcs12->guest_gdtr_base);
10849 vmcs_writel(GUEST_IDTR_BASE, vmcs12->guest_idtr_base);
10851 if (from_vmentry &&
10852 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS)) {
10853 kvm_set_dr(vcpu, 7, vmcs12->guest_dr7);
10854 vmcs_write64(GUEST_IA32_DEBUGCTL, vmcs12->guest_ia32_debugctl);
10856 kvm_set_dr(vcpu, 7, vcpu->arch.dr7);
10857 vmcs_write64(GUEST_IA32_DEBUGCTL, vmx->nested.vmcs01_debugctl);
10859 if (from_vmentry) {
10860 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
10861 vmcs12->vm_entry_intr_info_field);
10862 vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE,
10863 vmcs12->vm_entry_exception_error_code);
10864 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
10865 vmcs12->vm_entry_instruction_len);
10866 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO,
10867 vmcs12->guest_interruptibility_info);
10868 vmx->loaded_vmcs->nmi_known_unmasked =
10869 !(vmcs12->guest_interruptibility_info & GUEST_INTR_STATE_NMI);
10871 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0);
10873 vmcs_write32(GUEST_SYSENTER_CS, vmcs12->guest_sysenter_cs);
10874 vmx_set_rflags(vcpu, vmcs12->guest_rflags);
10875 vmcs_writel(GUEST_PENDING_DBG_EXCEPTIONS,
10876 vmcs12->guest_pending_dbg_exceptions);
10877 vmcs_writel(GUEST_SYSENTER_ESP, vmcs12->guest_sysenter_esp);
10878 vmcs_writel(GUEST_SYSENTER_EIP, vmcs12->guest_sysenter_eip);
10880 if (nested_cpu_has_xsaves(vmcs12))
10881 vmcs_write64(XSS_EXIT_BITMAP, vmcs12->xss_exit_bitmap);
10882 vmcs_write64(VMCS_LINK_POINTER, -1ull);
10884 exec_control = vmcs12->pin_based_vm_exec_control;
10886 /* Preemption timer setting is only taken from vmcs01. */
10887 exec_control &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
10888 exec_control |= vmcs_config.pin_based_exec_ctrl;
10889 if (vmx->hv_deadline_tsc == -1)
10890 exec_control &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
10892 /* Posted interrupts setting is only taken from vmcs12. */
10893 if (nested_cpu_has_posted_intr(vmcs12)) {
10894 vmx->nested.posted_intr_nv = vmcs12->posted_intr_nv;
10895 vmx->nested.pi_pending = false;
10896 vmcs_write16(POSTED_INTR_NV, POSTED_INTR_NESTED_VECTOR);
10898 exec_control &= ~PIN_BASED_POSTED_INTR;
10901 vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, exec_control);
10903 vmx->nested.preemption_timer_expired = false;
10904 if (nested_cpu_has_preemption_timer(vmcs12))
10905 vmx_start_preemption_timer(vcpu);
10908 * Whether page-faults are trapped is determined by a combination of
10909 * 3 settings: PFEC_MASK, PFEC_MATCH and EXCEPTION_BITMAP.PF.
10910 * If enable_ept, L0 doesn't care about page faults and we should
10911 * set all of these to L1's desires. However, if !enable_ept, L0 does
10912 * care about (at least some) page faults, and because it is not easy
10913 * (if at all possible?) to merge L0 and L1's desires, we simply ask
10914 * to exit on each and every L2 page fault. This is done by setting
10915 * MASK=MATCH=0 and (see below) EB.PF=1.
10916 * Note that below we don't need special code to set EB.PF beyond the
10917 * "or"ing of the EB of vmcs01 and vmcs12, because when enable_ept,
10918 * vmcs01's EB.PF is 0 so the "or" will take vmcs12's value, and when
10919 * !enable_ept, EB.PF is 1, so the "or" will always be 1.
10921 vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK,
10922 enable_ept ? vmcs12->page_fault_error_code_mask : 0);
10923 vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH,
10924 enable_ept ? vmcs12->page_fault_error_code_match : 0);
10926 if (cpu_has_secondary_exec_ctrls()) {
10927 exec_control = vmx->secondary_exec_control;
10929 /* Take the following fields only from vmcs12 */
10930 exec_control &= ~(SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
10931 SECONDARY_EXEC_ENABLE_INVPCID |
10932 SECONDARY_EXEC_RDTSCP |
10933 SECONDARY_EXEC_XSAVES |
10934 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY |
10935 SECONDARY_EXEC_APIC_REGISTER_VIRT |
10936 SECONDARY_EXEC_ENABLE_VMFUNC);
10937 if (nested_cpu_has(vmcs12,
10938 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS)) {
10939 vmcs12_exec_ctrl = vmcs12->secondary_vm_exec_control &
10940 ~SECONDARY_EXEC_ENABLE_PML;
10941 exec_control |= vmcs12_exec_ctrl;
10944 /* All VMFUNCs are currently emulated through L0 vmexits. */
10945 if (exec_control & SECONDARY_EXEC_ENABLE_VMFUNC)
10946 vmcs_write64(VM_FUNCTION_CONTROL, 0);
10948 if (exec_control & SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY) {
10949 vmcs_write64(EOI_EXIT_BITMAP0,
10950 vmcs12->eoi_exit_bitmap0);
10951 vmcs_write64(EOI_EXIT_BITMAP1,
10952 vmcs12->eoi_exit_bitmap1);
10953 vmcs_write64(EOI_EXIT_BITMAP2,
10954 vmcs12->eoi_exit_bitmap2);
10955 vmcs_write64(EOI_EXIT_BITMAP3,
10956 vmcs12->eoi_exit_bitmap3);
10957 vmcs_write16(GUEST_INTR_STATUS,
10958 vmcs12->guest_intr_status);
10962 * Write an illegal value to APIC_ACCESS_ADDR. Later,
10963 * nested_get_vmcs12_pages will either fix it up or
10964 * remove the VM execution control.
10966 if (exec_control & SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)
10967 vmcs_write64(APIC_ACCESS_ADDR, -1ull);
10969 vmcs_write32(SECONDARY_VM_EXEC_CONTROL, exec_control);
10974 * Set host-state according to L0's settings (vmcs12 is irrelevant here)
10975 * Some constant fields are set here by vmx_set_constant_host_state().
10976 * Other fields are different per CPU, and will be set later when
10977 * vmx_vcpu_load() is called, and when vmx_save_host_state() is called.
10979 vmx_set_constant_host_state(vmx);
10982 * Set the MSR load/store lists to match L0's settings.
10984 vmcs_write32(VM_EXIT_MSR_STORE_COUNT, 0);
10985 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, vmx->msr_autoload.host.nr);
10986 vmcs_write64(VM_EXIT_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.host.val));
10987 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, vmx->msr_autoload.guest.nr);
10988 vmcs_write64(VM_ENTRY_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.guest.val));
10991 * HOST_RSP is normally set correctly in vmx_vcpu_run() just before
10992 * entry, but only if the current (host) sp changed from the value
10993 * we wrote last (vmx->host_rsp). This cache is no longer relevant
10994 * if we switch vmcs, and rather than hold a separate cache per vmcs,
10995 * here we just force the write to happen on entry.
10999 exec_control = vmx_exec_control(vmx); /* L0's desires */
11000 exec_control &= ~CPU_BASED_VIRTUAL_INTR_PENDING;
11001 exec_control &= ~CPU_BASED_VIRTUAL_NMI_PENDING;
11002 exec_control &= ~CPU_BASED_TPR_SHADOW;
11003 exec_control |= vmcs12->cpu_based_vm_exec_control;
11006 * Write an illegal value to VIRTUAL_APIC_PAGE_ADDR. Later, if
11007 * nested_get_vmcs12_pages can't fix it up, the illegal value
11008 * will result in a VM entry failure.
11010 if (exec_control & CPU_BASED_TPR_SHADOW) {
11011 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, -1ull);
11012 vmcs_write32(TPR_THRESHOLD, vmcs12->tpr_threshold);
11014 #ifdef CONFIG_X86_64
11015 exec_control |= CPU_BASED_CR8_LOAD_EXITING |
11016 CPU_BASED_CR8_STORE_EXITING;
11021 * Merging of IO bitmap not currently supported.
11022 * Rather, exit every time.
11024 exec_control &= ~CPU_BASED_USE_IO_BITMAPS;
11025 exec_control |= CPU_BASED_UNCOND_IO_EXITING;
11027 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, exec_control);
11029 /* EXCEPTION_BITMAP and CR0_GUEST_HOST_MASK should basically be the
11030 * bitwise-or of what L1 wants to trap for L2, and what we want to
11031 * trap. Note that CR0.TS also needs updating - we do this later.
11033 update_exception_bitmap(vcpu);
11034 vcpu->arch.cr0_guest_owned_bits &= ~vmcs12->cr0_guest_host_mask;
11035 vmcs_writel(CR0_GUEST_HOST_MASK, ~vcpu->arch.cr0_guest_owned_bits);
11037 /* L2->L1 exit controls are emulated - the hardware exit is to L0 so
11038 * we should use its exit controls. Note that VM_EXIT_LOAD_IA32_EFER
11039 * bits are further modified by vmx_set_efer() below.
11041 vmcs_write32(VM_EXIT_CONTROLS, vmcs_config.vmexit_ctrl);
11043 /* vmcs12's VM_ENTRY_LOAD_IA32_EFER and VM_ENTRY_IA32E_MODE are
11044 * emulated by vmx_set_efer(), below.
11046 vm_entry_controls_init(vmx,
11047 (vmcs12->vm_entry_controls & ~VM_ENTRY_LOAD_IA32_EFER &
11048 ~VM_ENTRY_IA32E_MODE) |
11049 (vmcs_config.vmentry_ctrl & ~VM_ENTRY_IA32E_MODE));
11051 if (from_vmentry &&
11052 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_PAT)) {
11053 vmcs_write64(GUEST_IA32_PAT, vmcs12->guest_ia32_pat);
11054 vcpu->arch.pat = vmcs12->guest_ia32_pat;
11055 } else if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) {
11056 vmcs_write64(GUEST_IA32_PAT, vmx->vcpu.arch.pat);
11059 set_cr4_guest_host_mask(vmx);
11061 if (from_vmentry &&
11062 vmcs12->vm_entry_controls & VM_ENTRY_LOAD_BNDCFGS)
11063 vmcs_write64(GUEST_BNDCFGS, vmcs12->guest_bndcfgs);
11065 if (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING)
11066 vmcs_write64(TSC_OFFSET,
11067 vcpu->arch.tsc_offset + vmcs12->tsc_offset);
11069 vmcs_write64(TSC_OFFSET, vcpu->arch.tsc_offset);
11070 if (kvm_has_tsc_control)
11071 decache_tsc_multiplier(vmx);
11073 if (cpu_has_vmx_msr_bitmap())
11074 vmcs_write64(MSR_BITMAP, __pa(vmx->nested.vmcs02.msr_bitmap));
11078 * There is no direct mapping between vpid02 and vpid12, the
11079 * vpid02 is per-vCPU for L0 and reused while the value of
11080 * vpid12 is changed w/ one invvpid during nested vmentry.
11081 * The vpid12 is allocated by L1 for L2, so it will not
11082 * influence global bitmap(for vpid01 and vpid02 allocation)
11083 * even if spawn a lot of nested vCPUs.
11085 if (nested_cpu_has_vpid(vmcs12) && vmx->nested.vpid02) {
11086 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->nested.vpid02);
11087 if (vmcs12->virtual_processor_id != vmx->nested.last_vpid) {
11088 vmx->nested.last_vpid = vmcs12->virtual_processor_id;
11089 __vmx_flush_tlb(vcpu, to_vmx(vcpu)->nested.vpid02);
11092 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid);
11093 vmx_flush_tlb(vcpu);
11100 * Conceptually we want to copy the PML address and index from
11101 * vmcs01 here, and then back to vmcs01 on nested vmexit. But,
11102 * since we always flush the log on each vmexit, this happens
11103 * to be equivalent to simply resetting the fields in vmcs02.
11105 ASSERT(vmx->pml_pg);
11106 vmcs_write64(PML_ADDRESS, page_to_phys(vmx->pml_pg));
11107 vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1);
11110 if (nested_cpu_has_ept(vmcs12)) {
11111 if (nested_ept_init_mmu_context(vcpu)) {
11112 *entry_failure_code = ENTRY_FAIL_DEFAULT;
11115 } else if (nested_cpu_has2(vmcs12,
11116 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) {
11117 vmx_flush_tlb_ept_only(vcpu);
11121 * This sets GUEST_CR0 to vmcs12->guest_cr0, possibly modifying those
11122 * bits which we consider mandatory enabled.
11123 * The CR0_READ_SHADOW is what L2 should have expected to read given
11124 * the specifications by L1; It's not enough to take
11125 * vmcs12->cr0_read_shadow because on our cr0_guest_host_mask we we
11126 * have more bits than L1 expected.
11128 vmx_set_cr0(vcpu, vmcs12->guest_cr0);
11129 vmcs_writel(CR0_READ_SHADOW, nested_read_cr0(vmcs12));
11131 vmx_set_cr4(vcpu, vmcs12->guest_cr4);
11132 vmcs_writel(CR4_READ_SHADOW, nested_read_cr4(vmcs12));
11134 if (from_vmentry &&
11135 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_EFER))
11136 vcpu->arch.efer = vmcs12->guest_ia32_efer;
11137 else if (vmcs12->vm_entry_controls & VM_ENTRY_IA32E_MODE)
11138 vcpu->arch.efer |= (EFER_LMA | EFER_LME);
11140 vcpu->arch.efer &= ~(EFER_LMA | EFER_LME);
11141 /* Note: modifies VM_ENTRY/EXIT_CONTROLS and GUEST/HOST_IA32_EFER */
11142 vmx_set_efer(vcpu, vcpu->arch.efer);
11144 /* Shadow page tables on either EPT or shadow page tables. */
11145 if (nested_vmx_load_cr3(vcpu, vmcs12->guest_cr3, nested_cpu_has_ept(vmcs12),
11146 entry_failure_code))
11150 vcpu->arch.walk_mmu->inject_page_fault = vmx_inject_page_fault_nested;
11153 * L1 may access the L2's PDPTR, so save them to construct vmcs12
11156 vmcs_write64(GUEST_PDPTR0, vmcs12->guest_pdptr0);
11157 vmcs_write64(GUEST_PDPTR1, vmcs12->guest_pdptr1);
11158 vmcs_write64(GUEST_PDPTR2, vmcs12->guest_pdptr2);
11159 vmcs_write64(GUEST_PDPTR3, vmcs12->guest_pdptr3);
11162 kvm_register_write(vcpu, VCPU_REGS_RSP, vmcs12->guest_rsp);
11163 kvm_register_write(vcpu, VCPU_REGS_RIP, vmcs12->guest_rip);
11167 static int check_vmentry_prereqs(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
11169 struct vcpu_vmx *vmx = to_vmx(vcpu);
11171 if (vmcs12->guest_activity_state != GUEST_ACTIVITY_ACTIVE &&
11172 vmcs12->guest_activity_state != GUEST_ACTIVITY_HLT)
11173 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11175 if (nested_vmx_check_io_bitmap_controls(vcpu, vmcs12))
11176 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11178 if (nested_vmx_check_msr_bitmap_controls(vcpu, vmcs12))
11179 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11181 if (nested_vmx_check_apic_access_controls(vcpu, vmcs12))
11182 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11184 if (nested_vmx_check_tpr_shadow_controls(vcpu, vmcs12))
11185 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11187 if (nested_vmx_check_apicv_controls(vcpu, vmcs12))
11188 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11190 if (nested_vmx_check_msr_switch_controls(vcpu, vmcs12))
11191 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11193 if (nested_vmx_check_pml_controls(vcpu, vmcs12))
11194 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11196 if (!vmx_control_verify(vmcs12->cpu_based_vm_exec_control,
11197 vmx->nested.nested_vmx_procbased_ctls_low,
11198 vmx->nested.nested_vmx_procbased_ctls_high) ||
11199 (nested_cpu_has(vmcs12, CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) &&
11200 !vmx_control_verify(vmcs12->secondary_vm_exec_control,
11201 vmx->nested.nested_vmx_secondary_ctls_low,
11202 vmx->nested.nested_vmx_secondary_ctls_high)) ||
11203 !vmx_control_verify(vmcs12->pin_based_vm_exec_control,
11204 vmx->nested.nested_vmx_pinbased_ctls_low,
11205 vmx->nested.nested_vmx_pinbased_ctls_high) ||
11206 !vmx_control_verify(vmcs12->vm_exit_controls,
11207 vmx->nested.nested_vmx_exit_ctls_low,
11208 vmx->nested.nested_vmx_exit_ctls_high) ||
11209 !vmx_control_verify(vmcs12->vm_entry_controls,
11210 vmx->nested.nested_vmx_entry_ctls_low,
11211 vmx->nested.nested_vmx_entry_ctls_high))
11212 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11214 if (nested_cpu_has_vmfunc(vmcs12)) {
11215 if (vmcs12->vm_function_control &
11216 ~vmx->nested.nested_vmx_vmfunc_controls)
11217 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11219 if (nested_cpu_has_eptp_switching(vmcs12)) {
11220 if (!nested_cpu_has_ept(vmcs12) ||
11221 !page_address_valid(vcpu, vmcs12->eptp_list_address))
11222 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11226 if (vmcs12->cr3_target_count > nested_cpu_vmx_misc_cr3_count(vcpu))
11227 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11229 if (!nested_host_cr0_valid(vcpu, vmcs12->host_cr0) ||
11230 !nested_host_cr4_valid(vcpu, vmcs12->host_cr4) ||
11231 !nested_cr3_valid(vcpu, vmcs12->host_cr3))
11232 return VMXERR_ENTRY_INVALID_HOST_STATE_FIELD;
11237 static int check_vmentry_postreqs(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12,
11242 *exit_qual = ENTRY_FAIL_DEFAULT;
11244 if (!nested_guest_cr0_valid(vcpu, vmcs12->guest_cr0) ||
11245 !nested_guest_cr4_valid(vcpu, vmcs12->guest_cr4))
11248 if (!nested_cpu_has2(vmcs12, SECONDARY_EXEC_SHADOW_VMCS) &&
11249 vmcs12->vmcs_link_pointer != -1ull) {
11250 *exit_qual = ENTRY_FAIL_VMCS_LINK_PTR;
11255 * If the load IA32_EFER VM-entry control is 1, the following checks
11256 * are performed on the field for the IA32_EFER MSR:
11257 * - Bits reserved in the IA32_EFER MSR must be 0.
11258 * - Bit 10 (corresponding to IA32_EFER.LMA) must equal the value of
11259 * the IA-32e mode guest VM-exit control. It must also be identical
11260 * to bit 8 (LME) if bit 31 in the CR0 field (corresponding to
11263 if (to_vmx(vcpu)->nested.nested_run_pending &&
11264 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_EFER)) {
11265 ia32e = (vmcs12->vm_entry_controls & VM_ENTRY_IA32E_MODE) != 0;
11266 if (!kvm_valid_efer(vcpu, vmcs12->guest_ia32_efer) ||
11267 ia32e != !!(vmcs12->guest_ia32_efer & EFER_LMA) ||
11268 ((vmcs12->guest_cr0 & X86_CR0_PG) &&
11269 ia32e != !!(vmcs12->guest_ia32_efer & EFER_LME)))
11274 * If the load IA32_EFER VM-exit control is 1, bits reserved in the
11275 * IA32_EFER MSR must be 0 in the field for that register. In addition,
11276 * the values of the LMA and LME bits in the field must each be that of
11277 * the host address-space size VM-exit control.
11279 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_EFER) {
11280 ia32e = (vmcs12->vm_exit_controls &
11281 VM_EXIT_HOST_ADDR_SPACE_SIZE) != 0;
11282 if (!kvm_valid_efer(vcpu, vmcs12->host_ia32_efer) ||
11283 ia32e != !!(vmcs12->host_ia32_efer & EFER_LMA) ||
11284 ia32e != !!(vmcs12->host_ia32_efer & EFER_LME))
11291 static int enter_vmx_non_root_mode(struct kvm_vcpu *vcpu, bool from_vmentry)
11293 struct vcpu_vmx *vmx = to_vmx(vcpu);
11294 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
11298 enter_guest_mode(vcpu);
11300 if (!(vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS))
11301 vmx->nested.vmcs01_debugctl = vmcs_read64(GUEST_IA32_DEBUGCTL);
11303 vmx_switch_vmcs(vcpu, &vmx->nested.vmcs02);
11304 vmx_segment_cache_clear(vmx);
11306 if (prepare_vmcs02(vcpu, vmcs12, from_vmentry, &exit_qual)) {
11307 leave_guest_mode(vcpu);
11308 vmx_switch_vmcs(vcpu, &vmx->vmcs01);
11309 nested_vmx_entry_failure(vcpu, vmcs12,
11310 EXIT_REASON_INVALID_STATE, exit_qual);
11314 nested_get_vmcs12_pages(vcpu, vmcs12);
11316 msr_entry_idx = nested_vmx_load_msr(vcpu,
11317 vmcs12->vm_entry_msr_load_addr,
11318 vmcs12->vm_entry_msr_load_count);
11319 if (msr_entry_idx) {
11320 leave_guest_mode(vcpu);
11321 vmx_switch_vmcs(vcpu, &vmx->vmcs01);
11322 nested_vmx_entry_failure(vcpu, vmcs12,
11323 EXIT_REASON_MSR_LOAD_FAIL, msr_entry_idx);
11328 * Note no nested_vmx_succeed or nested_vmx_fail here. At this point
11329 * we are no longer running L1, and VMLAUNCH/VMRESUME has not yet
11330 * returned as far as L1 is concerned. It will only return (and set
11331 * the success flag) when L2 exits (see nested_vmx_vmexit()).
11337 * nested_vmx_run() handles a nested entry, i.e., a VMLAUNCH or VMRESUME on L1
11338 * for running an L2 nested guest.
11340 static int nested_vmx_run(struct kvm_vcpu *vcpu, bool launch)
11342 struct vmcs12 *vmcs12;
11343 struct vcpu_vmx *vmx = to_vmx(vcpu);
11344 u32 interrupt_shadow = vmx_get_interrupt_shadow(vcpu);
11348 if (!nested_vmx_check_permission(vcpu))
11351 if (!nested_vmx_check_vmcs12(vcpu))
11354 vmcs12 = get_vmcs12(vcpu);
11356 if (enable_shadow_vmcs)
11357 copy_shadow_to_vmcs12(vmx);
11360 * The nested entry process starts with enforcing various prerequisites
11361 * on vmcs12 as required by the Intel SDM, and act appropriately when
11362 * they fail: As the SDM explains, some conditions should cause the
11363 * instruction to fail, while others will cause the instruction to seem
11364 * to succeed, but return an EXIT_REASON_INVALID_STATE.
11365 * To speed up the normal (success) code path, we should avoid checking
11366 * for misconfigurations which will anyway be caught by the processor
11367 * when using the merged vmcs02.
11369 if (interrupt_shadow & KVM_X86_SHADOW_INT_MOV_SS) {
11370 nested_vmx_failValid(vcpu,
11371 VMXERR_ENTRY_EVENTS_BLOCKED_BY_MOV_SS);
11375 if (vmcs12->launch_state == launch) {
11376 nested_vmx_failValid(vcpu,
11377 launch ? VMXERR_VMLAUNCH_NONCLEAR_VMCS
11378 : VMXERR_VMRESUME_NONLAUNCHED_VMCS);
11382 ret = check_vmentry_prereqs(vcpu, vmcs12);
11384 nested_vmx_failValid(vcpu, ret);
11389 * After this point, the trap flag no longer triggers a singlestep trap
11390 * on the vm entry instructions; don't call kvm_skip_emulated_instruction.
11391 * This is not 100% correct; for performance reasons, we delegate most
11392 * of the checks on host state to the processor. If those fail,
11393 * the singlestep trap is missed.
11395 skip_emulated_instruction(vcpu);
11397 ret = check_vmentry_postreqs(vcpu, vmcs12, &exit_qual);
11399 nested_vmx_entry_failure(vcpu, vmcs12,
11400 EXIT_REASON_INVALID_STATE, exit_qual);
11405 * We're finally done with prerequisite checking, and can start with
11406 * the nested entry.
11409 ret = enter_vmx_non_root_mode(vcpu, true);
11413 /* Hide L1D cache contents from the nested guest. */
11414 vmx->vcpu.arch.l1tf_flush_l1d = true;
11417 * If we're entering a halted L2 vcpu and the L2 vcpu won't be woken
11418 * by event injection, halt vcpu.
11420 if ((vmcs12->guest_activity_state == GUEST_ACTIVITY_HLT) &&
11421 !(vmcs12->vm_entry_intr_info_field & INTR_INFO_VALID_MASK))
11422 return kvm_vcpu_halt(vcpu);
11424 vmx->nested.nested_run_pending = 1;
11429 return kvm_skip_emulated_instruction(vcpu);
11433 * On a nested exit from L2 to L1, vmcs12.guest_cr0 might not be up-to-date
11434 * because L2 may have changed some cr0 bits directly (CRO_GUEST_HOST_MASK).
11435 * This function returns the new value we should put in vmcs12.guest_cr0.
11436 * It's not enough to just return the vmcs02 GUEST_CR0. Rather,
11437 * 1. Bits that neither L0 nor L1 trapped, were set directly by L2 and are now
11438 * available in vmcs02 GUEST_CR0. (Note: It's enough to check that L0
11439 * didn't trap the bit, because if L1 did, so would L0).
11440 * 2. Bits that L1 asked to trap (and therefore L0 also did) could not have
11441 * been modified by L2, and L1 knows it. So just leave the old value of
11442 * the bit from vmcs12.guest_cr0. Note that the bit from vmcs02 GUEST_CR0
11443 * isn't relevant, because if L0 traps this bit it can set it to anything.
11444 * 3. Bits that L1 didn't trap, but L0 did. L1 believes the guest could have
11445 * changed these bits, and therefore they need to be updated, but L0
11446 * didn't necessarily allow them to be changed in GUEST_CR0 - and rather
11447 * put them in vmcs02 CR0_READ_SHADOW. So take these bits from there.
11449 static inline unsigned long
11450 vmcs12_guest_cr0(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
11453 /*1*/ (vmcs_readl(GUEST_CR0) & vcpu->arch.cr0_guest_owned_bits) |
11454 /*2*/ (vmcs12->guest_cr0 & vmcs12->cr0_guest_host_mask) |
11455 /*3*/ (vmcs_readl(CR0_READ_SHADOW) & ~(vmcs12->cr0_guest_host_mask |
11456 vcpu->arch.cr0_guest_owned_bits));
11459 static inline unsigned long
11460 vmcs12_guest_cr4(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
11463 /*1*/ (vmcs_readl(GUEST_CR4) & vcpu->arch.cr4_guest_owned_bits) |
11464 /*2*/ (vmcs12->guest_cr4 & vmcs12->cr4_guest_host_mask) |
11465 /*3*/ (vmcs_readl(CR4_READ_SHADOW) & ~(vmcs12->cr4_guest_host_mask |
11466 vcpu->arch.cr4_guest_owned_bits));
11469 static void vmcs12_save_pending_event(struct kvm_vcpu *vcpu,
11470 struct vmcs12 *vmcs12)
11475 if (vcpu->arch.exception.injected) {
11476 nr = vcpu->arch.exception.nr;
11477 idt_vectoring = nr | VECTORING_INFO_VALID_MASK;
11479 if (kvm_exception_is_soft(nr)) {
11480 vmcs12->vm_exit_instruction_len =
11481 vcpu->arch.event_exit_inst_len;
11482 idt_vectoring |= INTR_TYPE_SOFT_EXCEPTION;
11484 idt_vectoring |= INTR_TYPE_HARD_EXCEPTION;
11486 if (vcpu->arch.exception.has_error_code) {
11487 idt_vectoring |= VECTORING_INFO_DELIVER_CODE_MASK;
11488 vmcs12->idt_vectoring_error_code =
11489 vcpu->arch.exception.error_code;
11492 vmcs12->idt_vectoring_info_field = idt_vectoring;
11493 } else if (vcpu->arch.nmi_injected) {
11494 vmcs12->idt_vectoring_info_field =
11495 INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK | NMI_VECTOR;
11496 } else if (vcpu->arch.interrupt.pending) {
11497 nr = vcpu->arch.interrupt.nr;
11498 idt_vectoring = nr | VECTORING_INFO_VALID_MASK;
11500 if (vcpu->arch.interrupt.soft) {
11501 idt_vectoring |= INTR_TYPE_SOFT_INTR;
11502 vmcs12->vm_entry_instruction_len =
11503 vcpu->arch.event_exit_inst_len;
11505 idt_vectoring |= INTR_TYPE_EXT_INTR;
11507 vmcs12->idt_vectoring_info_field = idt_vectoring;
11511 static int vmx_check_nested_events(struct kvm_vcpu *vcpu, bool external_intr)
11513 struct vcpu_vmx *vmx = to_vmx(vcpu);
11514 unsigned long exit_qual;
11515 bool block_nested_events =
11516 vmx->nested.nested_run_pending || kvm_event_needs_reinjection(vcpu);
11518 if (vcpu->arch.exception.pending &&
11519 nested_vmx_check_exception(vcpu, &exit_qual)) {
11520 if (block_nested_events)
11522 nested_vmx_inject_exception_vmexit(vcpu, exit_qual);
11526 if (nested_cpu_has_preemption_timer(get_vmcs12(vcpu)) &&
11527 vmx->nested.preemption_timer_expired) {
11528 if (block_nested_events)
11530 nested_vmx_vmexit(vcpu, EXIT_REASON_PREEMPTION_TIMER, 0, 0);
11534 if (vcpu->arch.nmi_pending && nested_exit_on_nmi(vcpu)) {
11535 if (block_nested_events)
11537 nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI,
11538 NMI_VECTOR | INTR_TYPE_NMI_INTR |
11539 INTR_INFO_VALID_MASK, 0);
11541 * The NMI-triggered VM exit counts as injection:
11542 * clear this one and block further NMIs.
11544 vcpu->arch.nmi_pending = 0;
11545 vmx_set_nmi_mask(vcpu, true);
11549 if ((kvm_cpu_has_interrupt(vcpu) || external_intr) &&
11550 nested_exit_on_intr(vcpu)) {
11551 if (block_nested_events)
11553 nested_vmx_vmexit(vcpu, EXIT_REASON_EXTERNAL_INTERRUPT, 0, 0);
11557 vmx_complete_nested_posted_interrupt(vcpu);
11561 static u32 vmx_get_preemption_timer_value(struct kvm_vcpu *vcpu)
11563 ktime_t remaining =
11564 hrtimer_get_remaining(&to_vmx(vcpu)->nested.preemption_timer);
11567 if (ktime_to_ns(remaining) <= 0)
11570 value = ktime_to_ns(remaining) * vcpu->arch.virtual_tsc_khz;
11571 do_div(value, 1000000);
11572 return value >> VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE;
11576 * Update the guest state fields of vmcs12 to reflect changes that
11577 * occurred while L2 was running. (The "IA-32e mode guest" bit of the
11578 * VM-entry controls is also updated, since this is really a guest
11581 static void sync_vmcs12(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
11583 vmcs12->guest_cr0 = vmcs12_guest_cr0(vcpu, vmcs12);
11584 vmcs12->guest_cr4 = vmcs12_guest_cr4(vcpu, vmcs12);
11586 vmcs12->guest_rsp = kvm_register_read(vcpu, VCPU_REGS_RSP);
11587 vmcs12->guest_rip = kvm_register_read(vcpu, VCPU_REGS_RIP);
11588 vmcs12->guest_rflags = vmcs_readl(GUEST_RFLAGS);
11590 vmcs12->guest_es_selector = vmcs_read16(GUEST_ES_SELECTOR);
11591 vmcs12->guest_cs_selector = vmcs_read16(GUEST_CS_SELECTOR);
11592 vmcs12->guest_ss_selector = vmcs_read16(GUEST_SS_SELECTOR);
11593 vmcs12->guest_ds_selector = vmcs_read16(GUEST_DS_SELECTOR);
11594 vmcs12->guest_fs_selector = vmcs_read16(GUEST_FS_SELECTOR);
11595 vmcs12->guest_gs_selector = vmcs_read16(GUEST_GS_SELECTOR);
11596 vmcs12->guest_ldtr_selector = vmcs_read16(GUEST_LDTR_SELECTOR);
11597 vmcs12->guest_tr_selector = vmcs_read16(GUEST_TR_SELECTOR);
11598 vmcs12->guest_es_limit = vmcs_read32(GUEST_ES_LIMIT);
11599 vmcs12->guest_cs_limit = vmcs_read32(GUEST_CS_LIMIT);
11600 vmcs12->guest_ss_limit = vmcs_read32(GUEST_SS_LIMIT);
11601 vmcs12->guest_ds_limit = vmcs_read32(GUEST_DS_LIMIT);
11602 vmcs12->guest_fs_limit = vmcs_read32(GUEST_FS_LIMIT);
11603 vmcs12->guest_gs_limit = vmcs_read32(GUEST_GS_LIMIT);
11604 vmcs12->guest_ldtr_limit = vmcs_read32(GUEST_LDTR_LIMIT);
11605 vmcs12->guest_tr_limit = vmcs_read32(GUEST_TR_LIMIT);
11606 vmcs12->guest_gdtr_limit = vmcs_read32(GUEST_GDTR_LIMIT);
11607 vmcs12->guest_idtr_limit = vmcs_read32(GUEST_IDTR_LIMIT);
11608 vmcs12->guest_es_ar_bytes = vmcs_read32(GUEST_ES_AR_BYTES);
11609 vmcs12->guest_cs_ar_bytes = vmcs_read32(GUEST_CS_AR_BYTES);
11610 vmcs12->guest_ss_ar_bytes = vmcs_read32(GUEST_SS_AR_BYTES);
11611 vmcs12->guest_ds_ar_bytes = vmcs_read32(GUEST_DS_AR_BYTES);
11612 vmcs12->guest_fs_ar_bytes = vmcs_read32(GUEST_FS_AR_BYTES);
11613 vmcs12->guest_gs_ar_bytes = vmcs_read32(GUEST_GS_AR_BYTES);
11614 vmcs12->guest_ldtr_ar_bytes = vmcs_read32(GUEST_LDTR_AR_BYTES);
11615 vmcs12->guest_tr_ar_bytes = vmcs_read32(GUEST_TR_AR_BYTES);
11616 vmcs12->guest_es_base = vmcs_readl(GUEST_ES_BASE);
11617 vmcs12->guest_cs_base = vmcs_readl(GUEST_CS_BASE);
11618 vmcs12->guest_ss_base = vmcs_readl(GUEST_SS_BASE);
11619 vmcs12->guest_ds_base = vmcs_readl(GUEST_DS_BASE);
11620 vmcs12->guest_fs_base = vmcs_readl(GUEST_FS_BASE);
11621 vmcs12->guest_gs_base = vmcs_readl(GUEST_GS_BASE);
11622 vmcs12->guest_ldtr_base = vmcs_readl(GUEST_LDTR_BASE);
11623 vmcs12->guest_tr_base = vmcs_readl(GUEST_TR_BASE);
11624 vmcs12->guest_gdtr_base = vmcs_readl(GUEST_GDTR_BASE);
11625 vmcs12->guest_idtr_base = vmcs_readl(GUEST_IDTR_BASE);
11627 vmcs12->guest_interruptibility_info =
11628 vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
11629 vmcs12->guest_pending_dbg_exceptions =
11630 vmcs_readl(GUEST_PENDING_DBG_EXCEPTIONS);
11631 if (vcpu->arch.mp_state == KVM_MP_STATE_HALTED)
11632 vmcs12->guest_activity_state = GUEST_ACTIVITY_HLT;
11634 vmcs12->guest_activity_state = GUEST_ACTIVITY_ACTIVE;
11636 if (nested_cpu_has_preemption_timer(vmcs12)) {
11637 if (vmcs12->vm_exit_controls &
11638 VM_EXIT_SAVE_VMX_PREEMPTION_TIMER)
11639 vmcs12->vmx_preemption_timer_value =
11640 vmx_get_preemption_timer_value(vcpu);
11641 hrtimer_cancel(&to_vmx(vcpu)->nested.preemption_timer);
11645 * In some cases (usually, nested EPT), L2 is allowed to change its
11646 * own CR3 without exiting. If it has changed it, we must keep it.
11647 * Of course, if L0 is using shadow page tables, GUEST_CR3 was defined
11648 * by L0, not L1 or L2, so we mustn't unconditionally copy it to vmcs12.
11650 * Additionally, restore L2's PDPTR to vmcs12.
11653 vmcs12->guest_cr3 = vmcs_readl(GUEST_CR3);
11654 vmcs12->guest_pdptr0 = vmcs_read64(GUEST_PDPTR0);
11655 vmcs12->guest_pdptr1 = vmcs_read64(GUEST_PDPTR1);
11656 vmcs12->guest_pdptr2 = vmcs_read64(GUEST_PDPTR2);
11657 vmcs12->guest_pdptr3 = vmcs_read64(GUEST_PDPTR3);
11660 vmcs12->guest_linear_address = vmcs_readl(GUEST_LINEAR_ADDRESS);
11662 if (nested_cpu_has_vid(vmcs12))
11663 vmcs12->guest_intr_status = vmcs_read16(GUEST_INTR_STATUS);
11665 vmcs12->vm_entry_controls =
11666 (vmcs12->vm_entry_controls & ~VM_ENTRY_IA32E_MODE) |
11667 (vm_entry_controls_get(to_vmx(vcpu)) & VM_ENTRY_IA32E_MODE);
11669 if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_DEBUG_CONTROLS) {
11670 kvm_get_dr(vcpu, 7, (unsigned long *)&vmcs12->guest_dr7);
11671 vmcs12->guest_ia32_debugctl = vmcs_read64(GUEST_IA32_DEBUGCTL);
11674 /* TODO: These cannot have changed unless we have MSR bitmaps and
11675 * the relevant bit asks not to trap the change */
11676 if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_IA32_PAT)
11677 vmcs12->guest_ia32_pat = vmcs_read64(GUEST_IA32_PAT);
11678 if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_IA32_EFER)
11679 vmcs12->guest_ia32_efer = vcpu->arch.efer;
11680 vmcs12->guest_sysenter_cs = vmcs_read32(GUEST_SYSENTER_CS);
11681 vmcs12->guest_sysenter_esp = vmcs_readl(GUEST_SYSENTER_ESP);
11682 vmcs12->guest_sysenter_eip = vmcs_readl(GUEST_SYSENTER_EIP);
11683 if (kvm_mpx_supported())
11684 vmcs12->guest_bndcfgs = vmcs_read64(GUEST_BNDCFGS);
11688 * prepare_vmcs12 is part of what we need to do when the nested L2 guest exits
11689 * and we want to prepare to run its L1 parent. L1 keeps a vmcs for L2 (vmcs12),
11690 * and this function updates it to reflect the changes to the guest state while
11691 * L2 was running (and perhaps made some exits which were handled directly by L0
11692 * without going back to L1), and to reflect the exit reason.
11693 * Note that we do not have to copy here all VMCS fields, just those that
11694 * could have changed by the L2 guest or the exit - i.e., the guest-state and
11695 * exit-information fields only. Other fields are modified by L1 with VMWRITE,
11696 * which already writes to vmcs12 directly.
11698 static void prepare_vmcs12(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12,
11699 u32 exit_reason, u32 exit_intr_info,
11700 unsigned long exit_qualification)
11702 /* update guest state fields: */
11703 sync_vmcs12(vcpu, vmcs12);
11705 /* update exit information fields: */
11707 vmcs12->vm_exit_reason = exit_reason;
11708 vmcs12->exit_qualification = exit_qualification;
11709 vmcs12->vm_exit_intr_info = exit_intr_info;
11711 vmcs12->idt_vectoring_info_field = 0;
11712 vmcs12->vm_exit_instruction_len = vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
11713 vmcs12->vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
11715 if (!(vmcs12->vm_exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY)) {
11716 vmcs12->launch_state = 1;
11718 /* vm_entry_intr_info_field is cleared on exit. Emulate this
11719 * instead of reading the real value. */
11720 vmcs12->vm_entry_intr_info_field &= ~INTR_INFO_VALID_MASK;
11723 * Transfer the event that L0 or L1 may wanted to inject into
11724 * L2 to IDT_VECTORING_INFO_FIELD.
11726 vmcs12_save_pending_event(vcpu, vmcs12);
11730 * Drop what we picked up for L2 via vmx_complete_interrupts. It is
11731 * preserved above and would only end up incorrectly in L1.
11733 vcpu->arch.nmi_injected = false;
11734 kvm_clear_exception_queue(vcpu);
11735 kvm_clear_interrupt_queue(vcpu);
11738 static void load_vmcs12_mmu_host_state(struct kvm_vcpu *vcpu,
11739 struct vmcs12 *vmcs12)
11741 u32 entry_failure_code;
11743 nested_ept_uninit_mmu_context(vcpu);
11746 * Only PDPTE load can fail as the value of cr3 was checked on entry and
11747 * couldn't have changed.
11749 if (nested_vmx_load_cr3(vcpu, vmcs12->host_cr3, false, &entry_failure_code))
11750 nested_vmx_abort(vcpu, VMX_ABORT_LOAD_HOST_PDPTE_FAIL);
11753 vcpu->arch.walk_mmu->inject_page_fault = kvm_inject_page_fault;
11757 * A part of what we need to when the nested L2 guest exits and we want to
11758 * run its L1 parent, is to reset L1's guest state to the host state specified
11760 * This function is to be called not only on normal nested exit, but also on
11761 * a nested entry failure, as explained in Intel's spec, 3B.23.7 ("VM-Entry
11762 * Failures During or After Loading Guest State").
11763 * This function should be called when the active VMCS is L1's (vmcs01).
11765 static void load_vmcs12_host_state(struct kvm_vcpu *vcpu,
11766 struct vmcs12 *vmcs12)
11768 struct kvm_segment seg;
11770 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_EFER)
11771 vcpu->arch.efer = vmcs12->host_ia32_efer;
11772 else if (vmcs12->vm_exit_controls & VM_EXIT_HOST_ADDR_SPACE_SIZE)
11773 vcpu->arch.efer |= (EFER_LMA | EFER_LME);
11775 vcpu->arch.efer &= ~(EFER_LMA | EFER_LME);
11776 vmx_set_efer(vcpu, vcpu->arch.efer);
11778 kvm_register_write(vcpu, VCPU_REGS_RSP, vmcs12->host_rsp);
11779 kvm_register_write(vcpu, VCPU_REGS_RIP, vmcs12->host_rip);
11780 vmx_set_rflags(vcpu, X86_EFLAGS_FIXED);
11782 * Note that calling vmx_set_cr0 is important, even if cr0 hasn't
11783 * actually changed, because vmx_set_cr0 refers to efer set above.
11785 * CR0_GUEST_HOST_MASK is already set in the original vmcs01
11786 * (KVM doesn't change it);
11788 vcpu->arch.cr0_guest_owned_bits = X86_CR0_TS;
11789 vmx_set_cr0(vcpu, vmcs12->host_cr0);
11791 /* Same as above - no reason to call set_cr4_guest_host_mask(). */
11792 vcpu->arch.cr4_guest_owned_bits = ~vmcs_readl(CR4_GUEST_HOST_MASK);
11793 vmx_set_cr4(vcpu, vmcs12->host_cr4);
11795 load_vmcs12_mmu_host_state(vcpu, vmcs12);
11799 * Trivially support vpid by letting L2s share their parent
11800 * L1's vpid. TODO: move to a more elaborate solution, giving
11801 * each L2 its own vpid and exposing the vpid feature to L1.
11803 vmx_flush_tlb(vcpu);
11805 /* Restore posted intr vector. */
11806 if (nested_cpu_has_posted_intr(vmcs12))
11807 vmcs_write16(POSTED_INTR_NV, POSTED_INTR_VECTOR);
11809 vmcs_write32(GUEST_SYSENTER_CS, vmcs12->host_ia32_sysenter_cs);
11810 vmcs_writel(GUEST_SYSENTER_ESP, vmcs12->host_ia32_sysenter_esp);
11811 vmcs_writel(GUEST_SYSENTER_EIP, vmcs12->host_ia32_sysenter_eip);
11812 vmcs_writel(GUEST_IDTR_BASE, vmcs12->host_idtr_base);
11813 vmcs_writel(GUEST_GDTR_BASE, vmcs12->host_gdtr_base);
11814 vmcs_write32(GUEST_IDTR_LIMIT, 0xFFFF);
11815 vmcs_write32(GUEST_GDTR_LIMIT, 0xFFFF);
11817 /* If not VM_EXIT_CLEAR_BNDCFGS, the L2 value propagates to L1. */
11818 if (vmcs12->vm_exit_controls & VM_EXIT_CLEAR_BNDCFGS)
11819 vmcs_write64(GUEST_BNDCFGS, 0);
11821 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PAT) {
11822 vmcs_write64(GUEST_IA32_PAT, vmcs12->host_ia32_pat);
11823 vcpu->arch.pat = vmcs12->host_ia32_pat;
11825 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL)
11826 vmcs_write64(GUEST_IA32_PERF_GLOBAL_CTRL,
11827 vmcs12->host_ia32_perf_global_ctrl);
11829 /* Set L1 segment info according to Intel SDM
11830 27.5.2 Loading Host Segment and Descriptor-Table Registers */
11831 seg = (struct kvm_segment) {
11833 .limit = 0xFFFFFFFF,
11834 .selector = vmcs12->host_cs_selector,
11840 if (vmcs12->vm_exit_controls & VM_EXIT_HOST_ADDR_SPACE_SIZE)
11844 vmx_set_segment(vcpu, &seg, VCPU_SREG_CS);
11845 seg = (struct kvm_segment) {
11847 .limit = 0xFFFFFFFF,
11854 seg.selector = vmcs12->host_ds_selector;
11855 vmx_set_segment(vcpu, &seg, VCPU_SREG_DS);
11856 seg.selector = vmcs12->host_es_selector;
11857 vmx_set_segment(vcpu, &seg, VCPU_SREG_ES);
11858 seg.selector = vmcs12->host_ss_selector;
11859 vmx_set_segment(vcpu, &seg, VCPU_SREG_SS);
11860 seg.selector = vmcs12->host_fs_selector;
11861 seg.base = vmcs12->host_fs_base;
11862 vmx_set_segment(vcpu, &seg, VCPU_SREG_FS);
11863 seg.selector = vmcs12->host_gs_selector;
11864 seg.base = vmcs12->host_gs_base;
11865 vmx_set_segment(vcpu, &seg, VCPU_SREG_GS);
11866 seg = (struct kvm_segment) {
11867 .base = vmcs12->host_tr_base,
11869 .selector = vmcs12->host_tr_selector,
11873 vmx_set_segment(vcpu, &seg, VCPU_SREG_TR);
11875 kvm_set_dr(vcpu, 7, 0x400);
11876 vmcs_write64(GUEST_IA32_DEBUGCTL, 0);
11878 if (cpu_has_vmx_msr_bitmap())
11879 vmx_update_msr_bitmap(vcpu);
11881 if (nested_vmx_load_msr(vcpu, vmcs12->vm_exit_msr_load_addr,
11882 vmcs12->vm_exit_msr_load_count))
11883 nested_vmx_abort(vcpu, VMX_ABORT_LOAD_HOST_MSR_FAIL);
11887 * Emulate an exit from nested guest (L2) to L1, i.e., prepare to run L1
11888 * and modify vmcs12 to make it see what it would expect to see there if
11889 * L2 was its real guest. Must only be called when in L2 (is_guest_mode())
11891 static void nested_vmx_vmexit(struct kvm_vcpu *vcpu, u32 exit_reason,
11892 u32 exit_intr_info,
11893 unsigned long exit_qualification)
11895 struct vcpu_vmx *vmx = to_vmx(vcpu);
11896 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
11898 /* trying to cancel vmlaunch/vmresume is a bug */
11899 WARN_ON_ONCE(vmx->nested.nested_run_pending);
11902 * The only expected VM-instruction error is "VM entry with
11903 * invalid control field(s)." Anything else indicates a
11906 WARN_ON_ONCE(vmx->fail && (vmcs_read32(VM_INSTRUCTION_ERROR) !=
11907 VMXERR_ENTRY_INVALID_CONTROL_FIELD));
11909 leave_guest_mode(vcpu);
11911 if (likely(!vmx->fail)) {
11912 prepare_vmcs12(vcpu, vmcs12, exit_reason, exit_intr_info,
11913 exit_qualification);
11915 if (nested_vmx_store_msr(vcpu, vmcs12->vm_exit_msr_store_addr,
11916 vmcs12->vm_exit_msr_store_count))
11917 nested_vmx_abort(vcpu, VMX_ABORT_SAVE_GUEST_MSR_FAIL);
11920 vmx_switch_vmcs(vcpu, &vmx->vmcs01);
11921 vm_entry_controls_reset_shadow(vmx);
11922 vm_exit_controls_reset_shadow(vmx);
11923 vmx_segment_cache_clear(vmx);
11925 /* Update any VMCS fields that might have changed while L2 ran */
11926 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, vmx->msr_autoload.host.nr);
11927 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, vmx->msr_autoload.guest.nr);
11928 vmcs_write64(TSC_OFFSET, vcpu->arch.tsc_offset);
11929 if (vmx->hv_deadline_tsc == -1)
11930 vmcs_clear_bits(PIN_BASED_VM_EXEC_CONTROL,
11931 PIN_BASED_VMX_PREEMPTION_TIMER);
11933 vmcs_set_bits(PIN_BASED_VM_EXEC_CONTROL,
11934 PIN_BASED_VMX_PREEMPTION_TIMER);
11935 if (kvm_has_tsc_control)
11936 decache_tsc_multiplier(vmx);
11938 if (vmx->nested.change_vmcs01_virtual_x2apic_mode) {
11939 vmx->nested.change_vmcs01_virtual_x2apic_mode = false;
11940 vmx_set_virtual_x2apic_mode(vcpu,
11941 vcpu->arch.apic_base & X2APIC_ENABLE);
11942 } else if (!nested_cpu_has_ept(vmcs12) &&
11943 nested_cpu_has2(vmcs12,
11944 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) {
11945 vmx_flush_tlb_ept_only(vcpu);
11948 /* This is needed for same reason as it was needed in prepare_vmcs02 */
11951 /* Unpin physical memory we referred to in vmcs02 */
11952 if (vmx->nested.apic_access_page) {
11953 kvm_release_page_dirty(vmx->nested.apic_access_page);
11954 vmx->nested.apic_access_page = NULL;
11956 if (vmx->nested.virtual_apic_page) {
11957 kvm_release_page_dirty(vmx->nested.virtual_apic_page);
11958 vmx->nested.virtual_apic_page = NULL;
11960 if (vmx->nested.pi_desc_page) {
11961 kunmap(vmx->nested.pi_desc_page);
11962 kvm_release_page_dirty(vmx->nested.pi_desc_page);
11963 vmx->nested.pi_desc_page = NULL;
11964 vmx->nested.pi_desc = NULL;
11968 * We are now running in L2, mmu_notifier will force to reload the
11969 * page's hpa for L2 vmcs. Need to reload it for L1 before entering L1.
11971 kvm_make_request(KVM_REQ_APIC_PAGE_RELOAD, vcpu);
11973 if (enable_shadow_vmcs)
11974 vmx->nested.sync_shadow_vmcs = true;
11976 /* in case we halted in L2 */
11977 vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
11979 if (likely(!vmx->fail)) {
11981 * TODO: SDM says that with acknowledge interrupt on
11982 * exit, bit 31 of the VM-exit interrupt information
11983 * (valid interrupt) is always set to 1 on
11984 * EXIT_REASON_EXTERNAL_INTERRUPT, so we shouldn't
11985 * need kvm_cpu_has_interrupt(). See the commit
11986 * message for details.
11988 if (nested_exit_intr_ack_set(vcpu) &&
11989 exit_reason == EXIT_REASON_EXTERNAL_INTERRUPT &&
11990 kvm_cpu_has_interrupt(vcpu)) {
11991 int irq = kvm_cpu_get_interrupt(vcpu);
11993 vmcs12->vm_exit_intr_info = irq |
11994 INTR_INFO_VALID_MASK | INTR_TYPE_EXT_INTR;
11997 trace_kvm_nested_vmexit_inject(vmcs12->vm_exit_reason,
11998 vmcs12->exit_qualification,
11999 vmcs12->idt_vectoring_info_field,
12000 vmcs12->vm_exit_intr_info,
12001 vmcs12->vm_exit_intr_error_code,
12004 load_vmcs12_host_state(vcpu, vmcs12);
12010 * After an early L2 VM-entry failure, we're now back
12011 * in L1 which thinks it just finished a VMLAUNCH or
12012 * VMRESUME instruction, so we need to set the failure
12013 * flag and the VM-instruction error field of the VMCS
12016 nested_vmx_failValid(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD);
12018 load_vmcs12_mmu_host_state(vcpu, vmcs12);
12021 * The emulated instruction was already skipped in
12022 * nested_vmx_run, but the updated RIP was never
12023 * written back to the vmcs01.
12025 skip_emulated_instruction(vcpu);
12030 * Forcibly leave nested mode in order to be able to reset the VCPU later on.
12032 static void vmx_leave_nested(struct kvm_vcpu *vcpu)
12034 if (is_guest_mode(vcpu)) {
12035 to_vmx(vcpu)->nested.nested_run_pending = 0;
12036 nested_vmx_vmexit(vcpu, -1, 0, 0);
12038 free_nested(to_vmx(vcpu));
12042 * L1's failure to enter L2 is a subset of a normal exit, as explained in
12043 * 23.7 "VM-entry failures during or after loading guest state" (this also
12044 * lists the acceptable exit-reason and exit-qualification parameters).
12045 * It should only be called before L2 actually succeeded to run, and when
12046 * vmcs01 is current (it doesn't leave_guest_mode() or switch vmcss).
12048 static void nested_vmx_entry_failure(struct kvm_vcpu *vcpu,
12049 struct vmcs12 *vmcs12,
12050 u32 reason, unsigned long qualification)
12052 load_vmcs12_host_state(vcpu, vmcs12);
12053 vmcs12->vm_exit_reason = reason | VMX_EXIT_REASONS_FAILED_VMENTRY;
12054 vmcs12->exit_qualification = qualification;
12055 nested_vmx_succeed(vcpu);
12056 if (enable_shadow_vmcs)
12057 to_vmx(vcpu)->nested.sync_shadow_vmcs = true;
12060 static int vmx_check_intercept(struct kvm_vcpu *vcpu,
12061 struct x86_instruction_info *info,
12062 enum x86_intercept_stage stage)
12064 return X86EMUL_CONTINUE;
12067 #ifdef CONFIG_X86_64
12068 /* (a << shift) / divisor, return 1 if overflow otherwise 0 */
12069 static inline int u64_shl_div_u64(u64 a, unsigned int shift,
12070 u64 divisor, u64 *result)
12072 u64 low = a << shift, high = a >> (64 - shift);
12074 /* To avoid the overflow on divq */
12075 if (high >= divisor)
12078 /* Low hold the result, high hold rem which is discarded */
12079 asm("divq %2\n\t" : "=a" (low), "=d" (high) :
12080 "rm" (divisor), "0" (low), "1" (high));
12086 static int vmx_set_hv_timer(struct kvm_vcpu *vcpu, u64 guest_deadline_tsc)
12088 struct vcpu_vmx *vmx = to_vmx(vcpu);
12089 u64 tscl = rdtsc();
12090 u64 guest_tscl = kvm_read_l1_tsc(vcpu, tscl);
12091 u64 delta_tsc = max(guest_deadline_tsc, guest_tscl) - guest_tscl;
12093 /* Convert to host delta tsc if tsc scaling is enabled */
12094 if (vcpu->arch.tsc_scaling_ratio != kvm_default_tsc_scaling_ratio &&
12095 u64_shl_div_u64(delta_tsc,
12096 kvm_tsc_scaling_ratio_frac_bits,
12097 vcpu->arch.tsc_scaling_ratio,
12102 * If the delta tsc can't fit in the 32 bit after the multi shift,
12103 * we can't use the preemption timer.
12104 * It's possible that it fits on later vmentries, but checking
12105 * on every vmentry is costly so we just use an hrtimer.
12107 if (delta_tsc >> (cpu_preemption_timer_multi + 32))
12110 vmx->hv_deadline_tsc = tscl + delta_tsc;
12111 vmcs_set_bits(PIN_BASED_VM_EXEC_CONTROL,
12112 PIN_BASED_VMX_PREEMPTION_TIMER);
12114 return delta_tsc == 0;
12117 static void vmx_cancel_hv_timer(struct kvm_vcpu *vcpu)
12119 struct vcpu_vmx *vmx = to_vmx(vcpu);
12120 vmx->hv_deadline_tsc = -1;
12121 vmcs_clear_bits(PIN_BASED_VM_EXEC_CONTROL,
12122 PIN_BASED_VMX_PREEMPTION_TIMER);
12126 static void vmx_sched_in(struct kvm_vcpu *vcpu, int cpu)
12129 shrink_ple_window(vcpu);
12132 static void vmx_slot_enable_log_dirty(struct kvm *kvm,
12133 struct kvm_memory_slot *slot)
12135 kvm_mmu_slot_leaf_clear_dirty(kvm, slot);
12136 kvm_mmu_slot_largepage_remove_write_access(kvm, slot);
12139 static void vmx_slot_disable_log_dirty(struct kvm *kvm,
12140 struct kvm_memory_slot *slot)
12142 kvm_mmu_slot_set_dirty(kvm, slot);
12145 static void vmx_flush_log_dirty(struct kvm *kvm)
12147 kvm_flush_pml_buffers(kvm);
12150 static int vmx_write_pml_buffer(struct kvm_vcpu *vcpu)
12152 struct vmcs12 *vmcs12;
12153 struct vcpu_vmx *vmx = to_vmx(vcpu);
12155 struct page *page = NULL;
12158 if (is_guest_mode(vcpu)) {
12159 WARN_ON_ONCE(vmx->nested.pml_full);
12162 * Check if PML is enabled for the nested guest.
12163 * Whether eptp bit 6 is set is already checked
12164 * as part of A/D emulation.
12166 vmcs12 = get_vmcs12(vcpu);
12167 if (!nested_cpu_has_pml(vmcs12))
12170 if (vmcs12->guest_pml_index >= PML_ENTITY_NUM) {
12171 vmx->nested.pml_full = true;
12175 gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS) & ~0xFFFull;
12177 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->pml_address);
12178 if (is_error_page(page))
12181 pml_address = kmap(page);
12182 pml_address[vmcs12->guest_pml_index--] = gpa;
12184 kvm_release_page_clean(page);
12190 static void vmx_enable_log_dirty_pt_masked(struct kvm *kvm,
12191 struct kvm_memory_slot *memslot,
12192 gfn_t offset, unsigned long mask)
12194 kvm_mmu_clear_dirty_pt_masked(kvm, memslot, offset, mask);
12197 static void __pi_post_block(struct kvm_vcpu *vcpu)
12199 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
12200 struct pi_desc old, new;
12204 old.control = new.control = pi_desc->control;
12205 WARN(old.nv != POSTED_INTR_WAKEUP_VECTOR,
12206 "Wakeup handler not enabled while the VCPU is blocked\n");
12208 dest = cpu_physical_id(vcpu->cpu);
12210 if (x2apic_enabled())
12213 new.ndst = (dest << 8) & 0xFF00;
12215 /* set 'NV' to 'notification vector' */
12216 new.nv = POSTED_INTR_VECTOR;
12217 } while (cmpxchg64(&pi_desc->control, old.control,
12218 new.control) != old.control);
12220 if (!WARN_ON_ONCE(vcpu->pre_pcpu == -1)) {
12221 spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
12222 list_del(&vcpu->blocked_vcpu_list);
12223 spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
12224 vcpu->pre_pcpu = -1;
12229 * This routine does the following things for vCPU which is going
12230 * to be blocked if VT-d PI is enabled.
12231 * - Store the vCPU to the wakeup list, so when interrupts happen
12232 * we can find the right vCPU to wake up.
12233 * - Change the Posted-interrupt descriptor as below:
12234 * 'NDST' <-- vcpu->pre_pcpu
12235 * 'NV' <-- POSTED_INTR_WAKEUP_VECTOR
12236 * - If 'ON' is set during this process, which means at least one
12237 * interrupt is posted for this vCPU, we cannot block it, in
12238 * this case, return 1, otherwise, return 0.
12241 static int pi_pre_block(struct kvm_vcpu *vcpu)
12244 struct pi_desc old, new;
12245 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
12247 if (!kvm_arch_has_assigned_device(vcpu->kvm) ||
12248 !irq_remapping_cap(IRQ_POSTING_CAP) ||
12249 !kvm_vcpu_apicv_active(vcpu))
12252 WARN_ON(irqs_disabled());
12253 local_irq_disable();
12254 if (!WARN_ON_ONCE(vcpu->pre_pcpu != -1)) {
12255 vcpu->pre_pcpu = vcpu->cpu;
12256 spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
12257 list_add_tail(&vcpu->blocked_vcpu_list,
12258 &per_cpu(blocked_vcpu_on_cpu,
12260 spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
12264 old.control = new.control = pi_desc->control;
12266 WARN((pi_desc->sn == 1),
12267 "Warning: SN field of posted-interrupts "
12268 "is set before blocking\n");
12271 * Since vCPU can be preempted during this process,
12272 * vcpu->cpu could be different with pre_pcpu, we
12273 * need to set pre_pcpu as the destination of wakeup
12274 * notification event, then we can find the right vCPU
12275 * to wakeup in wakeup handler if interrupts happen
12276 * when the vCPU is in blocked state.
12278 dest = cpu_physical_id(vcpu->pre_pcpu);
12280 if (x2apic_enabled())
12283 new.ndst = (dest << 8) & 0xFF00;
12285 /* set 'NV' to 'wakeup vector' */
12286 new.nv = POSTED_INTR_WAKEUP_VECTOR;
12287 } while (cmpxchg64(&pi_desc->control, old.control,
12288 new.control) != old.control);
12290 /* We should not block the vCPU if an interrupt is posted for it. */
12291 if (pi_test_on(pi_desc) == 1)
12292 __pi_post_block(vcpu);
12294 local_irq_enable();
12295 return (vcpu->pre_pcpu == -1);
12298 static int vmx_pre_block(struct kvm_vcpu *vcpu)
12300 if (pi_pre_block(vcpu))
12303 if (kvm_lapic_hv_timer_in_use(vcpu))
12304 kvm_lapic_switch_to_sw_timer(vcpu);
12309 static void pi_post_block(struct kvm_vcpu *vcpu)
12311 if (vcpu->pre_pcpu == -1)
12314 WARN_ON(irqs_disabled());
12315 local_irq_disable();
12316 __pi_post_block(vcpu);
12317 local_irq_enable();
12320 static void vmx_post_block(struct kvm_vcpu *vcpu)
12322 if (kvm_x86_ops->set_hv_timer)
12323 kvm_lapic_switch_to_hv_timer(vcpu);
12325 pi_post_block(vcpu);
12329 * vmx_update_pi_irte - set IRTE for Posted-Interrupts
12332 * @host_irq: host irq of the interrupt
12333 * @guest_irq: gsi of the interrupt
12334 * @set: set or unset PI
12335 * returns 0 on success, < 0 on failure
12337 static int vmx_update_pi_irte(struct kvm *kvm, unsigned int host_irq,
12338 uint32_t guest_irq, bool set)
12340 struct kvm_kernel_irq_routing_entry *e;
12341 struct kvm_irq_routing_table *irq_rt;
12342 struct kvm_lapic_irq irq;
12343 struct kvm_vcpu *vcpu;
12344 struct vcpu_data vcpu_info;
12347 if (!kvm_arch_has_assigned_device(kvm) ||
12348 !irq_remapping_cap(IRQ_POSTING_CAP) ||
12349 !kvm_vcpu_apicv_active(kvm->vcpus[0]))
12352 idx = srcu_read_lock(&kvm->irq_srcu);
12353 irq_rt = srcu_dereference(kvm->irq_routing, &kvm->irq_srcu);
12354 if (guest_irq >= irq_rt->nr_rt_entries ||
12355 hlist_empty(&irq_rt->map[guest_irq])) {
12356 pr_warn_once("no route for guest_irq %u/%u (broken user space?)\n",
12357 guest_irq, irq_rt->nr_rt_entries);
12361 hlist_for_each_entry(e, &irq_rt->map[guest_irq], link) {
12362 if (e->type != KVM_IRQ_ROUTING_MSI)
12365 * VT-d PI cannot support posting multicast/broadcast
12366 * interrupts to a vCPU, we still use interrupt remapping
12367 * for these kind of interrupts.
12369 * For lowest-priority interrupts, we only support
12370 * those with single CPU as the destination, e.g. user
12371 * configures the interrupts via /proc/irq or uses
12372 * irqbalance to make the interrupts single-CPU.
12374 * We will support full lowest-priority interrupt later.
12377 kvm_set_msi_irq(kvm, e, &irq);
12378 if (!kvm_intr_is_single_vcpu(kvm, &irq, &vcpu)) {
12380 * Make sure the IRTE is in remapped mode if
12381 * we don't handle it in posted mode.
12383 ret = irq_set_vcpu_affinity(host_irq, NULL);
12386 "failed to back to remapped mode, irq: %u\n",
12394 vcpu_info.pi_desc_addr = __pa(vcpu_to_pi_desc(vcpu));
12395 vcpu_info.vector = irq.vector;
12397 trace_kvm_pi_irte_update(host_irq, vcpu->vcpu_id, e->gsi,
12398 vcpu_info.vector, vcpu_info.pi_desc_addr, set);
12401 ret = irq_set_vcpu_affinity(host_irq, &vcpu_info);
12403 ret = irq_set_vcpu_affinity(host_irq, NULL);
12406 printk(KERN_INFO "%s: failed to update PI IRTE\n",
12414 srcu_read_unlock(&kvm->irq_srcu, idx);
12418 static void vmx_setup_mce(struct kvm_vcpu *vcpu)
12420 if (vcpu->arch.mcg_cap & MCG_LMCE_P)
12421 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits |=
12422 FEATURE_CONTROL_LMCE;
12424 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits &=
12425 ~FEATURE_CONTROL_LMCE;
12428 static struct kvm_x86_ops vmx_x86_ops __ro_after_init = {
12429 .cpu_has_kvm_support = cpu_has_kvm_support,
12430 .disabled_by_bios = vmx_disabled_by_bios,
12431 .hardware_setup = hardware_setup,
12432 .hardware_unsetup = hardware_unsetup,
12433 .check_processor_compatibility = vmx_check_processor_compat,
12434 .hardware_enable = hardware_enable,
12435 .hardware_disable = hardware_disable,
12436 .cpu_has_accelerated_tpr = report_flexpriority,
12437 .has_emulated_msr = vmx_has_emulated_msr,
12439 .vm_init = vmx_vm_init,
12441 .vcpu_create = vmx_create_vcpu,
12442 .vcpu_free = vmx_free_vcpu,
12443 .vcpu_reset = vmx_vcpu_reset,
12445 .prepare_guest_switch = vmx_save_host_state,
12446 .vcpu_load = vmx_vcpu_load,
12447 .vcpu_put = vmx_vcpu_put,
12449 .update_bp_intercept = update_exception_bitmap,
12450 .get_msr = vmx_get_msr,
12451 .set_msr = vmx_set_msr,
12452 .get_segment_base = vmx_get_segment_base,
12453 .get_segment = vmx_get_segment,
12454 .set_segment = vmx_set_segment,
12455 .get_cpl = vmx_get_cpl,
12456 .get_cs_db_l_bits = vmx_get_cs_db_l_bits,
12457 .decache_cr0_guest_bits = vmx_decache_cr0_guest_bits,
12458 .decache_cr3 = vmx_decache_cr3,
12459 .decache_cr4_guest_bits = vmx_decache_cr4_guest_bits,
12460 .set_cr0 = vmx_set_cr0,
12461 .set_cr3 = vmx_set_cr3,
12462 .set_cr4 = vmx_set_cr4,
12463 .set_efer = vmx_set_efer,
12464 .get_idt = vmx_get_idt,
12465 .set_idt = vmx_set_idt,
12466 .get_gdt = vmx_get_gdt,
12467 .set_gdt = vmx_set_gdt,
12468 .get_dr6 = vmx_get_dr6,
12469 .set_dr6 = vmx_set_dr6,
12470 .set_dr7 = vmx_set_dr7,
12471 .sync_dirty_debug_regs = vmx_sync_dirty_debug_regs,
12472 .cache_reg = vmx_cache_reg,
12473 .get_rflags = vmx_get_rflags,
12474 .set_rflags = vmx_set_rflags,
12476 .tlb_flush = vmx_flush_tlb,
12478 .run = vmx_vcpu_run,
12479 .handle_exit = vmx_handle_exit,
12480 .skip_emulated_instruction = skip_emulated_instruction,
12481 .set_interrupt_shadow = vmx_set_interrupt_shadow,
12482 .get_interrupt_shadow = vmx_get_interrupt_shadow,
12483 .patch_hypercall = vmx_patch_hypercall,
12484 .set_irq = vmx_inject_irq,
12485 .set_nmi = vmx_inject_nmi,
12486 .queue_exception = vmx_queue_exception,
12487 .cancel_injection = vmx_cancel_injection,
12488 .interrupt_allowed = vmx_interrupt_allowed,
12489 .nmi_allowed = vmx_nmi_allowed,
12490 .get_nmi_mask = vmx_get_nmi_mask,
12491 .set_nmi_mask = vmx_set_nmi_mask,
12492 .enable_nmi_window = enable_nmi_window,
12493 .enable_irq_window = enable_irq_window,
12494 .update_cr8_intercept = update_cr8_intercept,
12495 .set_virtual_x2apic_mode = vmx_set_virtual_x2apic_mode,
12496 .set_apic_access_page_addr = vmx_set_apic_access_page_addr,
12497 .get_enable_apicv = vmx_get_enable_apicv,
12498 .refresh_apicv_exec_ctrl = vmx_refresh_apicv_exec_ctrl,
12499 .load_eoi_exitmap = vmx_load_eoi_exitmap,
12500 .apicv_post_state_restore = vmx_apicv_post_state_restore,
12501 .hwapic_irr_update = vmx_hwapic_irr_update,
12502 .hwapic_isr_update = vmx_hwapic_isr_update,
12503 .sync_pir_to_irr = vmx_sync_pir_to_irr,
12504 .deliver_posted_interrupt = vmx_deliver_posted_interrupt,
12506 .set_tss_addr = vmx_set_tss_addr,
12507 .get_tdp_level = get_ept_level,
12508 .get_mt_mask = vmx_get_mt_mask,
12510 .get_exit_info = vmx_get_exit_info,
12512 .get_lpage_level = vmx_get_lpage_level,
12514 .cpuid_update = vmx_cpuid_update,
12516 .rdtscp_supported = vmx_rdtscp_supported,
12517 .invpcid_supported = vmx_invpcid_supported,
12519 .set_supported_cpuid = vmx_set_supported_cpuid,
12521 .has_wbinvd_exit = cpu_has_vmx_wbinvd_exit,
12523 .write_tsc_offset = vmx_write_tsc_offset,
12525 .set_tdp_cr3 = vmx_set_cr3,
12527 .check_intercept = vmx_check_intercept,
12528 .handle_external_intr = vmx_handle_external_intr,
12529 .mpx_supported = vmx_mpx_supported,
12530 .xsaves_supported = vmx_xsaves_supported,
12532 .check_nested_events = vmx_check_nested_events,
12534 .sched_in = vmx_sched_in,
12536 .slot_enable_log_dirty = vmx_slot_enable_log_dirty,
12537 .slot_disable_log_dirty = vmx_slot_disable_log_dirty,
12538 .flush_log_dirty = vmx_flush_log_dirty,
12539 .enable_log_dirty_pt_masked = vmx_enable_log_dirty_pt_masked,
12540 .write_log_dirty = vmx_write_pml_buffer,
12542 .pre_block = vmx_pre_block,
12543 .post_block = vmx_post_block,
12545 .pmu_ops = &intel_pmu_ops,
12547 .update_pi_irte = vmx_update_pi_irte,
12549 #ifdef CONFIG_X86_64
12550 .set_hv_timer = vmx_set_hv_timer,
12551 .cancel_hv_timer = vmx_cancel_hv_timer,
12554 .setup_mce = vmx_setup_mce,
12557 static void vmx_cleanup_l1d_flush(void)
12559 if (vmx_l1d_flush_pages) {
12560 free_pages((unsigned long)vmx_l1d_flush_pages, L1D_CACHE_ORDER);
12561 vmx_l1d_flush_pages = NULL;
12563 /* Restore state so sysfs ignores VMX */
12564 l1tf_vmx_mitigation = VMENTER_L1D_FLUSH_AUTO;
12568 static void vmx_exit(void)
12570 #ifdef CONFIG_KEXEC_CORE
12571 RCU_INIT_POINTER(crash_vmclear_loaded_vmcss, NULL);
12577 vmx_cleanup_l1d_flush();
12579 module_exit(vmx_exit)
12581 static int __init vmx_init(void)
12585 r = kvm_init(&vmx_x86_ops, sizeof(struct vcpu_vmx),
12586 __alignof__(struct vcpu_vmx), THIS_MODULE);
12591 * Must be called after kvm_init() so enable_ept is properly set
12592 * up. Hand the parameter mitigation value in which was stored in
12593 * the pre module init parser. If no parameter was given, it will
12594 * contain 'auto' which will be turned into the default 'cond'
12597 if (boot_cpu_has(X86_BUG_L1TF)) {
12598 r = vmx_setup_l1d_flush(vmentry_l1d_flush_param);
12605 #ifdef CONFIG_KEXEC_CORE
12606 rcu_assign_pointer(crash_vmclear_loaded_vmcss,
12607 crash_vmclear_local_loaded_vmcss);
12612 module_init(vmx_init)