2 * Kernel-based Virtual Machine driver for Linux
4 * This module enables machines with Intel VT-x extensions to run virtual
5 * machines without emulation or binary translation.
7 * Copyright (C) 2006 Qumranet, Inc.
8 * Copyright 2010 Red Hat, Inc. and/or its affiliates.
11 * Avi Kivity <avi@qumranet.com>
12 * Yaniv Kamay <yaniv@qumranet.com>
14 * This work is licensed under the terms of the GNU GPL, version 2. See
15 * the COPYING file in the top-level directory.
24 #include <linux/kvm_host.h>
25 #include <linux/module.h>
26 #include <linux/kernel.h>
28 #include <linux/highmem.h>
29 #include <linux/sched.h>
30 #include <linux/moduleparam.h>
31 #include <linux/mod_devicetable.h>
32 #include <linux/trace_events.h>
33 #include <linux/slab.h>
34 #include <linux/tboot.h>
35 #include <linux/hrtimer.h>
36 #include <linux/frame.h>
37 #include <linux/nospec.h>
38 #include "kvm_cache_regs.h"
45 #include <asm/virtext.h>
47 #include <asm/fpu/internal.h>
48 #include <asm/perf_event.h>
49 #include <asm/debugreg.h>
50 #include <asm/kexec.h>
52 #include <asm/irq_remapping.h>
53 #include <asm/mmu_context.h>
54 #include <asm/microcode.h>
55 #include <asm/spec-ctrl.h>
60 #define __ex(x) __kvm_handle_fault_on_reboot(x)
61 #define __ex_clear(x, reg) \
62 ____kvm_handle_fault_on_reboot(x, "xor " reg " , " reg)
64 MODULE_AUTHOR("Qumranet");
65 MODULE_LICENSE("GPL");
67 static const struct x86_cpu_id vmx_cpu_id
[] = {
68 X86_FEATURE_MATCH(X86_FEATURE_VMX
),
71 MODULE_DEVICE_TABLE(x86cpu
, vmx_cpu_id
);
73 static bool __read_mostly nosmt
;
74 module_param(nosmt
, bool, S_IRUGO
);
76 static bool __read_mostly enable_vpid
= 1;
77 module_param_named(vpid
, enable_vpid
, bool, 0444);
79 static bool __read_mostly enable_vnmi
= 1;
80 module_param_named(vnmi
, enable_vnmi
, bool, S_IRUGO
);
82 static bool __read_mostly flexpriority_enabled
= 1;
83 module_param_named(flexpriority
, flexpriority_enabled
, bool, S_IRUGO
);
85 static bool __read_mostly enable_ept
= 1;
86 module_param_named(ept
, enable_ept
, bool, S_IRUGO
);
88 static bool __read_mostly enable_unrestricted_guest
= 1;
89 module_param_named(unrestricted_guest
,
90 enable_unrestricted_guest
, bool, S_IRUGO
);
92 static bool __read_mostly enable_ept_ad_bits
= 1;
93 module_param_named(eptad
, enable_ept_ad_bits
, bool, S_IRUGO
);
95 static bool __read_mostly emulate_invalid_guest_state
= true;
96 module_param(emulate_invalid_guest_state
, bool, S_IRUGO
);
98 static bool __read_mostly fasteoi
= 1;
99 module_param(fasteoi
, bool, S_IRUGO
);
101 static bool __read_mostly enable_apicv
= 1;
102 module_param(enable_apicv
, bool, S_IRUGO
);
104 static bool __read_mostly enable_shadow_vmcs
= 1;
105 module_param_named(enable_shadow_vmcs
, enable_shadow_vmcs
, bool, S_IRUGO
);
107 * If nested=1, nested virtualization is supported, i.e., guests may use
108 * VMX and be a hypervisor for its own guests. If nested=0, guests may not
109 * use VMX instructions.
111 static bool __read_mostly nested
= 0;
112 module_param(nested
, bool, S_IRUGO
);
114 static u64 __read_mostly host_xss
;
116 static bool __read_mostly enable_pml
= 1;
117 module_param_named(pml
, enable_pml
, bool, S_IRUGO
);
121 #define MSR_TYPE_RW 3
123 #define MSR_BITMAP_MODE_X2APIC 1
124 #define MSR_BITMAP_MODE_X2APIC_APICV 2
125 #define MSR_BITMAP_MODE_LM 4
127 #define KVM_VMX_TSC_MULTIPLIER_MAX 0xffffffffffffffffULL
129 /* Guest_tsc -> host_tsc conversion requires 64-bit division. */
130 static int __read_mostly cpu_preemption_timer_multi
;
131 static bool __read_mostly enable_preemption_timer
= 1;
133 module_param_named(preemption_timer
, enable_preemption_timer
, bool, S_IRUGO
);
136 #define KVM_GUEST_CR0_MASK (X86_CR0_NW | X86_CR0_CD)
137 #define KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST (X86_CR0_WP | X86_CR0_NE)
138 #define KVM_VM_CR0_ALWAYS_ON \
139 (KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST | X86_CR0_PG | X86_CR0_PE)
140 #define KVM_CR4_GUEST_OWNED_BITS \
141 (X86_CR4_PVI | X86_CR4_DE | X86_CR4_PCE | X86_CR4_OSFXSR \
142 | X86_CR4_OSXMMEXCPT | X86_CR4_LA57 | X86_CR4_TSD)
144 #define KVM_PMODE_VM_CR4_ALWAYS_ON (X86_CR4_PAE | X86_CR4_VMXE)
145 #define KVM_RMODE_VM_CR4_ALWAYS_ON (X86_CR4_VME | X86_CR4_PAE | X86_CR4_VMXE)
147 #define RMODE_GUEST_OWNED_EFLAGS_BITS (~(X86_EFLAGS_IOPL | X86_EFLAGS_VM))
149 #define VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE 5
152 * Hyper-V requires all of these, so mark them as supported even though
153 * they are just treated the same as all-context.
155 #define VMX_VPID_EXTENT_SUPPORTED_MASK \
156 (VMX_VPID_EXTENT_INDIVIDUAL_ADDR_BIT | \
157 VMX_VPID_EXTENT_SINGLE_CONTEXT_BIT | \
158 VMX_VPID_EXTENT_GLOBAL_CONTEXT_BIT | \
159 VMX_VPID_EXTENT_SINGLE_NON_GLOBAL_BIT)
162 * These 2 parameters are used to config the controls for Pause-Loop Exiting:
163 * ple_gap: upper bound on the amount of time between two successive
164 * executions of PAUSE in a loop. Also indicate if ple enabled.
165 * According to test, this time is usually smaller than 128 cycles.
166 * ple_window: upper bound on the amount of time a guest is allowed to execute
167 * in a PAUSE loop. Tests indicate that most spinlocks are held for
168 * less than 2^12 cycles
169 * Time is measured based on a counter that runs at the same rate as the TSC,
170 * refer SDM volume 3b section 21.6.13 & 22.1.3.
172 #define KVM_VMX_DEFAULT_PLE_GAP 128
173 #define KVM_VMX_DEFAULT_PLE_WINDOW 4096
174 #define KVM_VMX_DEFAULT_PLE_WINDOW_GROW 2
175 #define KVM_VMX_DEFAULT_PLE_WINDOW_SHRINK 0
176 #define KVM_VMX_DEFAULT_PLE_WINDOW_MAX \
177 INT_MAX / KVM_VMX_DEFAULT_PLE_WINDOW_GROW
179 static int ple_gap
= KVM_VMX_DEFAULT_PLE_GAP
;
180 module_param(ple_gap
, int, S_IRUGO
);
182 static int ple_window
= KVM_VMX_DEFAULT_PLE_WINDOW
;
183 module_param(ple_window
, int, S_IRUGO
);
185 /* Default doubles per-vcpu window every exit. */
186 static int ple_window_grow
= KVM_VMX_DEFAULT_PLE_WINDOW_GROW
;
187 module_param(ple_window_grow
, int, S_IRUGO
);
189 /* Default resets per-vcpu window every exit to ple_window. */
190 static int ple_window_shrink
= KVM_VMX_DEFAULT_PLE_WINDOW_SHRINK
;
191 module_param(ple_window_shrink
, int, S_IRUGO
);
193 /* Default is to compute the maximum so we can never overflow. */
194 static int ple_window_actual_max
= KVM_VMX_DEFAULT_PLE_WINDOW_MAX
;
195 static int ple_window_max
= KVM_VMX_DEFAULT_PLE_WINDOW_MAX
;
196 module_param(ple_window_max
, int, S_IRUGO
);
198 extern const ulong vmx_return
;
200 static DEFINE_STATIC_KEY_FALSE(vmx_l1d_should_flush
);
202 static enum vmx_l1d_flush_state __read_mostly vmentry_l1d_flush
= VMENTER_L1D_FLUSH_COND
;
204 static const struct {
206 enum vmx_l1d_flush_state cmd
;
207 } vmentry_l1d_param
[] = {
208 {"auto", VMENTER_L1D_FLUSH_AUTO
},
209 {"never", VMENTER_L1D_FLUSH_NEVER
},
210 {"cond", VMENTER_L1D_FLUSH_COND
},
211 {"always", VMENTER_L1D_FLUSH_ALWAYS
},
214 static int vmentry_l1d_flush_set(const char *s
, const struct kernel_param
*kp
)
221 for (i
= 0; i
< ARRAY_SIZE(vmentry_l1d_param
); i
++) {
222 if (!strcmp(s
, vmentry_l1d_param
[i
].option
)) {
223 vmentry_l1d_flush
= vmentry_l1d_param
[i
].cmd
;
231 static int vmentry_l1d_flush_get(char *s
, const struct kernel_param
*kp
)
233 return sprintf(s
, "%s\n", vmentry_l1d_param
[vmentry_l1d_flush
].option
);
236 static const struct kernel_param_ops vmentry_l1d_flush_ops
= {
237 .set
= vmentry_l1d_flush_set
,
238 .get
= vmentry_l1d_flush_get
,
240 module_param_cb(vmentry_l1d_flush
, &vmentry_l1d_flush_ops
, &vmentry_l1d_flush
, S_IRUGO
);
242 #define NR_AUTOLOAD_MSRS 8
251 * Track a VMCS that may be loaded on a certain CPU. If it is (cpu!=-1), also
252 * remember whether it was VMLAUNCHed, and maintain a linked list of all VMCSs
253 * loaded on this CPU (so we can clear them if the CPU goes down).
257 struct vmcs
*shadow_vmcs
;
260 bool nmi_known_unmasked
;
261 unsigned long vmcs_host_cr3
; /* May not match real cr3 */
262 unsigned long vmcs_host_cr4
; /* May not match real cr4 */
263 /* Support for vnmi-less CPUs */
264 int soft_vnmi_blocked
;
266 s64 vnmi_blocked_time
;
267 unsigned long *msr_bitmap
;
268 struct list_head loaded_vmcss_on_cpu_link
;
271 struct shared_msr_entry
{
278 * struct vmcs12 describes the state that our guest hypervisor (L1) keeps for a
279 * single nested guest (L2), hence the name vmcs12. Any VMX implementation has
280 * a VMCS structure, and vmcs12 is our emulated VMX's VMCS. This structure is
281 * stored in guest memory specified by VMPTRLD, but is opaque to the guest,
282 * which must access it using VMREAD/VMWRITE/VMCLEAR instructions.
283 * More than one of these structures may exist, if L1 runs multiple L2 guests.
284 * nested_vmx_run() will use the data here to build the vmcs02: a VMCS for the
285 * underlying hardware which will be used to run L2.
286 * This structure is packed to ensure that its layout is identical across
287 * machines (necessary for live migration).
288 * If there are changes in this struct, VMCS12_REVISION must be changed.
290 typedef u64 natural_width
;
291 struct __packed vmcs12
{
292 /* According to the Intel spec, a VMCS region must start with the
293 * following two fields. Then follow implementation-specific data.
298 u32 launch_state
; /* set to 0 by VMCLEAR, to 1 by VMLAUNCH */
299 u32 padding
[7]; /* room for future expansion */
304 u64 vm_exit_msr_store_addr
;
305 u64 vm_exit_msr_load_addr
;
306 u64 vm_entry_msr_load_addr
;
308 u64 virtual_apic_page_addr
;
309 u64 apic_access_addr
;
310 u64 posted_intr_desc_addr
;
311 u64 vm_function_control
;
313 u64 eoi_exit_bitmap0
;
314 u64 eoi_exit_bitmap1
;
315 u64 eoi_exit_bitmap2
;
316 u64 eoi_exit_bitmap3
;
317 u64 eptp_list_address
;
319 u64 guest_physical_address
;
320 u64 vmcs_link_pointer
;
322 u64 guest_ia32_debugctl
;
325 u64 guest_ia32_perf_global_ctrl
;
333 u64 host_ia32_perf_global_ctrl
;
334 u64 padding64
[8]; /* room for future expansion */
336 * To allow migration of L1 (complete with its L2 guests) between
337 * machines of different natural widths (32 or 64 bit), we cannot have
338 * unsigned long fields with no explict size. We use u64 (aliased
339 * natural_width) instead. Luckily, x86 is little-endian.
341 natural_width cr0_guest_host_mask
;
342 natural_width cr4_guest_host_mask
;
343 natural_width cr0_read_shadow
;
344 natural_width cr4_read_shadow
;
345 natural_width cr3_target_value0
;
346 natural_width cr3_target_value1
;
347 natural_width cr3_target_value2
;
348 natural_width cr3_target_value3
;
349 natural_width exit_qualification
;
350 natural_width guest_linear_address
;
351 natural_width guest_cr0
;
352 natural_width guest_cr3
;
353 natural_width guest_cr4
;
354 natural_width guest_es_base
;
355 natural_width guest_cs_base
;
356 natural_width guest_ss_base
;
357 natural_width guest_ds_base
;
358 natural_width guest_fs_base
;
359 natural_width guest_gs_base
;
360 natural_width guest_ldtr_base
;
361 natural_width guest_tr_base
;
362 natural_width guest_gdtr_base
;
363 natural_width guest_idtr_base
;
364 natural_width guest_dr7
;
365 natural_width guest_rsp
;
366 natural_width guest_rip
;
367 natural_width guest_rflags
;
368 natural_width guest_pending_dbg_exceptions
;
369 natural_width guest_sysenter_esp
;
370 natural_width guest_sysenter_eip
;
371 natural_width host_cr0
;
372 natural_width host_cr3
;
373 natural_width host_cr4
;
374 natural_width host_fs_base
;
375 natural_width host_gs_base
;
376 natural_width host_tr_base
;
377 natural_width host_gdtr_base
;
378 natural_width host_idtr_base
;
379 natural_width host_ia32_sysenter_esp
;
380 natural_width host_ia32_sysenter_eip
;
381 natural_width host_rsp
;
382 natural_width host_rip
;
383 natural_width paddingl
[8]; /* room for future expansion */
384 u32 pin_based_vm_exec_control
;
385 u32 cpu_based_vm_exec_control
;
386 u32 exception_bitmap
;
387 u32 page_fault_error_code_mask
;
388 u32 page_fault_error_code_match
;
389 u32 cr3_target_count
;
390 u32 vm_exit_controls
;
391 u32 vm_exit_msr_store_count
;
392 u32 vm_exit_msr_load_count
;
393 u32 vm_entry_controls
;
394 u32 vm_entry_msr_load_count
;
395 u32 vm_entry_intr_info_field
;
396 u32 vm_entry_exception_error_code
;
397 u32 vm_entry_instruction_len
;
399 u32 secondary_vm_exec_control
;
400 u32 vm_instruction_error
;
402 u32 vm_exit_intr_info
;
403 u32 vm_exit_intr_error_code
;
404 u32 idt_vectoring_info_field
;
405 u32 idt_vectoring_error_code
;
406 u32 vm_exit_instruction_len
;
407 u32 vmx_instruction_info
;
414 u32 guest_ldtr_limit
;
416 u32 guest_gdtr_limit
;
417 u32 guest_idtr_limit
;
418 u32 guest_es_ar_bytes
;
419 u32 guest_cs_ar_bytes
;
420 u32 guest_ss_ar_bytes
;
421 u32 guest_ds_ar_bytes
;
422 u32 guest_fs_ar_bytes
;
423 u32 guest_gs_ar_bytes
;
424 u32 guest_ldtr_ar_bytes
;
425 u32 guest_tr_ar_bytes
;
426 u32 guest_interruptibility_info
;
427 u32 guest_activity_state
;
428 u32 guest_sysenter_cs
;
429 u32 host_ia32_sysenter_cs
;
430 u32 vmx_preemption_timer_value
;
431 u32 padding32
[7]; /* room for future expansion */
432 u16 virtual_processor_id
;
434 u16 guest_es_selector
;
435 u16 guest_cs_selector
;
436 u16 guest_ss_selector
;
437 u16 guest_ds_selector
;
438 u16 guest_fs_selector
;
439 u16 guest_gs_selector
;
440 u16 guest_ldtr_selector
;
441 u16 guest_tr_selector
;
442 u16 guest_intr_status
;
444 u16 host_es_selector
;
445 u16 host_cs_selector
;
446 u16 host_ss_selector
;
447 u16 host_ds_selector
;
448 u16 host_fs_selector
;
449 u16 host_gs_selector
;
450 u16 host_tr_selector
;
454 * VMCS12_REVISION is an arbitrary id that should be changed if the content or
455 * layout of struct vmcs12 is changed. MSR_IA32_VMX_BASIC returns this id, and
456 * VMPTRLD verifies that the VMCS region that L1 is loading contains this id.
458 #define VMCS12_REVISION 0x11e57ed0
461 * VMCS12_SIZE is the number of bytes L1 should allocate for the VMXON region
462 * and any VMCS region. Although only sizeof(struct vmcs12) are used by the
463 * current implementation, 4K are reserved to avoid future complications.
465 #define VMCS12_SIZE 0x1000
468 * The nested_vmx structure is part of vcpu_vmx, and holds information we need
469 * for correct emulation of VMX (i.e., nested VMX) on this vcpu.
472 /* Has the level1 guest done vmxon? */
477 /* The guest-physical address of the current VMCS L1 keeps for L2 */
480 * Cache of the guest's VMCS, existing outside of guest memory.
481 * Loaded from guest memory during VMPTRLD. Flushed to guest
482 * memory during VMCLEAR and VMPTRLD.
484 struct vmcs12
*cached_vmcs12
;
486 * Indicates if the shadow vmcs must be updated with the
487 * data hold by vmcs12
489 bool sync_shadow_vmcs
;
491 bool change_vmcs01_virtual_x2apic_mode
;
492 /* L2 must run next, and mustn't decide to exit to L1. */
493 bool nested_run_pending
;
495 struct loaded_vmcs vmcs02
;
498 * Guest pages referred to in the vmcs02 with host-physical
499 * pointers, so we must keep them pinned while L2 runs.
501 struct page
*apic_access_page
;
502 struct page
*virtual_apic_page
;
503 struct page
*pi_desc_page
;
504 struct pi_desc
*pi_desc
;
508 struct hrtimer preemption_timer
;
509 bool preemption_timer_expired
;
511 /* to migrate it to L2 if VM_ENTRY_LOAD_DEBUG_CONTROLS is off */
518 * We only store the "true" versions of the VMX capability MSRs. We
519 * generate the "non-true" versions by setting the must-be-1 bits
520 * according to the SDM.
522 u32 nested_vmx_procbased_ctls_low
;
523 u32 nested_vmx_procbased_ctls_high
;
524 u32 nested_vmx_secondary_ctls_low
;
525 u32 nested_vmx_secondary_ctls_high
;
526 u32 nested_vmx_pinbased_ctls_low
;
527 u32 nested_vmx_pinbased_ctls_high
;
528 u32 nested_vmx_exit_ctls_low
;
529 u32 nested_vmx_exit_ctls_high
;
530 u32 nested_vmx_entry_ctls_low
;
531 u32 nested_vmx_entry_ctls_high
;
532 u32 nested_vmx_misc_low
;
533 u32 nested_vmx_misc_high
;
534 u32 nested_vmx_ept_caps
;
535 u32 nested_vmx_vpid_caps
;
536 u64 nested_vmx_basic
;
537 u64 nested_vmx_cr0_fixed0
;
538 u64 nested_vmx_cr0_fixed1
;
539 u64 nested_vmx_cr4_fixed0
;
540 u64 nested_vmx_cr4_fixed1
;
541 u64 nested_vmx_vmcs_enum
;
542 u64 nested_vmx_vmfunc_controls
;
544 /* SMM related state */
546 /* in VMX operation on SMM entry? */
548 /* in guest mode on SMM entry? */
553 #define POSTED_INTR_ON 0
554 #define POSTED_INTR_SN 1
556 /* Posted-Interrupt Descriptor */
558 u32 pir
[8]; /* Posted interrupt requested */
561 /* bit 256 - Outstanding Notification */
563 /* bit 257 - Suppress Notification */
565 /* bit 271:258 - Reserved */
567 /* bit 279:272 - Notification Vector */
569 /* bit 287:280 - Reserved */
571 /* bit 319:288 - Notification Destination */
579 static bool pi_test_and_set_on(struct pi_desc
*pi_desc
)
581 return test_and_set_bit(POSTED_INTR_ON
,
582 (unsigned long *)&pi_desc
->control
);
585 static bool pi_test_and_clear_on(struct pi_desc
*pi_desc
)
587 return test_and_clear_bit(POSTED_INTR_ON
,
588 (unsigned long *)&pi_desc
->control
);
591 static int pi_test_and_set_pir(int vector
, struct pi_desc
*pi_desc
)
593 return test_and_set_bit(vector
, (unsigned long *)pi_desc
->pir
);
596 static inline void pi_clear_sn(struct pi_desc
*pi_desc
)
598 return clear_bit(POSTED_INTR_SN
,
599 (unsigned long *)&pi_desc
->control
);
602 static inline void pi_set_sn(struct pi_desc
*pi_desc
)
604 return set_bit(POSTED_INTR_SN
,
605 (unsigned long *)&pi_desc
->control
);
608 static inline void pi_clear_on(struct pi_desc
*pi_desc
)
610 clear_bit(POSTED_INTR_ON
,
611 (unsigned long *)&pi_desc
->control
);
614 static inline int pi_test_on(struct pi_desc
*pi_desc
)
616 return test_bit(POSTED_INTR_ON
,
617 (unsigned long *)&pi_desc
->control
);
620 static inline int pi_test_sn(struct pi_desc
*pi_desc
)
622 return test_bit(POSTED_INTR_SN
,
623 (unsigned long *)&pi_desc
->control
);
628 struct vmx_msr_entry val
[NR_AUTOLOAD_MSRS
];
632 struct kvm_vcpu vcpu
;
633 unsigned long host_rsp
;
637 u32 idt_vectoring_info
;
639 struct shared_msr_entry
*guest_msrs
;
642 unsigned long host_idt_base
;
644 u64 msr_host_kernel_gs_base
;
645 u64 msr_guest_kernel_gs_base
;
648 u64 arch_capabilities
;
651 u32 vm_entry_controls_shadow
;
652 u32 vm_exit_controls_shadow
;
653 u32 secondary_exec_control
;
656 * loaded_vmcs points to the VMCS currently used in this vcpu. For a
657 * non-nested (L1) guest, it always points to vmcs01. For a nested
658 * guest (L2), it points to a different VMCS.
660 struct loaded_vmcs vmcs01
;
661 struct loaded_vmcs
*loaded_vmcs
;
662 bool __launched
; /* temporary, used in vmx_vcpu_run */
663 struct msr_autoload
{
664 struct vmx_msrs guest
;
665 struct vmx_msrs host
;
669 u16 fs_sel
, gs_sel
, ldt_sel
;
673 int gs_ldt_reload_needed
;
674 int fs_reload_needed
;
675 u64 msr_host_bndcfgs
;
680 struct kvm_segment segs
[8];
683 u32 bitmask
; /* 4 bits per segment (1 bit per field) */
684 struct kvm_save_segment
{
692 bool emulation_required
;
696 /* Posted interrupt descriptor */
697 struct pi_desc pi_desc
;
699 /* Support for a guest hypervisor (nested VMX) */
700 struct nested_vmx nested
;
702 /* Dynamic PLE window. */
704 bool ple_window_dirty
;
706 /* Support for PML */
707 #define PML_ENTITY_NUM 512
710 /* apic deadline value in host tsc */
713 u64 current_tsc_ratio
;
718 * Only bits masked by msr_ia32_feature_control_valid_bits can be set in
719 * msr_ia32_feature_control. FEATURE_CONTROL_LOCKED is always included
720 * in msr_ia32_feature_control_valid_bits.
722 u64 msr_ia32_feature_control
;
723 u64 msr_ia32_feature_control_valid_bits
;
726 enum segment_cache_field
{
735 static inline struct vcpu_vmx
*to_vmx(struct kvm_vcpu
*vcpu
)
737 return container_of(vcpu
, struct vcpu_vmx
, vcpu
);
740 static struct pi_desc
*vcpu_to_pi_desc(struct kvm_vcpu
*vcpu
)
742 return &(to_vmx(vcpu
)->pi_desc
);
745 #define VMCS12_OFFSET(x) offsetof(struct vmcs12, x)
746 #define FIELD(number, name) [number] = VMCS12_OFFSET(name)
747 #define FIELD64(number, name) [number] = VMCS12_OFFSET(name), \
748 [number##_HIGH] = VMCS12_OFFSET(name)+4
751 static unsigned long shadow_read_only_fields
[] = {
753 * We do NOT shadow fields that are modified when L0
754 * traps and emulates any vmx instruction (e.g. VMPTRLD,
755 * VMXON...) executed by L1.
756 * For example, VM_INSTRUCTION_ERROR is read
757 * by L1 if a vmx instruction fails (part of the error path).
758 * Note the code assumes this logic. If for some reason
759 * we start shadowing these fields then we need to
760 * force a shadow sync when L0 emulates vmx instructions
761 * (e.g. force a sync if VM_INSTRUCTION_ERROR is modified
762 * by nested_vmx_failValid)
766 VM_EXIT_INSTRUCTION_LEN
,
767 IDT_VECTORING_INFO_FIELD
,
768 IDT_VECTORING_ERROR_CODE
,
769 VM_EXIT_INTR_ERROR_CODE
,
771 GUEST_LINEAR_ADDRESS
,
772 GUEST_PHYSICAL_ADDRESS
774 static int max_shadow_read_only_fields
=
775 ARRAY_SIZE(shadow_read_only_fields
);
777 static unsigned long shadow_read_write_fields
[] = {
784 GUEST_INTERRUPTIBILITY_INFO
,
797 CPU_BASED_VM_EXEC_CONTROL
,
798 VM_ENTRY_EXCEPTION_ERROR_CODE
,
799 VM_ENTRY_INTR_INFO_FIELD
,
800 VM_ENTRY_INSTRUCTION_LEN
,
801 VM_ENTRY_EXCEPTION_ERROR_CODE
,
807 static int max_shadow_read_write_fields
=
808 ARRAY_SIZE(shadow_read_write_fields
);
810 static const unsigned short vmcs_field_to_offset_table
[] = {
811 FIELD(VIRTUAL_PROCESSOR_ID
, virtual_processor_id
),
812 FIELD(POSTED_INTR_NV
, posted_intr_nv
),
813 FIELD(GUEST_ES_SELECTOR
, guest_es_selector
),
814 FIELD(GUEST_CS_SELECTOR
, guest_cs_selector
),
815 FIELD(GUEST_SS_SELECTOR
, guest_ss_selector
),
816 FIELD(GUEST_DS_SELECTOR
, guest_ds_selector
),
817 FIELD(GUEST_FS_SELECTOR
, guest_fs_selector
),
818 FIELD(GUEST_GS_SELECTOR
, guest_gs_selector
),
819 FIELD(GUEST_LDTR_SELECTOR
, guest_ldtr_selector
),
820 FIELD(GUEST_TR_SELECTOR
, guest_tr_selector
),
821 FIELD(GUEST_INTR_STATUS
, guest_intr_status
),
822 FIELD(GUEST_PML_INDEX
, guest_pml_index
),
823 FIELD(HOST_ES_SELECTOR
, host_es_selector
),
824 FIELD(HOST_CS_SELECTOR
, host_cs_selector
),
825 FIELD(HOST_SS_SELECTOR
, host_ss_selector
),
826 FIELD(HOST_DS_SELECTOR
, host_ds_selector
),
827 FIELD(HOST_FS_SELECTOR
, host_fs_selector
),
828 FIELD(HOST_GS_SELECTOR
, host_gs_selector
),
829 FIELD(HOST_TR_SELECTOR
, host_tr_selector
),
830 FIELD64(IO_BITMAP_A
, io_bitmap_a
),
831 FIELD64(IO_BITMAP_B
, io_bitmap_b
),
832 FIELD64(MSR_BITMAP
, msr_bitmap
),
833 FIELD64(VM_EXIT_MSR_STORE_ADDR
, vm_exit_msr_store_addr
),
834 FIELD64(VM_EXIT_MSR_LOAD_ADDR
, vm_exit_msr_load_addr
),
835 FIELD64(VM_ENTRY_MSR_LOAD_ADDR
, vm_entry_msr_load_addr
),
836 FIELD64(TSC_OFFSET
, tsc_offset
),
837 FIELD64(VIRTUAL_APIC_PAGE_ADDR
, virtual_apic_page_addr
),
838 FIELD64(APIC_ACCESS_ADDR
, apic_access_addr
),
839 FIELD64(POSTED_INTR_DESC_ADDR
, posted_intr_desc_addr
),
840 FIELD64(VM_FUNCTION_CONTROL
, vm_function_control
),
841 FIELD64(EPT_POINTER
, ept_pointer
),
842 FIELD64(EOI_EXIT_BITMAP0
, eoi_exit_bitmap0
),
843 FIELD64(EOI_EXIT_BITMAP1
, eoi_exit_bitmap1
),
844 FIELD64(EOI_EXIT_BITMAP2
, eoi_exit_bitmap2
),
845 FIELD64(EOI_EXIT_BITMAP3
, eoi_exit_bitmap3
),
846 FIELD64(EPTP_LIST_ADDRESS
, eptp_list_address
),
847 FIELD64(XSS_EXIT_BITMAP
, xss_exit_bitmap
),
848 FIELD64(GUEST_PHYSICAL_ADDRESS
, guest_physical_address
),
849 FIELD64(VMCS_LINK_POINTER
, vmcs_link_pointer
),
850 FIELD64(PML_ADDRESS
, pml_address
),
851 FIELD64(GUEST_IA32_DEBUGCTL
, guest_ia32_debugctl
),
852 FIELD64(GUEST_IA32_PAT
, guest_ia32_pat
),
853 FIELD64(GUEST_IA32_EFER
, guest_ia32_efer
),
854 FIELD64(GUEST_IA32_PERF_GLOBAL_CTRL
, guest_ia32_perf_global_ctrl
),
855 FIELD64(GUEST_PDPTR0
, guest_pdptr0
),
856 FIELD64(GUEST_PDPTR1
, guest_pdptr1
),
857 FIELD64(GUEST_PDPTR2
, guest_pdptr2
),
858 FIELD64(GUEST_PDPTR3
, guest_pdptr3
),
859 FIELD64(GUEST_BNDCFGS
, guest_bndcfgs
),
860 FIELD64(HOST_IA32_PAT
, host_ia32_pat
),
861 FIELD64(HOST_IA32_EFER
, host_ia32_efer
),
862 FIELD64(HOST_IA32_PERF_GLOBAL_CTRL
, host_ia32_perf_global_ctrl
),
863 FIELD(PIN_BASED_VM_EXEC_CONTROL
, pin_based_vm_exec_control
),
864 FIELD(CPU_BASED_VM_EXEC_CONTROL
, cpu_based_vm_exec_control
),
865 FIELD(EXCEPTION_BITMAP
, exception_bitmap
),
866 FIELD(PAGE_FAULT_ERROR_CODE_MASK
, page_fault_error_code_mask
),
867 FIELD(PAGE_FAULT_ERROR_CODE_MATCH
, page_fault_error_code_match
),
868 FIELD(CR3_TARGET_COUNT
, cr3_target_count
),
869 FIELD(VM_EXIT_CONTROLS
, vm_exit_controls
),
870 FIELD(VM_EXIT_MSR_STORE_COUNT
, vm_exit_msr_store_count
),
871 FIELD(VM_EXIT_MSR_LOAD_COUNT
, vm_exit_msr_load_count
),
872 FIELD(VM_ENTRY_CONTROLS
, vm_entry_controls
),
873 FIELD(VM_ENTRY_MSR_LOAD_COUNT
, vm_entry_msr_load_count
),
874 FIELD(VM_ENTRY_INTR_INFO_FIELD
, vm_entry_intr_info_field
),
875 FIELD(VM_ENTRY_EXCEPTION_ERROR_CODE
, vm_entry_exception_error_code
),
876 FIELD(VM_ENTRY_INSTRUCTION_LEN
, vm_entry_instruction_len
),
877 FIELD(TPR_THRESHOLD
, tpr_threshold
),
878 FIELD(SECONDARY_VM_EXEC_CONTROL
, secondary_vm_exec_control
),
879 FIELD(VM_INSTRUCTION_ERROR
, vm_instruction_error
),
880 FIELD(VM_EXIT_REASON
, vm_exit_reason
),
881 FIELD(VM_EXIT_INTR_INFO
, vm_exit_intr_info
),
882 FIELD(VM_EXIT_INTR_ERROR_CODE
, vm_exit_intr_error_code
),
883 FIELD(IDT_VECTORING_INFO_FIELD
, idt_vectoring_info_field
),
884 FIELD(IDT_VECTORING_ERROR_CODE
, idt_vectoring_error_code
),
885 FIELD(VM_EXIT_INSTRUCTION_LEN
, vm_exit_instruction_len
),
886 FIELD(VMX_INSTRUCTION_INFO
, vmx_instruction_info
),
887 FIELD(GUEST_ES_LIMIT
, guest_es_limit
),
888 FIELD(GUEST_CS_LIMIT
, guest_cs_limit
),
889 FIELD(GUEST_SS_LIMIT
, guest_ss_limit
),
890 FIELD(GUEST_DS_LIMIT
, guest_ds_limit
),
891 FIELD(GUEST_FS_LIMIT
, guest_fs_limit
),
892 FIELD(GUEST_GS_LIMIT
, guest_gs_limit
),
893 FIELD(GUEST_LDTR_LIMIT
, guest_ldtr_limit
),
894 FIELD(GUEST_TR_LIMIT
, guest_tr_limit
),
895 FIELD(GUEST_GDTR_LIMIT
, guest_gdtr_limit
),
896 FIELD(GUEST_IDTR_LIMIT
, guest_idtr_limit
),
897 FIELD(GUEST_ES_AR_BYTES
, guest_es_ar_bytes
),
898 FIELD(GUEST_CS_AR_BYTES
, guest_cs_ar_bytes
),
899 FIELD(GUEST_SS_AR_BYTES
, guest_ss_ar_bytes
),
900 FIELD(GUEST_DS_AR_BYTES
, guest_ds_ar_bytes
),
901 FIELD(GUEST_FS_AR_BYTES
, guest_fs_ar_bytes
),
902 FIELD(GUEST_GS_AR_BYTES
, guest_gs_ar_bytes
),
903 FIELD(GUEST_LDTR_AR_BYTES
, guest_ldtr_ar_bytes
),
904 FIELD(GUEST_TR_AR_BYTES
, guest_tr_ar_bytes
),
905 FIELD(GUEST_INTERRUPTIBILITY_INFO
, guest_interruptibility_info
),
906 FIELD(GUEST_ACTIVITY_STATE
, guest_activity_state
),
907 FIELD(GUEST_SYSENTER_CS
, guest_sysenter_cs
),
908 FIELD(HOST_IA32_SYSENTER_CS
, host_ia32_sysenter_cs
),
909 FIELD(VMX_PREEMPTION_TIMER_VALUE
, vmx_preemption_timer_value
),
910 FIELD(CR0_GUEST_HOST_MASK
, cr0_guest_host_mask
),
911 FIELD(CR4_GUEST_HOST_MASK
, cr4_guest_host_mask
),
912 FIELD(CR0_READ_SHADOW
, cr0_read_shadow
),
913 FIELD(CR4_READ_SHADOW
, cr4_read_shadow
),
914 FIELD(CR3_TARGET_VALUE0
, cr3_target_value0
),
915 FIELD(CR3_TARGET_VALUE1
, cr3_target_value1
),
916 FIELD(CR3_TARGET_VALUE2
, cr3_target_value2
),
917 FIELD(CR3_TARGET_VALUE3
, cr3_target_value3
),
918 FIELD(EXIT_QUALIFICATION
, exit_qualification
),
919 FIELD(GUEST_LINEAR_ADDRESS
, guest_linear_address
),
920 FIELD(GUEST_CR0
, guest_cr0
),
921 FIELD(GUEST_CR3
, guest_cr3
),
922 FIELD(GUEST_CR4
, guest_cr4
),
923 FIELD(GUEST_ES_BASE
, guest_es_base
),
924 FIELD(GUEST_CS_BASE
, guest_cs_base
),
925 FIELD(GUEST_SS_BASE
, guest_ss_base
),
926 FIELD(GUEST_DS_BASE
, guest_ds_base
),
927 FIELD(GUEST_FS_BASE
, guest_fs_base
),
928 FIELD(GUEST_GS_BASE
, guest_gs_base
),
929 FIELD(GUEST_LDTR_BASE
, guest_ldtr_base
),
930 FIELD(GUEST_TR_BASE
, guest_tr_base
),
931 FIELD(GUEST_GDTR_BASE
, guest_gdtr_base
),
932 FIELD(GUEST_IDTR_BASE
, guest_idtr_base
),
933 FIELD(GUEST_DR7
, guest_dr7
),
934 FIELD(GUEST_RSP
, guest_rsp
),
935 FIELD(GUEST_RIP
, guest_rip
),
936 FIELD(GUEST_RFLAGS
, guest_rflags
),
937 FIELD(GUEST_PENDING_DBG_EXCEPTIONS
, guest_pending_dbg_exceptions
),
938 FIELD(GUEST_SYSENTER_ESP
, guest_sysenter_esp
),
939 FIELD(GUEST_SYSENTER_EIP
, guest_sysenter_eip
),
940 FIELD(HOST_CR0
, host_cr0
),
941 FIELD(HOST_CR3
, host_cr3
),
942 FIELD(HOST_CR4
, host_cr4
),
943 FIELD(HOST_FS_BASE
, host_fs_base
),
944 FIELD(HOST_GS_BASE
, host_gs_base
),
945 FIELD(HOST_TR_BASE
, host_tr_base
),
946 FIELD(HOST_GDTR_BASE
, host_gdtr_base
),
947 FIELD(HOST_IDTR_BASE
, host_idtr_base
),
948 FIELD(HOST_IA32_SYSENTER_ESP
, host_ia32_sysenter_esp
),
949 FIELD(HOST_IA32_SYSENTER_EIP
, host_ia32_sysenter_eip
),
950 FIELD(HOST_RSP
, host_rsp
),
951 FIELD(HOST_RIP
, host_rip
),
954 static inline short vmcs_field_to_offset(unsigned long field
)
956 const size_t size
= ARRAY_SIZE(vmcs_field_to_offset_table
);
957 unsigned short offset
;
959 BUILD_BUG_ON(size
> SHRT_MAX
);
963 field
= array_index_nospec(field
, size
);
964 offset
= vmcs_field_to_offset_table
[field
];
970 static inline struct vmcs12
*get_vmcs12(struct kvm_vcpu
*vcpu
)
972 return to_vmx(vcpu
)->nested
.cached_vmcs12
;
975 static bool nested_ept_ad_enabled(struct kvm_vcpu
*vcpu
);
976 static unsigned long nested_ept_get_cr3(struct kvm_vcpu
*vcpu
);
977 static u64
construct_eptp(struct kvm_vcpu
*vcpu
, unsigned long root_hpa
);
978 static bool vmx_xsaves_supported(void);
979 static void vmx_set_segment(struct kvm_vcpu
*vcpu
,
980 struct kvm_segment
*var
, int seg
);
981 static void vmx_get_segment(struct kvm_vcpu
*vcpu
,
982 struct kvm_segment
*var
, int seg
);
983 static bool guest_state_valid(struct kvm_vcpu
*vcpu
);
984 static u32
vmx_segment_access_rights(struct kvm_segment
*var
);
985 static void copy_shadow_to_vmcs12(struct vcpu_vmx
*vmx
);
986 static bool vmx_get_nmi_mask(struct kvm_vcpu
*vcpu
);
987 static void vmx_set_nmi_mask(struct kvm_vcpu
*vcpu
, bool masked
);
988 static bool nested_vmx_is_page_fault_vmexit(struct vmcs12
*vmcs12
,
990 static void vmx_update_msr_bitmap(struct kvm_vcpu
*vcpu
);
991 static void __always_inline
vmx_disable_intercept_for_msr(unsigned long *msr_bitmap
,
994 static DEFINE_PER_CPU(struct vmcs
*, vmxarea
);
995 static DEFINE_PER_CPU(struct vmcs
*, current_vmcs
);
997 * We maintain a per-CPU linked-list of VMCS loaded on that CPU. This is needed
998 * when a CPU is brought down, and we need to VMCLEAR all VMCSs loaded on it.
1000 static DEFINE_PER_CPU(struct list_head
, loaded_vmcss_on_cpu
);
1003 * We maintian a per-CPU linked-list of vCPU, so in wakeup_handler() we
1004 * can find which vCPU should be waken up.
1006 static DEFINE_PER_CPU(struct list_head
, blocked_vcpu_on_cpu
);
1007 static DEFINE_PER_CPU(spinlock_t
, blocked_vcpu_on_cpu_lock
);
1017 static unsigned long *vmx_bitmap
[VMX_BITMAP_NR
];
1019 #define vmx_io_bitmap_a (vmx_bitmap[VMX_IO_BITMAP_A])
1020 #define vmx_io_bitmap_b (vmx_bitmap[VMX_IO_BITMAP_B])
1021 #define vmx_vmread_bitmap (vmx_bitmap[VMX_VMREAD_BITMAP])
1022 #define vmx_vmwrite_bitmap (vmx_bitmap[VMX_VMWRITE_BITMAP])
1024 static bool cpu_has_load_ia32_efer
;
1025 static bool cpu_has_load_perf_global_ctrl
;
1027 static DECLARE_BITMAP(vmx_vpid_bitmap
, VMX_NR_VPIDS
);
1028 static DEFINE_SPINLOCK(vmx_vpid_lock
);
1030 static struct vmcs_config
{
1035 u32 pin_based_exec_ctrl
;
1036 u32 cpu_based_exec_ctrl
;
1037 u32 cpu_based_2nd_exec_ctrl
;
1042 static struct vmx_capability
{
1047 #define VMX_SEGMENT_FIELD(seg) \
1048 [VCPU_SREG_##seg] = { \
1049 .selector = GUEST_##seg##_SELECTOR, \
1050 .base = GUEST_##seg##_BASE, \
1051 .limit = GUEST_##seg##_LIMIT, \
1052 .ar_bytes = GUEST_##seg##_AR_BYTES, \
1055 static const struct kvm_vmx_segment_field
{
1060 } kvm_vmx_segment_fields
[] = {
1061 VMX_SEGMENT_FIELD(CS
),
1062 VMX_SEGMENT_FIELD(DS
),
1063 VMX_SEGMENT_FIELD(ES
),
1064 VMX_SEGMENT_FIELD(FS
),
1065 VMX_SEGMENT_FIELD(GS
),
1066 VMX_SEGMENT_FIELD(SS
),
1067 VMX_SEGMENT_FIELD(TR
),
1068 VMX_SEGMENT_FIELD(LDTR
),
1071 static u64 host_efer
;
1073 static void ept_save_pdptrs(struct kvm_vcpu
*vcpu
);
1076 * Keep MSR_STAR at the end, as setup_msrs() will try to optimize it
1077 * away by decrementing the array size.
1079 static const u32 vmx_msr_index
[] = {
1080 #ifdef CONFIG_X86_64
1081 MSR_SYSCALL_MASK
, MSR_LSTAR
, MSR_CSTAR
,
1083 MSR_EFER
, MSR_TSC_AUX
, MSR_STAR
,
1086 static inline bool is_exception_n(u32 intr_info
, u8 vector
)
1088 return (intr_info
& (INTR_INFO_INTR_TYPE_MASK
| INTR_INFO_VECTOR_MASK
|
1089 INTR_INFO_VALID_MASK
)) ==
1090 (INTR_TYPE_HARD_EXCEPTION
| vector
| INTR_INFO_VALID_MASK
);
1093 static inline bool is_debug(u32 intr_info
)
1095 return is_exception_n(intr_info
, DB_VECTOR
);
1098 static inline bool is_breakpoint(u32 intr_info
)
1100 return is_exception_n(intr_info
, BP_VECTOR
);
1103 static inline bool is_page_fault(u32 intr_info
)
1105 return is_exception_n(intr_info
, PF_VECTOR
);
1108 static inline bool is_no_device(u32 intr_info
)
1110 return is_exception_n(intr_info
, NM_VECTOR
);
1113 static inline bool is_invalid_opcode(u32 intr_info
)
1115 return is_exception_n(intr_info
, UD_VECTOR
);
1118 static inline bool is_external_interrupt(u32 intr_info
)
1120 return (intr_info
& (INTR_INFO_INTR_TYPE_MASK
| INTR_INFO_VALID_MASK
))
1121 == (INTR_TYPE_EXT_INTR
| INTR_INFO_VALID_MASK
);
1124 static inline bool is_machine_check(u32 intr_info
)
1126 return (intr_info
& (INTR_INFO_INTR_TYPE_MASK
| INTR_INFO_VECTOR_MASK
|
1127 INTR_INFO_VALID_MASK
)) ==
1128 (INTR_TYPE_HARD_EXCEPTION
| MC_VECTOR
| INTR_INFO_VALID_MASK
);
1131 /* Undocumented: icebp/int1 */
1132 static inline bool is_icebp(u32 intr_info
)
1134 return (intr_info
& (INTR_INFO_INTR_TYPE_MASK
| INTR_INFO_VALID_MASK
))
1135 == (INTR_TYPE_PRIV_SW_EXCEPTION
| INTR_INFO_VALID_MASK
);
1138 static inline bool cpu_has_vmx_msr_bitmap(void)
1140 return vmcs_config
.cpu_based_exec_ctrl
& CPU_BASED_USE_MSR_BITMAPS
;
1143 static inline bool cpu_has_vmx_tpr_shadow(void)
1145 return vmcs_config
.cpu_based_exec_ctrl
& CPU_BASED_TPR_SHADOW
;
1148 static inline bool cpu_need_tpr_shadow(struct kvm_vcpu
*vcpu
)
1150 return cpu_has_vmx_tpr_shadow() && lapic_in_kernel(vcpu
);
1153 static inline bool cpu_has_secondary_exec_ctrls(void)
1155 return vmcs_config
.cpu_based_exec_ctrl
&
1156 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS
;
1159 static inline bool cpu_has_vmx_virtualize_apic_accesses(void)
1161 return vmcs_config
.cpu_based_2nd_exec_ctrl
&
1162 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES
;
1165 static inline bool cpu_has_vmx_virtualize_x2apic_mode(void)
1167 return vmcs_config
.cpu_based_2nd_exec_ctrl
&
1168 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE
;
1171 static inline bool cpu_has_vmx_apic_register_virt(void)
1173 return vmcs_config
.cpu_based_2nd_exec_ctrl
&
1174 SECONDARY_EXEC_APIC_REGISTER_VIRT
;
1177 static inline bool cpu_has_vmx_virtual_intr_delivery(void)
1179 return vmcs_config
.cpu_based_2nd_exec_ctrl
&
1180 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY
;
1184 * Comment's format: document - errata name - stepping - processor name.
1186 * https://www.virtualbox.org/svn/vbox/trunk/src/VBox/VMM/VMMR0/HMR0.cpp
1188 static u32 vmx_preemption_cpu_tfms
[] = {
1189 /* 323344.pdf - BA86 - D0 - Xeon 7500 Series */
1191 /* 323056.pdf - AAX65 - C2 - Xeon L3406 */
1192 /* 322814.pdf - AAT59 - C2 - i7-600, i5-500, i5-400 and i3-300 Mobile */
1193 /* 322911.pdf - AAU65 - C2 - i5-600, i3-500 Desktop and Pentium G6950 */
1195 /* 322911.pdf - AAU65 - K0 - i5-600, i3-500 Desktop and Pentium G6950 */
1197 /* 322373.pdf - AAO95 - B1 - Xeon 3400 Series */
1198 /* 322166.pdf - AAN92 - B1 - i7-800 and i5-700 Desktop */
1200 * 320767.pdf - AAP86 - B1 -
1201 * i7-900 Mobile Extreme, i7-800 and i7-700 Mobile
1204 /* 321333.pdf - AAM126 - C0 - Xeon 3500 */
1206 /* 321333.pdf - AAM126 - C1 - Xeon 3500 */
1208 /* 320836.pdf - AAJ124 - C0 - i7-900 Desktop Extreme and i7-900 Desktop */
1210 /* 321333.pdf - AAM126 - D0 - Xeon 3500 */
1211 /* 321324.pdf - AAK139 - D0 - Xeon 5500 */
1212 /* 320836.pdf - AAJ124 - D0 - i7-900 Extreme and i7-900 Desktop */
1216 static inline bool cpu_has_broken_vmx_preemption_timer(void)
1218 u32 eax
= cpuid_eax(0x00000001), i
;
1220 /* Clear the reserved bits */
1221 eax
&= ~(0x3U
<< 14 | 0xfU
<< 28);
1222 for (i
= 0; i
< ARRAY_SIZE(vmx_preemption_cpu_tfms
); i
++)
1223 if (eax
== vmx_preemption_cpu_tfms
[i
])
1229 static inline bool cpu_has_vmx_preemption_timer(void)
1231 return vmcs_config
.pin_based_exec_ctrl
&
1232 PIN_BASED_VMX_PREEMPTION_TIMER
;
1235 static inline bool cpu_has_vmx_posted_intr(void)
1237 return IS_ENABLED(CONFIG_X86_LOCAL_APIC
) &&
1238 vmcs_config
.pin_based_exec_ctrl
& PIN_BASED_POSTED_INTR
;
1241 static inline bool cpu_has_vmx_apicv(void)
1243 return cpu_has_vmx_apic_register_virt() &&
1244 cpu_has_vmx_virtual_intr_delivery() &&
1245 cpu_has_vmx_posted_intr();
1248 static inline bool cpu_has_vmx_flexpriority(void)
1250 return cpu_has_vmx_tpr_shadow() &&
1251 cpu_has_vmx_virtualize_apic_accesses();
1254 static inline bool cpu_has_vmx_ept_execute_only(void)
1256 return vmx_capability
.ept
& VMX_EPT_EXECUTE_ONLY_BIT
;
1259 static inline bool cpu_has_vmx_ept_2m_page(void)
1261 return vmx_capability
.ept
& VMX_EPT_2MB_PAGE_BIT
;
1264 static inline bool cpu_has_vmx_ept_1g_page(void)
1266 return vmx_capability
.ept
& VMX_EPT_1GB_PAGE_BIT
;
1269 static inline bool cpu_has_vmx_ept_4levels(void)
1271 return vmx_capability
.ept
& VMX_EPT_PAGE_WALK_4_BIT
;
1274 static inline bool cpu_has_vmx_ept_mt_wb(void)
1276 return vmx_capability
.ept
& VMX_EPTP_WB_BIT
;
1279 static inline bool cpu_has_vmx_ept_5levels(void)
1281 return vmx_capability
.ept
& VMX_EPT_PAGE_WALK_5_BIT
;
1284 static inline bool cpu_has_vmx_ept_ad_bits(void)
1286 return vmx_capability
.ept
& VMX_EPT_AD_BIT
;
1289 static inline bool cpu_has_vmx_invept_context(void)
1291 return vmx_capability
.ept
& VMX_EPT_EXTENT_CONTEXT_BIT
;
1294 static inline bool cpu_has_vmx_invept_global(void)
1296 return vmx_capability
.ept
& VMX_EPT_EXTENT_GLOBAL_BIT
;
1299 static inline bool cpu_has_vmx_invvpid_single(void)
1301 return vmx_capability
.vpid
& VMX_VPID_EXTENT_SINGLE_CONTEXT_BIT
;
1304 static inline bool cpu_has_vmx_invvpid_global(void)
1306 return vmx_capability
.vpid
& VMX_VPID_EXTENT_GLOBAL_CONTEXT_BIT
;
1309 static inline bool cpu_has_vmx_invvpid(void)
1311 return vmx_capability
.vpid
& VMX_VPID_INVVPID_BIT
;
1314 static inline bool cpu_has_vmx_ept(void)
1316 return vmcs_config
.cpu_based_2nd_exec_ctrl
&
1317 SECONDARY_EXEC_ENABLE_EPT
;
1320 static inline bool cpu_has_vmx_unrestricted_guest(void)
1322 return vmcs_config
.cpu_based_2nd_exec_ctrl
&
1323 SECONDARY_EXEC_UNRESTRICTED_GUEST
;
1326 static inline bool cpu_has_vmx_ple(void)
1328 return vmcs_config
.cpu_based_2nd_exec_ctrl
&
1329 SECONDARY_EXEC_PAUSE_LOOP_EXITING
;
1332 static inline bool cpu_has_vmx_basic_inout(void)
1334 return (((u64
)vmcs_config
.basic_cap
<< 32) & VMX_BASIC_INOUT
);
1337 static inline bool cpu_need_virtualize_apic_accesses(struct kvm_vcpu
*vcpu
)
1339 return flexpriority_enabled
&& lapic_in_kernel(vcpu
);
1342 static inline bool cpu_has_vmx_vpid(void)
1344 return vmcs_config
.cpu_based_2nd_exec_ctrl
&
1345 SECONDARY_EXEC_ENABLE_VPID
;
1348 static inline bool cpu_has_vmx_rdtscp(void)
1350 return vmcs_config
.cpu_based_2nd_exec_ctrl
&
1351 SECONDARY_EXEC_RDTSCP
;
1354 static inline bool cpu_has_vmx_invpcid(void)
1356 return vmcs_config
.cpu_based_2nd_exec_ctrl
&
1357 SECONDARY_EXEC_ENABLE_INVPCID
;
1360 static inline bool cpu_has_virtual_nmis(void)
1362 return vmcs_config
.pin_based_exec_ctrl
& PIN_BASED_VIRTUAL_NMIS
;
1365 static inline bool cpu_has_vmx_wbinvd_exit(void)
1367 return vmcs_config
.cpu_based_2nd_exec_ctrl
&
1368 SECONDARY_EXEC_WBINVD_EXITING
;
1371 static inline bool cpu_has_vmx_shadow_vmcs(void)
1374 rdmsrl(MSR_IA32_VMX_MISC
, vmx_msr
);
1375 /* check if the cpu supports writing r/o exit information fields */
1376 if (!(vmx_msr
& MSR_IA32_VMX_MISC_VMWRITE_SHADOW_RO_FIELDS
))
1379 return vmcs_config
.cpu_based_2nd_exec_ctrl
&
1380 SECONDARY_EXEC_SHADOW_VMCS
;
1383 static inline bool cpu_has_vmx_pml(void)
1385 return vmcs_config
.cpu_based_2nd_exec_ctrl
& SECONDARY_EXEC_ENABLE_PML
;
1388 static inline bool cpu_has_vmx_tsc_scaling(void)
1390 return vmcs_config
.cpu_based_2nd_exec_ctrl
&
1391 SECONDARY_EXEC_TSC_SCALING
;
1394 static inline bool cpu_has_vmx_vmfunc(void)
1396 return vmcs_config
.cpu_based_2nd_exec_ctrl
&
1397 SECONDARY_EXEC_ENABLE_VMFUNC
;
1400 static inline bool report_flexpriority(void)
1402 return flexpriority_enabled
;
1405 static inline unsigned nested_cpu_vmx_misc_cr3_count(struct kvm_vcpu
*vcpu
)
1407 return vmx_misc_cr3_count(to_vmx(vcpu
)->nested
.nested_vmx_misc_low
);
1410 static inline bool nested_cpu_has(struct vmcs12
*vmcs12
, u32 bit
)
1412 return vmcs12
->cpu_based_vm_exec_control
& bit
;
1415 static inline bool nested_cpu_has2(struct vmcs12
*vmcs12
, u32 bit
)
1417 return (vmcs12
->cpu_based_vm_exec_control
&
1418 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS
) &&
1419 (vmcs12
->secondary_vm_exec_control
& bit
);
1422 static inline bool nested_cpu_has_preemption_timer(struct vmcs12
*vmcs12
)
1424 return vmcs12
->pin_based_vm_exec_control
&
1425 PIN_BASED_VMX_PREEMPTION_TIMER
;
1428 static inline int nested_cpu_has_ept(struct vmcs12
*vmcs12
)
1430 return nested_cpu_has2(vmcs12
, SECONDARY_EXEC_ENABLE_EPT
);
1433 static inline bool nested_cpu_has_xsaves(struct vmcs12
*vmcs12
)
1435 return nested_cpu_has2(vmcs12
, SECONDARY_EXEC_XSAVES
);
1438 static inline bool nested_cpu_has_pml(struct vmcs12
*vmcs12
)
1440 return nested_cpu_has2(vmcs12
, SECONDARY_EXEC_ENABLE_PML
);
1443 static inline bool nested_cpu_has_virt_x2apic_mode(struct vmcs12
*vmcs12
)
1445 return nested_cpu_has2(vmcs12
, SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE
);
1448 static inline bool nested_cpu_has_vpid(struct vmcs12
*vmcs12
)
1450 return nested_cpu_has2(vmcs12
, SECONDARY_EXEC_ENABLE_VPID
);
1453 static inline bool nested_cpu_has_apic_reg_virt(struct vmcs12
*vmcs12
)
1455 return nested_cpu_has2(vmcs12
, SECONDARY_EXEC_APIC_REGISTER_VIRT
);
1458 static inline bool nested_cpu_has_vid(struct vmcs12
*vmcs12
)
1460 return nested_cpu_has2(vmcs12
, SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY
);
1463 static inline bool nested_cpu_has_posted_intr(struct vmcs12
*vmcs12
)
1465 return vmcs12
->pin_based_vm_exec_control
& PIN_BASED_POSTED_INTR
;
1468 static inline bool nested_cpu_has_vmfunc(struct vmcs12
*vmcs12
)
1470 return nested_cpu_has2(vmcs12
, SECONDARY_EXEC_ENABLE_VMFUNC
);
1473 static inline bool nested_cpu_has_eptp_switching(struct vmcs12
*vmcs12
)
1475 return nested_cpu_has_vmfunc(vmcs12
) &&
1476 (vmcs12
->vm_function_control
&
1477 VMX_VMFUNC_EPTP_SWITCHING
);
1480 static inline bool is_nmi(u32 intr_info
)
1482 return (intr_info
& (INTR_INFO_INTR_TYPE_MASK
| INTR_INFO_VALID_MASK
))
1483 == (INTR_TYPE_NMI_INTR
| INTR_INFO_VALID_MASK
);
1486 static void nested_vmx_vmexit(struct kvm_vcpu
*vcpu
, u32 exit_reason
,
1488 unsigned long exit_qualification
);
1489 static void nested_vmx_entry_failure(struct kvm_vcpu
*vcpu
,
1490 struct vmcs12
*vmcs12
,
1491 u32 reason
, unsigned long qualification
);
1493 static int __find_msr_index(struct vcpu_vmx
*vmx
, u32 msr
)
1497 for (i
= 0; i
< vmx
->nmsrs
; ++i
)
1498 if (vmx_msr_index
[vmx
->guest_msrs
[i
].index
] == msr
)
1503 static inline void __invvpid(int ext
, u16 vpid
, gva_t gva
)
1509 } operand
= { vpid
, 0, gva
};
1511 asm volatile (__ex(ASM_VMX_INVVPID
)
1512 /* CF==1 or ZF==1 --> rc = -1 */
1513 "; ja 1f ; ud2 ; 1:"
1514 : : "a"(&operand
), "c"(ext
) : "cc", "memory");
1517 static inline void __invept(int ext
, u64 eptp
, gpa_t gpa
)
1521 } operand
= {eptp
, gpa
};
1523 asm volatile (__ex(ASM_VMX_INVEPT
)
1524 /* CF==1 or ZF==1 --> rc = -1 */
1525 "; ja 1f ; ud2 ; 1:\n"
1526 : : "a" (&operand
), "c" (ext
) : "cc", "memory");
1529 static struct shared_msr_entry
*find_msr_entry(struct vcpu_vmx
*vmx
, u32 msr
)
1533 i
= __find_msr_index(vmx
, msr
);
1535 return &vmx
->guest_msrs
[i
];
1539 static void vmcs_clear(struct vmcs
*vmcs
)
1541 u64 phys_addr
= __pa(vmcs
);
1544 asm volatile (__ex(ASM_VMX_VMCLEAR_RAX
) "; setna %0"
1545 : "=qm"(error
) : "a"(&phys_addr
), "m"(phys_addr
)
1548 printk(KERN_ERR
"kvm: vmclear fail: %p/%llx\n",
1552 static inline void loaded_vmcs_init(struct loaded_vmcs
*loaded_vmcs
)
1554 vmcs_clear(loaded_vmcs
->vmcs
);
1555 if (loaded_vmcs
->shadow_vmcs
&& loaded_vmcs
->launched
)
1556 vmcs_clear(loaded_vmcs
->shadow_vmcs
);
1557 loaded_vmcs
->cpu
= -1;
1558 loaded_vmcs
->launched
= 0;
1561 static void vmcs_load(struct vmcs
*vmcs
)
1563 u64 phys_addr
= __pa(vmcs
);
1566 asm volatile (__ex(ASM_VMX_VMPTRLD_RAX
) "; setna %0"
1567 : "=qm"(error
) : "a"(&phys_addr
), "m"(phys_addr
)
1570 printk(KERN_ERR
"kvm: vmptrld %p/%llx failed\n",
1574 #ifdef CONFIG_KEXEC_CORE
1576 * This bitmap is used to indicate whether the vmclear
1577 * operation is enabled on all cpus. All disabled by
1580 static cpumask_t crash_vmclear_enabled_bitmap
= CPU_MASK_NONE
;
1582 static inline void crash_enable_local_vmclear(int cpu
)
1584 cpumask_set_cpu(cpu
, &crash_vmclear_enabled_bitmap
);
1587 static inline void crash_disable_local_vmclear(int cpu
)
1589 cpumask_clear_cpu(cpu
, &crash_vmclear_enabled_bitmap
);
1592 static inline int crash_local_vmclear_enabled(int cpu
)
1594 return cpumask_test_cpu(cpu
, &crash_vmclear_enabled_bitmap
);
1597 static void crash_vmclear_local_loaded_vmcss(void)
1599 int cpu
= raw_smp_processor_id();
1600 struct loaded_vmcs
*v
;
1602 if (!crash_local_vmclear_enabled(cpu
))
1605 list_for_each_entry(v
, &per_cpu(loaded_vmcss_on_cpu
, cpu
),
1606 loaded_vmcss_on_cpu_link
)
1607 vmcs_clear(v
->vmcs
);
1610 static inline void crash_enable_local_vmclear(int cpu
) { }
1611 static inline void crash_disable_local_vmclear(int cpu
) { }
1612 #endif /* CONFIG_KEXEC_CORE */
1614 static void __loaded_vmcs_clear(void *arg
)
1616 struct loaded_vmcs
*loaded_vmcs
= arg
;
1617 int cpu
= raw_smp_processor_id();
1619 if (loaded_vmcs
->cpu
!= cpu
)
1620 return; /* vcpu migration can race with cpu offline */
1621 if (per_cpu(current_vmcs
, cpu
) == loaded_vmcs
->vmcs
)
1622 per_cpu(current_vmcs
, cpu
) = NULL
;
1623 crash_disable_local_vmclear(cpu
);
1624 list_del(&loaded_vmcs
->loaded_vmcss_on_cpu_link
);
1627 * we should ensure updating loaded_vmcs->loaded_vmcss_on_cpu_link
1628 * is before setting loaded_vmcs->vcpu to -1 which is done in
1629 * loaded_vmcs_init. Otherwise, other cpu can see vcpu = -1 fist
1630 * then adds the vmcs into percpu list before it is deleted.
1634 loaded_vmcs_init(loaded_vmcs
);
1635 crash_enable_local_vmclear(cpu
);
1638 static void loaded_vmcs_clear(struct loaded_vmcs
*loaded_vmcs
)
1640 int cpu
= loaded_vmcs
->cpu
;
1643 smp_call_function_single(cpu
,
1644 __loaded_vmcs_clear
, loaded_vmcs
, 1);
1647 static inline void vpid_sync_vcpu_single(int vpid
)
1652 if (cpu_has_vmx_invvpid_single())
1653 __invvpid(VMX_VPID_EXTENT_SINGLE_CONTEXT
, vpid
, 0);
1656 static inline void vpid_sync_vcpu_global(void)
1658 if (cpu_has_vmx_invvpid_global())
1659 __invvpid(VMX_VPID_EXTENT_ALL_CONTEXT
, 0, 0);
1662 static inline void vpid_sync_context(int vpid
)
1664 if (cpu_has_vmx_invvpid_single())
1665 vpid_sync_vcpu_single(vpid
);
1667 vpid_sync_vcpu_global();
1670 static inline void ept_sync_global(void)
1672 __invept(VMX_EPT_EXTENT_GLOBAL
, 0, 0);
1675 static inline void ept_sync_context(u64 eptp
)
1677 if (cpu_has_vmx_invept_context())
1678 __invept(VMX_EPT_EXTENT_CONTEXT
, eptp
, 0);
1683 static __always_inline
void vmcs_check16(unsigned long field
)
1685 BUILD_BUG_ON_MSG(__builtin_constant_p(field
) && ((field
) & 0x6001) == 0x2000,
1686 "16-bit accessor invalid for 64-bit field");
1687 BUILD_BUG_ON_MSG(__builtin_constant_p(field
) && ((field
) & 0x6001) == 0x2001,
1688 "16-bit accessor invalid for 64-bit high field");
1689 BUILD_BUG_ON_MSG(__builtin_constant_p(field
) && ((field
) & 0x6000) == 0x4000,
1690 "16-bit accessor invalid for 32-bit high field");
1691 BUILD_BUG_ON_MSG(__builtin_constant_p(field
) && ((field
) & 0x6000) == 0x6000,
1692 "16-bit accessor invalid for natural width field");
1695 static __always_inline
void vmcs_check32(unsigned long field
)
1697 BUILD_BUG_ON_MSG(__builtin_constant_p(field
) && ((field
) & 0x6000) == 0,
1698 "32-bit accessor invalid for 16-bit field");
1699 BUILD_BUG_ON_MSG(__builtin_constant_p(field
) && ((field
) & 0x6000) == 0x6000,
1700 "32-bit accessor invalid for natural width field");
1703 static __always_inline
void vmcs_check64(unsigned long field
)
1705 BUILD_BUG_ON_MSG(__builtin_constant_p(field
) && ((field
) & 0x6000) == 0,
1706 "64-bit accessor invalid for 16-bit field");
1707 BUILD_BUG_ON_MSG(__builtin_constant_p(field
) && ((field
) & 0x6001) == 0x2001,
1708 "64-bit accessor invalid for 64-bit high field");
1709 BUILD_BUG_ON_MSG(__builtin_constant_p(field
) && ((field
) & 0x6000) == 0x4000,
1710 "64-bit accessor invalid for 32-bit field");
1711 BUILD_BUG_ON_MSG(__builtin_constant_p(field
) && ((field
) & 0x6000) == 0x6000,
1712 "64-bit accessor invalid for natural width field");
1715 static __always_inline
void vmcs_checkl(unsigned long field
)
1717 BUILD_BUG_ON_MSG(__builtin_constant_p(field
) && ((field
) & 0x6000) == 0,
1718 "Natural width accessor invalid for 16-bit field");
1719 BUILD_BUG_ON_MSG(__builtin_constant_p(field
) && ((field
) & 0x6001) == 0x2000,
1720 "Natural width accessor invalid for 64-bit field");
1721 BUILD_BUG_ON_MSG(__builtin_constant_p(field
) && ((field
) & 0x6001) == 0x2001,
1722 "Natural width accessor invalid for 64-bit high field");
1723 BUILD_BUG_ON_MSG(__builtin_constant_p(field
) && ((field
) & 0x6000) == 0x4000,
1724 "Natural width accessor invalid for 32-bit field");
1727 static __always_inline
unsigned long __vmcs_readl(unsigned long field
)
1729 unsigned long value
;
1731 asm volatile (__ex_clear(ASM_VMX_VMREAD_RDX_RAX
, "%0")
1732 : "=a"(value
) : "d"(field
) : "cc");
1736 static __always_inline u16
vmcs_read16(unsigned long field
)
1738 vmcs_check16(field
);
1739 return __vmcs_readl(field
);
1742 static __always_inline u32
vmcs_read32(unsigned long field
)
1744 vmcs_check32(field
);
1745 return __vmcs_readl(field
);
1748 static __always_inline u64
vmcs_read64(unsigned long field
)
1750 vmcs_check64(field
);
1751 #ifdef CONFIG_X86_64
1752 return __vmcs_readl(field
);
1754 return __vmcs_readl(field
) | ((u64
)__vmcs_readl(field
+1) << 32);
1758 static __always_inline
unsigned long vmcs_readl(unsigned long field
)
1761 return __vmcs_readl(field
);
1764 static noinline
void vmwrite_error(unsigned long field
, unsigned long value
)
1766 printk(KERN_ERR
"vmwrite error: reg %lx value %lx (err %d)\n",
1767 field
, value
, vmcs_read32(VM_INSTRUCTION_ERROR
));
1771 static __always_inline
void __vmcs_writel(unsigned long field
, unsigned long value
)
1775 asm volatile (__ex(ASM_VMX_VMWRITE_RAX_RDX
) "; setna %0"
1776 : "=q"(error
) : "a"(value
), "d"(field
) : "cc");
1777 if (unlikely(error
))
1778 vmwrite_error(field
, value
);
1781 static __always_inline
void vmcs_write16(unsigned long field
, u16 value
)
1783 vmcs_check16(field
);
1784 __vmcs_writel(field
, value
);
1787 static __always_inline
void vmcs_write32(unsigned long field
, u32 value
)
1789 vmcs_check32(field
);
1790 __vmcs_writel(field
, value
);
1793 static __always_inline
void vmcs_write64(unsigned long field
, u64 value
)
1795 vmcs_check64(field
);
1796 __vmcs_writel(field
, value
);
1797 #ifndef CONFIG_X86_64
1799 __vmcs_writel(field
+1, value
>> 32);
1803 static __always_inline
void vmcs_writel(unsigned long field
, unsigned long value
)
1806 __vmcs_writel(field
, value
);
1809 static __always_inline
void vmcs_clear_bits(unsigned long field
, u32 mask
)
1811 BUILD_BUG_ON_MSG(__builtin_constant_p(field
) && ((field
) & 0x6000) == 0x2000,
1812 "vmcs_clear_bits does not support 64-bit fields");
1813 __vmcs_writel(field
, __vmcs_readl(field
) & ~mask
);
1816 static __always_inline
void vmcs_set_bits(unsigned long field
, u32 mask
)
1818 BUILD_BUG_ON_MSG(__builtin_constant_p(field
) && ((field
) & 0x6000) == 0x2000,
1819 "vmcs_set_bits does not support 64-bit fields");
1820 __vmcs_writel(field
, __vmcs_readl(field
) | mask
);
1823 static inline void vm_entry_controls_reset_shadow(struct vcpu_vmx
*vmx
)
1825 vmx
->vm_entry_controls_shadow
= vmcs_read32(VM_ENTRY_CONTROLS
);
1828 static inline void vm_entry_controls_init(struct vcpu_vmx
*vmx
, u32 val
)
1830 vmcs_write32(VM_ENTRY_CONTROLS
, val
);
1831 vmx
->vm_entry_controls_shadow
= val
;
1834 static inline void vm_entry_controls_set(struct vcpu_vmx
*vmx
, u32 val
)
1836 if (vmx
->vm_entry_controls_shadow
!= val
)
1837 vm_entry_controls_init(vmx
, val
);
1840 static inline u32
vm_entry_controls_get(struct vcpu_vmx
*vmx
)
1842 return vmx
->vm_entry_controls_shadow
;
1846 static inline void vm_entry_controls_setbit(struct vcpu_vmx
*vmx
, u32 val
)
1848 vm_entry_controls_set(vmx
, vm_entry_controls_get(vmx
) | val
);
1851 static inline void vm_entry_controls_clearbit(struct vcpu_vmx
*vmx
, u32 val
)
1853 vm_entry_controls_set(vmx
, vm_entry_controls_get(vmx
) & ~val
);
1856 static inline void vm_exit_controls_reset_shadow(struct vcpu_vmx
*vmx
)
1858 vmx
->vm_exit_controls_shadow
= vmcs_read32(VM_EXIT_CONTROLS
);
1861 static inline void vm_exit_controls_init(struct vcpu_vmx
*vmx
, u32 val
)
1863 vmcs_write32(VM_EXIT_CONTROLS
, val
);
1864 vmx
->vm_exit_controls_shadow
= val
;
1867 static inline void vm_exit_controls_set(struct vcpu_vmx
*vmx
, u32 val
)
1869 if (vmx
->vm_exit_controls_shadow
!= val
)
1870 vm_exit_controls_init(vmx
, val
);
1873 static inline u32
vm_exit_controls_get(struct vcpu_vmx
*vmx
)
1875 return vmx
->vm_exit_controls_shadow
;
1879 static inline void vm_exit_controls_setbit(struct vcpu_vmx
*vmx
, u32 val
)
1881 vm_exit_controls_set(vmx
, vm_exit_controls_get(vmx
) | val
);
1884 static inline void vm_exit_controls_clearbit(struct vcpu_vmx
*vmx
, u32 val
)
1886 vm_exit_controls_set(vmx
, vm_exit_controls_get(vmx
) & ~val
);
1889 static void vmx_segment_cache_clear(struct vcpu_vmx
*vmx
)
1891 vmx
->segment_cache
.bitmask
= 0;
1894 static bool vmx_segment_cache_test_set(struct vcpu_vmx
*vmx
, unsigned seg
,
1898 u32 mask
= 1 << (seg
* SEG_FIELD_NR
+ field
);
1900 if (!(vmx
->vcpu
.arch
.regs_avail
& (1 << VCPU_EXREG_SEGMENTS
))) {
1901 vmx
->vcpu
.arch
.regs_avail
|= (1 << VCPU_EXREG_SEGMENTS
);
1902 vmx
->segment_cache
.bitmask
= 0;
1904 ret
= vmx
->segment_cache
.bitmask
& mask
;
1905 vmx
->segment_cache
.bitmask
|= mask
;
1909 static u16
vmx_read_guest_seg_selector(struct vcpu_vmx
*vmx
, unsigned seg
)
1911 u16
*p
= &vmx
->segment_cache
.seg
[seg
].selector
;
1913 if (!vmx_segment_cache_test_set(vmx
, seg
, SEG_FIELD_SEL
))
1914 *p
= vmcs_read16(kvm_vmx_segment_fields
[seg
].selector
);
1918 static ulong
vmx_read_guest_seg_base(struct vcpu_vmx
*vmx
, unsigned seg
)
1920 ulong
*p
= &vmx
->segment_cache
.seg
[seg
].base
;
1922 if (!vmx_segment_cache_test_set(vmx
, seg
, SEG_FIELD_BASE
))
1923 *p
= vmcs_readl(kvm_vmx_segment_fields
[seg
].base
);
1927 static u32
vmx_read_guest_seg_limit(struct vcpu_vmx
*vmx
, unsigned seg
)
1929 u32
*p
= &vmx
->segment_cache
.seg
[seg
].limit
;
1931 if (!vmx_segment_cache_test_set(vmx
, seg
, SEG_FIELD_LIMIT
))
1932 *p
= vmcs_read32(kvm_vmx_segment_fields
[seg
].limit
);
1936 static u32
vmx_read_guest_seg_ar(struct vcpu_vmx
*vmx
, unsigned seg
)
1938 u32
*p
= &vmx
->segment_cache
.seg
[seg
].ar
;
1940 if (!vmx_segment_cache_test_set(vmx
, seg
, SEG_FIELD_AR
))
1941 *p
= vmcs_read32(kvm_vmx_segment_fields
[seg
].ar_bytes
);
1945 static void update_exception_bitmap(struct kvm_vcpu
*vcpu
)
1949 eb
= (1u << PF_VECTOR
) | (1u << UD_VECTOR
) | (1u << MC_VECTOR
) |
1950 (1u << DB_VECTOR
) | (1u << AC_VECTOR
);
1951 if ((vcpu
->guest_debug
&
1952 (KVM_GUESTDBG_ENABLE
| KVM_GUESTDBG_USE_SW_BP
)) ==
1953 (KVM_GUESTDBG_ENABLE
| KVM_GUESTDBG_USE_SW_BP
))
1954 eb
|= 1u << BP_VECTOR
;
1955 if (to_vmx(vcpu
)->rmode
.vm86_active
)
1958 eb
&= ~(1u << PF_VECTOR
); /* bypass_guest_pf = 0 */
1960 /* When we are running a nested L2 guest and L1 specified for it a
1961 * certain exception bitmap, we must trap the same exceptions and pass
1962 * them to L1. When running L2, we will only handle the exceptions
1963 * specified above if L1 did not want them.
1965 if (is_guest_mode(vcpu
))
1966 eb
|= get_vmcs12(vcpu
)->exception_bitmap
;
1968 vmcs_write32(EXCEPTION_BITMAP
, eb
);
1972 * Check if MSR is intercepted for currently loaded MSR bitmap.
1974 static bool msr_write_intercepted(struct kvm_vcpu
*vcpu
, u32 msr
)
1976 unsigned long *msr_bitmap
;
1977 int f
= sizeof(unsigned long);
1979 if (!cpu_has_vmx_msr_bitmap())
1982 msr_bitmap
= to_vmx(vcpu
)->loaded_vmcs
->msr_bitmap
;
1984 if (msr
<= 0x1fff) {
1985 return !!test_bit(msr
, msr_bitmap
+ 0x800 / f
);
1986 } else if ((msr
>= 0xc0000000) && (msr
<= 0xc0001fff)) {
1988 return !!test_bit(msr
, msr_bitmap
+ 0xc00 / f
);
1995 * Check if MSR is intercepted for L01 MSR bitmap.
1997 static bool msr_write_intercepted_l01(struct kvm_vcpu
*vcpu
, u32 msr
)
1999 unsigned long *msr_bitmap
;
2000 int f
= sizeof(unsigned long);
2002 if (!cpu_has_vmx_msr_bitmap())
2005 msr_bitmap
= to_vmx(vcpu
)->vmcs01
.msr_bitmap
;
2007 if (msr
<= 0x1fff) {
2008 return !!test_bit(msr
, msr_bitmap
+ 0x800 / f
);
2009 } else if ((msr
>= 0xc0000000) && (msr
<= 0xc0001fff)) {
2011 return !!test_bit(msr
, msr_bitmap
+ 0xc00 / f
);
2017 static void clear_atomic_switch_msr_special(struct vcpu_vmx
*vmx
,
2018 unsigned long entry
, unsigned long exit
)
2020 vm_entry_controls_clearbit(vmx
, entry
);
2021 vm_exit_controls_clearbit(vmx
, exit
);
2024 static int find_msr(struct vmx_msrs
*m
, unsigned int msr
)
2028 for (i
= 0; i
< m
->nr
; ++i
) {
2029 if (m
->val
[i
].index
== msr
)
2035 static void clear_atomic_switch_msr(struct vcpu_vmx
*vmx
, unsigned msr
)
2038 struct msr_autoload
*m
= &vmx
->msr_autoload
;
2042 if (cpu_has_load_ia32_efer
) {
2043 clear_atomic_switch_msr_special(vmx
,
2044 VM_ENTRY_LOAD_IA32_EFER
,
2045 VM_EXIT_LOAD_IA32_EFER
);
2049 case MSR_CORE_PERF_GLOBAL_CTRL
:
2050 if (cpu_has_load_perf_global_ctrl
) {
2051 clear_atomic_switch_msr_special(vmx
,
2052 VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL
,
2053 VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL
);
2058 i
= find_msr(&m
->guest
, msr
);
2062 m
->guest
.val
[i
] = m
->guest
.val
[m
->guest
.nr
];
2063 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT
, m
->guest
.nr
);
2066 i
= find_msr(&m
->host
, msr
);
2071 m
->host
.val
[i
] = m
->host
.val
[m
->host
.nr
];
2072 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT
, m
->host
.nr
);
2075 static void add_atomic_switch_msr_special(struct vcpu_vmx
*vmx
,
2076 unsigned long entry
, unsigned long exit
,
2077 unsigned long guest_val_vmcs
, unsigned long host_val_vmcs
,
2078 u64 guest_val
, u64 host_val
)
2080 vmcs_write64(guest_val_vmcs
, guest_val
);
2081 vmcs_write64(host_val_vmcs
, host_val
);
2082 vm_entry_controls_setbit(vmx
, entry
);
2083 vm_exit_controls_setbit(vmx
, exit
);
2086 static void add_atomic_switch_msr(struct vcpu_vmx
*vmx
, unsigned msr
,
2087 u64 guest_val
, u64 host_val
, bool entry_only
)
2090 struct msr_autoload
*m
= &vmx
->msr_autoload
;
2094 if (cpu_has_load_ia32_efer
) {
2095 add_atomic_switch_msr_special(vmx
,
2096 VM_ENTRY_LOAD_IA32_EFER
,
2097 VM_EXIT_LOAD_IA32_EFER
,
2100 guest_val
, host_val
);
2104 case MSR_CORE_PERF_GLOBAL_CTRL
:
2105 if (cpu_has_load_perf_global_ctrl
) {
2106 add_atomic_switch_msr_special(vmx
,
2107 VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL
,
2108 VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL
,
2109 GUEST_IA32_PERF_GLOBAL_CTRL
,
2110 HOST_IA32_PERF_GLOBAL_CTRL
,
2111 guest_val
, host_val
);
2115 case MSR_IA32_PEBS_ENABLE
:
2116 /* PEBS needs a quiescent period after being disabled (to write
2117 * a record). Disabling PEBS through VMX MSR swapping doesn't
2118 * provide that period, so a CPU could write host's record into
2121 wrmsrl(MSR_IA32_PEBS_ENABLE
, 0);
2124 i
= find_msr(&m
->guest
, msr
);
2126 j
= find_msr(&m
->host
, msr
);
2128 if (i
== NR_AUTOLOAD_MSRS
|| j
== NR_AUTOLOAD_MSRS
) {
2129 printk_once(KERN_WARNING
"Not enough msr switch entries. "
2130 "Can't add msr %x\n", msr
);
2135 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT
, m
->guest
.nr
);
2137 m
->guest
.val
[i
].index
= msr
;
2138 m
->guest
.val
[i
].value
= guest_val
;
2145 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT
, m
->host
.nr
);
2147 m
->host
.val
[j
].index
= msr
;
2148 m
->host
.val
[j
].value
= host_val
;
2151 static bool update_transition_efer(struct vcpu_vmx
*vmx
, int efer_offset
)
2153 u64 guest_efer
= vmx
->vcpu
.arch
.efer
;
2154 u64 ignore_bits
= 0;
2158 * NX is needed to handle CR0.WP=1, CR4.SMEP=1. Testing
2159 * host CPUID is more efficient than testing guest CPUID
2160 * or CR4. Host SMEP is anyway a requirement for guest SMEP.
2162 if (boot_cpu_has(X86_FEATURE_SMEP
))
2163 guest_efer
|= EFER_NX
;
2164 else if (!(guest_efer
& EFER_NX
))
2165 ignore_bits
|= EFER_NX
;
2169 * LMA and LME handled by hardware; SCE meaningless outside long mode.
2171 ignore_bits
|= EFER_SCE
;
2172 #ifdef CONFIG_X86_64
2173 ignore_bits
|= EFER_LMA
| EFER_LME
;
2174 /* SCE is meaningful only in long mode on Intel */
2175 if (guest_efer
& EFER_LMA
)
2176 ignore_bits
&= ~(u64
)EFER_SCE
;
2179 clear_atomic_switch_msr(vmx
, MSR_EFER
);
2182 * On EPT, we can't emulate NX, so we must switch EFER atomically.
2183 * On CPUs that support "load IA32_EFER", always switch EFER
2184 * atomically, since it's faster than switching it manually.
2186 if (cpu_has_load_ia32_efer
||
2187 (enable_ept
&& ((vmx
->vcpu
.arch
.efer
^ host_efer
) & EFER_NX
))) {
2188 if (!(guest_efer
& EFER_LMA
))
2189 guest_efer
&= ~EFER_LME
;
2190 if (guest_efer
!= host_efer
)
2191 add_atomic_switch_msr(vmx
, MSR_EFER
,
2192 guest_efer
, host_efer
, false);
2195 guest_efer
&= ~ignore_bits
;
2196 guest_efer
|= host_efer
& ignore_bits
;
2198 vmx
->guest_msrs
[efer_offset
].data
= guest_efer
;
2199 vmx
->guest_msrs
[efer_offset
].mask
= ~ignore_bits
;
2205 #ifdef CONFIG_X86_32
2207 * On 32-bit kernels, VM exits still load the FS and GS bases from the
2208 * VMCS rather than the segment table. KVM uses this helper to figure
2209 * out the current bases to poke them into the VMCS before entry.
2211 static unsigned long segment_base(u16 selector
)
2213 struct desc_struct
*table
;
2216 if (!(selector
& ~SEGMENT_RPL_MASK
))
2219 table
= get_current_gdt_ro();
2221 if ((selector
& SEGMENT_TI_MASK
) == SEGMENT_LDT
) {
2222 u16 ldt_selector
= kvm_read_ldt();
2224 if (!(ldt_selector
& ~SEGMENT_RPL_MASK
))
2227 table
= (struct desc_struct
*)segment_base(ldt_selector
);
2229 v
= get_desc_base(&table
[selector
>> 3]);
2234 static void vmx_save_host_state(struct kvm_vcpu
*vcpu
)
2236 struct vcpu_vmx
*vmx
= to_vmx(vcpu
);
2239 if (vmx
->host_state
.loaded
)
2242 vmx
->host_state
.loaded
= 1;
2244 * Set host fs and gs selectors. Unfortunately, 22.2.3 does not
2245 * allow segment selectors with cpl > 0 or ti == 1.
2247 vmx
->host_state
.ldt_sel
= kvm_read_ldt();
2248 vmx
->host_state
.gs_ldt_reload_needed
= vmx
->host_state
.ldt_sel
;
2249 savesegment(fs
, vmx
->host_state
.fs_sel
);
2250 if (!(vmx
->host_state
.fs_sel
& 7)) {
2251 vmcs_write16(HOST_FS_SELECTOR
, vmx
->host_state
.fs_sel
);
2252 vmx
->host_state
.fs_reload_needed
= 0;
2254 vmcs_write16(HOST_FS_SELECTOR
, 0);
2255 vmx
->host_state
.fs_reload_needed
= 1;
2257 savesegment(gs
, vmx
->host_state
.gs_sel
);
2258 if (!(vmx
->host_state
.gs_sel
& 7))
2259 vmcs_write16(HOST_GS_SELECTOR
, vmx
->host_state
.gs_sel
);
2261 vmcs_write16(HOST_GS_SELECTOR
, 0);
2262 vmx
->host_state
.gs_ldt_reload_needed
= 1;
2265 #ifdef CONFIG_X86_64
2266 savesegment(ds
, vmx
->host_state
.ds_sel
);
2267 savesegment(es
, vmx
->host_state
.es_sel
);
2270 #ifdef CONFIG_X86_64
2271 vmcs_writel(HOST_FS_BASE
, read_msr(MSR_FS_BASE
));
2272 vmcs_writel(HOST_GS_BASE
, read_msr(MSR_GS_BASE
));
2274 vmcs_writel(HOST_FS_BASE
, segment_base(vmx
->host_state
.fs_sel
));
2275 vmcs_writel(HOST_GS_BASE
, segment_base(vmx
->host_state
.gs_sel
));
2278 #ifdef CONFIG_X86_64
2279 rdmsrl(MSR_KERNEL_GS_BASE
, vmx
->msr_host_kernel_gs_base
);
2280 if (is_long_mode(&vmx
->vcpu
))
2281 wrmsrl(MSR_KERNEL_GS_BASE
, vmx
->msr_guest_kernel_gs_base
);
2283 if (boot_cpu_has(X86_FEATURE_MPX
))
2284 rdmsrl(MSR_IA32_BNDCFGS
, vmx
->host_state
.msr_host_bndcfgs
);
2285 for (i
= 0; i
< vmx
->save_nmsrs
; ++i
)
2286 kvm_set_shared_msr(vmx
->guest_msrs
[i
].index
,
2287 vmx
->guest_msrs
[i
].data
,
2288 vmx
->guest_msrs
[i
].mask
);
2291 static void __vmx_load_host_state(struct vcpu_vmx
*vmx
)
2293 if (!vmx
->host_state
.loaded
)
2296 ++vmx
->vcpu
.stat
.host_state_reload
;
2297 vmx
->host_state
.loaded
= 0;
2298 #ifdef CONFIG_X86_64
2299 if (is_long_mode(&vmx
->vcpu
))
2300 rdmsrl(MSR_KERNEL_GS_BASE
, vmx
->msr_guest_kernel_gs_base
);
2302 if (vmx
->host_state
.gs_ldt_reload_needed
) {
2303 kvm_load_ldt(vmx
->host_state
.ldt_sel
);
2304 #ifdef CONFIG_X86_64
2305 load_gs_index(vmx
->host_state
.gs_sel
);
2307 loadsegment(gs
, vmx
->host_state
.gs_sel
);
2310 if (vmx
->host_state
.fs_reload_needed
)
2311 loadsegment(fs
, vmx
->host_state
.fs_sel
);
2312 #ifdef CONFIG_X86_64
2313 if (unlikely(vmx
->host_state
.ds_sel
| vmx
->host_state
.es_sel
)) {
2314 loadsegment(ds
, vmx
->host_state
.ds_sel
);
2315 loadsegment(es
, vmx
->host_state
.es_sel
);
2318 invalidate_tss_limit();
2319 #ifdef CONFIG_X86_64
2320 wrmsrl(MSR_KERNEL_GS_BASE
, vmx
->msr_host_kernel_gs_base
);
2322 if (vmx
->host_state
.msr_host_bndcfgs
)
2323 wrmsrl(MSR_IA32_BNDCFGS
, vmx
->host_state
.msr_host_bndcfgs
);
2324 load_fixmap_gdt(raw_smp_processor_id());
2327 static void vmx_load_host_state(struct vcpu_vmx
*vmx
)
2330 __vmx_load_host_state(vmx
);
2334 static void vmx_vcpu_pi_load(struct kvm_vcpu
*vcpu
, int cpu
)
2336 struct pi_desc
*pi_desc
= vcpu_to_pi_desc(vcpu
);
2337 struct pi_desc old
, new;
2341 * In case of hot-plug or hot-unplug, we may have to undo
2342 * vmx_vcpu_pi_put even if there is no assigned device. And we
2343 * always keep PI.NDST up to date for simplicity: it makes the
2344 * code easier, and CPU migration is not a fast path.
2346 if (!pi_test_sn(pi_desc
) && vcpu
->cpu
== cpu
)
2350 * First handle the simple case where no cmpxchg is necessary; just
2351 * allow posting non-urgent interrupts.
2353 * If the 'nv' field is POSTED_INTR_WAKEUP_VECTOR, do not change
2354 * PI.NDST: pi_post_block will do it for us and the wakeup_handler
2355 * expects the VCPU to be on the blocked_vcpu_list that matches
2358 if (pi_desc
->nv
== POSTED_INTR_WAKEUP_VECTOR
||
2360 pi_clear_sn(pi_desc
);
2364 /* The full case. */
2366 old
.control
= new.control
= pi_desc
->control
;
2368 dest
= cpu_physical_id(cpu
);
2370 if (x2apic_enabled())
2373 new.ndst
= (dest
<< 8) & 0xFF00;
2376 } while (cmpxchg64(&pi_desc
->control
, old
.control
,
2377 new.control
) != old
.control
);
2380 static void decache_tsc_multiplier(struct vcpu_vmx
*vmx
)
2382 vmx
->current_tsc_ratio
= vmx
->vcpu
.arch
.tsc_scaling_ratio
;
2383 vmcs_write64(TSC_MULTIPLIER
, vmx
->current_tsc_ratio
);
2387 * Switches to specified vcpu, until a matching vcpu_put(), but assumes
2388 * vcpu mutex is already taken.
2390 static void vmx_vcpu_load(struct kvm_vcpu
*vcpu
, int cpu
)
2392 struct vcpu_vmx
*vmx
= to_vmx(vcpu
);
2393 bool already_loaded
= vmx
->loaded_vmcs
->cpu
== cpu
;
2395 if (!already_loaded
) {
2396 loaded_vmcs_clear(vmx
->loaded_vmcs
);
2397 local_irq_disable();
2398 crash_disable_local_vmclear(cpu
);
2401 * Read loaded_vmcs->cpu should be before fetching
2402 * loaded_vmcs->loaded_vmcss_on_cpu_link.
2403 * See the comments in __loaded_vmcs_clear().
2407 list_add(&vmx
->loaded_vmcs
->loaded_vmcss_on_cpu_link
,
2408 &per_cpu(loaded_vmcss_on_cpu
, cpu
));
2409 crash_enable_local_vmclear(cpu
);
2413 if (per_cpu(current_vmcs
, cpu
) != vmx
->loaded_vmcs
->vmcs
) {
2414 per_cpu(current_vmcs
, cpu
) = vmx
->loaded_vmcs
->vmcs
;
2415 vmcs_load(vmx
->loaded_vmcs
->vmcs
);
2416 indirect_branch_prediction_barrier();
2419 if (!already_loaded
) {
2420 void *gdt
= get_current_gdt_ro();
2421 unsigned long sysenter_esp
;
2423 kvm_make_request(KVM_REQ_TLB_FLUSH
, vcpu
);
2426 * Linux uses per-cpu TSS and GDT, so set these when switching
2427 * processors. See 22.2.4.
2429 vmcs_writel(HOST_TR_BASE
,
2430 (unsigned long)&get_cpu_entry_area(cpu
)->tss
.x86_tss
);
2431 vmcs_writel(HOST_GDTR_BASE
, (unsigned long)gdt
); /* 22.2.4 */
2434 * VM exits change the host TR limit to 0x67 after a VM
2435 * exit. This is okay, since 0x67 covers everything except
2436 * the IO bitmap and have have code to handle the IO bitmap
2437 * being lost after a VM exit.
2439 BUILD_BUG_ON(IO_BITMAP_OFFSET
- 1 != 0x67);
2441 rdmsrl(MSR_IA32_SYSENTER_ESP
, sysenter_esp
);
2442 vmcs_writel(HOST_IA32_SYSENTER_ESP
, sysenter_esp
); /* 22.2.3 */
2444 vmx
->loaded_vmcs
->cpu
= cpu
;
2447 /* Setup TSC multiplier */
2448 if (kvm_has_tsc_control
&&
2449 vmx
->current_tsc_ratio
!= vcpu
->arch
.tsc_scaling_ratio
)
2450 decache_tsc_multiplier(vmx
);
2452 vmx_vcpu_pi_load(vcpu
, cpu
);
2453 vmx
->host_pkru
= read_pkru();
2456 static void vmx_vcpu_pi_put(struct kvm_vcpu
*vcpu
)
2458 struct pi_desc
*pi_desc
= vcpu_to_pi_desc(vcpu
);
2460 if (!kvm_arch_has_assigned_device(vcpu
->kvm
) ||
2461 !irq_remapping_cap(IRQ_POSTING_CAP
) ||
2462 !kvm_vcpu_apicv_active(vcpu
))
2465 /* Set SN when the vCPU is preempted */
2466 if (vcpu
->preempted
)
2470 static void vmx_vcpu_put(struct kvm_vcpu
*vcpu
)
2472 vmx_vcpu_pi_put(vcpu
);
2474 __vmx_load_host_state(to_vmx(vcpu
));
2477 static bool emulation_required(struct kvm_vcpu
*vcpu
)
2479 return emulate_invalid_guest_state
&& !guest_state_valid(vcpu
);
2482 static void vmx_decache_cr0_guest_bits(struct kvm_vcpu
*vcpu
);
2485 * Return the cr0 value that a nested guest would read. This is a combination
2486 * of the real cr0 used to run the guest (guest_cr0), and the bits shadowed by
2487 * its hypervisor (cr0_read_shadow).
2489 static inline unsigned long nested_read_cr0(struct vmcs12
*fields
)
2491 return (fields
->guest_cr0
& ~fields
->cr0_guest_host_mask
) |
2492 (fields
->cr0_read_shadow
& fields
->cr0_guest_host_mask
);
2494 static inline unsigned long nested_read_cr4(struct vmcs12
*fields
)
2496 return (fields
->guest_cr4
& ~fields
->cr4_guest_host_mask
) |
2497 (fields
->cr4_read_shadow
& fields
->cr4_guest_host_mask
);
2500 static unsigned long vmx_get_rflags(struct kvm_vcpu
*vcpu
)
2502 unsigned long rflags
, save_rflags
;
2504 if (!test_bit(VCPU_EXREG_RFLAGS
, (ulong
*)&vcpu
->arch
.regs_avail
)) {
2505 __set_bit(VCPU_EXREG_RFLAGS
, (ulong
*)&vcpu
->arch
.regs_avail
);
2506 rflags
= vmcs_readl(GUEST_RFLAGS
);
2507 if (to_vmx(vcpu
)->rmode
.vm86_active
) {
2508 rflags
&= RMODE_GUEST_OWNED_EFLAGS_BITS
;
2509 save_rflags
= to_vmx(vcpu
)->rmode
.save_rflags
;
2510 rflags
|= save_rflags
& ~RMODE_GUEST_OWNED_EFLAGS_BITS
;
2512 to_vmx(vcpu
)->rflags
= rflags
;
2514 return to_vmx(vcpu
)->rflags
;
2517 static void vmx_set_rflags(struct kvm_vcpu
*vcpu
, unsigned long rflags
)
2519 unsigned long old_rflags
= vmx_get_rflags(vcpu
);
2521 __set_bit(VCPU_EXREG_RFLAGS
, (ulong
*)&vcpu
->arch
.regs_avail
);
2522 to_vmx(vcpu
)->rflags
= rflags
;
2523 if (to_vmx(vcpu
)->rmode
.vm86_active
) {
2524 to_vmx(vcpu
)->rmode
.save_rflags
= rflags
;
2525 rflags
|= X86_EFLAGS_IOPL
| X86_EFLAGS_VM
;
2527 vmcs_writel(GUEST_RFLAGS
, rflags
);
2529 if ((old_rflags
^ to_vmx(vcpu
)->rflags
) & X86_EFLAGS_VM
)
2530 to_vmx(vcpu
)->emulation_required
= emulation_required(vcpu
);
2533 static u32
vmx_get_interrupt_shadow(struct kvm_vcpu
*vcpu
)
2535 u32 interruptibility
= vmcs_read32(GUEST_INTERRUPTIBILITY_INFO
);
2538 if (interruptibility
& GUEST_INTR_STATE_STI
)
2539 ret
|= KVM_X86_SHADOW_INT_STI
;
2540 if (interruptibility
& GUEST_INTR_STATE_MOV_SS
)
2541 ret
|= KVM_X86_SHADOW_INT_MOV_SS
;
2546 static void vmx_set_interrupt_shadow(struct kvm_vcpu
*vcpu
, int mask
)
2548 u32 interruptibility_old
= vmcs_read32(GUEST_INTERRUPTIBILITY_INFO
);
2549 u32 interruptibility
= interruptibility_old
;
2551 interruptibility
&= ~(GUEST_INTR_STATE_STI
| GUEST_INTR_STATE_MOV_SS
);
2553 if (mask
& KVM_X86_SHADOW_INT_MOV_SS
)
2554 interruptibility
|= GUEST_INTR_STATE_MOV_SS
;
2555 else if (mask
& KVM_X86_SHADOW_INT_STI
)
2556 interruptibility
|= GUEST_INTR_STATE_STI
;
2558 if ((interruptibility
!= interruptibility_old
))
2559 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO
, interruptibility
);
2562 static void skip_emulated_instruction(struct kvm_vcpu
*vcpu
)
2566 rip
= kvm_rip_read(vcpu
);
2567 rip
+= vmcs_read32(VM_EXIT_INSTRUCTION_LEN
);
2568 kvm_rip_write(vcpu
, rip
);
2570 /* skipping an emulated instruction also counts */
2571 vmx_set_interrupt_shadow(vcpu
, 0);
2574 static void nested_vmx_inject_exception_vmexit(struct kvm_vcpu
*vcpu
,
2575 unsigned long exit_qual
)
2577 struct vmcs12
*vmcs12
= get_vmcs12(vcpu
);
2578 unsigned int nr
= vcpu
->arch
.exception
.nr
;
2579 u32 intr_info
= nr
| INTR_INFO_VALID_MASK
;
2581 if (vcpu
->arch
.exception
.has_error_code
) {
2582 vmcs12
->vm_exit_intr_error_code
= vcpu
->arch
.exception
.error_code
;
2583 intr_info
|= INTR_INFO_DELIVER_CODE_MASK
;
2586 if (kvm_exception_is_soft(nr
))
2587 intr_info
|= INTR_TYPE_SOFT_EXCEPTION
;
2589 intr_info
|= INTR_TYPE_HARD_EXCEPTION
;
2591 if (!(vmcs12
->idt_vectoring_info_field
& VECTORING_INFO_VALID_MASK
) &&
2592 vmx_get_nmi_mask(vcpu
))
2593 intr_info
|= INTR_INFO_UNBLOCK_NMI
;
2595 nested_vmx_vmexit(vcpu
, EXIT_REASON_EXCEPTION_NMI
, intr_info
, exit_qual
);
2599 * KVM wants to inject page-faults which it got to the guest. This function
2600 * checks whether in a nested guest, we need to inject them to L1 or L2.
2602 static int nested_vmx_check_exception(struct kvm_vcpu
*vcpu
, unsigned long *exit_qual
)
2604 struct vmcs12
*vmcs12
= get_vmcs12(vcpu
);
2605 unsigned int nr
= vcpu
->arch
.exception
.nr
;
2607 if (nr
== PF_VECTOR
) {
2608 if (vcpu
->arch
.exception
.nested_apf
) {
2609 *exit_qual
= vcpu
->arch
.apf
.nested_apf_token
;
2613 * FIXME: we must not write CR2 when L1 intercepts an L2 #PF exception.
2614 * The fix is to add the ancillary datum (CR2 or DR6) to structs
2615 * kvm_queued_exception and kvm_vcpu_events, so that CR2 and DR6
2616 * can be written only when inject_pending_event runs. This should be
2617 * conditional on a new capability---if the capability is disabled,
2618 * kvm_multiple_exception would write the ancillary information to
2619 * CR2 or DR6, for backwards ABI-compatibility.
2621 if (nested_vmx_is_page_fault_vmexit(vmcs12
,
2622 vcpu
->arch
.exception
.error_code
)) {
2623 *exit_qual
= vcpu
->arch
.cr2
;
2627 if (vmcs12
->exception_bitmap
& (1u << nr
)) {
2628 if (nr
== DB_VECTOR
)
2629 *exit_qual
= vcpu
->arch
.dr6
;
2639 static void vmx_queue_exception(struct kvm_vcpu
*vcpu
)
2641 struct vcpu_vmx
*vmx
= to_vmx(vcpu
);
2642 unsigned nr
= vcpu
->arch
.exception
.nr
;
2643 bool has_error_code
= vcpu
->arch
.exception
.has_error_code
;
2644 u32 error_code
= vcpu
->arch
.exception
.error_code
;
2645 u32 intr_info
= nr
| INTR_INFO_VALID_MASK
;
2647 if (has_error_code
) {
2648 vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE
, error_code
);
2649 intr_info
|= INTR_INFO_DELIVER_CODE_MASK
;
2652 if (vmx
->rmode
.vm86_active
) {
2654 if (kvm_exception_is_soft(nr
))
2655 inc_eip
= vcpu
->arch
.event_exit_inst_len
;
2656 if (kvm_inject_realmode_interrupt(vcpu
, nr
, inc_eip
) != EMULATE_DONE
)
2657 kvm_make_request(KVM_REQ_TRIPLE_FAULT
, vcpu
);
2661 if (kvm_exception_is_soft(nr
)) {
2662 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN
,
2663 vmx
->vcpu
.arch
.event_exit_inst_len
);
2664 intr_info
|= INTR_TYPE_SOFT_EXCEPTION
;
2666 intr_info
|= INTR_TYPE_HARD_EXCEPTION
;
2668 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD
, intr_info
);
2671 static bool vmx_rdtscp_supported(void)
2673 return cpu_has_vmx_rdtscp();
2676 static bool vmx_invpcid_supported(void)
2678 return cpu_has_vmx_invpcid() && enable_ept
;
2682 * Swap MSR entry in host/guest MSR entry array.
2684 static void move_msr_up(struct vcpu_vmx
*vmx
, int from
, int to
)
2686 struct shared_msr_entry tmp
;
2688 tmp
= vmx
->guest_msrs
[to
];
2689 vmx
->guest_msrs
[to
] = vmx
->guest_msrs
[from
];
2690 vmx
->guest_msrs
[from
] = tmp
;
2694 * Set up the vmcs to automatically save and restore system
2695 * msrs. Don't touch the 64-bit msrs if the guest is in legacy
2696 * mode, as fiddling with msrs is very expensive.
2698 static void setup_msrs(struct vcpu_vmx
*vmx
)
2700 int save_nmsrs
, index
;
2703 #ifdef CONFIG_X86_64
2704 if (is_long_mode(&vmx
->vcpu
)) {
2705 index
= __find_msr_index(vmx
, MSR_SYSCALL_MASK
);
2707 move_msr_up(vmx
, index
, save_nmsrs
++);
2708 index
= __find_msr_index(vmx
, MSR_LSTAR
);
2710 move_msr_up(vmx
, index
, save_nmsrs
++);
2711 index
= __find_msr_index(vmx
, MSR_CSTAR
);
2713 move_msr_up(vmx
, index
, save_nmsrs
++);
2714 index
= __find_msr_index(vmx
, MSR_TSC_AUX
);
2715 if (index
>= 0 && guest_cpuid_has(&vmx
->vcpu
, X86_FEATURE_RDTSCP
))
2716 move_msr_up(vmx
, index
, save_nmsrs
++);
2718 * MSR_STAR is only needed on long mode guests, and only
2719 * if efer.sce is enabled.
2721 index
= __find_msr_index(vmx
, MSR_STAR
);
2722 if ((index
>= 0) && (vmx
->vcpu
.arch
.efer
& EFER_SCE
))
2723 move_msr_up(vmx
, index
, save_nmsrs
++);
2726 index
= __find_msr_index(vmx
, MSR_EFER
);
2727 if (index
>= 0 && update_transition_efer(vmx
, index
))
2728 move_msr_up(vmx
, index
, save_nmsrs
++);
2730 vmx
->save_nmsrs
= save_nmsrs
;
2732 if (cpu_has_vmx_msr_bitmap())
2733 vmx_update_msr_bitmap(&vmx
->vcpu
);
2737 * reads and returns guest's timestamp counter "register"
2738 * guest_tsc = (host_tsc * tsc multiplier) >> 48 + tsc_offset
2739 * -- Intel TSC Scaling for Virtualization White Paper, sec 1.3
2741 static u64
guest_read_tsc(struct kvm_vcpu
*vcpu
)
2743 u64 host_tsc
, tsc_offset
;
2746 tsc_offset
= vmcs_read64(TSC_OFFSET
);
2747 return kvm_scale_tsc(vcpu
, host_tsc
) + tsc_offset
;
2751 * writes 'offset' into guest's timestamp counter offset register
2753 static void vmx_write_tsc_offset(struct kvm_vcpu
*vcpu
, u64 offset
)
2755 if (is_guest_mode(vcpu
)) {
2757 * We're here if L1 chose not to trap WRMSR to TSC. According
2758 * to the spec, this should set L1's TSC; The offset that L1
2759 * set for L2 remains unchanged, and still needs to be added
2760 * to the newly set TSC to get L2's TSC.
2762 struct vmcs12
*vmcs12
;
2763 /* recalculate vmcs02.TSC_OFFSET: */
2764 vmcs12
= get_vmcs12(vcpu
);
2765 vmcs_write64(TSC_OFFSET
, offset
+
2766 (nested_cpu_has(vmcs12
, CPU_BASED_USE_TSC_OFFSETING
) ?
2767 vmcs12
->tsc_offset
: 0));
2769 trace_kvm_write_tsc_offset(vcpu
->vcpu_id
,
2770 vmcs_read64(TSC_OFFSET
), offset
);
2771 vmcs_write64(TSC_OFFSET
, offset
);
2776 * nested_vmx_allowed() checks whether a guest should be allowed to use VMX
2777 * instructions and MSRs (i.e., nested VMX). Nested VMX is disabled for
2778 * all guests if the "nested" module option is off, and can also be disabled
2779 * for a single guest by disabling its VMX cpuid bit.
2781 static inline bool nested_vmx_allowed(struct kvm_vcpu
*vcpu
)
2783 return nested
&& guest_cpuid_has(vcpu
, X86_FEATURE_VMX
);
2787 * nested_vmx_setup_ctls_msrs() sets up variables containing the values to be
2788 * returned for the various VMX controls MSRs when nested VMX is enabled.
2789 * The same values should also be used to verify that vmcs12 control fields are
2790 * valid during nested entry from L1 to L2.
2791 * Each of these control msrs has a low and high 32-bit half: A low bit is on
2792 * if the corresponding bit in the (32-bit) control field *must* be on, and a
2793 * bit in the high half is on if the corresponding bit in the control field
2794 * may be on. See also vmx_control_verify().
2796 static void nested_vmx_setup_ctls_msrs(struct vcpu_vmx
*vmx
)
2799 * Note that as a general rule, the high half of the MSRs (bits in
2800 * the control fields which may be 1) should be initialized by the
2801 * intersection of the underlying hardware's MSR (i.e., features which
2802 * can be supported) and the list of features we want to expose -
2803 * because they are known to be properly supported in our code.
2804 * Also, usually, the low half of the MSRs (bits which must be 1) can
2805 * be set to 0, meaning that L1 may turn off any of these bits. The
2806 * reason is that if one of these bits is necessary, it will appear
2807 * in vmcs01 and prepare_vmcs02, when it bitwise-or's the control
2808 * fields of vmcs01 and vmcs02, will turn these bits off - and
2809 * nested_vmx_exit_reflected() will not pass related exits to L1.
2810 * These rules have exceptions below.
2813 /* pin-based controls */
2814 rdmsr(MSR_IA32_VMX_PINBASED_CTLS
,
2815 vmx
->nested
.nested_vmx_pinbased_ctls_low
,
2816 vmx
->nested
.nested_vmx_pinbased_ctls_high
);
2817 vmx
->nested
.nested_vmx_pinbased_ctls_low
|=
2818 PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR
;
2819 vmx
->nested
.nested_vmx_pinbased_ctls_high
&=
2820 PIN_BASED_EXT_INTR_MASK
|
2821 PIN_BASED_NMI_EXITING
|
2822 PIN_BASED_VIRTUAL_NMIS
;
2823 vmx
->nested
.nested_vmx_pinbased_ctls_high
|=
2824 PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR
|
2825 PIN_BASED_VMX_PREEMPTION_TIMER
;
2826 if (kvm_vcpu_apicv_active(&vmx
->vcpu
))
2827 vmx
->nested
.nested_vmx_pinbased_ctls_high
|=
2828 PIN_BASED_POSTED_INTR
;
2831 rdmsr(MSR_IA32_VMX_EXIT_CTLS
,
2832 vmx
->nested
.nested_vmx_exit_ctls_low
,
2833 vmx
->nested
.nested_vmx_exit_ctls_high
);
2834 vmx
->nested
.nested_vmx_exit_ctls_low
=
2835 VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR
;
2837 vmx
->nested
.nested_vmx_exit_ctls_high
&=
2838 #ifdef CONFIG_X86_64
2839 VM_EXIT_HOST_ADDR_SPACE_SIZE
|
2841 VM_EXIT_LOAD_IA32_PAT
| VM_EXIT_SAVE_IA32_PAT
;
2842 vmx
->nested
.nested_vmx_exit_ctls_high
|=
2843 VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR
|
2844 VM_EXIT_LOAD_IA32_EFER
| VM_EXIT_SAVE_IA32_EFER
|
2845 VM_EXIT_SAVE_VMX_PREEMPTION_TIMER
| VM_EXIT_ACK_INTR_ON_EXIT
;
2847 if (kvm_mpx_supported())
2848 vmx
->nested
.nested_vmx_exit_ctls_high
|= VM_EXIT_CLEAR_BNDCFGS
;
2850 /* We support free control of debug control saving. */
2851 vmx
->nested
.nested_vmx_exit_ctls_low
&= ~VM_EXIT_SAVE_DEBUG_CONTROLS
;
2853 /* entry controls */
2854 rdmsr(MSR_IA32_VMX_ENTRY_CTLS
,
2855 vmx
->nested
.nested_vmx_entry_ctls_low
,
2856 vmx
->nested
.nested_vmx_entry_ctls_high
);
2857 vmx
->nested
.nested_vmx_entry_ctls_low
=
2858 VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR
;
2859 vmx
->nested
.nested_vmx_entry_ctls_high
&=
2860 #ifdef CONFIG_X86_64
2861 VM_ENTRY_IA32E_MODE
|
2863 VM_ENTRY_LOAD_IA32_PAT
;
2864 vmx
->nested
.nested_vmx_entry_ctls_high
|=
2865 (VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR
| VM_ENTRY_LOAD_IA32_EFER
);
2866 if (kvm_mpx_supported())
2867 vmx
->nested
.nested_vmx_entry_ctls_high
|= VM_ENTRY_LOAD_BNDCFGS
;
2869 /* We support free control of debug control loading. */
2870 vmx
->nested
.nested_vmx_entry_ctls_low
&= ~VM_ENTRY_LOAD_DEBUG_CONTROLS
;
2872 /* cpu-based controls */
2873 rdmsr(MSR_IA32_VMX_PROCBASED_CTLS
,
2874 vmx
->nested
.nested_vmx_procbased_ctls_low
,
2875 vmx
->nested
.nested_vmx_procbased_ctls_high
);
2876 vmx
->nested
.nested_vmx_procbased_ctls_low
=
2877 CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR
;
2878 vmx
->nested
.nested_vmx_procbased_ctls_high
&=
2879 CPU_BASED_VIRTUAL_INTR_PENDING
|
2880 CPU_BASED_VIRTUAL_NMI_PENDING
| CPU_BASED_USE_TSC_OFFSETING
|
2881 CPU_BASED_HLT_EXITING
| CPU_BASED_INVLPG_EXITING
|
2882 CPU_BASED_MWAIT_EXITING
| CPU_BASED_CR3_LOAD_EXITING
|
2883 CPU_BASED_CR3_STORE_EXITING
|
2884 #ifdef CONFIG_X86_64
2885 CPU_BASED_CR8_LOAD_EXITING
| CPU_BASED_CR8_STORE_EXITING
|
2887 CPU_BASED_MOV_DR_EXITING
| CPU_BASED_UNCOND_IO_EXITING
|
2888 CPU_BASED_USE_IO_BITMAPS
| CPU_BASED_MONITOR_TRAP_FLAG
|
2889 CPU_BASED_MONITOR_EXITING
| CPU_BASED_RDPMC_EXITING
|
2890 CPU_BASED_RDTSC_EXITING
| CPU_BASED_PAUSE_EXITING
|
2891 CPU_BASED_TPR_SHADOW
| CPU_BASED_ACTIVATE_SECONDARY_CONTROLS
;
2893 * We can allow some features even when not supported by the
2894 * hardware. For example, L1 can specify an MSR bitmap - and we
2895 * can use it to avoid exits to L1 - even when L0 runs L2
2896 * without MSR bitmaps.
2898 vmx
->nested
.nested_vmx_procbased_ctls_high
|=
2899 CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR
|
2900 CPU_BASED_USE_MSR_BITMAPS
;
2902 /* We support free control of CR3 access interception. */
2903 vmx
->nested
.nested_vmx_procbased_ctls_low
&=
2904 ~(CPU_BASED_CR3_LOAD_EXITING
| CPU_BASED_CR3_STORE_EXITING
);
2907 * secondary cpu-based controls. Do not include those that
2908 * depend on CPUID bits, they are added later by vmx_cpuid_update.
2910 rdmsr(MSR_IA32_VMX_PROCBASED_CTLS2
,
2911 vmx
->nested
.nested_vmx_secondary_ctls_low
,
2912 vmx
->nested
.nested_vmx_secondary_ctls_high
);
2913 vmx
->nested
.nested_vmx_secondary_ctls_low
= 0;
2914 vmx
->nested
.nested_vmx_secondary_ctls_high
&=
2915 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES
|
2916 SECONDARY_EXEC_DESC
|
2917 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE
|
2918 SECONDARY_EXEC_APIC_REGISTER_VIRT
|
2919 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY
|
2920 SECONDARY_EXEC_WBINVD_EXITING
;
2923 /* nested EPT: emulate EPT also to L1 */
2924 vmx
->nested
.nested_vmx_secondary_ctls_high
|=
2925 SECONDARY_EXEC_ENABLE_EPT
;
2926 vmx
->nested
.nested_vmx_ept_caps
= VMX_EPT_PAGE_WALK_4_BIT
|
2927 VMX_EPTP_WB_BIT
| VMX_EPT_INVEPT_BIT
;
2928 if (cpu_has_vmx_ept_execute_only())
2929 vmx
->nested
.nested_vmx_ept_caps
|=
2930 VMX_EPT_EXECUTE_ONLY_BIT
;
2931 vmx
->nested
.nested_vmx_ept_caps
&= vmx_capability
.ept
;
2932 vmx
->nested
.nested_vmx_ept_caps
|= VMX_EPT_EXTENT_GLOBAL_BIT
|
2933 VMX_EPT_EXTENT_CONTEXT_BIT
| VMX_EPT_2MB_PAGE_BIT
|
2934 VMX_EPT_1GB_PAGE_BIT
;
2935 if (enable_ept_ad_bits
) {
2936 vmx
->nested
.nested_vmx_secondary_ctls_high
|=
2937 SECONDARY_EXEC_ENABLE_PML
;
2938 vmx
->nested
.nested_vmx_ept_caps
|= VMX_EPT_AD_BIT
;
2942 if (cpu_has_vmx_vmfunc()) {
2943 vmx
->nested
.nested_vmx_secondary_ctls_high
|=
2944 SECONDARY_EXEC_ENABLE_VMFUNC
;
2946 * Advertise EPTP switching unconditionally
2947 * since we emulate it
2950 vmx
->nested
.nested_vmx_vmfunc_controls
=
2951 VMX_VMFUNC_EPTP_SWITCHING
;
2955 * Old versions of KVM use the single-context version without
2956 * checking for support, so declare that it is supported even
2957 * though it is treated as global context. The alternative is
2958 * not failing the single-context invvpid, and it is worse.
2961 vmx
->nested
.nested_vmx_secondary_ctls_high
|=
2962 SECONDARY_EXEC_ENABLE_VPID
;
2963 vmx
->nested
.nested_vmx_vpid_caps
= VMX_VPID_INVVPID_BIT
|
2964 VMX_VPID_EXTENT_SUPPORTED_MASK
;
2967 if (enable_unrestricted_guest
)
2968 vmx
->nested
.nested_vmx_secondary_ctls_high
|=
2969 SECONDARY_EXEC_UNRESTRICTED_GUEST
;
2971 /* miscellaneous data */
2972 rdmsr(MSR_IA32_VMX_MISC
,
2973 vmx
->nested
.nested_vmx_misc_low
,
2974 vmx
->nested
.nested_vmx_misc_high
);
2975 vmx
->nested
.nested_vmx_misc_low
&= VMX_MISC_SAVE_EFER_LMA
;
2976 vmx
->nested
.nested_vmx_misc_low
|=
2977 VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE
|
2978 VMX_MISC_ACTIVITY_HLT
;
2979 vmx
->nested
.nested_vmx_misc_high
= 0;
2982 * This MSR reports some information about VMX support. We
2983 * should return information about the VMX we emulate for the
2984 * guest, and the VMCS structure we give it - not about the
2985 * VMX support of the underlying hardware.
2987 vmx
->nested
.nested_vmx_basic
=
2989 VMX_BASIC_TRUE_CTLS
|
2990 ((u64
)VMCS12_SIZE
<< VMX_BASIC_VMCS_SIZE_SHIFT
) |
2991 (VMX_BASIC_MEM_TYPE_WB
<< VMX_BASIC_MEM_TYPE_SHIFT
);
2993 if (cpu_has_vmx_basic_inout())
2994 vmx
->nested
.nested_vmx_basic
|= VMX_BASIC_INOUT
;
2997 * These MSRs specify bits which the guest must keep fixed on
2998 * while L1 is in VMXON mode (in L1's root mode, or running an L2).
2999 * We picked the standard core2 setting.
3001 #define VMXON_CR0_ALWAYSON (X86_CR0_PE | X86_CR0_PG | X86_CR0_NE)
3002 #define VMXON_CR4_ALWAYSON X86_CR4_VMXE
3003 vmx
->nested
.nested_vmx_cr0_fixed0
= VMXON_CR0_ALWAYSON
;
3004 vmx
->nested
.nested_vmx_cr4_fixed0
= VMXON_CR4_ALWAYSON
;
3006 /* These MSRs specify bits which the guest must keep fixed off. */
3007 rdmsrl(MSR_IA32_VMX_CR0_FIXED1
, vmx
->nested
.nested_vmx_cr0_fixed1
);
3008 rdmsrl(MSR_IA32_VMX_CR4_FIXED1
, vmx
->nested
.nested_vmx_cr4_fixed1
);
3010 /* highest index: VMX_PREEMPTION_TIMER_VALUE */
3011 vmx
->nested
.nested_vmx_vmcs_enum
= 0x2e;
3015 * if fixed0[i] == 1: val[i] must be 1
3016 * if fixed1[i] == 0: val[i] must be 0
3018 static inline bool fixed_bits_valid(u64 val
, u64 fixed0
, u64 fixed1
)
3020 return ((val
& fixed1
) | fixed0
) == val
;
3023 static inline bool vmx_control_verify(u32 control
, u32 low
, u32 high
)
3025 return fixed_bits_valid(control
, low
, high
);
3028 static inline u64
vmx_control_msr(u32 low
, u32 high
)
3030 return low
| ((u64
)high
<< 32);
3033 static bool is_bitwise_subset(u64 superset
, u64 subset
, u64 mask
)
3038 return (superset
| subset
) == superset
;
3041 static int vmx_restore_vmx_basic(struct vcpu_vmx
*vmx
, u64 data
)
3043 const u64 feature_and_reserved
=
3044 /* feature (except bit 48; see below) */
3045 BIT_ULL(49) | BIT_ULL(54) | BIT_ULL(55) |
3047 BIT_ULL(31) | GENMASK_ULL(47, 45) | GENMASK_ULL(63, 56);
3048 u64 vmx_basic
= vmx
->nested
.nested_vmx_basic
;
3050 if (!is_bitwise_subset(vmx_basic
, data
, feature_and_reserved
))
3054 * KVM does not emulate a version of VMX that constrains physical
3055 * addresses of VMX structures (e.g. VMCS) to 32-bits.
3057 if (data
& BIT_ULL(48))
3060 if (vmx_basic_vmcs_revision_id(vmx_basic
) !=
3061 vmx_basic_vmcs_revision_id(data
))
3064 if (vmx_basic_vmcs_size(vmx_basic
) > vmx_basic_vmcs_size(data
))
3067 vmx
->nested
.nested_vmx_basic
= data
;
3072 vmx_restore_control_msr(struct vcpu_vmx
*vmx
, u32 msr_index
, u64 data
)
3077 switch (msr_index
) {
3078 case MSR_IA32_VMX_TRUE_PINBASED_CTLS
:
3079 lowp
= &vmx
->nested
.nested_vmx_pinbased_ctls_low
;
3080 highp
= &vmx
->nested
.nested_vmx_pinbased_ctls_high
;
3082 case MSR_IA32_VMX_TRUE_PROCBASED_CTLS
:
3083 lowp
= &vmx
->nested
.nested_vmx_procbased_ctls_low
;
3084 highp
= &vmx
->nested
.nested_vmx_procbased_ctls_high
;
3086 case MSR_IA32_VMX_TRUE_EXIT_CTLS
:
3087 lowp
= &vmx
->nested
.nested_vmx_exit_ctls_low
;
3088 highp
= &vmx
->nested
.nested_vmx_exit_ctls_high
;
3090 case MSR_IA32_VMX_TRUE_ENTRY_CTLS
:
3091 lowp
= &vmx
->nested
.nested_vmx_entry_ctls_low
;
3092 highp
= &vmx
->nested
.nested_vmx_entry_ctls_high
;
3094 case MSR_IA32_VMX_PROCBASED_CTLS2
:
3095 lowp
= &vmx
->nested
.nested_vmx_secondary_ctls_low
;
3096 highp
= &vmx
->nested
.nested_vmx_secondary_ctls_high
;
3102 supported
= vmx_control_msr(*lowp
, *highp
);
3104 /* Check must-be-1 bits are still 1. */
3105 if (!is_bitwise_subset(data
, supported
, GENMASK_ULL(31, 0)))
3108 /* Check must-be-0 bits are still 0. */
3109 if (!is_bitwise_subset(supported
, data
, GENMASK_ULL(63, 32)))
3113 *highp
= data
>> 32;
3117 static int vmx_restore_vmx_misc(struct vcpu_vmx
*vmx
, u64 data
)
3119 const u64 feature_and_reserved_bits
=
3121 BIT_ULL(5) | GENMASK_ULL(8, 6) | BIT_ULL(14) | BIT_ULL(15) |
3122 BIT_ULL(28) | BIT_ULL(29) | BIT_ULL(30) |
3124 GENMASK_ULL(13, 9) | BIT_ULL(31);
3127 vmx_misc
= vmx_control_msr(vmx
->nested
.nested_vmx_misc_low
,
3128 vmx
->nested
.nested_vmx_misc_high
);
3130 if (!is_bitwise_subset(vmx_misc
, data
, feature_and_reserved_bits
))
3133 if ((vmx
->nested
.nested_vmx_pinbased_ctls_high
&
3134 PIN_BASED_VMX_PREEMPTION_TIMER
) &&
3135 vmx_misc_preemption_timer_rate(data
) !=
3136 vmx_misc_preemption_timer_rate(vmx_misc
))
3139 if (vmx_misc_cr3_count(data
) > vmx_misc_cr3_count(vmx_misc
))
3142 if (vmx_misc_max_msr(data
) > vmx_misc_max_msr(vmx_misc
))
3145 if (vmx_misc_mseg_revid(data
) != vmx_misc_mseg_revid(vmx_misc
))
3148 vmx
->nested
.nested_vmx_misc_low
= data
;
3149 vmx
->nested
.nested_vmx_misc_high
= data
>> 32;
3153 static int vmx_restore_vmx_ept_vpid_cap(struct vcpu_vmx
*vmx
, u64 data
)
3155 u64 vmx_ept_vpid_cap
;
3157 vmx_ept_vpid_cap
= vmx_control_msr(vmx
->nested
.nested_vmx_ept_caps
,
3158 vmx
->nested
.nested_vmx_vpid_caps
);
3160 /* Every bit is either reserved or a feature bit. */
3161 if (!is_bitwise_subset(vmx_ept_vpid_cap
, data
, -1ULL))
3164 vmx
->nested
.nested_vmx_ept_caps
= data
;
3165 vmx
->nested
.nested_vmx_vpid_caps
= data
>> 32;
3169 static int vmx_restore_fixed0_msr(struct vcpu_vmx
*vmx
, u32 msr_index
, u64 data
)
3173 switch (msr_index
) {
3174 case MSR_IA32_VMX_CR0_FIXED0
:
3175 msr
= &vmx
->nested
.nested_vmx_cr0_fixed0
;
3177 case MSR_IA32_VMX_CR4_FIXED0
:
3178 msr
= &vmx
->nested
.nested_vmx_cr4_fixed0
;
3185 * 1 bits (which indicates bits which "must-be-1" during VMX operation)
3186 * must be 1 in the restored value.
3188 if (!is_bitwise_subset(data
, *msr
, -1ULL))
3196 * Called when userspace is restoring VMX MSRs.
3198 * Returns 0 on success, non-0 otherwise.
3200 static int vmx_set_vmx_msr(struct kvm_vcpu
*vcpu
, u32 msr_index
, u64 data
)
3202 struct vcpu_vmx
*vmx
= to_vmx(vcpu
);
3204 switch (msr_index
) {
3205 case MSR_IA32_VMX_BASIC
:
3206 return vmx_restore_vmx_basic(vmx
, data
);
3207 case MSR_IA32_VMX_PINBASED_CTLS
:
3208 case MSR_IA32_VMX_PROCBASED_CTLS
:
3209 case MSR_IA32_VMX_EXIT_CTLS
:
3210 case MSR_IA32_VMX_ENTRY_CTLS
:
3212 * The "non-true" VMX capability MSRs are generated from the
3213 * "true" MSRs, so we do not support restoring them directly.
3215 * If userspace wants to emulate VMX_BASIC[55]=0, userspace
3216 * should restore the "true" MSRs with the must-be-1 bits
3217 * set according to the SDM Vol 3. A.2 "RESERVED CONTROLS AND
3218 * DEFAULT SETTINGS".
3221 case MSR_IA32_VMX_TRUE_PINBASED_CTLS
:
3222 case MSR_IA32_VMX_TRUE_PROCBASED_CTLS
:
3223 case MSR_IA32_VMX_TRUE_EXIT_CTLS
:
3224 case MSR_IA32_VMX_TRUE_ENTRY_CTLS
:
3225 case MSR_IA32_VMX_PROCBASED_CTLS2
:
3226 return vmx_restore_control_msr(vmx
, msr_index
, data
);
3227 case MSR_IA32_VMX_MISC
:
3228 return vmx_restore_vmx_misc(vmx
, data
);
3229 case MSR_IA32_VMX_CR0_FIXED0
:
3230 case MSR_IA32_VMX_CR4_FIXED0
:
3231 return vmx_restore_fixed0_msr(vmx
, msr_index
, data
);
3232 case MSR_IA32_VMX_CR0_FIXED1
:
3233 case MSR_IA32_VMX_CR4_FIXED1
:
3235 * These MSRs are generated based on the vCPU's CPUID, so we
3236 * do not support restoring them directly.
3239 case MSR_IA32_VMX_EPT_VPID_CAP
:
3240 return vmx_restore_vmx_ept_vpid_cap(vmx
, data
);
3241 case MSR_IA32_VMX_VMCS_ENUM
:
3242 vmx
->nested
.nested_vmx_vmcs_enum
= data
;
3246 * The rest of the VMX capability MSRs do not support restore.
3252 /* Returns 0 on success, non-0 otherwise. */
3253 static int vmx_get_vmx_msr(struct kvm_vcpu
*vcpu
, u32 msr_index
, u64
*pdata
)
3255 struct vcpu_vmx
*vmx
= to_vmx(vcpu
);
3257 switch (msr_index
) {
3258 case MSR_IA32_VMX_BASIC
:
3259 *pdata
= vmx
->nested
.nested_vmx_basic
;
3261 case MSR_IA32_VMX_TRUE_PINBASED_CTLS
:
3262 case MSR_IA32_VMX_PINBASED_CTLS
:
3263 *pdata
= vmx_control_msr(
3264 vmx
->nested
.nested_vmx_pinbased_ctls_low
,
3265 vmx
->nested
.nested_vmx_pinbased_ctls_high
);
3266 if (msr_index
== MSR_IA32_VMX_PINBASED_CTLS
)
3267 *pdata
|= PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR
;
3269 case MSR_IA32_VMX_TRUE_PROCBASED_CTLS
:
3270 case MSR_IA32_VMX_PROCBASED_CTLS
:
3271 *pdata
= vmx_control_msr(
3272 vmx
->nested
.nested_vmx_procbased_ctls_low
,
3273 vmx
->nested
.nested_vmx_procbased_ctls_high
);
3274 if (msr_index
== MSR_IA32_VMX_PROCBASED_CTLS
)
3275 *pdata
|= CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR
;
3277 case MSR_IA32_VMX_TRUE_EXIT_CTLS
:
3278 case MSR_IA32_VMX_EXIT_CTLS
:
3279 *pdata
= vmx_control_msr(
3280 vmx
->nested
.nested_vmx_exit_ctls_low
,
3281 vmx
->nested
.nested_vmx_exit_ctls_high
);
3282 if (msr_index
== MSR_IA32_VMX_EXIT_CTLS
)
3283 *pdata
|= VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR
;
3285 case MSR_IA32_VMX_TRUE_ENTRY_CTLS
:
3286 case MSR_IA32_VMX_ENTRY_CTLS
:
3287 *pdata
= vmx_control_msr(
3288 vmx
->nested
.nested_vmx_entry_ctls_low
,
3289 vmx
->nested
.nested_vmx_entry_ctls_high
);
3290 if (msr_index
== MSR_IA32_VMX_ENTRY_CTLS
)
3291 *pdata
|= VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR
;
3293 case MSR_IA32_VMX_MISC
:
3294 *pdata
= vmx_control_msr(
3295 vmx
->nested
.nested_vmx_misc_low
,
3296 vmx
->nested
.nested_vmx_misc_high
);
3298 case MSR_IA32_VMX_CR0_FIXED0
:
3299 *pdata
= vmx
->nested
.nested_vmx_cr0_fixed0
;
3301 case MSR_IA32_VMX_CR0_FIXED1
:
3302 *pdata
= vmx
->nested
.nested_vmx_cr0_fixed1
;
3304 case MSR_IA32_VMX_CR4_FIXED0
:
3305 *pdata
= vmx
->nested
.nested_vmx_cr4_fixed0
;
3307 case MSR_IA32_VMX_CR4_FIXED1
:
3308 *pdata
= vmx
->nested
.nested_vmx_cr4_fixed1
;
3310 case MSR_IA32_VMX_VMCS_ENUM
:
3311 *pdata
= vmx
->nested
.nested_vmx_vmcs_enum
;
3313 case MSR_IA32_VMX_PROCBASED_CTLS2
:
3314 *pdata
= vmx_control_msr(
3315 vmx
->nested
.nested_vmx_secondary_ctls_low
,
3316 vmx
->nested
.nested_vmx_secondary_ctls_high
);
3318 case MSR_IA32_VMX_EPT_VPID_CAP
:
3319 *pdata
= vmx
->nested
.nested_vmx_ept_caps
|
3320 ((u64
)vmx
->nested
.nested_vmx_vpid_caps
<< 32);
3322 case MSR_IA32_VMX_VMFUNC
:
3323 *pdata
= vmx
->nested
.nested_vmx_vmfunc_controls
;
3332 static inline bool vmx_feature_control_msr_valid(struct kvm_vcpu
*vcpu
,
3335 uint64_t valid_bits
= to_vmx(vcpu
)->msr_ia32_feature_control_valid_bits
;
3337 return !(val
& ~valid_bits
);
3341 * Reads an msr value (of 'msr_index') into 'pdata'.
3342 * Returns 0 on success, non-0 otherwise.
3343 * Assumes vcpu_load() was already called.
3345 static int vmx_get_msr(struct kvm_vcpu
*vcpu
, struct msr_data
*msr_info
)
3347 struct shared_msr_entry
*msr
;
3349 switch (msr_info
->index
) {
3350 #ifdef CONFIG_X86_64
3352 msr_info
->data
= vmcs_readl(GUEST_FS_BASE
);
3355 msr_info
->data
= vmcs_readl(GUEST_GS_BASE
);
3357 case MSR_KERNEL_GS_BASE
:
3358 vmx_load_host_state(to_vmx(vcpu
));
3359 msr_info
->data
= to_vmx(vcpu
)->msr_guest_kernel_gs_base
;
3363 return kvm_get_msr_common(vcpu
, msr_info
);
3365 msr_info
->data
= guest_read_tsc(vcpu
);
3367 case MSR_IA32_SPEC_CTRL
:
3368 if (!msr_info
->host_initiated
&&
3369 !guest_cpuid_has(vcpu
, X86_FEATURE_SPEC_CTRL
))
3372 msr_info
->data
= to_vmx(vcpu
)->spec_ctrl
;
3374 case MSR_IA32_ARCH_CAPABILITIES
:
3375 if (!msr_info
->host_initiated
&&
3376 !guest_cpuid_has(vcpu
, X86_FEATURE_ARCH_CAPABILITIES
))
3378 msr_info
->data
= to_vmx(vcpu
)->arch_capabilities
;
3380 case MSR_IA32_SYSENTER_CS
:
3381 msr_info
->data
= vmcs_read32(GUEST_SYSENTER_CS
);
3383 case MSR_IA32_SYSENTER_EIP
:
3384 msr_info
->data
= vmcs_readl(GUEST_SYSENTER_EIP
);
3386 case MSR_IA32_SYSENTER_ESP
:
3387 msr_info
->data
= vmcs_readl(GUEST_SYSENTER_ESP
);
3389 case MSR_IA32_BNDCFGS
:
3390 if (!kvm_mpx_supported() ||
3391 (!msr_info
->host_initiated
&&
3392 !guest_cpuid_has(vcpu
, X86_FEATURE_MPX
)))
3394 msr_info
->data
= vmcs_read64(GUEST_BNDCFGS
);
3396 case MSR_IA32_MCG_EXT_CTL
:
3397 if (!msr_info
->host_initiated
&&
3398 !(to_vmx(vcpu
)->msr_ia32_feature_control
&
3399 FEATURE_CONTROL_LMCE
))
3401 msr_info
->data
= vcpu
->arch
.mcg_ext_ctl
;
3403 case MSR_IA32_FEATURE_CONTROL
:
3404 msr_info
->data
= to_vmx(vcpu
)->msr_ia32_feature_control
;
3406 case MSR_IA32_VMX_BASIC
... MSR_IA32_VMX_VMFUNC
:
3407 if (!nested_vmx_allowed(vcpu
))
3409 return vmx_get_vmx_msr(vcpu
, msr_info
->index
, &msr_info
->data
);
3411 if (!vmx_xsaves_supported())
3413 msr_info
->data
= vcpu
->arch
.ia32_xss
;
3416 if (!msr_info
->host_initiated
&&
3417 !guest_cpuid_has(vcpu
, X86_FEATURE_RDTSCP
))
3419 /* Otherwise falls through */
3421 msr
= find_msr_entry(to_vmx(vcpu
), msr_info
->index
);
3423 msr_info
->data
= msr
->data
;
3426 return kvm_get_msr_common(vcpu
, msr_info
);
3432 static void vmx_leave_nested(struct kvm_vcpu
*vcpu
);
3435 * Writes msr value into into the appropriate "register".
3436 * Returns 0 on success, non-0 otherwise.
3437 * Assumes vcpu_load() was already called.
3439 static int vmx_set_msr(struct kvm_vcpu
*vcpu
, struct msr_data
*msr_info
)
3441 struct vcpu_vmx
*vmx
= to_vmx(vcpu
);
3442 struct shared_msr_entry
*msr
;
3444 u32 msr_index
= msr_info
->index
;
3445 u64 data
= msr_info
->data
;
3447 switch (msr_index
) {
3449 ret
= kvm_set_msr_common(vcpu
, msr_info
);
3451 #ifdef CONFIG_X86_64
3453 vmx_segment_cache_clear(vmx
);
3454 vmcs_writel(GUEST_FS_BASE
, data
);
3457 vmx_segment_cache_clear(vmx
);
3458 vmcs_writel(GUEST_GS_BASE
, data
);
3460 case MSR_KERNEL_GS_BASE
:
3461 vmx_load_host_state(vmx
);
3462 vmx
->msr_guest_kernel_gs_base
= data
;
3465 case MSR_IA32_SYSENTER_CS
:
3466 vmcs_write32(GUEST_SYSENTER_CS
, data
);
3468 case MSR_IA32_SYSENTER_EIP
:
3469 vmcs_writel(GUEST_SYSENTER_EIP
, data
);
3471 case MSR_IA32_SYSENTER_ESP
:
3472 vmcs_writel(GUEST_SYSENTER_ESP
, data
);
3474 case MSR_IA32_BNDCFGS
:
3475 if (!kvm_mpx_supported() ||
3476 (!msr_info
->host_initiated
&&
3477 !guest_cpuid_has(vcpu
, X86_FEATURE_MPX
)))
3479 if (is_noncanonical_address(data
& PAGE_MASK
, vcpu
) ||
3480 (data
& MSR_IA32_BNDCFGS_RSVD
))
3482 vmcs_write64(GUEST_BNDCFGS
, data
);
3485 kvm_write_tsc(vcpu
, msr_info
);
3487 case MSR_IA32_SPEC_CTRL
:
3488 if (!msr_info
->host_initiated
&&
3489 !guest_cpuid_has(vcpu
, X86_FEATURE_SPEC_CTRL
))
3492 /* The STIBP bit doesn't fault even if it's not advertised */
3493 if (data
& ~(SPEC_CTRL_IBRS
| SPEC_CTRL_STIBP
| SPEC_CTRL_SSBD
))
3496 vmx
->spec_ctrl
= data
;
3503 * When it's written (to non-zero) for the first time, pass
3507 * The handling of the MSR bitmap for L2 guests is done in
3508 * nested_vmx_merge_msr_bitmap. We should not touch the
3509 * vmcs02.msr_bitmap here since it gets completely overwritten
3510 * in the merging. We update the vmcs01 here for L1 as well
3511 * since it will end up touching the MSR anyway now.
3513 vmx_disable_intercept_for_msr(vmx
->vmcs01
.msr_bitmap
,
3517 case MSR_IA32_PRED_CMD
:
3518 if (!msr_info
->host_initiated
&&
3519 !guest_cpuid_has(vcpu
, X86_FEATURE_SPEC_CTRL
))
3522 if (data
& ~PRED_CMD_IBPB
)
3528 wrmsrl(MSR_IA32_PRED_CMD
, PRED_CMD_IBPB
);
3532 * When it's written (to non-zero) for the first time, pass
3536 * The handling of the MSR bitmap for L2 guests is done in
3537 * nested_vmx_merge_msr_bitmap. We should not touch the
3538 * vmcs02.msr_bitmap here since it gets completely overwritten
3541 vmx_disable_intercept_for_msr(vmx
->vmcs01
.msr_bitmap
, MSR_IA32_PRED_CMD
,
3544 case MSR_IA32_ARCH_CAPABILITIES
:
3545 if (!msr_info
->host_initiated
)
3547 vmx
->arch_capabilities
= data
;
3549 case MSR_IA32_CR_PAT
:
3550 if (vmcs_config
.vmentry_ctrl
& VM_ENTRY_LOAD_IA32_PAT
) {
3551 if (!kvm_mtrr_valid(vcpu
, MSR_IA32_CR_PAT
, data
))
3553 vmcs_write64(GUEST_IA32_PAT
, data
);
3554 vcpu
->arch
.pat
= data
;
3557 ret
= kvm_set_msr_common(vcpu
, msr_info
);
3559 case MSR_IA32_TSC_ADJUST
:
3560 ret
= kvm_set_msr_common(vcpu
, msr_info
);
3562 case MSR_IA32_MCG_EXT_CTL
:
3563 if ((!msr_info
->host_initiated
&&
3564 !(to_vmx(vcpu
)->msr_ia32_feature_control
&
3565 FEATURE_CONTROL_LMCE
)) ||
3566 (data
& ~MCG_EXT_CTL_LMCE_EN
))
3568 vcpu
->arch
.mcg_ext_ctl
= data
;
3570 case MSR_IA32_FEATURE_CONTROL
:
3571 if (!vmx_feature_control_msr_valid(vcpu
, data
) ||
3572 (to_vmx(vcpu
)->msr_ia32_feature_control
&
3573 FEATURE_CONTROL_LOCKED
&& !msr_info
->host_initiated
))
3575 vmx
->msr_ia32_feature_control
= data
;
3576 if (msr_info
->host_initiated
&& data
== 0)
3577 vmx_leave_nested(vcpu
);
3579 case MSR_IA32_VMX_BASIC
... MSR_IA32_VMX_VMFUNC
:
3580 if (!msr_info
->host_initiated
)
3581 return 1; /* they are read-only */
3582 if (!nested_vmx_allowed(vcpu
))
3584 return vmx_set_vmx_msr(vcpu
, msr_index
, data
);
3586 if (!vmx_xsaves_supported())
3589 * The only supported bit as of Skylake is bit 8, but
3590 * it is not supported on KVM.
3594 vcpu
->arch
.ia32_xss
= data
;
3595 if (vcpu
->arch
.ia32_xss
!= host_xss
)
3596 add_atomic_switch_msr(vmx
, MSR_IA32_XSS
,
3597 vcpu
->arch
.ia32_xss
, host_xss
, false);
3599 clear_atomic_switch_msr(vmx
, MSR_IA32_XSS
);
3602 if (!msr_info
->host_initiated
&&
3603 !guest_cpuid_has(vcpu
, X86_FEATURE_RDTSCP
))
3605 /* Check reserved bit, higher 32 bits should be zero */
3606 if ((data
>> 32) != 0)
3608 /* Otherwise falls through */
3610 msr
= find_msr_entry(vmx
, msr_index
);
3612 u64 old_msr_data
= msr
->data
;
3614 if (msr
- vmx
->guest_msrs
< vmx
->save_nmsrs
) {
3616 ret
= kvm_set_shared_msr(msr
->index
, msr
->data
,
3620 msr
->data
= old_msr_data
;
3624 ret
= kvm_set_msr_common(vcpu
, msr_info
);
3630 static void vmx_cache_reg(struct kvm_vcpu
*vcpu
, enum kvm_reg reg
)
3632 __set_bit(reg
, (unsigned long *)&vcpu
->arch
.regs_avail
);
3635 vcpu
->arch
.regs
[VCPU_REGS_RSP
] = vmcs_readl(GUEST_RSP
);
3638 vcpu
->arch
.regs
[VCPU_REGS_RIP
] = vmcs_readl(GUEST_RIP
);
3640 case VCPU_EXREG_PDPTR
:
3642 ept_save_pdptrs(vcpu
);
3649 static __init
int cpu_has_kvm_support(void)
3651 return cpu_has_vmx();
3654 static __init
int vmx_disabled_by_bios(void)
3658 rdmsrl(MSR_IA32_FEATURE_CONTROL
, msr
);
3659 if (msr
& FEATURE_CONTROL_LOCKED
) {
3660 /* launched w/ TXT and VMX disabled */
3661 if (!(msr
& FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX
)
3664 /* launched w/o TXT and VMX only enabled w/ TXT */
3665 if (!(msr
& FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX
)
3666 && (msr
& FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX
)
3667 && !tboot_enabled()) {
3668 printk(KERN_WARNING
"kvm: disable TXT in the BIOS or "
3669 "activate TXT before enabling KVM\n");
3672 /* launched w/o TXT and VMX disabled */
3673 if (!(msr
& FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX
)
3674 && !tboot_enabled())
3681 static void kvm_cpu_vmxon(u64 addr
)
3683 cr4_set_bits(X86_CR4_VMXE
);
3684 intel_pt_handle_vmx(1);
3686 asm volatile (ASM_VMX_VMXON_RAX
3687 : : "a"(&addr
), "m"(addr
)
3691 static int hardware_enable(void)
3693 int cpu
= raw_smp_processor_id();
3694 u64 phys_addr
= __pa(per_cpu(vmxarea
, cpu
));
3697 if (cr4_read_shadow() & X86_CR4_VMXE
)
3700 INIT_LIST_HEAD(&per_cpu(loaded_vmcss_on_cpu
, cpu
));
3701 INIT_LIST_HEAD(&per_cpu(blocked_vcpu_on_cpu
, cpu
));
3702 spin_lock_init(&per_cpu(blocked_vcpu_on_cpu_lock
, cpu
));
3705 * Now we can enable the vmclear operation in kdump
3706 * since the loaded_vmcss_on_cpu list on this cpu
3707 * has been initialized.
3709 * Though the cpu is not in VMX operation now, there
3710 * is no problem to enable the vmclear operation
3711 * for the loaded_vmcss_on_cpu list is empty!
3713 crash_enable_local_vmclear(cpu
);
3715 rdmsrl(MSR_IA32_FEATURE_CONTROL
, old
);
3717 test_bits
= FEATURE_CONTROL_LOCKED
;
3718 test_bits
|= FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX
;
3719 if (tboot_enabled())
3720 test_bits
|= FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX
;
3722 if ((old
& test_bits
) != test_bits
) {
3723 /* enable and lock */
3724 wrmsrl(MSR_IA32_FEATURE_CONTROL
, old
| test_bits
);
3726 kvm_cpu_vmxon(phys_addr
);
3733 static void vmclear_local_loaded_vmcss(void)
3735 int cpu
= raw_smp_processor_id();
3736 struct loaded_vmcs
*v
, *n
;
3738 list_for_each_entry_safe(v
, n
, &per_cpu(loaded_vmcss_on_cpu
, cpu
),
3739 loaded_vmcss_on_cpu_link
)
3740 __loaded_vmcs_clear(v
);
3744 /* Just like cpu_vmxoff(), but with the __kvm_handle_fault_on_reboot()
3747 static void kvm_cpu_vmxoff(void)
3749 asm volatile (__ex(ASM_VMX_VMXOFF
) : : : "cc");
3751 intel_pt_handle_vmx(0);
3752 cr4_clear_bits(X86_CR4_VMXE
);
3755 static void hardware_disable(void)
3757 vmclear_local_loaded_vmcss();
3761 static __init
int adjust_vmx_controls(u32 ctl_min
, u32 ctl_opt
,
3762 u32 msr
, u32
*result
)
3764 u32 vmx_msr_low
, vmx_msr_high
;
3765 u32 ctl
= ctl_min
| ctl_opt
;
3767 rdmsr(msr
, vmx_msr_low
, vmx_msr_high
);
3769 ctl
&= vmx_msr_high
; /* bit == 0 in high word ==> must be zero */
3770 ctl
|= vmx_msr_low
; /* bit == 1 in low word ==> must be one */
3772 /* Ensure minimum (required) set of control bits are supported. */
3780 static __init
bool allow_1_setting(u32 msr
, u32 ctl
)
3782 u32 vmx_msr_low
, vmx_msr_high
;
3784 rdmsr(msr
, vmx_msr_low
, vmx_msr_high
);
3785 return vmx_msr_high
& ctl
;
3788 static __init
int setup_vmcs_config(struct vmcs_config
*vmcs_conf
)
3790 u32 vmx_msr_low
, vmx_msr_high
;
3791 u32 min
, opt
, min2
, opt2
;
3792 u32 _pin_based_exec_control
= 0;
3793 u32 _cpu_based_exec_control
= 0;
3794 u32 _cpu_based_2nd_exec_control
= 0;
3795 u32 _vmexit_control
= 0;
3796 u32 _vmentry_control
= 0;
3798 min
= CPU_BASED_HLT_EXITING
|
3799 #ifdef CONFIG_X86_64
3800 CPU_BASED_CR8_LOAD_EXITING
|
3801 CPU_BASED_CR8_STORE_EXITING
|
3803 CPU_BASED_CR3_LOAD_EXITING
|
3804 CPU_BASED_CR3_STORE_EXITING
|
3805 CPU_BASED_USE_IO_BITMAPS
|
3806 CPU_BASED_MOV_DR_EXITING
|
3807 CPU_BASED_USE_TSC_OFFSETING
|
3808 CPU_BASED_INVLPG_EXITING
|
3809 CPU_BASED_RDPMC_EXITING
;
3811 if (!kvm_mwait_in_guest())
3812 min
|= CPU_BASED_MWAIT_EXITING
|
3813 CPU_BASED_MONITOR_EXITING
;
3815 opt
= CPU_BASED_TPR_SHADOW
|
3816 CPU_BASED_USE_MSR_BITMAPS
|
3817 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS
;
3818 if (adjust_vmx_controls(min
, opt
, MSR_IA32_VMX_PROCBASED_CTLS
,
3819 &_cpu_based_exec_control
) < 0)
3821 #ifdef CONFIG_X86_64
3822 if ((_cpu_based_exec_control
& CPU_BASED_TPR_SHADOW
))
3823 _cpu_based_exec_control
&= ~CPU_BASED_CR8_LOAD_EXITING
&
3824 ~CPU_BASED_CR8_STORE_EXITING
;
3826 if (_cpu_based_exec_control
& CPU_BASED_ACTIVATE_SECONDARY_CONTROLS
) {
3828 opt2
= SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES
|
3829 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE
|
3830 SECONDARY_EXEC_WBINVD_EXITING
|
3831 SECONDARY_EXEC_ENABLE_VPID
|
3832 SECONDARY_EXEC_ENABLE_EPT
|
3833 SECONDARY_EXEC_UNRESTRICTED_GUEST
|
3834 SECONDARY_EXEC_PAUSE_LOOP_EXITING
|
3835 SECONDARY_EXEC_RDTSCP
|
3836 SECONDARY_EXEC_ENABLE_INVPCID
|
3837 SECONDARY_EXEC_APIC_REGISTER_VIRT
|
3838 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY
|
3839 SECONDARY_EXEC_SHADOW_VMCS
|
3840 SECONDARY_EXEC_XSAVES
|
3841 SECONDARY_EXEC_RDSEED_EXITING
|
3842 SECONDARY_EXEC_RDRAND_EXITING
|
3843 SECONDARY_EXEC_ENABLE_PML
|
3844 SECONDARY_EXEC_TSC_SCALING
|
3845 SECONDARY_EXEC_ENABLE_VMFUNC
;
3846 if (adjust_vmx_controls(min2
, opt2
,
3847 MSR_IA32_VMX_PROCBASED_CTLS2
,
3848 &_cpu_based_2nd_exec_control
) < 0)
3851 #ifndef CONFIG_X86_64
3852 if (!(_cpu_based_2nd_exec_control
&
3853 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES
))
3854 _cpu_based_exec_control
&= ~CPU_BASED_TPR_SHADOW
;
3857 if (!(_cpu_based_exec_control
& CPU_BASED_TPR_SHADOW
))
3858 _cpu_based_2nd_exec_control
&= ~(
3859 SECONDARY_EXEC_APIC_REGISTER_VIRT
|
3860 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE
|
3861 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY
);
3863 rdmsr_safe(MSR_IA32_VMX_EPT_VPID_CAP
,
3864 &vmx_capability
.ept
, &vmx_capability
.vpid
);
3866 if (_cpu_based_2nd_exec_control
& SECONDARY_EXEC_ENABLE_EPT
) {
3867 /* CR3 accesses and invlpg don't need to cause VM Exits when EPT
3869 _cpu_based_exec_control
&= ~(CPU_BASED_CR3_LOAD_EXITING
|
3870 CPU_BASED_CR3_STORE_EXITING
|
3871 CPU_BASED_INVLPG_EXITING
);
3872 } else if (vmx_capability
.ept
) {
3873 vmx_capability
.ept
= 0;
3874 pr_warn_once("EPT CAP should not exist if not support "
3875 "1-setting enable EPT VM-execution control\n");
3877 if (!(_cpu_based_2nd_exec_control
& SECONDARY_EXEC_ENABLE_VPID
) &&
3878 vmx_capability
.vpid
) {
3879 vmx_capability
.vpid
= 0;
3880 pr_warn_once("VPID CAP should not exist if not support "
3881 "1-setting enable VPID VM-execution control\n");
3884 min
= VM_EXIT_SAVE_DEBUG_CONTROLS
| VM_EXIT_ACK_INTR_ON_EXIT
;
3885 #ifdef CONFIG_X86_64
3886 min
|= VM_EXIT_HOST_ADDR_SPACE_SIZE
;
3888 opt
= VM_EXIT_SAVE_IA32_PAT
| VM_EXIT_LOAD_IA32_PAT
|
3889 VM_EXIT_CLEAR_BNDCFGS
;
3890 if (adjust_vmx_controls(min
, opt
, MSR_IA32_VMX_EXIT_CTLS
,
3891 &_vmexit_control
) < 0)
3894 min
= PIN_BASED_EXT_INTR_MASK
| PIN_BASED_NMI_EXITING
;
3895 opt
= PIN_BASED_VIRTUAL_NMIS
| PIN_BASED_POSTED_INTR
|
3896 PIN_BASED_VMX_PREEMPTION_TIMER
;
3897 if (adjust_vmx_controls(min
, opt
, MSR_IA32_VMX_PINBASED_CTLS
,
3898 &_pin_based_exec_control
) < 0)
3901 if (cpu_has_broken_vmx_preemption_timer())
3902 _pin_based_exec_control
&= ~PIN_BASED_VMX_PREEMPTION_TIMER
;
3903 if (!(_cpu_based_2nd_exec_control
&
3904 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY
))
3905 _pin_based_exec_control
&= ~PIN_BASED_POSTED_INTR
;
3907 min
= VM_ENTRY_LOAD_DEBUG_CONTROLS
;
3908 opt
= VM_ENTRY_LOAD_IA32_PAT
| VM_ENTRY_LOAD_BNDCFGS
;
3909 if (adjust_vmx_controls(min
, opt
, MSR_IA32_VMX_ENTRY_CTLS
,
3910 &_vmentry_control
) < 0)
3913 rdmsr(MSR_IA32_VMX_BASIC
, vmx_msr_low
, vmx_msr_high
);
3915 /* IA-32 SDM Vol 3B: VMCS size is never greater than 4kB. */
3916 if ((vmx_msr_high
& 0x1fff) > PAGE_SIZE
)
3919 #ifdef CONFIG_X86_64
3920 /* IA-32 SDM Vol 3B: 64-bit CPUs always have VMX_BASIC_MSR[48]==0. */
3921 if (vmx_msr_high
& (1u<<16))
3925 /* Require Write-Back (WB) memory type for VMCS accesses. */
3926 if (((vmx_msr_high
>> 18) & 15) != 6)
3929 vmcs_conf
->size
= vmx_msr_high
& 0x1fff;
3930 vmcs_conf
->order
= get_order(vmcs_conf
->size
);
3931 vmcs_conf
->basic_cap
= vmx_msr_high
& ~0x1fff;
3932 vmcs_conf
->revision_id
= vmx_msr_low
;
3934 vmcs_conf
->pin_based_exec_ctrl
= _pin_based_exec_control
;
3935 vmcs_conf
->cpu_based_exec_ctrl
= _cpu_based_exec_control
;
3936 vmcs_conf
->cpu_based_2nd_exec_ctrl
= _cpu_based_2nd_exec_control
;
3937 vmcs_conf
->vmexit_ctrl
= _vmexit_control
;
3938 vmcs_conf
->vmentry_ctrl
= _vmentry_control
;
3940 cpu_has_load_ia32_efer
=
3941 allow_1_setting(MSR_IA32_VMX_ENTRY_CTLS
,
3942 VM_ENTRY_LOAD_IA32_EFER
)
3943 && allow_1_setting(MSR_IA32_VMX_EXIT_CTLS
,
3944 VM_EXIT_LOAD_IA32_EFER
);
3946 cpu_has_load_perf_global_ctrl
=
3947 allow_1_setting(MSR_IA32_VMX_ENTRY_CTLS
,
3948 VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL
)
3949 && allow_1_setting(MSR_IA32_VMX_EXIT_CTLS
,
3950 VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL
);
3953 * Some cpus support VM_ENTRY_(LOAD|SAVE)_IA32_PERF_GLOBAL_CTRL
3954 * but due to errata below it can't be used. Workaround is to use
3955 * msr load mechanism to switch IA32_PERF_GLOBAL_CTRL.
3957 * VM Exit May Incorrectly Clear IA32_PERF_GLOBAL_CTRL [34:32]
3962 * BC86,AAY89,BD102 (model 44)
3966 if (cpu_has_load_perf_global_ctrl
&& boot_cpu_data
.x86
== 0x6) {
3967 switch (boot_cpu_data
.x86_model
) {
3973 cpu_has_load_perf_global_ctrl
= false;
3974 printk_once(KERN_WARNING
"kvm: VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL "
3975 "does not work properly. Using workaround\n");
3982 if (boot_cpu_has(X86_FEATURE_XSAVES
))
3983 rdmsrl(MSR_IA32_XSS
, host_xss
);
3988 static struct vmcs
*alloc_vmcs_cpu(int cpu
)
3990 int node
= cpu_to_node(cpu
);
3994 pages
= __alloc_pages_node(node
, GFP_KERNEL
, vmcs_config
.order
);
3997 vmcs
= page_address(pages
);
3998 memset(vmcs
, 0, vmcs_config
.size
);
3999 vmcs
->revision_id
= vmcs_config
.revision_id
; /* vmcs revision id */
4003 static void free_vmcs(struct vmcs
*vmcs
)
4005 free_pages((unsigned long)vmcs
, vmcs_config
.order
);
4009 * Free a VMCS, but before that VMCLEAR it on the CPU where it was last loaded
4011 static void free_loaded_vmcs(struct loaded_vmcs
*loaded_vmcs
)
4013 if (!loaded_vmcs
->vmcs
)
4015 loaded_vmcs_clear(loaded_vmcs
);
4016 free_vmcs(loaded_vmcs
->vmcs
);
4017 loaded_vmcs
->vmcs
= NULL
;
4018 if (loaded_vmcs
->msr_bitmap
)
4019 free_page((unsigned long)loaded_vmcs
->msr_bitmap
);
4020 WARN_ON(loaded_vmcs
->shadow_vmcs
!= NULL
);
4023 static struct vmcs
*alloc_vmcs(void)
4025 return alloc_vmcs_cpu(raw_smp_processor_id());
4028 static int alloc_loaded_vmcs(struct loaded_vmcs
*loaded_vmcs
)
4030 loaded_vmcs
->vmcs
= alloc_vmcs();
4031 if (!loaded_vmcs
->vmcs
)
4034 loaded_vmcs
->shadow_vmcs
= NULL
;
4035 loaded_vmcs_init(loaded_vmcs
);
4037 if (cpu_has_vmx_msr_bitmap()) {
4038 loaded_vmcs
->msr_bitmap
= (unsigned long *)__get_free_page(GFP_KERNEL
);
4039 if (!loaded_vmcs
->msr_bitmap
)
4041 memset(loaded_vmcs
->msr_bitmap
, 0xff, PAGE_SIZE
);
4046 free_loaded_vmcs(loaded_vmcs
);
4050 static void free_kvm_area(void)
4054 for_each_possible_cpu(cpu
) {
4055 free_vmcs(per_cpu(vmxarea
, cpu
));
4056 per_cpu(vmxarea
, cpu
) = NULL
;
4060 enum vmcs_field_type
{
4061 VMCS_FIELD_TYPE_U16
= 0,
4062 VMCS_FIELD_TYPE_U64
= 1,
4063 VMCS_FIELD_TYPE_U32
= 2,
4064 VMCS_FIELD_TYPE_NATURAL_WIDTH
= 3
4067 static inline int vmcs_field_type(unsigned long field
)
4069 if (0x1 & field
) /* the *_HIGH fields are all 32 bit */
4070 return VMCS_FIELD_TYPE_U32
;
4071 return (field
>> 13) & 0x3 ;
4074 static inline int vmcs_field_readonly(unsigned long field
)
4076 return (((field
>> 10) & 0x3) == 1);
4079 static void init_vmcs_shadow_fields(void)
4083 /* No checks for read only fields yet */
4085 for (i
= j
= 0; i
< max_shadow_read_write_fields
; i
++) {
4086 switch (shadow_read_write_fields
[i
]) {
4088 if (!kvm_mpx_supported())
4096 shadow_read_write_fields
[j
] =
4097 shadow_read_write_fields
[i
];
4100 max_shadow_read_write_fields
= j
;
4102 /* shadowed fields guest access without vmexit */
4103 for (i
= 0; i
< max_shadow_read_write_fields
; i
++) {
4104 unsigned long field
= shadow_read_write_fields
[i
];
4106 clear_bit(field
, vmx_vmwrite_bitmap
);
4107 clear_bit(field
, vmx_vmread_bitmap
);
4108 if (vmcs_field_type(field
) == VMCS_FIELD_TYPE_U64
) {
4109 clear_bit(field
+ 1, vmx_vmwrite_bitmap
);
4110 clear_bit(field
+ 1, vmx_vmread_bitmap
);
4113 for (i
= 0; i
< max_shadow_read_only_fields
; i
++) {
4114 unsigned long field
= shadow_read_only_fields
[i
];
4116 clear_bit(field
, vmx_vmread_bitmap
);
4117 if (vmcs_field_type(field
) == VMCS_FIELD_TYPE_U64
)
4118 clear_bit(field
+ 1, vmx_vmread_bitmap
);
4122 static __init
int alloc_kvm_area(void)
4126 for_each_possible_cpu(cpu
) {
4129 vmcs
= alloc_vmcs_cpu(cpu
);
4135 per_cpu(vmxarea
, cpu
) = vmcs
;
4140 static void fix_pmode_seg(struct kvm_vcpu
*vcpu
, int seg
,
4141 struct kvm_segment
*save
)
4143 if (!emulate_invalid_guest_state
) {
4145 * CS and SS RPL should be equal during guest entry according
4146 * to VMX spec, but in reality it is not always so. Since vcpu
4147 * is in the middle of the transition from real mode to
4148 * protected mode it is safe to assume that RPL 0 is a good
4151 if (seg
== VCPU_SREG_CS
|| seg
== VCPU_SREG_SS
)
4152 save
->selector
&= ~SEGMENT_RPL_MASK
;
4153 save
->dpl
= save
->selector
& SEGMENT_RPL_MASK
;
4156 vmx_set_segment(vcpu
, save
, seg
);
4159 static void enter_pmode(struct kvm_vcpu
*vcpu
)
4161 unsigned long flags
;
4162 struct vcpu_vmx
*vmx
= to_vmx(vcpu
);
4165 * Update real mode segment cache. It may be not up-to-date if sement
4166 * register was written while vcpu was in a guest mode.
4168 vmx_get_segment(vcpu
, &vmx
->rmode
.segs
[VCPU_SREG_ES
], VCPU_SREG_ES
);
4169 vmx_get_segment(vcpu
, &vmx
->rmode
.segs
[VCPU_SREG_DS
], VCPU_SREG_DS
);
4170 vmx_get_segment(vcpu
, &vmx
->rmode
.segs
[VCPU_SREG_FS
], VCPU_SREG_FS
);
4171 vmx_get_segment(vcpu
, &vmx
->rmode
.segs
[VCPU_SREG_GS
], VCPU_SREG_GS
);
4172 vmx_get_segment(vcpu
, &vmx
->rmode
.segs
[VCPU_SREG_SS
], VCPU_SREG_SS
);
4173 vmx_get_segment(vcpu
, &vmx
->rmode
.segs
[VCPU_SREG_CS
], VCPU_SREG_CS
);
4175 vmx
->rmode
.vm86_active
= 0;
4177 vmx_segment_cache_clear(vmx
);
4179 vmx_set_segment(vcpu
, &vmx
->rmode
.segs
[VCPU_SREG_TR
], VCPU_SREG_TR
);
4181 flags
= vmcs_readl(GUEST_RFLAGS
);
4182 flags
&= RMODE_GUEST_OWNED_EFLAGS_BITS
;
4183 flags
|= vmx
->rmode
.save_rflags
& ~RMODE_GUEST_OWNED_EFLAGS_BITS
;
4184 vmcs_writel(GUEST_RFLAGS
, flags
);
4186 vmcs_writel(GUEST_CR4
, (vmcs_readl(GUEST_CR4
) & ~X86_CR4_VME
) |
4187 (vmcs_readl(CR4_READ_SHADOW
) & X86_CR4_VME
));
4189 update_exception_bitmap(vcpu
);
4191 fix_pmode_seg(vcpu
, VCPU_SREG_CS
, &vmx
->rmode
.segs
[VCPU_SREG_CS
]);
4192 fix_pmode_seg(vcpu
, VCPU_SREG_SS
, &vmx
->rmode
.segs
[VCPU_SREG_SS
]);
4193 fix_pmode_seg(vcpu
, VCPU_SREG_ES
, &vmx
->rmode
.segs
[VCPU_SREG_ES
]);
4194 fix_pmode_seg(vcpu
, VCPU_SREG_DS
, &vmx
->rmode
.segs
[VCPU_SREG_DS
]);
4195 fix_pmode_seg(vcpu
, VCPU_SREG_FS
, &vmx
->rmode
.segs
[VCPU_SREG_FS
]);
4196 fix_pmode_seg(vcpu
, VCPU_SREG_GS
, &vmx
->rmode
.segs
[VCPU_SREG_GS
]);
4199 static void fix_rmode_seg(int seg
, struct kvm_segment
*save
)
4201 const struct kvm_vmx_segment_field
*sf
= &kvm_vmx_segment_fields
[seg
];
4202 struct kvm_segment var
= *save
;
4205 if (seg
== VCPU_SREG_CS
)
4208 if (!emulate_invalid_guest_state
) {
4209 var
.selector
= var
.base
>> 4;
4210 var
.base
= var
.base
& 0xffff0;
4220 if (save
->base
& 0xf)
4221 printk_once(KERN_WARNING
"kvm: segment base is not "
4222 "paragraph aligned when entering "
4223 "protected mode (seg=%d)", seg
);
4226 vmcs_write16(sf
->selector
, var
.selector
);
4227 vmcs_writel(sf
->base
, var
.base
);
4228 vmcs_write32(sf
->limit
, var
.limit
);
4229 vmcs_write32(sf
->ar_bytes
, vmx_segment_access_rights(&var
));
4232 static void enter_rmode(struct kvm_vcpu
*vcpu
)
4234 unsigned long flags
;
4235 struct vcpu_vmx
*vmx
= to_vmx(vcpu
);
4237 vmx_get_segment(vcpu
, &vmx
->rmode
.segs
[VCPU_SREG_TR
], VCPU_SREG_TR
);
4238 vmx_get_segment(vcpu
, &vmx
->rmode
.segs
[VCPU_SREG_ES
], VCPU_SREG_ES
);
4239 vmx_get_segment(vcpu
, &vmx
->rmode
.segs
[VCPU_SREG_DS
], VCPU_SREG_DS
);
4240 vmx_get_segment(vcpu
, &vmx
->rmode
.segs
[VCPU_SREG_FS
], VCPU_SREG_FS
);
4241 vmx_get_segment(vcpu
, &vmx
->rmode
.segs
[VCPU_SREG_GS
], VCPU_SREG_GS
);
4242 vmx_get_segment(vcpu
, &vmx
->rmode
.segs
[VCPU_SREG_SS
], VCPU_SREG_SS
);
4243 vmx_get_segment(vcpu
, &vmx
->rmode
.segs
[VCPU_SREG_CS
], VCPU_SREG_CS
);
4245 vmx
->rmode
.vm86_active
= 1;
4248 * Very old userspace does not call KVM_SET_TSS_ADDR before entering
4249 * vcpu. Warn the user that an update is overdue.
4251 if (!vcpu
->kvm
->arch
.tss_addr
)
4252 printk_once(KERN_WARNING
"kvm: KVM_SET_TSS_ADDR need to be "
4253 "called before entering vcpu\n");
4255 vmx_segment_cache_clear(vmx
);
4257 vmcs_writel(GUEST_TR_BASE
, vcpu
->kvm
->arch
.tss_addr
);
4258 vmcs_write32(GUEST_TR_LIMIT
, RMODE_TSS_SIZE
- 1);
4259 vmcs_write32(GUEST_TR_AR_BYTES
, 0x008b);
4261 flags
= vmcs_readl(GUEST_RFLAGS
);
4262 vmx
->rmode
.save_rflags
= flags
;
4264 flags
|= X86_EFLAGS_IOPL
| X86_EFLAGS_VM
;
4266 vmcs_writel(GUEST_RFLAGS
, flags
);
4267 vmcs_writel(GUEST_CR4
, vmcs_readl(GUEST_CR4
) | X86_CR4_VME
);
4268 update_exception_bitmap(vcpu
);
4270 fix_rmode_seg(VCPU_SREG_SS
, &vmx
->rmode
.segs
[VCPU_SREG_SS
]);
4271 fix_rmode_seg(VCPU_SREG_CS
, &vmx
->rmode
.segs
[VCPU_SREG_CS
]);
4272 fix_rmode_seg(VCPU_SREG_ES
, &vmx
->rmode
.segs
[VCPU_SREG_ES
]);
4273 fix_rmode_seg(VCPU_SREG_DS
, &vmx
->rmode
.segs
[VCPU_SREG_DS
]);
4274 fix_rmode_seg(VCPU_SREG_GS
, &vmx
->rmode
.segs
[VCPU_SREG_GS
]);
4275 fix_rmode_seg(VCPU_SREG_FS
, &vmx
->rmode
.segs
[VCPU_SREG_FS
]);
4277 kvm_mmu_reset_context(vcpu
);
4280 static void vmx_set_efer(struct kvm_vcpu
*vcpu
, u64 efer
)
4282 struct vcpu_vmx
*vmx
= to_vmx(vcpu
);
4283 struct shared_msr_entry
*msr
= find_msr_entry(vmx
, MSR_EFER
);
4289 * Force kernel_gs_base reloading before EFER changes, as control
4290 * of this msr depends on is_long_mode().
4292 vmx_load_host_state(to_vmx(vcpu
));
4293 vcpu
->arch
.efer
= efer
;
4294 if (efer
& EFER_LMA
) {
4295 vm_entry_controls_setbit(to_vmx(vcpu
), VM_ENTRY_IA32E_MODE
);
4298 vm_entry_controls_clearbit(to_vmx(vcpu
), VM_ENTRY_IA32E_MODE
);
4300 msr
->data
= efer
& ~EFER_LME
;
4305 #ifdef CONFIG_X86_64
4307 static void enter_lmode(struct kvm_vcpu
*vcpu
)
4311 vmx_segment_cache_clear(to_vmx(vcpu
));
4313 guest_tr_ar
= vmcs_read32(GUEST_TR_AR_BYTES
);
4314 if ((guest_tr_ar
& VMX_AR_TYPE_MASK
) != VMX_AR_TYPE_BUSY_64_TSS
) {
4315 pr_debug_ratelimited("%s: tss fixup for long mode. \n",
4317 vmcs_write32(GUEST_TR_AR_BYTES
,
4318 (guest_tr_ar
& ~VMX_AR_TYPE_MASK
)
4319 | VMX_AR_TYPE_BUSY_64_TSS
);
4321 vmx_set_efer(vcpu
, vcpu
->arch
.efer
| EFER_LMA
);
4324 static void exit_lmode(struct kvm_vcpu
*vcpu
)
4326 vm_entry_controls_clearbit(to_vmx(vcpu
), VM_ENTRY_IA32E_MODE
);
4327 vmx_set_efer(vcpu
, vcpu
->arch
.efer
& ~EFER_LMA
);
4332 static inline void __vmx_flush_tlb(struct kvm_vcpu
*vcpu
, int vpid
)
4335 if (!VALID_PAGE(vcpu
->arch
.mmu
.root_hpa
))
4337 ept_sync_context(construct_eptp(vcpu
, vcpu
->arch
.mmu
.root_hpa
));
4339 vpid_sync_context(vpid
);
4343 static void vmx_flush_tlb(struct kvm_vcpu
*vcpu
)
4345 __vmx_flush_tlb(vcpu
, to_vmx(vcpu
)->vpid
);
4348 static void vmx_flush_tlb_ept_only(struct kvm_vcpu
*vcpu
)
4351 vmx_flush_tlb(vcpu
);
4354 static void vmx_decache_cr0_guest_bits(struct kvm_vcpu
*vcpu
)
4356 ulong cr0_guest_owned_bits
= vcpu
->arch
.cr0_guest_owned_bits
;
4358 vcpu
->arch
.cr0
&= ~cr0_guest_owned_bits
;
4359 vcpu
->arch
.cr0
|= vmcs_readl(GUEST_CR0
) & cr0_guest_owned_bits
;
4362 static void vmx_decache_cr3(struct kvm_vcpu
*vcpu
)
4364 if (enable_ept
&& is_paging(vcpu
))
4365 vcpu
->arch
.cr3
= vmcs_readl(GUEST_CR3
);
4366 __set_bit(VCPU_EXREG_CR3
, (ulong
*)&vcpu
->arch
.regs_avail
);
4369 static void vmx_decache_cr4_guest_bits(struct kvm_vcpu
*vcpu
)
4371 ulong cr4_guest_owned_bits
= vcpu
->arch
.cr4_guest_owned_bits
;
4373 vcpu
->arch
.cr4
&= ~cr4_guest_owned_bits
;
4374 vcpu
->arch
.cr4
|= vmcs_readl(GUEST_CR4
) & cr4_guest_owned_bits
;
4377 static void ept_load_pdptrs(struct kvm_vcpu
*vcpu
)
4379 struct kvm_mmu
*mmu
= vcpu
->arch
.walk_mmu
;
4381 if (!test_bit(VCPU_EXREG_PDPTR
,
4382 (unsigned long *)&vcpu
->arch
.regs_dirty
))
4385 if (is_paging(vcpu
) && is_pae(vcpu
) && !is_long_mode(vcpu
)) {
4386 vmcs_write64(GUEST_PDPTR0
, mmu
->pdptrs
[0]);
4387 vmcs_write64(GUEST_PDPTR1
, mmu
->pdptrs
[1]);
4388 vmcs_write64(GUEST_PDPTR2
, mmu
->pdptrs
[2]);
4389 vmcs_write64(GUEST_PDPTR3
, mmu
->pdptrs
[3]);
4393 static void ept_save_pdptrs(struct kvm_vcpu
*vcpu
)
4395 struct kvm_mmu
*mmu
= vcpu
->arch
.walk_mmu
;
4397 if (is_paging(vcpu
) && is_pae(vcpu
) && !is_long_mode(vcpu
)) {
4398 mmu
->pdptrs
[0] = vmcs_read64(GUEST_PDPTR0
);
4399 mmu
->pdptrs
[1] = vmcs_read64(GUEST_PDPTR1
);
4400 mmu
->pdptrs
[2] = vmcs_read64(GUEST_PDPTR2
);
4401 mmu
->pdptrs
[3] = vmcs_read64(GUEST_PDPTR3
);
4404 __set_bit(VCPU_EXREG_PDPTR
,
4405 (unsigned long *)&vcpu
->arch
.regs_avail
);
4406 __set_bit(VCPU_EXREG_PDPTR
,
4407 (unsigned long *)&vcpu
->arch
.regs_dirty
);
4410 static bool nested_guest_cr0_valid(struct kvm_vcpu
*vcpu
, unsigned long val
)
4412 u64 fixed0
= to_vmx(vcpu
)->nested
.nested_vmx_cr0_fixed0
;
4413 u64 fixed1
= to_vmx(vcpu
)->nested
.nested_vmx_cr0_fixed1
;
4414 struct vmcs12
*vmcs12
= get_vmcs12(vcpu
);
4416 if (to_vmx(vcpu
)->nested
.nested_vmx_secondary_ctls_high
&
4417 SECONDARY_EXEC_UNRESTRICTED_GUEST
&&
4418 nested_cpu_has2(vmcs12
, SECONDARY_EXEC_UNRESTRICTED_GUEST
))
4419 fixed0
&= ~(X86_CR0_PE
| X86_CR0_PG
);
4421 return fixed_bits_valid(val
, fixed0
, fixed1
);
4424 static bool nested_host_cr0_valid(struct kvm_vcpu
*vcpu
, unsigned long val
)
4426 u64 fixed0
= to_vmx(vcpu
)->nested
.nested_vmx_cr0_fixed0
;
4427 u64 fixed1
= to_vmx(vcpu
)->nested
.nested_vmx_cr0_fixed1
;
4429 return fixed_bits_valid(val
, fixed0
, fixed1
);
4432 static bool nested_cr4_valid(struct kvm_vcpu
*vcpu
, unsigned long val
)
4434 u64 fixed0
= to_vmx(vcpu
)->nested
.nested_vmx_cr4_fixed0
;
4435 u64 fixed1
= to_vmx(vcpu
)->nested
.nested_vmx_cr4_fixed1
;
4437 return fixed_bits_valid(val
, fixed0
, fixed1
);
4440 /* No difference in the restrictions on guest and host CR4 in VMX operation. */
4441 #define nested_guest_cr4_valid nested_cr4_valid
4442 #define nested_host_cr4_valid nested_cr4_valid
4444 static int vmx_set_cr4(struct kvm_vcpu
*vcpu
, unsigned long cr4
);
4446 static void ept_update_paging_mode_cr0(unsigned long *hw_cr0
,
4448 struct kvm_vcpu
*vcpu
)
4450 if (!test_bit(VCPU_EXREG_CR3
, (ulong
*)&vcpu
->arch
.regs_avail
))
4451 vmx_decache_cr3(vcpu
);
4452 if (!(cr0
& X86_CR0_PG
)) {
4453 /* From paging/starting to nonpaging */
4454 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL
,
4455 vmcs_read32(CPU_BASED_VM_EXEC_CONTROL
) |
4456 (CPU_BASED_CR3_LOAD_EXITING
|
4457 CPU_BASED_CR3_STORE_EXITING
));
4458 vcpu
->arch
.cr0
= cr0
;
4459 vmx_set_cr4(vcpu
, kvm_read_cr4(vcpu
));
4460 } else if (!is_paging(vcpu
)) {
4461 /* From nonpaging to paging */
4462 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL
,
4463 vmcs_read32(CPU_BASED_VM_EXEC_CONTROL
) &
4464 ~(CPU_BASED_CR3_LOAD_EXITING
|
4465 CPU_BASED_CR3_STORE_EXITING
));
4466 vcpu
->arch
.cr0
= cr0
;
4467 vmx_set_cr4(vcpu
, kvm_read_cr4(vcpu
));
4470 if (!(cr0
& X86_CR0_WP
))
4471 *hw_cr0
&= ~X86_CR0_WP
;
4474 static void vmx_set_cr0(struct kvm_vcpu
*vcpu
, unsigned long cr0
)
4476 struct vcpu_vmx
*vmx
= to_vmx(vcpu
);
4477 unsigned long hw_cr0
;
4479 hw_cr0
= (cr0
& ~KVM_GUEST_CR0_MASK
);
4480 if (enable_unrestricted_guest
)
4481 hw_cr0
|= KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST
;
4483 hw_cr0
|= KVM_VM_CR0_ALWAYS_ON
;
4485 if (vmx
->rmode
.vm86_active
&& (cr0
& X86_CR0_PE
))
4488 if (!vmx
->rmode
.vm86_active
&& !(cr0
& X86_CR0_PE
))
4492 #ifdef CONFIG_X86_64
4493 if (vcpu
->arch
.efer
& EFER_LME
) {
4494 if (!is_paging(vcpu
) && (cr0
& X86_CR0_PG
))
4496 if (is_paging(vcpu
) && !(cr0
& X86_CR0_PG
))
4502 ept_update_paging_mode_cr0(&hw_cr0
, cr0
, vcpu
);
4504 vmcs_writel(CR0_READ_SHADOW
, cr0
);
4505 vmcs_writel(GUEST_CR0
, hw_cr0
);
4506 vcpu
->arch
.cr0
= cr0
;
4508 /* depends on vcpu->arch.cr0 to be set to a new value */
4509 vmx
->emulation_required
= emulation_required(vcpu
);
4512 static int get_ept_level(struct kvm_vcpu
*vcpu
)
4514 if (cpu_has_vmx_ept_5levels() && (cpuid_maxphyaddr(vcpu
) > 48))
4519 static u64
construct_eptp(struct kvm_vcpu
*vcpu
, unsigned long root_hpa
)
4521 u64 eptp
= VMX_EPTP_MT_WB
;
4523 eptp
|= (get_ept_level(vcpu
) == 5) ? VMX_EPTP_PWL_5
: VMX_EPTP_PWL_4
;
4525 if (enable_ept_ad_bits
&&
4526 (!is_guest_mode(vcpu
) || nested_ept_ad_enabled(vcpu
)))
4527 eptp
|= VMX_EPTP_AD_ENABLE_BIT
;
4528 eptp
|= (root_hpa
& PAGE_MASK
);
4533 static void vmx_set_cr3(struct kvm_vcpu
*vcpu
, unsigned long cr3
)
4535 unsigned long guest_cr3
;
4540 eptp
= construct_eptp(vcpu
, cr3
);
4541 vmcs_write64(EPT_POINTER
, eptp
);
4542 if (is_paging(vcpu
) || is_guest_mode(vcpu
))
4543 guest_cr3
= kvm_read_cr3(vcpu
);
4545 guest_cr3
= vcpu
->kvm
->arch
.ept_identity_map_addr
;
4546 ept_load_pdptrs(vcpu
);
4549 vmx_flush_tlb(vcpu
);
4550 vmcs_writel(GUEST_CR3
, guest_cr3
);
4553 static int vmx_set_cr4(struct kvm_vcpu
*vcpu
, unsigned long cr4
)
4556 * Pass through host's Machine Check Enable value to hw_cr4, which
4557 * is in force while we are in guest mode. Do not let guests control
4558 * this bit, even if host CR4.MCE == 0.
4560 unsigned long hw_cr4
=
4561 (cr4_read_shadow() & X86_CR4_MCE
) |
4562 (cr4
& ~X86_CR4_MCE
) |
4563 (to_vmx(vcpu
)->rmode
.vm86_active
?
4564 KVM_RMODE_VM_CR4_ALWAYS_ON
: KVM_PMODE_VM_CR4_ALWAYS_ON
);
4566 if (cr4
& X86_CR4_VMXE
) {
4568 * To use VMXON (and later other VMX instructions), a guest
4569 * must first be able to turn on cr4.VMXE (see handle_vmon()).
4570 * So basically the check on whether to allow nested VMX
4573 if (!nested_vmx_allowed(vcpu
))
4577 if (to_vmx(vcpu
)->nested
.vmxon
&& !nested_cr4_valid(vcpu
, cr4
))
4580 vcpu
->arch
.cr4
= cr4
;
4582 if (!is_paging(vcpu
)) {
4583 hw_cr4
&= ~X86_CR4_PAE
;
4584 hw_cr4
|= X86_CR4_PSE
;
4585 } else if (!(cr4
& X86_CR4_PAE
)) {
4586 hw_cr4
&= ~X86_CR4_PAE
;
4590 if (!enable_unrestricted_guest
&& !is_paging(vcpu
))
4592 * SMEP/SMAP/PKU is disabled if CPU is in non-paging mode in
4593 * hardware. To emulate this behavior, SMEP/SMAP/PKU needs
4594 * to be manually disabled when guest switches to non-paging
4597 * If !enable_unrestricted_guest, the CPU is always running
4598 * with CR0.PG=1 and CR4 needs to be modified.
4599 * If enable_unrestricted_guest, the CPU automatically
4600 * disables SMEP/SMAP/PKU when the guest sets CR0.PG=0.
4602 hw_cr4
&= ~(X86_CR4_SMEP
| X86_CR4_SMAP
| X86_CR4_PKE
);
4604 vmcs_writel(CR4_READ_SHADOW
, cr4
);
4605 vmcs_writel(GUEST_CR4
, hw_cr4
);
4609 static void vmx_get_segment(struct kvm_vcpu
*vcpu
,
4610 struct kvm_segment
*var
, int seg
)
4612 struct vcpu_vmx
*vmx
= to_vmx(vcpu
);
4615 if (vmx
->rmode
.vm86_active
&& seg
!= VCPU_SREG_LDTR
) {
4616 *var
= vmx
->rmode
.segs
[seg
];
4617 if (seg
== VCPU_SREG_TR
4618 || var
->selector
== vmx_read_guest_seg_selector(vmx
, seg
))
4620 var
->base
= vmx_read_guest_seg_base(vmx
, seg
);
4621 var
->selector
= vmx_read_guest_seg_selector(vmx
, seg
);
4624 var
->base
= vmx_read_guest_seg_base(vmx
, seg
);
4625 var
->limit
= vmx_read_guest_seg_limit(vmx
, seg
);
4626 var
->selector
= vmx_read_guest_seg_selector(vmx
, seg
);
4627 ar
= vmx_read_guest_seg_ar(vmx
, seg
);
4628 var
->unusable
= (ar
>> 16) & 1;
4629 var
->type
= ar
& 15;
4630 var
->s
= (ar
>> 4) & 1;
4631 var
->dpl
= (ar
>> 5) & 3;
4633 * Some userspaces do not preserve unusable property. Since usable
4634 * segment has to be present according to VMX spec we can use present
4635 * property to amend userspace bug by making unusable segment always
4636 * nonpresent. vmx_segment_access_rights() already marks nonpresent
4637 * segment as unusable.
4639 var
->present
= !var
->unusable
;
4640 var
->avl
= (ar
>> 12) & 1;
4641 var
->l
= (ar
>> 13) & 1;
4642 var
->db
= (ar
>> 14) & 1;
4643 var
->g
= (ar
>> 15) & 1;
4646 static u64
vmx_get_segment_base(struct kvm_vcpu
*vcpu
, int seg
)
4648 struct kvm_segment s
;
4650 if (to_vmx(vcpu
)->rmode
.vm86_active
) {
4651 vmx_get_segment(vcpu
, &s
, seg
);
4654 return vmx_read_guest_seg_base(to_vmx(vcpu
), seg
);
4657 static int vmx_get_cpl(struct kvm_vcpu
*vcpu
)
4659 struct vcpu_vmx
*vmx
= to_vmx(vcpu
);
4661 if (unlikely(vmx
->rmode
.vm86_active
))
4664 int ar
= vmx_read_guest_seg_ar(vmx
, VCPU_SREG_SS
);
4665 return VMX_AR_DPL(ar
);
4669 static u32
vmx_segment_access_rights(struct kvm_segment
*var
)
4673 if (var
->unusable
|| !var
->present
)
4676 ar
= var
->type
& 15;
4677 ar
|= (var
->s
& 1) << 4;
4678 ar
|= (var
->dpl
& 3) << 5;
4679 ar
|= (var
->present
& 1) << 7;
4680 ar
|= (var
->avl
& 1) << 12;
4681 ar
|= (var
->l
& 1) << 13;
4682 ar
|= (var
->db
& 1) << 14;
4683 ar
|= (var
->g
& 1) << 15;
4689 static void vmx_set_segment(struct kvm_vcpu
*vcpu
,
4690 struct kvm_segment
*var
, int seg
)
4692 struct vcpu_vmx
*vmx
= to_vmx(vcpu
);
4693 const struct kvm_vmx_segment_field
*sf
= &kvm_vmx_segment_fields
[seg
];
4695 vmx_segment_cache_clear(vmx
);
4697 if (vmx
->rmode
.vm86_active
&& seg
!= VCPU_SREG_LDTR
) {
4698 vmx
->rmode
.segs
[seg
] = *var
;
4699 if (seg
== VCPU_SREG_TR
)
4700 vmcs_write16(sf
->selector
, var
->selector
);
4702 fix_rmode_seg(seg
, &vmx
->rmode
.segs
[seg
]);
4706 vmcs_writel(sf
->base
, var
->base
);
4707 vmcs_write32(sf
->limit
, var
->limit
);
4708 vmcs_write16(sf
->selector
, var
->selector
);
4711 * Fix the "Accessed" bit in AR field of segment registers for older
4713 * IA32 arch specifies that at the time of processor reset the
4714 * "Accessed" bit in the AR field of segment registers is 1. And qemu
4715 * is setting it to 0 in the userland code. This causes invalid guest
4716 * state vmexit when "unrestricted guest" mode is turned on.
4717 * Fix for this setup issue in cpu_reset is being pushed in the qemu
4718 * tree. Newer qemu binaries with that qemu fix would not need this
4721 if (enable_unrestricted_guest
&& (seg
!= VCPU_SREG_LDTR
))
4722 var
->type
|= 0x1; /* Accessed */
4724 vmcs_write32(sf
->ar_bytes
, vmx_segment_access_rights(var
));
4727 vmx
->emulation_required
= emulation_required(vcpu
);
4730 static void vmx_get_cs_db_l_bits(struct kvm_vcpu
*vcpu
, int *db
, int *l
)
4732 u32 ar
= vmx_read_guest_seg_ar(to_vmx(vcpu
), VCPU_SREG_CS
);
4734 *db
= (ar
>> 14) & 1;
4735 *l
= (ar
>> 13) & 1;
4738 static void vmx_get_idt(struct kvm_vcpu
*vcpu
, struct desc_ptr
*dt
)
4740 dt
->size
= vmcs_read32(GUEST_IDTR_LIMIT
);
4741 dt
->address
= vmcs_readl(GUEST_IDTR_BASE
);
4744 static void vmx_set_idt(struct kvm_vcpu
*vcpu
, struct desc_ptr
*dt
)
4746 vmcs_write32(GUEST_IDTR_LIMIT
, dt
->size
);
4747 vmcs_writel(GUEST_IDTR_BASE
, dt
->address
);
4750 static void vmx_get_gdt(struct kvm_vcpu
*vcpu
, struct desc_ptr
*dt
)
4752 dt
->size
= vmcs_read32(GUEST_GDTR_LIMIT
);
4753 dt
->address
= vmcs_readl(GUEST_GDTR_BASE
);
4756 static void vmx_set_gdt(struct kvm_vcpu
*vcpu
, struct desc_ptr
*dt
)
4758 vmcs_write32(GUEST_GDTR_LIMIT
, dt
->size
);
4759 vmcs_writel(GUEST_GDTR_BASE
, dt
->address
);
4762 static bool rmode_segment_valid(struct kvm_vcpu
*vcpu
, int seg
)
4764 struct kvm_segment var
;
4767 vmx_get_segment(vcpu
, &var
, seg
);
4769 if (seg
== VCPU_SREG_CS
)
4771 ar
= vmx_segment_access_rights(&var
);
4773 if (var
.base
!= (var
.selector
<< 4))
4775 if (var
.limit
!= 0xffff)
4783 static bool code_segment_valid(struct kvm_vcpu
*vcpu
)
4785 struct kvm_segment cs
;
4786 unsigned int cs_rpl
;
4788 vmx_get_segment(vcpu
, &cs
, VCPU_SREG_CS
);
4789 cs_rpl
= cs
.selector
& SEGMENT_RPL_MASK
;
4793 if (~cs
.type
& (VMX_AR_TYPE_CODE_MASK
|VMX_AR_TYPE_ACCESSES_MASK
))
4797 if (cs
.type
& VMX_AR_TYPE_WRITEABLE_MASK
) {
4798 if (cs
.dpl
> cs_rpl
)
4801 if (cs
.dpl
!= cs_rpl
)
4807 /* TODO: Add Reserved field check, this'll require a new member in the kvm_segment_field structure */
4811 static bool stack_segment_valid(struct kvm_vcpu
*vcpu
)
4813 struct kvm_segment ss
;
4814 unsigned int ss_rpl
;
4816 vmx_get_segment(vcpu
, &ss
, VCPU_SREG_SS
);
4817 ss_rpl
= ss
.selector
& SEGMENT_RPL_MASK
;
4821 if (ss
.type
!= 3 && ss
.type
!= 7)
4825 if (ss
.dpl
!= ss_rpl
) /* DPL != RPL */
4833 static bool data_segment_valid(struct kvm_vcpu
*vcpu
, int seg
)
4835 struct kvm_segment var
;
4838 vmx_get_segment(vcpu
, &var
, seg
);
4839 rpl
= var
.selector
& SEGMENT_RPL_MASK
;
4847 if (~var
.type
& (VMX_AR_TYPE_CODE_MASK
|VMX_AR_TYPE_WRITEABLE_MASK
)) {
4848 if (var
.dpl
< rpl
) /* DPL < RPL */
4852 /* TODO: Add other members to kvm_segment_field to allow checking for other access
4858 static bool tr_valid(struct kvm_vcpu
*vcpu
)
4860 struct kvm_segment tr
;
4862 vmx_get_segment(vcpu
, &tr
, VCPU_SREG_TR
);
4866 if (tr
.selector
& SEGMENT_TI_MASK
) /* TI = 1 */
4868 if (tr
.type
!= 3 && tr
.type
!= 11) /* TODO: Check if guest is in IA32e mode */
4876 static bool ldtr_valid(struct kvm_vcpu
*vcpu
)
4878 struct kvm_segment ldtr
;
4880 vmx_get_segment(vcpu
, &ldtr
, VCPU_SREG_LDTR
);
4884 if (ldtr
.selector
& SEGMENT_TI_MASK
) /* TI = 1 */
4894 static bool cs_ss_rpl_check(struct kvm_vcpu
*vcpu
)
4896 struct kvm_segment cs
, ss
;
4898 vmx_get_segment(vcpu
, &cs
, VCPU_SREG_CS
);
4899 vmx_get_segment(vcpu
, &ss
, VCPU_SREG_SS
);
4901 return ((cs
.selector
& SEGMENT_RPL_MASK
) ==
4902 (ss
.selector
& SEGMENT_RPL_MASK
));
4906 * Check if guest state is valid. Returns true if valid, false if
4908 * We assume that registers are always usable
4910 static bool guest_state_valid(struct kvm_vcpu
*vcpu
)
4912 if (enable_unrestricted_guest
)
4915 /* real mode guest state checks */
4916 if (!is_protmode(vcpu
) || (vmx_get_rflags(vcpu
) & X86_EFLAGS_VM
)) {
4917 if (!rmode_segment_valid(vcpu
, VCPU_SREG_CS
))
4919 if (!rmode_segment_valid(vcpu
, VCPU_SREG_SS
))
4921 if (!rmode_segment_valid(vcpu
, VCPU_SREG_DS
))
4923 if (!rmode_segment_valid(vcpu
, VCPU_SREG_ES
))
4925 if (!rmode_segment_valid(vcpu
, VCPU_SREG_FS
))
4927 if (!rmode_segment_valid(vcpu
, VCPU_SREG_GS
))
4930 /* protected mode guest state checks */
4931 if (!cs_ss_rpl_check(vcpu
))
4933 if (!code_segment_valid(vcpu
))
4935 if (!stack_segment_valid(vcpu
))
4937 if (!data_segment_valid(vcpu
, VCPU_SREG_DS
))
4939 if (!data_segment_valid(vcpu
, VCPU_SREG_ES
))
4941 if (!data_segment_valid(vcpu
, VCPU_SREG_FS
))
4943 if (!data_segment_valid(vcpu
, VCPU_SREG_GS
))
4945 if (!tr_valid(vcpu
))
4947 if (!ldtr_valid(vcpu
))
4951 * - Add checks on RIP
4952 * - Add checks on RFLAGS
4958 static bool page_address_valid(struct kvm_vcpu
*vcpu
, gpa_t gpa
)
4960 return PAGE_ALIGNED(gpa
) && !(gpa
>> cpuid_maxphyaddr(vcpu
));
4963 static int init_rmode_tss(struct kvm
*kvm
)
4969 idx
= srcu_read_lock(&kvm
->srcu
);
4970 fn
= kvm
->arch
.tss_addr
>> PAGE_SHIFT
;
4971 r
= kvm_clear_guest_page(kvm
, fn
, 0, PAGE_SIZE
);
4974 data
= TSS_BASE_SIZE
+ TSS_REDIRECTION_SIZE
;
4975 r
= kvm_write_guest_page(kvm
, fn
++, &data
,
4976 TSS_IOPB_BASE_OFFSET
, sizeof(u16
));
4979 r
= kvm_clear_guest_page(kvm
, fn
++, 0, PAGE_SIZE
);
4982 r
= kvm_clear_guest_page(kvm
, fn
, 0, PAGE_SIZE
);
4986 r
= kvm_write_guest_page(kvm
, fn
, &data
,
4987 RMODE_TSS_SIZE
- 2 * PAGE_SIZE
- 1,
4990 srcu_read_unlock(&kvm
->srcu
, idx
);
4994 static int init_rmode_identity_map(struct kvm
*kvm
)
4997 kvm_pfn_t identity_map_pfn
;
5000 /* Protect kvm->arch.ept_identity_pagetable_done. */
5001 mutex_lock(&kvm
->slots_lock
);
5003 if (likely(kvm
->arch
.ept_identity_pagetable_done
))
5006 if (!kvm
->arch
.ept_identity_map_addr
)
5007 kvm
->arch
.ept_identity_map_addr
= VMX_EPT_IDENTITY_PAGETABLE_ADDR
;
5008 identity_map_pfn
= kvm
->arch
.ept_identity_map_addr
>> PAGE_SHIFT
;
5010 r
= __x86_set_memory_region(kvm
, IDENTITY_PAGETABLE_PRIVATE_MEMSLOT
,
5011 kvm
->arch
.ept_identity_map_addr
, PAGE_SIZE
);
5015 idx
= srcu_read_lock(&kvm
->srcu
);
5016 r
= kvm_clear_guest_page(kvm
, identity_map_pfn
, 0, PAGE_SIZE
);
5019 /* Set up identity-mapping pagetable for EPT in real mode */
5020 for (i
= 0; i
< PT32_ENT_PER_PAGE
; i
++) {
5021 tmp
= (i
<< 22) + (_PAGE_PRESENT
| _PAGE_RW
| _PAGE_USER
|
5022 _PAGE_ACCESSED
| _PAGE_DIRTY
| _PAGE_PSE
);
5023 r
= kvm_write_guest_page(kvm
, identity_map_pfn
,
5024 &tmp
, i
* sizeof(tmp
), sizeof(tmp
));
5028 kvm
->arch
.ept_identity_pagetable_done
= true;
5031 srcu_read_unlock(&kvm
->srcu
, idx
);
5034 mutex_unlock(&kvm
->slots_lock
);
5038 static void seg_setup(int seg
)
5040 const struct kvm_vmx_segment_field
*sf
= &kvm_vmx_segment_fields
[seg
];
5043 vmcs_write16(sf
->selector
, 0);
5044 vmcs_writel(sf
->base
, 0);
5045 vmcs_write32(sf
->limit
, 0xffff);
5047 if (seg
== VCPU_SREG_CS
)
5048 ar
|= 0x08; /* code segment */
5050 vmcs_write32(sf
->ar_bytes
, ar
);
5053 static int alloc_apic_access_page(struct kvm
*kvm
)
5058 mutex_lock(&kvm
->slots_lock
);
5059 if (kvm
->arch
.apic_access_page_done
)
5061 r
= __x86_set_memory_region(kvm
, APIC_ACCESS_PAGE_PRIVATE_MEMSLOT
,
5062 APIC_DEFAULT_PHYS_BASE
, PAGE_SIZE
);
5066 page
= gfn_to_page(kvm
, APIC_DEFAULT_PHYS_BASE
>> PAGE_SHIFT
);
5067 if (is_error_page(page
)) {
5073 * Do not pin the page in memory, so that memory hot-unplug
5074 * is able to migrate it.
5077 kvm
->arch
.apic_access_page_done
= true;
5079 mutex_unlock(&kvm
->slots_lock
);
5083 static int allocate_vpid(void)
5089 spin_lock(&vmx_vpid_lock
);
5090 vpid
= find_first_zero_bit(vmx_vpid_bitmap
, VMX_NR_VPIDS
);
5091 if (vpid
< VMX_NR_VPIDS
)
5092 __set_bit(vpid
, vmx_vpid_bitmap
);
5095 spin_unlock(&vmx_vpid_lock
);
5099 static void free_vpid(int vpid
)
5101 if (!enable_vpid
|| vpid
== 0)
5103 spin_lock(&vmx_vpid_lock
);
5104 __clear_bit(vpid
, vmx_vpid_bitmap
);
5105 spin_unlock(&vmx_vpid_lock
);
5108 static void __always_inline
vmx_disable_intercept_for_msr(unsigned long *msr_bitmap
,
5111 int f
= sizeof(unsigned long);
5113 if (!cpu_has_vmx_msr_bitmap())
5117 * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
5118 * have the write-low and read-high bitmap offsets the wrong way round.
5119 * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
5121 if (msr
<= 0x1fff) {
5122 if (type
& MSR_TYPE_R
)
5124 __clear_bit(msr
, msr_bitmap
+ 0x000 / f
);
5126 if (type
& MSR_TYPE_W
)
5128 __clear_bit(msr
, msr_bitmap
+ 0x800 / f
);
5130 } else if ((msr
>= 0xc0000000) && (msr
<= 0xc0001fff)) {
5132 if (type
& MSR_TYPE_R
)
5134 __clear_bit(msr
, msr_bitmap
+ 0x400 / f
);
5136 if (type
& MSR_TYPE_W
)
5138 __clear_bit(msr
, msr_bitmap
+ 0xc00 / f
);
5143 static void __always_inline
vmx_enable_intercept_for_msr(unsigned long *msr_bitmap
,
5146 int f
= sizeof(unsigned long);
5148 if (!cpu_has_vmx_msr_bitmap())
5152 * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
5153 * have the write-low and read-high bitmap offsets the wrong way round.
5154 * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
5156 if (msr
<= 0x1fff) {
5157 if (type
& MSR_TYPE_R
)
5159 __set_bit(msr
, msr_bitmap
+ 0x000 / f
);
5161 if (type
& MSR_TYPE_W
)
5163 __set_bit(msr
, msr_bitmap
+ 0x800 / f
);
5165 } else if ((msr
>= 0xc0000000) && (msr
<= 0xc0001fff)) {
5167 if (type
& MSR_TYPE_R
)
5169 __set_bit(msr
, msr_bitmap
+ 0x400 / f
);
5171 if (type
& MSR_TYPE_W
)
5173 __set_bit(msr
, msr_bitmap
+ 0xc00 / f
);
5178 static void __always_inline
vmx_set_intercept_for_msr(unsigned long *msr_bitmap
,
5179 u32 msr
, int type
, bool value
)
5182 vmx_enable_intercept_for_msr(msr_bitmap
, msr
, type
);
5184 vmx_disable_intercept_for_msr(msr_bitmap
, msr
, type
);
5188 * If a msr is allowed by L0, we should check whether it is allowed by L1.
5189 * The corresponding bit will be cleared unless both of L0 and L1 allow it.
5191 static void nested_vmx_disable_intercept_for_msr(unsigned long *msr_bitmap_l1
,
5192 unsigned long *msr_bitmap_nested
,
5195 int f
= sizeof(unsigned long);
5197 if (!cpu_has_vmx_msr_bitmap()) {
5203 * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
5204 * have the write-low and read-high bitmap offsets the wrong way round.
5205 * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
5207 if (msr
<= 0x1fff) {
5208 if (type
& MSR_TYPE_R
&&
5209 !test_bit(msr
, msr_bitmap_l1
+ 0x000 / f
))
5211 __clear_bit(msr
, msr_bitmap_nested
+ 0x000 / f
);
5213 if (type
& MSR_TYPE_W
&&
5214 !test_bit(msr
, msr_bitmap_l1
+ 0x800 / f
))
5216 __clear_bit(msr
, msr_bitmap_nested
+ 0x800 / f
);
5218 } else if ((msr
>= 0xc0000000) && (msr
<= 0xc0001fff)) {
5220 if (type
& MSR_TYPE_R
&&
5221 !test_bit(msr
, msr_bitmap_l1
+ 0x400 / f
))
5223 __clear_bit(msr
, msr_bitmap_nested
+ 0x400 / f
);
5225 if (type
& MSR_TYPE_W
&&
5226 !test_bit(msr
, msr_bitmap_l1
+ 0xc00 / f
))
5228 __clear_bit(msr
, msr_bitmap_nested
+ 0xc00 / f
);
5233 static u8
vmx_msr_bitmap_mode(struct kvm_vcpu
*vcpu
)
5237 if (cpu_has_secondary_exec_ctrls() &&
5238 (vmcs_read32(SECONDARY_VM_EXEC_CONTROL
) &
5239 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE
)) {
5240 mode
|= MSR_BITMAP_MODE_X2APIC
;
5241 if (enable_apicv
&& kvm_vcpu_apicv_active(vcpu
))
5242 mode
|= MSR_BITMAP_MODE_X2APIC_APICV
;
5245 if (is_long_mode(vcpu
))
5246 mode
|= MSR_BITMAP_MODE_LM
;
5251 #define X2APIC_MSR(r) (APIC_BASE_MSR + ((r) >> 4))
5253 static void vmx_update_msr_bitmap_x2apic(unsigned long *msr_bitmap
,
5258 for (msr
= 0x800; msr
<= 0x8ff; msr
+= BITS_PER_LONG
) {
5259 unsigned word
= msr
/ BITS_PER_LONG
;
5260 msr_bitmap
[word
] = (mode
& MSR_BITMAP_MODE_X2APIC_APICV
) ? 0 : ~0;
5261 msr_bitmap
[word
+ (0x800 / sizeof(long))] = ~0;
5264 if (mode
& MSR_BITMAP_MODE_X2APIC
) {
5266 * TPR reads and writes can be virtualized even if virtual interrupt
5267 * delivery is not in use.
5269 vmx_disable_intercept_for_msr(msr_bitmap
, X2APIC_MSR(APIC_TASKPRI
), MSR_TYPE_RW
);
5270 if (mode
& MSR_BITMAP_MODE_X2APIC_APICV
) {
5271 vmx_enable_intercept_for_msr(msr_bitmap
, X2APIC_MSR(APIC_TMCCT
), MSR_TYPE_R
);
5272 vmx_disable_intercept_for_msr(msr_bitmap
, X2APIC_MSR(APIC_EOI
), MSR_TYPE_W
);
5273 vmx_disable_intercept_for_msr(msr_bitmap
, X2APIC_MSR(APIC_SELF_IPI
), MSR_TYPE_W
);
5278 static void vmx_update_msr_bitmap(struct kvm_vcpu
*vcpu
)
5280 struct vcpu_vmx
*vmx
= to_vmx(vcpu
);
5281 unsigned long *msr_bitmap
= vmx
->vmcs01
.msr_bitmap
;
5282 u8 mode
= vmx_msr_bitmap_mode(vcpu
);
5283 u8 changed
= mode
^ vmx
->msr_bitmap_mode
;
5288 vmx_set_intercept_for_msr(msr_bitmap
, MSR_KERNEL_GS_BASE
, MSR_TYPE_RW
,
5289 !(mode
& MSR_BITMAP_MODE_LM
));
5291 if (changed
& (MSR_BITMAP_MODE_X2APIC
| MSR_BITMAP_MODE_X2APIC_APICV
))
5292 vmx_update_msr_bitmap_x2apic(msr_bitmap
, mode
);
5294 vmx
->msr_bitmap_mode
= mode
;
5297 static bool vmx_get_enable_apicv(struct kvm_vcpu
*vcpu
)
5299 return enable_apicv
;
5302 static void nested_mark_vmcs12_pages_dirty(struct kvm_vcpu
*vcpu
)
5304 struct vmcs12
*vmcs12
= get_vmcs12(vcpu
);
5308 * Don't need to mark the APIC access page dirty; it is never
5309 * written to by the CPU during APIC virtualization.
5312 if (nested_cpu_has(vmcs12
, CPU_BASED_TPR_SHADOW
)) {
5313 gfn
= vmcs12
->virtual_apic_page_addr
>> PAGE_SHIFT
;
5314 kvm_vcpu_mark_page_dirty(vcpu
, gfn
);
5317 if (nested_cpu_has_posted_intr(vmcs12
)) {
5318 gfn
= vmcs12
->posted_intr_desc_addr
>> PAGE_SHIFT
;
5319 kvm_vcpu_mark_page_dirty(vcpu
, gfn
);
5324 static void vmx_complete_nested_posted_interrupt(struct kvm_vcpu
*vcpu
)
5326 struct vcpu_vmx
*vmx
= to_vmx(vcpu
);
5331 if (!vmx
->nested
.pi_desc
|| !vmx
->nested
.pi_pending
)
5334 vmx
->nested
.pi_pending
= false;
5335 if (!pi_test_and_clear_on(vmx
->nested
.pi_desc
))
5338 max_irr
= find_last_bit((unsigned long *)vmx
->nested
.pi_desc
->pir
, 256);
5339 if (max_irr
!= 256) {
5340 vapic_page
= kmap(vmx
->nested
.virtual_apic_page
);
5341 __kvm_apic_update_irr(vmx
->nested
.pi_desc
->pir
, vapic_page
);
5342 kunmap(vmx
->nested
.virtual_apic_page
);
5344 status
= vmcs_read16(GUEST_INTR_STATUS
);
5345 if ((u8
)max_irr
> ((u8
)status
& 0xff)) {
5347 status
|= (u8
)max_irr
;
5348 vmcs_write16(GUEST_INTR_STATUS
, status
);
5352 nested_mark_vmcs12_pages_dirty(vcpu
);
5355 static inline bool kvm_vcpu_trigger_posted_interrupt(struct kvm_vcpu
*vcpu
,
5359 int pi_vec
= nested
? POSTED_INTR_NESTED_VECTOR
: POSTED_INTR_VECTOR
;
5361 if (vcpu
->mode
== IN_GUEST_MODE
) {
5363 * The vector of interrupt to be delivered to vcpu had
5364 * been set in PIR before this function.
5366 * Following cases will be reached in this block, and
5367 * we always send a notification event in all cases as
5370 * Case 1: vcpu keeps in non-root mode. Sending a
5371 * notification event posts the interrupt to vcpu.
5373 * Case 2: vcpu exits to root mode and is still
5374 * runnable. PIR will be synced to vIRR before the
5375 * next vcpu entry. Sending a notification event in
5376 * this case has no effect, as vcpu is not in root
5379 * Case 3: vcpu exits to root mode and is blocked.
5380 * vcpu_block() has already synced PIR to vIRR and
5381 * never blocks vcpu if vIRR is not cleared. Therefore,
5382 * a blocked vcpu here does not wait for any requested
5383 * interrupts in PIR, and sending a notification event
5384 * which has no effect is safe here.
5387 apic
->send_IPI_mask(get_cpu_mask(vcpu
->cpu
), pi_vec
);
5394 static int vmx_deliver_nested_posted_interrupt(struct kvm_vcpu
*vcpu
,
5397 struct vcpu_vmx
*vmx
= to_vmx(vcpu
);
5399 if (is_guest_mode(vcpu
) &&
5400 vector
== vmx
->nested
.posted_intr_nv
) {
5402 * If a posted intr is not recognized by hardware,
5403 * we will accomplish it in the next vmentry.
5405 vmx
->nested
.pi_pending
= true;
5406 kvm_make_request(KVM_REQ_EVENT
, vcpu
);
5407 /* the PIR and ON have been set by L1. */
5408 if (!kvm_vcpu_trigger_posted_interrupt(vcpu
, true))
5409 kvm_vcpu_kick(vcpu
);
5415 * Send interrupt to vcpu via posted interrupt way.
5416 * 1. If target vcpu is running(non-root mode), send posted interrupt
5417 * notification to vcpu and hardware will sync PIR to vIRR atomically.
5418 * 2. If target vcpu isn't running(root mode), kick it to pick up the
5419 * interrupt from PIR in next vmentry.
5421 static void vmx_deliver_posted_interrupt(struct kvm_vcpu
*vcpu
, int vector
)
5423 struct vcpu_vmx
*vmx
= to_vmx(vcpu
);
5426 r
= vmx_deliver_nested_posted_interrupt(vcpu
, vector
);
5430 if (pi_test_and_set_pir(vector
, &vmx
->pi_desc
))
5433 /* If a previous notification has sent the IPI, nothing to do. */
5434 if (pi_test_and_set_on(&vmx
->pi_desc
))
5437 if (!kvm_vcpu_trigger_posted_interrupt(vcpu
, false))
5438 kvm_vcpu_kick(vcpu
);
5442 * Set up the vmcs's constant host-state fields, i.e., host-state fields that
5443 * will not change in the lifetime of the guest.
5444 * Note that host-state that does change is set elsewhere. E.g., host-state
5445 * that is set differently for each CPU is set in vmx_vcpu_load(), not here.
5447 static void vmx_set_constant_host_state(struct vcpu_vmx
*vmx
)
5452 unsigned long cr0
, cr3
, cr4
;
5455 WARN_ON(cr0
& X86_CR0_TS
);
5456 vmcs_writel(HOST_CR0
, cr0
); /* 22.2.3 */
5459 * Save the most likely value for this task's CR3 in the VMCS.
5460 * We can't use __get_current_cr3_fast() because we're not atomic.
5463 vmcs_writel(HOST_CR3
, cr3
); /* 22.2.3 FIXME: shadow tables */
5464 vmx
->loaded_vmcs
->vmcs_host_cr3
= cr3
;
5466 /* Save the most likely value for this task's CR4 in the VMCS. */
5467 cr4
= cr4_read_shadow();
5468 vmcs_writel(HOST_CR4
, cr4
); /* 22.2.3, 22.2.5 */
5469 vmx
->loaded_vmcs
->vmcs_host_cr4
= cr4
;
5471 vmcs_write16(HOST_CS_SELECTOR
, __KERNEL_CS
); /* 22.2.4 */
5472 #ifdef CONFIG_X86_64
5474 * Load null selectors, so we can avoid reloading them in
5475 * __vmx_load_host_state(), in case userspace uses the null selectors
5476 * too (the expected case).
5478 vmcs_write16(HOST_DS_SELECTOR
, 0);
5479 vmcs_write16(HOST_ES_SELECTOR
, 0);
5481 vmcs_write16(HOST_DS_SELECTOR
, __KERNEL_DS
); /* 22.2.4 */
5482 vmcs_write16(HOST_ES_SELECTOR
, __KERNEL_DS
); /* 22.2.4 */
5484 vmcs_write16(HOST_SS_SELECTOR
, __KERNEL_DS
); /* 22.2.4 */
5485 vmcs_write16(HOST_TR_SELECTOR
, GDT_ENTRY_TSS
*8); /* 22.2.4 */
5488 vmcs_writel(HOST_IDTR_BASE
, dt
.address
); /* 22.2.4 */
5489 vmx
->host_idt_base
= dt
.address
;
5491 vmcs_writel(HOST_RIP
, vmx_return
); /* 22.2.5 */
5493 rdmsr(MSR_IA32_SYSENTER_CS
, low32
, high32
);
5494 vmcs_write32(HOST_IA32_SYSENTER_CS
, low32
);
5495 rdmsrl(MSR_IA32_SYSENTER_EIP
, tmpl
);
5496 vmcs_writel(HOST_IA32_SYSENTER_EIP
, tmpl
); /* 22.2.3 */
5498 if (vmcs_config
.vmexit_ctrl
& VM_EXIT_LOAD_IA32_PAT
) {
5499 rdmsr(MSR_IA32_CR_PAT
, low32
, high32
);
5500 vmcs_write64(HOST_IA32_PAT
, low32
| ((u64
) high32
<< 32));
5504 static void set_cr4_guest_host_mask(struct vcpu_vmx
*vmx
)
5506 vmx
->vcpu
.arch
.cr4_guest_owned_bits
= KVM_CR4_GUEST_OWNED_BITS
;
5508 vmx
->vcpu
.arch
.cr4_guest_owned_bits
|= X86_CR4_PGE
;
5509 if (is_guest_mode(&vmx
->vcpu
))
5510 vmx
->vcpu
.arch
.cr4_guest_owned_bits
&=
5511 ~get_vmcs12(&vmx
->vcpu
)->cr4_guest_host_mask
;
5512 vmcs_writel(CR4_GUEST_HOST_MASK
, ~vmx
->vcpu
.arch
.cr4_guest_owned_bits
);
5515 static u32
vmx_pin_based_exec_ctrl(struct vcpu_vmx
*vmx
)
5517 u32 pin_based_exec_ctrl
= vmcs_config
.pin_based_exec_ctrl
;
5519 if (!kvm_vcpu_apicv_active(&vmx
->vcpu
))
5520 pin_based_exec_ctrl
&= ~PIN_BASED_POSTED_INTR
;
5523 pin_based_exec_ctrl
&= ~PIN_BASED_VIRTUAL_NMIS
;
5525 /* Enable the preemption timer dynamically */
5526 pin_based_exec_ctrl
&= ~PIN_BASED_VMX_PREEMPTION_TIMER
;
5527 return pin_based_exec_ctrl
;
5530 static void vmx_refresh_apicv_exec_ctrl(struct kvm_vcpu
*vcpu
)
5532 struct vcpu_vmx
*vmx
= to_vmx(vcpu
);
5534 vmcs_write32(PIN_BASED_VM_EXEC_CONTROL
, vmx_pin_based_exec_ctrl(vmx
));
5535 if (cpu_has_secondary_exec_ctrls()) {
5536 if (kvm_vcpu_apicv_active(vcpu
))
5537 vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL
,
5538 SECONDARY_EXEC_APIC_REGISTER_VIRT
|
5539 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY
);
5541 vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL
,
5542 SECONDARY_EXEC_APIC_REGISTER_VIRT
|
5543 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY
);
5546 if (cpu_has_vmx_msr_bitmap())
5547 vmx_update_msr_bitmap(vcpu
);
5550 static u32
vmx_exec_control(struct vcpu_vmx
*vmx
)
5552 u32 exec_control
= vmcs_config
.cpu_based_exec_ctrl
;
5554 if (vmx
->vcpu
.arch
.switch_db_regs
& KVM_DEBUGREG_WONT_EXIT
)
5555 exec_control
&= ~CPU_BASED_MOV_DR_EXITING
;
5557 if (!cpu_need_tpr_shadow(&vmx
->vcpu
)) {
5558 exec_control
&= ~CPU_BASED_TPR_SHADOW
;
5559 #ifdef CONFIG_X86_64
5560 exec_control
|= CPU_BASED_CR8_STORE_EXITING
|
5561 CPU_BASED_CR8_LOAD_EXITING
;
5565 exec_control
|= CPU_BASED_CR3_STORE_EXITING
|
5566 CPU_BASED_CR3_LOAD_EXITING
|
5567 CPU_BASED_INVLPG_EXITING
;
5568 return exec_control
;
5571 static bool vmx_rdrand_supported(void)
5573 return vmcs_config
.cpu_based_2nd_exec_ctrl
&
5574 SECONDARY_EXEC_RDRAND_EXITING
;
5577 static bool vmx_rdseed_supported(void)
5579 return vmcs_config
.cpu_based_2nd_exec_ctrl
&
5580 SECONDARY_EXEC_RDSEED_EXITING
;
5583 static void vmx_compute_secondary_exec_control(struct vcpu_vmx
*vmx
)
5585 struct kvm_vcpu
*vcpu
= &vmx
->vcpu
;
5587 u32 exec_control
= vmcs_config
.cpu_based_2nd_exec_ctrl
;
5588 if (!cpu_need_virtualize_apic_accesses(vcpu
))
5589 exec_control
&= ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES
;
5591 exec_control
&= ~SECONDARY_EXEC_ENABLE_VPID
;
5593 exec_control
&= ~SECONDARY_EXEC_ENABLE_EPT
;
5594 enable_unrestricted_guest
= 0;
5595 /* Enable INVPCID for non-ept guests may cause performance regression. */
5596 exec_control
&= ~SECONDARY_EXEC_ENABLE_INVPCID
;
5598 if (!enable_unrestricted_guest
)
5599 exec_control
&= ~SECONDARY_EXEC_UNRESTRICTED_GUEST
;
5601 exec_control
&= ~SECONDARY_EXEC_PAUSE_LOOP_EXITING
;
5602 if (!kvm_vcpu_apicv_active(vcpu
))
5603 exec_control
&= ~(SECONDARY_EXEC_APIC_REGISTER_VIRT
|
5604 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY
);
5605 exec_control
&= ~SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE
;
5606 /* SECONDARY_EXEC_SHADOW_VMCS is enabled when L1 executes VMPTRLD
5608 We can NOT enable shadow_vmcs here because we don't have yet
5611 exec_control
&= ~SECONDARY_EXEC_SHADOW_VMCS
;
5614 exec_control
&= ~SECONDARY_EXEC_ENABLE_PML
;
5616 if (vmx_xsaves_supported()) {
5617 /* Exposing XSAVES only when XSAVE is exposed */
5618 bool xsaves_enabled
=
5619 guest_cpuid_has(vcpu
, X86_FEATURE_XSAVE
) &&
5620 guest_cpuid_has(vcpu
, X86_FEATURE_XSAVES
);
5622 if (!xsaves_enabled
)
5623 exec_control
&= ~SECONDARY_EXEC_XSAVES
;
5627 vmx
->nested
.nested_vmx_secondary_ctls_high
|=
5628 SECONDARY_EXEC_XSAVES
;
5630 vmx
->nested
.nested_vmx_secondary_ctls_high
&=
5631 ~SECONDARY_EXEC_XSAVES
;
5635 if (vmx_rdtscp_supported()) {
5636 bool rdtscp_enabled
= guest_cpuid_has(vcpu
, X86_FEATURE_RDTSCP
);
5637 if (!rdtscp_enabled
)
5638 exec_control
&= ~SECONDARY_EXEC_RDTSCP
;
5642 vmx
->nested
.nested_vmx_secondary_ctls_high
|=
5643 SECONDARY_EXEC_RDTSCP
;
5645 vmx
->nested
.nested_vmx_secondary_ctls_high
&=
5646 ~SECONDARY_EXEC_RDTSCP
;
5650 if (vmx_invpcid_supported()) {
5651 /* Exposing INVPCID only when PCID is exposed */
5652 bool invpcid_enabled
=
5653 guest_cpuid_has(vcpu
, X86_FEATURE_INVPCID
) &&
5654 guest_cpuid_has(vcpu
, X86_FEATURE_PCID
);
5656 if (!invpcid_enabled
) {
5657 exec_control
&= ~SECONDARY_EXEC_ENABLE_INVPCID
;
5658 guest_cpuid_clear(vcpu
, X86_FEATURE_INVPCID
);
5662 if (invpcid_enabled
)
5663 vmx
->nested
.nested_vmx_secondary_ctls_high
|=
5664 SECONDARY_EXEC_ENABLE_INVPCID
;
5666 vmx
->nested
.nested_vmx_secondary_ctls_high
&=
5667 ~SECONDARY_EXEC_ENABLE_INVPCID
;
5671 if (vmx_rdrand_supported()) {
5672 bool rdrand_enabled
= guest_cpuid_has(vcpu
, X86_FEATURE_RDRAND
);
5674 exec_control
&= ~SECONDARY_EXEC_RDRAND_EXITING
;
5678 vmx
->nested
.nested_vmx_secondary_ctls_high
|=
5679 SECONDARY_EXEC_RDRAND_EXITING
;
5681 vmx
->nested
.nested_vmx_secondary_ctls_high
&=
5682 ~SECONDARY_EXEC_RDRAND_EXITING
;
5686 if (vmx_rdseed_supported()) {
5687 bool rdseed_enabled
= guest_cpuid_has(vcpu
, X86_FEATURE_RDSEED
);
5689 exec_control
&= ~SECONDARY_EXEC_RDSEED_EXITING
;
5693 vmx
->nested
.nested_vmx_secondary_ctls_high
|=
5694 SECONDARY_EXEC_RDSEED_EXITING
;
5696 vmx
->nested
.nested_vmx_secondary_ctls_high
&=
5697 ~SECONDARY_EXEC_RDSEED_EXITING
;
5701 vmx
->secondary_exec_control
= exec_control
;
5704 static void ept_set_mmio_spte_mask(void)
5707 * EPT Misconfigurations can be generated if the value of bits 2:0
5708 * of an EPT paging-structure entry is 110b (write/execute).
5710 kvm_mmu_set_mmio_spte_mask(VMX_EPT_RWX_MASK
,
5711 VMX_EPT_MISCONFIG_WX_VALUE
);
5714 #define VMX_XSS_EXIT_BITMAP 0
5716 * Sets up the vmcs for emulated real mode.
5718 static void vmx_vcpu_setup(struct vcpu_vmx
*vmx
)
5720 #ifdef CONFIG_X86_64
5726 vmcs_write64(IO_BITMAP_A
, __pa(vmx_io_bitmap_a
));
5727 vmcs_write64(IO_BITMAP_B
, __pa(vmx_io_bitmap_b
));
5729 if (enable_shadow_vmcs
) {
5730 vmcs_write64(VMREAD_BITMAP
, __pa(vmx_vmread_bitmap
));
5731 vmcs_write64(VMWRITE_BITMAP
, __pa(vmx_vmwrite_bitmap
));
5733 if (cpu_has_vmx_msr_bitmap())
5734 vmcs_write64(MSR_BITMAP
, __pa(vmx
->vmcs01
.msr_bitmap
));
5736 vmcs_write64(VMCS_LINK_POINTER
, -1ull); /* 22.3.1.5 */
5739 vmcs_write32(PIN_BASED_VM_EXEC_CONTROL
, vmx_pin_based_exec_ctrl(vmx
));
5740 vmx
->hv_deadline_tsc
= -1;
5742 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL
, vmx_exec_control(vmx
));
5744 if (cpu_has_secondary_exec_ctrls()) {
5745 vmx_compute_secondary_exec_control(vmx
);
5746 vmcs_write32(SECONDARY_VM_EXEC_CONTROL
,
5747 vmx
->secondary_exec_control
);
5750 if (kvm_vcpu_apicv_active(&vmx
->vcpu
)) {
5751 vmcs_write64(EOI_EXIT_BITMAP0
, 0);
5752 vmcs_write64(EOI_EXIT_BITMAP1
, 0);
5753 vmcs_write64(EOI_EXIT_BITMAP2
, 0);
5754 vmcs_write64(EOI_EXIT_BITMAP3
, 0);
5756 vmcs_write16(GUEST_INTR_STATUS
, 0);
5758 vmcs_write16(POSTED_INTR_NV
, POSTED_INTR_VECTOR
);
5759 vmcs_write64(POSTED_INTR_DESC_ADDR
, __pa((&vmx
->pi_desc
)));
5763 vmcs_write32(PLE_GAP
, ple_gap
);
5764 vmx
->ple_window
= ple_window
;
5765 vmx
->ple_window_dirty
= true;
5768 vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK
, 0);
5769 vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH
, 0);
5770 vmcs_write32(CR3_TARGET_COUNT
, 0); /* 22.2.1 */
5772 vmcs_write16(HOST_FS_SELECTOR
, 0); /* 22.2.4 */
5773 vmcs_write16(HOST_GS_SELECTOR
, 0); /* 22.2.4 */
5774 vmx_set_constant_host_state(vmx
);
5775 #ifdef CONFIG_X86_64
5776 rdmsrl(MSR_FS_BASE
, a
);
5777 vmcs_writel(HOST_FS_BASE
, a
); /* 22.2.4 */
5778 rdmsrl(MSR_GS_BASE
, a
);
5779 vmcs_writel(HOST_GS_BASE
, a
); /* 22.2.4 */
5781 vmcs_writel(HOST_FS_BASE
, 0); /* 22.2.4 */
5782 vmcs_writel(HOST_GS_BASE
, 0); /* 22.2.4 */
5785 if (cpu_has_vmx_vmfunc())
5786 vmcs_write64(VM_FUNCTION_CONTROL
, 0);
5788 vmcs_write32(VM_EXIT_MSR_STORE_COUNT
, 0);
5789 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT
, 0);
5790 vmcs_write64(VM_EXIT_MSR_LOAD_ADDR
, __pa(vmx
->msr_autoload
.host
.val
));
5791 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT
, 0);
5792 vmcs_write64(VM_ENTRY_MSR_LOAD_ADDR
, __pa(vmx
->msr_autoload
.guest
.val
));
5794 if (vmcs_config
.vmentry_ctrl
& VM_ENTRY_LOAD_IA32_PAT
)
5795 vmcs_write64(GUEST_IA32_PAT
, vmx
->vcpu
.arch
.pat
);
5797 for (i
= 0; i
< ARRAY_SIZE(vmx_msr_index
); ++i
) {
5798 u32 index
= vmx_msr_index
[i
];
5799 u32 data_low
, data_high
;
5802 if (rdmsr_safe(index
, &data_low
, &data_high
) < 0)
5804 if (wrmsr_safe(index
, data_low
, data_high
) < 0)
5806 vmx
->guest_msrs
[j
].index
= i
;
5807 vmx
->guest_msrs
[j
].data
= 0;
5808 vmx
->guest_msrs
[j
].mask
= -1ull;
5812 if (boot_cpu_has(X86_FEATURE_ARCH_CAPABILITIES
))
5813 rdmsrl(MSR_IA32_ARCH_CAPABILITIES
, vmx
->arch_capabilities
);
5815 vm_exit_controls_init(vmx
, vmcs_config
.vmexit_ctrl
);
5817 /* 22.2.1, 20.8.1 */
5818 vm_entry_controls_init(vmx
, vmcs_config
.vmentry_ctrl
);
5820 vmx
->vcpu
.arch
.cr0_guest_owned_bits
= X86_CR0_TS
;
5821 vmcs_writel(CR0_GUEST_HOST_MASK
, ~X86_CR0_TS
);
5823 set_cr4_guest_host_mask(vmx
);
5825 if (vmx_xsaves_supported())
5826 vmcs_write64(XSS_EXIT_BITMAP
, VMX_XSS_EXIT_BITMAP
);
5829 ASSERT(vmx
->pml_pg
);
5830 vmcs_write64(PML_ADDRESS
, page_to_phys(vmx
->pml_pg
));
5831 vmcs_write16(GUEST_PML_INDEX
, PML_ENTITY_NUM
- 1);
5835 static void vmx_vcpu_reset(struct kvm_vcpu
*vcpu
, bool init_event
)
5837 struct vcpu_vmx
*vmx
= to_vmx(vcpu
);
5838 struct msr_data apic_base_msr
;
5841 vmx
->rmode
.vm86_active
= 0;
5844 vmx
->vcpu
.arch
.regs
[VCPU_REGS_RDX
] = get_rdx_init_val();
5845 kvm_set_cr8(vcpu
, 0);
5848 apic_base_msr
.data
= APIC_DEFAULT_PHYS_BASE
|
5849 MSR_IA32_APICBASE_ENABLE
;
5850 if (kvm_vcpu_is_reset_bsp(vcpu
))
5851 apic_base_msr
.data
|= MSR_IA32_APICBASE_BSP
;
5852 apic_base_msr
.host_initiated
= true;
5853 kvm_set_apic_base(vcpu
, &apic_base_msr
);
5856 vmx_segment_cache_clear(vmx
);
5858 seg_setup(VCPU_SREG_CS
);
5859 vmcs_write16(GUEST_CS_SELECTOR
, 0xf000);
5860 vmcs_writel(GUEST_CS_BASE
, 0xffff0000ul
);
5862 seg_setup(VCPU_SREG_DS
);
5863 seg_setup(VCPU_SREG_ES
);
5864 seg_setup(VCPU_SREG_FS
);
5865 seg_setup(VCPU_SREG_GS
);
5866 seg_setup(VCPU_SREG_SS
);
5868 vmcs_write16(GUEST_TR_SELECTOR
, 0);
5869 vmcs_writel(GUEST_TR_BASE
, 0);
5870 vmcs_write32(GUEST_TR_LIMIT
, 0xffff);
5871 vmcs_write32(GUEST_TR_AR_BYTES
, 0x008b);
5873 vmcs_write16(GUEST_LDTR_SELECTOR
, 0);
5874 vmcs_writel(GUEST_LDTR_BASE
, 0);
5875 vmcs_write32(GUEST_LDTR_LIMIT
, 0xffff);
5876 vmcs_write32(GUEST_LDTR_AR_BYTES
, 0x00082);
5879 vmcs_write32(GUEST_SYSENTER_CS
, 0);
5880 vmcs_writel(GUEST_SYSENTER_ESP
, 0);
5881 vmcs_writel(GUEST_SYSENTER_EIP
, 0);
5882 vmcs_write64(GUEST_IA32_DEBUGCTL
, 0);
5885 kvm_set_rflags(vcpu
, X86_EFLAGS_FIXED
);
5886 kvm_rip_write(vcpu
, 0xfff0);
5888 vmcs_writel(GUEST_GDTR_BASE
, 0);
5889 vmcs_write32(GUEST_GDTR_LIMIT
, 0xffff);
5891 vmcs_writel(GUEST_IDTR_BASE
, 0);
5892 vmcs_write32(GUEST_IDTR_LIMIT
, 0xffff);
5894 vmcs_write32(GUEST_ACTIVITY_STATE
, GUEST_ACTIVITY_ACTIVE
);
5895 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO
, 0);
5896 vmcs_writel(GUEST_PENDING_DBG_EXCEPTIONS
, 0);
5897 if (kvm_mpx_supported())
5898 vmcs_write64(GUEST_BNDCFGS
, 0);
5902 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD
, 0); /* 22.2.1 */
5904 if (cpu_has_vmx_tpr_shadow() && !init_event
) {
5905 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR
, 0);
5906 if (cpu_need_tpr_shadow(vcpu
))
5907 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR
,
5908 __pa(vcpu
->arch
.apic
->regs
));
5909 vmcs_write32(TPR_THRESHOLD
, 0);
5912 kvm_make_request(KVM_REQ_APIC_PAGE_RELOAD
, vcpu
);
5915 vmcs_write16(VIRTUAL_PROCESSOR_ID
, vmx
->vpid
);
5917 cr0
= X86_CR0_NW
| X86_CR0_CD
| X86_CR0_ET
;
5918 vmx
->vcpu
.arch
.cr0
= cr0
;
5919 vmx_set_cr0(vcpu
, cr0
); /* enter rmode */
5920 vmx_set_cr4(vcpu
, 0);
5921 vmx_set_efer(vcpu
, 0);
5923 update_exception_bitmap(vcpu
);
5925 vpid_sync_context(vmx
->vpid
);
5929 * In nested virtualization, check if L1 asked to exit on external interrupts.
5930 * For most existing hypervisors, this will always return true.
5932 static bool nested_exit_on_intr(struct kvm_vcpu
*vcpu
)
5934 return get_vmcs12(vcpu
)->pin_based_vm_exec_control
&
5935 PIN_BASED_EXT_INTR_MASK
;
5939 * In nested virtualization, check if L1 has set
5940 * VM_EXIT_ACK_INTR_ON_EXIT
5942 static bool nested_exit_intr_ack_set(struct kvm_vcpu
*vcpu
)
5944 return get_vmcs12(vcpu
)->vm_exit_controls
&
5945 VM_EXIT_ACK_INTR_ON_EXIT
;
5948 static bool nested_exit_on_nmi(struct kvm_vcpu
*vcpu
)
5950 return get_vmcs12(vcpu
)->pin_based_vm_exec_control
&
5951 PIN_BASED_NMI_EXITING
;
5954 static void enable_irq_window(struct kvm_vcpu
*vcpu
)
5956 vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL
,
5957 CPU_BASED_VIRTUAL_INTR_PENDING
);
5960 static void enable_nmi_window(struct kvm_vcpu
*vcpu
)
5963 vmcs_read32(GUEST_INTERRUPTIBILITY_INFO
) & GUEST_INTR_STATE_STI
) {
5964 enable_irq_window(vcpu
);
5968 vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL
,
5969 CPU_BASED_VIRTUAL_NMI_PENDING
);
5972 static void vmx_inject_irq(struct kvm_vcpu
*vcpu
)
5974 struct vcpu_vmx
*vmx
= to_vmx(vcpu
);
5976 int irq
= vcpu
->arch
.interrupt
.nr
;
5978 trace_kvm_inj_virq(irq
);
5980 ++vcpu
->stat
.irq_injections
;
5981 if (vmx
->rmode
.vm86_active
) {
5983 if (vcpu
->arch
.interrupt
.soft
)
5984 inc_eip
= vcpu
->arch
.event_exit_inst_len
;
5985 if (kvm_inject_realmode_interrupt(vcpu
, irq
, inc_eip
) != EMULATE_DONE
)
5986 kvm_make_request(KVM_REQ_TRIPLE_FAULT
, vcpu
);
5989 intr
= irq
| INTR_INFO_VALID_MASK
;
5990 if (vcpu
->arch
.interrupt
.soft
) {
5991 intr
|= INTR_TYPE_SOFT_INTR
;
5992 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN
,
5993 vmx
->vcpu
.arch
.event_exit_inst_len
);
5995 intr
|= INTR_TYPE_EXT_INTR
;
5996 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD
, intr
);
5999 static void vmx_inject_nmi(struct kvm_vcpu
*vcpu
)
6001 struct vcpu_vmx
*vmx
= to_vmx(vcpu
);
6005 * Tracking the NMI-blocked state in software is built upon
6006 * finding the next open IRQ window. This, in turn, depends on
6007 * well-behaving guests: They have to keep IRQs disabled at
6008 * least as long as the NMI handler runs. Otherwise we may
6009 * cause NMI nesting, maybe breaking the guest. But as this is
6010 * highly unlikely, we can live with the residual risk.
6012 vmx
->loaded_vmcs
->soft_vnmi_blocked
= 1;
6013 vmx
->loaded_vmcs
->vnmi_blocked_time
= 0;
6016 ++vcpu
->stat
.nmi_injections
;
6017 vmx
->loaded_vmcs
->nmi_known_unmasked
= false;
6019 if (vmx
->rmode
.vm86_active
) {
6020 if (kvm_inject_realmode_interrupt(vcpu
, NMI_VECTOR
, 0) != EMULATE_DONE
)
6021 kvm_make_request(KVM_REQ_TRIPLE_FAULT
, vcpu
);
6025 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD
,
6026 INTR_TYPE_NMI_INTR
| INTR_INFO_VALID_MASK
| NMI_VECTOR
);
6029 static bool vmx_get_nmi_mask(struct kvm_vcpu
*vcpu
)
6031 struct vcpu_vmx
*vmx
= to_vmx(vcpu
);
6035 return vmx
->loaded_vmcs
->soft_vnmi_blocked
;
6036 if (vmx
->loaded_vmcs
->nmi_known_unmasked
)
6038 masked
= vmcs_read32(GUEST_INTERRUPTIBILITY_INFO
) & GUEST_INTR_STATE_NMI
;
6039 vmx
->loaded_vmcs
->nmi_known_unmasked
= !masked
;
6043 static void vmx_set_nmi_mask(struct kvm_vcpu
*vcpu
, bool masked
)
6045 struct vcpu_vmx
*vmx
= to_vmx(vcpu
);
6048 if (vmx
->loaded_vmcs
->soft_vnmi_blocked
!= masked
) {
6049 vmx
->loaded_vmcs
->soft_vnmi_blocked
= masked
;
6050 vmx
->loaded_vmcs
->vnmi_blocked_time
= 0;
6053 vmx
->loaded_vmcs
->nmi_known_unmasked
= !masked
;
6055 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO
,
6056 GUEST_INTR_STATE_NMI
);
6058 vmcs_clear_bits(GUEST_INTERRUPTIBILITY_INFO
,
6059 GUEST_INTR_STATE_NMI
);
6063 static int vmx_nmi_allowed(struct kvm_vcpu
*vcpu
)
6065 if (to_vmx(vcpu
)->nested
.nested_run_pending
)
6069 to_vmx(vcpu
)->loaded_vmcs
->soft_vnmi_blocked
)
6072 return !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO
) &
6073 (GUEST_INTR_STATE_MOV_SS
| GUEST_INTR_STATE_STI
6074 | GUEST_INTR_STATE_NMI
));
6077 static int vmx_interrupt_allowed(struct kvm_vcpu
*vcpu
)
6079 return (!to_vmx(vcpu
)->nested
.nested_run_pending
&&
6080 vmcs_readl(GUEST_RFLAGS
) & X86_EFLAGS_IF
) &&
6081 !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO
) &
6082 (GUEST_INTR_STATE_STI
| GUEST_INTR_STATE_MOV_SS
));
6085 static int vmx_set_tss_addr(struct kvm
*kvm
, unsigned int addr
)
6089 ret
= x86_set_memory_region(kvm
, TSS_PRIVATE_MEMSLOT
, addr
,
6093 kvm
->arch
.tss_addr
= addr
;
6094 return init_rmode_tss(kvm
);
6097 static bool rmode_exception(struct kvm_vcpu
*vcpu
, int vec
)
6102 * Update instruction length as we may reinject the exception
6103 * from user space while in guest debugging mode.
6105 to_vmx(vcpu
)->vcpu
.arch
.event_exit_inst_len
=
6106 vmcs_read32(VM_EXIT_INSTRUCTION_LEN
);
6107 if (vcpu
->guest_debug
& KVM_GUESTDBG_USE_SW_BP
)
6111 if (vcpu
->guest_debug
&
6112 (KVM_GUESTDBG_SINGLESTEP
| KVM_GUESTDBG_USE_HW_BP
))
6129 static int handle_rmode_exception(struct kvm_vcpu
*vcpu
,
6130 int vec
, u32 err_code
)
6133 * Instruction with address size override prefix opcode 0x67
6134 * Cause the #SS fault with 0 error code in VM86 mode.
6136 if (((vec
== GP_VECTOR
) || (vec
== SS_VECTOR
)) && err_code
== 0) {
6137 if (emulate_instruction(vcpu
, 0) == EMULATE_DONE
) {
6138 if (vcpu
->arch
.halt_request
) {
6139 vcpu
->arch
.halt_request
= 0;
6140 return kvm_vcpu_halt(vcpu
);
6148 * Forward all other exceptions that are valid in real mode.
6149 * FIXME: Breaks guest debugging in real mode, needs to be fixed with
6150 * the required debugging infrastructure rework.
6152 kvm_queue_exception(vcpu
, vec
);
6157 * Trigger machine check on the host. We assume all the MSRs are already set up
6158 * by the CPU and that we still run on the same CPU as the MCE occurred on.
6159 * We pass a fake environment to the machine check handler because we want
6160 * the guest to be always treated like user space, no matter what context
6161 * it used internally.
6163 static void kvm_machine_check(void)
6165 #if defined(CONFIG_X86_MCE) && defined(CONFIG_X86_64)
6166 struct pt_regs regs
= {
6167 .cs
= 3, /* Fake ring 3 no matter what the guest ran on */
6168 .flags
= X86_EFLAGS_IF
,
6171 do_machine_check(®s
, 0);
6175 static int handle_machine_check(struct kvm_vcpu
*vcpu
)
6177 /* already handled by vcpu_run */
6181 static int handle_exception(struct kvm_vcpu
*vcpu
)
6183 struct vcpu_vmx
*vmx
= to_vmx(vcpu
);
6184 struct kvm_run
*kvm_run
= vcpu
->run
;
6185 u32 intr_info
, ex_no
, error_code
;
6186 unsigned long cr2
, rip
, dr6
;
6188 enum emulation_result er
;
6190 vect_info
= vmx
->idt_vectoring_info
;
6191 intr_info
= vmx
->exit_intr_info
;
6193 if (is_machine_check(intr_info
))
6194 return handle_machine_check(vcpu
);
6196 if (is_nmi(intr_info
))
6197 return 1; /* already handled by vmx_vcpu_run() */
6199 if (is_invalid_opcode(intr_info
)) {
6200 er
= emulate_instruction(vcpu
, EMULTYPE_TRAP_UD
);
6201 if (er
== EMULATE_USER_EXIT
)
6203 if (er
!= EMULATE_DONE
)
6204 kvm_queue_exception(vcpu
, UD_VECTOR
);
6209 if (intr_info
& INTR_INFO_DELIVER_CODE_MASK
)
6210 error_code
= vmcs_read32(VM_EXIT_INTR_ERROR_CODE
);
6213 * The #PF with PFEC.RSVD = 1 indicates the guest is accessing
6214 * MMIO, it is better to report an internal error.
6215 * See the comments in vmx_handle_exit.
6217 if ((vect_info
& VECTORING_INFO_VALID_MASK
) &&
6218 !(is_page_fault(intr_info
) && !(error_code
& PFERR_RSVD_MASK
))) {
6219 vcpu
->run
->exit_reason
= KVM_EXIT_INTERNAL_ERROR
;
6220 vcpu
->run
->internal
.suberror
= KVM_INTERNAL_ERROR_SIMUL_EX
;
6221 vcpu
->run
->internal
.ndata
= 3;
6222 vcpu
->run
->internal
.data
[0] = vect_info
;
6223 vcpu
->run
->internal
.data
[1] = intr_info
;
6224 vcpu
->run
->internal
.data
[2] = error_code
;
6228 if (is_page_fault(intr_info
)) {
6229 cr2
= vmcs_readl(EXIT_QUALIFICATION
);
6230 /* EPT won't cause page fault directly */
6231 WARN_ON_ONCE(!vcpu
->arch
.apf
.host_apf_reason
&& enable_ept
);
6232 return kvm_handle_page_fault(vcpu
, error_code
, cr2
, NULL
, 0);
6235 ex_no
= intr_info
& INTR_INFO_VECTOR_MASK
;
6237 if (vmx
->rmode
.vm86_active
&& rmode_exception(vcpu
, ex_no
))
6238 return handle_rmode_exception(vcpu
, ex_no
, error_code
);
6242 kvm_queue_exception_e(vcpu
, AC_VECTOR
, error_code
);
6245 dr6
= vmcs_readl(EXIT_QUALIFICATION
);
6246 if (!(vcpu
->guest_debug
&
6247 (KVM_GUESTDBG_SINGLESTEP
| KVM_GUESTDBG_USE_HW_BP
))) {
6248 vcpu
->arch
.dr6
&= ~15;
6249 vcpu
->arch
.dr6
|= dr6
| DR6_RTM
;
6250 if (is_icebp(intr_info
))
6251 skip_emulated_instruction(vcpu
);
6253 kvm_queue_exception(vcpu
, DB_VECTOR
);
6256 kvm_run
->debug
.arch
.dr6
= dr6
| DR6_FIXED_1
;
6257 kvm_run
->debug
.arch
.dr7
= vmcs_readl(GUEST_DR7
);
6261 * Update instruction length as we may reinject #BP from
6262 * user space while in guest debugging mode. Reading it for
6263 * #DB as well causes no harm, it is not used in that case.
6265 vmx
->vcpu
.arch
.event_exit_inst_len
=
6266 vmcs_read32(VM_EXIT_INSTRUCTION_LEN
);
6267 kvm_run
->exit_reason
= KVM_EXIT_DEBUG
;
6268 rip
= kvm_rip_read(vcpu
);
6269 kvm_run
->debug
.arch
.pc
= vmcs_readl(GUEST_CS_BASE
) + rip
;
6270 kvm_run
->debug
.arch
.exception
= ex_no
;
6273 kvm_run
->exit_reason
= KVM_EXIT_EXCEPTION
;
6274 kvm_run
->ex
.exception
= ex_no
;
6275 kvm_run
->ex
.error_code
= error_code
;
6281 static int handle_external_interrupt(struct kvm_vcpu
*vcpu
)
6283 ++vcpu
->stat
.irq_exits
;
6287 static int handle_triple_fault(struct kvm_vcpu
*vcpu
)
6289 vcpu
->run
->exit_reason
= KVM_EXIT_SHUTDOWN
;
6290 vcpu
->mmio_needed
= 0;
6294 static int handle_io(struct kvm_vcpu
*vcpu
)
6296 unsigned long exit_qualification
;
6297 int size
, in
, string
, ret
;
6300 exit_qualification
= vmcs_readl(EXIT_QUALIFICATION
);
6301 string
= (exit_qualification
& 16) != 0;
6302 in
= (exit_qualification
& 8) != 0;
6304 ++vcpu
->stat
.io_exits
;
6307 return emulate_instruction(vcpu
, 0) == EMULATE_DONE
;
6309 port
= exit_qualification
>> 16;
6310 size
= (exit_qualification
& 7) + 1;
6312 ret
= kvm_skip_emulated_instruction(vcpu
);
6315 * TODO: we might be squashing a KVM_GUESTDBG_SINGLESTEP-triggered
6316 * KVM_EXIT_DEBUG here.
6318 return kvm_fast_pio_out(vcpu
, size
, port
) && ret
;
6322 vmx_patch_hypercall(struct kvm_vcpu
*vcpu
, unsigned char *hypercall
)
6325 * Patch in the VMCALL instruction:
6327 hypercall
[0] = 0x0f;
6328 hypercall
[1] = 0x01;
6329 hypercall
[2] = 0xc1;
6332 /* called to set cr0 as appropriate for a mov-to-cr0 exit. */
6333 static int handle_set_cr0(struct kvm_vcpu
*vcpu
, unsigned long val
)
6335 if (is_guest_mode(vcpu
)) {
6336 struct vmcs12
*vmcs12
= get_vmcs12(vcpu
);
6337 unsigned long orig_val
= val
;
6340 * We get here when L2 changed cr0 in a way that did not change
6341 * any of L1's shadowed bits (see nested_vmx_exit_handled_cr),
6342 * but did change L0 shadowed bits. So we first calculate the
6343 * effective cr0 value that L1 would like to write into the
6344 * hardware. It consists of the L2-owned bits from the new
6345 * value combined with the L1-owned bits from L1's guest_cr0.
6347 val
= (val
& ~vmcs12
->cr0_guest_host_mask
) |
6348 (vmcs12
->guest_cr0
& vmcs12
->cr0_guest_host_mask
);
6350 if (!nested_guest_cr0_valid(vcpu
, val
))
6353 if (kvm_set_cr0(vcpu
, val
))
6355 vmcs_writel(CR0_READ_SHADOW
, orig_val
);
6358 if (to_vmx(vcpu
)->nested
.vmxon
&&
6359 !nested_host_cr0_valid(vcpu
, val
))
6362 return kvm_set_cr0(vcpu
, val
);
6366 static int handle_set_cr4(struct kvm_vcpu
*vcpu
, unsigned long val
)
6368 if (is_guest_mode(vcpu
)) {
6369 struct vmcs12
*vmcs12
= get_vmcs12(vcpu
);
6370 unsigned long orig_val
= val
;
6372 /* analogously to handle_set_cr0 */
6373 val
= (val
& ~vmcs12
->cr4_guest_host_mask
) |
6374 (vmcs12
->guest_cr4
& vmcs12
->cr4_guest_host_mask
);
6375 if (kvm_set_cr4(vcpu
, val
))
6377 vmcs_writel(CR4_READ_SHADOW
, orig_val
);
6380 return kvm_set_cr4(vcpu
, val
);
6383 static int handle_cr(struct kvm_vcpu
*vcpu
)
6385 unsigned long exit_qualification
, val
;
6391 exit_qualification
= vmcs_readl(EXIT_QUALIFICATION
);
6392 cr
= exit_qualification
& 15;
6393 reg
= (exit_qualification
>> 8) & 15;
6394 switch ((exit_qualification
>> 4) & 3) {
6395 case 0: /* mov to cr */
6396 val
= kvm_register_readl(vcpu
, reg
);
6397 trace_kvm_cr_write(cr
, val
);
6400 err
= handle_set_cr0(vcpu
, val
);
6401 return kvm_complete_insn_gp(vcpu
, err
);
6403 err
= kvm_set_cr3(vcpu
, val
);
6404 return kvm_complete_insn_gp(vcpu
, err
);
6406 err
= handle_set_cr4(vcpu
, val
);
6407 return kvm_complete_insn_gp(vcpu
, err
);
6409 u8 cr8_prev
= kvm_get_cr8(vcpu
);
6411 err
= kvm_set_cr8(vcpu
, cr8
);
6412 ret
= kvm_complete_insn_gp(vcpu
, err
);
6413 if (lapic_in_kernel(vcpu
))
6415 if (cr8_prev
<= cr8
)
6418 * TODO: we might be squashing a
6419 * KVM_GUESTDBG_SINGLESTEP-triggered
6420 * KVM_EXIT_DEBUG here.
6422 vcpu
->run
->exit_reason
= KVM_EXIT_SET_TPR
;
6428 WARN_ONCE(1, "Guest should always own CR0.TS");
6429 vmx_set_cr0(vcpu
, kvm_read_cr0_bits(vcpu
, ~X86_CR0_TS
));
6430 trace_kvm_cr_write(0, kvm_read_cr0(vcpu
));
6431 return kvm_skip_emulated_instruction(vcpu
);
6432 case 1: /*mov from cr*/
6435 val
= kvm_read_cr3(vcpu
);
6436 kvm_register_write(vcpu
, reg
, val
);
6437 trace_kvm_cr_read(cr
, val
);
6438 return kvm_skip_emulated_instruction(vcpu
);
6440 val
= kvm_get_cr8(vcpu
);
6441 kvm_register_write(vcpu
, reg
, val
);
6442 trace_kvm_cr_read(cr
, val
);
6443 return kvm_skip_emulated_instruction(vcpu
);
6447 val
= (exit_qualification
>> LMSW_SOURCE_DATA_SHIFT
) & 0x0f;
6448 trace_kvm_cr_write(0, (kvm_read_cr0(vcpu
) & ~0xful
) | val
);
6449 kvm_lmsw(vcpu
, val
);
6451 return kvm_skip_emulated_instruction(vcpu
);
6455 vcpu
->run
->exit_reason
= 0;
6456 vcpu_unimpl(vcpu
, "unhandled control register: op %d cr %d\n",
6457 (int)(exit_qualification
>> 4) & 3, cr
);
6461 static int handle_dr(struct kvm_vcpu
*vcpu
)
6463 unsigned long exit_qualification
;
6466 exit_qualification
= vmcs_readl(EXIT_QUALIFICATION
);
6467 dr
= exit_qualification
& DEBUG_REG_ACCESS_NUM
;
6469 /* First, if DR does not exist, trigger UD */
6470 if (!kvm_require_dr(vcpu
, dr
))
6473 /* Do not handle if the CPL > 0, will trigger GP on re-entry */
6474 if (!kvm_require_cpl(vcpu
, 0))
6476 dr7
= vmcs_readl(GUEST_DR7
);
6479 * As the vm-exit takes precedence over the debug trap, we
6480 * need to emulate the latter, either for the host or the
6481 * guest debugging itself.
6483 if (vcpu
->guest_debug
& KVM_GUESTDBG_USE_HW_BP
) {
6484 vcpu
->run
->debug
.arch
.dr6
= vcpu
->arch
.dr6
;
6485 vcpu
->run
->debug
.arch
.dr7
= dr7
;
6486 vcpu
->run
->debug
.arch
.pc
= kvm_get_linear_rip(vcpu
);
6487 vcpu
->run
->debug
.arch
.exception
= DB_VECTOR
;
6488 vcpu
->run
->exit_reason
= KVM_EXIT_DEBUG
;
6491 vcpu
->arch
.dr6
&= ~15;
6492 vcpu
->arch
.dr6
|= DR6_BD
| DR6_RTM
;
6493 kvm_queue_exception(vcpu
, DB_VECTOR
);
6498 if (vcpu
->guest_debug
== 0) {
6499 vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL
,
6500 CPU_BASED_MOV_DR_EXITING
);
6503 * No more DR vmexits; force a reload of the debug registers
6504 * and reenter on this instruction. The next vmexit will
6505 * retrieve the full state of the debug registers.
6507 vcpu
->arch
.switch_db_regs
|= KVM_DEBUGREG_WONT_EXIT
;
6511 reg
= DEBUG_REG_ACCESS_REG(exit_qualification
);
6512 if (exit_qualification
& TYPE_MOV_FROM_DR
) {
6515 if (kvm_get_dr(vcpu
, dr
, &val
))
6517 kvm_register_write(vcpu
, reg
, val
);
6519 if (kvm_set_dr(vcpu
, dr
, kvm_register_readl(vcpu
, reg
)))
6522 return kvm_skip_emulated_instruction(vcpu
);
6525 static u64
vmx_get_dr6(struct kvm_vcpu
*vcpu
)
6527 return vcpu
->arch
.dr6
;
6530 static void vmx_set_dr6(struct kvm_vcpu
*vcpu
, unsigned long val
)
6534 static void vmx_sync_dirty_debug_regs(struct kvm_vcpu
*vcpu
)
6536 get_debugreg(vcpu
->arch
.db
[0], 0);
6537 get_debugreg(vcpu
->arch
.db
[1], 1);
6538 get_debugreg(vcpu
->arch
.db
[2], 2);
6539 get_debugreg(vcpu
->arch
.db
[3], 3);
6540 get_debugreg(vcpu
->arch
.dr6
, 6);
6541 vcpu
->arch
.dr7
= vmcs_readl(GUEST_DR7
);
6543 vcpu
->arch
.switch_db_regs
&= ~KVM_DEBUGREG_WONT_EXIT
;
6544 vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL
, CPU_BASED_MOV_DR_EXITING
);
6547 static void vmx_set_dr7(struct kvm_vcpu
*vcpu
, unsigned long val
)
6549 vmcs_writel(GUEST_DR7
, val
);
6552 static int handle_cpuid(struct kvm_vcpu
*vcpu
)
6554 return kvm_emulate_cpuid(vcpu
);
6557 static int handle_rdmsr(struct kvm_vcpu
*vcpu
)
6559 u32 ecx
= vcpu
->arch
.regs
[VCPU_REGS_RCX
];
6560 struct msr_data msr_info
;
6562 msr_info
.index
= ecx
;
6563 msr_info
.host_initiated
= false;
6564 if (vmx_get_msr(vcpu
, &msr_info
)) {
6565 trace_kvm_msr_read_ex(ecx
);
6566 kvm_inject_gp(vcpu
, 0);
6570 trace_kvm_msr_read(ecx
, msr_info
.data
);
6572 /* FIXME: handling of bits 32:63 of rax, rdx */
6573 vcpu
->arch
.regs
[VCPU_REGS_RAX
] = msr_info
.data
& -1u;
6574 vcpu
->arch
.regs
[VCPU_REGS_RDX
] = (msr_info
.data
>> 32) & -1u;
6575 return kvm_skip_emulated_instruction(vcpu
);
6578 static int handle_wrmsr(struct kvm_vcpu
*vcpu
)
6580 struct msr_data msr
;
6581 u32 ecx
= vcpu
->arch
.regs
[VCPU_REGS_RCX
];
6582 u64 data
= (vcpu
->arch
.regs
[VCPU_REGS_RAX
] & -1u)
6583 | ((u64
)(vcpu
->arch
.regs
[VCPU_REGS_RDX
] & -1u) << 32);
6587 msr
.host_initiated
= false;
6588 if (kvm_set_msr(vcpu
, &msr
) != 0) {
6589 trace_kvm_msr_write_ex(ecx
, data
);
6590 kvm_inject_gp(vcpu
, 0);
6594 trace_kvm_msr_write(ecx
, data
);
6595 return kvm_skip_emulated_instruction(vcpu
);
6598 static int handle_tpr_below_threshold(struct kvm_vcpu
*vcpu
)
6600 kvm_apic_update_ppr(vcpu
);
6604 static int handle_interrupt_window(struct kvm_vcpu
*vcpu
)
6606 vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL
,
6607 CPU_BASED_VIRTUAL_INTR_PENDING
);
6609 kvm_make_request(KVM_REQ_EVENT
, vcpu
);
6611 ++vcpu
->stat
.irq_window_exits
;
6615 static int handle_halt(struct kvm_vcpu
*vcpu
)
6617 return kvm_emulate_halt(vcpu
);
6620 static int handle_vmcall(struct kvm_vcpu
*vcpu
)
6622 return kvm_emulate_hypercall(vcpu
);
6625 static int handle_invd(struct kvm_vcpu
*vcpu
)
6627 return emulate_instruction(vcpu
, 0) == EMULATE_DONE
;
6630 static int handle_invlpg(struct kvm_vcpu
*vcpu
)
6632 unsigned long exit_qualification
= vmcs_readl(EXIT_QUALIFICATION
);
6634 kvm_mmu_invlpg(vcpu
, exit_qualification
);
6635 return kvm_skip_emulated_instruction(vcpu
);
6638 static int handle_rdpmc(struct kvm_vcpu
*vcpu
)
6642 err
= kvm_rdpmc(vcpu
);
6643 return kvm_complete_insn_gp(vcpu
, err
);
6646 static int handle_wbinvd(struct kvm_vcpu
*vcpu
)
6648 return kvm_emulate_wbinvd(vcpu
);
6651 static int handle_xsetbv(struct kvm_vcpu
*vcpu
)
6653 u64 new_bv
= kvm_read_edx_eax(vcpu
);
6654 u32 index
= kvm_register_read(vcpu
, VCPU_REGS_RCX
);
6656 if (kvm_set_xcr(vcpu
, index
, new_bv
) == 0)
6657 return kvm_skip_emulated_instruction(vcpu
);
6661 static int handle_xsaves(struct kvm_vcpu
*vcpu
)
6663 kvm_skip_emulated_instruction(vcpu
);
6664 WARN(1, "this should never happen\n");
6668 static int handle_xrstors(struct kvm_vcpu
*vcpu
)
6670 kvm_skip_emulated_instruction(vcpu
);
6671 WARN(1, "this should never happen\n");
6675 static int handle_apic_access(struct kvm_vcpu
*vcpu
)
6677 if (likely(fasteoi
)) {
6678 unsigned long exit_qualification
= vmcs_readl(EXIT_QUALIFICATION
);
6679 int access_type
, offset
;
6681 access_type
= exit_qualification
& APIC_ACCESS_TYPE
;
6682 offset
= exit_qualification
& APIC_ACCESS_OFFSET
;
6684 * Sane guest uses MOV to write EOI, with written value
6685 * not cared. So make a short-circuit here by avoiding
6686 * heavy instruction emulation.
6688 if ((access_type
== TYPE_LINEAR_APIC_INST_WRITE
) &&
6689 (offset
== APIC_EOI
)) {
6690 kvm_lapic_set_eoi(vcpu
);
6691 return kvm_skip_emulated_instruction(vcpu
);
6694 return emulate_instruction(vcpu
, 0) == EMULATE_DONE
;
6697 static int handle_apic_eoi_induced(struct kvm_vcpu
*vcpu
)
6699 unsigned long exit_qualification
= vmcs_readl(EXIT_QUALIFICATION
);
6700 int vector
= exit_qualification
& 0xff;
6702 /* EOI-induced VM exit is trap-like and thus no need to adjust IP */
6703 kvm_apic_set_eoi_accelerated(vcpu
, vector
);
6707 static int handle_apic_write(struct kvm_vcpu
*vcpu
)
6709 unsigned long exit_qualification
= vmcs_readl(EXIT_QUALIFICATION
);
6710 u32 offset
= exit_qualification
& 0xfff;
6712 /* APIC-write VM exit is trap-like and thus no need to adjust IP */
6713 kvm_apic_write_nodecode(vcpu
, offset
);
6717 static int handle_task_switch(struct kvm_vcpu
*vcpu
)
6719 struct vcpu_vmx
*vmx
= to_vmx(vcpu
);
6720 unsigned long exit_qualification
;
6721 bool has_error_code
= false;
6724 int reason
, type
, idt_v
, idt_index
;
6726 idt_v
= (vmx
->idt_vectoring_info
& VECTORING_INFO_VALID_MASK
);
6727 idt_index
= (vmx
->idt_vectoring_info
& VECTORING_INFO_VECTOR_MASK
);
6728 type
= (vmx
->idt_vectoring_info
& VECTORING_INFO_TYPE_MASK
);
6730 exit_qualification
= vmcs_readl(EXIT_QUALIFICATION
);
6732 reason
= (u32
)exit_qualification
>> 30;
6733 if (reason
== TASK_SWITCH_GATE
&& idt_v
) {
6735 case INTR_TYPE_NMI_INTR
:
6736 vcpu
->arch
.nmi_injected
= false;
6737 vmx_set_nmi_mask(vcpu
, true);
6739 case INTR_TYPE_EXT_INTR
:
6740 case INTR_TYPE_SOFT_INTR
:
6741 kvm_clear_interrupt_queue(vcpu
);
6743 case INTR_TYPE_HARD_EXCEPTION
:
6744 if (vmx
->idt_vectoring_info
&
6745 VECTORING_INFO_DELIVER_CODE_MASK
) {
6746 has_error_code
= true;
6748 vmcs_read32(IDT_VECTORING_ERROR_CODE
);
6751 case INTR_TYPE_SOFT_EXCEPTION
:
6752 kvm_clear_exception_queue(vcpu
);
6758 tss_selector
= exit_qualification
;
6760 if (!idt_v
|| (type
!= INTR_TYPE_HARD_EXCEPTION
&&
6761 type
!= INTR_TYPE_EXT_INTR
&&
6762 type
!= INTR_TYPE_NMI_INTR
))
6763 skip_emulated_instruction(vcpu
);
6765 if (kvm_task_switch(vcpu
, tss_selector
,
6766 type
== INTR_TYPE_SOFT_INTR
? idt_index
: -1, reason
,
6767 has_error_code
, error_code
) == EMULATE_FAIL
) {
6768 vcpu
->run
->exit_reason
= KVM_EXIT_INTERNAL_ERROR
;
6769 vcpu
->run
->internal
.suberror
= KVM_INTERNAL_ERROR_EMULATION
;
6770 vcpu
->run
->internal
.ndata
= 0;
6775 * TODO: What about debug traps on tss switch?
6776 * Are we supposed to inject them and update dr6?
6782 static int handle_ept_violation(struct kvm_vcpu
*vcpu
)
6784 unsigned long exit_qualification
;
6788 exit_qualification
= vmcs_readl(EXIT_QUALIFICATION
);
6791 * EPT violation happened while executing iret from NMI,
6792 * "blocked by NMI" bit has to be set before next VM entry.
6793 * There are errata that may cause this bit to not be set:
6796 if (!(to_vmx(vcpu
)->idt_vectoring_info
& VECTORING_INFO_VALID_MASK
) &&
6798 (exit_qualification
& INTR_INFO_UNBLOCK_NMI
))
6799 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO
, GUEST_INTR_STATE_NMI
);
6801 gpa
= vmcs_read64(GUEST_PHYSICAL_ADDRESS
);
6802 trace_kvm_page_fault(gpa
, exit_qualification
);
6804 /* Is it a read fault? */
6805 error_code
= (exit_qualification
& EPT_VIOLATION_ACC_READ
)
6806 ? PFERR_USER_MASK
: 0;
6807 /* Is it a write fault? */
6808 error_code
|= (exit_qualification
& EPT_VIOLATION_ACC_WRITE
)
6809 ? PFERR_WRITE_MASK
: 0;
6810 /* Is it a fetch fault? */
6811 error_code
|= (exit_qualification
& EPT_VIOLATION_ACC_INSTR
)
6812 ? PFERR_FETCH_MASK
: 0;
6813 /* ept page table entry is present? */
6814 error_code
|= (exit_qualification
&
6815 (EPT_VIOLATION_READABLE
| EPT_VIOLATION_WRITABLE
|
6816 EPT_VIOLATION_EXECUTABLE
))
6817 ? PFERR_PRESENT_MASK
: 0;
6819 error_code
|= (exit_qualification
& 0x100) != 0 ?
6820 PFERR_GUEST_FINAL_MASK
: PFERR_GUEST_PAGE_MASK
;
6822 vcpu
->arch
.exit_qualification
= exit_qualification
;
6823 return kvm_mmu_page_fault(vcpu
, gpa
, error_code
, NULL
, 0);
6826 static int handle_ept_misconfig(struct kvm_vcpu
*vcpu
)
6832 * A nested guest cannot optimize MMIO vmexits, because we have an
6833 * nGPA here instead of the required GPA.
6835 gpa
= vmcs_read64(GUEST_PHYSICAL_ADDRESS
);
6836 if (!is_guest_mode(vcpu
) &&
6837 !kvm_io_bus_write(vcpu
, KVM_FAST_MMIO_BUS
, gpa
, 0, NULL
)) {
6838 trace_kvm_fast_mmio(gpa
);
6840 * Doing kvm_skip_emulated_instruction() depends on undefined
6841 * behavior: Intel's manual doesn't mandate
6842 * VM_EXIT_INSTRUCTION_LEN to be set in VMCS when EPT MISCONFIG
6843 * occurs and while on real hardware it was observed to be set,
6844 * other hypervisors (namely Hyper-V) don't set it, we end up
6845 * advancing IP with some random value. Disable fast mmio when
6846 * running nested and keep it for real hardware in hope that
6847 * VM_EXIT_INSTRUCTION_LEN will always be set correctly.
6849 if (!static_cpu_has(X86_FEATURE_HYPERVISOR
))
6850 return kvm_skip_emulated_instruction(vcpu
);
6852 return x86_emulate_instruction(vcpu
, gpa
, EMULTYPE_SKIP
,
6853 NULL
, 0) == EMULATE_DONE
;
6856 ret
= kvm_mmu_page_fault(vcpu
, gpa
, PFERR_RSVD_MASK
, NULL
, 0);
6860 /* It is the real ept misconfig */
6863 vcpu
->run
->exit_reason
= KVM_EXIT_UNKNOWN
;
6864 vcpu
->run
->hw
.hardware_exit_reason
= EXIT_REASON_EPT_MISCONFIG
;
6869 static int handle_nmi_window(struct kvm_vcpu
*vcpu
)
6871 WARN_ON_ONCE(!enable_vnmi
);
6872 vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL
,
6873 CPU_BASED_VIRTUAL_NMI_PENDING
);
6874 ++vcpu
->stat
.nmi_window_exits
;
6875 kvm_make_request(KVM_REQ_EVENT
, vcpu
);
6880 static int handle_invalid_guest_state(struct kvm_vcpu
*vcpu
)
6882 struct vcpu_vmx
*vmx
= to_vmx(vcpu
);
6883 enum emulation_result err
= EMULATE_DONE
;
6886 bool intr_window_requested
;
6887 unsigned count
= 130;
6889 cpu_exec_ctrl
= vmcs_read32(CPU_BASED_VM_EXEC_CONTROL
);
6890 intr_window_requested
= cpu_exec_ctrl
& CPU_BASED_VIRTUAL_INTR_PENDING
;
6892 while (vmx
->emulation_required
&& count
-- != 0) {
6893 if (intr_window_requested
&& vmx_interrupt_allowed(vcpu
))
6894 return handle_interrupt_window(&vmx
->vcpu
);
6896 if (kvm_test_request(KVM_REQ_EVENT
, vcpu
))
6899 err
= emulate_instruction(vcpu
, 0);
6901 if (err
== EMULATE_USER_EXIT
) {
6902 ++vcpu
->stat
.mmio_exits
;
6907 if (err
!= EMULATE_DONE
) {
6908 vcpu
->run
->exit_reason
= KVM_EXIT_INTERNAL_ERROR
;
6909 vcpu
->run
->internal
.suberror
= KVM_INTERNAL_ERROR_EMULATION
;
6910 vcpu
->run
->internal
.ndata
= 0;
6914 if (vcpu
->arch
.halt_request
) {
6915 vcpu
->arch
.halt_request
= 0;
6916 ret
= kvm_vcpu_halt(vcpu
);
6920 if (signal_pending(current
))
6930 static int __grow_ple_window(int val
)
6932 if (ple_window_grow
< 1)
6935 val
= min(val
, ple_window_actual_max
);
6937 if (ple_window_grow
< ple_window
)
6938 val
*= ple_window_grow
;
6940 val
+= ple_window_grow
;
6945 static int __shrink_ple_window(int val
, int modifier
, int minimum
)
6950 if (modifier
< ple_window
)
6955 return max(val
, minimum
);
6958 static void grow_ple_window(struct kvm_vcpu
*vcpu
)
6960 struct vcpu_vmx
*vmx
= to_vmx(vcpu
);
6961 int old
= vmx
->ple_window
;
6963 vmx
->ple_window
= __grow_ple_window(old
);
6965 if (vmx
->ple_window
!= old
)
6966 vmx
->ple_window_dirty
= true;
6968 trace_kvm_ple_window_grow(vcpu
->vcpu_id
, vmx
->ple_window
, old
);
6971 static void shrink_ple_window(struct kvm_vcpu
*vcpu
)
6973 struct vcpu_vmx
*vmx
= to_vmx(vcpu
);
6974 int old
= vmx
->ple_window
;
6976 vmx
->ple_window
= __shrink_ple_window(old
,
6977 ple_window_shrink
, ple_window
);
6979 if (vmx
->ple_window
!= old
)
6980 vmx
->ple_window_dirty
= true;
6982 trace_kvm_ple_window_shrink(vcpu
->vcpu_id
, vmx
->ple_window
, old
);
6986 * ple_window_actual_max is computed to be one grow_ple_window() below
6987 * ple_window_max. (See __grow_ple_window for the reason.)
6988 * This prevents overflows, because ple_window_max is int.
6989 * ple_window_max effectively rounded down to a multiple of ple_window_grow in
6991 * ple_window_max is also prevented from setting vmx->ple_window < ple_window.
6993 static void update_ple_window_actual_max(void)
6995 ple_window_actual_max
=
6996 __shrink_ple_window(max(ple_window_max
, ple_window
),
6997 ple_window_grow
, INT_MIN
);
7001 * Handler for POSTED_INTERRUPT_WAKEUP_VECTOR.
7003 static void wakeup_handler(void)
7005 struct kvm_vcpu
*vcpu
;
7006 int cpu
= smp_processor_id();
7008 spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock
, cpu
));
7009 list_for_each_entry(vcpu
, &per_cpu(blocked_vcpu_on_cpu
, cpu
),
7010 blocked_vcpu_list
) {
7011 struct pi_desc
*pi_desc
= vcpu_to_pi_desc(vcpu
);
7013 if (pi_test_on(pi_desc
) == 1)
7014 kvm_vcpu_kick(vcpu
);
7016 spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock
, cpu
));
7019 void vmx_enable_tdp(void)
7021 kvm_mmu_set_mask_ptes(VMX_EPT_READABLE_MASK
,
7022 enable_ept_ad_bits
? VMX_EPT_ACCESS_BIT
: 0ull,
7023 enable_ept_ad_bits
? VMX_EPT_DIRTY_BIT
: 0ull,
7024 0ull, VMX_EPT_EXECUTABLE_MASK
,
7025 cpu_has_vmx_ept_execute_only() ? 0ull : VMX_EPT_READABLE_MASK
,
7026 VMX_EPT_RWX_MASK
, 0ull);
7028 ept_set_mmio_spte_mask();
7032 static __init
int hardware_setup(void)
7036 rdmsrl_safe(MSR_EFER
, &host_efer
);
7038 for (i
= 0; i
< ARRAY_SIZE(vmx_msr_index
); ++i
)
7039 kvm_define_shared_msr(i
, vmx_msr_index
[i
]);
7041 for (i
= 0; i
< VMX_BITMAP_NR
; i
++) {
7042 vmx_bitmap
[i
] = (unsigned long *)__get_free_page(GFP_KERNEL
);
7047 memset(vmx_vmread_bitmap
, 0xff, PAGE_SIZE
);
7048 memset(vmx_vmwrite_bitmap
, 0xff, PAGE_SIZE
);
7050 memset(vmx_io_bitmap_a
, 0xff, PAGE_SIZE
);
7052 memset(vmx_io_bitmap_b
, 0xff, PAGE_SIZE
);
7054 if (setup_vmcs_config(&vmcs_config
) < 0) {
7059 if (boot_cpu_has(X86_FEATURE_NX
))
7060 kvm_enable_efer_bits(EFER_NX
);
7062 if (!cpu_has_vmx_vpid() || !cpu_has_vmx_invvpid() ||
7063 !(cpu_has_vmx_invvpid_single() || cpu_has_vmx_invvpid_global()))
7066 if (!cpu_has_vmx_shadow_vmcs())
7067 enable_shadow_vmcs
= 0;
7068 if (enable_shadow_vmcs
)
7069 init_vmcs_shadow_fields();
7071 if (!cpu_has_vmx_ept() ||
7072 !cpu_has_vmx_ept_4levels() ||
7073 !cpu_has_vmx_ept_mt_wb() ||
7074 !cpu_has_vmx_invept_global())
7077 if (!cpu_has_vmx_ept_ad_bits() || !enable_ept
)
7078 enable_ept_ad_bits
= 0;
7080 if (!cpu_has_vmx_unrestricted_guest() || !enable_ept
)
7081 enable_unrestricted_guest
= 0;
7083 if (!cpu_has_vmx_flexpriority())
7084 flexpriority_enabled
= 0;
7086 if (!cpu_has_virtual_nmis())
7090 * set_apic_access_page_addr() is used to reload apic access
7091 * page upon invalidation. No need to do anything if not
7092 * using the APIC_ACCESS_ADDR VMCS field.
7094 if (!flexpriority_enabled
)
7095 kvm_x86_ops
->set_apic_access_page_addr
= NULL
;
7097 if (!cpu_has_vmx_tpr_shadow())
7098 kvm_x86_ops
->update_cr8_intercept
= NULL
;
7100 if (enable_ept
&& !cpu_has_vmx_ept_2m_page())
7101 kvm_disable_largepages();
7103 if (!cpu_has_vmx_ple()) {
7106 ple_window_grow
= 0;
7108 ple_window_shrink
= 0;
7111 if (!cpu_has_vmx_apicv()) {
7113 kvm_x86_ops
->sync_pir_to_irr
= NULL
;
7116 if (cpu_has_vmx_tsc_scaling()) {
7117 kvm_has_tsc_control
= true;
7118 kvm_max_tsc_scaling_ratio
= KVM_VMX_TSC_MULTIPLIER_MAX
;
7119 kvm_tsc_scaling_ratio_frac_bits
= 48;
7122 set_bit(0, vmx_vpid_bitmap
); /* 0 is reserved for host */
7129 update_ple_window_actual_max();
7132 * Only enable PML when hardware supports PML feature, and both EPT
7133 * and EPT A/D bit features are enabled -- PML depends on them to work.
7135 if (!enable_ept
|| !enable_ept_ad_bits
|| !cpu_has_vmx_pml())
7139 kvm_x86_ops
->slot_enable_log_dirty
= NULL
;
7140 kvm_x86_ops
->slot_disable_log_dirty
= NULL
;
7141 kvm_x86_ops
->flush_log_dirty
= NULL
;
7142 kvm_x86_ops
->enable_log_dirty_pt_masked
= NULL
;
7145 if (cpu_has_vmx_preemption_timer() && enable_preemption_timer
) {
7148 rdmsrl(MSR_IA32_VMX_MISC
, vmx_msr
);
7149 cpu_preemption_timer_multi
=
7150 vmx_msr
& VMX_MISC_PREEMPTION_TIMER_RATE_MASK
;
7152 kvm_x86_ops
->set_hv_timer
= NULL
;
7153 kvm_x86_ops
->cancel_hv_timer
= NULL
;
7156 kvm_set_posted_intr_wakeup_handler(wakeup_handler
);
7158 kvm_mce_cap_supported
|= MCG_LMCE_P
;
7160 return alloc_kvm_area();
7163 for (i
= 0; i
< VMX_BITMAP_NR
; i
++)
7164 free_page((unsigned long)vmx_bitmap
[i
]);
7169 static __exit
void hardware_unsetup(void)
7173 for (i
= 0; i
< VMX_BITMAP_NR
; i
++)
7174 free_page((unsigned long)vmx_bitmap
[i
]);
7180 * Indicate a busy-waiting vcpu in spinlock. We do not enable the PAUSE
7181 * exiting, so only get here on cpu with PAUSE-Loop-Exiting.
7183 static int handle_pause(struct kvm_vcpu
*vcpu
)
7186 grow_ple_window(vcpu
);
7189 * Intel sdm vol3 ch-25.1.3 says: The "PAUSE-loop exiting"
7190 * VM-execution control is ignored if CPL > 0. OTOH, KVM
7191 * never set PAUSE_EXITING and just set PLE if supported,
7192 * so the vcpu must be CPL=0 if it gets a PAUSE exit.
7194 kvm_vcpu_on_spin(vcpu
, true);
7195 return kvm_skip_emulated_instruction(vcpu
);
7198 static int handle_nop(struct kvm_vcpu
*vcpu
)
7200 return kvm_skip_emulated_instruction(vcpu
);
7203 static int handle_mwait(struct kvm_vcpu
*vcpu
)
7205 printk_once(KERN_WARNING
"kvm: MWAIT instruction emulated as NOP!\n");
7206 return handle_nop(vcpu
);
7209 static int handle_invalid_op(struct kvm_vcpu
*vcpu
)
7211 kvm_queue_exception(vcpu
, UD_VECTOR
);
7215 static int handle_monitor_trap(struct kvm_vcpu
*vcpu
)
7220 static int handle_monitor(struct kvm_vcpu
*vcpu
)
7222 printk_once(KERN_WARNING
"kvm: MONITOR instruction emulated as NOP!\n");
7223 return handle_nop(vcpu
);
7227 * The following 3 functions, nested_vmx_succeed()/failValid()/failInvalid(),
7228 * set the success or error code of an emulated VMX instruction, as specified
7229 * by Vol 2B, VMX Instruction Reference, "Conventions".
7231 static void nested_vmx_succeed(struct kvm_vcpu
*vcpu
)
7233 vmx_set_rflags(vcpu
, vmx_get_rflags(vcpu
)
7234 & ~(X86_EFLAGS_CF
| X86_EFLAGS_PF
| X86_EFLAGS_AF
|
7235 X86_EFLAGS_ZF
| X86_EFLAGS_SF
| X86_EFLAGS_OF
));
7238 static void nested_vmx_failInvalid(struct kvm_vcpu
*vcpu
)
7240 vmx_set_rflags(vcpu
, (vmx_get_rflags(vcpu
)
7241 & ~(X86_EFLAGS_PF
| X86_EFLAGS_AF
| X86_EFLAGS_ZF
|
7242 X86_EFLAGS_SF
| X86_EFLAGS_OF
))
7246 static void nested_vmx_failValid(struct kvm_vcpu
*vcpu
,
7247 u32 vm_instruction_error
)
7249 if (to_vmx(vcpu
)->nested
.current_vmptr
== -1ull) {
7251 * failValid writes the error number to the current VMCS, which
7252 * can't be done there isn't a current VMCS.
7254 nested_vmx_failInvalid(vcpu
);
7257 vmx_set_rflags(vcpu
, (vmx_get_rflags(vcpu
)
7258 & ~(X86_EFLAGS_CF
| X86_EFLAGS_PF
| X86_EFLAGS_AF
|
7259 X86_EFLAGS_SF
| X86_EFLAGS_OF
))
7261 get_vmcs12(vcpu
)->vm_instruction_error
= vm_instruction_error
;
7263 * We don't need to force a shadow sync because
7264 * VM_INSTRUCTION_ERROR is not shadowed
7268 static void nested_vmx_abort(struct kvm_vcpu
*vcpu
, u32 indicator
)
7270 /* TODO: not to reset guest simply here. */
7271 kvm_make_request(KVM_REQ_TRIPLE_FAULT
, vcpu
);
7272 pr_debug_ratelimited("kvm: nested vmx abort, indicator %d\n", indicator
);
7275 static enum hrtimer_restart
vmx_preemption_timer_fn(struct hrtimer
*timer
)
7277 struct vcpu_vmx
*vmx
=
7278 container_of(timer
, struct vcpu_vmx
, nested
.preemption_timer
);
7280 vmx
->nested
.preemption_timer_expired
= true;
7281 kvm_make_request(KVM_REQ_EVENT
, &vmx
->vcpu
);
7282 kvm_vcpu_kick(&vmx
->vcpu
);
7284 return HRTIMER_NORESTART
;
7288 * Decode the memory-address operand of a vmx instruction, as recorded on an
7289 * exit caused by such an instruction (run by a guest hypervisor).
7290 * On success, returns 0. When the operand is invalid, returns 1 and throws
7293 static int get_vmx_mem_address(struct kvm_vcpu
*vcpu
,
7294 unsigned long exit_qualification
,
7295 u32 vmx_instruction_info
, bool wr
, gva_t
*ret
)
7299 struct kvm_segment s
;
7302 * According to Vol. 3B, "Information for VM Exits Due to Instruction
7303 * Execution", on an exit, vmx_instruction_info holds most of the
7304 * addressing components of the operand. Only the displacement part
7305 * is put in exit_qualification (see 3B, "Basic VM-Exit Information").
7306 * For how an actual address is calculated from all these components,
7307 * refer to Vol. 1, "Operand Addressing".
7309 int scaling
= vmx_instruction_info
& 3;
7310 int addr_size
= (vmx_instruction_info
>> 7) & 7;
7311 bool is_reg
= vmx_instruction_info
& (1u << 10);
7312 int seg_reg
= (vmx_instruction_info
>> 15) & 7;
7313 int index_reg
= (vmx_instruction_info
>> 18) & 0xf;
7314 bool index_is_valid
= !(vmx_instruction_info
& (1u << 22));
7315 int base_reg
= (vmx_instruction_info
>> 23) & 0xf;
7316 bool base_is_valid
= !(vmx_instruction_info
& (1u << 27));
7319 kvm_queue_exception(vcpu
, UD_VECTOR
);
7323 /* Addr = segment_base + offset */
7324 /* offset = base + [index * scale] + displacement */
7325 off
= exit_qualification
; /* holds the displacement */
7327 off
+= kvm_register_read(vcpu
, base_reg
);
7329 off
+= kvm_register_read(vcpu
, index_reg
)<<scaling
;
7330 vmx_get_segment(vcpu
, &s
, seg_reg
);
7331 *ret
= s
.base
+ off
;
7333 if (addr_size
== 1) /* 32 bit */
7336 /* Checks for #GP/#SS exceptions. */
7338 if (is_long_mode(vcpu
)) {
7339 /* Long mode: #GP(0)/#SS(0) if the memory address is in a
7340 * non-canonical form. This is the only check on the memory
7341 * destination for long mode!
7343 exn
= is_noncanonical_address(*ret
, vcpu
);
7344 } else if (is_protmode(vcpu
)) {
7345 /* Protected mode: apply checks for segment validity in the
7347 * - segment type check (#GP(0) may be thrown)
7348 * - usability check (#GP(0)/#SS(0))
7349 * - limit check (#GP(0)/#SS(0))
7352 /* #GP(0) if the destination operand is located in a
7353 * read-only data segment or any code segment.
7355 exn
= ((s
.type
& 0xa) == 0 || (s
.type
& 8));
7357 /* #GP(0) if the source operand is located in an
7358 * execute-only code segment
7360 exn
= ((s
.type
& 0xa) == 8);
7362 kvm_queue_exception_e(vcpu
, GP_VECTOR
, 0);
7365 /* Protected mode: #GP(0)/#SS(0) if the segment is unusable.
7367 exn
= (s
.unusable
!= 0);
7368 /* Protected mode: #GP(0)/#SS(0) if the memory
7369 * operand is outside the segment limit.
7371 exn
= exn
|| (off
+ sizeof(u64
) > s
.limit
);
7374 kvm_queue_exception_e(vcpu
,
7375 seg_reg
== VCPU_SREG_SS
?
7376 SS_VECTOR
: GP_VECTOR
,
7384 static int nested_vmx_get_vmptr(struct kvm_vcpu
*vcpu
, gpa_t
*vmpointer
)
7387 struct x86_exception e
;
7389 if (get_vmx_mem_address(vcpu
, vmcs_readl(EXIT_QUALIFICATION
),
7390 vmcs_read32(VMX_INSTRUCTION_INFO
), false, &gva
))
7393 if (kvm_read_guest_virt(&vcpu
->arch
.emulate_ctxt
, gva
, vmpointer
,
7394 sizeof(*vmpointer
), &e
)) {
7395 kvm_inject_page_fault(vcpu
, &e
);
7402 static int enter_vmx_operation(struct kvm_vcpu
*vcpu
)
7404 struct vcpu_vmx
*vmx
= to_vmx(vcpu
);
7405 struct vmcs
*shadow_vmcs
;
7408 r
= alloc_loaded_vmcs(&vmx
->nested
.vmcs02
);
7412 vmx
->nested
.cached_vmcs12
= kmalloc(VMCS12_SIZE
, GFP_KERNEL
);
7413 if (!vmx
->nested
.cached_vmcs12
)
7414 goto out_cached_vmcs12
;
7416 if (enable_shadow_vmcs
) {
7417 shadow_vmcs
= alloc_vmcs();
7419 goto out_shadow_vmcs
;
7420 /* mark vmcs as shadow */
7421 shadow_vmcs
->revision_id
|= (1u << 31);
7422 /* init shadow vmcs */
7423 vmcs_clear(shadow_vmcs
);
7424 vmx
->vmcs01
.shadow_vmcs
= shadow_vmcs
;
7427 hrtimer_init(&vmx
->nested
.preemption_timer
, CLOCK_MONOTONIC
,
7428 HRTIMER_MODE_REL_PINNED
);
7429 vmx
->nested
.preemption_timer
.function
= vmx_preemption_timer_fn
;
7431 vmx
->nested
.vmxon
= true;
7435 kfree(vmx
->nested
.cached_vmcs12
);
7438 free_loaded_vmcs(&vmx
->nested
.vmcs02
);
7445 * Emulate the VMXON instruction.
7446 * Currently, we just remember that VMX is active, and do not save or even
7447 * inspect the argument to VMXON (the so-called "VMXON pointer") because we
7448 * do not currently need to store anything in that guest-allocated memory
7449 * region. Consequently, VMCLEAR and VMPTRLD also do not verify that the their
7450 * argument is different from the VMXON pointer (which the spec says they do).
7452 static int handle_vmon(struct kvm_vcpu
*vcpu
)
7457 struct vcpu_vmx
*vmx
= to_vmx(vcpu
);
7458 const u64 VMXON_NEEDED_FEATURES
= FEATURE_CONTROL_LOCKED
7459 | FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX
;
7462 * The Intel VMX Instruction Reference lists a bunch of bits that are
7463 * prerequisite to running VMXON, most notably cr4.VMXE must be set to
7464 * 1 (see vmx_set_cr4() for when we allow the guest to set this).
7465 * Otherwise, we should fail with #UD. But most faulting conditions
7466 * have already been checked by hardware, prior to the VM-exit for
7467 * VMXON. We do test guest cr4.VMXE because processor CR4 always has
7468 * that bit set to 1 in non-root mode.
7470 if (!kvm_read_cr4_bits(vcpu
, X86_CR4_VMXE
)) {
7471 kvm_queue_exception(vcpu
, UD_VECTOR
);
7475 if (vmx
->nested
.vmxon
) {
7476 nested_vmx_failValid(vcpu
, VMXERR_VMXON_IN_VMX_ROOT_OPERATION
);
7477 return kvm_skip_emulated_instruction(vcpu
);
7480 if ((vmx
->msr_ia32_feature_control
& VMXON_NEEDED_FEATURES
)
7481 != VMXON_NEEDED_FEATURES
) {
7482 kvm_inject_gp(vcpu
, 0);
7486 if (nested_vmx_get_vmptr(vcpu
, &vmptr
))
7491 * The first 4 bytes of VMXON region contain the supported
7492 * VMCS revision identifier
7494 * Note - IA32_VMX_BASIC[48] will never be 1 for the nested case;
7495 * which replaces physical address width with 32
7497 if (!PAGE_ALIGNED(vmptr
) || (vmptr
>> cpuid_maxphyaddr(vcpu
))) {
7498 nested_vmx_failInvalid(vcpu
);
7499 return kvm_skip_emulated_instruction(vcpu
);
7502 page
= kvm_vcpu_gpa_to_page(vcpu
, vmptr
);
7503 if (is_error_page(page
)) {
7504 nested_vmx_failInvalid(vcpu
);
7505 return kvm_skip_emulated_instruction(vcpu
);
7507 if (*(u32
*)kmap(page
) != VMCS12_REVISION
) {
7509 kvm_release_page_clean(page
);
7510 nested_vmx_failInvalid(vcpu
);
7511 return kvm_skip_emulated_instruction(vcpu
);
7514 kvm_release_page_clean(page
);
7516 vmx
->nested
.vmxon_ptr
= vmptr
;
7517 ret
= enter_vmx_operation(vcpu
);
7521 nested_vmx_succeed(vcpu
);
7522 return kvm_skip_emulated_instruction(vcpu
);
7526 * Intel's VMX Instruction Reference specifies a common set of prerequisites
7527 * for running VMX instructions (except VMXON, whose prerequisites are
7528 * slightly different). It also specifies what exception to inject otherwise.
7529 * Note that many of these exceptions have priority over VM exits, so they
7530 * don't have to be checked again here.
7532 static int nested_vmx_check_permission(struct kvm_vcpu
*vcpu
)
7534 if (!to_vmx(vcpu
)->nested
.vmxon
) {
7535 kvm_queue_exception(vcpu
, UD_VECTOR
);
7541 static void vmx_disable_shadow_vmcs(struct vcpu_vmx
*vmx
)
7543 vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL
, SECONDARY_EXEC_SHADOW_VMCS
);
7544 vmcs_write64(VMCS_LINK_POINTER
, -1ull);
7547 static inline void nested_release_vmcs12(struct vcpu_vmx
*vmx
)
7549 if (vmx
->nested
.current_vmptr
== -1ull)
7552 if (enable_shadow_vmcs
) {
7553 /* copy to memory all shadowed fields in case
7554 they were modified */
7555 copy_shadow_to_vmcs12(vmx
);
7556 vmx
->nested
.sync_shadow_vmcs
= false;
7557 vmx_disable_shadow_vmcs(vmx
);
7559 vmx
->nested
.posted_intr_nv
= -1;
7561 /* Flush VMCS12 to guest memory */
7562 kvm_vcpu_write_guest_page(&vmx
->vcpu
,
7563 vmx
->nested
.current_vmptr
>> PAGE_SHIFT
,
7564 vmx
->nested
.cached_vmcs12
, 0, VMCS12_SIZE
);
7566 vmx
->nested
.current_vmptr
= -1ull;
7570 * Free whatever needs to be freed from vmx->nested when L1 goes down, or
7571 * just stops using VMX.
7573 static void free_nested(struct vcpu_vmx
*vmx
)
7575 if (!vmx
->nested
.vmxon
&& !vmx
->nested
.smm
.vmxon
)
7578 vmx
->nested
.vmxon
= false;
7579 vmx
->nested
.smm
.vmxon
= false;
7580 free_vpid(vmx
->nested
.vpid02
);
7581 vmx
->nested
.posted_intr_nv
= -1;
7582 vmx
->nested
.current_vmptr
= -1ull;
7583 if (enable_shadow_vmcs
) {
7584 vmx_disable_shadow_vmcs(vmx
);
7585 vmcs_clear(vmx
->vmcs01
.shadow_vmcs
);
7586 free_vmcs(vmx
->vmcs01
.shadow_vmcs
);
7587 vmx
->vmcs01
.shadow_vmcs
= NULL
;
7589 kfree(vmx
->nested
.cached_vmcs12
);
7590 /* Unpin physical memory we referred to in the vmcs02 */
7591 if (vmx
->nested
.apic_access_page
) {
7592 kvm_release_page_dirty(vmx
->nested
.apic_access_page
);
7593 vmx
->nested
.apic_access_page
= NULL
;
7595 if (vmx
->nested
.virtual_apic_page
) {
7596 kvm_release_page_dirty(vmx
->nested
.virtual_apic_page
);
7597 vmx
->nested
.virtual_apic_page
= NULL
;
7599 if (vmx
->nested
.pi_desc_page
) {
7600 kunmap(vmx
->nested
.pi_desc_page
);
7601 kvm_release_page_dirty(vmx
->nested
.pi_desc_page
);
7602 vmx
->nested
.pi_desc_page
= NULL
;
7603 vmx
->nested
.pi_desc
= NULL
;
7606 free_loaded_vmcs(&vmx
->nested
.vmcs02
);
7609 /* Emulate the VMXOFF instruction */
7610 static int handle_vmoff(struct kvm_vcpu
*vcpu
)
7612 if (!nested_vmx_check_permission(vcpu
))
7614 free_nested(to_vmx(vcpu
));
7615 nested_vmx_succeed(vcpu
);
7616 return kvm_skip_emulated_instruction(vcpu
);
7619 /* Emulate the VMCLEAR instruction */
7620 static int handle_vmclear(struct kvm_vcpu
*vcpu
)
7622 struct vcpu_vmx
*vmx
= to_vmx(vcpu
);
7626 if (!nested_vmx_check_permission(vcpu
))
7629 if (nested_vmx_get_vmptr(vcpu
, &vmptr
))
7632 if (!PAGE_ALIGNED(vmptr
) || (vmptr
>> cpuid_maxphyaddr(vcpu
))) {
7633 nested_vmx_failValid(vcpu
, VMXERR_VMCLEAR_INVALID_ADDRESS
);
7634 return kvm_skip_emulated_instruction(vcpu
);
7637 if (vmptr
== vmx
->nested
.vmxon_ptr
) {
7638 nested_vmx_failValid(vcpu
, VMXERR_VMCLEAR_VMXON_POINTER
);
7639 return kvm_skip_emulated_instruction(vcpu
);
7642 if (vmptr
== vmx
->nested
.current_vmptr
)
7643 nested_release_vmcs12(vmx
);
7645 kvm_vcpu_write_guest(vcpu
,
7646 vmptr
+ offsetof(struct vmcs12
, launch_state
),
7647 &zero
, sizeof(zero
));
7649 nested_vmx_succeed(vcpu
);
7650 return kvm_skip_emulated_instruction(vcpu
);
7653 static int nested_vmx_run(struct kvm_vcpu
*vcpu
, bool launch
);
7655 /* Emulate the VMLAUNCH instruction */
7656 static int handle_vmlaunch(struct kvm_vcpu
*vcpu
)
7658 return nested_vmx_run(vcpu
, true);
7661 /* Emulate the VMRESUME instruction */
7662 static int handle_vmresume(struct kvm_vcpu
*vcpu
)
7665 return nested_vmx_run(vcpu
, false);
7669 * Read a vmcs12 field. Since these can have varying lengths and we return
7670 * one type, we chose the biggest type (u64) and zero-extend the return value
7671 * to that size. Note that the caller, handle_vmread, might need to use only
7672 * some of the bits we return here (e.g., on 32-bit guests, only 32 bits of
7673 * 64-bit fields are to be returned).
7675 static inline int vmcs12_read_any(struct kvm_vcpu
*vcpu
,
7676 unsigned long field
, u64
*ret
)
7678 short offset
= vmcs_field_to_offset(field
);
7684 p
= ((char *)(get_vmcs12(vcpu
))) + offset
;
7686 switch (vmcs_field_type(field
)) {
7687 case VMCS_FIELD_TYPE_NATURAL_WIDTH
:
7688 *ret
= *((natural_width
*)p
);
7690 case VMCS_FIELD_TYPE_U16
:
7693 case VMCS_FIELD_TYPE_U32
:
7696 case VMCS_FIELD_TYPE_U64
:
7706 static inline int vmcs12_write_any(struct kvm_vcpu
*vcpu
,
7707 unsigned long field
, u64 field_value
){
7708 short offset
= vmcs_field_to_offset(field
);
7709 char *p
= ((char *) get_vmcs12(vcpu
)) + offset
;
7713 switch (vmcs_field_type(field
)) {
7714 case VMCS_FIELD_TYPE_U16
:
7715 *(u16
*)p
= field_value
;
7717 case VMCS_FIELD_TYPE_U32
:
7718 *(u32
*)p
= field_value
;
7720 case VMCS_FIELD_TYPE_U64
:
7721 *(u64
*)p
= field_value
;
7723 case VMCS_FIELD_TYPE_NATURAL_WIDTH
:
7724 *(natural_width
*)p
= field_value
;
7733 static void copy_shadow_to_vmcs12(struct vcpu_vmx
*vmx
)
7736 unsigned long field
;
7738 struct vmcs
*shadow_vmcs
= vmx
->vmcs01
.shadow_vmcs
;
7739 const unsigned long *fields
= shadow_read_write_fields
;
7740 const int num_fields
= max_shadow_read_write_fields
;
7744 vmcs_load(shadow_vmcs
);
7746 for (i
= 0; i
< num_fields
; i
++) {
7748 switch (vmcs_field_type(field
)) {
7749 case VMCS_FIELD_TYPE_U16
:
7750 field_value
= vmcs_read16(field
);
7752 case VMCS_FIELD_TYPE_U32
:
7753 field_value
= vmcs_read32(field
);
7755 case VMCS_FIELD_TYPE_U64
:
7756 field_value
= vmcs_read64(field
);
7758 case VMCS_FIELD_TYPE_NATURAL_WIDTH
:
7759 field_value
= vmcs_readl(field
);
7765 vmcs12_write_any(&vmx
->vcpu
, field
, field_value
);
7768 vmcs_clear(shadow_vmcs
);
7769 vmcs_load(vmx
->loaded_vmcs
->vmcs
);
7774 static void copy_vmcs12_to_shadow(struct vcpu_vmx
*vmx
)
7776 const unsigned long *fields
[] = {
7777 shadow_read_write_fields
,
7778 shadow_read_only_fields
7780 const int max_fields
[] = {
7781 max_shadow_read_write_fields
,
7782 max_shadow_read_only_fields
7785 unsigned long field
;
7786 u64 field_value
= 0;
7787 struct vmcs
*shadow_vmcs
= vmx
->vmcs01
.shadow_vmcs
;
7789 vmcs_load(shadow_vmcs
);
7791 for (q
= 0; q
< ARRAY_SIZE(fields
); q
++) {
7792 for (i
= 0; i
< max_fields
[q
]; i
++) {
7793 field
= fields
[q
][i
];
7794 vmcs12_read_any(&vmx
->vcpu
, field
, &field_value
);
7796 switch (vmcs_field_type(field
)) {
7797 case VMCS_FIELD_TYPE_U16
:
7798 vmcs_write16(field
, (u16
)field_value
);
7800 case VMCS_FIELD_TYPE_U32
:
7801 vmcs_write32(field
, (u32
)field_value
);
7803 case VMCS_FIELD_TYPE_U64
:
7804 vmcs_write64(field
, (u64
)field_value
);
7806 case VMCS_FIELD_TYPE_NATURAL_WIDTH
:
7807 vmcs_writel(field
, (long)field_value
);
7816 vmcs_clear(shadow_vmcs
);
7817 vmcs_load(vmx
->loaded_vmcs
->vmcs
);
7821 * VMX instructions which assume a current vmcs12 (i.e., that VMPTRLD was
7822 * used before) all generate the same failure when it is missing.
7824 static int nested_vmx_check_vmcs12(struct kvm_vcpu
*vcpu
)
7826 struct vcpu_vmx
*vmx
= to_vmx(vcpu
);
7827 if (vmx
->nested
.current_vmptr
== -1ull) {
7828 nested_vmx_failInvalid(vcpu
);
7834 static int handle_vmread(struct kvm_vcpu
*vcpu
)
7836 unsigned long field
;
7838 unsigned long exit_qualification
= vmcs_readl(EXIT_QUALIFICATION
);
7839 u32 vmx_instruction_info
= vmcs_read32(VMX_INSTRUCTION_INFO
);
7842 if (!nested_vmx_check_permission(vcpu
))
7845 if (!nested_vmx_check_vmcs12(vcpu
))
7846 return kvm_skip_emulated_instruction(vcpu
);
7848 /* Decode instruction info and find the field to read */
7849 field
= kvm_register_readl(vcpu
, (((vmx_instruction_info
) >> 28) & 0xf));
7850 /* Read the field, zero-extended to a u64 field_value */
7851 if (vmcs12_read_any(vcpu
, field
, &field_value
) < 0) {
7852 nested_vmx_failValid(vcpu
, VMXERR_UNSUPPORTED_VMCS_COMPONENT
);
7853 return kvm_skip_emulated_instruction(vcpu
);
7856 * Now copy part of this value to register or memory, as requested.
7857 * Note that the number of bits actually copied is 32 or 64 depending
7858 * on the guest's mode (32 or 64 bit), not on the given field's length.
7860 if (vmx_instruction_info
& (1u << 10)) {
7861 kvm_register_writel(vcpu
, (((vmx_instruction_info
) >> 3) & 0xf),
7864 if (get_vmx_mem_address(vcpu
, exit_qualification
,
7865 vmx_instruction_info
, true, &gva
))
7867 /* _system ok, as hardware has verified cpl=0 */
7868 kvm_write_guest_virt_system(&vcpu
->arch
.emulate_ctxt
, gva
,
7869 &field_value
, (is_long_mode(vcpu
) ? 8 : 4), NULL
);
7872 nested_vmx_succeed(vcpu
);
7873 return kvm_skip_emulated_instruction(vcpu
);
7877 static int handle_vmwrite(struct kvm_vcpu
*vcpu
)
7879 unsigned long field
;
7881 unsigned long exit_qualification
= vmcs_readl(EXIT_QUALIFICATION
);
7882 u32 vmx_instruction_info
= vmcs_read32(VMX_INSTRUCTION_INFO
);
7883 /* The value to write might be 32 or 64 bits, depending on L1's long
7884 * mode, and eventually we need to write that into a field of several
7885 * possible lengths. The code below first zero-extends the value to 64
7886 * bit (field_value), and then copies only the appropriate number of
7887 * bits into the vmcs12 field.
7889 u64 field_value
= 0;
7890 struct x86_exception e
;
7892 if (!nested_vmx_check_permission(vcpu
))
7895 if (!nested_vmx_check_vmcs12(vcpu
))
7896 return kvm_skip_emulated_instruction(vcpu
);
7898 if (vmx_instruction_info
& (1u << 10))
7899 field_value
= kvm_register_readl(vcpu
,
7900 (((vmx_instruction_info
) >> 3) & 0xf));
7902 if (get_vmx_mem_address(vcpu
, exit_qualification
,
7903 vmx_instruction_info
, false, &gva
))
7905 if (kvm_read_guest_virt(&vcpu
->arch
.emulate_ctxt
, gva
,
7906 &field_value
, (is_64_bit_mode(vcpu
) ? 8 : 4), &e
)) {
7907 kvm_inject_page_fault(vcpu
, &e
);
7913 field
= kvm_register_readl(vcpu
, (((vmx_instruction_info
) >> 28) & 0xf));
7914 if (vmcs_field_readonly(field
)) {
7915 nested_vmx_failValid(vcpu
,
7916 VMXERR_VMWRITE_READ_ONLY_VMCS_COMPONENT
);
7917 return kvm_skip_emulated_instruction(vcpu
);
7920 if (vmcs12_write_any(vcpu
, field
, field_value
) < 0) {
7921 nested_vmx_failValid(vcpu
, VMXERR_UNSUPPORTED_VMCS_COMPONENT
);
7922 return kvm_skip_emulated_instruction(vcpu
);
7925 nested_vmx_succeed(vcpu
);
7926 return kvm_skip_emulated_instruction(vcpu
);
7929 static void set_current_vmptr(struct vcpu_vmx
*vmx
, gpa_t vmptr
)
7931 vmx
->nested
.current_vmptr
= vmptr
;
7932 if (enable_shadow_vmcs
) {
7933 vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL
,
7934 SECONDARY_EXEC_SHADOW_VMCS
);
7935 vmcs_write64(VMCS_LINK_POINTER
,
7936 __pa(vmx
->vmcs01
.shadow_vmcs
));
7937 vmx
->nested
.sync_shadow_vmcs
= true;
7941 /* Emulate the VMPTRLD instruction */
7942 static int handle_vmptrld(struct kvm_vcpu
*vcpu
)
7944 struct vcpu_vmx
*vmx
= to_vmx(vcpu
);
7947 if (!nested_vmx_check_permission(vcpu
))
7950 if (nested_vmx_get_vmptr(vcpu
, &vmptr
))
7953 if (!PAGE_ALIGNED(vmptr
) || (vmptr
>> cpuid_maxphyaddr(vcpu
))) {
7954 nested_vmx_failValid(vcpu
, VMXERR_VMPTRLD_INVALID_ADDRESS
);
7955 return kvm_skip_emulated_instruction(vcpu
);
7958 if (vmptr
== vmx
->nested
.vmxon_ptr
) {
7959 nested_vmx_failValid(vcpu
, VMXERR_VMPTRLD_VMXON_POINTER
);
7960 return kvm_skip_emulated_instruction(vcpu
);
7963 if (vmx
->nested
.current_vmptr
!= vmptr
) {
7964 struct vmcs12
*new_vmcs12
;
7966 page
= kvm_vcpu_gpa_to_page(vcpu
, vmptr
);
7967 if (is_error_page(page
)) {
7968 nested_vmx_failInvalid(vcpu
);
7969 return kvm_skip_emulated_instruction(vcpu
);
7971 new_vmcs12
= kmap(page
);
7972 if (new_vmcs12
->revision_id
!= VMCS12_REVISION
) {
7974 kvm_release_page_clean(page
);
7975 nested_vmx_failValid(vcpu
,
7976 VMXERR_VMPTRLD_INCORRECT_VMCS_REVISION_ID
);
7977 return kvm_skip_emulated_instruction(vcpu
);
7980 nested_release_vmcs12(vmx
);
7982 * Load VMCS12 from guest memory since it is not already
7985 memcpy(vmx
->nested
.cached_vmcs12
, new_vmcs12
, VMCS12_SIZE
);
7987 kvm_release_page_clean(page
);
7989 set_current_vmptr(vmx
, vmptr
);
7992 nested_vmx_succeed(vcpu
);
7993 return kvm_skip_emulated_instruction(vcpu
);
7996 /* Emulate the VMPTRST instruction */
7997 static int handle_vmptrst(struct kvm_vcpu
*vcpu
)
7999 unsigned long exit_qualification
= vmcs_readl(EXIT_QUALIFICATION
);
8000 u32 vmx_instruction_info
= vmcs_read32(VMX_INSTRUCTION_INFO
);
8002 struct x86_exception e
;
8004 if (!nested_vmx_check_permission(vcpu
))
8007 if (get_vmx_mem_address(vcpu
, exit_qualification
,
8008 vmx_instruction_info
, true, &vmcs_gva
))
8010 /* ok to use *_system, as hardware has verified cpl=0 */
8011 if (kvm_write_guest_virt_system(&vcpu
->arch
.emulate_ctxt
, vmcs_gva
,
8012 (void *)&to_vmx(vcpu
)->nested
.current_vmptr
,
8014 kvm_inject_page_fault(vcpu
, &e
);
8017 nested_vmx_succeed(vcpu
);
8018 return kvm_skip_emulated_instruction(vcpu
);
8021 /* Emulate the INVEPT instruction */
8022 static int handle_invept(struct kvm_vcpu
*vcpu
)
8024 struct vcpu_vmx
*vmx
= to_vmx(vcpu
);
8025 u32 vmx_instruction_info
, types
;
8028 struct x86_exception e
;
8033 if (!(vmx
->nested
.nested_vmx_secondary_ctls_high
&
8034 SECONDARY_EXEC_ENABLE_EPT
) ||
8035 !(vmx
->nested
.nested_vmx_ept_caps
& VMX_EPT_INVEPT_BIT
)) {
8036 kvm_queue_exception(vcpu
, UD_VECTOR
);
8040 if (!nested_vmx_check_permission(vcpu
))
8043 vmx_instruction_info
= vmcs_read32(VMX_INSTRUCTION_INFO
);
8044 type
= kvm_register_readl(vcpu
, (vmx_instruction_info
>> 28) & 0xf);
8046 types
= (vmx
->nested
.nested_vmx_ept_caps
>> VMX_EPT_EXTENT_SHIFT
) & 6;
8048 if (type
>= 32 || !(types
& (1 << type
))) {
8049 nested_vmx_failValid(vcpu
,
8050 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID
);
8051 return kvm_skip_emulated_instruction(vcpu
);
8054 /* According to the Intel VMX instruction reference, the memory
8055 * operand is read even if it isn't needed (e.g., for type==global)
8057 if (get_vmx_mem_address(vcpu
, vmcs_readl(EXIT_QUALIFICATION
),
8058 vmx_instruction_info
, false, &gva
))
8060 if (kvm_read_guest_virt(&vcpu
->arch
.emulate_ctxt
, gva
, &operand
,
8061 sizeof(operand
), &e
)) {
8062 kvm_inject_page_fault(vcpu
, &e
);
8067 case VMX_EPT_EXTENT_GLOBAL
:
8069 * TODO: track mappings and invalidate
8070 * single context requests appropriately
8072 case VMX_EPT_EXTENT_CONTEXT
:
8073 kvm_mmu_sync_roots(vcpu
);
8074 kvm_make_request(KVM_REQ_TLB_FLUSH
, vcpu
);
8075 nested_vmx_succeed(vcpu
);
8082 return kvm_skip_emulated_instruction(vcpu
);
8085 static int handle_invvpid(struct kvm_vcpu
*vcpu
)
8087 struct vcpu_vmx
*vmx
= to_vmx(vcpu
);
8088 u32 vmx_instruction_info
;
8089 unsigned long type
, types
;
8091 struct x86_exception e
;
8097 if (!(vmx
->nested
.nested_vmx_secondary_ctls_high
&
8098 SECONDARY_EXEC_ENABLE_VPID
) ||
8099 !(vmx
->nested
.nested_vmx_vpid_caps
& VMX_VPID_INVVPID_BIT
)) {
8100 kvm_queue_exception(vcpu
, UD_VECTOR
);
8104 if (!nested_vmx_check_permission(vcpu
))
8107 vmx_instruction_info
= vmcs_read32(VMX_INSTRUCTION_INFO
);
8108 type
= kvm_register_readl(vcpu
, (vmx_instruction_info
>> 28) & 0xf);
8110 types
= (vmx
->nested
.nested_vmx_vpid_caps
&
8111 VMX_VPID_EXTENT_SUPPORTED_MASK
) >> 8;
8113 if (type
>= 32 || !(types
& (1 << type
))) {
8114 nested_vmx_failValid(vcpu
,
8115 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID
);
8116 return kvm_skip_emulated_instruction(vcpu
);
8119 /* according to the intel vmx instruction reference, the memory
8120 * operand is read even if it isn't needed (e.g., for type==global)
8122 if (get_vmx_mem_address(vcpu
, vmcs_readl(EXIT_QUALIFICATION
),
8123 vmx_instruction_info
, false, &gva
))
8125 if (kvm_read_guest_virt(&vcpu
->arch
.emulate_ctxt
, gva
, &operand
,
8126 sizeof(operand
), &e
)) {
8127 kvm_inject_page_fault(vcpu
, &e
);
8130 if (operand
.vpid
>> 16) {
8131 nested_vmx_failValid(vcpu
,
8132 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID
);
8133 return kvm_skip_emulated_instruction(vcpu
);
8137 case VMX_VPID_EXTENT_INDIVIDUAL_ADDR
:
8138 if (is_noncanonical_address(operand
.gla
, vcpu
)) {
8139 nested_vmx_failValid(vcpu
,
8140 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID
);
8141 return kvm_skip_emulated_instruction(vcpu
);
8144 case VMX_VPID_EXTENT_SINGLE_CONTEXT
:
8145 case VMX_VPID_EXTENT_SINGLE_NON_GLOBAL
:
8146 if (!operand
.vpid
) {
8147 nested_vmx_failValid(vcpu
,
8148 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID
);
8149 return kvm_skip_emulated_instruction(vcpu
);
8152 case VMX_VPID_EXTENT_ALL_CONTEXT
:
8156 return kvm_skip_emulated_instruction(vcpu
);
8159 __vmx_flush_tlb(vcpu
, vmx
->nested
.vpid02
);
8160 nested_vmx_succeed(vcpu
);
8162 return kvm_skip_emulated_instruction(vcpu
);
8165 static int handle_pml_full(struct kvm_vcpu
*vcpu
)
8167 unsigned long exit_qualification
;
8169 trace_kvm_pml_full(vcpu
->vcpu_id
);
8171 exit_qualification
= vmcs_readl(EXIT_QUALIFICATION
);
8174 * PML buffer FULL happened while executing iret from NMI,
8175 * "blocked by NMI" bit has to be set before next VM entry.
8177 if (!(to_vmx(vcpu
)->idt_vectoring_info
& VECTORING_INFO_VALID_MASK
) &&
8179 (exit_qualification
& INTR_INFO_UNBLOCK_NMI
))
8180 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO
,
8181 GUEST_INTR_STATE_NMI
);
8184 * PML buffer already flushed at beginning of VMEXIT. Nothing to do
8185 * here.., and there's no userspace involvement needed for PML.
8190 static int handle_preemption_timer(struct kvm_vcpu
*vcpu
)
8192 kvm_lapic_expired_hv_timer(vcpu
);
8196 static bool valid_ept_address(struct kvm_vcpu
*vcpu
, u64 address
)
8198 struct vcpu_vmx
*vmx
= to_vmx(vcpu
);
8199 int maxphyaddr
= cpuid_maxphyaddr(vcpu
);
8201 /* Check for memory type validity */
8202 switch (address
& VMX_EPTP_MT_MASK
) {
8203 case VMX_EPTP_MT_UC
:
8204 if (!(vmx
->nested
.nested_vmx_ept_caps
& VMX_EPTP_UC_BIT
))
8207 case VMX_EPTP_MT_WB
:
8208 if (!(vmx
->nested
.nested_vmx_ept_caps
& VMX_EPTP_WB_BIT
))
8215 /* only 4 levels page-walk length are valid */
8216 if ((address
& VMX_EPTP_PWL_MASK
) != VMX_EPTP_PWL_4
)
8219 /* Reserved bits should not be set */
8220 if (address
>> maxphyaddr
|| ((address
>> 7) & 0x1f))
8223 /* AD, if set, should be supported */
8224 if (address
& VMX_EPTP_AD_ENABLE_BIT
) {
8225 if (!(vmx
->nested
.nested_vmx_ept_caps
& VMX_EPT_AD_BIT
))
8232 static int nested_vmx_eptp_switching(struct kvm_vcpu
*vcpu
,
8233 struct vmcs12
*vmcs12
)
8235 u32 index
= vcpu
->arch
.regs
[VCPU_REGS_RCX
];
8237 bool accessed_dirty
;
8238 struct kvm_mmu
*mmu
= vcpu
->arch
.walk_mmu
;
8240 if (!nested_cpu_has_eptp_switching(vmcs12
) ||
8241 !nested_cpu_has_ept(vmcs12
))
8244 if (index
>= VMFUNC_EPTP_ENTRIES
)
8248 if (kvm_vcpu_read_guest_page(vcpu
, vmcs12
->eptp_list_address
>> PAGE_SHIFT
,
8249 &address
, index
* 8, 8))
8252 accessed_dirty
= !!(address
& VMX_EPTP_AD_ENABLE_BIT
);
8255 * If the (L2) guest does a vmfunc to the currently
8256 * active ept pointer, we don't have to do anything else
8258 if (vmcs12
->ept_pointer
!= address
) {
8259 if (!valid_ept_address(vcpu
, address
))
8262 kvm_mmu_unload(vcpu
);
8263 mmu
->ept_ad
= accessed_dirty
;
8264 mmu
->base_role
.ad_disabled
= !accessed_dirty
;
8265 vmcs12
->ept_pointer
= address
;
8267 * TODO: Check what's the correct approach in case
8268 * mmu reload fails. Currently, we just let the next
8269 * reload potentially fail
8271 kvm_mmu_reload(vcpu
);
8277 static int handle_vmfunc(struct kvm_vcpu
*vcpu
)
8279 struct vcpu_vmx
*vmx
= to_vmx(vcpu
);
8280 struct vmcs12
*vmcs12
;
8281 u32 function
= vcpu
->arch
.regs
[VCPU_REGS_RAX
];
8284 * VMFUNC is only supported for nested guests, but we always enable the
8285 * secondary control for simplicity; for non-nested mode, fake that we
8286 * didn't by injecting #UD.
8288 if (!is_guest_mode(vcpu
)) {
8289 kvm_queue_exception(vcpu
, UD_VECTOR
);
8293 vmcs12
= get_vmcs12(vcpu
);
8294 if ((vmcs12
->vm_function_control
& (1 << function
)) == 0)
8299 if (nested_vmx_eptp_switching(vcpu
, vmcs12
))
8305 return kvm_skip_emulated_instruction(vcpu
);
8308 nested_vmx_vmexit(vcpu
, vmx
->exit_reason
,
8309 vmcs_read32(VM_EXIT_INTR_INFO
),
8310 vmcs_readl(EXIT_QUALIFICATION
));
8315 * The exit handlers return 1 if the exit was handled fully and guest execution
8316 * may resume. Otherwise they set the kvm_run parameter to indicate what needs
8317 * to be done to userspace and return 0.
8319 static int (*const kvm_vmx_exit_handlers
[])(struct kvm_vcpu
*vcpu
) = {
8320 [EXIT_REASON_EXCEPTION_NMI
] = handle_exception
,
8321 [EXIT_REASON_EXTERNAL_INTERRUPT
] = handle_external_interrupt
,
8322 [EXIT_REASON_TRIPLE_FAULT
] = handle_triple_fault
,
8323 [EXIT_REASON_NMI_WINDOW
] = handle_nmi_window
,
8324 [EXIT_REASON_IO_INSTRUCTION
] = handle_io
,
8325 [EXIT_REASON_CR_ACCESS
] = handle_cr
,
8326 [EXIT_REASON_DR_ACCESS
] = handle_dr
,
8327 [EXIT_REASON_CPUID
] = handle_cpuid
,
8328 [EXIT_REASON_MSR_READ
] = handle_rdmsr
,
8329 [EXIT_REASON_MSR_WRITE
] = handle_wrmsr
,
8330 [EXIT_REASON_PENDING_INTERRUPT
] = handle_interrupt_window
,
8331 [EXIT_REASON_HLT
] = handle_halt
,
8332 [EXIT_REASON_INVD
] = handle_invd
,
8333 [EXIT_REASON_INVLPG
] = handle_invlpg
,
8334 [EXIT_REASON_RDPMC
] = handle_rdpmc
,
8335 [EXIT_REASON_VMCALL
] = handle_vmcall
,
8336 [EXIT_REASON_VMCLEAR
] = handle_vmclear
,
8337 [EXIT_REASON_VMLAUNCH
] = handle_vmlaunch
,
8338 [EXIT_REASON_VMPTRLD
] = handle_vmptrld
,
8339 [EXIT_REASON_VMPTRST
] = handle_vmptrst
,
8340 [EXIT_REASON_VMREAD
] = handle_vmread
,
8341 [EXIT_REASON_VMRESUME
] = handle_vmresume
,
8342 [EXIT_REASON_VMWRITE
] = handle_vmwrite
,
8343 [EXIT_REASON_VMOFF
] = handle_vmoff
,
8344 [EXIT_REASON_VMON
] = handle_vmon
,
8345 [EXIT_REASON_TPR_BELOW_THRESHOLD
] = handle_tpr_below_threshold
,
8346 [EXIT_REASON_APIC_ACCESS
] = handle_apic_access
,
8347 [EXIT_REASON_APIC_WRITE
] = handle_apic_write
,
8348 [EXIT_REASON_EOI_INDUCED
] = handle_apic_eoi_induced
,
8349 [EXIT_REASON_WBINVD
] = handle_wbinvd
,
8350 [EXIT_REASON_XSETBV
] = handle_xsetbv
,
8351 [EXIT_REASON_TASK_SWITCH
] = handle_task_switch
,
8352 [EXIT_REASON_MCE_DURING_VMENTRY
] = handle_machine_check
,
8353 [EXIT_REASON_EPT_VIOLATION
] = handle_ept_violation
,
8354 [EXIT_REASON_EPT_MISCONFIG
] = handle_ept_misconfig
,
8355 [EXIT_REASON_PAUSE_INSTRUCTION
] = handle_pause
,
8356 [EXIT_REASON_MWAIT_INSTRUCTION
] = handle_mwait
,
8357 [EXIT_REASON_MONITOR_TRAP_FLAG
] = handle_monitor_trap
,
8358 [EXIT_REASON_MONITOR_INSTRUCTION
] = handle_monitor
,
8359 [EXIT_REASON_INVEPT
] = handle_invept
,
8360 [EXIT_REASON_INVVPID
] = handle_invvpid
,
8361 [EXIT_REASON_RDRAND
] = handle_invalid_op
,
8362 [EXIT_REASON_RDSEED
] = handle_invalid_op
,
8363 [EXIT_REASON_XSAVES
] = handle_xsaves
,
8364 [EXIT_REASON_XRSTORS
] = handle_xrstors
,
8365 [EXIT_REASON_PML_FULL
] = handle_pml_full
,
8366 [EXIT_REASON_VMFUNC
] = handle_vmfunc
,
8367 [EXIT_REASON_PREEMPTION_TIMER
] = handle_preemption_timer
,
8370 static const int kvm_vmx_max_exit_handlers
=
8371 ARRAY_SIZE(kvm_vmx_exit_handlers
);
8373 static bool nested_vmx_exit_handled_io(struct kvm_vcpu
*vcpu
,
8374 struct vmcs12
*vmcs12
)
8376 unsigned long exit_qualification
;
8377 gpa_t bitmap
, last_bitmap
;
8382 if (!nested_cpu_has(vmcs12
, CPU_BASED_USE_IO_BITMAPS
))
8383 return nested_cpu_has(vmcs12
, CPU_BASED_UNCOND_IO_EXITING
);
8385 exit_qualification
= vmcs_readl(EXIT_QUALIFICATION
);
8387 port
= exit_qualification
>> 16;
8388 size
= (exit_qualification
& 7) + 1;
8390 last_bitmap
= (gpa_t
)-1;
8395 bitmap
= vmcs12
->io_bitmap_a
;
8396 else if (port
< 0x10000)
8397 bitmap
= vmcs12
->io_bitmap_b
;
8400 bitmap
+= (port
& 0x7fff) / 8;
8402 if (last_bitmap
!= bitmap
)
8403 if (kvm_vcpu_read_guest(vcpu
, bitmap
, &b
, 1))
8405 if (b
& (1 << (port
& 7)))
8410 last_bitmap
= bitmap
;
8417 * Return 1 if we should exit from L2 to L1 to handle an MSR access access,
8418 * rather than handle it ourselves in L0. I.e., check whether L1 expressed
8419 * disinterest in the current event (read or write a specific MSR) by using an
8420 * MSR bitmap. This may be the case even when L0 doesn't use MSR bitmaps.
8422 static bool nested_vmx_exit_handled_msr(struct kvm_vcpu
*vcpu
,
8423 struct vmcs12
*vmcs12
, u32 exit_reason
)
8425 u32 msr_index
= vcpu
->arch
.regs
[VCPU_REGS_RCX
];
8428 if (!nested_cpu_has(vmcs12
, CPU_BASED_USE_MSR_BITMAPS
))
8432 * The MSR_BITMAP page is divided into four 1024-byte bitmaps,
8433 * for the four combinations of read/write and low/high MSR numbers.
8434 * First we need to figure out which of the four to use:
8436 bitmap
= vmcs12
->msr_bitmap
;
8437 if (exit_reason
== EXIT_REASON_MSR_WRITE
)
8439 if (msr_index
>= 0xc0000000) {
8440 msr_index
-= 0xc0000000;
8444 /* Then read the msr_index'th bit from this bitmap: */
8445 if (msr_index
< 1024*8) {
8447 if (kvm_vcpu_read_guest(vcpu
, bitmap
+ msr_index
/8, &b
, 1))
8449 return 1 & (b
>> (msr_index
& 7));
8451 return true; /* let L1 handle the wrong parameter */
8455 * Return 1 if we should exit from L2 to L1 to handle a CR access exit,
8456 * rather than handle it ourselves in L0. I.e., check if L1 wanted to
8457 * intercept (via guest_host_mask etc.) the current event.
8459 static bool nested_vmx_exit_handled_cr(struct kvm_vcpu
*vcpu
,
8460 struct vmcs12
*vmcs12
)
8462 unsigned long exit_qualification
= vmcs_readl(EXIT_QUALIFICATION
);
8463 int cr
= exit_qualification
& 15;
8467 switch ((exit_qualification
>> 4) & 3) {
8468 case 0: /* mov to cr */
8469 reg
= (exit_qualification
>> 8) & 15;
8470 val
= kvm_register_readl(vcpu
, reg
);
8473 if (vmcs12
->cr0_guest_host_mask
&
8474 (val
^ vmcs12
->cr0_read_shadow
))
8478 if ((vmcs12
->cr3_target_count
>= 1 &&
8479 vmcs12
->cr3_target_value0
== val
) ||
8480 (vmcs12
->cr3_target_count
>= 2 &&
8481 vmcs12
->cr3_target_value1
== val
) ||
8482 (vmcs12
->cr3_target_count
>= 3 &&
8483 vmcs12
->cr3_target_value2
== val
) ||
8484 (vmcs12
->cr3_target_count
>= 4 &&
8485 vmcs12
->cr3_target_value3
== val
))
8487 if (nested_cpu_has(vmcs12
, CPU_BASED_CR3_LOAD_EXITING
))
8491 if (vmcs12
->cr4_guest_host_mask
&
8492 (vmcs12
->cr4_read_shadow
^ val
))
8496 if (nested_cpu_has(vmcs12
, CPU_BASED_CR8_LOAD_EXITING
))
8502 if ((vmcs12
->cr0_guest_host_mask
& X86_CR0_TS
) &&
8503 (vmcs12
->cr0_read_shadow
& X86_CR0_TS
))
8506 case 1: /* mov from cr */
8509 if (vmcs12
->cpu_based_vm_exec_control
&
8510 CPU_BASED_CR3_STORE_EXITING
)
8514 if (vmcs12
->cpu_based_vm_exec_control
&
8515 CPU_BASED_CR8_STORE_EXITING
)
8522 * lmsw can change bits 1..3 of cr0, and only set bit 0 of
8523 * cr0. Other attempted changes are ignored, with no exit.
8525 val
= (exit_qualification
>> LMSW_SOURCE_DATA_SHIFT
) & 0x0f;
8526 if (vmcs12
->cr0_guest_host_mask
& 0xe &
8527 (val
^ vmcs12
->cr0_read_shadow
))
8529 if ((vmcs12
->cr0_guest_host_mask
& 0x1) &&
8530 !(vmcs12
->cr0_read_shadow
& 0x1) &&
8539 * Return 1 if we should exit from L2 to L1 to handle an exit, or 0 if we
8540 * should handle it ourselves in L0 (and then continue L2). Only call this
8541 * when in is_guest_mode (L2).
8543 static bool nested_vmx_exit_reflected(struct kvm_vcpu
*vcpu
, u32 exit_reason
)
8545 u32 intr_info
= vmcs_read32(VM_EXIT_INTR_INFO
);
8546 struct vcpu_vmx
*vmx
= to_vmx(vcpu
);
8547 struct vmcs12
*vmcs12
= get_vmcs12(vcpu
);
8549 if (vmx
->nested
.nested_run_pending
)
8552 if (unlikely(vmx
->fail
)) {
8553 pr_info_ratelimited("%s failed vm entry %x\n", __func__
,
8554 vmcs_read32(VM_INSTRUCTION_ERROR
));
8559 * The host physical addresses of some pages of guest memory
8560 * are loaded into the vmcs02 (e.g. vmcs12's Virtual APIC
8561 * Page). The CPU may write to these pages via their host
8562 * physical address while L2 is running, bypassing any
8563 * address-translation-based dirty tracking (e.g. EPT write
8566 * Mark them dirty on every exit from L2 to prevent them from
8567 * getting out of sync with dirty tracking.
8569 nested_mark_vmcs12_pages_dirty(vcpu
);
8571 trace_kvm_nested_vmexit(kvm_rip_read(vcpu
), exit_reason
,
8572 vmcs_readl(EXIT_QUALIFICATION
),
8573 vmx
->idt_vectoring_info
,
8575 vmcs_read32(VM_EXIT_INTR_ERROR_CODE
),
8578 switch (exit_reason
) {
8579 case EXIT_REASON_EXCEPTION_NMI
:
8580 if (is_nmi(intr_info
))
8582 else if (is_page_fault(intr_info
))
8583 return !vmx
->vcpu
.arch
.apf
.host_apf_reason
&& enable_ept
;
8584 else if (is_no_device(intr_info
) &&
8585 !(vmcs12
->guest_cr0
& X86_CR0_TS
))
8587 else if (is_debug(intr_info
) &&
8589 (KVM_GUESTDBG_SINGLESTEP
| KVM_GUESTDBG_USE_HW_BP
))
8591 else if (is_breakpoint(intr_info
) &&
8592 vcpu
->guest_debug
& KVM_GUESTDBG_USE_SW_BP
)
8594 return vmcs12
->exception_bitmap
&
8595 (1u << (intr_info
& INTR_INFO_VECTOR_MASK
));
8596 case EXIT_REASON_EXTERNAL_INTERRUPT
:
8598 case EXIT_REASON_TRIPLE_FAULT
:
8600 case EXIT_REASON_PENDING_INTERRUPT
:
8601 return nested_cpu_has(vmcs12
, CPU_BASED_VIRTUAL_INTR_PENDING
);
8602 case EXIT_REASON_NMI_WINDOW
:
8603 return nested_cpu_has(vmcs12
, CPU_BASED_VIRTUAL_NMI_PENDING
);
8604 case EXIT_REASON_TASK_SWITCH
:
8606 case EXIT_REASON_CPUID
:
8608 case EXIT_REASON_HLT
:
8609 return nested_cpu_has(vmcs12
, CPU_BASED_HLT_EXITING
);
8610 case EXIT_REASON_INVD
:
8612 case EXIT_REASON_INVLPG
:
8613 return nested_cpu_has(vmcs12
, CPU_BASED_INVLPG_EXITING
);
8614 case EXIT_REASON_RDPMC
:
8615 return nested_cpu_has(vmcs12
, CPU_BASED_RDPMC_EXITING
);
8616 case EXIT_REASON_RDRAND
:
8617 return nested_cpu_has2(vmcs12
, SECONDARY_EXEC_RDRAND_EXITING
);
8618 case EXIT_REASON_RDSEED
:
8619 return nested_cpu_has2(vmcs12
, SECONDARY_EXEC_RDSEED_EXITING
);
8620 case EXIT_REASON_RDTSC
: case EXIT_REASON_RDTSCP
:
8621 return nested_cpu_has(vmcs12
, CPU_BASED_RDTSC_EXITING
);
8622 case EXIT_REASON_VMCALL
: case EXIT_REASON_VMCLEAR
:
8623 case EXIT_REASON_VMLAUNCH
: case EXIT_REASON_VMPTRLD
:
8624 case EXIT_REASON_VMPTRST
: case EXIT_REASON_VMREAD
:
8625 case EXIT_REASON_VMRESUME
: case EXIT_REASON_VMWRITE
:
8626 case EXIT_REASON_VMOFF
: case EXIT_REASON_VMON
:
8627 case EXIT_REASON_INVEPT
: case EXIT_REASON_INVVPID
:
8629 * VMX instructions trap unconditionally. This allows L1 to
8630 * emulate them for its L2 guest, i.e., allows 3-level nesting!
8633 case EXIT_REASON_CR_ACCESS
:
8634 return nested_vmx_exit_handled_cr(vcpu
, vmcs12
);
8635 case EXIT_REASON_DR_ACCESS
:
8636 return nested_cpu_has(vmcs12
, CPU_BASED_MOV_DR_EXITING
);
8637 case EXIT_REASON_IO_INSTRUCTION
:
8638 return nested_vmx_exit_handled_io(vcpu
, vmcs12
);
8639 case EXIT_REASON_GDTR_IDTR
: case EXIT_REASON_LDTR_TR
:
8640 return nested_cpu_has2(vmcs12
, SECONDARY_EXEC_DESC
);
8641 case EXIT_REASON_MSR_READ
:
8642 case EXIT_REASON_MSR_WRITE
:
8643 return nested_vmx_exit_handled_msr(vcpu
, vmcs12
, exit_reason
);
8644 case EXIT_REASON_INVALID_STATE
:
8646 case EXIT_REASON_MWAIT_INSTRUCTION
:
8647 return nested_cpu_has(vmcs12
, CPU_BASED_MWAIT_EXITING
);
8648 case EXIT_REASON_MONITOR_TRAP_FLAG
:
8649 return nested_cpu_has(vmcs12
, CPU_BASED_MONITOR_TRAP_FLAG
);
8650 case EXIT_REASON_MONITOR_INSTRUCTION
:
8651 return nested_cpu_has(vmcs12
, CPU_BASED_MONITOR_EXITING
);
8652 case EXIT_REASON_PAUSE_INSTRUCTION
:
8653 return nested_cpu_has(vmcs12
, CPU_BASED_PAUSE_EXITING
) ||
8654 nested_cpu_has2(vmcs12
,
8655 SECONDARY_EXEC_PAUSE_LOOP_EXITING
);
8656 case EXIT_REASON_MCE_DURING_VMENTRY
:
8658 case EXIT_REASON_TPR_BELOW_THRESHOLD
:
8659 return nested_cpu_has(vmcs12
, CPU_BASED_TPR_SHADOW
);
8660 case EXIT_REASON_APIC_ACCESS
:
8661 return nested_cpu_has2(vmcs12
,
8662 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES
);
8663 case EXIT_REASON_APIC_WRITE
:
8664 case EXIT_REASON_EOI_INDUCED
:
8665 /* apic_write and eoi_induced should exit unconditionally. */
8667 case EXIT_REASON_EPT_VIOLATION
:
8669 * L0 always deals with the EPT violation. If nested EPT is
8670 * used, and the nested mmu code discovers that the address is
8671 * missing in the guest EPT table (EPT12), the EPT violation
8672 * will be injected with nested_ept_inject_page_fault()
8675 case EXIT_REASON_EPT_MISCONFIG
:
8677 * L2 never uses directly L1's EPT, but rather L0's own EPT
8678 * table (shadow on EPT) or a merged EPT table that L0 built
8679 * (EPT on EPT). So any problems with the structure of the
8680 * table is L0's fault.
8683 case EXIT_REASON_INVPCID
:
8685 nested_cpu_has2(vmcs12
, SECONDARY_EXEC_ENABLE_INVPCID
) &&
8686 nested_cpu_has(vmcs12
, CPU_BASED_INVLPG_EXITING
);
8687 case EXIT_REASON_WBINVD
:
8688 return nested_cpu_has2(vmcs12
, SECONDARY_EXEC_WBINVD_EXITING
);
8689 case EXIT_REASON_XSETBV
:
8691 case EXIT_REASON_XSAVES
: case EXIT_REASON_XRSTORS
:
8693 * This should never happen, since it is not possible to
8694 * set XSS to a non-zero value---neither in L1 nor in L2.
8695 * If if it were, XSS would have to be checked against
8696 * the XSS exit bitmap in vmcs12.
8698 return nested_cpu_has2(vmcs12
, SECONDARY_EXEC_XSAVES
);
8699 case EXIT_REASON_PREEMPTION_TIMER
:
8701 case EXIT_REASON_PML_FULL
:
8702 /* We emulate PML support to L1. */
8704 case EXIT_REASON_VMFUNC
:
8705 /* VM functions are emulated through L2->L0 vmexits. */
8712 static int nested_vmx_reflect_vmexit(struct kvm_vcpu
*vcpu
, u32 exit_reason
)
8714 u32 exit_intr_info
= vmcs_read32(VM_EXIT_INTR_INFO
);
8717 * At this point, the exit interruption info in exit_intr_info
8718 * is only valid for EXCEPTION_NMI exits. For EXTERNAL_INTERRUPT
8719 * we need to query the in-kernel LAPIC.
8721 WARN_ON(exit_reason
== EXIT_REASON_EXTERNAL_INTERRUPT
);
8722 if ((exit_intr_info
&
8723 (INTR_INFO_VALID_MASK
| INTR_INFO_DELIVER_CODE_MASK
)) ==
8724 (INTR_INFO_VALID_MASK
| INTR_INFO_DELIVER_CODE_MASK
)) {
8725 struct vmcs12
*vmcs12
= get_vmcs12(vcpu
);
8726 vmcs12
->vm_exit_intr_error_code
=
8727 vmcs_read32(VM_EXIT_INTR_ERROR_CODE
);
8730 nested_vmx_vmexit(vcpu
, exit_reason
, exit_intr_info
,
8731 vmcs_readl(EXIT_QUALIFICATION
));
8735 static void vmx_get_exit_info(struct kvm_vcpu
*vcpu
, u64
*info1
, u64
*info2
)
8737 *info1
= vmcs_readl(EXIT_QUALIFICATION
);
8738 *info2
= vmcs_read32(VM_EXIT_INTR_INFO
);
8741 static void vmx_destroy_pml_buffer(struct vcpu_vmx
*vmx
)
8744 __free_page(vmx
->pml_pg
);
8749 static void vmx_flush_pml_buffer(struct kvm_vcpu
*vcpu
)
8751 struct vcpu_vmx
*vmx
= to_vmx(vcpu
);
8755 pml_idx
= vmcs_read16(GUEST_PML_INDEX
);
8757 /* Do nothing if PML buffer is empty */
8758 if (pml_idx
== (PML_ENTITY_NUM
- 1))
8761 /* PML index always points to next available PML buffer entity */
8762 if (pml_idx
>= PML_ENTITY_NUM
)
8767 pml_buf
= page_address(vmx
->pml_pg
);
8768 for (; pml_idx
< PML_ENTITY_NUM
; pml_idx
++) {
8771 gpa
= pml_buf
[pml_idx
];
8772 WARN_ON(gpa
& (PAGE_SIZE
- 1));
8773 kvm_vcpu_mark_page_dirty(vcpu
, gpa
>> PAGE_SHIFT
);
8776 /* reset PML index */
8777 vmcs_write16(GUEST_PML_INDEX
, PML_ENTITY_NUM
- 1);
8781 * Flush all vcpus' PML buffer and update logged GPAs to dirty_bitmap.
8782 * Called before reporting dirty_bitmap to userspace.
8784 static void kvm_flush_pml_buffers(struct kvm
*kvm
)
8787 struct kvm_vcpu
*vcpu
;
8789 * We only need to kick vcpu out of guest mode here, as PML buffer
8790 * is flushed at beginning of all VMEXITs, and it's obvious that only
8791 * vcpus running in guest are possible to have unflushed GPAs in PML
8794 kvm_for_each_vcpu(i
, vcpu
, kvm
)
8795 kvm_vcpu_kick(vcpu
);
8798 static void vmx_dump_sel(char *name
, uint32_t sel
)
8800 pr_err("%s sel=0x%04x, attr=0x%05x, limit=0x%08x, base=0x%016lx\n",
8801 name
, vmcs_read16(sel
),
8802 vmcs_read32(sel
+ GUEST_ES_AR_BYTES
- GUEST_ES_SELECTOR
),
8803 vmcs_read32(sel
+ GUEST_ES_LIMIT
- GUEST_ES_SELECTOR
),
8804 vmcs_readl(sel
+ GUEST_ES_BASE
- GUEST_ES_SELECTOR
));
8807 static void vmx_dump_dtsel(char *name
, uint32_t limit
)
8809 pr_err("%s limit=0x%08x, base=0x%016lx\n",
8810 name
, vmcs_read32(limit
),
8811 vmcs_readl(limit
+ GUEST_GDTR_BASE
- GUEST_GDTR_LIMIT
));
8814 static void dump_vmcs(void)
8816 u32 vmentry_ctl
= vmcs_read32(VM_ENTRY_CONTROLS
);
8817 u32 vmexit_ctl
= vmcs_read32(VM_EXIT_CONTROLS
);
8818 u32 cpu_based_exec_ctrl
= vmcs_read32(CPU_BASED_VM_EXEC_CONTROL
);
8819 u32 pin_based_exec_ctrl
= vmcs_read32(PIN_BASED_VM_EXEC_CONTROL
);
8820 u32 secondary_exec_control
= 0;
8821 unsigned long cr4
= vmcs_readl(GUEST_CR4
);
8822 u64 efer
= vmcs_read64(GUEST_IA32_EFER
);
8825 if (cpu_has_secondary_exec_ctrls())
8826 secondary_exec_control
= vmcs_read32(SECONDARY_VM_EXEC_CONTROL
);
8828 pr_err("*** Guest State ***\n");
8829 pr_err("CR0: actual=0x%016lx, shadow=0x%016lx, gh_mask=%016lx\n",
8830 vmcs_readl(GUEST_CR0
), vmcs_readl(CR0_READ_SHADOW
),
8831 vmcs_readl(CR0_GUEST_HOST_MASK
));
8832 pr_err("CR4: actual=0x%016lx, shadow=0x%016lx, gh_mask=%016lx\n",
8833 cr4
, vmcs_readl(CR4_READ_SHADOW
), vmcs_readl(CR4_GUEST_HOST_MASK
));
8834 pr_err("CR3 = 0x%016lx\n", vmcs_readl(GUEST_CR3
));
8835 if ((secondary_exec_control
& SECONDARY_EXEC_ENABLE_EPT
) &&
8836 (cr4
& X86_CR4_PAE
) && !(efer
& EFER_LMA
))
8838 pr_err("PDPTR0 = 0x%016llx PDPTR1 = 0x%016llx\n",
8839 vmcs_read64(GUEST_PDPTR0
), vmcs_read64(GUEST_PDPTR1
));
8840 pr_err("PDPTR2 = 0x%016llx PDPTR3 = 0x%016llx\n",
8841 vmcs_read64(GUEST_PDPTR2
), vmcs_read64(GUEST_PDPTR3
));
8843 pr_err("RSP = 0x%016lx RIP = 0x%016lx\n",
8844 vmcs_readl(GUEST_RSP
), vmcs_readl(GUEST_RIP
));
8845 pr_err("RFLAGS=0x%08lx DR7 = 0x%016lx\n",
8846 vmcs_readl(GUEST_RFLAGS
), vmcs_readl(GUEST_DR7
));
8847 pr_err("Sysenter RSP=%016lx CS:RIP=%04x:%016lx\n",
8848 vmcs_readl(GUEST_SYSENTER_ESP
),
8849 vmcs_read32(GUEST_SYSENTER_CS
), vmcs_readl(GUEST_SYSENTER_EIP
));
8850 vmx_dump_sel("CS: ", GUEST_CS_SELECTOR
);
8851 vmx_dump_sel("DS: ", GUEST_DS_SELECTOR
);
8852 vmx_dump_sel("SS: ", GUEST_SS_SELECTOR
);
8853 vmx_dump_sel("ES: ", GUEST_ES_SELECTOR
);
8854 vmx_dump_sel("FS: ", GUEST_FS_SELECTOR
);
8855 vmx_dump_sel("GS: ", GUEST_GS_SELECTOR
);
8856 vmx_dump_dtsel("GDTR:", GUEST_GDTR_LIMIT
);
8857 vmx_dump_sel("LDTR:", GUEST_LDTR_SELECTOR
);
8858 vmx_dump_dtsel("IDTR:", GUEST_IDTR_LIMIT
);
8859 vmx_dump_sel("TR: ", GUEST_TR_SELECTOR
);
8860 if ((vmexit_ctl
& (VM_EXIT_SAVE_IA32_PAT
| VM_EXIT_SAVE_IA32_EFER
)) ||
8861 (vmentry_ctl
& (VM_ENTRY_LOAD_IA32_PAT
| VM_ENTRY_LOAD_IA32_EFER
)))
8862 pr_err("EFER = 0x%016llx PAT = 0x%016llx\n",
8863 efer
, vmcs_read64(GUEST_IA32_PAT
));
8864 pr_err("DebugCtl = 0x%016llx DebugExceptions = 0x%016lx\n",
8865 vmcs_read64(GUEST_IA32_DEBUGCTL
),
8866 vmcs_readl(GUEST_PENDING_DBG_EXCEPTIONS
));
8867 if (vmentry_ctl
& VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL
)
8868 pr_err("PerfGlobCtl = 0x%016llx\n",
8869 vmcs_read64(GUEST_IA32_PERF_GLOBAL_CTRL
));
8870 if (vmentry_ctl
& VM_ENTRY_LOAD_BNDCFGS
)
8871 pr_err("BndCfgS = 0x%016llx\n", vmcs_read64(GUEST_BNDCFGS
));
8872 pr_err("Interruptibility = %08x ActivityState = %08x\n",
8873 vmcs_read32(GUEST_INTERRUPTIBILITY_INFO
),
8874 vmcs_read32(GUEST_ACTIVITY_STATE
));
8875 if (secondary_exec_control
& SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY
)
8876 pr_err("InterruptStatus = %04x\n",
8877 vmcs_read16(GUEST_INTR_STATUS
));
8879 pr_err("*** Host State ***\n");
8880 pr_err("RIP = 0x%016lx RSP = 0x%016lx\n",
8881 vmcs_readl(HOST_RIP
), vmcs_readl(HOST_RSP
));
8882 pr_err("CS=%04x SS=%04x DS=%04x ES=%04x FS=%04x GS=%04x TR=%04x\n",
8883 vmcs_read16(HOST_CS_SELECTOR
), vmcs_read16(HOST_SS_SELECTOR
),
8884 vmcs_read16(HOST_DS_SELECTOR
), vmcs_read16(HOST_ES_SELECTOR
),
8885 vmcs_read16(HOST_FS_SELECTOR
), vmcs_read16(HOST_GS_SELECTOR
),
8886 vmcs_read16(HOST_TR_SELECTOR
));
8887 pr_err("FSBase=%016lx GSBase=%016lx TRBase=%016lx\n",
8888 vmcs_readl(HOST_FS_BASE
), vmcs_readl(HOST_GS_BASE
),
8889 vmcs_readl(HOST_TR_BASE
));
8890 pr_err("GDTBase=%016lx IDTBase=%016lx\n",
8891 vmcs_readl(HOST_GDTR_BASE
), vmcs_readl(HOST_IDTR_BASE
));
8892 pr_err("CR0=%016lx CR3=%016lx CR4=%016lx\n",
8893 vmcs_readl(HOST_CR0
), vmcs_readl(HOST_CR3
),
8894 vmcs_readl(HOST_CR4
));
8895 pr_err("Sysenter RSP=%016lx CS:RIP=%04x:%016lx\n",
8896 vmcs_readl(HOST_IA32_SYSENTER_ESP
),
8897 vmcs_read32(HOST_IA32_SYSENTER_CS
),
8898 vmcs_readl(HOST_IA32_SYSENTER_EIP
));
8899 if (vmexit_ctl
& (VM_EXIT_LOAD_IA32_PAT
| VM_EXIT_LOAD_IA32_EFER
))
8900 pr_err("EFER = 0x%016llx PAT = 0x%016llx\n",
8901 vmcs_read64(HOST_IA32_EFER
),
8902 vmcs_read64(HOST_IA32_PAT
));
8903 if (vmexit_ctl
& VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL
)
8904 pr_err("PerfGlobCtl = 0x%016llx\n",
8905 vmcs_read64(HOST_IA32_PERF_GLOBAL_CTRL
));
8907 pr_err("*** Control State ***\n");
8908 pr_err("PinBased=%08x CPUBased=%08x SecondaryExec=%08x\n",
8909 pin_based_exec_ctrl
, cpu_based_exec_ctrl
, secondary_exec_control
);
8910 pr_err("EntryControls=%08x ExitControls=%08x\n", vmentry_ctl
, vmexit_ctl
);
8911 pr_err("ExceptionBitmap=%08x PFECmask=%08x PFECmatch=%08x\n",
8912 vmcs_read32(EXCEPTION_BITMAP
),
8913 vmcs_read32(PAGE_FAULT_ERROR_CODE_MASK
),
8914 vmcs_read32(PAGE_FAULT_ERROR_CODE_MATCH
));
8915 pr_err("VMEntry: intr_info=%08x errcode=%08x ilen=%08x\n",
8916 vmcs_read32(VM_ENTRY_INTR_INFO_FIELD
),
8917 vmcs_read32(VM_ENTRY_EXCEPTION_ERROR_CODE
),
8918 vmcs_read32(VM_ENTRY_INSTRUCTION_LEN
));
8919 pr_err("VMExit: intr_info=%08x errcode=%08x ilen=%08x\n",
8920 vmcs_read32(VM_EXIT_INTR_INFO
),
8921 vmcs_read32(VM_EXIT_INTR_ERROR_CODE
),
8922 vmcs_read32(VM_EXIT_INSTRUCTION_LEN
));
8923 pr_err(" reason=%08x qualification=%016lx\n",
8924 vmcs_read32(VM_EXIT_REASON
), vmcs_readl(EXIT_QUALIFICATION
));
8925 pr_err("IDTVectoring: info=%08x errcode=%08x\n",
8926 vmcs_read32(IDT_VECTORING_INFO_FIELD
),
8927 vmcs_read32(IDT_VECTORING_ERROR_CODE
));
8928 pr_err("TSC Offset = 0x%016llx\n", vmcs_read64(TSC_OFFSET
));
8929 if (secondary_exec_control
& SECONDARY_EXEC_TSC_SCALING
)
8930 pr_err("TSC Multiplier = 0x%016llx\n",
8931 vmcs_read64(TSC_MULTIPLIER
));
8932 if (cpu_based_exec_ctrl
& CPU_BASED_TPR_SHADOW
)
8933 pr_err("TPR Threshold = 0x%02x\n", vmcs_read32(TPR_THRESHOLD
));
8934 if (pin_based_exec_ctrl
& PIN_BASED_POSTED_INTR
)
8935 pr_err("PostedIntrVec = 0x%02x\n", vmcs_read16(POSTED_INTR_NV
));
8936 if ((secondary_exec_control
& SECONDARY_EXEC_ENABLE_EPT
))
8937 pr_err("EPT pointer = 0x%016llx\n", vmcs_read64(EPT_POINTER
));
8938 n
= vmcs_read32(CR3_TARGET_COUNT
);
8939 for (i
= 0; i
+ 1 < n
; i
+= 4)
8940 pr_err("CR3 target%u=%016lx target%u=%016lx\n",
8941 i
, vmcs_readl(CR3_TARGET_VALUE0
+ i
* 2),
8942 i
+ 1, vmcs_readl(CR3_TARGET_VALUE0
+ i
* 2 + 2));
8944 pr_err("CR3 target%u=%016lx\n",
8945 i
, vmcs_readl(CR3_TARGET_VALUE0
+ i
* 2));
8946 if (secondary_exec_control
& SECONDARY_EXEC_PAUSE_LOOP_EXITING
)
8947 pr_err("PLE Gap=%08x Window=%08x\n",
8948 vmcs_read32(PLE_GAP
), vmcs_read32(PLE_WINDOW
));
8949 if (secondary_exec_control
& SECONDARY_EXEC_ENABLE_VPID
)
8950 pr_err("Virtual processor ID = 0x%04x\n",
8951 vmcs_read16(VIRTUAL_PROCESSOR_ID
));
8955 * The guest has exited. See if we can fix it or if we need userspace
8958 static int vmx_handle_exit(struct kvm_vcpu
*vcpu
)
8960 struct vcpu_vmx
*vmx
= to_vmx(vcpu
);
8961 u32 exit_reason
= vmx
->exit_reason
;
8962 u32 vectoring_info
= vmx
->idt_vectoring_info
;
8964 trace_kvm_exit(exit_reason
, vcpu
, KVM_ISA_VMX
);
8967 * Flush logged GPAs PML buffer, this will make dirty_bitmap more
8968 * updated. Another good is, in kvm_vm_ioctl_get_dirty_log, before
8969 * querying dirty_bitmap, we only need to kick all vcpus out of guest
8970 * mode as if vcpus is in root mode, the PML buffer must has been
8974 vmx_flush_pml_buffer(vcpu
);
8976 /* If guest state is invalid, start emulating */
8977 if (vmx
->emulation_required
)
8978 return handle_invalid_guest_state(vcpu
);
8980 if (is_guest_mode(vcpu
) && nested_vmx_exit_reflected(vcpu
, exit_reason
))
8981 return nested_vmx_reflect_vmexit(vcpu
, exit_reason
);
8983 if (exit_reason
& VMX_EXIT_REASONS_FAILED_VMENTRY
) {
8985 vcpu
->run
->exit_reason
= KVM_EXIT_FAIL_ENTRY
;
8986 vcpu
->run
->fail_entry
.hardware_entry_failure_reason
8991 if (unlikely(vmx
->fail
)) {
8992 vcpu
->run
->exit_reason
= KVM_EXIT_FAIL_ENTRY
;
8993 vcpu
->run
->fail_entry
.hardware_entry_failure_reason
8994 = vmcs_read32(VM_INSTRUCTION_ERROR
);
9000 * Do not try to fix EXIT_REASON_EPT_MISCONFIG if it caused by
9001 * delivery event since it indicates guest is accessing MMIO.
9002 * The vm-exit can be triggered again after return to guest that
9003 * will cause infinite loop.
9005 if ((vectoring_info
& VECTORING_INFO_VALID_MASK
) &&
9006 (exit_reason
!= EXIT_REASON_EXCEPTION_NMI
&&
9007 exit_reason
!= EXIT_REASON_EPT_VIOLATION
&&
9008 exit_reason
!= EXIT_REASON_PML_FULL
&&
9009 exit_reason
!= EXIT_REASON_TASK_SWITCH
)) {
9010 vcpu
->run
->exit_reason
= KVM_EXIT_INTERNAL_ERROR
;
9011 vcpu
->run
->internal
.suberror
= KVM_INTERNAL_ERROR_DELIVERY_EV
;
9012 vcpu
->run
->internal
.ndata
= 3;
9013 vcpu
->run
->internal
.data
[0] = vectoring_info
;
9014 vcpu
->run
->internal
.data
[1] = exit_reason
;
9015 vcpu
->run
->internal
.data
[2] = vcpu
->arch
.exit_qualification
;
9016 if (exit_reason
== EXIT_REASON_EPT_MISCONFIG
) {
9017 vcpu
->run
->internal
.ndata
++;
9018 vcpu
->run
->internal
.data
[3] =
9019 vmcs_read64(GUEST_PHYSICAL_ADDRESS
);
9024 if (unlikely(!enable_vnmi
&&
9025 vmx
->loaded_vmcs
->soft_vnmi_blocked
)) {
9026 if (vmx_interrupt_allowed(vcpu
)) {
9027 vmx
->loaded_vmcs
->soft_vnmi_blocked
= 0;
9028 } else if (vmx
->loaded_vmcs
->vnmi_blocked_time
> 1000000000LL &&
9029 vcpu
->arch
.nmi_pending
) {
9031 * This CPU don't support us in finding the end of an
9032 * NMI-blocked window if the guest runs with IRQs
9033 * disabled. So we pull the trigger after 1 s of
9034 * futile waiting, but inform the user about this.
9036 printk(KERN_WARNING
"%s: Breaking out of NMI-blocked "
9037 "state on VCPU %d after 1 s timeout\n",
9038 __func__
, vcpu
->vcpu_id
);
9039 vmx
->loaded_vmcs
->soft_vnmi_blocked
= 0;
9043 if (exit_reason
< kvm_vmx_max_exit_handlers
9044 && kvm_vmx_exit_handlers
[exit_reason
])
9045 return kvm_vmx_exit_handlers
[exit_reason
](vcpu
);
9047 vcpu_unimpl(vcpu
, "vmx: unexpected exit reason 0x%x\n",
9049 kvm_queue_exception(vcpu
, UD_VECTOR
);
9055 * Software based L1D cache flush which is used when microcode providing
9056 * the cache control MSR is not loaded.
9058 * The L1D cache is 32 KiB on Nehalem and later microarchitectures, but to
9059 * flush it is required to read in 64 KiB because the replacement algorithm
9060 * is not exactly LRU. This could be sized at runtime via topology
9061 * information but as all relevant affected CPUs have 32KiB L1D cache size
9062 * there is no point in doing so.
9064 #define L1D_CACHE_ORDER 4
9065 static void *vmx_l1d_flush_pages
;
9067 static void vmx_l1d_flush(struct kvm_vcpu
*vcpu
)
9069 int size
= PAGE_SIZE
<< L1D_CACHE_ORDER
;
9073 * This code is only executed when the the flush mode is 'cond' or
9076 * If 'flush always', keep the flush bit set, otherwise clear
9077 * it. The flush bit gets set again either from vcpu_run() or from
9078 * one of the unsafe VMEXIT handlers.
9080 always
= vmentry_l1d_flush
== VMENTER_L1D_FLUSH_ALWAYS
;
9081 vcpu
->arch
.l1tf_flush_l1d
= always
;
9083 vcpu
->stat
.l1d_flush
++;
9085 if (static_cpu_has(X86_FEATURE_FLUSH_L1D
)) {
9086 wrmsrl(MSR_IA32_FLUSH_CMD
, L1D_FLUSH
);
9091 /* First ensure the pages are in the TLB */
9092 "xorl %%eax, %%eax\n"
9093 ".Lpopulate_tlb:\n\t"
9094 "movzbl (%[empty_zp], %%" _ASM_AX
"), %%ecx\n\t"
9095 "addl $4096, %%eax\n\t"
9096 "cmpl %%eax, %[size]\n\t"
9097 "jne .Lpopulate_tlb\n\t"
9098 "xorl %%eax, %%eax\n\t"
9100 /* Now fill the cache */
9101 "xorl %%eax, %%eax\n"
9103 "movzbl (%[empty_zp], %%" _ASM_AX
"), %%ecx\n\t"
9104 "addl $64, %%eax\n\t"
9105 "cmpl %%eax, %[size]\n\t"
9106 "jne .Lfill_cache\n\t"
9108 :: [empty_zp
] "r" (vmx_l1d_flush_pages
),
9110 : "eax", "ebx", "ecx", "edx");
9113 static void update_cr8_intercept(struct kvm_vcpu
*vcpu
, int tpr
, int irr
)
9115 struct vmcs12
*vmcs12
= get_vmcs12(vcpu
);
9117 if (is_guest_mode(vcpu
) &&
9118 nested_cpu_has(vmcs12
, CPU_BASED_TPR_SHADOW
))
9121 if (irr
== -1 || tpr
< irr
) {
9122 vmcs_write32(TPR_THRESHOLD
, 0);
9126 vmcs_write32(TPR_THRESHOLD
, irr
);
9129 static void vmx_set_virtual_x2apic_mode(struct kvm_vcpu
*vcpu
, bool set
)
9131 u32 sec_exec_control
;
9133 /* Postpone execution until vmcs01 is the current VMCS. */
9134 if (is_guest_mode(vcpu
)) {
9135 to_vmx(vcpu
)->nested
.change_vmcs01_virtual_x2apic_mode
= true;
9139 if (!cpu_has_vmx_virtualize_x2apic_mode())
9142 if (!cpu_need_tpr_shadow(vcpu
))
9145 sec_exec_control
= vmcs_read32(SECONDARY_VM_EXEC_CONTROL
);
9148 sec_exec_control
&= ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES
;
9149 sec_exec_control
|= SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE
;
9151 sec_exec_control
&= ~SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE
;
9152 sec_exec_control
|= SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES
;
9153 vmx_flush_tlb_ept_only(vcpu
);
9155 vmcs_write32(SECONDARY_VM_EXEC_CONTROL
, sec_exec_control
);
9157 vmx_update_msr_bitmap(vcpu
);
9160 static void vmx_set_apic_access_page_addr(struct kvm_vcpu
*vcpu
, hpa_t hpa
)
9162 struct vcpu_vmx
*vmx
= to_vmx(vcpu
);
9165 * Currently we do not handle the nested case where L2 has an
9166 * APIC access page of its own; that page is still pinned.
9167 * Hence, we skip the case where the VCPU is in guest mode _and_
9168 * L1 prepared an APIC access page for L2.
9170 * For the case where L1 and L2 share the same APIC access page
9171 * (flexpriority=Y but SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES clear
9172 * in the vmcs12), this function will only update either the vmcs01
9173 * or the vmcs02. If the former, the vmcs02 will be updated by
9174 * prepare_vmcs02. If the latter, the vmcs01 will be updated in
9175 * the next L2->L1 exit.
9177 if (!is_guest_mode(vcpu
) ||
9178 !nested_cpu_has2(get_vmcs12(&vmx
->vcpu
),
9179 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES
)) {
9180 vmcs_write64(APIC_ACCESS_ADDR
, hpa
);
9181 vmx_flush_tlb_ept_only(vcpu
);
9185 static void vmx_hwapic_isr_update(struct kvm_vcpu
*vcpu
, int max_isr
)
9193 status
= vmcs_read16(GUEST_INTR_STATUS
);
9195 if (max_isr
!= old
) {
9197 status
|= max_isr
<< 8;
9198 vmcs_write16(GUEST_INTR_STATUS
, status
);
9202 static void vmx_set_rvi(int vector
)
9210 status
= vmcs_read16(GUEST_INTR_STATUS
);
9211 old
= (u8
)status
& 0xff;
9212 if ((u8
)vector
!= old
) {
9214 status
|= (u8
)vector
;
9215 vmcs_write16(GUEST_INTR_STATUS
, status
);
9219 static void vmx_hwapic_irr_update(struct kvm_vcpu
*vcpu
, int max_irr
)
9221 if (!is_guest_mode(vcpu
)) {
9222 vmx_set_rvi(max_irr
);
9230 * In guest mode. If a vmexit is needed, vmx_check_nested_events
9233 if (nested_exit_on_intr(vcpu
))
9237 * Else, fall back to pre-APICv interrupt injection since L2
9238 * is run without virtual interrupt delivery.
9240 if (!kvm_event_needs_reinjection(vcpu
) &&
9241 vmx_interrupt_allowed(vcpu
)) {
9242 kvm_queue_interrupt(vcpu
, max_irr
, false);
9243 vmx_inject_irq(vcpu
);
9247 static int vmx_sync_pir_to_irr(struct kvm_vcpu
*vcpu
)
9249 struct vcpu_vmx
*vmx
= to_vmx(vcpu
);
9252 WARN_ON(!vcpu
->arch
.apicv_active
);
9253 if (pi_test_on(&vmx
->pi_desc
)) {
9254 pi_clear_on(&vmx
->pi_desc
);
9256 * IOMMU can write to PIR.ON, so the barrier matters even on UP.
9257 * But on x86 this is just a compiler barrier anyway.
9259 smp_mb__after_atomic();
9260 max_irr
= kvm_apic_update_irr(vcpu
, vmx
->pi_desc
.pir
);
9262 max_irr
= kvm_lapic_find_highest_irr(vcpu
);
9264 vmx_hwapic_irr_update(vcpu
, max_irr
);
9268 static void vmx_load_eoi_exitmap(struct kvm_vcpu
*vcpu
, u64
*eoi_exit_bitmap
)
9270 if (!kvm_vcpu_apicv_active(vcpu
))
9273 vmcs_write64(EOI_EXIT_BITMAP0
, eoi_exit_bitmap
[0]);
9274 vmcs_write64(EOI_EXIT_BITMAP1
, eoi_exit_bitmap
[1]);
9275 vmcs_write64(EOI_EXIT_BITMAP2
, eoi_exit_bitmap
[2]);
9276 vmcs_write64(EOI_EXIT_BITMAP3
, eoi_exit_bitmap
[3]);
9279 static void vmx_apicv_post_state_restore(struct kvm_vcpu
*vcpu
)
9281 struct vcpu_vmx
*vmx
= to_vmx(vcpu
);
9283 pi_clear_on(&vmx
->pi_desc
);
9284 memset(vmx
->pi_desc
.pir
, 0, sizeof(vmx
->pi_desc
.pir
));
9287 static void vmx_complete_atomic_exit(struct vcpu_vmx
*vmx
)
9289 u32 exit_intr_info
= 0;
9290 u16 basic_exit_reason
= (u16
)vmx
->exit_reason
;
9292 if (!(basic_exit_reason
== EXIT_REASON_MCE_DURING_VMENTRY
9293 || basic_exit_reason
== EXIT_REASON_EXCEPTION_NMI
))
9296 if (!(vmx
->exit_reason
& VMX_EXIT_REASONS_FAILED_VMENTRY
))
9297 exit_intr_info
= vmcs_read32(VM_EXIT_INTR_INFO
);
9298 vmx
->exit_intr_info
= exit_intr_info
;
9300 /* if exit due to PF check for async PF */
9301 if (is_page_fault(exit_intr_info
))
9302 vmx
->vcpu
.arch
.apf
.host_apf_reason
= kvm_read_and_reset_pf_reason();
9304 /* Handle machine checks before interrupts are enabled */
9305 if (basic_exit_reason
== EXIT_REASON_MCE_DURING_VMENTRY
||
9306 is_machine_check(exit_intr_info
))
9307 kvm_machine_check();
9309 /* We need to handle NMIs before interrupts are enabled */
9310 if (is_nmi(exit_intr_info
)) {
9311 kvm_before_handle_nmi(&vmx
->vcpu
);
9313 kvm_after_handle_nmi(&vmx
->vcpu
);
9317 static void vmx_handle_external_intr(struct kvm_vcpu
*vcpu
)
9319 u32 exit_intr_info
= vmcs_read32(VM_EXIT_INTR_INFO
);
9321 if ((exit_intr_info
& (INTR_INFO_VALID_MASK
| INTR_INFO_INTR_TYPE_MASK
))
9322 == (INTR_INFO_VALID_MASK
| INTR_TYPE_EXT_INTR
)) {
9323 unsigned int vector
;
9324 unsigned long entry
;
9326 struct vcpu_vmx
*vmx
= to_vmx(vcpu
);
9327 #ifdef CONFIG_X86_64
9331 vector
= exit_intr_info
& INTR_INFO_VECTOR_MASK
;
9332 desc
= (gate_desc
*)vmx
->host_idt_base
+ vector
;
9333 entry
= gate_offset(desc
);
9335 #ifdef CONFIG_X86_64
9336 "mov %%" _ASM_SP
", %[sp]\n\t"
9337 "and $0xfffffffffffffff0, %%" _ASM_SP
"\n\t"
9342 __ASM_SIZE(push
) " $%c[cs]\n\t"
9345 #ifdef CONFIG_X86_64
9350 THUNK_TARGET(entry
),
9351 [ss
]"i"(__KERNEL_DS
),
9352 [cs
]"i"(__KERNEL_CS
)
9354 vcpu
->arch
.l1tf_flush_l1d
= true;
9357 STACK_FRAME_NON_STANDARD(vmx_handle_external_intr
);
9359 static bool vmx_has_emulated_msr(int index
)
9362 case MSR_IA32_SMBASE
:
9364 * We cannot do SMM unless we can run the guest in big
9367 return enable_unrestricted_guest
|| emulate_invalid_guest_state
;
9368 case MSR_AMD64_VIRT_SPEC_CTRL
:
9369 /* This is AMD only. */
9376 static bool vmx_mpx_supported(void)
9378 return (vmcs_config
.vmexit_ctrl
& VM_EXIT_CLEAR_BNDCFGS
) &&
9379 (vmcs_config
.vmentry_ctrl
& VM_ENTRY_LOAD_BNDCFGS
);
9382 static bool vmx_xsaves_supported(void)
9384 return vmcs_config
.cpu_based_2nd_exec_ctrl
&
9385 SECONDARY_EXEC_XSAVES
;
9388 static void vmx_recover_nmi_blocking(struct vcpu_vmx
*vmx
)
9393 bool idtv_info_valid
;
9395 idtv_info_valid
= vmx
->idt_vectoring_info
& VECTORING_INFO_VALID_MASK
;
9398 if (vmx
->loaded_vmcs
->nmi_known_unmasked
)
9401 * Can't use vmx->exit_intr_info since we're not sure what
9402 * the exit reason is.
9404 exit_intr_info
= vmcs_read32(VM_EXIT_INTR_INFO
);
9405 unblock_nmi
= (exit_intr_info
& INTR_INFO_UNBLOCK_NMI
) != 0;
9406 vector
= exit_intr_info
& INTR_INFO_VECTOR_MASK
;
9408 * SDM 3: 27.7.1.2 (September 2008)
9409 * Re-set bit "block by NMI" before VM entry if vmexit caused by
9410 * a guest IRET fault.
9411 * SDM 3: 23.2.2 (September 2008)
9412 * Bit 12 is undefined in any of the following cases:
9413 * If the VM exit sets the valid bit in the IDT-vectoring
9414 * information field.
9415 * If the VM exit is due to a double fault.
9417 if ((exit_intr_info
& INTR_INFO_VALID_MASK
) && unblock_nmi
&&
9418 vector
!= DF_VECTOR
&& !idtv_info_valid
)
9419 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO
,
9420 GUEST_INTR_STATE_NMI
);
9422 vmx
->loaded_vmcs
->nmi_known_unmasked
=
9423 !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO
)
9424 & GUEST_INTR_STATE_NMI
);
9425 } else if (unlikely(vmx
->loaded_vmcs
->soft_vnmi_blocked
))
9426 vmx
->loaded_vmcs
->vnmi_blocked_time
+=
9427 ktime_to_ns(ktime_sub(ktime_get(),
9428 vmx
->loaded_vmcs
->entry_time
));
9431 static void __vmx_complete_interrupts(struct kvm_vcpu
*vcpu
,
9432 u32 idt_vectoring_info
,
9433 int instr_len_field
,
9434 int error_code_field
)
9438 bool idtv_info_valid
;
9440 idtv_info_valid
= idt_vectoring_info
& VECTORING_INFO_VALID_MASK
;
9442 vcpu
->arch
.nmi_injected
= false;
9443 kvm_clear_exception_queue(vcpu
);
9444 kvm_clear_interrupt_queue(vcpu
);
9446 if (!idtv_info_valid
)
9449 kvm_make_request(KVM_REQ_EVENT
, vcpu
);
9451 vector
= idt_vectoring_info
& VECTORING_INFO_VECTOR_MASK
;
9452 type
= idt_vectoring_info
& VECTORING_INFO_TYPE_MASK
;
9455 case INTR_TYPE_NMI_INTR
:
9456 vcpu
->arch
.nmi_injected
= true;
9458 * SDM 3: 27.7.1.2 (September 2008)
9459 * Clear bit "block by NMI" before VM entry if a NMI
9462 vmx_set_nmi_mask(vcpu
, false);
9464 case INTR_TYPE_SOFT_EXCEPTION
:
9465 vcpu
->arch
.event_exit_inst_len
= vmcs_read32(instr_len_field
);
9467 case INTR_TYPE_HARD_EXCEPTION
:
9468 if (idt_vectoring_info
& VECTORING_INFO_DELIVER_CODE_MASK
) {
9469 u32 err
= vmcs_read32(error_code_field
);
9470 kvm_requeue_exception_e(vcpu
, vector
, err
);
9472 kvm_requeue_exception(vcpu
, vector
);
9474 case INTR_TYPE_SOFT_INTR
:
9475 vcpu
->arch
.event_exit_inst_len
= vmcs_read32(instr_len_field
);
9477 case INTR_TYPE_EXT_INTR
:
9478 kvm_queue_interrupt(vcpu
, vector
, type
== INTR_TYPE_SOFT_INTR
);
9485 static void vmx_complete_interrupts(struct vcpu_vmx
*vmx
)
9487 __vmx_complete_interrupts(&vmx
->vcpu
, vmx
->idt_vectoring_info
,
9488 VM_EXIT_INSTRUCTION_LEN
,
9489 IDT_VECTORING_ERROR_CODE
);
9492 static void vmx_cancel_injection(struct kvm_vcpu
*vcpu
)
9494 __vmx_complete_interrupts(vcpu
,
9495 vmcs_read32(VM_ENTRY_INTR_INFO_FIELD
),
9496 VM_ENTRY_INSTRUCTION_LEN
,
9497 VM_ENTRY_EXCEPTION_ERROR_CODE
);
9499 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD
, 0);
9502 static void atomic_switch_perf_msrs(struct vcpu_vmx
*vmx
)
9505 struct perf_guest_switch_msr
*msrs
;
9507 msrs
= perf_guest_get_msrs(&nr_msrs
);
9512 for (i
= 0; i
< nr_msrs
; i
++)
9513 if (msrs
[i
].host
== msrs
[i
].guest
)
9514 clear_atomic_switch_msr(vmx
, msrs
[i
].msr
);
9516 add_atomic_switch_msr(vmx
, msrs
[i
].msr
, msrs
[i
].guest
,
9517 msrs
[i
].host
, false);
9520 static void vmx_arm_hv_timer(struct kvm_vcpu
*vcpu
)
9522 struct vcpu_vmx
*vmx
= to_vmx(vcpu
);
9526 if (vmx
->hv_deadline_tsc
== -1)
9530 if (vmx
->hv_deadline_tsc
> tscl
)
9531 /* sure to be 32 bit only because checked on set_hv_timer */
9532 delta_tsc
= (u32
)((vmx
->hv_deadline_tsc
- tscl
) >>
9533 cpu_preemption_timer_multi
);
9537 vmcs_write32(VMX_PREEMPTION_TIMER_VALUE
, delta_tsc
);
9540 static void __noclone
vmx_vcpu_run(struct kvm_vcpu
*vcpu
)
9542 struct vcpu_vmx
*vmx
= to_vmx(vcpu
);
9543 unsigned long debugctlmsr
, cr3
, cr4
;
9545 /* Record the guest's net vcpu time for enforced NMI injections. */
9546 if (unlikely(!enable_vnmi
&&
9547 vmx
->loaded_vmcs
->soft_vnmi_blocked
))
9548 vmx
->loaded_vmcs
->entry_time
= ktime_get();
9550 /* Don't enter VMX if guest state is invalid, let the exit handler
9551 start emulation until we arrive back to a valid state */
9552 if (vmx
->emulation_required
)
9555 if (vmx
->ple_window_dirty
) {
9556 vmx
->ple_window_dirty
= false;
9557 vmcs_write32(PLE_WINDOW
, vmx
->ple_window
);
9560 if (vmx
->nested
.sync_shadow_vmcs
) {
9561 copy_vmcs12_to_shadow(vmx
);
9562 vmx
->nested
.sync_shadow_vmcs
= false;
9565 if (test_bit(VCPU_REGS_RSP
, (unsigned long *)&vcpu
->arch
.regs_dirty
))
9566 vmcs_writel(GUEST_RSP
, vcpu
->arch
.regs
[VCPU_REGS_RSP
]);
9567 if (test_bit(VCPU_REGS_RIP
, (unsigned long *)&vcpu
->arch
.regs_dirty
))
9568 vmcs_writel(GUEST_RIP
, vcpu
->arch
.regs
[VCPU_REGS_RIP
]);
9570 cr3
= __get_current_cr3_fast();
9571 if (unlikely(cr3
!= vmx
->loaded_vmcs
->vmcs_host_cr3
)) {
9572 vmcs_writel(HOST_CR3
, cr3
);
9573 vmx
->loaded_vmcs
->vmcs_host_cr3
= cr3
;
9576 cr4
= cr4_read_shadow();
9577 if (unlikely(cr4
!= vmx
->loaded_vmcs
->vmcs_host_cr4
)) {
9578 vmcs_writel(HOST_CR4
, cr4
);
9579 vmx
->loaded_vmcs
->vmcs_host_cr4
= cr4
;
9582 /* When single-stepping over STI and MOV SS, we must clear the
9583 * corresponding interruptibility bits in the guest state. Otherwise
9584 * vmentry fails as it then expects bit 14 (BS) in pending debug
9585 * exceptions being set, but that's not correct for the guest debugging
9587 if (vcpu
->guest_debug
& KVM_GUESTDBG_SINGLESTEP
)
9588 vmx_set_interrupt_shadow(vcpu
, 0);
9590 if (static_cpu_has(X86_FEATURE_PKU
) &&
9591 kvm_read_cr4_bits(vcpu
, X86_CR4_PKE
) &&
9592 vcpu
->arch
.pkru
!= vmx
->host_pkru
)
9593 __write_pkru(vcpu
->arch
.pkru
);
9595 atomic_switch_perf_msrs(vmx
);
9596 debugctlmsr
= get_debugctlmsr();
9598 vmx_arm_hv_timer(vcpu
);
9601 * If this vCPU has touched SPEC_CTRL, restore the guest's value if
9602 * it's non-zero. Since vmentry is serialising on affected CPUs, there
9603 * is no need to worry about the conditional branch over the wrmsr
9604 * being speculatively taken.
9606 x86_spec_ctrl_set_guest(vmx
->spec_ctrl
, 0);
9608 vmx
->__launched
= vmx
->loaded_vmcs
->launched
;
9610 if (static_branch_unlikely(&vmx_l1d_should_flush
)) {
9611 if (vcpu
->arch
.l1tf_flush_l1d
)
9612 vmx_l1d_flush(vcpu
);
9616 /* Store host registers */
9617 "push %%" _ASM_DX
"; push %%" _ASM_BP
";"
9618 "push %%" _ASM_CX
" \n\t" /* placeholder for guest rcx */
9619 "push %%" _ASM_CX
" \n\t"
9620 "cmp %%" _ASM_SP
", %c[host_rsp](%0) \n\t"
9622 "mov %%" _ASM_SP
", %c[host_rsp](%0) \n\t"
9623 __ex(ASM_VMX_VMWRITE_RSP_RDX
) "\n\t"
9625 /* Reload cr2 if changed */
9626 "mov %c[cr2](%0), %%" _ASM_AX
" \n\t"
9627 "mov %%cr2, %%" _ASM_DX
" \n\t"
9628 "cmp %%" _ASM_AX
", %%" _ASM_DX
" \n\t"
9630 "mov %%" _ASM_AX
", %%cr2 \n\t"
9632 /* Check if vmlaunch of vmresume is needed */
9633 "cmpl $0, %c[launched](%0) \n\t"
9634 /* Load guest registers. Don't clobber flags. */
9635 "mov %c[rax](%0), %%" _ASM_AX
" \n\t"
9636 "mov %c[rbx](%0), %%" _ASM_BX
" \n\t"
9637 "mov %c[rdx](%0), %%" _ASM_DX
" \n\t"
9638 "mov %c[rsi](%0), %%" _ASM_SI
" \n\t"
9639 "mov %c[rdi](%0), %%" _ASM_DI
" \n\t"
9640 "mov %c[rbp](%0), %%" _ASM_BP
" \n\t"
9641 #ifdef CONFIG_X86_64
9642 "mov %c[r8](%0), %%r8 \n\t"
9643 "mov %c[r9](%0), %%r9 \n\t"
9644 "mov %c[r10](%0), %%r10 \n\t"
9645 "mov %c[r11](%0), %%r11 \n\t"
9646 "mov %c[r12](%0), %%r12 \n\t"
9647 "mov %c[r13](%0), %%r13 \n\t"
9648 "mov %c[r14](%0), %%r14 \n\t"
9649 "mov %c[r15](%0), %%r15 \n\t"
9651 "mov %c[rcx](%0), %%" _ASM_CX
" \n\t" /* kills %0 (ecx) */
9653 /* Enter guest mode */
9655 __ex(ASM_VMX_VMLAUNCH
) "\n\t"
9657 "1: " __ex(ASM_VMX_VMRESUME
) "\n\t"
9659 /* Save guest registers, load host registers, keep flags */
9660 "mov %0, %c[wordsize](%%" _ASM_SP
") \n\t"
9662 "setbe %c[fail](%0)\n\t"
9663 "mov %%" _ASM_AX
", %c[rax](%0) \n\t"
9664 "mov %%" _ASM_BX
", %c[rbx](%0) \n\t"
9665 __ASM_SIZE(pop
) " %c[rcx](%0) \n\t"
9666 "mov %%" _ASM_DX
", %c[rdx](%0) \n\t"
9667 "mov %%" _ASM_SI
", %c[rsi](%0) \n\t"
9668 "mov %%" _ASM_DI
", %c[rdi](%0) \n\t"
9669 "mov %%" _ASM_BP
", %c[rbp](%0) \n\t"
9670 #ifdef CONFIG_X86_64
9671 "mov %%r8, %c[r8](%0) \n\t"
9672 "mov %%r9, %c[r9](%0) \n\t"
9673 "mov %%r10, %c[r10](%0) \n\t"
9674 "mov %%r11, %c[r11](%0) \n\t"
9675 "mov %%r12, %c[r12](%0) \n\t"
9676 "mov %%r13, %c[r13](%0) \n\t"
9677 "mov %%r14, %c[r14](%0) \n\t"
9678 "mov %%r15, %c[r15](%0) \n\t"
9679 "xor %%r8d, %%r8d \n\t"
9680 "xor %%r9d, %%r9d \n\t"
9681 "xor %%r10d, %%r10d \n\t"
9682 "xor %%r11d, %%r11d \n\t"
9683 "xor %%r12d, %%r12d \n\t"
9684 "xor %%r13d, %%r13d \n\t"
9685 "xor %%r14d, %%r14d \n\t"
9686 "xor %%r15d, %%r15d \n\t"
9688 "mov %%cr2, %%" _ASM_AX
" \n\t"
9689 "mov %%" _ASM_AX
", %c[cr2](%0) \n\t"
9691 "xor %%eax, %%eax \n\t"
9692 "xor %%ebx, %%ebx \n\t"
9693 "xor %%esi, %%esi \n\t"
9694 "xor %%edi, %%edi \n\t"
9695 "pop %%" _ASM_BP
"; pop %%" _ASM_DX
" \n\t"
9696 ".pushsection .rodata \n\t"
9697 ".global vmx_return \n\t"
9698 "vmx_return: " _ASM_PTR
" 2b \n\t"
9700 : : "c"(vmx
), "d"((unsigned long)HOST_RSP
),
9701 [launched
]"i"(offsetof(struct vcpu_vmx
, __launched
)),
9702 [fail
]"i"(offsetof(struct vcpu_vmx
, fail
)),
9703 [host_rsp
]"i"(offsetof(struct vcpu_vmx
, host_rsp
)),
9704 [rax
]"i"(offsetof(struct vcpu_vmx
, vcpu
.arch
.regs
[VCPU_REGS_RAX
])),
9705 [rbx
]"i"(offsetof(struct vcpu_vmx
, vcpu
.arch
.regs
[VCPU_REGS_RBX
])),
9706 [rcx
]"i"(offsetof(struct vcpu_vmx
, vcpu
.arch
.regs
[VCPU_REGS_RCX
])),
9707 [rdx
]"i"(offsetof(struct vcpu_vmx
, vcpu
.arch
.regs
[VCPU_REGS_RDX
])),
9708 [rsi
]"i"(offsetof(struct vcpu_vmx
, vcpu
.arch
.regs
[VCPU_REGS_RSI
])),
9709 [rdi
]"i"(offsetof(struct vcpu_vmx
, vcpu
.arch
.regs
[VCPU_REGS_RDI
])),
9710 [rbp
]"i"(offsetof(struct vcpu_vmx
, vcpu
.arch
.regs
[VCPU_REGS_RBP
])),
9711 #ifdef CONFIG_X86_64
9712 [r8
]"i"(offsetof(struct vcpu_vmx
, vcpu
.arch
.regs
[VCPU_REGS_R8
])),
9713 [r9
]"i"(offsetof(struct vcpu_vmx
, vcpu
.arch
.regs
[VCPU_REGS_R9
])),
9714 [r10
]"i"(offsetof(struct vcpu_vmx
, vcpu
.arch
.regs
[VCPU_REGS_R10
])),
9715 [r11
]"i"(offsetof(struct vcpu_vmx
, vcpu
.arch
.regs
[VCPU_REGS_R11
])),
9716 [r12
]"i"(offsetof(struct vcpu_vmx
, vcpu
.arch
.regs
[VCPU_REGS_R12
])),
9717 [r13
]"i"(offsetof(struct vcpu_vmx
, vcpu
.arch
.regs
[VCPU_REGS_R13
])),
9718 [r14
]"i"(offsetof(struct vcpu_vmx
, vcpu
.arch
.regs
[VCPU_REGS_R14
])),
9719 [r15
]"i"(offsetof(struct vcpu_vmx
, vcpu
.arch
.regs
[VCPU_REGS_R15
])),
9721 [cr2
]"i"(offsetof(struct vcpu_vmx
, vcpu
.arch
.cr2
)),
9722 [wordsize
]"i"(sizeof(ulong
))
9724 #ifdef CONFIG_X86_64
9725 , "rax", "rbx", "rdi", "rsi"
9726 , "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15"
9728 , "eax", "ebx", "edi", "esi"
9733 * We do not use IBRS in the kernel. If this vCPU has used the
9734 * SPEC_CTRL MSR it may have left it on; save the value and
9735 * turn it off. This is much more efficient than blindly adding
9736 * it to the atomic save/restore list. Especially as the former
9737 * (Saving guest MSRs on vmexit) doesn't even exist in KVM.
9739 * For non-nested case:
9740 * If the L01 MSR bitmap does not intercept the MSR, then we need to
9744 * If the L02 MSR bitmap does not intercept the MSR, then we need to
9747 if (unlikely(!msr_write_intercepted(vcpu
, MSR_IA32_SPEC_CTRL
)))
9748 vmx
->spec_ctrl
= native_read_msr(MSR_IA32_SPEC_CTRL
);
9750 x86_spec_ctrl_restore_host(vmx
->spec_ctrl
, 0);
9752 /* Eliminate branch target predictions from guest mode */
9755 /* MSR_IA32_DEBUGCTLMSR is zeroed on vmexit. Restore it if needed */
9757 update_debugctlmsr(debugctlmsr
);
9759 #ifndef CONFIG_X86_64
9761 * The sysexit path does not restore ds/es, so we must set them to
9762 * a reasonable value ourselves.
9764 * We can't defer this to vmx_load_host_state() since that function
9765 * may be executed in interrupt context, which saves and restore segments
9766 * around it, nullifying its effect.
9768 loadsegment(ds
, __USER_DS
);
9769 loadsegment(es
, __USER_DS
);
9772 vcpu
->arch
.regs_avail
= ~((1 << VCPU_REGS_RIP
) | (1 << VCPU_REGS_RSP
)
9773 | (1 << VCPU_EXREG_RFLAGS
)
9774 | (1 << VCPU_EXREG_PDPTR
)
9775 | (1 << VCPU_EXREG_SEGMENTS
)
9776 | (1 << VCPU_EXREG_CR3
));
9777 vcpu
->arch
.regs_dirty
= 0;
9780 * eager fpu is enabled if PKEY is supported and CR4 is switched
9781 * back on host, so it is safe to read guest PKRU from current
9784 if (static_cpu_has(X86_FEATURE_PKU
) &&
9785 kvm_read_cr4_bits(vcpu
, X86_CR4_PKE
)) {
9786 vcpu
->arch
.pkru
= __read_pkru();
9787 if (vcpu
->arch
.pkru
!= vmx
->host_pkru
)
9788 __write_pkru(vmx
->host_pkru
);
9792 * the KVM_REQ_EVENT optimization bit is only on for one entry, and if
9793 * we did not inject a still-pending event to L1 now because of
9794 * nested_run_pending, we need to re-enable this bit.
9796 if (vmx
->nested
.nested_run_pending
)
9797 kvm_make_request(KVM_REQ_EVENT
, vcpu
);
9799 vmx
->nested
.nested_run_pending
= 0;
9800 vmx
->idt_vectoring_info
= 0;
9802 vmx
->exit_reason
= vmx
->fail
? 0xdead : vmcs_read32(VM_EXIT_REASON
);
9803 if (vmx
->fail
|| (vmx
->exit_reason
& VMX_EXIT_REASONS_FAILED_VMENTRY
))
9806 vmx
->loaded_vmcs
->launched
= 1;
9807 vmx
->idt_vectoring_info
= vmcs_read32(IDT_VECTORING_INFO_FIELD
);
9809 vmx_complete_atomic_exit(vmx
);
9810 vmx_recover_nmi_blocking(vmx
);
9811 vmx_complete_interrupts(vmx
);
9813 STACK_FRAME_NON_STANDARD(vmx_vcpu_run
);
9815 static void vmx_switch_vmcs(struct kvm_vcpu
*vcpu
, struct loaded_vmcs
*vmcs
)
9817 struct vcpu_vmx
*vmx
= to_vmx(vcpu
);
9820 if (vmx
->loaded_vmcs
== vmcs
)
9824 vmx
->loaded_vmcs
= vmcs
;
9826 vmx_vcpu_load(vcpu
, cpu
);
9831 * Ensure that the current vmcs of the logical processor is the
9832 * vmcs01 of the vcpu before calling free_nested().
9834 static void vmx_free_vcpu_nested(struct kvm_vcpu
*vcpu
)
9836 struct vcpu_vmx
*vmx
= to_vmx(vcpu
);
9839 r
= vcpu_load(vcpu
);
9841 vmx_switch_vmcs(vcpu
, &vmx
->vmcs01
);
9846 static void vmx_free_vcpu(struct kvm_vcpu
*vcpu
)
9848 struct vcpu_vmx
*vmx
= to_vmx(vcpu
);
9851 vmx_destroy_pml_buffer(vmx
);
9852 free_vpid(vmx
->vpid
);
9853 leave_guest_mode(vcpu
);
9854 vmx_free_vcpu_nested(vcpu
);
9855 free_loaded_vmcs(vmx
->loaded_vmcs
);
9856 kfree(vmx
->guest_msrs
);
9857 kvm_vcpu_uninit(vcpu
);
9858 kmem_cache_free(kvm_vcpu_cache
, vmx
);
9861 static struct kvm_vcpu
*vmx_create_vcpu(struct kvm
*kvm
, unsigned int id
)
9864 struct vcpu_vmx
*vmx
= kmem_cache_zalloc(kvm_vcpu_cache
, GFP_KERNEL
);
9865 unsigned long *msr_bitmap
;
9869 return ERR_PTR(-ENOMEM
);
9871 vmx
->vpid
= allocate_vpid();
9873 err
= kvm_vcpu_init(&vmx
->vcpu
, kvm
, id
);
9880 * If PML is turned on, failure on enabling PML just results in failure
9881 * of creating the vcpu, therefore we can simplify PML logic (by
9882 * avoiding dealing with cases, such as enabling PML partially on vcpus
9883 * for the guest, etc.
9886 vmx
->pml_pg
= alloc_page(GFP_KERNEL
| __GFP_ZERO
);
9891 vmx
->guest_msrs
= kmalloc(PAGE_SIZE
, GFP_KERNEL
);
9892 BUILD_BUG_ON(ARRAY_SIZE(vmx_msr_index
) * sizeof(vmx
->guest_msrs
[0])
9895 if (!vmx
->guest_msrs
)
9898 err
= alloc_loaded_vmcs(&vmx
->vmcs01
);
9902 msr_bitmap
= vmx
->vmcs01
.msr_bitmap
;
9903 vmx_disable_intercept_for_msr(msr_bitmap
, MSR_FS_BASE
, MSR_TYPE_RW
);
9904 vmx_disable_intercept_for_msr(msr_bitmap
, MSR_GS_BASE
, MSR_TYPE_RW
);
9905 vmx_disable_intercept_for_msr(msr_bitmap
, MSR_KERNEL_GS_BASE
, MSR_TYPE_RW
);
9906 vmx_disable_intercept_for_msr(msr_bitmap
, MSR_IA32_SYSENTER_CS
, MSR_TYPE_RW
);
9907 vmx_disable_intercept_for_msr(msr_bitmap
, MSR_IA32_SYSENTER_ESP
, MSR_TYPE_RW
);
9908 vmx_disable_intercept_for_msr(msr_bitmap
, MSR_IA32_SYSENTER_EIP
, MSR_TYPE_RW
);
9909 vmx
->msr_bitmap_mode
= 0;
9911 vmx
->loaded_vmcs
= &vmx
->vmcs01
;
9913 vmx_vcpu_load(&vmx
->vcpu
, cpu
);
9914 vmx
->vcpu
.cpu
= cpu
;
9915 vmx_vcpu_setup(vmx
);
9916 vmx_vcpu_put(&vmx
->vcpu
);
9918 if (cpu_need_virtualize_apic_accesses(&vmx
->vcpu
)) {
9919 err
= alloc_apic_access_page(kvm
);
9925 err
= init_rmode_identity_map(kvm
);
9931 nested_vmx_setup_ctls_msrs(vmx
);
9932 vmx
->nested
.vpid02
= allocate_vpid();
9935 vmx
->nested
.posted_intr_nv
= -1;
9936 vmx
->nested
.current_vmptr
= -1ull;
9938 vmx
->msr_ia32_feature_control_valid_bits
= FEATURE_CONTROL_LOCKED
;
9941 * Enforce invariant: pi_desc.nv is always either POSTED_INTR_VECTOR
9942 * or POSTED_INTR_WAKEUP_VECTOR.
9944 vmx
->pi_desc
.nv
= POSTED_INTR_VECTOR
;
9945 vmx
->pi_desc
.sn
= 1;
9950 free_vpid(vmx
->nested
.vpid02
);
9951 free_loaded_vmcs(vmx
->loaded_vmcs
);
9953 kfree(vmx
->guest_msrs
);
9955 vmx_destroy_pml_buffer(vmx
);
9957 kvm_vcpu_uninit(&vmx
->vcpu
);
9959 free_vpid(vmx
->vpid
);
9960 kmem_cache_free(kvm_vcpu_cache
, vmx
);
9961 return ERR_PTR(err
);
9964 #define L1TF_MSG "SMT enabled with L1TF CPU bug present. Refer to CVE-2018-3620 for details.\n"
9966 static int vmx_vm_init(struct kvm
*kvm
)
9968 if (boot_cpu_has(X86_BUG_L1TF
) && cpu_smt_control
== CPU_SMT_ENABLED
) {
9978 static void __init
vmx_check_processor_compat(void *rtn
)
9980 struct vmcs_config vmcs_conf
;
9983 if (setup_vmcs_config(&vmcs_conf
) < 0)
9985 if (memcmp(&vmcs_config
, &vmcs_conf
, sizeof(struct vmcs_config
)) != 0) {
9986 printk(KERN_ERR
"kvm: CPU %d feature inconsistency!\n",
9987 smp_processor_id());
9992 static u64
vmx_get_mt_mask(struct kvm_vcpu
*vcpu
, gfn_t gfn
, bool is_mmio
)
9997 /* For VT-d and EPT combination
9998 * 1. MMIO: always map as UC
10000 * a. VT-d without snooping control feature: can't guarantee the
10001 * result, try to trust guest.
10002 * b. VT-d with snooping control feature: snooping control feature of
10003 * VT-d engine can guarantee the cache correctness. Just set it
10004 * to WB to keep consistent with host. So the same as item 3.
10005 * 3. EPT without VT-d: always map as WB and set IPAT=1 to keep
10006 * consistent with host MTRR
10009 cache
= MTRR_TYPE_UNCACHABLE
;
10013 if (!kvm_arch_has_noncoherent_dma(vcpu
->kvm
)) {
10014 ipat
= VMX_EPT_IPAT_BIT
;
10015 cache
= MTRR_TYPE_WRBACK
;
10019 if (kvm_read_cr0(vcpu
) & X86_CR0_CD
) {
10020 ipat
= VMX_EPT_IPAT_BIT
;
10021 if (kvm_check_has_quirk(vcpu
->kvm
, KVM_X86_QUIRK_CD_NW_CLEARED
))
10022 cache
= MTRR_TYPE_WRBACK
;
10024 cache
= MTRR_TYPE_UNCACHABLE
;
10028 cache
= kvm_mtrr_get_guest_memory_type(vcpu
, gfn
);
10031 return (cache
<< VMX_EPT_MT_EPTE_SHIFT
) | ipat
;
10034 static int vmx_get_lpage_level(void)
10036 if (enable_ept
&& !cpu_has_vmx_ept_1g_page())
10037 return PT_DIRECTORY_LEVEL
;
10039 /* For shadow and EPT supported 1GB page */
10040 return PT_PDPE_LEVEL
;
10043 static void vmcs_set_secondary_exec_control(u32 new_ctl
)
10046 * These bits in the secondary execution controls field
10047 * are dynamic, the others are mostly based on the hypervisor
10048 * architecture and the guest's CPUID. Do not touch the
10052 SECONDARY_EXEC_SHADOW_VMCS
|
10053 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE
|
10054 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES
;
10056 u32 cur_ctl
= vmcs_read32(SECONDARY_VM_EXEC_CONTROL
);
10058 vmcs_write32(SECONDARY_VM_EXEC_CONTROL
,
10059 (new_ctl
& ~mask
) | (cur_ctl
& mask
));
10063 * Generate MSR_IA32_VMX_CR{0,4}_FIXED1 according to CPUID. Only set bits
10064 * (indicating "allowed-1") if they are supported in the guest's CPUID.
10066 static void nested_vmx_cr_fixed1_bits_update(struct kvm_vcpu
*vcpu
)
10068 struct vcpu_vmx
*vmx
= to_vmx(vcpu
);
10069 struct kvm_cpuid_entry2
*entry
;
10071 vmx
->nested
.nested_vmx_cr0_fixed1
= 0xffffffff;
10072 vmx
->nested
.nested_vmx_cr4_fixed1
= X86_CR4_PCE
;
10074 #define cr4_fixed1_update(_cr4_mask, _reg, _cpuid_mask) do { \
10075 if (entry && (entry->_reg & (_cpuid_mask))) \
10076 vmx->nested.nested_vmx_cr4_fixed1 |= (_cr4_mask); \
10079 entry
= kvm_find_cpuid_entry(vcpu
, 0x1, 0);
10080 cr4_fixed1_update(X86_CR4_VME
, edx
, bit(X86_FEATURE_VME
));
10081 cr4_fixed1_update(X86_CR4_PVI
, edx
, bit(X86_FEATURE_VME
));
10082 cr4_fixed1_update(X86_CR4_TSD
, edx
, bit(X86_FEATURE_TSC
));
10083 cr4_fixed1_update(X86_CR4_DE
, edx
, bit(X86_FEATURE_DE
));
10084 cr4_fixed1_update(X86_CR4_PSE
, edx
, bit(X86_FEATURE_PSE
));
10085 cr4_fixed1_update(X86_CR4_PAE
, edx
, bit(X86_FEATURE_PAE
));
10086 cr4_fixed1_update(X86_CR4_MCE
, edx
, bit(X86_FEATURE_MCE
));
10087 cr4_fixed1_update(X86_CR4_PGE
, edx
, bit(X86_FEATURE_PGE
));
10088 cr4_fixed1_update(X86_CR4_OSFXSR
, edx
, bit(X86_FEATURE_FXSR
));
10089 cr4_fixed1_update(X86_CR4_OSXMMEXCPT
, edx
, bit(X86_FEATURE_XMM
));
10090 cr4_fixed1_update(X86_CR4_VMXE
, ecx
, bit(X86_FEATURE_VMX
));
10091 cr4_fixed1_update(X86_CR4_SMXE
, ecx
, bit(X86_FEATURE_SMX
));
10092 cr4_fixed1_update(X86_CR4_PCIDE
, ecx
, bit(X86_FEATURE_PCID
));
10093 cr4_fixed1_update(X86_CR4_OSXSAVE
, ecx
, bit(X86_FEATURE_XSAVE
));
10095 entry
= kvm_find_cpuid_entry(vcpu
, 0x7, 0);
10096 cr4_fixed1_update(X86_CR4_FSGSBASE
, ebx
, bit(X86_FEATURE_FSGSBASE
));
10097 cr4_fixed1_update(X86_CR4_SMEP
, ebx
, bit(X86_FEATURE_SMEP
));
10098 cr4_fixed1_update(X86_CR4_SMAP
, ebx
, bit(X86_FEATURE_SMAP
));
10099 cr4_fixed1_update(X86_CR4_PKE
, ecx
, bit(X86_FEATURE_PKU
));
10100 cr4_fixed1_update(X86_CR4_UMIP
, ecx
, bit(X86_FEATURE_UMIP
));
10102 #undef cr4_fixed1_update
10105 static void vmx_cpuid_update(struct kvm_vcpu
*vcpu
)
10107 struct vcpu_vmx
*vmx
= to_vmx(vcpu
);
10109 if (cpu_has_secondary_exec_ctrls()) {
10110 vmx_compute_secondary_exec_control(vmx
);
10111 vmcs_set_secondary_exec_control(vmx
->secondary_exec_control
);
10114 if (nested_vmx_allowed(vcpu
))
10115 to_vmx(vcpu
)->msr_ia32_feature_control_valid_bits
|=
10116 FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX
;
10118 to_vmx(vcpu
)->msr_ia32_feature_control_valid_bits
&=
10119 ~FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX
;
10121 if (nested_vmx_allowed(vcpu
))
10122 nested_vmx_cr_fixed1_bits_update(vcpu
);
10125 static void vmx_set_supported_cpuid(u32 func
, struct kvm_cpuid_entry2
*entry
)
10127 if (func
== 1 && nested
)
10128 entry
->ecx
|= bit(X86_FEATURE_VMX
);
10131 static void nested_ept_inject_page_fault(struct kvm_vcpu
*vcpu
,
10132 struct x86_exception
*fault
)
10134 struct vmcs12
*vmcs12
= get_vmcs12(vcpu
);
10135 struct vcpu_vmx
*vmx
= to_vmx(vcpu
);
10137 unsigned long exit_qualification
= vcpu
->arch
.exit_qualification
;
10139 if (vmx
->nested
.pml_full
) {
10140 exit_reason
= EXIT_REASON_PML_FULL
;
10141 vmx
->nested
.pml_full
= false;
10142 exit_qualification
&= INTR_INFO_UNBLOCK_NMI
;
10143 } else if (fault
->error_code
& PFERR_RSVD_MASK
)
10144 exit_reason
= EXIT_REASON_EPT_MISCONFIG
;
10146 exit_reason
= EXIT_REASON_EPT_VIOLATION
;
10148 nested_vmx_vmexit(vcpu
, exit_reason
, 0, exit_qualification
);
10149 vmcs12
->guest_physical_address
= fault
->address
;
10152 static bool nested_ept_ad_enabled(struct kvm_vcpu
*vcpu
)
10154 return nested_ept_get_cr3(vcpu
) & VMX_EPTP_AD_ENABLE_BIT
;
10157 /* Callbacks for nested_ept_init_mmu_context: */
10159 static unsigned long nested_ept_get_cr3(struct kvm_vcpu
*vcpu
)
10161 /* return the page table to be shadowed - in our case, EPT12 */
10162 return get_vmcs12(vcpu
)->ept_pointer
;
10165 static int nested_ept_init_mmu_context(struct kvm_vcpu
*vcpu
)
10167 WARN_ON(mmu_is_nested(vcpu
));
10168 if (!valid_ept_address(vcpu
, nested_ept_get_cr3(vcpu
)))
10171 kvm_mmu_unload(vcpu
);
10172 kvm_init_shadow_ept_mmu(vcpu
,
10173 to_vmx(vcpu
)->nested
.nested_vmx_ept_caps
&
10174 VMX_EPT_EXECUTE_ONLY_BIT
,
10175 nested_ept_ad_enabled(vcpu
));
10176 vcpu
->arch
.mmu
.set_cr3
= vmx_set_cr3
;
10177 vcpu
->arch
.mmu
.get_cr3
= nested_ept_get_cr3
;
10178 vcpu
->arch
.mmu
.inject_page_fault
= nested_ept_inject_page_fault
;
10180 vcpu
->arch
.walk_mmu
= &vcpu
->arch
.nested_mmu
;
10184 static void nested_ept_uninit_mmu_context(struct kvm_vcpu
*vcpu
)
10186 vcpu
->arch
.walk_mmu
= &vcpu
->arch
.mmu
;
10189 static bool nested_vmx_is_page_fault_vmexit(struct vmcs12
*vmcs12
,
10192 bool inequality
, bit
;
10194 bit
= (vmcs12
->exception_bitmap
& (1u << PF_VECTOR
)) != 0;
10196 (error_code
& vmcs12
->page_fault_error_code_mask
) !=
10197 vmcs12
->page_fault_error_code_match
;
10198 return inequality
^ bit
;
10201 static void vmx_inject_page_fault_nested(struct kvm_vcpu
*vcpu
,
10202 struct x86_exception
*fault
)
10204 struct vmcs12
*vmcs12
= get_vmcs12(vcpu
);
10206 WARN_ON(!is_guest_mode(vcpu
));
10208 if (nested_vmx_is_page_fault_vmexit(vmcs12
, fault
->error_code
) &&
10209 !to_vmx(vcpu
)->nested
.nested_run_pending
) {
10210 vmcs12
->vm_exit_intr_error_code
= fault
->error_code
;
10211 nested_vmx_vmexit(vcpu
, EXIT_REASON_EXCEPTION_NMI
,
10212 PF_VECTOR
| INTR_TYPE_HARD_EXCEPTION
|
10213 INTR_INFO_DELIVER_CODE_MASK
| INTR_INFO_VALID_MASK
,
10216 kvm_inject_page_fault(vcpu
, fault
);
10220 static inline bool nested_vmx_merge_msr_bitmap(struct kvm_vcpu
*vcpu
,
10221 struct vmcs12
*vmcs12
);
10223 static void nested_get_vmcs12_pages(struct kvm_vcpu
*vcpu
,
10224 struct vmcs12
*vmcs12
)
10226 struct vcpu_vmx
*vmx
= to_vmx(vcpu
);
10230 if (nested_cpu_has2(vmcs12
, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES
)) {
10232 * Translate L1 physical address to host physical
10233 * address for vmcs02. Keep the page pinned, so this
10234 * physical address remains valid. We keep a reference
10235 * to it so we can release it later.
10237 if (vmx
->nested
.apic_access_page
) { /* shouldn't happen */
10238 kvm_release_page_dirty(vmx
->nested
.apic_access_page
);
10239 vmx
->nested
.apic_access_page
= NULL
;
10241 page
= kvm_vcpu_gpa_to_page(vcpu
, vmcs12
->apic_access_addr
);
10243 * If translation failed, no matter: This feature asks
10244 * to exit when accessing the given address, and if it
10245 * can never be accessed, this feature won't do
10248 if (!is_error_page(page
)) {
10249 vmx
->nested
.apic_access_page
= page
;
10250 hpa
= page_to_phys(vmx
->nested
.apic_access_page
);
10251 vmcs_write64(APIC_ACCESS_ADDR
, hpa
);
10253 vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL
,
10254 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES
);
10256 } else if (!(nested_cpu_has_virt_x2apic_mode(vmcs12
)) &&
10257 cpu_need_virtualize_apic_accesses(&vmx
->vcpu
)) {
10258 vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL
,
10259 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES
);
10260 kvm_vcpu_reload_apic_access_page(vcpu
);
10263 if (nested_cpu_has(vmcs12
, CPU_BASED_TPR_SHADOW
)) {
10264 if (vmx
->nested
.virtual_apic_page
) { /* shouldn't happen */
10265 kvm_release_page_dirty(vmx
->nested
.virtual_apic_page
);
10266 vmx
->nested
.virtual_apic_page
= NULL
;
10268 page
= kvm_vcpu_gpa_to_page(vcpu
, vmcs12
->virtual_apic_page_addr
);
10271 * If translation failed, VM entry will fail because
10272 * prepare_vmcs02 set VIRTUAL_APIC_PAGE_ADDR to -1ull.
10273 * Failing the vm entry is _not_ what the processor
10274 * does but it's basically the only possibility we
10275 * have. We could still enter the guest if CR8 load
10276 * exits are enabled, CR8 store exits are enabled, and
10277 * virtualize APIC access is disabled; in this case
10278 * the processor would never use the TPR shadow and we
10279 * could simply clear the bit from the execution
10280 * control. But such a configuration is useless, so
10281 * let's keep the code simple.
10283 if (!is_error_page(page
)) {
10284 vmx
->nested
.virtual_apic_page
= page
;
10285 hpa
= page_to_phys(vmx
->nested
.virtual_apic_page
);
10286 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR
, hpa
);
10290 if (nested_cpu_has_posted_intr(vmcs12
)) {
10291 if (vmx
->nested
.pi_desc_page
) { /* shouldn't happen */
10292 kunmap(vmx
->nested
.pi_desc_page
);
10293 kvm_release_page_dirty(vmx
->nested
.pi_desc_page
);
10294 vmx
->nested
.pi_desc_page
= NULL
;
10296 page
= kvm_vcpu_gpa_to_page(vcpu
, vmcs12
->posted_intr_desc_addr
);
10297 if (is_error_page(page
))
10299 vmx
->nested
.pi_desc_page
= page
;
10300 vmx
->nested
.pi_desc
= kmap(vmx
->nested
.pi_desc_page
);
10301 vmx
->nested
.pi_desc
=
10302 (struct pi_desc
*)((void *)vmx
->nested
.pi_desc
+
10303 (unsigned long)(vmcs12
->posted_intr_desc_addr
&
10305 vmcs_write64(POSTED_INTR_DESC_ADDR
,
10306 page_to_phys(vmx
->nested
.pi_desc_page
) +
10307 (unsigned long)(vmcs12
->posted_intr_desc_addr
&
10310 if (cpu_has_vmx_msr_bitmap() &&
10311 nested_cpu_has(vmcs12
, CPU_BASED_USE_MSR_BITMAPS
) &&
10312 nested_vmx_merge_msr_bitmap(vcpu
, vmcs12
))
10313 vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL
,
10314 CPU_BASED_USE_MSR_BITMAPS
);
10316 vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL
,
10317 CPU_BASED_USE_MSR_BITMAPS
);
10320 static void vmx_start_preemption_timer(struct kvm_vcpu
*vcpu
)
10322 u64 preemption_timeout
= get_vmcs12(vcpu
)->vmx_preemption_timer_value
;
10323 struct vcpu_vmx
*vmx
= to_vmx(vcpu
);
10325 if (vcpu
->arch
.virtual_tsc_khz
== 0)
10328 /* Make sure short timeouts reliably trigger an immediate vmexit.
10329 * hrtimer_start does not guarantee this. */
10330 if (preemption_timeout
<= 1) {
10331 vmx_preemption_timer_fn(&vmx
->nested
.preemption_timer
);
10335 preemption_timeout
<<= VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE
;
10336 preemption_timeout
*= 1000000;
10337 do_div(preemption_timeout
, vcpu
->arch
.virtual_tsc_khz
);
10338 hrtimer_start(&vmx
->nested
.preemption_timer
,
10339 ns_to_ktime(preemption_timeout
), HRTIMER_MODE_REL
);
10342 static int nested_vmx_check_io_bitmap_controls(struct kvm_vcpu
*vcpu
,
10343 struct vmcs12
*vmcs12
)
10345 if (!nested_cpu_has(vmcs12
, CPU_BASED_USE_IO_BITMAPS
))
10348 if (!page_address_valid(vcpu
, vmcs12
->io_bitmap_a
) ||
10349 !page_address_valid(vcpu
, vmcs12
->io_bitmap_b
))
10355 static int nested_vmx_check_msr_bitmap_controls(struct kvm_vcpu
*vcpu
,
10356 struct vmcs12
*vmcs12
)
10358 if (!nested_cpu_has(vmcs12
, CPU_BASED_USE_MSR_BITMAPS
))
10361 if (!page_address_valid(vcpu
, vmcs12
->msr_bitmap
))
10367 static int nested_vmx_check_tpr_shadow_controls(struct kvm_vcpu
*vcpu
,
10368 struct vmcs12
*vmcs12
)
10370 if (!nested_cpu_has(vmcs12
, CPU_BASED_TPR_SHADOW
))
10373 if (!page_address_valid(vcpu
, vmcs12
->virtual_apic_page_addr
))
10380 * Merge L0's and L1's MSR bitmap, return false to indicate that
10381 * we do not use the hardware.
10383 static inline bool nested_vmx_merge_msr_bitmap(struct kvm_vcpu
*vcpu
,
10384 struct vmcs12
*vmcs12
)
10388 unsigned long *msr_bitmap_l1
;
10389 unsigned long *msr_bitmap_l0
= to_vmx(vcpu
)->nested
.vmcs02
.msr_bitmap
;
10391 * pred_cmd & spec_ctrl are trying to verify two things:
10393 * 1. L0 gave a permission to L1 to actually passthrough the MSR. This
10394 * ensures that we do not accidentally generate an L02 MSR bitmap
10395 * from the L12 MSR bitmap that is too permissive.
10396 * 2. That L1 or L2s have actually used the MSR. This avoids
10397 * unnecessarily merging of the bitmap if the MSR is unused. This
10398 * works properly because we only update the L01 MSR bitmap lazily.
10399 * So even if L0 should pass L1 these MSRs, the L01 bitmap is only
10400 * updated to reflect this when L1 (or its L2s) actually write to
10403 bool pred_cmd
= !msr_write_intercepted_l01(vcpu
, MSR_IA32_PRED_CMD
);
10404 bool spec_ctrl
= !msr_write_intercepted_l01(vcpu
, MSR_IA32_SPEC_CTRL
);
10406 if (!nested_cpu_has_virt_x2apic_mode(vmcs12
) &&
10407 !pred_cmd
&& !spec_ctrl
)
10410 page
= kvm_vcpu_gpa_to_page(vcpu
, vmcs12
->msr_bitmap
);
10411 if (is_error_page(page
))
10413 msr_bitmap_l1
= (unsigned long *)kmap(page
);
10415 memset(msr_bitmap_l0
, 0xff, PAGE_SIZE
);
10417 if (nested_cpu_has_virt_x2apic_mode(vmcs12
)) {
10418 if (nested_cpu_has_apic_reg_virt(vmcs12
))
10419 for (msr
= 0x800; msr
<= 0x8ff; msr
++)
10420 nested_vmx_disable_intercept_for_msr(
10421 msr_bitmap_l1
, msr_bitmap_l0
,
10424 nested_vmx_disable_intercept_for_msr(
10425 msr_bitmap_l1
, msr_bitmap_l0
,
10426 APIC_BASE_MSR
+ (APIC_TASKPRI
>> 4),
10427 MSR_TYPE_R
| MSR_TYPE_W
);
10429 if (nested_cpu_has_vid(vmcs12
)) {
10430 nested_vmx_disable_intercept_for_msr(
10431 msr_bitmap_l1
, msr_bitmap_l0
,
10432 APIC_BASE_MSR
+ (APIC_EOI
>> 4),
10434 nested_vmx_disable_intercept_for_msr(
10435 msr_bitmap_l1
, msr_bitmap_l0
,
10436 APIC_BASE_MSR
+ (APIC_SELF_IPI
>> 4),
10442 nested_vmx_disable_intercept_for_msr(
10443 msr_bitmap_l1
, msr_bitmap_l0
,
10444 MSR_IA32_SPEC_CTRL
,
10445 MSR_TYPE_R
| MSR_TYPE_W
);
10448 nested_vmx_disable_intercept_for_msr(
10449 msr_bitmap_l1
, msr_bitmap_l0
,
10454 kvm_release_page_clean(page
);
10459 static int nested_vmx_check_apicv_controls(struct kvm_vcpu
*vcpu
,
10460 struct vmcs12
*vmcs12
)
10462 if (!nested_cpu_has_virt_x2apic_mode(vmcs12
) &&
10463 !nested_cpu_has_apic_reg_virt(vmcs12
) &&
10464 !nested_cpu_has_vid(vmcs12
) &&
10465 !nested_cpu_has_posted_intr(vmcs12
))
10469 * If virtualize x2apic mode is enabled,
10470 * virtualize apic access must be disabled.
10472 if (nested_cpu_has_virt_x2apic_mode(vmcs12
) &&
10473 nested_cpu_has2(vmcs12
, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES
))
10477 * If virtual interrupt delivery is enabled,
10478 * we must exit on external interrupts.
10480 if (nested_cpu_has_vid(vmcs12
) &&
10481 !nested_exit_on_intr(vcpu
))
10485 * bits 15:8 should be zero in posted_intr_nv,
10486 * the descriptor address has been already checked
10487 * in nested_get_vmcs12_pages.
10489 if (nested_cpu_has_posted_intr(vmcs12
) &&
10490 (!nested_cpu_has_vid(vmcs12
) ||
10491 !nested_exit_intr_ack_set(vcpu
) ||
10492 vmcs12
->posted_intr_nv
& 0xff00))
10495 /* tpr shadow is needed by all apicv features. */
10496 if (!nested_cpu_has(vmcs12
, CPU_BASED_TPR_SHADOW
))
10502 static int nested_vmx_check_msr_switch(struct kvm_vcpu
*vcpu
,
10503 unsigned long count_field
,
10504 unsigned long addr_field
)
10509 if (vmcs12_read_any(vcpu
, count_field
, &count
) ||
10510 vmcs12_read_any(vcpu
, addr_field
, &addr
)) {
10516 maxphyaddr
= cpuid_maxphyaddr(vcpu
);
10517 if (!IS_ALIGNED(addr
, 16) || addr
>> maxphyaddr
||
10518 (addr
+ count
* sizeof(struct vmx_msr_entry
) - 1) >> maxphyaddr
) {
10519 pr_debug_ratelimited(
10520 "nVMX: invalid MSR switch (0x%lx, %d, %llu, 0x%08llx)",
10521 addr_field
, maxphyaddr
, count
, addr
);
10527 static int nested_vmx_check_msr_switch_controls(struct kvm_vcpu
*vcpu
,
10528 struct vmcs12
*vmcs12
)
10530 if (vmcs12
->vm_exit_msr_load_count
== 0 &&
10531 vmcs12
->vm_exit_msr_store_count
== 0 &&
10532 vmcs12
->vm_entry_msr_load_count
== 0)
10533 return 0; /* Fast path */
10534 if (nested_vmx_check_msr_switch(vcpu
, VM_EXIT_MSR_LOAD_COUNT
,
10535 VM_EXIT_MSR_LOAD_ADDR
) ||
10536 nested_vmx_check_msr_switch(vcpu
, VM_EXIT_MSR_STORE_COUNT
,
10537 VM_EXIT_MSR_STORE_ADDR
) ||
10538 nested_vmx_check_msr_switch(vcpu
, VM_ENTRY_MSR_LOAD_COUNT
,
10539 VM_ENTRY_MSR_LOAD_ADDR
))
10544 static int nested_vmx_check_pml_controls(struct kvm_vcpu
*vcpu
,
10545 struct vmcs12
*vmcs12
)
10547 u64 address
= vmcs12
->pml_address
;
10548 int maxphyaddr
= cpuid_maxphyaddr(vcpu
);
10550 if (nested_cpu_has2(vmcs12
, SECONDARY_EXEC_ENABLE_PML
)) {
10551 if (!nested_cpu_has_ept(vmcs12
) ||
10552 !IS_ALIGNED(address
, 4096) ||
10553 address
>> maxphyaddr
)
10560 static int nested_vmx_msr_check_common(struct kvm_vcpu
*vcpu
,
10561 struct vmx_msr_entry
*e
)
10563 /* x2APIC MSR accesses are not allowed */
10564 if (vcpu
->arch
.apic_base
& X2APIC_ENABLE
&& e
->index
>> 8 == 0x8)
10566 if (e
->index
== MSR_IA32_UCODE_WRITE
|| /* SDM Table 35-2 */
10567 e
->index
== MSR_IA32_UCODE_REV
)
10569 if (e
->reserved
!= 0)
10574 static int nested_vmx_load_msr_check(struct kvm_vcpu
*vcpu
,
10575 struct vmx_msr_entry
*e
)
10577 if (e
->index
== MSR_FS_BASE
||
10578 e
->index
== MSR_GS_BASE
||
10579 e
->index
== MSR_IA32_SMM_MONITOR_CTL
|| /* SMM is not supported */
10580 nested_vmx_msr_check_common(vcpu
, e
))
10585 static int nested_vmx_store_msr_check(struct kvm_vcpu
*vcpu
,
10586 struct vmx_msr_entry
*e
)
10588 if (e
->index
== MSR_IA32_SMBASE
|| /* SMM is not supported */
10589 nested_vmx_msr_check_common(vcpu
, e
))
10595 * Load guest's/host's msr at nested entry/exit.
10596 * return 0 for success, entry index for failure.
10598 static u32
nested_vmx_load_msr(struct kvm_vcpu
*vcpu
, u64 gpa
, u32 count
)
10601 struct vmx_msr_entry e
;
10602 struct msr_data msr
;
10604 msr
.host_initiated
= false;
10605 for (i
= 0; i
< count
; i
++) {
10606 if (kvm_vcpu_read_guest(vcpu
, gpa
+ i
* sizeof(e
),
10608 pr_debug_ratelimited(
10609 "%s cannot read MSR entry (%u, 0x%08llx)\n",
10610 __func__
, i
, gpa
+ i
* sizeof(e
));
10613 if (nested_vmx_load_msr_check(vcpu
, &e
)) {
10614 pr_debug_ratelimited(
10615 "%s check failed (%u, 0x%x, 0x%x)\n",
10616 __func__
, i
, e
.index
, e
.reserved
);
10619 msr
.index
= e
.index
;
10620 msr
.data
= e
.value
;
10621 if (kvm_set_msr(vcpu
, &msr
)) {
10622 pr_debug_ratelimited(
10623 "%s cannot write MSR (%u, 0x%x, 0x%llx)\n",
10624 __func__
, i
, e
.index
, e
.value
);
10633 static int nested_vmx_store_msr(struct kvm_vcpu
*vcpu
, u64 gpa
, u32 count
)
10636 struct vmx_msr_entry e
;
10638 for (i
= 0; i
< count
; i
++) {
10639 struct msr_data msr_info
;
10640 if (kvm_vcpu_read_guest(vcpu
,
10641 gpa
+ i
* sizeof(e
),
10642 &e
, 2 * sizeof(u32
))) {
10643 pr_debug_ratelimited(
10644 "%s cannot read MSR entry (%u, 0x%08llx)\n",
10645 __func__
, i
, gpa
+ i
* sizeof(e
));
10648 if (nested_vmx_store_msr_check(vcpu
, &e
)) {
10649 pr_debug_ratelimited(
10650 "%s check failed (%u, 0x%x, 0x%x)\n",
10651 __func__
, i
, e
.index
, e
.reserved
);
10654 msr_info
.host_initiated
= false;
10655 msr_info
.index
= e
.index
;
10656 if (kvm_get_msr(vcpu
, &msr_info
)) {
10657 pr_debug_ratelimited(
10658 "%s cannot read MSR (%u, 0x%x)\n",
10659 __func__
, i
, e
.index
);
10662 if (kvm_vcpu_write_guest(vcpu
,
10663 gpa
+ i
* sizeof(e
) +
10664 offsetof(struct vmx_msr_entry
, value
),
10665 &msr_info
.data
, sizeof(msr_info
.data
))) {
10666 pr_debug_ratelimited(
10667 "%s cannot write MSR (%u, 0x%x, 0x%llx)\n",
10668 __func__
, i
, e
.index
, msr_info
.data
);
10675 static bool nested_cr3_valid(struct kvm_vcpu
*vcpu
, unsigned long val
)
10677 unsigned long invalid_mask
;
10679 invalid_mask
= (~0ULL) << cpuid_maxphyaddr(vcpu
);
10680 return (val
& invalid_mask
) == 0;
10684 * Load guest's/host's cr3 at nested entry/exit. nested_ept is true if we are
10685 * emulating VM entry into a guest with EPT enabled.
10686 * Returns 0 on success, 1 on failure. Invalid state exit qualification code
10687 * is assigned to entry_failure_code on failure.
10689 static int nested_vmx_load_cr3(struct kvm_vcpu
*vcpu
, unsigned long cr3
, bool nested_ept
,
10690 u32
*entry_failure_code
)
10692 if (cr3
!= kvm_read_cr3(vcpu
) || (!nested_ept
&& pdptrs_changed(vcpu
))) {
10693 if (!nested_cr3_valid(vcpu
, cr3
)) {
10694 *entry_failure_code
= ENTRY_FAIL_DEFAULT
;
10699 * If PAE paging and EPT are both on, CR3 is not used by the CPU and
10700 * must not be dereferenced.
10702 if (!is_long_mode(vcpu
) && is_pae(vcpu
) && is_paging(vcpu
) &&
10704 if (!load_pdptrs(vcpu
, vcpu
->arch
.walk_mmu
, cr3
)) {
10705 *entry_failure_code
= ENTRY_FAIL_PDPTE
;
10710 vcpu
->arch
.cr3
= cr3
;
10711 __set_bit(VCPU_EXREG_CR3
, (ulong
*)&vcpu
->arch
.regs_avail
);
10714 kvm_mmu_reset_context(vcpu
);
10719 * prepare_vmcs02 is called when the L1 guest hypervisor runs its nested
10720 * L2 guest. L1 has a vmcs for L2 (vmcs12), and this function "merges" it
10721 * with L0's requirements for its guest (a.k.a. vmcs01), so we can run the L2
10722 * guest in a way that will both be appropriate to L1's requests, and our
10723 * needs. In addition to modifying the active vmcs (which is vmcs02), this
10724 * function also has additional necessary side-effects, like setting various
10725 * vcpu->arch fields.
10726 * Returns 0 on success, 1 on failure. Invalid state exit qualification code
10727 * is assigned to entry_failure_code on failure.
10729 static int prepare_vmcs02(struct kvm_vcpu
*vcpu
, struct vmcs12
*vmcs12
,
10730 bool from_vmentry
, u32
*entry_failure_code
)
10732 struct vcpu_vmx
*vmx
= to_vmx(vcpu
);
10733 u32 exec_control
, vmcs12_exec_ctrl
;
10735 vmcs_write16(GUEST_ES_SELECTOR
, vmcs12
->guest_es_selector
);
10736 vmcs_write16(GUEST_CS_SELECTOR
, vmcs12
->guest_cs_selector
);
10737 vmcs_write16(GUEST_SS_SELECTOR
, vmcs12
->guest_ss_selector
);
10738 vmcs_write16(GUEST_DS_SELECTOR
, vmcs12
->guest_ds_selector
);
10739 vmcs_write16(GUEST_FS_SELECTOR
, vmcs12
->guest_fs_selector
);
10740 vmcs_write16(GUEST_GS_SELECTOR
, vmcs12
->guest_gs_selector
);
10741 vmcs_write16(GUEST_LDTR_SELECTOR
, vmcs12
->guest_ldtr_selector
);
10742 vmcs_write16(GUEST_TR_SELECTOR
, vmcs12
->guest_tr_selector
);
10743 vmcs_write32(GUEST_ES_LIMIT
, vmcs12
->guest_es_limit
);
10744 vmcs_write32(GUEST_CS_LIMIT
, vmcs12
->guest_cs_limit
);
10745 vmcs_write32(GUEST_SS_LIMIT
, vmcs12
->guest_ss_limit
);
10746 vmcs_write32(GUEST_DS_LIMIT
, vmcs12
->guest_ds_limit
);
10747 vmcs_write32(GUEST_FS_LIMIT
, vmcs12
->guest_fs_limit
);
10748 vmcs_write32(GUEST_GS_LIMIT
, vmcs12
->guest_gs_limit
);
10749 vmcs_write32(GUEST_LDTR_LIMIT
, vmcs12
->guest_ldtr_limit
);
10750 vmcs_write32(GUEST_TR_LIMIT
, vmcs12
->guest_tr_limit
);
10751 vmcs_write32(GUEST_GDTR_LIMIT
, vmcs12
->guest_gdtr_limit
);
10752 vmcs_write32(GUEST_IDTR_LIMIT
, vmcs12
->guest_idtr_limit
);
10753 vmcs_write32(GUEST_ES_AR_BYTES
, vmcs12
->guest_es_ar_bytes
);
10754 vmcs_write32(GUEST_CS_AR_BYTES
, vmcs12
->guest_cs_ar_bytes
);
10755 vmcs_write32(GUEST_SS_AR_BYTES
, vmcs12
->guest_ss_ar_bytes
);
10756 vmcs_write32(GUEST_DS_AR_BYTES
, vmcs12
->guest_ds_ar_bytes
);
10757 vmcs_write32(GUEST_FS_AR_BYTES
, vmcs12
->guest_fs_ar_bytes
);
10758 vmcs_write32(GUEST_GS_AR_BYTES
, vmcs12
->guest_gs_ar_bytes
);
10759 vmcs_write32(GUEST_LDTR_AR_BYTES
, vmcs12
->guest_ldtr_ar_bytes
);
10760 vmcs_write32(GUEST_TR_AR_BYTES
, vmcs12
->guest_tr_ar_bytes
);
10761 vmcs_writel(GUEST_ES_BASE
, vmcs12
->guest_es_base
);
10762 vmcs_writel(GUEST_CS_BASE
, vmcs12
->guest_cs_base
);
10763 vmcs_writel(GUEST_SS_BASE
, vmcs12
->guest_ss_base
);
10764 vmcs_writel(GUEST_DS_BASE
, vmcs12
->guest_ds_base
);
10765 vmcs_writel(GUEST_FS_BASE
, vmcs12
->guest_fs_base
);
10766 vmcs_writel(GUEST_GS_BASE
, vmcs12
->guest_gs_base
);
10767 vmcs_writel(GUEST_LDTR_BASE
, vmcs12
->guest_ldtr_base
);
10768 vmcs_writel(GUEST_TR_BASE
, vmcs12
->guest_tr_base
);
10769 vmcs_writel(GUEST_GDTR_BASE
, vmcs12
->guest_gdtr_base
);
10770 vmcs_writel(GUEST_IDTR_BASE
, vmcs12
->guest_idtr_base
);
10772 if (from_vmentry
&&
10773 (vmcs12
->vm_entry_controls
& VM_ENTRY_LOAD_DEBUG_CONTROLS
)) {
10774 kvm_set_dr(vcpu
, 7, vmcs12
->guest_dr7
);
10775 vmcs_write64(GUEST_IA32_DEBUGCTL
, vmcs12
->guest_ia32_debugctl
);
10777 kvm_set_dr(vcpu
, 7, vcpu
->arch
.dr7
);
10778 vmcs_write64(GUEST_IA32_DEBUGCTL
, vmx
->nested
.vmcs01_debugctl
);
10780 if (from_vmentry
) {
10781 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD
,
10782 vmcs12
->vm_entry_intr_info_field
);
10783 vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE
,
10784 vmcs12
->vm_entry_exception_error_code
);
10785 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN
,
10786 vmcs12
->vm_entry_instruction_len
);
10787 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO
,
10788 vmcs12
->guest_interruptibility_info
);
10789 vmx
->loaded_vmcs
->nmi_known_unmasked
=
10790 !(vmcs12
->guest_interruptibility_info
& GUEST_INTR_STATE_NMI
);
10792 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD
, 0);
10794 vmcs_write32(GUEST_SYSENTER_CS
, vmcs12
->guest_sysenter_cs
);
10795 vmx_set_rflags(vcpu
, vmcs12
->guest_rflags
);
10796 vmcs_writel(GUEST_PENDING_DBG_EXCEPTIONS
,
10797 vmcs12
->guest_pending_dbg_exceptions
);
10798 vmcs_writel(GUEST_SYSENTER_ESP
, vmcs12
->guest_sysenter_esp
);
10799 vmcs_writel(GUEST_SYSENTER_EIP
, vmcs12
->guest_sysenter_eip
);
10801 if (nested_cpu_has_xsaves(vmcs12
))
10802 vmcs_write64(XSS_EXIT_BITMAP
, vmcs12
->xss_exit_bitmap
);
10803 vmcs_write64(VMCS_LINK_POINTER
, -1ull);
10805 exec_control
= vmcs12
->pin_based_vm_exec_control
;
10807 /* Preemption timer setting is only taken from vmcs01. */
10808 exec_control
&= ~PIN_BASED_VMX_PREEMPTION_TIMER
;
10809 exec_control
|= vmcs_config
.pin_based_exec_ctrl
;
10810 if (vmx
->hv_deadline_tsc
== -1)
10811 exec_control
&= ~PIN_BASED_VMX_PREEMPTION_TIMER
;
10813 /* Posted interrupts setting is only taken from vmcs12. */
10814 if (nested_cpu_has_posted_intr(vmcs12
)) {
10815 vmx
->nested
.posted_intr_nv
= vmcs12
->posted_intr_nv
;
10816 vmx
->nested
.pi_pending
= false;
10817 vmcs_write16(POSTED_INTR_NV
, POSTED_INTR_NESTED_VECTOR
);
10819 exec_control
&= ~PIN_BASED_POSTED_INTR
;
10822 vmcs_write32(PIN_BASED_VM_EXEC_CONTROL
, exec_control
);
10824 vmx
->nested
.preemption_timer_expired
= false;
10825 if (nested_cpu_has_preemption_timer(vmcs12
))
10826 vmx_start_preemption_timer(vcpu
);
10829 * Whether page-faults are trapped is determined by a combination of
10830 * 3 settings: PFEC_MASK, PFEC_MATCH and EXCEPTION_BITMAP.PF.
10831 * If enable_ept, L0 doesn't care about page faults and we should
10832 * set all of these to L1's desires. However, if !enable_ept, L0 does
10833 * care about (at least some) page faults, and because it is not easy
10834 * (if at all possible?) to merge L0 and L1's desires, we simply ask
10835 * to exit on each and every L2 page fault. This is done by setting
10836 * MASK=MATCH=0 and (see below) EB.PF=1.
10837 * Note that below we don't need special code to set EB.PF beyond the
10838 * "or"ing of the EB of vmcs01 and vmcs12, because when enable_ept,
10839 * vmcs01's EB.PF is 0 so the "or" will take vmcs12's value, and when
10840 * !enable_ept, EB.PF is 1, so the "or" will always be 1.
10842 vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK
,
10843 enable_ept
? vmcs12
->page_fault_error_code_mask
: 0);
10844 vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH
,
10845 enable_ept
? vmcs12
->page_fault_error_code_match
: 0);
10847 if (cpu_has_secondary_exec_ctrls()) {
10848 exec_control
= vmx
->secondary_exec_control
;
10850 /* Take the following fields only from vmcs12 */
10851 exec_control
&= ~(SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES
|
10852 SECONDARY_EXEC_ENABLE_INVPCID
|
10853 SECONDARY_EXEC_RDTSCP
|
10854 SECONDARY_EXEC_XSAVES
|
10855 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY
|
10856 SECONDARY_EXEC_APIC_REGISTER_VIRT
|
10857 SECONDARY_EXEC_ENABLE_VMFUNC
);
10858 if (nested_cpu_has(vmcs12
,
10859 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS
)) {
10860 vmcs12_exec_ctrl
= vmcs12
->secondary_vm_exec_control
&
10861 ~SECONDARY_EXEC_ENABLE_PML
;
10862 exec_control
|= vmcs12_exec_ctrl
;
10865 /* All VMFUNCs are currently emulated through L0 vmexits. */
10866 if (exec_control
& SECONDARY_EXEC_ENABLE_VMFUNC
)
10867 vmcs_write64(VM_FUNCTION_CONTROL
, 0);
10869 if (exec_control
& SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY
) {
10870 vmcs_write64(EOI_EXIT_BITMAP0
,
10871 vmcs12
->eoi_exit_bitmap0
);
10872 vmcs_write64(EOI_EXIT_BITMAP1
,
10873 vmcs12
->eoi_exit_bitmap1
);
10874 vmcs_write64(EOI_EXIT_BITMAP2
,
10875 vmcs12
->eoi_exit_bitmap2
);
10876 vmcs_write64(EOI_EXIT_BITMAP3
,
10877 vmcs12
->eoi_exit_bitmap3
);
10878 vmcs_write16(GUEST_INTR_STATUS
,
10879 vmcs12
->guest_intr_status
);
10883 * Write an illegal value to APIC_ACCESS_ADDR. Later,
10884 * nested_get_vmcs12_pages will either fix it up or
10885 * remove the VM execution control.
10887 if (exec_control
& SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES
)
10888 vmcs_write64(APIC_ACCESS_ADDR
, -1ull);
10890 vmcs_write32(SECONDARY_VM_EXEC_CONTROL
, exec_control
);
10895 * Set host-state according to L0's settings (vmcs12 is irrelevant here)
10896 * Some constant fields are set here by vmx_set_constant_host_state().
10897 * Other fields are different per CPU, and will be set later when
10898 * vmx_vcpu_load() is called, and when vmx_save_host_state() is called.
10900 vmx_set_constant_host_state(vmx
);
10903 * Set the MSR load/store lists to match L0's settings.
10905 vmcs_write32(VM_EXIT_MSR_STORE_COUNT
, 0);
10906 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT
, vmx
->msr_autoload
.host
.nr
);
10907 vmcs_write64(VM_EXIT_MSR_LOAD_ADDR
, __pa(vmx
->msr_autoload
.host
.val
));
10908 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT
, vmx
->msr_autoload
.guest
.nr
);
10909 vmcs_write64(VM_ENTRY_MSR_LOAD_ADDR
, __pa(vmx
->msr_autoload
.guest
.val
));
10912 * HOST_RSP is normally set correctly in vmx_vcpu_run() just before
10913 * entry, but only if the current (host) sp changed from the value
10914 * we wrote last (vmx->host_rsp). This cache is no longer relevant
10915 * if we switch vmcs, and rather than hold a separate cache per vmcs,
10916 * here we just force the write to happen on entry.
10920 exec_control
= vmx_exec_control(vmx
); /* L0's desires */
10921 exec_control
&= ~CPU_BASED_VIRTUAL_INTR_PENDING
;
10922 exec_control
&= ~CPU_BASED_VIRTUAL_NMI_PENDING
;
10923 exec_control
&= ~CPU_BASED_TPR_SHADOW
;
10924 exec_control
|= vmcs12
->cpu_based_vm_exec_control
;
10927 * Write an illegal value to VIRTUAL_APIC_PAGE_ADDR. Later, if
10928 * nested_get_vmcs12_pages can't fix it up, the illegal value
10929 * will result in a VM entry failure.
10931 if (exec_control
& CPU_BASED_TPR_SHADOW
) {
10932 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR
, -1ull);
10933 vmcs_write32(TPR_THRESHOLD
, vmcs12
->tpr_threshold
);
10935 #ifdef CONFIG_X86_64
10936 exec_control
|= CPU_BASED_CR8_LOAD_EXITING
|
10937 CPU_BASED_CR8_STORE_EXITING
;
10942 * Merging of IO bitmap not currently supported.
10943 * Rather, exit every time.
10945 exec_control
&= ~CPU_BASED_USE_IO_BITMAPS
;
10946 exec_control
|= CPU_BASED_UNCOND_IO_EXITING
;
10948 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL
, exec_control
);
10950 /* EXCEPTION_BITMAP and CR0_GUEST_HOST_MASK should basically be the
10951 * bitwise-or of what L1 wants to trap for L2, and what we want to
10952 * trap. Note that CR0.TS also needs updating - we do this later.
10954 update_exception_bitmap(vcpu
);
10955 vcpu
->arch
.cr0_guest_owned_bits
&= ~vmcs12
->cr0_guest_host_mask
;
10956 vmcs_writel(CR0_GUEST_HOST_MASK
, ~vcpu
->arch
.cr0_guest_owned_bits
);
10958 /* L2->L1 exit controls are emulated - the hardware exit is to L0 so
10959 * we should use its exit controls. Note that VM_EXIT_LOAD_IA32_EFER
10960 * bits are further modified by vmx_set_efer() below.
10962 vmcs_write32(VM_EXIT_CONTROLS
, vmcs_config
.vmexit_ctrl
);
10964 /* vmcs12's VM_ENTRY_LOAD_IA32_EFER and VM_ENTRY_IA32E_MODE are
10965 * emulated by vmx_set_efer(), below.
10967 vm_entry_controls_init(vmx
,
10968 (vmcs12
->vm_entry_controls
& ~VM_ENTRY_LOAD_IA32_EFER
&
10969 ~VM_ENTRY_IA32E_MODE
) |
10970 (vmcs_config
.vmentry_ctrl
& ~VM_ENTRY_IA32E_MODE
));
10972 if (from_vmentry
&&
10973 (vmcs12
->vm_entry_controls
& VM_ENTRY_LOAD_IA32_PAT
)) {
10974 vmcs_write64(GUEST_IA32_PAT
, vmcs12
->guest_ia32_pat
);
10975 vcpu
->arch
.pat
= vmcs12
->guest_ia32_pat
;
10976 } else if (vmcs_config
.vmentry_ctrl
& VM_ENTRY_LOAD_IA32_PAT
) {
10977 vmcs_write64(GUEST_IA32_PAT
, vmx
->vcpu
.arch
.pat
);
10980 set_cr4_guest_host_mask(vmx
);
10982 if (from_vmentry
&&
10983 vmcs12
->vm_entry_controls
& VM_ENTRY_LOAD_BNDCFGS
)
10984 vmcs_write64(GUEST_BNDCFGS
, vmcs12
->guest_bndcfgs
);
10986 if (vmcs12
->cpu_based_vm_exec_control
& CPU_BASED_USE_TSC_OFFSETING
)
10987 vmcs_write64(TSC_OFFSET
,
10988 vcpu
->arch
.tsc_offset
+ vmcs12
->tsc_offset
);
10990 vmcs_write64(TSC_OFFSET
, vcpu
->arch
.tsc_offset
);
10991 if (kvm_has_tsc_control
)
10992 decache_tsc_multiplier(vmx
);
10994 if (cpu_has_vmx_msr_bitmap())
10995 vmcs_write64(MSR_BITMAP
, __pa(vmx
->nested
.vmcs02
.msr_bitmap
));
10999 * There is no direct mapping between vpid02 and vpid12, the
11000 * vpid02 is per-vCPU for L0 and reused while the value of
11001 * vpid12 is changed w/ one invvpid during nested vmentry.
11002 * The vpid12 is allocated by L1 for L2, so it will not
11003 * influence global bitmap(for vpid01 and vpid02 allocation)
11004 * even if spawn a lot of nested vCPUs.
11006 if (nested_cpu_has_vpid(vmcs12
) && vmx
->nested
.vpid02
) {
11007 vmcs_write16(VIRTUAL_PROCESSOR_ID
, vmx
->nested
.vpid02
);
11008 if (vmcs12
->virtual_processor_id
!= vmx
->nested
.last_vpid
) {
11009 vmx
->nested
.last_vpid
= vmcs12
->virtual_processor_id
;
11010 __vmx_flush_tlb(vcpu
, to_vmx(vcpu
)->nested
.vpid02
);
11013 vmcs_write16(VIRTUAL_PROCESSOR_ID
, vmx
->vpid
);
11014 vmx_flush_tlb(vcpu
);
11021 * Conceptually we want to copy the PML address and index from
11022 * vmcs01 here, and then back to vmcs01 on nested vmexit. But,
11023 * since we always flush the log on each vmexit, this happens
11024 * to be equivalent to simply resetting the fields in vmcs02.
11026 ASSERT(vmx
->pml_pg
);
11027 vmcs_write64(PML_ADDRESS
, page_to_phys(vmx
->pml_pg
));
11028 vmcs_write16(GUEST_PML_INDEX
, PML_ENTITY_NUM
- 1);
11031 if (nested_cpu_has_ept(vmcs12
)) {
11032 if (nested_ept_init_mmu_context(vcpu
)) {
11033 *entry_failure_code
= ENTRY_FAIL_DEFAULT
;
11036 } else if (nested_cpu_has2(vmcs12
,
11037 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES
)) {
11038 vmx_flush_tlb_ept_only(vcpu
);
11042 * This sets GUEST_CR0 to vmcs12->guest_cr0, possibly modifying those
11043 * bits which we consider mandatory enabled.
11044 * The CR0_READ_SHADOW is what L2 should have expected to read given
11045 * the specifications by L1; It's not enough to take
11046 * vmcs12->cr0_read_shadow because on our cr0_guest_host_mask we we
11047 * have more bits than L1 expected.
11049 vmx_set_cr0(vcpu
, vmcs12
->guest_cr0
);
11050 vmcs_writel(CR0_READ_SHADOW
, nested_read_cr0(vmcs12
));
11052 vmx_set_cr4(vcpu
, vmcs12
->guest_cr4
);
11053 vmcs_writel(CR4_READ_SHADOW
, nested_read_cr4(vmcs12
));
11055 if (from_vmentry
&&
11056 (vmcs12
->vm_entry_controls
& VM_ENTRY_LOAD_IA32_EFER
))
11057 vcpu
->arch
.efer
= vmcs12
->guest_ia32_efer
;
11058 else if (vmcs12
->vm_entry_controls
& VM_ENTRY_IA32E_MODE
)
11059 vcpu
->arch
.efer
|= (EFER_LMA
| EFER_LME
);
11061 vcpu
->arch
.efer
&= ~(EFER_LMA
| EFER_LME
);
11062 /* Note: modifies VM_ENTRY/EXIT_CONTROLS and GUEST/HOST_IA32_EFER */
11063 vmx_set_efer(vcpu
, vcpu
->arch
.efer
);
11065 /* Shadow page tables on either EPT or shadow page tables. */
11066 if (nested_vmx_load_cr3(vcpu
, vmcs12
->guest_cr3
, nested_cpu_has_ept(vmcs12
),
11067 entry_failure_code
))
11071 vcpu
->arch
.walk_mmu
->inject_page_fault
= vmx_inject_page_fault_nested
;
11074 * L1 may access the L2's PDPTR, so save them to construct vmcs12
11077 vmcs_write64(GUEST_PDPTR0
, vmcs12
->guest_pdptr0
);
11078 vmcs_write64(GUEST_PDPTR1
, vmcs12
->guest_pdptr1
);
11079 vmcs_write64(GUEST_PDPTR2
, vmcs12
->guest_pdptr2
);
11080 vmcs_write64(GUEST_PDPTR3
, vmcs12
->guest_pdptr3
);
11083 kvm_register_write(vcpu
, VCPU_REGS_RSP
, vmcs12
->guest_rsp
);
11084 kvm_register_write(vcpu
, VCPU_REGS_RIP
, vmcs12
->guest_rip
);
11088 static int check_vmentry_prereqs(struct kvm_vcpu
*vcpu
, struct vmcs12
*vmcs12
)
11090 struct vcpu_vmx
*vmx
= to_vmx(vcpu
);
11092 if (vmcs12
->guest_activity_state
!= GUEST_ACTIVITY_ACTIVE
&&
11093 vmcs12
->guest_activity_state
!= GUEST_ACTIVITY_HLT
)
11094 return VMXERR_ENTRY_INVALID_CONTROL_FIELD
;
11096 if (nested_vmx_check_io_bitmap_controls(vcpu
, vmcs12
))
11097 return VMXERR_ENTRY_INVALID_CONTROL_FIELD
;
11099 if (nested_vmx_check_msr_bitmap_controls(vcpu
, vmcs12
))
11100 return VMXERR_ENTRY_INVALID_CONTROL_FIELD
;
11102 if (nested_vmx_check_tpr_shadow_controls(vcpu
, vmcs12
))
11103 return VMXERR_ENTRY_INVALID_CONTROL_FIELD
;
11105 if (nested_vmx_check_apicv_controls(vcpu
, vmcs12
))
11106 return VMXERR_ENTRY_INVALID_CONTROL_FIELD
;
11108 if (nested_vmx_check_msr_switch_controls(vcpu
, vmcs12
))
11109 return VMXERR_ENTRY_INVALID_CONTROL_FIELD
;
11111 if (nested_vmx_check_pml_controls(vcpu
, vmcs12
))
11112 return VMXERR_ENTRY_INVALID_CONTROL_FIELD
;
11114 if (!vmx_control_verify(vmcs12
->cpu_based_vm_exec_control
,
11115 vmx
->nested
.nested_vmx_procbased_ctls_low
,
11116 vmx
->nested
.nested_vmx_procbased_ctls_high
) ||
11117 (nested_cpu_has(vmcs12
, CPU_BASED_ACTIVATE_SECONDARY_CONTROLS
) &&
11118 !vmx_control_verify(vmcs12
->secondary_vm_exec_control
,
11119 vmx
->nested
.nested_vmx_secondary_ctls_low
,
11120 vmx
->nested
.nested_vmx_secondary_ctls_high
)) ||
11121 !vmx_control_verify(vmcs12
->pin_based_vm_exec_control
,
11122 vmx
->nested
.nested_vmx_pinbased_ctls_low
,
11123 vmx
->nested
.nested_vmx_pinbased_ctls_high
) ||
11124 !vmx_control_verify(vmcs12
->vm_exit_controls
,
11125 vmx
->nested
.nested_vmx_exit_ctls_low
,
11126 vmx
->nested
.nested_vmx_exit_ctls_high
) ||
11127 !vmx_control_verify(vmcs12
->vm_entry_controls
,
11128 vmx
->nested
.nested_vmx_entry_ctls_low
,
11129 vmx
->nested
.nested_vmx_entry_ctls_high
))
11130 return VMXERR_ENTRY_INVALID_CONTROL_FIELD
;
11132 if (nested_cpu_has_vmfunc(vmcs12
)) {
11133 if (vmcs12
->vm_function_control
&
11134 ~vmx
->nested
.nested_vmx_vmfunc_controls
)
11135 return VMXERR_ENTRY_INVALID_CONTROL_FIELD
;
11137 if (nested_cpu_has_eptp_switching(vmcs12
)) {
11138 if (!nested_cpu_has_ept(vmcs12
) ||
11139 !page_address_valid(vcpu
, vmcs12
->eptp_list_address
))
11140 return VMXERR_ENTRY_INVALID_CONTROL_FIELD
;
11144 if (vmcs12
->cr3_target_count
> nested_cpu_vmx_misc_cr3_count(vcpu
))
11145 return VMXERR_ENTRY_INVALID_CONTROL_FIELD
;
11147 if (!nested_host_cr0_valid(vcpu
, vmcs12
->host_cr0
) ||
11148 !nested_host_cr4_valid(vcpu
, vmcs12
->host_cr4
) ||
11149 !nested_cr3_valid(vcpu
, vmcs12
->host_cr3
))
11150 return VMXERR_ENTRY_INVALID_HOST_STATE_FIELD
;
11155 static int check_vmentry_postreqs(struct kvm_vcpu
*vcpu
, struct vmcs12
*vmcs12
,
11160 *exit_qual
= ENTRY_FAIL_DEFAULT
;
11162 if (!nested_guest_cr0_valid(vcpu
, vmcs12
->guest_cr0
) ||
11163 !nested_guest_cr4_valid(vcpu
, vmcs12
->guest_cr4
))
11166 if (!nested_cpu_has2(vmcs12
, SECONDARY_EXEC_SHADOW_VMCS
) &&
11167 vmcs12
->vmcs_link_pointer
!= -1ull) {
11168 *exit_qual
= ENTRY_FAIL_VMCS_LINK_PTR
;
11173 * If the load IA32_EFER VM-entry control is 1, the following checks
11174 * are performed on the field for the IA32_EFER MSR:
11175 * - Bits reserved in the IA32_EFER MSR must be 0.
11176 * - Bit 10 (corresponding to IA32_EFER.LMA) must equal the value of
11177 * the IA-32e mode guest VM-exit control. It must also be identical
11178 * to bit 8 (LME) if bit 31 in the CR0 field (corresponding to
11181 if (to_vmx(vcpu
)->nested
.nested_run_pending
&&
11182 (vmcs12
->vm_entry_controls
& VM_ENTRY_LOAD_IA32_EFER
)) {
11183 ia32e
= (vmcs12
->vm_entry_controls
& VM_ENTRY_IA32E_MODE
) != 0;
11184 if (!kvm_valid_efer(vcpu
, vmcs12
->guest_ia32_efer
) ||
11185 ia32e
!= !!(vmcs12
->guest_ia32_efer
& EFER_LMA
) ||
11186 ((vmcs12
->guest_cr0
& X86_CR0_PG
) &&
11187 ia32e
!= !!(vmcs12
->guest_ia32_efer
& EFER_LME
)))
11192 * If the load IA32_EFER VM-exit control is 1, bits reserved in the
11193 * IA32_EFER MSR must be 0 in the field for that register. In addition,
11194 * the values of the LMA and LME bits in the field must each be that of
11195 * the host address-space size VM-exit control.
11197 if (vmcs12
->vm_exit_controls
& VM_EXIT_LOAD_IA32_EFER
) {
11198 ia32e
= (vmcs12
->vm_exit_controls
&
11199 VM_EXIT_HOST_ADDR_SPACE_SIZE
) != 0;
11200 if (!kvm_valid_efer(vcpu
, vmcs12
->host_ia32_efer
) ||
11201 ia32e
!= !!(vmcs12
->host_ia32_efer
& EFER_LMA
) ||
11202 ia32e
!= !!(vmcs12
->host_ia32_efer
& EFER_LME
))
11206 if ((vmcs12
->vm_entry_controls
& VM_ENTRY_LOAD_BNDCFGS
) &&
11207 (is_noncanonical_address(vmcs12
->guest_bndcfgs
& PAGE_MASK
, vcpu
) ||
11208 (vmcs12
->guest_bndcfgs
& MSR_IA32_BNDCFGS_RSVD
)))
11214 static int enter_vmx_non_root_mode(struct kvm_vcpu
*vcpu
, bool from_vmentry
)
11216 struct vcpu_vmx
*vmx
= to_vmx(vcpu
);
11217 struct vmcs12
*vmcs12
= get_vmcs12(vcpu
);
11221 enter_guest_mode(vcpu
);
11223 if (!(vmcs12
->vm_entry_controls
& VM_ENTRY_LOAD_DEBUG_CONTROLS
))
11224 vmx
->nested
.vmcs01_debugctl
= vmcs_read64(GUEST_IA32_DEBUGCTL
);
11226 vmx_switch_vmcs(vcpu
, &vmx
->nested
.vmcs02
);
11227 vmx_segment_cache_clear(vmx
);
11229 if (prepare_vmcs02(vcpu
, vmcs12
, from_vmentry
, &exit_qual
)) {
11230 leave_guest_mode(vcpu
);
11231 vmx_switch_vmcs(vcpu
, &vmx
->vmcs01
);
11232 nested_vmx_entry_failure(vcpu
, vmcs12
,
11233 EXIT_REASON_INVALID_STATE
, exit_qual
);
11237 nested_get_vmcs12_pages(vcpu
, vmcs12
);
11239 msr_entry_idx
= nested_vmx_load_msr(vcpu
,
11240 vmcs12
->vm_entry_msr_load_addr
,
11241 vmcs12
->vm_entry_msr_load_count
);
11242 if (msr_entry_idx
) {
11243 leave_guest_mode(vcpu
);
11244 vmx_switch_vmcs(vcpu
, &vmx
->vmcs01
);
11245 nested_vmx_entry_failure(vcpu
, vmcs12
,
11246 EXIT_REASON_MSR_LOAD_FAIL
, msr_entry_idx
);
11251 * Note no nested_vmx_succeed or nested_vmx_fail here. At this point
11252 * we are no longer running L1, and VMLAUNCH/VMRESUME has not yet
11253 * returned as far as L1 is concerned. It will only return (and set
11254 * the success flag) when L2 exits (see nested_vmx_vmexit()).
11260 * nested_vmx_run() handles a nested entry, i.e., a VMLAUNCH or VMRESUME on L1
11261 * for running an L2 nested guest.
11263 static int nested_vmx_run(struct kvm_vcpu
*vcpu
, bool launch
)
11265 struct vmcs12
*vmcs12
;
11266 struct vcpu_vmx
*vmx
= to_vmx(vcpu
);
11267 u32 interrupt_shadow
= vmx_get_interrupt_shadow(vcpu
);
11271 if (!nested_vmx_check_permission(vcpu
))
11274 if (!nested_vmx_check_vmcs12(vcpu
))
11277 vmcs12
= get_vmcs12(vcpu
);
11279 if (enable_shadow_vmcs
)
11280 copy_shadow_to_vmcs12(vmx
);
11283 * The nested entry process starts with enforcing various prerequisites
11284 * on vmcs12 as required by the Intel SDM, and act appropriately when
11285 * they fail: As the SDM explains, some conditions should cause the
11286 * instruction to fail, while others will cause the instruction to seem
11287 * to succeed, but return an EXIT_REASON_INVALID_STATE.
11288 * To speed up the normal (success) code path, we should avoid checking
11289 * for misconfigurations which will anyway be caught by the processor
11290 * when using the merged vmcs02.
11292 if (interrupt_shadow
& KVM_X86_SHADOW_INT_MOV_SS
) {
11293 nested_vmx_failValid(vcpu
,
11294 VMXERR_ENTRY_EVENTS_BLOCKED_BY_MOV_SS
);
11298 if (vmcs12
->launch_state
== launch
) {
11299 nested_vmx_failValid(vcpu
,
11300 launch
? VMXERR_VMLAUNCH_NONCLEAR_VMCS
11301 : VMXERR_VMRESUME_NONLAUNCHED_VMCS
);
11305 ret
= check_vmentry_prereqs(vcpu
, vmcs12
);
11307 nested_vmx_failValid(vcpu
, ret
);
11312 * After this point, the trap flag no longer triggers a singlestep trap
11313 * on the vm entry instructions; don't call kvm_skip_emulated_instruction.
11314 * This is not 100% correct; for performance reasons, we delegate most
11315 * of the checks on host state to the processor. If those fail,
11316 * the singlestep trap is missed.
11318 skip_emulated_instruction(vcpu
);
11320 ret
= check_vmentry_postreqs(vcpu
, vmcs12
, &exit_qual
);
11322 nested_vmx_entry_failure(vcpu
, vmcs12
,
11323 EXIT_REASON_INVALID_STATE
, exit_qual
);
11328 * We're finally done with prerequisite checking, and can start with
11329 * the nested entry.
11332 ret
= enter_vmx_non_root_mode(vcpu
, true);
11336 /* Hide L1D cache contents from the nested guest. */
11337 vmx
->vcpu
.arch
.l1tf_flush_l1d
= true;
11339 if (vmcs12
->guest_activity_state
== GUEST_ACTIVITY_HLT
)
11340 return kvm_vcpu_halt(vcpu
);
11342 vmx
->nested
.nested_run_pending
= 1;
11347 return kvm_skip_emulated_instruction(vcpu
);
11351 * On a nested exit from L2 to L1, vmcs12.guest_cr0 might not be up-to-date
11352 * because L2 may have changed some cr0 bits directly (CRO_GUEST_HOST_MASK).
11353 * This function returns the new value we should put in vmcs12.guest_cr0.
11354 * It's not enough to just return the vmcs02 GUEST_CR0. Rather,
11355 * 1. Bits that neither L0 nor L1 trapped, were set directly by L2 and are now
11356 * available in vmcs02 GUEST_CR0. (Note: It's enough to check that L0
11357 * didn't trap the bit, because if L1 did, so would L0).
11358 * 2. Bits that L1 asked to trap (and therefore L0 also did) could not have
11359 * been modified by L2, and L1 knows it. So just leave the old value of
11360 * the bit from vmcs12.guest_cr0. Note that the bit from vmcs02 GUEST_CR0
11361 * isn't relevant, because if L0 traps this bit it can set it to anything.
11362 * 3. Bits that L1 didn't trap, but L0 did. L1 believes the guest could have
11363 * changed these bits, and therefore they need to be updated, but L0
11364 * didn't necessarily allow them to be changed in GUEST_CR0 - and rather
11365 * put them in vmcs02 CR0_READ_SHADOW. So take these bits from there.
11367 static inline unsigned long
11368 vmcs12_guest_cr0(struct kvm_vcpu
*vcpu
, struct vmcs12
*vmcs12
)
11371 /*1*/ (vmcs_readl(GUEST_CR0
) & vcpu
->arch
.cr0_guest_owned_bits
) |
11372 /*2*/ (vmcs12
->guest_cr0
& vmcs12
->cr0_guest_host_mask
) |
11373 /*3*/ (vmcs_readl(CR0_READ_SHADOW
) & ~(vmcs12
->cr0_guest_host_mask
|
11374 vcpu
->arch
.cr0_guest_owned_bits
));
11377 static inline unsigned long
11378 vmcs12_guest_cr4(struct kvm_vcpu
*vcpu
, struct vmcs12
*vmcs12
)
11381 /*1*/ (vmcs_readl(GUEST_CR4
) & vcpu
->arch
.cr4_guest_owned_bits
) |
11382 /*2*/ (vmcs12
->guest_cr4
& vmcs12
->cr4_guest_host_mask
) |
11383 /*3*/ (vmcs_readl(CR4_READ_SHADOW
) & ~(vmcs12
->cr4_guest_host_mask
|
11384 vcpu
->arch
.cr4_guest_owned_bits
));
11387 static void vmcs12_save_pending_event(struct kvm_vcpu
*vcpu
,
11388 struct vmcs12
*vmcs12
)
11393 if (vcpu
->arch
.exception
.injected
) {
11394 nr
= vcpu
->arch
.exception
.nr
;
11395 idt_vectoring
= nr
| VECTORING_INFO_VALID_MASK
;
11397 if (kvm_exception_is_soft(nr
)) {
11398 vmcs12
->vm_exit_instruction_len
=
11399 vcpu
->arch
.event_exit_inst_len
;
11400 idt_vectoring
|= INTR_TYPE_SOFT_EXCEPTION
;
11402 idt_vectoring
|= INTR_TYPE_HARD_EXCEPTION
;
11404 if (vcpu
->arch
.exception
.has_error_code
) {
11405 idt_vectoring
|= VECTORING_INFO_DELIVER_CODE_MASK
;
11406 vmcs12
->idt_vectoring_error_code
=
11407 vcpu
->arch
.exception
.error_code
;
11410 vmcs12
->idt_vectoring_info_field
= idt_vectoring
;
11411 } else if (vcpu
->arch
.nmi_injected
) {
11412 vmcs12
->idt_vectoring_info_field
=
11413 INTR_TYPE_NMI_INTR
| INTR_INFO_VALID_MASK
| NMI_VECTOR
;
11414 } else if (vcpu
->arch
.interrupt
.pending
) {
11415 nr
= vcpu
->arch
.interrupt
.nr
;
11416 idt_vectoring
= nr
| VECTORING_INFO_VALID_MASK
;
11418 if (vcpu
->arch
.interrupt
.soft
) {
11419 idt_vectoring
|= INTR_TYPE_SOFT_INTR
;
11420 vmcs12
->vm_entry_instruction_len
=
11421 vcpu
->arch
.event_exit_inst_len
;
11423 idt_vectoring
|= INTR_TYPE_EXT_INTR
;
11425 vmcs12
->idt_vectoring_info_field
= idt_vectoring
;
11429 static int vmx_check_nested_events(struct kvm_vcpu
*vcpu
, bool external_intr
)
11431 struct vcpu_vmx
*vmx
= to_vmx(vcpu
);
11432 unsigned long exit_qual
;
11433 bool block_nested_events
=
11434 vmx
->nested
.nested_run_pending
|| kvm_event_needs_reinjection(vcpu
);
11436 if (vcpu
->arch
.exception
.pending
&&
11437 nested_vmx_check_exception(vcpu
, &exit_qual
)) {
11438 if (block_nested_events
)
11440 nested_vmx_inject_exception_vmexit(vcpu
, exit_qual
);
11444 if (nested_cpu_has_preemption_timer(get_vmcs12(vcpu
)) &&
11445 vmx
->nested
.preemption_timer_expired
) {
11446 if (block_nested_events
)
11448 nested_vmx_vmexit(vcpu
, EXIT_REASON_PREEMPTION_TIMER
, 0, 0);
11452 if (vcpu
->arch
.nmi_pending
&& nested_exit_on_nmi(vcpu
)) {
11453 if (block_nested_events
)
11455 nested_vmx_vmexit(vcpu
, EXIT_REASON_EXCEPTION_NMI
,
11456 NMI_VECTOR
| INTR_TYPE_NMI_INTR
|
11457 INTR_INFO_VALID_MASK
, 0);
11459 * The NMI-triggered VM exit counts as injection:
11460 * clear this one and block further NMIs.
11462 vcpu
->arch
.nmi_pending
= 0;
11463 vmx_set_nmi_mask(vcpu
, true);
11467 if ((kvm_cpu_has_interrupt(vcpu
) || external_intr
) &&
11468 nested_exit_on_intr(vcpu
)) {
11469 if (block_nested_events
)
11471 nested_vmx_vmexit(vcpu
, EXIT_REASON_EXTERNAL_INTERRUPT
, 0, 0);
11475 vmx_complete_nested_posted_interrupt(vcpu
);
11479 static u32
vmx_get_preemption_timer_value(struct kvm_vcpu
*vcpu
)
11481 ktime_t remaining
=
11482 hrtimer_get_remaining(&to_vmx(vcpu
)->nested
.preemption_timer
);
11485 if (ktime_to_ns(remaining
) <= 0)
11488 value
= ktime_to_ns(remaining
) * vcpu
->arch
.virtual_tsc_khz
;
11489 do_div(value
, 1000000);
11490 return value
>> VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE
;
11494 * Update the guest state fields of vmcs12 to reflect changes that
11495 * occurred while L2 was running. (The "IA-32e mode guest" bit of the
11496 * VM-entry controls is also updated, since this is really a guest
11499 static void sync_vmcs12(struct kvm_vcpu
*vcpu
, struct vmcs12
*vmcs12
)
11501 vmcs12
->guest_cr0
= vmcs12_guest_cr0(vcpu
, vmcs12
);
11502 vmcs12
->guest_cr4
= vmcs12_guest_cr4(vcpu
, vmcs12
);
11504 vmcs12
->guest_rsp
= kvm_register_read(vcpu
, VCPU_REGS_RSP
);
11505 vmcs12
->guest_rip
= kvm_register_read(vcpu
, VCPU_REGS_RIP
);
11506 vmcs12
->guest_rflags
= vmcs_readl(GUEST_RFLAGS
);
11508 vmcs12
->guest_es_selector
= vmcs_read16(GUEST_ES_SELECTOR
);
11509 vmcs12
->guest_cs_selector
= vmcs_read16(GUEST_CS_SELECTOR
);
11510 vmcs12
->guest_ss_selector
= vmcs_read16(GUEST_SS_SELECTOR
);
11511 vmcs12
->guest_ds_selector
= vmcs_read16(GUEST_DS_SELECTOR
);
11512 vmcs12
->guest_fs_selector
= vmcs_read16(GUEST_FS_SELECTOR
);
11513 vmcs12
->guest_gs_selector
= vmcs_read16(GUEST_GS_SELECTOR
);
11514 vmcs12
->guest_ldtr_selector
= vmcs_read16(GUEST_LDTR_SELECTOR
);
11515 vmcs12
->guest_tr_selector
= vmcs_read16(GUEST_TR_SELECTOR
);
11516 vmcs12
->guest_es_limit
= vmcs_read32(GUEST_ES_LIMIT
);
11517 vmcs12
->guest_cs_limit
= vmcs_read32(GUEST_CS_LIMIT
);
11518 vmcs12
->guest_ss_limit
= vmcs_read32(GUEST_SS_LIMIT
);
11519 vmcs12
->guest_ds_limit
= vmcs_read32(GUEST_DS_LIMIT
);
11520 vmcs12
->guest_fs_limit
= vmcs_read32(GUEST_FS_LIMIT
);
11521 vmcs12
->guest_gs_limit
= vmcs_read32(GUEST_GS_LIMIT
);
11522 vmcs12
->guest_ldtr_limit
= vmcs_read32(GUEST_LDTR_LIMIT
);
11523 vmcs12
->guest_tr_limit
= vmcs_read32(GUEST_TR_LIMIT
);
11524 vmcs12
->guest_gdtr_limit
= vmcs_read32(GUEST_GDTR_LIMIT
);
11525 vmcs12
->guest_idtr_limit
= vmcs_read32(GUEST_IDTR_LIMIT
);
11526 vmcs12
->guest_es_ar_bytes
= vmcs_read32(GUEST_ES_AR_BYTES
);
11527 vmcs12
->guest_cs_ar_bytes
= vmcs_read32(GUEST_CS_AR_BYTES
);
11528 vmcs12
->guest_ss_ar_bytes
= vmcs_read32(GUEST_SS_AR_BYTES
);
11529 vmcs12
->guest_ds_ar_bytes
= vmcs_read32(GUEST_DS_AR_BYTES
);
11530 vmcs12
->guest_fs_ar_bytes
= vmcs_read32(GUEST_FS_AR_BYTES
);
11531 vmcs12
->guest_gs_ar_bytes
= vmcs_read32(GUEST_GS_AR_BYTES
);
11532 vmcs12
->guest_ldtr_ar_bytes
= vmcs_read32(GUEST_LDTR_AR_BYTES
);
11533 vmcs12
->guest_tr_ar_bytes
= vmcs_read32(GUEST_TR_AR_BYTES
);
11534 vmcs12
->guest_es_base
= vmcs_readl(GUEST_ES_BASE
);
11535 vmcs12
->guest_cs_base
= vmcs_readl(GUEST_CS_BASE
);
11536 vmcs12
->guest_ss_base
= vmcs_readl(GUEST_SS_BASE
);
11537 vmcs12
->guest_ds_base
= vmcs_readl(GUEST_DS_BASE
);
11538 vmcs12
->guest_fs_base
= vmcs_readl(GUEST_FS_BASE
);
11539 vmcs12
->guest_gs_base
= vmcs_readl(GUEST_GS_BASE
);
11540 vmcs12
->guest_ldtr_base
= vmcs_readl(GUEST_LDTR_BASE
);
11541 vmcs12
->guest_tr_base
= vmcs_readl(GUEST_TR_BASE
);
11542 vmcs12
->guest_gdtr_base
= vmcs_readl(GUEST_GDTR_BASE
);
11543 vmcs12
->guest_idtr_base
= vmcs_readl(GUEST_IDTR_BASE
);
11545 vmcs12
->guest_interruptibility_info
=
11546 vmcs_read32(GUEST_INTERRUPTIBILITY_INFO
);
11547 vmcs12
->guest_pending_dbg_exceptions
=
11548 vmcs_readl(GUEST_PENDING_DBG_EXCEPTIONS
);
11549 if (vcpu
->arch
.mp_state
== KVM_MP_STATE_HALTED
)
11550 vmcs12
->guest_activity_state
= GUEST_ACTIVITY_HLT
;
11552 vmcs12
->guest_activity_state
= GUEST_ACTIVITY_ACTIVE
;
11554 if (nested_cpu_has_preemption_timer(vmcs12
)) {
11555 if (vmcs12
->vm_exit_controls
&
11556 VM_EXIT_SAVE_VMX_PREEMPTION_TIMER
)
11557 vmcs12
->vmx_preemption_timer_value
=
11558 vmx_get_preemption_timer_value(vcpu
);
11559 hrtimer_cancel(&to_vmx(vcpu
)->nested
.preemption_timer
);
11563 * In some cases (usually, nested EPT), L2 is allowed to change its
11564 * own CR3 without exiting. If it has changed it, we must keep it.
11565 * Of course, if L0 is using shadow page tables, GUEST_CR3 was defined
11566 * by L0, not L1 or L2, so we mustn't unconditionally copy it to vmcs12.
11568 * Additionally, restore L2's PDPTR to vmcs12.
11571 vmcs12
->guest_cr3
= vmcs_readl(GUEST_CR3
);
11572 vmcs12
->guest_pdptr0
= vmcs_read64(GUEST_PDPTR0
);
11573 vmcs12
->guest_pdptr1
= vmcs_read64(GUEST_PDPTR1
);
11574 vmcs12
->guest_pdptr2
= vmcs_read64(GUEST_PDPTR2
);
11575 vmcs12
->guest_pdptr3
= vmcs_read64(GUEST_PDPTR3
);
11578 vmcs12
->guest_linear_address
= vmcs_readl(GUEST_LINEAR_ADDRESS
);
11580 if (nested_cpu_has_vid(vmcs12
))
11581 vmcs12
->guest_intr_status
= vmcs_read16(GUEST_INTR_STATUS
);
11583 vmcs12
->vm_entry_controls
=
11584 (vmcs12
->vm_entry_controls
& ~VM_ENTRY_IA32E_MODE
) |
11585 (vm_entry_controls_get(to_vmx(vcpu
)) & VM_ENTRY_IA32E_MODE
);
11587 if (vmcs12
->vm_exit_controls
& VM_EXIT_SAVE_DEBUG_CONTROLS
) {
11588 kvm_get_dr(vcpu
, 7, (unsigned long *)&vmcs12
->guest_dr7
);
11589 vmcs12
->guest_ia32_debugctl
= vmcs_read64(GUEST_IA32_DEBUGCTL
);
11592 /* TODO: These cannot have changed unless we have MSR bitmaps and
11593 * the relevant bit asks not to trap the change */
11594 if (vmcs12
->vm_exit_controls
& VM_EXIT_SAVE_IA32_PAT
)
11595 vmcs12
->guest_ia32_pat
= vmcs_read64(GUEST_IA32_PAT
);
11596 if (vmcs12
->vm_exit_controls
& VM_EXIT_SAVE_IA32_EFER
)
11597 vmcs12
->guest_ia32_efer
= vcpu
->arch
.efer
;
11598 vmcs12
->guest_sysenter_cs
= vmcs_read32(GUEST_SYSENTER_CS
);
11599 vmcs12
->guest_sysenter_esp
= vmcs_readl(GUEST_SYSENTER_ESP
);
11600 vmcs12
->guest_sysenter_eip
= vmcs_readl(GUEST_SYSENTER_EIP
);
11601 if (kvm_mpx_supported())
11602 vmcs12
->guest_bndcfgs
= vmcs_read64(GUEST_BNDCFGS
);
11606 * prepare_vmcs12 is part of what we need to do when the nested L2 guest exits
11607 * and we want to prepare to run its L1 parent. L1 keeps a vmcs for L2 (vmcs12),
11608 * and this function updates it to reflect the changes to the guest state while
11609 * L2 was running (and perhaps made some exits which were handled directly by L0
11610 * without going back to L1), and to reflect the exit reason.
11611 * Note that we do not have to copy here all VMCS fields, just those that
11612 * could have changed by the L2 guest or the exit - i.e., the guest-state and
11613 * exit-information fields only. Other fields are modified by L1 with VMWRITE,
11614 * which already writes to vmcs12 directly.
11616 static void prepare_vmcs12(struct kvm_vcpu
*vcpu
, struct vmcs12
*vmcs12
,
11617 u32 exit_reason
, u32 exit_intr_info
,
11618 unsigned long exit_qualification
)
11620 /* update guest state fields: */
11621 sync_vmcs12(vcpu
, vmcs12
);
11623 /* update exit information fields: */
11625 vmcs12
->vm_exit_reason
= exit_reason
;
11626 vmcs12
->exit_qualification
= exit_qualification
;
11627 vmcs12
->vm_exit_intr_info
= exit_intr_info
;
11629 vmcs12
->idt_vectoring_info_field
= 0;
11630 vmcs12
->vm_exit_instruction_len
= vmcs_read32(VM_EXIT_INSTRUCTION_LEN
);
11631 vmcs12
->vmx_instruction_info
= vmcs_read32(VMX_INSTRUCTION_INFO
);
11633 if (!(vmcs12
->vm_exit_reason
& VMX_EXIT_REASONS_FAILED_VMENTRY
)) {
11634 vmcs12
->launch_state
= 1;
11636 /* vm_entry_intr_info_field is cleared on exit. Emulate this
11637 * instead of reading the real value. */
11638 vmcs12
->vm_entry_intr_info_field
&= ~INTR_INFO_VALID_MASK
;
11641 * Transfer the event that L0 or L1 may wanted to inject into
11642 * L2 to IDT_VECTORING_INFO_FIELD.
11644 vmcs12_save_pending_event(vcpu
, vmcs12
);
11648 * Drop what we picked up for L2 via vmx_complete_interrupts. It is
11649 * preserved above and would only end up incorrectly in L1.
11651 vcpu
->arch
.nmi_injected
= false;
11652 kvm_clear_exception_queue(vcpu
);
11653 kvm_clear_interrupt_queue(vcpu
);
11656 static void load_vmcs12_mmu_host_state(struct kvm_vcpu
*vcpu
,
11657 struct vmcs12
*vmcs12
)
11659 u32 entry_failure_code
;
11661 nested_ept_uninit_mmu_context(vcpu
);
11664 * Only PDPTE load can fail as the value of cr3 was checked on entry and
11665 * couldn't have changed.
11667 if (nested_vmx_load_cr3(vcpu
, vmcs12
->host_cr3
, false, &entry_failure_code
))
11668 nested_vmx_abort(vcpu
, VMX_ABORT_LOAD_HOST_PDPTE_FAIL
);
11671 vcpu
->arch
.walk_mmu
->inject_page_fault
= kvm_inject_page_fault
;
11675 * A part of what we need to when the nested L2 guest exits and we want to
11676 * run its L1 parent, is to reset L1's guest state to the host state specified
11678 * This function is to be called not only on normal nested exit, but also on
11679 * a nested entry failure, as explained in Intel's spec, 3B.23.7 ("VM-Entry
11680 * Failures During or After Loading Guest State").
11681 * This function should be called when the active VMCS is L1's (vmcs01).
11683 static void load_vmcs12_host_state(struct kvm_vcpu
*vcpu
,
11684 struct vmcs12
*vmcs12
)
11686 struct kvm_segment seg
;
11688 if (vmcs12
->vm_exit_controls
& VM_EXIT_LOAD_IA32_EFER
)
11689 vcpu
->arch
.efer
= vmcs12
->host_ia32_efer
;
11690 else if (vmcs12
->vm_exit_controls
& VM_EXIT_HOST_ADDR_SPACE_SIZE
)
11691 vcpu
->arch
.efer
|= (EFER_LMA
| EFER_LME
);
11693 vcpu
->arch
.efer
&= ~(EFER_LMA
| EFER_LME
);
11694 vmx_set_efer(vcpu
, vcpu
->arch
.efer
);
11696 kvm_register_write(vcpu
, VCPU_REGS_RSP
, vmcs12
->host_rsp
);
11697 kvm_register_write(vcpu
, VCPU_REGS_RIP
, vmcs12
->host_rip
);
11698 vmx_set_rflags(vcpu
, X86_EFLAGS_FIXED
);
11700 * Note that calling vmx_set_cr0 is important, even if cr0 hasn't
11701 * actually changed, because vmx_set_cr0 refers to efer set above.
11703 * CR0_GUEST_HOST_MASK is already set in the original vmcs01
11704 * (KVM doesn't change it);
11706 vcpu
->arch
.cr0_guest_owned_bits
= X86_CR0_TS
;
11707 vmx_set_cr0(vcpu
, vmcs12
->host_cr0
);
11709 /* Same as above - no reason to call set_cr4_guest_host_mask(). */
11710 vcpu
->arch
.cr4_guest_owned_bits
= ~vmcs_readl(CR4_GUEST_HOST_MASK
);
11711 vmx_set_cr4(vcpu
, vmcs12
->host_cr4
);
11713 load_vmcs12_mmu_host_state(vcpu
, vmcs12
);
11717 * Trivially support vpid by letting L2s share their parent
11718 * L1's vpid. TODO: move to a more elaborate solution, giving
11719 * each L2 its own vpid and exposing the vpid feature to L1.
11721 vmx_flush_tlb(vcpu
);
11723 /* Restore posted intr vector. */
11724 if (nested_cpu_has_posted_intr(vmcs12
))
11725 vmcs_write16(POSTED_INTR_NV
, POSTED_INTR_VECTOR
);
11727 vmcs_write32(GUEST_SYSENTER_CS
, vmcs12
->host_ia32_sysenter_cs
);
11728 vmcs_writel(GUEST_SYSENTER_ESP
, vmcs12
->host_ia32_sysenter_esp
);
11729 vmcs_writel(GUEST_SYSENTER_EIP
, vmcs12
->host_ia32_sysenter_eip
);
11730 vmcs_writel(GUEST_IDTR_BASE
, vmcs12
->host_idtr_base
);
11731 vmcs_writel(GUEST_GDTR_BASE
, vmcs12
->host_gdtr_base
);
11732 vmcs_write32(GUEST_IDTR_LIMIT
, 0xFFFF);
11733 vmcs_write32(GUEST_GDTR_LIMIT
, 0xFFFF);
11735 /* If not VM_EXIT_CLEAR_BNDCFGS, the L2 value propagates to L1. */
11736 if (vmcs12
->vm_exit_controls
& VM_EXIT_CLEAR_BNDCFGS
)
11737 vmcs_write64(GUEST_BNDCFGS
, 0);
11739 if (vmcs12
->vm_exit_controls
& VM_EXIT_LOAD_IA32_PAT
) {
11740 vmcs_write64(GUEST_IA32_PAT
, vmcs12
->host_ia32_pat
);
11741 vcpu
->arch
.pat
= vmcs12
->host_ia32_pat
;
11743 if (vmcs12
->vm_exit_controls
& VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL
)
11744 vmcs_write64(GUEST_IA32_PERF_GLOBAL_CTRL
,
11745 vmcs12
->host_ia32_perf_global_ctrl
);
11747 /* Set L1 segment info according to Intel SDM
11748 27.5.2 Loading Host Segment and Descriptor-Table Registers */
11749 seg
= (struct kvm_segment
) {
11751 .limit
= 0xFFFFFFFF,
11752 .selector
= vmcs12
->host_cs_selector
,
11758 if (vmcs12
->vm_exit_controls
& VM_EXIT_HOST_ADDR_SPACE_SIZE
)
11762 vmx_set_segment(vcpu
, &seg
, VCPU_SREG_CS
);
11763 seg
= (struct kvm_segment
) {
11765 .limit
= 0xFFFFFFFF,
11772 seg
.selector
= vmcs12
->host_ds_selector
;
11773 vmx_set_segment(vcpu
, &seg
, VCPU_SREG_DS
);
11774 seg
.selector
= vmcs12
->host_es_selector
;
11775 vmx_set_segment(vcpu
, &seg
, VCPU_SREG_ES
);
11776 seg
.selector
= vmcs12
->host_ss_selector
;
11777 vmx_set_segment(vcpu
, &seg
, VCPU_SREG_SS
);
11778 seg
.selector
= vmcs12
->host_fs_selector
;
11779 seg
.base
= vmcs12
->host_fs_base
;
11780 vmx_set_segment(vcpu
, &seg
, VCPU_SREG_FS
);
11781 seg
.selector
= vmcs12
->host_gs_selector
;
11782 seg
.base
= vmcs12
->host_gs_base
;
11783 vmx_set_segment(vcpu
, &seg
, VCPU_SREG_GS
);
11784 seg
= (struct kvm_segment
) {
11785 .base
= vmcs12
->host_tr_base
,
11787 .selector
= vmcs12
->host_tr_selector
,
11791 vmx_set_segment(vcpu
, &seg
, VCPU_SREG_TR
);
11793 kvm_set_dr(vcpu
, 7, 0x400);
11794 vmcs_write64(GUEST_IA32_DEBUGCTL
, 0);
11796 if (cpu_has_vmx_msr_bitmap())
11797 vmx_update_msr_bitmap(vcpu
);
11799 if (nested_vmx_load_msr(vcpu
, vmcs12
->vm_exit_msr_load_addr
,
11800 vmcs12
->vm_exit_msr_load_count
))
11801 nested_vmx_abort(vcpu
, VMX_ABORT_LOAD_HOST_MSR_FAIL
);
11805 * Emulate an exit from nested guest (L2) to L1, i.e., prepare to run L1
11806 * and modify vmcs12 to make it see what it would expect to see there if
11807 * L2 was its real guest. Must only be called when in L2 (is_guest_mode())
11809 static void nested_vmx_vmexit(struct kvm_vcpu
*vcpu
, u32 exit_reason
,
11810 u32 exit_intr_info
,
11811 unsigned long exit_qualification
)
11813 struct vcpu_vmx
*vmx
= to_vmx(vcpu
);
11814 struct vmcs12
*vmcs12
= get_vmcs12(vcpu
);
11816 /* trying to cancel vmlaunch/vmresume is a bug */
11817 WARN_ON_ONCE(vmx
->nested
.nested_run_pending
);
11820 * The only expected VM-instruction error is "VM entry with
11821 * invalid control field(s)." Anything else indicates a
11824 WARN_ON_ONCE(vmx
->fail
&& (vmcs_read32(VM_INSTRUCTION_ERROR
) !=
11825 VMXERR_ENTRY_INVALID_CONTROL_FIELD
));
11827 leave_guest_mode(vcpu
);
11829 if (likely(!vmx
->fail
)) {
11830 if (exit_reason
== -1)
11831 sync_vmcs12(vcpu
, vmcs12
);
11833 prepare_vmcs12(vcpu
, vmcs12
, exit_reason
, exit_intr_info
,
11834 exit_qualification
);
11836 if (nested_vmx_store_msr(vcpu
, vmcs12
->vm_exit_msr_store_addr
,
11837 vmcs12
->vm_exit_msr_store_count
))
11838 nested_vmx_abort(vcpu
, VMX_ABORT_SAVE_GUEST_MSR_FAIL
);
11841 vmx_switch_vmcs(vcpu
, &vmx
->vmcs01
);
11842 vm_entry_controls_reset_shadow(vmx
);
11843 vm_exit_controls_reset_shadow(vmx
);
11844 vmx_segment_cache_clear(vmx
);
11846 /* Update any VMCS fields that might have changed while L2 ran */
11847 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT
, vmx
->msr_autoload
.host
.nr
);
11848 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT
, vmx
->msr_autoload
.guest
.nr
);
11849 vmcs_write64(TSC_OFFSET
, vcpu
->arch
.tsc_offset
);
11850 if (vmx
->hv_deadline_tsc
== -1)
11851 vmcs_clear_bits(PIN_BASED_VM_EXEC_CONTROL
,
11852 PIN_BASED_VMX_PREEMPTION_TIMER
);
11854 vmcs_set_bits(PIN_BASED_VM_EXEC_CONTROL
,
11855 PIN_BASED_VMX_PREEMPTION_TIMER
);
11856 if (kvm_has_tsc_control
)
11857 decache_tsc_multiplier(vmx
);
11859 if (vmx
->nested
.change_vmcs01_virtual_x2apic_mode
) {
11860 vmx
->nested
.change_vmcs01_virtual_x2apic_mode
= false;
11861 vmx_set_virtual_x2apic_mode(vcpu
,
11862 vcpu
->arch
.apic_base
& X2APIC_ENABLE
);
11863 } else if (!nested_cpu_has_ept(vmcs12
) &&
11864 nested_cpu_has2(vmcs12
,
11865 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES
)) {
11866 vmx_flush_tlb_ept_only(vcpu
);
11869 /* This is needed for same reason as it was needed in prepare_vmcs02 */
11872 /* Unpin physical memory we referred to in vmcs02 */
11873 if (vmx
->nested
.apic_access_page
) {
11874 kvm_release_page_dirty(vmx
->nested
.apic_access_page
);
11875 vmx
->nested
.apic_access_page
= NULL
;
11877 if (vmx
->nested
.virtual_apic_page
) {
11878 kvm_release_page_dirty(vmx
->nested
.virtual_apic_page
);
11879 vmx
->nested
.virtual_apic_page
= NULL
;
11881 if (vmx
->nested
.pi_desc_page
) {
11882 kunmap(vmx
->nested
.pi_desc_page
);
11883 kvm_release_page_dirty(vmx
->nested
.pi_desc_page
);
11884 vmx
->nested
.pi_desc_page
= NULL
;
11885 vmx
->nested
.pi_desc
= NULL
;
11889 * We are now running in L2, mmu_notifier will force to reload the
11890 * page's hpa for L2 vmcs. Need to reload it for L1 before entering L1.
11892 kvm_make_request(KVM_REQ_APIC_PAGE_RELOAD
, vcpu
);
11894 if (enable_shadow_vmcs
&& exit_reason
!= -1)
11895 vmx
->nested
.sync_shadow_vmcs
= true;
11897 /* in case we halted in L2 */
11898 vcpu
->arch
.mp_state
= KVM_MP_STATE_RUNNABLE
;
11900 if (likely(!vmx
->fail
)) {
11902 * TODO: SDM says that with acknowledge interrupt on
11903 * exit, bit 31 of the VM-exit interrupt information
11904 * (valid interrupt) is always set to 1 on
11905 * EXIT_REASON_EXTERNAL_INTERRUPT, so we shouldn't
11906 * need kvm_cpu_has_interrupt(). See the commit
11907 * message for details.
11909 if (nested_exit_intr_ack_set(vcpu
) &&
11910 exit_reason
== EXIT_REASON_EXTERNAL_INTERRUPT
&&
11911 kvm_cpu_has_interrupt(vcpu
)) {
11912 int irq
= kvm_cpu_get_interrupt(vcpu
);
11914 vmcs12
->vm_exit_intr_info
= irq
|
11915 INTR_INFO_VALID_MASK
| INTR_TYPE_EXT_INTR
;
11918 if (exit_reason
!= -1)
11919 trace_kvm_nested_vmexit_inject(vmcs12
->vm_exit_reason
,
11920 vmcs12
->exit_qualification
,
11921 vmcs12
->idt_vectoring_info_field
,
11922 vmcs12
->vm_exit_intr_info
,
11923 vmcs12
->vm_exit_intr_error_code
,
11926 load_vmcs12_host_state(vcpu
, vmcs12
);
11932 * After an early L2 VM-entry failure, we're now back
11933 * in L1 which thinks it just finished a VMLAUNCH or
11934 * VMRESUME instruction, so we need to set the failure
11935 * flag and the VM-instruction error field of the VMCS
11938 nested_vmx_failValid(vcpu
, VMXERR_ENTRY_INVALID_CONTROL_FIELD
);
11940 load_vmcs12_mmu_host_state(vcpu
, vmcs12
);
11943 * The emulated instruction was already skipped in
11944 * nested_vmx_run, but the updated RIP was never
11945 * written back to the vmcs01.
11947 skip_emulated_instruction(vcpu
);
11952 * Forcibly leave nested mode in order to be able to reset the VCPU later on.
11954 static void vmx_leave_nested(struct kvm_vcpu
*vcpu
)
11956 if (is_guest_mode(vcpu
)) {
11957 to_vmx(vcpu
)->nested
.nested_run_pending
= 0;
11958 nested_vmx_vmexit(vcpu
, -1, 0, 0);
11960 free_nested(to_vmx(vcpu
));
11964 * L1's failure to enter L2 is a subset of a normal exit, as explained in
11965 * 23.7 "VM-entry failures during or after loading guest state" (this also
11966 * lists the acceptable exit-reason and exit-qualification parameters).
11967 * It should only be called before L2 actually succeeded to run, and when
11968 * vmcs01 is current (it doesn't leave_guest_mode() or switch vmcss).
11970 static void nested_vmx_entry_failure(struct kvm_vcpu
*vcpu
,
11971 struct vmcs12
*vmcs12
,
11972 u32 reason
, unsigned long qualification
)
11974 load_vmcs12_host_state(vcpu
, vmcs12
);
11975 vmcs12
->vm_exit_reason
= reason
| VMX_EXIT_REASONS_FAILED_VMENTRY
;
11976 vmcs12
->exit_qualification
= qualification
;
11977 nested_vmx_succeed(vcpu
);
11978 if (enable_shadow_vmcs
)
11979 to_vmx(vcpu
)->nested
.sync_shadow_vmcs
= true;
11982 static int vmx_check_intercept(struct kvm_vcpu
*vcpu
,
11983 struct x86_instruction_info
*info
,
11984 enum x86_intercept_stage stage
)
11986 return X86EMUL_CONTINUE
;
11989 #ifdef CONFIG_X86_64
11990 /* (a << shift) / divisor, return 1 if overflow otherwise 0 */
11991 static inline int u64_shl_div_u64(u64 a
, unsigned int shift
,
11992 u64 divisor
, u64
*result
)
11994 u64 low
= a
<< shift
, high
= a
>> (64 - shift
);
11996 /* To avoid the overflow on divq */
11997 if (high
>= divisor
)
12000 /* Low hold the result, high hold rem which is discarded */
12001 asm("divq %2\n\t" : "=a" (low
), "=d" (high
) :
12002 "rm" (divisor
), "0" (low
), "1" (high
));
12008 static int vmx_set_hv_timer(struct kvm_vcpu
*vcpu
, u64 guest_deadline_tsc
)
12010 struct vcpu_vmx
*vmx
= to_vmx(vcpu
);
12011 u64 tscl
= rdtsc();
12012 u64 guest_tscl
= kvm_read_l1_tsc(vcpu
, tscl
);
12013 u64 delta_tsc
= max(guest_deadline_tsc
, guest_tscl
) - guest_tscl
;
12015 /* Convert to host delta tsc if tsc scaling is enabled */
12016 if (vcpu
->arch
.tsc_scaling_ratio
!= kvm_default_tsc_scaling_ratio
&&
12017 u64_shl_div_u64(delta_tsc
,
12018 kvm_tsc_scaling_ratio_frac_bits
,
12019 vcpu
->arch
.tsc_scaling_ratio
,
12024 * If the delta tsc can't fit in the 32 bit after the multi shift,
12025 * we can't use the preemption timer.
12026 * It's possible that it fits on later vmentries, but checking
12027 * on every vmentry is costly so we just use an hrtimer.
12029 if (delta_tsc
>> (cpu_preemption_timer_multi
+ 32))
12032 vmx
->hv_deadline_tsc
= tscl
+ delta_tsc
;
12033 vmcs_set_bits(PIN_BASED_VM_EXEC_CONTROL
,
12034 PIN_BASED_VMX_PREEMPTION_TIMER
);
12036 return delta_tsc
== 0;
12039 static void vmx_cancel_hv_timer(struct kvm_vcpu
*vcpu
)
12041 struct vcpu_vmx
*vmx
= to_vmx(vcpu
);
12042 vmx
->hv_deadline_tsc
= -1;
12043 vmcs_clear_bits(PIN_BASED_VM_EXEC_CONTROL
,
12044 PIN_BASED_VMX_PREEMPTION_TIMER
);
12048 static void vmx_sched_in(struct kvm_vcpu
*vcpu
, int cpu
)
12051 shrink_ple_window(vcpu
);
12054 static void vmx_slot_enable_log_dirty(struct kvm
*kvm
,
12055 struct kvm_memory_slot
*slot
)
12057 kvm_mmu_slot_leaf_clear_dirty(kvm
, slot
);
12058 kvm_mmu_slot_largepage_remove_write_access(kvm
, slot
);
12061 static void vmx_slot_disable_log_dirty(struct kvm
*kvm
,
12062 struct kvm_memory_slot
*slot
)
12064 kvm_mmu_slot_set_dirty(kvm
, slot
);
12067 static void vmx_flush_log_dirty(struct kvm
*kvm
)
12069 kvm_flush_pml_buffers(kvm
);
12072 static int vmx_write_pml_buffer(struct kvm_vcpu
*vcpu
)
12074 struct vmcs12
*vmcs12
;
12075 struct vcpu_vmx
*vmx
= to_vmx(vcpu
);
12077 struct page
*page
= NULL
;
12080 if (is_guest_mode(vcpu
)) {
12081 WARN_ON_ONCE(vmx
->nested
.pml_full
);
12084 * Check if PML is enabled for the nested guest.
12085 * Whether eptp bit 6 is set is already checked
12086 * as part of A/D emulation.
12088 vmcs12
= get_vmcs12(vcpu
);
12089 if (!nested_cpu_has_pml(vmcs12
))
12092 if (vmcs12
->guest_pml_index
>= PML_ENTITY_NUM
) {
12093 vmx
->nested
.pml_full
= true;
12097 gpa
= vmcs_read64(GUEST_PHYSICAL_ADDRESS
) & ~0xFFFull
;
12099 page
= kvm_vcpu_gpa_to_page(vcpu
, vmcs12
->pml_address
);
12100 if (is_error_page(page
))
12103 pml_address
= kmap(page
);
12104 pml_address
[vmcs12
->guest_pml_index
--] = gpa
;
12106 kvm_release_page_clean(page
);
12112 static void vmx_enable_log_dirty_pt_masked(struct kvm
*kvm
,
12113 struct kvm_memory_slot
*memslot
,
12114 gfn_t offset
, unsigned long mask
)
12116 kvm_mmu_clear_dirty_pt_masked(kvm
, memslot
, offset
, mask
);
12119 static void __pi_post_block(struct kvm_vcpu
*vcpu
)
12121 struct pi_desc
*pi_desc
= vcpu_to_pi_desc(vcpu
);
12122 struct pi_desc old
, new;
12126 old
.control
= new.control
= pi_desc
->control
;
12127 WARN(old
.nv
!= POSTED_INTR_WAKEUP_VECTOR
,
12128 "Wakeup handler not enabled while the VCPU is blocked\n");
12130 dest
= cpu_physical_id(vcpu
->cpu
);
12132 if (x2apic_enabled())
12135 new.ndst
= (dest
<< 8) & 0xFF00;
12137 /* set 'NV' to 'notification vector' */
12138 new.nv
= POSTED_INTR_VECTOR
;
12139 } while (cmpxchg64(&pi_desc
->control
, old
.control
,
12140 new.control
) != old
.control
);
12142 if (!WARN_ON_ONCE(vcpu
->pre_pcpu
== -1)) {
12143 spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock
, vcpu
->pre_pcpu
));
12144 list_del(&vcpu
->blocked_vcpu_list
);
12145 spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock
, vcpu
->pre_pcpu
));
12146 vcpu
->pre_pcpu
= -1;
12151 * This routine does the following things for vCPU which is going
12152 * to be blocked if VT-d PI is enabled.
12153 * - Store the vCPU to the wakeup list, so when interrupts happen
12154 * we can find the right vCPU to wake up.
12155 * - Change the Posted-interrupt descriptor as below:
12156 * 'NDST' <-- vcpu->pre_pcpu
12157 * 'NV' <-- POSTED_INTR_WAKEUP_VECTOR
12158 * - If 'ON' is set during this process, which means at least one
12159 * interrupt is posted for this vCPU, we cannot block it, in
12160 * this case, return 1, otherwise, return 0.
12163 static int pi_pre_block(struct kvm_vcpu
*vcpu
)
12166 struct pi_desc old
, new;
12167 struct pi_desc
*pi_desc
= vcpu_to_pi_desc(vcpu
);
12169 if (!kvm_arch_has_assigned_device(vcpu
->kvm
) ||
12170 !irq_remapping_cap(IRQ_POSTING_CAP
) ||
12171 !kvm_vcpu_apicv_active(vcpu
))
12174 WARN_ON(irqs_disabled());
12175 local_irq_disable();
12176 if (!WARN_ON_ONCE(vcpu
->pre_pcpu
!= -1)) {
12177 vcpu
->pre_pcpu
= vcpu
->cpu
;
12178 spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock
, vcpu
->pre_pcpu
));
12179 list_add_tail(&vcpu
->blocked_vcpu_list
,
12180 &per_cpu(blocked_vcpu_on_cpu
,
12182 spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock
, vcpu
->pre_pcpu
));
12186 old
.control
= new.control
= pi_desc
->control
;
12188 WARN((pi_desc
->sn
== 1),
12189 "Warning: SN field of posted-interrupts "
12190 "is set before blocking\n");
12193 * Since vCPU can be preempted during this process,
12194 * vcpu->cpu could be different with pre_pcpu, we
12195 * need to set pre_pcpu as the destination of wakeup
12196 * notification event, then we can find the right vCPU
12197 * to wakeup in wakeup handler if interrupts happen
12198 * when the vCPU is in blocked state.
12200 dest
= cpu_physical_id(vcpu
->pre_pcpu
);
12202 if (x2apic_enabled())
12205 new.ndst
= (dest
<< 8) & 0xFF00;
12207 /* set 'NV' to 'wakeup vector' */
12208 new.nv
= POSTED_INTR_WAKEUP_VECTOR
;
12209 } while (cmpxchg64(&pi_desc
->control
, old
.control
,
12210 new.control
) != old
.control
);
12212 /* We should not block the vCPU if an interrupt is posted for it. */
12213 if (pi_test_on(pi_desc
) == 1)
12214 __pi_post_block(vcpu
);
12216 local_irq_enable();
12217 return (vcpu
->pre_pcpu
== -1);
12220 static int vmx_pre_block(struct kvm_vcpu
*vcpu
)
12222 if (pi_pre_block(vcpu
))
12225 if (kvm_lapic_hv_timer_in_use(vcpu
))
12226 kvm_lapic_switch_to_sw_timer(vcpu
);
12231 static void pi_post_block(struct kvm_vcpu
*vcpu
)
12233 if (vcpu
->pre_pcpu
== -1)
12236 WARN_ON(irqs_disabled());
12237 local_irq_disable();
12238 __pi_post_block(vcpu
);
12239 local_irq_enable();
12242 static void vmx_post_block(struct kvm_vcpu
*vcpu
)
12244 if (kvm_x86_ops
->set_hv_timer
)
12245 kvm_lapic_switch_to_hv_timer(vcpu
);
12247 pi_post_block(vcpu
);
12251 * vmx_update_pi_irte - set IRTE for Posted-Interrupts
12254 * @host_irq: host irq of the interrupt
12255 * @guest_irq: gsi of the interrupt
12256 * @set: set or unset PI
12257 * returns 0 on success, < 0 on failure
12259 static int vmx_update_pi_irte(struct kvm
*kvm
, unsigned int host_irq
,
12260 uint32_t guest_irq
, bool set
)
12262 struct kvm_kernel_irq_routing_entry
*e
;
12263 struct kvm_irq_routing_table
*irq_rt
;
12264 struct kvm_lapic_irq irq
;
12265 struct kvm_vcpu
*vcpu
;
12266 struct vcpu_data vcpu_info
;
12269 if (!kvm_arch_has_assigned_device(kvm
) ||
12270 !irq_remapping_cap(IRQ_POSTING_CAP
) ||
12271 !kvm_vcpu_apicv_active(kvm
->vcpus
[0]))
12274 idx
= srcu_read_lock(&kvm
->irq_srcu
);
12275 irq_rt
= srcu_dereference(kvm
->irq_routing
, &kvm
->irq_srcu
);
12276 if (guest_irq
>= irq_rt
->nr_rt_entries
||
12277 hlist_empty(&irq_rt
->map
[guest_irq
])) {
12278 pr_warn_once("no route for guest_irq %u/%u (broken user space?)\n",
12279 guest_irq
, irq_rt
->nr_rt_entries
);
12283 hlist_for_each_entry(e
, &irq_rt
->map
[guest_irq
], link
) {
12284 if (e
->type
!= KVM_IRQ_ROUTING_MSI
)
12287 * VT-d PI cannot support posting multicast/broadcast
12288 * interrupts to a vCPU, we still use interrupt remapping
12289 * for these kind of interrupts.
12291 * For lowest-priority interrupts, we only support
12292 * those with single CPU as the destination, e.g. user
12293 * configures the interrupts via /proc/irq or uses
12294 * irqbalance to make the interrupts single-CPU.
12296 * We will support full lowest-priority interrupt later.
12299 kvm_set_msi_irq(kvm
, e
, &irq
);
12300 if (!kvm_intr_is_single_vcpu(kvm
, &irq
, &vcpu
)) {
12302 * Make sure the IRTE is in remapped mode if
12303 * we don't handle it in posted mode.
12305 ret
= irq_set_vcpu_affinity(host_irq
, NULL
);
12308 "failed to back to remapped mode, irq: %u\n",
12316 vcpu_info
.pi_desc_addr
= __pa(vcpu_to_pi_desc(vcpu
));
12317 vcpu_info
.vector
= irq
.vector
;
12319 trace_kvm_pi_irte_update(vcpu
->vcpu_id
, host_irq
, e
->gsi
,
12320 vcpu_info
.vector
, vcpu_info
.pi_desc_addr
, set
);
12323 ret
= irq_set_vcpu_affinity(host_irq
, &vcpu_info
);
12325 ret
= irq_set_vcpu_affinity(host_irq
, NULL
);
12328 printk(KERN_INFO
"%s: failed to update PI IRTE\n",
12336 srcu_read_unlock(&kvm
->irq_srcu
, idx
);
12340 static void vmx_setup_mce(struct kvm_vcpu
*vcpu
)
12342 if (vcpu
->arch
.mcg_cap
& MCG_LMCE_P
)
12343 to_vmx(vcpu
)->msr_ia32_feature_control_valid_bits
|=
12344 FEATURE_CONTROL_LMCE
;
12346 to_vmx(vcpu
)->msr_ia32_feature_control_valid_bits
&=
12347 ~FEATURE_CONTROL_LMCE
;
12350 static int vmx_smi_allowed(struct kvm_vcpu
*vcpu
)
12352 /* we need a nested vmexit to enter SMM, postpone if run is pending */
12353 if (to_vmx(vcpu
)->nested
.nested_run_pending
)
12358 static int vmx_pre_enter_smm(struct kvm_vcpu
*vcpu
, char *smstate
)
12360 struct vcpu_vmx
*vmx
= to_vmx(vcpu
);
12362 vmx
->nested
.smm
.guest_mode
= is_guest_mode(vcpu
);
12363 if (vmx
->nested
.smm
.guest_mode
)
12364 nested_vmx_vmexit(vcpu
, -1, 0, 0);
12366 vmx
->nested
.smm
.vmxon
= vmx
->nested
.vmxon
;
12367 vmx
->nested
.vmxon
= false;
12371 static int vmx_pre_leave_smm(struct kvm_vcpu
*vcpu
, u64 smbase
)
12373 struct vcpu_vmx
*vmx
= to_vmx(vcpu
);
12376 if (vmx
->nested
.smm
.vmxon
) {
12377 vmx
->nested
.vmxon
= true;
12378 vmx
->nested
.smm
.vmxon
= false;
12381 if (vmx
->nested
.smm
.guest_mode
) {
12382 vcpu
->arch
.hflags
&= ~HF_SMM_MASK
;
12383 ret
= enter_vmx_non_root_mode(vcpu
, false);
12384 vcpu
->arch
.hflags
|= HF_SMM_MASK
;
12388 vmx
->nested
.smm
.guest_mode
= false;
12393 static int enable_smi_window(struct kvm_vcpu
*vcpu
)
12398 static struct kvm_x86_ops vmx_x86_ops __ro_after_init
= {
12399 .cpu_has_kvm_support
= cpu_has_kvm_support
,
12400 .disabled_by_bios
= vmx_disabled_by_bios
,
12401 .hardware_setup
= hardware_setup
,
12402 .hardware_unsetup
= hardware_unsetup
,
12403 .check_processor_compatibility
= vmx_check_processor_compat
,
12404 .hardware_enable
= hardware_enable
,
12405 .hardware_disable
= hardware_disable
,
12406 .cpu_has_accelerated_tpr
= report_flexpriority
,
12407 .has_emulated_msr
= vmx_has_emulated_msr
,
12409 .vm_init
= vmx_vm_init
,
12411 .vcpu_create
= vmx_create_vcpu
,
12412 .vcpu_free
= vmx_free_vcpu
,
12413 .vcpu_reset
= vmx_vcpu_reset
,
12415 .prepare_guest_switch
= vmx_save_host_state
,
12416 .vcpu_load
= vmx_vcpu_load
,
12417 .vcpu_put
= vmx_vcpu_put
,
12419 .update_bp_intercept
= update_exception_bitmap
,
12420 .get_msr
= vmx_get_msr
,
12421 .set_msr
= vmx_set_msr
,
12422 .get_segment_base
= vmx_get_segment_base
,
12423 .get_segment
= vmx_get_segment
,
12424 .set_segment
= vmx_set_segment
,
12425 .get_cpl
= vmx_get_cpl
,
12426 .get_cs_db_l_bits
= vmx_get_cs_db_l_bits
,
12427 .decache_cr0_guest_bits
= vmx_decache_cr0_guest_bits
,
12428 .decache_cr3
= vmx_decache_cr3
,
12429 .decache_cr4_guest_bits
= vmx_decache_cr4_guest_bits
,
12430 .set_cr0
= vmx_set_cr0
,
12431 .set_cr3
= vmx_set_cr3
,
12432 .set_cr4
= vmx_set_cr4
,
12433 .set_efer
= vmx_set_efer
,
12434 .get_idt
= vmx_get_idt
,
12435 .set_idt
= vmx_set_idt
,
12436 .get_gdt
= vmx_get_gdt
,
12437 .set_gdt
= vmx_set_gdt
,
12438 .get_dr6
= vmx_get_dr6
,
12439 .set_dr6
= vmx_set_dr6
,
12440 .set_dr7
= vmx_set_dr7
,
12441 .sync_dirty_debug_regs
= vmx_sync_dirty_debug_regs
,
12442 .cache_reg
= vmx_cache_reg
,
12443 .get_rflags
= vmx_get_rflags
,
12444 .set_rflags
= vmx_set_rflags
,
12446 .tlb_flush
= vmx_flush_tlb
,
12448 .run
= vmx_vcpu_run
,
12449 .handle_exit
= vmx_handle_exit
,
12450 .skip_emulated_instruction
= skip_emulated_instruction
,
12451 .set_interrupt_shadow
= vmx_set_interrupt_shadow
,
12452 .get_interrupt_shadow
= vmx_get_interrupt_shadow
,
12453 .patch_hypercall
= vmx_patch_hypercall
,
12454 .set_irq
= vmx_inject_irq
,
12455 .set_nmi
= vmx_inject_nmi
,
12456 .queue_exception
= vmx_queue_exception
,
12457 .cancel_injection
= vmx_cancel_injection
,
12458 .interrupt_allowed
= vmx_interrupt_allowed
,
12459 .nmi_allowed
= vmx_nmi_allowed
,
12460 .get_nmi_mask
= vmx_get_nmi_mask
,
12461 .set_nmi_mask
= vmx_set_nmi_mask
,
12462 .enable_nmi_window
= enable_nmi_window
,
12463 .enable_irq_window
= enable_irq_window
,
12464 .update_cr8_intercept
= update_cr8_intercept
,
12465 .set_virtual_x2apic_mode
= vmx_set_virtual_x2apic_mode
,
12466 .set_apic_access_page_addr
= vmx_set_apic_access_page_addr
,
12467 .get_enable_apicv
= vmx_get_enable_apicv
,
12468 .refresh_apicv_exec_ctrl
= vmx_refresh_apicv_exec_ctrl
,
12469 .load_eoi_exitmap
= vmx_load_eoi_exitmap
,
12470 .apicv_post_state_restore
= vmx_apicv_post_state_restore
,
12471 .hwapic_irr_update
= vmx_hwapic_irr_update
,
12472 .hwapic_isr_update
= vmx_hwapic_isr_update
,
12473 .sync_pir_to_irr
= vmx_sync_pir_to_irr
,
12474 .deliver_posted_interrupt
= vmx_deliver_posted_interrupt
,
12476 .set_tss_addr
= vmx_set_tss_addr
,
12477 .get_tdp_level
= get_ept_level
,
12478 .get_mt_mask
= vmx_get_mt_mask
,
12480 .get_exit_info
= vmx_get_exit_info
,
12482 .get_lpage_level
= vmx_get_lpage_level
,
12484 .cpuid_update
= vmx_cpuid_update
,
12486 .rdtscp_supported
= vmx_rdtscp_supported
,
12487 .invpcid_supported
= vmx_invpcid_supported
,
12489 .set_supported_cpuid
= vmx_set_supported_cpuid
,
12491 .has_wbinvd_exit
= cpu_has_vmx_wbinvd_exit
,
12493 .write_tsc_offset
= vmx_write_tsc_offset
,
12495 .set_tdp_cr3
= vmx_set_cr3
,
12497 .check_intercept
= vmx_check_intercept
,
12498 .handle_external_intr
= vmx_handle_external_intr
,
12499 .mpx_supported
= vmx_mpx_supported
,
12500 .xsaves_supported
= vmx_xsaves_supported
,
12502 .check_nested_events
= vmx_check_nested_events
,
12504 .sched_in
= vmx_sched_in
,
12506 .slot_enable_log_dirty
= vmx_slot_enable_log_dirty
,
12507 .slot_disable_log_dirty
= vmx_slot_disable_log_dirty
,
12508 .flush_log_dirty
= vmx_flush_log_dirty
,
12509 .enable_log_dirty_pt_masked
= vmx_enable_log_dirty_pt_masked
,
12510 .write_log_dirty
= vmx_write_pml_buffer
,
12512 .pre_block
= vmx_pre_block
,
12513 .post_block
= vmx_post_block
,
12515 .pmu_ops
= &intel_pmu_ops
,
12517 .update_pi_irte
= vmx_update_pi_irte
,
12519 #ifdef CONFIG_X86_64
12520 .set_hv_timer
= vmx_set_hv_timer
,
12521 .cancel_hv_timer
= vmx_cancel_hv_timer
,
12524 .setup_mce
= vmx_setup_mce
,
12526 .smi_allowed
= vmx_smi_allowed
,
12527 .pre_enter_smm
= vmx_pre_enter_smm
,
12528 .pre_leave_smm
= vmx_pre_leave_smm
,
12529 .enable_smi_window
= enable_smi_window
,
12532 static int __init
vmx_setup_l1d_flush(void)
12536 if (!boot_cpu_has_bug(X86_BUG_L1TF
))
12540 l1tf_vmx_mitigation
= VMENTER_L1D_FLUSH_EPT_DISABLED
;
12544 l1tf_vmx_mitigation
= vmentry_l1d_flush
;
12546 if (vmentry_l1d_flush
== VMENTER_L1D_FLUSH_NEVER
)
12549 if (!boot_cpu_has(X86_FEATURE_FLUSH_L1D
)) {
12550 page
= alloc_pages(GFP_KERNEL
, L1D_CACHE_ORDER
);
12553 vmx_l1d_flush_pages
= page_address(page
);
12556 static_branch_enable(&vmx_l1d_should_flush
);
12560 static void vmx_cleanup_l1d_flush(void)
12562 if (vmx_l1d_flush_pages
) {
12563 free_pages((unsigned long)vmx_l1d_flush_pages
, L1D_CACHE_ORDER
);
12564 vmx_l1d_flush_pages
= NULL
;
12566 /* Restore state so sysfs ignores VMX */
12567 l1tf_vmx_mitigation
= VMENTER_L1D_FLUSH_AUTO
;
12570 static void vmx_exit(void)
12572 #ifdef CONFIG_KEXEC_CORE
12573 RCU_INIT_POINTER(crash_vmclear_loaded_vmcss
, NULL
);
12579 vmx_cleanup_l1d_flush();
12581 module_exit(vmx_exit
)
12583 static int __init
vmx_init(void)
12587 r
= kvm_init(&vmx_x86_ops
, sizeof(struct vcpu_vmx
),
12588 __alignof__(struct vcpu_vmx
), THIS_MODULE
);
12593 * Must be called after kvm_init() so enable_ept is properly set up
12595 r
= vmx_setup_l1d_flush();
12601 #ifdef CONFIG_KEXEC_CORE
12602 rcu_assign_pointer(crash_vmclear_loaded_vmcss
,
12603 crash_vmclear_local_loaded_vmcss
);
12608 module_init(vmx_init
)