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/spec-ctrl.h>
55 #include <asm/mshyperv.h>
59 #include "vmx_evmcs.h"
61 #define __ex(x) __kvm_handle_fault_on_reboot(x)
62 #define __ex_clear(x, reg) \
63 ____kvm_handle_fault_on_reboot(x, "xor " reg " , " reg)
65 MODULE_AUTHOR("Qumranet");
66 MODULE_LICENSE("GPL");
68 static const struct x86_cpu_id vmx_cpu_id[] = {
69 X86_FEATURE_MATCH(X86_FEATURE_VMX),
72 MODULE_DEVICE_TABLE(x86cpu, vmx_cpu_id);
74 static bool __read_mostly enable_vpid = 1;
75 module_param_named(vpid, enable_vpid, bool, 0444);
77 static bool __read_mostly enable_vnmi = 1;
78 module_param_named(vnmi, enable_vnmi, bool, S_IRUGO);
80 static bool __read_mostly flexpriority_enabled = 1;
81 module_param_named(flexpriority, flexpriority_enabled, bool, S_IRUGO);
83 static bool __read_mostly enable_ept = 1;
84 module_param_named(ept, enable_ept, bool, S_IRUGO);
86 static bool __read_mostly enable_unrestricted_guest = 1;
87 module_param_named(unrestricted_guest,
88 enable_unrestricted_guest, bool, S_IRUGO);
90 static bool __read_mostly enable_ept_ad_bits = 1;
91 module_param_named(eptad, enable_ept_ad_bits, bool, S_IRUGO);
93 static bool __read_mostly emulate_invalid_guest_state = true;
94 module_param(emulate_invalid_guest_state, bool, S_IRUGO);
96 static bool __read_mostly fasteoi = 1;
97 module_param(fasteoi, bool, S_IRUGO);
99 static bool __read_mostly enable_apicv = 1;
100 module_param(enable_apicv, bool, S_IRUGO);
102 static bool __read_mostly enable_shadow_vmcs = 1;
103 module_param_named(enable_shadow_vmcs, enable_shadow_vmcs, bool, S_IRUGO);
105 * If nested=1, nested virtualization is supported, i.e., guests may use
106 * VMX and be a hypervisor for its own guests. If nested=0, guests may not
107 * use VMX instructions.
109 static bool __read_mostly nested = 0;
110 module_param(nested, bool, S_IRUGO);
112 static u64 __read_mostly host_xss;
114 static bool __read_mostly enable_pml = 1;
115 module_param_named(pml, enable_pml, bool, S_IRUGO);
119 #define MSR_TYPE_RW 3
121 #define MSR_BITMAP_MODE_X2APIC 1
122 #define MSR_BITMAP_MODE_X2APIC_APICV 2
123 #define MSR_BITMAP_MODE_LM 4
125 #define KVM_VMX_TSC_MULTIPLIER_MAX 0xffffffffffffffffULL
127 /* Guest_tsc -> host_tsc conversion requires 64-bit division. */
128 static int __read_mostly cpu_preemption_timer_multi;
129 static bool __read_mostly enable_preemption_timer = 1;
131 module_param_named(preemption_timer, enable_preemption_timer, bool, S_IRUGO);
134 #define KVM_GUEST_CR0_MASK (X86_CR0_NW | X86_CR0_CD)
135 #define KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST X86_CR0_NE
136 #define KVM_VM_CR0_ALWAYS_ON \
137 (KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST | \
138 X86_CR0_WP | X86_CR0_PG | X86_CR0_PE)
139 #define KVM_CR4_GUEST_OWNED_BITS \
140 (X86_CR4_PVI | X86_CR4_DE | X86_CR4_PCE | X86_CR4_OSFXSR \
141 | X86_CR4_OSXMMEXCPT | X86_CR4_LA57 | X86_CR4_TSD)
143 #define KVM_VM_CR4_ALWAYS_ON_UNRESTRICTED_GUEST X86_CR4_VMXE
144 #define KVM_PMODE_VM_CR4_ALWAYS_ON (X86_CR4_PAE | X86_CR4_VMXE)
145 #define KVM_RMODE_VM_CR4_ALWAYS_ON (X86_CR4_VME | X86_CR4_PAE | X86_CR4_VMXE)
147 #define RMODE_GUEST_OWNED_EFLAGS_BITS (~(X86_EFLAGS_IOPL | X86_EFLAGS_VM))
149 #define VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE 5
152 * Hyper-V requires all of these, so mark them as supported even though
153 * they are just treated the same as all-context.
155 #define VMX_VPID_EXTENT_SUPPORTED_MASK \
156 (VMX_VPID_EXTENT_INDIVIDUAL_ADDR_BIT | \
157 VMX_VPID_EXTENT_SINGLE_CONTEXT_BIT | \
158 VMX_VPID_EXTENT_GLOBAL_CONTEXT_BIT | \
159 VMX_VPID_EXTENT_SINGLE_NON_GLOBAL_BIT)
162 * These 2 parameters are used to config the controls for Pause-Loop Exiting:
163 * ple_gap: upper bound on the amount of time between two successive
164 * executions of PAUSE in a loop. Also indicate if ple enabled.
165 * According to test, this time is usually smaller than 128 cycles.
166 * ple_window: upper bound on the amount of time a guest is allowed to execute
167 * in a PAUSE loop. Tests indicate that most spinlocks are held for
168 * less than 2^12 cycles
169 * Time is measured based on a counter that runs at the same rate as the TSC,
170 * refer SDM volume 3b section 21.6.13 & 22.1.3.
172 static unsigned int ple_gap = KVM_DEFAULT_PLE_GAP;
174 static unsigned int ple_window = KVM_VMX_DEFAULT_PLE_WINDOW;
175 module_param(ple_window, uint, 0444);
177 /* Default doubles per-vcpu window every exit. */
178 static unsigned int ple_window_grow = KVM_DEFAULT_PLE_WINDOW_GROW;
179 module_param(ple_window_grow, uint, 0444);
181 /* Default resets per-vcpu window every exit to ple_window. */
182 static unsigned int ple_window_shrink = KVM_DEFAULT_PLE_WINDOW_SHRINK;
183 module_param(ple_window_shrink, uint, 0444);
185 /* Default is to compute the maximum so we can never overflow. */
186 static unsigned int ple_window_max = KVM_VMX_DEFAULT_PLE_WINDOW_MAX;
187 module_param(ple_window_max, uint, 0444);
189 extern const ulong vmx_return;
194 unsigned int tss_addr;
195 bool ept_identity_pagetable_done;
196 gpa_t ept_identity_map_addr;
199 #define NR_AUTOLOAD_MSRS 8
208 * Track a VMCS that may be loaded on a certain CPU. If it is (cpu!=-1), also
209 * remember whether it was VMLAUNCHed, and maintain a linked list of all VMCSs
210 * loaded on this CPU (so we can clear them if the CPU goes down).
214 struct vmcs *shadow_vmcs;
217 bool nmi_known_unmasked;
218 unsigned long vmcs_host_cr3; /* May not match real cr3 */
219 unsigned long vmcs_host_cr4; /* May not match real cr4 */
220 /* Support for vnmi-less CPUs */
221 int soft_vnmi_blocked;
223 s64 vnmi_blocked_time;
224 unsigned long *msr_bitmap;
225 struct list_head loaded_vmcss_on_cpu_link;
228 struct shared_msr_entry {
235 * struct vmcs12 describes the state that our guest hypervisor (L1) keeps for a
236 * single nested guest (L2), hence the name vmcs12. Any VMX implementation has
237 * a VMCS structure, and vmcs12 is our emulated VMX's VMCS. This structure is
238 * stored in guest memory specified by VMPTRLD, but is opaque to the guest,
239 * which must access it using VMREAD/VMWRITE/VMCLEAR instructions.
240 * More than one of these structures may exist, if L1 runs multiple L2 guests.
241 * nested_vmx_run() will use the data here to build the vmcs02: a VMCS for the
242 * underlying hardware which will be used to run L2.
243 * This structure is packed to ensure that its layout is identical across
244 * machines (necessary for live migration).
246 * IMPORTANT: Changing the layout of existing fields in this structure
247 * will break save/restore compatibility with older kvm releases. When
248 * adding new fields, either use space in the reserved padding* arrays
249 * or add the new fields to the end of the structure.
251 typedef u64 natural_width;
252 struct __packed vmcs12 {
253 /* According to the Intel spec, a VMCS region must start with the
254 * following two fields. Then follow implementation-specific data.
259 u32 launch_state; /* set to 0 by VMCLEAR, to 1 by VMLAUNCH */
260 u32 padding[7]; /* room for future expansion */
265 u64 vm_exit_msr_store_addr;
266 u64 vm_exit_msr_load_addr;
267 u64 vm_entry_msr_load_addr;
269 u64 virtual_apic_page_addr;
270 u64 apic_access_addr;
271 u64 posted_intr_desc_addr;
273 u64 eoi_exit_bitmap0;
274 u64 eoi_exit_bitmap1;
275 u64 eoi_exit_bitmap2;
276 u64 eoi_exit_bitmap3;
278 u64 guest_physical_address;
279 u64 vmcs_link_pointer;
280 u64 guest_ia32_debugctl;
283 u64 guest_ia32_perf_global_ctrl;
291 u64 host_ia32_perf_global_ctrl;
294 u64 vm_function_control;
295 u64 eptp_list_address;
297 u64 padding64[3]; /* room for future expansion */
299 * To allow migration of L1 (complete with its L2 guests) between
300 * machines of different natural widths (32 or 64 bit), we cannot have
301 * unsigned long fields with no explict size. We use u64 (aliased
302 * natural_width) instead. Luckily, x86 is little-endian.
304 natural_width cr0_guest_host_mask;
305 natural_width cr4_guest_host_mask;
306 natural_width cr0_read_shadow;
307 natural_width cr4_read_shadow;
308 natural_width cr3_target_value0;
309 natural_width cr3_target_value1;
310 natural_width cr3_target_value2;
311 natural_width cr3_target_value3;
312 natural_width exit_qualification;
313 natural_width guest_linear_address;
314 natural_width guest_cr0;
315 natural_width guest_cr3;
316 natural_width guest_cr4;
317 natural_width guest_es_base;
318 natural_width guest_cs_base;
319 natural_width guest_ss_base;
320 natural_width guest_ds_base;
321 natural_width guest_fs_base;
322 natural_width guest_gs_base;
323 natural_width guest_ldtr_base;
324 natural_width guest_tr_base;
325 natural_width guest_gdtr_base;
326 natural_width guest_idtr_base;
327 natural_width guest_dr7;
328 natural_width guest_rsp;
329 natural_width guest_rip;
330 natural_width guest_rflags;
331 natural_width guest_pending_dbg_exceptions;
332 natural_width guest_sysenter_esp;
333 natural_width guest_sysenter_eip;
334 natural_width host_cr0;
335 natural_width host_cr3;
336 natural_width host_cr4;
337 natural_width host_fs_base;
338 natural_width host_gs_base;
339 natural_width host_tr_base;
340 natural_width host_gdtr_base;
341 natural_width host_idtr_base;
342 natural_width host_ia32_sysenter_esp;
343 natural_width host_ia32_sysenter_eip;
344 natural_width host_rsp;
345 natural_width host_rip;
346 natural_width paddingl[8]; /* room for future expansion */
347 u32 pin_based_vm_exec_control;
348 u32 cpu_based_vm_exec_control;
349 u32 exception_bitmap;
350 u32 page_fault_error_code_mask;
351 u32 page_fault_error_code_match;
352 u32 cr3_target_count;
353 u32 vm_exit_controls;
354 u32 vm_exit_msr_store_count;
355 u32 vm_exit_msr_load_count;
356 u32 vm_entry_controls;
357 u32 vm_entry_msr_load_count;
358 u32 vm_entry_intr_info_field;
359 u32 vm_entry_exception_error_code;
360 u32 vm_entry_instruction_len;
362 u32 secondary_vm_exec_control;
363 u32 vm_instruction_error;
365 u32 vm_exit_intr_info;
366 u32 vm_exit_intr_error_code;
367 u32 idt_vectoring_info_field;
368 u32 idt_vectoring_error_code;
369 u32 vm_exit_instruction_len;
370 u32 vmx_instruction_info;
377 u32 guest_ldtr_limit;
379 u32 guest_gdtr_limit;
380 u32 guest_idtr_limit;
381 u32 guest_es_ar_bytes;
382 u32 guest_cs_ar_bytes;
383 u32 guest_ss_ar_bytes;
384 u32 guest_ds_ar_bytes;
385 u32 guest_fs_ar_bytes;
386 u32 guest_gs_ar_bytes;
387 u32 guest_ldtr_ar_bytes;
388 u32 guest_tr_ar_bytes;
389 u32 guest_interruptibility_info;
390 u32 guest_activity_state;
391 u32 guest_sysenter_cs;
392 u32 host_ia32_sysenter_cs;
393 u32 vmx_preemption_timer_value;
394 u32 padding32[7]; /* room for future expansion */
395 u16 virtual_processor_id;
397 u16 guest_es_selector;
398 u16 guest_cs_selector;
399 u16 guest_ss_selector;
400 u16 guest_ds_selector;
401 u16 guest_fs_selector;
402 u16 guest_gs_selector;
403 u16 guest_ldtr_selector;
404 u16 guest_tr_selector;
405 u16 guest_intr_status;
406 u16 host_es_selector;
407 u16 host_cs_selector;
408 u16 host_ss_selector;
409 u16 host_ds_selector;
410 u16 host_fs_selector;
411 u16 host_gs_selector;
412 u16 host_tr_selector;
417 * For save/restore compatibility, the vmcs12 field offsets must not change.
419 #define CHECK_OFFSET(field, loc) \
420 BUILD_BUG_ON_MSG(offsetof(struct vmcs12, field) != (loc), \
421 "Offset of " #field " in struct vmcs12 has changed.")
423 static inline void vmx_check_vmcs12_offsets(void) {
424 CHECK_OFFSET(revision_id, 0);
425 CHECK_OFFSET(abort, 4);
426 CHECK_OFFSET(launch_state, 8);
427 CHECK_OFFSET(io_bitmap_a, 40);
428 CHECK_OFFSET(io_bitmap_b, 48);
429 CHECK_OFFSET(msr_bitmap, 56);
430 CHECK_OFFSET(vm_exit_msr_store_addr, 64);
431 CHECK_OFFSET(vm_exit_msr_load_addr, 72);
432 CHECK_OFFSET(vm_entry_msr_load_addr, 80);
433 CHECK_OFFSET(tsc_offset, 88);
434 CHECK_OFFSET(virtual_apic_page_addr, 96);
435 CHECK_OFFSET(apic_access_addr, 104);
436 CHECK_OFFSET(posted_intr_desc_addr, 112);
437 CHECK_OFFSET(ept_pointer, 120);
438 CHECK_OFFSET(eoi_exit_bitmap0, 128);
439 CHECK_OFFSET(eoi_exit_bitmap1, 136);
440 CHECK_OFFSET(eoi_exit_bitmap2, 144);
441 CHECK_OFFSET(eoi_exit_bitmap3, 152);
442 CHECK_OFFSET(xss_exit_bitmap, 160);
443 CHECK_OFFSET(guest_physical_address, 168);
444 CHECK_OFFSET(vmcs_link_pointer, 176);
445 CHECK_OFFSET(guest_ia32_debugctl, 184);
446 CHECK_OFFSET(guest_ia32_pat, 192);
447 CHECK_OFFSET(guest_ia32_efer, 200);
448 CHECK_OFFSET(guest_ia32_perf_global_ctrl, 208);
449 CHECK_OFFSET(guest_pdptr0, 216);
450 CHECK_OFFSET(guest_pdptr1, 224);
451 CHECK_OFFSET(guest_pdptr2, 232);
452 CHECK_OFFSET(guest_pdptr3, 240);
453 CHECK_OFFSET(guest_bndcfgs, 248);
454 CHECK_OFFSET(host_ia32_pat, 256);
455 CHECK_OFFSET(host_ia32_efer, 264);
456 CHECK_OFFSET(host_ia32_perf_global_ctrl, 272);
457 CHECK_OFFSET(vmread_bitmap, 280);
458 CHECK_OFFSET(vmwrite_bitmap, 288);
459 CHECK_OFFSET(vm_function_control, 296);
460 CHECK_OFFSET(eptp_list_address, 304);
461 CHECK_OFFSET(pml_address, 312);
462 CHECK_OFFSET(cr0_guest_host_mask, 344);
463 CHECK_OFFSET(cr4_guest_host_mask, 352);
464 CHECK_OFFSET(cr0_read_shadow, 360);
465 CHECK_OFFSET(cr4_read_shadow, 368);
466 CHECK_OFFSET(cr3_target_value0, 376);
467 CHECK_OFFSET(cr3_target_value1, 384);
468 CHECK_OFFSET(cr3_target_value2, 392);
469 CHECK_OFFSET(cr3_target_value3, 400);
470 CHECK_OFFSET(exit_qualification, 408);
471 CHECK_OFFSET(guest_linear_address, 416);
472 CHECK_OFFSET(guest_cr0, 424);
473 CHECK_OFFSET(guest_cr3, 432);
474 CHECK_OFFSET(guest_cr4, 440);
475 CHECK_OFFSET(guest_es_base, 448);
476 CHECK_OFFSET(guest_cs_base, 456);
477 CHECK_OFFSET(guest_ss_base, 464);
478 CHECK_OFFSET(guest_ds_base, 472);
479 CHECK_OFFSET(guest_fs_base, 480);
480 CHECK_OFFSET(guest_gs_base, 488);
481 CHECK_OFFSET(guest_ldtr_base, 496);
482 CHECK_OFFSET(guest_tr_base, 504);
483 CHECK_OFFSET(guest_gdtr_base, 512);
484 CHECK_OFFSET(guest_idtr_base, 520);
485 CHECK_OFFSET(guest_dr7, 528);
486 CHECK_OFFSET(guest_rsp, 536);
487 CHECK_OFFSET(guest_rip, 544);
488 CHECK_OFFSET(guest_rflags, 552);
489 CHECK_OFFSET(guest_pending_dbg_exceptions, 560);
490 CHECK_OFFSET(guest_sysenter_esp, 568);
491 CHECK_OFFSET(guest_sysenter_eip, 576);
492 CHECK_OFFSET(host_cr0, 584);
493 CHECK_OFFSET(host_cr3, 592);
494 CHECK_OFFSET(host_cr4, 600);
495 CHECK_OFFSET(host_fs_base, 608);
496 CHECK_OFFSET(host_gs_base, 616);
497 CHECK_OFFSET(host_tr_base, 624);
498 CHECK_OFFSET(host_gdtr_base, 632);
499 CHECK_OFFSET(host_idtr_base, 640);
500 CHECK_OFFSET(host_ia32_sysenter_esp, 648);
501 CHECK_OFFSET(host_ia32_sysenter_eip, 656);
502 CHECK_OFFSET(host_rsp, 664);
503 CHECK_OFFSET(host_rip, 672);
504 CHECK_OFFSET(pin_based_vm_exec_control, 744);
505 CHECK_OFFSET(cpu_based_vm_exec_control, 748);
506 CHECK_OFFSET(exception_bitmap, 752);
507 CHECK_OFFSET(page_fault_error_code_mask, 756);
508 CHECK_OFFSET(page_fault_error_code_match, 760);
509 CHECK_OFFSET(cr3_target_count, 764);
510 CHECK_OFFSET(vm_exit_controls, 768);
511 CHECK_OFFSET(vm_exit_msr_store_count, 772);
512 CHECK_OFFSET(vm_exit_msr_load_count, 776);
513 CHECK_OFFSET(vm_entry_controls, 780);
514 CHECK_OFFSET(vm_entry_msr_load_count, 784);
515 CHECK_OFFSET(vm_entry_intr_info_field, 788);
516 CHECK_OFFSET(vm_entry_exception_error_code, 792);
517 CHECK_OFFSET(vm_entry_instruction_len, 796);
518 CHECK_OFFSET(tpr_threshold, 800);
519 CHECK_OFFSET(secondary_vm_exec_control, 804);
520 CHECK_OFFSET(vm_instruction_error, 808);
521 CHECK_OFFSET(vm_exit_reason, 812);
522 CHECK_OFFSET(vm_exit_intr_info, 816);
523 CHECK_OFFSET(vm_exit_intr_error_code, 820);
524 CHECK_OFFSET(idt_vectoring_info_field, 824);
525 CHECK_OFFSET(idt_vectoring_error_code, 828);
526 CHECK_OFFSET(vm_exit_instruction_len, 832);
527 CHECK_OFFSET(vmx_instruction_info, 836);
528 CHECK_OFFSET(guest_es_limit, 840);
529 CHECK_OFFSET(guest_cs_limit, 844);
530 CHECK_OFFSET(guest_ss_limit, 848);
531 CHECK_OFFSET(guest_ds_limit, 852);
532 CHECK_OFFSET(guest_fs_limit, 856);
533 CHECK_OFFSET(guest_gs_limit, 860);
534 CHECK_OFFSET(guest_ldtr_limit, 864);
535 CHECK_OFFSET(guest_tr_limit, 868);
536 CHECK_OFFSET(guest_gdtr_limit, 872);
537 CHECK_OFFSET(guest_idtr_limit, 876);
538 CHECK_OFFSET(guest_es_ar_bytes, 880);
539 CHECK_OFFSET(guest_cs_ar_bytes, 884);
540 CHECK_OFFSET(guest_ss_ar_bytes, 888);
541 CHECK_OFFSET(guest_ds_ar_bytes, 892);
542 CHECK_OFFSET(guest_fs_ar_bytes, 896);
543 CHECK_OFFSET(guest_gs_ar_bytes, 900);
544 CHECK_OFFSET(guest_ldtr_ar_bytes, 904);
545 CHECK_OFFSET(guest_tr_ar_bytes, 908);
546 CHECK_OFFSET(guest_interruptibility_info, 912);
547 CHECK_OFFSET(guest_activity_state, 916);
548 CHECK_OFFSET(guest_sysenter_cs, 920);
549 CHECK_OFFSET(host_ia32_sysenter_cs, 924);
550 CHECK_OFFSET(vmx_preemption_timer_value, 928);
551 CHECK_OFFSET(virtual_processor_id, 960);
552 CHECK_OFFSET(posted_intr_nv, 962);
553 CHECK_OFFSET(guest_es_selector, 964);
554 CHECK_OFFSET(guest_cs_selector, 966);
555 CHECK_OFFSET(guest_ss_selector, 968);
556 CHECK_OFFSET(guest_ds_selector, 970);
557 CHECK_OFFSET(guest_fs_selector, 972);
558 CHECK_OFFSET(guest_gs_selector, 974);
559 CHECK_OFFSET(guest_ldtr_selector, 976);
560 CHECK_OFFSET(guest_tr_selector, 978);
561 CHECK_OFFSET(guest_intr_status, 980);
562 CHECK_OFFSET(host_es_selector, 982);
563 CHECK_OFFSET(host_cs_selector, 984);
564 CHECK_OFFSET(host_ss_selector, 986);
565 CHECK_OFFSET(host_ds_selector, 988);
566 CHECK_OFFSET(host_fs_selector, 990);
567 CHECK_OFFSET(host_gs_selector, 992);
568 CHECK_OFFSET(host_tr_selector, 994);
569 CHECK_OFFSET(guest_pml_index, 996);
573 * VMCS12_REVISION is an arbitrary id that should be changed if the content or
574 * layout of struct vmcs12 is changed. MSR_IA32_VMX_BASIC returns this id, and
575 * VMPTRLD verifies that the VMCS region that L1 is loading contains this id.
577 * IMPORTANT: Changing this value will break save/restore compatibility with
578 * older kvm releases.
580 #define VMCS12_REVISION 0x11e57ed0
583 * VMCS12_SIZE is the number of bytes L1 should allocate for the VMXON region
584 * and any VMCS region. Although only sizeof(struct vmcs12) are used by the
585 * current implementation, 4K are reserved to avoid future complications.
587 #define VMCS12_SIZE 0x1000
590 * VMCS12_MAX_FIELD_INDEX is the highest index value used in any
591 * supported VMCS12 field encoding.
593 #define VMCS12_MAX_FIELD_INDEX 0x17
595 struct nested_vmx_msrs {
597 * We only store the "true" versions of the VMX capability MSRs. We
598 * generate the "non-true" versions by setting the must-be-1 bits
599 * according to the SDM.
601 u32 procbased_ctls_low;
602 u32 procbased_ctls_high;
603 u32 secondary_ctls_low;
604 u32 secondary_ctls_high;
605 u32 pinbased_ctls_low;
606 u32 pinbased_ctls_high;
625 * The nested_vmx structure is part of vcpu_vmx, and holds information we need
626 * for correct emulation of VMX (i.e., nested VMX) on this vcpu.
629 /* Has the level1 guest done vmxon? */
634 /* The guest-physical address of the current VMCS L1 keeps for L2 */
637 * Cache of the guest's VMCS, existing outside of guest memory.
638 * Loaded from guest memory during VMPTRLD. Flushed to guest
639 * memory during VMCLEAR and VMPTRLD.
641 struct vmcs12 *cached_vmcs12;
643 * Indicates if the shadow vmcs must be updated with the
644 * data hold by vmcs12
646 bool sync_shadow_vmcs;
649 bool change_vmcs01_virtual_apic_mode;
651 /* L2 must run next, and mustn't decide to exit to L1. */
652 bool nested_run_pending;
654 struct loaded_vmcs vmcs02;
657 * Guest pages referred to in the vmcs02 with host-physical
658 * pointers, so we must keep them pinned while L2 runs.
660 struct page *apic_access_page;
661 struct page *virtual_apic_page;
662 struct page *pi_desc_page;
663 struct pi_desc *pi_desc;
667 struct hrtimer preemption_timer;
668 bool preemption_timer_expired;
670 /* to migrate it to L2 if VM_ENTRY_LOAD_DEBUG_CONTROLS is off */
676 struct nested_vmx_msrs msrs;
678 /* SMM related state */
680 /* in VMX operation on SMM entry? */
682 /* in guest mode on SMM entry? */
687 #define POSTED_INTR_ON 0
688 #define POSTED_INTR_SN 1
690 /* Posted-Interrupt Descriptor */
692 u32 pir[8]; /* Posted interrupt requested */
695 /* bit 256 - Outstanding Notification */
697 /* bit 257 - Suppress Notification */
699 /* bit 271:258 - Reserved */
701 /* bit 279:272 - Notification Vector */
703 /* bit 287:280 - Reserved */
705 /* bit 319:288 - Notification Destination */
713 static bool pi_test_and_set_on(struct pi_desc *pi_desc)
715 return test_and_set_bit(POSTED_INTR_ON,
716 (unsigned long *)&pi_desc->control);
719 static bool pi_test_and_clear_on(struct pi_desc *pi_desc)
721 return test_and_clear_bit(POSTED_INTR_ON,
722 (unsigned long *)&pi_desc->control);
725 static int pi_test_and_set_pir(int vector, struct pi_desc *pi_desc)
727 return test_and_set_bit(vector, (unsigned long *)pi_desc->pir);
730 static inline void pi_clear_sn(struct pi_desc *pi_desc)
732 return clear_bit(POSTED_INTR_SN,
733 (unsigned long *)&pi_desc->control);
736 static inline void pi_set_sn(struct pi_desc *pi_desc)
738 return set_bit(POSTED_INTR_SN,
739 (unsigned long *)&pi_desc->control);
742 static inline void pi_clear_on(struct pi_desc *pi_desc)
744 clear_bit(POSTED_INTR_ON,
745 (unsigned long *)&pi_desc->control);
748 static inline int pi_test_on(struct pi_desc *pi_desc)
750 return test_bit(POSTED_INTR_ON,
751 (unsigned long *)&pi_desc->control);
754 static inline int pi_test_sn(struct pi_desc *pi_desc)
756 return test_bit(POSTED_INTR_SN,
757 (unsigned long *)&pi_desc->control);
761 struct kvm_vcpu vcpu;
762 unsigned long host_rsp;
766 u32 idt_vectoring_info;
768 struct shared_msr_entry *guest_msrs;
771 unsigned long host_idt_base;
773 u64 msr_host_kernel_gs_base;
774 u64 msr_guest_kernel_gs_base;
777 u64 arch_capabilities;
780 u32 vm_entry_controls_shadow;
781 u32 vm_exit_controls_shadow;
782 u32 secondary_exec_control;
785 * loaded_vmcs points to the VMCS currently used in this vcpu. For a
786 * non-nested (L1) guest, it always points to vmcs01. For a nested
787 * guest (L2), it points to a different VMCS.
789 struct loaded_vmcs vmcs01;
790 struct loaded_vmcs *loaded_vmcs;
791 bool __launched; /* temporary, used in vmx_vcpu_run */
792 struct msr_autoload {
794 struct vmx_msr_entry guest[NR_AUTOLOAD_MSRS];
795 struct vmx_msr_entry host[NR_AUTOLOAD_MSRS];
799 u16 fs_sel, gs_sel, ldt_sel;
803 int gs_ldt_reload_needed;
804 int fs_reload_needed;
805 u64 msr_host_bndcfgs;
810 struct kvm_segment segs[8];
813 u32 bitmask; /* 4 bits per segment (1 bit per field) */
814 struct kvm_save_segment {
822 bool emulation_required;
826 /* Posted interrupt descriptor */
827 struct pi_desc pi_desc;
829 /* Support for a guest hypervisor (nested VMX) */
830 struct nested_vmx nested;
832 /* Dynamic PLE window. */
834 bool ple_window_dirty;
836 /* Support for PML */
837 #define PML_ENTITY_NUM 512
840 /* apic deadline value in host tsc */
843 u64 current_tsc_ratio;
847 unsigned long host_debugctlmsr;
850 * Only bits masked by msr_ia32_feature_control_valid_bits can be set in
851 * msr_ia32_feature_control. FEATURE_CONTROL_LOCKED is always included
852 * in msr_ia32_feature_control_valid_bits.
854 u64 msr_ia32_feature_control;
855 u64 msr_ia32_feature_control_valid_bits;
858 enum segment_cache_field {
867 static inline struct kvm_vmx *to_kvm_vmx(struct kvm *kvm)
869 return container_of(kvm, struct kvm_vmx, kvm);
872 static inline struct vcpu_vmx *to_vmx(struct kvm_vcpu *vcpu)
874 return container_of(vcpu, struct vcpu_vmx, vcpu);
877 static struct pi_desc *vcpu_to_pi_desc(struct kvm_vcpu *vcpu)
879 return &(to_vmx(vcpu)->pi_desc);
882 #define ROL16(val, n) ((u16)(((u16)(val) << (n)) | ((u16)(val) >> (16 - (n)))))
883 #define VMCS12_OFFSET(x) offsetof(struct vmcs12, x)
884 #define FIELD(number, name) [ROL16(number, 6)] = VMCS12_OFFSET(name)
885 #define FIELD64(number, name) \
886 FIELD(number, name), \
887 [ROL16(number##_HIGH, 6)] = VMCS12_OFFSET(name) + sizeof(u32)
890 static u16 shadow_read_only_fields[] = {
891 #define SHADOW_FIELD_RO(x) x,
892 #include "vmx_shadow_fields.h"
894 static int max_shadow_read_only_fields =
895 ARRAY_SIZE(shadow_read_only_fields);
897 static u16 shadow_read_write_fields[] = {
898 #define SHADOW_FIELD_RW(x) x,
899 #include "vmx_shadow_fields.h"
901 static int max_shadow_read_write_fields =
902 ARRAY_SIZE(shadow_read_write_fields);
904 static const unsigned short vmcs_field_to_offset_table[] = {
905 FIELD(VIRTUAL_PROCESSOR_ID, virtual_processor_id),
906 FIELD(POSTED_INTR_NV, posted_intr_nv),
907 FIELD(GUEST_ES_SELECTOR, guest_es_selector),
908 FIELD(GUEST_CS_SELECTOR, guest_cs_selector),
909 FIELD(GUEST_SS_SELECTOR, guest_ss_selector),
910 FIELD(GUEST_DS_SELECTOR, guest_ds_selector),
911 FIELD(GUEST_FS_SELECTOR, guest_fs_selector),
912 FIELD(GUEST_GS_SELECTOR, guest_gs_selector),
913 FIELD(GUEST_LDTR_SELECTOR, guest_ldtr_selector),
914 FIELD(GUEST_TR_SELECTOR, guest_tr_selector),
915 FIELD(GUEST_INTR_STATUS, guest_intr_status),
916 FIELD(GUEST_PML_INDEX, guest_pml_index),
917 FIELD(HOST_ES_SELECTOR, host_es_selector),
918 FIELD(HOST_CS_SELECTOR, host_cs_selector),
919 FIELD(HOST_SS_SELECTOR, host_ss_selector),
920 FIELD(HOST_DS_SELECTOR, host_ds_selector),
921 FIELD(HOST_FS_SELECTOR, host_fs_selector),
922 FIELD(HOST_GS_SELECTOR, host_gs_selector),
923 FIELD(HOST_TR_SELECTOR, host_tr_selector),
924 FIELD64(IO_BITMAP_A, io_bitmap_a),
925 FIELD64(IO_BITMAP_B, io_bitmap_b),
926 FIELD64(MSR_BITMAP, msr_bitmap),
927 FIELD64(VM_EXIT_MSR_STORE_ADDR, vm_exit_msr_store_addr),
928 FIELD64(VM_EXIT_MSR_LOAD_ADDR, vm_exit_msr_load_addr),
929 FIELD64(VM_ENTRY_MSR_LOAD_ADDR, vm_entry_msr_load_addr),
930 FIELD64(PML_ADDRESS, pml_address),
931 FIELD64(TSC_OFFSET, tsc_offset),
932 FIELD64(VIRTUAL_APIC_PAGE_ADDR, virtual_apic_page_addr),
933 FIELD64(APIC_ACCESS_ADDR, apic_access_addr),
934 FIELD64(POSTED_INTR_DESC_ADDR, posted_intr_desc_addr),
935 FIELD64(VM_FUNCTION_CONTROL, vm_function_control),
936 FIELD64(EPT_POINTER, ept_pointer),
937 FIELD64(EOI_EXIT_BITMAP0, eoi_exit_bitmap0),
938 FIELD64(EOI_EXIT_BITMAP1, eoi_exit_bitmap1),
939 FIELD64(EOI_EXIT_BITMAP2, eoi_exit_bitmap2),
940 FIELD64(EOI_EXIT_BITMAP3, eoi_exit_bitmap3),
941 FIELD64(EPTP_LIST_ADDRESS, eptp_list_address),
942 FIELD64(VMREAD_BITMAP, vmread_bitmap),
943 FIELD64(VMWRITE_BITMAP, vmwrite_bitmap),
944 FIELD64(XSS_EXIT_BITMAP, xss_exit_bitmap),
945 FIELD64(GUEST_PHYSICAL_ADDRESS, guest_physical_address),
946 FIELD64(VMCS_LINK_POINTER, vmcs_link_pointer),
947 FIELD64(GUEST_IA32_DEBUGCTL, guest_ia32_debugctl),
948 FIELD64(GUEST_IA32_PAT, guest_ia32_pat),
949 FIELD64(GUEST_IA32_EFER, guest_ia32_efer),
950 FIELD64(GUEST_IA32_PERF_GLOBAL_CTRL, guest_ia32_perf_global_ctrl),
951 FIELD64(GUEST_PDPTR0, guest_pdptr0),
952 FIELD64(GUEST_PDPTR1, guest_pdptr1),
953 FIELD64(GUEST_PDPTR2, guest_pdptr2),
954 FIELD64(GUEST_PDPTR3, guest_pdptr3),
955 FIELD64(GUEST_BNDCFGS, guest_bndcfgs),
956 FIELD64(HOST_IA32_PAT, host_ia32_pat),
957 FIELD64(HOST_IA32_EFER, host_ia32_efer),
958 FIELD64(HOST_IA32_PERF_GLOBAL_CTRL, host_ia32_perf_global_ctrl),
959 FIELD(PIN_BASED_VM_EXEC_CONTROL, pin_based_vm_exec_control),
960 FIELD(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control),
961 FIELD(EXCEPTION_BITMAP, exception_bitmap),
962 FIELD(PAGE_FAULT_ERROR_CODE_MASK, page_fault_error_code_mask),
963 FIELD(PAGE_FAULT_ERROR_CODE_MATCH, page_fault_error_code_match),
964 FIELD(CR3_TARGET_COUNT, cr3_target_count),
965 FIELD(VM_EXIT_CONTROLS, vm_exit_controls),
966 FIELD(VM_EXIT_MSR_STORE_COUNT, vm_exit_msr_store_count),
967 FIELD(VM_EXIT_MSR_LOAD_COUNT, vm_exit_msr_load_count),
968 FIELD(VM_ENTRY_CONTROLS, vm_entry_controls),
969 FIELD(VM_ENTRY_MSR_LOAD_COUNT, vm_entry_msr_load_count),
970 FIELD(VM_ENTRY_INTR_INFO_FIELD, vm_entry_intr_info_field),
971 FIELD(VM_ENTRY_EXCEPTION_ERROR_CODE, vm_entry_exception_error_code),
972 FIELD(VM_ENTRY_INSTRUCTION_LEN, vm_entry_instruction_len),
973 FIELD(TPR_THRESHOLD, tpr_threshold),
974 FIELD(SECONDARY_VM_EXEC_CONTROL, secondary_vm_exec_control),
975 FIELD(VM_INSTRUCTION_ERROR, vm_instruction_error),
976 FIELD(VM_EXIT_REASON, vm_exit_reason),
977 FIELD(VM_EXIT_INTR_INFO, vm_exit_intr_info),
978 FIELD(VM_EXIT_INTR_ERROR_CODE, vm_exit_intr_error_code),
979 FIELD(IDT_VECTORING_INFO_FIELD, idt_vectoring_info_field),
980 FIELD(IDT_VECTORING_ERROR_CODE, idt_vectoring_error_code),
981 FIELD(VM_EXIT_INSTRUCTION_LEN, vm_exit_instruction_len),
982 FIELD(VMX_INSTRUCTION_INFO, vmx_instruction_info),
983 FIELD(GUEST_ES_LIMIT, guest_es_limit),
984 FIELD(GUEST_CS_LIMIT, guest_cs_limit),
985 FIELD(GUEST_SS_LIMIT, guest_ss_limit),
986 FIELD(GUEST_DS_LIMIT, guest_ds_limit),
987 FIELD(GUEST_FS_LIMIT, guest_fs_limit),
988 FIELD(GUEST_GS_LIMIT, guest_gs_limit),
989 FIELD(GUEST_LDTR_LIMIT, guest_ldtr_limit),
990 FIELD(GUEST_TR_LIMIT, guest_tr_limit),
991 FIELD(GUEST_GDTR_LIMIT, guest_gdtr_limit),
992 FIELD(GUEST_IDTR_LIMIT, guest_idtr_limit),
993 FIELD(GUEST_ES_AR_BYTES, guest_es_ar_bytes),
994 FIELD(GUEST_CS_AR_BYTES, guest_cs_ar_bytes),
995 FIELD(GUEST_SS_AR_BYTES, guest_ss_ar_bytes),
996 FIELD(GUEST_DS_AR_BYTES, guest_ds_ar_bytes),
997 FIELD(GUEST_FS_AR_BYTES, guest_fs_ar_bytes),
998 FIELD(GUEST_GS_AR_BYTES, guest_gs_ar_bytes),
999 FIELD(GUEST_LDTR_AR_BYTES, guest_ldtr_ar_bytes),
1000 FIELD(GUEST_TR_AR_BYTES, guest_tr_ar_bytes),
1001 FIELD(GUEST_INTERRUPTIBILITY_INFO, guest_interruptibility_info),
1002 FIELD(GUEST_ACTIVITY_STATE, guest_activity_state),
1003 FIELD(GUEST_SYSENTER_CS, guest_sysenter_cs),
1004 FIELD(HOST_IA32_SYSENTER_CS, host_ia32_sysenter_cs),
1005 FIELD(VMX_PREEMPTION_TIMER_VALUE, vmx_preemption_timer_value),
1006 FIELD(CR0_GUEST_HOST_MASK, cr0_guest_host_mask),
1007 FIELD(CR4_GUEST_HOST_MASK, cr4_guest_host_mask),
1008 FIELD(CR0_READ_SHADOW, cr0_read_shadow),
1009 FIELD(CR4_READ_SHADOW, cr4_read_shadow),
1010 FIELD(CR3_TARGET_VALUE0, cr3_target_value0),
1011 FIELD(CR3_TARGET_VALUE1, cr3_target_value1),
1012 FIELD(CR3_TARGET_VALUE2, cr3_target_value2),
1013 FIELD(CR3_TARGET_VALUE3, cr3_target_value3),
1014 FIELD(EXIT_QUALIFICATION, exit_qualification),
1015 FIELD(GUEST_LINEAR_ADDRESS, guest_linear_address),
1016 FIELD(GUEST_CR0, guest_cr0),
1017 FIELD(GUEST_CR3, guest_cr3),
1018 FIELD(GUEST_CR4, guest_cr4),
1019 FIELD(GUEST_ES_BASE, guest_es_base),
1020 FIELD(GUEST_CS_BASE, guest_cs_base),
1021 FIELD(GUEST_SS_BASE, guest_ss_base),
1022 FIELD(GUEST_DS_BASE, guest_ds_base),
1023 FIELD(GUEST_FS_BASE, guest_fs_base),
1024 FIELD(GUEST_GS_BASE, guest_gs_base),
1025 FIELD(GUEST_LDTR_BASE, guest_ldtr_base),
1026 FIELD(GUEST_TR_BASE, guest_tr_base),
1027 FIELD(GUEST_GDTR_BASE, guest_gdtr_base),
1028 FIELD(GUEST_IDTR_BASE, guest_idtr_base),
1029 FIELD(GUEST_DR7, guest_dr7),
1030 FIELD(GUEST_RSP, guest_rsp),
1031 FIELD(GUEST_RIP, guest_rip),
1032 FIELD(GUEST_RFLAGS, guest_rflags),
1033 FIELD(GUEST_PENDING_DBG_EXCEPTIONS, guest_pending_dbg_exceptions),
1034 FIELD(GUEST_SYSENTER_ESP, guest_sysenter_esp),
1035 FIELD(GUEST_SYSENTER_EIP, guest_sysenter_eip),
1036 FIELD(HOST_CR0, host_cr0),
1037 FIELD(HOST_CR3, host_cr3),
1038 FIELD(HOST_CR4, host_cr4),
1039 FIELD(HOST_FS_BASE, host_fs_base),
1040 FIELD(HOST_GS_BASE, host_gs_base),
1041 FIELD(HOST_TR_BASE, host_tr_base),
1042 FIELD(HOST_GDTR_BASE, host_gdtr_base),
1043 FIELD(HOST_IDTR_BASE, host_idtr_base),
1044 FIELD(HOST_IA32_SYSENTER_ESP, host_ia32_sysenter_esp),
1045 FIELD(HOST_IA32_SYSENTER_EIP, host_ia32_sysenter_eip),
1046 FIELD(HOST_RSP, host_rsp),
1047 FIELD(HOST_RIP, host_rip),
1050 static inline short vmcs_field_to_offset(unsigned long field)
1052 const size_t size = ARRAY_SIZE(vmcs_field_to_offset_table);
1053 unsigned short offset;
1059 index = ROL16(field, 6);
1063 index = array_index_nospec(index, size);
1064 offset = vmcs_field_to_offset_table[index];
1070 static inline struct vmcs12 *get_vmcs12(struct kvm_vcpu *vcpu)
1072 return to_vmx(vcpu)->nested.cached_vmcs12;
1075 static bool nested_ept_ad_enabled(struct kvm_vcpu *vcpu);
1076 static unsigned long nested_ept_get_cr3(struct kvm_vcpu *vcpu);
1077 static u64 construct_eptp(struct kvm_vcpu *vcpu, unsigned long root_hpa);
1078 static bool vmx_xsaves_supported(void);
1079 static void vmx_set_segment(struct kvm_vcpu *vcpu,
1080 struct kvm_segment *var, int seg);
1081 static void vmx_get_segment(struct kvm_vcpu *vcpu,
1082 struct kvm_segment *var, int seg);
1083 static bool guest_state_valid(struct kvm_vcpu *vcpu);
1084 static u32 vmx_segment_access_rights(struct kvm_segment *var);
1085 static void copy_shadow_to_vmcs12(struct vcpu_vmx *vmx);
1086 static bool vmx_get_nmi_mask(struct kvm_vcpu *vcpu);
1087 static void vmx_set_nmi_mask(struct kvm_vcpu *vcpu, bool masked);
1088 static bool nested_vmx_is_page_fault_vmexit(struct vmcs12 *vmcs12,
1090 static void vmx_update_msr_bitmap(struct kvm_vcpu *vcpu);
1091 static void __always_inline vmx_disable_intercept_for_msr(unsigned long *msr_bitmap,
1094 static DEFINE_PER_CPU(struct vmcs *, vmxarea);
1095 static DEFINE_PER_CPU(struct vmcs *, current_vmcs);
1097 * We maintain a per-CPU linked-list of VMCS loaded on that CPU. This is needed
1098 * when a CPU is brought down, and we need to VMCLEAR all VMCSs loaded on it.
1100 static DEFINE_PER_CPU(struct list_head, loaded_vmcss_on_cpu);
1103 * We maintian a per-CPU linked-list of vCPU, so in wakeup_handler() we
1104 * can find which vCPU should be waken up.
1106 static DEFINE_PER_CPU(struct list_head, blocked_vcpu_on_cpu);
1107 static DEFINE_PER_CPU(spinlock_t, blocked_vcpu_on_cpu_lock);
1115 static unsigned long *vmx_bitmap[VMX_BITMAP_NR];
1117 #define vmx_vmread_bitmap (vmx_bitmap[VMX_VMREAD_BITMAP])
1118 #define vmx_vmwrite_bitmap (vmx_bitmap[VMX_VMWRITE_BITMAP])
1120 static bool cpu_has_load_ia32_efer;
1121 static bool cpu_has_load_perf_global_ctrl;
1123 static DECLARE_BITMAP(vmx_vpid_bitmap, VMX_NR_VPIDS);
1124 static DEFINE_SPINLOCK(vmx_vpid_lock);
1126 static struct vmcs_config {
1131 u32 pin_based_exec_ctrl;
1132 u32 cpu_based_exec_ctrl;
1133 u32 cpu_based_2nd_exec_ctrl;
1136 struct nested_vmx_msrs nested;
1139 static struct vmx_capability {
1144 #define VMX_SEGMENT_FIELD(seg) \
1145 [VCPU_SREG_##seg] = { \
1146 .selector = GUEST_##seg##_SELECTOR, \
1147 .base = GUEST_##seg##_BASE, \
1148 .limit = GUEST_##seg##_LIMIT, \
1149 .ar_bytes = GUEST_##seg##_AR_BYTES, \
1152 static const struct kvm_vmx_segment_field {
1157 } kvm_vmx_segment_fields[] = {
1158 VMX_SEGMENT_FIELD(CS),
1159 VMX_SEGMENT_FIELD(DS),
1160 VMX_SEGMENT_FIELD(ES),
1161 VMX_SEGMENT_FIELD(FS),
1162 VMX_SEGMENT_FIELD(GS),
1163 VMX_SEGMENT_FIELD(SS),
1164 VMX_SEGMENT_FIELD(TR),
1165 VMX_SEGMENT_FIELD(LDTR),
1168 static u64 host_efer;
1170 static void ept_save_pdptrs(struct kvm_vcpu *vcpu);
1173 * Keep MSR_STAR at the end, as setup_msrs() will try to optimize it
1174 * away by decrementing the array size.
1176 static const u32 vmx_msr_index[] = {
1177 #ifdef CONFIG_X86_64
1178 MSR_SYSCALL_MASK, MSR_LSTAR, MSR_CSTAR,
1180 MSR_EFER, MSR_TSC_AUX, MSR_STAR,
1183 DEFINE_STATIC_KEY_FALSE(enable_evmcs);
1185 #define current_evmcs ((struct hv_enlightened_vmcs *)this_cpu_read(current_vmcs))
1187 #define KVM_EVMCS_VERSION 1
1189 #if IS_ENABLED(CONFIG_HYPERV)
1190 static bool __read_mostly enlightened_vmcs = true;
1191 module_param(enlightened_vmcs, bool, 0444);
1193 static inline void evmcs_write64(unsigned long field, u64 value)
1196 int offset = get_evmcs_offset(field, &clean_field);
1201 *(u64 *)((char *)current_evmcs + offset) = value;
1203 current_evmcs->hv_clean_fields &= ~clean_field;
1206 static inline void evmcs_write32(unsigned long field, u32 value)
1209 int offset = get_evmcs_offset(field, &clean_field);
1214 *(u32 *)((char *)current_evmcs + offset) = value;
1215 current_evmcs->hv_clean_fields &= ~clean_field;
1218 static inline void evmcs_write16(unsigned long field, u16 value)
1221 int offset = get_evmcs_offset(field, &clean_field);
1226 *(u16 *)((char *)current_evmcs + offset) = value;
1227 current_evmcs->hv_clean_fields &= ~clean_field;
1230 static inline u64 evmcs_read64(unsigned long field)
1232 int offset = get_evmcs_offset(field, NULL);
1237 return *(u64 *)((char *)current_evmcs + offset);
1240 static inline u32 evmcs_read32(unsigned long field)
1242 int offset = get_evmcs_offset(field, NULL);
1247 return *(u32 *)((char *)current_evmcs + offset);
1250 static inline u16 evmcs_read16(unsigned long field)
1252 int offset = get_evmcs_offset(field, NULL);
1257 return *(u16 *)((char *)current_evmcs + offset);
1260 static inline void evmcs_touch_msr_bitmap(void)
1262 if (unlikely(!current_evmcs))
1265 if (current_evmcs->hv_enlightenments_control.msr_bitmap)
1266 current_evmcs->hv_clean_fields &=
1267 ~HV_VMX_ENLIGHTENED_CLEAN_FIELD_MSR_BITMAP;
1270 static void evmcs_load(u64 phys_addr)
1272 struct hv_vp_assist_page *vp_ap =
1273 hv_get_vp_assist_page(smp_processor_id());
1275 vp_ap->current_nested_vmcs = phys_addr;
1276 vp_ap->enlighten_vmentry = 1;
1279 static void evmcs_sanitize_exec_ctrls(struct vmcs_config *vmcs_conf)
1282 * Enlightened VMCSv1 doesn't support these:
1284 * POSTED_INTR_NV = 0x00000002,
1285 * GUEST_INTR_STATUS = 0x00000810,
1286 * APIC_ACCESS_ADDR = 0x00002014,
1287 * POSTED_INTR_DESC_ADDR = 0x00002016,
1288 * EOI_EXIT_BITMAP0 = 0x0000201c,
1289 * EOI_EXIT_BITMAP1 = 0x0000201e,
1290 * EOI_EXIT_BITMAP2 = 0x00002020,
1291 * EOI_EXIT_BITMAP3 = 0x00002022,
1293 vmcs_conf->pin_based_exec_ctrl &= ~PIN_BASED_POSTED_INTR;
1294 vmcs_conf->cpu_based_2nd_exec_ctrl &=
1295 ~SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY;
1296 vmcs_conf->cpu_based_2nd_exec_ctrl &=
1297 ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
1298 vmcs_conf->cpu_based_2nd_exec_ctrl &=
1299 ~SECONDARY_EXEC_APIC_REGISTER_VIRT;
1302 * GUEST_PML_INDEX = 0x00000812,
1303 * PML_ADDRESS = 0x0000200e,
1305 vmcs_conf->cpu_based_2nd_exec_ctrl &= ~SECONDARY_EXEC_ENABLE_PML;
1307 /* VM_FUNCTION_CONTROL = 0x00002018, */
1308 vmcs_conf->cpu_based_2nd_exec_ctrl &= ~SECONDARY_EXEC_ENABLE_VMFUNC;
1311 * EPTP_LIST_ADDRESS = 0x00002024,
1312 * VMREAD_BITMAP = 0x00002026,
1313 * VMWRITE_BITMAP = 0x00002028,
1315 vmcs_conf->cpu_based_2nd_exec_ctrl &= ~SECONDARY_EXEC_SHADOW_VMCS;
1318 * TSC_MULTIPLIER = 0x00002032,
1320 vmcs_conf->cpu_based_2nd_exec_ctrl &= ~SECONDARY_EXEC_TSC_SCALING;
1323 * PLE_GAP = 0x00004020,
1324 * PLE_WINDOW = 0x00004022,
1326 vmcs_conf->cpu_based_2nd_exec_ctrl &= ~SECONDARY_EXEC_PAUSE_LOOP_EXITING;
1329 * VMX_PREEMPTION_TIMER_VALUE = 0x0000482E,
1331 vmcs_conf->pin_based_exec_ctrl &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
1334 * GUEST_IA32_PERF_GLOBAL_CTRL = 0x00002808,
1335 * HOST_IA32_PERF_GLOBAL_CTRL = 0x00002c04,
1337 vmcs_conf->vmexit_ctrl &= ~VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL;
1338 vmcs_conf->vmentry_ctrl &= ~VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL;
1341 * Currently unsupported in KVM:
1342 * GUEST_IA32_RTIT_CTL = 0x00002814,
1345 #else /* !IS_ENABLED(CONFIG_HYPERV) */
1346 static inline void evmcs_write64(unsigned long field, u64 value) {}
1347 static inline void evmcs_write32(unsigned long field, u32 value) {}
1348 static inline void evmcs_write16(unsigned long field, u16 value) {}
1349 static inline u64 evmcs_read64(unsigned long field) { return 0; }
1350 static inline u32 evmcs_read32(unsigned long field) { return 0; }
1351 static inline u16 evmcs_read16(unsigned long field) { return 0; }
1352 static inline void evmcs_load(u64 phys_addr) {}
1353 static inline void evmcs_sanitize_exec_ctrls(struct vmcs_config *vmcs_conf) {}
1354 static inline void evmcs_touch_msr_bitmap(void) {}
1355 #endif /* IS_ENABLED(CONFIG_HYPERV) */
1357 static inline bool is_exception_n(u32 intr_info, u8 vector)
1359 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
1360 INTR_INFO_VALID_MASK)) ==
1361 (INTR_TYPE_HARD_EXCEPTION | vector | INTR_INFO_VALID_MASK);
1364 static inline bool is_debug(u32 intr_info)
1366 return is_exception_n(intr_info, DB_VECTOR);
1369 static inline bool is_breakpoint(u32 intr_info)
1371 return is_exception_n(intr_info, BP_VECTOR);
1374 static inline bool is_page_fault(u32 intr_info)
1376 return is_exception_n(intr_info, PF_VECTOR);
1379 static inline bool is_no_device(u32 intr_info)
1381 return is_exception_n(intr_info, NM_VECTOR);
1384 static inline bool is_invalid_opcode(u32 intr_info)
1386 return is_exception_n(intr_info, UD_VECTOR);
1389 static inline bool is_gp_fault(u32 intr_info)
1391 return is_exception_n(intr_info, GP_VECTOR);
1394 static inline bool is_external_interrupt(u32 intr_info)
1396 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK))
1397 == (INTR_TYPE_EXT_INTR | INTR_INFO_VALID_MASK);
1400 static inline bool is_machine_check(u32 intr_info)
1402 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
1403 INTR_INFO_VALID_MASK)) ==
1404 (INTR_TYPE_HARD_EXCEPTION | MC_VECTOR | INTR_INFO_VALID_MASK);
1407 /* Undocumented: icebp/int1 */
1408 static inline bool is_icebp(u32 intr_info)
1410 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK))
1411 == (INTR_TYPE_PRIV_SW_EXCEPTION | INTR_INFO_VALID_MASK);
1414 static inline bool cpu_has_vmx_msr_bitmap(void)
1416 return vmcs_config.cpu_based_exec_ctrl & CPU_BASED_USE_MSR_BITMAPS;
1419 static inline bool cpu_has_vmx_tpr_shadow(void)
1421 return vmcs_config.cpu_based_exec_ctrl & CPU_BASED_TPR_SHADOW;
1424 static inline bool cpu_need_tpr_shadow(struct kvm_vcpu *vcpu)
1426 return cpu_has_vmx_tpr_shadow() && lapic_in_kernel(vcpu);
1429 static inline bool cpu_has_secondary_exec_ctrls(void)
1431 return vmcs_config.cpu_based_exec_ctrl &
1432 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
1435 static inline bool cpu_has_vmx_virtualize_apic_accesses(void)
1437 return vmcs_config.cpu_based_2nd_exec_ctrl &
1438 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
1441 static inline bool cpu_has_vmx_virtualize_x2apic_mode(void)
1443 return vmcs_config.cpu_based_2nd_exec_ctrl &
1444 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
1447 static inline bool cpu_has_vmx_apic_register_virt(void)
1449 return vmcs_config.cpu_based_2nd_exec_ctrl &
1450 SECONDARY_EXEC_APIC_REGISTER_VIRT;
1453 static inline bool cpu_has_vmx_virtual_intr_delivery(void)
1455 return vmcs_config.cpu_based_2nd_exec_ctrl &
1456 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY;
1460 * Comment's format: document - errata name - stepping - processor name.
1462 * https://www.virtualbox.org/svn/vbox/trunk/src/VBox/VMM/VMMR0/HMR0.cpp
1464 static u32 vmx_preemption_cpu_tfms[] = {
1465 /* 323344.pdf - BA86 - D0 - Xeon 7500 Series */
1467 /* 323056.pdf - AAX65 - C2 - Xeon L3406 */
1468 /* 322814.pdf - AAT59 - C2 - i7-600, i5-500, i5-400 and i3-300 Mobile */
1469 /* 322911.pdf - AAU65 - C2 - i5-600, i3-500 Desktop and Pentium G6950 */
1471 /* 322911.pdf - AAU65 - K0 - i5-600, i3-500 Desktop and Pentium G6950 */
1473 /* 322373.pdf - AAO95 - B1 - Xeon 3400 Series */
1474 /* 322166.pdf - AAN92 - B1 - i7-800 and i5-700 Desktop */
1476 * 320767.pdf - AAP86 - B1 -
1477 * i7-900 Mobile Extreme, i7-800 and i7-700 Mobile
1480 /* 321333.pdf - AAM126 - C0 - Xeon 3500 */
1482 /* 321333.pdf - AAM126 - C1 - Xeon 3500 */
1484 /* 320836.pdf - AAJ124 - C0 - i7-900 Desktop Extreme and i7-900 Desktop */
1486 /* 321333.pdf - AAM126 - D0 - Xeon 3500 */
1487 /* 321324.pdf - AAK139 - D0 - Xeon 5500 */
1488 /* 320836.pdf - AAJ124 - D0 - i7-900 Extreme and i7-900 Desktop */
1492 static inline bool cpu_has_broken_vmx_preemption_timer(void)
1494 u32 eax = cpuid_eax(0x00000001), i;
1496 /* Clear the reserved bits */
1497 eax &= ~(0x3U << 14 | 0xfU << 28);
1498 for (i = 0; i < ARRAY_SIZE(vmx_preemption_cpu_tfms); i++)
1499 if (eax == vmx_preemption_cpu_tfms[i])
1505 static inline bool cpu_has_vmx_preemption_timer(void)
1507 return vmcs_config.pin_based_exec_ctrl &
1508 PIN_BASED_VMX_PREEMPTION_TIMER;
1511 static inline bool cpu_has_vmx_posted_intr(void)
1513 return IS_ENABLED(CONFIG_X86_LOCAL_APIC) &&
1514 vmcs_config.pin_based_exec_ctrl & PIN_BASED_POSTED_INTR;
1517 static inline bool cpu_has_vmx_apicv(void)
1519 return cpu_has_vmx_apic_register_virt() &&
1520 cpu_has_vmx_virtual_intr_delivery() &&
1521 cpu_has_vmx_posted_intr();
1524 static inline bool cpu_has_vmx_flexpriority(void)
1526 return cpu_has_vmx_tpr_shadow() &&
1527 cpu_has_vmx_virtualize_apic_accesses();
1530 static inline bool cpu_has_vmx_ept_execute_only(void)
1532 return vmx_capability.ept & VMX_EPT_EXECUTE_ONLY_BIT;
1535 static inline bool cpu_has_vmx_ept_2m_page(void)
1537 return vmx_capability.ept & VMX_EPT_2MB_PAGE_BIT;
1540 static inline bool cpu_has_vmx_ept_1g_page(void)
1542 return vmx_capability.ept & VMX_EPT_1GB_PAGE_BIT;
1545 static inline bool cpu_has_vmx_ept_4levels(void)
1547 return vmx_capability.ept & VMX_EPT_PAGE_WALK_4_BIT;
1550 static inline bool cpu_has_vmx_ept_mt_wb(void)
1552 return vmx_capability.ept & VMX_EPTP_WB_BIT;
1555 static inline bool cpu_has_vmx_ept_5levels(void)
1557 return vmx_capability.ept & VMX_EPT_PAGE_WALK_5_BIT;
1560 static inline bool cpu_has_vmx_ept_ad_bits(void)
1562 return vmx_capability.ept & VMX_EPT_AD_BIT;
1565 static inline bool cpu_has_vmx_invept_context(void)
1567 return vmx_capability.ept & VMX_EPT_EXTENT_CONTEXT_BIT;
1570 static inline bool cpu_has_vmx_invept_global(void)
1572 return vmx_capability.ept & VMX_EPT_EXTENT_GLOBAL_BIT;
1575 static inline bool cpu_has_vmx_invvpid_individual_addr(void)
1577 return vmx_capability.vpid & VMX_VPID_EXTENT_INDIVIDUAL_ADDR_BIT;
1580 static inline bool cpu_has_vmx_invvpid_single(void)
1582 return vmx_capability.vpid & VMX_VPID_EXTENT_SINGLE_CONTEXT_BIT;
1585 static inline bool cpu_has_vmx_invvpid_global(void)
1587 return vmx_capability.vpid & VMX_VPID_EXTENT_GLOBAL_CONTEXT_BIT;
1590 static inline bool cpu_has_vmx_invvpid(void)
1592 return vmx_capability.vpid & VMX_VPID_INVVPID_BIT;
1595 static inline bool cpu_has_vmx_ept(void)
1597 return vmcs_config.cpu_based_2nd_exec_ctrl &
1598 SECONDARY_EXEC_ENABLE_EPT;
1601 static inline bool cpu_has_vmx_unrestricted_guest(void)
1603 return vmcs_config.cpu_based_2nd_exec_ctrl &
1604 SECONDARY_EXEC_UNRESTRICTED_GUEST;
1607 static inline bool cpu_has_vmx_ple(void)
1609 return vmcs_config.cpu_based_2nd_exec_ctrl &
1610 SECONDARY_EXEC_PAUSE_LOOP_EXITING;
1613 static inline bool cpu_has_vmx_basic_inout(void)
1615 return (((u64)vmcs_config.basic_cap << 32) & VMX_BASIC_INOUT);
1618 static inline bool cpu_need_virtualize_apic_accesses(struct kvm_vcpu *vcpu)
1620 return flexpriority_enabled && lapic_in_kernel(vcpu);
1623 static inline bool cpu_has_vmx_vpid(void)
1625 return vmcs_config.cpu_based_2nd_exec_ctrl &
1626 SECONDARY_EXEC_ENABLE_VPID;
1629 static inline bool cpu_has_vmx_rdtscp(void)
1631 return vmcs_config.cpu_based_2nd_exec_ctrl &
1632 SECONDARY_EXEC_RDTSCP;
1635 static inline bool cpu_has_vmx_invpcid(void)
1637 return vmcs_config.cpu_based_2nd_exec_ctrl &
1638 SECONDARY_EXEC_ENABLE_INVPCID;
1641 static inline bool cpu_has_virtual_nmis(void)
1643 return vmcs_config.pin_based_exec_ctrl & PIN_BASED_VIRTUAL_NMIS;
1646 static inline bool cpu_has_vmx_wbinvd_exit(void)
1648 return vmcs_config.cpu_based_2nd_exec_ctrl &
1649 SECONDARY_EXEC_WBINVD_EXITING;
1652 static inline bool cpu_has_vmx_shadow_vmcs(void)
1655 rdmsrl(MSR_IA32_VMX_MISC, vmx_msr);
1656 /* check if the cpu supports writing r/o exit information fields */
1657 if (!(vmx_msr & MSR_IA32_VMX_MISC_VMWRITE_SHADOW_RO_FIELDS))
1660 return vmcs_config.cpu_based_2nd_exec_ctrl &
1661 SECONDARY_EXEC_SHADOW_VMCS;
1664 static inline bool cpu_has_vmx_pml(void)
1666 return vmcs_config.cpu_based_2nd_exec_ctrl & SECONDARY_EXEC_ENABLE_PML;
1669 static inline bool cpu_has_vmx_tsc_scaling(void)
1671 return vmcs_config.cpu_based_2nd_exec_ctrl &
1672 SECONDARY_EXEC_TSC_SCALING;
1675 static inline bool cpu_has_vmx_vmfunc(void)
1677 return vmcs_config.cpu_based_2nd_exec_ctrl &
1678 SECONDARY_EXEC_ENABLE_VMFUNC;
1681 static bool vmx_umip_emulated(void)
1683 return vmcs_config.cpu_based_2nd_exec_ctrl &
1684 SECONDARY_EXEC_DESC;
1687 static inline bool report_flexpriority(void)
1689 return flexpriority_enabled;
1692 static inline unsigned nested_cpu_vmx_misc_cr3_count(struct kvm_vcpu *vcpu)
1694 return vmx_misc_cr3_count(to_vmx(vcpu)->nested.msrs.misc_low);
1698 * Do the virtual VMX capability MSRs specify that L1 can use VMWRITE
1699 * to modify any valid field of the VMCS, or are the VM-exit
1700 * information fields read-only?
1702 static inline bool nested_cpu_has_vmwrite_any_field(struct kvm_vcpu *vcpu)
1704 return to_vmx(vcpu)->nested.msrs.misc_low &
1705 MSR_IA32_VMX_MISC_VMWRITE_SHADOW_RO_FIELDS;
1708 static inline bool nested_cpu_has(struct vmcs12 *vmcs12, u32 bit)
1710 return vmcs12->cpu_based_vm_exec_control & bit;
1713 static inline bool nested_cpu_has2(struct vmcs12 *vmcs12, u32 bit)
1715 return (vmcs12->cpu_based_vm_exec_control &
1716 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) &&
1717 (vmcs12->secondary_vm_exec_control & bit);
1720 static inline bool nested_cpu_has_preemption_timer(struct vmcs12 *vmcs12)
1722 return vmcs12->pin_based_vm_exec_control &
1723 PIN_BASED_VMX_PREEMPTION_TIMER;
1726 static inline bool nested_cpu_has_nmi_exiting(struct vmcs12 *vmcs12)
1728 return vmcs12->pin_based_vm_exec_control & PIN_BASED_NMI_EXITING;
1731 static inline bool nested_cpu_has_virtual_nmis(struct vmcs12 *vmcs12)
1733 return vmcs12->pin_based_vm_exec_control & PIN_BASED_VIRTUAL_NMIS;
1736 static inline int nested_cpu_has_ept(struct vmcs12 *vmcs12)
1738 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_EPT);
1741 static inline bool nested_cpu_has_xsaves(struct vmcs12 *vmcs12)
1743 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_XSAVES);
1746 static inline bool nested_cpu_has_pml(struct vmcs12 *vmcs12)
1748 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_PML);
1751 static inline bool nested_cpu_has_virt_x2apic_mode(struct vmcs12 *vmcs12)
1753 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE);
1756 static inline bool nested_cpu_has_vpid(struct vmcs12 *vmcs12)
1758 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_VPID);
1761 static inline bool nested_cpu_has_apic_reg_virt(struct vmcs12 *vmcs12)
1763 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_APIC_REGISTER_VIRT);
1766 static inline bool nested_cpu_has_vid(struct vmcs12 *vmcs12)
1768 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
1771 static inline bool nested_cpu_has_posted_intr(struct vmcs12 *vmcs12)
1773 return vmcs12->pin_based_vm_exec_control & PIN_BASED_POSTED_INTR;
1776 static inline bool nested_cpu_has_vmfunc(struct vmcs12 *vmcs12)
1778 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_VMFUNC);
1781 static inline bool nested_cpu_has_eptp_switching(struct vmcs12 *vmcs12)
1783 return nested_cpu_has_vmfunc(vmcs12) &&
1784 (vmcs12->vm_function_control &
1785 VMX_VMFUNC_EPTP_SWITCHING);
1788 static inline bool is_nmi(u32 intr_info)
1790 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK))
1791 == (INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK);
1794 static void nested_vmx_vmexit(struct kvm_vcpu *vcpu, u32 exit_reason,
1796 unsigned long exit_qualification);
1797 static void nested_vmx_entry_failure(struct kvm_vcpu *vcpu,
1798 struct vmcs12 *vmcs12,
1799 u32 reason, unsigned long qualification);
1801 static int __find_msr_index(struct vcpu_vmx *vmx, u32 msr)
1805 for (i = 0; i < vmx->nmsrs; ++i)
1806 if (vmx_msr_index[vmx->guest_msrs[i].index] == msr)
1811 static inline void __invvpid(int ext, u16 vpid, gva_t gva)
1817 } operand = { vpid, 0, gva };
1819 asm volatile (__ex(ASM_VMX_INVVPID)
1820 /* CF==1 or ZF==1 --> rc = -1 */
1821 "; ja 1f ; ud2 ; 1:"
1822 : : "a"(&operand), "c"(ext) : "cc", "memory");
1825 static inline void __invept(int ext, u64 eptp, gpa_t gpa)
1829 } operand = {eptp, gpa};
1831 asm volatile (__ex(ASM_VMX_INVEPT)
1832 /* CF==1 or ZF==1 --> rc = -1 */
1833 "; ja 1f ; ud2 ; 1:\n"
1834 : : "a" (&operand), "c" (ext) : "cc", "memory");
1837 static struct shared_msr_entry *find_msr_entry(struct vcpu_vmx *vmx, u32 msr)
1841 i = __find_msr_index(vmx, msr);
1843 return &vmx->guest_msrs[i];
1847 static void vmcs_clear(struct vmcs *vmcs)
1849 u64 phys_addr = __pa(vmcs);
1852 asm volatile (__ex(ASM_VMX_VMCLEAR_RAX) "; setna %0"
1853 : "=qm"(error) : "a"(&phys_addr), "m"(phys_addr)
1856 printk(KERN_ERR "kvm: vmclear fail: %p/%llx\n",
1860 static inline void loaded_vmcs_init(struct loaded_vmcs *loaded_vmcs)
1862 vmcs_clear(loaded_vmcs->vmcs);
1863 if (loaded_vmcs->shadow_vmcs && loaded_vmcs->launched)
1864 vmcs_clear(loaded_vmcs->shadow_vmcs);
1865 loaded_vmcs->cpu = -1;
1866 loaded_vmcs->launched = 0;
1869 static void vmcs_load(struct vmcs *vmcs)
1871 u64 phys_addr = __pa(vmcs);
1874 if (static_branch_unlikely(&enable_evmcs))
1875 return evmcs_load(phys_addr);
1877 asm volatile (__ex(ASM_VMX_VMPTRLD_RAX) "; setna %0"
1878 : "=qm"(error) : "a"(&phys_addr), "m"(phys_addr)
1881 printk(KERN_ERR "kvm: vmptrld %p/%llx failed\n",
1885 #ifdef CONFIG_KEXEC_CORE
1887 * This bitmap is used to indicate whether the vmclear
1888 * operation is enabled on all cpus. All disabled by
1891 static cpumask_t crash_vmclear_enabled_bitmap = CPU_MASK_NONE;
1893 static inline void crash_enable_local_vmclear(int cpu)
1895 cpumask_set_cpu(cpu, &crash_vmclear_enabled_bitmap);
1898 static inline void crash_disable_local_vmclear(int cpu)
1900 cpumask_clear_cpu(cpu, &crash_vmclear_enabled_bitmap);
1903 static inline int crash_local_vmclear_enabled(int cpu)
1905 return cpumask_test_cpu(cpu, &crash_vmclear_enabled_bitmap);
1908 static void crash_vmclear_local_loaded_vmcss(void)
1910 int cpu = raw_smp_processor_id();
1911 struct loaded_vmcs *v;
1913 if (!crash_local_vmclear_enabled(cpu))
1916 list_for_each_entry(v, &per_cpu(loaded_vmcss_on_cpu, cpu),
1917 loaded_vmcss_on_cpu_link)
1918 vmcs_clear(v->vmcs);
1921 static inline void crash_enable_local_vmclear(int cpu) { }
1922 static inline void crash_disable_local_vmclear(int cpu) { }
1923 #endif /* CONFIG_KEXEC_CORE */
1925 static void __loaded_vmcs_clear(void *arg)
1927 struct loaded_vmcs *loaded_vmcs = arg;
1928 int cpu = raw_smp_processor_id();
1930 if (loaded_vmcs->cpu != cpu)
1931 return; /* vcpu migration can race with cpu offline */
1932 if (per_cpu(current_vmcs, cpu) == loaded_vmcs->vmcs)
1933 per_cpu(current_vmcs, cpu) = NULL;
1934 crash_disable_local_vmclear(cpu);
1935 list_del(&loaded_vmcs->loaded_vmcss_on_cpu_link);
1938 * we should ensure updating loaded_vmcs->loaded_vmcss_on_cpu_link
1939 * is before setting loaded_vmcs->vcpu to -1 which is done in
1940 * loaded_vmcs_init. Otherwise, other cpu can see vcpu = -1 fist
1941 * then adds the vmcs into percpu list before it is deleted.
1945 loaded_vmcs_init(loaded_vmcs);
1946 crash_enable_local_vmclear(cpu);
1949 static void loaded_vmcs_clear(struct loaded_vmcs *loaded_vmcs)
1951 int cpu = loaded_vmcs->cpu;
1954 smp_call_function_single(cpu,
1955 __loaded_vmcs_clear, loaded_vmcs, 1);
1958 static inline void vpid_sync_vcpu_single(int vpid)
1963 if (cpu_has_vmx_invvpid_single())
1964 __invvpid(VMX_VPID_EXTENT_SINGLE_CONTEXT, vpid, 0);
1967 static inline void vpid_sync_vcpu_global(void)
1969 if (cpu_has_vmx_invvpid_global())
1970 __invvpid(VMX_VPID_EXTENT_ALL_CONTEXT, 0, 0);
1973 static inline void vpid_sync_context(int vpid)
1975 if (cpu_has_vmx_invvpid_single())
1976 vpid_sync_vcpu_single(vpid);
1978 vpid_sync_vcpu_global();
1981 static inline void ept_sync_global(void)
1983 __invept(VMX_EPT_EXTENT_GLOBAL, 0, 0);
1986 static inline void ept_sync_context(u64 eptp)
1988 if (cpu_has_vmx_invept_context())
1989 __invept(VMX_EPT_EXTENT_CONTEXT, eptp, 0);
1994 static __always_inline void vmcs_check16(unsigned long field)
1996 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2000,
1997 "16-bit accessor invalid for 64-bit field");
1998 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2001,
1999 "16-bit accessor invalid for 64-bit high field");
2000 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x4000,
2001 "16-bit accessor invalid for 32-bit high field");
2002 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x6000,
2003 "16-bit accessor invalid for natural width field");
2006 static __always_inline void vmcs_check32(unsigned long field)
2008 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0,
2009 "32-bit accessor invalid for 16-bit field");
2010 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x6000,
2011 "32-bit accessor invalid for natural width field");
2014 static __always_inline void vmcs_check64(unsigned long field)
2016 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0,
2017 "64-bit accessor invalid for 16-bit field");
2018 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2001,
2019 "64-bit accessor invalid for 64-bit high field");
2020 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x4000,
2021 "64-bit accessor invalid for 32-bit field");
2022 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x6000,
2023 "64-bit accessor invalid for natural width field");
2026 static __always_inline void vmcs_checkl(unsigned long field)
2028 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0,
2029 "Natural width accessor invalid for 16-bit field");
2030 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2000,
2031 "Natural width accessor invalid for 64-bit field");
2032 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2001,
2033 "Natural width accessor invalid for 64-bit high field");
2034 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x4000,
2035 "Natural width accessor invalid for 32-bit field");
2038 static __always_inline unsigned long __vmcs_readl(unsigned long field)
2040 unsigned long value;
2042 asm volatile (__ex_clear(ASM_VMX_VMREAD_RDX_RAX, "%0")
2043 : "=a"(value) : "d"(field) : "cc");
2047 static __always_inline u16 vmcs_read16(unsigned long field)
2049 vmcs_check16(field);
2050 if (static_branch_unlikely(&enable_evmcs))
2051 return evmcs_read16(field);
2052 return __vmcs_readl(field);
2055 static __always_inline u32 vmcs_read32(unsigned long field)
2057 vmcs_check32(field);
2058 if (static_branch_unlikely(&enable_evmcs))
2059 return evmcs_read32(field);
2060 return __vmcs_readl(field);
2063 static __always_inline u64 vmcs_read64(unsigned long field)
2065 vmcs_check64(field);
2066 if (static_branch_unlikely(&enable_evmcs))
2067 return evmcs_read64(field);
2068 #ifdef CONFIG_X86_64
2069 return __vmcs_readl(field);
2071 return __vmcs_readl(field) | ((u64)__vmcs_readl(field+1) << 32);
2075 static __always_inline unsigned long vmcs_readl(unsigned long field)
2078 if (static_branch_unlikely(&enable_evmcs))
2079 return evmcs_read64(field);
2080 return __vmcs_readl(field);
2083 static noinline void vmwrite_error(unsigned long field, unsigned long value)
2085 printk(KERN_ERR "vmwrite error: reg %lx value %lx (err %d)\n",
2086 field, value, vmcs_read32(VM_INSTRUCTION_ERROR));
2090 static __always_inline void __vmcs_writel(unsigned long field, unsigned long value)
2094 asm volatile (__ex(ASM_VMX_VMWRITE_RAX_RDX) "; setna %0"
2095 : "=q"(error) : "a"(value), "d"(field) : "cc");
2096 if (unlikely(error))
2097 vmwrite_error(field, value);
2100 static __always_inline void vmcs_write16(unsigned long field, u16 value)
2102 vmcs_check16(field);
2103 if (static_branch_unlikely(&enable_evmcs))
2104 return evmcs_write16(field, value);
2106 __vmcs_writel(field, value);
2109 static __always_inline void vmcs_write32(unsigned long field, u32 value)
2111 vmcs_check32(field);
2112 if (static_branch_unlikely(&enable_evmcs))
2113 return evmcs_write32(field, value);
2115 __vmcs_writel(field, value);
2118 static __always_inline void vmcs_write64(unsigned long field, u64 value)
2120 vmcs_check64(field);
2121 if (static_branch_unlikely(&enable_evmcs))
2122 return evmcs_write64(field, value);
2124 __vmcs_writel(field, value);
2125 #ifndef CONFIG_X86_64
2127 __vmcs_writel(field+1, value >> 32);
2131 static __always_inline void vmcs_writel(unsigned long field, unsigned long value)
2134 if (static_branch_unlikely(&enable_evmcs))
2135 return evmcs_write64(field, value);
2137 __vmcs_writel(field, value);
2140 static __always_inline void vmcs_clear_bits(unsigned long field, u32 mask)
2142 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x2000,
2143 "vmcs_clear_bits does not support 64-bit fields");
2144 if (static_branch_unlikely(&enable_evmcs))
2145 return evmcs_write32(field, evmcs_read32(field) & ~mask);
2147 __vmcs_writel(field, __vmcs_readl(field) & ~mask);
2150 static __always_inline void vmcs_set_bits(unsigned long field, u32 mask)
2152 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x2000,
2153 "vmcs_set_bits does not support 64-bit fields");
2154 if (static_branch_unlikely(&enable_evmcs))
2155 return evmcs_write32(field, evmcs_read32(field) | mask);
2157 __vmcs_writel(field, __vmcs_readl(field) | mask);
2160 static inline void vm_entry_controls_reset_shadow(struct vcpu_vmx *vmx)
2162 vmx->vm_entry_controls_shadow = vmcs_read32(VM_ENTRY_CONTROLS);
2165 static inline void vm_entry_controls_init(struct vcpu_vmx *vmx, u32 val)
2167 vmcs_write32(VM_ENTRY_CONTROLS, val);
2168 vmx->vm_entry_controls_shadow = val;
2171 static inline void vm_entry_controls_set(struct vcpu_vmx *vmx, u32 val)
2173 if (vmx->vm_entry_controls_shadow != val)
2174 vm_entry_controls_init(vmx, val);
2177 static inline u32 vm_entry_controls_get(struct vcpu_vmx *vmx)
2179 return vmx->vm_entry_controls_shadow;
2183 static inline void vm_entry_controls_setbit(struct vcpu_vmx *vmx, u32 val)
2185 vm_entry_controls_set(vmx, vm_entry_controls_get(vmx) | val);
2188 static inline void vm_entry_controls_clearbit(struct vcpu_vmx *vmx, u32 val)
2190 vm_entry_controls_set(vmx, vm_entry_controls_get(vmx) & ~val);
2193 static inline void vm_exit_controls_reset_shadow(struct vcpu_vmx *vmx)
2195 vmx->vm_exit_controls_shadow = vmcs_read32(VM_EXIT_CONTROLS);
2198 static inline void vm_exit_controls_init(struct vcpu_vmx *vmx, u32 val)
2200 vmcs_write32(VM_EXIT_CONTROLS, val);
2201 vmx->vm_exit_controls_shadow = val;
2204 static inline void vm_exit_controls_set(struct vcpu_vmx *vmx, u32 val)
2206 if (vmx->vm_exit_controls_shadow != val)
2207 vm_exit_controls_init(vmx, val);
2210 static inline u32 vm_exit_controls_get(struct vcpu_vmx *vmx)
2212 return vmx->vm_exit_controls_shadow;
2216 static inline void vm_exit_controls_setbit(struct vcpu_vmx *vmx, u32 val)
2218 vm_exit_controls_set(vmx, vm_exit_controls_get(vmx) | val);
2221 static inline void vm_exit_controls_clearbit(struct vcpu_vmx *vmx, u32 val)
2223 vm_exit_controls_set(vmx, vm_exit_controls_get(vmx) & ~val);
2226 static void vmx_segment_cache_clear(struct vcpu_vmx *vmx)
2228 vmx->segment_cache.bitmask = 0;
2231 static bool vmx_segment_cache_test_set(struct vcpu_vmx *vmx, unsigned seg,
2235 u32 mask = 1 << (seg * SEG_FIELD_NR + field);
2237 if (!(vmx->vcpu.arch.regs_avail & (1 << VCPU_EXREG_SEGMENTS))) {
2238 vmx->vcpu.arch.regs_avail |= (1 << VCPU_EXREG_SEGMENTS);
2239 vmx->segment_cache.bitmask = 0;
2241 ret = vmx->segment_cache.bitmask & mask;
2242 vmx->segment_cache.bitmask |= mask;
2246 static u16 vmx_read_guest_seg_selector(struct vcpu_vmx *vmx, unsigned seg)
2248 u16 *p = &vmx->segment_cache.seg[seg].selector;
2250 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_SEL))
2251 *p = vmcs_read16(kvm_vmx_segment_fields[seg].selector);
2255 static ulong vmx_read_guest_seg_base(struct vcpu_vmx *vmx, unsigned seg)
2257 ulong *p = &vmx->segment_cache.seg[seg].base;
2259 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_BASE))
2260 *p = vmcs_readl(kvm_vmx_segment_fields[seg].base);
2264 static u32 vmx_read_guest_seg_limit(struct vcpu_vmx *vmx, unsigned seg)
2266 u32 *p = &vmx->segment_cache.seg[seg].limit;
2268 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_LIMIT))
2269 *p = vmcs_read32(kvm_vmx_segment_fields[seg].limit);
2273 static u32 vmx_read_guest_seg_ar(struct vcpu_vmx *vmx, unsigned seg)
2275 u32 *p = &vmx->segment_cache.seg[seg].ar;
2277 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_AR))
2278 *p = vmcs_read32(kvm_vmx_segment_fields[seg].ar_bytes);
2282 static void update_exception_bitmap(struct kvm_vcpu *vcpu)
2286 eb = (1u << PF_VECTOR) | (1u << UD_VECTOR) | (1u << MC_VECTOR) |
2287 (1u << DB_VECTOR) | (1u << AC_VECTOR);
2289 * Guest access to VMware backdoor ports could legitimately
2290 * trigger #GP because of TSS I/O permission bitmap.
2291 * We intercept those #GP and allow access to them anyway
2294 if (enable_vmware_backdoor)
2295 eb |= (1u << GP_VECTOR);
2296 if ((vcpu->guest_debug &
2297 (KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_SW_BP)) ==
2298 (KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_SW_BP))
2299 eb |= 1u << BP_VECTOR;
2300 if (to_vmx(vcpu)->rmode.vm86_active)
2303 eb &= ~(1u << PF_VECTOR); /* bypass_guest_pf = 0 */
2305 /* When we are running a nested L2 guest and L1 specified for it a
2306 * certain exception bitmap, we must trap the same exceptions and pass
2307 * them to L1. When running L2, we will only handle the exceptions
2308 * specified above if L1 did not want them.
2310 if (is_guest_mode(vcpu))
2311 eb |= get_vmcs12(vcpu)->exception_bitmap;
2313 vmcs_write32(EXCEPTION_BITMAP, eb);
2317 * Check if MSR is intercepted for currently loaded MSR bitmap.
2319 static bool msr_write_intercepted(struct kvm_vcpu *vcpu, u32 msr)
2321 unsigned long *msr_bitmap;
2322 int f = sizeof(unsigned long);
2324 if (!cpu_has_vmx_msr_bitmap())
2327 msr_bitmap = to_vmx(vcpu)->loaded_vmcs->msr_bitmap;
2329 if (msr <= 0x1fff) {
2330 return !!test_bit(msr, msr_bitmap + 0x800 / f);
2331 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
2333 return !!test_bit(msr, msr_bitmap + 0xc00 / f);
2340 * Check if MSR is intercepted for L01 MSR bitmap.
2342 static bool msr_write_intercepted_l01(struct kvm_vcpu *vcpu, u32 msr)
2344 unsigned long *msr_bitmap;
2345 int f = sizeof(unsigned long);
2347 if (!cpu_has_vmx_msr_bitmap())
2350 msr_bitmap = to_vmx(vcpu)->vmcs01.msr_bitmap;
2352 if (msr <= 0x1fff) {
2353 return !!test_bit(msr, msr_bitmap + 0x800 / f);
2354 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
2356 return !!test_bit(msr, msr_bitmap + 0xc00 / f);
2362 static void clear_atomic_switch_msr_special(struct vcpu_vmx *vmx,
2363 unsigned long entry, unsigned long exit)
2365 vm_entry_controls_clearbit(vmx, entry);
2366 vm_exit_controls_clearbit(vmx, exit);
2369 static void clear_atomic_switch_msr(struct vcpu_vmx *vmx, unsigned msr)
2372 struct msr_autoload *m = &vmx->msr_autoload;
2376 if (cpu_has_load_ia32_efer) {
2377 clear_atomic_switch_msr_special(vmx,
2378 VM_ENTRY_LOAD_IA32_EFER,
2379 VM_EXIT_LOAD_IA32_EFER);
2383 case MSR_CORE_PERF_GLOBAL_CTRL:
2384 if (cpu_has_load_perf_global_ctrl) {
2385 clear_atomic_switch_msr_special(vmx,
2386 VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL,
2387 VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL);
2393 for (i = 0; i < m->nr; ++i)
2394 if (m->guest[i].index == msr)
2400 m->guest[i] = m->guest[m->nr];
2401 m->host[i] = m->host[m->nr];
2402 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, m->nr);
2403 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, m->nr);
2406 static void add_atomic_switch_msr_special(struct vcpu_vmx *vmx,
2407 unsigned long entry, unsigned long exit,
2408 unsigned long guest_val_vmcs, unsigned long host_val_vmcs,
2409 u64 guest_val, u64 host_val)
2411 vmcs_write64(guest_val_vmcs, guest_val);
2412 vmcs_write64(host_val_vmcs, host_val);
2413 vm_entry_controls_setbit(vmx, entry);
2414 vm_exit_controls_setbit(vmx, exit);
2417 static void add_atomic_switch_msr(struct vcpu_vmx *vmx, unsigned msr,
2418 u64 guest_val, u64 host_val)
2421 struct msr_autoload *m = &vmx->msr_autoload;
2425 if (cpu_has_load_ia32_efer) {
2426 add_atomic_switch_msr_special(vmx,
2427 VM_ENTRY_LOAD_IA32_EFER,
2428 VM_EXIT_LOAD_IA32_EFER,
2431 guest_val, host_val);
2435 case MSR_CORE_PERF_GLOBAL_CTRL:
2436 if (cpu_has_load_perf_global_ctrl) {
2437 add_atomic_switch_msr_special(vmx,
2438 VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL,
2439 VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL,
2440 GUEST_IA32_PERF_GLOBAL_CTRL,
2441 HOST_IA32_PERF_GLOBAL_CTRL,
2442 guest_val, host_val);
2446 case MSR_IA32_PEBS_ENABLE:
2447 /* PEBS needs a quiescent period after being disabled (to write
2448 * a record). Disabling PEBS through VMX MSR swapping doesn't
2449 * provide that period, so a CPU could write host's record into
2452 wrmsrl(MSR_IA32_PEBS_ENABLE, 0);
2455 for (i = 0; i < m->nr; ++i)
2456 if (m->guest[i].index == msr)
2459 if (i == NR_AUTOLOAD_MSRS) {
2460 printk_once(KERN_WARNING "Not enough msr switch entries. "
2461 "Can't add msr %x\n", msr);
2463 } else if (i == m->nr) {
2465 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, m->nr);
2466 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, m->nr);
2469 m->guest[i].index = msr;
2470 m->guest[i].value = guest_val;
2471 m->host[i].index = msr;
2472 m->host[i].value = host_val;
2475 static bool update_transition_efer(struct vcpu_vmx *vmx, int efer_offset)
2477 u64 guest_efer = vmx->vcpu.arch.efer;
2478 u64 ignore_bits = 0;
2482 * NX is needed to handle CR0.WP=1, CR4.SMEP=1. Testing
2483 * host CPUID is more efficient than testing guest CPUID
2484 * or CR4. Host SMEP is anyway a requirement for guest SMEP.
2486 if (boot_cpu_has(X86_FEATURE_SMEP))
2487 guest_efer |= EFER_NX;
2488 else if (!(guest_efer & EFER_NX))
2489 ignore_bits |= EFER_NX;
2493 * LMA and LME handled by hardware; SCE meaningless outside long mode.
2495 ignore_bits |= EFER_SCE;
2496 #ifdef CONFIG_X86_64
2497 ignore_bits |= EFER_LMA | EFER_LME;
2498 /* SCE is meaningful only in long mode on Intel */
2499 if (guest_efer & EFER_LMA)
2500 ignore_bits &= ~(u64)EFER_SCE;
2503 clear_atomic_switch_msr(vmx, MSR_EFER);
2506 * On EPT, we can't emulate NX, so we must switch EFER atomically.
2507 * On CPUs that support "load IA32_EFER", always switch EFER
2508 * atomically, since it's faster than switching it manually.
2510 if (cpu_has_load_ia32_efer ||
2511 (enable_ept && ((vmx->vcpu.arch.efer ^ host_efer) & EFER_NX))) {
2512 if (!(guest_efer & EFER_LMA))
2513 guest_efer &= ~EFER_LME;
2514 if (guest_efer != host_efer)
2515 add_atomic_switch_msr(vmx, MSR_EFER,
2516 guest_efer, host_efer);
2519 guest_efer &= ~ignore_bits;
2520 guest_efer |= host_efer & ignore_bits;
2522 vmx->guest_msrs[efer_offset].data = guest_efer;
2523 vmx->guest_msrs[efer_offset].mask = ~ignore_bits;
2529 #ifdef CONFIG_X86_32
2531 * On 32-bit kernels, VM exits still load the FS and GS bases from the
2532 * VMCS rather than the segment table. KVM uses this helper to figure
2533 * out the current bases to poke them into the VMCS before entry.
2535 static unsigned long segment_base(u16 selector)
2537 struct desc_struct *table;
2540 if (!(selector & ~SEGMENT_RPL_MASK))
2543 table = get_current_gdt_ro();
2545 if ((selector & SEGMENT_TI_MASK) == SEGMENT_LDT) {
2546 u16 ldt_selector = kvm_read_ldt();
2548 if (!(ldt_selector & ~SEGMENT_RPL_MASK))
2551 table = (struct desc_struct *)segment_base(ldt_selector);
2553 v = get_desc_base(&table[selector >> 3]);
2558 static void vmx_save_host_state(struct kvm_vcpu *vcpu)
2560 struct vcpu_vmx *vmx = to_vmx(vcpu);
2561 #ifdef CONFIG_X86_64
2562 int cpu = raw_smp_processor_id();
2566 if (vmx->host_state.loaded)
2569 vmx->host_state.loaded = 1;
2571 * Set host fs and gs selectors. Unfortunately, 22.2.3 does not
2572 * allow segment selectors with cpl > 0 or ti == 1.
2574 vmx->host_state.ldt_sel = kvm_read_ldt();
2575 vmx->host_state.gs_ldt_reload_needed = vmx->host_state.ldt_sel;
2577 #ifdef CONFIG_X86_64
2578 save_fsgs_for_kvm();
2579 vmx->host_state.fs_sel = current->thread.fsindex;
2580 vmx->host_state.gs_sel = current->thread.gsindex;
2582 savesegment(fs, vmx->host_state.fs_sel);
2583 savesegment(gs, vmx->host_state.gs_sel);
2585 if (!(vmx->host_state.fs_sel & 7)) {
2586 vmcs_write16(HOST_FS_SELECTOR, vmx->host_state.fs_sel);
2587 vmx->host_state.fs_reload_needed = 0;
2589 vmcs_write16(HOST_FS_SELECTOR, 0);
2590 vmx->host_state.fs_reload_needed = 1;
2592 if (!(vmx->host_state.gs_sel & 7))
2593 vmcs_write16(HOST_GS_SELECTOR, vmx->host_state.gs_sel);
2595 vmcs_write16(HOST_GS_SELECTOR, 0);
2596 vmx->host_state.gs_ldt_reload_needed = 1;
2599 #ifdef CONFIG_X86_64
2600 savesegment(ds, vmx->host_state.ds_sel);
2601 savesegment(es, vmx->host_state.es_sel);
2603 vmcs_writel(HOST_FS_BASE, current->thread.fsbase);
2604 vmcs_writel(HOST_GS_BASE, cpu_kernelmode_gs_base(cpu));
2606 vmx->msr_host_kernel_gs_base = current->thread.gsbase;
2607 if (is_long_mode(&vmx->vcpu))
2608 wrmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base);
2610 vmcs_writel(HOST_FS_BASE, segment_base(vmx->host_state.fs_sel));
2611 vmcs_writel(HOST_GS_BASE, segment_base(vmx->host_state.gs_sel));
2613 if (boot_cpu_has(X86_FEATURE_MPX))
2614 rdmsrl(MSR_IA32_BNDCFGS, vmx->host_state.msr_host_bndcfgs);
2615 for (i = 0; i < vmx->save_nmsrs; ++i)
2616 kvm_set_shared_msr(vmx->guest_msrs[i].index,
2617 vmx->guest_msrs[i].data,
2618 vmx->guest_msrs[i].mask);
2621 static void __vmx_load_host_state(struct vcpu_vmx *vmx)
2623 if (!vmx->host_state.loaded)
2626 ++vmx->vcpu.stat.host_state_reload;
2627 vmx->host_state.loaded = 0;
2628 #ifdef CONFIG_X86_64
2629 if (is_long_mode(&vmx->vcpu))
2630 rdmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base);
2632 if (vmx->host_state.gs_ldt_reload_needed) {
2633 kvm_load_ldt(vmx->host_state.ldt_sel);
2634 #ifdef CONFIG_X86_64
2635 load_gs_index(vmx->host_state.gs_sel);
2637 loadsegment(gs, vmx->host_state.gs_sel);
2640 if (vmx->host_state.fs_reload_needed)
2641 loadsegment(fs, vmx->host_state.fs_sel);
2642 #ifdef CONFIG_X86_64
2643 if (unlikely(vmx->host_state.ds_sel | vmx->host_state.es_sel)) {
2644 loadsegment(ds, vmx->host_state.ds_sel);
2645 loadsegment(es, vmx->host_state.es_sel);
2648 invalidate_tss_limit();
2649 #ifdef CONFIG_X86_64
2650 wrmsrl(MSR_KERNEL_GS_BASE, vmx->msr_host_kernel_gs_base);
2652 if (vmx->host_state.msr_host_bndcfgs)
2653 wrmsrl(MSR_IA32_BNDCFGS, vmx->host_state.msr_host_bndcfgs);
2654 load_fixmap_gdt(raw_smp_processor_id());
2657 static void vmx_load_host_state(struct vcpu_vmx *vmx)
2660 __vmx_load_host_state(vmx);
2664 static void vmx_vcpu_pi_load(struct kvm_vcpu *vcpu, int cpu)
2666 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
2667 struct pi_desc old, new;
2671 * In case of hot-plug or hot-unplug, we may have to undo
2672 * vmx_vcpu_pi_put even if there is no assigned device. And we
2673 * always keep PI.NDST up to date for simplicity: it makes the
2674 * code easier, and CPU migration is not a fast path.
2676 if (!pi_test_sn(pi_desc) && vcpu->cpu == cpu)
2680 * First handle the simple case where no cmpxchg is necessary; just
2681 * allow posting non-urgent interrupts.
2683 * If the 'nv' field is POSTED_INTR_WAKEUP_VECTOR, do not change
2684 * PI.NDST: pi_post_block will do it for us and the wakeup_handler
2685 * expects the VCPU to be on the blocked_vcpu_list that matches
2688 if (pi_desc->nv == POSTED_INTR_WAKEUP_VECTOR ||
2690 pi_clear_sn(pi_desc);
2694 /* The full case. */
2696 old.control = new.control = pi_desc->control;
2698 dest = cpu_physical_id(cpu);
2700 if (x2apic_enabled())
2703 new.ndst = (dest << 8) & 0xFF00;
2706 } while (cmpxchg64(&pi_desc->control, old.control,
2707 new.control) != old.control);
2710 static void decache_tsc_multiplier(struct vcpu_vmx *vmx)
2712 vmx->current_tsc_ratio = vmx->vcpu.arch.tsc_scaling_ratio;
2713 vmcs_write64(TSC_MULTIPLIER, vmx->current_tsc_ratio);
2717 * Switches to specified vcpu, until a matching vcpu_put(), but assumes
2718 * vcpu mutex is already taken.
2720 static void vmx_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
2722 struct vcpu_vmx *vmx = to_vmx(vcpu);
2723 bool already_loaded = vmx->loaded_vmcs->cpu == cpu;
2725 if (!already_loaded) {
2726 loaded_vmcs_clear(vmx->loaded_vmcs);
2727 local_irq_disable();
2728 crash_disable_local_vmclear(cpu);
2731 * Read loaded_vmcs->cpu should be before fetching
2732 * loaded_vmcs->loaded_vmcss_on_cpu_link.
2733 * See the comments in __loaded_vmcs_clear().
2737 list_add(&vmx->loaded_vmcs->loaded_vmcss_on_cpu_link,
2738 &per_cpu(loaded_vmcss_on_cpu, cpu));
2739 crash_enable_local_vmclear(cpu);
2743 if (per_cpu(current_vmcs, cpu) != vmx->loaded_vmcs->vmcs) {
2744 per_cpu(current_vmcs, cpu) = vmx->loaded_vmcs->vmcs;
2745 vmcs_load(vmx->loaded_vmcs->vmcs);
2746 indirect_branch_prediction_barrier();
2749 if (!already_loaded) {
2750 void *gdt = get_current_gdt_ro();
2751 unsigned long sysenter_esp;
2753 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
2756 * Linux uses per-cpu TSS and GDT, so set these when switching
2757 * processors. See 22.2.4.
2759 vmcs_writel(HOST_TR_BASE,
2760 (unsigned long)&get_cpu_entry_area(cpu)->tss.x86_tss);
2761 vmcs_writel(HOST_GDTR_BASE, (unsigned long)gdt); /* 22.2.4 */
2764 * VM exits change the host TR limit to 0x67 after a VM
2765 * exit. This is okay, since 0x67 covers everything except
2766 * the IO bitmap and have have code to handle the IO bitmap
2767 * being lost after a VM exit.
2769 BUILD_BUG_ON(IO_BITMAP_OFFSET - 1 != 0x67);
2771 rdmsrl(MSR_IA32_SYSENTER_ESP, sysenter_esp);
2772 vmcs_writel(HOST_IA32_SYSENTER_ESP, sysenter_esp); /* 22.2.3 */
2774 vmx->loaded_vmcs->cpu = cpu;
2777 /* Setup TSC multiplier */
2778 if (kvm_has_tsc_control &&
2779 vmx->current_tsc_ratio != vcpu->arch.tsc_scaling_ratio)
2780 decache_tsc_multiplier(vmx);
2782 vmx_vcpu_pi_load(vcpu, cpu);
2783 vmx->host_pkru = read_pkru();
2784 vmx->host_debugctlmsr = get_debugctlmsr();
2787 static void vmx_vcpu_pi_put(struct kvm_vcpu *vcpu)
2789 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
2791 if (!kvm_arch_has_assigned_device(vcpu->kvm) ||
2792 !irq_remapping_cap(IRQ_POSTING_CAP) ||
2793 !kvm_vcpu_apicv_active(vcpu))
2796 /* Set SN when the vCPU is preempted */
2797 if (vcpu->preempted)
2801 static void vmx_vcpu_put(struct kvm_vcpu *vcpu)
2803 vmx_vcpu_pi_put(vcpu);
2805 __vmx_load_host_state(to_vmx(vcpu));
2808 static bool emulation_required(struct kvm_vcpu *vcpu)
2810 return emulate_invalid_guest_state && !guest_state_valid(vcpu);
2813 static void vmx_decache_cr0_guest_bits(struct kvm_vcpu *vcpu);
2816 * Return the cr0 value that a nested guest would read. This is a combination
2817 * of the real cr0 used to run the guest (guest_cr0), and the bits shadowed by
2818 * its hypervisor (cr0_read_shadow).
2820 static inline unsigned long nested_read_cr0(struct vmcs12 *fields)
2822 return (fields->guest_cr0 & ~fields->cr0_guest_host_mask) |
2823 (fields->cr0_read_shadow & fields->cr0_guest_host_mask);
2825 static inline unsigned long nested_read_cr4(struct vmcs12 *fields)
2827 return (fields->guest_cr4 & ~fields->cr4_guest_host_mask) |
2828 (fields->cr4_read_shadow & fields->cr4_guest_host_mask);
2831 static unsigned long vmx_get_rflags(struct kvm_vcpu *vcpu)
2833 unsigned long rflags, save_rflags;
2835 if (!test_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail)) {
2836 __set_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail);
2837 rflags = vmcs_readl(GUEST_RFLAGS);
2838 if (to_vmx(vcpu)->rmode.vm86_active) {
2839 rflags &= RMODE_GUEST_OWNED_EFLAGS_BITS;
2840 save_rflags = to_vmx(vcpu)->rmode.save_rflags;
2841 rflags |= save_rflags & ~RMODE_GUEST_OWNED_EFLAGS_BITS;
2843 to_vmx(vcpu)->rflags = rflags;
2845 return to_vmx(vcpu)->rflags;
2848 static void vmx_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags)
2850 unsigned long old_rflags = vmx_get_rflags(vcpu);
2852 __set_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail);
2853 to_vmx(vcpu)->rflags = rflags;
2854 if (to_vmx(vcpu)->rmode.vm86_active) {
2855 to_vmx(vcpu)->rmode.save_rflags = rflags;
2856 rflags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM;
2858 vmcs_writel(GUEST_RFLAGS, rflags);
2860 if ((old_rflags ^ to_vmx(vcpu)->rflags) & X86_EFLAGS_VM)
2861 to_vmx(vcpu)->emulation_required = emulation_required(vcpu);
2864 static u32 vmx_get_interrupt_shadow(struct kvm_vcpu *vcpu)
2866 u32 interruptibility = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
2869 if (interruptibility & GUEST_INTR_STATE_STI)
2870 ret |= KVM_X86_SHADOW_INT_STI;
2871 if (interruptibility & GUEST_INTR_STATE_MOV_SS)
2872 ret |= KVM_X86_SHADOW_INT_MOV_SS;
2877 static void vmx_set_interrupt_shadow(struct kvm_vcpu *vcpu, int mask)
2879 u32 interruptibility_old = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
2880 u32 interruptibility = interruptibility_old;
2882 interruptibility &= ~(GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS);
2884 if (mask & KVM_X86_SHADOW_INT_MOV_SS)
2885 interruptibility |= GUEST_INTR_STATE_MOV_SS;
2886 else if (mask & KVM_X86_SHADOW_INT_STI)
2887 interruptibility |= GUEST_INTR_STATE_STI;
2889 if ((interruptibility != interruptibility_old))
2890 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, interruptibility);
2893 static void skip_emulated_instruction(struct kvm_vcpu *vcpu)
2897 rip = kvm_rip_read(vcpu);
2898 rip += vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
2899 kvm_rip_write(vcpu, rip);
2901 /* skipping an emulated instruction also counts */
2902 vmx_set_interrupt_shadow(vcpu, 0);
2905 static void nested_vmx_inject_exception_vmexit(struct kvm_vcpu *vcpu,
2906 unsigned long exit_qual)
2908 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
2909 unsigned int nr = vcpu->arch.exception.nr;
2910 u32 intr_info = nr | INTR_INFO_VALID_MASK;
2912 if (vcpu->arch.exception.has_error_code) {
2913 vmcs12->vm_exit_intr_error_code = vcpu->arch.exception.error_code;
2914 intr_info |= INTR_INFO_DELIVER_CODE_MASK;
2917 if (kvm_exception_is_soft(nr))
2918 intr_info |= INTR_TYPE_SOFT_EXCEPTION;
2920 intr_info |= INTR_TYPE_HARD_EXCEPTION;
2922 if (!(vmcs12->idt_vectoring_info_field & VECTORING_INFO_VALID_MASK) &&
2923 vmx_get_nmi_mask(vcpu))
2924 intr_info |= INTR_INFO_UNBLOCK_NMI;
2926 nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI, intr_info, exit_qual);
2930 * KVM wants to inject page-faults which it got to the guest. This function
2931 * checks whether in a nested guest, we need to inject them to L1 or L2.
2933 static int nested_vmx_check_exception(struct kvm_vcpu *vcpu, unsigned long *exit_qual)
2935 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
2936 unsigned int nr = vcpu->arch.exception.nr;
2938 if (nr == PF_VECTOR) {
2939 if (vcpu->arch.exception.nested_apf) {
2940 *exit_qual = vcpu->arch.apf.nested_apf_token;
2944 * FIXME: we must not write CR2 when L1 intercepts an L2 #PF exception.
2945 * The fix is to add the ancillary datum (CR2 or DR6) to structs
2946 * kvm_queued_exception and kvm_vcpu_events, so that CR2 and DR6
2947 * can be written only when inject_pending_event runs. This should be
2948 * conditional on a new capability---if the capability is disabled,
2949 * kvm_multiple_exception would write the ancillary information to
2950 * CR2 or DR6, for backwards ABI-compatibility.
2952 if (nested_vmx_is_page_fault_vmexit(vmcs12,
2953 vcpu->arch.exception.error_code)) {
2954 *exit_qual = vcpu->arch.cr2;
2958 if (vmcs12->exception_bitmap & (1u << nr)) {
2959 if (nr == DB_VECTOR)
2960 *exit_qual = vcpu->arch.dr6;
2970 static void vmx_clear_hlt(struct kvm_vcpu *vcpu)
2973 * Ensure that we clear the HLT state in the VMCS. We don't need to
2974 * explicitly skip the instruction because if the HLT state is set,
2975 * then the instruction is already executing and RIP has already been
2978 if (kvm_hlt_in_guest(vcpu->kvm) &&
2979 vmcs_read32(GUEST_ACTIVITY_STATE) == GUEST_ACTIVITY_HLT)
2980 vmcs_write32(GUEST_ACTIVITY_STATE, GUEST_ACTIVITY_ACTIVE);
2983 static void vmx_queue_exception(struct kvm_vcpu *vcpu)
2985 struct vcpu_vmx *vmx = to_vmx(vcpu);
2986 unsigned nr = vcpu->arch.exception.nr;
2987 bool has_error_code = vcpu->arch.exception.has_error_code;
2988 u32 error_code = vcpu->arch.exception.error_code;
2989 u32 intr_info = nr | INTR_INFO_VALID_MASK;
2991 if (has_error_code) {
2992 vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE, error_code);
2993 intr_info |= INTR_INFO_DELIVER_CODE_MASK;
2996 if (vmx->rmode.vm86_active) {
2998 if (kvm_exception_is_soft(nr))
2999 inc_eip = vcpu->arch.event_exit_inst_len;
3000 if (kvm_inject_realmode_interrupt(vcpu, nr, inc_eip) != EMULATE_DONE)
3001 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
3005 WARN_ON_ONCE(vmx->emulation_required);
3007 if (kvm_exception_is_soft(nr)) {
3008 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
3009 vmx->vcpu.arch.event_exit_inst_len);
3010 intr_info |= INTR_TYPE_SOFT_EXCEPTION;
3012 intr_info |= INTR_TYPE_HARD_EXCEPTION;
3014 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr_info);
3016 vmx_clear_hlt(vcpu);
3019 static bool vmx_rdtscp_supported(void)
3021 return cpu_has_vmx_rdtscp();
3024 static bool vmx_invpcid_supported(void)
3026 return cpu_has_vmx_invpcid() && enable_ept;
3030 * Swap MSR entry in host/guest MSR entry array.
3032 static void move_msr_up(struct vcpu_vmx *vmx, int from, int to)
3034 struct shared_msr_entry tmp;
3036 tmp = vmx->guest_msrs[to];
3037 vmx->guest_msrs[to] = vmx->guest_msrs[from];
3038 vmx->guest_msrs[from] = tmp;
3042 * Set up the vmcs to automatically save and restore system
3043 * msrs. Don't touch the 64-bit msrs if the guest is in legacy
3044 * mode, as fiddling with msrs is very expensive.
3046 static void setup_msrs(struct vcpu_vmx *vmx)
3048 int save_nmsrs, index;
3051 #ifdef CONFIG_X86_64
3052 if (is_long_mode(&vmx->vcpu)) {
3053 index = __find_msr_index(vmx, MSR_SYSCALL_MASK);
3055 move_msr_up(vmx, index, save_nmsrs++);
3056 index = __find_msr_index(vmx, MSR_LSTAR);
3058 move_msr_up(vmx, index, save_nmsrs++);
3059 index = __find_msr_index(vmx, MSR_CSTAR);
3061 move_msr_up(vmx, index, save_nmsrs++);
3062 index = __find_msr_index(vmx, MSR_TSC_AUX);
3063 if (index >= 0 && guest_cpuid_has(&vmx->vcpu, X86_FEATURE_RDTSCP))
3064 move_msr_up(vmx, index, save_nmsrs++);
3066 * MSR_STAR is only needed on long mode guests, and only
3067 * if efer.sce is enabled.
3069 index = __find_msr_index(vmx, MSR_STAR);
3070 if ((index >= 0) && (vmx->vcpu.arch.efer & EFER_SCE))
3071 move_msr_up(vmx, index, save_nmsrs++);
3074 index = __find_msr_index(vmx, MSR_EFER);
3075 if (index >= 0 && update_transition_efer(vmx, index))
3076 move_msr_up(vmx, index, save_nmsrs++);
3078 vmx->save_nmsrs = save_nmsrs;
3080 if (cpu_has_vmx_msr_bitmap())
3081 vmx_update_msr_bitmap(&vmx->vcpu);
3084 static u64 vmx_read_l1_tsc_offset(struct kvm_vcpu *vcpu)
3086 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
3088 if (is_guest_mode(vcpu) &&
3089 (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING))
3090 return vcpu->arch.tsc_offset - vmcs12->tsc_offset;
3092 return vcpu->arch.tsc_offset;
3096 * writes 'offset' into guest's timestamp counter offset register
3098 static void vmx_write_tsc_offset(struct kvm_vcpu *vcpu, u64 offset)
3100 if (is_guest_mode(vcpu)) {
3102 * We're here if L1 chose not to trap WRMSR to TSC. According
3103 * to the spec, this should set L1's TSC; The offset that L1
3104 * set for L2 remains unchanged, and still needs to be added
3105 * to the newly set TSC to get L2's TSC.
3107 struct vmcs12 *vmcs12;
3108 /* recalculate vmcs02.TSC_OFFSET: */
3109 vmcs12 = get_vmcs12(vcpu);
3110 vmcs_write64(TSC_OFFSET, offset +
3111 (nested_cpu_has(vmcs12, CPU_BASED_USE_TSC_OFFSETING) ?
3112 vmcs12->tsc_offset : 0));
3114 trace_kvm_write_tsc_offset(vcpu->vcpu_id,
3115 vmcs_read64(TSC_OFFSET), offset);
3116 vmcs_write64(TSC_OFFSET, offset);
3121 * nested_vmx_allowed() checks whether a guest should be allowed to use VMX
3122 * instructions and MSRs (i.e., nested VMX). Nested VMX is disabled for
3123 * all guests if the "nested" module option is off, and can also be disabled
3124 * for a single guest by disabling its VMX cpuid bit.
3126 static inline bool nested_vmx_allowed(struct kvm_vcpu *vcpu)
3128 return nested && guest_cpuid_has(vcpu, X86_FEATURE_VMX);
3132 * nested_vmx_setup_ctls_msrs() sets up variables containing the values to be
3133 * returned for the various VMX controls MSRs when nested VMX is enabled.
3134 * The same values should also be used to verify that vmcs12 control fields are
3135 * valid during nested entry from L1 to L2.
3136 * Each of these control msrs has a low and high 32-bit half: A low bit is on
3137 * if the corresponding bit in the (32-bit) control field *must* be on, and a
3138 * bit in the high half is on if the corresponding bit in the control field
3139 * may be on. See also vmx_control_verify().
3141 static void nested_vmx_setup_ctls_msrs(struct nested_vmx_msrs *msrs, bool apicv)
3144 memset(msrs, 0, sizeof(*msrs));
3149 * Note that as a general rule, the high half of the MSRs (bits in
3150 * the control fields which may be 1) should be initialized by the
3151 * intersection of the underlying hardware's MSR (i.e., features which
3152 * can be supported) and the list of features we want to expose -
3153 * because they are known to be properly supported in our code.
3154 * Also, usually, the low half of the MSRs (bits which must be 1) can
3155 * be set to 0, meaning that L1 may turn off any of these bits. The
3156 * reason is that if one of these bits is necessary, it will appear
3157 * in vmcs01 and prepare_vmcs02, when it bitwise-or's the control
3158 * fields of vmcs01 and vmcs02, will turn these bits off - and
3159 * nested_vmx_exit_reflected() will not pass related exits to L1.
3160 * These rules have exceptions below.
3163 /* pin-based controls */
3164 rdmsr(MSR_IA32_VMX_PINBASED_CTLS,
3165 msrs->pinbased_ctls_low,
3166 msrs->pinbased_ctls_high);
3167 msrs->pinbased_ctls_low |=
3168 PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
3169 msrs->pinbased_ctls_high &=
3170 PIN_BASED_EXT_INTR_MASK |
3171 PIN_BASED_NMI_EXITING |
3172 PIN_BASED_VIRTUAL_NMIS |
3173 (apicv ? PIN_BASED_POSTED_INTR : 0);
3174 msrs->pinbased_ctls_high |=
3175 PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR |
3176 PIN_BASED_VMX_PREEMPTION_TIMER;
3179 rdmsr(MSR_IA32_VMX_EXIT_CTLS,
3180 msrs->exit_ctls_low,
3181 msrs->exit_ctls_high);
3182 msrs->exit_ctls_low =
3183 VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR;
3185 msrs->exit_ctls_high &=
3186 #ifdef CONFIG_X86_64
3187 VM_EXIT_HOST_ADDR_SPACE_SIZE |
3189 VM_EXIT_LOAD_IA32_PAT | VM_EXIT_SAVE_IA32_PAT;
3190 msrs->exit_ctls_high |=
3191 VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR |
3192 VM_EXIT_LOAD_IA32_EFER | VM_EXIT_SAVE_IA32_EFER |
3193 VM_EXIT_SAVE_VMX_PREEMPTION_TIMER | VM_EXIT_ACK_INTR_ON_EXIT;
3195 if (kvm_mpx_supported())
3196 msrs->exit_ctls_high |= VM_EXIT_CLEAR_BNDCFGS;
3198 /* We support free control of debug control saving. */
3199 msrs->exit_ctls_low &= ~VM_EXIT_SAVE_DEBUG_CONTROLS;
3201 /* entry controls */
3202 rdmsr(MSR_IA32_VMX_ENTRY_CTLS,
3203 msrs->entry_ctls_low,
3204 msrs->entry_ctls_high);
3205 msrs->entry_ctls_low =
3206 VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR;
3207 msrs->entry_ctls_high &=
3208 #ifdef CONFIG_X86_64
3209 VM_ENTRY_IA32E_MODE |
3211 VM_ENTRY_LOAD_IA32_PAT;
3212 msrs->entry_ctls_high |=
3213 (VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR | VM_ENTRY_LOAD_IA32_EFER);
3214 if (kvm_mpx_supported())
3215 msrs->entry_ctls_high |= VM_ENTRY_LOAD_BNDCFGS;
3217 /* We support free control of debug control loading. */
3218 msrs->entry_ctls_low &= ~VM_ENTRY_LOAD_DEBUG_CONTROLS;
3220 /* cpu-based controls */
3221 rdmsr(MSR_IA32_VMX_PROCBASED_CTLS,
3222 msrs->procbased_ctls_low,
3223 msrs->procbased_ctls_high);
3224 msrs->procbased_ctls_low =
3225 CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
3226 msrs->procbased_ctls_high &=
3227 CPU_BASED_VIRTUAL_INTR_PENDING |
3228 CPU_BASED_VIRTUAL_NMI_PENDING | CPU_BASED_USE_TSC_OFFSETING |
3229 CPU_BASED_HLT_EXITING | CPU_BASED_INVLPG_EXITING |
3230 CPU_BASED_MWAIT_EXITING | CPU_BASED_CR3_LOAD_EXITING |
3231 CPU_BASED_CR3_STORE_EXITING |
3232 #ifdef CONFIG_X86_64
3233 CPU_BASED_CR8_LOAD_EXITING | CPU_BASED_CR8_STORE_EXITING |
3235 CPU_BASED_MOV_DR_EXITING | CPU_BASED_UNCOND_IO_EXITING |
3236 CPU_BASED_USE_IO_BITMAPS | CPU_BASED_MONITOR_TRAP_FLAG |
3237 CPU_BASED_MONITOR_EXITING | CPU_BASED_RDPMC_EXITING |
3238 CPU_BASED_RDTSC_EXITING | CPU_BASED_PAUSE_EXITING |
3239 CPU_BASED_TPR_SHADOW | CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
3241 * We can allow some features even when not supported by the
3242 * hardware. For example, L1 can specify an MSR bitmap - and we
3243 * can use it to avoid exits to L1 - even when L0 runs L2
3244 * without MSR bitmaps.
3246 msrs->procbased_ctls_high |=
3247 CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR |
3248 CPU_BASED_USE_MSR_BITMAPS;
3250 /* We support free control of CR3 access interception. */
3251 msrs->procbased_ctls_low &=
3252 ~(CPU_BASED_CR3_LOAD_EXITING | CPU_BASED_CR3_STORE_EXITING);
3255 * secondary cpu-based controls. Do not include those that
3256 * depend on CPUID bits, they are added later by vmx_cpuid_update.
3258 rdmsr(MSR_IA32_VMX_PROCBASED_CTLS2,
3259 msrs->secondary_ctls_low,
3260 msrs->secondary_ctls_high);
3261 msrs->secondary_ctls_low = 0;
3262 msrs->secondary_ctls_high &=
3263 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
3264 SECONDARY_EXEC_DESC |
3265 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
3266 SECONDARY_EXEC_APIC_REGISTER_VIRT |
3267 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY |
3268 SECONDARY_EXEC_WBINVD_EXITING;
3271 /* nested EPT: emulate EPT also to L1 */
3272 msrs->secondary_ctls_high |=
3273 SECONDARY_EXEC_ENABLE_EPT;
3274 msrs->ept_caps = VMX_EPT_PAGE_WALK_4_BIT |
3275 VMX_EPTP_WB_BIT | VMX_EPT_INVEPT_BIT;
3276 if (cpu_has_vmx_ept_execute_only())
3278 VMX_EPT_EXECUTE_ONLY_BIT;
3279 msrs->ept_caps &= vmx_capability.ept;
3280 msrs->ept_caps |= VMX_EPT_EXTENT_GLOBAL_BIT |
3281 VMX_EPT_EXTENT_CONTEXT_BIT | VMX_EPT_2MB_PAGE_BIT |
3282 VMX_EPT_1GB_PAGE_BIT;
3283 if (enable_ept_ad_bits) {
3284 msrs->secondary_ctls_high |=
3285 SECONDARY_EXEC_ENABLE_PML;
3286 msrs->ept_caps |= VMX_EPT_AD_BIT;
3290 if (cpu_has_vmx_vmfunc()) {
3291 msrs->secondary_ctls_high |=
3292 SECONDARY_EXEC_ENABLE_VMFUNC;
3294 * Advertise EPTP switching unconditionally
3295 * since we emulate it
3298 msrs->vmfunc_controls =
3299 VMX_VMFUNC_EPTP_SWITCHING;
3303 * Old versions of KVM use the single-context version without
3304 * checking for support, so declare that it is supported even
3305 * though it is treated as global context. The alternative is
3306 * not failing the single-context invvpid, and it is worse.
3309 msrs->secondary_ctls_high |=
3310 SECONDARY_EXEC_ENABLE_VPID;
3311 msrs->vpid_caps = VMX_VPID_INVVPID_BIT |
3312 VMX_VPID_EXTENT_SUPPORTED_MASK;
3315 if (enable_unrestricted_guest)
3316 msrs->secondary_ctls_high |=
3317 SECONDARY_EXEC_UNRESTRICTED_GUEST;
3319 /* miscellaneous data */
3320 rdmsr(MSR_IA32_VMX_MISC,
3323 msrs->misc_low &= VMX_MISC_SAVE_EFER_LMA;
3325 MSR_IA32_VMX_MISC_VMWRITE_SHADOW_RO_FIELDS |
3326 VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE |
3327 VMX_MISC_ACTIVITY_HLT;
3328 msrs->misc_high = 0;
3331 * This MSR reports some information about VMX support. We
3332 * should return information about the VMX we emulate for the
3333 * guest, and the VMCS structure we give it - not about the
3334 * VMX support of the underlying hardware.
3338 VMX_BASIC_TRUE_CTLS |
3339 ((u64)VMCS12_SIZE << VMX_BASIC_VMCS_SIZE_SHIFT) |
3340 (VMX_BASIC_MEM_TYPE_WB << VMX_BASIC_MEM_TYPE_SHIFT);
3342 if (cpu_has_vmx_basic_inout())
3343 msrs->basic |= VMX_BASIC_INOUT;
3346 * These MSRs specify bits which the guest must keep fixed on
3347 * while L1 is in VMXON mode (in L1's root mode, or running an L2).
3348 * We picked the standard core2 setting.
3350 #define VMXON_CR0_ALWAYSON (X86_CR0_PE | X86_CR0_PG | X86_CR0_NE)
3351 #define VMXON_CR4_ALWAYSON X86_CR4_VMXE
3352 msrs->cr0_fixed0 = VMXON_CR0_ALWAYSON;
3353 msrs->cr4_fixed0 = VMXON_CR4_ALWAYSON;
3355 /* These MSRs specify bits which the guest must keep fixed off. */
3356 rdmsrl(MSR_IA32_VMX_CR0_FIXED1, msrs->cr0_fixed1);
3357 rdmsrl(MSR_IA32_VMX_CR4_FIXED1, msrs->cr4_fixed1);
3359 /* highest index: VMX_PREEMPTION_TIMER_VALUE */
3360 msrs->vmcs_enum = VMCS12_MAX_FIELD_INDEX << 1;
3364 * if fixed0[i] == 1: val[i] must be 1
3365 * if fixed1[i] == 0: val[i] must be 0
3367 static inline bool fixed_bits_valid(u64 val, u64 fixed0, u64 fixed1)
3369 return ((val & fixed1) | fixed0) == val;
3372 static inline bool vmx_control_verify(u32 control, u32 low, u32 high)
3374 return fixed_bits_valid(control, low, high);
3377 static inline u64 vmx_control_msr(u32 low, u32 high)
3379 return low | ((u64)high << 32);
3382 static bool is_bitwise_subset(u64 superset, u64 subset, u64 mask)
3387 return (superset | subset) == superset;
3390 static int vmx_restore_vmx_basic(struct vcpu_vmx *vmx, u64 data)
3392 const u64 feature_and_reserved =
3393 /* feature (except bit 48; see below) */
3394 BIT_ULL(49) | BIT_ULL(54) | BIT_ULL(55) |
3396 BIT_ULL(31) | GENMASK_ULL(47, 45) | GENMASK_ULL(63, 56);
3397 u64 vmx_basic = vmx->nested.msrs.basic;
3399 if (!is_bitwise_subset(vmx_basic, data, feature_and_reserved))
3403 * KVM does not emulate a version of VMX that constrains physical
3404 * addresses of VMX structures (e.g. VMCS) to 32-bits.
3406 if (data & BIT_ULL(48))
3409 if (vmx_basic_vmcs_revision_id(vmx_basic) !=
3410 vmx_basic_vmcs_revision_id(data))
3413 if (vmx_basic_vmcs_size(vmx_basic) > vmx_basic_vmcs_size(data))
3416 vmx->nested.msrs.basic = data;
3421 vmx_restore_control_msr(struct vcpu_vmx *vmx, u32 msr_index, u64 data)
3426 switch (msr_index) {
3427 case MSR_IA32_VMX_TRUE_PINBASED_CTLS:
3428 lowp = &vmx->nested.msrs.pinbased_ctls_low;
3429 highp = &vmx->nested.msrs.pinbased_ctls_high;
3431 case MSR_IA32_VMX_TRUE_PROCBASED_CTLS:
3432 lowp = &vmx->nested.msrs.procbased_ctls_low;
3433 highp = &vmx->nested.msrs.procbased_ctls_high;
3435 case MSR_IA32_VMX_TRUE_EXIT_CTLS:
3436 lowp = &vmx->nested.msrs.exit_ctls_low;
3437 highp = &vmx->nested.msrs.exit_ctls_high;
3439 case MSR_IA32_VMX_TRUE_ENTRY_CTLS:
3440 lowp = &vmx->nested.msrs.entry_ctls_low;
3441 highp = &vmx->nested.msrs.entry_ctls_high;
3443 case MSR_IA32_VMX_PROCBASED_CTLS2:
3444 lowp = &vmx->nested.msrs.secondary_ctls_low;
3445 highp = &vmx->nested.msrs.secondary_ctls_high;
3451 supported = vmx_control_msr(*lowp, *highp);
3453 /* Check must-be-1 bits are still 1. */
3454 if (!is_bitwise_subset(data, supported, GENMASK_ULL(31, 0)))
3457 /* Check must-be-0 bits are still 0. */
3458 if (!is_bitwise_subset(supported, data, GENMASK_ULL(63, 32)))
3462 *highp = data >> 32;
3466 static int vmx_restore_vmx_misc(struct vcpu_vmx *vmx, u64 data)
3468 const u64 feature_and_reserved_bits =
3470 BIT_ULL(5) | GENMASK_ULL(8, 6) | BIT_ULL(14) | BIT_ULL(15) |
3471 BIT_ULL(28) | BIT_ULL(29) | BIT_ULL(30) |
3473 GENMASK_ULL(13, 9) | BIT_ULL(31);
3476 vmx_misc = vmx_control_msr(vmx->nested.msrs.misc_low,
3477 vmx->nested.msrs.misc_high);
3479 if (!is_bitwise_subset(vmx_misc, data, feature_and_reserved_bits))
3482 if ((vmx->nested.msrs.pinbased_ctls_high &
3483 PIN_BASED_VMX_PREEMPTION_TIMER) &&
3484 vmx_misc_preemption_timer_rate(data) !=
3485 vmx_misc_preemption_timer_rate(vmx_misc))
3488 if (vmx_misc_cr3_count(data) > vmx_misc_cr3_count(vmx_misc))
3491 if (vmx_misc_max_msr(data) > vmx_misc_max_msr(vmx_misc))
3494 if (vmx_misc_mseg_revid(data) != vmx_misc_mseg_revid(vmx_misc))
3497 vmx->nested.msrs.misc_low = data;
3498 vmx->nested.msrs.misc_high = data >> 32;
3501 * If L1 has read-only VM-exit information fields, use the
3502 * less permissive vmx_vmwrite_bitmap to specify write
3503 * permissions for the shadow VMCS.
3505 if (enable_shadow_vmcs && !nested_cpu_has_vmwrite_any_field(&vmx->vcpu))
3506 vmcs_write64(VMWRITE_BITMAP, __pa(vmx_vmwrite_bitmap));
3511 static int vmx_restore_vmx_ept_vpid_cap(struct vcpu_vmx *vmx, u64 data)
3513 u64 vmx_ept_vpid_cap;
3515 vmx_ept_vpid_cap = vmx_control_msr(vmx->nested.msrs.ept_caps,
3516 vmx->nested.msrs.vpid_caps);
3518 /* Every bit is either reserved or a feature bit. */
3519 if (!is_bitwise_subset(vmx_ept_vpid_cap, data, -1ULL))
3522 vmx->nested.msrs.ept_caps = data;
3523 vmx->nested.msrs.vpid_caps = data >> 32;
3527 static int vmx_restore_fixed0_msr(struct vcpu_vmx *vmx, u32 msr_index, u64 data)
3531 switch (msr_index) {
3532 case MSR_IA32_VMX_CR0_FIXED0:
3533 msr = &vmx->nested.msrs.cr0_fixed0;
3535 case MSR_IA32_VMX_CR4_FIXED0:
3536 msr = &vmx->nested.msrs.cr4_fixed0;
3543 * 1 bits (which indicates bits which "must-be-1" during VMX operation)
3544 * must be 1 in the restored value.
3546 if (!is_bitwise_subset(data, *msr, -1ULL))
3554 * Called when userspace is restoring VMX MSRs.
3556 * Returns 0 on success, non-0 otherwise.
3558 static int vmx_set_vmx_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 data)
3560 struct vcpu_vmx *vmx = to_vmx(vcpu);
3563 * Don't allow changes to the VMX capability MSRs while the vCPU
3564 * is in VMX operation.
3566 if (vmx->nested.vmxon)
3569 switch (msr_index) {
3570 case MSR_IA32_VMX_BASIC:
3571 return vmx_restore_vmx_basic(vmx, data);
3572 case MSR_IA32_VMX_PINBASED_CTLS:
3573 case MSR_IA32_VMX_PROCBASED_CTLS:
3574 case MSR_IA32_VMX_EXIT_CTLS:
3575 case MSR_IA32_VMX_ENTRY_CTLS:
3577 * The "non-true" VMX capability MSRs are generated from the
3578 * "true" MSRs, so we do not support restoring them directly.
3580 * If userspace wants to emulate VMX_BASIC[55]=0, userspace
3581 * should restore the "true" MSRs with the must-be-1 bits
3582 * set according to the SDM Vol 3. A.2 "RESERVED CONTROLS AND
3583 * DEFAULT SETTINGS".
3586 case MSR_IA32_VMX_TRUE_PINBASED_CTLS:
3587 case MSR_IA32_VMX_TRUE_PROCBASED_CTLS:
3588 case MSR_IA32_VMX_TRUE_EXIT_CTLS:
3589 case MSR_IA32_VMX_TRUE_ENTRY_CTLS:
3590 case MSR_IA32_VMX_PROCBASED_CTLS2:
3591 return vmx_restore_control_msr(vmx, msr_index, data);
3592 case MSR_IA32_VMX_MISC:
3593 return vmx_restore_vmx_misc(vmx, data);
3594 case MSR_IA32_VMX_CR0_FIXED0:
3595 case MSR_IA32_VMX_CR4_FIXED0:
3596 return vmx_restore_fixed0_msr(vmx, msr_index, data);
3597 case MSR_IA32_VMX_CR0_FIXED1:
3598 case MSR_IA32_VMX_CR4_FIXED1:
3600 * These MSRs are generated based on the vCPU's CPUID, so we
3601 * do not support restoring them directly.
3604 case MSR_IA32_VMX_EPT_VPID_CAP:
3605 return vmx_restore_vmx_ept_vpid_cap(vmx, data);
3606 case MSR_IA32_VMX_VMCS_ENUM:
3607 vmx->nested.msrs.vmcs_enum = data;
3611 * The rest of the VMX capability MSRs do not support restore.
3617 /* Returns 0 on success, non-0 otherwise. */
3618 static int vmx_get_vmx_msr(struct nested_vmx_msrs *msrs, u32 msr_index, u64 *pdata)
3620 switch (msr_index) {
3621 case MSR_IA32_VMX_BASIC:
3622 *pdata = msrs->basic;
3624 case MSR_IA32_VMX_TRUE_PINBASED_CTLS:
3625 case MSR_IA32_VMX_PINBASED_CTLS:
3626 *pdata = vmx_control_msr(
3627 msrs->pinbased_ctls_low,
3628 msrs->pinbased_ctls_high);
3629 if (msr_index == MSR_IA32_VMX_PINBASED_CTLS)
3630 *pdata |= PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
3632 case MSR_IA32_VMX_TRUE_PROCBASED_CTLS:
3633 case MSR_IA32_VMX_PROCBASED_CTLS:
3634 *pdata = vmx_control_msr(
3635 msrs->procbased_ctls_low,
3636 msrs->procbased_ctls_high);
3637 if (msr_index == MSR_IA32_VMX_PROCBASED_CTLS)
3638 *pdata |= CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
3640 case MSR_IA32_VMX_TRUE_EXIT_CTLS:
3641 case MSR_IA32_VMX_EXIT_CTLS:
3642 *pdata = vmx_control_msr(
3643 msrs->exit_ctls_low,
3644 msrs->exit_ctls_high);
3645 if (msr_index == MSR_IA32_VMX_EXIT_CTLS)
3646 *pdata |= VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR;
3648 case MSR_IA32_VMX_TRUE_ENTRY_CTLS:
3649 case MSR_IA32_VMX_ENTRY_CTLS:
3650 *pdata = vmx_control_msr(
3651 msrs->entry_ctls_low,
3652 msrs->entry_ctls_high);
3653 if (msr_index == MSR_IA32_VMX_ENTRY_CTLS)
3654 *pdata |= VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR;
3656 case MSR_IA32_VMX_MISC:
3657 *pdata = vmx_control_msr(
3661 case MSR_IA32_VMX_CR0_FIXED0:
3662 *pdata = msrs->cr0_fixed0;
3664 case MSR_IA32_VMX_CR0_FIXED1:
3665 *pdata = msrs->cr0_fixed1;
3667 case MSR_IA32_VMX_CR4_FIXED0:
3668 *pdata = msrs->cr4_fixed0;
3670 case MSR_IA32_VMX_CR4_FIXED1:
3671 *pdata = msrs->cr4_fixed1;
3673 case MSR_IA32_VMX_VMCS_ENUM:
3674 *pdata = msrs->vmcs_enum;
3676 case MSR_IA32_VMX_PROCBASED_CTLS2:
3677 *pdata = vmx_control_msr(
3678 msrs->secondary_ctls_low,
3679 msrs->secondary_ctls_high);
3681 case MSR_IA32_VMX_EPT_VPID_CAP:
3682 *pdata = msrs->ept_caps |
3683 ((u64)msrs->vpid_caps << 32);
3685 case MSR_IA32_VMX_VMFUNC:
3686 *pdata = msrs->vmfunc_controls;
3695 static inline bool vmx_feature_control_msr_valid(struct kvm_vcpu *vcpu,
3698 uint64_t valid_bits = to_vmx(vcpu)->msr_ia32_feature_control_valid_bits;
3700 return !(val & ~valid_bits);
3703 static int vmx_get_msr_feature(struct kvm_msr_entry *msr)
3705 switch (msr->index) {
3706 case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC:
3709 return vmx_get_vmx_msr(&vmcs_config.nested, msr->index, &msr->data);
3718 * Reads an msr value (of 'msr_index') into 'pdata'.
3719 * Returns 0 on success, non-0 otherwise.
3720 * Assumes vcpu_load() was already called.
3722 static int vmx_get_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
3724 struct vcpu_vmx *vmx = to_vmx(vcpu);
3725 struct shared_msr_entry *msr;
3727 switch (msr_info->index) {
3728 #ifdef CONFIG_X86_64
3730 msr_info->data = vmcs_readl(GUEST_FS_BASE);
3733 msr_info->data = vmcs_readl(GUEST_GS_BASE);
3735 case MSR_KERNEL_GS_BASE:
3736 vmx_load_host_state(vmx);
3737 msr_info->data = vmx->msr_guest_kernel_gs_base;
3741 return kvm_get_msr_common(vcpu, msr_info);
3742 case MSR_IA32_SPEC_CTRL:
3743 if (!msr_info->host_initiated &&
3744 !guest_cpuid_has(vcpu, X86_FEATURE_SPEC_CTRL))
3747 msr_info->data = to_vmx(vcpu)->spec_ctrl;
3749 case MSR_IA32_ARCH_CAPABILITIES:
3750 if (!msr_info->host_initiated &&
3751 !guest_cpuid_has(vcpu, X86_FEATURE_ARCH_CAPABILITIES))
3753 msr_info->data = to_vmx(vcpu)->arch_capabilities;
3755 case MSR_IA32_SYSENTER_CS:
3756 msr_info->data = vmcs_read32(GUEST_SYSENTER_CS);
3758 case MSR_IA32_SYSENTER_EIP:
3759 msr_info->data = vmcs_readl(GUEST_SYSENTER_EIP);
3761 case MSR_IA32_SYSENTER_ESP:
3762 msr_info->data = vmcs_readl(GUEST_SYSENTER_ESP);
3764 case MSR_IA32_BNDCFGS:
3765 if (!kvm_mpx_supported() ||
3766 (!msr_info->host_initiated &&
3767 !guest_cpuid_has(vcpu, X86_FEATURE_MPX)))
3769 msr_info->data = vmcs_read64(GUEST_BNDCFGS);
3771 case MSR_IA32_MCG_EXT_CTL:
3772 if (!msr_info->host_initiated &&
3773 !(vmx->msr_ia32_feature_control &
3774 FEATURE_CONTROL_LMCE))
3776 msr_info->data = vcpu->arch.mcg_ext_ctl;
3778 case MSR_IA32_FEATURE_CONTROL:
3779 msr_info->data = vmx->msr_ia32_feature_control;
3781 case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC:
3782 if (!nested_vmx_allowed(vcpu))
3784 return vmx_get_vmx_msr(&vmx->nested.msrs, msr_info->index,
3787 if (!vmx_xsaves_supported())
3789 msr_info->data = vcpu->arch.ia32_xss;
3792 if (!msr_info->host_initiated &&
3793 !guest_cpuid_has(vcpu, X86_FEATURE_RDTSCP))
3795 /* Otherwise falls through */
3797 msr = find_msr_entry(vmx, msr_info->index);
3799 msr_info->data = msr->data;
3802 return kvm_get_msr_common(vcpu, msr_info);
3808 static void vmx_leave_nested(struct kvm_vcpu *vcpu);
3811 * Writes msr value into into the appropriate "register".
3812 * Returns 0 on success, non-0 otherwise.
3813 * Assumes vcpu_load() was already called.
3815 static int vmx_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
3817 struct vcpu_vmx *vmx = to_vmx(vcpu);
3818 struct shared_msr_entry *msr;
3820 u32 msr_index = msr_info->index;
3821 u64 data = msr_info->data;
3823 switch (msr_index) {
3825 ret = kvm_set_msr_common(vcpu, msr_info);
3827 #ifdef CONFIG_X86_64
3829 vmx_segment_cache_clear(vmx);
3830 vmcs_writel(GUEST_FS_BASE, data);
3833 vmx_segment_cache_clear(vmx);
3834 vmcs_writel(GUEST_GS_BASE, data);
3836 case MSR_KERNEL_GS_BASE:
3837 vmx_load_host_state(vmx);
3838 vmx->msr_guest_kernel_gs_base = data;
3841 case MSR_IA32_SYSENTER_CS:
3842 vmcs_write32(GUEST_SYSENTER_CS, data);
3844 case MSR_IA32_SYSENTER_EIP:
3845 vmcs_writel(GUEST_SYSENTER_EIP, data);
3847 case MSR_IA32_SYSENTER_ESP:
3848 vmcs_writel(GUEST_SYSENTER_ESP, data);
3850 case MSR_IA32_BNDCFGS:
3851 if (!kvm_mpx_supported() ||
3852 (!msr_info->host_initiated &&
3853 !guest_cpuid_has(vcpu, X86_FEATURE_MPX)))
3855 if (is_noncanonical_address(data & PAGE_MASK, vcpu) ||
3856 (data & MSR_IA32_BNDCFGS_RSVD))
3858 vmcs_write64(GUEST_BNDCFGS, data);
3860 case MSR_IA32_SPEC_CTRL:
3861 if (!msr_info->host_initiated &&
3862 !guest_cpuid_has(vcpu, X86_FEATURE_SPEC_CTRL))
3865 /* The STIBP bit doesn't fault even if it's not advertised */
3866 if (data & ~(SPEC_CTRL_IBRS | SPEC_CTRL_STIBP | SPEC_CTRL_SSBD))
3869 vmx->spec_ctrl = data;
3876 * When it's written (to non-zero) for the first time, pass
3880 * The handling of the MSR bitmap for L2 guests is done in
3881 * nested_vmx_merge_msr_bitmap. We should not touch the
3882 * vmcs02.msr_bitmap here since it gets completely overwritten
3883 * in the merging. We update the vmcs01 here for L1 as well
3884 * since it will end up touching the MSR anyway now.
3886 vmx_disable_intercept_for_msr(vmx->vmcs01.msr_bitmap,
3890 case MSR_IA32_PRED_CMD:
3891 if (!msr_info->host_initiated &&
3892 !guest_cpuid_has(vcpu, X86_FEATURE_SPEC_CTRL))
3895 if (data & ~PRED_CMD_IBPB)
3901 wrmsrl(MSR_IA32_PRED_CMD, PRED_CMD_IBPB);
3905 * When it's written (to non-zero) for the first time, pass
3909 * The handling of the MSR bitmap for L2 guests is done in
3910 * nested_vmx_merge_msr_bitmap. We should not touch the
3911 * vmcs02.msr_bitmap here since it gets completely overwritten
3914 vmx_disable_intercept_for_msr(vmx->vmcs01.msr_bitmap, MSR_IA32_PRED_CMD,
3917 case MSR_IA32_ARCH_CAPABILITIES:
3918 if (!msr_info->host_initiated)
3920 vmx->arch_capabilities = data;
3922 case MSR_IA32_CR_PAT:
3923 if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) {
3924 if (!kvm_mtrr_valid(vcpu, MSR_IA32_CR_PAT, data))
3926 vmcs_write64(GUEST_IA32_PAT, data);
3927 vcpu->arch.pat = data;
3930 ret = kvm_set_msr_common(vcpu, msr_info);
3932 case MSR_IA32_TSC_ADJUST:
3933 ret = kvm_set_msr_common(vcpu, msr_info);
3935 case MSR_IA32_MCG_EXT_CTL:
3936 if ((!msr_info->host_initiated &&
3937 !(to_vmx(vcpu)->msr_ia32_feature_control &
3938 FEATURE_CONTROL_LMCE)) ||
3939 (data & ~MCG_EXT_CTL_LMCE_EN))
3941 vcpu->arch.mcg_ext_ctl = data;
3943 case MSR_IA32_FEATURE_CONTROL:
3944 if (!vmx_feature_control_msr_valid(vcpu, data) ||
3945 (to_vmx(vcpu)->msr_ia32_feature_control &
3946 FEATURE_CONTROL_LOCKED && !msr_info->host_initiated))
3948 vmx->msr_ia32_feature_control = data;
3949 if (msr_info->host_initiated && data == 0)
3950 vmx_leave_nested(vcpu);
3952 case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC:
3953 if (!msr_info->host_initiated)
3954 return 1; /* they are read-only */
3955 if (!nested_vmx_allowed(vcpu))
3957 return vmx_set_vmx_msr(vcpu, msr_index, data);
3959 if (!vmx_xsaves_supported())
3962 * The only supported bit as of Skylake is bit 8, but
3963 * it is not supported on KVM.
3967 vcpu->arch.ia32_xss = data;
3968 if (vcpu->arch.ia32_xss != host_xss)
3969 add_atomic_switch_msr(vmx, MSR_IA32_XSS,
3970 vcpu->arch.ia32_xss, host_xss);
3972 clear_atomic_switch_msr(vmx, MSR_IA32_XSS);
3975 if (!msr_info->host_initiated &&
3976 !guest_cpuid_has(vcpu, X86_FEATURE_RDTSCP))
3978 /* Check reserved bit, higher 32 bits should be zero */
3979 if ((data >> 32) != 0)
3981 /* Otherwise falls through */
3983 msr = find_msr_entry(vmx, msr_index);
3985 u64 old_msr_data = msr->data;
3987 if (msr - vmx->guest_msrs < vmx->save_nmsrs) {
3989 ret = kvm_set_shared_msr(msr->index, msr->data,
3993 msr->data = old_msr_data;
3997 ret = kvm_set_msr_common(vcpu, msr_info);
4003 static void vmx_cache_reg(struct kvm_vcpu *vcpu, enum kvm_reg reg)
4005 __set_bit(reg, (unsigned long *)&vcpu->arch.regs_avail);
4008 vcpu->arch.regs[VCPU_REGS_RSP] = vmcs_readl(GUEST_RSP);
4011 vcpu->arch.regs[VCPU_REGS_RIP] = vmcs_readl(GUEST_RIP);
4013 case VCPU_EXREG_PDPTR:
4015 ept_save_pdptrs(vcpu);
4022 static __init int cpu_has_kvm_support(void)
4024 return cpu_has_vmx();
4027 static __init int vmx_disabled_by_bios(void)
4031 rdmsrl(MSR_IA32_FEATURE_CONTROL, msr);
4032 if (msr & FEATURE_CONTROL_LOCKED) {
4033 /* launched w/ TXT and VMX disabled */
4034 if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX)
4037 /* launched w/o TXT and VMX only enabled w/ TXT */
4038 if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX)
4039 && (msr & FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX)
4040 && !tboot_enabled()) {
4041 printk(KERN_WARNING "kvm: disable TXT in the BIOS or "
4042 "activate TXT before enabling KVM\n");
4045 /* launched w/o TXT and VMX disabled */
4046 if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX)
4047 && !tboot_enabled())
4054 static void kvm_cpu_vmxon(u64 addr)
4056 cr4_set_bits(X86_CR4_VMXE);
4057 intel_pt_handle_vmx(1);
4059 asm volatile (ASM_VMX_VMXON_RAX
4060 : : "a"(&addr), "m"(addr)
4064 static int hardware_enable(void)
4066 int cpu = raw_smp_processor_id();
4067 u64 phys_addr = __pa(per_cpu(vmxarea, cpu));
4070 if (cr4_read_shadow() & X86_CR4_VMXE)
4074 * This can happen if we hot-added a CPU but failed to allocate
4075 * VP assist page for it.
4077 if (static_branch_unlikely(&enable_evmcs) &&
4078 !hv_get_vp_assist_page(cpu))
4081 INIT_LIST_HEAD(&per_cpu(loaded_vmcss_on_cpu, cpu));
4082 INIT_LIST_HEAD(&per_cpu(blocked_vcpu_on_cpu, cpu));
4083 spin_lock_init(&per_cpu(blocked_vcpu_on_cpu_lock, cpu));
4086 * Now we can enable the vmclear operation in kdump
4087 * since the loaded_vmcss_on_cpu list on this cpu
4088 * has been initialized.
4090 * Though the cpu is not in VMX operation now, there
4091 * is no problem to enable the vmclear operation
4092 * for the loaded_vmcss_on_cpu list is empty!
4094 crash_enable_local_vmclear(cpu);
4096 rdmsrl(MSR_IA32_FEATURE_CONTROL, old);
4098 test_bits = FEATURE_CONTROL_LOCKED;
4099 test_bits |= FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
4100 if (tboot_enabled())
4101 test_bits |= FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX;
4103 if ((old & test_bits) != test_bits) {
4104 /* enable and lock */
4105 wrmsrl(MSR_IA32_FEATURE_CONTROL, old | test_bits);
4107 kvm_cpu_vmxon(phys_addr);
4114 static void vmclear_local_loaded_vmcss(void)
4116 int cpu = raw_smp_processor_id();
4117 struct loaded_vmcs *v, *n;
4119 list_for_each_entry_safe(v, n, &per_cpu(loaded_vmcss_on_cpu, cpu),
4120 loaded_vmcss_on_cpu_link)
4121 __loaded_vmcs_clear(v);
4125 /* Just like cpu_vmxoff(), but with the __kvm_handle_fault_on_reboot()
4128 static void kvm_cpu_vmxoff(void)
4130 asm volatile (__ex(ASM_VMX_VMXOFF) : : : "cc");
4132 intel_pt_handle_vmx(0);
4133 cr4_clear_bits(X86_CR4_VMXE);
4136 static void hardware_disable(void)
4138 vmclear_local_loaded_vmcss();
4142 static __init int adjust_vmx_controls(u32 ctl_min, u32 ctl_opt,
4143 u32 msr, u32 *result)
4145 u32 vmx_msr_low, vmx_msr_high;
4146 u32 ctl = ctl_min | ctl_opt;
4148 rdmsr(msr, vmx_msr_low, vmx_msr_high);
4150 ctl &= vmx_msr_high; /* bit == 0 in high word ==> must be zero */
4151 ctl |= vmx_msr_low; /* bit == 1 in low word ==> must be one */
4153 /* Ensure minimum (required) set of control bits are supported. */
4161 static __init bool allow_1_setting(u32 msr, u32 ctl)
4163 u32 vmx_msr_low, vmx_msr_high;
4165 rdmsr(msr, vmx_msr_low, vmx_msr_high);
4166 return vmx_msr_high & ctl;
4169 static __init int setup_vmcs_config(struct vmcs_config *vmcs_conf)
4171 u32 vmx_msr_low, vmx_msr_high;
4172 u32 min, opt, min2, opt2;
4173 u32 _pin_based_exec_control = 0;
4174 u32 _cpu_based_exec_control = 0;
4175 u32 _cpu_based_2nd_exec_control = 0;
4176 u32 _vmexit_control = 0;
4177 u32 _vmentry_control = 0;
4179 memset(vmcs_conf, 0, sizeof(*vmcs_conf));
4180 min = CPU_BASED_HLT_EXITING |
4181 #ifdef CONFIG_X86_64
4182 CPU_BASED_CR8_LOAD_EXITING |
4183 CPU_BASED_CR8_STORE_EXITING |
4185 CPU_BASED_CR3_LOAD_EXITING |
4186 CPU_BASED_CR3_STORE_EXITING |
4187 CPU_BASED_UNCOND_IO_EXITING |
4188 CPU_BASED_MOV_DR_EXITING |
4189 CPU_BASED_USE_TSC_OFFSETING |
4190 CPU_BASED_MWAIT_EXITING |
4191 CPU_BASED_MONITOR_EXITING |
4192 CPU_BASED_INVLPG_EXITING |
4193 CPU_BASED_RDPMC_EXITING;
4195 opt = CPU_BASED_TPR_SHADOW |
4196 CPU_BASED_USE_MSR_BITMAPS |
4197 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
4198 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PROCBASED_CTLS,
4199 &_cpu_based_exec_control) < 0)
4201 #ifdef CONFIG_X86_64
4202 if ((_cpu_based_exec_control & CPU_BASED_TPR_SHADOW))
4203 _cpu_based_exec_control &= ~CPU_BASED_CR8_LOAD_EXITING &
4204 ~CPU_BASED_CR8_STORE_EXITING;
4206 if (_cpu_based_exec_control & CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) {
4208 opt2 = SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
4209 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
4210 SECONDARY_EXEC_WBINVD_EXITING |
4211 SECONDARY_EXEC_ENABLE_VPID |
4212 SECONDARY_EXEC_ENABLE_EPT |
4213 SECONDARY_EXEC_UNRESTRICTED_GUEST |
4214 SECONDARY_EXEC_PAUSE_LOOP_EXITING |
4215 SECONDARY_EXEC_DESC |
4216 SECONDARY_EXEC_RDTSCP |
4217 SECONDARY_EXEC_ENABLE_INVPCID |
4218 SECONDARY_EXEC_APIC_REGISTER_VIRT |
4219 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY |
4220 SECONDARY_EXEC_SHADOW_VMCS |
4221 SECONDARY_EXEC_XSAVES |
4222 SECONDARY_EXEC_RDSEED_EXITING |
4223 SECONDARY_EXEC_RDRAND_EXITING |
4224 SECONDARY_EXEC_ENABLE_PML |
4225 SECONDARY_EXEC_TSC_SCALING |
4226 SECONDARY_EXEC_ENABLE_VMFUNC;
4227 if (adjust_vmx_controls(min2, opt2,
4228 MSR_IA32_VMX_PROCBASED_CTLS2,
4229 &_cpu_based_2nd_exec_control) < 0)
4232 #ifndef CONFIG_X86_64
4233 if (!(_cpu_based_2nd_exec_control &
4234 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES))
4235 _cpu_based_exec_control &= ~CPU_BASED_TPR_SHADOW;
4238 if (!(_cpu_based_exec_control & CPU_BASED_TPR_SHADOW))
4239 _cpu_based_2nd_exec_control &= ~(
4240 SECONDARY_EXEC_APIC_REGISTER_VIRT |
4241 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
4242 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
4244 rdmsr_safe(MSR_IA32_VMX_EPT_VPID_CAP,
4245 &vmx_capability.ept, &vmx_capability.vpid);
4247 if (_cpu_based_2nd_exec_control & SECONDARY_EXEC_ENABLE_EPT) {
4248 /* CR3 accesses and invlpg don't need to cause VM Exits when EPT
4250 _cpu_based_exec_control &= ~(CPU_BASED_CR3_LOAD_EXITING |
4251 CPU_BASED_CR3_STORE_EXITING |
4252 CPU_BASED_INVLPG_EXITING);
4253 } else if (vmx_capability.ept) {
4254 vmx_capability.ept = 0;
4255 pr_warn_once("EPT CAP should not exist if not support "
4256 "1-setting enable EPT VM-execution control\n");
4258 if (!(_cpu_based_2nd_exec_control & SECONDARY_EXEC_ENABLE_VPID) &&
4259 vmx_capability.vpid) {
4260 vmx_capability.vpid = 0;
4261 pr_warn_once("VPID CAP should not exist if not support "
4262 "1-setting enable VPID VM-execution control\n");
4265 min = VM_EXIT_SAVE_DEBUG_CONTROLS | VM_EXIT_ACK_INTR_ON_EXIT;
4266 #ifdef CONFIG_X86_64
4267 min |= VM_EXIT_HOST_ADDR_SPACE_SIZE;
4269 opt = VM_EXIT_SAVE_IA32_PAT | VM_EXIT_LOAD_IA32_PAT |
4270 VM_EXIT_CLEAR_BNDCFGS;
4271 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_EXIT_CTLS,
4272 &_vmexit_control) < 0)
4275 min = PIN_BASED_EXT_INTR_MASK | PIN_BASED_NMI_EXITING;
4276 opt = PIN_BASED_VIRTUAL_NMIS | PIN_BASED_POSTED_INTR |
4277 PIN_BASED_VMX_PREEMPTION_TIMER;
4278 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PINBASED_CTLS,
4279 &_pin_based_exec_control) < 0)
4282 if (cpu_has_broken_vmx_preemption_timer())
4283 _pin_based_exec_control &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
4284 if (!(_cpu_based_2nd_exec_control &
4285 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY))
4286 _pin_based_exec_control &= ~PIN_BASED_POSTED_INTR;
4288 min = VM_ENTRY_LOAD_DEBUG_CONTROLS;
4289 opt = VM_ENTRY_LOAD_IA32_PAT | VM_ENTRY_LOAD_BNDCFGS;
4290 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_ENTRY_CTLS,
4291 &_vmentry_control) < 0)
4294 rdmsr(MSR_IA32_VMX_BASIC, vmx_msr_low, vmx_msr_high);
4296 /* IA-32 SDM Vol 3B: VMCS size is never greater than 4kB. */
4297 if ((vmx_msr_high & 0x1fff) > PAGE_SIZE)
4300 #ifdef CONFIG_X86_64
4301 /* IA-32 SDM Vol 3B: 64-bit CPUs always have VMX_BASIC_MSR[48]==0. */
4302 if (vmx_msr_high & (1u<<16))
4306 /* Require Write-Back (WB) memory type for VMCS accesses. */
4307 if (((vmx_msr_high >> 18) & 15) != 6)
4310 vmcs_conf->size = vmx_msr_high & 0x1fff;
4311 vmcs_conf->order = get_order(vmcs_conf->size);
4312 vmcs_conf->basic_cap = vmx_msr_high & ~0x1fff;
4314 /* KVM supports Enlightened VMCS v1 only */
4315 if (static_branch_unlikely(&enable_evmcs))
4316 vmcs_conf->revision_id = KVM_EVMCS_VERSION;
4318 vmcs_conf->revision_id = vmx_msr_low;
4320 vmcs_conf->pin_based_exec_ctrl = _pin_based_exec_control;
4321 vmcs_conf->cpu_based_exec_ctrl = _cpu_based_exec_control;
4322 vmcs_conf->cpu_based_2nd_exec_ctrl = _cpu_based_2nd_exec_control;
4323 vmcs_conf->vmexit_ctrl = _vmexit_control;
4324 vmcs_conf->vmentry_ctrl = _vmentry_control;
4326 if (static_branch_unlikely(&enable_evmcs))
4327 evmcs_sanitize_exec_ctrls(vmcs_conf);
4329 cpu_has_load_ia32_efer =
4330 allow_1_setting(MSR_IA32_VMX_ENTRY_CTLS,
4331 VM_ENTRY_LOAD_IA32_EFER)
4332 && allow_1_setting(MSR_IA32_VMX_EXIT_CTLS,
4333 VM_EXIT_LOAD_IA32_EFER);
4335 cpu_has_load_perf_global_ctrl =
4336 allow_1_setting(MSR_IA32_VMX_ENTRY_CTLS,
4337 VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL)
4338 && allow_1_setting(MSR_IA32_VMX_EXIT_CTLS,
4339 VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL);
4342 * Some cpus support VM_ENTRY_(LOAD|SAVE)_IA32_PERF_GLOBAL_CTRL
4343 * but due to errata below it can't be used. Workaround is to use
4344 * msr load mechanism to switch IA32_PERF_GLOBAL_CTRL.
4346 * VM Exit May Incorrectly Clear IA32_PERF_GLOBAL_CTRL [34:32]
4351 * BC86,AAY89,BD102 (model 44)
4355 if (cpu_has_load_perf_global_ctrl && boot_cpu_data.x86 == 0x6) {
4356 switch (boot_cpu_data.x86_model) {
4362 cpu_has_load_perf_global_ctrl = false;
4363 printk_once(KERN_WARNING"kvm: VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL "
4364 "does not work properly. Using workaround\n");
4371 if (boot_cpu_has(X86_FEATURE_XSAVES))
4372 rdmsrl(MSR_IA32_XSS, host_xss);
4377 static struct vmcs *alloc_vmcs_cpu(int cpu)
4379 int node = cpu_to_node(cpu);
4383 pages = __alloc_pages_node(node, GFP_KERNEL, vmcs_config.order);
4386 vmcs = page_address(pages);
4387 memset(vmcs, 0, vmcs_config.size);
4388 vmcs->revision_id = vmcs_config.revision_id; /* vmcs revision id */
4392 static void free_vmcs(struct vmcs *vmcs)
4394 free_pages((unsigned long)vmcs, vmcs_config.order);
4398 * Free a VMCS, but before that VMCLEAR it on the CPU where it was last loaded
4400 static void free_loaded_vmcs(struct loaded_vmcs *loaded_vmcs)
4402 if (!loaded_vmcs->vmcs)
4404 loaded_vmcs_clear(loaded_vmcs);
4405 free_vmcs(loaded_vmcs->vmcs);
4406 loaded_vmcs->vmcs = NULL;
4407 if (loaded_vmcs->msr_bitmap)
4408 free_page((unsigned long)loaded_vmcs->msr_bitmap);
4409 WARN_ON(loaded_vmcs->shadow_vmcs != NULL);
4412 static struct vmcs *alloc_vmcs(void)
4414 return alloc_vmcs_cpu(raw_smp_processor_id());
4417 static int alloc_loaded_vmcs(struct loaded_vmcs *loaded_vmcs)
4419 loaded_vmcs->vmcs = alloc_vmcs();
4420 if (!loaded_vmcs->vmcs)
4423 loaded_vmcs->shadow_vmcs = NULL;
4424 loaded_vmcs_init(loaded_vmcs);
4426 if (cpu_has_vmx_msr_bitmap()) {
4427 loaded_vmcs->msr_bitmap = (unsigned long *)__get_free_page(GFP_KERNEL);
4428 if (!loaded_vmcs->msr_bitmap)
4430 memset(loaded_vmcs->msr_bitmap, 0xff, PAGE_SIZE);
4432 if (IS_ENABLED(CONFIG_HYPERV) &&
4433 static_branch_unlikely(&enable_evmcs) &&
4434 (ms_hyperv.nested_features & HV_X64_NESTED_MSR_BITMAP)) {
4435 struct hv_enlightened_vmcs *evmcs =
4436 (struct hv_enlightened_vmcs *)loaded_vmcs->vmcs;
4438 evmcs->hv_enlightenments_control.msr_bitmap = 1;
4444 free_loaded_vmcs(loaded_vmcs);
4448 static void free_kvm_area(void)
4452 for_each_possible_cpu(cpu) {
4453 free_vmcs(per_cpu(vmxarea, cpu));
4454 per_cpu(vmxarea, cpu) = NULL;
4458 enum vmcs_field_width {
4459 VMCS_FIELD_WIDTH_U16 = 0,
4460 VMCS_FIELD_WIDTH_U64 = 1,
4461 VMCS_FIELD_WIDTH_U32 = 2,
4462 VMCS_FIELD_WIDTH_NATURAL_WIDTH = 3
4465 static inline int vmcs_field_width(unsigned long field)
4467 if (0x1 & field) /* the *_HIGH fields are all 32 bit */
4468 return VMCS_FIELD_WIDTH_U32;
4469 return (field >> 13) & 0x3 ;
4472 static inline int vmcs_field_readonly(unsigned long field)
4474 return (((field >> 10) & 0x3) == 1);
4477 static void init_vmcs_shadow_fields(void)
4481 for (i = j = 0; i < max_shadow_read_only_fields; i++) {
4482 u16 field = shadow_read_only_fields[i];
4483 if (vmcs_field_width(field) == VMCS_FIELD_WIDTH_U64 &&
4484 (i + 1 == max_shadow_read_only_fields ||
4485 shadow_read_only_fields[i + 1] != field + 1))
4486 pr_err("Missing field from shadow_read_only_field %x\n",
4489 clear_bit(field, vmx_vmread_bitmap);
4490 #ifdef CONFIG_X86_64
4495 shadow_read_only_fields[j] = field;
4498 max_shadow_read_only_fields = j;
4500 for (i = j = 0; i < max_shadow_read_write_fields; i++) {
4501 u16 field = shadow_read_write_fields[i];
4502 if (vmcs_field_width(field) == VMCS_FIELD_WIDTH_U64 &&
4503 (i + 1 == max_shadow_read_write_fields ||
4504 shadow_read_write_fields[i + 1] != field + 1))
4505 pr_err("Missing field from shadow_read_write_field %x\n",
4509 * PML and the preemption timer can be emulated, but the
4510 * processor cannot vmwrite to fields that don't exist
4514 case GUEST_PML_INDEX:
4515 if (!cpu_has_vmx_pml())
4518 case VMX_PREEMPTION_TIMER_VALUE:
4519 if (!cpu_has_vmx_preemption_timer())
4522 case GUEST_INTR_STATUS:
4523 if (!cpu_has_vmx_apicv())
4530 clear_bit(field, vmx_vmwrite_bitmap);
4531 clear_bit(field, vmx_vmread_bitmap);
4532 #ifdef CONFIG_X86_64
4537 shadow_read_write_fields[j] = field;
4540 max_shadow_read_write_fields = j;
4543 static __init int alloc_kvm_area(void)
4547 for_each_possible_cpu(cpu) {
4550 vmcs = alloc_vmcs_cpu(cpu);
4556 per_cpu(vmxarea, cpu) = vmcs;
4561 static void fix_pmode_seg(struct kvm_vcpu *vcpu, int seg,
4562 struct kvm_segment *save)
4564 if (!emulate_invalid_guest_state) {
4566 * CS and SS RPL should be equal during guest entry according
4567 * to VMX spec, but in reality it is not always so. Since vcpu
4568 * is in the middle of the transition from real mode to
4569 * protected mode it is safe to assume that RPL 0 is a good
4572 if (seg == VCPU_SREG_CS || seg == VCPU_SREG_SS)
4573 save->selector &= ~SEGMENT_RPL_MASK;
4574 save->dpl = save->selector & SEGMENT_RPL_MASK;
4577 vmx_set_segment(vcpu, save, seg);
4580 static void enter_pmode(struct kvm_vcpu *vcpu)
4582 unsigned long flags;
4583 struct vcpu_vmx *vmx = to_vmx(vcpu);
4586 * Update real mode segment cache. It may be not up-to-date if sement
4587 * register was written while vcpu was in a guest mode.
4589 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_ES], VCPU_SREG_ES);
4590 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_DS], VCPU_SREG_DS);
4591 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_FS], VCPU_SREG_FS);
4592 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_GS], VCPU_SREG_GS);
4593 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_SS], VCPU_SREG_SS);
4594 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_CS], VCPU_SREG_CS);
4596 vmx->rmode.vm86_active = 0;
4598 vmx_segment_cache_clear(vmx);
4600 vmx_set_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_TR], VCPU_SREG_TR);
4602 flags = vmcs_readl(GUEST_RFLAGS);
4603 flags &= RMODE_GUEST_OWNED_EFLAGS_BITS;
4604 flags |= vmx->rmode.save_rflags & ~RMODE_GUEST_OWNED_EFLAGS_BITS;
4605 vmcs_writel(GUEST_RFLAGS, flags);
4607 vmcs_writel(GUEST_CR4, (vmcs_readl(GUEST_CR4) & ~X86_CR4_VME) |
4608 (vmcs_readl(CR4_READ_SHADOW) & X86_CR4_VME));
4610 update_exception_bitmap(vcpu);
4612 fix_pmode_seg(vcpu, VCPU_SREG_CS, &vmx->rmode.segs[VCPU_SREG_CS]);
4613 fix_pmode_seg(vcpu, VCPU_SREG_SS, &vmx->rmode.segs[VCPU_SREG_SS]);
4614 fix_pmode_seg(vcpu, VCPU_SREG_ES, &vmx->rmode.segs[VCPU_SREG_ES]);
4615 fix_pmode_seg(vcpu, VCPU_SREG_DS, &vmx->rmode.segs[VCPU_SREG_DS]);
4616 fix_pmode_seg(vcpu, VCPU_SREG_FS, &vmx->rmode.segs[VCPU_SREG_FS]);
4617 fix_pmode_seg(vcpu, VCPU_SREG_GS, &vmx->rmode.segs[VCPU_SREG_GS]);
4620 static void fix_rmode_seg(int seg, struct kvm_segment *save)
4622 const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
4623 struct kvm_segment var = *save;
4626 if (seg == VCPU_SREG_CS)
4629 if (!emulate_invalid_guest_state) {
4630 var.selector = var.base >> 4;
4631 var.base = var.base & 0xffff0;
4641 if (save->base & 0xf)
4642 printk_once(KERN_WARNING "kvm: segment base is not "
4643 "paragraph aligned when entering "
4644 "protected mode (seg=%d)", seg);
4647 vmcs_write16(sf->selector, var.selector);
4648 vmcs_writel(sf->base, var.base);
4649 vmcs_write32(sf->limit, var.limit);
4650 vmcs_write32(sf->ar_bytes, vmx_segment_access_rights(&var));
4653 static void enter_rmode(struct kvm_vcpu *vcpu)
4655 unsigned long flags;
4656 struct vcpu_vmx *vmx = to_vmx(vcpu);
4657 struct kvm_vmx *kvm_vmx = to_kvm_vmx(vcpu->kvm);
4659 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_TR], VCPU_SREG_TR);
4660 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_ES], VCPU_SREG_ES);
4661 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_DS], VCPU_SREG_DS);
4662 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_FS], VCPU_SREG_FS);
4663 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_GS], VCPU_SREG_GS);
4664 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_SS], VCPU_SREG_SS);
4665 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_CS], VCPU_SREG_CS);
4667 vmx->rmode.vm86_active = 1;
4670 * Very old userspace does not call KVM_SET_TSS_ADDR before entering
4671 * vcpu. Warn the user that an update is overdue.
4673 if (!kvm_vmx->tss_addr)
4674 printk_once(KERN_WARNING "kvm: KVM_SET_TSS_ADDR need to be "
4675 "called before entering vcpu\n");
4677 vmx_segment_cache_clear(vmx);
4679 vmcs_writel(GUEST_TR_BASE, kvm_vmx->tss_addr);
4680 vmcs_write32(GUEST_TR_LIMIT, RMODE_TSS_SIZE - 1);
4681 vmcs_write32(GUEST_TR_AR_BYTES, 0x008b);
4683 flags = vmcs_readl(GUEST_RFLAGS);
4684 vmx->rmode.save_rflags = flags;
4686 flags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM;
4688 vmcs_writel(GUEST_RFLAGS, flags);
4689 vmcs_writel(GUEST_CR4, vmcs_readl(GUEST_CR4) | X86_CR4_VME);
4690 update_exception_bitmap(vcpu);
4692 fix_rmode_seg(VCPU_SREG_SS, &vmx->rmode.segs[VCPU_SREG_SS]);
4693 fix_rmode_seg(VCPU_SREG_CS, &vmx->rmode.segs[VCPU_SREG_CS]);
4694 fix_rmode_seg(VCPU_SREG_ES, &vmx->rmode.segs[VCPU_SREG_ES]);
4695 fix_rmode_seg(VCPU_SREG_DS, &vmx->rmode.segs[VCPU_SREG_DS]);
4696 fix_rmode_seg(VCPU_SREG_GS, &vmx->rmode.segs[VCPU_SREG_GS]);
4697 fix_rmode_seg(VCPU_SREG_FS, &vmx->rmode.segs[VCPU_SREG_FS]);
4699 kvm_mmu_reset_context(vcpu);
4702 static void vmx_set_efer(struct kvm_vcpu *vcpu, u64 efer)
4704 struct vcpu_vmx *vmx = to_vmx(vcpu);
4705 struct shared_msr_entry *msr = find_msr_entry(vmx, MSR_EFER);
4711 * Force kernel_gs_base reloading before EFER changes, as control
4712 * of this msr depends on is_long_mode().
4714 vmx_load_host_state(to_vmx(vcpu));
4715 vcpu->arch.efer = efer;
4716 if (efer & EFER_LMA) {
4717 vm_entry_controls_setbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
4720 vm_entry_controls_clearbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
4722 msr->data = efer & ~EFER_LME;
4727 #ifdef CONFIG_X86_64
4729 static void enter_lmode(struct kvm_vcpu *vcpu)
4733 vmx_segment_cache_clear(to_vmx(vcpu));
4735 guest_tr_ar = vmcs_read32(GUEST_TR_AR_BYTES);
4736 if ((guest_tr_ar & VMX_AR_TYPE_MASK) != VMX_AR_TYPE_BUSY_64_TSS) {
4737 pr_debug_ratelimited("%s: tss fixup for long mode. \n",
4739 vmcs_write32(GUEST_TR_AR_BYTES,
4740 (guest_tr_ar & ~VMX_AR_TYPE_MASK)
4741 | VMX_AR_TYPE_BUSY_64_TSS);
4743 vmx_set_efer(vcpu, vcpu->arch.efer | EFER_LMA);
4746 static void exit_lmode(struct kvm_vcpu *vcpu)
4748 vm_entry_controls_clearbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
4749 vmx_set_efer(vcpu, vcpu->arch.efer & ~EFER_LMA);
4754 static inline void __vmx_flush_tlb(struct kvm_vcpu *vcpu, int vpid,
4755 bool invalidate_gpa)
4757 if (enable_ept && (invalidate_gpa || !enable_vpid)) {
4758 if (!VALID_PAGE(vcpu->arch.mmu.root_hpa))
4760 ept_sync_context(construct_eptp(vcpu, vcpu->arch.mmu.root_hpa));
4762 vpid_sync_context(vpid);
4766 static void vmx_flush_tlb(struct kvm_vcpu *vcpu, bool invalidate_gpa)
4768 __vmx_flush_tlb(vcpu, to_vmx(vcpu)->vpid, invalidate_gpa);
4771 static void vmx_decache_cr0_guest_bits(struct kvm_vcpu *vcpu)
4773 ulong cr0_guest_owned_bits = vcpu->arch.cr0_guest_owned_bits;
4775 vcpu->arch.cr0 &= ~cr0_guest_owned_bits;
4776 vcpu->arch.cr0 |= vmcs_readl(GUEST_CR0) & cr0_guest_owned_bits;
4779 static void vmx_decache_cr3(struct kvm_vcpu *vcpu)
4781 if (enable_unrestricted_guest || (enable_ept && is_paging(vcpu)))
4782 vcpu->arch.cr3 = vmcs_readl(GUEST_CR3);
4783 __set_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail);
4786 static void vmx_decache_cr4_guest_bits(struct kvm_vcpu *vcpu)
4788 ulong cr4_guest_owned_bits = vcpu->arch.cr4_guest_owned_bits;
4790 vcpu->arch.cr4 &= ~cr4_guest_owned_bits;
4791 vcpu->arch.cr4 |= vmcs_readl(GUEST_CR4) & cr4_guest_owned_bits;
4794 static void ept_load_pdptrs(struct kvm_vcpu *vcpu)
4796 struct kvm_mmu *mmu = vcpu->arch.walk_mmu;
4798 if (!test_bit(VCPU_EXREG_PDPTR,
4799 (unsigned long *)&vcpu->arch.regs_dirty))
4802 if (is_paging(vcpu) && is_pae(vcpu) && !is_long_mode(vcpu)) {
4803 vmcs_write64(GUEST_PDPTR0, mmu->pdptrs[0]);
4804 vmcs_write64(GUEST_PDPTR1, mmu->pdptrs[1]);
4805 vmcs_write64(GUEST_PDPTR2, mmu->pdptrs[2]);
4806 vmcs_write64(GUEST_PDPTR3, mmu->pdptrs[3]);
4810 static void ept_save_pdptrs(struct kvm_vcpu *vcpu)
4812 struct kvm_mmu *mmu = vcpu->arch.walk_mmu;
4814 if (is_paging(vcpu) && is_pae(vcpu) && !is_long_mode(vcpu)) {
4815 mmu->pdptrs[0] = vmcs_read64(GUEST_PDPTR0);
4816 mmu->pdptrs[1] = vmcs_read64(GUEST_PDPTR1);
4817 mmu->pdptrs[2] = vmcs_read64(GUEST_PDPTR2);
4818 mmu->pdptrs[3] = vmcs_read64(GUEST_PDPTR3);
4821 __set_bit(VCPU_EXREG_PDPTR,
4822 (unsigned long *)&vcpu->arch.regs_avail);
4823 __set_bit(VCPU_EXREG_PDPTR,
4824 (unsigned long *)&vcpu->arch.regs_dirty);
4827 static bool nested_guest_cr0_valid(struct kvm_vcpu *vcpu, unsigned long val)
4829 u64 fixed0 = to_vmx(vcpu)->nested.msrs.cr0_fixed0;
4830 u64 fixed1 = to_vmx(vcpu)->nested.msrs.cr0_fixed1;
4831 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
4833 if (to_vmx(vcpu)->nested.msrs.secondary_ctls_high &
4834 SECONDARY_EXEC_UNRESTRICTED_GUEST &&
4835 nested_cpu_has2(vmcs12, SECONDARY_EXEC_UNRESTRICTED_GUEST))
4836 fixed0 &= ~(X86_CR0_PE | X86_CR0_PG);
4838 return fixed_bits_valid(val, fixed0, fixed1);
4841 static bool nested_host_cr0_valid(struct kvm_vcpu *vcpu, unsigned long val)
4843 u64 fixed0 = to_vmx(vcpu)->nested.msrs.cr0_fixed0;
4844 u64 fixed1 = to_vmx(vcpu)->nested.msrs.cr0_fixed1;
4846 return fixed_bits_valid(val, fixed0, fixed1);
4849 static bool nested_cr4_valid(struct kvm_vcpu *vcpu, unsigned long val)
4851 u64 fixed0 = to_vmx(vcpu)->nested.msrs.cr4_fixed0;
4852 u64 fixed1 = to_vmx(vcpu)->nested.msrs.cr4_fixed1;
4854 return fixed_bits_valid(val, fixed0, fixed1);
4857 /* No difference in the restrictions on guest and host CR4 in VMX operation. */
4858 #define nested_guest_cr4_valid nested_cr4_valid
4859 #define nested_host_cr4_valid nested_cr4_valid
4861 static int vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4);
4863 static void ept_update_paging_mode_cr0(unsigned long *hw_cr0,
4865 struct kvm_vcpu *vcpu)
4867 if (!test_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail))
4868 vmx_decache_cr3(vcpu);
4869 if (!(cr0 & X86_CR0_PG)) {
4870 /* From paging/starting to nonpaging */
4871 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL,
4872 vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) |
4873 (CPU_BASED_CR3_LOAD_EXITING |
4874 CPU_BASED_CR3_STORE_EXITING));
4875 vcpu->arch.cr0 = cr0;
4876 vmx_set_cr4(vcpu, kvm_read_cr4(vcpu));
4877 } else if (!is_paging(vcpu)) {
4878 /* From nonpaging to paging */
4879 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL,
4880 vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) &
4881 ~(CPU_BASED_CR3_LOAD_EXITING |
4882 CPU_BASED_CR3_STORE_EXITING));
4883 vcpu->arch.cr0 = cr0;
4884 vmx_set_cr4(vcpu, kvm_read_cr4(vcpu));
4887 if (!(cr0 & X86_CR0_WP))
4888 *hw_cr0 &= ~X86_CR0_WP;
4891 static void vmx_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
4893 struct vcpu_vmx *vmx = to_vmx(vcpu);
4894 unsigned long hw_cr0;
4896 hw_cr0 = (cr0 & ~KVM_GUEST_CR0_MASK);
4897 if (enable_unrestricted_guest)
4898 hw_cr0 |= KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST;
4900 hw_cr0 |= KVM_VM_CR0_ALWAYS_ON;
4902 if (vmx->rmode.vm86_active && (cr0 & X86_CR0_PE))
4905 if (!vmx->rmode.vm86_active && !(cr0 & X86_CR0_PE))
4909 #ifdef CONFIG_X86_64
4910 if (vcpu->arch.efer & EFER_LME) {
4911 if (!is_paging(vcpu) && (cr0 & X86_CR0_PG))
4913 if (is_paging(vcpu) && !(cr0 & X86_CR0_PG))
4918 if (enable_ept && !enable_unrestricted_guest)
4919 ept_update_paging_mode_cr0(&hw_cr0, cr0, vcpu);
4921 vmcs_writel(CR0_READ_SHADOW, cr0);
4922 vmcs_writel(GUEST_CR0, hw_cr0);
4923 vcpu->arch.cr0 = cr0;
4925 /* depends on vcpu->arch.cr0 to be set to a new value */
4926 vmx->emulation_required = emulation_required(vcpu);
4929 static int get_ept_level(struct kvm_vcpu *vcpu)
4931 if (cpu_has_vmx_ept_5levels() && (cpuid_maxphyaddr(vcpu) > 48))
4936 static u64 construct_eptp(struct kvm_vcpu *vcpu, unsigned long root_hpa)
4938 u64 eptp = VMX_EPTP_MT_WB;
4940 eptp |= (get_ept_level(vcpu) == 5) ? VMX_EPTP_PWL_5 : VMX_EPTP_PWL_4;
4942 if (enable_ept_ad_bits &&
4943 (!is_guest_mode(vcpu) || nested_ept_ad_enabled(vcpu)))
4944 eptp |= VMX_EPTP_AD_ENABLE_BIT;
4945 eptp |= (root_hpa & PAGE_MASK);
4950 static void vmx_set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3)
4952 unsigned long guest_cr3;
4957 eptp = construct_eptp(vcpu, cr3);
4958 vmcs_write64(EPT_POINTER, eptp);
4959 if (enable_unrestricted_guest || is_paging(vcpu) ||
4960 is_guest_mode(vcpu))
4961 guest_cr3 = kvm_read_cr3(vcpu);
4963 guest_cr3 = to_kvm_vmx(vcpu->kvm)->ept_identity_map_addr;
4964 ept_load_pdptrs(vcpu);
4967 vmx_flush_tlb(vcpu, true);
4968 vmcs_writel(GUEST_CR3, guest_cr3);
4971 static int vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
4974 * Pass through host's Machine Check Enable value to hw_cr4, which
4975 * is in force while we are in guest mode. Do not let guests control
4976 * this bit, even if host CR4.MCE == 0.
4978 unsigned long hw_cr4;
4980 hw_cr4 = (cr4_read_shadow() & X86_CR4_MCE) | (cr4 & ~X86_CR4_MCE);
4981 if (enable_unrestricted_guest)
4982 hw_cr4 |= KVM_VM_CR4_ALWAYS_ON_UNRESTRICTED_GUEST;
4983 else if (to_vmx(vcpu)->rmode.vm86_active)
4984 hw_cr4 |= KVM_RMODE_VM_CR4_ALWAYS_ON;
4986 hw_cr4 |= KVM_PMODE_VM_CR4_ALWAYS_ON;
4988 if (!boot_cpu_has(X86_FEATURE_UMIP) && vmx_umip_emulated()) {
4989 if (cr4 & X86_CR4_UMIP) {
4990 vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL,
4991 SECONDARY_EXEC_DESC);
4992 hw_cr4 &= ~X86_CR4_UMIP;
4993 } else if (!is_guest_mode(vcpu) ||
4994 !nested_cpu_has2(get_vmcs12(vcpu), SECONDARY_EXEC_DESC))
4995 vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL,
4996 SECONDARY_EXEC_DESC);
4999 if (cr4 & X86_CR4_VMXE) {
5001 * To use VMXON (and later other VMX instructions), a guest
5002 * must first be able to turn on cr4.VMXE (see handle_vmon()).
5003 * So basically the check on whether to allow nested VMX
5006 if (!nested_vmx_allowed(vcpu))
5010 if (to_vmx(vcpu)->nested.vmxon && !nested_cr4_valid(vcpu, cr4))
5013 vcpu->arch.cr4 = cr4;
5015 if (!enable_unrestricted_guest) {
5017 if (!is_paging(vcpu)) {
5018 hw_cr4 &= ~X86_CR4_PAE;
5019 hw_cr4 |= X86_CR4_PSE;
5020 } else if (!(cr4 & X86_CR4_PAE)) {
5021 hw_cr4 &= ~X86_CR4_PAE;
5026 * SMEP/SMAP/PKU is disabled if CPU is in non-paging mode in
5027 * hardware. To emulate this behavior, SMEP/SMAP/PKU needs
5028 * to be manually disabled when guest switches to non-paging
5031 * If !enable_unrestricted_guest, the CPU is always running
5032 * with CR0.PG=1 and CR4 needs to be modified.
5033 * If enable_unrestricted_guest, the CPU automatically
5034 * disables SMEP/SMAP/PKU when the guest sets CR0.PG=0.
5036 if (!is_paging(vcpu))
5037 hw_cr4 &= ~(X86_CR4_SMEP | X86_CR4_SMAP | X86_CR4_PKE);
5040 vmcs_writel(CR4_READ_SHADOW, cr4);
5041 vmcs_writel(GUEST_CR4, hw_cr4);
5045 static void vmx_get_segment(struct kvm_vcpu *vcpu,
5046 struct kvm_segment *var, int seg)
5048 struct vcpu_vmx *vmx = to_vmx(vcpu);
5051 if (vmx->rmode.vm86_active && seg != VCPU_SREG_LDTR) {
5052 *var = vmx->rmode.segs[seg];
5053 if (seg == VCPU_SREG_TR
5054 || var->selector == vmx_read_guest_seg_selector(vmx, seg))
5056 var->base = vmx_read_guest_seg_base(vmx, seg);
5057 var->selector = vmx_read_guest_seg_selector(vmx, seg);
5060 var->base = vmx_read_guest_seg_base(vmx, seg);
5061 var->limit = vmx_read_guest_seg_limit(vmx, seg);
5062 var->selector = vmx_read_guest_seg_selector(vmx, seg);
5063 ar = vmx_read_guest_seg_ar(vmx, seg);
5064 var->unusable = (ar >> 16) & 1;
5065 var->type = ar & 15;
5066 var->s = (ar >> 4) & 1;
5067 var->dpl = (ar >> 5) & 3;
5069 * Some userspaces do not preserve unusable property. Since usable
5070 * segment has to be present according to VMX spec we can use present
5071 * property to amend userspace bug by making unusable segment always
5072 * nonpresent. vmx_segment_access_rights() already marks nonpresent
5073 * segment as unusable.
5075 var->present = !var->unusable;
5076 var->avl = (ar >> 12) & 1;
5077 var->l = (ar >> 13) & 1;
5078 var->db = (ar >> 14) & 1;
5079 var->g = (ar >> 15) & 1;
5082 static u64 vmx_get_segment_base(struct kvm_vcpu *vcpu, int seg)
5084 struct kvm_segment s;
5086 if (to_vmx(vcpu)->rmode.vm86_active) {
5087 vmx_get_segment(vcpu, &s, seg);
5090 return vmx_read_guest_seg_base(to_vmx(vcpu), seg);
5093 static int vmx_get_cpl(struct kvm_vcpu *vcpu)
5095 struct vcpu_vmx *vmx = to_vmx(vcpu);
5097 if (unlikely(vmx->rmode.vm86_active))
5100 int ar = vmx_read_guest_seg_ar(vmx, VCPU_SREG_SS);
5101 return VMX_AR_DPL(ar);
5105 static u32 vmx_segment_access_rights(struct kvm_segment *var)
5109 if (var->unusable || !var->present)
5112 ar = var->type & 15;
5113 ar |= (var->s & 1) << 4;
5114 ar |= (var->dpl & 3) << 5;
5115 ar |= (var->present & 1) << 7;
5116 ar |= (var->avl & 1) << 12;
5117 ar |= (var->l & 1) << 13;
5118 ar |= (var->db & 1) << 14;
5119 ar |= (var->g & 1) << 15;
5125 static void vmx_set_segment(struct kvm_vcpu *vcpu,
5126 struct kvm_segment *var, int seg)
5128 struct vcpu_vmx *vmx = to_vmx(vcpu);
5129 const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
5131 vmx_segment_cache_clear(vmx);
5133 if (vmx->rmode.vm86_active && seg != VCPU_SREG_LDTR) {
5134 vmx->rmode.segs[seg] = *var;
5135 if (seg == VCPU_SREG_TR)
5136 vmcs_write16(sf->selector, var->selector);
5138 fix_rmode_seg(seg, &vmx->rmode.segs[seg]);
5142 vmcs_writel(sf->base, var->base);
5143 vmcs_write32(sf->limit, var->limit);
5144 vmcs_write16(sf->selector, var->selector);
5147 * Fix the "Accessed" bit in AR field of segment registers for older
5149 * IA32 arch specifies that at the time of processor reset the
5150 * "Accessed" bit in the AR field of segment registers is 1. And qemu
5151 * is setting it to 0 in the userland code. This causes invalid guest
5152 * state vmexit when "unrestricted guest" mode is turned on.
5153 * Fix for this setup issue in cpu_reset is being pushed in the qemu
5154 * tree. Newer qemu binaries with that qemu fix would not need this
5157 if (enable_unrestricted_guest && (seg != VCPU_SREG_LDTR))
5158 var->type |= 0x1; /* Accessed */
5160 vmcs_write32(sf->ar_bytes, vmx_segment_access_rights(var));
5163 vmx->emulation_required = emulation_required(vcpu);
5166 static void vmx_get_cs_db_l_bits(struct kvm_vcpu *vcpu, int *db, int *l)
5168 u32 ar = vmx_read_guest_seg_ar(to_vmx(vcpu), VCPU_SREG_CS);
5170 *db = (ar >> 14) & 1;
5171 *l = (ar >> 13) & 1;
5174 static void vmx_get_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
5176 dt->size = vmcs_read32(GUEST_IDTR_LIMIT);
5177 dt->address = vmcs_readl(GUEST_IDTR_BASE);
5180 static void vmx_set_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
5182 vmcs_write32(GUEST_IDTR_LIMIT, dt->size);
5183 vmcs_writel(GUEST_IDTR_BASE, dt->address);
5186 static void vmx_get_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
5188 dt->size = vmcs_read32(GUEST_GDTR_LIMIT);
5189 dt->address = vmcs_readl(GUEST_GDTR_BASE);
5192 static void vmx_set_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
5194 vmcs_write32(GUEST_GDTR_LIMIT, dt->size);
5195 vmcs_writel(GUEST_GDTR_BASE, dt->address);
5198 static bool rmode_segment_valid(struct kvm_vcpu *vcpu, int seg)
5200 struct kvm_segment var;
5203 vmx_get_segment(vcpu, &var, seg);
5205 if (seg == VCPU_SREG_CS)
5207 ar = vmx_segment_access_rights(&var);
5209 if (var.base != (var.selector << 4))
5211 if (var.limit != 0xffff)
5219 static bool code_segment_valid(struct kvm_vcpu *vcpu)
5221 struct kvm_segment cs;
5222 unsigned int cs_rpl;
5224 vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
5225 cs_rpl = cs.selector & SEGMENT_RPL_MASK;
5229 if (~cs.type & (VMX_AR_TYPE_CODE_MASK|VMX_AR_TYPE_ACCESSES_MASK))
5233 if (cs.type & VMX_AR_TYPE_WRITEABLE_MASK) {
5234 if (cs.dpl > cs_rpl)
5237 if (cs.dpl != cs_rpl)
5243 /* TODO: Add Reserved field check, this'll require a new member in the kvm_segment_field structure */
5247 static bool stack_segment_valid(struct kvm_vcpu *vcpu)
5249 struct kvm_segment ss;
5250 unsigned int ss_rpl;
5252 vmx_get_segment(vcpu, &ss, VCPU_SREG_SS);
5253 ss_rpl = ss.selector & SEGMENT_RPL_MASK;
5257 if (ss.type != 3 && ss.type != 7)
5261 if (ss.dpl != ss_rpl) /* DPL != RPL */
5269 static bool data_segment_valid(struct kvm_vcpu *vcpu, int seg)
5271 struct kvm_segment var;
5274 vmx_get_segment(vcpu, &var, seg);
5275 rpl = var.selector & SEGMENT_RPL_MASK;
5283 if (~var.type & (VMX_AR_TYPE_CODE_MASK|VMX_AR_TYPE_WRITEABLE_MASK)) {
5284 if (var.dpl < rpl) /* DPL < RPL */
5288 /* TODO: Add other members to kvm_segment_field to allow checking for other access
5294 static bool tr_valid(struct kvm_vcpu *vcpu)
5296 struct kvm_segment tr;
5298 vmx_get_segment(vcpu, &tr, VCPU_SREG_TR);
5302 if (tr.selector & SEGMENT_TI_MASK) /* TI = 1 */
5304 if (tr.type != 3 && tr.type != 11) /* TODO: Check if guest is in IA32e mode */
5312 static bool ldtr_valid(struct kvm_vcpu *vcpu)
5314 struct kvm_segment ldtr;
5316 vmx_get_segment(vcpu, &ldtr, VCPU_SREG_LDTR);
5320 if (ldtr.selector & SEGMENT_TI_MASK) /* TI = 1 */
5330 static bool cs_ss_rpl_check(struct kvm_vcpu *vcpu)
5332 struct kvm_segment cs, ss;
5334 vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
5335 vmx_get_segment(vcpu, &ss, VCPU_SREG_SS);
5337 return ((cs.selector & SEGMENT_RPL_MASK) ==
5338 (ss.selector & SEGMENT_RPL_MASK));
5342 * Check if guest state is valid. Returns true if valid, false if
5344 * We assume that registers are always usable
5346 static bool guest_state_valid(struct kvm_vcpu *vcpu)
5348 if (enable_unrestricted_guest)
5351 /* real mode guest state checks */
5352 if (!is_protmode(vcpu) || (vmx_get_rflags(vcpu) & X86_EFLAGS_VM)) {
5353 if (!rmode_segment_valid(vcpu, VCPU_SREG_CS))
5355 if (!rmode_segment_valid(vcpu, VCPU_SREG_SS))
5357 if (!rmode_segment_valid(vcpu, VCPU_SREG_DS))
5359 if (!rmode_segment_valid(vcpu, VCPU_SREG_ES))
5361 if (!rmode_segment_valid(vcpu, VCPU_SREG_FS))
5363 if (!rmode_segment_valid(vcpu, VCPU_SREG_GS))
5366 /* protected mode guest state checks */
5367 if (!cs_ss_rpl_check(vcpu))
5369 if (!code_segment_valid(vcpu))
5371 if (!stack_segment_valid(vcpu))
5373 if (!data_segment_valid(vcpu, VCPU_SREG_DS))
5375 if (!data_segment_valid(vcpu, VCPU_SREG_ES))
5377 if (!data_segment_valid(vcpu, VCPU_SREG_FS))
5379 if (!data_segment_valid(vcpu, VCPU_SREG_GS))
5381 if (!tr_valid(vcpu))
5383 if (!ldtr_valid(vcpu))
5387 * - Add checks on RIP
5388 * - Add checks on RFLAGS
5394 static bool page_address_valid(struct kvm_vcpu *vcpu, gpa_t gpa)
5396 return PAGE_ALIGNED(gpa) && !(gpa >> cpuid_maxphyaddr(vcpu));
5399 static int init_rmode_tss(struct kvm *kvm)
5405 idx = srcu_read_lock(&kvm->srcu);
5406 fn = to_kvm_vmx(kvm)->tss_addr >> PAGE_SHIFT;
5407 r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE);
5410 data = TSS_BASE_SIZE + TSS_REDIRECTION_SIZE;
5411 r = kvm_write_guest_page(kvm, fn++, &data,
5412 TSS_IOPB_BASE_OFFSET, sizeof(u16));
5415 r = kvm_clear_guest_page(kvm, fn++, 0, PAGE_SIZE);
5418 r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE);
5422 r = kvm_write_guest_page(kvm, fn, &data,
5423 RMODE_TSS_SIZE - 2 * PAGE_SIZE - 1,
5426 srcu_read_unlock(&kvm->srcu, idx);
5430 static int init_rmode_identity_map(struct kvm *kvm)
5432 struct kvm_vmx *kvm_vmx = to_kvm_vmx(kvm);
5434 kvm_pfn_t identity_map_pfn;
5437 /* Protect kvm_vmx->ept_identity_pagetable_done. */
5438 mutex_lock(&kvm->slots_lock);
5440 if (likely(kvm_vmx->ept_identity_pagetable_done))
5443 if (!kvm_vmx->ept_identity_map_addr)
5444 kvm_vmx->ept_identity_map_addr = VMX_EPT_IDENTITY_PAGETABLE_ADDR;
5445 identity_map_pfn = kvm_vmx->ept_identity_map_addr >> PAGE_SHIFT;
5447 r = __x86_set_memory_region(kvm, IDENTITY_PAGETABLE_PRIVATE_MEMSLOT,
5448 kvm_vmx->ept_identity_map_addr, PAGE_SIZE);
5452 idx = srcu_read_lock(&kvm->srcu);
5453 r = kvm_clear_guest_page(kvm, identity_map_pfn, 0, PAGE_SIZE);
5456 /* Set up identity-mapping pagetable for EPT in real mode */
5457 for (i = 0; i < PT32_ENT_PER_PAGE; i++) {
5458 tmp = (i << 22) + (_PAGE_PRESENT | _PAGE_RW | _PAGE_USER |
5459 _PAGE_ACCESSED | _PAGE_DIRTY | _PAGE_PSE);
5460 r = kvm_write_guest_page(kvm, identity_map_pfn,
5461 &tmp, i * sizeof(tmp), sizeof(tmp));
5465 kvm_vmx->ept_identity_pagetable_done = true;
5468 srcu_read_unlock(&kvm->srcu, idx);
5471 mutex_unlock(&kvm->slots_lock);
5475 static void seg_setup(int seg)
5477 const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
5480 vmcs_write16(sf->selector, 0);
5481 vmcs_writel(sf->base, 0);
5482 vmcs_write32(sf->limit, 0xffff);
5484 if (seg == VCPU_SREG_CS)
5485 ar |= 0x08; /* code segment */
5487 vmcs_write32(sf->ar_bytes, ar);
5490 static int alloc_apic_access_page(struct kvm *kvm)
5495 mutex_lock(&kvm->slots_lock);
5496 if (kvm->arch.apic_access_page_done)
5498 r = __x86_set_memory_region(kvm, APIC_ACCESS_PAGE_PRIVATE_MEMSLOT,
5499 APIC_DEFAULT_PHYS_BASE, PAGE_SIZE);
5503 page = gfn_to_page(kvm, APIC_DEFAULT_PHYS_BASE >> PAGE_SHIFT);
5504 if (is_error_page(page)) {
5510 * Do not pin the page in memory, so that memory hot-unplug
5511 * is able to migrate it.
5514 kvm->arch.apic_access_page_done = true;
5516 mutex_unlock(&kvm->slots_lock);
5520 static int allocate_vpid(void)
5526 spin_lock(&vmx_vpid_lock);
5527 vpid = find_first_zero_bit(vmx_vpid_bitmap, VMX_NR_VPIDS);
5528 if (vpid < VMX_NR_VPIDS)
5529 __set_bit(vpid, vmx_vpid_bitmap);
5532 spin_unlock(&vmx_vpid_lock);
5536 static void free_vpid(int vpid)
5538 if (!enable_vpid || vpid == 0)
5540 spin_lock(&vmx_vpid_lock);
5541 __clear_bit(vpid, vmx_vpid_bitmap);
5542 spin_unlock(&vmx_vpid_lock);
5545 static void __always_inline vmx_disable_intercept_for_msr(unsigned long *msr_bitmap,
5548 int f = sizeof(unsigned long);
5550 if (!cpu_has_vmx_msr_bitmap())
5553 if (static_branch_unlikely(&enable_evmcs))
5554 evmcs_touch_msr_bitmap();
5557 * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
5558 * have the write-low and read-high bitmap offsets the wrong way round.
5559 * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
5561 if (msr <= 0x1fff) {
5562 if (type & MSR_TYPE_R)
5564 __clear_bit(msr, msr_bitmap + 0x000 / f);
5566 if (type & MSR_TYPE_W)
5568 __clear_bit(msr, msr_bitmap + 0x800 / f);
5570 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
5572 if (type & MSR_TYPE_R)
5574 __clear_bit(msr, msr_bitmap + 0x400 / f);
5576 if (type & MSR_TYPE_W)
5578 __clear_bit(msr, msr_bitmap + 0xc00 / f);
5583 static void __always_inline vmx_enable_intercept_for_msr(unsigned long *msr_bitmap,
5586 int f = sizeof(unsigned long);
5588 if (!cpu_has_vmx_msr_bitmap())
5591 if (static_branch_unlikely(&enable_evmcs))
5592 evmcs_touch_msr_bitmap();
5595 * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
5596 * have the write-low and read-high bitmap offsets the wrong way round.
5597 * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
5599 if (msr <= 0x1fff) {
5600 if (type & MSR_TYPE_R)
5602 __set_bit(msr, msr_bitmap + 0x000 / f);
5604 if (type & MSR_TYPE_W)
5606 __set_bit(msr, msr_bitmap + 0x800 / f);
5608 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
5610 if (type & MSR_TYPE_R)
5612 __set_bit(msr, msr_bitmap + 0x400 / f);
5614 if (type & MSR_TYPE_W)
5616 __set_bit(msr, msr_bitmap + 0xc00 / f);
5621 static void __always_inline vmx_set_intercept_for_msr(unsigned long *msr_bitmap,
5622 u32 msr, int type, bool value)
5625 vmx_enable_intercept_for_msr(msr_bitmap, msr, type);
5627 vmx_disable_intercept_for_msr(msr_bitmap, msr, type);
5631 * If a msr is allowed by L0, we should check whether it is allowed by L1.
5632 * The corresponding bit will be cleared unless both of L0 and L1 allow it.
5634 static void nested_vmx_disable_intercept_for_msr(unsigned long *msr_bitmap_l1,
5635 unsigned long *msr_bitmap_nested,
5638 int f = sizeof(unsigned long);
5641 * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
5642 * have the write-low and read-high bitmap offsets the wrong way round.
5643 * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
5645 if (msr <= 0x1fff) {
5646 if (type & MSR_TYPE_R &&
5647 !test_bit(msr, msr_bitmap_l1 + 0x000 / f))
5649 __clear_bit(msr, msr_bitmap_nested + 0x000 / f);
5651 if (type & MSR_TYPE_W &&
5652 !test_bit(msr, msr_bitmap_l1 + 0x800 / f))
5654 __clear_bit(msr, msr_bitmap_nested + 0x800 / f);
5656 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
5658 if (type & MSR_TYPE_R &&
5659 !test_bit(msr, msr_bitmap_l1 + 0x400 / f))
5661 __clear_bit(msr, msr_bitmap_nested + 0x400 / f);
5663 if (type & MSR_TYPE_W &&
5664 !test_bit(msr, msr_bitmap_l1 + 0xc00 / f))
5666 __clear_bit(msr, msr_bitmap_nested + 0xc00 / f);
5671 static u8 vmx_msr_bitmap_mode(struct kvm_vcpu *vcpu)
5675 if (cpu_has_secondary_exec_ctrls() &&
5676 (vmcs_read32(SECONDARY_VM_EXEC_CONTROL) &
5677 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE)) {
5678 mode |= MSR_BITMAP_MODE_X2APIC;
5679 if (enable_apicv && kvm_vcpu_apicv_active(vcpu))
5680 mode |= MSR_BITMAP_MODE_X2APIC_APICV;
5683 if (is_long_mode(vcpu))
5684 mode |= MSR_BITMAP_MODE_LM;
5689 #define X2APIC_MSR(r) (APIC_BASE_MSR + ((r) >> 4))
5691 static void vmx_update_msr_bitmap_x2apic(unsigned long *msr_bitmap,
5696 for (msr = 0x800; msr <= 0x8ff; msr += BITS_PER_LONG) {
5697 unsigned word = msr / BITS_PER_LONG;
5698 msr_bitmap[word] = (mode & MSR_BITMAP_MODE_X2APIC_APICV) ? 0 : ~0;
5699 msr_bitmap[word + (0x800 / sizeof(long))] = ~0;
5702 if (mode & MSR_BITMAP_MODE_X2APIC) {
5704 * TPR reads and writes can be virtualized even if virtual interrupt
5705 * delivery is not in use.
5707 vmx_disable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_TASKPRI), MSR_TYPE_RW);
5708 if (mode & MSR_BITMAP_MODE_X2APIC_APICV) {
5709 vmx_enable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_TMCCT), MSR_TYPE_R);
5710 vmx_disable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_EOI), MSR_TYPE_W);
5711 vmx_disable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_SELF_IPI), MSR_TYPE_W);
5716 static void vmx_update_msr_bitmap(struct kvm_vcpu *vcpu)
5718 struct vcpu_vmx *vmx = to_vmx(vcpu);
5719 unsigned long *msr_bitmap = vmx->vmcs01.msr_bitmap;
5720 u8 mode = vmx_msr_bitmap_mode(vcpu);
5721 u8 changed = mode ^ vmx->msr_bitmap_mode;
5726 vmx_set_intercept_for_msr(msr_bitmap, MSR_KERNEL_GS_BASE, MSR_TYPE_RW,
5727 !(mode & MSR_BITMAP_MODE_LM));
5729 if (changed & (MSR_BITMAP_MODE_X2APIC | MSR_BITMAP_MODE_X2APIC_APICV))
5730 vmx_update_msr_bitmap_x2apic(msr_bitmap, mode);
5732 vmx->msr_bitmap_mode = mode;
5735 static bool vmx_get_enable_apicv(struct kvm_vcpu *vcpu)
5737 return enable_apicv;
5740 static void nested_mark_vmcs12_pages_dirty(struct kvm_vcpu *vcpu)
5742 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
5746 * Don't need to mark the APIC access page dirty; it is never
5747 * written to by the CPU during APIC virtualization.
5750 if (nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW)) {
5751 gfn = vmcs12->virtual_apic_page_addr >> PAGE_SHIFT;
5752 kvm_vcpu_mark_page_dirty(vcpu, gfn);
5755 if (nested_cpu_has_posted_intr(vmcs12)) {
5756 gfn = vmcs12->posted_intr_desc_addr >> PAGE_SHIFT;
5757 kvm_vcpu_mark_page_dirty(vcpu, gfn);
5762 static void vmx_complete_nested_posted_interrupt(struct kvm_vcpu *vcpu)
5764 struct vcpu_vmx *vmx = to_vmx(vcpu);
5769 if (!vmx->nested.pi_desc || !vmx->nested.pi_pending)
5772 vmx->nested.pi_pending = false;
5773 if (!pi_test_and_clear_on(vmx->nested.pi_desc))
5776 max_irr = find_last_bit((unsigned long *)vmx->nested.pi_desc->pir, 256);
5777 if (max_irr != 256) {
5778 vapic_page = kmap(vmx->nested.virtual_apic_page);
5779 __kvm_apic_update_irr(vmx->nested.pi_desc->pir,
5780 vapic_page, &max_irr);
5781 kunmap(vmx->nested.virtual_apic_page);
5783 status = vmcs_read16(GUEST_INTR_STATUS);
5784 if ((u8)max_irr > ((u8)status & 0xff)) {
5786 status |= (u8)max_irr;
5787 vmcs_write16(GUEST_INTR_STATUS, status);
5791 nested_mark_vmcs12_pages_dirty(vcpu);
5794 static inline bool kvm_vcpu_trigger_posted_interrupt(struct kvm_vcpu *vcpu,
5798 int pi_vec = nested ? POSTED_INTR_NESTED_VECTOR : POSTED_INTR_VECTOR;
5800 if (vcpu->mode == IN_GUEST_MODE) {
5802 * The vector of interrupt to be delivered to vcpu had
5803 * been set in PIR before this function.
5805 * Following cases will be reached in this block, and
5806 * we always send a notification event in all cases as
5809 * Case 1: vcpu keeps in non-root mode. Sending a
5810 * notification event posts the interrupt to vcpu.
5812 * Case 2: vcpu exits to root mode and is still
5813 * runnable. PIR will be synced to vIRR before the
5814 * next vcpu entry. Sending a notification event in
5815 * this case has no effect, as vcpu is not in root
5818 * Case 3: vcpu exits to root mode and is blocked.
5819 * vcpu_block() has already synced PIR to vIRR and
5820 * never blocks vcpu if vIRR is not cleared. Therefore,
5821 * a blocked vcpu here does not wait for any requested
5822 * interrupts in PIR, and sending a notification event
5823 * which has no effect is safe here.
5826 apic->send_IPI_mask(get_cpu_mask(vcpu->cpu), pi_vec);
5833 static int vmx_deliver_nested_posted_interrupt(struct kvm_vcpu *vcpu,
5836 struct vcpu_vmx *vmx = to_vmx(vcpu);
5838 if (is_guest_mode(vcpu) &&
5839 vector == vmx->nested.posted_intr_nv) {
5841 * If a posted intr is not recognized by hardware,
5842 * we will accomplish it in the next vmentry.
5844 vmx->nested.pi_pending = true;
5845 kvm_make_request(KVM_REQ_EVENT, vcpu);
5846 /* the PIR and ON have been set by L1. */
5847 if (!kvm_vcpu_trigger_posted_interrupt(vcpu, true))
5848 kvm_vcpu_kick(vcpu);
5854 * Send interrupt to vcpu via posted interrupt way.
5855 * 1. If target vcpu is running(non-root mode), send posted interrupt
5856 * notification to vcpu and hardware will sync PIR to vIRR atomically.
5857 * 2. If target vcpu isn't running(root mode), kick it to pick up the
5858 * interrupt from PIR in next vmentry.
5860 static void vmx_deliver_posted_interrupt(struct kvm_vcpu *vcpu, int vector)
5862 struct vcpu_vmx *vmx = to_vmx(vcpu);
5865 r = vmx_deliver_nested_posted_interrupt(vcpu, vector);
5869 if (pi_test_and_set_pir(vector, &vmx->pi_desc))
5872 /* If a previous notification has sent the IPI, nothing to do. */
5873 if (pi_test_and_set_on(&vmx->pi_desc))
5876 if (!kvm_vcpu_trigger_posted_interrupt(vcpu, false))
5877 kvm_vcpu_kick(vcpu);
5881 * Set up the vmcs's constant host-state fields, i.e., host-state fields that
5882 * will not change in the lifetime of the guest.
5883 * Note that host-state that does change is set elsewhere. E.g., host-state
5884 * that is set differently for each CPU is set in vmx_vcpu_load(), not here.
5886 static void vmx_set_constant_host_state(struct vcpu_vmx *vmx)
5891 unsigned long cr0, cr3, cr4;
5894 WARN_ON(cr0 & X86_CR0_TS);
5895 vmcs_writel(HOST_CR0, cr0); /* 22.2.3 */
5898 * Save the most likely value for this task's CR3 in the VMCS.
5899 * We can't use __get_current_cr3_fast() because we're not atomic.
5902 vmcs_writel(HOST_CR3, cr3); /* 22.2.3 FIXME: shadow tables */
5903 vmx->loaded_vmcs->vmcs_host_cr3 = cr3;
5905 /* Save the most likely value for this task's CR4 in the VMCS. */
5906 cr4 = cr4_read_shadow();
5907 vmcs_writel(HOST_CR4, cr4); /* 22.2.3, 22.2.5 */
5908 vmx->loaded_vmcs->vmcs_host_cr4 = cr4;
5910 vmcs_write16(HOST_CS_SELECTOR, __KERNEL_CS); /* 22.2.4 */
5911 #ifdef CONFIG_X86_64
5913 * Load null selectors, so we can avoid reloading them in
5914 * __vmx_load_host_state(), in case userspace uses the null selectors
5915 * too (the expected case).
5917 vmcs_write16(HOST_DS_SELECTOR, 0);
5918 vmcs_write16(HOST_ES_SELECTOR, 0);
5920 vmcs_write16(HOST_DS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
5921 vmcs_write16(HOST_ES_SELECTOR, __KERNEL_DS); /* 22.2.4 */
5923 vmcs_write16(HOST_SS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
5924 vmcs_write16(HOST_TR_SELECTOR, GDT_ENTRY_TSS*8); /* 22.2.4 */
5927 vmcs_writel(HOST_IDTR_BASE, dt.address); /* 22.2.4 */
5928 vmx->host_idt_base = dt.address;
5930 vmcs_writel(HOST_RIP, vmx_return); /* 22.2.5 */
5932 rdmsr(MSR_IA32_SYSENTER_CS, low32, high32);
5933 vmcs_write32(HOST_IA32_SYSENTER_CS, low32);
5934 rdmsrl(MSR_IA32_SYSENTER_EIP, tmpl);
5935 vmcs_writel(HOST_IA32_SYSENTER_EIP, tmpl); /* 22.2.3 */
5937 if (vmcs_config.vmexit_ctrl & VM_EXIT_LOAD_IA32_PAT) {
5938 rdmsr(MSR_IA32_CR_PAT, low32, high32);
5939 vmcs_write64(HOST_IA32_PAT, low32 | ((u64) high32 << 32));
5943 static void set_cr4_guest_host_mask(struct vcpu_vmx *vmx)
5945 vmx->vcpu.arch.cr4_guest_owned_bits = KVM_CR4_GUEST_OWNED_BITS;
5947 vmx->vcpu.arch.cr4_guest_owned_bits |= X86_CR4_PGE;
5948 if (is_guest_mode(&vmx->vcpu))
5949 vmx->vcpu.arch.cr4_guest_owned_bits &=
5950 ~get_vmcs12(&vmx->vcpu)->cr4_guest_host_mask;
5951 vmcs_writel(CR4_GUEST_HOST_MASK, ~vmx->vcpu.arch.cr4_guest_owned_bits);
5954 static u32 vmx_pin_based_exec_ctrl(struct vcpu_vmx *vmx)
5956 u32 pin_based_exec_ctrl = vmcs_config.pin_based_exec_ctrl;
5958 if (!kvm_vcpu_apicv_active(&vmx->vcpu))
5959 pin_based_exec_ctrl &= ~PIN_BASED_POSTED_INTR;
5962 pin_based_exec_ctrl &= ~PIN_BASED_VIRTUAL_NMIS;
5964 /* Enable the preemption timer dynamically */
5965 pin_based_exec_ctrl &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
5966 return pin_based_exec_ctrl;
5969 static void vmx_refresh_apicv_exec_ctrl(struct kvm_vcpu *vcpu)
5971 struct vcpu_vmx *vmx = to_vmx(vcpu);
5973 vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, vmx_pin_based_exec_ctrl(vmx));
5974 if (cpu_has_secondary_exec_ctrls()) {
5975 if (kvm_vcpu_apicv_active(vcpu))
5976 vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL,
5977 SECONDARY_EXEC_APIC_REGISTER_VIRT |
5978 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
5980 vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL,
5981 SECONDARY_EXEC_APIC_REGISTER_VIRT |
5982 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
5985 if (cpu_has_vmx_msr_bitmap())
5986 vmx_update_msr_bitmap(vcpu);
5989 static u32 vmx_exec_control(struct vcpu_vmx *vmx)
5991 u32 exec_control = vmcs_config.cpu_based_exec_ctrl;
5993 if (vmx->vcpu.arch.switch_db_regs & KVM_DEBUGREG_WONT_EXIT)
5994 exec_control &= ~CPU_BASED_MOV_DR_EXITING;
5996 if (!cpu_need_tpr_shadow(&vmx->vcpu)) {
5997 exec_control &= ~CPU_BASED_TPR_SHADOW;
5998 #ifdef CONFIG_X86_64
5999 exec_control |= CPU_BASED_CR8_STORE_EXITING |
6000 CPU_BASED_CR8_LOAD_EXITING;
6004 exec_control |= CPU_BASED_CR3_STORE_EXITING |
6005 CPU_BASED_CR3_LOAD_EXITING |
6006 CPU_BASED_INVLPG_EXITING;
6007 if (kvm_mwait_in_guest(vmx->vcpu.kvm))
6008 exec_control &= ~(CPU_BASED_MWAIT_EXITING |
6009 CPU_BASED_MONITOR_EXITING);
6010 if (kvm_hlt_in_guest(vmx->vcpu.kvm))
6011 exec_control &= ~CPU_BASED_HLT_EXITING;
6012 return exec_control;
6015 static bool vmx_rdrand_supported(void)
6017 return vmcs_config.cpu_based_2nd_exec_ctrl &
6018 SECONDARY_EXEC_RDRAND_EXITING;
6021 static bool vmx_rdseed_supported(void)
6023 return vmcs_config.cpu_based_2nd_exec_ctrl &
6024 SECONDARY_EXEC_RDSEED_EXITING;
6027 static void vmx_compute_secondary_exec_control(struct vcpu_vmx *vmx)
6029 struct kvm_vcpu *vcpu = &vmx->vcpu;
6031 u32 exec_control = vmcs_config.cpu_based_2nd_exec_ctrl;
6033 if (!cpu_need_virtualize_apic_accesses(vcpu))
6034 exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
6036 exec_control &= ~SECONDARY_EXEC_ENABLE_VPID;
6038 exec_control &= ~SECONDARY_EXEC_ENABLE_EPT;
6039 enable_unrestricted_guest = 0;
6040 /* Enable INVPCID for non-ept guests may cause performance regression. */
6041 exec_control &= ~SECONDARY_EXEC_ENABLE_INVPCID;
6043 if (!enable_unrestricted_guest)
6044 exec_control &= ~SECONDARY_EXEC_UNRESTRICTED_GUEST;
6045 if (kvm_pause_in_guest(vmx->vcpu.kvm))
6046 exec_control &= ~SECONDARY_EXEC_PAUSE_LOOP_EXITING;
6047 if (!kvm_vcpu_apicv_active(vcpu))
6048 exec_control &= ~(SECONDARY_EXEC_APIC_REGISTER_VIRT |
6049 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
6050 exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
6052 /* SECONDARY_EXEC_DESC is enabled/disabled on writes to CR4.UMIP,
6053 * in vmx_set_cr4. */
6054 exec_control &= ~SECONDARY_EXEC_DESC;
6056 /* SECONDARY_EXEC_SHADOW_VMCS is enabled when L1 executes VMPTRLD
6058 We can NOT enable shadow_vmcs here because we don't have yet
6061 exec_control &= ~SECONDARY_EXEC_SHADOW_VMCS;
6064 exec_control &= ~SECONDARY_EXEC_ENABLE_PML;
6066 if (vmx_xsaves_supported()) {
6067 /* Exposing XSAVES only when XSAVE is exposed */
6068 bool xsaves_enabled =
6069 guest_cpuid_has(vcpu, X86_FEATURE_XSAVE) &&
6070 guest_cpuid_has(vcpu, X86_FEATURE_XSAVES);
6072 if (!xsaves_enabled)
6073 exec_control &= ~SECONDARY_EXEC_XSAVES;
6077 vmx->nested.msrs.secondary_ctls_high |=
6078 SECONDARY_EXEC_XSAVES;
6080 vmx->nested.msrs.secondary_ctls_high &=
6081 ~SECONDARY_EXEC_XSAVES;
6085 if (vmx_rdtscp_supported()) {
6086 bool rdtscp_enabled = guest_cpuid_has(vcpu, X86_FEATURE_RDTSCP);
6087 if (!rdtscp_enabled)
6088 exec_control &= ~SECONDARY_EXEC_RDTSCP;
6092 vmx->nested.msrs.secondary_ctls_high |=
6093 SECONDARY_EXEC_RDTSCP;
6095 vmx->nested.msrs.secondary_ctls_high &=
6096 ~SECONDARY_EXEC_RDTSCP;
6100 if (vmx_invpcid_supported()) {
6101 /* Exposing INVPCID only when PCID is exposed */
6102 bool invpcid_enabled =
6103 guest_cpuid_has(vcpu, X86_FEATURE_INVPCID) &&
6104 guest_cpuid_has(vcpu, X86_FEATURE_PCID);
6106 if (!invpcid_enabled) {
6107 exec_control &= ~SECONDARY_EXEC_ENABLE_INVPCID;
6108 guest_cpuid_clear(vcpu, X86_FEATURE_INVPCID);
6112 if (invpcid_enabled)
6113 vmx->nested.msrs.secondary_ctls_high |=
6114 SECONDARY_EXEC_ENABLE_INVPCID;
6116 vmx->nested.msrs.secondary_ctls_high &=
6117 ~SECONDARY_EXEC_ENABLE_INVPCID;
6121 if (vmx_rdrand_supported()) {
6122 bool rdrand_enabled = guest_cpuid_has(vcpu, X86_FEATURE_RDRAND);
6124 exec_control &= ~SECONDARY_EXEC_RDRAND_EXITING;
6128 vmx->nested.msrs.secondary_ctls_high |=
6129 SECONDARY_EXEC_RDRAND_EXITING;
6131 vmx->nested.msrs.secondary_ctls_high &=
6132 ~SECONDARY_EXEC_RDRAND_EXITING;
6136 if (vmx_rdseed_supported()) {
6137 bool rdseed_enabled = guest_cpuid_has(vcpu, X86_FEATURE_RDSEED);
6139 exec_control &= ~SECONDARY_EXEC_RDSEED_EXITING;
6143 vmx->nested.msrs.secondary_ctls_high |=
6144 SECONDARY_EXEC_RDSEED_EXITING;
6146 vmx->nested.msrs.secondary_ctls_high &=
6147 ~SECONDARY_EXEC_RDSEED_EXITING;
6151 vmx->secondary_exec_control = exec_control;
6154 static void ept_set_mmio_spte_mask(void)
6157 * EPT Misconfigurations can be generated if the value of bits 2:0
6158 * of an EPT paging-structure entry is 110b (write/execute).
6160 kvm_mmu_set_mmio_spte_mask(VMX_EPT_RWX_MASK,
6161 VMX_EPT_MISCONFIG_WX_VALUE);
6164 #define VMX_XSS_EXIT_BITMAP 0
6166 * Sets up the vmcs for emulated real mode.
6168 static void vmx_vcpu_setup(struct vcpu_vmx *vmx)
6170 #ifdef CONFIG_X86_64
6175 if (enable_shadow_vmcs) {
6177 * At vCPU creation, "VMWRITE to any supported field
6178 * in the VMCS" is supported, so use the more
6179 * permissive vmx_vmread_bitmap to specify both read
6180 * and write permissions for the shadow VMCS.
6182 vmcs_write64(VMREAD_BITMAP, __pa(vmx_vmread_bitmap));
6183 vmcs_write64(VMWRITE_BITMAP, __pa(vmx_vmread_bitmap));
6185 if (cpu_has_vmx_msr_bitmap())
6186 vmcs_write64(MSR_BITMAP, __pa(vmx->vmcs01.msr_bitmap));
6188 vmcs_write64(VMCS_LINK_POINTER, -1ull); /* 22.3.1.5 */
6191 vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, vmx_pin_based_exec_ctrl(vmx));
6192 vmx->hv_deadline_tsc = -1;
6194 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, vmx_exec_control(vmx));
6196 if (cpu_has_secondary_exec_ctrls()) {
6197 vmx_compute_secondary_exec_control(vmx);
6198 vmcs_write32(SECONDARY_VM_EXEC_CONTROL,
6199 vmx->secondary_exec_control);
6202 if (kvm_vcpu_apicv_active(&vmx->vcpu)) {
6203 vmcs_write64(EOI_EXIT_BITMAP0, 0);
6204 vmcs_write64(EOI_EXIT_BITMAP1, 0);
6205 vmcs_write64(EOI_EXIT_BITMAP2, 0);
6206 vmcs_write64(EOI_EXIT_BITMAP3, 0);
6208 vmcs_write16(GUEST_INTR_STATUS, 0);
6210 vmcs_write16(POSTED_INTR_NV, POSTED_INTR_VECTOR);
6211 vmcs_write64(POSTED_INTR_DESC_ADDR, __pa((&vmx->pi_desc)));
6214 if (!kvm_pause_in_guest(vmx->vcpu.kvm)) {
6215 vmcs_write32(PLE_GAP, ple_gap);
6216 vmx->ple_window = ple_window;
6217 vmx->ple_window_dirty = true;
6220 vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK, 0);
6221 vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH, 0);
6222 vmcs_write32(CR3_TARGET_COUNT, 0); /* 22.2.1 */
6224 vmcs_write16(HOST_FS_SELECTOR, 0); /* 22.2.4 */
6225 vmcs_write16(HOST_GS_SELECTOR, 0); /* 22.2.4 */
6226 vmx_set_constant_host_state(vmx);
6227 #ifdef CONFIG_X86_64
6228 rdmsrl(MSR_FS_BASE, a);
6229 vmcs_writel(HOST_FS_BASE, a); /* 22.2.4 */
6230 rdmsrl(MSR_GS_BASE, a);
6231 vmcs_writel(HOST_GS_BASE, a); /* 22.2.4 */
6233 vmcs_writel(HOST_FS_BASE, 0); /* 22.2.4 */
6234 vmcs_writel(HOST_GS_BASE, 0); /* 22.2.4 */
6237 if (cpu_has_vmx_vmfunc())
6238 vmcs_write64(VM_FUNCTION_CONTROL, 0);
6240 vmcs_write32(VM_EXIT_MSR_STORE_COUNT, 0);
6241 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, 0);
6242 vmcs_write64(VM_EXIT_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.host));
6243 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, 0);
6244 vmcs_write64(VM_ENTRY_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.guest));
6246 if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT)
6247 vmcs_write64(GUEST_IA32_PAT, vmx->vcpu.arch.pat);
6249 for (i = 0; i < ARRAY_SIZE(vmx_msr_index); ++i) {
6250 u32 index = vmx_msr_index[i];
6251 u32 data_low, data_high;
6254 if (rdmsr_safe(index, &data_low, &data_high) < 0)
6256 if (wrmsr_safe(index, data_low, data_high) < 0)
6258 vmx->guest_msrs[j].index = i;
6259 vmx->guest_msrs[j].data = 0;
6260 vmx->guest_msrs[j].mask = -1ull;
6264 if (boot_cpu_has(X86_FEATURE_ARCH_CAPABILITIES))
6265 rdmsrl(MSR_IA32_ARCH_CAPABILITIES, vmx->arch_capabilities);
6267 vm_exit_controls_init(vmx, vmcs_config.vmexit_ctrl);
6269 /* 22.2.1, 20.8.1 */
6270 vm_entry_controls_init(vmx, vmcs_config.vmentry_ctrl);
6272 vmx->vcpu.arch.cr0_guest_owned_bits = X86_CR0_TS;
6273 vmcs_writel(CR0_GUEST_HOST_MASK, ~X86_CR0_TS);
6275 set_cr4_guest_host_mask(vmx);
6277 if (vmx_xsaves_supported())
6278 vmcs_write64(XSS_EXIT_BITMAP, VMX_XSS_EXIT_BITMAP);
6281 ASSERT(vmx->pml_pg);
6282 vmcs_write64(PML_ADDRESS, page_to_phys(vmx->pml_pg));
6283 vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1);
6287 static void vmx_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event)
6289 struct vcpu_vmx *vmx = to_vmx(vcpu);
6290 struct msr_data apic_base_msr;
6293 vmx->rmode.vm86_active = 0;
6296 vcpu->arch.microcode_version = 0x100000000ULL;
6297 vmx->vcpu.arch.regs[VCPU_REGS_RDX] = get_rdx_init_val();
6298 kvm_set_cr8(vcpu, 0);
6301 apic_base_msr.data = APIC_DEFAULT_PHYS_BASE |
6302 MSR_IA32_APICBASE_ENABLE;
6303 if (kvm_vcpu_is_reset_bsp(vcpu))
6304 apic_base_msr.data |= MSR_IA32_APICBASE_BSP;
6305 apic_base_msr.host_initiated = true;
6306 kvm_set_apic_base(vcpu, &apic_base_msr);
6309 vmx_segment_cache_clear(vmx);
6311 seg_setup(VCPU_SREG_CS);
6312 vmcs_write16(GUEST_CS_SELECTOR, 0xf000);
6313 vmcs_writel(GUEST_CS_BASE, 0xffff0000ul);
6315 seg_setup(VCPU_SREG_DS);
6316 seg_setup(VCPU_SREG_ES);
6317 seg_setup(VCPU_SREG_FS);
6318 seg_setup(VCPU_SREG_GS);
6319 seg_setup(VCPU_SREG_SS);
6321 vmcs_write16(GUEST_TR_SELECTOR, 0);
6322 vmcs_writel(GUEST_TR_BASE, 0);
6323 vmcs_write32(GUEST_TR_LIMIT, 0xffff);
6324 vmcs_write32(GUEST_TR_AR_BYTES, 0x008b);
6326 vmcs_write16(GUEST_LDTR_SELECTOR, 0);
6327 vmcs_writel(GUEST_LDTR_BASE, 0);
6328 vmcs_write32(GUEST_LDTR_LIMIT, 0xffff);
6329 vmcs_write32(GUEST_LDTR_AR_BYTES, 0x00082);
6332 vmcs_write32(GUEST_SYSENTER_CS, 0);
6333 vmcs_writel(GUEST_SYSENTER_ESP, 0);
6334 vmcs_writel(GUEST_SYSENTER_EIP, 0);
6335 vmcs_write64(GUEST_IA32_DEBUGCTL, 0);
6338 kvm_set_rflags(vcpu, X86_EFLAGS_FIXED);
6339 kvm_rip_write(vcpu, 0xfff0);
6341 vmcs_writel(GUEST_GDTR_BASE, 0);
6342 vmcs_write32(GUEST_GDTR_LIMIT, 0xffff);
6344 vmcs_writel(GUEST_IDTR_BASE, 0);
6345 vmcs_write32(GUEST_IDTR_LIMIT, 0xffff);
6347 vmcs_write32(GUEST_ACTIVITY_STATE, GUEST_ACTIVITY_ACTIVE);
6348 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, 0);
6349 vmcs_writel(GUEST_PENDING_DBG_EXCEPTIONS, 0);
6350 if (kvm_mpx_supported())
6351 vmcs_write64(GUEST_BNDCFGS, 0);
6355 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0); /* 22.2.1 */
6357 if (cpu_has_vmx_tpr_shadow() && !init_event) {
6358 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, 0);
6359 if (cpu_need_tpr_shadow(vcpu))
6360 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR,
6361 __pa(vcpu->arch.apic->regs));
6362 vmcs_write32(TPR_THRESHOLD, 0);
6365 kvm_make_request(KVM_REQ_APIC_PAGE_RELOAD, vcpu);
6368 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid);
6370 cr0 = X86_CR0_NW | X86_CR0_CD | X86_CR0_ET;
6371 vmx->vcpu.arch.cr0 = cr0;
6372 vmx_set_cr0(vcpu, cr0); /* enter rmode */
6373 vmx_set_cr4(vcpu, 0);
6374 vmx_set_efer(vcpu, 0);
6376 update_exception_bitmap(vcpu);
6378 vpid_sync_context(vmx->vpid);
6380 vmx_clear_hlt(vcpu);
6384 * In nested virtualization, check if L1 asked to exit on external interrupts.
6385 * For most existing hypervisors, this will always return true.
6387 static bool nested_exit_on_intr(struct kvm_vcpu *vcpu)
6389 return get_vmcs12(vcpu)->pin_based_vm_exec_control &
6390 PIN_BASED_EXT_INTR_MASK;
6394 * In nested virtualization, check if L1 has set
6395 * VM_EXIT_ACK_INTR_ON_EXIT
6397 static bool nested_exit_intr_ack_set(struct kvm_vcpu *vcpu)
6399 return get_vmcs12(vcpu)->vm_exit_controls &
6400 VM_EXIT_ACK_INTR_ON_EXIT;
6403 static bool nested_exit_on_nmi(struct kvm_vcpu *vcpu)
6405 return nested_cpu_has_nmi_exiting(get_vmcs12(vcpu));
6408 static void enable_irq_window(struct kvm_vcpu *vcpu)
6410 vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL,
6411 CPU_BASED_VIRTUAL_INTR_PENDING);
6414 static void enable_nmi_window(struct kvm_vcpu *vcpu)
6417 vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & GUEST_INTR_STATE_STI) {
6418 enable_irq_window(vcpu);
6422 vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL,
6423 CPU_BASED_VIRTUAL_NMI_PENDING);
6426 static void vmx_inject_irq(struct kvm_vcpu *vcpu)
6428 struct vcpu_vmx *vmx = to_vmx(vcpu);
6430 int irq = vcpu->arch.interrupt.nr;
6432 trace_kvm_inj_virq(irq);
6434 ++vcpu->stat.irq_injections;
6435 if (vmx->rmode.vm86_active) {
6437 if (vcpu->arch.interrupt.soft)
6438 inc_eip = vcpu->arch.event_exit_inst_len;
6439 if (kvm_inject_realmode_interrupt(vcpu, irq, inc_eip) != EMULATE_DONE)
6440 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
6443 intr = irq | INTR_INFO_VALID_MASK;
6444 if (vcpu->arch.interrupt.soft) {
6445 intr |= INTR_TYPE_SOFT_INTR;
6446 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
6447 vmx->vcpu.arch.event_exit_inst_len);
6449 intr |= INTR_TYPE_EXT_INTR;
6450 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr);
6452 vmx_clear_hlt(vcpu);
6455 static void vmx_inject_nmi(struct kvm_vcpu *vcpu)
6457 struct vcpu_vmx *vmx = to_vmx(vcpu);
6461 * Tracking the NMI-blocked state in software is built upon
6462 * finding the next open IRQ window. This, in turn, depends on
6463 * well-behaving guests: They have to keep IRQs disabled at
6464 * least as long as the NMI handler runs. Otherwise we may
6465 * cause NMI nesting, maybe breaking the guest. But as this is
6466 * highly unlikely, we can live with the residual risk.
6468 vmx->loaded_vmcs->soft_vnmi_blocked = 1;
6469 vmx->loaded_vmcs->vnmi_blocked_time = 0;
6472 ++vcpu->stat.nmi_injections;
6473 vmx->loaded_vmcs->nmi_known_unmasked = false;
6475 if (vmx->rmode.vm86_active) {
6476 if (kvm_inject_realmode_interrupt(vcpu, NMI_VECTOR, 0) != EMULATE_DONE)
6477 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
6481 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
6482 INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK | NMI_VECTOR);
6484 vmx_clear_hlt(vcpu);
6487 static bool vmx_get_nmi_mask(struct kvm_vcpu *vcpu)
6489 struct vcpu_vmx *vmx = to_vmx(vcpu);
6493 return vmx->loaded_vmcs->soft_vnmi_blocked;
6494 if (vmx->loaded_vmcs->nmi_known_unmasked)
6496 masked = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & GUEST_INTR_STATE_NMI;
6497 vmx->loaded_vmcs->nmi_known_unmasked = !masked;
6501 static void vmx_set_nmi_mask(struct kvm_vcpu *vcpu, bool masked)
6503 struct vcpu_vmx *vmx = to_vmx(vcpu);
6506 if (vmx->loaded_vmcs->soft_vnmi_blocked != masked) {
6507 vmx->loaded_vmcs->soft_vnmi_blocked = masked;
6508 vmx->loaded_vmcs->vnmi_blocked_time = 0;
6511 vmx->loaded_vmcs->nmi_known_unmasked = !masked;
6513 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
6514 GUEST_INTR_STATE_NMI);
6516 vmcs_clear_bits(GUEST_INTERRUPTIBILITY_INFO,
6517 GUEST_INTR_STATE_NMI);
6521 static int vmx_nmi_allowed(struct kvm_vcpu *vcpu)
6523 if (to_vmx(vcpu)->nested.nested_run_pending)
6527 to_vmx(vcpu)->loaded_vmcs->soft_vnmi_blocked)
6530 return !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) &
6531 (GUEST_INTR_STATE_MOV_SS | GUEST_INTR_STATE_STI
6532 | GUEST_INTR_STATE_NMI));
6535 static int vmx_interrupt_allowed(struct kvm_vcpu *vcpu)
6537 return (!to_vmx(vcpu)->nested.nested_run_pending &&
6538 vmcs_readl(GUEST_RFLAGS) & X86_EFLAGS_IF) &&
6539 !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) &
6540 (GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS));
6543 static int vmx_set_tss_addr(struct kvm *kvm, unsigned int addr)
6547 if (enable_unrestricted_guest)
6550 ret = x86_set_memory_region(kvm, TSS_PRIVATE_MEMSLOT, addr,
6554 to_kvm_vmx(kvm)->tss_addr = addr;
6555 return init_rmode_tss(kvm);
6558 static int vmx_set_identity_map_addr(struct kvm *kvm, u64 ident_addr)
6560 to_kvm_vmx(kvm)->ept_identity_map_addr = ident_addr;
6564 static bool rmode_exception(struct kvm_vcpu *vcpu, int vec)
6569 * Update instruction length as we may reinject the exception
6570 * from user space while in guest debugging mode.
6572 to_vmx(vcpu)->vcpu.arch.event_exit_inst_len =
6573 vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
6574 if (vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
6578 if (vcpu->guest_debug &
6579 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))
6596 static int handle_rmode_exception(struct kvm_vcpu *vcpu,
6597 int vec, u32 err_code)
6600 * Instruction with address size override prefix opcode 0x67
6601 * Cause the #SS fault with 0 error code in VM86 mode.
6603 if (((vec == GP_VECTOR) || (vec == SS_VECTOR)) && err_code == 0) {
6604 if (emulate_instruction(vcpu, 0) == EMULATE_DONE) {
6605 if (vcpu->arch.halt_request) {
6606 vcpu->arch.halt_request = 0;
6607 return kvm_vcpu_halt(vcpu);
6615 * Forward all other exceptions that are valid in real mode.
6616 * FIXME: Breaks guest debugging in real mode, needs to be fixed with
6617 * the required debugging infrastructure rework.
6619 kvm_queue_exception(vcpu, vec);
6624 * Trigger machine check on the host. We assume all the MSRs are already set up
6625 * by the CPU and that we still run on the same CPU as the MCE occurred on.
6626 * We pass a fake environment to the machine check handler because we want
6627 * the guest to be always treated like user space, no matter what context
6628 * it used internally.
6630 static void kvm_machine_check(void)
6632 #if defined(CONFIG_X86_MCE) && defined(CONFIG_X86_64)
6633 struct pt_regs regs = {
6634 .cs = 3, /* Fake ring 3 no matter what the guest ran on */
6635 .flags = X86_EFLAGS_IF,
6638 do_machine_check(®s, 0);
6642 static int handle_machine_check(struct kvm_vcpu *vcpu)
6644 /* already handled by vcpu_run */
6648 static int handle_exception(struct kvm_vcpu *vcpu)
6650 struct vcpu_vmx *vmx = to_vmx(vcpu);
6651 struct kvm_run *kvm_run = vcpu->run;
6652 u32 intr_info, ex_no, error_code;
6653 unsigned long cr2, rip, dr6;
6655 enum emulation_result er;
6657 vect_info = vmx->idt_vectoring_info;
6658 intr_info = vmx->exit_intr_info;
6660 if (is_machine_check(intr_info))
6661 return handle_machine_check(vcpu);
6663 if (is_nmi(intr_info))
6664 return 1; /* already handled by vmx_vcpu_run() */
6666 if (is_invalid_opcode(intr_info))
6667 return handle_ud(vcpu);
6670 if (intr_info & INTR_INFO_DELIVER_CODE_MASK)
6671 error_code = vmcs_read32(VM_EXIT_INTR_ERROR_CODE);
6673 if (!vmx->rmode.vm86_active && is_gp_fault(intr_info)) {
6674 WARN_ON_ONCE(!enable_vmware_backdoor);
6675 er = emulate_instruction(vcpu,
6676 EMULTYPE_VMWARE | EMULTYPE_NO_UD_ON_FAIL);
6677 if (er == EMULATE_USER_EXIT)
6679 else if (er != EMULATE_DONE)
6680 kvm_queue_exception_e(vcpu, GP_VECTOR, error_code);
6685 * The #PF with PFEC.RSVD = 1 indicates the guest is accessing
6686 * MMIO, it is better to report an internal error.
6687 * See the comments in vmx_handle_exit.
6689 if ((vect_info & VECTORING_INFO_VALID_MASK) &&
6690 !(is_page_fault(intr_info) && !(error_code & PFERR_RSVD_MASK))) {
6691 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
6692 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_SIMUL_EX;
6693 vcpu->run->internal.ndata = 3;
6694 vcpu->run->internal.data[0] = vect_info;
6695 vcpu->run->internal.data[1] = intr_info;
6696 vcpu->run->internal.data[2] = error_code;
6700 if (is_page_fault(intr_info)) {
6701 cr2 = vmcs_readl(EXIT_QUALIFICATION);
6702 /* EPT won't cause page fault directly */
6703 WARN_ON_ONCE(!vcpu->arch.apf.host_apf_reason && enable_ept);
6704 return kvm_handle_page_fault(vcpu, error_code, cr2, NULL, 0);
6707 ex_no = intr_info & INTR_INFO_VECTOR_MASK;
6709 if (vmx->rmode.vm86_active && rmode_exception(vcpu, ex_no))
6710 return handle_rmode_exception(vcpu, ex_no, error_code);
6714 kvm_queue_exception_e(vcpu, AC_VECTOR, error_code);
6717 dr6 = vmcs_readl(EXIT_QUALIFICATION);
6718 if (!(vcpu->guest_debug &
6719 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))) {
6720 vcpu->arch.dr6 &= ~15;
6721 vcpu->arch.dr6 |= dr6 | DR6_RTM;
6722 if (is_icebp(intr_info))
6723 skip_emulated_instruction(vcpu);
6725 kvm_queue_exception(vcpu, DB_VECTOR);
6728 kvm_run->debug.arch.dr6 = dr6 | DR6_FIXED_1;
6729 kvm_run->debug.arch.dr7 = vmcs_readl(GUEST_DR7);
6733 * Update instruction length as we may reinject #BP from
6734 * user space while in guest debugging mode. Reading it for
6735 * #DB as well causes no harm, it is not used in that case.
6737 vmx->vcpu.arch.event_exit_inst_len =
6738 vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
6739 kvm_run->exit_reason = KVM_EXIT_DEBUG;
6740 rip = kvm_rip_read(vcpu);
6741 kvm_run->debug.arch.pc = vmcs_readl(GUEST_CS_BASE) + rip;
6742 kvm_run->debug.arch.exception = ex_no;
6745 kvm_run->exit_reason = KVM_EXIT_EXCEPTION;
6746 kvm_run->ex.exception = ex_no;
6747 kvm_run->ex.error_code = error_code;
6753 static int handle_external_interrupt(struct kvm_vcpu *vcpu)
6755 ++vcpu->stat.irq_exits;
6759 static int handle_triple_fault(struct kvm_vcpu *vcpu)
6761 vcpu->run->exit_reason = KVM_EXIT_SHUTDOWN;
6762 vcpu->mmio_needed = 0;
6766 static int handle_io(struct kvm_vcpu *vcpu)
6768 unsigned long exit_qualification;
6769 int size, in, string;
6772 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
6773 string = (exit_qualification & 16) != 0;
6775 ++vcpu->stat.io_exits;
6778 return emulate_instruction(vcpu, 0) == EMULATE_DONE;
6780 port = exit_qualification >> 16;
6781 size = (exit_qualification & 7) + 1;
6782 in = (exit_qualification & 8) != 0;
6784 return kvm_fast_pio(vcpu, size, port, in);
6788 vmx_patch_hypercall(struct kvm_vcpu *vcpu, unsigned char *hypercall)
6791 * Patch in the VMCALL instruction:
6793 hypercall[0] = 0x0f;
6794 hypercall[1] = 0x01;
6795 hypercall[2] = 0xc1;
6798 /* called to set cr0 as appropriate for a mov-to-cr0 exit. */
6799 static int handle_set_cr0(struct kvm_vcpu *vcpu, unsigned long val)
6801 if (is_guest_mode(vcpu)) {
6802 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
6803 unsigned long orig_val = val;
6806 * We get here when L2 changed cr0 in a way that did not change
6807 * any of L1's shadowed bits (see nested_vmx_exit_handled_cr),
6808 * but did change L0 shadowed bits. So we first calculate the
6809 * effective cr0 value that L1 would like to write into the
6810 * hardware. It consists of the L2-owned bits from the new
6811 * value combined with the L1-owned bits from L1's guest_cr0.
6813 val = (val & ~vmcs12->cr0_guest_host_mask) |
6814 (vmcs12->guest_cr0 & vmcs12->cr0_guest_host_mask);
6816 if (!nested_guest_cr0_valid(vcpu, val))
6819 if (kvm_set_cr0(vcpu, val))
6821 vmcs_writel(CR0_READ_SHADOW, orig_val);
6824 if (to_vmx(vcpu)->nested.vmxon &&
6825 !nested_host_cr0_valid(vcpu, val))
6828 return kvm_set_cr0(vcpu, val);
6832 static int handle_set_cr4(struct kvm_vcpu *vcpu, unsigned long val)
6834 if (is_guest_mode(vcpu)) {
6835 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
6836 unsigned long orig_val = val;
6838 /* analogously to handle_set_cr0 */
6839 val = (val & ~vmcs12->cr4_guest_host_mask) |
6840 (vmcs12->guest_cr4 & vmcs12->cr4_guest_host_mask);
6841 if (kvm_set_cr4(vcpu, val))
6843 vmcs_writel(CR4_READ_SHADOW, orig_val);
6846 return kvm_set_cr4(vcpu, val);
6849 static int handle_desc(struct kvm_vcpu *vcpu)
6851 WARN_ON(!(vcpu->arch.cr4 & X86_CR4_UMIP));
6852 return emulate_instruction(vcpu, 0) == EMULATE_DONE;
6855 static int handle_cr(struct kvm_vcpu *vcpu)
6857 unsigned long exit_qualification, val;
6863 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
6864 cr = exit_qualification & 15;
6865 reg = (exit_qualification >> 8) & 15;
6866 switch ((exit_qualification >> 4) & 3) {
6867 case 0: /* mov to cr */
6868 val = kvm_register_readl(vcpu, reg);
6869 trace_kvm_cr_write(cr, val);
6872 err = handle_set_cr0(vcpu, val);
6873 return kvm_complete_insn_gp(vcpu, err);
6875 WARN_ON_ONCE(enable_unrestricted_guest);
6876 err = kvm_set_cr3(vcpu, val);
6877 return kvm_complete_insn_gp(vcpu, err);
6879 err = handle_set_cr4(vcpu, val);
6880 return kvm_complete_insn_gp(vcpu, err);
6882 u8 cr8_prev = kvm_get_cr8(vcpu);
6884 err = kvm_set_cr8(vcpu, cr8);
6885 ret = kvm_complete_insn_gp(vcpu, err);
6886 if (lapic_in_kernel(vcpu))
6888 if (cr8_prev <= cr8)
6891 * TODO: we might be squashing a
6892 * KVM_GUESTDBG_SINGLESTEP-triggered
6893 * KVM_EXIT_DEBUG here.
6895 vcpu->run->exit_reason = KVM_EXIT_SET_TPR;
6901 WARN_ONCE(1, "Guest should always own CR0.TS");
6902 vmx_set_cr0(vcpu, kvm_read_cr0_bits(vcpu, ~X86_CR0_TS));
6903 trace_kvm_cr_write(0, kvm_read_cr0(vcpu));
6904 return kvm_skip_emulated_instruction(vcpu);
6905 case 1: /*mov from cr*/
6908 WARN_ON_ONCE(enable_unrestricted_guest);
6909 val = kvm_read_cr3(vcpu);
6910 kvm_register_write(vcpu, reg, val);
6911 trace_kvm_cr_read(cr, val);
6912 return kvm_skip_emulated_instruction(vcpu);
6914 val = kvm_get_cr8(vcpu);
6915 kvm_register_write(vcpu, reg, val);
6916 trace_kvm_cr_read(cr, val);
6917 return kvm_skip_emulated_instruction(vcpu);
6921 val = (exit_qualification >> LMSW_SOURCE_DATA_SHIFT) & 0x0f;
6922 trace_kvm_cr_write(0, (kvm_read_cr0(vcpu) & ~0xful) | val);
6923 kvm_lmsw(vcpu, val);
6925 return kvm_skip_emulated_instruction(vcpu);
6929 vcpu->run->exit_reason = 0;
6930 vcpu_unimpl(vcpu, "unhandled control register: op %d cr %d\n",
6931 (int)(exit_qualification >> 4) & 3, cr);
6935 static int handle_dr(struct kvm_vcpu *vcpu)
6937 unsigned long exit_qualification;
6940 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
6941 dr = exit_qualification & DEBUG_REG_ACCESS_NUM;
6943 /* First, if DR does not exist, trigger UD */
6944 if (!kvm_require_dr(vcpu, dr))
6947 /* Do not handle if the CPL > 0, will trigger GP on re-entry */
6948 if (!kvm_require_cpl(vcpu, 0))
6950 dr7 = vmcs_readl(GUEST_DR7);
6953 * As the vm-exit takes precedence over the debug trap, we
6954 * need to emulate the latter, either for the host or the
6955 * guest debugging itself.
6957 if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP) {
6958 vcpu->run->debug.arch.dr6 = vcpu->arch.dr6;
6959 vcpu->run->debug.arch.dr7 = dr7;
6960 vcpu->run->debug.arch.pc = kvm_get_linear_rip(vcpu);
6961 vcpu->run->debug.arch.exception = DB_VECTOR;
6962 vcpu->run->exit_reason = KVM_EXIT_DEBUG;
6965 vcpu->arch.dr6 &= ~15;
6966 vcpu->arch.dr6 |= DR6_BD | DR6_RTM;
6967 kvm_queue_exception(vcpu, DB_VECTOR);
6972 if (vcpu->guest_debug == 0) {
6973 vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL,
6974 CPU_BASED_MOV_DR_EXITING);
6977 * No more DR vmexits; force a reload of the debug registers
6978 * and reenter on this instruction. The next vmexit will
6979 * retrieve the full state of the debug registers.
6981 vcpu->arch.switch_db_regs |= KVM_DEBUGREG_WONT_EXIT;
6985 reg = DEBUG_REG_ACCESS_REG(exit_qualification);
6986 if (exit_qualification & TYPE_MOV_FROM_DR) {
6989 if (kvm_get_dr(vcpu, dr, &val))
6991 kvm_register_write(vcpu, reg, val);
6993 if (kvm_set_dr(vcpu, dr, kvm_register_readl(vcpu, reg)))
6996 return kvm_skip_emulated_instruction(vcpu);
6999 static u64 vmx_get_dr6(struct kvm_vcpu *vcpu)
7001 return vcpu->arch.dr6;
7004 static void vmx_set_dr6(struct kvm_vcpu *vcpu, unsigned long val)
7008 static void vmx_sync_dirty_debug_regs(struct kvm_vcpu *vcpu)
7010 get_debugreg(vcpu->arch.db[0], 0);
7011 get_debugreg(vcpu->arch.db[1], 1);
7012 get_debugreg(vcpu->arch.db[2], 2);
7013 get_debugreg(vcpu->arch.db[3], 3);
7014 get_debugreg(vcpu->arch.dr6, 6);
7015 vcpu->arch.dr7 = vmcs_readl(GUEST_DR7);
7017 vcpu->arch.switch_db_regs &= ~KVM_DEBUGREG_WONT_EXIT;
7018 vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL, CPU_BASED_MOV_DR_EXITING);
7021 static void vmx_set_dr7(struct kvm_vcpu *vcpu, unsigned long val)
7023 vmcs_writel(GUEST_DR7, val);
7026 static int handle_cpuid(struct kvm_vcpu *vcpu)
7028 return kvm_emulate_cpuid(vcpu);
7031 static int handle_rdmsr(struct kvm_vcpu *vcpu)
7033 u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX];
7034 struct msr_data msr_info;
7036 msr_info.index = ecx;
7037 msr_info.host_initiated = false;
7038 if (vmx_get_msr(vcpu, &msr_info)) {
7039 trace_kvm_msr_read_ex(ecx);
7040 kvm_inject_gp(vcpu, 0);
7044 trace_kvm_msr_read(ecx, msr_info.data);
7046 /* FIXME: handling of bits 32:63 of rax, rdx */
7047 vcpu->arch.regs[VCPU_REGS_RAX] = msr_info.data & -1u;
7048 vcpu->arch.regs[VCPU_REGS_RDX] = (msr_info.data >> 32) & -1u;
7049 return kvm_skip_emulated_instruction(vcpu);
7052 static int handle_wrmsr(struct kvm_vcpu *vcpu)
7054 struct msr_data msr;
7055 u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX];
7056 u64 data = (vcpu->arch.regs[VCPU_REGS_RAX] & -1u)
7057 | ((u64)(vcpu->arch.regs[VCPU_REGS_RDX] & -1u) << 32);
7061 msr.host_initiated = false;
7062 if (kvm_set_msr(vcpu, &msr) != 0) {
7063 trace_kvm_msr_write_ex(ecx, data);
7064 kvm_inject_gp(vcpu, 0);
7068 trace_kvm_msr_write(ecx, data);
7069 return kvm_skip_emulated_instruction(vcpu);
7072 static int handle_tpr_below_threshold(struct kvm_vcpu *vcpu)
7074 kvm_apic_update_ppr(vcpu);
7078 static int handle_interrupt_window(struct kvm_vcpu *vcpu)
7080 vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL,
7081 CPU_BASED_VIRTUAL_INTR_PENDING);
7083 kvm_make_request(KVM_REQ_EVENT, vcpu);
7085 ++vcpu->stat.irq_window_exits;
7089 static int handle_halt(struct kvm_vcpu *vcpu)
7091 return kvm_emulate_halt(vcpu);
7094 static int handle_vmcall(struct kvm_vcpu *vcpu)
7096 return kvm_emulate_hypercall(vcpu);
7099 static int handle_invd(struct kvm_vcpu *vcpu)
7101 return emulate_instruction(vcpu, 0) == EMULATE_DONE;
7104 static int handle_invlpg(struct kvm_vcpu *vcpu)
7106 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7108 kvm_mmu_invlpg(vcpu, exit_qualification);
7109 return kvm_skip_emulated_instruction(vcpu);
7112 static int handle_rdpmc(struct kvm_vcpu *vcpu)
7116 err = kvm_rdpmc(vcpu);
7117 return kvm_complete_insn_gp(vcpu, err);
7120 static int handle_wbinvd(struct kvm_vcpu *vcpu)
7122 return kvm_emulate_wbinvd(vcpu);
7125 static int handle_xsetbv(struct kvm_vcpu *vcpu)
7127 u64 new_bv = kvm_read_edx_eax(vcpu);
7128 u32 index = kvm_register_read(vcpu, VCPU_REGS_RCX);
7130 if (kvm_set_xcr(vcpu, index, new_bv) == 0)
7131 return kvm_skip_emulated_instruction(vcpu);
7135 static int handle_xsaves(struct kvm_vcpu *vcpu)
7137 kvm_skip_emulated_instruction(vcpu);
7138 WARN(1, "this should never happen\n");
7142 static int handle_xrstors(struct kvm_vcpu *vcpu)
7144 kvm_skip_emulated_instruction(vcpu);
7145 WARN(1, "this should never happen\n");
7149 static int handle_apic_access(struct kvm_vcpu *vcpu)
7151 if (likely(fasteoi)) {
7152 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7153 int access_type, offset;
7155 access_type = exit_qualification & APIC_ACCESS_TYPE;
7156 offset = exit_qualification & APIC_ACCESS_OFFSET;
7158 * Sane guest uses MOV to write EOI, with written value
7159 * not cared. So make a short-circuit here by avoiding
7160 * heavy instruction emulation.
7162 if ((access_type == TYPE_LINEAR_APIC_INST_WRITE) &&
7163 (offset == APIC_EOI)) {
7164 kvm_lapic_set_eoi(vcpu);
7165 return kvm_skip_emulated_instruction(vcpu);
7168 return emulate_instruction(vcpu, 0) == EMULATE_DONE;
7171 static int handle_apic_eoi_induced(struct kvm_vcpu *vcpu)
7173 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7174 int vector = exit_qualification & 0xff;
7176 /* EOI-induced VM exit is trap-like and thus no need to adjust IP */
7177 kvm_apic_set_eoi_accelerated(vcpu, vector);
7181 static int handle_apic_write(struct kvm_vcpu *vcpu)
7183 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7184 u32 offset = exit_qualification & 0xfff;
7186 /* APIC-write VM exit is trap-like and thus no need to adjust IP */
7187 kvm_apic_write_nodecode(vcpu, offset);
7191 static int handle_task_switch(struct kvm_vcpu *vcpu)
7193 struct vcpu_vmx *vmx = to_vmx(vcpu);
7194 unsigned long exit_qualification;
7195 bool has_error_code = false;
7198 int reason, type, idt_v, idt_index;
7200 idt_v = (vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK);
7201 idt_index = (vmx->idt_vectoring_info & VECTORING_INFO_VECTOR_MASK);
7202 type = (vmx->idt_vectoring_info & VECTORING_INFO_TYPE_MASK);
7204 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7206 reason = (u32)exit_qualification >> 30;
7207 if (reason == TASK_SWITCH_GATE && idt_v) {
7209 case INTR_TYPE_NMI_INTR:
7210 vcpu->arch.nmi_injected = false;
7211 vmx_set_nmi_mask(vcpu, true);
7213 case INTR_TYPE_EXT_INTR:
7214 case INTR_TYPE_SOFT_INTR:
7215 kvm_clear_interrupt_queue(vcpu);
7217 case INTR_TYPE_HARD_EXCEPTION:
7218 if (vmx->idt_vectoring_info &
7219 VECTORING_INFO_DELIVER_CODE_MASK) {
7220 has_error_code = true;
7222 vmcs_read32(IDT_VECTORING_ERROR_CODE);
7225 case INTR_TYPE_SOFT_EXCEPTION:
7226 kvm_clear_exception_queue(vcpu);
7232 tss_selector = exit_qualification;
7234 if (!idt_v || (type != INTR_TYPE_HARD_EXCEPTION &&
7235 type != INTR_TYPE_EXT_INTR &&
7236 type != INTR_TYPE_NMI_INTR))
7237 skip_emulated_instruction(vcpu);
7239 if (kvm_task_switch(vcpu, tss_selector,
7240 type == INTR_TYPE_SOFT_INTR ? idt_index : -1, reason,
7241 has_error_code, error_code) == EMULATE_FAIL) {
7242 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
7243 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
7244 vcpu->run->internal.ndata = 0;
7249 * TODO: What about debug traps on tss switch?
7250 * Are we supposed to inject them and update dr6?
7256 static int handle_ept_violation(struct kvm_vcpu *vcpu)
7258 unsigned long exit_qualification;
7262 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7265 * EPT violation happened while executing iret from NMI,
7266 * "blocked by NMI" bit has to be set before next VM entry.
7267 * There are errata that may cause this bit to not be set:
7270 if (!(to_vmx(vcpu)->idt_vectoring_info & VECTORING_INFO_VALID_MASK) &&
7272 (exit_qualification & INTR_INFO_UNBLOCK_NMI))
7273 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO, GUEST_INTR_STATE_NMI);
7275 gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS);
7276 trace_kvm_page_fault(gpa, exit_qualification);
7278 /* Is it a read fault? */
7279 error_code = (exit_qualification & EPT_VIOLATION_ACC_READ)
7280 ? PFERR_USER_MASK : 0;
7281 /* Is it a write fault? */
7282 error_code |= (exit_qualification & EPT_VIOLATION_ACC_WRITE)
7283 ? PFERR_WRITE_MASK : 0;
7284 /* Is it a fetch fault? */
7285 error_code |= (exit_qualification & EPT_VIOLATION_ACC_INSTR)
7286 ? PFERR_FETCH_MASK : 0;
7287 /* ept page table entry is present? */
7288 error_code |= (exit_qualification &
7289 (EPT_VIOLATION_READABLE | EPT_VIOLATION_WRITABLE |
7290 EPT_VIOLATION_EXECUTABLE))
7291 ? PFERR_PRESENT_MASK : 0;
7293 error_code |= (exit_qualification & 0x100) != 0 ?
7294 PFERR_GUEST_FINAL_MASK : PFERR_GUEST_PAGE_MASK;
7296 vcpu->arch.exit_qualification = exit_qualification;
7297 return kvm_mmu_page_fault(vcpu, gpa, error_code, NULL, 0);
7300 static int handle_ept_misconfig(struct kvm_vcpu *vcpu)
7305 * A nested guest cannot optimize MMIO vmexits, because we have an
7306 * nGPA here instead of the required GPA.
7308 gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS);
7309 if (!is_guest_mode(vcpu) &&
7310 !kvm_io_bus_write(vcpu, KVM_FAST_MMIO_BUS, gpa, 0, NULL)) {
7311 trace_kvm_fast_mmio(gpa);
7313 * Doing kvm_skip_emulated_instruction() depends on undefined
7314 * behavior: Intel's manual doesn't mandate
7315 * VM_EXIT_INSTRUCTION_LEN to be set in VMCS when EPT MISCONFIG
7316 * occurs and while on real hardware it was observed to be set,
7317 * other hypervisors (namely Hyper-V) don't set it, we end up
7318 * advancing IP with some random value. Disable fast mmio when
7319 * running nested and keep it for real hardware in hope that
7320 * VM_EXIT_INSTRUCTION_LEN will always be set correctly.
7322 if (!static_cpu_has(X86_FEATURE_HYPERVISOR))
7323 return kvm_skip_emulated_instruction(vcpu);
7325 return x86_emulate_instruction(vcpu, gpa, EMULTYPE_SKIP,
7326 NULL, 0) == EMULATE_DONE;
7329 return kvm_mmu_page_fault(vcpu, gpa, PFERR_RSVD_MASK, NULL, 0);
7332 static int handle_nmi_window(struct kvm_vcpu *vcpu)
7334 WARN_ON_ONCE(!enable_vnmi);
7335 vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL,
7336 CPU_BASED_VIRTUAL_NMI_PENDING);
7337 ++vcpu->stat.nmi_window_exits;
7338 kvm_make_request(KVM_REQ_EVENT, vcpu);
7343 static int handle_invalid_guest_state(struct kvm_vcpu *vcpu)
7345 struct vcpu_vmx *vmx = to_vmx(vcpu);
7346 enum emulation_result err = EMULATE_DONE;
7349 bool intr_window_requested;
7350 unsigned count = 130;
7353 * We should never reach the point where we are emulating L2
7354 * due to invalid guest state as that means we incorrectly
7355 * allowed a nested VMEntry with an invalid vmcs12.
7357 WARN_ON_ONCE(vmx->emulation_required && vmx->nested.nested_run_pending);
7359 cpu_exec_ctrl = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
7360 intr_window_requested = cpu_exec_ctrl & CPU_BASED_VIRTUAL_INTR_PENDING;
7362 while (vmx->emulation_required && count-- != 0) {
7363 if (intr_window_requested && vmx_interrupt_allowed(vcpu))
7364 return handle_interrupt_window(&vmx->vcpu);
7366 if (kvm_test_request(KVM_REQ_EVENT, vcpu))
7369 err = emulate_instruction(vcpu, 0);
7371 if (err == EMULATE_USER_EXIT) {
7372 ++vcpu->stat.mmio_exits;
7377 if (err != EMULATE_DONE)
7378 goto emulation_error;
7380 if (vmx->emulation_required && !vmx->rmode.vm86_active &&
7381 vcpu->arch.exception.pending)
7382 goto emulation_error;
7384 if (vcpu->arch.halt_request) {
7385 vcpu->arch.halt_request = 0;
7386 ret = kvm_vcpu_halt(vcpu);
7390 if (signal_pending(current))
7400 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
7401 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
7402 vcpu->run->internal.ndata = 0;
7406 static void grow_ple_window(struct kvm_vcpu *vcpu)
7408 struct vcpu_vmx *vmx = to_vmx(vcpu);
7409 int old = vmx->ple_window;
7411 vmx->ple_window = __grow_ple_window(old, ple_window,
7415 if (vmx->ple_window != old)
7416 vmx->ple_window_dirty = true;
7418 trace_kvm_ple_window_grow(vcpu->vcpu_id, vmx->ple_window, old);
7421 static void shrink_ple_window(struct kvm_vcpu *vcpu)
7423 struct vcpu_vmx *vmx = to_vmx(vcpu);
7424 int old = vmx->ple_window;
7426 vmx->ple_window = __shrink_ple_window(old, ple_window,
7430 if (vmx->ple_window != old)
7431 vmx->ple_window_dirty = true;
7433 trace_kvm_ple_window_shrink(vcpu->vcpu_id, vmx->ple_window, old);
7437 * Handler for POSTED_INTERRUPT_WAKEUP_VECTOR.
7439 static void wakeup_handler(void)
7441 struct kvm_vcpu *vcpu;
7442 int cpu = smp_processor_id();
7444 spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, cpu));
7445 list_for_each_entry(vcpu, &per_cpu(blocked_vcpu_on_cpu, cpu),
7446 blocked_vcpu_list) {
7447 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
7449 if (pi_test_on(pi_desc) == 1)
7450 kvm_vcpu_kick(vcpu);
7452 spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, cpu));
7455 static void vmx_enable_tdp(void)
7457 kvm_mmu_set_mask_ptes(VMX_EPT_READABLE_MASK,
7458 enable_ept_ad_bits ? VMX_EPT_ACCESS_BIT : 0ull,
7459 enable_ept_ad_bits ? VMX_EPT_DIRTY_BIT : 0ull,
7460 0ull, VMX_EPT_EXECUTABLE_MASK,
7461 cpu_has_vmx_ept_execute_only() ? 0ull : VMX_EPT_READABLE_MASK,
7462 VMX_EPT_RWX_MASK, 0ull);
7464 ept_set_mmio_spte_mask();
7468 static __init int hardware_setup(void)
7472 rdmsrl_safe(MSR_EFER, &host_efer);
7474 for (i = 0; i < ARRAY_SIZE(vmx_msr_index); ++i)
7475 kvm_define_shared_msr(i, vmx_msr_index[i]);
7477 for (i = 0; i < VMX_BITMAP_NR; i++) {
7478 vmx_bitmap[i] = (unsigned long *)__get_free_page(GFP_KERNEL);
7483 memset(vmx_vmread_bitmap, 0xff, PAGE_SIZE);
7484 memset(vmx_vmwrite_bitmap, 0xff, PAGE_SIZE);
7486 if (setup_vmcs_config(&vmcs_config) < 0) {
7491 if (boot_cpu_has(X86_FEATURE_NX))
7492 kvm_enable_efer_bits(EFER_NX);
7494 if (!cpu_has_vmx_vpid() || !cpu_has_vmx_invvpid() ||
7495 !(cpu_has_vmx_invvpid_single() || cpu_has_vmx_invvpid_global()))
7498 if (!cpu_has_vmx_ept() ||
7499 !cpu_has_vmx_ept_4levels() ||
7500 !cpu_has_vmx_ept_mt_wb() ||
7501 !cpu_has_vmx_invept_global())
7504 if (!cpu_has_vmx_ept_ad_bits() || !enable_ept)
7505 enable_ept_ad_bits = 0;
7507 if (!cpu_has_vmx_unrestricted_guest() || !enable_ept)
7508 enable_unrestricted_guest = 0;
7510 if (!cpu_has_vmx_flexpriority())
7511 flexpriority_enabled = 0;
7513 if (!cpu_has_virtual_nmis())
7517 * set_apic_access_page_addr() is used to reload apic access
7518 * page upon invalidation. No need to do anything if not
7519 * using the APIC_ACCESS_ADDR VMCS field.
7521 if (!flexpriority_enabled)
7522 kvm_x86_ops->set_apic_access_page_addr = NULL;
7524 if (!cpu_has_vmx_tpr_shadow())
7525 kvm_x86_ops->update_cr8_intercept = NULL;
7527 if (enable_ept && !cpu_has_vmx_ept_2m_page())
7528 kvm_disable_largepages();
7530 if (!cpu_has_vmx_ple()) {
7533 ple_window_grow = 0;
7535 ple_window_shrink = 0;
7538 if (!cpu_has_vmx_apicv()) {
7540 kvm_x86_ops->sync_pir_to_irr = NULL;
7543 if (cpu_has_vmx_tsc_scaling()) {
7544 kvm_has_tsc_control = true;
7545 kvm_max_tsc_scaling_ratio = KVM_VMX_TSC_MULTIPLIER_MAX;
7546 kvm_tsc_scaling_ratio_frac_bits = 48;
7549 set_bit(0, vmx_vpid_bitmap); /* 0 is reserved for host */
7557 * Only enable PML when hardware supports PML feature, and both EPT
7558 * and EPT A/D bit features are enabled -- PML depends on them to work.
7560 if (!enable_ept || !enable_ept_ad_bits || !cpu_has_vmx_pml())
7564 kvm_x86_ops->slot_enable_log_dirty = NULL;
7565 kvm_x86_ops->slot_disable_log_dirty = NULL;
7566 kvm_x86_ops->flush_log_dirty = NULL;
7567 kvm_x86_ops->enable_log_dirty_pt_masked = NULL;
7570 if (cpu_has_vmx_preemption_timer() && enable_preemption_timer) {
7573 rdmsrl(MSR_IA32_VMX_MISC, vmx_msr);
7574 cpu_preemption_timer_multi =
7575 vmx_msr & VMX_MISC_PREEMPTION_TIMER_RATE_MASK;
7577 kvm_x86_ops->set_hv_timer = NULL;
7578 kvm_x86_ops->cancel_hv_timer = NULL;
7581 if (!cpu_has_vmx_shadow_vmcs())
7582 enable_shadow_vmcs = 0;
7583 if (enable_shadow_vmcs)
7584 init_vmcs_shadow_fields();
7586 kvm_set_posted_intr_wakeup_handler(wakeup_handler);
7587 nested_vmx_setup_ctls_msrs(&vmcs_config.nested, enable_apicv);
7589 kvm_mce_cap_supported |= MCG_LMCE_P;
7591 return alloc_kvm_area();
7594 for (i = 0; i < VMX_BITMAP_NR; i++)
7595 free_page((unsigned long)vmx_bitmap[i]);
7600 static __exit void hardware_unsetup(void)
7604 for (i = 0; i < VMX_BITMAP_NR; i++)
7605 free_page((unsigned long)vmx_bitmap[i]);
7611 * Indicate a busy-waiting vcpu in spinlock. We do not enable the PAUSE
7612 * exiting, so only get here on cpu with PAUSE-Loop-Exiting.
7614 static int handle_pause(struct kvm_vcpu *vcpu)
7616 if (!kvm_pause_in_guest(vcpu->kvm))
7617 grow_ple_window(vcpu);
7620 * Intel sdm vol3 ch-25.1.3 says: The "PAUSE-loop exiting"
7621 * VM-execution control is ignored if CPL > 0. OTOH, KVM
7622 * never set PAUSE_EXITING and just set PLE if supported,
7623 * so the vcpu must be CPL=0 if it gets a PAUSE exit.
7625 kvm_vcpu_on_spin(vcpu, true);
7626 return kvm_skip_emulated_instruction(vcpu);
7629 static int handle_nop(struct kvm_vcpu *vcpu)
7631 return kvm_skip_emulated_instruction(vcpu);
7634 static int handle_mwait(struct kvm_vcpu *vcpu)
7636 printk_once(KERN_WARNING "kvm: MWAIT instruction emulated as NOP!\n");
7637 return handle_nop(vcpu);
7640 static int handle_invalid_op(struct kvm_vcpu *vcpu)
7642 kvm_queue_exception(vcpu, UD_VECTOR);
7646 static int handle_monitor_trap(struct kvm_vcpu *vcpu)
7651 static int handle_monitor(struct kvm_vcpu *vcpu)
7653 printk_once(KERN_WARNING "kvm: MONITOR instruction emulated as NOP!\n");
7654 return handle_nop(vcpu);
7658 * The following 3 functions, nested_vmx_succeed()/failValid()/failInvalid(),
7659 * set the success or error code of an emulated VMX instruction, as specified
7660 * by Vol 2B, VMX Instruction Reference, "Conventions".
7662 static void nested_vmx_succeed(struct kvm_vcpu *vcpu)
7664 vmx_set_rflags(vcpu, vmx_get_rflags(vcpu)
7665 & ~(X86_EFLAGS_CF | X86_EFLAGS_PF | X86_EFLAGS_AF |
7666 X86_EFLAGS_ZF | X86_EFLAGS_SF | X86_EFLAGS_OF));
7669 static void nested_vmx_failInvalid(struct kvm_vcpu *vcpu)
7671 vmx_set_rflags(vcpu, (vmx_get_rflags(vcpu)
7672 & ~(X86_EFLAGS_PF | X86_EFLAGS_AF | X86_EFLAGS_ZF |
7673 X86_EFLAGS_SF | X86_EFLAGS_OF))
7677 static void nested_vmx_failValid(struct kvm_vcpu *vcpu,
7678 u32 vm_instruction_error)
7680 if (to_vmx(vcpu)->nested.current_vmptr == -1ull) {
7682 * failValid writes the error number to the current VMCS, which
7683 * can't be done there isn't a current VMCS.
7685 nested_vmx_failInvalid(vcpu);
7688 vmx_set_rflags(vcpu, (vmx_get_rflags(vcpu)
7689 & ~(X86_EFLAGS_CF | X86_EFLAGS_PF | X86_EFLAGS_AF |
7690 X86_EFLAGS_SF | X86_EFLAGS_OF))
7692 get_vmcs12(vcpu)->vm_instruction_error = vm_instruction_error;
7694 * We don't need to force a shadow sync because
7695 * VM_INSTRUCTION_ERROR is not shadowed
7699 static void nested_vmx_abort(struct kvm_vcpu *vcpu, u32 indicator)
7701 /* TODO: not to reset guest simply here. */
7702 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
7703 pr_debug_ratelimited("kvm: nested vmx abort, indicator %d\n", indicator);
7706 static enum hrtimer_restart vmx_preemption_timer_fn(struct hrtimer *timer)
7708 struct vcpu_vmx *vmx =
7709 container_of(timer, struct vcpu_vmx, nested.preemption_timer);
7711 vmx->nested.preemption_timer_expired = true;
7712 kvm_make_request(KVM_REQ_EVENT, &vmx->vcpu);
7713 kvm_vcpu_kick(&vmx->vcpu);
7715 return HRTIMER_NORESTART;
7719 * Decode the memory-address operand of a vmx instruction, as recorded on an
7720 * exit caused by such an instruction (run by a guest hypervisor).
7721 * On success, returns 0. When the operand is invalid, returns 1 and throws
7724 static int get_vmx_mem_address(struct kvm_vcpu *vcpu,
7725 unsigned long exit_qualification,
7726 u32 vmx_instruction_info, bool wr, gva_t *ret)
7730 struct kvm_segment s;
7733 * According to Vol. 3B, "Information for VM Exits Due to Instruction
7734 * Execution", on an exit, vmx_instruction_info holds most of the
7735 * addressing components of the operand. Only the displacement part
7736 * is put in exit_qualification (see 3B, "Basic VM-Exit Information").
7737 * For how an actual address is calculated from all these components,
7738 * refer to Vol. 1, "Operand Addressing".
7740 int scaling = vmx_instruction_info & 3;
7741 int addr_size = (vmx_instruction_info >> 7) & 7;
7742 bool is_reg = vmx_instruction_info & (1u << 10);
7743 int seg_reg = (vmx_instruction_info >> 15) & 7;
7744 int index_reg = (vmx_instruction_info >> 18) & 0xf;
7745 bool index_is_valid = !(vmx_instruction_info & (1u << 22));
7746 int base_reg = (vmx_instruction_info >> 23) & 0xf;
7747 bool base_is_valid = !(vmx_instruction_info & (1u << 27));
7750 kvm_queue_exception(vcpu, UD_VECTOR);
7754 /* Addr = segment_base + offset */
7755 /* offset = base + [index * scale] + displacement */
7756 off = exit_qualification; /* holds the displacement */
7758 off += kvm_register_read(vcpu, base_reg);
7760 off += kvm_register_read(vcpu, index_reg)<<scaling;
7761 vmx_get_segment(vcpu, &s, seg_reg);
7762 *ret = s.base + off;
7764 if (addr_size == 1) /* 32 bit */
7767 /* Checks for #GP/#SS exceptions. */
7769 if (is_long_mode(vcpu)) {
7770 /* Long mode: #GP(0)/#SS(0) if the memory address is in a
7771 * non-canonical form. This is the only check on the memory
7772 * destination for long mode!
7774 exn = is_noncanonical_address(*ret, vcpu);
7775 } else if (is_protmode(vcpu)) {
7776 /* Protected mode: apply checks for segment validity in the
7778 * - segment type check (#GP(0) may be thrown)
7779 * - usability check (#GP(0)/#SS(0))
7780 * - limit check (#GP(0)/#SS(0))
7783 /* #GP(0) if the destination operand is located in a
7784 * read-only data segment or any code segment.
7786 exn = ((s.type & 0xa) == 0 || (s.type & 8));
7788 /* #GP(0) if the source operand is located in an
7789 * execute-only code segment
7791 exn = ((s.type & 0xa) == 8);
7793 kvm_queue_exception_e(vcpu, GP_VECTOR, 0);
7796 /* Protected mode: #GP(0)/#SS(0) if the segment is unusable.
7798 exn = (s.unusable != 0);
7799 /* Protected mode: #GP(0)/#SS(0) if the memory
7800 * operand is outside the segment limit.
7802 exn = exn || (off + sizeof(u64) > s.limit);
7805 kvm_queue_exception_e(vcpu,
7806 seg_reg == VCPU_SREG_SS ?
7807 SS_VECTOR : GP_VECTOR,
7815 static int nested_vmx_get_vmptr(struct kvm_vcpu *vcpu, gpa_t *vmpointer)
7818 struct x86_exception e;
7820 if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
7821 vmcs_read32(VMX_INSTRUCTION_INFO), false, &gva))
7824 if (kvm_read_guest_virt(vcpu, gva, vmpointer, sizeof(*vmpointer), &e)) {
7825 kvm_inject_page_fault(vcpu, &e);
7832 static int enter_vmx_operation(struct kvm_vcpu *vcpu)
7834 struct vcpu_vmx *vmx = to_vmx(vcpu);
7835 struct vmcs *shadow_vmcs;
7838 r = alloc_loaded_vmcs(&vmx->nested.vmcs02);
7842 vmx->nested.cached_vmcs12 = kmalloc(VMCS12_SIZE, GFP_KERNEL);
7843 if (!vmx->nested.cached_vmcs12)
7844 goto out_cached_vmcs12;
7846 if (enable_shadow_vmcs) {
7847 shadow_vmcs = alloc_vmcs();
7849 goto out_shadow_vmcs;
7850 /* mark vmcs as shadow */
7851 shadow_vmcs->revision_id |= (1u << 31);
7852 /* init shadow vmcs */
7853 vmcs_clear(shadow_vmcs);
7854 vmx->vmcs01.shadow_vmcs = shadow_vmcs;
7857 hrtimer_init(&vmx->nested.preemption_timer, CLOCK_MONOTONIC,
7858 HRTIMER_MODE_REL_PINNED);
7859 vmx->nested.preemption_timer.function = vmx_preemption_timer_fn;
7861 vmx->nested.vmxon = true;
7865 kfree(vmx->nested.cached_vmcs12);
7868 free_loaded_vmcs(&vmx->nested.vmcs02);
7875 * Emulate the VMXON instruction.
7876 * Currently, we just remember that VMX is active, and do not save or even
7877 * inspect the argument to VMXON (the so-called "VMXON pointer") because we
7878 * do not currently need to store anything in that guest-allocated memory
7879 * region. Consequently, VMCLEAR and VMPTRLD also do not verify that the their
7880 * argument is different from the VMXON pointer (which the spec says they do).
7882 static int handle_vmon(struct kvm_vcpu *vcpu)
7887 struct vcpu_vmx *vmx = to_vmx(vcpu);
7888 const u64 VMXON_NEEDED_FEATURES = FEATURE_CONTROL_LOCKED
7889 | FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
7892 * The Intel VMX Instruction Reference lists a bunch of bits that are
7893 * prerequisite to running VMXON, most notably cr4.VMXE must be set to
7894 * 1 (see vmx_set_cr4() for when we allow the guest to set this).
7895 * Otherwise, we should fail with #UD. But most faulting conditions
7896 * have already been checked by hardware, prior to the VM-exit for
7897 * VMXON. We do test guest cr4.VMXE because processor CR4 always has
7898 * that bit set to 1 in non-root mode.
7900 if (!kvm_read_cr4_bits(vcpu, X86_CR4_VMXE)) {
7901 kvm_queue_exception(vcpu, UD_VECTOR);
7905 /* CPL=0 must be checked manually. */
7906 if (vmx_get_cpl(vcpu)) {
7907 kvm_queue_exception(vcpu, UD_VECTOR);
7911 if (vmx->nested.vmxon) {
7912 nested_vmx_failValid(vcpu, VMXERR_VMXON_IN_VMX_ROOT_OPERATION);
7913 return kvm_skip_emulated_instruction(vcpu);
7916 if ((vmx->msr_ia32_feature_control & VMXON_NEEDED_FEATURES)
7917 != VMXON_NEEDED_FEATURES) {
7918 kvm_inject_gp(vcpu, 0);
7922 if (nested_vmx_get_vmptr(vcpu, &vmptr))
7927 * The first 4 bytes of VMXON region contain the supported
7928 * VMCS revision identifier
7930 * Note - IA32_VMX_BASIC[48] will never be 1 for the nested case;
7931 * which replaces physical address width with 32
7933 if (!PAGE_ALIGNED(vmptr) || (vmptr >> cpuid_maxphyaddr(vcpu))) {
7934 nested_vmx_failInvalid(vcpu);
7935 return kvm_skip_emulated_instruction(vcpu);
7938 page = kvm_vcpu_gpa_to_page(vcpu, vmptr);
7939 if (is_error_page(page)) {
7940 nested_vmx_failInvalid(vcpu);
7941 return kvm_skip_emulated_instruction(vcpu);
7943 if (*(u32 *)kmap(page) != VMCS12_REVISION) {
7945 kvm_release_page_clean(page);
7946 nested_vmx_failInvalid(vcpu);
7947 return kvm_skip_emulated_instruction(vcpu);
7950 kvm_release_page_clean(page);
7952 vmx->nested.vmxon_ptr = vmptr;
7953 ret = enter_vmx_operation(vcpu);
7957 nested_vmx_succeed(vcpu);
7958 return kvm_skip_emulated_instruction(vcpu);
7962 * Intel's VMX Instruction Reference specifies a common set of prerequisites
7963 * for running VMX instructions (except VMXON, whose prerequisites are
7964 * slightly different). It also specifies what exception to inject otherwise.
7965 * Note that many of these exceptions have priority over VM exits, so they
7966 * don't have to be checked again here.
7968 static int nested_vmx_check_permission(struct kvm_vcpu *vcpu)
7970 if (vmx_get_cpl(vcpu)) {
7971 kvm_queue_exception(vcpu, UD_VECTOR);
7975 if (!to_vmx(vcpu)->nested.vmxon) {
7976 kvm_queue_exception(vcpu, UD_VECTOR);
7982 static void vmx_disable_shadow_vmcs(struct vcpu_vmx *vmx)
7984 vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL, SECONDARY_EXEC_SHADOW_VMCS);
7985 vmcs_write64(VMCS_LINK_POINTER, -1ull);
7988 static inline void nested_release_vmcs12(struct vcpu_vmx *vmx)
7990 if (vmx->nested.current_vmptr == -1ull)
7993 if (enable_shadow_vmcs) {
7994 /* copy to memory all shadowed fields in case
7995 they were modified */
7996 copy_shadow_to_vmcs12(vmx);
7997 vmx->nested.sync_shadow_vmcs = false;
7998 vmx_disable_shadow_vmcs(vmx);
8000 vmx->nested.posted_intr_nv = -1;
8002 /* Flush VMCS12 to guest memory */
8003 kvm_vcpu_write_guest_page(&vmx->vcpu,
8004 vmx->nested.current_vmptr >> PAGE_SHIFT,
8005 vmx->nested.cached_vmcs12, 0, VMCS12_SIZE);
8007 vmx->nested.current_vmptr = -1ull;
8011 * Free whatever needs to be freed from vmx->nested when L1 goes down, or
8012 * just stops using VMX.
8014 static void free_nested(struct vcpu_vmx *vmx)
8016 if (!vmx->nested.vmxon && !vmx->nested.smm.vmxon)
8019 vmx->nested.vmxon = false;
8020 vmx->nested.smm.vmxon = false;
8021 free_vpid(vmx->nested.vpid02);
8022 vmx->nested.posted_intr_nv = -1;
8023 vmx->nested.current_vmptr = -1ull;
8024 if (enable_shadow_vmcs) {
8025 vmx_disable_shadow_vmcs(vmx);
8026 vmcs_clear(vmx->vmcs01.shadow_vmcs);
8027 free_vmcs(vmx->vmcs01.shadow_vmcs);
8028 vmx->vmcs01.shadow_vmcs = NULL;
8030 kfree(vmx->nested.cached_vmcs12);
8031 /* Unpin physical memory we referred to in the vmcs02 */
8032 if (vmx->nested.apic_access_page) {
8033 kvm_release_page_dirty(vmx->nested.apic_access_page);
8034 vmx->nested.apic_access_page = NULL;
8036 if (vmx->nested.virtual_apic_page) {
8037 kvm_release_page_dirty(vmx->nested.virtual_apic_page);
8038 vmx->nested.virtual_apic_page = NULL;
8040 if (vmx->nested.pi_desc_page) {
8041 kunmap(vmx->nested.pi_desc_page);
8042 kvm_release_page_dirty(vmx->nested.pi_desc_page);
8043 vmx->nested.pi_desc_page = NULL;
8044 vmx->nested.pi_desc = NULL;
8047 free_loaded_vmcs(&vmx->nested.vmcs02);
8050 /* Emulate the VMXOFF instruction */
8051 static int handle_vmoff(struct kvm_vcpu *vcpu)
8053 if (!nested_vmx_check_permission(vcpu))
8055 free_nested(to_vmx(vcpu));
8056 nested_vmx_succeed(vcpu);
8057 return kvm_skip_emulated_instruction(vcpu);
8060 /* Emulate the VMCLEAR instruction */
8061 static int handle_vmclear(struct kvm_vcpu *vcpu)
8063 struct vcpu_vmx *vmx = to_vmx(vcpu);
8067 if (!nested_vmx_check_permission(vcpu))
8070 if (nested_vmx_get_vmptr(vcpu, &vmptr))
8073 if (!PAGE_ALIGNED(vmptr) || (vmptr >> cpuid_maxphyaddr(vcpu))) {
8074 nested_vmx_failValid(vcpu, VMXERR_VMCLEAR_INVALID_ADDRESS);
8075 return kvm_skip_emulated_instruction(vcpu);
8078 if (vmptr == vmx->nested.vmxon_ptr) {
8079 nested_vmx_failValid(vcpu, VMXERR_VMCLEAR_VMXON_POINTER);
8080 return kvm_skip_emulated_instruction(vcpu);
8083 if (vmptr == vmx->nested.current_vmptr)
8084 nested_release_vmcs12(vmx);
8086 kvm_vcpu_write_guest(vcpu,
8087 vmptr + offsetof(struct vmcs12, launch_state),
8088 &zero, sizeof(zero));
8090 nested_vmx_succeed(vcpu);
8091 return kvm_skip_emulated_instruction(vcpu);
8094 static int nested_vmx_run(struct kvm_vcpu *vcpu, bool launch);
8096 /* Emulate the VMLAUNCH instruction */
8097 static int handle_vmlaunch(struct kvm_vcpu *vcpu)
8099 return nested_vmx_run(vcpu, true);
8102 /* Emulate the VMRESUME instruction */
8103 static int handle_vmresume(struct kvm_vcpu *vcpu)
8106 return nested_vmx_run(vcpu, false);
8110 * Read a vmcs12 field. Since these can have varying lengths and we return
8111 * one type, we chose the biggest type (u64) and zero-extend the return value
8112 * to that size. Note that the caller, handle_vmread, might need to use only
8113 * some of the bits we return here (e.g., on 32-bit guests, only 32 bits of
8114 * 64-bit fields are to be returned).
8116 static inline int vmcs12_read_any(struct kvm_vcpu *vcpu,
8117 unsigned long field, u64 *ret)
8119 short offset = vmcs_field_to_offset(field);
8125 p = ((char *)(get_vmcs12(vcpu))) + offset;
8127 switch (vmcs_field_width(field)) {
8128 case VMCS_FIELD_WIDTH_NATURAL_WIDTH:
8129 *ret = *((natural_width *)p);
8131 case VMCS_FIELD_WIDTH_U16:
8134 case VMCS_FIELD_WIDTH_U32:
8137 case VMCS_FIELD_WIDTH_U64:
8147 static inline int vmcs12_write_any(struct kvm_vcpu *vcpu,
8148 unsigned long field, u64 field_value){
8149 short offset = vmcs_field_to_offset(field);
8150 char *p = ((char *) get_vmcs12(vcpu)) + offset;
8154 switch (vmcs_field_width(field)) {
8155 case VMCS_FIELD_WIDTH_U16:
8156 *(u16 *)p = field_value;
8158 case VMCS_FIELD_WIDTH_U32:
8159 *(u32 *)p = field_value;
8161 case VMCS_FIELD_WIDTH_U64:
8162 *(u64 *)p = field_value;
8164 case VMCS_FIELD_WIDTH_NATURAL_WIDTH:
8165 *(natural_width *)p = field_value;
8175 * Copy the writable VMCS shadow fields back to the VMCS12, in case
8176 * they have been modified by the L1 guest. Note that the "read-only"
8177 * VM-exit information fields are actually writable if the vCPU is
8178 * configured to support "VMWRITE to any supported field in the VMCS."
8180 static void copy_shadow_to_vmcs12(struct vcpu_vmx *vmx)
8182 const u16 *fields[] = {
8183 shadow_read_write_fields,
8184 shadow_read_only_fields
8186 const int max_fields[] = {
8187 max_shadow_read_write_fields,
8188 max_shadow_read_only_fields
8191 unsigned long field;
8193 struct vmcs *shadow_vmcs = vmx->vmcs01.shadow_vmcs;
8197 vmcs_load(shadow_vmcs);
8199 for (q = 0; q < ARRAY_SIZE(fields); q++) {
8200 for (i = 0; i < max_fields[q]; i++) {
8201 field = fields[q][i];
8202 field_value = __vmcs_readl(field);
8203 vmcs12_write_any(&vmx->vcpu, field, field_value);
8206 * Skip the VM-exit information fields if they are read-only.
8208 if (!nested_cpu_has_vmwrite_any_field(&vmx->vcpu))
8212 vmcs_clear(shadow_vmcs);
8213 vmcs_load(vmx->loaded_vmcs->vmcs);
8218 static void copy_vmcs12_to_shadow(struct vcpu_vmx *vmx)
8220 const u16 *fields[] = {
8221 shadow_read_write_fields,
8222 shadow_read_only_fields
8224 const int max_fields[] = {
8225 max_shadow_read_write_fields,
8226 max_shadow_read_only_fields
8229 unsigned long field;
8230 u64 field_value = 0;
8231 struct vmcs *shadow_vmcs = vmx->vmcs01.shadow_vmcs;
8233 vmcs_load(shadow_vmcs);
8235 for (q = 0; q < ARRAY_SIZE(fields); q++) {
8236 for (i = 0; i < max_fields[q]; i++) {
8237 field = fields[q][i];
8238 vmcs12_read_any(&vmx->vcpu, field, &field_value);
8239 __vmcs_writel(field, field_value);
8243 vmcs_clear(shadow_vmcs);
8244 vmcs_load(vmx->loaded_vmcs->vmcs);
8248 * VMX instructions which assume a current vmcs12 (i.e., that VMPTRLD was
8249 * used before) all generate the same failure when it is missing.
8251 static int nested_vmx_check_vmcs12(struct kvm_vcpu *vcpu)
8253 struct vcpu_vmx *vmx = to_vmx(vcpu);
8254 if (vmx->nested.current_vmptr == -1ull) {
8255 nested_vmx_failInvalid(vcpu);
8261 static int handle_vmread(struct kvm_vcpu *vcpu)
8263 unsigned long field;
8265 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
8266 u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
8269 if (!nested_vmx_check_permission(vcpu))
8272 if (!nested_vmx_check_vmcs12(vcpu))
8273 return kvm_skip_emulated_instruction(vcpu);
8275 /* Decode instruction info and find the field to read */
8276 field = kvm_register_readl(vcpu, (((vmx_instruction_info) >> 28) & 0xf));
8277 /* Read the field, zero-extended to a u64 field_value */
8278 if (vmcs12_read_any(vcpu, field, &field_value) < 0) {
8279 nested_vmx_failValid(vcpu, VMXERR_UNSUPPORTED_VMCS_COMPONENT);
8280 return kvm_skip_emulated_instruction(vcpu);
8283 * Now copy part of this value to register or memory, as requested.
8284 * Note that the number of bits actually copied is 32 or 64 depending
8285 * on the guest's mode (32 or 64 bit), not on the given field's length.
8287 if (vmx_instruction_info & (1u << 10)) {
8288 kvm_register_writel(vcpu, (((vmx_instruction_info) >> 3) & 0xf),
8291 if (get_vmx_mem_address(vcpu, exit_qualification,
8292 vmx_instruction_info, true, &gva))
8294 /* _system ok, nested_vmx_check_permission has verified cpl=0 */
8295 kvm_write_guest_virt_system(vcpu, gva, &field_value,
8296 (is_long_mode(vcpu) ? 8 : 4), NULL);
8299 nested_vmx_succeed(vcpu);
8300 return kvm_skip_emulated_instruction(vcpu);
8304 static int handle_vmwrite(struct kvm_vcpu *vcpu)
8306 unsigned long field;
8308 struct vcpu_vmx *vmx = to_vmx(vcpu);
8309 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
8310 u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
8312 /* The value to write might be 32 or 64 bits, depending on L1's long
8313 * mode, and eventually we need to write that into a field of several
8314 * possible lengths. The code below first zero-extends the value to 64
8315 * bit (field_value), and then copies only the appropriate number of
8316 * bits into the vmcs12 field.
8318 u64 field_value = 0;
8319 struct x86_exception e;
8321 if (!nested_vmx_check_permission(vcpu))
8324 if (!nested_vmx_check_vmcs12(vcpu))
8325 return kvm_skip_emulated_instruction(vcpu);
8327 if (vmx_instruction_info & (1u << 10))
8328 field_value = kvm_register_readl(vcpu,
8329 (((vmx_instruction_info) >> 3) & 0xf));
8331 if (get_vmx_mem_address(vcpu, exit_qualification,
8332 vmx_instruction_info, false, &gva))
8334 if (kvm_read_guest_virt(vcpu, gva, &field_value,
8335 (is_64_bit_mode(vcpu) ? 8 : 4), &e)) {
8336 kvm_inject_page_fault(vcpu, &e);
8342 field = kvm_register_readl(vcpu, (((vmx_instruction_info) >> 28) & 0xf));
8344 * If the vCPU supports "VMWRITE to any supported field in the
8345 * VMCS," then the "read-only" fields are actually read/write.
8347 if (vmcs_field_readonly(field) &&
8348 !nested_cpu_has_vmwrite_any_field(vcpu)) {
8349 nested_vmx_failValid(vcpu,
8350 VMXERR_VMWRITE_READ_ONLY_VMCS_COMPONENT);
8351 return kvm_skip_emulated_instruction(vcpu);
8354 if (vmcs12_write_any(vcpu, field, field_value) < 0) {
8355 nested_vmx_failValid(vcpu, VMXERR_UNSUPPORTED_VMCS_COMPONENT);
8356 return kvm_skip_emulated_instruction(vcpu);
8360 #define SHADOW_FIELD_RW(x) case x:
8361 #include "vmx_shadow_fields.h"
8363 * The fields that can be updated by L1 without a vmexit are
8364 * always updated in the vmcs02, the others go down the slow
8365 * path of prepare_vmcs02.
8369 vmx->nested.dirty_vmcs12 = true;
8373 nested_vmx_succeed(vcpu);
8374 return kvm_skip_emulated_instruction(vcpu);
8377 static void set_current_vmptr(struct vcpu_vmx *vmx, gpa_t vmptr)
8379 vmx->nested.current_vmptr = vmptr;
8380 if (enable_shadow_vmcs) {
8381 vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL,
8382 SECONDARY_EXEC_SHADOW_VMCS);
8383 vmcs_write64(VMCS_LINK_POINTER,
8384 __pa(vmx->vmcs01.shadow_vmcs));
8385 vmx->nested.sync_shadow_vmcs = true;
8387 vmx->nested.dirty_vmcs12 = true;
8390 /* Emulate the VMPTRLD instruction */
8391 static int handle_vmptrld(struct kvm_vcpu *vcpu)
8393 struct vcpu_vmx *vmx = to_vmx(vcpu);
8396 if (!nested_vmx_check_permission(vcpu))
8399 if (nested_vmx_get_vmptr(vcpu, &vmptr))
8402 if (!PAGE_ALIGNED(vmptr) || (vmptr >> cpuid_maxphyaddr(vcpu))) {
8403 nested_vmx_failValid(vcpu, VMXERR_VMPTRLD_INVALID_ADDRESS);
8404 return kvm_skip_emulated_instruction(vcpu);
8407 if (vmptr == vmx->nested.vmxon_ptr) {
8408 nested_vmx_failValid(vcpu, VMXERR_VMPTRLD_VMXON_POINTER);
8409 return kvm_skip_emulated_instruction(vcpu);
8412 if (vmx->nested.current_vmptr != vmptr) {
8413 struct vmcs12 *new_vmcs12;
8415 page = kvm_vcpu_gpa_to_page(vcpu, vmptr);
8416 if (is_error_page(page)) {
8417 nested_vmx_failInvalid(vcpu);
8418 return kvm_skip_emulated_instruction(vcpu);
8420 new_vmcs12 = kmap(page);
8421 if (new_vmcs12->revision_id != VMCS12_REVISION) {
8423 kvm_release_page_clean(page);
8424 nested_vmx_failValid(vcpu,
8425 VMXERR_VMPTRLD_INCORRECT_VMCS_REVISION_ID);
8426 return kvm_skip_emulated_instruction(vcpu);
8429 nested_release_vmcs12(vmx);
8431 * Load VMCS12 from guest memory since it is not already
8434 memcpy(vmx->nested.cached_vmcs12, new_vmcs12, VMCS12_SIZE);
8436 kvm_release_page_clean(page);
8438 set_current_vmptr(vmx, vmptr);
8441 nested_vmx_succeed(vcpu);
8442 return kvm_skip_emulated_instruction(vcpu);
8445 /* Emulate the VMPTRST instruction */
8446 static int handle_vmptrst(struct kvm_vcpu *vcpu)
8448 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
8449 u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
8451 struct x86_exception e;
8453 if (!nested_vmx_check_permission(vcpu))
8456 if (get_vmx_mem_address(vcpu, exit_qualification,
8457 vmx_instruction_info, true, &vmcs_gva))
8459 /* *_system ok, nested_vmx_check_permission has verified cpl=0 */
8460 if (kvm_write_guest_virt_system(vcpu, vmcs_gva,
8461 (void *)&to_vmx(vcpu)->nested.current_vmptr,
8463 kvm_inject_page_fault(vcpu, &e);
8466 nested_vmx_succeed(vcpu);
8467 return kvm_skip_emulated_instruction(vcpu);
8470 /* Emulate the INVEPT instruction */
8471 static int handle_invept(struct kvm_vcpu *vcpu)
8473 struct vcpu_vmx *vmx = to_vmx(vcpu);
8474 u32 vmx_instruction_info, types;
8477 struct x86_exception e;
8482 if (!(vmx->nested.msrs.secondary_ctls_high &
8483 SECONDARY_EXEC_ENABLE_EPT) ||
8484 !(vmx->nested.msrs.ept_caps & VMX_EPT_INVEPT_BIT)) {
8485 kvm_queue_exception(vcpu, UD_VECTOR);
8489 if (!nested_vmx_check_permission(vcpu))
8492 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
8493 type = kvm_register_readl(vcpu, (vmx_instruction_info >> 28) & 0xf);
8495 types = (vmx->nested.msrs.ept_caps >> VMX_EPT_EXTENT_SHIFT) & 6;
8497 if (type >= 32 || !(types & (1 << type))) {
8498 nested_vmx_failValid(vcpu,
8499 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
8500 return kvm_skip_emulated_instruction(vcpu);
8503 /* According to the Intel VMX instruction reference, the memory
8504 * operand is read even if it isn't needed (e.g., for type==global)
8506 if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
8507 vmx_instruction_info, false, &gva))
8509 if (kvm_read_guest_virt(vcpu, gva, &operand, sizeof(operand), &e)) {
8510 kvm_inject_page_fault(vcpu, &e);
8515 case VMX_EPT_EXTENT_GLOBAL:
8517 * TODO: track mappings and invalidate
8518 * single context requests appropriately
8520 case VMX_EPT_EXTENT_CONTEXT:
8521 kvm_mmu_sync_roots(vcpu);
8522 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
8523 nested_vmx_succeed(vcpu);
8530 return kvm_skip_emulated_instruction(vcpu);
8533 static int handle_invvpid(struct kvm_vcpu *vcpu)
8535 struct vcpu_vmx *vmx = to_vmx(vcpu);
8536 u32 vmx_instruction_info;
8537 unsigned long type, types;
8539 struct x86_exception e;
8545 if (!(vmx->nested.msrs.secondary_ctls_high &
8546 SECONDARY_EXEC_ENABLE_VPID) ||
8547 !(vmx->nested.msrs.vpid_caps & VMX_VPID_INVVPID_BIT)) {
8548 kvm_queue_exception(vcpu, UD_VECTOR);
8552 if (!nested_vmx_check_permission(vcpu))
8555 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
8556 type = kvm_register_readl(vcpu, (vmx_instruction_info >> 28) & 0xf);
8558 types = (vmx->nested.msrs.vpid_caps &
8559 VMX_VPID_EXTENT_SUPPORTED_MASK) >> 8;
8561 if (type >= 32 || !(types & (1 << type))) {
8562 nested_vmx_failValid(vcpu,
8563 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
8564 return kvm_skip_emulated_instruction(vcpu);
8567 /* according to the intel vmx instruction reference, the memory
8568 * operand is read even if it isn't needed (e.g., for type==global)
8570 if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
8571 vmx_instruction_info, false, &gva))
8573 if (kvm_read_guest_virt(vcpu, gva, &operand, sizeof(operand), &e)) {
8574 kvm_inject_page_fault(vcpu, &e);
8577 if (operand.vpid >> 16) {
8578 nested_vmx_failValid(vcpu,
8579 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
8580 return kvm_skip_emulated_instruction(vcpu);
8584 case VMX_VPID_EXTENT_INDIVIDUAL_ADDR:
8585 if (!operand.vpid ||
8586 is_noncanonical_address(operand.gla, vcpu)) {
8587 nested_vmx_failValid(vcpu,
8588 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
8589 return kvm_skip_emulated_instruction(vcpu);
8591 if (cpu_has_vmx_invvpid_individual_addr() &&
8592 vmx->nested.vpid02) {
8593 __invvpid(VMX_VPID_EXTENT_INDIVIDUAL_ADDR,
8594 vmx->nested.vpid02, operand.gla);
8596 __vmx_flush_tlb(vcpu, vmx->nested.vpid02, true);
8598 case VMX_VPID_EXTENT_SINGLE_CONTEXT:
8599 case VMX_VPID_EXTENT_SINGLE_NON_GLOBAL:
8600 if (!operand.vpid) {
8601 nested_vmx_failValid(vcpu,
8602 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
8603 return kvm_skip_emulated_instruction(vcpu);
8605 __vmx_flush_tlb(vcpu, vmx->nested.vpid02, true);
8607 case VMX_VPID_EXTENT_ALL_CONTEXT:
8608 __vmx_flush_tlb(vcpu, vmx->nested.vpid02, true);
8612 return kvm_skip_emulated_instruction(vcpu);
8615 nested_vmx_succeed(vcpu);
8617 return kvm_skip_emulated_instruction(vcpu);
8620 static int handle_pml_full(struct kvm_vcpu *vcpu)
8622 unsigned long exit_qualification;
8624 trace_kvm_pml_full(vcpu->vcpu_id);
8626 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
8629 * PML buffer FULL happened while executing iret from NMI,
8630 * "blocked by NMI" bit has to be set before next VM entry.
8632 if (!(to_vmx(vcpu)->idt_vectoring_info & VECTORING_INFO_VALID_MASK) &&
8634 (exit_qualification & INTR_INFO_UNBLOCK_NMI))
8635 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
8636 GUEST_INTR_STATE_NMI);
8639 * PML buffer already flushed at beginning of VMEXIT. Nothing to do
8640 * here.., and there's no userspace involvement needed for PML.
8645 static int handle_preemption_timer(struct kvm_vcpu *vcpu)
8647 kvm_lapic_expired_hv_timer(vcpu);
8651 static bool valid_ept_address(struct kvm_vcpu *vcpu, u64 address)
8653 struct vcpu_vmx *vmx = to_vmx(vcpu);
8654 int maxphyaddr = cpuid_maxphyaddr(vcpu);
8656 /* Check for memory type validity */
8657 switch (address & VMX_EPTP_MT_MASK) {
8658 case VMX_EPTP_MT_UC:
8659 if (!(vmx->nested.msrs.ept_caps & VMX_EPTP_UC_BIT))
8662 case VMX_EPTP_MT_WB:
8663 if (!(vmx->nested.msrs.ept_caps & VMX_EPTP_WB_BIT))
8670 /* only 4 levels page-walk length are valid */
8671 if ((address & VMX_EPTP_PWL_MASK) != VMX_EPTP_PWL_4)
8674 /* Reserved bits should not be set */
8675 if (address >> maxphyaddr || ((address >> 7) & 0x1f))
8678 /* AD, if set, should be supported */
8679 if (address & VMX_EPTP_AD_ENABLE_BIT) {
8680 if (!(vmx->nested.msrs.ept_caps & VMX_EPT_AD_BIT))
8687 static int nested_vmx_eptp_switching(struct kvm_vcpu *vcpu,
8688 struct vmcs12 *vmcs12)
8690 u32 index = vcpu->arch.regs[VCPU_REGS_RCX];
8692 bool accessed_dirty;
8693 struct kvm_mmu *mmu = vcpu->arch.walk_mmu;
8695 if (!nested_cpu_has_eptp_switching(vmcs12) ||
8696 !nested_cpu_has_ept(vmcs12))
8699 if (index >= VMFUNC_EPTP_ENTRIES)
8703 if (kvm_vcpu_read_guest_page(vcpu, vmcs12->eptp_list_address >> PAGE_SHIFT,
8704 &address, index * 8, 8))
8707 accessed_dirty = !!(address & VMX_EPTP_AD_ENABLE_BIT);
8710 * If the (L2) guest does a vmfunc to the currently
8711 * active ept pointer, we don't have to do anything else
8713 if (vmcs12->ept_pointer != address) {
8714 if (!valid_ept_address(vcpu, address))
8717 kvm_mmu_unload(vcpu);
8718 mmu->ept_ad = accessed_dirty;
8719 mmu->base_role.ad_disabled = !accessed_dirty;
8720 vmcs12->ept_pointer = address;
8722 * TODO: Check what's the correct approach in case
8723 * mmu reload fails. Currently, we just let the next
8724 * reload potentially fail
8726 kvm_mmu_reload(vcpu);
8732 static int handle_vmfunc(struct kvm_vcpu *vcpu)
8734 struct vcpu_vmx *vmx = to_vmx(vcpu);
8735 struct vmcs12 *vmcs12;
8736 u32 function = vcpu->arch.regs[VCPU_REGS_RAX];
8739 * VMFUNC is only supported for nested guests, but we always enable the
8740 * secondary control for simplicity; for non-nested mode, fake that we
8741 * didn't by injecting #UD.
8743 if (!is_guest_mode(vcpu)) {
8744 kvm_queue_exception(vcpu, UD_VECTOR);
8748 vmcs12 = get_vmcs12(vcpu);
8749 if ((vmcs12->vm_function_control & (1 << function)) == 0)
8754 if (nested_vmx_eptp_switching(vcpu, vmcs12))
8760 return kvm_skip_emulated_instruction(vcpu);
8763 nested_vmx_vmexit(vcpu, vmx->exit_reason,
8764 vmcs_read32(VM_EXIT_INTR_INFO),
8765 vmcs_readl(EXIT_QUALIFICATION));
8770 * The exit handlers return 1 if the exit was handled fully and guest execution
8771 * may resume. Otherwise they set the kvm_run parameter to indicate what needs
8772 * to be done to userspace and return 0.
8774 static int (*const kvm_vmx_exit_handlers[])(struct kvm_vcpu *vcpu) = {
8775 [EXIT_REASON_EXCEPTION_NMI] = handle_exception,
8776 [EXIT_REASON_EXTERNAL_INTERRUPT] = handle_external_interrupt,
8777 [EXIT_REASON_TRIPLE_FAULT] = handle_triple_fault,
8778 [EXIT_REASON_NMI_WINDOW] = handle_nmi_window,
8779 [EXIT_REASON_IO_INSTRUCTION] = handle_io,
8780 [EXIT_REASON_CR_ACCESS] = handle_cr,
8781 [EXIT_REASON_DR_ACCESS] = handle_dr,
8782 [EXIT_REASON_CPUID] = handle_cpuid,
8783 [EXIT_REASON_MSR_READ] = handle_rdmsr,
8784 [EXIT_REASON_MSR_WRITE] = handle_wrmsr,
8785 [EXIT_REASON_PENDING_INTERRUPT] = handle_interrupt_window,
8786 [EXIT_REASON_HLT] = handle_halt,
8787 [EXIT_REASON_INVD] = handle_invd,
8788 [EXIT_REASON_INVLPG] = handle_invlpg,
8789 [EXIT_REASON_RDPMC] = handle_rdpmc,
8790 [EXIT_REASON_VMCALL] = handle_vmcall,
8791 [EXIT_REASON_VMCLEAR] = handle_vmclear,
8792 [EXIT_REASON_VMLAUNCH] = handle_vmlaunch,
8793 [EXIT_REASON_VMPTRLD] = handle_vmptrld,
8794 [EXIT_REASON_VMPTRST] = handle_vmptrst,
8795 [EXIT_REASON_VMREAD] = handle_vmread,
8796 [EXIT_REASON_VMRESUME] = handle_vmresume,
8797 [EXIT_REASON_VMWRITE] = handle_vmwrite,
8798 [EXIT_REASON_VMOFF] = handle_vmoff,
8799 [EXIT_REASON_VMON] = handle_vmon,
8800 [EXIT_REASON_TPR_BELOW_THRESHOLD] = handle_tpr_below_threshold,
8801 [EXIT_REASON_APIC_ACCESS] = handle_apic_access,
8802 [EXIT_REASON_APIC_WRITE] = handle_apic_write,
8803 [EXIT_REASON_EOI_INDUCED] = handle_apic_eoi_induced,
8804 [EXIT_REASON_WBINVD] = handle_wbinvd,
8805 [EXIT_REASON_XSETBV] = handle_xsetbv,
8806 [EXIT_REASON_TASK_SWITCH] = handle_task_switch,
8807 [EXIT_REASON_MCE_DURING_VMENTRY] = handle_machine_check,
8808 [EXIT_REASON_GDTR_IDTR] = handle_desc,
8809 [EXIT_REASON_LDTR_TR] = handle_desc,
8810 [EXIT_REASON_EPT_VIOLATION] = handle_ept_violation,
8811 [EXIT_REASON_EPT_MISCONFIG] = handle_ept_misconfig,
8812 [EXIT_REASON_PAUSE_INSTRUCTION] = handle_pause,
8813 [EXIT_REASON_MWAIT_INSTRUCTION] = handle_mwait,
8814 [EXIT_REASON_MONITOR_TRAP_FLAG] = handle_monitor_trap,
8815 [EXIT_REASON_MONITOR_INSTRUCTION] = handle_monitor,
8816 [EXIT_REASON_INVEPT] = handle_invept,
8817 [EXIT_REASON_INVVPID] = handle_invvpid,
8818 [EXIT_REASON_RDRAND] = handle_invalid_op,
8819 [EXIT_REASON_RDSEED] = handle_invalid_op,
8820 [EXIT_REASON_XSAVES] = handle_xsaves,
8821 [EXIT_REASON_XRSTORS] = handle_xrstors,
8822 [EXIT_REASON_PML_FULL] = handle_pml_full,
8823 [EXIT_REASON_VMFUNC] = handle_vmfunc,
8824 [EXIT_REASON_PREEMPTION_TIMER] = handle_preemption_timer,
8827 static const int kvm_vmx_max_exit_handlers =
8828 ARRAY_SIZE(kvm_vmx_exit_handlers);
8830 static bool nested_vmx_exit_handled_io(struct kvm_vcpu *vcpu,
8831 struct vmcs12 *vmcs12)
8833 unsigned long exit_qualification;
8834 gpa_t bitmap, last_bitmap;
8839 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_IO_BITMAPS))
8840 return nested_cpu_has(vmcs12, CPU_BASED_UNCOND_IO_EXITING);
8842 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
8844 port = exit_qualification >> 16;
8845 size = (exit_qualification & 7) + 1;
8847 last_bitmap = (gpa_t)-1;
8852 bitmap = vmcs12->io_bitmap_a;
8853 else if (port < 0x10000)
8854 bitmap = vmcs12->io_bitmap_b;
8857 bitmap += (port & 0x7fff) / 8;
8859 if (last_bitmap != bitmap)
8860 if (kvm_vcpu_read_guest(vcpu, bitmap, &b, 1))
8862 if (b & (1 << (port & 7)))
8867 last_bitmap = bitmap;
8874 * Return 1 if we should exit from L2 to L1 to handle an MSR access access,
8875 * rather than handle it ourselves in L0. I.e., check whether L1 expressed
8876 * disinterest in the current event (read or write a specific MSR) by using an
8877 * MSR bitmap. This may be the case even when L0 doesn't use MSR bitmaps.
8879 static bool nested_vmx_exit_handled_msr(struct kvm_vcpu *vcpu,
8880 struct vmcs12 *vmcs12, u32 exit_reason)
8882 u32 msr_index = vcpu->arch.regs[VCPU_REGS_RCX];
8885 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS))
8889 * The MSR_BITMAP page is divided into four 1024-byte bitmaps,
8890 * for the four combinations of read/write and low/high MSR numbers.
8891 * First we need to figure out which of the four to use:
8893 bitmap = vmcs12->msr_bitmap;
8894 if (exit_reason == EXIT_REASON_MSR_WRITE)
8896 if (msr_index >= 0xc0000000) {
8897 msr_index -= 0xc0000000;
8901 /* Then read the msr_index'th bit from this bitmap: */
8902 if (msr_index < 1024*8) {
8904 if (kvm_vcpu_read_guest(vcpu, bitmap + msr_index/8, &b, 1))
8906 return 1 & (b >> (msr_index & 7));
8908 return true; /* let L1 handle the wrong parameter */
8912 * Return 1 if we should exit from L2 to L1 to handle a CR access exit,
8913 * rather than handle it ourselves in L0. I.e., check if L1 wanted to
8914 * intercept (via guest_host_mask etc.) the current event.
8916 static bool nested_vmx_exit_handled_cr(struct kvm_vcpu *vcpu,
8917 struct vmcs12 *vmcs12)
8919 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
8920 int cr = exit_qualification & 15;
8924 switch ((exit_qualification >> 4) & 3) {
8925 case 0: /* mov to cr */
8926 reg = (exit_qualification >> 8) & 15;
8927 val = kvm_register_readl(vcpu, reg);
8930 if (vmcs12->cr0_guest_host_mask &
8931 (val ^ vmcs12->cr0_read_shadow))
8935 if ((vmcs12->cr3_target_count >= 1 &&
8936 vmcs12->cr3_target_value0 == val) ||
8937 (vmcs12->cr3_target_count >= 2 &&
8938 vmcs12->cr3_target_value1 == val) ||
8939 (vmcs12->cr3_target_count >= 3 &&
8940 vmcs12->cr3_target_value2 == val) ||
8941 (vmcs12->cr3_target_count >= 4 &&
8942 vmcs12->cr3_target_value3 == val))
8944 if (nested_cpu_has(vmcs12, CPU_BASED_CR3_LOAD_EXITING))
8948 if (vmcs12->cr4_guest_host_mask &
8949 (vmcs12->cr4_read_shadow ^ val))
8953 if (nested_cpu_has(vmcs12, CPU_BASED_CR8_LOAD_EXITING))
8959 if ((vmcs12->cr0_guest_host_mask & X86_CR0_TS) &&
8960 (vmcs12->cr0_read_shadow & X86_CR0_TS))
8963 case 1: /* mov from cr */
8966 if (vmcs12->cpu_based_vm_exec_control &
8967 CPU_BASED_CR3_STORE_EXITING)
8971 if (vmcs12->cpu_based_vm_exec_control &
8972 CPU_BASED_CR8_STORE_EXITING)
8979 * lmsw can change bits 1..3 of cr0, and only set bit 0 of
8980 * cr0. Other attempted changes are ignored, with no exit.
8982 val = (exit_qualification >> LMSW_SOURCE_DATA_SHIFT) & 0x0f;
8983 if (vmcs12->cr0_guest_host_mask & 0xe &
8984 (val ^ vmcs12->cr0_read_shadow))
8986 if ((vmcs12->cr0_guest_host_mask & 0x1) &&
8987 !(vmcs12->cr0_read_shadow & 0x1) &&
8996 * Return 1 if we should exit from L2 to L1 to handle an exit, or 0 if we
8997 * should handle it ourselves in L0 (and then continue L2). Only call this
8998 * when in is_guest_mode (L2).
9000 static bool nested_vmx_exit_reflected(struct kvm_vcpu *vcpu, u32 exit_reason)
9002 u32 intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
9003 struct vcpu_vmx *vmx = to_vmx(vcpu);
9004 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
9006 if (vmx->nested.nested_run_pending)
9009 if (unlikely(vmx->fail)) {
9010 pr_info_ratelimited("%s failed vm entry %x\n", __func__,
9011 vmcs_read32(VM_INSTRUCTION_ERROR));
9016 * The host physical addresses of some pages of guest memory
9017 * are loaded into the vmcs02 (e.g. vmcs12's Virtual APIC
9018 * Page). The CPU may write to these pages via their host
9019 * physical address while L2 is running, bypassing any
9020 * address-translation-based dirty tracking (e.g. EPT write
9023 * Mark them dirty on every exit from L2 to prevent them from
9024 * getting out of sync with dirty tracking.
9026 nested_mark_vmcs12_pages_dirty(vcpu);
9028 trace_kvm_nested_vmexit(kvm_rip_read(vcpu), exit_reason,
9029 vmcs_readl(EXIT_QUALIFICATION),
9030 vmx->idt_vectoring_info,
9032 vmcs_read32(VM_EXIT_INTR_ERROR_CODE),
9035 switch (exit_reason) {
9036 case EXIT_REASON_EXCEPTION_NMI:
9037 if (is_nmi(intr_info))
9039 else if (is_page_fault(intr_info))
9040 return !vmx->vcpu.arch.apf.host_apf_reason && enable_ept;
9041 else if (is_no_device(intr_info) &&
9042 !(vmcs12->guest_cr0 & X86_CR0_TS))
9044 else if (is_debug(intr_info) &&
9046 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))
9048 else if (is_breakpoint(intr_info) &&
9049 vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
9051 return vmcs12->exception_bitmap &
9052 (1u << (intr_info & INTR_INFO_VECTOR_MASK));
9053 case EXIT_REASON_EXTERNAL_INTERRUPT:
9055 case EXIT_REASON_TRIPLE_FAULT:
9057 case EXIT_REASON_PENDING_INTERRUPT:
9058 return nested_cpu_has(vmcs12, CPU_BASED_VIRTUAL_INTR_PENDING);
9059 case EXIT_REASON_NMI_WINDOW:
9060 return nested_cpu_has(vmcs12, CPU_BASED_VIRTUAL_NMI_PENDING);
9061 case EXIT_REASON_TASK_SWITCH:
9063 case EXIT_REASON_CPUID:
9065 case EXIT_REASON_HLT:
9066 return nested_cpu_has(vmcs12, CPU_BASED_HLT_EXITING);
9067 case EXIT_REASON_INVD:
9069 case EXIT_REASON_INVLPG:
9070 return nested_cpu_has(vmcs12, CPU_BASED_INVLPG_EXITING);
9071 case EXIT_REASON_RDPMC:
9072 return nested_cpu_has(vmcs12, CPU_BASED_RDPMC_EXITING);
9073 case EXIT_REASON_RDRAND:
9074 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_RDRAND_EXITING);
9075 case EXIT_REASON_RDSEED:
9076 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_RDSEED_EXITING);
9077 case EXIT_REASON_RDTSC: case EXIT_REASON_RDTSCP:
9078 return nested_cpu_has(vmcs12, CPU_BASED_RDTSC_EXITING);
9079 case EXIT_REASON_VMCALL: case EXIT_REASON_VMCLEAR:
9080 case EXIT_REASON_VMLAUNCH: case EXIT_REASON_VMPTRLD:
9081 case EXIT_REASON_VMPTRST: case EXIT_REASON_VMREAD:
9082 case EXIT_REASON_VMRESUME: case EXIT_REASON_VMWRITE:
9083 case EXIT_REASON_VMOFF: case EXIT_REASON_VMON:
9084 case EXIT_REASON_INVEPT: case EXIT_REASON_INVVPID:
9086 * VMX instructions trap unconditionally. This allows L1 to
9087 * emulate them for its L2 guest, i.e., allows 3-level nesting!
9090 case EXIT_REASON_CR_ACCESS:
9091 return nested_vmx_exit_handled_cr(vcpu, vmcs12);
9092 case EXIT_REASON_DR_ACCESS:
9093 return nested_cpu_has(vmcs12, CPU_BASED_MOV_DR_EXITING);
9094 case EXIT_REASON_IO_INSTRUCTION:
9095 return nested_vmx_exit_handled_io(vcpu, vmcs12);
9096 case EXIT_REASON_GDTR_IDTR: case EXIT_REASON_LDTR_TR:
9097 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_DESC);
9098 case EXIT_REASON_MSR_READ:
9099 case EXIT_REASON_MSR_WRITE:
9100 return nested_vmx_exit_handled_msr(vcpu, vmcs12, exit_reason);
9101 case EXIT_REASON_INVALID_STATE:
9103 case EXIT_REASON_MWAIT_INSTRUCTION:
9104 return nested_cpu_has(vmcs12, CPU_BASED_MWAIT_EXITING);
9105 case EXIT_REASON_MONITOR_TRAP_FLAG:
9106 return nested_cpu_has(vmcs12, CPU_BASED_MONITOR_TRAP_FLAG);
9107 case EXIT_REASON_MONITOR_INSTRUCTION:
9108 return nested_cpu_has(vmcs12, CPU_BASED_MONITOR_EXITING);
9109 case EXIT_REASON_PAUSE_INSTRUCTION:
9110 return nested_cpu_has(vmcs12, CPU_BASED_PAUSE_EXITING) ||
9111 nested_cpu_has2(vmcs12,
9112 SECONDARY_EXEC_PAUSE_LOOP_EXITING);
9113 case EXIT_REASON_MCE_DURING_VMENTRY:
9115 case EXIT_REASON_TPR_BELOW_THRESHOLD:
9116 return nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW);
9117 case EXIT_REASON_APIC_ACCESS:
9118 case EXIT_REASON_APIC_WRITE:
9119 case EXIT_REASON_EOI_INDUCED:
9121 * The controls for "virtualize APIC accesses," "APIC-
9122 * register virtualization," and "virtual-interrupt
9123 * delivery" only come from vmcs12.
9126 case EXIT_REASON_EPT_VIOLATION:
9128 * L0 always deals with the EPT violation. If nested EPT is
9129 * used, and the nested mmu code discovers that the address is
9130 * missing in the guest EPT table (EPT12), the EPT violation
9131 * will be injected with nested_ept_inject_page_fault()
9134 case EXIT_REASON_EPT_MISCONFIG:
9136 * L2 never uses directly L1's EPT, but rather L0's own EPT
9137 * table (shadow on EPT) or a merged EPT table that L0 built
9138 * (EPT on EPT). So any problems with the structure of the
9139 * table is L0's fault.
9142 case EXIT_REASON_INVPCID:
9144 nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_INVPCID) &&
9145 nested_cpu_has(vmcs12, CPU_BASED_INVLPG_EXITING);
9146 case EXIT_REASON_WBINVD:
9147 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_WBINVD_EXITING);
9148 case EXIT_REASON_XSETBV:
9150 case EXIT_REASON_XSAVES: case EXIT_REASON_XRSTORS:
9152 * This should never happen, since it is not possible to
9153 * set XSS to a non-zero value---neither in L1 nor in L2.
9154 * If if it were, XSS would have to be checked against
9155 * the XSS exit bitmap in vmcs12.
9157 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_XSAVES);
9158 case EXIT_REASON_PREEMPTION_TIMER:
9160 case EXIT_REASON_PML_FULL:
9161 /* We emulate PML support to L1. */
9163 case EXIT_REASON_VMFUNC:
9164 /* VM functions are emulated through L2->L0 vmexits. */
9171 static int nested_vmx_reflect_vmexit(struct kvm_vcpu *vcpu, u32 exit_reason)
9173 u32 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
9176 * At this point, the exit interruption info in exit_intr_info
9177 * is only valid for EXCEPTION_NMI exits. For EXTERNAL_INTERRUPT
9178 * we need to query the in-kernel LAPIC.
9180 WARN_ON(exit_reason == EXIT_REASON_EXTERNAL_INTERRUPT);
9181 if ((exit_intr_info &
9182 (INTR_INFO_VALID_MASK | INTR_INFO_DELIVER_CODE_MASK)) ==
9183 (INTR_INFO_VALID_MASK | INTR_INFO_DELIVER_CODE_MASK)) {
9184 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
9185 vmcs12->vm_exit_intr_error_code =
9186 vmcs_read32(VM_EXIT_INTR_ERROR_CODE);
9189 nested_vmx_vmexit(vcpu, exit_reason, exit_intr_info,
9190 vmcs_readl(EXIT_QUALIFICATION));
9194 static void vmx_get_exit_info(struct kvm_vcpu *vcpu, u64 *info1, u64 *info2)
9196 *info1 = vmcs_readl(EXIT_QUALIFICATION);
9197 *info2 = vmcs_read32(VM_EXIT_INTR_INFO);
9200 static void vmx_destroy_pml_buffer(struct vcpu_vmx *vmx)
9203 __free_page(vmx->pml_pg);
9208 static void vmx_flush_pml_buffer(struct kvm_vcpu *vcpu)
9210 struct vcpu_vmx *vmx = to_vmx(vcpu);
9214 pml_idx = vmcs_read16(GUEST_PML_INDEX);
9216 /* Do nothing if PML buffer is empty */
9217 if (pml_idx == (PML_ENTITY_NUM - 1))
9220 /* PML index always points to next available PML buffer entity */
9221 if (pml_idx >= PML_ENTITY_NUM)
9226 pml_buf = page_address(vmx->pml_pg);
9227 for (; pml_idx < PML_ENTITY_NUM; pml_idx++) {
9230 gpa = pml_buf[pml_idx];
9231 WARN_ON(gpa & (PAGE_SIZE - 1));
9232 kvm_vcpu_mark_page_dirty(vcpu, gpa >> PAGE_SHIFT);
9235 /* reset PML index */
9236 vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1);
9240 * Flush all vcpus' PML buffer and update logged GPAs to dirty_bitmap.
9241 * Called before reporting dirty_bitmap to userspace.
9243 static void kvm_flush_pml_buffers(struct kvm *kvm)
9246 struct kvm_vcpu *vcpu;
9248 * We only need to kick vcpu out of guest mode here, as PML buffer
9249 * is flushed at beginning of all VMEXITs, and it's obvious that only
9250 * vcpus running in guest are possible to have unflushed GPAs in PML
9253 kvm_for_each_vcpu(i, vcpu, kvm)
9254 kvm_vcpu_kick(vcpu);
9257 static void vmx_dump_sel(char *name, uint32_t sel)
9259 pr_err("%s sel=0x%04x, attr=0x%05x, limit=0x%08x, base=0x%016lx\n",
9260 name, vmcs_read16(sel),
9261 vmcs_read32(sel + GUEST_ES_AR_BYTES - GUEST_ES_SELECTOR),
9262 vmcs_read32(sel + GUEST_ES_LIMIT - GUEST_ES_SELECTOR),
9263 vmcs_readl(sel + GUEST_ES_BASE - GUEST_ES_SELECTOR));
9266 static void vmx_dump_dtsel(char *name, uint32_t limit)
9268 pr_err("%s limit=0x%08x, base=0x%016lx\n",
9269 name, vmcs_read32(limit),
9270 vmcs_readl(limit + GUEST_GDTR_BASE - GUEST_GDTR_LIMIT));
9273 static void dump_vmcs(void)
9275 u32 vmentry_ctl = vmcs_read32(VM_ENTRY_CONTROLS);
9276 u32 vmexit_ctl = vmcs_read32(VM_EXIT_CONTROLS);
9277 u32 cpu_based_exec_ctrl = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
9278 u32 pin_based_exec_ctrl = vmcs_read32(PIN_BASED_VM_EXEC_CONTROL);
9279 u32 secondary_exec_control = 0;
9280 unsigned long cr4 = vmcs_readl(GUEST_CR4);
9281 u64 efer = vmcs_read64(GUEST_IA32_EFER);
9284 if (cpu_has_secondary_exec_ctrls())
9285 secondary_exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
9287 pr_err("*** Guest State ***\n");
9288 pr_err("CR0: actual=0x%016lx, shadow=0x%016lx, gh_mask=%016lx\n",
9289 vmcs_readl(GUEST_CR0), vmcs_readl(CR0_READ_SHADOW),
9290 vmcs_readl(CR0_GUEST_HOST_MASK));
9291 pr_err("CR4: actual=0x%016lx, shadow=0x%016lx, gh_mask=%016lx\n",
9292 cr4, vmcs_readl(CR4_READ_SHADOW), vmcs_readl(CR4_GUEST_HOST_MASK));
9293 pr_err("CR3 = 0x%016lx\n", vmcs_readl(GUEST_CR3));
9294 if ((secondary_exec_control & SECONDARY_EXEC_ENABLE_EPT) &&
9295 (cr4 & X86_CR4_PAE) && !(efer & EFER_LMA))
9297 pr_err("PDPTR0 = 0x%016llx PDPTR1 = 0x%016llx\n",
9298 vmcs_read64(GUEST_PDPTR0), vmcs_read64(GUEST_PDPTR1));
9299 pr_err("PDPTR2 = 0x%016llx PDPTR3 = 0x%016llx\n",
9300 vmcs_read64(GUEST_PDPTR2), vmcs_read64(GUEST_PDPTR3));
9302 pr_err("RSP = 0x%016lx RIP = 0x%016lx\n",
9303 vmcs_readl(GUEST_RSP), vmcs_readl(GUEST_RIP));
9304 pr_err("RFLAGS=0x%08lx DR7 = 0x%016lx\n",
9305 vmcs_readl(GUEST_RFLAGS), vmcs_readl(GUEST_DR7));
9306 pr_err("Sysenter RSP=%016lx CS:RIP=%04x:%016lx\n",
9307 vmcs_readl(GUEST_SYSENTER_ESP),
9308 vmcs_read32(GUEST_SYSENTER_CS), vmcs_readl(GUEST_SYSENTER_EIP));
9309 vmx_dump_sel("CS: ", GUEST_CS_SELECTOR);
9310 vmx_dump_sel("DS: ", GUEST_DS_SELECTOR);
9311 vmx_dump_sel("SS: ", GUEST_SS_SELECTOR);
9312 vmx_dump_sel("ES: ", GUEST_ES_SELECTOR);
9313 vmx_dump_sel("FS: ", GUEST_FS_SELECTOR);
9314 vmx_dump_sel("GS: ", GUEST_GS_SELECTOR);
9315 vmx_dump_dtsel("GDTR:", GUEST_GDTR_LIMIT);
9316 vmx_dump_sel("LDTR:", GUEST_LDTR_SELECTOR);
9317 vmx_dump_dtsel("IDTR:", GUEST_IDTR_LIMIT);
9318 vmx_dump_sel("TR: ", GUEST_TR_SELECTOR);
9319 if ((vmexit_ctl & (VM_EXIT_SAVE_IA32_PAT | VM_EXIT_SAVE_IA32_EFER)) ||
9320 (vmentry_ctl & (VM_ENTRY_LOAD_IA32_PAT | VM_ENTRY_LOAD_IA32_EFER)))
9321 pr_err("EFER = 0x%016llx PAT = 0x%016llx\n",
9322 efer, vmcs_read64(GUEST_IA32_PAT));
9323 pr_err("DebugCtl = 0x%016llx DebugExceptions = 0x%016lx\n",
9324 vmcs_read64(GUEST_IA32_DEBUGCTL),
9325 vmcs_readl(GUEST_PENDING_DBG_EXCEPTIONS));
9326 if (cpu_has_load_perf_global_ctrl &&
9327 vmentry_ctl & VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL)
9328 pr_err("PerfGlobCtl = 0x%016llx\n",
9329 vmcs_read64(GUEST_IA32_PERF_GLOBAL_CTRL));
9330 if (vmentry_ctl & VM_ENTRY_LOAD_BNDCFGS)
9331 pr_err("BndCfgS = 0x%016llx\n", vmcs_read64(GUEST_BNDCFGS));
9332 pr_err("Interruptibility = %08x ActivityState = %08x\n",
9333 vmcs_read32(GUEST_INTERRUPTIBILITY_INFO),
9334 vmcs_read32(GUEST_ACTIVITY_STATE));
9335 if (secondary_exec_control & SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY)
9336 pr_err("InterruptStatus = %04x\n",
9337 vmcs_read16(GUEST_INTR_STATUS));
9339 pr_err("*** Host State ***\n");
9340 pr_err("RIP = 0x%016lx RSP = 0x%016lx\n",
9341 vmcs_readl(HOST_RIP), vmcs_readl(HOST_RSP));
9342 pr_err("CS=%04x SS=%04x DS=%04x ES=%04x FS=%04x GS=%04x TR=%04x\n",
9343 vmcs_read16(HOST_CS_SELECTOR), vmcs_read16(HOST_SS_SELECTOR),
9344 vmcs_read16(HOST_DS_SELECTOR), vmcs_read16(HOST_ES_SELECTOR),
9345 vmcs_read16(HOST_FS_SELECTOR), vmcs_read16(HOST_GS_SELECTOR),
9346 vmcs_read16(HOST_TR_SELECTOR));
9347 pr_err("FSBase=%016lx GSBase=%016lx TRBase=%016lx\n",
9348 vmcs_readl(HOST_FS_BASE), vmcs_readl(HOST_GS_BASE),
9349 vmcs_readl(HOST_TR_BASE));
9350 pr_err("GDTBase=%016lx IDTBase=%016lx\n",
9351 vmcs_readl(HOST_GDTR_BASE), vmcs_readl(HOST_IDTR_BASE));
9352 pr_err("CR0=%016lx CR3=%016lx CR4=%016lx\n",
9353 vmcs_readl(HOST_CR0), vmcs_readl(HOST_CR3),
9354 vmcs_readl(HOST_CR4));
9355 pr_err("Sysenter RSP=%016lx CS:RIP=%04x:%016lx\n",
9356 vmcs_readl(HOST_IA32_SYSENTER_ESP),
9357 vmcs_read32(HOST_IA32_SYSENTER_CS),
9358 vmcs_readl(HOST_IA32_SYSENTER_EIP));
9359 if (vmexit_ctl & (VM_EXIT_LOAD_IA32_PAT | VM_EXIT_LOAD_IA32_EFER))
9360 pr_err("EFER = 0x%016llx PAT = 0x%016llx\n",
9361 vmcs_read64(HOST_IA32_EFER),
9362 vmcs_read64(HOST_IA32_PAT));
9363 if (cpu_has_load_perf_global_ctrl &&
9364 vmexit_ctl & VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL)
9365 pr_err("PerfGlobCtl = 0x%016llx\n",
9366 vmcs_read64(HOST_IA32_PERF_GLOBAL_CTRL));
9368 pr_err("*** Control State ***\n");
9369 pr_err("PinBased=%08x CPUBased=%08x SecondaryExec=%08x\n",
9370 pin_based_exec_ctrl, cpu_based_exec_ctrl, secondary_exec_control);
9371 pr_err("EntryControls=%08x ExitControls=%08x\n", vmentry_ctl, vmexit_ctl);
9372 pr_err("ExceptionBitmap=%08x PFECmask=%08x PFECmatch=%08x\n",
9373 vmcs_read32(EXCEPTION_BITMAP),
9374 vmcs_read32(PAGE_FAULT_ERROR_CODE_MASK),
9375 vmcs_read32(PAGE_FAULT_ERROR_CODE_MATCH));
9376 pr_err("VMEntry: intr_info=%08x errcode=%08x ilen=%08x\n",
9377 vmcs_read32(VM_ENTRY_INTR_INFO_FIELD),
9378 vmcs_read32(VM_ENTRY_EXCEPTION_ERROR_CODE),
9379 vmcs_read32(VM_ENTRY_INSTRUCTION_LEN));
9380 pr_err("VMExit: intr_info=%08x errcode=%08x ilen=%08x\n",
9381 vmcs_read32(VM_EXIT_INTR_INFO),
9382 vmcs_read32(VM_EXIT_INTR_ERROR_CODE),
9383 vmcs_read32(VM_EXIT_INSTRUCTION_LEN));
9384 pr_err(" reason=%08x qualification=%016lx\n",
9385 vmcs_read32(VM_EXIT_REASON), vmcs_readl(EXIT_QUALIFICATION));
9386 pr_err("IDTVectoring: info=%08x errcode=%08x\n",
9387 vmcs_read32(IDT_VECTORING_INFO_FIELD),
9388 vmcs_read32(IDT_VECTORING_ERROR_CODE));
9389 pr_err("TSC Offset = 0x%016llx\n", vmcs_read64(TSC_OFFSET));
9390 if (secondary_exec_control & SECONDARY_EXEC_TSC_SCALING)
9391 pr_err("TSC Multiplier = 0x%016llx\n",
9392 vmcs_read64(TSC_MULTIPLIER));
9393 if (cpu_based_exec_ctrl & CPU_BASED_TPR_SHADOW)
9394 pr_err("TPR Threshold = 0x%02x\n", vmcs_read32(TPR_THRESHOLD));
9395 if (pin_based_exec_ctrl & PIN_BASED_POSTED_INTR)
9396 pr_err("PostedIntrVec = 0x%02x\n", vmcs_read16(POSTED_INTR_NV));
9397 if ((secondary_exec_control & SECONDARY_EXEC_ENABLE_EPT))
9398 pr_err("EPT pointer = 0x%016llx\n", vmcs_read64(EPT_POINTER));
9399 n = vmcs_read32(CR3_TARGET_COUNT);
9400 for (i = 0; i + 1 < n; i += 4)
9401 pr_err("CR3 target%u=%016lx target%u=%016lx\n",
9402 i, vmcs_readl(CR3_TARGET_VALUE0 + i * 2),
9403 i + 1, vmcs_readl(CR3_TARGET_VALUE0 + i * 2 + 2));
9405 pr_err("CR3 target%u=%016lx\n",
9406 i, vmcs_readl(CR3_TARGET_VALUE0 + i * 2));
9407 if (secondary_exec_control & SECONDARY_EXEC_PAUSE_LOOP_EXITING)
9408 pr_err("PLE Gap=%08x Window=%08x\n",
9409 vmcs_read32(PLE_GAP), vmcs_read32(PLE_WINDOW));
9410 if (secondary_exec_control & SECONDARY_EXEC_ENABLE_VPID)
9411 pr_err("Virtual processor ID = 0x%04x\n",
9412 vmcs_read16(VIRTUAL_PROCESSOR_ID));
9416 * The guest has exited. See if we can fix it or if we need userspace
9419 static int vmx_handle_exit(struct kvm_vcpu *vcpu)
9421 struct vcpu_vmx *vmx = to_vmx(vcpu);
9422 u32 exit_reason = vmx->exit_reason;
9423 u32 vectoring_info = vmx->idt_vectoring_info;
9425 trace_kvm_exit(exit_reason, vcpu, KVM_ISA_VMX);
9428 * Flush logged GPAs PML buffer, this will make dirty_bitmap more
9429 * updated. Another good is, in kvm_vm_ioctl_get_dirty_log, before
9430 * querying dirty_bitmap, we only need to kick all vcpus out of guest
9431 * mode as if vcpus is in root mode, the PML buffer must has been
9435 vmx_flush_pml_buffer(vcpu);
9437 /* If guest state is invalid, start emulating */
9438 if (vmx->emulation_required)
9439 return handle_invalid_guest_state(vcpu);
9441 if (is_guest_mode(vcpu) && nested_vmx_exit_reflected(vcpu, exit_reason))
9442 return nested_vmx_reflect_vmexit(vcpu, exit_reason);
9444 if (exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY) {
9446 vcpu->run->exit_reason = KVM_EXIT_FAIL_ENTRY;
9447 vcpu->run->fail_entry.hardware_entry_failure_reason
9452 if (unlikely(vmx->fail)) {
9453 vcpu->run->exit_reason = KVM_EXIT_FAIL_ENTRY;
9454 vcpu->run->fail_entry.hardware_entry_failure_reason
9455 = vmcs_read32(VM_INSTRUCTION_ERROR);
9461 * Do not try to fix EXIT_REASON_EPT_MISCONFIG if it caused by
9462 * delivery event since it indicates guest is accessing MMIO.
9463 * The vm-exit can be triggered again after return to guest that
9464 * will cause infinite loop.
9466 if ((vectoring_info & VECTORING_INFO_VALID_MASK) &&
9467 (exit_reason != EXIT_REASON_EXCEPTION_NMI &&
9468 exit_reason != EXIT_REASON_EPT_VIOLATION &&
9469 exit_reason != EXIT_REASON_PML_FULL &&
9470 exit_reason != EXIT_REASON_TASK_SWITCH)) {
9471 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
9472 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_DELIVERY_EV;
9473 vcpu->run->internal.ndata = 3;
9474 vcpu->run->internal.data[0] = vectoring_info;
9475 vcpu->run->internal.data[1] = exit_reason;
9476 vcpu->run->internal.data[2] = vcpu->arch.exit_qualification;
9477 if (exit_reason == EXIT_REASON_EPT_MISCONFIG) {
9478 vcpu->run->internal.ndata++;
9479 vcpu->run->internal.data[3] =
9480 vmcs_read64(GUEST_PHYSICAL_ADDRESS);
9485 if (unlikely(!enable_vnmi &&
9486 vmx->loaded_vmcs->soft_vnmi_blocked)) {
9487 if (vmx_interrupt_allowed(vcpu)) {
9488 vmx->loaded_vmcs->soft_vnmi_blocked = 0;
9489 } else if (vmx->loaded_vmcs->vnmi_blocked_time > 1000000000LL &&
9490 vcpu->arch.nmi_pending) {
9492 * This CPU don't support us in finding the end of an
9493 * NMI-blocked window if the guest runs with IRQs
9494 * disabled. So we pull the trigger after 1 s of
9495 * futile waiting, but inform the user about this.
9497 printk(KERN_WARNING "%s: Breaking out of NMI-blocked "
9498 "state on VCPU %d after 1 s timeout\n",
9499 __func__, vcpu->vcpu_id);
9500 vmx->loaded_vmcs->soft_vnmi_blocked = 0;
9504 if (exit_reason < kvm_vmx_max_exit_handlers
9505 && kvm_vmx_exit_handlers[exit_reason])
9506 return kvm_vmx_exit_handlers[exit_reason](vcpu);
9508 vcpu_unimpl(vcpu, "vmx: unexpected exit reason 0x%x\n",
9510 kvm_queue_exception(vcpu, UD_VECTOR);
9515 static void update_cr8_intercept(struct kvm_vcpu *vcpu, int tpr, int irr)
9517 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
9519 if (is_guest_mode(vcpu) &&
9520 nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW))
9523 if (irr == -1 || tpr < irr) {
9524 vmcs_write32(TPR_THRESHOLD, 0);
9528 vmcs_write32(TPR_THRESHOLD, irr);
9531 static void vmx_set_virtual_apic_mode(struct kvm_vcpu *vcpu)
9533 u32 sec_exec_control;
9535 if (!lapic_in_kernel(vcpu))
9538 /* Postpone execution until vmcs01 is the current VMCS. */
9539 if (is_guest_mode(vcpu)) {
9540 to_vmx(vcpu)->nested.change_vmcs01_virtual_apic_mode = true;
9544 if (!cpu_need_tpr_shadow(vcpu))
9547 sec_exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
9548 sec_exec_control &= ~(SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
9549 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE);
9551 switch (kvm_get_apic_mode(vcpu)) {
9552 case LAPIC_MODE_INVALID:
9553 WARN_ONCE(true, "Invalid local APIC state");
9554 case LAPIC_MODE_DISABLED:
9556 case LAPIC_MODE_XAPIC:
9557 if (flexpriority_enabled) {
9559 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
9560 vmx_flush_tlb(vcpu, true);
9563 case LAPIC_MODE_X2APIC:
9564 if (cpu_has_vmx_virtualize_x2apic_mode())
9566 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
9569 vmcs_write32(SECONDARY_VM_EXEC_CONTROL, sec_exec_control);
9571 vmx_update_msr_bitmap(vcpu);
9574 static void vmx_set_apic_access_page_addr(struct kvm_vcpu *vcpu, hpa_t hpa)
9576 if (!is_guest_mode(vcpu)) {
9577 vmcs_write64(APIC_ACCESS_ADDR, hpa);
9578 vmx_flush_tlb(vcpu, true);
9582 static void vmx_hwapic_isr_update(struct kvm_vcpu *vcpu, int max_isr)
9590 status = vmcs_read16(GUEST_INTR_STATUS);
9592 if (max_isr != old) {
9594 status |= max_isr << 8;
9595 vmcs_write16(GUEST_INTR_STATUS, status);
9599 static void vmx_set_rvi(int vector)
9607 status = vmcs_read16(GUEST_INTR_STATUS);
9608 old = (u8)status & 0xff;
9609 if ((u8)vector != old) {
9611 status |= (u8)vector;
9612 vmcs_write16(GUEST_INTR_STATUS, status);
9616 static void vmx_hwapic_irr_update(struct kvm_vcpu *vcpu, int max_irr)
9619 * When running L2, updating RVI is only relevant when
9620 * vmcs12 virtual-interrupt-delivery enabled.
9621 * However, it can be enabled only when L1 also
9622 * intercepts external-interrupts and in that case
9623 * we should not update vmcs02 RVI but instead intercept
9624 * interrupt. Therefore, do nothing when running L2.
9626 if (!is_guest_mode(vcpu))
9627 vmx_set_rvi(max_irr);
9630 static int vmx_sync_pir_to_irr(struct kvm_vcpu *vcpu)
9632 struct vcpu_vmx *vmx = to_vmx(vcpu);
9634 bool max_irr_updated;
9636 WARN_ON(!vcpu->arch.apicv_active);
9637 if (pi_test_on(&vmx->pi_desc)) {
9638 pi_clear_on(&vmx->pi_desc);
9640 * IOMMU can write to PIR.ON, so the barrier matters even on UP.
9641 * But on x86 this is just a compiler barrier anyway.
9643 smp_mb__after_atomic();
9645 kvm_apic_update_irr(vcpu, vmx->pi_desc.pir, &max_irr);
9648 * If we are running L2 and L1 has a new pending interrupt
9649 * which can be injected, we should re-evaluate
9650 * what should be done with this new L1 interrupt.
9651 * If L1 intercepts external-interrupts, we should
9652 * exit from L2 to L1. Otherwise, interrupt should be
9653 * delivered directly to L2.
9655 if (is_guest_mode(vcpu) && max_irr_updated) {
9656 if (nested_exit_on_intr(vcpu))
9657 kvm_vcpu_exiting_guest_mode(vcpu);
9659 kvm_make_request(KVM_REQ_EVENT, vcpu);
9662 max_irr = kvm_lapic_find_highest_irr(vcpu);
9664 vmx_hwapic_irr_update(vcpu, max_irr);
9668 static void vmx_load_eoi_exitmap(struct kvm_vcpu *vcpu, u64 *eoi_exit_bitmap)
9670 if (!kvm_vcpu_apicv_active(vcpu))
9673 vmcs_write64(EOI_EXIT_BITMAP0, eoi_exit_bitmap[0]);
9674 vmcs_write64(EOI_EXIT_BITMAP1, eoi_exit_bitmap[1]);
9675 vmcs_write64(EOI_EXIT_BITMAP2, eoi_exit_bitmap[2]);
9676 vmcs_write64(EOI_EXIT_BITMAP3, eoi_exit_bitmap[3]);
9679 static void vmx_apicv_post_state_restore(struct kvm_vcpu *vcpu)
9681 struct vcpu_vmx *vmx = to_vmx(vcpu);
9683 pi_clear_on(&vmx->pi_desc);
9684 memset(vmx->pi_desc.pir, 0, sizeof(vmx->pi_desc.pir));
9687 static void vmx_complete_atomic_exit(struct vcpu_vmx *vmx)
9689 u32 exit_intr_info = 0;
9690 u16 basic_exit_reason = (u16)vmx->exit_reason;
9692 if (!(basic_exit_reason == EXIT_REASON_MCE_DURING_VMENTRY
9693 || basic_exit_reason == EXIT_REASON_EXCEPTION_NMI))
9696 if (!(vmx->exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY))
9697 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
9698 vmx->exit_intr_info = exit_intr_info;
9700 /* if exit due to PF check for async PF */
9701 if (is_page_fault(exit_intr_info))
9702 vmx->vcpu.arch.apf.host_apf_reason = kvm_read_and_reset_pf_reason();
9704 /* Handle machine checks before interrupts are enabled */
9705 if (basic_exit_reason == EXIT_REASON_MCE_DURING_VMENTRY ||
9706 is_machine_check(exit_intr_info))
9707 kvm_machine_check();
9709 /* We need to handle NMIs before interrupts are enabled */
9710 if (is_nmi(exit_intr_info)) {
9711 kvm_before_interrupt(&vmx->vcpu);
9713 kvm_after_interrupt(&vmx->vcpu);
9717 static void vmx_handle_external_intr(struct kvm_vcpu *vcpu)
9719 u32 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
9721 if ((exit_intr_info & (INTR_INFO_VALID_MASK | INTR_INFO_INTR_TYPE_MASK))
9722 == (INTR_INFO_VALID_MASK | INTR_TYPE_EXT_INTR)) {
9723 unsigned int vector;
9724 unsigned long entry;
9726 struct vcpu_vmx *vmx = to_vmx(vcpu);
9727 #ifdef CONFIG_X86_64
9731 vector = exit_intr_info & INTR_INFO_VECTOR_MASK;
9732 desc = (gate_desc *)vmx->host_idt_base + vector;
9733 entry = gate_offset(desc);
9735 #ifdef CONFIG_X86_64
9736 "mov %%" _ASM_SP ", %[sp]\n\t"
9737 "and $0xfffffffffffffff0, %%" _ASM_SP "\n\t"
9742 __ASM_SIZE(push) " $%c[cs]\n\t"
9745 #ifdef CONFIG_X86_64
9750 THUNK_TARGET(entry),
9751 [ss]"i"(__KERNEL_DS),
9752 [cs]"i"(__KERNEL_CS)
9756 STACK_FRAME_NON_STANDARD(vmx_handle_external_intr);
9758 static bool vmx_has_emulated_msr(int index)
9761 case MSR_IA32_SMBASE:
9763 * We cannot do SMM unless we can run the guest in big
9766 return enable_unrestricted_guest || emulate_invalid_guest_state;
9767 case MSR_AMD64_VIRT_SPEC_CTRL:
9768 /* This is AMD only. */
9775 static bool vmx_mpx_supported(void)
9777 return (vmcs_config.vmexit_ctrl & VM_EXIT_CLEAR_BNDCFGS) &&
9778 (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_BNDCFGS);
9781 static bool vmx_xsaves_supported(void)
9783 return vmcs_config.cpu_based_2nd_exec_ctrl &
9784 SECONDARY_EXEC_XSAVES;
9787 static void vmx_recover_nmi_blocking(struct vcpu_vmx *vmx)
9792 bool idtv_info_valid;
9794 idtv_info_valid = vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK;
9797 if (vmx->loaded_vmcs->nmi_known_unmasked)
9800 * Can't use vmx->exit_intr_info since we're not sure what
9801 * the exit reason is.
9803 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
9804 unblock_nmi = (exit_intr_info & INTR_INFO_UNBLOCK_NMI) != 0;
9805 vector = exit_intr_info & INTR_INFO_VECTOR_MASK;
9807 * SDM 3: 27.7.1.2 (September 2008)
9808 * Re-set bit "block by NMI" before VM entry if vmexit caused by
9809 * a guest IRET fault.
9810 * SDM 3: 23.2.2 (September 2008)
9811 * Bit 12 is undefined in any of the following cases:
9812 * If the VM exit sets the valid bit in the IDT-vectoring
9813 * information field.
9814 * If the VM exit is due to a double fault.
9816 if ((exit_intr_info & INTR_INFO_VALID_MASK) && unblock_nmi &&
9817 vector != DF_VECTOR && !idtv_info_valid)
9818 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
9819 GUEST_INTR_STATE_NMI);
9821 vmx->loaded_vmcs->nmi_known_unmasked =
9822 !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO)
9823 & GUEST_INTR_STATE_NMI);
9824 } else if (unlikely(vmx->loaded_vmcs->soft_vnmi_blocked))
9825 vmx->loaded_vmcs->vnmi_blocked_time +=
9826 ktime_to_ns(ktime_sub(ktime_get(),
9827 vmx->loaded_vmcs->entry_time));
9830 static void __vmx_complete_interrupts(struct kvm_vcpu *vcpu,
9831 u32 idt_vectoring_info,
9832 int instr_len_field,
9833 int error_code_field)
9837 bool idtv_info_valid;
9839 idtv_info_valid = idt_vectoring_info & VECTORING_INFO_VALID_MASK;
9841 vcpu->arch.nmi_injected = false;
9842 kvm_clear_exception_queue(vcpu);
9843 kvm_clear_interrupt_queue(vcpu);
9845 if (!idtv_info_valid)
9848 kvm_make_request(KVM_REQ_EVENT, vcpu);
9850 vector = idt_vectoring_info & VECTORING_INFO_VECTOR_MASK;
9851 type = idt_vectoring_info & VECTORING_INFO_TYPE_MASK;
9854 case INTR_TYPE_NMI_INTR:
9855 vcpu->arch.nmi_injected = true;
9857 * SDM 3: 27.7.1.2 (September 2008)
9858 * Clear bit "block by NMI" before VM entry if a NMI
9861 vmx_set_nmi_mask(vcpu, false);
9863 case INTR_TYPE_SOFT_EXCEPTION:
9864 vcpu->arch.event_exit_inst_len = vmcs_read32(instr_len_field);
9866 case INTR_TYPE_HARD_EXCEPTION:
9867 if (idt_vectoring_info & VECTORING_INFO_DELIVER_CODE_MASK) {
9868 u32 err = vmcs_read32(error_code_field);
9869 kvm_requeue_exception_e(vcpu, vector, err);
9871 kvm_requeue_exception(vcpu, vector);
9873 case INTR_TYPE_SOFT_INTR:
9874 vcpu->arch.event_exit_inst_len = vmcs_read32(instr_len_field);
9876 case INTR_TYPE_EXT_INTR:
9877 kvm_queue_interrupt(vcpu, vector, type == INTR_TYPE_SOFT_INTR);
9884 static void vmx_complete_interrupts(struct vcpu_vmx *vmx)
9886 __vmx_complete_interrupts(&vmx->vcpu, vmx->idt_vectoring_info,
9887 VM_EXIT_INSTRUCTION_LEN,
9888 IDT_VECTORING_ERROR_CODE);
9891 static void vmx_cancel_injection(struct kvm_vcpu *vcpu)
9893 __vmx_complete_interrupts(vcpu,
9894 vmcs_read32(VM_ENTRY_INTR_INFO_FIELD),
9895 VM_ENTRY_INSTRUCTION_LEN,
9896 VM_ENTRY_EXCEPTION_ERROR_CODE);
9898 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0);
9901 static void atomic_switch_perf_msrs(struct vcpu_vmx *vmx)
9904 struct perf_guest_switch_msr *msrs;
9906 msrs = perf_guest_get_msrs(&nr_msrs);
9911 for (i = 0; i < nr_msrs; i++)
9912 if (msrs[i].host == msrs[i].guest)
9913 clear_atomic_switch_msr(vmx, msrs[i].msr);
9915 add_atomic_switch_msr(vmx, msrs[i].msr, msrs[i].guest,
9919 static void vmx_arm_hv_timer(struct kvm_vcpu *vcpu)
9921 struct vcpu_vmx *vmx = to_vmx(vcpu);
9925 if (vmx->hv_deadline_tsc == -1)
9929 if (vmx->hv_deadline_tsc > tscl)
9930 /* sure to be 32 bit only because checked on set_hv_timer */
9931 delta_tsc = (u32)((vmx->hv_deadline_tsc - tscl) >>
9932 cpu_preemption_timer_multi);
9936 vmcs_write32(VMX_PREEMPTION_TIMER_VALUE, delta_tsc);
9939 static void __noclone vmx_vcpu_run(struct kvm_vcpu *vcpu)
9941 struct vcpu_vmx *vmx = to_vmx(vcpu);
9942 unsigned long cr3, cr4, evmcs_rsp;
9944 /* Record the guest's net vcpu time for enforced NMI injections. */
9945 if (unlikely(!enable_vnmi &&
9946 vmx->loaded_vmcs->soft_vnmi_blocked))
9947 vmx->loaded_vmcs->entry_time = ktime_get();
9949 /* Don't enter VMX if guest state is invalid, let the exit handler
9950 start emulation until we arrive back to a valid state */
9951 if (vmx->emulation_required)
9954 if (vmx->ple_window_dirty) {
9955 vmx->ple_window_dirty = false;
9956 vmcs_write32(PLE_WINDOW, vmx->ple_window);
9959 if (vmx->nested.sync_shadow_vmcs) {
9960 copy_vmcs12_to_shadow(vmx);
9961 vmx->nested.sync_shadow_vmcs = false;
9964 if (test_bit(VCPU_REGS_RSP, (unsigned long *)&vcpu->arch.regs_dirty))
9965 vmcs_writel(GUEST_RSP, vcpu->arch.regs[VCPU_REGS_RSP]);
9966 if (test_bit(VCPU_REGS_RIP, (unsigned long *)&vcpu->arch.regs_dirty))
9967 vmcs_writel(GUEST_RIP, vcpu->arch.regs[VCPU_REGS_RIP]);
9969 cr3 = __get_current_cr3_fast();
9970 if (unlikely(cr3 != vmx->loaded_vmcs->vmcs_host_cr3)) {
9971 vmcs_writel(HOST_CR3, cr3);
9972 vmx->loaded_vmcs->vmcs_host_cr3 = cr3;
9975 cr4 = cr4_read_shadow();
9976 if (unlikely(cr4 != vmx->loaded_vmcs->vmcs_host_cr4)) {
9977 vmcs_writel(HOST_CR4, cr4);
9978 vmx->loaded_vmcs->vmcs_host_cr4 = cr4;
9981 /* When single-stepping over STI and MOV SS, we must clear the
9982 * corresponding interruptibility bits in the guest state. Otherwise
9983 * vmentry fails as it then expects bit 14 (BS) in pending debug
9984 * exceptions being set, but that's not correct for the guest debugging
9986 if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP)
9987 vmx_set_interrupt_shadow(vcpu, 0);
9989 if (static_cpu_has(X86_FEATURE_PKU) &&
9990 kvm_read_cr4_bits(vcpu, X86_CR4_PKE) &&
9991 vcpu->arch.pkru != vmx->host_pkru)
9992 __write_pkru(vcpu->arch.pkru);
9994 atomic_switch_perf_msrs(vmx);
9996 vmx_arm_hv_timer(vcpu);
9999 * If this vCPU has touched SPEC_CTRL, restore the guest's value if
10000 * it's non-zero. Since vmentry is serialising on affected CPUs, there
10001 * is no need to worry about the conditional branch over the wrmsr
10002 * being speculatively taken.
10004 x86_spec_ctrl_set_guest(vmx->spec_ctrl, 0);
10006 vmx->__launched = vmx->loaded_vmcs->launched;
10008 evmcs_rsp = static_branch_unlikely(&enable_evmcs) ?
10009 (unsigned long)¤t_evmcs->host_rsp : 0;
10012 /* Store host registers */
10013 "push %%" _ASM_DX "; push %%" _ASM_BP ";"
10014 "push %%" _ASM_CX " \n\t" /* placeholder for guest rcx */
10015 "push %%" _ASM_CX " \n\t"
10016 "cmp %%" _ASM_SP ", %c[host_rsp](%0) \n\t"
10018 "mov %%" _ASM_SP ", %c[host_rsp](%0) \n\t"
10019 /* Avoid VMWRITE when Enlightened VMCS is in use */
10020 "test %%" _ASM_SI ", %%" _ASM_SI " \n\t"
10022 "mov %%" _ASM_SP ", (%%" _ASM_SI ") \n\t"
10025 __ex(ASM_VMX_VMWRITE_RSP_RDX) "\n\t"
10027 /* Reload cr2 if changed */
10028 "mov %c[cr2](%0), %%" _ASM_AX " \n\t"
10029 "mov %%cr2, %%" _ASM_DX " \n\t"
10030 "cmp %%" _ASM_AX ", %%" _ASM_DX " \n\t"
10032 "mov %%" _ASM_AX", %%cr2 \n\t"
10034 /* Check if vmlaunch of vmresume is needed */
10035 "cmpl $0, %c[launched](%0) \n\t"
10036 /* Load guest registers. Don't clobber flags. */
10037 "mov %c[rax](%0), %%" _ASM_AX " \n\t"
10038 "mov %c[rbx](%0), %%" _ASM_BX " \n\t"
10039 "mov %c[rdx](%0), %%" _ASM_DX " \n\t"
10040 "mov %c[rsi](%0), %%" _ASM_SI " \n\t"
10041 "mov %c[rdi](%0), %%" _ASM_DI " \n\t"
10042 "mov %c[rbp](%0), %%" _ASM_BP " \n\t"
10043 #ifdef CONFIG_X86_64
10044 "mov %c[r8](%0), %%r8 \n\t"
10045 "mov %c[r9](%0), %%r9 \n\t"
10046 "mov %c[r10](%0), %%r10 \n\t"
10047 "mov %c[r11](%0), %%r11 \n\t"
10048 "mov %c[r12](%0), %%r12 \n\t"
10049 "mov %c[r13](%0), %%r13 \n\t"
10050 "mov %c[r14](%0), %%r14 \n\t"
10051 "mov %c[r15](%0), %%r15 \n\t"
10053 "mov %c[rcx](%0), %%" _ASM_CX " \n\t" /* kills %0 (ecx) */
10055 /* Enter guest mode */
10057 __ex(ASM_VMX_VMLAUNCH) "\n\t"
10059 "1: " __ex(ASM_VMX_VMRESUME) "\n\t"
10061 /* Save guest registers, load host registers, keep flags */
10062 "mov %0, %c[wordsize](%%" _ASM_SP ") \n\t"
10064 "setbe %c[fail](%0)\n\t"
10065 "mov %%" _ASM_AX ", %c[rax](%0) \n\t"
10066 "mov %%" _ASM_BX ", %c[rbx](%0) \n\t"
10067 __ASM_SIZE(pop) " %c[rcx](%0) \n\t"
10068 "mov %%" _ASM_DX ", %c[rdx](%0) \n\t"
10069 "mov %%" _ASM_SI ", %c[rsi](%0) \n\t"
10070 "mov %%" _ASM_DI ", %c[rdi](%0) \n\t"
10071 "mov %%" _ASM_BP ", %c[rbp](%0) \n\t"
10072 #ifdef CONFIG_X86_64
10073 "mov %%r8, %c[r8](%0) \n\t"
10074 "mov %%r9, %c[r9](%0) \n\t"
10075 "mov %%r10, %c[r10](%0) \n\t"
10076 "mov %%r11, %c[r11](%0) \n\t"
10077 "mov %%r12, %c[r12](%0) \n\t"
10078 "mov %%r13, %c[r13](%0) \n\t"
10079 "mov %%r14, %c[r14](%0) \n\t"
10080 "mov %%r15, %c[r15](%0) \n\t"
10081 "xor %%r8d, %%r8d \n\t"
10082 "xor %%r9d, %%r9d \n\t"
10083 "xor %%r10d, %%r10d \n\t"
10084 "xor %%r11d, %%r11d \n\t"
10085 "xor %%r12d, %%r12d \n\t"
10086 "xor %%r13d, %%r13d \n\t"
10087 "xor %%r14d, %%r14d \n\t"
10088 "xor %%r15d, %%r15d \n\t"
10090 "mov %%cr2, %%" _ASM_AX " \n\t"
10091 "mov %%" _ASM_AX ", %c[cr2](%0) \n\t"
10093 "xor %%eax, %%eax \n\t"
10094 "xor %%ebx, %%ebx \n\t"
10095 "xor %%esi, %%esi \n\t"
10096 "xor %%edi, %%edi \n\t"
10097 "pop %%" _ASM_BP "; pop %%" _ASM_DX " \n\t"
10098 ".pushsection .rodata \n\t"
10099 ".global vmx_return \n\t"
10100 "vmx_return: " _ASM_PTR " 2b \n\t"
10102 : : "c"(vmx), "d"((unsigned long)HOST_RSP), "S"(evmcs_rsp),
10103 [launched]"i"(offsetof(struct vcpu_vmx, __launched)),
10104 [fail]"i"(offsetof(struct vcpu_vmx, fail)),
10105 [host_rsp]"i"(offsetof(struct vcpu_vmx, host_rsp)),
10106 [rax]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RAX])),
10107 [rbx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBX])),
10108 [rcx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RCX])),
10109 [rdx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDX])),
10110 [rsi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RSI])),
10111 [rdi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDI])),
10112 [rbp]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBP])),
10113 #ifdef CONFIG_X86_64
10114 [r8]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R8])),
10115 [r9]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R9])),
10116 [r10]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R10])),
10117 [r11]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R11])),
10118 [r12]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R12])),
10119 [r13]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R13])),
10120 [r14]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R14])),
10121 [r15]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R15])),
10123 [cr2]"i"(offsetof(struct vcpu_vmx, vcpu.arch.cr2)),
10124 [wordsize]"i"(sizeof(ulong))
10126 #ifdef CONFIG_X86_64
10127 , "rax", "rbx", "rdi"
10128 , "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15"
10130 , "eax", "ebx", "edi"
10135 * We do not use IBRS in the kernel. If this vCPU has used the
10136 * SPEC_CTRL MSR it may have left it on; save the value and
10137 * turn it off. This is much more efficient than blindly adding
10138 * it to the atomic save/restore list. Especially as the former
10139 * (Saving guest MSRs on vmexit) doesn't even exist in KVM.
10141 * For non-nested case:
10142 * If the L01 MSR bitmap does not intercept the MSR, then we need to
10146 * If the L02 MSR bitmap does not intercept the MSR, then we need to
10149 if (unlikely(!msr_write_intercepted(vcpu, MSR_IA32_SPEC_CTRL)))
10150 vmx->spec_ctrl = native_read_msr(MSR_IA32_SPEC_CTRL);
10152 x86_spec_ctrl_restore_host(vmx->spec_ctrl, 0);
10154 /* Eliminate branch target predictions from guest mode */
10157 /* All fields are clean at this point */
10158 if (static_branch_unlikely(&enable_evmcs))
10159 current_evmcs->hv_clean_fields |=
10160 HV_VMX_ENLIGHTENED_CLEAN_FIELD_ALL;
10162 /* MSR_IA32_DEBUGCTLMSR is zeroed on vmexit. Restore it if needed */
10163 if (vmx->host_debugctlmsr)
10164 update_debugctlmsr(vmx->host_debugctlmsr);
10166 #ifndef CONFIG_X86_64
10168 * The sysexit path does not restore ds/es, so we must set them to
10169 * a reasonable value ourselves.
10171 * We can't defer this to vmx_load_host_state() since that function
10172 * may be executed in interrupt context, which saves and restore segments
10173 * around it, nullifying its effect.
10175 loadsegment(ds, __USER_DS);
10176 loadsegment(es, __USER_DS);
10179 vcpu->arch.regs_avail = ~((1 << VCPU_REGS_RIP) | (1 << VCPU_REGS_RSP)
10180 | (1 << VCPU_EXREG_RFLAGS)
10181 | (1 << VCPU_EXREG_PDPTR)
10182 | (1 << VCPU_EXREG_SEGMENTS)
10183 | (1 << VCPU_EXREG_CR3));
10184 vcpu->arch.regs_dirty = 0;
10187 * eager fpu is enabled if PKEY is supported and CR4 is switched
10188 * back on host, so it is safe to read guest PKRU from current
10191 if (static_cpu_has(X86_FEATURE_PKU) &&
10192 kvm_read_cr4_bits(vcpu, X86_CR4_PKE)) {
10193 vcpu->arch.pkru = __read_pkru();
10194 if (vcpu->arch.pkru != vmx->host_pkru)
10195 __write_pkru(vmx->host_pkru);
10198 vmx->nested.nested_run_pending = 0;
10199 vmx->idt_vectoring_info = 0;
10201 vmx->exit_reason = vmx->fail ? 0xdead : vmcs_read32(VM_EXIT_REASON);
10202 if (vmx->fail || (vmx->exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY))
10205 vmx->loaded_vmcs->launched = 1;
10206 vmx->idt_vectoring_info = vmcs_read32(IDT_VECTORING_INFO_FIELD);
10208 vmx_complete_atomic_exit(vmx);
10209 vmx_recover_nmi_blocking(vmx);
10210 vmx_complete_interrupts(vmx);
10212 STACK_FRAME_NON_STANDARD(vmx_vcpu_run);
10214 static struct kvm *vmx_vm_alloc(void)
10216 struct kvm_vmx *kvm_vmx = vzalloc(sizeof(struct kvm_vmx));
10217 return &kvm_vmx->kvm;
10220 static void vmx_vm_free(struct kvm *kvm)
10222 vfree(to_kvm_vmx(kvm));
10225 static void vmx_switch_vmcs(struct kvm_vcpu *vcpu, struct loaded_vmcs *vmcs)
10227 struct vcpu_vmx *vmx = to_vmx(vcpu);
10230 if (vmx->loaded_vmcs == vmcs)
10234 vmx->loaded_vmcs = vmcs;
10235 vmx_vcpu_put(vcpu);
10236 vmx_vcpu_load(vcpu, cpu);
10241 * Ensure that the current vmcs of the logical processor is the
10242 * vmcs01 of the vcpu before calling free_nested().
10244 static void vmx_free_vcpu_nested(struct kvm_vcpu *vcpu)
10246 struct vcpu_vmx *vmx = to_vmx(vcpu);
10249 vmx_switch_vmcs(vcpu, &vmx->vmcs01);
10254 static void vmx_free_vcpu(struct kvm_vcpu *vcpu)
10256 struct vcpu_vmx *vmx = to_vmx(vcpu);
10259 vmx_destroy_pml_buffer(vmx);
10260 free_vpid(vmx->vpid);
10261 leave_guest_mode(vcpu);
10262 vmx_free_vcpu_nested(vcpu);
10263 free_loaded_vmcs(vmx->loaded_vmcs);
10264 kfree(vmx->guest_msrs);
10265 kvm_vcpu_uninit(vcpu);
10266 kmem_cache_free(kvm_vcpu_cache, vmx);
10269 static struct kvm_vcpu *vmx_create_vcpu(struct kvm *kvm, unsigned int id)
10272 struct vcpu_vmx *vmx = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
10273 unsigned long *msr_bitmap;
10277 return ERR_PTR(-ENOMEM);
10279 vmx->vpid = allocate_vpid();
10281 err = kvm_vcpu_init(&vmx->vcpu, kvm, id);
10288 * If PML is turned on, failure on enabling PML just results in failure
10289 * of creating the vcpu, therefore we can simplify PML logic (by
10290 * avoiding dealing with cases, such as enabling PML partially on vcpus
10291 * for the guest, etc.
10294 vmx->pml_pg = alloc_page(GFP_KERNEL | __GFP_ZERO);
10299 vmx->guest_msrs = kmalloc(PAGE_SIZE, GFP_KERNEL);
10300 BUILD_BUG_ON(ARRAY_SIZE(vmx_msr_index) * sizeof(vmx->guest_msrs[0])
10303 if (!vmx->guest_msrs)
10306 err = alloc_loaded_vmcs(&vmx->vmcs01);
10310 msr_bitmap = vmx->vmcs01.msr_bitmap;
10311 vmx_disable_intercept_for_msr(msr_bitmap, MSR_FS_BASE, MSR_TYPE_RW);
10312 vmx_disable_intercept_for_msr(msr_bitmap, MSR_GS_BASE, MSR_TYPE_RW);
10313 vmx_disable_intercept_for_msr(msr_bitmap, MSR_KERNEL_GS_BASE, MSR_TYPE_RW);
10314 vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_SYSENTER_CS, MSR_TYPE_RW);
10315 vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_SYSENTER_ESP, MSR_TYPE_RW);
10316 vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_SYSENTER_EIP, MSR_TYPE_RW);
10317 vmx->msr_bitmap_mode = 0;
10319 vmx->loaded_vmcs = &vmx->vmcs01;
10321 vmx_vcpu_load(&vmx->vcpu, cpu);
10322 vmx->vcpu.cpu = cpu;
10323 vmx_vcpu_setup(vmx);
10324 vmx_vcpu_put(&vmx->vcpu);
10326 if (cpu_need_virtualize_apic_accesses(&vmx->vcpu)) {
10327 err = alloc_apic_access_page(kvm);
10332 if (enable_ept && !enable_unrestricted_guest) {
10333 err = init_rmode_identity_map(kvm);
10339 nested_vmx_setup_ctls_msrs(&vmx->nested.msrs,
10340 kvm_vcpu_apicv_active(&vmx->vcpu));
10341 vmx->nested.vpid02 = allocate_vpid();
10344 vmx->nested.posted_intr_nv = -1;
10345 vmx->nested.current_vmptr = -1ull;
10347 vmx->msr_ia32_feature_control_valid_bits = FEATURE_CONTROL_LOCKED;
10350 * Enforce invariant: pi_desc.nv is always either POSTED_INTR_VECTOR
10351 * or POSTED_INTR_WAKEUP_VECTOR.
10353 vmx->pi_desc.nv = POSTED_INTR_VECTOR;
10354 vmx->pi_desc.sn = 1;
10359 free_vpid(vmx->nested.vpid02);
10360 free_loaded_vmcs(vmx->loaded_vmcs);
10362 kfree(vmx->guest_msrs);
10364 vmx_destroy_pml_buffer(vmx);
10366 kvm_vcpu_uninit(&vmx->vcpu);
10368 free_vpid(vmx->vpid);
10369 kmem_cache_free(kvm_vcpu_cache, vmx);
10370 return ERR_PTR(err);
10373 static int vmx_vm_init(struct kvm *kvm)
10376 kvm->arch.pause_in_guest = true;
10380 static void __init vmx_check_processor_compat(void *rtn)
10382 struct vmcs_config vmcs_conf;
10385 if (setup_vmcs_config(&vmcs_conf) < 0)
10386 *(int *)rtn = -EIO;
10387 nested_vmx_setup_ctls_msrs(&vmcs_conf.nested, enable_apicv);
10388 if (memcmp(&vmcs_config, &vmcs_conf, sizeof(struct vmcs_config)) != 0) {
10389 printk(KERN_ERR "kvm: CPU %d feature inconsistency!\n",
10390 smp_processor_id());
10391 *(int *)rtn = -EIO;
10395 static u64 vmx_get_mt_mask(struct kvm_vcpu *vcpu, gfn_t gfn, bool is_mmio)
10400 /* For VT-d and EPT combination
10401 * 1. MMIO: always map as UC
10402 * 2. EPT with VT-d:
10403 * a. VT-d without snooping control feature: can't guarantee the
10404 * result, try to trust guest.
10405 * b. VT-d with snooping control feature: snooping control feature of
10406 * VT-d engine can guarantee the cache correctness. Just set it
10407 * to WB to keep consistent with host. So the same as item 3.
10408 * 3. EPT without VT-d: always map as WB and set IPAT=1 to keep
10409 * consistent with host MTRR
10412 cache = MTRR_TYPE_UNCACHABLE;
10416 if (!kvm_arch_has_noncoherent_dma(vcpu->kvm)) {
10417 ipat = VMX_EPT_IPAT_BIT;
10418 cache = MTRR_TYPE_WRBACK;
10422 if (kvm_read_cr0(vcpu) & X86_CR0_CD) {
10423 ipat = VMX_EPT_IPAT_BIT;
10424 if (kvm_check_has_quirk(vcpu->kvm, KVM_X86_QUIRK_CD_NW_CLEARED))
10425 cache = MTRR_TYPE_WRBACK;
10427 cache = MTRR_TYPE_UNCACHABLE;
10431 cache = kvm_mtrr_get_guest_memory_type(vcpu, gfn);
10434 return (cache << VMX_EPT_MT_EPTE_SHIFT) | ipat;
10437 static int vmx_get_lpage_level(void)
10439 if (enable_ept && !cpu_has_vmx_ept_1g_page())
10440 return PT_DIRECTORY_LEVEL;
10442 /* For shadow and EPT supported 1GB page */
10443 return PT_PDPE_LEVEL;
10446 static void vmcs_set_secondary_exec_control(u32 new_ctl)
10449 * These bits in the secondary execution controls field
10450 * are dynamic, the others are mostly based on the hypervisor
10451 * architecture and the guest's CPUID. Do not touch the
10455 SECONDARY_EXEC_SHADOW_VMCS |
10456 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
10457 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
10458 SECONDARY_EXEC_DESC;
10460 u32 cur_ctl = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
10462 vmcs_write32(SECONDARY_VM_EXEC_CONTROL,
10463 (new_ctl & ~mask) | (cur_ctl & mask));
10467 * Generate MSR_IA32_VMX_CR{0,4}_FIXED1 according to CPUID. Only set bits
10468 * (indicating "allowed-1") if they are supported in the guest's CPUID.
10470 static void nested_vmx_cr_fixed1_bits_update(struct kvm_vcpu *vcpu)
10472 struct vcpu_vmx *vmx = to_vmx(vcpu);
10473 struct kvm_cpuid_entry2 *entry;
10475 vmx->nested.msrs.cr0_fixed1 = 0xffffffff;
10476 vmx->nested.msrs.cr4_fixed1 = X86_CR4_PCE;
10478 #define cr4_fixed1_update(_cr4_mask, _reg, _cpuid_mask) do { \
10479 if (entry && (entry->_reg & (_cpuid_mask))) \
10480 vmx->nested.msrs.cr4_fixed1 |= (_cr4_mask); \
10483 entry = kvm_find_cpuid_entry(vcpu, 0x1, 0);
10484 cr4_fixed1_update(X86_CR4_VME, edx, bit(X86_FEATURE_VME));
10485 cr4_fixed1_update(X86_CR4_PVI, edx, bit(X86_FEATURE_VME));
10486 cr4_fixed1_update(X86_CR4_TSD, edx, bit(X86_FEATURE_TSC));
10487 cr4_fixed1_update(X86_CR4_DE, edx, bit(X86_FEATURE_DE));
10488 cr4_fixed1_update(X86_CR4_PSE, edx, bit(X86_FEATURE_PSE));
10489 cr4_fixed1_update(X86_CR4_PAE, edx, bit(X86_FEATURE_PAE));
10490 cr4_fixed1_update(X86_CR4_MCE, edx, bit(X86_FEATURE_MCE));
10491 cr4_fixed1_update(X86_CR4_PGE, edx, bit(X86_FEATURE_PGE));
10492 cr4_fixed1_update(X86_CR4_OSFXSR, edx, bit(X86_FEATURE_FXSR));
10493 cr4_fixed1_update(X86_CR4_OSXMMEXCPT, edx, bit(X86_FEATURE_XMM));
10494 cr4_fixed1_update(X86_CR4_VMXE, ecx, bit(X86_FEATURE_VMX));
10495 cr4_fixed1_update(X86_CR4_SMXE, ecx, bit(X86_FEATURE_SMX));
10496 cr4_fixed1_update(X86_CR4_PCIDE, ecx, bit(X86_FEATURE_PCID));
10497 cr4_fixed1_update(X86_CR4_OSXSAVE, ecx, bit(X86_FEATURE_XSAVE));
10499 entry = kvm_find_cpuid_entry(vcpu, 0x7, 0);
10500 cr4_fixed1_update(X86_CR4_FSGSBASE, ebx, bit(X86_FEATURE_FSGSBASE));
10501 cr4_fixed1_update(X86_CR4_SMEP, ebx, bit(X86_FEATURE_SMEP));
10502 cr4_fixed1_update(X86_CR4_SMAP, ebx, bit(X86_FEATURE_SMAP));
10503 cr4_fixed1_update(X86_CR4_PKE, ecx, bit(X86_FEATURE_PKU));
10504 cr4_fixed1_update(X86_CR4_UMIP, ecx, bit(X86_FEATURE_UMIP));
10506 #undef cr4_fixed1_update
10509 static void vmx_cpuid_update(struct kvm_vcpu *vcpu)
10511 struct vcpu_vmx *vmx = to_vmx(vcpu);
10513 if (cpu_has_secondary_exec_ctrls()) {
10514 vmx_compute_secondary_exec_control(vmx);
10515 vmcs_set_secondary_exec_control(vmx->secondary_exec_control);
10518 if (nested_vmx_allowed(vcpu))
10519 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits |=
10520 FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
10522 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits &=
10523 ~FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
10525 if (nested_vmx_allowed(vcpu))
10526 nested_vmx_cr_fixed1_bits_update(vcpu);
10529 static void vmx_set_supported_cpuid(u32 func, struct kvm_cpuid_entry2 *entry)
10531 if (func == 1 && nested)
10532 entry->ecx |= bit(X86_FEATURE_VMX);
10535 static void nested_ept_inject_page_fault(struct kvm_vcpu *vcpu,
10536 struct x86_exception *fault)
10538 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
10539 struct vcpu_vmx *vmx = to_vmx(vcpu);
10541 unsigned long exit_qualification = vcpu->arch.exit_qualification;
10543 if (vmx->nested.pml_full) {
10544 exit_reason = EXIT_REASON_PML_FULL;
10545 vmx->nested.pml_full = false;
10546 exit_qualification &= INTR_INFO_UNBLOCK_NMI;
10547 } else if (fault->error_code & PFERR_RSVD_MASK)
10548 exit_reason = EXIT_REASON_EPT_MISCONFIG;
10550 exit_reason = EXIT_REASON_EPT_VIOLATION;
10552 nested_vmx_vmexit(vcpu, exit_reason, 0, exit_qualification);
10553 vmcs12->guest_physical_address = fault->address;
10556 static bool nested_ept_ad_enabled(struct kvm_vcpu *vcpu)
10558 return nested_ept_get_cr3(vcpu) & VMX_EPTP_AD_ENABLE_BIT;
10561 /* Callbacks for nested_ept_init_mmu_context: */
10563 static unsigned long nested_ept_get_cr3(struct kvm_vcpu *vcpu)
10565 /* return the page table to be shadowed - in our case, EPT12 */
10566 return get_vmcs12(vcpu)->ept_pointer;
10569 static int nested_ept_init_mmu_context(struct kvm_vcpu *vcpu)
10571 WARN_ON(mmu_is_nested(vcpu));
10572 if (!valid_ept_address(vcpu, nested_ept_get_cr3(vcpu)))
10575 kvm_mmu_unload(vcpu);
10576 kvm_init_shadow_ept_mmu(vcpu,
10577 to_vmx(vcpu)->nested.msrs.ept_caps &
10578 VMX_EPT_EXECUTE_ONLY_BIT,
10579 nested_ept_ad_enabled(vcpu));
10580 vcpu->arch.mmu.set_cr3 = vmx_set_cr3;
10581 vcpu->arch.mmu.get_cr3 = nested_ept_get_cr3;
10582 vcpu->arch.mmu.inject_page_fault = nested_ept_inject_page_fault;
10584 vcpu->arch.walk_mmu = &vcpu->arch.nested_mmu;
10588 static void nested_ept_uninit_mmu_context(struct kvm_vcpu *vcpu)
10590 vcpu->arch.walk_mmu = &vcpu->arch.mmu;
10593 static bool nested_vmx_is_page_fault_vmexit(struct vmcs12 *vmcs12,
10596 bool inequality, bit;
10598 bit = (vmcs12->exception_bitmap & (1u << PF_VECTOR)) != 0;
10600 (error_code & vmcs12->page_fault_error_code_mask) !=
10601 vmcs12->page_fault_error_code_match;
10602 return inequality ^ bit;
10605 static void vmx_inject_page_fault_nested(struct kvm_vcpu *vcpu,
10606 struct x86_exception *fault)
10608 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
10610 WARN_ON(!is_guest_mode(vcpu));
10612 if (nested_vmx_is_page_fault_vmexit(vmcs12, fault->error_code) &&
10613 !to_vmx(vcpu)->nested.nested_run_pending) {
10614 vmcs12->vm_exit_intr_error_code = fault->error_code;
10615 nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI,
10616 PF_VECTOR | INTR_TYPE_HARD_EXCEPTION |
10617 INTR_INFO_DELIVER_CODE_MASK | INTR_INFO_VALID_MASK,
10620 kvm_inject_page_fault(vcpu, fault);
10624 static inline bool nested_vmx_prepare_msr_bitmap(struct kvm_vcpu *vcpu,
10625 struct vmcs12 *vmcs12);
10627 static void nested_get_vmcs12_pages(struct kvm_vcpu *vcpu,
10628 struct vmcs12 *vmcs12)
10630 struct vcpu_vmx *vmx = to_vmx(vcpu);
10634 if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) {
10636 * Translate L1 physical address to host physical
10637 * address for vmcs02. Keep the page pinned, so this
10638 * physical address remains valid. We keep a reference
10639 * to it so we can release it later.
10641 if (vmx->nested.apic_access_page) { /* shouldn't happen */
10642 kvm_release_page_dirty(vmx->nested.apic_access_page);
10643 vmx->nested.apic_access_page = NULL;
10645 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->apic_access_addr);
10647 * If translation failed, no matter: This feature asks
10648 * to exit when accessing the given address, and if it
10649 * can never be accessed, this feature won't do
10652 if (!is_error_page(page)) {
10653 vmx->nested.apic_access_page = page;
10654 hpa = page_to_phys(vmx->nested.apic_access_page);
10655 vmcs_write64(APIC_ACCESS_ADDR, hpa);
10657 vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL,
10658 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES);
10662 if (nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW)) {
10663 if (vmx->nested.virtual_apic_page) { /* shouldn't happen */
10664 kvm_release_page_dirty(vmx->nested.virtual_apic_page);
10665 vmx->nested.virtual_apic_page = NULL;
10667 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->virtual_apic_page_addr);
10670 * If translation failed, VM entry will fail because
10671 * prepare_vmcs02 set VIRTUAL_APIC_PAGE_ADDR to -1ull.
10672 * Failing the vm entry is _not_ what the processor
10673 * does but it's basically the only possibility we
10674 * have. We could still enter the guest if CR8 load
10675 * exits are enabled, CR8 store exits are enabled, and
10676 * virtualize APIC access is disabled; in this case
10677 * the processor would never use the TPR shadow and we
10678 * could simply clear the bit from the execution
10679 * control. But such a configuration is useless, so
10680 * let's keep the code simple.
10682 if (!is_error_page(page)) {
10683 vmx->nested.virtual_apic_page = page;
10684 hpa = page_to_phys(vmx->nested.virtual_apic_page);
10685 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, hpa);
10689 if (nested_cpu_has_posted_intr(vmcs12)) {
10690 if (vmx->nested.pi_desc_page) { /* shouldn't happen */
10691 kunmap(vmx->nested.pi_desc_page);
10692 kvm_release_page_dirty(vmx->nested.pi_desc_page);
10693 vmx->nested.pi_desc_page = NULL;
10695 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->posted_intr_desc_addr);
10696 if (is_error_page(page))
10698 vmx->nested.pi_desc_page = page;
10699 vmx->nested.pi_desc = kmap(vmx->nested.pi_desc_page);
10700 vmx->nested.pi_desc =
10701 (struct pi_desc *)((void *)vmx->nested.pi_desc +
10702 (unsigned long)(vmcs12->posted_intr_desc_addr &
10704 vmcs_write64(POSTED_INTR_DESC_ADDR,
10705 page_to_phys(vmx->nested.pi_desc_page) +
10706 (unsigned long)(vmcs12->posted_intr_desc_addr &
10709 if (nested_vmx_prepare_msr_bitmap(vcpu, vmcs12))
10710 vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL,
10711 CPU_BASED_USE_MSR_BITMAPS);
10713 vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL,
10714 CPU_BASED_USE_MSR_BITMAPS);
10717 static void vmx_start_preemption_timer(struct kvm_vcpu *vcpu)
10719 u64 preemption_timeout = get_vmcs12(vcpu)->vmx_preemption_timer_value;
10720 struct vcpu_vmx *vmx = to_vmx(vcpu);
10722 if (vcpu->arch.virtual_tsc_khz == 0)
10725 /* Make sure short timeouts reliably trigger an immediate vmexit.
10726 * hrtimer_start does not guarantee this. */
10727 if (preemption_timeout <= 1) {
10728 vmx_preemption_timer_fn(&vmx->nested.preemption_timer);
10732 preemption_timeout <<= VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE;
10733 preemption_timeout *= 1000000;
10734 do_div(preemption_timeout, vcpu->arch.virtual_tsc_khz);
10735 hrtimer_start(&vmx->nested.preemption_timer,
10736 ns_to_ktime(preemption_timeout), HRTIMER_MODE_REL);
10739 static int nested_vmx_check_io_bitmap_controls(struct kvm_vcpu *vcpu,
10740 struct vmcs12 *vmcs12)
10742 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_IO_BITMAPS))
10745 if (!page_address_valid(vcpu, vmcs12->io_bitmap_a) ||
10746 !page_address_valid(vcpu, vmcs12->io_bitmap_b))
10752 static int nested_vmx_check_msr_bitmap_controls(struct kvm_vcpu *vcpu,
10753 struct vmcs12 *vmcs12)
10755 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS))
10758 if (!page_address_valid(vcpu, vmcs12->msr_bitmap))
10764 static int nested_vmx_check_tpr_shadow_controls(struct kvm_vcpu *vcpu,
10765 struct vmcs12 *vmcs12)
10767 if (!nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW))
10770 if (!page_address_valid(vcpu, vmcs12->virtual_apic_page_addr))
10777 * Merge L0's and L1's MSR bitmap, return false to indicate that
10778 * we do not use the hardware.
10780 static inline bool nested_vmx_prepare_msr_bitmap(struct kvm_vcpu *vcpu,
10781 struct vmcs12 *vmcs12)
10785 unsigned long *msr_bitmap_l1;
10786 unsigned long *msr_bitmap_l0 = to_vmx(vcpu)->nested.vmcs02.msr_bitmap;
10788 * pred_cmd & spec_ctrl are trying to verify two things:
10790 * 1. L0 gave a permission to L1 to actually passthrough the MSR. This
10791 * ensures that we do not accidentally generate an L02 MSR bitmap
10792 * from the L12 MSR bitmap that is too permissive.
10793 * 2. That L1 or L2s have actually used the MSR. This avoids
10794 * unnecessarily merging of the bitmap if the MSR is unused. This
10795 * works properly because we only update the L01 MSR bitmap lazily.
10796 * So even if L0 should pass L1 these MSRs, the L01 bitmap is only
10797 * updated to reflect this when L1 (or its L2s) actually write to
10800 bool pred_cmd = !msr_write_intercepted_l01(vcpu, MSR_IA32_PRED_CMD);
10801 bool spec_ctrl = !msr_write_intercepted_l01(vcpu, MSR_IA32_SPEC_CTRL);
10803 /* Nothing to do if the MSR bitmap is not in use. */
10804 if (!cpu_has_vmx_msr_bitmap() ||
10805 !nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS))
10808 if (!nested_cpu_has_virt_x2apic_mode(vmcs12) &&
10809 !pred_cmd && !spec_ctrl)
10812 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->msr_bitmap);
10813 if (is_error_page(page))
10816 msr_bitmap_l1 = (unsigned long *)kmap(page);
10817 if (nested_cpu_has_apic_reg_virt(vmcs12)) {
10819 * L0 need not intercept reads for MSRs between 0x800 and 0x8ff, it
10820 * just lets the processor take the value from the virtual-APIC page;
10821 * take those 256 bits directly from the L1 bitmap.
10823 for (msr = 0x800; msr <= 0x8ff; msr += BITS_PER_LONG) {
10824 unsigned word = msr / BITS_PER_LONG;
10825 msr_bitmap_l0[word] = msr_bitmap_l1[word];
10826 msr_bitmap_l0[word + (0x800 / sizeof(long))] = ~0;
10829 for (msr = 0x800; msr <= 0x8ff; msr += BITS_PER_LONG) {
10830 unsigned word = msr / BITS_PER_LONG;
10831 msr_bitmap_l0[word] = ~0;
10832 msr_bitmap_l0[word + (0x800 / sizeof(long))] = ~0;
10836 nested_vmx_disable_intercept_for_msr(
10837 msr_bitmap_l1, msr_bitmap_l0,
10838 X2APIC_MSR(APIC_TASKPRI),
10841 if (nested_cpu_has_vid(vmcs12)) {
10842 nested_vmx_disable_intercept_for_msr(
10843 msr_bitmap_l1, msr_bitmap_l0,
10844 X2APIC_MSR(APIC_EOI),
10846 nested_vmx_disable_intercept_for_msr(
10847 msr_bitmap_l1, msr_bitmap_l0,
10848 X2APIC_MSR(APIC_SELF_IPI),
10853 nested_vmx_disable_intercept_for_msr(
10854 msr_bitmap_l1, msr_bitmap_l0,
10855 MSR_IA32_SPEC_CTRL,
10856 MSR_TYPE_R | MSR_TYPE_W);
10859 nested_vmx_disable_intercept_for_msr(
10860 msr_bitmap_l1, msr_bitmap_l0,
10865 kvm_release_page_clean(page);
10870 static int nested_vmx_check_apic_access_controls(struct kvm_vcpu *vcpu,
10871 struct vmcs12 *vmcs12)
10873 if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES) &&
10874 !page_address_valid(vcpu, vmcs12->apic_access_addr))
10880 static int nested_vmx_check_apicv_controls(struct kvm_vcpu *vcpu,
10881 struct vmcs12 *vmcs12)
10883 if (!nested_cpu_has_virt_x2apic_mode(vmcs12) &&
10884 !nested_cpu_has_apic_reg_virt(vmcs12) &&
10885 !nested_cpu_has_vid(vmcs12) &&
10886 !nested_cpu_has_posted_intr(vmcs12))
10890 * If virtualize x2apic mode is enabled,
10891 * virtualize apic access must be disabled.
10893 if (nested_cpu_has_virt_x2apic_mode(vmcs12) &&
10894 nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES))
10898 * If virtual interrupt delivery is enabled,
10899 * we must exit on external interrupts.
10901 if (nested_cpu_has_vid(vmcs12) &&
10902 !nested_exit_on_intr(vcpu))
10906 * bits 15:8 should be zero in posted_intr_nv,
10907 * the descriptor address has been already checked
10908 * in nested_get_vmcs12_pages.
10910 if (nested_cpu_has_posted_intr(vmcs12) &&
10911 (!nested_cpu_has_vid(vmcs12) ||
10912 !nested_exit_intr_ack_set(vcpu) ||
10913 vmcs12->posted_intr_nv & 0xff00))
10916 /* tpr shadow is needed by all apicv features. */
10917 if (!nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW))
10923 static int nested_vmx_check_msr_switch(struct kvm_vcpu *vcpu,
10924 unsigned long count_field,
10925 unsigned long addr_field)
10930 if (vmcs12_read_any(vcpu, count_field, &count) ||
10931 vmcs12_read_any(vcpu, addr_field, &addr)) {
10937 maxphyaddr = cpuid_maxphyaddr(vcpu);
10938 if (!IS_ALIGNED(addr, 16) || addr >> maxphyaddr ||
10939 (addr + count * sizeof(struct vmx_msr_entry) - 1) >> maxphyaddr) {
10940 pr_debug_ratelimited(
10941 "nVMX: invalid MSR switch (0x%lx, %d, %llu, 0x%08llx)",
10942 addr_field, maxphyaddr, count, addr);
10948 static int nested_vmx_check_msr_switch_controls(struct kvm_vcpu *vcpu,
10949 struct vmcs12 *vmcs12)
10951 if (vmcs12->vm_exit_msr_load_count == 0 &&
10952 vmcs12->vm_exit_msr_store_count == 0 &&
10953 vmcs12->vm_entry_msr_load_count == 0)
10954 return 0; /* Fast path */
10955 if (nested_vmx_check_msr_switch(vcpu, VM_EXIT_MSR_LOAD_COUNT,
10956 VM_EXIT_MSR_LOAD_ADDR) ||
10957 nested_vmx_check_msr_switch(vcpu, VM_EXIT_MSR_STORE_COUNT,
10958 VM_EXIT_MSR_STORE_ADDR) ||
10959 nested_vmx_check_msr_switch(vcpu, VM_ENTRY_MSR_LOAD_COUNT,
10960 VM_ENTRY_MSR_LOAD_ADDR))
10965 static int nested_vmx_check_pml_controls(struct kvm_vcpu *vcpu,
10966 struct vmcs12 *vmcs12)
10968 u64 address = vmcs12->pml_address;
10969 int maxphyaddr = cpuid_maxphyaddr(vcpu);
10971 if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_PML)) {
10972 if (!nested_cpu_has_ept(vmcs12) ||
10973 !IS_ALIGNED(address, 4096) ||
10974 address >> maxphyaddr)
10981 static int nested_vmx_msr_check_common(struct kvm_vcpu *vcpu,
10982 struct vmx_msr_entry *e)
10984 /* x2APIC MSR accesses are not allowed */
10985 if (vcpu->arch.apic_base & X2APIC_ENABLE && e->index >> 8 == 0x8)
10987 if (e->index == MSR_IA32_UCODE_WRITE || /* SDM Table 35-2 */
10988 e->index == MSR_IA32_UCODE_REV)
10990 if (e->reserved != 0)
10995 static int nested_vmx_load_msr_check(struct kvm_vcpu *vcpu,
10996 struct vmx_msr_entry *e)
10998 if (e->index == MSR_FS_BASE ||
10999 e->index == MSR_GS_BASE ||
11000 e->index == MSR_IA32_SMM_MONITOR_CTL || /* SMM is not supported */
11001 nested_vmx_msr_check_common(vcpu, e))
11006 static int nested_vmx_store_msr_check(struct kvm_vcpu *vcpu,
11007 struct vmx_msr_entry *e)
11009 if (e->index == MSR_IA32_SMBASE || /* SMM is not supported */
11010 nested_vmx_msr_check_common(vcpu, e))
11016 * Load guest's/host's msr at nested entry/exit.
11017 * return 0 for success, entry index for failure.
11019 static u32 nested_vmx_load_msr(struct kvm_vcpu *vcpu, u64 gpa, u32 count)
11022 struct vmx_msr_entry e;
11023 struct msr_data msr;
11025 msr.host_initiated = false;
11026 for (i = 0; i < count; i++) {
11027 if (kvm_vcpu_read_guest(vcpu, gpa + i * sizeof(e),
11029 pr_debug_ratelimited(
11030 "%s cannot read MSR entry (%u, 0x%08llx)\n",
11031 __func__, i, gpa + i * sizeof(e));
11034 if (nested_vmx_load_msr_check(vcpu, &e)) {
11035 pr_debug_ratelimited(
11036 "%s check failed (%u, 0x%x, 0x%x)\n",
11037 __func__, i, e.index, e.reserved);
11040 msr.index = e.index;
11041 msr.data = e.value;
11042 if (kvm_set_msr(vcpu, &msr)) {
11043 pr_debug_ratelimited(
11044 "%s cannot write MSR (%u, 0x%x, 0x%llx)\n",
11045 __func__, i, e.index, e.value);
11054 static int nested_vmx_store_msr(struct kvm_vcpu *vcpu, u64 gpa, u32 count)
11057 struct vmx_msr_entry e;
11059 for (i = 0; i < count; i++) {
11060 struct msr_data msr_info;
11061 if (kvm_vcpu_read_guest(vcpu,
11062 gpa + i * sizeof(e),
11063 &e, 2 * sizeof(u32))) {
11064 pr_debug_ratelimited(
11065 "%s cannot read MSR entry (%u, 0x%08llx)\n",
11066 __func__, i, gpa + i * sizeof(e));
11069 if (nested_vmx_store_msr_check(vcpu, &e)) {
11070 pr_debug_ratelimited(
11071 "%s check failed (%u, 0x%x, 0x%x)\n",
11072 __func__, i, e.index, e.reserved);
11075 msr_info.host_initiated = false;
11076 msr_info.index = e.index;
11077 if (kvm_get_msr(vcpu, &msr_info)) {
11078 pr_debug_ratelimited(
11079 "%s cannot read MSR (%u, 0x%x)\n",
11080 __func__, i, e.index);
11083 if (kvm_vcpu_write_guest(vcpu,
11084 gpa + i * sizeof(e) +
11085 offsetof(struct vmx_msr_entry, value),
11086 &msr_info.data, sizeof(msr_info.data))) {
11087 pr_debug_ratelimited(
11088 "%s cannot write MSR (%u, 0x%x, 0x%llx)\n",
11089 __func__, i, e.index, msr_info.data);
11096 static bool nested_cr3_valid(struct kvm_vcpu *vcpu, unsigned long val)
11098 unsigned long invalid_mask;
11100 invalid_mask = (~0ULL) << cpuid_maxphyaddr(vcpu);
11101 return (val & invalid_mask) == 0;
11105 * Load guest's/host's cr3 at nested entry/exit. nested_ept is true if we are
11106 * emulating VM entry into a guest with EPT enabled.
11107 * Returns 0 on success, 1 on failure. Invalid state exit qualification code
11108 * is assigned to entry_failure_code on failure.
11110 static int nested_vmx_load_cr3(struct kvm_vcpu *vcpu, unsigned long cr3, bool nested_ept,
11111 u32 *entry_failure_code)
11113 if (cr3 != kvm_read_cr3(vcpu) || (!nested_ept && pdptrs_changed(vcpu))) {
11114 if (!nested_cr3_valid(vcpu, cr3)) {
11115 *entry_failure_code = ENTRY_FAIL_DEFAULT;
11120 * If PAE paging and EPT are both on, CR3 is not used by the CPU and
11121 * must not be dereferenced.
11123 if (!is_long_mode(vcpu) && is_pae(vcpu) && is_paging(vcpu) &&
11125 if (!load_pdptrs(vcpu, vcpu->arch.walk_mmu, cr3)) {
11126 *entry_failure_code = ENTRY_FAIL_PDPTE;
11131 vcpu->arch.cr3 = cr3;
11132 __set_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail);
11135 kvm_mmu_reset_context(vcpu);
11139 static void prepare_vmcs02_full(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
11141 struct vcpu_vmx *vmx = to_vmx(vcpu);
11143 vmcs_write16(GUEST_ES_SELECTOR, vmcs12->guest_es_selector);
11144 vmcs_write16(GUEST_SS_SELECTOR, vmcs12->guest_ss_selector);
11145 vmcs_write16(GUEST_DS_SELECTOR, vmcs12->guest_ds_selector);
11146 vmcs_write16(GUEST_FS_SELECTOR, vmcs12->guest_fs_selector);
11147 vmcs_write16(GUEST_GS_SELECTOR, vmcs12->guest_gs_selector);
11148 vmcs_write16(GUEST_LDTR_SELECTOR, vmcs12->guest_ldtr_selector);
11149 vmcs_write16(GUEST_TR_SELECTOR, vmcs12->guest_tr_selector);
11150 vmcs_write32(GUEST_ES_LIMIT, vmcs12->guest_es_limit);
11151 vmcs_write32(GUEST_SS_LIMIT, vmcs12->guest_ss_limit);
11152 vmcs_write32(GUEST_DS_LIMIT, vmcs12->guest_ds_limit);
11153 vmcs_write32(GUEST_FS_LIMIT, vmcs12->guest_fs_limit);
11154 vmcs_write32(GUEST_GS_LIMIT, vmcs12->guest_gs_limit);
11155 vmcs_write32(GUEST_LDTR_LIMIT, vmcs12->guest_ldtr_limit);
11156 vmcs_write32(GUEST_TR_LIMIT, vmcs12->guest_tr_limit);
11157 vmcs_write32(GUEST_GDTR_LIMIT, vmcs12->guest_gdtr_limit);
11158 vmcs_write32(GUEST_IDTR_LIMIT, vmcs12->guest_idtr_limit);
11159 vmcs_write32(GUEST_ES_AR_BYTES, vmcs12->guest_es_ar_bytes);
11160 vmcs_write32(GUEST_SS_AR_BYTES, vmcs12->guest_ss_ar_bytes);
11161 vmcs_write32(GUEST_DS_AR_BYTES, vmcs12->guest_ds_ar_bytes);
11162 vmcs_write32(GUEST_FS_AR_BYTES, vmcs12->guest_fs_ar_bytes);
11163 vmcs_write32(GUEST_GS_AR_BYTES, vmcs12->guest_gs_ar_bytes);
11164 vmcs_write32(GUEST_LDTR_AR_BYTES, vmcs12->guest_ldtr_ar_bytes);
11165 vmcs_write32(GUEST_TR_AR_BYTES, vmcs12->guest_tr_ar_bytes);
11166 vmcs_writel(GUEST_SS_BASE, vmcs12->guest_ss_base);
11167 vmcs_writel(GUEST_DS_BASE, vmcs12->guest_ds_base);
11168 vmcs_writel(GUEST_FS_BASE, vmcs12->guest_fs_base);
11169 vmcs_writel(GUEST_GS_BASE, vmcs12->guest_gs_base);
11170 vmcs_writel(GUEST_LDTR_BASE, vmcs12->guest_ldtr_base);
11171 vmcs_writel(GUEST_TR_BASE, vmcs12->guest_tr_base);
11172 vmcs_writel(GUEST_GDTR_BASE, vmcs12->guest_gdtr_base);
11173 vmcs_writel(GUEST_IDTR_BASE, vmcs12->guest_idtr_base);
11175 vmcs_write32(GUEST_SYSENTER_CS, vmcs12->guest_sysenter_cs);
11176 vmcs_writel(GUEST_PENDING_DBG_EXCEPTIONS,
11177 vmcs12->guest_pending_dbg_exceptions);
11178 vmcs_writel(GUEST_SYSENTER_ESP, vmcs12->guest_sysenter_esp);
11179 vmcs_writel(GUEST_SYSENTER_EIP, vmcs12->guest_sysenter_eip);
11181 if (nested_cpu_has_xsaves(vmcs12))
11182 vmcs_write64(XSS_EXIT_BITMAP, vmcs12->xss_exit_bitmap);
11183 vmcs_write64(VMCS_LINK_POINTER, -1ull);
11185 if (cpu_has_vmx_posted_intr())
11186 vmcs_write16(POSTED_INTR_NV, POSTED_INTR_NESTED_VECTOR);
11189 * Whether page-faults are trapped is determined by a combination of
11190 * 3 settings: PFEC_MASK, PFEC_MATCH and EXCEPTION_BITMAP.PF.
11191 * If enable_ept, L0 doesn't care about page faults and we should
11192 * set all of these to L1's desires. However, if !enable_ept, L0 does
11193 * care about (at least some) page faults, and because it is not easy
11194 * (if at all possible?) to merge L0 and L1's desires, we simply ask
11195 * to exit on each and every L2 page fault. This is done by setting
11196 * MASK=MATCH=0 and (see below) EB.PF=1.
11197 * Note that below we don't need special code to set EB.PF beyond the
11198 * "or"ing of the EB of vmcs01 and vmcs12, because when enable_ept,
11199 * vmcs01's EB.PF is 0 so the "or" will take vmcs12's value, and when
11200 * !enable_ept, EB.PF is 1, so the "or" will always be 1.
11202 vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK,
11203 enable_ept ? vmcs12->page_fault_error_code_mask : 0);
11204 vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH,
11205 enable_ept ? vmcs12->page_fault_error_code_match : 0);
11207 /* All VMFUNCs are currently emulated through L0 vmexits. */
11208 if (cpu_has_vmx_vmfunc())
11209 vmcs_write64(VM_FUNCTION_CONTROL, 0);
11211 if (cpu_has_vmx_apicv()) {
11212 vmcs_write64(EOI_EXIT_BITMAP0, vmcs12->eoi_exit_bitmap0);
11213 vmcs_write64(EOI_EXIT_BITMAP1, vmcs12->eoi_exit_bitmap1);
11214 vmcs_write64(EOI_EXIT_BITMAP2, vmcs12->eoi_exit_bitmap2);
11215 vmcs_write64(EOI_EXIT_BITMAP3, vmcs12->eoi_exit_bitmap3);
11219 * Set host-state according to L0's settings (vmcs12 is irrelevant here)
11220 * Some constant fields are set here by vmx_set_constant_host_state().
11221 * Other fields are different per CPU, and will be set later when
11222 * vmx_vcpu_load() is called, and when vmx_save_host_state() is called.
11224 vmx_set_constant_host_state(vmx);
11227 * Set the MSR load/store lists to match L0's settings.
11229 vmcs_write32(VM_EXIT_MSR_STORE_COUNT, 0);
11230 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, vmx->msr_autoload.nr);
11231 vmcs_write64(VM_EXIT_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.host));
11232 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, vmx->msr_autoload.nr);
11233 vmcs_write64(VM_ENTRY_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.guest));
11235 set_cr4_guest_host_mask(vmx);
11237 if (vmx_mpx_supported())
11238 vmcs_write64(GUEST_BNDCFGS, vmcs12->guest_bndcfgs);
11241 if (nested_cpu_has_vpid(vmcs12) && vmx->nested.vpid02)
11242 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->nested.vpid02);
11244 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid);
11248 * L1 may access the L2's PDPTR, so save them to construct vmcs12
11251 vmcs_write64(GUEST_PDPTR0, vmcs12->guest_pdptr0);
11252 vmcs_write64(GUEST_PDPTR1, vmcs12->guest_pdptr1);
11253 vmcs_write64(GUEST_PDPTR2, vmcs12->guest_pdptr2);
11254 vmcs_write64(GUEST_PDPTR3, vmcs12->guest_pdptr3);
11257 if (cpu_has_vmx_msr_bitmap())
11258 vmcs_write64(MSR_BITMAP, __pa(vmx->nested.vmcs02.msr_bitmap));
11262 * prepare_vmcs02 is called when the L1 guest hypervisor runs its nested
11263 * L2 guest. L1 has a vmcs for L2 (vmcs12), and this function "merges" it
11264 * with L0's requirements for its guest (a.k.a. vmcs01), so we can run the L2
11265 * guest in a way that will both be appropriate to L1's requests, and our
11266 * needs. In addition to modifying the active vmcs (which is vmcs02), this
11267 * function also has additional necessary side-effects, like setting various
11268 * vcpu->arch fields.
11269 * Returns 0 on success, 1 on failure. Invalid state exit qualification code
11270 * is assigned to entry_failure_code on failure.
11272 static int prepare_vmcs02(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12,
11273 u32 *entry_failure_code)
11275 struct vcpu_vmx *vmx = to_vmx(vcpu);
11276 u32 exec_control, vmcs12_exec_ctrl;
11278 if (vmx->nested.dirty_vmcs12) {
11279 prepare_vmcs02_full(vcpu, vmcs12);
11280 vmx->nested.dirty_vmcs12 = false;
11284 * First, the fields that are shadowed. This must be kept in sync
11285 * with vmx_shadow_fields.h.
11288 vmcs_write16(GUEST_CS_SELECTOR, vmcs12->guest_cs_selector);
11289 vmcs_write32(GUEST_CS_LIMIT, vmcs12->guest_cs_limit);
11290 vmcs_write32(GUEST_CS_AR_BYTES, vmcs12->guest_cs_ar_bytes);
11291 vmcs_writel(GUEST_ES_BASE, vmcs12->guest_es_base);
11292 vmcs_writel(GUEST_CS_BASE, vmcs12->guest_cs_base);
11295 * Not in vmcs02: GUEST_PML_INDEX, HOST_FS_SELECTOR, HOST_GS_SELECTOR,
11296 * HOST_FS_BASE, HOST_GS_BASE.
11299 if (vmx->nested.nested_run_pending &&
11300 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS)) {
11301 kvm_set_dr(vcpu, 7, vmcs12->guest_dr7);
11302 vmcs_write64(GUEST_IA32_DEBUGCTL, vmcs12->guest_ia32_debugctl);
11304 kvm_set_dr(vcpu, 7, vcpu->arch.dr7);
11305 vmcs_write64(GUEST_IA32_DEBUGCTL, vmx->nested.vmcs01_debugctl);
11307 if (vmx->nested.nested_run_pending) {
11308 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
11309 vmcs12->vm_entry_intr_info_field);
11310 vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE,
11311 vmcs12->vm_entry_exception_error_code);
11312 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
11313 vmcs12->vm_entry_instruction_len);
11314 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO,
11315 vmcs12->guest_interruptibility_info);
11316 vmx->loaded_vmcs->nmi_known_unmasked =
11317 !(vmcs12->guest_interruptibility_info & GUEST_INTR_STATE_NMI);
11319 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0);
11321 vmx_set_rflags(vcpu, vmcs12->guest_rflags);
11323 exec_control = vmcs12->pin_based_vm_exec_control;
11325 /* Preemption timer setting is only taken from vmcs01. */
11326 exec_control &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
11327 exec_control |= vmcs_config.pin_based_exec_ctrl;
11328 if (vmx->hv_deadline_tsc == -1)
11329 exec_control &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
11331 /* Posted interrupts setting is only taken from vmcs12. */
11332 if (nested_cpu_has_posted_intr(vmcs12)) {
11333 vmx->nested.posted_intr_nv = vmcs12->posted_intr_nv;
11334 vmx->nested.pi_pending = false;
11336 exec_control &= ~PIN_BASED_POSTED_INTR;
11339 vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, exec_control);
11341 vmx->nested.preemption_timer_expired = false;
11342 if (nested_cpu_has_preemption_timer(vmcs12))
11343 vmx_start_preemption_timer(vcpu);
11345 if (cpu_has_secondary_exec_ctrls()) {
11346 exec_control = vmx->secondary_exec_control;
11348 /* Take the following fields only from vmcs12 */
11349 exec_control &= ~(SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
11350 SECONDARY_EXEC_ENABLE_INVPCID |
11351 SECONDARY_EXEC_RDTSCP |
11352 SECONDARY_EXEC_XSAVES |
11353 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY |
11354 SECONDARY_EXEC_APIC_REGISTER_VIRT |
11355 SECONDARY_EXEC_ENABLE_VMFUNC);
11356 if (nested_cpu_has(vmcs12,
11357 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS)) {
11358 vmcs12_exec_ctrl = vmcs12->secondary_vm_exec_control &
11359 ~SECONDARY_EXEC_ENABLE_PML;
11360 exec_control |= vmcs12_exec_ctrl;
11363 if (exec_control & SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY)
11364 vmcs_write16(GUEST_INTR_STATUS,
11365 vmcs12->guest_intr_status);
11368 * Write an illegal value to APIC_ACCESS_ADDR. Later,
11369 * nested_get_vmcs12_pages will either fix it up or
11370 * remove the VM execution control.
11372 if (exec_control & SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)
11373 vmcs_write64(APIC_ACCESS_ADDR, -1ull);
11375 vmcs_write32(SECONDARY_VM_EXEC_CONTROL, exec_control);
11379 * HOST_RSP is normally set correctly in vmx_vcpu_run() just before
11380 * entry, but only if the current (host) sp changed from the value
11381 * we wrote last (vmx->host_rsp). This cache is no longer relevant
11382 * if we switch vmcs, and rather than hold a separate cache per vmcs,
11383 * here we just force the write to happen on entry.
11387 exec_control = vmx_exec_control(vmx); /* L0's desires */
11388 exec_control &= ~CPU_BASED_VIRTUAL_INTR_PENDING;
11389 exec_control &= ~CPU_BASED_VIRTUAL_NMI_PENDING;
11390 exec_control &= ~CPU_BASED_TPR_SHADOW;
11391 exec_control |= vmcs12->cpu_based_vm_exec_control;
11394 * Write an illegal value to VIRTUAL_APIC_PAGE_ADDR. Later, if
11395 * nested_get_vmcs12_pages can't fix it up, the illegal value
11396 * will result in a VM entry failure.
11398 if (exec_control & CPU_BASED_TPR_SHADOW) {
11399 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, -1ull);
11400 vmcs_write32(TPR_THRESHOLD, vmcs12->tpr_threshold);
11402 #ifdef CONFIG_X86_64
11403 exec_control |= CPU_BASED_CR8_LOAD_EXITING |
11404 CPU_BASED_CR8_STORE_EXITING;
11409 * A vmexit (to either L1 hypervisor or L0 userspace) is always needed
11410 * for I/O port accesses.
11412 exec_control &= ~CPU_BASED_USE_IO_BITMAPS;
11413 exec_control |= CPU_BASED_UNCOND_IO_EXITING;
11415 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, exec_control);
11417 /* EXCEPTION_BITMAP and CR0_GUEST_HOST_MASK should basically be the
11418 * bitwise-or of what L1 wants to trap for L2, and what we want to
11419 * trap. Note that CR0.TS also needs updating - we do this later.
11421 update_exception_bitmap(vcpu);
11422 vcpu->arch.cr0_guest_owned_bits &= ~vmcs12->cr0_guest_host_mask;
11423 vmcs_writel(CR0_GUEST_HOST_MASK, ~vcpu->arch.cr0_guest_owned_bits);
11425 /* L2->L1 exit controls are emulated - the hardware exit is to L0 so
11426 * we should use its exit controls. Note that VM_EXIT_LOAD_IA32_EFER
11427 * bits are further modified by vmx_set_efer() below.
11429 vmcs_write32(VM_EXIT_CONTROLS, vmcs_config.vmexit_ctrl);
11431 /* vmcs12's VM_ENTRY_LOAD_IA32_EFER and VM_ENTRY_IA32E_MODE are
11432 * emulated by vmx_set_efer(), below.
11434 vm_entry_controls_init(vmx,
11435 (vmcs12->vm_entry_controls & ~VM_ENTRY_LOAD_IA32_EFER &
11436 ~VM_ENTRY_IA32E_MODE) |
11437 (vmcs_config.vmentry_ctrl & ~VM_ENTRY_IA32E_MODE));
11439 if (vmx->nested.nested_run_pending &&
11440 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_PAT)) {
11441 vmcs_write64(GUEST_IA32_PAT, vmcs12->guest_ia32_pat);
11442 vcpu->arch.pat = vmcs12->guest_ia32_pat;
11443 } else if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) {
11444 vmcs_write64(GUEST_IA32_PAT, vmx->vcpu.arch.pat);
11447 vmcs_write64(TSC_OFFSET, vcpu->arch.tsc_offset);
11449 if (kvm_has_tsc_control)
11450 decache_tsc_multiplier(vmx);
11454 * There is no direct mapping between vpid02 and vpid12, the
11455 * vpid02 is per-vCPU for L0 and reused while the value of
11456 * vpid12 is changed w/ one invvpid during nested vmentry.
11457 * The vpid12 is allocated by L1 for L2, so it will not
11458 * influence global bitmap(for vpid01 and vpid02 allocation)
11459 * even if spawn a lot of nested vCPUs.
11461 if (nested_cpu_has_vpid(vmcs12) && vmx->nested.vpid02) {
11462 if (vmcs12->virtual_processor_id != vmx->nested.last_vpid) {
11463 vmx->nested.last_vpid = vmcs12->virtual_processor_id;
11464 __vmx_flush_tlb(vcpu, vmx->nested.vpid02, true);
11467 vmx_flush_tlb(vcpu, true);
11473 * Conceptually we want to copy the PML address and index from
11474 * vmcs01 here, and then back to vmcs01 on nested vmexit. But,
11475 * since we always flush the log on each vmexit, this happens
11476 * to be equivalent to simply resetting the fields in vmcs02.
11478 ASSERT(vmx->pml_pg);
11479 vmcs_write64(PML_ADDRESS, page_to_phys(vmx->pml_pg));
11480 vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1);
11483 if (nested_cpu_has_ept(vmcs12)) {
11484 if (nested_ept_init_mmu_context(vcpu)) {
11485 *entry_failure_code = ENTRY_FAIL_DEFAULT;
11488 } else if (nested_cpu_has2(vmcs12,
11489 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) {
11490 vmx_flush_tlb(vcpu, true);
11494 * This sets GUEST_CR0 to vmcs12->guest_cr0, possibly modifying those
11495 * bits which we consider mandatory enabled.
11496 * The CR0_READ_SHADOW is what L2 should have expected to read given
11497 * the specifications by L1; It's not enough to take
11498 * vmcs12->cr0_read_shadow because on our cr0_guest_host_mask we we
11499 * have more bits than L1 expected.
11501 vmx_set_cr0(vcpu, vmcs12->guest_cr0);
11502 vmcs_writel(CR0_READ_SHADOW, nested_read_cr0(vmcs12));
11504 vmx_set_cr4(vcpu, vmcs12->guest_cr4);
11505 vmcs_writel(CR4_READ_SHADOW, nested_read_cr4(vmcs12));
11507 if (vmx->nested.nested_run_pending &&
11508 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_EFER))
11509 vcpu->arch.efer = vmcs12->guest_ia32_efer;
11510 else if (vmcs12->vm_entry_controls & VM_ENTRY_IA32E_MODE)
11511 vcpu->arch.efer |= (EFER_LMA | EFER_LME);
11513 vcpu->arch.efer &= ~(EFER_LMA | EFER_LME);
11514 /* Note: modifies VM_ENTRY/EXIT_CONTROLS and GUEST/HOST_IA32_EFER */
11515 vmx_set_efer(vcpu, vcpu->arch.efer);
11518 * Guest state is invalid and unrestricted guest is disabled,
11519 * which means L1 attempted VMEntry to L2 with invalid state.
11520 * Fail the VMEntry.
11522 if (vmx->emulation_required) {
11523 *entry_failure_code = ENTRY_FAIL_DEFAULT;
11527 /* Shadow page tables on either EPT or shadow page tables. */
11528 if (nested_vmx_load_cr3(vcpu, vmcs12->guest_cr3, nested_cpu_has_ept(vmcs12),
11529 entry_failure_code))
11533 vcpu->arch.walk_mmu->inject_page_fault = vmx_inject_page_fault_nested;
11535 kvm_register_write(vcpu, VCPU_REGS_RSP, vmcs12->guest_rsp);
11536 kvm_register_write(vcpu, VCPU_REGS_RIP, vmcs12->guest_rip);
11540 static int nested_vmx_check_nmi_controls(struct vmcs12 *vmcs12)
11542 if (!nested_cpu_has_nmi_exiting(vmcs12) &&
11543 nested_cpu_has_virtual_nmis(vmcs12))
11546 if (!nested_cpu_has_virtual_nmis(vmcs12) &&
11547 nested_cpu_has(vmcs12, CPU_BASED_VIRTUAL_NMI_PENDING))
11553 static int check_vmentry_prereqs(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
11555 struct vcpu_vmx *vmx = to_vmx(vcpu);
11557 if (vmcs12->guest_activity_state != GUEST_ACTIVITY_ACTIVE &&
11558 vmcs12->guest_activity_state != GUEST_ACTIVITY_HLT)
11559 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11561 if (nested_vmx_check_io_bitmap_controls(vcpu, vmcs12))
11562 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11564 if (nested_vmx_check_msr_bitmap_controls(vcpu, vmcs12))
11565 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11567 if (nested_vmx_check_apic_access_controls(vcpu, vmcs12))
11568 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11570 if (nested_vmx_check_tpr_shadow_controls(vcpu, vmcs12))
11571 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11573 if (nested_vmx_check_apicv_controls(vcpu, vmcs12))
11574 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11576 if (nested_vmx_check_msr_switch_controls(vcpu, vmcs12))
11577 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11579 if (nested_vmx_check_pml_controls(vcpu, vmcs12))
11580 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11582 if (!vmx_control_verify(vmcs12->cpu_based_vm_exec_control,
11583 vmx->nested.msrs.procbased_ctls_low,
11584 vmx->nested.msrs.procbased_ctls_high) ||
11585 (nested_cpu_has(vmcs12, CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) &&
11586 !vmx_control_verify(vmcs12->secondary_vm_exec_control,
11587 vmx->nested.msrs.secondary_ctls_low,
11588 vmx->nested.msrs.secondary_ctls_high)) ||
11589 !vmx_control_verify(vmcs12->pin_based_vm_exec_control,
11590 vmx->nested.msrs.pinbased_ctls_low,
11591 vmx->nested.msrs.pinbased_ctls_high) ||
11592 !vmx_control_verify(vmcs12->vm_exit_controls,
11593 vmx->nested.msrs.exit_ctls_low,
11594 vmx->nested.msrs.exit_ctls_high) ||
11595 !vmx_control_verify(vmcs12->vm_entry_controls,
11596 vmx->nested.msrs.entry_ctls_low,
11597 vmx->nested.msrs.entry_ctls_high))
11598 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11600 if (nested_vmx_check_nmi_controls(vmcs12))
11601 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11603 if (nested_cpu_has_vmfunc(vmcs12)) {
11604 if (vmcs12->vm_function_control &
11605 ~vmx->nested.msrs.vmfunc_controls)
11606 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11608 if (nested_cpu_has_eptp_switching(vmcs12)) {
11609 if (!nested_cpu_has_ept(vmcs12) ||
11610 !page_address_valid(vcpu, vmcs12->eptp_list_address))
11611 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11615 if (vmcs12->cr3_target_count > nested_cpu_vmx_misc_cr3_count(vcpu))
11616 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11618 if (!nested_host_cr0_valid(vcpu, vmcs12->host_cr0) ||
11619 !nested_host_cr4_valid(vcpu, vmcs12->host_cr4) ||
11620 !nested_cr3_valid(vcpu, vmcs12->host_cr3))
11621 return VMXERR_ENTRY_INVALID_HOST_STATE_FIELD;
11626 static int check_vmentry_postreqs(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12,
11631 *exit_qual = ENTRY_FAIL_DEFAULT;
11633 if (!nested_guest_cr0_valid(vcpu, vmcs12->guest_cr0) ||
11634 !nested_guest_cr4_valid(vcpu, vmcs12->guest_cr4))
11637 if (!nested_cpu_has2(vmcs12, SECONDARY_EXEC_SHADOW_VMCS) &&
11638 vmcs12->vmcs_link_pointer != -1ull) {
11639 *exit_qual = ENTRY_FAIL_VMCS_LINK_PTR;
11644 * If the load IA32_EFER VM-entry control is 1, the following checks
11645 * are performed on the field for the IA32_EFER MSR:
11646 * - Bits reserved in the IA32_EFER MSR must be 0.
11647 * - Bit 10 (corresponding to IA32_EFER.LMA) must equal the value of
11648 * the IA-32e mode guest VM-exit control. It must also be identical
11649 * to bit 8 (LME) if bit 31 in the CR0 field (corresponding to
11652 if (to_vmx(vcpu)->nested.nested_run_pending &&
11653 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_EFER)) {
11654 ia32e = (vmcs12->vm_entry_controls & VM_ENTRY_IA32E_MODE) != 0;
11655 if (!kvm_valid_efer(vcpu, vmcs12->guest_ia32_efer) ||
11656 ia32e != !!(vmcs12->guest_ia32_efer & EFER_LMA) ||
11657 ((vmcs12->guest_cr0 & X86_CR0_PG) &&
11658 ia32e != !!(vmcs12->guest_ia32_efer & EFER_LME)))
11663 * If the load IA32_EFER VM-exit control is 1, bits reserved in the
11664 * IA32_EFER MSR must be 0 in the field for that register. In addition,
11665 * the values of the LMA and LME bits in the field must each be that of
11666 * the host address-space size VM-exit control.
11668 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_EFER) {
11669 ia32e = (vmcs12->vm_exit_controls &
11670 VM_EXIT_HOST_ADDR_SPACE_SIZE) != 0;
11671 if (!kvm_valid_efer(vcpu, vmcs12->host_ia32_efer) ||
11672 ia32e != !!(vmcs12->host_ia32_efer & EFER_LMA) ||
11673 ia32e != !!(vmcs12->host_ia32_efer & EFER_LME))
11677 if ((vmcs12->vm_entry_controls & VM_ENTRY_LOAD_BNDCFGS) &&
11678 (is_noncanonical_address(vmcs12->guest_bndcfgs & PAGE_MASK, vcpu) ||
11679 (vmcs12->guest_bndcfgs & MSR_IA32_BNDCFGS_RSVD)))
11685 static int enter_vmx_non_root_mode(struct kvm_vcpu *vcpu)
11687 struct vcpu_vmx *vmx = to_vmx(vcpu);
11688 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
11693 enter_guest_mode(vcpu);
11695 if (!(vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS))
11696 vmx->nested.vmcs01_debugctl = vmcs_read64(GUEST_IA32_DEBUGCTL);
11698 vmx_switch_vmcs(vcpu, &vmx->nested.vmcs02);
11699 vmx_segment_cache_clear(vmx);
11701 if (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING)
11702 vcpu->arch.tsc_offset += vmcs12->tsc_offset;
11704 r = EXIT_REASON_INVALID_STATE;
11705 if (prepare_vmcs02(vcpu, vmcs12, &exit_qual))
11708 nested_get_vmcs12_pages(vcpu, vmcs12);
11710 r = EXIT_REASON_MSR_LOAD_FAIL;
11711 msr_entry_idx = nested_vmx_load_msr(vcpu,
11712 vmcs12->vm_entry_msr_load_addr,
11713 vmcs12->vm_entry_msr_load_count);
11718 * Note no nested_vmx_succeed or nested_vmx_fail here. At this point
11719 * we are no longer running L1, and VMLAUNCH/VMRESUME has not yet
11720 * returned as far as L1 is concerned. It will only return (and set
11721 * the success flag) when L2 exits (see nested_vmx_vmexit()).
11726 if (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING)
11727 vcpu->arch.tsc_offset -= vmcs12->tsc_offset;
11728 leave_guest_mode(vcpu);
11729 vmx_switch_vmcs(vcpu, &vmx->vmcs01);
11730 nested_vmx_entry_failure(vcpu, vmcs12, r, exit_qual);
11735 * nested_vmx_run() handles a nested entry, i.e., a VMLAUNCH or VMRESUME on L1
11736 * for running an L2 nested guest.
11738 static int nested_vmx_run(struct kvm_vcpu *vcpu, bool launch)
11740 struct vmcs12 *vmcs12;
11741 struct vcpu_vmx *vmx = to_vmx(vcpu);
11742 u32 interrupt_shadow = vmx_get_interrupt_shadow(vcpu);
11746 if (!nested_vmx_check_permission(vcpu))
11749 if (!nested_vmx_check_vmcs12(vcpu))
11752 vmcs12 = get_vmcs12(vcpu);
11754 if (enable_shadow_vmcs)
11755 copy_shadow_to_vmcs12(vmx);
11758 * The nested entry process starts with enforcing various prerequisites
11759 * on vmcs12 as required by the Intel SDM, and act appropriately when
11760 * they fail: As the SDM explains, some conditions should cause the
11761 * instruction to fail, while others will cause the instruction to seem
11762 * to succeed, but return an EXIT_REASON_INVALID_STATE.
11763 * To speed up the normal (success) code path, we should avoid checking
11764 * for misconfigurations which will anyway be caught by the processor
11765 * when using the merged vmcs02.
11767 if (interrupt_shadow & KVM_X86_SHADOW_INT_MOV_SS) {
11768 nested_vmx_failValid(vcpu,
11769 VMXERR_ENTRY_EVENTS_BLOCKED_BY_MOV_SS);
11773 if (vmcs12->launch_state == launch) {
11774 nested_vmx_failValid(vcpu,
11775 launch ? VMXERR_VMLAUNCH_NONCLEAR_VMCS
11776 : VMXERR_VMRESUME_NONLAUNCHED_VMCS);
11780 ret = check_vmentry_prereqs(vcpu, vmcs12);
11782 nested_vmx_failValid(vcpu, ret);
11787 * After this point, the trap flag no longer triggers a singlestep trap
11788 * on the vm entry instructions; don't call kvm_skip_emulated_instruction.
11789 * This is not 100% correct; for performance reasons, we delegate most
11790 * of the checks on host state to the processor. If those fail,
11791 * the singlestep trap is missed.
11793 skip_emulated_instruction(vcpu);
11795 ret = check_vmentry_postreqs(vcpu, vmcs12, &exit_qual);
11797 nested_vmx_entry_failure(vcpu, vmcs12,
11798 EXIT_REASON_INVALID_STATE, exit_qual);
11803 * We're finally done with prerequisite checking, and can start with
11804 * the nested entry.
11807 vmx->nested.nested_run_pending = 1;
11808 ret = enter_vmx_non_root_mode(vcpu);
11810 vmx->nested.nested_run_pending = 0;
11815 * If we're entering a halted L2 vcpu and the L2 vcpu won't be woken
11816 * by event injection, halt vcpu.
11818 if ((vmcs12->guest_activity_state == GUEST_ACTIVITY_HLT) &&
11819 !(vmcs12->vm_entry_intr_info_field & INTR_INFO_VALID_MASK)) {
11820 vmx->nested.nested_run_pending = 0;
11821 return kvm_vcpu_halt(vcpu);
11826 return kvm_skip_emulated_instruction(vcpu);
11830 * On a nested exit from L2 to L1, vmcs12.guest_cr0 might not be up-to-date
11831 * because L2 may have changed some cr0 bits directly (CRO_GUEST_HOST_MASK).
11832 * This function returns the new value we should put in vmcs12.guest_cr0.
11833 * It's not enough to just return the vmcs02 GUEST_CR0. Rather,
11834 * 1. Bits that neither L0 nor L1 trapped, were set directly by L2 and are now
11835 * available in vmcs02 GUEST_CR0. (Note: It's enough to check that L0
11836 * didn't trap the bit, because if L1 did, so would L0).
11837 * 2. Bits that L1 asked to trap (and therefore L0 also did) could not have
11838 * been modified by L2, and L1 knows it. So just leave the old value of
11839 * the bit from vmcs12.guest_cr0. Note that the bit from vmcs02 GUEST_CR0
11840 * isn't relevant, because if L0 traps this bit it can set it to anything.
11841 * 3. Bits that L1 didn't trap, but L0 did. L1 believes the guest could have
11842 * changed these bits, and therefore they need to be updated, but L0
11843 * didn't necessarily allow them to be changed in GUEST_CR0 - and rather
11844 * put them in vmcs02 CR0_READ_SHADOW. So take these bits from there.
11846 static inline unsigned long
11847 vmcs12_guest_cr0(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
11850 /*1*/ (vmcs_readl(GUEST_CR0) & vcpu->arch.cr0_guest_owned_bits) |
11851 /*2*/ (vmcs12->guest_cr0 & vmcs12->cr0_guest_host_mask) |
11852 /*3*/ (vmcs_readl(CR0_READ_SHADOW) & ~(vmcs12->cr0_guest_host_mask |
11853 vcpu->arch.cr0_guest_owned_bits));
11856 static inline unsigned long
11857 vmcs12_guest_cr4(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
11860 /*1*/ (vmcs_readl(GUEST_CR4) & vcpu->arch.cr4_guest_owned_bits) |
11861 /*2*/ (vmcs12->guest_cr4 & vmcs12->cr4_guest_host_mask) |
11862 /*3*/ (vmcs_readl(CR4_READ_SHADOW) & ~(vmcs12->cr4_guest_host_mask |
11863 vcpu->arch.cr4_guest_owned_bits));
11866 static void vmcs12_save_pending_event(struct kvm_vcpu *vcpu,
11867 struct vmcs12 *vmcs12)
11872 if (vcpu->arch.exception.injected) {
11873 nr = vcpu->arch.exception.nr;
11874 idt_vectoring = nr | VECTORING_INFO_VALID_MASK;
11876 if (kvm_exception_is_soft(nr)) {
11877 vmcs12->vm_exit_instruction_len =
11878 vcpu->arch.event_exit_inst_len;
11879 idt_vectoring |= INTR_TYPE_SOFT_EXCEPTION;
11881 idt_vectoring |= INTR_TYPE_HARD_EXCEPTION;
11883 if (vcpu->arch.exception.has_error_code) {
11884 idt_vectoring |= VECTORING_INFO_DELIVER_CODE_MASK;
11885 vmcs12->idt_vectoring_error_code =
11886 vcpu->arch.exception.error_code;
11889 vmcs12->idt_vectoring_info_field = idt_vectoring;
11890 } else if (vcpu->arch.nmi_injected) {
11891 vmcs12->idt_vectoring_info_field =
11892 INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK | NMI_VECTOR;
11893 } else if (vcpu->arch.interrupt.injected) {
11894 nr = vcpu->arch.interrupt.nr;
11895 idt_vectoring = nr | VECTORING_INFO_VALID_MASK;
11897 if (vcpu->arch.interrupt.soft) {
11898 idt_vectoring |= INTR_TYPE_SOFT_INTR;
11899 vmcs12->vm_entry_instruction_len =
11900 vcpu->arch.event_exit_inst_len;
11902 idt_vectoring |= INTR_TYPE_EXT_INTR;
11904 vmcs12->idt_vectoring_info_field = idt_vectoring;
11908 static int vmx_check_nested_events(struct kvm_vcpu *vcpu, bool external_intr)
11910 struct vcpu_vmx *vmx = to_vmx(vcpu);
11911 unsigned long exit_qual;
11912 bool block_nested_events =
11913 vmx->nested.nested_run_pending || kvm_event_needs_reinjection(vcpu);
11915 if (vcpu->arch.exception.pending &&
11916 nested_vmx_check_exception(vcpu, &exit_qual)) {
11917 if (block_nested_events)
11919 nested_vmx_inject_exception_vmexit(vcpu, exit_qual);
11923 if (nested_cpu_has_preemption_timer(get_vmcs12(vcpu)) &&
11924 vmx->nested.preemption_timer_expired) {
11925 if (block_nested_events)
11927 nested_vmx_vmexit(vcpu, EXIT_REASON_PREEMPTION_TIMER, 0, 0);
11931 if (vcpu->arch.nmi_pending && nested_exit_on_nmi(vcpu)) {
11932 if (block_nested_events)
11934 nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI,
11935 NMI_VECTOR | INTR_TYPE_NMI_INTR |
11936 INTR_INFO_VALID_MASK, 0);
11938 * The NMI-triggered VM exit counts as injection:
11939 * clear this one and block further NMIs.
11941 vcpu->arch.nmi_pending = 0;
11942 vmx_set_nmi_mask(vcpu, true);
11946 if ((kvm_cpu_has_interrupt(vcpu) || external_intr) &&
11947 nested_exit_on_intr(vcpu)) {
11948 if (block_nested_events)
11950 nested_vmx_vmexit(vcpu, EXIT_REASON_EXTERNAL_INTERRUPT, 0, 0);
11954 vmx_complete_nested_posted_interrupt(vcpu);
11958 static u32 vmx_get_preemption_timer_value(struct kvm_vcpu *vcpu)
11960 ktime_t remaining =
11961 hrtimer_get_remaining(&to_vmx(vcpu)->nested.preemption_timer);
11964 if (ktime_to_ns(remaining) <= 0)
11967 value = ktime_to_ns(remaining) * vcpu->arch.virtual_tsc_khz;
11968 do_div(value, 1000000);
11969 return value >> VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE;
11973 * Update the guest state fields of vmcs12 to reflect changes that
11974 * occurred while L2 was running. (The "IA-32e mode guest" bit of the
11975 * VM-entry controls is also updated, since this is really a guest
11978 static void sync_vmcs12(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
11980 vmcs12->guest_cr0 = vmcs12_guest_cr0(vcpu, vmcs12);
11981 vmcs12->guest_cr4 = vmcs12_guest_cr4(vcpu, vmcs12);
11983 vmcs12->guest_rsp = kvm_register_read(vcpu, VCPU_REGS_RSP);
11984 vmcs12->guest_rip = kvm_register_read(vcpu, VCPU_REGS_RIP);
11985 vmcs12->guest_rflags = vmcs_readl(GUEST_RFLAGS);
11987 vmcs12->guest_es_selector = vmcs_read16(GUEST_ES_SELECTOR);
11988 vmcs12->guest_cs_selector = vmcs_read16(GUEST_CS_SELECTOR);
11989 vmcs12->guest_ss_selector = vmcs_read16(GUEST_SS_SELECTOR);
11990 vmcs12->guest_ds_selector = vmcs_read16(GUEST_DS_SELECTOR);
11991 vmcs12->guest_fs_selector = vmcs_read16(GUEST_FS_SELECTOR);
11992 vmcs12->guest_gs_selector = vmcs_read16(GUEST_GS_SELECTOR);
11993 vmcs12->guest_ldtr_selector = vmcs_read16(GUEST_LDTR_SELECTOR);
11994 vmcs12->guest_tr_selector = vmcs_read16(GUEST_TR_SELECTOR);
11995 vmcs12->guest_es_limit = vmcs_read32(GUEST_ES_LIMIT);
11996 vmcs12->guest_cs_limit = vmcs_read32(GUEST_CS_LIMIT);
11997 vmcs12->guest_ss_limit = vmcs_read32(GUEST_SS_LIMIT);
11998 vmcs12->guest_ds_limit = vmcs_read32(GUEST_DS_LIMIT);
11999 vmcs12->guest_fs_limit = vmcs_read32(GUEST_FS_LIMIT);
12000 vmcs12->guest_gs_limit = vmcs_read32(GUEST_GS_LIMIT);
12001 vmcs12->guest_ldtr_limit = vmcs_read32(GUEST_LDTR_LIMIT);
12002 vmcs12->guest_tr_limit = vmcs_read32(GUEST_TR_LIMIT);
12003 vmcs12->guest_gdtr_limit = vmcs_read32(GUEST_GDTR_LIMIT);
12004 vmcs12->guest_idtr_limit = vmcs_read32(GUEST_IDTR_LIMIT);
12005 vmcs12->guest_es_ar_bytes = vmcs_read32(GUEST_ES_AR_BYTES);
12006 vmcs12->guest_cs_ar_bytes = vmcs_read32(GUEST_CS_AR_BYTES);
12007 vmcs12->guest_ss_ar_bytes = vmcs_read32(GUEST_SS_AR_BYTES);
12008 vmcs12->guest_ds_ar_bytes = vmcs_read32(GUEST_DS_AR_BYTES);
12009 vmcs12->guest_fs_ar_bytes = vmcs_read32(GUEST_FS_AR_BYTES);
12010 vmcs12->guest_gs_ar_bytes = vmcs_read32(GUEST_GS_AR_BYTES);
12011 vmcs12->guest_ldtr_ar_bytes = vmcs_read32(GUEST_LDTR_AR_BYTES);
12012 vmcs12->guest_tr_ar_bytes = vmcs_read32(GUEST_TR_AR_BYTES);
12013 vmcs12->guest_es_base = vmcs_readl(GUEST_ES_BASE);
12014 vmcs12->guest_cs_base = vmcs_readl(GUEST_CS_BASE);
12015 vmcs12->guest_ss_base = vmcs_readl(GUEST_SS_BASE);
12016 vmcs12->guest_ds_base = vmcs_readl(GUEST_DS_BASE);
12017 vmcs12->guest_fs_base = vmcs_readl(GUEST_FS_BASE);
12018 vmcs12->guest_gs_base = vmcs_readl(GUEST_GS_BASE);
12019 vmcs12->guest_ldtr_base = vmcs_readl(GUEST_LDTR_BASE);
12020 vmcs12->guest_tr_base = vmcs_readl(GUEST_TR_BASE);
12021 vmcs12->guest_gdtr_base = vmcs_readl(GUEST_GDTR_BASE);
12022 vmcs12->guest_idtr_base = vmcs_readl(GUEST_IDTR_BASE);
12024 vmcs12->guest_interruptibility_info =
12025 vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
12026 vmcs12->guest_pending_dbg_exceptions =
12027 vmcs_readl(GUEST_PENDING_DBG_EXCEPTIONS);
12028 if (vcpu->arch.mp_state == KVM_MP_STATE_HALTED)
12029 vmcs12->guest_activity_state = GUEST_ACTIVITY_HLT;
12031 vmcs12->guest_activity_state = GUEST_ACTIVITY_ACTIVE;
12033 if (nested_cpu_has_preemption_timer(vmcs12)) {
12034 if (vmcs12->vm_exit_controls &
12035 VM_EXIT_SAVE_VMX_PREEMPTION_TIMER)
12036 vmcs12->vmx_preemption_timer_value =
12037 vmx_get_preemption_timer_value(vcpu);
12038 hrtimer_cancel(&to_vmx(vcpu)->nested.preemption_timer);
12042 * In some cases (usually, nested EPT), L2 is allowed to change its
12043 * own CR3 without exiting. If it has changed it, we must keep it.
12044 * Of course, if L0 is using shadow page tables, GUEST_CR3 was defined
12045 * by L0, not L1 or L2, so we mustn't unconditionally copy it to vmcs12.
12047 * Additionally, restore L2's PDPTR to vmcs12.
12050 vmcs12->guest_cr3 = vmcs_readl(GUEST_CR3);
12051 vmcs12->guest_pdptr0 = vmcs_read64(GUEST_PDPTR0);
12052 vmcs12->guest_pdptr1 = vmcs_read64(GUEST_PDPTR1);
12053 vmcs12->guest_pdptr2 = vmcs_read64(GUEST_PDPTR2);
12054 vmcs12->guest_pdptr3 = vmcs_read64(GUEST_PDPTR3);
12057 vmcs12->guest_linear_address = vmcs_readl(GUEST_LINEAR_ADDRESS);
12059 if (nested_cpu_has_vid(vmcs12))
12060 vmcs12->guest_intr_status = vmcs_read16(GUEST_INTR_STATUS);
12062 vmcs12->vm_entry_controls =
12063 (vmcs12->vm_entry_controls & ~VM_ENTRY_IA32E_MODE) |
12064 (vm_entry_controls_get(to_vmx(vcpu)) & VM_ENTRY_IA32E_MODE);
12066 if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_DEBUG_CONTROLS) {
12067 kvm_get_dr(vcpu, 7, (unsigned long *)&vmcs12->guest_dr7);
12068 vmcs12->guest_ia32_debugctl = vmcs_read64(GUEST_IA32_DEBUGCTL);
12071 /* TODO: These cannot have changed unless we have MSR bitmaps and
12072 * the relevant bit asks not to trap the change */
12073 if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_IA32_PAT)
12074 vmcs12->guest_ia32_pat = vmcs_read64(GUEST_IA32_PAT);
12075 if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_IA32_EFER)
12076 vmcs12->guest_ia32_efer = vcpu->arch.efer;
12077 vmcs12->guest_sysenter_cs = vmcs_read32(GUEST_SYSENTER_CS);
12078 vmcs12->guest_sysenter_esp = vmcs_readl(GUEST_SYSENTER_ESP);
12079 vmcs12->guest_sysenter_eip = vmcs_readl(GUEST_SYSENTER_EIP);
12080 if (kvm_mpx_supported())
12081 vmcs12->guest_bndcfgs = vmcs_read64(GUEST_BNDCFGS);
12085 * prepare_vmcs12 is part of what we need to do when the nested L2 guest exits
12086 * and we want to prepare to run its L1 parent. L1 keeps a vmcs for L2 (vmcs12),
12087 * and this function updates it to reflect the changes to the guest state while
12088 * L2 was running (and perhaps made some exits which were handled directly by L0
12089 * without going back to L1), and to reflect the exit reason.
12090 * Note that we do not have to copy here all VMCS fields, just those that
12091 * could have changed by the L2 guest or the exit - i.e., the guest-state and
12092 * exit-information fields only. Other fields are modified by L1 with VMWRITE,
12093 * which already writes to vmcs12 directly.
12095 static void prepare_vmcs12(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12,
12096 u32 exit_reason, u32 exit_intr_info,
12097 unsigned long exit_qualification)
12099 /* update guest state fields: */
12100 sync_vmcs12(vcpu, vmcs12);
12102 /* update exit information fields: */
12104 vmcs12->vm_exit_reason = exit_reason;
12105 vmcs12->exit_qualification = exit_qualification;
12106 vmcs12->vm_exit_intr_info = exit_intr_info;
12108 vmcs12->idt_vectoring_info_field = 0;
12109 vmcs12->vm_exit_instruction_len = vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
12110 vmcs12->vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
12112 if (!(vmcs12->vm_exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY)) {
12113 vmcs12->launch_state = 1;
12115 /* vm_entry_intr_info_field is cleared on exit. Emulate this
12116 * instead of reading the real value. */
12117 vmcs12->vm_entry_intr_info_field &= ~INTR_INFO_VALID_MASK;
12120 * Transfer the event that L0 or L1 may wanted to inject into
12121 * L2 to IDT_VECTORING_INFO_FIELD.
12123 vmcs12_save_pending_event(vcpu, vmcs12);
12127 * Drop what we picked up for L2 via vmx_complete_interrupts. It is
12128 * preserved above and would only end up incorrectly in L1.
12130 vcpu->arch.nmi_injected = false;
12131 kvm_clear_exception_queue(vcpu);
12132 kvm_clear_interrupt_queue(vcpu);
12135 static void load_vmcs12_mmu_host_state(struct kvm_vcpu *vcpu,
12136 struct vmcs12 *vmcs12)
12138 u32 entry_failure_code;
12140 nested_ept_uninit_mmu_context(vcpu);
12143 * Only PDPTE load can fail as the value of cr3 was checked on entry and
12144 * couldn't have changed.
12146 if (nested_vmx_load_cr3(vcpu, vmcs12->host_cr3, false, &entry_failure_code))
12147 nested_vmx_abort(vcpu, VMX_ABORT_LOAD_HOST_PDPTE_FAIL);
12150 vcpu->arch.walk_mmu->inject_page_fault = kvm_inject_page_fault;
12154 * A part of what we need to when the nested L2 guest exits and we want to
12155 * run its L1 parent, is to reset L1's guest state to the host state specified
12157 * This function is to be called not only on normal nested exit, but also on
12158 * a nested entry failure, as explained in Intel's spec, 3B.23.7 ("VM-Entry
12159 * Failures During or After Loading Guest State").
12160 * This function should be called when the active VMCS is L1's (vmcs01).
12162 static void load_vmcs12_host_state(struct kvm_vcpu *vcpu,
12163 struct vmcs12 *vmcs12)
12165 struct kvm_segment seg;
12167 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_EFER)
12168 vcpu->arch.efer = vmcs12->host_ia32_efer;
12169 else if (vmcs12->vm_exit_controls & VM_EXIT_HOST_ADDR_SPACE_SIZE)
12170 vcpu->arch.efer |= (EFER_LMA | EFER_LME);
12172 vcpu->arch.efer &= ~(EFER_LMA | EFER_LME);
12173 vmx_set_efer(vcpu, vcpu->arch.efer);
12175 kvm_register_write(vcpu, VCPU_REGS_RSP, vmcs12->host_rsp);
12176 kvm_register_write(vcpu, VCPU_REGS_RIP, vmcs12->host_rip);
12177 vmx_set_rflags(vcpu, X86_EFLAGS_FIXED);
12179 * Note that calling vmx_set_cr0 is important, even if cr0 hasn't
12180 * actually changed, because vmx_set_cr0 refers to efer set above.
12182 * CR0_GUEST_HOST_MASK is already set in the original vmcs01
12183 * (KVM doesn't change it);
12185 vcpu->arch.cr0_guest_owned_bits = X86_CR0_TS;
12186 vmx_set_cr0(vcpu, vmcs12->host_cr0);
12188 /* Same as above - no reason to call set_cr4_guest_host_mask(). */
12189 vcpu->arch.cr4_guest_owned_bits = ~vmcs_readl(CR4_GUEST_HOST_MASK);
12190 vmx_set_cr4(vcpu, vmcs12->host_cr4);
12192 load_vmcs12_mmu_host_state(vcpu, vmcs12);
12195 * If vmcs01 don't use VPID, CPU flushes TLB on every
12196 * VMEntry/VMExit. Thus, no need to flush TLB.
12198 * If vmcs12 uses VPID, TLB entries populated by L2 are
12199 * tagged with vmx->nested.vpid02 while L1 entries are tagged
12200 * with vmx->vpid. Thus, no need to flush TLB.
12202 * Therefore, flush TLB only in case vmcs01 uses VPID and
12203 * vmcs12 don't use VPID as in this case L1 & L2 TLB entries
12204 * are both tagged with vmx->vpid.
12207 !(nested_cpu_has_vpid(vmcs12) && to_vmx(vcpu)->nested.vpid02)) {
12208 vmx_flush_tlb(vcpu, true);
12211 vmcs_write32(GUEST_SYSENTER_CS, vmcs12->host_ia32_sysenter_cs);
12212 vmcs_writel(GUEST_SYSENTER_ESP, vmcs12->host_ia32_sysenter_esp);
12213 vmcs_writel(GUEST_SYSENTER_EIP, vmcs12->host_ia32_sysenter_eip);
12214 vmcs_writel(GUEST_IDTR_BASE, vmcs12->host_idtr_base);
12215 vmcs_writel(GUEST_GDTR_BASE, vmcs12->host_gdtr_base);
12216 vmcs_write32(GUEST_IDTR_LIMIT, 0xFFFF);
12217 vmcs_write32(GUEST_GDTR_LIMIT, 0xFFFF);
12219 /* If not VM_EXIT_CLEAR_BNDCFGS, the L2 value propagates to L1. */
12220 if (vmcs12->vm_exit_controls & VM_EXIT_CLEAR_BNDCFGS)
12221 vmcs_write64(GUEST_BNDCFGS, 0);
12223 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PAT) {
12224 vmcs_write64(GUEST_IA32_PAT, vmcs12->host_ia32_pat);
12225 vcpu->arch.pat = vmcs12->host_ia32_pat;
12227 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL)
12228 vmcs_write64(GUEST_IA32_PERF_GLOBAL_CTRL,
12229 vmcs12->host_ia32_perf_global_ctrl);
12231 /* Set L1 segment info according to Intel SDM
12232 27.5.2 Loading Host Segment and Descriptor-Table Registers */
12233 seg = (struct kvm_segment) {
12235 .limit = 0xFFFFFFFF,
12236 .selector = vmcs12->host_cs_selector,
12242 if (vmcs12->vm_exit_controls & VM_EXIT_HOST_ADDR_SPACE_SIZE)
12246 vmx_set_segment(vcpu, &seg, VCPU_SREG_CS);
12247 seg = (struct kvm_segment) {
12249 .limit = 0xFFFFFFFF,
12256 seg.selector = vmcs12->host_ds_selector;
12257 vmx_set_segment(vcpu, &seg, VCPU_SREG_DS);
12258 seg.selector = vmcs12->host_es_selector;
12259 vmx_set_segment(vcpu, &seg, VCPU_SREG_ES);
12260 seg.selector = vmcs12->host_ss_selector;
12261 vmx_set_segment(vcpu, &seg, VCPU_SREG_SS);
12262 seg.selector = vmcs12->host_fs_selector;
12263 seg.base = vmcs12->host_fs_base;
12264 vmx_set_segment(vcpu, &seg, VCPU_SREG_FS);
12265 seg.selector = vmcs12->host_gs_selector;
12266 seg.base = vmcs12->host_gs_base;
12267 vmx_set_segment(vcpu, &seg, VCPU_SREG_GS);
12268 seg = (struct kvm_segment) {
12269 .base = vmcs12->host_tr_base,
12271 .selector = vmcs12->host_tr_selector,
12275 vmx_set_segment(vcpu, &seg, VCPU_SREG_TR);
12277 kvm_set_dr(vcpu, 7, 0x400);
12278 vmcs_write64(GUEST_IA32_DEBUGCTL, 0);
12280 if (cpu_has_vmx_msr_bitmap())
12281 vmx_update_msr_bitmap(vcpu);
12283 if (nested_vmx_load_msr(vcpu, vmcs12->vm_exit_msr_load_addr,
12284 vmcs12->vm_exit_msr_load_count))
12285 nested_vmx_abort(vcpu, VMX_ABORT_LOAD_HOST_MSR_FAIL);
12289 * Emulate an exit from nested guest (L2) to L1, i.e., prepare to run L1
12290 * and modify vmcs12 to make it see what it would expect to see there if
12291 * L2 was its real guest. Must only be called when in L2 (is_guest_mode())
12293 static void nested_vmx_vmexit(struct kvm_vcpu *vcpu, u32 exit_reason,
12294 u32 exit_intr_info,
12295 unsigned long exit_qualification)
12297 struct vcpu_vmx *vmx = to_vmx(vcpu);
12298 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
12300 /* trying to cancel vmlaunch/vmresume is a bug */
12301 WARN_ON_ONCE(vmx->nested.nested_run_pending);
12304 * The only expected VM-instruction error is "VM entry with
12305 * invalid control field(s)." Anything else indicates a
12308 WARN_ON_ONCE(vmx->fail && (vmcs_read32(VM_INSTRUCTION_ERROR) !=
12309 VMXERR_ENTRY_INVALID_CONTROL_FIELD));
12311 leave_guest_mode(vcpu);
12313 if (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING)
12314 vcpu->arch.tsc_offset -= vmcs12->tsc_offset;
12316 if (likely(!vmx->fail)) {
12317 if (exit_reason == -1)
12318 sync_vmcs12(vcpu, vmcs12);
12320 prepare_vmcs12(vcpu, vmcs12, exit_reason, exit_intr_info,
12321 exit_qualification);
12323 if (nested_vmx_store_msr(vcpu, vmcs12->vm_exit_msr_store_addr,
12324 vmcs12->vm_exit_msr_store_count))
12325 nested_vmx_abort(vcpu, VMX_ABORT_SAVE_GUEST_MSR_FAIL);
12328 vmx_switch_vmcs(vcpu, &vmx->vmcs01);
12329 vm_entry_controls_reset_shadow(vmx);
12330 vm_exit_controls_reset_shadow(vmx);
12331 vmx_segment_cache_clear(vmx);
12333 /* Update any VMCS fields that might have changed while L2 ran */
12334 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, vmx->msr_autoload.nr);
12335 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, vmx->msr_autoload.nr);
12336 vmcs_write64(TSC_OFFSET, vcpu->arch.tsc_offset);
12337 if (vmx->hv_deadline_tsc == -1)
12338 vmcs_clear_bits(PIN_BASED_VM_EXEC_CONTROL,
12339 PIN_BASED_VMX_PREEMPTION_TIMER);
12341 vmcs_set_bits(PIN_BASED_VM_EXEC_CONTROL,
12342 PIN_BASED_VMX_PREEMPTION_TIMER);
12343 if (kvm_has_tsc_control)
12344 decache_tsc_multiplier(vmx);
12346 if (vmx->nested.change_vmcs01_virtual_apic_mode) {
12347 vmx->nested.change_vmcs01_virtual_apic_mode = false;
12348 vmx_set_virtual_apic_mode(vcpu);
12349 } else if (!nested_cpu_has_ept(vmcs12) &&
12350 nested_cpu_has2(vmcs12,
12351 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) {
12352 vmx_flush_tlb(vcpu, true);
12355 /* This is needed for same reason as it was needed in prepare_vmcs02 */
12358 /* Unpin physical memory we referred to in vmcs02 */
12359 if (vmx->nested.apic_access_page) {
12360 kvm_release_page_dirty(vmx->nested.apic_access_page);
12361 vmx->nested.apic_access_page = NULL;
12363 if (vmx->nested.virtual_apic_page) {
12364 kvm_release_page_dirty(vmx->nested.virtual_apic_page);
12365 vmx->nested.virtual_apic_page = NULL;
12367 if (vmx->nested.pi_desc_page) {
12368 kunmap(vmx->nested.pi_desc_page);
12369 kvm_release_page_dirty(vmx->nested.pi_desc_page);
12370 vmx->nested.pi_desc_page = NULL;
12371 vmx->nested.pi_desc = NULL;
12375 * We are now running in L2, mmu_notifier will force to reload the
12376 * page's hpa for L2 vmcs. Need to reload it for L1 before entering L1.
12378 kvm_make_request(KVM_REQ_APIC_PAGE_RELOAD, vcpu);
12380 if (enable_shadow_vmcs && exit_reason != -1)
12381 vmx->nested.sync_shadow_vmcs = true;
12383 /* in case we halted in L2 */
12384 vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
12386 if (likely(!vmx->fail)) {
12388 * TODO: SDM says that with acknowledge interrupt on
12389 * exit, bit 31 of the VM-exit interrupt information
12390 * (valid interrupt) is always set to 1 on
12391 * EXIT_REASON_EXTERNAL_INTERRUPT, so we shouldn't
12392 * need kvm_cpu_has_interrupt(). See the commit
12393 * message for details.
12395 if (nested_exit_intr_ack_set(vcpu) &&
12396 exit_reason == EXIT_REASON_EXTERNAL_INTERRUPT &&
12397 kvm_cpu_has_interrupt(vcpu)) {
12398 int irq = kvm_cpu_get_interrupt(vcpu);
12400 vmcs12->vm_exit_intr_info = irq |
12401 INTR_INFO_VALID_MASK | INTR_TYPE_EXT_INTR;
12404 if (exit_reason != -1)
12405 trace_kvm_nested_vmexit_inject(vmcs12->vm_exit_reason,
12406 vmcs12->exit_qualification,
12407 vmcs12->idt_vectoring_info_field,
12408 vmcs12->vm_exit_intr_info,
12409 vmcs12->vm_exit_intr_error_code,
12412 load_vmcs12_host_state(vcpu, vmcs12);
12418 * After an early L2 VM-entry failure, we're now back
12419 * in L1 which thinks it just finished a VMLAUNCH or
12420 * VMRESUME instruction, so we need to set the failure
12421 * flag and the VM-instruction error field of the VMCS
12424 nested_vmx_failValid(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD);
12426 load_vmcs12_mmu_host_state(vcpu, vmcs12);
12429 * The emulated instruction was already skipped in
12430 * nested_vmx_run, but the updated RIP was never
12431 * written back to the vmcs01.
12433 skip_emulated_instruction(vcpu);
12438 * Forcibly leave nested mode in order to be able to reset the VCPU later on.
12440 static void vmx_leave_nested(struct kvm_vcpu *vcpu)
12442 if (is_guest_mode(vcpu)) {
12443 to_vmx(vcpu)->nested.nested_run_pending = 0;
12444 nested_vmx_vmexit(vcpu, -1, 0, 0);
12446 free_nested(to_vmx(vcpu));
12450 * L1's failure to enter L2 is a subset of a normal exit, as explained in
12451 * 23.7 "VM-entry failures during or after loading guest state" (this also
12452 * lists the acceptable exit-reason and exit-qualification parameters).
12453 * It should only be called before L2 actually succeeded to run, and when
12454 * vmcs01 is current (it doesn't leave_guest_mode() or switch vmcss).
12456 static void nested_vmx_entry_failure(struct kvm_vcpu *vcpu,
12457 struct vmcs12 *vmcs12,
12458 u32 reason, unsigned long qualification)
12460 load_vmcs12_host_state(vcpu, vmcs12);
12461 vmcs12->vm_exit_reason = reason | VMX_EXIT_REASONS_FAILED_VMENTRY;
12462 vmcs12->exit_qualification = qualification;
12463 nested_vmx_succeed(vcpu);
12464 if (enable_shadow_vmcs)
12465 to_vmx(vcpu)->nested.sync_shadow_vmcs = true;
12468 static int vmx_check_intercept(struct kvm_vcpu *vcpu,
12469 struct x86_instruction_info *info,
12470 enum x86_intercept_stage stage)
12472 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
12473 struct x86_emulate_ctxt *ctxt = &vcpu->arch.emulate_ctxt;
12476 * RDPID causes #UD if disabled through secondary execution controls.
12477 * Because it is marked as EmulateOnUD, we need to intercept it here.
12479 if (info->intercept == x86_intercept_rdtscp &&
12480 !nested_cpu_has2(vmcs12, SECONDARY_EXEC_RDTSCP)) {
12481 ctxt->exception.vector = UD_VECTOR;
12482 ctxt->exception.error_code_valid = false;
12483 return X86EMUL_PROPAGATE_FAULT;
12486 /* TODO: check more intercepts... */
12487 return X86EMUL_CONTINUE;
12490 #ifdef CONFIG_X86_64
12491 /* (a << shift) / divisor, return 1 if overflow otherwise 0 */
12492 static inline int u64_shl_div_u64(u64 a, unsigned int shift,
12493 u64 divisor, u64 *result)
12495 u64 low = a << shift, high = a >> (64 - shift);
12497 /* To avoid the overflow on divq */
12498 if (high >= divisor)
12501 /* Low hold the result, high hold rem which is discarded */
12502 asm("divq %2\n\t" : "=a" (low), "=d" (high) :
12503 "rm" (divisor), "0" (low), "1" (high));
12509 static int vmx_set_hv_timer(struct kvm_vcpu *vcpu, u64 guest_deadline_tsc)
12511 struct vcpu_vmx *vmx;
12512 u64 tscl, guest_tscl, delta_tsc, lapic_timer_advance_cycles;
12514 if (kvm_mwait_in_guest(vcpu->kvm))
12515 return -EOPNOTSUPP;
12517 vmx = to_vmx(vcpu);
12519 guest_tscl = kvm_read_l1_tsc(vcpu, tscl);
12520 delta_tsc = max(guest_deadline_tsc, guest_tscl) - guest_tscl;
12521 lapic_timer_advance_cycles = nsec_to_cycles(vcpu, lapic_timer_advance_ns);
12523 if (delta_tsc > lapic_timer_advance_cycles)
12524 delta_tsc -= lapic_timer_advance_cycles;
12528 /* Convert to host delta tsc if tsc scaling is enabled */
12529 if (vcpu->arch.tsc_scaling_ratio != kvm_default_tsc_scaling_ratio &&
12530 u64_shl_div_u64(delta_tsc,
12531 kvm_tsc_scaling_ratio_frac_bits,
12532 vcpu->arch.tsc_scaling_ratio,
12537 * If the delta tsc can't fit in the 32 bit after the multi shift,
12538 * we can't use the preemption timer.
12539 * It's possible that it fits on later vmentries, but checking
12540 * on every vmentry is costly so we just use an hrtimer.
12542 if (delta_tsc >> (cpu_preemption_timer_multi + 32))
12545 vmx->hv_deadline_tsc = tscl + delta_tsc;
12546 vmcs_set_bits(PIN_BASED_VM_EXEC_CONTROL,
12547 PIN_BASED_VMX_PREEMPTION_TIMER);
12549 return delta_tsc == 0;
12552 static void vmx_cancel_hv_timer(struct kvm_vcpu *vcpu)
12554 struct vcpu_vmx *vmx = to_vmx(vcpu);
12555 vmx->hv_deadline_tsc = -1;
12556 vmcs_clear_bits(PIN_BASED_VM_EXEC_CONTROL,
12557 PIN_BASED_VMX_PREEMPTION_TIMER);
12561 static void vmx_sched_in(struct kvm_vcpu *vcpu, int cpu)
12563 if (!kvm_pause_in_guest(vcpu->kvm))
12564 shrink_ple_window(vcpu);
12567 static void vmx_slot_enable_log_dirty(struct kvm *kvm,
12568 struct kvm_memory_slot *slot)
12570 kvm_mmu_slot_leaf_clear_dirty(kvm, slot);
12571 kvm_mmu_slot_largepage_remove_write_access(kvm, slot);
12574 static void vmx_slot_disable_log_dirty(struct kvm *kvm,
12575 struct kvm_memory_slot *slot)
12577 kvm_mmu_slot_set_dirty(kvm, slot);
12580 static void vmx_flush_log_dirty(struct kvm *kvm)
12582 kvm_flush_pml_buffers(kvm);
12585 static int vmx_write_pml_buffer(struct kvm_vcpu *vcpu)
12587 struct vmcs12 *vmcs12;
12588 struct vcpu_vmx *vmx = to_vmx(vcpu);
12590 struct page *page = NULL;
12593 if (is_guest_mode(vcpu)) {
12594 WARN_ON_ONCE(vmx->nested.pml_full);
12597 * Check if PML is enabled for the nested guest.
12598 * Whether eptp bit 6 is set is already checked
12599 * as part of A/D emulation.
12601 vmcs12 = get_vmcs12(vcpu);
12602 if (!nested_cpu_has_pml(vmcs12))
12605 if (vmcs12->guest_pml_index >= PML_ENTITY_NUM) {
12606 vmx->nested.pml_full = true;
12610 gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS) & ~0xFFFull;
12612 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->pml_address);
12613 if (is_error_page(page))
12616 pml_address = kmap(page);
12617 pml_address[vmcs12->guest_pml_index--] = gpa;
12619 kvm_release_page_clean(page);
12625 static void vmx_enable_log_dirty_pt_masked(struct kvm *kvm,
12626 struct kvm_memory_slot *memslot,
12627 gfn_t offset, unsigned long mask)
12629 kvm_mmu_clear_dirty_pt_masked(kvm, memslot, offset, mask);
12632 static void __pi_post_block(struct kvm_vcpu *vcpu)
12634 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
12635 struct pi_desc old, new;
12639 old.control = new.control = pi_desc->control;
12640 WARN(old.nv != POSTED_INTR_WAKEUP_VECTOR,
12641 "Wakeup handler not enabled while the VCPU is blocked\n");
12643 dest = cpu_physical_id(vcpu->cpu);
12645 if (x2apic_enabled())
12648 new.ndst = (dest << 8) & 0xFF00;
12650 /* set 'NV' to 'notification vector' */
12651 new.nv = POSTED_INTR_VECTOR;
12652 } while (cmpxchg64(&pi_desc->control, old.control,
12653 new.control) != old.control);
12655 if (!WARN_ON_ONCE(vcpu->pre_pcpu == -1)) {
12656 spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
12657 list_del(&vcpu->blocked_vcpu_list);
12658 spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
12659 vcpu->pre_pcpu = -1;
12664 * This routine does the following things for vCPU which is going
12665 * to be blocked if VT-d PI is enabled.
12666 * - Store the vCPU to the wakeup list, so when interrupts happen
12667 * we can find the right vCPU to wake up.
12668 * - Change the Posted-interrupt descriptor as below:
12669 * 'NDST' <-- vcpu->pre_pcpu
12670 * 'NV' <-- POSTED_INTR_WAKEUP_VECTOR
12671 * - If 'ON' is set during this process, which means at least one
12672 * interrupt is posted for this vCPU, we cannot block it, in
12673 * this case, return 1, otherwise, return 0.
12676 static int pi_pre_block(struct kvm_vcpu *vcpu)
12679 struct pi_desc old, new;
12680 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
12682 if (!kvm_arch_has_assigned_device(vcpu->kvm) ||
12683 !irq_remapping_cap(IRQ_POSTING_CAP) ||
12684 !kvm_vcpu_apicv_active(vcpu))
12687 WARN_ON(irqs_disabled());
12688 local_irq_disable();
12689 if (!WARN_ON_ONCE(vcpu->pre_pcpu != -1)) {
12690 vcpu->pre_pcpu = vcpu->cpu;
12691 spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
12692 list_add_tail(&vcpu->blocked_vcpu_list,
12693 &per_cpu(blocked_vcpu_on_cpu,
12695 spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
12699 old.control = new.control = pi_desc->control;
12701 WARN((pi_desc->sn == 1),
12702 "Warning: SN field of posted-interrupts "
12703 "is set before blocking\n");
12706 * Since vCPU can be preempted during this process,
12707 * vcpu->cpu could be different with pre_pcpu, we
12708 * need to set pre_pcpu as the destination of wakeup
12709 * notification event, then we can find the right vCPU
12710 * to wakeup in wakeup handler if interrupts happen
12711 * when the vCPU is in blocked state.
12713 dest = cpu_physical_id(vcpu->pre_pcpu);
12715 if (x2apic_enabled())
12718 new.ndst = (dest << 8) & 0xFF00;
12720 /* set 'NV' to 'wakeup vector' */
12721 new.nv = POSTED_INTR_WAKEUP_VECTOR;
12722 } while (cmpxchg64(&pi_desc->control, old.control,
12723 new.control) != old.control);
12725 /* We should not block the vCPU if an interrupt is posted for it. */
12726 if (pi_test_on(pi_desc) == 1)
12727 __pi_post_block(vcpu);
12729 local_irq_enable();
12730 return (vcpu->pre_pcpu == -1);
12733 static int vmx_pre_block(struct kvm_vcpu *vcpu)
12735 if (pi_pre_block(vcpu))
12738 if (kvm_lapic_hv_timer_in_use(vcpu))
12739 kvm_lapic_switch_to_sw_timer(vcpu);
12744 static void pi_post_block(struct kvm_vcpu *vcpu)
12746 if (vcpu->pre_pcpu == -1)
12749 WARN_ON(irqs_disabled());
12750 local_irq_disable();
12751 __pi_post_block(vcpu);
12752 local_irq_enable();
12755 static void vmx_post_block(struct kvm_vcpu *vcpu)
12757 if (kvm_x86_ops->set_hv_timer)
12758 kvm_lapic_switch_to_hv_timer(vcpu);
12760 pi_post_block(vcpu);
12764 * vmx_update_pi_irte - set IRTE for Posted-Interrupts
12767 * @host_irq: host irq of the interrupt
12768 * @guest_irq: gsi of the interrupt
12769 * @set: set or unset PI
12770 * returns 0 on success, < 0 on failure
12772 static int vmx_update_pi_irte(struct kvm *kvm, unsigned int host_irq,
12773 uint32_t guest_irq, bool set)
12775 struct kvm_kernel_irq_routing_entry *e;
12776 struct kvm_irq_routing_table *irq_rt;
12777 struct kvm_lapic_irq irq;
12778 struct kvm_vcpu *vcpu;
12779 struct vcpu_data vcpu_info;
12782 if (!kvm_arch_has_assigned_device(kvm) ||
12783 !irq_remapping_cap(IRQ_POSTING_CAP) ||
12784 !kvm_vcpu_apicv_active(kvm->vcpus[0]))
12787 idx = srcu_read_lock(&kvm->irq_srcu);
12788 irq_rt = srcu_dereference(kvm->irq_routing, &kvm->irq_srcu);
12789 if (guest_irq >= irq_rt->nr_rt_entries ||
12790 hlist_empty(&irq_rt->map[guest_irq])) {
12791 pr_warn_once("no route for guest_irq %u/%u (broken user space?)\n",
12792 guest_irq, irq_rt->nr_rt_entries);
12796 hlist_for_each_entry(e, &irq_rt->map[guest_irq], link) {
12797 if (e->type != KVM_IRQ_ROUTING_MSI)
12800 * VT-d PI cannot support posting multicast/broadcast
12801 * interrupts to a vCPU, we still use interrupt remapping
12802 * for these kind of interrupts.
12804 * For lowest-priority interrupts, we only support
12805 * those with single CPU as the destination, e.g. user
12806 * configures the interrupts via /proc/irq or uses
12807 * irqbalance to make the interrupts single-CPU.
12809 * We will support full lowest-priority interrupt later.
12812 kvm_set_msi_irq(kvm, e, &irq);
12813 if (!kvm_intr_is_single_vcpu(kvm, &irq, &vcpu)) {
12815 * Make sure the IRTE is in remapped mode if
12816 * we don't handle it in posted mode.
12818 ret = irq_set_vcpu_affinity(host_irq, NULL);
12821 "failed to back to remapped mode, irq: %u\n",
12829 vcpu_info.pi_desc_addr = __pa(vcpu_to_pi_desc(vcpu));
12830 vcpu_info.vector = irq.vector;
12832 trace_kvm_pi_irte_update(host_irq, vcpu->vcpu_id, e->gsi,
12833 vcpu_info.vector, vcpu_info.pi_desc_addr, set);
12836 ret = irq_set_vcpu_affinity(host_irq, &vcpu_info);
12838 ret = irq_set_vcpu_affinity(host_irq, NULL);
12841 printk(KERN_INFO "%s: failed to update PI IRTE\n",
12849 srcu_read_unlock(&kvm->irq_srcu, idx);
12853 static void vmx_setup_mce(struct kvm_vcpu *vcpu)
12855 if (vcpu->arch.mcg_cap & MCG_LMCE_P)
12856 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits |=
12857 FEATURE_CONTROL_LMCE;
12859 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits &=
12860 ~FEATURE_CONTROL_LMCE;
12863 static int vmx_smi_allowed(struct kvm_vcpu *vcpu)
12865 /* we need a nested vmexit to enter SMM, postpone if run is pending */
12866 if (to_vmx(vcpu)->nested.nested_run_pending)
12871 static int vmx_pre_enter_smm(struct kvm_vcpu *vcpu, char *smstate)
12873 struct vcpu_vmx *vmx = to_vmx(vcpu);
12875 vmx->nested.smm.guest_mode = is_guest_mode(vcpu);
12876 if (vmx->nested.smm.guest_mode)
12877 nested_vmx_vmexit(vcpu, -1, 0, 0);
12879 vmx->nested.smm.vmxon = vmx->nested.vmxon;
12880 vmx->nested.vmxon = false;
12881 vmx_clear_hlt(vcpu);
12885 static int vmx_pre_leave_smm(struct kvm_vcpu *vcpu, u64 smbase)
12887 struct vcpu_vmx *vmx = to_vmx(vcpu);
12890 if (vmx->nested.smm.vmxon) {
12891 vmx->nested.vmxon = true;
12892 vmx->nested.smm.vmxon = false;
12895 if (vmx->nested.smm.guest_mode) {
12896 vcpu->arch.hflags &= ~HF_SMM_MASK;
12897 ret = enter_vmx_non_root_mode(vcpu);
12898 vcpu->arch.hflags |= HF_SMM_MASK;
12902 vmx->nested.smm.guest_mode = false;
12907 static int enable_smi_window(struct kvm_vcpu *vcpu)
12912 static struct kvm_x86_ops vmx_x86_ops __ro_after_init = {
12913 .cpu_has_kvm_support = cpu_has_kvm_support,
12914 .disabled_by_bios = vmx_disabled_by_bios,
12915 .hardware_setup = hardware_setup,
12916 .hardware_unsetup = hardware_unsetup,
12917 .check_processor_compatibility = vmx_check_processor_compat,
12918 .hardware_enable = hardware_enable,
12919 .hardware_disable = hardware_disable,
12920 .cpu_has_accelerated_tpr = report_flexpriority,
12921 .has_emulated_msr = vmx_has_emulated_msr,
12923 .vm_init = vmx_vm_init,
12924 .vm_alloc = vmx_vm_alloc,
12925 .vm_free = vmx_vm_free,
12927 .vcpu_create = vmx_create_vcpu,
12928 .vcpu_free = vmx_free_vcpu,
12929 .vcpu_reset = vmx_vcpu_reset,
12931 .prepare_guest_switch = vmx_save_host_state,
12932 .vcpu_load = vmx_vcpu_load,
12933 .vcpu_put = vmx_vcpu_put,
12935 .update_bp_intercept = update_exception_bitmap,
12936 .get_msr_feature = vmx_get_msr_feature,
12937 .get_msr = vmx_get_msr,
12938 .set_msr = vmx_set_msr,
12939 .get_segment_base = vmx_get_segment_base,
12940 .get_segment = vmx_get_segment,
12941 .set_segment = vmx_set_segment,
12942 .get_cpl = vmx_get_cpl,
12943 .get_cs_db_l_bits = vmx_get_cs_db_l_bits,
12944 .decache_cr0_guest_bits = vmx_decache_cr0_guest_bits,
12945 .decache_cr3 = vmx_decache_cr3,
12946 .decache_cr4_guest_bits = vmx_decache_cr4_guest_bits,
12947 .set_cr0 = vmx_set_cr0,
12948 .set_cr3 = vmx_set_cr3,
12949 .set_cr4 = vmx_set_cr4,
12950 .set_efer = vmx_set_efer,
12951 .get_idt = vmx_get_idt,
12952 .set_idt = vmx_set_idt,
12953 .get_gdt = vmx_get_gdt,
12954 .set_gdt = vmx_set_gdt,
12955 .get_dr6 = vmx_get_dr6,
12956 .set_dr6 = vmx_set_dr6,
12957 .set_dr7 = vmx_set_dr7,
12958 .sync_dirty_debug_regs = vmx_sync_dirty_debug_regs,
12959 .cache_reg = vmx_cache_reg,
12960 .get_rflags = vmx_get_rflags,
12961 .set_rflags = vmx_set_rflags,
12963 .tlb_flush = vmx_flush_tlb,
12965 .run = vmx_vcpu_run,
12966 .handle_exit = vmx_handle_exit,
12967 .skip_emulated_instruction = skip_emulated_instruction,
12968 .set_interrupt_shadow = vmx_set_interrupt_shadow,
12969 .get_interrupt_shadow = vmx_get_interrupt_shadow,
12970 .patch_hypercall = vmx_patch_hypercall,
12971 .set_irq = vmx_inject_irq,
12972 .set_nmi = vmx_inject_nmi,
12973 .queue_exception = vmx_queue_exception,
12974 .cancel_injection = vmx_cancel_injection,
12975 .interrupt_allowed = vmx_interrupt_allowed,
12976 .nmi_allowed = vmx_nmi_allowed,
12977 .get_nmi_mask = vmx_get_nmi_mask,
12978 .set_nmi_mask = vmx_set_nmi_mask,
12979 .enable_nmi_window = enable_nmi_window,
12980 .enable_irq_window = enable_irq_window,
12981 .update_cr8_intercept = update_cr8_intercept,
12982 .set_virtual_apic_mode = vmx_set_virtual_apic_mode,
12983 .set_apic_access_page_addr = vmx_set_apic_access_page_addr,
12984 .get_enable_apicv = vmx_get_enable_apicv,
12985 .refresh_apicv_exec_ctrl = vmx_refresh_apicv_exec_ctrl,
12986 .load_eoi_exitmap = vmx_load_eoi_exitmap,
12987 .apicv_post_state_restore = vmx_apicv_post_state_restore,
12988 .hwapic_irr_update = vmx_hwapic_irr_update,
12989 .hwapic_isr_update = vmx_hwapic_isr_update,
12990 .sync_pir_to_irr = vmx_sync_pir_to_irr,
12991 .deliver_posted_interrupt = vmx_deliver_posted_interrupt,
12993 .set_tss_addr = vmx_set_tss_addr,
12994 .set_identity_map_addr = vmx_set_identity_map_addr,
12995 .get_tdp_level = get_ept_level,
12996 .get_mt_mask = vmx_get_mt_mask,
12998 .get_exit_info = vmx_get_exit_info,
13000 .get_lpage_level = vmx_get_lpage_level,
13002 .cpuid_update = vmx_cpuid_update,
13004 .rdtscp_supported = vmx_rdtscp_supported,
13005 .invpcid_supported = vmx_invpcid_supported,
13007 .set_supported_cpuid = vmx_set_supported_cpuid,
13009 .has_wbinvd_exit = cpu_has_vmx_wbinvd_exit,
13011 .read_l1_tsc_offset = vmx_read_l1_tsc_offset,
13012 .write_tsc_offset = vmx_write_tsc_offset,
13014 .set_tdp_cr3 = vmx_set_cr3,
13016 .check_intercept = vmx_check_intercept,
13017 .handle_external_intr = vmx_handle_external_intr,
13018 .mpx_supported = vmx_mpx_supported,
13019 .xsaves_supported = vmx_xsaves_supported,
13020 .umip_emulated = vmx_umip_emulated,
13022 .check_nested_events = vmx_check_nested_events,
13024 .sched_in = vmx_sched_in,
13026 .slot_enable_log_dirty = vmx_slot_enable_log_dirty,
13027 .slot_disable_log_dirty = vmx_slot_disable_log_dirty,
13028 .flush_log_dirty = vmx_flush_log_dirty,
13029 .enable_log_dirty_pt_masked = vmx_enable_log_dirty_pt_masked,
13030 .write_log_dirty = vmx_write_pml_buffer,
13032 .pre_block = vmx_pre_block,
13033 .post_block = vmx_post_block,
13035 .pmu_ops = &intel_pmu_ops,
13037 .update_pi_irte = vmx_update_pi_irte,
13039 #ifdef CONFIG_X86_64
13040 .set_hv_timer = vmx_set_hv_timer,
13041 .cancel_hv_timer = vmx_cancel_hv_timer,
13044 .setup_mce = vmx_setup_mce,
13046 .smi_allowed = vmx_smi_allowed,
13047 .pre_enter_smm = vmx_pre_enter_smm,
13048 .pre_leave_smm = vmx_pre_leave_smm,
13049 .enable_smi_window = enable_smi_window,
13052 static int __init vmx_init(void)
13056 #if IS_ENABLED(CONFIG_HYPERV)
13058 * Enlightened VMCS usage should be recommended and the host needs
13059 * to support eVMCS v1 or above. We can also disable eVMCS support
13060 * with module parameter.
13062 if (enlightened_vmcs &&
13063 ms_hyperv.hints & HV_X64_ENLIGHTENED_VMCS_RECOMMENDED &&
13064 (ms_hyperv.nested_features & HV_X64_ENLIGHTENED_VMCS_VERSION) >=
13065 KVM_EVMCS_VERSION) {
13068 /* Check that we have assist pages on all online CPUs */
13069 for_each_online_cpu(cpu) {
13070 if (!hv_get_vp_assist_page(cpu)) {
13071 enlightened_vmcs = false;
13076 if (enlightened_vmcs) {
13077 pr_info("KVM: vmx: using Hyper-V Enlightened VMCS\n");
13078 static_branch_enable(&enable_evmcs);
13081 enlightened_vmcs = false;
13085 r = kvm_init(&vmx_x86_ops, sizeof(struct vcpu_vmx),
13086 __alignof__(struct vcpu_vmx), THIS_MODULE);
13090 #ifdef CONFIG_KEXEC_CORE
13091 rcu_assign_pointer(crash_vmclear_loaded_vmcss,
13092 crash_vmclear_local_loaded_vmcss);
13094 vmx_check_vmcs12_offsets();
13099 static void __exit vmx_exit(void)
13101 #ifdef CONFIG_KEXEC_CORE
13102 RCU_INIT_POINTER(crash_vmclear_loaded_vmcss, NULL);
13108 #if IS_ENABLED(CONFIG_HYPERV)
13109 if (static_branch_unlikely(&enable_evmcs)) {
13111 struct hv_vp_assist_page *vp_ap;
13113 * Reset everything to support using non-enlightened VMCS
13114 * access later (e.g. when we reload the module with
13115 * enlightened_vmcs=0)
13117 for_each_online_cpu(cpu) {
13118 vp_ap = hv_get_vp_assist_page(cpu);
13123 vp_ap->current_nested_vmcs = 0;
13124 vp_ap->enlighten_vmentry = 0;
13127 static_branch_disable(&enable_evmcs);
13132 module_init(vmx_init)
13133 module_exit(vmx_exit)