2 * Kernel-based Virtual Machine driver for Linux
4 * This module enables machines with Intel VT-x extensions to run virtual
5 * machines without emulation or binary translation.
7 * Copyright (C) 2006 Qumranet, Inc.
8 * Copyright 2010 Red Hat, Inc. and/or its affiliates.
11 * Avi Kivity <avi@qumranet.com>
12 * Yaniv Kamay <yaniv@qumranet.com>
14 * This work is licensed under the terms of the GNU GPL, version 2. See
15 * the COPYING file in the top-level directory.
24 #include <linux/kvm_host.h>
25 #include <linux/module.h>
26 #include <linux/kernel.h>
28 #include <linux/highmem.h>
29 #include <linux/sched.h>
30 #include <linux/moduleparam.h>
31 #include <linux/mod_devicetable.h>
32 #include <linux/trace_events.h>
33 #include <linux/slab.h>
34 #include <linux/tboot.h>
35 #include <linux/hrtimer.h>
36 #include <linux/frame.h>
37 #include "kvm_cache_regs.h"
44 #include <asm/virtext.h>
46 #include <asm/fpu/internal.h>
47 #include <asm/perf_event.h>
48 #include <asm/debugreg.h>
49 #include <asm/kexec.h>
51 #include <asm/irq_remapping.h>
52 #include <asm/mmu_context.h>
53 #include <asm/nospec-branch.h>
58 #define __ex(x) __kvm_handle_fault_on_reboot(x)
59 #define __ex_clear(x, reg) \
60 ____kvm_handle_fault_on_reboot(x, "xor " reg " , " reg)
62 MODULE_AUTHOR("Qumranet");
63 MODULE_LICENSE("GPL");
65 static const struct x86_cpu_id vmx_cpu_id[] = {
66 X86_FEATURE_MATCH(X86_FEATURE_VMX),
69 MODULE_DEVICE_TABLE(x86cpu, vmx_cpu_id);
71 static bool __read_mostly enable_vpid = 1;
72 module_param_named(vpid, enable_vpid, bool, 0444);
74 static bool __read_mostly enable_vnmi = 1;
75 module_param_named(vnmi, enable_vnmi, bool, S_IRUGO);
77 static bool __read_mostly flexpriority_enabled = 1;
78 module_param_named(flexpriority, flexpriority_enabled, bool, S_IRUGO);
80 static bool __read_mostly enable_ept = 1;
81 module_param_named(ept, enable_ept, bool, S_IRUGO);
83 static bool __read_mostly enable_unrestricted_guest = 1;
84 module_param_named(unrestricted_guest,
85 enable_unrestricted_guest, bool, S_IRUGO);
87 static bool __read_mostly enable_ept_ad_bits = 1;
88 module_param_named(eptad, enable_ept_ad_bits, bool, S_IRUGO);
90 static bool __read_mostly emulate_invalid_guest_state = true;
91 module_param(emulate_invalid_guest_state, bool, S_IRUGO);
93 static bool __read_mostly fasteoi = 1;
94 module_param(fasteoi, bool, S_IRUGO);
96 static bool __read_mostly enable_apicv = 1;
97 module_param(enable_apicv, bool, S_IRUGO);
99 static bool __read_mostly enable_shadow_vmcs = 1;
100 module_param_named(enable_shadow_vmcs, enable_shadow_vmcs, bool, S_IRUGO);
102 * If nested=1, nested virtualization is supported, i.e., guests may use
103 * VMX and be a hypervisor for its own guests. If nested=0, guests may not
104 * use VMX instructions.
106 static bool __read_mostly nested = 0;
107 module_param(nested, bool, S_IRUGO);
109 static u64 __read_mostly host_xss;
111 static bool __read_mostly enable_pml = 1;
112 module_param_named(pml, enable_pml, bool, S_IRUGO);
114 #define KVM_VMX_TSC_MULTIPLIER_MAX 0xffffffffffffffffULL
116 /* Guest_tsc -> host_tsc conversion requires 64-bit division. */
117 static int __read_mostly cpu_preemption_timer_multi;
118 static bool __read_mostly enable_preemption_timer = 1;
120 module_param_named(preemption_timer, enable_preemption_timer, bool, S_IRUGO);
123 #define KVM_GUEST_CR0_MASK (X86_CR0_NW | X86_CR0_CD)
124 #define KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST (X86_CR0_WP | X86_CR0_NE)
125 #define KVM_VM_CR0_ALWAYS_ON \
126 (KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST | X86_CR0_PG | X86_CR0_PE)
127 #define KVM_CR4_GUEST_OWNED_BITS \
128 (X86_CR4_PVI | X86_CR4_DE | X86_CR4_PCE | X86_CR4_OSFXSR \
129 | X86_CR4_OSXMMEXCPT | X86_CR4_LA57 | X86_CR4_TSD)
131 #define KVM_PMODE_VM_CR4_ALWAYS_ON (X86_CR4_PAE | X86_CR4_VMXE)
132 #define KVM_RMODE_VM_CR4_ALWAYS_ON (X86_CR4_VME | X86_CR4_PAE | X86_CR4_VMXE)
134 #define RMODE_GUEST_OWNED_EFLAGS_BITS (~(X86_EFLAGS_IOPL | X86_EFLAGS_VM))
136 #define VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE 5
139 * Hyper-V requires all of these, so mark them as supported even though
140 * they are just treated the same as all-context.
142 #define VMX_VPID_EXTENT_SUPPORTED_MASK \
143 (VMX_VPID_EXTENT_INDIVIDUAL_ADDR_BIT | \
144 VMX_VPID_EXTENT_SINGLE_CONTEXT_BIT | \
145 VMX_VPID_EXTENT_GLOBAL_CONTEXT_BIT | \
146 VMX_VPID_EXTENT_SINGLE_NON_GLOBAL_BIT)
149 * These 2 parameters are used to config the controls for Pause-Loop Exiting:
150 * ple_gap: upper bound on the amount of time between two successive
151 * executions of PAUSE in a loop. Also indicate if ple enabled.
152 * According to test, this time is usually smaller than 128 cycles.
153 * ple_window: upper bound on the amount of time a guest is allowed to execute
154 * in a PAUSE loop. Tests indicate that most spinlocks are held for
155 * less than 2^12 cycles
156 * Time is measured based on a counter that runs at the same rate as the TSC,
157 * refer SDM volume 3b section 21.6.13 & 22.1.3.
159 #define KVM_VMX_DEFAULT_PLE_GAP 128
160 #define KVM_VMX_DEFAULT_PLE_WINDOW 4096
161 #define KVM_VMX_DEFAULT_PLE_WINDOW_GROW 2
162 #define KVM_VMX_DEFAULT_PLE_WINDOW_SHRINK 0
163 #define KVM_VMX_DEFAULT_PLE_WINDOW_MAX \
164 INT_MAX / KVM_VMX_DEFAULT_PLE_WINDOW_GROW
166 static int ple_gap = KVM_VMX_DEFAULT_PLE_GAP;
167 module_param(ple_gap, int, S_IRUGO);
169 static int ple_window = KVM_VMX_DEFAULT_PLE_WINDOW;
170 module_param(ple_window, int, S_IRUGO);
172 /* Default doubles per-vcpu window every exit. */
173 static int ple_window_grow = KVM_VMX_DEFAULT_PLE_WINDOW_GROW;
174 module_param(ple_window_grow, int, S_IRUGO);
176 /* Default resets per-vcpu window every exit to ple_window. */
177 static int ple_window_shrink = KVM_VMX_DEFAULT_PLE_WINDOW_SHRINK;
178 module_param(ple_window_shrink, int, S_IRUGO);
180 /* Default is to compute the maximum so we can never overflow. */
181 static int ple_window_actual_max = KVM_VMX_DEFAULT_PLE_WINDOW_MAX;
182 static int ple_window_max = KVM_VMX_DEFAULT_PLE_WINDOW_MAX;
183 module_param(ple_window_max, int, S_IRUGO);
185 extern const ulong vmx_return;
187 #define NR_AUTOLOAD_MSRS 8
196 * Track a VMCS that may be loaded on a certain CPU. If it is (cpu!=-1), also
197 * remember whether it was VMLAUNCHed, and maintain a linked list of all VMCSs
198 * loaded on this CPU (so we can clear them if the CPU goes down).
202 struct vmcs *shadow_vmcs;
205 bool nmi_known_unmasked;
206 unsigned long vmcs_host_cr3; /* May not match real cr3 */
207 unsigned long vmcs_host_cr4; /* May not match real cr4 */
208 /* Support for vnmi-less CPUs */
209 int soft_vnmi_blocked;
211 s64 vnmi_blocked_time;
212 struct list_head loaded_vmcss_on_cpu_link;
215 struct shared_msr_entry {
222 * struct vmcs12 describes the state that our guest hypervisor (L1) keeps for a
223 * single nested guest (L2), hence the name vmcs12. Any VMX implementation has
224 * a VMCS structure, and vmcs12 is our emulated VMX's VMCS. This structure is
225 * stored in guest memory specified by VMPTRLD, but is opaque to the guest,
226 * which must access it using VMREAD/VMWRITE/VMCLEAR instructions.
227 * More than one of these structures may exist, if L1 runs multiple L2 guests.
228 * nested_vmx_run() will use the data here to build the vmcs02: a VMCS for the
229 * underlying hardware which will be used to run L2.
230 * This structure is packed to ensure that its layout is identical across
231 * machines (necessary for live migration).
232 * If there are changes in this struct, VMCS12_REVISION must be changed.
234 typedef u64 natural_width;
235 struct __packed vmcs12 {
236 /* According to the Intel spec, a VMCS region must start with the
237 * following two fields. Then follow implementation-specific data.
242 u32 launch_state; /* set to 0 by VMCLEAR, to 1 by VMLAUNCH */
243 u32 padding[7]; /* room for future expansion */
248 u64 vm_exit_msr_store_addr;
249 u64 vm_exit_msr_load_addr;
250 u64 vm_entry_msr_load_addr;
252 u64 virtual_apic_page_addr;
253 u64 apic_access_addr;
254 u64 posted_intr_desc_addr;
255 u64 vm_function_control;
257 u64 eoi_exit_bitmap0;
258 u64 eoi_exit_bitmap1;
259 u64 eoi_exit_bitmap2;
260 u64 eoi_exit_bitmap3;
261 u64 eptp_list_address;
263 u64 guest_physical_address;
264 u64 vmcs_link_pointer;
266 u64 guest_ia32_debugctl;
269 u64 guest_ia32_perf_global_ctrl;
277 u64 host_ia32_perf_global_ctrl;
278 u64 padding64[8]; /* room for future expansion */
280 * To allow migration of L1 (complete with its L2 guests) between
281 * machines of different natural widths (32 or 64 bit), we cannot have
282 * unsigned long fields with no explict size. We use u64 (aliased
283 * natural_width) instead. Luckily, x86 is little-endian.
285 natural_width cr0_guest_host_mask;
286 natural_width cr4_guest_host_mask;
287 natural_width cr0_read_shadow;
288 natural_width cr4_read_shadow;
289 natural_width cr3_target_value0;
290 natural_width cr3_target_value1;
291 natural_width cr3_target_value2;
292 natural_width cr3_target_value3;
293 natural_width exit_qualification;
294 natural_width guest_linear_address;
295 natural_width guest_cr0;
296 natural_width guest_cr3;
297 natural_width guest_cr4;
298 natural_width guest_es_base;
299 natural_width guest_cs_base;
300 natural_width guest_ss_base;
301 natural_width guest_ds_base;
302 natural_width guest_fs_base;
303 natural_width guest_gs_base;
304 natural_width guest_ldtr_base;
305 natural_width guest_tr_base;
306 natural_width guest_gdtr_base;
307 natural_width guest_idtr_base;
308 natural_width guest_dr7;
309 natural_width guest_rsp;
310 natural_width guest_rip;
311 natural_width guest_rflags;
312 natural_width guest_pending_dbg_exceptions;
313 natural_width guest_sysenter_esp;
314 natural_width guest_sysenter_eip;
315 natural_width host_cr0;
316 natural_width host_cr3;
317 natural_width host_cr4;
318 natural_width host_fs_base;
319 natural_width host_gs_base;
320 natural_width host_tr_base;
321 natural_width host_gdtr_base;
322 natural_width host_idtr_base;
323 natural_width host_ia32_sysenter_esp;
324 natural_width host_ia32_sysenter_eip;
325 natural_width host_rsp;
326 natural_width host_rip;
327 natural_width paddingl[8]; /* room for future expansion */
328 u32 pin_based_vm_exec_control;
329 u32 cpu_based_vm_exec_control;
330 u32 exception_bitmap;
331 u32 page_fault_error_code_mask;
332 u32 page_fault_error_code_match;
333 u32 cr3_target_count;
334 u32 vm_exit_controls;
335 u32 vm_exit_msr_store_count;
336 u32 vm_exit_msr_load_count;
337 u32 vm_entry_controls;
338 u32 vm_entry_msr_load_count;
339 u32 vm_entry_intr_info_field;
340 u32 vm_entry_exception_error_code;
341 u32 vm_entry_instruction_len;
343 u32 secondary_vm_exec_control;
344 u32 vm_instruction_error;
346 u32 vm_exit_intr_info;
347 u32 vm_exit_intr_error_code;
348 u32 idt_vectoring_info_field;
349 u32 idt_vectoring_error_code;
350 u32 vm_exit_instruction_len;
351 u32 vmx_instruction_info;
358 u32 guest_ldtr_limit;
360 u32 guest_gdtr_limit;
361 u32 guest_idtr_limit;
362 u32 guest_es_ar_bytes;
363 u32 guest_cs_ar_bytes;
364 u32 guest_ss_ar_bytes;
365 u32 guest_ds_ar_bytes;
366 u32 guest_fs_ar_bytes;
367 u32 guest_gs_ar_bytes;
368 u32 guest_ldtr_ar_bytes;
369 u32 guest_tr_ar_bytes;
370 u32 guest_interruptibility_info;
371 u32 guest_activity_state;
372 u32 guest_sysenter_cs;
373 u32 host_ia32_sysenter_cs;
374 u32 vmx_preemption_timer_value;
375 u32 padding32[7]; /* room for future expansion */
376 u16 virtual_processor_id;
378 u16 guest_es_selector;
379 u16 guest_cs_selector;
380 u16 guest_ss_selector;
381 u16 guest_ds_selector;
382 u16 guest_fs_selector;
383 u16 guest_gs_selector;
384 u16 guest_ldtr_selector;
385 u16 guest_tr_selector;
386 u16 guest_intr_status;
388 u16 host_es_selector;
389 u16 host_cs_selector;
390 u16 host_ss_selector;
391 u16 host_ds_selector;
392 u16 host_fs_selector;
393 u16 host_gs_selector;
394 u16 host_tr_selector;
398 * VMCS12_REVISION is an arbitrary id that should be changed if the content or
399 * layout of struct vmcs12 is changed. MSR_IA32_VMX_BASIC returns this id, and
400 * VMPTRLD verifies that the VMCS region that L1 is loading contains this id.
402 #define VMCS12_REVISION 0x11e57ed0
405 * VMCS12_SIZE is the number of bytes L1 should allocate for the VMXON region
406 * and any VMCS region. Although only sizeof(struct vmcs12) are used by the
407 * current implementation, 4K are reserved to avoid future complications.
409 #define VMCS12_SIZE 0x1000
412 * VMCS12_MAX_FIELD_INDEX is the highest index value used in any
413 * supported VMCS12 field encoding.
415 #define VMCS12_MAX_FIELD_INDEX 0x17
418 * The nested_vmx structure is part of vcpu_vmx, and holds information we need
419 * for correct emulation of VMX (i.e., nested VMX) on this vcpu.
422 /* Has the level1 guest done vmxon? */
427 /* The guest-physical address of the current VMCS L1 keeps for L2 */
430 * Cache of the guest's VMCS, existing outside of guest memory.
431 * Loaded from guest memory during VMPTRLD. Flushed to guest
432 * memory during VMCLEAR and VMPTRLD.
434 struct vmcs12 *cached_vmcs12;
436 * Indicates if the shadow vmcs must be updated with the
437 * data hold by vmcs12
439 bool sync_shadow_vmcs;
442 bool change_vmcs01_virtual_x2apic_mode;
443 /* L2 must run next, and mustn't decide to exit to L1. */
444 bool nested_run_pending;
446 struct loaded_vmcs vmcs02;
449 * Guest pages referred to in the vmcs02 with host-physical
450 * pointers, so we must keep them pinned while L2 runs.
452 struct page *apic_access_page;
453 struct page *virtual_apic_page;
454 struct page *pi_desc_page;
455 struct pi_desc *pi_desc;
459 unsigned long *msr_bitmap;
461 struct hrtimer preemption_timer;
462 bool preemption_timer_expired;
464 /* to migrate it to L2 if VM_ENTRY_LOAD_DEBUG_CONTROLS is off */
471 * We only store the "true" versions of the VMX capability MSRs. We
472 * generate the "non-true" versions by setting the must-be-1 bits
473 * according to the SDM.
475 u32 nested_vmx_procbased_ctls_low;
476 u32 nested_vmx_procbased_ctls_high;
477 u32 nested_vmx_secondary_ctls_low;
478 u32 nested_vmx_secondary_ctls_high;
479 u32 nested_vmx_pinbased_ctls_low;
480 u32 nested_vmx_pinbased_ctls_high;
481 u32 nested_vmx_exit_ctls_low;
482 u32 nested_vmx_exit_ctls_high;
483 u32 nested_vmx_entry_ctls_low;
484 u32 nested_vmx_entry_ctls_high;
485 u32 nested_vmx_misc_low;
486 u32 nested_vmx_misc_high;
487 u32 nested_vmx_ept_caps;
488 u32 nested_vmx_vpid_caps;
489 u64 nested_vmx_basic;
490 u64 nested_vmx_cr0_fixed0;
491 u64 nested_vmx_cr0_fixed1;
492 u64 nested_vmx_cr4_fixed0;
493 u64 nested_vmx_cr4_fixed1;
494 u64 nested_vmx_vmcs_enum;
495 u64 nested_vmx_vmfunc_controls;
497 /* SMM related state */
499 /* in VMX operation on SMM entry? */
501 /* in guest mode on SMM entry? */
506 #define POSTED_INTR_ON 0
507 #define POSTED_INTR_SN 1
509 /* Posted-Interrupt Descriptor */
511 u32 pir[8]; /* Posted interrupt requested */
514 /* bit 256 - Outstanding Notification */
516 /* bit 257 - Suppress Notification */
518 /* bit 271:258 - Reserved */
520 /* bit 279:272 - Notification Vector */
522 /* bit 287:280 - Reserved */
524 /* bit 319:288 - Notification Destination */
532 static bool pi_test_and_set_on(struct pi_desc *pi_desc)
534 return test_and_set_bit(POSTED_INTR_ON,
535 (unsigned long *)&pi_desc->control);
538 static bool pi_test_and_clear_on(struct pi_desc *pi_desc)
540 return test_and_clear_bit(POSTED_INTR_ON,
541 (unsigned long *)&pi_desc->control);
544 static int pi_test_and_set_pir(int vector, struct pi_desc *pi_desc)
546 return test_and_set_bit(vector, (unsigned long *)pi_desc->pir);
549 static inline void pi_clear_sn(struct pi_desc *pi_desc)
551 return clear_bit(POSTED_INTR_SN,
552 (unsigned long *)&pi_desc->control);
555 static inline void pi_set_sn(struct pi_desc *pi_desc)
557 return set_bit(POSTED_INTR_SN,
558 (unsigned long *)&pi_desc->control);
561 static inline void pi_clear_on(struct pi_desc *pi_desc)
563 clear_bit(POSTED_INTR_ON,
564 (unsigned long *)&pi_desc->control);
567 static inline int pi_test_on(struct pi_desc *pi_desc)
569 return test_bit(POSTED_INTR_ON,
570 (unsigned long *)&pi_desc->control);
573 static inline int pi_test_sn(struct pi_desc *pi_desc)
575 return test_bit(POSTED_INTR_SN,
576 (unsigned long *)&pi_desc->control);
580 struct kvm_vcpu vcpu;
581 unsigned long host_rsp;
584 u32 idt_vectoring_info;
586 struct shared_msr_entry *guest_msrs;
589 unsigned long host_idt_base;
591 u64 msr_host_kernel_gs_base;
592 u64 msr_guest_kernel_gs_base;
594 u32 vm_entry_controls_shadow;
595 u32 vm_exit_controls_shadow;
596 u32 secondary_exec_control;
599 * loaded_vmcs points to the VMCS currently used in this vcpu. For a
600 * non-nested (L1) guest, it always points to vmcs01. For a nested
601 * guest (L2), it points to a different VMCS.
603 struct loaded_vmcs vmcs01;
604 struct loaded_vmcs *loaded_vmcs;
605 bool __launched; /* temporary, used in vmx_vcpu_run */
606 struct msr_autoload {
608 struct vmx_msr_entry guest[NR_AUTOLOAD_MSRS];
609 struct vmx_msr_entry host[NR_AUTOLOAD_MSRS];
613 u16 fs_sel, gs_sel, ldt_sel;
617 int gs_ldt_reload_needed;
618 int fs_reload_needed;
619 u64 msr_host_bndcfgs;
624 struct kvm_segment segs[8];
627 u32 bitmask; /* 4 bits per segment (1 bit per field) */
628 struct kvm_save_segment {
636 bool emulation_required;
640 /* Posted interrupt descriptor */
641 struct pi_desc pi_desc;
643 /* Support for a guest hypervisor (nested VMX) */
644 struct nested_vmx nested;
646 /* Dynamic PLE window. */
648 bool ple_window_dirty;
650 /* Support for PML */
651 #define PML_ENTITY_NUM 512
654 /* apic deadline value in host tsc */
657 u64 current_tsc_ratio;
661 unsigned long host_debugctlmsr;
664 * Only bits masked by msr_ia32_feature_control_valid_bits can be set in
665 * msr_ia32_feature_control. FEATURE_CONTROL_LOCKED is always included
666 * in msr_ia32_feature_control_valid_bits.
668 u64 msr_ia32_feature_control;
669 u64 msr_ia32_feature_control_valid_bits;
672 enum segment_cache_field {
681 static inline struct vcpu_vmx *to_vmx(struct kvm_vcpu *vcpu)
683 return container_of(vcpu, struct vcpu_vmx, vcpu);
686 static struct pi_desc *vcpu_to_pi_desc(struct kvm_vcpu *vcpu)
688 return &(to_vmx(vcpu)->pi_desc);
691 #define ROL16(val, n) ((u16)(((u16)(val) << (n)) | ((u16)(val) >> (16 - (n)))))
692 #define VMCS12_OFFSET(x) offsetof(struct vmcs12, x)
693 #define FIELD(number, name) [ROL16(number, 6)] = VMCS12_OFFSET(name)
694 #define FIELD64(number, name) \
695 FIELD(number, name), \
696 [ROL16(number##_HIGH, 6)] = VMCS12_OFFSET(name) + sizeof(u32)
699 static u16 shadow_read_only_fields[] = {
700 #define SHADOW_FIELD_RO(x) x,
701 #include "vmx_shadow_fields.h"
703 static int max_shadow_read_only_fields =
704 ARRAY_SIZE(shadow_read_only_fields);
706 static u16 shadow_read_write_fields[] = {
707 #define SHADOW_FIELD_RW(x) x,
708 #include "vmx_shadow_fields.h"
710 static int max_shadow_read_write_fields =
711 ARRAY_SIZE(shadow_read_write_fields);
713 static const unsigned short vmcs_field_to_offset_table[] = {
714 FIELD(VIRTUAL_PROCESSOR_ID, virtual_processor_id),
715 FIELD(POSTED_INTR_NV, posted_intr_nv),
716 FIELD(GUEST_ES_SELECTOR, guest_es_selector),
717 FIELD(GUEST_CS_SELECTOR, guest_cs_selector),
718 FIELD(GUEST_SS_SELECTOR, guest_ss_selector),
719 FIELD(GUEST_DS_SELECTOR, guest_ds_selector),
720 FIELD(GUEST_FS_SELECTOR, guest_fs_selector),
721 FIELD(GUEST_GS_SELECTOR, guest_gs_selector),
722 FIELD(GUEST_LDTR_SELECTOR, guest_ldtr_selector),
723 FIELD(GUEST_TR_SELECTOR, guest_tr_selector),
724 FIELD(GUEST_INTR_STATUS, guest_intr_status),
725 FIELD(GUEST_PML_INDEX, guest_pml_index),
726 FIELD(HOST_ES_SELECTOR, host_es_selector),
727 FIELD(HOST_CS_SELECTOR, host_cs_selector),
728 FIELD(HOST_SS_SELECTOR, host_ss_selector),
729 FIELD(HOST_DS_SELECTOR, host_ds_selector),
730 FIELD(HOST_FS_SELECTOR, host_fs_selector),
731 FIELD(HOST_GS_SELECTOR, host_gs_selector),
732 FIELD(HOST_TR_SELECTOR, host_tr_selector),
733 FIELD64(IO_BITMAP_A, io_bitmap_a),
734 FIELD64(IO_BITMAP_B, io_bitmap_b),
735 FIELD64(MSR_BITMAP, msr_bitmap),
736 FIELD64(VM_EXIT_MSR_STORE_ADDR, vm_exit_msr_store_addr),
737 FIELD64(VM_EXIT_MSR_LOAD_ADDR, vm_exit_msr_load_addr),
738 FIELD64(VM_ENTRY_MSR_LOAD_ADDR, vm_entry_msr_load_addr),
739 FIELD64(TSC_OFFSET, tsc_offset),
740 FIELD64(VIRTUAL_APIC_PAGE_ADDR, virtual_apic_page_addr),
741 FIELD64(APIC_ACCESS_ADDR, apic_access_addr),
742 FIELD64(POSTED_INTR_DESC_ADDR, posted_intr_desc_addr),
743 FIELD64(VM_FUNCTION_CONTROL, vm_function_control),
744 FIELD64(EPT_POINTER, ept_pointer),
745 FIELD64(EOI_EXIT_BITMAP0, eoi_exit_bitmap0),
746 FIELD64(EOI_EXIT_BITMAP1, eoi_exit_bitmap1),
747 FIELD64(EOI_EXIT_BITMAP2, eoi_exit_bitmap2),
748 FIELD64(EOI_EXIT_BITMAP3, eoi_exit_bitmap3),
749 FIELD64(EPTP_LIST_ADDRESS, eptp_list_address),
750 FIELD64(XSS_EXIT_BITMAP, xss_exit_bitmap),
751 FIELD64(GUEST_PHYSICAL_ADDRESS, guest_physical_address),
752 FIELD64(VMCS_LINK_POINTER, vmcs_link_pointer),
753 FIELD64(PML_ADDRESS, pml_address),
754 FIELD64(GUEST_IA32_DEBUGCTL, guest_ia32_debugctl),
755 FIELD64(GUEST_IA32_PAT, guest_ia32_pat),
756 FIELD64(GUEST_IA32_EFER, guest_ia32_efer),
757 FIELD64(GUEST_IA32_PERF_GLOBAL_CTRL, guest_ia32_perf_global_ctrl),
758 FIELD64(GUEST_PDPTR0, guest_pdptr0),
759 FIELD64(GUEST_PDPTR1, guest_pdptr1),
760 FIELD64(GUEST_PDPTR2, guest_pdptr2),
761 FIELD64(GUEST_PDPTR3, guest_pdptr3),
762 FIELD64(GUEST_BNDCFGS, guest_bndcfgs),
763 FIELD64(HOST_IA32_PAT, host_ia32_pat),
764 FIELD64(HOST_IA32_EFER, host_ia32_efer),
765 FIELD64(HOST_IA32_PERF_GLOBAL_CTRL, host_ia32_perf_global_ctrl),
766 FIELD(PIN_BASED_VM_EXEC_CONTROL, pin_based_vm_exec_control),
767 FIELD(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control),
768 FIELD(EXCEPTION_BITMAP, exception_bitmap),
769 FIELD(PAGE_FAULT_ERROR_CODE_MASK, page_fault_error_code_mask),
770 FIELD(PAGE_FAULT_ERROR_CODE_MATCH, page_fault_error_code_match),
771 FIELD(CR3_TARGET_COUNT, cr3_target_count),
772 FIELD(VM_EXIT_CONTROLS, vm_exit_controls),
773 FIELD(VM_EXIT_MSR_STORE_COUNT, vm_exit_msr_store_count),
774 FIELD(VM_EXIT_MSR_LOAD_COUNT, vm_exit_msr_load_count),
775 FIELD(VM_ENTRY_CONTROLS, vm_entry_controls),
776 FIELD(VM_ENTRY_MSR_LOAD_COUNT, vm_entry_msr_load_count),
777 FIELD(VM_ENTRY_INTR_INFO_FIELD, vm_entry_intr_info_field),
778 FIELD(VM_ENTRY_EXCEPTION_ERROR_CODE, vm_entry_exception_error_code),
779 FIELD(VM_ENTRY_INSTRUCTION_LEN, vm_entry_instruction_len),
780 FIELD(TPR_THRESHOLD, tpr_threshold),
781 FIELD(SECONDARY_VM_EXEC_CONTROL, secondary_vm_exec_control),
782 FIELD(VM_INSTRUCTION_ERROR, vm_instruction_error),
783 FIELD(VM_EXIT_REASON, vm_exit_reason),
784 FIELD(VM_EXIT_INTR_INFO, vm_exit_intr_info),
785 FIELD(VM_EXIT_INTR_ERROR_CODE, vm_exit_intr_error_code),
786 FIELD(IDT_VECTORING_INFO_FIELD, idt_vectoring_info_field),
787 FIELD(IDT_VECTORING_ERROR_CODE, idt_vectoring_error_code),
788 FIELD(VM_EXIT_INSTRUCTION_LEN, vm_exit_instruction_len),
789 FIELD(VMX_INSTRUCTION_INFO, vmx_instruction_info),
790 FIELD(GUEST_ES_LIMIT, guest_es_limit),
791 FIELD(GUEST_CS_LIMIT, guest_cs_limit),
792 FIELD(GUEST_SS_LIMIT, guest_ss_limit),
793 FIELD(GUEST_DS_LIMIT, guest_ds_limit),
794 FIELD(GUEST_FS_LIMIT, guest_fs_limit),
795 FIELD(GUEST_GS_LIMIT, guest_gs_limit),
796 FIELD(GUEST_LDTR_LIMIT, guest_ldtr_limit),
797 FIELD(GUEST_TR_LIMIT, guest_tr_limit),
798 FIELD(GUEST_GDTR_LIMIT, guest_gdtr_limit),
799 FIELD(GUEST_IDTR_LIMIT, guest_idtr_limit),
800 FIELD(GUEST_ES_AR_BYTES, guest_es_ar_bytes),
801 FIELD(GUEST_CS_AR_BYTES, guest_cs_ar_bytes),
802 FIELD(GUEST_SS_AR_BYTES, guest_ss_ar_bytes),
803 FIELD(GUEST_DS_AR_BYTES, guest_ds_ar_bytes),
804 FIELD(GUEST_FS_AR_BYTES, guest_fs_ar_bytes),
805 FIELD(GUEST_GS_AR_BYTES, guest_gs_ar_bytes),
806 FIELD(GUEST_LDTR_AR_BYTES, guest_ldtr_ar_bytes),
807 FIELD(GUEST_TR_AR_BYTES, guest_tr_ar_bytes),
808 FIELD(GUEST_INTERRUPTIBILITY_INFO, guest_interruptibility_info),
809 FIELD(GUEST_ACTIVITY_STATE, guest_activity_state),
810 FIELD(GUEST_SYSENTER_CS, guest_sysenter_cs),
811 FIELD(HOST_IA32_SYSENTER_CS, host_ia32_sysenter_cs),
812 FIELD(VMX_PREEMPTION_TIMER_VALUE, vmx_preemption_timer_value),
813 FIELD(CR0_GUEST_HOST_MASK, cr0_guest_host_mask),
814 FIELD(CR4_GUEST_HOST_MASK, cr4_guest_host_mask),
815 FIELD(CR0_READ_SHADOW, cr0_read_shadow),
816 FIELD(CR4_READ_SHADOW, cr4_read_shadow),
817 FIELD(CR3_TARGET_VALUE0, cr3_target_value0),
818 FIELD(CR3_TARGET_VALUE1, cr3_target_value1),
819 FIELD(CR3_TARGET_VALUE2, cr3_target_value2),
820 FIELD(CR3_TARGET_VALUE3, cr3_target_value3),
821 FIELD(EXIT_QUALIFICATION, exit_qualification),
822 FIELD(GUEST_LINEAR_ADDRESS, guest_linear_address),
823 FIELD(GUEST_CR0, guest_cr0),
824 FIELD(GUEST_CR3, guest_cr3),
825 FIELD(GUEST_CR4, guest_cr4),
826 FIELD(GUEST_ES_BASE, guest_es_base),
827 FIELD(GUEST_CS_BASE, guest_cs_base),
828 FIELD(GUEST_SS_BASE, guest_ss_base),
829 FIELD(GUEST_DS_BASE, guest_ds_base),
830 FIELD(GUEST_FS_BASE, guest_fs_base),
831 FIELD(GUEST_GS_BASE, guest_gs_base),
832 FIELD(GUEST_LDTR_BASE, guest_ldtr_base),
833 FIELD(GUEST_TR_BASE, guest_tr_base),
834 FIELD(GUEST_GDTR_BASE, guest_gdtr_base),
835 FIELD(GUEST_IDTR_BASE, guest_idtr_base),
836 FIELD(GUEST_DR7, guest_dr7),
837 FIELD(GUEST_RSP, guest_rsp),
838 FIELD(GUEST_RIP, guest_rip),
839 FIELD(GUEST_RFLAGS, guest_rflags),
840 FIELD(GUEST_PENDING_DBG_EXCEPTIONS, guest_pending_dbg_exceptions),
841 FIELD(GUEST_SYSENTER_ESP, guest_sysenter_esp),
842 FIELD(GUEST_SYSENTER_EIP, guest_sysenter_eip),
843 FIELD(HOST_CR0, host_cr0),
844 FIELD(HOST_CR3, host_cr3),
845 FIELD(HOST_CR4, host_cr4),
846 FIELD(HOST_FS_BASE, host_fs_base),
847 FIELD(HOST_GS_BASE, host_gs_base),
848 FIELD(HOST_TR_BASE, host_tr_base),
849 FIELD(HOST_GDTR_BASE, host_gdtr_base),
850 FIELD(HOST_IDTR_BASE, host_idtr_base),
851 FIELD(HOST_IA32_SYSENTER_ESP, host_ia32_sysenter_esp),
852 FIELD(HOST_IA32_SYSENTER_EIP, host_ia32_sysenter_eip),
853 FIELD(HOST_RSP, host_rsp),
854 FIELD(HOST_RIP, host_rip),
857 static inline short vmcs_field_to_offset(unsigned long field)
864 index = ROL16(field, 6);
865 if (index >= ARRAY_SIZE(vmcs_field_to_offset_table))
869 * FIXME: Mitigation for CVE-2017-5753. To be replaced with a
874 if (vmcs_field_to_offset_table[index] == 0)
877 return vmcs_field_to_offset_table[index];
880 static inline struct vmcs12 *get_vmcs12(struct kvm_vcpu *vcpu)
882 return to_vmx(vcpu)->nested.cached_vmcs12;
885 static bool nested_ept_ad_enabled(struct kvm_vcpu *vcpu);
886 static unsigned long nested_ept_get_cr3(struct kvm_vcpu *vcpu);
887 static u64 construct_eptp(struct kvm_vcpu *vcpu, unsigned long root_hpa);
888 static bool vmx_xsaves_supported(void);
889 static void vmx_set_segment(struct kvm_vcpu *vcpu,
890 struct kvm_segment *var, int seg);
891 static void vmx_get_segment(struct kvm_vcpu *vcpu,
892 struct kvm_segment *var, int seg);
893 static bool guest_state_valid(struct kvm_vcpu *vcpu);
894 static u32 vmx_segment_access_rights(struct kvm_segment *var);
895 static void copy_shadow_to_vmcs12(struct vcpu_vmx *vmx);
896 static bool vmx_get_nmi_mask(struct kvm_vcpu *vcpu);
897 static void vmx_set_nmi_mask(struct kvm_vcpu *vcpu, bool masked);
898 static bool nested_vmx_is_page_fault_vmexit(struct vmcs12 *vmcs12,
901 static DEFINE_PER_CPU(struct vmcs *, vmxarea);
902 static DEFINE_PER_CPU(struct vmcs *, current_vmcs);
904 * We maintain a per-CPU linked-list of VMCS loaded on that CPU. This is needed
905 * when a CPU is brought down, and we need to VMCLEAR all VMCSs loaded on it.
907 static DEFINE_PER_CPU(struct list_head, loaded_vmcss_on_cpu);
910 * We maintian a per-CPU linked-list of vCPU, so in wakeup_handler() we
911 * can find which vCPU should be waken up.
913 static DEFINE_PER_CPU(struct list_head, blocked_vcpu_on_cpu);
914 static DEFINE_PER_CPU(spinlock_t, blocked_vcpu_on_cpu_lock);
917 VMX_MSR_BITMAP_LEGACY,
918 VMX_MSR_BITMAP_LONGMODE,
919 VMX_MSR_BITMAP_LEGACY_X2APIC_APICV,
920 VMX_MSR_BITMAP_LONGMODE_X2APIC_APICV,
921 VMX_MSR_BITMAP_LEGACY_X2APIC,
922 VMX_MSR_BITMAP_LONGMODE_X2APIC,
928 static unsigned long *vmx_bitmap[VMX_BITMAP_NR];
930 #define vmx_msr_bitmap_legacy (vmx_bitmap[VMX_MSR_BITMAP_LEGACY])
931 #define vmx_msr_bitmap_longmode (vmx_bitmap[VMX_MSR_BITMAP_LONGMODE])
932 #define vmx_msr_bitmap_legacy_x2apic_apicv (vmx_bitmap[VMX_MSR_BITMAP_LEGACY_X2APIC_APICV])
933 #define vmx_msr_bitmap_longmode_x2apic_apicv (vmx_bitmap[VMX_MSR_BITMAP_LONGMODE_X2APIC_APICV])
934 #define vmx_msr_bitmap_legacy_x2apic (vmx_bitmap[VMX_MSR_BITMAP_LEGACY_X2APIC])
935 #define vmx_msr_bitmap_longmode_x2apic (vmx_bitmap[VMX_MSR_BITMAP_LONGMODE_X2APIC])
936 #define vmx_vmread_bitmap (vmx_bitmap[VMX_VMREAD_BITMAP])
937 #define vmx_vmwrite_bitmap (vmx_bitmap[VMX_VMWRITE_BITMAP])
939 static bool cpu_has_load_ia32_efer;
940 static bool cpu_has_load_perf_global_ctrl;
942 static DECLARE_BITMAP(vmx_vpid_bitmap, VMX_NR_VPIDS);
943 static DEFINE_SPINLOCK(vmx_vpid_lock);
945 static struct vmcs_config {
950 u32 pin_based_exec_ctrl;
951 u32 cpu_based_exec_ctrl;
952 u32 cpu_based_2nd_exec_ctrl;
957 static struct vmx_capability {
962 #define VMX_SEGMENT_FIELD(seg) \
963 [VCPU_SREG_##seg] = { \
964 .selector = GUEST_##seg##_SELECTOR, \
965 .base = GUEST_##seg##_BASE, \
966 .limit = GUEST_##seg##_LIMIT, \
967 .ar_bytes = GUEST_##seg##_AR_BYTES, \
970 static const struct kvm_vmx_segment_field {
975 } kvm_vmx_segment_fields[] = {
976 VMX_SEGMENT_FIELD(CS),
977 VMX_SEGMENT_FIELD(DS),
978 VMX_SEGMENT_FIELD(ES),
979 VMX_SEGMENT_FIELD(FS),
980 VMX_SEGMENT_FIELD(GS),
981 VMX_SEGMENT_FIELD(SS),
982 VMX_SEGMENT_FIELD(TR),
983 VMX_SEGMENT_FIELD(LDTR),
986 static u64 host_efer;
988 static void ept_save_pdptrs(struct kvm_vcpu *vcpu);
991 * Keep MSR_STAR at the end, as setup_msrs() will try to optimize it
992 * away by decrementing the array size.
994 static const u32 vmx_msr_index[] = {
996 MSR_SYSCALL_MASK, MSR_LSTAR, MSR_CSTAR,
998 MSR_EFER, MSR_TSC_AUX, MSR_STAR,
1001 static inline bool is_exception_n(u32 intr_info, u8 vector)
1003 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
1004 INTR_INFO_VALID_MASK)) ==
1005 (INTR_TYPE_HARD_EXCEPTION | vector | INTR_INFO_VALID_MASK);
1008 static inline bool is_debug(u32 intr_info)
1010 return is_exception_n(intr_info, DB_VECTOR);
1013 static inline bool is_breakpoint(u32 intr_info)
1015 return is_exception_n(intr_info, BP_VECTOR);
1018 static inline bool is_page_fault(u32 intr_info)
1020 return is_exception_n(intr_info, PF_VECTOR);
1023 static inline bool is_no_device(u32 intr_info)
1025 return is_exception_n(intr_info, NM_VECTOR);
1028 static inline bool is_invalid_opcode(u32 intr_info)
1030 return is_exception_n(intr_info, UD_VECTOR);
1033 static inline bool is_external_interrupt(u32 intr_info)
1035 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK))
1036 == (INTR_TYPE_EXT_INTR | INTR_INFO_VALID_MASK);
1039 static inline bool is_machine_check(u32 intr_info)
1041 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
1042 INTR_INFO_VALID_MASK)) ==
1043 (INTR_TYPE_HARD_EXCEPTION | MC_VECTOR | INTR_INFO_VALID_MASK);
1046 static inline bool cpu_has_vmx_msr_bitmap(void)
1048 return vmcs_config.cpu_based_exec_ctrl & CPU_BASED_USE_MSR_BITMAPS;
1051 static inline bool cpu_has_vmx_tpr_shadow(void)
1053 return vmcs_config.cpu_based_exec_ctrl & CPU_BASED_TPR_SHADOW;
1056 static inline bool cpu_need_tpr_shadow(struct kvm_vcpu *vcpu)
1058 return cpu_has_vmx_tpr_shadow() && lapic_in_kernel(vcpu);
1061 static inline bool cpu_has_secondary_exec_ctrls(void)
1063 return vmcs_config.cpu_based_exec_ctrl &
1064 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
1067 static inline bool cpu_has_vmx_virtualize_apic_accesses(void)
1069 return vmcs_config.cpu_based_2nd_exec_ctrl &
1070 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
1073 static inline bool cpu_has_vmx_virtualize_x2apic_mode(void)
1075 return vmcs_config.cpu_based_2nd_exec_ctrl &
1076 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
1079 static inline bool cpu_has_vmx_apic_register_virt(void)
1081 return vmcs_config.cpu_based_2nd_exec_ctrl &
1082 SECONDARY_EXEC_APIC_REGISTER_VIRT;
1085 static inline bool cpu_has_vmx_virtual_intr_delivery(void)
1087 return vmcs_config.cpu_based_2nd_exec_ctrl &
1088 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY;
1092 * Comment's format: document - errata name - stepping - processor name.
1094 * https://www.virtualbox.org/svn/vbox/trunk/src/VBox/VMM/VMMR0/HMR0.cpp
1096 static u32 vmx_preemption_cpu_tfms[] = {
1097 /* 323344.pdf - BA86 - D0 - Xeon 7500 Series */
1099 /* 323056.pdf - AAX65 - C2 - Xeon L3406 */
1100 /* 322814.pdf - AAT59 - C2 - i7-600, i5-500, i5-400 and i3-300 Mobile */
1101 /* 322911.pdf - AAU65 - C2 - i5-600, i3-500 Desktop and Pentium G6950 */
1103 /* 322911.pdf - AAU65 - K0 - i5-600, i3-500 Desktop and Pentium G6950 */
1105 /* 322373.pdf - AAO95 - B1 - Xeon 3400 Series */
1106 /* 322166.pdf - AAN92 - B1 - i7-800 and i5-700 Desktop */
1108 * 320767.pdf - AAP86 - B1 -
1109 * i7-900 Mobile Extreme, i7-800 and i7-700 Mobile
1112 /* 321333.pdf - AAM126 - C0 - Xeon 3500 */
1114 /* 321333.pdf - AAM126 - C1 - Xeon 3500 */
1116 /* 320836.pdf - AAJ124 - C0 - i7-900 Desktop Extreme and i7-900 Desktop */
1118 /* 321333.pdf - AAM126 - D0 - Xeon 3500 */
1119 /* 321324.pdf - AAK139 - D0 - Xeon 5500 */
1120 /* 320836.pdf - AAJ124 - D0 - i7-900 Extreme and i7-900 Desktop */
1124 static inline bool cpu_has_broken_vmx_preemption_timer(void)
1126 u32 eax = cpuid_eax(0x00000001), i;
1128 /* Clear the reserved bits */
1129 eax &= ~(0x3U << 14 | 0xfU << 28);
1130 for (i = 0; i < ARRAY_SIZE(vmx_preemption_cpu_tfms); i++)
1131 if (eax == vmx_preemption_cpu_tfms[i])
1137 static inline bool cpu_has_vmx_preemption_timer(void)
1139 return vmcs_config.pin_based_exec_ctrl &
1140 PIN_BASED_VMX_PREEMPTION_TIMER;
1143 static inline bool cpu_has_vmx_posted_intr(void)
1145 return IS_ENABLED(CONFIG_X86_LOCAL_APIC) &&
1146 vmcs_config.pin_based_exec_ctrl & PIN_BASED_POSTED_INTR;
1149 static inline bool cpu_has_vmx_apicv(void)
1151 return cpu_has_vmx_apic_register_virt() &&
1152 cpu_has_vmx_virtual_intr_delivery() &&
1153 cpu_has_vmx_posted_intr();
1156 static inline bool cpu_has_vmx_flexpriority(void)
1158 return cpu_has_vmx_tpr_shadow() &&
1159 cpu_has_vmx_virtualize_apic_accesses();
1162 static inline bool cpu_has_vmx_ept_execute_only(void)
1164 return vmx_capability.ept & VMX_EPT_EXECUTE_ONLY_BIT;
1167 static inline bool cpu_has_vmx_ept_2m_page(void)
1169 return vmx_capability.ept & VMX_EPT_2MB_PAGE_BIT;
1172 static inline bool cpu_has_vmx_ept_1g_page(void)
1174 return vmx_capability.ept & VMX_EPT_1GB_PAGE_BIT;
1177 static inline bool cpu_has_vmx_ept_4levels(void)
1179 return vmx_capability.ept & VMX_EPT_PAGE_WALK_4_BIT;
1182 static inline bool cpu_has_vmx_ept_mt_wb(void)
1184 return vmx_capability.ept & VMX_EPTP_WB_BIT;
1187 static inline bool cpu_has_vmx_ept_5levels(void)
1189 return vmx_capability.ept & VMX_EPT_PAGE_WALK_5_BIT;
1192 static inline bool cpu_has_vmx_ept_ad_bits(void)
1194 return vmx_capability.ept & VMX_EPT_AD_BIT;
1197 static inline bool cpu_has_vmx_invept_context(void)
1199 return vmx_capability.ept & VMX_EPT_EXTENT_CONTEXT_BIT;
1202 static inline bool cpu_has_vmx_invept_global(void)
1204 return vmx_capability.ept & VMX_EPT_EXTENT_GLOBAL_BIT;
1207 static inline bool cpu_has_vmx_invvpid_single(void)
1209 return vmx_capability.vpid & VMX_VPID_EXTENT_SINGLE_CONTEXT_BIT;
1212 static inline bool cpu_has_vmx_invvpid_global(void)
1214 return vmx_capability.vpid & VMX_VPID_EXTENT_GLOBAL_CONTEXT_BIT;
1217 static inline bool cpu_has_vmx_invvpid(void)
1219 return vmx_capability.vpid & VMX_VPID_INVVPID_BIT;
1222 static inline bool cpu_has_vmx_ept(void)
1224 return vmcs_config.cpu_based_2nd_exec_ctrl &
1225 SECONDARY_EXEC_ENABLE_EPT;
1228 static inline bool cpu_has_vmx_unrestricted_guest(void)
1230 return vmcs_config.cpu_based_2nd_exec_ctrl &
1231 SECONDARY_EXEC_UNRESTRICTED_GUEST;
1234 static inline bool cpu_has_vmx_ple(void)
1236 return vmcs_config.cpu_based_2nd_exec_ctrl &
1237 SECONDARY_EXEC_PAUSE_LOOP_EXITING;
1240 static inline bool cpu_has_vmx_basic_inout(void)
1242 return (((u64)vmcs_config.basic_cap << 32) & VMX_BASIC_INOUT);
1245 static inline bool cpu_need_virtualize_apic_accesses(struct kvm_vcpu *vcpu)
1247 return flexpriority_enabled && lapic_in_kernel(vcpu);
1250 static inline bool cpu_has_vmx_vpid(void)
1252 return vmcs_config.cpu_based_2nd_exec_ctrl &
1253 SECONDARY_EXEC_ENABLE_VPID;
1256 static inline bool cpu_has_vmx_rdtscp(void)
1258 return vmcs_config.cpu_based_2nd_exec_ctrl &
1259 SECONDARY_EXEC_RDTSCP;
1262 static inline bool cpu_has_vmx_invpcid(void)
1264 return vmcs_config.cpu_based_2nd_exec_ctrl &
1265 SECONDARY_EXEC_ENABLE_INVPCID;
1268 static inline bool cpu_has_virtual_nmis(void)
1270 return vmcs_config.pin_based_exec_ctrl & PIN_BASED_VIRTUAL_NMIS;
1273 static inline bool cpu_has_vmx_wbinvd_exit(void)
1275 return vmcs_config.cpu_based_2nd_exec_ctrl &
1276 SECONDARY_EXEC_WBINVD_EXITING;
1279 static inline bool cpu_has_vmx_shadow_vmcs(void)
1282 rdmsrl(MSR_IA32_VMX_MISC, vmx_msr);
1283 /* check if the cpu supports writing r/o exit information fields */
1284 if (!(vmx_msr & MSR_IA32_VMX_MISC_VMWRITE_SHADOW_RO_FIELDS))
1287 return vmcs_config.cpu_based_2nd_exec_ctrl &
1288 SECONDARY_EXEC_SHADOW_VMCS;
1291 static inline bool cpu_has_vmx_pml(void)
1293 return vmcs_config.cpu_based_2nd_exec_ctrl & SECONDARY_EXEC_ENABLE_PML;
1296 static inline bool cpu_has_vmx_tsc_scaling(void)
1298 return vmcs_config.cpu_based_2nd_exec_ctrl &
1299 SECONDARY_EXEC_TSC_SCALING;
1302 static inline bool cpu_has_vmx_vmfunc(void)
1304 return vmcs_config.cpu_based_2nd_exec_ctrl &
1305 SECONDARY_EXEC_ENABLE_VMFUNC;
1308 static inline bool report_flexpriority(void)
1310 return flexpriority_enabled;
1313 static inline unsigned nested_cpu_vmx_misc_cr3_count(struct kvm_vcpu *vcpu)
1315 return vmx_misc_cr3_count(to_vmx(vcpu)->nested.nested_vmx_misc_low);
1318 static inline bool nested_cpu_has(struct vmcs12 *vmcs12, u32 bit)
1320 return vmcs12->cpu_based_vm_exec_control & bit;
1323 static inline bool nested_cpu_has2(struct vmcs12 *vmcs12, u32 bit)
1325 return (vmcs12->cpu_based_vm_exec_control &
1326 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) &&
1327 (vmcs12->secondary_vm_exec_control & bit);
1330 static inline bool nested_cpu_has_preemption_timer(struct vmcs12 *vmcs12)
1332 return vmcs12->pin_based_vm_exec_control &
1333 PIN_BASED_VMX_PREEMPTION_TIMER;
1336 static inline int nested_cpu_has_ept(struct vmcs12 *vmcs12)
1338 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_EPT);
1341 static inline bool nested_cpu_has_xsaves(struct vmcs12 *vmcs12)
1343 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_XSAVES);
1346 static inline bool nested_cpu_has_pml(struct vmcs12 *vmcs12)
1348 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_PML);
1351 static inline bool nested_cpu_has_virt_x2apic_mode(struct vmcs12 *vmcs12)
1353 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE);
1356 static inline bool nested_cpu_has_vpid(struct vmcs12 *vmcs12)
1358 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_VPID);
1361 static inline bool nested_cpu_has_apic_reg_virt(struct vmcs12 *vmcs12)
1363 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_APIC_REGISTER_VIRT);
1366 static inline bool nested_cpu_has_vid(struct vmcs12 *vmcs12)
1368 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
1371 static inline bool nested_cpu_has_posted_intr(struct vmcs12 *vmcs12)
1373 return vmcs12->pin_based_vm_exec_control & PIN_BASED_POSTED_INTR;
1376 static inline bool nested_cpu_has_vmfunc(struct vmcs12 *vmcs12)
1378 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_VMFUNC);
1381 static inline bool nested_cpu_has_eptp_switching(struct vmcs12 *vmcs12)
1383 return nested_cpu_has_vmfunc(vmcs12) &&
1384 (vmcs12->vm_function_control &
1385 VMX_VMFUNC_EPTP_SWITCHING);
1388 static inline bool is_nmi(u32 intr_info)
1390 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK))
1391 == (INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK);
1394 static void nested_vmx_vmexit(struct kvm_vcpu *vcpu, u32 exit_reason,
1396 unsigned long exit_qualification);
1397 static void nested_vmx_entry_failure(struct kvm_vcpu *vcpu,
1398 struct vmcs12 *vmcs12,
1399 u32 reason, unsigned long qualification);
1401 static int __find_msr_index(struct vcpu_vmx *vmx, u32 msr)
1405 for (i = 0; i < vmx->nmsrs; ++i)
1406 if (vmx_msr_index[vmx->guest_msrs[i].index] == msr)
1411 static inline void __invvpid(int ext, u16 vpid, gva_t gva)
1417 } operand = { vpid, 0, gva };
1419 asm volatile (__ex(ASM_VMX_INVVPID)
1420 /* CF==1 or ZF==1 --> rc = -1 */
1421 "; ja 1f ; ud2 ; 1:"
1422 : : "a"(&operand), "c"(ext) : "cc", "memory");
1425 static inline void __invept(int ext, u64 eptp, gpa_t gpa)
1429 } operand = {eptp, gpa};
1431 asm volatile (__ex(ASM_VMX_INVEPT)
1432 /* CF==1 or ZF==1 --> rc = -1 */
1433 "; ja 1f ; ud2 ; 1:\n"
1434 : : "a" (&operand), "c" (ext) : "cc", "memory");
1437 static struct shared_msr_entry *find_msr_entry(struct vcpu_vmx *vmx, u32 msr)
1441 i = __find_msr_index(vmx, msr);
1443 return &vmx->guest_msrs[i];
1447 static void vmcs_clear(struct vmcs *vmcs)
1449 u64 phys_addr = __pa(vmcs);
1452 asm volatile (__ex(ASM_VMX_VMCLEAR_RAX) "; setna %0"
1453 : "=qm"(error) : "a"(&phys_addr), "m"(phys_addr)
1456 printk(KERN_ERR "kvm: vmclear fail: %p/%llx\n",
1460 static inline void loaded_vmcs_init(struct loaded_vmcs *loaded_vmcs)
1462 vmcs_clear(loaded_vmcs->vmcs);
1463 if (loaded_vmcs->shadow_vmcs && loaded_vmcs->launched)
1464 vmcs_clear(loaded_vmcs->shadow_vmcs);
1465 loaded_vmcs->cpu = -1;
1466 loaded_vmcs->launched = 0;
1469 static void vmcs_load(struct vmcs *vmcs)
1471 u64 phys_addr = __pa(vmcs);
1474 asm volatile (__ex(ASM_VMX_VMPTRLD_RAX) "; setna %0"
1475 : "=qm"(error) : "a"(&phys_addr), "m"(phys_addr)
1478 printk(KERN_ERR "kvm: vmptrld %p/%llx failed\n",
1482 #ifdef CONFIG_KEXEC_CORE
1484 * This bitmap is used to indicate whether the vmclear
1485 * operation is enabled on all cpus. All disabled by
1488 static cpumask_t crash_vmclear_enabled_bitmap = CPU_MASK_NONE;
1490 static inline void crash_enable_local_vmclear(int cpu)
1492 cpumask_set_cpu(cpu, &crash_vmclear_enabled_bitmap);
1495 static inline void crash_disable_local_vmclear(int cpu)
1497 cpumask_clear_cpu(cpu, &crash_vmclear_enabled_bitmap);
1500 static inline int crash_local_vmclear_enabled(int cpu)
1502 return cpumask_test_cpu(cpu, &crash_vmclear_enabled_bitmap);
1505 static void crash_vmclear_local_loaded_vmcss(void)
1507 int cpu = raw_smp_processor_id();
1508 struct loaded_vmcs *v;
1510 if (!crash_local_vmclear_enabled(cpu))
1513 list_for_each_entry(v, &per_cpu(loaded_vmcss_on_cpu, cpu),
1514 loaded_vmcss_on_cpu_link)
1515 vmcs_clear(v->vmcs);
1518 static inline void crash_enable_local_vmclear(int cpu) { }
1519 static inline void crash_disable_local_vmclear(int cpu) { }
1520 #endif /* CONFIG_KEXEC_CORE */
1522 static void __loaded_vmcs_clear(void *arg)
1524 struct loaded_vmcs *loaded_vmcs = arg;
1525 int cpu = raw_smp_processor_id();
1527 if (loaded_vmcs->cpu != cpu)
1528 return; /* vcpu migration can race with cpu offline */
1529 if (per_cpu(current_vmcs, cpu) == loaded_vmcs->vmcs)
1530 per_cpu(current_vmcs, cpu) = NULL;
1531 crash_disable_local_vmclear(cpu);
1532 list_del(&loaded_vmcs->loaded_vmcss_on_cpu_link);
1535 * we should ensure updating loaded_vmcs->loaded_vmcss_on_cpu_link
1536 * is before setting loaded_vmcs->vcpu to -1 which is done in
1537 * loaded_vmcs_init. Otherwise, other cpu can see vcpu = -1 fist
1538 * then adds the vmcs into percpu list before it is deleted.
1542 loaded_vmcs_init(loaded_vmcs);
1543 crash_enable_local_vmclear(cpu);
1546 static void loaded_vmcs_clear(struct loaded_vmcs *loaded_vmcs)
1548 int cpu = loaded_vmcs->cpu;
1551 smp_call_function_single(cpu,
1552 __loaded_vmcs_clear, loaded_vmcs, 1);
1555 static inline void vpid_sync_vcpu_single(int vpid)
1560 if (cpu_has_vmx_invvpid_single())
1561 __invvpid(VMX_VPID_EXTENT_SINGLE_CONTEXT, vpid, 0);
1564 static inline void vpid_sync_vcpu_global(void)
1566 if (cpu_has_vmx_invvpid_global())
1567 __invvpid(VMX_VPID_EXTENT_ALL_CONTEXT, 0, 0);
1570 static inline void vpid_sync_context(int vpid)
1572 if (cpu_has_vmx_invvpid_single())
1573 vpid_sync_vcpu_single(vpid);
1575 vpid_sync_vcpu_global();
1578 static inline void ept_sync_global(void)
1580 __invept(VMX_EPT_EXTENT_GLOBAL, 0, 0);
1583 static inline void ept_sync_context(u64 eptp)
1585 if (cpu_has_vmx_invept_context())
1586 __invept(VMX_EPT_EXTENT_CONTEXT, eptp, 0);
1591 static __always_inline void vmcs_check16(unsigned long field)
1593 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2000,
1594 "16-bit accessor invalid for 64-bit field");
1595 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2001,
1596 "16-bit accessor invalid for 64-bit high field");
1597 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x4000,
1598 "16-bit accessor invalid for 32-bit high field");
1599 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x6000,
1600 "16-bit accessor invalid for natural width field");
1603 static __always_inline void vmcs_check32(unsigned long field)
1605 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0,
1606 "32-bit accessor invalid for 16-bit field");
1607 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x6000,
1608 "32-bit accessor invalid for natural width field");
1611 static __always_inline void vmcs_check64(unsigned long field)
1613 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0,
1614 "64-bit accessor invalid for 16-bit field");
1615 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2001,
1616 "64-bit accessor invalid for 64-bit high field");
1617 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x4000,
1618 "64-bit accessor invalid for 32-bit field");
1619 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x6000,
1620 "64-bit accessor invalid for natural width field");
1623 static __always_inline void vmcs_checkl(unsigned long field)
1625 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0,
1626 "Natural width accessor invalid for 16-bit field");
1627 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2000,
1628 "Natural width accessor invalid for 64-bit field");
1629 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2001,
1630 "Natural width accessor invalid for 64-bit high field");
1631 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x4000,
1632 "Natural width accessor invalid for 32-bit field");
1635 static __always_inline unsigned long __vmcs_readl(unsigned long field)
1637 unsigned long value;
1639 asm volatile (__ex_clear(ASM_VMX_VMREAD_RDX_RAX, "%0")
1640 : "=a"(value) : "d"(field) : "cc");
1644 static __always_inline u16 vmcs_read16(unsigned long field)
1646 vmcs_check16(field);
1647 return __vmcs_readl(field);
1650 static __always_inline u32 vmcs_read32(unsigned long field)
1652 vmcs_check32(field);
1653 return __vmcs_readl(field);
1656 static __always_inline u64 vmcs_read64(unsigned long field)
1658 vmcs_check64(field);
1659 #ifdef CONFIG_X86_64
1660 return __vmcs_readl(field);
1662 return __vmcs_readl(field) | ((u64)__vmcs_readl(field+1) << 32);
1666 static __always_inline unsigned long vmcs_readl(unsigned long field)
1669 return __vmcs_readl(field);
1672 static noinline void vmwrite_error(unsigned long field, unsigned long value)
1674 printk(KERN_ERR "vmwrite error: reg %lx value %lx (err %d)\n",
1675 field, value, vmcs_read32(VM_INSTRUCTION_ERROR));
1679 static __always_inline void __vmcs_writel(unsigned long field, unsigned long value)
1683 asm volatile (__ex(ASM_VMX_VMWRITE_RAX_RDX) "; setna %0"
1684 : "=q"(error) : "a"(value), "d"(field) : "cc");
1685 if (unlikely(error))
1686 vmwrite_error(field, value);
1689 static __always_inline void vmcs_write16(unsigned long field, u16 value)
1691 vmcs_check16(field);
1692 __vmcs_writel(field, value);
1695 static __always_inline void vmcs_write32(unsigned long field, u32 value)
1697 vmcs_check32(field);
1698 __vmcs_writel(field, value);
1701 static __always_inline void vmcs_write64(unsigned long field, u64 value)
1703 vmcs_check64(field);
1704 __vmcs_writel(field, value);
1705 #ifndef CONFIG_X86_64
1707 __vmcs_writel(field+1, value >> 32);
1711 static __always_inline void vmcs_writel(unsigned long field, unsigned long value)
1714 __vmcs_writel(field, value);
1717 static __always_inline void vmcs_clear_bits(unsigned long field, u32 mask)
1719 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x2000,
1720 "vmcs_clear_bits does not support 64-bit fields");
1721 __vmcs_writel(field, __vmcs_readl(field) & ~mask);
1724 static __always_inline void vmcs_set_bits(unsigned long field, u32 mask)
1726 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x2000,
1727 "vmcs_set_bits does not support 64-bit fields");
1728 __vmcs_writel(field, __vmcs_readl(field) | mask);
1731 static inline void vm_entry_controls_reset_shadow(struct vcpu_vmx *vmx)
1733 vmx->vm_entry_controls_shadow = vmcs_read32(VM_ENTRY_CONTROLS);
1736 static inline void vm_entry_controls_init(struct vcpu_vmx *vmx, u32 val)
1738 vmcs_write32(VM_ENTRY_CONTROLS, val);
1739 vmx->vm_entry_controls_shadow = val;
1742 static inline void vm_entry_controls_set(struct vcpu_vmx *vmx, u32 val)
1744 if (vmx->vm_entry_controls_shadow != val)
1745 vm_entry_controls_init(vmx, val);
1748 static inline u32 vm_entry_controls_get(struct vcpu_vmx *vmx)
1750 return vmx->vm_entry_controls_shadow;
1754 static inline void vm_entry_controls_setbit(struct vcpu_vmx *vmx, u32 val)
1756 vm_entry_controls_set(vmx, vm_entry_controls_get(vmx) | val);
1759 static inline void vm_entry_controls_clearbit(struct vcpu_vmx *vmx, u32 val)
1761 vm_entry_controls_set(vmx, vm_entry_controls_get(vmx) & ~val);
1764 static inline void vm_exit_controls_reset_shadow(struct vcpu_vmx *vmx)
1766 vmx->vm_exit_controls_shadow = vmcs_read32(VM_EXIT_CONTROLS);
1769 static inline void vm_exit_controls_init(struct vcpu_vmx *vmx, u32 val)
1771 vmcs_write32(VM_EXIT_CONTROLS, val);
1772 vmx->vm_exit_controls_shadow = val;
1775 static inline void vm_exit_controls_set(struct vcpu_vmx *vmx, u32 val)
1777 if (vmx->vm_exit_controls_shadow != val)
1778 vm_exit_controls_init(vmx, val);
1781 static inline u32 vm_exit_controls_get(struct vcpu_vmx *vmx)
1783 return vmx->vm_exit_controls_shadow;
1787 static inline void vm_exit_controls_setbit(struct vcpu_vmx *vmx, u32 val)
1789 vm_exit_controls_set(vmx, vm_exit_controls_get(vmx) | val);
1792 static inline void vm_exit_controls_clearbit(struct vcpu_vmx *vmx, u32 val)
1794 vm_exit_controls_set(vmx, vm_exit_controls_get(vmx) & ~val);
1797 static void vmx_segment_cache_clear(struct vcpu_vmx *vmx)
1799 vmx->segment_cache.bitmask = 0;
1802 static bool vmx_segment_cache_test_set(struct vcpu_vmx *vmx, unsigned seg,
1806 u32 mask = 1 << (seg * SEG_FIELD_NR + field);
1808 if (!(vmx->vcpu.arch.regs_avail & (1 << VCPU_EXREG_SEGMENTS))) {
1809 vmx->vcpu.arch.regs_avail |= (1 << VCPU_EXREG_SEGMENTS);
1810 vmx->segment_cache.bitmask = 0;
1812 ret = vmx->segment_cache.bitmask & mask;
1813 vmx->segment_cache.bitmask |= mask;
1817 static u16 vmx_read_guest_seg_selector(struct vcpu_vmx *vmx, unsigned seg)
1819 u16 *p = &vmx->segment_cache.seg[seg].selector;
1821 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_SEL))
1822 *p = vmcs_read16(kvm_vmx_segment_fields[seg].selector);
1826 static ulong vmx_read_guest_seg_base(struct vcpu_vmx *vmx, unsigned seg)
1828 ulong *p = &vmx->segment_cache.seg[seg].base;
1830 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_BASE))
1831 *p = vmcs_readl(kvm_vmx_segment_fields[seg].base);
1835 static u32 vmx_read_guest_seg_limit(struct vcpu_vmx *vmx, unsigned seg)
1837 u32 *p = &vmx->segment_cache.seg[seg].limit;
1839 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_LIMIT))
1840 *p = vmcs_read32(kvm_vmx_segment_fields[seg].limit);
1844 static u32 vmx_read_guest_seg_ar(struct vcpu_vmx *vmx, unsigned seg)
1846 u32 *p = &vmx->segment_cache.seg[seg].ar;
1848 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_AR))
1849 *p = vmcs_read32(kvm_vmx_segment_fields[seg].ar_bytes);
1853 static void update_exception_bitmap(struct kvm_vcpu *vcpu)
1857 eb = (1u << PF_VECTOR) | (1u << UD_VECTOR) | (1u << MC_VECTOR) |
1858 (1u << DB_VECTOR) | (1u << AC_VECTOR);
1859 if ((vcpu->guest_debug &
1860 (KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_SW_BP)) ==
1861 (KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_SW_BP))
1862 eb |= 1u << BP_VECTOR;
1863 if (to_vmx(vcpu)->rmode.vm86_active)
1866 eb &= ~(1u << PF_VECTOR); /* bypass_guest_pf = 0 */
1868 /* When we are running a nested L2 guest and L1 specified for it a
1869 * certain exception bitmap, we must trap the same exceptions and pass
1870 * them to L1. When running L2, we will only handle the exceptions
1871 * specified above if L1 did not want them.
1873 if (is_guest_mode(vcpu))
1874 eb |= get_vmcs12(vcpu)->exception_bitmap;
1876 vmcs_write32(EXCEPTION_BITMAP, eb);
1879 static void clear_atomic_switch_msr_special(struct vcpu_vmx *vmx,
1880 unsigned long entry, unsigned long exit)
1882 vm_entry_controls_clearbit(vmx, entry);
1883 vm_exit_controls_clearbit(vmx, exit);
1886 static void clear_atomic_switch_msr(struct vcpu_vmx *vmx, unsigned msr)
1889 struct msr_autoload *m = &vmx->msr_autoload;
1893 if (cpu_has_load_ia32_efer) {
1894 clear_atomic_switch_msr_special(vmx,
1895 VM_ENTRY_LOAD_IA32_EFER,
1896 VM_EXIT_LOAD_IA32_EFER);
1900 case MSR_CORE_PERF_GLOBAL_CTRL:
1901 if (cpu_has_load_perf_global_ctrl) {
1902 clear_atomic_switch_msr_special(vmx,
1903 VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL,
1904 VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL);
1910 for (i = 0; i < m->nr; ++i)
1911 if (m->guest[i].index == msr)
1917 m->guest[i] = m->guest[m->nr];
1918 m->host[i] = m->host[m->nr];
1919 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, m->nr);
1920 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, m->nr);
1923 static void add_atomic_switch_msr_special(struct vcpu_vmx *vmx,
1924 unsigned long entry, unsigned long exit,
1925 unsigned long guest_val_vmcs, unsigned long host_val_vmcs,
1926 u64 guest_val, u64 host_val)
1928 vmcs_write64(guest_val_vmcs, guest_val);
1929 vmcs_write64(host_val_vmcs, host_val);
1930 vm_entry_controls_setbit(vmx, entry);
1931 vm_exit_controls_setbit(vmx, exit);
1934 static void add_atomic_switch_msr(struct vcpu_vmx *vmx, unsigned msr,
1935 u64 guest_val, u64 host_val)
1938 struct msr_autoload *m = &vmx->msr_autoload;
1942 if (cpu_has_load_ia32_efer) {
1943 add_atomic_switch_msr_special(vmx,
1944 VM_ENTRY_LOAD_IA32_EFER,
1945 VM_EXIT_LOAD_IA32_EFER,
1948 guest_val, host_val);
1952 case MSR_CORE_PERF_GLOBAL_CTRL:
1953 if (cpu_has_load_perf_global_ctrl) {
1954 add_atomic_switch_msr_special(vmx,
1955 VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL,
1956 VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL,
1957 GUEST_IA32_PERF_GLOBAL_CTRL,
1958 HOST_IA32_PERF_GLOBAL_CTRL,
1959 guest_val, host_val);
1963 case MSR_IA32_PEBS_ENABLE:
1964 /* PEBS needs a quiescent period after being disabled (to write
1965 * a record). Disabling PEBS through VMX MSR swapping doesn't
1966 * provide that period, so a CPU could write host's record into
1969 wrmsrl(MSR_IA32_PEBS_ENABLE, 0);
1972 for (i = 0; i < m->nr; ++i)
1973 if (m->guest[i].index == msr)
1976 if (i == NR_AUTOLOAD_MSRS) {
1977 printk_once(KERN_WARNING "Not enough msr switch entries. "
1978 "Can't add msr %x\n", msr);
1980 } else if (i == m->nr) {
1982 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, m->nr);
1983 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, m->nr);
1986 m->guest[i].index = msr;
1987 m->guest[i].value = guest_val;
1988 m->host[i].index = msr;
1989 m->host[i].value = host_val;
1992 static bool update_transition_efer(struct vcpu_vmx *vmx, int efer_offset)
1994 u64 guest_efer = vmx->vcpu.arch.efer;
1995 u64 ignore_bits = 0;
1999 * NX is needed to handle CR0.WP=1, CR4.SMEP=1. Testing
2000 * host CPUID is more efficient than testing guest CPUID
2001 * or CR4. Host SMEP is anyway a requirement for guest SMEP.
2003 if (boot_cpu_has(X86_FEATURE_SMEP))
2004 guest_efer |= EFER_NX;
2005 else if (!(guest_efer & EFER_NX))
2006 ignore_bits |= EFER_NX;
2010 * LMA and LME handled by hardware; SCE meaningless outside long mode.
2012 ignore_bits |= EFER_SCE;
2013 #ifdef CONFIG_X86_64
2014 ignore_bits |= EFER_LMA | EFER_LME;
2015 /* SCE is meaningful only in long mode on Intel */
2016 if (guest_efer & EFER_LMA)
2017 ignore_bits &= ~(u64)EFER_SCE;
2020 clear_atomic_switch_msr(vmx, MSR_EFER);
2023 * On EPT, we can't emulate NX, so we must switch EFER atomically.
2024 * On CPUs that support "load IA32_EFER", always switch EFER
2025 * atomically, since it's faster than switching it manually.
2027 if (cpu_has_load_ia32_efer ||
2028 (enable_ept && ((vmx->vcpu.arch.efer ^ host_efer) & EFER_NX))) {
2029 if (!(guest_efer & EFER_LMA))
2030 guest_efer &= ~EFER_LME;
2031 if (guest_efer != host_efer)
2032 add_atomic_switch_msr(vmx, MSR_EFER,
2033 guest_efer, host_efer);
2036 guest_efer &= ~ignore_bits;
2037 guest_efer |= host_efer & ignore_bits;
2039 vmx->guest_msrs[efer_offset].data = guest_efer;
2040 vmx->guest_msrs[efer_offset].mask = ~ignore_bits;
2046 #ifdef CONFIG_X86_32
2048 * On 32-bit kernels, VM exits still load the FS and GS bases from the
2049 * VMCS rather than the segment table. KVM uses this helper to figure
2050 * out the current bases to poke them into the VMCS before entry.
2052 static unsigned long segment_base(u16 selector)
2054 struct desc_struct *table;
2057 if (!(selector & ~SEGMENT_RPL_MASK))
2060 table = get_current_gdt_ro();
2062 if ((selector & SEGMENT_TI_MASK) == SEGMENT_LDT) {
2063 u16 ldt_selector = kvm_read_ldt();
2065 if (!(ldt_selector & ~SEGMENT_RPL_MASK))
2068 table = (struct desc_struct *)segment_base(ldt_selector);
2070 v = get_desc_base(&table[selector >> 3]);
2075 static void vmx_save_host_state(struct kvm_vcpu *vcpu)
2077 struct vcpu_vmx *vmx = to_vmx(vcpu);
2080 if (vmx->host_state.loaded)
2083 vmx->host_state.loaded = 1;
2085 * Set host fs and gs selectors. Unfortunately, 22.2.3 does not
2086 * allow segment selectors with cpl > 0 or ti == 1.
2088 vmx->host_state.ldt_sel = kvm_read_ldt();
2089 vmx->host_state.gs_ldt_reload_needed = vmx->host_state.ldt_sel;
2090 savesegment(fs, vmx->host_state.fs_sel);
2091 if (!(vmx->host_state.fs_sel & 7)) {
2092 vmcs_write16(HOST_FS_SELECTOR, vmx->host_state.fs_sel);
2093 vmx->host_state.fs_reload_needed = 0;
2095 vmcs_write16(HOST_FS_SELECTOR, 0);
2096 vmx->host_state.fs_reload_needed = 1;
2098 savesegment(gs, vmx->host_state.gs_sel);
2099 if (!(vmx->host_state.gs_sel & 7))
2100 vmcs_write16(HOST_GS_SELECTOR, vmx->host_state.gs_sel);
2102 vmcs_write16(HOST_GS_SELECTOR, 0);
2103 vmx->host_state.gs_ldt_reload_needed = 1;
2106 #ifdef CONFIG_X86_64
2107 savesegment(ds, vmx->host_state.ds_sel);
2108 savesegment(es, vmx->host_state.es_sel);
2111 #ifdef CONFIG_X86_64
2112 vmcs_writel(HOST_FS_BASE, read_msr(MSR_FS_BASE));
2113 vmcs_writel(HOST_GS_BASE, read_msr(MSR_GS_BASE));
2115 vmcs_writel(HOST_FS_BASE, segment_base(vmx->host_state.fs_sel));
2116 vmcs_writel(HOST_GS_BASE, segment_base(vmx->host_state.gs_sel));
2119 #ifdef CONFIG_X86_64
2120 rdmsrl(MSR_KERNEL_GS_BASE, vmx->msr_host_kernel_gs_base);
2121 if (is_long_mode(&vmx->vcpu))
2122 wrmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base);
2124 if (boot_cpu_has(X86_FEATURE_MPX))
2125 rdmsrl(MSR_IA32_BNDCFGS, vmx->host_state.msr_host_bndcfgs);
2126 for (i = 0; i < vmx->save_nmsrs; ++i)
2127 kvm_set_shared_msr(vmx->guest_msrs[i].index,
2128 vmx->guest_msrs[i].data,
2129 vmx->guest_msrs[i].mask);
2132 static void __vmx_load_host_state(struct vcpu_vmx *vmx)
2134 if (!vmx->host_state.loaded)
2137 ++vmx->vcpu.stat.host_state_reload;
2138 vmx->host_state.loaded = 0;
2139 #ifdef CONFIG_X86_64
2140 if (is_long_mode(&vmx->vcpu))
2141 rdmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base);
2143 if (vmx->host_state.gs_ldt_reload_needed) {
2144 kvm_load_ldt(vmx->host_state.ldt_sel);
2145 #ifdef CONFIG_X86_64
2146 load_gs_index(vmx->host_state.gs_sel);
2148 loadsegment(gs, vmx->host_state.gs_sel);
2151 if (vmx->host_state.fs_reload_needed)
2152 loadsegment(fs, vmx->host_state.fs_sel);
2153 #ifdef CONFIG_X86_64
2154 if (unlikely(vmx->host_state.ds_sel | vmx->host_state.es_sel)) {
2155 loadsegment(ds, vmx->host_state.ds_sel);
2156 loadsegment(es, vmx->host_state.es_sel);
2159 invalidate_tss_limit();
2160 #ifdef CONFIG_X86_64
2161 wrmsrl(MSR_KERNEL_GS_BASE, vmx->msr_host_kernel_gs_base);
2163 if (vmx->host_state.msr_host_bndcfgs)
2164 wrmsrl(MSR_IA32_BNDCFGS, vmx->host_state.msr_host_bndcfgs);
2165 load_fixmap_gdt(raw_smp_processor_id());
2168 static void vmx_load_host_state(struct vcpu_vmx *vmx)
2171 __vmx_load_host_state(vmx);
2175 static void vmx_vcpu_pi_load(struct kvm_vcpu *vcpu, int cpu)
2177 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
2178 struct pi_desc old, new;
2182 * In case of hot-plug or hot-unplug, we may have to undo
2183 * vmx_vcpu_pi_put even if there is no assigned device. And we
2184 * always keep PI.NDST up to date for simplicity: it makes the
2185 * code easier, and CPU migration is not a fast path.
2187 if (!pi_test_sn(pi_desc) && vcpu->cpu == cpu)
2191 * First handle the simple case where no cmpxchg is necessary; just
2192 * allow posting non-urgent interrupts.
2194 * If the 'nv' field is POSTED_INTR_WAKEUP_VECTOR, do not change
2195 * PI.NDST: pi_post_block will do it for us and the wakeup_handler
2196 * expects the VCPU to be on the blocked_vcpu_list that matches
2199 if (pi_desc->nv == POSTED_INTR_WAKEUP_VECTOR ||
2201 pi_clear_sn(pi_desc);
2205 /* The full case. */
2207 old.control = new.control = pi_desc->control;
2209 dest = cpu_physical_id(cpu);
2211 if (x2apic_enabled())
2214 new.ndst = (dest << 8) & 0xFF00;
2217 } while (cmpxchg64(&pi_desc->control, old.control,
2218 new.control) != old.control);
2221 static void decache_tsc_multiplier(struct vcpu_vmx *vmx)
2223 vmx->current_tsc_ratio = vmx->vcpu.arch.tsc_scaling_ratio;
2224 vmcs_write64(TSC_MULTIPLIER, vmx->current_tsc_ratio);
2228 * Switches to specified vcpu, until a matching vcpu_put(), but assumes
2229 * vcpu mutex is already taken.
2231 static void vmx_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
2233 struct vcpu_vmx *vmx = to_vmx(vcpu);
2234 bool already_loaded = vmx->loaded_vmcs->cpu == cpu;
2236 if (!already_loaded) {
2237 loaded_vmcs_clear(vmx->loaded_vmcs);
2238 local_irq_disable();
2239 crash_disable_local_vmclear(cpu);
2242 * Read loaded_vmcs->cpu should be before fetching
2243 * loaded_vmcs->loaded_vmcss_on_cpu_link.
2244 * See the comments in __loaded_vmcs_clear().
2248 list_add(&vmx->loaded_vmcs->loaded_vmcss_on_cpu_link,
2249 &per_cpu(loaded_vmcss_on_cpu, cpu));
2250 crash_enable_local_vmclear(cpu);
2254 if (per_cpu(current_vmcs, cpu) != vmx->loaded_vmcs->vmcs) {
2255 per_cpu(current_vmcs, cpu) = vmx->loaded_vmcs->vmcs;
2256 vmcs_load(vmx->loaded_vmcs->vmcs);
2259 if (!already_loaded) {
2260 void *gdt = get_current_gdt_ro();
2261 unsigned long sysenter_esp;
2263 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
2266 * Linux uses per-cpu TSS and GDT, so set these when switching
2267 * processors. See 22.2.4.
2269 vmcs_writel(HOST_TR_BASE,
2270 (unsigned long)&get_cpu_entry_area(cpu)->tss.x86_tss);
2271 vmcs_writel(HOST_GDTR_BASE, (unsigned long)gdt); /* 22.2.4 */
2274 * VM exits change the host TR limit to 0x67 after a VM
2275 * exit. This is okay, since 0x67 covers everything except
2276 * the IO bitmap and have have code to handle the IO bitmap
2277 * being lost after a VM exit.
2279 BUILD_BUG_ON(IO_BITMAP_OFFSET - 1 != 0x67);
2281 rdmsrl(MSR_IA32_SYSENTER_ESP, sysenter_esp);
2282 vmcs_writel(HOST_IA32_SYSENTER_ESP, sysenter_esp); /* 22.2.3 */
2284 vmx->loaded_vmcs->cpu = cpu;
2287 /* Setup TSC multiplier */
2288 if (kvm_has_tsc_control &&
2289 vmx->current_tsc_ratio != vcpu->arch.tsc_scaling_ratio)
2290 decache_tsc_multiplier(vmx);
2292 vmx_vcpu_pi_load(vcpu, cpu);
2293 vmx->host_pkru = read_pkru();
2294 vmx->host_debugctlmsr = get_debugctlmsr();
2297 static void vmx_vcpu_pi_put(struct kvm_vcpu *vcpu)
2299 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
2301 if (!kvm_arch_has_assigned_device(vcpu->kvm) ||
2302 !irq_remapping_cap(IRQ_POSTING_CAP) ||
2303 !kvm_vcpu_apicv_active(vcpu))
2306 /* Set SN when the vCPU is preempted */
2307 if (vcpu->preempted)
2311 static void vmx_vcpu_put(struct kvm_vcpu *vcpu)
2313 vmx_vcpu_pi_put(vcpu);
2315 __vmx_load_host_state(to_vmx(vcpu));
2318 static bool emulation_required(struct kvm_vcpu *vcpu)
2320 return emulate_invalid_guest_state && !guest_state_valid(vcpu);
2323 static void vmx_decache_cr0_guest_bits(struct kvm_vcpu *vcpu);
2326 * Return the cr0 value that a nested guest would read. This is a combination
2327 * of the real cr0 used to run the guest (guest_cr0), and the bits shadowed by
2328 * its hypervisor (cr0_read_shadow).
2330 static inline unsigned long nested_read_cr0(struct vmcs12 *fields)
2332 return (fields->guest_cr0 & ~fields->cr0_guest_host_mask) |
2333 (fields->cr0_read_shadow & fields->cr0_guest_host_mask);
2335 static inline unsigned long nested_read_cr4(struct vmcs12 *fields)
2337 return (fields->guest_cr4 & ~fields->cr4_guest_host_mask) |
2338 (fields->cr4_read_shadow & fields->cr4_guest_host_mask);
2341 static unsigned long vmx_get_rflags(struct kvm_vcpu *vcpu)
2343 unsigned long rflags, save_rflags;
2345 if (!test_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail)) {
2346 __set_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail);
2347 rflags = vmcs_readl(GUEST_RFLAGS);
2348 if (to_vmx(vcpu)->rmode.vm86_active) {
2349 rflags &= RMODE_GUEST_OWNED_EFLAGS_BITS;
2350 save_rflags = to_vmx(vcpu)->rmode.save_rflags;
2351 rflags |= save_rflags & ~RMODE_GUEST_OWNED_EFLAGS_BITS;
2353 to_vmx(vcpu)->rflags = rflags;
2355 return to_vmx(vcpu)->rflags;
2358 static void vmx_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags)
2360 unsigned long old_rflags = vmx_get_rflags(vcpu);
2362 __set_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail);
2363 to_vmx(vcpu)->rflags = rflags;
2364 if (to_vmx(vcpu)->rmode.vm86_active) {
2365 to_vmx(vcpu)->rmode.save_rflags = rflags;
2366 rflags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM;
2368 vmcs_writel(GUEST_RFLAGS, rflags);
2370 if ((old_rflags ^ to_vmx(vcpu)->rflags) & X86_EFLAGS_VM)
2371 to_vmx(vcpu)->emulation_required = emulation_required(vcpu);
2374 static u32 vmx_get_interrupt_shadow(struct kvm_vcpu *vcpu)
2376 u32 interruptibility = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
2379 if (interruptibility & GUEST_INTR_STATE_STI)
2380 ret |= KVM_X86_SHADOW_INT_STI;
2381 if (interruptibility & GUEST_INTR_STATE_MOV_SS)
2382 ret |= KVM_X86_SHADOW_INT_MOV_SS;
2387 static void vmx_set_interrupt_shadow(struct kvm_vcpu *vcpu, int mask)
2389 u32 interruptibility_old = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
2390 u32 interruptibility = interruptibility_old;
2392 interruptibility &= ~(GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS);
2394 if (mask & KVM_X86_SHADOW_INT_MOV_SS)
2395 interruptibility |= GUEST_INTR_STATE_MOV_SS;
2396 else if (mask & KVM_X86_SHADOW_INT_STI)
2397 interruptibility |= GUEST_INTR_STATE_STI;
2399 if ((interruptibility != interruptibility_old))
2400 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, interruptibility);
2403 static void skip_emulated_instruction(struct kvm_vcpu *vcpu)
2407 rip = kvm_rip_read(vcpu);
2408 rip += vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
2409 kvm_rip_write(vcpu, rip);
2411 /* skipping an emulated instruction also counts */
2412 vmx_set_interrupt_shadow(vcpu, 0);
2415 static void nested_vmx_inject_exception_vmexit(struct kvm_vcpu *vcpu,
2416 unsigned long exit_qual)
2418 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
2419 unsigned int nr = vcpu->arch.exception.nr;
2420 u32 intr_info = nr | INTR_INFO_VALID_MASK;
2422 if (vcpu->arch.exception.has_error_code) {
2423 vmcs12->vm_exit_intr_error_code = vcpu->arch.exception.error_code;
2424 intr_info |= INTR_INFO_DELIVER_CODE_MASK;
2427 if (kvm_exception_is_soft(nr))
2428 intr_info |= INTR_TYPE_SOFT_EXCEPTION;
2430 intr_info |= INTR_TYPE_HARD_EXCEPTION;
2432 if (!(vmcs12->idt_vectoring_info_field & VECTORING_INFO_VALID_MASK) &&
2433 vmx_get_nmi_mask(vcpu))
2434 intr_info |= INTR_INFO_UNBLOCK_NMI;
2436 nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI, intr_info, exit_qual);
2440 * KVM wants to inject page-faults which it got to the guest. This function
2441 * checks whether in a nested guest, we need to inject them to L1 or L2.
2443 static int nested_vmx_check_exception(struct kvm_vcpu *vcpu, unsigned long *exit_qual)
2445 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
2446 unsigned int nr = vcpu->arch.exception.nr;
2448 if (nr == PF_VECTOR) {
2449 if (vcpu->arch.exception.nested_apf) {
2450 *exit_qual = vcpu->arch.apf.nested_apf_token;
2454 * FIXME: we must not write CR2 when L1 intercepts an L2 #PF exception.
2455 * The fix is to add the ancillary datum (CR2 or DR6) to structs
2456 * kvm_queued_exception and kvm_vcpu_events, so that CR2 and DR6
2457 * can be written only when inject_pending_event runs. This should be
2458 * conditional on a new capability---if the capability is disabled,
2459 * kvm_multiple_exception would write the ancillary information to
2460 * CR2 or DR6, for backwards ABI-compatibility.
2462 if (nested_vmx_is_page_fault_vmexit(vmcs12,
2463 vcpu->arch.exception.error_code)) {
2464 *exit_qual = vcpu->arch.cr2;
2468 if (vmcs12->exception_bitmap & (1u << nr)) {
2469 if (nr == DB_VECTOR)
2470 *exit_qual = vcpu->arch.dr6;
2480 static void vmx_queue_exception(struct kvm_vcpu *vcpu)
2482 struct vcpu_vmx *vmx = to_vmx(vcpu);
2483 unsigned nr = vcpu->arch.exception.nr;
2484 bool has_error_code = vcpu->arch.exception.has_error_code;
2485 u32 error_code = vcpu->arch.exception.error_code;
2486 u32 intr_info = nr | INTR_INFO_VALID_MASK;
2488 if (has_error_code) {
2489 vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE, error_code);
2490 intr_info |= INTR_INFO_DELIVER_CODE_MASK;
2493 if (vmx->rmode.vm86_active) {
2495 if (kvm_exception_is_soft(nr))
2496 inc_eip = vcpu->arch.event_exit_inst_len;
2497 if (kvm_inject_realmode_interrupt(vcpu, nr, inc_eip) != EMULATE_DONE)
2498 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
2502 if (kvm_exception_is_soft(nr)) {
2503 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
2504 vmx->vcpu.arch.event_exit_inst_len);
2505 intr_info |= INTR_TYPE_SOFT_EXCEPTION;
2507 intr_info |= INTR_TYPE_HARD_EXCEPTION;
2509 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr_info);
2512 static bool vmx_rdtscp_supported(void)
2514 return cpu_has_vmx_rdtscp();
2517 static bool vmx_invpcid_supported(void)
2519 return cpu_has_vmx_invpcid() && enable_ept;
2523 * Swap MSR entry in host/guest MSR entry array.
2525 static void move_msr_up(struct vcpu_vmx *vmx, int from, int to)
2527 struct shared_msr_entry tmp;
2529 tmp = vmx->guest_msrs[to];
2530 vmx->guest_msrs[to] = vmx->guest_msrs[from];
2531 vmx->guest_msrs[from] = tmp;
2534 static void vmx_set_msr_bitmap(struct kvm_vcpu *vcpu)
2536 unsigned long *msr_bitmap;
2538 if (is_guest_mode(vcpu))
2539 msr_bitmap = to_vmx(vcpu)->nested.msr_bitmap;
2540 else if (cpu_has_secondary_exec_ctrls() &&
2541 (vmcs_read32(SECONDARY_VM_EXEC_CONTROL) &
2542 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE)) {
2543 if (enable_apicv && kvm_vcpu_apicv_active(vcpu)) {
2544 if (is_long_mode(vcpu))
2545 msr_bitmap = vmx_msr_bitmap_longmode_x2apic_apicv;
2547 msr_bitmap = vmx_msr_bitmap_legacy_x2apic_apicv;
2549 if (is_long_mode(vcpu))
2550 msr_bitmap = vmx_msr_bitmap_longmode_x2apic;
2552 msr_bitmap = vmx_msr_bitmap_legacy_x2apic;
2555 if (is_long_mode(vcpu))
2556 msr_bitmap = vmx_msr_bitmap_longmode;
2558 msr_bitmap = vmx_msr_bitmap_legacy;
2561 vmcs_write64(MSR_BITMAP, __pa(msr_bitmap));
2565 * Set up the vmcs to automatically save and restore system
2566 * msrs. Don't touch the 64-bit msrs if the guest is in legacy
2567 * mode, as fiddling with msrs is very expensive.
2569 static void setup_msrs(struct vcpu_vmx *vmx)
2571 int save_nmsrs, index;
2574 #ifdef CONFIG_X86_64
2575 if (is_long_mode(&vmx->vcpu)) {
2576 index = __find_msr_index(vmx, MSR_SYSCALL_MASK);
2578 move_msr_up(vmx, index, save_nmsrs++);
2579 index = __find_msr_index(vmx, MSR_LSTAR);
2581 move_msr_up(vmx, index, save_nmsrs++);
2582 index = __find_msr_index(vmx, MSR_CSTAR);
2584 move_msr_up(vmx, index, save_nmsrs++);
2585 index = __find_msr_index(vmx, MSR_TSC_AUX);
2586 if (index >= 0 && guest_cpuid_has(&vmx->vcpu, X86_FEATURE_RDTSCP))
2587 move_msr_up(vmx, index, save_nmsrs++);
2589 * MSR_STAR is only needed on long mode guests, and only
2590 * if efer.sce is enabled.
2592 index = __find_msr_index(vmx, MSR_STAR);
2593 if ((index >= 0) && (vmx->vcpu.arch.efer & EFER_SCE))
2594 move_msr_up(vmx, index, save_nmsrs++);
2597 index = __find_msr_index(vmx, MSR_EFER);
2598 if (index >= 0 && update_transition_efer(vmx, index))
2599 move_msr_up(vmx, index, save_nmsrs++);
2601 vmx->save_nmsrs = save_nmsrs;
2603 if (cpu_has_vmx_msr_bitmap())
2604 vmx_set_msr_bitmap(&vmx->vcpu);
2608 * reads and returns guest's timestamp counter "register"
2609 * guest_tsc = (host_tsc * tsc multiplier) >> 48 + tsc_offset
2610 * -- Intel TSC Scaling for Virtualization White Paper, sec 1.3
2612 static u64 guest_read_tsc(struct kvm_vcpu *vcpu)
2614 u64 host_tsc, tsc_offset;
2617 tsc_offset = vmcs_read64(TSC_OFFSET);
2618 return kvm_scale_tsc(vcpu, host_tsc) + tsc_offset;
2622 * writes 'offset' into guest's timestamp counter offset register
2624 static void vmx_write_tsc_offset(struct kvm_vcpu *vcpu, u64 offset)
2626 if (is_guest_mode(vcpu)) {
2628 * We're here if L1 chose not to trap WRMSR to TSC. According
2629 * to the spec, this should set L1's TSC; The offset that L1
2630 * set for L2 remains unchanged, and still needs to be added
2631 * to the newly set TSC to get L2's TSC.
2633 struct vmcs12 *vmcs12;
2634 /* recalculate vmcs02.TSC_OFFSET: */
2635 vmcs12 = get_vmcs12(vcpu);
2636 vmcs_write64(TSC_OFFSET, offset +
2637 (nested_cpu_has(vmcs12, CPU_BASED_USE_TSC_OFFSETING) ?
2638 vmcs12->tsc_offset : 0));
2640 trace_kvm_write_tsc_offset(vcpu->vcpu_id,
2641 vmcs_read64(TSC_OFFSET), offset);
2642 vmcs_write64(TSC_OFFSET, offset);
2647 * nested_vmx_allowed() checks whether a guest should be allowed to use VMX
2648 * instructions and MSRs (i.e., nested VMX). Nested VMX is disabled for
2649 * all guests if the "nested" module option is off, and can also be disabled
2650 * for a single guest by disabling its VMX cpuid bit.
2652 static inline bool nested_vmx_allowed(struct kvm_vcpu *vcpu)
2654 return nested && guest_cpuid_has(vcpu, X86_FEATURE_VMX);
2658 * nested_vmx_setup_ctls_msrs() sets up variables containing the values to be
2659 * returned for the various VMX controls MSRs when nested VMX is enabled.
2660 * The same values should also be used to verify that vmcs12 control fields are
2661 * valid during nested entry from L1 to L2.
2662 * Each of these control msrs has a low and high 32-bit half: A low bit is on
2663 * if the corresponding bit in the (32-bit) control field *must* be on, and a
2664 * bit in the high half is on if the corresponding bit in the control field
2665 * may be on. See also vmx_control_verify().
2667 static void nested_vmx_setup_ctls_msrs(struct vcpu_vmx *vmx)
2670 * Note that as a general rule, the high half of the MSRs (bits in
2671 * the control fields which may be 1) should be initialized by the
2672 * intersection of the underlying hardware's MSR (i.e., features which
2673 * can be supported) and the list of features we want to expose -
2674 * because they are known to be properly supported in our code.
2675 * Also, usually, the low half of the MSRs (bits which must be 1) can
2676 * be set to 0, meaning that L1 may turn off any of these bits. The
2677 * reason is that if one of these bits is necessary, it will appear
2678 * in vmcs01 and prepare_vmcs02, when it bitwise-or's the control
2679 * fields of vmcs01 and vmcs02, will turn these bits off - and
2680 * nested_vmx_exit_reflected() will not pass related exits to L1.
2681 * These rules have exceptions below.
2684 /* pin-based controls */
2685 rdmsr(MSR_IA32_VMX_PINBASED_CTLS,
2686 vmx->nested.nested_vmx_pinbased_ctls_low,
2687 vmx->nested.nested_vmx_pinbased_ctls_high);
2688 vmx->nested.nested_vmx_pinbased_ctls_low |=
2689 PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
2690 vmx->nested.nested_vmx_pinbased_ctls_high &=
2691 PIN_BASED_EXT_INTR_MASK |
2692 PIN_BASED_NMI_EXITING |
2693 PIN_BASED_VIRTUAL_NMIS;
2694 vmx->nested.nested_vmx_pinbased_ctls_high |=
2695 PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR |
2696 PIN_BASED_VMX_PREEMPTION_TIMER;
2697 if (kvm_vcpu_apicv_active(&vmx->vcpu))
2698 vmx->nested.nested_vmx_pinbased_ctls_high |=
2699 PIN_BASED_POSTED_INTR;
2702 rdmsr(MSR_IA32_VMX_EXIT_CTLS,
2703 vmx->nested.nested_vmx_exit_ctls_low,
2704 vmx->nested.nested_vmx_exit_ctls_high);
2705 vmx->nested.nested_vmx_exit_ctls_low =
2706 VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR;
2708 vmx->nested.nested_vmx_exit_ctls_high &=
2709 #ifdef CONFIG_X86_64
2710 VM_EXIT_HOST_ADDR_SPACE_SIZE |
2712 VM_EXIT_LOAD_IA32_PAT | VM_EXIT_SAVE_IA32_PAT;
2713 vmx->nested.nested_vmx_exit_ctls_high |=
2714 VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR |
2715 VM_EXIT_LOAD_IA32_EFER | VM_EXIT_SAVE_IA32_EFER |
2716 VM_EXIT_SAVE_VMX_PREEMPTION_TIMER | VM_EXIT_ACK_INTR_ON_EXIT;
2718 if (kvm_mpx_supported())
2719 vmx->nested.nested_vmx_exit_ctls_high |= VM_EXIT_CLEAR_BNDCFGS;
2721 /* We support free control of debug control saving. */
2722 vmx->nested.nested_vmx_exit_ctls_low &= ~VM_EXIT_SAVE_DEBUG_CONTROLS;
2724 /* entry controls */
2725 rdmsr(MSR_IA32_VMX_ENTRY_CTLS,
2726 vmx->nested.nested_vmx_entry_ctls_low,
2727 vmx->nested.nested_vmx_entry_ctls_high);
2728 vmx->nested.nested_vmx_entry_ctls_low =
2729 VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR;
2730 vmx->nested.nested_vmx_entry_ctls_high &=
2731 #ifdef CONFIG_X86_64
2732 VM_ENTRY_IA32E_MODE |
2734 VM_ENTRY_LOAD_IA32_PAT;
2735 vmx->nested.nested_vmx_entry_ctls_high |=
2736 (VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR | VM_ENTRY_LOAD_IA32_EFER);
2737 if (kvm_mpx_supported())
2738 vmx->nested.nested_vmx_entry_ctls_high |= VM_ENTRY_LOAD_BNDCFGS;
2740 /* We support free control of debug control loading. */
2741 vmx->nested.nested_vmx_entry_ctls_low &= ~VM_ENTRY_LOAD_DEBUG_CONTROLS;
2743 /* cpu-based controls */
2744 rdmsr(MSR_IA32_VMX_PROCBASED_CTLS,
2745 vmx->nested.nested_vmx_procbased_ctls_low,
2746 vmx->nested.nested_vmx_procbased_ctls_high);
2747 vmx->nested.nested_vmx_procbased_ctls_low =
2748 CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
2749 vmx->nested.nested_vmx_procbased_ctls_high &=
2750 CPU_BASED_VIRTUAL_INTR_PENDING |
2751 CPU_BASED_VIRTUAL_NMI_PENDING | CPU_BASED_USE_TSC_OFFSETING |
2752 CPU_BASED_HLT_EXITING | CPU_BASED_INVLPG_EXITING |
2753 CPU_BASED_MWAIT_EXITING | CPU_BASED_CR3_LOAD_EXITING |
2754 CPU_BASED_CR3_STORE_EXITING |
2755 #ifdef CONFIG_X86_64
2756 CPU_BASED_CR8_LOAD_EXITING | CPU_BASED_CR8_STORE_EXITING |
2758 CPU_BASED_MOV_DR_EXITING | CPU_BASED_UNCOND_IO_EXITING |
2759 CPU_BASED_USE_IO_BITMAPS | CPU_BASED_MONITOR_TRAP_FLAG |
2760 CPU_BASED_MONITOR_EXITING | CPU_BASED_RDPMC_EXITING |
2761 CPU_BASED_RDTSC_EXITING | CPU_BASED_PAUSE_EXITING |
2762 CPU_BASED_TPR_SHADOW | CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
2764 * We can allow some features even when not supported by the
2765 * hardware. For example, L1 can specify an MSR bitmap - and we
2766 * can use it to avoid exits to L1 - even when L0 runs L2
2767 * without MSR bitmaps.
2769 vmx->nested.nested_vmx_procbased_ctls_high |=
2770 CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR |
2771 CPU_BASED_USE_MSR_BITMAPS;
2773 /* We support free control of CR3 access interception. */
2774 vmx->nested.nested_vmx_procbased_ctls_low &=
2775 ~(CPU_BASED_CR3_LOAD_EXITING | CPU_BASED_CR3_STORE_EXITING);
2778 * secondary cpu-based controls. Do not include those that
2779 * depend on CPUID bits, they are added later by vmx_cpuid_update.
2781 rdmsr(MSR_IA32_VMX_PROCBASED_CTLS2,
2782 vmx->nested.nested_vmx_secondary_ctls_low,
2783 vmx->nested.nested_vmx_secondary_ctls_high);
2784 vmx->nested.nested_vmx_secondary_ctls_low = 0;
2785 vmx->nested.nested_vmx_secondary_ctls_high &=
2786 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
2787 SECONDARY_EXEC_DESC |
2788 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
2789 SECONDARY_EXEC_APIC_REGISTER_VIRT |
2790 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY |
2791 SECONDARY_EXEC_WBINVD_EXITING;
2794 /* nested EPT: emulate EPT also to L1 */
2795 vmx->nested.nested_vmx_secondary_ctls_high |=
2796 SECONDARY_EXEC_ENABLE_EPT;
2797 vmx->nested.nested_vmx_ept_caps = VMX_EPT_PAGE_WALK_4_BIT |
2798 VMX_EPTP_WB_BIT | VMX_EPT_INVEPT_BIT;
2799 if (cpu_has_vmx_ept_execute_only())
2800 vmx->nested.nested_vmx_ept_caps |=
2801 VMX_EPT_EXECUTE_ONLY_BIT;
2802 vmx->nested.nested_vmx_ept_caps &= vmx_capability.ept;
2803 vmx->nested.nested_vmx_ept_caps |= VMX_EPT_EXTENT_GLOBAL_BIT |
2804 VMX_EPT_EXTENT_CONTEXT_BIT | VMX_EPT_2MB_PAGE_BIT |
2805 VMX_EPT_1GB_PAGE_BIT;
2806 if (enable_ept_ad_bits) {
2807 vmx->nested.nested_vmx_secondary_ctls_high |=
2808 SECONDARY_EXEC_ENABLE_PML;
2809 vmx->nested.nested_vmx_ept_caps |= VMX_EPT_AD_BIT;
2813 if (cpu_has_vmx_vmfunc()) {
2814 vmx->nested.nested_vmx_secondary_ctls_high |=
2815 SECONDARY_EXEC_ENABLE_VMFUNC;
2817 * Advertise EPTP switching unconditionally
2818 * since we emulate it
2821 vmx->nested.nested_vmx_vmfunc_controls =
2822 VMX_VMFUNC_EPTP_SWITCHING;
2826 * Old versions of KVM use the single-context version without
2827 * checking for support, so declare that it is supported even
2828 * though it is treated as global context. The alternative is
2829 * not failing the single-context invvpid, and it is worse.
2832 vmx->nested.nested_vmx_secondary_ctls_high |=
2833 SECONDARY_EXEC_ENABLE_VPID;
2834 vmx->nested.nested_vmx_vpid_caps = VMX_VPID_INVVPID_BIT |
2835 VMX_VPID_EXTENT_SUPPORTED_MASK;
2838 if (enable_unrestricted_guest)
2839 vmx->nested.nested_vmx_secondary_ctls_high |=
2840 SECONDARY_EXEC_UNRESTRICTED_GUEST;
2842 /* miscellaneous data */
2843 rdmsr(MSR_IA32_VMX_MISC,
2844 vmx->nested.nested_vmx_misc_low,
2845 vmx->nested.nested_vmx_misc_high);
2846 vmx->nested.nested_vmx_misc_low &= VMX_MISC_SAVE_EFER_LMA;
2847 vmx->nested.nested_vmx_misc_low |=
2848 VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE |
2849 VMX_MISC_ACTIVITY_HLT;
2850 vmx->nested.nested_vmx_misc_high = 0;
2853 * This MSR reports some information about VMX support. We
2854 * should return information about the VMX we emulate for the
2855 * guest, and the VMCS structure we give it - not about the
2856 * VMX support of the underlying hardware.
2858 vmx->nested.nested_vmx_basic =
2860 VMX_BASIC_TRUE_CTLS |
2861 ((u64)VMCS12_SIZE << VMX_BASIC_VMCS_SIZE_SHIFT) |
2862 (VMX_BASIC_MEM_TYPE_WB << VMX_BASIC_MEM_TYPE_SHIFT);
2864 if (cpu_has_vmx_basic_inout())
2865 vmx->nested.nested_vmx_basic |= VMX_BASIC_INOUT;
2868 * These MSRs specify bits which the guest must keep fixed on
2869 * while L1 is in VMXON mode (in L1's root mode, or running an L2).
2870 * We picked the standard core2 setting.
2872 #define VMXON_CR0_ALWAYSON (X86_CR0_PE | X86_CR0_PG | X86_CR0_NE)
2873 #define VMXON_CR4_ALWAYSON X86_CR4_VMXE
2874 vmx->nested.nested_vmx_cr0_fixed0 = VMXON_CR0_ALWAYSON;
2875 vmx->nested.nested_vmx_cr4_fixed0 = VMXON_CR4_ALWAYSON;
2877 /* These MSRs specify bits which the guest must keep fixed off. */
2878 rdmsrl(MSR_IA32_VMX_CR0_FIXED1, vmx->nested.nested_vmx_cr0_fixed1);
2879 rdmsrl(MSR_IA32_VMX_CR4_FIXED1, vmx->nested.nested_vmx_cr4_fixed1);
2881 /* highest index: VMX_PREEMPTION_TIMER_VALUE */
2882 vmx->nested.nested_vmx_vmcs_enum = VMCS12_MAX_FIELD_INDEX << 1;
2886 * if fixed0[i] == 1: val[i] must be 1
2887 * if fixed1[i] == 0: val[i] must be 0
2889 static inline bool fixed_bits_valid(u64 val, u64 fixed0, u64 fixed1)
2891 return ((val & fixed1) | fixed0) == val;
2894 static inline bool vmx_control_verify(u32 control, u32 low, u32 high)
2896 return fixed_bits_valid(control, low, high);
2899 static inline u64 vmx_control_msr(u32 low, u32 high)
2901 return low | ((u64)high << 32);
2904 static bool is_bitwise_subset(u64 superset, u64 subset, u64 mask)
2909 return (superset | subset) == superset;
2912 static int vmx_restore_vmx_basic(struct vcpu_vmx *vmx, u64 data)
2914 const u64 feature_and_reserved =
2915 /* feature (except bit 48; see below) */
2916 BIT_ULL(49) | BIT_ULL(54) | BIT_ULL(55) |
2918 BIT_ULL(31) | GENMASK_ULL(47, 45) | GENMASK_ULL(63, 56);
2919 u64 vmx_basic = vmx->nested.nested_vmx_basic;
2921 if (!is_bitwise_subset(vmx_basic, data, feature_and_reserved))
2925 * KVM does not emulate a version of VMX that constrains physical
2926 * addresses of VMX structures (e.g. VMCS) to 32-bits.
2928 if (data & BIT_ULL(48))
2931 if (vmx_basic_vmcs_revision_id(vmx_basic) !=
2932 vmx_basic_vmcs_revision_id(data))
2935 if (vmx_basic_vmcs_size(vmx_basic) > vmx_basic_vmcs_size(data))
2938 vmx->nested.nested_vmx_basic = data;
2943 vmx_restore_control_msr(struct vcpu_vmx *vmx, u32 msr_index, u64 data)
2948 switch (msr_index) {
2949 case MSR_IA32_VMX_TRUE_PINBASED_CTLS:
2950 lowp = &vmx->nested.nested_vmx_pinbased_ctls_low;
2951 highp = &vmx->nested.nested_vmx_pinbased_ctls_high;
2953 case MSR_IA32_VMX_TRUE_PROCBASED_CTLS:
2954 lowp = &vmx->nested.nested_vmx_procbased_ctls_low;
2955 highp = &vmx->nested.nested_vmx_procbased_ctls_high;
2957 case MSR_IA32_VMX_TRUE_EXIT_CTLS:
2958 lowp = &vmx->nested.nested_vmx_exit_ctls_low;
2959 highp = &vmx->nested.nested_vmx_exit_ctls_high;
2961 case MSR_IA32_VMX_TRUE_ENTRY_CTLS:
2962 lowp = &vmx->nested.nested_vmx_entry_ctls_low;
2963 highp = &vmx->nested.nested_vmx_entry_ctls_high;
2965 case MSR_IA32_VMX_PROCBASED_CTLS2:
2966 lowp = &vmx->nested.nested_vmx_secondary_ctls_low;
2967 highp = &vmx->nested.nested_vmx_secondary_ctls_high;
2973 supported = vmx_control_msr(*lowp, *highp);
2975 /* Check must-be-1 bits are still 1. */
2976 if (!is_bitwise_subset(data, supported, GENMASK_ULL(31, 0)))
2979 /* Check must-be-0 bits are still 0. */
2980 if (!is_bitwise_subset(supported, data, GENMASK_ULL(63, 32)))
2984 *highp = data >> 32;
2988 static int vmx_restore_vmx_misc(struct vcpu_vmx *vmx, u64 data)
2990 const u64 feature_and_reserved_bits =
2992 BIT_ULL(5) | GENMASK_ULL(8, 6) | BIT_ULL(14) | BIT_ULL(15) |
2993 BIT_ULL(28) | BIT_ULL(29) | BIT_ULL(30) |
2995 GENMASK_ULL(13, 9) | BIT_ULL(31);
2998 vmx_misc = vmx_control_msr(vmx->nested.nested_vmx_misc_low,
2999 vmx->nested.nested_vmx_misc_high);
3001 if (!is_bitwise_subset(vmx_misc, data, feature_and_reserved_bits))
3004 if ((vmx->nested.nested_vmx_pinbased_ctls_high &
3005 PIN_BASED_VMX_PREEMPTION_TIMER) &&
3006 vmx_misc_preemption_timer_rate(data) !=
3007 vmx_misc_preemption_timer_rate(vmx_misc))
3010 if (vmx_misc_cr3_count(data) > vmx_misc_cr3_count(vmx_misc))
3013 if (vmx_misc_max_msr(data) > vmx_misc_max_msr(vmx_misc))
3016 if (vmx_misc_mseg_revid(data) != vmx_misc_mseg_revid(vmx_misc))
3019 vmx->nested.nested_vmx_misc_low = data;
3020 vmx->nested.nested_vmx_misc_high = data >> 32;
3024 static int vmx_restore_vmx_ept_vpid_cap(struct vcpu_vmx *vmx, u64 data)
3026 u64 vmx_ept_vpid_cap;
3028 vmx_ept_vpid_cap = vmx_control_msr(vmx->nested.nested_vmx_ept_caps,
3029 vmx->nested.nested_vmx_vpid_caps);
3031 /* Every bit is either reserved or a feature bit. */
3032 if (!is_bitwise_subset(vmx_ept_vpid_cap, data, -1ULL))
3035 vmx->nested.nested_vmx_ept_caps = data;
3036 vmx->nested.nested_vmx_vpid_caps = data >> 32;
3040 static int vmx_restore_fixed0_msr(struct vcpu_vmx *vmx, u32 msr_index, u64 data)
3044 switch (msr_index) {
3045 case MSR_IA32_VMX_CR0_FIXED0:
3046 msr = &vmx->nested.nested_vmx_cr0_fixed0;
3048 case MSR_IA32_VMX_CR4_FIXED0:
3049 msr = &vmx->nested.nested_vmx_cr4_fixed0;
3056 * 1 bits (which indicates bits which "must-be-1" during VMX operation)
3057 * must be 1 in the restored value.
3059 if (!is_bitwise_subset(data, *msr, -1ULL))
3067 * Called when userspace is restoring VMX MSRs.
3069 * Returns 0 on success, non-0 otherwise.
3071 static int vmx_set_vmx_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 data)
3073 struct vcpu_vmx *vmx = to_vmx(vcpu);
3075 switch (msr_index) {
3076 case MSR_IA32_VMX_BASIC:
3077 return vmx_restore_vmx_basic(vmx, data);
3078 case MSR_IA32_VMX_PINBASED_CTLS:
3079 case MSR_IA32_VMX_PROCBASED_CTLS:
3080 case MSR_IA32_VMX_EXIT_CTLS:
3081 case MSR_IA32_VMX_ENTRY_CTLS:
3083 * The "non-true" VMX capability MSRs are generated from the
3084 * "true" MSRs, so we do not support restoring them directly.
3086 * If userspace wants to emulate VMX_BASIC[55]=0, userspace
3087 * should restore the "true" MSRs with the must-be-1 bits
3088 * set according to the SDM Vol 3. A.2 "RESERVED CONTROLS AND
3089 * DEFAULT SETTINGS".
3092 case MSR_IA32_VMX_TRUE_PINBASED_CTLS:
3093 case MSR_IA32_VMX_TRUE_PROCBASED_CTLS:
3094 case MSR_IA32_VMX_TRUE_EXIT_CTLS:
3095 case MSR_IA32_VMX_TRUE_ENTRY_CTLS:
3096 case MSR_IA32_VMX_PROCBASED_CTLS2:
3097 return vmx_restore_control_msr(vmx, msr_index, data);
3098 case MSR_IA32_VMX_MISC:
3099 return vmx_restore_vmx_misc(vmx, data);
3100 case MSR_IA32_VMX_CR0_FIXED0:
3101 case MSR_IA32_VMX_CR4_FIXED0:
3102 return vmx_restore_fixed0_msr(vmx, msr_index, data);
3103 case MSR_IA32_VMX_CR0_FIXED1:
3104 case MSR_IA32_VMX_CR4_FIXED1:
3106 * These MSRs are generated based on the vCPU's CPUID, so we
3107 * do not support restoring them directly.
3110 case MSR_IA32_VMX_EPT_VPID_CAP:
3111 return vmx_restore_vmx_ept_vpid_cap(vmx, data);
3112 case MSR_IA32_VMX_VMCS_ENUM:
3113 vmx->nested.nested_vmx_vmcs_enum = data;
3117 * The rest of the VMX capability MSRs do not support restore.
3123 /* Returns 0 on success, non-0 otherwise. */
3124 static int vmx_get_vmx_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 *pdata)
3126 struct vcpu_vmx *vmx = to_vmx(vcpu);
3128 switch (msr_index) {
3129 case MSR_IA32_VMX_BASIC:
3130 *pdata = vmx->nested.nested_vmx_basic;
3132 case MSR_IA32_VMX_TRUE_PINBASED_CTLS:
3133 case MSR_IA32_VMX_PINBASED_CTLS:
3134 *pdata = vmx_control_msr(
3135 vmx->nested.nested_vmx_pinbased_ctls_low,
3136 vmx->nested.nested_vmx_pinbased_ctls_high);
3137 if (msr_index == MSR_IA32_VMX_PINBASED_CTLS)
3138 *pdata |= PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
3140 case MSR_IA32_VMX_TRUE_PROCBASED_CTLS:
3141 case MSR_IA32_VMX_PROCBASED_CTLS:
3142 *pdata = vmx_control_msr(
3143 vmx->nested.nested_vmx_procbased_ctls_low,
3144 vmx->nested.nested_vmx_procbased_ctls_high);
3145 if (msr_index == MSR_IA32_VMX_PROCBASED_CTLS)
3146 *pdata |= CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
3148 case MSR_IA32_VMX_TRUE_EXIT_CTLS:
3149 case MSR_IA32_VMX_EXIT_CTLS:
3150 *pdata = vmx_control_msr(
3151 vmx->nested.nested_vmx_exit_ctls_low,
3152 vmx->nested.nested_vmx_exit_ctls_high);
3153 if (msr_index == MSR_IA32_VMX_EXIT_CTLS)
3154 *pdata |= VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR;
3156 case MSR_IA32_VMX_TRUE_ENTRY_CTLS:
3157 case MSR_IA32_VMX_ENTRY_CTLS:
3158 *pdata = vmx_control_msr(
3159 vmx->nested.nested_vmx_entry_ctls_low,
3160 vmx->nested.nested_vmx_entry_ctls_high);
3161 if (msr_index == MSR_IA32_VMX_ENTRY_CTLS)
3162 *pdata |= VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR;
3164 case MSR_IA32_VMX_MISC:
3165 *pdata = vmx_control_msr(
3166 vmx->nested.nested_vmx_misc_low,
3167 vmx->nested.nested_vmx_misc_high);
3169 case MSR_IA32_VMX_CR0_FIXED0:
3170 *pdata = vmx->nested.nested_vmx_cr0_fixed0;
3172 case MSR_IA32_VMX_CR0_FIXED1:
3173 *pdata = vmx->nested.nested_vmx_cr0_fixed1;
3175 case MSR_IA32_VMX_CR4_FIXED0:
3176 *pdata = vmx->nested.nested_vmx_cr4_fixed0;
3178 case MSR_IA32_VMX_CR4_FIXED1:
3179 *pdata = vmx->nested.nested_vmx_cr4_fixed1;
3181 case MSR_IA32_VMX_VMCS_ENUM:
3182 *pdata = vmx->nested.nested_vmx_vmcs_enum;
3184 case MSR_IA32_VMX_PROCBASED_CTLS2:
3185 *pdata = vmx_control_msr(
3186 vmx->nested.nested_vmx_secondary_ctls_low,
3187 vmx->nested.nested_vmx_secondary_ctls_high);
3189 case MSR_IA32_VMX_EPT_VPID_CAP:
3190 *pdata = vmx->nested.nested_vmx_ept_caps |
3191 ((u64)vmx->nested.nested_vmx_vpid_caps << 32);
3193 case MSR_IA32_VMX_VMFUNC:
3194 *pdata = vmx->nested.nested_vmx_vmfunc_controls;
3203 static inline bool vmx_feature_control_msr_valid(struct kvm_vcpu *vcpu,
3206 uint64_t valid_bits = to_vmx(vcpu)->msr_ia32_feature_control_valid_bits;
3208 return !(val & ~valid_bits);
3212 * Reads an msr value (of 'msr_index') into 'pdata'.
3213 * Returns 0 on success, non-0 otherwise.
3214 * Assumes vcpu_load() was already called.
3216 static int vmx_get_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
3218 struct vcpu_vmx *vmx = to_vmx(vcpu);
3219 struct shared_msr_entry *msr;
3221 switch (msr_info->index) {
3222 #ifdef CONFIG_X86_64
3224 msr_info->data = vmcs_readl(GUEST_FS_BASE);
3227 msr_info->data = vmcs_readl(GUEST_GS_BASE);
3229 case MSR_KERNEL_GS_BASE:
3230 vmx_load_host_state(vmx);
3231 msr_info->data = vmx->msr_guest_kernel_gs_base;
3235 return kvm_get_msr_common(vcpu, msr_info);
3237 msr_info->data = guest_read_tsc(vcpu);
3239 case MSR_IA32_SYSENTER_CS:
3240 msr_info->data = vmcs_read32(GUEST_SYSENTER_CS);
3242 case MSR_IA32_SYSENTER_EIP:
3243 msr_info->data = vmcs_readl(GUEST_SYSENTER_EIP);
3245 case MSR_IA32_SYSENTER_ESP:
3246 msr_info->data = vmcs_readl(GUEST_SYSENTER_ESP);
3248 case MSR_IA32_BNDCFGS:
3249 if (!kvm_mpx_supported() ||
3250 (!msr_info->host_initiated &&
3251 !guest_cpuid_has(vcpu, X86_FEATURE_MPX)))
3253 msr_info->data = vmcs_read64(GUEST_BNDCFGS);
3255 case MSR_IA32_MCG_EXT_CTL:
3256 if (!msr_info->host_initiated &&
3257 !(vmx->msr_ia32_feature_control &
3258 FEATURE_CONTROL_LMCE))
3260 msr_info->data = vcpu->arch.mcg_ext_ctl;
3262 case MSR_IA32_FEATURE_CONTROL:
3263 msr_info->data = vmx->msr_ia32_feature_control;
3265 case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC:
3266 if (!nested_vmx_allowed(vcpu))
3268 return vmx_get_vmx_msr(vcpu, msr_info->index, &msr_info->data);
3270 if (!vmx_xsaves_supported())
3272 msr_info->data = vcpu->arch.ia32_xss;
3275 if (!msr_info->host_initiated &&
3276 !guest_cpuid_has(vcpu, X86_FEATURE_RDTSCP))
3278 /* Otherwise falls through */
3280 msr = find_msr_entry(vmx, msr_info->index);
3282 msr_info->data = msr->data;
3285 return kvm_get_msr_common(vcpu, msr_info);
3291 static void vmx_leave_nested(struct kvm_vcpu *vcpu);
3294 * Writes msr value into into the appropriate "register".
3295 * Returns 0 on success, non-0 otherwise.
3296 * Assumes vcpu_load() was already called.
3298 static int vmx_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
3300 struct vcpu_vmx *vmx = to_vmx(vcpu);
3301 struct shared_msr_entry *msr;
3303 u32 msr_index = msr_info->index;
3304 u64 data = msr_info->data;
3306 switch (msr_index) {
3308 ret = kvm_set_msr_common(vcpu, msr_info);
3310 #ifdef CONFIG_X86_64
3312 vmx_segment_cache_clear(vmx);
3313 vmcs_writel(GUEST_FS_BASE, data);
3316 vmx_segment_cache_clear(vmx);
3317 vmcs_writel(GUEST_GS_BASE, data);
3319 case MSR_KERNEL_GS_BASE:
3320 vmx_load_host_state(vmx);
3321 vmx->msr_guest_kernel_gs_base = data;
3324 case MSR_IA32_SYSENTER_CS:
3325 vmcs_write32(GUEST_SYSENTER_CS, data);
3327 case MSR_IA32_SYSENTER_EIP:
3328 vmcs_writel(GUEST_SYSENTER_EIP, data);
3330 case MSR_IA32_SYSENTER_ESP:
3331 vmcs_writel(GUEST_SYSENTER_ESP, data);
3333 case MSR_IA32_BNDCFGS:
3334 if (!kvm_mpx_supported() ||
3335 (!msr_info->host_initiated &&
3336 !guest_cpuid_has(vcpu, X86_FEATURE_MPX)))
3338 if (is_noncanonical_address(data & PAGE_MASK, vcpu) ||
3339 (data & MSR_IA32_BNDCFGS_RSVD))
3341 vmcs_write64(GUEST_BNDCFGS, data);
3344 kvm_write_tsc(vcpu, msr_info);
3346 case MSR_IA32_CR_PAT:
3347 if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) {
3348 if (!kvm_mtrr_valid(vcpu, MSR_IA32_CR_PAT, data))
3350 vmcs_write64(GUEST_IA32_PAT, data);
3351 vcpu->arch.pat = data;
3354 ret = kvm_set_msr_common(vcpu, msr_info);
3356 case MSR_IA32_TSC_ADJUST:
3357 ret = kvm_set_msr_common(vcpu, msr_info);
3359 case MSR_IA32_MCG_EXT_CTL:
3360 if ((!msr_info->host_initiated &&
3361 !(to_vmx(vcpu)->msr_ia32_feature_control &
3362 FEATURE_CONTROL_LMCE)) ||
3363 (data & ~MCG_EXT_CTL_LMCE_EN))
3365 vcpu->arch.mcg_ext_ctl = data;
3367 case MSR_IA32_FEATURE_CONTROL:
3368 if (!vmx_feature_control_msr_valid(vcpu, data) ||
3369 (to_vmx(vcpu)->msr_ia32_feature_control &
3370 FEATURE_CONTROL_LOCKED && !msr_info->host_initiated))
3372 vmx->msr_ia32_feature_control = data;
3373 if (msr_info->host_initiated && data == 0)
3374 vmx_leave_nested(vcpu);
3376 case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC:
3377 if (!msr_info->host_initiated)
3378 return 1; /* they are read-only */
3379 if (!nested_vmx_allowed(vcpu))
3381 return vmx_set_vmx_msr(vcpu, msr_index, data);
3383 if (!vmx_xsaves_supported())
3386 * The only supported bit as of Skylake is bit 8, but
3387 * it is not supported on KVM.
3391 vcpu->arch.ia32_xss = data;
3392 if (vcpu->arch.ia32_xss != host_xss)
3393 add_atomic_switch_msr(vmx, MSR_IA32_XSS,
3394 vcpu->arch.ia32_xss, host_xss);
3396 clear_atomic_switch_msr(vmx, MSR_IA32_XSS);
3399 if (!msr_info->host_initiated &&
3400 !guest_cpuid_has(vcpu, X86_FEATURE_RDTSCP))
3402 /* Check reserved bit, higher 32 bits should be zero */
3403 if ((data >> 32) != 0)
3405 /* Otherwise falls through */
3407 msr = find_msr_entry(vmx, msr_index);
3409 u64 old_msr_data = msr->data;
3411 if (msr - vmx->guest_msrs < vmx->save_nmsrs) {
3413 ret = kvm_set_shared_msr(msr->index, msr->data,
3417 msr->data = old_msr_data;
3421 ret = kvm_set_msr_common(vcpu, msr_info);
3427 static void vmx_cache_reg(struct kvm_vcpu *vcpu, enum kvm_reg reg)
3429 __set_bit(reg, (unsigned long *)&vcpu->arch.regs_avail);
3432 vcpu->arch.regs[VCPU_REGS_RSP] = vmcs_readl(GUEST_RSP);
3435 vcpu->arch.regs[VCPU_REGS_RIP] = vmcs_readl(GUEST_RIP);
3437 case VCPU_EXREG_PDPTR:
3439 ept_save_pdptrs(vcpu);
3446 static __init int cpu_has_kvm_support(void)
3448 return cpu_has_vmx();
3451 static __init int vmx_disabled_by_bios(void)
3455 rdmsrl(MSR_IA32_FEATURE_CONTROL, msr);
3456 if (msr & FEATURE_CONTROL_LOCKED) {
3457 /* launched w/ TXT and VMX disabled */
3458 if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX)
3461 /* launched w/o TXT and VMX only enabled w/ TXT */
3462 if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX)
3463 && (msr & FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX)
3464 && !tboot_enabled()) {
3465 printk(KERN_WARNING "kvm: disable TXT in the BIOS or "
3466 "activate TXT before enabling KVM\n");
3469 /* launched w/o TXT and VMX disabled */
3470 if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX)
3471 && !tboot_enabled())
3478 static void kvm_cpu_vmxon(u64 addr)
3480 cr4_set_bits(X86_CR4_VMXE);
3481 intel_pt_handle_vmx(1);
3483 asm volatile (ASM_VMX_VMXON_RAX
3484 : : "a"(&addr), "m"(addr)
3488 static int hardware_enable(void)
3490 int cpu = raw_smp_processor_id();
3491 u64 phys_addr = __pa(per_cpu(vmxarea, cpu));
3494 if (cr4_read_shadow() & X86_CR4_VMXE)
3497 INIT_LIST_HEAD(&per_cpu(loaded_vmcss_on_cpu, cpu));
3498 INIT_LIST_HEAD(&per_cpu(blocked_vcpu_on_cpu, cpu));
3499 spin_lock_init(&per_cpu(blocked_vcpu_on_cpu_lock, cpu));
3502 * Now we can enable the vmclear operation in kdump
3503 * since the loaded_vmcss_on_cpu list on this cpu
3504 * has been initialized.
3506 * Though the cpu is not in VMX operation now, there
3507 * is no problem to enable the vmclear operation
3508 * for the loaded_vmcss_on_cpu list is empty!
3510 crash_enable_local_vmclear(cpu);
3512 rdmsrl(MSR_IA32_FEATURE_CONTROL, old);
3514 test_bits = FEATURE_CONTROL_LOCKED;
3515 test_bits |= FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
3516 if (tboot_enabled())
3517 test_bits |= FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX;
3519 if ((old & test_bits) != test_bits) {
3520 /* enable and lock */
3521 wrmsrl(MSR_IA32_FEATURE_CONTROL, old | test_bits);
3523 kvm_cpu_vmxon(phys_addr);
3530 static void vmclear_local_loaded_vmcss(void)
3532 int cpu = raw_smp_processor_id();
3533 struct loaded_vmcs *v, *n;
3535 list_for_each_entry_safe(v, n, &per_cpu(loaded_vmcss_on_cpu, cpu),
3536 loaded_vmcss_on_cpu_link)
3537 __loaded_vmcs_clear(v);
3541 /* Just like cpu_vmxoff(), but with the __kvm_handle_fault_on_reboot()
3544 static void kvm_cpu_vmxoff(void)
3546 asm volatile (__ex(ASM_VMX_VMXOFF) : : : "cc");
3548 intel_pt_handle_vmx(0);
3549 cr4_clear_bits(X86_CR4_VMXE);
3552 static void hardware_disable(void)
3554 vmclear_local_loaded_vmcss();
3558 static __init int adjust_vmx_controls(u32 ctl_min, u32 ctl_opt,
3559 u32 msr, u32 *result)
3561 u32 vmx_msr_low, vmx_msr_high;
3562 u32 ctl = ctl_min | ctl_opt;
3564 rdmsr(msr, vmx_msr_low, vmx_msr_high);
3566 ctl &= vmx_msr_high; /* bit == 0 in high word ==> must be zero */
3567 ctl |= vmx_msr_low; /* bit == 1 in low word ==> must be one */
3569 /* Ensure minimum (required) set of control bits are supported. */
3577 static __init bool allow_1_setting(u32 msr, u32 ctl)
3579 u32 vmx_msr_low, vmx_msr_high;
3581 rdmsr(msr, vmx_msr_low, vmx_msr_high);
3582 return vmx_msr_high & ctl;
3585 static __init int setup_vmcs_config(struct vmcs_config *vmcs_conf)
3587 u32 vmx_msr_low, vmx_msr_high;
3588 u32 min, opt, min2, opt2;
3589 u32 _pin_based_exec_control = 0;
3590 u32 _cpu_based_exec_control = 0;
3591 u32 _cpu_based_2nd_exec_control = 0;
3592 u32 _vmexit_control = 0;
3593 u32 _vmentry_control = 0;
3595 min = CPU_BASED_HLT_EXITING |
3596 #ifdef CONFIG_X86_64
3597 CPU_BASED_CR8_LOAD_EXITING |
3598 CPU_BASED_CR8_STORE_EXITING |
3600 CPU_BASED_CR3_LOAD_EXITING |
3601 CPU_BASED_CR3_STORE_EXITING |
3602 CPU_BASED_UNCOND_IO_EXITING |
3603 CPU_BASED_MOV_DR_EXITING |
3604 CPU_BASED_USE_TSC_OFFSETING |
3605 CPU_BASED_INVLPG_EXITING |
3606 CPU_BASED_RDPMC_EXITING;
3608 if (!kvm_mwait_in_guest())
3609 min |= CPU_BASED_MWAIT_EXITING |
3610 CPU_BASED_MONITOR_EXITING;
3612 opt = CPU_BASED_TPR_SHADOW |
3613 CPU_BASED_USE_MSR_BITMAPS |
3614 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
3615 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PROCBASED_CTLS,
3616 &_cpu_based_exec_control) < 0)
3618 #ifdef CONFIG_X86_64
3619 if ((_cpu_based_exec_control & CPU_BASED_TPR_SHADOW))
3620 _cpu_based_exec_control &= ~CPU_BASED_CR8_LOAD_EXITING &
3621 ~CPU_BASED_CR8_STORE_EXITING;
3623 if (_cpu_based_exec_control & CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) {
3625 opt2 = SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
3626 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
3627 SECONDARY_EXEC_WBINVD_EXITING |
3628 SECONDARY_EXEC_ENABLE_VPID |
3629 SECONDARY_EXEC_ENABLE_EPT |
3630 SECONDARY_EXEC_UNRESTRICTED_GUEST |
3631 SECONDARY_EXEC_PAUSE_LOOP_EXITING |
3632 SECONDARY_EXEC_DESC |
3633 SECONDARY_EXEC_RDTSCP |
3634 SECONDARY_EXEC_ENABLE_INVPCID |
3635 SECONDARY_EXEC_APIC_REGISTER_VIRT |
3636 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY |
3637 SECONDARY_EXEC_SHADOW_VMCS |
3638 SECONDARY_EXEC_XSAVES |
3639 SECONDARY_EXEC_RDSEED_EXITING |
3640 SECONDARY_EXEC_RDRAND_EXITING |
3641 SECONDARY_EXEC_ENABLE_PML |
3642 SECONDARY_EXEC_TSC_SCALING |
3643 SECONDARY_EXEC_ENABLE_VMFUNC;
3644 if (adjust_vmx_controls(min2, opt2,
3645 MSR_IA32_VMX_PROCBASED_CTLS2,
3646 &_cpu_based_2nd_exec_control) < 0)
3649 #ifndef CONFIG_X86_64
3650 if (!(_cpu_based_2nd_exec_control &
3651 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES))
3652 _cpu_based_exec_control &= ~CPU_BASED_TPR_SHADOW;
3655 if (!(_cpu_based_exec_control & CPU_BASED_TPR_SHADOW))
3656 _cpu_based_2nd_exec_control &= ~(
3657 SECONDARY_EXEC_APIC_REGISTER_VIRT |
3658 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
3659 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
3661 rdmsr_safe(MSR_IA32_VMX_EPT_VPID_CAP,
3662 &vmx_capability.ept, &vmx_capability.vpid);
3664 if (_cpu_based_2nd_exec_control & SECONDARY_EXEC_ENABLE_EPT) {
3665 /* CR3 accesses and invlpg don't need to cause VM Exits when EPT
3667 _cpu_based_exec_control &= ~(CPU_BASED_CR3_LOAD_EXITING |
3668 CPU_BASED_CR3_STORE_EXITING |
3669 CPU_BASED_INVLPG_EXITING);
3670 } else if (vmx_capability.ept) {
3671 vmx_capability.ept = 0;
3672 pr_warn_once("EPT CAP should not exist if not support "
3673 "1-setting enable EPT VM-execution control\n");
3675 if (!(_cpu_based_2nd_exec_control & SECONDARY_EXEC_ENABLE_VPID) &&
3676 vmx_capability.vpid) {
3677 vmx_capability.vpid = 0;
3678 pr_warn_once("VPID CAP should not exist if not support "
3679 "1-setting enable VPID VM-execution control\n");
3682 min = VM_EXIT_SAVE_DEBUG_CONTROLS | VM_EXIT_ACK_INTR_ON_EXIT;
3683 #ifdef CONFIG_X86_64
3684 min |= VM_EXIT_HOST_ADDR_SPACE_SIZE;
3686 opt = VM_EXIT_SAVE_IA32_PAT | VM_EXIT_LOAD_IA32_PAT |
3687 VM_EXIT_CLEAR_BNDCFGS;
3688 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_EXIT_CTLS,
3689 &_vmexit_control) < 0)
3692 min = PIN_BASED_EXT_INTR_MASK | PIN_BASED_NMI_EXITING;
3693 opt = PIN_BASED_VIRTUAL_NMIS | PIN_BASED_POSTED_INTR |
3694 PIN_BASED_VMX_PREEMPTION_TIMER;
3695 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PINBASED_CTLS,
3696 &_pin_based_exec_control) < 0)
3699 if (cpu_has_broken_vmx_preemption_timer())
3700 _pin_based_exec_control &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
3701 if (!(_cpu_based_2nd_exec_control &
3702 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY))
3703 _pin_based_exec_control &= ~PIN_BASED_POSTED_INTR;
3705 min = VM_ENTRY_LOAD_DEBUG_CONTROLS;
3706 opt = VM_ENTRY_LOAD_IA32_PAT | VM_ENTRY_LOAD_BNDCFGS;
3707 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_ENTRY_CTLS,
3708 &_vmentry_control) < 0)
3711 rdmsr(MSR_IA32_VMX_BASIC, vmx_msr_low, vmx_msr_high);
3713 /* IA-32 SDM Vol 3B: VMCS size is never greater than 4kB. */
3714 if ((vmx_msr_high & 0x1fff) > PAGE_SIZE)
3717 #ifdef CONFIG_X86_64
3718 /* IA-32 SDM Vol 3B: 64-bit CPUs always have VMX_BASIC_MSR[48]==0. */
3719 if (vmx_msr_high & (1u<<16))
3723 /* Require Write-Back (WB) memory type for VMCS accesses. */
3724 if (((vmx_msr_high >> 18) & 15) != 6)
3727 vmcs_conf->size = vmx_msr_high & 0x1fff;
3728 vmcs_conf->order = get_order(vmcs_conf->size);
3729 vmcs_conf->basic_cap = vmx_msr_high & ~0x1fff;
3730 vmcs_conf->revision_id = vmx_msr_low;
3732 vmcs_conf->pin_based_exec_ctrl = _pin_based_exec_control;
3733 vmcs_conf->cpu_based_exec_ctrl = _cpu_based_exec_control;
3734 vmcs_conf->cpu_based_2nd_exec_ctrl = _cpu_based_2nd_exec_control;
3735 vmcs_conf->vmexit_ctrl = _vmexit_control;
3736 vmcs_conf->vmentry_ctrl = _vmentry_control;
3738 cpu_has_load_ia32_efer =
3739 allow_1_setting(MSR_IA32_VMX_ENTRY_CTLS,
3740 VM_ENTRY_LOAD_IA32_EFER)
3741 && allow_1_setting(MSR_IA32_VMX_EXIT_CTLS,
3742 VM_EXIT_LOAD_IA32_EFER);
3744 cpu_has_load_perf_global_ctrl =
3745 allow_1_setting(MSR_IA32_VMX_ENTRY_CTLS,
3746 VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL)
3747 && allow_1_setting(MSR_IA32_VMX_EXIT_CTLS,
3748 VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL);
3751 * Some cpus support VM_ENTRY_(LOAD|SAVE)_IA32_PERF_GLOBAL_CTRL
3752 * but due to errata below it can't be used. Workaround is to use
3753 * msr load mechanism to switch IA32_PERF_GLOBAL_CTRL.
3755 * VM Exit May Incorrectly Clear IA32_PERF_GLOBAL_CTRL [34:32]
3760 * BC86,AAY89,BD102 (model 44)
3764 if (cpu_has_load_perf_global_ctrl && boot_cpu_data.x86 == 0x6) {
3765 switch (boot_cpu_data.x86_model) {
3771 cpu_has_load_perf_global_ctrl = false;
3772 printk_once(KERN_WARNING"kvm: VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL "
3773 "does not work properly. Using workaround\n");
3780 if (boot_cpu_has(X86_FEATURE_XSAVES))
3781 rdmsrl(MSR_IA32_XSS, host_xss);
3786 static struct vmcs *alloc_vmcs_cpu(int cpu)
3788 int node = cpu_to_node(cpu);
3792 pages = __alloc_pages_node(node, GFP_KERNEL, vmcs_config.order);
3795 vmcs = page_address(pages);
3796 memset(vmcs, 0, vmcs_config.size);
3797 vmcs->revision_id = vmcs_config.revision_id; /* vmcs revision id */
3801 static struct vmcs *alloc_vmcs(void)
3803 return alloc_vmcs_cpu(raw_smp_processor_id());
3806 static void free_vmcs(struct vmcs *vmcs)
3808 free_pages((unsigned long)vmcs, vmcs_config.order);
3812 * Free a VMCS, but before that VMCLEAR it on the CPU where it was last loaded
3814 static void free_loaded_vmcs(struct loaded_vmcs *loaded_vmcs)
3816 if (!loaded_vmcs->vmcs)
3818 loaded_vmcs_clear(loaded_vmcs);
3819 free_vmcs(loaded_vmcs->vmcs);
3820 loaded_vmcs->vmcs = NULL;
3821 WARN_ON(loaded_vmcs->shadow_vmcs != NULL);
3824 static void vmx_nested_free_vmcs02(struct vcpu_vmx *vmx)
3826 struct loaded_vmcs *loaded_vmcs = &vmx->nested.vmcs02;
3829 * Just leak the VMCS02 if the WARN triggers. Better than
3832 if (WARN_ON(vmx->loaded_vmcs == loaded_vmcs))
3834 free_loaded_vmcs(loaded_vmcs);
3837 static void free_kvm_area(void)
3841 for_each_possible_cpu(cpu) {
3842 free_vmcs(per_cpu(vmxarea, cpu));
3843 per_cpu(vmxarea, cpu) = NULL;
3847 enum vmcs_field_width {
3848 VMCS_FIELD_WIDTH_U16 = 0,
3849 VMCS_FIELD_WIDTH_U64 = 1,
3850 VMCS_FIELD_WIDTH_U32 = 2,
3851 VMCS_FIELD_WIDTH_NATURAL_WIDTH = 3
3854 static inline int vmcs_field_width(unsigned long field)
3856 if (0x1 & field) /* the *_HIGH fields are all 32 bit */
3857 return VMCS_FIELD_WIDTH_U32;
3858 return (field >> 13) & 0x3 ;
3861 static inline int vmcs_field_readonly(unsigned long field)
3863 return (((field >> 10) & 0x3) == 1);
3866 static void init_vmcs_shadow_fields(void)
3870 for (i = j = 0; i < max_shadow_read_only_fields; i++) {
3871 u16 field = shadow_read_only_fields[i];
3872 if (vmcs_field_width(field) == VMCS_FIELD_WIDTH_U64 &&
3873 (i + 1 == max_shadow_read_only_fields ||
3874 shadow_read_only_fields[i + 1] != field + 1))
3875 pr_err("Missing field from shadow_read_only_field %x\n",
3878 clear_bit(field, vmx_vmread_bitmap);
3879 #ifdef CONFIG_X86_64
3884 shadow_read_only_fields[j] = field;
3887 max_shadow_read_only_fields = j;
3889 for (i = j = 0; i < max_shadow_read_write_fields; i++) {
3890 u16 field = shadow_read_write_fields[i];
3891 if (vmcs_field_width(field) == VMCS_FIELD_WIDTH_U64 &&
3892 (i + 1 == max_shadow_read_write_fields ||
3893 shadow_read_write_fields[i + 1] != field + 1))
3894 pr_err("Missing field from shadow_read_write_field %x\n",
3898 * PML and the preemption timer can be emulated, but the
3899 * processor cannot vmwrite to fields that don't exist
3903 case GUEST_PML_INDEX:
3904 if (!cpu_has_vmx_pml())
3907 case VMX_PREEMPTION_TIMER_VALUE:
3908 if (!cpu_has_vmx_preemption_timer())
3911 case GUEST_INTR_STATUS:
3912 if (!cpu_has_vmx_apicv())
3919 clear_bit(field, vmx_vmwrite_bitmap);
3920 clear_bit(field, vmx_vmread_bitmap);
3921 #ifdef CONFIG_X86_64
3926 shadow_read_write_fields[j] = field;
3929 max_shadow_read_write_fields = j;
3932 static __init int alloc_kvm_area(void)
3936 for_each_possible_cpu(cpu) {
3939 vmcs = alloc_vmcs_cpu(cpu);
3945 per_cpu(vmxarea, cpu) = vmcs;
3950 static void fix_pmode_seg(struct kvm_vcpu *vcpu, int seg,
3951 struct kvm_segment *save)
3953 if (!emulate_invalid_guest_state) {
3955 * CS and SS RPL should be equal during guest entry according
3956 * to VMX spec, but in reality it is not always so. Since vcpu
3957 * is in the middle of the transition from real mode to
3958 * protected mode it is safe to assume that RPL 0 is a good
3961 if (seg == VCPU_SREG_CS || seg == VCPU_SREG_SS)
3962 save->selector &= ~SEGMENT_RPL_MASK;
3963 save->dpl = save->selector & SEGMENT_RPL_MASK;
3966 vmx_set_segment(vcpu, save, seg);
3969 static void enter_pmode(struct kvm_vcpu *vcpu)
3971 unsigned long flags;
3972 struct vcpu_vmx *vmx = to_vmx(vcpu);
3975 * Update real mode segment cache. It may be not up-to-date if sement
3976 * register was written while vcpu was in a guest mode.
3978 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_ES], VCPU_SREG_ES);
3979 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_DS], VCPU_SREG_DS);
3980 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_FS], VCPU_SREG_FS);
3981 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_GS], VCPU_SREG_GS);
3982 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_SS], VCPU_SREG_SS);
3983 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_CS], VCPU_SREG_CS);
3985 vmx->rmode.vm86_active = 0;
3987 vmx_segment_cache_clear(vmx);
3989 vmx_set_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_TR], VCPU_SREG_TR);
3991 flags = vmcs_readl(GUEST_RFLAGS);
3992 flags &= RMODE_GUEST_OWNED_EFLAGS_BITS;
3993 flags |= vmx->rmode.save_rflags & ~RMODE_GUEST_OWNED_EFLAGS_BITS;
3994 vmcs_writel(GUEST_RFLAGS, flags);
3996 vmcs_writel(GUEST_CR4, (vmcs_readl(GUEST_CR4) & ~X86_CR4_VME) |
3997 (vmcs_readl(CR4_READ_SHADOW) & X86_CR4_VME));
3999 update_exception_bitmap(vcpu);
4001 fix_pmode_seg(vcpu, VCPU_SREG_CS, &vmx->rmode.segs[VCPU_SREG_CS]);
4002 fix_pmode_seg(vcpu, VCPU_SREG_SS, &vmx->rmode.segs[VCPU_SREG_SS]);
4003 fix_pmode_seg(vcpu, VCPU_SREG_ES, &vmx->rmode.segs[VCPU_SREG_ES]);
4004 fix_pmode_seg(vcpu, VCPU_SREG_DS, &vmx->rmode.segs[VCPU_SREG_DS]);
4005 fix_pmode_seg(vcpu, VCPU_SREG_FS, &vmx->rmode.segs[VCPU_SREG_FS]);
4006 fix_pmode_seg(vcpu, VCPU_SREG_GS, &vmx->rmode.segs[VCPU_SREG_GS]);
4009 static void fix_rmode_seg(int seg, struct kvm_segment *save)
4011 const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
4012 struct kvm_segment var = *save;
4015 if (seg == VCPU_SREG_CS)
4018 if (!emulate_invalid_guest_state) {
4019 var.selector = var.base >> 4;
4020 var.base = var.base & 0xffff0;
4030 if (save->base & 0xf)
4031 printk_once(KERN_WARNING "kvm: segment base is not "
4032 "paragraph aligned when entering "
4033 "protected mode (seg=%d)", seg);
4036 vmcs_write16(sf->selector, var.selector);
4037 vmcs_writel(sf->base, var.base);
4038 vmcs_write32(sf->limit, var.limit);
4039 vmcs_write32(sf->ar_bytes, vmx_segment_access_rights(&var));
4042 static void enter_rmode(struct kvm_vcpu *vcpu)
4044 unsigned long flags;
4045 struct vcpu_vmx *vmx = to_vmx(vcpu);
4047 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_TR], VCPU_SREG_TR);
4048 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_ES], VCPU_SREG_ES);
4049 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_DS], VCPU_SREG_DS);
4050 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_FS], VCPU_SREG_FS);
4051 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_GS], VCPU_SREG_GS);
4052 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_SS], VCPU_SREG_SS);
4053 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_CS], VCPU_SREG_CS);
4055 vmx->rmode.vm86_active = 1;
4058 * Very old userspace does not call KVM_SET_TSS_ADDR before entering
4059 * vcpu. Warn the user that an update is overdue.
4061 if (!vcpu->kvm->arch.tss_addr)
4062 printk_once(KERN_WARNING "kvm: KVM_SET_TSS_ADDR need to be "
4063 "called before entering vcpu\n");
4065 vmx_segment_cache_clear(vmx);
4067 vmcs_writel(GUEST_TR_BASE, vcpu->kvm->arch.tss_addr);
4068 vmcs_write32(GUEST_TR_LIMIT, RMODE_TSS_SIZE - 1);
4069 vmcs_write32(GUEST_TR_AR_BYTES, 0x008b);
4071 flags = vmcs_readl(GUEST_RFLAGS);
4072 vmx->rmode.save_rflags = flags;
4074 flags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM;
4076 vmcs_writel(GUEST_RFLAGS, flags);
4077 vmcs_writel(GUEST_CR4, vmcs_readl(GUEST_CR4) | X86_CR4_VME);
4078 update_exception_bitmap(vcpu);
4080 fix_rmode_seg(VCPU_SREG_SS, &vmx->rmode.segs[VCPU_SREG_SS]);
4081 fix_rmode_seg(VCPU_SREG_CS, &vmx->rmode.segs[VCPU_SREG_CS]);
4082 fix_rmode_seg(VCPU_SREG_ES, &vmx->rmode.segs[VCPU_SREG_ES]);
4083 fix_rmode_seg(VCPU_SREG_DS, &vmx->rmode.segs[VCPU_SREG_DS]);
4084 fix_rmode_seg(VCPU_SREG_GS, &vmx->rmode.segs[VCPU_SREG_GS]);
4085 fix_rmode_seg(VCPU_SREG_FS, &vmx->rmode.segs[VCPU_SREG_FS]);
4087 kvm_mmu_reset_context(vcpu);
4090 static void vmx_set_efer(struct kvm_vcpu *vcpu, u64 efer)
4092 struct vcpu_vmx *vmx = to_vmx(vcpu);
4093 struct shared_msr_entry *msr = find_msr_entry(vmx, MSR_EFER);
4099 * Force kernel_gs_base reloading before EFER changes, as control
4100 * of this msr depends on is_long_mode().
4102 vmx_load_host_state(to_vmx(vcpu));
4103 vcpu->arch.efer = efer;
4104 if (efer & EFER_LMA) {
4105 vm_entry_controls_setbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
4108 vm_entry_controls_clearbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
4110 msr->data = efer & ~EFER_LME;
4115 #ifdef CONFIG_X86_64
4117 static void enter_lmode(struct kvm_vcpu *vcpu)
4121 vmx_segment_cache_clear(to_vmx(vcpu));
4123 guest_tr_ar = vmcs_read32(GUEST_TR_AR_BYTES);
4124 if ((guest_tr_ar & VMX_AR_TYPE_MASK) != VMX_AR_TYPE_BUSY_64_TSS) {
4125 pr_debug_ratelimited("%s: tss fixup for long mode. \n",
4127 vmcs_write32(GUEST_TR_AR_BYTES,
4128 (guest_tr_ar & ~VMX_AR_TYPE_MASK)
4129 | VMX_AR_TYPE_BUSY_64_TSS);
4131 vmx_set_efer(vcpu, vcpu->arch.efer | EFER_LMA);
4134 static void exit_lmode(struct kvm_vcpu *vcpu)
4136 vm_entry_controls_clearbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
4137 vmx_set_efer(vcpu, vcpu->arch.efer & ~EFER_LMA);
4142 static inline void __vmx_flush_tlb(struct kvm_vcpu *vcpu, int vpid,
4143 bool invalidate_gpa)
4145 if (enable_ept && (invalidate_gpa || !enable_vpid)) {
4146 if (!VALID_PAGE(vcpu->arch.mmu.root_hpa))
4148 ept_sync_context(construct_eptp(vcpu, vcpu->arch.mmu.root_hpa));
4150 vpid_sync_context(vpid);
4154 static void vmx_flush_tlb(struct kvm_vcpu *vcpu, bool invalidate_gpa)
4156 __vmx_flush_tlb(vcpu, to_vmx(vcpu)->vpid, invalidate_gpa);
4159 static void vmx_flush_tlb_ept_only(struct kvm_vcpu *vcpu)
4162 vmx_flush_tlb(vcpu, true);
4165 static void vmx_decache_cr0_guest_bits(struct kvm_vcpu *vcpu)
4167 ulong cr0_guest_owned_bits = vcpu->arch.cr0_guest_owned_bits;
4169 vcpu->arch.cr0 &= ~cr0_guest_owned_bits;
4170 vcpu->arch.cr0 |= vmcs_readl(GUEST_CR0) & cr0_guest_owned_bits;
4173 static void vmx_decache_cr3(struct kvm_vcpu *vcpu)
4175 if (enable_ept && is_paging(vcpu))
4176 vcpu->arch.cr3 = vmcs_readl(GUEST_CR3);
4177 __set_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail);
4180 static void vmx_decache_cr4_guest_bits(struct kvm_vcpu *vcpu)
4182 ulong cr4_guest_owned_bits = vcpu->arch.cr4_guest_owned_bits;
4184 vcpu->arch.cr4 &= ~cr4_guest_owned_bits;
4185 vcpu->arch.cr4 |= vmcs_readl(GUEST_CR4) & cr4_guest_owned_bits;
4188 static void ept_load_pdptrs(struct kvm_vcpu *vcpu)
4190 struct kvm_mmu *mmu = vcpu->arch.walk_mmu;
4192 if (!test_bit(VCPU_EXREG_PDPTR,
4193 (unsigned long *)&vcpu->arch.regs_dirty))
4196 if (is_paging(vcpu) && is_pae(vcpu) && !is_long_mode(vcpu)) {
4197 vmcs_write64(GUEST_PDPTR0, mmu->pdptrs[0]);
4198 vmcs_write64(GUEST_PDPTR1, mmu->pdptrs[1]);
4199 vmcs_write64(GUEST_PDPTR2, mmu->pdptrs[2]);
4200 vmcs_write64(GUEST_PDPTR3, mmu->pdptrs[3]);
4204 static void ept_save_pdptrs(struct kvm_vcpu *vcpu)
4206 struct kvm_mmu *mmu = vcpu->arch.walk_mmu;
4208 if (is_paging(vcpu) && is_pae(vcpu) && !is_long_mode(vcpu)) {
4209 mmu->pdptrs[0] = vmcs_read64(GUEST_PDPTR0);
4210 mmu->pdptrs[1] = vmcs_read64(GUEST_PDPTR1);
4211 mmu->pdptrs[2] = vmcs_read64(GUEST_PDPTR2);
4212 mmu->pdptrs[3] = vmcs_read64(GUEST_PDPTR3);
4215 __set_bit(VCPU_EXREG_PDPTR,
4216 (unsigned long *)&vcpu->arch.regs_avail);
4217 __set_bit(VCPU_EXREG_PDPTR,
4218 (unsigned long *)&vcpu->arch.regs_dirty);
4221 static bool nested_guest_cr0_valid(struct kvm_vcpu *vcpu, unsigned long val)
4223 u64 fixed0 = to_vmx(vcpu)->nested.nested_vmx_cr0_fixed0;
4224 u64 fixed1 = to_vmx(vcpu)->nested.nested_vmx_cr0_fixed1;
4225 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
4227 if (to_vmx(vcpu)->nested.nested_vmx_secondary_ctls_high &
4228 SECONDARY_EXEC_UNRESTRICTED_GUEST &&
4229 nested_cpu_has2(vmcs12, SECONDARY_EXEC_UNRESTRICTED_GUEST))
4230 fixed0 &= ~(X86_CR0_PE | X86_CR0_PG);
4232 return fixed_bits_valid(val, fixed0, fixed1);
4235 static bool nested_host_cr0_valid(struct kvm_vcpu *vcpu, unsigned long val)
4237 u64 fixed0 = to_vmx(vcpu)->nested.nested_vmx_cr0_fixed0;
4238 u64 fixed1 = to_vmx(vcpu)->nested.nested_vmx_cr0_fixed1;
4240 return fixed_bits_valid(val, fixed0, fixed1);
4243 static bool nested_cr4_valid(struct kvm_vcpu *vcpu, unsigned long val)
4245 u64 fixed0 = to_vmx(vcpu)->nested.nested_vmx_cr4_fixed0;
4246 u64 fixed1 = to_vmx(vcpu)->nested.nested_vmx_cr4_fixed1;
4248 return fixed_bits_valid(val, fixed0, fixed1);
4251 /* No difference in the restrictions on guest and host CR4 in VMX operation. */
4252 #define nested_guest_cr4_valid nested_cr4_valid
4253 #define nested_host_cr4_valid nested_cr4_valid
4255 static int vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4);
4257 static void ept_update_paging_mode_cr0(unsigned long *hw_cr0,
4259 struct kvm_vcpu *vcpu)
4261 if (!test_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail))
4262 vmx_decache_cr3(vcpu);
4263 if (!(cr0 & X86_CR0_PG)) {
4264 /* From paging/starting to nonpaging */
4265 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL,
4266 vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) |
4267 (CPU_BASED_CR3_LOAD_EXITING |
4268 CPU_BASED_CR3_STORE_EXITING));
4269 vcpu->arch.cr0 = cr0;
4270 vmx_set_cr4(vcpu, kvm_read_cr4(vcpu));
4271 } else if (!is_paging(vcpu)) {
4272 /* From nonpaging to paging */
4273 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL,
4274 vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) &
4275 ~(CPU_BASED_CR3_LOAD_EXITING |
4276 CPU_BASED_CR3_STORE_EXITING));
4277 vcpu->arch.cr0 = cr0;
4278 vmx_set_cr4(vcpu, kvm_read_cr4(vcpu));
4281 if (!(cr0 & X86_CR0_WP))
4282 *hw_cr0 &= ~X86_CR0_WP;
4285 static void vmx_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
4287 struct vcpu_vmx *vmx = to_vmx(vcpu);
4288 unsigned long hw_cr0;
4290 hw_cr0 = (cr0 & ~KVM_GUEST_CR0_MASK);
4291 if (enable_unrestricted_guest)
4292 hw_cr0 |= KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST;
4294 hw_cr0 |= KVM_VM_CR0_ALWAYS_ON;
4296 if (vmx->rmode.vm86_active && (cr0 & X86_CR0_PE))
4299 if (!vmx->rmode.vm86_active && !(cr0 & X86_CR0_PE))
4303 #ifdef CONFIG_X86_64
4304 if (vcpu->arch.efer & EFER_LME) {
4305 if (!is_paging(vcpu) && (cr0 & X86_CR0_PG))
4307 if (is_paging(vcpu) && !(cr0 & X86_CR0_PG))
4313 ept_update_paging_mode_cr0(&hw_cr0, cr0, vcpu);
4315 vmcs_writel(CR0_READ_SHADOW, cr0);
4316 vmcs_writel(GUEST_CR0, hw_cr0);
4317 vcpu->arch.cr0 = cr0;
4319 /* depends on vcpu->arch.cr0 to be set to a new value */
4320 vmx->emulation_required = emulation_required(vcpu);
4323 static int get_ept_level(struct kvm_vcpu *vcpu)
4325 if (cpu_has_vmx_ept_5levels() && (cpuid_maxphyaddr(vcpu) > 48))
4330 static u64 construct_eptp(struct kvm_vcpu *vcpu, unsigned long root_hpa)
4332 u64 eptp = VMX_EPTP_MT_WB;
4334 eptp |= (get_ept_level(vcpu) == 5) ? VMX_EPTP_PWL_5 : VMX_EPTP_PWL_4;
4336 if (enable_ept_ad_bits &&
4337 (!is_guest_mode(vcpu) || nested_ept_ad_enabled(vcpu)))
4338 eptp |= VMX_EPTP_AD_ENABLE_BIT;
4339 eptp |= (root_hpa & PAGE_MASK);
4344 static void vmx_set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3)
4346 unsigned long guest_cr3;
4351 eptp = construct_eptp(vcpu, cr3);
4352 vmcs_write64(EPT_POINTER, eptp);
4353 if (is_paging(vcpu) || is_guest_mode(vcpu))
4354 guest_cr3 = kvm_read_cr3(vcpu);
4356 guest_cr3 = vcpu->kvm->arch.ept_identity_map_addr;
4357 ept_load_pdptrs(vcpu);
4360 vmx_flush_tlb(vcpu, true);
4361 vmcs_writel(GUEST_CR3, guest_cr3);
4364 static int vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
4367 * Pass through host's Machine Check Enable value to hw_cr4, which
4368 * is in force while we are in guest mode. Do not let guests control
4369 * this bit, even if host CR4.MCE == 0.
4371 unsigned long hw_cr4 =
4372 (cr4_read_shadow() & X86_CR4_MCE) |
4373 (cr4 & ~X86_CR4_MCE) |
4374 (to_vmx(vcpu)->rmode.vm86_active ?
4375 KVM_RMODE_VM_CR4_ALWAYS_ON : KVM_PMODE_VM_CR4_ALWAYS_ON);
4377 if ((cr4 & X86_CR4_UMIP) && !boot_cpu_has(X86_FEATURE_UMIP)) {
4378 vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL,
4379 SECONDARY_EXEC_DESC);
4380 hw_cr4 &= ~X86_CR4_UMIP;
4382 vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL,
4383 SECONDARY_EXEC_DESC);
4385 if (cr4 & X86_CR4_VMXE) {
4387 * To use VMXON (and later other VMX instructions), a guest
4388 * must first be able to turn on cr4.VMXE (see handle_vmon()).
4389 * So basically the check on whether to allow nested VMX
4392 if (!nested_vmx_allowed(vcpu))
4396 if (to_vmx(vcpu)->nested.vmxon && !nested_cr4_valid(vcpu, cr4))
4399 vcpu->arch.cr4 = cr4;
4401 if (!is_paging(vcpu)) {
4402 hw_cr4 &= ~X86_CR4_PAE;
4403 hw_cr4 |= X86_CR4_PSE;
4404 } else if (!(cr4 & X86_CR4_PAE)) {
4405 hw_cr4 &= ~X86_CR4_PAE;
4409 if (!enable_unrestricted_guest && !is_paging(vcpu))
4411 * SMEP/SMAP/PKU is disabled if CPU is in non-paging mode in
4412 * hardware. To emulate this behavior, SMEP/SMAP/PKU needs
4413 * to be manually disabled when guest switches to non-paging
4416 * If !enable_unrestricted_guest, the CPU is always running
4417 * with CR0.PG=1 and CR4 needs to be modified.
4418 * If enable_unrestricted_guest, the CPU automatically
4419 * disables SMEP/SMAP/PKU when the guest sets CR0.PG=0.
4421 hw_cr4 &= ~(X86_CR4_SMEP | X86_CR4_SMAP | X86_CR4_PKE);
4423 vmcs_writel(CR4_READ_SHADOW, cr4);
4424 vmcs_writel(GUEST_CR4, hw_cr4);
4428 static void vmx_get_segment(struct kvm_vcpu *vcpu,
4429 struct kvm_segment *var, int seg)
4431 struct vcpu_vmx *vmx = to_vmx(vcpu);
4434 if (vmx->rmode.vm86_active && seg != VCPU_SREG_LDTR) {
4435 *var = vmx->rmode.segs[seg];
4436 if (seg == VCPU_SREG_TR
4437 || var->selector == vmx_read_guest_seg_selector(vmx, seg))
4439 var->base = vmx_read_guest_seg_base(vmx, seg);
4440 var->selector = vmx_read_guest_seg_selector(vmx, seg);
4443 var->base = vmx_read_guest_seg_base(vmx, seg);
4444 var->limit = vmx_read_guest_seg_limit(vmx, seg);
4445 var->selector = vmx_read_guest_seg_selector(vmx, seg);
4446 ar = vmx_read_guest_seg_ar(vmx, seg);
4447 var->unusable = (ar >> 16) & 1;
4448 var->type = ar & 15;
4449 var->s = (ar >> 4) & 1;
4450 var->dpl = (ar >> 5) & 3;
4452 * Some userspaces do not preserve unusable property. Since usable
4453 * segment has to be present according to VMX spec we can use present
4454 * property to amend userspace bug by making unusable segment always
4455 * nonpresent. vmx_segment_access_rights() already marks nonpresent
4456 * segment as unusable.
4458 var->present = !var->unusable;
4459 var->avl = (ar >> 12) & 1;
4460 var->l = (ar >> 13) & 1;
4461 var->db = (ar >> 14) & 1;
4462 var->g = (ar >> 15) & 1;
4465 static u64 vmx_get_segment_base(struct kvm_vcpu *vcpu, int seg)
4467 struct kvm_segment s;
4469 if (to_vmx(vcpu)->rmode.vm86_active) {
4470 vmx_get_segment(vcpu, &s, seg);
4473 return vmx_read_guest_seg_base(to_vmx(vcpu), seg);
4476 static int vmx_get_cpl(struct kvm_vcpu *vcpu)
4478 struct vcpu_vmx *vmx = to_vmx(vcpu);
4480 if (unlikely(vmx->rmode.vm86_active))
4483 int ar = vmx_read_guest_seg_ar(vmx, VCPU_SREG_SS);
4484 return VMX_AR_DPL(ar);
4488 static u32 vmx_segment_access_rights(struct kvm_segment *var)
4492 if (var->unusable || !var->present)
4495 ar = var->type & 15;
4496 ar |= (var->s & 1) << 4;
4497 ar |= (var->dpl & 3) << 5;
4498 ar |= (var->present & 1) << 7;
4499 ar |= (var->avl & 1) << 12;
4500 ar |= (var->l & 1) << 13;
4501 ar |= (var->db & 1) << 14;
4502 ar |= (var->g & 1) << 15;
4508 static void vmx_set_segment(struct kvm_vcpu *vcpu,
4509 struct kvm_segment *var, int seg)
4511 struct vcpu_vmx *vmx = to_vmx(vcpu);
4512 const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
4514 vmx_segment_cache_clear(vmx);
4516 if (vmx->rmode.vm86_active && seg != VCPU_SREG_LDTR) {
4517 vmx->rmode.segs[seg] = *var;
4518 if (seg == VCPU_SREG_TR)
4519 vmcs_write16(sf->selector, var->selector);
4521 fix_rmode_seg(seg, &vmx->rmode.segs[seg]);
4525 vmcs_writel(sf->base, var->base);
4526 vmcs_write32(sf->limit, var->limit);
4527 vmcs_write16(sf->selector, var->selector);
4530 * Fix the "Accessed" bit in AR field of segment registers for older
4532 * IA32 arch specifies that at the time of processor reset the
4533 * "Accessed" bit in the AR field of segment registers is 1. And qemu
4534 * is setting it to 0 in the userland code. This causes invalid guest
4535 * state vmexit when "unrestricted guest" mode is turned on.
4536 * Fix for this setup issue in cpu_reset is being pushed in the qemu
4537 * tree. Newer qemu binaries with that qemu fix would not need this
4540 if (enable_unrestricted_guest && (seg != VCPU_SREG_LDTR))
4541 var->type |= 0x1; /* Accessed */
4543 vmcs_write32(sf->ar_bytes, vmx_segment_access_rights(var));
4546 vmx->emulation_required = emulation_required(vcpu);
4549 static void vmx_get_cs_db_l_bits(struct kvm_vcpu *vcpu, int *db, int *l)
4551 u32 ar = vmx_read_guest_seg_ar(to_vmx(vcpu), VCPU_SREG_CS);
4553 *db = (ar >> 14) & 1;
4554 *l = (ar >> 13) & 1;
4557 static void vmx_get_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
4559 dt->size = vmcs_read32(GUEST_IDTR_LIMIT);
4560 dt->address = vmcs_readl(GUEST_IDTR_BASE);
4563 static void vmx_set_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
4565 vmcs_write32(GUEST_IDTR_LIMIT, dt->size);
4566 vmcs_writel(GUEST_IDTR_BASE, dt->address);
4569 static void vmx_get_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
4571 dt->size = vmcs_read32(GUEST_GDTR_LIMIT);
4572 dt->address = vmcs_readl(GUEST_GDTR_BASE);
4575 static void vmx_set_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
4577 vmcs_write32(GUEST_GDTR_LIMIT, dt->size);
4578 vmcs_writel(GUEST_GDTR_BASE, dt->address);
4581 static bool rmode_segment_valid(struct kvm_vcpu *vcpu, int seg)
4583 struct kvm_segment var;
4586 vmx_get_segment(vcpu, &var, seg);
4588 if (seg == VCPU_SREG_CS)
4590 ar = vmx_segment_access_rights(&var);
4592 if (var.base != (var.selector << 4))
4594 if (var.limit != 0xffff)
4602 static bool code_segment_valid(struct kvm_vcpu *vcpu)
4604 struct kvm_segment cs;
4605 unsigned int cs_rpl;
4607 vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
4608 cs_rpl = cs.selector & SEGMENT_RPL_MASK;
4612 if (~cs.type & (VMX_AR_TYPE_CODE_MASK|VMX_AR_TYPE_ACCESSES_MASK))
4616 if (cs.type & VMX_AR_TYPE_WRITEABLE_MASK) {
4617 if (cs.dpl > cs_rpl)
4620 if (cs.dpl != cs_rpl)
4626 /* TODO: Add Reserved field check, this'll require a new member in the kvm_segment_field structure */
4630 static bool stack_segment_valid(struct kvm_vcpu *vcpu)
4632 struct kvm_segment ss;
4633 unsigned int ss_rpl;
4635 vmx_get_segment(vcpu, &ss, VCPU_SREG_SS);
4636 ss_rpl = ss.selector & SEGMENT_RPL_MASK;
4640 if (ss.type != 3 && ss.type != 7)
4644 if (ss.dpl != ss_rpl) /* DPL != RPL */
4652 static bool data_segment_valid(struct kvm_vcpu *vcpu, int seg)
4654 struct kvm_segment var;
4657 vmx_get_segment(vcpu, &var, seg);
4658 rpl = var.selector & SEGMENT_RPL_MASK;
4666 if (~var.type & (VMX_AR_TYPE_CODE_MASK|VMX_AR_TYPE_WRITEABLE_MASK)) {
4667 if (var.dpl < rpl) /* DPL < RPL */
4671 /* TODO: Add other members to kvm_segment_field to allow checking for other access
4677 static bool tr_valid(struct kvm_vcpu *vcpu)
4679 struct kvm_segment tr;
4681 vmx_get_segment(vcpu, &tr, VCPU_SREG_TR);
4685 if (tr.selector & SEGMENT_TI_MASK) /* TI = 1 */
4687 if (tr.type != 3 && tr.type != 11) /* TODO: Check if guest is in IA32e mode */
4695 static bool ldtr_valid(struct kvm_vcpu *vcpu)
4697 struct kvm_segment ldtr;
4699 vmx_get_segment(vcpu, &ldtr, VCPU_SREG_LDTR);
4703 if (ldtr.selector & SEGMENT_TI_MASK) /* TI = 1 */
4713 static bool cs_ss_rpl_check(struct kvm_vcpu *vcpu)
4715 struct kvm_segment cs, ss;
4717 vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
4718 vmx_get_segment(vcpu, &ss, VCPU_SREG_SS);
4720 return ((cs.selector & SEGMENT_RPL_MASK) ==
4721 (ss.selector & SEGMENT_RPL_MASK));
4725 * Check if guest state is valid. Returns true if valid, false if
4727 * We assume that registers are always usable
4729 static bool guest_state_valid(struct kvm_vcpu *vcpu)
4731 if (enable_unrestricted_guest)
4734 /* real mode guest state checks */
4735 if (!is_protmode(vcpu) || (vmx_get_rflags(vcpu) & X86_EFLAGS_VM)) {
4736 if (!rmode_segment_valid(vcpu, VCPU_SREG_CS))
4738 if (!rmode_segment_valid(vcpu, VCPU_SREG_SS))
4740 if (!rmode_segment_valid(vcpu, VCPU_SREG_DS))
4742 if (!rmode_segment_valid(vcpu, VCPU_SREG_ES))
4744 if (!rmode_segment_valid(vcpu, VCPU_SREG_FS))
4746 if (!rmode_segment_valid(vcpu, VCPU_SREG_GS))
4749 /* protected mode guest state checks */
4750 if (!cs_ss_rpl_check(vcpu))
4752 if (!code_segment_valid(vcpu))
4754 if (!stack_segment_valid(vcpu))
4756 if (!data_segment_valid(vcpu, VCPU_SREG_DS))
4758 if (!data_segment_valid(vcpu, VCPU_SREG_ES))
4760 if (!data_segment_valid(vcpu, VCPU_SREG_FS))
4762 if (!data_segment_valid(vcpu, VCPU_SREG_GS))
4764 if (!tr_valid(vcpu))
4766 if (!ldtr_valid(vcpu))
4770 * - Add checks on RIP
4771 * - Add checks on RFLAGS
4777 static bool page_address_valid(struct kvm_vcpu *vcpu, gpa_t gpa)
4779 return PAGE_ALIGNED(gpa) && !(gpa >> cpuid_maxphyaddr(vcpu));
4782 static int init_rmode_tss(struct kvm *kvm)
4788 idx = srcu_read_lock(&kvm->srcu);
4789 fn = kvm->arch.tss_addr >> PAGE_SHIFT;
4790 r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE);
4793 data = TSS_BASE_SIZE + TSS_REDIRECTION_SIZE;
4794 r = kvm_write_guest_page(kvm, fn++, &data,
4795 TSS_IOPB_BASE_OFFSET, sizeof(u16));
4798 r = kvm_clear_guest_page(kvm, fn++, 0, PAGE_SIZE);
4801 r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE);
4805 r = kvm_write_guest_page(kvm, fn, &data,
4806 RMODE_TSS_SIZE - 2 * PAGE_SIZE - 1,
4809 srcu_read_unlock(&kvm->srcu, idx);
4813 static int init_rmode_identity_map(struct kvm *kvm)
4816 kvm_pfn_t identity_map_pfn;
4819 /* Protect kvm->arch.ept_identity_pagetable_done. */
4820 mutex_lock(&kvm->slots_lock);
4822 if (likely(kvm->arch.ept_identity_pagetable_done))
4825 if (!kvm->arch.ept_identity_map_addr)
4826 kvm->arch.ept_identity_map_addr = VMX_EPT_IDENTITY_PAGETABLE_ADDR;
4827 identity_map_pfn = kvm->arch.ept_identity_map_addr >> PAGE_SHIFT;
4829 r = __x86_set_memory_region(kvm, IDENTITY_PAGETABLE_PRIVATE_MEMSLOT,
4830 kvm->arch.ept_identity_map_addr, PAGE_SIZE);
4834 idx = srcu_read_lock(&kvm->srcu);
4835 r = kvm_clear_guest_page(kvm, identity_map_pfn, 0, PAGE_SIZE);
4838 /* Set up identity-mapping pagetable for EPT in real mode */
4839 for (i = 0; i < PT32_ENT_PER_PAGE; i++) {
4840 tmp = (i << 22) + (_PAGE_PRESENT | _PAGE_RW | _PAGE_USER |
4841 _PAGE_ACCESSED | _PAGE_DIRTY | _PAGE_PSE);
4842 r = kvm_write_guest_page(kvm, identity_map_pfn,
4843 &tmp, i * sizeof(tmp), sizeof(tmp));
4847 kvm->arch.ept_identity_pagetable_done = true;
4850 srcu_read_unlock(&kvm->srcu, idx);
4853 mutex_unlock(&kvm->slots_lock);
4857 static void seg_setup(int seg)
4859 const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
4862 vmcs_write16(sf->selector, 0);
4863 vmcs_writel(sf->base, 0);
4864 vmcs_write32(sf->limit, 0xffff);
4866 if (seg == VCPU_SREG_CS)
4867 ar |= 0x08; /* code segment */
4869 vmcs_write32(sf->ar_bytes, ar);
4872 static int alloc_apic_access_page(struct kvm *kvm)
4877 mutex_lock(&kvm->slots_lock);
4878 if (kvm->arch.apic_access_page_done)
4880 r = __x86_set_memory_region(kvm, APIC_ACCESS_PAGE_PRIVATE_MEMSLOT,
4881 APIC_DEFAULT_PHYS_BASE, PAGE_SIZE);
4885 page = gfn_to_page(kvm, APIC_DEFAULT_PHYS_BASE >> PAGE_SHIFT);
4886 if (is_error_page(page)) {
4892 * Do not pin the page in memory, so that memory hot-unplug
4893 * is able to migrate it.
4896 kvm->arch.apic_access_page_done = true;
4898 mutex_unlock(&kvm->slots_lock);
4902 static int allocate_vpid(void)
4908 spin_lock(&vmx_vpid_lock);
4909 vpid = find_first_zero_bit(vmx_vpid_bitmap, VMX_NR_VPIDS);
4910 if (vpid < VMX_NR_VPIDS)
4911 __set_bit(vpid, vmx_vpid_bitmap);
4914 spin_unlock(&vmx_vpid_lock);
4918 static void free_vpid(int vpid)
4920 if (!enable_vpid || vpid == 0)
4922 spin_lock(&vmx_vpid_lock);
4923 __clear_bit(vpid, vmx_vpid_bitmap);
4924 spin_unlock(&vmx_vpid_lock);
4927 #define MSR_TYPE_R 1
4928 #define MSR_TYPE_W 2
4929 static void __vmx_disable_intercept_for_msr(unsigned long *msr_bitmap,
4932 int f = sizeof(unsigned long);
4934 if (!cpu_has_vmx_msr_bitmap())
4938 * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
4939 * have the write-low and read-high bitmap offsets the wrong way round.
4940 * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
4942 if (msr <= 0x1fff) {
4943 if (type & MSR_TYPE_R)
4945 __clear_bit(msr, msr_bitmap + 0x000 / f);
4947 if (type & MSR_TYPE_W)
4949 __clear_bit(msr, msr_bitmap + 0x800 / f);
4951 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
4953 if (type & MSR_TYPE_R)
4955 __clear_bit(msr, msr_bitmap + 0x400 / f);
4957 if (type & MSR_TYPE_W)
4959 __clear_bit(msr, msr_bitmap + 0xc00 / f);
4965 * If a msr is allowed by L0, we should check whether it is allowed by L1.
4966 * The corresponding bit will be cleared unless both of L0 and L1 allow it.
4968 static void nested_vmx_disable_intercept_for_msr(unsigned long *msr_bitmap_l1,
4969 unsigned long *msr_bitmap_nested,
4972 int f = sizeof(unsigned long);
4975 * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
4976 * have the write-low and read-high bitmap offsets the wrong way round.
4977 * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
4979 if (msr <= 0x1fff) {
4980 if (type & MSR_TYPE_R &&
4981 !test_bit(msr, msr_bitmap_l1 + 0x000 / f))
4983 __clear_bit(msr, msr_bitmap_nested + 0x000 / f);
4985 if (type & MSR_TYPE_W &&
4986 !test_bit(msr, msr_bitmap_l1 + 0x800 / f))
4988 __clear_bit(msr, msr_bitmap_nested + 0x800 / f);
4990 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
4992 if (type & MSR_TYPE_R &&
4993 !test_bit(msr, msr_bitmap_l1 + 0x400 / f))
4995 __clear_bit(msr, msr_bitmap_nested + 0x400 / f);
4997 if (type & MSR_TYPE_W &&
4998 !test_bit(msr, msr_bitmap_l1 + 0xc00 / f))
5000 __clear_bit(msr, msr_bitmap_nested + 0xc00 / f);
5005 static void vmx_disable_intercept_for_msr(u32 msr, bool longmode_only)
5008 __vmx_disable_intercept_for_msr(vmx_msr_bitmap_legacy,
5009 msr, MSR_TYPE_R | MSR_TYPE_W);
5010 __vmx_disable_intercept_for_msr(vmx_msr_bitmap_longmode,
5011 msr, MSR_TYPE_R | MSR_TYPE_W);
5014 #define X2APIC_MSR(r) (APIC_BASE_MSR + ((r) >> 4))
5016 static void vmx_disable_intercept_msr_x2apic(u32 msr, int type, bool apicv_only)
5018 __vmx_disable_intercept_for_msr(vmx_msr_bitmap_legacy_x2apic_apicv,
5020 __vmx_disable_intercept_for_msr(vmx_msr_bitmap_longmode_x2apic_apicv,
5023 __vmx_disable_intercept_for_msr(vmx_msr_bitmap_legacy_x2apic,
5025 __vmx_disable_intercept_for_msr(vmx_msr_bitmap_longmode_x2apic,
5030 static bool vmx_get_enable_apicv(struct kvm_vcpu *vcpu)
5032 return enable_apicv;
5035 static void nested_mark_vmcs12_pages_dirty(struct kvm_vcpu *vcpu)
5037 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
5041 * Don't need to mark the APIC access page dirty; it is never
5042 * written to by the CPU during APIC virtualization.
5045 if (nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW)) {
5046 gfn = vmcs12->virtual_apic_page_addr >> PAGE_SHIFT;
5047 kvm_vcpu_mark_page_dirty(vcpu, gfn);
5050 if (nested_cpu_has_posted_intr(vmcs12)) {
5051 gfn = vmcs12->posted_intr_desc_addr >> PAGE_SHIFT;
5052 kvm_vcpu_mark_page_dirty(vcpu, gfn);
5057 static void vmx_complete_nested_posted_interrupt(struct kvm_vcpu *vcpu)
5059 struct vcpu_vmx *vmx = to_vmx(vcpu);
5064 if (!vmx->nested.pi_desc || !vmx->nested.pi_pending)
5067 vmx->nested.pi_pending = false;
5068 if (!pi_test_and_clear_on(vmx->nested.pi_desc))
5071 max_irr = find_last_bit((unsigned long *)vmx->nested.pi_desc->pir, 256);
5072 if (max_irr != 256) {
5073 vapic_page = kmap(vmx->nested.virtual_apic_page);
5074 __kvm_apic_update_irr(vmx->nested.pi_desc->pir,
5075 vapic_page, &max_irr);
5076 kunmap(vmx->nested.virtual_apic_page);
5078 status = vmcs_read16(GUEST_INTR_STATUS);
5079 if ((u8)max_irr > ((u8)status & 0xff)) {
5081 status |= (u8)max_irr;
5082 vmcs_write16(GUEST_INTR_STATUS, status);
5086 nested_mark_vmcs12_pages_dirty(vcpu);
5089 static inline bool kvm_vcpu_trigger_posted_interrupt(struct kvm_vcpu *vcpu,
5093 int pi_vec = nested ? POSTED_INTR_NESTED_VECTOR : POSTED_INTR_VECTOR;
5095 if (vcpu->mode == IN_GUEST_MODE) {
5097 * The vector of interrupt to be delivered to vcpu had
5098 * been set in PIR before this function.
5100 * Following cases will be reached in this block, and
5101 * we always send a notification event in all cases as
5104 * Case 1: vcpu keeps in non-root mode. Sending a
5105 * notification event posts the interrupt to vcpu.
5107 * Case 2: vcpu exits to root mode and is still
5108 * runnable. PIR will be synced to vIRR before the
5109 * next vcpu entry. Sending a notification event in
5110 * this case has no effect, as vcpu is not in root
5113 * Case 3: vcpu exits to root mode and is blocked.
5114 * vcpu_block() has already synced PIR to vIRR and
5115 * never blocks vcpu if vIRR is not cleared. Therefore,
5116 * a blocked vcpu here does not wait for any requested
5117 * interrupts in PIR, and sending a notification event
5118 * which has no effect is safe here.
5121 apic->send_IPI_mask(get_cpu_mask(vcpu->cpu), pi_vec);
5128 static int vmx_deliver_nested_posted_interrupt(struct kvm_vcpu *vcpu,
5131 struct vcpu_vmx *vmx = to_vmx(vcpu);
5133 if (is_guest_mode(vcpu) &&
5134 vector == vmx->nested.posted_intr_nv) {
5136 * If a posted intr is not recognized by hardware,
5137 * we will accomplish it in the next vmentry.
5139 vmx->nested.pi_pending = true;
5140 kvm_make_request(KVM_REQ_EVENT, vcpu);
5141 /* the PIR and ON have been set by L1. */
5142 if (!kvm_vcpu_trigger_posted_interrupt(vcpu, true))
5143 kvm_vcpu_kick(vcpu);
5149 * Send interrupt to vcpu via posted interrupt way.
5150 * 1. If target vcpu is running(non-root mode), send posted interrupt
5151 * notification to vcpu and hardware will sync PIR to vIRR atomically.
5152 * 2. If target vcpu isn't running(root mode), kick it to pick up the
5153 * interrupt from PIR in next vmentry.
5155 static void vmx_deliver_posted_interrupt(struct kvm_vcpu *vcpu, int vector)
5157 struct vcpu_vmx *vmx = to_vmx(vcpu);
5160 r = vmx_deliver_nested_posted_interrupt(vcpu, vector);
5164 if (pi_test_and_set_pir(vector, &vmx->pi_desc))
5167 /* If a previous notification has sent the IPI, nothing to do. */
5168 if (pi_test_and_set_on(&vmx->pi_desc))
5171 if (!kvm_vcpu_trigger_posted_interrupt(vcpu, false))
5172 kvm_vcpu_kick(vcpu);
5176 * Set up the vmcs's constant host-state fields, i.e., host-state fields that
5177 * will not change in the lifetime of the guest.
5178 * Note that host-state that does change is set elsewhere. E.g., host-state
5179 * that is set differently for each CPU is set in vmx_vcpu_load(), not here.
5181 static void vmx_set_constant_host_state(struct vcpu_vmx *vmx)
5186 unsigned long cr0, cr3, cr4;
5189 WARN_ON(cr0 & X86_CR0_TS);
5190 vmcs_writel(HOST_CR0, cr0); /* 22.2.3 */
5193 * Save the most likely value for this task's CR3 in the VMCS.
5194 * We can't use __get_current_cr3_fast() because we're not atomic.
5197 vmcs_writel(HOST_CR3, cr3); /* 22.2.3 FIXME: shadow tables */
5198 vmx->loaded_vmcs->vmcs_host_cr3 = cr3;
5200 /* Save the most likely value for this task's CR4 in the VMCS. */
5201 cr4 = cr4_read_shadow();
5202 vmcs_writel(HOST_CR4, cr4); /* 22.2.3, 22.2.5 */
5203 vmx->loaded_vmcs->vmcs_host_cr4 = cr4;
5205 vmcs_write16(HOST_CS_SELECTOR, __KERNEL_CS); /* 22.2.4 */
5206 #ifdef CONFIG_X86_64
5208 * Load null selectors, so we can avoid reloading them in
5209 * __vmx_load_host_state(), in case userspace uses the null selectors
5210 * too (the expected case).
5212 vmcs_write16(HOST_DS_SELECTOR, 0);
5213 vmcs_write16(HOST_ES_SELECTOR, 0);
5215 vmcs_write16(HOST_DS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
5216 vmcs_write16(HOST_ES_SELECTOR, __KERNEL_DS); /* 22.2.4 */
5218 vmcs_write16(HOST_SS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
5219 vmcs_write16(HOST_TR_SELECTOR, GDT_ENTRY_TSS*8); /* 22.2.4 */
5222 vmcs_writel(HOST_IDTR_BASE, dt.address); /* 22.2.4 */
5223 vmx->host_idt_base = dt.address;
5225 vmcs_writel(HOST_RIP, vmx_return); /* 22.2.5 */
5227 rdmsr(MSR_IA32_SYSENTER_CS, low32, high32);
5228 vmcs_write32(HOST_IA32_SYSENTER_CS, low32);
5229 rdmsrl(MSR_IA32_SYSENTER_EIP, tmpl);
5230 vmcs_writel(HOST_IA32_SYSENTER_EIP, tmpl); /* 22.2.3 */
5232 if (vmcs_config.vmexit_ctrl & VM_EXIT_LOAD_IA32_PAT) {
5233 rdmsr(MSR_IA32_CR_PAT, low32, high32);
5234 vmcs_write64(HOST_IA32_PAT, low32 | ((u64) high32 << 32));
5238 static void set_cr4_guest_host_mask(struct vcpu_vmx *vmx)
5240 vmx->vcpu.arch.cr4_guest_owned_bits = KVM_CR4_GUEST_OWNED_BITS;
5242 vmx->vcpu.arch.cr4_guest_owned_bits |= X86_CR4_PGE;
5243 if (is_guest_mode(&vmx->vcpu))
5244 vmx->vcpu.arch.cr4_guest_owned_bits &=
5245 ~get_vmcs12(&vmx->vcpu)->cr4_guest_host_mask;
5246 vmcs_writel(CR4_GUEST_HOST_MASK, ~vmx->vcpu.arch.cr4_guest_owned_bits);
5249 static u32 vmx_pin_based_exec_ctrl(struct vcpu_vmx *vmx)
5251 u32 pin_based_exec_ctrl = vmcs_config.pin_based_exec_ctrl;
5253 if (!kvm_vcpu_apicv_active(&vmx->vcpu))
5254 pin_based_exec_ctrl &= ~PIN_BASED_POSTED_INTR;
5257 pin_based_exec_ctrl &= ~PIN_BASED_VIRTUAL_NMIS;
5259 /* Enable the preemption timer dynamically */
5260 pin_based_exec_ctrl &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
5261 return pin_based_exec_ctrl;
5264 static void vmx_refresh_apicv_exec_ctrl(struct kvm_vcpu *vcpu)
5266 struct vcpu_vmx *vmx = to_vmx(vcpu);
5268 vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, vmx_pin_based_exec_ctrl(vmx));
5269 if (cpu_has_secondary_exec_ctrls()) {
5270 if (kvm_vcpu_apicv_active(vcpu))
5271 vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL,
5272 SECONDARY_EXEC_APIC_REGISTER_VIRT |
5273 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
5275 vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL,
5276 SECONDARY_EXEC_APIC_REGISTER_VIRT |
5277 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
5280 if (cpu_has_vmx_msr_bitmap())
5281 vmx_set_msr_bitmap(vcpu);
5284 static u32 vmx_exec_control(struct vcpu_vmx *vmx)
5286 u32 exec_control = vmcs_config.cpu_based_exec_ctrl;
5288 if (vmx->vcpu.arch.switch_db_regs & KVM_DEBUGREG_WONT_EXIT)
5289 exec_control &= ~CPU_BASED_MOV_DR_EXITING;
5291 if (!cpu_need_tpr_shadow(&vmx->vcpu)) {
5292 exec_control &= ~CPU_BASED_TPR_SHADOW;
5293 #ifdef CONFIG_X86_64
5294 exec_control |= CPU_BASED_CR8_STORE_EXITING |
5295 CPU_BASED_CR8_LOAD_EXITING;
5299 exec_control |= CPU_BASED_CR3_STORE_EXITING |
5300 CPU_BASED_CR3_LOAD_EXITING |
5301 CPU_BASED_INVLPG_EXITING;
5302 return exec_control;
5305 static bool vmx_rdrand_supported(void)
5307 return vmcs_config.cpu_based_2nd_exec_ctrl &
5308 SECONDARY_EXEC_RDRAND_EXITING;
5311 static bool vmx_rdseed_supported(void)
5313 return vmcs_config.cpu_based_2nd_exec_ctrl &
5314 SECONDARY_EXEC_RDSEED_EXITING;
5317 static void vmx_compute_secondary_exec_control(struct vcpu_vmx *vmx)
5319 struct kvm_vcpu *vcpu = &vmx->vcpu;
5321 u32 exec_control = vmcs_config.cpu_based_2nd_exec_ctrl;
5323 if (!cpu_need_virtualize_apic_accesses(vcpu))
5324 exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
5326 exec_control &= ~SECONDARY_EXEC_ENABLE_VPID;
5328 exec_control &= ~SECONDARY_EXEC_ENABLE_EPT;
5329 enable_unrestricted_guest = 0;
5330 /* Enable INVPCID for non-ept guests may cause performance regression. */
5331 exec_control &= ~SECONDARY_EXEC_ENABLE_INVPCID;
5333 if (!enable_unrestricted_guest)
5334 exec_control &= ~SECONDARY_EXEC_UNRESTRICTED_GUEST;
5336 exec_control &= ~SECONDARY_EXEC_PAUSE_LOOP_EXITING;
5337 if (!kvm_vcpu_apicv_active(vcpu))
5338 exec_control &= ~(SECONDARY_EXEC_APIC_REGISTER_VIRT |
5339 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
5340 exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
5342 /* SECONDARY_EXEC_DESC is enabled/disabled on writes to CR4.UMIP,
5343 * in vmx_set_cr4. */
5344 exec_control &= ~SECONDARY_EXEC_DESC;
5346 /* SECONDARY_EXEC_SHADOW_VMCS is enabled when L1 executes VMPTRLD
5348 We can NOT enable shadow_vmcs here because we don't have yet
5351 exec_control &= ~SECONDARY_EXEC_SHADOW_VMCS;
5354 exec_control &= ~SECONDARY_EXEC_ENABLE_PML;
5356 if (vmx_xsaves_supported()) {
5357 /* Exposing XSAVES only when XSAVE is exposed */
5358 bool xsaves_enabled =
5359 guest_cpuid_has(vcpu, X86_FEATURE_XSAVE) &&
5360 guest_cpuid_has(vcpu, X86_FEATURE_XSAVES);
5362 if (!xsaves_enabled)
5363 exec_control &= ~SECONDARY_EXEC_XSAVES;
5367 vmx->nested.nested_vmx_secondary_ctls_high |=
5368 SECONDARY_EXEC_XSAVES;
5370 vmx->nested.nested_vmx_secondary_ctls_high &=
5371 ~SECONDARY_EXEC_XSAVES;
5375 if (vmx_rdtscp_supported()) {
5376 bool rdtscp_enabled = guest_cpuid_has(vcpu, X86_FEATURE_RDTSCP);
5377 if (!rdtscp_enabled)
5378 exec_control &= ~SECONDARY_EXEC_RDTSCP;
5382 vmx->nested.nested_vmx_secondary_ctls_high |=
5383 SECONDARY_EXEC_RDTSCP;
5385 vmx->nested.nested_vmx_secondary_ctls_high &=
5386 ~SECONDARY_EXEC_RDTSCP;
5390 if (vmx_invpcid_supported()) {
5391 /* Exposing INVPCID only when PCID is exposed */
5392 bool invpcid_enabled =
5393 guest_cpuid_has(vcpu, X86_FEATURE_INVPCID) &&
5394 guest_cpuid_has(vcpu, X86_FEATURE_PCID);
5396 if (!invpcid_enabled) {
5397 exec_control &= ~SECONDARY_EXEC_ENABLE_INVPCID;
5398 guest_cpuid_clear(vcpu, X86_FEATURE_INVPCID);
5402 if (invpcid_enabled)
5403 vmx->nested.nested_vmx_secondary_ctls_high |=
5404 SECONDARY_EXEC_ENABLE_INVPCID;
5406 vmx->nested.nested_vmx_secondary_ctls_high &=
5407 ~SECONDARY_EXEC_ENABLE_INVPCID;
5411 if (vmx_rdrand_supported()) {
5412 bool rdrand_enabled = guest_cpuid_has(vcpu, X86_FEATURE_RDRAND);
5414 exec_control &= ~SECONDARY_EXEC_RDRAND_EXITING;
5418 vmx->nested.nested_vmx_secondary_ctls_high |=
5419 SECONDARY_EXEC_RDRAND_EXITING;
5421 vmx->nested.nested_vmx_secondary_ctls_high &=
5422 ~SECONDARY_EXEC_RDRAND_EXITING;
5426 if (vmx_rdseed_supported()) {
5427 bool rdseed_enabled = guest_cpuid_has(vcpu, X86_FEATURE_RDSEED);
5429 exec_control &= ~SECONDARY_EXEC_RDSEED_EXITING;
5433 vmx->nested.nested_vmx_secondary_ctls_high |=
5434 SECONDARY_EXEC_RDSEED_EXITING;
5436 vmx->nested.nested_vmx_secondary_ctls_high &=
5437 ~SECONDARY_EXEC_RDSEED_EXITING;
5441 vmx->secondary_exec_control = exec_control;
5444 static void ept_set_mmio_spte_mask(void)
5447 * EPT Misconfigurations can be generated if the value of bits 2:0
5448 * of an EPT paging-structure entry is 110b (write/execute).
5450 kvm_mmu_set_mmio_spte_mask(VMX_EPT_RWX_MASK,
5451 VMX_EPT_MISCONFIG_WX_VALUE);
5454 #define VMX_XSS_EXIT_BITMAP 0
5456 * Sets up the vmcs for emulated real mode.
5458 static void vmx_vcpu_setup(struct vcpu_vmx *vmx)
5460 #ifdef CONFIG_X86_64
5465 if (enable_shadow_vmcs) {
5466 vmcs_write64(VMREAD_BITMAP, __pa(vmx_vmread_bitmap));
5467 vmcs_write64(VMWRITE_BITMAP, __pa(vmx_vmwrite_bitmap));
5469 if (cpu_has_vmx_msr_bitmap())
5470 vmcs_write64(MSR_BITMAP, __pa(vmx_msr_bitmap_legacy));
5472 vmcs_write64(VMCS_LINK_POINTER, -1ull); /* 22.3.1.5 */
5475 vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, vmx_pin_based_exec_ctrl(vmx));
5476 vmx->hv_deadline_tsc = -1;
5478 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, vmx_exec_control(vmx));
5480 if (cpu_has_secondary_exec_ctrls()) {
5481 vmx_compute_secondary_exec_control(vmx);
5482 vmcs_write32(SECONDARY_VM_EXEC_CONTROL,
5483 vmx->secondary_exec_control);
5486 if (kvm_vcpu_apicv_active(&vmx->vcpu)) {
5487 vmcs_write64(EOI_EXIT_BITMAP0, 0);
5488 vmcs_write64(EOI_EXIT_BITMAP1, 0);
5489 vmcs_write64(EOI_EXIT_BITMAP2, 0);
5490 vmcs_write64(EOI_EXIT_BITMAP3, 0);
5492 vmcs_write16(GUEST_INTR_STATUS, 0);
5494 vmcs_write16(POSTED_INTR_NV, POSTED_INTR_VECTOR);
5495 vmcs_write64(POSTED_INTR_DESC_ADDR, __pa((&vmx->pi_desc)));
5499 vmcs_write32(PLE_GAP, ple_gap);
5500 vmx->ple_window = ple_window;
5501 vmx->ple_window_dirty = true;
5504 vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK, 0);
5505 vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH, 0);
5506 vmcs_write32(CR3_TARGET_COUNT, 0); /* 22.2.1 */
5508 vmcs_write16(HOST_FS_SELECTOR, 0); /* 22.2.4 */
5509 vmcs_write16(HOST_GS_SELECTOR, 0); /* 22.2.4 */
5510 vmx_set_constant_host_state(vmx);
5511 #ifdef CONFIG_X86_64
5512 rdmsrl(MSR_FS_BASE, a);
5513 vmcs_writel(HOST_FS_BASE, a); /* 22.2.4 */
5514 rdmsrl(MSR_GS_BASE, a);
5515 vmcs_writel(HOST_GS_BASE, a); /* 22.2.4 */
5517 vmcs_writel(HOST_FS_BASE, 0); /* 22.2.4 */
5518 vmcs_writel(HOST_GS_BASE, 0); /* 22.2.4 */
5521 if (cpu_has_vmx_vmfunc())
5522 vmcs_write64(VM_FUNCTION_CONTROL, 0);
5524 vmcs_write32(VM_EXIT_MSR_STORE_COUNT, 0);
5525 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, 0);
5526 vmcs_write64(VM_EXIT_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.host));
5527 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, 0);
5528 vmcs_write64(VM_ENTRY_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.guest));
5530 if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT)
5531 vmcs_write64(GUEST_IA32_PAT, vmx->vcpu.arch.pat);
5533 for (i = 0; i < ARRAY_SIZE(vmx_msr_index); ++i) {
5534 u32 index = vmx_msr_index[i];
5535 u32 data_low, data_high;
5538 if (rdmsr_safe(index, &data_low, &data_high) < 0)
5540 if (wrmsr_safe(index, data_low, data_high) < 0)
5542 vmx->guest_msrs[j].index = i;
5543 vmx->guest_msrs[j].data = 0;
5544 vmx->guest_msrs[j].mask = -1ull;
5549 vm_exit_controls_init(vmx, vmcs_config.vmexit_ctrl);
5551 /* 22.2.1, 20.8.1 */
5552 vm_entry_controls_init(vmx, vmcs_config.vmentry_ctrl);
5554 vmx->vcpu.arch.cr0_guest_owned_bits = X86_CR0_TS;
5555 vmcs_writel(CR0_GUEST_HOST_MASK, ~X86_CR0_TS);
5557 set_cr4_guest_host_mask(vmx);
5559 if (vmx_xsaves_supported())
5560 vmcs_write64(XSS_EXIT_BITMAP, VMX_XSS_EXIT_BITMAP);
5563 ASSERT(vmx->pml_pg);
5564 vmcs_write64(PML_ADDRESS, page_to_phys(vmx->pml_pg));
5565 vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1);
5569 static void vmx_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event)
5571 struct vcpu_vmx *vmx = to_vmx(vcpu);
5572 struct msr_data apic_base_msr;
5575 vmx->rmode.vm86_active = 0;
5577 vmx->vcpu.arch.regs[VCPU_REGS_RDX] = get_rdx_init_val();
5578 kvm_set_cr8(vcpu, 0);
5581 apic_base_msr.data = APIC_DEFAULT_PHYS_BASE |
5582 MSR_IA32_APICBASE_ENABLE;
5583 if (kvm_vcpu_is_reset_bsp(vcpu))
5584 apic_base_msr.data |= MSR_IA32_APICBASE_BSP;
5585 apic_base_msr.host_initiated = true;
5586 kvm_set_apic_base(vcpu, &apic_base_msr);
5589 vmx_segment_cache_clear(vmx);
5591 seg_setup(VCPU_SREG_CS);
5592 vmcs_write16(GUEST_CS_SELECTOR, 0xf000);
5593 vmcs_writel(GUEST_CS_BASE, 0xffff0000ul);
5595 seg_setup(VCPU_SREG_DS);
5596 seg_setup(VCPU_SREG_ES);
5597 seg_setup(VCPU_SREG_FS);
5598 seg_setup(VCPU_SREG_GS);
5599 seg_setup(VCPU_SREG_SS);
5601 vmcs_write16(GUEST_TR_SELECTOR, 0);
5602 vmcs_writel(GUEST_TR_BASE, 0);
5603 vmcs_write32(GUEST_TR_LIMIT, 0xffff);
5604 vmcs_write32(GUEST_TR_AR_BYTES, 0x008b);
5606 vmcs_write16(GUEST_LDTR_SELECTOR, 0);
5607 vmcs_writel(GUEST_LDTR_BASE, 0);
5608 vmcs_write32(GUEST_LDTR_LIMIT, 0xffff);
5609 vmcs_write32(GUEST_LDTR_AR_BYTES, 0x00082);
5612 vmcs_write32(GUEST_SYSENTER_CS, 0);
5613 vmcs_writel(GUEST_SYSENTER_ESP, 0);
5614 vmcs_writel(GUEST_SYSENTER_EIP, 0);
5615 vmcs_write64(GUEST_IA32_DEBUGCTL, 0);
5618 kvm_set_rflags(vcpu, X86_EFLAGS_FIXED);
5619 kvm_rip_write(vcpu, 0xfff0);
5621 vmcs_writel(GUEST_GDTR_BASE, 0);
5622 vmcs_write32(GUEST_GDTR_LIMIT, 0xffff);
5624 vmcs_writel(GUEST_IDTR_BASE, 0);
5625 vmcs_write32(GUEST_IDTR_LIMIT, 0xffff);
5627 vmcs_write32(GUEST_ACTIVITY_STATE, GUEST_ACTIVITY_ACTIVE);
5628 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, 0);
5629 vmcs_writel(GUEST_PENDING_DBG_EXCEPTIONS, 0);
5630 if (kvm_mpx_supported())
5631 vmcs_write64(GUEST_BNDCFGS, 0);
5635 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0); /* 22.2.1 */
5637 if (cpu_has_vmx_tpr_shadow() && !init_event) {
5638 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, 0);
5639 if (cpu_need_tpr_shadow(vcpu))
5640 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR,
5641 __pa(vcpu->arch.apic->regs));
5642 vmcs_write32(TPR_THRESHOLD, 0);
5645 kvm_make_request(KVM_REQ_APIC_PAGE_RELOAD, vcpu);
5648 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid);
5650 cr0 = X86_CR0_NW | X86_CR0_CD | X86_CR0_ET;
5651 vmx->vcpu.arch.cr0 = cr0;
5652 vmx_set_cr0(vcpu, cr0); /* enter rmode */
5653 vmx_set_cr4(vcpu, 0);
5654 vmx_set_efer(vcpu, 0);
5656 update_exception_bitmap(vcpu);
5658 vpid_sync_context(vmx->vpid);
5662 * In nested virtualization, check if L1 asked to exit on external interrupts.
5663 * For most existing hypervisors, this will always return true.
5665 static bool nested_exit_on_intr(struct kvm_vcpu *vcpu)
5667 return get_vmcs12(vcpu)->pin_based_vm_exec_control &
5668 PIN_BASED_EXT_INTR_MASK;
5672 * In nested virtualization, check if L1 has set
5673 * VM_EXIT_ACK_INTR_ON_EXIT
5675 static bool nested_exit_intr_ack_set(struct kvm_vcpu *vcpu)
5677 return get_vmcs12(vcpu)->vm_exit_controls &
5678 VM_EXIT_ACK_INTR_ON_EXIT;
5681 static bool nested_exit_on_nmi(struct kvm_vcpu *vcpu)
5683 return get_vmcs12(vcpu)->pin_based_vm_exec_control &
5684 PIN_BASED_NMI_EXITING;
5687 static void enable_irq_window(struct kvm_vcpu *vcpu)
5689 vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL,
5690 CPU_BASED_VIRTUAL_INTR_PENDING);
5693 static void enable_nmi_window(struct kvm_vcpu *vcpu)
5696 vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & GUEST_INTR_STATE_STI) {
5697 enable_irq_window(vcpu);
5701 vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL,
5702 CPU_BASED_VIRTUAL_NMI_PENDING);
5705 static void vmx_inject_irq(struct kvm_vcpu *vcpu)
5707 struct vcpu_vmx *vmx = to_vmx(vcpu);
5709 int irq = vcpu->arch.interrupt.nr;
5711 trace_kvm_inj_virq(irq);
5713 ++vcpu->stat.irq_injections;
5714 if (vmx->rmode.vm86_active) {
5716 if (vcpu->arch.interrupt.soft)
5717 inc_eip = vcpu->arch.event_exit_inst_len;
5718 if (kvm_inject_realmode_interrupt(vcpu, irq, inc_eip) != EMULATE_DONE)
5719 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
5722 intr = irq | INTR_INFO_VALID_MASK;
5723 if (vcpu->arch.interrupt.soft) {
5724 intr |= INTR_TYPE_SOFT_INTR;
5725 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
5726 vmx->vcpu.arch.event_exit_inst_len);
5728 intr |= INTR_TYPE_EXT_INTR;
5729 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr);
5732 static void vmx_inject_nmi(struct kvm_vcpu *vcpu)
5734 struct vcpu_vmx *vmx = to_vmx(vcpu);
5738 * Tracking the NMI-blocked state in software is built upon
5739 * finding the next open IRQ window. This, in turn, depends on
5740 * well-behaving guests: They have to keep IRQs disabled at
5741 * least as long as the NMI handler runs. Otherwise we may
5742 * cause NMI nesting, maybe breaking the guest. But as this is
5743 * highly unlikely, we can live with the residual risk.
5745 vmx->loaded_vmcs->soft_vnmi_blocked = 1;
5746 vmx->loaded_vmcs->vnmi_blocked_time = 0;
5749 ++vcpu->stat.nmi_injections;
5750 vmx->loaded_vmcs->nmi_known_unmasked = false;
5752 if (vmx->rmode.vm86_active) {
5753 if (kvm_inject_realmode_interrupt(vcpu, NMI_VECTOR, 0) != EMULATE_DONE)
5754 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
5758 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
5759 INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK | NMI_VECTOR);
5762 static bool vmx_get_nmi_mask(struct kvm_vcpu *vcpu)
5764 struct vcpu_vmx *vmx = to_vmx(vcpu);
5768 return vmx->loaded_vmcs->soft_vnmi_blocked;
5769 if (vmx->loaded_vmcs->nmi_known_unmasked)
5771 masked = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & GUEST_INTR_STATE_NMI;
5772 vmx->loaded_vmcs->nmi_known_unmasked = !masked;
5776 static void vmx_set_nmi_mask(struct kvm_vcpu *vcpu, bool masked)
5778 struct vcpu_vmx *vmx = to_vmx(vcpu);
5781 if (vmx->loaded_vmcs->soft_vnmi_blocked != masked) {
5782 vmx->loaded_vmcs->soft_vnmi_blocked = masked;
5783 vmx->loaded_vmcs->vnmi_blocked_time = 0;
5786 vmx->loaded_vmcs->nmi_known_unmasked = !masked;
5788 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
5789 GUEST_INTR_STATE_NMI);
5791 vmcs_clear_bits(GUEST_INTERRUPTIBILITY_INFO,
5792 GUEST_INTR_STATE_NMI);
5796 static int vmx_nmi_allowed(struct kvm_vcpu *vcpu)
5798 if (to_vmx(vcpu)->nested.nested_run_pending)
5802 to_vmx(vcpu)->loaded_vmcs->soft_vnmi_blocked)
5805 return !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) &
5806 (GUEST_INTR_STATE_MOV_SS | GUEST_INTR_STATE_STI
5807 | GUEST_INTR_STATE_NMI));
5810 static int vmx_interrupt_allowed(struct kvm_vcpu *vcpu)
5812 return (!to_vmx(vcpu)->nested.nested_run_pending &&
5813 vmcs_readl(GUEST_RFLAGS) & X86_EFLAGS_IF) &&
5814 !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) &
5815 (GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS));
5818 static int vmx_set_tss_addr(struct kvm *kvm, unsigned int addr)
5822 ret = x86_set_memory_region(kvm, TSS_PRIVATE_MEMSLOT, addr,
5826 kvm->arch.tss_addr = addr;
5827 return init_rmode_tss(kvm);
5830 static bool rmode_exception(struct kvm_vcpu *vcpu, int vec)
5835 * Update instruction length as we may reinject the exception
5836 * from user space while in guest debugging mode.
5838 to_vmx(vcpu)->vcpu.arch.event_exit_inst_len =
5839 vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
5840 if (vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
5844 if (vcpu->guest_debug &
5845 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))
5862 static int handle_rmode_exception(struct kvm_vcpu *vcpu,
5863 int vec, u32 err_code)
5866 * Instruction with address size override prefix opcode 0x67
5867 * Cause the #SS fault with 0 error code in VM86 mode.
5869 if (((vec == GP_VECTOR) || (vec == SS_VECTOR)) && err_code == 0) {
5870 if (emulate_instruction(vcpu, 0) == EMULATE_DONE) {
5871 if (vcpu->arch.halt_request) {
5872 vcpu->arch.halt_request = 0;
5873 return kvm_vcpu_halt(vcpu);
5881 * Forward all other exceptions that are valid in real mode.
5882 * FIXME: Breaks guest debugging in real mode, needs to be fixed with
5883 * the required debugging infrastructure rework.
5885 kvm_queue_exception(vcpu, vec);
5890 * Trigger machine check on the host. We assume all the MSRs are already set up
5891 * by the CPU and that we still run on the same CPU as the MCE occurred on.
5892 * We pass a fake environment to the machine check handler because we want
5893 * the guest to be always treated like user space, no matter what context
5894 * it used internally.
5896 static void kvm_machine_check(void)
5898 #if defined(CONFIG_X86_MCE) && defined(CONFIG_X86_64)
5899 struct pt_regs regs = {
5900 .cs = 3, /* Fake ring 3 no matter what the guest ran on */
5901 .flags = X86_EFLAGS_IF,
5904 do_machine_check(®s, 0);
5908 static int handle_machine_check(struct kvm_vcpu *vcpu)
5910 /* already handled by vcpu_run */
5914 static int handle_exception(struct kvm_vcpu *vcpu)
5916 struct vcpu_vmx *vmx = to_vmx(vcpu);
5917 struct kvm_run *kvm_run = vcpu->run;
5918 u32 intr_info, ex_no, error_code;
5919 unsigned long cr2, rip, dr6;
5921 enum emulation_result er;
5923 vect_info = vmx->idt_vectoring_info;
5924 intr_info = vmx->exit_intr_info;
5926 if (is_machine_check(intr_info))
5927 return handle_machine_check(vcpu);
5929 if (is_nmi(intr_info))
5930 return 1; /* already handled by vmx_vcpu_run() */
5932 if (is_invalid_opcode(intr_info)) {
5933 er = emulate_instruction(vcpu, EMULTYPE_TRAP_UD);
5934 if (er == EMULATE_USER_EXIT)
5936 if (er != EMULATE_DONE)
5937 kvm_queue_exception(vcpu, UD_VECTOR);
5942 if (intr_info & INTR_INFO_DELIVER_CODE_MASK)
5943 error_code = vmcs_read32(VM_EXIT_INTR_ERROR_CODE);
5946 * The #PF with PFEC.RSVD = 1 indicates the guest is accessing
5947 * MMIO, it is better to report an internal error.
5948 * See the comments in vmx_handle_exit.
5950 if ((vect_info & VECTORING_INFO_VALID_MASK) &&
5951 !(is_page_fault(intr_info) && !(error_code & PFERR_RSVD_MASK))) {
5952 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
5953 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_SIMUL_EX;
5954 vcpu->run->internal.ndata = 3;
5955 vcpu->run->internal.data[0] = vect_info;
5956 vcpu->run->internal.data[1] = intr_info;
5957 vcpu->run->internal.data[2] = error_code;
5961 if (is_page_fault(intr_info)) {
5962 cr2 = vmcs_readl(EXIT_QUALIFICATION);
5963 /* EPT won't cause page fault directly */
5964 WARN_ON_ONCE(!vcpu->arch.apf.host_apf_reason && enable_ept);
5965 return kvm_handle_page_fault(vcpu, error_code, cr2, NULL, 0);
5968 ex_no = intr_info & INTR_INFO_VECTOR_MASK;
5970 if (vmx->rmode.vm86_active && rmode_exception(vcpu, ex_no))
5971 return handle_rmode_exception(vcpu, ex_no, error_code);
5975 kvm_queue_exception_e(vcpu, AC_VECTOR, error_code);
5978 dr6 = vmcs_readl(EXIT_QUALIFICATION);
5979 if (!(vcpu->guest_debug &
5980 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))) {
5981 vcpu->arch.dr6 &= ~15;
5982 vcpu->arch.dr6 |= dr6 | DR6_RTM;
5983 if (!(dr6 & ~DR6_RESERVED)) /* icebp */
5984 skip_emulated_instruction(vcpu);
5986 kvm_queue_exception(vcpu, DB_VECTOR);
5989 kvm_run->debug.arch.dr6 = dr6 | DR6_FIXED_1;
5990 kvm_run->debug.arch.dr7 = vmcs_readl(GUEST_DR7);
5994 * Update instruction length as we may reinject #BP from
5995 * user space while in guest debugging mode. Reading it for
5996 * #DB as well causes no harm, it is not used in that case.
5998 vmx->vcpu.arch.event_exit_inst_len =
5999 vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
6000 kvm_run->exit_reason = KVM_EXIT_DEBUG;
6001 rip = kvm_rip_read(vcpu);
6002 kvm_run->debug.arch.pc = vmcs_readl(GUEST_CS_BASE) + rip;
6003 kvm_run->debug.arch.exception = ex_no;
6006 kvm_run->exit_reason = KVM_EXIT_EXCEPTION;
6007 kvm_run->ex.exception = ex_no;
6008 kvm_run->ex.error_code = error_code;
6014 static int handle_external_interrupt(struct kvm_vcpu *vcpu)
6016 ++vcpu->stat.irq_exits;
6020 static int handle_triple_fault(struct kvm_vcpu *vcpu)
6022 vcpu->run->exit_reason = KVM_EXIT_SHUTDOWN;
6023 vcpu->mmio_needed = 0;
6027 static int handle_io(struct kvm_vcpu *vcpu)
6029 unsigned long exit_qualification;
6030 int size, in, string, ret;
6033 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
6034 string = (exit_qualification & 16) != 0;
6035 in = (exit_qualification & 8) != 0;
6037 ++vcpu->stat.io_exits;
6040 return emulate_instruction(vcpu, 0) == EMULATE_DONE;
6042 port = exit_qualification >> 16;
6043 size = (exit_qualification & 7) + 1;
6045 ret = kvm_skip_emulated_instruction(vcpu);
6048 * TODO: we might be squashing a KVM_GUESTDBG_SINGLESTEP-triggered
6049 * KVM_EXIT_DEBUG here.
6051 return kvm_fast_pio_out(vcpu, size, port) && ret;
6055 vmx_patch_hypercall(struct kvm_vcpu *vcpu, unsigned char *hypercall)
6058 * Patch in the VMCALL instruction:
6060 hypercall[0] = 0x0f;
6061 hypercall[1] = 0x01;
6062 hypercall[2] = 0xc1;
6065 /* called to set cr0 as appropriate for a mov-to-cr0 exit. */
6066 static int handle_set_cr0(struct kvm_vcpu *vcpu, unsigned long val)
6068 if (is_guest_mode(vcpu)) {
6069 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
6070 unsigned long orig_val = val;
6073 * We get here when L2 changed cr0 in a way that did not change
6074 * any of L1's shadowed bits (see nested_vmx_exit_handled_cr),
6075 * but did change L0 shadowed bits. So we first calculate the
6076 * effective cr0 value that L1 would like to write into the
6077 * hardware. It consists of the L2-owned bits from the new
6078 * value combined with the L1-owned bits from L1's guest_cr0.
6080 val = (val & ~vmcs12->cr0_guest_host_mask) |
6081 (vmcs12->guest_cr0 & vmcs12->cr0_guest_host_mask);
6083 if (!nested_guest_cr0_valid(vcpu, val))
6086 if (kvm_set_cr0(vcpu, val))
6088 vmcs_writel(CR0_READ_SHADOW, orig_val);
6091 if (to_vmx(vcpu)->nested.vmxon &&
6092 !nested_host_cr0_valid(vcpu, val))
6095 return kvm_set_cr0(vcpu, val);
6099 static int handle_set_cr4(struct kvm_vcpu *vcpu, unsigned long val)
6101 if (is_guest_mode(vcpu)) {
6102 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
6103 unsigned long orig_val = val;
6105 /* analogously to handle_set_cr0 */
6106 val = (val & ~vmcs12->cr4_guest_host_mask) |
6107 (vmcs12->guest_cr4 & vmcs12->cr4_guest_host_mask);
6108 if (kvm_set_cr4(vcpu, val))
6110 vmcs_writel(CR4_READ_SHADOW, orig_val);
6113 return kvm_set_cr4(vcpu, val);
6116 static int handle_desc(struct kvm_vcpu *vcpu)
6118 WARN_ON(!(vcpu->arch.cr4 & X86_CR4_UMIP));
6119 return emulate_instruction(vcpu, 0) == EMULATE_DONE;
6122 static int handle_cr(struct kvm_vcpu *vcpu)
6124 unsigned long exit_qualification, val;
6130 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
6131 cr = exit_qualification & 15;
6132 reg = (exit_qualification >> 8) & 15;
6133 switch ((exit_qualification >> 4) & 3) {
6134 case 0: /* mov to cr */
6135 val = kvm_register_readl(vcpu, reg);
6136 trace_kvm_cr_write(cr, val);
6139 err = handle_set_cr0(vcpu, val);
6140 return kvm_complete_insn_gp(vcpu, err);
6142 err = kvm_set_cr3(vcpu, val);
6143 return kvm_complete_insn_gp(vcpu, err);
6145 err = handle_set_cr4(vcpu, val);
6146 return kvm_complete_insn_gp(vcpu, err);
6148 u8 cr8_prev = kvm_get_cr8(vcpu);
6150 err = kvm_set_cr8(vcpu, cr8);
6151 ret = kvm_complete_insn_gp(vcpu, err);
6152 if (lapic_in_kernel(vcpu))
6154 if (cr8_prev <= cr8)
6157 * TODO: we might be squashing a
6158 * KVM_GUESTDBG_SINGLESTEP-triggered
6159 * KVM_EXIT_DEBUG here.
6161 vcpu->run->exit_reason = KVM_EXIT_SET_TPR;
6167 WARN_ONCE(1, "Guest should always own CR0.TS");
6168 vmx_set_cr0(vcpu, kvm_read_cr0_bits(vcpu, ~X86_CR0_TS));
6169 trace_kvm_cr_write(0, kvm_read_cr0(vcpu));
6170 return kvm_skip_emulated_instruction(vcpu);
6171 case 1: /*mov from cr*/
6174 val = kvm_read_cr3(vcpu);
6175 kvm_register_write(vcpu, reg, val);
6176 trace_kvm_cr_read(cr, val);
6177 return kvm_skip_emulated_instruction(vcpu);
6179 val = kvm_get_cr8(vcpu);
6180 kvm_register_write(vcpu, reg, val);
6181 trace_kvm_cr_read(cr, val);
6182 return kvm_skip_emulated_instruction(vcpu);
6186 val = (exit_qualification >> LMSW_SOURCE_DATA_SHIFT) & 0x0f;
6187 trace_kvm_cr_write(0, (kvm_read_cr0(vcpu) & ~0xful) | val);
6188 kvm_lmsw(vcpu, val);
6190 return kvm_skip_emulated_instruction(vcpu);
6194 vcpu->run->exit_reason = 0;
6195 vcpu_unimpl(vcpu, "unhandled control register: op %d cr %d\n",
6196 (int)(exit_qualification >> 4) & 3, cr);
6200 static int handle_dr(struct kvm_vcpu *vcpu)
6202 unsigned long exit_qualification;
6205 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
6206 dr = exit_qualification & DEBUG_REG_ACCESS_NUM;
6208 /* First, if DR does not exist, trigger UD */
6209 if (!kvm_require_dr(vcpu, dr))
6212 /* Do not handle if the CPL > 0, will trigger GP on re-entry */
6213 if (!kvm_require_cpl(vcpu, 0))
6215 dr7 = vmcs_readl(GUEST_DR7);
6218 * As the vm-exit takes precedence over the debug trap, we
6219 * need to emulate the latter, either for the host or the
6220 * guest debugging itself.
6222 if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP) {
6223 vcpu->run->debug.arch.dr6 = vcpu->arch.dr6;
6224 vcpu->run->debug.arch.dr7 = dr7;
6225 vcpu->run->debug.arch.pc = kvm_get_linear_rip(vcpu);
6226 vcpu->run->debug.arch.exception = DB_VECTOR;
6227 vcpu->run->exit_reason = KVM_EXIT_DEBUG;
6230 vcpu->arch.dr6 &= ~15;
6231 vcpu->arch.dr6 |= DR6_BD | DR6_RTM;
6232 kvm_queue_exception(vcpu, DB_VECTOR);
6237 if (vcpu->guest_debug == 0) {
6238 vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL,
6239 CPU_BASED_MOV_DR_EXITING);
6242 * No more DR vmexits; force a reload of the debug registers
6243 * and reenter on this instruction. The next vmexit will
6244 * retrieve the full state of the debug registers.
6246 vcpu->arch.switch_db_regs |= KVM_DEBUGREG_WONT_EXIT;
6250 reg = DEBUG_REG_ACCESS_REG(exit_qualification);
6251 if (exit_qualification & TYPE_MOV_FROM_DR) {
6254 if (kvm_get_dr(vcpu, dr, &val))
6256 kvm_register_write(vcpu, reg, val);
6258 if (kvm_set_dr(vcpu, dr, kvm_register_readl(vcpu, reg)))
6261 return kvm_skip_emulated_instruction(vcpu);
6264 static u64 vmx_get_dr6(struct kvm_vcpu *vcpu)
6266 return vcpu->arch.dr6;
6269 static void vmx_set_dr6(struct kvm_vcpu *vcpu, unsigned long val)
6273 static void vmx_sync_dirty_debug_regs(struct kvm_vcpu *vcpu)
6275 get_debugreg(vcpu->arch.db[0], 0);
6276 get_debugreg(vcpu->arch.db[1], 1);
6277 get_debugreg(vcpu->arch.db[2], 2);
6278 get_debugreg(vcpu->arch.db[3], 3);
6279 get_debugreg(vcpu->arch.dr6, 6);
6280 vcpu->arch.dr7 = vmcs_readl(GUEST_DR7);
6282 vcpu->arch.switch_db_regs &= ~KVM_DEBUGREG_WONT_EXIT;
6283 vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL, CPU_BASED_MOV_DR_EXITING);
6286 static void vmx_set_dr7(struct kvm_vcpu *vcpu, unsigned long val)
6288 vmcs_writel(GUEST_DR7, val);
6291 static int handle_cpuid(struct kvm_vcpu *vcpu)
6293 return kvm_emulate_cpuid(vcpu);
6296 static int handle_rdmsr(struct kvm_vcpu *vcpu)
6298 u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX];
6299 struct msr_data msr_info;
6301 msr_info.index = ecx;
6302 msr_info.host_initiated = false;
6303 if (vmx_get_msr(vcpu, &msr_info)) {
6304 trace_kvm_msr_read_ex(ecx);
6305 kvm_inject_gp(vcpu, 0);
6309 trace_kvm_msr_read(ecx, msr_info.data);
6311 /* FIXME: handling of bits 32:63 of rax, rdx */
6312 vcpu->arch.regs[VCPU_REGS_RAX] = msr_info.data & -1u;
6313 vcpu->arch.regs[VCPU_REGS_RDX] = (msr_info.data >> 32) & -1u;
6314 return kvm_skip_emulated_instruction(vcpu);
6317 static int handle_wrmsr(struct kvm_vcpu *vcpu)
6319 struct msr_data msr;
6320 u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX];
6321 u64 data = (vcpu->arch.regs[VCPU_REGS_RAX] & -1u)
6322 | ((u64)(vcpu->arch.regs[VCPU_REGS_RDX] & -1u) << 32);
6326 msr.host_initiated = false;
6327 if (kvm_set_msr(vcpu, &msr) != 0) {
6328 trace_kvm_msr_write_ex(ecx, data);
6329 kvm_inject_gp(vcpu, 0);
6333 trace_kvm_msr_write(ecx, data);
6334 return kvm_skip_emulated_instruction(vcpu);
6337 static int handle_tpr_below_threshold(struct kvm_vcpu *vcpu)
6339 kvm_apic_update_ppr(vcpu);
6343 static int handle_interrupt_window(struct kvm_vcpu *vcpu)
6345 vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL,
6346 CPU_BASED_VIRTUAL_INTR_PENDING);
6348 kvm_make_request(KVM_REQ_EVENT, vcpu);
6350 ++vcpu->stat.irq_window_exits;
6354 static int handle_halt(struct kvm_vcpu *vcpu)
6356 return kvm_emulate_halt(vcpu);
6359 static int handle_vmcall(struct kvm_vcpu *vcpu)
6361 return kvm_emulate_hypercall(vcpu);
6364 static int handle_invd(struct kvm_vcpu *vcpu)
6366 return emulate_instruction(vcpu, 0) == EMULATE_DONE;
6369 static int handle_invlpg(struct kvm_vcpu *vcpu)
6371 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
6373 kvm_mmu_invlpg(vcpu, exit_qualification);
6374 return kvm_skip_emulated_instruction(vcpu);
6377 static int handle_rdpmc(struct kvm_vcpu *vcpu)
6381 err = kvm_rdpmc(vcpu);
6382 return kvm_complete_insn_gp(vcpu, err);
6385 static int handle_wbinvd(struct kvm_vcpu *vcpu)
6387 return kvm_emulate_wbinvd(vcpu);
6390 static int handle_xsetbv(struct kvm_vcpu *vcpu)
6392 u64 new_bv = kvm_read_edx_eax(vcpu);
6393 u32 index = kvm_register_read(vcpu, VCPU_REGS_RCX);
6395 if (kvm_set_xcr(vcpu, index, new_bv) == 0)
6396 return kvm_skip_emulated_instruction(vcpu);
6400 static int handle_xsaves(struct kvm_vcpu *vcpu)
6402 kvm_skip_emulated_instruction(vcpu);
6403 WARN(1, "this should never happen\n");
6407 static int handle_xrstors(struct kvm_vcpu *vcpu)
6409 kvm_skip_emulated_instruction(vcpu);
6410 WARN(1, "this should never happen\n");
6414 static int handle_apic_access(struct kvm_vcpu *vcpu)
6416 if (likely(fasteoi)) {
6417 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
6418 int access_type, offset;
6420 access_type = exit_qualification & APIC_ACCESS_TYPE;
6421 offset = exit_qualification & APIC_ACCESS_OFFSET;
6423 * Sane guest uses MOV to write EOI, with written value
6424 * not cared. So make a short-circuit here by avoiding
6425 * heavy instruction emulation.
6427 if ((access_type == TYPE_LINEAR_APIC_INST_WRITE) &&
6428 (offset == APIC_EOI)) {
6429 kvm_lapic_set_eoi(vcpu);
6430 return kvm_skip_emulated_instruction(vcpu);
6433 return emulate_instruction(vcpu, 0) == EMULATE_DONE;
6436 static int handle_apic_eoi_induced(struct kvm_vcpu *vcpu)
6438 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
6439 int vector = exit_qualification & 0xff;
6441 /* EOI-induced VM exit is trap-like and thus no need to adjust IP */
6442 kvm_apic_set_eoi_accelerated(vcpu, vector);
6446 static int handle_apic_write(struct kvm_vcpu *vcpu)
6448 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
6449 u32 offset = exit_qualification & 0xfff;
6451 /* APIC-write VM exit is trap-like and thus no need to adjust IP */
6452 kvm_apic_write_nodecode(vcpu, offset);
6456 static int handle_task_switch(struct kvm_vcpu *vcpu)
6458 struct vcpu_vmx *vmx = to_vmx(vcpu);
6459 unsigned long exit_qualification;
6460 bool has_error_code = false;
6463 int reason, type, idt_v, idt_index;
6465 idt_v = (vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK);
6466 idt_index = (vmx->idt_vectoring_info & VECTORING_INFO_VECTOR_MASK);
6467 type = (vmx->idt_vectoring_info & VECTORING_INFO_TYPE_MASK);
6469 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
6471 reason = (u32)exit_qualification >> 30;
6472 if (reason == TASK_SWITCH_GATE && idt_v) {
6474 case INTR_TYPE_NMI_INTR:
6475 vcpu->arch.nmi_injected = false;
6476 vmx_set_nmi_mask(vcpu, true);
6478 case INTR_TYPE_EXT_INTR:
6479 case INTR_TYPE_SOFT_INTR:
6480 kvm_clear_interrupt_queue(vcpu);
6482 case INTR_TYPE_HARD_EXCEPTION:
6483 if (vmx->idt_vectoring_info &
6484 VECTORING_INFO_DELIVER_CODE_MASK) {
6485 has_error_code = true;
6487 vmcs_read32(IDT_VECTORING_ERROR_CODE);
6490 case INTR_TYPE_SOFT_EXCEPTION:
6491 kvm_clear_exception_queue(vcpu);
6497 tss_selector = exit_qualification;
6499 if (!idt_v || (type != INTR_TYPE_HARD_EXCEPTION &&
6500 type != INTR_TYPE_EXT_INTR &&
6501 type != INTR_TYPE_NMI_INTR))
6502 skip_emulated_instruction(vcpu);
6504 if (kvm_task_switch(vcpu, tss_selector,
6505 type == INTR_TYPE_SOFT_INTR ? idt_index : -1, reason,
6506 has_error_code, error_code) == EMULATE_FAIL) {
6507 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
6508 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
6509 vcpu->run->internal.ndata = 0;
6514 * TODO: What about debug traps on tss switch?
6515 * Are we supposed to inject them and update dr6?
6521 static int handle_ept_violation(struct kvm_vcpu *vcpu)
6523 unsigned long exit_qualification;
6527 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
6530 * EPT violation happened while executing iret from NMI,
6531 * "blocked by NMI" bit has to be set before next VM entry.
6532 * There are errata that may cause this bit to not be set:
6535 if (!(to_vmx(vcpu)->idt_vectoring_info & VECTORING_INFO_VALID_MASK) &&
6537 (exit_qualification & INTR_INFO_UNBLOCK_NMI))
6538 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO, GUEST_INTR_STATE_NMI);
6540 gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS);
6541 trace_kvm_page_fault(gpa, exit_qualification);
6543 /* Is it a read fault? */
6544 error_code = (exit_qualification & EPT_VIOLATION_ACC_READ)
6545 ? PFERR_USER_MASK : 0;
6546 /* Is it a write fault? */
6547 error_code |= (exit_qualification & EPT_VIOLATION_ACC_WRITE)
6548 ? PFERR_WRITE_MASK : 0;
6549 /* Is it a fetch fault? */
6550 error_code |= (exit_qualification & EPT_VIOLATION_ACC_INSTR)
6551 ? PFERR_FETCH_MASK : 0;
6552 /* ept page table entry is present? */
6553 error_code |= (exit_qualification &
6554 (EPT_VIOLATION_READABLE | EPT_VIOLATION_WRITABLE |
6555 EPT_VIOLATION_EXECUTABLE))
6556 ? PFERR_PRESENT_MASK : 0;
6558 error_code |= (exit_qualification & 0x100) != 0 ?
6559 PFERR_GUEST_FINAL_MASK : PFERR_GUEST_PAGE_MASK;
6561 vcpu->arch.exit_qualification = exit_qualification;
6562 return kvm_mmu_page_fault(vcpu, gpa, error_code, NULL, 0);
6565 static int handle_ept_misconfig(struct kvm_vcpu *vcpu)
6571 * A nested guest cannot optimize MMIO vmexits, because we have an
6572 * nGPA here instead of the required GPA.
6574 gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS);
6575 if (!is_guest_mode(vcpu) &&
6576 !kvm_io_bus_write(vcpu, KVM_FAST_MMIO_BUS, gpa, 0, NULL)) {
6577 trace_kvm_fast_mmio(gpa);
6579 * Doing kvm_skip_emulated_instruction() depends on undefined
6580 * behavior: Intel's manual doesn't mandate
6581 * VM_EXIT_INSTRUCTION_LEN to be set in VMCS when EPT MISCONFIG
6582 * occurs and while on real hardware it was observed to be set,
6583 * other hypervisors (namely Hyper-V) don't set it, we end up
6584 * advancing IP with some random value. Disable fast mmio when
6585 * running nested and keep it for real hardware in hope that
6586 * VM_EXIT_INSTRUCTION_LEN will always be set correctly.
6588 if (!static_cpu_has(X86_FEATURE_HYPERVISOR))
6589 return kvm_skip_emulated_instruction(vcpu);
6591 return x86_emulate_instruction(vcpu, gpa, EMULTYPE_SKIP,
6592 NULL, 0) == EMULATE_DONE;
6595 ret = kvm_mmu_page_fault(vcpu, gpa, PFERR_RSVD_MASK, NULL, 0);
6599 /* It is the real ept misconfig */
6602 vcpu->run->exit_reason = KVM_EXIT_UNKNOWN;
6603 vcpu->run->hw.hardware_exit_reason = EXIT_REASON_EPT_MISCONFIG;
6608 static int handle_nmi_window(struct kvm_vcpu *vcpu)
6610 WARN_ON_ONCE(!enable_vnmi);
6611 vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL,
6612 CPU_BASED_VIRTUAL_NMI_PENDING);
6613 ++vcpu->stat.nmi_window_exits;
6614 kvm_make_request(KVM_REQ_EVENT, vcpu);
6619 static int handle_invalid_guest_state(struct kvm_vcpu *vcpu)
6621 struct vcpu_vmx *vmx = to_vmx(vcpu);
6622 enum emulation_result err = EMULATE_DONE;
6625 bool intr_window_requested;
6626 unsigned count = 130;
6628 cpu_exec_ctrl = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
6629 intr_window_requested = cpu_exec_ctrl & CPU_BASED_VIRTUAL_INTR_PENDING;
6631 while (vmx->emulation_required && count-- != 0) {
6632 if (intr_window_requested && vmx_interrupt_allowed(vcpu))
6633 return handle_interrupt_window(&vmx->vcpu);
6635 if (kvm_test_request(KVM_REQ_EVENT, vcpu))
6638 err = emulate_instruction(vcpu, 0);
6640 if (err == EMULATE_USER_EXIT) {
6641 ++vcpu->stat.mmio_exits;
6646 if (err != EMULATE_DONE) {
6647 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
6648 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
6649 vcpu->run->internal.ndata = 0;
6653 if (vcpu->arch.halt_request) {
6654 vcpu->arch.halt_request = 0;
6655 ret = kvm_vcpu_halt(vcpu);
6659 if (signal_pending(current))
6669 static int __grow_ple_window(int val)
6671 if (ple_window_grow < 1)
6674 val = min(val, ple_window_actual_max);
6676 if (ple_window_grow < ple_window)
6677 val *= ple_window_grow;
6679 val += ple_window_grow;
6684 static int __shrink_ple_window(int val, int modifier, int minimum)
6689 if (modifier < ple_window)
6694 return max(val, minimum);
6697 static void grow_ple_window(struct kvm_vcpu *vcpu)
6699 struct vcpu_vmx *vmx = to_vmx(vcpu);
6700 int old = vmx->ple_window;
6702 vmx->ple_window = __grow_ple_window(old);
6704 if (vmx->ple_window != old)
6705 vmx->ple_window_dirty = true;
6707 trace_kvm_ple_window_grow(vcpu->vcpu_id, vmx->ple_window, old);
6710 static void shrink_ple_window(struct kvm_vcpu *vcpu)
6712 struct vcpu_vmx *vmx = to_vmx(vcpu);
6713 int old = vmx->ple_window;
6715 vmx->ple_window = __shrink_ple_window(old,
6716 ple_window_shrink, ple_window);
6718 if (vmx->ple_window != old)
6719 vmx->ple_window_dirty = true;
6721 trace_kvm_ple_window_shrink(vcpu->vcpu_id, vmx->ple_window, old);
6725 * ple_window_actual_max is computed to be one grow_ple_window() below
6726 * ple_window_max. (See __grow_ple_window for the reason.)
6727 * This prevents overflows, because ple_window_max is int.
6728 * ple_window_max effectively rounded down to a multiple of ple_window_grow in
6730 * ple_window_max is also prevented from setting vmx->ple_window < ple_window.
6732 static void update_ple_window_actual_max(void)
6734 ple_window_actual_max =
6735 __shrink_ple_window(max(ple_window_max, ple_window),
6736 ple_window_grow, INT_MIN);
6740 * Handler for POSTED_INTERRUPT_WAKEUP_VECTOR.
6742 static void wakeup_handler(void)
6744 struct kvm_vcpu *vcpu;
6745 int cpu = smp_processor_id();
6747 spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, cpu));
6748 list_for_each_entry(vcpu, &per_cpu(blocked_vcpu_on_cpu, cpu),
6749 blocked_vcpu_list) {
6750 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
6752 if (pi_test_on(pi_desc) == 1)
6753 kvm_vcpu_kick(vcpu);
6755 spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, cpu));
6758 void vmx_enable_tdp(void)
6760 kvm_mmu_set_mask_ptes(VMX_EPT_READABLE_MASK,
6761 enable_ept_ad_bits ? VMX_EPT_ACCESS_BIT : 0ull,
6762 enable_ept_ad_bits ? VMX_EPT_DIRTY_BIT : 0ull,
6763 0ull, VMX_EPT_EXECUTABLE_MASK,
6764 cpu_has_vmx_ept_execute_only() ? 0ull : VMX_EPT_READABLE_MASK,
6765 VMX_EPT_RWX_MASK, 0ull);
6767 ept_set_mmio_spte_mask();
6771 static __init int hardware_setup(void)
6773 int r = -ENOMEM, i, msr;
6775 rdmsrl_safe(MSR_EFER, &host_efer);
6777 for (i = 0; i < ARRAY_SIZE(vmx_msr_index); ++i)
6778 kvm_define_shared_msr(i, vmx_msr_index[i]);
6780 for (i = 0; i < VMX_BITMAP_NR; i++) {
6781 vmx_bitmap[i] = (unsigned long *)__get_free_page(GFP_KERNEL);
6786 memset(vmx_vmread_bitmap, 0xff, PAGE_SIZE);
6787 memset(vmx_vmwrite_bitmap, 0xff, PAGE_SIZE);
6789 memset(vmx_msr_bitmap_legacy, 0xff, PAGE_SIZE);
6790 memset(vmx_msr_bitmap_longmode, 0xff, PAGE_SIZE);
6792 if (setup_vmcs_config(&vmcs_config) < 0) {
6797 if (boot_cpu_has(X86_FEATURE_NX))
6798 kvm_enable_efer_bits(EFER_NX);
6800 if (!cpu_has_vmx_vpid() || !cpu_has_vmx_invvpid() ||
6801 !(cpu_has_vmx_invvpid_single() || cpu_has_vmx_invvpid_global()))
6804 if (!cpu_has_vmx_ept() ||
6805 !cpu_has_vmx_ept_4levels() ||
6806 !cpu_has_vmx_ept_mt_wb() ||
6807 !cpu_has_vmx_invept_global())
6810 if (!cpu_has_vmx_ept_ad_bits() || !enable_ept)
6811 enable_ept_ad_bits = 0;
6813 if (!cpu_has_vmx_unrestricted_guest() || !enable_ept)
6814 enable_unrestricted_guest = 0;
6816 if (!cpu_has_vmx_flexpriority())
6817 flexpriority_enabled = 0;
6819 if (!cpu_has_virtual_nmis())
6823 * set_apic_access_page_addr() is used to reload apic access
6824 * page upon invalidation. No need to do anything if not
6825 * using the APIC_ACCESS_ADDR VMCS field.
6827 if (!flexpriority_enabled)
6828 kvm_x86_ops->set_apic_access_page_addr = NULL;
6830 if (!cpu_has_vmx_tpr_shadow())
6831 kvm_x86_ops->update_cr8_intercept = NULL;
6833 if (enable_ept && !cpu_has_vmx_ept_2m_page())
6834 kvm_disable_largepages();
6836 if (!cpu_has_vmx_ple()) {
6839 ple_window_grow = 0;
6841 ple_window_shrink = 0;
6844 if (!cpu_has_vmx_apicv()) {
6846 kvm_x86_ops->sync_pir_to_irr = NULL;
6849 if (cpu_has_vmx_tsc_scaling()) {
6850 kvm_has_tsc_control = true;
6851 kvm_max_tsc_scaling_ratio = KVM_VMX_TSC_MULTIPLIER_MAX;
6852 kvm_tsc_scaling_ratio_frac_bits = 48;
6855 vmx_disable_intercept_for_msr(MSR_FS_BASE, false);
6856 vmx_disable_intercept_for_msr(MSR_GS_BASE, false);
6857 vmx_disable_intercept_for_msr(MSR_KERNEL_GS_BASE, true);
6858 vmx_disable_intercept_for_msr(MSR_IA32_SYSENTER_CS, false);
6859 vmx_disable_intercept_for_msr(MSR_IA32_SYSENTER_ESP, false);
6860 vmx_disable_intercept_for_msr(MSR_IA32_SYSENTER_EIP, false);
6862 memcpy(vmx_msr_bitmap_legacy_x2apic_apicv,
6863 vmx_msr_bitmap_legacy, PAGE_SIZE);
6864 memcpy(vmx_msr_bitmap_longmode_x2apic_apicv,
6865 vmx_msr_bitmap_longmode, PAGE_SIZE);
6866 memcpy(vmx_msr_bitmap_legacy_x2apic,
6867 vmx_msr_bitmap_legacy, PAGE_SIZE);
6868 memcpy(vmx_msr_bitmap_longmode_x2apic,
6869 vmx_msr_bitmap_longmode, PAGE_SIZE);
6871 set_bit(0, vmx_vpid_bitmap); /* 0 is reserved for host */
6873 for (msr = 0x800; msr <= 0x8ff; msr++) {
6874 if (msr == X2APIC_MSR(APIC_TMCCT))
6876 vmx_disable_intercept_msr_x2apic(msr, MSR_TYPE_R, true);
6880 * TPR reads and writes can be virtualized even if virtual interrupt
6881 * delivery is not in use.
6883 vmx_disable_intercept_msr_x2apic(X2APIC_MSR(APIC_TASKPRI), MSR_TYPE_R | MSR_TYPE_W, false);
6884 vmx_disable_intercept_msr_x2apic(X2APIC_MSR(APIC_EOI), MSR_TYPE_W, true);
6885 vmx_disable_intercept_msr_x2apic(X2APIC_MSR(APIC_SELF_IPI), MSR_TYPE_W, true);
6892 update_ple_window_actual_max();
6895 * Only enable PML when hardware supports PML feature, and both EPT
6896 * and EPT A/D bit features are enabled -- PML depends on them to work.
6898 if (!enable_ept || !enable_ept_ad_bits || !cpu_has_vmx_pml())
6902 kvm_x86_ops->slot_enable_log_dirty = NULL;
6903 kvm_x86_ops->slot_disable_log_dirty = NULL;
6904 kvm_x86_ops->flush_log_dirty = NULL;
6905 kvm_x86_ops->enable_log_dirty_pt_masked = NULL;
6908 if (cpu_has_vmx_preemption_timer() && enable_preemption_timer) {
6911 rdmsrl(MSR_IA32_VMX_MISC, vmx_msr);
6912 cpu_preemption_timer_multi =
6913 vmx_msr & VMX_MISC_PREEMPTION_TIMER_RATE_MASK;
6915 kvm_x86_ops->set_hv_timer = NULL;
6916 kvm_x86_ops->cancel_hv_timer = NULL;
6919 if (!cpu_has_vmx_shadow_vmcs())
6920 enable_shadow_vmcs = 0;
6921 if (enable_shadow_vmcs)
6922 init_vmcs_shadow_fields();
6924 kvm_set_posted_intr_wakeup_handler(wakeup_handler);
6926 kvm_mce_cap_supported |= MCG_LMCE_P;
6928 return alloc_kvm_area();
6931 for (i = 0; i < VMX_BITMAP_NR; i++)
6932 free_page((unsigned long)vmx_bitmap[i]);
6937 static __exit void hardware_unsetup(void)
6941 for (i = 0; i < VMX_BITMAP_NR; i++)
6942 free_page((unsigned long)vmx_bitmap[i]);
6948 * Indicate a busy-waiting vcpu in spinlock. We do not enable the PAUSE
6949 * exiting, so only get here on cpu with PAUSE-Loop-Exiting.
6951 static int handle_pause(struct kvm_vcpu *vcpu)
6954 grow_ple_window(vcpu);
6957 * Intel sdm vol3 ch-25.1.3 says: The "PAUSE-loop exiting"
6958 * VM-execution control is ignored if CPL > 0. OTOH, KVM
6959 * never set PAUSE_EXITING and just set PLE if supported,
6960 * so the vcpu must be CPL=0 if it gets a PAUSE exit.
6962 kvm_vcpu_on_spin(vcpu, true);
6963 return kvm_skip_emulated_instruction(vcpu);
6966 static int handle_nop(struct kvm_vcpu *vcpu)
6968 return kvm_skip_emulated_instruction(vcpu);
6971 static int handle_mwait(struct kvm_vcpu *vcpu)
6973 printk_once(KERN_WARNING "kvm: MWAIT instruction emulated as NOP!\n");
6974 return handle_nop(vcpu);
6977 static int handle_invalid_op(struct kvm_vcpu *vcpu)
6979 kvm_queue_exception(vcpu, UD_VECTOR);
6983 static int handle_monitor_trap(struct kvm_vcpu *vcpu)
6988 static int handle_monitor(struct kvm_vcpu *vcpu)
6990 printk_once(KERN_WARNING "kvm: MONITOR instruction emulated as NOP!\n");
6991 return handle_nop(vcpu);
6995 * The following 3 functions, nested_vmx_succeed()/failValid()/failInvalid(),
6996 * set the success or error code of an emulated VMX instruction, as specified
6997 * by Vol 2B, VMX Instruction Reference, "Conventions".
6999 static void nested_vmx_succeed(struct kvm_vcpu *vcpu)
7001 vmx_set_rflags(vcpu, vmx_get_rflags(vcpu)
7002 & ~(X86_EFLAGS_CF | X86_EFLAGS_PF | X86_EFLAGS_AF |
7003 X86_EFLAGS_ZF | X86_EFLAGS_SF | X86_EFLAGS_OF));
7006 static void nested_vmx_failInvalid(struct kvm_vcpu *vcpu)
7008 vmx_set_rflags(vcpu, (vmx_get_rflags(vcpu)
7009 & ~(X86_EFLAGS_PF | X86_EFLAGS_AF | X86_EFLAGS_ZF |
7010 X86_EFLAGS_SF | X86_EFLAGS_OF))
7014 static void nested_vmx_failValid(struct kvm_vcpu *vcpu,
7015 u32 vm_instruction_error)
7017 if (to_vmx(vcpu)->nested.current_vmptr == -1ull) {
7019 * failValid writes the error number to the current VMCS, which
7020 * can't be done there isn't a current VMCS.
7022 nested_vmx_failInvalid(vcpu);
7025 vmx_set_rflags(vcpu, (vmx_get_rflags(vcpu)
7026 & ~(X86_EFLAGS_CF | X86_EFLAGS_PF | X86_EFLAGS_AF |
7027 X86_EFLAGS_SF | X86_EFLAGS_OF))
7029 get_vmcs12(vcpu)->vm_instruction_error = vm_instruction_error;
7031 * We don't need to force a shadow sync because
7032 * VM_INSTRUCTION_ERROR is not shadowed
7036 static void nested_vmx_abort(struct kvm_vcpu *vcpu, u32 indicator)
7038 /* TODO: not to reset guest simply here. */
7039 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
7040 pr_debug_ratelimited("kvm: nested vmx abort, indicator %d\n", indicator);
7043 static enum hrtimer_restart vmx_preemption_timer_fn(struct hrtimer *timer)
7045 struct vcpu_vmx *vmx =
7046 container_of(timer, struct vcpu_vmx, nested.preemption_timer);
7048 vmx->nested.preemption_timer_expired = true;
7049 kvm_make_request(KVM_REQ_EVENT, &vmx->vcpu);
7050 kvm_vcpu_kick(&vmx->vcpu);
7052 return HRTIMER_NORESTART;
7056 * Decode the memory-address operand of a vmx instruction, as recorded on an
7057 * exit caused by such an instruction (run by a guest hypervisor).
7058 * On success, returns 0. When the operand is invalid, returns 1 and throws
7061 static int get_vmx_mem_address(struct kvm_vcpu *vcpu,
7062 unsigned long exit_qualification,
7063 u32 vmx_instruction_info, bool wr, gva_t *ret)
7067 struct kvm_segment s;
7070 * According to Vol. 3B, "Information for VM Exits Due to Instruction
7071 * Execution", on an exit, vmx_instruction_info holds most of the
7072 * addressing components of the operand. Only the displacement part
7073 * is put in exit_qualification (see 3B, "Basic VM-Exit Information").
7074 * For how an actual address is calculated from all these components,
7075 * refer to Vol. 1, "Operand Addressing".
7077 int scaling = vmx_instruction_info & 3;
7078 int addr_size = (vmx_instruction_info >> 7) & 7;
7079 bool is_reg = vmx_instruction_info & (1u << 10);
7080 int seg_reg = (vmx_instruction_info >> 15) & 7;
7081 int index_reg = (vmx_instruction_info >> 18) & 0xf;
7082 bool index_is_valid = !(vmx_instruction_info & (1u << 22));
7083 int base_reg = (vmx_instruction_info >> 23) & 0xf;
7084 bool base_is_valid = !(vmx_instruction_info & (1u << 27));
7087 kvm_queue_exception(vcpu, UD_VECTOR);
7091 /* Addr = segment_base + offset */
7092 /* offset = base + [index * scale] + displacement */
7093 off = exit_qualification; /* holds the displacement */
7095 off += kvm_register_read(vcpu, base_reg);
7097 off += kvm_register_read(vcpu, index_reg)<<scaling;
7098 vmx_get_segment(vcpu, &s, seg_reg);
7099 *ret = s.base + off;
7101 if (addr_size == 1) /* 32 bit */
7104 /* Checks for #GP/#SS exceptions. */
7106 if (is_long_mode(vcpu)) {
7107 /* Long mode: #GP(0)/#SS(0) if the memory address is in a
7108 * non-canonical form. This is the only check on the memory
7109 * destination for long mode!
7111 exn = is_noncanonical_address(*ret, vcpu);
7112 } else if (is_protmode(vcpu)) {
7113 /* Protected mode: apply checks for segment validity in the
7115 * - segment type check (#GP(0) may be thrown)
7116 * - usability check (#GP(0)/#SS(0))
7117 * - limit check (#GP(0)/#SS(0))
7120 /* #GP(0) if the destination operand is located in a
7121 * read-only data segment or any code segment.
7123 exn = ((s.type & 0xa) == 0 || (s.type & 8));
7125 /* #GP(0) if the source operand is located in an
7126 * execute-only code segment
7128 exn = ((s.type & 0xa) == 8);
7130 kvm_queue_exception_e(vcpu, GP_VECTOR, 0);
7133 /* Protected mode: #GP(0)/#SS(0) if the segment is unusable.
7135 exn = (s.unusable != 0);
7136 /* Protected mode: #GP(0)/#SS(0) if the memory
7137 * operand is outside the segment limit.
7139 exn = exn || (off + sizeof(u64) > s.limit);
7142 kvm_queue_exception_e(vcpu,
7143 seg_reg == VCPU_SREG_SS ?
7144 SS_VECTOR : GP_VECTOR,
7152 static int nested_vmx_get_vmptr(struct kvm_vcpu *vcpu, gpa_t *vmpointer)
7155 struct x86_exception e;
7157 if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
7158 vmcs_read32(VMX_INSTRUCTION_INFO), false, &gva))
7161 if (kvm_read_guest_virt(&vcpu->arch.emulate_ctxt, gva, vmpointer,
7162 sizeof(*vmpointer), &e)) {
7163 kvm_inject_page_fault(vcpu, &e);
7170 static int enter_vmx_operation(struct kvm_vcpu *vcpu)
7172 struct vcpu_vmx *vmx = to_vmx(vcpu);
7173 struct vmcs *shadow_vmcs;
7175 vmx->nested.vmcs02.vmcs = alloc_vmcs();
7176 vmx->nested.vmcs02.shadow_vmcs = NULL;
7177 if (!vmx->nested.vmcs02.vmcs)
7179 loaded_vmcs_init(&vmx->nested.vmcs02);
7181 if (cpu_has_vmx_msr_bitmap()) {
7182 vmx->nested.msr_bitmap =
7183 (unsigned long *)__get_free_page(GFP_KERNEL);
7184 if (!vmx->nested.msr_bitmap)
7185 goto out_msr_bitmap;
7186 memset(vmx->nested.msr_bitmap, 0xff, PAGE_SIZE);
7189 vmx->nested.cached_vmcs12 = kmalloc(VMCS12_SIZE, GFP_KERNEL);
7190 if (!vmx->nested.cached_vmcs12)
7191 goto out_cached_vmcs12;
7193 if (enable_shadow_vmcs) {
7194 shadow_vmcs = alloc_vmcs();
7196 goto out_shadow_vmcs;
7197 /* mark vmcs as shadow */
7198 shadow_vmcs->revision_id |= (1u << 31);
7199 /* init shadow vmcs */
7200 vmcs_clear(shadow_vmcs);
7201 vmx->vmcs01.shadow_vmcs = shadow_vmcs;
7204 hrtimer_init(&vmx->nested.preemption_timer, CLOCK_MONOTONIC,
7205 HRTIMER_MODE_REL_PINNED);
7206 vmx->nested.preemption_timer.function = vmx_preemption_timer_fn;
7208 vmx->nested.vmxon = true;
7212 kfree(vmx->nested.cached_vmcs12);
7215 free_page((unsigned long)vmx->nested.msr_bitmap);
7218 vmx_nested_free_vmcs02(vmx);
7225 * Emulate the VMXON instruction.
7226 * Currently, we just remember that VMX is active, and do not save or even
7227 * inspect the argument to VMXON (the so-called "VMXON pointer") because we
7228 * do not currently need to store anything in that guest-allocated memory
7229 * region. Consequently, VMCLEAR and VMPTRLD also do not verify that the their
7230 * argument is different from the VMXON pointer (which the spec says they do).
7232 static int handle_vmon(struct kvm_vcpu *vcpu)
7237 struct vcpu_vmx *vmx = to_vmx(vcpu);
7238 const u64 VMXON_NEEDED_FEATURES = FEATURE_CONTROL_LOCKED
7239 | FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
7242 * The Intel VMX Instruction Reference lists a bunch of bits that are
7243 * prerequisite to running VMXON, most notably cr4.VMXE must be set to
7244 * 1 (see vmx_set_cr4() for when we allow the guest to set this).
7245 * Otherwise, we should fail with #UD. But most faulting conditions
7246 * have already been checked by hardware, prior to the VM-exit for
7247 * VMXON. We do test guest cr4.VMXE because processor CR4 always has
7248 * that bit set to 1 in non-root mode.
7250 if (!kvm_read_cr4_bits(vcpu, X86_CR4_VMXE)) {
7251 kvm_queue_exception(vcpu, UD_VECTOR);
7255 if (vmx->nested.vmxon) {
7256 nested_vmx_failValid(vcpu, VMXERR_VMXON_IN_VMX_ROOT_OPERATION);
7257 return kvm_skip_emulated_instruction(vcpu);
7260 if ((vmx->msr_ia32_feature_control & VMXON_NEEDED_FEATURES)
7261 != VMXON_NEEDED_FEATURES) {
7262 kvm_inject_gp(vcpu, 0);
7266 if (nested_vmx_get_vmptr(vcpu, &vmptr))
7271 * The first 4 bytes of VMXON region contain the supported
7272 * VMCS revision identifier
7274 * Note - IA32_VMX_BASIC[48] will never be 1 for the nested case;
7275 * which replaces physical address width with 32
7277 if (!PAGE_ALIGNED(vmptr) || (vmptr >> cpuid_maxphyaddr(vcpu))) {
7278 nested_vmx_failInvalid(vcpu);
7279 return kvm_skip_emulated_instruction(vcpu);
7282 page = kvm_vcpu_gpa_to_page(vcpu, vmptr);
7283 if (is_error_page(page)) {
7284 nested_vmx_failInvalid(vcpu);
7285 return kvm_skip_emulated_instruction(vcpu);
7287 if (*(u32 *)kmap(page) != VMCS12_REVISION) {
7289 kvm_release_page_clean(page);
7290 nested_vmx_failInvalid(vcpu);
7291 return kvm_skip_emulated_instruction(vcpu);
7294 kvm_release_page_clean(page);
7296 vmx->nested.vmxon_ptr = vmptr;
7297 ret = enter_vmx_operation(vcpu);
7301 nested_vmx_succeed(vcpu);
7302 return kvm_skip_emulated_instruction(vcpu);
7306 * Intel's VMX Instruction Reference specifies a common set of prerequisites
7307 * for running VMX instructions (except VMXON, whose prerequisites are
7308 * slightly different). It also specifies what exception to inject otherwise.
7309 * Note that many of these exceptions have priority over VM exits, so they
7310 * don't have to be checked again here.
7312 static int nested_vmx_check_permission(struct kvm_vcpu *vcpu)
7314 if (!to_vmx(vcpu)->nested.vmxon) {
7315 kvm_queue_exception(vcpu, UD_VECTOR);
7321 static void vmx_disable_shadow_vmcs(struct vcpu_vmx *vmx)
7323 vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL, SECONDARY_EXEC_SHADOW_VMCS);
7324 vmcs_write64(VMCS_LINK_POINTER, -1ull);
7327 static inline void nested_release_vmcs12(struct vcpu_vmx *vmx)
7329 if (vmx->nested.current_vmptr == -1ull)
7332 if (enable_shadow_vmcs) {
7333 /* copy to memory all shadowed fields in case
7334 they were modified */
7335 copy_shadow_to_vmcs12(vmx);
7336 vmx->nested.sync_shadow_vmcs = false;
7337 vmx_disable_shadow_vmcs(vmx);
7339 vmx->nested.posted_intr_nv = -1;
7341 /* Flush VMCS12 to guest memory */
7342 kvm_vcpu_write_guest_page(&vmx->vcpu,
7343 vmx->nested.current_vmptr >> PAGE_SHIFT,
7344 vmx->nested.cached_vmcs12, 0, VMCS12_SIZE);
7346 vmx->nested.current_vmptr = -1ull;
7350 * Free whatever needs to be freed from vmx->nested when L1 goes down, or
7351 * just stops using VMX.
7353 static void free_nested(struct vcpu_vmx *vmx)
7355 if (!vmx->nested.vmxon && !vmx->nested.smm.vmxon)
7358 vmx->nested.vmxon = false;
7359 vmx->nested.smm.vmxon = false;
7360 free_vpid(vmx->nested.vpid02);
7361 vmx->nested.posted_intr_nv = -1;
7362 vmx->nested.current_vmptr = -1ull;
7363 if (vmx->nested.msr_bitmap) {
7364 free_page((unsigned long)vmx->nested.msr_bitmap);
7365 vmx->nested.msr_bitmap = NULL;
7367 if (enable_shadow_vmcs) {
7368 vmx_disable_shadow_vmcs(vmx);
7369 vmcs_clear(vmx->vmcs01.shadow_vmcs);
7370 free_vmcs(vmx->vmcs01.shadow_vmcs);
7371 vmx->vmcs01.shadow_vmcs = NULL;
7373 kfree(vmx->nested.cached_vmcs12);
7374 /* Unpin physical memory we referred to in the vmcs02 */
7375 if (vmx->nested.apic_access_page) {
7376 kvm_release_page_dirty(vmx->nested.apic_access_page);
7377 vmx->nested.apic_access_page = NULL;
7379 if (vmx->nested.virtual_apic_page) {
7380 kvm_release_page_dirty(vmx->nested.virtual_apic_page);
7381 vmx->nested.virtual_apic_page = NULL;
7383 if (vmx->nested.pi_desc_page) {
7384 kunmap(vmx->nested.pi_desc_page);
7385 kvm_release_page_dirty(vmx->nested.pi_desc_page);
7386 vmx->nested.pi_desc_page = NULL;
7387 vmx->nested.pi_desc = NULL;
7390 vmx_nested_free_vmcs02(vmx);
7393 /* Emulate the VMXOFF instruction */
7394 static int handle_vmoff(struct kvm_vcpu *vcpu)
7396 if (!nested_vmx_check_permission(vcpu))
7398 free_nested(to_vmx(vcpu));
7399 nested_vmx_succeed(vcpu);
7400 return kvm_skip_emulated_instruction(vcpu);
7403 /* Emulate the VMCLEAR instruction */
7404 static int handle_vmclear(struct kvm_vcpu *vcpu)
7406 struct vcpu_vmx *vmx = to_vmx(vcpu);
7410 if (!nested_vmx_check_permission(vcpu))
7413 if (nested_vmx_get_vmptr(vcpu, &vmptr))
7416 if (!PAGE_ALIGNED(vmptr) || (vmptr >> cpuid_maxphyaddr(vcpu))) {
7417 nested_vmx_failValid(vcpu, VMXERR_VMCLEAR_INVALID_ADDRESS);
7418 return kvm_skip_emulated_instruction(vcpu);
7421 if (vmptr == vmx->nested.vmxon_ptr) {
7422 nested_vmx_failValid(vcpu, VMXERR_VMCLEAR_VMXON_POINTER);
7423 return kvm_skip_emulated_instruction(vcpu);
7426 if (vmptr == vmx->nested.current_vmptr)
7427 nested_release_vmcs12(vmx);
7429 kvm_vcpu_write_guest(vcpu,
7430 vmptr + offsetof(struct vmcs12, launch_state),
7431 &zero, sizeof(zero));
7433 nested_vmx_succeed(vcpu);
7434 return kvm_skip_emulated_instruction(vcpu);
7437 static int nested_vmx_run(struct kvm_vcpu *vcpu, bool launch);
7439 /* Emulate the VMLAUNCH instruction */
7440 static int handle_vmlaunch(struct kvm_vcpu *vcpu)
7442 return nested_vmx_run(vcpu, true);
7445 /* Emulate the VMRESUME instruction */
7446 static int handle_vmresume(struct kvm_vcpu *vcpu)
7449 return nested_vmx_run(vcpu, false);
7453 * Read a vmcs12 field. Since these can have varying lengths and we return
7454 * one type, we chose the biggest type (u64) and zero-extend the return value
7455 * to that size. Note that the caller, handle_vmread, might need to use only
7456 * some of the bits we return here (e.g., on 32-bit guests, only 32 bits of
7457 * 64-bit fields are to be returned).
7459 static inline int vmcs12_read_any(struct kvm_vcpu *vcpu,
7460 unsigned long field, u64 *ret)
7462 short offset = vmcs_field_to_offset(field);
7468 p = ((char *)(get_vmcs12(vcpu))) + offset;
7470 switch (vmcs_field_width(field)) {
7471 case VMCS_FIELD_WIDTH_NATURAL_WIDTH:
7472 *ret = *((natural_width *)p);
7474 case VMCS_FIELD_WIDTH_U16:
7477 case VMCS_FIELD_WIDTH_U32:
7480 case VMCS_FIELD_WIDTH_U64:
7490 static inline int vmcs12_write_any(struct kvm_vcpu *vcpu,
7491 unsigned long field, u64 field_value){
7492 short offset = vmcs_field_to_offset(field);
7493 char *p = ((char *) get_vmcs12(vcpu)) + offset;
7497 switch (vmcs_field_width(field)) {
7498 case VMCS_FIELD_WIDTH_U16:
7499 *(u16 *)p = field_value;
7501 case VMCS_FIELD_WIDTH_U32:
7502 *(u32 *)p = field_value;
7504 case VMCS_FIELD_WIDTH_U64:
7505 *(u64 *)p = field_value;
7507 case VMCS_FIELD_WIDTH_NATURAL_WIDTH:
7508 *(natural_width *)p = field_value;
7517 static void copy_shadow_to_vmcs12(struct vcpu_vmx *vmx)
7520 unsigned long field;
7522 struct vmcs *shadow_vmcs = vmx->vmcs01.shadow_vmcs;
7523 const u16 *fields = shadow_read_write_fields;
7524 const int num_fields = max_shadow_read_write_fields;
7528 vmcs_load(shadow_vmcs);
7530 for (i = 0; i < num_fields; i++) {
7532 field_value = __vmcs_readl(field);
7533 vmcs12_write_any(&vmx->vcpu, field, field_value);
7536 vmcs_clear(shadow_vmcs);
7537 vmcs_load(vmx->loaded_vmcs->vmcs);
7542 static void copy_vmcs12_to_shadow(struct vcpu_vmx *vmx)
7544 const u16 *fields[] = {
7545 shadow_read_write_fields,
7546 shadow_read_only_fields
7548 const int max_fields[] = {
7549 max_shadow_read_write_fields,
7550 max_shadow_read_only_fields
7553 unsigned long field;
7554 u64 field_value = 0;
7555 struct vmcs *shadow_vmcs = vmx->vmcs01.shadow_vmcs;
7557 vmcs_load(shadow_vmcs);
7559 for (q = 0; q < ARRAY_SIZE(fields); q++) {
7560 for (i = 0; i < max_fields[q]; i++) {
7561 field = fields[q][i];
7562 vmcs12_read_any(&vmx->vcpu, field, &field_value);
7563 __vmcs_writel(field, field_value);
7567 vmcs_clear(shadow_vmcs);
7568 vmcs_load(vmx->loaded_vmcs->vmcs);
7572 * VMX instructions which assume a current vmcs12 (i.e., that VMPTRLD was
7573 * used before) all generate the same failure when it is missing.
7575 static int nested_vmx_check_vmcs12(struct kvm_vcpu *vcpu)
7577 struct vcpu_vmx *vmx = to_vmx(vcpu);
7578 if (vmx->nested.current_vmptr == -1ull) {
7579 nested_vmx_failInvalid(vcpu);
7585 static int handle_vmread(struct kvm_vcpu *vcpu)
7587 unsigned long field;
7589 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7590 u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
7593 if (!nested_vmx_check_permission(vcpu))
7596 if (!nested_vmx_check_vmcs12(vcpu))
7597 return kvm_skip_emulated_instruction(vcpu);
7599 /* Decode instruction info and find the field to read */
7600 field = kvm_register_readl(vcpu, (((vmx_instruction_info) >> 28) & 0xf));
7601 /* Read the field, zero-extended to a u64 field_value */
7602 if (vmcs12_read_any(vcpu, field, &field_value) < 0) {
7603 nested_vmx_failValid(vcpu, VMXERR_UNSUPPORTED_VMCS_COMPONENT);
7604 return kvm_skip_emulated_instruction(vcpu);
7607 * Now copy part of this value to register or memory, as requested.
7608 * Note that the number of bits actually copied is 32 or 64 depending
7609 * on the guest's mode (32 or 64 bit), not on the given field's length.
7611 if (vmx_instruction_info & (1u << 10)) {
7612 kvm_register_writel(vcpu, (((vmx_instruction_info) >> 3) & 0xf),
7615 if (get_vmx_mem_address(vcpu, exit_qualification,
7616 vmx_instruction_info, true, &gva))
7618 /* _system ok, as hardware has verified cpl=0 */
7619 kvm_write_guest_virt_system(&vcpu->arch.emulate_ctxt, gva,
7620 &field_value, (is_long_mode(vcpu) ? 8 : 4), NULL);
7623 nested_vmx_succeed(vcpu);
7624 return kvm_skip_emulated_instruction(vcpu);
7628 static int handle_vmwrite(struct kvm_vcpu *vcpu)
7630 unsigned long field;
7632 struct vcpu_vmx *vmx = to_vmx(vcpu);
7633 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7634 u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
7636 /* The value to write might be 32 or 64 bits, depending on L1's long
7637 * mode, and eventually we need to write that into a field of several
7638 * possible lengths. The code below first zero-extends the value to 64
7639 * bit (field_value), and then copies only the appropriate number of
7640 * bits into the vmcs12 field.
7642 u64 field_value = 0;
7643 struct x86_exception e;
7645 if (!nested_vmx_check_permission(vcpu))
7648 if (!nested_vmx_check_vmcs12(vcpu))
7649 return kvm_skip_emulated_instruction(vcpu);
7651 if (vmx_instruction_info & (1u << 10))
7652 field_value = kvm_register_readl(vcpu,
7653 (((vmx_instruction_info) >> 3) & 0xf));
7655 if (get_vmx_mem_address(vcpu, exit_qualification,
7656 vmx_instruction_info, false, &gva))
7658 if (kvm_read_guest_virt(&vcpu->arch.emulate_ctxt, gva,
7659 &field_value, (is_64_bit_mode(vcpu) ? 8 : 4), &e)) {
7660 kvm_inject_page_fault(vcpu, &e);
7666 field = kvm_register_readl(vcpu, (((vmx_instruction_info) >> 28) & 0xf));
7667 if (vmcs_field_readonly(field)) {
7668 nested_vmx_failValid(vcpu,
7669 VMXERR_VMWRITE_READ_ONLY_VMCS_COMPONENT);
7670 return kvm_skip_emulated_instruction(vcpu);
7673 if (vmcs12_write_any(vcpu, field, field_value) < 0) {
7674 nested_vmx_failValid(vcpu, VMXERR_UNSUPPORTED_VMCS_COMPONENT);
7675 return kvm_skip_emulated_instruction(vcpu);
7679 #define SHADOW_FIELD_RW(x) case x:
7680 #include "vmx_shadow_fields.h"
7682 * The fields that can be updated by L1 without a vmexit are
7683 * always updated in the vmcs02, the others go down the slow
7684 * path of prepare_vmcs02.
7688 vmx->nested.dirty_vmcs12 = true;
7692 nested_vmx_succeed(vcpu);
7693 return kvm_skip_emulated_instruction(vcpu);
7696 static void set_current_vmptr(struct vcpu_vmx *vmx, gpa_t vmptr)
7698 vmx->nested.current_vmptr = vmptr;
7699 if (enable_shadow_vmcs) {
7700 vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL,
7701 SECONDARY_EXEC_SHADOW_VMCS);
7702 vmcs_write64(VMCS_LINK_POINTER,
7703 __pa(vmx->vmcs01.shadow_vmcs));
7704 vmx->nested.sync_shadow_vmcs = true;
7706 vmx->nested.dirty_vmcs12 = true;
7709 /* Emulate the VMPTRLD instruction */
7710 static int handle_vmptrld(struct kvm_vcpu *vcpu)
7712 struct vcpu_vmx *vmx = to_vmx(vcpu);
7715 if (!nested_vmx_check_permission(vcpu))
7718 if (nested_vmx_get_vmptr(vcpu, &vmptr))
7721 if (!PAGE_ALIGNED(vmptr) || (vmptr >> cpuid_maxphyaddr(vcpu))) {
7722 nested_vmx_failValid(vcpu, VMXERR_VMPTRLD_INVALID_ADDRESS);
7723 return kvm_skip_emulated_instruction(vcpu);
7726 if (vmptr == vmx->nested.vmxon_ptr) {
7727 nested_vmx_failValid(vcpu, VMXERR_VMPTRLD_VMXON_POINTER);
7728 return kvm_skip_emulated_instruction(vcpu);
7731 if (vmx->nested.current_vmptr != vmptr) {
7732 struct vmcs12 *new_vmcs12;
7734 page = kvm_vcpu_gpa_to_page(vcpu, vmptr);
7735 if (is_error_page(page)) {
7736 nested_vmx_failInvalid(vcpu);
7737 return kvm_skip_emulated_instruction(vcpu);
7739 new_vmcs12 = kmap(page);
7740 if (new_vmcs12->revision_id != VMCS12_REVISION) {
7742 kvm_release_page_clean(page);
7743 nested_vmx_failValid(vcpu,
7744 VMXERR_VMPTRLD_INCORRECT_VMCS_REVISION_ID);
7745 return kvm_skip_emulated_instruction(vcpu);
7748 nested_release_vmcs12(vmx);
7750 * Load VMCS12 from guest memory since it is not already
7753 memcpy(vmx->nested.cached_vmcs12, new_vmcs12, VMCS12_SIZE);
7755 kvm_release_page_clean(page);
7757 set_current_vmptr(vmx, vmptr);
7760 nested_vmx_succeed(vcpu);
7761 return kvm_skip_emulated_instruction(vcpu);
7764 /* Emulate the VMPTRST instruction */
7765 static int handle_vmptrst(struct kvm_vcpu *vcpu)
7767 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7768 u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
7770 struct x86_exception e;
7772 if (!nested_vmx_check_permission(vcpu))
7775 if (get_vmx_mem_address(vcpu, exit_qualification,
7776 vmx_instruction_info, true, &vmcs_gva))
7778 /* ok to use *_system, as hardware has verified cpl=0 */
7779 if (kvm_write_guest_virt_system(&vcpu->arch.emulate_ctxt, vmcs_gva,
7780 (void *)&to_vmx(vcpu)->nested.current_vmptr,
7782 kvm_inject_page_fault(vcpu, &e);
7785 nested_vmx_succeed(vcpu);
7786 return kvm_skip_emulated_instruction(vcpu);
7789 /* Emulate the INVEPT instruction */
7790 static int handle_invept(struct kvm_vcpu *vcpu)
7792 struct vcpu_vmx *vmx = to_vmx(vcpu);
7793 u32 vmx_instruction_info, types;
7796 struct x86_exception e;
7801 if (!(vmx->nested.nested_vmx_secondary_ctls_high &
7802 SECONDARY_EXEC_ENABLE_EPT) ||
7803 !(vmx->nested.nested_vmx_ept_caps & VMX_EPT_INVEPT_BIT)) {
7804 kvm_queue_exception(vcpu, UD_VECTOR);
7808 if (!nested_vmx_check_permission(vcpu))
7811 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
7812 type = kvm_register_readl(vcpu, (vmx_instruction_info >> 28) & 0xf);
7814 types = (vmx->nested.nested_vmx_ept_caps >> VMX_EPT_EXTENT_SHIFT) & 6;
7816 if (type >= 32 || !(types & (1 << type))) {
7817 nested_vmx_failValid(vcpu,
7818 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
7819 return kvm_skip_emulated_instruction(vcpu);
7822 /* According to the Intel VMX instruction reference, the memory
7823 * operand is read even if it isn't needed (e.g., for type==global)
7825 if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
7826 vmx_instruction_info, false, &gva))
7828 if (kvm_read_guest_virt(&vcpu->arch.emulate_ctxt, gva, &operand,
7829 sizeof(operand), &e)) {
7830 kvm_inject_page_fault(vcpu, &e);
7835 case VMX_EPT_EXTENT_GLOBAL:
7837 * TODO: track mappings and invalidate
7838 * single context requests appropriately
7840 case VMX_EPT_EXTENT_CONTEXT:
7841 kvm_mmu_sync_roots(vcpu);
7842 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
7843 nested_vmx_succeed(vcpu);
7850 return kvm_skip_emulated_instruction(vcpu);
7853 static int handle_invvpid(struct kvm_vcpu *vcpu)
7855 struct vcpu_vmx *vmx = to_vmx(vcpu);
7856 u32 vmx_instruction_info;
7857 unsigned long type, types;
7859 struct x86_exception e;
7865 if (!(vmx->nested.nested_vmx_secondary_ctls_high &
7866 SECONDARY_EXEC_ENABLE_VPID) ||
7867 !(vmx->nested.nested_vmx_vpid_caps & VMX_VPID_INVVPID_BIT)) {
7868 kvm_queue_exception(vcpu, UD_VECTOR);
7872 if (!nested_vmx_check_permission(vcpu))
7875 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
7876 type = kvm_register_readl(vcpu, (vmx_instruction_info >> 28) & 0xf);
7878 types = (vmx->nested.nested_vmx_vpid_caps &
7879 VMX_VPID_EXTENT_SUPPORTED_MASK) >> 8;
7881 if (type >= 32 || !(types & (1 << type))) {
7882 nested_vmx_failValid(vcpu,
7883 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
7884 return kvm_skip_emulated_instruction(vcpu);
7887 /* according to the intel vmx instruction reference, the memory
7888 * operand is read even if it isn't needed (e.g., for type==global)
7890 if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
7891 vmx_instruction_info, false, &gva))
7893 if (kvm_read_guest_virt(&vcpu->arch.emulate_ctxt, gva, &operand,
7894 sizeof(operand), &e)) {
7895 kvm_inject_page_fault(vcpu, &e);
7898 if (operand.vpid >> 16) {
7899 nested_vmx_failValid(vcpu,
7900 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
7901 return kvm_skip_emulated_instruction(vcpu);
7905 case VMX_VPID_EXTENT_INDIVIDUAL_ADDR:
7906 if (is_noncanonical_address(operand.gla, vcpu)) {
7907 nested_vmx_failValid(vcpu,
7908 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
7909 return kvm_skip_emulated_instruction(vcpu);
7912 case VMX_VPID_EXTENT_SINGLE_CONTEXT:
7913 case VMX_VPID_EXTENT_SINGLE_NON_GLOBAL:
7914 if (!operand.vpid) {
7915 nested_vmx_failValid(vcpu,
7916 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
7917 return kvm_skip_emulated_instruction(vcpu);
7920 case VMX_VPID_EXTENT_ALL_CONTEXT:
7924 return kvm_skip_emulated_instruction(vcpu);
7927 __vmx_flush_tlb(vcpu, vmx->nested.vpid02, true);
7928 nested_vmx_succeed(vcpu);
7930 return kvm_skip_emulated_instruction(vcpu);
7933 static int handle_pml_full(struct kvm_vcpu *vcpu)
7935 unsigned long exit_qualification;
7937 trace_kvm_pml_full(vcpu->vcpu_id);
7939 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7942 * PML buffer FULL happened while executing iret from NMI,
7943 * "blocked by NMI" bit has to be set before next VM entry.
7945 if (!(to_vmx(vcpu)->idt_vectoring_info & VECTORING_INFO_VALID_MASK) &&
7947 (exit_qualification & INTR_INFO_UNBLOCK_NMI))
7948 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
7949 GUEST_INTR_STATE_NMI);
7952 * PML buffer already flushed at beginning of VMEXIT. Nothing to do
7953 * here.., and there's no userspace involvement needed for PML.
7958 static int handle_preemption_timer(struct kvm_vcpu *vcpu)
7960 kvm_lapic_expired_hv_timer(vcpu);
7964 static bool valid_ept_address(struct kvm_vcpu *vcpu, u64 address)
7966 struct vcpu_vmx *vmx = to_vmx(vcpu);
7967 int maxphyaddr = cpuid_maxphyaddr(vcpu);
7969 /* Check for memory type validity */
7970 switch (address & VMX_EPTP_MT_MASK) {
7971 case VMX_EPTP_MT_UC:
7972 if (!(vmx->nested.nested_vmx_ept_caps & VMX_EPTP_UC_BIT))
7975 case VMX_EPTP_MT_WB:
7976 if (!(vmx->nested.nested_vmx_ept_caps & VMX_EPTP_WB_BIT))
7983 /* only 4 levels page-walk length are valid */
7984 if ((address & VMX_EPTP_PWL_MASK) != VMX_EPTP_PWL_4)
7987 /* Reserved bits should not be set */
7988 if (address >> maxphyaddr || ((address >> 7) & 0x1f))
7991 /* AD, if set, should be supported */
7992 if (address & VMX_EPTP_AD_ENABLE_BIT) {
7993 if (!(vmx->nested.nested_vmx_ept_caps & VMX_EPT_AD_BIT))
8000 static int nested_vmx_eptp_switching(struct kvm_vcpu *vcpu,
8001 struct vmcs12 *vmcs12)
8003 u32 index = vcpu->arch.regs[VCPU_REGS_RCX];
8005 bool accessed_dirty;
8006 struct kvm_mmu *mmu = vcpu->arch.walk_mmu;
8008 if (!nested_cpu_has_eptp_switching(vmcs12) ||
8009 !nested_cpu_has_ept(vmcs12))
8012 if (index >= VMFUNC_EPTP_ENTRIES)
8016 if (kvm_vcpu_read_guest_page(vcpu, vmcs12->eptp_list_address >> PAGE_SHIFT,
8017 &address, index * 8, 8))
8020 accessed_dirty = !!(address & VMX_EPTP_AD_ENABLE_BIT);
8023 * If the (L2) guest does a vmfunc to the currently
8024 * active ept pointer, we don't have to do anything else
8026 if (vmcs12->ept_pointer != address) {
8027 if (!valid_ept_address(vcpu, address))
8030 kvm_mmu_unload(vcpu);
8031 mmu->ept_ad = accessed_dirty;
8032 mmu->base_role.ad_disabled = !accessed_dirty;
8033 vmcs12->ept_pointer = address;
8035 * TODO: Check what's the correct approach in case
8036 * mmu reload fails. Currently, we just let the next
8037 * reload potentially fail
8039 kvm_mmu_reload(vcpu);
8045 static int handle_vmfunc(struct kvm_vcpu *vcpu)
8047 struct vcpu_vmx *vmx = to_vmx(vcpu);
8048 struct vmcs12 *vmcs12;
8049 u32 function = vcpu->arch.regs[VCPU_REGS_RAX];
8052 * VMFUNC is only supported for nested guests, but we always enable the
8053 * secondary control for simplicity; for non-nested mode, fake that we
8054 * didn't by injecting #UD.
8056 if (!is_guest_mode(vcpu)) {
8057 kvm_queue_exception(vcpu, UD_VECTOR);
8061 vmcs12 = get_vmcs12(vcpu);
8062 if ((vmcs12->vm_function_control & (1 << function)) == 0)
8067 if (nested_vmx_eptp_switching(vcpu, vmcs12))
8073 return kvm_skip_emulated_instruction(vcpu);
8076 nested_vmx_vmexit(vcpu, vmx->exit_reason,
8077 vmcs_read32(VM_EXIT_INTR_INFO),
8078 vmcs_readl(EXIT_QUALIFICATION));
8083 * The exit handlers return 1 if the exit was handled fully and guest execution
8084 * may resume. Otherwise they set the kvm_run parameter to indicate what needs
8085 * to be done to userspace and return 0.
8087 static int (*const kvm_vmx_exit_handlers[])(struct kvm_vcpu *vcpu) = {
8088 [EXIT_REASON_EXCEPTION_NMI] = handle_exception,
8089 [EXIT_REASON_EXTERNAL_INTERRUPT] = handle_external_interrupt,
8090 [EXIT_REASON_TRIPLE_FAULT] = handle_triple_fault,
8091 [EXIT_REASON_NMI_WINDOW] = handle_nmi_window,
8092 [EXIT_REASON_IO_INSTRUCTION] = handle_io,
8093 [EXIT_REASON_CR_ACCESS] = handle_cr,
8094 [EXIT_REASON_DR_ACCESS] = handle_dr,
8095 [EXIT_REASON_CPUID] = handle_cpuid,
8096 [EXIT_REASON_MSR_READ] = handle_rdmsr,
8097 [EXIT_REASON_MSR_WRITE] = handle_wrmsr,
8098 [EXIT_REASON_PENDING_INTERRUPT] = handle_interrupt_window,
8099 [EXIT_REASON_HLT] = handle_halt,
8100 [EXIT_REASON_INVD] = handle_invd,
8101 [EXIT_REASON_INVLPG] = handle_invlpg,
8102 [EXIT_REASON_RDPMC] = handle_rdpmc,
8103 [EXIT_REASON_VMCALL] = handle_vmcall,
8104 [EXIT_REASON_VMCLEAR] = handle_vmclear,
8105 [EXIT_REASON_VMLAUNCH] = handle_vmlaunch,
8106 [EXIT_REASON_VMPTRLD] = handle_vmptrld,
8107 [EXIT_REASON_VMPTRST] = handle_vmptrst,
8108 [EXIT_REASON_VMREAD] = handle_vmread,
8109 [EXIT_REASON_VMRESUME] = handle_vmresume,
8110 [EXIT_REASON_VMWRITE] = handle_vmwrite,
8111 [EXIT_REASON_VMOFF] = handle_vmoff,
8112 [EXIT_REASON_VMON] = handle_vmon,
8113 [EXIT_REASON_TPR_BELOW_THRESHOLD] = handle_tpr_below_threshold,
8114 [EXIT_REASON_APIC_ACCESS] = handle_apic_access,
8115 [EXIT_REASON_APIC_WRITE] = handle_apic_write,
8116 [EXIT_REASON_EOI_INDUCED] = handle_apic_eoi_induced,
8117 [EXIT_REASON_WBINVD] = handle_wbinvd,
8118 [EXIT_REASON_XSETBV] = handle_xsetbv,
8119 [EXIT_REASON_TASK_SWITCH] = handle_task_switch,
8120 [EXIT_REASON_MCE_DURING_VMENTRY] = handle_machine_check,
8121 [EXIT_REASON_GDTR_IDTR] = handle_desc,
8122 [EXIT_REASON_LDTR_TR] = handle_desc,
8123 [EXIT_REASON_EPT_VIOLATION] = handle_ept_violation,
8124 [EXIT_REASON_EPT_MISCONFIG] = handle_ept_misconfig,
8125 [EXIT_REASON_PAUSE_INSTRUCTION] = handle_pause,
8126 [EXIT_REASON_MWAIT_INSTRUCTION] = handle_mwait,
8127 [EXIT_REASON_MONITOR_TRAP_FLAG] = handle_monitor_trap,
8128 [EXIT_REASON_MONITOR_INSTRUCTION] = handle_monitor,
8129 [EXIT_REASON_INVEPT] = handle_invept,
8130 [EXIT_REASON_INVVPID] = handle_invvpid,
8131 [EXIT_REASON_RDRAND] = handle_invalid_op,
8132 [EXIT_REASON_RDSEED] = handle_invalid_op,
8133 [EXIT_REASON_XSAVES] = handle_xsaves,
8134 [EXIT_REASON_XRSTORS] = handle_xrstors,
8135 [EXIT_REASON_PML_FULL] = handle_pml_full,
8136 [EXIT_REASON_VMFUNC] = handle_vmfunc,
8137 [EXIT_REASON_PREEMPTION_TIMER] = handle_preemption_timer,
8140 static const int kvm_vmx_max_exit_handlers =
8141 ARRAY_SIZE(kvm_vmx_exit_handlers);
8143 static bool nested_vmx_exit_handled_io(struct kvm_vcpu *vcpu,
8144 struct vmcs12 *vmcs12)
8146 unsigned long exit_qualification;
8147 gpa_t bitmap, last_bitmap;
8152 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_IO_BITMAPS))
8153 return nested_cpu_has(vmcs12, CPU_BASED_UNCOND_IO_EXITING);
8155 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
8157 port = exit_qualification >> 16;
8158 size = (exit_qualification & 7) + 1;
8160 last_bitmap = (gpa_t)-1;
8165 bitmap = vmcs12->io_bitmap_a;
8166 else if (port < 0x10000)
8167 bitmap = vmcs12->io_bitmap_b;
8170 bitmap += (port & 0x7fff) / 8;
8172 if (last_bitmap != bitmap)
8173 if (kvm_vcpu_read_guest(vcpu, bitmap, &b, 1))
8175 if (b & (1 << (port & 7)))
8180 last_bitmap = bitmap;
8187 * Return 1 if we should exit from L2 to L1 to handle an MSR access access,
8188 * rather than handle it ourselves in L0. I.e., check whether L1 expressed
8189 * disinterest in the current event (read or write a specific MSR) by using an
8190 * MSR bitmap. This may be the case even when L0 doesn't use MSR bitmaps.
8192 static bool nested_vmx_exit_handled_msr(struct kvm_vcpu *vcpu,
8193 struct vmcs12 *vmcs12, u32 exit_reason)
8195 u32 msr_index = vcpu->arch.regs[VCPU_REGS_RCX];
8198 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS))
8202 * The MSR_BITMAP page is divided into four 1024-byte bitmaps,
8203 * for the four combinations of read/write and low/high MSR numbers.
8204 * First we need to figure out which of the four to use:
8206 bitmap = vmcs12->msr_bitmap;
8207 if (exit_reason == EXIT_REASON_MSR_WRITE)
8209 if (msr_index >= 0xc0000000) {
8210 msr_index -= 0xc0000000;
8214 /* Then read the msr_index'th bit from this bitmap: */
8215 if (msr_index < 1024*8) {
8217 if (kvm_vcpu_read_guest(vcpu, bitmap + msr_index/8, &b, 1))
8219 return 1 & (b >> (msr_index & 7));
8221 return true; /* let L1 handle the wrong parameter */
8225 * Return 1 if we should exit from L2 to L1 to handle a CR access exit,
8226 * rather than handle it ourselves in L0. I.e., check if L1 wanted to
8227 * intercept (via guest_host_mask etc.) the current event.
8229 static bool nested_vmx_exit_handled_cr(struct kvm_vcpu *vcpu,
8230 struct vmcs12 *vmcs12)
8232 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
8233 int cr = exit_qualification & 15;
8237 switch ((exit_qualification >> 4) & 3) {
8238 case 0: /* mov to cr */
8239 reg = (exit_qualification >> 8) & 15;
8240 val = kvm_register_readl(vcpu, reg);
8243 if (vmcs12->cr0_guest_host_mask &
8244 (val ^ vmcs12->cr0_read_shadow))
8248 if ((vmcs12->cr3_target_count >= 1 &&
8249 vmcs12->cr3_target_value0 == val) ||
8250 (vmcs12->cr3_target_count >= 2 &&
8251 vmcs12->cr3_target_value1 == val) ||
8252 (vmcs12->cr3_target_count >= 3 &&
8253 vmcs12->cr3_target_value2 == val) ||
8254 (vmcs12->cr3_target_count >= 4 &&
8255 vmcs12->cr3_target_value3 == val))
8257 if (nested_cpu_has(vmcs12, CPU_BASED_CR3_LOAD_EXITING))
8261 if (vmcs12->cr4_guest_host_mask &
8262 (vmcs12->cr4_read_shadow ^ val))
8266 if (nested_cpu_has(vmcs12, CPU_BASED_CR8_LOAD_EXITING))
8272 if ((vmcs12->cr0_guest_host_mask & X86_CR0_TS) &&
8273 (vmcs12->cr0_read_shadow & X86_CR0_TS))
8276 case 1: /* mov from cr */
8279 if (vmcs12->cpu_based_vm_exec_control &
8280 CPU_BASED_CR3_STORE_EXITING)
8284 if (vmcs12->cpu_based_vm_exec_control &
8285 CPU_BASED_CR8_STORE_EXITING)
8292 * lmsw can change bits 1..3 of cr0, and only set bit 0 of
8293 * cr0. Other attempted changes are ignored, with no exit.
8295 val = (exit_qualification >> LMSW_SOURCE_DATA_SHIFT) & 0x0f;
8296 if (vmcs12->cr0_guest_host_mask & 0xe &
8297 (val ^ vmcs12->cr0_read_shadow))
8299 if ((vmcs12->cr0_guest_host_mask & 0x1) &&
8300 !(vmcs12->cr0_read_shadow & 0x1) &&
8309 * Return 1 if we should exit from L2 to L1 to handle an exit, or 0 if we
8310 * should handle it ourselves in L0 (and then continue L2). Only call this
8311 * when in is_guest_mode (L2).
8313 static bool nested_vmx_exit_reflected(struct kvm_vcpu *vcpu, u32 exit_reason)
8315 u32 intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
8316 struct vcpu_vmx *vmx = to_vmx(vcpu);
8317 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
8319 if (vmx->nested.nested_run_pending)
8322 if (unlikely(vmx->fail)) {
8323 pr_info_ratelimited("%s failed vm entry %x\n", __func__,
8324 vmcs_read32(VM_INSTRUCTION_ERROR));
8329 * The host physical addresses of some pages of guest memory
8330 * are loaded into the vmcs02 (e.g. vmcs12's Virtual APIC
8331 * Page). The CPU may write to these pages via their host
8332 * physical address while L2 is running, bypassing any
8333 * address-translation-based dirty tracking (e.g. EPT write
8336 * Mark them dirty on every exit from L2 to prevent them from
8337 * getting out of sync with dirty tracking.
8339 nested_mark_vmcs12_pages_dirty(vcpu);
8341 trace_kvm_nested_vmexit(kvm_rip_read(vcpu), exit_reason,
8342 vmcs_readl(EXIT_QUALIFICATION),
8343 vmx->idt_vectoring_info,
8345 vmcs_read32(VM_EXIT_INTR_ERROR_CODE),
8348 switch (exit_reason) {
8349 case EXIT_REASON_EXCEPTION_NMI:
8350 if (is_nmi(intr_info))
8352 else if (is_page_fault(intr_info))
8353 return !vmx->vcpu.arch.apf.host_apf_reason && enable_ept;
8354 else if (is_no_device(intr_info) &&
8355 !(vmcs12->guest_cr0 & X86_CR0_TS))
8357 else if (is_debug(intr_info) &&
8359 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))
8361 else if (is_breakpoint(intr_info) &&
8362 vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
8364 return vmcs12->exception_bitmap &
8365 (1u << (intr_info & INTR_INFO_VECTOR_MASK));
8366 case EXIT_REASON_EXTERNAL_INTERRUPT:
8368 case EXIT_REASON_TRIPLE_FAULT:
8370 case EXIT_REASON_PENDING_INTERRUPT:
8371 return nested_cpu_has(vmcs12, CPU_BASED_VIRTUAL_INTR_PENDING);
8372 case EXIT_REASON_NMI_WINDOW:
8373 return nested_cpu_has(vmcs12, CPU_BASED_VIRTUAL_NMI_PENDING);
8374 case EXIT_REASON_TASK_SWITCH:
8376 case EXIT_REASON_CPUID:
8378 case EXIT_REASON_HLT:
8379 return nested_cpu_has(vmcs12, CPU_BASED_HLT_EXITING);
8380 case EXIT_REASON_INVD:
8382 case EXIT_REASON_INVLPG:
8383 return nested_cpu_has(vmcs12, CPU_BASED_INVLPG_EXITING);
8384 case EXIT_REASON_RDPMC:
8385 return nested_cpu_has(vmcs12, CPU_BASED_RDPMC_EXITING);
8386 case EXIT_REASON_RDRAND:
8387 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_RDRAND_EXITING);
8388 case EXIT_REASON_RDSEED:
8389 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_RDSEED_EXITING);
8390 case EXIT_REASON_RDTSC: case EXIT_REASON_RDTSCP:
8391 return nested_cpu_has(vmcs12, CPU_BASED_RDTSC_EXITING);
8392 case EXIT_REASON_VMCALL: case EXIT_REASON_VMCLEAR:
8393 case EXIT_REASON_VMLAUNCH: case EXIT_REASON_VMPTRLD:
8394 case EXIT_REASON_VMPTRST: case EXIT_REASON_VMREAD:
8395 case EXIT_REASON_VMRESUME: case EXIT_REASON_VMWRITE:
8396 case EXIT_REASON_VMOFF: case EXIT_REASON_VMON:
8397 case EXIT_REASON_INVEPT: case EXIT_REASON_INVVPID:
8399 * VMX instructions trap unconditionally. This allows L1 to
8400 * emulate them for its L2 guest, i.e., allows 3-level nesting!
8403 case EXIT_REASON_CR_ACCESS:
8404 return nested_vmx_exit_handled_cr(vcpu, vmcs12);
8405 case EXIT_REASON_DR_ACCESS:
8406 return nested_cpu_has(vmcs12, CPU_BASED_MOV_DR_EXITING);
8407 case EXIT_REASON_IO_INSTRUCTION:
8408 return nested_vmx_exit_handled_io(vcpu, vmcs12);
8409 case EXIT_REASON_GDTR_IDTR: case EXIT_REASON_LDTR_TR:
8410 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_DESC);
8411 case EXIT_REASON_MSR_READ:
8412 case EXIT_REASON_MSR_WRITE:
8413 return nested_vmx_exit_handled_msr(vcpu, vmcs12, exit_reason);
8414 case EXIT_REASON_INVALID_STATE:
8416 case EXIT_REASON_MWAIT_INSTRUCTION:
8417 return nested_cpu_has(vmcs12, CPU_BASED_MWAIT_EXITING);
8418 case EXIT_REASON_MONITOR_TRAP_FLAG:
8419 return nested_cpu_has(vmcs12, CPU_BASED_MONITOR_TRAP_FLAG);
8420 case EXIT_REASON_MONITOR_INSTRUCTION:
8421 return nested_cpu_has(vmcs12, CPU_BASED_MONITOR_EXITING);
8422 case EXIT_REASON_PAUSE_INSTRUCTION:
8423 return nested_cpu_has(vmcs12, CPU_BASED_PAUSE_EXITING) ||
8424 nested_cpu_has2(vmcs12,
8425 SECONDARY_EXEC_PAUSE_LOOP_EXITING);
8426 case EXIT_REASON_MCE_DURING_VMENTRY:
8428 case EXIT_REASON_TPR_BELOW_THRESHOLD:
8429 return nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW);
8430 case EXIT_REASON_APIC_ACCESS:
8431 return nested_cpu_has2(vmcs12,
8432 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES);
8433 case EXIT_REASON_APIC_WRITE:
8434 case EXIT_REASON_EOI_INDUCED:
8435 /* apic_write and eoi_induced should exit unconditionally. */
8437 case EXIT_REASON_EPT_VIOLATION:
8439 * L0 always deals with the EPT violation. If nested EPT is
8440 * used, and the nested mmu code discovers that the address is
8441 * missing in the guest EPT table (EPT12), the EPT violation
8442 * will be injected with nested_ept_inject_page_fault()
8445 case EXIT_REASON_EPT_MISCONFIG:
8447 * L2 never uses directly L1's EPT, but rather L0's own EPT
8448 * table (shadow on EPT) or a merged EPT table that L0 built
8449 * (EPT on EPT). So any problems with the structure of the
8450 * table is L0's fault.
8453 case EXIT_REASON_INVPCID:
8455 nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_INVPCID) &&
8456 nested_cpu_has(vmcs12, CPU_BASED_INVLPG_EXITING);
8457 case EXIT_REASON_WBINVD:
8458 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_WBINVD_EXITING);
8459 case EXIT_REASON_XSETBV:
8461 case EXIT_REASON_XSAVES: case EXIT_REASON_XRSTORS:
8463 * This should never happen, since it is not possible to
8464 * set XSS to a non-zero value---neither in L1 nor in L2.
8465 * If if it were, XSS would have to be checked against
8466 * the XSS exit bitmap in vmcs12.
8468 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_XSAVES);
8469 case EXIT_REASON_PREEMPTION_TIMER:
8471 case EXIT_REASON_PML_FULL:
8472 /* We emulate PML support to L1. */
8474 case EXIT_REASON_VMFUNC:
8475 /* VM functions are emulated through L2->L0 vmexits. */
8482 static int nested_vmx_reflect_vmexit(struct kvm_vcpu *vcpu, u32 exit_reason)
8484 u32 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
8487 * At this point, the exit interruption info in exit_intr_info
8488 * is only valid for EXCEPTION_NMI exits. For EXTERNAL_INTERRUPT
8489 * we need to query the in-kernel LAPIC.
8491 WARN_ON(exit_reason == EXIT_REASON_EXTERNAL_INTERRUPT);
8492 if ((exit_intr_info &
8493 (INTR_INFO_VALID_MASK | INTR_INFO_DELIVER_CODE_MASK)) ==
8494 (INTR_INFO_VALID_MASK | INTR_INFO_DELIVER_CODE_MASK)) {
8495 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
8496 vmcs12->vm_exit_intr_error_code =
8497 vmcs_read32(VM_EXIT_INTR_ERROR_CODE);
8500 nested_vmx_vmexit(vcpu, exit_reason, exit_intr_info,
8501 vmcs_readl(EXIT_QUALIFICATION));
8505 static void vmx_get_exit_info(struct kvm_vcpu *vcpu, u64 *info1, u64 *info2)
8507 *info1 = vmcs_readl(EXIT_QUALIFICATION);
8508 *info2 = vmcs_read32(VM_EXIT_INTR_INFO);
8511 static void vmx_destroy_pml_buffer(struct vcpu_vmx *vmx)
8514 __free_page(vmx->pml_pg);
8519 static void vmx_flush_pml_buffer(struct kvm_vcpu *vcpu)
8521 struct vcpu_vmx *vmx = to_vmx(vcpu);
8525 pml_idx = vmcs_read16(GUEST_PML_INDEX);
8527 /* Do nothing if PML buffer is empty */
8528 if (pml_idx == (PML_ENTITY_NUM - 1))
8531 /* PML index always points to next available PML buffer entity */
8532 if (pml_idx >= PML_ENTITY_NUM)
8537 pml_buf = page_address(vmx->pml_pg);
8538 for (; pml_idx < PML_ENTITY_NUM; pml_idx++) {
8541 gpa = pml_buf[pml_idx];
8542 WARN_ON(gpa & (PAGE_SIZE - 1));
8543 kvm_vcpu_mark_page_dirty(vcpu, gpa >> PAGE_SHIFT);
8546 /* reset PML index */
8547 vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1);
8551 * Flush all vcpus' PML buffer and update logged GPAs to dirty_bitmap.
8552 * Called before reporting dirty_bitmap to userspace.
8554 static void kvm_flush_pml_buffers(struct kvm *kvm)
8557 struct kvm_vcpu *vcpu;
8559 * We only need to kick vcpu out of guest mode here, as PML buffer
8560 * is flushed at beginning of all VMEXITs, and it's obvious that only
8561 * vcpus running in guest are possible to have unflushed GPAs in PML
8564 kvm_for_each_vcpu(i, vcpu, kvm)
8565 kvm_vcpu_kick(vcpu);
8568 static void vmx_dump_sel(char *name, uint32_t sel)
8570 pr_err("%s sel=0x%04x, attr=0x%05x, limit=0x%08x, base=0x%016lx\n",
8571 name, vmcs_read16(sel),
8572 vmcs_read32(sel + GUEST_ES_AR_BYTES - GUEST_ES_SELECTOR),
8573 vmcs_read32(sel + GUEST_ES_LIMIT - GUEST_ES_SELECTOR),
8574 vmcs_readl(sel + GUEST_ES_BASE - GUEST_ES_SELECTOR));
8577 static void vmx_dump_dtsel(char *name, uint32_t limit)
8579 pr_err("%s limit=0x%08x, base=0x%016lx\n",
8580 name, vmcs_read32(limit),
8581 vmcs_readl(limit + GUEST_GDTR_BASE - GUEST_GDTR_LIMIT));
8584 static void dump_vmcs(void)
8586 u32 vmentry_ctl = vmcs_read32(VM_ENTRY_CONTROLS);
8587 u32 vmexit_ctl = vmcs_read32(VM_EXIT_CONTROLS);
8588 u32 cpu_based_exec_ctrl = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
8589 u32 pin_based_exec_ctrl = vmcs_read32(PIN_BASED_VM_EXEC_CONTROL);
8590 u32 secondary_exec_control = 0;
8591 unsigned long cr4 = vmcs_readl(GUEST_CR4);
8592 u64 efer = vmcs_read64(GUEST_IA32_EFER);
8595 if (cpu_has_secondary_exec_ctrls())
8596 secondary_exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
8598 pr_err("*** Guest State ***\n");
8599 pr_err("CR0: actual=0x%016lx, shadow=0x%016lx, gh_mask=%016lx\n",
8600 vmcs_readl(GUEST_CR0), vmcs_readl(CR0_READ_SHADOW),
8601 vmcs_readl(CR0_GUEST_HOST_MASK));
8602 pr_err("CR4: actual=0x%016lx, shadow=0x%016lx, gh_mask=%016lx\n",
8603 cr4, vmcs_readl(CR4_READ_SHADOW), vmcs_readl(CR4_GUEST_HOST_MASK));
8604 pr_err("CR3 = 0x%016lx\n", vmcs_readl(GUEST_CR3));
8605 if ((secondary_exec_control & SECONDARY_EXEC_ENABLE_EPT) &&
8606 (cr4 & X86_CR4_PAE) && !(efer & EFER_LMA))
8608 pr_err("PDPTR0 = 0x%016llx PDPTR1 = 0x%016llx\n",
8609 vmcs_read64(GUEST_PDPTR0), vmcs_read64(GUEST_PDPTR1));
8610 pr_err("PDPTR2 = 0x%016llx PDPTR3 = 0x%016llx\n",
8611 vmcs_read64(GUEST_PDPTR2), vmcs_read64(GUEST_PDPTR3));
8613 pr_err("RSP = 0x%016lx RIP = 0x%016lx\n",
8614 vmcs_readl(GUEST_RSP), vmcs_readl(GUEST_RIP));
8615 pr_err("RFLAGS=0x%08lx DR7 = 0x%016lx\n",
8616 vmcs_readl(GUEST_RFLAGS), vmcs_readl(GUEST_DR7));
8617 pr_err("Sysenter RSP=%016lx CS:RIP=%04x:%016lx\n",
8618 vmcs_readl(GUEST_SYSENTER_ESP),
8619 vmcs_read32(GUEST_SYSENTER_CS), vmcs_readl(GUEST_SYSENTER_EIP));
8620 vmx_dump_sel("CS: ", GUEST_CS_SELECTOR);
8621 vmx_dump_sel("DS: ", GUEST_DS_SELECTOR);
8622 vmx_dump_sel("SS: ", GUEST_SS_SELECTOR);
8623 vmx_dump_sel("ES: ", GUEST_ES_SELECTOR);
8624 vmx_dump_sel("FS: ", GUEST_FS_SELECTOR);
8625 vmx_dump_sel("GS: ", GUEST_GS_SELECTOR);
8626 vmx_dump_dtsel("GDTR:", GUEST_GDTR_LIMIT);
8627 vmx_dump_sel("LDTR:", GUEST_LDTR_SELECTOR);
8628 vmx_dump_dtsel("IDTR:", GUEST_IDTR_LIMIT);
8629 vmx_dump_sel("TR: ", GUEST_TR_SELECTOR);
8630 if ((vmexit_ctl & (VM_EXIT_SAVE_IA32_PAT | VM_EXIT_SAVE_IA32_EFER)) ||
8631 (vmentry_ctl & (VM_ENTRY_LOAD_IA32_PAT | VM_ENTRY_LOAD_IA32_EFER)))
8632 pr_err("EFER = 0x%016llx PAT = 0x%016llx\n",
8633 efer, vmcs_read64(GUEST_IA32_PAT));
8634 pr_err("DebugCtl = 0x%016llx DebugExceptions = 0x%016lx\n",
8635 vmcs_read64(GUEST_IA32_DEBUGCTL),
8636 vmcs_readl(GUEST_PENDING_DBG_EXCEPTIONS));
8637 if (vmentry_ctl & VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL)
8638 pr_err("PerfGlobCtl = 0x%016llx\n",
8639 vmcs_read64(GUEST_IA32_PERF_GLOBAL_CTRL));
8640 if (vmentry_ctl & VM_ENTRY_LOAD_BNDCFGS)
8641 pr_err("BndCfgS = 0x%016llx\n", vmcs_read64(GUEST_BNDCFGS));
8642 pr_err("Interruptibility = %08x ActivityState = %08x\n",
8643 vmcs_read32(GUEST_INTERRUPTIBILITY_INFO),
8644 vmcs_read32(GUEST_ACTIVITY_STATE));
8645 if (secondary_exec_control & SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY)
8646 pr_err("InterruptStatus = %04x\n",
8647 vmcs_read16(GUEST_INTR_STATUS));
8649 pr_err("*** Host State ***\n");
8650 pr_err("RIP = 0x%016lx RSP = 0x%016lx\n",
8651 vmcs_readl(HOST_RIP), vmcs_readl(HOST_RSP));
8652 pr_err("CS=%04x SS=%04x DS=%04x ES=%04x FS=%04x GS=%04x TR=%04x\n",
8653 vmcs_read16(HOST_CS_SELECTOR), vmcs_read16(HOST_SS_SELECTOR),
8654 vmcs_read16(HOST_DS_SELECTOR), vmcs_read16(HOST_ES_SELECTOR),
8655 vmcs_read16(HOST_FS_SELECTOR), vmcs_read16(HOST_GS_SELECTOR),
8656 vmcs_read16(HOST_TR_SELECTOR));
8657 pr_err("FSBase=%016lx GSBase=%016lx TRBase=%016lx\n",
8658 vmcs_readl(HOST_FS_BASE), vmcs_readl(HOST_GS_BASE),
8659 vmcs_readl(HOST_TR_BASE));
8660 pr_err("GDTBase=%016lx IDTBase=%016lx\n",
8661 vmcs_readl(HOST_GDTR_BASE), vmcs_readl(HOST_IDTR_BASE));
8662 pr_err("CR0=%016lx CR3=%016lx CR4=%016lx\n",
8663 vmcs_readl(HOST_CR0), vmcs_readl(HOST_CR3),
8664 vmcs_readl(HOST_CR4));
8665 pr_err("Sysenter RSP=%016lx CS:RIP=%04x:%016lx\n",
8666 vmcs_readl(HOST_IA32_SYSENTER_ESP),
8667 vmcs_read32(HOST_IA32_SYSENTER_CS),
8668 vmcs_readl(HOST_IA32_SYSENTER_EIP));
8669 if (vmexit_ctl & (VM_EXIT_LOAD_IA32_PAT | VM_EXIT_LOAD_IA32_EFER))
8670 pr_err("EFER = 0x%016llx PAT = 0x%016llx\n",
8671 vmcs_read64(HOST_IA32_EFER),
8672 vmcs_read64(HOST_IA32_PAT));
8673 if (vmexit_ctl & VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL)
8674 pr_err("PerfGlobCtl = 0x%016llx\n",
8675 vmcs_read64(HOST_IA32_PERF_GLOBAL_CTRL));
8677 pr_err("*** Control State ***\n");
8678 pr_err("PinBased=%08x CPUBased=%08x SecondaryExec=%08x\n",
8679 pin_based_exec_ctrl, cpu_based_exec_ctrl, secondary_exec_control);
8680 pr_err("EntryControls=%08x ExitControls=%08x\n", vmentry_ctl, vmexit_ctl);
8681 pr_err("ExceptionBitmap=%08x PFECmask=%08x PFECmatch=%08x\n",
8682 vmcs_read32(EXCEPTION_BITMAP),
8683 vmcs_read32(PAGE_FAULT_ERROR_CODE_MASK),
8684 vmcs_read32(PAGE_FAULT_ERROR_CODE_MATCH));
8685 pr_err("VMEntry: intr_info=%08x errcode=%08x ilen=%08x\n",
8686 vmcs_read32(VM_ENTRY_INTR_INFO_FIELD),
8687 vmcs_read32(VM_ENTRY_EXCEPTION_ERROR_CODE),
8688 vmcs_read32(VM_ENTRY_INSTRUCTION_LEN));
8689 pr_err("VMExit: intr_info=%08x errcode=%08x ilen=%08x\n",
8690 vmcs_read32(VM_EXIT_INTR_INFO),
8691 vmcs_read32(VM_EXIT_INTR_ERROR_CODE),
8692 vmcs_read32(VM_EXIT_INSTRUCTION_LEN));
8693 pr_err(" reason=%08x qualification=%016lx\n",
8694 vmcs_read32(VM_EXIT_REASON), vmcs_readl(EXIT_QUALIFICATION));
8695 pr_err("IDTVectoring: info=%08x errcode=%08x\n",
8696 vmcs_read32(IDT_VECTORING_INFO_FIELD),
8697 vmcs_read32(IDT_VECTORING_ERROR_CODE));
8698 pr_err("TSC Offset = 0x%016llx\n", vmcs_read64(TSC_OFFSET));
8699 if (secondary_exec_control & SECONDARY_EXEC_TSC_SCALING)
8700 pr_err("TSC Multiplier = 0x%016llx\n",
8701 vmcs_read64(TSC_MULTIPLIER));
8702 if (cpu_based_exec_ctrl & CPU_BASED_TPR_SHADOW)
8703 pr_err("TPR Threshold = 0x%02x\n", vmcs_read32(TPR_THRESHOLD));
8704 if (pin_based_exec_ctrl & PIN_BASED_POSTED_INTR)
8705 pr_err("PostedIntrVec = 0x%02x\n", vmcs_read16(POSTED_INTR_NV));
8706 if ((secondary_exec_control & SECONDARY_EXEC_ENABLE_EPT))
8707 pr_err("EPT pointer = 0x%016llx\n", vmcs_read64(EPT_POINTER));
8708 n = vmcs_read32(CR3_TARGET_COUNT);
8709 for (i = 0; i + 1 < n; i += 4)
8710 pr_err("CR3 target%u=%016lx target%u=%016lx\n",
8711 i, vmcs_readl(CR3_TARGET_VALUE0 + i * 2),
8712 i + 1, vmcs_readl(CR3_TARGET_VALUE0 + i * 2 + 2));
8714 pr_err("CR3 target%u=%016lx\n",
8715 i, vmcs_readl(CR3_TARGET_VALUE0 + i * 2));
8716 if (secondary_exec_control & SECONDARY_EXEC_PAUSE_LOOP_EXITING)
8717 pr_err("PLE Gap=%08x Window=%08x\n",
8718 vmcs_read32(PLE_GAP), vmcs_read32(PLE_WINDOW));
8719 if (secondary_exec_control & SECONDARY_EXEC_ENABLE_VPID)
8720 pr_err("Virtual processor ID = 0x%04x\n",
8721 vmcs_read16(VIRTUAL_PROCESSOR_ID));
8725 * The guest has exited. See if we can fix it or if we need userspace
8728 static int vmx_handle_exit(struct kvm_vcpu *vcpu)
8730 struct vcpu_vmx *vmx = to_vmx(vcpu);
8731 u32 exit_reason = vmx->exit_reason;
8732 u32 vectoring_info = vmx->idt_vectoring_info;
8734 trace_kvm_exit(exit_reason, vcpu, KVM_ISA_VMX);
8737 * Flush logged GPAs PML buffer, this will make dirty_bitmap more
8738 * updated. Another good is, in kvm_vm_ioctl_get_dirty_log, before
8739 * querying dirty_bitmap, we only need to kick all vcpus out of guest
8740 * mode as if vcpus is in root mode, the PML buffer must has been
8744 vmx_flush_pml_buffer(vcpu);
8746 /* If guest state is invalid, start emulating */
8747 if (vmx->emulation_required)
8748 return handle_invalid_guest_state(vcpu);
8750 if (is_guest_mode(vcpu) && nested_vmx_exit_reflected(vcpu, exit_reason))
8751 return nested_vmx_reflect_vmexit(vcpu, exit_reason);
8753 if (exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY) {
8755 vcpu->run->exit_reason = KVM_EXIT_FAIL_ENTRY;
8756 vcpu->run->fail_entry.hardware_entry_failure_reason
8761 if (unlikely(vmx->fail)) {
8762 vcpu->run->exit_reason = KVM_EXIT_FAIL_ENTRY;
8763 vcpu->run->fail_entry.hardware_entry_failure_reason
8764 = vmcs_read32(VM_INSTRUCTION_ERROR);
8770 * Do not try to fix EXIT_REASON_EPT_MISCONFIG if it caused by
8771 * delivery event since it indicates guest is accessing MMIO.
8772 * The vm-exit can be triggered again after return to guest that
8773 * will cause infinite loop.
8775 if ((vectoring_info & VECTORING_INFO_VALID_MASK) &&
8776 (exit_reason != EXIT_REASON_EXCEPTION_NMI &&
8777 exit_reason != EXIT_REASON_EPT_VIOLATION &&
8778 exit_reason != EXIT_REASON_PML_FULL &&
8779 exit_reason != EXIT_REASON_TASK_SWITCH)) {
8780 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
8781 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_DELIVERY_EV;
8782 vcpu->run->internal.ndata = 3;
8783 vcpu->run->internal.data[0] = vectoring_info;
8784 vcpu->run->internal.data[1] = exit_reason;
8785 vcpu->run->internal.data[2] = vcpu->arch.exit_qualification;
8786 if (exit_reason == EXIT_REASON_EPT_MISCONFIG) {
8787 vcpu->run->internal.ndata++;
8788 vcpu->run->internal.data[3] =
8789 vmcs_read64(GUEST_PHYSICAL_ADDRESS);
8794 if (unlikely(!enable_vnmi &&
8795 vmx->loaded_vmcs->soft_vnmi_blocked)) {
8796 if (vmx_interrupt_allowed(vcpu)) {
8797 vmx->loaded_vmcs->soft_vnmi_blocked = 0;
8798 } else if (vmx->loaded_vmcs->vnmi_blocked_time > 1000000000LL &&
8799 vcpu->arch.nmi_pending) {
8801 * This CPU don't support us in finding the end of an
8802 * NMI-blocked window if the guest runs with IRQs
8803 * disabled. So we pull the trigger after 1 s of
8804 * futile waiting, but inform the user about this.
8806 printk(KERN_WARNING "%s: Breaking out of NMI-blocked "
8807 "state on VCPU %d after 1 s timeout\n",
8808 __func__, vcpu->vcpu_id);
8809 vmx->loaded_vmcs->soft_vnmi_blocked = 0;
8813 if (exit_reason < kvm_vmx_max_exit_handlers
8814 && kvm_vmx_exit_handlers[exit_reason])
8815 return kvm_vmx_exit_handlers[exit_reason](vcpu);
8817 vcpu_unimpl(vcpu, "vmx: unexpected exit reason 0x%x\n",
8819 kvm_queue_exception(vcpu, UD_VECTOR);
8824 static void update_cr8_intercept(struct kvm_vcpu *vcpu, int tpr, int irr)
8826 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
8828 if (is_guest_mode(vcpu) &&
8829 nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW))
8832 if (irr == -1 || tpr < irr) {
8833 vmcs_write32(TPR_THRESHOLD, 0);
8837 vmcs_write32(TPR_THRESHOLD, irr);
8840 static void vmx_set_virtual_x2apic_mode(struct kvm_vcpu *vcpu, bool set)
8842 u32 sec_exec_control;
8844 /* Postpone execution until vmcs01 is the current VMCS. */
8845 if (is_guest_mode(vcpu)) {
8846 to_vmx(vcpu)->nested.change_vmcs01_virtual_x2apic_mode = true;
8850 if (!cpu_has_vmx_virtualize_x2apic_mode())
8853 if (!cpu_need_tpr_shadow(vcpu))
8856 sec_exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
8859 sec_exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
8860 sec_exec_control |= SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
8862 sec_exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
8863 sec_exec_control |= SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
8864 vmx_flush_tlb_ept_only(vcpu);
8866 vmcs_write32(SECONDARY_VM_EXEC_CONTROL, sec_exec_control);
8868 vmx_set_msr_bitmap(vcpu);
8871 static void vmx_set_apic_access_page_addr(struct kvm_vcpu *vcpu, hpa_t hpa)
8873 struct vcpu_vmx *vmx = to_vmx(vcpu);
8876 * Currently we do not handle the nested case where L2 has an
8877 * APIC access page of its own; that page is still pinned.
8878 * Hence, we skip the case where the VCPU is in guest mode _and_
8879 * L1 prepared an APIC access page for L2.
8881 * For the case where L1 and L2 share the same APIC access page
8882 * (flexpriority=Y but SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES clear
8883 * in the vmcs12), this function will only update either the vmcs01
8884 * or the vmcs02. If the former, the vmcs02 will be updated by
8885 * prepare_vmcs02. If the latter, the vmcs01 will be updated in
8886 * the next L2->L1 exit.
8888 if (!is_guest_mode(vcpu) ||
8889 !nested_cpu_has2(get_vmcs12(&vmx->vcpu),
8890 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) {
8891 vmcs_write64(APIC_ACCESS_ADDR, hpa);
8892 vmx_flush_tlb_ept_only(vcpu);
8896 static void vmx_hwapic_isr_update(struct kvm_vcpu *vcpu, int max_isr)
8904 status = vmcs_read16(GUEST_INTR_STATUS);
8906 if (max_isr != old) {
8908 status |= max_isr << 8;
8909 vmcs_write16(GUEST_INTR_STATUS, status);
8913 static void vmx_set_rvi(int vector)
8921 status = vmcs_read16(GUEST_INTR_STATUS);
8922 old = (u8)status & 0xff;
8923 if ((u8)vector != old) {
8925 status |= (u8)vector;
8926 vmcs_write16(GUEST_INTR_STATUS, status);
8930 static void vmx_hwapic_irr_update(struct kvm_vcpu *vcpu, int max_irr)
8933 * When running L2, updating RVI is only relevant when
8934 * vmcs12 virtual-interrupt-delivery enabled.
8935 * However, it can be enabled only when L1 also
8936 * intercepts external-interrupts and in that case
8937 * we should not update vmcs02 RVI but instead intercept
8938 * interrupt. Therefore, do nothing when running L2.
8940 if (!is_guest_mode(vcpu))
8941 vmx_set_rvi(max_irr);
8944 static int vmx_sync_pir_to_irr(struct kvm_vcpu *vcpu)
8946 struct vcpu_vmx *vmx = to_vmx(vcpu);
8948 bool max_irr_updated;
8950 WARN_ON(!vcpu->arch.apicv_active);
8951 if (pi_test_on(&vmx->pi_desc)) {
8952 pi_clear_on(&vmx->pi_desc);
8954 * IOMMU can write to PIR.ON, so the barrier matters even on UP.
8955 * But on x86 this is just a compiler barrier anyway.
8957 smp_mb__after_atomic();
8959 kvm_apic_update_irr(vcpu, vmx->pi_desc.pir, &max_irr);
8962 * If we are running L2 and L1 has a new pending interrupt
8963 * which can be injected, we should re-evaluate
8964 * what should be done with this new L1 interrupt.
8965 * If L1 intercepts external-interrupts, we should
8966 * exit from L2 to L1. Otherwise, interrupt should be
8967 * delivered directly to L2.
8969 if (is_guest_mode(vcpu) && max_irr_updated) {
8970 if (nested_exit_on_intr(vcpu))
8971 kvm_vcpu_exiting_guest_mode(vcpu);
8973 kvm_make_request(KVM_REQ_EVENT, vcpu);
8976 max_irr = kvm_lapic_find_highest_irr(vcpu);
8978 vmx_hwapic_irr_update(vcpu, max_irr);
8982 static void vmx_load_eoi_exitmap(struct kvm_vcpu *vcpu, u64 *eoi_exit_bitmap)
8984 if (!kvm_vcpu_apicv_active(vcpu))
8987 vmcs_write64(EOI_EXIT_BITMAP0, eoi_exit_bitmap[0]);
8988 vmcs_write64(EOI_EXIT_BITMAP1, eoi_exit_bitmap[1]);
8989 vmcs_write64(EOI_EXIT_BITMAP2, eoi_exit_bitmap[2]);
8990 vmcs_write64(EOI_EXIT_BITMAP3, eoi_exit_bitmap[3]);
8993 static void vmx_apicv_post_state_restore(struct kvm_vcpu *vcpu)
8995 struct vcpu_vmx *vmx = to_vmx(vcpu);
8997 pi_clear_on(&vmx->pi_desc);
8998 memset(vmx->pi_desc.pir, 0, sizeof(vmx->pi_desc.pir));
9001 static void vmx_complete_atomic_exit(struct vcpu_vmx *vmx)
9003 u32 exit_intr_info = 0;
9004 u16 basic_exit_reason = (u16)vmx->exit_reason;
9006 if (!(basic_exit_reason == EXIT_REASON_MCE_DURING_VMENTRY
9007 || basic_exit_reason == EXIT_REASON_EXCEPTION_NMI))
9010 if (!(vmx->exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY))
9011 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
9012 vmx->exit_intr_info = exit_intr_info;
9014 /* if exit due to PF check for async PF */
9015 if (is_page_fault(exit_intr_info))
9016 vmx->vcpu.arch.apf.host_apf_reason = kvm_read_and_reset_pf_reason();
9018 /* Handle machine checks before interrupts are enabled */
9019 if (basic_exit_reason == EXIT_REASON_MCE_DURING_VMENTRY ||
9020 is_machine_check(exit_intr_info))
9021 kvm_machine_check();
9023 /* We need to handle NMIs before interrupts are enabled */
9024 if (is_nmi(exit_intr_info)) {
9025 kvm_before_handle_nmi(&vmx->vcpu);
9027 kvm_after_handle_nmi(&vmx->vcpu);
9031 static void vmx_handle_external_intr(struct kvm_vcpu *vcpu)
9033 u32 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
9035 if ((exit_intr_info & (INTR_INFO_VALID_MASK | INTR_INFO_INTR_TYPE_MASK))
9036 == (INTR_INFO_VALID_MASK | INTR_TYPE_EXT_INTR)) {
9037 unsigned int vector;
9038 unsigned long entry;
9040 struct vcpu_vmx *vmx = to_vmx(vcpu);
9041 #ifdef CONFIG_X86_64
9045 vector = exit_intr_info & INTR_INFO_VECTOR_MASK;
9046 desc = (gate_desc *)vmx->host_idt_base + vector;
9047 entry = gate_offset(desc);
9049 #ifdef CONFIG_X86_64
9050 "mov %%" _ASM_SP ", %[sp]\n\t"
9051 "and $0xfffffffffffffff0, %%" _ASM_SP "\n\t"
9056 __ASM_SIZE(push) " $%c[cs]\n\t"
9059 #ifdef CONFIG_X86_64
9064 THUNK_TARGET(entry),
9065 [ss]"i"(__KERNEL_DS),
9066 [cs]"i"(__KERNEL_CS)
9070 STACK_FRAME_NON_STANDARD(vmx_handle_external_intr);
9072 static bool vmx_has_high_real_mode_segbase(void)
9074 return enable_unrestricted_guest || emulate_invalid_guest_state;
9077 static bool vmx_mpx_supported(void)
9079 return (vmcs_config.vmexit_ctrl & VM_EXIT_CLEAR_BNDCFGS) &&
9080 (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_BNDCFGS);
9083 static bool vmx_xsaves_supported(void)
9085 return vmcs_config.cpu_based_2nd_exec_ctrl &
9086 SECONDARY_EXEC_XSAVES;
9089 static bool vmx_umip_emulated(void)
9091 return vmcs_config.cpu_based_2nd_exec_ctrl &
9092 SECONDARY_EXEC_DESC;
9095 static void vmx_recover_nmi_blocking(struct vcpu_vmx *vmx)
9100 bool idtv_info_valid;
9102 idtv_info_valid = vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK;
9105 if (vmx->loaded_vmcs->nmi_known_unmasked)
9108 * Can't use vmx->exit_intr_info since we're not sure what
9109 * the exit reason is.
9111 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
9112 unblock_nmi = (exit_intr_info & INTR_INFO_UNBLOCK_NMI) != 0;
9113 vector = exit_intr_info & INTR_INFO_VECTOR_MASK;
9115 * SDM 3: 27.7.1.2 (September 2008)
9116 * Re-set bit "block by NMI" before VM entry if vmexit caused by
9117 * a guest IRET fault.
9118 * SDM 3: 23.2.2 (September 2008)
9119 * Bit 12 is undefined in any of the following cases:
9120 * If the VM exit sets the valid bit in the IDT-vectoring
9121 * information field.
9122 * If the VM exit is due to a double fault.
9124 if ((exit_intr_info & INTR_INFO_VALID_MASK) && unblock_nmi &&
9125 vector != DF_VECTOR && !idtv_info_valid)
9126 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
9127 GUEST_INTR_STATE_NMI);
9129 vmx->loaded_vmcs->nmi_known_unmasked =
9130 !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO)
9131 & GUEST_INTR_STATE_NMI);
9132 } else if (unlikely(vmx->loaded_vmcs->soft_vnmi_blocked))
9133 vmx->loaded_vmcs->vnmi_blocked_time +=
9134 ktime_to_ns(ktime_sub(ktime_get(),
9135 vmx->loaded_vmcs->entry_time));
9138 static void __vmx_complete_interrupts(struct kvm_vcpu *vcpu,
9139 u32 idt_vectoring_info,
9140 int instr_len_field,
9141 int error_code_field)
9145 bool idtv_info_valid;
9147 idtv_info_valid = idt_vectoring_info & VECTORING_INFO_VALID_MASK;
9149 vcpu->arch.nmi_injected = false;
9150 kvm_clear_exception_queue(vcpu);
9151 kvm_clear_interrupt_queue(vcpu);
9153 if (!idtv_info_valid)
9156 kvm_make_request(KVM_REQ_EVENT, vcpu);
9158 vector = idt_vectoring_info & VECTORING_INFO_VECTOR_MASK;
9159 type = idt_vectoring_info & VECTORING_INFO_TYPE_MASK;
9162 case INTR_TYPE_NMI_INTR:
9163 vcpu->arch.nmi_injected = true;
9165 * SDM 3: 27.7.1.2 (September 2008)
9166 * Clear bit "block by NMI" before VM entry if a NMI
9169 vmx_set_nmi_mask(vcpu, false);
9171 case INTR_TYPE_SOFT_EXCEPTION:
9172 vcpu->arch.event_exit_inst_len = vmcs_read32(instr_len_field);
9174 case INTR_TYPE_HARD_EXCEPTION:
9175 if (idt_vectoring_info & VECTORING_INFO_DELIVER_CODE_MASK) {
9176 u32 err = vmcs_read32(error_code_field);
9177 kvm_requeue_exception_e(vcpu, vector, err);
9179 kvm_requeue_exception(vcpu, vector);
9181 case INTR_TYPE_SOFT_INTR:
9182 vcpu->arch.event_exit_inst_len = vmcs_read32(instr_len_field);
9184 case INTR_TYPE_EXT_INTR:
9185 kvm_queue_interrupt(vcpu, vector, type == INTR_TYPE_SOFT_INTR);
9192 static void vmx_complete_interrupts(struct vcpu_vmx *vmx)
9194 __vmx_complete_interrupts(&vmx->vcpu, vmx->idt_vectoring_info,
9195 VM_EXIT_INSTRUCTION_LEN,
9196 IDT_VECTORING_ERROR_CODE);
9199 static void vmx_cancel_injection(struct kvm_vcpu *vcpu)
9201 __vmx_complete_interrupts(vcpu,
9202 vmcs_read32(VM_ENTRY_INTR_INFO_FIELD),
9203 VM_ENTRY_INSTRUCTION_LEN,
9204 VM_ENTRY_EXCEPTION_ERROR_CODE);
9206 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0);
9209 static void atomic_switch_perf_msrs(struct vcpu_vmx *vmx)
9212 struct perf_guest_switch_msr *msrs;
9214 msrs = perf_guest_get_msrs(&nr_msrs);
9219 for (i = 0; i < nr_msrs; i++)
9220 if (msrs[i].host == msrs[i].guest)
9221 clear_atomic_switch_msr(vmx, msrs[i].msr);
9223 add_atomic_switch_msr(vmx, msrs[i].msr, msrs[i].guest,
9227 static void vmx_arm_hv_timer(struct kvm_vcpu *vcpu)
9229 struct vcpu_vmx *vmx = to_vmx(vcpu);
9233 if (vmx->hv_deadline_tsc == -1)
9237 if (vmx->hv_deadline_tsc > tscl)
9238 /* sure to be 32 bit only because checked on set_hv_timer */
9239 delta_tsc = (u32)((vmx->hv_deadline_tsc - tscl) >>
9240 cpu_preemption_timer_multi);
9244 vmcs_write32(VMX_PREEMPTION_TIMER_VALUE, delta_tsc);
9247 static void __noclone vmx_vcpu_run(struct kvm_vcpu *vcpu)
9249 struct vcpu_vmx *vmx = to_vmx(vcpu);
9250 unsigned long cr3, cr4;
9252 /* Record the guest's net vcpu time for enforced NMI injections. */
9253 if (unlikely(!enable_vnmi &&
9254 vmx->loaded_vmcs->soft_vnmi_blocked))
9255 vmx->loaded_vmcs->entry_time = ktime_get();
9257 /* Don't enter VMX if guest state is invalid, let the exit handler
9258 start emulation until we arrive back to a valid state */
9259 if (vmx->emulation_required)
9262 if (vmx->ple_window_dirty) {
9263 vmx->ple_window_dirty = false;
9264 vmcs_write32(PLE_WINDOW, vmx->ple_window);
9267 if (vmx->nested.sync_shadow_vmcs) {
9268 copy_vmcs12_to_shadow(vmx);
9269 vmx->nested.sync_shadow_vmcs = false;
9272 if (test_bit(VCPU_REGS_RSP, (unsigned long *)&vcpu->arch.regs_dirty))
9273 vmcs_writel(GUEST_RSP, vcpu->arch.regs[VCPU_REGS_RSP]);
9274 if (test_bit(VCPU_REGS_RIP, (unsigned long *)&vcpu->arch.regs_dirty))
9275 vmcs_writel(GUEST_RIP, vcpu->arch.regs[VCPU_REGS_RIP]);
9277 cr3 = __get_current_cr3_fast();
9278 if (unlikely(cr3 != vmx->loaded_vmcs->vmcs_host_cr3)) {
9279 vmcs_writel(HOST_CR3, cr3);
9280 vmx->loaded_vmcs->vmcs_host_cr3 = cr3;
9283 cr4 = cr4_read_shadow();
9284 if (unlikely(cr4 != vmx->loaded_vmcs->vmcs_host_cr4)) {
9285 vmcs_writel(HOST_CR4, cr4);
9286 vmx->loaded_vmcs->vmcs_host_cr4 = cr4;
9289 /* When single-stepping over STI and MOV SS, we must clear the
9290 * corresponding interruptibility bits in the guest state. Otherwise
9291 * vmentry fails as it then expects bit 14 (BS) in pending debug
9292 * exceptions being set, but that's not correct for the guest debugging
9294 if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP)
9295 vmx_set_interrupt_shadow(vcpu, 0);
9297 if (static_cpu_has(X86_FEATURE_PKU) &&
9298 kvm_read_cr4_bits(vcpu, X86_CR4_PKE) &&
9299 vcpu->arch.pkru != vmx->host_pkru)
9300 __write_pkru(vcpu->arch.pkru);
9302 atomic_switch_perf_msrs(vmx);
9304 vmx_arm_hv_timer(vcpu);
9306 vmx->__launched = vmx->loaded_vmcs->launched;
9308 /* Store host registers */
9309 "push %%" _ASM_DX "; push %%" _ASM_BP ";"
9310 "push %%" _ASM_CX " \n\t" /* placeholder for guest rcx */
9311 "push %%" _ASM_CX " \n\t"
9312 "cmp %%" _ASM_SP ", %c[host_rsp](%0) \n\t"
9314 "mov %%" _ASM_SP ", %c[host_rsp](%0) \n\t"
9315 __ex(ASM_VMX_VMWRITE_RSP_RDX) "\n\t"
9317 /* Reload cr2 if changed */
9318 "mov %c[cr2](%0), %%" _ASM_AX " \n\t"
9319 "mov %%cr2, %%" _ASM_DX " \n\t"
9320 "cmp %%" _ASM_AX ", %%" _ASM_DX " \n\t"
9322 "mov %%" _ASM_AX", %%cr2 \n\t"
9324 /* Check if vmlaunch of vmresume is needed */
9325 "cmpl $0, %c[launched](%0) \n\t"
9326 /* Load guest registers. Don't clobber flags. */
9327 "mov %c[rax](%0), %%" _ASM_AX " \n\t"
9328 "mov %c[rbx](%0), %%" _ASM_BX " \n\t"
9329 "mov %c[rdx](%0), %%" _ASM_DX " \n\t"
9330 "mov %c[rsi](%0), %%" _ASM_SI " \n\t"
9331 "mov %c[rdi](%0), %%" _ASM_DI " \n\t"
9332 "mov %c[rbp](%0), %%" _ASM_BP " \n\t"
9333 #ifdef CONFIG_X86_64
9334 "mov %c[r8](%0), %%r8 \n\t"
9335 "mov %c[r9](%0), %%r9 \n\t"
9336 "mov %c[r10](%0), %%r10 \n\t"
9337 "mov %c[r11](%0), %%r11 \n\t"
9338 "mov %c[r12](%0), %%r12 \n\t"
9339 "mov %c[r13](%0), %%r13 \n\t"
9340 "mov %c[r14](%0), %%r14 \n\t"
9341 "mov %c[r15](%0), %%r15 \n\t"
9343 "mov %c[rcx](%0), %%" _ASM_CX " \n\t" /* kills %0 (ecx) */
9345 /* Enter guest mode */
9347 __ex(ASM_VMX_VMLAUNCH) "\n\t"
9349 "1: " __ex(ASM_VMX_VMRESUME) "\n\t"
9351 /* Save guest registers, load host registers, keep flags */
9352 "mov %0, %c[wordsize](%%" _ASM_SP ") \n\t"
9354 "setbe %c[fail](%0)\n\t"
9355 "mov %%" _ASM_AX ", %c[rax](%0) \n\t"
9356 "mov %%" _ASM_BX ", %c[rbx](%0) \n\t"
9357 __ASM_SIZE(pop) " %c[rcx](%0) \n\t"
9358 "mov %%" _ASM_DX ", %c[rdx](%0) \n\t"
9359 "mov %%" _ASM_SI ", %c[rsi](%0) \n\t"
9360 "mov %%" _ASM_DI ", %c[rdi](%0) \n\t"
9361 "mov %%" _ASM_BP ", %c[rbp](%0) \n\t"
9362 #ifdef CONFIG_X86_64
9363 "mov %%r8, %c[r8](%0) \n\t"
9364 "mov %%r9, %c[r9](%0) \n\t"
9365 "mov %%r10, %c[r10](%0) \n\t"
9366 "mov %%r11, %c[r11](%0) \n\t"
9367 "mov %%r12, %c[r12](%0) \n\t"
9368 "mov %%r13, %c[r13](%0) \n\t"
9369 "mov %%r14, %c[r14](%0) \n\t"
9370 "mov %%r15, %c[r15](%0) \n\t"
9371 "xor %%r8d, %%r8d \n\t"
9372 "xor %%r9d, %%r9d \n\t"
9373 "xor %%r10d, %%r10d \n\t"
9374 "xor %%r11d, %%r11d \n\t"
9375 "xor %%r12d, %%r12d \n\t"
9376 "xor %%r13d, %%r13d \n\t"
9377 "xor %%r14d, %%r14d \n\t"
9378 "xor %%r15d, %%r15d \n\t"
9380 "mov %%cr2, %%" _ASM_AX " \n\t"
9381 "mov %%" _ASM_AX ", %c[cr2](%0) \n\t"
9383 "xor %%eax, %%eax \n\t"
9384 "xor %%ebx, %%ebx \n\t"
9385 "xor %%esi, %%esi \n\t"
9386 "xor %%edi, %%edi \n\t"
9387 "pop %%" _ASM_BP "; pop %%" _ASM_DX " \n\t"
9388 ".pushsection .rodata \n\t"
9389 ".global vmx_return \n\t"
9390 "vmx_return: " _ASM_PTR " 2b \n\t"
9392 : : "c"(vmx), "d"((unsigned long)HOST_RSP),
9393 [launched]"i"(offsetof(struct vcpu_vmx, __launched)),
9394 [fail]"i"(offsetof(struct vcpu_vmx, fail)),
9395 [host_rsp]"i"(offsetof(struct vcpu_vmx, host_rsp)),
9396 [rax]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RAX])),
9397 [rbx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBX])),
9398 [rcx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RCX])),
9399 [rdx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDX])),
9400 [rsi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RSI])),
9401 [rdi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDI])),
9402 [rbp]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBP])),
9403 #ifdef CONFIG_X86_64
9404 [r8]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R8])),
9405 [r9]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R9])),
9406 [r10]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R10])),
9407 [r11]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R11])),
9408 [r12]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R12])),
9409 [r13]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R13])),
9410 [r14]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R14])),
9411 [r15]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R15])),
9413 [cr2]"i"(offsetof(struct vcpu_vmx, vcpu.arch.cr2)),
9414 [wordsize]"i"(sizeof(ulong))
9416 #ifdef CONFIG_X86_64
9417 , "rax", "rbx", "rdi", "rsi"
9418 , "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15"
9420 , "eax", "ebx", "edi", "esi"
9424 /* Eliminate branch target predictions from guest mode */
9427 /* MSR_IA32_DEBUGCTLMSR is zeroed on vmexit. Restore it if needed */
9428 if (vmx->host_debugctlmsr)
9429 update_debugctlmsr(vmx->host_debugctlmsr);
9431 #ifndef CONFIG_X86_64
9433 * The sysexit path does not restore ds/es, so we must set them to
9434 * a reasonable value ourselves.
9436 * We can't defer this to vmx_load_host_state() since that function
9437 * may be executed in interrupt context, which saves and restore segments
9438 * around it, nullifying its effect.
9440 loadsegment(ds, __USER_DS);
9441 loadsegment(es, __USER_DS);
9444 vcpu->arch.regs_avail = ~((1 << VCPU_REGS_RIP) | (1 << VCPU_REGS_RSP)
9445 | (1 << VCPU_EXREG_RFLAGS)
9446 | (1 << VCPU_EXREG_PDPTR)
9447 | (1 << VCPU_EXREG_SEGMENTS)
9448 | (1 << VCPU_EXREG_CR3));
9449 vcpu->arch.regs_dirty = 0;
9452 * eager fpu is enabled if PKEY is supported and CR4 is switched
9453 * back on host, so it is safe to read guest PKRU from current
9456 if (static_cpu_has(X86_FEATURE_PKU) &&
9457 kvm_read_cr4_bits(vcpu, X86_CR4_PKE)) {
9458 vcpu->arch.pkru = __read_pkru();
9459 if (vcpu->arch.pkru != vmx->host_pkru)
9460 __write_pkru(vmx->host_pkru);
9464 * the KVM_REQ_EVENT optimization bit is only on for one entry, and if
9465 * we did not inject a still-pending event to L1 now because of
9466 * nested_run_pending, we need to re-enable this bit.
9468 if (vmx->nested.nested_run_pending)
9469 kvm_make_request(KVM_REQ_EVENT, vcpu);
9471 vmx->nested.nested_run_pending = 0;
9472 vmx->idt_vectoring_info = 0;
9474 vmx->exit_reason = vmx->fail ? 0xdead : vmcs_read32(VM_EXIT_REASON);
9475 if (vmx->fail || (vmx->exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY))
9478 vmx->loaded_vmcs->launched = 1;
9479 vmx->idt_vectoring_info = vmcs_read32(IDT_VECTORING_INFO_FIELD);
9481 vmx_complete_atomic_exit(vmx);
9482 vmx_recover_nmi_blocking(vmx);
9483 vmx_complete_interrupts(vmx);
9485 STACK_FRAME_NON_STANDARD(vmx_vcpu_run);
9487 static void vmx_switch_vmcs(struct kvm_vcpu *vcpu, struct loaded_vmcs *vmcs)
9489 struct vcpu_vmx *vmx = to_vmx(vcpu);
9492 if (vmx->loaded_vmcs == vmcs)
9496 vmx->loaded_vmcs = vmcs;
9498 vmx_vcpu_load(vcpu, cpu);
9503 * Ensure that the current vmcs of the logical processor is the
9504 * vmcs01 of the vcpu before calling free_nested().
9506 static void vmx_free_vcpu_nested(struct kvm_vcpu *vcpu)
9508 struct vcpu_vmx *vmx = to_vmx(vcpu);
9511 vmx_switch_vmcs(vcpu, &vmx->vmcs01);
9516 static void vmx_free_vcpu(struct kvm_vcpu *vcpu)
9518 struct vcpu_vmx *vmx = to_vmx(vcpu);
9521 vmx_destroy_pml_buffer(vmx);
9522 free_vpid(vmx->vpid);
9523 leave_guest_mode(vcpu);
9524 vmx_free_vcpu_nested(vcpu);
9525 free_loaded_vmcs(vmx->loaded_vmcs);
9526 kfree(vmx->guest_msrs);
9527 kvm_vcpu_uninit(vcpu);
9528 kmem_cache_free(kvm_vcpu_cache, vmx);
9531 static struct kvm_vcpu *vmx_create_vcpu(struct kvm *kvm, unsigned int id)
9534 struct vcpu_vmx *vmx = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
9538 return ERR_PTR(-ENOMEM);
9540 vmx->vpid = allocate_vpid();
9542 err = kvm_vcpu_init(&vmx->vcpu, kvm, id);
9549 * If PML is turned on, failure on enabling PML just results in failure
9550 * of creating the vcpu, therefore we can simplify PML logic (by
9551 * avoiding dealing with cases, such as enabling PML partially on vcpus
9552 * for the guest, etc.
9555 vmx->pml_pg = alloc_page(GFP_KERNEL | __GFP_ZERO);
9560 vmx->guest_msrs = kmalloc(PAGE_SIZE, GFP_KERNEL);
9561 BUILD_BUG_ON(ARRAY_SIZE(vmx_msr_index) * sizeof(vmx->guest_msrs[0])
9564 if (!vmx->guest_msrs)
9567 vmx->loaded_vmcs = &vmx->vmcs01;
9568 vmx->loaded_vmcs->vmcs = alloc_vmcs();
9569 vmx->loaded_vmcs->shadow_vmcs = NULL;
9570 if (!vmx->loaded_vmcs->vmcs)
9572 loaded_vmcs_init(vmx->loaded_vmcs);
9575 vmx_vcpu_load(&vmx->vcpu, cpu);
9576 vmx->vcpu.cpu = cpu;
9577 vmx_vcpu_setup(vmx);
9578 vmx_vcpu_put(&vmx->vcpu);
9580 if (cpu_need_virtualize_apic_accesses(&vmx->vcpu)) {
9581 err = alloc_apic_access_page(kvm);
9587 err = init_rmode_identity_map(kvm);
9593 nested_vmx_setup_ctls_msrs(vmx);
9594 vmx->nested.vpid02 = allocate_vpid();
9597 vmx->nested.posted_intr_nv = -1;
9598 vmx->nested.current_vmptr = -1ull;
9600 vmx->msr_ia32_feature_control_valid_bits = FEATURE_CONTROL_LOCKED;
9603 * Enforce invariant: pi_desc.nv is always either POSTED_INTR_VECTOR
9604 * or POSTED_INTR_WAKEUP_VECTOR.
9606 vmx->pi_desc.nv = POSTED_INTR_VECTOR;
9607 vmx->pi_desc.sn = 1;
9612 free_vpid(vmx->nested.vpid02);
9613 free_loaded_vmcs(vmx->loaded_vmcs);
9615 kfree(vmx->guest_msrs);
9617 vmx_destroy_pml_buffer(vmx);
9619 kvm_vcpu_uninit(&vmx->vcpu);
9621 free_vpid(vmx->vpid);
9622 kmem_cache_free(kvm_vcpu_cache, vmx);
9623 return ERR_PTR(err);
9626 static void __init vmx_check_processor_compat(void *rtn)
9628 struct vmcs_config vmcs_conf;
9631 if (setup_vmcs_config(&vmcs_conf) < 0)
9633 if (memcmp(&vmcs_config, &vmcs_conf, sizeof(struct vmcs_config)) != 0) {
9634 printk(KERN_ERR "kvm: CPU %d feature inconsistency!\n",
9635 smp_processor_id());
9640 static u64 vmx_get_mt_mask(struct kvm_vcpu *vcpu, gfn_t gfn, bool is_mmio)
9645 /* For VT-d and EPT combination
9646 * 1. MMIO: always map as UC
9648 * a. VT-d without snooping control feature: can't guarantee the
9649 * result, try to trust guest.
9650 * b. VT-d with snooping control feature: snooping control feature of
9651 * VT-d engine can guarantee the cache correctness. Just set it
9652 * to WB to keep consistent with host. So the same as item 3.
9653 * 3. EPT without VT-d: always map as WB and set IPAT=1 to keep
9654 * consistent with host MTRR
9657 cache = MTRR_TYPE_UNCACHABLE;
9661 if (!kvm_arch_has_noncoherent_dma(vcpu->kvm)) {
9662 ipat = VMX_EPT_IPAT_BIT;
9663 cache = MTRR_TYPE_WRBACK;
9667 if (kvm_read_cr0(vcpu) & X86_CR0_CD) {
9668 ipat = VMX_EPT_IPAT_BIT;
9669 if (kvm_check_has_quirk(vcpu->kvm, KVM_X86_QUIRK_CD_NW_CLEARED))
9670 cache = MTRR_TYPE_WRBACK;
9672 cache = MTRR_TYPE_UNCACHABLE;
9676 cache = kvm_mtrr_get_guest_memory_type(vcpu, gfn);
9679 return (cache << VMX_EPT_MT_EPTE_SHIFT) | ipat;
9682 static int vmx_get_lpage_level(void)
9684 if (enable_ept && !cpu_has_vmx_ept_1g_page())
9685 return PT_DIRECTORY_LEVEL;
9687 /* For shadow and EPT supported 1GB page */
9688 return PT_PDPE_LEVEL;
9691 static void vmcs_set_secondary_exec_control(u32 new_ctl)
9694 * These bits in the secondary execution controls field
9695 * are dynamic, the others are mostly based on the hypervisor
9696 * architecture and the guest's CPUID. Do not touch the
9700 SECONDARY_EXEC_SHADOW_VMCS |
9701 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
9702 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
9703 SECONDARY_EXEC_DESC;
9705 u32 cur_ctl = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
9707 vmcs_write32(SECONDARY_VM_EXEC_CONTROL,
9708 (new_ctl & ~mask) | (cur_ctl & mask));
9712 * Generate MSR_IA32_VMX_CR{0,4}_FIXED1 according to CPUID. Only set bits
9713 * (indicating "allowed-1") if they are supported in the guest's CPUID.
9715 static void nested_vmx_cr_fixed1_bits_update(struct kvm_vcpu *vcpu)
9717 struct vcpu_vmx *vmx = to_vmx(vcpu);
9718 struct kvm_cpuid_entry2 *entry;
9720 vmx->nested.nested_vmx_cr0_fixed1 = 0xffffffff;
9721 vmx->nested.nested_vmx_cr4_fixed1 = X86_CR4_PCE;
9723 #define cr4_fixed1_update(_cr4_mask, _reg, _cpuid_mask) do { \
9724 if (entry && (entry->_reg & (_cpuid_mask))) \
9725 vmx->nested.nested_vmx_cr4_fixed1 |= (_cr4_mask); \
9728 entry = kvm_find_cpuid_entry(vcpu, 0x1, 0);
9729 cr4_fixed1_update(X86_CR4_VME, edx, bit(X86_FEATURE_VME));
9730 cr4_fixed1_update(X86_CR4_PVI, edx, bit(X86_FEATURE_VME));
9731 cr4_fixed1_update(X86_CR4_TSD, edx, bit(X86_FEATURE_TSC));
9732 cr4_fixed1_update(X86_CR4_DE, edx, bit(X86_FEATURE_DE));
9733 cr4_fixed1_update(X86_CR4_PSE, edx, bit(X86_FEATURE_PSE));
9734 cr4_fixed1_update(X86_CR4_PAE, edx, bit(X86_FEATURE_PAE));
9735 cr4_fixed1_update(X86_CR4_MCE, edx, bit(X86_FEATURE_MCE));
9736 cr4_fixed1_update(X86_CR4_PGE, edx, bit(X86_FEATURE_PGE));
9737 cr4_fixed1_update(X86_CR4_OSFXSR, edx, bit(X86_FEATURE_FXSR));
9738 cr4_fixed1_update(X86_CR4_OSXMMEXCPT, edx, bit(X86_FEATURE_XMM));
9739 cr4_fixed1_update(X86_CR4_VMXE, ecx, bit(X86_FEATURE_VMX));
9740 cr4_fixed1_update(X86_CR4_SMXE, ecx, bit(X86_FEATURE_SMX));
9741 cr4_fixed1_update(X86_CR4_PCIDE, ecx, bit(X86_FEATURE_PCID));
9742 cr4_fixed1_update(X86_CR4_OSXSAVE, ecx, bit(X86_FEATURE_XSAVE));
9744 entry = kvm_find_cpuid_entry(vcpu, 0x7, 0);
9745 cr4_fixed1_update(X86_CR4_FSGSBASE, ebx, bit(X86_FEATURE_FSGSBASE));
9746 cr4_fixed1_update(X86_CR4_SMEP, ebx, bit(X86_FEATURE_SMEP));
9747 cr4_fixed1_update(X86_CR4_SMAP, ebx, bit(X86_FEATURE_SMAP));
9748 cr4_fixed1_update(X86_CR4_PKE, ecx, bit(X86_FEATURE_PKU));
9749 cr4_fixed1_update(X86_CR4_UMIP, ecx, bit(X86_FEATURE_UMIP));
9751 #undef cr4_fixed1_update
9754 static void vmx_cpuid_update(struct kvm_vcpu *vcpu)
9756 struct vcpu_vmx *vmx = to_vmx(vcpu);
9758 if (cpu_has_secondary_exec_ctrls()) {
9759 vmx_compute_secondary_exec_control(vmx);
9760 vmcs_set_secondary_exec_control(vmx->secondary_exec_control);
9763 if (nested_vmx_allowed(vcpu))
9764 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits |=
9765 FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
9767 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits &=
9768 ~FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
9770 if (nested_vmx_allowed(vcpu))
9771 nested_vmx_cr_fixed1_bits_update(vcpu);
9774 static void vmx_set_supported_cpuid(u32 func, struct kvm_cpuid_entry2 *entry)
9776 if (func == 1 && nested)
9777 entry->ecx |= bit(X86_FEATURE_VMX);
9780 static void nested_ept_inject_page_fault(struct kvm_vcpu *vcpu,
9781 struct x86_exception *fault)
9783 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
9784 struct vcpu_vmx *vmx = to_vmx(vcpu);
9786 unsigned long exit_qualification = vcpu->arch.exit_qualification;
9788 if (vmx->nested.pml_full) {
9789 exit_reason = EXIT_REASON_PML_FULL;
9790 vmx->nested.pml_full = false;
9791 exit_qualification &= INTR_INFO_UNBLOCK_NMI;
9792 } else if (fault->error_code & PFERR_RSVD_MASK)
9793 exit_reason = EXIT_REASON_EPT_MISCONFIG;
9795 exit_reason = EXIT_REASON_EPT_VIOLATION;
9797 nested_vmx_vmexit(vcpu, exit_reason, 0, exit_qualification);
9798 vmcs12->guest_physical_address = fault->address;
9801 static bool nested_ept_ad_enabled(struct kvm_vcpu *vcpu)
9803 return nested_ept_get_cr3(vcpu) & VMX_EPTP_AD_ENABLE_BIT;
9806 /* Callbacks for nested_ept_init_mmu_context: */
9808 static unsigned long nested_ept_get_cr3(struct kvm_vcpu *vcpu)
9810 /* return the page table to be shadowed - in our case, EPT12 */
9811 return get_vmcs12(vcpu)->ept_pointer;
9814 static int nested_ept_init_mmu_context(struct kvm_vcpu *vcpu)
9816 WARN_ON(mmu_is_nested(vcpu));
9817 if (!valid_ept_address(vcpu, nested_ept_get_cr3(vcpu)))
9820 kvm_mmu_unload(vcpu);
9821 kvm_init_shadow_ept_mmu(vcpu,
9822 to_vmx(vcpu)->nested.nested_vmx_ept_caps &
9823 VMX_EPT_EXECUTE_ONLY_BIT,
9824 nested_ept_ad_enabled(vcpu));
9825 vcpu->arch.mmu.set_cr3 = vmx_set_cr3;
9826 vcpu->arch.mmu.get_cr3 = nested_ept_get_cr3;
9827 vcpu->arch.mmu.inject_page_fault = nested_ept_inject_page_fault;
9829 vcpu->arch.walk_mmu = &vcpu->arch.nested_mmu;
9833 static void nested_ept_uninit_mmu_context(struct kvm_vcpu *vcpu)
9835 vcpu->arch.walk_mmu = &vcpu->arch.mmu;
9838 static bool nested_vmx_is_page_fault_vmexit(struct vmcs12 *vmcs12,
9841 bool inequality, bit;
9843 bit = (vmcs12->exception_bitmap & (1u << PF_VECTOR)) != 0;
9845 (error_code & vmcs12->page_fault_error_code_mask) !=
9846 vmcs12->page_fault_error_code_match;
9847 return inequality ^ bit;
9850 static void vmx_inject_page_fault_nested(struct kvm_vcpu *vcpu,
9851 struct x86_exception *fault)
9853 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
9855 WARN_ON(!is_guest_mode(vcpu));
9857 if (nested_vmx_is_page_fault_vmexit(vmcs12, fault->error_code) &&
9858 !to_vmx(vcpu)->nested.nested_run_pending) {
9859 vmcs12->vm_exit_intr_error_code = fault->error_code;
9860 nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI,
9861 PF_VECTOR | INTR_TYPE_HARD_EXCEPTION |
9862 INTR_INFO_DELIVER_CODE_MASK | INTR_INFO_VALID_MASK,
9865 kvm_inject_page_fault(vcpu, fault);
9869 static inline bool nested_vmx_prepare_msr_bitmap(struct kvm_vcpu *vcpu,
9870 struct vmcs12 *vmcs12);
9872 static void nested_get_vmcs12_pages(struct kvm_vcpu *vcpu,
9873 struct vmcs12 *vmcs12)
9875 struct vcpu_vmx *vmx = to_vmx(vcpu);
9879 if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) {
9881 * Translate L1 physical address to host physical
9882 * address for vmcs02. Keep the page pinned, so this
9883 * physical address remains valid. We keep a reference
9884 * to it so we can release it later.
9886 if (vmx->nested.apic_access_page) { /* shouldn't happen */
9887 kvm_release_page_dirty(vmx->nested.apic_access_page);
9888 vmx->nested.apic_access_page = NULL;
9890 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->apic_access_addr);
9892 * If translation failed, no matter: This feature asks
9893 * to exit when accessing the given address, and if it
9894 * can never be accessed, this feature won't do
9897 if (!is_error_page(page)) {
9898 vmx->nested.apic_access_page = page;
9899 hpa = page_to_phys(vmx->nested.apic_access_page);
9900 vmcs_write64(APIC_ACCESS_ADDR, hpa);
9902 vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL,
9903 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES);
9905 } else if (!(nested_cpu_has_virt_x2apic_mode(vmcs12)) &&
9906 cpu_need_virtualize_apic_accesses(&vmx->vcpu)) {
9907 vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL,
9908 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES);
9909 kvm_vcpu_reload_apic_access_page(vcpu);
9912 if (nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW)) {
9913 if (vmx->nested.virtual_apic_page) { /* shouldn't happen */
9914 kvm_release_page_dirty(vmx->nested.virtual_apic_page);
9915 vmx->nested.virtual_apic_page = NULL;
9917 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->virtual_apic_page_addr);
9920 * If translation failed, VM entry will fail because
9921 * prepare_vmcs02 set VIRTUAL_APIC_PAGE_ADDR to -1ull.
9922 * Failing the vm entry is _not_ what the processor
9923 * does but it's basically the only possibility we
9924 * have. We could still enter the guest if CR8 load
9925 * exits are enabled, CR8 store exits are enabled, and
9926 * virtualize APIC access is disabled; in this case
9927 * the processor would never use the TPR shadow and we
9928 * could simply clear the bit from the execution
9929 * control. But such a configuration is useless, so
9930 * let's keep the code simple.
9932 if (!is_error_page(page)) {
9933 vmx->nested.virtual_apic_page = page;
9934 hpa = page_to_phys(vmx->nested.virtual_apic_page);
9935 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, hpa);
9939 if (nested_cpu_has_posted_intr(vmcs12)) {
9940 if (vmx->nested.pi_desc_page) { /* shouldn't happen */
9941 kunmap(vmx->nested.pi_desc_page);
9942 kvm_release_page_dirty(vmx->nested.pi_desc_page);
9943 vmx->nested.pi_desc_page = NULL;
9945 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->posted_intr_desc_addr);
9946 if (is_error_page(page))
9948 vmx->nested.pi_desc_page = page;
9949 vmx->nested.pi_desc = kmap(vmx->nested.pi_desc_page);
9950 vmx->nested.pi_desc =
9951 (struct pi_desc *)((void *)vmx->nested.pi_desc +
9952 (unsigned long)(vmcs12->posted_intr_desc_addr &
9954 vmcs_write64(POSTED_INTR_DESC_ADDR,
9955 page_to_phys(vmx->nested.pi_desc_page) +
9956 (unsigned long)(vmcs12->posted_intr_desc_addr &
9959 if (!nested_vmx_prepare_msr_bitmap(vcpu, vmcs12))
9960 vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL,
9961 CPU_BASED_USE_MSR_BITMAPS);
9964 static void vmx_start_preemption_timer(struct kvm_vcpu *vcpu)
9966 u64 preemption_timeout = get_vmcs12(vcpu)->vmx_preemption_timer_value;
9967 struct vcpu_vmx *vmx = to_vmx(vcpu);
9969 if (vcpu->arch.virtual_tsc_khz == 0)
9972 /* Make sure short timeouts reliably trigger an immediate vmexit.
9973 * hrtimer_start does not guarantee this. */
9974 if (preemption_timeout <= 1) {
9975 vmx_preemption_timer_fn(&vmx->nested.preemption_timer);
9979 preemption_timeout <<= VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE;
9980 preemption_timeout *= 1000000;
9981 do_div(preemption_timeout, vcpu->arch.virtual_tsc_khz);
9982 hrtimer_start(&vmx->nested.preemption_timer,
9983 ns_to_ktime(preemption_timeout), HRTIMER_MODE_REL);
9986 static int nested_vmx_check_io_bitmap_controls(struct kvm_vcpu *vcpu,
9987 struct vmcs12 *vmcs12)
9989 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_IO_BITMAPS))
9992 if (!page_address_valid(vcpu, vmcs12->io_bitmap_a) ||
9993 !page_address_valid(vcpu, vmcs12->io_bitmap_b))
9999 static int nested_vmx_check_msr_bitmap_controls(struct kvm_vcpu *vcpu,
10000 struct vmcs12 *vmcs12)
10002 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS))
10005 if (!page_address_valid(vcpu, vmcs12->msr_bitmap))
10011 static int nested_vmx_check_tpr_shadow_controls(struct kvm_vcpu *vcpu,
10012 struct vmcs12 *vmcs12)
10014 if (!nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW))
10017 if (!page_address_valid(vcpu, vmcs12->virtual_apic_page_addr))
10024 * Merge L0's and L1's MSR bitmap, return false to indicate that
10025 * we do not use the hardware.
10027 static inline bool nested_vmx_prepare_msr_bitmap(struct kvm_vcpu *vcpu,
10028 struct vmcs12 *vmcs12)
10032 unsigned long *msr_bitmap_l1;
10033 unsigned long *msr_bitmap_l0 = to_vmx(vcpu)->nested.msr_bitmap;
10035 /* Nothing to do if the MSR bitmap is not in use. */
10036 if (!cpu_has_vmx_msr_bitmap() ||
10037 !nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS))
10040 /* This shortcut is ok because we support only x2APIC MSRs so far. */
10041 if (!nested_cpu_has_virt_x2apic_mode(vmcs12))
10044 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->msr_bitmap);
10045 if (is_error_page(page))
10048 msr_bitmap_l1 = (unsigned long *)kmap(page);
10049 if (nested_cpu_has_apic_reg_virt(vmcs12)) {
10051 * L0 need not intercept reads for MSRs between 0x800 and 0x8ff, it
10052 * just lets the processor take the value from the virtual-APIC page;
10053 * take those 256 bits directly from the L1 bitmap.
10055 for (msr = 0x800; msr <= 0x8ff; msr += BITS_PER_LONG) {
10056 unsigned word = msr / BITS_PER_LONG;
10057 msr_bitmap_l0[word] = msr_bitmap_l1[word];
10058 msr_bitmap_l0[word + (0x800 / sizeof(long))] = ~0;
10061 for (msr = 0x800; msr <= 0x8ff; msr += BITS_PER_LONG) {
10062 unsigned word = msr / BITS_PER_LONG;
10063 msr_bitmap_l0[word] = ~0;
10064 msr_bitmap_l0[word + (0x800 / sizeof(long))] = ~0;
10068 nested_vmx_disable_intercept_for_msr(
10069 msr_bitmap_l1, msr_bitmap_l0,
10070 X2APIC_MSR(APIC_TASKPRI),
10073 if (nested_cpu_has_vid(vmcs12)) {
10074 nested_vmx_disable_intercept_for_msr(
10075 msr_bitmap_l1, msr_bitmap_l0,
10076 X2APIC_MSR(APIC_EOI),
10078 nested_vmx_disable_intercept_for_msr(
10079 msr_bitmap_l1, msr_bitmap_l0,
10080 X2APIC_MSR(APIC_SELF_IPI),
10084 kvm_release_page_clean(page);
10089 static int nested_vmx_check_apicv_controls(struct kvm_vcpu *vcpu,
10090 struct vmcs12 *vmcs12)
10092 if (!nested_cpu_has_virt_x2apic_mode(vmcs12) &&
10093 !nested_cpu_has_apic_reg_virt(vmcs12) &&
10094 !nested_cpu_has_vid(vmcs12) &&
10095 !nested_cpu_has_posted_intr(vmcs12))
10099 * If virtualize x2apic mode is enabled,
10100 * virtualize apic access must be disabled.
10102 if (nested_cpu_has_virt_x2apic_mode(vmcs12) &&
10103 nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES))
10107 * If virtual interrupt delivery is enabled,
10108 * we must exit on external interrupts.
10110 if (nested_cpu_has_vid(vmcs12) &&
10111 !nested_exit_on_intr(vcpu))
10115 * bits 15:8 should be zero in posted_intr_nv,
10116 * the descriptor address has been already checked
10117 * in nested_get_vmcs12_pages.
10119 if (nested_cpu_has_posted_intr(vmcs12) &&
10120 (!nested_cpu_has_vid(vmcs12) ||
10121 !nested_exit_intr_ack_set(vcpu) ||
10122 vmcs12->posted_intr_nv & 0xff00))
10125 /* tpr shadow is needed by all apicv features. */
10126 if (!nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW))
10132 static int nested_vmx_check_msr_switch(struct kvm_vcpu *vcpu,
10133 unsigned long count_field,
10134 unsigned long addr_field)
10139 if (vmcs12_read_any(vcpu, count_field, &count) ||
10140 vmcs12_read_any(vcpu, addr_field, &addr)) {
10146 maxphyaddr = cpuid_maxphyaddr(vcpu);
10147 if (!IS_ALIGNED(addr, 16) || addr >> maxphyaddr ||
10148 (addr + count * sizeof(struct vmx_msr_entry) - 1) >> maxphyaddr) {
10149 pr_debug_ratelimited(
10150 "nVMX: invalid MSR switch (0x%lx, %d, %llu, 0x%08llx)",
10151 addr_field, maxphyaddr, count, addr);
10157 static int nested_vmx_check_msr_switch_controls(struct kvm_vcpu *vcpu,
10158 struct vmcs12 *vmcs12)
10160 if (vmcs12->vm_exit_msr_load_count == 0 &&
10161 vmcs12->vm_exit_msr_store_count == 0 &&
10162 vmcs12->vm_entry_msr_load_count == 0)
10163 return 0; /* Fast path */
10164 if (nested_vmx_check_msr_switch(vcpu, VM_EXIT_MSR_LOAD_COUNT,
10165 VM_EXIT_MSR_LOAD_ADDR) ||
10166 nested_vmx_check_msr_switch(vcpu, VM_EXIT_MSR_STORE_COUNT,
10167 VM_EXIT_MSR_STORE_ADDR) ||
10168 nested_vmx_check_msr_switch(vcpu, VM_ENTRY_MSR_LOAD_COUNT,
10169 VM_ENTRY_MSR_LOAD_ADDR))
10174 static int nested_vmx_check_pml_controls(struct kvm_vcpu *vcpu,
10175 struct vmcs12 *vmcs12)
10177 u64 address = vmcs12->pml_address;
10178 int maxphyaddr = cpuid_maxphyaddr(vcpu);
10180 if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_PML)) {
10181 if (!nested_cpu_has_ept(vmcs12) ||
10182 !IS_ALIGNED(address, 4096) ||
10183 address >> maxphyaddr)
10190 static int nested_vmx_msr_check_common(struct kvm_vcpu *vcpu,
10191 struct vmx_msr_entry *e)
10193 /* x2APIC MSR accesses are not allowed */
10194 if (vcpu->arch.apic_base & X2APIC_ENABLE && e->index >> 8 == 0x8)
10196 if (e->index == MSR_IA32_UCODE_WRITE || /* SDM Table 35-2 */
10197 e->index == MSR_IA32_UCODE_REV)
10199 if (e->reserved != 0)
10204 static int nested_vmx_load_msr_check(struct kvm_vcpu *vcpu,
10205 struct vmx_msr_entry *e)
10207 if (e->index == MSR_FS_BASE ||
10208 e->index == MSR_GS_BASE ||
10209 e->index == MSR_IA32_SMM_MONITOR_CTL || /* SMM is not supported */
10210 nested_vmx_msr_check_common(vcpu, e))
10215 static int nested_vmx_store_msr_check(struct kvm_vcpu *vcpu,
10216 struct vmx_msr_entry *e)
10218 if (e->index == MSR_IA32_SMBASE || /* SMM is not supported */
10219 nested_vmx_msr_check_common(vcpu, e))
10225 * Load guest's/host's msr at nested entry/exit.
10226 * return 0 for success, entry index for failure.
10228 static u32 nested_vmx_load_msr(struct kvm_vcpu *vcpu, u64 gpa, u32 count)
10231 struct vmx_msr_entry e;
10232 struct msr_data msr;
10234 msr.host_initiated = false;
10235 for (i = 0; i < count; i++) {
10236 if (kvm_vcpu_read_guest(vcpu, gpa + i * sizeof(e),
10238 pr_debug_ratelimited(
10239 "%s cannot read MSR entry (%u, 0x%08llx)\n",
10240 __func__, i, gpa + i * sizeof(e));
10243 if (nested_vmx_load_msr_check(vcpu, &e)) {
10244 pr_debug_ratelimited(
10245 "%s check failed (%u, 0x%x, 0x%x)\n",
10246 __func__, i, e.index, e.reserved);
10249 msr.index = e.index;
10250 msr.data = e.value;
10251 if (kvm_set_msr(vcpu, &msr)) {
10252 pr_debug_ratelimited(
10253 "%s cannot write MSR (%u, 0x%x, 0x%llx)\n",
10254 __func__, i, e.index, e.value);
10263 static int nested_vmx_store_msr(struct kvm_vcpu *vcpu, u64 gpa, u32 count)
10266 struct vmx_msr_entry e;
10268 for (i = 0; i < count; i++) {
10269 struct msr_data msr_info;
10270 if (kvm_vcpu_read_guest(vcpu,
10271 gpa + i * sizeof(e),
10272 &e, 2 * sizeof(u32))) {
10273 pr_debug_ratelimited(
10274 "%s cannot read MSR entry (%u, 0x%08llx)\n",
10275 __func__, i, gpa + i * sizeof(e));
10278 if (nested_vmx_store_msr_check(vcpu, &e)) {
10279 pr_debug_ratelimited(
10280 "%s check failed (%u, 0x%x, 0x%x)\n",
10281 __func__, i, e.index, e.reserved);
10284 msr_info.host_initiated = false;
10285 msr_info.index = e.index;
10286 if (kvm_get_msr(vcpu, &msr_info)) {
10287 pr_debug_ratelimited(
10288 "%s cannot read MSR (%u, 0x%x)\n",
10289 __func__, i, e.index);
10292 if (kvm_vcpu_write_guest(vcpu,
10293 gpa + i * sizeof(e) +
10294 offsetof(struct vmx_msr_entry, value),
10295 &msr_info.data, sizeof(msr_info.data))) {
10296 pr_debug_ratelimited(
10297 "%s cannot write MSR (%u, 0x%x, 0x%llx)\n",
10298 __func__, i, e.index, msr_info.data);
10305 static bool nested_cr3_valid(struct kvm_vcpu *vcpu, unsigned long val)
10307 unsigned long invalid_mask;
10309 invalid_mask = (~0ULL) << cpuid_maxphyaddr(vcpu);
10310 return (val & invalid_mask) == 0;
10314 * Load guest's/host's cr3 at nested entry/exit. nested_ept is true if we are
10315 * emulating VM entry into a guest with EPT enabled.
10316 * Returns 0 on success, 1 on failure. Invalid state exit qualification code
10317 * is assigned to entry_failure_code on failure.
10319 static int nested_vmx_load_cr3(struct kvm_vcpu *vcpu, unsigned long cr3, bool nested_ept,
10320 u32 *entry_failure_code)
10322 if (cr3 != kvm_read_cr3(vcpu) || (!nested_ept && pdptrs_changed(vcpu))) {
10323 if (!nested_cr3_valid(vcpu, cr3)) {
10324 *entry_failure_code = ENTRY_FAIL_DEFAULT;
10329 * If PAE paging and EPT are both on, CR3 is not used by the CPU and
10330 * must not be dereferenced.
10332 if (!is_long_mode(vcpu) && is_pae(vcpu) && is_paging(vcpu) &&
10334 if (!load_pdptrs(vcpu, vcpu->arch.walk_mmu, cr3)) {
10335 *entry_failure_code = ENTRY_FAIL_PDPTE;
10340 vcpu->arch.cr3 = cr3;
10341 __set_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail);
10344 kvm_mmu_reset_context(vcpu);
10348 static void prepare_vmcs02_full(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12,
10351 struct vcpu_vmx *vmx = to_vmx(vcpu);
10353 vmcs_write16(GUEST_ES_SELECTOR, vmcs12->guest_es_selector);
10354 vmcs_write16(GUEST_SS_SELECTOR, vmcs12->guest_ss_selector);
10355 vmcs_write16(GUEST_DS_SELECTOR, vmcs12->guest_ds_selector);
10356 vmcs_write16(GUEST_FS_SELECTOR, vmcs12->guest_fs_selector);
10357 vmcs_write16(GUEST_GS_SELECTOR, vmcs12->guest_gs_selector);
10358 vmcs_write16(GUEST_LDTR_SELECTOR, vmcs12->guest_ldtr_selector);
10359 vmcs_write16(GUEST_TR_SELECTOR, vmcs12->guest_tr_selector);
10360 vmcs_write32(GUEST_ES_LIMIT, vmcs12->guest_es_limit);
10361 vmcs_write32(GUEST_SS_LIMIT, vmcs12->guest_ss_limit);
10362 vmcs_write32(GUEST_DS_LIMIT, vmcs12->guest_ds_limit);
10363 vmcs_write32(GUEST_FS_LIMIT, vmcs12->guest_fs_limit);
10364 vmcs_write32(GUEST_GS_LIMIT, vmcs12->guest_gs_limit);
10365 vmcs_write32(GUEST_LDTR_LIMIT, vmcs12->guest_ldtr_limit);
10366 vmcs_write32(GUEST_TR_LIMIT, vmcs12->guest_tr_limit);
10367 vmcs_write32(GUEST_GDTR_LIMIT, vmcs12->guest_gdtr_limit);
10368 vmcs_write32(GUEST_IDTR_LIMIT, vmcs12->guest_idtr_limit);
10369 vmcs_write32(GUEST_ES_AR_BYTES, vmcs12->guest_es_ar_bytes);
10370 vmcs_write32(GUEST_SS_AR_BYTES, vmcs12->guest_ss_ar_bytes);
10371 vmcs_write32(GUEST_DS_AR_BYTES, vmcs12->guest_ds_ar_bytes);
10372 vmcs_write32(GUEST_FS_AR_BYTES, vmcs12->guest_fs_ar_bytes);
10373 vmcs_write32(GUEST_GS_AR_BYTES, vmcs12->guest_gs_ar_bytes);
10374 vmcs_write32(GUEST_LDTR_AR_BYTES, vmcs12->guest_ldtr_ar_bytes);
10375 vmcs_write32(GUEST_TR_AR_BYTES, vmcs12->guest_tr_ar_bytes);
10376 vmcs_writel(GUEST_SS_BASE, vmcs12->guest_ss_base);
10377 vmcs_writel(GUEST_DS_BASE, vmcs12->guest_ds_base);
10378 vmcs_writel(GUEST_FS_BASE, vmcs12->guest_fs_base);
10379 vmcs_writel(GUEST_GS_BASE, vmcs12->guest_gs_base);
10380 vmcs_writel(GUEST_LDTR_BASE, vmcs12->guest_ldtr_base);
10381 vmcs_writel(GUEST_TR_BASE, vmcs12->guest_tr_base);
10382 vmcs_writel(GUEST_GDTR_BASE, vmcs12->guest_gdtr_base);
10383 vmcs_writel(GUEST_IDTR_BASE, vmcs12->guest_idtr_base);
10385 vmcs_write32(GUEST_SYSENTER_CS, vmcs12->guest_sysenter_cs);
10386 vmcs_writel(GUEST_PENDING_DBG_EXCEPTIONS,
10387 vmcs12->guest_pending_dbg_exceptions);
10388 vmcs_writel(GUEST_SYSENTER_ESP, vmcs12->guest_sysenter_esp);
10389 vmcs_writel(GUEST_SYSENTER_EIP, vmcs12->guest_sysenter_eip);
10391 if (nested_cpu_has_xsaves(vmcs12))
10392 vmcs_write64(XSS_EXIT_BITMAP, vmcs12->xss_exit_bitmap);
10393 vmcs_write64(VMCS_LINK_POINTER, -1ull);
10395 if (cpu_has_vmx_posted_intr())
10396 vmcs_write16(POSTED_INTR_NV, POSTED_INTR_NESTED_VECTOR);
10399 * Whether page-faults are trapped is determined by a combination of
10400 * 3 settings: PFEC_MASK, PFEC_MATCH and EXCEPTION_BITMAP.PF.
10401 * If enable_ept, L0 doesn't care about page faults and we should
10402 * set all of these to L1's desires. However, if !enable_ept, L0 does
10403 * care about (at least some) page faults, and because it is not easy
10404 * (if at all possible?) to merge L0 and L1's desires, we simply ask
10405 * to exit on each and every L2 page fault. This is done by setting
10406 * MASK=MATCH=0 and (see below) EB.PF=1.
10407 * Note that below we don't need special code to set EB.PF beyond the
10408 * "or"ing of the EB of vmcs01 and vmcs12, because when enable_ept,
10409 * vmcs01's EB.PF is 0 so the "or" will take vmcs12's value, and when
10410 * !enable_ept, EB.PF is 1, so the "or" will always be 1.
10412 vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK,
10413 enable_ept ? vmcs12->page_fault_error_code_mask : 0);
10414 vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH,
10415 enable_ept ? vmcs12->page_fault_error_code_match : 0);
10417 /* All VMFUNCs are currently emulated through L0 vmexits. */
10418 if (cpu_has_vmx_vmfunc())
10419 vmcs_write64(VM_FUNCTION_CONTROL, 0);
10421 if (cpu_has_vmx_apicv()) {
10422 vmcs_write64(EOI_EXIT_BITMAP0, vmcs12->eoi_exit_bitmap0);
10423 vmcs_write64(EOI_EXIT_BITMAP1, vmcs12->eoi_exit_bitmap1);
10424 vmcs_write64(EOI_EXIT_BITMAP2, vmcs12->eoi_exit_bitmap2);
10425 vmcs_write64(EOI_EXIT_BITMAP3, vmcs12->eoi_exit_bitmap3);
10429 * Set host-state according to L0's settings (vmcs12 is irrelevant here)
10430 * Some constant fields are set here by vmx_set_constant_host_state().
10431 * Other fields are different per CPU, and will be set later when
10432 * vmx_vcpu_load() is called, and when vmx_save_host_state() is called.
10434 vmx_set_constant_host_state(vmx);
10437 * Set the MSR load/store lists to match L0's settings.
10439 vmcs_write32(VM_EXIT_MSR_STORE_COUNT, 0);
10440 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, vmx->msr_autoload.nr);
10441 vmcs_write64(VM_EXIT_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.host));
10442 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, vmx->msr_autoload.nr);
10443 vmcs_write64(VM_ENTRY_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.guest));
10445 set_cr4_guest_host_mask(vmx);
10447 if (vmx_mpx_supported())
10448 vmcs_write64(GUEST_BNDCFGS, vmcs12->guest_bndcfgs);
10451 if (nested_cpu_has_vpid(vmcs12) && vmx->nested.vpid02)
10452 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->nested.vpid02);
10454 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid);
10458 * L1 may access the L2's PDPTR, so save them to construct vmcs12
10461 vmcs_write64(GUEST_PDPTR0, vmcs12->guest_pdptr0);
10462 vmcs_write64(GUEST_PDPTR1, vmcs12->guest_pdptr1);
10463 vmcs_write64(GUEST_PDPTR2, vmcs12->guest_pdptr2);
10464 vmcs_write64(GUEST_PDPTR3, vmcs12->guest_pdptr3);
10469 * prepare_vmcs02 is called when the L1 guest hypervisor runs its nested
10470 * L2 guest. L1 has a vmcs for L2 (vmcs12), and this function "merges" it
10471 * with L0's requirements for its guest (a.k.a. vmcs01), so we can run the L2
10472 * guest in a way that will both be appropriate to L1's requests, and our
10473 * needs. In addition to modifying the active vmcs (which is vmcs02), this
10474 * function also has additional necessary side-effects, like setting various
10475 * vcpu->arch fields.
10476 * Returns 0 on success, 1 on failure. Invalid state exit qualification code
10477 * is assigned to entry_failure_code on failure.
10479 static int prepare_vmcs02(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12,
10480 bool from_vmentry, u32 *entry_failure_code)
10482 struct vcpu_vmx *vmx = to_vmx(vcpu);
10483 u32 exec_control, vmcs12_exec_ctrl;
10486 * First, the fields that are shadowed. This must be kept in sync
10487 * with vmx_shadow_fields.h.
10490 vmcs_write16(GUEST_CS_SELECTOR, vmcs12->guest_cs_selector);
10491 vmcs_write32(GUEST_CS_LIMIT, vmcs12->guest_cs_limit);
10492 vmcs_write32(GUEST_CS_AR_BYTES, vmcs12->guest_cs_ar_bytes);
10493 vmcs_writel(GUEST_ES_BASE, vmcs12->guest_es_base);
10494 vmcs_writel(GUEST_CS_BASE, vmcs12->guest_cs_base);
10497 * Not in vmcs02: GUEST_PML_INDEX, HOST_FS_SELECTOR, HOST_GS_SELECTOR,
10498 * HOST_FS_BASE, HOST_GS_BASE.
10501 if (from_vmentry &&
10502 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS)) {
10503 kvm_set_dr(vcpu, 7, vmcs12->guest_dr7);
10504 vmcs_write64(GUEST_IA32_DEBUGCTL, vmcs12->guest_ia32_debugctl);
10506 kvm_set_dr(vcpu, 7, vcpu->arch.dr7);
10507 vmcs_write64(GUEST_IA32_DEBUGCTL, vmx->nested.vmcs01_debugctl);
10509 if (from_vmentry) {
10510 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
10511 vmcs12->vm_entry_intr_info_field);
10512 vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE,
10513 vmcs12->vm_entry_exception_error_code);
10514 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
10515 vmcs12->vm_entry_instruction_len);
10516 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO,
10517 vmcs12->guest_interruptibility_info);
10518 vmx->loaded_vmcs->nmi_known_unmasked =
10519 !(vmcs12->guest_interruptibility_info & GUEST_INTR_STATE_NMI);
10521 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0);
10523 vmx_set_rflags(vcpu, vmcs12->guest_rflags);
10525 exec_control = vmcs12->pin_based_vm_exec_control;
10527 /* Preemption timer setting is only taken from vmcs01. */
10528 exec_control &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
10529 exec_control |= vmcs_config.pin_based_exec_ctrl;
10530 if (vmx->hv_deadline_tsc == -1)
10531 exec_control &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
10533 /* Posted interrupts setting is only taken from vmcs12. */
10534 if (nested_cpu_has_posted_intr(vmcs12)) {
10535 vmx->nested.posted_intr_nv = vmcs12->posted_intr_nv;
10536 vmx->nested.pi_pending = false;
10538 exec_control &= ~PIN_BASED_POSTED_INTR;
10541 vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, exec_control);
10543 vmx->nested.preemption_timer_expired = false;
10544 if (nested_cpu_has_preemption_timer(vmcs12))
10545 vmx_start_preemption_timer(vcpu);
10547 if (cpu_has_secondary_exec_ctrls()) {
10548 exec_control = vmx->secondary_exec_control;
10550 /* Take the following fields only from vmcs12 */
10551 exec_control &= ~(SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
10552 SECONDARY_EXEC_ENABLE_INVPCID |
10553 SECONDARY_EXEC_RDTSCP |
10554 SECONDARY_EXEC_XSAVES |
10555 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY |
10556 SECONDARY_EXEC_APIC_REGISTER_VIRT |
10557 SECONDARY_EXEC_ENABLE_VMFUNC);
10558 if (nested_cpu_has(vmcs12,
10559 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS)) {
10560 vmcs12_exec_ctrl = vmcs12->secondary_vm_exec_control &
10561 ~SECONDARY_EXEC_ENABLE_PML;
10562 exec_control |= vmcs12_exec_ctrl;
10565 if (exec_control & SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY)
10566 vmcs_write16(GUEST_INTR_STATUS,
10567 vmcs12->guest_intr_status);
10570 * Write an illegal value to APIC_ACCESS_ADDR. Later,
10571 * nested_get_vmcs12_pages will either fix it up or
10572 * remove the VM execution control.
10574 if (exec_control & SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)
10575 vmcs_write64(APIC_ACCESS_ADDR, -1ull);
10577 vmcs_write32(SECONDARY_VM_EXEC_CONTROL, exec_control);
10581 * HOST_RSP is normally set correctly in vmx_vcpu_run() just before
10582 * entry, but only if the current (host) sp changed from the value
10583 * we wrote last (vmx->host_rsp). This cache is no longer relevant
10584 * if we switch vmcs, and rather than hold a separate cache per vmcs,
10585 * here we just force the write to happen on entry.
10589 exec_control = vmx_exec_control(vmx); /* L0's desires */
10590 exec_control &= ~CPU_BASED_VIRTUAL_INTR_PENDING;
10591 exec_control &= ~CPU_BASED_VIRTUAL_NMI_PENDING;
10592 exec_control &= ~CPU_BASED_TPR_SHADOW;
10593 exec_control |= vmcs12->cpu_based_vm_exec_control;
10596 * Write an illegal value to VIRTUAL_APIC_PAGE_ADDR. Later, if
10597 * nested_get_vmcs12_pages can't fix it up, the illegal value
10598 * will result in a VM entry failure.
10600 if (exec_control & CPU_BASED_TPR_SHADOW) {
10601 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, -1ull);
10602 vmcs_write32(TPR_THRESHOLD, vmcs12->tpr_threshold);
10604 #ifdef CONFIG_X86_64
10605 exec_control |= CPU_BASED_CR8_LOAD_EXITING |
10606 CPU_BASED_CR8_STORE_EXITING;
10611 * A vmexit (to either L1 hypervisor or L0 userspace) is always needed
10612 * for I/O port accesses.
10614 exec_control &= ~CPU_BASED_USE_IO_BITMAPS;
10615 exec_control |= CPU_BASED_UNCOND_IO_EXITING;
10617 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, exec_control);
10619 /* EXCEPTION_BITMAP and CR0_GUEST_HOST_MASK should basically be the
10620 * bitwise-or of what L1 wants to trap for L2, and what we want to
10621 * trap. Note that CR0.TS also needs updating - we do this later.
10623 update_exception_bitmap(vcpu);
10624 vcpu->arch.cr0_guest_owned_bits &= ~vmcs12->cr0_guest_host_mask;
10625 vmcs_writel(CR0_GUEST_HOST_MASK, ~vcpu->arch.cr0_guest_owned_bits);
10627 /* L2->L1 exit controls are emulated - the hardware exit is to L0 so
10628 * we should use its exit controls. Note that VM_EXIT_LOAD_IA32_EFER
10629 * bits are further modified by vmx_set_efer() below.
10631 vmcs_write32(VM_EXIT_CONTROLS, vmcs_config.vmexit_ctrl);
10633 /* vmcs12's VM_ENTRY_LOAD_IA32_EFER and VM_ENTRY_IA32E_MODE are
10634 * emulated by vmx_set_efer(), below.
10636 vm_entry_controls_init(vmx,
10637 (vmcs12->vm_entry_controls & ~VM_ENTRY_LOAD_IA32_EFER &
10638 ~VM_ENTRY_IA32E_MODE) |
10639 (vmcs_config.vmentry_ctrl & ~VM_ENTRY_IA32E_MODE));
10641 if (from_vmentry &&
10642 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_PAT)) {
10643 vmcs_write64(GUEST_IA32_PAT, vmcs12->guest_ia32_pat);
10644 vcpu->arch.pat = vmcs12->guest_ia32_pat;
10645 } else if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) {
10646 vmcs_write64(GUEST_IA32_PAT, vmx->vcpu.arch.pat);
10649 if (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING)
10650 vmcs_write64(TSC_OFFSET,
10651 vcpu->arch.tsc_offset + vmcs12->tsc_offset);
10653 vmcs_write64(TSC_OFFSET, vcpu->arch.tsc_offset);
10654 if (kvm_has_tsc_control)
10655 decache_tsc_multiplier(vmx);
10659 * There is no direct mapping between vpid02 and vpid12, the
10660 * vpid02 is per-vCPU for L0 and reused while the value of
10661 * vpid12 is changed w/ one invvpid during nested vmentry.
10662 * The vpid12 is allocated by L1 for L2, so it will not
10663 * influence global bitmap(for vpid01 and vpid02 allocation)
10664 * even if spawn a lot of nested vCPUs.
10666 if (nested_cpu_has_vpid(vmcs12) && vmx->nested.vpid02) {
10667 if (vmcs12->virtual_processor_id != vmx->nested.last_vpid) {
10668 vmx->nested.last_vpid = vmcs12->virtual_processor_id;
10669 __vmx_flush_tlb(vcpu, to_vmx(vcpu)->nested.vpid02, true);
10672 vmx_flush_tlb(vcpu, true);
10678 * Conceptually we want to copy the PML address and index from
10679 * vmcs01 here, and then back to vmcs01 on nested vmexit. But,
10680 * since we always flush the log on each vmexit, this happens
10681 * to be equivalent to simply resetting the fields in vmcs02.
10683 ASSERT(vmx->pml_pg);
10684 vmcs_write64(PML_ADDRESS, page_to_phys(vmx->pml_pg));
10685 vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1);
10688 if (nested_cpu_has_ept(vmcs12)) {
10689 if (nested_ept_init_mmu_context(vcpu)) {
10690 *entry_failure_code = ENTRY_FAIL_DEFAULT;
10693 } else if (nested_cpu_has2(vmcs12,
10694 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) {
10695 vmx_flush_tlb_ept_only(vcpu);
10699 * This sets GUEST_CR0 to vmcs12->guest_cr0, possibly modifying those
10700 * bits which we consider mandatory enabled.
10701 * The CR0_READ_SHADOW is what L2 should have expected to read given
10702 * the specifications by L1; It's not enough to take
10703 * vmcs12->cr0_read_shadow because on our cr0_guest_host_mask we we
10704 * have more bits than L1 expected.
10706 vmx_set_cr0(vcpu, vmcs12->guest_cr0);
10707 vmcs_writel(CR0_READ_SHADOW, nested_read_cr0(vmcs12));
10709 vmx_set_cr4(vcpu, vmcs12->guest_cr4);
10710 vmcs_writel(CR4_READ_SHADOW, nested_read_cr4(vmcs12));
10712 if (from_vmentry &&
10713 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_EFER))
10714 vcpu->arch.efer = vmcs12->guest_ia32_efer;
10715 else if (vmcs12->vm_entry_controls & VM_ENTRY_IA32E_MODE)
10716 vcpu->arch.efer |= (EFER_LMA | EFER_LME);
10718 vcpu->arch.efer &= ~(EFER_LMA | EFER_LME);
10719 /* Note: modifies VM_ENTRY/EXIT_CONTROLS and GUEST/HOST_IA32_EFER */
10720 vmx_set_efer(vcpu, vcpu->arch.efer);
10722 if (vmx->nested.dirty_vmcs12) {
10723 prepare_vmcs02_full(vcpu, vmcs12, from_vmentry);
10724 vmx->nested.dirty_vmcs12 = false;
10727 /* Shadow page tables on either EPT or shadow page tables. */
10728 if (nested_vmx_load_cr3(vcpu, vmcs12->guest_cr3, nested_cpu_has_ept(vmcs12),
10729 entry_failure_code))
10733 vcpu->arch.walk_mmu->inject_page_fault = vmx_inject_page_fault_nested;
10735 kvm_register_write(vcpu, VCPU_REGS_RSP, vmcs12->guest_rsp);
10736 kvm_register_write(vcpu, VCPU_REGS_RIP, vmcs12->guest_rip);
10740 static int check_vmentry_prereqs(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
10742 struct vcpu_vmx *vmx = to_vmx(vcpu);
10744 if (vmcs12->guest_activity_state != GUEST_ACTIVITY_ACTIVE &&
10745 vmcs12->guest_activity_state != GUEST_ACTIVITY_HLT)
10746 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
10748 if (nested_vmx_check_io_bitmap_controls(vcpu, vmcs12))
10749 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
10751 if (nested_vmx_check_msr_bitmap_controls(vcpu, vmcs12))
10752 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
10754 if (nested_vmx_check_tpr_shadow_controls(vcpu, vmcs12))
10755 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
10757 if (nested_vmx_check_apicv_controls(vcpu, vmcs12))
10758 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
10760 if (nested_vmx_check_msr_switch_controls(vcpu, vmcs12))
10761 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
10763 if (nested_vmx_check_pml_controls(vcpu, vmcs12))
10764 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
10766 if (!vmx_control_verify(vmcs12->cpu_based_vm_exec_control,
10767 vmx->nested.nested_vmx_procbased_ctls_low,
10768 vmx->nested.nested_vmx_procbased_ctls_high) ||
10769 (nested_cpu_has(vmcs12, CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) &&
10770 !vmx_control_verify(vmcs12->secondary_vm_exec_control,
10771 vmx->nested.nested_vmx_secondary_ctls_low,
10772 vmx->nested.nested_vmx_secondary_ctls_high)) ||
10773 !vmx_control_verify(vmcs12->pin_based_vm_exec_control,
10774 vmx->nested.nested_vmx_pinbased_ctls_low,
10775 vmx->nested.nested_vmx_pinbased_ctls_high) ||
10776 !vmx_control_verify(vmcs12->vm_exit_controls,
10777 vmx->nested.nested_vmx_exit_ctls_low,
10778 vmx->nested.nested_vmx_exit_ctls_high) ||
10779 !vmx_control_verify(vmcs12->vm_entry_controls,
10780 vmx->nested.nested_vmx_entry_ctls_low,
10781 vmx->nested.nested_vmx_entry_ctls_high))
10782 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
10784 if (nested_cpu_has_vmfunc(vmcs12)) {
10785 if (vmcs12->vm_function_control &
10786 ~vmx->nested.nested_vmx_vmfunc_controls)
10787 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
10789 if (nested_cpu_has_eptp_switching(vmcs12)) {
10790 if (!nested_cpu_has_ept(vmcs12) ||
10791 !page_address_valid(vcpu, vmcs12->eptp_list_address))
10792 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
10796 if (vmcs12->cr3_target_count > nested_cpu_vmx_misc_cr3_count(vcpu))
10797 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
10799 if (!nested_host_cr0_valid(vcpu, vmcs12->host_cr0) ||
10800 !nested_host_cr4_valid(vcpu, vmcs12->host_cr4) ||
10801 !nested_cr3_valid(vcpu, vmcs12->host_cr3))
10802 return VMXERR_ENTRY_INVALID_HOST_STATE_FIELD;
10807 static int check_vmentry_postreqs(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12,
10812 *exit_qual = ENTRY_FAIL_DEFAULT;
10814 if (!nested_guest_cr0_valid(vcpu, vmcs12->guest_cr0) ||
10815 !nested_guest_cr4_valid(vcpu, vmcs12->guest_cr4))
10818 if (!nested_cpu_has2(vmcs12, SECONDARY_EXEC_SHADOW_VMCS) &&
10819 vmcs12->vmcs_link_pointer != -1ull) {
10820 *exit_qual = ENTRY_FAIL_VMCS_LINK_PTR;
10825 * If the load IA32_EFER VM-entry control is 1, the following checks
10826 * are performed on the field for the IA32_EFER MSR:
10827 * - Bits reserved in the IA32_EFER MSR must be 0.
10828 * - Bit 10 (corresponding to IA32_EFER.LMA) must equal the value of
10829 * the IA-32e mode guest VM-exit control. It must also be identical
10830 * to bit 8 (LME) if bit 31 in the CR0 field (corresponding to
10833 if (to_vmx(vcpu)->nested.nested_run_pending &&
10834 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_EFER)) {
10835 ia32e = (vmcs12->vm_entry_controls & VM_ENTRY_IA32E_MODE) != 0;
10836 if (!kvm_valid_efer(vcpu, vmcs12->guest_ia32_efer) ||
10837 ia32e != !!(vmcs12->guest_ia32_efer & EFER_LMA) ||
10838 ((vmcs12->guest_cr0 & X86_CR0_PG) &&
10839 ia32e != !!(vmcs12->guest_ia32_efer & EFER_LME)))
10844 * If the load IA32_EFER VM-exit control is 1, bits reserved in the
10845 * IA32_EFER MSR must be 0 in the field for that register. In addition,
10846 * the values of the LMA and LME bits in the field must each be that of
10847 * the host address-space size VM-exit control.
10849 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_EFER) {
10850 ia32e = (vmcs12->vm_exit_controls &
10851 VM_EXIT_HOST_ADDR_SPACE_SIZE) != 0;
10852 if (!kvm_valid_efer(vcpu, vmcs12->host_ia32_efer) ||
10853 ia32e != !!(vmcs12->host_ia32_efer & EFER_LMA) ||
10854 ia32e != !!(vmcs12->host_ia32_efer & EFER_LME))
10858 if ((vmcs12->vm_entry_controls & VM_ENTRY_LOAD_BNDCFGS) &&
10859 (is_noncanonical_address(vmcs12->guest_bndcfgs & PAGE_MASK, vcpu) ||
10860 (vmcs12->guest_bndcfgs & MSR_IA32_BNDCFGS_RSVD)))
10866 static int enter_vmx_non_root_mode(struct kvm_vcpu *vcpu, bool from_vmentry)
10868 struct vcpu_vmx *vmx = to_vmx(vcpu);
10869 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
10873 enter_guest_mode(vcpu);
10875 if (!(vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS))
10876 vmx->nested.vmcs01_debugctl = vmcs_read64(GUEST_IA32_DEBUGCTL);
10878 vmx_switch_vmcs(vcpu, &vmx->nested.vmcs02);
10879 vmx_segment_cache_clear(vmx);
10881 if (prepare_vmcs02(vcpu, vmcs12, from_vmentry, &exit_qual)) {
10882 leave_guest_mode(vcpu);
10883 vmx_switch_vmcs(vcpu, &vmx->vmcs01);
10884 nested_vmx_entry_failure(vcpu, vmcs12,
10885 EXIT_REASON_INVALID_STATE, exit_qual);
10889 nested_get_vmcs12_pages(vcpu, vmcs12);
10891 msr_entry_idx = nested_vmx_load_msr(vcpu,
10892 vmcs12->vm_entry_msr_load_addr,
10893 vmcs12->vm_entry_msr_load_count);
10894 if (msr_entry_idx) {
10895 leave_guest_mode(vcpu);
10896 vmx_switch_vmcs(vcpu, &vmx->vmcs01);
10897 nested_vmx_entry_failure(vcpu, vmcs12,
10898 EXIT_REASON_MSR_LOAD_FAIL, msr_entry_idx);
10903 * Note no nested_vmx_succeed or nested_vmx_fail here. At this point
10904 * we are no longer running L1, and VMLAUNCH/VMRESUME has not yet
10905 * returned as far as L1 is concerned. It will only return (and set
10906 * the success flag) when L2 exits (see nested_vmx_vmexit()).
10912 * nested_vmx_run() handles a nested entry, i.e., a VMLAUNCH or VMRESUME on L1
10913 * for running an L2 nested guest.
10915 static int nested_vmx_run(struct kvm_vcpu *vcpu, bool launch)
10917 struct vmcs12 *vmcs12;
10918 struct vcpu_vmx *vmx = to_vmx(vcpu);
10919 u32 interrupt_shadow = vmx_get_interrupt_shadow(vcpu);
10923 if (!nested_vmx_check_permission(vcpu))
10926 if (!nested_vmx_check_vmcs12(vcpu))
10929 vmcs12 = get_vmcs12(vcpu);
10931 if (enable_shadow_vmcs)
10932 copy_shadow_to_vmcs12(vmx);
10935 * The nested entry process starts with enforcing various prerequisites
10936 * on vmcs12 as required by the Intel SDM, and act appropriately when
10937 * they fail: As the SDM explains, some conditions should cause the
10938 * instruction to fail, while others will cause the instruction to seem
10939 * to succeed, but return an EXIT_REASON_INVALID_STATE.
10940 * To speed up the normal (success) code path, we should avoid checking
10941 * for misconfigurations which will anyway be caught by the processor
10942 * when using the merged vmcs02.
10944 if (interrupt_shadow & KVM_X86_SHADOW_INT_MOV_SS) {
10945 nested_vmx_failValid(vcpu,
10946 VMXERR_ENTRY_EVENTS_BLOCKED_BY_MOV_SS);
10950 if (vmcs12->launch_state == launch) {
10951 nested_vmx_failValid(vcpu,
10952 launch ? VMXERR_VMLAUNCH_NONCLEAR_VMCS
10953 : VMXERR_VMRESUME_NONLAUNCHED_VMCS);
10957 ret = check_vmentry_prereqs(vcpu, vmcs12);
10959 nested_vmx_failValid(vcpu, ret);
10964 * After this point, the trap flag no longer triggers a singlestep trap
10965 * on the vm entry instructions; don't call kvm_skip_emulated_instruction.
10966 * This is not 100% correct; for performance reasons, we delegate most
10967 * of the checks on host state to the processor. If those fail,
10968 * the singlestep trap is missed.
10970 skip_emulated_instruction(vcpu);
10972 ret = check_vmentry_postreqs(vcpu, vmcs12, &exit_qual);
10974 nested_vmx_entry_failure(vcpu, vmcs12,
10975 EXIT_REASON_INVALID_STATE, exit_qual);
10980 * We're finally done with prerequisite checking, and can start with
10981 * the nested entry.
10984 ret = enter_vmx_non_root_mode(vcpu, true);
10988 if (vmcs12->guest_activity_state == GUEST_ACTIVITY_HLT)
10989 return kvm_vcpu_halt(vcpu);
10991 vmx->nested.nested_run_pending = 1;
10996 return kvm_skip_emulated_instruction(vcpu);
11000 * On a nested exit from L2 to L1, vmcs12.guest_cr0 might not be up-to-date
11001 * because L2 may have changed some cr0 bits directly (CRO_GUEST_HOST_MASK).
11002 * This function returns the new value we should put in vmcs12.guest_cr0.
11003 * It's not enough to just return the vmcs02 GUEST_CR0. Rather,
11004 * 1. Bits that neither L0 nor L1 trapped, were set directly by L2 and are now
11005 * available in vmcs02 GUEST_CR0. (Note: It's enough to check that L0
11006 * didn't trap the bit, because if L1 did, so would L0).
11007 * 2. Bits that L1 asked to trap (and therefore L0 also did) could not have
11008 * been modified by L2, and L1 knows it. So just leave the old value of
11009 * the bit from vmcs12.guest_cr0. Note that the bit from vmcs02 GUEST_CR0
11010 * isn't relevant, because if L0 traps this bit it can set it to anything.
11011 * 3. Bits that L1 didn't trap, but L0 did. L1 believes the guest could have
11012 * changed these bits, and therefore they need to be updated, but L0
11013 * didn't necessarily allow them to be changed in GUEST_CR0 - and rather
11014 * put them in vmcs02 CR0_READ_SHADOW. So take these bits from there.
11016 static inline unsigned long
11017 vmcs12_guest_cr0(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
11020 /*1*/ (vmcs_readl(GUEST_CR0) & vcpu->arch.cr0_guest_owned_bits) |
11021 /*2*/ (vmcs12->guest_cr0 & vmcs12->cr0_guest_host_mask) |
11022 /*3*/ (vmcs_readl(CR0_READ_SHADOW) & ~(vmcs12->cr0_guest_host_mask |
11023 vcpu->arch.cr0_guest_owned_bits));
11026 static inline unsigned long
11027 vmcs12_guest_cr4(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
11030 /*1*/ (vmcs_readl(GUEST_CR4) & vcpu->arch.cr4_guest_owned_bits) |
11031 /*2*/ (vmcs12->guest_cr4 & vmcs12->cr4_guest_host_mask) |
11032 /*3*/ (vmcs_readl(CR4_READ_SHADOW) & ~(vmcs12->cr4_guest_host_mask |
11033 vcpu->arch.cr4_guest_owned_bits));
11036 static void vmcs12_save_pending_event(struct kvm_vcpu *vcpu,
11037 struct vmcs12 *vmcs12)
11042 if (vcpu->arch.exception.injected) {
11043 nr = vcpu->arch.exception.nr;
11044 idt_vectoring = nr | VECTORING_INFO_VALID_MASK;
11046 if (kvm_exception_is_soft(nr)) {
11047 vmcs12->vm_exit_instruction_len =
11048 vcpu->arch.event_exit_inst_len;
11049 idt_vectoring |= INTR_TYPE_SOFT_EXCEPTION;
11051 idt_vectoring |= INTR_TYPE_HARD_EXCEPTION;
11053 if (vcpu->arch.exception.has_error_code) {
11054 idt_vectoring |= VECTORING_INFO_DELIVER_CODE_MASK;
11055 vmcs12->idt_vectoring_error_code =
11056 vcpu->arch.exception.error_code;
11059 vmcs12->idt_vectoring_info_field = idt_vectoring;
11060 } else if (vcpu->arch.nmi_injected) {
11061 vmcs12->idt_vectoring_info_field =
11062 INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK | NMI_VECTOR;
11063 } else if (vcpu->arch.interrupt.pending) {
11064 nr = vcpu->arch.interrupt.nr;
11065 idt_vectoring = nr | VECTORING_INFO_VALID_MASK;
11067 if (vcpu->arch.interrupt.soft) {
11068 idt_vectoring |= INTR_TYPE_SOFT_INTR;
11069 vmcs12->vm_entry_instruction_len =
11070 vcpu->arch.event_exit_inst_len;
11072 idt_vectoring |= INTR_TYPE_EXT_INTR;
11074 vmcs12->idt_vectoring_info_field = idt_vectoring;
11078 static int vmx_check_nested_events(struct kvm_vcpu *vcpu, bool external_intr)
11080 struct vcpu_vmx *vmx = to_vmx(vcpu);
11081 unsigned long exit_qual;
11082 bool block_nested_events =
11083 vmx->nested.nested_run_pending || kvm_event_needs_reinjection(vcpu);
11085 if (vcpu->arch.exception.pending &&
11086 nested_vmx_check_exception(vcpu, &exit_qual)) {
11087 if (block_nested_events)
11089 nested_vmx_inject_exception_vmexit(vcpu, exit_qual);
11093 if (nested_cpu_has_preemption_timer(get_vmcs12(vcpu)) &&
11094 vmx->nested.preemption_timer_expired) {
11095 if (block_nested_events)
11097 nested_vmx_vmexit(vcpu, EXIT_REASON_PREEMPTION_TIMER, 0, 0);
11101 if (vcpu->arch.nmi_pending && nested_exit_on_nmi(vcpu)) {
11102 if (block_nested_events)
11104 nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI,
11105 NMI_VECTOR | INTR_TYPE_NMI_INTR |
11106 INTR_INFO_VALID_MASK, 0);
11108 * The NMI-triggered VM exit counts as injection:
11109 * clear this one and block further NMIs.
11111 vcpu->arch.nmi_pending = 0;
11112 vmx_set_nmi_mask(vcpu, true);
11116 if ((kvm_cpu_has_interrupt(vcpu) || external_intr) &&
11117 nested_exit_on_intr(vcpu)) {
11118 if (block_nested_events)
11120 nested_vmx_vmexit(vcpu, EXIT_REASON_EXTERNAL_INTERRUPT, 0, 0);
11124 vmx_complete_nested_posted_interrupt(vcpu);
11128 static u32 vmx_get_preemption_timer_value(struct kvm_vcpu *vcpu)
11130 ktime_t remaining =
11131 hrtimer_get_remaining(&to_vmx(vcpu)->nested.preemption_timer);
11134 if (ktime_to_ns(remaining) <= 0)
11137 value = ktime_to_ns(remaining) * vcpu->arch.virtual_tsc_khz;
11138 do_div(value, 1000000);
11139 return value >> VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE;
11143 * Update the guest state fields of vmcs12 to reflect changes that
11144 * occurred while L2 was running. (The "IA-32e mode guest" bit of the
11145 * VM-entry controls is also updated, since this is really a guest
11148 static void sync_vmcs12(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
11150 vmcs12->guest_cr0 = vmcs12_guest_cr0(vcpu, vmcs12);
11151 vmcs12->guest_cr4 = vmcs12_guest_cr4(vcpu, vmcs12);
11153 vmcs12->guest_rsp = kvm_register_read(vcpu, VCPU_REGS_RSP);
11154 vmcs12->guest_rip = kvm_register_read(vcpu, VCPU_REGS_RIP);
11155 vmcs12->guest_rflags = vmcs_readl(GUEST_RFLAGS);
11157 vmcs12->guest_es_selector = vmcs_read16(GUEST_ES_SELECTOR);
11158 vmcs12->guest_cs_selector = vmcs_read16(GUEST_CS_SELECTOR);
11159 vmcs12->guest_ss_selector = vmcs_read16(GUEST_SS_SELECTOR);
11160 vmcs12->guest_ds_selector = vmcs_read16(GUEST_DS_SELECTOR);
11161 vmcs12->guest_fs_selector = vmcs_read16(GUEST_FS_SELECTOR);
11162 vmcs12->guest_gs_selector = vmcs_read16(GUEST_GS_SELECTOR);
11163 vmcs12->guest_ldtr_selector = vmcs_read16(GUEST_LDTR_SELECTOR);
11164 vmcs12->guest_tr_selector = vmcs_read16(GUEST_TR_SELECTOR);
11165 vmcs12->guest_es_limit = vmcs_read32(GUEST_ES_LIMIT);
11166 vmcs12->guest_cs_limit = vmcs_read32(GUEST_CS_LIMIT);
11167 vmcs12->guest_ss_limit = vmcs_read32(GUEST_SS_LIMIT);
11168 vmcs12->guest_ds_limit = vmcs_read32(GUEST_DS_LIMIT);
11169 vmcs12->guest_fs_limit = vmcs_read32(GUEST_FS_LIMIT);
11170 vmcs12->guest_gs_limit = vmcs_read32(GUEST_GS_LIMIT);
11171 vmcs12->guest_ldtr_limit = vmcs_read32(GUEST_LDTR_LIMIT);
11172 vmcs12->guest_tr_limit = vmcs_read32(GUEST_TR_LIMIT);
11173 vmcs12->guest_gdtr_limit = vmcs_read32(GUEST_GDTR_LIMIT);
11174 vmcs12->guest_idtr_limit = vmcs_read32(GUEST_IDTR_LIMIT);
11175 vmcs12->guest_es_ar_bytes = vmcs_read32(GUEST_ES_AR_BYTES);
11176 vmcs12->guest_cs_ar_bytes = vmcs_read32(GUEST_CS_AR_BYTES);
11177 vmcs12->guest_ss_ar_bytes = vmcs_read32(GUEST_SS_AR_BYTES);
11178 vmcs12->guest_ds_ar_bytes = vmcs_read32(GUEST_DS_AR_BYTES);
11179 vmcs12->guest_fs_ar_bytes = vmcs_read32(GUEST_FS_AR_BYTES);
11180 vmcs12->guest_gs_ar_bytes = vmcs_read32(GUEST_GS_AR_BYTES);
11181 vmcs12->guest_ldtr_ar_bytes = vmcs_read32(GUEST_LDTR_AR_BYTES);
11182 vmcs12->guest_tr_ar_bytes = vmcs_read32(GUEST_TR_AR_BYTES);
11183 vmcs12->guest_es_base = vmcs_readl(GUEST_ES_BASE);
11184 vmcs12->guest_cs_base = vmcs_readl(GUEST_CS_BASE);
11185 vmcs12->guest_ss_base = vmcs_readl(GUEST_SS_BASE);
11186 vmcs12->guest_ds_base = vmcs_readl(GUEST_DS_BASE);
11187 vmcs12->guest_fs_base = vmcs_readl(GUEST_FS_BASE);
11188 vmcs12->guest_gs_base = vmcs_readl(GUEST_GS_BASE);
11189 vmcs12->guest_ldtr_base = vmcs_readl(GUEST_LDTR_BASE);
11190 vmcs12->guest_tr_base = vmcs_readl(GUEST_TR_BASE);
11191 vmcs12->guest_gdtr_base = vmcs_readl(GUEST_GDTR_BASE);
11192 vmcs12->guest_idtr_base = vmcs_readl(GUEST_IDTR_BASE);
11194 vmcs12->guest_interruptibility_info =
11195 vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
11196 vmcs12->guest_pending_dbg_exceptions =
11197 vmcs_readl(GUEST_PENDING_DBG_EXCEPTIONS);
11198 if (vcpu->arch.mp_state == KVM_MP_STATE_HALTED)
11199 vmcs12->guest_activity_state = GUEST_ACTIVITY_HLT;
11201 vmcs12->guest_activity_state = GUEST_ACTIVITY_ACTIVE;
11203 if (nested_cpu_has_preemption_timer(vmcs12)) {
11204 if (vmcs12->vm_exit_controls &
11205 VM_EXIT_SAVE_VMX_PREEMPTION_TIMER)
11206 vmcs12->vmx_preemption_timer_value =
11207 vmx_get_preemption_timer_value(vcpu);
11208 hrtimer_cancel(&to_vmx(vcpu)->nested.preemption_timer);
11212 * In some cases (usually, nested EPT), L2 is allowed to change its
11213 * own CR3 without exiting. If it has changed it, we must keep it.
11214 * Of course, if L0 is using shadow page tables, GUEST_CR3 was defined
11215 * by L0, not L1 or L2, so we mustn't unconditionally copy it to vmcs12.
11217 * Additionally, restore L2's PDPTR to vmcs12.
11220 vmcs12->guest_cr3 = vmcs_readl(GUEST_CR3);
11221 vmcs12->guest_pdptr0 = vmcs_read64(GUEST_PDPTR0);
11222 vmcs12->guest_pdptr1 = vmcs_read64(GUEST_PDPTR1);
11223 vmcs12->guest_pdptr2 = vmcs_read64(GUEST_PDPTR2);
11224 vmcs12->guest_pdptr3 = vmcs_read64(GUEST_PDPTR3);
11227 vmcs12->guest_linear_address = vmcs_readl(GUEST_LINEAR_ADDRESS);
11229 if (nested_cpu_has_vid(vmcs12))
11230 vmcs12->guest_intr_status = vmcs_read16(GUEST_INTR_STATUS);
11232 vmcs12->vm_entry_controls =
11233 (vmcs12->vm_entry_controls & ~VM_ENTRY_IA32E_MODE) |
11234 (vm_entry_controls_get(to_vmx(vcpu)) & VM_ENTRY_IA32E_MODE);
11236 if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_DEBUG_CONTROLS) {
11237 kvm_get_dr(vcpu, 7, (unsigned long *)&vmcs12->guest_dr7);
11238 vmcs12->guest_ia32_debugctl = vmcs_read64(GUEST_IA32_DEBUGCTL);
11241 /* TODO: These cannot have changed unless we have MSR bitmaps and
11242 * the relevant bit asks not to trap the change */
11243 if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_IA32_PAT)
11244 vmcs12->guest_ia32_pat = vmcs_read64(GUEST_IA32_PAT);
11245 if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_IA32_EFER)
11246 vmcs12->guest_ia32_efer = vcpu->arch.efer;
11247 vmcs12->guest_sysenter_cs = vmcs_read32(GUEST_SYSENTER_CS);
11248 vmcs12->guest_sysenter_esp = vmcs_readl(GUEST_SYSENTER_ESP);
11249 vmcs12->guest_sysenter_eip = vmcs_readl(GUEST_SYSENTER_EIP);
11250 if (kvm_mpx_supported())
11251 vmcs12->guest_bndcfgs = vmcs_read64(GUEST_BNDCFGS);
11255 * prepare_vmcs12 is part of what we need to do when the nested L2 guest exits
11256 * and we want to prepare to run its L1 parent. L1 keeps a vmcs for L2 (vmcs12),
11257 * and this function updates it to reflect the changes to the guest state while
11258 * L2 was running (and perhaps made some exits which were handled directly by L0
11259 * without going back to L1), and to reflect the exit reason.
11260 * Note that we do not have to copy here all VMCS fields, just those that
11261 * could have changed by the L2 guest or the exit - i.e., the guest-state and
11262 * exit-information fields only. Other fields are modified by L1 with VMWRITE,
11263 * which already writes to vmcs12 directly.
11265 static void prepare_vmcs12(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12,
11266 u32 exit_reason, u32 exit_intr_info,
11267 unsigned long exit_qualification)
11269 /* update guest state fields: */
11270 sync_vmcs12(vcpu, vmcs12);
11272 /* update exit information fields: */
11274 vmcs12->vm_exit_reason = exit_reason;
11275 vmcs12->exit_qualification = exit_qualification;
11276 vmcs12->vm_exit_intr_info = exit_intr_info;
11278 vmcs12->idt_vectoring_info_field = 0;
11279 vmcs12->vm_exit_instruction_len = vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
11280 vmcs12->vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
11282 if (!(vmcs12->vm_exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY)) {
11283 vmcs12->launch_state = 1;
11285 /* vm_entry_intr_info_field is cleared on exit. Emulate this
11286 * instead of reading the real value. */
11287 vmcs12->vm_entry_intr_info_field &= ~INTR_INFO_VALID_MASK;
11290 * Transfer the event that L0 or L1 may wanted to inject into
11291 * L2 to IDT_VECTORING_INFO_FIELD.
11293 vmcs12_save_pending_event(vcpu, vmcs12);
11297 * Drop what we picked up for L2 via vmx_complete_interrupts. It is
11298 * preserved above and would only end up incorrectly in L1.
11300 vcpu->arch.nmi_injected = false;
11301 kvm_clear_exception_queue(vcpu);
11302 kvm_clear_interrupt_queue(vcpu);
11305 static void load_vmcs12_mmu_host_state(struct kvm_vcpu *vcpu,
11306 struct vmcs12 *vmcs12)
11308 u32 entry_failure_code;
11310 nested_ept_uninit_mmu_context(vcpu);
11313 * Only PDPTE load can fail as the value of cr3 was checked on entry and
11314 * couldn't have changed.
11316 if (nested_vmx_load_cr3(vcpu, vmcs12->host_cr3, false, &entry_failure_code))
11317 nested_vmx_abort(vcpu, VMX_ABORT_LOAD_HOST_PDPTE_FAIL);
11320 vcpu->arch.walk_mmu->inject_page_fault = kvm_inject_page_fault;
11324 * A part of what we need to when the nested L2 guest exits and we want to
11325 * run its L1 parent, is to reset L1's guest state to the host state specified
11327 * This function is to be called not only on normal nested exit, but also on
11328 * a nested entry failure, as explained in Intel's spec, 3B.23.7 ("VM-Entry
11329 * Failures During or After Loading Guest State").
11330 * This function should be called when the active VMCS is L1's (vmcs01).
11332 static void load_vmcs12_host_state(struct kvm_vcpu *vcpu,
11333 struct vmcs12 *vmcs12)
11335 struct kvm_segment seg;
11337 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_EFER)
11338 vcpu->arch.efer = vmcs12->host_ia32_efer;
11339 else if (vmcs12->vm_exit_controls & VM_EXIT_HOST_ADDR_SPACE_SIZE)
11340 vcpu->arch.efer |= (EFER_LMA | EFER_LME);
11342 vcpu->arch.efer &= ~(EFER_LMA | EFER_LME);
11343 vmx_set_efer(vcpu, vcpu->arch.efer);
11345 kvm_register_write(vcpu, VCPU_REGS_RSP, vmcs12->host_rsp);
11346 kvm_register_write(vcpu, VCPU_REGS_RIP, vmcs12->host_rip);
11347 vmx_set_rflags(vcpu, X86_EFLAGS_FIXED);
11349 * Note that calling vmx_set_cr0 is important, even if cr0 hasn't
11350 * actually changed, because vmx_set_cr0 refers to efer set above.
11352 * CR0_GUEST_HOST_MASK is already set in the original vmcs01
11353 * (KVM doesn't change it);
11355 vcpu->arch.cr0_guest_owned_bits = X86_CR0_TS;
11356 vmx_set_cr0(vcpu, vmcs12->host_cr0);
11358 /* Same as above - no reason to call set_cr4_guest_host_mask(). */
11359 vcpu->arch.cr4_guest_owned_bits = ~vmcs_readl(CR4_GUEST_HOST_MASK);
11360 vmx_set_cr4(vcpu, vmcs12->host_cr4);
11362 load_vmcs12_mmu_host_state(vcpu, vmcs12);
11366 * Trivially support vpid by letting L2s share their parent
11367 * L1's vpid. TODO: move to a more elaborate solution, giving
11368 * each L2 its own vpid and exposing the vpid feature to L1.
11370 vmx_flush_tlb(vcpu, true);
11373 vmcs_write32(GUEST_SYSENTER_CS, vmcs12->host_ia32_sysenter_cs);
11374 vmcs_writel(GUEST_SYSENTER_ESP, vmcs12->host_ia32_sysenter_esp);
11375 vmcs_writel(GUEST_SYSENTER_EIP, vmcs12->host_ia32_sysenter_eip);
11376 vmcs_writel(GUEST_IDTR_BASE, vmcs12->host_idtr_base);
11377 vmcs_writel(GUEST_GDTR_BASE, vmcs12->host_gdtr_base);
11378 vmcs_write32(GUEST_IDTR_LIMIT, 0xFFFF);
11379 vmcs_write32(GUEST_GDTR_LIMIT, 0xFFFF);
11381 /* If not VM_EXIT_CLEAR_BNDCFGS, the L2 value propagates to L1. */
11382 if (vmcs12->vm_exit_controls & VM_EXIT_CLEAR_BNDCFGS)
11383 vmcs_write64(GUEST_BNDCFGS, 0);
11385 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PAT) {
11386 vmcs_write64(GUEST_IA32_PAT, vmcs12->host_ia32_pat);
11387 vcpu->arch.pat = vmcs12->host_ia32_pat;
11389 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL)
11390 vmcs_write64(GUEST_IA32_PERF_GLOBAL_CTRL,
11391 vmcs12->host_ia32_perf_global_ctrl);
11393 /* Set L1 segment info according to Intel SDM
11394 27.5.2 Loading Host Segment and Descriptor-Table Registers */
11395 seg = (struct kvm_segment) {
11397 .limit = 0xFFFFFFFF,
11398 .selector = vmcs12->host_cs_selector,
11404 if (vmcs12->vm_exit_controls & VM_EXIT_HOST_ADDR_SPACE_SIZE)
11408 vmx_set_segment(vcpu, &seg, VCPU_SREG_CS);
11409 seg = (struct kvm_segment) {
11411 .limit = 0xFFFFFFFF,
11418 seg.selector = vmcs12->host_ds_selector;
11419 vmx_set_segment(vcpu, &seg, VCPU_SREG_DS);
11420 seg.selector = vmcs12->host_es_selector;
11421 vmx_set_segment(vcpu, &seg, VCPU_SREG_ES);
11422 seg.selector = vmcs12->host_ss_selector;
11423 vmx_set_segment(vcpu, &seg, VCPU_SREG_SS);
11424 seg.selector = vmcs12->host_fs_selector;
11425 seg.base = vmcs12->host_fs_base;
11426 vmx_set_segment(vcpu, &seg, VCPU_SREG_FS);
11427 seg.selector = vmcs12->host_gs_selector;
11428 seg.base = vmcs12->host_gs_base;
11429 vmx_set_segment(vcpu, &seg, VCPU_SREG_GS);
11430 seg = (struct kvm_segment) {
11431 .base = vmcs12->host_tr_base,
11433 .selector = vmcs12->host_tr_selector,
11437 vmx_set_segment(vcpu, &seg, VCPU_SREG_TR);
11439 kvm_set_dr(vcpu, 7, 0x400);
11440 vmcs_write64(GUEST_IA32_DEBUGCTL, 0);
11442 if (cpu_has_vmx_msr_bitmap())
11443 vmx_set_msr_bitmap(vcpu);
11445 if (nested_vmx_load_msr(vcpu, vmcs12->vm_exit_msr_load_addr,
11446 vmcs12->vm_exit_msr_load_count))
11447 nested_vmx_abort(vcpu, VMX_ABORT_LOAD_HOST_MSR_FAIL);
11451 * Emulate an exit from nested guest (L2) to L1, i.e., prepare to run L1
11452 * and modify vmcs12 to make it see what it would expect to see there if
11453 * L2 was its real guest. Must only be called when in L2 (is_guest_mode())
11455 static void nested_vmx_vmexit(struct kvm_vcpu *vcpu, u32 exit_reason,
11456 u32 exit_intr_info,
11457 unsigned long exit_qualification)
11459 struct vcpu_vmx *vmx = to_vmx(vcpu);
11460 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
11462 /* trying to cancel vmlaunch/vmresume is a bug */
11463 WARN_ON_ONCE(vmx->nested.nested_run_pending);
11466 * The only expected VM-instruction error is "VM entry with
11467 * invalid control field(s)." Anything else indicates a
11470 WARN_ON_ONCE(vmx->fail && (vmcs_read32(VM_INSTRUCTION_ERROR) !=
11471 VMXERR_ENTRY_INVALID_CONTROL_FIELD));
11473 leave_guest_mode(vcpu);
11475 if (likely(!vmx->fail)) {
11476 if (exit_reason == -1)
11477 sync_vmcs12(vcpu, vmcs12);
11479 prepare_vmcs12(vcpu, vmcs12, exit_reason, exit_intr_info,
11480 exit_qualification);
11482 if (nested_vmx_store_msr(vcpu, vmcs12->vm_exit_msr_store_addr,
11483 vmcs12->vm_exit_msr_store_count))
11484 nested_vmx_abort(vcpu, VMX_ABORT_SAVE_GUEST_MSR_FAIL);
11487 vmx_switch_vmcs(vcpu, &vmx->vmcs01);
11488 vm_entry_controls_reset_shadow(vmx);
11489 vm_exit_controls_reset_shadow(vmx);
11490 vmx_segment_cache_clear(vmx);
11492 /* Update any VMCS fields that might have changed while L2 ran */
11493 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, vmx->msr_autoload.nr);
11494 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, vmx->msr_autoload.nr);
11495 vmcs_write64(TSC_OFFSET, vcpu->arch.tsc_offset);
11496 if (vmx->hv_deadline_tsc == -1)
11497 vmcs_clear_bits(PIN_BASED_VM_EXEC_CONTROL,
11498 PIN_BASED_VMX_PREEMPTION_TIMER);
11500 vmcs_set_bits(PIN_BASED_VM_EXEC_CONTROL,
11501 PIN_BASED_VMX_PREEMPTION_TIMER);
11502 if (kvm_has_tsc_control)
11503 decache_tsc_multiplier(vmx);
11505 if (vmx->nested.change_vmcs01_virtual_x2apic_mode) {
11506 vmx->nested.change_vmcs01_virtual_x2apic_mode = false;
11507 vmx_set_virtual_x2apic_mode(vcpu,
11508 vcpu->arch.apic_base & X2APIC_ENABLE);
11509 } else if (!nested_cpu_has_ept(vmcs12) &&
11510 nested_cpu_has2(vmcs12,
11511 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) {
11512 vmx_flush_tlb_ept_only(vcpu);
11515 /* This is needed for same reason as it was needed in prepare_vmcs02 */
11518 /* Unpin physical memory we referred to in vmcs02 */
11519 if (vmx->nested.apic_access_page) {
11520 kvm_release_page_dirty(vmx->nested.apic_access_page);
11521 vmx->nested.apic_access_page = NULL;
11523 if (vmx->nested.virtual_apic_page) {
11524 kvm_release_page_dirty(vmx->nested.virtual_apic_page);
11525 vmx->nested.virtual_apic_page = NULL;
11527 if (vmx->nested.pi_desc_page) {
11528 kunmap(vmx->nested.pi_desc_page);
11529 kvm_release_page_dirty(vmx->nested.pi_desc_page);
11530 vmx->nested.pi_desc_page = NULL;
11531 vmx->nested.pi_desc = NULL;
11535 * We are now running in L2, mmu_notifier will force to reload the
11536 * page's hpa for L2 vmcs. Need to reload it for L1 before entering L1.
11538 kvm_make_request(KVM_REQ_APIC_PAGE_RELOAD, vcpu);
11540 if (enable_shadow_vmcs && exit_reason != -1)
11541 vmx->nested.sync_shadow_vmcs = true;
11543 /* in case we halted in L2 */
11544 vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
11546 if (likely(!vmx->fail)) {
11548 * TODO: SDM says that with acknowledge interrupt on
11549 * exit, bit 31 of the VM-exit interrupt information
11550 * (valid interrupt) is always set to 1 on
11551 * EXIT_REASON_EXTERNAL_INTERRUPT, so we shouldn't
11552 * need kvm_cpu_has_interrupt(). See the commit
11553 * message for details.
11555 if (nested_exit_intr_ack_set(vcpu) &&
11556 exit_reason == EXIT_REASON_EXTERNAL_INTERRUPT &&
11557 kvm_cpu_has_interrupt(vcpu)) {
11558 int irq = kvm_cpu_get_interrupt(vcpu);
11560 vmcs12->vm_exit_intr_info = irq |
11561 INTR_INFO_VALID_MASK | INTR_TYPE_EXT_INTR;
11564 if (exit_reason != -1)
11565 trace_kvm_nested_vmexit_inject(vmcs12->vm_exit_reason,
11566 vmcs12->exit_qualification,
11567 vmcs12->idt_vectoring_info_field,
11568 vmcs12->vm_exit_intr_info,
11569 vmcs12->vm_exit_intr_error_code,
11572 load_vmcs12_host_state(vcpu, vmcs12);
11578 * After an early L2 VM-entry failure, we're now back
11579 * in L1 which thinks it just finished a VMLAUNCH or
11580 * VMRESUME instruction, so we need to set the failure
11581 * flag and the VM-instruction error field of the VMCS
11584 nested_vmx_failValid(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD);
11586 load_vmcs12_mmu_host_state(vcpu, vmcs12);
11589 * The emulated instruction was already skipped in
11590 * nested_vmx_run, but the updated RIP was never
11591 * written back to the vmcs01.
11593 skip_emulated_instruction(vcpu);
11598 * Forcibly leave nested mode in order to be able to reset the VCPU later on.
11600 static void vmx_leave_nested(struct kvm_vcpu *vcpu)
11602 if (is_guest_mode(vcpu)) {
11603 to_vmx(vcpu)->nested.nested_run_pending = 0;
11604 nested_vmx_vmexit(vcpu, -1, 0, 0);
11606 free_nested(to_vmx(vcpu));
11610 * L1's failure to enter L2 is a subset of a normal exit, as explained in
11611 * 23.7 "VM-entry failures during or after loading guest state" (this also
11612 * lists the acceptable exit-reason and exit-qualification parameters).
11613 * It should only be called before L2 actually succeeded to run, and when
11614 * vmcs01 is current (it doesn't leave_guest_mode() or switch vmcss).
11616 static void nested_vmx_entry_failure(struct kvm_vcpu *vcpu,
11617 struct vmcs12 *vmcs12,
11618 u32 reason, unsigned long qualification)
11620 load_vmcs12_host_state(vcpu, vmcs12);
11621 vmcs12->vm_exit_reason = reason | VMX_EXIT_REASONS_FAILED_VMENTRY;
11622 vmcs12->exit_qualification = qualification;
11623 nested_vmx_succeed(vcpu);
11624 if (enable_shadow_vmcs)
11625 to_vmx(vcpu)->nested.sync_shadow_vmcs = true;
11628 static int vmx_check_intercept(struct kvm_vcpu *vcpu,
11629 struct x86_instruction_info *info,
11630 enum x86_intercept_stage stage)
11632 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
11633 struct x86_emulate_ctxt *ctxt = &vcpu->arch.emulate_ctxt;
11636 * RDPID causes #UD if disabled through secondary execution controls.
11637 * Because it is marked as EmulateOnUD, we need to intercept it here.
11639 if (info->intercept == x86_intercept_rdtscp &&
11640 !nested_cpu_has2(vmcs12, SECONDARY_EXEC_RDTSCP)) {
11641 ctxt->exception.vector = UD_VECTOR;
11642 ctxt->exception.error_code_valid = false;
11643 return X86EMUL_PROPAGATE_FAULT;
11646 /* TODO: check more intercepts... */
11647 return X86EMUL_CONTINUE;
11650 #ifdef CONFIG_X86_64
11651 /* (a << shift) / divisor, return 1 if overflow otherwise 0 */
11652 static inline int u64_shl_div_u64(u64 a, unsigned int shift,
11653 u64 divisor, u64 *result)
11655 u64 low = a << shift, high = a >> (64 - shift);
11657 /* To avoid the overflow on divq */
11658 if (high >= divisor)
11661 /* Low hold the result, high hold rem which is discarded */
11662 asm("divq %2\n\t" : "=a" (low), "=d" (high) :
11663 "rm" (divisor), "0" (low), "1" (high));
11669 static int vmx_set_hv_timer(struct kvm_vcpu *vcpu, u64 guest_deadline_tsc)
11671 struct vcpu_vmx *vmx = to_vmx(vcpu);
11672 u64 tscl = rdtsc();
11673 u64 guest_tscl = kvm_read_l1_tsc(vcpu, tscl);
11674 u64 delta_tsc = max(guest_deadline_tsc, guest_tscl) - guest_tscl;
11676 /* Convert to host delta tsc if tsc scaling is enabled */
11677 if (vcpu->arch.tsc_scaling_ratio != kvm_default_tsc_scaling_ratio &&
11678 u64_shl_div_u64(delta_tsc,
11679 kvm_tsc_scaling_ratio_frac_bits,
11680 vcpu->arch.tsc_scaling_ratio,
11685 * If the delta tsc can't fit in the 32 bit after the multi shift,
11686 * we can't use the preemption timer.
11687 * It's possible that it fits on later vmentries, but checking
11688 * on every vmentry is costly so we just use an hrtimer.
11690 if (delta_tsc >> (cpu_preemption_timer_multi + 32))
11693 vmx->hv_deadline_tsc = tscl + delta_tsc;
11694 vmcs_set_bits(PIN_BASED_VM_EXEC_CONTROL,
11695 PIN_BASED_VMX_PREEMPTION_TIMER);
11697 return delta_tsc == 0;
11700 static void vmx_cancel_hv_timer(struct kvm_vcpu *vcpu)
11702 struct vcpu_vmx *vmx = to_vmx(vcpu);
11703 vmx->hv_deadline_tsc = -1;
11704 vmcs_clear_bits(PIN_BASED_VM_EXEC_CONTROL,
11705 PIN_BASED_VMX_PREEMPTION_TIMER);
11709 static void vmx_sched_in(struct kvm_vcpu *vcpu, int cpu)
11712 shrink_ple_window(vcpu);
11715 static void vmx_slot_enable_log_dirty(struct kvm *kvm,
11716 struct kvm_memory_slot *slot)
11718 kvm_mmu_slot_leaf_clear_dirty(kvm, slot);
11719 kvm_mmu_slot_largepage_remove_write_access(kvm, slot);
11722 static void vmx_slot_disable_log_dirty(struct kvm *kvm,
11723 struct kvm_memory_slot *slot)
11725 kvm_mmu_slot_set_dirty(kvm, slot);
11728 static void vmx_flush_log_dirty(struct kvm *kvm)
11730 kvm_flush_pml_buffers(kvm);
11733 static int vmx_write_pml_buffer(struct kvm_vcpu *vcpu)
11735 struct vmcs12 *vmcs12;
11736 struct vcpu_vmx *vmx = to_vmx(vcpu);
11738 struct page *page = NULL;
11741 if (is_guest_mode(vcpu)) {
11742 WARN_ON_ONCE(vmx->nested.pml_full);
11745 * Check if PML is enabled for the nested guest.
11746 * Whether eptp bit 6 is set is already checked
11747 * as part of A/D emulation.
11749 vmcs12 = get_vmcs12(vcpu);
11750 if (!nested_cpu_has_pml(vmcs12))
11753 if (vmcs12->guest_pml_index >= PML_ENTITY_NUM) {
11754 vmx->nested.pml_full = true;
11758 gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS) & ~0xFFFull;
11760 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->pml_address);
11761 if (is_error_page(page))
11764 pml_address = kmap(page);
11765 pml_address[vmcs12->guest_pml_index--] = gpa;
11767 kvm_release_page_clean(page);
11773 static void vmx_enable_log_dirty_pt_masked(struct kvm *kvm,
11774 struct kvm_memory_slot *memslot,
11775 gfn_t offset, unsigned long mask)
11777 kvm_mmu_clear_dirty_pt_masked(kvm, memslot, offset, mask);
11780 static void __pi_post_block(struct kvm_vcpu *vcpu)
11782 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
11783 struct pi_desc old, new;
11787 old.control = new.control = pi_desc->control;
11788 WARN(old.nv != POSTED_INTR_WAKEUP_VECTOR,
11789 "Wakeup handler not enabled while the VCPU is blocked\n");
11791 dest = cpu_physical_id(vcpu->cpu);
11793 if (x2apic_enabled())
11796 new.ndst = (dest << 8) & 0xFF00;
11798 /* set 'NV' to 'notification vector' */
11799 new.nv = POSTED_INTR_VECTOR;
11800 } while (cmpxchg64(&pi_desc->control, old.control,
11801 new.control) != old.control);
11803 if (!WARN_ON_ONCE(vcpu->pre_pcpu == -1)) {
11804 spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
11805 list_del(&vcpu->blocked_vcpu_list);
11806 spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
11807 vcpu->pre_pcpu = -1;
11812 * This routine does the following things for vCPU which is going
11813 * to be blocked if VT-d PI is enabled.
11814 * - Store the vCPU to the wakeup list, so when interrupts happen
11815 * we can find the right vCPU to wake up.
11816 * - Change the Posted-interrupt descriptor as below:
11817 * 'NDST' <-- vcpu->pre_pcpu
11818 * 'NV' <-- POSTED_INTR_WAKEUP_VECTOR
11819 * - If 'ON' is set during this process, which means at least one
11820 * interrupt is posted for this vCPU, we cannot block it, in
11821 * this case, return 1, otherwise, return 0.
11824 static int pi_pre_block(struct kvm_vcpu *vcpu)
11827 struct pi_desc old, new;
11828 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
11830 if (!kvm_arch_has_assigned_device(vcpu->kvm) ||
11831 !irq_remapping_cap(IRQ_POSTING_CAP) ||
11832 !kvm_vcpu_apicv_active(vcpu))
11835 WARN_ON(irqs_disabled());
11836 local_irq_disable();
11837 if (!WARN_ON_ONCE(vcpu->pre_pcpu != -1)) {
11838 vcpu->pre_pcpu = vcpu->cpu;
11839 spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
11840 list_add_tail(&vcpu->blocked_vcpu_list,
11841 &per_cpu(blocked_vcpu_on_cpu,
11843 spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
11847 old.control = new.control = pi_desc->control;
11849 WARN((pi_desc->sn == 1),
11850 "Warning: SN field of posted-interrupts "
11851 "is set before blocking\n");
11854 * Since vCPU can be preempted during this process,
11855 * vcpu->cpu could be different with pre_pcpu, we
11856 * need to set pre_pcpu as the destination of wakeup
11857 * notification event, then we can find the right vCPU
11858 * to wakeup in wakeup handler if interrupts happen
11859 * when the vCPU is in blocked state.
11861 dest = cpu_physical_id(vcpu->pre_pcpu);
11863 if (x2apic_enabled())
11866 new.ndst = (dest << 8) & 0xFF00;
11868 /* set 'NV' to 'wakeup vector' */
11869 new.nv = POSTED_INTR_WAKEUP_VECTOR;
11870 } while (cmpxchg64(&pi_desc->control, old.control,
11871 new.control) != old.control);
11873 /* We should not block the vCPU if an interrupt is posted for it. */
11874 if (pi_test_on(pi_desc) == 1)
11875 __pi_post_block(vcpu);
11877 local_irq_enable();
11878 return (vcpu->pre_pcpu == -1);
11881 static int vmx_pre_block(struct kvm_vcpu *vcpu)
11883 if (pi_pre_block(vcpu))
11886 if (kvm_lapic_hv_timer_in_use(vcpu))
11887 kvm_lapic_switch_to_sw_timer(vcpu);
11892 static void pi_post_block(struct kvm_vcpu *vcpu)
11894 if (vcpu->pre_pcpu == -1)
11897 WARN_ON(irqs_disabled());
11898 local_irq_disable();
11899 __pi_post_block(vcpu);
11900 local_irq_enable();
11903 static void vmx_post_block(struct kvm_vcpu *vcpu)
11905 if (kvm_x86_ops->set_hv_timer)
11906 kvm_lapic_switch_to_hv_timer(vcpu);
11908 pi_post_block(vcpu);
11912 * vmx_update_pi_irte - set IRTE for Posted-Interrupts
11915 * @host_irq: host irq of the interrupt
11916 * @guest_irq: gsi of the interrupt
11917 * @set: set or unset PI
11918 * returns 0 on success, < 0 on failure
11920 static int vmx_update_pi_irte(struct kvm *kvm, unsigned int host_irq,
11921 uint32_t guest_irq, bool set)
11923 struct kvm_kernel_irq_routing_entry *e;
11924 struct kvm_irq_routing_table *irq_rt;
11925 struct kvm_lapic_irq irq;
11926 struct kvm_vcpu *vcpu;
11927 struct vcpu_data vcpu_info;
11930 if (!kvm_arch_has_assigned_device(kvm) ||
11931 !irq_remapping_cap(IRQ_POSTING_CAP) ||
11932 !kvm_vcpu_apicv_active(kvm->vcpus[0]))
11935 idx = srcu_read_lock(&kvm->irq_srcu);
11936 irq_rt = srcu_dereference(kvm->irq_routing, &kvm->irq_srcu);
11937 if (guest_irq >= irq_rt->nr_rt_entries ||
11938 hlist_empty(&irq_rt->map[guest_irq])) {
11939 pr_warn_once("no route for guest_irq %u/%u (broken user space?)\n",
11940 guest_irq, irq_rt->nr_rt_entries);
11944 hlist_for_each_entry(e, &irq_rt->map[guest_irq], link) {
11945 if (e->type != KVM_IRQ_ROUTING_MSI)
11948 * VT-d PI cannot support posting multicast/broadcast
11949 * interrupts to a vCPU, we still use interrupt remapping
11950 * for these kind of interrupts.
11952 * For lowest-priority interrupts, we only support
11953 * those with single CPU as the destination, e.g. user
11954 * configures the interrupts via /proc/irq or uses
11955 * irqbalance to make the interrupts single-CPU.
11957 * We will support full lowest-priority interrupt later.
11960 kvm_set_msi_irq(kvm, e, &irq);
11961 if (!kvm_intr_is_single_vcpu(kvm, &irq, &vcpu)) {
11963 * Make sure the IRTE is in remapped mode if
11964 * we don't handle it in posted mode.
11966 ret = irq_set_vcpu_affinity(host_irq, NULL);
11969 "failed to back to remapped mode, irq: %u\n",
11977 vcpu_info.pi_desc_addr = __pa(vcpu_to_pi_desc(vcpu));
11978 vcpu_info.vector = irq.vector;
11980 trace_kvm_pi_irte_update(vcpu->vcpu_id, host_irq, e->gsi,
11981 vcpu_info.vector, vcpu_info.pi_desc_addr, set);
11984 ret = irq_set_vcpu_affinity(host_irq, &vcpu_info);
11986 ret = irq_set_vcpu_affinity(host_irq, NULL);
11989 printk(KERN_INFO "%s: failed to update PI IRTE\n",
11997 srcu_read_unlock(&kvm->irq_srcu, idx);
12001 static void vmx_setup_mce(struct kvm_vcpu *vcpu)
12003 if (vcpu->arch.mcg_cap & MCG_LMCE_P)
12004 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits |=
12005 FEATURE_CONTROL_LMCE;
12007 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits &=
12008 ~FEATURE_CONTROL_LMCE;
12011 static int vmx_smi_allowed(struct kvm_vcpu *vcpu)
12013 /* we need a nested vmexit to enter SMM, postpone if run is pending */
12014 if (to_vmx(vcpu)->nested.nested_run_pending)
12019 static int vmx_pre_enter_smm(struct kvm_vcpu *vcpu, char *smstate)
12021 struct vcpu_vmx *vmx = to_vmx(vcpu);
12023 vmx->nested.smm.guest_mode = is_guest_mode(vcpu);
12024 if (vmx->nested.smm.guest_mode)
12025 nested_vmx_vmexit(vcpu, -1, 0, 0);
12027 vmx->nested.smm.vmxon = vmx->nested.vmxon;
12028 vmx->nested.vmxon = false;
12032 static int vmx_pre_leave_smm(struct kvm_vcpu *vcpu, u64 smbase)
12034 struct vcpu_vmx *vmx = to_vmx(vcpu);
12037 if (vmx->nested.smm.vmxon) {
12038 vmx->nested.vmxon = true;
12039 vmx->nested.smm.vmxon = false;
12042 if (vmx->nested.smm.guest_mode) {
12043 vcpu->arch.hflags &= ~HF_SMM_MASK;
12044 ret = enter_vmx_non_root_mode(vcpu, false);
12045 vcpu->arch.hflags |= HF_SMM_MASK;
12049 vmx->nested.smm.guest_mode = false;
12054 static int enable_smi_window(struct kvm_vcpu *vcpu)
12059 static struct kvm_x86_ops vmx_x86_ops __ro_after_init = {
12060 .cpu_has_kvm_support = cpu_has_kvm_support,
12061 .disabled_by_bios = vmx_disabled_by_bios,
12062 .hardware_setup = hardware_setup,
12063 .hardware_unsetup = hardware_unsetup,
12064 .check_processor_compatibility = vmx_check_processor_compat,
12065 .hardware_enable = hardware_enable,
12066 .hardware_disable = hardware_disable,
12067 .cpu_has_accelerated_tpr = report_flexpriority,
12068 .cpu_has_high_real_mode_segbase = vmx_has_high_real_mode_segbase,
12070 .vcpu_create = vmx_create_vcpu,
12071 .vcpu_free = vmx_free_vcpu,
12072 .vcpu_reset = vmx_vcpu_reset,
12074 .prepare_guest_switch = vmx_save_host_state,
12075 .vcpu_load = vmx_vcpu_load,
12076 .vcpu_put = vmx_vcpu_put,
12078 .update_bp_intercept = update_exception_bitmap,
12079 .get_msr = vmx_get_msr,
12080 .set_msr = vmx_set_msr,
12081 .get_segment_base = vmx_get_segment_base,
12082 .get_segment = vmx_get_segment,
12083 .set_segment = vmx_set_segment,
12084 .get_cpl = vmx_get_cpl,
12085 .get_cs_db_l_bits = vmx_get_cs_db_l_bits,
12086 .decache_cr0_guest_bits = vmx_decache_cr0_guest_bits,
12087 .decache_cr3 = vmx_decache_cr3,
12088 .decache_cr4_guest_bits = vmx_decache_cr4_guest_bits,
12089 .set_cr0 = vmx_set_cr0,
12090 .set_cr3 = vmx_set_cr3,
12091 .set_cr4 = vmx_set_cr4,
12092 .set_efer = vmx_set_efer,
12093 .get_idt = vmx_get_idt,
12094 .set_idt = vmx_set_idt,
12095 .get_gdt = vmx_get_gdt,
12096 .set_gdt = vmx_set_gdt,
12097 .get_dr6 = vmx_get_dr6,
12098 .set_dr6 = vmx_set_dr6,
12099 .set_dr7 = vmx_set_dr7,
12100 .sync_dirty_debug_regs = vmx_sync_dirty_debug_regs,
12101 .cache_reg = vmx_cache_reg,
12102 .get_rflags = vmx_get_rflags,
12103 .set_rflags = vmx_set_rflags,
12105 .tlb_flush = vmx_flush_tlb,
12107 .run = vmx_vcpu_run,
12108 .handle_exit = vmx_handle_exit,
12109 .skip_emulated_instruction = skip_emulated_instruction,
12110 .set_interrupt_shadow = vmx_set_interrupt_shadow,
12111 .get_interrupt_shadow = vmx_get_interrupt_shadow,
12112 .patch_hypercall = vmx_patch_hypercall,
12113 .set_irq = vmx_inject_irq,
12114 .set_nmi = vmx_inject_nmi,
12115 .queue_exception = vmx_queue_exception,
12116 .cancel_injection = vmx_cancel_injection,
12117 .interrupt_allowed = vmx_interrupt_allowed,
12118 .nmi_allowed = vmx_nmi_allowed,
12119 .get_nmi_mask = vmx_get_nmi_mask,
12120 .set_nmi_mask = vmx_set_nmi_mask,
12121 .enable_nmi_window = enable_nmi_window,
12122 .enable_irq_window = enable_irq_window,
12123 .update_cr8_intercept = update_cr8_intercept,
12124 .set_virtual_x2apic_mode = vmx_set_virtual_x2apic_mode,
12125 .set_apic_access_page_addr = vmx_set_apic_access_page_addr,
12126 .get_enable_apicv = vmx_get_enable_apicv,
12127 .refresh_apicv_exec_ctrl = vmx_refresh_apicv_exec_ctrl,
12128 .load_eoi_exitmap = vmx_load_eoi_exitmap,
12129 .apicv_post_state_restore = vmx_apicv_post_state_restore,
12130 .hwapic_irr_update = vmx_hwapic_irr_update,
12131 .hwapic_isr_update = vmx_hwapic_isr_update,
12132 .sync_pir_to_irr = vmx_sync_pir_to_irr,
12133 .deliver_posted_interrupt = vmx_deliver_posted_interrupt,
12135 .set_tss_addr = vmx_set_tss_addr,
12136 .get_tdp_level = get_ept_level,
12137 .get_mt_mask = vmx_get_mt_mask,
12139 .get_exit_info = vmx_get_exit_info,
12141 .get_lpage_level = vmx_get_lpage_level,
12143 .cpuid_update = vmx_cpuid_update,
12145 .rdtscp_supported = vmx_rdtscp_supported,
12146 .invpcid_supported = vmx_invpcid_supported,
12148 .set_supported_cpuid = vmx_set_supported_cpuid,
12150 .has_wbinvd_exit = cpu_has_vmx_wbinvd_exit,
12152 .write_tsc_offset = vmx_write_tsc_offset,
12154 .set_tdp_cr3 = vmx_set_cr3,
12156 .check_intercept = vmx_check_intercept,
12157 .handle_external_intr = vmx_handle_external_intr,
12158 .mpx_supported = vmx_mpx_supported,
12159 .xsaves_supported = vmx_xsaves_supported,
12160 .umip_emulated = vmx_umip_emulated,
12162 .check_nested_events = vmx_check_nested_events,
12164 .sched_in = vmx_sched_in,
12166 .slot_enable_log_dirty = vmx_slot_enable_log_dirty,
12167 .slot_disable_log_dirty = vmx_slot_disable_log_dirty,
12168 .flush_log_dirty = vmx_flush_log_dirty,
12169 .enable_log_dirty_pt_masked = vmx_enable_log_dirty_pt_masked,
12170 .write_log_dirty = vmx_write_pml_buffer,
12172 .pre_block = vmx_pre_block,
12173 .post_block = vmx_post_block,
12175 .pmu_ops = &intel_pmu_ops,
12177 .update_pi_irte = vmx_update_pi_irte,
12179 #ifdef CONFIG_X86_64
12180 .set_hv_timer = vmx_set_hv_timer,
12181 .cancel_hv_timer = vmx_cancel_hv_timer,
12184 .setup_mce = vmx_setup_mce,
12186 .smi_allowed = vmx_smi_allowed,
12187 .pre_enter_smm = vmx_pre_enter_smm,
12188 .pre_leave_smm = vmx_pre_leave_smm,
12189 .enable_smi_window = enable_smi_window,
12192 static int __init vmx_init(void)
12194 int r = kvm_init(&vmx_x86_ops, sizeof(struct vcpu_vmx),
12195 __alignof__(struct vcpu_vmx), THIS_MODULE);
12199 #ifdef CONFIG_KEXEC_CORE
12200 rcu_assign_pointer(crash_vmclear_loaded_vmcss,
12201 crash_vmclear_local_loaded_vmcss);
12207 static void __exit vmx_exit(void)
12209 #ifdef CONFIG_KEXEC_CORE
12210 RCU_INIT_POINTER(crash_vmclear_loaded_vmcss, NULL);
12217 module_init(vmx_init)
12218 module_exit(vmx_exit)