2 * Kernel-based Virtual Machine driver for Linux
4 * This module enables machines with Intel VT-x extensions to run virtual
5 * machines without emulation or binary translation.
7 * Copyright (C) 2006 Qumranet, Inc.
8 * Copyright 2010 Red Hat, Inc. and/or its affiliates.
11 * Avi Kivity <avi@qumranet.com>
12 * Yaniv Kamay <yaniv@qumranet.com>
14 * This work is licensed under the terms of the GNU GPL, version 2. See
15 * the COPYING file in the top-level directory.
24 #include <linux/kvm_host.h>
25 #include <linux/module.h>
26 #include <linux/kernel.h>
28 #include <linux/highmem.h>
29 #include <linux/sched.h>
30 #include <linux/moduleparam.h>
31 #include <linux/mod_devicetable.h>
32 #include <linux/trace_events.h>
33 #include <linux/slab.h>
34 #include <linux/tboot.h>
35 #include <linux/hrtimer.h>
36 #include <linux/frame.h>
37 #include <linux/nospec.h>
38 #include "kvm_cache_regs.h"
45 #include <asm/virtext.h>
47 #include <asm/fpu/internal.h>
48 #include <asm/perf_event.h>
49 #include <asm/debugreg.h>
50 #include <asm/kexec.h>
52 #include <asm/irq_remapping.h>
53 #include <asm/mmu_context.h>
54 #include <asm/nospec-branch.h>
59 #define __ex(x) __kvm_handle_fault_on_reboot(x)
60 #define __ex_clear(x, reg) \
61 ____kvm_handle_fault_on_reboot(x, "xor " reg " , " reg)
63 MODULE_AUTHOR("Qumranet");
64 MODULE_LICENSE("GPL");
66 static const struct x86_cpu_id vmx_cpu_id
[] = {
67 X86_FEATURE_MATCH(X86_FEATURE_VMX
),
70 MODULE_DEVICE_TABLE(x86cpu
, vmx_cpu_id
);
72 static bool __read_mostly enable_vpid
= 1;
73 module_param_named(vpid
, enable_vpid
, bool, 0444);
75 static bool __read_mostly enable_vnmi
= 1;
76 module_param_named(vnmi
, enable_vnmi
, bool, S_IRUGO
);
78 static bool __read_mostly flexpriority_enabled
= 1;
79 module_param_named(flexpriority
, flexpriority_enabled
, bool, S_IRUGO
);
81 static bool __read_mostly enable_ept
= 1;
82 module_param_named(ept
, enable_ept
, bool, S_IRUGO
);
84 static bool __read_mostly enable_unrestricted_guest
= 1;
85 module_param_named(unrestricted_guest
,
86 enable_unrestricted_guest
, bool, S_IRUGO
);
88 static bool __read_mostly enable_ept_ad_bits
= 1;
89 module_param_named(eptad
, enable_ept_ad_bits
, bool, S_IRUGO
);
91 static bool __read_mostly emulate_invalid_guest_state
= true;
92 module_param(emulate_invalid_guest_state
, bool, S_IRUGO
);
94 static bool __read_mostly fasteoi
= 1;
95 module_param(fasteoi
, bool, S_IRUGO
);
97 static bool __read_mostly enable_apicv
= 1;
98 module_param(enable_apicv
, bool, S_IRUGO
);
100 static bool __read_mostly enable_shadow_vmcs
= 1;
101 module_param_named(enable_shadow_vmcs
, enable_shadow_vmcs
, bool, S_IRUGO
);
103 * If nested=1, nested virtualization is supported, i.e., guests may use
104 * VMX and be a hypervisor for its own guests. If nested=0, guests may not
105 * use VMX instructions.
107 static bool __read_mostly nested
= 0;
108 module_param(nested
, bool, S_IRUGO
);
110 static u64 __read_mostly host_xss
;
112 static bool __read_mostly enable_pml
= 1;
113 module_param_named(pml
, enable_pml
, bool, S_IRUGO
);
117 #define MSR_TYPE_RW 3
119 #define MSR_BITMAP_MODE_X2APIC 1
120 #define MSR_BITMAP_MODE_X2APIC_APICV 2
121 #define MSR_BITMAP_MODE_LM 4
123 #define KVM_VMX_TSC_MULTIPLIER_MAX 0xffffffffffffffffULL
125 /* Guest_tsc -> host_tsc conversion requires 64-bit division. */
126 static int __read_mostly cpu_preemption_timer_multi
;
127 static bool __read_mostly enable_preemption_timer
= 1;
129 module_param_named(preemption_timer
, enable_preemption_timer
, bool, S_IRUGO
);
132 #define KVM_GUEST_CR0_MASK (X86_CR0_NW | X86_CR0_CD)
133 #define KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST (X86_CR0_WP | X86_CR0_NE)
134 #define KVM_VM_CR0_ALWAYS_ON \
135 (KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST | X86_CR0_PG | X86_CR0_PE)
136 #define KVM_CR4_GUEST_OWNED_BITS \
137 (X86_CR4_PVI | X86_CR4_DE | X86_CR4_PCE | X86_CR4_OSFXSR \
138 | X86_CR4_OSXMMEXCPT | X86_CR4_LA57 | X86_CR4_TSD)
140 #define KVM_PMODE_VM_CR4_ALWAYS_ON (X86_CR4_PAE | X86_CR4_VMXE)
141 #define KVM_RMODE_VM_CR4_ALWAYS_ON (X86_CR4_VME | X86_CR4_PAE | X86_CR4_VMXE)
143 #define RMODE_GUEST_OWNED_EFLAGS_BITS (~(X86_EFLAGS_IOPL | X86_EFLAGS_VM))
145 #define VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE 5
148 * Hyper-V requires all of these, so mark them as supported even though
149 * they are just treated the same as all-context.
151 #define VMX_VPID_EXTENT_SUPPORTED_MASK \
152 (VMX_VPID_EXTENT_INDIVIDUAL_ADDR_BIT | \
153 VMX_VPID_EXTENT_SINGLE_CONTEXT_BIT | \
154 VMX_VPID_EXTENT_GLOBAL_CONTEXT_BIT | \
155 VMX_VPID_EXTENT_SINGLE_NON_GLOBAL_BIT)
158 * These 2 parameters are used to config the controls for Pause-Loop Exiting:
159 * ple_gap: upper bound on the amount of time between two successive
160 * executions of PAUSE in a loop. Also indicate if ple enabled.
161 * According to test, this time is usually smaller than 128 cycles.
162 * ple_window: upper bound on the amount of time a guest is allowed to execute
163 * in a PAUSE loop. Tests indicate that most spinlocks are held for
164 * less than 2^12 cycles
165 * Time is measured based on a counter that runs at the same rate as the TSC,
166 * refer SDM volume 3b section 21.6.13 & 22.1.3.
168 #define KVM_VMX_DEFAULT_PLE_GAP 128
169 #define KVM_VMX_DEFAULT_PLE_WINDOW 4096
170 #define KVM_VMX_DEFAULT_PLE_WINDOW_GROW 2
171 #define KVM_VMX_DEFAULT_PLE_WINDOW_SHRINK 0
172 #define KVM_VMX_DEFAULT_PLE_WINDOW_MAX \
173 INT_MAX / KVM_VMX_DEFAULT_PLE_WINDOW_GROW
175 static int ple_gap
= KVM_VMX_DEFAULT_PLE_GAP
;
176 module_param(ple_gap
, int, S_IRUGO
);
178 static int ple_window
= KVM_VMX_DEFAULT_PLE_WINDOW
;
179 module_param(ple_window
, int, S_IRUGO
);
181 /* Default doubles per-vcpu window every exit. */
182 static int ple_window_grow
= KVM_VMX_DEFAULT_PLE_WINDOW_GROW
;
183 module_param(ple_window_grow
, int, S_IRUGO
);
185 /* Default resets per-vcpu window every exit to ple_window. */
186 static int ple_window_shrink
= KVM_VMX_DEFAULT_PLE_WINDOW_SHRINK
;
187 module_param(ple_window_shrink
, int, S_IRUGO
);
189 /* Default is to compute the maximum so we can never overflow. */
190 static int ple_window_actual_max
= KVM_VMX_DEFAULT_PLE_WINDOW_MAX
;
191 static int ple_window_max
= KVM_VMX_DEFAULT_PLE_WINDOW_MAX
;
192 module_param(ple_window_max
, int, S_IRUGO
);
194 extern const ulong vmx_return
;
196 #define NR_AUTOLOAD_MSRS 8
205 * Track a VMCS that may be loaded on a certain CPU. If it is (cpu!=-1), also
206 * remember whether it was VMLAUNCHed, and maintain a linked list of all VMCSs
207 * loaded on this CPU (so we can clear them if the CPU goes down).
211 struct vmcs
*shadow_vmcs
;
214 bool nmi_known_unmasked
;
215 unsigned long vmcs_host_cr3
; /* May not match real cr3 */
216 unsigned long vmcs_host_cr4
; /* May not match real cr4 */
217 /* Support for vnmi-less CPUs */
218 int soft_vnmi_blocked
;
220 s64 vnmi_blocked_time
;
221 unsigned long *msr_bitmap
;
222 struct list_head loaded_vmcss_on_cpu_link
;
225 struct shared_msr_entry
{
232 * struct vmcs12 describes the state that our guest hypervisor (L1) keeps for a
233 * single nested guest (L2), hence the name vmcs12. Any VMX implementation has
234 * a VMCS structure, and vmcs12 is our emulated VMX's VMCS. This structure is
235 * stored in guest memory specified by VMPTRLD, but is opaque to the guest,
236 * which must access it using VMREAD/VMWRITE/VMCLEAR instructions.
237 * More than one of these structures may exist, if L1 runs multiple L2 guests.
238 * nested_vmx_run() will use the data here to build the vmcs02: a VMCS for the
239 * underlying hardware which will be used to run L2.
240 * This structure is packed to ensure that its layout is identical across
241 * machines (necessary for live migration).
242 * If there are changes in this struct, VMCS12_REVISION must be changed.
244 typedef u64 natural_width
;
245 struct __packed vmcs12
{
246 /* According to the Intel spec, a VMCS region must start with the
247 * following two fields. Then follow implementation-specific data.
252 u32 launch_state
; /* set to 0 by VMCLEAR, to 1 by VMLAUNCH */
253 u32 padding
[7]; /* room for future expansion */
258 u64 vm_exit_msr_store_addr
;
259 u64 vm_exit_msr_load_addr
;
260 u64 vm_entry_msr_load_addr
;
262 u64 virtual_apic_page_addr
;
263 u64 apic_access_addr
;
264 u64 posted_intr_desc_addr
;
265 u64 vm_function_control
;
267 u64 eoi_exit_bitmap0
;
268 u64 eoi_exit_bitmap1
;
269 u64 eoi_exit_bitmap2
;
270 u64 eoi_exit_bitmap3
;
271 u64 eptp_list_address
;
273 u64 guest_physical_address
;
274 u64 vmcs_link_pointer
;
276 u64 guest_ia32_debugctl
;
279 u64 guest_ia32_perf_global_ctrl
;
287 u64 host_ia32_perf_global_ctrl
;
288 u64 padding64
[8]; /* room for future expansion */
290 * To allow migration of L1 (complete with its L2 guests) between
291 * machines of different natural widths (32 or 64 bit), we cannot have
292 * unsigned long fields with no explict size. We use u64 (aliased
293 * natural_width) instead. Luckily, x86 is little-endian.
295 natural_width cr0_guest_host_mask
;
296 natural_width cr4_guest_host_mask
;
297 natural_width cr0_read_shadow
;
298 natural_width cr4_read_shadow
;
299 natural_width cr3_target_value0
;
300 natural_width cr3_target_value1
;
301 natural_width cr3_target_value2
;
302 natural_width cr3_target_value3
;
303 natural_width exit_qualification
;
304 natural_width guest_linear_address
;
305 natural_width guest_cr0
;
306 natural_width guest_cr3
;
307 natural_width guest_cr4
;
308 natural_width guest_es_base
;
309 natural_width guest_cs_base
;
310 natural_width guest_ss_base
;
311 natural_width guest_ds_base
;
312 natural_width guest_fs_base
;
313 natural_width guest_gs_base
;
314 natural_width guest_ldtr_base
;
315 natural_width guest_tr_base
;
316 natural_width guest_gdtr_base
;
317 natural_width guest_idtr_base
;
318 natural_width guest_dr7
;
319 natural_width guest_rsp
;
320 natural_width guest_rip
;
321 natural_width guest_rflags
;
322 natural_width guest_pending_dbg_exceptions
;
323 natural_width guest_sysenter_esp
;
324 natural_width guest_sysenter_eip
;
325 natural_width host_cr0
;
326 natural_width host_cr3
;
327 natural_width host_cr4
;
328 natural_width host_fs_base
;
329 natural_width host_gs_base
;
330 natural_width host_tr_base
;
331 natural_width host_gdtr_base
;
332 natural_width host_idtr_base
;
333 natural_width host_ia32_sysenter_esp
;
334 natural_width host_ia32_sysenter_eip
;
335 natural_width host_rsp
;
336 natural_width host_rip
;
337 natural_width paddingl
[8]; /* room for future expansion */
338 u32 pin_based_vm_exec_control
;
339 u32 cpu_based_vm_exec_control
;
340 u32 exception_bitmap
;
341 u32 page_fault_error_code_mask
;
342 u32 page_fault_error_code_match
;
343 u32 cr3_target_count
;
344 u32 vm_exit_controls
;
345 u32 vm_exit_msr_store_count
;
346 u32 vm_exit_msr_load_count
;
347 u32 vm_entry_controls
;
348 u32 vm_entry_msr_load_count
;
349 u32 vm_entry_intr_info_field
;
350 u32 vm_entry_exception_error_code
;
351 u32 vm_entry_instruction_len
;
353 u32 secondary_vm_exec_control
;
354 u32 vm_instruction_error
;
356 u32 vm_exit_intr_info
;
357 u32 vm_exit_intr_error_code
;
358 u32 idt_vectoring_info_field
;
359 u32 idt_vectoring_error_code
;
360 u32 vm_exit_instruction_len
;
361 u32 vmx_instruction_info
;
368 u32 guest_ldtr_limit
;
370 u32 guest_gdtr_limit
;
371 u32 guest_idtr_limit
;
372 u32 guest_es_ar_bytes
;
373 u32 guest_cs_ar_bytes
;
374 u32 guest_ss_ar_bytes
;
375 u32 guest_ds_ar_bytes
;
376 u32 guest_fs_ar_bytes
;
377 u32 guest_gs_ar_bytes
;
378 u32 guest_ldtr_ar_bytes
;
379 u32 guest_tr_ar_bytes
;
380 u32 guest_interruptibility_info
;
381 u32 guest_activity_state
;
382 u32 guest_sysenter_cs
;
383 u32 host_ia32_sysenter_cs
;
384 u32 vmx_preemption_timer_value
;
385 u32 padding32
[7]; /* room for future expansion */
386 u16 virtual_processor_id
;
388 u16 guest_es_selector
;
389 u16 guest_cs_selector
;
390 u16 guest_ss_selector
;
391 u16 guest_ds_selector
;
392 u16 guest_fs_selector
;
393 u16 guest_gs_selector
;
394 u16 guest_ldtr_selector
;
395 u16 guest_tr_selector
;
396 u16 guest_intr_status
;
398 u16 host_es_selector
;
399 u16 host_cs_selector
;
400 u16 host_ss_selector
;
401 u16 host_ds_selector
;
402 u16 host_fs_selector
;
403 u16 host_gs_selector
;
404 u16 host_tr_selector
;
408 * VMCS12_REVISION is an arbitrary id that should be changed if the content or
409 * layout of struct vmcs12 is changed. MSR_IA32_VMX_BASIC returns this id, and
410 * VMPTRLD verifies that the VMCS region that L1 is loading contains this id.
412 #define VMCS12_REVISION 0x11e57ed0
415 * VMCS12_SIZE is the number of bytes L1 should allocate for the VMXON region
416 * and any VMCS region. Although only sizeof(struct vmcs12) are used by the
417 * current implementation, 4K are reserved to avoid future complications.
419 #define VMCS12_SIZE 0x1000
422 * VMCS12_MAX_FIELD_INDEX is the highest index value used in any
423 * supported VMCS12 field encoding.
425 #define VMCS12_MAX_FIELD_INDEX 0x17
428 * The nested_vmx structure is part of vcpu_vmx, and holds information we need
429 * for correct emulation of VMX (i.e., nested VMX) on this vcpu.
432 /* Has the level1 guest done vmxon? */
437 /* The guest-physical address of the current VMCS L1 keeps for L2 */
440 * Cache of the guest's VMCS, existing outside of guest memory.
441 * Loaded from guest memory during VMPTRLD. Flushed to guest
442 * memory during VMCLEAR and VMPTRLD.
444 struct vmcs12
*cached_vmcs12
;
446 * Indicates if the shadow vmcs must be updated with the
447 * data hold by vmcs12
449 bool sync_shadow_vmcs
;
452 bool change_vmcs01_virtual_x2apic_mode
;
453 /* L2 must run next, and mustn't decide to exit to L1. */
454 bool nested_run_pending
;
456 struct loaded_vmcs vmcs02
;
459 * Guest pages referred to in the vmcs02 with host-physical
460 * pointers, so we must keep them pinned while L2 runs.
462 struct page
*apic_access_page
;
463 struct page
*virtual_apic_page
;
464 struct page
*pi_desc_page
;
465 struct pi_desc
*pi_desc
;
469 struct hrtimer preemption_timer
;
470 bool preemption_timer_expired
;
472 /* to migrate it to L2 if VM_ENTRY_LOAD_DEBUG_CONTROLS is off */
479 * We only store the "true" versions of the VMX capability MSRs. We
480 * generate the "non-true" versions by setting the must-be-1 bits
481 * according to the SDM.
483 u32 nested_vmx_procbased_ctls_low
;
484 u32 nested_vmx_procbased_ctls_high
;
485 u32 nested_vmx_secondary_ctls_low
;
486 u32 nested_vmx_secondary_ctls_high
;
487 u32 nested_vmx_pinbased_ctls_low
;
488 u32 nested_vmx_pinbased_ctls_high
;
489 u32 nested_vmx_exit_ctls_low
;
490 u32 nested_vmx_exit_ctls_high
;
491 u32 nested_vmx_entry_ctls_low
;
492 u32 nested_vmx_entry_ctls_high
;
493 u32 nested_vmx_misc_low
;
494 u32 nested_vmx_misc_high
;
495 u32 nested_vmx_ept_caps
;
496 u32 nested_vmx_vpid_caps
;
497 u64 nested_vmx_basic
;
498 u64 nested_vmx_cr0_fixed0
;
499 u64 nested_vmx_cr0_fixed1
;
500 u64 nested_vmx_cr4_fixed0
;
501 u64 nested_vmx_cr4_fixed1
;
502 u64 nested_vmx_vmcs_enum
;
503 u64 nested_vmx_vmfunc_controls
;
505 /* SMM related state */
507 /* in VMX operation on SMM entry? */
509 /* in guest mode on SMM entry? */
514 #define POSTED_INTR_ON 0
515 #define POSTED_INTR_SN 1
517 /* Posted-Interrupt Descriptor */
519 u32 pir
[8]; /* Posted interrupt requested */
522 /* bit 256 - Outstanding Notification */
524 /* bit 257 - Suppress Notification */
526 /* bit 271:258 - Reserved */
528 /* bit 279:272 - Notification Vector */
530 /* bit 287:280 - Reserved */
532 /* bit 319:288 - Notification Destination */
540 static bool pi_test_and_set_on(struct pi_desc
*pi_desc
)
542 return test_and_set_bit(POSTED_INTR_ON
,
543 (unsigned long *)&pi_desc
->control
);
546 static bool pi_test_and_clear_on(struct pi_desc
*pi_desc
)
548 return test_and_clear_bit(POSTED_INTR_ON
,
549 (unsigned long *)&pi_desc
->control
);
552 static int pi_test_and_set_pir(int vector
, struct pi_desc
*pi_desc
)
554 return test_and_set_bit(vector
, (unsigned long *)pi_desc
->pir
);
557 static inline void pi_clear_sn(struct pi_desc
*pi_desc
)
559 return clear_bit(POSTED_INTR_SN
,
560 (unsigned long *)&pi_desc
->control
);
563 static inline void pi_set_sn(struct pi_desc
*pi_desc
)
565 return set_bit(POSTED_INTR_SN
,
566 (unsigned long *)&pi_desc
->control
);
569 static inline void pi_clear_on(struct pi_desc
*pi_desc
)
571 clear_bit(POSTED_INTR_ON
,
572 (unsigned long *)&pi_desc
->control
);
575 static inline int pi_test_on(struct pi_desc
*pi_desc
)
577 return test_bit(POSTED_INTR_ON
,
578 (unsigned long *)&pi_desc
->control
);
581 static inline int pi_test_sn(struct pi_desc
*pi_desc
)
583 return test_bit(POSTED_INTR_SN
,
584 (unsigned long *)&pi_desc
->control
);
588 struct kvm_vcpu vcpu
;
589 unsigned long host_rsp
;
593 u32 idt_vectoring_info
;
595 struct shared_msr_entry
*guest_msrs
;
598 unsigned long host_idt_base
;
600 u64 msr_host_kernel_gs_base
;
601 u64 msr_guest_kernel_gs_base
;
604 u64 arch_capabilities
;
607 u32 vm_entry_controls_shadow
;
608 u32 vm_exit_controls_shadow
;
609 u32 secondary_exec_control
;
612 * loaded_vmcs points to the VMCS currently used in this vcpu. For a
613 * non-nested (L1) guest, it always points to vmcs01. For a nested
614 * guest (L2), it points to a different VMCS.
616 struct loaded_vmcs vmcs01
;
617 struct loaded_vmcs
*loaded_vmcs
;
618 bool __launched
; /* temporary, used in vmx_vcpu_run */
619 struct msr_autoload
{
621 struct vmx_msr_entry guest
[NR_AUTOLOAD_MSRS
];
622 struct vmx_msr_entry host
[NR_AUTOLOAD_MSRS
];
626 u16 fs_sel
, gs_sel
, ldt_sel
;
630 int gs_ldt_reload_needed
;
631 int fs_reload_needed
;
632 u64 msr_host_bndcfgs
;
637 struct kvm_segment segs
[8];
640 u32 bitmask
; /* 4 bits per segment (1 bit per field) */
641 struct kvm_save_segment
{
649 bool emulation_required
;
653 /* Posted interrupt descriptor */
654 struct pi_desc pi_desc
;
656 /* Support for a guest hypervisor (nested VMX) */
657 struct nested_vmx nested
;
659 /* Dynamic PLE window. */
661 bool ple_window_dirty
;
663 /* Support for PML */
664 #define PML_ENTITY_NUM 512
667 /* apic deadline value in host tsc */
670 u64 current_tsc_ratio
;
674 unsigned long host_debugctlmsr
;
677 * Only bits masked by msr_ia32_feature_control_valid_bits can be set in
678 * msr_ia32_feature_control. FEATURE_CONTROL_LOCKED is always included
679 * in msr_ia32_feature_control_valid_bits.
681 u64 msr_ia32_feature_control
;
682 u64 msr_ia32_feature_control_valid_bits
;
685 enum segment_cache_field
{
694 static inline struct vcpu_vmx
*to_vmx(struct kvm_vcpu
*vcpu
)
696 return container_of(vcpu
, struct vcpu_vmx
, vcpu
);
699 static struct pi_desc
*vcpu_to_pi_desc(struct kvm_vcpu
*vcpu
)
701 return &(to_vmx(vcpu
)->pi_desc
);
704 #define ROL16(val, n) ((u16)(((u16)(val) << (n)) | ((u16)(val) >> (16 - (n)))))
705 #define VMCS12_OFFSET(x) offsetof(struct vmcs12, x)
706 #define FIELD(number, name) [ROL16(number, 6)] = VMCS12_OFFSET(name)
707 #define FIELD64(number, name) \
708 FIELD(number, name), \
709 [ROL16(number##_HIGH, 6)] = VMCS12_OFFSET(name) + sizeof(u32)
712 static u16 shadow_read_only_fields
[] = {
713 #define SHADOW_FIELD_RO(x) x,
714 #include "vmx_shadow_fields.h"
716 static int max_shadow_read_only_fields
=
717 ARRAY_SIZE(shadow_read_only_fields
);
719 static u16 shadow_read_write_fields
[] = {
720 #define SHADOW_FIELD_RW(x) x,
721 #include "vmx_shadow_fields.h"
723 static int max_shadow_read_write_fields
=
724 ARRAY_SIZE(shadow_read_write_fields
);
726 static const unsigned short vmcs_field_to_offset_table
[] = {
727 FIELD(VIRTUAL_PROCESSOR_ID
, virtual_processor_id
),
728 FIELD(POSTED_INTR_NV
, posted_intr_nv
),
729 FIELD(GUEST_ES_SELECTOR
, guest_es_selector
),
730 FIELD(GUEST_CS_SELECTOR
, guest_cs_selector
),
731 FIELD(GUEST_SS_SELECTOR
, guest_ss_selector
),
732 FIELD(GUEST_DS_SELECTOR
, guest_ds_selector
),
733 FIELD(GUEST_FS_SELECTOR
, guest_fs_selector
),
734 FIELD(GUEST_GS_SELECTOR
, guest_gs_selector
),
735 FIELD(GUEST_LDTR_SELECTOR
, guest_ldtr_selector
),
736 FIELD(GUEST_TR_SELECTOR
, guest_tr_selector
),
737 FIELD(GUEST_INTR_STATUS
, guest_intr_status
),
738 FIELD(GUEST_PML_INDEX
, guest_pml_index
),
739 FIELD(HOST_ES_SELECTOR
, host_es_selector
),
740 FIELD(HOST_CS_SELECTOR
, host_cs_selector
),
741 FIELD(HOST_SS_SELECTOR
, host_ss_selector
),
742 FIELD(HOST_DS_SELECTOR
, host_ds_selector
),
743 FIELD(HOST_FS_SELECTOR
, host_fs_selector
),
744 FIELD(HOST_GS_SELECTOR
, host_gs_selector
),
745 FIELD(HOST_TR_SELECTOR
, host_tr_selector
),
746 FIELD64(IO_BITMAP_A
, io_bitmap_a
),
747 FIELD64(IO_BITMAP_B
, io_bitmap_b
),
748 FIELD64(MSR_BITMAP
, msr_bitmap
),
749 FIELD64(VM_EXIT_MSR_STORE_ADDR
, vm_exit_msr_store_addr
),
750 FIELD64(VM_EXIT_MSR_LOAD_ADDR
, vm_exit_msr_load_addr
),
751 FIELD64(VM_ENTRY_MSR_LOAD_ADDR
, vm_entry_msr_load_addr
),
752 FIELD64(TSC_OFFSET
, tsc_offset
),
753 FIELD64(VIRTUAL_APIC_PAGE_ADDR
, virtual_apic_page_addr
),
754 FIELD64(APIC_ACCESS_ADDR
, apic_access_addr
),
755 FIELD64(POSTED_INTR_DESC_ADDR
, posted_intr_desc_addr
),
756 FIELD64(VM_FUNCTION_CONTROL
, vm_function_control
),
757 FIELD64(EPT_POINTER
, ept_pointer
),
758 FIELD64(EOI_EXIT_BITMAP0
, eoi_exit_bitmap0
),
759 FIELD64(EOI_EXIT_BITMAP1
, eoi_exit_bitmap1
),
760 FIELD64(EOI_EXIT_BITMAP2
, eoi_exit_bitmap2
),
761 FIELD64(EOI_EXIT_BITMAP3
, eoi_exit_bitmap3
),
762 FIELD64(EPTP_LIST_ADDRESS
, eptp_list_address
),
763 FIELD64(XSS_EXIT_BITMAP
, xss_exit_bitmap
),
764 FIELD64(GUEST_PHYSICAL_ADDRESS
, guest_physical_address
),
765 FIELD64(VMCS_LINK_POINTER
, vmcs_link_pointer
),
766 FIELD64(PML_ADDRESS
, pml_address
),
767 FIELD64(GUEST_IA32_DEBUGCTL
, guest_ia32_debugctl
),
768 FIELD64(GUEST_IA32_PAT
, guest_ia32_pat
),
769 FIELD64(GUEST_IA32_EFER
, guest_ia32_efer
),
770 FIELD64(GUEST_IA32_PERF_GLOBAL_CTRL
, guest_ia32_perf_global_ctrl
),
771 FIELD64(GUEST_PDPTR0
, guest_pdptr0
),
772 FIELD64(GUEST_PDPTR1
, guest_pdptr1
),
773 FIELD64(GUEST_PDPTR2
, guest_pdptr2
),
774 FIELD64(GUEST_PDPTR3
, guest_pdptr3
),
775 FIELD64(GUEST_BNDCFGS
, guest_bndcfgs
),
776 FIELD64(HOST_IA32_PAT
, host_ia32_pat
),
777 FIELD64(HOST_IA32_EFER
, host_ia32_efer
),
778 FIELD64(HOST_IA32_PERF_GLOBAL_CTRL
, host_ia32_perf_global_ctrl
),
779 FIELD(PIN_BASED_VM_EXEC_CONTROL
, pin_based_vm_exec_control
),
780 FIELD(CPU_BASED_VM_EXEC_CONTROL
, cpu_based_vm_exec_control
),
781 FIELD(EXCEPTION_BITMAP
, exception_bitmap
),
782 FIELD(PAGE_FAULT_ERROR_CODE_MASK
, page_fault_error_code_mask
),
783 FIELD(PAGE_FAULT_ERROR_CODE_MATCH
, page_fault_error_code_match
),
784 FIELD(CR3_TARGET_COUNT
, cr3_target_count
),
785 FIELD(VM_EXIT_CONTROLS
, vm_exit_controls
),
786 FIELD(VM_EXIT_MSR_STORE_COUNT
, vm_exit_msr_store_count
),
787 FIELD(VM_EXIT_MSR_LOAD_COUNT
, vm_exit_msr_load_count
),
788 FIELD(VM_ENTRY_CONTROLS
, vm_entry_controls
),
789 FIELD(VM_ENTRY_MSR_LOAD_COUNT
, vm_entry_msr_load_count
),
790 FIELD(VM_ENTRY_INTR_INFO_FIELD
, vm_entry_intr_info_field
),
791 FIELD(VM_ENTRY_EXCEPTION_ERROR_CODE
, vm_entry_exception_error_code
),
792 FIELD(VM_ENTRY_INSTRUCTION_LEN
, vm_entry_instruction_len
),
793 FIELD(TPR_THRESHOLD
, tpr_threshold
),
794 FIELD(SECONDARY_VM_EXEC_CONTROL
, secondary_vm_exec_control
),
795 FIELD(VM_INSTRUCTION_ERROR
, vm_instruction_error
),
796 FIELD(VM_EXIT_REASON
, vm_exit_reason
),
797 FIELD(VM_EXIT_INTR_INFO
, vm_exit_intr_info
),
798 FIELD(VM_EXIT_INTR_ERROR_CODE
, vm_exit_intr_error_code
),
799 FIELD(IDT_VECTORING_INFO_FIELD
, idt_vectoring_info_field
),
800 FIELD(IDT_VECTORING_ERROR_CODE
, idt_vectoring_error_code
),
801 FIELD(VM_EXIT_INSTRUCTION_LEN
, vm_exit_instruction_len
),
802 FIELD(VMX_INSTRUCTION_INFO
, vmx_instruction_info
),
803 FIELD(GUEST_ES_LIMIT
, guest_es_limit
),
804 FIELD(GUEST_CS_LIMIT
, guest_cs_limit
),
805 FIELD(GUEST_SS_LIMIT
, guest_ss_limit
),
806 FIELD(GUEST_DS_LIMIT
, guest_ds_limit
),
807 FIELD(GUEST_FS_LIMIT
, guest_fs_limit
),
808 FIELD(GUEST_GS_LIMIT
, guest_gs_limit
),
809 FIELD(GUEST_LDTR_LIMIT
, guest_ldtr_limit
),
810 FIELD(GUEST_TR_LIMIT
, guest_tr_limit
),
811 FIELD(GUEST_GDTR_LIMIT
, guest_gdtr_limit
),
812 FIELD(GUEST_IDTR_LIMIT
, guest_idtr_limit
),
813 FIELD(GUEST_ES_AR_BYTES
, guest_es_ar_bytes
),
814 FIELD(GUEST_CS_AR_BYTES
, guest_cs_ar_bytes
),
815 FIELD(GUEST_SS_AR_BYTES
, guest_ss_ar_bytes
),
816 FIELD(GUEST_DS_AR_BYTES
, guest_ds_ar_bytes
),
817 FIELD(GUEST_FS_AR_BYTES
, guest_fs_ar_bytes
),
818 FIELD(GUEST_GS_AR_BYTES
, guest_gs_ar_bytes
),
819 FIELD(GUEST_LDTR_AR_BYTES
, guest_ldtr_ar_bytes
),
820 FIELD(GUEST_TR_AR_BYTES
, guest_tr_ar_bytes
),
821 FIELD(GUEST_INTERRUPTIBILITY_INFO
, guest_interruptibility_info
),
822 FIELD(GUEST_ACTIVITY_STATE
, guest_activity_state
),
823 FIELD(GUEST_SYSENTER_CS
, guest_sysenter_cs
),
824 FIELD(HOST_IA32_SYSENTER_CS
, host_ia32_sysenter_cs
),
825 FIELD(VMX_PREEMPTION_TIMER_VALUE
, vmx_preemption_timer_value
),
826 FIELD(CR0_GUEST_HOST_MASK
, cr0_guest_host_mask
),
827 FIELD(CR4_GUEST_HOST_MASK
, cr4_guest_host_mask
),
828 FIELD(CR0_READ_SHADOW
, cr0_read_shadow
),
829 FIELD(CR4_READ_SHADOW
, cr4_read_shadow
),
830 FIELD(CR3_TARGET_VALUE0
, cr3_target_value0
),
831 FIELD(CR3_TARGET_VALUE1
, cr3_target_value1
),
832 FIELD(CR3_TARGET_VALUE2
, cr3_target_value2
),
833 FIELD(CR3_TARGET_VALUE3
, cr3_target_value3
),
834 FIELD(EXIT_QUALIFICATION
, exit_qualification
),
835 FIELD(GUEST_LINEAR_ADDRESS
, guest_linear_address
),
836 FIELD(GUEST_CR0
, guest_cr0
),
837 FIELD(GUEST_CR3
, guest_cr3
),
838 FIELD(GUEST_CR4
, guest_cr4
),
839 FIELD(GUEST_ES_BASE
, guest_es_base
),
840 FIELD(GUEST_CS_BASE
, guest_cs_base
),
841 FIELD(GUEST_SS_BASE
, guest_ss_base
),
842 FIELD(GUEST_DS_BASE
, guest_ds_base
),
843 FIELD(GUEST_FS_BASE
, guest_fs_base
),
844 FIELD(GUEST_GS_BASE
, guest_gs_base
),
845 FIELD(GUEST_LDTR_BASE
, guest_ldtr_base
),
846 FIELD(GUEST_TR_BASE
, guest_tr_base
),
847 FIELD(GUEST_GDTR_BASE
, guest_gdtr_base
),
848 FIELD(GUEST_IDTR_BASE
, guest_idtr_base
),
849 FIELD(GUEST_DR7
, guest_dr7
),
850 FIELD(GUEST_RSP
, guest_rsp
),
851 FIELD(GUEST_RIP
, guest_rip
),
852 FIELD(GUEST_RFLAGS
, guest_rflags
),
853 FIELD(GUEST_PENDING_DBG_EXCEPTIONS
, guest_pending_dbg_exceptions
),
854 FIELD(GUEST_SYSENTER_ESP
, guest_sysenter_esp
),
855 FIELD(GUEST_SYSENTER_EIP
, guest_sysenter_eip
),
856 FIELD(HOST_CR0
, host_cr0
),
857 FIELD(HOST_CR3
, host_cr3
),
858 FIELD(HOST_CR4
, host_cr4
),
859 FIELD(HOST_FS_BASE
, host_fs_base
),
860 FIELD(HOST_GS_BASE
, host_gs_base
),
861 FIELD(HOST_TR_BASE
, host_tr_base
),
862 FIELD(HOST_GDTR_BASE
, host_gdtr_base
),
863 FIELD(HOST_IDTR_BASE
, host_idtr_base
),
864 FIELD(HOST_IA32_SYSENTER_ESP
, host_ia32_sysenter_esp
),
865 FIELD(HOST_IA32_SYSENTER_EIP
, host_ia32_sysenter_eip
),
866 FIELD(HOST_RSP
, host_rsp
),
867 FIELD(HOST_RIP
, host_rip
),
870 static inline short vmcs_field_to_offset(unsigned long field
)
872 const size_t size
= ARRAY_SIZE(vmcs_field_to_offset_table
);
873 unsigned short offset
;
879 index
= ROL16(field
, 6);
883 index
= array_index_nospec(index
, size
);
884 offset
= vmcs_field_to_offset_table
[index
];
890 static inline struct vmcs12
*get_vmcs12(struct kvm_vcpu
*vcpu
)
892 return to_vmx(vcpu
)->nested
.cached_vmcs12
;
895 static bool nested_ept_ad_enabled(struct kvm_vcpu
*vcpu
);
896 static unsigned long nested_ept_get_cr3(struct kvm_vcpu
*vcpu
);
897 static u64
construct_eptp(struct kvm_vcpu
*vcpu
, unsigned long root_hpa
);
898 static bool vmx_xsaves_supported(void);
899 static void vmx_set_segment(struct kvm_vcpu
*vcpu
,
900 struct kvm_segment
*var
, int seg
);
901 static void vmx_get_segment(struct kvm_vcpu
*vcpu
,
902 struct kvm_segment
*var
, int seg
);
903 static bool guest_state_valid(struct kvm_vcpu
*vcpu
);
904 static u32
vmx_segment_access_rights(struct kvm_segment
*var
);
905 static void copy_shadow_to_vmcs12(struct vcpu_vmx
*vmx
);
906 static bool vmx_get_nmi_mask(struct kvm_vcpu
*vcpu
);
907 static void vmx_set_nmi_mask(struct kvm_vcpu
*vcpu
, bool masked
);
908 static bool nested_vmx_is_page_fault_vmexit(struct vmcs12
*vmcs12
,
910 static void vmx_update_msr_bitmap(struct kvm_vcpu
*vcpu
);
911 static void __always_inline
vmx_disable_intercept_for_msr(unsigned long *msr_bitmap
,
914 static DEFINE_PER_CPU(struct vmcs
*, vmxarea
);
915 static DEFINE_PER_CPU(struct vmcs
*, current_vmcs
);
917 * We maintain a per-CPU linked-list of VMCS loaded on that CPU. This is needed
918 * when a CPU is brought down, and we need to VMCLEAR all VMCSs loaded on it.
920 static DEFINE_PER_CPU(struct list_head
, loaded_vmcss_on_cpu
);
923 * We maintian a per-CPU linked-list of vCPU, so in wakeup_handler() we
924 * can find which vCPU should be waken up.
926 static DEFINE_PER_CPU(struct list_head
, blocked_vcpu_on_cpu
);
927 static DEFINE_PER_CPU(spinlock_t
, blocked_vcpu_on_cpu_lock
);
935 static unsigned long *vmx_bitmap
[VMX_BITMAP_NR
];
937 #define vmx_vmread_bitmap (vmx_bitmap[VMX_VMREAD_BITMAP])
938 #define vmx_vmwrite_bitmap (vmx_bitmap[VMX_VMWRITE_BITMAP])
940 static bool cpu_has_load_ia32_efer
;
941 static bool cpu_has_load_perf_global_ctrl
;
943 static DECLARE_BITMAP(vmx_vpid_bitmap
, VMX_NR_VPIDS
);
944 static DEFINE_SPINLOCK(vmx_vpid_lock
);
946 static struct vmcs_config
{
951 u32 pin_based_exec_ctrl
;
952 u32 cpu_based_exec_ctrl
;
953 u32 cpu_based_2nd_exec_ctrl
;
958 static struct vmx_capability
{
963 #define VMX_SEGMENT_FIELD(seg) \
964 [VCPU_SREG_##seg] = { \
965 .selector = GUEST_##seg##_SELECTOR, \
966 .base = GUEST_##seg##_BASE, \
967 .limit = GUEST_##seg##_LIMIT, \
968 .ar_bytes = GUEST_##seg##_AR_BYTES, \
971 static const struct kvm_vmx_segment_field
{
976 } kvm_vmx_segment_fields
[] = {
977 VMX_SEGMENT_FIELD(CS
),
978 VMX_SEGMENT_FIELD(DS
),
979 VMX_SEGMENT_FIELD(ES
),
980 VMX_SEGMENT_FIELD(FS
),
981 VMX_SEGMENT_FIELD(GS
),
982 VMX_SEGMENT_FIELD(SS
),
983 VMX_SEGMENT_FIELD(TR
),
984 VMX_SEGMENT_FIELD(LDTR
),
987 static u64 host_efer
;
989 static void ept_save_pdptrs(struct kvm_vcpu
*vcpu
);
992 * Keep MSR_STAR at the end, as setup_msrs() will try to optimize it
993 * away by decrementing the array size.
995 static const u32 vmx_msr_index
[] = {
997 MSR_SYSCALL_MASK
, MSR_LSTAR
, MSR_CSTAR
,
999 MSR_EFER
, MSR_TSC_AUX
, MSR_STAR
,
1002 static inline bool is_exception_n(u32 intr_info
, u8 vector
)
1004 return (intr_info
& (INTR_INFO_INTR_TYPE_MASK
| INTR_INFO_VECTOR_MASK
|
1005 INTR_INFO_VALID_MASK
)) ==
1006 (INTR_TYPE_HARD_EXCEPTION
| vector
| INTR_INFO_VALID_MASK
);
1009 static inline bool is_debug(u32 intr_info
)
1011 return is_exception_n(intr_info
, DB_VECTOR
);
1014 static inline bool is_breakpoint(u32 intr_info
)
1016 return is_exception_n(intr_info
, BP_VECTOR
);
1019 static inline bool is_page_fault(u32 intr_info
)
1021 return is_exception_n(intr_info
, PF_VECTOR
);
1024 static inline bool is_no_device(u32 intr_info
)
1026 return is_exception_n(intr_info
, NM_VECTOR
);
1029 static inline bool is_invalid_opcode(u32 intr_info
)
1031 return is_exception_n(intr_info
, UD_VECTOR
);
1034 static inline bool is_external_interrupt(u32 intr_info
)
1036 return (intr_info
& (INTR_INFO_INTR_TYPE_MASK
| INTR_INFO_VALID_MASK
))
1037 == (INTR_TYPE_EXT_INTR
| INTR_INFO_VALID_MASK
);
1040 static inline bool is_machine_check(u32 intr_info
)
1042 return (intr_info
& (INTR_INFO_INTR_TYPE_MASK
| INTR_INFO_VECTOR_MASK
|
1043 INTR_INFO_VALID_MASK
)) ==
1044 (INTR_TYPE_HARD_EXCEPTION
| MC_VECTOR
| INTR_INFO_VALID_MASK
);
1047 static inline bool cpu_has_vmx_msr_bitmap(void)
1049 return vmcs_config
.cpu_based_exec_ctrl
& CPU_BASED_USE_MSR_BITMAPS
;
1052 static inline bool cpu_has_vmx_tpr_shadow(void)
1054 return vmcs_config
.cpu_based_exec_ctrl
& CPU_BASED_TPR_SHADOW
;
1057 static inline bool cpu_need_tpr_shadow(struct kvm_vcpu
*vcpu
)
1059 return cpu_has_vmx_tpr_shadow() && lapic_in_kernel(vcpu
);
1062 static inline bool cpu_has_secondary_exec_ctrls(void)
1064 return vmcs_config
.cpu_based_exec_ctrl
&
1065 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS
;
1068 static inline bool cpu_has_vmx_virtualize_apic_accesses(void)
1070 return vmcs_config
.cpu_based_2nd_exec_ctrl
&
1071 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES
;
1074 static inline bool cpu_has_vmx_virtualize_x2apic_mode(void)
1076 return vmcs_config
.cpu_based_2nd_exec_ctrl
&
1077 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE
;
1080 static inline bool cpu_has_vmx_apic_register_virt(void)
1082 return vmcs_config
.cpu_based_2nd_exec_ctrl
&
1083 SECONDARY_EXEC_APIC_REGISTER_VIRT
;
1086 static inline bool cpu_has_vmx_virtual_intr_delivery(void)
1088 return vmcs_config
.cpu_based_2nd_exec_ctrl
&
1089 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY
;
1093 * Comment's format: document - errata name - stepping - processor name.
1095 * https://www.virtualbox.org/svn/vbox/trunk/src/VBox/VMM/VMMR0/HMR0.cpp
1097 static u32 vmx_preemption_cpu_tfms
[] = {
1098 /* 323344.pdf - BA86 - D0 - Xeon 7500 Series */
1100 /* 323056.pdf - AAX65 - C2 - Xeon L3406 */
1101 /* 322814.pdf - AAT59 - C2 - i7-600, i5-500, i5-400 and i3-300 Mobile */
1102 /* 322911.pdf - AAU65 - C2 - i5-600, i3-500 Desktop and Pentium G6950 */
1104 /* 322911.pdf - AAU65 - K0 - i5-600, i3-500 Desktop and Pentium G6950 */
1106 /* 322373.pdf - AAO95 - B1 - Xeon 3400 Series */
1107 /* 322166.pdf - AAN92 - B1 - i7-800 and i5-700 Desktop */
1109 * 320767.pdf - AAP86 - B1 -
1110 * i7-900 Mobile Extreme, i7-800 and i7-700 Mobile
1113 /* 321333.pdf - AAM126 - C0 - Xeon 3500 */
1115 /* 321333.pdf - AAM126 - C1 - Xeon 3500 */
1117 /* 320836.pdf - AAJ124 - C0 - i7-900 Desktop Extreme and i7-900 Desktop */
1119 /* 321333.pdf - AAM126 - D0 - Xeon 3500 */
1120 /* 321324.pdf - AAK139 - D0 - Xeon 5500 */
1121 /* 320836.pdf - AAJ124 - D0 - i7-900 Extreme and i7-900 Desktop */
1125 static inline bool cpu_has_broken_vmx_preemption_timer(void)
1127 u32 eax
= cpuid_eax(0x00000001), i
;
1129 /* Clear the reserved bits */
1130 eax
&= ~(0x3U
<< 14 | 0xfU
<< 28);
1131 for (i
= 0; i
< ARRAY_SIZE(vmx_preemption_cpu_tfms
); i
++)
1132 if (eax
== vmx_preemption_cpu_tfms
[i
])
1138 static inline bool cpu_has_vmx_preemption_timer(void)
1140 return vmcs_config
.pin_based_exec_ctrl
&
1141 PIN_BASED_VMX_PREEMPTION_TIMER
;
1144 static inline bool cpu_has_vmx_posted_intr(void)
1146 return IS_ENABLED(CONFIG_X86_LOCAL_APIC
) &&
1147 vmcs_config
.pin_based_exec_ctrl
& PIN_BASED_POSTED_INTR
;
1150 static inline bool cpu_has_vmx_apicv(void)
1152 return cpu_has_vmx_apic_register_virt() &&
1153 cpu_has_vmx_virtual_intr_delivery() &&
1154 cpu_has_vmx_posted_intr();
1157 static inline bool cpu_has_vmx_flexpriority(void)
1159 return cpu_has_vmx_tpr_shadow() &&
1160 cpu_has_vmx_virtualize_apic_accesses();
1163 static inline bool cpu_has_vmx_ept_execute_only(void)
1165 return vmx_capability
.ept
& VMX_EPT_EXECUTE_ONLY_BIT
;
1168 static inline bool cpu_has_vmx_ept_2m_page(void)
1170 return vmx_capability
.ept
& VMX_EPT_2MB_PAGE_BIT
;
1173 static inline bool cpu_has_vmx_ept_1g_page(void)
1175 return vmx_capability
.ept
& VMX_EPT_1GB_PAGE_BIT
;
1178 static inline bool cpu_has_vmx_ept_4levels(void)
1180 return vmx_capability
.ept
& VMX_EPT_PAGE_WALK_4_BIT
;
1183 static inline bool cpu_has_vmx_ept_mt_wb(void)
1185 return vmx_capability
.ept
& VMX_EPTP_WB_BIT
;
1188 static inline bool cpu_has_vmx_ept_5levels(void)
1190 return vmx_capability
.ept
& VMX_EPT_PAGE_WALK_5_BIT
;
1193 static inline bool cpu_has_vmx_ept_ad_bits(void)
1195 return vmx_capability
.ept
& VMX_EPT_AD_BIT
;
1198 static inline bool cpu_has_vmx_invept_context(void)
1200 return vmx_capability
.ept
& VMX_EPT_EXTENT_CONTEXT_BIT
;
1203 static inline bool cpu_has_vmx_invept_global(void)
1205 return vmx_capability
.ept
& VMX_EPT_EXTENT_GLOBAL_BIT
;
1208 static inline bool cpu_has_vmx_invvpid_single(void)
1210 return vmx_capability
.vpid
& VMX_VPID_EXTENT_SINGLE_CONTEXT_BIT
;
1213 static inline bool cpu_has_vmx_invvpid_global(void)
1215 return vmx_capability
.vpid
& VMX_VPID_EXTENT_GLOBAL_CONTEXT_BIT
;
1218 static inline bool cpu_has_vmx_invvpid(void)
1220 return vmx_capability
.vpid
& VMX_VPID_INVVPID_BIT
;
1223 static inline bool cpu_has_vmx_ept(void)
1225 return vmcs_config
.cpu_based_2nd_exec_ctrl
&
1226 SECONDARY_EXEC_ENABLE_EPT
;
1229 static inline bool cpu_has_vmx_unrestricted_guest(void)
1231 return vmcs_config
.cpu_based_2nd_exec_ctrl
&
1232 SECONDARY_EXEC_UNRESTRICTED_GUEST
;
1235 static inline bool cpu_has_vmx_ple(void)
1237 return vmcs_config
.cpu_based_2nd_exec_ctrl
&
1238 SECONDARY_EXEC_PAUSE_LOOP_EXITING
;
1241 static inline bool cpu_has_vmx_basic_inout(void)
1243 return (((u64
)vmcs_config
.basic_cap
<< 32) & VMX_BASIC_INOUT
);
1246 static inline bool cpu_need_virtualize_apic_accesses(struct kvm_vcpu
*vcpu
)
1248 return flexpriority_enabled
&& lapic_in_kernel(vcpu
);
1251 static inline bool cpu_has_vmx_vpid(void)
1253 return vmcs_config
.cpu_based_2nd_exec_ctrl
&
1254 SECONDARY_EXEC_ENABLE_VPID
;
1257 static inline bool cpu_has_vmx_rdtscp(void)
1259 return vmcs_config
.cpu_based_2nd_exec_ctrl
&
1260 SECONDARY_EXEC_RDTSCP
;
1263 static inline bool cpu_has_vmx_invpcid(void)
1265 return vmcs_config
.cpu_based_2nd_exec_ctrl
&
1266 SECONDARY_EXEC_ENABLE_INVPCID
;
1269 static inline bool cpu_has_virtual_nmis(void)
1271 return vmcs_config
.pin_based_exec_ctrl
& PIN_BASED_VIRTUAL_NMIS
;
1274 static inline bool cpu_has_vmx_wbinvd_exit(void)
1276 return vmcs_config
.cpu_based_2nd_exec_ctrl
&
1277 SECONDARY_EXEC_WBINVD_EXITING
;
1280 static inline bool cpu_has_vmx_shadow_vmcs(void)
1283 rdmsrl(MSR_IA32_VMX_MISC
, vmx_msr
);
1284 /* check if the cpu supports writing r/o exit information fields */
1285 if (!(vmx_msr
& MSR_IA32_VMX_MISC_VMWRITE_SHADOW_RO_FIELDS
))
1288 return vmcs_config
.cpu_based_2nd_exec_ctrl
&
1289 SECONDARY_EXEC_SHADOW_VMCS
;
1292 static inline bool cpu_has_vmx_pml(void)
1294 return vmcs_config
.cpu_based_2nd_exec_ctrl
& SECONDARY_EXEC_ENABLE_PML
;
1297 static inline bool cpu_has_vmx_tsc_scaling(void)
1299 return vmcs_config
.cpu_based_2nd_exec_ctrl
&
1300 SECONDARY_EXEC_TSC_SCALING
;
1303 static inline bool cpu_has_vmx_vmfunc(void)
1305 return vmcs_config
.cpu_based_2nd_exec_ctrl
&
1306 SECONDARY_EXEC_ENABLE_VMFUNC
;
1309 static inline bool report_flexpriority(void)
1311 return flexpriority_enabled
;
1314 static inline unsigned nested_cpu_vmx_misc_cr3_count(struct kvm_vcpu
*vcpu
)
1316 return vmx_misc_cr3_count(to_vmx(vcpu
)->nested
.nested_vmx_misc_low
);
1319 static inline bool nested_cpu_has(struct vmcs12
*vmcs12
, u32 bit
)
1321 return vmcs12
->cpu_based_vm_exec_control
& bit
;
1324 static inline bool nested_cpu_has2(struct vmcs12
*vmcs12
, u32 bit
)
1326 return (vmcs12
->cpu_based_vm_exec_control
&
1327 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS
) &&
1328 (vmcs12
->secondary_vm_exec_control
& bit
);
1331 static inline bool nested_cpu_has_preemption_timer(struct vmcs12
*vmcs12
)
1333 return vmcs12
->pin_based_vm_exec_control
&
1334 PIN_BASED_VMX_PREEMPTION_TIMER
;
1337 static inline int nested_cpu_has_ept(struct vmcs12
*vmcs12
)
1339 return nested_cpu_has2(vmcs12
, SECONDARY_EXEC_ENABLE_EPT
);
1342 static inline bool nested_cpu_has_xsaves(struct vmcs12
*vmcs12
)
1344 return nested_cpu_has2(vmcs12
, SECONDARY_EXEC_XSAVES
);
1347 static inline bool nested_cpu_has_pml(struct vmcs12
*vmcs12
)
1349 return nested_cpu_has2(vmcs12
, SECONDARY_EXEC_ENABLE_PML
);
1352 static inline bool nested_cpu_has_virt_x2apic_mode(struct vmcs12
*vmcs12
)
1354 return nested_cpu_has2(vmcs12
, SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE
);
1357 static inline bool nested_cpu_has_vpid(struct vmcs12
*vmcs12
)
1359 return nested_cpu_has2(vmcs12
, SECONDARY_EXEC_ENABLE_VPID
);
1362 static inline bool nested_cpu_has_apic_reg_virt(struct vmcs12
*vmcs12
)
1364 return nested_cpu_has2(vmcs12
, SECONDARY_EXEC_APIC_REGISTER_VIRT
);
1367 static inline bool nested_cpu_has_vid(struct vmcs12
*vmcs12
)
1369 return nested_cpu_has2(vmcs12
, SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY
);
1372 static inline bool nested_cpu_has_posted_intr(struct vmcs12
*vmcs12
)
1374 return vmcs12
->pin_based_vm_exec_control
& PIN_BASED_POSTED_INTR
;
1377 static inline bool nested_cpu_has_vmfunc(struct vmcs12
*vmcs12
)
1379 return nested_cpu_has2(vmcs12
, SECONDARY_EXEC_ENABLE_VMFUNC
);
1382 static inline bool nested_cpu_has_eptp_switching(struct vmcs12
*vmcs12
)
1384 return nested_cpu_has_vmfunc(vmcs12
) &&
1385 (vmcs12
->vm_function_control
&
1386 VMX_VMFUNC_EPTP_SWITCHING
);
1389 static inline bool is_nmi(u32 intr_info
)
1391 return (intr_info
& (INTR_INFO_INTR_TYPE_MASK
| INTR_INFO_VALID_MASK
))
1392 == (INTR_TYPE_NMI_INTR
| INTR_INFO_VALID_MASK
);
1395 static void nested_vmx_vmexit(struct kvm_vcpu
*vcpu
, u32 exit_reason
,
1397 unsigned long exit_qualification
);
1398 static void nested_vmx_entry_failure(struct kvm_vcpu
*vcpu
,
1399 struct vmcs12
*vmcs12
,
1400 u32 reason
, unsigned long qualification
);
1402 static int __find_msr_index(struct vcpu_vmx
*vmx
, u32 msr
)
1406 for (i
= 0; i
< vmx
->nmsrs
; ++i
)
1407 if (vmx_msr_index
[vmx
->guest_msrs
[i
].index
] == msr
)
1412 static inline void __invvpid(int ext
, u16 vpid
, gva_t gva
)
1418 } operand
= { vpid
, 0, gva
};
1420 asm volatile (__ex(ASM_VMX_INVVPID
)
1421 /* CF==1 or ZF==1 --> rc = -1 */
1422 "; ja 1f ; ud2 ; 1:"
1423 : : "a"(&operand
), "c"(ext
) : "cc", "memory");
1426 static inline void __invept(int ext
, u64 eptp
, gpa_t gpa
)
1430 } operand
= {eptp
, gpa
};
1432 asm volatile (__ex(ASM_VMX_INVEPT
)
1433 /* CF==1 or ZF==1 --> rc = -1 */
1434 "; ja 1f ; ud2 ; 1:\n"
1435 : : "a" (&operand
), "c" (ext
) : "cc", "memory");
1438 static struct shared_msr_entry
*find_msr_entry(struct vcpu_vmx
*vmx
, u32 msr
)
1442 i
= __find_msr_index(vmx
, msr
);
1444 return &vmx
->guest_msrs
[i
];
1448 static void vmcs_clear(struct vmcs
*vmcs
)
1450 u64 phys_addr
= __pa(vmcs
);
1453 asm volatile (__ex(ASM_VMX_VMCLEAR_RAX
) "; setna %0"
1454 : "=qm"(error
) : "a"(&phys_addr
), "m"(phys_addr
)
1457 printk(KERN_ERR
"kvm: vmclear fail: %p/%llx\n",
1461 static inline void loaded_vmcs_init(struct loaded_vmcs
*loaded_vmcs
)
1463 vmcs_clear(loaded_vmcs
->vmcs
);
1464 if (loaded_vmcs
->shadow_vmcs
&& loaded_vmcs
->launched
)
1465 vmcs_clear(loaded_vmcs
->shadow_vmcs
);
1466 loaded_vmcs
->cpu
= -1;
1467 loaded_vmcs
->launched
= 0;
1470 static void vmcs_load(struct vmcs
*vmcs
)
1472 u64 phys_addr
= __pa(vmcs
);
1475 asm volatile (__ex(ASM_VMX_VMPTRLD_RAX
) "; setna %0"
1476 : "=qm"(error
) : "a"(&phys_addr
), "m"(phys_addr
)
1479 printk(KERN_ERR
"kvm: vmptrld %p/%llx failed\n",
1483 #ifdef CONFIG_KEXEC_CORE
1485 * This bitmap is used to indicate whether the vmclear
1486 * operation is enabled on all cpus. All disabled by
1489 static cpumask_t crash_vmclear_enabled_bitmap
= CPU_MASK_NONE
;
1491 static inline void crash_enable_local_vmclear(int cpu
)
1493 cpumask_set_cpu(cpu
, &crash_vmclear_enabled_bitmap
);
1496 static inline void crash_disable_local_vmclear(int cpu
)
1498 cpumask_clear_cpu(cpu
, &crash_vmclear_enabled_bitmap
);
1501 static inline int crash_local_vmclear_enabled(int cpu
)
1503 return cpumask_test_cpu(cpu
, &crash_vmclear_enabled_bitmap
);
1506 static void crash_vmclear_local_loaded_vmcss(void)
1508 int cpu
= raw_smp_processor_id();
1509 struct loaded_vmcs
*v
;
1511 if (!crash_local_vmclear_enabled(cpu
))
1514 list_for_each_entry(v
, &per_cpu(loaded_vmcss_on_cpu
, cpu
),
1515 loaded_vmcss_on_cpu_link
)
1516 vmcs_clear(v
->vmcs
);
1519 static inline void crash_enable_local_vmclear(int cpu
) { }
1520 static inline void crash_disable_local_vmclear(int cpu
) { }
1521 #endif /* CONFIG_KEXEC_CORE */
1523 static void __loaded_vmcs_clear(void *arg
)
1525 struct loaded_vmcs
*loaded_vmcs
= arg
;
1526 int cpu
= raw_smp_processor_id();
1528 if (loaded_vmcs
->cpu
!= cpu
)
1529 return; /* vcpu migration can race with cpu offline */
1530 if (per_cpu(current_vmcs
, cpu
) == loaded_vmcs
->vmcs
)
1531 per_cpu(current_vmcs
, cpu
) = NULL
;
1532 crash_disable_local_vmclear(cpu
);
1533 list_del(&loaded_vmcs
->loaded_vmcss_on_cpu_link
);
1536 * we should ensure updating loaded_vmcs->loaded_vmcss_on_cpu_link
1537 * is before setting loaded_vmcs->vcpu to -1 which is done in
1538 * loaded_vmcs_init. Otherwise, other cpu can see vcpu = -1 fist
1539 * then adds the vmcs into percpu list before it is deleted.
1543 loaded_vmcs_init(loaded_vmcs
);
1544 crash_enable_local_vmclear(cpu
);
1547 static void loaded_vmcs_clear(struct loaded_vmcs
*loaded_vmcs
)
1549 int cpu
= loaded_vmcs
->cpu
;
1552 smp_call_function_single(cpu
,
1553 __loaded_vmcs_clear
, loaded_vmcs
, 1);
1556 static inline void vpid_sync_vcpu_single(int vpid
)
1561 if (cpu_has_vmx_invvpid_single())
1562 __invvpid(VMX_VPID_EXTENT_SINGLE_CONTEXT
, vpid
, 0);
1565 static inline void vpid_sync_vcpu_global(void)
1567 if (cpu_has_vmx_invvpid_global())
1568 __invvpid(VMX_VPID_EXTENT_ALL_CONTEXT
, 0, 0);
1571 static inline void vpid_sync_context(int vpid
)
1573 if (cpu_has_vmx_invvpid_single())
1574 vpid_sync_vcpu_single(vpid
);
1576 vpid_sync_vcpu_global();
1579 static inline void ept_sync_global(void)
1581 __invept(VMX_EPT_EXTENT_GLOBAL
, 0, 0);
1584 static inline void ept_sync_context(u64 eptp
)
1586 if (cpu_has_vmx_invept_context())
1587 __invept(VMX_EPT_EXTENT_CONTEXT
, eptp
, 0);
1592 static __always_inline
void vmcs_check16(unsigned long field
)
1594 BUILD_BUG_ON_MSG(__builtin_constant_p(field
) && ((field
) & 0x6001) == 0x2000,
1595 "16-bit accessor invalid for 64-bit field");
1596 BUILD_BUG_ON_MSG(__builtin_constant_p(field
) && ((field
) & 0x6001) == 0x2001,
1597 "16-bit accessor invalid for 64-bit high field");
1598 BUILD_BUG_ON_MSG(__builtin_constant_p(field
) && ((field
) & 0x6000) == 0x4000,
1599 "16-bit accessor invalid for 32-bit high field");
1600 BUILD_BUG_ON_MSG(__builtin_constant_p(field
) && ((field
) & 0x6000) == 0x6000,
1601 "16-bit accessor invalid for natural width field");
1604 static __always_inline
void vmcs_check32(unsigned long field
)
1606 BUILD_BUG_ON_MSG(__builtin_constant_p(field
) && ((field
) & 0x6000) == 0,
1607 "32-bit accessor invalid for 16-bit field");
1608 BUILD_BUG_ON_MSG(__builtin_constant_p(field
) && ((field
) & 0x6000) == 0x6000,
1609 "32-bit accessor invalid for natural width field");
1612 static __always_inline
void vmcs_check64(unsigned long field
)
1614 BUILD_BUG_ON_MSG(__builtin_constant_p(field
) && ((field
) & 0x6000) == 0,
1615 "64-bit accessor invalid for 16-bit field");
1616 BUILD_BUG_ON_MSG(__builtin_constant_p(field
) && ((field
) & 0x6001) == 0x2001,
1617 "64-bit accessor invalid for 64-bit high field");
1618 BUILD_BUG_ON_MSG(__builtin_constant_p(field
) && ((field
) & 0x6000) == 0x4000,
1619 "64-bit accessor invalid for 32-bit field");
1620 BUILD_BUG_ON_MSG(__builtin_constant_p(field
) && ((field
) & 0x6000) == 0x6000,
1621 "64-bit accessor invalid for natural width field");
1624 static __always_inline
void vmcs_checkl(unsigned long field
)
1626 BUILD_BUG_ON_MSG(__builtin_constant_p(field
) && ((field
) & 0x6000) == 0,
1627 "Natural width accessor invalid for 16-bit field");
1628 BUILD_BUG_ON_MSG(__builtin_constant_p(field
) && ((field
) & 0x6001) == 0x2000,
1629 "Natural width accessor invalid for 64-bit field");
1630 BUILD_BUG_ON_MSG(__builtin_constant_p(field
) && ((field
) & 0x6001) == 0x2001,
1631 "Natural width accessor invalid for 64-bit high field");
1632 BUILD_BUG_ON_MSG(__builtin_constant_p(field
) && ((field
) & 0x6000) == 0x4000,
1633 "Natural width accessor invalid for 32-bit field");
1636 static __always_inline
unsigned long __vmcs_readl(unsigned long field
)
1638 unsigned long value
;
1640 asm volatile (__ex_clear(ASM_VMX_VMREAD_RDX_RAX
, "%0")
1641 : "=a"(value
) : "d"(field
) : "cc");
1645 static __always_inline u16
vmcs_read16(unsigned long field
)
1647 vmcs_check16(field
);
1648 return __vmcs_readl(field
);
1651 static __always_inline u32
vmcs_read32(unsigned long field
)
1653 vmcs_check32(field
);
1654 return __vmcs_readl(field
);
1657 static __always_inline u64
vmcs_read64(unsigned long field
)
1659 vmcs_check64(field
);
1660 #ifdef CONFIG_X86_64
1661 return __vmcs_readl(field
);
1663 return __vmcs_readl(field
) | ((u64
)__vmcs_readl(field
+1) << 32);
1667 static __always_inline
unsigned long vmcs_readl(unsigned long field
)
1670 return __vmcs_readl(field
);
1673 static noinline
void vmwrite_error(unsigned long field
, unsigned long value
)
1675 printk(KERN_ERR
"vmwrite error: reg %lx value %lx (err %d)\n",
1676 field
, value
, vmcs_read32(VM_INSTRUCTION_ERROR
));
1680 static __always_inline
void __vmcs_writel(unsigned long field
, unsigned long value
)
1684 asm volatile (__ex(ASM_VMX_VMWRITE_RAX_RDX
) "; setna %0"
1685 : "=q"(error
) : "a"(value
), "d"(field
) : "cc");
1686 if (unlikely(error
))
1687 vmwrite_error(field
, value
);
1690 static __always_inline
void vmcs_write16(unsigned long field
, u16 value
)
1692 vmcs_check16(field
);
1693 __vmcs_writel(field
, value
);
1696 static __always_inline
void vmcs_write32(unsigned long field
, u32 value
)
1698 vmcs_check32(field
);
1699 __vmcs_writel(field
, value
);
1702 static __always_inline
void vmcs_write64(unsigned long field
, u64 value
)
1704 vmcs_check64(field
);
1705 __vmcs_writel(field
, value
);
1706 #ifndef CONFIG_X86_64
1708 __vmcs_writel(field
+1, value
>> 32);
1712 static __always_inline
void vmcs_writel(unsigned long field
, unsigned long value
)
1715 __vmcs_writel(field
, value
);
1718 static __always_inline
void vmcs_clear_bits(unsigned long field
, u32 mask
)
1720 BUILD_BUG_ON_MSG(__builtin_constant_p(field
) && ((field
) & 0x6000) == 0x2000,
1721 "vmcs_clear_bits does not support 64-bit fields");
1722 __vmcs_writel(field
, __vmcs_readl(field
) & ~mask
);
1725 static __always_inline
void vmcs_set_bits(unsigned long field
, u32 mask
)
1727 BUILD_BUG_ON_MSG(__builtin_constant_p(field
) && ((field
) & 0x6000) == 0x2000,
1728 "vmcs_set_bits does not support 64-bit fields");
1729 __vmcs_writel(field
, __vmcs_readl(field
) | mask
);
1732 static inline void vm_entry_controls_reset_shadow(struct vcpu_vmx
*vmx
)
1734 vmx
->vm_entry_controls_shadow
= vmcs_read32(VM_ENTRY_CONTROLS
);
1737 static inline void vm_entry_controls_init(struct vcpu_vmx
*vmx
, u32 val
)
1739 vmcs_write32(VM_ENTRY_CONTROLS
, val
);
1740 vmx
->vm_entry_controls_shadow
= val
;
1743 static inline void vm_entry_controls_set(struct vcpu_vmx
*vmx
, u32 val
)
1745 if (vmx
->vm_entry_controls_shadow
!= val
)
1746 vm_entry_controls_init(vmx
, val
);
1749 static inline u32
vm_entry_controls_get(struct vcpu_vmx
*vmx
)
1751 return vmx
->vm_entry_controls_shadow
;
1755 static inline void vm_entry_controls_setbit(struct vcpu_vmx
*vmx
, u32 val
)
1757 vm_entry_controls_set(vmx
, vm_entry_controls_get(vmx
) | val
);
1760 static inline void vm_entry_controls_clearbit(struct vcpu_vmx
*vmx
, u32 val
)
1762 vm_entry_controls_set(vmx
, vm_entry_controls_get(vmx
) & ~val
);
1765 static inline void vm_exit_controls_reset_shadow(struct vcpu_vmx
*vmx
)
1767 vmx
->vm_exit_controls_shadow
= vmcs_read32(VM_EXIT_CONTROLS
);
1770 static inline void vm_exit_controls_init(struct vcpu_vmx
*vmx
, u32 val
)
1772 vmcs_write32(VM_EXIT_CONTROLS
, val
);
1773 vmx
->vm_exit_controls_shadow
= val
;
1776 static inline void vm_exit_controls_set(struct vcpu_vmx
*vmx
, u32 val
)
1778 if (vmx
->vm_exit_controls_shadow
!= val
)
1779 vm_exit_controls_init(vmx
, val
);
1782 static inline u32
vm_exit_controls_get(struct vcpu_vmx
*vmx
)
1784 return vmx
->vm_exit_controls_shadow
;
1788 static inline void vm_exit_controls_setbit(struct vcpu_vmx
*vmx
, u32 val
)
1790 vm_exit_controls_set(vmx
, vm_exit_controls_get(vmx
) | val
);
1793 static inline void vm_exit_controls_clearbit(struct vcpu_vmx
*vmx
, u32 val
)
1795 vm_exit_controls_set(vmx
, vm_exit_controls_get(vmx
) & ~val
);
1798 static void vmx_segment_cache_clear(struct vcpu_vmx
*vmx
)
1800 vmx
->segment_cache
.bitmask
= 0;
1803 static bool vmx_segment_cache_test_set(struct vcpu_vmx
*vmx
, unsigned seg
,
1807 u32 mask
= 1 << (seg
* SEG_FIELD_NR
+ field
);
1809 if (!(vmx
->vcpu
.arch
.regs_avail
& (1 << VCPU_EXREG_SEGMENTS
))) {
1810 vmx
->vcpu
.arch
.regs_avail
|= (1 << VCPU_EXREG_SEGMENTS
);
1811 vmx
->segment_cache
.bitmask
= 0;
1813 ret
= vmx
->segment_cache
.bitmask
& mask
;
1814 vmx
->segment_cache
.bitmask
|= mask
;
1818 static u16
vmx_read_guest_seg_selector(struct vcpu_vmx
*vmx
, unsigned seg
)
1820 u16
*p
= &vmx
->segment_cache
.seg
[seg
].selector
;
1822 if (!vmx_segment_cache_test_set(vmx
, seg
, SEG_FIELD_SEL
))
1823 *p
= vmcs_read16(kvm_vmx_segment_fields
[seg
].selector
);
1827 static ulong
vmx_read_guest_seg_base(struct vcpu_vmx
*vmx
, unsigned seg
)
1829 ulong
*p
= &vmx
->segment_cache
.seg
[seg
].base
;
1831 if (!vmx_segment_cache_test_set(vmx
, seg
, SEG_FIELD_BASE
))
1832 *p
= vmcs_readl(kvm_vmx_segment_fields
[seg
].base
);
1836 static u32
vmx_read_guest_seg_limit(struct vcpu_vmx
*vmx
, unsigned seg
)
1838 u32
*p
= &vmx
->segment_cache
.seg
[seg
].limit
;
1840 if (!vmx_segment_cache_test_set(vmx
, seg
, SEG_FIELD_LIMIT
))
1841 *p
= vmcs_read32(kvm_vmx_segment_fields
[seg
].limit
);
1845 static u32
vmx_read_guest_seg_ar(struct vcpu_vmx
*vmx
, unsigned seg
)
1847 u32
*p
= &vmx
->segment_cache
.seg
[seg
].ar
;
1849 if (!vmx_segment_cache_test_set(vmx
, seg
, SEG_FIELD_AR
))
1850 *p
= vmcs_read32(kvm_vmx_segment_fields
[seg
].ar_bytes
);
1854 static void update_exception_bitmap(struct kvm_vcpu
*vcpu
)
1858 eb
= (1u << PF_VECTOR
) | (1u << UD_VECTOR
) | (1u << MC_VECTOR
) |
1859 (1u << DB_VECTOR
) | (1u << AC_VECTOR
);
1860 if ((vcpu
->guest_debug
&
1861 (KVM_GUESTDBG_ENABLE
| KVM_GUESTDBG_USE_SW_BP
)) ==
1862 (KVM_GUESTDBG_ENABLE
| KVM_GUESTDBG_USE_SW_BP
))
1863 eb
|= 1u << BP_VECTOR
;
1864 if (to_vmx(vcpu
)->rmode
.vm86_active
)
1867 eb
&= ~(1u << PF_VECTOR
); /* bypass_guest_pf = 0 */
1869 /* When we are running a nested L2 guest and L1 specified for it a
1870 * certain exception bitmap, we must trap the same exceptions and pass
1871 * them to L1. When running L2, we will only handle the exceptions
1872 * specified above if L1 did not want them.
1874 if (is_guest_mode(vcpu
))
1875 eb
|= get_vmcs12(vcpu
)->exception_bitmap
;
1877 vmcs_write32(EXCEPTION_BITMAP
, eb
);
1881 * Check if MSR is intercepted for currently loaded MSR bitmap.
1883 static bool msr_write_intercepted(struct kvm_vcpu
*vcpu
, u32 msr
)
1885 unsigned long *msr_bitmap
;
1886 int f
= sizeof(unsigned long);
1888 if (!cpu_has_vmx_msr_bitmap())
1891 msr_bitmap
= to_vmx(vcpu
)->loaded_vmcs
->msr_bitmap
;
1893 if (msr
<= 0x1fff) {
1894 return !!test_bit(msr
, msr_bitmap
+ 0x800 / f
);
1895 } else if ((msr
>= 0xc0000000) && (msr
<= 0xc0001fff)) {
1897 return !!test_bit(msr
, msr_bitmap
+ 0xc00 / f
);
1904 * Check if MSR is intercepted for L01 MSR bitmap.
1906 static bool msr_write_intercepted_l01(struct kvm_vcpu
*vcpu
, u32 msr
)
1908 unsigned long *msr_bitmap
;
1909 int f
= sizeof(unsigned long);
1911 if (!cpu_has_vmx_msr_bitmap())
1914 msr_bitmap
= to_vmx(vcpu
)->vmcs01
.msr_bitmap
;
1916 if (msr
<= 0x1fff) {
1917 return !!test_bit(msr
, msr_bitmap
+ 0x800 / f
);
1918 } else if ((msr
>= 0xc0000000) && (msr
<= 0xc0001fff)) {
1920 return !!test_bit(msr
, msr_bitmap
+ 0xc00 / f
);
1926 static void clear_atomic_switch_msr_special(struct vcpu_vmx
*vmx
,
1927 unsigned long entry
, unsigned long exit
)
1929 vm_entry_controls_clearbit(vmx
, entry
);
1930 vm_exit_controls_clearbit(vmx
, exit
);
1933 static void clear_atomic_switch_msr(struct vcpu_vmx
*vmx
, unsigned msr
)
1936 struct msr_autoload
*m
= &vmx
->msr_autoload
;
1940 if (cpu_has_load_ia32_efer
) {
1941 clear_atomic_switch_msr_special(vmx
,
1942 VM_ENTRY_LOAD_IA32_EFER
,
1943 VM_EXIT_LOAD_IA32_EFER
);
1947 case MSR_CORE_PERF_GLOBAL_CTRL
:
1948 if (cpu_has_load_perf_global_ctrl
) {
1949 clear_atomic_switch_msr_special(vmx
,
1950 VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL
,
1951 VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL
);
1957 for (i
= 0; i
< m
->nr
; ++i
)
1958 if (m
->guest
[i
].index
== msr
)
1964 m
->guest
[i
] = m
->guest
[m
->nr
];
1965 m
->host
[i
] = m
->host
[m
->nr
];
1966 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT
, m
->nr
);
1967 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT
, m
->nr
);
1970 static void add_atomic_switch_msr_special(struct vcpu_vmx
*vmx
,
1971 unsigned long entry
, unsigned long exit
,
1972 unsigned long guest_val_vmcs
, unsigned long host_val_vmcs
,
1973 u64 guest_val
, u64 host_val
)
1975 vmcs_write64(guest_val_vmcs
, guest_val
);
1976 vmcs_write64(host_val_vmcs
, host_val
);
1977 vm_entry_controls_setbit(vmx
, entry
);
1978 vm_exit_controls_setbit(vmx
, exit
);
1981 static void add_atomic_switch_msr(struct vcpu_vmx
*vmx
, unsigned msr
,
1982 u64 guest_val
, u64 host_val
)
1985 struct msr_autoload
*m
= &vmx
->msr_autoload
;
1989 if (cpu_has_load_ia32_efer
) {
1990 add_atomic_switch_msr_special(vmx
,
1991 VM_ENTRY_LOAD_IA32_EFER
,
1992 VM_EXIT_LOAD_IA32_EFER
,
1995 guest_val
, host_val
);
1999 case MSR_CORE_PERF_GLOBAL_CTRL
:
2000 if (cpu_has_load_perf_global_ctrl
) {
2001 add_atomic_switch_msr_special(vmx
,
2002 VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL
,
2003 VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL
,
2004 GUEST_IA32_PERF_GLOBAL_CTRL
,
2005 HOST_IA32_PERF_GLOBAL_CTRL
,
2006 guest_val
, host_val
);
2010 case MSR_IA32_PEBS_ENABLE
:
2011 /* PEBS needs a quiescent period after being disabled (to write
2012 * a record). Disabling PEBS through VMX MSR swapping doesn't
2013 * provide that period, so a CPU could write host's record into
2016 wrmsrl(MSR_IA32_PEBS_ENABLE
, 0);
2019 for (i
= 0; i
< m
->nr
; ++i
)
2020 if (m
->guest
[i
].index
== msr
)
2023 if (i
== NR_AUTOLOAD_MSRS
) {
2024 printk_once(KERN_WARNING
"Not enough msr switch entries. "
2025 "Can't add msr %x\n", msr
);
2027 } else if (i
== m
->nr
) {
2029 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT
, m
->nr
);
2030 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT
, m
->nr
);
2033 m
->guest
[i
].index
= msr
;
2034 m
->guest
[i
].value
= guest_val
;
2035 m
->host
[i
].index
= msr
;
2036 m
->host
[i
].value
= host_val
;
2039 static bool update_transition_efer(struct vcpu_vmx
*vmx
, int efer_offset
)
2041 u64 guest_efer
= vmx
->vcpu
.arch
.efer
;
2042 u64 ignore_bits
= 0;
2046 * NX is needed to handle CR0.WP=1, CR4.SMEP=1. Testing
2047 * host CPUID is more efficient than testing guest CPUID
2048 * or CR4. Host SMEP is anyway a requirement for guest SMEP.
2050 if (boot_cpu_has(X86_FEATURE_SMEP
))
2051 guest_efer
|= EFER_NX
;
2052 else if (!(guest_efer
& EFER_NX
))
2053 ignore_bits
|= EFER_NX
;
2057 * LMA and LME handled by hardware; SCE meaningless outside long mode.
2059 ignore_bits
|= EFER_SCE
;
2060 #ifdef CONFIG_X86_64
2061 ignore_bits
|= EFER_LMA
| EFER_LME
;
2062 /* SCE is meaningful only in long mode on Intel */
2063 if (guest_efer
& EFER_LMA
)
2064 ignore_bits
&= ~(u64
)EFER_SCE
;
2067 clear_atomic_switch_msr(vmx
, MSR_EFER
);
2070 * On EPT, we can't emulate NX, so we must switch EFER atomically.
2071 * On CPUs that support "load IA32_EFER", always switch EFER
2072 * atomically, since it's faster than switching it manually.
2074 if (cpu_has_load_ia32_efer
||
2075 (enable_ept
&& ((vmx
->vcpu
.arch
.efer
^ host_efer
) & EFER_NX
))) {
2076 if (!(guest_efer
& EFER_LMA
))
2077 guest_efer
&= ~EFER_LME
;
2078 if (guest_efer
!= host_efer
)
2079 add_atomic_switch_msr(vmx
, MSR_EFER
,
2080 guest_efer
, host_efer
);
2083 guest_efer
&= ~ignore_bits
;
2084 guest_efer
|= host_efer
& ignore_bits
;
2086 vmx
->guest_msrs
[efer_offset
].data
= guest_efer
;
2087 vmx
->guest_msrs
[efer_offset
].mask
= ~ignore_bits
;
2093 #ifdef CONFIG_X86_32
2095 * On 32-bit kernels, VM exits still load the FS and GS bases from the
2096 * VMCS rather than the segment table. KVM uses this helper to figure
2097 * out the current bases to poke them into the VMCS before entry.
2099 static unsigned long segment_base(u16 selector
)
2101 struct desc_struct
*table
;
2104 if (!(selector
& ~SEGMENT_RPL_MASK
))
2107 table
= get_current_gdt_ro();
2109 if ((selector
& SEGMENT_TI_MASK
) == SEGMENT_LDT
) {
2110 u16 ldt_selector
= kvm_read_ldt();
2112 if (!(ldt_selector
& ~SEGMENT_RPL_MASK
))
2115 table
= (struct desc_struct
*)segment_base(ldt_selector
);
2117 v
= get_desc_base(&table
[selector
>> 3]);
2122 static void vmx_save_host_state(struct kvm_vcpu
*vcpu
)
2124 struct vcpu_vmx
*vmx
= to_vmx(vcpu
);
2127 if (vmx
->host_state
.loaded
)
2130 vmx
->host_state
.loaded
= 1;
2132 * Set host fs and gs selectors. Unfortunately, 22.2.3 does not
2133 * allow segment selectors with cpl > 0 or ti == 1.
2135 vmx
->host_state
.ldt_sel
= kvm_read_ldt();
2136 vmx
->host_state
.gs_ldt_reload_needed
= vmx
->host_state
.ldt_sel
;
2137 savesegment(fs
, vmx
->host_state
.fs_sel
);
2138 if (!(vmx
->host_state
.fs_sel
& 7)) {
2139 vmcs_write16(HOST_FS_SELECTOR
, vmx
->host_state
.fs_sel
);
2140 vmx
->host_state
.fs_reload_needed
= 0;
2142 vmcs_write16(HOST_FS_SELECTOR
, 0);
2143 vmx
->host_state
.fs_reload_needed
= 1;
2145 savesegment(gs
, vmx
->host_state
.gs_sel
);
2146 if (!(vmx
->host_state
.gs_sel
& 7))
2147 vmcs_write16(HOST_GS_SELECTOR
, vmx
->host_state
.gs_sel
);
2149 vmcs_write16(HOST_GS_SELECTOR
, 0);
2150 vmx
->host_state
.gs_ldt_reload_needed
= 1;
2153 #ifdef CONFIG_X86_64
2154 savesegment(ds
, vmx
->host_state
.ds_sel
);
2155 savesegment(es
, vmx
->host_state
.es_sel
);
2158 #ifdef CONFIG_X86_64
2159 vmcs_writel(HOST_FS_BASE
, read_msr(MSR_FS_BASE
));
2160 vmcs_writel(HOST_GS_BASE
, read_msr(MSR_GS_BASE
));
2162 vmcs_writel(HOST_FS_BASE
, segment_base(vmx
->host_state
.fs_sel
));
2163 vmcs_writel(HOST_GS_BASE
, segment_base(vmx
->host_state
.gs_sel
));
2166 #ifdef CONFIG_X86_64
2167 rdmsrl(MSR_KERNEL_GS_BASE
, vmx
->msr_host_kernel_gs_base
);
2168 if (is_long_mode(&vmx
->vcpu
))
2169 wrmsrl(MSR_KERNEL_GS_BASE
, vmx
->msr_guest_kernel_gs_base
);
2171 if (boot_cpu_has(X86_FEATURE_MPX
))
2172 rdmsrl(MSR_IA32_BNDCFGS
, vmx
->host_state
.msr_host_bndcfgs
);
2173 for (i
= 0; i
< vmx
->save_nmsrs
; ++i
)
2174 kvm_set_shared_msr(vmx
->guest_msrs
[i
].index
,
2175 vmx
->guest_msrs
[i
].data
,
2176 vmx
->guest_msrs
[i
].mask
);
2179 static void __vmx_load_host_state(struct vcpu_vmx
*vmx
)
2181 if (!vmx
->host_state
.loaded
)
2184 ++vmx
->vcpu
.stat
.host_state_reload
;
2185 vmx
->host_state
.loaded
= 0;
2186 #ifdef CONFIG_X86_64
2187 if (is_long_mode(&vmx
->vcpu
))
2188 rdmsrl(MSR_KERNEL_GS_BASE
, vmx
->msr_guest_kernel_gs_base
);
2190 if (vmx
->host_state
.gs_ldt_reload_needed
) {
2191 kvm_load_ldt(vmx
->host_state
.ldt_sel
);
2192 #ifdef CONFIG_X86_64
2193 load_gs_index(vmx
->host_state
.gs_sel
);
2195 loadsegment(gs
, vmx
->host_state
.gs_sel
);
2198 if (vmx
->host_state
.fs_reload_needed
)
2199 loadsegment(fs
, vmx
->host_state
.fs_sel
);
2200 #ifdef CONFIG_X86_64
2201 if (unlikely(vmx
->host_state
.ds_sel
| vmx
->host_state
.es_sel
)) {
2202 loadsegment(ds
, vmx
->host_state
.ds_sel
);
2203 loadsegment(es
, vmx
->host_state
.es_sel
);
2206 invalidate_tss_limit();
2207 #ifdef CONFIG_X86_64
2208 wrmsrl(MSR_KERNEL_GS_BASE
, vmx
->msr_host_kernel_gs_base
);
2210 if (vmx
->host_state
.msr_host_bndcfgs
)
2211 wrmsrl(MSR_IA32_BNDCFGS
, vmx
->host_state
.msr_host_bndcfgs
);
2212 load_fixmap_gdt(raw_smp_processor_id());
2215 static void vmx_load_host_state(struct vcpu_vmx
*vmx
)
2218 __vmx_load_host_state(vmx
);
2222 static void vmx_vcpu_pi_load(struct kvm_vcpu
*vcpu
, int cpu
)
2224 struct pi_desc
*pi_desc
= vcpu_to_pi_desc(vcpu
);
2225 struct pi_desc old
, new;
2229 * In case of hot-plug or hot-unplug, we may have to undo
2230 * vmx_vcpu_pi_put even if there is no assigned device. And we
2231 * always keep PI.NDST up to date for simplicity: it makes the
2232 * code easier, and CPU migration is not a fast path.
2234 if (!pi_test_sn(pi_desc
) && vcpu
->cpu
== cpu
)
2238 * First handle the simple case where no cmpxchg is necessary; just
2239 * allow posting non-urgent interrupts.
2241 * If the 'nv' field is POSTED_INTR_WAKEUP_VECTOR, do not change
2242 * PI.NDST: pi_post_block will do it for us and the wakeup_handler
2243 * expects the VCPU to be on the blocked_vcpu_list that matches
2246 if (pi_desc
->nv
== POSTED_INTR_WAKEUP_VECTOR
||
2248 pi_clear_sn(pi_desc
);
2252 /* The full case. */
2254 old
.control
= new.control
= pi_desc
->control
;
2256 dest
= cpu_physical_id(cpu
);
2258 if (x2apic_enabled())
2261 new.ndst
= (dest
<< 8) & 0xFF00;
2264 } while (cmpxchg64(&pi_desc
->control
, old
.control
,
2265 new.control
) != old
.control
);
2268 static void decache_tsc_multiplier(struct vcpu_vmx
*vmx
)
2270 vmx
->current_tsc_ratio
= vmx
->vcpu
.arch
.tsc_scaling_ratio
;
2271 vmcs_write64(TSC_MULTIPLIER
, vmx
->current_tsc_ratio
);
2275 * Switches to specified vcpu, until a matching vcpu_put(), but assumes
2276 * vcpu mutex is already taken.
2278 static void vmx_vcpu_load(struct kvm_vcpu
*vcpu
, int cpu
)
2280 struct vcpu_vmx
*vmx
= to_vmx(vcpu
);
2281 bool already_loaded
= vmx
->loaded_vmcs
->cpu
== cpu
;
2283 if (!already_loaded
) {
2284 loaded_vmcs_clear(vmx
->loaded_vmcs
);
2285 local_irq_disable();
2286 crash_disable_local_vmclear(cpu
);
2289 * Read loaded_vmcs->cpu should be before fetching
2290 * loaded_vmcs->loaded_vmcss_on_cpu_link.
2291 * See the comments in __loaded_vmcs_clear().
2295 list_add(&vmx
->loaded_vmcs
->loaded_vmcss_on_cpu_link
,
2296 &per_cpu(loaded_vmcss_on_cpu
, cpu
));
2297 crash_enable_local_vmclear(cpu
);
2301 if (per_cpu(current_vmcs
, cpu
) != vmx
->loaded_vmcs
->vmcs
) {
2302 per_cpu(current_vmcs
, cpu
) = vmx
->loaded_vmcs
->vmcs
;
2303 vmcs_load(vmx
->loaded_vmcs
->vmcs
);
2304 indirect_branch_prediction_barrier();
2307 if (!already_loaded
) {
2308 void *gdt
= get_current_gdt_ro();
2309 unsigned long sysenter_esp
;
2311 kvm_make_request(KVM_REQ_TLB_FLUSH
, vcpu
);
2314 * Linux uses per-cpu TSS and GDT, so set these when switching
2315 * processors. See 22.2.4.
2317 vmcs_writel(HOST_TR_BASE
,
2318 (unsigned long)&get_cpu_entry_area(cpu
)->tss
.x86_tss
);
2319 vmcs_writel(HOST_GDTR_BASE
, (unsigned long)gdt
); /* 22.2.4 */
2322 * VM exits change the host TR limit to 0x67 after a VM
2323 * exit. This is okay, since 0x67 covers everything except
2324 * the IO bitmap and have have code to handle the IO bitmap
2325 * being lost after a VM exit.
2327 BUILD_BUG_ON(IO_BITMAP_OFFSET
- 1 != 0x67);
2329 rdmsrl(MSR_IA32_SYSENTER_ESP
, sysenter_esp
);
2330 vmcs_writel(HOST_IA32_SYSENTER_ESP
, sysenter_esp
); /* 22.2.3 */
2332 vmx
->loaded_vmcs
->cpu
= cpu
;
2335 /* Setup TSC multiplier */
2336 if (kvm_has_tsc_control
&&
2337 vmx
->current_tsc_ratio
!= vcpu
->arch
.tsc_scaling_ratio
)
2338 decache_tsc_multiplier(vmx
);
2340 vmx_vcpu_pi_load(vcpu
, cpu
);
2341 vmx
->host_pkru
= read_pkru();
2342 vmx
->host_debugctlmsr
= get_debugctlmsr();
2345 static void vmx_vcpu_pi_put(struct kvm_vcpu
*vcpu
)
2347 struct pi_desc
*pi_desc
= vcpu_to_pi_desc(vcpu
);
2349 if (!kvm_arch_has_assigned_device(vcpu
->kvm
) ||
2350 !irq_remapping_cap(IRQ_POSTING_CAP
) ||
2351 !kvm_vcpu_apicv_active(vcpu
))
2354 /* Set SN when the vCPU is preempted */
2355 if (vcpu
->preempted
)
2359 static void vmx_vcpu_put(struct kvm_vcpu
*vcpu
)
2361 vmx_vcpu_pi_put(vcpu
);
2363 __vmx_load_host_state(to_vmx(vcpu
));
2366 static bool emulation_required(struct kvm_vcpu
*vcpu
)
2368 return emulate_invalid_guest_state
&& !guest_state_valid(vcpu
);
2371 static void vmx_decache_cr0_guest_bits(struct kvm_vcpu
*vcpu
);
2374 * Return the cr0 value that a nested guest would read. This is a combination
2375 * of the real cr0 used to run the guest (guest_cr0), and the bits shadowed by
2376 * its hypervisor (cr0_read_shadow).
2378 static inline unsigned long nested_read_cr0(struct vmcs12
*fields
)
2380 return (fields
->guest_cr0
& ~fields
->cr0_guest_host_mask
) |
2381 (fields
->cr0_read_shadow
& fields
->cr0_guest_host_mask
);
2383 static inline unsigned long nested_read_cr4(struct vmcs12
*fields
)
2385 return (fields
->guest_cr4
& ~fields
->cr4_guest_host_mask
) |
2386 (fields
->cr4_read_shadow
& fields
->cr4_guest_host_mask
);
2389 static unsigned long vmx_get_rflags(struct kvm_vcpu
*vcpu
)
2391 unsigned long rflags
, save_rflags
;
2393 if (!test_bit(VCPU_EXREG_RFLAGS
, (ulong
*)&vcpu
->arch
.regs_avail
)) {
2394 __set_bit(VCPU_EXREG_RFLAGS
, (ulong
*)&vcpu
->arch
.regs_avail
);
2395 rflags
= vmcs_readl(GUEST_RFLAGS
);
2396 if (to_vmx(vcpu
)->rmode
.vm86_active
) {
2397 rflags
&= RMODE_GUEST_OWNED_EFLAGS_BITS
;
2398 save_rflags
= to_vmx(vcpu
)->rmode
.save_rflags
;
2399 rflags
|= save_rflags
& ~RMODE_GUEST_OWNED_EFLAGS_BITS
;
2401 to_vmx(vcpu
)->rflags
= rflags
;
2403 return to_vmx(vcpu
)->rflags
;
2406 static void vmx_set_rflags(struct kvm_vcpu
*vcpu
, unsigned long rflags
)
2408 unsigned long old_rflags
= vmx_get_rflags(vcpu
);
2410 __set_bit(VCPU_EXREG_RFLAGS
, (ulong
*)&vcpu
->arch
.regs_avail
);
2411 to_vmx(vcpu
)->rflags
= rflags
;
2412 if (to_vmx(vcpu
)->rmode
.vm86_active
) {
2413 to_vmx(vcpu
)->rmode
.save_rflags
= rflags
;
2414 rflags
|= X86_EFLAGS_IOPL
| X86_EFLAGS_VM
;
2416 vmcs_writel(GUEST_RFLAGS
, rflags
);
2418 if ((old_rflags
^ to_vmx(vcpu
)->rflags
) & X86_EFLAGS_VM
)
2419 to_vmx(vcpu
)->emulation_required
= emulation_required(vcpu
);
2422 static u32
vmx_get_interrupt_shadow(struct kvm_vcpu
*vcpu
)
2424 u32 interruptibility
= vmcs_read32(GUEST_INTERRUPTIBILITY_INFO
);
2427 if (interruptibility
& GUEST_INTR_STATE_STI
)
2428 ret
|= KVM_X86_SHADOW_INT_STI
;
2429 if (interruptibility
& GUEST_INTR_STATE_MOV_SS
)
2430 ret
|= KVM_X86_SHADOW_INT_MOV_SS
;
2435 static void vmx_set_interrupt_shadow(struct kvm_vcpu
*vcpu
, int mask
)
2437 u32 interruptibility_old
= vmcs_read32(GUEST_INTERRUPTIBILITY_INFO
);
2438 u32 interruptibility
= interruptibility_old
;
2440 interruptibility
&= ~(GUEST_INTR_STATE_STI
| GUEST_INTR_STATE_MOV_SS
);
2442 if (mask
& KVM_X86_SHADOW_INT_MOV_SS
)
2443 interruptibility
|= GUEST_INTR_STATE_MOV_SS
;
2444 else if (mask
& KVM_X86_SHADOW_INT_STI
)
2445 interruptibility
|= GUEST_INTR_STATE_STI
;
2447 if ((interruptibility
!= interruptibility_old
))
2448 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO
, interruptibility
);
2451 static void skip_emulated_instruction(struct kvm_vcpu
*vcpu
)
2455 rip
= kvm_rip_read(vcpu
);
2456 rip
+= vmcs_read32(VM_EXIT_INSTRUCTION_LEN
);
2457 kvm_rip_write(vcpu
, rip
);
2459 /* skipping an emulated instruction also counts */
2460 vmx_set_interrupt_shadow(vcpu
, 0);
2463 static void nested_vmx_inject_exception_vmexit(struct kvm_vcpu
*vcpu
,
2464 unsigned long exit_qual
)
2466 struct vmcs12
*vmcs12
= get_vmcs12(vcpu
);
2467 unsigned int nr
= vcpu
->arch
.exception
.nr
;
2468 u32 intr_info
= nr
| INTR_INFO_VALID_MASK
;
2470 if (vcpu
->arch
.exception
.has_error_code
) {
2471 vmcs12
->vm_exit_intr_error_code
= vcpu
->arch
.exception
.error_code
;
2472 intr_info
|= INTR_INFO_DELIVER_CODE_MASK
;
2475 if (kvm_exception_is_soft(nr
))
2476 intr_info
|= INTR_TYPE_SOFT_EXCEPTION
;
2478 intr_info
|= INTR_TYPE_HARD_EXCEPTION
;
2480 if (!(vmcs12
->idt_vectoring_info_field
& VECTORING_INFO_VALID_MASK
) &&
2481 vmx_get_nmi_mask(vcpu
))
2482 intr_info
|= INTR_INFO_UNBLOCK_NMI
;
2484 nested_vmx_vmexit(vcpu
, EXIT_REASON_EXCEPTION_NMI
, intr_info
, exit_qual
);
2488 * KVM wants to inject page-faults which it got to the guest. This function
2489 * checks whether in a nested guest, we need to inject them to L1 or L2.
2491 static int nested_vmx_check_exception(struct kvm_vcpu
*vcpu
, unsigned long *exit_qual
)
2493 struct vmcs12
*vmcs12
= get_vmcs12(vcpu
);
2494 unsigned int nr
= vcpu
->arch
.exception
.nr
;
2496 if (nr
== PF_VECTOR
) {
2497 if (vcpu
->arch
.exception
.nested_apf
) {
2498 *exit_qual
= vcpu
->arch
.apf
.nested_apf_token
;
2502 * FIXME: we must not write CR2 when L1 intercepts an L2 #PF exception.
2503 * The fix is to add the ancillary datum (CR2 or DR6) to structs
2504 * kvm_queued_exception and kvm_vcpu_events, so that CR2 and DR6
2505 * can be written only when inject_pending_event runs. This should be
2506 * conditional on a new capability---if the capability is disabled,
2507 * kvm_multiple_exception would write the ancillary information to
2508 * CR2 or DR6, for backwards ABI-compatibility.
2510 if (nested_vmx_is_page_fault_vmexit(vmcs12
,
2511 vcpu
->arch
.exception
.error_code
)) {
2512 *exit_qual
= vcpu
->arch
.cr2
;
2516 if (vmcs12
->exception_bitmap
& (1u << nr
)) {
2517 if (nr
== DB_VECTOR
)
2518 *exit_qual
= vcpu
->arch
.dr6
;
2528 static void vmx_queue_exception(struct kvm_vcpu
*vcpu
)
2530 struct vcpu_vmx
*vmx
= to_vmx(vcpu
);
2531 unsigned nr
= vcpu
->arch
.exception
.nr
;
2532 bool has_error_code
= vcpu
->arch
.exception
.has_error_code
;
2533 u32 error_code
= vcpu
->arch
.exception
.error_code
;
2534 u32 intr_info
= nr
| INTR_INFO_VALID_MASK
;
2536 if (has_error_code
) {
2537 vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE
, error_code
);
2538 intr_info
|= INTR_INFO_DELIVER_CODE_MASK
;
2541 if (vmx
->rmode
.vm86_active
) {
2543 if (kvm_exception_is_soft(nr
))
2544 inc_eip
= vcpu
->arch
.event_exit_inst_len
;
2545 if (kvm_inject_realmode_interrupt(vcpu
, nr
, inc_eip
) != EMULATE_DONE
)
2546 kvm_make_request(KVM_REQ_TRIPLE_FAULT
, vcpu
);
2550 if (kvm_exception_is_soft(nr
)) {
2551 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN
,
2552 vmx
->vcpu
.arch
.event_exit_inst_len
);
2553 intr_info
|= INTR_TYPE_SOFT_EXCEPTION
;
2555 intr_info
|= INTR_TYPE_HARD_EXCEPTION
;
2557 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD
, intr_info
);
2560 static bool vmx_rdtscp_supported(void)
2562 return cpu_has_vmx_rdtscp();
2565 static bool vmx_invpcid_supported(void)
2567 return cpu_has_vmx_invpcid() && enable_ept
;
2571 * Swap MSR entry in host/guest MSR entry array.
2573 static void move_msr_up(struct vcpu_vmx
*vmx
, int from
, int to
)
2575 struct shared_msr_entry tmp
;
2577 tmp
= vmx
->guest_msrs
[to
];
2578 vmx
->guest_msrs
[to
] = vmx
->guest_msrs
[from
];
2579 vmx
->guest_msrs
[from
] = tmp
;
2583 * Set up the vmcs to automatically save and restore system
2584 * msrs. Don't touch the 64-bit msrs if the guest is in legacy
2585 * mode, as fiddling with msrs is very expensive.
2587 static void setup_msrs(struct vcpu_vmx
*vmx
)
2589 int save_nmsrs
, index
;
2592 #ifdef CONFIG_X86_64
2593 if (is_long_mode(&vmx
->vcpu
)) {
2594 index
= __find_msr_index(vmx
, MSR_SYSCALL_MASK
);
2596 move_msr_up(vmx
, index
, save_nmsrs
++);
2597 index
= __find_msr_index(vmx
, MSR_LSTAR
);
2599 move_msr_up(vmx
, index
, save_nmsrs
++);
2600 index
= __find_msr_index(vmx
, MSR_CSTAR
);
2602 move_msr_up(vmx
, index
, save_nmsrs
++);
2603 index
= __find_msr_index(vmx
, MSR_TSC_AUX
);
2604 if (index
>= 0 && guest_cpuid_has(&vmx
->vcpu
, X86_FEATURE_RDTSCP
))
2605 move_msr_up(vmx
, index
, save_nmsrs
++);
2607 * MSR_STAR is only needed on long mode guests, and only
2608 * if efer.sce is enabled.
2610 index
= __find_msr_index(vmx
, MSR_STAR
);
2611 if ((index
>= 0) && (vmx
->vcpu
.arch
.efer
& EFER_SCE
))
2612 move_msr_up(vmx
, index
, save_nmsrs
++);
2615 index
= __find_msr_index(vmx
, MSR_EFER
);
2616 if (index
>= 0 && update_transition_efer(vmx
, index
))
2617 move_msr_up(vmx
, index
, save_nmsrs
++);
2619 vmx
->save_nmsrs
= save_nmsrs
;
2621 if (cpu_has_vmx_msr_bitmap())
2622 vmx_update_msr_bitmap(&vmx
->vcpu
);
2626 * reads and returns guest's timestamp counter "register"
2627 * guest_tsc = (host_tsc * tsc multiplier) >> 48 + tsc_offset
2628 * -- Intel TSC Scaling for Virtualization White Paper, sec 1.3
2630 static u64
guest_read_tsc(struct kvm_vcpu
*vcpu
)
2632 u64 host_tsc
, tsc_offset
;
2635 tsc_offset
= vmcs_read64(TSC_OFFSET
);
2636 return kvm_scale_tsc(vcpu
, host_tsc
) + tsc_offset
;
2640 * writes 'offset' into guest's timestamp counter offset register
2642 static void vmx_write_tsc_offset(struct kvm_vcpu
*vcpu
, u64 offset
)
2644 if (is_guest_mode(vcpu
)) {
2646 * We're here if L1 chose not to trap WRMSR to TSC. According
2647 * to the spec, this should set L1's TSC; The offset that L1
2648 * set for L2 remains unchanged, and still needs to be added
2649 * to the newly set TSC to get L2's TSC.
2651 struct vmcs12
*vmcs12
;
2652 /* recalculate vmcs02.TSC_OFFSET: */
2653 vmcs12
= get_vmcs12(vcpu
);
2654 vmcs_write64(TSC_OFFSET
, offset
+
2655 (nested_cpu_has(vmcs12
, CPU_BASED_USE_TSC_OFFSETING
) ?
2656 vmcs12
->tsc_offset
: 0));
2658 trace_kvm_write_tsc_offset(vcpu
->vcpu_id
,
2659 vmcs_read64(TSC_OFFSET
), offset
);
2660 vmcs_write64(TSC_OFFSET
, offset
);
2665 * nested_vmx_allowed() checks whether a guest should be allowed to use VMX
2666 * instructions and MSRs (i.e., nested VMX). Nested VMX is disabled for
2667 * all guests if the "nested" module option is off, and can also be disabled
2668 * for a single guest by disabling its VMX cpuid bit.
2670 static inline bool nested_vmx_allowed(struct kvm_vcpu
*vcpu
)
2672 return nested
&& guest_cpuid_has(vcpu
, X86_FEATURE_VMX
);
2676 * nested_vmx_setup_ctls_msrs() sets up variables containing the values to be
2677 * returned for the various VMX controls MSRs when nested VMX is enabled.
2678 * The same values should also be used to verify that vmcs12 control fields are
2679 * valid during nested entry from L1 to L2.
2680 * Each of these control msrs has a low and high 32-bit half: A low bit is on
2681 * if the corresponding bit in the (32-bit) control field *must* be on, and a
2682 * bit in the high half is on if the corresponding bit in the control field
2683 * may be on. See also vmx_control_verify().
2685 static void nested_vmx_setup_ctls_msrs(struct vcpu_vmx
*vmx
)
2688 * Note that as a general rule, the high half of the MSRs (bits in
2689 * the control fields which may be 1) should be initialized by the
2690 * intersection of the underlying hardware's MSR (i.e., features which
2691 * can be supported) and the list of features we want to expose -
2692 * because they are known to be properly supported in our code.
2693 * Also, usually, the low half of the MSRs (bits which must be 1) can
2694 * be set to 0, meaning that L1 may turn off any of these bits. The
2695 * reason is that if one of these bits is necessary, it will appear
2696 * in vmcs01 and prepare_vmcs02, when it bitwise-or's the control
2697 * fields of vmcs01 and vmcs02, will turn these bits off - and
2698 * nested_vmx_exit_reflected() will not pass related exits to L1.
2699 * These rules have exceptions below.
2702 /* pin-based controls */
2703 rdmsr(MSR_IA32_VMX_PINBASED_CTLS
,
2704 vmx
->nested
.nested_vmx_pinbased_ctls_low
,
2705 vmx
->nested
.nested_vmx_pinbased_ctls_high
);
2706 vmx
->nested
.nested_vmx_pinbased_ctls_low
|=
2707 PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR
;
2708 vmx
->nested
.nested_vmx_pinbased_ctls_high
&=
2709 PIN_BASED_EXT_INTR_MASK
|
2710 PIN_BASED_NMI_EXITING
|
2711 PIN_BASED_VIRTUAL_NMIS
;
2712 vmx
->nested
.nested_vmx_pinbased_ctls_high
|=
2713 PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR
|
2714 PIN_BASED_VMX_PREEMPTION_TIMER
;
2715 if (kvm_vcpu_apicv_active(&vmx
->vcpu
))
2716 vmx
->nested
.nested_vmx_pinbased_ctls_high
|=
2717 PIN_BASED_POSTED_INTR
;
2720 rdmsr(MSR_IA32_VMX_EXIT_CTLS
,
2721 vmx
->nested
.nested_vmx_exit_ctls_low
,
2722 vmx
->nested
.nested_vmx_exit_ctls_high
);
2723 vmx
->nested
.nested_vmx_exit_ctls_low
=
2724 VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR
;
2726 vmx
->nested
.nested_vmx_exit_ctls_high
&=
2727 #ifdef CONFIG_X86_64
2728 VM_EXIT_HOST_ADDR_SPACE_SIZE
|
2730 VM_EXIT_LOAD_IA32_PAT
| VM_EXIT_SAVE_IA32_PAT
;
2731 vmx
->nested
.nested_vmx_exit_ctls_high
|=
2732 VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR
|
2733 VM_EXIT_LOAD_IA32_EFER
| VM_EXIT_SAVE_IA32_EFER
|
2734 VM_EXIT_SAVE_VMX_PREEMPTION_TIMER
| VM_EXIT_ACK_INTR_ON_EXIT
;
2736 if (kvm_mpx_supported())
2737 vmx
->nested
.nested_vmx_exit_ctls_high
|= VM_EXIT_CLEAR_BNDCFGS
;
2739 /* We support free control of debug control saving. */
2740 vmx
->nested
.nested_vmx_exit_ctls_low
&= ~VM_EXIT_SAVE_DEBUG_CONTROLS
;
2742 /* entry controls */
2743 rdmsr(MSR_IA32_VMX_ENTRY_CTLS
,
2744 vmx
->nested
.nested_vmx_entry_ctls_low
,
2745 vmx
->nested
.nested_vmx_entry_ctls_high
);
2746 vmx
->nested
.nested_vmx_entry_ctls_low
=
2747 VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR
;
2748 vmx
->nested
.nested_vmx_entry_ctls_high
&=
2749 #ifdef CONFIG_X86_64
2750 VM_ENTRY_IA32E_MODE
|
2752 VM_ENTRY_LOAD_IA32_PAT
;
2753 vmx
->nested
.nested_vmx_entry_ctls_high
|=
2754 (VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR
| VM_ENTRY_LOAD_IA32_EFER
);
2755 if (kvm_mpx_supported())
2756 vmx
->nested
.nested_vmx_entry_ctls_high
|= VM_ENTRY_LOAD_BNDCFGS
;
2758 /* We support free control of debug control loading. */
2759 vmx
->nested
.nested_vmx_entry_ctls_low
&= ~VM_ENTRY_LOAD_DEBUG_CONTROLS
;
2761 /* cpu-based controls */
2762 rdmsr(MSR_IA32_VMX_PROCBASED_CTLS
,
2763 vmx
->nested
.nested_vmx_procbased_ctls_low
,
2764 vmx
->nested
.nested_vmx_procbased_ctls_high
);
2765 vmx
->nested
.nested_vmx_procbased_ctls_low
=
2766 CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR
;
2767 vmx
->nested
.nested_vmx_procbased_ctls_high
&=
2768 CPU_BASED_VIRTUAL_INTR_PENDING
|
2769 CPU_BASED_VIRTUAL_NMI_PENDING
| CPU_BASED_USE_TSC_OFFSETING
|
2770 CPU_BASED_HLT_EXITING
| CPU_BASED_INVLPG_EXITING
|
2771 CPU_BASED_MWAIT_EXITING
| CPU_BASED_CR3_LOAD_EXITING
|
2772 CPU_BASED_CR3_STORE_EXITING
|
2773 #ifdef CONFIG_X86_64
2774 CPU_BASED_CR8_LOAD_EXITING
| CPU_BASED_CR8_STORE_EXITING
|
2776 CPU_BASED_MOV_DR_EXITING
| CPU_BASED_UNCOND_IO_EXITING
|
2777 CPU_BASED_USE_IO_BITMAPS
| CPU_BASED_MONITOR_TRAP_FLAG
|
2778 CPU_BASED_MONITOR_EXITING
| CPU_BASED_RDPMC_EXITING
|
2779 CPU_BASED_RDTSC_EXITING
| CPU_BASED_PAUSE_EXITING
|
2780 CPU_BASED_TPR_SHADOW
| CPU_BASED_ACTIVATE_SECONDARY_CONTROLS
;
2782 * We can allow some features even when not supported by the
2783 * hardware. For example, L1 can specify an MSR bitmap - and we
2784 * can use it to avoid exits to L1 - even when L0 runs L2
2785 * without MSR bitmaps.
2787 vmx
->nested
.nested_vmx_procbased_ctls_high
|=
2788 CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR
|
2789 CPU_BASED_USE_MSR_BITMAPS
;
2791 /* We support free control of CR3 access interception. */
2792 vmx
->nested
.nested_vmx_procbased_ctls_low
&=
2793 ~(CPU_BASED_CR3_LOAD_EXITING
| CPU_BASED_CR3_STORE_EXITING
);
2796 * secondary cpu-based controls. Do not include those that
2797 * depend on CPUID bits, they are added later by vmx_cpuid_update.
2799 rdmsr(MSR_IA32_VMX_PROCBASED_CTLS2
,
2800 vmx
->nested
.nested_vmx_secondary_ctls_low
,
2801 vmx
->nested
.nested_vmx_secondary_ctls_high
);
2802 vmx
->nested
.nested_vmx_secondary_ctls_low
= 0;
2803 vmx
->nested
.nested_vmx_secondary_ctls_high
&=
2804 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES
|
2805 SECONDARY_EXEC_DESC
|
2806 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE
|
2807 SECONDARY_EXEC_APIC_REGISTER_VIRT
|
2808 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY
|
2809 SECONDARY_EXEC_WBINVD_EXITING
;
2812 /* nested EPT: emulate EPT also to L1 */
2813 vmx
->nested
.nested_vmx_secondary_ctls_high
|=
2814 SECONDARY_EXEC_ENABLE_EPT
;
2815 vmx
->nested
.nested_vmx_ept_caps
= VMX_EPT_PAGE_WALK_4_BIT
|
2816 VMX_EPTP_WB_BIT
| VMX_EPT_INVEPT_BIT
;
2817 if (cpu_has_vmx_ept_execute_only())
2818 vmx
->nested
.nested_vmx_ept_caps
|=
2819 VMX_EPT_EXECUTE_ONLY_BIT
;
2820 vmx
->nested
.nested_vmx_ept_caps
&= vmx_capability
.ept
;
2821 vmx
->nested
.nested_vmx_ept_caps
|= VMX_EPT_EXTENT_GLOBAL_BIT
|
2822 VMX_EPT_EXTENT_CONTEXT_BIT
| VMX_EPT_2MB_PAGE_BIT
|
2823 VMX_EPT_1GB_PAGE_BIT
;
2824 if (enable_ept_ad_bits
) {
2825 vmx
->nested
.nested_vmx_secondary_ctls_high
|=
2826 SECONDARY_EXEC_ENABLE_PML
;
2827 vmx
->nested
.nested_vmx_ept_caps
|= VMX_EPT_AD_BIT
;
2831 if (cpu_has_vmx_vmfunc()) {
2832 vmx
->nested
.nested_vmx_secondary_ctls_high
|=
2833 SECONDARY_EXEC_ENABLE_VMFUNC
;
2835 * Advertise EPTP switching unconditionally
2836 * since we emulate it
2839 vmx
->nested
.nested_vmx_vmfunc_controls
=
2840 VMX_VMFUNC_EPTP_SWITCHING
;
2844 * Old versions of KVM use the single-context version without
2845 * checking for support, so declare that it is supported even
2846 * though it is treated as global context. The alternative is
2847 * not failing the single-context invvpid, and it is worse.
2850 vmx
->nested
.nested_vmx_secondary_ctls_high
|=
2851 SECONDARY_EXEC_ENABLE_VPID
;
2852 vmx
->nested
.nested_vmx_vpid_caps
= VMX_VPID_INVVPID_BIT
|
2853 VMX_VPID_EXTENT_SUPPORTED_MASK
;
2856 if (enable_unrestricted_guest
)
2857 vmx
->nested
.nested_vmx_secondary_ctls_high
|=
2858 SECONDARY_EXEC_UNRESTRICTED_GUEST
;
2860 /* miscellaneous data */
2861 rdmsr(MSR_IA32_VMX_MISC
,
2862 vmx
->nested
.nested_vmx_misc_low
,
2863 vmx
->nested
.nested_vmx_misc_high
);
2864 vmx
->nested
.nested_vmx_misc_low
&= VMX_MISC_SAVE_EFER_LMA
;
2865 vmx
->nested
.nested_vmx_misc_low
|=
2866 VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE
|
2867 VMX_MISC_ACTIVITY_HLT
;
2868 vmx
->nested
.nested_vmx_misc_high
= 0;
2871 * This MSR reports some information about VMX support. We
2872 * should return information about the VMX we emulate for the
2873 * guest, and the VMCS structure we give it - not about the
2874 * VMX support of the underlying hardware.
2876 vmx
->nested
.nested_vmx_basic
=
2878 VMX_BASIC_TRUE_CTLS
|
2879 ((u64
)VMCS12_SIZE
<< VMX_BASIC_VMCS_SIZE_SHIFT
) |
2880 (VMX_BASIC_MEM_TYPE_WB
<< VMX_BASIC_MEM_TYPE_SHIFT
);
2882 if (cpu_has_vmx_basic_inout())
2883 vmx
->nested
.nested_vmx_basic
|= VMX_BASIC_INOUT
;
2886 * These MSRs specify bits which the guest must keep fixed on
2887 * while L1 is in VMXON mode (in L1's root mode, or running an L2).
2888 * We picked the standard core2 setting.
2890 #define VMXON_CR0_ALWAYSON (X86_CR0_PE | X86_CR0_PG | X86_CR0_NE)
2891 #define VMXON_CR4_ALWAYSON X86_CR4_VMXE
2892 vmx
->nested
.nested_vmx_cr0_fixed0
= VMXON_CR0_ALWAYSON
;
2893 vmx
->nested
.nested_vmx_cr4_fixed0
= VMXON_CR4_ALWAYSON
;
2895 /* These MSRs specify bits which the guest must keep fixed off. */
2896 rdmsrl(MSR_IA32_VMX_CR0_FIXED1
, vmx
->nested
.nested_vmx_cr0_fixed1
);
2897 rdmsrl(MSR_IA32_VMX_CR4_FIXED1
, vmx
->nested
.nested_vmx_cr4_fixed1
);
2899 /* highest index: VMX_PREEMPTION_TIMER_VALUE */
2900 vmx
->nested
.nested_vmx_vmcs_enum
= VMCS12_MAX_FIELD_INDEX
<< 1;
2904 * if fixed0[i] == 1: val[i] must be 1
2905 * if fixed1[i] == 0: val[i] must be 0
2907 static inline bool fixed_bits_valid(u64 val
, u64 fixed0
, u64 fixed1
)
2909 return ((val
& fixed1
) | fixed0
) == val
;
2912 static inline bool vmx_control_verify(u32 control
, u32 low
, u32 high
)
2914 return fixed_bits_valid(control
, low
, high
);
2917 static inline u64
vmx_control_msr(u32 low
, u32 high
)
2919 return low
| ((u64
)high
<< 32);
2922 static bool is_bitwise_subset(u64 superset
, u64 subset
, u64 mask
)
2927 return (superset
| subset
) == superset
;
2930 static int vmx_restore_vmx_basic(struct vcpu_vmx
*vmx
, u64 data
)
2932 const u64 feature_and_reserved
=
2933 /* feature (except bit 48; see below) */
2934 BIT_ULL(49) | BIT_ULL(54) | BIT_ULL(55) |
2936 BIT_ULL(31) | GENMASK_ULL(47, 45) | GENMASK_ULL(63, 56);
2937 u64 vmx_basic
= vmx
->nested
.nested_vmx_basic
;
2939 if (!is_bitwise_subset(vmx_basic
, data
, feature_and_reserved
))
2943 * KVM does not emulate a version of VMX that constrains physical
2944 * addresses of VMX structures (e.g. VMCS) to 32-bits.
2946 if (data
& BIT_ULL(48))
2949 if (vmx_basic_vmcs_revision_id(vmx_basic
) !=
2950 vmx_basic_vmcs_revision_id(data
))
2953 if (vmx_basic_vmcs_size(vmx_basic
) > vmx_basic_vmcs_size(data
))
2956 vmx
->nested
.nested_vmx_basic
= data
;
2961 vmx_restore_control_msr(struct vcpu_vmx
*vmx
, u32 msr_index
, u64 data
)
2966 switch (msr_index
) {
2967 case MSR_IA32_VMX_TRUE_PINBASED_CTLS
:
2968 lowp
= &vmx
->nested
.nested_vmx_pinbased_ctls_low
;
2969 highp
= &vmx
->nested
.nested_vmx_pinbased_ctls_high
;
2971 case MSR_IA32_VMX_TRUE_PROCBASED_CTLS
:
2972 lowp
= &vmx
->nested
.nested_vmx_procbased_ctls_low
;
2973 highp
= &vmx
->nested
.nested_vmx_procbased_ctls_high
;
2975 case MSR_IA32_VMX_TRUE_EXIT_CTLS
:
2976 lowp
= &vmx
->nested
.nested_vmx_exit_ctls_low
;
2977 highp
= &vmx
->nested
.nested_vmx_exit_ctls_high
;
2979 case MSR_IA32_VMX_TRUE_ENTRY_CTLS
:
2980 lowp
= &vmx
->nested
.nested_vmx_entry_ctls_low
;
2981 highp
= &vmx
->nested
.nested_vmx_entry_ctls_high
;
2983 case MSR_IA32_VMX_PROCBASED_CTLS2
:
2984 lowp
= &vmx
->nested
.nested_vmx_secondary_ctls_low
;
2985 highp
= &vmx
->nested
.nested_vmx_secondary_ctls_high
;
2991 supported
= vmx_control_msr(*lowp
, *highp
);
2993 /* Check must-be-1 bits are still 1. */
2994 if (!is_bitwise_subset(data
, supported
, GENMASK_ULL(31, 0)))
2997 /* Check must-be-0 bits are still 0. */
2998 if (!is_bitwise_subset(supported
, data
, GENMASK_ULL(63, 32)))
3002 *highp
= data
>> 32;
3006 static int vmx_restore_vmx_misc(struct vcpu_vmx
*vmx
, u64 data
)
3008 const u64 feature_and_reserved_bits
=
3010 BIT_ULL(5) | GENMASK_ULL(8, 6) | BIT_ULL(14) | BIT_ULL(15) |
3011 BIT_ULL(28) | BIT_ULL(29) | BIT_ULL(30) |
3013 GENMASK_ULL(13, 9) | BIT_ULL(31);
3016 vmx_misc
= vmx_control_msr(vmx
->nested
.nested_vmx_misc_low
,
3017 vmx
->nested
.nested_vmx_misc_high
);
3019 if (!is_bitwise_subset(vmx_misc
, data
, feature_and_reserved_bits
))
3022 if ((vmx
->nested
.nested_vmx_pinbased_ctls_high
&
3023 PIN_BASED_VMX_PREEMPTION_TIMER
) &&
3024 vmx_misc_preemption_timer_rate(data
) !=
3025 vmx_misc_preemption_timer_rate(vmx_misc
))
3028 if (vmx_misc_cr3_count(data
) > vmx_misc_cr3_count(vmx_misc
))
3031 if (vmx_misc_max_msr(data
) > vmx_misc_max_msr(vmx_misc
))
3034 if (vmx_misc_mseg_revid(data
) != vmx_misc_mseg_revid(vmx_misc
))
3037 vmx
->nested
.nested_vmx_misc_low
= data
;
3038 vmx
->nested
.nested_vmx_misc_high
= data
>> 32;
3042 static int vmx_restore_vmx_ept_vpid_cap(struct vcpu_vmx
*vmx
, u64 data
)
3044 u64 vmx_ept_vpid_cap
;
3046 vmx_ept_vpid_cap
= vmx_control_msr(vmx
->nested
.nested_vmx_ept_caps
,
3047 vmx
->nested
.nested_vmx_vpid_caps
);
3049 /* Every bit is either reserved or a feature bit. */
3050 if (!is_bitwise_subset(vmx_ept_vpid_cap
, data
, -1ULL))
3053 vmx
->nested
.nested_vmx_ept_caps
= data
;
3054 vmx
->nested
.nested_vmx_vpid_caps
= data
>> 32;
3058 static int vmx_restore_fixed0_msr(struct vcpu_vmx
*vmx
, u32 msr_index
, u64 data
)
3062 switch (msr_index
) {
3063 case MSR_IA32_VMX_CR0_FIXED0
:
3064 msr
= &vmx
->nested
.nested_vmx_cr0_fixed0
;
3066 case MSR_IA32_VMX_CR4_FIXED0
:
3067 msr
= &vmx
->nested
.nested_vmx_cr4_fixed0
;
3074 * 1 bits (which indicates bits which "must-be-1" during VMX operation)
3075 * must be 1 in the restored value.
3077 if (!is_bitwise_subset(data
, *msr
, -1ULL))
3085 * Called when userspace is restoring VMX MSRs.
3087 * Returns 0 on success, non-0 otherwise.
3089 static int vmx_set_vmx_msr(struct kvm_vcpu
*vcpu
, u32 msr_index
, u64 data
)
3091 struct vcpu_vmx
*vmx
= to_vmx(vcpu
);
3093 switch (msr_index
) {
3094 case MSR_IA32_VMX_BASIC
:
3095 return vmx_restore_vmx_basic(vmx
, data
);
3096 case MSR_IA32_VMX_PINBASED_CTLS
:
3097 case MSR_IA32_VMX_PROCBASED_CTLS
:
3098 case MSR_IA32_VMX_EXIT_CTLS
:
3099 case MSR_IA32_VMX_ENTRY_CTLS
:
3101 * The "non-true" VMX capability MSRs are generated from the
3102 * "true" MSRs, so we do not support restoring them directly.
3104 * If userspace wants to emulate VMX_BASIC[55]=0, userspace
3105 * should restore the "true" MSRs with the must-be-1 bits
3106 * set according to the SDM Vol 3. A.2 "RESERVED CONTROLS AND
3107 * DEFAULT SETTINGS".
3110 case MSR_IA32_VMX_TRUE_PINBASED_CTLS
:
3111 case MSR_IA32_VMX_TRUE_PROCBASED_CTLS
:
3112 case MSR_IA32_VMX_TRUE_EXIT_CTLS
:
3113 case MSR_IA32_VMX_TRUE_ENTRY_CTLS
:
3114 case MSR_IA32_VMX_PROCBASED_CTLS2
:
3115 return vmx_restore_control_msr(vmx
, msr_index
, data
);
3116 case MSR_IA32_VMX_MISC
:
3117 return vmx_restore_vmx_misc(vmx
, data
);
3118 case MSR_IA32_VMX_CR0_FIXED0
:
3119 case MSR_IA32_VMX_CR4_FIXED0
:
3120 return vmx_restore_fixed0_msr(vmx
, msr_index
, data
);
3121 case MSR_IA32_VMX_CR0_FIXED1
:
3122 case MSR_IA32_VMX_CR4_FIXED1
:
3124 * These MSRs are generated based on the vCPU's CPUID, so we
3125 * do not support restoring them directly.
3128 case MSR_IA32_VMX_EPT_VPID_CAP
:
3129 return vmx_restore_vmx_ept_vpid_cap(vmx
, data
);
3130 case MSR_IA32_VMX_VMCS_ENUM
:
3131 vmx
->nested
.nested_vmx_vmcs_enum
= data
;
3135 * The rest of the VMX capability MSRs do not support restore.
3141 /* Returns 0 on success, non-0 otherwise. */
3142 static int vmx_get_vmx_msr(struct kvm_vcpu
*vcpu
, u32 msr_index
, u64
*pdata
)
3144 struct vcpu_vmx
*vmx
= to_vmx(vcpu
);
3146 switch (msr_index
) {
3147 case MSR_IA32_VMX_BASIC
:
3148 *pdata
= vmx
->nested
.nested_vmx_basic
;
3150 case MSR_IA32_VMX_TRUE_PINBASED_CTLS
:
3151 case MSR_IA32_VMX_PINBASED_CTLS
:
3152 *pdata
= vmx_control_msr(
3153 vmx
->nested
.nested_vmx_pinbased_ctls_low
,
3154 vmx
->nested
.nested_vmx_pinbased_ctls_high
);
3155 if (msr_index
== MSR_IA32_VMX_PINBASED_CTLS
)
3156 *pdata
|= PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR
;
3158 case MSR_IA32_VMX_TRUE_PROCBASED_CTLS
:
3159 case MSR_IA32_VMX_PROCBASED_CTLS
:
3160 *pdata
= vmx_control_msr(
3161 vmx
->nested
.nested_vmx_procbased_ctls_low
,
3162 vmx
->nested
.nested_vmx_procbased_ctls_high
);
3163 if (msr_index
== MSR_IA32_VMX_PROCBASED_CTLS
)
3164 *pdata
|= CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR
;
3166 case MSR_IA32_VMX_TRUE_EXIT_CTLS
:
3167 case MSR_IA32_VMX_EXIT_CTLS
:
3168 *pdata
= vmx_control_msr(
3169 vmx
->nested
.nested_vmx_exit_ctls_low
,
3170 vmx
->nested
.nested_vmx_exit_ctls_high
);
3171 if (msr_index
== MSR_IA32_VMX_EXIT_CTLS
)
3172 *pdata
|= VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR
;
3174 case MSR_IA32_VMX_TRUE_ENTRY_CTLS
:
3175 case MSR_IA32_VMX_ENTRY_CTLS
:
3176 *pdata
= vmx_control_msr(
3177 vmx
->nested
.nested_vmx_entry_ctls_low
,
3178 vmx
->nested
.nested_vmx_entry_ctls_high
);
3179 if (msr_index
== MSR_IA32_VMX_ENTRY_CTLS
)
3180 *pdata
|= VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR
;
3182 case MSR_IA32_VMX_MISC
:
3183 *pdata
= vmx_control_msr(
3184 vmx
->nested
.nested_vmx_misc_low
,
3185 vmx
->nested
.nested_vmx_misc_high
);
3187 case MSR_IA32_VMX_CR0_FIXED0
:
3188 *pdata
= vmx
->nested
.nested_vmx_cr0_fixed0
;
3190 case MSR_IA32_VMX_CR0_FIXED1
:
3191 *pdata
= vmx
->nested
.nested_vmx_cr0_fixed1
;
3193 case MSR_IA32_VMX_CR4_FIXED0
:
3194 *pdata
= vmx
->nested
.nested_vmx_cr4_fixed0
;
3196 case MSR_IA32_VMX_CR4_FIXED1
:
3197 *pdata
= vmx
->nested
.nested_vmx_cr4_fixed1
;
3199 case MSR_IA32_VMX_VMCS_ENUM
:
3200 *pdata
= vmx
->nested
.nested_vmx_vmcs_enum
;
3202 case MSR_IA32_VMX_PROCBASED_CTLS2
:
3203 *pdata
= vmx_control_msr(
3204 vmx
->nested
.nested_vmx_secondary_ctls_low
,
3205 vmx
->nested
.nested_vmx_secondary_ctls_high
);
3207 case MSR_IA32_VMX_EPT_VPID_CAP
:
3208 *pdata
= vmx
->nested
.nested_vmx_ept_caps
|
3209 ((u64
)vmx
->nested
.nested_vmx_vpid_caps
<< 32);
3211 case MSR_IA32_VMX_VMFUNC
:
3212 *pdata
= vmx
->nested
.nested_vmx_vmfunc_controls
;
3221 static inline bool vmx_feature_control_msr_valid(struct kvm_vcpu
*vcpu
,
3224 uint64_t valid_bits
= to_vmx(vcpu
)->msr_ia32_feature_control_valid_bits
;
3226 return !(val
& ~valid_bits
);
3230 * Reads an msr value (of 'msr_index') into 'pdata'.
3231 * Returns 0 on success, non-0 otherwise.
3232 * Assumes vcpu_load() was already called.
3234 static int vmx_get_msr(struct kvm_vcpu
*vcpu
, struct msr_data
*msr_info
)
3236 struct vcpu_vmx
*vmx
= to_vmx(vcpu
);
3237 struct shared_msr_entry
*msr
;
3239 switch (msr_info
->index
) {
3240 #ifdef CONFIG_X86_64
3242 msr_info
->data
= vmcs_readl(GUEST_FS_BASE
);
3245 msr_info
->data
= vmcs_readl(GUEST_GS_BASE
);
3247 case MSR_KERNEL_GS_BASE
:
3248 vmx_load_host_state(vmx
);
3249 msr_info
->data
= vmx
->msr_guest_kernel_gs_base
;
3253 return kvm_get_msr_common(vcpu
, msr_info
);
3255 msr_info
->data
= guest_read_tsc(vcpu
);
3257 case MSR_IA32_SPEC_CTRL
:
3258 if (!msr_info
->host_initiated
&&
3259 !guest_cpuid_has(vcpu
, X86_FEATURE_IBRS
) &&
3260 !guest_cpuid_has(vcpu
, X86_FEATURE_SPEC_CTRL
))
3263 msr_info
->data
= to_vmx(vcpu
)->spec_ctrl
;
3265 case MSR_IA32_ARCH_CAPABILITIES
:
3266 if (!msr_info
->host_initiated
&&
3267 !guest_cpuid_has(vcpu
, X86_FEATURE_ARCH_CAPABILITIES
))
3269 msr_info
->data
= to_vmx(vcpu
)->arch_capabilities
;
3271 case MSR_IA32_SYSENTER_CS
:
3272 msr_info
->data
= vmcs_read32(GUEST_SYSENTER_CS
);
3274 case MSR_IA32_SYSENTER_EIP
:
3275 msr_info
->data
= vmcs_readl(GUEST_SYSENTER_EIP
);
3277 case MSR_IA32_SYSENTER_ESP
:
3278 msr_info
->data
= vmcs_readl(GUEST_SYSENTER_ESP
);
3280 case MSR_IA32_BNDCFGS
:
3281 if (!kvm_mpx_supported() ||
3282 (!msr_info
->host_initiated
&&
3283 !guest_cpuid_has(vcpu
, X86_FEATURE_MPX
)))
3285 msr_info
->data
= vmcs_read64(GUEST_BNDCFGS
);
3287 case MSR_IA32_MCG_EXT_CTL
:
3288 if (!msr_info
->host_initiated
&&
3289 !(vmx
->msr_ia32_feature_control
&
3290 FEATURE_CONTROL_LMCE
))
3292 msr_info
->data
= vcpu
->arch
.mcg_ext_ctl
;
3294 case MSR_IA32_FEATURE_CONTROL
:
3295 msr_info
->data
= vmx
->msr_ia32_feature_control
;
3297 case MSR_IA32_VMX_BASIC
... MSR_IA32_VMX_VMFUNC
:
3298 if (!nested_vmx_allowed(vcpu
))
3300 return vmx_get_vmx_msr(vcpu
, msr_info
->index
, &msr_info
->data
);
3302 if (!vmx_xsaves_supported())
3304 msr_info
->data
= vcpu
->arch
.ia32_xss
;
3307 if (!msr_info
->host_initiated
&&
3308 !guest_cpuid_has(vcpu
, X86_FEATURE_RDTSCP
))
3310 /* Otherwise falls through */
3312 msr
= find_msr_entry(vmx
, msr_info
->index
);
3314 msr_info
->data
= msr
->data
;
3317 return kvm_get_msr_common(vcpu
, msr_info
);
3323 static void vmx_leave_nested(struct kvm_vcpu
*vcpu
);
3326 * Writes msr value into into the appropriate "register".
3327 * Returns 0 on success, non-0 otherwise.
3328 * Assumes vcpu_load() was already called.
3330 static int vmx_set_msr(struct kvm_vcpu
*vcpu
, struct msr_data
*msr_info
)
3332 struct vcpu_vmx
*vmx
= to_vmx(vcpu
);
3333 struct shared_msr_entry
*msr
;
3335 u32 msr_index
= msr_info
->index
;
3336 u64 data
= msr_info
->data
;
3338 switch (msr_index
) {
3340 ret
= kvm_set_msr_common(vcpu
, msr_info
);
3342 #ifdef CONFIG_X86_64
3344 vmx_segment_cache_clear(vmx
);
3345 vmcs_writel(GUEST_FS_BASE
, data
);
3348 vmx_segment_cache_clear(vmx
);
3349 vmcs_writel(GUEST_GS_BASE
, data
);
3351 case MSR_KERNEL_GS_BASE
:
3352 vmx_load_host_state(vmx
);
3353 vmx
->msr_guest_kernel_gs_base
= data
;
3356 case MSR_IA32_SYSENTER_CS
:
3357 vmcs_write32(GUEST_SYSENTER_CS
, data
);
3359 case MSR_IA32_SYSENTER_EIP
:
3360 vmcs_writel(GUEST_SYSENTER_EIP
, data
);
3362 case MSR_IA32_SYSENTER_ESP
:
3363 vmcs_writel(GUEST_SYSENTER_ESP
, data
);
3365 case MSR_IA32_BNDCFGS
:
3366 if (!kvm_mpx_supported() ||
3367 (!msr_info
->host_initiated
&&
3368 !guest_cpuid_has(vcpu
, X86_FEATURE_MPX
)))
3370 if (is_noncanonical_address(data
& PAGE_MASK
, vcpu
) ||
3371 (data
& MSR_IA32_BNDCFGS_RSVD
))
3373 vmcs_write64(GUEST_BNDCFGS
, data
);
3376 kvm_write_tsc(vcpu
, msr_info
);
3378 case MSR_IA32_SPEC_CTRL
:
3379 if (!msr_info
->host_initiated
&&
3380 !guest_cpuid_has(vcpu
, X86_FEATURE_IBRS
) &&
3381 !guest_cpuid_has(vcpu
, X86_FEATURE_SPEC_CTRL
))
3384 /* The STIBP bit doesn't fault even if it's not advertised */
3385 if (data
& ~(SPEC_CTRL_IBRS
| SPEC_CTRL_STIBP
))
3388 vmx
->spec_ctrl
= data
;
3395 * When it's written (to non-zero) for the first time, pass
3399 * The handling of the MSR bitmap for L2 guests is done in
3400 * nested_vmx_merge_msr_bitmap. We should not touch the
3401 * vmcs02.msr_bitmap here since it gets completely overwritten
3402 * in the merging. We update the vmcs01 here for L1 as well
3403 * since it will end up touching the MSR anyway now.
3405 vmx_disable_intercept_for_msr(vmx
->vmcs01
.msr_bitmap
,
3409 case MSR_IA32_PRED_CMD
:
3410 if (!msr_info
->host_initiated
&&
3411 !guest_cpuid_has(vcpu
, X86_FEATURE_IBPB
) &&
3412 !guest_cpuid_has(vcpu
, X86_FEATURE_SPEC_CTRL
))
3415 if (data
& ~PRED_CMD_IBPB
)
3421 wrmsrl(MSR_IA32_PRED_CMD
, PRED_CMD_IBPB
);
3425 * When it's written (to non-zero) for the first time, pass
3429 * The handling of the MSR bitmap for L2 guests is done in
3430 * nested_vmx_merge_msr_bitmap. We should not touch the
3431 * vmcs02.msr_bitmap here since it gets completely overwritten
3434 vmx_disable_intercept_for_msr(vmx
->vmcs01
.msr_bitmap
, MSR_IA32_PRED_CMD
,
3437 case MSR_IA32_ARCH_CAPABILITIES
:
3438 if (!msr_info
->host_initiated
)
3440 vmx
->arch_capabilities
= data
;
3442 case MSR_IA32_CR_PAT
:
3443 if (vmcs_config
.vmentry_ctrl
& VM_ENTRY_LOAD_IA32_PAT
) {
3444 if (!kvm_mtrr_valid(vcpu
, MSR_IA32_CR_PAT
, data
))
3446 vmcs_write64(GUEST_IA32_PAT
, data
);
3447 vcpu
->arch
.pat
= data
;
3450 ret
= kvm_set_msr_common(vcpu
, msr_info
);
3452 case MSR_IA32_TSC_ADJUST
:
3453 ret
= kvm_set_msr_common(vcpu
, msr_info
);
3455 case MSR_IA32_MCG_EXT_CTL
:
3456 if ((!msr_info
->host_initiated
&&
3457 !(to_vmx(vcpu
)->msr_ia32_feature_control
&
3458 FEATURE_CONTROL_LMCE
)) ||
3459 (data
& ~MCG_EXT_CTL_LMCE_EN
))
3461 vcpu
->arch
.mcg_ext_ctl
= data
;
3463 case MSR_IA32_FEATURE_CONTROL
:
3464 if (!vmx_feature_control_msr_valid(vcpu
, data
) ||
3465 (to_vmx(vcpu
)->msr_ia32_feature_control
&
3466 FEATURE_CONTROL_LOCKED
&& !msr_info
->host_initiated
))
3468 vmx
->msr_ia32_feature_control
= data
;
3469 if (msr_info
->host_initiated
&& data
== 0)
3470 vmx_leave_nested(vcpu
);
3472 case MSR_IA32_VMX_BASIC
... MSR_IA32_VMX_VMFUNC
:
3473 if (!msr_info
->host_initiated
)
3474 return 1; /* they are read-only */
3475 if (!nested_vmx_allowed(vcpu
))
3477 return vmx_set_vmx_msr(vcpu
, msr_index
, data
);
3479 if (!vmx_xsaves_supported())
3482 * The only supported bit as of Skylake is bit 8, but
3483 * it is not supported on KVM.
3487 vcpu
->arch
.ia32_xss
= data
;
3488 if (vcpu
->arch
.ia32_xss
!= host_xss
)
3489 add_atomic_switch_msr(vmx
, MSR_IA32_XSS
,
3490 vcpu
->arch
.ia32_xss
, host_xss
);
3492 clear_atomic_switch_msr(vmx
, MSR_IA32_XSS
);
3495 if (!msr_info
->host_initiated
&&
3496 !guest_cpuid_has(vcpu
, X86_FEATURE_RDTSCP
))
3498 /* Check reserved bit, higher 32 bits should be zero */
3499 if ((data
>> 32) != 0)
3501 /* Otherwise falls through */
3503 msr
= find_msr_entry(vmx
, msr_index
);
3505 u64 old_msr_data
= msr
->data
;
3507 if (msr
- vmx
->guest_msrs
< vmx
->save_nmsrs
) {
3509 ret
= kvm_set_shared_msr(msr
->index
, msr
->data
,
3513 msr
->data
= old_msr_data
;
3517 ret
= kvm_set_msr_common(vcpu
, msr_info
);
3523 static void vmx_cache_reg(struct kvm_vcpu
*vcpu
, enum kvm_reg reg
)
3525 __set_bit(reg
, (unsigned long *)&vcpu
->arch
.regs_avail
);
3528 vcpu
->arch
.regs
[VCPU_REGS_RSP
] = vmcs_readl(GUEST_RSP
);
3531 vcpu
->arch
.regs
[VCPU_REGS_RIP
] = vmcs_readl(GUEST_RIP
);
3533 case VCPU_EXREG_PDPTR
:
3535 ept_save_pdptrs(vcpu
);
3542 static __init
int cpu_has_kvm_support(void)
3544 return cpu_has_vmx();
3547 static __init
int vmx_disabled_by_bios(void)
3551 rdmsrl(MSR_IA32_FEATURE_CONTROL
, msr
);
3552 if (msr
& FEATURE_CONTROL_LOCKED
) {
3553 /* launched w/ TXT and VMX disabled */
3554 if (!(msr
& FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX
)
3557 /* launched w/o TXT and VMX only enabled w/ TXT */
3558 if (!(msr
& FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX
)
3559 && (msr
& FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX
)
3560 && !tboot_enabled()) {
3561 printk(KERN_WARNING
"kvm: disable TXT in the BIOS or "
3562 "activate TXT before enabling KVM\n");
3565 /* launched w/o TXT and VMX disabled */
3566 if (!(msr
& FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX
)
3567 && !tboot_enabled())
3574 static void kvm_cpu_vmxon(u64 addr
)
3576 cr4_set_bits(X86_CR4_VMXE
);
3577 intel_pt_handle_vmx(1);
3579 asm volatile (ASM_VMX_VMXON_RAX
3580 : : "a"(&addr
), "m"(addr
)
3584 static int hardware_enable(void)
3586 int cpu
= raw_smp_processor_id();
3587 u64 phys_addr
= __pa(per_cpu(vmxarea
, cpu
));
3590 if (cr4_read_shadow() & X86_CR4_VMXE
)
3593 INIT_LIST_HEAD(&per_cpu(loaded_vmcss_on_cpu
, cpu
));
3594 INIT_LIST_HEAD(&per_cpu(blocked_vcpu_on_cpu
, cpu
));
3595 spin_lock_init(&per_cpu(blocked_vcpu_on_cpu_lock
, cpu
));
3598 * Now we can enable the vmclear operation in kdump
3599 * since the loaded_vmcss_on_cpu list on this cpu
3600 * has been initialized.
3602 * Though the cpu is not in VMX operation now, there
3603 * is no problem to enable the vmclear operation
3604 * for the loaded_vmcss_on_cpu list is empty!
3606 crash_enable_local_vmclear(cpu
);
3608 rdmsrl(MSR_IA32_FEATURE_CONTROL
, old
);
3610 test_bits
= FEATURE_CONTROL_LOCKED
;
3611 test_bits
|= FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX
;
3612 if (tboot_enabled())
3613 test_bits
|= FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX
;
3615 if ((old
& test_bits
) != test_bits
) {
3616 /* enable and lock */
3617 wrmsrl(MSR_IA32_FEATURE_CONTROL
, old
| test_bits
);
3619 kvm_cpu_vmxon(phys_addr
);
3626 static void vmclear_local_loaded_vmcss(void)
3628 int cpu
= raw_smp_processor_id();
3629 struct loaded_vmcs
*v
, *n
;
3631 list_for_each_entry_safe(v
, n
, &per_cpu(loaded_vmcss_on_cpu
, cpu
),
3632 loaded_vmcss_on_cpu_link
)
3633 __loaded_vmcs_clear(v
);
3637 /* Just like cpu_vmxoff(), but with the __kvm_handle_fault_on_reboot()
3640 static void kvm_cpu_vmxoff(void)
3642 asm volatile (__ex(ASM_VMX_VMXOFF
) : : : "cc");
3644 intel_pt_handle_vmx(0);
3645 cr4_clear_bits(X86_CR4_VMXE
);
3648 static void hardware_disable(void)
3650 vmclear_local_loaded_vmcss();
3654 static __init
int adjust_vmx_controls(u32 ctl_min
, u32 ctl_opt
,
3655 u32 msr
, u32
*result
)
3657 u32 vmx_msr_low
, vmx_msr_high
;
3658 u32 ctl
= ctl_min
| ctl_opt
;
3660 rdmsr(msr
, vmx_msr_low
, vmx_msr_high
);
3662 ctl
&= vmx_msr_high
; /* bit == 0 in high word ==> must be zero */
3663 ctl
|= vmx_msr_low
; /* bit == 1 in low word ==> must be one */
3665 /* Ensure minimum (required) set of control bits are supported. */
3673 static __init
bool allow_1_setting(u32 msr
, u32 ctl
)
3675 u32 vmx_msr_low
, vmx_msr_high
;
3677 rdmsr(msr
, vmx_msr_low
, vmx_msr_high
);
3678 return vmx_msr_high
& ctl
;
3681 static __init
int setup_vmcs_config(struct vmcs_config
*vmcs_conf
)
3683 u32 vmx_msr_low
, vmx_msr_high
;
3684 u32 min
, opt
, min2
, opt2
;
3685 u32 _pin_based_exec_control
= 0;
3686 u32 _cpu_based_exec_control
= 0;
3687 u32 _cpu_based_2nd_exec_control
= 0;
3688 u32 _vmexit_control
= 0;
3689 u32 _vmentry_control
= 0;
3691 min
= CPU_BASED_HLT_EXITING
|
3692 #ifdef CONFIG_X86_64
3693 CPU_BASED_CR8_LOAD_EXITING
|
3694 CPU_BASED_CR8_STORE_EXITING
|
3696 CPU_BASED_CR3_LOAD_EXITING
|
3697 CPU_BASED_CR3_STORE_EXITING
|
3698 CPU_BASED_UNCOND_IO_EXITING
|
3699 CPU_BASED_MOV_DR_EXITING
|
3700 CPU_BASED_USE_TSC_OFFSETING
|
3701 CPU_BASED_INVLPG_EXITING
|
3702 CPU_BASED_RDPMC_EXITING
;
3704 if (!kvm_mwait_in_guest())
3705 min
|= CPU_BASED_MWAIT_EXITING
|
3706 CPU_BASED_MONITOR_EXITING
;
3708 opt
= CPU_BASED_TPR_SHADOW
|
3709 CPU_BASED_USE_MSR_BITMAPS
|
3710 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS
;
3711 if (adjust_vmx_controls(min
, opt
, MSR_IA32_VMX_PROCBASED_CTLS
,
3712 &_cpu_based_exec_control
) < 0)
3714 #ifdef CONFIG_X86_64
3715 if ((_cpu_based_exec_control
& CPU_BASED_TPR_SHADOW
))
3716 _cpu_based_exec_control
&= ~CPU_BASED_CR8_LOAD_EXITING
&
3717 ~CPU_BASED_CR8_STORE_EXITING
;
3719 if (_cpu_based_exec_control
& CPU_BASED_ACTIVATE_SECONDARY_CONTROLS
) {
3721 opt2
= SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES
|
3722 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE
|
3723 SECONDARY_EXEC_WBINVD_EXITING
|
3724 SECONDARY_EXEC_ENABLE_VPID
|
3725 SECONDARY_EXEC_ENABLE_EPT
|
3726 SECONDARY_EXEC_UNRESTRICTED_GUEST
|
3727 SECONDARY_EXEC_PAUSE_LOOP_EXITING
|
3728 SECONDARY_EXEC_DESC
|
3729 SECONDARY_EXEC_RDTSCP
|
3730 SECONDARY_EXEC_ENABLE_INVPCID
|
3731 SECONDARY_EXEC_APIC_REGISTER_VIRT
|
3732 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY
|
3733 SECONDARY_EXEC_SHADOW_VMCS
|
3734 SECONDARY_EXEC_XSAVES
|
3735 SECONDARY_EXEC_RDSEED_EXITING
|
3736 SECONDARY_EXEC_RDRAND_EXITING
|
3737 SECONDARY_EXEC_ENABLE_PML
|
3738 SECONDARY_EXEC_TSC_SCALING
|
3739 SECONDARY_EXEC_ENABLE_VMFUNC
;
3740 if (adjust_vmx_controls(min2
, opt2
,
3741 MSR_IA32_VMX_PROCBASED_CTLS2
,
3742 &_cpu_based_2nd_exec_control
) < 0)
3745 #ifndef CONFIG_X86_64
3746 if (!(_cpu_based_2nd_exec_control
&
3747 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES
))
3748 _cpu_based_exec_control
&= ~CPU_BASED_TPR_SHADOW
;
3751 if (!(_cpu_based_exec_control
& CPU_BASED_TPR_SHADOW
))
3752 _cpu_based_2nd_exec_control
&= ~(
3753 SECONDARY_EXEC_APIC_REGISTER_VIRT
|
3754 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE
|
3755 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY
);
3757 rdmsr_safe(MSR_IA32_VMX_EPT_VPID_CAP
,
3758 &vmx_capability
.ept
, &vmx_capability
.vpid
);
3760 if (_cpu_based_2nd_exec_control
& SECONDARY_EXEC_ENABLE_EPT
) {
3761 /* CR3 accesses and invlpg don't need to cause VM Exits when EPT
3763 _cpu_based_exec_control
&= ~(CPU_BASED_CR3_LOAD_EXITING
|
3764 CPU_BASED_CR3_STORE_EXITING
|
3765 CPU_BASED_INVLPG_EXITING
);
3766 } else if (vmx_capability
.ept
) {
3767 vmx_capability
.ept
= 0;
3768 pr_warn_once("EPT CAP should not exist if not support "
3769 "1-setting enable EPT VM-execution control\n");
3771 if (!(_cpu_based_2nd_exec_control
& SECONDARY_EXEC_ENABLE_VPID
) &&
3772 vmx_capability
.vpid
) {
3773 vmx_capability
.vpid
= 0;
3774 pr_warn_once("VPID CAP should not exist if not support "
3775 "1-setting enable VPID VM-execution control\n");
3778 min
= VM_EXIT_SAVE_DEBUG_CONTROLS
| VM_EXIT_ACK_INTR_ON_EXIT
;
3779 #ifdef CONFIG_X86_64
3780 min
|= VM_EXIT_HOST_ADDR_SPACE_SIZE
;
3782 opt
= VM_EXIT_SAVE_IA32_PAT
| VM_EXIT_LOAD_IA32_PAT
|
3783 VM_EXIT_CLEAR_BNDCFGS
;
3784 if (adjust_vmx_controls(min
, opt
, MSR_IA32_VMX_EXIT_CTLS
,
3785 &_vmexit_control
) < 0)
3788 min
= PIN_BASED_EXT_INTR_MASK
| PIN_BASED_NMI_EXITING
;
3789 opt
= PIN_BASED_VIRTUAL_NMIS
| PIN_BASED_POSTED_INTR
|
3790 PIN_BASED_VMX_PREEMPTION_TIMER
;
3791 if (adjust_vmx_controls(min
, opt
, MSR_IA32_VMX_PINBASED_CTLS
,
3792 &_pin_based_exec_control
) < 0)
3795 if (cpu_has_broken_vmx_preemption_timer())
3796 _pin_based_exec_control
&= ~PIN_BASED_VMX_PREEMPTION_TIMER
;
3797 if (!(_cpu_based_2nd_exec_control
&
3798 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY
))
3799 _pin_based_exec_control
&= ~PIN_BASED_POSTED_INTR
;
3801 min
= VM_ENTRY_LOAD_DEBUG_CONTROLS
;
3802 opt
= VM_ENTRY_LOAD_IA32_PAT
| VM_ENTRY_LOAD_BNDCFGS
;
3803 if (adjust_vmx_controls(min
, opt
, MSR_IA32_VMX_ENTRY_CTLS
,
3804 &_vmentry_control
) < 0)
3807 rdmsr(MSR_IA32_VMX_BASIC
, vmx_msr_low
, vmx_msr_high
);
3809 /* IA-32 SDM Vol 3B: VMCS size is never greater than 4kB. */
3810 if ((vmx_msr_high
& 0x1fff) > PAGE_SIZE
)
3813 #ifdef CONFIG_X86_64
3814 /* IA-32 SDM Vol 3B: 64-bit CPUs always have VMX_BASIC_MSR[48]==0. */
3815 if (vmx_msr_high
& (1u<<16))
3819 /* Require Write-Back (WB) memory type for VMCS accesses. */
3820 if (((vmx_msr_high
>> 18) & 15) != 6)
3823 vmcs_conf
->size
= vmx_msr_high
& 0x1fff;
3824 vmcs_conf
->order
= get_order(vmcs_conf
->size
);
3825 vmcs_conf
->basic_cap
= vmx_msr_high
& ~0x1fff;
3826 vmcs_conf
->revision_id
= vmx_msr_low
;
3828 vmcs_conf
->pin_based_exec_ctrl
= _pin_based_exec_control
;
3829 vmcs_conf
->cpu_based_exec_ctrl
= _cpu_based_exec_control
;
3830 vmcs_conf
->cpu_based_2nd_exec_ctrl
= _cpu_based_2nd_exec_control
;
3831 vmcs_conf
->vmexit_ctrl
= _vmexit_control
;
3832 vmcs_conf
->vmentry_ctrl
= _vmentry_control
;
3834 cpu_has_load_ia32_efer
=
3835 allow_1_setting(MSR_IA32_VMX_ENTRY_CTLS
,
3836 VM_ENTRY_LOAD_IA32_EFER
)
3837 && allow_1_setting(MSR_IA32_VMX_EXIT_CTLS
,
3838 VM_EXIT_LOAD_IA32_EFER
);
3840 cpu_has_load_perf_global_ctrl
=
3841 allow_1_setting(MSR_IA32_VMX_ENTRY_CTLS
,
3842 VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL
)
3843 && allow_1_setting(MSR_IA32_VMX_EXIT_CTLS
,
3844 VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL
);
3847 * Some cpus support VM_ENTRY_(LOAD|SAVE)_IA32_PERF_GLOBAL_CTRL
3848 * but due to errata below it can't be used. Workaround is to use
3849 * msr load mechanism to switch IA32_PERF_GLOBAL_CTRL.
3851 * VM Exit May Incorrectly Clear IA32_PERF_GLOBAL_CTRL [34:32]
3856 * BC86,AAY89,BD102 (model 44)
3860 if (cpu_has_load_perf_global_ctrl
&& boot_cpu_data
.x86
== 0x6) {
3861 switch (boot_cpu_data
.x86_model
) {
3867 cpu_has_load_perf_global_ctrl
= false;
3868 printk_once(KERN_WARNING
"kvm: VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL "
3869 "does not work properly. Using workaround\n");
3876 if (boot_cpu_has(X86_FEATURE_XSAVES
))
3877 rdmsrl(MSR_IA32_XSS
, host_xss
);
3882 static struct vmcs
*alloc_vmcs_cpu(int cpu
)
3884 int node
= cpu_to_node(cpu
);
3888 pages
= __alloc_pages_node(node
, GFP_KERNEL
, vmcs_config
.order
);
3891 vmcs
= page_address(pages
);
3892 memset(vmcs
, 0, vmcs_config
.size
);
3893 vmcs
->revision_id
= vmcs_config
.revision_id
; /* vmcs revision id */
3897 static void free_vmcs(struct vmcs
*vmcs
)
3899 free_pages((unsigned long)vmcs
, vmcs_config
.order
);
3903 * Free a VMCS, but before that VMCLEAR it on the CPU where it was last loaded
3905 static void free_loaded_vmcs(struct loaded_vmcs
*loaded_vmcs
)
3907 if (!loaded_vmcs
->vmcs
)
3909 loaded_vmcs_clear(loaded_vmcs
);
3910 free_vmcs(loaded_vmcs
->vmcs
);
3911 loaded_vmcs
->vmcs
= NULL
;
3912 if (loaded_vmcs
->msr_bitmap
)
3913 free_page((unsigned long)loaded_vmcs
->msr_bitmap
);
3914 WARN_ON(loaded_vmcs
->shadow_vmcs
!= NULL
);
3917 static struct vmcs
*alloc_vmcs(void)
3919 return alloc_vmcs_cpu(raw_smp_processor_id());
3922 static int alloc_loaded_vmcs(struct loaded_vmcs
*loaded_vmcs
)
3924 loaded_vmcs
->vmcs
= alloc_vmcs();
3925 if (!loaded_vmcs
->vmcs
)
3928 loaded_vmcs
->shadow_vmcs
= NULL
;
3929 loaded_vmcs_init(loaded_vmcs
);
3931 if (cpu_has_vmx_msr_bitmap()) {
3932 loaded_vmcs
->msr_bitmap
= (unsigned long *)__get_free_page(GFP_KERNEL
);
3933 if (!loaded_vmcs
->msr_bitmap
)
3935 memset(loaded_vmcs
->msr_bitmap
, 0xff, PAGE_SIZE
);
3940 free_loaded_vmcs(loaded_vmcs
);
3944 static void free_kvm_area(void)
3948 for_each_possible_cpu(cpu
) {
3949 free_vmcs(per_cpu(vmxarea
, cpu
));
3950 per_cpu(vmxarea
, cpu
) = NULL
;
3954 enum vmcs_field_width
{
3955 VMCS_FIELD_WIDTH_U16
= 0,
3956 VMCS_FIELD_WIDTH_U64
= 1,
3957 VMCS_FIELD_WIDTH_U32
= 2,
3958 VMCS_FIELD_WIDTH_NATURAL_WIDTH
= 3
3961 static inline int vmcs_field_width(unsigned long field
)
3963 if (0x1 & field
) /* the *_HIGH fields are all 32 bit */
3964 return VMCS_FIELD_WIDTH_U32
;
3965 return (field
>> 13) & 0x3 ;
3968 static inline int vmcs_field_readonly(unsigned long field
)
3970 return (((field
>> 10) & 0x3) == 1);
3973 static void init_vmcs_shadow_fields(void)
3977 for (i
= j
= 0; i
< max_shadow_read_only_fields
; i
++) {
3978 u16 field
= shadow_read_only_fields
[i
];
3979 if (vmcs_field_width(field
) == VMCS_FIELD_WIDTH_U64
&&
3980 (i
+ 1 == max_shadow_read_only_fields
||
3981 shadow_read_only_fields
[i
+ 1] != field
+ 1))
3982 pr_err("Missing field from shadow_read_only_field %x\n",
3985 clear_bit(field
, vmx_vmread_bitmap
);
3986 #ifdef CONFIG_X86_64
3991 shadow_read_only_fields
[j
] = field
;
3994 max_shadow_read_only_fields
= j
;
3996 for (i
= j
= 0; i
< max_shadow_read_write_fields
; i
++) {
3997 u16 field
= shadow_read_write_fields
[i
];
3998 if (vmcs_field_width(field
) == VMCS_FIELD_WIDTH_U64
&&
3999 (i
+ 1 == max_shadow_read_write_fields
||
4000 shadow_read_write_fields
[i
+ 1] != field
+ 1))
4001 pr_err("Missing field from shadow_read_write_field %x\n",
4005 * PML and the preemption timer can be emulated, but the
4006 * processor cannot vmwrite to fields that don't exist
4010 case GUEST_PML_INDEX
:
4011 if (!cpu_has_vmx_pml())
4014 case VMX_PREEMPTION_TIMER_VALUE
:
4015 if (!cpu_has_vmx_preemption_timer())
4018 case GUEST_INTR_STATUS
:
4019 if (!cpu_has_vmx_apicv())
4026 clear_bit(field
, vmx_vmwrite_bitmap
);
4027 clear_bit(field
, vmx_vmread_bitmap
);
4028 #ifdef CONFIG_X86_64
4033 shadow_read_write_fields
[j
] = field
;
4036 max_shadow_read_write_fields
= j
;
4039 static __init
int alloc_kvm_area(void)
4043 for_each_possible_cpu(cpu
) {
4046 vmcs
= alloc_vmcs_cpu(cpu
);
4052 per_cpu(vmxarea
, cpu
) = vmcs
;
4057 static void fix_pmode_seg(struct kvm_vcpu
*vcpu
, int seg
,
4058 struct kvm_segment
*save
)
4060 if (!emulate_invalid_guest_state
) {
4062 * CS and SS RPL should be equal during guest entry according
4063 * to VMX spec, but in reality it is not always so. Since vcpu
4064 * is in the middle of the transition from real mode to
4065 * protected mode it is safe to assume that RPL 0 is a good
4068 if (seg
== VCPU_SREG_CS
|| seg
== VCPU_SREG_SS
)
4069 save
->selector
&= ~SEGMENT_RPL_MASK
;
4070 save
->dpl
= save
->selector
& SEGMENT_RPL_MASK
;
4073 vmx_set_segment(vcpu
, save
, seg
);
4076 static void enter_pmode(struct kvm_vcpu
*vcpu
)
4078 unsigned long flags
;
4079 struct vcpu_vmx
*vmx
= to_vmx(vcpu
);
4082 * Update real mode segment cache. It may be not up-to-date if sement
4083 * register was written while vcpu was in a guest mode.
4085 vmx_get_segment(vcpu
, &vmx
->rmode
.segs
[VCPU_SREG_ES
], VCPU_SREG_ES
);
4086 vmx_get_segment(vcpu
, &vmx
->rmode
.segs
[VCPU_SREG_DS
], VCPU_SREG_DS
);
4087 vmx_get_segment(vcpu
, &vmx
->rmode
.segs
[VCPU_SREG_FS
], VCPU_SREG_FS
);
4088 vmx_get_segment(vcpu
, &vmx
->rmode
.segs
[VCPU_SREG_GS
], VCPU_SREG_GS
);
4089 vmx_get_segment(vcpu
, &vmx
->rmode
.segs
[VCPU_SREG_SS
], VCPU_SREG_SS
);
4090 vmx_get_segment(vcpu
, &vmx
->rmode
.segs
[VCPU_SREG_CS
], VCPU_SREG_CS
);
4092 vmx
->rmode
.vm86_active
= 0;
4094 vmx_segment_cache_clear(vmx
);
4096 vmx_set_segment(vcpu
, &vmx
->rmode
.segs
[VCPU_SREG_TR
], VCPU_SREG_TR
);
4098 flags
= vmcs_readl(GUEST_RFLAGS
);
4099 flags
&= RMODE_GUEST_OWNED_EFLAGS_BITS
;
4100 flags
|= vmx
->rmode
.save_rflags
& ~RMODE_GUEST_OWNED_EFLAGS_BITS
;
4101 vmcs_writel(GUEST_RFLAGS
, flags
);
4103 vmcs_writel(GUEST_CR4
, (vmcs_readl(GUEST_CR4
) & ~X86_CR4_VME
) |
4104 (vmcs_readl(CR4_READ_SHADOW
) & X86_CR4_VME
));
4106 update_exception_bitmap(vcpu
);
4108 fix_pmode_seg(vcpu
, VCPU_SREG_CS
, &vmx
->rmode
.segs
[VCPU_SREG_CS
]);
4109 fix_pmode_seg(vcpu
, VCPU_SREG_SS
, &vmx
->rmode
.segs
[VCPU_SREG_SS
]);
4110 fix_pmode_seg(vcpu
, VCPU_SREG_ES
, &vmx
->rmode
.segs
[VCPU_SREG_ES
]);
4111 fix_pmode_seg(vcpu
, VCPU_SREG_DS
, &vmx
->rmode
.segs
[VCPU_SREG_DS
]);
4112 fix_pmode_seg(vcpu
, VCPU_SREG_FS
, &vmx
->rmode
.segs
[VCPU_SREG_FS
]);
4113 fix_pmode_seg(vcpu
, VCPU_SREG_GS
, &vmx
->rmode
.segs
[VCPU_SREG_GS
]);
4116 static void fix_rmode_seg(int seg
, struct kvm_segment
*save
)
4118 const struct kvm_vmx_segment_field
*sf
= &kvm_vmx_segment_fields
[seg
];
4119 struct kvm_segment var
= *save
;
4122 if (seg
== VCPU_SREG_CS
)
4125 if (!emulate_invalid_guest_state
) {
4126 var
.selector
= var
.base
>> 4;
4127 var
.base
= var
.base
& 0xffff0;
4137 if (save
->base
& 0xf)
4138 printk_once(KERN_WARNING
"kvm: segment base is not "
4139 "paragraph aligned when entering "
4140 "protected mode (seg=%d)", seg
);
4143 vmcs_write16(sf
->selector
, var
.selector
);
4144 vmcs_writel(sf
->base
, var
.base
);
4145 vmcs_write32(sf
->limit
, var
.limit
);
4146 vmcs_write32(sf
->ar_bytes
, vmx_segment_access_rights(&var
));
4149 static void enter_rmode(struct kvm_vcpu
*vcpu
)
4151 unsigned long flags
;
4152 struct vcpu_vmx
*vmx
= to_vmx(vcpu
);
4154 vmx_get_segment(vcpu
, &vmx
->rmode
.segs
[VCPU_SREG_TR
], VCPU_SREG_TR
);
4155 vmx_get_segment(vcpu
, &vmx
->rmode
.segs
[VCPU_SREG_ES
], VCPU_SREG_ES
);
4156 vmx_get_segment(vcpu
, &vmx
->rmode
.segs
[VCPU_SREG_DS
], VCPU_SREG_DS
);
4157 vmx_get_segment(vcpu
, &vmx
->rmode
.segs
[VCPU_SREG_FS
], VCPU_SREG_FS
);
4158 vmx_get_segment(vcpu
, &vmx
->rmode
.segs
[VCPU_SREG_GS
], VCPU_SREG_GS
);
4159 vmx_get_segment(vcpu
, &vmx
->rmode
.segs
[VCPU_SREG_SS
], VCPU_SREG_SS
);
4160 vmx_get_segment(vcpu
, &vmx
->rmode
.segs
[VCPU_SREG_CS
], VCPU_SREG_CS
);
4162 vmx
->rmode
.vm86_active
= 1;
4165 * Very old userspace does not call KVM_SET_TSS_ADDR before entering
4166 * vcpu. Warn the user that an update is overdue.
4168 if (!vcpu
->kvm
->arch
.tss_addr
)
4169 printk_once(KERN_WARNING
"kvm: KVM_SET_TSS_ADDR need to be "
4170 "called before entering vcpu\n");
4172 vmx_segment_cache_clear(vmx
);
4174 vmcs_writel(GUEST_TR_BASE
, vcpu
->kvm
->arch
.tss_addr
);
4175 vmcs_write32(GUEST_TR_LIMIT
, RMODE_TSS_SIZE
- 1);
4176 vmcs_write32(GUEST_TR_AR_BYTES
, 0x008b);
4178 flags
= vmcs_readl(GUEST_RFLAGS
);
4179 vmx
->rmode
.save_rflags
= flags
;
4181 flags
|= X86_EFLAGS_IOPL
| X86_EFLAGS_VM
;
4183 vmcs_writel(GUEST_RFLAGS
, flags
);
4184 vmcs_writel(GUEST_CR4
, vmcs_readl(GUEST_CR4
) | X86_CR4_VME
);
4185 update_exception_bitmap(vcpu
);
4187 fix_rmode_seg(VCPU_SREG_SS
, &vmx
->rmode
.segs
[VCPU_SREG_SS
]);
4188 fix_rmode_seg(VCPU_SREG_CS
, &vmx
->rmode
.segs
[VCPU_SREG_CS
]);
4189 fix_rmode_seg(VCPU_SREG_ES
, &vmx
->rmode
.segs
[VCPU_SREG_ES
]);
4190 fix_rmode_seg(VCPU_SREG_DS
, &vmx
->rmode
.segs
[VCPU_SREG_DS
]);
4191 fix_rmode_seg(VCPU_SREG_GS
, &vmx
->rmode
.segs
[VCPU_SREG_GS
]);
4192 fix_rmode_seg(VCPU_SREG_FS
, &vmx
->rmode
.segs
[VCPU_SREG_FS
]);
4194 kvm_mmu_reset_context(vcpu
);
4197 static void vmx_set_efer(struct kvm_vcpu
*vcpu
, u64 efer
)
4199 struct vcpu_vmx
*vmx
= to_vmx(vcpu
);
4200 struct shared_msr_entry
*msr
= find_msr_entry(vmx
, MSR_EFER
);
4206 * Force kernel_gs_base reloading before EFER changes, as control
4207 * of this msr depends on is_long_mode().
4209 vmx_load_host_state(to_vmx(vcpu
));
4210 vcpu
->arch
.efer
= efer
;
4211 if (efer
& EFER_LMA
) {
4212 vm_entry_controls_setbit(to_vmx(vcpu
), VM_ENTRY_IA32E_MODE
);
4215 vm_entry_controls_clearbit(to_vmx(vcpu
), VM_ENTRY_IA32E_MODE
);
4217 msr
->data
= efer
& ~EFER_LME
;
4222 #ifdef CONFIG_X86_64
4224 static void enter_lmode(struct kvm_vcpu
*vcpu
)
4228 vmx_segment_cache_clear(to_vmx(vcpu
));
4230 guest_tr_ar
= vmcs_read32(GUEST_TR_AR_BYTES
);
4231 if ((guest_tr_ar
& VMX_AR_TYPE_MASK
) != VMX_AR_TYPE_BUSY_64_TSS
) {
4232 pr_debug_ratelimited("%s: tss fixup for long mode. \n",
4234 vmcs_write32(GUEST_TR_AR_BYTES
,
4235 (guest_tr_ar
& ~VMX_AR_TYPE_MASK
)
4236 | VMX_AR_TYPE_BUSY_64_TSS
);
4238 vmx_set_efer(vcpu
, vcpu
->arch
.efer
| EFER_LMA
);
4241 static void exit_lmode(struct kvm_vcpu
*vcpu
)
4243 vm_entry_controls_clearbit(to_vmx(vcpu
), VM_ENTRY_IA32E_MODE
);
4244 vmx_set_efer(vcpu
, vcpu
->arch
.efer
& ~EFER_LMA
);
4249 static inline void __vmx_flush_tlb(struct kvm_vcpu
*vcpu
, int vpid
,
4250 bool invalidate_gpa
)
4252 if (enable_ept
&& (invalidate_gpa
|| !enable_vpid
)) {
4253 if (!VALID_PAGE(vcpu
->arch
.mmu
.root_hpa
))
4255 ept_sync_context(construct_eptp(vcpu
, vcpu
->arch
.mmu
.root_hpa
));
4257 vpid_sync_context(vpid
);
4261 static void vmx_flush_tlb(struct kvm_vcpu
*vcpu
, bool invalidate_gpa
)
4263 __vmx_flush_tlb(vcpu
, to_vmx(vcpu
)->vpid
, invalidate_gpa
);
4266 static void vmx_flush_tlb_ept_only(struct kvm_vcpu
*vcpu
)
4269 vmx_flush_tlb(vcpu
, true);
4272 static void vmx_decache_cr0_guest_bits(struct kvm_vcpu
*vcpu
)
4274 ulong cr0_guest_owned_bits
= vcpu
->arch
.cr0_guest_owned_bits
;
4276 vcpu
->arch
.cr0
&= ~cr0_guest_owned_bits
;
4277 vcpu
->arch
.cr0
|= vmcs_readl(GUEST_CR0
) & cr0_guest_owned_bits
;
4280 static void vmx_decache_cr3(struct kvm_vcpu
*vcpu
)
4282 if (enable_ept
&& is_paging(vcpu
))
4283 vcpu
->arch
.cr3
= vmcs_readl(GUEST_CR3
);
4284 __set_bit(VCPU_EXREG_CR3
, (ulong
*)&vcpu
->arch
.regs_avail
);
4287 static void vmx_decache_cr4_guest_bits(struct kvm_vcpu
*vcpu
)
4289 ulong cr4_guest_owned_bits
= vcpu
->arch
.cr4_guest_owned_bits
;
4291 vcpu
->arch
.cr4
&= ~cr4_guest_owned_bits
;
4292 vcpu
->arch
.cr4
|= vmcs_readl(GUEST_CR4
) & cr4_guest_owned_bits
;
4295 static void ept_load_pdptrs(struct kvm_vcpu
*vcpu
)
4297 struct kvm_mmu
*mmu
= vcpu
->arch
.walk_mmu
;
4299 if (!test_bit(VCPU_EXREG_PDPTR
,
4300 (unsigned long *)&vcpu
->arch
.regs_dirty
))
4303 if (is_paging(vcpu
) && is_pae(vcpu
) && !is_long_mode(vcpu
)) {
4304 vmcs_write64(GUEST_PDPTR0
, mmu
->pdptrs
[0]);
4305 vmcs_write64(GUEST_PDPTR1
, mmu
->pdptrs
[1]);
4306 vmcs_write64(GUEST_PDPTR2
, mmu
->pdptrs
[2]);
4307 vmcs_write64(GUEST_PDPTR3
, mmu
->pdptrs
[3]);
4311 static void ept_save_pdptrs(struct kvm_vcpu
*vcpu
)
4313 struct kvm_mmu
*mmu
= vcpu
->arch
.walk_mmu
;
4315 if (is_paging(vcpu
) && is_pae(vcpu
) && !is_long_mode(vcpu
)) {
4316 mmu
->pdptrs
[0] = vmcs_read64(GUEST_PDPTR0
);
4317 mmu
->pdptrs
[1] = vmcs_read64(GUEST_PDPTR1
);
4318 mmu
->pdptrs
[2] = vmcs_read64(GUEST_PDPTR2
);
4319 mmu
->pdptrs
[3] = vmcs_read64(GUEST_PDPTR3
);
4322 __set_bit(VCPU_EXREG_PDPTR
,
4323 (unsigned long *)&vcpu
->arch
.regs_avail
);
4324 __set_bit(VCPU_EXREG_PDPTR
,
4325 (unsigned long *)&vcpu
->arch
.regs_dirty
);
4328 static bool nested_guest_cr0_valid(struct kvm_vcpu
*vcpu
, unsigned long val
)
4330 u64 fixed0
= to_vmx(vcpu
)->nested
.nested_vmx_cr0_fixed0
;
4331 u64 fixed1
= to_vmx(vcpu
)->nested
.nested_vmx_cr0_fixed1
;
4332 struct vmcs12
*vmcs12
= get_vmcs12(vcpu
);
4334 if (to_vmx(vcpu
)->nested
.nested_vmx_secondary_ctls_high
&
4335 SECONDARY_EXEC_UNRESTRICTED_GUEST
&&
4336 nested_cpu_has2(vmcs12
, SECONDARY_EXEC_UNRESTRICTED_GUEST
))
4337 fixed0
&= ~(X86_CR0_PE
| X86_CR0_PG
);
4339 return fixed_bits_valid(val
, fixed0
, fixed1
);
4342 static bool nested_host_cr0_valid(struct kvm_vcpu
*vcpu
, unsigned long val
)
4344 u64 fixed0
= to_vmx(vcpu
)->nested
.nested_vmx_cr0_fixed0
;
4345 u64 fixed1
= to_vmx(vcpu
)->nested
.nested_vmx_cr0_fixed1
;
4347 return fixed_bits_valid(val
, fixed0
, fixed1
);
4350 static bool nested_cr4_valid(struct kvm_vcpu
*vcpu
, unsigned long val
)
4352 u64 fixed0
= to_vmx(vcpu
)->nested
.nested_vmx_cr4_fixed0
;
4353 u64 fixed1
= to_vmx(vcpu
)->nested
.nested_vmx_cr4_fixed1
;
4355 return fixed_bits_valid(val
, fixed0
, fixed1
);
4358 /* No difference in the restrictions on guest and host CR4 in VMX operation. */
4359 #define nested_guest_cr4_valid nested_cr4_valid
4360 #define nested_host_cr4_valid nested_cr4_valid
4362 static int vmx_set_cr4(struct kvm_vcpu
*vcpu
, unsigned long cr4
);
4364 static void ept_update_paging_mode_cr0(unsigned long *hw_cr0
,
4366 struct kvm_vcpu
*vcpu
)
4368 if (!test_bit(VCPU_EXREG_CR3
, (ulong
*)&vcpu
->arch
.regs_avail
))
4369 vmx_decache_cr3(vcpu
);
4370 if (!(cr0
& X86_CR0_PG
)) {
4371 /* From paging/starting to nonpaging */
4372 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL
,
4373 vmcs_read32(CPU_BASED_VM_EXEC_CONTROL
) |
4374 (CPU_BASED_CR3_LOAD_EXITING
|
4375 CPU_BASED_CR3_STORE_EXITING
));
4376 vcpu
->arch
.cr0
= cr0
;
4377 vmx_set_cr4(vcpu
, kvm_read_cr4(vcpu
));
4378 } else if (!is_paging(vcpu
)) {
4379 /* From nonpaging to paging */
4380 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL
,
4381 vmcs_read32(CPU_BASED_VM_EXEC_CONTROL
) &
4382 ~(CPU_BASED_CR3_LOAD_EXITING
|
4383 CPU_BASED_CR3_STORE_EXITING
));
4384 vcpu
->arch
.cr0
= cr0
;
4385 vmx_set_cr4(vcpu
, kvm_read_cr4(vcpu
));
4388 if (!(cr0
& X86_CR0_WP
))
4389 *hw_cr0
&= ~X86_CR0_WP
;
4392 static void vmx_set_cr0(struct kvm_vcpu
*vcpu
, unsigned long cr0
)
4394 struct vcpu_vmx
*vmx
= to_vmx(vcpu
);
4395 unsigned long hw_cr0
;
4397 hw_cr0
= (cr0
& ~KVM_GUEST_CR0_MASK
);
4398 if (enable_unrestricted_guest
)
4399 hw_cr0
|= KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST
;
4401 hw_cr0
|= KVM_VM_CR0_ALWAYS_ON
;
4403 if (vmx
->rmode
.vm86_active
&& (cr0
& X86_CR0_PE
))
4406 if (!vmx
->rmode
.vm86_active
&& !(cr0
& X86_CR0_PE
))
4410 #ifdef CONFIG_X86_64
4411 if (vcpu
->arch
.efer
& EFER_LME
) {
4412 if (!is_paging(vcpu
) && (cr0
& X86_CR0_PG
))
4414 if (is_paging(vcpu
) && !(cr0
& X86_CR0_PG
))
4420 ept_update_paging_mode_cr0(&hw_cr0
, cr0
, vcpu
);
4422 vmcs_writel(CR0_READ_SHADOW
, cr0
);
4423 vmcs_writel(GUEST_CR0
, hw_cr0
);
4424 vcpu
->arch
.cr0
= cr0
;
4426 /* depends on vcpu->arch.cr0 to be set to a new value */
4427 vmx
->emulation_required
= emulation_required(vcpu
);
4430 static int get_ept_level(struct kvm_vcpu
*vcpu
)
4432 if (cpu_has_vmx_ept_5levels() && (cpuid_maxphyaddr(vcpu
) > 48))
4437 static u64
construct_eptp(struct kvm_vcpu
*vcpu
, unsigned long root_hpa
)
4439 u64 eptp
= VMX_EPTP_MT_WB
;
4441 eptp
|= (get_ept_level(vcpu
) == 5) ? VMX_EPTP_PWL_5
: VMX_EPTP_PWL_4
;
4443 if (enable_ept_ad_bits
&&
4444 (!is_guest_mode(vcpu
) || nested_ept_ad_enabled(vcpu
)))
4445 eptp
|= VMX_EPTP_AD_ENABLE_BIT
;
4446 eptp
|= (root_hpa
& PAGE_MASK
);
4451 static void vmx_set_cr3(struct kvm_vcpu
*vcpu
, unsigned long cr3
)
4453 unsigned long guest_cr3
;
4458 eptp
= construct_eptp(vcpu
, cr3
);
4459 vmcs_write64(EPT_POINTER
, eptp
);
4460 if (is_paging(vcpu
) || is_guest_mode(vcpu
))
4461 guest_cr3
= kvm_read_cr3(vcpu
);
4463 guest_cr3
= vcpu
->kvm
->arch
.ept_identity_map_addr
;
4464 ept_load_pdptrs(vcpu
);
4467 vmx_flush_tlb(vcpu
, true);
4468 vmcs_writel(GUEST_CR3
, guest_cr3
);
4471 static int vmx_set_cr4(struct kvm_vcpu
*vcpu
, unsigned long cr4
)
4474 * Pass through host's Machine Check Enable value to hw_cr4, which
4475 * is in force while we are in guest mode. Do not let guests control
4476 * this bit, even if host CR4.MCE == 0.
4478 unsigned long hw_cr4
=
4479 (cr4_read_shadow() & X86_CR4_MCE
) |
4480 (cr4
& ~X86_CR4_MCE
) |
4481 (to_vmx(vcpu
)->rmode
.vm86_active
?
4482 KVM_RMODE_VM_CR4_ALWAYS_ON
: KVM_PMODE_VM_CR4_ALWAYS_ON
);
4484 if ((cr4
& X86_CR4_UMIP
) && !boot_cpu_has(X86_FEATURE_UMIP
)) {
4485 vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL
,
4486 SECONDARY_EXEC_DESC
);
4487 hw_cr4
&= ~X86_CR4_UMIP
;
4489 vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL
,
4490 SECONDARY_EXEC_DESC
);
4492 if (cr4
& X86_CR4_VMXE
) {
4494 * To use VMXON (and later other VMX instructions), a guest
4495 * must first be able to turn on cr4.VMXE (see handle_vmon()).
4496 * So basically the check on whether to allow nested VMX
4499 if (!nested_vmx_allowed(vcpu
))
4503 if (to_vmx(vcpu
)->nested
.vmxon
&& !nested_cr4_valid(vcpu
, cr4
))
4506 vcpu
->arch
.cr4
= cr4
;
4508 if (!is_paging(vcpu
)) {
4509 hw_cr4
&= ~X86_CR4_PAE
;
4510 hw_cr4
|= X86_CR4_PSE
;
4511 } else if (!(cr4
& X86_CR4_PAE
)) {
4512 hw_cr4
&= ~X86_CR4_PAE
;
4516 if (!enable_unrestricted_guest
&& !is_paging(vcpu
))
4518 * SMEP/SMAP/PKU is disabled if CPU is in non-paging mode in
4519 * hardware. To emulate this behavior, SMEP/SMAP/PKU needs
4520 * to be manually disabled when guest switches to non-paging
4523 * If !enable_unrestricted_guest, the CPU is always running
4524 * with CR0.PG=1 and CR4 needs to be modified.
4525 * If enable_unrestricted_guest, the CPU automatically
4526 * disables SMEP/SMAP/PKU when the guest sets CR0.PG=0.
4528 hw_cr4
&= ~(X86_CR4_SMEP
| X86_CR4_SMAP
| X86_CR4_PKE
);
4530 vmcs_writel(CR4_READ_SHADOW
, cr4
);
4531 vmcs_writel(GUEST_CR4
, hw_cr4
);
4535 static void vmx_get_segment(struct kvm_vcpu
*vcpu
,
4536 struct kvm_segment
*var
, int seg
)
4538 struct vcpu_vmx
*vmx
= to_vmx(vcpu
);
4541 if (vmx
->rmode
.vm86_active
&& seg
!= VCPU_SREG_LDTR
) {
4542 *var
= vmx
->rmode
.segs
[seg
];
4543 if (seg
== VCPU_SREG_TR
4544 || var
->selector
== vmx_read_guest_seg_selector(vmx
, seg
))
4546 var
->base
= vmx_read_guest_seg_base(vmx
, seg
);
4547 var
->selector
= vmx_read_guest_seg_selector(vmx
, seg
);
4550 var
->base
= vmx_read_guest_seg_base(vmx
, seg
);
4551 var
->limit
= vmx_read_guest_seg_limit(vmx
, seg
);
4552 var
->selector
= vmx_read_guest_seg_selector(vmx
, seg
);
4553 ar
= vmx_read_guest_seg_ar(vmx
, seg
);
4554 var
->unusable
= (ar
>> 16) & 1;
4555 var
->type
= ar
& 15;
4556 var
->s
= (ar
>> 4) & 1;
4557 var
->dpl
= (ar
>> 5) & 3;
4559 * Some userspaces do not preserve unusable property. Since usable
4560 * segment has to be present according to VMX spec we can use present
4561 * property to amend userspace bug by making unusable segment always
4562 * nonpresent. vmx_segment_access_rights() already marks nonpresent
4563 * segment as unusable.
4565 var
->present
= !var
->unusable
;
4566 var
->avl
= (ar
>> 12) & 1;
4567 var
->l
= (ar
>> 13) & 1;
4568 var
->db
= (ar
>> 14) & 1;
4569 var
->g
= (ar
>> 15) & 1;
4572 static u64
vmx_get_segment_base(struct kvm_vcpu
*vcpu
, int seg
)
4574 struct kvm_segment s
;
4576 if (to_vmx(vcpu
)->rmode
.vm86_active
) {
4577 vmx_get_segment(vcpu
, &s
, seg
);
4580 return vmx_read_guest_seg_base(to_vmx(vcpu
), seg
);
4583 static int vmx_get_cpl(struct kvm_vcpu
*vcpu
)
4585 struct vcpu_vmx
*vmx
= to_vmx(vcpu
);
4587 if (unlikely(vmx
->rmode
.vm86_active
))
4590 int ar
= vmx_read_guest_seg_ar(vmx
, VCPU_SREG_SS
);
4591 return VMX_AR_DPL(ar
);
4595 static u32
vmx_segment_access_rights(struct kvm_segment
*var
)
4599 if (var
->unusable
|| !var
->present
)
4602 ar
= var
->type
& 15;
4603 ar
|= (var
->s
& 1) << 4;
4604 ar
|= (var
->dpl
& 3) << 5;
4605 ar
|= (var
->present
& 1) << 7;
4606 ar
|= (var
->avl
& 1) << 12;
4607 ar
|= (var
->l
& 1) << 13;
4608 ar
|= (var
->db
& 1) << 14;
4609 ar
|= (var
->g
& 1) << 15;
4615 static void vmx_set_segment(struct kvm_vcpu
*vcpu
,
4616 struct kvm_segment
*var
, int seg
)
4618 struct vcpu_vmx
*vmx
= to_vmx(vcpu
);
4619 const struct kvm_vmx_segment_field
*sf
= &kvm_vmx_segment_fields
[seg
];
4621 vmx_segment_cache_clear(vmx
);
4623 if (vmx
->rmode
.vm86_active
&& seg
!= VCPU_SREG_LDTR
) {
4624 vmx
->rmode
.segs
[seg
] = *var
;
4625 if (seg
== VCPU_SREG_TR
)
4626 vmcs_write16(sf
->selector
, var
->selector
);
4628 fix_rmode_seg(seg
, &vmx
->rmode
.segs
[seg
]);
4632 vmcs_writel(sf
->base
, var
->base
);
4633 vmcs_write32(sf
->limit
, var
->limit
);
4634 vmcs_write16(sf
->selector
, var
->selector
);
4637 * Fix the "Accessed" bit in AR field of segment registers for older
4639 * IA32 arch specifies that at the time of processor reset the
4640 * "Accessed" bit in the AR field of segment registers is 1. And qemu
4641 * is setting it to 0 in the userland code. This causes invalid guest
4642 * state vmexit when "unrestricted guest" mode is turned on.
4643 * Fix for this setup issue in cpu_reset is being pushed in the qemu
4644 * tree. Newer qemu binaries with that qemu fix would not need this
4647 if (enable_unrestricted_guest
&& (seg
!= VCPU_SREG_LDTR
))
4648 var
->type
|= 0x1; /* Accessed */
4650 vmcs_write32(sf
->ar_bytes
, vmx_segment_access_rights(var
));
4653 vmx
->emulation_required
= emulation_required(vcpu
);
4656 static void vmx_get_cs_db_l_bits(struct kvm_vcpu
*vcpu
, int *db
, int *l
)
4658 u32 ar
= vmx_read_guest_seg_ar(to_vmx(vcpu
), VCPU_SREG_CS
);
4660 *db
= (ar
>> 14) & 1;
4661 *l
= (ar
>> 13) & 1;
4664 static void vmx_get_idt(struct kvm_vcpu
*vcpu
, struct desc_ptr
*dt
)
4666 dt
->size
= vmcs_read32(GUEST_IDTR_LIMIT
);
4667 dt
->address
= vmcs_readl(GUEST_IDTR_BASE
);
4670 static void vmx_set_idt(struct kvm_vcpu
*vcpu
, struct desc_ptr
*dt
)
4672 vmcs_write32(GUEST_IDTR_LIMIT
, dt
->size
);
4673 vmcs_writel(GUEST_IDTR_BASE
, dt
->address
);
4676 static void vmx_get_gdt(struct kvm_vcpu
*vcpu
, struct desc_ptr
*dt
)
4678 dt
->size
= vmcs_read32(GUEST_GDTR_LIMIT
);
4679 dt
->address
= vmcs_readl(GUEST_GDTR_BASE
);
4682 static void vmx_set_gdt(struct kvm_vcpu
*vcpu
, struct desc_ptr
*dt
)
4684 vmcs_write32(GUEST_GDTR_LIMIT
, dt
->size
);
4685 vmcs_writel(GUEST_GDTR_BASE
, dt
->address
);
4688 static bool rmode_segment_valid(struct kvm_vcpu
*vcpu
, int seg
)
4690 struct kvm_segment var
;
4693 vmx_get_segment(vcpu
, &var
, seg
);
4695 if (seg
== VCPU_SREG_CS
)
4697 ar
= vmx_segment_access_rights(&var
);
4699 if (var
.base
!= (var
.selector
<< 4))
4701 if (var
.limit
!= 0xffff)
4709 static bool code_segment_valid(struct kvm_vcpu
*vcpu
)
4711 struct kvm_segment cs
;
4712 unsigned int cs_rpl
;
4714 vmx_get_segment(vcpu
, &cs
, VCPU_SREG_CS
);
4715 cs_rpl
= cs
.selector
& SEGMENT_RPL_MASK
;
4719 if (~cs
.type
& (VMX_AR_TYPE_CODE_MASK
|VMX_AR_TYPE_ACCESSES_MASK
))
4723 if (cs
.type
& VMX_AR_TYPE_WRITEABLE_MASK
) {
4724 if (cs
.dpl
> cs_rpl
)
4727 if (cs
.dpl
!= cs_rpl
)
4733 /* TODO: Add Reserved field check, this'll require a new member in the kvm_segment_field structure */
4737 static bool stack_segment_valid(struct kvm_vcpu
*vcpu
)
4739 struct kvm_segment ss
;
4740 unsigned int ss_rpl
;
4742 vmx_get_segment(vcpu
, &ss
, VCPU_SREG_SS
);
4743 ss_rpl
= ss
.selector
& SEGMENT_RPL_MASK
;
4747 if (ss
.type
!= 3 && ss
.type
!= 7)
4751 if (ss
.dpl
!= ss_rpl
) /* DPL != RPL */
4759 static bool data_segment_valid(struct kvm_vcpu
*vcpu
, int seg
)
4761 struct kvm_segment var
;
4764 vmx_get_segment(vcpu
, &var
, seg
);
4765 rpl
= var
.selector
& SEGMENT_RPL_MASK
;
4773 if (~var
.type
& (VMX_AR_TYPE_CODE_MASK
|VMX_AR_TYPE_WRITEABLE_MASK
)) {
4774 if (var
.dpl
< rpl
) /* DPL < RPL */
4778 /* TODO: Add other members to kvm_segment_field to allow checking for other access
4784 static bool tr_valid(struct kvm_vcpu
*vcpu
)
4786 struct kvm_segment tr
;
4788 vmx_get_segment(vcpu
, &tr
, VCPU_SREG_TR
);
4792 if (tr
.selector
& SEGMENT_TI_MASK
) /* TI = 1 */
4794 if (tr
.type
!= 3 && tr
.type
!= 11) /* TODO: Check if guest is in IA32e mode */
4802 static bool ldtr_valid(struct kvm_vcpu
*vcpu
)
4804 struct kvm_segment ldtr
;
4806 vmx_get_segment(vcpu
, &ldtr
, VCPU_SREG_LDTR
);
4810 if (ldtr
.selector
& SEGMENT_TI_MASK
) /* TI = 1 */
4820 static bool cs_ss_rpl_check(struct kvm_vcpu
*vcpu
)
4822 struct kvm_segment cs
, ss
;
4824 vmx_get_segment(vcpu
, &cs
, VCPU_SREG_CS
);
4825 vmx_get_segment(vcpu
, &ss
, VCPU_SREG_SS
);
4827 return ((cs
.selector
& SEGMENT_RPL_MASK
) ==
4828 (ss
.selector
& SEGMENT_RPL_MASK
));
4832 * Check if guest state is valid. Returns true if valid, false if
4834 * We assume that registers are always usable
4836 static bool guest_state_valid(struct kvm_vcpu
*vcpu
)
4838 if (enable_unrestricted_guest
)
4841 /* real mode guest state checks */
4842 if (!is_protmode(vcpu
) || (vmx_get_rflags(vcpu
) & X86_EFLAGS_VM
)) {
4843 if (!rmode_segment_valid(vcpu
, VCPU_SREG_CS
))
4845 if (!rmode_segment_valid(vcpu
, VCPU_SREG_SS
))
4847 if (!rmode_segment_valid(vcpu
, VCPU_SREG_DS
))
4849 if (!rmode_segment_valid(vcpu
, VCPU_SREG_ES
))
4851 if (!rmode_segment_valid(vcpu
, VCPU_SREG_FS
))
4853 if (!rmode_segment_valid(vcpu
, VCPU_SREG_GS
))
4856 /* protected mode guest state checks */
4857 if (!cs_ss_rpl_check(vcpu
))
4859 if (!code_segment_valid(vcpu
))
4861 if (!stack_segment_valid(vcpu
))
4863 if (!data_segment_valid(vcpu
, VCPU_SREG_DS
))
4865 if (!data_segment_valid(vcpu
, VCPU_SREG_ES
))
4867 if (!data_segment_valid(vcpu
, VCPU_SREG_FS
))
4869 if (!data_segment_valid(vcpu
, VCPU_SREG_GS
))
4871 if (!tr_valid(vcpu
))
4873 if (!ldtr_valid(vcpu
))
4877 * - Add checks on RIP
4878 * - Add checks on RFLAGS
4884 static bool page_address_valid(struct kvm_vcpu
*vcpu
, gpa_t gpa
)
4886 return PAGE_ALIGNED(gpa
) && !(gpa
>> cpuid_maxphyaddr(vcpu
));
4889 static int init_rmode_tss(struct kvm
*kvm
)
4895 idx
= srcu_read_lock(&kvm
->srcu
);
4896 fn
= kvm
->arch
.tss_addr
>> PAGE_SHIFT
;
4897 r
= kvm_clear_guest_page(kvm
, fn
, 0, PAGE_SIZE
);
4900 data
= TSS_BASE_SIZE
+ TSS_REDIRECTION_SIZE
;
4901 r
= kvm_write_guest_page(kvm
, fn
++, &data
,
4902 TSS_IOPB_BASE_OFFSET
, sizeof(u16
));
4905 r
= kvm_clear_guest_page(kvm
, fn
++, 0, PAGE_SIZE
);
4908 r
= kvm_clear_guest_page(kvm
, fn
, 0, PAGE_SIZE
);
4912 r
= kvm_write_guest_page(kvm
, fn
, &data
,
4913 RMODE_TSS_SIZE
- 2 * PAGE_SIZE
- 1,
4916 srcu_read_unlock(&kvm
->srcu
, idx
);
4920 static int init_rmode_identity_map(struct kvm
*kvm
)
4923 kvm_pfn_t identity_map_pfn
;
4926 /* Protect kvm->arch.ept_identity_pagetable_done. */
4927 mutex_lock(&kvm
->slots_lock
);
4929 if (likely(kvm
->arch
.ept_identity_pagetable_done
))
4932 if (!kvm
->arch
.ept_identity_map_addr
)
4933 kvm
->arch
.ept_identity_map_addr
= VMX_EPT_IDENTITY_PAGETABLE_ADDR
;
4934 identity_map_pfn
= kvm
->arch
.ept_identity_map_addr
>> PAGE_SHIFT
;
4936 r
= __x86_set_memory_region(kvm
, IDENTITY_PAGETABLE_PRIVATE_MEMSLOT
,
4937 kvm
->arch
.ept_identity_map_addr
, PAGE_SIZE
);
4941 idx
= srcu_read_lock(&kvm
->srcu
);
4942 r
= kvm_clear_guest_page(kvm
, identity_map_pfn
, 0, PAGE_SIZE
);
4945 /* Set up identity-mapping pagetable for EPT in real mode */
4946 for (i
= 0; i
< PT32_ENT_PER_PAGE
; i
++) {
4947 tmp
= (i
<< 22) + (_PAGE_PRESENT
| _PAGE_RW
| _PAGE_USER
|
4948 _PAGE_ACCESSED
| _PAGE_DIRTY
| _PAGE_PSE
);
4949 r
= kvm_write_guest_page(kvm
, identity_map_pfn
,
4950 &tmp
, i
* sizeof(tmp
), sizeof(tmp
));
4954 kvm
->arch
.ept_identity_pagetable_done
= true;
4957 srcu_read_unlock(&kvm
->srcu
, idx
);
4960 mutex_unlock(&kvm
->slots_lock
);
4964 static void seg_setup(int seg
)
4966 const struct kvm_vmx_segment_field
*sf
= &kvm_vmx_segment_fields
[seg
];
4969 vmcs_write16(sf
->selector
, 0);
4970 vmcs_writel(sf
->base
, 0);
4971 vmcs_write32(sf
->limit
, 0xffff);
4973 if (seg
== VCPU_SREG_CS
)
4974 ar
|= 0x08; /* code segment */
4976 vmcs_write32(sf
->ar_bytes
, ar
);
4979 static int alloc_apic_access_page(struct kvm
*kvm
)
4984 mutex_lock(&kvm
->slots_lock
);
4985 if (kvm
->arch
.apic_access_page_done
)
4987 r
= __x86_set_memory_region(kvm
, APIC_ACCESS_PAGE_PRIVATE_MEMSLOT
,
4988 APIC_DEFAULT_PHYS_BASE
, PAGE_SIZE
);
4992 page
= gfn_to_page(kvm
, APIC_DEFAULT_PHYS_BASE
>> PAGE_SHIFT
);
4993 if (is_error_page(page
)) {
4999 * Do not pin the page in memory, so that memory hot-unplug
5000 * is able to migrate it.
5003 kvm
->arch
.apic_access_page_done
= true;
5005 mutex_unlock(&kvm
->slots_lock
);
5009 static int allocate_vpid(void)
5015 spin_lock(&vmx_vpid_lock
);
5016 vpid
= find_first_zero_bit(vmx_vpid_bitmap
, VMX_NR_VPIDS
);
5017 if (vpid
< VMX_NR_VPIDS
)
5018 __set_bit(vpid
, vmx_vpid_bitmap
);
5021 spin_unlock(&vmx_vpid_lock
);
5025 static void free_vpid(int vpid
)
5027 if (!enable_vpid
|| vpid
== 0)
5029 spin_lock(&vmx_vpid_lock
);
5030 __clear_bit(vpid
, vmx_vpid_bitmap
);
5031 spin_unlock(&vmx_vpid_lock
);
5034 static void __always_inline
vmx_disable_intercept_for_msr(unsigned long *msr_bitmap
,
5037 int f
= sizeof(unsigned long);
5039 if (!cpu_has_vmx_msr_bitmap())
5043 * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
5044 * have the write-low and read-high bitmap offsets the wrong way round.
5045 * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
5047 if (msr
<= 0x1fff) {
5048 if (type
& MSR_TYPE_R
)
5050 __clear_bit(msr
, msr_bitmap
+ 0x000 / f
);
5052 if (type
& MSR_TYPE_W
)
5054 __clear_bit(msr
, msr_bitmap
+ 0x800 / f
);
5056 } else if ((msr
>= 0xc0000000) && (msr
<= 0xc0001fff)) {
5058 if (type
& MSR_TYPE_R
)
5060 __clear_bit(msr
, msr_bitmap
+ 0x400 / f
);
5062 if (type
& MSR_TYPE_W
)
5064 __clear_bit(msr
, msr_bitmap
+ 0xc00 / f
);
5069 static void __always_inline
vmx_enable_intercept_for_msr(unsigned long *msr_bitmap
,
5072 int f
= sizeof(unsigned long);
5074 if (!cpu_has_vmx_msr_bitmap())
5078 * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
5079 * have the write-low and read-high bitmap offsets the wrong way round.
5080 * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
5082 if (msr
<= 0x1fff) {
5083 if (type
& MSR_TYPE_R
)
5085 __set_bit(msr
, msr_bitmap
+ 0x000 / f
);
5087 if (type
& MSR_TYPE_W
)
5089 __set_bit(msr
, msr_bitmap
+ 0x800 / f
);
5091 } else if ((msr
>= 0xc0000000) && (msr
<= 0xc0001fff)) {
5093 if (type
& MSR_TYPE_R
)
5095 __set_bit(msr
, msr_bitmap
+ 0x400 / f
);
5097 if (type
& MSR_TYPE_W
)
5099 __set_bit(msr
, msr_bitmap
+ 0xc00 / f
);
5104 static void __always_inline
vmx_set_intercept_for_msr(unsigned long *msr_bitmap
,
5105 u32 msr
, int type
, bool value
)
5108 vmx_enable_intercept_for_msr(msr_bitmap
, msr
, type
);
5110 vmx_disable_intercept_for_msr(msr_bitmap
, msr
, type
);
5114 * If a msr is allowed by L0, we should check whether it is allowed by L1.
5115 * The corresponding bit will be cleared unless both of L0 and L1 allow it.
5117 static void nested_vmx_disable_intercept_for_msr(unsigned long *msr_bitmap_l1
,
5118 unsigned long *msr_bitmap_nested
,
5121 int f
= sizeof(unsigned long);
5124 * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
5125 * have the write-low and read-high bitmap offsets the wrong way round.
5126 * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
5128 if (msr
<= 0x1fff) {
5129 if (type
& MSR_TYPE_R
&&
5130 !test_bit(msr
, msr_bitmap_l1
+ 0x000 / f
))
5132 __clear_bit(msr
, msr_bitmap_nested
+ 0x000 / f
);
5134 if (type
& MSR_TYPE_W
&&
5135 !test_bit(msr
, msr_bitmap_l1
+ 0x800 / f
))
5137 __clear_bit(msr
, msr_bitmap_nested
+ 0x800 / f
);
5139 } else if ((msr
>= 0xc0000000) && (msr
<= 0xc0001fff)) {
5141 if (type
& MSR_TYPE_R
&&
5142 !test_bit(msr
, msr_bitmap_l1
+ 0x400 / f
))
5144 __clear_bit(msr
, msr_bitmap_nested
+ 0x400 / f
);
5146 if (type
& MSR_TYPE_W
&&
5147 !test_bit(msr
, msr_bitmap_l1
+ 0xc00 / f
))
5149 __clear_bit(msr
, msr_bitmap_nested
+ 0xc00 / f
);
5154 static u8
vmx_msr_bitmap_mode(struct kvm_vcpu
*vcpu
)
5158 if (cpu_has_secondary_exec_ctrls() &&
5159 (vmcs_read32(SECONDARY_VM_EXEC_CONTROL
) &
5160 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE
)) {
5161 mode
|= MSR_BITMAP_MODE_X2APIC
;
5162 if (enable_apicv
&& kvm_vcpu_apicv_active(vcpu
))
5163 mode
|= MSR_BITMAP_MODE_X2APIC_APICV
;
5166 if (is_long_mode(vcpu
))
5167 mode
|= MSR_BITMAP_MODE_LM
;
5172 #define X2APIC_MSR(r) (APIC_BASE_MSR + ((r) >> 4))
5174 static void vmx_update_msr_bitmap_x2apic(unsigned long *msr_bitmap
,
5179 for (msr
= 0x800; msr
<= 0x8ff; msr
+= BITS_PER_LONG
) {
5180 unsigned word
= msr
/ BITS_PER_LONG
;
5181 msr_bitmap
[word
] = (mode
& MSR_BITMAP_MODE_X2APIC_APICV
) ? 0 : ~0;
5182 msr_bitmap
[word
+ (0x800 / sizeof(long))] = ~0;
5185 if (mode
& MSR_BITMAP_MODE_X2APIC
) {
5187 * TPR reads and writes can be virtualized even if virtual interrupt
5188 * delivery is not in use.
5190 vmx_disable_intercept_for_msr(msr_bitmap
, X2APIC_MSR(APIC_TASKPRI
), MSR_TYPE_RW
);
5191 if (mode
& MSR_BITMAP_MODE_X2APIC_APICV
) {
5192 vmx_enable_intercept_for_msr(msr_bitmap
, X2APIC_MSR(APIC_TMCCT
), MSR_TYPE_R
);
5193 vmx_disable_intercept_for_msr(msr_bitmap
, X2APIC_MSR(APIC_EOI
), MSR_TYPE_W
);
5194 vmx_disable_intercept_for_msr(msr_bitmap
, X2APIC_MSR(APIC_SELF_IPI
), MSR_TYPE_W
);
5199 static void vmx_update_msr_bitmap(struct kvm_vcpu
*vcpu
)
5201 struct vcpu_vmx
*vmx
= to_vmx(vcpu
);
5202 unsigned long *msr_bitmap
= vmx
->vmcs01
.msr_bitmap
;
5203 u8 mode
= vmx_msr_bitmap_mode(vcpu
);
5204 u8 changed
= mode
^ vmx
->msr_bitmap_mode
;
5209 vmx_set_intercept_for_msr(msr_bitmap
, MSR_KERNEL_GS_BASE
, MSR_TYPE_RW
,
5210 !(mode
& MSR_BITMAP_MODE_LM
));
5212 if (changed
& (MSR_BITMAP_MODE_X2APIC
| MSR_BITMAP_MODE_X2APIC_APICV
))
5213 vmx_update_msr_bitmap_x2apic(msr_bitmap
, mode
);
5215 vmx
->msr_bitmap_mode
= mode
;
5218 static bool vmx_get_enable_apicv(struct kvm_vcpu
*vcpu
)
5220 return enable_apicv
;
5223 static void nested_mark_vmcs12_pages_dirty(struct kvm_vcpu
*vcpu
)
5225 struct vmcs12
*vmcs12
= get_vmcs12(vcpu
);
5229 * Don't need to mark the APIC access page dirty; it is never
5230 * written to by the CPU during APIC virtualization.
5233 if (nested_cpu_has(vmcs12
, CPU_BASED_TPR_SHADOW
)) {
5234 gfn
= vmcs12
->virtual_apic_page_addr
>> PAGE_SHIFT
;
5235 kvm_vcpu_mark_page_dirty(vcpu
, gfn
);
5238 if (nested_cpu_has_posted_intr(vmcs12
)) {
5239 gfn
= vmcs12
->posted_intr_desc_addr
>> PAGE_SHIFT
;
5240 kvm_vcpu_mark_page_dirty(vcpu
, gfn
);
5245 static void vmx_complete_nested_posted_interrupt(struct kvm_vcpu
*vcpu
)
5247 struct vcpu_vmx
*vmx
= to_vmx(vcpu
);
5252 if (!vmx
->nested
.pi_desc
|| !vmx
->nested
.pi_pending
)
5255 vmx
->nested
.pi_pending
= false;
5256 if (!pi_test_and_clear_on(vmx
->nested
.pi_desc
))
5259 max_irr
= find_last_bit((unsigned long *)vmx
->nested
.pi_desc
->pir
, 256);
5260 if (max_irr
!= 256) {
5261 vapic_page
= kmap(vmx
->nested
.virtual_apic_page
);
5262 __kvm_apic_update_irr(vmx
->nested
.pi_desc
->pir
,
5263 vapic_page
, &max_irr
);
5264 kunmap(vmx
->nested
.virtual_apic_page
);
5266 status
= vmcs_read16(GUEST_INTR_STATUS
);
5267 if ((u8
)max_irr
> ((u8
)status
& 0xff)) {
5269 status
|= (u8
)max_irr
;
5270 vmcs_write16(GUEST_INTR_STATUS
, status
);
5274 nested_mark_vmcs12_pages_dirty(vcpu
);
5277 static inline bool kvm_vcpu_trigger_posted_interrupt(struct kvm_vcpu
*vcpu
,
5281 int pi_vec
= nested
? POSTED_INTR_NESTED_VECTOR
: POSTED_INTR_VECTOR
;
5283 if (vcpu
->mode
== IN_GUEST_MODE
) {
5285 * The vector of interrupt to be delivered to vcpu had
5286 * been set in PIR before this function.
5288 * Following cases will be reached in this block, and
5289 * we always send a notification event in all cases as
5292 * Case 1: vcpu keeps in non-root mode. Sending a
5293 * notification event posts the interrupt to vcpu.
5295 * Case 2: vcpu exits to root mode and is still
5296 * runnable. PIR will be synced to vIRR before the
5297 * next vcpu entry. Sending a notification event in
5298 * this case has no effect, as vcpu is not in root
5301 * Case 3: vcpu exits to root mode and is blocked.
5302 * vcpu_block() has already synced PIR to vIRR and
5303 * never blocks vcpu if vIRR is not cleared. Therefore,
5304 * a blocked vcpu here does not wait for any requested
5305 * interrupts in PIR, and sending a notification event
5306 * which has no effect is safe here.
5309 apic
->send_IPI_mask(get_cpu_mask(vcpu
->cpu
), pi_vec
);
5316 static int vmx_deliver_nested_posted_interrupt(struct kvm_vcpu
*vcpu
,
5319 struct vcpu_vmx
*vmx
= to_vmx(vcpu
);
5321 if (is_guest_mode(vcpu
) &&
5322 vector
== vmx
->nested
.posted_intr_nv
) {
5324 * If a posted intr is not recognized by hardware,
5325 * we will accomplish it in the next vmentry.
5327 vmx
->nested
.pi_pending
= true;
5328 kvm_make_request(KVM_REQ_EVENT
, vcpu
);
5329 /* the PIR and ON have been set by L1. */
5330 if (!kvm_vcpu_trigger_posted_interrupt(vcpu
, true))
5331 kvm_vcpu_kick(vcpu
);
5337 * Send interrupt to vcpu via posted interrupt way.
5338 * 1. If target vcpu is running(non-root mode), send posted interrupt
5339 * notification to vcpu and hardware will sync PIR to vIRR atomically.
5340 * 2. If target vcpu isn't running(root mode), kick it to pick up the
5341 * interrupt from PIR in next vmentry.
5343 static void vmx_deliver_posted_interrupt(struct kvm_vcpu
*vcpu
, int vector
)
5345 struct vcpu_vmx
*vmx
= to_vmx(vcpu
);
5348 r
= vmx_deliver_nested_posted_interrupt(vcpu
, vector
);
5352 if (pi_test_and_set_pir(vector
, &vmx
->pi_desc
))
5355 /* If a previous notification has sent the IPI, nothing to do. */
5356 if (pi_test_and_set_on(&vmx
->pi_desc
))
5359 if (!kvm_vcpu_trigger_posted_interrupt(vcpu
, false))
5360 kvm_vcpu_kick(vcpu
);
5364 * Set up the vmcs's constant host-state fields, i.e., host-state fields that
5365 * will not change in the lifetime of the guest.
5366 * Note that host-state that does change is set elsewhere. E.g., host-state
5367 * that is set differently for each CPU is set in vmx_vcpu_load(), not here.
5369 static void vmx_set_constant_host_state(struct vcpu_vmx
*vmx
)
5374 unsigned long cr0
, cr3
, cr4
;
5377 WARN_ON(cr0
& X86_CR0_TS
);
5378 vmcs_writel(HOST_CR0
, cr0
); /* 22.2.3 */
5381 * Save the most likely value for this task's CR3 in the VMCS.
5382 * We can't use __get_current_cr3_fast() because we're not atomic.
5385 vmcs_writel(HOST_CR3
, cr3
); /* 22.2.3 FIXME: shadow tables */
5386 vmx
->loaded_vmcs
->vmcs_host_cr3
= cr3
;
5388 /* Save the most likely value for this task's CR4 in the VMCS. */
5389 cr4
= cr4_read_shadow();
5390 vmcs_writel(HOST_CR4
, cr4
); /* 22.2.3, 22.2.5 */
5391 vmx
->loaded_vmcs
->vmcs_host_cr4
= cr4
;
5393 vmcs_write16(HOST_CS_SELECTOR
, __KERNEL_CS
); /* 22.2.4 */
5394 #ifdef CONFIG_X86_64
5396 * Load null selectors, so we can avoid reloading them in
5397 * __vmx_load_host_state(), in case userspace uses the null selectors
5398 * too (the expected case).
5400 vmcs_write16(HOST_DS_SELECTOR
, 0);
5401 vmcs_write16(HOST_ES_SELECTOR
, 0);
5403 vmcs_write16(HOST_DS_SELECTOR
, __KERNEL_DS
); /* 22.2.4 */
5404 vmcs_write16(HOST_ES_SELECTOR
, __KERNEL_DS
); /* 22.2.4 */
5406 vmcs_write16(HOST_SS_SELECTOR
, __KERNEL_DS
); /* 22.2.4 */
5407 vmcs_write16(HOST_TR_SELECTOR
, GDT_ENTRY_TSS
*8); /* 22.2.4 */
5410 vmcs_writel(HOST_IDTR_BASE
, dt
.address
); /* 22.2.4 */
5411 vmx
->host_idt_base
= dt
.address
;
5413 vmcs_writel(HOST_RIP
, vmx_return
); /* 22.2.5 */
5415 rdmsr(MSR_IA32_SYSENTER_CS
, low32
, high32
);
5416 vmcs_write32(HOST_IA32_SYSENTER_CS
, low32
);
5417 rdmsrl(MSR_IA32_SYSENTER_EIP
, tmpl
);
5418 vmcs_writel(HOST_IA32_SYSENTER_EIP
, tmpl
); /* 22.2.3 */
5420 if (vmcs_config
.vmexit_ctrl
& VM_EXIT_LOAD_IA32_PAT
) {
5421 rdmsr(MSR_IA32_CR_PAT
, low32
, high32
);
5422 vmcs_write64(HOST_IA32_PAT
, low32
| ((u64
) high32
<< 32));
5426 static void set_cr4_guest_host_mask(struct vcpu_vmx
*vmx
)
5428 vmx
->vcpu
.arch
.cr4_guest_owned_bits
= KVM_CR4_GUEST_OWNED_BITS
;
5430 vmx
->vcpu
.arch
.cr4_guest_owned_bits
|= X86_CR4_PGE
;
5431 if (is_guest_mode(&vmx
->vcpu
))
5432 vmx
->vcpu
.arch
.cr4_guest_owned_bits
&=
5433 ~get_vmcs12(&vmx
->vcpu
)->cr4_guest_host_mask
;
5434 vmcs_writel(CR4_GUEST_HOST_MASK
, ~vmx
->vcpu
.arch
.cr4_guest_owned_bits
);
5437 static u32
vmx_pin_based_exec_ctrl(struct vcpu_vmx
*vmx
)
5439 u32 pin_based_exec_ctrl
= vmcs_config
.pin_based_exec_ctrl
;
5441 if (!kvm_vcpu_apicv_active(&vmx
->vcpu
))
5442 pin_based_exec_ctrl
&= ~PIN_BASED_POSTED_INTR
;
5445 pin_based_exec_ctrl
&= ~PIN_BASED_VIRTUAL_NMIS
;
5447 /* Enable the preemption timer dynamically */
5448 pin_based_exec_ctrl
&= ~PIN_BASED_VMX_PREEMPTION_TIMER
;
5449 return pin_based_exec_ctrl
;
5452 static void vmx_refresh_apicv_exec_ctrl(struct kvm_vcpu
*vcpu
)
5454 struct vcpu_vmx
*vmx
= to_vmx(vcpu
);
5456 vmcs_write32(PIN_BASED_VM_EXEC_CONTROL
, vmx_pin_based_exec_ctrl(vmx
));
5457 if (cpu_has_secondary_exec_ctrls()) {
5458 if (kvm_vcpu_apicv_active(vcpu
))
5459 vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL
,
5460 SECONDARY_EXEC_APIC_REGISTER_VIRT
|
5461 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY
);
5463 vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL
,
5464 SECONDARY_EXEC_APIC_REGISTER_VIRT
|
5465 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY
);
5468 if (cpu_has_vmx_msr_bitmap())
5469 vmx_update_msr_bitmap(vcpu
);
5472 static u32
vmx_exec_control(struct vcpu_vmx
*vmx
)
5474 u32 exec_control
= vmcs_config
.cpu_based_exec_ctrl
;
5476 if (vmx
->vcpu
.arch
.switch_db_regs
& KVM_DEBUGREG_WONT_EXIT
)
5477 exec_control
&= ~CPU_BASED_MOV_DR_EXITING
;
5479 if (!cpu_need_tpr_shadow(&vmx
->vcpu
)) {
5480 exec_control
&= ~CPU_BASED_TPR_SHADOW
;
5481 #ifdef CONFIG_X86_64
5482 exec_control
|= CPU_BASED_CR8_STORE_EXITING
|
5483 CPU_BASED_CR8_LOAD_EXITING
;
5487 exec_control
|= CPU_BASED_CR3_STORE_EXITING
|
5488 CPU_BASED_CR3_LOAD_EXITING
|
5489 CPU_BASED_INVLPG_EXITING
;
5490 return exec_control
;
5493 static bool vmx_rdrand_supported(void)
5495 return vmcs_config
.cpu_based_2nd_exec_ctrl
&
5496 SECONDARY_EXEC_RDRAND_EXITING
;
5499 static bool vmx_rdseed_supported(void)
5501 return vmcs_config
.cpu_based_2nd_exec_ctrl
&
5502 SECONDARY_EXEC_RDSEED_EXITING
;
5505 static void vmx_compute_secondary_exec_control(struct vcpu_vmx
*vmx
)
5507 struct kvm_vcpu
*vcpu
= &vmx
->vcpu
;
5509 u32 exec_control
= vmcs_config
.cpu_based_2nd_exec_ctrl
;
5511 if (!cpu_need_virtualize_apic_accesses(vcpu
))
5512 exec_control
&= ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES
;
5514 exec_control
&= ~SECONDARY_EXEC_ENABLE_VPID
;
5516 exec_control
&= ~SECONDARY_EXEC_ENABLE_EPT
;
5517 enable_unrestricted_guest
= 0;
5518 /* Enable INVPCID for non-ept guests may cause performance regression. */
5519 exec_control
&= ~SECONDARY_EXEC_ENABLE_INVPCID
;
5521 if (!enable_unrestricted_guest
)
5522 exec_control
&= ~SECONDARY_EXEC_UNRESTRICTED_GUEST
;
5524 exec_control
&= ~SECONDARY_EXEC_PAUSE_LOOP_EXITING
;
5525 if (!kvm_vcpu_apicv_active(vcpu
))
5526 exec_control
&= ~(SECONDARY_EXEC_APIC_REGISTER_VIRT
|
5527 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY
);
5528 exec_control
&= ~SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE
;
5530 /* SECONDARY_EXEC_DESC is enabled/disabled on writes to CR4.UMIP,
5531 * in vmx_set_cr4. */
5532 exec_control
&= ~SECONDARY_EXEC_DESC
;
5534 /* SECONDARY_EXEC_SHADOW_VMCS is enabled when L1 executes VMPTRLD
5536 We can NOT enable shadow_vmcs here because we don't have yet
5539 exec_control
&= ~SECONDARY_EXEC_SHADOW_VMCS
;
5542 exec_control
&= ~SECONDARY_EXEC_ENABLE_PML
;
5544 if (vmx_xsaves_supported()) {
5545 /* Exposing XSAVES only when XSAVE is exposed */
5546 bool xsaves_enabled
=
5547 guest_cpuid_has(vcpu
, X86_FEATURE_XSAVE
) &&
5548 guest_cpuid_has(vcpu
, X86_FEATURE_XSAVES
);
5550 if (!xsaves_enabled
)
5551 exec_control
&= ~SECONDARY_EXEC_XSAVES
;
5555 vmx
->nested
.nested_vmx_secondary_ctls_high
|=
5556 SECONDARY_EXEC_XSAVES
;
5558 vmx
->nested
.nested_vmx_secondary_ctls_high
&=
5559 ~SECONDARY_EXEC_XSAVES
;
5563 if (vmx_rdtscp_supported()) {
5564 bool rdtscp_enabled
= guest_cpuid_has(vcpu
, X86_FEATURE_RDTSCP
);
5565 if (!rdtscp_enabled
)
5566 exec_control
&= ~SECONDARY_EXEC_RDTSCP
;
5570 vmx
->nested
.nested_vmx_secondary_ctls_high
|=
5571 SECONDARY_EXEC_RDTSCP
;
5573 vmx
->nested
.nested_vmx_secondary_ctls_high
&=
5574 ~SECONDARY_EXEC_RDTSCP
;
5578 if (vmx_invpcid_supported()) {
5579 /* Exposing INVPCID only when PCID is exposed */
5580 bool invpcid_enabled
=
5581 guest_cpuid_has(vcpu
, X86_FEATURE_INVPCID
) &&
5582 guest_cpuid_has(vcpu
, X86_FEATURE_PCID
);
5584 if (!invpcid_enabled
) {
5585 exec_control
&= ~SECONDARY_EXEC_ENABLE_INVPCID
;
5586 guest_cpuid_clear(vcpu
, X86_FEATURE_INVPCID
);
5590 if (invpcid_enabled
)
5591 vmx
->nested
.nested_vmx_secondary_ctls_high
|=
5592 SECONDARY_EXEC_ENABLE_INVPCID
;
5594 vmx
->nested
.nested_vmx_secondary_ctls_high
&=
5595 ~SECONDARY_EXEC_ENABLE_INVPCID
;
5599 if (vmx_rdrand_supported()) {
5600 bool rdrand_enabled
= guest_cpuid_has(vcpu
, X86_FEATURE_RDRAND
);
5602 exec_control
&= ~SECONDARY_EXEC_RDRAND_EXITING
;
5606 vmx
->nested
.nested_vmx_secondary_ctls_high
|=
5607 SECONDARY_EXEC_RDRAND_EXITING
;
5609 vmx
->nested
.nested_vmx_secondary_ctls_high
&=
5610 ~SECONDARY_EXEC_RDRAND_EXITING
;
5614 if (vmx_rdseed_supported()) {
5615 bool rdseed_enabled
= guest_cpuid_has(vcpu
, X86_FEATURE_RDSEED
);
5617 exec_control
&= ~SECONDARY_EXEC_RDSEED_EXITING
;
5621 vmx
->nested
.nested_vmx_secondary_ctls_high
|=
5622 SECONDARY_EXEC_RDSEED_EXITING
;
5624 vmx
->nested
.nested_vmx_secondary_ctls_high
&=
5625 ~SECONDARY_EXEC_RDSEED_EXITING
;
5629 vmx
->secondary_exec_control
= exec_control
;
5632 static void ept_set_mmio_spte_mask(void)
5635 * EPT Misconfigurations can be generated if the value of bits 2:0
5636 * of an EPT paging-structure entry is 110b (write/execute).
5638 kvm_mmu_set_mmio_spte_mask(VMX_EPT_RWX_MASK
,
5639 VMX_EPT_MISCONFIG_WX_VALUE
);
5642 #define VMX_XSS_EXIT_BITMAP 0
5644 * Sets up the vmcs for emulated real mode.
5646 static void vmx_vcpu_setup(struct vcpu_vmx
*vmx
)
5648 #ifdef CONFIG_X86_64
5653 if (enable_shadow_vmcs
) {
5654 vmcs_write64(VMREAD_BITMAP
, __pa(vmx_vmread_bitmap
));
5655 vmcs_write64(VMWRITE_BITMAP
, __pa(vmx_vmwrite_bitmap
));
5657 if (cpu_has_vmx_msr_bitmap())
5658 vmcs_write64(MSR_BITMAP
, __pa(vmx
->vmcs01
.msr_bitmap
));
5660 vmcs_write64(VMCS_LINK_POINTER
, -1ull); /* 22.3.1.5 */
5663 vmcs_write32(PIN_BASED_VM_EXEC_CONTROL
, vmx_pin_based_exec_ctrl(vmx
));
5664 vmx
->hv_deadline_tsc
= -1;
5666 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL
, vmx_exec_control(vmx
));
5668 if (cpu_has_secondary_exec_ctrls()) {
5669 vmx_compute_secondary_exec_control(vmx
);
5670 vmcs_write32(SECONDARY_VM_EXEC_CONTROL
,
5671 vmx
->secondary_exec_control
);
5674 if (kvm_vcpu_apicv_active(&vmx
->vcpu
)) {
5675 vmcs_write64(EOI_EXIT_BITMAP0
, 0);
5676 vmcs_write64(EOI_EXIT_BITMAP1
, 0);
5677 vmcs_write64(EOI_EXIT_BITMAP2
, 0);
5678 vmcs_write64(EOI_EXIT_BITMAP3
, 0);
5680 vmcs_write16(GUEST_INTR_STATUS
, 0);
5682 vmcs_write16(POSTED_INTR_NV
, POSTED_INTR_VECTOR
);
5683 vmcs_write64(POSTED_INTR_DESC_ADDR
, __pa((&vmx
->pi_desc
)));
5687 vmcs_write32(PLE_GAP
, ple_gap
);
5688 vmx
->ple_window
= ple_window
;
5689 vmx
->ple_window_dirty
= true;
5692 vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK
, 0);
5693 vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH
, 0);
5694 vmcs_write32(CR3_TARGET_COUNT
, 0); /* 22.2.1 */
5696 vmcs_write16(HOST_FS_SELECTOR
, 0); /* 22.2.4 */
5697 vmcs_write16(HOST_GS_SELECTOR
, 0); /* 22.2.4 */
5698 vmx_set_constant_host_state(vmx
);
5699 #ifdef CONFIG_X86_64
5700 rdmsrl(MSR_FS_BASE
, a
);
5701 vmcs_writel(HOST_FS_BASE
, a
); /* 22.2.4 */
5702 rdmsrl(MSR_GS_BASE
, a
);
5703 vmcs_writel(HOST_GS_BASE
, a
); /* 22.2.4 */
5705 vmcs_writel(HOST_FS_BASE
, 0); /* 22.2.4 */
5706 vmcs_writel(HOST_GS_BASE
, 0); /* 22.2.4 */
5709 if (cpu_has_vmx_vmfunc())
5710 vmcs_write64(VM_FUNCTION_CONTROL
, 0);
5712 vmcs_write32(VM_EXIT_MSR_STORE_COUNT
, 0);
5713 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT
, 0);
5714 vmcs_write64(VM_EXIT_MSR_LOAD_ADDR
, __pa(vmx
->msr_autoload
.host
));
5715 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT
, 0);
5716 vmcs_write64(VM_ENTRY_MSR_LOAD_ADDR
, __pa(vmx
->msr_autoload
.guest
));
5718 if (vmcs_config
.vmentry_ctrl
& VM_ENTRY_LOAD_IA32_PAT
)
5719 vmcs_write64(GUEST_IA32_PAT
, vmx
->vcpu
.arch
.pat
);
5721 for (i
= 0; i
< ARRAY_SIZE(vmx_msr_index
); ++i
) {
5722 u32 index
= vmx_msr_index
[i
];
5723 u32 data_low
, data_high
;
5726 if (rdmsr_safe(index
, &data_low
, &data_high
) < 0)
5728 if (wrmsr_safe(index
, data_low
, data_high
) < 0)
5730 vmx
->guest_msrs
[j
].index
= i
;
5731 vmx
->guest_msrs
[j
].data
= 0;
5732 vmx
->guest_msrs
[j
].mask
= -1ull;
5736 if (boot_cpu_has(X86_FEATURE_ARCH_CAPABILITIES
))
5737 rdmsrl(MSR_IA32_ARCH_CAPABILITIES
, vmx
->arch_capabilities
);
5739 vm_exit_controls_init(vmx
, vmcs_config
.vmexit_ctrl
);
5741 /* 22.2.1, 20.8.1 */
5742 vm_entry_controls_init(vmx
, vmcs_config
.vmentry_ctrl
);
5744 vmx
->vcpu
.arch
.cr0_guest_owned_bits
= X86_CR0_TS
;
5745 vmcs_writel(CR0_GUEST_HOST_MASK
, ~X86_CR0_TS
);
5747 set_cr4_guest_host_mask(vmx
);
5749 if (vmx_xsaves_supported())
5750 vmcs_write64(XSS_EXIT_BITMAP
, VMX_XSS_EXIT_BITMAP
);
5753 ASSERT(vmx
->pml_pg
);
5754 vmcs_write64(PML_ADDRESS
, page_to_phys(vmx
->pml_pg
));
5755 vmcs_write16(GUEST_PML_INDEX
, PML_ENTITY_NUM
- 1);
5759 static void vmx_vcpu_reset(struct kvm_vcpu
*vcpu
, bool init_event
)
5761 struct vcpu_vmx
*vmx
= to_vmx(vcpu
);
5762 struct msr_data apic_base_msr
;
5765 vmx
->rmode
.vm86_active
= 0;
5768 vmx
->vcpu
.arch
.regs
[VCPU_REGS_RDX
] = get_rdx_init_val();
5769 kvm_set_cr8(vcpu
, 0);
5772 apic_base_msr
.data
= APIC_DEFAULT_PHYS_BASE
|
5773 MSR_IA32_APICBASE_ENABLE
;
5774 if (kvm_vcpu_is_reset_bsp(vcpu
))
5775 apic_base_msr
.data
|= MSR_IA32_APICBASE_BSP
;
5776 apic_base_msr
.host_initiated
= true;
5777 kvm_set_apic_base(vcpu
, &apic_base_msr
);
5780 vmx_segment_cache_clear(vmx
);
5782 seg_setup(VCPU_SREG_CS
);
5783 vmcs_write16(GUEST_CS_SELECTOR
, 0xf000);
5784 vmcs_writel(GUEST_CS_BASE
, 0xffff0000ul
);
5786 seg_setup(VCPU_SREG_DS
);
5787 seg_setup(VCPU_SREG_ES
);
5788 seg_setup(VCPU_SREG_FS
);
5789 seg_setup(VCPU_SREG_GS
);
5790 seg_setup(VCPU_SREG_SS
);
5792 vmcs_write16(GUEST_TR_SELECTOR
, 0);
5793 vmcs_writel(GUEST_TR_BASE
, 0);
5794 vmcs_write32(GUEST_TR_LIMIT
, 0xffff);
5795 vmcs_write32(GUEST_TR_AR_BYTES
, 0x008b);
5797 vmcs_write16(GUEST_LDTR_SELECTOR
, 0);
5798 vmcs_writel(GUEST_LDTR_BASE
, 0);
5799 vmcs_write32(GUEST_LDTR_LIMIT
, 0xffff);
5800 vmcs_write32(GUEST_LDTR_AR_BYTES
, 0x00082);
5803 vmcs_write32(GUEST_SYSENTER_CS
, 0);
5804 vmcs_writel(GUEST_SYSENTER_ESP
, 0);
5805 vmcs_writel(GUEST_SYSENTER_EIP
, 0);
5806 vmcs_write64(GUEST_IA32_DEBUGCTL
, 0);
5809 kvm_set_rflags(vcpu
, X86_EFLAGS_FIXED
);
5810 kvm_rip_write(vcpu
, 0xfff0);
5812 vmcs_writel(GUEST_GDTR_BASE
, 0);
5813 vmcs_write32(GUEST_GDTR_LIMIT
, 0xffff);
5815 vmcs_writel(GUEST_IDTR_BASE
, 0);
5816 vmcs_write32(GUEST_IDTR_LIMIT
, 0xffff);
5818 vmcs_write32(GUEST_ACTIVITY_STATE
, GUEST_ACTIVITY_ACTIVE
);
5819 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO
, 0);
5820 vmcs_writel(GUEST_PENDING_DBG_EXCEPTIONS
, 0);
5821 if (kvm_mpx_supported())
5822 vmcs_write64(GUEST_BNDCFGS
, 0);
5826 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD
, 0); /* 22.2.1 */
5828 if (cpu_has_vmx_tpr_shadow() && !init_event
) {
5829 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR
, 0);
5830 if (cpu_need_tpr_shadow(vcpu
))
5831 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR
,
5832 __pa(vcpu
->arch
.apic
->regs
));
5833 vmcs_write32(TPR_THRESHOLD
, 0);
5836 kvm_make_request(KVM_REQ_APIC_PAGE_RELOAD
, vcpu
);
5839 vmcs_write16(VIRTUAL_PROCESSOR_ID
, vmx
->vpid
);
5841 cr0
= X86_CR0_NW
| X86_CR0_CD
| X86_CR0_ET
;
5842 vmx
->vcpu
.arch
.cr0
= cr0
;
5843 vmx_set_cr0(vcpu
, cr0
); /* enter rmode */
5844 vmx_set_cr4(vcpu
, 0);
5845 vmx_set_efer(vcpu
, 0);
5847 update_exception_bitmap(vcpu
);
5849 vpid_sync_context(vmx
->vpid
);
5853 * In nested virtualization, check if L1 asked to exit on external interrupts.
5854 * For most existing hypervisors, this will always return true.
5856 static bool nested_exit_on_intr(struct kvm_vcpu
*vcpu
)
5858 return get_vmcs12(vcpu
)->pin_based_vm_exec_control
&
5859 PIN_BASED_EXT_INTR_MASK
;
5863 * In nested virtualization, check if L1 has set
5864 * VM_EXIT_ACK_INTR_ON_EXIT
5866 static bool nested_exit_intr_ack_set(struct kvm_vcpu
*vcpu
)
5868 return get_vmcs12(vcpu
)->vm_exit_controls
&
5869 VM_EXIT_ACK_INTR_ON_EXIT
;
5872 static bool nested_exit_on_nmi(struct kvm_vcpu
*vcpu
)
5874 return get_vmcs12(vcpu
)->pin_based_vm_exec_control
&
5875 PIN_BASED_NMI_EXITING
;
5878 static void enable_irq_window(struct kvm_vcpu
*vcpu
)
5880 vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL
,
5881 CPU_BASED_VIRTUAL_INTR_PENDING
);
5884 static void enable_nmi_window(struct kvm_vcpu
*vcpu
)
5887 vmcs_read32(GUEST_INTERRUPTIBILITY_INFO
) & GUEST_INTR_STATE_STI
) {
5888 enable_irq_window(vcpu
);
5892 vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL
,
5893 CPU_BASED_VIRTUAL_NMI_PENDING
);
5896 static void vmx_inject_irq(struct kvm_vcpu
*vcpu
)
5898 struct vcpu_vmx
*vmx
= to_vmx(vcpu
);
5900 int irq
= vcpu
->arch
.interrupt
.nr
;
5902 trace_kvm_inj_virq(irq
);
5904 ++vcpu
->stat
.irq_injections
;
5905 if (vmx
->rmode
.vm86_active
) {
5907 if (vcpu
->arch
.interrupt
.soft
)
5908 inc_eip
= vcpu
->arch
.event_exit_inst_len
;
5909 if (kvm_inject_realmode_interrupt(vcpu
, irq
, inc_eip
) != EMULATE_DONE
)
5910 kvm_make_request(KVM_REQ_TRIPLE_FAULT
, vcpu
);
5913 intr
= irq
| INTR_INFO_VALID_MASK
;
5914 if (vcpu
->arch
.interrupt
.soft
) {
5915 intr
|= INTR_TYPE_SOFT_INTR
;
5916 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN
,
5917 vmx
->vcpu
.arch
.event_exit_inst_len
);
5919 intr
|= INTR_TYPE_EXT_INTR
;
5920 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD
, intr
);
5923 static void vmx_inject_nmi(struct kvm_vcpu
*vcpu
)
5925 struct vcpu_vmx
*vmx
= to_vmx(vcpu
);
5929 * Tracking the NMI-blocked state in software is built upon
5930 * finding the next open IRQ window. This, in turn, depends on
5931 * well-behaving guests: They have to keep IRQs disabled at
5932 * least as long as the NMI handler runs. Otherwise we may
5933 * cause NMI nesting, maybe breaking the guest. But as this is
5934 * highly unlikely, we can live with the residual risk.
5936 vmx
->loaded_vmcs
->soft_vnmi_blocked
= 1;
5937 vmx
->loaded_vmcs
->vnmi_blocked_time
= 0;
5940 ++vcpu
->stat
.nmi_injections
;
5941 vmx
->loaded_vmcs
->nmi_known_unmasked
= false;
5943 if (vmx
->rmode
.vm86_active
) {
5944 if (kvm_inject_realmode_interrupt(vcpu
, NMI_VECTOR
, 0) != EMULATE_DONE
)
5945 kvm_make_request(KVM_REQ_TRIPLE_FAULT
, vcpu
);
5949 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD
,
5950 INTR_TYPE_NMI_INTR
| INTR_INFO_VALID_MASK
| NMI_VECTOR
);
5953 static bool vmx_get_nmi_mask(struct kvm_vcpu
*vcpu
)
5955 struct vcpu_vmx
*vmx
= to_vmx(vcpu
);
5959 return vmx
->loaded_vmcs
->soft_vnmi_blocked
;
5960 if (vmx
->loaded_vmcs
->nmi_known_unmasked
)
5962 masked
= vmcs_read32(GUEST_INTERRUPTIBILITY_INFO
) & GUEST_INTR_STATE_NMI
;
5963 vmx
->loaded_vmcs
->nmi_known_unmasked
= !masked
;
5967 static void vmx_set_nmi_mask(struct kvm_vcpu
*vcpu
, bool masked
)
5969 struct vcpu_vmx
*vmx
= to_vmx(vcpu
);
5972 if (vmx
->loaded_vmcs
->soft_vnmi_blocked
!= masked
) {
5973 vmx
->loaded_vmcs
->soft_vnmi_blocked
= masked
;
5974 vmx
->loaded_vmcs
->vnmi_blocked_time
= 0;
5977 vmx
->loaded_vmcs
->nmi_known_unmasked
= !masked
;
5979 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO
,
5980 GUEST_INTR_STATE_NMI
);
5982 vmcs_clear_bits(GUEST_INTERRUPTIBILITY_INFO
,
5983 GUEST_INTR_STATE_NMI
);
5987 static int vmx_nmi_allowed(struct kvm_vcpu
*vcpu
)
5989 if (to_vmx(vcpu
)->nested
.nested_run_pending
)
5993 to_vmx(vcpu
)->loaded_vmcs
->soft_vnmi_blocked
)
5996 return !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO
) &
5997 (GUEST_INTR_STATE_MOV_SS
| GUEST_INTR_STATE_STI
5998 | GUEST_INTR_STATE_NMI
));
6001 static int vmx_interrupt_allowed(struct kvm_vcpu
*vcpu
)
6003 return (!to_vmx(vcpu
)->nested
.nested_run_pending
&&
6004 vmcs_readl(GUEST_RFLAGS
) & X86_EFLAGS_IF
) &&
6005 !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO
) &
6006 (GUEST_INTR_STATE_STI
| GUEST_INTR_STATE_MOV_SS
));
6009 static int vmx_set_tss_addr(struct kvm
*kvm
, unsigned int addr
)
6013 ret
= x86_set_memory_region(kvm
, TSS_PRIVATE_MEMSLOT
, addr
,
6017 kvm
->arch
.tss_addr
= addr
;
6018 return init_rmode_tss(kvm
);
6021 static bool rmode_exception(struct kvm_vcpu
*vcpu
, int vec
)
6026 * Update instruction length as we may reinject the exception
6027 * from user space while in guest debugging mode.
6029 to_vmx(vcpu
)->vcpu
.arch
.event_exit_inst_len
=
6030 vmcs_read32(VM_EXIT_INSTRUCTION_LEN
);
6031 if (vcpu
->guest_debug
& KVM_GUESTDBG_USE_SW_BP
)
6035 if (vcpu
->guest_debug
&
6036 (KVM_GUESTDBG_SINGLESTEP
| KVM_GUESTDBG_USE_HW_BP
))
6053 static int handle_rmode_exception(struct kvm_vcpu
*vcpu
,
6054 int vec
, u32 err_code
)
6057 * Instruction with address size override prefix opcode 0x67
6058 * Cause the #SS fault with 0 error code in VM86 mode.
6060 if (((vec
== GP_VECTOR
) || (vec
== SS_VECTOR
)) && err_code
== 0) {
6061 if (emulate_instruction(vcpu
, 0) == EMULATE_DONE
) {
6062 if (vcpu
->arch
.halt_request
) {
6063 vcpu
->arch
.halt_request
= 0;
6064 return kvm_vcpu_halt(vcpu
);
6072 * Forward all other exceptions that are valid in real mode.
6073 * FIXME: Breaks guest debugging in real mode, needs to be fixed with
6074 * the required debugging infrastructure rework.
6076 kvm_queue_exception(vcpu
, vec
);
6081 * Trigger machine check on the host. We assume all the MSRs are already set up
6082 * by the CPU and that we still run on the same CPU as the MCE occurred on.
6083 * We pass a fake environment to the machine check handler because we want
6084 * the guest to be always treated like user space, no matter what context
6085 * it used internally.
6087 static void kvm_machine_check(void)
6089 #if defined(CONFIG_X86_MCE) && defined(CONFIG_X86_64)
6090 struct pt_regs regs
= {
6091 .cs
= 3, /* Fake ring 3 no matter what the guest ran on */
6092 .flags
= X86_EFLAGS_IF
,
6095 do_machine_check(®s
, 0);
6099 static int handle_machine_check(struct kvm_vcpu
*vcpu
)
6101 /* already handled by vcpu_run */
6105 static int handle_exception(struct kvm_vcpu
*vcpu
)
6107 struct vcpu_vmx
*vmx
= to_vmx(vcpu
);
6108 struct kvm_run
*kvm_run
= vcpu
->run
;
6109 u32 intr_info
, ex_no
, error_code
;
6110 unsigned long cr2
, rip
, dr6
;
6112 enum emulation_result er
;
6114 vect_info
= vmx
->idt_vectoring_info
;
6115 intr_info
= vmx
->exit_intr_info
;
6117 if (is_machine_check(intr_info
))
6118 return handle_machine_check(vcpu
);
6120 if (is_nmi(intr_info
))
6121 return 1; /* already handled by vmx_vcpu_run() */
6123 if (is_invalid_opcode(intr_info
)) {
6124 er
= emulate_instruction(vcpu
, EMULTYPE_TRAP_UD
);
6125 if (er
== EMULATE_USER_EXIT
)
6127 if (er
!= EMULATE_DONE
)
6128 kvm_queue_exception(vcpu
, UD_VECTOR
);
6133 if (intr_info
& INTR_INFO_DELIVER_CODE_MASK
)
6134 error_code
= vmcs_read32(VM_EXIT_INTR_ERROR_CODE
);
6137 * The #PF with PFEC.RSVD = 1 indicates the guest is accessing
6138 * MMIO, it is better to report an internal error.
6139 * See the comments in vmx_handle_exit.
6141 if ((vect_info
& VECTORING_INFO_VALID_MASK
) &&
6142 !(is_page_fault(intr_info
) && !(error_code
& PFERR_RSVD_MASK
))) {
6143 vcpu
->run
->exit_reason
= KVM_EXIT_INTERNAL_ERROR
;
6144 vcpu
->run
->internal
.suberror
= KVM_INTERNAL_ERROR_SIMUL_EX
;
6145 vcpu
->run
->internal
.ndata
= 3;
6146 vcpu
->run
->internal
.data
[0] = vect_info
;
6147 vcpu
->run
->internal
.data
[1] = intr_info
;
6148 vcpu
->run
->internal
.data
[2] = error_code
;
6152 if (is_page_fault(intr_info
)) {
6153 cr2
= vmcs_readl(EXIT_QUALIFICATION
);
6154 /* EPT won't cause page fault directly */
6155 WARN_ON_ONCE(!vcpu
->arch
.apf
.host_apf_reason
&& enable_ept
);
6156 return kvm_handle_page_fault(vcpu
, error_code
, cr2
, NULL
, 0);
6159 ex_no
= intr_info
& INTR_INFO_VECTOR_MASK
;
6161 if (vmx
->rmode
.vm86_active
&& rmode_exception(vcpu
, ex_no
))
6162 return handle_rmode_exception(vcpu
, ex_no
, error_code
);
6166 kvm_queue_exception_e(vcpu
, AC_VECTOR
, error_code
);
6169 dr6
= vmcs_readl(EXIT_QUALIFICATION
);
6170 if (!(vcpu
->guest_debug
&
6171 (KVM_GUESTDBG_SINGLESTEP
| KVM_GUESTDBG_USE_HW_BP
))) {
6172 vcpu
->arch
.dr6
&= ~15;
6173 vcpu
->arch
.dr6
|= dr6
| DR6_RTM
;
6174 if (!(dr6
& ~DR6_RESERVED
)) /* icebp */
6175 skip_emulated_instruction(vcpu
);
6177 kvm_queue_exception(vcpu
, DB_VECTOR
);
6180 kvm_run
->debug
.arch
.dr6
= dr6
| DR6_FIXED_1
;
6181 kvm_run
->debug
.arch
.dr7
= vmcs_readl(GUEST_DR7
);
6185 * Update instruction length as we may reinject #BP from
6186 * user space while in guest debugging mode. Reading it for
6187 * #DB as well causes no harm, it is not used in that case.
6189 vmx
->vcpu
.arch
.event_exit_inst_len
=
6190 vmcs_read32(VM_EXIT_INSTRUCTION_LEN
);
6191 kvm_run
->exit_reason
= KVM_EXIT_DEBUG
;
6192 rip
= kvm_rip_read(vcpu
);
6193 kvm_run
->debug
.arch
.pc
= vmcs_readl(GUEST_CS_BASE
) + rip
;
6194 kvm_run
->debug
.arch
.exception
= ex_no
;
6197 kvm_run
->exit_reason
= KVM_EXIT_EXCEPTION
;
6198 kvm_run
->ex
.exception
= ex_no
;
6199 kvm_run
->ex
.error_code
= error_code
;
6205 static int handle_external_interrupt(struct kvm_vcpu
*vcpu
)
6207 ++vcpu
->stat
.irq_exits
;
6211 static int handle_triple_fault(struct kvm_vcpu
*vcpu
)
6213 vcpu
->run
->exit_reason
= KVM_EXIT_SHUTDOWN
;
6214 vcpu
->mmio_needed
= 0;
6218 static int handle_io(struct kvm_vcpu
*vcpu
)
6220 unsigned long exit_qualification
;
6221 int size
, in
, string
, ret
;
6224 exit_qualification
= vmcs_readl(EXIT_QUALIFICATION
);
6225 string
= (exit_qualification
& 16) != 0;
6226 in
= (exit_qualification
& 8) != 0;
6228 ++vcpu
->stat
.io_exits
;
6231 return emulate_instruction(vcpu
, 0) == EMULATE_DONE
;
6233 port
= exit_qualification
>> 16;
6234 size
= (exit_qualification
& 7) + 1;
6236 ret
= kvm_skip_emulated_instruction(vcpu
);
6239 * TODO: we might be squashing a KVM_GUESTDBG_SINGLESTEP-triggered
6240 * KVM_EXIT_DEBUG here.
6242 return kvm_fast_pio_out(vcpu
, size
, port
) && ret
;
6246 vmx_patch_hypercall(struct kvm_vcpu
*vcpu
, unsigned char *hypercall
)
6249 * Patch in the VMCALL instruction:
6251 hypercall
[0] = 0x0f;
6252 hypercall
[1] = 0x01;
6253 hypercall
[2] = 0xc1;
6256 /* called to set cr0 as appropriate for a mov-to-cr0 exit. */
6257 static int handle_set_cr0(struct kvm_vcpu
*vcpu
, unsigned long val
)
6259 if (is_guest_mode(vcpu
)) {
6260 struct vmcs12
*vmcs12
= get_vmcs12(vcpu
);
6261 unsigned long orig_val
= val
;
6264 * We get here when L2 changed cr0 in a way that did not change
6265 * any of L1's shadowed bits (see nested_vmx_exit_handled_cr),
6266 * but did change L0 shadowed bits. So we first calculate the
6267 * effective cr0 value that L1 would like to write into the
6268 * hardware. It consists of the L2-owned bits from the new
6269 * value combined with the L1-owned bits from L1's guest_cr0.
6271 val
= (val
& ~vmcs12
->cr0_guest_host_mask
) |
6272 (vmcs12
->guest_cr0
& vmcs12
->cr0_guest_host_mask
);
6274 if (!nested_guest_cr0_valid(vcpu
, val
))
6277 if (kvm_set_cr0(vcpu
, val
))
6279 vmcs_writel(CR0_READ_SHADOW
, orig_val
);
6282 if (to_vmx(vcpu
)->nested
.vmxon
&&
6283 !nested_host_cr0_valid(vcpu
, val
))
6286 return kvm_set_cr0(vcpu
, val
);
6290 static int handle_set_cr4(struct kvm_vcpu
*vcpu
, unsigned long val
)
6292 if (is_guest_mode(vcpu
)) {
6293 struct vmcs12
*vmcs12
= get_vmcs12(vcpu
);
6294 unsigned long orig_val
= val
;
6296 /* analogously to handle_set_cr0 */
6297 val
= (val
& ~vmcs12
->cr4_guest_host_mask
) |
6298 (vmcs12
->guest_cr4
& vmcs12
->cr4_guest_host_mask
);
6299 if (kvm_set_cr4(vcpu
, val
))
6301 vmcs_writel(CR4_READ_SHADOW
, orig_val
);
6304 return kvm_set_cr4(vcpu
, val
);
6307 static int handle_desc(struct kvm_vcpu
*vcpu
)
6309 WARN_ON(!(vcpu
->arch
.cr4
& X86_CR4_UMIP
));
6310 return emulate_instruction(vcpu
, 0) == EMULATE_DONE
;
6313 static int handle_cr(struct kvm_vcpu
*vcpu
)
6315 unsigned long exit_qualification
, val
;
6321 exit_qualification
= vmcs_readl(EXIT_QUALIFICATION
);
6322 cr
= exit_qualification
& 15;
6323 reg
= (exit_qualification
>> 8) & 15;
6324 switch ((exit_qualification
>> 4) & 3) {
6325 case 0: /* mov to cr */
6326 val
= kvm_register_readl(vcpu
, reg
);
6327 trace_kvm_cr_write(cr
, val
);
6330 err
= handle_set_cr0(vcpu
, val
);
6331 return kvm_complete_insn_gp(vcpu
, err
);
6333 err
= kvm_set_cr3(vcpu
, val
);
6334 return kvm_complete_insn_gp(vcpu
, err
);
6336 err
= handle_set_cr4(vcpu
, val
);
6337 return kvm_complete_insn_gp(vcpu
, err
);
6339 u8 cr8_prev
= kvm_get_cr8(vcpu
);
6341 err
= kvm_set_cr8(vcpu
, cr8
);
6342 ret
= kvm_complete_insn_gp(vcpu
, err
);
6343 if (lapic_in_kernel(vcpu
))
6345 if (cr8_prev
<= cr8
)
6348 * TODO: we might be squashing a
6349 * KVM_GUESTDBG_SINGLESTEP-triggered
6350 * KVM_EXIT_DEBUG here.
6352 vcpu
->run
->exit_reason
= KVM_EXIT_SET_TPR
;
6358 WARN_ONCE(1, "Guest should always own CR0.TS");
6359 vmx_set_cr0(vcpu
, kvm_read_cr0_bits(vcpu
, ~X86_CR0_TS
));
6360 trace_kvm_cr_write(0, kvm_read_cr0(vcpu
));
6361 return kvm_skip_emulated_instruction(vcpu
);
6362 case 1: /*mov from cr*/
6365 val
= kvm_read_cr3(vcpu
);
6366 kvm_register_write(vcpu
, reg
, val
);
6367 trace_kvm_cr_read(cr
, val
);
6368 return kvm_skip_emulated_instruction(vcpu
);
6370 val
= kvm_get_cr8(vcpu
);
6371 kvm_register_write(vcpu
, reg
, val
);
6372 trace_kvm_cr_read(cr
, val
);
6373 return kvm_skip_emulated_instruction(vcpu
);
6377 val
= (exit_qualification
>> LMSW_SOURCE_DATA_SHIFT
) & 0x0f;
6378 trace_kvm_cr_write(0, (kvm_read_cr0(vcpu
) & ~0xful
) | val
);
6379 kvm_lmsw(vcpu
, val
);
6381 return kvm_skip_emulated_instruction(vcpu
);
6385 vcpu
->run
->exit_reason
= 0;
6386 vcpu_unimpl(vcpu
, "unhandled control register: op %d cr %d\n",
6387 (int)(exit_qualification
>> 4) & 3, cr
);
6391 static int handle_dr(struct kvm_vcpu
*vcpu
)
6393 unsigned long exit_qualification
;
6396 exit_qualification
= vmcs_readl(EXIT_QUALIFICATION
);
6397 dr
= exit_qualification
& DEBUG_REG_ACCESS_NUM
;
6399 /* First, if DR does not exist, trigger UD */
6400 if (!kvm_require_dr(vcpu
, dr
))
6403 /* Do not handle if the CPL > 0, will trigger GP on re-entry */
6404 if (!kvm_require_cpl(vcpu
, 0))
6406 dr7
= vmcs_readl(GUEST_DR7
);
6409 * As the vm-exit takes precedence over the debug trap, we
6410 * need to emulate the latter, either for the host or the
6411 * guest debugging itself.
6413 if (vcpu
->guest_debug
& KVM_GUESTDBG_USE_HW_BP
) {
6414 vcpu
->run
->debug
.arch
.dr6
= vcpu
->arch
.dr6
;
6415 vcpu
->run
->debug
.arch
.dr7
= dr7
;
6416 vcpu
->run
->debug
.arch
.pc
= kvm_get_linear_rip(vcpu
);
6417 vcpu
->run
->debug
.arch
.exception
= DB_VECTOR
;
6418 vcpu
->run
->exit_reason
= KVM_EXIT_DEBUG
;
6421 vcpu
->arch
.dr6
&= ~15;
6422 vcpu
->arch
.dr6
|= DR6_BD
| DR6_RTM
;
6423 kvm_queue_exception(vcpu
, DB_VECTOR
);
6428 if (vcpu
->guest_debug
== 0) {
6429 vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL
,
6430 CPU_BASED_MOV_DR_EXITING
);
6433 * No more DR vmexits; force a reload of the debug registers
6434 * and reenter on this instruction. The next vmexit will
6435 * retrieve the full state of the debug registers.
6437 vcpu
->arch
.switch_db_regs
|= KVM_DEBUGREG_WONT_EXIT
;
6441 reg
= DEBUG_REG_ACCESS_REG(exit_qualification
);
6442 if (exit_qualification
& TYPE_MOV_FROM_DR
) {
6445 if (kvm_get_dr(vcpu
, dr
, &val
))
6447 kvm_register_write(vcpu
, reg
, val
);
6449 if (kvm_set_dr(vcpu
, dr
, kvm_register_readl(vcpu
, reg
)))
6452 return kvm_skip_emulated_instruction(vcpu
);
6455 static u64
vmx_get_dr6(struct kvm_vcpu
*vcpu
)
6457 return vcpu
->arch
.dr6
;
6460 static void vmx_set_dr6(struct kvm_vcpu
*vcpu
, unsigned long val
)
6464 static void vmx_sync_dirty_debug_regs(struct kvm_vcpu
*vcpu
)
6466 get_debugreg(vcpu
->arch
.db
[0], 0);
6467 get_debugreg(vcpu
->arch
.db
[1], 1);
6468 get_debugreg(vcpu
->arch
.db
[2], 2);
6469 get_debugreg(vcpu
->arch
.db
[3], 3);
6470 get_debugreg(vcpu
->arch
.dr6
, 6);
6471 vcpu
->arch
.dr7
= vmcs_readl(GUEST_DR7
);
6473 vcpu
->arch
.switch_db_regs
&= ~KVM_DEBUGREG_WONT_EXIT
;
6474 vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL
, CPU_BASED_MOV_DR_EXITING
);
6477 static void vmx_set_dr7(struct kvm_vcpu
*vcpu
, unsigned long val
)
6479 vmcs_writel(GUEST_DR7
, val
);
6482 static int handle_cpuid(struct kvm_vcpu
*vcpu
)
6484 return kvm_emulate_cpuid(vcpu
);
6487 static int handle_rdmsr(struct kvm_vcpu
*vcpu
)
6489 u32 ecx
= vcpu
->arch
.regs
[VCPU_REGS_RCX
];
6490 struct msr_data msr_info
;
6492 msr_info
.index
= ecx
;
6493 msr_info
.host_initiated
= false;
6494 if (vmx_get_msr(vcpu
, &msr_info
)) {
6495 trace_kvm_msr_read_ex(ecx
);
6496 kvm_inject_gp(vcpu
, 0);
6500 trace_kvm_msr_read(ecx
, msr_info
.data
);
6502 /* FIXME: handling of bits 32:63 of rax, rdx */
6503 vcpu
->arch
.regs
[VCPU_REGS_RAX
] = msr_info
.data
& -1u;
6504 vcpu
->arch
.regs
[VCPU_REGS_RDX
] = (msr_info
.data
>> 32) & -1u;
6505 return kvm_skip_emulated_instruction(vcpu
);
6508 static int handle_wrmsr(struct kvm_vcpu
*vcpu
)
6510 struct msr_data msr
;
6511 u32 ecx
= vcpu
->arch
.regs
[VCPU_REGS_RCX
];
6512 u64 data
= (vcpu
->arch
.regs
[VCPU_REGS_RAX
] & -1u)
6513 | ((u64
)(vcpu
->arch
.regs
[VCPU_REGS_RDX
] & -1u) << 32);
6517 msr
.host_initiated
= false;
6518 if (kvm_set_msr(vcpu
, &msr
) != 0) {
6519 trace_kvm_msr_write_ex(ecx
, data
);
6520 kvm_inject_gp(vcpu
, 0);
6524 trace_kvm_msr_write(ecx
, data
);
6525 return kvm_skip_emulated_instruction(vcpu
);
6528 static int handle_tpr_below_threshold(struct kvm_vcpu
*vcpu
)
6530 kvm_apic_update_ppr(vcpu
);
6534 static int handle_interrupt_window(struct kvm_vcpu
*vcpu
)
6536 vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL
,
6537 CPU_BASED_VIRTUAL_INTR_PENDING
);
6539 kvm_make_request(KVM_REQ_EVENT
, vcpu
);
6541 ++vcpu
->stat
.irq_window_exits
;
6545 static int handle_halt(struct kvm_vcpu
*vcpu
)
6547 return kvm_emulate_halt(vcpu
);
6550 static int handle_vmcall(struct kvm_vcpu
*vcpu
)
6552 return kvm_emulate_hypercall(vcpu
);
6555 static int handle_invd(struct kvm_vcpu
*vcpu
)
6557 return emulate_instruction(vcpu
, 0) == EMULATE_DONE
;
6560 static int handle_invlpg(struct kvm_vcpu
*vcpu
)
6562 unsigned long exit_qualification
= vmcs_readl(EXIT_QUALIFICATION
);
6564 kvm_mmu_invlpg(vcpu
, exit_qualification
);
6565 return kvm_skip_emulated_instruction(vcpu
);
6568 static int handle_rdpmc(struct kvm_vcpu
*vcpu
)
6572 err
= kvm_rdpmc(vcpu
);
6573 return kvm_complete_insn_gp(vcpu
, err
);
6576 static int handle_wbinvd(struct kvm_vcpu
*vcpu
)
6578 return kvm_emulate_wbinvd(vcpu
);
6581 static int handle_xsetbv(struct kvm_vcpu
*vcpu
)
6583 u64 new_bv
= kvm_read_edx_eax(vcpu
);
6584 u32 index
= kvm_register_read(vcpu
, VCPU_REGS_RCX
);
6586 if (kvm_set_xcr(vcpu
, index
, new_bv
) == 0)
6587 return kvm_skip_emulated_instruction(vcpu
);
6591 static int handle_xsaves(struct kvm_vcpu
*vcpu
)
6593 kvm_skip_emulated_instruction(vcpu
);
6594 WARN(1, "this should never happen\n");
6598 static int handle_xrstors(struct kvm_vcpu
*vcpu
)
6600 kvm_skip_emulated_instruction(vcpu
);
6601 WARN(1, "this should never happen\n");
6605 static int handle_apic_access(struct kvm_vcpu
*vcpu
)
6607 if (likely(fasteoi
)) {
6608 unsigned long exit_qualification
= vmcs_readl(EXIT_QUALIFICATION
);
6609 int access_type
, offset
;
6611 access_type
= exit_qualification
& APIC_ACCESS_TYPE
;
6612 offset
= exit_qualification
& APIC_ACCESS_OFFSET
;
6614 * Sane guest uses MOV to write EOI, with written value
6615 * not cared. So make a short-circuit here by avoiding
6616 * heavy instruction emulation.
6618 if ((access_type
== TYPE_LINEAR_APIC_INST_WRITE
) &&
6619 (offset
== APIC_EOI
)) {
6620 kvm_lapic_set_eoi(vcpu
);
6621 return kvm_skip_emulated_instruction(vcpu
);
6624 return emulate_instruction(vcpu
, 0) == EMULATE_DONE
;
6627 static int handle_apic_eoi_induced(struct kvm_vcpu
*vcpu
)
6629 unsigned long exit_qualification
= vmcs_readl(EXIT_QUALIFICATION
);
6630 int vector
= exit_qualification
& 0xff;
6632 /* EOI-induced VM exit is trap-like and thus no need to adjust IP */
6633 kvm_apic_set_eoi_accelerated(vcpu
, vector
);
6637 static int handle_apic_write(struct kvm_vcpu
*vcpu
)
6639 unsigned long exit_qualification
= vmcs_readl(EXIT_QUALIFICATION
);
6640 u32 offset
= exit_qualification
& 0xfff;
6642 /* APIC-write VM exit is trap-like and thus no need to adjust IP */
6643 kvm_apic_write_nodecode(vcpu
, offset
);
6647 static int handle_task_switch(struct kvm_vcpu
*vcpu
)
6649 struct vcpu_vmx
*vmx
= to_vmx(vcpu
);
6650 unsigned long exit_qualification
;
6651 bool has_error_code
= false;
6654 int reason
, type
, idt_v
, idt_index
;
6656 idt_v
= (vmx
->idt_vectoring_info
& VECTORING_INFO_VALID_MASK
);
6657 idt_index
= (vmx
->idt_vectoring_info
& VECTORING_INFO_VECTOR_MASK
);
6658 type
= (vmx
->idt_vectoring_info
& VECTORING_INFO_TYPE_MASK
);
6660 exit_qualification
= vmcs_readl(EXIT_QUALIFICATION
);
6662 reason
= (u32
)exit_qualification
>> 30;
6663 if (reason
== TASK_SWITCH_GATE
&& idt_v
) {
6665 case INTR_TYPE_NMI_INTR
:
6666 vcpu
->arch
.nmi_injected
= false;
6667 vmx_set_nmi_mask(vcpu
, true);
6669 case INTR_TYPE_EXT_INTR
:
6670 case INTR_TYPE_SOFT_INTR
:
6671 kvm_clear_interrupt_queue(vcpu
);
6673 case INTR_TYPE_HARD_EXCEPTION
:
6674 if (vmx
->idt_vectoring_info
&
6675 VECTORING_INFO_DELIVER_CODE_MASK
) {
6676 has_error_code
= true;
6678 vmcs_read32(IDT_VECTORING_ERROR_CODE
);
6681 case INTR_TYPE_SOFT_EXCEPTION
:
6682 kvm_clear_exception_queue(vcpu
);
6688 tss_selector
= exit_qualification
;
6690 if (!idt_v
|| (type
!= INTR_TYPE_HARD_EXCEPTION
&&
6691 type
!= INTR_TYPE_EXT_INTR
&&
6692 type
!= INTR_TYPE_NMI_INTR
))
6693 skip_emulated_instruction(vcpu
);
6695 if (kvm_task_switch(vcpu
, tss_selector
,
6696 type
== INTR_TYPE_SOFT_INTR
? idt_index
: -1, reason
,
6697 has_error_code
, error_code
) == EMULATE_FAIL
) {
6698 vcpu
->run
->exit_reason
= KVM_EXIT_INTERNAL_ERROR
;
6699 vcpu
->run
->internal
.suberror
= KVM_INTERNAL_ERROR_EMULATION
;
6700 vcpu
->run
->internal
.ndata
= 0;
6705 * TODO: What about debug traps on tss switch?
6706 * Are we supposed to inject them and update dr6?
6712 static int handle_ept_violation(struct kvm_vcpu
*vcpu
)
6714 unsigned long exit_qualification
;
6718 exit_qualification
= vmcs_readl(EXIT_QUALIFICATION
);
6721 * EPT violation happened while executing iret from NMI,
6722 * "blocked by NMI" bit has to be set before next VM entry.
6723 * There are errata that may cause this bit to not be set:
6726 if (!(to_vmx(vcpu
)->idt_vectoring_info
& VECTORING_INFO_VALID_MASK
) &&
6728 (exit_qualification
& INTR_INFO_UNBLOCK_NMI
))
6729 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO
, GUEST_INTR_STATE_NMI
);
6731 gpa
= vmcs_read64(GUEST_PHYSICAL_ADDRESS
);
6732 trace_kvm_page_fault(gpa
, exit_qualification
);
6734 /* Is it a read fault? */
6735 error_code
= (exit_qualification
& EPT_VIOLATION_ACC_READ
)
6736 ? PFERR_USER_MASK
: 0;
6737 /* Is it a write fault? */
6738 error_code
|= (exit_qualification
& EPT_VIOLATION_ACC_WRITE
)
6739 ? PFERR_WRITE_MASK
: 0;
6740 /* Is it a fetch fault? */
6741 error_code
|= (exit_qualification
& EPT_VIOLATION_ACC_INSTR
)
6742 ? PFERR_FETCH_MASK
: 0;
6743 /* ept page table entry is present? */
6744 error_code
|= (exit_qualification
&
6745 (EPT_VIOLATION_READABLE
| EPT_VIOLATION_WRITABLE
|
6746 EPT_VIOLATION_EXECUTABLE
))
6747 ? PFERR_PRESENT_MASK
: 0;
6749 error_code
|= (exit_qualification
& 0x100) != 0 ?
6750 PFERR_GUEST_FINAL_MASK
: PFERR_GUEST_PAGE_MASK
;
6752 vcpu
->arch
.exit_qualification
= exit_qualification
;
6753 return kvm_mmu_page_fault(vcpu
, gpa
, error_code
, NULL
, 0);
6756 static int handle_ept_misconfig(struct kvm_vcpu
*vcpu
)
6762 * A nested guest cannot optimize MMIO vmexits, because we have an
6763 * nGPA here instead of the required GPA.
6765 gpa
= vmcs_read64(GUEST_PHYSICAL_ADDRESS
);
6766 if (!is_guest_mode(vcpu
) &&
6767 !kvm_io_bus_write(vcpu
, KVM_FAST_MMIO_BUS
, gpa
, 0, NULL
)) {
6768 trace_kvm_fast_mmio(gpa
);
6770 * Doing kvm_skip_emulated_instruction() depends on undefined
6771 * behavior: Intel's manual doesn't mandate
6772 * VM_EXIT_INSTRUCTION_LEN to be set in VMCS when EPT MISCONFIG
6773 * occurs and while on real hardware it was observed to be set,
6774 * other hypervisors (namely Hyper-V) don't set it, we end up
6775 * advancing IP with some random value. Disable fast mmio when
6776 * running nested and keep it for real hardware in hope that
6777 * VM_EXIT_INSTRUCTION_LEN will always be set correctly.
6779 if (!static_cpu_has(X86_FEATURE_HYPERVISOR
))
6780 return kvm_skip_emulated_instruction(vcpu
);
6782 return x86_emulate_instruction(vcpu
, gpa
, EMULTYPE_SKIP
,
6783 NULL
, 0) == EMULATE_DONE
;
6786 ret
= kvm_mmu_page_fault(vcpu
, gpa
, PFERR_RSVD_MASK
, NULL
, 0);
6790 /* It is the real ept misconfig */
6793 vcpu
->run
->exit_reason
= KVM_EXIT_UNKNOWN
;
6794 vcpu
->run
->hw
.hardware_exit_reason
= EXIT_REASON_EPT_MISCONFIG
;
6799 static int handle_nmi_window(struct kvm_vcpu
*vcpu
)
6801 WARN_ON_ONCE(!enable_vnmi
);
6802 vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL
,
6803 CPU_BASED_VIRTUAL_NMI_PENDING
);
6804 ++vcpu
->stat
.nmi_window_exits
;
6805 kvm_make_request(KVM_REQ_EVENT
, vcpu
);
6810 static int handle_invalid_guest_state(struct kvm_vcpu
*vcpu
)
6812 struct vcpu_vmx
*vmx
= to_vmx(vcpu
);
6813 enum emulation_result err
= EMULATE_DONE
;
6816 bool intr_window_requested
;
6817 unsigned count
= 130;
6819 cpu_exec_ctrl
= vmcs_read32(CPU_BASED_VM_EXEC_CONTROL
);
6820 intr_window_requested
= cpu_exec_ctrl
& CPU_BASED_VIRTUAL_INTR_PENDING
;
6822 while (vmx
->emulation_required
&& count
-- != 0) {
6823 if (intr_window_requested
&& vmx_interrupt_allowed(vcpu
))
6824 return handle_interrupt_window(&vmx
->vcpu
);
6826 if (kvm_test_request(KVM_REQ_EVENT
, vcpu
))
6829 err
= emulate_instruction(vcpu
, 0);
6831 if (err
== EMULATE_USER_EXIT
) {
6832 ++vcpu
->stat
.mmio_exits
;
6837 if (err
!= EMULATE_DONE
) {
6838 vcpu
->run
->exit_reason
= KVM_EXIT_INTERNAL_ERROR
;
6839 vcpu
->run
->internal
.suberror
= KVM_INTERNAL_ERROR_EMULATION
;
6840 vcpu
->run
->internal
.ndata
= 0;
6844 if (vcpu
->arch
.halt_request
) {
6845 vcpu
->arch
.halt_request
= 0;
6846 ret
= kvm_vcpu_halt(vcpu
);
6850 if (signal_pending(current
))
6860 static int __grow_ple_window(int val
)
6862 if (ple_window_grow
< 1)
6865 val
= min(val
, ple_window_actual_max
);
6867 if (ple_window_grow
< ple_window
)
6868 val
*= ple_window_grow
;
6870 val
+= ple_window_grow
;
6875 static int __shrink_ple_window(int val
, int modifier
, int minimum
)
6880 if (modifier
< ple_window
)
6885 return max(val
, minimum
);
6888 static void grow_ple_window(struct kvm_vcpu
*vcpu
)
6890 struct vcpu_vmx
*vmx
= to_vmx(vcpu
);
6891 int old
= vmx
->ple_window
;
6893 vmx
->ple_window
= __grow_ple_window(old
);
6895 if (vmx
->ple_window
!= old
)
6896 vmx
->ple_window_dirty
= true;
6898 trace_kvm_ple_window_grow(vcpu
->vcpu_id
, vmx
->ple_window
, old
);
6901 static void shrink_ple_window(struct kvm_vcpu
*vcpu
)
6903 struct vcpu_vmx
*vmx
= to_vmx(vcpu
);
6904 int old
= vmx
->ple_window
;
6906 vmx
->ple_window
= __shrink_ple_window(old
,
6907 ple_window_shrink
, ple_window
);
6909 if (vmx
->ple_window
!= old
)
6910 vmx
->ple_window_dirty
= true;
6912 trace_kvm_ple_window_shrink(vcpu
->vcpu_id
, vmx
->ple_window
, old
);
6916 * ple_window_actual_max is computed to be one grow_ple_window() below
6917 * ple_window_max. (See __grow_ple_window for the reason.)
6918 * This prevents overflows, because ple_window_max is int.
6919 * ple_window_max effectively rounded down to a multiple of ple_window_grow in
6921 * ple_window_max is also prevented from setting vmx->ple_window < ple_window.
6923 static void update_ple_window_actual_max(void)
6925 ple_window_actual_max
=
6926 __shrink_ple_window(max(ple_window_max
, ple_window
),
6927 ple_window_grow
, INT_MIN
);
6931 * Handler for POSTED_INTERRUPT_WAKEUP_VECTOR.
6933 static void wakeup_handler(void)
6935 struct kvm_vcpu
*vcpu
;
6936 int cpu
= smp_processor_id();
6938 spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock
, cpu
));
6939 list_for_each_entry(vcpu
, &per_cpu(blocked_vcpu_on_cpu
, cpu
),
6940 blocked_vcpu_list
) {
6941 struct pi_desc
*pi_desc
= vcpu_to_pi_desc(vcpu
);
6943 if (pi_test_on(pi_desc
) == 1)
6944 kvm_vcpu_kick(vcpu
);
6946 spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock
, cpu
));
6949 void vmx_enable_tdp(void)
6951 kvm_mmu_set_mask_ptes(VMX_EPT_READABLE_MASK
,
6952 enable_ept_ad_bits
? VMX_EPT_ACCESS_BIT
: 0ull,
6953 enable_ept_ad_bits
? VMX_EPT_DIRTY_BIT
: 0ull,
6954 0ull, VMX_EPT_EXECUTABLE_MASK
,
6955 cpu_has_vmx_ept_execute_only() ? 0ull : VMX_EPT_READABLE_MASK
,
6956 VMX_EPT_RWX_MASK
, 0ull);
6958 ept_set_mmio_spte_mask();
6962 static __init
int hardware_setup(void)
6966 rdmsrl_safe(MSR_EFER
, &host_efer
);
6968 for (i
= 0; i
< ARRAY_SIZE(vmx_msr_index
); ++i
)
6969 kvm_define_shared_msr(i
, vmx_msr_index
[i
]);
6971 for (i
= 0; i
< VMX_BITMAP_NR
; i
++) {
6972 vmx_bitmap
[i
] = (unsigned long *)__get_free_page(GFP_KERNEL
);
6977 memset(vmx_vmread_bitmap
, 0xff, PAGE_SIZE
);
6978 memset(vmx_vmwrite_bitmap
, 0xff, PAGE_SIZE
);
6980 if (setup_vmcs_config(&vmcs_config
) < 0) {
6985 if (boot_cpu_has(X86_FEATURE_NX
))
6986 kvm_enable_efer_bits(EFER_NX
);
6988 if (!cpu_has_vmx_vpid() || !cpu_has_vmx_invvpid() ||
6989 !(cpu_has_vmx_invvpid_single() || cpu_has_vmx_invvpid_global()))
6992 if (!cpu_has_vmx_ept() ||
6993 !cpu_has_vmx_ept_4levels() ||
6994 !cpu_has_vmx_ept_mt_wb() ||
6995 !cpu_has_vmx_invept_global())
6998 if (!cpu_has_vmx_ept_ad_bits() || !enable_ept
)
6999 enable_ept_ad_bits
= 0;
7001 if (!cpu_has_vmx_unrestricted_guest() || !enable_ept
)
7002 enable_unrestricted_guest
= 0;
7004 if (!cpu_has_vmx_flexpriority())
7005 flexpriority_enabled
= 0;
7007 if (!cpu_has_virtual_nmis())
7011 * set_apic_access_page_addr() is used to reload apic access
7012 * page upon invalidation. No need to do anything if not
7013 * using the APIC_ACCESS_ADDR VMCS field.
7015 if (!flexpriority_enabled
)
7016 kvm_x86_ops
->set_apic_access_page_addr
= NULL
;
7018 if (!cpu_has_vmx_tpr_shadow())
7019 kvm_x86_ops
->update_cr8_intercept
= NULL
;
7021 if (enable_ept
&& !cpu_has_vmx_ept_2m_page())
7022 kvm_disable_largepages();
7024 if (!cpu_has_vmx_ple()) {
7027 ple_window_grow
= 0;
7029 ple_window_shrink
= 0;
7032 if (!cpu_has_vmx_apicv()) {
7034 kvm_x86_ops
->sync_pir_to_irr
= NULL
;
7037 if (cpu_has_vmx_tsc_scaling()) {
7038 kvm_has_tsc_control
= true;
7039 kvm_max_tsc_scaling_ratio
= KVM_VMX_TSC_MULTIPLIER_MAX
;
7040 kvm_tsc_scaling_ratio_frac_bits
= 48;
7043 set_bit(0, vmx_vpid_bitmap
); /* 0 is reserved for host */
7050 update_ple_window_actual_max();
7053 * Only enable PML when hardware supports PML feature, and both EPT
7054 * and EPT A/D bit features are enabled -- PML depends on them to work.
7056 if (!enable_ept
|| !enable_ept_ad_bits
|| !cpu_has_vmx_pml())
7060 kvm_x86_ops
->slot_enable_log_dirty
= NULL
;
7061 kvm_x86_ops
->slot_disable_log_dirty
= NULL
;
7062 kvm_x86_ops
->flush_log_dirty
= NULL
;
7063 kvm_x86_ops
->enable_log_dirty_pt_masked
= NULL
;
7066 if (cpu_has_vmx_preemption_timer() && enable_preemption_timer
) {
7069 rdmsrl(MSR_IA32_VMX_MISC
, vmx_msr
);
7070 cpu_preemption_timer_multi
=
7071 vmx_msr
& VMX_MISC_PREEMPTION_TIMER_RATE_MASK
;
7073 kvm_x86_ops
->set_hv_timer
= NULL
;
7074 kvm_x86_ops
->cancel_hv_timer
= NULL
;
7077 if (!cpu_has_vmx_shadow_vmcs())
7078 enable_shadow_vmcs
= 0;
7079 if (enable_shadow_vmcs
)
7080 init_vmcs_shadow_fields();
7082 kvm_set_posted_intr_wakeup_handler(wakeup_handler
);
7084 kvm_mce_cap_supported
|= MCG_LMCE_P
;
7086 return alloc_kvm_area();
7089 for (i
= 0; i
< VMX_BITMAP_NR
; i
++)
7090 free_page((unsigned long)vmx_bitmap
[i
]);
7095 static __exit
void hardware_unsetup(void)
7099 for (i
= 0; i
< VMX_BITMAP_NR
; i
++)
7100 free_page((unsigned long)vmx_bitmap
[i
]);
7106 * Indicate a busy-waiting vcpu in spinlock. We do not enable the PAUSE
7107 * exiting, so only get here on cpu with PAUSE-Loop-Exiting.
7109 static int handle_pause(struct kvm_vcpu
*vcpu
)
7112 grow_ple_window(vcpu
);
7115 * Intel sdm vol3 ch-25.1.3 says: The "PAUSE-loop exiting"
7116 * VM-execution control is ignored if CPL > 0. OTOH, KVM
7117 * never set PAUSE_EXITING and just set PLE if supported,
7118 * so the vcpu must be CPL=0 if it gets a PAUSE exit.
7120 kvm_vcpu_on_spin(vcpu
, true);
7121 return kvm_skip_emulated_instruction(vcpu
);
7124 static int handle_nop(struct kvm_vcpu
*vcpu
)
7126 return kvm_skip_emulated_instruction(vcpu
);
7129 static int handle_mwait(struct kvm_vcpu
*vcpu
)
7131 printk_once(KERN_WARNING
"kvm: MWAIT instruction emulated as NOP!\n");
7132 return handle_nop(vcpu
);
7135 static int handle_invalid_op(struct kvm_vcpu
*vcpu
)
7137 kvm_queue_exception(vcpu
, UD_VECTOR
);
7141 static int handle_monitor_trap(struct kvm_vcpu
*vcpu
)
7146 static int handle_monitor(struct kvm_vcpu
*vcpu
)
7148 printk_once(KERN_WARNING
"kvm: MONITOR instruction emulated as NOP!\n");
7149 return handle_nop(vcpu
);
7153 * The following 3 functions, nested_vmx_succeed()/failValid()/failInvalid(),
7154 * set the success or error code of an emulated VMX instruction, as specified
7155 * by Vol 2B, VMX Instruction Reference, "Conventions".
7157 static void nested_vmx_succeed(struct kvm_vcpu
*vcpu
)
7159 vmx_set_rflags(vcpu
, vmx_get_rflags(vcpu
)
7160 & ~(X86_EFLAGS_CF
| X86_EFLAGS_PF
| X86_EFLAGS_AF
|
7161 X86_EFLAGS_ZF
| X86_EFLAGS_SF
| X86_EFLAGS_OF
));
7164 static void nested_vmx_failInvalid(struct kvm_vcpu
*vcpu
)
7166 vmx_set_rflags(vcpu
, (vmx_get_rflags(vcpu
)
7167 & ~(X86_EFLAGS_PF
| X86_EFLAGS_AF
| X86_EFLAGS_ZF
|
7168 X86_EFLAGS_SF
| X86_EFLAGS_OF
))
7172 static void nested_vmx_failValid(struct kvm_vcpu
*vcpu
,
7173 u32 vm_instruction_error
)
7175 if (to_vmx(vcpu
)->nested
.current_vmptr
== -1ull) {
7177 * failValid writes the error number to the current VMCS, which
7178 * can't be done there isn't a current VMCS.
7180 nested_vmx_failInvalid(vcpu
);
7183 vmx_set_rflags(vcpu
, (vmx_get_rflags(vcpu
)
7184 & ~(X86_EFLAGS_CF
| X86_EFLAGS_PF
| X86_EFLAGS_AF
|
7185 X86_EFLAGS_SF
| X86_EFLAGS_OF
))
7187 get_vmcs12(vcpu
)->vm_instruction_error
= vm_instruction_error
;
7189 * We don't need to force a shadow sync because
7190 * VM_INSTRUCTION_ERROR is not shadowed
7194 static void nested_vmx_abort(struct kvm_vcpu
*vcpu
, u32 indicator
)
7196 /* TODO: not to reset guest simply here. */
7197 kvm_make_request(KVM_REQ_TRIPLE_FAULT
, vcpu
);
7198 pr_debug_ratelimited("kvm: nested vmx abort, indicator %d\n", indicator
);
7201 static enum hrtimer_restart
vmx_preemption_timer_fn(struct hrtimer
*timer
)
7203 struct vcpu_vmx
*vmx
=
7204 container_of(timer
, struct vcpu_vmx
, nested
.preemption_timer
);
7206 vmx
->nested
.preemption_timer_expired
= true;
7207 kvm_make_request(KVM_REQ_EVENT
, &vmx
->vcpu
);
7208 kvm_vcpu_kick(&vmx
->vcpu
);
7210 return HRTIMER_NORESTART
;
7214 * Decode the memory-address operand of a vmx instruction, as recorded on an
7215 * exit caused by such an instruction (run by a guest hypervisor).
7216 * On success, returns 0. When the operand is invalid, returns 1 and throws
7219 static int get_vmx_mem_address(struct kvm_vcpu
*vcpu
,
7220 unsigned long exit_qualification
,
7221 u32 vmx_instruction_info
, bool wr
, gva_t
*ret
)
7225 struct kvm_segment s
;
7228 * According to Vol. 3B, "Information for VM Exits Due to Instruction
7229 * Execution", on an exit, vmx_instruction_info holds most of the
7230 * addressing components of the operand. Only the displacement part
7231 * is put in exit_qualification (see 3B, "Basic VM-Exit Information").
7232 * For how an actual address is calculated from all these components,
7233 * refer to Vol. 1, "Operand Addressing".
7235 int scaling
= vmx_instruction_info
& 3;
7236 int addr_size
= (vmx_instruction_info
>> 7) & 7;
7237 bool is_reg
= vmx_instruction_info
& (1u << 10);
7238 int seg_reg
= (vmx_instruction_info
>> 15) & 7;
7239 int index_reg
= (vmx_instruction_info
>> 18) & 0xf;
7240 bool index_is_valid
= !(vmx_instruction_info
& (1u << 22));
7241 int base_reg
= (vmx_instruction_info
>> 23) & 0xf;
7242 bool base_is_valid
= !(vmx_instruction_info
& (1u << 27));
7245 kvm_queue_exception(vcpu
, UD_VECTOR
);
7249 /* Addr = segment_base + offset */
7250 /* offset = base + [index * scale] + displacement */
7251 off
= exit_qualification
; /* holds the displacement */
7253 off
+= kvm_register_read(vcpu
, base_reg
);
7255 off
+= kvm_register_read(vcpu
, index_reg
)<<scaling
;
7256 vmx_get_segment(vcpu
, &s
, seg_reg
);
7257 *ret
= s
.base
+ off
;
7259 if (addr_size
== 1) /* 32 bit */
7262 /* Checks for #GP/#SS exceptions. */
7264 if (is_long_mode(vcpu
)) {
7265 /* Long mode: #GP(0)/#SS(0) if the memory address is in a
7266 * non-canonical form. This is the only check on the memory
7267 * destination for long mode!
7269 exn
= is_noncanonical_address(*ret
, vcpu
);
7270 } else if (is_protmode(vcpu
)) {
7271 /* Protected mode: apply checks for segment validity in the
7273 * - segment type check (#GP(0) may be thrown)
7274 * - usability check (#GP(0)/#SS(0))
7275 * - limit check (#GP(0)/#SS(0))
7278 /* #GP(0) if the destination operand is located in a
7279 * read-only data segment or any code segment.
7281 exn
= ((s
.type
& 0xa) == 0 || (s
.type
& 8));
7283 /* #GP(0) if the source operand is located in an
7284 * execute-only code segment
7286 exn
= ((s
.type
& 0xa) == 8);
7288 kvm_queue_exception_e(vcpu
, GP_VECTOR
, 0);
7291 /* Protected mode: #GP(0)/#SS(0) if the segment is unusable.
7293 exn
= (s
.unusable
!= 0);
7294 /* Protected mode: #GP(0)/#SS(0) if the memory
7295 * operand is outside the segment limit.
7297 exn
= exn
|| (off
+ sizeof(u64
) > s
.limit
);
7300 kvm_queue_exception_e(vcpu
,
7301 seg_reg
== VCPU_SREG_SS
?
7302 SS_VECTOR
: GP_VECTOR
,
7310 static int nested_vmx_get_vmptr(struct kvm_vcpu
*vcpu
, gpa_t
*vmpointer
)
7313 struct x86_exception e
;
7315 if (get_vmx_mem_address(vcpu
, vmcs_readl(EXIT_QUALIFICATION
),
7316 vmcs_read32(VMX_INSTRUCTION_INFO
), false, &gva
))
7319 if (kvm_read_guest_virt(&vcpu
->arch
.emulate_ctxt
, gva
, vmpointer
,
7320 sizeof(*vmpointer
), &e
)) {
7321 kvm_inject_page_fault(vcpu
, &e
);
7328 static int enter_vmx_operation(struct kvm_vcpu
*vcpu
)
7330 struct vcpu_vmx
*vmx
= to_vmx(vcpu
);
7331 struct vmcs
*shadow_vmcs
;
7334 r
= alloc_loaded_vmcs(&vmx
->nested
.vmcs02
);
7338 vmx
->nested
.cached_vmcs12
= kmalloc(VMCS12_SIZE
, GFP_KERNEL
);
7339 if (!vmx
->nested
.cached_vmcs12
)
7340 goto out_cached_vmcs12
;
7342 if (enable_shadow_vmcs
) {
7343 shadow_vmcs
= alloc_vmcs();
7345 goto out_shadow_vmcs
;
7346 /* mark vmcs as shadow */
7347 shadow_vmcs
->revision_id
|= (1u << 31);
7348 /* init shadow vmcs */
7349 vmcs_clear(shadow_vmcs
);
7350 vmx
->vmcs01
.shadow_vmcs
= shadow_vmcs
;
7353 hrtimer_init(&vmx
->nested
.preemption_timer
, CLOCK_MONOTONIC
,
7354 HRTIMER_MODE_REL_PINNED
);
7355 vmx
->nested
.preemption_timer
.function
= vmx_preemption_timer_fn
;
7357 vmx
->nested
.vmxon
= true;
7361 kfree(vmx
->nested
.cached_vmcs12
);
7364 free_loaded_vmcs(&vmx
->nested
.vmcs02
);
7371 * Emulate the VMXON instruction.
7372 * Currently, we just remember that VMX is active, and do not save or even
7373 * inspect the argument to VMXON (the so-called "VMXON pointer") because we
7374 * do not currently need to store anything in that guest-allocated memory
7375 * region. Consequently, VMCLEAR and VMPTRLD also do not verify that the their
7376 * argument is different from the VMXON pointer (which the spec says they do).
7378 static int handle_vmon(struct kvm_vcpu
*vcpu
)
7383 struct vcpu_vmx
*vmx
= to_vmx(vcpu
);
7384 const u64 VMXON_NEEDED_FEATURES
= FEATURE_CONTROL_LOCKED
7385 | FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX
;
7388 * The Intel VMX Instruction Reference lists a bunch of bits that are
7389 * prerequisite to running VMXON, most notably cr4.VMXE must be set to
7390 * 1 (see vmx_set_cr4() for when we allow the guest to set this).
7391 * Otherwise, we should fail with #UD. But most faulting conditions
7392 * have already been checked by hardware, prior to the VM-exit for
7393 * VMXON. We do test guest cr4.VMXE because processor CR4 always has
7394 * that bit set to 1 in non-root mode.
7396 if (!kvm_read_cr4_bits(vcpu
, X86_CR4_VMXE
)) {
7397 kvm_queue_exception(vcpu
, UD_VECTOR
);
7401 if (vmx
->nested
.vmxon
) {
7402 nested_vmx_failValid(vcpu
, VMXERR_VMXON_IN_VMX_ROOT_OPERATION
);
7403 return kvm_skip_emulated_instruction(vcpu
);
7406 if ((vmx
->msr_ia32_feature_control
& VMXON_NEEDED_FEATURES
)
7407 != VMXON_NEEDED_FEATURES
) {
7408 kvm_inject_gp(vcpu
, 0);
7412 if (nested_vmx_get_vmptr(vcpu
, &vmptr
))
7417 * The first 4 bytes of VMXON region contain the supported
7418 * VMCS revision identifier
7420 * Note - IA32_VMX_BASIC[48] will never be 1 for the nested case;
7421 * which replaces physical address width with 32
7423 if (!PAGE_ALIGNED(vmptr
) || (vmptr
>> cpuid_maxphyaddr(vcpu
))) {
7424 nested_vmx_failInvalid(vcpu
);
7425 return kvm_skip_emulated_instruction(vcpu
);
7428 page
= kvm_vcpu_gpa_to_page(vcpu
, vmptr
);
7429 if (is_error_page(page
)) {
7430 nested_vmx_failInvalid(vcpu
);
7431 return kvm_skip_emulated_instruction(vcpu
);
7433 if (*(u32
*)kmap(page
) != VMCS12_REVISION
) {
7435 kvm_release_page_clean(page
);
7436 nested_vmx_failInvalid(vcpu
);
7437 return kvm_skip_emulated_instruction(vcpu
);
7440 kvm_release_page_clean(page
);
7442 vmx
->nested
.vmxon_ptr
= vmptr
;
7443 ret
= enter_vmx_operation(vcpu
);
7447 nested_vmx_succeed(vcpu
);
7448 return kvm_skip_emulated_instruction(vcpu
);
7452 * Intel's VMX Instruction Reference specifies a common set of prerequisites
7453 * for running VMX instructions (except VMXON, whose prerequisites are
7454 * slightly different). It also specifies what exception to inject otherwise.
7455 * Note that many of these exceptions have priority over VM exits, so they
7456 * don't have to be checked again here.
7458 static int nested_vmx_check_permission(struct kvm_vcpu
*vcpu
)
7460 if (!to_vmx(vcpu
)->nested
.vmxon
) {
7461 kvm_queue_exception(vcpu
, UD_VECTOR
);
7467 static void vmx_disable_shadow_vmcs(struct vcpu_vmx
*vmx
)
7469 vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL
, SECONDARY_EXEC_SHADOW_VMCS
);
7470 vmcs_write64(VMCS_LINK_POINTER
, -1ull);
7473 static inline void nested_release_vmcs12(struct vcpu_vmx
*vmx
)
7475 if (vmx
->nested
.current_vmptr
== -1ull)
7478 if (enable_shadow_vmcs
) {
7479 /* copy to memory all shadowed fields in case
7480 they were modified */
7481 copy_shadow_to_vmcs12(vmx
);
7482 vmx
->nested
.sync_shadow_vmcs
= false;
7483 vmx_disable_shadow_vmcs(vmx
);
7485 vmx
->nested
.posted_intr_nv
= -1;
7487 /* Flush VMCS12 to guest memory */
7488 kvm_vcpu_write_guest_page(&vmx
->vcpu
,
7489 vmx
->nested
.current_vmptr
>> PAGE_SHIFT
,
7490 vmx
->nested
.cached_vmcs12
, 0, VMCS12_SIZE
);
7492 vmx
->nested
.current_vmptr
= -1ull;
7496 * Free whatever needs to be freed from vmx->nested when L1 goes down, or
7497 * just stops using VMX.
7499 static void free_nested(struct vcpu_vmx
*vmx
)
7501 if (!vmx
->nested
.vmxon
&& !vmx
->nested
.smm
.vmxon
)
7504 vmx
->nested
.vmxon
= false;
7505 vmx
->nested
.smm
.vmxon
= false;
7506 free_vpid(vmx
->nested
.vpid02
);
7507 vmx
->nested
.posted_intr_nv
= -1;
7508 vmx
->nested
.current_vmptr
= -1ull;
7509 if (enable_shadow_vmcs
) {
7510 vmx_disable_shadow_vmcs(vmx
);
7511 vmcs_clear(vmx
->vmcs01
.shadow_vmcs
);
7512 free_vmcs(vmx
->vmcs01
.shadow_vmcs
);
7513 vmx
->vmcs01
.shadow_vmcs
= NULL
;
7515 kfree(vmx
->nested
.cached_vmcs12
);
7516 /* Unpin physical memory we referred to in the vmcs02 */
7517 if (vmx
->nested
.apic_access_page
) {
7518 kvm_release_page_dirty(vmx
->nested
.apic_access_page
);
7519 vmx
->nested
.apic_access_page
= NULL
;
7521 if (vmx
->nested
.virtual_apic_page
) {
7522 kvm_release_page_dirty(vmx
->nested
.virtual_apic_page
);
7523 vmx
->nested
.virtual_apic_page
= NULL
;
7525 if (vmx
->nested
.pi_desc_page
) {
7526 kunmap(vmx
->nested
.pi_desc_page
);
7527 kvm_release_page_dirty(vmx
->nested
.pi_desc_page
);
7528 vmx
->nested
.pi_desc_page
= NULL
;
7529 vmx
->nested
.pi_desc
= NULL
;
7532 free_loaded_vmcs(&vmx
->nested
.vmcs02
);
7535 /* Emulate the VMXOFF instruction */
7536 static int handle_vmoff(struct kvm_vcpu
*vcpu
)
7538 if (!nested_vmx_check_permission(vcpu
))
7540 free_nested(to_vmx(vcpu
));
7541 nested_vmx_succeed(vcpu
);
7542 return kvm_skip_emulated_instruction(vcpu
);
7545 /* Emulate the VMCLEAR instruction */
7546 static int handle_vmclear(struct kvm_vcpu
*vcpu
)
7548 struct vcpu_vmx
*vmx
= to_vmx(vcpu
);
7552 if (!nested_vmx_check_permission(vcpu
))
7555 if (nested_vmx_get_vmptr(vcpu
, &vmptr
))
7558 if (!PAGE_ALIGNED(vmptr
) || (vmptr
>> cpuid_maxphyaddr(vcpu
))) {
7559 nested_vmx_failValid(vcpu
, VMXERR_VMCLEAR_INVALID_ADDRESS
);
7560 return kvm_skip_emulated_instruction(vcpu
);
7563 if (vmptr
== vmx
->nested
.vmxon_ptr
) {
7564 nested_vmx_failValid(vcpu
, VMXERR_VMCLEAR_VMXON_POINTER
);
7565 return kvm_skip_emulated_instruction(vcpu
);
7568 if (vmptr
== vmx
->nested
.current_vmptr
)
7569 nested_release_vmcs12(vmx
);
7571 kvm_vcpu_write_guest(vcpu
,
7572 vmptr
+ offsetof(struct vmcs12
, launch_state
),
7573 &zero
, sizeof(zero
));
7575 nested_vmx_succeed(vcpu
);
7576 return kvm_skip_emulated_instruction(vcpu
);
7579 static int nested_vmx_run(struct kvm_vcpu
*vcpu
, bool launch
);
7581 /* Emulate the VMLAUNCH instruction */
7582 static int handle_vmlaunch(struct kvm_vcpu
*vcpu
)
7584 return nested_vmx_run(vcpu
, true);
7587 /* Emulate the VMRESUME instruction */
7588 static int handle_vmresume(struct kvm_vcpu
*vcpu
)
7591 return nested_vmx_run(vcpu
, false);
7595 * Read a vmcs12 field. Since these can have varying lengths and we return
7596 * one type, we chose the biggest type (u64) and zero-extend the return value
7597 * to that size. Note that the caller, handle_vmread, might need to use only
7598 * some of the bits we return here (e.g., on 32-bit guests, only 32 bits of
7599 * 64-bit fields are to be returned).
7601 static inline int vmcs12_read_any(struct kvm_vcpu
*vcpu
,
7602 unsigned long field
, u64
*ret
)
7604 short offset
= vmcs_field_to_offset(field
);
7610 p
= ((char *)(get_vmcs12(vcpu
))) + offset
;
7612 switch (vmcs_field_width(field
)) {
7613 case VMCS_FIELD_WIDTH_NATURAL_WIDTH
:
7614 *ret
= *((natural_width
*)p
);
7616 case VMCS_FIELD_WIDTH_U16
:
7619 case VMCS_FIELD_WIDTH_U32
:
7622 case VMCS_FIELD_WIDTH_U64
:
7632 static inline int vmcs12_write_any(struct kvm_vcpu
*vcpu
,
7633 unsigned long field
, u64 field_value
){
7634 short offset
= vmcs_field_to_offset(field
);
7635 char *p
= ((char *) get_vmcs12(vcpu
)) + offset
;
7639 switch (vmcs_field_width(field
)) {
7640 case VMCS_FIELD_WIDTH_U16
:
7641 *(u16
*)p
= field_value
;
7643 case VMCS_FIELD_WIDTH_U32
:
7644 *(u32
*)p
= field_value
;
7646 case VMCS_FIELD_WIDTH_U64
:
7647 *(u64
*)p
= field_value
;
7649 case VMCS_FIELD_WIDTH_NATURAL_WIDTH
:
7650 *(natural_width
*)p
= field_value
;
7659 static void copy_shadow_to_vmcs12(struct vcpu_vmx
*vmx
)
7662 unsigned long field
;
7664 struct vmcs
*shadow_vmcs
= vmx
->vmcs01
.shadow_vmcs
;
7665 const u16
*fields
= shadow_read_write_fields
;
7666 const int num_fields
= max_shadow_read_write_fields
;
7670 vmcs_load(shadow_vmcs
);
7672 for (i
= 0; i
< num_fields
; i
++) {
7674 field_value
= __vmcs_readl(field
);
7675 vmcs12_write_any(&vmx
->vcpu
, field
, field_value
);
7678 vmcs_clear(shadow_vmcs
);
7679 vmcs_load(vmx
->loaded_vmcs
->vmcs
);
7684 static void copy_vmcs12_to_shadow(struct vcpu_vmx
*vmx
)
7686 const u16
*fields
[] = {
7687 shadow_read_write_fields
,
7688 shadow_read_only_fields
7690 const int max_fields
[] = {
7691 max_shadow_read_write_fields
,
7692 max_shadow_read_only_fields
7695 unsigned long field
;
7696 u64 field_value
= 0;
7697 struct vmcs
*shadow_vmcs
= vmx
->vmcs01
.shadow_vmcs
;
7699 vmcs_load(shadow_vmcs
);
7701 for (q
= 0; q
< ARRAY_SIZE(fields
); q
++) {
7702 for (i
= 0; i
< max_fields
[q
]; i
++) {
7703 field
= fields
[q
][i
];
7704 vmcs12_read_any(&vmx
->vcpu
, field
, &field_value
);
7705 __vmcs_writel(field
, field_value
);
7709 vmcs_clear(shadow_vmcs
);
7710 vmcs_load(vmx
->loaded_vmcs
->vmcs
);
7714 * VMX instructions which assume a current vmcs12 (i.e., that VMPTRLD was
7715 * used before) all generate the same failure when it is missing.
7717 static int nested_vmx_check_vmcs12(struct kvm_vcpu
*vcpu
)
7719 struct vcpu_vmx
*vmx
= to_vmx(vcpu
);
7720 if (vmx
->nested
.current_vmptr
== -1ull) {
7721 nested_vmx_failInvalid(vcpu
);
7727 static int handle_vmread(struct kvm_vcpu
*vcpu
)
7729 unsigned long field
;
7731 unsigned long exit_qualification
= vmcs_readl(EXIT_QUALIFICATION
);
7732 u32 vmx_instruction_info
= vmcs_read32(VMX_INSTRUCTION_INFO
);
7735 if (!nested_vmx_check_permission(vcpu
))
7738 if (!nested_vmx_check_vmcs12(vcpu
))
7739 return kvm_skip_emulated_instruction(vcpu
);
7741 /* Decode instruction info and find the field to read */
7742 field
= kvm_register_readl(vcpu
, (((vmx_instruction_info
) >> 28) & 0xf));
7743 /* Read the field, zero-extended to a u64 field_value */
7744 if (vmcs12_read_any(vcpu
, field
, &field_value
) < 0) {
7745 nested_vmx_failValid(vcpu
, VMXERR_UNSUPPORTED_VMCS_COMPONENT
);
7746 return kvm_skip_emulated_instruction(vcpu
);
7749 * Now copy part of this value to register or memory, as requested.
7750 * Note that the number of bits actually copied is 32 or 64 depending
7751 * on the guest's mode (32 or 64 bit), not on the given field's length.
7753 if (vmx_instruction_info
& (1u << 10)) {
7754 kvm_register_writel(vcpu
, (((vmx_instruction_info
) >> 3) & 0xf),
7757 if (get_vmx_mem_address(vcpu
, exit_qualification
,
7758 vmx_instruction_info
, true, &gva
))
7760 /* _system ok, as hardware has verified cpl=0 */
7761 kvm_write_guest_virt_system(&vcpu
->arch
.emulate_ctxt
, gva
,
7762 &field_value
, (is_long_mode(vcpu
) ? 8 : 4), NULL
);
7765 nested_vmx_succeed(vcpu
);
7766 return kvm_skip_emulated_instruction(vcpu
);
7770 static int handle_vmwrite(struct kvm_vcpu
*vcpu
)
7772 unsigned long field
;
7774 struct vcpu_vmx
*vmx
= to_vmx(vcpu
);
7775 unsigned long exit_qualification
= vmcs_readl(EXIT_QUALIFICATION
);
7776 u32 vmx_instruction_info
= vmcs_read32(VMX_INSTRUCTION_INFO
);
7778 /* The value to write might be 32 or 64 bits, depending on L1's long
7779 * mode, and eventually we need to write that into a field of several
7780 * possible lengths. The code below first zero-extends the value to 64
7781 * bit (field_value), and then copies only the appropriate number of
7782 * bits into the vmcs12 field.
7784 u64 field_value
= 0;
7785 struct x86_exception e
;
7787 if (!nested_vmx_check_permission(vcpu
))
7790 if (!nested_vmx_check_vmcs12(vcpu
))
7791 return kvm_skip_emulated_instruction(vcpu
);
7793 if (vmx_instruction_info
& (1u << 10))
7794 field_value
= kvm_register_readl(vcpu
,
7795 (((vmx_instruction_info
) >> 3) & 0xf));
7797 if (get_vmx_mem_address(vcpu
, exit_qualification
,
7798 vmx_instruction_info
, false, &gva
))
7800 if (kvm_read_guest_virt(&vcpu
->arch
.emulate_ctxt
, gva
,
7801 &field_value
, (is_64_bit_mode(vcpu
) ? 8 : 4), &e
)) {
7802 kvm_inject_page_fault(vcpu
, &e
);
7808 field
= kvm_register_readl(vcpu
, (((vmx_instruction_info
) >> 28) & 0xf));
7809 if (vmcs_field_readonly(field
)) {
7810 nested_vmx_failValid(vcpu
,
7811 VMXERR_VMWRITE_READ_ONLY_VMCS_COMPONENT
);
7812 return kvm_skip_emulated_instruction(vcpu
);
7815 if (vmcs12_write_any(vcpu
, field
, field_value
) < 0) {
7816 nested_vmx_failValid(vcpu
, VMXERR_UNSUPPORTED_VMCS_COMPONENT
);
7817 return kvm_skip_emulated_instruction(vcpu
);
7821 #define SHADOW_FIELD_RW(x) case x:
7822 #include "vmx_shadow_fields.h"
7824 * The fields that can be updated by L1 without a vmexit are
7825 * always updated in the vmcs02, the others go down the slow
7826 * path of prepare_vmcs02.
7830 vmx
->nested
.dirty_vmcs12
= true;
7834 nested_vmx_succeed(vcpu
);
7835 return kvm_skip_emulated_instruction(vcpu
);
7838 static void set_current_vmptr(struct vcpu_vmx
*vmx
, gpa_t vmptr
)
7840 vmx
->nested
.current_vmptr
= vmptr
;
7841 if (enable_shadow_vmcs
) {
7842 vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL
,
7843 SECONDARY_EXEC_SHADOW_VMCS
);
7844 vmcs_write64(VMCS_LINK_POINTER
,
7845 __pa(vmx
->vmcs01
.shadow_vmcs
));
7846 vmx
->nested
.sync_shadow_vmcs
= true;
7848 vmx
->nested
.dirty_vmcs12
= true;
7851 /* Emulate the VMPTRLD instruction */
7852 static int handle_vmptrld(struct kvm_vcpu
*vcpu
)
7854 struct vcpu_vmx
*vmx
= to_vmx(vcpu
);
7857 if (!nested_vmx_check_permission(vcpu
))
7860 if (nested_vmx_get_vmptr(vcpu
, &vmptr
))
7863 if (!PAGE_ALIGNED(vmptr
) || (vmptr
>> cpuid_maxphyaddr(vcpu
))) {
7864 nested_vmx_failValid(vcpu
, VMXERR_VMPTRLD_INVALID_ADDRESS
);
7865 return kvm_skip_emulated_instruction(vcpu
);
7868 if (vmptr
== vmx
->nested
.vmxon_ptr
) {
7869 nested_vmx_failValid(vcpu
, VMXERR_VMPTRLD_VMXON_POINTER
);
7870 return kvm_skip_emulated_instruction(vcpu
);
7873 if (vmx
->nested
.current_vmptr
!= vmptr
) {
7874 struct vmcs12
*new_vmcs12
;
7876 page
= kvm_vcpu_gpa_to_page(vcpu
, vmptr
);
7877 if (is_error_page(page
)) {
7878 nested_vmx_failInvalid(vcpu
);
7879 return kvm_skip_emulated_instruction(vcpu
);
7881 new_vmcs12
= kmap(page
);
7882 if (new_vmcs12
->revision_id
!= VMCS12_REVISION
) {
7884 kvm_release_page_clean(page
);
7885 nested_vmx_failValid(vcpu
,
7886 VMXERR_VMPTRLD_INCORRECT_VMCS_REVISION_ID
);
7887 return kvm_skip_emulated_instruction(vcpu
);
7890 nested_release_vmcs12(vmx
);
7892 * Load VMCS12 from guest memory since it is not already
7895 memcpy(vmx
->nested
.cached_vmcs12
, new_vmcs12
, VMCS12_SIZE
);
7897 kvm_release_page_clean(page
);
7899 set_current_vmptr(vmx
, vmptr
);
7902 nested_vmx_succeed(vcpu
);
7903 return kvm_skip_emulated_instruction(vcpu
);
7906 /* Emulate the VMPTRST instruction */
7907 static int handle_vmptrst(struct kvm_vcpu
*vcpu
)
7909 unsigned long exit_qualification
= vmcs_readl(EXIT_QUALIFICATION
);
7910 u32 vmx_instruction_info
= vmcs_read32(VMX_INSTRUCTION_INFO
);
7912 struct x86_exception e
;
7914 if (!nested_vmx_check_permission(vcpu
))
7917 if (get_vmx_mem_address(vcpu
, exit_qualification
,
7918 vmx_instruction_info
, true, &vmcs_gva
))
7920 /* ok to use *_system, as hardware has verified cpl=0 */
7921 if (kvm_write_guest_virt_system(&vcpu
->arch
.emulate_ctxt
, vmcs_gva
,
7922 (void *)&to_vmx(vcpu
)->nested
.current_vmptr
,
7924 kvm_inject_page_fault(vcpu
, &e
);
7927 nested_vmx_succeed(vcpu
);
7928 return kvm_skip_emulated_instruction(vcpu
);
7931 /* Emulate the INVEPT instruction */
7932 static int handle_invept(struct kvm_vcpu
*vcpu
)
7934 struct vcpu_vmx
*vmx
= to_vmx(vcpu
);
7935 u32 vmx_instruction_info
, types
;
7938 struct x86_exception e
;
7943 if (!(vmx
->nested
.nested_vmx_secondary_ctls_high
&
7944 SECONDARY_EXEC_ENABLE_EPT
) ||
7945 !(vmx
->nested
.nested_vmx_ept_caps
& VMX_EPT_INVEPT_BIT
)) {
7946 kvm_queue_exception(vcpu
, UD_VECTOR
);
7950 if (!nested_vmx_check_permission(vcpu
))
7953 vmx_instruction_info
= vmcs_read32(VMX_INSTRUCTION_INFO
);
7954 type
= kvm_register_readl(vcpu
, (vmx_instruction_info
>> 28) & 0xf);
7956 types
= (vmx
->nested
.nested_vmx_ept_caps
>> VMX_EPT_EXTENT_SHIFT
) & 6;
7958 if (type
>= 32 || !(types
& (1 << type
))) {
7959 nested_vmx_failValid(vcpu
,
7960 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID
);
7961 return kvm_skip_emulated_instruction(vcpu
);
7964 /* According to the Intel VMX instruction reference, the memory
7965 * operand is read even if it isn't needed (e.g., for type==global)
7967 if (get_vmx_mem_address(vcpu
, vmcs_readl(EXIT_QUALIFICATION
),
7968 vmx_instruction_info
, false, &gva
))
7970 if (kvm_read_guest_virt(&vcpu
->arch
.emulate_ctxt
, gva
, &operand
,
7971 sizeof(operand
), &e
)) {
7972 kvm_inject_page_fault(vcpu
, &e
);
7977 case VMX_EPT_EXTENT_GLOBAL
:
7979 * TODO: track mappings and invalidate
7980 * single context requests appropriately
7982 case VMX_EPT_EXTENT_CONTEXT
:
7983 kvm_mmu_sync_roots(vcpu
);
7984 kvm_make_request(KVM_REQ_TLB_FLUSH
, vcpu
);
7985 nested_vmx_succeed(vcpu
);
7992 return kvm_skip_emulated_instruction(vcpu
);
7995 static int handle_invvpid(struct kvm_vcpu
*vcpu
)
7997 struct vcpu_vmx
*vmx
= to_vmx(vcpu
);
7998 u32 vmx_instruction_info
;
7999 unsigned long type
, types
;
8001 struct x86_exception e
;
8007 if (!(vmx
->nested
.nested_vmx_secondary_ctls_high
&
8008 SECONDARY_EXEC_ENABLE_VPID
) ||
8009 !(vmx
->nested
.nested_vmx_vpid_caps
& VMX_VPID_INVVPID_BIT
)) {
8010 kvm_queue_exception(vcpu
, UD_VECTOR
);
8014 if (!nested_vmx_check_permission(vcpu
))
8017 vmx_instruction_info
= vmcs_read32(VMX_INSTRUCTION_INFO
);
8018 type
= kvm_register_readl(vcpu
, (vmx_instruction_info
>> 28) & 0xf);
8020 types
= (vmx
->nested
.nested_vmx_vpid_caps
&
8021 VMX_VPID_EXTENT_SUPPORTED_MASK
) >> 8;
8023 if (type
>= 32 || !(types
& (1 << type
))) {
8024 nested_vmx_failValid(vcpu
,
8025 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID
);
8026 return kvm_skip_emulated_instruction(vcpu
);
8029 /* according to the intel vmx instruction reference, the memory
8030 * operand is read even if it isn't needed (e.g., for type==global)
8032 if (get_vmx_mem_address(vcpu
, vmcs_readl(EXIT_QUALIFICATION
),
8033 vmx_instruction_info
, false, &gva
))
8035 if (kvm_read_guest_virt(&vcpu
->arch
.emulate_ctxt
, gva
, &operand
,
8036 sizeof(operand
), &e
)) {
8037 kvm_inject_page_fault(vcpu
, &e
);
8040 if (operand
.vpid
>> 16) {
8041 nested_vmx_failValid(vcpu
,
8042 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID
);
8043 return kvm_skip_emulated_instruction(vcpu
);
8047 case VMX_VPID_EXTENT_INDIVIDUAL_ADDR
:
8048 if (is_noncanonical_address(operand
.gla
, vcpu
)) {
8049 nested_vmx_failValid(vcpu
,
8050 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID
);
8051 return kvm_skip_emulated_instruction(vcpu
);
8054 case VMX_VPID_EXTENT_SINGLE_CONTEXT
:
8055 case VMX_VPID_EXTENT_SINGLE_NON_GLOBAL
:
8056 if (!operand
.vpid
) {
8057 nested_vmx_failValid(vcpu
,
8058 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID
);
8059 return kvm_skip_emulated_instruction(vcpu
);
8062 case VMX_VPID_EXTENT_ALL_CONTEXT
:
8066 return kvm_skip_emulated_instruction(vcpu
);
8069 __vmx_flush_tlb(vcpu
, vmx
->nested
.vpid02
, true);
8070 nested_vmx_succeed(vcpu
);
8072 return kvm_skip_emulated_instruction(vcpu
);
8075 static int handle_pml_full(struct kvm_vcpu
*vcpu
)
8077 unsigned long exit_qualification
;
8079 trace_kvm_pml_full(vcpu
->vcpu_id
);
8081 exit_qualification
= vmcs_readl(EXIT_QUALIFICATION
);
8084 * PML buffer FULL happened while executing iret from NMI,
8085 * "blocked by NMI" bit has to be set before next VM entry.
8087 if (!(to_vmx(vcpu
)->idt_vectoring_info
& VECTORING_INFO_VALID_MASK
) &&
8089 (exit_qualification
& INTR_INFO_UNBLOCK_NMI
))
8090 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO
,
8091 GUEST_INTR_STATE_NMI
);
8094 * PML buffer already flushed at beginning of VMEXIT. Nothing to do
8095 * here.., and there's no userspace involvement needed for PML.
8100 static int handle_preemption_timer(struct kvm_vcpu
*vcpu
)
8102 kvm_lapic_expired_hv_timer(vcpu
);
8106 static bool valid_ept_address(struct kvm_vcpu
*vcpu
, u64 address
)
8108 struct vcpu_vmx
*vmx
= to_vmx(vcpu
);
8109 int maxphyaddr
= cpuid_maxphyaddr(vcpu
);
8111 /* Check for memory type validity */
8112 switch (address
& VMX_EPTP_MT_MASK
) {
8113 case VMX_EPTP_MT_UC
:
8114 if (!(vmx
->nested
.nested_vmx_ept_caps
& VMX_EPTP_UC_BIT
))
8117 case VMX_EPTP_MT_WB
:
8118 if (!(vmx
->nested
.nested_vmx_ept_caps
& VMX_EPTP_WB_BIT
))
8125 /* only 4 levels page-walk length are valid */
8126 if ((address
& VMX_EPTP_PWL_MASK
) != VMX_EPTP_PWL_4
)
8129 /* Reserved bits should not be set */
8130 if (address
>> maxphyaddr
|| ((address
>> 7) & 0x1f))
8133 /* AD, if set, should be supported */
8134 if (address
& VMX_EPTP_AD_ENABLE_BIT
) {
8135 if (!(vmx
->nested
.nested_vmx_ept_caps
& VMX_EPT_AD_BIT
))
8142 static int nested_vmx_eptp_switching(struct kvm_vcpu
*vcpu
,
8143 struct vmcs12
*vmcs12
)
8145 u32 index
= vcpu
->arch
.regs
[VCPU_REGS_RCX
];
8147 bool accessed_dirty
;
8148 struct kvm_mmu
*mmu
= vcpu
->arch
.walk_mmu
;
8150 if (!nested_cpu_has_eptp_switching(vmcs12
) ||
8151 !nested_cpu_has_ept(vmcs12
))
8154 if (index
>= VMFUNC_EPTP_ENTRIES
)
8158 if (kvm_vcpu_read_guest_page(vcpu
, vmcs12
->eptp_list_address
>> PAGE_SHIFT
,
8159 &address
, index
* 8, 8))
8162 accessed_dirty
= !!(address
& VMX_EPTP_AD_ENABLE_BIT
);
8165 * If the (L2) guest does a vmfunc to the currently
8166 * active ept pointer, we don't have to do anything else
8168 if (vmcs12
->ept_pointer
!= address
) {
8169 if (!valid_ept_address(vcpu
, address
))
8172 kvm_mmu_unload(vcpu
);
8173 mmu
->ept_ad
= accessed_dirty
;
8174 mmu
->base_role
.ad_disabled
= !accessed_dirty
;
8175 vmcs12
->ept_pointer
= address
;
8177 * TODO: Check what's the correct approach in case
8178 * mmu reload fails. Currently, we just let the next
8179 * reload potentially fail
8181 kvm_mmu_reload(vcpu
);
8187 static int handle_vmfunc(struct kvm_vcpu
*vcpu
)
8189 struct vcpu_vmx
*vmx
= to_vmx(vcpu
);
8190 struct vmcs12
*vmcs12
;
8191 u32 function
= vcpu
->arch
.regs
[VCPU_REGS_RAX
];
8194 * VMFUNC is only supported for nested guests, but we always enable the
8195 * secondary control for simplicity; for non-nested mode, fake that we
8196 * didn't by injecting #UD.
8198 if (!is_guest_mode(vcpu
)) {
8199 kvm_queue_exception(vcpu
, UD_VECTOR
);
8203 vmcs12
= get_vmcs12(vcpu
);
8204 if ((vmcs12
->vm_function_control
& (1 << function
)) == 0)
8209 if (nested_vmx_eptp_switching(vcpu
, vmcs12
))
8215 return kvm_skip_emulated_instruction(vcpu
);
8218 nested_vmx_vmexit(vcpu
, vmx
->exit_reason
,
8219 vmcs_read32(VM_EXIT_INTR_INFO
),
8220 vmcs_readl(EXIT_QUALIFICATION
));
8225 * The exit handlers return 1 if the exit was handled fully and guest execution
8226 * may resume. Otherwise they set the kvm_run parameter to indicate what needs
8227 * to be done to userspace and return 0.
8229 static int (*const kvm_vmx_exit_handlers
[])(struct kvm_vcpu
*vcpu
) = {
8230 [EXIT_REASON_EXCEPTION_NMI
] = handle_exception
,
8231 [EXIT_REASON_EXTERNAL_INTERRUPT
] = handle_external_interrupt
,
8232 [EXIT_REASON_TRIPLE_FAULT
] = handle_triple_fault
,
8233 [EXIT_REASON_NMI_WINDOW
] = handle_nmi_window
,
8234 [EXIT_REASON_IO_INSTRUCTION
] = handle_io
,
8235 [EXIT_REASON_CR_ACCESS
] = handle_cr
,
8236 [EXIT_REASON_DR_ACCESS
] = handle_dr
,
8237 [EXIT_REASON_CPUID
] = handle_cpuid
,
8238 [EXIT_REASON_MSR_READ
] = handle_rdmsr
,
8239 [EXIT_REASON_MSR_WRITE
] = handle_wrmsr
,
8240 [EXIT_REASON_PENDING_INTERRUPT
] = handle_interrupt_window
,
8241 [EXIT_REASON_HLT
] = handle_halt
,
8242 [EXIT_REASON_INVD
] = handle_invd
,
8243 [EXIT_REASON_INVLPG
] = handle_invlpg
,
8244 [EXIT_REASON_RDPMC
] = handle_rdpmc
,
8245 [EXIT_REASON_VMCALL
] = handle_vmcall
,
8246 [EXIT_REASON_VMCLEAR
] = handle_vmclear
,
8247 [EXIT_REASON_VMLAUNCH
] = handle_vmlaunch
,
8248 [EXIT_REASON_VMPTRLD
] = handle_vmptrld
,
8249 [EXIT_REASON_VMPTRST
] = handle_vmptrst
,
8250 [EXIT_REASON_VMREAD
] = handle_vmread
,
8251 [EXIT_REASON_VMRESUME
] = handle_vmresume
,
8252 [EXIT_REASON_VMWRITE
] = handle_vmwrite
,
8253 [EXIT_REASON_VMOFF
] = handle_vmoff
,
8254 [EXIT_REASON_VMON
] = handle_vmon
,
8255 [EXIT_REASON_TPR_BELOW_THRESHOLD
] = handle_tpr_below_threshold
,
8256 [EXIT_REASON_APIC_ACCESS
] = handle_apic_access
,
8257 [EXIT_REASON_APIC_WRITE
] = handle_apic_write
,
8258 [EXIT_REASON_EOI_INDUCED
] = handle_apic_eoi_induced
,
8259 [EXIT_REASON_WBINVD
] = handle_wbinvd
,
8260 [EXIT_REASON_XSETBV
] = handle_xsetbv
,
8261 [EXIT_REASON_TASK_SWITCH
] = handle_task_switch
,
8262 [EXIT_REASON_MCE_DURING_VMENTRY
] = handle_machine_check
,
8263 [EXIT_REASON_GDTR_IDTR
] = handle_desc
,
8264 [EXIT_REASON_LDTR_TR
] = handle_desc
,
8265 [EXIT_REASON_EPT_VIOLATION
] = handle_ept_violation
,
8266 [EXIT_REASON_EPT_MISCONFIG
] = handle_ept_misconfig
,
8267 [EXIT_REASON_PAUSE_INSTRUCTION
] = handle_pause
,
8268 [EXIT_REASON_MWAIT_INSTRUCTION
] = handle_mwait
,
8269 [EXIT_REASON_MONITOR_TRAP_FLAG
] = handle_monitor_trap
,
8270 [EXIT_REASON_MONITOR_INSTRUCTION
] = handle_monitor
,
8271 [EXIT_REASON_INVEPT
] = handle_invept
,
8272 [EXIT_REASON_INVVPID
] = handle_invvpid
,
8273 [EXIT_REASON_RDRAND
] = handle_invalid_op
,
8274 [EXIT_REASON_RDSEED
] = handle_invalid_op
,
8275 [EXIT_REASON_XSAVES
] = handle_xsaves
,
8276 [EXIT_REASON_XRSTORS
] = handle_xrstors
,
8277 [EXIT_REASON_PML_FULL
] = handle_pml_full
,
8278 [EXIT_REASON_VMFUNC
] = handle_vmfunc
,
8279 [EXIT_REASON_PREEMPTION_TIMER
] = handle_preemption_timer
,
8282 static const int kvm_vmx_max_exit_handlers
=
8283 ARRAY_SIZE(kvm_vmx_exit_handlers
);
8285 static bool nested_vmx_exit_handled_io(struct kvm_vcpu
*vcpu
,
8286 struct vmcs12
*vmcs12
)
8288 unsigned long exit_qualification
;
8289 gpa_t bitmap
, last_bitmap
;
8294 if (!nested_cpu_has(vmcs12
, CPU_BASED_USE_IO_BITMAPS
))
8295 return nested_cpu_has(vmcs12
, CPU_BASED_UNCOND_IO_EXITING
);
8297 exit_qualification
= vmcs_readl(EXIT_QUALIFICATION
);
8299 port
= exit_qualification
>> 16;
8300 size
= (exit_qualification
& 7) + 1;
8302 last_bitmap
= (gpa_t
)-1;
8307 bitmap
= vmcs12
->io_bitmap_a
;
8308 else if (port
< 0x10000)
8309 bitmap
= vmcs12
->io_bitmap_b
;
8312 bitmap
+= (port
& 0x7fff) / 8;
8314 if (last_bitmap
!= bitmap
)
8315 if (kvm_vcpu_read_guest(vcpu
, bitmap
, &b
, 1))
8317 if (b
& (1 << (port
& 7)))
8322 last_bitmap
= bitmap
;
8329 * Return 1 if we should exit from L2 to L1 to handle an MSR access access,
8330 * rather than handle it ourselves in L0. I.e., check whether L1 expressed
8331 * disinterest in the current event (read or write a specific MSR) by using an
8332 * MSR bitmap. This may be the case even when L0 doesn't use MSR bitmaps.
8334 static bool nested_vmx_exit_handled_msr(struct kvm_vcpu
*vcpu
,
8335 struct vmcs12
*vmcs12
, u32 exit_reason
)
8337 u32 msr_index
= vcpu
->arch
.regs
[VCPU_REGS_RCX
];
8340 if (!nested_cpu_has(vmcs12
, CPU_BASED_USE_MSR_BITMAPS
))
8344 * The MSR_BITMAP page is divided into four 1024-byte bitmaps,
8345 * for the four combinations of read/write and low/high MSR numbers.
8346 * First we need to figure out which of the four to use:
8348 bitmap
= vmcs12
->msr_bitmap
;
8349 if (exit_reason
== EXIT_REASON_MSR_WRITE
)
8351 if (msr_index
>= 0xc0000000) {
8352 msr_index
-= 0xc0000000;
8356 /* Then read the msr_index'th bit from this bitmap: */
8357 if (msr_index
< 1024*8) {
8359 if (kvm_vcpu_read_guest(vcpu
, bitmap
+ msr_index
/8, &b
, 1))
8361 return 1 & (b
>> (msr_index
& 7));
8363 return true; /* let L1 handle the wrong parameter */
8367 * Return 1 if we should exit from L2 to L1 to handle a CR access exit,
8368 * rather than handle it ourselves in L0. I.e., check if L1 wanted to
8369 * intercept (via guest_host_mask etc.) the current event.
8371 static bool nested_vmx_exit_handled_cr(struct kvm_vcpu
*vcpu
,
8372 struct vmcs12
*vmcs12
)
8374 unsigned long exit_qualification
= vmcs_readl(EXIT_QUALIFICATION
);
8375 int cr
= exit_qualification
& 15;
8379 switch ((exit_qualification
>> 4) & 3) {
8380 case 0: /* mov to cr */
8381 reg
= (exit_qualification
>> 8) & 15;
8382 val
= kvm_register_readl(vcpu
, reg
);
8385 if (vmcs12
->cr0_guest_host_mask
&
8386 (val
^ vmcs12
->cr0_read_shadow
))
8390 if ((vmcs12
->cr3_target_count
>= 1 &&
8391 vmcs12
->cr3_target_value0
== val
) ||
8392 (vmcs12
->cr3_target_count
>= 2 &&
8393 vmcs12
->cr3_target_value1
== val
) ||
8394 (vmcs12
->cr3_target_count
>= 3 &&
8395 vmcs12
->cr3_target_value2
== val
) ||
8396 (vmcs12
->cr3_target_count
>= 4 &&
8397 vmcs12
->cr3_target_value3
== val
))
8399 if (nested_cpu_has(vmcs12
, CPU_BASED_CR3_LOAD_EXITING
))
8403 if (vmcs12
->cr4_guest_host_mask
&
8404 (vmcs12
->cr4_read_shadow
^ val
))
8408 if (nested_cpu_has(vmcs12
, CPU_BASED_CR8_LOAD_EXITING
))
8414 if ((vmcs12
->cr0_guest_host_mask
& X86_CR0_TS
) &&
8415 (vmcs12
->cr0_read_shadow
& X86_CR0_TS
))
8418 case 1: /* mov from cr */
8421 if (vmcs12
->cpu_based_vm_exec_control
&
8422 CPU_BASED_CR3_STORE_EXITING
)
8426 if (vmcs12
->cpu_based_vm_exec_control
&
8427 CPU_BASED_CR8_STORE_EXITING
)
8434 * lmsw can change bits 1..3 of cr0, and only set bit 0 of
8435 * cr0. Other attempted changes are ignored, with no exit.
8437 val
= (exit_qualification
>> LMSW_SOURCE_DATA_SHIFT
) & 0x0f;
8438 if (vmcs12
->cr0_guest_host_mask
& 0xe &
8439 (val
^ vmcs12
->cr0_read_shadow
))
8441 if ((vmcs12
->cr0_guest_host_mask
& 0x1) &&
8442 !(vmcs12
->cr0_read_shadow
& 0x1) &&
8451 * Return 1 if we should exit from L2 to L1 to handle an exit, or 0 if we
8452 * should handle it ourselves in L0 (and then continue L2). Only call this
8453 * when in is_guest_mode (L2).
8455 static bool nested_vmx_exit_reflected(struct kvm_vcpu
*vcpu
, u32 exit_reason
)
8457 u32 intr_info
= vmcs_read32(VM_EXIT_INTR_INFO
);
8458 struct vcpu_vmx
*vmx
= to_vmx(vcpu
);
8459 struct vmcs12
*vmcs12
= get_vmcs12(vcpu
);
8461 if (vmx
->nested
.nested_run_pending
)
8464 if (unlikely(vmx
->fail
)) {
8465 pr_info_ratelimited("%s failed vm entry %x\n", __func__
,
8466 vmcs_read32(VM_INSTRUCTION_ERROR
));
8471 * The host physical addresses of some pages of guest memory
8472 * are loaded into the vmcs02 (e.g. vmcs12's Virtual APIC
8473 * Page). The CPU may write to these pages via their host
8474 * physical address while L2 is running, bypassing any
8475 * address-translation-based dirty tracking (e.g. EPT write
8478 * Mark them dirty on every exit from L2 to prevent them from
8479 * getting out of sync with dirty tracking.
8481 nested_mark_vmcs12_pages_dirty(vcpu
);
8483 trace_kvm_nested_vmexit(kvm_rip_read(vcpu
), exit_reason
,
8484 vmcs_readl(EXIT_QUALIFICATION
),
8485 vmx
->idt_vectoring_info
,
8487 vmcs_read32(VM_EXIT_INTR_ERROR_CODE
),
8490 switch (exit_reason
) {
8491 case EXIT_REASON_EXCEPTION_NMI
:
8492 if (is_nmi(intr_info
))
8494 else if (is_page_fault(intr_info
))
8495 return !vmx
->vcpu
.arch
.apf
.host_apf_reason
&& enable_ept
;
8496 else if (is_no_device(intr_info
) &&
8497 !(vmcs12
->guest_cr0
& X86_CR0_TS
))
8499 else if (is_debug(intr_info
) &&
8501 (KVM_GUESTDBG_SINGLESTEP
| KVM_GUESTDBG_USE_HW_BP
))
8503 else if (is_breakpoint(intr_info
) &&
8504 vcpu
->guest_debug
& KVM_GUESTDBG_USE_SW_BP
)
8506 return vmcs12
->exception_bitmap
&
8507 (1u << (intr_info
& INTR_INFO_VECTOR_MASK
));
8508 case EXIT_REASON_EXTERNAL_INTERRUPT
:
8510 case EXIT_REASON_TRIPLE_FAULT
:
8512 case EXIT_REASON_PENDING_INTERRUPT
:
8513 return nested_cpu_has(vmcs12
, CPU_BASED_VIRTUAL_INTR_PENDING
);
8514 case EXIT_REASON_NMI_WINDOW
:
8515 return nested_cpu_has(vmcs12
, CPU_BASED_VIRTUAL_NMI_PENDING
);
8516 case EXIT_REASON_TASK_SWITCH
:
8518 case EXIT_REASON_CPUID
:
8520 case EXIT_REASON_HLT
:
8521 return nested_cpu_has(vmcs12
, CPU_BASED_HLT_EXITING
);
8522 case EXIT_REASON_INVD
:
8524 case EXIT_REASON_INVLPG
:
8525 return nested_cpu_has(vmcs12
, CPU_BASED_INVLPG_EXITING
);
8526 case EXIT_REASON_RDPMC
:
8527 return nested_cpu_has(vmcs12
, CPU_BASED_RDPMC_EXITING
);
8528 case EXIT_REASON_RDRAND
:
8529 return nested_cpu_has2(vmcs12
, SECONDARY_EXEC_RDRAND_EXITING
);
8530 case EXIT_REASON_RDSEED
:
8531 return nested_cpu_has2(vmcs12
, SECONDARY_EXEC_RDSEED_EXITING
);
8532 case EXIT_REASON_RDTSC
: case EXIT_REASON_RDTSCP
:
8533 return nested_cpu_has(vmcs12
, CPU_BASED_RDTSC_EXITING
);
8534 case EXIT_REASON_VMCALL
: case EXIT_REASON_VMCLEAR
:
8535 case EXIT_REASON_VMLAUNCH
: case EXIT_REASON_VMPTRLD
:
8536 case EXIT_REASON_VMPTRST
: case EXIT_REASON_VMREAD
:
8537 case EXIT_REASON_VMRESUME
: case EXIT_REASON_VMWRITE
:
8538 case EXIT_REASON_VMOFF
: case EXIT_REASON_VMON
:
8539 case EXIT_REASON_INVEPT
: case EXIT_REASON_INVVPID
:
8541 * VMX instructions trap unconditionally. This allows L1 to
8542 * emulate them for its L2 guest, i.e., allows 3-level nesting!
8545 case EXIT_REASON_CR_ACCESS
:
8546 return nested_vmx_exit_handled_cr(vcpu
, vmcs12
);
8547 case EXIT_REASON_DR_ACCESS
:
8548 return nested_cpu_has(vmcs12
, CPU_BASED_MOV_DR_EXITING
);
8549 case EXIT_REASON_IO_INSTRUCTION
:
8550 return nested_vmx_exit_handled_io(vcpu
, vmcs12
);
8551 case EXIT_REASON_GDTR_IDTR
: case EXIT_REASON_LDTR_TR
:
8552 return nested_cpu_has2(vmcs12
, SECONDARY_EXEC_DESC
);
8553 case EXIT_REASON_MSR_READ
:
8554 case EXIT_REASON_MSR_WRITE
:
8555 return nested_vmx_exit_handled_msr(vcpu
, vmcs12
, exit_reason
);
8556 case EXIT_REASON_INVALID_STATE
:
8558 case EXIT_REASON_MWAIT_INSTRUCTION
:
8559 return nested_cpu_has(vmcs12
, CPU_BASED_MWAIT_EXITING
);
8560 case EXIT_REASON_MONITOR_TRAP_FLAG
:
8561 return nested_cpu_has(vmcs12
, CPU_BASED_MONITOR_TRAP_FLAG
);
8562 case EXIT_REASON_MONITOR_INSTRUCTION
:
8563 return nested_cpu_has(vmcs12
, CPU_BASED_MONITOR_EXITING
);
8564 case EXIT_REASON_PAUSE_INSTRUCTION
:
8565 return nested_cpu_has(vmcs12
, CPU_BASED_PAUSE_EXITING
) ||
8566 nested_cpu_has2(vmcs12
,
8567 SECONDARY_EXEC_PAUSE_LOOP_EXITING
);
8568 case EXIT_REASON_MCE_DURING_VMENTRY
:
8570 case EXIT_REASON_TPR_BELOW_THRESHOLD
:
8571 return nested_cpu_has(vmcs12
, CPU_BASED_TPR_SHADOW
);
8572 case EXIT_REASON_APIC_ACCESS
:
8573 return nested_cpu_has2(vmcs12
,
8574 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES
);
8575 case EXIT_REASON_APIC_WRITE
:
8576 case EXIT_REASON_EOI_INDUCED
:
8577 /* apic_write and eoi_induced should exit unconditionally. */
8579 case EXIT_REASON_EPT_VIOLATION
:
8581 * L0 always deals with the EPT violation. If nested EPT is
8582 * used, and the nested mmu code discovers that the address is
8583 * missing in the guest EPT table (EPT12), the EPT violation
8584 * will be injected with nested_ept_inject_page_fault()
8587 case EXIT_REASON_EPT_MISCONFIG
:
8589 * L2 never uses directly L1's EPT, but rather L0's own EPT
8590 * table (shadow on EPT) or a merged EPT table that L0 built
8591 * (EPT on EPT). So any problems with the structure of the
8592 * table is L0's fault.
8595 case EXIT_REASON_INVPCID
:
8597 nested_cpu_has2(vmcs12
, SECONDARY_EXEC_ENABLE_INVPCID
) &&
8598 nested_cpu_has(vmcs12
, CPU_BASED_INVLPG_EXITING
);
8599 case EXIT_REASON_WBINVD
:
8600 return nested_cpu_has2(vmcs12
, SECONDARY_EXEC_WBINVD_EXITING
);
8601 case EXIT_REASON_XSETBV
:
8603 case EXIT_REASON_XSAVES
: case EXIT_REASON_XRSTORS
:
8605 * This should never happen, since it is not possible to
8606 * set XSS to a non-zero value---neither in L1 nor in L2.
8607 * If if it were, XSS would have to be checked against
8608 * the XSS exit bitmap in vmcs12.
8610 return nested_cpu_has2(vmcs12
, SECONDARY_EXEC_XSAVES
);
8611 case EXIT_REASON_PREEMPTION_TIMER
:
8613 case EXIT_REASON_PML_FULL
:
8614 /* We emulate PML support to L1. */
8616 case EXIT_REASON_VMFUNC
:
8617 /* VM functions are emulated through L2->L0 vmexits. */
8624 static int nested_vmx_reflect_vmexit(struct kvm_vcpu
*vcpu
, u32 exit_reason
)
8626 u32 exit_intr_info
= vmcs_read32(VM_EXIT_INTR_INFO
);
8629 * At this point, the exit interruption info in exit_intr_info
8630 * is only valid for EXCEPTION_NMI exits. For EXTERNAL_INTERRUPT
8631 * we need to query the in-kernel LAPIC.
8633 WARN_ON(exit_reason
== EXIT_REASON_EXTERNAL_INTERRUPT
);
8634 if ((exit_intr_info
&
8635 (INTR_INFO_VALID_MASK
| INTR_INFO_DELIVER_CODE_MASK
)) ==
8636 (INTR_INFO_VALID_MASK
| INTR_INFO_DELIVER_CODE_MASK
)) {
8637 struct vmcs12
*vmcs12
= get_vmcs12(vcpu
);
8638 vmcs12
->vm_exit_intr_error_code
=
8639 vmcs_read32(VM_EXIT_INTR_ERROR_CODE
);
8642 nested_vmx_vmexit(vcpu
, exit_reason
, exit_intr_info
,
8643 vmcs_readl(EXIT_QUALIFICATION
));
8647 static void vmx_get_exit_info(struct kvm_vcpu
*vcpu
, u64
*info1
, u64
*info2
)
8649 *info1
= vmcs_readl(EXIT_QUALIFICATION
);
8650 *info2
= vmcs_read32(VM_EXIT_INTR_INFO
);
8653 static void vmx_destroy_pml_buffer(struct vcpu_vmx
*vmx
)
8656 __free_page(vmx
->pml_pg
);
8661 static void vmx_flush_pml_buffer(struct kvm_vcpu
*vcpu
)
8663 struct vcpu_vmx
*vmx
= to_vmx(vcpu
);
8667 pml_idx
= vmcs_read16(GUEST_PML_INDEX
);
8669 /* Do nothing if PML buffer is empty */
8670 if (pml_idx
== (PML_ENTITY_NUM
- 1))
8673 /* PML index always points to next available PML buffer entity */
8674 if (pml_idx
>= PML_ENTITY_NUM
)
8679 pml_buf
= page_address(vmx
->pml_pg
);
8680 for (; pml_idx
< PML_ENTITY_NUM
; pml_idx
++) {
8683 gpa
= pml_buf
[pml_idx
];
8684 WARN_ON(gpa
& (PAGE_SIZE
- 1));
8685 kvm_vcpu_mark_page_dirty(vcpu
, gpa
>> PAGE_SHIFT
);
8688 /* reset PML index */
8689 vmcs_write16(GUEST_PML_INDEX
, PML_ENTITY_NUM
- 1);
8693 * Flush all vcpus' PML buffer and update logged GPAs to dirty_bitmap.
8694 * Called before reporting dirty_bitmap to userspace.
8696 static void kvm_flush_pml_buffers(struct kvm
*kvm
)
8699 struct kvm_vcpu
*vcpu
;
8701 * We only need to kick vcpu out of guest mode here, as PML buffer
8702 * is flushed at beginning of all VMEXITs, and it's obvious that only
8703 * vcpus running in guest are possible to have unflushed GPAs in PML
8706 kvm_for_each_vcpu(i
, vcpu
, kvm
)
8707 kvm_vcpu_kick(vcpu
);
8710 static void vmx_dump_sel(char *name
, uint32_t sel
)
8712 pr_err("%s sel=0x%04x, attr=0x%05x, limit=0x%08x, base=0x%016lx\n",
8713 name
, vmcs_read16(sel
),
8714 vmcs_read32(sel
+ GUEST_ES_AR_BYTES
- GUEST_ES_SELECTOR
),
8715 vmcs_read32(sel
+ GUEST_ES_LIMIT
- GUEST_ES_SELECTOR
),
8716 vmcs_readl(sel
+ GUEST_ES_BASE
- GUEST_ES_SELECTOR
));
8719 static void vmx_dump_dtsel(char *name
, uint32_t limit
)
8721 pr_err("%s limit=0x%08x, base=0x%016lx\n",
8722 name
, vmcs_read32(limit
),
8723 vmcs_readl(limit
+ GUEST_GDTR_BASE
- GUEST_GDTR_LIMIT
));
8726 static void dump_vmcs(void)
8728 u32 vmentry_ctl
= vmcs_read32(VM_ENTRY_CONTROLS
);
8729 u32 vmexit_ctl
= vmcs_read32(VM_EXIT_CONTROLS
);
8730 u32 cpu_based_exec_ctrl
= vmcs_read32(CPU_BASED_VM_EXEC_CONTROL
);
8731 u32 pin_based_exec_ctrl
= vmcs_read32(PIN_BASED_VM_EXEC_CONTROL
);
8732 u32 secondary_exec_control
= 0;
8733 unsigned long cr4
= vmcs_readl(GUEST_CR4
);
8734 u64 efer
= vmcs_read64(GUEST_IA32_EFER
);
8737 if (cpu_has_secondary_exec_ctrls())
8738 secondary_exec_control
= vmcs_read32(SECONDARY_VM_EXEC_CONTROL
);
8740 pr_err("*** Guest State ***\n");
8741 pr_err("CR0: actual=0x%016lx, shadow=0x%016lx, gh_mask=%016lx\n",
8742 vmcs_readl(GUEST_CR0
), vmcs_readl(CR0_READ_SHADOW
),
8743 vmcs_readl(CR0_GUEST_HOST_MASK
));
8744 pr_err("CR4: actual=0x%016lx, shadow=0x%016lx, gh_mask=%016lx\n",
8745 cr4
, vmcs_readl(CR4_READ_SHADOW
), vmcs_readl(CR4_GUEST_HOST_MASK
));
8746 pr_err("CR3 = 0x%016lx\n", vmcs_readl(GUEST_CR3
));
8747 if ((secondary_exec_control
& SECONDARY_EXEC_ENABLE_EPT
) &&
8748 (cr4
& X86_CR4_PAE
) && !(efer
& EFER_LMA
))
8750 pr_err("PDPTR0 = 0x%016llx PDPTR1 = 0x%016llx\n",
8751 vmcs_read64(GUEST_PDPTR0
), vmcs_read64(GUEST_PDPTR1
));
8752 pr_err("PDPTR2 = 0x%016llx PDPTR3 = 0x%016llx\n",
8753 vmcs_read64(GUEST_PDPTR2
), vmcs_read64(GUEST_PDPTR3
));
8755 pr_err("RSP = 0x%016lx RIP = 0x%016lx\n",
8756 vmcs_readl(GUEST_RSP
), vmcs_readl(GUEST_RIP
));
8757 pr_err("RFLAGS=0x%08lx DR7 = 0x%016lx\n",
8758 vmcs_readl(GUEST_RFLAGS
), vmcs_readl(GUEST_DR7
));
8759 pr_err("Sysenter RSP=%016lx CS:RIP=%04x:%016lx\n",
8760 vmcs_readl(GUEST_SYSENTER_ESP
),
8761 vmcs_read32(GUEST_SYSENTER_CS
), vmcs_readl(GUEST_SYSENTER_EIP
));
8762 vmx_dump_sel("CS: ", GUEST_CS_SELECTOR
);
8763 vmx_dump_sel("DS: ", GUEST_DS_SELECTOR
);
8764 vmx_dump_sel("SS: ", GUEST_SS_SELECTOR
);
8765 vmx_dump_sel("ES: ", GUEST_ES_SELECTOR
);
8766 vmx_dump_sel("FS: ", GUEST_FS_SELECTOR
);
8767 vmx_dump_sel("GS: ", GUEST_GS_SELECTOR
);
8768 vmx_dump_dtsel("GDTR:", GUEST_GDTR_LIMIT
);
8769 vmx_dump_sel("LDTR:", GUEST_LDTR_SELECTOR
);
8770 vmx_dump_dtsel("IDTR:", GUEST_IDTR_LIMIT
);
8771 vmx_dump_sel("TR: ", GUEST_TR_SELECTOR
);
8772 if ((vmexit_ctl
& (VM_EXIT_SAVE_IA32_PAT
| VM_EXIT_SAVE_IA32_EFER
)) ||
8773 (vmentry_ctl
& (VM_ENTRY_LOAD_IA32_PAT
| VM_ENTRY_LOAD_IA32_EFER
)))
8774 pr_err("EFER = 0x%016llx PAT = 0x%016llx\n",
8775 efer
, vmcs_read64(GUEST_IA32_PAT
));
8776 pr_err("DebugCtl = 0x%016llx DebugExceptions = 0x%016lx\n",
8777 vmcs_read64(GUEST_IA32_DEBUGCTL
),
8778 vmcs_readl(GUEST_PENDING_DBG_EXCEPTIONS
));
8779 if (vmentry_ctl
& VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL
)
8780 pr_err("PerfGlobCtl = 0x%016llx\n",
8781 vmcs_read64(GUEST_IA32_PERF_GLOBAL_CTRL
));
8782 if (vmentry_ctl
& VM_ENTRY_LOAD_BNDCFGS
)
8783 pr_err("BndCfgS = 0x%016llx\n", vmcs_read64(GUEST_BNDCFGS
));
8784 pr_err("Interruptibility = %08x ActivityState = %08x\n",
8785 vmcs_read32(GUEST_INTERRUPTIBILITY_INFO
),
8786 vmcs_read32(GUEST_ACTIVITY_STATE
));
8787 if (secondary_exec_control
& SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY
)
8788 pr_err("InterruptStatus = %04x\n",
8789 vmcs_read16(GUEST_INTR_STATUS
));
8791 pr_err("*** Host State ***\n");
8792 pr_err("RIP = 0x%016lx RSP = 0x%016lx\n",
8793 vmcs_readl(HOST_RIP
), vmcs_readl(HOST_RSP
));
8794 pr_err("CS=%04x SS=%04x DS=%04x ES=%04x FS=%04x GS=%04x TR=%04x\n",
8795 vmcs_read16(HOST_CS_SELECTOR
), vmcs_read16(HOST_SS_SELECTOR
),
8796 vmcs_read16(HOST_DS_SELECTOR
), vmcs_read16(HOST_ES_SELECTOR
),
8797 vmcs_read16(HOST_FS_SELECTOR
), vmcs_read16(HOST_GS_SELECTOR
),
8798 vmcs_read16(HOST_TR_SELECTOR
));
8799 pr_err("FSBase=%016lx GSBase=%016lx TRBase=%016lx\n",
8800 vmcs_readl(HOST_FS_BASE
), vmcs_readl(HOST_GS_BASE
),
8801 vmcs_readl(HOST_TR_BASE
));
8802 pr_err("GDTBase=%016lx IDTBase=%016lx\n",
8803 vmcs_readl(HOST_GDTR_BASE
), vmcs_readl(HOST_IDTR_BASE
));
8804 pr_err("CR0=%016lx CR3=%016lx CR4=%016lx\n",
8805 vmcs_readl(HOST_CR0
), vmcs_readl(HOST_CR3
),
8806 vmcs_readl(HOST_CR4
));
8807 pr_err("Sysenter RSP=%016lx CS:RIP=%04x:%016lx\n",
8808 vmcs_readl(HOST_IA32_SYSENTER_ESP
),
8809 vmcs_read32(HOST_IA32_SYSENTER_CS
),
8810 vmcs_readl(HOST_IA32_SYSENTER_EIP
));
8811 if (vmexit_ctl
& (VM_EXIT_LOAD_IA32_PAT
| VM_EXIT_LOAD_IA32_EFER
))
8812 pr_err("EFER = 0x%016llx PAT = 0x%016llx\n",
8813 vmcs_read64(HOST_IA32_EFER
),
8814 vmcs_read64(HOST_IA32_PAT
));
8815 if (vmexit_ctl
& VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL
)
8816 pr_err("PerfGlobCtl = 0x%016llx\n",
8817 vmcs_read64(HOST_IA32_PERF_GLOBAL_CTRL
));
8819 pr_err("*** Control State ***\n");
8820 pr_err("PinBased=%08x CPUBased=%08x SecondaryExec=%08x\n",
8821 pin_based_exec_ctrl
, cpu_based_exec_ctrl
, secondary_exec_control
);
8822 pr_err("EntryControls=%08x ExitControls=%08x\n", vmentry_ctl
, vmexit_ctl
);
8823 pr_err("ExceptionBitmap=%08x PFECmask=%08x PFECmatch=%08x\n",
8824 vmcs_read32(EXCEPTION_BITMAP
),
8825 vmcs_read32(PAGE_FAULT_ERROR_CODE_MASK
),
8826 vmcs_read32(PAGE_FAULT_ERROR_CODE_MATCH
));
8827 pr_err("VMEntry: intr_info=%08x errcode=%08x ilen=%08x\n",
8828 vmcs_read32(VM_ENTRY_INTR_INFO_FIELD
),
8829 vmcs_read32(VM_ENTRY_EXCEPTION_ERROR_CODE
),
8830 vmcs_read32(VM_ENTRY_INSTRUCTION_LEN
));
8831 pr_err("VMExit: intr_info=%08x errcode=%08x ilen=%08x\n",
8832 vmcs_read32(VM_EXIT_INTR_INFO
),
8833 vmcs_read32(VM_EXIT_INTR_ERROR_CODE
),
8834 vmcs_read32(VM_EXIT_INSTRUCTION_LEN
));
8835 pr_err(" reason=%08x qualification=%016lx\n",
8836 vmcs_read32(VM_EXIT_REASON
), vmcs_readl(EXIT_QUALIFICATION
));
8837 pr_err("IDTVectoring: info=%08x errcode=%08x\n",
8838 vmcs_read32(IDT_VECTORING_INFO_FIELD
),
8839 vmcs_read32(IDT_VECTORING_ERROR_CODE
));
8840 pr_err("TSC Offset = 0x%016llx\n", vmcs_read64(TSC_OFFSET
));
8841 if (secondary_exec_control
& SECONDARY_EXEC_TSC_SCALING
)
8842 pr_err("TSC Multiplier = 0x%016llx\n",
8843 vmcs_read64(TSC_MULTIPLIER
));
8844 if (cpu_based_exec_ctrl
& CPU_BASED_TPR_SHADOW
)
8845 pr_err("TPR Threshold = 0x%02x\n", vmcs_read32(TPR_THRESHOLD
));
8846 if (pin_based_exec_ctrl
& PIN_BASED_POSTED_INTR
)
8847 pr_err("PostedIntrVec = 0x%02x\n", vmcs_read16(POSTED_INTR_NV
));
8848 if ((secondary_exec_control
& SECONDARY_EXEC_ENABLE_EPT
))
8849 pr_err("EPT pointer = 0x%016llx\n", vmcs_read64(EPT_POINTER
));
8850 n
= vmcs_read32(CR3_TARGET_COUNT
);
8851 for (i
= 0; i
+ 1 < n
; i
+= 4)
8852 pr_err("CR3 target%u=%016lx target%u=%016lx\n",
8853 i
, vmcs_readl(CR3_TARGET_VALUE0
+ i
* 2),
8854 i
+ 1, vmcs_readl(CR3_TARGET_VALUE0
+ i
* 2 + 2));
8856 pr_err("CR3 target%u=%016lx\n",
8857 i
, vmcs_readl(CR3_TARGET_VALUE0
+ i
* 2));
8858 if (secondary_exec_control
& SECONDARY_EXEC_PAUSE_LOOP_EXITING
)
8859 pr_err("PLE Gap=%08x Window=%08x\n",
8860 vmcs_read32(PLE_GAP
), vmcs_read32(PLE_WINDOW
));
8861 if (secondary_exec_control
& SECONDARY_EXEC_ENABLE_VPID
)
8862 pr_err("Virtual processor ID = 0x%04x\n",
8863 vmcs_read16(VIRTUAL_PROCESSOR_ID
));
8867 * The guest has exited. See if we can fix it or if we need userspace
8870 static int vmx_handle_exit(struct kvm_vcpu
*vcpu
)
8872 struct vcpu_vmx
*vmx
= to_vmx(vcpu
);
8873 u32 exit_reason
= vmx
->exit_reason
;
8874 u32 vectoring_info
= vmx
->idt_vectoring_info
;
8876 trace_kvm_exit(exit_reason
, vcpu
, KVM_ISA_VMX
);
8879 * Flush logged GPAs PML buffer, this will make dirty_bitmap more
8880 * updated. Another good is, in kvm_vm_ioctl_get_dirty_log, before
8881 * querying dirty_bitmap, we only need to kick all vcpus out of guest
8882 * mode as if vcpus is in root mode, the PML buffer must has been
8886 vmx_flush_pml_buffer(vcpu
);
8888 /* If guest state is invalid, start emulating */
8889 if (vmx
->emulation_required
)
8890 return handle_invalid_guest_state(vcpu
);
8892 if (is_guest_mode(vcpu
) && nested_vmx_exit_reflected(vcpu
, exit_reason
))
8893 return nested_vmx_reflect_vmexit(vcpu
, exit_reason
);
8895 if (exit_reason
& VMX_EXIT_REASONS_FAILED_VMENTRY
) {
8897 vcpu
->run
->exit_reason
= KVM_EXIT_FAIL_ENTRY
;
8898 vcpu
->run
->fail_entry
.hardware_entry_failure_reason
8903 if (unlikely(vmx
->fail
)) {
8904 vcpu
->run
->exit_reason
= KVM_EXIT_FAIL_ENTRY
;
8905 vcpu
->run
->fail_entry
.hardware_entry_failure_reason
8906 = vmcs_read32(VM_INSTRUCTION_ERROR
);
8912 * Do not try to fix EXIT_REASON_EPT_MISCONFIG if it caused by
8913 * delivery event since it indicates guest is accessing MMIO.
8914 * The vm-exit can be triggered again after return to guest that
8915 * will cause infinite loop.
8917 if ((vectoring_info
& VECTORING_INFO_VALID_MASK
) &&
8918 (exit_reason
!= EXIT_REASON_EXCEPTION_NMI
&&
8919 exit_reason
!= EXIT_REASON_EPT_VIOLATION
&&
8920 exit_reason
!= EXIT_REASON_PML_FULL
&&
8921 exit_reason
!= EXIT_REASON_TASK_SWITCH
)) {
8922 vcpu
->run
->exit_reason
= KVM_EXIT_INTERNAL_ERROR
;
8923 vcpu
->run
->internal
.suberror
= KVM_INTERNAL_ERROR_DELIVERY_EV
;
8924 vcpu
->run
->internal
.ndata
= 3;
8925 vcpu
->run
->internal
.data
[0] = vectoring_info
;
8926 vcpu
->run
->internal
.data
[1] = exit_reason
;
8927 vcpu
->run
->internal
.data
[2] = vcpu
->arch
.exit_qualification
;
8928 if (exit_reason
== EXIT_REASON_EPT_MISCONFIG
) {
8929 vcpu
->run
->internal
.ndata
++;
8930 vcpu
->run
->internal
.data
[3] =
8931 vmcs_read64(GUEST_PHYSICAL_ADDRESS
);
8936 if (unlikely(!enable_vnmi
&&
8937 vmx
->loaded_vmcs
->soft_vnmi_blocked
)) {
8938 if (vmx_interrupt_allowed(vcpu
)) {
8939 vmx
->loaded_vmcs
->soft_vnmi_blocked
= 0;
8940 } else if (vmx
->loaded_vmcs
->vnmi_blocked_time
> 1000000000LL &&
8941 vcpu
->arch
.nmi_pending
) {
8943 * This CPU don't support us in finding the end of an
8944 * NMI-blocked window if the guest runs with IRQs
8945 * disabled. So we pull the trigger after 1 s of
8946 * futile waiting, but inform the user about this.
8948 printk(KERN_WARNING
"%s: Breaking out of NMI-blocked "
8949 "state on VCPU %d after 1 s timeout\n",
8950 __func__
, vcpu
->vcpu_id
);
8951 vmx
->loaded_vmcs
->soft_vnmi_blocked
= 0;
8955 if (exit_reason
< kvm_vmx_max_exit_handlers
8956 && kvm_vmx_exit_handlers
[exit_reason
])
8957 return kvm_vmx_exit_handlers
[exit_reason
](vcpu
);
8959 vcpu_unimpl(vcpu
, "vmx: unexpected exit reason 0x%x\n",
8961 kvm_queue_exception(vcpu
, UD_VECTOR
);
8966 static void update_cr8_intercept(struct kvm_vcpu
*vcpu
, int tpr
, int irr
)
8968 struct vmcs12
*vmcs12
= get_vmcs12(vcpu
);
8970 if (is_guest_mode(vcpu
) &&
8971 nested_cpu_has(vmcs12
, CPU_BASED_TPR_SHADOW
))
8974 if (irr
== -1 || tpr
< irr
) {
8975 vmcs_write32(TPR_THRESHOLD
, 0);
8979 vmcs_write32(TPR_THRESHOLD
, irr
);
8982 static void vmx_set_virtual_x2apic_mode(struct kvm_vcpu
*vcpu
, bool set
)
8984 u32 sec_exec_control
;
8986 /* Postpone execution until vmcs01 is the current VMCS. */
8987 if (is_guest_mode(vcpu
)) {
8988 to_vmx(vcpu
)->nested
.change_vmcs01_virtual_x2apic_mode
= true;
8992 if (!cpu_has_vmx_virtualize_x2apic_mode())
8995 if (!cpu_need_tpr_shadow(vcpu
))
8998 sec_exec_control
= vmcs_read32(SECONDARY_VM_EXEC_CONTROL
);
9001 sec_exec_control
&= ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES
;
9002 sec_exec_control
|= SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE
;
9004 sec_exec_control
&= ~SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE
;
9005 sec_exec_control
|= SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES
;
9006 vmx_flush_tlb_ept_only(vcpu
);
9008 vmcs_write32(SECONDARY_VM_EXEC_CONTROL
, sec_exec_control
);
9010 vmx_update_msr_bitmap(vcpu
);
9013 static void vmx_set_apic_access_page_addr(struct kvm_vcpu
*vcpu
, hpa_t hpa
)
9015 struct vcpu_vmx
*vmx
= to_vmx(vcpu
);
9018 * Currently we do not handle the nested case where L2 has an
9019 * APIC access page of its own; that page is still pinned.
9020 * Hence, we skip the case where the VCPU is in guest mode _and_
9021 * L1 prepared an APIC access page for L2.
9023 * For the case where L1 and L2 share the same APIC access page
9024 * (flexpriority=Y but SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES clear
9025 * in the vmcs12), this function will only update either the vmcs01
9026 * or the vmcs02. If the former, the vmcs02 will be updated by
9027 * prepare_vmcs02. If the latter, the vmcs01 will be updated in
9028 * the next L2->L1 exit.
9030 if (!is_guest_mode(vcpu
) ||
9031 !nested_cpu_has2(get_vmcs12(&vmx
->vcpu
),
9032 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES
)) {
9033 vmcs_write64(APIC_ACCESS_ADDR
, hpa
);
9034 vmx_flush_tlb_ept_only(vcpu
);
9038 static void vmx_hwapic_isr_update(struct kvm_vcpu
*vcpu
, int max_isr
)
9046 status
= vmcs_read16(GUEST_INTR_STATUS
);
9048 if (max_isr
!= old
) {
9050 status
|= max_isr
<< 8;
9051 vmcs_write16(GUEST_INTR_STATUS
, status
);
9055 static void vmx_set_rvi(int vector
)
9063 status
= vmcs_read16(GUEST_INTR_STATUS
);
9064 old
= (u8
)status
& 0xff;
9065 if ((u8
)vector
!= old
) {
9067 status
|= (u8
)vector
;
9068 vmcs_write16(GUEST_INTR_STATUS
, status
);
9072 static void vmx_hwapic_irr_update(struct kvm_vcpu
*vcpu
, int max_irr
)
9075 * When running L2, updating RVI is only relevant when
9076 * vmcs12 virtual-interrupt-delivery enabled.
9077 * However, it can be enabled only when L1 also
9078 * intercepts external-interrupts and in that case
9079 * we should not update vmcs02 RVI but instead intercept
9080 * interrupt. Therefore, do nothing when running L2.
9082 if (!is_guest_mode(vcpu
))
9083 vmx_set_rvi(max_irr
);
9086 static int vmx_sync_pir_to_irr(struct kvm_vcpu
*vcpu
)
9088 struct vcpu_vmx
*vmx
= to_vmx(vcpu
);
9090 bool max_irr_updated
;
9092 WARN_ON(!vcpu
->arch
.apicv_active
);
9093 if (pi_test_on(&vmx
->pi_desc
)) {
9094 pi_clear_on(&vmx
->pi_desc
);
9096 * IOMMU can write to PIR.ON, so the barrier matters even on UP.
9097 * But on x86 this is just a compiler barrier anyway.
9099 smp_mb__after_atomic();
9101 kvm_apic_update_irr(vcpu
, vmx
->pi_desc
.pir
, &max_irr
);
9104 * If we are running L2 and L1 has a new pending interrupt
9105 * which can be injected, we should re-evaluate
9106 * what should be done with this new L1 interrupt.
9107 * If L1 intercepts external-interrupts, we should
9108 * exit from L2 to L1. Otherwise, interrupt should be
9109 * delivered directly to L2.
9111 if (is_guest_mode(vcpu
) && max_irr_updated
) {
9112 if (nested_exit_on_intr(vcpu
))
9113 kvm_vcpu_exiting_guest_mode(vcpu
);
9115 kvm_make_request(KVM_REQ_EVENT
, vcpu
);
9118 max_irr
= kvm_lapic_find_highest_irr(vcpu
);
9120 vmx_hwapic_irr_update(vcpu
, max_irr
);
9124 static void vmx_load_eoi_exitmap(struct kvm_vcpu
*vcpu
, u64
*eoi_exit_bitmap
)
9126 if (!kvm_vcpu_apicv_active(vcpu
))
9129 vmcs_write64(EOI_EXIT_BITMAP0
, eoi_exit_bitmap
[0]);
9130 vmcs_write64(EOI_EXIT_BITMAP1
, eoi_exit_bitmap
[1]);
9131 vmcs_write64(EOI_EXIT_BITMAP2
, eoi_exit_bitmap
[2]);
9132 vmcs_write64(EOI_EXIT_BITMAP3
, eoi_exit_bitmap
[3]);
9135 static void vmx_apicv_post_state_restore(struct kvm_vcpu
*vcpu
)
9137 struct vcpu_vmx
*vmx
= to_vmx(vcpu
);
9139 pi_clear_on(&vmx
->pi_desc
);
9140 memset(vmx
->pi_desc
.pir
, 0, sizeof(vmx
->pi_desc
.pir
));
9143 static void vmx_complete_atomic_exit(struct vcpu_vmx
*vmx
)
9145 u32 exit_intr_info
= 0;
9146 u16 basic_exit_reason
= (u16
)vmx
->exit_reason
;
9148 if (!(basic_exit_reason
== EXIT_REASON_MCE_DURING_VMENTRY
9149 || basic_exit_reason
== EXIT_REASON_EXCEPTION_NMI
))
9152 if (!(vmx
->exit_reason
& VMX_EXIT_REASONS_FAILED_VMENTRY
))
9153 exit_intr_info
= vmcs_read32(VM_EXIT_INTR_INFO
);
9154 vmx
->exit_intr_info
= exit_intr_info
;
9156 /* if exit due to PF check for async PF */
9157 if (is_page_fault(exit_intr_info
))
9158 vmx
->vcpu
.arch
.apf
.host_apf_reason
= kvm_read_and_reset_pf_reason();
9160 /* Handle machine checks before interrupts are enabled */
9161 if (basic_exit_reason
== EXIT_REASON_MCE_DURING_VMENTRY
||
9162 is_machine_check(exit_intr_info
))
9163 kvm_machine_check();
9165 /* We need to handle NMIs before interrupts are enabled */
9166 if (is_nmi(exit_intr_info
)) {
9167 kvm_before_handle_nmi(&vmx
->vcpu
);
9169 kvm_after_handle_nmi(&vmx
->vcpu
);
9173 static void vmx_handle_external_intr(struct kvm_vcpu
*vcpu
)
9175 u32 exit_intr_info
= vmcs_read32(VM_EXIT_INTR_INFO
);
9177 if ((exit_intr_info
& (INTR_INFO_VALID_MASK
| INTR_INFO_INTR_TYPE_MASK
))
9178 == (INTR_INFO_VALID_MASK
| INTR_TYPE_EXT_INTR
)) {
9179 unsigned int vector
;
9180 unsigned long entry
;
9182 struct vcpu_vmx
*vmx
= to_vmx(vcpu
);
9183 #ifdef CONFIG_X86_64
9187 vector
= exit_intr_info
& INTR_INFO_VECTOR_MASK
;
9188 desc
= (gate_desc
*)vmx
->host_idt_base
+ vector
;
9189 entry
= gate_offset(desc
);
9191 #ifdef CONFIG_X86_64
9192 "mov %%" _ASM_SP
", %[sp]\n\t"
9193 "and $0xfffffffffffffff0, %%" _ASM_SP
"\n\t"
9198 __ASM_SIZE(push
) " $%c[cs]\n\t"
9201 #ifdef CONFIG_X86_64
9206 THUNK_TARGET(entry
),
9207 [ss
]"i"(__KERNEL_DS
),
9208 [cs
]"i"(__KERNEL_CS
)
9212 STACK_FRAME_NON_STANDARD(vmx_handle_external_intr
);
9214 static bool vmx_has_high_real_mode_segbase(void)
9216 return enable_unrestricted_guest
|| emulate_invalid_guest_state
;
9219 static bool vmx_mpx_supported(void)
9221 return (vmcs_config
.vmexit_ctrl
& VM_EXIT_CLEAR_BNDCFGS
) &&
9222 (vmcs_config
.vmentry_ctrl
& VM_ENTRY_LOAD_BNDCFGS
);
9225 static bool vmx_xsaves_supported(void)
9227 return vmcs_config
.cpu_based_2nd_exec_ctrl
&
9228 SECONDARY_EXEC_XSAVES
;
9231 static bool vmx_umip_emulated(void)
9233 return vmcs_config
.cpu_based_2nd_exec_ctrl
&
9234 SECONDARY_EXEC_DESC
;
9237 static void vmx_recover_nmi_blocking(struct vcpu_vmx
*vmx
)
9242 bool idtv_info_valid
;
9244 idtv_info_valid
= vmx
->idt_vectoring_info
& VECTORING_INFO_VALID_MASK
;
9247 if (vmx
->loaded_vmcs
->nmi_known_unmasked
)
9250 * Can't use vmx->exit_intr_info since we're not sure what
9251 * the exit reason is.
9253 exit_intr_info
= vmcs_read32(VM_EXIT_INTR_INFO
);
9254 unblock_nmi
= (exit_intr_info
& INTR_INFO_UNBLOCK_NMI
) != 0;
9255 vector
= exit_intr_info
& INTR_INFO_VECTOR_MASK
;
9257 * SDM 3: 27.7.1.2 (September 2008)
9258 * Re-set bit "block by NMI" before VM entry if vmexit caused by
9259 * a guest IRET fault.
9260 * SDM 3: 23.2.2 (September 2008)
9261 * Bit 12 is undefined in any of the following cases:
9262 * If the VM exit sets the valid bit in the IDT-vectoring
9263 * information field.
9264 * If the VM exit is due to a double fault.
9266 if ((exit_intr_info
& INTR_INFO_VALID_MASK
) && unblock_nmi
&&
9267 vector
!= DF_VECTOR
&& !idtv_info_valid
)
9268 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO
,
9269 GUEST_INTR_STATE_NMI
);
9271 vmx
->loaded_vmcs
->nmi_known_unmasked
=
9272 !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO
)
9273 & GUEST_INTR_STATE_NMI
);
9274 } else if (unlikely(vmx
->loaded_vmcs
->soft_vnmi_blocked
))
9275 vmx
->loaded_vmcs
->vnmi_blocked_time
+=
9276 ktime_to_ns(ktime_sub(ktime_get(),
9277 vmx
->loaded_vmcs
->entry_time
));
9280 static void __vmx_complete_interrupts(struct kvm_vcpu
*vcpu
,
9281 u32 idt_vectoring_info
,
9282 int instr_len_field
,
9283 int error_code_field
)
9287 bool idtv_info_valid
;
9289 idtv_info_valid
= idt_vectoring_info
& VECTORING_INFO_VALID_MASK
;
9291 vcpu
->arch
.nmi_injected
= false;
9292 kvm_clear_exception_queue(vcpu
);
9293 kvm_clear_interrupt_queue(vcpu
);
9295 if (!idtv_info_valid
)
9298 kvm_make_request(KVM_REQ_EVENT
, vcpu
);
9300 vector
= idt_vectoring_info
& VECTORING_INFO_VECTOR_MASK
;
9301 type
= idt_vectoring_info
& VECTORING_INFO_TYPE_MASK
;
9304 case INTR_TYPE_NMI_INTR
:
9305 vcpu
->arch
.nmi_injected
= true;
9307 * SDM 3: 27.7.1.2 (September 2008)
9308 * Clear bit "block by NMI" before VM entry if a NMI
9311 vmx_set_nmi_mask(vcpu
, false);
9313 case INTR_TYPE_SOFT_EXCEPTION
:
9314 vcpu
->arch
.event_exit_inst_len
= vmcs_read32(instr_len_field
);
9316 case INTR_TYPE_HARD_EXCEPTION
:
9317 if (idt_vectoring_info
& VECTORING_INFO_DELIVER_CODE_MASK
) {
9318 u32 err
= vmcs_read32(error_code_field
);
9319 kvm_requeue_exception_e(vcpu
, vector
, err
);
9321 kvm_requeue_exception(vcpu
, vector
);
9323 case INTR_TYPE_SOFT_INTR
:
9324 vcpu
->arch
.event_exit_inst_len
= vmcs_read32(instr_len_field
);
9326 case INTR_TYPE_EXT_INTR
:
9327 kvm_queue_interrupt(vcpu
, vector
, type
== INTR_TYPE_SOFT_INTR
);
9334 static void vmx_complete_interrupts(struct vcpu_vmx
*vmx
)
9336 __vmx_complete_interrupts(&vmx
->vcpu
, vmx
->idt_vectoring_info
,
9337 VM_EXIT_INSTRUCTION_LEN
,
9338 IDT_VECTORING_ERROR_CODE
);
9341 static void vmx_cancel_injection(struct kvm_vcpu
*vcpu
)
9343 __vmx_complete_interrupts(vcpu
,
9344 vmcs_read32(VM_ENTRY_INTR_INFO_FIELD
),
9345 VM_ENTRY_INSTRUCTION_LEN
,
9346 VM_ENTRY_EXCEPTION_ERROR_CODE
);
9348 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD
, 0);
9351 static void atomic_switch_perf_msrs(struct vcpu_vmx
*vmx
)
9354 struct perf_guest_switch_msr
*msrs
;
9356 msrs
= perf_guest_get_msrs(&nr_msrs
);
9361 for (i
= 0; i
< nr_msrs
; i
++)
9362 if (msrs
[i
].host
== msrs
[i
].guest
)
9363 clear_atomic_switch_msr(vmx
, msrs
[i
].msr
);
9365 add_atomic_switch_msr(vmx
, msrs
[i
].msr
, msrs
[i
].guest
,
9369 static void vmx_arm_hv_timer(struct kvm_vcpu
*vcpu
)
9371 struct vcpu_vmx
*vmx
= to_vmx(vcpu
);
9375 if (vmx
->hv_deadline_tsc
== -1)
9379 if (vmx
->hv_deadline_tsc
> tscl
)
9380 /* sure to be 32 bit only because checked on set_hv_timer */
9381 delta_tsc
= (u32
)((vmx
->hv_deadline_tsc
- tscl
) >>
9382 cpu_preemption_timer_multi
);
9386 vmcs_write32(VMX_PREEMPTION_TIMER_VALUE
, delta_tsc
);
9389 static void __noclone
vmx_vcpu_run(struct kvm_vcpu
*vcpu
)
9391 struct vcpu_vmx
*vmx
= to_vmx(vcpu
);
9392 unsigned long cr3
, cr4
;
9394 /* Record the guest's net vcpu time for enforced NMI injections. */
9395 if (unlikely(!enable_vnmi
&&
9396 vmx
->loaded_vmcs
->soft_vnmi_blocked
))
9397 vmx
->loaded_vmcs
->entry_time
= ktime_get();
9399 /* Don't enter VMX if guest state is invalid, let the exit handler
9400 start emulation until we arrive back to a valid state */
9401 if (vmx
->emulation_required
)
9404 if (vmx
->ple_window_dirty
) {
9405 vmx
->ple_window_dirty
= false;
9406 vmcs_write32(PLE_WINDOW
, vmx
->ple_window
);
9409 if (vmx
->nested
.sync_shadow_vmcs
) {
9410 copy_vmcs12_to_shadow(vmx
);
9411 vmx
->nested
.sync_shadow_vmcs
= false;
9414 if (test_bit(VCPU_REGS_RSP
, (unsigned long *)&vcpu
->arch
.regs_dirty
))
9415 vmcs_writel(GUEST_RSP
, vcpu
->arch
.regs
[VCPU_REGS_RSP
]);
9416 if (test_bit(VCPU_REGS_RIP
, (unsigned long *)&vcpu
->arch
.regs_dirty
))
9417 vmcs_writel(GUEST_RIP
, vcpu
->arch
.regs
[VCPU_REGS_RIP
]);
9419 cr3
= __get_current_cr3_fast();
9420 if (unlikely(cr3
!= vmx
->loaded_vmcs
->vmcs_host_cr3
)) {
9421 vmcs_writel(HOST_CR3
, cr3
);
9422 vmx
->loaded_vmcs
->vmcs_host_cr3
= cr3
;
9425 cr4
= cr4_read_shadow();
9426 if (unlikely(cr4
!= vmx
->loaded_vmcs
->vmcs_host_cr4
)) {
9427 vmcs_writel(HOST_CR4
, cr4
);
9428 vmx
->loaded_vmcs
->vmcs_host_cr4
= cr4
;
9431 /* When single-stepping over STI and MOV SS, we must clear the
9432 * corresponding interruptibility bits in the guest state. Otherwise
9433 * vmentry fails as it then expects bit 14 (BS) in pending debug
9434 * exceptions being set, but that's not correct for the guest debugging
9436 if (vcpu
->guest_debug
& KVM_GUESTDBG_SINGLESTEP
)
9437 vmx_set_interrupt_shadow(vcpu
, 0);
9439 if (static_cpu_has(X86_FEATURE_PKU
) &&
9440 kvm_read_cr4_bits(vcpu
, X86_CR4_PKE
) &&
9441 vcpu
->arch
.pkru
!= vmx
->host_pkru
)
9442 __write_pkru(vcpu
->arch
.pkru
);
9444 atomic_switch_perf_msrs(vmx
);
9446 vmx_arm_hv_timer(vcpu
);
9449 * If this vCPU has touched SPEC_CTRL, restore the guest's value if
9450 * it's non-zero. Since vmentry is serialising on affected CPUs, there
9451 * is no need to worry about the conditional branch over the wrmsr
9452 * being speculatively taken.
9455 wrmsrl(MSR_IA32_SPEC_CTRL
, vmx
->spec_ctrl
);
9457 vmx
->__launched
= vmx
->loaded_vmcs
->launched
;
9459 /* Store host registers */
9460 "push %%" _ASM_DX
"; push %%" _ASM_BP
";"
9461 "push %%" _ASM_CX
" \n\t" /* placeholder for guest rcx */
9462 "push %%" _ASM_CX
" \n\t"
9463 "cmp %%" _ASM_SP
", %c[host_rsp](%0) \n\t"
9465 "mov %%" _ASM_SP
", %c[host_rsp](%0) \n\t"
9466 __ex(ASM_VMX_VMWRITE_RSP_RDX
) "\n\t"
9468 /* Reload cr2 if changed */
9469 "mov %c[cr2](%0), %%" _ASM_AX
" \n\t"
9470 "mov %%cr2, %%" _ASM_DX
" \n\t"
9471 "cmp %%" _ASM_AX
", %%" _ASM_DX
" \n\t"
9473 "mov %%" _ASM_AX
", %%cr2 \n\t"
9475 /* Check if vmlaunch of vmresume is needed */
9476 "cmpl $0, %c[launched](%0) \n\t"
9477 /* Load guest registers. Don't clobber flags. */
9478 "mov %c[rax](%0), %%" _ASM_AX
" \n\t"
9479 "mov %c[rbx](%0), %%" _ASM_BX
" \n\t"
9480 "mov %c[rdx](%0), %%" _ASM_DX
" \n\t"
9481 "mov %c[rsi](%0), %%" _ASM_SI
" \n\t"
9482 "mov %c[rdi](%0), %%" _ASM_DI
" \n\t"
9483 "mov %c[rbp](%0), %%" _ASM_BP
" \n\t"
9484 #ifdef CONFIG_X86_64
9485 "mov %c[r8](%0), %%r8 \n\t"
9486 "mov %c[r9](%0), %%r9 \n\t"
9487 "mov %c[r10](%0), %%r10 \n\t"
9488 "mov %c[r11](%0), %%r11 \n\t"
9489 "mov %c[r12](%0), %%r12 \n\t"
9490 "mov %c[r13](%0), %%r13 \n\t"
9491 "mov %c[r14](%0), %%r14 \n\t"
9492 "mov %c[r15](%0), %%r15 \n\t"
9494 "mov %c[rcx](%0), %%" _ASM_CX
" \n\t" /* kills %0 (ecx) */
9496 /* Enter guest mode */
9498 __ex(ASM_VMX_VMLAUNCH
) "\n\t"
9500 "1: " __ex(ASM_VMX_VMRESUME
) "\n\t"
9502 /* Save guest registers, load host registers, keep flags */
9503 "mov %0, %c[wordsize](%%" _ASM_SP
") \n\t"
9505 "setbe %c[fail](%0)\n\t"
9506 "mov %%" _ASM_AX
", %c[rax](%0) \n\t"
9507 "mov %%" _ASM_BX
", %c[rbx](%0) \n\t"
9508 __ASM_SIZE(pop
) " %c[rcx](%0) \n\t"
9509 "mov %%" _ASM_DX
", %c[rdx](%0) \n\t"
9510 "mov %%" _ASM_SI
", %c[rsi](%0) \n\t"
9511 "mov %%" _ASM_DI
", %c[rdi](%0) \n\t"
9512 "mov %%" _ASM_BP
", %c[rbp](%0) \n\t"
9513 #ifdef CONFIG_X86_64
9514 "mov %%r8, %c[r8](%0) \n\t"
9515 "mov %%r9, %c[r9](%0) \n\t"
9516 "mov %%r10, %c[r10](%0) \n\t"
9517 "mov %%r11, %c[r11](%0) \n\t"
9518 "mov %%r12, %c[r12](%0) \n\t"
9519 "mov %%r13, %c[r13](%0) \n\t"
9520 "mov %%r14, %c[r14](%0) \n\t"
9521 "mov %%r15, %c[r15](%0) \n\t"
9522 "xor %%r8d, %%r8d \n\t"
9523 "xor %%r9d, %%r9d \n\t"
9524 "xor %%r10d, %%r10d \n\t"
9525 "xor %%r11d, %%r11d \n\t"
9526 "xor %%r12d, %%r12d \n\t"
9527 "xor %%r13d, %%r13d \n\t"
9528 "xor %%r14d, %%r14d \n\t"
9529 "xor %%r15d, %%r15d \n\t"
9531 "mov %%cr2, %%" _ASM_AX
" \n\t"
9532 "mov %%" _ASM_AX
", %c[cr2](%0) \n\t"
9534 "xor %%eax, %%eax \n\t"
9535 "xor %%ebx, %%ebx \n\t"
9536 "xor %%esi, %%esi \n\t"
9537 "xor %%edi, %%edi \n\t"
9538 "pop %%" _ASM_BP
"; pop %%" _ASM_DX
" \n\t"
9539 ".pushsection .rodata \n\t"
9540 ".global vmx_return \n\t"
9541 "vmx_return: " _ASM_PTR
" 2b \n\t"
9543 : : "c"(vmx
), "d"((unsigned long)HOST_RSP
),
9544 [launched
]"i"(offsetof(struct vcpu_vmx
, __launched
)),
9545 [fail
]"i"(offsetof(struct vcpu_vmx
, fail
)),
9546 [host_rsp
]"i"(offsetof(struct vcpu_vmx
, host_rsp
)),
9547 [rax
]"i"(offsetof(struct vcpu_vmx
, vcpu
.arch
.regs
[VCPU_REGS_RAX
])),
9548 [rbx
]"i"(offsetof(struct vcpu_vmx
, vcpu
.arch
.regs
[VCPU_REGS_RBX
])),
9549 [rcx
]"i"(offsetof(struct vcpu_vmx
, vcpu
.arch
.regs
[VCPU_REGS_RCX
])),
9550 [rdx
]"i"(offsetof(struct vcpu_vmx
, vcpu
.arch
.regs
[VCPU_REGS_RDX
])),
9551 [rsi
]"i"(offsetof(struct vcpu_vmx
, vcpu
.arch
.regs
[VCPU_REGS_RSI
])),
9552 [rdi
]"i"(offsetof(struct vcpu_vmx
, vcpu
.arch
.regs
[VCPU_REGS_RDI
])),
9553 [rbp
]"i"(offsetof(struct vcpu_vmx
, vcpu
.arch
.regs
[VCPU_REGS_RBP
])),
9554 #ifdef CONFIG_X86_64
9555 [r8
]"i"(offsetof(struct vcpu_vmx
, vcpu
.arch
.regs
[VCPU_REGS_R8
])),
9556 [r9
]"i"(offsetof(struct vcpu_vmx
, vcpu
.arch
.regs
[VCPU_REGS_R9
])),
9557 [r10
]"i"(offsetof(struct vcpu_vmx
, vcpu
.arch
.regs
[VCPU_REGS_R10
])),
9558 [r11
]"i"(offsetof(struct vcpu_vmx
, vcpu
.arch
.regs
[VCPU_REGS_R11
])),
9559 [r12
]"i"(offsetof(struct vcpu_vmx
, vcpu
.arch
.regs
[VCPU_REGS_R12
])),
9560 [r13
]"i"(offsetof(struct vcpu_vmx
, vcpu
.arch
.regs
[VCPU_REGS_R13
])),
9561 [r14
]"i"(offsetof(struct vcpu_vmx
, vcpu
.arch
.regs
[VCPU_REGS_R14
])),
9562 [r15
]"i"(offsetof(struct vcpu_vmx
, vcpu
.arch
.regs
[VCPU_REGS_R15
])),
9564 [cr2
]"i"(offsetof(struct vcpu_vmx
, vcpu
.arch
.cr2
)),
9565 [wordsize
]"i"(sizeof(ulong
))
9567 #ifdef CONFIG_X86_64
9568 , "rax", "rbx", "rdi", "rsi"
9569 , "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15"
9571 , "eax", "ebx", "edi", "esi"
9576 * We do not use IBRS in the kernel. If this vCPU has used the
9577 * SPEC_CTRL MSR it may have left it on; save the value and
9578 * turn it off. This is much more efficient than blindly adding
9579 * it to the atomic save/restore list. Especially as the former
9580 * (Saving guest MSRs on vmexit) doesn't even exist in KVM.
9582 * For non-nested case:
9583 * If the L01 MSR bitmap does not intercept the MSR, then we need to
9587 * If the L02 MSR bitmap does not intercept the MSR, then we need to
9590 if (!msr_write_intercepted(vcpu
, MSR_IA32_SPEC_CTRL
))
9591 rdmsrl(MSR_IA32_SPEC_CTRL
, vmx
->spec_ctrl
);
9594 wrmsrl(MSR_IA32_SPEC_CTRL
, 0);
9596 /* Eliminate branch target predictions from guest mode */
9599 /* MSR_IA32_DEBUGCTLMSR is zeroed on vmexit. Restore it if needed */
9600 if (vmx
->host_debugctlmsr
)
9601 update_debugctlmsr(vmx
->host_debugctlmsr
);
9603 #ifndef CONFIG_X86_64
9605 * The sysexit path does not restore ds/es, so we must set them to
9606 * a reasonable value ourselves.
9608 * We can't defer this to vmx_load_host_state() since that function
9609 * may be executed in interrupt context, which saves and restore segments
9610 * around it, nullifying its effect.
9612 loadsegment(ds
, __USER_DS
);
9613 loadsegment(es
, __USER_DS
);
9616 vcpu
->arch
.regs_avail
= ~((1 << VCPU_REGS_RIP
) | (1 << VCPU_REGS_RSP
)
9617 | (1 << VCPU_EXREG_RFLAGS
)
9618 | (1 << VCPU_EXREG_PDPTR
)
9619 | (1 << VCPU_EXREG_SEGMENTS
)
9620 | (1 << VCPU_EXREG_CR3
));
9621 vcpu
->arch
.regs_dirty
= 0;
9624 * eager fpu is enabled if PKEY is supported and CR4 is switched
9625 * back on host, so it is safe to read guest PKRU from current
9628 if (static_cpu_has(X86_FEATURE_PKU
) &&
9629 kvm_read_cr4_bits(vcpu
, X86_CR4_PKE
)) {
9630 vcpu
->arch
.pkru
= __read_pkru();
9631 if (vcpu
->arch
.pkru
!= vmx
->host_pkru
)
9632 __write_pkru(vmx
->host_pkru
);
9636 * the KVM_REQ_EVENT optimization bit is only on for one entry, and if
9637 * we did not inject a still-pending event to L1 now because of
9638 * nested_run_pending, we need to re-enable this bit.
9640 if (vmx
->nested
.nested_run_pending
)
9641 kvm_make_request(KVM_REQ_EVENT
, vcpu
);
9643 vmx
->nested
.nested_run_pending
= 0;
9644 vmx
->idt_vectoring_info
= 0;
9646 vmx
->exit_reason
= vmx
->fail
? 0xdead : vmcs_read32(VM_EXIT_REASON
);
9647 if (vmx
->fail
|| (vmx
->exit_reason
& VMX_EXIT_REASONS_FAILED_VMENTRY
))
9650 vmx
->loaded_vmcs
->launched
= 1;
9651 vmx
->idt_vectoring_info
= vmcs_read32(IDT_VECTORING_INFO_FIELD
);
9653 vmx_complete_atomic_exit(vmx
);
9654 vmx_recover_nmi_blocking(vmx
);
9655 vmx_complete_interrupts(vmx
);
9657 STACK_FRAME_NON_STANDARD(vmx_vcpu_run
);
9659 static void vmx_switch_vmcs(struct kvm_vcpu
*vcpu
, struct loaded_vmcs
*vmcs
)
9661 struct vcpu_vmx
*vmx
= to_vmx(vcpu
);
9664 if (vmx
->loaded_vmcs
== vmcs
)
9668 vmx
->loaded_vmcs
= vmcs
;
9670 vmx_vcpu_load(vcpu
, cpu
);
9675 * Ensure that the current vmcs of the logical processor is the
9676 * vmcs01 of the vcpu before calling free_nested().
9678 static void vmx_free_vcpu_nested(struct kvm_vcpu
*vcpu
)
9680 struct vcpu_vmx
*vmx
= to_vmx(vcpu
);
9683 vmx_switch_vmcs(vcpu
, &vmx
->vmcs01
);
9688 static void vmx_free_vcpu(struct kvm_vcpu
*vcpu
)
9690 struct vcpu_vmx
*vmx
= to_vmx(vcpu
);
9693 vmx_destroy_pml_buffer(vmx
);
9694 free_vpid(vmx
->vpid
);
9695 leave_guest_mode(vcpu
);
9696 vmx_free_vcpu_nested(vcpu
);
9697 free_loaded_vmcs(vmx
->loaded_vmcs
);
9698 kfree(vmx
->guest_msrs
);
9699 kvm_vcpu_uninit(vcpu
);
9700 kmem_cache_free(kvm_vcpu_cache
, vmx
);
9703 static struct kvm_vcpu
*vmx_create_vcpu(struct kvm
*kvm
, unsigned int id
)
9706 struct vcpu_vmx
*vmx
= kmem_cache_zalloc(kvm_vcpu_cache
, GFP_KERNEL
);
9707 unsigned long *msr_bitmap
;
9711 return ERR_PTR(-ENOMEM
);
9713 vmx
->vpid
= allocate_vpid();
9715 err
= kvm_vcpu_init(&vmx
->vcpu
, kvm
, id
);
9722 * If PML is turned on, failure on enabling PML just results in failure
9723 * of creating the vcpu, therefore we can simplify PML logic (by
9724 * avoiding dealing with cases, such as enabling PML partially on vcpus
9725 * for the guest, etc.
9728 vmx
->pml_pg
= alloc_page(GFP_KERNEL
| __GFP_ZERO
);
9733 vmx
->guest_msrs
= kmalloc(PAGE_SIZE
, GFP_KERNEL
);
9734 BUILD_BUG_ON(ARRAY_SIZE(vmx_msr_index
) * sizeof(vmx
->guest_msrs
[0])
9737 if (!vmx
->guest_msrs
)
9740 err
= alloc_loaded_vmcs(&vmx
->vmcs01
);
9744 msr_bitmap
= vmx
->vmcs01
.msr_bitmap
;
9745 vmx_disable_intercept_for_msr(msr_bitmap
, MSR_FS_BASE
, MSR_TYPE_RW
);
9746 vmx_disable_intercept_for_msr(msr_bitmap
, MSR_GS_BASE
, MSR_TYPE_RW
);
9747 vmx_disable_intercept_for_msr(msr_bitmap
, MSR_KERNEL_GS_BASE
, MSR_TYPE_RW
);
9748 vmx_disable_intercept_for_msr(msr_bitmap
, MSR_IA32_SYSENTER_CS
, MSR_TYPE_RW
);
9749 vmx_disable_intercept_for_msr(msr_bitmap
, MSR_IA32_SYSENTER_ESP
, MSR_TYPE_RW
);
9750 vmx_disable_intercept_for_msr(msr_bitmap
, MSR_IA32_SYSENTER_EIP
, MSR_TYPE_RW
);
9751 vmx
->msr_bitmap_mode
= 0;
9753 vmx
->loaded_vmcs
= &vmx
->vmcs01
;
9755 vmx_vcpu_load(&vmx
->vcpu
, cpu
);
9756 vmx
->vcpu
.cpu
= cpu
;
9757 vmx_vcpu_setup(vmx
);
9758 vmx_vcpu_put(&vmx
->vcpu
);
9760 if (cpu_need_virtualize_apic_accesses(&vmx
->vcpu
)) {
9761 err
= alloc_apic_access_page(kvm
);
9767 err
= init_rmode_identity_map(kvm
);
9773 nested_vmx_setup_ctls_msrs(vmx
);
9774 vmx
->nested
.vpid02
= allocate_vpid();
9777 vmx
->nested
.posted_intr_nv
= -1;
9778 vmx
->nested
.current_vmptr
= -1ull;
9780 vmx
->msr_ia32_feature_control_valid_bits
= FEATURE_CONTROL_LOCKED
;
9783 * Enforce invariant: pi_desc.nv is always either POSTED_INTR_VECTOR
9784 * or POSTED_INTR_WAKEUP_VECTOR.
9786 vmx
->pi_desc
.nv
= POSTED_INTR_VECTOR
;
9787 vmx
->pi_desc
.sn
= 1;
9792 free_vpid(vmx
->nested
.vpid02
);
9793 free_loaded_vmcs(vmx
->loaded_vmcs
);
9795 kfree(vmx
->guest_msrs
);
9797 vmx_destroy_pml_buffer(vmx
);
9799 kvm_vcpu_uninit(&vmx
->vcpu
);
9801 free_vpid(vmx
->vpid
);
9802 kmem_cache_free(kvm_vcpu_cache
, vmx
);
9803 return ERR_PTR(err
);
9806 static void __init
vmx_check_processor_compat(void *rtn
)
9808 struct vmcs_config vmcs_conf
;
9811 if (setup_vmcs_config(&vmcs_conf
) < 0)
9813 if (memcmp(&vmcs_config
, &vmcs_conf
, sizeof(struct vmcs_config
)) != 0) {
9814 printk(KERN_ERR
"kvm: CPU %d feature inconsistency!\n",
9815 smp_processor_id());
9820 static u64
vmx_get_mt_mask(struct kvm_vcpu
*vcpu
, gfn_t gfn
, bool is_mmio
)
9825 /* For VT-d and EPT combination
9826 * 1. MMIO: always map as UC
9828 * a. VT-d without snooping control feature: can't guarantee the
9829 * result, try to trust guest.
9830 * b. VT-d with snooping control feature: snooping control feature of
9831 * VT-d engine can guarantee the cache correctness. Just set it
9832 * to WB to keep consistent with host. So the same as item 3.
9833 * 3. EPT without VT-d: always map as WB and set IPAT=1 to keep
9834 * consistent with host MTRR
9837 cache
= MTRR_TYPE_UNCACHABLE
;
9841 if (!kvm_arch_has_noncoherent_dma(vcpu
->kvm
)) {
9842 ipat
= VMX_EPT_IPAT_BIT
;
9843 cache
= MTRR_TYPE_WRBACK
;
9847 if (kvm_read_cr0(vcpu
) & X86_CR0_CD
) {
9848 ipat
= VMX_EPT_IPAT_BIT
;
9849 if (kvm_check_has_quirk(vcpu
->kvm
, KVM_X86_QUIRK_CD_NW_CLEARED
))
9850 cache
= MTRR_TYPE_WRBACK
;
9852 cache
= MTRR_TYPE_UNCACHABLE
;
9856 cache
= kvm_mtrr_get_guest_memory_type(vcpu
, gfn
);
9859 return (cache
<< VMX_EPT_MT_EPTE_SHIFT
) | ipat
;
9862 static int vmx_get_lpage_level(void)
9864 if (enable_ept
&& !cpu_has_vmx_ept_1g_page())
9865 return PT_DIRECTORY_LEVEL
;
9867 /* For shadow and EPT supported 1GB page */
9868 return PT_PDPE_LEVEL
;
9871 static void vmcs_set_secondary_exec_control(u32 new_ctl
)
9874 * These bits in the secondary execution controls field
9875 * are dynamic, the others are mostly based on the hypervisor
9876 * architecture and the guest's CPUID. Do not touch the
9880 SECONDARY_EXEC_SHADOW_VMCS
|
9881 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE
|
9882 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES
|
9883 SECONDARY_EXEC_DESC
;
9885 u32 cur_ctl
= vmcs_read32(SECONDARY_VM_EXEC_CONTROL
);
9887 vmcs_write32(SECONDARY_VM_EXEC_CONTROL
,
9888 (new_ctl
& ~mask
) | (cur_ctl
& mask
));
9892 * Generate MSR_IA32_VMX_CR{0,4}_FIXED1 according to CPUID. Only set bits
9893 * (indicating "allowed-1") if they are supported in the guest's CPUID.
9895 static void nested_vmx_cr_fixed1_bits_update(struct kvm_vcpu
*vcpu
)
9897 struct vcpu_vmx
*vmx
= to_vmx(vcpu
);
9898 struct kvm_cpuid_entry2
*entry
;
9900 vmx
->nested
.nested_vmx_cr0_fixed1
= 0xffffffff;
9901 vmx
->nested
.nested_vmx_cr4_fixed1
= X86_CR4_PCE
;
9903 #define cr4_fixed1_update(_cr4_mask, _reg, _cpuid_mask) do { \
9904 if (entry && (entry->_reg & (_cpuid_mask))) \
9905 vmx->nested.nested_vmx_cr4_fixed1 |= (_cr4_mask); \
9908 entry
= kvm_find_cpuid_entry(vcpu
, 0x1, 0);
9909 cr4_fixed1_update(X86_CR4_VME
, edx
, bit(X86_FEATURE_VME
));
9910 cr4_fixed1_update(X86_CR4_PVI
, edx
, bit(X86_FEATURE_VME
));
9911 cr4_fixed1_update(X86_CR4_TSD
, edx
, bit(X86_FEATURE_TSC
));
9912 cr4_fixed1_update(X86_CR4_DE
, edx
, bit(X86_FEATURE_DE
));
9913 cr4_fixed1_update(X86_CR4_PSE
, edx
, bit(X86_FEATURE_PSE
));
9914 cr4_fixed1_update(X86_CR4_PAE
, edx
, bit(X86_FEATURE_PAE
));
9915 cr4_fixed1_update(X86_CR4_MCE
, edx
, bit(X86_FEATURE_MCE
));
9916 cr4_fixed1_update(X86_CR4_PGE
, edx
, bit(X86_FEATURE_PGE
));
9917 cr4_fixed1_update(X86_CR4_OSFXSR
, edx
, bit(X86_FEATURE_FXSR
));
9918 cr4_fixed1_update(X86_CR4_OSXMMEXCPT
, edx
, bit(X86_FEATURE_XMM
));
9919 cr4_fixed1_update(X86_CR4_VMXE
, ecx
, bit(X86_FEATURE_VMX
));
9920 cr4_fixed1_update(X86_CR4_SMXE
, ecx
, bit(X86_FEATURE_SMX
));
9921 cr4_fixed1_update(X86_CR4_PCIDE
, ecx
, bit(X86_FEATURE_PCID
));
9922 cr4_fixed1_update(X86_CR4_OSXSAVE
, ecx
, bit(X86_FEATURE_XSAVE
));
9924 entry
= kvm_find_cpuid_entry(vcpu
, 0x7, 0);
9925 cr4_fixed1_update(X86_CR4_FSGSBASE
, ebx
, bit(X86_FEATURE_FSGSBASE
));
9926 cr4_fixed1_update(X86_CR4_SMEP
, ebx
, bit(X86_FEATURE_SMEP
));
9927 cr4_fixed1_update(X86_CR4_SMAP
, ebx
, bit(X86_FEATURE_SMAP
));
9928 cr4_fixed1_update(X86_CR4_PKE
, ecx
, bit(X86_FEATURE_PKU
));
9929 cr4_fixed1_update(X86_CR4_UMIP
, ecx
, bit(X86_FEATURE_UMIP
));
9931 #undef cr4_fixed1_update
9934 static void vmx_cpuid_update(struct kvm_vcpu
*vcpu
)
9936 struct vcpu_vmx
*vmx
= to_vmx(vcpu
);
9938 if (cpu_has_secondary_exec_ctrls()) {
9939 vmx_compute_secondary_exec_control(vmx
);
9940 vmcs_set_secondary_exec_control(vmx
->secondary_exec_control
);
9943 if (nested_vmx_allowed(vcpu
))
9944 to_vmx(vcpu
)->msr_ia32_feature_control_valid_bits
|=
9945 FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX
;
9947 to_vmx(vcpu
)->msr_ia32_feature_control_valid_bits
&=
9948 ~FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX
;
9950 if (nested_vmx_allowed(vcpu
))
9951 nested_vmx_cr_fixed1_bits_update(vcpu
);
9954 static void vmx_set_supported_cpuid(u32 func
, struct kvm_cpuid_entry2
*entry
)
9956 if (func
== 1 && nested
)
9957 entry
->ecx
|= bit(X86_FEATURE_VMX
);
9960 static void nested_ept_inject_page_fault(struct kvm_vcpu
*vcpu
,
9961 struct x86_exception
*fault
)
9963 struct vmcs12
*vmcs12
= get_vmcs12(vcpu
);
9964 struct vcpu_vmx
*vmx
= to_vmx(vcpu
);
9966 unsigned long exit_qualification
= vcpu
->arch
.exit_qualification
;
9968 if (vmx
->nested
.pml_full
) {
9969 exit_reason
= EXIT_REASON_PML_FULL
;
9970 vmx
->nested
.pml_full
= false;
9971 exit_qualification
&= INTR_INFO_UNBLOCK_NMI
;
9972 } else if (fault
->error_code
& PFERR_RSVD_MASK
)
9973 exit_reason
= EXIT_REASON_EPT_MISCONFIG
;
9975 exit_reason
= EXIT_REASON_EPT_VIOLATION
;
9977 nested_vmx_vmexit(vcpu
, exit_reason
, 0, exit_qualification
);
9978 vmcs12
->guest_physical_address
= fault
->address
;
9981 static bool nested_ept_ad_enabled(struct kvm_vcpu
*vcpu
)
9983 return nested_ept_get_cr3(vcpu
) & VMX_EPTP_AD_ENABLE_BIT
;
9986 /* Callbacks for nested_ept_init_mmu_context: */
9988 static unsigned long nested_ept_get_cr3(struct kvm_vcpu
*vcpu
)
9990 /* return the page table to be shadowed - in our case, EPT12 */
9991 return get_vmcs12(vcpu
)->ept_pointer
;
9994 static int nested_ept_init_mmu_context(struct kvm_vcpu
*vcpu
)
9996 WARN_ON(mmu_is_nested(vcpu
));
9997 if (!valid_ept_address(vcpu
, nested_ept_get_cr3(vcpu
)))
10000 kvm_mmu_unload(vcpu
);
10001 kvm_init_shadow_ept_mmu(vcpu
,
10002 to_vmx(vcpu
)->nested
.nested_vmx_ept_caps
&
10003 VMX_EPT_EXECUTE_ONLY_BIT
,
10004 nested_ept_ad_enabled(vcpu
));
10005 vcpu
->arch
.mmu
.set_cr3
= vmx_set_cr3
;
10006 vcpu
->arch
.mmu
.get_cr3
= nested_ept_get_cr3
;
10007 vcpu
->arch
.mmu
.inject_page_fault
= nested_ept_inject_page_fault
;
10009 vcpu
->arch
.walk_mmu
= &vcpu
->arch
.nested_mmu
;
10013 static void nested_ept_uninit_mmu_context(struct kvm_vcpu
*vcpu
)
10015 vcpu
->arch
.walk_mmu
= &vcpu
->arch
.mmu
;
10018 static bool nested_vmx_is_page_fault_vmexit(struct vmcs12
*vmcs12
,
10021 bool inequality
, bit
;
10023 bit
= (vmcs12
->exception_bitmap
& (1u << PF_VECTOR
)) != 0;
10025 (error_code
& vmcs12
->page_fault_error_code_mask
) !=
10026 vmcs12
->page_fault_error_code_match
;
10027 return inequality
^ bit
;
10030 static void vmx_inject_page_fault_nested(struct kvm_vcpu
*vcpu
,
10031 struct x86_exception
*fault
)
10033 struct vmcs12
*vmcs12
= get_vmcs12(vcpu
);
10035 WARN_ON(!is_guest_mode(vcpu
));
10037 if (nested_vmx_is_page_fault_vmexit(vmcs12
, fault
->error_code
) &&
10038 !to_vmx(vcpu
)->nested
.nested_run_pending
) {
10039 vmcs12
->vm_exit_intr_error_code
= fault
->error_code
;
10040 nested_vmx_vmexit(vcpu
, EXIT_REASON_EXCEPTION_NMI
,
10041 PF_VECTOR
| INTR_TYPE_HARD_EXCEPTION
|
10042 INTR_INFO_DELIVER_CODE_MASK
| INTR_INFO_VALID_MASK
,
10045 kvm_inject_page_fault(vcpu
, fault
);
10049 static inline bool nested_vmx_prepare_msr_bitmap(struct kvm_vcpu
*vcpu
,
10050 struct vmcs12
*vmcs12
);
10052 static void nested_get_vmcs12_pages(struct kvm_vcpu
*vcpu
,
10053 struct vmcs12
*vmcs12
)
10055 struct vcpu_vmx
*vmx
= to_vmx(vcpu
);
10059 if (nested_cpu_has2(vmcs12
, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES
)) {
10061 * Translate L1 physical address to host physical
10062 * address for vmcs02. Keep the page pinned, so this
10063 * physical address remains valid. We keep a reference
10064 * to it so we can release it later.
10066 if (vmx
->nested
.apic_access_page
) { /* shouldn't happen */
10067 kvm_release_page_dirty(vmx
->nested
.apic_access_page
);
10068 vmx
->nested
.apic_access_page
= NULL
;
10070 page
= kvm_vcpu_gpa_to_page(vcpu
, vmcs12
->apic_access_addr
);
10072 * If translation failed, no matter: This feature asks
10073 * to exit when accessing the given address, and if it
10074 * can never be accessed, this feature won't do
10077 if (!is_error_page(page
)) {
10078 vmx
->nested
.apic_access_page
= page
;
10079 hpa
= page_to_phys(vmx
->nested
.apic_access_page
);
10080 vmcs_write64(APIC_ACCESS_ADDR
, hpa
);
10082 vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL
,
10083 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES
);
10085 } else if (!(nested_cpu_has_virt_x2apic_mode(vmcs12
)) &&
10086 cpu_need_virtualize_apic_accesses(&vmx
->vcpu
)) {
10087 vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL
,
10088 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES
);
10089 kvm_vcpu_reload_apic_access_page(vcpu
);
10092 if (nested_cpu_has(vmcs12
, CPU_BASED_TPR_SHADOW
)) {
10093 if (vmx
->nested
.virtual_apic_page
) { /* shouldn't happen */
10094 kvm_release_page_dirty(vmx
->nested
.virtual_apic_page
);
10095 vmx
->nested
.virtual_apic_page
= NULL
;
10097 page
= kvm_vcpu_gpa_to_page(vcpu
, vmcs12
->virtual_apic_page_addr
);
10100 * If translation failed, VM entry will fail because
10101 * prepare_vmcs02 set VIRTUAL_APIC_PAGE_ADDR to -1ull.
10102 * Failing the vm entry is _not_ what the processor
10103 * does but it's basically the only possibility we
10104 * have. We could still enter the guest if CR8 load
10105 * exits are enabled, CR8 store exits are enabled, and
10106 * virtualize APIC access is disabled; in this case
10107 * the processor would never use the TPR shadow and we
10108 * could simply clear the bit from the execution
10109 * control. But such a configuration is useless, so
10110 * let's keep the code simple.
10112 if (!is_error_page(page
)) {
10113 vmx
->nested
.virtual_apic_page
= page
;
10114 hpa
= page_to_phys(vmx
->nested
.virtual_apic_page
);
10115 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR
, hpa
);
10119 if (nested_cpu_has_posted_intr(vmcs12
)) {
10120 if (vmx
->nested
.pi_desc_page
) { /* shouldn't happen */
10121 kunmap(vmx
->nested
.pi_desc_page
);
10122 kvm_release_page_dirty(vmx
->nested
.pi_desc_page
);
10123 vmx
->nested
.pi_desc_page
= NULL
;
10125 page
= kvm_vcpu_gpa_to_page(vcpu
, vmcs12
->posted_intr_desc_addr
);
10126 if (is_error_page(page
))
10128 vmx
->nested
.pi_desc_page
= page
;
10129 vmx
->nested
.pi_desc
= kmap(vmx
->nested
.pi_desc_page
);
10130 vmx
->nested
.pi_desc
=
10131 (struct pi_desc
*)((void *)vmx
->nested
.pi_desc
+
10132 (unsigned long)(vmcs12
->posted_intr_desc_addr
&
10134 vmcs_write64(POSTED_INTR_DESC_ADDR
,
10135 page_to_phys(vmx
->nested
.pi_desc_page
) +
10136 (unsigned long)(vmcs12
->posted_intr_desc_addr
&
10139 if (nested_vmx_prepare_msr_bitmap(vcpu
, vmcs12
))
10140 vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL
,
10141 CPU_BASED_USE_MSR_BITMAPS
);
10143 vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL
,
10144 CPU_BASED_USE_MSR_BITMAPS
);
10147 static void vmx_start_preemption_timer(struct kvm_vcpu
*vcpu
)
10149 u64 preemption_timeout
= get_vmcs12(vcpu
)->vmx_preemption_timer_value
;
10150 struct vcpu_vmx
*vmx
= to_vmx(vcpu
);
10152 if (vcpu
->arch
.virtual_tsc_khz
== 0)
10155 /* Make sure short timeouts reliably trigger an immediate vmexit.
10156 * hrtimer_start does not guarantee this. */
10157 if (preemption_timeout
<= 1) {
10158 vmx_preemption_timer_fn(&vmx
->nested
.preemption_timer
);
10162 preemption_timeout
<<= VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE
;
10163 preemption_timeout
*= 1000000;
10164 do_div(preemption_timeout
, vcpu
->arch
.virtual_tsc_khz
);
10165 hrtimer_start(&vmx
->nested
.preemption_timer
,
10166 ns_to_ktime(preemption_timeout
), HRTIMER_MODE_REL
);
10169 static int nested_vmx_check_io_bitmap_controls(struct kvm_vcpu
*vcpu
,
10170 struct vmcs12
*vmcs12
)
10172 if (!nested_cpu_has(vmcs12
, CPU_BASED_USE_IO_BITMAPS
))
10175 if (!page_address_valid(vcpu
, vmcs12
->io_bitmap_a
) ||
10176 !page_address_valid(vcpu
, vmcs12
->io_bitmap_b
))
10182 static int nested_vmx_check_msr_bitmap_controls(struct kvm_vcpu
*vcpu
,
10183 struct vmcs12
*vmcs12
)
10185 if (!nested_cpu_has(vmcs12
, CPU_BASED_USE_MSR_BITMAPS
))
10188 if (!page_address_valid(vcpu
, vmcs12
->msr_bitmap
))
10194 static int nested_vmx_check_tpr_shadow_controls(struct kvm_vcpu
*vcpu
,
10195 struct vmcs12
*vmcs12
)
10197 if (!nested_cpu_has(vmcs12
, CPU_BASED_TPR_SHADOW
))
10200 if (!page_address_valid(vcpu
, vmcs12
->virtual_apic_page_addr
))
10207 * Merge L0's and L1's MSR bitmap, return false to indicate that
10208 * we do not use the hardware.
10210 static inline bool nested_vmx_prepare_msr_bitmap(struct kvm_vcpu
*vcpu
,
10211 struct vmcs12
*vmcs12
)
10215 unsigned long *msr_bitmap_l1
;
10216 unsigned long *msr_bitmap_l0
= to_vmx(vcpu
)->nested
.vmcs02
.msr_bitmap
;
10218 * pred_cmd & spec_ctrl are trying to verify two things:
10220 * 1. L0 gave a permission to L1 to actually passthrough the MSR. This
10221 * ensures that we do not accidentally generate an L02 MSR bitmap
10222 * from the L12 MSR bitmap that is too permissive.
10223 * 2. That L1 or L2s have actually used the MSR. This avoids
10224 * unnecessarily merging of the bitmap if the MSR is unused. This
10225 * works properly because we only update the L01 MSR bitmap lazily.
10226 * So even if L0 should pass L1 these MSRs, the L01 bitmap is only
10227 * updated to reflect this when L1 (or its L2s) actually write to
10230 bool pred_cmd
= !msr_write_intercepted_l01(vcpu
, MSR_IA32_PRED_CMD
);
10231 bool spec_ctrl
= !msr_write_intercepted_l01(vcpu
, MSR_IA32_SPEC_CTRL
);
10233 /* Nothing to do if the MSR bitmap is not in use. */
10234 if (!cpu_has_vmx_msr_bitmap() ||
10235 !nested_cpu_has(vmcs12
, CPU_BASED_USE_MSR_BITMAPS
))
10238 if (!nested_cpu_has_virt_x2apic_mode(vmcs12
) &&
10239 !pred_cmd
&& !spec_ctrl
)
10242 page
= kvm_vcpu_gpa_to_page(vcpu
, vmcs12
->msr_bitmap
);
10243 if (is_error_page(page
))
10246 msr_bitmap_l1
= (unsigned long *)kmap(page
);
10247 if (nested_cpu_has_apic_reg_virt(vmcs12
)) {
10249 * L0 need not intercept reads for MSRs between 0x800 and 0x8ff, it
10250 * just lets the processor take the value from the virtual-APIC page;
10251 * take those 256 bits directly from the L1 bitmap.
10253 for (msr
= 0x800; msr
<= 0x8ff; msr
+= BITS_PER_LONG
) {
10254 unsigned word
= msr
/ BITS_PER_LONG
;
10255 msr_bitmap_l0
[word
] = msr_bitmap_l1
[word
];
10256 msr_bitmap_l0
[word
+ (0x800 / sizeof(long))] = ~0;
10259 for (msr
= 0x800; msr
<= 0x8ff; msr
+= BITS_PER_LONG
) {
10260 unsigned word
= msr
/ BITS_PER_LONG
;
10261 msr_bitmap_l0
[word
] = ~0;
10262 msr_bitmap_l0
[word
+ (0x800 / sizeof(long))] = ~0;
10266 nested_vmx_disable_intercept_for_msr(
10267 msr_bitmap_l1
, msr_bitmap_l0
,
10268 X2APIC_MSR(APIC_TASKPRI
),
10271 if (nested_cpu_has_vid(vmcs12
)) {
10272 nested_vmx_disable_intercept_for_msr(
10273 msr_bitmap_l1
, msr_bitmap_l0
,
10274 X2APIC_MSR(APIC_EOI
),
10276 nested_vmx_disable_intercept_for_msr(
10277 msr_bitmap_l1
, msr_bitmap_l0
,
10278 X2APIC_MSR(APIC_SELF_IPI
),
10283 nested_vmx_disable_intercept_for_msr(
10284 msr_bitmap_l1
, msr_bitmap_l0
,
10285 MSR_IA32_SPEC_CTRL
,
10286 MSR_TYPE_R
| MSR_TYPE_W
);
10289 nested_vmx_disable_intercept_for_msr(
10290 msr_bitmap_l1
, msr_bitmap_l0
,
10295 kvm_release_page_clean(page
);
10300 static int nested_vmx_check_apicv_controls(struct kvm_vcpu
*vcpu
,
10301 struct vmcs12
*vmcs12
)
10303 if (!nested_cpu_has_virt_x2apic_mode(vmcs12
) &&
10304 !nested_cpu_has_apic_reg_virt(vmcs12
) &&
10305 !nested_cpu_has_vid(vmcs12
) &&
10306 !nested_cpu_has_posted_intr(vmcs12
))
10310 * If virtualize x2apic mode is enabled,
10311 * virtualize apic access must be disabled.
10313 if (nested_cpu_has_virt_x2apic_mode(vmcs12
) &&
10314 nested_cpu_has2(vmcs12
, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES
))
10318 * If virtual interrupt delivery is enabled,
10319 * we must exit on external interrupts.
10321 if (nested_cpu_has_vid(vmcs12
) &&
10322 !nested_exit_on_intr(vcpu
))
10326 * bits 15:8 should be zero in posted_intr_nv,
10327 * the descriptor address has been already checked
10328 * in nested_get_vmcs12_pages.
10330 if (nested_cpu_has_posted_intr(vmcs12
) &&
10331 (!nested_cpu_has_vid(vmcs12
) ||
10332 !nested_exit_intr_ack_set(vcpu
) ||
10333 vmcs12
->posted_intr_nv
& 0xff00))
10336 /* tpr shadow is needed by all apicv features. */
10337 if (!nested_cpu_has(vmcs12
, CPU_BASED_TPR_SHADOW
))
10343 static int nested_vmx_check_msr_switch(struct kvm_vcpu
*vcpu
,
10344 unsigned long count_field
,
10345 unsigned long addr_field
)
10350 if (vmcs12_read_any(vcpu
, count_field
, &count
) ||
10351 vmcs12_read_any(vcpu
, addr_field
, &addr
)) {
10357 maxphyaddr
= cpuid_maxphyaddr(vcpu
);
10358 if (!IS_ALIGNED(addr
, 16) || addr
>> maxphyaddr
||
10359 (addr
+ count
* sizeof(struct vmx_msr_entry
) - 1) >> maxphyaddr
) {
10360 pr_debug_ratelimited(
10361 "nVMX: invalid MSR switch (0x%lx, %d, %llu, 0x%08llx)",
10362 addr_field
, maxphyaddr
, count
, addr
);
10368 static int nested_vmx_check_msr_switch_controls(struct kvm_vcpu
*vcpu
,
10369 struct vmcs12
*vmcs12
)
10371 if (vmcs12
->vm_exit_msr_load_count
== 0 &&
10372 vmcs12
->vm_exit_msr_store_count
== 0 &&
10373 vmcs12
->vm_entry_msr_load_count
== 0)
10374 return 0; /* Fast path */
10375 if (nested_vmx_check_msr_switch(vcpu
, VM_EXIT_MSR_LOAD_COUNT
,
10376 VM_EXIT_MSR_LOAD_ADDR
) ||
10377 nested_vmx_check_msr_switch(vcpu
, VM_EXIT_MSR_STORE_COUNT
,
10378 VM_EXIT_MSR_STORE_ADDR
) ||
10379 nested_vmx_check_msr_switch(vcpu
, VM_ENTRY_MSR_LOAD_COUNT
,
10380 VM_ENTRY_MSR_LOAD_ADDR
))
10385 static int nested_vmx_check_pml_controls(struct kvm_vcpu
*vcpu
,
10386 struct vmcs12
*vmcs12
)
10388 u64 address
= vmcs12
->pml_address
;
10389 int maxphyaddr
= cpuid_maxphyaddr(vcpu
);
10391 if (nested_cpu_has2(vmcs12
, SECONDARY_EXEC_ENABLE_PML
)) {
10392 if (!nested_cpu_has_ept(vmcs12
) ||
10393 !IS_ALIGNED(address
, 4096) ||
10394 address
>> maxphyaddr
)
10401 static int nested_vmx_msr_check_common(struct kvm_vcpu
*vcpu
,
10402 struct vmx_msr_entry
*e
)
10404 /* x2APIC MSR accesses are not allowed */
10405 if (vcpu
->arch
.apic_base
& X2APIC_ENABLE
&& e
->index
>> 8 == 0x8)
10407 if (e
->index
== MSR_IA32_UCODE_WRITE
|| /* SDM Table 35-2 */
10408 e
->index
== MSR_IA32_UCODE_REV
)
10410 if (e
->reserved
!= 0)
10415 static int nested_vmx_load_msr_check(struct kvm_vcpu
*vcpu
,
10416 struct vmx_msr_entry
*e
)
10418 if (e
->index
== MSR_FS_BASE
||
10419 e
->index
== MSR_GS_BASE
||
10420 e
->index
== MSR_IA32_SMM_MONITOR_CTL
|| /* SMM is not supported */
10421 nested_vmx_msr_check_common(vcpu
, e
))
10426 static int nested_vmx_store_msr_check(struct kvm_vcpu
*vcpu
,
10427 struct vmx_msr_entry
*e
)
10429 if (e
->index
== MSR_IA32_SMBASE
|| /* SMM is not supported */
10430 nested_vmx_msr_check_common(vcpu
, e
))
10436 * Load guest's/host's msr at nested entry/exit.
10437 * return 0 for success, entry index for failure.
10439 static u32
nested_vmx_load_msr(struct kvm_vcpu
*vcpu
, u64 gpa
, u32 count
)
10442 struct vmx_msr_entry e
;
10443 struct msr_data msr
;
10445 msr
.host_initiated
= false;
10446 for (i
= 0; i
< count
; i
++) {
10447 if (kvm_vcpu_read_guest(vcpu
, gpa
+ i
* sizeof(e
),
10449 pr_debug_ratelimited(
10450 "%s cannot read MSR entry (%u, 0x%08llx)\n",
10451 __func__
, i
, gpa
+ i
* sizeof(e
));
10454 if (nested_vmx_load_msr_check(vcpu
, &e
)) {
10455 pr_debug_ratelimited(
10456 "%s check failed (%u, 0x%x, 0x%x)\n",
10457 __func__
, i
, e
.index
, e
.reserved
);
10460 msr
.index
= e
.index
;
10461 msr
.data
= e
.value
;
10462 if (kvm_set_msr(vcpu
, &msr
)) {
10463 pr_debug_ratelimited(
10464 "%s cannot write MSR (%u, 0x%x, 0x%llx)\n",
10465 __func__
, i
, e
.index
, e
.value
);
10474 static int nested_vmx_store_msr(struct kvm_vcpu
*vcpu
, u64 gpa
, u32 count
)
10477 struct vmx_msr_entry e
;
10479 for (i
= 0; i
< count
; i
++) {
10480 struct msr_data msr_info
;
10481 if (kvm_vcpu_read_guest(vcpu
,
10482 gpa
+ i
* sizeof(e
),
10483 &e
, 2 * sizeof(u32
))) {
10484 pr_debug_ratelimited(
10485 "%s cannot read MSR entry (%u, 0x%08llx)\n",
10486 __func__
, i
, gpa
+ i
* sizeof(e
));
10489 if (nested_vmx_store_msr_check(vcpu
, &e
)) {
10490 pr_debug_ratelimited(
10491 "%s check failed (%u, 0x%x, 0x%x)\n",
10492 __func__
, i
, e
.index
, e
.reserved
);
10495 msr_info
.host_initiated
= false;
10496 msr_info
.index
= e
.index
;
10497 if (kvm_get_msr(vcpu
, &msr_info
)) {
10498 pr_debug_ratelimited(
10499 "%s cannot read MSR (%u, 0x%x)\n",
10500 __func__
, i
, e
.index
);
10503 if (kvm_vcpu_write_guest(vcpu
,
10504 gpa
+ i
* sizeof(e
) +
10505 offsetof(struct vmx_msr_entry
, value
),
10506 &msr_info
.data
, sizeof(msr_info
.data
))) {
10507 pr_debug_ratelimited(
10508 "%s cannot write MSR (%u, 0x%x, 0x%llx)\n",
10509 __func__
, i
, e
.index
, msr_info
.data
);
10516 static bool nested_cr3_valid(struct kvm_vcpu
*vcpu
, unsigned long val
)
10518 unsigned long invalid_mask
;
10520 invalid_mask
= (~0ULL) << cpuid_maxphyaddr(vcpu
);
10521 return (val
& invalid_mask
) == 0;
10525 * Load guest's/host's cr3 at nested entry/exit. nested_ept is true if we are
10526 * emulating VM entry into a guest with EPT enabled.
10527 * Returns 0 on success, 1 on failure. Invalid state exit qualification code
10528 * is assigned to entry_failure_code on failure.
10530 static int nested_vmx_load_cr3(struct kvm_vcpu
*vcpu
, unsigned long cr3
, bool nested_ept
,
10531 u32
*entry_failure_code
)
10533 if (cr3
!= kvm_read_cr3(vcpu
) || (!nested_ept
&& pdptrs_changed(vcpu
))) {
10534 if (!nested_cr3_valid(vcpu
, cr3
)) {
10535 *entry_failure_code
= ENTRY_FAIL_DEFAULT
;
10540 * If PAE paging and EPT are both on, CR3 is not used by the CPU and
10541 * must not be dereferenced.
10543 if (!is_long_mode(vcpu
) && is_pae(vcpu
) && is_paging(vcpu
) &&
10545 if (!load_pdptrs(vcpu
, vcpu
->arch
.walk_mmu
, cr3
)) {
10546 *entry_failure_code
= ENTRY_FAIL_PDPTE
;
10551 vcpu
->arch
.cr3
= cr3
;
10552 __set_bit(VCPU_EXREG_CR3
, (ulong
*)&vcpu
->arch
.regs_avail
);
10555 kvm_mmu_reset_context(vcpu
);
10559 static void prepare_vmcs02_full(struct kvm_vcpu
*vcpu
, struct vmcs12
*vmcs12
,
10562 struct vcpu_vmx
*vmx
= to_vmx(vcpu
);
10564 vmcs_write16(GUEST_ES_SELECTOR
, vmcs12
->guest_es_selector
);
10565 vmcs_write16(GUEST_SS_SELECTOR
, vmcs12
->guest_ss_selector
);
10566 vmcs_write16(GUEST_DS_SELECTOR
, vmcs12
->guest_ds_selector
);
10567 vmcs_write16(GUEST_FS_SELECTOR
, vmcs12
->guest_fs_selector
);
10568 vmcs_write16(GUEST_GS_SELECTOR
, vmcs12
->guest_gs_selector
);
10569 vmcs_write16(GUEST_LDTR_SELECTOR
, vmcs12
->guest_ldtr_selector
);
10570 vmcs_write16(GUEST_TR_SELECTOR
, vmcs12
->guest_tr_selector
);
10571 vmcs_write32(GUEST_ES_LIMIT
, vmcs12
->guest_es_limit
);
10572 vmcs_write32(GUEST_SS_LIMIT
, vmcs12
->guest_ss_limit
);
10573 vmcs_write32(GUEST_DS_LIMIT
, vmcs12
->guest_ds_limit
);
10574 vmcs_write32(GUEST_FS_LIMIT
, vmcs12
->guest_fs_limit
);
10575 vmcs_write32(GUEST_GS_LIMIT
, vmcs12
->guest_gs_limit
);
10576 vmcs_write32(GUEST_LDTR_LIMIT
, vmcs12
->guest_ldtr_limit
);
10577 vmcs_write32(GUEST_TR_LIMIT
, vmcs12
->guest_tr_limit
);
10578 vmcs_write32(GUEST_GDTR_LIMIT
, vmcs12
->guest_gdtr_limit
);
10579 vmcs_write32(GUEST_IDTR_LIMIT
, vmcs12
->guest_idtr_limit
);
10580 vmcs_write32(GUEST_ES_AR_BYTES
, vmcs12
->guest_es_ar_bytes
);
10581 vmcs_write32(GUEST_SS_AR_BYTES
, vmcs12
->guest_ss_ar_bytes
);
10582 vmcs_write32(GUEST_DS_AR_BYTES
, vmcs12
->guest_ds_ar_bytes
);
10583 vmcs_write32(GUEST_FS_AR_BYTES
, vmcs12
->guest_fs_ar_bytes
);
10584 vmcs_write32(GUEST_GS_AR_BYTES
, vmcs12
->guest_gs_ar_bytes
);
10585 vmcs_write32(GUEST_LDTR_AR_BYTES
, vmcs12
->guest_ldtr_ar_bytes
);
10586 vmcs_write32(GUEST_TR_AR_BYTES
, vmcs12
->guest_tr_ar_bytes
);
10587 vmcs_writel(GUEST_SS_BASE
, vmcs12
->guest_ss_base
);
10588 vmcs_writel(GUEST_DS_BASE
, vmcs12
->guest_ds_base
);
10589 vmcs_writel(GUEST_FS_BASE
, vmcs12
->guest_fs_base
);
10590 vmcs_writel(GUEST_GS_BASE
, vmcs12
->guest_gs_base
);
10591 vmcs_writel(GUEST_LDTR_BASE
, vmcs12
->guest_ldtr_base
);
10592 vmcs_writel(GUEST_TR_BASE
, vmcs12
->guest_tr_base
);
10593 vmcs_writel(GUEST_GDTR_BASE
, vmcs12
->guest_gdtr_base
);
10594 vmcs_writel(GUEST_IDTR_BASE
, vmcs12
->guest_idtr_base
);
10596 vmcs_write32(GUEST_SYSENTER_CS
, vmcs12
->guest_sysenter_cs
);
10597 vmcs_writel(GUEST_PENDING_DBG_EXCEPTIONS
,
10598 vmcs12
->guest_pending_dbg_exceptions
);
10599 vmcs_writel(GUEST_SYSENTER_ESP
, vmcs12
->guest_sysenter_esp
);
10600 vmcs_writel(GUEST_SYSENTER_EIP
, vmcs12
->guest_sysenter_eip
);
10602 if (nested_cpu_has_xsaves(vmcs12
))
10603 vmcs_write64(XSS_EXIT_BITMAP
, vmcs12
->xss_exit_bitmap
);
10604 vmcs_write64(VMCS_LINK_POINTER
, -1ull);
10606 if (cpu_has_vmx_posted_intr())
10607 vmcs_write16(POSTED_INTR_NV
, POSTED_INTR_NESTED_VECTOR
);
10610 * Whether page-faults are trapped is determined by a combination of
10611 * 3 settings: PFEC_MASK, PFEC_MATCH and EXCEPTION_BITMAP.PF.
10612 * If enable_ept, L0 doesn't care about page faults and we should
10613 * set all of these to L1's desires. However, if !enable_ept, L0 does
10614 * care about (at least some) page faults, and because it is not easy
10615 * (if at all possible?) to merge L0 and L1's desires, we simply ask
10616 * to exit on each and every L2 page fault. This is done by setting
10617 * MASK=MATCH=0 and (see below) EB.PF=1.
10618 * Note that below we don't need special code to set EB.PF beyond the
10619 * "or"ing of the EB of vmcs01 and vmcs12, because when enable_ept,
10620 * vmcs01's EB.PF is 0 so the "or" will take vmcs12's value, and when
10621 * !enable_ept, EB.PF is 1, so the "or" will always be 1.
10623 vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK
,
10624 enable_ept
? vmcs12
->page_fault_error_code_mask
: 0);
10625 vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH
,
10626 enable_ept
? vmcs12
->page_fault_error_code_match
: 0);
10628 /* All VMFUNCs are currently emulated through L0 vmexits. */
10629 if (cpu_has_vmx_vmfunc())
10630 vmcs_write64(VM_FUNCTION_CONTROL
, 0);
10632 if (cpu_has_vmx_apicv()) {
10633 vmcs_write64(EOI_EXIT_BITMAP0
, vmcs12
->eoi_exit_bitmap0
);
10634 vmcs_write64(EOI_EXIT_BITMAP1
, vmcs12
->eoi_exit_bitmap1
);
10635 vmcs_write64(EOI_EXIT_BITMAP2
, vmcs12
->eoi_exit_bitmap2
);
10636 vmcs_write64(EOI_EXIT_BITMAP3
, vmcs12
->eoi_exit_bitmap3
);
10640 * Set host-state according to L0's settings (vmcs12 is irrelevant here)
10641 * Some constant fields are set here by vmx_set_constant_host_state().
10642 * Other fields are different per CPU, and will be set later when
10643 * vmx_vcpu_load() is called, and when vmx_save_host_state() is called.
10645 vmx_set_constant_host_state(vmx
);
10648 * Set the MSR load/store lists to match L0's settings.
10650 vmcs_write32(VM_EXIT_MSR_STORE_COUNT
, 0);
10651 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT
, vmx
->msr_autoload
.nr
);
10652 vmcs_write64(VM_EXIT_MSR_LOAD_ADDR
, __pa(vmx
->msr_autoload
.host
));
10653 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT
, vmx
->msr_autoload
.nr
);
10654 vmcs_write64(VM_ENTRY_MSR_LOAD_ADDR
, __pa(vmx
->msr_autoload
.guest
));
10656 set_cr4_guest_host_mask(vmx
);
10658 if (vmx_mpx_supported())
10659 vmcs_write64(GUEST_BNDCFGS
, vmcs12
->guest_bndcfgs
);
10662 if (nested_cpu_has_vpid(vmcs12
) && vmx
->nested
.vpid02
)
10663 vmcs_write16(VIRTUAL_PROCESSOR_ID
, vmx
->nested
.vpid02
);
10665 vmcs_write16(VIRTUAL_PROCESSOR_ID
, vmx
->vpid
);
10669 * L1 may access the L2's PDPTR, so save them to construct vmcs12
10672 vmcs_write64(GUEST_PDPTR0
, vmcs12
->guest_pdptr0
);
10673 vmcs_write64(GUEST_PDPTR1
, vmcs12
->guest_pdptr1
);
10674 vmcs_write64(GUEST_PDPTR2
, vmcs12
->guest_pdptr2
);
10675 vmcs_write64(GUEST_PDPTR3
, vmcs12
->guest_pdptr3
);
10678 if (cpu_has_vmx_msr_bitmap())
10679 vmcs_write64(MSR_BITMAP
, __pa(vmx
->nested
.vmcs02
.msr_bitmap
));
10683 * prepare_vmcs02 is called when the L1 guest hypervisor runs its nested
10684 * L2 guest. L1 has a vmcs for L2 (vmcs12), and this function "merges" it
10685 * with L0's requirements for its guest (a.k.a. vmcs01), so we can run the L2
10686 * guest in a way that will both be appropriate to L1's requests, and our
10687 * needs. In addition to modifying the active vmcs (which is vmcs02), this
10688 * function also has additional necessary side-effects, like setting various
10689 * vcpu->arch fields.
10690 * Returns 0 on success, 1 on failure. Invalid state exit qualification code
10691 * is assigned to entry_failure_code on failure.
10693 static int prepare_vmcs02(struct kvm_vcpu
*vcpu
, struct vmcs12
*vmcs12
,
10694 bool from_vmentry
, u32
*entry_failure_code
)
10696 struct vcpu_vmx
*vmx
= to_vmx(vcpu
);
10697 u32 exec_control
, vmcs12_exec_ctrl
;
10700 * First, the fields that are shadowed. This must be kept in sync
10701 * with vmx_shadow_fields.h.
10704 vmcs_write16(GUEST_CS_SELECTOR
, vmcs12
->guest_cs_selector
);
10705 vmcs_write32(GUEST_CS_LIMIT
, vmcs12
->guest_cs_limit
);
10706 vmcs_write32(GUEST_CS_AR_BYTES
, vmcs12
->guest_cs_ar_bytes
);
10707 vmcs_writel(GUEST_ES_BASE
, vmcs12
->guest_es_base
);
10708 vmcs_writel(GUEST_CS_BASE
, vmcs12
->guest_cs_base
);
10711 * Not in vmcs02: GUEST_PML_INDEX, HOST_FS_SELECTOR, HOST_GS_SELECTOR,
10712 * HOST_FS_BASE, HOST_GS_BASE.
10715 if (from_vmentry
&&
10716 (vmcs12
->vm_entry_controls
& VM_ENTRY_LOAD_DEBUG_CONTROLS
)) {
10717 kvm_set_dr(vcpu
, 7, vmcs12
->guest_dr7
);
10718 vmcs_write64(GUEST_IA32_DEBUGCTL
, vmcs12
->guest_ia32_debugctl
);
10720 kvm_set_dr(vcpu
, 7, vcpu
->arch
.dr7
);
10721 vmcs_write64(GUEST_IA32_DEBUGCTL
, vmx
->nested
.vmcs01_debugctl
);
10723 if (from_vmentry
) {
10724 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD
,
10725 vmcs12
->vm_entry_intr_info_field
);
10726 vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE
,
10727 vmcs12
->vm_entry_exception_error_code
);
10728 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN
,
10729 vmcs12
->vm_entry_instruction_len
);
10730 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO
,
10731 vmcs12
->guest_interruptibility_info
);
10732 vmx
->loaded_vmcs
->nmi_known_unmasked
=
10733 !(vmcs12
->guest_interruptibility_info
& GUEST_INTR_STATE_NMI
);
10735 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD
, 0);
10737 vmx_set_rflags(vcpu
, vmcs12
->guest_rflags
);
10739 exec_control
= vmcs12
->pin_based_vm_exec_control
;
10741 /* Preemption timer setting is only taken from vmcs01. */
10742 exec_control
&= ~PIN_BASED_VMX_PREEMPTION_TIMER
;
10743 exec_control
|= vmcs_config
.pin_based_exec_ctrl
;
10744 if (vmx
->hv_deadline_tsc
== -1)
10745 exec_control
&= ~PIN_BASED_VMX_PREEMPTION_TIMER
;
10747 /* Posted interrupts setting is only taken from vmcs12. */
10748 if (nested_cpu_has_posted_intr(vmcs12
)) {
10749 vmx
->nested
.posted_intr_nv
= vmcs12
->posted_intr_nv
;
10750 vmx
->nested
.pi_pending
= false;
10752 exec_control
&= ~PIN_BASED_POSTED_INTR
;
10755 vmcs_write32(PIN_BASED_VM_EXEC_CONTROL
, exec_control
);
10757 vmx
->nested
.preemption_timer_expired
= false;
10758 if (nested_cpu_has_preemption_timer(vmcs12
))
10759 vmx_start_preemption_timer(vcpu
);
10761 if (cpu_has_secondary_exec_ctrls()) {
10762 exec_control
= vmx
->secondary_exec_control
;
10764 /* Take the following fields only from vmcs12 */
10765 exec_control
&= ~(SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES
|
10766 SECONDARY_EXEC_ENABLE_INVPCID
|
10767 SECONDARY_EXEC_RDTSCP
|
10768 SECONDARY_EXEC_XSAVES
|
10769 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY
|
10770 SECONDARY_EXEC_APIC_REGISTER_VIRT
|
10771 SECONDARY_EXEC_ENABLE_VMFUNC
);
10772 if (nested_cpu_has(vmcs12
,
10773 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS
)) {
10774 vmcs12_exec_ctrl
= vmcs12
->secondary_vm_exec_control
&
10775 ~SECONDARY_EXEC_ENABLE_PML
;
10776 exec_control
|= vmcs12_exec_ctrl
;
10779 if (exec_control
& SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY
)
10780 vmcs_write16(GUEST_INTR_STATUS
,
10781 vmcs12
->guest_intr_status
);
10784 * Write an illegal value to APIC_ACCESS_ADDR. Later,
10785 * nested_get_vmcs12_pages will either fix it up or
10786 * remove the VM execution control.
10788 if (exec_control
& SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES
)
10789 vmcs_write64(APIC_ACCESS_ADDR
, -1ull);
10791 vmcs_write32(SECONDARY_VM_EXEC_CONTROL
, exec_control
);
10795 * HOST_RSP is normally set correctly in vmx_vcpu_run() just before
10796 * entry, but only if the current (host) sp changed from the value
10797 * we wrote last (vmx->host_rsp). This cache is no longer relevant
10798 * if we switch vmcs, and rather than hold a separate cache per vmcs,
10799 * here we just force the write to happen on entry.
10803 exec_control
= vmx_exec_control(vmx
); /* L0's desires */
10804 exec_control
&= ~CPU_BASED_VIRTUAL_INTR_PENDING
;
10805 exec_control
&= ~CPU_BASED_VIRTUAL_NMI_PENDING
;
10806 exec_control
&= ~CPU_BASED_TPR_SHADOW
;
10807 exec_control
|= vmcs12
->cpu_based_vm_exec_control
;
10810 * Write an illegal value to VIRTUAL_APIC_PAGE_ADDR. Later, if
10811 * nested_get_vmcs12_pages can't fix it up, the illegal value
10812 * will result in a VM entry failure.
10814 if (exec_control
& CPU_BASED_TPR_SHADOW
) {
10815 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR
, -1ull);
10816 vmcs_write32(TPR_THRESHOLD
, vmcs12
->tpr_threshold
);
10818 #ifdef CONFIG_X86_64
10819 exec_control
|= CPU_BASED_CR8_LOAD_EXITING
|
10820 CPU_BASED_CR8_STORE_EXITING
;
10825 * A vmexit (to either L1 hypervisor or L0 userspace) is always needed
10826 * for I/O port accesses.
10828 exec_control
&= ~CPU_BASED_USE_IO_BITMAPS
;
10829 exec_control
|= CPU_BASED_UNCOND_IO_EXITING
;
10831 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL
, exec_control
);
10833 /* EXCEPTION_BITMAP and CR0_GUEST_HOST_MASK should basically be the
10834 * bitwise-or of what L1 wants to trap for L2, and what we want to
10835 * trap. Note that CR0.TS also needs updating - we do this later.
10837 update_exception_bitmap(vcpu
);
10838 vcpu
->arch
.cr0_guest_owned_bits
&= ~vmcs12
->cr0_guest_host_mask
;
10839 vmcs_writel(CR0_GUEST_HOST_MASK
, ~vcpu
->arch
.cr0_guest_owned_bits
);
10841 /* L2->L1 exit controls are emulated - the hardware exit is to L0 so
10842 * we should use its exit controls. Note that VM_EXIT_LOAD_IA32_EFER
10843 * bits are further modified by vmx_set_efer() below.
10845 vmcs_write32(VM_EXIT_CONTROLS
, vmcs_config
.vmexit_ctrl
);
10847 /* vmcs12's VM_ENTRY_LOAD_IA32_EFER and VM_ENTRY_IA32E_MODE are
10848 * emulated by vmx_set_efer(), below.
10850 vm_entry_controls_init(vmx
,
10851 (vmcs12
->vm_entry_controls
& ~VM_ENTRY_LOAD_IA32_EFER
&
10852 ~VM_ENTRY_IA32E_MODE
) |
10853 (vmcs_config
.vmentry_ctrl
& ~VM_ENTRY_IA32E_MODE
));
10855 if (from_vmentry
&&
10856 (vmcs12
->vm_entry_controls
& VM_ENTRY_LOAD_IA32_PAT
)) {
10857 vmcs_write64(GUEST_IA32_PAT
, vmcs12
->guest_ia32_pat
);
10858 vcpu
->arch
.pat
= vmcs12
->guest_ia32_pat
;
10859 } else if (vmcs_config
.vmentry_ctrl
& VM_ENTRY_LOAD_IA32_PAT
) {
10860 vmcs_write64(GUEST_IA32_PAT
, vmx
->vcpu
.arch
.pat
);
10863 if (vmcs12
->cpu_based_vm_exec_control
& CPU_BASED_USE_TSC_OFFSETING
)
10864 vmcs_write64(TSC_OFFSET
,
10865 vcpu
->arch
.tsc_offset
+ vmcs12
->tsc_offset
);
10867 vmcs_write64(TSC_OFFSET
, vcpu
->arch
.tsc_offset
);
10868 if (kvm_has_tsc_control
)
10869 decache_tsc_multiplier(vmx
);
10873 * There is no direct mapping between vpid02 and vpid12, the
10874 * vpid02 is per-vCPU for L0 and reused while the value of
10875 * vpid12 is changed w/ one invvpid during nested vmentry.
10876 * The vpid12 is allocated by L1 for L2, so it will not
10877 * influence global bitmap(for vpid01 and vpid02 allocation)
10878 * even if spawn a lot of nested vCPUs.
10880 if (nested_cpu_has_vpid(vmcs12
) && vmx
->nested
.vpid02
) {
10881 if (vmcs12
->virtual_processor_id
!= vmx
->nested
.last_vpid
) {
10882 vmx
->nested
.last_vpid
= vmcs12
->virtual_processor_id
;
10883 __vmx_flush_tlb(vcpu
, to_vmx(vcpu
)->nested
.vpid02
, true);
10886 vmx_flush_tlb(vcpu
, true);
10892 * Conceptually we want to copy the PML address and index from
10893 * vmcs01 here, and then back to vmcs01 on nested vmexit. But,
10894 * since we always flush the log on each vmexit, this happens
10895 * to be equivalent to simply resetting the fields in vmcs02.
10897 ASSERT(vmx
->pml_pg
);
10898 vmcs_write64(PML_ADDRESS
, page_to_phys(vmx
->pml_pg
));
10899 vmcs_write16(GUEST_PML_INDEX
, PML_ENTITY_NUM
- 1);
10902 if (nested_cpu_has_ept(vmcs12
)) {
10903 if (nested_ept_init_mmu_context(vcpu
)) {
10904 *entry_failure_code
= ENTRY_FAIL_DEFAULT
;
10907 } else if (nested_cpu_has2(vmcs12
,
10908 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES
)) {
10909 vmx_flush_tlb_ept_only(vcpu
);
10913 * This sets GUEST_CR0 to vmcs12->guest_cr0, possibly modifying those
10914 * bits which we consider mandatory enabled.
10915 * The CR0_READ_SHADOW is what L2 should have expected to read given
10916 * the specifications by L1; It's not enough to take
10917 * vmcs12->cr0_read_shadow because on our cr0_guest_host_mask we we
10918 * have more bits than L1 expected.
10920 vmx_set_cr0(vcpu
, vmcs12
->guest_cr0
);
10921 vmcs_writel(CR0_READ_SHADOW
, nested_read_cr0(vmcs12
));
10923 vmx_set_cr4(vcpu
, vmcs12
->guest_cr4
);
10924 vmcs_writel(CR4_READ_SHADOW
, nested_read_cr4(vmcs12
));
10926 if (from_vmentry
&&
10927 (vmcs12
->vm_entry_controls
& VM_ENTRY_LOAD_IA32_EFER
))
10928 vcpu
->arch
.efer
= vmcs12
->guest_ia32_efer
;
10929 else if (vmcs12
->vm_entry_controls
& VM_ENTRY_IA32E_MODE
)
10930 vcpu
->arch
.efer
|= (EFER_LMA
| EFER_LME
);
10932 vcpu
->arch
.efer
&= ~(EFER_LMA
| EFER_LME
);
10933 /* Note: modifies VM_ENTRY/EXIT_CONTROLS and GUEST/HOST_IA32_EFER */
10934 vmx_set_efer(vcpu
, vcpu
->arch
.efer
);
10936 if (vmx
->nested
.dirty_vmcs12
) {
10937 prepare_vmcs02_full(vcpu
, vmcs12
, from_vmentry
);
10938 vmx
->nested
.dirty_vmcs12
= false;
10941 /* Shadow page tables on either EPT or shadow page tables. */
10942 if (nested_vmx_load_cr3(vcpu
, vmcs12
->guest_cr3
, nested_cpu_has_ept(vmcs12
),
10943 entry_failure_code
))
10947 vcpu
->arch
.walk_mmu
->inject_page_fault
= vmx_inject_page_fault_nested
;
10949 kvm_register_write(vcpu
, VCPU_REGS_RSP
, vmcs12
->guest_rsp
);
10950 kvm_register_write(vcpu
, VCPU_REGS_RIP
, vmcs12
->guest_rip
);
10954 static int check_vmentry_prereqs(struct kvm_vcpu
*vcpu
, struct vmcs12
*vmcs12
)
10956 struct vcpu_vmx
*vmx
= to_vmx(vcpu
);
10958 if (vmcs12
->guest_activity_state
!= GUEST_ACTIVITY_ACTIVE
&&
10959 vmcs12
->guest_activity_state
!= GUEST_ACTIVITY_HLT
)
10960 return VMXERR_ENTRY_INVALID_CONTROL_FIELD
;
10962 if (nested_vmx_check_io_bitmap_controls(vcpu
, vmcs12
))
10963 return VMXERR_ENTRY_INVALID_CONTROL_FIELD
;
10965 if (nested_vmx_check_msr_bitmap_controls(vcpu
, vmcs12
))
10966 return VMXERR_ENTRY_INVALID_CONTROL_FIELD
;
10968 if (nested_vmx_check_tpr_shadow_controls(vcpu
, vmcs12
))
10969 return VMXERR_ENTRY_INVALID_CONTROL_FIELD
;
10971 if (nested_vmx_check_apicv_controls(vcpu
, vmcs12
))
10972 return VMXERR_ENTRY_INVALID_CONTROL_FIELD
;
10974 if (nested_vmx_check_msr_switch_controls(vcpu
, vmcs12
))
10975 return VMXERR_ENTRY_INVALID_CONTROL_FIELD
;
10977 if (nested_vmx_check_pml_controls(vcpu
, vmcs12
))
10978 return VMXERR_ENTRY_INVALID_CONTROL_FIELD
;
10980 if (!vmx_control_verify(vmcs12
->cpu_based_vm_exec_control
,
10981 vmx
->nested
.nested_vmx_procbased_ctls_low
,
10982 vmx
->nested
.nested_vmx_procbased_ctls_high
) ||
10983 (nested_cpu_has(vmcs12
, CPU_BASED_ACTIVATE_SECONDARY_CONTROLS
) &&
10984 !vmx_control_verify(vmcs12
->secondary_vm_exec_control
,
10985 vmx
->nested
.nested_vmx_secondary_ctls_low
,
10986 vmx
->nested
.nested_vmx_secondary_ctls_high
)) ||
10987 !vmx_control_verify(vmcs12
->pin_based_vm_exec_control
,
10988 vmx
->nested
.nested_vmx_pinbased_ctls_low
,
10989 vmx
->nested
.nested_vmx_pinbased_ctls_high
) ||
10990 !vmx_control_verify(vmcs12
->vm_exit_controls
,
10991 vmx
->nested
.nested_vmx_exit_ctls_low
,
10992 vmx
->nested
.nested_vmx_exit_ctls_high
) ||
10993 !vmx_control_verify(vmcs12
->vm_entry_controls
,
10994 vmx
->nested
.nested_vmx_entry_ctls_low
,
10995 vmx
->nested
.nested_vmx_entry_ctls_high
))
10996 return VMXERR_ENTRY_INVALID_CONTROL_FIELD
;
10998 if (nested_cpu_has_vmfunc(vmcs12
)) {
10999 if (vmcs12
->vm_function_control
&
11000 ~vmx
->nested
.nested_vmx_vmfunc_controls
)
11001 return VMXERR_ENTRY_INVALID_CONTROL_FIELD
;
11003 if (nested_cpu_has_eptp_switching(vmcs12
)) {
11004 if (!nested_cpu_has_ept(vmcs12
) ||
11005 !page_address_valid(vcpu
, vmcs12
->eptp_list_address
))
11006 return VMXERR_ENTRY_INVALID_CONTROL_FIELD
;
11010 if (vmcs12
->cr3_target_count
> nested_cpu_vmx_misc_cr3_count(vcpu
))
11011 return VMXERR_ENTRY_INVALID_CONTROL_FIELD
;
11013 if (!nested_host_cr0_valid(vcpu
, vmcs12
->host_cr0
) ||
11014 !nested_host_cr4_valid(vcpu
, vmcs12
->host_cr4
) ||
11015 !nested_cr3_valid(vcpu
, vmcs12
->host_cr3
))
11016 return VMXERR_ENTRY_INVALID_HOST_STATE_FIELD
;
11021 static int check_vmentry_postreqs(struct kvm_vcpu
*vcpu
, struct vmcs12
*vmcs12
,
11026 *exit_qual
= ENTRY_FAIL_DEFAULT
;
11028 if (!nested_guest_cr0_valid(vcpu
, vmcs12
->guest_cr0
) ||
11029 !nested_guest_cr4_valid(vcpu
, vmcs12
->guest_cr4
))
11032 if (!nested_cpu_has2(vmcs12
, SECONDARY_EXEC_SHADOW_VMCS
) &&
11033 vmcs12
->vmcs_link_pointer
!= -1ull) {
11034 *exit_qual
= ENTRY_FAIL_VMCS_LINK_PTR
;
11039 * If the load IA32_EFER VM-entry control is 1, the following checks
11040 * are performed on the field for the IA32_EFER MSR:
11041 * - Bits reserved in the IA32_EFER MSR must be 0.
11042 * - Bit 10 (corresponding to IA32_EFER.LMA) must equal the value of
11043 * the IA-32e mode guest VM-exit control. It must also be identical
11044 * to bit 8 (LME) if bit 31 in the CR0 field (corresponding to
11047 if (to_vmx(vcpu
)->nested
.nested_run_pending
&&
11048 (vmcs12
->vm_entry_controls
& VM_ENTRY_LOAD_IA32_EFER
)) {
11049 ia32e
= (vmcs12
->vm_entry_controls
& VM_ENTRY_IA32E_MODE
) != 0;
11050 if (!kvm_valid_efer(vcpu
, vmcs12
->guest_ia32_efer
) ||
11051 ia32e
!= !!(vmcs12
->guest_ia32_efer
& EFER_LMA
) ||
11052 ((vmcs12
->guest_cr0
& X86_CR0_PG
) &&
11053 ia32e
!= !!(vmcs12
->guest_ia32_efer
& EFER_LME
)))
11058 * If the load IA32_EFER VM-exit control is 1, bits reserved in the
11059 * IA32_EFER MSR must be 0 in the field for that register. In addition,
11060 * the values of the LMA and LME bits in the field must each be that of
11061 * the host address-space size VM-exit control.
11063 if (vmcs12
->vm_exit_controls
& VM_EXIT_LOAD_IA32_EFER
) {
11064 ia32e
= (vmcs12
->vm_exit_controls
&
11065 VM_EXIT_HOST_ADDR_SPACE_SIZE
) != 0;
11066 if (!kvm_valid_efer(vcpu
, vmcs12
->host_ia32_efer
) ||
11067 ia32e
!= !!(vmcs12
->host_ia32_efer
& EFER_LMA
) ||
11068 ia32e
!= !!(vmcs12
->host_ia32_efer
& EFER_LME
))
11072 if ((vmcs12
->vm_entry_controls
& VM_ENTRY_LOAD_BNDCFGS
) &&
11073 (is_noncanonical_address(vmcs12
->guest_bndcfgs
& PAGE_MASK
, vcpu
) ||
11074 (vmcs12
->guest_bndcfgs
& MSR_IA32_BNDCFGS_RSVD
)))
11080 static int enter_vmx_non_root_mode(struct kvm_vcpu
*vcpu
, bool from_vmentry
)
11082 struct vcpu_vmx
*vmx
= to_vmx(vcpu
);
11083 struct vmcs12
*vmcs12
= get_vmcs12(vcpu
);
11087 enter_guest_mode(vcpu
);
11089 if (!(vmcs12
->vm_entry_controls
& VM_ENTRY_LOAD_DEBUG_CONTROLS
))
11090 vmx
->nested
.vmcs01_debugctl
= vmcs_read64(GUEST_IA32_DEBUGCTL
);
11092 vmx_switch_vmcs(vcpu
, &vmx
->nested
.vmcs02
);
11093 vmx_segment_cache_clear(vmx
);
11095 if (prepare_vmcs02(vcpu
, vmcs12
, from_vmentry
, &exit_qual
)) {
11096 leave_guest_mode(vcpu
);
11097 vmx_switch_vmcs(vcpu
, &vmx
->vmcs01
);
11098 nested_vmx_entry_failure(vcpu
, vmcs12
,
11099 EXIT_REASON_INVALID_STATE
, exit_qual
);
11103 nested_get_vmcs12_pages(vcpu
, vmcs12
);
11105 msr_entry_idx
= nested_vmx_load_msr(vcpu
,
11106 vmcs12
->vm_entry_msr_load_addr
,
11107 vmcs12
->vm_entry_msr_load_count
);
11108 if (msr_entry_idx
) {
11109 leave_guest_mode(vcpu
);
11110 vmx_switch_vmcs(vcpu
, &vmx
->vmcs01
);
11111 nested_vmx_entry_failure(vcpu
, vmcs12
,
11112 EXIT_REASON_MSR_LOAD_FAIL
, msr_entry_idx
);
11117 * Note no nested_vmx_succeed or nested_vmx_fail here. At this point
11118 * we are no longer running L1, and VMLAUNCH/VMRESUME has not yet
11119 * returned as far as L1 is concerned. It will only return (and set
11120 * the success flag) when L2 exits (see nested_vmx_vmexit()).
11126 * nested_vmx_run() handles a nested entry, i.e., a VMLAUNCH or VMRESUME on L1
11127 * for running an L2 nested guest.
11129 static int nested_vmx_run(struct kvm_vcpu
*vcpu
, bool launch
)
11131 struct vmcs12
*vmcs12
;
11132 struct vcpu_vmx
*vmx
= to_vmx(vcpu
);
11133 u32 interrupt_shadow
= vmx_get_interrupt_shadow(vcpu
);
11137 if (!nested_vmx_check_permission(vcpu
))
11140 if (!nested_vmx_check_vmcs12(vcpu
))
11143 vmcs12
= get_vmcs12(vcpu
);
11145 if (enable_shadow_vmcs
)
11146 copy_shadow_to_vmcs12(vmx
);
11149 * The nested entry process starts with enforcing various prerequisites
11150 * on vmcs12 as required by the Intel SDM, and act appropriately when
11151 * they fail: As the SDM explains, some conditions should cause the
11152 * instruction to fail, while others will cause the instruction to seem
11153 * to succeed, but return an EXIT_REASON_INVALID_STATE.
11154 * To speed up the normal (success) code path, we should avoid checking
11155 * for misconfigurations which will anyway be caught by the processor
11156 * when using the merged vmcs02.
11158 if (interrupt_shadow
& KVM_X86_SHADOW_INT_MOV_SS
) {
11159 nested_vmx_failValid(vcpu
,
11160 VMXERR_ENTRY_EVENTS_BLOCKED_BY_MOV_SS
);
11164 if (vmcs12
->launch_state
== launch
) {
11165 nested_vmx_failValid(vcpu
,
11166 launch
? VMXERR_VMLAUNCH_NONCLEAR_VMCS
11167 : VMXERR_VMRESUME_NONLAUNCHED_VMCS
);
11171 ret
= check_vmentry_prereqs(vcpu
, vmcs12
);
11173 nested_vmx_failValid(vcpu
, ret
);
11178 * After this point, the trap flag no longer triggers a singlestep trap
11179 * on the vm entry instructions; don't call kvm_skip_emulated_instruction.
11180 * This is not 100% correct; for performance reasons, we delegate most
11181 * of the checks on host state to the processor. If those fail,
11182 * the singlestep trap is missed.
11184 skip_emulated_instruction(vcpu
);
11186 ret
= check_vmentry_postreqs(vcpu
, vmcs12
, &exit_qual
);
11188 nested_vmx_entry_failure(vcpu
, vmcs12
,
11189 EXIT_REASON_INVALID_STATE
, exit_qual
);
11194 * We're finally done with prerequisite checking, and can start with
11195 * the nested entry.
11198 ret
= enter_vmx_non_root_mode(vcpu
, true);
11202 if (vmcs12
->guest_activity_state
== GUEST_ACTIVITY_HLT
)
11203 return kvm_vcpu_halt(vcpu
);
11205 vmx
->nested
.nested_run_pending
= 1;
11210 return kvm_skip_emulated_instruction(vcpu
);
11214 * On a nested exit from L2 to L1, vmcs12.guest_cr0 might not be up-to-date
11215 * because L2 may have changed some cr0 bits directly (CRO_GUEST_HOST_MASK).
11216 * This function returns the new value we should put in vmcs12.guest_cr0.
11217 * It's not enough to just return the vmcs02 GUEST_CR0. Rather,
11218 * 1. Bits that neither L0 nor L1 trapped, were set directly by L2 and are now
11219 * available in vmcs02 GUEST_CR0. (Note: It's enough to check that L0
11220 * didn't trap the bit, because if L1 did, so would L0).
11221 * 2. Bits that L1 asked to trap (and therefore L0 also did) could not have
11222 * been modified by L2, and L1 knows it. So just leave the old value of
11223 * the bit from vmcs12.guest_cr0. Note that the bit from vmcs02 GUEST_CR0
11224 * isn't relevant, because if L0 traps this bit it can set it to anything.
11225 * 3. Bits that L1 didn't trap, but L0 did. L1 believes the guest could have
11226 * changed these bits, and therefore they need to be updated, but L0
11227 * didn't necessarily allow them to be changed in GUEST_CR0 - and rather
11228 * put them in vmcs02 CR0_READ_SHADOW. So take these bits from there.
11230 static inline unsigned long
11231 vmcs12_guest_cr0(struct kvm_vcpu
*vcpu
, struct vmcs12
*vmcs12
)
11234 /*1*/ (vmcs_readl(GUEST_CR0
) & vcpu
->arch
.cr0_guest_owned_bits
) |
11235 /*2*/ (vmcs12
->guest_cr0
& vmcs12
->cr0_guest_host_mask
) |
11236 /*3*/ (vmcs_readl(CR0_READ_SHADOW
) & ~(vmcs12
->cr0_guest_host_mask
|
11237 vcpu
->arch
.cr0_guest_owned_bits
));
11240 static inline unsigned long
11241 vmcs12_guest_cr4(struct kvm_vcpu
*vcpu
, struct vmcs12
*vmcs12
)
11244 /*1*/ (vmcs_readl(GUEST_CR4
) & vcpu
->arch
.cr4_guest_owned_bits
) |
11245 /*2*/ (vmcs12
->guest_cr4
& vmcs12
->cr4_guest_host_mask
) |
11246 /*3*/ (vmcs_readl(CR4_READ_SHADOW
) & ~(vmcs12
->cr4_guest_host_mask
|
11247 vcpu
->arch
.cr4_guest_owned_bits
));
11250 static void vmcs12_save_pending_event(struct kvm_vcpu
*vcpu
,
11251 struct vmcs12
*vmcs12
)
11256 if (vcpu
->arch
.exception
.injected
) {
11257 nr
= vcpu
->arch
.exception
.nr
;
11258 idt_vectoring
= nr
| VECTORING_INFO_VALID_MASK
;
11260 if (kvm_exception_is_soft(nr
)) {
11261 vmcs12
->vm_exit_instruction_len
=
11262 vcpu
->arch
.event_exit_inst_len
;
11263 idt_vectoring
|= INTR_TYPE_SOFT_EXCEPTION
;
11265 idt_vectoring
|= INTR_TYPE_HARD_EXCEPTION
;
11267 if (vcpu
->arch
.exception
.has_error_code
) {
11268 idt_vectoring
|= VECTORING_INFO_DELIVER_CODE_MASK
;
11269 vmcs12
->idt_vectoring_error_code
=
11270 vcpu
->arch
.exception
.error_code
;
11273 vmcs12
->idt_vectoring_info_field
= idt_vectoring
;
11274 } else if (vcpu
->arch
.nmi_injected
) {
11275 vmcs12
->idt_vectoring_info_field
=
11276 INTR_TYPE_NMI_INTR
| INTR_INFO_VALID_MASK
| NMI_VECTOR
;
11277 } else if (vcpu
->arch
.interrupt
.pending
) {
11278 nr
= vcpu
->arch
.interrupt
.nr
;
11279 idt_vectoring
= nr
| VECTORING_INFO_VALID_MASK
;
11281 if (vcpu
->arch
.interrupt
.soft
) {
11282 idt_vectoring
|= INTR_TYPE_SOFT_INTR
;
11283 vmcs12
->vm_entry_instruction_len
=
11284 vcpu
->arch
.event_exit_inst_len
;
11286 idt_vectoring
|= INTR_TYPE_EXT_INTR
;
11288 vmcs12
->idt_vectoring_info_field
= idt_vectoring
;
11292 static int vmx_check_nested_events(struct kvm_vcpu
*vcpu
, bool external_intr
)
11294 struct vcpu_vmx
*vmx
= to_vmx(vcpu
);
11295 unsigned long exit_qual
;
11296 bool block_nested_events
=
11297 vmx
->nested
.nested_run_pending
|| kvm_event_needs_reinjection(vcpu
);
11299 if (vcpu
->arch
.exception
.pending
&&
11300 nested_vmx_check_exception(vcpu
, &exit_qual
)) {
11301 if (block_nested_events
)
11303 nested_vmx_inject_exception_vmexit(vcpu
, exit_qual
);
11307 if (nested_cpu_has_preemption_timer(get_vmcs12(vcpu
)) &&
11308 vmx
->nested
.preemption_timer_expired
) {
11309 if (block_nested_events
)
11311 nested_vmx_vmexit(vcpu
, EXIT_REASON_PREEMPTION_TIMER
, 0, 0);
11315 if (vcpu
->arch
.nmi_pending
&& nested_exit_on_nmi(vcpu
)) {
11316 if (block_nested_events
)
11318 nested_vmx_vmexit(vcpu
, EXIT_REASON_EXCEPTION_NMI
,
11319 NMI_VECTOR
| INTR_TYPE_NMI_INTR
|
11320 INTR_INFO_VALID_MASK
, 0);
11322 * The NMI-triggered VM exit counts as injection:
11323 * clear this one and block further NMIs.
11325 vcpu
->arch
.nmi_pending
= 0;
11326 vmx_set_nmi_mask(vcpu
, true);
11330 if ((kvm_cpu_has_interrupt(vcpu
) || external_intr
) &&
11331 nested_exit_on_intr(vcpu
)) {
11332 if (block_nested_events
)
11334 nested_vmx_vmexit(vcpu
, EXIT_REASON_EXTERNAL_INTERRUPT
, 0, 0);
11338 vmx_complete_nested_posted_interrupt(vcpu
);
11342 static u32
vmx_get_preemption_timer_value(struct kvm_vcpu
*vcpu
)
11344 ktime_t remaining
=
11345 hrtimer_get_remaining(&to_vmx(vcpu
)->nested
.preemption_timer
);
11348 if (ktime_to_ns(remaining
) <= 0)
11351 value
= ktime_to_ns(remaining
) * vcpu
->arch
.virtual_tsc_khz
;
11352 do_div(value
, 1000000);
11353 return value
>> VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE
;
11357 * Update the guest state fields of vmcs12 to reflect changes that
11358 * occurred while L2 was running. (The "IA-32e mode guest" bit of the
11359 * VM-entry controls is also updated, since this is really a guest
11362 static void sync_vmcs12(struct kvm_vcpu
*vcpu
, struct vmcs12
*vmcs12
)
11364 vmcs12
->guest_cr0
= vmcs12_guest_cr0(vcpu
, vmcs12
);
11365 vmcs12
->guest_cr4
= vmcs12_guest_cr4(vcpu
, vmcs12
);
11367 vmcs12
->guest_rsp
= kvm_register_read(vcpu
, VCPU_REGS_RSP
);
11368 vmcs12
->guest_rip
= kvm_register_read(vcpu
, VCPU_REGS_RIP
);
11369 vmcs12
->guest_rflags
= vmcs_readl(GUEST_RFLAGS
);
11371 vmcs12
->guest_es_selector
= vmcs_read16(GUEST_ES_SELECTOR
);
11372 vmcs12
->guest_cs_selector
= vmcs_read16(GUEST_CS_SELECTOR
);
11373 vmcs12
->guest_ss_selector
= vmcs_read16(GUEST_SS_SELECTOR
);
11374 vmcs12
->guest_ds_selector
= vmcs_read16(GUEST_DS_SELECTOR
);
11375 vmcs12
->guest_fs_selector
= vmcs_read16(GUEST_FS_SELECTOR
);
11376 vmcs12
->guest_gs_selector
= vmcs_read16(GUEST_GS_SELECTOR
);
11377 vmcs12
->guest_ldtr_selector
= vmcs_read16(GUEST_LDTR_SELECTOR
);
11378 vmcs12
->guest_tr_selector
= vmcs_read16(GUEST_TR_SELECTOR
);
11379 vmcs12
->guest_es_limit
= vmcs_read32(GUEST_ES_LIMIT
);
11380 vmcs12
->guest_cs_limit
= vmcs_read32(GUEST_CS_LIMIT
);
11381 vmcs12
->guest_ss_limit
= vmcs_read32(GUEST_SS_LIMIT
);
11382 vmcs12
->guest_ds_limit
= vmcs_read32(GUEST_DS_LIMIT
);
11383 vmcs12
->guest_fs_limit
= vmcs_read32(GUEST_FS_LIMIT
);
11384 vmcs12
->guest_gs_limit
= vmcs_read32(GUEST_GS_LIMIT
);
11385 vmcs12
->guest_ldtr_limit
= vmcs_read32(GUEST_LDTR_LIMIT
);
11386 vmcs12
->guest_tr_limit
= vmcs_read32(GUEST_TR_LIMIT
);
11387 vmcs12
->guest_gdtr_limit
= vmcs_read32(GUEST_GDTR_LIMIT
);
11388 vmcs12
->guest_idtr_limit
= vmcs_read32(GUEST_IDTR_LIMIT
);
11389 vmcs12
->guest_es_ar_bytes
= vmcs_read32(GUEST_ES_AR_BYTES
);
11390 vmcs12
->guest_cs_ar_bytes
= vmcs_read32(GUEST_CS_AR_BYTES
);
11391 vmcs12
->guest_ss_ar_bytes
= vmcs_read32(GUEST_SS_AR_BYTES
);
11392 vmcs12
->guest_ds_ar_bytes
= vmcs_read32(GUEST_DS_AR_BYTES
);
11393 vmcs12
->guest_fs_ar_bytes
= vmcs_read32(GUEST_FS_AR_BYTES
);
11394 vmcs12
->guest_gs_ar_bytes
= vmcs_read32(GUEST_GS_AR_BYTES
);
11395 vmcs12
->guest_ldtr_ar_bytes
= vmcs_read32(GUEST_LDTR_AR_BYTES
);
11396 vmcs12
->guest_tr_ar_bytes
= vmcs_read32(GUEST_TR_AR_BYTES
);
11397 vmcs12
->guest_es_base
= vmcs_readl(GUEST_ES_BASE
);
11398 vmcs12
->guest_cs_base
= vmcs_readl(GUEST_CS_BASE
);
11399 vmcs12
->guest_ss_base
= vmcs_readl(GUEST_SS_BASE
);
11400 vmcs12
->guest_ds_base
= vmcs_readl(GUEST_DS_BASE
);
11401 vmcs12
->guest_fs_base
= vmcs_readl(GUEST_FS_BASE
);
11402 vmcs12
->guest_gs_base
= vmcs_readl(GUEST_GS_BASE
);
11403 vmcs12
->guest_ldtr_base
= vmcs_readl(GUEST_LDTR_BASE
);
11404 vmcs12
->guest_tr_base
= vmcs_readl(GUEST_TR_BASE
);
11405 vmcs12
->guest_gdtr_base
= vmcs_readl(GUEST_GDTR_BASE
);
11406 vmcs12
->guest_idtr_base
= vmcs_readl(GUEST_IDTR_BASE
);
11408 vmcs12
->guest_interruptibility_info
=
11409 vmcs_read32(GUEST_INTERRUPTIBILITY_INFO
);
11410 vmcs12
->guest_pending_dbg_exceptions
=
11411 vmcs_readl(GUEST_PENDING_DBG_EXCEPTIONS
);
11412 if (vcpu
->arch
.mp_state
== KVM_MP_STATE_HALTED
)
11413 vmcs12
->guest_activity_state
= GUEST_ACTIVITY_HLT
;
11415 vmcs12
->guest_activity_state
= GUEST_ACTIVITY_ACTIVE
;
11417 if (nested_cpu_has_preemption_timer(vmcs12
)) {
11418 if (vmcs12
->vm_exit_controls
&
11419 VM_EXIT_SAVE_VMX_PREEMPTION_TIMER
)
11420 vmcs12
->vmx_preemption_timer_value
=
11421 vmx_get_preemption_timer_value(vcpu
);
11422 hrtimer_cancel(&to_vmx(vcpu
)->nested
.preemption_timer
);
11426 * In some cases (usually, nested EPT), L2 is allowed to change its
11427 * own CR3 without exiting. If it has changed it, we must keep it.
11428 * Of course, if L0 is using shadow page tables, GUEST_CR3 was defined
11429 * by L0, not L1 or L2, so we mustn't unconditionally copy it to vmcs12.
11431 * Additionally, restore L2's PDPTR to vmcs12.
11434 vmcs12
->guest_cr3
= vmcs_readl(GUEST_CR3
);
11435 vmcs12
->guest_pdptr0
= vmcs_read64(GUEST_PDPTR0
);
11436 vmcs12
->guest_pdptr1
= vmcs_read64(GUEST_PDPTR1
);
11437 vmcs12
->guest_pdptr2
= vmcs_read64(GUEST_PDPTR2
);
11438 vmcs12
->guest_pdptr3
= vmcs_read64(GUEST_PDPTR3
);
11441 vmcs12
->guest_linear_address
= vmcs_readl(GUEST_LINEAR_ADDRESS
);
11443 if (nested_cpu_has_vid(vmcs12
))
11444 vmcs12
->guest_intr_status
= vmcs_read16(GUEST_INTR_STATUS
);
11446 vmcs12
->vm_entry_controls
=
11447 (vmcs12
->vm_entry_controls
& ~VM_ENTRY_IA32E_MODE
) |
11448 (vm_entry_controls_get(to_vmx(vcpu
)) & VM_ENTRY_IA32E_MODE
);
11450 if (vmcs12
->vm_exit_controls
& VM_EXIT_SAVE_DEBUG_CONTROLS
) {
11451 kvm_get_dr(vcpu
, 7, (unsigned long *)&vmcs12
->guest_dr7
);
11452 vmcs12
->guest_ia32_debugctl
= vmcs_read64(GUEST_IA32_DEBUGCTL
);
11455 /* TODO: These cannot have changed unless we have MSR bitmaps and
11456 * the relevant bit asks not to trap the change */
11457 if (vmcs12
->vm_exit_controls
& VM_EXIT_SAVE_IA32_PAT
)
11458 vmcs12
->guest_ia32_pat
= vmcs_read64(GUEST_IA32_PAT
);
11459 if (vmcs12
->vm_exit_controls
& VM_EXIT_SAVE_IA32_EFER
)
11460 vmcs12
->guest_ia32_efer
= vcpu
->arch
.efer
;
11461 vmcs12
->guest_sysenter_cs
= vmcs_read32(GUEST_SYSENTER_CS
);
11462 vmcs12
->guest_sysenter_esp
= vmcs_readl(GUEST_SYSENTER_ESP
);
11463 vmcs12
->guest_sysenter_eip
= vmcs_readl(GUEST_SYSENTER_EIP
);
11464 if (kvm_mpx_supported())
11465 vmcs12
->guest_bndcfgs
= vmcs_read64(GUEST_BNDCFGS
);
11469 * prepare_vmcs12 is part of what we need to do when the nested L2 guest exits
11470 * and we want to prepare to run its L1 parent. L1 keeps a vmcs for L2 (vmcs12),
11471 * and this function updates it to reflect the changes to the guest state while
11472 * L2 was running (and perhaps made some exits which were handled directly by L0
11473 * without going back to L1), and to reflect the exit reason.
11474 * Note that we do not have to copy here all VMCS fields, just those that
11475 * could have changed by the L2 guest or the exit - i.e., the guest-state and
11476 * exit-information fields only. Other fields are modified by L1 with VMWRITE,
11477 * which already writes to vmcs12 directly.
11479 static void prepare_vmcs12(struct kvm_vcpu
*vcpu
, struct vmcs12
*vmcs12
,
11480 u32 exit_reason
, u32 exit_intr_info
,
11481 unsigned long exit_qualification
)
11483 /* update guest state fields: */
11484 sync_vmcs12(vcpu
, vmcs12
);
11486 /* update exit information fields: */
11488 vmcs12
->vm_exit_reason
= exit_reason
;
11489 vmcs12
->exit_qualification
= exit_qualification
;
11490 vmcs12
->vm_exit_intr_info
= exit_intr_info
;
11492 vmcs12
->idt_vectoring_info_field
= 0;
11493 vmcs12
->vm_exit_instruction_len
= vmcs_read32(VM_EXIT_INSTRUCTION_LEN
);
11494 vmcs12
->vmx_instruction_info
= vmcs_read32(VMX_INSTRUCTION_INFO
);
11496 if (!(vmcs12
->vm_exit_reason
& VMX_EXIT_REASONS_FAILED_VMENTRY
)) {
11497 vmcs12
->launch_state
= 1;
11499 /* vm_entry_intr_info_field is cleared on exit. Emulate this
11500 * instead of reading the real value. */
11501 vmcs12
->vm_entry_intr_info_field
&= ~INTR_INFO_VALID_MASK
;
11504 * Transfer the event that L0 or L1 may wanted to inject into
11505 * L2 to IDT_VECTORING_INFO_FIELD.
11507 vmcs12_save_pending_event(vcpu
, vmcs12
);
11511 * Drop what we picked up for L2 via vmx_complete_interrupts. It is
11512 * preserved above and would only end up incorrectly in L1.
11514 vcpu
->arch
.nmi_injected
= false;
11515 kvm_clear_exception_queue(vcpu
);
11516 kvm_clear_interrupt_queue(vcpu
);
11519 static void load_vmcs12_mmu_host_state(struct kvm_vcpu
*vcpu
,
11520 struct vmcs12
*vmcs12
)
11522 u32 entry_failure_code
;
11524 nested_ept_uninit_mmu_context(vcpu
);
11527 * Only PDPTE load can fail as the value of cr3 was checked on entry and
11528 * couldn't have changed.
11530 if (nested_vmx_load_cr3(vcpu
, vmcs12
->host_cr3
, false, &entry_failure_code
))
11531 nested_vmx_abort(vcpu
, VMX_ABORT_LOAD_HOST_PDPTE_FAIL
);
11534 vcpu
->arch
.walk_mmu
->inject_page_fault
= kvm_inject_page_fault
;
11538 * A part of what we need to when the nested L2 guest exits and we want to
11539 * run its L1 parent, is to reset L1's guest state to the host state specified
11541 * This function is to be called not only on normal nested exit, but also on
11542 * a nested entry failure, as explained in Intel's spec, 3B.23.7 ("VM-Entry
11543 * Failures During or After Loading Guest State").
11544 * This function should be called when the active VMCS is L1's (vmcs01).
11546 static void load_vmcs12_host_state(struct kvm_vcpu
*vcpu
,
11547 struct vmcs12
*vmcs12
)
11549 struct kvm_segment seg
;
11551 if (vmcs12
->vm_exit_controls
& VM_EXIT_LOAD_IA32_EFER
)
11552 vcpu
->arch
.efer
= vmcs12
->host_ia32_efer
;
11553 else if (vmcs12
->vm_exit_controls
& VM_EXIT_HOST_ADDR_SPACE_SIZE
)
11554 vcpu
->arch
.efer
|= (EFER_LMA
| EFER_LME
);
11556 vcpu
->arch
.efer
&= ~(EFER_LMA
| EFER_LME
);
11557 vmx_set_efer(vcpu
, vcpu
->arch
.efer
);
11559 kvm_register_write(vcpu
, VCPU_REGS_RSP
, vmcs12
->host_rsp
);
11560 kvm_register_write(vcpu
, VCPU_REGS_RIP
, vmcs12
->host_rip
);
11561 vmx_set_rflags(vcpu
, X86_EFLAGS_FIXED
);
11563 * Note that calling vmx_set_cr0 is important, even if cr0 hasn't
11564 * actually changed, because vmx_set_cr0 refers to efer set above.
11566 * CR0_GUEST_HOST_MASK is already set in the original vmcs01
11567 * (KVM doesn't change it);
11569 vcpu
->arch
.cr0_guest_owned_bits
= X86_CR0_TS
;
11570 vmx_set_cr0(vcpu
, vmcs12
->host_cr0
);
11572 /* Same as above - no reason to call set_cr4_guest_host_mask(). */
11573 vcpu
->arch
.cr4_guest_owned_bits
= ~vmcs_readl(CR4_GUEST_HOST_MASK
);
11574 vmx_set_cr4(vcpu
, vmcs12
->host_cr4
);
11576 load_vmcs12_mmu_host_state(vcpu
, vmcs12
);
11580 * Trivially support vpid by letting L2s share their parent
11581 * L1's vpid. TODO: move to a more elaborate solution, giving
11582 * each L2 its own vpid and exposing the vpid feature to L1.
11584 vmx_flush_tlb(vcpu
, true);
11587 vmcs_write32(GUEST_SYSENTER_CS
, vmcs12
->host_ia32_sysenter_cs
);
11588 vmcs_writel(GUEST_SYSENTER_ESP
, vmcs12
->host_ia32_sysenter_esp
);
11589 vmcs_writel(GUEST_SYSENTER_EIP
, vmcs12
->host_ia32_sysenter_eip
);
11590 vmcs_writel(GUEST_IDTR_BASE
, vmcs12
->host_idtr_base
);
11591 vmcs_writel(GUEST_GDTR_BASE
, vmcs12
->host_gdtr_base
);
11592 vmcs_write32(GUEST_IDTR_LIMIT
, 0xFFFF);
11593 vmcs_write32(GUEST_GDTR_LIMIT
, 0xFFFF);
11595 /* If not VM_EXIT_CLEAR_BNDCFGS, the L2 value propagates to L1. */
11596 if (vmcs12
->vm_exit_controls
& VM_EXIT_CLEAR_BNDCFGS
)
11597 vmcs_write64(GUEST_BNDCFGS
, 0);
11599 if (vmcs12
->vm_exit_controls
& VM_EXIT_LOAD_IA32_PAT
) {
11600 vmcs_write64(GUEST_IA32_PAT
, vmcs12
->host_ia32_pat
);
11601 vcpu
->arch
.pat
= vmcs12
->host_ia32_pat
;
11603 if (vmcs12
->vm_exit_controls
& VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL
)
11604 vmcs_write64(GUEST_IA32_PERF_GLOBAL_CTRL
,
11605 vmcs12
->host_ia32_perf_global_ctrl
);
11607 /* Set L1 segment info according to Intel SDM
11608 27.5.2 Loading Host Segment and Descriptor-Table Registers */
11609 seg
= (struct kvm_segment
) {
11611 .limit
= 0xFFFFFFFF,
11612 .selector
= vmcs12
->host_cs_selector
,
11618 if (vmcs12
->vm_exit_controls
& VM_EXIT_HOST_ADDR_SPACE_SIZE
)
11622 vmx_set_segment(vcpu
, &seg
, VCPU_SREG_CS
);
11623 seg
= (struct kvm_segment
) {
11625 .limit
= 0xFFFFFFFF,
11632 seg
.selector
= vmcs12
->host_ds_selector
;
11633 vmx_set_segment(vcpu
, &seg
, VCPU_SREG_DS
);
11634 seg
.selector
= vmcs12
->host_es_selector
;
11635 vmx_set_segment(vcpu
, &seg
, VCPU_SREG_ES
);
11636 seg
.selector
= vmcs12
->host_ss_selector
;
11637 vmx_set_segment(vcpu
, &seg
, VCPU_SREG_SS
);
11638 seg
.selector
= vmcs12
->host_fs_selector
;
11639 seg
.base
= vmcs12
->host_fs_base
;
11640 vmx_set_segment(vcpu
, &seg
, VCPU_SREG_FS
);
11641 seg
.selector
= vmcs12
->host_gs_selector
;
11642 seg
.base
= vmcs12
->host_gs_base
;
11643 vmx_set_segment(vcpu
, &seg
, VCPU_SREG_GS
);
11644 seg
= (struct kvm_segment
) {
11645 .base
= vmcs12
->host_tr_base
,
11647 .selector
= vmcs12
->host_tr_selector
,
11651 vmx_set_segment(vcpu
, &seg
, VCPU_SREG_TR
);
11653 kvm_set_dr(vcpu
, 7, 0x400);
11654 vmcs_write64(GUEST_IA32_DEBUGCTL
, 0);
11656 if (cpu_has_vmx_msr_bitmap())
11657 vmx_update_msr_bitmap(vcpu
);
11659 if (nested_vmx_load_msr(vcpu
, vmcs12
->vm_exit_msr_load_addr
,
11660 vmcs12
->vm_exit_msr_load_count
))
11661 nested_vmx_abort(vcpu
, VMX_ABORT_LOAD_HOST_MSR_FAIL
);
11665 * Emulate an exit from nested guest (L2) to L1, i.e., prepare to run L1
11666 * and modify vmcs12 to make it see what it would expect to see there if
11667 * L2 was its real guest. Must only be called when in L2 (is_guest_mode())
11669 static void nested_vmx_vmexit(struct kvm_vcpu
*vcpu
, u32 exit_reason
,
11670 u32 exit_intr_info
,
11671 unsigned long exit_qualification
)
11673 struct vcpu_vmx
*vmx
= to_vmx(vcpu
);
11674 struct vmcs12
*vmcs12
= get_vmcs12(vcpu
);
11676 /* trying to cancel vmlaunch/vmresume is a bug */
11677 WARN_ON_ONCE(vmx
->nested
.nested_run_pending
);
11680 * The only expected VM-instruction error is "VM entry with
11681 * invalid control field(s)." Anything else indicates a
11684 WARN_ON_ONCE(vmx
->fail
&& (vmcs_read32(VM_INSTRUCTION_ERROR
) !=
11685 VMXERR_ENTRY_INVALID_CONTROL_FIELD
));
11687 leave_guest_mode(vcpu
);
11689 if (likely(!vmx
->fail
)) {
11690 if (exit_reason
== -1)
11691 sync_vmcs12(vcpu
, vmcs12
);
11693 prepare_vmcs12(vcpu
, vmcs12
, exit_reason
, exit_intr_info
,
11694 exit_qualification
);
11696 if (nested_vmx_store_msr(vcpu
, vmcs12
->vm_exit_msr_store_addr
,
11697 vmcs12
->vm_exit_msr_store_count
))
11698 nested_vmx_abort(vcpu
, VMX_ABORT_SAVE_GUEST_MSR_FAIL
);
11701 vmx_switch_vmcs(vcpu
, &vmx
->vmcs01
);
11702 vm_entry_controls_reset_shadow(vmx
);
11703 vm_exit_controls_reset_shadow(vmx
);
11704 vmx_segment_cache_clear(vmx
);
11706 /* Update any VMCS fields that might have changed while L2 ran */
11707 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT
, vmx
->msr_autoload
.nr
);
11708 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT
, vmx
->msr_autoload
.nr
);
11709 vmcs_write64(TSC_OFFSET
, vcpu
->arch
.tsc_offset
);
11710 if (vmx
->hv_deadline_tsc
== -1)
11711 vmcs_clear_bits(PIN_BASED_VM_EXEC_CONTROL
,
11712 PIN_BASED_VMX_PREEMPTION_TIMER
);
11714 vmcs_set_bits(PIN_BASED_VM_EXEC_CONTROL
,
11715 PIN_BASED_VMX_PREEMPTION_TIMER
);
11716 if (kvm_has_tsc_control
)
11717 decache_tsc_multiplier(vmx
);
11719 if (vmx
->nested
.change_vmcs01_virtual_x2apic_mode
) {
11720 vmx
->nested
.change_vmcs01_virtual_x2apic_mode
= false;
11721 vmx_set_virtual_x2apic_mode(vcpu
,
11722 vcpu
->arch
.apic_base
& X2APIC_ENABLE
);
11723 } else if (!nested_cpu_has_ept(vmcs12
) &&
11724 nested_cpu_has2(vmcs12
,
11725 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES
)) {
11726 vmx_flush_tlb_ept_only(vcpu
);
11729 /* This is needed for same reason as it was needed in prepare_vmcs02 */
11732 /* Unpin physical memory we referred to in vmcs02 */
11733 if (vmx
->nested
.apic_access_page
) {
11734 kvm_release_page_dirty(vmx
->nested
.apic_access_page
);
11735 vmx
->nested
.apic_access_page
= NULL
;
11737 if (vmx
->nested
.virtual_apic_page
) {
11738 kvm_release_page_dirty(vmx
->nested
.virtual_apic_page
);
11739 vmx
->nested
.virtual_apic_page
= NULL
;
11741 if (vmx
->nested
.pi_desc_page
) {
11742 kunmap(vmx
->nested
.pi_desc_page
);
11743 kvm_release_page_dirty(vmx
->nested
.pi_desc_page
);
11744 vmx
->nested
.pi_desc_page
= NULL
;
11745 vmx
->nested
.pi_desc
= NULL
;
11749 * We are now running in L2, mmu_notifier will force to reload the
11750 * page's hpa for L2 vmcs. Need to reload it for L1 before entering L1.
11752 kvm_make_request(KVM_REQ_APIC_PAGE_RELOAD
, vcpu
);
11754 if (enable_shadow_vmcs
&& exit_reason
!= -1)
11755 vmx
->nested
.sync_shadow_vmcs
= true;
11757 /* in case we halted in L2 */
11758 vcpu
->arch
.mp_state
= KVM_MP_STATE_RUNNABLE
;
11760 if (likely(!vmx
->fail
)) {
11762 * TODO: SDM says that with acknowledge interrupt on
11763 * exit, bit 31 of the VM-exit interrupt information
11764 * (valid interrupt) is always set to 1 on
11765 * EXIT_REASON_EXTERNAL_INTERRUPT, so we shouldn't
11766 * need kvm_cpu_has_interrupt(). See the commit
11767 * message for details.
11769 if (nested_exit_intr_ack_set(vcpu
) &&
11770 exit_reason
== EXIT_REASON_EXTERNAL_INTERRUPT
&&
11771 kvm_cpu_has_interrupt(vcpu
)) {
11772 int irq
= kvm_cpu_get_interrupt(vcpu
);
11774 vmcs12
->vm_exit_intr_info
= irq
|
11775 INTR_INFO_VALID_MASK
| INTR_TYPE_EXT_INTR
;
11778 if (exit_reason
!= -1)
11779 trace_kvm_nested_vmexit_inject(vmcs12
->vm_exit_reason
,
11780 vmcs12
->exit_qualification
,
11781 vmcs12
->idt_vectoring_info_field
,
11782 vmcs12
->vm_exit_intr_info
,
11783 vmcs12
->vm_exit_intr_error_code
,
11786 load_vmcs12_host_state(vcpu
, vmcs12
);
11792 * After an early L2 VM-entry failure, we're now back
11793 * in L1 which thinks it just finished a VMLAUNCH or
11794 * VMRESUME instruction, so we need to set the failure
11795 * flag and the VM-instruction error field of the VMCS
11798 nested_vmx_failValid(vcpu
, VMXERR_ENTRY_INVALID_CONTROL_FIELD
);
11800 load_vmcs12_mmu_host_state(vcpu
, vmcs12
);
11803 * The emulated instruction was already skipped in
11804 * nested_vmx_run, but the updated RIP was never
11805 * written back to the vmcs01.
11807 skip_emulated_instruction(vcpu
);
11812 * Forcibly leave nested mode in order to be able to reset the VCPU later on.
11814 static void vmx_leave_nested(struct kvm_vcpu
*vcpu
)
11816 if (is_guest_mode(vcpu
)) {
11817 to_vmx(vcpu
)->nested
.nested_run_pending
= 0;
11818 nested_vmx_vmexit(vcpu
, -1, 0, 0);
11820 free_nested(to_vmx(vcpu
));
11824 * L1's failure to enter L2 is a subset of a normal exit, as explained in
11825 * 23.7 "VM-entry failures during or after loading guest state" (this also
11826 * lists the acceptable exit-reason and exit-qualification parameters).
11827 * It should only be called before L2 actually succeeded to run, and when
11828 * vmcs01 is current (it doesn't leave_guest_mode() or switch vmcss).
11830 static void nested_vmx_entry_failure(struct kvm_vcpu
*vcpu
,
11831 struct vmcs12
*vmcs12
,
11832 u32 reason
, unsigned long qualification
)
11834 load_vmcs12_host_state(vcpu
, vmcs12
);
11835 vmcs12
->vm_exit_reason
= reason
| VMX_EXIT_REASONS_FAILED_VMENTRY
;
11836 vmcs12
->exit_qualification
= qualification
;
11837 nested_vmx_succeed(vcpu
);
11838 if (enable_shadow_vmcs
)
11839 to_vmx(vcpu
)->nested
.sync_shadow_vmcs
= true;
11842 static int vmx_check_intercept(struct kvm_vcpu
*vcpu
,
11843 struct x86_instruction_info
*info
,
11844 enum x86_intercept_stage stage
)
11846 struct vmcs12
*vmcs12
= get_vmcs12(vcpu
);
11847 struct x86_emulate_ctxt
*ctxt
= &vcpu
->arch
.emulate_ctxt
;
11850 * RDPID causes #UD if disabled through secondary execution controls.
11851 * Because it is marked as EmulateOnUD, we need to intercept it here.
11853 if (info
->intercept
== x86_intercept_rdtscp
&&
11854 !nested_cpu_has2(vmcs12
, SECONDARY_EXEC_RDTSCP
)) {
11855 ctxt
->exception
.vector
= UD_VECTOR
;
11856 ctxt
->exception
.error_code_valid
= false;
11857 return X86EMUL_PROPAGATE_FAULT
;
11860 /* TODO: check more intercepts... */
11861 return X86EMUL_CONTINUE
;
11864 #ifdef CONFIG_X86_64
11865 /* (a << shift) / divisor, return 1 if overflow otherwise 0 */
11866 static inline int u64_shl_div_u64(u64 a
, unsigned int shift
,
11867 u64 divisor
, u64
*result
)
11869 u64 low
= a
<< shift
, high
= a
>> (64 - shift
);
11871 /* To avoid the overflow on divq */
11872 if (high
>= divisor
)
11875 /* Low hold the result, high hold rem which is discarded */
11876 asm("divq %2\n\t" : "=a" (low
), "=d" (high
) :
11877 "rm" (divisor
), "0" (low
), "1" (high
));
11883 static int vmx_set_hv_timer(struct kvm_vcpu
*vcpu
, u64 guest_deadline_tsc
)
11885 struct vcpu_vmx
*vmx
= to_vmx(vcpu
);
11886 u64 tscl
= rdtsc();
11887 u64 guest_tscl
= kvm_read_l1_tsc(vcpu
, tscl
);
11888 u64 delta_tsc
= max(guest_deadline_tsc
, guest_tscl
) - guest_tscl
;
11890 /* Convert to host delta tsc if tsc scaling is enabled */
11891 if (vcpu
->arch
.tsc_scaling_ratio
!= kvm_default_tsc_scaling_ratio
&&
11892 u64_shl_div_u64(delta_tsc
,
11893 kvm_tsc_scaling_ratio_frac_bits
,
11894 vcpu
->arch
.tsc_scaling_ratio
,
11899 * If the delta tsc can't fit in the 32 bit after the multi shift,
11900 * we can't use the preemption timer.
11901 * It's possible that it fits on later vmentries, but checking
11902 * on every vmentry is costly so we just use an hrtimer.
11904 if (delta_tsc
>> (cpu_preemption_timer_multi
+ 32))
11907 vmx
->hv_deadline_tsc
= tscl
+ delta_tsc
;
11908 vmcs_set_bits(PIN_BASED_VM_EXEC_CONTROL
,
11909 PIN_BASED_VMX_PREEMPTION_TIMER
);
11911 return delta_tsc
== 0;
11914 static void vmx_cancel_hv_timer(struct kvm_vcpu
*vcpu
)
11916 struct vcpu_vmx
*vmx
= to_vmx(vcpu
);
11917 vmx
->hv_deadline_tsc
= -1;
11918 vmcs_clear_bits(PIN_BASED_VM_EXEC_CONTROL
,
11919 PIN_BASED_VMX_PREEMPTION_TIMER
);
11923 static void vmx_sched_in(struct kvm_vcpu
*vcpu
, int cpu
)
11926 shrink_ple_window(vcpu
);
11929 static void vmx_slot_enable_log_dirty(struct kvm
*kvm
,
11930 struct kvm_memory_slot
*slot
)
11932 kvm_mmu_slot_leaf_clear_dirty(kvm
, slot
);
11933 kvm_mmu_slot_largepage_remove_write_access(kvm
, slot
);
11936 static void vmx_slot_disable_log_dirty(struct kvm
*kvm
,
11937 struct kvm_memory_slot
*slot
)
11939 kvm_mmu_slot_set_dirty(kvm
, slot
);
11942 static void vmx_flush_log_dirty(struct kvm
*kvm
)
11944 kvm_flush_pml_buffers(kvm
);
11947 static int vmx_write_pml_buffer(struct kvm_vcpu
*vcpu
)
11949 struct vmcs12
*vmcs12
;
11950 struct vcpu_vmx
*vmx
= to_vmx(vcpu
);
11952 struct page
*page
= NULL
;
11955 if (is_guest_mode(vcpu
)) {
11956 WARN_ON_ONCE(vmx
->nested
.pml_full
);
11959 * Check if PML is enabled for the nested guest.
11960 * Whether eptp bit 6 is set is already checked
11961 * as part of A/D emulation.
11963 vmcs12
= get_vmcs12(vcpu
);
11964 if (!nested_cpu_has_pml(vmcs12
))
11967 if (vmcs12
->guest_pml_index
>= PML_ENTITY_NUM
) {
11968 vmx
->nested
.pml_full
= true;
11972 gpa
= vmcs_read64(GUEST_PHYSICAL_ADDRESS
) & ~0xFFFull
;
11974 page
= kvm_vcpu_gpa_to_page(vcpu
, vmcs12
->pml_address
);
11975 if (is_error_page(page
))
11978 pml_address
= kmap(page
);
11979 pml_address
[vmcs12
->guest_pml_index
--] = gpa
;
11981 kvm_release_page_clean(page
);
11987 static void vmx_enable_log_dirty_pt_masked(struct kvm
*kvm
,
11988 struct kvm_memory_slot
*memslot
,
11989 gfn_t offset
, unsigned long mask
)
11991 kvm_mmu_clear_dirty_pt_masked(kvm
, memslot
, offset
, mask
);
11994 static void __pi_post_block(struct kvm_vcpu
*vcpu
)
11996 struct pi_desc
*pi_desc
= vcpu_to_pi_desc(vcpu
);
11997 struct pi_desc old
, new;
12001 old
.control
= new.control
= pi_desc
->control
;
12002 WARN(old
.nv
!= POSTED_INTR_WAKEUP_VECTOR
,
12003 "Wakeup handler not enabled while the VCPU is blocked\n");
12005 dest
= cpu_physical_id(vcpu
->cpu
);
12007 if (x2apic_enabled())
12010 new.ndst
= (dest
<< 8) & 0xFF00;
12012 /* set 'NV' to 'notification vector' */
12013 new.nv
= POSTED_INTR_VECTOR
;
12014 } while (cmpxchg64(&pi_desc
->control
, old
.control
,
12015 new.control
) != old
.control
);
12017 if (!WARN_ON_ONCE(vcpu
->pre_pcpu
== -1)) {
12018 spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock
, vcpu
->pre_pcpu
));
12019 list_del(&vcpu
->blocked_vcpu_list
);
12020 spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock
, vcpu
->pre_pcpu
));
12021 vcpu
->pre_pcpu
= -1;
12026 * This routine does the following things for vCPU which is going
12027 * to be blocked if VT-d PI is enabled.
12028 * - Store the vCPU to the wakeup list, so when interrupts happen
12029 * we can find the right vCPU to wake up.
12030 * - Change the Posted-interrupt descriptor as below:
12031 * 'NDST' <-- vcpu->pre_pcpu
12032 * 'NV' <-- POSTED_INTR_WAKEUP_VECTOR
12033 * - If 'ON' is set during this process, which means at least one
12034 * interrupt is posted for this vCPU, we cannot block it, in
12035 * this case, return 1, otherwise, return 0.
12038 static int pi_pre_block(struct kvm_vcpu
*vcpu
)
12041 struct pi_desc old
, new;
12042 struct pi_desc
*pi_desc
= vcpu_to_pi_desc(vcpu
);
12044 if (!kvm_arch_has_assigned_device(vcpu
->kvm
) ||
12045 !irq_remapping_cap(IRQ_POSTING_CAP
) ||
12046 !kvm_vcpu_apicv_active(vcpu
))
12049 WARN_ON(irqs_disabled());
12050 local_irq_disable();
12051 if (!WARN_ON_ONCE(vcpu
->pre_pcpu
!= -1)) {
12052 vcpu
->pre_pcpu
= vcpu
->cpu
;
12053 spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock
, vcpu
->pre_pcpu
));
12054 list_add_tail(&vcpu
->blocked_vcpu_list
,
12055 &per_cpu(blocked_vcpu_on_cpu
,
12057 spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock
, vcpu
->pre_pcpu
));
12061 old
.control
= new.control
= pi_desc
->control
;
12063 WARN((pi_desc
->sn
== 1),
12064 "Warning: SN field of posted-interrupts "
12065 "is set before blocking\n");
12068 * Since vCPU can be preempted during this process,
12069 * vcpu->cpu could be different with pre_pcpu, we
12070 * need to set pre_pcpu as the destination of wakeup
12071 * notification event, then we can find the right vCPU
12072 * to wakeup in wakeup handler if interrupts happen
12073 * when the vCPU is in blocked state.
12075 dest
= cpu_physical_id(vcpu
->pre_pcpu
);
12077 if (x2apic_enabled())
12080 new.ndst
= (dest
<< 8) & 0xFF00;
12082 /* set 'NV' to 'wakeup vector' */
12083 new.nv
= POSTED_INTR_WAKEUP_VECTOR
;
12084 } while (cmpxchg64(&pi_desc
->control
, old
.control
,
12085 new.control
) != old
.control
);
12087 /* We should not block the vCPU if an interrupt is posted for it. */
12088 if (pi_test_on(pi_desc
) == 1)
12089 __pi_post_block(vcpu
);
12091 local_irq_enable();
12092 return (vcpu
->pre_pcpu
== -1);
12095 static int vmx_pre_block(struct kvm_vcpu
*vcpu
)
12097 if (pi_pre_block(vcpu
))
12100 if (kvm_lapic_hv_timer_in_use(vcpu
))
12101 kvm_lapic_switch_to_sw_timer(vcpu
);
12106 static void pi_post_block(struct kvm_vcpu
*vcpu
)
12108 if (vcpu
->pre_pcpu
== -1)
12111 WARN_ON(irqs_disabled());
12112 local_irq_disable();
12113 __pi_post_block(vcpu
);
12114 local_irq_enable();
12117 static void vmx_post_block(struct kvm_vcpu
*vcpu
)
12119 if (kvm_x86_ops
->set_hv_timer
)
12120 kvm_lapic_switch_to_hv_timer(vcpu
);
12122 pi_post_block(vcpu
);
12126 * vmx_update_pi_irte - set IRTE for Posted-Interrupts
12129 * @host_irq: host irq of the interrupt
12130 * @guest_irq: gsi of the interrupt
12131 * @set: set or unset PI
12132 * returns 0 on success, < 0 on failure
12134 static int vmx_update_pi_irte(struct kvm
*kvm
, unsigned int host_irq
,
12135 uint32_t guest_irq
, bool set
)
12137 struct kvm_kernel_irq_routing_entry
*e
;
12138 struct kvm_irq_routing_table
*irq_rt
;
12139 struct kvm_lapic_irq irq
;
12140 struct kvm_vcpu
*vcpu
;
12141 struct vcpu_data vcpu_info
;
12144 if (!kvm_arch_has_assigned_device(kvm
) ||
12145 !irq_remapping_cap(IRQ_POSTING_CAP
) ||
12146 !kvm_vcpu_apicv_active(kvm
->vcpus
[0]))
12149 idx
= srcu_read_lock(&kvm
->irq_srcu
);
12150 irq_rt
= srcu_dereference(kvm
->irq_routing
, &kvm
->irq_srcu
);
12151 if (guest_irq
>= irq_rt
->nr_rt_entries
||
12152 hlist_empty(&irq_rt
->map
[guest_irq
])) {
12153 pr_warn_once("no route for guest_irq %u/%u (broken user space?)\n",
12154 guest_irq
, irq_rt
->nr_rt_entries
);
12158 hlist_for_each_entry(e
, &irq_rt
->map
[guest_irq
], link
) {
12159 if (e
->type
!= KVM_IRQ_ROUTING_MSI
)
12162 * VT-d PI cannot support posting multicast/broadcast
12163 * interrupts to a vCPU, we still use interrupt remapping
12164 * for these kind of interrupts.
12166 * For lowest-priority interrupts, we only support
12167 * those with single CPU as the destination, e.g. user
12168 * configures the interrupts via /proc/irq or uses
12169 * irqbalance to make the interrupts single-CPU.
12171 * We will support full lowest-priority interrupt later.
12174 kvm_set_msi_irq(kvm
, e
, &irq
);
12175 if (!kvm_intr_is_single_vcpu(kvm
, &irq
, &vcpu
)) {
12177 * Make sure the IRTE is in remapped mode if
12178 * we don't handle it in posted mode.
12180 ret
= irq_set_vcpu_affinity(host_irq
, NULL
);
12183 "failed to back to remapped mode, irq: %u\n",
12191 vcpu_info
.pi_desc_addr
= __pa(vcpu_to_pi_desc(vcpu
));
12192 vcpu_info
.vector
= irq
.vector
;
12194 trace_kvm_pi_irte_update(vcpu
->vcpu_id
, host_irq
, e
->gsi
,
12195 vcpu_info
.vector
, vcpu_info
.pi_desc_addr
, set
);
12198 ret
= irq_set_vcpu_affinity(host_irq
, &vcpu_info
);
12200 ret
= irq_set_vcpu_affinity(host_irq
, NULL
);
12203 printk(KERN_INFO
"%s: failed to update PI IRTE\n",
12211 srcu_read_unlock(&kvm
->irq_srcu
, idx
);
12215 static void vmx_setup_mce(struct kvm_vcpu
*vcpu
)
12217 if (vcpu
->arch
.mcg_cap
& MCG_LMCE_P
)
12218 to_vmx(vcpu
)->msr_ia32_feature_control_valid_bits
|=
12219 FEATURE_CONTROL_LMCE
;
12221 to_vmx(vcpu
)->msr_ia32_feature_control_valid_bits
&=
12222 ~FEATURE_CONTROL_LMCE
;
12225 static int vmx_smi_allowed(struct kvm_vcpu
*vcpu
)
12227 /* we need a nested vmexit to enter SMM, postpone if run is pending */
12228 if (to_vmx(vcpu
)->nested
.nested_run_pending
)
12233 static int vmx_pre_enter_smm(struct kvm_vcpu
*vcpu
, char *smstate
)
12235 struct vcpu_vmx
*vmx
= to_vmx(vcpu
);
12237 vmx
->nested
.smm
.guest_mode
= is_guest_mode(vcpu
);
12238 if (vmx
->nested
.smm
.guest_mode
)
12239 nested_vmx_vmexit(vcpu
, -1, 0, 0);
12241 vmx
->nested
.smm
.vmxon
= vmx
->nested
.vmxon
;
12242 vmx
->nested
.vmxon
= false;
12246 static int vmx_pre_leave_smm(struct kvm_vcpu
*vcpu
, u64 smbase
)
12248 struct vcpu_vmx
*vmx
= to_vmx(vcpu
);
12251 if (vmx
->nested
.smm
.vmxon
) {
12252 vmx
->nested
.vmxon
= true;
12253 vmx
->nested
.smm
.vmxon
= false;
12256 if (vmx
->nested
.smm
.guest_mode
) {
12257 vcpu
->arch
.hflags
&= ~HF_SMM_MASK
;
12258 ret
= enter_vmx_non_root_mode(vcpu
, false);
12259 vcpu
->arch
.hflags
|= HF_SMM_MASK
;
12263 vmx
->nested
.smm
.guest_mode
= false;
12268 static int enable_smi_window(struct kvm_vcpu
*vcpu
)
12273 static struct kvm_x86_ops vmx_x86_ops __ro_after_init
= {
12274 .cpu_has_kvm_support
= cpu_has_kvm_support
,
12275 .disabled_by_bios
= vmx_disabled_by_bios
,
12276 .hardware_setup
= hardware_setup
,
12277 .hardware_unsetup
= hardware_unsetup
,
12278 .check_processor_compatibility
= vmx_check_processor_compat
,
12279 .hardware_enable
= hardware_enable
,
12280 .hardware_disable
= hardware_disable
,
12281 .cpu_has_accelerated_tpr
= report_flexpriority
,
12282 .cpu_has_high_real_mode_segbase
= vmx_has_high_real_mode_segbase
,
12284 .vcpu_create
= vmx_create_vcpu
,
12285 .vcpu_free
= vmx_free_vcpu
,
12286 .vcpu_reset
= vmx_vcpu_reset
,
12288 .prepare_guest_switch
= vmx_save_host_state
,
12289 .vcpu_load
= vmx_vcpu_load
,
12290 .vcpu_put
= vmx_vcpu_put
,
12292 .update_bp_intercept
= update_exception_bitmap
,
12293 .get_msr
= vmx_get_msr
,
12294 .set_msr
= vmx_set_msr
,
12295 .get_segment_base
= vmx_get_segment_base
,
12296 .get_segment
= vmx_get_segment
,
12297 .set_segment
= vmx_set_segment
,
12298 .get_cpl
= vmx_get_cpl
,
12299 .get_cs_db_l_bits
= vmx_get_cs_db_l_bits
,
12300 .decache_cr0_guest_bits
= vmx_decache_cr0_guest_bits
,
12301 .decache_cr3
= vmx_decache_cr3
,
12302 .decache_cr4_guest_bits
= vmx_decache_cr4_guest_bits
,
12303 .set_cr0
= vmx_set_cr0
,
12304 .set_cr3
= vmx_set_cr3
,
12305 .set_cr4
= vmx_set_cr4
,
12306 .set_efer
= vmx_set_efer
,
12307 .get_idt
= vmx_get_idt
,
12308 .set_idt
= vmx_set_idt
,
12309 .get_gdt
= vmx_get_gdt
,
12310 .set_gdt
= vmx_set_gdt
,
12311 .get_dr6
= vmx_get_dr6
,
12312 .set_dr6
= vmx_set_dr6
,
12313 .set_dr7
= vmx_set_dr7
,
12314 .sync_dirty_debug_regs
= vmx_sync_dirty_debug_regs
,
12315 .cache_reg
= vmx_cache_reg
,
12316 .get_rflags
= vmx_get_rflags
,
12317 .set_rflags
= vmx_set_rflags
,
12319 .tlb_flush
= vmx_flush_tlb
,
12321 .run
= vmx_vcpu_run
,
12322 .handle_exit
= vmx_handle_exit
,
12323 .skip_emulated_instruction
= skip_emulated_instruction
,
12324 .set_interrupt_shadow
= vmx_set_interrupt_shadow
,
12325 .get_interrupt_shadow
= vmx_get_interrupt_shadow
,
12326 .patch_hypercall
= vmx_patch_hypercall
,
12327 .set_irq
= vmx_inject_irq
,
12328 .set_nmi
= vmx_inject_nmi
,
12329 .queue_exception
= vmx_queue_exception
,
12330 .cancel_injection
= vmx_cancel_injection
,
12331 .interrupt_allowed
= vmx_interrupt_allowed
,
12332 .nmi_allowed
= vmx_nmi_allowed
,
12333 .get_nmi_mask
= vmx_get_nmi_mask
,
12334 .set_nmi_mask
= vmx_set_nmi_mask
,
12335 .enable_nmi_window
= enable_nmi_window
,
12336 .enable_irq_window
= enable_irq_window
,
12337 .update_cr8_intercept
= update_cr8_intercept
,
12338 .set_virtual_x2apic_mode
= vmx_set_virtual_x2apic_mode
,
12339 .set_apic_access_page_addr
= vmx_set_apic_access_page_addr
,
12340 .get_enable_apicv
= vmx_get_enable_apicv
,
12341 .refresh_apicv_exec_ctrl
= vmx_refresh_apicv_exec_ctrl
,
12342 .load_eoi_exitmap
= vmx_load_eoi_exitmap
,
12343 .apicv_post_state_restore
= vmx_apicv_post_state_restore
,
12344 .hwapic_irr_update
= vmx_hwapic_irr_update
,
12345 .hwapic_isr_update
= vmx_hwapic_isr_update
,
12346 .sync_pir_to_irr
= vmx_sync_pir_to_irr
,
12347 .deliver_posted_interrupt
= vmx_deliver_posted_interrupt
,
12349 .set_tss_addr
= vmx_set_tss_addr
,
12350 .get_tdp_level
= get_ept_level
,
12351 .get_mt_mask
= vmx_get_mt_mask
,
12353 .get_exit_info
= vmx_get_exit_info
,
12355 .get_lpage_level
= vmx_get_lpage_level
,
12357 .cpuid_update
= vmx_cpuid_update
,
12359 .rdtscp_supported
= vmx_rdtscp_supported
,
12360 .invpcid_supported
= vmx_invpcid_supported
,
12362 .set_supported_cpuid
= vmx_set_supported_cpuid
,
12364 .has_wbinvd_exit
= cpu_has_vmx_wbinvd_exit
,
12366 .write_tsc_offset
= vmx_write_tsc_offset
,
12368 .set_tdp_cr3
= vmx_set_cr3
,
12370 .check_intercept
= vmx_check_intercept
,
12371 .handle_external_intr
= vmx_handle_external_intr
,
12372 .mpx_supported
= vmx_mpx_supported
,
12373 .xsaves_supported
= vmx_xsaves_supported
,
12374 .umip_emulated
= vmx_umip_emulated
,
12376 .check_nested_events
= vmx_check_nested_events
,
12378 .sched_in
= vmx_sched_in
,
12380 .slot_enable_log_dirty
= vmx_slot_enable_log_dirty
,
12381 .slot_disable_log_dirty
= vmx_slot_disable_log_dirty
,
12382 .flush_log_dirty
= vmx_flush_log_dirty
,
12383 .enable_log_dirty_pt_masked
= vmx_enable_log_dirty_pt_masked
,
12384 .write_log_dirty
= vmx_write_pml_buffer
,
12386 .pre_block
= vmx_pre_block
,
12387 .post_block
= vmx_post_block
,
12389 .pmu_ops
= &intel_pmu_ops
,
12391 .update_pi_irte
= vmx_update_pi_irte
,
12393 #ifdef CONFIG_X86_64
12394 .set_hv_timer
= vmx_set_hv_timer
,
12395 .cancel_hv_timer
= vmx_cancel_hv_timer
,
12398 .setup_mce
= vmx_setup_mce
,
12400 .smi_allowed
= vmx_smi_allowed
,
12401 .pre_enter_smm
= vmx_pre_enter_smm
,
12402 .pre_leave_smm
= vmx_pre_leave_smm
,
12403 .enable_smi_window
= enable_smi_window
,
12406 static int __init
vmx_init(void)
12408 int r
= kvm_init(&vmx_x86_ops
, sizeof(struct vcpu_vmx
),
12409 __alignof__(struct vcpu_vmx
), THIS_MODULE
);
12413 #ifdef CONFIG_KEXEC_CORE
12414 rcu_assign_pointer(crash_vmclear_loaded_vmcss
,
12415 crash_vmclear_local_loaded_vmcss
);
12421 static void __exit
vmx_exit(void)
12423 #ifdef CONFIG_KEXEC_CORE
12424 RCU_INIT_POINTER(crash_vmclear_loaded_vmcss
, NULL
);
12431 module_init(vmx_init
)
12432 module_exit(vmx_exit
)