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/sched/smt.h>
31 #include <linux/moduleparam.h>
32 #include <linux/mod_devicetable.h>
33 #include <linux/trace_events.h>
34 #include <linux/slab.h>
35 #include <linux/tboot.h>
36 #include <linux/hrtimer.h>
37 #include <linux/frame.h>
38 #include <linux/nospec.h>
39 #include "kvm_cache_regs.h"
47 #include <asm/virtext.h>
49 #include <asm/fpu/internal.h>
50 #include <asm/perf_event.h>
51 #include <asm/debugreg.h>
52 #include <asm/kexec.h>
54 #include <asm/irq_remapping.h>
55 #include <asm/mmu_context.h>
56 #include <asm/spec-ctrl.h>
57 #include <asm/mshyperv.h>
61 #include "vmx_evmcs.h"
63 #define __ex(x) __kvm_handle_fault_on_reboot(x)
64 #define __ex_clear(x, reg) \
65 ____kvm_handle_fault_on_reboot(x, "xor " reg " , " reg)
67 MODULE_AUTHOR("Qumranet");
68 MODULE_LICENSE("GPL");
70 static const struct x86_cpu_id vmx_cpu_id[] = {
71 X86_FEATURE_MATCH(X86_FEATURE_VMX),
74 MODULE_DEVICE_TABLE(x86cpu, vmx_cpu_id);
76 static bool __read_mostly enable_vpid = 1;
77 module_param_named(vpid, enable_vpid, bool, 0444);
79 static bool __read_mostly enable_vnmi = 1;
80 module_param_named(vnmi, enable_vnmi, bool, S_IRUGO);
82 static bool __read_mostly flexpriority_enabled = 1;
83 module_param_named(flexpriority, flexpriority_enabled, bool, S_IRUGO);
85 static bool __read_mostly enable_ept = 1;
86 module_param_named(ept, enable_ept, bool, S_IRUGO);
88 static bool __read_mostly enable_unrestricted_guest = 1;
89 module_param_named(unrestricted_guest,
90 enable_unrestricted_guest, bool, S_IRUGO);
92 static bool __read_mostly enable_ept_ad_bits = 1;
93 module_param_named(eptad, enable_ept_ad_bits, bool, S_IRUGO);
95 static bool __read_mostly emulate_invalid_guest_state = true;
96 module_param(emulate_invalid_guest_state, bool, S_IRUGO);
98 static bool __read_mostly fasteoi = 1;
99 module_param(fasteoi, bool, S_IRUGO);
101 static bool __read_mostly enable_apicv = 1;
102 module_param(enable_apicv, bool, S_IRUGO);
104 static bool __read_mostly enable_shadow_vmcs = 1;
105 module_param_named(enable_shadow_vmcs, enable_shadow_vmcs, bool, S_IRUGO);
107 * If nested=1, nested virtualization is supported, i.e., guests may use
108 * VMX and be a hypervisor for its own guests. If nested=0, guests may not
109 * use VMX instructions.
111 static bool __read_mostly nested = 0;
112 module_param(nested, bool, S_IRUGO);
114 static u64 __read_mostly host_xss;
116 static bool __read_mostly enable_pml = 1;
117 module_param_named(pml, enable_pml, bool, S_IRUGO);
121 #define MSR_TYPE_RW 3
123 #define MSR_BITMAP_MODE_X2APIC 1
124 #define MSR_BITMAP_MODE_X2APIC_APICV 2
126 #define KVM_VMX_TSC_MULTIPLIER_MAX 0xffffffffffffffffULL
128 /* Guest_tsc -> host_tsc conversion requires 64-bit division. */
129 static int __read_mostly cpu_preemption_timer_multi;
130 static bool __read_mostly enable_preemption_timer = 1;
132 module_param_named(preemption_timer, enable_preemption_timer, bool, S_IRUGO);
135 #define KVM_GUEST_CR0_MASK (X86_CR0_NW | X86_CR0_CD)
136 #define KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST X86_CR0_NE
137 #define KVM_VM_CR0_ALWAYS_ON \
138 (KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST | \
139 X86_CR0_WP | X86_CR0_PG | X86_CR0_PE)
140 #define KVM_CR4_GUEST_OWNED_BITS \
141 (X86_CR4_PVI | X86_CR4_DE | X86_CR4_PCE | X86_CR4_OSFXSR \
142 | X86_CR4_OSXMMEXCPT | X86_CR4_LA57 | X86_CR4_TSD)
144 #define KVM_VM_CR4_ALWAYS_ON_UNRESTRICTED_GUEST X86_CR4_VMXE
145 #define KVM_PMODE_VM_CR4_ALWAYS_ON (X86_CR4_PAE | X86_CR4_VMXE)
146 #define KVM_RMODE_VM_CR4_ALWAYS_ON (X86_CR4_VME | X86_CR4_PAE | X86_CR4_VMXE)
148 #define RMODE_GUEST_OWNED_EFLAGS_BITS (~(X86_EFLAGS_IOPL | X86_EFLAGS_VM))
150 #define VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE 5
153 * Hyper-V requires all of these, so mark them as supported even though
154 * they are just treated the same as all-context.
156 #define VMX_VPID_EXTENT_SUPPORTED_MASK \
157 (VMX_VPID_EXTENT_INDIVIDUAL_ADDR_BIT | \
158 VMX_VPID_EXTENT_SINGLE_CONTEXT_BIT | \
159 VMX_VPID_EXTENT_GLOBAL_CONTEXT_BIT | \
160 VMX_VPID_EXTENT_SINGLE_NON_GLOBAL_BIT)
163 * These 2 parameters are used to config the controls for Pause-Loop Exiting:
164 * ple_gap: upper bound on the amount of time between two successive
165 * executions of PAUSE in a loop. Also indicate if ple enabled.
166 * According to test, this time is usually smaller than 128 cycles.
167 * ple_window: upper bound on the amount of time a guest is allowed to execute
168 * in a PAUSE loop. Tests indicate that most spinlocks are held for
169 * less than 2^12 cycles
170 * Time is measured based on a counter that runs at the same rate as the TSC,
171 * refer SDM volume 3b section 21.6.13 & 22.1.3.
173 static unsigned int ple_gap = KVM_DEFAULT_PLE_GAP;
174 module_param(ple_gap, uint, 0444);
176 static unsigned int ple_window = KVM_VMX_DEFAULT_PLE_WINDOW;
177 module_param(ple_window, uint, 0444);
179 /* Default doubles per-vcpu window every exit. */
180 static unsigned int ple_window_grow = KVM_DEFAULT_PLE_WINDOW_GROW;
181 module_param(ple_window_grow, uint, 0444);
183 /* Default resets per-vcpu window every exit to ple_window. */
184 static unsigned int ple_window_shrink = KVM_DEFAULT_PLE_WINDOW_SHRINK;
185 module_param(ple_window_shrink, uint, 0444);
187 /* Default is to compute the maximum so we can never overflow. */
188 static unsigned int ple_window_max = KVM_VMX_DEFAULT_PLE_WINDOW_MAX;
189 module_param(ple_window_max, uint, 0444);
191 extern const ulong vmx_return;
193 static DEFINE_STATIC_KEY_FALSE(vmx_l1d_should_flush);
194 static DEFINE_STATIC_KEY_FALSE(vmx_l1d_flush_cond);
195 static DEFINE_MUTEX(vmx_l1d_flush_mutex);
197 /* Storage for pre module init parameter parsing */
198 static enum vmx_l1d_flush_state __read_mostly vmentry_l1d_flush_param = VMENTER_L1D_FLUSH_AUTO;
200 static const struct {
203 } vmentry_l1d_param[] = {
204 [VMENTER_L1D_FLUSH_AUTO] = {"auto", true},
205 [VMENTER_L1D_FLUSH_NEVER] = {"never", true},
206 [VMENTER_L1D_FLUSH_COND] = {"cond", true},
207 [VMENTER_L1D_FLUSH_ALWAYS] = {"always", true},
208 [VMENTER_L1D_FLUSH_EPT_DISABLED] = {"EPT disabled", false},
209 [VMENTER_L1D_FLUSH_NOT_REQUIRED] = {"not required", false},
212 #define L1D_CACHE_ORDER 4
213 static void *vmx_l1d_flush_pages;
215 /* Control for disabling CPU Fill buffer clear */
216 static bool __read_mostly vmx_fb_clear_ctrl_available;
218 static int vmx_setup_l1d_flush(enum vmx_l1d_flush_state l1tf)
224 l1tf_vmx_mitigation = VMENTER_L1D_FLUSH_EPT_DISABLED;
228 if (boot_cpu_has(X86_FEATURE_ARCH_CAPABILITIES)) {
231 rdmsrl(MSR_IA32_ARCH_CAPABILITIES, msr);
232 if (msr & ARCH_CAP_SKIP_VMENTRY_L1DFLUSH) {
233 l1tf_vmx_mitigation = VMENTER_L1D_FLUSH_NOT_REQUIRED;
238 /* If set to auto use the default l1tf mitigation method */
239 if (l1tf == VMENTER_L1D_FLUSH_AUTO) {
240 switch (l1tf_mitigation) {
241 case L1TF_MITIGATION_OFF:
242 l1tf = VMENTER_L1D_FLUSH_NEVER;
244 case L1TF_MITIGATION_FLUSH_NOWARN:
245 case L1TF_MITIGATION_FLUSH:
246 case L1TF_MITIGATION_FLUSH_NOSMT:
247 l1tf = VMENTER_L1D_FLUSH_COND;
249 case L1TF_MITIGATION_FULL:
250 case L1TF_MITIGATION_FULL_FORCE:
251 l1tf = VMENTER_L1D_FLUSH_ALWAYS;
254 } else if (l1tf_mitigation == L1TF_MITIGATION_FULL_FORCE) {
255 l1tf = VMENTER_L1D_FLUSH_ALWAYS;
258 if (l1tf != VMENTER_L1D_FLUSH_NEVER && !vmx_l1d_flush_pages &&
259 !boot_cpu_has(X86_FEATURE_FLUSH_L1D)) {
260 page = alloc_pages(GFP_KERNEL, L1D_CACHE_ORDER);
263 vmx_l1d_flush_pages = page_address(page);
266 * Initialize each page with a different pattern in
267 * order to protect against KSM in the nested
268 * virtualization case.
270 for (i = 0; i < 1u << L1D_CACHE_ORDER; ++i) {
271 memset(vmx_l1d_flush_pages + i * PAGE_SIZE, i + 1,
276 l1tf_vmx_mitigation = l1tf;
278 if (l1tf != VMENTER_L1D_FLUSH_NEVER)
279 static_branch_enable(&vmx_l1d_should_flush);
281 static_branch_disable(&vmx_l1d_should_flush);
283 if (l1tf == VMENTER_L1D_FLUSH_COND)
284 static_branch_enable(&vmx_l1d_flush_cond);
286 static_branch_disable(&vmx_l1d_flush_cond);
290 static int vmentry_l1d_flush_parse(const char *s)
295 for (i = 0; i < ARRAY_SIZE(vmentry_l1d_param); i++) {
296 if (vmentry_l1d_param[i].for_parse &&
297 sysfs_streq(s, vmentry_l1d_param[i].option))
304 static int vmentry_l1d_flush_set(const char *s, const struct kernel_param *kp)
308 l1tf = vmentry_l1d_flush_parse(s);
312 if (!boot_cpu_has(X86_BUG_L1TF))
316 * Has vmx_init() run already? If not then this is the pre init
317 * parameter parsing. In that case just store the value and let
318 * vmx_init() do the proper setup after enable_ept has been
321 if (l1tf_vmx_mitigation == VMENTER_L1D_FLUSH_AUTO) {
322 vmentry_l1d_flush_param = l1tf;
326 mutex_lock(&vmx_l1d_flush_mutex);
327 ret = vmx_setup_l1d_flush(l1tf);
328 mutex_unlock(&vmx_l1d_flush_mutex);
332 static int vmentry_l1d_flush_get(char *s, const struct kernel_param *kp)
334 if (WARN_ON_ONCE(l1tf_vmx_mitigation >= ARRAY_SIZE(vmentry_l1d_param)))
335 return sprintf(s, "???\n");
337 return sprintf(s, "%s\n", vmentry_l1d_param[l1tf_vmx_mitigation].option);
340 static const struct kernel_param_ops vmentry_l1d_flush_ops = {
341 .set = vmentry_l1d_flush_set,
342 .get = vmentry_l1d_flush_get,
344 module_param_cb(vmentry_l1d_flush, &vmentry_l1d_flush_ops, NULL, 0644);
346 enum ept_pointers_status {
347 EPT_POINTERS_CHECK = 0,
348 EPT_POINTERS_MATCH = 1,
349 EPT_POINTERS_MISMATCH = 2
355 unsigned int tss_addr;
356 bool ept_identity_pagetable_done;
357 gpa_t ept_identity_map_addr;
359 enum ept_pointers_status ept_pointers_match;
360 spinlock_t ept_pointer_lock;
363 #define NR_AUTOLOAD_MSRS 8
377 * vmcs_host_state tracks registers that are loaded from the VMCS on VMEXIT
378 * and whose values change infrequently, but are not constant. I.e. this is
379 * used as a write-through cache of the corresponding VMCS fields.
381 struct vmcs_host_state {
382 unsigned long cr3; /* May not match real cr3 */
383 unsigned long cr4; /* May not match real cr4 */
384 unsigned long gs_base;
385 unsigned long fs_base;
387 u16 fs_sel, gs_sel, ldt_sel;
394 * Track a VMCS that may be loaded on a certain CPU. If it is (cpu!=-1), also
395 * remember whether it was VMLAUNCHed, and maintain a linked list of all VMCSs
396 * loaded on this CPU (so we can clear them if the CPU goes down).
400 struct vmcs *shadow_vmcs;
403 bool nmi_known_unmasked;
405 /* Support for vnmi-less CPUs */
406 int soft_vnmi_blocked;
408 s64 vnmi_blocked_time;
409 unsigned long *msr_bitmap;
410 struct list_head loaded_vmcss_on_cpu_link;
411 struct vmcs_host_state host_state;
414 struct shared_msr_entry {
421 * struct vmcs12 describes the state that our guest hypervisor (L1) keeps for a
422 * single nested guest (L2), hence the name vmcs12. Any VMX implementation has
423 * a VMCS structure, and vmcs12 is our emulated VMX's VMCS. This structure is
424 * stored in guest memory specified by VMPTRLD, but is opaque to the guest,
425 * which must access it using VMREAD/VMWRITE/VMCLEAR instructions.
426 * More than one of these structures may exist, if L1 runs multiple L2 guests.
427 * nested_vmx_run() will use the data here to build the vmcs02: a VMCS for the
428 * underlying hardware which will be used to run L2.
429 * This structure is packed to ensure that its layout is identical across
430 * machines (necessary for live migration).
432 * IMPORTANT: Changing the layout of existing fields in this structure
433 * will break save/restore compatibility with older kvm releases. When
434 * adding new fields, either use space in the reserved padding* arrays
435 * or add the new fields to the end of the structure.
437 typedef u64 natural_width;
438 struct __packed vmcs12 {
439 /* According to the Intel spec, a VMCS region must start with the
440 * following two fields. Then follow implementation-specific data.
445 u32 launch_state; /* set to 0 by VMCLEAR, to 1 by VMLAUNCH */
446 u32 padding[7]; /* room for future expansion */
451 u64 vm_exit_msr_store_addr;
452 u64 vm_exit_msr_load_addr;
453 u64 vm_entry_msr_load_addr;
455 u64 virtual_apic_page_addr;
456 u64 apic_access_addr;
457 u64 posted_intr_desc_addr;
459 u64 eoi_exit_bitmap0;
460 u64 eoi_exit_bitmap1;
461 u64 eoi_exit_bitmap2;
462 u64 eoi_exit_bitmap3;
464 u64 guest_physical_address;
465 u64 vmcs_link_pointer;
466 u64 guest_ia32_debugctl;
469 u64 guest_ia32_perf_global_ctrl;
477 u64 host_ia32_perf_global_ctrl;
480 u64 vm_function_control;
481 u64 eptp_list_address;
483 u64 padding64[3]; /* room for future expansion */
485 * To allow migration of L1 (complete with its L2 guests) between
486 * machines of different natural widths (32 or 64 bit), we cannot have
487 * unsigned long fields with no explict size. We use u64 (aliased
488 * natural_width) instead. Luckily, x86 is little-endian.
490 natural_width cr0_guest_host_mask;
491 natural_width cr4_guest_host_mask;
492 natural_width cr0_read_shadow;
493 natural_width cr4_read_shadow;
494 natural_width cr3_target_value0;
495 natural_width cr3_target_value1;
496 natural_width cr3_target_value2;
497 natural_width cr3_target_value3;
498 natural_width exit_qualification;
499 natural_width guest_linear_address;
500 natural_width guest_cr0;
501 natural_width guest_cr3;
502 natural_width guest_cr4;
503 natural_width guest_es_base;
504 natural_width guest_cs_base;
505 natural_width guest_ss_base;
506 natural_width guest_ds_base;
507 natural_width guest_fs_base;
508 natural_width guest_gs_base;
509 natural_width guest_ldtr_base;
510 natural_width guest_tr_base;
511 natural_width guest_gdtr_base;
512 natural_width guest_idtr_base;
513 natural_width guest_dr7;
514 natural_width guest_rsp;
515 natural_width guest_rip;
516 natural_width guest_rflags;
517 natural_width guest_pending_dbg_exceptions;
518 natural_width guest_sysenter_esp;
519 natural_width guest_sysenter_eip;
520 natural_width host_cr0;
521 natural_width host_cr3;
522 natural_width host_cr4;
523 natural_width host_fs_base;
524 natural_width host_gs_base;
525 natural_width host_tr_base;
526 natural_width host_gdtr_base;
527 natural_width host_idtr_base;
528 natural_width host_ia32_sysenter_esp;
529 natural_width host_ia32_sysenter_eip;
530 natural_width host_rsp;
531 natural_width host_rip;
532 natural_width paddingl[8]; /* room for future expansion */
533 u32 pin_based_vm_exec_control;
534 u32 cpu_based_vm_exec_control;
535 u32 exception_bitmap;
536 u32 page_fault_error_code_mask;
537 u32 page_fault_error_code_match;
538 u32 cr3_target_count;
539 u32 vm_exit_controls;
540 u32 vm_exit_msr_store_count;
541 u32 vm_exit_msr_load_count;
542 u32 vm_entry_controls;
543 u32 vm_entry_msr_load_count;
544 u32 vm_entry_intr_info_field;
545 u32 vm_entry_exception_error_code;
546 u32 vm_entry_instruction_len;
548 u32 secondary_vm_exec_control;
549 u32 vm_instruction_error;
551 u32 vm_exit_intr_info;
552 u32 vm_exit_intr_error_code;
553 u32 idt_vectoring_info_field;
554 u32 idt_vectoring_error_code;
555 u32 vm_exit_instruction_len;
556 u32 vmx_instruction_info;
563 u32 guest_ldtr_limit;
565 u32 guest_gdtr_limit;
566 u32 guest_idtr_limit;
567 u32 guest_es_ar_bytes;
568 u32 guest_cs_ar_bytes;
569 u32 guest_ss_ar_bytes;
570 u32 guest_ds_ar_bytes;
571 u32 guest_fs_ar_bytes;
572 u32 guest_gs_ar_bytes;
573 u32 guest_ldtr_ar_bytes;
574 u32 guest_tr_ar_bytes;
575 u32 guest_interruptibility_info;
576 u32 guest_activity_state;
577 u32 guest_sysenter_cs;
578 u32 host_ia32_sysenter_cs;
579 u32 vmx_preemption_timer_value;
580 u32 padding32[7]; /* room for future expansion */
581 u16 virtual_processor_id;
583 u16 guest_es_selector;
584 u16 guest_cs_selector;
585 u16 guest_ss_selector;
586 u16 guest_ds_selector;
587 u16 guest_fs_selector;
588 u16 guest_gs_selector;
589 u16 guest_ldtr_selector;
590 u16 guest_tr_selector;
591 u16 guest_intr_status;
592 u16 host_es_selector;
593 u16 host_cs_selector;
594 u16 host_ss_selector;
595 u16 host_ds_selector;
596 u16 host_fs_selector;
597 u16 host_gs_selector;
598 u16 host_tr_selector;
603 * For save/restore compatibility, the vmcs12 field offsets must not change.
605 #define CHECK_OFFSET(field, loc) \
606 BUILD_BUG_ON_MSG(offsetof(struct vmcs12, field) != (loc), \
607 "Offset of " #field " in struct vmcs12 has changed.")
609 static inline void vmx_check_vmcs12_offsets(void) {
610 CHECK_OFFSET(hdr, 0);
611 CHECK_OFFSET(abort, 4);
612 CHECK_OFFSET(launch_state, 8);
613 CHECK_OFFSET(io_bitmap_a, 40);
614 CHECK_OFFSET(io_bitmap_b, 48);
615 CHECK_OFFSET(msr_bitmap, 56);
616 CHECK_OFFSET(vm_exit_msr_store_addr, 64);
617 CHECK_OFFSET(vm_exit_msr_load_addr, 72);
618 CHECK_OFFSET(vm_entry_msr_load_addr, 80);
619 CHECK_OFFSET(tsc_offset, 88);
620 CHECK_OFFSET(virtual_apic_page_addr, 96);
621 CHECK_OFFSET(apic_access_addr, 104);
622 CHECK_OFFSET(posted_intr_desc_addr, 112);
623 CHECK_OFFSET(ept_pointer, 120);
624 CHECK_OFFSET(eoi_exit_bitmap0, 128);
625 CHECK_OFFSET(eoi_exit_bitmap1, 136);
626 CHECK_OFFSET(eoi_exit_bitmap2, 144);
627 CHECK_OFFSET(eoi_exit_bitmap3, 152);
628 CHECK_OFFSET(xss_exit_bitmap, 160);
629 CHECK_OFFSET(guest_physical_address, 168);
630 CHECK_OFFSET(vmcs_link_pointer, 176);
631 CHECK_OFFSET(guest_ia32_debugctl, 184);
632 CHECK_OFFSET(guest_ia32_pat, 192);
633 CHECK_OFFSET(guest_ia32_efer, 200);
634 CHECK_OFFSET(guest_ia32_perf_global_ctrl, 208);
635 CHECK_OFFSET(guest_pdptr0, 216);
636 CHECK_OFFSET(guest_pdptr1, 224);
637 CHECK_OFFSET(guest_pdptr2, 232);
638 CHECK_OFFSET(guest_pdptr3, 240);
639 CHECK_OFFSET(guest_bndcfgs, 248);
640 CHECK_OFFSET(host_ia32_pat, 256);
641 CHECK_OFFSET(host_ia32_efer, 264);
642 CHECK_OFFSET(host_ia32_perf_global_ctrl, 272);
643 CHECK_OFFSET(vmread_bitmap, 280);
644 CHECK_OFFSET(vmwrite_bitmap, 288);
645 CHECK_OFFSET(vm_function_control, 296);
646 CHECK_OFFSET(eptp_list_address, 304);
647 CHECK_OFFSET(pml_address, 312);
648 CHECK_OFFSET(cr0_guest_host_mask, 344);
649 CHECK_OFFSET(cr4_guest_host_mask, 352);
650 CHECK_OFFSET(cr0_read_shadow, 360);
651 CHECK_OFFSET(cr4_read_shadow, 368);
652 CHECK_OFFSET(cr3_target_value0, 376);
653 CHECK_OFFSET(cr3_target_value1, 384);
654 CHECK_OFFSET(cr3_target_value2, 392);
655 CHECK_OFFSET(cr3_target_value3, 400);
656 CHECK_OFFSET(exit_qualification, 408);
657 CHECK_OFFSET(guest_linear_address, 416);
658 CHECK_OFFSET(guest_cr0, 424);
659 CHECK_OFFSET(guest_cr3, 432);
660 CHECK_OFFSET(guest_cr4, 440);
661 CHECK_OFFSET(guest_es_base, 448);
662 CHECK_OFFSET(guest_cs_base, 456);
663 CHECK_OFFSET(guest_ss_base, 464);
664 CHECK_OFFSET(guest_ds_base, 472);
665 CHECK_OFFSET(guest_fs_base, 480);
666 CHECK_OFFSET(guest_gs_base, 488);
667 CHECK_OFFSET(guest_ldtr_base, 496);
668 CHECK_OFFSET(guest_tr_base, 504);
669 CHECK_OFFSET(guest_gdtr_base, 512);
670 CHECK_OFFSET(guest_idtr_base, 520);
671 CHECK_OFFSET(guest_dr7, 528);
672 CHECK_OFFSET(guest_rsp, 536);
673 CHECK_OFFSET(guest_rip, 544);
674 CHECK_OFFSET(guest_rflags, 552);
675 CHECK_OFFSET(guest_pending_dbg_exceptions, 560);
676 CHECK_OFFSET(guest_sysenter_esp, 568);
677 CHECK_OFFSET(guest_sysenter_eip, 576);
678 CHECK_OFFSET(host_cr0, 584);
679 CHECK_OFFSET(host_cr3, 592);
680 CHECK_OFFSET(host_cr4, 600);
681 CHECK_OFFSET(host_fs_base, 608);
682 CHECK_OFFSET(host_gs_base, 616);
683 CHECK_OFFSET(host_tr_base, 624);
684 CHECK_OFFSET(host_gdtr_base, 632);
685 CHECK_OFFSET(host_idtr_base, 640);
686 CHECK_OFFSET(host_ia32_sysenter_esp, 648);
687 CHECK_OFFSET(host_ia32_sysenter_eip, 656);
688 CHECK_OFFSET(host_rsp, 664);
689 CHECK_OFFSET(host_rip, 672);
690 CHECK_OFFSET(pin_based_vm_exec_control, 744);
691 CHECK_OFFSET(cpu_based_vm_exec_control, 748);
692 CHECK_OFFSET(exception_bitmap, 752);
693 CHECK_OFFSET(page_fault_error_code_mask, 756);
694 CHECK_OFFSET(page_fault_error_code_match, 760);
695 CHECK_OFFSET(cr3_target_count, 764);
696 CHECK_OFFSET(vm_exit_controls, 768);
697 CHECK_OFFSET(vm_exit_msr_store_count, 772);
698 CHECK_OFFSET(vm_exit_msr_load_count, 776);
699 CHECK_OFFSET(vm_entry_controls, 780);
700 CHECK_OFFSET(vm_entry_msr_load_count, 784);
701 CHECK_OFFSET(vm_entry_intr_info_field, 788);
702 CHECK_OFFSET(vm_entry_exception_error_code, 792);
703 CHECK_OFFSET(vm_entry_instruction_len, 796);
704 CHECK_OFFSET(tpr_threshold, 800);
705 CHECK_OFFSET(secondary_vm_exec_control, 804);
706 CHECK_OFFSET(vm_instruction_error, 808);
707 CHECK_OFFSET(vm_exit_reason, 812);
708 CHECK_OFFSET(vm_exit_intr_info, 816);
709 CHECK_OFFSET(vm_exit_intr_error_code, 820);
710 CHECK_OFFSET(idt_vectoring_info_field, 824);
711 CHECK_OFFSET(idt_vectoring_error_code, 828);
712 CHECK_OFFSET(vm_exit_instruction_len, 832);
713 CHECK_OFFSET(vmx_instruction_info, 836);
714 CHECK_OFFSET(guest_es_limit, 840);
715 CHECK_OFFSET(guest_cs_limit, 844);
716 CHECK_OFFSET(guest_ss_limit, 848);
717 CHECK_OFFSET(guest_ds_limit, 852);
718 CHECK_OFFSET(guest_fs_limit, 856);
719 CHECK_OFFSET(guest_gs_limit, 860);
720 CHECK_OFFSET(guest_ldtr_limit, 864);
721 CHECK_OFFSET(guest_tr_limit, 868);
722 CHECK_OFFSET(guest_gdtr_limit, 872);
723 CHECK_OFFSET(guest_idtr_limit, 876);
724 CHECK_OFFSET(guest_es_ar_bytes, 880);
725 CHECK_OFFSET(guest_cs_ar_bytes, 884);
726 CHECK_OFFSET(guest_ss_ar_bytes, 888);
727 CHECK_OFFSET(guest_ds_ar_bytes, 892);
728 CHECK_OFFSET(guest_fs_ar_bytes, 896);
729 CHECK_OFFSET(guest_gs_ar_bytes, 900);
730 CHECK_OFFSET(guest_ldtr_ar_bytes, 904);
731 CHECK_OFFSET(guest_tr_ar_bytes, 908);
732 CHECK_OFFSET(guest_interruptibility_info, 912);
733 CHECK_OFFSET(guest_activity_state, 916);
734 CHECK_OFFSET(guest_sysenter_cs, 920);
735 CHECK_OFFSET(host_ia32_sysenter_cs, 924);
736 CHECK_OFFSET(vmx_preemption_timer_value, 928);
737 CHECK_OFFSET(virtual_processor_id, 960);
738 CHECK_OFFSET(posted_intr_nv, 962);
739 CHECK_OFFSET(guest_es_selector, 964);
740 CHECK_OFFSET(guest_cs_selector, 966);
741 CHECK_OFFSET(guest_ss_selector, 968);
742 CHECK_OFFSET(guest_ds_selector, 970);
743 CHECK_OFFSET(guest_fs_selector, 972);
744 CHECK_OFFSET(guest_gs_selector, 974);
745 CHECK_OFFSET(guest_ldtr_selector, 976);
746 CHECK_OFFSET(guest_tr_selector, 978);
747 CHECK_OFFSET(guest_intr_status, 980);
748 CHECK_OFFSET(host_es_selector, 982);
749 CHECK_OFFSET(host_cs_selector, 984);
750 CHECK_OFFSET(host_ss_selector, 986);
751 CHECK_OFFSET(host_ds_selector, 988);
752 CHECK_OFFSET(host_fs_selector, 990);
753 CHECK_OFFSET(host_gs_selector, 992);
754 CHECK_OFFSET(host_tr_selector, 994);
755 CHECK_OFFSET(guest_pml_index, 996);
759 * VMCS12_REVISION is an arbitrary id that should be changed if the content or
760 * layout of struct vmcs12 is changed. MSR_IA32_VMX_BASIC returns this id, and
761 * VMPTRLD verifies that the VMCS region that L1 is loading contains this id.
763 * IMPORTANT: Changing this value will break save/restore compatibility with
764 * older kvm releases.
766 #define VMCS12_REVISION 0x11e57ed0
769 * VMCS12_SIZE is the number of bytes L1 should allocate for the VMXON region
770 * and any VMCS region. Although only sizeof(struct vmcs12) are used by the
771 * current implementation, 4K are reserved to avoid future complications.
773 #define VMCS12_SIZE 0x1000
776 * VMCS12_MAX_FIELD_INDEX is the highest index value used in any
777 * supported VMCS12 field encoding.
779 #define VMCS12_MAX_FIELD_INDEX 0x17
781 struct nested_vmx_msrs {
783 * We only store the "true" versions of the VMX capability MSRs. We
784 * generate the "non-true" versions by setting the must-be-1 bits
785 * according to the SDM.
787 u32 procbased_ctls_low;
788 u32 procbased_ctls_high;
789 u32 secondary_ctls_low;
790 u32 secondary_ctls_high;
791 u32 pinbased_ctls_low;
792 u32 pinbased_ctls_high;
811 * The nested_vmx structure is part of vcpu_vmx, and holds information we need
812 * for correct emulation of VMX (i.e., nested VMX) on this vcpu.
815 /* Has the level1 guest done vmxon? */
820 /* The guest-physical address of the current VMCS L1 keeps for L2 */
823 * Cache of the guest's VMCS, existing outside of guest memory.
824 * Loaded from guest memory during VMPTRLD. Flushed to guest
825 * memory during VMCLEAR and VMPTRLD.
827 struct vmcs12 *cached_vmcs12;
829 * Cache of the guest's shadow VMCS, existing outside of guest
830 * memory. Loaded from guest memory during VM entry. Flushed
831 * to guest memory during VM exit.
833 struct vmcs12 *cached_shadow_vmcs12;
835 * Indicates if the shadow vmcs must be updated with the
836 * data hold by vmcs12
838 bool sync_shadow_vmcs;
841 bool change_vmcs01_virtual_apic_mode;
843 /* L2 must run next, and mustn't decide to exit to L1. */
844 bool nested_run_pending;
846 struct loaded_vmcs vmcs02;
849 * Guest pages referred to in the vmcs02 with host-physical
850 * pointers, so we must keep them pinned while L2 runs.
852 struct page *apic_access_page;
853 struct page *virtual_apic_page;
854 struct page *pi_desc_page;
855 struct pi_desc *pi_desc;
859 struct hrtimer preemption_timer;
860 bool preemption_timer_expired;
862 /* to migrate it to L2 if VM_ENTRY_LOAD_DEBUG_CONTROLS is off */
864 u64 vmcs01_guest_bndcfgs;
869 struct nested_vmx_msrs msrs;
871 /* SMM related state */
873 /* in VMX operation on SMM entry? */
875 /* in guest mode on SMM entry? */
880 #define POSTED_INTR_ON 0
881 #define POSTED_INTR_SN 1
883 /* Posted-Interrupt Descriptor */
885 u32 pir[8]; /* Posted interrupt requested */
888 /* bit 256 - Outstanding Notification */
890 /* bit 257 - Suppress Notification */
892 /* bit 271:258 - Reserved */
894 /* bit 279:272 - Notification Vector */
896 /* bit 287:280 - Reserved */
898 /* bit 319:288 - Notification Destination */
906 static bool pi_test_and_set_on(struct pi_desc *pi_desc)
908 return test_and_set_bit(POSTED_INTR_ON,
909 (unsigned long *)&pi_desc->control);
912 static bool pi_test_and_clear_on(struct pi_desc *pi_desc)
914 return test_and_clear_bit(POSTED_INTR_ON,
915 (unsigned long *)&pi_desc->control);
918 static int pi_test_and_set_pir(int vector, struct pi_desc *pi_desc)
920 return test_and_set_bit(vector, (unsigned long *)pi_desc->pir);
923 static inline void pi_clear_sn(struct pi_desc *pi_desc)
925 return clear_bit(POSTED_INTR_SN,
926 (unsigned long *)&pi_desc->control);
929 static inline void pi_set_sn(struct pi_desc *pi_desc)
931 return set_bit(POSTED_INTR_SN,
932 (unsigned long *)&pi_desc->control);
935 static inline void pi_clear_on(struct pi_desc *pi_desc)
937 clear_bit(POSTED_INTR_ON,
938 (unsigned long *)&pi_desc->control);
941 static inline int pi_test_on(struct pi_desc *pi_desc)
943 return test_bit(POSTED_INTR_ON,
944 (unsigned long *)&pi_desc->control);
947 static inline int pi_test_sn(struct pi_desc *pi_desc)
949 return test_bit(POSTED_INTR_SN,
950 (unsigned long *)&pi_desc->control);
955 struct vmx_msr_entry val[NR_AUTOLOAD_MSRS];
959 struct kvm_vcpu vcpu;
960 unsigned long host_rsp;
964 u32 idt_vectoring_info;
966 struct shared_msr_entry *guest_msrs;
969 bool guest_msrs_dirty;
970 unsigned long host_idt_base;
972 u64 msr_host_kernel_gs_base;
973 u64 msr_guest_kernel_gs_base;
978 u32 vm_entry_controls_shadow;
979 u32 vm_exit_controls_shadow;
980 u32 secondary_exec_control;
983 * loaded_vmcs points to the VMCS currently used in this vcpu. For a
984 * non-nested (L1) guest, it always points to vmcs01. For a nested
985 * guest (L2), it points to a different VMCS. loaded_cpu_state points
986 * to the VMCS whose state is loaded into the CPU registers that only
987 * need to be switched when transitioning to/from the kernel; a NULL
988 * value indicates that host state is loaded.
990 struct loaded_vmcs vmcs01;
991 struct loaded_vmcs *loaded_vmcs;
992 struct loaded_vmcs *loaded_cpu_state;
993 bool __launched; /* temporary, used in vmx_vcpu_run */
994 struct msr_autoload {
995 struct vmx_msrs guest;
996 struct vmx_msrs host;
1002 struct kvm_segment segs[8];
1005 u32 bitmask; /* 4 bits per segment (1 bit per field) */
1006 struct kvm_save_segment {
1014 bool emulation_required;
1018 /* Posted interrupt descriptor */
1019 struct pi_desc pi_desc;
1021 /* Support for a guest hypervisor (nested VMX) */
1022 struct nested_vmx nested;
1024 /* Dynamic PLE window. */
1026 bool ple_window_dirty;
1028 bool req_immediate_exit;
1030 /* Support for PML */
1031 #define PML_ENTITY_NUM 512
1032 struct page *pml_pg;
1034 /* apic deadline value in host tsc */
1035 u64 hv_deadline_tsc;
1037 u64 current_tsc_ratio;
1041 unsigned long host_debugctlmsr;
1044 * Only bits masked by msr_ia32_feature_control_valid_bits can be set in
1045 * msr_ia32_feature_control. FEATURE_CONTROL_LOCKED is always included
1046 * in msr_ia32_feature_control_valid_bits.
1048 u64 msr_ia32_feature_control;
1049 u64 msr_ia32_feature_control_valid_bits;
1051 u64 msr_ia32_mcu_opt_ctrl;
1052 bool disable_fb_clear;
1055 enum segment_cache_field {
1058 SEG_FIELD_LIMIT = 2,
1064 static inline struct kvm_vmx *to_kvm_vmx(struct kvm *kvm)
1066 return container_of(kvm, struct kvm_vmx, kvm);
1069 static inline struct vcpu_vmx *to_vmx(struct kvm_vcpu *vcpu)
1071 return container_of(vcpu, struct vcpu_vmx, vcpu);
1074 static struct pi_desc *vcpu_to_pi_desc(struct kvm_vcpu *vcpu)
1076 return &(to_vmx(vcpu)->pi_desc);
1079 #define ROL16(val, n) ((u16)(((u16)(val) << (n)) | ((u16)(val) >> (16 - (n)))))
1080 #define VMCS12_OFFSET(x) offsetof(struct vmcs12, x)
1081 #define FIELD(number, name) [ROL16(number, 6)] = VMCS12_OFFSET(name)
1082 #define FIELD64(number, name) \
1083 FIELD(number, name), \
1084 [ROL16(number##_HIGH, 6)] = VMCS12_OFFSET(name) + sizeof(u32)
1087 static u16 shadow_read_only_fields[] = {
1088 #define SHADOW_FIELD_RO(x) x,
1089 #include "vmx_shadow_fields.h"
1091 static int max_shadow_read_only_fields =
1092 ARRAY_SIZE(shadow_read_only_fields);
1094 static u16 shadow_read_write_fields[] = {
1095 #define SHADOW_FIELD_RW(x) x,
1096 #include "vmx_shadow_fields.h"
1098 static int max_shadow_read_write_fields =
1099 ARRAY_SIZE(shadow_read_write_fields);
1101 static const unsigned short vmcs_field_to_offset_table[] = {
1102 FIELD(VIRTUAL_PROCESSOR_ID, virtual_processor_id),
1103 FIELD(POSTED_INTR_NV, posted_intr_nv),
1104 FIELD(GUEST_ES_SELECTOR, guest_es_selector),
1105 FIELD(GUEST_CS_SELECTOR, guest_cs_selector),
1106 FIELD(GUEST_SS_SELECTOR, guest_ss_selector),
1107 FIELD(GUEST_DS_SELECTOR, guest_ds_selector),
1108 FIELD(GUEST_FS_SELECTOR, guest_fs_selector),
1109 FIELD(GUEST_GS_SELECTOR, guest_gs_selector),
1110 FIELD(GUEST_LDTR_SELECTOR, guest_ldtr_selector),
1111 FIELD(GUEST_TR_SELECTOR, guest_tr_selector),
1112 FIELD(GUEST_INTR_STATUS, guest_intr_status),
1113 FIELD(GUEST_PML_INDEX, guest_pml_index),
1114 FIELD(HOST_ES_SELECTOR, host_es_selector),
1115 FIELD(HOST_CS_SELECTOR, host_cs_selector),
1116 FIELD(HOST_SS_SELECTOR, host_ss_selector),
1117 FIELD(HOST_DS_SELECTOR, host_ds_selector),
1118 FIELD(HOST_FS_SELECTOR, host_fs_selector),
1119 FIELD(HOST_GS_SELECTOR, host_gs_selector),
1120 FIELD(HOST_TR_SELECTOR, host_tr_selector),
1121 FIELD64(IO_BITMAP_A, io_bitmap_a),
1122 FIELD64(IO_BITMAP_B, io_bitmap_b),
1123 FIELD64(MSR_BITMAP, msr_bitmap),
1124 FIELD64(VM_EXIT_MSR_STORE_ADDR, vm_exit_msr_store_addr),
1125 FIELD64(VM_EXIT_MSR_LOAD_ADDR, vm_exit_msr_load_addr),
1126 FIELD64(VM_ENTRY_MSR_LOAD_ADDR, vm_entry_msr_load_addr),
1127 FIELD64(PML_ADDRESS, pml_address),
1128 FIELD64(TSC_OFFSET, tsc_offset),
1129 FIELD64(VIRTUAL_APIC_PAGE_ADDR, virtual_apic_page_addr),
1130 FIELD64(APIC_ACCESS_ADDR, apic_access_addr),
1131 FIELD64(POSTED_INTR_DESC_ADDR, posted_intr_desc_addr),
1132 FIELD64(VM_FUNCTION_CONTROL, vm_function_control),
1133 FIELD64(EPT_POINTER, ept_pointer),
1134 FIELD64(EOI_EXIT_BITMAP0, eoi_exit_bitmap0),
1135 FIELD64(EOI_EXIT_BITMAP1, eoi_exit_bitmap1),
1136 FIELD64(EOI_EXIT_BITMAP2, eoi_exit_bitmap2),
1137 FIELD64(EOI_EXIT_BITMAP3, eoi_exit_bitmap3),
1138 FIELD64(EPTP_LIST_ADDRESS, eptp_list_address),
1139 FIELD64(VMREAD_BITMAP, vmread_bitmap),
1140 FIELD64(VMWRITE_BITMAP, vmwrite_bitmap),
1141 FIELD64(XSS_EXIT_BITMAP, xss_exit_bitmap),
1142 FIELD64(GUEST_PHYSICAL_ADDRESS, guest_physical_address),
1143 FIELD64(VMCS_LINK_POINTER, vmcs_link_pointer),
1144 FIELD64(GUEST_IA32_DEBUGCTL, guest_ia32_debugctl),
1145 FIELD64(GUEST_IA32_PAT, guest_ia32_pat),
1146 FIELD64(GUEST_IA32_EFER, guest_ia32_efer),
1147 FIELD64(GUEST_IA32_PERF_GLOBAL_CTRL, guest_ia32_perf_global_ctrl),
1148 FIELD64(GUEST_PDPTR0, guest_pdptr0),
1149 FIELD64(GUEST_PDPTR1, guest_pdptr1),
1150 FIELD64(GUEST_PDPTR2, guest_pdptr2),
1151 FIELD64(GUEST_PDPTR3, guest_pdptr3),
1152 FIELD64(GUEST_BNDCFGS, guest_bndcfgs),
1153 FIELD64(HOST_IA32_PAT, host_ia32_pat),
1154 FIELD64(HOST_IA32_EFER, host_ia32_efer),
1155 FIELD64(HOST_IA32_PERF_GLOBAL_CTRL, host_ia32_perf_global_ctrl),
1156 FIELD(PIN_BASED_VM_EXEC_CONTROL, pin_based_vm_exec_control),
1157 FIELD(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control),
1158 FIELD(EXCEPTION_BITMAP, exception_bitmap),
1159 FIELD(PAGE_FAULT_ERROR_CODE_MASK, page_fault_error_code_mask),
1160 FIELD(PAGE_FAULT_ERROR_CODE_MATCH, page_fault_error_code_match),
1161 FIELD(CR3_TARGET_COUNT, cr3_target_count),
1162 FIELD(VM_EXIT_CONTROLS, vm_exit_controls),
1163 FIELD(VM_EXIT_MSR_STORE_COUNT, vm_exit_msr_store_count),
1164 FIELD(VM_EXIT_MSR_LOAD_COUNT, vm_exit_msr_load_count),
1165 FIELD(VM_ENTRY_CONTROLS, vm_entry_controls),
1166 FIELD(VM_ENTRY_MSR_LOAD_COUNT, vm_entry_msr_load_count),
1167 FIELD(VM_ENTRY_INTR_INFO_FIELD, vm_entry_intr_info_field),
1168 FIELD(VM_ENTRY_EXCEPTION_ERROR_CODE, vm_entry_exception_error_code),
1169 FIELD(VM_ENTRY_INSTRUCTION_LEN, vm_entry_instruction_len),
1170 FIELD(TPR_THRESHOLD, tpr_threshold),
1171 FIELD(SECONDARY_VM_EXEC_CONTROL, secondary_vm_exec_control),
1172 FIELD(VM_INSTRUCTION_ERROR, vm_instruction_error),
1173 FIELD(VM_EXIT_REASON, vm_exit_reason),
1174 FIELD(VM_EXIT_INTR_INFO, vm_exit_intr_info),
1175 FIELD(VM_EXIT_INTR_ERROR_CODE, vm_exit_intr_error_code),
1176 FIELD(IDT_VECTORING_INFO_FIELD, idt_vectoring_info_field),
1177 FIELD(IDT_VECTORING_ERROR_CODE, idt_vectoring_error_code),
1178 FIELD(VM_EXIT_INSTRUCTION_LEN, vm_exit_instruction_len),
1179 FIELD(VMX_INSTRUCTION_INFO, vmx_instruction_info),
1180 FIELD(GUEST_ES_LIMIT, guest_es_limit),
1181 FIELD(GUEST_CS_LIMIT, guest_cs_limit),
1182 FIELD(GUEST_SS_LIMIT, guest_ss_limit),
1183 FIELD(GUEST_DS_LIMIT, guest_ds_limit),
1184 FIELD(GUEST_FS_LIMIT, guest_fs_limit),
1185 FIELD(GUEST_GS_LIMIT, guest_gs_limit),
1186 FIELD(GUEST_LDTR_LIMIT, guest_ldtr_limit),
1187 FIELD(GUEST_TR_LIMIT, guest_tr_limit),
1188 FIELD(GUEST_GDTR_LIMIT, guest_gdtr_limit),
1189 FIELD(GUEST_IDTR_LIMIT, guest_idtr_limit),
1190 FIELD(GUEST_ES_AR_BYTES, guest_es_ar_bytes),
1191 FIELD(GUEST_CS_AR_BYTES, guest_cs_ar_bytes),
1192 FIELD(GUEST_SS_AR_BYTES, guest_ss_ar_bytes),
1193 FIELD(GUEST_DS_AR_BYTES, guest_ds_ar_bytes),
1194 FIELD(GUEST_FS_AR_BYTES, guest_fs_ar_bytes),
1195 FIELD(GUEST_GS_AR_BYTES, guest_gs_ar_bytes),
1196 FIELD(GUEST_LDTR_AR_BYTES, guest_ldtr_ar_bytes),
1197 FIELD(GUEST_TR_AR_BYTES, guest_tr_ar_bytes),
1198 FIELD(GUEST_INTERRUPTIBILITY_INFO, guest_interruptibility_info),
1199 FIELD(GUEST_ACTIVITY_STATE, guest_activity_state),
1200 FIELD(GUEST_SYSENTER_CS, guest_sysenter_cs),
1201 FIELD(HOST_IA32_SYSENTER_CS, host_ia32_sysenter_cs),
1202 FIELD(VMX_PREEMPTION_TIMER_VALUE, vmx_preemption_timer_value),
1203 FIELD(CR0_GUEST_HOST_MASK, cr0_guest_host_mask),
1204 FIELD(CR4_GUEST_HOST_MASK, cr4_guest_host_mask),
1205 FIELD(CR0_READ_SHADOW, cr0_read_shadow),
1206 FIELD(CR4_READ_SHADOW, cr4_read_shadow),
1207 FIELD(CR3_TARGET_VALUE0, cr3_target_value0),
1208 FIELD(CR3_TARGET_VALUE1, cr3_target_value1),
1209 FIELD(CR3_TARGET_VALUE2, cr3_target_value2),
1210 FIELD(CR3_TARGET_VALUE3, cr3_target_value3),
1211 FIELD(EXIT_QUALIFICATION, exit_qualification),
1212 FIELD(GUEST_LINEAR_ADDRESS, guest_linear_address),
1213 FIELD(GUEST_CR0, guest_cr0),
1214 FIELD(GUEST_CR3, guest_cr3),
1215 FIELD(GUEST_CR4, guest_cr4),
1216 FIELD(GUEST_ES_BASE, guest_es_base),
1217 FIELD(GUEST_CS_BASE, guest_cs_base),
1218 FIELD(GUEST_SS_BASE, guest_ss_base),
1219 FIELD(GUEST_DS_BASE, guest_ds_base),
1220 FIELD(GUEST_FS_BASE, guest_fs_base),
1221 FIELD(GUEST_GS_BASE, guest_gs_base),
1222 FIELD(GUEST_LDTR_BASE, guest_ldtr_base),
1223 FIELD(GUEST_TR_BASE, guest_tr_base),
1224 FIELD(GUEST_GDTR_BASE, guest_gdtr_base),
1225 FIELD(GUEST_IDTR_BASE, guest_idtr_base),
1226 FIELD(GUEST_DR7, guest_dr7),
1227 FIELD(GUEST_RSP, guest_rsp),
1228 FIELD(GUEST_RIP, guest_rip),
1229 FIELD(GUEST_RFLAGS, guest_rflags),
1230 FIELD(GUEST_PENDING_DBG_EXCEPTIONS, guest_pending_dbg_exceptions),
1231 FIELD(GUEST_SYSENTER_ESP, guest_sysenter_esp),
1232 FIELD(GUEST_SYSENTER_EIP, guest_sysenter_eip),
1233 FIELD(HOST_CR0, host_cr0),
1234 FIELD(HOST_CR3, host_cr3),
1235 FIELD(HOST_CR4, host_cr4),
1236 FIELD(HOST_FS_BASE, host_fs_base),
1237 FIELD(HOST_GS_BASE, host_gs_base),
1238 FIELD(HOST_TR_BASE, host_tr_base),
1239 FIELD(HOST_GDTR_BASE, host_gdtr_base),
1240 FIELD(HOST_IDTR_BASE, host_idtr_base),
1241 FIELD(HOST_IA32_SYSENTER_ESP, host_ia32_sysenter_esp),
1242 FIELD(HOST_IA32_SYSENTER_EIP, host_ia32_sysenter_eip),
1243 FIELD(HOST_RSP, host_rsp),
1244 FIELD(HOST_RIP, host_rip),
1247 static inline short vmcs_field_to_offset(unsigned long field)
1249 const size_t size = ARRAY_SIZE(vmcs_field_to_offset_table);
1250 unsigned short offset;
1256 index = ROL16(field, 6);
1260 index = array_index_nospec(index, size);
1261 offset = vmcs_field_to_offset_table[index];
1267 static inline struct vmcs12 *get_vmcs12(struct kvm_vcpu *vcpu)
1269 return to_vmx(vcpu)->nested.cached_vmcs12;
1272 static inline struct vmcs12 *get_shadow_vmcs12(struct kvm_vcpu *vcpu)
1274 return to_vmx(vcpu)->nested.cached_shadow_vmcs12;
1277 static bool nested_ept_ad_enabled(struct kvm_vcpu *vcpu);
1278 static unsigned long nested_ept_get_cr3(struct kvm_vcpu *vcpu);
1279 static u64 construct_eptp(struct kvm_vcpu *vcpu, unsigned long root_hpa);
1280 static bool vmx_xsaves_supported(void);
1281 static void vmx_set_segment(struct kvm_vcpu *vcpu,
1282 struct kvm_segment *var, int seg);
1283 static void vmx_get_segment(struct kvm_vcpu *vcpu,
1284 struct kvm_segment *var, int seg);
1285 static bool guest_state_valid(struct kvm_vcpu *vcpu);
1286 static u32 vmx_segment_access_rights(struct kvm_segment *var);
1287 static void copy_shadow_to_vmcs12(struct vcpu_vmx *vmx);
1288 static bool vmx_get_nmi_mask(struct kvm_vcpu *vcpu);
1289 static void vmx_set_nmi_mask(struct kvm_vcpu *vcpu, bool masked);
1290 static bool nested_vmx_is_page_fault_vmexit(struct vmcs12 *vmcs12,
1292 static void vmx_update_msr_bitmap(struct kvm_vcpu *vcpu);
1293 static __always_inline void vmx_disable_intercept_for_msr(unsigned long *msr_bitmap,
1296 static DEFINE_PER_CPU(struct vmcs *, vmxarea);
1297 static DEFINE_PER_CPU(struct vmcs *, current_vmcs);
1299 * We maintain a per-CPU linked-list of VMCS loaded on that CPU. This is needed
1300 * when a CPU is brought down, and we need to VMCLEAR all VMCSs loaded on it.
1302 static DEFINE_PER_CPU(struct list_head, loaded_vmcss_on_cpu);
1305 * We maintian a per-CPU linked-list of vCPU, so in wakeup_handler() we
1306 * can find which vCPU should be waken up.
1308 static DEFINE_PER_CPU(struct list_head, blocked_vcpu_on_cpu);
1309 static DEFINE_PER_CPU(spinlock_t, blocked_vcpu_on_cpu_lock);
1317 static unsigned long *vmx_bitmap[VMX_BITMAP_NR];
1319 #define vmx_vmread_bitmap (vmx_bitmap[VMX_VMREAD_BITMAP])
1320 #define vmx_vmwrite_bitmap (vmx_bitmap[VMX_VMWRITE_BITMAP])
1322 static bool cpu_has_load_ia32_efer;
1323 static bool cpu_has_load_perf_global_ctrl;
1325 static DECLARE_BITMAP(vmx_vpid_bitmap, VMX_NR_VPIDS);
1326 static DEFINE_SPINLOCK(vmx_vpid_lock);
1328 static struct vmcs_config {
1333 u32 pin_based_exec_ctrl;
1334 u32 cpu_based_exec_ctrl;
1335 u32 cpu_based_2nd_exec_ctrl;
1338 struct nested_vmx_msrs nested;
1341 static struct vmx_capability {
1346 #define VMX_SEGMENT_FIELD(seg) \
1347 [VCPU_SREG_##seg] = { \
1348 .selector = GUEST_##seg##_SELECTOR, \
1349 .base = GUEST_##seg##_BASE, \
1350 .limit = GUEST_##seg##_LIMIT, \
1351 .ar_bytes = GUEST_##seg##_AR_BYTES, \
1354 static const struct kvm_vmx_segment_field {
1359 } kvm_vmx_segment_fields[] = {
1360 VMX_SEGMENT_FIELD(CS),
1361 VMX_SEGMENT_FIELD(DS),
1362 VMX_SEGMENT_FIELD(ES),
1363 VMX_SEGMENT_FIELD(FS),
1364 VMX_SEGMENT_FIELD(GS),
1365 VMX_SEGMENT_FIELD(SS),
1366 VMX_SEGMENT_FIELD(TR),
1367 VMX_SEGMENT_FIELD(LDTR),
1370 static u64 host_efer;
1372 static void ept_save_pdptrs(struct kvm_vcpu *vcpu);
1375 * Keep MSR_STAR at the end, as setup_msrs() will try to optimize it
1376 * away by decrementing the array size.
1378 static const u32 vmx_msr_index[] = {
1379 #ifdef CONFIG_X86_64
1380 MSR_SYSCALL_MASK, MSR_LSTAR, MSR_CSTAR,
1382 MSR_EFER, MSR_TSC_AUX, MSR_STAR,
1385 DEFINE_STATIC_KEY_FALSE(enable_evmcs);
1387 #define current_evmcs ((struct hv_enlightened_vmcs *)this_cpu_read(current_vmcs))
1389 #define KVM_EVMCS_VERSION 1
1391 #if IS_ENABLED(CONFIG_HYPERV)
1392 static bool __read_mostly enlightened_vmcs = true;
1393 module_param(enlightened_vmcs, bool, 0444);
1395 static inline void evmcs_write64(unsigned long field, u64 value)
1398 int offset = get_evmcs_offset(field, &clean_field);
1403 *(u64 *)((char *)current_evmcs + offset) = value;
1405 current_evmcs->hv_clean_fields &= ~clean_field;
1408 static inline void evmcs_write32(unsigned long field, u32 value)
1411 int offset = get_evmcs_offset(field, &clean_field);
1416 *(u32 *)((char *)current_evmcs + offset) = value;
1417 current_evmcs->hv_clean_fields &= ~clean_field;
1420 static inline void evmcs_write16(unsigned long field, u16 value)
1423 int offset = get_evmcs_offset(field, &clean_field);
1428 *(u16 *)((char *)current_evmcs + offset) = value;
1429 current_evmcs->hv_clean_fields &= ~clean_field;
1432 static inline u64 evmcs_read64(unsigned long field)
1434 int offset = get_evmcs_offset(field, NULL);
1439 return *(u64 *)((char *)current_evmcs + offset);
1442 static inline u32 evmcs_read32(unsigned long field)
1444 int offset = get_evmcs_offset(field, NULL);
1449 return *(u32 *)((char *)current_evmcs + offset);
1452 static inline u16 evmcs_read16(unsigned long field)
1454 int offset = get_evmcs_offset(field, NULL);
1459 return *(u16 *)((char *)current_evmcs + offset);
1462 static inline void evmcs_touch_msr_bitmap(void)
1464 if (unlikely(!current_evmcs))
1467 if (current_evmcs->hv_enlightenments_control.msr_bitmap)
1468 current_evmcs->hv_clean_fields &=
1469 ~HV_VMX_ENLIGHTENED_CLEAN_FIELD_MSR_BITMAP;
1472 static void evmcs_load(u64 phys_addr)
1474 struct hv_vp_assist_page *vp_ap =
1475 hv_get_vp_assist_page(smp_processor_id());
1477 vp_ap->current_nested_vmcs = phys_addr;
1478 vp_ap->enlighten_vmentry = 1;
1481 static void evmcs_sanitize_exec_ctrls(struct vmcs_config *vmcs_conf)
1484 * Enlightened VMCSv1 doesn't support these:
1486 * POSTED_INTR_NV = 0x00000002,
1487 * GUEST_INTR_STATUS = 0x00000810,
1488 * APIC_ACCESS_ADDR = 0x00002014,
1489 * POSTED_INTR_DESC_ADDR = 0x00002016,
1490 * EOI_EXIT_BITMAP0 = 0x0000201c,
1491 * EOI_EXIT_BITMAP1 = 0x0000201e,
1492 * EOI_EXIT_BITMAP2 = 0x00002020,
1493 * EOI_EXIT_BITMAP3 = 0x00002022,
1495 vmcs_conf->pin_based_exec_ctrl &= ~PIN_BASED_POSTED_INTR;
1496 vmcs_conf->cpu_based_2nd_exec_ctrl &=
1497 ~SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY;
1498 vmcs_conf->cpu_based_2nd_exec_ctrl &=
1499 ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
1500 vmcs_conf->cpu_based_2nd_exec_ctrl &=
1501 ~SECONDARY_EXEC_APIC_REGISTER_VIRT;
1504 * GUEST_PML_INDEX = 0x00000812,
1505 * PML_ADDRESS = 0x0000200e,
1507 vmcs_conf->cpu_based_2nd_exec_ctrl &= ~SECONDARY_EXEC_ENABLE_PML;
1509 /* VM_FUNCTION_CONTROL = 0x00002018, */
1510 vmcs_conf->cpu_based_2nd_exec_ctrl &= ~SECONDARY_EXEC_ENABLE_VMFUNC;
1513 * EPTP_LIST_ADDRESS = 0x00002024,
1514 * VMREAD_BITMAP = 0x00002026,
1515 * VMWRITE_BITMAP = 0x00002028,
1517 vmcs_conf->cpu_based_2nd_exec_ctrl &= ~SECONDARY_EXEC_SHADOW_VMCS;
1520 * TSC_MULTIPLIER = 0x00002032,
1522 vmcs_conf->cpu_based_2nd_exec_ctrl &= ~SECONDARY_EXEC_TSC_SCALING;
1525 * PLE_GAP = 0x00004020,
1526 * PLE_WINDOW = 0x00004022,
1528 vmcs_conf->cpu_based_2nd_exec_ctrl &= ~SECONDARY_EXEC_PAUSE_LOOP_EXITING;
1531 * VMX_PREEMPTION_TIMER_VALUE = 0x0000482E,
1533 vmcs_conf->pin_based_exec_ctrl &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
1536 * GUEST_IA32_PERF_GLOBAL_CTRL = 0x00002808,
1537 * HOST_IA32_PERF_GLOBAL_CTRL = 0x00002c04,
1539 vmcs_conf->vmexit_ctrl &= ~VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL;
1540 vmcs_conf->vmentry_ctrl &= ~VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL;
1543 * Currently unsupported in KVM:
1544 * GUEST_IA32_RTIT_CTL = 0x00002814,
1548 /* check_ept_pointer() should be under protection of ept_pointer_lock. */
1549 static void check_ept_pointer_match(struct kvm *kvm)
1551 struct kvm_vcpu *vcpu;
1552 u64 tmp_eptp = INVALID_PAGE;
1555 kvm_for_each_vcpu(i, vcpu, kvm) {
1556 if (!VALID_PAGE(tmp_eptp)) {
1557 tmp_eptp = to_vmx(vcpu)->ept_pointer;
1558 } else if (tmp_eptp != to_vmx(vcpu)->ept_pointer) {
1559 to_kvm_vmx(kvm)->ept_pointers_match
1560 = EPT_POINTERS_MISMATCH;
1565 to_kvm_vmx(kvm)->ept_pointers_match = EPT_POINTERS_MATCH;
1568 static int vmx_hv_remote_flush_tlb(struct kvm *kvm)
1572 spin_lock(&to_kvm_vmx(kvm)->ept_pointer_lock);
1574 if (to_kvm_vmx(kvm)->ept_pointers_match == EPT_POINTERS_CHECK)
1575 check_ept_pointer_match(kvm);
1577 if (to_kvm_vmx(kvm)->ept_pointers_match != EPT_POINTERS_MATCH) {
1583 * FLUSH_GUEST_PHYSICAL_ADDRESS_SPACE hypercall needs the address of the
1584 * base of EPT PML4 table, strip off EPT configuration information.
1586 ret = hyperv_flush_guest_mapping(
1587 to_vmx(kvm_get_vcpu(kvm, 0))->ept_pointer & PAGE_MASK);
1590 spin_unlock(&to_kvm_vmx(kvm)->ept_pointer_lock);
1593 #else /* !IS_ENABLED(CONFIG_HYPERV) */
1594 static inline void evmcs_write64(unsigned long field, u64 value) {}
1595 static inline void evmcs_write32(unsigned long field, u32 value) {}
1596 static inline void evmcs_write16(unsigned long field, u16 value) {}
1597 static inline u64 evmcs_read64(unsigned long field) { return 0; }
1598 static inline u32 evmcs_read32(unsigned long field) { return 0; }
1599 static inline u16 evmcs_read16(unsigned long field) { return 0; }
1600 static inline void evmcs_load(u64 phys_addr) {}
1601 static inline void evmcs_sanitize_exec_ctrls(struct vmcs_config *vmcs_conf) {}
1602 static inline void evmcs_touch_msr_bitmap(void) {}
1603 #endif /* IS_ENABLED(CONFIG_HYPERV) */
1605 static inline bool is_exception_n(u32 intr_info, u8 vector)
1607 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
1608 INTR_INFO_VALID_MASK)) ==
1609 (INTR_TYPE_HARD_EXCEPTION | vector | INTR_INFO_VALID_MASK);
1612 static inline bool is_debug(u32 intr_info)
1614 return is_exception_n(intr_info, DB_VECTOR);
1617 static inline bool is_breakpoint(u32 intr_info)
1619 return is_exception_n(intr_info, BP_VECTOR);
1622 static inline bool is_page_fault(u32 intr_info)
1624 return is_exception_n(intr_info, PF_VECTOR);
1627 static inline bool is_no_device(u32 intr_info)
1629 return is_exception_n(intr_info, NM_VECTOR);
1632 static inline bool is_invalid_opcode(u32 intr_info)
1634 return is_exception_n(intr_info, UD_VECTOR);
1637 static inline bool is_gp_fault(u32 intr_info)
1639 return is_exception_n(intr_info, GP_VECTOR);
1642 static inline bool is_external_interrupt(u32 intr_info)
1644 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK))
1645 == (INTR_TYPE_EXT_INTR | INTR_INFO_VALID_MASK);
1648 static inline bool is_machine_check(u32 intr_info)
1650 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
1651 INTR_INFO_VALID_MASK)) ==
1652 (INTR_TYPE_HARD_EXCEPTION | MC_VECTOR | INTR_INFO_VALID_MASK);
1655 /* Undocumented: icebp/int1 */
1656 static inline bool is_icebp(u32 intr_info)
1658 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK))
1659 == (INTR_TYPE_PRIV_SW_EXCEPTION | INTR_INFO_VALID_MASK);
1662 static inline bool cpu_has_vmx_msr_bitmap(void)
1664 return vmcs_config.cpu_based_exec_ctrl & CPU_BASED_USE_MSR_BITMAPS;
1667 static inline bool cpu_has_vmx_tpr_shadow(void)
1669 return vmcs_config.cpu_based_exec_ctrl & CPU_BASED_TPR_SHADOW;
1672 static inline bool cpu_need_tpr_shadow(struct kvm_vcpu *vcpu)
1674 return cpu_has_vmx_tpr_shadow() && lapic_in_kernel(vcpu);
1677 static inline bool cpu_has_secondary_exec_ctrls(void)
1679 return vmcs_config.cpu_based_exec_ctrl &
1680 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
1683 static inline bool cpu_has_vmx_virtualize_apic_accesses(void)
1685 return vmcs_config.cpu_based_2nd_exec_ctrl &
1686 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
1689 static inline bool cpu_has_vmx_virtualize_x2apic_mode(void)
1691 return vmcs_config.cpu_based_2nd_exec_ctrl &
1692 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
1695 static inline bool cpu_has_vmx_apic_register_virt(void)
1697 return vmcs_config.cpu_based_2nd_exec_ctrl &
1698 SECONDARY_EXEC_APIC_REGISTER_VIRT;
1701 static inline bool cpu_has_vmx_virtual_intr_delivery(void)
1703 return vmcs_config.cpu_based_2nd_exec_ctrl &
1704 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY;
1707 static inline bool cpu_has_vmx_encls_vmexit(void)
1709 return vmcs_config.cpu_based_2nd_exec_ctrl &
1710 SECONDARY_EXEC_ENCLS_EXITING;
1714 * Comment's format: document - errata name - stepping - processor name.
1716 * https://www.virtualbox.org/svn/vbox/trunk/src/VBox/VMM/VMMR0/HMR0.cpp
1718 static u32 vmx_preemption_cpu_tfms[] = {
1719 /* 323344.pdf - BA86 - D0 - Xeon 7500 Series */
1721 /* 323056.pdf - AAX65 - C2 - Xeon L3406 */
1722 /* 322814.pdf - AAT59 - C2 - i7-600, i5-500, i5-400 and i3-300 Mobile */
1723 /* 322911.pdf - AAU65 - C2 - i5-600, i3-500 Desktop and Pentium G6950 */
1725 /* 322911.pdf - AAU65 - K0 - i5-600, i3-500 Desktop and Pentium G6950 */
1727 /* 322373.pdf - AAO95 - B1 - Xeon 3400 Series */
1728 /* 322166.pdf - AAN92 - B1 - i7-800 and i5-700 Desktop */
1730 * 320767.pdf - AAP86 - B1 -
1731 * i7-900 Mobile Extreme, i7-800 and i7-700 Mobile
1734 /* 321333.pdf - AAM126 - C0 - Xeon 3500 */
1736 /* 321333.pdf - AAM126 - C1 - Xeon 3500 */
1738 /* 320836.pdf - AAJ124 - C0 - i7-900 Desktop Extreme and i7-900 Desktop */
1740 /* 321333.pdf - AAM126 - D0 - Xeon 3500 */
1741 /* 321324.pdf - AAK139 - D0 - Xeon 5500 */
1742 /* 320836.pdf - AAJ124 - D0 - i7-900 Extreme and i7-900 Desktop */
1746 static inline bool cpu_has_broken_vmx_preemption_timer(void)
1748 u32 eax = cpuid_eax(0x00000001), i;
1750 /* Clear the reserved bits */
1751 eax &= ~(0x3U << 14 | 0xfU << 28);
1752 for (i = 0; i < ARRAY_SIZE(vmx_preemption_cpu_tfms); i++)
1753 if (eax == vmx_preemption_cpu_tfms[i])
1759 static inline bool cpu_has_vmx_preemption_timer(void)
1761 return vmcs_config.pin_based_exec_ctrl &
1762 PIN_BASED_VMX_PREEMPTION_TIMER;
1765 static inline bool cpu_has_vmx_posted_intr(void)
1767 return IS_ENABLED(CONFIG_X86_LOCAL_APIC) &&
1768 vmcs_config.pin_based_exec_ctrl & PIN_BASED_POSTED_INTR;
1771 static inline bool cpu_has_vmx_apicv(void)
1773 return cpu_has_vmx_apic_register_virt() &&
1774 cpu_has_vmx_virtual_intr_delivery() &&
1775 cpu_has_vmx_posted_intr();
1778 static inline bool cpu_has_vmx_flexpriority(void)
1780 return cpu_has_vmx_tpr_shadow() &&
1781 cpu_has_vmx_virtualize_apic_accesses();
1784 static inline bool cpu_has_vmx_ept_execute_only(void)
1786 return vmx_capability.ept & VMX_EPT_EXECUTE_ONLY_BIT;
1789 static inline bool cpu_has_vmx_ept_2m_page(void)
1791 return vmx_capability.ept & VMX_EPT_2MB_PAGE_BIT;
1794 static inline bool cpu_has_vmx_ept_1g_page(void)
1796 return vmx_capability.ept & VMX_EPT_1GB_PAGE_BIT;
1799 static inline bool cpu_has_vmx_ept_4levels(void)
1801 return vmx_capability.ept & VMX_EPT_PAGE_WALK_4_BIT;
1804 static inline bool cpu_has_vmx_ept_mt_wb(void)
1806 return vmx_capability.ept & VMX_EPTP_WB_BIT;
1809 static inline bool cpu_has_vmx_ept_5levels(void)
1811 return vmx_capability.ept & VMX_EPT_PAGE_WALK_5_BIT;
1814 static inline bool cpu_has_vmx_ept_ad_bits(void)
1816 return vmx_capability.ept & VMX_EPT_AD_BIT;
1819 static inline bool cpu_has_vmx_invept_context(void)
1821 return vmx_capability.ept & VMX_EPT_EXTENT_CONTEXT_BIT;
1824 static inline bool cpu_has_vmx_invept_global(void)
1826 return vmx_capability.ept & VMX_EPT_EXTENT_GLOBAL_BIT;
1829 static inline bool cpu_has_vmx_invvpid_individual_addr(void)
1831 return vmx_capability.vpid & VMX_VPID_EXTENT_INDIVIDUAL_ADDR_BIT;
1834 static inline bool cpu_has_vmx_invvpid_single(void)
1836 return vmx_capability.vpid & VMX_VPID_EXTENT_SINGLE_CONTEXT_BIT;
1839 static inline bool cpu_has_vmx_invvpid_global(void)
1841 return vmx_capability.vpid & VMX_VPID_EXTENT_GLOBAL_CONTEXT_BIT;
1844 static inline bool cpu_has_vmx_invvpid(void)
1846 return vmx_capability.vpid & VMX_VPID_INVVPID_BIT;
1849 static inline bool cpu_has_vmx_ept(void)
1851 return vmcs_config.cpu_based_2nd_exec_ctrl &
1852 SECONDARY_EXEC_ENABLE_EPT;
1855 static inline bool cpu_has_vmx_unrestricted_guest(void)
1857 return vmcs_config.cpu_based_2nd_exec_ctrl &
1858 SECONDARY_EXEC_UNRESTRICTED_GUEST;
1861 static inline bool cpu_has_vmx_ple(void)
1863 return vmcs_config.cpu_based_2nd_exec_ctrl &
1864 SECONDARY_EXEC_PAUSE_LOOP_EXITING;
1867 static inline bool cpu_has_vmx_basic_inout(void)
1869 return (((u64)vmcs_config.basic_cap << 32) & VMX_BASIC_INOUT);
1872 static inline bool cpu_need_virtualize_apic_accesses(struct kvm_vcpu *vcpu)
1874 return flexpriority_enabled && lapic_in_kernel(vcpu);
1877 static inline bool cpu_has_vmx_vpid(void)
1879 return vmcs_config.cpu_based_2nd_exec_ctrl &
1880 SECONDARY_EXEC_ENABLE_VPID;
1883 static inline bool cpu_has_vmx_rdtscp(void)
1885 return vmcs_config.cpu_based_2nd_exec_ctrl &
1886 SECONDARY_EXEC_RDTSCP;
1889 static inline bool cpu_has_vmx_invpcid(void)
1891 return vmcs_config.cpu_based_2nd_exec_ctrl &
1892 SECONDARY_EXEC_ENABLE_INVPCID;
1895 static inline bool cpu_has_virtual_nmis(void)
1897 return vmcs_config.pin_based_exec_ctrl & PIN_BASED_VIRTUAL_NMIS;
1900 static inline bool cpu_has_vmx_wbinvd_exit(void)
1902 return vmcs_config.cpu_based_2nd_exec_ctrl &
1903 SECONDARY_EXEC_WBINVD_EXITING;
1906 static inline bool cpu_has_vmx_shadow_vmcs(void)
1909 rdmsrl(MSR_IA32_VMX_MISC, vmx_msr);
1910 /* check if the cpu supports writing r/o exit information fields */
1911 if (!(vmx_msr & MSR_IA32_VMX_MISC_VMWRITE_SHADOW_RO_FIELDS))
1914 return vmcs_config.cpu_based_2nd_exec_ctrl &
1915 SECONDARY_EXEC_SHADOW_VMCS;
1918 static inline bool cpu_has_vmx_pml(void)
1920 return vmcs_config.cpu_based_2nd_exec_ctrl & SECONDARY_EXEC_ENABLE_PML;
1923 static inline bool cpu_has_vmx_tsc_scaling(void)
1925 return vmcs_config.cpu_based_2nd_exec_ctrl &
1926 SECONDARY_EXEC_TSC_SCALING;
1929 static inline bool cpu_has_vmx_vmfunc(void)
1931 return vmcs_config.cpu_based_2nd_exec_ctrl &
1932 SECONDARY_EXEC_ENABLE_VMFUNC;
1935 static bool vmx_umip_emulated(void)
1937 return vmcs_config.cpu_based_2nd_exec_ctrl &
1938 SECONDARY_EXEC_DESC;
1941 static inline bool report_flexpriority(void)
1943 return flexpriority_enabled;
1946 static inline unsigned nested_cpu_vmx_misc_cr3_count(struct kvm_vcpu *vcpu)
1948 return vmx_misc_cr3_count(to_vmx(vcpu)->nested.msrs.misc_low);
1952 * Do the virtual VMX capability MSRs specify that L1 can use VMWRITE
1953 * to modify any valid field of the VMCS, or are the VM-exit
1954 * information fields read-only?
1956 static inline bool nested_cpu_has_vmwrite_any_field(struct kvm_vcpu *vcpu)
1958 return to_vmx(vcpu)->nested.msrs.misc_low &
1959 MSR_IA32_VMX_MISC_VMWRITE_SHADOW_RO_FIELDS;
1962 static inline bool nested_cpu_has_zero_length_injection(struct kvm_vcpu *vcpu)
1964 return to_vmx(vcpu)->nested.msrs.misc_low & VMX_MISC_ZERO_LEN_INS;
1967 static inline bool nested_cpu_supports_monitor_trap_flag(struct kvm_vcpu *vcpu)
1969 return to_vmx(vcpu)->nested.msrs.procbased_ctls_high &
1970 CPU_BASED_MONITOR_TRAP_FLAG;
1973 static inline bool nested_cpu_has_vmx_shadow_vmcs(struct kvm_vcpu *vcpu)
1975 return to_vmx(vcpu)->nested.msrs.secondary_ctls_high &
1976 SECONDARY_EXEC_SHADOW_VMCS;
1979 static inline bool nested_cpu_has(struct vmcs12 *vmcs12, u32 bit)
1981 return vmcs12->cpu_based_vm_exec_control & bit;
1984 static inline bool nested_cpu_has2(struct vmcs12 *vmcs12, u32 bit)
1986 return (vmcs12->cpu_based_vm_exec_control &
1987 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) &&
1988 (vmcs12->secondary_vm_exec_control & bit);
1991 static inline bool nested_cpu_has_preemption_timer(struct vmcs12 *vmcs12)
1993 return vmcs12->pin_based_vm_exec_control &
1994 PIN_BASED_VMX_PREEMPTION_TIMER;
1997 static inline bool nested_cpu_has_nmi_exiting(struct vmcs12 *vmcs12)
1999 return vmcs12->pin_based_vm_exec_control & PIN_BASED_NMI_EXITING;
2002 static inline bool nested_cpu_has_virtual_nmis(struct vmcs12 *vmcs12)
2004 return vmcs12->pin_based_vm_exec_control & PIN_BASED_VIRTUAL_NMIS;
2007 static inline int nested_cpu_has_ept(struct vmcs12 *vmcs12)
2009 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_EPT);
2012 static inline bool nested_cpu_has_xsaves(struct vmcs12 *vmcs12)
2014 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_XSAVES);
2017 static inline bool nested_cpu_has_pml(struct vmcs12 *vmcs12)
2019 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_PML);
2022 static inline bool nested_cpu_has_virt_x2apic_mode(struct vmcs12 *vmcs12)
2024 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE);
2027 static inline bool nested_cpu_has_vpid(struct vmcs12 *vmcs12)
2029 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_VPID);
2032 static inline bool nested_cpu_has_apic_reg_virt(struct vmcs12 *vmcs12)
2034 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_APIC_REGISTER_VIRT);
2037 static inline bool nested_cpu_has_vid(struct vmcs12 *vmcs12)
2039 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
2042 static inline bool nested_cpu_has_posted_intr(struct vmcs12 *vmcs12)
2044 return vmcs12->pin_based_vm_exec_control & PIN_BASED_POSTED_INTR;
2047 static inline bool nested_cpu_has_vmfunc(struct vmcs12 *vmcs12)
2049 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_VMFUNC);
2052 static inline bool nested_cpu_has_eptp_switching(struct vmcs12 *vmcs12)
2054 return nested_cpu_has_vmfunc(vmcs12) &&
2055 (vmcs12->vm_function_control &
2056 VMX_VMFUNC_EPTP_SWITCHING);
2059 static inline bool nested_cpu_has_shadow_vmcs(struct vmcs12 *vmcs12)
2061 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_SHADOW_VMCS);
2064 static inline bool is_nmi(u32 intr_info)
2066 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK))
2067 == (INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK);
2070 static void nested_vmx_vmexit(struct kvm_vcpu *vcpu, u32 exit_reason,
2072 unsigned long exit_qualification);
2073 static void nested_vmx_entry_failure(struct kvm_vcpu *vcpu,
2074 struct vmcs12 *vmcs12,
2075 u32 reason, unsigned long qualification);
2077 static int __find_msr_index(struct vcpu_vmx *vmx, u32 msr)
2081 for (i = 0; i < vmx->nmsrs; ++i)
2082 if (vmx_msr_index[vmx->guest_msrs[i].index] == msr)
2087 static inline void __invvpid(unsigned long ext, u16 vpid, gva_t gva)
2093 } operand = { vpid, 0, gva };
2096 asm volatile (__ex(ASM_VMX_INVVPID) CC_SET(na)
2097 : CC_OUT(na) (error) : "a"(&operand), "c"(ext)
2102 static inline void __invept(unsigned long ext, u64 eptp, gpa_t gpa)
2106 } operand = {eptp, gpa};
2109 asm volatile (__ex(ASM_VMX_INVEPT) CC_SET(na)
2110 : CC_OUT(na) (error) : "a" (&operand), "c" (ext)
2115 static void vmx_setup_fb_clear_ctrl(void)
2119 if (boot_cpu_has(X86_FEATURE_ARCH_CAPABILITIES) &&
2120 !boot_cpu_has_bug(X86_BUG_MDS) &&
2121 !boot_cpu_has_bug(X86_BUG_TAA)) {
2122 rdmsrl(MSR_IA32_ARCH_CAPABILITIES, msr);
2123 if (msr & ARCH_CAP_FB_CLEAR_CTRL)
2124 vmx_fb_clear_ctrl_available = true;
2128 static __always_inline void vmx_disable_fb_clear(struct vcpu_vmx *vmx)
2132 if (!vmx->disable_fb_clear)
2135 rdmsrl(MSR_IA32_MCU_OPT_CTRL, msr);
2136 msr |= FB_CLEAR_DIS;
2137 wrmsrl(MSR_IA32_MCU_OPT_CTRL, msr);
2138 /* Cache the MSR value to avoid reading it later */
2139 vmx->msr_ia32_mcu_opt_ctrl = msr;
2142 static __always_inline void vmx_enable_fb_clear(struct vcpu_vmx *vmx)
2144 if (!vmx->disable_fb_clear)
2147 vmx->msr_ia32_mcu_opt_ctrl &= ~FB_CLEAR_DIS;
2148 wrmsrl(MSR_IA32_MCU_OPT_CTRL, vmx->msr_ia32_mcu_opt_ctrl);
2151 static void vmx_update_fb_clear_dis(struct kvm_vcpu *vcpu, struct vcpu_vmx *vmx)
2153 vmx->disable_fb_clear = vmx_fb_clear_ctrl_available;
2156 * If guest will not execute VERW, there is no need to set FB_CLEAR_DIS
2157 * at VMEntry. Skip the MSR read/write when a guest has no use case to
2160 if ((vcpu->arch.arch_capabilities & ARCH_CAP_FB_CLEAR) ||
2161 ((vcpu->arch.arch_capabilities & ARCH_CAP_MDS_NO) &&
2162 (vcpu->arch.arch_capabilities & ARCH_CAP_TAA_NO) &&
2163 (vcpu->arch.arch_capabilities & ARCH_CAP_PSDP_NO) &&
2164 (vcpu->arch.arch_capabilities & ARCH_CAP_FBSDP_NO) &&
2165 (vcpu->arch.arch_capabilities & ARCH_CAP_SBDR_SSDP_NO)))
2166 vmx->disable_fb_clear = false;
2169 static struct shared_msr_entry *find_msr_entry(struct vcpu_vmx *vmx, u32 msr)
2173 i = __find_msr_index(vmx, msr);
2175 return &vmx->guest_msrs[i];
2179 static void vmcs_clear(struct vmcs *vmcs)
2181 u64 phys_addr = __pa(vmcs);
2184 asm volatile (__ex(ASM_VMX_VMCLEAR_RAX) CC_SET(na)
2185 : CC_OUT(na) (error) : "a"(&phys_addr), "m"(phys_addr)
2187 if (unlikely(error))
2188 printk(KERN_ERR "kvm: vmclear fail: %p/%llx\n",
2192 static inline void loaded_vmcs_init(struct loaded_vmcs *loaded_vmcs)
2194 vmcs_clear(loaded_vmcs->vmcs);
2195 if (loaded_vmcs->shadow_vmcs && loaded_vmcs->launched)
2196 vmcs_clear(loaded_vmcs->shadow_vmcs);
2197 loaded_vmcs->cpu = -1;
2198 loaded_vmcs->launched = 0;
2201 static void vmcs_load(struct vmcs *vmcs)
2203 u64 phys_addr = __pa(vmcs);
2206 if (static_branch_unlikely(&enable_evmcs))
2207 return evmcs_load(phys_addr);
2209 asm volatile (__ex(ASM_VMX_VMPTRLD_RAX) CC_SET(na)
2210 : CC_OUT(na) (error) : "a"(&phys_addr), "m"(phys_addr)
2212 if (unlikely(error))
2213 printk(KERN_ERR "kvm: vmptrld %p/%llx failed\n",
2217 #ifdef CONFIG_KEXEC_CORE
2218 static void crash_vmclear_local_loaded_vmcss(void)
2220 int cpu = raw_smp_processor_id();
2221 struct loaded_vmcs *v;
2223 list_for_each_entry(v, &per_cpu(loaded_vmcss_on_cpu, cpu),
2224 loaded_vmcss_on_cpu_link)
2225 vmcs_clear(v->vmcs);
2227 #endif /* CONFIG_KEXEC_CORE */
2229 static void __loaded_vmcs_clear(void *arg)
2231 struct loaded_vmcs *loaded_vmcs = arg;
2232 int cpu = raw_smp_processor_id();
2234 if (loaded_vmcs->cpu != cpu)
2235 return; /* vcpu migration can race with cpu offline */
2236 if (per_cpu(current_vmcs, cpu) == loaded_vmcs->vmcs)
2237 per_cpu(current_vmcs, cpu) = NULL;
2239 vmcs_clear(loaded_vmcs->vmcs);
2240 if (loaded_vmcs->shadow_vmcs && loaded_vmcs->launched)
2241 vmcs_clear(loaded_vmcs->shadow_vmcs);
2243 list_del(&loaded_vmcs->loaded_vmcss_on_cpu_link);
2246 * Ensure all writes to loaded_vmcs, including deleting it from its
2247 * current percpu list, complete before setting loaded_vmcs->vcpu to
2248 * -1, otherwise a different cpu can see vcpu == -1 first and add
2249 * loaded_vmcs to its percpu list before it's deleted from this cpu's
2250 * list. Pairs with the smp_rmb() in vmx_vcpu_load_vmcs().
2254 loaded_vmcs->cpu = -1;
2255 loaded_vmcs->launched = 0;
2258 static void loaded_vmcs_clear(struct loaded_vmcs *loaded_vmcs)
2260 int cpu = loaded_vmcs->cpu;
2263 smp_call_function_single(cpu,
2264 __loaded_vmcs_clear, loaded_vmcs, 1);
2267 static inline bool vpid_sync_vcpu_addr(int vpid, gva_t addr)
2272 if (cpu_has_vmx_invvpid_individual_addr()) {
2273 __invvpid(VMX_VPID_EXTENT_INDIVIDUAL_ADDR, vpid, addr);
2280 static inline void vpid_sync_vcpu_single(int vpid)
2285 if (cpu_has_vmx_invvpid_single())
2286 __invvpid(VMX_VPID_EXTENT_SINGLE_CONTEXT, vpid, 0);
2289 static inline void vpid_sync_vcpu_global(void)
2291 if (cpu_has_vmx_invvpid_global())
2292 __invvpid(VMX_VPID_EXTENT_ALL_CONTEXT, 0, 0);
2295 static inline void vpid_sync_context(int vpid)
2297 if (cpu_has_vmx_invvpid_single())
2298 vpid_sync_vcpu_single(vpid);
2300 vpid_sync_vcpu_global();
2303 static inline void ept_sync_global(void)
2305 __invept(VMX_EPT_EXTENT_GLOBAL, 0, 0);
2308 static inline void ept_sync_context(u64 eptp)
2310 if (cpu_has_vmx_invept_context())
2311 __invept(VMX_EPT_EXTENT_CONTEXT, eptp, 0);
2316 static __always_inline void vmcs_check16(unsigned long field)
2318 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2000,
2319 "16-bit accessor invalid for 64-bit field");
2320 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2001,
2321 "16-bit accessor invalid for 64-bit high field");
2322 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x4000,
2323 "16-bit accessor invalid for 32-bit high field");
2324 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x6000,
2325 "16-bit accessor invalid for natural width field");
2328 static __always_inline void vmcs_check32(unsigned long field)
2330 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0,
2331 "32-bit accessor invalid for 16-bit field");
2332 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x6000,
2333 "32-bit accessor invalid for natural width field");
2336 static __always_inline void vmcs_check64(unsigned long field)
2338 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0,
2339 "64-bit accessor invalid for 16-bit field");
2340 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2001,
2341 "64-bit accessor invalid for 64-bit high field");
2342 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x4000,
2343 "64-bit accessor invalid for 32-bit field");
2344 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x6000,
2345 "64-bit accessor invalid for natural width field");
2348 static __always_inline void vmcs_checkl(unsigned long field)
2350 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0,
2351 "Natural width accessor invalid for 16-bit field");
2352 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2000,
2353 "Natural width accessor invalid for 64-bit field");
2354 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2001,
2355 "Natural width accessor invalid for 64-bit high field");
2356 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x4000,
2357 "Natural width accessor invalid for 32-bit field");
2360 static __always_inline unsigned long __vmcs_readl(unsigned long field)
2362 unsigned long value;
2364 asm volatile (__ex_clear(ASM_VMX_VMREAD_RDX_RAX, "%0")
2365 : "=a"(value) : "d"(field) : "cc");
2369 static __always_inline u16 vmcs_read16(unsigned long field)
2371 vmcs_check16(field);
2372 if (static_branch_unlikely(&enable_evmcs))
2373 return evmcs_read16(field);
2374 return __vmcs_readl(field);
2377 static __always_inline u32 vmcs_read32(unsigned long field)
2379 vmcs_check32(field);
2380 if (static_branch_unlikely(&enable_evmcs))
2381 return evmcs_read32(field);
2382 return __vmcs_readl(field);
2385 static __always_inline u64 vmcs_read64(unsigned long field)
2387 vmcs_check64(field);
2388 if (static_branch_unlikely(&enable_evmcs))
2389 return evmcs_read64(field);
2390 #ifdef CONFIG_X86_64
2391 return __vmcs_readl(field);
2393 return __vmcs_readl(field) | ((u64)__vmcs_readl(field+1) << 32);
2397 static __always_inline unsigned long vmcs_readl(unsigned long field)
2400 if (static_branch_unlikely(&enable_evmcs))
2401 return evmcs_read64(field);
2402 return __vmcs_readl(field);
2405 static noinline void vmwrite_error(unsigned long field, unsigned long value)
2407 printk(KERN_ERR "vmwrite error: reg %lx value %lx (err %d)\n",
2408 field, value, vmcs_read32(VM_INSTRUCTION_ERROR));
2412 static __always_inline void __vmcs_writel(unsigned long field, unsigned long value)
2416 asm volatile (__ex(ASM_VMX_VMWRITE_RAX_RDX) CC_SET(na)
2417 : CC_OUT(na) (error) : "a"(value), "d"(field));
2418 if (unlikely(error))
2419 vmwrite_error(field, value);
2422 static __always_inline void vmcs_write16(unsigned long field, u16 value)
2424 vmcs_check16(field);
2425 if (static_branch_unlikely(&enable_evmcs))
2426 return evmcs_write16(field, value);
2428 __vmcs_writel(field, value);
2431 static __always_inline void vmcs_write32(unsigned long field, u32 value)
2433 vmcs_check32(field);
2434 if (static_branch_unlikely(&enable_evmcs))
2435 return evmcs_write32(field, value);
2437 __vmcs_writel(field, value);
2440 static __always_inline void vmcs_write64(unsigned long field, u64 value)
2442 vmcs_check64(field);
2443 if (static_branch_unlikely(&enable_evmcs))
2444 return evmcs_write64(field, value);
2446 __vmcs_writel(field, value);
2447 #ifndef CONFIG_X86_64
2449 __vmcs_writel(field+1, value >> 32);
2453 static __always_inline void vmcs_writel(unsigned long field, unsigned long value)
2456 if (static_branch_unlikely(&enable_evmcs))
2457 return evmcs_write64(field, value);
2459 __vmcs_writel(field, value);
2462 static __always_inline void vmcs_clear_bits(unsigned long field, u32 mask)
2464 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x2000,
2465 "vmcs_clear_bits does not support 64-bit fields");
2466 if (static_branch_unlikely(&enable_evmcs))
2467 return evmcs_write32(field, evmcs_read32(field) & ~mask);
2469 __vmcs_writel(field, __vmcs_readl(field) & ~mask);
2472 static __always_inline void vmcs_set_bits(unsigned long field, u32 mask)
2474 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x2000,
2475 "vmcs_set_bits does not support 64-bit fields");
2476 if (static_branch_unlikely(&enable_evmcs))
2477 return evmcs_write32(field, evmcs_read32(field) | mask);
2479 __vmcs_writel(field, __vmcs_readl(field) | mask);
2482 static inline void vm_entry_controls_reset_shadow(struct vcpu_vmx *vmx)
2484 vmx->vm_entry_controls_shadow = vmcs_read32(VM_ENTRY_CONTROLS);
2487 static inline void vm_entry_controls_init(struct vcpu_vmx *vmx, u32 val)
2489 vmcs_write32(VM_ENTRY_CONTROLS, val);
2490 vmx->vm_entry_controls_shadow = val;
2493 static inline void vm_entry_controls_set(struct vcpu_vmx *vmx, u32 val)
2495 if (vmx->vm_entry_controls_shadow != val)
2496 vm_entry_controls_init(vmx, val);
2499 static inline u32 vm_entry_controls_get(struct vcpu_vmx *vmx)
2501 return vmx->vm_entry_controls_shadow;
2505 static inline void vm_entry_controls_setbit(struct vcpu_vmx *vmx, u32 val)
2507 vm_entry_controls_set(vmx, vm_entry_controls_get(vmx) | val);
2510 static inline void vm_entry_controls_clearbit(struct vcpu_vmx *vmx, u32 val)
2512 vm_entry_controls_set(vmx, vm_entry_controls_get(vmx) & ~val);
2515 static inline void vm_exit_controls_reset_shadow(struct vcpu_vmx *vmx)
2517 vmx->vm_exit_controls_shadow = vmcs_read32(VM_EXIT_CONTROLS);
2520 static inline void vm_exit_controls_init(struct vcpu_vmx *vmx, u32 val)
2522 vmcs_write32(VM_EXIT_CONTROLS, val);
2523 vmx->vm_exit_controls_shadow = val;
2526 static inline void vm_exit_controls_set(struct vcpu_vmx *vmx, u32 val)
2528 if (vmx->vm_exit_controls_shadow != val)
2529 vm_exit_controls_init(vmx, val);
2532 static inline u32 vm_exit_controls_get(struct vcpu_vmx *vmx)
2534 return vmx->vm_exit_controls_shadow;
2538 static inline void vm_exit_controls_setbit(struct vcpu_vmx *vmx, u32 val)
2540 vm_exit_controls_set(vmx, vm_exit_controls_get(vmx) | val);
2543 static inline void vm_exit_controls_clearbit(struct vcpu_vmx *vmx, u32 val)
2545 vm_exit_controls_set(vmx, vm_exit_controls_get(vmx) & ~val);
2548 static void vmx_segment_cache_clear(struct vcpu_vmx *vmx)
2550 vmx->segment_cache.bitmask = 0;
2553 static bool vmx_segment_cache_test_set(struct vcpu_vmx *vmx, unsigned seg,
2557 u32 mask = 1 << (seg * SEG_FIELD_NR + field);
2559 if (!(vmx->vcpu.arch.regs_avail & (1 << VCPU_EXREG_SEGMENTS))) {
2560 vmx->vcpu.arch.regs_avail |= (1 << VCPU_EXREG_SEGMENTS);
2561 vmx->segment_cache.bitmask = 0;
2563 ret = vmx->segment_cache.bitmask & mask;
2564 vmx->segment_cache.bitmask |= mask;
2568 static u16 vmx_read_guest_seg_selector(struct vcpu_vmx *vmx, unsigned seg)
2570 u16 *p = &vmx->segment_cache.seg[seg].selector;
2572 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_SEL))
2573 *p = vmcs_read16(kvm_vmx_segment_fields[seg].selector);
2577 static ulong vmx_read_guest_seg_base(struct vcpu_vmx *vmx, unsigned seg)
2579 ulong *p = &vmx->segment_cache.seg[seg].base;
2581 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_BASE))
2582 *p = vmcs_readl(kvm_vmx_segment_fields[seg].base);
2586 static u32 vmx_read_guest_seg_limit(struct vcpu_vmx *vmx, unsigned seg)
2588 u32 *p = &vmx->segment_cache.seg[seg].limit;
2590 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_LIMIT))
2591 *p = vmcs_read32(kvm_vmx_segment_fields[seg].limit);
2595 static u32 vmx_read_guest_seg_ar(struct vcpu_vmx *vmx, unsigned seg)
2597 u32 *p = &vmx->segment_cache.seg[seg].ar;
2599 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_AR))
2600 *p = vmcs_read32(kvm_vmx_segment_fields[seg].ar_bytes);
2604 static void update_exception_bitmap(struct kvm_vcpu *vcpu)
2608 eb = (1u << PF_VECTOR) | (1u << UD_VECTOR) | (1u << MC_VECTOR) |
2609 (1u << DB_VECTOR) | (1u << AC_VECTOR);
2611 * Guest access to VMware backdoor ports could legitimately
2612 * trigger #GP because of TSS I/O permission bitmap.
2613 * We intercept those #GP and allow access to them anyway
2616 if (enable_vmware_backdoor)
2617 eb |= (1u << GP_VECTOR);
2618 if ((vcpu->guest_debug &
2619 (KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_SW_BP)) ==
2620 (KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_SW_BP))
2621 eb |= 1u << BP_VECTOR;
2622 if (to_vmx(vcpu)->rmode.vm86_active)
2625 eb &= ~(1u << PF_VECTOR); /* bypass_guest_pf = 0 */
2627 /* When we are running a nested L2 guest and L1 specified for it a
2628 * certain exception bitmap, we must trap the same exceptions and pass
2629 * them to L1. When running L2, we will only handle the exceptions
2630 * specified above if L1 did not want them.
2632 if (is_guest_mode(vcpu))
2633 eb |= get_vmcs12(vcpu)->exception_bitmap;
2635 vmcs_write32(EXCEPTION_BITMAP, eb);
2639 * Check if MSR is intercepted for currently loaded MSR bitmap.
2641 static bool msr_write_intercepted(struct kvm_vcpu *vcpu, u32 msr)
2643 unsigned long *msr_bitmap;
2644 int f = sizeof(unsigned long);
2646 if (!cpu_has_vmx_msr_bitmap())
2649 msr_bitmap = to_vmx(vcpu)->loaded_vmcs->msr_bitmap;
2651 if (msr <= 0x1fff) {
2652 return !!test_bit(msr, msr_bitmap + 0x800 / f);
2653 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
2655 return !!test_bit(msr, msr_bitmap + 0xc00 / f);
2662 * Check if MSR is intercepted for L01 MSR bitmap.
2664 static bool msr_write_intercepted_l01(struct kvm_vcpu *vcpu, u32 msr)
2666 unsigned long *msr_bitmap;
2667 int f = sizeof(unsigned long);
2669 if (!cpu_has_vmx_msr_bitmap())
2672 msr_bitmap = to_vmx(vcpu)->vmcs01.msr_bitmap;
2674 if (msr <= 0x1fff) {
2675 return !!test_bit(msr, msr_bitmap + 0x800 / f);
2676 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
2678 return !!test_bit(msr, msr_bitmap + 0xc00 / f);
2684 static void clear_atomic_switch_msr_special(struct vcpu_vmx *vmx,
2685 unsigned long entry, unsigned long exit)
2687 vm_entry_controls_clearbit(vmx, entry);
2688 vm_exit_controls_clearbit(vmx, exit);
2691 static int find_msr(struct vmx_msrs *m, unsigned int msr)
2695 for (i = 0; i < m->nr; ++i) {
2696 if (m->val[i].index == msr)
2702 static void clear_atomic_switch_msr(struct vcpu_vmx *vmx, unsigned msr)
2705 struct msr_autoload *m = &vmx->msr_autoload;
2709 if (cpu_has_load_ia32_efer) {
2710 clear_atomic_switch_msr_special(vmx,
2711 VM_ENTRY_LOAD_IA32_EFER,
2712 VM_EXIT_LOAD_IA32_EFER);
2716 case MSR_CORE_PERF_GLOBAL_CTRL:
2717 if (cpu_has_load_perf_global_ctrl) {
2718 clear_atomic_switch_msr_special(vmx,
2719 VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL,
2720 VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL);
2725 i = find_msr(&m->guest, msr);
2729 m->guest.val[i] = m->guest.val[m->guest.nr];
2730 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, m->guest.nr);
2733 i = find_msr(&m->host, msr);
2738 m->host.val[i] = m->host.val[m->host.nr];
2739 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, m->host.nr);
2742 static void add_atomic_switch_msr_special(struct vcpu_vmx *vmx,
2743 unsigned long entry, unsigned long exit,
2744 unsigned long guest_val_vmcs, unsigned long host_val_vmcs,
2745 u64 guest_val, u64 host_val)
2747 vmcs_write64(guest_val_vmcs, guest_val);
2748 vmcs_write64(host_val_vmcs, host_val);
2749 vm_entry_controls_setbit(vmx, entry);
2750 vm_exit_controls_setbit(vmx, exit);
2753 static void add_atomic_switch_msr(struct vcpu_vmx *vmx, unsigned msr,
2754 u64 guest_val, u64 host_val, bool entry_only)
2757 struct msr_autoload *m = &vmx->msr_autoload;
2761 if (cpu_has_load_ia32_efer) {
2762 add_atomic_switch_msr_special(vmx,
2763 VM_ENTRY_LOAD_IA32_EFER,
2764 VM_EXIT_LOAD_IA32_EFER,
2767 guest_val, host_val);
2771 case MSR_CORE_PERF_GLOBAL_CTRL:
2772 if (cpu_has_load_perf_global_ctrl) {
2773 add_atomic_switch_msr_special(vmx,
2774 VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL,
2775 VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL,
2776 GUEST_IA32_PERF_GLOBAL_CTRL,
2777 HOST_IA32_PERF_GLOBAL_CTRL,
2778 guest_val, host_val);
2782 case MSR_IA32_PEBS_ENABLE:
2783 /* PEBS needs a quiescent period after being disabled (to write
2784 * a record). Disabling PEBS through VMX MSR swapping doesn't
2785 * provide that period, so a CPU could write host's record into
2788 wrmsrl(MSR_IA32_PEBS_ENABLE, 0);
2791 i = find_msr(&m->guest, msr);
2793 j = find_msr(&m->host, msr);
2795 if ((i < 0 && m->guest.nr == NR_AUTOLOAD_MSRS) ||
2796 (j < 0 && m->host.nr == NR_AUTOLOAD_MSRS)) {
2797 printk_once(KERN_WARNING "Not enough msr switch entries. "
2798 "Can't add msr %x\n", msr);
2803 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, m->guest.nr);
2805 m->guest.val[i].index = msr;
2806 m->guest.val[i].value = guest_val;
2813 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, m->host.nr);
2815 m->host.val[j].index = msr;
2816 m->host.val[j].value = host_val;
2819 static bool update_transition_efer(struct vcpu_vmx *vmx, int efer_offset)
2821 u64 guest_efer = vmx->vcpu.arch.efer;
2822 u64 ignore_bits = 0;
2824 /* Shadow paging assumes NX to be available. */
2826 guest_efer |= EFER_NX;
2829 * LMA and LME handled by hardware; SCE meaningless outside long mode.
2831 ignore_bits |= EFER_SCE;
2832 #ifdef CONFIG_X86_64
2833 ignore_bits |= EFER_LMA | EFER_LME;
2834 /* SCE is meaningful only in long mode on Intel */
2835 if (guest_efer & EFER_LMA)
2836 ignore_bits &= ~(u64)EFER_SCE;
2839 clear_atomic_switch_msr(vmx, MSR_EFER);
2842 * On EPT, we can't emulate NX, so we must switch EFER atomically.
2843 * On CPUs that support "load IA32_EFER", always switch EFER
2844 * atomically, since it's faster than switching it manually.
2846 if (cpu_has_load_ia32_efer ||
2847 (enable_ept && ((vmx->vcpu.arch.efer ^ host_efer) & EFER_NX))) {
2848 if (!(guest_efer & EFER_LMA))
2849 guest_efer &= ~EFER_LME;
2850 if (guest_efer != host_efer)
2851 add_atomic_switch_msr(vmx, MSR_EFER,
2852 guest_efer, host_efer, false);
2855 guest_efer &= ~ignore_bits;
2856 guest_efer |= host_efer & ignore_bits;
2858 vmx->guest_msrs[efer_offset].data = guest_efer;
2859 vmx->guest_msrs[efer_offset].mask = ~ignore_bits;
2865 #ifdef CONFIG_X86_32
2867 * On 32-bit kernels, VM exits still load the FS and GS bases from the
2868 * VMCS rather than the segment table. KVM uses this helper to figure
2869 * out the current bases to poke them into the VMCS before entry.
2871 static unsigned long segment_base(u16 selector)
2873 struct desc_struct *table;
2876 if (!(selector & ~SEGMENT_RPL_MASK))
2879 table = get_current_gdt_ro();
2881 if ((selector & SEGMENT_TI_MASK) == SEGMENT_LDT) {
2882 u16 ldt_selector = kvm_read_ldt();
2884 if (!(ldt_selector & ~SEGMENT_RPL_MASK))
2887 table = (struct desc_struct *)segment_base(ldt_selector);
2889 v = get_desc_base(&table[selector >> 3]);
2894 static void vmx_prepare_switch_to_guest(struct kvm_vcpu *vcpu)
2896 struct vcpu_vmx *vmx = to_vmx(vcpu);
2897 struct vmcs_host_state *host_state;
2898 #ifdef CONFIG_X86_64
2899 int cpu = raw_smp_processor_id();
2901 unsigned long fs_base, gs_base;
2905 vmx->req_immediate_exit = false;
2908 * Note that guest MSRs to be saved/restored can also be changed
2909 * when guest state is loaded. This happens when guest transitions
2910 * to/from long-mode by setting MSR_EFER.LMA.
2912 if (!vmx->loaded_cpu_state || vmx->guest_msrs_dirty) {
2913 vmx->guest_msrs_dirty = false;
2914 for (i = 0; i < vmx->save_nmsrs; ++i)
2915 kvm_set_shared_msr(vmx->guest_msrs[i].index,
2916 vmx->guest_msrs[i].data,
2917 vmx->guest_msrs[i].mask);
2921 if (vmx->loaded_cpu_state)
2924 vmx->loaded_cpu_state = vmx->loaded_vmcs;
2925 host_state = &vmx->loaded_cpu_state->host_state;
2928 * Set host fs and gs selectors. Unfortunately, 22.2.3 does not
2929 * allow segment selectors with cpl > 0 or ti == 1.
2931 host_state->ldt_sel = kvm_read_ldt();
2933 #ifdef CONFIG_X86_64
2934 savesegment(ds, host_state->ds_sel);
2935 savesegment(es, host_state->es_sel);
2937 gs_base = cpu_kernelmode_gs_base(cpu);
2938 if (likely(is_64bit_mm(current->mm))) {
2939 save_fsgs_for_kvm();
2940 fs_sel = current->thread.fsindex;
2941 gs_sel = current->thread.gsindex;
2942 fs_base = current->thread.fsbase;
2943 vmx->msr_host_kernel_gs_base = current->thread.gsbase;
2945 savesegment(fs, fs_sel);
2946 savesegment(gs, gs_sel);
2947 fs_base = read_msr(MSR_FS_BASE);
2948 vmx->msr_host_kernel_gs_base = read_msr(MSR_KERNEL_GS_BASE);
2951 wrmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base);
2953 savesegment(fs, fs_sel);
2954 savesegment(gs, gs_sel);
2955 fs_base = segment_base(fs_sel);
2956 gs_base = segment_base(gs_sel);
2959 if (unlikely(fs_sel != host_state->fs_sel)) {
2961 vmcs_write16(HOST_FS_SELECTOR, fs_sel);
2963 vmcs_write16(HOST_FS_SELECTOR, 0);
2964 host_state->fs_sel = fs_sel;
2966 if (unlikely(gs_sel != host_state->gs_sel)) {
2968 vmcs_write16(HOST_GS_SELECTOR, gs_sel);
2970 vmcs_write16(HOST_GS_SELECTOR, 0);
2971 host_state->gs_sel = gs_sel;
2973 if (unlikely(fs_base != host_state->fs_base)) {
2974 vmcs_writel(HOST_FS_BASE, fs_base);
2975 host_state->fs_base = fs_base;
2977 if (unlikely(gs_base != host_state->gs_base)) {
2978 vmcs_writel(HOST_GS_BASE, gs_base);
2979 host_state->gs_base = gs_base;
2983 static void vmx_prepare_switch_to_host(struct vcpu_vmx *vmx)
2985 struct vmcs_host_state *host_state;
2987 if (!vmx->loaded_cpu_state)
2990 WARN_ON_ONCE(vmx->loaded_cpu_state != vmx->loaded_vmcs);
2991 host_state = &vmx->loaded_cpu_state->host_state;
2993 ++vmx->vcpu.stat.host_state_reload;
2994 vmx->loaded_cpu_state = NULL;
2996 #ifdef CONFIG_X86_64
2997 rdmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base);
2999 if (host_state->ldt_sel || (host_state->gs_sel & 7)) {
3000 kvm_load_ldt(host_state->ldt_sel);
3001 #ifdef CONFIG_X86_64
3002 load_gs_index(host_state->gs_sel);
3004 loadsegment(gs, host_state->gs_sel);
3007 if (host_state->fs_sel & 7)
3008 loadsegment(fs, host_state->fs_sel);
3009 #ifdef CONFIG_X86_64
3010 if (unlikely(host_state->ds_sel | host_state->es_sel)) {
3011 loadsegment(ds, host_state->ds_sel);
3012 loadsegment(es, host_state->es_sel);
3015 invalidate_tss_limit();
3016 #ifdef CONFIG_X86_64
3017 wrmsrl(MSR_KERNEL_GS_BASE, vmx->msr_host_kernel_gs_base);
3019 load_fixmap_gdt(raw_smp_processor_id());
3022 #ifdef CONFIG_X86_64
3023 static u64 vmx_read_guest_kernel_gs_base(struct vcpu_vmx *vmx)
3026 if (vmx->loaded_cpu_state)
3027 rdmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base);
3029 return vmx->msr_guest_kernel_gs_base;
3032 static void vmx_write_guest_kernel_gs_base(struct vcpu_vmx *vmx, u64 data)
3035 if (vmx->loaded_cpu_state)
3036 wrmsrl(MSR_KERNEL_GS_BASE, data);
3038 vmx->msr_guest_kernel_gs_base = data;
3042 static void vmx_vcpu_pi_load(struct kvm_vcpu *vcpu, int cpu)
3044 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
3045 struct pi_desc old, new;
3049 * In case of hot-plug or hot-unplug, we may have to undo
3050 * vmx_vcpu_pi_put even if there is no assigned device. And we
3051 * always keep PI.NDST up to date for simplicity: it makes the
3052 * code easier, and CPU migration is not a fast path.
3054 if (!pi_test_sn(pi_desc) && vcpu->cpu == cpu)
3058 * First handle the simple case where no cmpxchg is necessary; just
3059 * allow posting non-urgent interrupts.
3061 * If the 'nv' field is POSTED_INTR_WAKEUP_VECTOR, do not change
3062 * PI.NDST: pi_post_block will do it for us and the wakeup_handler
3063 * expects the VCPU to be on the blocked_vcpu_list that matches
3066 if (pi_desc->nv == POSTED_INTR_WAKEUP_VECTOR ||
3068 pi_clear_sn(pi_desc);
3072 /* The full case. */
3074 old.control = new.control = pi_desc->control;
3076 dest = cpu_physical_id(cpu);
3078 if (x2apic_enabled())
3081 new.ndst = (dest << 8) & 0xFF00;
3084 } while (cmpxchg64(&pi_desc->control, old.control,
3085 new.control) != old.control);
3088 static void decache_tsc_multiplier(struct vcpu_vmx *vmx)
3090 vmx->current_tsc_ratio = vmx->vcpu.arch.tsc_scaling_ratio;
3091 vmcs_write64(TSC_MULTIPLIER, vmx->current_tsc_ratio);
3095 * Switches to specified vcpu, until a matching vcpu_put(), but assumes
3096 * vcpu mutex is already taken.
3098 static void vmx_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
3100 struct vcpu_vmx *vmx = to_vmx(vcpu);
3101 bool already_loaded = vmx->loaded_vmcs->cpu == cpu;
3103 if (!already_loaded) {
3104 loaded_vmcs_clear(vmx->loaded_vmcs);
3105 local_irq_disable();
3108 * Ensure loaded_vmcs->cpu is read before adding loaded_vmcs to
3109 * this cpu's percpu list, otherwise it may not yet be deleted
3110 * from its previous cpu's percpu list. Pairs with the
3111 * smb_wmb() in __loaded_vmcs_clear().
3115 list_add(&vmx->loaded_vmcs->loaded_vmcss_on_cpu_link,
3116 &per_cpu(loaded_vmcss_on_cpu, cpu));
3120 if (per_cpu(current_vmcs, cpu) != vmx->loaded_vmcs->vmcs) {
3121 per_cpu(current_vmcs, cpu) = vmx->loaded_vmcs->vmcs;
3122 vmcs_load(vmx->loaded_vmcs->vmcs);
3123 indirect_branch_prediction_barrier();
3126 if (!already_loaded) {
3127 void *gdt = get_current_gdt_ro();
3128 unsigned long sysenter_esp;
3130 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
3133 * Linux uses per-cpu TSS and GDT, so set these when switching
3134 * processors. See 22.2.4.
3136 vmcs_writel(HOST_TR_BASE,
3137 (unsigned long)&get_cpu_entry_area(cpu)->tss.x86_tss);
3138 vmcs_writel(HOST_GDTR_BASE, (unsigned long)gdt); /* 22.2.4 */
3141 * VM exits change the host TR limit to 0x67 after a VM
3142 * exit. This is okay, since 0x67 covers everything except
3143 * the IO bitmap and have have code to handle the IO bitmap
3144 * being lost after a VM exit.
3146 BUILD_BUG_ON(IO_BITMAP_OFFSET - 1 != 0x67);
3148 rdmsrl(MSR_IA32_SYSENTER_ESP, sysenter_esp);
3149 vmcs_writel(HOST_IA32_SYSENTER_ESP, sysenter_esp); /* 22.2.3 */
3151 vmx->loaded_vmcs->cpu = cpu;
3154 /* Setup TSC multiplier */
3155 if (kvm_has_tsc_control &&
3156 vmx->current_tsc_ratio != vcpu->arch.tsc_scaling_ratio)
3157 decache_tsc_multiplier(vmx);
3159 vmx_vcpu_pi_load(vcpu, cpu);
3160 vmx->host_pkru = read_pkru();
3161 vmx->host_debugctlmsr = get_debugctlmsr();
3164 static void vmx_vcpu_pi_put(struct kvm_vcpu *vcpu)
3166 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
3168 if (!kvm_arch_has_assigned_device(vcpu->kvm) ||
3169 !irq_remapping_cap(IRQ_POSTING_CAP) ||
3170 !kvm_vcpu_apicv_active(vcpu))
3173 /* Set SN when the vCPU is preempted */
3174 if (vcpu->preempted)
3178 static void vmx_vcpu_put(struct kvm_vcpu *vcpu)
3180 vmx_vcpu_pi_put(vcpu);
3182 vmx_prepare_switch_to_host(to_vmx(vcpu));
3185 static bool emulation_required(struct kvm_vcpu *vcpu)
3187 return emulate_invalid_guest_state && !guest_state_valid(vcpu);
3190 static void vmx_decache_cr0_guest_bits(struct kvm_vcpu *vcpu);
3193 * Return the cr0 value that a nested guest would read. This is a combination
3194 * of the real cr0 used to run the guest (guest_cr0), and the bits shadowed by
3195 * its hypervisor (cr0_read_shadow).
3197 static inline unsigned long nested_read_cr0(struct vmcs12 *fields)
3199 return (fields->guest_cr0 & ~fields->cr0_guest_host_mask) |
3200 (fields->cr0_read_shadow & fields->cr0_guest_host_mask);
3202 static inline unsigned long nested_read_cr4(struct vmcs12 *fields)
3204 return (fields->guest_cr4 & ~fields->cr4_guest_host_mask) |
3205 (fields->cr4_read_shadow & fields->cr4_guest_host_mask);
3208 static unsigned long vmx_get_rflags(struct kvm_vcpu *vcpu)
3210 unsigned long rflags, save_rflags;
3212 if (!test_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail)) {
3213 __set_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail);
3214 rflags = vmcs_readl(GUEST_RFLAGS);
3215 if (to_vmx(vcpu)->rmode.vm86_active) {
3216 rflags &= RMODE_GUEST_OWNED_EFLAGS_BITS;
3217 save_rflags = to_vmx(vcpu)->rmode.save_rflags;
3218 rflags |= save_rflags & ~RMODE_GUEST_OWNED_EFLAGS_BITS;
3220 to_vmx(vcpu)->rflags = rflags;
3222 return to_vmx(vcpu)->rflags;
3225 static void vmx_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags)
3227 unsigned long old_rflags = vmx_get_rflags(vcpu);
3229 __set_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail);
3230 to_vmx(vcpu)->rflags = rflags;
3231 if (to_vmx(vcpu)->rmode.vm86_active) {
3232 to_vmx(vcpu)->rmode.save_rflags = rflags;
3233 rflags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM;
3235 vmcs_writel(GUEST_RFLAGS, rflags);
3237 if ((old_rflags ^ to_vmx(vcpu)->rflags) & X86_EFLAGS_VM)
3238 to_vmx(vcpu)->emulation_required = emulation_required(vcpu);
3241 static u32 vmx_get_interrupt_shadow(struct kvm_vcpu *vcpu)
3243 u32 interruptibility = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
3246 if (interruptibility & GUEST_INTR_STATE_STI)
3247 ret |= KVM_X86_SHADOW_INT_STI;
3248 if (interruptibility & GUEST_INTR_STATE_MOV_SS)
3249 ret |= KVM_X86_SHADOW_INT_MOV_SS;
3254 static void vmx_set_interrupt_shadow(struct kvm_vcpu *vcpu, int mask)
3256 u32 interruptibility_old = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
3257 u32 interruptibility = interruptibility_old;
3259 interruptibility &= ~(GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS);
3261 if (mask & KVM_X86_SHADOW_INT_MOV_SS)
3262 interruptibility |= GUEST_INTR_STATE_MOV_SS;
3263 else if (mask & KVM_X86_SHADOW_INT_STI)
3264 interruptibility |= GUEST_INTR_STATE_STI;
3266 if ((interruptibility != interruptibility_old))
3267 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, interruptibility);
3270 static void skip_emulated_instruction(struct kvm_vcpu *vcpu)
3274 rip = kvm_rip_read(vcpu);
3275 rip += vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
3276 kvm_rip_write(vcpu, rip);
3278 /* skipping an emulated instruction also counts */
3279 vmx_set_interrupt_shadow(vcpu, 0);
3282 static void nested_vmx_inject_exception_vmexit(struct kvm_vcpu *vcpu,
3283 unsigned long exit_qual)
3285 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
3286 unsigned int nr = vcpu->arch.exception.nr;
3287 u32 intr_info = nr | INTR_INFO_VALID_MASK;
3289 if (vcpu->arch.exception.has_error_code) {
3290 vmcs12->vm_exit_intr_error_code = vcpu->arch.exception.error_code;
3291 intr_info |= INTR_INFO_DELIVER_CODE_MASK;
3294 if (kvm_exception_is_soft(nr))
3295 intr_info |= INTR_TYPE_SOFT_EXCEPTION;
3297 intr_info |= INTR_TYPE_HARD_EXCEPTION;
3299 if (!(vmcs12->idt_vectoring_info_field & VECTORING_INFO_VALID_MASK) &&
3300 vmx_get_nmi_mask(vcpu))
3301 intr_info |= INTR_INFO_UNBLOCK_NMI;
3303 nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI, intr_info, exit_qual);
3307 * KVM wants to inject page-faults which it got to the guest. This function
3308 * checks whether in a nested guest, we need to inject them to L1 or L2.
3310 static int nested_vmx_check_exception(struct kvm_vcpu *vcpu, unsigned long *exit_qual)
3312 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
3313 unsigned int nr = vcpu->arch.exception.nr;
3315 if (nr == PF_VECTOR) {
3316 if (vcpu->arch.exception.nested_apf) {
3317 *exit_qual = vcpu->arch.apf.nested_apf_token;
3321 * FIXME: we must not write CR2 when L1 intercepts an L2 #PF exception.
3322 * The fix is to add the ancillary datum (CR2 or DR6) to structs
3323 * kvm_queued_exception and kvm_vcpu_events, so that CR2 and DR6
3324 * can be written only when inject_pending_event runs. This should be
3325 * conditional on a new capability---if the capability is disabled,
3326 * kvm_multiple_exception would write the ancillary information to
3327 * CR2 or DR6, for backwards ABI-compatibility.
3329 if (nested_vmx_is_page_fault_vmexit(vmcs12,
3330 vcpu->arch.exception.error_code)) {
3331 *exit_qual = vcpu->arch.cr2;
3335 if (vmcs12->exception_bitmap & (1u << nr)) {
3336 if (nr == DB_VECTOR) {
3337 *exit_qual = vcpu->arch.dr6;
3338 *exit_qual &= ~(DR6_FIXED_1 | DR6_BT);
3339 *exit_qual ^= DR6_RTM;
3350 static void vmx_clear_hlt(struct kvm_vcpu *vcpu)
3353 * Ensure that we clear the HLT state in the VMCS. We don't need to
3354 * explicitly skip the instruction because if the HLT state is set,
3355 * then the instruction is already executing and RIP has already been
3358 if (kvm_hlt_in_guest(vcpu->kvm) &&
3359 vmcs_read32(GUEST_ACTIVITY_STATE) == GUEST_ACTIVITY_HLT)
3360 vmcs_write32(GUEST_ACTIVITY_STATE, GUEST_ACTIVITY_ACTIVE);
3363 static void vmx_queue_exception(struct kvm_vcpu *vcpu)
3365 struct vcpu_vmx *vmx = to_vmx(vcpu);
3366 unsigned nr = vcpu->arch.exception.nr;
3367 bool has_error_code = vcpu->arch.exception.has_error_code;
3368 u32 error_code = vcpu->arch.exception.error_code;
3369 u32 intr_info = nr | INTR_INFO_VALID_MASK;
3371 if (has_error_code) {
3372 vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE, error_code);
3373 intr_info |= INTR_INFO_DELIVER_CODE_MASK;
3376 if (vmx->rmode.vm86_active) {
3378 if (kvm_exception_is_soft(nr))
3379 inc_eip = vcpu->arch.event_exit_inst_len;
3380 if (kvm_inject_realmode_interrupt(vcpu, nr, inc_eip) != EMULATE_DONE)
3381 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
3385 WARN_ON_ONCE(vmx->emulation_required);
3387 if (kvm_exception_is_soft(nr)) {
3388 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
3389 vmx->vcpu.arch.event_exit_inst_len);
3390 intr_info |= INTR_TYPE_SOFT_EXCEPTION;
3392 intr_info |= INTR_TYPE_HARD_EXCEPTION;
3394 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr_info);
3396 vmx_clear_hlt(vcpu);
3399 static bool vmx_rdtscp_supported(void)
3401 return cpu_has_vmx_rdtscp();
3404 static bool vmx_invpcid_supported(void)
3406 return cpu_has_vmx_invpcid();
3410 * Swap MSR entry in host/guest MSR entry array.
3412 static void move_msr_up(struct vcpu_vmx *vmx, int from, int to)
3414 struct shared_msr_entry tmp;
3416 tmp = vmx->guest_msrs[to];
3417 vmx->guest_msrs[to] = vmx->guest_msrs[from];
3418 vmx->guest_msrs[from] = tmp;
3422 * Set up the vmcs to automatically save and restore system
3423 * msrs. Don't touch the 64-bit msrs if the guest is in legacy
3424 * mode, as fiddling with msrs is very expensive.
3426 static void setup_msrs(struct vcpu_vmx *vmx)
3428 int save_nmsrs, index;
3431 #ifdef CONFIG_X86_64
3432 if (is_long_mode(&vmx->vcpu)) {
3433 index = __find_msr_index(vmx, MSR_SYSCALL_MASK);
3435 move_msr_up(vmx, index, save_nmsrs++);
3436 index = __find_msr_index(vmx, MSR_LSTAR);
3438 move_msr_up(vmx, index, save_nmsrs++);
3439 index = __find_msr_index(vmx, MSR_CSTAR);
3441 move_msr_up(vmx, index, save_nmsrs++);
3443 * MSR_STAR is only needed on long mode guests, and only
3444 * if efer.sce is enabled.
3446 index = __find_msr_index(vmx, MSR_STAR);
3447 if ((index >= 0) && (vmx->vcpu.arch.efer & EFER_SCE))
3448 move_msr_up(vmx, index, save_nmsrs++);
3451 index = __find_msr_index(vmx, MSR_EFER);
3452 if (index >= 0 && update_transition_efer(vmx, index))
3453 move_msr_up(vmx, index, save_nmsrs++);
3454 index = __find_msr_index(vmx, MSR_TSC_AUX);
3455 if (index >= 0 && guest_cpuid_has(&vmx->vcpu, X86_FEATURE_RDTSCP))
3456 move_msr_up(vmx, index, save_nmsrs++);
3458 vmx->save_nmsrs = save_nmsrs;
3459 vmx->guest_msrs_dirty = true;
3461 if (cpu_has_vmx_msr_bitmap())
3462 vmx_update_msr_bitmap(&vmx->vcpu);
3465 static u64 vmx_read_l1_tsc_offset(struct kvm_vcpu *vcpu)
3467 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
3469 if (is_guest_mode(vcpu) &&
3470 (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING))
3471 return vcpu->arch.tsc_offset - vmcs12->tsc_offset;
3473 return vcpu->arch.tsc_offset;
3476 static u64 vmx_write_l1_tsc_offset(struct kvm_vcpu *vcpu, u64 offset)
3478 u64 active_offset = offset;
3479 if (is_guest_mode(vcpu)) {
3481 * We're here if L1 chose not to trap WRMSR to TSC. According
3482 * to the spec, this should set L1's TSC; The offset that L1
3483 * set for L2 remains unchanged, and still needs to be added
3484 * to the newly set TSC to get L2's TSC.
3486 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
3487 if (nested_cpu_has(vmcs12, CPU_BASED_USE_TSC_OFFSETING))
3488 active_offset += vmcs12->tsc_offset;
3490 trace_kvm_write_tsc_offset(vcpu->vcpu_id,
3491 vmcs_read64(TSC_OFFSET), offset);
3494 vmcs_write64(TSC_OFFSET, active_offset);
3495 return active_offset;
3499 * nested_vmx_allowed() checks whether a guest should be allowed to use VMX
3500 * instructions and MSRs (i.e., nested VMX). Nested VMX is disabled for
3501 * all guests if the "nested" module option is off, and can also be disabled
3502 * for a single guest by disabling its VMX cpuid bit.
3504 static inline bool nested_vmx_allowed(struct kvm_vcpu *vcpu)
3506 return nested && guest_cpuid_has(vcpu, X86_FEATURE_VMX);
3510 * nested_vmx_setup_ctls_msrs() sets up variables containing the values to be
3511 * returned for the various VMX controls MSRs when nested VMX is enabled.
3512 * The same values should also be used to verify that vmcs12 control fields are
3513 * valid during nested entry from L1 to L2.
3514 * Each of these control msrs has a low and high 32-bit half: A low bit is on
3515 * if the corresponding bit in the (32-bit) control field *must* be on, and a
3516 * bit in the high half is on if the corresponding bit in the control field
3517 * may be on. See also vmx_control_verify().
3519 static void nested_vmx_setup_ctls_msrs(struct nested_vmx_msrs *msrs, bool apicv)
3522 memset(msrs, 0, sizeof(*msrs));
3527 * Note that as a general rule, the high half of the MSRs (bits in
3528 * the control fields which may be 1) should be initialized by the
3529 * intersection of the underlying hardware's MSR (i.e., features which
3530 * can be supported) and the list of features we want to expose -
3531 * because they are known to be properly supported in our code.
3532 * Also, usually, the low half of the MSRs (bits which must be 1) can
3533 * be set to 0, meaning that L1 may turn off any of these bits. The
3534 * reason is that if one of these bits is necessary, it will appear
3535 * in vmcs01 and prepare_vmcs02, when it bitwise-or's the control
3536 * fields of vmcs01 and vmcs02, will turn these bits off - and
3537 * nested_vmx_exit_reflected() will not pass related exits to L1.
3538 * These rules have exceptions below.
3541 /* pin-based controls */
3542 rdmsr(MSR_IA32_VMX_PINBASED_CTLS,
3543 msrs->pinbased_ctls_low,
3544 msrs->pinbased_ctls_high);
3545 msrs->pinbased_ctls_low |=
3546 PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
3547 msrs->pinbased_ctls_high &=
3548 PIN_BASED_EXT_INTR_MASK |
3549 PIN_BASED_NMI_EXITING |
3550 PIN_BASED_VIRTUAL_NMIS |
3551 (apicv ? PIN_BASED_POSTED_INTR : 0);
3552 msrs->pinbased_ctls_high |=
3553 PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR |
3554 PIN_BASED_VMX_PREEMPTION_TIMER;
3557 rdmsr(MSR_IA32_VMX_EXIT_CTLS,
3558 msrs->exit_ctls_low,
3559 msrs->exit_ctls_high);
3560 msrs->exit_ctls_low =
3561 VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR;
3563 msrs->exit_ctls_high &=
3564 #ifdef CONFIG_X86_64
3565 VM_EXIT_HOST_ADDR_SPACE_SIZE |
3567 VM_EXIT_LOAD_IA32_PAT | VM_EXIT_SAVE_IA32_PAT;
3568 msrs->exit_ctls_high |=
3569 VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR |
3570 VM_EXIT_LOAD_IA32_EFER | VM_EXIT_SAVE_IA32_EFER |
3571 VM_EXIT_SAVE_VMX_PREEMPTION_TIMER | VM_EXIT_ACK_INTR_ON_EXIT;
3573 /* We support free control of debug control saving. */
3574 msrs->exit_ctls_low &= ~VM_EXIT_SAVE_DEBUG_CONTROLS;
3576 /* entry controls */
3577 rdmsr(MSR_IA32_VMX_ENTRY_CTLS,
3578 msrs->entry_ctls_low,
3579 msrs->entry_ctls_high);
3580 msrs->entry_ctls_low =
3581 VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR;
3582 msrs->entry_ctls_high &=
3583 #ifdef CONFIG_X86_64
3584 VM_ENTRY_IA32E_MODE |
3586 VM_ENTRY_LOAD_IA32_PAT;
3587 msrs->entry_ctls_high |=
3588 (VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR | VM_ENTRY_LOAD_IA32_EFER);
3590 /* We support free control of debug control loading. */
3591 msrs->entry_ctls_low &= ~VM_ENTRY_LOAD_DEBUG_CONTROLS;
3593 /* cpu-based controls */
3594 rdmsr(MSR_IA32_VMX_PROCBASED_CTLS,
3595 msrs->procbased_ctls_low,
3596 msrs->procbased_ctls_high);
3597 msrs->procbased_ctls_low =
3598 CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
3599 msrs->procbased_ctls_high &=
3600 CPU_BASED_VIRTUAL_INTR_PENDING |
3601 CPU_BASED_VIRTUAL_NMI_PENDING | CPU_BASED_USE_TSC_OFFSETING |
3602 CPU_BASED_HLT_EXITING | CPU_BASED_INVLPG_EXITING |
3603 CPU_BASED_MWAIT_EXITING | CPU_BASED_CR3_LOAD_EXITING |
3604 CPU_BASED_CR3_STORE_EXITING |
3605 #ifdef CONFIG_X86_64
3606 CPU_BASED_CR8_LOAD_EXITING | CPU_BASED_CR8_STORE_EXITING |
3608 CPU_BASED_MOV_DR_EXITING | CPU_BASED_UNCOND_IO_EXITING |
3609 CPU_BASED_USE_IO_BITMAPS | CPU_BASED_MONITOR_TRAP_FLAG |
3610 CPU_BASED_MONITOR_EXITING | CPU_BASED_RDPMC_EXITING |
3611 CPU_BASED_RDTSC_EXITING | CPU_BASED_PAUSE_EXITING |
3612 CPU_BASED_TPR_SHADOW | CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
3614 * We can allow some features even when not supported by the
3615 * hardware. For example, L1 can specify an MSR bitmap - and we
3616 * can use it to avoid exits to L1 - even when L0 runs L2
3617 * without MSR bitmaps.
3619 msrs->procbased_ctls_high |=
3620 CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR |
3621 CPU_BASED_USE_MSR_BITMAPS;
3623 /* We support free control of CR3 access interception. */
3624 msrs->procbased_ctls_low &=
3625 ~(CPU_BASED_CR3_LOAD_EXITING | CPU_BASED_CR3_STORE_EXITING);
3628 * secondary cpu-based controls. Do not include those that
3629 * depend on CPUID bits, they are added later by vmx_cpuid_update.
3631 if (msrs->procbased_ctls_high & CPU_BASED_ACTIVATE_SECONDARY_CONTROLS)
3632 rdmsr(MSR_IA32_VMX_PROCBASED_CTLS2,
3633 msrs->secondary_ctls_low,
3634 msrs->secondary_ctls_high);
3636 msrs->secondary_ctls_low = 0;
3637 msrs->secondary_ctls_high &=
3638 SECONDARY_EXEC_DESC |
3639 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
3640 SECONDARY_EXEC_APIC_REGISTER_VIRT |
3641 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY |
3642 SECONDARY_EXEC_WBINVD_EXITING;
3645 * We can emulate "VMCS shadowing," even if the hardware
3646 * doesn't support it.
3648 msrs->secondary_ctls_high |=
3649 SECONDARY_EXEC_SHADOW_VMCS;
3652 /* nested EPT: emulate EPT also to L1 */
3653 msrs->secondary_ctls_high |=
3654 SECONDARY_EXEC_ENABLE_EPT;
3655 msrs->ept_caps = VMX_EPT_PAGE_WALK_4_BIT |
3656 VMX_EPTP_WB_BIT | VMX_EPT_INVEPT_BIT;
3657 if (cpu_has_vmx_ept_execute_only())
3659 VMX_EPT_EXECUTE_ONLY_BIT;
3660 msrs->ept_caps &= vmx_capability.ept;
3661 msrs->ept_caps |= VMX_EPT_EXTENT_GLOBAL_BIT |
3662 VMX_EPT_EXTENT_CONTEXT_BIT | VMX_EPT_2MB_PAGE_BIT |
3663 VMX_EPT_1GB_PAGE_BIT;
3664 if (enable_ept_ad_bits) {
3665 msrs->secondary_ctls_high |=
3666 SECONDARY_EXEC_ENABLE_PML;
3667 msrs->ept_caps |= VMX_EPT_AD_BIT;
3671 if (cpu_has_vmx_vmfunc()) {
3672 msrs->secondary_ctls_high |=
3673 SECONDARY_EXEC_ENABLE_VMFUNC;
3675 * Advertise EPTP switching unconditionally
3676 * since we emulate it
3679 msrs->vmfunc_controls =
3680 VMX_VMFUNC_EPTP_SWITCHING;
3684 * Old versions of KVM use the single-context version without
3685 * checking for support, so declare that it is supported even
3686 * though it is treated as global context. The alternative is
3687 * not failing the single-context invvpid, and it is worse.
3690 msrs->secondary_ctls_high |=
3691 SECONDARY_EXEC_ENABLE_VPID;
3692 msrs->vpid_caps = VMX_VPID_INVVPID_BIT |
3693 VMX_VPID_EXTENT_SUPPORTED_MASK;
3696 if (enable_unrestricted_guest)
3697 msrs->secondary_ctls_high |=
3698 SECONDARY_EXEC_UNRESTRICTED_GUEST;
3700 if (flexpriority_enabled)
3701 msrs->secondary_ctls_high |=
3702 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
3704 /* miscellaneous data */
3705 rdmsr(MSR_IA32_VMX_MISC,
3708 msrs->misc_low &= VMX_MISC_SAVE_EFER_LMA;
3710 MSR_IA32_VMX_MISC_VMWRITE_SHADOW_RO_FIELDS |
3711 VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE |
3712 VMX_MISC_ACTIVITY_HLT;
3713 msrs->misc_high = 0;
3716 * This MSR reports some information about VMX support. We
3717 * should return information about the VMX we emulate for the
3718 * guest, and the VMCS structure we give it - not about the
3719 * VMX support of the underlying hardware.
3723 VMX_BASIC_TRUE_CTLS |
3724 ((u64)VMCS12_SIZE << VMX_BASIC_VMCS_SIZE_SHIFT) |
3725 (VMX_BASIC_MEM_TYPE_WB << VMX_BASIC_MEM_TYPE_SHIFT);
3727 if (cpu_has_vmx_basic_inout())
3728 msrs->basic |= VMX_BASIC_INOUT;
3731 * These MSRs specify bits which the guest must keep fixed on
3732 * while L1 is in VMXON mode (in L1's root mode, or running an L2).
3733 * We picked the standard core2 setting.
3735 #define VMXON_CR0_ALWAYSON (X86_CR0_PE | X86_CR0_PG | X86_CR0_NE)
3736 #define VMXON_CR4_ALWAYSON X86_CR4_VMXE
3737 msrs->cr0_fixed0 = VMXON_CR0_ALWAYSON;
3738 msrs->cr4_fixed0 = VMXON_CR4_ALWAYSON;
3740 /* These MSRs specify bits which the guest must keep fixed off. */
3741 rdmsrl(MSR_IA32_VMX_CR0_FIXED1, msrs->cr0_fixed1);
3742 rdmsrl(MSR_IA32_VMX_CR4_FIXED1, msrs->cr4_fixed1);
3744 /* highest index: VMX_PREEMPTION_TIMER_VALUE */
3745 msrs->vmcs_enum = VMCS12_MAX_FIELD_INDEX << 1;
3749 * if fixed0[i] == 1: val[i] must be 1
3750 * if fixed1[i] == 0: val[i] must be 0
3752 static inline bool fixed_bits_valid(u64 val, u64 fixed0, u64 fixed1)
3754 return ((val & fixed1) | fixed0) == val;
3757 static inline bool vmx_control_verify(u32 control, u32 low, u32 high)
3759 return fixed_bits_valid(control, low, high);
3762 static inline u64 vmx_control_msr(u32 low, u32 high)
3764 return low | ((u64)high << 32);
3767 static bool is_bitwise_subset(u64 superset, u64 subset, u64 mask)
3772 return (superset | subset) == superset;
3775 static int vmx_restore_vmx_basic(struct vcpu_vmx *vmx, u64 data)
3777 const u64 feature_and_reserved =
3778 /* feature (except bit 48; see below) */
3779 BIT_ULL(49) | BIT_ULL(54) | BIT_ULL(55) |
3781 BIT_ULL(31) | GENMASK_ULL(47, 45) | GENMASK_ULL(63, 56);
3782 u64 vmx_basic = vmx->nested.msrs.basic;
3784 if (!is_bitwise_subset(vmx_basic, data, feature_and_reserved))
3788 * KVM does not emulate a version of VMX that constrains physical
3789 * addresses of VMX structures (e.g. VMCS) to 32-bits.
3791 if (data & BIT_ULL(48))
3794 if (vmx_basic_vmcs_revision_id(vmx_basic) !=
3795 vmx_basic_vmcs_revision_id(data))
3798 if (vmx_basic_vmcs_size(vmx_basic) > vmx_basic_vmcs_size(data))
3801 vmx->nested.msrs.basic = data;
3806 vmx_restore_control_msr(struct vcpu_vmx *vmx, u32 msr_index, u64 data)
3811 switch (msr_index) {
3812 case MSR_IA32_VMX_TRUE_PINBASED_CTLS:
3813 lowp = &vmx->nested.msrs.pinbased_ctls_low;
3814 highp = &vmx->nested.msrs.pinbased_ctls_high;
3816 case MSR_IA32_VMX_TRUE_PROCBASED_CTLS:
3817 lowp = &vmx->nested.msrs.procbased_ctls_low;
3818 highp = &vmx->nested.msrs.procbased_ctls_high;
3820 case MSR_IA32_VMX_TRUE_EXIT_CTLS:
3821 lowp = &vmx->nested.msrs.exit_ctls_low;
3822 highp = &vmx->nested.msrs.exit_ctls_high;
3824 case MSR_IA32_VMX_TRUE_ENTRY_CTLS:
3825 lowp = &vmx->nested.msrs.entry_ctls_low;
3826 highp = &vmx->nested.msrs.entry_ctls_high;
3828 case MSR_IA32_VMX_PROCBASED_CTLS2:
3829 lowp = &vmx->nested.msrs.secondary_ctls_low;
3830 highp = &vmx->nested.msrs.secondary_ctls_high;
3836 supported = vmx_control_msr(*lowp, *highp);
3838 /* Check must-be-1 bits are still 1. */
3839 if (!is_bitwise_subset(data, supported, GENMASK_ULL(31, 0)))
3842 /* Check must-be-0 bits are still 0. */
3843 if (!is_bitwise_subset(supported, data, GENMASK_ULL(63, 32)))
3847 *highp = data >> 32;
3851 static int vmx_restore_vmx_misc(struct vcpu_vmx *vmx, u64 data)
3853 const u64 feature_and_reserved_bits =
3855 BIT_ULL(5) | GENMASK_ULL(8, 6) | BIT_ULL(14) | BIT_ULL(15) |
3856 BIT_ULL(28) | BIT_ULL(29) | BIT_ULL(30) |
3858 GENMASK_ULL(13, 9) | BIT_ULL(31);
3861 vmx_misc = vmx_control_msr(vmx->nested.msrs.misc_low,
3862 vmx->nested.msrs.misc_high);
3864 if (!is_bitwise_subset(vmx_misc, data, feature_and_reserved_bits))
3867 if ((vmx->nested.msrs.pinbased_ctls_high &
3868 PIN_BASED_VMX_PREEMPTION_TIMER) &&
3869 vmx_misc_preemption_timer_rate(data) !=
3870 vmx_misc_preemption_timer_rate(vmx_misc))
3873 if (vmx_misc_cr3_count(data) > vmx_misc_cr3_count(vmx_misc))
3876 if (vmx_misc_max_msr(data) > vmx_misc_max_msr(vmx_misc))
3879 if (vmx_misc_mseg_revid(data) != vmx_misc_mseg_revid(vmx_misc))
3882 vmx->nested.msrs.misc_low = data;
3883 vmx->nested.msrs.misc_high = data >> 32;
3886 * If L1 has read-only VM-exit information fields, use the
3887 * less permissive vmx_vmwrite_bitmap to specify write
3888 * permissions for the shadow VMCS.
3890 if (enable_shadow_vmcs && !nested_cpu_has_vmwrite_any_field(&vmx->vcpu))
3891 vmcs_write64(VMWRITE_BITMAP, __pa(vmx_vmwrite_bitmap));
3896 static int vmx_restore_vmx_ept_vpid_cap(struct vcpu_vmx *vmx, u64 data)
3898 u64 vmx_ept_vpid_cap;
3900 vmx_ept_vpid_cap = vmx_control_msr(vmx->nested.msrs.ept_caps,
3901 vmx->nested.msrs.vpid_caps);
3903 /* Every bit is either reserved or a feature bit. */
3904 if (!is_bitwise_subset(vmx_ept_vpid_cap, data, -1ULL))
3907 vmx->nested.msrs.ept_caps = data;
3908 vmx->nested.msrs.vpid_caps = data >> 32;
3912 static int vmx_restore_fixed0_msr(struct vcpu_vmx *vmx, u32 msr_index, u64 data)
3916 switch (msr_index) {
3917 case MSR_IA32_VMX_CR0_FIXED0:
3918 msr = &vmx->nested.msrs.cr0_fixed0;
3920 case MSR_IA32_VMX_CR4_FIXED0:
3921 msr = &vmx->nested.msrs.cr4_fixed0;
3928 * 1 bits (which indicates bits which "must-be-1" during VMX operation)
3929 * must be 1 in the restored value.
3931 if (!is_bitwise_subset(data, *msr, -1ULL))
3939 * Called when userspace is restoring VMX MSRs.
3941 * Returns 0 on success, non-0 otherwise.
3943 static int vmx_set_vmx_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 data)
3945 struct vcpu_vmx *vmx = to_vmx(vcpu);
3948 * Don't allow changes to the VMX capability MSRs while the vCPU
3949 * is in VMX operation.
3951 if (vmx->nested.vmxon)
3954 switch (msr_index) {
3955 case MSR_IA32_VMX_BASIC:
3956 return vmx_restore_vmx_basic(vmx, data);
3957 case MSR_IA32_VMX_PINBASED_CTLS:
3958 case MSR_IA32_VMX_PROCBASED_CTLS:
3959 case MSR_IA32_VMX_EXIT_CTLS:
3960 case MSR_IA32_VMX_ENTRY_CTLS:
3962 * The "non-true" VMX capability MSRs are generated from the
3963 * "true" MSRs, so we do not support restoring them directly.
3965 * If userspace wants to emulate VMX_BASIC[55]=0, userspace
3966 * should restore the "true" MSRs with the must-be-1 bits
3967 * set according to the SDM Vol 3. A.2 "RESERVED CONTROLS AND
3968 * DEFAULT SETTINGS".
3971 case MSR_IA32_VMX_TRUE_PINBASED_CTLS:
3972 case MSR_IA32_VMX_TRUE_PROCBASED_CTLS:
3973 case MSR_IA32_VMX_TRUE_EXIT_CTLS:
3974 case MSR_IA32_VMX_TRUE_ENTRY_CTLS:
3975 case MSR_IA32_VMX_PROCBASED_CTLS2:
3976 return vmx_restore_control_msr(vmx, msr_index, data);
3977 case MSR_IA32_VMX_MISC:
3978 return vmx_restore_vmx_misc(vmx, data);
3979 case MSR_IA32_VMX_CR0_FIXED0:
3980 case MSR_IA32_VMX_CR4_FIXED0:
3981 return vmx_restore_fixed0_msr(vmx, msr_index, data);
3982 case MSR_IA32_VMX_CR0_FIXED1:
3983 case MSR_IA32_VMX_CR4_FIXED1:
3985 * These MSRs are generated based on the vCPU's CPUID, so we
3986 * do not support restoring them directly.
3989 case MSR_IA32_VMX_EPT_VPID_CAP:
3990 return vmx_restore_vmx_ept_vpid_cap(vmx, data);
3991 case MSR_IA32_VMX_VMCS_ENUM:
3992 vmx->nested.msrs.vmcs_enum = data;
3996 * The rest of the VMX capability MSRs do not support restore.
4002 /* Returns 0 on success, non-0 otherwise. */
4003 static int vmx_get_vmx_msr(struct nested_vmx_msrs *msrs, u32 msr_index, u64 *pdata)
4005 switch (msr_index) {
4006 case MSR_IA32_VMX_BASIC:
4007 *pdata = msrs->basic;
4009 case MSR_IA32_VMX_TRUE_PINBASED_CTLS:
4010 case MSR_IA32_VMX_PINBASED_CTLS:
4011 *pdata = vmx_control_msr(
4012 msrs->pinbased_ctls_low,
4013 msrs->pinbased_ctls_high);
4014 if (msr_index == MSR_IA32_VMX_PINBASED_CTLS)
4015 *pdata |= PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
4017 case MSR_IA32_VMX_TRUE_PROCBASED_CTLS:
4018 case MSR_IA32_VMX_PROCBASED_CTLS:
4019 *pdata = vmx_control_msr(
4020 msrs->procbased_ctls_low,
4021 msrs->procbased_ctls_high);
4022 if (msr_index == MSR_IA32_VMX_PROCBASED_CTLS)
4023 *pdata |= CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
4025 case MSR_IA32_VMX_TRUE_EXIT_CTLS:
4026 case MSR_IA32_VMX_EXIT_CTLS:
4027 *pdata = vmx_control_msr(
4028 msrs->exit_ctls_low,
4029 msrs->exit_ctls_high);
4030 if (msr_index == MSR_IA32_VMX_EXIT_CTLS)
4031 *pdata |= VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR;
4033 case MSR_IA32_VMX_TRUE_ENTRY_CTLS:
4034 case MSR_IA32_VMX_ENTRY_CTLS:
4035 *pdata = vmx_control_msr(
4036 msrs->entry_ctls_low,
4037 msrs->entry_ctls_high);
4038 if (msr_index == MSR_IA32_VMX_ENTRY_CTLS)
4039 *pdata |= VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR;
4041 case MSR_IA32_VMX_MISC:
4042 *pdata = vmx_control_msr(
4046 case MSR_IA32_VMX_CR0_FIXED0:
4047 *pdata = msrs->cr0_fixed0;
4049 case MSR_IA32_VMX_CR0_FIXED1:
4050 *pdata = msrs->cr0_fixed1;
4052 case MSR_IA32_VMX_CR4_FIXED0:
4053 *pdata = msrs->cr4_fixed0;
4055 case MSR_IA32_VMX_CR4_FIXED1:
4056 *pdata = msrs->cr4_fixed1;
4058 case MSR_IA32_VMX_VMCS_ENUM:
4059 *pdata = msrs->vmcs_enum;
4061 case MSR_IA32_VMX_PROCBASED_CTLS2:
4062 *pdata = vmx_control_msr(
4063 msrs->secondary_ctls_low,
4064 msrs->secondary_ctls_high);
4066 case MSR_IA32_VMX_EPT_VPID_CAP:
4067 *pdata = msrs->ept_caps |
4068 ((u64)msrs->vpid_caps << 32);
4070 case MSR_IA32_VMX_VMFUNC:
4071 *pdata = msrs->vmfunc_controls;
4080 static inline bool vmx_feature_control_msr_valid(struct kvm_vcpu *vcpu,
4083 uint64_t valid_bits = to_vmx(vcpu)->msr_ia32_feature_control_valid_bits;
4085 return !(val & ~valid_bits);
4088 static int vmx_get_msr_feature(struct kvm_msr_entry *msr)
4090 switch (msr->index) {
4091 case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC:
4094 return vmx_get_vmx_msr(&vmcs_config.nested, msr->index, &msr->data);
4103 * Reads an msr value (of 'msr_index') into 'pdata'.
4104 * Returns 0 on success, non-0 otherwise.
4105 * Assumes vcpu_load() was already called.
4107 static int vmx_get_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
4109 struct vcpu_vmx *vmx = to_vmx(vcpu);
4110 struct shared_msr_entry *msr;
4112 switch (msr_info->index) {
4113 #ifdef CONFIG_X86_64
4115 msr_info->data = vmcs_readl(GUEST_FS_BASE);
4118 msr_info->data = vmcs_readl(GUEST_GS_BASE);
4120 case MSR_KERNEL_GS_BASE:
4121 msr_info->data = vmx_read_guest_kernel_gs_base(vmx);
4125 return kvm_get_msr_common(vcpu, msr_info);
4126 case MSR_IA32_SPEC_CTRL:
4127 if (!msr_info->host_initiated &&
4128 !guest_has_spec_ctrl_msr(vcpu))
4131 msr_info->data = to_vmx(vcpu)->spec_ctrl;
4133 case MSR_IA32_SYSENTER_CS:
4134 msr_info->data = vmcs_read32(GUEST_SYSENTER_CS);
4136 case MSR_IA32_SYSENTER_EIP:
4137 msr_info->data = vmcs_readl(GUEST_SYSENTER_EIP);
4139 case MSR_IA32_SYSENTER_ESP:
4140 msr_info->data = vmcs_readl(GUEST_SYSENTER_ESP);
4142 case MSR_IA32_BNDCFGS:
4143 if (!kvm_mpx_supported() ||
4144 (!msr_info->host_initiated &&
4145 !guest_cpuid_has(vcpu, X86_FEATURE_MPX)))
4147 msr_info->data = vmcs_read64(GUEST_BNDCFGS);
4149 case MSR_IA32_MCG_EXT_CTL:
4150 if (!msr_info->host_initiated &&
4151 !(vmx->msr_ia32_feature_control &
4152 FEATURE_CONTROL_LMCE))
4154 msr_info->data = vcpu->arch.mcg_ext_ctl;
4156 case MSR_IA32_FEATURE_CONTROL:
4157 msr_info->data = vmx->msr_ia32_feature_control;
4159 case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC:
4160 if (!nested_vmx_allowed(vcpu))
4162 return vmx_get_vmx_msr(&vmx->nested.msrs, msr_info->index,
4165 if (!vmx_xsaves_supported() ||
4166 (!msr_info->host_initiated &&
4167 !(guest_cpuid_has(vcpu, X86_FEATURE_XSAVE) &&
4168 guest_cpuid_has(vcpu, X86_FEATURE_XSAVES))))
4170 msr_info->data = vcpu->arch.ia32_xss;
4173 if (!msr_info->host_initiated &&
4174 !guest_cpuid_has(vcpu, X86_FEATURE_RDTSCP))
4176 /* Otherwise falls through */
4178 msr = find_msr_entry(vmx, msr_info->index);
4180 msr_info->data = msr->data;
4183 return kvm_get_msr_common(vcpu, msr_info);
4189 static void vmx_leave_nested(struct kvm_vcpu *vcpu);
4192 * Writes msr value into into the appropriate "register".
4193 * Returns 0 on success, non-0 otherwise.
4194 * Assumes vcpu_load() was already called.
4196 static int vmx_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
4198 struct vcpu_vmx *vmx = to_vmx(vcpu);
4199 struct shared_msr_entry *msr;
4201 u32 msr_index = msr_info->index;
4202 u64 data = msr_info->data;
4204 switch (msr_index) {
4206 ret = kvm_set_msr_common(vcpu, msr_info);
4208 #ifdef CONFIG_X86_64
4210 vmx_segment_cache_clear(vmx);
4211 vmcs_writel(GUEST_FS_BASE, data);
4214 vmx_segment_cache_clear(vmx);
4215 vmcs_writel(GUEST_GS_BASE, data);
4217 case MSR_KERNEL_GS_BASE:
4218 vmx_write_guest_kernel_gs_base(vmx, data);
4221 case MSR_IA32_SYSENTER_CS:
4222 vmcs_write32(GUEST_SYSENTER_CS, data);
4224 case MSR_IA32_SYSENTER_EIP:
4225 vmcs_writel(GUEST_SYSENTER_EIP, data);
4227 case MSR_IA32_SYSENTER_ESP:
4228 vmcs_writel(GUEST_SYSENTER_ESP, data);
4230 case MSR_IA32_BNDCFGS:
4231 if (!kvm_mpx_supported() ||
4232 (!msr_info->host_initiated &&
4233 !guest_cpuid_has(vcpu, X86_FEATURE_MPX)))
4235 if (is_noncanonical_address(data & PAGE_MASK, vcpu) ||
4236 (data & MSR_IA32_BNDCFGS_RSVD))
4238 vmcs_write64(GUEST_BNDCFGS, data);
4240 case MSR_IA32_SPEC_CTRL:
4241 if (!msr_info->host_initiated &&
4242 !guest_has_spec_ctrl_msr(vcpu))
4245 /* The STIBP bit doesn't fault even if it's not advertised */
4246 if (data & ~(SPEC_CTRL_IBRS | SPEC_CTRL_STIBP | SPEC_CTRL_SSBD))
4249 vmx->spec_ctrl = data;
4256 * When it's written (to non-zero) for the first time, pass
4260 * The handling of the MSR bitmap for L2 guests is done in
4261 * nested_vmx_merge_msr_bitmap. We should not touch the
4262 * vmcs02.msr_bitmap here since it gets completely overwritten
4263 * in the merging. We update the vmcs01 here for L1 as well
4264 * since it will end up touching the MSR anyway now.
4266 vmx_disable_intercept_for_msr(vmx->vmcs01.msr_bitmap,
4270 case MSR_IA32_PRED_CMD:
4271 if (!msr_info->host_initiated &&
4272 !guest_has_pred_cmd_msr(vcpu))
4275 if (data & ~PRED_CMD_IBPB)
4281 wrmsrl(MSR_IA32_PRED_CMD, PRED_CMD_IBPB);
4285 * When it's written (to non-zero) for the first time, pass
4289 * The handling of the MSR bitmap for L2 guests is done in
4290 * nested_vmx_merge_msr_bitmap. We should not touch the
4291 * vmcs02.msr_bitmap here since it gets completely overwritten
4294 vmx_disable_intercept_for_msr(vmx->vmcs01.msr_bitmap, MSR_IA32_PRED_CMD,
4297 case MSR_IA32_CR_PAT:
4298 if (!kvm_pat_valid(data))
4301 if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) {
4302 vmcs_write64(GUEST_IA32_PAT, data);
4303 vcpu->arch.pat = data;
4306 ret = kvm_set_msr_common(vcpu, msr_info);
4308 case MSR_IA32_TSC_ADJUST:
4309 ret = kvm_set_msr_common(vcpu, msr_info);
4311 case MSR_IA32_MCG_EXT_CTL:
4312 if ((!msr_info->host_initiated &&
4313 !(to_vmx(vcpu)->msr_ia32_feature_control &
4314 FEATURE_CONTROL_LMCE)) ||
4315 (data & ~MCG_EXT_CTL_LMCE_EN))
4317 vcpu->arch.mcg_ext_ctl = data;
4319 case MSR_IA32_FEATURE_CONTROL:
4320 if (!vmx_feature_control_msr_valid(vcpu, data) ||
4321 (to_vmx(vcpu)->msr_ia32_feature_control &
4322 FEATURE_CONTROL_LOCKED && !msr_info->host_initiated))
4324 vmx->msr_ia32_feature_control = data;
4325 if (msr_info->host_initiated && data == 0)
4326 vmx_leave_nested(vcpu);
4328 case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC:
4329 if (!msr_info->host_initiated)
4330 return 1; /* they are read-only */
4331 if (!nested_vmx_allowed(vcpu))
4333 return vmx_set_vmx_msr(vcpu, msr_index, data);
4335 if (!vmx_xsaves_supported() ||
4336 (!msr_info->host_initiated &&
4337 !(guest_cpuid_has(vcpu, X86_FEATURE_XSAVE) &&
4338 guest_cpuid_has(vcpu, X86_FEATURE_XSAVES))))
4341 * The only supported bit as of Skylake is bit 8, but
4342 * it is not supported on KVM.
4346 vcpu->arch.ia32_xss = data;
4347 if (vcpu->arch.ia32_xss != host_xss)
4348 add_atomic_switch_msr(vmx, MSR_IA32_XSS,
4349 vcpu->arch.ia32_xss, host_xss, false);
4351 clear_atomic_switch_msr(vmx, MSR_IA32_XSS);
4354 if (!msr_info->host_initiated &&
4355 !guest_cpuid_has(vcpu, X86_FEATURE_RDTSCP))
4357 /* Check reserved bit, higher 32 bits should be zero */
4358 if ((data >> 32) != 0)
4360 /* Otherwise falls through */
4362 msr = find_msr_entry(vmx, msr_index);
4364 u64 old_msr_data = msr->data;
4366 if (msr - vmx->guest_msrs < vmx->save_nmsrs) {
4368 ret = kvm_set_shared_msr(msr->index, msr->data,
4372 msr->data = old_msr_data;
4376 ret = kvm_set_msr_common(vcpu, msr_info);
4379 /* FB_CLEAR may have changed, also update the FB_CLEAR_DIS behavior */
4380 if (msr_index == MSR_IA32_ARCH_CAPABILITIES)
4381 vmx_update_fb_clear_dis(vcpu, vmx);
4386 static void vmx_cache_reg(struct kvm_vcpu *vcpu, enum kvm_reg reg)
4388 __set_bit(reg, (unsigned long *)&vcpu->arch.regs_avail);
4391 vcpu->arch.regs[VCPU_REGS_RSP] = vmcs_readl(GUEST_RSP);
4394 vcpu->arch.regs[VCPU_REGS_RIP] = vmcs_readl(GUEST_RIP);
4396 case VCPU_EXREG_PDPTR:
4398 ept_save_pdptrs(vcpu);
4405 static __init int cpu_has_kvm_support(void)
4407 return cpu_has_vmx();
4410 static __init int vmx_disabled_by_bios(void)
4414 rdmsrl(MSR_IA32_FEATURE_CONTROL, msr);
4415 if (msr & FEATURE_CONTROL_LOCKED) {
4416 /* launched w/ TXT and VMX disabled */
4417 if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX)
4420 /* launched w/o TXT and VMX only enabled w/ TXT */
4421 if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX)
4422 && (msr & FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX)
4423 && !tboot_enabled()) {
4424 printk(KERN_WARNING "kvm: disable TXT in the BIOS or "
4425 "activate TXT before enabling KVM\n");
4428 /* launched w/o TXT and VMX disabled */
4429 if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX)
4430 && !tboot_enabled())
4437 static void kvm_cpu_vmxon(u64 addr)
4439 cr4_set_bits(X86_CR4_VMXE);
4440 intel_pt_handle_vmx(1);
4442 asm volatile (ASM_VMX_VMXON_RAX
4443 : : "a"(&addr), "m"(addr)
4447 static int hardware_enable(void)
4449 int cpu = raw_smp_processor_id();
4450 u64 phys_addr = __pa(per_cpu(vmxarea, cpu));
4453 if (cr4_read_shadow() & X86_CR4_VMXE)
4457 * This can happen if we hot-added a CPU but failed to allocate
4458 * VP assist page for it.
4460 if (static_branch_unlikely(&enable_evmcs) &&
4461 !hv_get_vp_assist_page(cpu))
4464 rdmsrl(MSR_IA32_FEATURE_CONTROL, old);
4466 test_bits = FEATURE_CONTROL_LOCKED;
4467 test_bits |= FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
4468 if (tboot_enabled())
4469 test_bits |= FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX;
4471 if ((old & test_bits) != test_bits) {
4472 /* enable and lock */
4473 wrmsrl(MSR_IA32_FEATURE_CONTROL, old | test_bits);
4475 kvm_cpu_vmxon(phys_addr);
4482 static void vmclear_local_loaded_vmcss(void)
4484 int cpu = raw_smp_processor_id();
4485 struct loaded_vmcs *v, *n;
4487 list_for_each_entry_safe(v, n, &per_cpu(loaded_vmcss_on_cpu, cpu),
4488 loaded_vmcss_on_cpu_link)
4489 __loaded_vmcs_clear(v);
4493 /* Just like cpu_vmxoff(), but with the __kvm_handle_fault_on_reboot()
4496 static void kvm_cpu_vmxoff(void)
4498 asm volatile (__ex(ASM_VMX_VMXOFF) : : : "cc");
4500 intel_pt_handle_vmx(0);
4501 cr4_clear_bits(X86_CR4_VMXE);
4504 static void hardware_disable(void)
4506 vmclear_local_loaded_vmcss();
4510 static __init int adjust_vmx_controls(u32 ctl_min, u32 ctl_opt,
4511 u32 msr, u32 *result)
4513 u32 vmx_msr_low, vmx_msr_high;
4514 u32 ctl = ctl_min | ctl_opt;
4516 rdmsr(msr, vmx_msr_low, vmx_msr_high);
4518 ctl &= vmx_msr_high; /* bit == 0 in high word ==> must be zero */
4519 ctl |= vmx_msr_low; /* bit == 1 in low word ==> must be one */
4521 /* Ensure minimum (required) set of control bits are supported. */
4529 static __init bool allow_1_setting(u32 msr, u32 ctl)
4531 u32 vmx_msr_low, vmx_msr_high;
4533 rdmsr(msr, vmx_msr_low, vmx_msr_high);
4534 return vmx_msr_high & ctl;
4537 static __init int setup_vmcs_config(struct vmcs_config *vmcs_conf)
4539 u32 vmx_msr_low, vmx_msr_high;
4540 u32 min, opt, min2, opt2;
4541 u32 _pin_based_exec_control = 0;
4542 u32 _cpu_based_exec_control = 0;
4543 u32 _cpu_based_2nd_exec_control = 0;
4544 u32 _vmexit_control = 0;
4545 u32 _vmentry_control = 0;
4547 memset(vmcs_conf, 0, sizeof(*vmcs_conf));
4548 min = CPU_BASED_HLT_EXITING |
4549 #ifdef CONFIG_X86_64
4550 CPU_BASED_CR8_LOAD_EXITING |
4551 CPU_BASED_CR8_STORE_EXITING |
4553 CPU_BASED_CR3_LOAD_EXITING |
4554 CPU_BASED_CR3_STORE_EXITING |
4555 CPU_BASED_UNCOND_IO_EXITING |
4556 CPU_BASED_MOV_DR_EXITING |
4557 CPU_BASED_USE_TSC_OFFSETING |
4558 CPU_BASED_MWAIT_EXITING |
4559 CPU_BASED_MONITOR_EXITING |
4560 CPU_BASED_INVLPG_EXITING |
4561 CPU_BASED_RDPMC_EXITING;
4563 opt = CPU_BASED_TPR_SHADOW |
4564 CPU_BASED_USE_MSR_BITMAPS |
4565 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
4566 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PROCBASED_CTLS,
4567 &_cpu_based_exec_control) < 0)
4569 #ifdef CONFIG_X86_64
4570 if ((_cpu_based_exec_control & CPU_BASED_TPR_SHADOW))
4571 _cpu_based_exec_control &= ~CPU_BASED_CR8_LOAD_EXITING &
4572 ~CPU_BASED_CR8_STORE_EXITING;
4574 if (_cpu_based_exec_control & CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) {
4576 opt2 = SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
4577 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
4578 SECONDARY_EXEC_WBINVD_EXITING |
4579 SECONDARY_EXEC_ENABLE_VPID |
4580 SECONDARY_EXEC_ENABLE_EPT |
4581 SECONDARY_EXEC_UNRESTRICTED_GUEST |
4582 SECONDARY_EXEC_PAUSE_LOOP_EXITING |
4583 SECONDARY_EXEC_DESC |
4584 SECONDARY_EXEC_RDTSCP |
4585 SECONDARY_EXEC_ENABLE_INVPCID |
4586 SECONDARY_EXEC_APIC_REGISTER_VIRT |
4587 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY |
4588 SECONDARY_EXEC_SHADOW_VMCS |
4589 SECONDARY_EXEC_XSAVES |
4590 SECONDARY_EXEC_RDSEED_EXITING |
4591 SECONDARY_EXEC_RDRAND_EXITING |
4592 SECONDARY_EXEC_ENABLE_PML |
4593 SECONDARY_EXEC_TSC_SCALING |
4594 SECONDARY_EXEC_ENABLE_VMFUNC |
4595 SECONDARY_EXEC_ENCLS_EXITING;
4596 if (adjust_vmx_controls(min2, opt2,
4597 MSR_IA32_VMX_PROCBASED_CTLS2,
4598 &_cpu_based_2nd_exec_control) < 0)
4601 #ifndef CONFIG_X86_64
4602 if (!(_cpu_based_2nd_exec_control &
4603 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES))
4604 _cpu_based_exec_control &= ~CPU_BASED_TPR_SHADOW;
4607 if (!(_cpu_based_exec_control & CPU_BASED_TPR_SHADOW))
4608 _cpu_based_2nd_exec_control &= ~(
4609 SECONDARY_EXEC_APIC_REGISTER_VIRT |
4610 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
4611 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
4613 rdmsr_safe(MSR_IA32_VMX_EPT_VPID_CAP,
4614 &vmx_capability.ept, &vmx_capability.vpid);
4616 if (_cpu_based_2nd_exec_control & SECONDARY_EXEC_ENABLE_EPT) {
4617 /* CR3 accesses and invlpg don't need to cause VM Exits when EPT
4619 _cpu_based_exec_control &= ~(CPU_BASED_CR3_LOAD_EXITING |
4620 CPU_BASED_CR3_STORE_EXITING |
4621 CPU_BASED_INVLPG_EXITING);
4622 } else if (vmx_capability.ept) {
4623 vmx_capability.ept = 0;
4624 pr_warn_once("EPT CAP should not exist if not support "
4625 "1-setting enable EPT VM-execution control\n");
4627 if (!(_cpu_based_2nd_exec_control & SECONDARY_EXEC_ENABLE_VPID) &&
4628 vmx_capability.vpid) {
4629 vmx_capability.vpid = 0;
4630 pr_warn_once("VPID CAP should not exist if not support "
4631 "1-setting enable VPID VM-execution control\n");
4634 min = VM_EXIT_SAVE_DEBUG_CONTROLS | VM_EXIT_ACK_INTR_ON_EXIT;
4635 #ifdef CONFIG_X86_64
4636 min |= VM_EXIT_HOST_ADDR_SPACE_SIZE;
4638 opt = VM_EXIT_SAVE_IA32_PAT | VM_EXIT_LOAD_IA32_PAT |
4639 VM_EXIT_CLEAR_BNDCFGS;
4640 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_EXIT_CTLS,
4641 &_vmexit_control) < 0)
4644 min = PIN_BASED_EXT_INTR_MASK | PIN_BASED_NMI_EXITING;
4645 opt = PIN_BASED_VIRTUAL_NMIS | PIN_BASED_POSTED_INTR |
4646 PIN_BASED_VMX_PREEMPTION_TIMER;
4647 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PINBASED_CTLS,
4648 &_pin_based_exec_control) < 0)
4651 if (cpu_has_broken_vmx_preemption_timer())
4652 _pin_based_exec_control &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
4653 if (!(_cpu_based_2nd_exec_control &
4654 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY))
4655 _pin_based_exec_control &= ~PIN_BASED_POSTED_INTR;
4657 min = VM_ENTRY_LOAD_DEBUG_CONTROLS;
4658 opt = VM_ENTRY_LOAD_IA32_PAT | VM_ENTRY_LOAD_BNDCFGS;
4659 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_ENTRY_CTLS,
4660 &_vmentry_control) < 0)
4663 rdmsr(MSR_IA32_VMX_BASIC, vmx_msr_low, vmx_msr_high);
4665 /* IA-32 SDM Vol 3B: VMCS size is never greater than 4kB. */
4666 if ((vmx_msr_high & 0x1fff) > PAGE_SIZE)
4669 #ifdef CONFIG_X86_64
4670 /* IA-32 SDM Vol 3B: 64-bit CPUs always have VMX_BASIC_MSR[48]==0. */
4671 if (vmx_msr_high & (1u<<16))
4675 /* Require Write-Back (WB) memory type for VMCS accesses. */
4676 if (((vmx_msr_high >> 18) & 15) != 6)
4679 vmcs_conf->size = vmx_msr_high & 0x1fff;
4680 vmcs_conf->order = get_order(vmcs_conf->size);
4681 vmcs_conf->basic_cap = vmx_msr_high & ~0x1fff;
4683 vmcs_conf->revision_id = vmx_msr_low;
4685 vmcs_conf->pin_based_exec_ctrl = _pin_based_exec_control;
4686 vmcs_conf->cpu_based_exec_ctrl = _cpu_based_exec_control;
4687 vmcs_conf->cpu_based_2nd_exec_ctrl = _cpu_based_2nd_exec_control;
4688 vmcs_conf->vmexit_ctrl = _vmexit_control;
4689 vmcs_conf->vmentry_ctrl = _vmentry_control;
4691 if (static_branch_unlikely(&enable_evmcs))
4692 evmcs_sanitize_exec_ctrls(vmcs_conf);
4694 cpu_has_load_ia32_efer =
4695 allow_1_setting(MSR_IA32_VMX_ENTRY_CTLS,
4696 VM_ENTRY_LOAD_IA32_EFER)
4697 && allow_1_setting(MSR_IA32_VMX_EXIT_CTLS,
4698 VM_EXIT_LOAD_IA32_EFER);
4700 cpu_has_load_perf_global_ctrl =
4701 allow_1_setting(MSR_IA32_VMX_ENTRY_CTLS,
4702 VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL)
4703 && allow_1_setting(MSR_IA32_VMX_EXIT_CTLS,
4704 VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL);
4707 * Some cpus support VM_ENTRY_(LOAD|SAVE)_IA32_PERF_GLOBAL_CTRL
4708 * but due to errata below it can't be used. Workaround is to use
4709 * msr load mechanism to switch IA32_PERF_GLOBAL_CTRL.
4711 * VM Exit May Incorrectly Clear IA32_PERF_GLOBAL_CTRL [34:32]
4716 * BC86,AAY89,BD102 (model 44)
4720 if (cpu_has_load_perf_global_ctrl && boot_cpu_data.x86 == 0x6) {
4721 switch (boot_cpu_data.x86_model) {
4727 cpu_has_load_perf_global_ctrl = false;
4728 printk_once(KERN_WARNING"kvm: VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL "
4729 "does not work properly. Using workaround\n");
4736 if (boot_cpu_has(X86_FEATURE_XSAVES))
4737 rdmsrl(MSR_IA32_XSS, host_xss);
4742 static struct vmcs *alloc_vmcs_cpu(bool shadow, int cpu)
4744 int node = cpu_to_node(cpu);
4748 pages = __alloc_pages_node(node, GFP_KERNEL, vmcs_config.order);
4751 vmcs = page_address(pages);
4752 memset(vmcs, 0, vmcs_config.size);
4754 /* KVM supports Enlightened VMCS v1 only */
4755 if (static_branch_unlikely(&enable_evmcs))
4756 vmcs->hdr.revision_id = KVM_EVMCS_VERSION;
4758 vmcs->hdr.revision_id = vmcs_config.revision_id;
4761 vmcs->hdr.shadow_vmcs = 1;
4765 static void free_vmcs(struct vmcs *vmcs)
4767 free_pages((unsigned long)vmcs, vmcs_config.order);
4771 * Free a VMCS, but before that VMCLEAR it on the CPU where it was last loaded
4773 static void free_loaded_vmcs(struct loaded_vmcs *loaded_vmcs)
4775 if (!loaded_vmcs->vmcs)
4777 loaded_vmcs_clear(loaded_vmcs);
4778 free_vmcs(loaded_vmcs->vmcs);
4779 loaded_vmcs->vmcs = NULL;
4780 if (loaded_vmcs->msr_bitmap)
4781 free_page((unsigned long)loaded_vmcs->msr_bitmap);
4782 WARN_ON(loaded_vmcs->shadow_vmcs != NULL);
4785 static struct vmcs *alloc_vmcs(bool shadow)
4787 return alloc_vmcs_cpu(shadow, raw_smp_processor_id());
4790 static int alloc_loaded_vmcs(struct loaded_vmcs *loaded_vmcs)
4792 loaded_vmcs->vmcs = alloc_vmcs(false);
4793 if (!loaded_vmcs->vmcs)
4796 loaded_vmcs->shadow_vmcs = NULL;
4797 loaded_vmcs_init(loaded_vmcs);
4799 if (cpu_has_vmx_msr_bitmap()) {
4800 loaded_vmcs->msr_bitmap = (unsigned long *)__get_free_page(GFP_KERNEL);
4801 if (!loaded_vmcs->msr_bitmap)
4803 memset(loaded_vmcs->msr_bitmap, 0xff, PAGE_SIZE);
4805 if (IS_ENABLED(CONFIG_HYPERV) &&
4806 static_branch_unlikely(&enable_evmcs) &&
4807 (ms_hyperv.nested_features & HV_X64_NESTED_MSR_BITMAP)) {
4808 struct hv_enlightened_vmcs *evmcs =
4809 (struct hv_enlightened_vmcs *)loaded_vmcs->vmcs;
4811 evmcs->hv_enlightenments_control.msr_bitmap = 1;
4815 memset(&loaded_vmcs->host_state, 0, sizeof(struct vmcs_host_state));
4820 free_loaded_vmcs(loaded_vmcs);
4824 static void free_kvm_area(void)
4828 for_each_possible_cpu(cpu) {
4829 free_vmcs(per_cpu(vmxarea, cpu));
4830 per_cpu(vmxarea, cpu) = NULL;
4834 enum vmcs_field_width {
4835 VMCS_FIELD_WIDTH_U16 = 0,
4836 VMCS_FIELD_WIDTH_U64 = 1,
4837 VMCS_FIELD_WIDTH_U32 = 2,
4838 VMCS_FIELD_WIDTH_NATURAL_WIDTH = 3
4841 static inline int vmcs_field_width(unsigned long field)
4843 if (0x1 & field) /* the *_HIGH fields are all 32 bit */
4844 return VMCS_FIELD_WIDTH_U32;
4845 return (field >> 13) & 0x3 ;
4848 static inline int vmcs_field_readonly(unsigned long field)
4850 return (((field >> 10) & 0x3) == 1);
4853 static void init_vmcs_shadow_fields(void)
4857 for (i = j = 0; i < max_shadow_read_only_fields; i++) {
4858 u16 field = shadow_read_only_fields[i];
4859 if (vmcs_field_width(field) == VMCS_FIELD_WIDTH_U64 &&
4860 (i + 1 == max_shadow_read_only_fields ||
4861 shadow_read_only_fields[i + 1] != field + 1))
4862 pr_err("Missing field from shadow_read_only_field %x\n",
4865 clear_bit(field, vmx_vmread_bitmap);
4866 #ifdef CONFIG_X86_64
4871 shadow_read_only_fields[j] = field;
4874 max_shadow_read_only_fields = j;
4876 for (i = j = 0; i < max_shadow_read_write_fields; i++) {
4877 u16 field = shadow_read_write_fields[i];
4878 if (vmcs_field_width(field) == VMCS_FIELD_WIDTH_U64 &&
4879 (i + 1 == max_shadow_read_write_fields ||
4880 shadow_read_write_fields[i + 1] != field + 1))
4881 pr_err("Missing field from shadow_read_write_field %x\n",
4885 * PML and the preemption timer can be emulated, but the
4886 * processor cannot vmwrite to fields that don't exist
4890 case GUEST_PML_INDEX:
4891 if (!cpu_has_vmx_pml())
4894 case VMX_PREEMPTION_TIMER_VALUE:
4895 if (!cpu_has_vmx_preemption_timer())
4898 case GUEST_INTR_STATUS:
4899 if (!cpu_has_vmx_apicv())
4906 clear_bit(field, vmx_vmwrite_bitmap);
4907 clear_bit(field, vmx_vmread_bitmap);
4908 #ifdef CONFIG_X86_64
4913 shadow_read_write_fields[j] = field;
4916 max_shadow_read_write_fields = j;
4919 static __init int alloc_kvm_area(void)
4923 for_each_possible_cpu(cpu) {
4926 vmcs = alloc_vmcs_cpu(false, cpu);
4933 * When eVMCS is enabled, alloc_vmcs_cpu() sets
4934 * vmcs->revision_id to KVM_EVMCS_VERSION instead of
4935 * revision_id reported by MSR_IA32_VMX_BASIC.
4937 * However, even though not explictly documented by
4938 * TLFS, VMXArea passed as VMXON argument should
4939 * still be marked with revision_id reported by
4942 if (static_branch_unlikely(&enable_evmcs))
4943 vmcs->hdr.revision_id = vmcs_config.revision_id;
4945 per_cpu(vmxarea, cpu) = vmcs;
4950 static void fix_pmode_seg(struct kvm_vcpu *vcpu, int seg,
4951 struct kvm_segment *save)
4953 if (!emulate_invalid_guest_state) {
4955 * CS and SS RPL should be equal during guest entry according
4956 * to VMX spec, but in reality it is not always so. Since vcpu
4957 * is in the middle of the transition from real mode to
4958 * protected mode it is safe to assume that RPL 0 is a good
4961 if (seg == VCPU_SREG_CS || seg == VCPU_SREG_SS)
4962 save->selector &= ~SEGMENT_RPL_MASK;
4963 save->dpl = save->selector & SEGMENT_RPL_MASK;
4966 vmx_set_segment(vcpu, save, seg);
4969 static void enter_pmode(struct kvm_vcpu *vcpu)
4971 unsigned long flags;
4972 struct vcpu_vmx *vmx = to_vmx(vcpu);
4975 * Update real mode segment cache. It may be not up-to-date if sement
4976 * register was written while vcpu was in a guest mode.
4978 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_ES], VCPU_SREG_ES);
4979 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_DS], VCPU_SREG_DS);
4980 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_FS], VCPU_SREG_FS);
4981 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_GS], VCPU_SREG_GS);
4982 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_SS], VCPU_SREG_SS);
4983 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_CS], VCPU_SREG_CS);
4985 vmx->rmode.vm86_active = 0;
4987 vmx_segment_cache_clear(vmx);
4989 vmx_set_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_TR], VCPU_SREG_TR);
4991 flags = vmcs_readl(GUEST_RFLAGS);
4992 flags &= RMODE_GUEST_OWNED_EFLAGS_BITS;
4993 flags |= vmx->rmode.save_rflags & ~RMODE_GUEST_OWNED_EFLAGS_BITS;
4994 vmcs_writel(GUEST_RFLAGS, flags);
4996 vmcs_writel(GUEST_CR4, (vmcs_readl(GUEST_CR4) & ~X86_CR4_VME) |
4997 (vmcs_readl(CR4_READ_SHADOW) & X86_CR4_VME));
4999 update_exception_bitmap(vcpu);
5001 fix_pmode_seg(vcpu, VCPU_SREG_CS, &vmx->rmode.segs[VCPU_SREG_CS]);
5002 fix_pmode_seg(vcpu, VCPU_SREG_SS, &vmx->rmode.segs[VCPU_SREG_SS]);
5003 fix_pmode_seg(vcpu, VCPU_SREG_ES, &vmx->rmode.segs[VCPU_SREG_ES]);
5004 fix_pmode_seg(vcpu, VCPU_SREG_DS, &vmx->rmode.segs[VCPU_SREG_DS]);
5005 fix_pmode_seg(vcpu, VCPU_SREG_FS, &vmx->rmode.segs[VCPU_SREG_FS]);
5006 fix_pmode_seg(vcpu, VCPU_SREG_GS, &vmx->rmode.segs[VCPU_SREG_GS]);
5009 static void fix_rmode_seg(int seg, struct kvm_segment *save)
5011 const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
5012 struct kvm_segment var = *save;
5015 if (seg == VCPU_SREG_CS)
5018 if (!emulate_invalid_guest_state) {
5019 var.selector = var.base >> 4;
5020 var.base = var.base & 0xffff0;
5030 if (save->base & 0xf)
5031 printk_once(KERN_WARNING "kvm: segment base is not "
5032 "paragraph aligned when entering "
5033 "protected mode (seg=%d)", seg);
5036 vmcs_write16(sf->selector, var.selector);
5037 vmcs_writel(sf->base, var.base);
5038 vmcs_write32(sf->limit, var.limit);
5039 vmcs_write32(sf->ar_bytes, vmx_segment_access_rights(&var));
5042 static void enter_rmode(struct kvm_vcpu *vcpu)
5044 unsigned long flags;
5045 struct vcpu_vmx *vmx = to_vmx(vcpu);
5046 struct kvm_vmx *kvm_vmx = to_kvm_vmx(vcpu->kvm);
5048 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_TR], VCPU_SREG_TR);
5049 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_ES], VCPU_SREG_ES);
5050 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_DS], VCPU_SREG_DS);
5051 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_FS], VCPU_SREG_FS);
5052 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_GS], VCPU_SREG_GS);
5053 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_SS], VCPU_SREG_SS);
5054 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_CS], VCPU_SREG_CS);
5056 vmx->rmode.vm86_active = 1;
5059 * Very old userspace does not call KVM_SET_TSS_ADDR before entering
5060 * vcpu. Warn the user that an update is overdue.
5062 if (!kvm_vmx->tss_addr)
5063 printk_once(KERN_WARNING "kvm: KVM_SET_TSS_ADDR need to be "
5064 "called before entering vcpu\n");
5066 vmx_segment_cache_clear(vmx);
5068 vmcs_writel(GUEST_TR_BASE, kvm_vmx->tss_addr);
5069 vmcs_write32(GUEST_TR_LIMIT, RMODE_TSS_SIZE - 1);
5070 vmcs_write32(GUEST_TR_AR_BYTES, 0x008b);
5072 flags = vmcs_readl(GUEST_RFLAGS);
5073 vmx->rmode.save_rflags = flags;
5075 flags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM;
5077 vmcs_writel(GUEST_RFLAGS, flags);
5078 vmcs_writel(GUEST_CR4, vmcs_readl(GUEST_CR4) | X86_CR4_VME);
5079 update_exception_bitmap(vcpu);
5081 fix_rmode_seg(VCPU_SREG_SS, &vmx->rmode.segs[VCPU_SREG_SS]);
5082 fix_rmode_seg(VCPU_SREG_CS, &vmx->rmode.segs[VCPU_SREG_CS]);
5083 fix_rmode_seg(VCPU_SREG_ES, &vmx->rmode.segs[VCPU_SREG_ES]);
5084 fix_rmode_seg(VCPU_SREG_DS, &vmx->rmode.segs[VCPU_SREG_DS]);
5085 fix_rmode_seg(VCPU_SREG_GS, &vmx->rmode.segs[VCPU_SREG_GS]);
5086 fix_rmode_seg(VCPU_SREG_FS, &vmx->rmode.segs[VCPU_SREG_FS]);
5088 kvm_mmu_reset_context(vcpu);
5091 static void vmx_set_efer(struct kvm_vcpu *vcpu, u64 efer)
5093 struct vcpu_vmx *vmx = to_vmx(vcpu);
5094 struct shared_msr_entry *msr = find_msr_entry(vmx, MSR_EFER);
5099 vcpu->arch.efer = efer;
5100 if (efer & EFER_LMA) {
5101 vm_entry_controls_setbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
5104 vm_entry_controls_clearbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
5106 msr->data = efer & ~EFER_LME;
5111 #ifdef CONFIG_X86_64
5113 static void enter_lmode(struct kvm_vcpu *vcpu)
5117 vmx_segment_cache_clear(to_vmx(vcpu));
5119 guest_tr_ar = vmcs_read32(GUEST_TR_AR_BYTES);
5120 if ((guest_tr_ar & VMX_AR_TYPE_MASK) != VMX_AR_TYPE_BUSY_64_TSS) {
5121 pr_debug_ratelimited("%s: tss fixup for long mode. \n",
5123 vmcs_write32(GUEST_TR_AR_BYTES,
5124 (guest_tr_ar & ~VMX_AR_TYPE_MASK)
5125 | VMX_AR_TYPE_BUSY_64_TSS);
5127 vmx_set_efer(vcpu, vcpu->arch.efer | EFER_LMA);
5130 static void exit_lmode(struct kvm_vcpu *vcpu)
5132 vm_entry_controls_clearbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
5133 vmx_set_efer(vcpu, vcpu->arch.efer & ~EFER_LMA);
5138 static inline void __vmx_flush_tlb(struct kvm_vcpu *vcpu, int vpid,
5139 bool invalidate_gpa)
5141 if (enable_ept && (invalidate_gpa || !enable_vpid)) {
5142 if (!VALID_PAGE(vcpu->arch.mmu.root_hpa))
5144 ept_sync_context(construct_eptp(vcpu, vcpu->arch.mmu.root_hpa));
5146 vpid_sync_context(vpid);
5150 static void vmx_flush_tlb(struct kvm_vcpu *vcpu, bool invalidate_gpa)
5152 __vmx_flush_tlb(vcpu, to_vmx(vcpu)->vpid, invalidate_gpa);
5155 static void vmx_flush_tlb_gva(struct kvm_vcpu *vcpu, gva_t addr)
5157 int vpid = to_vmx(vcpu)->vpid;
5159 if (!vpid_sync_vcpu_addr(vpid, addr))
5160 vpid_sync_context(vpid);
5163 * If VPIDs are not supported or enabled, then the above is a no-op.
5164 * But we don't really need a TLB flush in that case anyway, because
5165 * each VM entry/exit includes an implicit flush when VPID is 0.
5169 static void vmx_decache_cr0_guest_bits(struct kvm_vcpu *vcpu)
5171 ulong cr0_guest_owned_bits = vcpu->arch.cr0_guest_owned_bits;
5173 vcpu->arch.cr0 &= ~cr0_guest_owned_bits;
5174 vcpu->arch.cr0 |= vmcs_readl(GUEST_CR0) & cr0_guest_owned_bits;
5177 static void vmx_decache_cr3(struct kvm_vcpu *vcpu)
5179 if (enable_unrestricted_guest || (enable_ept && is_paging(vcpu)))
5180 vcpu->arch.cr3 = vmcs_readl(GUEST_CR3);
5181 __set_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail);
5184 static void vmx_decache_cr4_guest_bits(struct kvm_vcpu *vcpu)
5186 ulong cr4_guest_owned_bits = vcpu->arch.cr4_guest_owned_bits;
5188 vcpu->arch.cr4 &= ~cr4_guest_owned_bits;
5189 vcpu->arch.cr4 |= vmcs_readl(GUEST_CR4) & cr4_guest_owned_bits;
5192 static void ept_load_pdptrs(struct kvm_vcpu *vcpu)
5194 struct kvm_mmu *mmu = vcpu->arch.walk_mmu;
5196 if (!test_bit(VCPU_EXREG_PDPTR,
5197 (unsigned long *)&vcpu->arch.regs_dirty))
5200 if (is_pae_paging(vcpu)) {
5201 vmcs_write64(GUEST_PDPTR0, mmu->pdptrs[0]);
5202 vmcs_write64(GUEST_PDPTR1, mmu->pdptrs[1]);
5203 vmcs_write64(GUEST_PDPTR2, mmu->pdptrs[2]);
5204 vmcs_write64(GUEST_PDPTR3, mmu->pdptrs[3]);
5208 static void ept_save_pdptrs(struct kvm_vcpu *vcpu)
5210 struct kvm_mmu *mmu = vcpu->arch.walk_mmu;
5212 if (is_pae_paging(vcpu)) {
5213 mmu->pdptrs[0] = vmcs_read64(GUEST_PDPTR0);
5214 mmu->pdptrs[1] = vmcs_read64(GUEST_PDPTR1);
5215 mmu->pdptrs[2] = vmcs_read64(GUEST_PDPTR2);
5216 mmu->pdptrs[3] = vmcs_read64(GUEST_PDPTR3);
5219 __set_bit(VCPU_EXREG_PDPTR,
5220 (unsigned long *)&vcpu->arch.regs_avail);
5221 __set_bit(VCPU_EXREG_PDPTR,
5222 (unsigned long *)&vcpu->arch.regs_dirty);
5225 static bool nested_guest_cr0_valid(struct kvm_vcpu *vcpu, unsigned long val)
5227 u64 fixed0 = to_vmx(vcpu)->nested.msrs.cr0_fixed0;
5228 u64 fixed1 = to_vmx(vcpu)->nested.msrs.cr0_fixed1;
5229 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
5231 if (to_vmx(vcpu)->nested.msrs.secondary_ctls_high &
5232 SECONDARY_EXEC_UNRESTRICTED_GUEST &&
5233 nested_cpu_has2(vmcs12, SECONDARY_EXEC_UNRESTRICTED_GUEST))
5234 fixed0 &= ~(X86_CR0_PE | X86_CR0_PG);
5236 return fixed_bits_valid(val, fixed0, fixed1);
5239 static bool nested_host_cr0_valid(struct kvm_vcpu *vcpu, unsigned long val)
5241 u64 fixed0 = to_vmx(vcpu)->nested.msrs.cr0_fixed0;
5242 u64 fixed1 = to_vmx(vcpu)->nested.msrs.cr0_fixed1;
5244 return fixed_bits_valid(val, fixed0, fixed1);
5247 static bool nested_cr4_valid(struct kvm_vcpu *vcpu, unsigned long val)
5249 u64 fixed0 = to_vmx(vcpu)->nested.msrs.cr4_fixed0;
5250 u64 fixed1 = to_vmx(vcpu)->nested.msrs.cr4_fixed1;
5252 return fixed_bits_valid(val, fixed0, fixed1);
5255 /* No difference in the restrictions on guest and host CR4 in VMX operation. */
5256 #define nested_guest_cr4_valid nested_cr4_valid
5257 #define nested_host_cr4_valid nested_cr4_valid
5259 static int vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4);
5261 static void ept_update_paging_mode_cr0(unsigned long *hw_cr0,
5263 struct kvm_vcpu *vcpu)
5265 if (!test_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail))
5266 vmx_decache_cr3(vcpu);
5267 if (!(cr0 & X86_CR0_PG)) {
5268 /* From paging/starting to nonpaging */
5269 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL,
5270 vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) |
5271 (CPU_BASED_CR3_LOAD_EXITING |
5272 CPU_BASED_CR3_STORE_EXITING));
5273 vcpu->arch.cr0 = cr0;
5274 vmx_set_cr4(vcpu, kvm_read_cr4(vcpu));
5275 } else if (!is_paging(vcpu)) {
5276 /* From nonpaging to paging */
5277 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL,
5278 vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) &
5279 ~(CPU_BASED_CR3_LOAD_EXITING |
5280 CPU_BASED_CR3_STORE_EXITING));
5281 vcpu->arch.cr0 = cr0;
5282 vmx_set_cr4(vcpu, kvm_read_cr4(vcpu));
5285 if (!(cr0 & X86_CR0_WP))
5286 *hw_cr0 &= ~X86_CR0_WP;
5289 static void vmx_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
5291 struct vcpu_vmx *vmx = to_vmx(vcpu);
5292 unsigned long hw_cr0;
5294 hw_cr0 = (cr0 & ~KVM_GUEST_CR0_MASK);
5295 if (enable_unrestricted_guest)
5296 hw_cr0 |= KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST;
5298 hw_cr0 |= KVM_VM_CR0_ALWAYS_ON;
5300 if (vmx->rmode.vm86_active && (cr0 & X86_CR0_PE))
5303 if (!vmx->rmode.vm86_active && !(cr0 & X86_CR0_PE))
5307 #ifdef CONFIG_X86_64
5308 if (vcpu->arch.efer & EFER_LME) {
5309 if (!is_paging(vcpu) && (cr0 & X86_CR0_PG))
5311 if (is_paging(vcpu) && !(cr0 & X86_CR0_PG))
5316 if (enable_ept && !enable_unrestricted_guest)
5317 ept_update_paging_mode_cr0(&hw_cr0, cr0, vcpu);
5319 vmcs_writel(CR0_READ_SHADOW, cr0);
5320 vmcs_writel(GUEST_CR0, hw_cr0);
5321 vcpu->arch.cr0 = cr0;
5323 /* depends on vcpu->arch.cr0 to be set to a new value */
5324 vmx->emulation_required = emulation_required(vcpu);
5327 static int get_ept_level(struct kvm_vcpu *vcpu)
5329 /* Nested EPT currently only supports 4-level walks. */
5330 if (is_guest_mode(vcpu) && nested_cpu_has_ept(get_vmcs12(vcpu)))
5332 if (cpu_has_vmx_ept_5levels() && (cpuid_maxphyaddr(vcpu) > 48))
5337 static u64 construct_eptp(struct kvm_vcpu *vcpu, unsigned long root_hpa)
5339 u64 eptp = VMX_EPTP_MT_WB;
5341 eptp |= (get_ept_level(vcpu) == 5) ? VMX_EPTP_PWL_5 : VMX_EPTP_PWL_4;
5343 if (enable_ept_ad_bits &&
5344 (!is_guest_mode(vcpu) || nested_ept_ad_enabled(vcpu)))
5345 eptp |= VMX_EPTP_AD_ENABLE_BIT;
5346 eptp |= (root_hpa & PAGE_MASK);
5351 static void vmx_set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3)
5353 struct kvm *kvm = vcpu->kvm;
5354 unsigned long guest_cr3;
5359 eptp = construct_eptp(vcpu, cr3);
5360 vmcs_write64(EPT_POINTER, eptp);
5362 if (kvm_x86_ops->tlb_remote_flush) {
5363 spin_lock(&to_kvm_vmx(kvm)->ept_pointer_lock);
5364 to_vmx(vcpu)->ept_pointer = eptp;
5365 to_kvm_vmx(kvm)->ept_pointers_match
5366 = EPT_POINTERS_CHECK;
5367 spin_unlock(&to_kvm_vmx(kvm)->ept_pointer_lock);
5370 if (enable_unrestricted_guest || is_paging(vcpu) ||
5371 is_guest_mode(vcpu))
5372 guest_cr3 = kvm_read_cr3(vcpu);
5374 guest_cr3 = to_kvm_vmx(kvm)->ept_identity_map_addr;
5375 ept_load_pdptrs(vcpu);
5378 vmcs_writel(GUEST_CR3, guest_cr3);
5381 static int vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
5384 * Pass through host's Machine Check Enable value to hw_cr4, which
5385 * is in force while we are in guest mode. Do not let guests control
5386 * this bit, even if host CR4.MCE == 0.
5388 unsigned long hw_cr4;
5390 hw_cr4 = (cr4_read_shadow() & X86_CR4_MCE) | (cr4 & ~X86_CR4_MCE);
5391 if (enable_unrestricted_guest)
5392 hw_cr4 |= KVM_VM_CR4_ALWAYS_ON_UNRESTRICTED_GUEST;
5393 else if (to_vmx(vcpu)->rmode.vm86_active)
5394 hw_cr4 |= KVM_RMODE_VM_CR4_ALWAYS_ON;
5396 hw_cr4 |= KVM_PMODE_VM_CR4_ALWAYS_ON;
5398 if (!boot_cpu_has(X86_FEATURE_UMIP) && vmx_umip_emulated()) {
5399 if (cr4 & X86_CR4_UMIP) {
5400 vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL,
5401 SECONDARY_EXEC_DESC);
5402 hw_cr4 &= ~X86_CR4_UMIP;
5403 } else if (!is_guest_mode(vcpu) ||
5404 !nested_cpu_has2(get_vmcs12(vcpu), SECONDARY_EXEC_DESC))
5405 vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL,
5406 SECONDARY_EXEC_DESC);
5409 if (cr4 & X86_CR4_VMXE) {
5411 * To use VMXON (and later other VMX instructions), a guest
5412 * must first be able to turn on cr4.VMXE (see handle_vmon()).
5413 * So basically the check on whether to allow nested VMX
5414 * is here. We operate under the default treatment of SMM,
5415 * so VMX cannot be enabled under SMM.
5417 if (!nested_vmx_allowed(vcpu) || is_smm(vcpu))
5421 if (to_vmx(vcpu)->nested.vmxon && !nested_cr4_valid(vcpu, cr4))
5424 vcpu->arch.cr4 = cr4;
5426 if (!enable_unrestricted_guest) {
5428 if (!is_paging(vcpu)) {
5429 hw_cr4 &= ~X86_CR4_PAE;
5430 hw_cr4 |= X86_CR4_PSE;
5431 } else if (!(cr4 & X86_CR4_PAE)) {
5432 hw_cr4 &= ~X86_CR4_PAE;
5437 * SMEP/SMAP/PKU is disabled if CPU is in non-paging mode in
5438 * hardware. To emulate this behavior, SMEP/SMAP/PKU needs
5439 * to be manually disabled when guest switches to non-paging
5442 * If !enable_unrestricted_guest, the CPU is always running
5443 * with CR0.PG=1 and CR4 needs to be modified.
5444 * If enable_unrestricted_guest, the CPU automatically
5445 * disables SMEP/SMAP/PKU when the guest sets CR0.PG=0.
5447 if (!is_paging(vcpu))
5448 hw_cr4 &= ~(X86_CR4_SMEP | X86_CR4_SMAP | X86_CR4_PKE);
5451 vmcs_writel(CR4_READ_SHADOW, cr4);
5452 vmcs_writel(GUEST_CR4, hw_cr4);
5456 static void vmx_get_segment(struct kvm_vcpu *vcpu,
5457 struct kvm_segment *var, int seg)
5459 struct vcpu_vmx *vmx = to_vmx(vcpu);
5462 if (vmx->rmode.vm86_active && seg != VCPU_SREG_LDTR) {
5463 *var = vmx->rmode.segs[seg];
5464 if (seg == VCPU_SREG_TR
5465 || var->selector == vmx_read_guest_seg_selector(vmx, seg))
5467 var->base = vmx_read_guest_seg_base(vmx, seg);
5468 var->selector = vmx_read_guest_seg_selector(vmx, seg);
5471 var->base = vmx_read_guest_seg_base(vmx, seg);
5472 var->limit = vmx_read_guest_seg_limit(vmx, seg);
5473 var->selector = vmx_read_guest_seg_selector(vmx, seg);
5474 ar = vmx_read_guest_seg_ar(vmx, seg);
5475 var->unusable = (ar >> 16) & 1;
5476 var->type = ar & 15;
5477 var->s = (ar >> 4) & 1;
5478 var->dpl = (ar >> 5) & 3;
5480 * Some userspaces do not preserve unusable property. Since usable
5481 * segment has to be present according to VMX spec we can use present
5482 * property to amend userspace bug by making unusable segment always
5483 * nonpresent. vmx_segment_access_rights() already marks nonpresent
5484 * segment as unusable.
5486 var->present = !var->unusable;
5487 var->avl = (ar >> 12) & 1;
5488 var->l = (ar >> 13) & 1;
5489 var->db = (ar >> 14) & 1;
5490 var->g = (ar >> 15) & 1;
5493 static u64 vmx_get_segment_base(struct kvm_vcpu *vcpu, int seg)
5495 struct kvm_segment s;
5497 if (to_vmx(vcpu)->rmode.vm86_active) {
5498 vmx_get_segment(vcpu, &s, seg);
5501 return vmx_read_guest_seg_base(to_vmx(vcpu), seg);
5504 static int vmx_get_cpl(struct kvm_vcpu *vcpu)
5506 struct vcpu_vmx *vmx = to_vmx(vcpu);
5508 if (unlikely(vmx->rmode.vm86_active))
5511 int ar = vmx_read_guest_seg_ar(vmx, VCPU_SREG_SS);
5512 return VMX_AR_DPL(ar);
5516 static u32 vmx_segment_access_rights(struct kvm_segment *var)
5520 if (var->unusable || !var->present)
5523 ar = var->type & 15;
5524 ar |= (var->s & 1) << 4;
5525 ar |= (var->dpl & 3) << 5;
5526 ar |= (var->present & 1) << 7;
5527 ar |= (var->avl & 1) << 12;
5528 ar |= (var->l & 1) << 13;
5529 ar |= (var->db & 1) << 14;
5530 ar |= (var->g & 1) << 15;
5536 static void vmx_set_segment(struct kvm_vcpu *vcpu,
5537 struct kvm_segment *var, int seg)
5539 struct vcpu_vmx *vmx = to_vmx(vcpu);
5540 const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
5542 vmx_segment_cache_clear(vmx);
5544 if (vmx->rmode.vm86_active && seg != VCPU_SREG_LDTR) {
5545 vmx->rmode.segs[seg] = *var;
5546 if (seg == VCPU_SREG_TR)
5547 vmcs_write16(sf->selector, var->selector);
5549 fix_rmode_seg(seg, &vmx->rmode.segs[seg]);
5553 vmcs_writel(sf->base, var->base);
5554 vmcs_write32(sf->limit, var->limit);
5555 vmcs_write16(sf->selector, var->selector);
5558 * Fix the "Accessed" bit in AR field of segment registers for older
5560 * IA32 arch specifies that at the time of processor reset the
5561 * "Accessed" bit in the AR field of segment registers is 1. And qemu
5562 * is setting it to 0 in the userland code. This causes invalid guest
5563 * state vmexit when "unrestricted guest" mode is turned on.
5564 * Fix for this setup issue in cpu_reset is being pushed in the qemu
5565 * tree. Newer qemu binaries with that qemu fix would not need this
5568 if (enable_unrestricted_guest && (seg != VCPU_SREG_LDTR))
5569 var->type |= 0x1; /* Accessed */
5571 vmcs_write32(sf->ar_bytes, vmx_segment_access_rights(var));
5574 vmx->emulation_required = emulation_required(vcpu);
5577 static void vmx_get_cs_db_l_bits(struct kvm_vcpu *vcpu, int *db, int *l)
5579 u32 ar = vmx_read_guest_seg_ar(to_vmx(vcpu), VCPU_SREG_CS);
5581 *db = (ar >> 14) & 1;
5582 *l = (ar >> 13) & 1;
5585 static void vmx_get_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
5587 dt->size = vmcs_read32(GUEST_IDTR_LIMIT);
5588 dt->address = vmcs_readl(GUEST_IDTR_BASE);
5591 static void vmx_set_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
5593 vmcs_write32(GUEST_IDTR_LIMIT, dt->size);
5594 vmcs_writel(GUEST_IDTR_BASE, dt->address);
5597 static void vmx_get_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
5599 dt->size = vmcs_read32(GUEST_GDTR_LIMIT);
5600 dt->address = vmcs_readl(GUEST_GDTR_BASE);
5603 static void vmx_set_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
5605 vmcs_write32(GUEST_GDTR_LIMIT, dt->size);
5606 vmcs_writel(GUEST_GDTR_BASE, dt->address);
5609 static bool rmode_segment_valid(struct kvm_vcpu *vcpu, int seg)
5611 struct kvm_segment var;
5614 vmx_get_segment(vcpu, &var, seg);
5616 if (seg == VCPU_SREG_CS)
5618 ar = vmx_segment_access_rights(&var);
5620 if (var.base != (var.selector << 4))
5622 if (var.limit != 0xffff)
5630 static bool code_segment_valid(struct kvm_vcpu *vcpu)
5632 struct kvm_segment cs;
5633 unsigned int cs_rpl;
5635 vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
5636 cs_rpl = cs.selector & SEGMENT_RPL_MASK;
5640 if (~cs.type & (VMX_AR_TYPE_CODE_MASK|VMX_AR_TYPE_ACCESSES_MASK))
5644 if (cs.type & VMX_AR_TYPE_WRITEABLE_MASK) {
5645 if (cs.dpl > cs_rpl)
5648 if (cs.dpl != cs_rpl)
5654 /* TODO: Add Reserved field check, this'll require a new member in the kvm_segment_field structure */
5658 static bool stack_segment_valid(struct kvm_vcpu *vcpu)
5660 struct kvm_segment ss;
5661 unsigned int ss_rpl;
5663 vmx_get_segment(vcpu, &ss, VCPU_SREG_SS);
5664 ss_rpl = ss.selector & SEGMENT_RPL_MASK;
5668 if (ss.type != 3 && ss.type != 7)
5672 if (ss.dpl != ss_rpl) /* DPL != RPL */
5680 static bool data_segment_valid(struct kvm_vcpu *vcpu, int seg)
5682 struct kvm_segment var;
5685 vmx_get_segment(vcpu, &var, seg);
5686 rpl = var.selector & SEGMENT_RPL_MASK;
5694 if (~var.type & (VMX_AR_TYPE_CODE_MASK|VMX_AR_TYPE_WRITEABLE_MASK)) {
5695 if (var.dpl < rpl) /* DPL < RPL */
5699 /* TODO: Add other members to kvm_segment_field to allow checking for other access
5705 static bool tr_valid(struct kvm_vcpu *vcpu)
5707 struct kvm_segment tr;
5709 vmx_get_segment(vcpu, &tr, VCPU_SREG_TR);
5713 if (tr.selector & SEGMENT_TI_MASK) /* TI = 1 */
5715 if (tr.type != 3 && tr.type != 11) /* TODO: Check if guest is in IA32e mode */
5723 static bool ldtr_valid(struct kvm_vcpu *vcpu)
5725 struct kvm_segment ldtr;
5727 vmx_get_segment(vcpu, &ldtr, VCPU_SREG_LDTR);
5731 if (ldtr.selector & SEGMENT_TI_MASK) /* TI = 1 */
5741 static bool cs_ss_rpl_check(struct kvm_vcpu *vcpu)
5743 struct kvm_segment cs, ss;
5745 vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
5746 vmx_get_segment(vcpu, &ss, VCPU_SREG_SS);
5748 return ((cs.selector & SEGMENT_RPL_MASK) ==
5749 (ss.selector & SEGMENT_RPL_MASK));
5752 static bool nested_vmx_check_io_bitmaps(struct kvm_vcpu *vcpu,
5753 unsigned int port, int size);
5754 static bool nested_vmx_exit_handled_io(struct kvm_vcpu *vcpu,
5755 struct vmcs12 *vmcs12)
5757 unsigned long exit_qualification;
5758 unsigned short port;
5761 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_IO_BITMAPS))
5762 return nested_cpu_has(vmcs12, CPU_BASED_UNCOND_IO_EXITING);
5764 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
5766 port = exit_qualification >> 16;
5767 size = (exit_qualification & 7) + 1;
5769 return nested_vmx_check_io_bitmaps(vcpu, port, size);
5773 * Check if guest state is valid. Returns true if valid, false if
5775 * We assume that registers are always usable
5777 static bool guest_state_valid(struct kvm_vcpu *vcpu)
5779 if (enable_unrestricted_guest)
5782 /* real mode guest state checks */
5783 if (!is_protmode(vcpu) || (vmx_get_rflags(vcpu) & X86_EFLAGS_VM)) {
5784 if (!rmode_segment_valid(vcpu, VCPU_SREG_CS))
5786 if (!rmode_segment_valid(vcpu, VCPU_SREG_SS))
5788 if (!rmode_segment_valid(vcpu, VCPU_SREG_DS))
5790 if (!rmode_segment_valid(vcpu, VCPU_SREG_ES))
5792 if (!rmode_segment_valid(vcpu, VCPU_SREG_FS))
5794 if (!rmode_segment_valid(vcpu, VCPU_SREG_GS))
5797 /* protected mode guest state checks */
5798 if (!cs_ss_rpl_check(vcpu))
5800 if (!code_segment_valid(vcpu))
5802 if (!stack_segment_valid(vcpu))
5804 if (!data_segment_valid(vcpu, VCPU_SREG_DS))
5806 if (!data_segment_valid(vcpu, VCPU_SREG_ES))
5808 if (!data_segment_valid(vcpu, VCPU_SREG_FS))
5810 if (!data_segment_valid(vcpu, VCPU_SREG_GS))
5812 if (!tr_valid(vcpu))
5814 if (!ldtr_valid(vcpu))
5818 * - Add checks on RIP
5819 * - Add checks on RFLAGS
5825 static bool page_address_valid(struct kvm_vcpu *vcpu, gpa_t gpa)
5827 return PAGE_ALIGNED(gpa) && !(gpa >> cpuid_maxphyaddr(vcpu));
5830 static int init_rmode_tss(struct kvm *kvm)
5836 idx = srcu_read_lock(&kvm->srcu);
5837 fn = to_kvm_vmx(kvm)->tss_addr >> PAGE_SHIFT;
5838 r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE);
5841 data = TSS_BASE_SIZE + TSS_REDIRECTION_SIZE;
5842 r = kvm_write_guest_page(kvm, fn++, &data,
5843 TSS_IOPB_BASE_OFFSET, sizeof(u16));
5846 r = kvm_clear_guest_page(kvm, fn++, 0, PAGE_SIZE);
5849 r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE);
5853 r = kvm_write_guest_page(kvm, fn, &data,
5854 RMODE_TSS_SIZE - 2 * PAGE_SIZE - 1,
5857 srcu_read_unlock(&kvm->srcu, idx);
5861 static int init_rmode_identity_map(struct kvm *kvm)
5863 struct kvm_vmx *kvm_vmx = to_kvm_vmx(kvm);
5865 kvm_pfn_t identity_map_pfn;
5868 /* Protect kvm_vmx->ept_identity_pagetable_done. */
5869 mutex_lock(&kvm->slots_lock);
5871 if (likely(kvm_vmx->ept_identity_pagetable_done))
5874 if (!kvm_vmx->ept_identity_map_addr)
5875 kvm_vmx->ept_identity_map_addr = VMX_EPT_IDENTITY_PAGETABLE_ADDR;
5876 identity_map_pfn = kvm_vmx->ept_identity_map_addr >> PAGE_SHIFT;
5878 r = __x86_set_memory_region(kvm, IDENTITY_PAGETABLE_PRIVATE_MEMSLOT,
5879 kvm_vmx->ept_identity_map_addr, PAGE_SIZE);
5883 idx = srcu_read_lock(&kvm->srcu);
5884 r = kvm_clear_guest_page(kvm, identity_map_pfn, 0, PAGE_SIZE);
5887 /* Set up identity-mapping pagetable for EPT in real mode */
5888 for (i = 0; i < PT32_ENT_PER_PAGE; i++) {
5889 tmp = (i << 22) + (_PAGE_PRESENT | _PAGE_RW | _PAGE_USER |
5890 _PAGE_ACCESSED | _PAGE_DIRTY | _PAGE_PSE);
5891 r = kvm_write_guest_page(kvm, identity_map_pfn,
5892 &tmp, i * sizeof(tmp), sizeof(tmp));
5896 kvm_vmx->ept_identity_pagetable_done = true;
5899 srcu_read_unlock(&kvm->srcu, idx);
5902 mutex_unlock(&kvm->slots_lock);
5906 static void seg_setup(int seg)
5908 const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
5911 vmcs_write16(sf->selector, 0);
5912 vmcs_writel(sf->base, 0);
5913 vmcs_write32(sf->limit, 0xffff);
5915 if (seg == VCPU_SREG_CS)
5916 ar |= 0x08; /* code segment */
5918 vmcs_write32(sf->ar_bytes, ar);
5921 static int alloc_apic_access_page(struct kvm *kvm)
5926 mutex_lock(&kvm->slots_lock);
5927 if (kvm->arch.apic_access_page_done)
5929 r = __x86_set_memory_region(kvm, APIC_ACCESS_PAGE_PRIVATE_MEMSLOT,
5930 APIC_DEFAULT_PHYS_BASE, PAGE_SIZE);
5934 page = gfn_to_page(kvm, APIC_DEFAULT_PHYS_BASE >> PAGE_SHIFT);
5935 if (is_error_page(page)) {
5941 * Do not pin the page in memory, so that memory hot-unplug
5942 * is able to migrate it.
5945 kvm->arch.apic_access_page_done = true;
5947 mutex_unlock(&kvm->slots_lock);
5951 static int allocate_vpid(void)
5957 spin_lock(&vmx_vpid_lock);
5958 vpid = find_first_zero_bit(vmx_vpid_bitmap, VMX_NR_VPIDS);
5959 if (vpid < VMX_NR_VPIDS)
5960 __set_bit(vpid, vmx_vpid_bitmap);
5963 spin_unlock(&vmx_vpid_lock);
5967 static void free_vpid(int vpid)
5969 if (!enable_vpid || vpid == 0)
5971 spin_lock(&vmx_vpid_lock);
5972 __clear_bit(vpid, vmx_vpid_bitmap);
5973 spin_unlock(&vmx_vpid_lock);
5976 static __always_inline void vmx_disable_intercept_for_msr(unsigned long *msr_bitmap,
5979 int f = sizeof(unsigned long);
5981 if (!cpu_has_vmx_msr_bitmap())
5984 if (static_branch_unlikely(&enable_evmcs))
5985 evmcs_touch_msr_bitmap();
5988 * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
5989 * have the write-low and read-high bitmap offsets the wrong way round.
5990 * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
5992 if (msr <= 0x1fff) {
5993 if (type & MSR_TYPE_R)
5995 __clear_bit(msr, msr_bitmap + 0x000 / f);
5997 if (type & MSR_TYPE_W)
5999 __clear_bit(msr, msr_bitmap + 0x800 / f);
6001 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
6003 if (type & MSR_TYPE_R)
6005 __clear_bit(msr, msr_bitmap + 0x400 / f);
6007 if (type & MSR_TYPE_W)
6009 __clear_bit(msr, msr_bitmap + 0xc00 / f);
6014 static __always_inline void vmx_enable_intercept_for_msr(unsigned long *msr_bitmap,
6017 int f = sizeof(unsigned long);
6019 if (!cpu_has_vmx_msr_bitmap())
6022 if (static_branch_unlikely(&enable_evmcs))
6023 evmcs_touch_msr_bitmap();
6026 * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
6027 * have the write-low and read-high bitmap offsets the wrong way round.
6028 * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
6030 if (msr <= 0x1fff) {
6031 if (type & MSR_TYPE_R)
6033 __set_bit(msr, msr_bitmap + 0x000 / f);
6035 if (type & MSR_TYPE_W)
6037 __set_bit(msr, msr_bitmap + 0x800 / f);
6039 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
6041 if (type & MSR_TYPE_R)
6043 __set_bit(msr, msr_bitmap + 0x400 / f);
6045 if (type & MSR_TYPE_W)
6047 __set_bit(msr, msr_bitmap + 0xc00 / f);
6052 static __always_inline void vmx_set_intercept_for_msr(unsigned long *msr_bitmap,
6053 u32 msr, int type, bool value)
6056 vmx_enable_intercept_for_msr(msr_bitmap, msr, type);
6058 vmx_disable_intercept_for_msr(msr_bitmap, msr, type);
6062 * If a msr is allowed by L0, we should check whether it is allowed by L1.
6063 * The corresponding bit will be cleared unless both of L0 and L1 allow it.
6065 static void nested_vmx_disable_intercept_for_msr(unsigned long *msr_bitmap_l1,
6066 unsigned long *msr_bitmap_nested,
6069 int f = sizeof(unsigned long);
6072 * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
6073 * have the write-low and read-high bitmap offsets the wrong way round.
6074 * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
6076 if (msr <= 0x1fff) {
6077 if (type & MSR_TYPE_R &&
6078 !test_bit(msr, msr_bitmap_l1 + 0x000 / f))
6080 __clear_bit(msr, msr_bitmap_nested + 0x000 / f);
6082 if (type & MSR_TYPE_W &&
6083 !test_bit(msr, msr_bitmap_l1 + 0x800 / f))
6085 __clear_bit(msr, msr_bitmap_nested + 0x800 / f);
6087 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
6089 if (type & MSR_TYPE_R &&
6090 !test_bit(msr, msr_bitmap_l1 + 0x400 / f))
6092 __clear_bit(msr, msr_bitmap_nested + 0x400 / f);
6094 if (type & MSR_TYPE_W &&
6095 !test_bit(msr, msr_bitmap_l1 + 0xc00 / f))
6097 __clear_bit(msr, msr_bitmap_nested + 0xc00 / f);
6102 static u8 vmx_msr_bitmap_mode(struct kvm_vcpu *vcpu)
6106 if (cpu_has_secondary_exec_ctrls() &&
6107 (vmcs_read32(SECONDARY_VM_EXEC_CONTROL) &
6108 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE)) {
6109 mode |= MSR_BITMAP_MODE_X2APIC;
6110 if (enable_apicv && kvm_vcpu_apicv_active(vcpu))
6111 mode |= MSR_BITMAP_MODE_X2APIC_APICV;
6117 #define X2APIC_MSR(r) (APIC_BASE_MSR + ((r) >> 4))
6119 static void vmx_update_msr_bitmap_x2apic(unsigned long *msr_bitmap,
6124 for (msr = 0x800; msr <= 0x8ff; msr += BITS_PER_LONG) {
6125 unsigned word = msr / BITS_PER_LONG;
6126 msr_bitmap[word] = (mode & MSR_BITMAP_MODE_X2APIC_APICV) ? 0 : ~0;
6127 msr_bitmap[word + (0x800 / sizeof(long))] = ~0;
6130 if (mode & MSR_BITMAP_MODE_X2APIC) {
6132 * TPR reads and writes can be virtualized even if virtual interrupt
6133 * delivery is not in use.
6135 vmx_disable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_TASKPRI), MSR_TYPE_RW);
6136 if (mode & MSR_BITMAP_MODE_X2APIC_APICV) {
6137 vmx_enable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_TMCCT), MSR_TYPE_R);
6138 vmx_disable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_EOI), MSR_TYPE_W);
6139 vmx_disable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_SELF_IPI), MSR_TYPE_W);
6144 static void vmx_update_msr_bitmap(struct kvm_vcpu *vcpu)
6146 struct vcpu_vmx *vmx = to_vmx(vcpu);
6147 unsigned long *msr_bitmap = vmx->vmcs01.msr_bitmap;
6148 u8 mode = vmx_msr_bitmap_mode(vcpu);
6149 u8 changed = mode ^ vmx->msr_bitmap_mode;
6154 if (changed & (MSR_BITMAP_MODE_X2APIC | MSR_BITMAP_MODE_X2APIC_APICV))
6155 vmx_update_msr_bitmap_x2apic(msr_bitmap, mode);
6157 vmx->msr_bitmap_mode = mode;
6160 static bool vmx_get_enable_apicv(struct kvm_vcpu *vcpu)
6162 return enable_apicv;
6165 static void nested_mark_vmcs12_pages_dirty(struct kvm_vcpu *vcpu)
6167 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
6171 * Don't need to mark the APIC access page dirty; it is never
6172 * written to by the CPU during APIC virtualization.
6175 if (nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW)) {
6176 gfn = vmcs12->virtual_apic_page_addr >> PAGE_SHIFT;
6177 kvm_vcpu_mark_page_dirty(vcpu, gfn);
6180 if (nested_cpu_has_posted_intr(vmcs12)) {
6181 gfn = vmcs12->posted_intr_desc_addr >> PAGE_SHIFT;
6182 kvm_vcpu_mark_page_dirty(vcpu, gfn);
6187 static void vmx_complete_nested_posted_interrupt(struct kvm_vcpu *vcpu)
6189 struct vcpu_vmx *vmx = to_vmx(vcpu);
6194 if (!vmx->nested.pi_desc || !vmx->nested.pi_pending)
6197 vmx->nested.pi_pending = false;
6198 if (!pi_test_and_clear_on(vmx->nested.pi_desc))
6201 max_irr = find_last_bit((unsigned long *)vmx->nested.pi_desc->pir, 256);
6202 if (max_irr != 256) {
6203 vapic_page = kmap(vmx->nested.virtual_apic_page);
6204 __kvm_apic_update_irr(vmx->nested.pi_desc->pir,
6205 vapic_page, &max_irr);
6206 kunmap(vmx->nested.virtual_apic_page);
6208 status = vmcs_read16(GUEST_INTR_STATUS);
6209 if ((u8)max_irr > ((u8)status & 0xff)) {
6211 status |= (u8)max_irr;
6212 vmcs_write16(GUEST_INTR_STATUS, status);
6216 nested_mark_vmcs12_pages_dirty(vcpu);
6219 static u8 vmx_get_rvi(void)
6221 return vmcs_read16(GUEST_INTR_STATUS) & 0xff;
6224 static bool vmx_guest_apic_has_interrupt(struct kvm_vcpu *vcpu)
6226 struct vcpu_vmx *vmx = to_vmx(vcpu);
6231 if (WARN_ON_ONCE(!is_guest_mode(vcpu)) ||
6232 !nested_cpu_has_vid(get_vmcs12(vcpu)) ||
6233 WARN_ON_ONCE(!vmx->nested.virtual_apic_page))
6236 rvi = vmx_get_rvi();
6238 vapic_page = kmap(vmx->nested.virtual_apic_page);
6239 vppr = *((u32 *)(vapic_page + APIC_PROCPRI));
6240 kunmap(vmx->nested.virtual_apic_page);
6242 return ((rvi & 0xf0) > (vppr & 0xf0));
6245 static inline bool kvm_vcpu_trigger_posted_interrupt(struct kvm_vcpu *vcpu,
6249 int pi_vec = nested ? POSTED_INTR_NESTED_VECTOR : POSTED_INTR_VECTOR;
6251 if (vcpu->mode == IN_GUEST_MODE) {
6253 * The vector of interrupt to be delivered to vcpu had
6254 * been set in PIR before this function.
6256 * Following cases will be reached in this block, and
6257 * we always send a notification event in all cases as
6260 * Case 1: vcpu keeps in non-root mode. Sending a
6261 * notification event posts the interrupt to vcpu.
6263 * Case 2: vcpu exits to root mode and is still
6264 * runnable. PIR will be synced to vIRR before the
6265 * next vcpu entry. Sending a notification event in
6266 * this case has no effect, as vcpu is not in root
6269 * Case 3: vcpu exits to root mode and is blocked.
6270 * vcpu_block() has already synced PIR to vIRR and
6271 * never blocks vcpu if vIRR is not cleared. Therefore,
6272 * a blocked vcpu here does not wait for any requested
6273 * interrupts in PIR, and sending a notification event
6274 * which has no effect is safe here.
6277 apic->send_IPI_mask(get_cpu_mask(vcpu->cpu), pi_vec);
6284 static int vmx_deliver_nested_posted_interrupt(struct kvm_vcpu *vcpu,
6287 struct vcpu_vmx *vmx = to_vmx(vcpu);
6289 if (is_guest_mode(vcpu) &&
6290 vector == vmx->nested.posted_intr_nv) {
6292 * If a posted intr is not recognized by hardware,
6293 * we will accomplish it in the next vmentry.
6295 vmx->nested.pi_pending = true;
6296 kvm_make_request(KVM_REQ_EVENT, vcpu);
6297 /* the PIR and ON have been set by L1. */
6298 if (!kvm_vcpu_trigger_posted_interrupt(vcpu, true))
6299 kvm_vcpu_kick(vcpu);
6305 * Send interrupt to vcpu via posted interrupt way.
6306 * 1. If target vcpu is running(non-root mode), send posted interrupt
6307 * notification to vcpu and hardware will sync PIR to vIRR atomically.
6308 * 2. If target vcpu isn't running(root mode), kick it to pick up the
6309 * interrupt from PIR in next vmentry.
6311 static int vmx_deliver_posted_interrupt(struct kvm_vcpu *vcpu, int vector)
6313 struct vcpu_vmx *vmx = to_vmx(vcpu);
6316 r = vmx_deliver_nested_posted_interrupt(vcpu, vector);
6320 if (!vcpu->arch.apicv_active)
6323 if (pi_test_and_set_pir(vector, &vmx->pi_desc))
6326 /* If a previous notification has sent the IPI, nothing to do. */
6327 if (pi_test_and_set_on(&vmx->pi_desc))
6330 if (!kvm_vcpu_trigger_posted_interrupt(vcpu, false))
6331 kvm_vcpu_kick(vcpu);
6337 * Set up the vmcs's constant host-state fields, i.e., host-state fields that
6338 * will not change in the lifetime of the guest.
6339 * Note that host-state that does change is set elsewhere. E.g., host-state
6340 * that is set differently for each CPU is set in vmx_vcpu_load(), not here.
6342 static void vmx_set_constant_host_state(struct vcpu_vmx *vmx)
6347 unsigned long cr0, cr3, cr4;
6350 WARN_ON(cr0 & X86_CR0_TS);
6351 vmcs_writel(HOST_CR0, cr0); /* 22.2.3 */
6354 * Save the most likely value for this task's CR3 in the VMCS.
6355 * We can't use __get_current_cr3_fast() because we're not atomic.
6358 vmcs_writel(HOST_CR3, cr3); /* 22.2.3 FIXME: shadow tables */
6359 vmx->loaded_vmcs->host_state.cr3 = cr3;
6361 /* Save the most likely value for this task's CR4 in the VMCS. */
6362 cr4 = cr4_read_shadow();
6363 vmcs_writel(HOST_CR4, cr4); /* 22.2.3, 22.2.5 */
6364 vmx->loaded_vmcs->host_state.cr4 = cr4;
6366 vmcs_write16(HOST_CS_SELECTOR, __KERNEL_CS); /* 22.2.4 */
6367 #ifdef CONFIG_X86_64
6369 * Load null selectors, so we can avoid reloading them in
6370 * vmx_prepare_switch_to_host(), in case userspace uses
6371 * the null selectors too (the expected case).
6373 vmcs_write16(HOST_DS_SELECTOR, 0);
6374 vmcs_write16(HOST_ES_SELECTOR, 0);
6376 vmcs_write16(HOST_DS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
6377 vmcs_write16(HOST_ES_SELECTOR, __KERNEL_DS); /* 22.2.4 */
6379 vmcs_write16(HOST_SS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
6380 vmcs_write16(HOST_TR_SELECTOR, GDT_ENTRY_TSS*8); /* 22.2.4 */
6383 vmcs_writel(HOST_IDTR_BASE, dt.address); /* 22.2.4 */
6384 vmx->host_idt_base = dt.address;
6386 vmcs_writel(HOST_RIP, vmx_return); /* 22.2.5 */
6388 rdmsr(MSR_IA32_SYSENTER_CS, low32, high32);
6389 vmcs_write32(HOST_IA32_SYSENTER_CS, low32);
6390 rdmsrl(MSR_IA32_SYSENTER_EIP, tmpl);
6391 vmcs_writel(HOST_IA32_SYSENTER_EIP, tmpl); /* 22.2.3 */
6393 if (vmcs_config.vmexit_ctrl & VM_EXIT_LOAD_IA32_PAT) {
6394 rdmsr(MSR_IA32_CR_PAT, low32, high32);
6395 vmcs_write64(HOST_IA32_PAT, low32 | ((u64) high32 << 32));
6399 static void set_cr4_guest_host_mask(struct vcpu_vmx *vmx)
6401 BUILD_BUG_ON(KVM_CR4_GUEST_OWNED_BITS & ~KVM_POSSIBLE_CR4_GUEST_BITS);
6403 vmx->vcpu.arch.cr4_guest_owned_bits = KVM_CR4_GUEST_OWNED_BITS;
6405 vmx->vcpu.arch.cr4_guest_owned_bits |= X86_CR4_PGE;
6406 if (is_guest_mode(&vmx->vcpu))
6407 vmx->vcpu.arch.cr4_guest_owned_bits &=
6408 ~get_vmcs12(&vmx->vcpu)->cr4_guest_host_mask;
6409 vmcs_writel(CR4_GUEST_HOST_MASK, ~vmx->vcpu.arch.cr4_guest_owned_bits);
6412 static u32 vmx_pin_based_exec_ctrl(struct vcpu_vmx *vmx)
6414 u32 pin_based_exec_ctrl = vmcs_config.pin_based_exec_ctrl;
6416 if (!kvm_vcpu_apicv_active(&vmx->vcpu))
6417 pin_based_exec_ctrl &= ~PIN_BASED_POSTED_INTR;
6420 pin_based_exec_ctrl &= ~PIN_BASED_VIRTUAL_NMIS;
6422 /* Enable the preemption timer dynamically */
6423 pin_based_exec_ctrl &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
6424 return pin_based_exec_ctrl;
6427 static void vmx_refresh_apicv_exec_ctrl(struct kvm_vcpu *vcpu)
6429 struct vcpu_vmx *vmx = to_vmx(vcpu);
6431 vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, vmx_pin_based_exec_ctrl(vmx));
6432 if (cpu_has_secondary_exec_ctrls()) {
6433 if (kvm_vcpu_apicv_active(vcpu))
6434 vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL,
6435 SECONDARY_EXEC_APIC_REGISTER_VIRT |
6436 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
6438 vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL,
6439 SECONDARY_EXEC_APIC_REGISTER_VIRT |
6440 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
6443 if (cpu_has_vmx_msr_bitmap())
6444 vmx_update_msr_bitmap(vcpu);
6447 static u32 vmx_exec_control(struct vcpu_vmx *vmx)
6449 u32 exec_control = vmcs_config.cpu_based_exec_ctrl;
6451 if (vmx->vcpu.arch.switch_db_regs & KVM_DEBUGREG_WONT_EXIT)
6452 exec_control &= ~CPU_BASED_MOV_DR_EXITING;
6454 if (!cpu_need_tpr_shadow(&vmx->vcpu)) {
6455 exec_control &= ~CPU_BASED_TPR_SHADOW;
6456 #ifdef CONFIG_X86_64
6457 exec_control |= CPU_BASED_CR8_STORE_EXITING |
6458 CPU_BASED_CR8_LOAD_EXITING;
6462 exec_control |= CPU_BASED_CR3_STORE_EXITING |
6463 CPU_BASED_CR3_LOAD_EXITING |
6464 CPU_BASED_INVLPG_EXITING;
6465 if (kvm_mwait_in_guest(vmx->vcpu.kvm))
6466 exec_control &= ~(CPU_BASED_MWAIT_EXITING |
6467 CPU_BASED_MONITOR_EXITING);
6468 if (kvm_hlt_in_guest(vmx->vcpu.kvm))
6469 exec_control &= ~CPU_BASED_HLT_EXITING;
6470 return exec_control;
6473 static bool vmx_rdrand_supported(void)
6475 return vmcs_config.cpu_based_2nd_exec_ctrl &
6476 SECONDARY_EXEC_RDRAND_EXITING;
6479 static bool vmx_rdseed_supported(void)
6481 return vmcs_config.cpu_based_2nd_exec_ctrl &
6482 SECONDARY_EXEC_RDSEED_EXITING;
6485 static void vmx_compute_secondary_exec_control(struct vcpu_vmx *vmx)
6487 struct kvm_vcpu *vcpu = &vmx->vcpu;
6489 u32 exec_control = vmcs_config.cpu_based_2nd_exec_ctrl;
6491 if (!cpu_need_virtualize_apic_accesses(vcpu))
6492 exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
6494 exec_control &= ~SECONDARY_EXEC_ENABLE_VPID;
6496 exec_control &= ~SECONDARY_EXEC_ENABLE_EPT;
6497 enable_unrestricted_guest = 0;
6499 if (!enable_unrestricted_guest)
6500 exec_control &= ~SECONDARY_EXEC_UNRESTRICTED_GUEST;
6501 if (kvm_pause_in_guest(vmx->vcpu.kvm))
6502 exec_control &= ~SECONDARY_EXEC_PAUSE_LOOP_EXITING;
6503 if (!kvm_vcpu_apicv_active(vcpu))
6504 exec_control &= ~(SECONDARY_EXEC_APIC_REGISTER_VIRT |
6505 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
6506 exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
6508 /* SECONDARY_EXEC_DESC is enabled/disabled on writes to CR4.UMIP,
6509 * in vmx_set_cr4. */
6510 exec_control &= ~SECONDARY_EXEC_DESC;
6512 /* SECONDARY_EXEC_SHADOW_VMCS is enabled when L1 executes VMPTRLD
6514 We can NOT enable shadow_vmcs here because we don't have yet
6517 exec_control &= ~SECONDARY_EXEC_SHADOW_VMCS;
6520 exec_control &= ~SECONDARY_EXEC_ENABLE_PML;
6522 if (vmx_xsaves_supported()) {
6523 /* Exposing XSAVES only when XSAVE is exposed */
6524 bool xsaves_enabled =
6525 guest_cpuid_has(vcpu, X86_FEATURE_XSAVE) &&
6526 guest_cpuid_has(vcpu, X86_FEATURE_XSAVES);
6528 if (!xsaves_enabled)
6529 exec_control &= ~SECONDARY_EXEC_XSAVES;
6533 vmx->nested.msrs.secondary_ctls_high |=
6534 SECONDARY_EXEC_XSAVES;
6536 vmx->nested.msrs.secondary_ctls_high &=
6537 ~SECONDARY_EXEC_XSAVES;
6541 if (vmx_rdtscp_supported()) {
6542 bool rdtscp_enabled = guest_cpuid_has(vcpu, X86_FEATURE_RDTSCP);
6543 if (!rdtscp_enabled)
6544 exec_control &= ~SECONDARY_EXEC_RDTSCP;
6548 vmx->nested.msrs.secondary_ctls_high |=
6549 SECONDARY_EXEC_RDTSCP;
6551 vmx->nested.msrs.secondary_ctls_high &=
6552 ~SECONDARY_EXEC_RDTSCP;
6556 if (vmx_invpcid_supported()) {
6557 /* Exposing INVPCID only when PCID is exposed */
6558 bool invpcid_enabled =
6559 guest_cpuid_has(vcpu, X86_FEATURE_INVPCID) &&
6560 guest_cpuid_has(vcpu, X86_FEATURE_PCID);
6562 if (!invpcid_enabled) {
6563 exec_control &= ~SECONDARY_EXEC_ENABLE_INVPCID;
6564 guest_cpuid_clear(vcpu, X86_FEATURE_INVPCID);
6568 if (invpcid_enabled)
6569 vmx->nested.msrs.secondary_ctls_high |=
6570 SECONDARY_EXEC_ENABLE_INVPCID;
6572 vmx->nested.msrs.secondary_ctls_high &=
6573 ~SECONDARY_EXEC_ENABLE_INVPCID;
6577 if (vmx_rdrand_supported()) {
6578 bool rdrand_enabled = guest_cpuid_has(vcpu, X86_FEATURE_RDRAND);
6580 exec_control &= ~SECONDARY_EXEC_RDRAND_EXITING;
6584 vmx->nested.msrs.secondary_ctls_high |=
6585 SECONDARY_EXEC_RDRAND_EXITING;
6587 vmx->nested.msrs.secondary_ctls_high &=
6588 ~SECONDARY_EXEC_RDRAND_EXITING;
6592 if (vmx_rdseed_supported()) {
6593 bool rdseed_enabled = guest_cpuid_has(vcpu, X86_FEATURE_RDSEED);
6595 exec_control &= ~SECONDARY_EXEC_RDSEED_EXITING;
6599 vmx->nested.msrs.secondary_ctls_high |=
6600 SECONDARY_EXEC_RDSEED_EXITING;
6602 vmx->nested.msrs.secondary_ctls_high &=
6603 ~SECONDARY_EXEC_RDSEED_EXITING;
6607 vmx->secondary_exec_control = exec_control;
6610 static void ept_set_mmio_spte_mask(void)
6613 * EPT Misconfigurations can be generated if the value of bits 2:0
6614 * of an EPT paging-structure entry is 110b (write/execute).
6616 kvm_mmu_set_mmio_spte_mask(VMX_EPT_RWX_MASK,
6617 VMX_EPT_MISCONFIG_WX_VALUE);
6620 #define VMX_XSS_EXIT_BITMAP 0
6622 * Sets up the vmcs for emulated real mode.
6624 static void vmx_vcpu_setup(struct vcpu_vmx *vmx)
6628 if (enable_shadow_vmcs) {
6630 * At vCPU creation, "VMWRITE to any supported field
6631 * in the VMCS" is supported, so use the more
6632 * permissive vmx_vmread_bitmap to specify both read
6633 * and write permissions for the shadow VMCS.
6635 vmcs_write64(VMREAD_BITMAP, __pa(vmx_vmread_bitmap));
6636 vmcs_write64(VMWRITE_BITMAP, __pa(vmx_vmread_bitmap));
6638 if (cpu_has_vmx_msr_bitmap())
6639 vmcs_write64(MSR_BITMAP, __pa(vmx->vmcs01.msr_bitmap));
6641 vmcs_write64(VMCS_LINK_POINTER, -1ull); /* 22.3.1.5 */
6644 vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, vmx_pin_based_exec_ctrl(vmx));
6645 vmx->hv_deadline_tsc = -1;
6647 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, vmx_exec_control(vmx));
6649 if (cpu_has_secondary_exec_ctrls()) {
6650 vmx_compute_secondary_exec_control(vmx);
6651 vmcs_write32(SECONDARY_VM_EXEC_CONTROL,
6652 vmx->secondary_exec_control);
6655 if (kvm_vcpu_apicv_active(&vmx->vcpu)) {
6656 vmcs_write64(EOI_EXIT_BITMAP0, 0);
6657 vmcs_write64(EOI_EXIT_BITMAP1, 0);
6658 vmcs_write64(EOI_EXIT_BITMAP2, 0);
6659 vmcs_write64(EOI_EXIT_BITMAP3, 0);
6661 vmcs_write16(GUEST_INTR_STATUS, 0);
6663 vmcs_write16(POSTED_INTR_NV, POSTED_INTR_VECTOR);
6664 vmcs_write64(POSTED_INTR_DESC_ADDR, __pa((&vmx->pi_desc)));
6667 if (!kvm_pause_in_guest(vmx->vcpu.kvm)) {
6668 vmcs_write32(PLE_GAP, ple_gap);
6669 vmx->ple_window = ple_window;
6670 vmx->ple_window_dirty = true;
6673 vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK, 0);
6674 vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH, 0);
6675 vmcs_write32(CR3_TARGET_COUNT, 0); /* 22.2.1 */
6677 vmcs_write16(HOST_FS_SELECTOR, 0); /* 22.2.4 */
6678 vmcs_write16(HOST_GS_SELECTOR, 0); /* 22.2.4 */
6679 vmx_set_constant_host_state(vmx);
6680 vmcs_writel(HOST_FS_BASE, 0); /* 22.2.4 */
6681 vmcs_writel(HOST_GS_BASE, 0); /* 22.2.4 */
6683 if (cpu_has_vmx_vmfunc())
6684 vmcs_write64(VM_FUNCTION_CONTROL, 0);
6686 vmcs_write32(VM_EXIT_MSR_STORE_COUNT, 0);
6687 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, 0);
6688 vmcs_write64(VM_EXIT_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.host.val));
6689 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, 0);
6690 vmcs_write64(VM_ENTRY_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.guest.val));
6692 if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT)
6693 vmcs_write64(GUEST_IA32_PAT, vmx->vcpu.arch.pat);
6695 for (i = 0; i < ARRAY_SIZE(vmx_msr_index); ++i) {
6696 u32 index = vmx_msr_index[i];
6697 u32 data_low, data_high;
6700 if (rdmsr_safe(index, &data_low, &data_high) < 0)
6702 if (wrmsr_safe(index, data_low, data_high) < 0)
6704 vmx->guest_msrs[j].index = i;
6705 vmx->guest_msrs[j].data = 0;
6706 vmx->guest_msrs[j].mask = -1ull;
6710 vm_exit_controls_init(vmx, vmcs_config.vmexit_ctrl);
6712 /* 22.2.1, 20.8.1 */
6713 vm_entry_controls_init(vmx, vmcs_config.vmentry_ctrl);
6715 vmx->vcpu.arch.cr0_guest_owned_bits = X86_CR0_TS;
6716 vmcs_writel(CR0_GUEST_HOST_MASK, ~X86_CR0_TS);
6718 set_cr4_guest_host_mask(vmx);
6720 if (vmx_xsaves_supported())
6721 vmcs_write64(XSS_EXIT_BITMAP, VMX_XSS_EXIT_BITMAP);
6724 ASSERT(vmx->pml_pg);
6725 vmcs_write64(PML_ADDRESS, page_to_phys(vmx->pml_pg));
6726 vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1);
6729 if (cpu_has_vmx_encls_vmexit())
6730 vmcs_write64(ENCLS_EXITING_BITMAP, -1ull);
6733 static void vmx_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event)
6735 struct vcpu_vmx *vmx = to_vmx(vcpu);
6736 struct msr_data apic_base_msr;
6739 vmx->rmode.vm86_active = 0;
6742 vcpu->arch.microcode_version = 0x100000000ULL;
6743 vmx->vcpu.arch.regs[VCPU_REGS_RDX] = get_rdx_init_val();
6744 kvm_set_cr8(vcpu, 0);
6747 apic_base_msr.data = APIC_DEFAULT_PHYS_BASE |
6748 MSR_IA32_APICBASE_ENABLE;
6749 if (kvm_vcpu_is_reset_bsp(vcpu))
6750 apic_base_msr.data |= MSR_IA32_APICBASE_BSP;
6751 apic_base_msr.host_initiated = true;
6752 kvm_set_apic_base(vcpu, &apic_base_msr);
6755 vmx_segment_cache_clear(vmx);
6757 seg_setup(VCPU_SREG_CS);
6758 vmcs_write16(GUEST_CS_SELECTOR, 0xf000);
6759 vmcs_writel(GUEST_CS_BASE, 0xffff0000ul);
6761 seg_setup(VCPU_SREG_DS);
6762 seg_setup(VCPU_SREG_ES);
6763 seg_setup(VCPU_SREG_FS);
6764 seg_setup(VCPU_SREG_GS);
6765 seg_setup(VCPU_SREG_SS);
6767 vmcs_write16(GUEST_TR_SELECTOR, 0);
6768 vmcs_writel(GUEST_TR_BASE, 0);
6769 vmcs_write32(GUEST_TR_LIMIT, 0xffff);
6770 vmcs_write32(GUEST_TR_AR_BYTES, 0x008b);
6772 vmcs_write16(GUEST_LDTR_SELECTOR, 0);
6773 vmcs_writel(GUEST_LDTR_BASE, 0);
6774 vmcs_write32(GUEST_LDTR_LIMIT, 0xffff);
6775 vmcs_write32(GUEST_LDTR_AR_BYTES, 0x00082);
6778 vmcs_write32(GUEST_SYSENTER_CS, 0);
6779 vmcs_writel(GUEST_SYSENTER_ESP, 0);
6780 vmcs_writel(GUEST_SYSENTER_EIP, 0);
6781 vmcs_write64(GUEST_IA32_DEBUGCTL, 0);
6784 kvm_set_rflags(vcpu, X86_EFLAGS_FIXED);
6785 kvm_rip_write(vcpu, 0xfff0);
6787 vmcs_writel(GUEST_GDTR_BASE, 0);
6788 vmcs_write32(GUEST_GDTR_LIMIT, 0xffff);
6790 vmcs_writel(GUEST_IDTR_BASE, 0);
6791 vmcs_write32(GUEST_IDTR_LIMIT, 0xffff);
6793 vmcs_write32(GUEST_ACTIVITY_STATE, GUEST_ACTIVITY_ACTIVE);
6794 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, 0);
6795 vmcs_writel(GUEST_PENDING_DBG_EXCEPTIONS, 0);
6796 if (kvm_mpx_supported())
6797 vmcs_write64(GUEST_BNDCFGS, 0);
6801 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0); /* 22.2.1 */
6803 if (cpu_has_vmx_tpr_shadow() && !init_event) {
6804 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, 0);
6805 if (cpu_need_tpr_shadow(vcpu))
6806 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR,
6807 __pa(vcpu->arch.apic->regs));
6808 vmcs_write32(TPR_THRESHOLD, 0);
6811 kvm_make_request(KVM_REQ_APIC_PAGE_RELOAD, vcpu);
6814 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid);
6816 cr0 = X86_CR0_NW | X86_CR0_CD | X86_CR0_ET;
6817 vmx->vcpu.arch.cr0 = cr0;
6818 vmx_set_cr0(vcpu, cr0); /* enter rmode */
6819 vmx_set_cr4(vcpu, 0);
6820 vmx_set_efer(vcpu, 0);
6822 update_exception_bitmap(vcpu);
6824 vpid_sync_context(vmx->vpid);
6826 vmx_clear_hlt(vcpu);
6828 vmx_update_fb_clear_dis(vcpu, vmx);
6832 * In nested virtualization, check if L1 asked to exit on external interrupts.
6833 * For most existing hypervisors, this will always return true.
6835 static bool nested_exit_on_intr(struct kvm_vcpu *vcpu)
6837 return get_vmcs12(vcpu)->pin_based_vm_exec_control &
6838 PIN_BASED_EXT_INTR_MASK;
6842 * In nested virtualization, check if L1 has set
6843 * VM_EXIT_ACK_INTR_ON_EXIT
6845 static bool nested_exit_intr_ack_set(struct kvm_vcpu *vcpu)
6847 return get_vmcs12(vcpu)->vm_exit_controls &
6848 VM_EXIT_ACK_INTR_ON_EXIT;
6851 static bool nested_exit_on_nmi(struct kvm_vcpu *vcpu)
6853 return nested_cpu_has_nmi_exiting(get_vmcs12(vcpu));
6856 static void enable_irq_window(struct kvm_vcpu *vcpu)
6858 vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL,
6859 CPU_BASED_VIRTUAL_INTR_PENDING);
6862 static void enable_nmi_window(struct kvm_vcpu *vcpu)
6865 vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & GUEST_INTR_STATE_STI) {
6866 enable_irq_window(vcpu);
6870 vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL,
6871 CPU_BASED_VIRTUAL_NMI_PENDING);
6874 static void vmx_inject_irq(struct kvm_vcpu *vcpu)
6876 struct vcpu_vmx *vmx = to_vmx(vcpu);
6878 int irq = vcpu->arch.interrupt.nr;
6880 trace_kvm_inj_virq(irq);
6882 ++vcpu->stat.irq_injections;
6883 if (vmx->rmode.vm86_active) {
6885 if (vcpu->arch.interrupt.soft)
6886 inc_eip = vcpu->arch.event_exit_inst_len;
6887 if (kvm_inject_realmode_interrupt(vcpu, irq, inc_eip) != EMULATE_DONE)
6888 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
6891 intr = irq | INTR_INFO_VALID_MASK;
6892 if (vcpu->arch.interrupt.soft) {
6893 intr |= INTR_TYPE_SOFT_INTR;
6894 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
6895 vmx->vcpu.arch.event_exit_inst_len);
6897 intr |= INTR_TYPE_EXT_INTR;
6898 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr);
6900 vmx_clear_hlt(vcpu);
6903 static void vmx_inject_nmi(struct kvm_vcpu *vcpu)
6905 struct vcpu_vmx *vmx = to_vmx(vcpu);
6909 * Tracking the NMI-blocked state in software is built upon
6910 * finding the next open IRQ window. This, in turn, depends on
6911 * well-behaving guests: They have to keep IRQs disabled at
6912 * least as long as the NMI handler runs. Otherwise we may
6913 * cause NMI nesting, maybe breaking the guest. But as this is
6914 * highly unlikely, we can live with the residual risk.
6916 vmx->loaded_vmcs->soft_vnmi_blocked = 1;
6917 vmx->loaded_vmcs->vnmi_blocked_time = 0;
6920 ++vcpu->stat.nmi_injections;
6921 vmx->loaded_vmcs->nmi_known_unmasked = false;
6923 if (vmx->rmode.vm86_active) {
6924 if (kvm_inject_realmode_interrupt(vcpu, NMI_VECTOR, 0) != EMULATE_DONE)
6925 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
6929 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
6930 INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK | NMI_VECTOR);
6932 vmx_clear_hlt(vcpu);
6935 static bool vmx_get_nmi_mask(struct kvm_vcpu *vcpu)
6937 struct vcpu_vmx *vmx = to_vmx(vcpu);
6941 return vmx->loaded_vmcs->soft_vnmi_blocked;
6942 if (vmx->loaded_vmcs->nmi_known_unmasked)
6944 masked = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & GUEST_INTR_STATE_NMI;
6945 vmx->loaded_vmcs->nmi_known_unmasked = !masked;
6949 static void vmx_set_nmi_mask(struct kvm_vcpu *vcpu, bool masked)
6951 struct vcpu_vmx *vmx = to_vmx(vcpu);
6954 if (vmx->loaded_vmcs->soft_vnmi_blocked != masked) {
6955 vmx->loaded_vmcs->soft_vnmi_blocked = masked;
6956 vmx->loaded_vmcs->vnmi_blocked_time = 0;
6959 vmx->loaded_vmcs->nmi_known_unmasked = !masked;
6961 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
6962 GUEST_INTR_STATE_NMI);
6964 vmcs_clear_bits(GUEST_INTERRUPTIBILITY_INFO,
6965 GUEST_INTR_STATE_NMI);
6969 static int vmx_nmi_allowed(struct kvm_vcpu *vcpu)
6971 if (to_vmx(vcpu)->nested.nested_run_pending)
6975 to_vmx(vcpu)->loaded_vmcs->soft_vnmi_blocked)
6978 return !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) &
6979 (GUEST_INTR_STATE_MOV_SS | GUEST_INTR_STATE_STI
6980 | GUEST_INTR_STATE_NMI));
6983 static int vmx_interrupt_allowed(struct kvm_vcpu *vcpu)
6985 if (to_vmx(vcpu)->nested.nested_run_pending)
6988 if (is_guest_mode(vcpu) && nested_exit_on_intr(vcpu))
6991 return (vmcs_readl(GUEST_RFLAGS) & X86_EFLAGS_IF) &&
6992 !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) &
6993 (GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS));
6996 static int vmx_set_tss_addr(struct kvm *kvm, unsigned int addr)
7000 if (enable_unrestricted_guest)
7003 ret = x86_set_memory_region(kvm, TSS_PRIVATE_MEMSLOT, addr,
7007 to_kvm_vmx(kvm)->tss_addr = addr;
7008 return init_rmode_tss(kvm);
7011 static int vmx_set_identity_map_addr(struct kvm *kvm, u64 ident_addr)
7013 to_kvm_vmx(kvm)->ept_identity_map_addr = ident_addr;
7017 static bool rmode_exception(struct kvm_vcpu *vcpu, int vec)
7022 * Update instruction length as we may reinject the exception
7023 * from user space while in guest debugging mode.
7025 to_vmx(vcpu)->vcpu.arch.event_exit_inst_len =
7026 vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
7027 if (vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
7031 if (vcpu->guest_debug &
7032 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))
7049 static int handle_rmode_exception(struct kvm_vcpu *vcpu,
7050 int vec, u32 err_code)
7053 * Instruction with address size override prefix opcode 0x67
7054 * Cause the #SS fault with 0 error code in VM86 mode.
7056 if (((vec == GP_VECTOR) || (vec == SS_VECTOR)) && err_code == 0) {
7057 if (kvm_emulate_instruction(vcpu, 0) == EMULATE_DONE) {
7058 if (vcpu->arch.halt_request) {
7059 vcpu->arch.halt_request = 0;
7060 return kvm_vcpu_halt(vcpu);
7068 * Forward all other exceptions that are valid in real mode.
7069 * FIXME: Breaks guest debugging in real mode, needs to be fixed with
7070 * the required debugging infrastructure rework.
7072 kvm_queue_exception(vcpu, vec);
7077 * Trigger machine check on the host. We assume all the MSRs are already set up
7078 * by the CPU and that we still run on the same CPU as the MCE occurred on.
7079 * We pass a fake environment to the machine check handler because we want
7080 * the guest to be always treated like user space, no matter what context
7081 * it used internally.
7083 static void kvm_machine_check(void)
7085 #if defined(CONFIG_X86_MCE)
7086 struct pt_regs regs = {
7087 .cs = 3, /* Fake ring 3 no matter what the guest ran on */
7088 .flags = X86_EFLAGS_IF,
7091 do_machine_check(®s, 0);
7095 static int handle_machine_check(struct kvm_vcpu *vcpu)
7097 /* already handled by vcpu_run */
7101 static int handle_exception(struct kvm_vcpu *vcpu)
7103 struct vcpu_vmx *vmx = to_vmx(vcpu);
7104 struct kvm_run *kvm_run = vcpu->run;
7105 u32 intr_info, ex_no, error_code;
7106 unsigned long cr2, rip, dr6;
7108 enum emulation_result er;
7110 vect_info = vmx->idt_vectoring_info;
7111 intr_info = vmx->exit_intr_info;
7113 if (is_machine_check(intr_info))
7114 return handle_machine_check(vcpu);
7116 if (is_nmi(intr_info))
7117 return 1; /* already handled by vmx_vcpu_run() */
7119 if (is_invalid_opcode(intr_info))
7120 return handle_ud(vcpu);
7123 if (intr_info & INTR_INFO_DELIVER_CODE_MASK)
7124 error_code = vmcs_read32(VM_EXIT_INTR_ERROR_CODE);
7126 if (!vmx->rmode.vm86_active && is_gp_fault(intr_info)) {
7127 WARN_ON_ONCE(!enable_vmware_backdoor);
7128 er = kvm_emulate_instruction(vcpu,
7129 EMULTYPE_VMWARE | EMULTYPE_NO_UD_ON_FAIL);
7130 if (er == EMULATE_USER_EXIT)
7132 else if (er != EMULATE_DONE)
7133 kvm_queue_exception_e(vcpu, GP_VECTOR, error_code);
7138 * The #PF with PFEC.RSVD = 1 indicates the guest is accessing
7139 * MMIO, it is better to report an internal error.
7140 * See the comments in vmx_handle_exit.
7142 if ((vect_info & VECTORING_INFO_VALID_MASK) &&
7143 !(is_page_fault(intr_info) && !(error_code & PFERR_RSVD_MASK))) {
7144 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
7145 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_SIMUL_EX;
7146 vcpu->run->internal.ndata = 3;
7147 vcpu->run->internal.data[0] = vect_info;
7148 vcpu->run->internal.data[1] = intr_info;
7149 vcpu->run->internal.data[2] = error_code;
7153 if (is_page_fault(intr_info)) {
7154 cr2 = vmcs_readl(EXIT_QUALIFICATION);
7155 /* EPT won't cause page fault directly */
7156 WARN_ON_ONCE(!vcpu->arch.apf.host_apf_reason && enable_ept);
7157 return kvm_handle_page_fault(vcpu, error_code, cr2, NULL, 0);
7160 ex_no = intr_info & INTR_INFO_VECTOR_MASK;
7162 if (vmx->rmode.vm86_active && rmode_exception(vcpu, ex_no))
7163 return handle_rmode_exception(vcpu, ex_no, error_code);
7167 kvm_queue_exception_e(vcpu, AC_VECTOR, error_code);
7170 dr6 = vmcs_readl(EXIT_QUALIFICATION);
7171 if (!(vcpu->guest_debug &
7172 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))) {
7173 vcpu->arch.dr6 &= ~15;
7174 vcpu->arch.dr6 |= dr6 | DR6_RTM;
7175 if (is_icebp(intr_info))
7176 skip_emulated_instruction(vcpu);
7178 kvm_queue_exception(vcpu, DB_VECTOR);
7181 kvm_run->debug.arch.dr6 = dr6 | DR6_FIXED_1;
7182 kvm_run->debug.arch.dr7 = vmcs_readl(GUEST_DR7);
7186 * Update instruction length as we may reinject #BP from
7187 * user space while in guest debugging mode. Reading it for
7188 * #DB as well causes no harm, it is not used in that case.
7190 vmx->vcpu.arch.event_exit_inst_len =
7191 vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
7192 kvm_run->exit_reason = KVM_EXIT_DEBUG;
7193 rip = kvm_rip_read(vcpu);
7194 kvm_run->debug.arch.pc = vmcs_readl(GUEST_CS_BASE) + rip;
7195 kvm_run->debug.arch.exception = ex_no;
7198 kvm_run->exit_reason = KVM_EXIT_EXCEPTION;
7199 kvm_run->ex.exception = ex_no;
7200 kvm_run->ex.error_code = error_code;
7206 static int handle_external_interrupt(struct kvm_vcpu *vcpu)
7208 ++vcpu->stat.irq_exits;
7212 static int handle_triple_fault(struct kvm_vcpu *vcpu)
7214 vcpu->run->exit_reason = KVM_EXIT_SHUTDOWN;
7215 vcpu->mmio_needed = 0;
7219 static int handle_io(struct kvm_vcpu *vcpu)
7221 unsigned long exit_qualification;
7222 int size, in, string;
7225 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7226 string = (exit_qualification & 16) != 0;
7228 ++vcpu->stat.io_exits;
7231 return kvm_emulate_instruction(vcpu, 0) == EMULATE_DONE;
7233 port = exit_qualification >> 16;
7234 size = (exit_qualification & 7) + 1;
7235 in = (exit_qualification & 8) != 0;
7237 return kvm_fast_pio(vcpu, size, port, in);
7241 vmx_patch_hypercall(struct kvm_vcpu *vcpu, unsigned char *hypercall)
7244 * Patch in the VMCALL instruction:
7246 hypercall[0] = 0x0f;
7247 hypercall[1] = 0x01;
7248 hypercall[2] = 0xc1;
7251 /* called to set cr0 as appropriate for a mov-to-cr0 exit. */
7252 static int handle_set_cr0(struct kvm_vcpu *vcpu, unsigned long val)
7254 if (is_guest_mode(vcpu)) {
7255 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
7256 unsigned long orig_val = val;
7259 * We get here when L2 changed cr0 in a way that did not change
7260 * any of L1's shadowed bits (see nested_vmx_exit_handled_cr),
7261 * but did change L0 shadowed bits. So we first calculate the
7262 * effective cr0 value that L1 would like to write into the
7263 * hardware. It consists of the L2-owned bits from the new
7264 * value combined with the L1-owned bits from L1's guest_cr0.
7266 val = (val & ~vmcs12->cr0_guest_host_mask) |
7267 (vmcs12->guest_cr0 & vmcs12->cr0_guest_host_mask);
7269 if (!nested_guest_cr0_valid(vcpu, val))
7272 if (kvm_set_cr0(vcpu, val))
7274 vmcs_writel(CR0_READ_SHADOW, orig_val);
7277 if (to_vmx(vcpu)->nested.vmxon &&
7278 !nested_host_cr0_valid(vcpu, val))
7281 return kvm_set_cr0(vcpu, val);
7285 static int handle_set_cr4(struct kvm_vcpu *vcpu, unsigned long val)
7287 if (is_guest_mode(vcpu)) {
7288 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
7289 unsigned long orig_val = val;
7291 /* analogously to handle_set_cr0 */
7292 val = (val & ~vmcs12->cr4_guest_host_mask) |
7293 (vmcs12->guest_cr4 & vmcs12->cr4_guest_host_mask);
7294 if (kvm_set_cr4(vcpu, val))
7296 vmcs_writel(CR4_READ_SHADOW, orig_val);
7299 return kvm_set_cr4(vcpu, val);
7302 static int handle_desc(struct kvm_vcpu *vcpu)
7304 WARN_ON(!(vcpu->arch.cr4 & X86_CR4_UMIP));
7305 return kvm_emulate_instruction(vcpu, 0) == EMULATE_DONE;
7308 static int handle_cr(struct kvm_vcpu *vcpu)
7310 unsigned long exit_qualification, val;
7316 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7317 cr = exit_qualification & 15;
7318 reg = (exit_qualification >> 8) & 15;
7319 switch ((exit_qualification >> 4) & 3) {
7320 case 0: /* mov to cr */
7321 val = kvm_register_readl(vcpu, reg);
7322 trace_kvm_cr_write(cr, val);
7325 err = handle_set_cr0(vcpu, val);
7326 return kvm_complete_insn_gp(vcpu, err);
7328 WARN_ON_ONCE(enable_unrestricted_guest);
7329 err = kvm_set_cr3(vcpu, val);
7330 return kvm_complete_insn_gp(vcpu, err);
7332 err = handle_set_cr4(vcpu, val);
7333 return kvm_complete_insn_gp(vcpu, err);
7335 u8 cr8_prev = kvm_get_cr8(vcpu);
7337 err = kvm_set_cr8(vcpu, cr8);
7338 ret = kvm_complete_insn_gp(vcpu, err);
7339 if (lapic_in_kernel(vcpu))
7341 if (cr8_prev <= cr8)
7344 * TODO: we might be squashing a
7345 * KVM_GUESTDBG_SINGLESTEP-triggered
7346 * KVM_EXIT_DEBUG here.
7348 vcpu->run->exit_reason = KVM_EXIT_SET_TPR;
7354 WARN_ONCE(1, "Guest should always own CR0.TS");
7355 vmx_set_cr0(vcpu, kvm_read_cr0_bits(vcpu, ~X86_CR0_TS));
7356 trace_kvm_cr_write(0, kvm_read_cr0(vcpu));
7357 return kvm_skip_emulated_instruction(vcpu);
7358 case 1: /*mov from cr*/
7361 WARN_ON_ONCE(enable_unrestricted_guest);
7362 val = kvm_read_cr3(vcpu);
7363 kvm_register_write(vcpu, reg, val);
7364 trace_kvm_cr_read(cr, val);
7365 return kvm_skip_emulated_instruction(vcpu);
7367 val = kvm_get_cr8(vcpu);
7368 kvm_register_write(vcpu, reg, val);
7369 trace_kvm_cr_read(cr, val);
7370 return kvm_skip_emulated_instruction(vcpu);
7374 val = (exit_qualification >> LMSW_SOURCE_DATA_SHIFT) & 0x0f;
7375 trace_kvm_cr_write(0, (kvm_read_cr0(vcpu) & ~0xful) | val);
7376 kvm_lmsw(vcpu, val);
7378 return kvm_skip_emulated_instruction(vcpu);
7382 vcpu->run->exit_reason = 0;
7383 vcpu_unimpl(vcpu, "unhandled control register: op %d cr %d\n",
7384 (int)(exit_qualification >> 4) & 3, cr);
7388 static int handle_dr(struct kvm_vcpu *vcpu)
7390 unsigned long exit_qualification;
7393 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7394 dr = exit_qualification & DEBUG_REG_ACCESS_NUM;
7396 /* First, if DR does not exist, trigger UD */
7397 if (!kvm_require_dr(vcpu, dr))
7400 /* Do not handle if the CPL > 0, will trigger GP on re-entry */
7401 if (!kvm_require_cpl(vcpu, 0))
7403 dr7 = vmcs_readl(GUEST_DR7);
7406 * As the vm-exit takes precedence over the debug trap, we
7407 * need to emulate the latter, either for the host or the
7408 * guest debugging itself.
7410 if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP) {
7411 vcpu->run->debug.arch.dr6 = vcpu->arch.dr6;
7412 vcpu->run->debug.arch.dr7 = dr7;
7413 vcpu->run->debug.arch.pc = kvm_get_linear_rip(vcpu);
7414 vcpu->run->debug.arch.exception = DB_VECTOR;
7415 vcpu->run->exit_reason = KVM_EXIT_DEBUG;
7418 vcpu->arch.dr6 &= ~15;
7419 vcpu->arch.dr6 |= DR6_BD | DR6_RTM;
7420 kvm_queue_exception(vcpu, DB_VECTOR);
7425 if (vcpu->guest_debug == 0) {
7426 vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL,
7427 CPU_BASED_MOV_DR_EXITING);
7430 * No more DR vmexits; force a reload of the debug registers
7431 * and reenter on this instruction. The next vmexit will
7432 * retrieve the full state of the debug registers.
7434 vcpu->arch.switch_db_regs |= KVM_DEBUGREG_WONT_EXIT;
7438 reg = DEBUG_REG_ACCESS_REG(exit_qualification);
7439 if (exit_qualification & TYPE_MOV_FROM_DR) {
7442 if (kvm_get_dr(vcpu, dr, &val))
7444 kvm_register_write(vcpu, reg, val);
7446 if (kvm_set_dr(vcpu, dr, kvm_register_readl(vcpu, reg)))
7449 return kvm_skip_emulated_instruction(vcpu);
7452 static u64 vmx_get_dr6(struct kvm_vcpu *vcpu)
7454 return vcpu->arch.dr6;
7457 static void vmx_set_dr6(struct kvm_vcpu *vcpu, unsigned long val)
7461 static void vmx_sync_dirty_debug_regs(struct kvm_vcpu *vcpu)
7463 get_debugreg(vcpu->arch.db[0], 0);
7464 get_debugreg(vcpu->arch.db[1], 1);
7465 get_debugreg(vcpu->arch.db[2], 2);
7466 get_debugreg(vcpu->arch.db[3], 3);
7467 get_debugreg(vcpu->arch.dr6, 6);
7468 vcpu->arch.dr7 = vmcs_readl(GUEST_DR7);
7470 vcpu->arch.switch_db_regs &= ~KVM_DEBUGREG_WONT_EXIT;
7471 vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL, CPU_BASED_MOV_DR_EXITING);
7474 static void vmx_set_dr7(struct kvm_vcpu *vcpu, unsigned long val)
7476 vmcs_writel(GUEST_DR7, val);
7479 static int handle_cpuid(struct kvm_vcpu *vcpu)
7481 return kvm_emulate_cpuid(vcpu);
7484 static int handle_rdmsr(struct kvm_vcpu *vcpu)
7486 u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX];
7487 struct msr_data msr_info;
7489 msr_info.index = ecx;
7490 msr_info.host_initiated = false;
7491 if (vmx_get_msr(vcpu, &msr_info)) {
7492 trace_kvm_msr_read_ex(ecx);
7493 kvm_inject_gp(vcpu, 0);
7497 trace_kvm_msr_read(ecx, msr_info.data);
7499 /* FIXME: handling of bits 32:63 of rax, rdx */
7500 vcpu->arch.regs[VCPU_REGS_RAX] = msr_info.data & -1u;
7501 vcpu->arch.regs[VCPU_REGS_RDX] = (msr_info.data >> 32) & -1u;
7502 return kvm_skip_emulated_instruction(vcpu);
7505 static int handle_wrmsr(struct kvm_vcpu *vcpu)
7507 struct msr_data msr;
7508 u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX];
7509 u64 data = (vcpu->arch.regs[VCPU_REGS_RAX] & -1u)
7510 | ((u64)(vcpu->arch.regs[VCPU_REGS_RDX] & -1u) << 32);
7514 msr.host_initiated = false;
7515 if (kvm_set_msr(vcpu, &msr) != 0) {
7516 trace_kvm_msr_write_ex(ecx, data);
7517 kvm_inject_gp(vcpu, 0);
7521 trace_kvm_msr_write(ecx, data);
7522 return kvm_skip_emulated_instruction(vcpu);
7525 static int handle_tpr_below_threshold(struct kvm_vcpu *vcpu)
7527 kvm_apic_update_ppr(vcpu);
7531 static int handle_interrupt_window(struct kvm_vcpu *vcpu)
7533 vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL,
7534 CPU_BASED_VIRTUAL_INTR_PENDING);
7536 kvm_make_request(KVM_REQ_EVENT, vcpu);
7538 ++vcpu->stat.irq_window_exits;
7542 static int handle_halt(struct kvm_vcpu *vcpu)
7544 return kvm_emulate_halt(vcpu);
7547 static int handle_vmcall(struct kvm_vcpu *vcpu)
7549 return kvm_emulate_hypercall(vcpu);
7552 static int handle_invd(struct kvm_vcpu *vcpu)
7554 return kvm_emulate_instruction(vcpu, 0) == EMULATE_DONE;
7557 static int handle_invlpg(struct kvm_vcpu *vcpu)
7559 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7561 kvm_mmu_invlpg(vcpu, exit_qualification);
7562 return kvm_skip_emulated_instruction(vcpu);
7565 static int handle_rdpmc(struct kvm_vcpu *vcpu)
7569 err = kvm_rdpmc(vcpu);
7570 return kvm_complete_insn_gp(vcpu, err);
7573 static int handle_wbinvd(struct kvm_vcpu *vcpu)
7575 return kvm_emulate_wbinvd(vcpu);
7578 static int handle_xsetbv(struct kvm_vcpu *vcpu)
7580 u64 new_bv = kvm_read_edx_eax(vcpu);
7581 u32 index = kvm_register_read(vcpu, VCPU_REGS_RCX);
7583 if (kvm_set_xcr(vcpu, index, new_bv) == 0)
7584 return kvm_skip_emulated_instruction(vcpu);
7588 static int handle_xsaves(struct kvm_vcpu *vcpu)
7590 kvm_skip_emulated_instruction(vcpu);
7591 WARN(1, "this should never happen\n");
7595 static int handle_xrstors(struct kvm_vcpu *vcpu)
7597 kvm_skip_emulated_instruction(vcpu);
7598 WARN(1, "this should never happen\n");
7602 static int handle_apic_access(struct kvm_vcpu *vcpu)
7604 if (likely(fasteoi)) {
7605 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7606 int access_type, offset;
7608 access_type = exit_qualification & APIC_ACCESS_TYPE;
7609 offset = exit_qualification & APIC_ACCESS_OFFSET;
7611 * Sane guest uses MOV to write EOI, with written value
7612 * not cared. So make a short-circuit here by avoiding
7613 * heavy instruction emulation.
7615 if ((access_type == TYPE_LINEAR_APIC_INST_WRITE) &&
7616 (offset == APIC_EOI)) {
7617 kvm_lapic_set_eoi(vcpu);
7618 return kvm_skip_emulated_instruction(vcpu);
7621 return kvm_emulate_instruction(vcpu, 0) == EMULATE_DONE;
7624 static int handle_apic_eoi_induced(struct kvm_vcpu *vcpu)
7626 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7627 int vector = exit_qualification & 0xff;
7629 /* EOI-induced VM exit is trap-like and thus no need to adjust IP */
7630 kvm_apic_set_eoi_accelerated(vcpu, vector);
7634 static int handle_apic_write(struct kvm_vcpu *vcpu)
7636 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7637 u32 offset = exit_qualification & 0xfff;
7639 /* APIC-write VM exit is trap-like and thus no need to adjust IP */
7640 kvm_apic_write_nodecode(vcpu, offset);
7644 static int handle_task_switch(struct kvm_vcpu *vcpu)
7646 struct vcpu_vmx *vmx = to_vmx(vcpu);
7647 unsigned long exit_qualification;
7648 bool has_error_code = false;
7651 int reason, type, idt_v, idt_index;
7653 idt_v = (vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK);
7654 idt_index = (vmx->idt_vectoring_info & VECTORING_INFO_VECTOR_MASK);
7655 type = (vmx->idt_vectoring_info & VECTORING_INFO_TYPE_MASK);
7657 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7659 reason = (u32)exit_qualification >> 30;
7660 if (reason == TASK_SWITCH_GATE && idt_v) {
7662 case INTR_TYPE_NMI_INTR:
7663 vcpu->arch.nmi_injected = false;
7664 vmx_set_nmi_mask(vcpu, true);
7666 case INTR_TYPE_EXT_INTR:
7667 case INTR_TYPE_SOFT_INTR:
7668 kvm_clear_interrupt_queue(vcpu);
7670 case INTR_TYPE_HARD_EXCEPTION:
7671 if (vmx->idt_vectoring_info &
7672 VECTORING_INFO_DELIVER_CODE_MASK) {
7673 has_error_code = true;
7675 vmcs_read32(IDT_VECTORING_ERROR_CODE);
7678 case INTR_TYPE_SOFT_EXCEPTION:
7679 kvm_clear_exception_queue(vcpu);
7685 tss_selector = exit_qualification;
7687 if (!idt_v || (type != INTR_TYPE_HARD_EXCEPTION &&
7688 type != INTR_TYPE_EXT_INTR &&
7689 type != INTR_TYPE_NMI_INTR))
7690 skip_emulated_instruction(vcpu);
7692 if (kvm_task_switch(vcpu, tss_selector,
7693 type == INTR_TYPE_SOFT_INTR ? idt_index : -1, reason,
7694 has_error_code, error_code) == EMULATE_FAIL) {
7695 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
7696 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
7697 vcpu->run->internal.ndata = 0;
7702 * TODO: What about debug traps on tss switch?
7703 * Are we supposed to inject them and update dr6?
7709 static int handle_ept_violation(struct kvm_vcpu *vcpu)
7711 unsigned long exit_qualification;
7715 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7718 * EPT violation happened while executing iret from NMI,
7719 * "blocked by NMI" bit has to be set before next VM entry.
7720 * There are errata that may cause this bit to not be set:
7723 if (!(to_vmx(vcpu)->idt_vectoring_info & VECTORING_INFO_VALID_MASK) &&
7725 (exit_qualification & INTR_INFO_UNBLOCK_NMI))
7726 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO, GUEST_INTR_STATE_NMI);
7728 gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS);
7729 trace_kvm_page_fault(gpa, exit_qualification);
7731 /* Is it a read fault? */
7732 error_code = (exit_qualification & EPT_VIOLATION_ACC_READ)
7733 ? PFERR_USER_MASK : 0;
7734 /* Is it a write fault? */
7735 error_code |= (exit_qualification & EPT_VIOLATION_ACC_WRITE)
7736 ? PFERR_WRITE_MASK : 0;
7737 /* Is it a fetch fault? */
7738 error_code |= (exit_qualification & EPT_VIOLATION_ACC_INSTR)
7739 ? PFERR_FETCH_MASK : 0;
7740 /* ept page table entry is present? */
7741 error_code |= (exit_qualification &
7742 (EPT_VIOLATION_READABLE | EPT_VIOLATION_WRITABLE |
7743 EPT_VIOLATION_EXECUTABLE))
7744 ? PFERR_PRESENT_MASK : 0;
7746 error_code |= (exit_qualification & 0x100) != 0 ?
7747 PFERR_GUEST_FINAL_MASK : PFERR_GUEST_PAGE_MASK;
7749 vcpu->arch.exit_qualification = exit_qualification;
7750 return kvm_mmu_page_fault(vcpu, gpa, error_code, NULL, 0);
7753 static int handle_ept_misconfig(struct kvm_vcpu *vcpu)
7758 * A nested guest cannot optimize MMIO vmexits, because we have an
7759 * nGPA here instead of the required GPA.
7761 gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS);
7762 if (!is_guest_mode(vcpu) &&
7763 !kvm_io_bus_write(vcpu, KVM_FAST_MMIO_BUS, gpa, 0, NULL)) {
7764 trace_kvm_fast_mmio(gpa);
7766 * Doing kvm_skip_emulated_instruction() depends on undefined
7767 * behavior: Intel's manual doesn't mandate
7768 * VM_EXIT_INSTRUCTION_LEN to be set in VMCS when EPT MISCONFIG
7769 * occurs and while on real hardware it was observed to be set,
7770 * other hypervisors (namely Hyper-V) don't set it, we end up
7771 * advancing IP with some random value. Disable fast mmio when
7772 * running nested and keep it for real hardware in hope that
7773 * VM_EXIT_INSTRUCTION_LEN will always be set correctly.
7775 if (!static_cpu_has(X86_FEATURE_HYPERVISOR))
7776 return kvm_skip_emulated_instruction(vcpu);
7778 return kvm_emulate_instruction(vcpu, EMULTYPE_SKIP) ==
7782 return kvm_mmu_page_fault(vcpu, gpa, PFERR_RSVD_MASK, NULL, 0);
7785 static int handle_nmi_window(struct kvm_vcpu *vcpu)
7787 WARN_ON_ONCE(!enable_vnmi);
7788 vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL,
7789 CPU_BASED_VIRTUAL_NMI_PENDING);
7790 ++vcpu->stat.nmi_window_exits;
7791 kvm_make_request(KVM_REQ_EVENT, vcpu);
7796 static int handle_invalid_guest_state(struct kvm_vcpu *vcpu)
7798 struct vcpu_vmx *vmx = to_vmx(vcpu);
7799 enum emulation_result err = EMULATE_DONE;
7802 bool intr_window_requested;
7803 unsigned count = 130;
7806 * We should never reach the point where we are emulating L2
7807 * due to invalid guest state as that means we incorrectly
7808 * allowed a nested VMEntry with an invalid vmcs12.
7810 WARN_ON_ONCE(vmx->emulation_required && vmx->nested.nested_run_pending);
7812 cpu_exec_ctrl = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
7813 intr_window_requested = cpu_exec_ctrl & CPU_BASED_VIRTUAL_INTR_PENDING;
7815 while (vmx->emulation_required && count-- != 0) {
7816 if (intr_window_requested && vmx_interrupt_allowed(vcpu))
7817 return handle_interrupt_window(&vmx->vcpu);
7819 if (kvm_test_request(KVM_REQ_EVENT, vcpu))
7822 err = kvm_emulate_instruction(vcpu, 0);
7824 if (err == EMULATE_USER_EXIT) {
7825 ++vcpu->stat.mmio_exits;
7830 if (err != EMULATE_DONE)
7831 goto emulation_error;
7833 if (vmx->emulation_required && !vmx->rmode.vm86_active &&
7834 vcpu->arch.exception.pending)
7835 goto emulation_error;
7837 if (vcpu->arch.halt_request) {
7838 vcpu->arch.halt_request = 0;
7839 ret = kvm_vcpu_halt(vcpu);
7843 if (signal_pending(current))
7853 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
7854 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
7855 vcpu->run->internal.ndata = 0;
7859 static void grow_ple_window(struct kvm_vcpu *vcpu)
7861 struct vcpu_vmx *vmx = to_vmx(vcpu);
7862 int old = vmx->ple_window;
7864 vmx->ple_window = __grow_ple_window(old, ple_window,
7868 if (vmx->ple_window != old)
7869 vmx->ple_window_dirty = true;
7871 trace_kvm_ple_window_grow(vcpu->vcpu_id, vmx->ple_window, old);
7874 static void shrink_ple_window(struct kvm_vcpu *vcpu)
7876 struct vcpu_vmx *vmx = to_vmx(vcpu);
7877 int old = vmx->ple_window;
7879 vmx->ple_window = __shrink_ple_window(old, ple_window,
7883 if (vmx->ple_window != old)
7884 vmx->ple_window_dirty = true;
7886 trace_kvm_ple_window_shrink(vcpu->vcpu_id, vmx->ple_window, old);
7890 * Handler for POSTED_INTERRUPT_WAKEUP_VECTOR.
7892 static void wakeup_handler(void)
7894 struct kvm_vcpu *vcpu;
7895 int cpu = smp_processor_id();
7897 spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, cpu));
7898 list_for_each_entry(vcpu, &per_cpu(blocked_vcpu_on_cpu, cpu),
7899 blocked_vcpu_list) {
7900 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
7902 if (pi_test_on(pi_desc) == 1)
7903 kvm_vcpu_kick(vcpu);
7905 spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, cpu));
7908 static void vmx_enable_tdp(void)
7910 kvm_mmu_set_mask_ptes(VMX_EPT_READABLE_MASK,
7911 enable_ept_ad_bits ? VMX_EPT_ACCESS_BIT : 0ull,
7912 enable_ept_ad_bits ? VMX_EPT_DIRTY_BIT : 0ull,
7913 0ull, VMX_EPT_EXECUTABLE_MASK,
7914 cpu_has_vmx_ept_execute_only() ? 0ull : VMX_EPT_READABLE_MASK,
7915 VMX_EPT_RWX_MASK, 0ull);
7917 ept_set_mmio_spte_mask();
7921 static __init int hardware_setup(void)
7923 unsigned long host_bndcfgs;
7926 rdmsrl_safe(MSR_EFER, &host_efer);
7928 for (i = 0; i < ARRAY_SIZE(vmx_msr_index); ++i)
7929 kvm_define_shared_msr(i, vmx_msr_index[i]);
7931 for (i = 0; i < VMX_BITMAP_NR; i++) {
7932 vmx_bitmap[i] = (unsigned long *)__get_free_page(GFP_KERNEL);
7937 memset(vmx_vmread_bitmap, 0xff, PAGE_SIZE);
7938 memset(vmx_vmwrite_bitmap, 0xff, PAGE_SIZE);
7940 if (setup_vmcs_config(&vmcs_config) < 0) {
7945 if (boot_cpu_has(X86_FEATURE_NX))
7946 kvm_enable_efer_bits(EFER_NX);
7948 if (boot_cpu_has(X86_FEATURE_MPX)) {
7949 rdmsrl(MSR_IA32_BNDCFGS, host_bndcfgs);
7950 WARN_ONCE(host_bndcfgs, "KVM: BNDCFGS in host will be lost");
7953 if (!cpu_has_vmx_vpid() || !cpu_has_vmx_invvpid() ||
7954 !(cpu_has_vmx_invvpid_single() || cpu_has_vmx_invvpid_global()))
7957 if (!cpu_has_vmx_ept() ||
7958 !cpu_has_vmx_ept_4levels() ||
7959 !cpu_has_vmx_ept_mt_wb() ||
7960 !cpu_has_vmx_invept_global())
7963 if (!cpu_has_vmx_ept_ad_bits() || !enable_ept)
7964 enable_ept_ad_bits = 0;
7966 if (!cpu_has_vmx_unrestricted_guest() || !enable_ept)
7967 enable_unrestricted_guest = 0;
7969 if (!cpu_has_vmx_flexpriority())
7970 flexpriority_enabled = 0;
7972 if (!cpu_has_virtual_nmis())
7976 * set_apic_access_page_addr() is used to reload apic access
7977 * page upon invalidation. No need to do anything if not
7978 * using the APIC_ACCESS_ADDR VMCS field.
7980 if (!flexpriority_enabled)
7981 kvm_x86_ops->set_apic_access_page_addr = NULL;
7983 if (!cpu_has_vmx_tpr_shadow())
7984 kvm_x86_ops->update_cr8_intercept = NULL;
7986 if (enable_ept && !cpu_has_vmx_ept_2m_page())
7987 kvm_disable_largepages();
7989 #if IS_ENABLED(CONFIG_HYPERV)
7990 if (ms_hyperv.nested_features & HV_X64_NESTED_GUEST_MAPPING_FLUSH
7992 kvm_x86_ops->tlb_remote_flush = vmx_hv_remote_flush_tlb;
7995 if (!cpu_has_vmx_ple()) {
7998 ple_window_grow = 0;
8000 ple_window_shrink = 0;
8003 if (!cpu_has_vmx_apicv()) {
8005 kvm_x86_ops->sync_pir_to_irr = NULL;
8008 if (cpu_has_vmx_tsc_scaling()) {
8009 kvm_has_tsc_control = true;
8010 kvm_max_tsc_scaling_ratio = KVM_VMX_TSC_MULTIPLIER_MAX;
8011 kvm_tsc_scaling_ratio_frac_bits = 48;
8014 set_bit(0, vmx_vpid_bitmap); /* 0 is reserved for host */
8022 kvm_x86_ops->get_nested_state = NULL;
8023 kvm_x86_ops->set_nested_state = NULL;
8027 * Only enable PML when hardware supports PML feature, and both EPT
8028 * and EPT A/D bit features are enabled -- PML depends on them to work.
8030 if (!enable_ept || !enable_ept_ad_bits || !cpu_has_vmx_pml())
8034 kvm_x86_ops->slot_enable_log_dirty = NULL;
8035 kvm_x86_ops->slot_disable_log_dirty = NULL;
8036 kvm_x86_ops->flush_log_dirty = NULL;
8037 kvm_x86_ops->enable_log_dirty_pt_masked = NULL;
8040 if (!cpu_has_vmx_preemption_timer())
8041 kvm_x86_ops->request_immediate_exit = __kvm_request_immediate_exit;
8043 if (cpu_has_vmx_preemption_timer() && enable_preemption_timer) {
8046 rdmsrl(MSR_IA32_VMX_MISC, vmx_msr);
8047 cpu_preemption_timer_multi =
8048 vmx_msr & VMX_MISC_PREEMPTION_TIMER_RATE_MASK;
8050 kvm_x86_ops->set_hv_timer = NULL;
8051 kvm_x86_ops->cancel_hv_timer = NULL;
8054 if (!cpu_has_vmx_shadow_vmcs())
8055 enable_shadow_vmcs = 0;
8056 if (enable_shadow_vmcs)
8057 init_vmcs_shadow_fields();
8059 kvm_set_posted_intr_wakeup_handler(wakeup_handler);
8060 nested_vmx_setup_ctls_msrs(&vmcs_config.nested, enable_apicv);
8062 kvm_mce_cap_supported |= MCG_LMCE_P;
8064 r = alloc_kvm_area();
8070 for (i = 0; i < VMX_BITMAP_NR; i++)
8071 free_page((unsigned long)vmx_bitmap[i]);
8076 static __exit void hardware_unsetup(void)
8080 for (i = 0; i < VMX_BITMAP_NR; i++)
8081 free_page((unsigned long)vmx_bitmap[i]);
8087 * Indicate a busy-waiting vcpu in spinlock. We do not enable the PAUSE
8088 * exiting, so only get here on cpu with PAUSE-Loop-Exiting.
8090 static int handle_pause(struct kvm_vcpu *vcpu)
8092 if (!kvm_pause_in_guest(vcpu->kvm))
8093 grow_ple_window(vcpu);
8096 * Intel sdm vol3 ch-25.1.3 says: The "PAUSE-loop exiting"
8097 * VM-execution control is ignored if CPL > 0. OTOH, KVM
8098 * never set PAUSE_EXITING and just set PLE if supported,
8099 * so the vcpu must be CPL=0 if it gets a PAUSE exit.
8101 kvm_vcpu_on_spin(vcpu, true);
8102 return kvm_skip_emulated_instruction(vcpu);
8105 static int handle_nop(struct kvm_vcpu *vcpu)
8107 return kvm_skip_emulated_instruction(vcpu);
8110 static int handle_mwait(struct kvm_vcpu *vcpu)
8112 printk_once(KERN_WARNING "kvm: MWAIT instruction emulated as NOP!\n");
8113 return handle_nop(vcpu);
8116 static int handle_invalid_op(struct kvm_vcpu *vcpu)
8118 kvm_queue_exception(vcpu, UD_VECTOR);
8122 static int handle_monitor_trap(struct kvm_vcpu *vcpu)
8127 static int handle_monitor(struct kvm_vcpu *vcpu)
8129 printk_once(KERN_WARNING "kvm: MONITOR instruction emulated as NOP!\n");
8130 return handle_nop(vcpu);
8134 * The following 3 functions, nested_vmx_succeed()/failValid()/failInvalid(),
8135 * set the success or error code of an emulated VMX instruction, as specified
8136 * by Vol 2B, VMX Instruction Reference, "Conventions".
8138 static void nested_vmx_succeed(struct kvm_vcpu *vcpu)
8140 vmx_set_rflags(vcpu, vmx_get_rflags(vcpu)
8141 & ~(X86_EFLAGS_CF | X86_EFLAGS_PF | X86_EFLAGS_AF |
8142 X86_EFLAGS_ZF | X86_EFLAGS_SF | X86_EFLAGS_OF));
8145 static void nested_vmx_failInvalid(struct kvm_vcpu *vcpu)
8147 vmx_set_rflags(vcpu, (vmx_get_rflags(vcpu)
8148 & ~(X86_EFLAGS_PF | X86_EFLAGS_AF | X86_EFLAGS_ZF |
8149 X86_EFLAGS_SF | X86_EFLAGS_OF))
8153 static void nested_vmx_failValid(struct kvm_vcpu *vcpu,
8154 u32 vm_instruction_error)
8156 if (to_vmx(vcpu)->nested.current_vmptr == -1ull) {
8158 * failValid writes the error number to the current VMCS, which
8159 * can't be done there isn't a current VMCS.
8161 nested_vmx_failInvalid(vcpu);
8164 vmx_set_rflags(vcpu, (vmx_get_rflags(vcpu)
8165 & ~(X86_EFLAGS_CF | X86_EFLAGS_PF | X86_EFLAGS_AF |
8166 X86_EFLAGS_SF | X86_EFLAGS_OF))
8168 get_vmcs12(vcpu)->vm_instruction_error = vm_instruction_error;
8170 * We don't need to force a shadow sync because
8171 * VM_INSTRUCTION_ERROR is not shadowed
8175 static void nested_vmx_abort(struct kvm_vcpu *vcpu, u32 indicator)
8177 /* TODO: not to reset guest simply here. */
8178 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
8179 pr_debug_ratelimited("kvm: nested vmx abort, indicator %d\n", indicator);
8182 static enum hrtimer_restart vmx_preemption_timer_fn(struct hrtimer *timer)
8184 struct vcpu_vmx *vmx =
8185 container_of(timer, struct vcpu_vmx, nested.preemption_timer);
8187 vmx->nested.preemption_timer_expired = true;
8188 kvm_make_request(KVM_REQ_EVENT, &vmx->vcpu);
8189 kvm_vcpu_kick(&vmx->vcpu);
8191 return HRTIMER_NORESTART;
8195 * Decode the memory-address operand of a vmx instruction, as recorded on an
8196 * exit caused by such an instruction (run by a guest hypervisor).
8197 * On success, returns 0. When the operand is invalid, returns 1 and throws
8200 static int get_vmx_mem_address(struct kvm_vcpu *vcpu,
8201 unsigned long exit_qualification,
8202 u32 vmx_instruction_info, bool wr, gva_t *ret)
8206 struct kvm_segment s;
8209 * According to Vol. 3B, "Information for VM Exits Due to Instruction
8210 * Execution", on an exit, vmx_instruction_info holds most of the
8211 * addressing components of the operand. Only the displacement part
8212 * is put in exit_qualification (see 3B, "Basic VM-Exit Information").
8213 * For how an actual address is calculated from all these components,
8214 * refer to Vol. 1, "Operand Addressing".
8216 int scaling = vmx_instruction_info & 3;
8217 int addr_size = (vmx_instruction_info >> 7) & 7;
8218 bool is_reg = vmx_instruction_info & (1u << 10);
8219 int seg_reg = (vmx_instruction_info >> 15) & 7;
8220 int index_reg = (vmx_instruction_info >> 18) & 0xf;
8221 bool index_is_valid = !(vmx_instruction_info & (1u << 22));
8222 int base_reg = (vmx_instruction_info >> 23) & 0xf;
8223 bool base_is_valid = !(vmx_instruction_info & (1u << 27));
8226 kvm_queue_exception(vcpu, UD_VECTOR);
8230 /* Addr = segment_base + offset */
8231 /* offset = base + [index * scale] + displacement */
8232 off = exit_qualification; /* holds the displacement */
8234 off = (gva_t)sign_extend64(off, 31);
8235 else if (addr_size == 0)
8236 off = (gva_t)sign_extend64(off, 15);
8238 off += kvm_register_read(vcpu, base_reg);
8240 off += kvm_register_read(vcpu, index_reg)<<scaling;
8241 vmx_get_segment(vcpu, &s, seg_reg);
8244 * The effective address, i.e. @off, of a memory operand is truncated
8245 * based on the address size of the instruction. Note that this is
8246 * the *effective address*, i.e. the address prior to accounting for
8247 * the segment's base.
8249 if (addr_size == 1) /* 32 bit */
8251 else if (addr_size == 0) /* 16 bit */
8254 /* Checks for #GP/#SS exceptions. */
8256 if (is_long_mode(vcpu)) {
8258 * The virtual/linear address is never truncated in 64-bit
8259 * mode, e.g. a 32-bit address size can yield a 64-bit virtual
8260 * address when using FS/GS with a non-zero base.
8262 *ret = s.base + off;
8264 /* Long mode: #GP(0)/#SS(0) if the memory address is in a
8265 * non-canonical form. This is the only check on the memory
8266 * destination for long mode!
8268 exn = is_noncanonical_address(*ret, vcpu);
8269 } else if (is_protmode(vcpu)) {
8271 * When not in long mode, the virtual/linear address is
8272 * unconditionally truncated to 32 bits regardless of the
8275 *ret = (s.base + off) & 0xffffffff;
8277 /* Protected mode: apply checks for segment validity in the
8279 * - segment type check (#GP(0) may be thrown)
8280 * - usability check (#GP(0)/#SS(0))
8281 * - limit check (#GP(0)/#SS(0))
8284 /* #GP(0) if the destination operand is located in a
8285 * read-only data segment or any code segment.
8287 exn = ((s.type & 0xa) == 0 || (s.type & 8));
8289 /* #GP(0) if the source operand is located in an
8290 * execute-only code segment
8292 exn = ((s.type & 0xa) == 8);
8294 kvm_queue_exception_e(vcpu, GP_VECTOR, 0);
8297 /* Protected mode: #GP(0)/#SS(0) if the segment is unusable.
8299 exn = (s.unusable != 0);
8302 * Protected mode: #GP(0)/#SS(0) if the memory operand is
8303 * outside the segment limit. All CPUs that support VMX ignore
8304 * limit checks for flat segments, i.e. segments with base==0,
8305 * limit==0xffffffff and of type expand-up data or code.
8307 if (!(s.base == 0 && s.limit == 0xffffffff &&
8308 ((s.type & 8) || !(s.type & 4))))
8309 exn = exn || (off + sizeof(u64) > s.limit);
8312 kvm_queue_exception_e(vcpu,
8313 seg_reg == VCPU_SREG_SS ?
8314 SS_VECTOR : GP_VECTOR,
8322 static int nested_vmx_get_vmptr(struct kvm_vcpu *vcpu, gpa_t *vmpointer)
8325 struct x86_exception e;
8327 if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
8328 vmcs_read32(VMX_INSTRUCTION_INFO), false, &gva))
8331 if (kvm_read_guest_virt(vcpu, gva, vmpointer, sizeof(*vmpointer), &e)) {
8332 kvm_inject_page_fault(vcpu, &e);
8340 * Allocate a shadow VMCS and associate it with the currently loaded
8341 * VMCS, unless such a shadow VMCS already exists. The newly allocated
8342 * VMCS is also VMCLEARed, so that it is ready for use.
8344 static struct vmcs *alloc_shadow_vmcs(struct kvm_vcpu *vcpu)
8346 struct vcpu_vmx *vmx = to_vmx(vcpu);
8347 struct loaded_vmcs *loaded_vmcs = vmx->loaded_vmcs;
8350 * We should allocate a shadow vmcs for vmcs01 only when L1
8351 * executes VMXON and free it when L1 executes VMXOFF.
8352 * As it is invalid to execute VMXON twice, we shouldn't reach
8353 * here when vmcs01 already have an allocated shadow vmcs.
8355 WARN_ON(loaded_vmcs == &vmx->vmcs01 && loaded_vmcs->shadow_vmcs);
8357 if (!loaded_vmcs->shadow_vmcs) {
8358 loaded_vmcs->shadow_vmcs = alloc_vmcs(true);
8359 if (loaded_vmcs->shadow_vmcs)
8360 vmcs_clear(loaded_vmcs->shadow_vmcs);
8362 return loaded_vmcs->shadow_vmcs;
8365 static int enter_vmx_operation(struct kvm_vcpu *vcpu)
8367 struct vcpu_vmx *vmx = to_vmx(vcpu);
8370 r = alloc_loaded_vmcs(&vmx->nested.vmcs02);
8374 vmx->nested.cached_vmcs12 = kzalloc(VMCS12_SIZE, GFP_KERNEL);
8375 if (!vmx->nested.cached_vmcs12)
8376 goto out_cached_vmcs12;
8378 vmx->nested.cached_shadow_vmcs12 = kzalloc(VMCS12_SIZE, GFP_KERNEL);
8379 if (!vmx->nested.cached_shadow_vmcs12)
8380 goto out_cached_shadow_vmcs12;
8382 if (enable_shadow_vmcs && !alloc_shadow_vmcs(vcpu))
8383 goto out_shadow_vmcs;
8385 hrtimer_init(&vmx->nested.preemption_timer, CLOCK_MONOTONIC,
8386 HRTIMER_MODE_REL_PINNED);
8387 vmx->nested.preemption_timer.function = vmx_preemption_timer_fn;
8389 vmx->nested.vpid02 = allocate_vpid();
8391 vmx->nested.vmxon = true;
8395 kfree(vmx->nested.cached_shadow_vmcs12);
8397 out_cached_shadow_vmcs12:
8398 kfree(vmx->nested.cached_vmcs12);
8401 free_loaded_vmcs(&vmx->nested.vmcs02);
8408 * Emulate the VMXON instruction.
8409 * Currently, we just remember that VMX is active, and do not save or even
8410 * inspect the argument to VMXON (the so-called "VMXON pointer") because we
8411 * do not currently need to store anything in that guest-allocated memory
8412 * region. Consequently, VMCLEAR and VMPTRLD also do not verify that the their
8413 * argument is different from the VMXON pointer (which the spec says they do).
8415 static int handle_vmon(struct kvm_vcpu *vcpu)
8420 struct vcpu_vmx *vmx = to_vmx(vcpu);
8421 const u64 VMXON_NEEDED_FEATURES = FEATURE_CONTROL_LOCKED
8422 | FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
8425 * The Intel VMX Instruction Reference lists a bunch of bits that are
8426 * prerequisite to running VMXON, most notably cr4.VMXE must be set to
8427 * 1 (see vmx_set_cr4() for when we allow the guest to set this).
8428 * Otherwise, we should fail with #UD. But most faulting conditions
8429 * have already been checked by hardware, prior to the VM-exit for
8430 * VMXON. We do test guest cr4.VMXE because processor CR4 always has
8431 * that bit set to 1 in non-root mode.
8433 if (!kvm_read_cr4_bits(vcpu, X86_CR4_VMXE)) {
8434 kvm_queue_exception(vcpu, UD_VECTOR);
8438 /* CPL=0 must be checked manually. */
8439 if (vmx_get_cpl(vcpu)) {
8440 kvm_inject_gp(vcpu, 0);
8444 if (vmx->nested.vmxon) {
8445 nested_vmx_failValid(vcpu, VMXERR_VMXON_IN_VMX_ROOT_OPERATION);
8446 return kvm_skip_emulated_instruction(vcpu);
8449 if ((vmx->msr_ia32_feature_control & VMXON_NEEDED_FEATURES)
8450 != VMXON_NEEDED_FEATURES) {
8451 kvm_inject_gp(vcpu, 0);
8455 if (nested_vmx_get_vmptr(vcpu, &vmptr))
8460 * The first 4 bytes of VMXON region contain the supported
8461 * VMCS revision identifier
8463 * Note - IA32_VMX_BASIC[48] will never be 1 for the nested case;
8464 * which replaces physical address width with 32
8466 if (!PAGE_ALIGNED(vmptr) || (vmptr >> cpuid_maxphyaddr(vcpu))) {
8467 nested_vmx_failInvalid(vcpu);
8468 return kvm_skip_emulated_instruction(vcpu);
8471 page = kvm_vcpu_gpa_to_page(vcpu, vmptr);
8472 if (is_error_page(page)) {
8473 nested_vmx_failInvalid(vcpu);
8474 return kvm_skip_emulated_instruction(vcpu);
8476 if (*(u32 *)kmap(page) != VMCS12_REVISION) {
8478 kvm_release_page_clean(page);
8479 nested_vmx_failInvalid(vcpu);
8480 return kvm_skip_emulated_instruction(vcpu);
8483 kvm_release_page_clean(page);
8485 vmx->nested.vmxon_ptr = vmptr;
8486 ret = enter_vmx_operation(vcpu);
8490 nested_vmx_succeed(vcpu);
8491 return kvm_skip_emulated_instruction(vcpu);
8495 * Intel's VMX Instruction Reference specifies a common set of prerequisites
8496 * for running VMX instructions (except VMXON, whose prerequisites are
8497 * slightly different). It also specifies what exception to inject otherwise.
8498 * Note that many of these exceptions have priority over VM exits, so they
8499 * don't have to be checked again here.
8501 static int nested_vmx_check_permission(struct kvm_vcpu *vcpu)
8503 if (!to_vmx(vcpu)->nested.vmxon) {
8504 kvm_queue_exception(vcpu, UD_VECTOR);
8508 if (vmx_get_cpl(vcpu)) {
8509 kvm_inject_gp(vcpu, 0);
8516 static void vmx_disable_shadow_vmcs(struct vcpu_vmx *vmx)
8518 vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL, SECONDARY_EXEC_SHADOW_VMCS);
8519 vmcs_write64(VMCS_LINK_POINTER, -1ull);
8520 vmx->nested.sync_shadow_vmcs = false;
8523 static inline void nested_release_vmcs12(struct vcpu_vmx *vmx)
8525 if (vmx->nested.current_vmptr == -1ull)
8528 if (enable_shadow_vmcs) {
8529 /* copy to memory all shadowed fields in case
8530 they were modified */
8531 copy_shadow_to_vmcs12(vmx);
8532 vmx_disable_shadow_vmcs(vmx);
8534 vmx->nested.posted_intr_nv = -1;
8536 /* Flush VMCS12 to guest memory */
8537 kvm_vcpu_write_guest_page(&vmx->vcpu,
8538 vmx->nested.current_vmptr >> PAGE_SHIFT,
8539 vmx->nested.cached_vmcs12, 0, VMCS12_SIZE);
8541 vmx->nested.current_vmptr = -1ull;
8545 * Free whatever needs to be freed from vmx->nested when L1 goes down, or
8546 * just stops using VMX.
8548 static void free_nested(struct vcpu_vmx *vmx)
8550 if (!vmx->nested.vmxon && !vmx->nested.smm.vmxon)
8553 kvm_clear_request(KVM_REQ_GET_VMCS12_PAGES, &vmx->vcpu);
8555 hrtimer_cancel(&vmx->nested.preemption_timer);
8556 vmx->nested.vmxon = false;
8557 vmx->nested.smm.vmxon = false;
8558 free_vpid(vmx->nested.vpid02);
8559 vmx->nested.posted_intr_nv = -1;
8560 vmx->nested.current_vmptr = -1ull;
8561 if (enable_shadow_vmcs) {
8562 vmx_disable_shadow_vmcs(vmx);
8563 vmcs_clear(vmx->vmcs01.shadow_vmcs);
8564 free_vmcs(vmx->vmcs01.shadow_vmcs);
8565 vmx->vmcs01.shadow_vmcs = NULL;
8567 kfree(vmx->nested.cached_vmcs12);
8568 kfree(vmx->nested.cached_shadow_vmcs12);
8569 /* Unpin physical memory we referred to in the vmcs02 */
8570 if (vmx->nested.apic_access_page) {
8571 kvm_release_page_dirty(vmx->nested.apic_access_page);
8572 vmx->nested.apic_access_page = NULL;
8574 if (vmx->nested.virtual_apic_page) {
8575 kvm_release_page_dirty(vmx->nested.virtual_apic_page);
8576 vmx->nested.virtual_apic_page = NULL;
8578 if (vmx->nested.pi_desc_page) {
8579 kunmap(vmx->nested.pi_desc_page);
8580 kvm_release_page_dirty(vmx->nested.pi_desc_page);
8581 vmx->nested.pi_desc_page = NULL;
8582 vmx->nested.pi_desc = NULL;
8585 free_loaded_vmcs(&vmx->nested.vmcs02);
8588 /* Emulate the VMXOFF instruction */
8589 static int handle_vmoff(struct kvm_vcpu *vcpu)
8591 if (!nested_vmx_check_permission(vcpu))
8593 free_nested(to_vmx(vcpu));
8594 nested_vmx_succeed(vcpu);
8595 return kvm_skip_emulated_instruction(vcpu);
8598 /* Emulate the VMCLEAR instruction */
8599 static int handle_vmclear(struct kvm_vcpu *vcpu)
8601 struct vcpu_vmx *vmx = to_vmx(vcpu);
8605 if (!nested_vmx_check_permission(vcpu))
8608 if (nested_vmx_get_vmptr(vcpu, &vmptr))
8611 if (!PAGE_ALIGNED(vmptr) || (vmptr >> cpuid_maxphyaddr(vcpu))) {
8612 nested_vmx_failValid(vcpu, VMXERR_VMCLEAR_INVALID_ADDRESS);
8613 return kvm_skip_emulated_instruction(vcpu);
8616 if (vmptr == vmx->nested.vmxon_ptr) {
8617 nested_vmx_failValid(vcpu, VMXERR_VMCLEAR_VMXON_POINTER);
8618 return kvm_skip_emulated_instruction(vcpu);
8621 if (vmptr == vmx->nested.current_vmptr)
8622 nested_release_vmcs12(vmx);
8624 kvm_vcpu_write_guest(vcpu,
8625 vmptr + offsetof(struct vmcs12, launch_state),
8626 &zero, sizeof(zero));
8628 nested_vmx_succeed(vcpu);
8629 return kvm_skip_emulated_instruction(vcpu);
8632 static int nested_vmx_run(struct kvm_vcpu *vcpu, bool launch);
8634 /* Emulate the VMLAUNCH instruction */
8635 static int handle_vmlaunch(struct kvm_vcpu *vcpu)
8637 return nested_vmx_run(vcpu, true);
8640 /* Emulate the VMRESUME instruction */
8641 static int handle_vmresume(struct kvm_vcpu *vcpu)
8644 return nested_vmx_run(vcpu, false);
8648 * Read a vmcs12 field. Since these can have varying lengths and we return
8649 * one type, we chose the biggest type (u64) and zero-extend the return value
8650 * to that size. Note that the caller, handle_vmread, might need to use only
8651 * some of the bits we return here (e.g., on 32-bit guests, only 32 bits of
8652 * 64-bit fields are to be returned).
8654 static inline int vmcs12_read_any(struct vmcs12 *vmcs12,
8655 unsigned long field, u64 *ret)
8657 short offset = vmcs_field_to_offset(field);
8663 p = (char *)vmcs12 + offset;
8665 switch (vmcs_field_width(field)) {
8666 case VMCS_FIELD_WIDTH_NATURAL_WIDTH:
8667 *ret = *((natural_width *)p);
8669 case VMCS_FIELD_WIDTH_U16:
8672 case VMCS_FIELD_WIDTH_U32:
8675 case VMCS_FIELD_WIDTH_U64:
8685 static inline int vmcs12_write_any(struct vmcs12 *vmcs12,
8686 unsigned long field, u64 field_value){
8687 short offset = vmcs_field_to_offset(field);
8688 char *p = (char *)vmcs12 + offset;
8692 switch (vmcs_field_width(field)) {
8693 case VMCS_FIELD_WIDTH_U16:
8694 *(u16 *)p = field_value;
8696 case VMCS_FIELD_WIDTH_U32:
8697 *(u32 *)p = field_value;
8699 case VMCS_FIELD_WIDTH_U64:
8700 *(u64 *)p = field_value;
8702 case VMCS_FIELD_WIDTH_NATURAL_WIDTH:
8703 *(natural_width *)p = field_value;
8713 * Copy the writable VMCS shadow fields back to the VMCS12, in case
8714 * they have been modified by the L1 guest. Note that the "read-only"
8715 * VM-exit information fields are actually writable if the vCPU is
8716 * configured to support "VMWRITE to any supported field in the VMCS."
8718 static void copy_shadow_to_vmcs12(struct vcpu_vmx *vmx)
8720 const u16 *fields[] = {
8721 shadow_read_write_fields,
8722 shadow_read_only_fields
8724 const int max_fields[] = {
8725 max_shadow_read_write_fields,
8726 max_shadow_read_only_fields
8729 unsigned long field;
8731 struct vmcs *shadow_vmcs = vmx->vmcs01.shadow_vmcs;
8733 if (WARN_ON(!shadow_vmcs))
8738 vmcs_load(shadow_vmcs);
8740 for (q = 0; q < ARRAY_SIZE(fields); q++) {
8741 for (i = 0; i < max_fields[q]; i++) {
8742 field = fields[q][i];
8743 field_value = __vmcs_readl(field);
8744 vmcs12_write_any(get_vmcs12(&vmx->vcpu), field, field_value);
8747 * Skip the VM-exit information fields if they are read-only.
8749 if (!nested_cpu_has_vmwrite_any_field(&vmx->vcpu))
8753 vmcs_clear(shadow_vmcs);
8754 vmcs_load(vmx->loaded_vmcs->vmcs);
8759 static void copy_vmcs12_to_shadow(struct vcpu_vmx *vmx)
8761 const u16 *fields[] = {
8762 shadow_read_write_fields,
8763 shadow_read_only_fields
8765 const int max_fields[] = {
8766 max_shadow_read_write_fields,
8767 max_shadow_read_only_fields
8770 unsigned long field;
8771 u64 field_value = 0;
8772 struct vmcs *shadow_vmcs = vmx->vmcs01.shadow_vmcs;
8774 if (WARN_ON(!shadow_vmcs))
8777 vmcs_load(shadow_vmcs);
8779 for (q = 0; q < ARRAY_SIZE(fields); q++) {
8780 for (i = 0; i < max_fields[q]; i++) {
8781 field = fields[q][i];
8782 vmcs12_read_any(get_vmcs12(&vmx->vcpu), field, &field_value);
8783 __vmcs_writel(field, field_value);
8787 vmcs_clear(shadow_vmcs);
8788 vmcs_load(vmx->loaded_vmcs->vmcs);
8792 * VMX instructions which assume a current vmcs12 (i.e., that VMPTRLD was
8793 * used before) all generate the same failure when it is missing.
8795 static int nested_vmx_check_vmcs12(struct kvm_vcpu *vcpu)
8797 struct vcpu_vmx *vmx = to_vmx(vcpu);
8798 if (vmx->nested.current_vmptr == -1ull) {
8799 nested_vmx_failInvalid(vcpu);
8805 static int handle_vmread(struct kvm_vcpu *vcpu)
8807 unsigned long field;
8809 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
8810 u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
8812 struct vmcs12 *vmcs12;
8813 struct x86_exception e;
8815 if (!nested_vmx_check_permission(vcpu))
8818 if (!nested_vmx_check_vmcs12(vcpu))
8819 return kvm_skip_emulated_instruction(vcpu);
8821 if (!is_guest_mode(vcpu))
8822 vmcs12 = get_vmcs12(vcpu);
8825 * When vmcs->vmcs_link_pointer is -1ull, any VMREAD
8826 * to shadowed-field sets the ALU flags for VMfailInvalid.
8828 if (get_vmcs12(vcpu)->vmcs_link_pointer == -1ull) {
8829 nested_vmx_failInvalid(vcpu);
8830 return kvm_skip_emulated_instruction(vcpu);
8832 vmcs12 = get_shadow_vmcs12(vcpu);
8835 /* Decode instruction info and find the field to read */
8836 field = kvm_register_readl(vcpu, (((vmx_instruction_info) >> 28) & 0xf));
8837 /* Read the field, zero-extended to a u64 field_value */
8838 if (vmcs12_read_any(vmcs12, field, &field_value) < 0) {
8839 nested_vmx_failValid(vcpu, VMXERR_UNSUPPORTED_VMCS_COMPONENT);
8840 return kvm_skip_emulated_instruction(vcpu);
8843 * Now copy part of this value to register or memory, as requested.
8844 * Note that the number of bits actually copied is 32 or 64 depending
8845 * on the guest's mode (32 or 64 bit), not on the given field's length.
8847 if (vmx_instruction_info & (1u << 10)) {
8848 kvm_register_writel(vcpu, (((vmx_instruction_info) >> 3) & 0xf),
8851 if (get_vmx_mem_address(vcpu, exit_qualification,
8852 vmx_instruction_info, true, &gva))
8854 /* _system ok, nested_vmx_check_permission has verified cpl=0 */
8855 if (kvm_write_guest_virt_system(vcpu, gva, &field_value,
8856 (is_long_mode(vcpu) ? 8 : 4),
8858 kvm_inject_page_fault(vcpu, &e);
8863 nested_vmx_succeed(vcpu);
8864 return kvm_skip_emulated_instruction(vcpu);
8868 static int handle_vmwrite(struct kvm_vcpu *vcpu)
8870 unsigned long field;
8872 struct vcpu_vmx *vmx = to_vmx(vcpu);
8873 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
8874 u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
8876 /* The value to write might be 32 or 64 bits, depending on L1's long
8877 * mode, and eventually we need to write that into a field of several
8878 * possible lengths. The code below first zero-extends the value to 64
8879 * bit (field_value), and then copies only the appropriate number of
8880 * bits into the vmcs12 field.
8882 u64 field_value = 0;
8883 struct x86_exception e;
8884 struct vmcs12 *vmcs12;
8886 if (!nested_vmx_check_permission(vcpu))
8889 if (!nested_vmx_check_vmcs12(vcpu))
8890 return kvm_skip_emulated_instruction(vcpu);
8892 if (vmx_instruction_info & (1u << 10))
8893 field_value = kvm_register_readl(vcpu,
8894 (((vmx_instruction_info) >> 3) & 0xf));
8896 if (get_vmx_mem_address(vcpu, exit_qualification,
8897 vmx_instruction_info, false, &gva))
8899 if (kvm_read_guest_virt(vcpu, gva, &field_value,
8900 (is_64_bit_mode(vcpu) ? 8 : 4), &e)) {
8901 kvm_inject_page_fault(vcpu, &e);
8907 field = kvm_register_readl(vcpu, (((vmx_instruction_info) >> 28) & 0xf));
8909 * If the vCPU supports "VMWRITE to any supported field in the
8910 * VMCS," then the "read-only" fields are actually read/write.
8912 if (vmcs_field_readonly(field) &&
8913 !nested_cpu_has_vmwrite_any_field(vcpu)) {
8914 nested_vmx_failValid(vcpu,
8915 VMXERR_VMWRITE_READ_ONLY_VMCS_COMPONENT);
8916 return kvm_skip_emulated_instruction(vcpu);
8919 if (!is_guest_mode(vcpu))
8920 vmcs12 = get_vmcs12(vcpu);
8923 * When vmcs->vmcs_link_pointer is -1ull, any VMWRITE
8924 * to shadowed-field sets the ALU flags for VMfailInvalid.
8926 if (get_vmcs12(vcpu)->vmcs_link_pointer == -1ull) {
8927 nested_vmx_failInvalid(vcpu);
8928 return kvm_skip_emulated_instruction(vcpu);
8930 vmcs12 = get_shadow_vmcs12(vcpu);
8934 if (vmcs12_write_any(vmcs12, field, field_value) < 0) {
8935 nested_vmx_failValid(vcpu, VMXERR_UNSUPPORTED_VMCS_COMPONENT);
8936 return kvm_skip_emulated_instruction(vcpu);
8940 * Do not track vmcs12 dirty-state if in guest-mode
8941 * as we actually dirty shadow vmcs12 instead of vmcs12.
8943 if (!is_guest_mode(vcpu)) {
8945 #define SHADOW_FIELD_RW(x) case x:
8946 #include "vmx_shadow_fields.h"
8948 * The fields that can be updated by L1 without a vmexit are
8949 * always updated in the vmcs02, the others go down the slow
8950 * path of prepare_vmcs02.
8954 vmx->nested.dirty_vmcs12 = true;
8959 nested_vmx_succeed(vcpu);
8960 return kvm_skip_emulated_instruction(vcpu);
8963 static void set_current_vmptr(struct vcpu_vmx *vmx, gpa_t vmptr)
8965 vmx->nested.current_vmptr = vmptr;
8966 if (enable_shadow_vmcs) {
8967 vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL,
8968 SECONDARY_EXEC_SHADOW_VMCS);
8969 vmcs_write64(VMCS_LINK_POINTER,
8970 __pa(vmx->vmcs01.shadow_vmcs));
8971 vmx->nested.sync_shadow_vmcs = true;
8973 vmx->nested.dirty_vmcs12 = true;
8976 /* Emulate the VMPTRLD instruction */
8977 static int handle_vmptrld(struct kvm_vcpu *vcpu)
8979 struct vcpu_vmx *vmx = to_vmx(vcpu);
8982 if (!nested_vmx_check_permission(vcpu))
8985 if (nested_vmx_get_vmptr(vcpu, &vmptr))
8988 if (!PAGE_ALIGNED(vmptr) || (vmptr >> cpuid_maxphyaddr(vcpu))) {
8989 nested_vmx_failValid(vcpu, VMXERR_VMPTRLD_INVALID_ADDRESS);
8990 return kvm_skip_emulated_instruction(vcpu);
8993 if (vmptr == vmx->nested.vmxon_ptr) {
8994 nested_vmx_failValid(vcpu, VMXERR_VMPTRLD_VMXON_POINTER);
8995 return kvm_skip_emulated_instruction(vcpu);
8998 if (vmx->nested.current_vmptr != vmptr) {
8999 struct vmcs12 *new_vmcs12;
9001 page = kvm_vcpu_gpa_to_page(vcpu, vmptr);
9002 if (is_error_page(page)) {
9003 nested_vmx_failInvalid(vcpu);
9004 return kvm_skip_emulated_instruction(vcpu);
9006 new_vmcs12 = kmap(page);
9007 if (new_vmcs12->hdr.revision_id != VMCS12_REVISION ||
9008 (new_vmcs12->hdr.shadow_vmcs &&
9009 !nested_cpu_has_vmx_shadow_vmcs(vcpu))) {
9011 kvm_release_page_clean(page);
9012 nested_vmx_failValid(vcpu,
9013 VMXERR_VMPTRLD_INCORRECT_VMCS_REVISION_ID);
9014 return kvm_skip_emulated_instruction(vcpu);
9017 nested_release_vmcs12(vmx);
9019 * Load VMCS12 from guest memory since it is not already
9022 memcpy(vmx->nested.cached_vmcs12, new_vmcs12, VMCS12_SIZE);
9024 kvm_release_page_clean(page);
9026 set_current_vmptr(vmx, vmptr);
9029 nested_vmx_succeed(vcpu);
9030 return kvm_skip_emulated_instruction(vcpu);
9033 /* Emulate the VMPTRST instruction */
9034 static int handle_vmptrst(struct kvm_vcpu *vcpu)
9036 unsigned long exit_qual = vmcs_readl(EXIT_QUALIFICATION);
9037 u32 instr_info = vmcs_read32(VMX_INSTRUCTION_INFO);
9038 gpa_t current_vmptr = to_vmx(vcpu)->nested.current_vmptr;
9039 struct x86_exception e;
9042 if (!nested_vmx_check_permission(vcpu))
9045 if (get_vmx_mem_address(vcpu, exit_qual, instr_info, true, &gva))
9047 /* *_system ok, nested_vmx_check_permission has verified cpl=0 */
9048 if (kvm_write_guest_virt_system(vcpu, gva, (void *)¤t_vmptr,
9049 sizeof(gpa_t), &e)) {
9050 kvm_inject_page_fault(vcpu, &e);
9053 nested_vmx_succeed(vcpu);
9054 return kvm_skip_emulated_instruction(vcpu);
9057 /* Emulate the INVEPT instruction */
9058 static int handle_invept(struct kvm_vcpu *vcpu)
9060 struct vcpu_vmx *vmx = to_vmx(vcpu);
9061 u32 vmx_instruction_info, types;
9064 struct x86_exception e;
9069 if (!(vmx->nested.msrs.secondary_ctls_high &
9070 SECONDARY_EXEC_ENABLE_EPT) ||
9071 !(vmx->nested.msrs.ept_caps & VMX_EPT_INVEPT_BIT)) {
9072 kvm_queue_exception(vcpu, UD_VECTOR);
9076 if (!nested_vmx_check_permission(vcpu))
9079 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
9080 type = kvm_register_readl(vcpu, (vmx_instruction_info >> 28) & 0xf);
9082 types = (vmx->nested.msrs.ept_caps >> VMX_EPT_EXTENT_SHIFT) & 6;
9084 if (type >= 32 || !(types & (1 << type))) {
9085 nested_vmx_failValid(vcpu,
9086 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
9087 return kvm_skip_emulated_instruction(vcpu);
9090 /* According to the Intel VMX instruction reference, the memory
9091 * operand is read even if it isn't needed (e.g., for type==global)
9093 if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
9094 vmx_instruction_info, false, &gva))
9096 if (kvm_read_guest_virt(vcpu, gva, &operand, sizeof(operand), &e)) {
9097 kvm_inject_page_fault(vcpu, &e);
9102 case VMX_EPT_EXTENT_GLOBAL:
9104 * TODO: track mappings and invalidate
9105 * single context requests appropriately
9107 case VMX_EPT_EXTENT_CONTEXT:
9108 kvm_mmu_sync_roots(vcpu);
9109 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
9110 nested_vmx_succeed(vcpu);
9117 return kvm_skip_emulated_instruction(vcpu);
9120 static int handle_invvpid(struct kvm_vcpu *vcpu)
9122 struct vcpu_vmx *vmx = to_vmx(vcpu);
9123 u32 vmx_instruction_info;
9124 unsigned long type, types;
9126 struct x86_exception e;
9132 if (!(vmx->nested.msrs.secondary_ctls_high &
9133 SECONDARY_EXEC_ENABLE_VPID) ||
9134 !(vmx->nested.msrs.vpid_caps & VMX_VPID_INVVPID_BIT)) {
9135 kvm_queue_exception(vcpu, UD_VECTOR);
9139 if (!nested_vmx_check_permission(vcpu))
9142 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
9143 type = kvm_register_readl(vcpu, (vmx_instruction_info >> 28) & 0xf);
9145 types = (vmx->nested.msrs.vpid_caps &
9146 VMX_VPID_EXTENT_SUPPORTED_MASK) >> 8;
9148 if (type >= 32 || !(types & (1 << type))) {
9149 nested_vmx_failValid(vcpu,
9150 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
9151 return kvm_skip_emulated_instruction(vcpu);
9154 /* according to the intel vmx instruction reference, the memory
9155 * operand is read even if it isn't needed (e.g., for type==global)
9157 if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
9158 vmx_instruction_info, false, &gva))
9160 if (kvm_read_guest_virt(vcpu, gva, &operand, sizeof(operand), &e)) {
9161 kvm_inject_page_fault(vcpu, &e);
9164 if (operand.vpid >> 16) {
9165 nested_vmx_failValid(vcpu,
9166 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
9167 return kvm_skip_emulated_instruction(vcpu);
9171 case VMX_VPID_EXTENT_INDIVIDUAL_ADDR:
9172 if (!operand.vpid ||
9173 is_noncanonical_address(operand.gla, vcpu)) {
9174 nested_vmx_failValid(vcpu,
9175 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
9176 return kvm_skip_emulated_instruction(vcpu);
9178 if (cpu_has_vmx_invvpid_individual_addr() &&
9179 vmx->nested.vpid02) {
9180 __invvpid(VMX_VPID_EXTENT_INDIVIDUAL_ADDR,
9181 vmx->nested.vpid02, operand.gla);
9183 __vmx_flush_tlb(vcpu, vmx->nested.vpid02, true);
9185 case VMX_VPID_EXTENT_SINGLE_CONTEXT:
9186 case VMX_VPID_EXTENT_SINGLE_NON_GLOBAL:
9187 if (!operand.vpid) {
9188 nested_vmx_failValid(vcpu,
9189 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
9190 return kvm_skip_emulated_instruction(vcpu);
9192 __vmx_flush_tlb(vcpu, vmx->nested.vpid02, true);
9194 case VMX_VPID_EXTENT_ALL_CONTEXT:
9195 __vmx_flush_tlb(vcpu, vmx->nested.vpid02, true);
9199 return kvm_skip_emulated_instruction(vcpu);
9202 nested_vmx_succeed(vcpu);
9204 return kvm_skip_emulated_instruction(vcpu);
9207 static int handle_invpcid(struct kvm_vcpu *vcpu)
9209 u32 vmx_instruction_info;
9213 struct x86_exception e;
9215 unsigned long roots_to_free = 0;
9221 if (!guest_cpuid_has(vcpu, X86_FEATURE_INVPCID)) {
9222 kvm_queue_exception(vcpu, UD_VECTOR);
9226 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
9227 type = kvm_register_readl(vcpu, (vmx_instruction_info >> 28) & 0xf);
9230 kvm_inject_gp(vcpu, 0);
9234 /* According to the Intel instruction reference, the memory operand
9235 * is read even if it isn't needed (e.g., for type==all)
9237 if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
9238 vmx_instruction_info, false, &gva))
9241 if (kvm_read_guest_virt(vcpu, gva, &operand, sizeof(operand), &e)) {
9242 kvm_inject_page_fault(vcpu, &e);
9246 if (operand.pcid >> 12 != 0) {
9247 kvm_inject_gp(vcpu, 0);
9251 pcid_enabled = kvm_read_cr4_bits(vcpu, X86_CR4_PCIDE);
9254 case INVPCID_TYPE_INDIV_ADDR:
9255 if ((!pcid_enabled && (operand.pcid != 0)) ||
9256 is_noncanonical_address(operand.gla, vcpu)) {
9257 kvm_inject_gp(vcpu, 0);
9260 kvm_mmu_invpcid_gva(vcpu, operand.gla, operand.pcid);
9261 return kvm_skip_emulated_instruction(vcpu);
9263 case INVPCID_TYPE_SINGLE_CTXT:
9264 if (!pcid_enabled && (operand.pcid != 0)) {
9265 kvm_inject_gp(vcpu, 0);
9269 if (kvm_get_active_pcid(vcpu) == operand.pcid) {
9270 kvm_mmu_sync_roots(vcpu);
9271 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
9274 for (i = 0; i < KVM_MMU_NUM_PREV_ROOTS; i++)
9275 if (kvm_get_pcid(vcpu, vcpu->arch.mmu.prev_roots[i].cr3)
9277 roots_to_free |= KVM_MMU_ROOT_PREVIOUS(i);
9279 kvm_mmu_free_roots(vcpu, roots_to_free);
9281 * If neither the current cr3 nor any of the prev_roots use the
9282 * given PCID, then nothing needs to be done here because a
9283 * resync will happen anyway before switching to any other CR3.
9286 return kvm_skip_emulated_instruction(vcpu);
9288 case INVPCID_TYPE_ALL_NON_GLOBAL:
9290 * Currently, KVM doesn't mark global entries in the shadow
9291 * page tables, so a non-global flush just degenerates to a
9292 * global flush. If needed, we could optimize this later by
9293 * keeping track of global entries in shadow page tables.
9297 case INVPCID_TYPE_ALL_INCL_GLOBAL:
9298 kvm_mmu_unload(vcpu);
9299 return kvm_skip_emulated_instruction(vcpu);
9302 BUG(); /* We have already checked above that type <= 3 */
9306 static int handle_pml_full(struct kvm_vcpu *vcpu)
9308 unsigned long exit_qualification;
9310 trace_kvm_pml_full(vcpu->vcpu_id);
9312 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
9315 * PML buffer FULL happened while executing iret from NMI,
9316 * "blocked by NMI" bit has to be set before next VM entry.
9318 if (!(to_vmx(vcpu)->idt_vectoring_info & VECTORING_INFO_VALID_MASK) &&
9320 (exit_qualification & INTR_INFO_UNBLOCK_NMI))
9321 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
9322 GUEST_INTR_STATE_NMI);
9325 * PML buffer already flushed at beginning of VMEXIT. Nothing to do
9326 * here.., and there's no userspace involvement needed for PML.
9331 static int handle_preemption_timer(struct kvm_vcpu *vcpu)
9333 if (!to_vmx(vcpu)->req_immediate_exit)
9334 kvm_lapic_expired_hv_timer(vcpu);
9338 static bool valid_ept_address(struct kvm_vcpu *vcpu, u64 address)
9340 struct vcpu_vmx *vmx = to_vmx(vcpu);
9341 int maxphyaddr = cpuid_maxphyaddr(vcpu);
9343 /* Check for memory type validity */
9344 switch (address & VMX_EPTP_MT_MASK) {
9345 case VMX_EPTP_MT_UC:
9346 if (!(vmx->nested.msrs.ept_caps & VMX_EPTP_UC_BIT))
9349 case VMX_EPTP_MT_WB:
9350 if (!(vmx->nested.msrs.ept_caps & VMX_EPTP_WB_BIT))
9357 /* only 4 levels page-walk length are valid */
9358 if ((address & VMX_EPTP_PWL_MASK) != VMX_EPTP_PWL_4)
9361 /* Reserved bits should not be set */
9362 if (address >> maxphyaddr || ((address >> 7) & 0x1f))
9365 /* AD, if set, should be supported */
9366 if (address & VMX_EPTP_AD_ENABLE_BIT) {
9367 if (!(vmx->nested.msrs.ept_caps & VMX_EPT_AD_BIT))
9374 static int nested_vmx_eptp_switching(struct kvm_vcpu *vcpu,
9375 struct vmcs12 *vmcs12)
9377 u32 index = vcpu->arch.regs[VCPU_REGS_RCX];
9379 bool accessed_dirty;
9380 struct kvm_mmu *mmu = vcpu->arch.walk_mmu;
9382 if (!nested_cpu_has_eptp_switching(vmcs12) ||
9383 !nested_cpu_has_ept(vmcs12))
9386 if (index >= VMFUNC_EPTP_ENTRIES)
9390 if (kvm_vcpu_read_guest_page(vcpu, vmcs12->eptp_list_address >> PAGE_SHIFT,
9391 &address, index * 8, 8))
9394 accessed_dirty = !!(address & VMX_EPTP_AD_ENABLE_BIT);
9397 * If the (L2) guest does a vmfunc to the currently
9398 * active ept pointer, we don't have to do anything else
9400 if (vmcs12->ept_pointer != address) {
9401 if (!valid_ept_address(vcpu, address))
9404 kvm_mmu_unload(vcpu);
9405 mmu->ept_ad = accessed_dirty;
9406 mmu->base_role.ad_disabled = !accessed_dirty;
9407 vmcs12->ept_pointer = address;
9409 * TODO: Check what's the correct approach in case
9410 * mmu reload fails. Currently, we just let the next
9411 * reload potentially fail
9413 kvm_mmu_reload(vcpu);
9419 static int handle_vmfunc(struct kvm_vcpu *vcpu)
9421 struct vcpu_vmx *vmx = to_vmx(vcpu);
9422 struct vmcs12 *vmcs12;
9423 u32 function = vcpu->arch.regs[VCPU_REGS_RAX];
9426 * VMFUNC is only supported for nested guests, but we always enable the
9427 * secondary control for simplicity; for non-nested mode, fake that we
9428 * didn't by injecting #UD.
9430 if (!is_guest_mode(vcpu)) {
9431 kvm_queue_exception(vcpu, UD_VECTOR);
9435 vmcs12 = get_vmcs12(vcpu);
9436 if ((vmcs12->vm_function_control & (1 << function)) == 0)
9441 if (nested_vmx_eptp_switching(vcpu, vmcs12))
9447 return kvm_skip_emulated_instruction(vcpu);
9450 nested_vmx_vmexit(vcpu, vmx->exit_reason,
9451 vmcs_read32(VM_EXIT_INTR_INFO),
9452 vmcs_readl(EXIT_QUALIFICATION));
9456 static int handle_encls(struct kvm_vcpu *vcpu)
9459 * SGX virtualization is not yet supported. There is no software
9460 * enable bit for SGX, so we have to trap ENCLS and inject a #UD
9461 * to prevent the guest from executing ENCLS.
9463 kvm_queue_exception(vcpu, UD_VECTOR);
9468 * The exit handlers return 1 if the exit was handled fully and guest execution
9469 * may resume. Otherwise they set the kvm_run parameter to indicate what needs
9470 * to be done to userspace and return 0.
9472 static int (*const kvm_vmx_exit_handlers[])(struct kvm_vcpu *vcpu) = {
9473 [EXIT_REASON_EXCEPTION_NMI] = handle_exception,
9474 [EXIT_REASON_EXTERNAL_INTERRUPT] = handle_external_interrupt,
9475 [EXIT_REASON_TRIPLE_FAULT] = handle_triple_fault,
9476 [EXIT_REASON_NMI_WINDOW] = handle_nmi_window,
9477 [EXIT_REASON_IO_INSTRUCTION] = handle_io,
9478 [EXIT_REASON_CR_ACCESS] = handle_cr,
9479 [EXIT_REASON_DR_ACCESS] = handle_dr,
9480 [EXIT_REASON_CPUID] = handle_cpuid,
9481 [EXIT_REASON_MSR_READ] = handle_rdmsr,
9482 [EXIT_REASON_MSR_WRITE] = handle_wrmsr,
9483 [EXIT_REASON_PENDING_INTERRUPT] = handle_interrupt_window,
9484 [EXIT_REASON_HLT] = handle_halt,
9485 [EXIT_REASON_INVD] = handle_invd,
9486 [EXIT_REASON_INVLPG] = handle_invlpg,
9487 [EXIT_REASON_RDPMC] = handle_rdpmc,
9488 [EXIT_REASON_VMCALL] = handle_vmcall,
9489 [EXIT_REASON_VMCLEAR] = handle_vmclear,
9490 [EXIT_REASON_VMLAUNCH] = handle_vmlaunch,
9491 [EXIT_REASON_VMPTRLD] = handle_vmptrld,
9492 [EXIT_REASON_VMPTRST] = handle_vmptrst,
9493 [EXIT_REASON_VMREAD] = handle_vmread,
9494 [EXIT_REASON_VMRESUME] = handle_vmresume,
9495 [EXIT_REASON_VMWRITE] = handle_vmwrite,
9496 [EXIT_REASON_VMOFF] = handle_vmoff,
9497 [EXIT_REASON_VMON] = handle_vmon,
9498 [EXIT_REASON_TPR_BELOW_THRESHOLD] = handle_tpr_below_threshold,
9499 [EXIT_REASON_APIC_ACCESS] = handle_apic_access,
9500 [EXIT_REASON_APIC_WRITE] = handle_apic_write,
9501 [EXIT_REASON_EOI_INDUCED] = handle_apic_eoi_induced,
9502 [EXIT_REASON_WBINVD] = handle_wbinvd,
9503 [EXIT_REASON_XSETBV] = handle_xsetbv,
9504 [EXIT_REASON_TASK_SWITCH] = handle_task_switch,
9505 [EXIT_REASON_MCE_DURING_VMENTRY] = handle_machine_check,
9506 [EXIT_REASON_GDTR_IDTR] = handle_desc,
9507 [EXIT_REASON_LDTR_TR] = handle_desc,
9508 [EXIT_REASON_EPT_VIOLATION] = handle_ept_violation,
9509 [EXIT_REASON_EPT_MISCONFIG] = handle_ept_misconfig,
9510 [EXIT_REASON_PAUSE_INSTRUCTION] = handle_pause,
9511 [EXIT_REASON_MWAIT_INSTRUCTION] = handle_mwait,
9512 [EXIT_REASON_MONITOR_TRAP_FLAG] = handle_monitor_trap,
9513 [EXIT_REASON_MONITOR_INSTRUCTION] = handle_monitor,
9514 [EXIT_REASON_INVEPT] = handle_invept,
9515 [EXIT_REASON_INVVPID] = handle_invvpid,
9516 [EXIT_REASON_RDRAND] = handle_invalid_op,
9517 [EXIT_REASON_RDSEED] = handle_invalid_op,
9518 [EXIT_REASON_XSAVES] = handle_xsaves,
9519 [EXIT_REASON_XRSTORS] = handle_xrstors,
9520 [EXIT_REASON_PML_FULL] = handle_pml_full,
9521 [EXIT_REASON_INVPCID] = handle_invpcid,
9522 [EXIT_REASON_VMFUNC] = handle_vmfunc,
9523 [EXIT_REASON_PREEMPTION_TIMER] = handle_preemption_timer,
9524 [EXIT_REASON_ENCLS] = handle_encls,
9527 static const int kvm_vmx_max_exit_handlers =
9528 ARRAY_SIZE(kvm_vmx_exit_handlers);
9531 * Return true if an IO instruction with the specified port and size should cause
9532 * a VM-exit into L1.
9534 bool nested_vmx_check_io_bitmaps(struct kvm_vcpu *vcpu, unsigned int port,
9537 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
9538 gpa_t bitmap, last_bitmap;
9541 last_bitmap = (gpa_t)-1;
9546 bitmap = vmcs12->io_bitmap_a;
9547 else if (port < 0x10000)
9548 bitmap = vmcs12->io_bitmap_b;
9551 bitmap += (port & 0x7fff) / 8;
9553 if (last_bitmap != bitmap)
9554 if (kvm_vcpu_read_guest(vcpu, bitmap, &b, 1))
9556 if (b & (1 << (port & 7)))
9561 last_bitmap = bitmap;
9568 * Return 1 if we should exit from L2 to L1 to handle an MSR access access,
9569 * rather than handle it ourselves in L0. I.e., check whether L1 expressed
9570 * disinterest in the current event (read or write a specific MSR) by using an
9571 * MSR bitmap. This may be the case even when L0 doesn't use MSR bitmaps.
9573 static bool nested_vmx_exit_handled_msr(struct kvm_vcpu *vcpu,
9574 struct vmcs12 *vmcs12, u32 exit_reason)
9576 u32 msr_index = vcpu->arch.regs[VCPU_REGS_RCX];
9579 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS))
9583 * The MSR_BITMAP page is divided into four 1024-byte bitmaps,
9584 * for the four combinations of read/write and low/high MSR numbers.
9585 * First we need to figure out which of the four to use:
9587 bitmap = vmcs12->msr_bitmap;
9588 if (exit_reason == EXIT_REASON_MSR_WRITE)
9590 if (msr_index >= 0xc0000000) {
9591 msr_index -= 0xc0000000;
9595 /* Then read the msr_index'th bit from this bitmap: */
9596 if (msr_index < 1024*8) {
9598 if (kvm_vcpu_read_guest(vcpu, bitmap + msr_index/8, &b, 1))
9600 return 1 & (b >> (msr_index & 7));
9602 return true; /* let L1 handle the wrong parameter */
9606 * Return 1 if we should exit from L2 to L1 to handle a CR access exit,
9607 * rather than handle it ourselves in L0. I.e., check if L1 wanted to
9608 * intercept (via guest_host_mask etc.) the current event.
9610 static bool nested_vmx_exit_handled_cr(struct kvm_vcpu *vcpu,
9611 struct vmcs12 *vmcs12)
9613 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
9614 int cr = exit_qualification & 15;
9618 switch ((exit_qualification >> 4) & 3) {
9619 case 0: /* mov to cr */
9620 reg = (exit_qualification >> 8) & 15;
9621 val = kvm_register_readl(vcpu, reg);
9624 if (vmcs12->cr0_guest_host_mask &
9625 (val ^ vmcs12->cr0_read_shadow))
9629 if ((vmcs12->cr3_target_count >= 1 &&
9630 vmcs12->cr3_target_value0 == val) ||
9631 (vmcs12->cr3_target_count >= 2 &&
9632 vmcs12->cr3_target_value1 == val) ||
9633 (vmcs12->cr3_target_count >= 3 &&
9634 vmcs12->cr3_target_value2 == val) ||
9635 (vmcs12->cr3_target_count >= 4 &&
9636 vmcs12->cr3_target_value3 == val))
9638 if (nested_cpu_has(vmcs12, CPU_BASED_CR3_LOAD_EXITING))
9642 if (vmcs12->cr4_guest_host_mask &
9643 (vmcs12->cr4_read_shadow ^ val))
9647 if (nested_cpu_has(vmcs12, CPU_BASED_CR8_LOAD_EXITING))
9653 if ((vmcs12->cr0_guest_host_mask & X86_CR0_TS) &&
9654 (vmcs12->cr0_read_shadow & X86_CR0_TS))
9657 case 1: /* mov from cr */
9660 if (vmcs12->cpu_based_vm_exec_control &
9661 CPU_BASED_CR3_STORE_EXITING)
9665 if (vmcs12->cpu_based_vm_exec_control &
9666 CPU_BASED_CR8_STORE_EXITING)
9673 * lmsw can change bits 1..3 of cr0, and only set bit 0 of
9674 * cr0. Other attempted changes are ignored, with no exit.
9676 val = (exit_qualification >> LMSW_SOURCE_DATA_SHIFT) & 0x0f;
9677 if (vmcs12->cr0_guest_host_mask & 0xe &
9678 (val ^ vmcs12->cr0_read_shadow))
9680 if ((vmcs12->cr0_guest_host_mask & 0x1) &&
9681 !(vmcs12->cr0_read_shadow & 0x1) &&
9689 static bool nested_vmx_exit_handled_vmcs_access(struct kvm_vcpu *vcpu,
9690 struct vmcs12 *vmcs12, gpa_t bitmap)
9692 u32 vmx_instruction_info;
9693 unsigned long field;
9696 if (!nested_cpu_has_shadow_vmcs(vmcs12))
9699 /* Decode instruction info and find the field to access */
9700 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
9701 field = kvm_register_read(vcpu, (((vmx_instruction_info) >> 28) & 0xf));
9703 /* Out-of-range fields always cause a VM exit from L2 to L1 */
9707 if (kvm_vcpu_read_guest(vcpu, bitmap + field/8, &b, 1))
9710 return 1 & (b >> (field & 7));
9714 * Return 1 if we should exit from L2 to L1 to handle an exit, or 0 if we
9715 * should handle it ourselves in L0 (and then continue L2). Only call this
9716 * when in is_guest_mode (L2).
9718 static bool nested_vmx_exit_reflected(struct kvm_vcpu *vcpu, u32 exit_reason)
9720 u32 intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
9721 struct vcpu_vmx *vmx = to_vmx(vcpu);
9722 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
9724 if (vmx->nested.nested_run_pending)
9727 if (unlikely(vmx->fail)) {
9728 pr_info_ratelimited("%s failed vm entry %x\n", __func__,
9729 vmcs_read32(VM_INSTRUCTION_ERROR));
9734 * The host physical addresses of some pages of guest memory
9735 * are loaded into the vmcs02 (e.g. vmcs12's Virtual APIC
9736 * Page). The CPU may write to these pages via their host
9737 * physical address while L2 is running, bypassing any
9738 * address-translation-based dirty tracking (e.g. EPT write
9741 * Mark them dirty on every exit from L2 to prevent them from
9742 * getting out of sync with dirty tracking.
9744 nested_mark_vmcs12_pages_dirty(vcpu);
9746 trace_kvm_nested_vmexit(kvm_rip_read(vcpu), exit_reason,
9747 vmcs_readl(EXIT_QUALIFICATION),
9748 vmx->idt_vectoring_info,
9750 vmcs_read32(VM_EXIT_INTR_ERROR_CODE),
9753 switch ((u16)exit_reason) {
9754 case EXIT_REASON_EXCEPTION_NMI:
9755 if (is_nmi(intr_info))
9757 else if (is_page_fault(intr_info))
9758 return !vmx->vcpu.arch.apf.host_apf_reason && enable_ept;
9759 else if (is_no_device(intr_info) &&
9760 !(vmcs12->guest_cr0 & X86_CR0_TS))
9762 else if (is_debug(intr_info) &&
9764 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))
9766 else if (is_breakpoint(intr_info) &&
9767 vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
9769 return vmcs12->exception_bitmap &
9770 (1u << (intr_info & INTR_INFO_VECTOR_MASK));
9771 case EXIT_REASON_EXTERNAL_INTERRUPT:
9773 case EXIT_REASON_TRIPLE_FAULT:
9775 case EXIT_REASON_PENDING_INTERRUPT:
9776 return nested_cpu_has(vmcs12, CPU_BASED_VIRTUAL_INTR_PENDING);
9777 case EXIT_REASON_NMI_WINDOW:
9778 return nested_cpu_has(vmcs12, CPU_BASED_VIRTUAL_NMI_PENDING);
9779 case EXIT_REASON_TASK_SWITCH:
9781 case EXIT_REASON_CPUID:
9783 case EXIT_REASON_HLT:
9784 return nested_cpu_has(vmcs12, CPU_BASED_HLT_EXITING);
9785 case EXIT_REASON_INVD:
9787 case EXIT_REASON_INVLPG:
9788 return nested_cpu_has(vmcs12, CPU_BASED_INVLPG_EXITING);
9789 case EXIT_REASON_RDPMC:
9790 return nested_cpu_has(vmcs12, CPU_BASED_RDPMC_EXITING);
9791 case EXIT_REASON_RDRAND:
9792 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_RDRAND_EXITING);
9793 case EXIT_REASON_RDSEED:
9794 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_RDSEED_EXITING);
9795 case EXIT_REASON_RDTSC: case EXIT_REASON_RDTSCP:
9796 return nested_cpu_has(vmcs12, CPU_BASED_RDTSC_EXITING);
9797 case EXIT_REASON_VMREAD:
9798 return nested_vmx_exit_handled_vmcs_access(vcpu, vmcs12,
9799 vmcs12->vmread_bitmap);
9800 case EXIT_REASON_VMWRITE:
9801 return nested_vmx_exit_handled_vmcs_access(vcpu, vmcs12,
9802 vmcs12->vmwrite_bitmap);
9803 case EXIT_REASON_VMCALL: case EXIT_REASON_VMCLEAR:
9804 case EXIT_REASON_VMLAUNCH: case EXIT_REASON_VMPTRLD:
9805 case EXIT_REASON_VMPTRST: case EXIT_REASON_VMRESUME:
9806 case EXIT_REASON_VMOFF: case EXIT_REASON_VMON:
9807 case EXIT_REASON_INVEPT: case EXIT_REASON_INVVPID:
9809 * VMX instructions trap unconditionally. This allows L1 to
9810 * emulate them for its L2 guest, i.e., allows 3-level nesting!
9813 case EXIT_REASON_CR_ACCESS:
9814 return nested_vmx_exit_handled_cr(vcpu, vmcs12);
9815 case EXIT_REASON_DR_ACCESS:
9816 return nested_cpu_has(vmcs12, CPU_BASED_MOV_DR_EXITING);
9817 case EXIT_REASON_IO_INSTRUCTION:
9818 return nested_vmx_exit_handled_io(vcpu, vmcs12);
9819 case EXIT_REASON_GDTR_IDTR: case EXIT_REASON_LDTR_TR:
9820 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_DESC);
9821 case EXIT_REASON_MSR_READ:
9822 case EXIT_REASON_MSR_WRITE:
9823 return nested_vmx_exit_handled_msr(vcpu, vmcs12, exit_reason);
9824 case EXIT_REASON_INVALID_STATE:
9826 case EXIT_REASON_MWAIT_INSTRUCTION:
9827 return nested_cpu_has(vmcs12, CPU_BASED_MWAIT_EXITING);
9828 case EXIT_REASON_MONITOR_TRAP_FLAG:
9829 return nested_cpu_has(vmcs12, CPU_BASED_MONITOR_TRAP_FLAG);
9830 case EXIT_REASON_MONITOR_INSTRUCTION:
9831 return nested_cpu_has(vmcs12, CPU_BASED_MONITOR_EXITING);
9832 case EXIT_REASON_PAUSE_INSTRUCTION:
9833 return nested_cpu_has(vmcs12, CPU_BASED_PAUSE_EXITING) ||
9834 nested_cpu_has2(vmcs12,
9835 SECONDARY_EXEC_PAUSE_LOOP_EXITING);
9836 case EXIT_REASON_MCE_DURING_VMENTRY:
9838 case EXIT_REASON_TPR_BELOW_THRESHOLD:
9839 return nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW);
9840 case EXIT_REASON_APIC_ACCESS:
9841 case EXIT_REASON_APIC_WRITE:
9842 case EXIT_REASON_EOI_INDUCED:
9844 * The controls for "virtualize APIC accesses," "APIC-
9845 * register virtualization," and "virtual-interrupt
9846 * delivery" only come from vmcs12.
9849 case EXIT_REASON_EPT_VIOLATION:
9851 * L0 always deals with the EPT violation. If nested EPT is
9852 * used, and the nested mmu code discovers that the address is
9853 * missing in the guest EPT table (EPT12), the EPT violation
9854 * will be injected with nested_ept_inject_page_fault()
9857 case EXIT_REASON_EPT_MISCONFIG:
9859 * L2 never uses directly L1's EPT, but rather L0's own EPT
9860 * table (shadow on EPT) or a merged EPT table that L0 built
9861 * (EPT on EPT). So any problems with the structure of the
9862 * table is L0's fault.
9865 case EXIT_REASON_INVPCID:
9867 nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_INVPCID) &&
9868 nested_cpu_has(vmcs12, CPU_BASED_INVLPG_EXITING);
9869 case EXIT_REASON_WBINVD:
9870 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_WBINVD_EXITING);
9871 case EXIT_REASON_XSETBV:
9873 case EXIT_REASON_XSAVES: case EXIT_REASON_XRSTORS:
9875 * This should never happen, since it is not possible to
9876 * set XSS to a non-zero value---neither in L1 nor in L2.
9877 * If if it were, XSS would have to be checked against
9878 * the XSS exit bitmap in vmcs12.
9880 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_XSAVES);
9881 case EXIT_REASON_PREEMPTION_TIMER:
9883 case EXIT_REASON_PML_FULL:
9884 /* We emulate PML support to L1. */
9886 case EXIT_REASON_VMFUNC:
9887 /* VM functions are emulated through L2->L0 vmexits. */
9889 case EXIT_REASON_ENCLS:
9890 /* SGX is never exposed to L1 */
9897 static int nested_vmx_reflect_vmexit(struct kvm_vcpu *vcpu, u32 exit_reason)
9899 u32 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
9902 * At this point, the exit interruption info in exit_intr_info
9903 * is only valid for EXCEPTION_NMI exits. For EXTERNAL_INTERRUPT
9904 * we need to query the in-kernel LAPIC.
9906 WARN_ON(exit_reason == EXIT_REASON_EXTERNAL_INTERRUPT);
9907 if ((exit_intr_info &
9908 (INTR_INFO_VALID_MASK | INTR_INFO_DELIVER_CODE_MASK)) ==
9909 (INTR_INFO_VALID_MASK | INTR_INFO_DELIVER_CODE_MASK)) {
9910 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
9911 vmcs12->vm_exit_intr_error_code =
9912 vmcs_read32(VM_EXIT_INTR_ERROR_CODE);
9915 nested_vmx_vmexit(vcpu, exit_reason, exit_intr_info,
9916 vmcs_readl(EXIT_QUALIFICATION));
9920 static void vmx_get_exit_info(struct kvm_vcpu *vcpu, u64 *info1, u64 *info2)
9922 *info1 = vmcs_readl(EXIT_QUALIFICATION);
9923 *info2 = vmcs_read32(VM_EXIT_INTR_INFO);
9926 static void vmx_destroy_pml_buffer(struct vcpu_vmx *vmx)
9929 __free_page(vmx->pml_pg);
9934 static void vmx_flush_pml_buffer(struct kvm_vcpu *vcpu)
9936 struct vcpu_vmx *vmx = to_vmx(vcpu);
9940 pml_idx = vmcs_read16(GUEST_PML_INDEX);
9942 /* Do nothing if PML buffer is empty */
9943 if (pml_idx == (PML_ENTITY_NUM - 1))
9946 /* PML index always points to next available PML buffer entity */
9947 if (pml_idx >= PML_ENTITY_NUM)
9952 pml_buf = page_address(vmx->pml_pg);
9953 for (; pml_idx < PML_ENTITY_NUM; pml_idx++) {
9956 gpa = pml_buf[pml_idx];
9957 WARN_ON(gpa & (PAGE_SIZE - 1));
9958 kvm_vcpu_mark_page_dirty(vcpu, gpa >> PAGE_SHIFT);
9961 /* reset PML index */
9962 vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1);
9966 * Flush all vcpus' PML buffer and update logged GPAs to dirty_bitmap.
9967 * Called before reporting dirty_bitmap to userspace.
9969 static void kvm_flush_pml_buffers(struct kvm *kvm)
9972 struct kvm_vcpu *vcpu;
9974 * We only need to kick vcpu out of guest mode here, as PML buffer
9975 * is flushed at beginning of all VMEXITs, and it's obvious that only
9976 * vcpus running in guest are possible to have unflushed GPAs in PML
9979 kvm_for_each_vcpu(i, vcpu, kvm)
9980 kvm_vcpu_kick(vcpu);
9983 static void vmx_dump_sel(char *name, uint32_t sel)
9985 pr_err("%s sel=0x%04x, attr=0x%05x, limit=0x%08x, base=0x%016lx\n",
9986 name, vmcs_read16(sel),
9987 vmcs_read32(sel + GUEST_ES_AR_BYTES - GUEST_ES_SELECTOR),
9988 vmcs_read32(sel + GUEST_ES_LIMIT - GUEST_ES_SELECTOR),
9989 vmcs_readl(sel + GUEST_ES_BASE - GUEST_ES_SELECTOR));
9992 static void vmx_dump_dtsel(char *name, uint32_t limit)
9994 pr_err("%s limit=0x%08x, base=0x%016lx\n",
9995 name, vmcs_read32(limit),
9996 vmcs_readl(limit + GUEST_GDTR_BASE - GUEST_GDTR_LIMIT));
9999 static void dump_vmcs(void)
10001 u32 vmentry_ctl = vmcs_read32(VM_ENTRY_CONTROLS);
10002 u32 vmexit_ctl = vmcs_read32(VM_EXIT_CONTROLS);
10003 u32 cpu_based_exec_ctrl = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
10004 u32 pin_based_exec_ctrl = vmcs_read32(PIN_BASED_VM_EXEC_CONTROL);
10005 u32 secondary_exec_control = 0;
10006 unsigned long cr4 = vmcs_readl(GUEST_CR4);
10007 u64 efer = vmcs_read64(GUEST_IA32_EFER);
10010 if (cpu_has_secondary_exec_ctrls())
10011 secondary_exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
10013 pr_err("*** Guest State ***\n");
10014 pr_err("CR0: actual=0x%016lx, shadow=0x%016lx, gh_mask=%016lx\n",
10015 vmcs_readl(GUEST_CR0), vmcs_readl(CR0_READ_SHADOW),
10016 vmcs_readl(CR0_GUEST_HOST_MASK));
10017 pr_err("CR4: actual=0x%016lx, shadow=0x%016lx, gh_mask=%016lx\n",
10018 cr4, vmcs_readl(CR4_READ_SHADOW), vmcs_readl(CR4_GUEST_HOST_MASK));
10019 pr_err("CR3 = 0x%016lx\n", vmcs_readl(GUEST_CR3));
10020 if ((secondary_exec_control & SECONDARY_EXEC_ENABLE_EPT) &&
10021 (cr4 & X86_CR4_PAE) && !(efer & EFER_LMA))
10023 pr_err("PDPTR0 = 0x%016llx PDPTR1 = 0x%016llx\n",
10024 vmcs_read64(GUEST_PDPTR0), vmcs_read64(GUEST_PDPTR1));
10025 pr_err("PDPTR2 = 0x%016llx PDPTR3 = 0x%016llx\n",
10026 vmcs_read64(GUEST_PDPTR2), vmcs_read64(GUEST_PDPTR3));
10028 pr_err("RSP = 0x%016lx RIP = 0x%016lx\n",
10029 vmcs_readl(GUEST_RSP), vmcs_readl(GUEST_RIP));
10030 pr_err("RFLAGS=0x%08lx DR7 = 0x%016lx\n",
10031 vmcs_readl(GUEST_RFLAGS), vmcs_readl(GUEST_DR7));
10032 pr_err("Sysenter RSP=%016lx CS:RIP=%04x:%016lx\n",
10033 vmcs_readl(GUEST_SYSENTER_ESP),
10034 vmcs_read32(GUEST_SYSENTER_CS), vmcs_readl(GUEST_SYSENTER_EIP));
10035 vmx_dump_sel("CS: ", GUEST_CS_SELECTOR);
10036 vmx_dump_sel("DS: ", GUEST_DS_SELECTOR);
10037 vmx_dump_sel("SS: ", GUEST_SS_SELECTOR);
10038 vmx_dump_sel("ES: ", GUEST_ES_SELECTOR);
10039 vmx_dump_sel("FS: ", GUEST_FS_SELECTOR);
10040 vmx_dump_sel("GS: ", GUEST_GS_SELECTOR);
10041 vmx_dump_dtsel("GDTR:", GUEST_GDTR_LIMIT);
10042 vmx_dump_sel("LDTR:", GUEST_LDTR_SELECTOR);
10043 vmx_dump_dtsel("IDTR:", GUEST_IDTR_LIMIT);
10044 vmx_dump_sel("TR: ", GUEST_TR_SELECTOR);
10045 if ((vmexit_ctl & (VM_EXIT_SAVE_IA32_PAT | VM_EXIT_SAVE_IA32_EFER)) ||
10046 (vmentry_ctl & (VM_ENTRY_LOAD_IA32_PAT | VM_ENTRY_LOAD_IA32_EFER)))
10047 pr_err("EFER = 0x%016llx PAT = 0x%016llx\n",
10048 efer, vmcs_read64(GUEST_IA32_PAT));
10049 pr_err("DebugCtl = 0x%016llx DebugExceptions = 0x%016lx\n",
10050 vmcs_read64(GUEST_IA32_DEBUGCTL),
10051 vmcs_readl(GUEST_PENDING_DBG_EXCEPTIONS));
10052 if (cpu_has_load_perf_global_ctrl &&
10053 vmentry_ctl & VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL)
10054 pr_err("PerfGlobCtl = 0x%016llx\n",
10055 vmcs_read64(GUEST_IA32_PERF_GLOBAL_CTRL));
10056 if (vmentry_ctl & VM_ENTRY_LOAD_BNDCFGS)
10057 pr_err("BndCfgS = 0x%016llx\n", vmcs_read64(GUEST_BNDCFGS));
10058 pr_err("Interruptibility = %08x ActivityState = %08x\n",
10059 vmcs_read32(GUEST_INTERRUPTIBILITY_INFO),
10060 vmcs_read32(GUEST_ACTIVITY_STATE));
10061 if (secondary_exec_control & SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY)
10062 pr_err("InterruptStatus = %04x\n",
10063 vmcs_read16(GUEST_INTR_STATUS));
10065 pr_err("*** Host State ***\n");
10066 pr_err("RIP = 0x%016lx RSP = 0x%016lx\n",
10067 vmcs_readl(HOST_RIP), vmcs_readl(HOST_RSP));
10068 pr_err("CS=%04x SS=%04x DS=%04x ES=%04x FS=%04x GS=%04x TR=%04x\n",
10069 vmcs_read16(HOST_CS_SELECTOR), vmcs_read16(HOST_SS_SELECTOR),
10070 vmcs_read16(HOST_DS_SELECTOR), vmcs_read16(HOST_ES_SELECTOR),
10071 vmcs_read16(HOST_FS_SELECTOR), vmcs_read16(HOST_GS_SELECTOR),
10072 vmcs_read16(HOST_TR_SELECTOR));
10073 pr_err("FSBase=%016lx GSBase=%016lx TRBase=%016lx\n",
10074 vmcs_readl(HOST_FS_BASE), vmcs_readl(HOST_GS_BASE),
10075 vmcs_readl(HOST_TR_BASE));
10076 pr_err("GDTBase=%016lx IDTBase=%016lx\n",
10077 vmcs_readl(HOST_GDTR_BASE), vmcs_readl(HOST_IDTR_BASE));
10078 pr_err("CR0=%016lx CR3=%016lx CR4=%016lx\n",
10079 vmcs_readl(HOST_CR0), vmcs_readl(HOST_CR3),
10080 vmcs_readl(HOST_CR4));
10081 pr_err("Sysenter RSP=%016lx CS:RIP=%04x:%016lx\n",
10082 vmcs_readl(HOST_IA32_SYSENTER_ESP),
10083 vmcs_read32(HOST_IA32_SYSENTER_CS),
10084 vmcs_readl(HOST_IA32_SYSENTER_EIP));
10085 if (vmexit_ctl & (VM_EXIT_LOAD_IA32_PAT | VM_EXIT_LOAD_IA32_EFER))
10086 pr_err("EFER = 0x%016llx PAT = 0x%016llx\n",
10087 vmcs_read64(HOST_IA32_EFER),
10088 vmcs_read64(HOST_IA32_PAT));
10089 if (cpu_has_load_perf_global_ctrl &&
10090 vmexit_ctl & VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL)
10091 pr_err("PerfGlobCtl = 0x%016llx\n",
10092 vmcs_read64(HOST_IA32_PERF_GLOBAL_CTRL));
10094 pr_err("*** Control State ***\n");
10095 pr_err("PinBased=%08x CPUBased=%08x SecondaryExec=%08x\n",
10096 pin_based_exec_ctrl, cpu_based_exec_ctrl, secondary_exec_control);
10097 pr_err("EntryControls=%08x ExitControls=%08x\n", vmentry_ctl, vmexit_ctl);
10098 pr_err("ExceptionBitmap=%08x PFECmask=%08x PFECmatch=%08x\n",
10099 vmcs_read32(EXCEPTION_BITMAP),
10100 vmcs_read32(PAGE_FAULT_ERROR_CODE_MASK),
10101 vmcs_read32(PAGE_FAULT_ERROR_CODE_MATCH));
10102 pr_err("VMEntry: intr_info=%08x errcode=%08x ilen=%08x\n",
10103 vmcs_read32(VM_ENTRY_INTR_INFO_FIELD),
10104 vmcs_read32(VM_ENTRY_EXCEPTION_ERROR_CODE),
10105 vmcs_read32(VM_ENTRY_INSTRUCTION_LEN));
10106 pr_err("VMExit: intr_info=%08x errcode=%08x ilen=%08x\n",
10107 vmcs_read32(VM_EXIT_INTR_INFO),
10108 vmcs_read32(VM_EXIT_INTR_ERROR_CODE),
10109 vmcs_read32(VM_EXIT_INSTRUCTION_LEN));
10110 pr_err(" reason=%08x qualification=%016lx\n",
10111 vmcs_read32(VM_EXIT_REASON), vmcs_readl(EXIT_QUALIFICATION));
10112 pr_err("IDTVectoring: info=%08x errcode=%08x\n",
10113 vmcs_read32(IDT_VECTORING_INFO_FIELD),
10114 vmcs_read32(IDT_VECTORING_ERROR_CODE));
10115 pr_err("TSC Offset = 0x%016llx\n", vmcs_read64(TSC_OFFSET));
10116 if (secondary_exec_control & SECONDARY_EXEC_TSC_SCALING)
10117 pr_err("TSC Multiplier = 0x%016llx\n",
10118 vmcs_read64(TSC_MULTIPLIER));
10119 if (cpu_based_exec_ctrl & CPU_BASED_TPR_SHADOW)
10120 pr_err("TPR Threshold = 0x%02x\n", vmcs_read32(TPR_THRESHOLD));
10121 if (pin_based_exec_ctrl & PIN_BASED_POSTED_INTR)
10122 pr_err("PostedIntrVec = 0x%02x\n", vmcs_read16(POSTED_INTR_NV));
10123 if ((secondary_exec_control & SECONDARY_EXEC_ENABLE_EPT))
10124 pr_err("EPT pointer = 0x%016llx\n", vmcs_read64(EPT_POINTER));
10125 n = vmcs_read32(CR3_TARGET_COUNT);
10126 for (i = 0; i + 1 < n; i += 4)
10127 pr_err("CR3 target%u=%016lx target%u=%016lx\n",
10128 i, vmcs_readl(CR3_TARGET_VALUE0 + i * 2),
10129 i + 1, vmcs_readl(CR3_TARGET_VALUE0 + i * 2 + 2));
10131 pr_err("CR3 target%u=%016lx\n",
10132 i, vmcs_readl(CR3_TARGET_VALUE0 + i * 2));
10133 if (secondary_exec_control & SECONDARY_EXEC_PAUSE_LOOP_EXITING)
10134 pr_err("PLE Gap=%08x Window=%08x\n",
10135 vmcs_read32(PLE_GAP), vmcs_read32(PLE_WINDOW));
10136 if (secondary_exec_control & SECONDARY_EXEC_ENABLE_VPID)
10137 pr_err("Virtual processor ID = 0x%04x\n",
10138 vmcs_read16(VIRTUAL_PROCESSOR_ID));
10142 * The guest has exited. See if we can fix it or if we need userspace
10145 static int vmx_handle_exit(struct kvm_vcpu *vcpu)
10147 struct vcpu_vmx *vmx = to_vmx(vcpu);
10148 u32 exit_reason = vmx->exit_reason;
10149 u32 vectoring_info = vmx->idt_vectoring_info;
10151 trace_kvm_exit(exit_reason, vcpu, KVM_ISA_VMX);
10154 * Flush logged GPAs PML buffer, this will make dirty_bitmap more
10155 * updated. Another good is, in kvm_vm_ioctl_get_dirty_log, before
10156 * querying dirty_bitmap, we only need to kick all vcpus out of guest
10157 * mode as if vcpus is in root mode, the PML buffer must has been
10161 vmx_flush_pml_buffer(vcpu);
10163 /* If guest state is invalid, start emulating */
10164 if (vmx->emulation_required)
10165 return handle_invalid_guest_state(vcpu);
10167 if (is_guest_mode(vcpu) && nested_vmx_exit_reflected(vcpu, exit_reason))
10168 return nested_vmx_reflect_vmexit(vcpu, exit_reason);
10170 if (exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY) {
10172 vcpu->run->exit_reason = KVM_EXIT_FAIL_ENTRY;
10173 vcpu->run->fail_entry.hardware_entry_failure_reason
10178 if (unlikely(vmx->fail)) {
10179 vcpu->run->exit_reason = KVM_EXIT_FAIL_ENTRY;
10180 vcpu->run->fail_entry.hardware_entry_failure_reason
10181 = vmcs_read32(VM_INSTRUCTION_ERROR);
10187 * Do not try to fix EXIT_REASON_EPT_MISCONFIG if it caused by
10188 * delivery event since it indicates guest is accessing MMIO.
10189 * The vm-exit can be triggered again after return to guest that
10190 * will cause infinite loop.
10192 if ((vectoring_info & VECTORING_INFO_VALID_MASK) &&
10193 (exit_reason != EXIT_REASON_EXCEPTION_NMI &&
10194 exit_reason != EXIT_REASON_EPT_VIOLATION &&
10195 exit_reason != EXIT_REASON_PML_FULL &&
10196 exit_reason != EXIT_REASON_APIC_ACCESS &&
10197 exit_reason != EXIT_REASON_TASK_SWITCH)) {
10198 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
10199 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_DELIVERY_EV;
10200 vcpu->run->internal.ndata = 3;
10201 vcpu->run->internal.data[0] = vectoring_info;
10202 vcpu->run->internal.data[1] = exit_reason;
10203 vcpu->run->internal.data[2] = vcpu->arch.exit_qualification;
10204 if (exit_reason == EXIT_REASON_EPT_MISCONFIG) {
10205 vcpu->run->internal.ndata++;
10206 vcpu->run->internal.data[3] =
10207 vmcs_read64(GUEST_PHYSICAL_ADDRESS);
10212 if (unlikely(!enable_vnmi &&
10213 vmx->loaded_vmcs->soft_vnmi_blocked)) {
10214 if (vmx_interrupt_allowed(vcpu)) {
10215 vmx->loaded_vmcs->soft_vnmi_blocked = 0;
10216 } else if (vmx->loaded_vmcs->vnmi_blocked_time > 1000000000LL &&
10217 vcpu->arch.nmi_pending) {
10219 * This CPU don't support us in finding the end of an
10220 * NMI-blocked window if the guest runs with IRQs
10221 * disabled. So we pull the trigger after 1 s of
10222 * futile waiting, but inform the user about this.
10224 printk(KERN_WARNING "%s: Breaking out of NMI-blocked "
10225 "state on VCPU %d after 1 s timeout\n",
10226 __func__, vcpu->vcpu_id);
10227 vmx->loaded_vmcs->soft_vnmi_blocked = 0;
10231 if (exit_reason < kvm_vmx_max_exit_handlers
10232 && kvm_vmx_exit_handlers[exit_reason])
10233 return kvm_vmx_exit_handlers[exit_reason](vcpu);
10235 vcpu_unimpl(vcpu, "vmx: unexpected exit reason 0x%x\n",
10237 kvm_queue_exception(vcpu, UD_VECTOR);
10243 * Software based L1D cache flush which is used when microcode providing
10244 * the cache control MSR is not loaded.
10246 * The L1D cache is 32 KiB on Nehalem and later microarchitectures, but to
10247 * flush it is required to read in 64 KiB because the replacement algorithm
10248 * is not exactly LRU. This could be sized at runtime via topology
10249 * information but as all relevant affected CPUs have 32KiB L1D cache size
10250 * there is no point in doing so.
10252 static void vmx_l1d_flush(struct kvm_vcpu *vcpu)
10254 int size = PAGE_SIZE << L1D_CACHE_ORDER;
10257 * This code is only executed when the the flush mode is 'cond' or
10260 if (static_branch_likely(&vmx_l1d_flush_cond)) {
10264 * Clear the per-vcpu flush bit, it gets set again
10265 * either from vcpu_run() or from one of the unsafe
10268 flush_l1d = vcpu->arch.l1tf_flush_l1d;
10269 vcpu->arch.l1tf_flush_l1d = false;
10272 * Clear the per-cpu flush bit, it gets set again from
10273 * the interrupt handlers.
10275 flush_l1d |= kvm_get_cpu_l1tf_flush_l1d();
10276 kvm_clear_cpu_l1tf_flush_l1d();
10282 vcpu->stat.l1d_flush++;
10284 if (static_cpu_has(X86_FEATURE_FLUSH_L1D)) {
10285 wrmsrl(MSR_IA32_FLUSH_CMD, L1D_FLUSH);
10290 /* First ensure the pages are in the TLB */
10291 "xorl %%eax, %%eax\n"
10292 ".Lpopulate_tlb:\n\t"
10293 "movzbl (%[flush_pages], %%" _ASM_AX "), %%ecx\n\t"
10294 "addl $4096, %%eax\n\t"
10295 "cmpl %%eax, %[size]\n\t"
10296 "jne .Lpopulate_tlb\n\t"
10297 "xorl %%eax, %%eax\n\t"
10299 /* Now fill the cache */
10300 "xorl %%eax, %%eax\n"
10302 "movzbl (%[flush_pages], %%" _ASM_AX "), %%ecx\n\t"
10303 "addl $64, %%eax\n\t"
10304 "cmpl %%eax, %[size]\n\t"
10305 "jne .Lfill_cache\n\t"
10307 :: [flush_pages] "r" (vmx_l1d_flush_pages),
10309 : "eax", "ebx", "ecx", "edx");
10312 static void update_cr8_intercept(struct kvm_vcpu *vcpu, int tpr, int irr)
10314 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
10316 if (is_guest_mode(vcpu) &&
10317 nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW))
10320 if (irr == -1 || tpr < irr) {
10321 vmcs_write32(TPR_THRESHOLD, 0);
10325 vmcs_write32(TPR_THRESHOLD, irr);
10328 static void vmx_set_virtual_apic_mode(struct kvm_vcpu *vcpu)
10330 u32 sec_exec_control;
10332 if (!lapic_in_kernel(vcpu))
10335 if (!flexpriority_enabled &&
10336 !cpu_has_vmx_virtualize_x2apic_mode())
10339 /* Postpone execution until vmcs01 is the current VMCS. */
10340 if (is_guest_mode(vcpu)) {
10341 to_vmx(vcpu)->nested.change_vmcs01_virtual_apic_mode = true;
10345 sec_exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
10346 sec_exec_control &= ~(SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
10347 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE);
10349 switch (kvm_get_apic_mode(vcpu)) {
10350 case LAPIC_MODE_INVALID:
10351 WARN_ONCE(true, "Invalid local APIC state");
10352 case LAPIC_MODE_DISABLED:
10354 case LAPIC_MODE_XAPIC:
10355 if (flexpriority_enabled) {
10356 sec_exec_control |=
10357 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
10358 vmx_flush_tlb(vcpu, true);
10361 case LAPIC_MODE_X2APIC:
10362 if (cpu_has_vmx_virtualize_x2apic_mode())
10363 sec_exec_control |=
10364 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
10367 vmcs_write32(SECONDARY_VM_EXEC_CONTROL, sec_exec_control);
10369 vmx_update_msr_bitmap(vcpu);
10372 static void vmx_set_apic_access_page_addr(struct kvm_vcpu *vcpu, hpa_t hpa)
10374 if (!is_guest_mode(vcpu)) {
10375 vmcs_write64(APIC_ACCESS_ADDR, hpa);
10376 vmx_flush_tlb(vcpu, true);
10380 static void vmx_hwapic_isr_update(struct kvm_vcpu *vcpu, int max_isr)
10388 status = vmcs_read16(GUEST_INTR_STATUS);
10390 if (max_isr != old) {
10392 status |= max_isr << 8;
10393 vmcs_write16(GUEST_INTR_STATUS, status);
10397 static void vmx_set_rvi(int vector)
10405 status = vmcs_read16(GUEST_INTR_STATUS);
10406 old = (u8)status & 0xff;
10407 if ((u8)vector != old) {
10409 status |= (u8)vector;
10410 vmcs_write16(GUEST_INTR_STATUS, status);
10414 static void vmx_hwapic_irr_update(struct kvm_vcpu *vcpu, int max_irr)
10417 * When running L2, updating RVI is only relevant when
10418 * vmcs12 virtual-interrupt-delivery enabled.
10419 * However, it can be enabled only when L1 also
10420 * intercepts external-interrupts and in that case
10421 * we should not update vmcs02 RVI but instead intercept
10422 * interrupt. Therefore, do nothing when running L2.
10424 if (!is_guest_mode(vcpu))
10425 vmx_set_rvi(max_irr);
10428 static int vmx_sync_pir_to_irr(struct kvm_vcpu *vcpu)
10430 struct vcpu_vmx *vmx = to_vmx(vcpu);
10432 bool max_irr_updated;
10434 WARN_ON(!vcpu->arch.apicv_active);
10435 if (pi_test_on(&vmx->pi_desc)) {
10436 pi_clear_on(&vmx->pi_desc);
10438 * IOMMU can write to PIR.ON, so the barrier matters even on UP.
10439 * But on x86 this is just a compiler barrier anyway.
10441 smp_mb__after_atomic();
10443 kvm_apic_update_irr(vcpu, vmx->pi_desc.pir, &max_irr);
10446 * If we are running L2 and L1 has a new pending interrupt
10447 * which can be injected, we should re-evaluate
10448 * what should be done with this new L1 interrupt.
10449 * If L1 intercepts external-interrupts, we should
10450 * exit from L2 to L1. Otherwise, interrupt should be
10451 * delivered directly to L2.
10453 if (is_guest_mode(vcpu) && max_irr_updated) {
10454 if (nested_exit_on_intr(vcpu))
10455 kvm_vcpu_exiting_guest_mode(vcpu);
10457 kvm_make_request(KVM_REQ_EVENT, vcpu);
10460 max_irr = kvm_lapic_find_highest_irr(vcpu);
10462 vmx_hwapic_irr_update(vcpu, max_irr);
10466 static u8 vmx_has_apicv_interrupt(struct kvm_vcpu *vcpu)
10468 u8 rvi = vmx_get_rvi();
10469 u8 vppr = kvm_lapic_get_reg(vcpu->arch.apic, APIC_PROCPRI);
10471 return ((rvi & 0xf0) > (vppr & 0xf0));
10474 static bool vmx_dy_apicv_has_pending_interrupt(struct kvm_vcpu *vcpu)
10476 return pi_test_on(vcpu_to_pi_desc(vcpu));
10479 static void vmx_load_eoi_exitmap(struct kvm_vcpu *vcpu, u64 *eoi_exit_bitmap)
10481 if (!kvm_vcpu_apicv_active(vcpu))
10484 vmcs_write64(EOI_EXIT_BITMAP0, eoi_exit_bitmap[0]);
10485 vmcs_write64(EOI_EXIT_BITMAP1, eoi_exit_bitmap[1]);
10486 vmcs_write64(EOI_EXIT_BITMAP2, eoi_exit_bitmap[2]);
10487 vmcs_write64(EOI_EXIT_BITMAP3, eoi_exit_bitmap[3]);
10490 static void vmx_apicv_post_state_restore(struct kvm_vcpu *vcpu)
10492 struct vcpu_vmx *vmx = to_vmx(vcpu);
10494 pi_clear_on(&vmx->pi_desc);
10495 memset(vmx->pi_desc.pir, 0, sizeof(vmx->pi_desc.pir));
10498 static void vmx_complete_atomic_exit(struct vcpu_vmx *vmx)
10500 if (vmx->exit_reason != EXIT_REASON_EXCEPTION_NMI)
10503 vmx->exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
10505 /* if exit due to PF check for async PF */
10506 if (is_page_fault(vmx->exit_intr_info))
10507 vmx->vcpu.arch.apf.host_apf_reason = kvm_read_and_reset_pf_reason();
10509 /* Handle machine checks before interrupts are enabled */
10510 if (is_machine_check(vmx->exit_intr_info))
10511 kvm_machine_check();
10513 /* We need to handle NMIs before interrupts are enabled */
10514 if (is_nmi(vmx->exit_intr_info)) {
10515 kvm_before_interrupt(&vmx->vcpu);
10517 kvm_after_interrupt(&vmx->vcpu);
10521 static void vmx_handle_external_intr(struct kvm_vcpu *vcpu)
10523 u32 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
10525 if ((exit_intr_info & (INTR_INFO_VALID_MASK | INTR_INFO_INTR_TYPE_MASK))
10526 == (INTR_INFO_VALID_MASK | INTR_TYPE_EXT_INTR)) {
10527 unsigned int vector;
10528 unsigned long entry;
10530 struct vcpu_vmx *vmx = to_vmx(vcpu);
10531 #ifdef CONFIG_X86_64
10535 vector = exit_intr_info & INTR_INFO_VECTOR_MASK;
10536 desc = (gate_desc *)vmx->host_idt_base + vector;
10537 entry = gate_offset(desc);
10539 #ifdef CONFIG_X86_64
10540 "mov %%" _ASM_SP ", %[sp]\n\t"
10541 "and $0xfffffffffffffff0, %%" _ASM_SP "\n\t"
10546 __ASM_SIZE(push) " $%c[cs]\n\t"
10549 #ifdef CONFIG_X86_64
10552 ASM_CALL_CONSTRAINT
10554 THUNK_TARGET(entry),
10555 [ss]"i"(__KERNEL_DS),
10556 [cs]"i"(__KERNEL_CS)
10560 STACK_FRAME_NON_STANDARD(vmx_handle_external_intr);
10562 static bool vmx_has_emulated_msr(int index)
10565 case MSR_IA32_SMBASE:
10567 * We cannot do SMM unless we can run the guest in big
10570 return enable_unrestricted_guest || emulate_invalid_guest_state;
10571 case MSR_AMD64_VIRT_SPEC_CTRL:
10572 /* This is AMD only. */
10579 static bool vmx_mpx_supported(void)
10581 return (vmcs_config.vmexit_ctrl & VM_EXIT_CLEAR_BNDCFGS) &&
10582 (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_BNDCFGS);
10585 static bool vmx_xsaves_supported(void)
10587 return vmcs_config.cpu_based_2nd_exec_ctrl &
10588 SECONDARY_EXEC_XSAVES;
10591 static void vmx_recover_nmi_blocking(struct vcpu_vmx *vmx)
10593 u32 exit_intr_info;
10596 bool idtv_info_valid;
10598 idtv_info_valid = vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK;
10601 if (vmx->loaded_vmcs->nmi_known_unmasked)
10604 * Can't use vmx->exit_intr_info since we're not sure what
10605 * the exit reason is.
10607 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
10608 unblock_nmi = (exit_intr_info & INTR_INFO_UNBLOCK_NMI) != 0;
10609 vector = exit_intr_info & INTR_INFO_VECTOR_MASK;
10611 * SDM 3: 27.7.1.2 (September 2008)
10612 * Re-set bit "block by NMI" before VM entry if vmexit caused by
10613 * a guest IRET fault.
10614 * SDM 3: 23.2.2 (September 2008)
10615 * Bit 12 is undefined in any of the following cases:
10616 * If the VM exit sets the valid bit in the IDT-vectoring
10617 * information field.
10618 * If the VM exit is due to a double fault.
10620 if ((exit_intr_info & INTR_INFO_VALID_MASK) && unblock_nmi &&
10621 vector != DF_VECTOR && !idtv_info_valid)
10622 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
10623 GUEST_INTR_STATE_NMI);
10625 vmx->loaded_vmcs->nmi_known_unmasked =
10626 !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO)
10627 & GUEST_INTR_STATE_NMI);
10628 } else if (unlikely(vmx->loaded_vmcs->soft_vnmi_blocked))
10629 vmx->loaded_vmcs->vnmi_blocked_time +=
10630 ktime_to_ns(ktime_sub(ktime_get(),
10631 vmx->loaded_vmcs->entry_time));
10634 static void __vmx_complete_interrupts(struct kvm_vcpu *vcpu,
10635 u32 idt_vectoring_info,
10636 int instr_len_field,
10637 int error_code_field)
10641 bool idtv_info_valid;
10643 idtv_info_valid = idt_vectoring_info & VECTORING_INFO_VALID_MASK;
10645 vcpu->arch.nmi_injected = false;
10646 kvm_clear_exception_queue(vcpu);
10647 kvm_clear_interrupt_queue(vcpu);
10649 if (!idtv_info_valid)
10652 kvm_make_request(KVM_REQ_EVENT, vcpu);
10654 vector = idt_vectoring_info & VECTORING_INFO_VECTOR_MASK;
10655 type = idt_vectoring_info & VECTORING_INFO_TYPE_MASK;
10658 case INTR_TYPE_NMI_INTR:
10659 vcpu->arch.nmi_injected = true;
10661 * SDM 3: 27.7.1.2 (September 2008)
10662 * Clear bit "block by NMI" before VM entry if a NMI
10663 * delivery faulted.
10665 vmx_set_nmi_mask(vcpu, false);
10667 case INTR_TYPE_SOFT_EXCEPTION:
10668 vcpu->arch.event_exit_inst_len = vmcs_read32(instr_len_field);
10670 case INTR_TYPE_HARD_EXCEPTION:
10671 if (idt_vectoring_info & VECTORING_INFO_DELIVER_CODE_MASK) {
10672 u32 err = vmcs_read32(error_code_field);
10673 kvm_requeue_exception_e(vcpu, vector, err);
10675 kvm_requeue_exception(vcpu, vector);
10677 case INTR_TYPE_SOFT_INTR:
10678 vcpu->arch.event_exit_inst_len = vmcs_read32(instr_len_field);
10680 case INTR_TYPE_EXT_INTR:
10681 kvm_queue_interrupt(vcpu, vector, type == INTR_TYPE_SOFT_INTR);
10688 static void vmx_complete_interrupts(struct vcpu_vmx *vmx)
10690 __vmx_complete_interrupts(&vmx->vcpu, vmx->idt_vectoring_info,
10691 VM_EXIT_INSTRUCTION_LEN,
10692 IDT_VECTORING_ERROR_CODE);
10695 static void vmx_cancel_injection(struct kvm_vcpu *vcpu)
10697 __vmx_complete_interrupts(vcpu,
10698 vmcs_read32(VM_ENTRY_INTR_INFO_FIELD),
10699 VM_ENTRY_INSTRUCTION_LEN,
10700 VM_ENTRY_EXCEPTION_ERROR_CODE);
10702 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0);
10705 static void atomic_switch_perf_msrs(struct vcpu_vmx *vmx)
10708 struct perf_guest_switch_msr *msrs;
10710 msrs = perf_guest_get_msrs(&nr_msrs);
10715 for (i = 0; i < nr_msrs; i++)
10716 if (msrs[i].host == msrs[i].guest)
10717 clear_atomic_switch_msr(vmx, msrs[i].msr);
10719 add_atomic_switch_msr(vmx, msrs[i].msr, msrs[i].guest,
10720 msrs[i].host, false);
10723 static void vmx_arm_hv_timer(struct vcpu_vmx *vmx, u32 val)
10725 vmcs_write32(VMX_PREEMPTION_TIMER_VALUE, val);
10726 if (!vmx->loaded_vmcs->hv_timer_armed)
10727 vmcs_set_bits(PIN_BASED_VM_EXEC_CONTROL,
10728 PIN_BASED_VMX_PREEMPTION_TIMER);
10729 vmx->loaded_vmcs->hv_timer_armed = true;
10732 static void vmx_update_hv_timer(struct kvm_vcpu *vcpu)
10734 struct vcpu_vmx *vmx = to_vmx(vcpu);
10738 if (vmx->req_immediate_exit) {
10739 vmx_arm_hv_timer(vmx, 0);
10743 if (vmx->hv_deadline_tsc != -1) {
10745 if (vmx->hv_deadline_tsc > tscl)
10746 /* set_hv_timer ensures the delta fits in 32-bits */
10747 delta_tsc = (u32)((vmx->hv_deadline_tsc - tscl) >>
10748 cpu_preemption_timer_multi);
10752 vmx_arm_hv_timer(vmx, delta_tsc);
10756 if (vmx->loaded_vmcs->hv_timer_armed)
10757 vmcs_clear_bits(PIN_BASED_VM_EXEC_CONTROL,
10758 PIN_BASED_VMX_PREEMPTION_TIMER);
10759 vmx->loaded_vmcs->hv_timer_armed = false;
10762 static void __noclone vmx_vcpu_run(struct kvm_vcpu *vcpu)
10764 struct vcpu_vmx *vmx = to_vmx(vcpu);
10765 unsigned long cr3, cr4, evmcs_rsp;
10767 /* Record the guest's net vcpu time for enforced NMI injections. */
10768 if (unlikely(!enable_vnmi &&
10769 vmx->loaded_vmcs->soft_vnmi_blocked))
10770 vmx->loaded_vmcs->entry_time = ktime_get();
10772 /* Don't enter VMX if guest state is invalid, let the exit handler
10773 start emulation until we arrive back to a valid state */
10774 if (vmx->emulation_required)
10777 if (vmx->ple_window_dirty) {
10778 vmx->ple_window_dirty = false;
10779 vmcs_write32(PLE_WINDOW, vmx->ple_window);
10782 if (vmx->nested.sync_shadow_vmcs) {
10783 copy_vmcs12_to_shadow(vmx);
10784 vmx->nested.sync_shadow_vmcs = false;
10787 if (test_bit(VCPU_REGS_RSP, (unsigned long *)&vcpu->arch.regs_dirty))
10788 vmcs_writel(GUEST_RSP, vcpu->arch.regs[VCPU_REGS_RSP]);
10789 if (test_bit(VCPU_REGS_RIP, (unsigned long *)&vcpu->arch.regs_dirty))
10790 vmcs_writel(GUEST_RIP, vcpu->arch.regs[VCPU_REGS_RIP]);
10792 cr3 = __get_current_cr3_fast();
10793 if (unlikely(cr3 != vmx->loaded_vmcs->host_state.cr3)) {
10794 vmcs_writel(HOST_CR3, cr3);
10795 vmx->loaded_vmcs->host_state.cr3 = cr3;
10798 cr4 = cr4_read_shadow();
10799 if (unlikely(cr4 != vmx->loaded_vmcs->host_state.cr4)) {
10800 vmcs_writel(HOST_CR4, cr4);
10801 vmx->loaded_vmcs->host_state.cr4 = cr4;
10804 /* When single-stepping over STI and MOV SS, we must clear the
10805 * corresponding interruptibility bits in the guest state. Otherwise
10806 * vmentry fails as it then expects bit 14 (BS) in pending debug
10807 * exceptions being set, but that's not correct for the guest debugging
10809 if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP)
10810 vmx_set_interrupt_shadow(vcpu, 0);
10812 kvm_load_guest_xcr0(vcpu);
10814 if (static_cpu_has(X86_FEATURE_PKU) &&
10815 kvm_read_cr4_bits(vcpu, X86_CR4_PKE) &&
10816 vcpu->arch.pkru != vmx->host_pkru)
10817 __write_pkru(vcpu->arch.pkru);
10819 atomic_switch_perf_msrs(vmx);
10821 vmx_update_hv_timer(vcpu);
10824 * If this vCPU has touched SPEC_CTRL, restore the guest's value if
10825 * it's non-zero. Since vmentry is serialising on affected CPUs, there
10826 * is no need to worry about the conditional branch over the wrmsr
10827 * being speculatively taken.
10829 x86_spec_ctrl_set_guest(vmx->spec_ctrl, 0);
10831 vmx->__launched = vmx->loaded_vmcs->launched;
10833 evmcs_rsp = static_branch_unlikely(&enable_evmcs) ?
10834 (unsigned long)¤t_evmcs->host_rsp : 0;
10836 /* L1D Flush includes CPU buffer clear to mitigate MDS */
10837 if (static_branch_unlikely(&vmx_l1d_should_flush))
10838 vmx_l1d_flush(vcpu);
10839 else if (static_branch_unlikely(&mds_user_clear))
10840 mds_clear_cpu_buffers();
10841 else if (static_branch_unlikely(&mmio_stale_data_clear) &&
10842 kvm_arch_has_assigned_device(vcpu->kvm))
10843 mds_clear_cpu_buffers();
10845 vmx_disable_fb_clear(vmx);
10848 /* Store host registers */
10849 "push %%" _ASM_DX "; push %%" _ASM_BP ";"
10850 "push %%" _ASM_CX " \n\t" /* placeholder for guest rcx */
10851 "push %%" _ASM_CX " \n\t"
10852 "cmp %%" _ASM_SP ", %c[host_rsp](%%" _ASM_CX ") \n\t"
10854 "mov %%" _ASM_SP ", %c[host_rsp](%%" _ASM_CX ") \n\t"
10855 /* Avoid VMWRITE when Enlightened VMCS is in use */
10856 "test %%" _ASM_SI ", %%" _ASM_SI " \n\t"
10858 "mov %%" _ASM_SP ", (%%" _ASM_SI ") \n\t"
10861 __ex(ASM_VMX_VMWRITE_RSP_RDX) "\n\t"
10863 /* Reload cr2 if changed */
10864 "mov %c[cr2](%%" _ASM_CX "), %%" _ASM_AX " \n\t"
10865 "mov %%cr2, %%" _ASM_DX " \n\t"
10866 "cmp %%" _ASM_AX ", %%" _ASM_DX " \n\t"
10868 "mov %%" _ASM_AX", %%cr2 \n\t"
10870 /* Check if vmlaunch of vmresume is needed */
10871 "cmpb $0, %c[launched](%%" _ASM_CX ") \n\t"
10872 /* Load guest registers. Don't clobber flags. */
10873 "mov %c[rax](%%" _ASM_CX "), %%" _ASM_AX " \n\t"
10874 "mov %c[rbx](%%" _ASM_CX "), %%" _ASM_BX " \n\t"
10875 "mov %c[rdx](%%" _ASM_CX "), %%" _ASM_DX " \n\t"
10876 "mov %c[rsi](%%" _ASM_CX "), %%" _ASM_SI " \n\t"
10877 "mov %c[rdi](%%" _ASM_CX "), %%" _ASM_DI " \n\t"
10878 "mov %c[rbp](%%" _ASM_CX "), %%" _ASM_BP " \n\t"
10879 #ifdef CONFIG_X86_64
10880 "mov %c[r8](%%" _ASM_CX "), %%r8 \n\t"
10881 "mov %c[r9](%%" _ASM_CX "), %%r9 \n\t"
10882 "mov %c[r10](%%" _ASM_CX "), %%r10 \n\t"
10883 "mov %c[r11](%%" _ASM_CX "), %%r11 \n\t"
10884 "mov %c[r12](%%" _ASM_CX "), %%r12 \n\t"
10885 "mov %c[r13](%%" _ASM_CX "), %%r13 \n\t"
10886 "mov %c[r14](%%" _ASM_CX "), %%r14 \n\t"
10887 "mov %c[r15](%%" _ASM_CX "), %%r15 \n\t"
10889 /* Load guest RCX. This kills the vmx_vcpu pointer! */
10890 "mov %c[rcx](%%" _ASM_CX "), %%" _ASM_CX " \n\t"
10892 /* Enter guest mode */
10894 __ex(ASM_VMX_VMLAUNCH) "\n\t"
10896 "1: " __ex(ASM_VMX_VMRESUME) "\n\t"
10899 /* Save guest's RCX to the stack placeholder (see above) */
10900 "mov %%" _ASM_CX ", %c[wordsize](%%" _ASM_SP ") \n\t"
10902 /* Load host's RCX, i.e. the vmx_vcpu pointer */
10903 "pop %%" _ASM_CX " \n\t"
10905 /* Set vmx->fail based on EFLAGS.{CF,ZF} */
10906 "setbe %c[fail](%%" _ASM_CX ")\n\t"
10908 /* Save all guest registers, including RCX from the stack */
10909 "mov %%" _ASM_AX ", %c[rax](%%" _ASM_CX ") \n\t"
10910 "mov %%" _ASM_BX ", %c[rbx](%%" _ASM_CX ") \n\t"
10911 __ASM_SIZE(pop) " %c[rcx](%%" _ASM_CX ") \n\t"
10912 "mov %%" _ASM_DX ", %c[rdx](%%" _ASM_CX ") \n\t"
10913 "mov %%" _ASM_SI ", %c[rsi](%%" _ASM_CX ") \n\t"
10914 "mov %%" _ASM_DI ", %c[rdi](%%" _ASM_CX ") \n\t"
10915 "mov %%" _ASM_BP ", %c[rbp](%%" _ASM_CX ") \n\t"
10916 #ifdef CONFIG_X86_64
10917 "mov %%r8, %c[r8](%%" _ASM_CX ") \n\t"
10918 "mov %%r9, %c[r9](%%" _ASM_CX ") \n\t"
10919 "mov %%r10, %c[r10](%%" _ASM_CX ") \n\t"
10920 "mov %%r11, %c[r11](%%" _ASM_CX ") \n\t"
10921 "mov %%r12, %c[r12](%%" _ASM_CX ") \n\t"
10922 "mov %%r13, %c[r13](%%" _ASM_CX ") \n\t"
10923 "mov %%r14, %c[r14](%%" _ASM_CX ") \n\t"
10924 "mov %%r15, %c[r15](%%" _ASM_CX ") \n\t"
10927 * Clear all general purpose registers (except RSP, which is loaded by
10928 * the CPU during VM-Exit) to prevent speculative use of the guest's
10929 * values, even those that are saved/loaded via the stack. In theory,
10930 * an L1 cache miss when restoring registers could lead to speculative
10931 * execution with the guest's values. Zeroing XORs are dirt cheap,
10932 * i.e. the extra paranoia is essentially free.
10934 "xor %%r8d, %%r8d \n\t"
10935 "xor %%r9d, %%r9d \n\t"
10936 "xor %%r10d, %%r10d \n\t"
10937 "xor %%r11d, %%r11d \n\t"
10938 "xor %%r12d, %%r12d \n\t"
10939 "xor %%r13d, %%r13d \n\t"
10940 "xor %%r14d, %%r14d \n\t"
10941 "xor %%r15d, %%r15d \n\t"
10943 "mov %%cr2, %%" _ASM_AX " \n\t"
10944 "mov %%" _ASM_AX ", %c[cr2](%%" _ASM_CX ") \n\t"
10946 "xor %%eax, %%eax \n\t"
10947 "xor %%ebx, %%ebx \n\t"
10948 "xor %%ecx, %%ecx \n\t"
10949 "xor %%edx, %%edx \n\t"
10950 "xor %%esi, %%esi \n\t"
10951 "xor %%edi, %%edi \n\t"
10952 "xor %%ebp, %%ebp \n\t"
10953 "pop %%" _ASM_BP "; pop %%" _ASM_DX " \n\t"
10954 ".pushsection .rodata \n\t"
10955 ".global vmx_return \n\t"
10956 "vmx_return: " _ASM_PTR " 2b \n\t"
10958 : "=c"((int){0}), "=d"((int){0}), "=S"((int){0})
10959 : "c"(vmx), "d"((unsigned long)HOST_RSP), "S"(evmcs_rsp),
10960 [launched]"i"(offsetof(struct vcpu_vmx, __launched)),
10961 [fail]"i"(offsetof(struct vcpu_vmx, fail)),
10962 [host_rsp]"i"(offsetof(struct vcpu_vmx, host_rsp)),
10963 [rax]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RAX])),
10964 [rbx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBX])),
10965 [rcx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RCX])),
10966 [rdx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDX])),
10967 [rsi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RSI])),
10968 [rdi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDI])),
10969 [rbp]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBP])),
10970 #ifdef CONFIG_X86_64
10971 [r8]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R8])),
10972 [r9]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R9])),
10973 [r10]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R10])),
10974 [r11]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R11])),
10975 [r12]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R12])),
10976 [r13]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R13])),
10977 [r14]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R14])),
10978 [r15]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R15])),
10980 [cr2]"i"(offsetof(struct vcpu_vmx, vcpu.arch.cr2)),
10981 [wordsize]"i"(sizeof(ulong))
10983 #ifdef CONFIG_X86_64
10984 , "rax", "rbx", "rdi"
10985 , "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15"
10987 , "eax", "ebx", "edi"
10991 /* Eliminate branch target predictions from guest mode */
10994 vmx_enable_fb_clear(vmx);
10997 * We do not use IBRS in the kernel. If this vCPU has used the
10998 * SPEC_CTRL MSR it may have left it on; save the value and
10999 * turn it off. This is much more efficient than blindly adding
11000 * it to the atomic save/restore list. Especially as the former
11001 * (Saving guest MSRs on vmexit) doesn't even exist in KVM.
11003 * For non-nested case:
11004 * If the L01 MSR bitmap does not intercept the MSR, then we need to
11008 * If the L02 MSR bitmap does not intercept the MSR, then we need to
11011 if (unlikely(!msr_write_intercepted(vcpu, MSR_IA32_SPEC_CTRL)))
11012 vmx->spec_ctrl = native_read_msr(MSR_IA32_SPEC_CTRL);
11014 x86_spec_ctrl_restore_host(vmx->spec_ctrl, 0);
11016 /* All fields are clean at this point */
11017 if (static_branch_unlikely(&enable_evmcs))
11018 current_evmcs->hv_clean_fields |=
11019 HV_VMX_ENLIGHTENED_CLEAN_FIELD_ALL;
11021 /* MSR_IA32_DEBUGCTLMSR is zeroed on vmexit. Restore it if needed */
11022 if (vmx->host_debugctlmsr)
11023 update_debugctlmsr(vmx->host_debugctlmsr);
11025 #ifndef CONFIG_X86_64
11027 * The sysexit path does not restore ds/es, so we must set them to
11028 * a reasonable value ourselves.
11030 * We can't defer this to vmx_prepare_switch_to_host() since that
11031 * function may be executed in interrupt context, which saves and
11032 * restore segments around it, nullifying its effect.
11034 loadsegment(ds, __USER_DS);
11035 loadsegment(es, __USER_DS);
11038 vcpu->arch.regs_avail = ~((1 << VCPU_REGS_RIP) | (1 << VCPU_REGS_RSP)
11039 | (1 << VCPU_EXREG_RFLAGS)
11040 | (1 << VCPU_EXREG_PDPTR)
11041 | (1 << VCPU_EXREG_SEGMENTS)
11042 | (1 << VCPU_EXREG_CR3));
11043 vcpu->arch.regs_dirty = 0;
11046 * eager fpu is enabled if PKEY is supported and CR4 is switched
11047 * back on host, so it is safe to read guest PKRU from current
11050 if (static_cpu_has(X86_FEATURE_PKU) &&
11051 kvm_read_cr4_bits(vcpu, X86_CR4_PKE)) {
11052 vcpu->arch.pkru = __read_pkru();
11053 if (vcpu->arch.pkru != vmx->host_pkru)
11054 __write_pkru(vmx->host_pkru);
11057 kvm_put_guest_xcr0(vcpu);
11059 vmx->nested.nested_run_pending = 0;
11060 vmx->idt_vectoring_info = 0;
11062 vmx->exit_reason = vmx->fail ? 0xdead : vmcs_read32(VM_EXIT_REASON);
11063 if ((u16)vmx->exit_reason == EXIT_REASON_MCE_DURING_VMENTRY)
11064 kvm_machine_check();
11066 if (vmx->fail || (vmx->exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY))
11069 vmx->loaded_vmcs->launched = 1;
11070 vmx->idt_vectoring_info = vmcs_read32(IDT_VECTORING_INFO_FIELD);
11072 vmx_complete_atomic_exit(vmx);
11073 vmx_recover_nmi_blocking(vmx);
11074 vmx_complete_interrupts(vmx);
11076 STACK_FRAME_NON_STANDARD(vmx_vcpu_run);
11078 static struct kvm *vmx_vm_alloc(void)
11080 struct kvm_vmx *kvm_vmx = vzalloc(sizeof(struct kvm_vmx));
11085 return &kvm_vmx->kvm;
11088 static void vmx_vm_free(struct kvm *kvm)
11090 vfree(to_kvm_vmx(kvm));
11093 static void vmx_switch_vmcs(struct kvm_vcpu *vcpu, struct loaded_vmcs *vmcs)
11095 struct vcpu_vmx *vmx = to_vmx(vcpu);
11098 if (vmx->loaded_vmcs == vmcs)
11102 vmx_vcpu_put(vcpu);
11103 vmx->loaded_vmcs = vmcs;
11104 vmx_vcpu_load(vcpu, cpu);
11109 * Ensure that the current vmcs of the logical processor is the
11110 * vmcs01 of the vcpu before calling free_nested().
11112 static void vmx_free_vcpu_nested(struct kvm_vcpu *vcpu)
11114 struct vcpu_vmx *vmx = to_vmx(vcpu);
11117 vmx_switch_vmcs(vcpu, &vmx->vmcs01);
11122 static void vmx_free_vcpu(struct kvm_vcpu *vcpu)
11124 struct vcpu_vmx *vmx = to_vmx(vcpu);
11127 vmx_destroy_pml_buffer(vmx);
11128 free_vpid(vmx->vpid);
11129 leave_guest_mode(vcpu);
11130 vmx_free_vcpu_nested(vcpu);
11131 free_loaded_vmcs(vmx->loaded_vmcs);
11132 kfree(vmx->guest_msrs);
11133 kvm_vcpu_uninit(vcpu);
11134 kmem_cache_free(kvm_vcpu_cache, vmx);
11137 static struct kvm_vcpu *vmx_create_vcpu(struct kvm *kvm, unsigned int id)
11140 struct vcpu_vmx *vmx = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
11141 unsigned long *msr_bitmap;
11145 return ERR_PTR(-ENOMEM);
11147 vmx->vpid = allocate_vpid();
11149 err = kvm_vcpu_init(&vmx->vcpu, kvm, id);
11156 * If PML is turned on, failure on enabling PML just results in failure
11157 * of creating the vcpu, therefore we can simplify PML logic (by
11158 * avoiding dealing with cases, such as enabling PML partially on vcpus
11159 * for the guest, etc.
11162 vmx->pml_pg = alloc_page(GFP_KERNEL | __GFP_ZERO);
11167 vmx->guest_msrs = kmalloc(PAGE_SIZE, GFP_KERNEL);
11168 BUILD_BUG_ON(ARRAY_SIZE(vmx_msr_index) * sizeof(vmx->guest_msrs[0])
11171 if (!vmx->guest_msrs)
11174 err = alloc_loaded_vmcs(&vmx->vmcs01);
11178 msr_bitmap = vmx->vmcs01.msr_bitmap;
11179 vmx_disable_intercept_for_msr(msr_bitmap, MSR_FS_BASE, MSR_TYPE_RW);
11180 vmx_disable_intercept_for_msr(msr_bitmap, MSR_GS_BASE, MSR_TYPE_RW);
11181 vmx_disable_intercept_for_msr(msr_bitmap, MSR_KERNEL_GS_BASE, MSR_TYPE_RW);
11182 vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_SYSENTER_CS, MSR_TYPE_RW);
11183 vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_SYSENTER_ESP, MSR_TYPE_RW);
11184 vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_SYSENTER_EIP, MSR_TYPE_RW);
11185 vmx->msr_bitmap_mode = 0;
11187 vmx->loaded_vmcs = &vmx->vmcs01;
11189 vmx_vcpu_load(&vmx->vcpu, cpu);
11190 vmx->vcpu.cpu = cpu;
11191 vmx_vcpu_setup(vmx);
11192 vmx_vcpu_put(&vmx->vcpu);
11194 if (cpu_need_virtualize_apic_accesses(&vmx->vcpu)) {
11195 err = alloc_apic_access_page(kvm);
11200 if (enable_ept && !enable_unrestricted_guest) {
11201 err = init_rmode_identity_map(kvm);
11207 nested_vmx_setup_ctls_msrs(&vmx->nested.msrs,
11208 kvm_vcpu_apicv_active(&vmx->vcpu));
11210 vmx->nested.posted_intr_nv = -1;
11211 vmx->nested.current_vmptr = -1ull;
11213 vmx->msr_ia32_feature_control_valid_bits = FEATURE_CONTROL_LOCKED;
11216 * Enforce invariant: pi_desc.nv is always either POSTED_INTR_VECTOR
11217 * or POSTED_INTR_WAKEUP_VECTOR.
11219 vmx->pi_desc.nv = POSTED_INTR_VECTOR;
11220 vmx->pi_desc.sn = 1;
11225 free_loaded_vmcs(vmx->loaded_vmcs);
11227 kfree(vmx->guest_msrs);
11229 vmx_destroy_pml_buffer(vmx);
11231 kvm_vcpu_uninit(&vmx->vcpu);
11233 free_vpid(vmx->vpid);
11234 kmem_cache_free(kvm_vcpu_cache, vmx);
11235 return ERR_PTR(err);
11238 #define L1TF_MSG_SMT "L1TF CPU bug present and SMT on, data leak possible. See CVE-2018-3646 and https://www.kernel.org/doc/html/latest/admin-guide/hw-vuln/l1tf.html for details.\n"
11239 #define L1TF_MSG_L1D "L1TF CPU bug present and virtualization mitigation disabled, data leak possible. See CVE-2018-3646 and https://www.kernel.org/doc/html/latest/admin-guide/hw-vuln/l1tf.html for details.\n"
11241 static int vmx_vm_init(struct kvm *kvm)
11243 spin_lock_init(&to_kvm_vmx(kvm)->ept_pointer_lock);
11246 kvm->arch.pause_in_guest = true;
11248 if (boot_cpu_has(X86_BUG_L1TF) && enable_ept) {
11249 switch (l1tf_mitigation) {
11250 case L1TF_MITIGATION_OFF:
11251 case L1TF_MITIGATION_FLUSH_NOWARN:
11252 /* 'I explicitly don't care' is set */
11254 case L1TF_MITIGATION_FLUSH:
11255 case L1TF_MITIGATION_FLUSH_NOSMT:
11256 case L1TF_MITIGATION_FULL:
11258 * Warn upon starting the first VM in a potentially
11259 * insecure environment.
11261 if (sched_smt_active())
11262 pr_warn_once(L1TF_MSG_SMT);
11263 if (l1tf_vmx_mitigation == VMENTER_L1D_FLUSH_NEVER)
11264 pr_warn_once(L1TF_MSG_L1D);
11266 case L1TF_MITIGATION_FULL_FORCE:
11267 /* Flush is enforced */
11274 static void __init vmx_check_processor_compat(void *rtn)
11276 struct vmcs_config vmcs_conf;
11279 if (setup_vmcs_config(&vmcs_conf) < 0)
11280 *(int *)rtn = -EIO;
11281 nested_vmx_setup_ctls_msrs(&vmcs_conf.nested, enable_apicv);
11282 if (memcmp(&vmcs_config, &vmcs_conf, sizeof(struct vmcs_config)) != 0) {
11283 printk(KERN_ERR "kvm: CPU %d feature inconsistency!\n",
11284 smp_processor_id());
11285 *(int *)rtn = -EIO;
11289 static u64 vmx_get_mt_mask(struct kvm_vcpu *vcpu, gfn_t gfn, bool is_mmio)
11294 /* For VT-d and EPT combination
11295 * 1. MMIO: always map as UC
11296 * 2. EPT with VT-d:
11297 * a. VT-d without snooping control feature: can't guarantee the
11298 * result, try to trust guest.
11299 * b. VT-d with snooping control feature: snooping control feature of
11300 * VT-d engine can guarantee the cache correctness. Just set it
11301 * to WB to keep consistent with host. So the same as item 3.
11302 * 3. EPT without VT-d: always map as WB and set IPAT=1 to keep
11303 * consistent with host MTRR
11306 cache = MTRR_TYPE_UNCACHABLE;
11310 if (!kvm_arch_has_noncoherent_dma(vcpu->kvm)) {
11311 ipat = VMX_EPT_IPAT_BIT;
11312 cache = MTRR_TYPE_WRBACK;
11316 if (kvm_read_cr0(vcpu) & X86_CR0_CD) {
11317 ipat = VMX_EPT_IPAT_BIT;
11318 if (kvm_check_has_quirk(vcpu->kvm, KVM_X86_QUIRK_CD_NW_CLEARED))
11319 cache = MTRR_TYPE_WRBACK;
11321 cache = MTRR_TYPE_UNCACHABLE;
11325 cache = kvm_mtrr_get_guest_memory_type(vcpu, gfn);
11328 return (cache << VMX_EPT_MT_EPTE_SHIFT) | ipat;
11331 static int vmx_get_lpage_level(void)
11333 if (enable_ept && !cpu_has_vmx_ept_1g_page())
11334 return PT_DIRECTORY_LEVEL;
11336 /* For shadow and EPT supported 1GB page */
11337 return PT_PDPE_LEVEL;
11340 static void vmcs_set_secondary_exec_control(u32 new_ctl)
11343 * These bits in the secondary execution controls field
11344 * are dynamic, the others are mostly based on the hypervisor
11345 * architecture and the guest's CPUID. Do not touch the
11349 SECONDARY_EXEC_SHADOW_VMCS |
11350 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
11351 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
11352 SECONDARY_EXEC_DESC;
11354 u32 cur_ctl = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
11356 vmcs_write32(SECONDARY_VM_EXEC_CONTROL,
11357 (new_ctl & ~mask) | (cur_ctl & mask));
11361 * Generate MSR_IA32_VMX_CR{0,4}_FIXED1 according to CPUID. Only set bits
11362 * (indicating "allowed-1") if they are supported in the guest's CPUID.
11364 static void nested_vmx_cr_fixed1_bits_update(struct kvm_vcpu *vcpu)
11366 struct vcpu_vmx *vmx = to_vmx(vcpu);
11367 struct kvm_cpuid_entry2 *entry;
11369 vmx->nested.msrs.cr0_fixed1 = 0xffffffff;
11370 vmx->nested.msrs.cr4_fixed1 = X86_CR4_PCE;
11372 #define cr4_fixed1_update(_cr4_mask, _reg, _cpuid_mask) do { \
11373 if (entry && (entry->_reg & (_cpuid_mask))) \
11374 vmx->nested.msrs.cr4_fixed1 |= (_cr4_mask); \
11377 entry = kvm_find_cpuid_entry(vcpu, 0x1, 0);
11378 cr4_fixed1_update(X86_CR4_VME, edx, bit(X86_FEATURE_VME));
11379 cr4_fixed1_update(X86_CR4_PVI, edx, bit(X86_FEATURE_VME));
11380 cr4_fixed1_update(X86_CR4_TSD, edx, bit(X86_FEATURE_TSC));
11381 cr4_fixed1_update(X86_CR4_DE, edx, bit(X86_FEATURE_DE));
11382 cr4_fixed1_update(X86_CR4_PSE, edx, bit(X86_FEATURE_PSE));
11383 cr4_fixed1_update(X86_CR4_PAE, edx, bit(X86_FEATURE_PAE));
11384 cr4_fixed1_update(X86_CR4_MCE, edx, bit(X86_FEATURE_MCE));
11385 cr4_fixed1_update(X86_CR4_PGE, edx, bit(X86_FEATURE_PGE));
11386 cr4_fixed1_update(X86_CR4_OSFXSR, edx, bit(X86_FEATURE_FXSR));
11387 cr4_fixed1_update(X86_CR4_OSXMMEXCPT, edx, bit(X86_FEATURE_XMM));
11388 cr4_fixed1_update(X86_CR4_VMXE, ecx, bit(X86_FEATURE_VMX));
11389 cr4_fixed1_update(X86_CR4_SMXE, ecx, bit(X86_FEATURE_SMX));
11390 cr4_fixed1_update(X86_CR4_PCIDE, ecx, bit(X86_FEATURE_PCID));
11391 cr4_fixed1_update(X86_CR4_OSXSAVE, ecx, bit(X86_FEATURE_XSAVE));
11393 entry = kvm_find_cpuid_entry(vcpu, 0x7, 0);
11394 cr4_fixed1_update(X86_CR4_FSGSBASE, ebx, bit(X86_FEATURE_FSGSBASE));
11395 cr4_fixed1_update(X86_CR4_SMEP, ebx, bit(X86_FEATURE_SMEP));
11396 cr4_fixed1_update(X86_CR4_SMAP, ebx, bit(X86_FEATURE_SMAP));
11397 cr4_fixed1_update(X86_CR4_PKE, ecx, bit(X86_FEATURE_PKU));
11398 cr4_fixed1_update(X86_CR4_UMIP, ecx, bit(X86_FEATURE_UMIP));
11400 #undef cr4_fixed1_update
11403 static void nested_vmx_entry_exit_ctls_update(struct kvm_vcpu *vcpu)
11405 struct vcpu_vmx *vmx = to_vmx(vcpu);
11407 if (kvm_mpx_supported()) {
11408 bool mpx_enabled = guest_cpuid_has(vcpu, X86_FEATURE_MPX);
11411 vmx->nested.msrs.entry_ctls_high |= VM_ENTRY_LOAD_BNDCFGS;
11412 vmx->nested.msrs.exit_ctls_high |= VM_EXIT_CLEAR_BNDCFGS;
11414 vmx->nested.msrs.entry_ctls_high &= ~VM_ENTRY_LOAD_BNDCFGS;
11415 vmx->nested.msrs.exit_ctls_high &= ~VM_EXIT_CLEAR_BNDCFGS;
11420 static void vmx_cpuid_update(struct kvm_vcpu *vcpu)
11422 struct vcpu_vmx *vmx = to_vmx(vcpu);
11424 if (cpu_has_secondary_exec_ctrls()) {
11425 vmx_compute_secondary_exec_control(vmx);
11426 vmcs_set_secondary_exec_control(vmx->secondary_exec_control);
11429 if (nested_vmx_allowed(vcpu))
11430 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits |=
11431 FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
11433 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits &=
11434 ~FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
11436 if (nested_vmx_allowed(vcpu)) {
11437 nested_vmx_cr_fixed1_bits_update(vcpu);
11438 nested_vmx_entry_exit_ctls_update(vcpu);
11442 static void vmx_set_supported_cpuid(u32 func, struct kvm_cpuid_entry2 *entry)
11444 if (func == 1 && nested)
11445 entry->ecx |= bit(X86_FEATURE_VMX);
11448 static void nested_ept_inject_page_fault(struct kvm_vcpu *vcpu,
11449 struct x86_exception *fault)
11451 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
11452 struct vcpu_vmx *vmx = to_vmx(vcpu);
11454 unsigned long exit_qualification = vcpu->arch.exit_qualification;
11456 if (vmx->nested.pml_full) {
11457 exit_reason = EXIT_REASON_PML_FULL;
11458 vmx->nested.pml_full = false;
11459 exit_qualification &= INTR_INFO_UNBLOCK_NMI;
11460 } else if (fault->error_code & PFERR_RSVD_MASK)
11461 exit_reason = EXIT_REASON_EPT_MISCONFIG;
11463 exit_reason = EXIT_REASON_EPT_VIOLATION;
11465 nested_vmx_vmexit(vcpu, exit_reason, 0, exit_qualification);
11466 vmcs12->guest_physical_address = fault->address;
11469 static bool nested_ept_ad_enabled(struct kvm_vcpu *vcpu)
11471 return nested_ept_get_cr3(vcpu) & VMX_EPTP_AD_ENABLE_BIT;
11474 /* Callbacks for nested_ept_init_mmu_context: */
11476 static unsigned long nested_ept_get_cr3(struct kvm_vcpu *vcpu)
11478 /* return the page table to be shadowed - in our case, EPT12 */
11479 return get_vmcs12(vcpu)->ept_pointer;
11482 static int nested_ept_init_mmu_context(struct kvm_vcpu *vcpu)
11484 WARN_ON(mmu_is_nested(vcpu));
11485 if (!valid_ept_address(vcpu, nested_ept_get_cr3(vcpu)))
11488 kvm_init_shadow_ept_mmu(vcpu,
11489 to_vmx(vcpu)->nested.msrs.ept_caps &
11490 VMX_EPT_EXECUTE_ONLY_BIT,
11491 nested_ept_ad_enabled(vcpu),
11492 nested_ept_get_cr3(vcpu));
11493 vcpu->arch.mmu.set_cr3 = vmx_set_cr3;
11494 vcpu->arch.mmu.get_cr3 = nested_ept_get_cr3;
11495 vcpu->arch.mmu.inject_page_fault = nested_ept_inject_page_fault;
11497 vcpu->arch.walk_mmu = &vcpu->arch.nested_mmu;
11501 static void nested_ept_uninit_mmu_context(struct kvm_vcpu *vcpu)
11503 vcpu->arch.walk_mmu = &vcpu->arch.mmu;
11506 static bool nested_vmx_is_page_fault_vmexit(struct vmcs12 *vmcs12,
11509 bool inequality, bit;
11511 bit = (vmcs12->exception_bitmap & (1u << PF_VECTOR)) != 0;
11513 (error_code & vmcs12->page_fault_error_code_mask) !=
11514 vmcs12->page_fault_error_code_match;
11515 return inequality ^ bit;
11518 static void vmx_inject_page_fault_nested(struct kvm_vcpu *vcpu,
11519 struct x86_exception *fault)
11521 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
11523 WARN_ON(!is_guest_mode(vcpu));
11525 if (nested_vmx_is_page_fault_vmexit(vmcs12, fault->error_code) &&
11526 !to_vmx(vcpu)->nested.nested_run_pending) {
11527 vmcs12->vm_exit_intr_error_code = fault->error_code;
11528 nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI,
11529 PF_VECTOR | INTR_TYPE_HARD_EXCEPTION |
11530 INTR_INFO_DELIVER_CODE_MASK | INTR_INFO_VALID_MASK,
11533 kvm_inject_page_fault(vcpu, fault);
11537 static inline bool nested_vmx_prepare_msr_bitmap(struct kvm_vcpu *vcpu,
11538 struct vmcs12 *vmcs12);
11540 static void nested_get_vmcs12_pages(struct kvm_vcpu *vcpu)
11542 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
11543 struct vcpu_vmx *vmx = to_vmx(vcpu);
11547 if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) {
11549 * Translate L1 physical address to host physical
11550 * address for vmcs02. Keep the page pinned, so this
11551 * physical address remains valid. We keep a reference
11552 * to it so we can release it later.
11554 if (vmx->nested.apic_access_page) { /* shouldn't happen */
11555 kvm_release_page_dirty(vmx->nested.apic_access_page);
11556 vmx->nested.apic_access_page = NULL;
11558 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->apic_access_addr);
11560 * If translation failed, no matter: This feature asks
11561 * to exit when accessing the given address, and if it
11562 * can never be accessed, this feature won't do
11565 if (!is_error_page(page)) {
11566 vmx->nested.apic_access_page = page;
11567 hpa = page_to_phys(vmx->nested.apic_access_page);
11568 vmcs_write64(APIC_ACCESS_ADDR, hpa);
11570 vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL,
11571 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES);
11575 if (nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW)) {
11576 if (vmx->nested.virtual_apic_page) { /* shouldn't happen */
11577 kvm_release_page_dirty(vmx->nested.virtual_apic_page);
11578 vmx->nested.virtual_apic_page = NULL;
11580 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->virtual_apic_page_addr);
11583 * If translation failed, VM entry will fail because
11584 * prepare_vmcs02 set VIRTUAL_APIC_PAGE_ADDR to -1ull.
11585 * Failing the vm entry is _not_ what the processor
11586 * does but it's basically the only possibility we
11587 * have. We could still enter the guest if CR8 load
11588 * exits are enabled, CR8 store exits are enabled, and
11589 * virtualize APIC access is disabled; in this case
11590 * the processor would never use the TPR shadow and we
11591 * could simply clear the bit from the execution
11592 * control. But such a configuration is useless, so
11593 * let's keep the code simple.
11595 if (!is_error_page(page)) {
11596 vmx->nested.virtual_apic_page = page;
11597 hpa = page_to_phys(vmx->nested.virtual_apic_page);
11598 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, hpa);
11602 if (nested_cpu_has_posted_intr(vmcs12)) {
11603 if (vmx->nested.pi_desc_page) { /* shouldn't happen */
11604 kunmap(vmx->nested.pi_desc_page);
11605 kvm_release_page_dirty(vmx->nested.pi_desc_page);
11606 vmx->nested.pi_desc_page = NULL;
11607 vmx->nested.pi_desc = NULL;
11608 vmcs_write64(POSTED_INTR_DESC_ADDR, -1ull);
11610 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->posted_intr_desc_addr);
11611 if (is_error_page(page))
11613 vmx->nested.pi_desc_page = page;
11614 vmx->nested.pi_desc = kmap(vmx->nested.pi_desc_page);
11615 vmx->nested.pi_desc =
11616 (struct pi_desc *)((void *)vmx->nested.pi_desc +
11617 (unsigned long)(vmcs12->posted_intr_desc_addr &
11619 vmcs_write64(POSTED_INTR_DESC_ADDR,
11620 page_to_phys(vmx->nested.pi_desc_page) +
11621 (unsigned long)(vmcs12->posted_intr_desc_addr &
11624 if (nested_vmx_prepare_msr_bitmap(vcpu, vmcs12))
11625 vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL,
11626 CPU_BASED_USE_MSR_BITMAPS);
11628 vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL,
11629 CPU_BASED_USE_MSR_BITMAPS);
11632 static void vmx_start_preemption_timer(struct kvm_vcpu *vcpu)
11634 u64 preemption_timeout = get_vmcs12(vcpu)->vmx_preemption_timer_value;
11635 struct vcpu_vmx *vmx = to_vmx(vcpu);
11638 * A timer value of zero is architecturally guaranteed to cause
11639 * a VMExit prior to executing any instructions in the guest.
11641 if (preemption_timeout == 0) {
11642 vmx_preemption_timer_fn(&vmx->nested.preemption_timer);
11646 if (vcpu->arch.virtual_tsc_khz == 0)
11649 preemption_timeout <<= VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE;
11650 preemption_timeout *= 1000000;
11651 do_div(preemption_timeout, vcpu->arch.virtual_tsc_khz);
11652 hrtimer_start(&vmx->nested.preemption_timer,
11653 ns_to_ktime(preemption_timeout), HRTIMER_MODE_REL);
11656 static int nested_vmx_check_io_bitmap_controls(struct kvm_vcpu *vcpu,
11657 struct vmcs12 *vmcs12)
11659 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_IO_BITMAPS))
11662 if (!page_address_valid(vcpu, vmcs12->io_bitmap_a) ||
11663 !page_address_valid(vcpu, vmcs12->io_bitmap_b))
11669 static int nested_vmx_check_msr_bitmap_controls(struct kvm_vcpu *vcpu,
11670 struct vmcs12 *vmcs12)
11672 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS))
11675 if (!page_address_valid(vcpu, vmcs12->msr_bitmap))
11681 static int nested_vmx_check_tpr_shadow_controls(struct kvm_vcpu *vcpu,
11682 struct vmcs12 *vmcs12)
11684 if (!nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW))
11687 if (!page_address_valid(vcpu, vmcs12->virtual_apic_page_addr))
11693 static inline void enable_x2apic_msr_intercepts(unsigned long *msr_bitmap) {
11696 for (msr = 0x800; msr <= 0x8ff; msr += BITS_PER_LONG) {
11697 unsigned word = msr / BITS_PER_LONG;
11699 msr_bitmap[word] = ~0;
11700 msr_bitmap[word + (0x800 / sizeof(long))] = ~0;
11705 * Merge L0's and L1's MSR bitmap, return false to indicate that
11706 * we do not use the hardware.
11708 static inline bool nested_vmx_prepare_msr_bitmap(struct kvm_vcpu *vcpu,
11709 struct vmcs12 *vmcs12)
11713 unsigned long *msr_bitmap_l1;
11714 unsigned long *msr_bitmap_l0 = to_vmx(vcpu)->nested.vmcs02.msr_bitmap;
11716 * pred_cmd & spec_ctrl are trying to verify two things:
11718 * 1. L0 gave a permission to L1 to actually passthrough the MSR. This
11719 * ensures that we do not accidentally generate an L02 MSR bitmap
11720 * from the L12 MSR bitmap that is too permissive.
11721 * 2. That L1 or L2s have actually used the MSR. This avoids
11722 * unnecessarily merging of the bitmap if the MSR is unused. This
11723 * works properly because we only update the L01 MSR bitmap lazily.
11724 * So even if L0 should pass L1 these MSRs, the L01 bitmap is only
11725 * updated to reflect this when L1 (or its L2s) actually write to
11728 bool pred_cmd = !msr_write_intercepted_l01(vcpu, MSR_IA32_PRED_CMD);
11729 bool spec_ctrl = !msr_write_intercepted_l01(vcpu, MSR_IA32_SPEC_CTRL);
11731 /* Nothing to do if the MSR bitmap is not in use. */
11732 if (!cpu_has_vmx_msr_bitmap() ||
11733 !nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS))
11736 if (!nested_cpu_has_virt_x2apic_mode(vmcs12) &&
11737 !pred_cmd && !spec_ctrl)
11740 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->msr_bitmap);
11741 if (is_error_page(page))
11744 msr_bitmap_l1 = (unsigned long *)kmap(page);
11747 * To keep the control flow simple, pay eight 8-byte writes (sixteen
11748 * 4-byte writes on 32-bit systems) up front to enable intercepts for
11749 * the x2APIC MSR range and selectively disable them below.
11751 enable_x2apic_msr_intercepts(msr_bitmap_l0);
11753 if (nested_cpu_has_virt_x2apic_mode(vmcs12)) {
11754 if (nested_cpu_has_apic_reg_virt(vmcs12)) {
11756 * L0 need not intercept reads for MSRs between 0x800
11757 * and 0x8ff, it just lets the processor take the value
11758 * from the virtual-APIC page; take those 256 bits
11759 * directly from the L1 bitmap.
11761 for (msr = 0x800; msr <= 0x8ff; msr += BITS_PER_LONG) {
11762 unsigned word = msr / BITS_PER_LONG;
11764 msr_bitmap_l0[word] = msr_bitmap_l1[word];
11768 nested_vmx_disable_intercept_for_msr(
11769 msr_bitmap_l1, msr_bitmap_l0,
11770 X2APIC_MSR(APIC_TASKPRI),
11771 MSR_TYPE_R | MSR_TYPE_W);
11773 if (nested_cpu_has_vid(vmcs12)) {
11774 nested_vmx_disable_intercept_for_msr(
11775 msr_bitmap_l1, msr_bitmap_l0,
11776 X2APIC_MSR(APIC_EOI),
11778 nested_vmx_disable_intercept_for_msr(
11779 msr_bitmap_l1, msr_bitmap_l0,
11780 X2APIC_MSR(APIC_SELF_IPI),
11786 nested_vmx_disable_intercept_for_msr(
11787 msr_bitmap_l1, msr_bitmap_l0,
11788 MSR_IA32_SPEC_CTRL,
11789 MSR_TYPE_R | MSR_TYPE_W);
11792 nested_vmx_disable_intercept_for_msr(
11793 msr_bitmap_l1, msr_bitmap_l0,
11798 kvm_release_page_clean(page);
11803 static void nested_cache_shadow_vmcs12(struct kvm_vcpu *vcpu,
11804 struct vmcs12 *vmcs12)
11806 struct vmcs12 *shadow;
11809 if (!nested_cpu_has_shadow_vmcs(vmcs12) ||
11810 vmcs12->vmcs_link_pointer == -1ull)
11813 shadow = get_shadow_vmcs12(vcpu);
11814 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->vmcs_link_pointer);
11816 memcpy(shadow, kmap(page), VMCS12_SIZE);
11819 kvm_release_page_clean(page);
11822 static void nested_flush_cached_shadow_vmcs12(struct kvm_vcpu *vcpu,
11823 struct vmcs12 *vmcs12)
11825 struct vcpu_vmx *vmx = to_vmx(vcpu);
11827 if (!nested_cpu_has_shadow_vmcs(vmcs12) ||
11828 vmcs12->vmcs_link_pointer == -1ull)
11831 kvm_write_guest(vmx->vcpu.kvm, vmcs12->vmcs_link_pointer,
11832 get_shadow_vmcs12(vcpu), VMCS12_SIZE);
11835 static int nested_vmx_check_apic_access_controls(struct kvm_vcpu *vcpu,
11836 struct vmcs12 *vmcs12)
11838 if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES) &&
11839 !page_address_valid(vcpu, vmcs12->apic_access_addr))
11845 static int nested_vmx_check_apicv_controls(struct kvm_vcpu *vcpu,
11846 struct vmcs12 *vmcs12)
11848 if (!nested_cpu_has_virt_x2apic_mode(vmcs12) &&
11849 !nested_cpu_has_apic_reg_virt(vmcs12) &&
11850 !nested_cpu_has_vid(vmcs12) &&
11851 !nested_cpu_has_posted_intr(vmcs12))
11855 * If virtualize x2apic mode is enabled,
11856 * virtualize apic access must be disabled.
11858 if (nested_cpu_has_virt_x2apic_mode(vmcs12) &&
11859 nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES))
11863 * If virtual interrupt delivery is enabled,
11864 * we must exit on external interrupts.
11866 if (nested_cpu_has_vid(vmcs12) &&
11867 !nested_exit_on_intr(vcpu))
11871 * bits 15:8 should be zero in posted_intr_nv,
11872 * the descriptor address has been already checked
11873 * in nested_get_vmcs12_pages.
11875 * bits 5:0 of posted_intr_desc_addr should be zero.
11877 if (nested_cpu_has_posted_intr(vmcs12) &&
11878 (!nested_cpu_has_vid(vmcs12) ||
11879 !nested_exit_intr_ack_set(vcpu) ||
11880 (vmcs12->posted_intr_nv & 0xff00) ||
11881 (vmcs12->posted_intr_desc_addr & 0x3f) ||
11882 (vmcs12->posted_intr_desc_addr >> cpuid_maxphyaddr(vcpu))))
11885 /* tpr shadow is needed by all apicv features. */
11886 if (!nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW))
11892 static int nested_vmx_check_msr_switch(struct kvm_vcpu *vcpu,
11893 unsigned long count_field,
11894 unsigned long addr_field)
11896 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
11900 if (vmcs12_read_any(vmcs12, count_field, &count) ||
11901 vmcs12_read_any(vmcs12, addr_field, &addr)) {
11907 maxphyaddr = cpuid_maxphyaddr(vcpu);
11908 if (!IS_ALIGNED(addr, 16) || addr >> maxphyaddr ||
11909 (addr + count * sizeof(struct vmx_msr_entry) - 1) >> maxphyaddr) {
11910 pr_debug_ratelimited(
11911 "nVMX: invalid MSR switch (0x%lx, %d, %llu, 0x%08llx)",
11912 addr_field, maxphyaddr, count, addr);
11918 static int nested_vmx_check_msr_switch_controls(struct kvm_vcpu *vcpu,
11919 struct vmcs12 *vmcs12)
11921 if (vmcs12->vm_exit_msr_load_count == 0 &&
11922 vmcs12->vm_exit_msr_store_count == 0 &&
11923 vmcs12->vm_entry_msr_load_count == 0)
11924 return 0; /* Fast path */
11925 if (nested_vmx_check_msr_switch(vcpu, VM_EXIT_MSR_LOAD_COUNT,
11926 VM_EXIT_MSR_LOAD_ADDR) ||
11927 nested_vmx_check_msr_switch(vcpu, VM_EXIT_MSR_STORE_COUNT,
11928 VM_EXIT_MSR_STORE_ADDR) ||
11929 nested_vmx_check_msr_switch(vcpu, VM_ENTRY_MSR_LOAD_COUNT,
11930 VM_ENTRY_MSR_LOAD_ADDR))
11935 static int nested_vmx_check_pml_controls(struct kvm_vcpu *vcpu,
11936 struct vmcs12 *vmcs12)
11938 u64 address = vmcs12->pml_address;
11939 int maxphyaddr = cpuid_maxphyaddr(vcpu);
11941 if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_PML)) {
11942 if (!nested_cpu_has_ept(vmcs12) ||
11943 !IS_ALIGNED(address, 4096) ||
11944 address >> maxphyaddr)
11951 static int nested_vmx_check_shadow_vmcs_controls(struct kvm_vcpu *vcpu,
11952 struct vmcs12 *vmcs12)
11954 if (!nested_cpu_has_shadow_vmcs(vmcs12))
11957 if (!page_address_valid(vcpu, vmcs12->vmread_bitmap) ||
11958 !page_address_valid(vcpu, vmcs12->vmwrite_bitmap))
11964 static int nested_vmx_msr_check_common(struct kvm_vcpu *vcpu,
11965 struct vmx_msr_entry *e)
11967 /* x2APIC MSR accesses are not allowed */
11968 if (vcpu->arch.apic_base & X2APIC_ENABLE && e->index >> 8 == 0x8)
11970 if (e->index == MSR_IA32_UCODE_WRITE || /* SDM Table 35-2 */
11971 e->index == MSR_IA32_UCODE_REV)
11973 if (e->reserved != 0)
11978 static int nested_vmx_load_msr_check(struct kvm_vcpu *vcpu,
11979 struct vmx_msr_entry *e)
11981 if (e->index == MSR_FS_BASE ||
11982 e->index == MSR_GS_BASE ||
11983 e->index == MSR_IA32_SMM_MONITOR_CTL || /* SMM is not supported */
11984 nested_vmx_msr_check_common(vcpu, e))
11989 static int nested_vmx_store_msr_check(struct kvm_vcpu *vcpu,
11990 struct vmx_msr_entry *e)
11992 if (e->index == MSR_IA32_SMBASE || /* SMM is not supported */
11993 nested_vmx_msr_check_common(vcpu, e))
11999 * Load guest's/host's msr at nested entry/exit.
12000 * return 0 for success, entry index for failure.
12002 static u32 nested_vmx_load_msr(struct kvm_vcpu *vcpu, u64 gpa, u32 count)
12005 struct vmx_msr_entry e;
12006 struct msr_data msr;
12008 msr.host_initiated = false;
12009 for (i = 0; i < count; i++) {
12010 if (kvm_vcpu_read_guest(vcpu, gpa + i * sizeof(e),
12012 pr_debug_ratelimited(
12013 "%s cannot read MSR entry (%u, 0x%08llx)\n",
12014 __func__, i, gpa + i * sizeof(e));
12017 if (nested_vmx_load_msr_check(vcpu, &e)) {
12018 pr_debug_ratelimited(
12019 "%s check failed (%u, 0x%x, 0x%x)\n",
12020 __func__, i, e.index, e.reserved);
12023 msr.index = e.index;
12024 msr.data = e.value;
12025 if (kvm_set_msr(vcpu, &msr)) {
12026 pr_debug_ratelimited(
12027 "%s cannot write MSR (%u, 0x%x, 0x%llx)\n",
12028 __func__, i, e.index, e.value);
12037 static int nested_vmx_store_msr(struct kvm_vcpu *vcpu, u64 gpa, u32 count)
12040 struct vmx_msr_entry e;
12042 for (i = 0; i < count; i++) {
12043 struct msr_data msr_info;
12044 if (kvm_vcpu_read_guest(vcpu,
12045 gpa + i * sizeof(e),
12046 &e, 2 * sizeof(u32))) {
12047 pr_debug_ratelimited(
12048 "%s cannot read MSR entry (%u, 0x%08llx)\n",
12049 __func__, i, gpa + i * sizeof(e));
12052 if (nested_vmx_store_msr_check(vcpu, &e)) {
12053 pr_debug_ratelimited(
12054 "%s check failed (%u, 0x%x, 0x%x)\n",
12055 __func__, i, e.index, e.reserved);
12058 msr_info.host_initiated = false;
12059 msr_info.index = e.index;
12060 if (kvm_get_msr(vcpu, &msr_info)) {
12061 pr_debug_ratelimited(
12062 "%s cannot read MSR (%u, 0x%x)\n",
12063 __func__, i, e.index);
12066 if (kvm_vcpu_write_guest(vcpu,
12067 gpa + i * sizeof(e) +
12068 offsetof(struct vmx_msr_entry, value),
12069 &msr_info.data, sizeof(msr_info.data))) {
12070 pr_debug_ratelimited(
12071 "%s cannot write MSR (%u, 0x%x, 0x%llx)\n",
12072 __func__, i, e.index, msr_info.data);
12079 static bool nested_cr3_valid(struct kvm_vcpu *vcpu, unsigned long val)
12081 unsigned long invalid_mask;
12083 invalid_mask = (~0ULL) << cpuid_maxphyaddr(vcpu);
12084 return (val & invalid_mask) == 0;
12088 * Load guest's/host's cr3 at nested entry/exit. nested_ept is true if we are
12089 * emulating VM entry into a guest with EPT enabled.
12090 * Returns 0 on success, 1 on failure. Invalid state exit qualification code
12091 * is assigned to entry_failure_code on failure.
12093 static int nested_vmx_load_cr3(struct kvm_vcpu *vcpu, unsigned long cr3, bool nested_ept,
12094 u32 *entry_failure_code)
12096 if (cr3 != kvm_read_cr3(vcpu) || (!nested_ept && pdptrs_changed(vcpu))) {
12097 if (!nested_cr3_valid(vcpu, cr3)) {
12098 *entry_failure_code = ENTRY_FAIL_DEFAULT;
12103 * If PAE paging and EPT are both on, CR3 is not used by the CPU and
12104 * must not be dereferenced.
12106 if (is_pae_paging(vcpu) && !nested_ept) {
12107 if (!load_pdptrs(vcpu, vcpu->arch.walk_mmu, cr3)) {
12108 *entry_failure_code = ENTRY_FAIL_PDPTE;
12115 kvm_mmu_new_cr3(vcpu, cr3, false);
12117 vcpu->arch.cr3 = cr3;
12118 __set_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail);
12120 kvm_init_mmu(vcpu, false);
12125 static void prepare_vmcs02_full(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
12127 struct vcpu_vmx *vmx = to_vmx(vcpu);
12129 vmcs_write16(GUEST_ES_SELECTOR, vmcs12->guest_es_selector);
12130 vmcs_write16(GUEST_SS_SELECTOR, vmcs12->guest_ss_selector);
12131 vmcs_write16(GUEST_DS_SELECTOR, vmcs12->guest_ds_selector);
12132 vmcs_write16(GUEST_FS_SELECTOR, vmcs12->guest_fs_selector);
12133 vmcs_write16(GUEST_GS_SELECTOR, vmcs12->guest_gs_selector);
12134 vmcs_write16(GUEST_LDTR_SELECTOR, vmcs12->guest_ldtr_selector);
12135 vmcs_write16(GUEST_TR_SELECTOR, vmcs12->guest_tr_selector);
12136 vmcs_write32(GUEST_ES_LIMIT, vmcs12->guest_es_limit);
12137 vmcs_write32(GUEST_SS_LIMIT, vmcs12->guest_ss_limit);
12138 vmcs_write32(GUEST_DS_LIMIT, vmcs12->guest_ds_limit);
12139 vmcs_write32(GUEST_FS_LIMIT, vmcs12->guest_fs_limit);
12140 vmcs_write32(GUEST_GS_LIMIT, vmcs12->guest_gs_limit);
12141 vmcs_write32(GUEST_LDTR_LIMIT, vmcs12->guest_ldtr_limit);
12142 vmcs_write32(GUEST_TR_LIMIT, vmcs12->guest_tr_limit);
12143 vmcs_write32(GUEST_GDTR_LIMIT, vmcs12->guest_gdtr_limit);
12144 vmcs_write32(GUEST_IDTR_LIMIT, vmcs12->guest_idtr_limit);
12145 vmcs_write32(GUEST_ES_AR_BYTES, vmcs12->guest_es_ar_bytes);
12146 vmcs_write32(GUEST_SS_AR_BYTES, vmcs12->guest_ss_ar_bytes);
12147 vmcs_write32(GUEST_DS_AR_BYTES, vmcs12->guest_ds_ar_bytes);
12148 vmcs_write32(GUEST_FS_AR_BYTES, vmcs12->guest_fs_ar_bytes);
12149 vmcs_write32(GUEST_GS_AR_BYTES, vmcs12->guest_gs_ar_bytes);
12150 vmcs_write32(GUEST_LDTR_AR_BYTES, vmcs12->guest_ldtr_ar_bytes);
12151 vmcs_write32(GUEST_TR_AR_BYTES, vmcs12->guest_tr_ar_bytes);
12152 vmcs_writel(GUEST_SS_BASE, vmcs12->guest_ss_base);
12153 vmcs_writel(GUEST_DS_BASE, vmcs12->guest_ds_base);
12154 vmcs_writel(GUEST_FS_BASE, vmcs12->guest_fs_base);
12155 vmcs_writel(GUEST_GS_BASE, vmcs12->guest_gs_base);
12156 vmcs_writel(GUEST_LDTR_BASE, vmcs12->guest_ldtr_base);
12157 vmcs_writel(GUEST_TR_BASE, vmcs12->guest_tr_base);
12158 vmcs_writel(GUEST_GDTR_BASE, vmcs12->guest_gdtr_base);
12159 vmcs_writel(GUEST_IDTR_BASE, vmcs12->guest_idtr_base);
12161 vmcs_write32(GUEST_SYSENTER_CS, vmcs12->guest_sysenter_cs);
12162 vmcs_writel(GUEST_PENDING_DBG_EXCEPTIONS,
12163 vmcs12->guest_pending_dbg_exceptions);
12164 vmcs_writel(GUEST_SYSENTER_ESP, vmcs12->guest_sysenter_esp);
12165 vmcs_writel(GUEST_SYSENTER_EIP, vmcs12->guest_sysenter_eip);
12167 if (nested_cpu_has_xsaves(vmcs12))
12168 vmcs_write64(XSS_EXIT_BITMAP, vmcs12->xss_exit_bitmap);
12169 vmcs_write64(VMCS_LINK_POINTER, -1ull);
12171 if (cpu_has_vmx_posted_intr())
12172 vmcs_write16(POSTED_INTR_NV, POSTED_INTR_NESTED_VECTOR);
12175 * Whether page-faults are trapped is determined by a combination of
12176 * 3 settings: PFEC_MASK, PFEC_MATCH and EXCEPTION_BITMAP.PF.
12177 * If enable_ept, L0 doesn't care about page faults and we should
12178 * set all of these to L1's desires. However, if !enable_ept, L0 does
12179 * care about (at least some) page faults, and because it is not easy
12180 * (if at all possible?) to merge L0 and L1's desires, we simply ask
12181 * to exit on each and every L2 page fault. This is done by setting
12182 * MASK=MATCH=0 and (see below) EB.PF=1.
12183 * Note that below we don't need special code to set EB.PF beyond the
12184 * "or"ing of the EB of vmcs01 and vmcs12, because when enable_ept,
12185 * vmcs01's EB.PF is 0 so the "or" will take vmcs12's value, and when
12186 * !enable_ept, EB.PF is 1, so the "or" will always be 1.
12188 vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK,
12189 enable_ept ? vmcs12->page_fault_error_code_mask : 0);
12190 vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH,
12191 enable_ept ? vmcs12->page_fault_error_code_match : 0);
12193 /* All VMFUNCs are currently emulated through L0 vmexits. */
12194 if (cpu_has_vmx_vmfunc())
12195 vmcs_write64(VM_FUNCTION_CONTROL, 0);
12197 if (cpu_has_vmx_apicv()) {
12198 vmcs_write64(EOI_EXIT_BITMAP0, vmcs12->eoi_exit_bitmap0);
12199 vmcs_write64(EOI_EXIT_BITMAP1, vmcs12->eoi_exit_bitmap1);
12200 vmcs_write64(EOI_EXIT_BITMAP2, vmcs12->eoi_exit_bitmap2);
12201 vmcs_write64(EOI_EXIT_BITMAP3, vmcs12->eoi_exit_bitmap3);
12205 * Set host-state according to L0's settings (vmcs12 is irrelevant here)
12206 * Some constant fields are set here by vmx_set_constant_host_state().
12207 * Other fields are different per CPU, and will be set later when
12208 * vmx_vcpu_load() is called, and when vmx_prepare_switch_to_guest()
12211 vmx_set_constant_host_state(vmx);
12214 * Set the MSR load/store lists to match L0's settings.
12216 vmcs_write32(VM_EXIT_MSR_STORE_COUNT, 0);
12217 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, vmx->msr_autoload.host.nr);
12218 vmcs_write64(VM_EXIT_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.host.val));
12219 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, vmx->msr_autoload.guest.nr);
12220 vmcs_write64(VM_ENTRY_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.guest.val));
12222 set_cr4_guest_host_mask(vmx);
12224 if (kvm_mpx_supported() && vmx->nested.nested_run_pending &&
12225 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_BNDCFGS))
12226 vmcs_write64(GUEST_BNDCFGS, vmcs12->guest_bndcfgs);
12229 if (nested_cpu_has_vpid(vmcs12) && vmx->nested.vpid02)
12230 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->nested.vpid02);
12232 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid);
12236 * L1 may access the L2's PDPTR, so save them to construct vmcs12
12239 vmcs_write64(GUEST_PDPTR0, vmcs12->guest_pdptr0);
12240 vmcs_write64(GUEST_PDPTR1, vmcs12->guest_pdptr1);
12241 vmcs_write64(GUEST_PDPTR2, vmcs12->guest_pdptr2);
12242 vmcs_write64(GUEST_PDPTR3, vmcs12->guest_pdptr3);
12245 if (cpu_has_vmx_msr_bitmap())
12246 vmcs_write64(MSR_BITMAP, __pa(vmx->nested.vmcs02.msr_bitmap));
12250 * prepare_vmcs02 is called when the L1 guest hypervisor runs its nested
12251 * L2 guest. L1 has a vmcs for L2 (vmcs12), and this function "merges" it
12252 * with L0's requirements for its guest (a.k.a. vmcs01), so we can run the L2
12253 * guest in a way that will both be appropriate to L1's requests, and our
12254 * needs. In addition to modifying the active vmcs (which is vmcs02), this
12255 * function also has additional necessary side-effects, like setting various
12256 * vcpu->arch fields.
12257 * Returns 0 on success, 1 on failure. Invalid state exit qualification code
12258 * is assigned to entry_failure_code on failure.
12260 static int prepare_vmcs02(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12,
12261 u32 *entry_failure_code)
12263 struct vcpu_vmx *vmx = to_vmx(vcpu);
12264 u32 exec_control, vmcs12_exec_ctrl;
12266 if (vmx->nested.dirty_vmcs12) {
12267 prepare_vmcs02_full(vcpu, vmcs12);
12268 vmx->nested.dirty_vmcs12 = false;
12272 * First, the fields that are shadowed. This must be kept in sync
12273 * with vmx_shadow_fields.h.
12276 vmcs_write16(GUEST_CS_SELECTOR, vmcs12->guest_cs_selector);
12277 vmcs_write32(GUEST_CS_LIMIT, vmcs12->guest_cs_limit);
12278 vmcs_write32(GUEST_CS_AR_BYTES, vmcs12->guest_cs_ar_bytes);
12279 vmcs_writel(GUEST_ES_BASE, vmcs12->guest_es_base);
12280 vmcs_writel(GUEST_CS_BASE, vmcs12->guest_cs_base);
12282 if (vmx->nested.nested_run_pending &&
12283 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS)) {
12284 kvm_set_dr(vcpu, 7, vmcs12->guest_dr7);
12285 vmcs_write64(GUEST_IA32_DEBUGCTL, vmcs12->guest_ia32_debugctl);
12287 kvm_set_dr(vcpu, 7, vcpu->arch.dr7);
12288 vmcs_write64(GUEST_IA32_DEBUGCTL, vmx->nested.vmcs01_debugctl);
12290 if (kvm_mpx_supported() && (!vmx->nested.nested_run_pending ||
12291 !(vmcs12->vm_entry_controls & VM_ENTRY_LOAD_BNDCFGS)))
12292 vmcs_write64(GUEST_BNDCFGS, vmx->nested.vmcs01_guest_bndcfgs);
12293 if (vmx->nested.nested_run_pending) {
12294 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
12295 vmcs12->vm_entry_intr_info_field);
12296 vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE,
12297 vmcs12->vm_entry_exception_error_code);
12298 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
12299 vmcs12->vm_entry_instruction_len);
12300 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO,
12301 vmcs12->guest_interruptibility_info);
12302 vmx->loaded_vmcs->nmi_known_unmasked =
12303 !(vmcs12->guest_interruptibility_info & GUEST_INTR_STATE_NMI);
12305 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0);
12307 vmx_set_rflags(vcpu, vmcs12->guest_rflags);
12309 exec_control = vmcs12->pin_based_vm_exec_control;
12311 /* Preemption timer setting is computed directly in vmx_vcpu_run. */
12312 exec_control |= vmcs_config.pin_based_exec_ctrl;
12313 exec_control &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
12314 vmx->loaded_vmcs->hv_timer_armed = false;
12316 /* Posted interrupts setting is only taken from vmcs12. */
12317 if (nested_cpu_has_posted_intr(vmcs12)) {
12318 vmx->nested.posted_intr_nv = vmcs12->posted_intr_nv;
12319 vmx->nested.pi_pending = false;
12321 exec_control &= ~PIN_BASED_POSTED_INTR;
12324 vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, exec_control);
12326 vmx->nested.preemption_timer_expired = false;
12327 if (nested_cpu_has_preemption_timer(vmcs12))
12328 vmx_start_preemption_timer(vcpu);
12330 if (cpu_has_secondary_exec_ctrls()) {
12331 exec_control = vmx->secondary_exec_control;
12333 /* Take the following fields only from vmcs12 */
12334 exec_control &= ~(SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
12335 SECONDARY_EXEC_ENABLE_INVPCID |
12336 SECONDARY_EXEC_RDTSCP |
12337 SECONDARY_EXEC_XSAVES |
12338 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY |
12339 SECONDARY_EXEC_APIC_REGISTER_VIRT |
12340 SECONDARY_EXEC_ENABLE_VMFUNC);
12341 if (nested_cpu_has(vmcs12,
12342 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS)) {
12343 vmcs12_exec_ctrl = vmcs12->secondary_vm_exec_control &
12344 ~SECONDARY_EXEC_ENABLE_PML;
12345 exec_control |= vmcs12_exec_ctrl;
12348 /* VMCS shadowing for L2 is emulated for now */
12349 exec_control &= ~SECONDARY_EXEC_SHADOW_VMCS;
12351 if (exec_control & SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY)
12352 vmcs_write16(GUEST_INTR_STATUS,
12353 vmcs12->guest_intr_status);
12356 * Write an illegal value to APIC_ACCESS_ADDR. Later,
12357 * nested_get_vmcs12_pages will either fix it up or
12358 * remove the VM execution control.
12360 if (exec_control & SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)
12361 vmcs_write64(APIC_ACCESS_ADDR, -1ull);
12363 if (exec_control & SECONDARY_EXEC_ENCLS_EXITING)
12364 vmcs_write64(ENCLS_EXITING_BITMAP, -1ull);
12366 vmcs_write32(SECONDARY_VM_EXEC_CONTROL, exec_control);
12370 * HOST_RSP is normally set correctly in vmx_vcpu_run() just before
12371 * entry, but only if the current (host) sp changed from the value
12372 * we wrote last (vmx->host_rsp). This cache is no longer relevant
12373 * if we switch vmcs, and rather than hold a separate cache per vmcs,
12374 * here we just force the write to happen on entry.
12378 exec_control = vmx_exec_control(vmx); /* L0's desires */
12379 exec_control &= ~CPU_BASED_VIRTUAL_INTR_PENDING;
12380 exec_control &= ~CPU_BASED_VIRTUAL_NMI_PENDING;
12381 exec_control &= ~CPU_BASED_TPR_SHADOW;
12382 exec_control |= vmcs12->cpu_based_vm_exec_control;
12385 * Write an illegal value to VIRTUAL_APIC_PAGE_ADDR. Later, if
12386 * nested_get_vmcs12_pages can't fix it up, the illegal value
12387 * will result in a VM entry failure.
12389 if (exec_control & CPU_BASED_TPR_SHADOW) {
12390 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, -1ull);
12391 vmcs_write32(TPR_THRESHOLD, vmcs12->tpr_threshold);
12393 #ifdef CONFIG_X86_64
12394 exec_control |= CPU_BASED_CR8_LOAD_EXITING |
12395 CPU_BASED_CR8_STORE_EXITING;
12400 * A vmexit (to either L1 hypervisor or L0 userspace) is always needed
12401 * for I/O port accesses.
12403 exec_control &= ~CPU_BASED_USE_IO_BITMAPS;
12404 exec_control |= CPU_BASED_UNCOND_IO_EXITING;
12406 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, exec_control);
12408 /* EXCEPTION_BITMAP and CR0_GUEST_HOST_MASK should basically be the
12409 * bitwise-or of what L1 wants to trap for L2, and what we want to
12410 * trap. Note that CR0.TS also needs updating - we do this later.
12412 update_exception_bitmap(vcpu);
12413 vcpu->arch.cr0_guest_owned_bits &= ~vmcs12->cr0_guest_host_mask;
12414 vmcs_writel(CR0_GUEST_HOST_MASK, ~vcpu->arch.cr0_guest_owned_bits);
12416 /* L2->L1 exit controls are emulated - the hardware exit is to L0 so
12417 * we should use its exit controls. Note that VM_EXIT_LOAD_IA32_EFER
12418 * bits are further modified by vmx_set_efer() below.
12420 vmcs_write32(VM_EXIT_CONTROLS, vmcs_config.vmexit_ctrl);
12422 /* vmcs12's VM_ENTRY_LOAD_IA32_EFER and VM_ENTRY_IA32E_MODE are
12423 * emulated by vmx_set_efer(), below.
12425 vm_entry_controls_init(vmx,
12426 (vmcs12->vm_entry_controls & ~VM_ENTRY_LOAD_IA32_EFER &
12427 ~VM_ENTRY_IA32E_MODE) |
12428 (vmcs_config.vmentry_ctrl & ~VM_ENTRY_IA32E_MODE));
12430 if (vmx->nested.nested_run_pending &&
12431 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_PAT)) {
12432 vmcs_write64(GUEST_IA32_PAT, vmcs12->guest_ia32_pat);
12433 vcpu->arch.pat = vmcs12->guest_ia32_pat;
12434 } else if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) {
12435 vmcs_write64(GUEST_IA32_PAT, vmx->vcpu.arch.pat);
12438 vmcs_write64(TSC_OFFSET, vcpu->arch.tsc_offset);
12440 if (kvm_has_tsc_control)
12441 decache_tsc_multiplier(vmx);
12445 * There is no direct mapping between vpid02 and vpid12, the
12446 * vpid02 is per-vCPU for L0 and reused while the value of
12447 * vpid12 is changed w/ one invvpid during nested vmentry.
12448 * The vpid12 is allocated by L1 for L2, so it will not
12449 * influence global bitmap(for vpid01 and vpid02 allocation)
12450 * even if spawn a lot of nested vCPUs.
12452 if (nested_cpu_has_vpid(vmcs12) && vmx->nested.vpid02) {
12453 if (vmcs12->virtual_processor_id != vmx->nested.last_vpid) {
12454 vmx->nested.last_vpid = vmcs12->virtual_processor_id;
12455 __vmx_flush_tlb(vcpu, vmx->nested.vpid02, true);
12458 vmx_flush_tlb(vcpu, true);
12464 * Conceptually we want to copy the PML address and index from
12465 * vmcs01 here, and then back to vmcs01 on nested vmexit. But,
12466 * since we always flush the log on each vmexit, this happens
12467 * to be equivalent to simply resetting the fields in vmcs02.
12469 ASSERT(vmx->pml_pg);
12470 vmcs_write64(PML_ADDRESS, page_to_phys(vmx->pml_pg));
12471 vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1);
12474 if (nested_cpu_has_ept(vmcs12)) {
12475 if (nested_ept_init_mmu_context(vcpu)) {
12476 *entry_failure_code = ENTRY_FAIL_DEFAULT;
12479 } else if (nested_cpu_has2(vmcs12,
12480 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) {
12481 vmx_flush_tlb(vcpu, true);
12485 * This sets GUEST_CR0 to vmcs12->guest_cr0, possibly modifying those
12486 * bits which we consider mandatory enabled.
12487 * The CR0_READ_SHADOW is what L2 should have expected to read given
12488 * the specifications by L1; It's not enough to take
12489 * vmcs12->cr0_read_shadow because on our cr0_guest_host_mask we we
12490 * have more bits than L1 expected.
12492 vmx_set_cr0(vcpu, vmcs12->guest_cr0);
12493 vmcs_writel(CR0_READ_SHADOW, nested_read_cr0(vmcs12));
12495 vmx_set_cr4(vcpu, vmcs12->guest_cr4);
12496 vmcs_writel(CR4_READ_SHADOW, nested_read_cr4(vmcs12));
12498 if (vmx->nested.nested_run_pending &&
12499 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_EFER))
12500 vcpu->arch.efer = vmcs12->guest_ia32_efer;
12501 else if (vmcs12->vm_entry_controls & VM_ENTRY_IA32E_MODE)
12502 vcpu->arch.efer |= (EFER_LMA | EFER_LME);
12504 vcpu->arch.efer &= ~(EFER_LMA | EFER_LME);
12505 /* Note: modifies VM_ENTRY/EXIT_CONTROLS and GUEST/HOST_IA32_EFER */
12506 vmx_set_efer(vcpu, vcpu->arch.efer);
12509 * Guest state is invalid and unrestricted guest is disabled,
12510 * which means L1 attempted VMEntry to L2 with invalid state.
12511 * Fail the VMEntry.
12513 if (vmx->emulation_required) {
12514 *entry_failure_code = ENTRY_FAIL_DEFAULT;
12518 /* Shadow page tables on either EPT or shadow page tables. */
12519 if (nested_vmx_load_cr3(vcpu, vmcs12->guest_cr3, nested_cpu_has_ept(vmcs12),
12520 entry_failure_code))
12524 vcpu->arch.walk_mmu->inject_page_fault = vmx_inject_page_fault_nested;
12526 kvm_register_write(vcpu, VCPU_REGS_RSP, vmcs12->guest_rsp);
12527 kvm_register_write(vcpu, VCPU_REGS_RIP, vmcs12->guest_rip);
12531 static int nested_vmx_check_nmi_controls(struct vmcs12 *vmcs12)
12533 if (!nested_cpu_has_nmi_exiting(vmcs12) &&
12534 nested_cpu_has_virtual_nmis(vmcs12))
12537 if (!nested_cpu_has_virtual_nmis(vmcs12) &&
12538 nested_cpu_has(vmcs12, CPU_BASED_VIRTUAL_NMI_PENDING))
12544 static int check_vmentry_prereqs(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
12546 struct vcpu_vmx *vmx = to_vmx(vcpu);
12548 if (vmcs12->guest_activity_state != GUEST_ACTIVITY_ACTIVE &&
12549 vmcs12->guest_activity_state != GUEST_ACTIVITY_HLT)
12550 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12552 if (nested_cpu_has_vpid(vmcs12) && !vmcs12->virtual_processor_id)
12553 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12555 if (nested_vmx_check_io_bitmap_controls(vcpu, vmcs12))
12556 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12558 if (nested_vmx_check_msr_bitmap_controls(vcpu, vmcs12))
12559 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12561 if (nested_vmx_check_apic_access_controls(vcpu, vmcs12))
12562 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12564 if (nested_vmx_check_tpr_shadow_controls(vcpu, vmcs12))
12565 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12567 if (nested_vmx_check_apicv_controls(vcpu, vmcs12))
12568 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12570 if (nested_vmx_check_msr_switch_controls(vcpu, vmcs12))
12571 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12573 if (nested_vmx_check_pml_controls(vcpu, vmcs12))
12574 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12576 if (nested_vmx_check_shadow_vmcs_controls(vcpu, vmcs12))
12577 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12579 if (!vmx_control_verify(vmcs12->cpu_based_vm_exec_control,
12580 vmx->nested.msrs.procbased_ctls_low,
12581 vmx->nested.msrs.procbased_ctls_high) ||
12582 (nested_cpu_has(vmcs12, CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) &&
12583 !vmx_control_verify(vmcs12->secondary_vm_exec_control,
12584 vmx->nested.msrs.secondary_ctls_low,
12585 vmx->nested.msrs.secondary_ctls_high)) ||
12586 !vmx_control_verify(vmcs12->pin_based_vm_exec_control,
12587 vmx->nested.msrs.pinbased_ctls_low,
12588 vmx->nested.msrs.pinbased_ctls_high) ||
12589 !vmx_control_verify(vmcs12->vm_exit_controls,
12590 vmx->nested.msrs.exit_ctls_low,
12591 vmx->nested.msrs.exit_ctls_high) ||
12592 !vmx_control_verify(vmcs12->vm_entry_controls,
12593 vmx->nested.msrs.entry_ctls_low,
12594 vmx->nested.msrs.entry_ctls_high))
12595 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12597 if (nested_vmx_check_nmi_controls(vmcs12))
12598 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12600 if (nested_cpu_has_vmfunc(vmcs12)) {
12601 if (vmcs12->vm_function_control &
12602 ~vmx->nested.msrs.vmfunc_controls)
12603 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12605 if (nested_cpu_has_eptp_switching(vmcs12)) {
12606 if (!nested_cpu_has_ept(vmcs12) ||
12607 !page_address_valid(vcpu, vmcs12->eptp_list_address))
12608 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12612 if (vmcs12->cr3_target_count > nested_cpu_vmx_misc_cr3_count(vcpu))
12613 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12615 if (!nested_host_cr0_valid(vcpu, vmcs12->host_cr0) ||
12616 !nested_host_cr4_valid(vcpu, vmcs12->host_cr4) ||
12617 !nested_cr3_valid(vcpu, vmcs12->host_cr3))
12618 return VMXERR_ENTRY_INVALID_HOST_STATE_FIELD;
12621 * From the Intel SDM, volume 3:
12622 * Fields relevant to VM-entry event injection must be set properly.
12623 * These fields are the VM-entry interruption-information field, the
12624 * VM-entry exception error code, and the VM-entry instruction length.
12626 if (vmcs12->vm_entry_intr_info_field & INTR_INFO_VALID_MASK) {
12627 u32 intr_info = vmcs12->vm_entry_intr_info_field;
12628 u8 vector = intr_info & INTR_INFO_VECTOR_MASK;
12629 u32 intr_type = intr_info & INTR_INFO_INTR_TYPE_MASK;
12630 bool has_error_code = intr_info & INTR_INFO_DELIVER_CODE_MASK;
12631 bool should_have_error_code;
12632 bool urg = nested_cpu_has2(vmcs12,
12633 SECONDARY_EXEC_UNRESTRICTED_GUEST);
12634 bool prot_mode = !urg || vmcs12->guest_cr0 & X86_CR0_PE;
12636 /* VM-entry interruption-info field: interruption type */
12637 if (intr_type == INTR_TYPE_RESERVED ||
12638 (intr_type == INTR_TYPE_OTHER_EVENT &&
12639 !nested_cpu_supports_monitor_trap_flag(vcpu)))
12640 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12642 /* VM-entry interruption-info field: vector */
12643 if ((intr_type == INTR_TYPE_NMI_INTR && vector != NMI_VECTOR) ||
12644 (intr_type == INTR_TYPE_HARD_EXCEPTION && vector > 31) ||
12645 (intr_type == INTR_TYPE_OTHER_EVENT && vector != 0))
12646 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12648 /* VM-entry interruption-info field: deliver error code */
12649 should_have_error_code =
12650 intr_type == INTR_TYPE_HARD_EXCEPTION && prot_mode &&
12651 x86_exception_has_error_code(vector);
12652 if (has_error_code != should_have_error_code)
12653 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12655 /* VM-entry exception error code */
12656 if (has_error_code &&
12657 vmcs12->vm_entry_exception_error_code & GENMASK(31, 16))
12658 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12660 /* VM-entry interruption-info field: reserved bits */
12661 if (intr_info & INTR_INFO_RESVD_BITS_MASK)
12662 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12664 /* VM-entry instruction length */
12665 switch (intr_type) {
12666 case INTR_TYPE_SOFT_EXCEPTION:
12667 case INTR_TYPE_SOFT_INTR:
12668 case INTR_TYPE_PRIV_SW_EXCEPTION:
12669 if ((vmcs12->vm_entry_instruction_len > 15) ||
12670 (vmcs12->vm_entry_instruction_len == 0 &&
12671 !nested_cpu_has_zero_length_injection(vcpu)))
12672 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12679 static int nested_vmx_check_vmcs_link_ptr(struct kvm_vcpu *vcpu,
12680 struct vmcs12 *vmcs12)
12684 struct vmcs12 *shadow;
12686 if (vmcs12->vmcs_link_pointer == -1ull)
12689 if (!page_address_valid(vcpu, vmcs12->vmcs_link_pointer))
12692 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->vmcs_link_pointer);
12693 if (is_error_page(page))
12697 shadow = kmap(page);
12698 if (shadow->hdr.revision_id != VMCS12_REVISION ||
12699 shadow->hdr.shadow_vmcs != nested_cpu_has_shadow_vmcs(vmcs12))
12702 kvm_release_page_clean(page);
12706 static int check_vmentry_postreqs(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12,
12711 *exit_qual = ENTRY_FAIL_DEFAULT;
12713 if (!nested_guest_cr0_valid(vcpu, vmcs12->guest_cr0) ||
12714 !nested_guest_cr4_valid(vcpu, vmcs12->guest_cr4))
12717 if (nested_vmx_check_vmcs_link_ptr(vcpu, vmcs12)) {
12718 *exit_qual = ENTRY_FAIL_VMCS_LINK_PTR;
12723 * If the load IA32_EFER VM-entry control is 1, the following checks
12724 * are performed on the field for the IA32_EFER MSR:
12725 * - Bits reserved in the IA32_EFER MSR must be 0.
12726 * - Bit 10 (corresponding to IA32_EFER.LMA) must equal the value of
12727 * the IA-32e mode guest VM-exit control. It must also be identical
12728 * to bit 8 (LME) if bit 31 in the CR0 field (corresponding to
12731 if (to_vmx(vcpu)->nested.nested_run_pending &&
12732 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_EFER)) {
12733 ia32e = (vmcs12->vm_entry_controls & VM_ENTRY_IA32E_MODE) != 0;
12734 if (!kvm_valid_efer(vcpu, vmcs12->guest_ia32_efer) ||
12735 ia32e != !!(vmcs12->guest_ia32_efer & EFER_LMA) ||
12736 ((vmcs12->guest_cr0 & X86_CR0_PG) &&
12737 ia32e != !!(vmcs12->guest_ia32_efer & EFER_LME)))
12742 * If the load IA32_EFER VM-exit control is 1, bits reserved in the
12743 * IA32_EFER MSR must be 0 in the field for that register. In addition,
12744 * the values of the LMA and LME bits in the field must each be that of
12745 * the host address-space size VM-exit control.
12747 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_EFER) {
12748 ia32e = (vmcs12->vm_exit_controls &
12749 VM_EXIT_HOST_ADDR_SPACE_SIZE) != 0;
12750 if (!kvm_valid_efer(vcpu, vmcs12->host_ia32_efer) ||
12751 ia32e != !!(vmcs12->host_ia32_efer & EFER_LMA) ||
12752 ia32e != !!(vmcs12->host_ia32_efer & EFER_LME))
12756 if ((vmcs12->vm_entry_controls & VM_ENTRY_LOAD_BNDCFGS) &&
12757 (is_noncanonical_address(vmcs12->guest_bndcfgs & PAGE_MASK, vcpu) ||
12758 (vmcs12->guest_bndcfgs & MSR_IA32_BNDCFGS_RSVD)))
12765 * If exit_qual is NULL, this is being called from state restore (either RSM
12766 * or KVM_SET_NESTED_STATE). Otherwise it's called from vmlaunch/vmresume.
12768 static int enter_vmx_non_root_mode(struct kvm_vcpu *vcpu, u32 *exit_qual)
12770 struct vcpu_vmx *vmx = to_vmx(vcpu);
12771 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
12772 bool from_vmentry = !!exit_qual;
12773 u32 dummy_exit_qual;
12774 bool evaluate_pending_interrupts;
12777 evaluate_pending_interrupts = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) &
12778 (CPU_BASED_VIRTUAL_INTR_PENDING | CPU_BASED_VIRTUAL_NMI_PENDING);
12779 if (likely(!evaluate_pending_interrupts) && kvm_vcpu_apicv_active(vcpu))
12780 evaluate_pending_interrupts |= vmx_has_apicv_interrupt(vcpu);
12782 enter_guest_mode(vcpu);
12784 if (!(vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS))
12785 vmx->nested.vmcs01_debugctl = vmcs_read64(GUEST_IA32_DEBUGCTL);
12786 if (kvm_mpx_supported() &&
12787 !(vmcs12->vm_entry_controls & VM_ENTRY_LOAD_BNDCFGS))
12788 vmx->nested.vmcs01_guest_bndcfgs = vmcs_read64(GUEST_BNDCFGS);
12790 vmx_switch_vmcs(vcpu, &vmx->nested.vmcs02);
12791 vmx_segment_cache_clear(vmx);
12793 if (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING)
12794 vcpu->arch.tsc_offset += vmcs12->tsc_offset;
12796 r = EXIT_REASON_INVALID_STATE;
12797 if (prepare_vmcs02(vcpu, vmcs12, from_vmentry ? exit_qual : &dummy_exit_qual))
12800 if (from_vmentry) {
12801 nested_get_vmcs12_pages(vcpu);
12803 r = EXIT_REASON_MSR_LOAD_FAIL;
12804 *exit_qual = nested_vmx_load_msr(vcpu,
12805 vmcs12->vm_entry_msr_load_addr,
12806 vmcs12->vm_entry_msr_load_count);
12811 * The MMU is not initialized to point at the right entities yet and
12812 * "get pages" would need to read data from the guest (i.e. we will
12813 * need to perform gpa to hpa translation). Request a call
12814 * to nested_get_vmcs12_pages before the next VM-entry. The MSRs
12815 * have already been set at vmentry time and should not be reset.
12817 kvm_make_request(KVM_REQ_GET_VMCS12_PAGES, vcpu);
12821 * If L1 had a pending IRQ/NMI until it executed
12822 * VMLAUNCH/VMRESUME which wasn't delivered because it was
12823 * disallowed (e.g. interrupts disabled), L0 needs to
12824 * evaluate if this pending event should cause an exit from L2
12825 * to L1 or delivered directly to L2 (e.g. In case L1 don't
12826 * intercept EXTERNAL_INTERRUPT).
12828 * Usually this would be handled by the processor noticing an
12829 * IRQ/NMI window request, or checking RVI during evaluation of
12830 * pending virtual interrupts. However, this setting was done
12831 * on VMCS01 and now VMCS02 is active instead. Thus, we force L0
12832 * to perform pending event evaluation by requesting a KVM_REQ_EVENT.
12834 if (unlikely(evaluate_pending_interrupts))
12835 kvm_make_request(KVM_REQ_EVENT, vcpu);
12838 * Note no nested_vmx_succeed or nested_vmx_fail here. At this point
12839 * we are no longer running L1, and VMLAUNCH/VMRESUME has not yet
12840 * returned as far as L1 is concerned. It will only return (and set
12841 * the success flag) when L2 exits (see nested_vmx_vmexit()).
12846 if (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING)
12847 vcpu->arch.tsc_offset -= vmcs12->tsc_offset;
12848 leave_guest_mode(vcpu);
12849 vmx_switch_vmcs(vcpu, &vmx->vmcs01);
12854 * nested_vmx_run() handles a nested entry, i.e., a VMLAUNCH or VMRESUME on L1
12855 * for running an L2 nested guest.
12857 static int nested_vmx_run(struct kvm_vcpu *vcpu, bool launch)
12859 struct vmcs12 *vmcs12;
12860 struct vcpu_vmx *vmx = to_vmx(vcpu);
12861 u32 interrupt_shadow = vmx_get_interrupt_shadow(vcpu);
12865 if (!nested_vmx_check_permission(vcpu))
12868 if (!nested_vmx_check_vmcs12(vcpu))
12871 vmcs12 = get_vmcs12(vcpu);
12874 * Can't VMLAUNCH or VMRESUME a shadow VMCS. Despite the fact
12875 * that there *is* a valid VMCS pointer, RFLAGS.CF is set
12876 * rather than RFLAGS.ZF, and no error number is stored to the
12877 * VM-instruction error field.
12879 if (vmcs12->hdr.shadow_vmcs) {
12880 nested_vmx_failInvalid(vcpu);
12884 if (enable_shadow_vmcs)
12885 copy_shadow_to_vmcs12(vmx);
12888 * The nested entry process starts with enforcing various prerequisites
12889 * on vmcs12 as required by the Intel SDM, and act appropriately when
12890 * they fail: As the SDM explains, some conditions should cause the
12891 * instruction to fail, while others will cause the instruction to seem
12892 * to succeed, but return an EXIT_REASON_INVALID_STATE.
12893 * To speed up the normal (success) code path, we should avoid checking
12894 * for misconfigurations which will anyway be caught by the processor
12895 * when using the merged vmcs02.
12897 if (interrupt_shadow & KVM_X86_SHADOW_INT_MOV_SS) {
12898 nested_vmx_failValid(vcpu,
12899 VMXERR_ENTRY_EVENTS_BLOCKED_BY_MOV_SS);
12903 if (vmcs12->launch_state == launch) {
12904 nested_vmx_failValid(vcpu,
12905 launch ? VMXERR_VMLAUNCH_NONCLEAR_VMCS
12906 : VMXERR_VMRESUME_NONLAUNCHED_VMCS);
12910 ret = check_vmentry_prereqs(vcpu, vmcs12);
12912 nested_vmx_failValid(vcpu, ret);
12917 * After this point, the trap flag no longer triggers a singlestep trap
12918 * on the vm entry instructions; don't call kvm_skip_emulated_instruction.
12919 * This is not 100% correct; for performance reasons, we delegate most
12920 * of the checks on host state to the processor. If those fail,
12921 * the singlestep trap is missed.
12923 skip_emulated_instruction(vcpu);
12925 ret = check_vmentry_postreqs(vcpu, vmcs12, &exit_qual);
12927 nested_vmx_entry_failure(vcpu, vmcs12,
12928 EXIT_REASON_INVALID_STATE, exit_qual);
12933 * We're finally done with prerequisite checking, and can start with
12934 * the nested entry.
12937 vmx->nested.nested_run_pending = 1;
12938 ret = enter_vmx_non_root_mode(vcpu, &exit_qual);
12940 nested_vmx_entry_failure(vcpu, vmcs12, ret, exit_qual);
12941 vmx->nested.nested_run_pending = 0;
12945 /* Hide L1D cache contents from the nested guest. */
12946 vmx->vcpu.arch.l1tf_flush_l1d = true;
12949 * Must happen outside of enter_vmx_non_root_mode() as it will
12950 * also be used as part of restoring nVMX state for
12951 * snapshot restore (migration).
12953 * In this flow, it is assumed that vmcs12 cache was
12954 * trasferred as part of captured nVMX state and should
12955 * therefore not be read from guest memory (which may not
12956 * exist on destination host yet).
12958 nested_cache_shadow_vmcs12(vcpu, vmcs12);
12961 * If we're entering a halted L2 vcpu and the L2 vcpu won't be
12962 * awakened by event injection or by an NMI-window VM-exit or
12963 * by an interrupt-window VM-exit, halt the vcpu.
12965 if ((vmcs12->guest_activity_state == GUEST_ACTIVITY_HLT) &&
12966 !(vmcs12->vm_entry_intr_info_field & INTR_INFO_VALID_MASK) &&
12967 !(vmcs12->cpu_based_vm_exec_control & CPU_BASED_VIRTUAL_NMI_PENDING) &&
12968 !((vmcs12->cpu_based_vm_exec_control & CPU_BASED_VIRTUAL_INTR_PENDING) &&
12969 (vmcs12->guest_rflags & X86_EFLAGS_IF))) {
12970 vmx->nested.nested_run_pending = 0;
12971 return kvm_vcpu_halt(vcpu);
12976 return kvm_skip_emulated_instruction(vcpu);
12980 * On a nested exit from L2 to L1, vmcs12.guest_cr0 might not be up-to-date
12981 * because L2 may have changed some cr0 bits directly (CRO_GUEST_HOST_MASK).
12982 * This function returns the new value we should put in vmcs12.guest_cr0.
12983 * It's not enough to just return the vmcs02 GUEST_CR0. Rather,
12984 * 1. Bits that neither L0 nor L1 trapped, were set directly by L2 and are now
12985 * available in vmcs02 GUEST_CR0. (Note: It's enough to check that L0
12986 * didn't trap the bit, because if L1 did, so would L0).
12987 * 2. Bits that L1 asked to trap (and therefore L0 also did) could not have
12988 * been modified by L2, and L1 knows it. So just leave the old value of
12989 * the bit from vmcs12.guest_cr0. Note that the bit from vmcs02 GUEST_CR0
12990 * isn't relevant, because if L0 traps this bit it can set it to anything.
12991 * 3. Bits that L1 didn't trap, but L0 did. L1 believes the guest could have
12992 * changed these bits, and therefore they need to be updated, but L0
12993 * didn't necessarily allow them to be changed in GUEST_CR0 - and rather
12994 * put them in vmcs02 CR0_READ_SHADOW. So take these bits from there.
12996 static inline unsigned long
12997 vmcs12_guest_cr0(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
13000 /*1*/ (vmcs_readl(GUEST_CR0) & vcpu->arch.cr0_guest_owned_bits) |
13001 /*2*/ (vmcs12->guest_cr0 & vmcs12->cr0_guest_host_mask) |
13002 /*3*/ (vmcs_readl(CR0_READ_SHADOW) & ~(vmcs12->cr0_guest_host_mask |
13003 vcpu->arch.cr0_guest_owned_bits));
13006 static inline unsigned long
13007 vmcs12_guest_cr4(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
13010 /*1*/ (vmcs_readl(GUEST_CR4) & vcpu->arch.cr4_guest_owned_bits) |
13011 /*2*/ (vmcs12->guest_cr4 & vmcs12->cr4_guest_host_mask) |
13012 /*3*/ (vmcs_readl(CR4_READ_SHADOW) & ~(vmcs12->cr4_guest_host_mask |
13013 vcpu->arch.cr4_guest_owned_bits));
13016 static void vmcs12_save_pending_event(struct kvm_vcpu *vcpu,
13017 struct vmcs12 *vmcs12)
13022 if (vcpu->arch.exception.injected) {
13023 nr = vcpu->arch.exception.nr;
13024 idt_vectoring = nr | VECTORING_INFO_VALID_MASK;
13026 if (kvm_exception_is_soft(nr)) {
13027 vmcs12->vm_exit_instruction_len =
13028 vcpu->arch.event_exit_inst_len;
13029 idt_vectoring |= INTR_TYPE_SOFT_EXCEPTION;
13031 idt_vectoring |= INTR_TYPE_HARD_EXCEPTION;
13033 if (vcpu->arch.exception.has_error_code) {
13034 idt_vectoring |= VECTORING_INFO_DELIVER_CODE_MASK;
13035 vmcs12->idt_vectoring_error_code =
13036 vcpu->arch.exception.error_code;
13039 vmcs12->idt_vectoring_info_field = idt_vectoring;
13040 } else if (vcpu->arch.nmi_injected) {
13041 vmcs12->idt_vectoring_info_field =
13042 INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK | NMI_VECTOR;
13043 } else if (vcpu->arch.interrupt.injected) {
13044 nr = vcpu->arch.interrupt.nr;
13045 idt_vectoring = nr | VECTORING_INFO_VALID_MASK;
13047 if (vcpu->arch.interrupt.soft) {
13048 idt_vectoring |= INTR_TYPE_SOFT_INTR;
13049 vmcs12->vm_entry_instruction_len =
13050 vcpu->arch.event_exit_inst_len;
13052 idt_vectoring |= INTR_TYPE_EXT_INTR;
13054 vmcs12->idt_vectoring_info_field = idt_vectoring;
13058 static int vmx_check_nested_events(struct kvm_vcpu *vcpu)
13060 struct vcpu_vmx *vmx = to_vmx(vcpu);
13061 unsigned long exit_qual;
13062 bool block_nested_events =
13063 vmx->nested.nested_run_pending || kvm_event_needs_reinjection(vcpu);
13065 if (vcpu->arch.exception.pending &&
13066 nested_vmx_check_exception(vcpu, &exit_qual)) {
13067 if (block_nested_events)
13069 nested_vmx_inject_exception_vmexit(vcpu, exit_qual);
13073 if (nested_cpu_has_preemption_timer(get_vmcs12(vcpu)) &&
13074 vmx->nested.preemption_timer_expired) {
13075 if (block_nested_events)
13077 nested_vmx_vmexit(vcpu, EXIT_REASON_PREEMPTION_TIMER, 0, 0);
13081 if (vcpu->arch.nmi_pending && nested_exit_on_nmi(vcpu)) {
13082 if (block_nested_events)
13084 nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI,
13085 NMI_VECTOR | INTR_TYPE_NMI_INTR |
13086 INTR_INFO_VALID_MASK, 0);
13088 * The NMI-triggered VM exit counts as injection:
13089 * clear this one and block further NMIs.
13091 vcpu->arch.nmi_pending = 0;
13092 vmx_set_nmi_mask(vcpu, true);
13096 if (kvm_cpu_has_interrupt(vcpu) && nested_exit_on_intr(vcpu)) {
13097 if (block_nested_events)
13099 nested_vmx_vmexit(vcpu, EXIT_REASON_EXTERNAL_INTERRUPT, 0, 0);
13103 vmx_complete_nested_posted_interrupt(vcpu);
13107 static void vmx_request_immediate_exit(struct kvm_vcpu *vcpu)
13109 to_vmx(vcpu)->req_immediate_exit = true;
13112 static u32 vmx_get_preemption_timer_value(struct kvm_vcpu *vcpu)
13114 ktime_t remaining =
13115 hrtimer_get_remaining(&to_vmx(vcpu)->nested.preemption_timer);
13118 if (ktime_to_ns(remaining) <= 0)
13121 value = ktime_to_ns(remaining) * vcpu->arch.virtual_tsc_khz;
13122 do_div(value, 1000000);
13123 return value >> VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE;
13127 * Update the guest state fields of vmcs12 to reflect changes that
13128 * occurred while L2 was running. (The "IA-32e mode guest" bit of the
13129 * VM-entry controls is also updated, since this is really a guest
13132 static void sync_vmcs12(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
13134 vmcs12->guest_cr0 = vmcs12_guest_cr0(vcpu, vmcs12);
13135 vmcs12->guest_cr4 = vmcs12_guest_cr4(vcpu, vmcs12);
13137 vmcs12->guest_rsp = kvm_register_read(vcpu, VCPU_REGS_RSP);
13138 vmcs12->guest_rip = kvm_register_read(vcpu, VCPU_REGS_RIP);
13139 vmcs12->guest_rflags = vmcs_readl(GUEST_RFLAGS);
13141 vmcs12->guest_es_selector = vmcs_read16(GUEST_ES_SELECTOR);
13142 vmcs12->guest_cs_selector = vmcs_read16(GUEST_CS_SELECTOR);
13143 vmcs12->guest_ss_selector = vmcs_read16(GUEST_SS_SELECTOR);
13144 vmcs12->guest_ds_selector = vmcs_read16(GUEST_DS_SELECTOR);
13145 vmcs12->guest_fs_selector = vmcs_read16(GUEST_FS_SELECTOR);
13146 vmcs12->guest_gs_selector = vmcs_read16(GUEST_GS_SELECTOR);
13147 vmcs12->guest_ldtr_selector = vmcs_read16(GUEST_LDTR_SELECTOR);
13148 vmcs12->guest_tr_selector = vmcs_read16(GUEST_TR_SELECTOR);
13149 vmcs12->guest_es_limit = vmcs_read32(GUEST_ES_LIMIT);
13150 vmcs12->guest_cs_limit = vmcs_read32(GUEST_CS_LIMIT);
13151 vmcs12->guest_ss_limit = vmcs_read32(GUEST_SS_LIMIT);
13152 vmcs12->guest_ds_limit = vmcs_read32(GUEST_DS_LIMIT);
13153 vmcs12->guest_fs_limit = vmcs_read32(GUEST_FS_LIMIT);
13154 vmcs12->guest_gs_limit = vmcs_read32(GUEST_GS_LIMIT);
13155 vmcs12->guest_ldtr_limit = vmcs_read32(GUEST_LDTR_LIMIT);
13156 vmcs12->guest_tr_limit = vmcs_read32(GUEST_TR_LIMIT);
13157 vmcs12->guest_gdtr_limit = vmcs_read32(GUEST_GDTR_LIMIT);
13158 vmcs12->guest_idtr_limit = vmcs_read32(GUEST_IDTR_LIMIT);
13159 vmcs12->guest_es_ar_bytes = vmcs_read32(GUEST_ES_AR_BYTES);
13160 vmcs12->guest_cs_ar_bytes = vmcs_read32(GUEST_CS_AR_BYTES);
13161 vmcs12->guest_ss_ar_bytes = vmcs_read32(GUEST_SS_AR_BYTES);
13162 vmcs12->guest_ds_ar_bytes = vmcs_read32(GUEST_DS_AR_BYTES);
13163 vmcs12->guest_fs_ar_bytes = vmcs_read32(GUEST_FS_AR_BYTES);
13164 vmcs12->guest_gs_ar_bytes = vmcs_read32(GUEST_GS_AR_BYTES);
13165 vmcs12->guest_ldtr_ar_bytes = vmcs_read32(GUEST_LDTR_AR_BYTES);
13166 vmcs12->guest_tr_ar_bytes = vmcs_read32(GUEST_TR_AR_BYTES);
13167 vmcs12->guest_es_base = vmcs_readl(GUEST_ES_BASE);
13168 vmcs12->guest_cs_base = vmcs_readl(GUEST_CS_BASE);
13169 vmcs12->guest_ss_base = vmcs_readl(GUEST_SS_BASE);
13170 vmcs12->guest_ds_base = vmcs_readl(GUEST_DS_BASE);
13171 vmcs12->guest_fs_base = vmcs_readl(GUEST_FS_BASE);
13172 vmcs12->guest_gs_base = vmcs_readl(GUEST_GS_BASE);
13173 vmcs12->guest_ldtr_base = vmcs_readl(GUEST_LDTR_BASE);
13174 vmcs12->guest_tr_base = vmcs_readl(GUEST_TR_BASE);
13175 vmcs12->guest_gdtr_base = vmcs_readl(GUEST_GDTR_BASE);
13176 vmcs12->guest_idtr_base = vmcs_readl(GUEST_IDTR_BASE);
13178 vmcs12->guest_interruptibility_info =
13179 vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
13180 vmcs12->guest_pending_dbg_exceptions =
13181 vmcs_readl(GUEST_PENDING_DBG_EXCEPTIONS);
13182 if (vcpu->arch.mp_state == KVM_MP_STATE_HALTED)
13183 vmcs12->guest_activity_state = GUEST_ACTIVITY_HLT;
13185 vmcs12->guest_activity_state = GUEST_ACTIVITY_ACTIVE;
13187 if (nested_cpu_has_preemption_timer(vmcs12)) {
13188 if (vmcs12->vm_exit_controls &
13189 VM_EXIT_SAVE_VMX_PREEMPTION_TIMER)
13190 vmcs12->vmx_preemption_timer_value =
13191 vmx_get_preemption_timer_value(vcpu);
13192 hrtimer_cancel(&to_vmx(vcpu)->nested.preemption_timer);
13196 * In some cases (usually, nested EPT), L2 is allowed to change its
13197 * own CR3 without exiting. If it has changed it, we must keep it.
13198 * Of course, if L0 is using shadow page tables, GUEST_CR3 was defined
13199 * by L0, not L1 or L2, so we mustn't unconditionally copy it to vmcs12.
13201 * Additionally, restore L2's PDPTR to vmcs12.
13204 vmcs12->guest_cr3 = vmcs_readl(GUEST_CR3);
13205 vmcs12->guest_pdptr0 = vmcs_read64(GUEST_PDPTR0);
13206 vmcs12->guest_pdptr1 = vmcs_read64(GUEST_PDPTR1);
13207 vmcs12->guest_pdptr2 = vmcs_read64(GUEST_PDPTR2);
13208 vmcs12->guest_pdptr3 = vmcs_read64(GUEST_PDPTR3);
13211 vmcs12->guest_linear_address = vmcs_readl(GUEST_LINEAR_ADDRESS);
13213 if (nested_cpu_has_vid(vmcs12))
13214 vmcs12->guest_intr_status = vmcs_read16(GUEST_INTR_STATUS);
13216 vmcs12->vm_entry_controls =
13217 (vmcs12->vm_entry_controls & ~VM_ENTRY_IA32E_MODE) |
13218 (vm_entry_controls_get(to_vmx(vcpu)) & VM_ENTRY_IA32E_MODE);
13220 if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_DEBUG_CONTROLS) {
13221 kvm_get_dr(vcpu, 7, (unsigned long *)&vmcs12->guest_dr7);
13222 vmcs12->guest_ia32_debugctl = vmcs_read64(GUEST_IA32_DEBUGCTL);
13225 /* TODO: These cannot have changed unless we have MSR bitmaps and
13226 * the relevant bit asks not to trap the change */
13227 if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_IA32_PAT)
13228 vmcs12->guest_ia32_pat = vmcs_read64(GUEST_IA32_PAT);
13229 if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_IA32_EFER)
13230 vmcs12->guest_ia32_efer = vcpu->arch.efer;
13231 vmcs12->guest_sysenter_cs = vmcs_read32(GUEST_SYSENTER_CS);
13232 vmcs12->guest_sysenter_esp = vmcs_readl(GUEST_SYSENTER_ESP);
13233 vmcs12->guest_sysenter_eip = vmcs_readl(GUEST_SYSENTER_EIP);
13234 if (kvm_mpx_supported())
13235 vmcs12->guest_bndcfgs = vmcs_read64(GUEST_BNDCFGS);
13239 * prepare_vmcs12 is part of what we need to do when the nested L2 guest exits
13240 * and we want to prepare to run its L1 parent. L1 keeps a vmcs for L2 (vmcs12),
13241 * and this function updates it to reflect the changes to the guest state while
13242 * L2 was running (and perhaps made some exits which were handled directly by L0
13243 * without going back to L1), and to reflect the exit reason.
13244 * Note that we do not have to copy here all VMCS fields, just those that
13245 * could have changed by the L2 guest or the exit - i.e., the guest-state and
13246 * exit-information fields only. Other fields are modified by L1 with VMWRITE,
13247 * which already writes to vmcs12 directly.
13249 static void prepare_vmcs12(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12,
13250 u32 exit_reason, u32 exit_intr_info,
13251 unsigned long exit_qualification)
13253 /* update guest state fields: */
13254 sync_vmcs12(vcpu, vmcs12);
13256 /* update exit information fields: */
13258 vmcs12->vm_exit_reason = exit_reason;
13259 vmcs12->exit_qualification = exit_qualification;
13260 vmcs12->vm_exit_intr_info = exit_intr_info;
13262 vmcs12->idt_vectoring_info_field = 0;
13263 vmcs12->vm_exit_instruction_len = vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
13264 vmcs12->vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
13266 if (!(vmcs12->vm_exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY)) {
13267 vmcs12->launch_state = 1;
13269 /* vm_entry_intr_info_field is cleared on exit. Emulate this
13270 * instead of reading the real value. */
13271 vmcs12->vm_entry_intr_info_field &= ~INTR_INFO_VALID_MASK;
13274 * Transfer the event that L0 or L1 may wanted to inject into
13275 * L2 to IDT_VECTORING_INFO_FIELD.
13277 vmcs12_save_pending_event(vcpu, vmcs12);
13282 * A part of what we need to when the nested L2 guest exits and we want to
13283 * run its L1 parent, is to reset L1's guest state to the host state specified
13285 * This function is to be called not only on normal nested exit, but also on
13286 * a nested entry failure, as explained in Intel's spec, 3B.23.7 ("VM-Entry
13287 * Failures During or After Loading Guest State").
13288 * This function should be called when the active VMCS is L1's (vmcs01).
13290 static void load_vmcs12_host_state(struct kvm_vcpu *vcpu,
13291 struct vmcs12 *vmcs12)
13293 struct kvm_segment seg;
13294 u32 entry_failure_code;
13296 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_EFER)
13297 vcpu->arch.efer = vmcs12->host_ia32_efer;
13298 else if (vmcs12->vm_exit_controls & VM_EXIT_HOST_ADDR_SPACE_SIZE)
13299 vcpu->arch.efer |= (EFER_LMA | EFER_LME);
13301 vcpu->arch.efer &= ~(EFER_LMA | EFER_LME);
13302 vmx_set_efer(vcpu, vcpu->arch.efer);
13304 kvm_register_write(vcpu, VCPU_REGS_RSP, vmcs12->host_rsp);
13305 kvm_register_write(vcpu, VCPU_REGS_RIP, vmcs12->host_rip);
13306 vmx_set_rflags(vcpu, X86_EFLAGS_FIXED);
13308 * Note that calling vmx_set_cr0 is important, even if cr0 hasn't
13309 * actually changed, because vmx_set_cr0 refers to efer set above.
13311 * CR0_GUEST_HOST_MASK is already set in the original vmcs01
13312 * (KVM doesn't change it);
13314 vcpu->arch.cr0_guest_owned_bits = X86_CR0_TS;
13315 vmx_set_cr0(vcpu, vmcs12->host_cr0);
13317 /* Same as above - no reason to call set_cr4_guest_host_mask(). */
13318 vcpu->arch.cr4_guest_owned_bits = ~vmcs_readl(CR4_GUEST_HOST_MASK);
13319 vmx_set_cr4(vcpu, vmcs12->host_cr4);
13321 nested_ept_uninit_mmu_context(vcpu);
13324 * Only PDPTE load can fail as the value of cr3 was checked on entry and
13325 * couldn't have changed.
13327 if (nested_vmx_load_cr3(vcpu, vmcs12->host_cr3, false, &entry_failure_code))
13328 nested_vmx_abort(vcpu, VMX_ABORT_LOAD_HOST_PDPTE_FAIL);
13331 vcpu->arch.walk_mmu->inject_page_fault = kvm_inject_page_fault;
13334 * If vmcs01 don't use VPID, CPU flushes TLB on every
13335 * VMEntry/VMExit. Thus, no need to flush TLB.
13337 * If vmcs12 uses VPID, TLB entries populated by L2 are
13338 * tagged with vmx->nested.vpid02 while L1 entries are tagged
13339 * with vmx->vpid. Thus, no need to flush TLB.
13341 * Therefore, flush TLB only in case vmcs01 uses VPID and
13342 * vmcs12 don't use VPID as in this case L1 & L2 TLB entries
13343 * are both tagged with vmx->vpid.
13346 !(nested_cpu_has_vpid(vmcs12) && to_vmx(vcpu)->nested.vpid02)) {
13347 vmx_flush_tlb(vcpu, true);
13350 vmcs_write32(GUEST_SYSENTER_CS, vmcs12->host_ia32_sysenter_cs);
13351 vmcs_writel(GUEST_SYSENTER_ESP, vmcs12->host_ia32_sysenter_esp);
13352 vmcs_writel(GUEST_SYSENTER_EIP, vmcs12->host_ia32_sysenter_eip);
13353 vmcs_writel(GUEST_IDTR_BASE, vmcs12->host_idtr_base);
13354 vmcs_writel(GUEST_GDTR_BASE, vmcs12->host_gdtr_base);
13355 vmcs_write32(GUEST_IDTR_LIMIT, 0xFFFF);
13356 vmcs_write32(GUEST_GDTR_LIMIT, 0xFFFF);
13358 /* If not VM_EXIT_CLEAR_BNDCFGS, the L2 value propagates to L1. */
13359 if (vmcs12->vm_exit_controls & VM_EXIT_CLEAR_BNDCFGS)
13360 vmcs_write64(GUEST_BNDCFGS, 0);
13362 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PAT) {
13363 vmcs_write64(GUEST_IA32_PAT, vmcs12->host_ia32_pat);
13364 vcpu->arch.pat = vmcs12->host_ia32_pat;
13366 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL)
13367 vmcs_write64(GUEST_IA32_PERF_GLOBAL_CTRL,
13368 vmcs12->host_ia32_perf_global_ctrl);
13370 /* Set L1 segment info according to Intel SDM
13371 27.5.2 Loading Host Segment and Descriptor-Table Registers */
13372 seg = (struct kvm_segment) {
13374 .limit = 0xFFFFFFFF,
13375 .selector = vmcs12->host_cs_selector,
13381 if (vmcs12->vm_exit_controls & VM_EXIT_HOST_ADDR_SPACE_SIZE)
13385 vmx_set_segment(vcpu, &seg, VCPU_SREG_CS);
13386 seg = (struct kvm_segment) {
13388 .limit = 0xFFFFFFFF,
13395 seg.selector = vmcs12->host_ds_selector;
13396 vmx_set_segment(vcpu, &seg, VCPU_SREG_DS);
13397 seg.selector = vmcs12->host_es_selector;
13398 vmx_set_segment(vcpu, &seg, VCPU_SREG_ES);
13399 seg.selector = vmcs12->host_ss_selector;
13400 vmx_set_segment(vcpu, &seg, VCPU_SREG_SS);
13401 seg.selector = vmcs12->host_fs_selector;
13402 seg.base = vmcs12->host_fs_base;
13403 vmx_set_segment(vcpu, &seg, VCPU_SREG_FS);
13404 seg.selector = vmcs12->host_gs_selector;
13405 seg.base = vmcs12->host_gs_base;
13406 vmx_set_segment(vcpu, &seg, VCPU_SREG_GS);
13407 seg = (struct kvm_segment) {
13408 .base = vmcs12->host_tr_base,
13410 .selector = vmcs12->host_tr_selector,
13414 vmx_set_segment(vcpu, &seg, VCPU_SREG_TR);
13416 kvm_set_dr(vcpu, 7, 0x400);
13417 vmcs_write64(GUEST_IA32_DEBUGCTL, 0);
13419 if (cpu_has_vmx_msr_bitmap())
13420 vmx_update_msr_bitmap(vcpu);
13422 if (nested_vmx_load_msr(vcpu, vmcs12->vm_exit_msr_load_addr,
13423 vmcs12->vm_exit_msr_load_count))
13424 nested_vmx_abort(vcpu, VMX_ABORT_LOAD_HOST_MSR_FAIL);
13427 static inline u64 nested_vmx_get_vmcs01_guest_efer(struct vcpu_vmx *vmx)
13429 struct shared_msr_entry *efer_msr;
13432 if (vm_entry_controls_get(vmx) & VM_ENTRY_LOAD_IA32_EFER)
13433 return vmcs_read64(GUEST_IA32_EFER);
13435 if (cpu_has_load_ia32_efer)
13438 for (i = 0; i < vmx->msr_autoload.guest.nr; ++i) {
13439 if (vmx->msr_autoload.guest.val[i].index == MSR_EFER)
13440 return vmx->msr_autoload.guest.val[i].value;
13443 efer_msr = find_msr_entry(vmx, MSR_EFER);
13445 return efer_msr->data;
13450 static void nested_vmx_restore_host_state(struct kvm_vcpu *vcpu)
13452 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
13453 struct vcpu_vmx *vmx = to_vmx(vcpu);
13454 struct vmx_msr_entry g, h;
13455 struct msr_data msr;
13459 vcpu->arch.pat = vmcs_read64(GUEST_IA32_PAT);
13461 if (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS) {
13463 * L1's host DR7 is lost if KVM_GUESTDBG_USE_HW_BP is set
13464 * as vmcs01.GUEST_DR7 contains a userspace defined value
13465 * and vcpu->arch.dr7 is not squirreled away before the
13466 * nested VMENTER (not worth adding a variable in nested_vmx).
13468 if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP)
13469 kvm_set_dr(vcpu, 7, DR7_FIXED_1);
13471 WARN_ON(kvm_set_dr(vcpu, 7, vmcs_readl(GUEST_DR7)));
13475 * Note that calling vmx_set_{efer,cr0,cr4} is important as they
13476 * handle a variety of side effects to KVM's software model.
13478 vmx_set_efer(vcpu, nested_vmx_get_vmcs01_guest_efer(vmx));
13480 vcpu->arch.cr0_guest_owned_bits = X86_CR0_TS;
13481 vmx_set_cr0(vcpu, vmcs_readl(CR0_READ_SHADOW));
13483 vcpu->arch.cr4_guest_owned_bits = ~vmcs_readl(CR4_GUEST_HOST_MASK);
13484 vmx_set_cr4(vcpu, vmcs_readl(CR4_READ_SHADOW));
13486 nested_ept_uninit_mmu_context(vcpu);
13487 vcpu->arch.cr3 = vmcs_readl(GUEST_CR3);
13488 __set_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail);
13491 * Use ept_save_pdptrs(vcpu) to load the MMU's cached PDPTRs
13492 * from vmcs01 (if necessary). The PDPTRs are not loaded on
13493 * VMFail, like everything else we just need to ensure our
13494 * software model is up-to-date.
13496 ept_save_pdptrs(vcpu);
13498 kvm_mmu_reset_context(vcpu);
13500 if (cpu_has_vmx_msr_bitmap())
13501 vmx_update_msr_bitmap(vcpu);
13504 * This nasty bit of open coding is a compromise between blindly
13505 * loading L1's MSRs using the exit load lists (incorrect emulation
13506 * of VMFail), leaving the nested VM's MSRs in the software model
13507 * (incorrect behavior) and snapshotting the modified MSRs (too
13508 * expensive since the lists are unbound by hardware). For each
13509 * MSR that was (prematurely) loaded from the nested VMEntry load
13510 * list, reload it from the exit load list if it exists and differs
13511 * from the guest value. The intent is to stuff host state as
13512 * silently as possible, not to fully process the exit load list.
13514 msr.host_initiated = false;
13515 for (i = 0; i < vmcs12->vm_entry_msr_load_count; i++) {
13516 gpa = vmcs12->vm_entry_msr_load_addr + (i * sizeof(g));
13517 if (kvm_vcpu_read_guest(vcpu, gpa, &g, sizeof(g))) {
13518 pr_debug_ratelimited(
13519 "%s read MSR index failed (%u, 0x%08llx)\n",
13524 for (j = 0; j < vmcs12->vm_exit_msr_load_count; j++) {
13525 gpa = vmcs12->vm_exit_msr_load_addr + (j * sizeof(h));
13526 if (kvm_vcpu_read_guest(vcpu, gpa, &h, sizeof(h))) {
13527 pr_debug_ratelimited(
13528 "%s read MSR failed (%u, 0x%08llx)\n",
13532 if (h.index != g.index)
13534 if (h.value == g.value)
13537 if (nested_vmx_load_msr_check(vcpu, &h)) {
13538 pr_debug_ratelimited(
13539 "%s check failed (%u, 0x%x, 0x%x)\n",
13540 __func__, j, h.index, h.reserved);
13544 msr.index = h.index;
13545 msr.data = h.value;
13546 if (kvm_set_msr(vcpu, &msr)) {
13547 pr_debug_ratelimited(
13548 "%s WRMSR failed (%u, 0x%x, 0x%llx)\n",
13549 __func__, j, h.index, h.value);
13558 nested_vmx_abort(vcpu, VMX_ABORT_LOAD_HOST_MSR_FAIL);
13562 * Emulate an exit from nested guest (L2) to L1, i.e., prepare to run L1
13563 * and modify vmcs12 to make it see what it would expect to see there if
13564 * L2 was its real guest. Must only be called when in L2 (is_guest_mode())
13566 static void nested_vmx_vmexit(struct kvm_vcpu *vcpu, u32 exit_reason,
13567 u32 exit_intr_info,
13568 unsigned long exit_qualification)
13570 struct vcpu_vmx *vmx = to_vmx(vcpu);
13571 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
13573 /* trying to cancel vmlaunch/vmresume is a bug */
13574 WARN_ON_ONCE(vmx->nested.nested_run_pending);
13577 * The only expected VM-instruction error is "VM entry with
13578 * invalid control field(s)." Anything else indicates a
13581 WARN_ON_ONCE(vmx->fail && (vmcs_read32(VM_INSTRUCTION_ERROR) !=
13582 VMXERR_ENTRY_INVALID_CONTROL_FIELD));
13584 leave_guest_mode(vcpu);
13586 if (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING)
13587 vcpu->arch.tsc_offset -= vmcs12->tsc_offset;
13589 if (likely(!vmx->fail)) {
13590 if (exit_reason == -1)
13591 sync_vmcs12(vcpu, vmcs12);
13593 prepare_vmcs12(vcpu, vmcs12, exit_reason, exit_intr_info,
13594 exit_qualification);
13597 * Must happen outside of sync_vmcs12() as it will
13598 * also be used to capture vmcs12 cache as part of
13599 * capturing nVMX state for snapshot (migration).
13601 * Otherwise, this flush will dirty guest memory at a
13602 * point it is already assumed by user-space to be
13605 nested_flush_cached_shadow_vmcs12(vcpu, vmcs12);
13607 if (nested_vmx_store_msr(vcpu, vmcs12->vm_exit_msr_store_addr,
13608 vmcs12->vm_exit_msr_store_count))
13609 nested_vmx_abort(vcpu, VMX_ABORT_SAVE_GUEST_MSR_FAIL);
13613 * Drop events/exceptions that were queued for re-injection to L2
13614 * (picked up via vmx_complete_interrupts()), as well as exceptions
13615 * that were pending for L2. Note, this must NOT be hoisted above
13616 * prepare_vmcs12(), events/exceptions queued for re-injection need to
13617 * be captured in vmcs12 (see vmcs12_save_pending_event()).
13619 vcpu->arch.nmi_injected = false;
13620 kvm_clear_exception_queue(vcpu);
13621 kvm_clear_interrupt_queue(vcpu);
13623 vmx_switch_vmcs(vcpu, &vmx->vmcs01);
13624 vm_entry_controls_reset_shadow(vmx);
13625 vm_exit_controls_reset_shadow(vmx);
13626 vmx_segment_cache_clear(vmx);
13628 /* Update any VMCS fields that might have changed while L2 ran */
13629 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, vmx->msr_autoload.host.nr);
13630 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, vmx->msr_autoload.guest.nr);
13631 vmcs_write64(TSC_OFFSET, vcpu->arch.tsc_offset);
13633 if (kvm_has_tsc_control)
13634 decache_tsc_multiplier(vmx);
13636 if (vmx->nested.change_vmcs01_virtual_apic_mode) {
13637 vmx->nested.change_vmcs01_virtual_apic_mode = false;
13638 vmx_set_virtual_apic_mode(vcpu);
13639 } else if (!nested_cpu_has_ept(vmcs12) &&
13640 nested_cpu_has2(vmcs12,
13641 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) {
13642 vmx_flush_tlb(vcpu, true);
13645 /* This is needed for same reason as it was needed in prepare_vmcs02 */
13648 /* Unpin physical memory we referred to in vmcs02 */
13649 if (vmx->nested.apic_access_page) {
13650 kvm_release_page_dirty(vmx->nested.apic_access_page);
13651 vmx->nested.apic_access_page = NULL;
13653 if (vmx->nested.virtual_apic_page) {
13654 kvm_release_page_dirty(vmx->nested.virtual_apic_page);
13655 vmx->nested.virtual_apic_page = NULL;
13657 if (vmx->nested.pi_desc_page) {
13658 kunmap(vmx->nested.pi_desc_page);
13659 kvm_release_page_dirty(vmx->nested.pi_desc_page);
13660 vmx->nested.pi_desc_page = NULL;
13661 vmx->nested.pi_desc = NULL;
13665 * We are now running in L2, mmu_notifier will force to reload the
13666 * page's hpa for L2 vmcs. Need to reload it for L1 before entering L1.
13668 kvm_make_request(KVM_REQ_APIC_PAGE_RELOAD, vcpu);
13670 if (enable_shadow_vmcs && exit_reason != -1)
13671 vmx->nested.sync_shadow_vmcs = true;
13673 /* in case we halted in L2 */
13674 vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
13676 if (likely(!vmx->fail)) {
13677 if (exit_reason == EXIT_REASON_EXTERNAL_INTERRUPT &&
13678 nested_exit_intr_ack_set(vcpu)) {
13679 int irq = kvm_cpu_get_interrupt(vcpu);
13681 vmcs12->vm_exit_intr_info = irq |
13682 INTR_INFO_VALID_MASK | INTR_TYPE_EXT_INTR;
13685 if (exit_reason != -1)
13686 trace_kvm_nested_vmexit_inject(vmcs12->vm_exit_reason,
13687 vmcs12->exit_qualification,
13688 vmcs12->idt_vectoring_info_field,
13689 vmcs12->vm_exit_intr_info,
13690 vmcs12->vm_exit_intr_error_code,
13693 load_vmcs12_host_state(vcpu, vmcs12);
13699 * After an early L2 VM-entry failure, we're now back
13700 * in L1 which thinks it just finished a VMLAUNCH or
13701 * VMRESUME instruction, so we need to set the failure
13702 * flag and the VM-instruction error field of the VMCS
13705 nested_vmx_failValid(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD);
13708 * Restore L1's host state to KVM's software model. We're here
13709 * because a consistency check was caught by hardware, which
13710 * means some amount of guest state has been propagated to KVM's
13711 * model and needs to be unwound to the host's state.
13713 nested_vmx_restore_host_state(vcpu);
13716 * The emulated instruction was already skipped in
13717 * nested_vmx_run, but the updated RIP was never
13718 * written back to the vmcs01.
13720 skip_emulated_instruction(vcpu);
13725 * Forcibly leave nested mode in order to be able to reset the VCPU later on.
13727 static void vmx_leave_nested(struct kvm_vcpu *vcpu)
13729 if (is_guest_mode(vcpu)) {
13730 to_vmx(vcpu)->nested.nested_run_pending = 0;
13731 nested_vmx_vmexit(vcpu, -1, 0, 0);
13733 free_nested(to_vmx(vcpu));
13737 * L1's failure to enter L2 is a subset of a normal exit, as explained in
13738 * 23.7 "VM-entry failures during or after loading guest state" (this also
13739 * lists the acceptable exit-reason and exit-qualification parameters).
13740 * It should only be called before L2 actually succeeded to run, and when
13741 * vmcs01 is current (it doesn't leave_guest_mode() or switch vmcss).
13743 static void nested_vmx_entry_failure(struct kvm_vcpu *vcpu,
13744 struct vmcs12 *vmcs12,
13745 u32 reason, unsigned long qualification)
13747 load_vmcs12_host_state(vcpu, vmcs12);
13748 vmcs12->vm_exit_reason = reason | VMX_EXIT_REASONS_FAILED_VMENTRY;
13749 vmcs12->exit_qualification = qualification;
13750 nested_vmx_succeed(vcpu);
13751 if (enable_shadow_vmcs)
13752 to_vmx(vcpu)->nested.sync_shadow_vmcs = true;
13755 static int vmx_check_intercept_io(struct kvm_vcpu *vcpu,
13756 struct x86_instruction_info *info)
13758 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
13759 unsigned short port;
13763 if (info->intercept == x86_intercept_in ||
13764 info->intercept == x86_intercept_ins) {
13765 port = info->src_val;
13766 size = info->dst_bytes;
13768 port = info->dst_val;
13769 size = info->src_bytes;
13773 * If the 'use IO bitmaps' VM-execution control is 0, IO instruction
13774 * VM-exits depend on the 'unconditional IO exiting' VM-execution
13777 * Otherwise, IO instruction VM-exits are controlled by the IO bitmaps.
13779 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_IO_BITMAPS))
13780 intercept = nested_cpu_has(vmcs12,
13781 CPU_BASED_UNCOND_IO_EXITING);
13783 intercept = nested_vmx_check_io_bitmaps(vcpu, port, size);
13785 /* FIXME: produce nested vmexit and return X86EMUL_INTERCEPTED. */
13786 return intercept ? X86EMUL_UNHANDLEABLE : X86EMUL_CONTINUE;
13789 static int vmx_check_intercept(struct kvm_vcpu *vcpu,
13790 struct x86_instruction_info *info,
13791 enum x86_intercept_stage stage)
13793 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
13794 struct x86_emulate_ctxt *ctxt = &vcpu->arch.emulate_ctxt;
13796 switch (info->intercept) {
13798 * RDPID causes #UD if disabled through secondary execution controls.
13799 * Because it is marked as EmulateOnUD, we need to intercept it here.
13801 case x86_intercept_rdtscp:
13802 if (!nested_cpu_has2(vmcs12, SECONDARY_EXEC_RDTSCP)) {
13803 ctxt->exception.vector = UD_VECTOR;
13804 ctxt->exception.error_code_valid = false;
13805 return X86EMUL_PROPAGATE_FAULT;
13809 case x86_intercept_in:
13810 case x86_intercept_ins:
13811 case x86_intercept_out:
13812 case x86_intercept_outs:
13813 return vmx_check_intercept_io(vcpu, info);
13815 case x86_intercept_lgdt:
13816 case x86_intercept_lidt:
13817 case x86_intercept_lldt:
13818 case x86_intercept_ltr:
13819 case x86_intercept_sgdt:
13820 case x86_intercept_sidt:
13821 case x86_intercept_sldt:
13822 case x86_intercept_str:
13823 if (!nested_cpu_has2(vmcs12, SECONDARY_EXEC_DESC))
13824 return X86EMUL_CONTINUE;
13826 /* FIXME: produce nested vmexit and return X86EMUL_INTERCEPTED. */
13829 /* TODO: check more intercepts... */
13834 return X86EMUL_UNHANDLEABLE;
13837 #ifdef CONFIG_X86_64
13838 /* (a << shift) / divisor, return 1 if overflow otherwise 0 */
13839 static inline int u64_shl_div_u64(u64 a, unsigned int shift,
13840 u64 divisor, u64 *result)
13842 u64 low = a << shift, high = a >> (64 - shift);
13844 /* To avoid the overflow on divq */
13845 if (high >= divisor)
13848 /* Low hold the result, high hold rem which is discarded */
13849 asm("divq %2\n\t" : "=a" (low), "=d" (high) :
13850 "rm" (divisor), "0" (low), "1" (high));
13856 static int vmx_set_hv_timer(struct kvm_vcpu *vcpu, u64 guest_deadline_tsc)
13858 struct vcpu_vmx *vmx;
13859 u64 tscl, guest_tscl, delta_tsc, lapic_timer_advance_cycles;
13861 if (kvm_mwait_in_guest(vcpu->kvm))
13862 return -EOPNOTSUPP;
13864 vmx = to_vmx(vcpu);
13866 guest_tscl = kvm_read_l1_tsc(vcpu, tscl);
13867 delta_tsc = max(guest_deadline_tsc, guest_tscl) - guest_tscl;
13868 lapic_timer_advance_cycles = nsec_to_cycles(vcpu, lapic_timer_advance_ns);
13870 if (delta_tsc > lapic_timer_advance_cycles)
13871 delta_tsc -= lapic_timer_advance_cycles;
13875 /* Convert to host delta tsc if tsc scaling is enabled */
13876 if (vcpu->arch.tsc_scaling_ratio != kvm_default_tsc_scaling_ratio &&
13877 u64_shl_div_u64(delta_tsc,
13878 kvm_tsc_scaling_ratio_frac_bits,
13879 vcpu->arch.tsc_scaling_ratio,
13884 * If the delta tsc can't fit in the 32 bit after the multi shift,
13885 * we can't use the preemption timer.
13886 * It's possible that it fits on later vmentries, but checking
13887 * on every vmentry is costly so we just use an hrtimer.
13889 if (delta_tsc >> (cpu_preemption_timer_multi + 32))
13892 vmx->hv_deadline_tsc = tscl + delta_tsc;
13893 return delta_tsc == 0;
13896 static void vmx_cancel_hv_timer(struct kvm_vcpu *vcpu)
13898 to_vmx(vcpu)->hv_deadline_tsc = -1;
13902 static void vmx_sched_in(struct kvm_vcpu *vcpu, int cpu)
13904 if (!kvm_pause_in_guest(vcpu->kvm))
13905 shrink_ple_window(vcpu);
13908 static void vmx_slot_enable_log_dirty(struct kvm *kvm,
13909 struct kvm_memory_slot *slot)
13911 kvm_mmu_slot_leaf_clear_dirty(kvm, slot);
13912 kvm_mmu_slot_largepage_remove_write_access(kvm, slot);
13915 static void vmx_slot_disable_log_dirty(struct kvm *kvm,
13916 struct kvm_memory_slot *slot)
13918 kvm_mmu_slot_set_dirty(kvm, slot);
13921 static void vmx_flush_log_dirty(struct kvm *kvm)
13923 kvm_flush_pml_buffers(kvm);
13926 static int vmx_write_pml_buffer(struct kvm_vcpu *vcpu, gpa_t gpa)
13928 struct vmcs12 *vmcs12;
13929 struct vcpu_vmx *vmx = to_vmx(vcpu);
13930 struct page *page = NULL;
13933 if (is_guest_mode(vcpu)) {
13934 WARN_ON_ONCE(vmx->nested.pml_full);
13937 * Check if PML is enabled for the nested guest.
13938 * Whether eptp bit 6 is set is already checked
13939 * as part of A/D emulation.
13941 vmcs12 = get_vmcs12(vcpu);
13942 if (!nested_cpu_has_pml(vmcs12))
13945 if (vmcs12->guest_pml_index >= PML_ENTITY_NUM) {
13946 vmx->nested.pml_full = true;
13952 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->pml_address);
13953 if (is_error_page(page))
13956 pml_address = kmap(page);
13957 pml_address[vmcs12->guest_pml_index--] = gpa;
13959 kvm_release_page_clean(page);
13965 static void vmx_enable_log_dirty_pt_masked(struct kvm *kvm,
13966 struct kvm_memory_slot *memslot,
13967 gfn_t offset, unsigned long mask)
13969 kvm_mmu_clear_dirty_pt_masked(kvm, memslot, offset, mask);
13972 static void __pi_post_block(struct kvm_vcpu *vcpu)
13974 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
13975 struct pi_desc old, new;
13979 old.control = new.control = pi_desc->control;
13980 WARN(old.nv != POSTED_INTR_WAKEUP_VECTOR,
13981 "Wakeup handler not enabled while the VCPU is blocked\n");
13983 dest = cpu_physical_id(vcpu->cpu);
13985 if (x2apic_enabled())
13988 new.ndst = (dest << 8) & 0xFF00;
13990 /* set 'NV' to 'notification vector' */
13991 new.nv = POSTED_INTR_VECTOR;
13992 } while (cmpxchg64(&pi_desc->control, old.control,
13993 new.control) != old.control);
13995 if (!WARN_ON_ONCE(vcpu->pre_pcpu == -1)) {
13996 spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
13997 list_del(&vcpu->blocked_vcpu_list);
13998 spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
13999 vcpu->pre_pcpu = -1;
14004 * This routine does the following things for vCPU which is going
14005 * to be blocked if VT-d PI is enabled.
14006 * - Store the vCPU to the wakeup list, so when interrupts happen
14007 * we can find the right vCPU to wake up.
14008 * - Change the Posted-interrupt descriptor as below:
14009 * 'NDST' <-- vcpu->pre_pcpu
14010 * 'NV' <-- POSTED_INTR_WAKEUP_VECTOR
14011 * - If 'ON' is set during this process, which means at least one
14012 * interrupt is posted for this vCPU, we cannot block it, in
14013 * this case, return 1, otherwise, return 0.
14016 static int pi_pre_block(struct kvm_vcpu *vcpu)
14019 struct pi_desc old, new;
14020 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
14022 if (!kvm_arch_has_assigned_device(vcpu->kvm) ||
14023 !irq_remapping_cap(IRQ_POSTING_CAP) ||
14024 !kvm_vcpu_apicv_active(vcpu))
14027 WARN_ON(irqs_disabled());
14028 local_irq_disable();
14029 if (!WARN_ON_ONCE(vcpu->pre_pcpu != -1)) {
14030 vcpu->pre_pcpu = vcpu->cpu;
14031 spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
14032 list_add_tail(&vcpu->blocked_vcpu_list,
14033 &per_cpu(blocked_vcpu_on_cpu,
14035 spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
14039 old.control = new.control = pi_desc->control;
14041 WARN((pi_desc->sn == 1),
14042 "Warning: SN field of posted-interrupts "
14043 "is set before blocking\n");
14046 * Since vCPU can be preempted during this process,
14047 * vcpu->cpu could be different with pre_pcpu, we
14048 * need to set pre_pcpu as the destination of wakeup
14049 * notification event, then we can find the right vCPU
14050 * to wakeup in wakeup handler if interrupts happen
14051 * when the vCPU is in blocked state.
14053 dest = cpu_physical_id(vcpu->pre_pcpu);
14055 if (x2apic_enabled())
14058 new.ndst = (dest << 8) & 0xFF00;
14060 /* set 'NV' to 'wakeup vector' */
14061 new.nv = POSTED_INTR_WAKEUP_VECTOR;
14062 } while (cmpxchg64(&pi_desc->control, old.control,
14063 new.control) != old.control);
14065 /* We should not block the vCPU if an interrupt is posted for it. */
14066 if (pi_test_on(pi_desc) == 1)
14067 __pi_post_block(vcpu);
14069 local_irq_enable();
14070 return (vcpu->pre_pcpu == -1);
14073 static int vmx_pre_block(struct kvm_vcpu *vcpu)
14075 if (pi_pre_block(vcpu))
14078 if (kvm_lapic_hv_timer_in_use(vcpu))
14079 kvm_lapic_switch_to_sw_timer(vcpu);
14084 static void pi_post_block(struct kvm_vcpu *vcpu)
14086 if (vcpu->pre_pcpu == -1)
14089 WARN_ON(irqs_disabled());
14090 local_irq_disable();
14091 __pi_post_block(vcpu);
14092 local_irq_enable();
14095 static void vmx_post_block(struct kvm_vcpu *vcpu)
14097 if (kvm_x86_ops->set_hv_timer)
14098 kvm_lapic_switch_to_hv_timer(vcpu);
14100 pi_post_block(vcpu);
14104 * vmx_update_pi_irte - set IRTE for Posted-Interrupts
14107 * @host_irq: host irq of the interrupt
14108 * @guest_irq: gsi of the interrupt
14109 * @set: set or unset PI
14110 * returns 0 on success, < 0 on failure
14112 static int vmx_update_pi_irte(struct kvm *kvm, unsigned int host_irq,
14113 uint32_t guest_irq, bool set)
14115 struct kvm_kernel_irq_routing_entry *e;
14116 struct kvm_irq_routing_table *irq_rt;
14117 struct kvm_lapic_irq irq;
14118 struct kvm_vcpu *vcpu;
14119 struct vcpu_data vcpu_info;
14122 if (!kvm_arch_has_assigned_device(kvm) ||
14123 !irq_remapping_cap(IRQ_POSTING_CAP) ||
14124 !kvm_vcpu_apicv_active(kvm->vcpus[0]))
14127 idx = srcu_read_lock(&kvm->irq_srcu);
14128 irq_rt = srcu_dereference(kvm->irq_routing, &kvm->irq_srcu);
14129 if (guest_irq >= irq_rt->nr_rt_entries ||
14130 hlist_empty(&irq_rt->map[guest_irq])) {
14131 pr_warn_once("no route for guest_irq %u/%u (broken user space?)\n",
14132 guest_irq, irq_rt->nr_rt_entries);
14136 hlist_for_each_entry(e, &irq_rt->map[guest_irq], link) {
14137 if (e->type != KVM_IRQ_ROUTING_MSI)
14140 * VT-d PI cannot support posting multicast/broadcast
14141 * interrupts to a vCPU, we still use interrupt remapping
14142 * for these kind of interrupts.
14144 * For lowest-priority interrupts, we only support
14145 * those with single CPU as the destination, e.g. user
14146 * configures the interrupts via /proc/irq or uses
14147 * irqbalance to make the interrupts single-CPU.
14149 * We will support full lowest-priority interrupt later.
14152 kvm_set_msi_irq(kvm, e, &irq);
14153 if (!kvm_intr_is_single_vcpu(kvm, &irq, &vcpu)) {
14155 * Make sure the IRTE is in remapped mode if
14156 * we don't handle it in posted mode.
14158 ret = irq_set_vcpu_affinity(host_irq, NULL);
14161 "failed to back to remapped mode, irq: %u\n",
14169 vcpu_info.pi_desc_addr = __pa(vcpu_to_pi_desc(vcpu));
14170 vcpu_info.vector = irq.vector;
14172 trace_kvm_pi_irte_update(host_irq, vcpu->vcpu_id, e->gsi,
14173 vcpu_info.vector, vcpu_info.pi_desc_addr, set);
14176 ret = irq_set_vcpu_affinity(host_irq, &vcpu_info);
14178 ret = irq_set_vcpu_affinity(host_irq, NULL);
14181 printk(KERN_INFO "%s: failed to update PI IRTE\n",
14189 srcu_read_unlock(&kvm->irq_srcu, idx);
14193 static void vmx_setup_mce(struct kvm_vcpu *vcpu)
14195 if (vcpu->arch.mcg_cap & MCG_LMCE_P)
14196 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits |=
14197 FEATURE_CONTROL_LMCE;
14199 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits &=
14200 ~FEATURE_CONTROL_LMCE;
14203 static int vmx_smi_allowed(struct kvm_vcpu *vcpu)
14205 /* we need a nested vmexit to enter SMM, postpone if run is pending */
14206 if (to_vmx(vcpu)->nested.nested_run_pending)
14211 static int vmx_pre_enter_smm(struct kvm_vcpu *vcpu, char *smstate)
14213 struct vcpu_vmx *vmx = to_vmx(vcpu);
14215 vmx->nested.smm.guest_mode = is_guest_mode(vcpu);
14216 if (vmx->nested.smm.guest_mode)
14217 nested_vmx_vmexit(vcpu, -1, 0, 0);
14219 vmx->nested.smm.vmxon = vmx->nested.vmxon;
14220 vmx->nested.vmxon = false;
14221 vmx_clear_hlt(vcpu);
14225 static int vmx_pre_leave_smm(struct kvm_vcpu *vcpu, u64 smbase)
14227 struct vcpu_vmx *vmx = to_vmx(vcpu);
14230 if (vmx->nested.smm.vmxon) {
14231 vmx->nested.vmxon = true;
14232 vmx->nested.smm.vmxon = false;
14235 if (vmx->nested.smm.guest_mode) {
14236 vcpu->arch.hflags &= ~HF_SMM_MASK;
14237 ret = enter_vmx_non_root_mode(vcpu, NULL);
14238 vcpu->arch.hflags |= HF_SMM_MASK;
14242 vmx->nested.smm.guest_mode = false;
14247 static int enable_smi_window(struct kvm_vcpu *vcpu)
14252 static int vmx_get_nested_state(struct kvm_vcpu *vcpu,
14253 struct kvm_nested_state __user *user_kvm_nested_state,
14254 u32 user_data_size)
14256 struct vcpu_vmx *vmx;
14257 struct vmcs12 *vmcs12;
14258 struct kvm_nested_state kvm_state = {
14261 .size = sizeof(kvm_state),
14262 .vmx.vmxon_pa = -1ull,
14263 .vmx.vmcs_pa = -1ull,
14267 return kvm_state.size + 2 * VMCS12_SIZE;
14269 vmx = to_vmx(vcpu);
14270 vmcs12 = get_vmcs12(vcpu);
14271 if (nested_vmx_allowed(vcpu) &&
14272 (vmx->nested.vmxon || vmx->nested.smm.vmxon)) {
14273 kvm_state.vmx.vmxon_pa = vmx->nested.vmxon_ptr;
14274 kvm_state.vmx.vmcs_pa = vmx->nested.current_vmptr;
14276 if (vmx->nested.current_vmptr != -1ull) {
14277 kvm_state.size += VMCS12_SIZE;
14279 if (is_guest_mode(vcpu) &&
14280 nested_cpu_has_shadow_vmcs(vmcs12) &&
14281 vmcs12->vmcs_link_pointer != -1ull)
14282 kvm_state.size += VMCS12_SIZE;
14285 if (vmx->nested.smm.vmxon)
14286 kvm_state.vmx.smm.flags |= KVM_STATE_NESTED_SMM_VMXON;
14288 if (vmx->nested.smm.guest_mode)
14289 kvm_state.vmx.smm.flags |= KVM_STATE_NESTED_SMM_GUEST_MODE;
14291 if (is_guest_mode(vcpu)) {
14292 kvm_state.flags |= KVM_STATE_NESTED_GUEST_MODE;
14294 if (vmx->nested.nested_run_pending)
14295 kvm_state.flags |= KVM_STATE_NESTED_RUN_PENDING;
14299 if (user_data_size < kvm_state.size)
14302 if (copy_to_user(user_kvm_nested_state, &kvm_state, sizeof(kvm_state)))
14305 if (vmx->nested.current_vmptr == -1ull)
14309 * When running L2, the authoritative vmcs12 state is in the
14310 * vmcs02. When running L1, the authoritative vmcs12 state is
14311 * in the shadow vmcs linked to vmcs01, unless
14312 * sync_shadow_vmcs is set, in which case, the authoritative
14313 * vmcs12 state is in the vmcs12 already.
14315 if (is_guest_mode(vcpu))
14316 sync_vmcs12(vcpu, vmcs12);
14317 else if (enable_shadow_vmcs && !vmx->nested.sync_shadow_vmcs)
14318 copy_shadow_to_vmcs12(vmx);
14321 * Copy over the full allocated size of vmcs12 rather than just the size
14324 if (copy_to_user(user_kvm_nested_state->data, vmcs12, VMCS12_SIZE))
14327 if (nested_cpu_has_shadow_vmcs(vmcs12) &&
14328 vmcs12->vmcs_link_pointer != -1ull) {
14329 if (copy_to_user(user_kvm_nested_state->data + VMCS12_SIZE,
14330 get_shadow_vmcs12(vcpu), VMCS12_SIZE))
14335 return kvm_state.size;
14338 static int vmx_set_nested_state(struct kvm_vcpu *vcpu,
14339 struct kvm_nested_state __user *user_kvm_nested_state,
14340 struct kvm_nested_state *kvm_state)
14342 struct vcpu_vmx *vmx = to_vmx(vcpu);
14343 struct vmcs12 *vmcs12;
14347 if (kvm_state->format != 0)
14350 if (!nested_vmx_allowed(vcpu))
14351 return kvm_state->vmx.vmxon_pa == -1ull ? 0 : -EINVAL;
14353 if (kvm_state->vmx.vmxon_pa == -1ull) {
14354 if (kvm_state->vmx.smm.flags)
14357 if (kvm_state->vmx.vmcs_pa != -1ull)
14360 vmx_leave_nested(vcpu);
14364 if (!page_address_valid(vcpu, kvm_state->vmx.vmxon_pa))
14367 if ((kvm_state->vmx.smm.flags & KVM_STATE_NESTED_SMM_GUEST_MODE) &&
14368 (kvm_state->flags & KVM_STATE_NESTED_GUEST_MODE))
14371 if (kvm_state->vmx.smm.flags &
14372 ~(KVM_STATE_NESTED_SMM_GUEST_MODE | KVM_STATE_NESTED_SMM_VMXON))
14376 * SMM temporarily disables VMX, so we cannot be in guest mode,
14377 * nor can VMLAUNCH/VMRESUME be pending. Outside SMM, SMM flags
14380 if (is_smm(vcpu) ? kvm_state->flags : kvm_state->vmx.smm.flags)
14383 if ((kvm_state->vmx.smm.flags & KVM_STATE_NESTED_SMM_GUEST_MODE) &&
14384 !(kvm_state->vmx.smm.flags & KVM_STATE_NESTED_SMM_VMXON))
14387 vmx_leave_nested(vcpu);
14388 if (kvm_state->vmx.vmxon_pa == -1ull)
14391 vmx->nested.vmxon_ptr = kvm_state->vmx.vmxon_pa;
14392 ret = enter_vmx_operation(vcpu);
14396 /* Empty 'VMXON' state is permitted */
14397 if (kvm_state->size < sizeof(*kvm_state) + sizeof(*vmcs12))
14400 if (kvm_state->vmx.vmcs_pa == kvm_state->vmx.vmxon_pa ||
14401 !page_address_valid(vcpu, kvm_state->vmx.vmcs_pa))
14404 set_current_vmptr(vmx, kvm_state->vmx.vmcs_pa);
14406 if (kvm_state->vmx.smm.flags & KVM_STATE_NESTED_SMM_VMXON) {
14407 vmx->nested.smm.vmxon = true;
14408 vmx->nested.vmxon = false;
14410 if (kvm_state->vmx.smm.flags & KVM_STATE_NESTED_SMM_GUEST_MODE)
14411 vmx->nested.smm.guest_mode = true;
14414 vmcs12 = get_vmcs12(vcpu);
14415 if (copy_from_user(vmcs12, user_kvm_nested_state->data, sizeof(*vmcs12)))
14418 if (vmcs12->hdr.revision_id != VMCS12_REVISION)
14421 if (!(kvm_state->flags & KVM_STATE_NESTED_GUEST_MODE))
14424 vmx->nested.nested_run_pending =
14425 !!(kvm_state->flags & KVM_STATE_NESTED_RUN_PENDING);
14427 if (nested_cpu_has_shadow_vmcs(vmcs12) &&
14428 vmcs12->vmcs_link_pointer != -1ull) {
14429 struct vmcs12 *shadow_vmcs12 = get_shadow_vmcs12(vcpu);
14430 if (kvm_state->size < sizeof(*kvm_state) + 2 * sizeof(*vmcs12))
14433 if (copy_from_user(shadow_vmcs12,
14434 user_kvm_nested_state->data + VMCS12_SIZE,
14438 if (shadow_vmcs12->hdr.revision_id != VMCS12_REVISION ||
14439 !shadow_vmcs12->hdr.shadow_vmcs)
14443 if (check_vmentry_prereqs(vcpu, vmcs12) ||
14444 check_vmentry_postreqs(vcpu, vmcs12, &exit_qual))
14447 vmx->nested.dirty_vmcs12 = true;
14448 ret = enter_vmx_non_root_mode(vcpu, NULL);
14455 static struct kvm_x86_ops vmx_x86_ops __ro_after_init = {
14456 .cpu_has_kvm_support = cpu_has_kvm_support,
14457 .disabled_by_bios = vmx_disabled_by_bios,
14458 .hardware_setup = hardware_setup,
14459 .hardware_unsetup = hardware_unsetup,
14460 .check_processor_compatibility = vmx_check_processor_compat,
14461 .hardware_enable = hardware_enable,
14462 .hardware_disable = hardware_disable,
14463 .cpu_has_accelerated_tpr = report_flexpriority,
14464 .has_emulated_msr = vmx_has_emulated_msr,
14466 .vm_init = vmx_vm_init,
14467 .vm_alloc = vmx_vm_alloc,
14468 .vm_free = vmx_vm_free,
14470 .vcpu_create = vmx_create_vcpu,
14471 .vcpu_free = vmx_free_vcpu,
14472 .vcpu_reset = vmx_vcpu_reset,
14474 .prepare_guest_switch = vmx_prepare_switch_to_guest,
14475 .vcpu_load = vmx_vcpu_load,
14476 .vcpu_put = vmx_vcpu_put,
14478 .update_bp_intercept = update_exception_bitmap,
14479 .get_msr_feature = vmx_get_msr_feature,
14480 .get_msr = vmx_get_msr,
14481 .set_msr = vmx_set_msr,
14482 .get_segment_base = vmx_get_segment_base,
14483 .get_segment = vmx_get_segment,
14484 .set_segment = vmx_set_segment,
14485 .get_cpl = vmx_get_cpl,
14486 .get_cs_db_l_bits = vmx_get_cs_db_l_bits,
14487 .decache_cr0_guest_bits = vmx_decache_cr0_guest_bits,
14488 .decache_cr3 = vmx_decache_cr3,
14489 .decache_cr4_guest_bits = vmx_decache_cr4_guest_bits,
14490 .set_cr0 = vmx_set_cr0,
14491 .set_cr3 = vmx_set_cr3,
14492 .set_cr4 = vmx_set_cr4,
14493 .set_efer = vmx_set_efer,
14494 .get_idt = vmx_get_idt,
14495 .set_idt = vmx_set_idt,
14496 .get_gdt = vmx_get_gdt,
14497 .set_gdt = vmx_set_gdt,
14498 .get_dr6 = vmx_get_dr6,
14499 .set_dr6 = vmx_set_dr6,
14500 .set_dr7 = vmx_set_dr7,
14501 .sync_dirty_debug_regs = vmx_sync_dirty_debug_regs,
14502 .cache_reg = vmx_cache_reg,
14503 .get_rflags = vmx_get_rflags,
14504 .set_rflags = vmx_set_rflags,
14506 .tlb_flush = vmx_flush_tlb,
14507 .tlb_flush_gva = vmx_flush_tlb_gva,
14509 .run = vmx_vcpu_run,
14510 .handle_exit = vmx_handle_exit,
14511 .skip_emulated_instruction = skip_emulated_instruction,
14512 .set_interrupt_shadow = vmx_set_interrupt_shadow,
14513 .get_interrupt_shadow = vmx_get_interrupt_shadow,
14514 .patch_hypercall = vmx_patch_hypercall,
14515 .set_irq = vmx_inject_irq,
14516 .set_nmi = vmx_inject_nmi,
14517 .queue_exception = vmx_queue_exception,
14518 .cancel_injection = vmx_cancel_injection,
14519 .interrupt_allowed = vmx_interrupt_allowed,
14520 .nmi_allowed = vmx_nmi_allowed,
14521 .get_nmi_mask = vmx_get_nmi_mask,
14522 .set_nmi_mask = vmx_set_nmi_mask,
14523 .enable_nmi_window = enable_nmi_window,
14524 .enable_irq_window = enable_irq_window,
14525 .update_cr8_intercept = update_cr8_intercept,
14526 .set_virtual_apic_mode = vmx_set_virtual_apic_mode,
14527 .set_apic_access_page_addr = vmx_set_apic_access_page_addr,
14528 .get_enable_apicv = vmx_get_enable_apicv,
14529 .refresh_apicv_exec_ctrl = vmx_refresh_apicv_exec_ctrl,
14530 .load_eoi_exitmap = vmx_load_eoi_exitmap,
14531 .apicv_post_state_restore = vmx_apicv_post_state_restore,
14532 .hwapic_irr_update = vmx_hwapic_irr_update,
14533 .hwapic_isr_update = vmx_hwapic_isr_update,
14534 .guest_apic_has_interrupt = vmx_guest_apic_has_interrupt,
14535 .sync_pir_to_irr = vmx_sync_pir_to_irr,
14536 .deliver_posted_interrupt = vmx_deliver_posted_interrupt,
14537 .dy_apicv_has_pending_interrupt = vmx_dy_apicv_has_pending_interrupt,
14539 .set_tss_addr = vmx_set_tss_addr,
14540 .set_identity_map_addr = vmx_set_identity_map_addr,
14541 .get_tdp_level = get_ept_level,
14542 .get_mt_mask = vmx_get_mt_mask,
14544 .get_exit_info = vmx_get_exit_info,
14546 .get_lpage_level = vmx_get_lpage_level,
14548 .cpuid_update = vmx_cpuid_update,
14550 .rdtscp_supported = vmx_rdtscp_supported,
14551 .invpcid_supported = vmx_invpcid_supported,
14553 .set_supported_cpuid = vmx_set_supported_cpuid,
14555 .has_wbinvd_exit = cpu_has_vmx_wbinvd_exit,
14557 .read_l1_tsc_offset = vmx_read_l1_tsc_offset,
14558 .write_l1_tsc_offset = vmx_write_l1_tsc_offset,
14560 .set_tdp_cr3 = vmx_set_cr3,
14562 .check_intercept = vmx_check_intercept,
14563 .handle_external_intr = vmx_handle_external_intr,
14564 .mpx_supported = vmx_mpx_supported,
14565 .xsaves_supported = vmx_xsaves_supported,
14566 .umip_emulated = vmx_umip_emulated,
14568 .check_nested_events = vmx_check_nested_events,
14569 .request_immediate_exit = vmx_request_immediate_exit,
14571 .sched_in = vmx_sched_in,
14573 .slot_enable_log_dirty = vmx_slot_enable_log_dirty,
14574 .slot_disable_log_dirty = vmx_slot_disable_log_dirty,
14575 .flush_log_dirty = vmx_flush_log_dirty,
14576 .enable_log_dirty_pt_masked = vmx_enable_log_dirty_pt_masked,
14577 .write_log_dirty = vmx_write_pml_buffer,
14579 .pre_block = vmx_pre_block,
14580 .post_block = vmx_post_block,
14582 .pmu_ops = &intel_pmu_ops,
14584 .update_pi_irte = vmx_update_pi_irte,
14586 #ifdef CONFIG_X86_64
14587 .set_hv_timer = vmx_set_hv_timer,
14588 .cancel_hv_timer = vmx_cancel_hv_timer,
14591 .setup_mce = vmx_setup_mce,
14593 .get_nested_state = vmx_get_nested_state,
14594 .set_nested_state = vmx_set_nested_state,
14595 .get_vmcs12_pages = nested_get_vmcs12_pages,
14597 .smi_allowed = vmx_smi_allowed,
14598 .pre_enter_smm = vmx_pre_enter_smm,
14599 .pre_leave_smm = vmx_pre_leave_smm,
14600 .enable_smi_window = enable_smi_window,
14603 static void vmx_cleanup_l1d_flush(void)
14605 if (vmx_l1d_flush_pages) {
14606 free_pages((unsigned long)vmx_l1d_flush_pages, L1D_CACHE_ORDER);
14607 vmx_l1d_flush_pages = NULL;
14609 /* Restore state so sysfs ignores VMX */
14610 l1tf_vmx_mitigation = VMENTER_L1D_FLUSH_AUTO;
14613 static void vmx_exit(void)
14615 #ifdef CONFIG_KEXEC_CORE
14616 RCU_INIT_POINTER(crash_vmclear_loaded_vmcss, NULL);
14622 #if IS_ENABLED(CONFIG_HYPERV)
14623 if (static_branch_unlikely(&enable_evmcs)) {
14625 struct hv_vp_assist_page *vp_ap;
14627 * Reset everything to support using non-enlightened VMCS
14628 * access later (e.g. when we reload the module with
14629 * enlightened_vmcs=0)
14631 for_each_online_cpu(cpu) {
14632 vp_ap = hv_get_vp_assist_page(cpu);
14637 vp_ap->current_nested_vmcs = 0;
14638 vp_ap->enlighten_vmentry = 0;
14641 static_branch_disable(&enable_evmcs);
14644 vmx_cleanup_l1d_flush();
14646 module_exit(vmx_exit);
14648 static int __init vmx_init(void)
14652 #if IS_ENABLED(CONFIG_HYPERV)
14654 * Enlightened VMCS usage should be recommended and the host needs
14655 * to support eVMCS v1 or above. We can also disable eVMCS support
14656 * with module parameter.
14658 if (enlightened_vmcs &&
14659 ms_hyperv.hints & HV_X64_ENLIGHTENED_VMCS_RECOMMENDED &&
14660 (ms_hyperv.nested_features & HV_X64_ENLIGHTENED_VMCS_VERSION) >=
14661 KVM_EVMCS_VERSION) {
14664 /* Check that we have assist pages on all online CPUs */
14665 for_each_online_cpu(cpu) {
14666 if (!hv_get_vp_assist_page(cpu)) {
14667 enlightened_vmcs = false;
14672 if (enlightened_vmcs) {
14673 pr_info("KVM: vmx: using Hyper-V Enlightened VMCS\n");
14674 static_branch_enable(&enable_evmcs);
14677 enlightened_vmcs = false;
14681 r = kvm_init(&vmx_x86_ops, sizeof(struct vcpu_vmx),
14682 __alignof__(struct vcpu_vmx), THIS_MODULE);
14687 * Must be called after kvm_init() so enable_ept is properly set
14688 * up. Hand the parameter mitigation value in which was stored in
14689 * the pre module init parser. If no parameter was given, it will
14690 * contain 'auto' which will be turned into the default 'cond'
14693 if (boot_cpu_has(X86_BUG_L1TF)) {
14694 r = vmx_setup_l1d_flush(vmentry_l1d_flush_param);
14701 vmx_setup_fb_clear_ctrl();
14703 for_each_possible_cpu(cpu) {
14704 INIT_LIST_HEAD(&per_cpu(loaded_vmcss_on_cpu, cpu));
14706 INIT_LIST_HEAD(&per_cpu(blocked_vcpu_on_cpu, cpu));
14707 spin_lock_init(&per_cpu(blocked_vcpu_on_cpu_lock, cpu));
14710 #ifdef CONFIG_KEXEC_CORE
14711 rcu_assign_pointer(crash_vmclear_loaded_vmcss,
14712 crash_vmclear_local_loaded_vmcss);
14714 vmx_check_vmcs12_offsets();
14718 module_init(vmx_init);