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.
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;
191 enum ept_pointers_status {
192 EPT_POINTERS_CHECK = 0,
193 EPT_POINTERS_MATCH = 1,
194 EPT_POINTERS_MISMATCH = 2
200 unsigned int tss_addr;
201 bool ept_identity_pagetable_done;
202 gpa_t ept_identity_map_addr;
204 enum ept_pointers_status ept_pointers_match;
205 spinlock_t ept_pointer_lock;
208 #define NR_AUTOLOAD_MSRS 8
222 * Track a VMCS that may be loaded on a certain CPU. If it is (cpu!=-1), also
223 * remember whether it was VMLAUNCHed, and maintain a linked list of all VMCSs
224 * loaded on this CPU (so we can clear them if the CPU goes down).
228 struct vmcs *shadow_vmcs;
231 bool nmi_known_unmasked;
232 unsigned long vmcs_host_cr3; /* May not match real cr3 */
233 unsigned long vmcs_host_cr4; /* May not match real cr4 */
234 /* Support for vnmi-less CPUs */
235 int soft_vnmi_blocked;
237 s64 vnmi_blocked_time;
238 unsigned long *msr_bitmap;
239 struct list_head loaded_vmcss_on_cpu_link;
242 struct shared_msr_entry {
249 * struct vmcs12 describes the state that our guest hypervisor (L1) keeps for a
250 * single nested guest (L2), hence the name vmcs12. Any VMX implementation has
251 * a VMCS structure, and vmcs12 is our emulated VMX's VMCS. This structure is
252 * stored in guest memory specified by VMPTRLD, but is opaque to the guest,
253 * which must access it using VMREAD/VMWRITE/VMCLEAR instructions.
254 * More than one of these structures may exist, if L1 runs multiple L2 guests.
255 * nested_vmx_run() will use the data here to build the vmcs02: a VMCS for the
256 * underlying hardware which will be used to run L2.
257 * This structure is packed to ensure that its layout is identical across
258 * machines (necessary for live migration).
260 * IMPORTANT: Changing the layout of existing fields in this structure
261 * will break save/restore compatibility with older kvm releases. When
262 * adding new fields, either use space in the reserved padding* arrays
263 * or add the new fields to the end of the structure.
265 typedef u64 natural_width;
266 struct __packed vmcs12 {
267 /* According to the Intel spec, a VMCS region must start with the
268 * following two fields. Then follow implementation-specific data.
273 u32 launch_state; /* set to 0 by VMCLEAR, to 1 by VMLAUNCH */
274 u32 padding[7]; /* room for future expansion */
279 u64 vm_exit_msr_store_addr;
280 u64 vm_exit_msr_load_addr;
281 u64 vm_entry_msr_load_addr;
283 u64 virtual_apic_page_addr;
284 u64 apic_access_addr;
285 u64 posted_intr_desc_addr;
287 u64 eoi_exit_bitmap0;
288 u64 eoi_exit_bitmap1;
289 u64 eoi_exit_bitmap2;
290 u64 eoi_exit_bitmap3;
292 u64 guest_physical_address;
293 u64 vmcs_link_pointer;
294 u64 guest_ia32_debugctl;
297 u64 guest_ia32_perf_global_ctrl;
305 u64 host_ia32_perf_global_ctrl;
308 u64 vm_function_control;
309 u64 eptp_list_address;
311 u64 padding64[3]; /* room for future expansion */
313 * To allow migration of L1 (complete with its L2 guests) between
314 * machines of different natural widths (32 or 64 bit), we cannot have
315 * unsigned long fields with no explict size. We use u64 (aliased
316 * natural_width) instead. Luckily, x86 is little-endian.
318 natural_width cr0_guest_host_mask;
319 natural_width cr4_guest_host_mask;
320 natural_width cr0_read_shadow;
321 natural_width cr4_read_shadow;
322 natural_width cr3_target_value0;
323 natural_width cr3_target_value1;
324 natural_width cr3_target_value2;
325 natural_width cr3_target_value3;
326 natural_width exit_qualification;
327 natural_width guest_linear_address;
328 natural_width guest_cr0;
329 natural_width guest_cr3;
330 natural_width guest_cr4;
331 natural_width guest_es_base;
332 natural_width guest_cs_base;
333 natural_width guest_ss_base;
334 natural_width guest_ds_base;
335 natural_width guest_fs_base;
336 natural_width guest_gs_base;
337 natural_width guest_ldtr_base;
338 natural_width guest_tr_base;
339 natural_width guest_gdtr_base;
340 natural_width guest_idtr_base;
341 natural_width guest_dr7;
342 natural_width guest_rsp;
343 natural_width guest_rip;
344 natural_width guest_rflags;
345 natural_width guest_pending_dbg_exceptions;
346 natural_width guest_sysenter_esp;
347 natural_width guest_sysenter_eip;
348 natural_width host_cr0;
349 natural_width host_cr3;
350 natural_width host_cr4;
351 natural_width host_fs_base;
352 natural_width host_gs_base;
353 natural_width host_tr_base;
354 natural_width host_gdtr_base;
355 natural_width host_idtr_base;
356 natural_width host_ia32_sysenter_esp;
357 natural_width host_ia32_sysenter_eip;
358 natural_width host_rsp;
359 natural_width host_rip;
360 natural_width paddingl[8]; /* room for future expansion */
361 u32 pin_based_vm_exec_control;
362 u32 cpu_based_vm_exec_control;
363 u32 exception_bitmap;
364 u32 page_fault_error_code_mask;
365 u32 page_fault_error_code_match;
366 u32 cr3_target_count;
367 u32 vm_exit_controls;
368 u32 vm_exit_msr_store_count;
369 u32 vm_exit_msr_load_count;
370 u32 vm_entry_controls;
371 u32 vm_entry_msr_load_count;
372 u32 vm_entry_intr_info_field;
373 u32 vm_entry_exception_error_code;
374 u32 vm_entry_instruction_len;
376 u32 secondary_vm_exec_control;
377 u32 vm_instruction_error;
379 u32 vm_exit_intr_info;
380 u32 vm_exit_intr_error_code;
381 u32 idt_vectoring_info_field;
382 u32 idt_vectoring_error_code;
383 u32 vm_exit_instruction_len;
384 u32 vmx_instruction_info;
391 u32 guest_ldtr_limit;
393 u32 guest_gdtr_limit;
394 u32 guest_idtr_limit;
395 u32 guest_es_ar_bytes;
396 u32 guest_cs_ar_bytes;
397 u32 guest_ss_ar_bytes;
398 u32 guest_ds_ar_bytes;
399 u32 guest_fs_ar_bytes;
400 u32 guest_gs_ar_bytes;
401 u32 guest_ldtr_ar_bytes;
402 u32 guest_tr_ar_bytes;
403 u32 guest_interruptibility_info;
404 u32 guest_activity_state;
405 u32 guest_sysenter_cs;
406 u32 host_ia32_sysenter_cs;
407 u32 vmx_preemption_timer_value;
408 u32 padding32[7]; /* room for future expansion */
409 u16 virtual_processor_id;
411 u16 guest_es_selector;
412 u16 guest_cs_selector;
413 u16 guest_ss_selector;
414 u16 guest_ds_selector;
415 u16 guest_fs_selector;
416 u16 guest_gs_selector;
417 u16 guest_ldtr_selector;
418 u16 guest_tr_selector;
419 u16 guest_intr_status;
420 u16 host_es_selector;
421 u16 host_cs_selector;
422 u16 host_ss_selector;
423 u16 host_ds_selector;
424 u16 host_fs_selector;
425 u16 host_gs_selector;
426 u16 host_tr_selector;
431 * For save/restore compatibility, the vmcs12 field offsets must not change.
433 #define CHECK_OFFSET(field, loc) \
434 BUILD_BUG_ON_MSG(offsetof(struct vmcs12, field) != (loc), \
435 "Offset of " #field " in struct vmcs12 has changed.")
437 static inline void vmx_check_vmcs12_offsets(void) {
438 CHECK_OFFSET(hdr, 0);
439 CHECK_OFFSET(abort, 4);
440 CHECK_OFFSET(launch_state, 8);
441 CHECK_OFFSET(io_bitmap_a, 40);
442 CHECK_OFFSET(io_bitmap_b, 48);
443 CHECK_OFFSET(msr_bitmap, 56);
444 CHECK_OFFSET(vm_exit_msr_store_addr, 64);
445 CHECK_OFFSET(vm_exit_msr_load_addr, 72);
446 CHECK_OFFSET(vm_entry_msr_load_addr, 80);
447 CHECK_OFFSET(tsc_offset, 88);
448 CHECK_OFFSET(virtual_apic_page_addr, 96);
449 CHECK_OFFSET(apic_access_addr, 104);
450 CHECK_OFFSET(posted_intr_desc_addr, 112);
451 CHECK_OFFSET(ept_pointer, 120);
452 CHECK_OFFSET(eoi_exit_bitmap0, 128);
453 CHECK_OFFSET(eoi_exit_bitmap1, 136);
454 CHECK_OFFSET(eoi_exit_bitmap2, 144);
455 CHECK_OFFSET(eoi_exit_bitmap3, 152);
456 CHECK_OFFSET(xss_exit_bitmap, 160);
457 CHECK_OFFSET(guest_physical_address, 168);
458 CHECK_OFFSET(vmcs_link_pointer, 176);
459 CHECK_OFFSET(guest_ia32_debugctl, 184);
460 CHECK_OFFSET(guest_ia32_pat, 192);
461 CHECK_OFFSET(guest_ia32_efer, 200);
462 CHECK_OFFSET(guest_ia32_perf_global_ctrl, 208);
463 CHECK_OFFSET(guest_pdptr0, 216);
464 CHECK_OFFSET(guest_pdptr1, 224);
465 CHECK_OFFSET(guest_pdptr2, 232);
466 CHECK_OFFSET(guest_pdptr3, 240);
467 CHECK_OFFSET(guest_bndcfgs, 248);
468 CHECK_OFFSET(host_ia32_pat, 256);
469 CHECK_OFFSET(host_ia32_efer, 264);
470 CHECK_OFFSET(host_ia32_perf_global_ctrl, 272);
471 CHECK_OFFSET(vmread_bitmap, 280);
472 CHECK_OFFSET(vmwrite_bitmap, 288);
473 CHECK_OFFSET(vm_function_control, 296);
474 CHECK_OFFSET(eptp_list_address, 304);
475 CHECK_OFFSET(pml_address, 312);
476 CHECK_OFFSET(cr0_guest_host_mask, 344);
477 CHECK_OFFSET(cr4_guest_host_mask, 352);
478 CHECK_OFFSET(cr0_read_shadow, 360);
479 CHECK_OFFSET(cr4_read_shadow, 368);
480 CHECK_OFFSET(cr3_target_value0, 376);
481 CHECK_OFFSET(cr3_target_value1, 384);
482 CHECK_OFFSET(cr3_target_value2, 392);
483 CHECK_OFFSET(cr3_target_value3, 400);
484 CHECK_OFFSET(exit_qualification, 408);
485 CHECK_OFFSET(guest_linear_address, 416);
486 CHECK_OFFSET(guest_cr0, 424);
487 CHECK_OFFSET(guest_cr3, 432);
488 CHECK_OFFSET(guest_cr4, 440);
489 CHECK_OFFSET(guest_es_base, 448);
490 CHECK_OFFSET(guest_cs_base, 456);
491 CHECK_OFFSET(guest_ss_base, 464);
492 CHECK_OFFSET(guest_ds_base, 472);
493 CHECK_OFFSET(guest_fs_base, 480);
494 CHECK_OFFSET(guest_gs_base, 488);
495 CHECK_OFFSET(guest_ldtr_base, 496);
496 CHECK_OFFSET(guest_tr_base, 504);
497 CHECK_OFFSET(guest_gdtr_base, 512);
498 CHECK_OFFSET(guest_idtr_base, 520);
499 CHECK_OFFSET(guest_dr7, 528);
500 CHECK_OFFSET(guest_rsp, 536);
501 CHECK_OFFSET(guest_rip, 544);
502 CHECK_OFFSET(guest_rflags, 552);
503 CHECK_OFFSET(guest_pending_dbg_exceptions, 560);
504 CHECK_OFFSET(guest_sysenter_esp, 568);
505 CHECK_OFFSET(guest_sysenter_eip, 576);
506 CHECK_OFFSET(host_cr0, 584);
507 CHECK_OFFSET(host_cr3, 592);
508 CHECK_OFFSET(host_cr4, 600);
509 CHECK_OFFSET(host_fs_base, 608);
510 CHECK_OFFSET(host_gs_base, 616);
511 CHECK_OFFSET(host_tr_base, 624);
512 CHECK_OFFSET(host_gdtr_base, 632);
513 CHECK_OFFSET(host_idtr_base, 640);
514 CHECK_OFFSET(host_ia32_sysenter_esp, 648);
515 CHECK_OFFSET(host_ia32_sysenter_eip, 656);
516 CHECK_OFFSET(host_rsp, 664);
517 CHECK_OFFSET(host_rip, 672);
518 CHECK_OFFSET(pin_based_vm_exec_control, 744);
519 CHECK_OFFSET(cpu_based_vm_exec_control, 748);
520 CHECK_OFFSET(exception_bitmap, 752);
521 CHECK_OFFSET(page_fault_error_code_mask, 756);
522 CHECK_OFFSET(page_fault_error_code_match, 760);
523 CHECK_OFFSET(cr3_target_count, 764);
524 CHECK_OFFSET(vm_exit_controls, 768);
525 CHECK_OFFSET(vm_exit_msr_store_count, 772);
526 CHECK_OFFSET(vm_exit_msr_load_count, 776);
527 CHECK_OFFSET(vm_entry_controls, 780);
528 CHECK_OFFSET(vm_entry_msr_load_count, 784);
529 CHECK_OFFSET(vm_entry_intr_info_field, 788);
530 CHECK_OFFSET(vm_entry_exception_error_code, 792);
531 CHECK_OFFSET(vm_entry_instruction_len, 796);
532 CHECK_OFFSET(tpr_threshold, 800);
533 CHECK_OFFSET(secondary_vm_exec_control, 804);
534 CHECK_OFFSET(vm_instruction_error, 808);
535 CHECK_OFFSET(vm_exit_reason, 812);
536 CHECK_OFFSET(vm_exit_intr_info, 816);
537 CHECK_OFFSET(vm_exit_intr_error_code, 820);
538 CHECK_OFFSET(idt_vectoring_info_field, 824);
539 CHECK_OFFSET(idt_vectoring_error_code, 828);
540 CHECK_OFFSET(vm_exit_instruction_len, 832);
541 CHECK_OFFSET(vmx_instruction_info, 836);
542 CHECK_OFFSET(guest_es_limit, 840);
543 CHECK_OFFSET(guest_cs_limit, 844);
544 CHECK_OFFSET(guest_ss_limit, 848);
545 CHECK_OFFSET(guest_ds_limit, 852);
546 CHECK_OFFSET(guest_fs_limit, 856);
547 CHECK_OFFSET(guest_gs_limit, 860);
548 CHECK_OFFSET(guest_ldtr_limit, 864);
549 CHECK_OFFSET(guest_tr_limit, 868);
550 CHECK_OFFSET(guest_gdtr_limit, 872);
551 CHECK_OFFSET(guest_idtr_limit, 876);
552 CHECK_OFFSET(guest_es_ar_bytes, 880);
553 CHECK_OFFSET(guest_cs_ar_bytes, 884);
554 CHECK_OFFSET(guest_ss_ar_bytes, 888);
555 CHECK_OFFSET(guest_ds_ar_bytes, 892);
556 CHECK_OFFSET(guest_fs_ar_bytes, 896);
557 CHECK_OFFSET(guest_gs_ar_bytes, 900);
558 CHECK_OFFSET(guest_ldtr_ar_bytes, 904);
559 CHECK_OFFSET(guest_tr_ar_bytes, 908);
560 CHECK_OFFSET(guest_interruptibility_info, 912);
561 CHECK_OFFSET(guest_activity_state, 916);
562 CHECK_OFFSET(guest_sysenter_cs, 920);
563 CHECK_OFFSET(host_ia32_sysenter_cs, 924);
564 CHECK_OFFSET(vmx_preemption_timer_value, 928);
565 CHECK_OFFSET(virtual_processor_id, 960);
566 CHECK_OFFSET(posted_intr_nv, 962);
567 CHECK_OFFSET(guest_es_selector, 964);
568 CHECK_OFFSET(guest_cs_selector, 966);
569 CHECK_OFFSET(guest_ss_selector, 968);
570 CHECK_OFFSET(guest_ds_selector, 970);
571 CHECK_OFFSET(guest_fs_selector, 972);
572 CHECK_OFFSET(guest_gs_selector, 974);
573 CHECK_OFFSET(guest_ldtr_selector, 976);
574 CHECK_OFFSET(guest_tr_selector, 978);
575 CHECK_OFFSET(guest_intr_status, 980);
576 CHECK_OFFSET(host_es_selector, 982);
577 CHECK_OFFSET(host_cs_selector, 984);
578 CHECK_OFFSET(host_ss_selector, 986);
579 CHECK_OFFSET(host_ds_selector, 988);
580 CHECK_OFFSET(host_fs_selector, 990);
581 CHECK_OFFSET(host_gs_selector, 992);
582 CHECK_OFFSET(host_tr_selector, 994);
583 CHECK_OFFSET(guest_pml_index, 996);
587 * VMCS12_REVISION is an arbitrary id that should be changed if the content or
588 * layout of struct vmcs12 is changed. MSR_IA32_VMX_BASIC returns this id, and
589 * VMPTRLD verifies that the VMCS region that L1 is loading contains this id.
591 * IMPORTANT: Changing this value will break save/restore compatibility with
592 * older kvm releases.
594 #define VMCS12_REVISION 0x11e57ed0
597 * VMCS12_SIZE is the number of bytes L1 should allocate for the VMXON region
598 * and any VMCS region. Although only sizeof(struct vmcs12) are used by the
599 * current implementation, 4K are reserved to avoid future complications.
601 #define VMCS12_SIZE 0x1000
604 * VMCS12_MAX_FIELD_INDEX is the highest index value used in any
605 * supported VMCS12 field encoding.
607 #define VMCS12_MAX_FIELD_INDEX 0x17
609 struct nested_vmx_msrs {
611 * We only store the "true" versions of the VMX capability MSRs. We
612 * generate the "non-true" versions by setting the must-be-1 bits
613 * according to the SDM.
615 u32 procbased_ctls_low;
616 u32 procbased_ctls_high;
617 u32 secondary_ctls_low;
618 u32 secondary_ctls_high;
619 u32 pinbased_ctls_low;
620 u32 pinbased_ctls_high;
639 * The nested_vmx structure is part of vcpu_vmx, and holds information we need
640 * for correct emulation of VMX (i.e., nested VMX) on this vcpu.
643 /* Has the level1 guest done vmxon? */
648 /* The guest-physical address of the current VMCS L1 keeps for L2 */
651 * Cache of the guest's VMCS, existing outside of guest memory.
652 * Loaded from guest memory during VMPTRLD. Flushed to guest
653 * memory during VMCLEAR and VMPTRLD.
655 struct vmcs12 *cached_vmcs12;
657 * Cache of the guest's shadow VMCS, existing outside of guest
658 * memory. Loaded from guest memory during VM entry. Flushed
659 * to guest memory during VM exit.
661 struct vmcs12 *cached_shadow_vmcs12;
663 * Indicates if the shadow vmcs must be updated with the
664 * data hold by vmcs12
666 bool sync_shadow_vmcs;
669 bool change_vmcs01_virtual_apic_mode;
671 /* L2 must run next, and mustn't decide to exit to L1. */
672 bool nested_run_pending;
674 struct loaded_vmcs vmcs02;
677 * Guest pages referred to in the vmcs02 with host-physical
678 * pointers, so we must keep them pinned while L2 runs.
680 struct page *apic_access_page;
681 struct page *virtual_apic_page;
682 struct page *pi_desc_page;
683 struct pi_desc *pi_desc;
687 struct hrtimer preemption_timer;
688 bool preemption_timer_expired;
690 /* to migrate it to L2 if VM_ENTRY_LOAD_DEBUG_CONTROLS is off */
696 struct nested_vmx_msrs msrs;
698 /* SMM related state */
700 /* in VMX operation on SMM entry? */
702 /* in guest mode on SMM entry? */
707 #define POSTED_INTR_ON 0
708 #define POSTED_INTR_SN 1
710 /* Posted-Interrupt Descriptor */
712 u32 pir[8]; /* Posted interrupt requested */
715 /* bit 256 - Outstanding Notification */
717 /* bit 257 - Suppress Notification */
719 /* bit 271:258 - Reserved */
721 /* bit 279:272 - Notification Vector */
723 /* bit 287:280 - Reserved */
725 /* bit 319:288 - Notification Destination */
733 static bool pi_test_and_set_on(struct pi_desc *pi_desc)
735 return test_and_set_bit(POSTED_INTR_ON,
736 (unsigned long *)&pi_desc->control);
739 static bool pi_test_and_clear_on(struct pi_desc *pi_desc)
741 return test_and_clear_bit(POSTED_INTR_ON,
742 (unsigned long *)&pi_desc->control);
745 static int pi_test_and_set_pir(int vector, struct pi_desc *pi_desc)
747 return test_and_set_bit(vector, (unsigned long *)pi_desc->pir);
750 static inline void pi_clear_sn(struct pi_desc *pi_desc)
752 return clear_bit(POSTED_INTR_SN,
753 (unsigned long *)&pi_desc->control);
756 static inline void pi_set_sn(struct pi_desc *pi_desc)
758 return set_bit(POSTED_INTR_SN,
759 (unsigned long *)&pi_desc->control);
762 static inline void pi_clear_on(struct pi_desc *pi_desc)
764 clear_bit(POSTED_INTR_ON,
765 (unsigned long *)&pi_desc->control);
768 static inline int pi_test_on(struct pi_desc *pi_desc)
770 return test_bit(POSTED_INTR_ON,
771 (unsigned long *)&pi_desc->control);
774 static inline int pi_test_sn(struct pi_desc *pi_desc)
776 return test_bit(POSTED_INTR_SN,
777 (unsigned long *)&pi_desc->control);
781 struct kvm_vcpu vcpu;
782 unsigned long host_rsp;
786 u32 idt_vectoring_info;
788 struct shared_msr_entry *guest_msrs;
791 unsigned long host_idt_base;
793 u64 msr_host_kernel_gs_base;
794 u64 msr_guest_kernel_gs_base;
797 u64 arch_capabilities;
800 u32 vm_entry_controls_shadow;
801 u32 vm_exit_controls_shadow;
802 u32 secondary_exec_control;
805 * loaded_vmcs points to the VMCS currently used in this vcpu. For a
806 * non-nested (L1) guest, it always points to vmcs01. For a nested
807 * guest (L2), it points to a different VMCS. loaded_cpu_state points
808 * to the VMCS whose state is loaded into the CPU registers that only
809 * need to be switched when transitioning to/from the kernel; a NULL
810 * value indicates that host state is loaded.
812 struct loaded_vmcs vmcs01;
813 struct loaded_vmcs *loaded_vmcs;
814 struct loaded_vmcs *loaded_cpu_state;
815 bool __launched; /* temporary, used in vmx_vcpu_run */
816 struct msr_autoload {
818 struct vmx_msr_entry guest[NR_AUTOLOAD_MSRS];
819 struct vmx_msr_entry host[NR_AUTOLOAD_MSRS];
823 u16 fs_sel, gs_sel, ldt_sel;
827 int gs_ldt_reload_needed;
828 int fs_reload_needed;
833 struct kvm_segment segs[8];
836 u32 bitmask; /* 4 bits per segment (1 bit per field) */
837 struct kvm_save_segment {
845 bool emulation_required;
849 /* Posted interrupt descriptor */
850 struct pi_desc pi_desc;
852 /* Support for a guest hypervisor (nested VMX) */
853 struct nested_vmx nested;
855 /* Dynamic PLE window. */
857 bool ple_window_dirty;
859 /* Support for PML */
860 #define PML_ENTITY_NUM 512
863 /* apic deadline value in host tsc */
866 u64 current_tsc_ratio;
870 unsigned long host_debugctlmsr;
873 * Only bits masked by msr_ia32_feature_control_valid_bits can be set in
874 * msr_ia32_feature_control. FEATURE_CONTROL_LOCKED is always included
875 * in msr_ia32_feature_control_valid_bits.
877 u64 msr_ia32_feature_control;
878 u64 msr_ia32_feature_control_valid_bits;
882 enum segment_cache_field {
891 static inline struct kvm_vmx *to_kvm_vmx(struct kvm *kvm)
893 return container_of(kvm, struct kvm_vmx, kvm);
896 static inline struct vcpu_vmx *to_vmx(struct kvm_vcpu *vcpu)
898 return container_of(vcpu, struct vcpu_vmx, vcpu);
901 static struct pi_desc *vcpu_to_pi_desc(struct kvm_vcpu *vcpu)
903 return &(to_vmx(vcpu)->pi_desc);
906 #define ROL16(val, n) ((u16)(((u16)(val) << (n)) | ((u16)(val) >> (16 - (n)))))
907 #define VMCS12_OFFSET(x) offsetof(struct vmcs12, x)
908 #define FIELD(number, name) [ROL16(number, 6)] = VMCS12_OFFSET(name)
909 #define FIELD64(number, name) \
910 FIELD(number, name), \
911 [ROL16(number##_HIGH, 6)] = VMCS12_OFFSET(name) + sizeof(u32)
914 static u16 shadow_read_only_fields[] = {
915 #define SHADOW_FIELD_RO(x) x,
916 #include "vmx_shadow_fields.h"
918 static int max_shadow_read_only_fields =
919 ARRAY_SIZE(shadow_read_only_fields);
921 static u16 shadow_read_write_fields[] = {
922 #define SHADOW_FIELD_RW(x) x,
923 #include "vmx_shadow_fields.h"
925 static int max_shadow_read_write_fields =
926 ARRAY_SIZE(shadow_read_write_fields);
928 static const unsigned short vmcs_field_to_offset_table[] = {
929 FIELD(VIRTUAL_PROCESSOR_ID, virtual_processor_id),
930 FIELD(POSTED_INTR_NV, posted_intr_nv),
931 FIELD(GUEST_ES_SELECTOR, guest_es_selector),
932 FIELD(GUEST_CS_SELECTOR, guest_cs_selector),
933 FIELD(GUEST_SS_SELECTOR, guest_ss_selector),
934 FIELD(GUEST_DS_SELECTOR, guest_ds_selector),
935 FIELD(GUEST_FS_SELECTOR, guest_fs_selector),
936 FIELD(GUEST_GS_SELECTOR, guest_gs_selector),
937 FIELD(GUEST_LDTR_SELECTOR, guest_ldtr_selector),
938 FIELD(GUEST_TR_SELECTOR, guest_tr_selector),
939 FIELD(GUEST_INTR_STATUS, guest_intr_status),
940 FIELD(GUEST_PML_INDEX, guest_pml_index),
941 FIELD(HOST_ES_SELECTOR, host_es_selector),
942 FIELD(HOST_CS_SELECTOR, host_cs_selector),
943 FIELD(HOST_SS_SELECTOR, host_ss_selector),
944 FIELD(HOST_DS_SELECTOR, host_ds_selector),
945 FIELD(HOST_FS_SELECTOR, host_fs_selector),
946 FIELD(HOST_GS_SELECTOR, host_gs_selector),
947 FIELD(HOST_TR_SELECTOR, host_tr_selector),
948 FIELD64(IO_BITMAP_A, io_bitmap_a),
949 FIELD64(IO_BITMAP_B, io_bitmap_b),
950 FIELD64(MSR_BITMAP, msr_bitmap),
951 FIELD64(VM_EXIT_MSR_STORE_ADDR, vm_exit_msr_store_addr),
952 FIELD64(VM_EXIT_MSR_LOAD_ADDR, vm_exit_msr_load_addr),
953 FIELD64(VM_ENTRY_MSR_LOAD_ADDR, vm_entry_msr_load_addr),
954 FIELD64(PML_ADDRESS, pml_address),
955 FIELD64(TSC_OFFSET, tsc_offset),
956 FIELD64(VIRTUAL_APIC_PAGE_ADDR, virtual_apic_page_addr),
957 FIELD64(APIC_ACCESS_ADDR, apic_access_addr),
958 FIELD64(POSTED_INTR_DESC_ADDR, posted_intr_desc_addr),
959 FIELD64(VM_FUNCTION_CONTROL, vm_function_control),
960 FIELD64(EPT_POINTER, ept_pointer),
961 FIELD64(EOI_EXIT_BITMAP0, eoi_exit_bitmap0),
962 FIELD64(EOI_EXIT_BITMAP1, eoi_exit_bitmap1),
963 FIELD64(EOI_EXIT_BITMAP2, eoi_exit_bitmap2),
964 FIELD64(EOI_EXIT_BITMAP3, eoi_exit_bitmap3),
965 FIELD64(EPTP_LIST_ADDRESS, eptp_list_address),
966 FIELD64(VMREAD_BITMAP, vmread_bitmap),
967 FIELD64(VMWRITE_BITMAP, vmwrite_bitmap),
968 FIELD64(XSS_EXIT_BITMAP, xss_exit_bitmap),
969 FIELD64(GUEST_PHYSICAL_ADDRESS, guest_physical_address),
970 FIELD64(VMCS_LINK_POINTER, vmcs_link_pointer),
971 FIELD64(GUEST_IA32_DEBUGCTL, guest_ia32_debugctl),
972 FIELD64(GUEST_IA32_PAT, guest_ia32_pat),
973 FIELD64(GUEST_IA32_EFER, guest_ia32_efer),
974 FIELD64(GUEST_IA32_PERF_GLOBAL_CTRL, guest_ia32_perf_global_ctrl),
975 FIELD64(GUEST_PDPTR0, guest_pdptr0),
976 FIELD64(GUEST_PDPTR1, guest_pdptr1),
977 FIELD64(GUEST_PDPTR2, guest_pdptr2),
978 FIELD64(GUEST_PDPTR3, guest_pdptr3),
979 FIELD64(GUEST_BNDCFGS, guest_bndcfgs),
980 FIELD64(HOST_IA32_PAT, host_ia32_pat),
981 FIELD64(HOST_IA32_EFER, host_ia32_efer),
982 FIELD64(HOST_IA32_PERF_GLOBAL_CTRL, host_ia32_perf_global_ctrl),
983 FIELD(PIN_BASED_VM_EXEC_CONTROL, pin_based_vm_exec_control),
984 FIELD(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control),
985 FIELD(EXCEPTION_BITMAP, exception_bitmap),
986 FIELD(PAGE_FAULT_ERROR_CODE_MASK, page_fault_error_code_mask),
987 FIELD(PAGE_FAULT_ERROR_CODE_MATCH, page_fault_error_code_match),
988 FIELD(CR3_TARGET_COUNT, cr3_target_count),
989 FIELD(VM_EXIT_CONTROLS, vm_exit_controls),
990 FIELD(VM_EXIT_MSR_STORE_COUNT, vm_exit_msr_store_count),
991 FIELD(VM_EXIT_MSR_LOAD_COUNT, vm_exit_msr_load_count),
992 FIELD(VM_ENTRY_CONTROLS, vm_entry_controls),
993 FIELD(VM_ENTRY_MSR_LOAD_COUNT, vm_entry_msr_load_count),
994 FIELD(VM_ENTRY_INTR_INFO_FIELD, vm_entry_intr_info_field),
995 FIELD(VM_ENTRY_EXCEPTION_ERROR_CODE, vm_entry_exception_error_code),
996 FIELD(VM_ENTRY_INSTRUCTION_LEN, vm_entry_instruction_len),
997 FIELD(TPR_THRESHOLD, tpr_threshold),
998 FIELD(SECONDARY_VM_EXEC_CONTROL, secondary_vm_exec_control),
999 FIELD(VM_INSTRUCTION_ERROR, vm_instruction_error),
1000 FIELD(VM_EXIT_REASON, vm_exit_reason),
1001 FIELD(VM_EXIT_INTR_INFO, vm_exit_intr_info),
1002 FIELD(VM_EXIT_INTR_ERROR_CODE, vm_exit_intr_error_code),
1003 FIELD(IDT_VECTORING_INFO_FIELD, idt_vectoring_info_field),
1004 FIELD(IDT_VECTORING_ERROR_CODE, idt_vectoring_error_code),
1005 FIELD(VM_EXIT_INSTRUCTION_LEN, vm_exit_instruction_len),
1006 FIELD(VMX_INSTRUCTION_INFO, vmx_instruction_info),
1007 FIELD(GUEST_ES_LIMIT, guest_es_limit),
1008 FIELD(GUEST_CS_LIMIT, guest_cs_limit),
1009 FIELD(GUEST_SS_LIMIT, guest_ss_limit),
1010 FIELD(GUEST_DS_LIMIT, guest_ds_limit),
1011 FIELD(GUEST_FS_LIMIT, guest_fs_limit),
1012 FIELD(GUEST_GS_LIMIT, guest_gs_limit),
1013 FIELD(GUEST_LDTR_LIMIT, guest_ldtr_limit),
1014 FIELD(GUEST_TR_LIMIT, guest_tr_limit),
1015 FIELD(GUEST_GDTR_LIMIT, guest_gdtr_limit),
1016 FIELD(GUEST_IDTR_LIMIT, guest_idtr_limit),
1017 FIELD(GUEST_ES_AR_BYTES, guest_es_ar_bytes),
1018 FIELD(GUEST_CS_AR_BYTES, guest_cs_ar_bytes),
1019 FIELD(GUEST_SS_AR_BYTES, guest_ss_ar_bytes),
1020 FIELD(GUEST_DS_AR_BYTES, guest_ds_ar_bytes),
1021 FIELD(GUEST_FS_AR_BYTES, guest_fs_ar_bytes),
1022 FIELD(GUEST_GS_AR_BYTES, guest_gs_ar_bytes),
1023 FIELD(GUEST_LDTR_AR_BYTES, guest_ldtr_ar_bytes),
1024 FIELD(GUEST_TR_AR_BYTES, guest_tr_ar_bytes),
1025 FIELD(GUEST_INTERRUPTIBILITY_INFO, guest_interruptibility_info),
1026 FIELD(GUEST_ACTIVITY_STATE, guest_activity_state),
1027 FIELD(GUEST_SYSENTER_CS, guest_sysenter_cs),
1028 FIELD(HOST_IA32_SYSENTER_CS, host_ia32_sysenter_cs),
1029 FIELD(VMX_PREEMPTION_TIMER_VALUE, vmx_preemption_timer_value),
1030 FIELD(CR0_GUEST_HOST_MASK, cr0_guest_host_mask),
1031 FIELD(CR4_GUEST_HOST_MASK, cr4_guest_host_mask),
1032 FIELD(CR0_READ_SHADOW, cr0_read_shadow),
1033 FIELD(CR4_READ_SHADOW, cr4_read_shadow),
1034 FIELD(CR3_TARGET_VALUE0, cr3_target_value0),
1035 FIELD(CR3_TARGET_VALUE1, cr3_target_value1),
1036 FIELD(CR3_TARGET_VALUE2, cr3_target_value2),
1037 FIELD(CR3_TARGET_VALUE3, cr3_target_value3),
1038 FIELD(EXIT_QUALIFICATION, exit_qualification),
1039 FIELD(GUEST_LINEAR_ADDRESS, guest_linear_address),
1040 FIELD(GUEST_CR0, guest_cr0),
1041 FIELD(GUEST_CR3, guest_cr3),
1042 FIELD(GUEST_CR4, guest_cr4),
1043 FIELD(GUEST_ES_BASE, guest_es_base),
1044 FIELD(GUEST_CS_BASE, guest_cs_base),
1045 FIELD(GUEST_SS_BASE, guest_ss_base),
1046 FIELD(GUEST_DS_BASE, guest_ds_base),
1047 FIELD(GUEST_FS_BASE, guest_fs_base),
1048 FIELD(GUEST_GS_BASE, guest_gs_base),
1049 FIELD(GUEST_LDTR_BASE, guest_ldtr_base),
1050 FIELD(GUEST_TR_BASE, guest_tr_base),
1051 FIELD(GUEST_GDTR_BASE, guest_gdtr_base),
1052 FIELD(GUEST_IDTR_BASE, guest_idtr_base),
1053 FIELD(GUEST_DR7, guest_dr7),
1054 FIELD(GUEST_RSP, guest_rsp),
1055 FIELD(GUEST_RIP, guest_rip),
1056 FIELD(GUEST_RFLAGS, guest_rflags),
1057 FIELD(GUEST_PENDING_DBG_EXCEPTIONS, guest_pending_dbg_exceptions),
1058 FIELD(GUEST_SYSENTER_ESP, guest_sysenter_esp),
1059 FIELD(GUEST_SYSENTER_EIP, guest_sysenter_eip),
1060 FIELD(HOST_CR0, host_cr0),
1061 FIELD(HOST_CR3, host_cr3),
1062 FIELD(HOST_CR4, host_cr4),
1063 FIELD(HOST_FS_BASE, host_fs_base),
1064 FIELD(HOST_GS_BASE, host_gs_base),
1065 FIELD(HOST_TR_BASE, host_tr_base),
1066 FIELD(HOST_GDTR_BASE, host_gdtr_base),
1067 FIELD(HOST_IDTR_BASE, host_idtr_base),
1068 FIELD(HOST_IA32_SYSENTER_ESP, host_ia32_sysenter_esp),
1069 FIELD(HOST_IA32_SYSENTER_EIP, host_ia32_sysenter_eip),
1070 FIELD(HOST_RSP, host_rsp),
1071 FIELD(HOST_RIP, host_rip),
1074 static inline short vmcs_field_to_offset(unsigned long field)
1076 const size_t size = ARRAY_SIZE(vmcs_field_to_offset_table);
1077 unsigned short offset;
1083 index = ROL16(field, 6);
1087 index = array_index_nospec(index, size);
1088 offset = vmcs_field_to_offset_table[index];
1094 static inline struct vmcs12 *get_vmcs12(struct kvm_vcpu *vcpu)
1096 return to_vmx(vcpu)->nested.cached_vmcs12;
1099 static inline struct vmcs12 *get_shadow_vmcs12(struct kvm_vcpu *vcpu)
1101 return to_vmx(vcpu)->nested.cached_shadow_vmcs12;
1104 static bool nested_ept_ad_enabled(struct kvm_vcpu *vcpu);
1105 static unsigned long nested_ept_get_cr3(struct kvm_vcpu *vcpu);
1106 static u64 construct_eptp(struct kvm_vcpu *vcpu, unsigned long root_hpa);
1107 static bool vmx_xsaves_supported(void);
1108 static void vmx_set_segment(struct kvm_vcpu *vcpu,
1109 struct kvm_segment *var, int seg);
1110 static void vmx_get_segment(struct kvm_vcpu *vcpu,
1111 struct kvm_segment *var, int seg);
1112 static bool guest_state_valid(struct kvm_vcpu *vcpu);
1113 static u32 vmx_segment_access_rights(struct kvm_segment *var);
1114 static void copy_shadow_to_vmcs12(struct vcpu_vmx *vmx);
1115 static bool vmx_get_nmi_mask(struct kvm_vcpu *vcpu);
1116 static void vmx_set_nmi_mask(struct kvm_vcpu *vcpu, bool masked);
1117 static bool nested_vmx_is_page_fault_vmexit(struct vmcs12 *vmcs12,
1119 static void vmx_update_msr_bitmap(struct kvm_vcpu *vcpu);
1120 static void __always_inline vmx_disable_intercept_for_msr(unsigned long *msr_bitmap,
1123 static DEFINE_PER_CPU(struct vmcs *, vmxarea);
1124 static DEFINE_PER_CPU(struct vmcs *, current_vmcs);
1126 * We maintain a per-CPU linked-list of VMCS loaded on that CPU. This is needed
1127 * when a CPU is brought down, and we need to VMCLEAR all VMCSs loaded on it.
1129 static DEFINE_PER_CPU(struct list_head, loaded_vmcss_on_cpu);
1132 * We maintian a per-CPU linked-list of vCPU, so in wakeup_handler() we
1133 * can find which vCPU should be waken up.
1135 static DEFINE_PER_CPU(struct list_head, blocked_vcpu_on_cpu);
1136 static DEFINE_PER_CPU(spinlock_t, blocked_vcpu_on_cpu_lock);
1144 static unsigned long *vmx_bitmap[VMX_BITMAP_NR];
1146 #define vmx_vmread_bitmap (vmx_bitmap[VMX_VMREAD_BITMAP])
1147 #define vmx_vmwrite_bitmap (vmx_bitmap[VMX_VMWRITE_BITMAP])
1149 static bool cpu_has_load_ia32_efer;
1150 static bool cpu_has_load_perf_global_ctrl;
1152 static DECLARE_BITMAP(vmx_vpid_bitmap, VMX_NR_VPIDS);
1153 static DEFINE_SPINLOCK(vmx_vpid_lock);
1155 static struct vmcs_config {
1160 u32 pin_based_exec_ctrl;
1161 u32 cpu_based_exec_ctrl;
1162 u32 cpu_based_2nd_exec_ctrl;
1165 struct nested_vmx_msrs nested;
1168 static struct vmx_capability {
1173 #define VMX_SEGMENT_FIELD(seg) \
1174 [VCPU_SREG_##seg] = { \
1175 .selector = GUEST_##seg##_SELECTOR, \
1176 .base = GUEST_##seg##_BASE, \
1177 .limit = GUEST_##seg##_LIMIT, \
1178 .ar_bytes = GUEST_##seg##_AR_BYTES, \
1181 static const struct kvm_vmx_segment_field {
1186 } kvm_vmx_segment_fields[] = {
1187 VMX_SEGMENT_FIELD(CS),
1188 VMX_SEGMENT_FIELD(DS),
1189 VMX_SEGMENT_FIELD(ES),
1190 VMX_SEGMENT_FIELD(FS),
1191 VMX_SEGMENT_FIELD(GS),
1192 VMX_SEGMENT_FIELD(SS),
1193 VMX_SEGMENT_FIELD(TR),
1194 VMX_SEGMENT_FIELD(LDTR),
1197 static u64 host_efer;
1199 static void ept_save_pdptrs(struct kvm_vcpu *vcpu);
1202 * Keep MSR_STAR at the end, as setup_msrs() will try to optimize it
1203 * away by decrementing the array size.
1205 static const u32 vmx_msr_index[] = {
1206 #ifdef CONFIG_X86_64
1207 MSR_SYSCALL_MASK, MSR_LSTAR, MSR_CSTAR,
1209 MSR_EFER, MSR_TSC_AUX, MSR_STAR,
1212 DEFINE_STATIC_KEY_FALSE(enable_evmcs);
1214 #define current_evmcs ((struct hv_enlightened_vmcs *)this_cpu_read(current_vmcs))
1216 #define KVM_EVMCS_VERSION 1
1218 #if IS_ENABLED(CONFIG_HYPERV)
1219 static bool __read_mostly enlightened_vmcs = true;
1220 module_param(enlightened_vmcs, bool, 0444);
1222 static inline void evmcs_write64(unsigned long field, u64 value)
1225 int offset = get_evmcs_offset(field, &clean_field);
1230 *(u64 *)((char *)current_evmcs + offset) = value;
1232 current_evmcs->hv_clean_fields &= ~clean_field;
1235 static inline void evmcs_write32(unsigned long field, u32 value)
1238 int offset = get_evmcs_offset(field, &clean_field);
1243 *(u32 *)((char *)current_evmcs + offset) = value;
1244 current_evmcs->hv_clean_fields &= ~clean_field;
1247 static inline void evmcs_write16(unsigned long field, u16 value)
1250 int offset = get_evmcs_offset(field, &clean_field);
1255 *(u16 *)((char *)current_evmcs + offset) = value;
1256 current_evmcs->hv_clean_fields &= ~clean_field;
1259 static inline u64 evmcs_read64(unsigned long field)
1261 int offset = get_evmcs_offset(field, NULL);
1266 return *(u64 *)((char *)current_evmcs + offset);
1269 static inline u32 evmcs_read32(unsigned long field)
1271 int offset = get_evmcs_offset(field, NULL);
1276 return *(u32 *)((char *)current_evmcs + offset);
1279 static inline u16 evmcs_read16(unsigned long field)
1281 int offset = get_evmcs_offset(field, NULL);
1286 return *(u16 *)((char *)current_evmcs + offset);
1289 static inline void evmcs_touch_msr_bitmap(void)
1291 if (unlikely(!current_evmcs))
1294 if (current_evmcs->hv_enlightenments_control.msr_bitmap)
1295 current_evmcs->hv_clean_fields &=
1296 ~HV_VMX_ENLIGHTENED_CLEAN_FIELD_MSR_BITMAP;
1299 static void evmcs_load(u64 phys_addr)
1301 struct hv_vp_assist_page *vp_ap =
1302 hv_get_vp_assist_page(smp_processor_id());
1304 vp_ap->current_nested_vmcs = phys_addr;
1305 vp_ap->enlighten_vmentry = 1;
1308 static void evmcs_sanitize_exec_ctrls(struct vmcs_config *vmcs_conf)
1311 * Enlightened VMCSv1 doesn't support these:
1313 * POSTED_INTR_NV = 0x00000002,
1314 * GUEST_INTR_STATUS = 0x00000810,
1315 * APIC_ACCESS_ADDR = 0x00002014,
1316 * POSTED_INTR_DESC_ADDR = 0x00002016,
1317 * EOI_EXIT_BITMAP0 = 0x0000201c,
1318 * EOI_EXIT_BITMAP1 = 0x0000201e,
1319 * EOI_EXIT_BITMAP2 = 0x00002020,
1320 * EOI_EXIT_BITMAP3 = 0x00002022,
1322 vmcs_conf->pin_based_exec_ctrl &= ~PIN_BASED_POSTED_INTR;
1323 vmcs_conf->cpu_based_2nd_exec_ctrl &=
1324 ~SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY;
1325 vmcs_conf->cpu_based_2nd_exec_ctrl &=
1326 ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
1327 vmcs_conf->cpu_based_2nd_exec_ctrl &=
1328 ~SECONDARY_EXEC_APIC_REGISTER_VIRT;
1331 * GUEST_PML_INDEX = 0x00000812,
1332 * PML_ADDRESS = 0x0000200e,
1334 vmcs_conf->cpu_based_2nd_exec_ctrl &= ~SECONDARY_EXEC_ENABLE_PML;
1336 /* VM_FUNCTION_CONTROL = 0x00002018, */
1337 vmcs_conf->cpu_based_2nd_exec_ctrl &= ~SECONDARY_EXEC_ENABLE_VMFUNC;
1340 * EPTP_LIST_ADDRESS = 0x00002024,
1341 * VMREAD_BITMAP = 0x00002026,
1342 * VMWRITE_BITMAP = 0x00002028,
1344 vmcs_conf->cpu_based_2nd_exec_ctrl &= ~SECONDARY_EXEC_SHADOW_VMCS;
1347 * TSC_MULTIPLIER = 0x00002032,
1349 vmcs_conf->cpu_based_2nd_exec_ctrl &= ~SECONDARY_EXEC_TSC_SCALING;
1352 * PLE_GAP = 0x00004020,
1353 * PLE_WINDOW = 0x00004022,
1355 vmcs_conf->cpu_based_2nd_exec_ctrl &= ~SECONDARY_EXEC_PAUSE_LOOP_EXITING;
1358 * VMX_PREEMPTION_TIMER_VALUE = 0x0000482E,
1360 vmcs_conf->pin_based_exec_ctrl &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
1363 * GUEST_IA32_PERF_GLOBAL_CTRL = 0x00002808,
1364 * HOST_IA32_PERF_GLOBAL_CTRL = 0x00002c04,
1366 vmcs_conf->vmexit_ctrl &= ~VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL;
1367 vmcs_conf->vmentry_ctrl &= ~VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL;
1370 * Currently unsupported in KVM:
1371 * GUEST_IA32_RTIT_CTL = 0x00002814,
1375 /* check_ept_pointer() should be under protection of ept_pointer_lock. */
1376 static void check_ept_pointer_match(struct kvm *kvm)
1378 struct kvm_vcpu *vcpu;
1379 u64 tmp_eptp = INVALID_PAGE;
1382 kvm_for_each_vcpu(i, vcpu, kvm) {
1383 if (!VALID_PAGE(tmp_eptp)) {
1384 tmp_eptp = to_vmx(vcpu)->ept_pointer;
1385 } else if (tmp_eptp != to_vmx(vcpu)->ept_pointer) {
1386 to_kvm_vmx(kvm)->ept_pointers_match
1387 = EPT_POINTERS_MISMATCH;
1392 to_kvm_vmx(kvm)->ept_pointers_match = EPT_POINTERS_MATCH;
1395 static int vmx_hv_remote_flush_tlb(struct kvm *kvm)
1399 spin_lock(&to_kvm_vmx(kvm)->ept_pointer_lock);
1401 if (to_kvm_vmx(kvm)->ept_pointers_match == EPT_POINTERS_CHECK)
1402 check_ept_pointer_match(kvm);
1404 if (to_kvm_vmx(kvm)->ept_pointers_match != EPT_POINTERS_MATCH) {
1409 ret = hyperv_flush_guest_mapping(
1410 to_vmx(kvm_get_vcpu(kvm, 0))->ept_pointer);
1413 spin_unlock(&to_kvm_vmx(kvm)->ept_pointer_lock);
1416 #else /* !IS_ENABLED(CONFIG_HYPERV) */
1417 static inline void evmcs_write64(unsigned long field, u64 value) {}
1418 static inline void evmcs_write32(unsigned long field, u32 value) {}
1419 static inline void evmcs_write16(unsigned long field, u16 value) {}
1420 static inline u64 evmcs_read64(unsigned long field) { return 0; }
1421 static inline u32 evmcs_read32(unsigned long field) { return 0; }
1422 static inline u16 evmcs_read16(unsigned long field) { return 0; }
1423 static inline void evmcs_load(u64 phys_addr) {}
1424 static inline void evmcs_sanitize_exec_ctrls(struct vmcs_config *vmcs_conf) {}
1425 static inline void evmcs_touch_msr_bitmap(void) {}
1426 #endif /* IS_ENABLED(CONFIG_HYPERV) */
1428 static inline bool is_exception_n(u32 intr_info, u8 vector)
1430 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
1431 INTR_INFO_VALID_MASK)) ==
1432 (INTR_TYPE_HARD_EXCEPTION | vector | INTR_INFO_VALID_MASK);
1435 static inline bool is_debug(u32 intr_info)
1437 return is_exception_n(intr_info, DB_VECTOR);
1440 static inline bool is_breakpoint(u32 intr_info)
1442 return is_exception_n(intr_info, BP_VECTOR);
1445 static inline bool is_page_fault(u32 intr_info)
1447 return is_exception_n(intr_info, PF_VECTOR);
1450 static inline bool is_no_device(u32 intr_info)
1452 return is_exception_n(intr_info, NM_VECTOR);
1455 static inline bool is_invalid_opcode(u32 intr_info)
1457 return is_exception_n(intr_info, UD_VECTOR);
1460 static inline bool is_gp_fault(u32 intr_info)
1462 return is_exception_n(intr_info, GP_VECTOR);
1465 static inline bool is_external_interrupt(u32 intr_info)
1467 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK))
1468 == (INTR_TYPE_EXT_INTR | INTR_INFO_VALID_MASK);
1471 static inline bool is_machine_check(u32 intr_info)
1473 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
1474 INTR_INFO_VALID_MASK)) ==
1475 (INTR_TYPE_HARD_EXCEPTION | MC_VECTOR | INTR_INFO_VALID_MASK);
1478 /* Undocumented: icebp/int1 */
1479 static inline bool is_icebp(u32 intr_info)
1481 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK))
1482 == (INTR_TYPE_PRIV_SW_EXCEPTION | INTR_INFO_VALID_MASK);
1485 static inline bool cpu_has_vmx_msr_bitmap(void)
1487 return vmcs_config.cpu_based_exec_ctrl & CPU_BASED_USE_MSR_BITMAPS;
1490 static inline bool cpu_has_vmx_tpr_shadow(void)
1492 return vmcs_config.cpu_based_exec_ctrl & CPU_BASED_TPR_SHADOW;
1495 static inline bool cpu_need_tpr_shadow(struct kvm_vcpu *vcpu)
1497 return cpu_has_vmx_tpr_shadow() && lapic_in_kernel(vcpu);
1500 static inline bool cpu_has_secondary_exec_ctrls(void)
1502 return vmcs_config.cpu_based_exec_ctrl &
1503 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
1506 static inline bool cpu_has_vmx_virtualize_apic_accesses(void)
1508 return vmcs_config.cpu_based_2nd_exec_ctrl &
1509 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
1512 static inline bool cpu_has_vmx_virtualize_x2apic_mode(void)
1514 return vmcs_config.cpu_based_2nd_exec_ctrl &
1515 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
1518 static inline bool cpu_has_vmx_apic_register_virt(void)
1520 return vmcs_config.cpu_based_2nd_exec_ctrl &
1521 SECONDARY_EXEC_APIC_REGISTER_VIRT;
1524 static inline bool cpu_has_vmx_virtual_intr_delivery(void)
1526 return vmcs_config.cpu_based_2nd_exec_ctrl &
1527 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY;
1531 * Comment's format: document - errata name - stepping - processor name.
1533 * https://www.virtualbox.org/svn/vbox/trunk/src/VBox/VMM/VMMR0/HMR0.cpp
1535 static u32 vmx_preemption_cpu_tfms[] = {
1536 /* 323344.pdf - BA86 - D0 - Xeon 7500 Series */
1538 /* 323056.pdf - AAX65 - C2 - Xeon L3406 */
1539 /* 322814.pdf - AAT59 - C2 - i7-600, i5-500, i5-400 and i3-300 Mobile */
1540 /* 322911.pdf - AAU65 - C2 - i5-600, i3-500 Desktop and Pentium G6950 */
1542 /* 322911.pdf - AAU65 - K0 - i5-600, i3-500 Desktop and Pentium G6950 */
1544 /* 322373.pdf - AAO95 - B1 - Xeon 3400 Series */
1545 /* 322166.pdf - AAN92 - B1 - i7-800 and i5-700 Desktop */
1547 * 320767.pdf - AAP86 - B1 -
1548 * i7-900 Mobile Extreme, i7-800 and i7-700 Mobile
1551 /* 321333.pdf - AAM126 - C0 - Xeon 3500 */
1553 /* 321333.pdf - AAM126 - C1 - Xeon 3500 */
1555 /* 320836.pdf - AAJ124 - C0 - i7-900 Desktop Extreme and i7-900 Desktop */
1557 /* 321333.pdf - AAM126 - D0 - Xeon 3500 */
1558 /* 321324.pdf - AAK139 - D0 - Xeon 5500 */
1559 /* 320836.pdf - AAJ124 - D0 - i7-900 Extreme and i7-900 Desktop */
1563 static inline bool cpu_has_broken_vmx_preemption_timer(void)
1565 u32 eax = cpuid_eax(0x00000001), i;
1567 /* Clear the reserved bits */
1568 eax &= ~(0x3U << 14 | 0xfU << 28);
1569 for (i = 0; i < ARRAY_SIZE(vmx_preemption_cpu_tfms); i++)
1570 if (eax == vmx_preemption_cpu_tfms[i])
1576 static inline bool cpu_has_vmx_preemption_timer(void)
1578 return vmcs_config.pin_based_exec_ctrl &
1579 PIN_BASED_VMX_PREEMPTION_TIMER;
1582 static inline bool cpu_has_vmx_posted_intr(void)
1584 return IS_ENABLED(CONFIG_X86_LOCAL_APIC) &&
1585 vmcs_config.pin_based_exec_ctrl & PIN_BASED_POSTED_INTR;
1588 static inline bool cpu_has_vmx_apicv(void)
1590 return cpu_has_vmx_apic_register_virt() &&
1591 cpu_has_vmx_virtual_intr_delivery() &&
1592 cpu_has_vmx_posted_intr();
1595 static inline bool cpu_has_vmx_flexpriority(void)
1597 return cpu_has_vmx_tpr_shadow() &&
1598 cpu_has_vmx_virtualize_apic_accesses();
1601 static inline bool cpu_has_vmx_ept_execute_only(void)
1603 return vmx_capability.ept & VMX_EPT_EXECUTE_ONLY_BIT;
1606 static inline bool cpu_has_vmx_ept_2m_page(void)
1608 return vmx_capability.ept & VMX_EPT_2MB_PAGE_BIT;
1611 static inline bool cpu_has_vmx_ept_1g_page(void)
1613 return vmx_capability.ept & VMX_EPT_1GB_PAGE_BIT;
1616 static inline bool cpu_has_vmx_ept_4levels(void)
1618 return vmx_capability.ept & VMX_EPT_PAGE_WALK_4_BIT;
1621 static inline bool cpu_has_vmx_ept_mt_wb(void)
1623 return vmx_capability.ept & VMX_EPTP_WB_BIT;
1626 static inline bool cpu_has_vmx_ept_5levels(void)
1628 return vmx_capability.ept & VMX_EPT_PAGE_WALK_5_BIT;
1631 static inline bool cpu_has_vmx_ept_ad_bits(void)
1633 return vmx_capability.ept & VMX_EPT_AD_BIT;
1636 static inline bool cpu_has_vmx_invept_context(void)
1638 return vmx_capability.ept & VMX_EPT_EXTENT_CONTEXT_BIT;
1641 static inline bool cpu_has_vmx_invept_global(void)
1643 return vmx_capability.ept & VMX_EPT_EXTENT_GLOBAL_BIT;
1646 static inline bool cpu_has_vmx_invvpid_individual_addr(void)
1648 return vmx_capability.vpid & VMX_VPID_EXTENT_INDIVIDUAL_ADDR_BIT;
1651 static inline bool cpu_has_vmx_invvpid_single(void)
1653 return vmx_capability.vpid & VMX_VPID_EXTENT_SINGLE_CONTEXT_BIT;
1656 static inline bool cpu_has_vmx_invvpid_global(void)
1658 return vmx_capability.vpid & VMX_VPID_EXTENT_GLOBAL_CONTEXT_BIT;
1661 static inline bool cpu_has_vmx_invvpid(void)
1663 return vmx_capability.vpid & VMX_VPID_INVVPID_BIT;
1666 static inline bool cpu_has_vmx_ept(void)
1668 return vmcs_config.cpu_based_2nd_exec_ctrl &
1669 SECONDARY_EXEC_ENABLE_EPT;
1672 static inline bool cpu_has_vmx_unrestricted_guest(void)
1674 return vmcs_config.cpu_based_2nd_exec_ctrl &
1675 SECONDARY_EXEC_UNRESTRICTED_GUEST;
1678 static inline bool cpu_has_vmx_ple(void)
1680 return vmcs_config.cpu_based_2nd_exec_ctrl &
1681 SECONDARY_EXEC_PAUSE_LOOP_EXITING;
1684 static inline bool cpu_has_vmx_basic_inout(void)
1686 return (((u64)vmcs_config.basic_cap << 32) & VMX_BASIC_INOUT);
1689 static inline bool cpu_need_virtualize_apic_accesses(struct kvm_vcpu *vcpu)
1691 return flexpriority_enabled && lapic_in_kernel(vcpu);
1694 static inline bool cpu_has_vmx_vpid(void)
1696 return vmcs_config.cpu_based_2nd_exec_ctrl &
1697 SECONDARY_EXEC_ENABLE_VPID;
1700 static inline bool cpu_has_vmx_rdtscp(void)
1702 return vmcs_config.cpu_based_2nd_exec_ctrl &
1703 SECONDARY_EXEC_RDTSCP;
1706 static inline bool cpu_has_vmx_invpcid(void)
1708 return vmcs_config.cpu_based_2nd_exec_ctrl &
1709 SECONDARY_EXEC_ENABLE_INVPCID;
1712 static inline bool cpu_has_virtual_nmis(void)
1714 return vmcs_config.pin_based_exec_ctrl & PIN_BASED_VIRTUAL_NMIS;
1717 static inline bool cpu_has_vmx_wbinvd_exit(void)
1719 return vmcs_config.cpu_based_2nd_exec_ctrl &
1720 SECONDARY_EXEC_WBINVD_EXITING;
1723 static inline bool cpu_has_vmx_shadow_vmcs(void)
1726 rdmsrl(MSR_IA32_VMX_MISC, vmx_msr);
1727 /* check if the cpu supports writing r/o exit information fields */
1728 if (!(vmx_msr & MSR_IA32_VMX_MISC_VMWRITE_SHADOW_RO_FIELDS))
1731 return vmcs_config.cpu_based_2nd_exec_ctrl &
1732 SECONDARY_EXEC_SHADOW_VMCS;
1735 static inline bool cpu_has_vmx_pml(void)
1737 return vmcs_config.cpu_based_2nd_exec_ctrl & SECONDARY_EXEC_ENABLE_PML;
1740 static inline bool cpu_has_vmx_tsc_scaling(void)
1742 return vmcs_config.cpu_based_2nd_exec_ctrl &
1743 SECONDARY_EXEC_TSC_SCALING;
1746 static inline bool cpu_has_vmx_vmfunc(void)
1748 return vmcs_config.cpu_based_2nd_exec_ctrl &
1749 SECONDARY_EXEC_ENABLE_VMFUNC;
1752 static bool vmx_umip_emulated(void)
1754 return vmcs_config.cpu_based_2nd_exec_ctrl &
1755 SECONDARY_EXEC_DESC;
1758 static inline bool report_flexpriority(void)
1760 return flexpriority_enabled;
1763 static inline unsigned nested_cpu_vmx_misc_cr3_count(struct kvm_vcpu *vcpu)
1765 return vmx_misc_cr3_count(to_vmx(vcpu)->nested.msrs.misc_low);
1769 * Do the virtual VMX capability MSRs specify that L1 can use VMWRITE
1770 * to modify any valid field of the VMCS, or are the VM-exit
1771 * information fields read-only?
1773 static inline bool nested_cpu_has_vmwrite_any_field(struct kvm_vcpu *vcpu)
1775 return to_vmx(vcpu)->nested.msrs.misc_low &
1776 MSR_IA32_VMX_MISC_VMWRITE_SHADOW_RO_FIELDS;
1779 static inline bool nested_cpu_has_zero_length_injection(struct kvm_vcpu *vcpu)
1781 return to_vmx(vcpu)->nested.msrs.misc_low & VMX_MISC_ZERO_LEN_INS;
1784 static inline bool nested_cpu_supports_monitor_trap_flag(struct kvm_vcpu *vcpu)
1786 return to_vmx(vcpu)->nested.msrs.procbased_ctls_high &
1787 CPU_BASED_MONITOR_TRAP_FLAG;
1790 static inline bool nested_cpu_has_vmx_shadow_vmcs(struct kvm_vcpu *vcpu)
1792 return to_vmx(vcpu)->nested.msrs.secondary_ctls_high &
1793 SECONDARY_EXEC_SHADOW_VMCS;
1796 static inline bool nested_cpu_has(struct vmcs12 *vmcs12, u32 bit)
1798 return vmcs12->cpu_based_vm_exec_control & bit;
1801 static inline bool nested_cpu_has2(struct vmcs12 *vmcs12, u32 bit)
1803 return (vmcs12->cpu_based_vm_exec_control &
1804 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) &&
1805 (vmcs12->secondary_vm_exec_control & bit);
1808 static inline bool nested_cpu_has_preemption_timer(struct vmcs12 *vmcs12)
1810 return vmcs12->pin_based_vm_exec_control &
1811 PIN_BASED_VMX_PREEMPTION_TIMER;
1814 static inline bool nested_cpu_has_nmi_exiting(struct vmcs12 *vmcs12)
1816 return vmcs12->pin_based_vm_exec_control & PIN_BASED_NMI_EXITING;
1819 static inline bool nested_cpu_has_virtual_nmis(struct vmcs12 *vmcs12)
1821 return vmcs12->pin_based_vm_exec_control & PIN_BASED_VIRTUAL_NMIS;
1824 static inline int nested_cpu_has_ept(struct vmcs12 *vmcs12)
1826 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_EPT);
1829 static inline bool nested_cpu_has_xsaves(struct vmcs12 *vmcs12)
1831 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_XSAVES);
1834 static inline bool nested_cpu_has_pml(struct vmcs12 *vmcs12)
1836 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_PML);
1839 static inline bool nested_cpu_has_virt_x2apic_mode(struct vmcs12 *vmcs12)
1841 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE);
1844 static inline bool nested_cpu_has_vpid(struct vmcs12 *vmcs12)
1846 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_VPID);
1849 static inline bool nested_cpu_has_apic_reg_virt(struct vmcs12 *vmcs12)
1851 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_APIC_REGISTER_VIRT);
1854 static inline bool nested_cpu_has_vid(struct vmcs12 *vmcs12)
1856 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
1859 static inline bool nested_cpu_has_posted_intr(struct vmcs12 *vmcs12)
1861 return vmcs12->pin_based_vm_exec_control & PIN_BASED_POSTED_INTR;
1864 static inline bool nested_cpu_has_vmfunc(struct vmcs12 *vmcs12)
1866 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_VMFUNC);
1869 static inline bool nested_cpu_has_eptp_switching(struct vmcs12 *vmcs12)
1871 return nested_cpu_has_vmfunc(vmcs12) &&
1872 (vmcs12->vm_function_control &
1873 VMX_VMFUNC_EPTP_SWITCHING);
1876 static inline bool nested_cpu_has_shadow_vmcs(struct vmcs12 *vmcs12)
1878 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_SHADOW_VMCS);
1881 static inline bool is_nmi(u32 intr_info)
1883 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK))
1884 == (INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK);
1887 static void nested_vmx_vmexit(struct kvm_vcpu *vcpu, u32 exit_reason,
1889 unsigned long exit_qualification);
1890 static void nested_vmx_entry_failure(struct kvm_vcpu *vcpu,
1891 struct vmcs12 *vmcs12,
1892 u32 reason, unsigned long qualification);
1894 static int __find_msr_index(struct vcpu_vmx *vmx, u32 msr)
1898 for (i = 0; i < vmx->nmsrs; ++i)
1899 if (vmx_msr_index[vmx->guest_msrs[i].index] == msr)
1904 static inline void __invvpid(int ext, u16 vpid, gva_t gva)
1910 } operand = { vpid, 0, gva };
1912 asm volatile (__ex(ASM_VMX_INVVPID)
1913 /* CF==1 or ZF==1 --> rc = -1 */
1914 "; ja 1f ; ud2 ; 1:"
1915 : : "a"(&operand), "c"(ext) : "cc", "memory");
1918 static inline void __invept(int ext, u64 eptp, gpa_t gpa)
1922 } operand = {eptp, gpa};
1924 asm volatile (__ex(ASM_VMX_INVEPT)
1925 /* CF==1 or ZF==1 --> rc = -1 */
1926 "; ja 1f ; ud2 ; 1:\n"
1927 : : "a" (&operand), "c" (ext) : "cc", "memory");
1930 static struct shared_msr_entry *find_msr_entry(struct vcpu_vmx *vmx, u32 msr)
1934 i = __find_msr_index(vmx, msr);
1936 return &vmx->guest_msrs[i];
1940 static void vmcs_clear(struct vmcs *vmcs)
1942 u64 phys_addr = __pa(vmcs);
1945 asm volatile (__ex(ASM_VMX_VMCLEAR_RAX) "; setna %0"
1946 : "=qm"(error) : "a"(&phys_addr), "m"(phys_addr)
1949 printk(KERN_ERR "kvm: vmclear fail: %p/%llx\n",
1953 static inline void loaded_vmcs_init(struct loaded_vmcs *loaded_vmcs)
1955 vmcs_clear(loaded_vmcs->vmcs);
1956 if (loaded_vmcs->shadow_vmcs && loaded_vmcs->launched)
1957 vmcs_clear(loaded_vmcs->shadow_vmcs);
1958 loaded_vmcs->cpu = -1;
1959 loaded_vmcs->launched = 0;
1962 static void vmcs_load(struct vmcs *vmcs)
1964 u64 phys_addr = __pa(vmcs);
1967 if (static_branch_unlikely(&enable_evmcs))
1968 return evmcs_load(phys_addr);
1970 asm volatile (__ex(ASM_VMX_VMPTRLD_RAX) "; setna %0"
1971 : "=qm"(error) : "a"(&phys_addr), "m"(phys_addr)
1974 printk(KERN_ERR "kvm: vmptrld %p/%llx failed\n",
1978 #ifdef CONFIG_KEXEC_CORE
1980 * This bitmap is used to indicate whether the vmclear
1981 * operation is enabled on all cpus. All disabled by
1984 static cpumask_t crash_vmclear_enabled_bitmap = CPU_MASK_NONE;
1986 static inline void crash_enable_local_vmclear(int cpu)
1988 cpumask_set_cpu(cpu, &crash_vmclear_enabled_bitmap);
1991 static inline void crash_disable_local_vmclear(int cpu)
1993 cpumask_clear_cpu(cpu, &crash_vmclear_enabled_bitmap);
1996 static inline int crash_local_vmclear_enabled(int cpu)
1998 return cpumask_test_cpu(cpu, &crash_vmclear_enabled_bitmap);
2001 static void crash_vmclear_local_loaded_vmcss(void)
2003 int cpu = raw_smp_processor_id();
2004 struct loaded_vmcs *v;
2006 if (!crash_local_vmclear_enabled(cpu))
2009 list_for_each_entry(v, &per_cpu(loaded_vmcss_on_cpu, cpu),
2010 loaded_vmcss_on_cpu_link)
2011 vmcs_clear(v->vmcs);
2014 static inline void crash_enable_local_vmclear(int cpu) { }
2015 static inline void crash_disable_local_vmclear(int cpu) { }
2016 #endif /* CONFIG_KEXEC_CORE */
2018 static void __loaded_vmcs_clear(void *arg)
2020 struct loaded_vmcs *loaded_vmcs = arg;
2021 int cpu = raw_smp_processor_id();
2023 if (loaded_vmcs->cpu != cpu)
2024 return; /* vcpu migration can race with cpu offline */
2025 if (per_cpu(current_vmcs, cpu) == loaded_vmcs->vmcs)
2026 per_cpu(current_vmcs, cpu) = NULL;
2027 crash_disable_local_vmclear(cpu);
2028 list_del(&loaded_vmcs->loaded_vmcss_on_cpu_link);
2031 * we should ensure updating loaded_vmcs->loaded_vmcss_on_cpu_link
2032 * is before setting loaded_vmcs->vcpu to -1 which is done in
2033 * loaded_vmcs_init. Otherwise, other cpu can see vcpu = -1 fist
2034 * then adds the vmcs into percpu list before it is deleted.
2038 loaded_vmcs_init(loaded_vmcs);
2039 crash_enable_local_vmclear(cpu);
2042 static void loaded_vmcs_clear(struct loaded_vmcs *loaded_vmcs)
2044 int cpu = loaded_vmcs->cpu;
2047 smp_call_function_single(cpu,
2048 __loaded_vmcs_clear, loaded_vmcs, 1);
2051 static inline bool vpid_sync_vcpu_addr(int vpid, gva_t addr)
2056 if (cpu_has_vmx_invvpid_individual_addr()) {
2057 __invvpid(VMX_VPID_EXTENT_INDIVIDUAL_ADDR, vpid, addr);
2064 static inline void vpid_sync_vcpu_single(int vpid)
2069 if (cpu_has_vmx_invvpid_single())
2070 __invvpid(VMX_VPID_EXTENT_SINGLE_CONTEXT, vpid, 0);
2073 static inline void vpid_sync_vcpu_global(void)
2075 if (cpu_has_vmx_invvpid_global())
2076 __invvpid(VMX_VPID_EXTENT_ALL_CONTEXT, 0, 0);
2079 static inline void vpid_sync_context(int vpid)
2081 if (cpu_has_vmx_invvpid_single())
2082 vpid_sync_vcpu_single(vpid);
2084 vpid_sync_vcpu_global();
2087 static inline void ept_sync_global(void)
2089 __invept(VMX_EPT_EXTENT_GLOBAL, 0, 0);
2092 static inline void ept_sync_context(u64 eptp)
2094 if (cpu_has_vmx_invept_context())
2095 __invept(VMX_EPT_EXTENT_CONTEXT, eptp, 0);
2100 static __always_inline void vmcs_check16(unsigned long field)
2102 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2000,
2103 "16-bit accessor invalid for 64-bit field");
2104 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2001,
2105 "16-bit accessor invalid for 64-bit high field");
2106 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x4000,
2107 "16-bit accessor invalid for 32-bit high field");
2108 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x6000,
2109 "16-bit accessor invalid for natural width field");
2112 static __always_inline void vmcs_check32(unsigned long field)
2114 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0,
2115 "32-bit accessor invalid for 16-bit field");
2116 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x6000,
2117 "32-bit accessor invalid for natural width field");
2120 static __always_inline void vmcs_check64(unsigned long field)
2122 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0,
2123 "64-bit accessor invalid for 16-bit field");
2124 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2001,
2125 "64-bit accessor invalid for 64-bit high field");
2126 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x4000,
2127 "64-bit accessor invalid for 32-bit field");
2128 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x6000,
2129 "64-bit accessor invalid for natural width field");
2132 static __always_inline void vmcs_checkl(unsigned long field)
2134 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0,
2135 "Natural width accessor invalid for 16-bit field");
2136 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2000,
2137 "Natural width accessor invalid for 64-bit field");
2138 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2001,
2139 "Natural width accessor invalid for 64-bit high field");
2140 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x4000,
2141 "Natural width accessor invalid for 32-bit field");
2144 static __always_inline unsigned long __vmcs_readl(unsigned long field)
2146 unsigned long value;
2148 asm volatile (__ex_clear(ASM_VMX_VMREAD_RDX_RAX, "%0")
2149 : "=a"(value) : "d"(field) : "cc");
2153 static __always_inline u16 vmcs_read16(unsigned long field)
2155 vmcs_check16(field);
2156 if (static_branch_unlikely(&enable_evmcs))
2157 return evmcs_read16(field);
2158 return __vmcs_readl(field);
2161 static __always_inline u32 vmcs_read32(unsigned long field)
2163 vmcs_check32(field);
2164 if (static_branch_unlikely(&enable_evmcs))
2165 return evmcs_read32(field);
2166 return __vmcs_readl(field);
2169 static __always_inline u64 vmcs_read64(unsigned long field)
2171 vmcs_check64(field);
2172 if (static_branch_unlikely(&enable_evmcs))
2173 return evmcs_read64(field);
2174 #ifdef CONFIG_X86_64
2175 return __vmcs_readl(field);
2177 return __vmcs_readl(field) | ((u64)__vmcs_readl(field+1) << 32);
2181 static __always_inline unsigned long vmcs_readl(unsigned long field)
2184 if (static_branch_unlikely(&enable_evmcs))
2185 return evmcs_read64(field);
2186 return __vmcs_readl(field);
2189 static noinline void vmwrite_error(unsigned long field, unsigned long value)
2191 printk(KERN_ERR "vmwrite error: reg %lx value %lx (err %d)\n",
2192 field, value, vmcs_read32(VM_INSTRUCTION_ERROR));
2196 static __always_inline void __vmcs_writel(unsigned long field, unsigned long value)
2200 asm volatile (__ex(ASM_VMX_VMWRITE_RAX_RDX) "; setna %0"
2201 : "=q"(error) : "a"(value), "d"(field) : "cc");
2202 if (unlikely(error))
2203 vmwrite_error(field, value);
2206 static __always_inline void vmcs_write16(unsigned long field, u16 value)
2208 vmcs_check16(field);
2209 if (static_branch_unlikely(&enable_evmcs))
2210 return evmcs_write16(field, value);
2212 __vmcs_writel(field, value);
2215 static __always_inline void vmcs_write32(unsigned long field, u32 value)
2217 vmcs_check32(field);
2218 if (static_branch_unlikely(&enable_evmcs))
2219 return evmcs_write32(field, value);
2221 __vmcs_writel(field, value);
2224 static __always_inline void vmcs_write64(unsigned long field, u64 value)
2226 vmcs_check64(field);
2227 if (static_branch_unlikely(&enable_evmcs))
2228 return evmcs_write64(field, value);
2230 __vmcs_writel(field, value);
2231 #ifndef CONFIG_X86_64
2233 __vmcs_writel(field+1, value >> 32);
2237 static __always_inline void vmcs_writel(unsigned long field, unsigned long value)
2240 if (static_branch_unlikely(&enable_evmcs))
2241 return evmcs_write64(field, value);
2243 __vmcs_writel(field, value);
2246 static __always_inline void vmcs_clear_bits(unsigned long field, u32 mask)
2248 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x2000,
2249 "vmcs_clear_bits does not support 64-bit fields");
2250 if (static_branch_unlikely(&enable_evmcs))
2251 return evmcs_write32(field, evmcs_read32(field) & ~mask);
2253 __vmcs_writel(field, __vmcs_readl(field) & ~mask);
2256 static __always_inline void vmcs_set_bits(unsigned long field, u32 mask)
2258 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x2000,
2259 "vmcs_set_bits does not support 64-bit fields");
2260 if (static_branch_unlikely(&enable_evmcs))
2261 return evmcs_write32(field, evmcs_read32(field) | mask);
2263 __vmcs_writel(field, __vmcs_readl(field) | mask);
2266 static inline void vm_entry_controls_reset_shadow(struct vcpu_vmx *vmx)
2268 vmx->vm_entry_controls_shadow = vmcs_read32(VM_ENTRY_CONTROLS);
2271 static inline void vm_entry_controls_init(struct vcpu_vmx *vmx, u32 val)
2273 vmcs_write32(VM_ENTRY_CONTROLS, val);
2274 vmx->vm_entry_controls_shadow = val;
2277 static inline void vm_entry_controls_set(struct vcpu_vmx *vmx, u32 val)
2279 if (vmx->vm_entry_controls_shadow != val)
2280 vm_entry_controls_init(vmx, val);
2283 static inline u32 vm_entry_controls_get(struct vcpu_vmx *vmx)
2285 return vmx->vm_entry_controls_shadow;
2289 static inline void vm_entry_controls_setbit(struct vcpu_vmx *vmx, u32 val)
2291 vm_entry_controls_set(vmx, vm_entry_controls_get(vmx) | val);
2294 static inline void vm_entry_controls_clearbit(struct vcpu_vmx *vmx, u32 val)
2296 vm_entry_controls_set(vmx, vm_entry_controls_get(vmx) & ~val);
2299 static inline void vm_exit_controls_reset_shadow(struct vcpu_vmx *vmx)
2301 vmx->vm_exit_controls_shadow = vmcs_read32(VM_EXIT_CONTROLS);
2304 static inline void vm_exit_controls_init(struct vcpu_vmx *vmx, u32 val)
2306 vmcs_write32(VM_EXIT_CONTROLS, val);
2307 vmx->vm_exit_controls_shadow = val;
2310 static inline void vm_exit_controls_set(struct vcpu_vmx *vmx, u32 val)
2312 if (vmx->vm_exit_controls_shadow != val)
2313 vm_exit_controls_init(vmx, val);
2316 static inline u32 vm_exit_controls_get(struct vcpu_vmx *vmx)
2318 return vmx->vm_exit_controls_shadow;
2322 static inline void vm_exit_controls_setbit(struct vcpu_vmx *vmx, u32 val)
2324 vm_exit_controls_set(vmx, vm_exit_controls_get(vmx) | val);
2327 static inline void vm_exit_controls_clearbit(struct vcpu_vmx *vmx, u32 val)
2329 vm_exit_controls_set(vmx, vm_exit_controls_get(vmx) & ~val);
2332 static void vmx_segment_cache_clear(struct vcpu_vmx *vmx)
2334 vmx->segment_cache.bitmask = 0;
2337 static bool vmx_segment_cache_test_set(struct vcpu_vmx *vmx, unsigned seg,
2341 u32 mask = 1 << (seg * SEG_FIELD_NR + field);
2343 if (!(vmx->vcpu.arch.regs_avail & (1 << VCPU_EXREG_SEGMENTS))) {
2344 vmx->vcpu.arch.regs_avail |= (1 << VCPU_EXREG_SEGMENTS);
2345 vmx->segment_cache.bitmask = 0;
2347 ret = vmx->segment_cache.bitmask & mask;
2348 vmx->segment_cache.bitmask |= mask;
2352 static u16 vmx_read_guest_seg_selector(struct vcpu_vmx *vmx, unsigned seg)
2354 u16 *p = &vmx->segment_cache.seg[seg].selector;
2356 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_SEL))
2357 *p = vmcs_read16(kvm_vmx_segment_fields[seg].selector);
2361 static ulong vmx_read_guest_seg_base(struct vcpu_vmx *vmx, unsigned seg)
2363 ulong *p = &vmx->segment_cache.seg[seg].base;
2365 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_BASE))
2366 *p = vmcs_readl(kvm_vmx_segment_fields[seg].base);
2370 static u32 vmx_read_guest_seg_limit(struct vcpu_vmx *vmx, unsigned seg)
2372 u32 *p = &vmx->segment_cache.seg[seg].limit;
2374 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_LIMIT))
2375 *p = vmcs_read32(kvm_vmx_segment_fields[seg].limit);
2379 static u32 vmx_read_guest_seg_ar(struct vcpu_vmx *vmx, unsigned seg)
2381 u32 *p = &vmx->segment_cache.seg[seg].ar;
2383 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_AR))
2384 *p = vmcs_read32(kvm_vmx_segment_fields[seg].ar_bytes);
2388 static void update_exception_bitmap(struct kvm_vcpu *vcpu)
2392 eb = (1u << PF_VECTOR) | (1u << UD_VECTOR) | (1u << MC_VECTOR) |
2393 (1u << DB_VECTOR) | (1u << AC_VECTOR);
2395 * Guest access to VMware backdoor ports could legitimately
2396 * trigger #GP because of TSS I/O permission bitmap.
2397 * We intercept those #GP and allow access to them anyway
2400 if (enable_vmware_backdoor)
2401 eb |= (1u << GP_VECTOR);
2402 if ((vcpu->guest_debug &
2403 (KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_SW_BP)) ==
2404 (KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_SW_BP))
2405 eb |= 1u << BP_VECTOR;
2406 if (to_vmx(vcpu)->rmode.vm86_active)
2409 eb &= ~(1u << PF_VECTOR); /* bypass_guest_pf = 0 */
2411 /* When we are running a nested L2 guest and L1 specified for it a
2412 * certain exception bitmap, we must trap the same exceptions and pass
2413 * them to L1. When running L2, we will only handle the exceptions
2414 * specified above if L1 did not want them.
2416 if (is_guest_mode(vcpu))
2417 eb |= get_vmcs12(vcpu)->exception_bitmap;
2419 vmcs_write32(EXCEPTION_BITMAP, eb);
2423 * Check if MSR is intercepted for currently loaded MSR bitmap.
2425 static bool msr_write_intercepted(struct kvm_vcpu *vcpu, u32 msr)
2427 unsigned long *msr_bitmap;
2428 int f = sizeof(unsigned long);
2430 if (!cpu_has_vmx_msr_bitmap())
2433 msr_bitmap = to_vmx(vcpu)->loaded_vmcs->msr_bitmap;
2435 if (msr <= 0x1fff) {
2436 return !!test_bit(msr, msr_bitmap + 0x800 / f);
2437 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
2439 return !!test_bit(msr, msr_bitmap + 0xc00 / f);
2446 * Check if MSR is intercepted for L01 MSR bitmap.
2448 static bool msr_write_intercepted_l01(struct kvm_vcpu *vcpu, u32 msr)
2450 unsigned long *msr_bitmap;
2451 int f = sizeof(unsigned long);
2453 if (!cpu_has_vmx_msr_bitmap())
2456 msr_bitmap = to_vmx(vcpu)->vmcs01.msr_bitmap;
2458 if (msr <= 0x1fff) {
2459 return !!test_bit(msr, msr_bitmap + 0x800 / f);
2460 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
2462 return !!test_bit(msr, msr_bitmap + 0xc00 / f);
2468 static void clear_atomic_switch_msr_special(struct vcpu_vmx *vmx,
2469 unsigned long entry, unsigned long exit)
2471 vm_entry_controls_clearbit(vmx, entry);
2472 vm_exit_controls_clearbit(vmx, exit);
2475 static void clear_atomic_switch_msr(struct vcpu_vmx *vmx, unsigned msr)
2478 struct msr_autoload *m = &vmx->msr_autoload;
2482 if (cpu_has_load_ia32_efer) {
2483 clear_atomic_switch_msr_special(vmx,
2484 VM_ENTRY_LOAD_IA32_EFER,
2485 VM_EXIT_LOAD_IA32_EFER);
2489 case MSR_CORE_PERF_GLOBAL_CTRL:
2490 if (cpu_has_load_perf_global_ctrl) {
2491 clear_atomic_switch_msr_special(vmx,
2492 VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL,
2493 VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL);
2499 for (i = 0; i < m->nr; ++i)
2500 if (m->guest[i].index == msr)
2506 m->guest[i] = m->guest[m->nr];
2507 m->host[i] = m->host[m->nr];
2508 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, m->nr);
2509 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, m->nr);
2512 static void add_atomic_switch_msr_special(struct vcpu_vmx *vmx,
2513 unsigned long entry, unsigned long exit,
2514 unsigned long guest_val_vmcs, unsigned long host_val_vmcs,
2515 u64 guest_val, u64 host_val)
2517 vmcs_write64(guest_val_vmcs, guest_val);
2518 vmcs_write64(host_val_vmcs, host_val);
2519 vm_entry_controls_setbit(vmx, entry);
2520 vm_exit_controls_setbit(vmx, exit);
2523 static void add_atomic_switch_msr(struct vcpu_vmx *vmx, unsigned msr,
2524 u64 guest_val, u64 host_val)
2527 struct msr_autoload *m = &vmx->msr_autoload;
2531 if (cpu_has_load_ia32_efer) {
2532 add_atomic_switch_msr_special(vmx,
2533 VM_ENTRY_LOAD_IA32_EFER,
2534 VM_EXIT_LOAD_IA32_EFER,
2537 guest_val, host_val);
2541 case MSR_CORE_PERF_GLOBAL_CTRL:
2542 if (cpu_has_load_perf_global_ctrl) {
2543 add_atomic_switch_msr_special(vmx,
2544 VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL,
2545 VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL,
2546 GUEST_IA32_PERF_GLOBAL_CTRL,
2547 HOST_IA32_PERF_GLOBAL_CTRL,
2548 guest_val, host_val);
2552 case MSR_IA32_PEBS_ENABLE:
2553 /* PEBS needs a quiescent period after being disabled (to write
2554 * a record). Disabling PEBS through VMX MSR swapping doesn't
2555 * provide that period, so a CPU could write host's record into
2558 wrmsrl(MSR_IA32_PEBS_ENABLE, 0);
2561 for (i = 0; i < m->nr; ++i)
2562 if (m->guest[i].index == msr)
2565 if (i == NR_AUTOLOAD_MSRS) {
2566 printk_once(KERN_WARNING "Not enough msr switch entries. "
2567 "Can't add msr %x\n", msr);
2569 } else if (i == m->nr) {
2571 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, m->nr);
2572 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, m->nr);
2575 m->guest[i].index = msr;
2576 m->guest[i].value = guest_val;
2577 m->host[i].index = msr;
2578 m->host[i].value = host_val;
2581 static bool update_transition_efer(struct vcpu_vmx *vmx, int efer_offset)
2583 u64 guest_efer = vmx->vcpu.arch.efer;
2584 u64 ignore_bits = 0;
2588 * NX is needed to handle CR0.WP=1, CR4.SMEP=1. Testing
2589 * host CPUID is more efficient than testing guest CPUID
2590 * or CR4. Host SMEP is anyway a requirement for guest SMEP.
2592 if (boot_cpu_has(X86_FEATURE_SMEP))
2593 guest_efer |= EFER_NX;
2594 else if (!(guest_efer & EFER_NX))
2595 ignore_bits |= EFER_NX;
2599 * LMA and LME handled by hardware; SCE meaningless outside long mode.
2601 ignore_bits |= EFER_SCE;
2602 #ifdef CONFIG_X86_64
2603 ignore_bits |= EFER_LMA | EFER_LME;
2604 /* SCE is meaningful only in long mode on Intel */
2605 if (guest_efer & EFER_LMA)
2606 ignore_bits &= ~(u64)EFER_SCE;
2609 clear_atomic_switch_msr(vmx, MSR_EFER);
2612 * On EPT, we can't emulate NX, so we must switch EFER atomically.
2613 * On CPUs that support "load IA32_EFER", always switch EFER
2614 * atomically, since it's faster than switching it manually.
2616 if (cpu_has_load_ia32_efer ||
2617 (enable_ept && ((vmx->vcpu.arch.efer ^ host_efer) & EFER_NX))) {
2618 if (!(guest_efer & EFER_LMA))
2619 guest_efer &= ~EFER_LME;
2620 if (guest_efer != host_efer)
2621 add_atomic_switch_msr(vmx, MSR_EFER,
2622 guest_efer, host_efer);
2625 guest_efer &= ~ignore_bits;
2626 guest_efer |= host_efer & ignore_bits;
2628 vmx->guest_msrs[efer_offset].data = guest_efer;
2629 vmx->guest_msrs[efer_offset].mask = ~ignore_bits;
2635 #ifdef CONFIG_X86_32
2637 * On 32-bit kernels, VM exits still load the FS and GS bases from the
2638 * VMCS rather than the segment table. KVM uses this helper to figure
2639 * out the current bases to poke them into the VMCS before entry.
2641 static unsigned long segment_base(u16 selector)
2643 struct desc_struct *table;
2646 if (!(selector & ~SEGMENT_RPL_MASK))
2649 table = get_current_gdt_ro();
2651 if ((selector & SEGMENT_TI_MASK) == SEGMENT_LDT) {
2652 u16 ldt_selector = kvm_read_ldt();
2654 if (!(ldt_selector & ~SEGMENT_RPL_MASK))
2657 table = (struct desc_struct *)segment_base(ldt_selector);
2659 v = get_desc_base(&table[selector >> 3]);
2664 static void vmx_save_host_state(struct kvm_vcpu *vcpu)
2666 struct vcpu_vmx *vmx = to_vmx(vcpu);
2667 #ifdef CONFIG_X86_64
2668 int cpu = raw_smp_processor_id();
2670 unsigned long fs_base, gs_base;
2674 if (vmx->loaded_cpu_state)
2677 vmx->loaded_cpu_state = vmx->loaded_vmcs;
2680 * Set host fs and gs selectors. Unfortunately, 22.2.3 does not
2681 * allow segment selectors with cpl > 0 or ti == 1.
2683 vmx->host_state.ldt_sel = kvm_read_ldt();
2684 vmx->host_state.gs_ldt_reload_needed = vmx->host_state.ldt_sel;
2686 #ifdef CONFIG_X86_64
2687 savesegment(ds, vmx->host_state.ds_sel);
2688 savesegment(es, vmx->host_state.es_sel);
2690 gs_base = cpu_kernelmode_gs_base(cpu);
2691 if (likely(is_64bit_mm(current->mm))) {
2692 save_fsgs_for_kvm();
2693 fs_sel = current->thread.fsindex;
2694 gs_sel = current->thread.gsindex;
2695 fs_base = current->thread.fsbase;
2696 vmx->msr_host_kernel_gs_base = current->thread.gsbase;
2698 savesegment(fs, fs_sel);
2699 savesegment(gs, gs_sel);
2700 fs_base = read_msr(MSR_FS_BASE);
2701 vmx->msr_host_kernel_gs_base = read_msr(MSR_KERNEL_GS_BASE);
2704 if (is_long_mode(&vmx->vcpu))
2705 wrmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base);
2707 savesegment(fs, fs_sel);
2708 savesegment(gs, gs_sel);
2709 fs_base = segment_base(fs_sel);
2710 gs_base = segment_base(gs_sel);
2713 vmx->host_state.fs_sel = fs_sel;
2714 if (!(fs_sel & 7)) {
2715 vmcs_write16(HOST_FS_SELECTOR, fs_sel);
2716 vmx->host_state.fs_reload_needed = 0;
2718 vmcs_write16(HOST_FS_SELECTOR, 0);
2719 vmx->host_state.fs_reload_needed = 1;
2721 vmx->host_state.gs_sel = gs_sel;
2723 vmcs_write16(HOST_GS_SELECTOR, gs_sel);
2725 vmcs_write16(HOST_GS_SELECTOR, 0);
2726 vmx->host_state.gs_ldt_reload_needed = 1;
2729 vmcs_writel(HOST_FS_BASE, fs_base);
2730 vmcs_writel(HOST_GS_BASE, gs_base);
2732 for (i = 0; i < vmx->save_nmsrs; ++i)
2733 kvm_set_shared_msr(vmx->guest_msrs[i].index,
2734 vmx->guest_msrs[i].data,
2735 vmx->guest_msrs[i].mask);
2738 static void __vmx_load_host_state(struct vcpu_vmx *vmx)
2740 if (!vmx->loaded_cpu_state)
2743 WARN_ON_ONCE(vmx->loaded_cpu_state != vmx->loaded_vmcs);
2745 ++vmx->vcpu.stat.host_state_reload;
2746 vmx->loaded_cpu_state = NULL;
2748 #ifdef CONFIG_X86_64
2749 if (is_long_mode(&vmx->vcpu))
2750 rdmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base);
2752 if (vmx->host_state.gs_ldt_reload_needed) {
2753 kvm_load_ldt(vmx->host_state.ldt_sel);
2754 #ifdef CONFIG_X86_64
2755 load_gs_index(vmx->host_state.gs_sel);
2757 loadsegment(gs, vmx->host_state.gs_sel);
2760 if (vmx->host_state.fs_reload_needed)
2761 loadsegment(fs, vmx->host_state.fs_sel);
2762 #ifdef CONFIG_X86_64
2763 if (unlikely(vmx->host_state.ds_sel | vmx->host_state.es_sel)) {
2764 loadsegment(ds, vmx->host_state.ds_sel);
2765 loadsegment(es, vmx->host_state.es_sel);
2768 invalidate_tss_limit();
2769 #ifdef CONFIG_X86_64
2770 wrmsrl(MSR_KERNEL_GS_BASE, vmx->msr_host_kernel_gs_base);
2772 load_fixmap_gdt(raw_smp_processor_id());
2775 #ifdef CONFIG_X86_64
2776 static u64 vmx_read_guest_kernel_gs_base(struct vcpu_vmx *vmx)
2778 if (is_long_mode(&vmx->vcpu)) {
2780 if (vmx->loaded_cpu_state)
2781 rdmsrl(MSR_KERNEL_GS_BASE,
2782 vmx->msr_guest_kernel_gs_base);
2785 return vmx->msr_guest_kernel_gs_base;
2788 static void vmx_write_guest_kernel_gs_base(struct vcpu_vmx *vmx, u64 data)
2790 if (is_long_mode(&vmx->vcpu)) {
2792 if (vmx->loaded_cpu_state)
2793 wrmsrl(MSR_KERNEL_GS_BASE, data);
2796 vmx->msr_guest_kernel_gs_base = data;
2800 static void vmx_vcpu_pi_load(struct kvm_vcpu *vcpu, int cpu)
2802 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
2803 struct pi_desc old, new;
2807 * In case of hot-plug or hot-unplug, we may have to undo
2808 * vmx_vcpu_pi_put even if there is no assigned device. And we
2809 * always keep PI.NDST up to date for simplicity: it makes the
2810 * code easier, and CPU migration is not a fast path.
2812 if (!pi_test_sn(pi_desc) && vcpu->cpu == cpu)
2816 * First handle the simple case where no cmpxchg is necessary; just
2817 * allow posting non-urgent interrupts.
2819 * If the 'nv' field is POSTED_INTR_WAKEUP_VECTOR, do not change
2820 * PI.NDST: pi_post_block will do it for us and the wakeup_handler
2821 * expects the VCPU to be on the blocked_vcpu_list that matches
2824 if (pi_desc->nv == POSTED_INTR_WAKEUP_VECTOR ||
2826 pi_clear_sn(pi_desc);
2830 /* The full case. */
2832 old.control = new.control = pi_desc->control;
2834 dest = cpu_physical_id(cpu);
2836 if (x2apic_enabled())
2839 new.ndst = (dest << 8) & 0xFF00;
2842 } while (cmpxchg64(&pi_desc->control, old.control,
2843 new.control) != old.control);
2846 static void decache_tsc_multiplier(struct vcpu_vmx *vmx)
2848 vmx->current_tsc_ratio = vmx->vcpu.arch.tsc_scaling_ratio;
2849 vmcs_write64(TSC_MULTIPLIER, vmx->current_tsc_ratio);
2853 * Switches to specified vcpu, until a matching vcpu_put(), but assumes
2854 * vcpu mutex is already taken.
2856 static void vmx_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
2858 struct vcpu_vmx *vmx = to_vmx(vcpu);
2859 bool already_loaded = vmx->loaded_vmcs->cpu == cpu;
2861 if (!already_loaded) {
2862 loaded_vmcs_clear(vmx->loaded_vmcs);
2863 local_irq_disable();
2864 crash_disable_local_vmclear(cpu);
2867 * Read loaded_vmcs->cpu should be before fetching
2868 * loaded_vmcs->loaded_vmcss_on_cpu_link.
2869 * See the comments in __loaded_vmcs_clear().
2873 list_add(&vmx->loaded_vmcs->loaded_vmcss_on_cpu_link,
2874 &per_cpu(loaded_vmcss_on_cpu, cpu));
2875 crash_enable_local_vmclear(cpu);
2879 if (per_cpu(current_vmcs, cpu) != vmx->loaded_vmcs->vmcs) {
2880 per_cpu(current_vmcs, cpu) = vmx->loaded_vmcs->vmcs;
2881 vmcs_load(vmx->loaded_vmcs->vmcs);
2882 indirect_branch_prediction_barrier();
2885 if (!already_loaded) {
2886 void *gdt = get_current_gdt_ro();
2887 unsigned long sysenter_esp;
2889 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
2892 * Linux uses per-cpu TSS and GDT, so set these when switching
2893 * processors. See 22.2.4.
2895 vmcs_writel(HOST_TR_BASE,
2896 (unsigned long)&get_cpu_entry_area(cpu)->tss.x86_tss);
2897 vmcs_writel(HOST_GDTR_BASE, (unsigned long)gdt); /* 22.2.4 */
2900 * VM exits change the host TR limit to 0x67 after a VM
2901 * exit. This is okay, since 0x67 covers everything except
2902 * the IO bitmap and have have code to handle the IO bitmap
2903 * being lost after a VM exit.
2905 BUILD_BUG_ON(IO_BITMAP_OFFSET - 1 != 0x67);
2907 rdmsrl(MSR_IA32_SYSENTER_ESP, sysenter_esp);
2908 vmcs_writel(HOST_IA32_SYSENTER_ESP, sysenter_esp); /* 22.2.3 */
2910 vmx->loaded_vmcs->cpu = cpu;
2913 /* Setup TSC multiplier */
2914 if (kvm_has_tsc_control &&
2915 vmx->current_tsc_ratio != vcpu->arch.tsc_scaling_ratio)
2916 decache_tsc_multiplier(vmx);
2918 vmx_vcpu_pi_load(vcpu, cpu);
2919 vmx->host_pkru = read_pkru();
2920 vmx->host_debugctlmsr = get_debugctlmsr();
2923 static void vmx_vcpu_pi_put(struct kvm_vcpu *vcpu)
2925 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
2927 if (!kvm_arch_has_assigned_device(vcpu->kvm) ||
2928 !irq_remapping_cap(IRQ_POSTING_CAP) ||
2929 !kvm_vcpu_apicv_active(vcpu))
2932 /* Set SN when the vCPU is preempted */
2933 if (vcpu->preempted)
2937 static void vmx_vcpu_put(struct kvm_vcpu *vcpu)
2939 vmx_vcpu_pi_put(vcpu);
2941 __vmx_load_host_state(to_vmx(vcpu));
2944 static bool emulation_required(struct kvm_vcpu *vcpu)
2946 return emulate_invalid_guest_state && !guest_state_valid(vcpu);
2949 static void vmx_decache_cr0_guest_bits(struct kvm_vcpu *vcpu);
2952 * Return the cr0 value that a nested guest would read. This is a combination
2953 * of the real cr0 used to run the guest (guest_cr0), and the bits shadowed by
2954 * its hypervisor (cr0_read_shadow).
2956 static inline unsigned long nested_read_cr0(struct vmcs12 *fields)
2958 return (fields->guest_cr0 & ~fields->cr0_guest_host_mask) |
2959 (fields->cr0_read_shadow & fields->cr0_guest_host_mask);
2961 static inline unsigned long nested_read_cr4(struct vmcs12 *fields)
2963 return (fields->guest_cr4 & ~fields->cr4_guest_host_mask) |
2964 (fields->cr4_read_shadow & fields->cr4_guest_host_mask);
2967 static unsigned long vmx_get_rflags(struct kvm_vcpu *vcpu)
2969 unsigned long rflags, save_rflags;
2971 if (!test_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail)) {
2972 __set_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail);
2973 rflags = vmcs_readl(GUEST_RFLAGS);
2974 if (to_vmx(vcpu)->rmode.vm86_active) {
2975 rflags &= RMODE_GUEST_OWNED_EFLAGS_BITS;
2976 save_rflags = to_vmx(vcpu)->rmode.save_rflags;
2977 rflags |= save_rflags & ~RMODE_GUEST_OWNED_EFLAGS_BITS;
2979 to_vmx(vcpu)->rflags = rflags;
2981 return to_vmx(vcpu)->rflags;
2984 static void vmx_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags)
2986 unsigned long old_rflags = vmx_get_rflags(vcpu);
2988 __set_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail);
2989 to_vmx(vcpu)->rflags = rflags;
2990 if (to_vmx(vcpu)->rmode.vm86_active) {
2991 to_vmx(vcpu)->rmode.save_rflags = rflags;
2992 rflags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM;
2994 vmcs_writel(GUEST_RFLAGS, rflags);
2996 if ((old_rflags ^ to_vmx(vcpu)->rflags) & X86_EFLAGS_VM)
2997 to_vmx(vcpu)->emulation_required = emulation_required(vcpu);
3000 static u32 vmx_get_interrupt_shadow(struct kvm_vcpu *vcpu)
3002 u32 interruptibility = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
3005 if (interruptibility & GUEST_INTR_STATE_STI)
3006 ret |= KVM_X86_SHADOW_INT_STI;
3007 if (interruptibility & GUEST_INTR_STATE_MOV_SS)
3008 ret |= KVM_X86_SHADOW_INT_MOV_SS;
3013 static void vmx_set_interrupt_shadow(struct kvm_vcpu *vcpu, int mask)
3015 u32 interruptibility_old = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
3016 u32 interruptibility = interruptibility_old;
3018 interruptibility &= ~(GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS);
3020 if (mask & KVM_X86_SHADOW_INT_MOV_SS)
3021 interruptibility |= GUEST_INTR_STATE_MOV_SS;
3022 else if (mask & KVM_X86_SHADOW_INT_STI)
3023 interruptibility |= GUEST_INTR_STATE_STI;
3025 if ((interruptibility != interruptibility_old))
3026 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, interruptibility);
3029 static void skip_emulated_instruction(struct kvm_vcpu *vcpu)
3033 rip = kvm_rip_read(vcpu);
3034 rip += vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
3035 kvm_rip_write(vcpu, rip);
3037 /* skipping an emulated instruction also counts */
3038 vmx_set_interrupt_shadow(vcpu, 0);
3041 static void nested_vmx_inject_exception_vmexit(struct kvm_vcpu *vcpu,
3042 unsigned long exit_qual)
3044 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
3045 unsigned int nr = vcpu->arch.exception.nr;
3046 u32 intr_info = nr | INTR_INFO_VALID_MASK;
3048 if (vcpu->arch.exception.has_error_code) {
3049 vmcs12->vm_exit_intr_error_code = vcpu->arch.exception.error_code;
3050 intr_info |= INTR_INFO_DELIVER_CODE_MASK;
3053 if (kvm_exception_is_soft(nr))
3054 intr_info |= INTR_TYPE_SOFT_EXCEPTION;
3056 intr_info |= INTR_TYPE_HARD_EXCEPTION;
3058 if (!(vmcs12->idt_vectoring_info_field & VECTORING_INFO_VALID_MASK) &&
3059 vmx_get_nmi_mask(vcpu))
3060 intr_info |= INTR_INFO_UNBLOCK_NMI;
3062 nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI, intr_info, exit_qual);
3066 * KVM wants to inject page-faults which it got to the guest. This function
3067 * checks whether in a nested guest, we need to inject them to L1 or L2.
3069 static int nested_vmx_check_exception(struct kvm_vcpu *vcpu, unsigned long *exit_qual)
3071 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
3072 unsigned int nr = vcpu->arch.exception.nr;
3074 if (nr == PF_VECTOR) {
3075 if (vcpu->arch.exception.nested_apf) {
3076 *exit_qual = vcpu->arch.apf.nested_apf_token;
3080 * FIXME: we must not write CR2 when L1 intercepts an L2 #PF exception.
3081 * The fix is to add the ancillary datum (CR2 or DR6) to structs
3082 * kvm_queued_exception and kvm_vcpu_events, so that CR2 and DR6
3083 * can be written only when inject_pending_event runs. This should be
3084 * conditional on a new capability---if the capability is disabled,
3085 * kvm_multiple_exception would write the ancillary information to
3086 * CR2 or DR6, for backwards ABI-compatibility.
3088 if (nested_vmx_is_page_fault_vmexit(vmcs12,
3089 vcpu->arch.exception.error_code)) {
3090 *exit_qual = vcpu->arch.cr2;
3094 if (vmcs12->exception_bitmap & (1u << nr)) {
3095 if (nr == DB_VECTOR)
3096 *exit_qual = vcpu->arch.dr6;
3106 static void vmx_clear_hlt(struct kvm_vcpu *vcpu)
3109 * Ensure that we clear the HLT state in the VMCS. We don't need to
3110 * explicitly skip the instruction because if the HLT state is set,
3111 * then the instruction is already executing and RIP has already been
3114 if (kvm_hlt_in_guest(vcpu->kvm) &&
3115 vmcs_read32(GUEST_ACTIVITY_STATE) == GUEST_ACTIVITY_HLT)
3116 vmcs_write32(GUEST_ACTIVITY_STATE, GUEST_ACTIVITY_ACTIVE);
3119 static void vmx_queue_exception(struct kvm_vcpu *vcpu)
3121 struct vcpu_vmx *vmx = to_vmx(vcpu);
3122 unsigned nr = vcpu->arch.exception.nr;
3123 bool has_error_code = vcpu->arch.exception.has_error_code;
3124 u32 error_code = vcpu->arch.exception.error_code;
3125 u32 intr_info = nr | INTR_INFO_VALID_MASK;
3127 if (has_error_code) {
3128 vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE, error_code);
3129 intr_info |= INTR_INFO_DELIVER_CODE_MASK;
3132 if (vmx->rmode.vm86_active) {
3134 if (kvm_exception_is_soft(nr))
3135 inc_eip = vcpu->arch.event_exit_inst_len;
3136 if (kvm_inject_realmode_interrupt(vcpu, nr, inc_eip) != EMULATE_DONE)
3137 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
3141 WARN_ON_ONCE(vmx->emulation_required);
3143 if (kvm_exception_is_soft(nr)) {
3144 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
3145 vmx->vcpu.arch.event_exit_inst_len);
3146 intr_info |= INTR_TYPE_SOFT_EXCEPTION;
3148 intr_info |= INTR_TYPE_HARD_EXCEPTION;
3150 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr_info);
3152 vmx_clear_hlt(vcpu);
3155 static bool vmx_rdtscp_supported(void)
3157 return cpu_has_vmx_rdtscp();
3160 static bool vmx_invpcid_supported(void)
3162 return cpu_has_vmx_invpcid();
3166 * Swap MSR entry in host/guest MSR entry array.
3168 static void move_msr_up(struct vcpu_vmx *vmx, int from, int to)
3170 struct shared_msr_entry tmp;
3172 tmp = vmx->guest_msrs[to];
3173 vmx->guest_msrs[to] = vmx->guest_msrs[from];
3174 vmx->guest_msrs[from] = tmp;
3178 * Set up the vmcs to automatically save and restore system
3179 * msrs. Don't touch the 64-bit msrs if the guest is in legacy
3180 * mode, as fiddling with msrs is very expensive.
3182 static void setup_msrs(struct vcpu_vmx *vmx)
3184 int save_nmsrs, index;
3187 #ifdef CONFIG_X86_64
3188 if (is_long_mode(&vmx->vcpu)) {
3189 index = __find_msr_index(vmx, MSR_SYSCALL_MASK);
3191 move_msr_up(vmx, index, save_nmsrs++);
3192 index = __find_msr_index(vmx, MSR_LSTAR);
3194 move_msr_up(vmx, index, save_nmsrs++);
3195 index = __find_msr_index(vmx, MSR_CSTAR);
3197 move_msr_up(vmx, index, save_nmsrs++);
3198 index = __find_msr_index(vmx, MSR_TSC_AUX);
3199 if (index >= 0 && guest_cpuid_has(&vmx->vcpu, X86_FEATURE_RDTSCP))
3200 move_msr_up(vmx, index, save_nmsrs++);
3202 * MSR_STAR is only needed on long mode guests, and only
3203 * if efer.sce is enabled.
3205 index = __find_msr_index(vmx, MSR_STAR);
3206 if ((index >= 0) && (vmx->vcpu.arch.efer & EFER_SCE))
3207 move_msr_up(vmx, index, save_nmsrs++);
3210 index = __find_msr_index(vmx, MSR_EFER);
3211 if (index >= 0 && update_transition_efer(vmx, index))
3212 move_msr_up(vmx, index, save_nmsrs++);
3214 vmx->save_nmsrs = save_nmsrs;
3216 if (cpu_has_vmx_msr_bitmap())
3217 vmx_update_msr_bitmap(&vmx->vcpu);
3220 static u64 vmx_read_l1_tsc_offset(struct kvm_vcpu *vcpu)
3222 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
3224 if (is_guest_mode(vcpu) &&
3225 (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING))
3226 return vcpu->arch.tsc_offset - vmcs12->tsc_offset;
3228 return vcpu->arch.tsc_offset;
3232 * writes 'offset' into guest's timestamp counter offset register
3234 static void vmx_write_tsc_offset(struct kvm_vcpu *vcpu, u64 offset)
3236 if (is_guest_mode(vcpu)) {
3238 * We're here if L1 chose not to trap WRMSR to TSC. According
3239 * to the spec, this should set L1's TSC; The offset that L1
3240 * set for L2 remains unchanged, and still needs to be added
3241 * to the newly set TSC to get L2's TSC.
3243 struct vmcs12 *vmcs12;
3244 /* recalculate vmcs02.TSC_OFFSET: */
3245 vmcs12 = get_vmcs12(vcpu);
3246 vmcs_write64(TSC_OFFSET, offset +
3247 (nested_cpu_has(vmcs12, CPU_BASED_USE_TSC_OFFSETING) ?
3248 vmcs12->tsc_offset : 0));
3250 trace_kvm_write_tsc_offset(vcpu->vcpu_id,
3251 vmcs_read64(TSC_OFFSET), offset);
3252 vmcs_write64(TSC_OFFSET, offset);
3257 * nested_vmx_allowed() checks whether a guest should be allowed to use VMX
3258 * instructions and MSRs (i.e., nested VMX). Nested VMX is disabled for
3259 * all guests if the "nested" module option is off, and can also be disabled
3260 * for a single guest by disabling its VMX cpuid bit.
3262 static inline bool nested_vmx_allowed(struct kvm_vcpu *vcpu)
3264 return nested && guest_cpuid_has(vcpu, X86_FEATURE_VMX);
3268 * nested_vmx_setup_ctls_msrs() sets up variables containing the values to be
3269 * returned for the various VMX controls MSRs when nested VMX is enabled.
3270 * The same values should also be used to verify that vmcs12 control fields are
3271 * valid during nested entry from L1 to L2.
3272 * Each of these control msrs has a low and high 32-bit half: A low bit is on
3273 * if the corresponding bit in the (32-bit) control field *must* be on, and a
3274 * bit in the high half is on if the corresponding bit in the control field
3275 * may be on. See also vmx_control_verify().
3277 static void nested_vmx_setup_ctls_msrs(struct nested_vmx_msrs *msrs, bool apicv)
3280 memset(msrs, 0, sizeof(*msrs));
3285 * Note that as a general rule, the high half of the MSRs (bits in
3286 * the control fields which may be 1) should be initialized by the
3287 * intersection of the underlying hardware's MSR (i.e., features which
3288 * can be supported) and the list of features we want to expose -
3289 * because they are known to be properly supported in our code.
3290 * Also, usually, the low half of the MSRs (bits which must be 1) can
3291 * be set to 0, meaning that L1 may turn off any of these bits. The
3292 * reason is that if one of these bits is necessary, it will appear
3293 * in vmcs01 and prepare_vmcs02, when it bitwise-or's the control
3294 * fields of vmcs01 and vmcs02, will turn these bits off - and
3295 * nested_vmx_exit_reflected() will not pass related exits to L1.
3296 * These rules have exceptions below.
3299 /* pin-based controls */
3300 rdmsr(MSR_IA32_VMX_PINBASED_CTLS,
3301 msrs->pinbased_ctls_low,
3302 msrs->pinbased_ctls_high);
3303 msrs->pinbased_ctls_low |=
3304 PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
3305 msrs->pinbased_ctls_high &=
3306 PIN_BASED_EXT_INTR_MASK |
3307 PIN_BASED_NMI_EXITING |
3308 PIN_BASED_VIRTUAL_NMIS |
3309 (apicv ? PIN_BASED_POSTED_INTR : 0);
3310 msrs->pinbased_ctls_high |=
3311 PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR |
3312 PIN_BASED_VMX_PREEMPTION_TIMER;
3315 rdmsr(MSR_IA32_VMX_EXIT_CTLS,
3316 msrs->exit_ctls_low,
3317 msrs->exit_ctls_high);
3318 msrs->exit_ctls_low =
3319 VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR;
3321 msrs->exit_ctls_high &=
3322 #ifdef CONFIG_X86_64
3323 VM_EXIT_HOST_ADDR_SPACE_SIZE |
3325 VM_EXIT_LOAD_IA32_PAT | VM_EXIT_SAVE_IA32_PAT;
3326 msrs->exit_ctls_high |=
3327 VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR |
3328 VM_EXIT_LOAD_IA32_EFER | VM_EXIT_SAVE_IA32_EFER |
3329 VM_EXIT_SAVE_VMX_PREEMPTION_TIMER | VM_EXIT_ACK_INTR_ON_EXIT;
3331 if (kvm_mpx_supported())
3332 msrs->exit_ctls_high |= VM_EXIT_CLEAR_BNDCFGS;
3334 /* We support free control of debug control saving. */
3335 msrs->exit_ctls_low &= ~VM_EXIT_SAVE_DEBUG_CONTROLS;
3337 /* entry controls */
3338 rdmsr(MSR_IA32_VMX_ENTRY_CTLS,
3339 msrs->entry_ctls_low,
3340 msrs->entry_ctls_high);
3341 msrs->entry_ctls_low =
3342 VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR;
3343 msrs->entry_ctls_high &=
3344 #ifdef CONFIG_X86_64
3345 VM_ENTRY_IA32E_MODE |
3347 VM_ENTRY_LOAD_IA32_PAT;
3348 msrs->entry_ctls_high |=
3349 (VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR | VM_ENTRY_LOAD_IA32_EFER);
3350 if (kvm_mpx_supported())
3351 msrs->entry_ctls_high |= VM_ENTRY_LOAD_BNDCFGS;
3353 /* We support free control of debug control loading. */
3354 msrs->entry_ctls_low &= ~VM_ENTRY_LOAD_DEBUG_CONTROLS;
3356 /* cpu-based controls */
3357 rdmsr(MSR_IA32_VMX_PROCBASED_CTLS,
3358 msrs->procbased_ctls_low,
3359 msrs->procbased_ctls_high);
3360 msrs->procbased_ctls_low =
3361 CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
3362 msrs->procbased_ctls_high &=
3363 CPU_BASED_VIRTUAL_INTR_PENDING |
3364 CPU_BASED_VIRTUAL_NMI_PENDING | CPU_BASED_USE_TSC_OFFSETING |
3365 CPU_BASED_HLT_EXITING | CPU_BASED_INVLPG_EXITING |
3366 CPU_BASED_MWAIT_EXITING | CPU_BASED_CR3_LOAD_EXITING |
3367 CPU_BASED_CR3_STORE_EXITING |
3368 #ifdef CONFIG_X86_64
3369 CPU_BASED_CR8_LOAD_EXITING | CPU_BASED_CR8_STORE_EXITING |
3371 CPU_BASED_MOV_DR_EXITING | CPU_BASED_UNCOND_IO_EXITING |
3372 CPU_BASED_USE_IO_BITMAPS | CPU_BASED_MONITOR_TRAP_FLAG |
3373 CPU_BASED_MONITOR_EXITING | CPU_BASED_RDPMC_EXITING |
3374 CPU_BASED_RDTSC_EXITING | CPU_BASED_PAUSE_EXITING |
3375 CPU_BASED_TPR_SHADOW | CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
3377 * We can allow some features even when not supported by the
3378 * hardware. For example, L1 can specify an MSR bitmap - and we
3379 * can use it to avoid exits to L1 - even when L0 runs L2
3380 * without MSR bitmaps.
3382 msrs->procbased_ctls_high |=
3383 CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR |
3384 CPU_BASED_USE_MSR_BITMAPS;
3386 /* We support free control of CR3 access interception. */
3387 msrs->procbased_ctls_low &=
3388 ~(CPU_BASED_CR3_LOAD_EXITING | CPU_BASED_CR3_STORE_EXITING);
3391 * secondary cpu-based controls. Do not include those that
3392 * depend on CPUID bits, they are added later by vmx_cpuid_update.
3394 rdmsr(MSR_IA32_VMX_PROCBASED_CTLS2,
3395 msrs->secondary_ctls_low,
3396 msrs->secondary_ctls_high);
3397 msrs->secondary_ctls_low = 0;
3398 msrs->secondary_ctls_high &=
3399 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
3400 SECONDARY_EXEC_DESC |
3401 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
3402 SECONDARY_EXEC_APIC_REGISTER_VIRT |
3403 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY |
3404 SECONDARY_EXEC_WBINVD_EXITING;
3406 * We can emulate "VMCS shadowing," even if the hardware
3407 * doesn't support it.
3409 msrs->secondary_ctls_high |=
3410 SECONDARY_EXEC_SHADOW_VMCS;
3413 /* nested EPT: emulate EPT also to L1 */
3414 msrs->secondary_ctls_high |=
3415 SECONDARY_EXEC_ENABLE_EPT;
3416 msrs->ept_caps = VMX_EPT_PAGE_WALK_4_BIT |
3417 VMX_EPTP_WB_BIT | VMX_EPT_INVEPT_BIT;
3418 if (cpu_has_vmx_ept_execute_only())
3420 VMX_EPT_EXECUTE_ONLY_BIT;
3421 msrs->ept_caps &= vmx_capability.ept;
3422 msrs->ept_caps |= VMX_EPT_EXTENT_GLOBAL_BIT |
3423 VMX_EPT_EXTENT_CONTEXT_BIT | VMX_EPT_2MB_PAGE_BIT |
3424 VMX_EPT_1GB_PAGE_BIT;
3425 if (enable_ept_ad_bits) {
3426 msrs->secondary_ctls_high |=
3427 SECONDARY_EXEC_ENABLE_PML;
3428 msrs->ept_caps |= VMX_EPT_AD_BIT;
3432 if (cpu_has_vmx_vmfunc()) {
3433 msrs->secondary_ctls_high |=
3434 SECONDARY_EXEC_ENABLE_VMFUNC;
3436 * Advertise EPTP switching unconditionally
3437 * since we emulate it
3440 msrs->vmfunc_controls =
3441 VMX_VMFUNC_EPTP_SWITCHING;
3445 * Old versions of KVM use the single-context version without
3446 * checking for support, so declare that it is supported even
3447 * though it is treated as global context. The alternative is
3448 * not failing the single-context invvpid, and it is worse.
3451 msrs->secondary_ctls_high |=
3452 SECONDARY_EXEC_ENABLE_VPID;
3453 msrs->vpid_caps = VMX_VPID_INVVPID_BIT |
3454 VMX_VPID_EXTENT_SUPPORTED_MASK;
3457 if (enable_unrestricted_guest)
3458 msrs->secondary_ctls_high |=
3459 SECONDARY_EXEC_UNRESTRICTED_GUEST;
3461 /* miscellaneous data */
3462 rdmsr(MSR_IA32_VMX_MISC,
3465 msrs->misc_low &= VMX_MISC_SAVE_EFER_LMA;
3467 MSR_IA32_VMX_MISC_VMWRITE_SHADOW_RO_FIELDS |
3468 VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE |
3469 VMX_MISC_ACTIVITY_HLT;
3470 msrs->misc_high = 0;
3473 * This MSR reports some information about VMX support. We
3474 * should return information about the VMX we emulate for the
3475 * guest, and the VMCS structure we give it - not about the
3476 * VMX support of the underlying hardware.
3480 VMX_BASIC_TRUE_CTLS |
3481 ((u64)VMCS12_SIZE << VMX_BASIC_VMCS_SIZE_SHIFT) |
3482 (VMX_BASIC_MEM_TYPE_WB << VMX_BASIC_MEM_TYPE_SHIFT);
3484 if (cpu_has_vmx_basic_inout())
3485 msrs->basic |= VMX_BASIC_INOUT;
3488 * These MSRs specify bits which the guest must keep fixed on
3489 * while L1 is in VMXON mode (in L1's root mode, or running an L2).
3490 * We picked the standard core2 setting.
3492 #define VMXON_CR0_ALWAYSON (X86_CR0_PE | X86_CR0_PG | X86_CR0_NE)
3493 #define VMXON_CR4_ALWAYSON X86_CR4_VMXE
3494 msrs->cr0_fixed0 = VMXON_CR0_ALWAYSON;
3495 msrs->cr4_fixed0 = VMXON_CR4_ALWAYSON;
3497 /* These MSRs specify bits which the guest must keep fixed off. */
3498 rdmsrl(MSR_IA32_VMX_CR0_FIXED1, msrs->cr0_fixed1);
3499 rdmsrl(MSR_IA32_VMX_CR4_FIXED1, msrs->cr4_fixed1);
3501 /* highest index: VMX_PREEMPTION_TIMER_VALUE */
3502 msrs->vmcs_enum = VMCS12_MAX_FIELD_INDEX << 1;
3506 * if fixed0[i] == 1: val[i] must be 1
3507 * if fixed1[i] == 0: val[i] must be 0
3509 static inline bool fixed_bits_valid(u64 val, u64 fixed0, u64 fixed1)
3511 return ((val & fixed1) | fixed0) == val;
3514 static inline bool vmx_control_verify(u32 control, u32 low, u32 high)
3516 return fixed_bits_valid(control, low, high);
3519 static inline u64 vmx_control_msr(u32 low, u32 high)
3521 return low | ((u64)high << 32);
3524 static bool is_bitwise_subset(u64 superset, u64 subset, u64 mask)
3529 return (superset | subset) == superset;
3532 static int vmx_restore_vmx_basic(struct vcpu_vmx *vmx, u64 data)
3534 const u64 feature_and_reserved =
3535 /* feature (except bit 48; see below) */
3536 BIT_ULL(49) | BIT_ULL(54) | BIT_ULL(55) |
3538 BIT_ULL(31) | GENMASK_ULL(47, 45) | GENMASK_ULL(63, 56);
3539 u64 vmx_basic = vmx->nested.msrs.basic;
3541 if (!is_bitwise_subset(vmx_basic, data, feature_and_reserved))
3545 * KVM does not emulate a version of VMX that constrains physical
3546 * addresses of VMX structures (e.g. VMCS) to 32-bits.
3548 if (data & BIT_ULL(48))
3551 if (vmx_basic_vmcs_revision_id(vmx_basic) !=
3552 vmx_basic_vmcs_revision_id(data))
3555 if (vmx_basic_vmcs_size(vmx_basic) > vmx_basic_vmcs_size(data))
3558 vmx->nested.msrs.basic = data;
3563 vmx_restore_control_msr(struct vcpu_vmx *vmx, u32 msr_index, u64 data)
3568 switch (msr_index) {
3569 case MSR_IA32_VMX_TRUE_PINBASED_CTLS:
3570 lowp = &vmx->nested.msrs.pinbased_ctls_low;
3571 highp = &vmx->nested.msrs.pinbased_ctls_high;
3573 case MSR_IA32_VMX_TRUE_PROCBASED_CTLS:
3574 lowp = &vmx->nested.msrs.procbased_ctls_low;
3575 highp = &vmx->nested.msrs.procbased_ctls_high;
3577 case MSR_IA32_VMX_TRUE_EXIT_CTLS:
3578 lowp = &vmx->nested.msrs.exit_ctls_low;
3579 highp = &vmx->nested.msrs.exit_ctls_high;
3581 case MSR_IA32_VMX_TRUE_ENTRY_CTLS:
3582 lowp = &vmx->nested.msrs.entry_ctls_low;
3583 highp = &vmx->nested.msrs.entry_ctls_high;
3585 case MSR_IA32_VMX_PROCBASED_CTLS2:
3586 lowp = &vmx->nested.msrs.secondary_ctls_low;
3587 highp = &vmx->nested.msrs.secondary_ctls_high;
3593 supported = vmx_control_msr(*lowp, *highp);
3595 /* Check must-be-1 bits are still 1. */
3596 if (!is_bitwise_subset(data, supported, GENMASK_ULL(31, 0)))
3599 /* Check must-be-0 bits are still 0. */
3600 if (!is_bitwise_subset(supported, data, GENMASK_ULL(63, 32)))
3604 *highp = data >> 32;
3608 static int vmx_restore_vmx_misc(struct vcpu_vmx *vmx, u64 data)
3610 const u64 feature_and_reserved_bits =
3612 BIT_ULL(5) | GENMASK_ULL(8, 6) | BIT_ULL(14) | BIT_ULL(15) |
3613 BIT_ULL(28) | BIT_ULL(29) | BIT_ULL(30) |
3615 GENMASK_ULL(13, 9) | BIT_ULL(31);
3618 vmx_misc = vmx_control_msr(vmx->nested.msrs.misc_low,
3619 vmx->nested.msrs.misc_high);
3621 if (!is_bitwise_subset(vmx_misc, data, feature_and_reserved_bits))
3624 if ((vmx->nested.msrs.pinbased_ctls_high &
3625 PIN_BASED_VMX_PREEMPTION_TIMER) &&
3626 vmx_misc_preemption_timer_rate(data) !=
3627 vmx_misc_preemption_timer_rate(vmx_misc))
3630 if (vmx_misc_cr3_count(data) > vmx_misc_cr3_count(vmx_misc))
3633 if (vmx_misc_max_msr(data) > vmx_misc_max_msr(vmx_misc))
3636 if (vmx_misc_mseg_revid(data) != vmx_misc_mseg_revid(vmx_misc))
3639 vmx->nested.msrs.misc_low = data;
3640 vmx->nested.msrs.misc_high = data >> 32;
3643 * If L1 has read-only VM-exit information fields, use the
3644 * less permissive vmx_vmwrite_bitmap to specify write
3645 * permissions for the shadow VMCS.
3647 if (enable_shadow_vmcs && !nested_cpu_has_vmwrite_any_field(&vmx->vcpu))
3648 vmcs_write64(VMWRITE_BITMAP, __pa(vmx_vmwrite_bitmap));
3653 static int vmx_restore_vmx_ept_vpid_cap(struct vcpu_vmx *vmx, u64 data)
3655 u64 vmx_ept_vpid_cap;
3657 vmx_ept_vpid_cap = vmx_control_msr(vmx->nested.msrs.ept_caps,
3658 vmx->nested.msrs.vpid_caps);
3660 /* Every bit is either reserved or a feature bit. */
3661 if (!is_bitwise_subset(vmx_ept_vpid_cap, data, -1ULL))
3664 vmx->nested.msrs.ept_caps = data;
3665 vmx->nested.msrs.vpid_caps = data >> 32;
3669 static int vmx_restore_fixed0_msr(struct vcpu_vmx *vmx, u32 msr_index, u64 data)
3673 switch (msr_index) {
3674 case MSR_IA32_VMX_CR0_FIXED0:
3675 msr = &vmx->nested.msrs.cr0_fixed0;
3677 case MSR_IA32_VMX_CR4_FIXED0:
3678 msr = &vmx->nested.msrs.cr4_fixed0;
3685 * 1 bits (which indicates bits which "must-be-1" during VMX operation)
3686 * must be 1 in the restored value.
3688 if (!is_bitwise_subset(data, *msr, -1ULL))
3696 * Called when userspace is restoring VMX MSRs.
3698 * Returns 0 on success, non-0 otherwise.
3700 static int vmx_set_vmx_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 data)
3702 struct vcpu_vmx *vmx = to_vmx(vcpu);
3705 * Don't allow changes to the VMX capability MSRs while the vCPU
3706 * is in VMX operation.
3708 if (vmx->nested.vmxon)
3711 switch (msr_index) {
3712 case MSR_IA32_VMX_BASIC:
3713 return vmx_restore_vmx_basic(vmx, data);
3714 case MSR_IA32_VMX_PINBASED_CTLS:
3715 case MSR_IA32_VMX_PROCBASED_CTLS:
3716 case MSR_IA32_VMX_EXIT_CTLS:
3717 case MSR_IA32_VMX_ENTRY_CTLS:
3719 * The "non-true" VMX capability MSRs are generated from the
3720 * "true" MSRs, so we do not support restoring them directly.
3722 * If userspace wants to emulate VMX_BASIC[55]=0, userspace
3723 * should restore the "true" MSRs with the must-be-1 bits
3724 * set according to the SDM Vol 3. A.2 "RESERVED CONTROLS AND
3725 * DEFAULT SETTINGS".
3728 case MSR_IA32_VMX_TRUE_PINBASED_CTLS:
3729 case MSR_IA32_VMX_TRUE_PROCBASED_CTLS:
3730 case MSR_IA32_VMX_TRUE_EXIT_CTLS:
3731 case MSR_IA32_VMX_TRUE_ENTRY_CTLS:
3732 case MSR_IA32_VMX_PROCBASED_CTLS2:
3733 return vmx_restore_control_msr(vmx, msr_index, data);
3734 case MSR_IA32_VMX_MISC:
3735 return vmx_restore_vmx_misc(vmx, data);
3736 case MSR_IA32_VMX_CR0_FIXED0:
3737 case MSR_IA32_VMX_CR4_FIXED0:
3738 return vmx_restore_fixed0_msr(vmx, msr_index, data);
3739 case MSR_IA32_VMX_CR0_FIXED1:
3740 case MSR_IA32_VMX_CR4_FIXED1:
3742 * These MSRs are generated based on the vCPU's CPUID, so we
3743 * do not support restoring them directly.
3746 case MSR_IA32_VMX_EPT_VPID_CAP:
3747 return vmx_restore_vmx_ept_vpid_cap(vmx, data);
3748 case MSR_IA32_VMX_VMCS_ENUM:
3749 vmx->nested.msrs.vmcs_enum = data;
3753 * The rest of the VMX capability MSRs do not support restore.
3759 /* Returns 0 on success, non-0 otherwise. */
3760 static int vmx_get_vmx_msr(struct nested_vmx_msrs *msrs, u32 msr_index, u64 *pdata)
3762 switch (msr_index) {
3763 case MSR_IA32_VMX_BASIC:
3764 *pdata = msrs->basic;
3766 case MSR_IA32_VMX_TRUE_PINBASED_CTLS:
3767 case MSR_IA32_VMX_PINBASED_CTLS:
3768 *pdata = vmx_control_msr(
3769 msrs->pinbased_ctls_low,
3770 msrs->pinbased_ctls_high);
3771 if (msr_index == MSR_IA32_VMX_PINBASED_CTLS)
3772 *pdata |= PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
3774 case MSR_IA32_VMX_TRUE_PROCBASED_CTLS:
3775 case MSR_IA32_VMX_PROCBASED_CTLS:
3776 *pdata = vmx_control_msr(
3777 msrs->procbased_ctls_low,
3778 msrs->procbased_ctls_high);
3779 if (msr_index == MSR_IA32_VMX_PROCBASED_CTLS)
3780 *pdata |= CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
3782 case MSR_IA32_VMX_TRUE_EXIT_CTLS:
3783 case MSR_IA32_VMX_EXIT_CTLS:
3784 *pdata = vmx_control_msr(
3785 msrs->exit_ctls_low,
3786 msrs->exit_ctls_high);
3787 if (msr_index == MSR_IA32_VMX_EXIT_CTLS)
3788 *pdata |= VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR;
3790 case MSR_IA32_VMX_TRUE_ENTRY_CTLS:
3791 case MSR_IA32_VMX_ENTRY_CTLS:
3792 *pdata = vmx_control_msr(
3793 msrs->entry_ctls_low,
3794 msrs->entry_ctls_high);
3795 if (msr_index == MSR_IA32_VMX_ENTRY_CTLS)
3796 *pdata |= VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR;
3798 case MSR_IA32_VMX_MISC:
3799 *pdata = vmx_control_msr(
3803 case MSR_IA32_VMX_CR0_FIXED0:
3804 *pdata = msrs->cr0_fixed0;
3806 case MSR_IA32_VMX_CR0_FIXED1:
3807 *pdata = msrs->cr0_fixed1;
3809 case MSR_IA32_VMX_CR4_FIXED0:
3810 *pdata = msrs->cr4_fixed0;
3812 case MSR_IA32_VMX_CR4_FIXED1:
3813 *pdata = msrs->cr4_fixed1;
3815 case MSR_IA32_VMX_VMCS_ENUM:
3816 *pdata = msrs->vmcs_enum;
3818 case MSR_IA32_VMX_PROCBASED_CTLS2:
3819 *pdata = vmx_control_msr(
3820 msrs->secondary_ctls_low,
3821 msrs->secondary_ctls_high);
3823 case MSR_IA32_VMX_EPT_VPID_CAP:
3824 *pdata = msrs->ept_caps |
3825 ((u64)msrs->vpid_caps << 32);
3827 case MSR_IA32_VMX_VMFUNC:
3828 *pdata = msrs->vmfunc_controls;
3837 static inline bool vmx_feature_control_msr_valid(struct kvm_vcpu *vcpu,
3840 uint64_t valid_bits = to_vmx(vcpu)->msr_ia32_feature_control_valid_bits;
3842 return !(val & ~valid_bits);
3845 static int vmx_get_msr_feature(struct kvm_msr_entry *msr)
3847 switch (msr->index) {
3848 case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC:
3851 return vmx_get_vmx_msr(&vmcs_config.nested, msr->index, &msr->data);
3860 * Reads an msr value (of 'msr_index') into 'pdata'.
3861 * Returns 0 on success, non-0 otherwise.
3862 * Assumes vcpu_load() was already called.
3864 static int vmx_get_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
3866 struct vcpu_vmx *vmx = to_vmx(vcpu);
3867 struct shared_msr_entry *msr;
3869 switch (msr_info->index) {
3870 #ifdef CONFIG_X86_64
3872 msr_info->data = vmcs_readl(GUEST_FS_BASE);
3875 msr_info->data = vmcs_readl(GUEST_GS_BASE);
3877 case MSR_KERNEL_GS_BASE:
3878 msr_info->data = vmx_read_guest_kernel_gs_base(vmx);
3882 return kvm_get_msr_common(vcpu, msr_info);
3883 case MSR_IA32_SPEC_CTRL:
3884 if (!msr_info->host_initiated &&
3885 !guest_cpuid_has(vcpu, X86_FEATURE_SPEC_CTRL))
3888 msr_info->data = to_vmx(vcpu)->spec_ctrl;
3890 case MSR_IA32_ARCH_CAPABILITIES:
3891 if (!msr_info->host_initiated &&
3892 !guest_cpuid_has(vcpu, X86_FEATURE_ARCH_CAPABILITIES))
3894 msr_info->data = to_vmx(vcpu)->arch_capabilities;
3896 case MSR_IA32_SYSENTER_CS:
3897 msr_info->data = vmcs_read32(GUEST_SYSENTER_CS);
3899 case MSR_IA32_SYSENTER_EIP:
3900 msr_info->data = vmcs_readl(GUEST_SYSENTER_EIP);
3902 case MSR_IA32_SYSENTER_ESP:
3903 msr_info->data = vmcs_readl(GUEST_SYSENTER_ESP);
3905 case MSR_IA32_BNDCFGS:
3906 if (!kvm_mpx_supported() ||
3907 (!msr_info->host_initiated &&
3908 !guest_cpuid_has(vcpu, X86_FEATURE_MPX)))
3910 msr_info->data = vmcs_read64(GUEST_BNDCFGS);
3912 case MSR_IA32_MCG_EXT_CTL:
3913 if (!msr_info->host_initiated &&
3914 !(vmx->msr_ia32_feature_control &
3915 FEATURE_CONTROL_LMCE))
3917 msr_info->data = vcpu->arch.mcg_ext_ctl;
3919 case MSR_IA32_FEATURE_CONTROL:
3920 msr_info->data = vmx->msr_ia32_feature_control;
3922 case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC:
3923 if (!nested_vmx_allowed(vcpu))
3925 return vmx_get_vmx_msr(&vmx->nested.msrs, msr_info->index,
3928 if (!vmx_xsaves_supported())
3930 msr_info->data = vcpu->arch.ia32_xss;
3933 if (!msr_info->host_initiated &&
3934 !guest_cpuid_has(vcpu, X86_FEATURE_RDTSCP))
3936 /* Otherwise falls through */
3938 msr = find_msr_entry(vmx, msr_info->index);
3940 msr_info->data = msr->data;
3943 return kvm_get_msr_common(vcpu, msr_info);
3949 static void vmx_leave_nested(struct kvm_vcpu *vcpu);
3952 * Writes msr value into into the appropriate "register".
3953 * Returns 0 on success, non-0 otherwise.
3954 * Assumes vcpu_load() was already called.
3956 static int vmx_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
3958 struct vcpu_vmx *vmx = to_vmx(vcpu);
3959 struct shared_msr_entry *msr;
3961 u32 msr_index = msr_info->index;
3962 u64 data = msr_info->data;
3964 switch (msr_index) {
3966 ret = kvm_set_msr_common(vcpu, msr_info);
3968 #ifdef CONFIG_X86_64
3970 vmx_segment_cache_clear(vmx);
3971 vmcs_writel(GUEST_FS_BASE, data);
3974 vmx_segment_cache_clear(vmx);
3975 vmcs_writel(GUEST_GS_BASE, data);
3977 case MSR_KERNEL_GS_BASE:
3978 vmx_write_guest_kernel_gs_base(vmx, data);
3981 case MSR_IA32_SYSENTER_CS:
3982 vmcs_write32(GUEST_SYSENTER_CS, data);
3984 case MSR_IA32_SYSENTER_EIP:
3985 vmcs_writel(GUEST_SYSENTER_EIP, data);
3987 case MSR_IA32_SYSENTER_ESP:
3988 vmcs_writel(GUEST_SYSENTER_ESP, data);
3990 case MSR_IA32_BNDCFGS:
3991 if (!kvm_mpx_supported() ||
3992 (!msr_info->host_initiated &&
3993 !guest_cpuid_has(vcpu, X86_FEATURE_MPX)))
3995 if (is_noncanonical_address(data & PAGE_MASK, vcpu) ||
3996 (data & MSR_IA32_BNDCFGS_RSVD))
3998 vmcs_write64(GUEST_BNDCFGS, data);
4000 case MSR_IA32_SPEC_CTRL:
4001 if (!msr_info->host_initiated &&
4002 !guest_cpuid_has(vcpu, X86_FEATURE_SPEC_CTRL))
4005 /* The STIBP bit doesn't fault even if it's not advertised */
4006 if (data & ~(SPEC_CTRL_IBRS | SPEC_CTRL_STIBP | SPEC_CTRL_SSBD))
4009 vmx->spec_ctrl = data;
4016 * When it's written (to non-zero) for the first time, pass
4020 * The handling of the MSR bitmap for L2 guests is done in
4021 * nested_vmx_merge_msr_bitmap. We should not touch the
4022 * vmcs02.msr_bitmap here since it gets completely overwritten
4023 * in the merging. We update the vmcs01 here for L1 as well
4024 * since it will end up touching the MSR anyway now.
4026 vmx_disable_intercept_for_msr(vmx->vmcs01.msr_bitmap,
4030 case MSR_IA32_PRED_CMD:
4031 if (!msr_info->host_initiated &&
4032 !guest_cpuid_has(vcpu, X86_FEATURE_SPEC_CTRL))
4035 if (data & ~PRED_CMD_IBPB)
4041 wrmsrl(MSR_IA32_PRED_CMD, PRED_CMD_IBPB);
4045 * When it's written (to non-zero) for the first time, pass
4049 * The handling of the MSR bitmap for L2 guests is done in
4050 * nested_vmx_merge_msr_bitmap. We should not touch the
4051 * vmcs02.msr_bitmap here since it gets completely overwritten
4054 vmx_disable_intercept_for_msr(vmx->vmcs01.msr_bitmap, MSR_IA32_PRED_CMD,
4057 case MSR_IA32_ARCH_CAPABILITIES:
4058 if (!msr_info->host_initiated)
4060 vmx->arch_capabilities = data;
4062 case MSR_IA32_CR_PAT:
4063 if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) {
4064 if (!kvm_mtrr_valid(vcpu, MSR_IA32_CR_PAT, data))
4066 vmcs_write64(GUEST_IA32_PAT, data);
4067 vcpu->arch.pat = data;
4070 ret = kvm_set_msr_common(vcpu, msr_info);
4072 case MSR_IA32_TSC_ADJUST:
4073 ret = kvm_set_msr_common(vcpu, msr_info);
4075 case MSR_IA32_MCG_EXT_CTL:
4076 if ((!msr_info->host_initiated &&
4077 !(to_vmx(vcpu)->msr_ia32_feature_control &
4078 FEATURE_CONTROL_LMCE)) ||
4079 (data & ~MCG_EXT_CTL_LMCE_EN))
4081 vcpu->arch.mcg_ext_ctl = data;
4083 case MSR_IA32_FEATURE_CONTROL:
4084 if (!vmx_feature_control_msr_valid(vcpu, data) ||
4085 (to_vmx(vcpu)->msr_ia32_feature_control &
4086 FEATURE_CONTROL_LOCKED && !msr_info->host_initiated))
4088 vmx->msr_ia32_feature_control = data;
4089 if (msr_info->host_initiated && data == 0)
4090 vmx_leave_nested(vcpu);
4092 case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC:
4093 if (!msr_info->host_initiated)
4094 return 1; /* they are read-only */
4095 if (!nested_vmx_allowed(vcpu))
4097 return vmx_set_vmx_msr(vcpu, msr_index, data);
4099 if (!vmx_xsaves_supported())
4102 * The only supported bit as of Skylake is bit 8, but
4103 * it is not supported on KVM.
4107 vcpu->arch.ia32_xss = data;
4108 if (vcpu->arch.ia32_xss != host_xss)
4109 add_atomic_switch_msr(vmx, MSR_IA32_XSS,
4110 vcpu->arch.ia32_xss, host_xss);
4112 clear_atomic_switch_msr(vmx, MSR_IA32_XSS);
4115 if (!msr_info->host_initiated &&
4116 !guest_cpuid_has(vcpu, X86_FEATURE_RDTSCP))
4118 /* Check reserved bit, higher 32 bits should be zero */
4119 if ((data >> 32) != 0)
4121 /* Otherwise falls through */
4123 msr = find_msr_entry(vmx, msr_index);
4125 u64 old_msr_data = msr->data;
4127 if (msr - vmx->guest_msrs < vmx->save_nmsrs) {
4129 ret = kvm_set_shared_msr(msr->index, msr->data,
4133 msr->data = old_msr_data;
4137 ret = kvm_set_msr_common(vcpu, msr_info);
4143 static void vmx_cache_reg(struct kvm_vcpu *vcpu, enum kvm_reg reg)
4145 __set_bit(reg, (unsigned long *)&vcpu->arch.regs_avail);
4148 vcpu->arch.regs[VCPU_REGS_RSP] = vmcs_readl(GUEST_RSP);
4151 vcpu->arch.regs[VCPU_REGS_RIP] = vmcs_readl(GUEST_RIP);
4153 case VCPU_EXREG_PDPTR:
4155 ept_save_pdptrs(vcpu);
4162 static __init int cpu_has_kvm_support(void)
4164 return cpu_has_vmx();
4167 static __init int vmx_disabled_by_bios(void)
4171 rdmsrl(MSR_IA32_FEATURE_CONTROL, msr);
4172 if (msr & FEATURE_CONTROL_LOCKED) {
4173 /* launched w/ TXT and VMX disabled */
4174 if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX)
4177 /* launched w/o TXT and VMX only enabled w/ TXT */
4178 if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX)
4179 && (msr & FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX)
4180 && !tboot_enabled()) {
4181 printk(KERN_WARNING "kvm: disable TXT in the BIOS or "
4182 "activate TXT before enabling KVM\n");
4185 /* launched w/o TXT and VMX disabled */
4186 if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX)
4187 && !tboot_enabled())
4194 static void kvm_cpu_vmxon(u64 addr)
4196 cr4_set_bits(X86_CR4_VMXE);
4197 intel_pt_handle_vmx(1);
4199 asm volatile (ASM_VMX_VMXON_RAX
4200 : : "a"(&addr), "m"(addr)
4204 static int hardware_enable(void)
4206 int cpu = raw_smp_processor_id();
4207 u64 phys_addr = __pa(per_cpu(vmxarea, cpu));
4210 if (cr4_read_shadow() & X86_CR4_VMXE)
4214 * This can happen if we hot-added a CPU but failed to allocate
4215 * VP assist page for it.
4217 if (static_branch_unlikely(&enable_evmcs) &&
4218 !hv_get_vp_assist_page(cpu))
4221 INIT_LIST_HEAD(&per_cpu(loaded_vmcss_on_cpu, cpu));
4222 INIT_LIST_HEAD(&per_cpu(blocked_vcpu_on_cpu, cpu));
4223 spin_lock_init(&per_cpu(blocked_vcpu_on_cpu_lock, cpu));
4226 * Now we can enable the vmclear operation in kdump
4227 * since the loaded_vmcss_on_cpu list on this cpu
4228 * has been initialized.
4230 * Though the cpu is not in VMX operation now, there
4231 * is no problem to enable the vmclear operation
4232 * for the loaded_vmcss_on_cpu list is empty!
4234 crash_enable_local_vmclear(cpu);
4236 rdmsrl(MSR_IA32_FEATURE_CONTROL, old);
4238 test_bits = FEATURE_CONTROL_LOCKED;
4239 test_bits |= FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
4240 if (tboot_enabled())
4241 test_bits |= FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX;
4243 if ((old & test_bits) != test_bits) {
4244 /* enable and lock */
4245 wrmsrl(MSR_IA32_FEATURE_CONTROL, old | test_bits);
4247 kvm_cpu_vmxon(phys_addr);
4254 static void vmclear_local_loaded_vmcss(void)
4256 int cpu = raw_smp_processor_id();
4257 struct loaded_vmcs *v, *n;
4259 list_for_each_entry_safe(v, n, &per_cpu(loaded_vmcss_on_cpu, cpu),
4260 loaded_vmcss_on_cpu_link)
4261 __loaded_vmcs_clear(v);
4265 /* Just like cpu_vmxoff(), but with the __kvm_handle_fault_on_reboot()
4268 static void kvm_cpu_vmxoff(void)
4270 asm volatile (__ex(ASM_VMX_VMXOFF) : : : "cc");
4272 intel_pt_handle_vmx(0);
4273 cr4_clear_bits(X86_CR4_VMXE);
4276 static void hardware_disable(void)
4278 vmclear_local_loaded_vmcss();
4282 static __init int adjust_vmx_controls(u32 ctl_min, u32 ctl_opt,
4283 u32 msr, u32 *result)
4285 u32 vmx_msr_low, vmx_msr_high;
4286 u32 ctl = ctl_min | ctl_opt;
4288 rdmsr(msr, vmx_msr_low, vmx_msr_high);
4290 ctl &= vmx_msr_high; /* bit == 0 in high word ==> must be zero */
4291 ctl |= vmx_msr_low; /* bit == 1 in low word ==> must be one */
4293 /* Ensure minimum (required) set of control bits are supported. */
4301 static __init bool allow_1_setting(u32 msr, u32 ctl)
4303 u32 vmx_msr_low, vmx_msr_high;
4305 rdmsr(msr, vmx_msr_low, vmx_msr_high);
4306 return vmx_msr_high & ctl;
4309 static __init int setup_vmcs_config(struct vmcs_config *vmcs_conf)
4311 u32 vmx_msr_low, vmx_msr_high;
4312 u32 min, opt, min2, opt2;
4313 u32 _pin_based_exec_control = 0;
4314 u32 _cpu_based_exec_control = 0;
4315 u32 _cpu_based_2nd_exec_control = 0;
4316 u32 _vmexit_control = 0;
4317 u32 _vmentry_control = 0;
4319 memset(vmcs_conf, 0, sizeof(*vmcs_conf));
4320 min = CPU_BASED_HLT_EXITING |
4321 #ifdef CONFIG_X86_64
4322 CPU_BASED_CR8_LOAD_EXITING |
4323 CPU_BASED_CR8_STORE_EXITING |
4325 CPU_BASED_CR3_LOAD_EXITING |
4326 CPU_BASED_CR3_STORE_EXITING |
4327 CPU_BASED_UNCOND_IO_EXITING |
4328 CPU_BASED_MOV_DR_EXITING |
4329 CPU_BASED_USE_TSC_OFFSETING |
4330 CPU_BASED_MWAIT_EXITING |
4331 CPU_BASED_MONITOR_EXITING |
4332 CPU_BASED_INVLPG_EXITING |
4333 CPU_BASED_RDPMC_EXITING;
4335 opt = CPU_BASED_TPR_SHADOW |
4336 CPU_BASED_USE_MSR_BITMAPS |
4337 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
4338 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PROCBASED_CTLS,
4339 &_cpu_based_exec_control) < 0)
4341 #ifdef CONFIG_X86_64
4342 if ((_cpu_based_exec_control & CPU_BASED_TPR_SHADOW))
4343 _cpu_based_exec_control &= ~CPU_BASED_CR8_LOAD_EXITING &
4344 ~CPU_BASED_CR8_STORE_EXITING;
4346 if (_cpu_based_exec_control & CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) {
4348 opt2 = SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
4349 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
4350 SECONDARY_EXEC_WBINVD_EXITING |
4351 SECONDARY_EXEC_ENABLE_VPID |
4352 SECONDARY_EXEC_ENABLE_EPT |
4353 SECONDARY_EXEC_UNRESTRICTED_GUEST |
4354 SECONDARY_EXEC_PAUSE_LOOP_EXITING |
4355 SECONDARY_EXEC_DESC |
4356 SECONDARY_EXEC_RDTSCP |
4357 SECONDARY_EXEC_ENABLE_INVPCID |
4358 SECONDARY_EXEC_APIC_REGISTER_VIRT |
4359 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY |
4360 SECONDARY_EXEC_SHADOW_VMCS |
4361 SECONDARY_EXEC_XSAVES |
4362 SECONDARY_EXEC_RDSEED_EXITING |
4363 SECONDARY_EXEC_RDRAND_EXITING |
4364 SECONDARY_EXEC_ENABLE_PML |
4365 SECONDARY_EXEC_TSC_SCALING |
4366 SECONDARY_EXEC_ENABLE_VMFUNC;
4367 if (adjust_vmx_controls(min2, opt2,
4368 MSR_IA32_VMX_PROCBASED_CTLS2,
4369 &_cpu_based_2nd_exec_control) < 0)
4372 #ifndef CONFIG_X86_64
4373 if (!(_cpu_based_2nd_exec_control &
4374 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES))
4375 _cpu_based_exec_control &= ~CPU_BASED_TPR_SHADOW;
4378 if (!(_cpu_based_exec_control & CPU_BASED_TPR_SHADOW))
4379 _cpu_based_2nd_exec_control &= ~(
4380 SECONDARY_EXEC_APIC_REGISTER_VIRT |
4381 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
4382 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
4384 rdmsr_safe(MSR_IA32_VMX_EPT_VPID_CAP,
4385 &vmx_capability.ept, &vmx_capability.vpid);
4387 if (_cpu_based_2nd_exec_control & SECONDARY_EXEC_ENABLE_EPT) {
4388 /* CR3 accesses and invlpg don't need to cause VM Exits when EPT
4390 _cpu_based_exec_control &= ~(CPU_BASED_CR3_LOAD_EXITING |
4391 CPU_BASED_CR3_STORE_EXITING |
4392 CPU_BASED_INVLPG_EXITING);
4393 } else if (vmx_capability.ept) {
4394 vmx_capability.ept = 0;
4395 pr_warn_once("EPT CAP should not exist if not support "
4396 "1-setting enable EPT VM-execution control\n");
4398 if (!(_cpu_based_2nd_exec_control & SECONDARY_EXEC_ENABLE_VPID) &&
4399 vmx_capability.vpid) {
4400 vmx_capability.vpid = 0;
4401 pr_warn_once("VPID CAP should not exist if not support "
4402 "1-setting enable VPID VM-execution control\n");
4405 min = VM_EXIT_SAVE_DEBUG_CONTROLS | VM_EXIT_ACK_INTR_ON_EXIT;
4406 #ifdef CONFIG_X86_64
4407 min |= VM_EXIT_HOST_ADDR_SPACE_SIZE;
4409 opt = VM_EXIT_SAVE_IA32_PAT | VM_EXIT_LOAD_IA32_PAT |
4410 VM_EXIT_CLEAR_BNDCFGS;
4411 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_EXIT_CTLS,
4412 &_vmexit_control) < 0)
4415 min = PIN_BASED_EXT_INTR_MASK | PIN_BASED_NMI_EXITING;
4416 opt = PIN_BASED_VIRTUAL_NMIS | PIN_BASED_POSTED_INTR |
4417 PIN_BASED_VMX_PREEMPTION_TIMER;
4418 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PINBASED_CTLS,
4419 &_pin_based_exec_control) < 0)
4422 if (cpu_has_broken_vmx_preemption_timer())
4423 _pin_based_exec_control &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
4424 if (!(_cpu_based_2nd_exec_control &
4425 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY))
4426 _pin_based_exec_control &= ~PIN_BASED_POSTED_INTR;
4428 min = VM_ENTRY_LOAD_DEBUG_CONTROLS;
4429 opt = VM_ENTRY_LOAD_IA32_PAT | VM_ENTRY_LOAD_BNDCFGS;
4430 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_ENTRY_CTLS,
4431 &_vmentry_control) < 0)
4434 rdmsr(MSR_IA32_VMX_BASIC, vmx_msr_low, vmx_msr_high);
4436 /* IA-32 SDM Vol 3B: VMCS size is never greater than 4kB. */
4437 if ((vmx_msr_high & 0x1fff) > PAGE_SIZE)
4440 #ifdef CONFIG_X86_64
4441 /* IA-32 SDM Vol 3B: 64-bit CPUs always have VMX_BASIC_MSR[48]==0. */
4442 if (vmx_msr_high & (1u<<16))
4446 /* Require Write-Back (WB) memory type for VMCS accesses. */
4447 if (((vmx_msr_high >> 18) & 15) != 6)
4450 vmcs_conf->size = vmx_msr_high & 0x1fff;
4451 vmcs_conf->order = get_order(vmcs_conf->size);
4452 vmcs_conf->basic_cap = vmx_msr_high & ~0x1fff;
4454 vmcs_conf->revision_id = vmx_msr_low;
4456 vmcs_conf->pin_based_exec_ctrl = _pin_based_exec_control;
4457 vmcs_conf->cpu_based_exec_ctrl = _cpu_based_exec_control;
4458 vmcs_conf->cpu_based_2nd_exec_ctrl = _cpu_based_2nd_exec_control;
4459 vmcs_conf->vmexit_ctrl = _vmexit_control;
4460 vmcs_conf->vmentry_ctrl = _vmentry_control;
4462 if (static_branch_unlikely(&enable_evmcs))
4463 evmcs_sanitize_exec_ctrls(vmcs_conf);
4465 cpu_has_load_ia32_efer =
4466 allow_1_setting(MSR_IA32_VMX_ENTRY_CTLS,
4467 VM_ENTRY_LOAD_IA32_EFER)
4468 && allow_1_setting(MSR_IA32_VMX_EXIT_CTLS,
4469 VM_EXIT_LOAD_IA32_EFER);
4471 cpu_has_load_perf_global_ctrl =
4472 allow_1_setting(MSR_IA32_VMX_ENTRY_CTLS,
4473 VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL)
4474 && allow_1_setting(MSR_IA32_VMX_EXIT_CTLS,
4475 VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL);
4478 * Some cpus support VM_ENTRY_(LOAD|SAVE)_IA32_PERF_GLOBAL_CTRL
4479 * but due to errata below it can't be used. Workaround is to use
4480 * msr load mechanism to switch IA32_PERF_GLOBAL_CTRL.
4482 * VM Exit May Incorrectly Clear IA32_PERF_GLOBAL_CTRL [34:32]
4487 * BC86,AAY89,BD102 (model 44)
4491 if (cpu_has_load_perf_global_ctrl && boot_cpu_data.x86 == 0x6) {
4492 switch (boot_cpu_data.x86_model) {
4498 cpu_has_load_perf_global_ctrl = false;
4499 printk_once(KERN_WARNING"kvm: VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL "
4500 "does not work properly. Using workaround\n");
4507 if (boot_cpu_has(X86_FEATURE_XSAVES))
4508 rdmsrl(MSR_IA32_XSS, host_xss);
4513 static struct vmcs *alloc_vmcs_cpu(bool shadow, int cpu)
4515 int node = cpu_to_node(cpu);
4519 pages = __alloc_pages_node(node, GFP_KERNEL, vmcs_config.order);
4522 vmcs = page_address(pages);
4523 memset(vmcs, 0, vmcs_config.size);
4525 /* KVM supports Enlightened VMCS v1 only */
4526 if (static_branch_unlikely(&enable_evmcs))
4527 vmcs->hdr.revision_id = KVM_EVMCS_VERSION;
4529 vmcs->hdr.revision_id = vmcs_config.revision_id;
4532 vmcs->hdr.shadow_vmcs = 1;
4536 static void free_vmcs(struct vmcs *vmcs)
4538 free_pages((unsigned long)vmcs, vmcs_config.order);
4542 * Free a VMCS, but before that VMCLEAR it on the CPU where it was last loaded
4544 static void free_loaded_vmcs(struct loaded_vmcs *loaded_vmcs)
4546 if (!loaded_vmcs->vmcs)
4548 loaded_vmcs_clear(loaded_vmcs);
4549 free_vmcs(loaded_vmcs->vmcs);
4550 loaded_vmcs->vmcs = NULL;
4551 if (loaded_vmcs->msr_bitmap)
4552 free_page((unsigned long)loaded_vmcs->msr_bitmap);
4553 WARN_ON(loaded_vmcs->shadow_vmcs != NULL);
4556 static struct vmcs *alloc_vmcs(bool shadow)
4558 return alloc_vmcs_cpu(shadow, raw_smp_processor_id());
4561 static int alloc_loaded_vmcs(struct loaded_vmcs *loaded_vmcs)
4563 loaded_vmcs->vmcs = alloc_vmcs(false);
4564 if (!loaded_vmcs->vmcs)
4567 loaded_vmcs->shadow_vmcs = NULL;
4568 loaded_vmcs_init(loaded_vmcs);
4570 if (cpu_has_vmx_msr_bitmap()) {
4571 loaded_vmcs->msr_bitmap = (unsigned long *)__get_free_page(GFP_KERNEL);
4572 if (!loaded_vmcs->msr_bitmap)
4574 memset(loaded_vmcs->msr_bitmap, 0xff, PAGE_SIZE);
4576 if (IS_ENABLED(CONFIG_HYPERV) &&
4577 static_branch_unlikely(&enable_evmcs) &&
4578 (ms_hyperv.nested_features & HV_X64_NESTED_MSR_BITMAP)) {
4579 struct hv_enlightened_vmcs *evmcs =
4580 (struct hv_enlightened_vmcs *)loaded_vmcs->vmcs;
4582 evmcs->hv_enlightenments_control.msr_bitmap = 1;
4588 free_loaded_vmcs(loaded_vmcs);
4592 static void free_kvm_area(void)
4596 for_each_possible_cpu(cpu) {
4597 free_vmcs(per_cpu(vmxarea, cpu));
4598 per_cpu(vmxarea, cpu) = NULL;
4602 enum vmcs_field_width {
4603 VMCS_FIELD_WIDTH_U16 = 0,
4604 VMCS_FIELD_WIDTH_U64 = 1,
4605 VMCS_FIELD_WIDTH_U32 = 2,
4606 VMCS_FIELD_WIDTH_NATURAL_WIDTH = 3
4609 static inline int vmcs_field_width(unsigned long field)
4611 if (0x1 & field) /* the *_HIGH fields are all 32 bit */
4612 return VMCS_FIELD_WIDTH_U32;
4613 return (field >> 13) & 0x3 ;
4616 static inline int vmcs_field_readonly(unsigned long field)
4618 return (((field >> 10) & 0x3) == 1);
4621 static void init_vmcs_shadow_fields(void)
4625 for (i = j = 0; i < max_shadow_read_only_fields; i++) {
4626 u16 field = shadow_read_only_fields[i];
4627 if (vmcs_field_width(field) == VMCS_FIELD_WIDTH_U64 &&
4628 (i + 1 == max_shadow_read_only_fields ||
4629 shadow_read_only_fields[i + 1] != field + 1))
4630 pr_err("Missing field from shadow_read_only_field %x\n",
4633 clear_bit(field, vmx_vmread_bitmap);
4634 #ifdef CONFIG_X86_64
4639 shadow_read_only_fields[j] = field;
4642 max_shadow_read_only_fields = j;
4644 for (i = j = 0; i < max_shadow_read_write_fields; i++) {
4645 u16 field = shadow_read_write_fields[i];
4646 if (vmcs_field_width(field) == VMCS_FIELD_WIDTH_U64 &&
4647 (i + 1 == max_shadow_read_write_fields ||
4648 shadow_read_write_fields[i + 1] != field + 1))
4649 pr_err("Missing field from shadow_read_write_field %x\n",
4653 * PML and the preemption timer can be emulated, but the
4654 * processor cannot vmwrite to fields that don't exist
4658 case GUEST_PML_INDEX:
4659 if (!cpu_has_vmx_pml())
4662 case VMX_PREEMPTION_TIMER_VALUE:
4663 if (!cpu_has_vmx_preemption_timer())
4666 case GUEST_INTR_STATUS:
4667 if (!cpu_has_vmx_apicv())
4674 clear_bit(field, vmx_vmwrite_bitmap);
4675 clear_bit(field, vmx_vmread_bitmap);
4676 #ifdef CONFIG_X86_64
4681 shadow_read_write_fields[j] = field;
4684 max_shadow_read_write_fields = j;
4687 static __init int alloc_kvm_area(void)
4691 for_each_possible_cpu(cpu) {
4694 vmcs = alloc_vmcs_cpu(false, cpu);
4701 * When eVMCS is enabled, alloc_vmcs_cpu() sets
4702 * vmcs->revision_id to KVM_EVMCS_VERSION instead of
4703 * revision_id reported by MSR_IA32_VMX_BASIC.
4705 * However, even though not explictly documented by
4706 * TLFS, VMXArea passed as VMXON argument should
4707 * still be marked with revision_id reported by
4710 if (static_branch_unlikely(&enable_evmcs))
4711 vmcs->hdr.revision_id = vmcs_config.revision_id;
4713 per_cpu(vmxarea, cpu) = vmcs;
4718 static void fix_pmode_seg(struct kvm_vcpu *vcpu, int seg,
4719 struct kvm_segment *save)
4721 if (!emulate_invalid_guest_state) {
4723 * CS and SS RPL should be equal during guest entry according
4724 * to VMX spec, but in reality it is not always so. Since vcpu
4725 * is in the middle of the transition from real mode to
4726 * protected mode it is safe to assume that RPL 0 is a good
4729 if (seg == VCPU_SREG_CS || seg == VCPU_SREG_SS)
4730 save->selector &= ~SEGMENT_RPL_MASK;
4731 save->dpl = save->selector & SEGMENT_RPL_MASK;
4734 vmx_set_segment(vcpu, save, seg);
4737 static void enter_pmode(struct kvm_vcpu *vcpu)
4739 unsigned long flags;
4740 struct vcpu_vmx *vmx = to_vmx(vcpu);
4743 * Update real mode segment cache. It may be not up-to-date if sement
4744 * register was written while vcpu was in a guest mode.
4746 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_ES], VCPU_SREG_ES);
4747 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_DS], VCPU_SREG_DS);
4748 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_FS], VCPU_SREG_FS);
4749 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_GS], VCPU_SREG_GS);
4750 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_SS], VCPU_SREG_SS);
4751 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_CS], VCPU_SREG_CS);
4753 vmx->rmode.vm86_active = 0;
4755 vmx_segment_cache_clear(vmx);
4757 vmx_set_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_TR], VCPU_SREG_TR);
4759 flags = vmcs_readl(GUEST_RFLAGS);
4760 flags &= RMODE_GUEST_OWNED_EFLAGS_BITS;
4761 flags |= vmx->rmode.save_rflags & ~RMODE_GUEST_OWNED_EFLAGS_BITS;
4762 vmcs_writel(GUEST_RFLAGS, flags);
4764 vmcs_writel(GUEST_CR4, (vmcs_readl(GUEST_CR4) & ~X86_CR4_VME) |
4765 (vmcs_readl(CR4_READ_SHADOW) & X86_CR4_VME));
4767 update_exception_bitmap(vcpu);
4769 fix_pmode_seg(vcpu, VCPU_SREG_CS, &vmx->rmode.segs[VCPU_SREG_CS]);
4770 fix_pmode_seg(vcpu, VCPU_SREG_SS, &vmx->rmode.segs[VCPU_SREG_SS]);
4771 fix_pmode_seg(vcpu, VCPU_SREG_ES, &vmx->rmode.segs[VCPU_SREG_ES]);
4772 fix_pmode_seg(vcpu, VCPU_SREG_DS, &vmx->rmode.segs[VCPU_SREG_DS]);
4773 fix_pmode_seg(vcpu, VCPU_SREG_FS, &vmx->rmode.segs[VCPU_SREG_FS]);
4774 fix_pmode_seg(vcpu, VCPU_SREG_GS, &vmx->rmode.segs[VCPU_SREG_GS]);
4777 static void fix_rmode_seg(int seg, struct kvm_segment *save)
4779 const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
4780 struct kvm_segment var = *save;
4783 if (seg == VCPU_SREG_CS)
4786 if (!emulate_invalid_guest_state) {
4787 var.selector = var.base >> 4;
4788 var.base = var.base & 0xffff0;
4798 if (save->base & 0xf)
4799 printk_once(KERN_WARNING "kvm: segment base is not "
4800 "paragraph aligned when entering "
4801 "protected mode (seg=%d)", seg);
4804 vmcs_write16(sf->selector, var.selector);
4805 vmcs_writel(sf->base, var.base);
4806 vmcs_write32(sf->limit, var.limit);
4807 vmcs_write32(sf->ar_bytes, vmx_segment_access_rights(&var));
4810 static void enter_rmode(struct kvm_vcpu *vcpu)
4812 unsigned long flags;
4813 struct vcpu_vmx *vmx = to_vmx(vcpu);
4814 struct kvm_vmx *kvm_vmx = to_kvm_vmx(vcpu->kvm);
4816 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_TR], VCPU_SREG_TR);
4817 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_ES], VCPU_SREG_ES);
4818 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_DS], VCPU_SREG_DS);
4819 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_FS], VCPU_SREG_FS);
4820 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_GS], VCPU_SREG_GS);
4821 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_SS], VCPU_SREG_SS);
4822 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_CS], VCPU_SREG_CS);
4824 vmx->rmode.vm86_active = 1;
4827 * Very old userspace does not call KVM_SET_TSS_ADDR before entering
4828 * vcpu. Warn the user that an update is overdue.
4830 if (!kvm_vmx->tss_addr)
4831 printk_once(KERN_WARNING "kvm: KVM_SET_TSS_ADDR need to be "
4832 "called before entering vcpu\n");
4834 vmx_segment_cache_clear(vmx);
4836 vmcs_writel(GUEST_TR_BASE, kvm_vmx->tss_addr);
4837 vmcs_write32(GUEST_TR_LIMIT, RMODE_TSS_SIZE - 1);
4838 vmcs_write32(GUEST_TR_AR_BYTES, 0x008b);
4840 flags = vmcs_readl(GUEST_RFLAGS);
4841 vmx->rmode.save_rflags = flags;
4843 flags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM;
4845 vmcs_writel(GUEST_RFLAGS, flags);
4846 vmcs_writel(GUEST_CR4, vmcs_readl(GUEST_CR4) | X86_CR4_VME);
4847 update_exception_bitmap(vcpu);
4849 fix_rmode_seg(VCPU_SREG_SS, &vmx->rmode.segs[VCPU_SREG_SS]);
4850 fix_rmode_seg(VCPU_SREG_CS, &vmx->rmode.segs[VCPU_SREG_CS]);
4851 fix_rmode_seg(VCPU_SREG_ES, &vmx->rmode.segs[VCPU_SREG_ES]);
4852 fix_rmode_seg(VCPU_SREG_DS, &vmx->rmode.segs[VCPU_SREG_DS]);
4853 fix_rmode_seg(VCPU_SREG_GS, &vmx->rmode.segs[VCPU_SREG_GS]);
4854 fix_rmode_seg(VCPU_SREG_FS, &vmx->rmode.segs[VCPU_SREG_FS]);
4856 kvm_mmu_reset_context(vcpu);
4859 static void vmx_set_efer(struct kvm_vcpu *vcpu, u64 efer)
4861 struct vcpu_vmx *vmx = to_vmx(vcpu);
4862 struct shared_msr_entry *msr = find_msr_entry(vmx, MSR_EFER);
4868 * MSR_KERNEL_GS_BASE is not intercepted when the guest is in
4869 * 64-bit mode as a 64-bit kernel may frequently access the
4870 * MSR. This means we need to manually save/restore the MSR
4871 * when switching between guest and host state, but only if
4872 * the guest is in 64-bit mode. Sync our cached value if the
4873 * guest is transitioning to 32-bit mode and the CPU contains
4874 * guest state, i.e. the cache is stale.
4876 #ifdef CONFIG_X86_64
4877 if (!(efer & EFER_LMA))
4878 (void)vmx_read_guest_kernel_gs_base(vmx);
4880 vcpu->arch.efer = efer;
4881 if (efer & EFER_LMA) {
4882 vm_entry_controls_setbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
4885 vm_entry_controls_clearbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
4887 msr->data = efer & ~EFER_LME;
4892 #ifdef CONFIG_X86_64
4894 static void enter_lmode(struct kvm_vcpu *vcpu)
4898 vmx_segment_cache_clear(to_vmx(vcpu));
4900 guest_tr_ar = vmcs_read32(GUEST_TR_AR_BYTES);
4901 if ((guest_tr_ar & VMX_AR_TYPE_MASK) != VMX_AR_TYPE_BUSY_64_TSS) {
4902 pr_debug_ratelimited("%s: tss fixup for long mode. \n",
4904 vmcs_write32(GUEST_TR_AR_BYTES,
4905 (guest_tr_ar & ~VMX_AR_TYPE_MASK)
4906 | VMX_AR_TYPE_BUSY_64_TSS);
4908 vmx_set_efer(vcpu, vcpu->arch.efer | EFER_LMA);
4911 static void exit_lmode(struct kvm_vcpu *vcpu)
4913 vm_entry_controls_clearbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
4914 vmx_set_efer(vcpu, vcpu->arch.efer & ~EFER_LMA);
4919 static inline void __vmx_flush_tlb(struct kvm_vcpu *vcpu, int vpid,
4920 bool invalidate_gpa)
4922 if (enable_ept && (invalidate_gpa || !enable_vpid)) {
4923 if (!VALID_PAGE(vcpu->arch.mmu.root_hpa))
4925 ept_sync_context(construct_eptp(vcpu, vcpu->arch.mmu.root_hpa));
4927 vpid_sync_context(vpid);
4931 static void vmx_flush_tlb(struct kvm_vcpu *vcpu, bool invalidate_gpa)
4933 __vmx_flush_tlb(vcpu, to_vmx(vcpu)->vpid, invalidate_gpa);
4936 static void vmx_flush_tlb_gva(struct kvm_vcpu *vcpu, gva_t addr)
4938 int vpid = to_vmx(vcpu)->vpid;
4940 if (!vpid_sync_vcpu_addr(vpid, addr))
4941 vpid_sync_context(vpid);
4944 * If VPIDs are not supported or enabled, then the above is a no-op.
4945 * But we don't really need a TLB flush in that case anyway, because
4946 * each VM entry/exit includes an implicit flush when VPID is 0.
4950 static void vmx_decache_cr0_guest_bits(struct kvm_vcpu *vcpu)
4952 ulong cr0_guest_owned_bits = vcpu->arch.cr0_guest_owned_bits;
4954 vcpu->arch.cr0 &= ~cr0_guest_owned_bits;
4955 vcpu->arch.cr0 |= vmcs_readl(GUEST_CR0) & cr0_guest_owned_bits;
4958 static void vmx_decache_cr3(struct kvm_vcpu *vcpu)
4960 if (enable_unrestricted_guest || (enable_ept && is_paging(vcpu)))
4961 vcpu->arch.cr3 = vmcs_readl(GUEST_CR3);
4962 __set_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail);
4965 static void vmx_decache_cr4_guest_bits(struct kvm_vcpu *vcpu)
4967 ulong cr4_guest_owned_bits = vcpu->arch.cr4_guest_owned_bits;
4969 vcpu->arch.cr4 &= ~cr4_guest_owned_bits;
4970 vcpu->arch.cr4 |= vmcs_readl(GUEST_CR4) & cr4_guest_owned_bits;
4973 static void ept_load_pdptrs(struct kvm_vcpu *vcpu)
4975 struct kvm_mmu *mmu = vcpu->arch.walk_mmu;
4977 if (!test_bit(VCPU_EXREG_PDPTR,
4978 (unsigned long *)&vcpu->arch.regs_dirty))
4981 if (is_paging(vcpu) && is_pae(vcpu) && !is_long_mode(vcpu)) {
4982 vmcs_write64(GUEST_PDPTR0, mmu->pdptrs[0]);
4983 vmcs_write64(GUEST_PDPTR1, mmu->pdptrs[1]);
4984 vmcs_write64(GUEST_PDPTR2, mmu->pdptrs[2]);
4985 vmcs_write64(GUEST_PDPTR3, mmu->pdptrs[3]);
4989 static void ept_save_pdptrs(struct kvm_vcpu *vcpu)
4991 struct kvm_mmu *mmu = vcpu->arch.walk_mmu;
4993 if (is_paging(vcpu) && is_pae(vcpu) && !is_long_mode(vcpu)) {
4994 mmu->pdptrs[0] = vmcs_read64(GUEST_PDPTR0);
4995 mmu->pdptrs[1] = vmcs_read64(GUEST_PDPTR1);
4996 mmu->pdptrs[2] = vmcs_read64(GUEST_PDPTR2);
4997 mmu->pdptrs[3] = vmcs_read64(GUEST_PDPTR3);
5000 __set_bit(VCPU_EXREG_PDPTR,
5001 (unsigned long *)&vcpu->arch.regs_avail);
5002 __set_bit(VCPU_EXREG_PDPTR,
5003 (unsigned long *)&vcpu->arch.regs_dirty);
5006 static bool nested_guest_cr0_valid(struct kvm_vcpu *vcpu, unsigned long val)
5008 u64 fixed0 = to_vmx(vcpu)->nested.msrs.cr0_fixed0;
5009 u64 fixed1 = to_vmx(vcpu)->nested.msrs.cr0_fixed1;
5010 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
5012 if (to_vmx(vcpu)->nested.msrs.secondary_ctls_high &
5013 SECONDARY_EXEC_UNRESTRICTED_GUEST &&
5014 nested_cpu_has2(vmcs12, SECONDARY_EXEC_UNRESTRICTED_GUEST))
5015 fixed0 &= ~(X86_CR0_PE | X86_CR0_PG);
5017 return fixed_bits_valid(val, fixed0, fixed1);
5020 static bool nested_host_cr0_valid(struct kvm_vcpu *vcpu, unsigned long val)
5022 u64 fixed0 = to_vmx(vcpu)->nested.msrs.cr0_fixed0;
5023 u64 fixed1 = to_vmx(vcpu)->nested.msrs.cr0_fixed1;
5025 return fixed_bits_valid(val, fixed0, fixed1);
5028 static bool nested_cr4_valid(struct kvm_vcpu *vcpu, unsigned long val)
5030 u64 fixed0 = to_vmx(vcpu)->nested.msrs.cr4_fixed0;
5031 u64 fixed1 = to_vmx(vcpu)->nested.msrs.cr4_fixed1;
5033 return fixed_bits_valid(val, fixed0, fixed1);
5036 /* No difference in the restrictions on guest and host CR4 in VMX operation. */
5037 #define nested_guest_cr4_valid nested_cr4_valid
5038 #define nested_host_cr4_valid nested_cr4_valid
5040 static int vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4);
5042 static void ept_update_paging_mode_cr0(unsigned long *hw_cr0,
5044 struct kvm_vcpu *vcpu)
5046 if (!test_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail))
5047 vmx_decache_cr3(vcpu);
5048 if (!(cr0 & X86_CR0_PG)) {
5049 /* From paging/starting to nonpaging */
5050 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL,
5051 vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) |
5052 (CPU_BASED_CR3_LOAD_EXITING |
5053 CPU_BASED_CR3_STORE_EXITING));
5054 vcpu->arch.cr0 = cr0;
5055 vmx_set_cr4(vcpu, kvm_read_cr4(vcpu));
5056 } else if (!is_paging(vcpu)) {
5057 /* From nonpaging to paging */
5058 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL,
5059 vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) &
5060 ~(CPU_BASED_CR3_LOAD_EXITING |
5061 CPU_BASED_CR3_STORE_EXITING));
5062 vcpu->arch.cr0 = cr0;
5063 vmx_set_cr4(vcpu, kvm_read_cr4(vcpu));
5066 if (!(cr0 & X86_CR0_WP))
5067 *hw_cr0 &= ~X86_CR0_WP;
5070 static void vmx_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
5072 struct vcpu_vmx *vmx = to_vmx(vcpu);
5073 unsigned long hw_cr0;
5075 hw_cr0 = (cr0 & ~KVM_GUEST_CR0_MASK);
5076 if (enable_unrestricted_guest)
5077 hw_cr0 |= KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST;
5079 hw_cr0 |= KVM_VM_CR0_ALWAYS_ON;
5081 if (vmx->rmode.vm86_active && (cr0 & X86_CR0_PE))
5084 if (!vmx->rmode.vm86_active && !(cr0 & X86_CR0_PE))
5088 #ifdef CONFIG_X86_64
5089 if (vcpu->arch.efer & EFER_LME) {
5090 if (!is_paging(vcpu) && (cr0 & X86_CR0_PG))
5092 if (is_paging(vcpu) && !(cr0 & X86_CR0_PG))
5097 if (enable_ept && !enable_unrestricted_guest)
5098 ept_update_paging_mode_cr0(&hw_cr0, cr0, vcpu);
5100 vmcs_writel(CR0_READ_SHADOW, cr0);
5101 vmcs_writel(GUEST_CR0, hw_cr0);
5102 vcpu->arch.cr0 = cr0;
5104 /* depends on vcpu->arch.cr0 to be set to a new value */
5105 vmx->emulation_required = emulation_required(vcpu);
5108 static int get_ept_level(struct kvm_vcpu *vcpu)
5110 if (cpu_has_vmx_ept_5levels() && (cpuid_maxphyaddr(vcpu) > 48))
5115 static u64 construct_eptp(struct kvm_vcpu *vcpu, unsigned long root_hpa)
5117 u64 eptp = VMX_EPTP_MT_WB;
5119 eptp |= (get_ept_level(vcpu) == 5) ? VMX_EPTP_PWL_5 : VMX_EPTP_PWL_4;
5121 if (enable_ept_ad_bits &&
5122 (!is_guest_mode(vcpu) || nested_ept_ad_enabled(vcpu)))
5123 eptp |= VMX_EPTP_AD_ENABLE_BIT;
5124 eptp |= (root_hpa & PAGE_MASK);
5129 static void vmx_set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3)
5131 struct kvm *kvm = vcpu->kvm;
5132 unsigned long guest_cr3;
5137 eptp = construct_eptp(vcpu, cr3);
5138 vmcs_write64(EPT_POINTER, eptp);
5140 if (kvm_x86_ops->tlb_remote_flush) {
5141 spin_lock(&to_kvm_vmx(kvm)->ept_pointer_lock);
5142 to_vmx(vcpu)->ept_pointer = eptp;
5143 to_kvm_vmx(kvm)->ept_pointers_match
5144 = EPT_POINTERS_CHECK;
5145 spin_unlock(&to_kvm_vmx(kvm)->ept_pointer_lock);
5148 if (enable_unrestricted_guest || is_paging(vcpu) ||
5149 is_guest_mode(vcpu))
5150 guest_cr3 = kvm_read_cr3(vcpu);
5152 guest_cr3 = to_kvm_vmx(kvm)->ept_identity_map_addr;
5153 ept_load_pdptrs(vcpu);
5156 vmcs_writel(GUEST_CR3, guest_cr3);
5159 static int vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
5162 * Pass through host's Machine Check Enable value to hw_cr4, which
5163 * is in force while we are in guest mode. Do not let guests control
5164 * this bit, even if host CR4.MCE == 0.
5166 unsigned long hw_cr4;
5168 hw_cr4 = (cr4_read_shadow() & X86_CR4_MCE) | (cr4 & ~X86_CR4_MCE);
5169 if (enable_unrestricted_guest)
5170 hw_cr4 |= KVM_VM_CR4_ALWAYS_ON_UNRESTRICTED_GUEST;
5171 else if (to_vmx(vcpu)->rmode.vm86_active)
5172 hw_cr4 |= KVM_RMODE_VM_CR4_ALWAYS_ON;
5174 hw_cr4 |= KVM_PMODE_VM_CR4_ALWAYS_ON;
5176 if (!boot_cpu_has(X86_FEATURE_UMIP) && vmx_umip_emulated()) {
5177 if (cr4 & X86_CR4_UMIP) {
5178 vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL,
5179 SECONDARY_EXEC_DESC);
5180 hw_cr4 &= ~X86_CR4_UMIP;
5181 } else if (!is_guest_mode(vcpu) ||
5182 !nested_cpu_has2(get_vmcs12(vcpu), SECONDARY_EXEC_DESC))
5183 vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL,
5184 SECONDARY_EXEC_DESC);
5187 if (cr4 & X86_CR4_VMXE) {
5189 * To use VMXON (and later other VMX instructions), a guest
5190 * must first be able to turn on cr4.VMXE (see handle_vmon()).
5191 * So basically the check on whether to allow nested VMX
5194 if (!nested_vmx_allowed(vcpu))
5198 if (to_vmx(vcpu)->nested.vmxon && !nested_cr4_valid(vcpu, cr4))
5201 vcpu->arch.cr4 = cr4;
5203 if (!enable_unrestricted_guest) {
5205 if (!is_paging(vcpu)) {
5206 hw_cr4 &= ~X86_CR4_PAE;
5207 hw_cr4 |= X86_CR4_PSE;
5208 } else if (!(cr4 & X86_CR4_PAE)) {
5209 hw_cr4 &= ~X86_CR4_PAE;
5214 * SMEP/SMAP/PKU is disabled if CPU is in non-paging mode in
5215 * hardware. To emulate this behavior, SMEP/SMAP/PKU needs
5216 * to be manually disabled when guest switches to non-paging
5219 * If !enable_unrestricted_guest, the CPU is always running
5220 * with CR0.PG=1 and CR4 needs to be modified.
5221 * If enable_unrestricted_guest, the CPU automatically
5222 * disables SMEP/SMAP/PKU when the guest sets CR0.PG=0.
5224 if (!is_paging(vcpu))
5225 hw_cr4 &= ~(X86_CR4_SMEP | X86_CR4_SMAP | X86_CR4_PKE);
5228 vmcs_writel(CR4_READ_SHADOW, cr4);
5229 vmcs_writel(GUEST_CR4, hw_cr4);
5233 static void vmx_get_segment(struct kvm_vcpu *vcpu,
5234 struct kvm_segment *var, int seg)
5236 struct vcpu_vmx *vmx = to_vmx(vcpu);
5239 if (vmx->rmode.vm86_active && seg != VCPU_SREG_LDTR) {
5240 *var = vmx->rmode.segs[seg];
5241 if (seg == VCPU_SREG_TR
5242 || var->selector == vmx_read_guest_seg_selector(vmx, seg))
5244 var->base = vmx_read_guest_seg_base(vmx, seg);
5245 var->selector = vmx_read_guest_seg_selector(vmx, seg);
5248 var->base = vmx_read_guest_seg_base(vmx, seg);
5249 var->limit = vmx_read_guest_seg_limit(vmx, seg);
5250 var->selector = vmx_read_guest_seg_selector(vmx, seg);
5251 ar = vmx_read_guest_seg_ar(vmx, seg);
5252 var->unusable = (ar >> 16) & 1;
5253 var->type = ar & 15;
5254 var->s = (ar >> 4) & 1;
5255 var->dpl = (ar >> 5) & 3;
5257 * Some userspaces do not preserve unusable property. Since usable
5258 * segment has to be present according to VMX spec we can use present
5259 * property to amend userspace bug by making unusable segment always
5260 * nonpresent. vmx_segment_access_rights() already marks nonpresent
5261 * segment as unusable.
5263 var->present = !var->unusable;
5264 var->avl = (ar >> 12) & 1;
5265 var->l = (ar >> 13) & 1;
5266 var->db = (ar >> 14) & 1;
5267 var->g = (ar >> 15) & 1;
5270 static u64 vmx_get_segment_base(struct kvm_vcpu *vcpu, int seg)
5272 struct kvm_segment s;
5274 if (to_vmx(vcpu)->rmode.vm86_active) {
5275 vmx_get_segment(vcpu, &s, seg);
5278 return vmx_read_guest_seg_base(to_vmx(vcpu), seg);
5281 static int vmx_get_cpl(struct kvm_vcpu *vcpu)
5283 struct vcpu_vmx *vmx = to_vmx(vcpu);
5285 if (unlikely(vmx->rmode.vm86_active))
5288 int ar = vmx_read_guest_seg_ar(vmx, VCPU_SREG_SS);
5289 return VMX_AR_DPL(ar);
5293 static u32 vmx_segment_access_rights(struct kvm_segment *var)
5297 if (var->unusable || !var->present)
5300 ar = var->type & 15;
5301 ar |= (var->s & 1) << 4;
5302 ar |= (var->dpl & 3) << 5;
5303 ar |= (var->present & 1) << 7;
5304 ar |= (var->avl & 1) << 12;
5305 ar |= (var->l & 1) << 13;
5306 ar |= (var->db & 1) << 14;
5307 ar |= (var->g & 1) << 15;
5313 static void vmx_set_segment(struct kvm_vcpu *vcpu,
5314 struct kvm_segment *var, int seg)
5316 struct vcpu_vmx *vmx = to_vmx(vcpu);
5317 const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
5319 vmx_segment_cache_clear(vmx);
5321 if (vmx->rmode.vm86_active && seg != VCPU_SREG_LDTR) {
5322 vmx->rmode.segs[seg] = *var;
5323 if (seg == VCPU_SREG_TR)
5324 vmcs_write16(sf->selector, var->selector);
5326 fix_rmode_seg(seg, &vmx->rmode.segs[seg]);
5330 vmcs_writel(sf->base, var->base);
5331 vmcs_write32(sf->limit, var->limit);
5332 vmcs_write16(sf->selector, var->selector);
5335 * Fix the "Accessed" bit in AR field of segment registers for older
5337 * IA32 arch specifies that at the time of processor reset the
5338 * "Accessed" bit in the AR field of segment registers is 1. And qemu
5339 * is setting it to 0 in the userland code. This causes invalid guest
5340 * state vmexit when "unrestricted guest" mode is turned on.
5341 * Fix for this setup issue in cpu_reset is being pushed in the qemu
5342 * tree. Newer qemu binaries with that qemu fix would not need this
5345 if (enable_unrestricted_guest && (seg != VCPU_SREG_LDTR))
5346 var->type |= 0x1; /* Accessed */
5348 vmcs_write32(sf->ar_bytes, vmx_segment_access_rights(var));
5351 vmx->emulation_required = emulation_required(vcpu);
5354 static void vmx_get_cs_db_l_bits(struct kvm_vcpu *vcpu, int *db, int *l)
5356 u32 ar = vmx_read_guest_seg_ar(to_vmx(vcpu), VCPU_SREG_CS);
5358 *db = (ar >> 14) & 1;
5359 *l = (ar >> 13) & 1;
5362 static void vmx_get_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
5364 dt->size = vmcs_read32(GUEST_IDTR_LIMIT);
5365 dt->address = vmcs_readl(GUEST_IDTR_BASE);
5368 static void vmx_set_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
5370 vmcs_write32(GUEST_IDTR_LIMIT, dt->size);
5371 vmcs_writel(GUEST_IDTR_BASE, dt->address);
5374 static void vmx_get_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
5376 dt->size = vmcs_read32(GUEST_GDTR_LIMIT);
5377 dt->address = vmcs_readl(GUEST_GDTR_BASE);
5380 static void vmx_set_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
5382 vmcs_write32(GUEST_GDTR_LIMIT, dt->size);
5383 vmcs_writel(GUEST_GDTR_BASE, dt->address);
5386 static bool rmode_segment_valid(struct kvm_vcpu *vcpu, int seg)
5388 struct kvm_segment var;
5391 vmx_get_segment(vcpu, &var, seg);
5393 if (seg == VCPU_SREG_CS)
5395 ar = vmx_segment_access_rights(&var);
5397 if (var.base != (var.selector << 4))
5399 if (var.limit != 0xffff)
5407 static bool code_segment_valid(struct kvm_vcpu *vcpu)
5409 struct kvm_segment cs;
5410 unsigned int cs_rpl;
5412 vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
5413 cs_rpl = cs.selector & SEGMENT_RPL_MASK;
5417 if (~cs.type & (VMX_AR_TYPE_CODE_MASK|VMX_AR_TYPE_ACCESSES_MASK))
5421 if (cs.type & VMX_AR_TYPE_WRITEABLE_MASK) {
5422 if (cs.dpl > cs_rpl)
5425 if (cs.dpl != cs_rpl)
5431 /* TODO: Add Reserved field check, this'll require a new member in the kvm_segment_field structure */
5435 static bool stack_segment_valid(struct kvm_vcpu *vcpu)
5437 struct kvm_segment ss;
5438 unsigned int ss_rpl;
5440 vmx_get_segment(vcpu, &ss, VCPU_SREG_SS);
5441 ss_rpl = ss.selector & SEGMENT_RPL_MASK;
5445 if (ss.type != 3 && ss.type != 7)
5449 if (ss.dpl != ss_rpl) /* DPL != RPL */
5457 static bool data_segment_valid(struct kvm_vcpu *vcpu, int seg)
5459 struct kvm_segment var;
5462 vmx_get_segment(vcpu, &var, seg);
5463 rpl = var.selector & SEGMENT_RPL_MASK;
5471 if (~var.type & (VMX_AR_TYPE_CODE_MASK|VMX_AR_TYPE_WRITEABLE_MASK)) {
5472 if (var.dpl < rpl) /* DPL < RPL */
5476 /* TODO: Add other members to kvm_segment_field to allow checking for other access
5482 static bool tr_valid(struct kvm_vcpu *vcpu)
5484 struct kvm_segment tr;
5486 vmx_get_segment(vcpu, &tr, VCPU_SREG_TR);
5490 if (tr.selector & SEGMENT_TI_MASK) /* TI = 1 */
5492 if (tr.type != 3 && tr.type != 11) /* TODO: Check if guest is in IA32e mode */
5500 static bool ldtr_valid(struct kvm_vcpu *vcpu)
5502 struct kvm_segment ldtr;
5504 vmx_get_segment(vcpu, &ldtr, VCPU_SREG_LDTR);
5508 if (ldtr.selector & SEGMENT_TI_MASK) /* TI = 1 */
5518 static bool cs_ss_rpl_check(struct kvm_vcpu *vcpu)
5520 struct kvm_segment cs, ss;
5522 vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
5523 vmx_get_segment(vcpu, &ss, VCPU_SREG_SS);
5525 return ((cs.selector & SEGMENT_RPL_MASK) ==
5526 (ss.selector & SEGMENT_RPL_MASK));
5530 * Check if guest state is valid. Returns true if valid, false if
5532 * We assume that registers are always usable
5534 static bool guest_state_valid(struct kvm_vcpu *vcpu)
5536 if (enable_unrestricted_guest)
5539 /* real mode guest state checks */
5540 if (!is_protmode(vcpu) || (vmx_get_rflags(vcpu) & X86_EFLAGS_VM)) {
5541 if (!rmode_segment_valid(vcpu, VCPU_SREG_CS))
5543 if (!rmode_segment_valid(vcpu, VCPU_SREG_SS))
5545 if (!rmode_segment_valid(vcpu, VCPU_SREG_DS))
5547 if (!rmode_segment_valid(vcpu, VCPU_SREG_ES))
5549 if (!rmode_segment_valid(vcpu, VCPU_SREG_FS))
5551 if (!rmode_segment_valid(vcpu, VCPU_SREG_GS))
5554 /* protected mode guest state checks */
5555 if (!cs_ss_rpl_check(vcpu))
5557 if (!code_segment_valid(vcpu))
5559 if (!stack_segment_valid(vcpu))
5561 if (!data_segment_valid(vcpu, VCPU_SREG_DS))
5563 if (!data_segment_valid(vcpu, VCPU_SREG_ES))
5565 if (!data_segment_valid(vcpu, VCPU_SREG_FS))
5567 if (!data_segment_valid(vcpu, VCPU_SREG_GS))
5569 if (!tr_valid(vcpu))
5571 if (!ldtr_valid(vcpu))
5575 * - Add checks on RIP
5576 * - Add checks on RFLAGS
5582 static bool page_address_valid(struct kvm_vcpu *vcpu, gpa_t gpa)
5584 return PAGE_ALIGNED(gpa) && !(gpa >> cpuid_maxphyaddr(vcpu));
5587 static int init_rmode_tss(struct kvm *kvm)
5593 idx = srcu_read_lock(&kvm->srcu);
5594 fn = to_kvm_vmx(kvm)->tss_addr >> PAGE_SHIFT;
5595 r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE);
5598 data = TSS_BASE_SIZE + TSS_REDIRECTION_SIZE;
5599 r = kvm_write_guest_page(kvm, fn++, &data,
5600 TSS_IOPB_BASE_OFFSET, sizeof(u16));
5603 r = kvm_clear_guest_page(kvm, fn++, 0, PAGE_SIZE);
5606 r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE);
5610 r = kvm_write_guest_page(kvm, fn, &data,
5611 RMODE_TSS_SIZE - 2 * PAGE_SIZE - 1,
5614 srcu_read_unlock(&kvm->srcu, idx);
5618 static int init_rmode_identity_map(struct kvm *kvm)
5620 struct kvm_vmx *kvm_vmx = to_kvm_vmx(kvm);
5622 kvm_pfn_t identity_map_pfn;
5625 /* Protect kvm_vmx->ept_identity_pagetable_done. */
5626 mutex_lock(&kvm->slots_lock);
5628 if (likely(kvm_vmx->ept_identity_pagetable_done))
5631 if (!kvm_vmx->ept_identity_map_addr)
5632 kvm_vmx->ept_identity_map_addr = VMX_EPT_IDENTITY_PAGETABLE_ADDR;
5633 identity_map_pfn = kvm_vmx->ept_identity_map_addr >> PAGE_SHIFT;
5635 r = __x86_set_memory_region(kvm, IDENTITY_PAGETABLE_PRIVATE_MEMSLOT,
5636 kvm_vmx->ept_identity_map_addr, PAGE_SIZE);
5640 idx = srcu_read_lock(&kvm->srcu);
5641 r = kvm_clear_guest_page(kvm, identity_map_pfn, 0, PAGE_SIZE);
5644 /* Set up identity-mapping pagetable for EPT in real mode */
5645 for (i = 0; i < PT32_ENT_PER_PAGE; i++) {
5646 tmp = (i << 22) + (_PAGE_PRESENT | _PAGE_RW | _PAGE_USER |
5647 _PAGE_ACCESSED | _PAGE_DIRTY | _PAGE_PSE);
5648 r = kvm_write_guest_page(kvm, identity_map_pfn,
5649 &tmp, i * sizeof(tmp), sizeof(tmp));
5653 kvm_vmx->ept_identity_pagetable_done = true;
5656 srcu_read_unlock(&kvm->srcu, idx);
5659 mutex_unlock(&kvm->slots_lock);
5663 static void seg_setup(int seg)
5665 const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
5668 vmcs_write16(sf->selector, 0);
5669 vmcs_writel(sf->base, 0);
5670 vmcs_write32(sf->limit, 0xffff);
5672 if (seg == VCPU_SREG_CS)
5673 ar |= 0x08; /* code segment */
5675 vmcs_write32(sf->ar_bytes, ar);
5678 static int alloc_apic_access_page(struct kvm *kvm)
5683 mutex_lock(&kvm->slots_lock);
5684 if (kvm->arch.apic_access_page_done)
5686 r = __x86_set_memory_region(kvm, APIC_ACCESS_PAGE_PRIVATE_MEMSLOT,
5687 APIC_DEFAULT_PHYS_BASE, PAGE_SIZE);
5691 page = gfn_to_page(kvm, APIC_DEFAULT_PHYS_BASE >> PAGE_SHIFT);
5692 if (is_error_page(page)) {
5698 * Do not pin the page in memory, so that memory hot-unplug
5699 * is able to migrate it.
5702 kvm->arch.apic_access_page_done = true;
5704 mutex_unlock(&kvm->slots_lock);
5708 static int allocate_vpid(void)
5714 spin_lock(&vmx_vpid_lock);
5715 vpid = find_first_zero_bit(vmx_vpid_bitmap, VMX_NR_VPIDS);
5716 if (vpid < VMX_NR_VPIDS)
5717 __set_bit(vpid, vmx_vpid_bitmap);
5720 spin_unlock(&vmx_vpid_lock);
5724 static void free_vpid(int vpid)
5726 if (!enable_vpid || vpid == 0)
5728 spin_lock(&vmx_vpid_lock);
5729 __clear_bit(vpid, vmx_vpid_bitmap);
5730 spin_unlock(&vmx_vpid_lock);
5733 static void __always_inline vmx_disable_intercept_for_msr(unsigned long *msr_bitmap,
5736 int f = sizeof(unsigned long);
5738 if (!cpu_has_vmx_msr_bitmap())
5741 if (static_branch_unlikely(&enable_evmcs))
5742 evmcs_touch_msr_bitmap();
5745 * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
5746 * have the write-low and read-high bitmap offsets the wrong way round.
5747 * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
5749 if (msr <= 0x1fff) {
5750 if (type & MSR_TYPE_R)
5752 __clear_bit(msr, msr_bitmap + 0x000 / f);
5754 if (type & MSR_TYPE_W)
5756 __clear_bit(msr, msr_bitmap + 0x800 / f);
5758 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
5760 if (type & MSR_TYPE_R)
5762 __clear_bit(msr, msr_bitmap + 0x400 / f);
5764 if (type & MSR_TYPE_W)
5766 __clear_bit(msr, msr_bitmap + 0xc00 / f);
5771 static void __always_inline vmx_enable_intercept_for_msr(unsigned long *msr_bitmap,
5774 int f = sizeof(unsigned long);
5776 if (!cpu_has_vmx_msr_bitmap())
5779 if (static_branch_unlikely(&enable_evmcs))
5780 evmcs_touch_msr_bitmap();
5783 * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
5784 * have the write-low and read-high bitmap offsets the wrong way round.
5785 * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
5787 if (msr <= 0x1fff) {
5788 if (type & MSR_TYPE_R)
5790 __set_bit(msr, msr_bitmap + 0x000 / f);
5792 if (type & MSR_TYPE_W)
5794 __set_bit(msr, msr_bitmap + 0x800 / f);
5796 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
5798 if (type & MSR_TYPE_R)
5800 __set_bit(msr, msr_bitmap + 0x400 / f);
5802 if (type & MSR_TYPE_W)
5804 __set_bit(msr, msr_bitmap + 0xc00 / f);
5809 static void __always_inline vmx_set_intercept_for_msr(unsigned long *msr_bitmap,
5810 u32 msr, int type, bool value)
5813 vmx_enable_intercept_for_msr(msr_bitmap, msr, type);
5815 vmx_disable_intercept_for_msr(msr_bitmap, msr, type);
5819 * If a msr is allowed by L0, we should check whether it is allowed by L1.
5820 * The corresponding bit will be cleared unless both of L0 and L1 allow it.
5822 static void nested_vmx_disable_intercept_for_msr(unsigned long *msr_bitmap_l1,
5823 unsigned long *msr_bitmap_nested,
5826 int f = sizeof(unsigned long);
5829 * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
5830 * have the write-low and read-high bitmap offsets the wrong way round.
5831 * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
5833 if (msr <= 0x1fff) {
5834 if (type & MSR_TYPE_R &&
5835 !test_bit(msr, msr_bitmap_l1 + 0x000 / f))
5837 __clear_bit(msr, msr_bitmap_nested + 0x000 / f);
5839 if (type & MSR_TYPE_W &&
5840 !test_bit(msr, msr_bitmap_l1 + 0x800 / f))
5842 __clear_bit(msr, msr_bitmap_nested + 0x800 / f);
5844 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
5846 if (type & MSR_TYPE_R &&
5847 !test_bit(msr, msr_bitmap_l1 + 0x400 / f))
5849 __clear_bit(msr, msr_bitmap_nested + 0x400 / f);
5851 if (type & MSR_TYPE_W &&
5852 !test_bit(msr, msr_bitmap_l1 + 0xc00 / f))
5854 __clear_bit(msr, msr_bitmap_nested + 0xc00 / f);
5859 static u8 vmx_msr_bitmap_mode(struct kvm_vcpu *vcpu)
5863 if (cpu_has_secondary_exec_ctrls() &&
5864 (vmcs_read32(SECONDARY_VM_EXEC_CONTROL) &
5865 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE)) {
5866 mode |= MSR_BITMAP_MODE_X2APIC;
5867 if (enable_apicv && kvm_vcpu_apicv_active(vcpu))
5868 mode |= MSR_BITMAP_MODE_X2APIC_APICV;
5871 if (is_long_mode(vcpu))
5872 mode |= MSR_BITMAP_MODE_LM;
5877 #define X2APIC_MSR(r) (APIC_BASE_MSR + ((r) >> 4))
5879 static void vmx_update_msr_bitmap_x2apic(unsigned long *msr_bitmap,
5884 for (msr = 0x800; msr <= 0x8ff; msr += BITS_PER_LONG) {
5885 unsigned word = msr / BITS_PER_LONG;
5886 msr_bitmap[word] = (mode & MSR_BITMAP_MODE_X2APIC_APICV) ? 0 : ~0;
5887 msr_bitmap[word + (0x800 / sizeof(long))] = ~0;
5890 if (mode & MSR_BITMAP_MODE_X2APIC) {
5892 * TPR reads and writes can be virtualized even if virtual interrupt
5893 * delivery is not in use.
5895 vmx_disable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_TASKPRI), MSR_TYPE_RW);
5896 if (mode & MSR_BITMAP_MODE_X2APIC_APICV) {
5897 vmx_enable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_TMCCT), MSR_TYPE_R);
5898 vmx_disable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_EOI), MSR_TYPE_W);
5899 vmx_disable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_SELF_IPI), MSR_TYPE_W);
5904 static void vmx_update_msr_bitmap(struct kvm_vcpu *vcpu)
5906 struct vcpu_vmx *vmx = to_vmx(vcpu);
5907 unsigned long *msr_bitmap = vmx->vmcs01.msr_bitmap;
5908 u8 mode = vmx_msr_bitmap_mode(vcpu);
5909 u8 changed = mode ^ vmx->msr_bitmap_mode;
5914 vmx_set_intercept_for_msr(msr_bitmap, MSR_KERNEL_GS_BASE, MSR_TYPE_RW,
5915 !(mode & MSR_BITMAP_MODE_LM));
5917 if (changed & (MSR_BITMAP_MODE_X2APIC | MSR_BITMAP_MODE_X2APIC_APICV))
5918 vmx_update_msr_bitmap_x2apic(msr_bitmap, mode);
5920 vmx->msr_bitmap_mode = mode;
5923 static bool vmx_get_enable_apicv(struct kvm_vcpu *vcpu)
5925 return enable_apicv;
5928 static void nested_mark_vmcs12_pages_dirty(struct kvm_vcpu *vcpu)
5930 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
5934 * Don't need to mark the APIC access page dirty; it is never
5935 * written to by the CPU during APIC virtualization.
5938 if (nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW)) {
5939 gfn = vmcs12->virtual_apic_page_addr >> PAGE_SHIFT;
5940 kvm_vcpu_mark_page_dirty(vcpu, gfn);
5943 if (nested_cpu_has_posted_intr(vmcs12)) {
5944 gfn = vmcs12->posted_intr_desc_addr >> PAGE_SHIFT;
5945 kvm_vcpu_mark_page_dirty(vcpu, gfn);
5950 static void vmx_complete_nested_posted_interrupt(struct kvm_vcpu *vcpu)
5952 struct vcpu_vmx *vmx = to_vmx(vcpu);
5957 if (!vmx->nested.pi_desc || !vmx->nested.pi_pending)
5960 vmx->nested.pi_pending = false;
5961 if (!pi_test_and_clear_on(vmx->nested.pi_desc))
5964 max_irr = find_last_bit((unsigned long *)vmx->nested.pi_desc->pir, 256);
5965 if (max_irr != 256) {
5966 vapic_page = kmap(vmx->nested.virtual_apic_page);
5967 __kvm_apic_update_irr(vmx->nested.pi_desc->pir,
5968 vapic_page, &max_irr);
5969 kunmap(vmx->nested.virtual_apic_page);
5971 status = vmcs_read16(GUEST_INTR_STATUS);
5972 if ((u8)max_irr > ((u8)status & 0xff)) {
5974 status |= (u8)max_irr;
5975 vmcs_write16(GUEST_INTR_STATUS, status);
5979 nested_mark_vmcs12_pages_dirty(vcpu);
5982 static inline bool kvm_vcpu_trigger_posted_interrupt(struct kvm_vcpu *vcpu,
5986 int pi_vec = nested ? POSTED_INTR_NESTED_VECTOR : POSTED_INTR_VECTOR;
5988 if (vcpu->mode == IN_GUEST_MODE) {
5990 * The vector of interrupt to be delivered to vcpu had
5991 * been set in PIR before this function.
5993 * Following cases will be reached in this block, and
5994 * we always send a notification event in all cases as
5997 * Case 1: vcpu keeps in non-root mode. Sending a
5998 * notification event posts the interrupt to vcpu.
6000 * Case 2: vcpu exits to root mode and is still
6001 * runnable. PIR will be synced to vIRR before the
6002 * next vcpu entry. Sending a notification event in
6003 * this case has no effect, as vcpu is not in root
6006 * Case 3: vcpu exits to root mode and is blocked.
6007 * vcpu_block() has already synced PIR to vIRR and
6008 * never blocks vcpu if vIRR is not cleared. Therefore,
6009 * a blocked vcpu here does not wait for any requested
6010 * interrupts in PIR, and sending a notification event
6011 * which has no effect is safe here.
6014 apic->send_IPI_mask(get_cpu_mask(vcpu->cpu), pi_vec);
6021 static int vmx_deliver_nested_posted_interrupt(struct kvm_vcpu *vcpu,
6024 struct vcpu_vmx *vmx = to_vmx(vcpu);
6026 if (is_guest_mode(vcpu) &&
6027 vector == vmx->nested.posted_intr_nv) {
6029 * If a posted intr is not recognized by hardware,
6030 * we will accomplish it in the next vmentry.
6032 vmx->nested.pi_pending = true;
6033 kvm_make_request(KVM_REQ_EVENT, vcpu);
6034 /* the PIR and ON have been set by L1. */
6035 if (!kvm_vcpu_trigger_posted_interrupt(vcpu, true))
6036 kvm_vcpu_kick(vcpu);
6042 * Send interrupt to vcpu via posted interrupt way.
6043 * 1. If target vcpu is running(non-root mode), send posted interrupt
6044 * notification to vcpu and hardware will sync PIR to vIRR atomically.
6045 * 2. If target vcpu isn't running(root mode), kick it to pick up the
6046 * interrupt from PIR in next vmentry.
6048 static void vmx_deliver_posted_interrupt(struct kvm_vcpu *vcpu, int vector)
6050 struct vcpu_vmx *vmx = to_vmx(vcpu);
6053 r = vmx_deliver_nested_posted_interrupt(vcpu, vector);
6057 if (pi_test_and_set_pir(vector, &vmx->pi_desc))
6060 /* If a previous notification has sent the IPI, nothing to do. */
6061 if (pi_test_and_set_on(&vmx->pi_desc))
6064 if (!kvm_vcpu_trigger_posted_interrupt(vcpu, false))
6065 kvm_vcpu_kick(vcpu);
6069 * Set up the vmcs's constant host-state fields, i.e., host-state fields that
6070 * will not change in the lifetime of the guest.
6071 * Note that host-state that does change is set elsewhere. E.g., host-state
6072 * that is set differently for each CPU is set in vmx_vcpu_load(), not here.
6074 static void vmx_set_constant_host_state(struct vcpu_vmx *vmx)
6079 unsigned long cr0, cr3, cr4;
6082 WARN_ON(cr0 & X86_CR0_TS);
6083 vmcs_writel(HOST_CR0, cr0); /* 22.2.3 */
6086 * Save the most likely value for this task's CR3 in the VMCS.
6087 * We can't use __get_current_cr3_fast() because we're not atomic.
6090 vmcs_writel(HOST_CR3, cr3); /* 22.2.3 FIXME: shadow tables */
6091 vmx->loaded_vmcs->vmcs_host_cr3 = cr3;
6093 /* Save the most likely value for this task's CR4 in the VMCS. */
6094 cr4 = cr4_read_shadow();
6095 vmcs_writel(HOST_CR4, cr4); /* 22.2.3, 22.2.5 */
6096 vmx->loaded_vmcs->vmcs_host_cr4 = cr4;
6098 vmcs_write16(HOST_CS_SELECTOR, __KERNEL_CS); /* 22.2.4 */
6099 #ifdef CONFIG_X86_64
6101 * Load null selectors, so we can avoid reloading them in
6102 * __vmx_load_host_state(), in case userspace uses the null selectors
6103 * too (the expected case).
6105 vmcs_write16(HOST_DS_SELECTOR, 0);
6106 vmcs_write16(HOST_ES_SELECTOR, 0);
6108 vmcs_write16(HOST_DS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
6109 vmcs_write16(HOST_ES_SELECTOR, __KERNEL_DS); /* 22.2.4 */
6111 vmcs_write16(HOST_SS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
6112 vmcs_write16(HOST_TR_SELECTOR, GDT_ENTRY_TSS*8); /* 22.2.4 */
6115 vmcs_writel(HOST_IDTR_BASE, dt.address); /* 22.2.4 */
6116 vmx->host_idt_base = dt.address;
6118 vmcs_writel(HOST_RIP, vmx_return); /* 22.2.5 */
6120 rdmsr(MSR_IA32_SYSENTER_CS, low32, high32);
6121 vmcs_write32(HOST_IA32_SYSENTER_CS, low32);
6122 rdmsrl(MSR_IA32_SYSENTER_EIP, tmpl);
6123 vmcs_writel(HOST_IA32_SYSENTER_EIP, tmpl); /* 22.2.3 */
6125 if (vmcs_config.vmexit_ctrl & VM_EXIT_LOAD_IA32_PAT) {
6126 rdmsr(MSR_IA32_CR_PAT, low32, high32);
6127 vmcs_write64(HOST_IA32_PAT, low32 | ((u64) high32 << 32));
6131 static void set_cr4_guest_host_mask(struct vcpu_vmx *vmx)
6133 vmx->vcpu.arch.cr4_guest_owned_bits = KVM_CR4_GUEST_OWNED_BITS;
6135 vmx->vcpu.arch.cr4_guest_owned_bits |= X86_CR4_PGE;
6136 if (is_guest_mode(&vmx->vcpu))
6137 vmx->vcpu.arch.cr4_guest_owned_bits &=
6138 ~get_vmcs12(&vmx->vcpu)->cr4_guest_host_mask;
6139 vmcs_writel(CR4_GUEST_HOST_MASK, ~vmx->vcpu.arch.cr4_guest_owned_bits);
6142 static u32 vmx_pin_based_exec_ctrl(struct vcpu_vmx *vmx)
6144 u32 pin_based_exec_ctrl = vmcs_config.pin_based_exec_ctrl;
6146 if (!kvm_vcpu_apicv_active(&vmx->vcpu))
6147 pin_based_exec_ctrl &= ~PIN_BASED_POSTED_INTR;
6150 pin_based_exec_ctrl &= ~PIN_BASED_VIRTUAL_NMIS;
6152 /* Enable the preemption timer dynamically */
6153 pin_based_exec_ctrl &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
6154 return pin_based_exec_ctrl;
6157 static void vmx_refresh_apicv_exec_ctrl(struct kvm_vcpu *vcpu)
6159 struct vcpu_vmx *vmx = to_vmx(vcpu);
6161 vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, vmx_pin_based_exec_ctrl(vmx));
6162 if (cpu_has_secondary_exec_ctrls()) {
6163 if (kvm_vcpu_apicv_active(vcpu))
6164 vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL,
6165 SECONDARY_EXEC_APIC_REGISTER_VIRT |
6166 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
6168 vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL,
6169 SECONDARY_EXEC_APIC_REGISTER_VIRT |
6170 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
6173 if (cpu_has_vmx_msr_bitmap())
6174 vmx_update_msr_bitmap(vcpu);
6177 static u32 vmx_exec_control(struct vcpu_vmx *vmx)
6179 u32 exec_control = vmcs_config.cpu_based_exec_ctrl;
6181 if (vmx->vcpu.arch.switch_db_regs & KVM_DEBUGREG_WONT_EXIT)
6182 exec_control &= ~CPU_BASED_MOV_DR_EXITING;
6184 if (!cpu_need_tpr_shadow(&vmx->vcpu)) {
6185 exec_control &= ~CPU_BASED_TPR_SHADOW;
6186 #ifdef CONFIG_X86_64
6187 exec_control |= CPU_BASED_CR8_STORE_EXITING |
6188 CPU_BASED_CR8_LOAD_EXITING;
6192 exec_control |= CPU_BASED_CR3_STORE_EXITING |
6193 CPU_BASED_CR3_LOAD_EXITING |
6194 CPU_BASED_INVLPG_EXITING;
6195 if (kvm_mwait_in_guest(vmx->vcpu.kvm))
6196 exec_control &= ~(CPU_BASED_MWAIT_EXITING |
6197 CPU_BASED_MONITOR_EXITING);
6198 if (kvm_hlt_in_guest(vmx->vcpu.kvm))
6199 exec_control &= ~CPU_BASED_HLT_EXITING;
6200 return exec_control;
6203 static bool vmx_rdrand_supported(void)
6205 return vmcs_config.cpu_based_2nd_exec_ctrl &
6206 SECONDARY_EXEC_RDRAND_EXITING;
6209 static bool vmx_rdseed_supported(void)
6211 return vmcs_config.cpu_based_2nd_exec_ctrl &
6212 SECONDARY_EXEC_RDSEED_EXITING;
6215 static void vmx_compute_secondary_exec_control(struct vcpu_vmx *vmx)
6217 struct kvm_vcpu *vcpu = &vmx->vcpu;
6219 u32 exec_control = vmcs_config.cpu_based_2nd_exec_ctrl;
6221 if (!cpu_need_virtualize_apic_accesses(vcpu))
6222 exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
6224 exec_control &= ~SECONDARY_EXEC_ENABLE_VPID;
6226 exec_control &= ~SECONDARY_EXEC_ENABLE_EPT;
6227 enable_unrestricted_guest = 0;
6229 if (!enable_unrestricted_guest)
6230 exec_control &= ~SECONDARY_EXEC_UNRESTRICTED_GUEST;
6231 if (kvm_pause_in_guest(vmx->vcpu.kvm))
6232 exec_control &= ~SECONDARY_EXEC_PAUSE_LOOP_EXITING;
6233 if (!kvm_vcpu_apicv_active(vcpu))
6234 exec_control &= ~(SECONDARY_EXEC_APIC_REGISTER_VIRT |
6235 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
6236 exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
6238 /* SECONDARY_EXEC_DESC is enabled/disabled on writes to CR4.UMIP,
6239 * in vmx_set_cr4. */
6240 exec_control &= ~SECONDARY_EXEC_DESC;
6242 /* SECONDARY_EXEC_SHADOW_VMCS is enabled when L1 executes VMPTRLD
6244 We can NOT enable shadow_vmcs here because we don't have yet
6247 exec_control &= ~SECONDARY_EXEC_SHADOW_VMCS;
6250 exec_control &= ~SECONDARY_EXEC_ENABLE_PML;
6252 if (vmx_xsaves_supported()) {
6253 /* Exposing XSAVES only when XSAVE is exposed */
6254 bool xsaves_enabled =
6255 guest_cpuid_has(vcpu, X86_FEATURE_XSAVE) &&
6256 guest_cpuid_has(vcpu, X86_FEATURE_XSAVES);
6258 if (!xsaves_enabled)
6259 exec_control &= ~SECONDARY_EXEC_XSAVES;
6263 vmx->nested.msrs.secondary_ctls_high |=
6264 SECONDARY_EXEC_XSAVES;
6266 vmx->nested.msrs.secondary_ctls_high &=
6267 ~SECONDARY_EXEC_XSAVES;
6271 if (vmx_rdtscp_supported()) {
6272 bool rdtscp_enabled = guest_cpuid_has(vcpu, X86_FEATURE_RDTSCP);
6273 if (!rdtscp_enabled)
6274 exec_control &= ~SECONDARY_EXEC_RDTSCP;
6278 vmx->nested.msrs.secondary_ctls_high |=
6279 SECONDARY_EXEC_RDTSCP;
6281 vmx->nested.msrs.secondary_ctls_high &=
6282 ~SECONDARY_EXEC_RDTSCP;
6286 if (vmx_invpcid_supported()) {
6287 /* Exposing INVPCID only when PCID is exposed */
6288 bool invpcid_enabled =
6289 guest_cpuid_has(vcpu, X86_FEATURE_INVPCID) &&
6290 guest_cpuid_has(vcpu, X86_FEATURE_PCID);
6292 if (!invpcid_enabled) {
6293 exec_control &= ~SECONDARY_EXEC_ENABLE_INVPCID;
6294 guest_cpuid_clear(vcpu, X86_FEATURE_INVPCID);
6298 if (invpcid_enabled)
6299 vmx->nested.msrs.secondary_ctls_high |=
6300 SECONDARY_EXEC_ENABLE_INVPCID;
6302 vmx->nested.msrs.secondary_ctls_high &=
6303 ~SECONDARY_EXEC_ENABLE_INVPCID;
6307 if (vmx_rdrand_supported()) {
6308 bool rdrand_enabled = guest_cpuid_has(vcpu, X86_FEATURE_RDRAND);
6310 exec_control &= ~SECONDARY_EXEC_RDRAND_EXITING;
6314 vmx->nested.msrs.secondary_ctls_high |=
6315 SECONDARY_EXEC_RDRAND_EXITING;
6317 vmx->nested.msrs.secondary_ctls_high &=
6318 ~SECONDARY_EXEC_RDRAND_EXITING;
6322 if (vmx_rdseed_supported()) {
6323 bool rdseed_enabled = guest_cpuid_has(vcpu, X86_FEATURE_RDSEED);
6325 exec_control &= ~SECONDARY_EXEC_RDSEED_EXITING;
6329 vmx->nested.msrs.secondary_ctls_high |=
6330 SECONDARY_EXEC_RDSEED_EXITING;
6332 vmx->nested.msrs.secondary_ctls_high &=
6333 ~SECONDARY_EXEC_RDSEED_EXITING;
6337 vmx->secondary_exec_control = exec_control;
6340 static void ept_set_mmio_spte_mask(void)
6343 * EPT Misconfigurations can be generated if the value of bits 2:0
6344 * of an EPT paging-structure entry is 110b (write/execute).
6346 kvm_mmu_set_mmio_spte_mask(VMX_EPT_RWX_MASK,
6347 VMX_EPT_MISCONFIG_WX_VALUE);
6350 #define VMX_XSS_EXIT_BITMAP 0
6352 * Sets up the vmcs for emulated real mode.
6354 static void vmx_vcpu_setup(struct vcpu_vmx *vmx)
6356 #ifdef CONFIG_X86_64
6361 if (enable_shadow_vmcs) {
6363 * At vCPU creation, "VMWRITE to any supported field
6364 * in the VMCS" is supported, so use the more
6365 * permissive vmx_vmread_bitmap to specify both read
6366 * and write permissions for the shadow VMCS.
6368 vmcs_write64(VMREAD_BITMAP, __pa(vmx_vmread_bitmap));
6369 vmcs_write64(VMWRITE_BITMAP, __pa(vmx_vmread_bitmap));
6371 if (cpu_has_vmx_msr_bitmap())
6372 vmcs_write64(MSR_BITMAP, __pa(vmx->vmcs01.msr_bitmap));
6374 vmcs_write64(VMCS_LINK_POINTER, -1ull); /* 22.3.1.5 */
6377 vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, vmx_pin_based_exec_ctrl(vmx));
6378 vmx->hv_deadline_tsc = -1;
6380 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, vmx_exec_control(vmx));
6382 if (cpu_has_secondary_exec_ctrls()) {
6383 vmx_compute_secondary_exec_control(vmx);
6384 vmcs_write32(SECONDARY_VM_EXEC_CONTROL,
6385 vmx->secondary_exec_control);
6388 if (kvm_vcpu_apicv_active(&vmx->vcpu)) {
6389 vmcs_write64(EOI_EXIT_BITMAP0, 0);
6390 vmcs_write64(EOI_EXIT_BITMAP1, 0);
6391 vmcs_write64(EOI_EXIT_BITMAP2, 0);
6392 vmcs_write64(EOI_EXIT_BITMAP3, 0);
6394 vmcs_write16(GUEST_INTR_STATUS, 0);
6396 vmcs_write16(POSTED_INTR_NV, POSTED_INTR_VECTOR);
6397 vmcs_write64(POSTED_INTR_DESC_ADDR, __pa((&vmx->pi_desc)));
6400 if (!kvm_pause_in_guest(vmx->vcpu.kvm)) {
6401 vmcs_write32(PLE_GAP, ple_gap);
6402 vmx->ple_window = ple_window;
6403 vmx->ple_window_dirty = true;
6406 vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK, 0);
6407 vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH, 0);
6408 vmcs_write32(CR3_TARGET_COUNT, 0); /* 22.2.1 */
6410 vmcs_write16(HOST_FS_SELECTOR, 0); /* 22.2.4 */
6411 vmcs_write16(HOST_GS_SELECTOR, 0); /* 22.2.4 */
6412 vmx_set_constant_host_state(vmx);
6413 #ifdef CONFIG_X86_64
6414 rdmsrl(MSR_FS_BASE, a);
6415 vmcs_writel(HOST_FS_BASE, a); /* 22.2.4 */
6416 rdmsrl(MSR_GS_BASE, a);
6417 vmcs_writel(HOST_GS_BASE, a); /* 22.2.4 */
6419 vmcs_writel(HOST_FS_BASE, 0); /* 22.2.4 */
6420 vmcs_writel(HOST_GS_BASE, 0); /* 22.2.4 */
6423 if (cpu_has_vmx_vmfunc())
6424 vmcs_write64(VM_FUNCTION_CONTROL, 0);
6426 vmcs_write32(VM_EXIT_MSR_STORE_COUNT, 0);
6427 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, 0);
6428 vmcs_write64(VM_EXIT_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.host));
6429 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, 0);
6430 vmcs_write64(VM_ENTRY_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.guest));
6432 if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT)
6433 vmcs_write64(GUEST_IA32_PAT, vmx->vcpu.arch.pat);
6435 for (i = 0; i < ARRAY_SIZE(vmx_msr_index); ++i) {
6436 u32 index = vmx_msr_index[i];
6437 u32 data_low, data_high;
6440 if (rdmsr_safe(index, &data_low, &data_high) < 0)
6442 if (wrmsr_safe(index, data_low, data_high) < 0)
6444 vmx->guest_msrs[j].index = i;
6445 vmx->guest_msrs[j].data = 0;
6446 vmx->guest_msrs[j].mask = -1ull;
6450 if (boot_cpu_has(X86_FEATURE_ARCH_CAPABILITIES))
6451 rdmsrl(MSR_IA32_ARCH_CAPABILITIES, vmx->arch_capabilities);
6453 vm_exit_controls_init(vmx, vmcs_config.vmexit_ctrl);
6455 /* 22.2.1, 20.8.1 */
6456 vm_entry_controls_init(vmx, vmcs_config.vmentry_ctrl);
6458 vmx->vcpu.arch.cr0_guest_owned_bits = X86_CR0_TS;
6459 vmcs_writel(CR0_GUEST_HOST_MASK, ~X86_CR0_TS);
6461 set_cr4_guest_host_mask(vmx);
6463 if (vmx_xsaves_supported())
6464 vmcs_write64(XSS_EXIT_BITMAP, VMX_XSS_EXIT_BITMAP);
6467 ASSERT(vmx->pml_pg);
6468 vmcs_write64(PML_ADDRESS, page_to_phys(vmx->pml_pg));
6469 vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1);
6473 static void vmx_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event)
6475 struct vcpu_vmx *vmx = to_vmx(vcpu);
6476 struct msr_data apic_base_msr;
6479 vmx->rmode.vm86_active = 0;
6482 vcpu->arch.microcode_version = 0x100000000ULL;
6483 vmx->vcpu.arch.regs[VCPU_REGS_RDX] = get_rdx_init_val();
6484 kvm_set_cr8(vcpu, 0);
6487 apic_base_msr.data = APIC_DEFAULT_PHYS_BASE |
6488 MSR_IA32_APICBASE_ENABLE;
6489 if (kvm_vcpu_is_reset_bsp(vcpu))
6490 apic_base_msr.data |= MSR_IA32_APICBASE_BSP;
6491 apic_base_msr.host_initiated = true;
6492 kvm_set_apic_base(vcpu, &apic_base_msr);
6495 vmx_segment_cache_clear(vmx);
6497 seg_setup(VCPU_SREG_CS);
6498 vmcs_write16(GUEST_CS_SELECTOR, 0xf000);
6499 vmcs_writel(GUEST_CS_BASE, 0xffff0000ul);
6501 seg_setup(VCPU_SREG_DS);
6502 seg_setup(VCPU_SREG_ES);
6503 seg_setup(VCPU_SREG_FS);
6504 seg_setup(VCPU_SREG_GS);
6505 seg_setup(VCPU_SREG_SS);
6507 vmcs_write16(GUEST_TR_SELECTOR, 0);
6508 vmcs_writel(GUEST_TR_BASE, 0);
6509 vmcs_write32(GUEST_TR_LIMIT, 0xffff);
6510 vmcs_write32(GUEST_TR_AR_BYTES, 0x008b);
6512 vmcs_write16(GUEST_LDTR_SELECTOR, 0);
6513 vmcs_writel(GUEST_LDTR_BASE, 0);
6514 vmcs_write32(GUEST_LDTR_LIMIT, 0xffff);
6515 vmcs_write32(GUEST_LDTR_AR_BYTES, 0x00082);
6518 vmcs_write32(GUEST_SYSENTER_CS, 0);
6519 vmcs_writel(GUEST_SYSENTER_ESP, 0);
6520 vmcs_writel(GUEST_SYSENTER_EIP, 0);
6521 vmcs_write64(GUEST_IA32_DEBUGCTL, 0);
6524 kvm_set_rflags(vcpu, X86_EFLAGS_FIXED);
6525 kvm_rip_write(vcpu, 0xfff0);
6527 vmcs_writel(GUEST_GDTR_BASE, 0);
6528 vmcs_write32(GUEST_GDTR_LIMIT, 0xffff);
6530 vmcs_writel(GUEST_IDTR_BASE, 0);
6531 vmcs_write32(GUEST_IDTR_LIMIT, 0xffff);
6533 vmcs_write32(GUEST_ACTIVITY_STATE, GUEST_ACTIVITY_ACTIVE);
6534 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, 0);
6535 vmcs_writel(GUEST_PENDING_DBG_EXCEPTIONS, 0);
6536 if (kvm_mpx_supported())
6537 vmcs_write64(GUEST_BNDCFGS, 0);
6541 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0); /* 22.2.1 */
6543 if (cpu_has_vmx_tpr_shadow() && !init_event) {
6544 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, 0);
6545 if (cpu_need_tpr_shadow(vcpu))
6546 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR,
6547 __pa(vcpu->arch.apic->regs));
6548 vmcs_write32(TPR_THRESHOLD, 0);
6551 kvm_make_request(KVM_REQ_APIC_PAGE_RELOAD, vcpu);
6554 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid);
6556 cr0 = X86_CR0_NW | X86_CR0_CD | X86_CR0_ET;
6557 vmx->vcpu.arch.cr0 = cr0;
6558 vmx_set_cr0(vcpu, cr0); /* enter rmode */
6559 vmx_set_cr4(vcpu, 0);
6560 vmx_set_efer(vcpu, 0);
6562 update_exception_bitmap(vcpu);
6564 vpid_sync_context(vmx->vpid);
6566 vmx_clear_hlt(vcpu);
6570 * In nested virtualization, check if L1 asked to exit on external interrupts.
6571 * For most existing hypervisors, this will always return true.
6573 static bool nested_exit_on_intr(struct kvm_vcpu *vcpu)
6575 return get_vmcs12(vcpu)->pin_based_vm_exec_control &
6576 PIN_BASED_EXT_INTR_MASK;
6580 * In nested virtualization, check if L1 has set
6581 * VM_EXIT_ACK_INTR_ON_EXIT
6583 static bool nested_exit_intr_ack_set(struct kvm_vcpu *vcpu)
6585 return get_vmcs12(vcpu)->vm_exit_controls &
6586 VM_EXIT_ACK_INTR_ON_EXIT;
6589 static bool nested_exit_on_nmi(struct kvm_vcpu *vcpu)
6591 return nested_cpu_has_nmi_exiting(get_vmcs12(vcpu));
6594 static void enable_irq_window(struct kvm_vcpu *vcpu)
6596 vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL,
6597 CPU_BASED_VIRTUAL_INTR_PENDING);
6600 static void enable_nmi_window(struct kvm_vcpu *vcpu)
6603 vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & GUEST_INTR_STATE_STI) {
6604 enable_irq_window(vcpu);
6608 vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL,
6609 CPU_BASED_VIRTUAL_NMI_PENDING);
6612 static void vmx_inject_irq(struct kvm_vcpu *vcpu)
6614 struct vcpu_vmx *vmx = to_vmx(vcpu);
6616 int irq = vcpu->arch.interrupt.nr;
6618 trace_kvm_inj_virq(irq);
6620 ++vcpu->stat.irq_injections;
6621 if (vmx->rmode.vm86_active) {
6623 if (vcpu->arch.interrupt.soft)
6624 inc_eip = vcpu->arch.event_exit_inst_len;
6625 if (kvm_inject_realmode_interrupt(vcpu, irq, inc_eip) != EMULATE_DONE)
6626 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
6629 intr = irq | INTR_INFO_VALID_MASK;
6630 if (vcpu->arch.interrupt.soft) {
6631 intr |= INTR_TYPE_SOFT_INTR;
6632 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
6633 vmx->vcpu.arch.event_exit_inst_len);
6635 intr |= INTR_TYPE_EXT_INTR;
6636 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr);
6638 vmx_clear_hlt(vcpu);
6641 static void vmx_inject_nmi(struct kvm_vcpu *vcpu)
6643 struct vcpu_vmx *vmx = to_vmx(vcpu);
6647 * Tracking the NMI-blocked state in software is built upon
6648 * finding the next open IRQ window. This, in turn, depends on
6649 * well-behaving guests: They have to keep IRQs disabled at
6650 * least as long as the NMI handler runs. Otherwise we may
6651 * cause NMI nesting, maybe breaking the guest. But as this is
6652 * highly unlikely, we can live with the residual risk.
6654 vmx->loaded_vmcs->soft_vnmi_blocked = 1;
6655 vmx->loaded_vmcs->vnmi_blocked_time = 0;
6658 ++vcpu->stat.nmi_injections;
6659 vmx->loaded_vmcs->nmi_known_unmasked = false;
6661 if (vmx->rmode.vm86_active) {
6662 if (kvm_inject_realmode_interrupt(vcpu, NMI_VECTOR, 0) != EMULATE_DONE)
6663 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
6667 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
6668 INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK | NMI_VECTOR);
6670 vmx_clear_hlt(vcpu);
6673 static bool vmx_get_nmi_mask(struct kvm_vcpu *vcpu)
6675 struct vcpu_vmx *vmx = to_vmx(vcpu);
6679 return vmx->loaded_vmcs->soft_vnmi_blocked;
6680 if (vmx->loaded_vmcs->nmi_known_unmasked)
6682 masked = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & GUEST_INTR_STATE_NMI;
6683 vmx->loaded_vmcs->nmi_known_unmasked = !masked;
6687 static void vmx_set_nmi_mask(struct kvm_vcpu *vcpu, bool masked)
6689 struct vcpu_vmx *vmx = to_vmx(vcpu);
6692 if (vmx->loaded_vmcs->soft_vnmi_blocked != masked) {
6693 vmx->loaded_vmcs->soft_vnmi_blocked = masked;
6694 vmx->loaded_vmcs->vnmi_blocked_time = 0;
6697 vmx->loaded_vmcs->nmi_known_unmasked = !masked;
6699 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
6700 GUEST_INTR_STATE_NMI);
6702 vmcs_clear_bits(GUEST_INTERRUPTIBILITY_INFO,
6703 GUEST_INTR_STATE_NMI);
6707 static int vmx_nmi_allowed(struct kvm_vcpu *vcpu)
6709 if (to_vmx(vcpu)->nested.nested_run_pending)
6713 to_vmx(vcpu)->loaded_vmcs->soft_vnmi_blocked)
6716 return !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) &
6717 (GUEST_INTR_STATE_MOV_SS | GUEST_INTR_STATE_STI
6718 | GUEST_INTR_STATE_NMI));
6721 static int vmx_interrupt_allowed(struct kvm_vcpu *vcpu)
6723 return (!to_vmx(vcpu)->nested.nested_run_pending &&
6724 vmcs_readl(GUEST_RFLAGS) & X86_EFLAGS_IF) &&
6725 !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) &
6726 (GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS));
6729 static int vmx_set_tss_addr(struct kvm *kvm, unsigned int addr)
6733 if (enable_unrestricted_guest)
6736 ret = x86_set_memory_region(kvm, TSS_PRIVATE_MEMSLOT, addr,
6740 to_kvm_vmx(kvm)->tss_addr = addr;
6741 return init_rmode_tss(kvm);
6744 static int vmx_set_identity_map_addr(struct kvm *kvm, u64 ident_addr)
6746 to_kvm_vmx(kvm)->ept_identity_map_addr = ident_addr;
6750 static bool rmode_exception(struct kvm_vcpu *vcpu, int vec)
6755 * Update instruction length as we may reinject the exception
6756 * from user space while in guest debugging mode.
6758 to_vmx(vcpu)->vcpu.arch.event_exit_inst_len =
6759 vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
6760 if (vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
6764 if (vcpu->guest_debug &
6765 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))
6782 static int handle_rmode_exception(struct kvm_vcpu *vcpu,
6783 int vec, u32 err_code)
6786 * Instruction with address size override prefix opcode 0x67
6787 * Cause the #SS fault with 0 error code in VM86 mode.
6789 if (((vec == GP_VECTOR) || (vec == SS_VECTOR)) && err_code == 0) {
6790 if (emulate_instruction(vcpu, 0) == EMULATE_DONE) {
6791 if (vcpu->arch.halt_request) {
6792 vcpu->arch.halt_request = 0;
6793 return kvm_vcpu_halt(vcpu);
6801 * Forward all other exceptions that are valid in real mode.
6802 * FIXME: Breaks guest debugging in real mode, needs to be fixed with
6803 * the required debugging infrastructure rework.
6805 kvm_queue_exception(vcpu, vec);
6810 * Trigger machine check on the host. We assume all the MSRs are already set up
6811 * by the CPU and that we still run on the same CPU as the MCE occurred on.
6812 * We pass a fake environment to the machine check handler because we want
6813 * the guest to be always treated like user space, no matter what context
6814 * it used internally.
6816 static void kvm_machine_check(void)
6818 #if defined(CONFIG_X86_MCE) && defined(CONFIG_X86_64)
6819 struct pt_regs regs = {
6820 .cs = 3, /* Fake ring 3 no matter what the guest ran on */
6821 .flags = X86_EFLAGS_IF,
6824 do_machine_check(®s, 0);
6828 static int handle_machine_check(struct kvm_vcpu *vcpu)
6830 /* already handled by vcpu_run */
6834 static int handle_exception(struct kvm_vcpu *vcpu)
6836 struct vcpu_vmx *vmx = to_vmx(vcpu);
6837 struct kvm_run *kvm_run = vcpu->run;
6838 u32 intr_info, ex_no, error_code;
6839 unsigned long cr2, rip, dr6;
6841 enum emulation_result er;
6843 vect_info = vmx->idt_vectoring_info;
6844 intr_info = vmx->exit_intr_info;
6846 if (is_machine_check(intr_info))
6847 return handle_machine_check(vcpu);
6849 if (is_nmi(intr_info))
6850 return 1; /* already handled by vmx_vcpu_run() */
6852 if (is_invalid_opcode(intr_info))
6853 return handle_ud(vcpu);
6856 if (intr_info & INTR_INFO_DELIVER_CODE_MASK)
6857 error_code = vmcs_read32(VM_EXIT_INTR_ERROR_CODE);
6859 if (!vmx->rmode.vm86_active && is_gp_fault(intr_info)) {
6860 WARN_ON_ONCE(!enable_vmware_backdoor);
6861 er = emulate_instruction(vcpu,
6862 EMULTYPE_VMWARE | EMULTYPE_NO_UD_ON_FAIL);
6863 if (er == EMULATE_USER_EXIT)
6865 else if (er != EMULATE_DONE)
6866 kvm_queue_exception_e(vcpu, GP_VECTOR, error_code);
6871 * The #PF with PFEC.RSVD = 1 indicates the guest is accessing
6872 * MMIO, it is better to report an internal error.
6873 * See the comments in vmx_handle_exit.
6875 if ((vect_info & VECTORING_INFO_VALID_MASK) &&
6876 !(is_page_fault(intr_info) && !(error_code & PFERR_RSVD_MASK))) {
6877 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
6878 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_SIMUL_EX;
6879 vcpu->run->internal.ndata = 3;
6880 vcpu->run->internal.data[0] = vect_info;
6881 vcpu->run->internal.data[1] = intr_info;
6882 vcpu->run->internal.data[2] = error_code;
6886 if (is_page_fault(intr_info)) {
6887 cr2 = vmcs_readl(EXIT_QUALIFICATION);
6888 /* EPT won't cause page fault directly */
6889 WARN_ON_ONCE(!vcpu->arch.apf.host_apf_reason && enable_ept);
6890 return kvm_handle_page_fault(vcpu, error_code, cr2, NULL, 0);
6893 ex_no = intr_info & INTR_INFO_VECTOR_MASK;
6895 if (vmx->rmode.vm86_active && rmode_exception(vcpu, ex_no))
6896 return handle_rmode_exception(vcpu, ex_no, error_code);
6900 kvm_queue_exception_e(vcpu, AC_VECTOR, error_code);
6903 dr6 = vmcs_readl(EXIT_QUALIFICATION);
6904 if (!(vcpu->guest_debug &
6905 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))) {
6906 vcpu->arch.dr6 &= ~15;
6907 vcpu->arch.dr6 |= dr6 | DR6_RTM;
6908 if (is_icebp(intr_info))
6909 skip_emulated_instruction(vcpu);
6911 kvm_queue_exception(vcpu, DB_VECTOR);
6914 kvm_run->debug.arch.dr6 = dr6 | DR6_FIXED_1;
6915 kvm_run->debug.arch.dr7 = vmcs_readl(GUEST_DR7);
6919 * Update instruction length as we may reinject #BP from
6920 * user space while in guest debugging mode. Reading it for
6921 * #DB as well causes no harm, it is not used in that case.
6923 vmx->vcpu.arch.event_exit_inst_len =
6924 vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
6925 kvm_run->exit_reason = KVM_EXIT_DEBUG;
6926 rip = kvm_rip_read(vcpu);
6927 kvm_run->debug.arch.pc = vmcs_readl(GUEST_CS_BASE) + rip;
6928 kvm_run->debug.arch.exception = ex_no;
6931 kvm_run->exit_reason = KVM_EXIT_EXCEPTION;
6932 kvm_run->ex.exception = ex_no;
6933 kvm_run->ex.error_code = error_code;
6939 static int handle_external_interrupt(struct kvm_vcpu *vcpu)
6941 ++vcpu->stat.irq_exits;
6945 static int handle_triple_fault(struct kvm_vcpu *vcpu)
6947 vcpu->run->exit_reason = KVM_EXIT_SHUTDOWN;
6948 vcpu->mmio_needed = 0;
6952 static int handle_io(struct kvm_vcpu *vcpu)
6954 unsigned long exit_qualification;
6955 int size, in, string;
6958 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
6959 string = (exit_qualification & 16) != 0;
6961 ++vcpu->stat.io_exits;
6964 return emulate_instruction(vcpu, 0) == EMULATE_DONE;
6966 port = exit_qualification >> 16;
6967 size = (exit_qualification & 7) + 1;
6968 in = (exit_qualification & 8) != 0;
6970 return kvm_fast_pio(vcpu, size, port, in);
6974 vmx_patch_hypercall(struct kvm_vcpu *vcpu, unsigned char *hypercall)
6977 * Patch in the VMCALL instruction:
6979 hypercall[0] = 0x0f;
6980 hypercall[1] = 0x01;
6981 hypercall[2] = 0xc1;
6984 /* called to set cr0 as appropriate for a mov-to-cr0 exit. */
6985 static int handle_set_cr0(struct kvm_vcpu *vcpu, unsigned long val)
6987 if (is_guest_mode(vcpu)) {
6988 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
6989 unsigned long orig_val = val;
6992 * We get here when L2 changed cr0 in a way that did not change
6993 * any of L1's shadowed bits (see nested_vmx_exit_handled_cr),
6994 * but did change L0 shadowed bits. So we first calculate the
6995 * effective cr0 value that L1 would like to write into the
6996 * hardware. It consists of the L2-owned bits from the new
6997 * value combined with the L1-owned bits from L1's guest_cr0.
6999 val = (val & ~vmcs12->cr0_guest_host_mask) |
7000 (vmcs12->guest_cr0 & vmcs12->cr0_guest_host_mask);
7002 if (!nested_guest_cr0_valid(vcpu, val))
7005 if (kvm_set_cr0(vcpu, val))
7007 vmcs_writel(CR0_READ_SHADOW, orig_val);
7010 if (to_vmx(vcpu)->nested.vmxon &&
7011 !nested_host_cr0_valid(vcpu, val))
7014 return kvm_set_cr0(vcpu, val);
7018 static int handle_set_cr4(struct kvm_vcpu *vcpu, unsigned long val)
7020 if (is_guest_mode(vcpu)) {
7021 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
7022 unsigned long orig_val = val;
7024 /* analogously to handle_set_cr0 */
7025 val = (val & ~vmcs12->cr4_guest_host_mask) |
7026 (vmcs12->guest_cr4 & vmcs12->cr4_guest_host_mask);
7027 if (kvm_set_cr4(vcpu, val))
7029 vmcs_writel(CR4_READ_SHADOW, orig_val);
7032 return kvm_set_cr4(vcpu, val);
7035 static int handle_desc(struct kvm_vcpu *vcpu)
7037 WARN_ON(!(vcpu->arch.cr4 & X86_CR4_UMIP));
7038 return emulate_instruction(vcpu, 0) == EMULATE_DONE;
7041 static int handle_cr(struct kvm_vcpu *vcpu)
7043 unsigned long exit_qualification, val;
7049 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7050 cr = exit_qualification & 15;
7051 reg = (exit_qualification >> 8) & 15;
7052 switch ((exit_qualification >> 4) & 3) {
7053 case 0: /* mov to cr */
7054 val = kvm_register_readl(vcpu, reg);
7055 trace_kvm_cr_write(cr, val);
7058 err = handle_set_cr0(vcpu, val);
7059 return kvm_complete_insn_gp(vcpu, err);
7061 WARN_ON_ONCE(enable_unrestricted_guest);
7062 err = kvm_set_cr3(vcpu, val);
7063 return kvm_complete_insn_gp(vcpu, err);
7065 err = handle_set_cr4(vcpu, val);
7066 return kvm_complete_insn_gp(vcpu, err);
7068 u8 cr8_prev = kvm_get_cr8(vcpu);
7070 err = kvm_set_cr8(vcpu, cr8);
7071 ret = kvm_complete_insn_gp(vcpu, err);
7072 if (lapic_in_kernel(vcpu))
7074 if (cr8_prev <= cr8)
7077 * TODO: we might be squashing a
7078 * KVM_GUESTDBG_SINGLESTEP-triggered
7079 * KVM_EXIT_DEBUG here.
7081 vcpu->run->exit_reason = KVM_EXIT_SET_TPR;
7087 WARN_ONCE(1, "Guest should always own CR0.TS");
7088 vmx_set_cr0(vcpu, kvm_read_cr0_bits(vcpu, ~X86_CR0_TS));
7089 trace_kvm_cr_write(0, kvm_read_cr0(vcpu));
7090 return kvm_skip_emulated_instruction(vcpu);
7091 case 1: /*mov from cr*/
7094 WARN_ON_ONCE(enable_unrestricted_guest);
7095 val = kvm_read_cr3(vcpu);
7096 kvm_register_write(vcpu, reg, val);
7097 trace_kvm_cr_read(cr, val);
7098 return kvm_skip_emulated_instruction(vcpu);
7100 val = kvm_get_cr8(vcpu);
7101 kvm_register_write(vcpu, reg, val);
7102 trace_kvm_cr_read(cr, val);
7103 return kvm_skip_emulated_instruction(vcpu);
7107 val = (exit_qualification >> LMSW_SOURCE_DATA_SHIFT) & 0x0f;
7108 trace_kvm_cr_write(0, (kvm_read_cr0(vcpu) & ~0xful) | val);
7109 kvm_lmsw(vcpu, val);
7111 return kvm_skip_emulated_instruction(vcpu);
7115 vcpu->run->exit_reason = 0;
7116 vcpu_unimpl(vcpu, "unhandled control register: op %d cr %d\n",
7117 (int)(exit_qualification >> 4) & 3, cr);
7121 static int handle_dr(struct kvm_vcpu *vcpu)
7123 unsigned long exit_qualification;
7126 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7127 dr = exit_qualification & DEBUG_REG_ACCESS_NUM;
7129 /* First, if DR does not exist, trigger UD */
7130 if (!kvm_require_dr(vcpu, dr))
7133 /* Do not handle if the CPL > 0, will trigger GP on re-entry */
7134 if (!kvm_require_cpl(vcpu, 0))
7136 dr7 = vmcs_readl(GUEST_DR7);
7139 * As the vm-exit takes precedence over the debug trap, we
7140 * need to emulate the latter, either for the host or the
7141 * guest debugging itself.
7143 if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP) {
7144 vcpu->run->debug.arch.dr6 = vcpu->arch.dr6;
7145 vcpu->run->debug.arch.dr7 = dr7;
7146 vcpu->run->debug.arch.pc = kvm_get_linear_rip(vcpu);
7147 vcpu->run->debug.arch.exception = DB_VECTOR;
7148 vcpu->run->exit_reason = KVM_EXIT_DEBUG;
7151 vcpu->arch.dr6 &= ~15;
7152 vcpu->arch.dr6 |= DR6_BD | DR6_RTM;
7153 kvm_queue_exception(vcpu, DB_VECTOR);
7158 if (vcpu->guest_debug == 0) {
7159 vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL,
7160 CPU_BASED_MOV_DR_EXITING);
7163 * No more DR vmexits; force a reload of the debug registers
7164 * and reenter on this instruction. The next vmexit will
7165 * retrieve the full state of the debug registers.
7167 vcpu->arch.switch_db_regs |= KVM_DEBUGREG_WONT_EXIT;
7171 reg = DEBUG_REG_ACCESS_REG(exit_qualification);
7172 if (exit_qualification & TYPE_MOV_FROM_DR) {
7175 if (kvm_get_dr(vcpu, dr, &val))
7177 kvm_register_write(vcpu, reg, val);
7179 if (kvm_set_dr(vcpu, dr, kvm_register_readl(vcpu, reg)))
7182 return kvm_skip_emulated_instruction(vcpu);
7185 static u64 vmx_get_dr6(struct kvm_vcpu *vcpu)
7187 return vcpu->arch.dr6;
7190 static void vmx_set_dr6(struct kvm_vcpu *vcpu, unsigned long val)
7194 static void vmx_sync_dirty_debug_regs(struct kvm_vcpu *vcpu)
7196 get_debugreg(vcpu->arch.db[0], 0);
7197 get_debugreg(vcpu->arch.db[1], 1);
7198 get_debugreg(vcpu->arch.db[2], 2);
7199 get_debugreg(vcpu->arch.db[3], 3);
7200 get_debugreg(vcpu->arch.dr6, 6);
7201 vcpu->arch.dr7 = vmcs_readl(GUEST_DR7);
7203 vcpu->arch.switch_db_regs &= ~KVM_DEBUGREG_WONT_EXIT;
7204 vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL, CPU_BASED_MOV_DR_EXITING);
7207 static void vmx_set_dr7(struct kvm_vcpu *vcpu, unsigned long val)
7209 vmcs_writel(GUEST_DR7, val);
7212 static int handle_cpuid(struct kvm_vcpu *vcpu)
7214 return kvm_emulate_cpuid(vcpu);
7217 static int handle_rdmsr(struct kvm_vcpu *vcpu)
7219 u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX];
7220 struct msr_data msr_info;
7222 msr_info.index = ecx;
7223 msr_info.host_initiated = false;
7224 if (vmx_get_msr(vcpu, &msr_info)) {
7225 trace_kvm_msr_read_ex(ecx);
7226 kvm_inject_gp(vcpu, 0);
7230 trace_kvm_msr_read(ecx, msr_info.data);
7232 /* FIXME: handling of bits 32:63 of rax, rdx */
7233 vcpu->arch.regs[VCPU_REGS_RAX] = msr_info.data & -1u;
7234 vcpu->arch.regs[VCPU_REGS_RDX] = (msr_info.data >> 32) & -1u;
7235 return kvm_skip_emulated_instruction(vcpu);
7238 static int handle_wrmsr(struct kvm_vcpu *vcpu)
7240 struct msr_data msr;
7241 u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX];
7242 u64 data = (vcpu->arch.regs[VCPU_REGS_RAX] & -1u)
7243 | ((u64)(vcpu->arch.regs[VCPU_REGS_RDX] & -1u) << 32);
7247 msr.host_initiated = false;
7248 if (kvm_set_msr(vcpu, &msr) != 0) {
7249 trace_kvm_msr_write_ex(ecx, data);
7250 kvm_inject_gp(vcpu, 0);
7254 trace_kvm_msr_write(ecx, data);
7255 return kvm_skip_emulated_instruction(vcpu);
7258 static int handle_tpr_below_threshold(struct kvm_vcpu *vcpu)
7260 kvm_apic_update_ppr(vcpu);
7264 static int handle_interrupt_window(struct kvm_vcpu *vcpu)
7266 vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL,
7267 CPU_BASED_VIRTUAL_INTR_PENDING);
7269 kvm_make_request(KVM_REQ_EVENT, vcpu);
7271 ++vcpu->stat.irq_window_exits;
7275 static int handle_halt(struct kvm_vcpu *vcpu)
7277 return kvm_emulate_halt(vcpu);
7280 static int handle_vmcall(struct kvm_vcpu *vcpu)
7282 return kvm_emulate_hypercall(vcpu);
7285 static int handle_invd(struct kvm_vcpu *vcpu)
7287 return emulate_instruction(vcpu, 0) == EMULATE_DONE;
7290 static int handle_invlpg(struct kvm_vcpu *vcpu)
7292 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7294 kvm_mmu_invlpg(vcpu, exit_qualification);
7295 return kvm_skip_emulated_instruction(vcpu);
7298 static int handle_rdpmc(struct kvm_vcpu *vcpu)
7302 err = kvm_rdpmc(vcpu);
7303 return kvm_complete_insn_gp(vcpu, err);
7306 static int handle_wbinvd(struct kvm_vcpu *vcpu)
7308 return kvm_emulate_wbinvd(vcpu);
7311 static int handle_xsetbv(struct kvm_vcpu *vcpu)
7313 u64 new_bv = kvm_read_edx_eax(vcpu);
7314 u32 index = kvm_register_read(vcpu, VCPU_REGS_RCX);
7316 if (kvm_set_xcr(vcpu, index, new_bv) == 0)
7317 return kvm_skip_emulated_instruction(vcpu);
7321 static int handle_xsaves(struct kvm_vcpu *vcpu)
7323 kvm_skip_emulated_instruction(vcpu);
7324 WARN(1, "this should never happen\n");
7328 static int handle_xrstors(struct kvm_vcpu *vcpu)
7330 kvm_skip_emulated_instruction(vcpu);
7331 WARN(1, "this should never happen\n");
7335 static int handle_apic_access(struct kvm_vcpu *vcpu)
7337 if (likely(fasteoi)) {
7338 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7339 int access_type, offset;
7341 access_type = exit_qualification & APIC_ACCESS_TYPE;
7342 offset = exit_qualification & APIC_ACCESS_OFFSET;
7344 * Sane guest uses MOV to write EOI, with written value
7345 * not cared. So make a short-circuit here by avoiding
7346 * heavy instruction emulation.
7348 if ((access_type == TYPE_LINEAR_APIC_INST_WRITE) &&
7349 (offset == APIC_EOI)) {
7350 kvm_lapic_set_eoi(vcpu);
7351 return kvm_skip_emulated_instruction(vcpu);
7354 return emulate_instruction(vcpu, 0) == EMULATE_DONE;
7357 static int handle_apic_eoi_induced(struct kvm_vcpu *vcpu)
7359 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7360 int vector = exit_qualification & 0xff;
7362 /* EOI-induced VM exit is trap-like and thus no need to adjust IP */
7363 kvm_apic_set_eoi_accelerated(vcpu, vector);
7367 static int handle_apic_write(struct kvm_vcpu *vcpu)
7369 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7370 u32 offset = exit_qualification & 0xfff;
7372 /* APIC-write VM exit is trap-like and thus no need to adjust IP */
7373 kvm_apic_write_nodecode(vcpu, offset);
7377 static int handle_task_switch(struct kvm_vcpu *vcpu)
7379 struct vcpu_vmx *vmx = to_vmx(vcpu);
7380 unsigned long exit_qualification;
7381 bool has_error_code = false;
7384 int reason, type, idt_v, idt_index;
7386 idt_v = (vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK);
7387 idt_index = (vmx->idt_vectoring_info & VECTORING_INFO_VECTOR_MASK);
7388 type = (vmx->idt_vectoring_info & VECTORING_INFO_TYPE_MASK);
7390 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7392 reason = (u32)exit_qualification >> 30;
7393 if (reason == TASK_SWITCH_GATE && idt_v) {
7395 case INTR_TYPE_NMI_INTR:
7396 vcpu->arch.nmi_injected = false;
7397 vmx_set_nmi_mask(vcpu, true);
7399 case INTR_TYPE_EXT_INTR:
7400 case INTR_TYPE_SOFT_INTR:
7401 kvm_clear_interrupt_queue(vcpu);
7403 case INTR_TYPE_HARD_EXCEPTION:
7404 if (vmx->idt_vectoring_info &
7405 VECTORING_INFO_DELIVER_CODE_MASK) {
7406 has_error_code = true;
7408 vmcs_read32(IDT_VECTORING_ERROR_CODE);
7411 case INTR_TYPE_SOFT_EXCEPTION:
7412 kvm_clear_exception_queue(vcpu);
7418 tss_selector = exit_qualification;
7420 if (!idt_v || (type != INTR_TYPE_HARD_EXCEPTION &&
7421 type != INTR_TYPE_EXT_INTR &&
7422 type != INTR_TYPE_NMI_INTR))
7423 skip_emulated_instruction(vcpu);
7425 if (kvm_task_switch(vcpu, tss_selector,
7426 type == INTR_TYPE_SOFT_INTR ? idt_index : -1, reason,
7427 has_error_code, error_code) == EMULATE_FAIL) {
7428 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
7429 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
7430 vcpu->run->internal.ndata = 0;
7435 * TODO: What about debug traps on tss switch?
7436 * Are we supposed to inject them and update dr6?
7442 static int handle_ept_violation(struct kvm_vcpu *vcpu)
7444 unsigned long exit_qualification;
7448 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7451 * EPT violation happened while executing iret from NMI,
7452 * "blocked by NMI" bit has to be set before next VM entry.
7453 * There are errata that may cause this bit to not be set:
7456 if (!(to_vmx(vcpu)->idt_vectoring_info & VECTORING_INFO_VALID_MASK) &&
7458 (exit_qualification & INTR_INFO_UNBLOCK_NMI))
7459 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO, GUEST_INTR_STATE_NMI);
7461 gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS);
7462 trace_kvm_page_fault(gpa, exit_qualification);
7464 /* Is it a read fault? */
7465 error_code = (exit_qualification & EPT_VIOLATION_ACC_READ)
7466 ? PFERR_USER_MASK : 0;
7467 /* Is it a write fault? */
7468 error_code |= (exit_qualification & EPT_VIOLATION_ACC_WRITE)
7469 ? PFERR_WRITE_MASK : 0;
7470 /* Is it a fetch fault? */
7471 error_code |= (exit_qualification & EPT_VIOLATION_ACC_INSTR)
7472 ? PFERR_FETCH_MASK : 0;
7473 /* ept page table entry is present? */
7474 error_code |= (exit_qualification &
7475 (EPT_VIOLATION_READABLE | EPT_VIOLATION_WRITABLE |
7476 EPT_VIOLATION_EXECUTABLE))
7477 ? PFERR_PRESENT_MASK : 0;
7479 error_code |= (exit_qualification & 0x100) != 0 ?
7480 PFERR_GUEST_FINAL_MASK : PFERR_GUEST_PAGE_MASK;
7482 vcpu->arch.exit_qualification = exit_qualification;
7483 return kvm_mmu_page_fault(vcpu, gpa, error_code, NULL, 0);
7486 static int handle_ept_misconfig(struct kvm_vcpu *vcpu)
7491 * A nested guest cannot optimize MMIO vmexits, because we have an
7492 * nGPA here instead of the required GPA.
7494 gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS);
7495 if (!is_guest_mode(vcpu) &&
7496 !kvm_io_bus_write(vcpu, KVM_FAST_MMIO_BUS, gpa, 0, NULL)) {
7497 trace_kvm_fast_mmio(gpa);
7499 * Doing kvm_skip_emulated_instruction() depends on undefined
7500 * behavior: Intel's manual doesn't mandate
7501 * VM_EXIT_INSTRUCTION_LEN to be set in VMCS when EPT MISCONFIG
7502 * occurs and while on real hardware it was observed to be set,
7503 * other hypervisors (namely Hyper-V) don't set it, we end up
7504 * advancing IP with some random value. Disable fast mmio when
7505 * running nested and keep it for real hardware in hope that
7506 * VM_EXIT_INSTRUCTION_LEN will always be set correctly.
7508 if (!static_cpu_has(X86_FEATURE_HYPERVISOR))
7509 return kvm_skip_emulated_instruction(vcpu);
7511 return x86_emulate_instruction(vcpu, gpa, EMULTYPE_SKIP,
7512 NULL, 0) == EMULATE_DONE;
7515 return kvm_mmu_page_fault(vcpu, gpa, PFERR_RSVD_MASK, NULL, 0);
7518 static int handle_nmi_window(struct kvm_vcpu *vcpu)
7520 WARN_ON_ONCE(!enable_vnmi);
7521 vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL,
7522 CPU_BASED_VIRTUAL_NMI_PENDING);
7523 ++vcpu->stat.nmi_window_exits;
7524 kvm_make_request(KVM_REQ_EVENT, vcpu);
7529 static int handle_invalid_guest_state(struct kvm_vcpu *vcpu)
7531 struct vcpu_vmx *vmx = to_vmx(vcpu);
7532 enum emulation_result err = EMULATE_DONE;
7535 bool intr_window_requested;
7536 unsigned count = 130;
7539 * We should never reach the point where we are emulating L2
7540 * due to invalid guest state as that means we incorrectly
7541 * allowed a nested VMEntry with an invalid vmcs12.
7543 WARN_ON_ONCE(vmx->emulation_required && vmx->nested.nested_run_pending);
7545 cpu_exec_ctrl = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
7546 intr_window_requested = cpu_exec_ctrl & CPU_BASED_VIRTUAL_INTR_PENDING;
7548 while (vmx->emulation_required && count-- != 0) {
7549 if (intr_window_requested && vmx_interrupt_allowed(vcpu))
7550 return handle_interrupt_window(&vmx->vcpu);
7552 if (kvm_test_request(KVM_REQ_EVENT, vcpu))
7555 err = emulate_instruction(vcpu, 0);
7557 if (err == EMULATE_USER_EXIT) {
7558 ++vcpu->stat.mmio_exits;
7563 if (err != EMULATE_DONE)
7564 goto emulation_error;
7566 if (vmx->emulation_required && !vmx->rmode.vm86_active &&
7567 vcpu->arch.exception.pending)
7568 goto emulation_error;
7570 if (vcpu->arch.halt_request) {
7571 vcpu->arch.halt_request = 0;
7572 ret = kvm_vcpu_halt(vcpu);
7576 if (signal_pending(current))
7586 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
7587 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
7588 vcpu->run->internal.ndata = 0;
7592 static void grow_ple_window(struct kvm_vcpu *vcpu)
7594 struct vcpu_vmx *vmx = to_vmx(vcpu);
7595 int old = vmx->ple_window;
7597 vmx->ple_window = __grow_ple_window(old, ple_window,
7601 if (vmx->ple_window != old)
7602 vmx->ple_window_dirty = true;
7604 trace_kvm_ple_window_grow(vcpu->vcpu_id, vmx->ple_window, old);
7607 static void shrink_ple_window(struct kvm_vcpu *vcpu)
7609 struct vcpu_vmx *vmx = to_vmx(vcpu);
7610 int old = vmx->ple_window;
7612 vmx->ple_window = __shrink_ple_window(old, ple_window,
7616 if (vmx->ple_window != old)
7617 vmx->ple_window_dirty = true;
7619 trace_kvm_ple_window_shrink(vcpu->vcpu_id, vmx->ple_window, old);
7623 * Handler for POSTED_INTERRUPT_WAKEUP_VECTOR.
7625 static void wakeup_handler(void)
7627 struct kvm_vcpu *vcpu;
7628 int cpu = smp_processor_id();
7630 spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, cpu));
7631 list_for_each_entry(vcpu, &per_cpu(blocked_vcpu_on_cpu, cpu),
7632 blocked_vcpu_list) {
7633 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
7635 if (pi_test_on(pi_desc) == 1)
7636 kvm_vcpu_kick(vcpu);
7638 spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, cpu));
7641 static void vmx_enable_tdp(void)
7643 kvm_mmu_set_mask_ptes(VMX_EPT_READABLE_MASK,
7644 enable_ept_ad_bits ? VMX_EPT_ACCESS_BIT : 0ull,
7645 enable_ept_ad_bits ? VMX_EPT_DIRTY_BIT : 0ull,
7646 0ull, VMX_EPT_EXECUTABLE_MASK,
7647 cpu_has_vmx_ept_execute_only() ? 0ull : VMX_EPT_READABLE_MASK,
7648 VMX_EPT_RWX_MASK, 0ull);
7650 ept_set_mmio_spte_mask();
7654 static __init int hardware_setup(void)
7656 unsigned long host_bndcfgs;
7659 rdmsrl_safe(MSR_EFER, &host_efer);
7661 for (i = 0; i < ARRAY_SIZE(vmx_msr_index); ++i)
7662 kvm_define_shared_msr(i, vmx_msr_index[i]);
7664 for (i = 0; i < VMX_BITMAP_NR; i++) {
7665 vmx_bitmap[i] = (unsigned long *)__get_free_page(GFP_KERNEL);
7670 memset(vmx_vmread_bitmap, 0xff, PAGE_SIZE);
7671 memset(vmx_vmwrite_bitmap, 0xff, PAGE_SIZE);
7673 if (setup_vmcs_config(&vmcs_config) < 0) {
7678 if (boot_cpu_has(X86_FEATURE_NX))
7679 kvm_enable_efer_bits(EFER_NX);
7681 if (boot_cpu_has(X86_FEATURE_MPX)) {
7682 rdmsrl(MSR_IA32_BNDCFGS, host_bndcfgs);
7683 WARN_ONCE(host_bndcfgs, "KVM: BNDCFGS in host will be lost");
7686 if (!cpu_has_vmx_vpid() || !cpu_has_vmx_invvpid() ||
7687 !(cpu_has_vmx_invvpid_single() || cpu_has_vmx_invvpid_global()))
7690 if (!cpu_has_vmx_ept() ||
7691 !cpu_has_vmx_ept_4levels() ||
7692 !cpu_has_vmx_ept_mt_wb() ||
7693 !cpu_has_vmx_invept_global())
7696 if (!cpu_has_vmx_ept_ad_bits() || !enable_ept)
7697 enable_ept_ad_bits = 0;
7699 if (!cpu_has_vmx_unrestricted_guest() || !enable_ept)
7700 enable_unrestricted_guest = 0;
7702 if (!cpu_has_vmx_flexpriority())
7703 flexpriority_enabled = 0;
7705 if (!cpu_has_virtual_nmis())
7709 * set_apic_access_page_addr() is used to reload apic access
7710 * page upon invalidation. No need to do anything if not
7711 * using the APIC_ACCESS_ADDR VMCS field.
7713 if (!flexpriority_enabled)
7714 kvm_x86_ops->set_apic_access_page_addr = NULL;
7716 if (!cpu_has_vmx_tpr_shadow())
7717 kvm_x86_ops->update_cr8_intercept = NULL;
7719 if (enable_ept && !cpu_has_vmx_ept_2m_page())
7720 kvm_disable_largepages();
7722 #if IS_ENABLED(CONFIG_HYPERV)
7723 if (ms_hyperv.nested_features & HV_X64_NESTED_GUEST_MAPPING_FLUSH
7725 kvm_x86_ops->tlb_remote_flush = vmx_hv_remote_flush_tlb;
7728 if (!cpu_has_vmx_ple()) {
7731 ple_window_grow = 0;
7733 ple_window_shrink = 0;
7736 if (!cpu_has_vmx_apicv()) {
7738 kvm_x86_ops->sync_pir_to_irr = NULL;
7741 if (cpu_has_vmx_tsc_scaling()) {
7742 kvm_has_tsc_control = true;
7743 kvm_max_tsc_scaling_ratio = KVM_VMX_TSC_MULTIPLIER_MAX;
7744 kvm_tsc_scaling_ratio_frac_bits = 48;
7747 set_bit(0, vmx_vpid_bitmap); /* 0 is reserved for host */
7755 kvm_x86_ops->get_nested_state = NULL;
7756 kvm_x86_ops->set_nested_state = NULL;
7760 * Only enable PML when hardware supports PML feature, and both EPT
7761 * and EPT A/D bit features are enabled -- PML depends on them to work.
7763 if (!enable_ept || !enable_ept_ad_bits || !cpu_has_vmx_pml())
7767 kvm_x86_ops->slot_enable_log_dirty = NULL;
7768 kvm_x86_ops->slot_disable_log_dirty = NULL;
7769 kvm_x86_ops->flush_log_dirty = NULL;
7770 kvm_x86_ops->enable_log_dirty_pt_masked = NULL;
7773 if (cpu_has_vmx_preemption_timer() && enable_preemption_timer) {
7776 rdmsrl(MSR_IA32_VMX_MISC, vmx_msr);
7777 cpu_preemption_timer_multi =
7778 vmx_msr & VMX_MISC_PREEMPTION_TIMER_RATE_MASK;
7780 kvm_x86_ops->set_hv_timer = NULL;
7781 kvm_x86_ops->cancel_hv_timer = NULL;
7784 if (!cpu_has_vmx_shadow_vmcs())
7785 enable_shadow_vmcs = 0;
7786 if (enable_shadow_vmcs)
7787 init_vmcs_shadow_fields();
7789 kvm_set_posted_intr_wakeup_handler(wakeup_handler);
7790 nested_vmx_setup_ctls_msrs(&vmcs_config.nested, enable_apicv);
7792 kvm_mce_cap_supported |= MCG_LMCE_P;
7794 return alloc_kvm_area();
7797 for (i = 0; i < VMX_BITMAP_NR; i++)
7798 free_page((unsigned long)vmx_bitmap[i]);
7803 static __exit void hardware_unsetup(void)
7807 for (i = 0; i < VMX_BITMAP_NR; i++)
7808 free_page((unsigned long)vmx_bitmap[i]);
7814 * Indicate a busy-waiting vcpu in spinlock. We do not enable the PAUSE
7815 * exiting, so only get here on cpu with PAUSE-Loop-Exiting.
7817 static int handle_pause(struct kvm_vcpu *vcpu)
7819 if (!kvm_pause_in_guest(vcpu->kvm))
7820 grow_ple_window(vcpu);
7823 * Intel sdm vol3 ch-25.1.3 says: The "PAUSE-loop exiting"
7824 * VM-execution control is ignored if CPL > 0. OTOH, KVM
7825 * never set PAUSE_EXITING and just set PLE if supported,
7826 * so the vcpu must be CPL=0 if it gets a PAUSE exit.
7828 kvm_vcpu_on_spin(vcpu, true);
7829 return kvm_skip_emulated_instruction(vcpu);
7832 static int handle_nop(struct kvm_vcpu *vcpu)
7834 return kvm_skip_emulated_instruction(vcpu);
7837 static int handle_mwait(struct kvm_vcpu *vcpu)
7839 printk_once(KERN_WARNING "kvm: MWAIT instruction emulated as NOP!\n");
7840 return handle_nop(vcpu);
7843 static int handle_invalid_op(struct kvm_vcpu *vcpu)
7845 kvm_queue_exception(vcpu, UD_VECTOR);
7849 static int handle_monitor_trap(struct kvm_vcpu *vcpu)
7854 static int handle_monitor(struct kvm_vcpu *vcpu)
7856 printk_once(KERN_WARNING "kvm: MONITOR instruction emulated as NOP!\n");
7857 return handle_nop(vcpu);
7861 * The following 3 functions, nested_vmx_succeed()/failValid()/failInvalid(),
7862 * set the success or error code of an emulated VMX instruction, as specified
7863 * by Vol 2B, VMX Instruction Reference, "Conventions".
7865 static void nested_vmx_succeed(struct kvm_vcpu *vcpu)
7867 vmx_set_rflags(vcpu, vmx_get_rflags(vcpu)
7868 & ~(X86_EFLAGS_CF | X86_EFLAGS_PF | X86_EFLAGS_AF |
7869 X86_EFLAGS_ZF | X86_EFLAGS_SF | X86_EFLAGS_OF));
7872 static void nested_vmx_failInvalid(struct kvm_vcpu *vcpu)
7874 vmx_set_rflags(vcpu, (vmx_get_rflags(vcpu)
7875 & ~(X86_EFLAGS_PF | X86_EFLAGS_AF | X86_EFLAGS_ZF |
7876 X86_EFLAGS_SF | X86_EFLAGS_OF))
7880 static void nested_vmx_failValid(struct kvm_vcpu *vcpu,
7881 u32 vm_instruction_error)
7883 if (to_vmx(vcpu)->nested.current_vmptr == -1ull) {
7885 * failValid writes the error number to the current VMCS, which
7886 * can't be done there isn't a current VMCS.
7888 nested_vmx_failInvalid(vcpu);
7891 vmx_set_rflags(vcpu, (vmx_get_rflags(vcpu)
7892 & ~(X86_EFLAGS_CF | X86_EFLAGS_PF | X86_EFLAGS_AF |
7893 X86_EFLAGS_SF | X86_EFLAGS_OF))
7895 get_vmcs12(vcpu)->vm_instruction_error = vm_instruction_error;
7897 * We don't need to force a shadow sync because
7898 * VM_INSTRUCTION_ERROR is not shadowed
7902 static void nested_vmx_abort(struct kvm_vcpu *vcpu, u32 indicator)
7904 /* TODO: not to reset guest simply here. */
7905 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
7906 pr_debug_ratelimited("kvm: nested vmx abort, indicator %d\n", indicator);
7909 static enum hrtimer_restart vmx_preemption_timer_fn(struct hrtimer *timer)
7911 struct vcpu_vmx *vmx =
7912 container_of(timer, struct vcpu_vmx, nested.preemption_timer);
7914 vmx->nested.preemption_timer_expired = true;
7915 kvm_make_request(KVM_REQ_EVENT, &vmx->vcpu);
7916 kvm_vcpu_kick(&vmx->vcpu);
7918 return HRTIMER_NORESTART;
7922 * Decode the memory-address operand of a vmx instruction, as recorded on an
7923 * exit caused by such an instruction (run by a guest hypervisor).
7924 * On success, returns 0. When the operand is invalid, returns 1 and throws
7927 static int get_vmx_mem_address(struct kvm_vcpu *vcpu,
7928 unsigned long exit_qualification,
7929 u32 vmx_instruction_info, bool wr, gva_t *ret)
7933 struct kvm_segment s;
7936 * According to Vol. 3B, "Information for VM Exits Due to Instruction
7937 * Execution", on an exit, vmx_instruction_info holds most of the
7938 * addressing components of the operand. Only the displacement part
7939 * is put in exit_qualification (see 3B, "Basic VM-Exit Information").
7940 * For how an actual address is calculated from all these components,
7941 * refer to Vol. 1, "Operand Addressing".
7943 int scaling = vmx_instruction_info & 3;
7944 int addr_size = (vmx_instruction_info >> 7) & 7;
7945 bool is_reg = vmx_instruction_info & (1u << 10);
7946 int seg_reg = (vmx_instruction_info >> 15) & 7;
7947 int index_reg = (vmx_instruction_info >> 18) & 0xf;
7948 bool index_is_valid = !(vmx_instruction_info & (1u << 22));
7949 int base_reg = (vmx_instruction_info >> 23) & 0xf;
7950 bool base_is_valid = !(vmx_instruction_info & (1u << 27));
7953 kvm_queue_exception(vcpu, UD_VECTOR);
7957 /* Addr = segment_base + offset */
7958 /* offset = base + [index * scale] + displacement */
7959 off = exit_qualification; /* holds the displacement */
7961 off += kvm_register_read(vcpu, base_reg);
7963 off += kvm_register_read(vcpu, index_reg)<<scaling;
7964 vmx_get_segment(vcpu, &s, seg_reg);
7965 *ret = s.base + off;
7967 if (addr_size == 1) /* 32 bit */
7970 /* Checks for #GP/#SS exceptions. */
7972 if (is_long_mode(vcpu)) {
7973 /* Long mode: #GP(0)/#SS(0) if the memory address is in a
7974 * non-canonical form. This is the only check on the memory
7975 * destination for long mode!
7977 exn = is_noncanonical_address(*ret, vcpu);
7978 } else if (is_protmode(vcpu)) {
7979 /* Protected mode: apply checks for segment validity in the
7981 * - segment type check (#GP(0) may be thrown)
7982 * - usability check (#GP(0)/#SS(0))
7983 * - limit check (#GP(0)/#SS(0))
7986 /* #GP(0) if the destination operand is located in a
7987 * read-only data segment or any code segment.
7989 exn = ((s.type & 0xa) == 0 || (s.type & 8));
7991 /* #GP(0) if the source operand is located in an
7992 * execute-only code segment
7994 exn = ((s.type & 0xa) == 8);
7996 kvm_queue_exception_e(vcpu, GP_VECTOR, 0);
7999 /* Protected mode: #GP(0)/#SS(0) if the segment is unusable.
8001 exn = (s.unusable != 0);
8002 /* Protected mode: #GP(0)/#SS(0) if the memory
8003 * operand is outside the segment limit.
8005 exn = exn || (off + sizeof(u64) > s.limit);
8008 kvm_queue_exception_e(vcpu,
8009 seg_reg == VCPU_SREG_SS ?
8010 SS_VECTOR : GP_VECTOR,
8018 static int nested_vmx_get_vmptr(struct kvm_vcpu *vcpu, gpa_t *vmpointer)
8021 struct x86_exception e;
8023 if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
8024 vmcs_read32(VMX_INSTRUCTION_INFO), false, &gva))
8027 if (kvm_read_guest_virt(vcpu, gva, vmpointer, sizeof(*vmpointer), &e)) {
8028 kvm_inject_page_fault(vcpu, &e);
8036 * Allocate a shadow VMCS and associate it with the currently loaded
8037 * VMCS, unless such a shadow VMCS already exists. The newly allocated
8038 * VMCS is also VMCLEARed, so that it is ready for use.
8040 static struct vmcs *alloc_shadow_vmcs(struct kvm_vcpu *vcpu)
8042 struct vcpu_vmx *vmx = to_vmx(vcpu);
8043 struct loaded_vmcs *loaded_vmcs = vmx->loaded_vmcs;
8046 * We should allocate a shadow vmcs for vmcs01 only when L1
8047 * executes VMXON and free it when L1 executes VMXOFF.
8048 * As it is invalid to execute VMXON twice, we shouldn't reach
8049 * here when vmcs01 already have an allocated shadow vmcs.
8051 WARN_ON(loaded_vmcs == &vmx->vmcs01 && loaded_vmcs->shadow_vmcs);
8053 if (!loaded_vmcs->shadow_vmcs) {
8054 loaded_vmcs->shadow_vmcs = alloc_vmcs(true);
8055 if (loaded_vmcs->shadow_vmcs)
8056 vmcs_clear(loaded_vmcs->shadow_vmcs);
8058 return loaded_vmcs->shadow_vmcs;
8061 static int enter_vmx_operation(struct kvm_vcpu *vcpu)
8063 struct vcpu_vmx *vmx = to_vmx(vcpu);
8066 r = alloc_loaded_vmcs(&vmx->nested.vmcs02);
8070 vmx->nested.cached_vmcs12 = kmalloc(VMCS12_SIZE, GFP_KERNEL);
8071 if (!vmx->nested.cached_vmcs12)
8072 goto out_cached_vmcs12;
8074 vmx->nested.cached_shadow_vmcs12 = kmalloc(VMCS12_SIZE, GFP_KERNEL);
8075 if (!vmx->nested.cached_shadow_vmcs12)
8076 goto out_cached_shadow_vmcs12;
8078 if (enable_shadow_vmcs && !alloc_shadow_vmcs(vcpu))
8079 goto out_shadow_vmcs;
8081 hrtimer_init(&vmx->nested.preemption_timer, CLOCK_MONOTONIC,
8082 HRTIMER_MODE_REL_PINNED);
8083 vmx->nested.preemption_timer.function = vmx_preemption_timer_fn;
8085 vmx->nested.vmxon = true;
8089 kfree(vmx->nested.cached_shadow_vmcs12);
8091 out_cached_shadow_vmcs12:
8092 kfree(vmx->nested.cached_vmcs12);
8095 free_loaded_vmcs(&vmx->nested.vmcs02);
8102 * Emulate the VMXON instruction.
8103 * Currently, we just remember that VMX is active, and do not save or even
8104 * inspect the argument to VMXON (the so-called "VMXON pointer") because we
8105 * do not currently need to store anything in that guest-allocated memory
8106 * region. Consequently, VMCLEAR and VMPTRLD also do not verify that the their
8107 * argument is different from the VMXON pointer (which the spec says they do).
8109 static int handle_vmon(struct kvm_vcpu *vcpu)
8114 struct vcpu_vmx *vmx = to_vmx(vcpu);
8115 const u64 VMXON_NEEDED_FEATURES = FEATURE_CONTROL_LOCKED
8116 | FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
8119 * The Intel VMX Instruction Reference lists a bunch of bits that are
8120 * prerequisite to running VMXON, most notably cr4.VMXE must be set to
8121 * 1 (see vmx_set_cr4() for when we allow the guest to set this).
8122 * Otherwise, we should fail with #UD. But most faulting conditions
8123 * have already been checked by hardware, prior to the VM-exit for
8124 * VMXON. We do test guest cr4.VMXE because processor CR4 always has
8125 * that bit set to 1 in non-root mode.
8127 if (!kvm_read_cr4_bits(vcpu, X86_CR4_VMXE)) {
8128 kvm_queue_exception(vcpu, UD_VECTOR);
8132 /* CPL=0 must be checked manually. */
8133 if (vmx_get_cpl(vcpu)) {
8134 kvm_inject_gp(vcpu, 0);
8138 if (vmx->nested.vmxon) {
8139 nested_vmx_failValid(vcpu, VMXERR_VMXON_IN_VMX_ROOT_OPERATION);
8140 return kvm_skip_emulated_instruction(vcpu);
8143 if ((vmx->msr_ia32_feature_control & VMXON_NEEDED_FEATURES)
8144 != VMXON_NEEDED_FEATURES) {
8145 kvm_inject_gp(vcpu, 0);
8149 if (nested_vmx_get_vmptr(vcpu, &vmptr))
8154 * The first 4 bytes of VMXON region contain the supported
8155 * VMCS revision identifier
8157 * Note - IA32_VMX_BASIC[48] will never be 1 for the nested case;
8158 * which replaces physical address width with 32
8160 if (!PAGE_ALIGNED(vmptr) || (vmptr >> cpuid_maxphyaddr(vcpu))) {
8161 nested_vmx_failInvalid(vcpu);
8162 return kvm_skip_emulated_instruction(vcpu);
8165 page = kvm_vcpu_gpa_to_page(vcpu, vmptr);
8166 if (is_error_page(page)) {
8167 nested_vmx_failInvalid(vcpu);
8168 return kvm_skip_emulated_instruction(vcpu);
8170 if (*(u32 *)kmap(page) != VMCS12_REVISION) {
8172 kvm_release_page_clean(page);
8173 nested_vmx_failInvalid(vcpu);
8174 return kvm_skip_emulated_instruction(vcpu);
8177 kvm_release_page_clean(page);
8179 vmx->nested.vmxon_ptr = vmptr;
8180 ret = enter_vmx_operation(vcpu);
8184 nested_vmx_succeed(vcpu);
8185 return kvm_skip_emulated_instruction(vcpu);
8189 * Intel's VMX Instruction Reference specifies a common set of prerequisites
8190 * for running VMX instructions (except VMXON, whose prerequisites are
8191 * slightly different). It also specifies what exception to inject otherwise.
8192 * Note that many of these exceptions have priority over VM exits, so they
8193 * don't have to be checked again here.
8195 static int nested_vmx_check_permission(struct kvm_vcpu *vcpu)
8197 if (!to_vmx(vcpu)->nested.vmxon) {
8198 kvm_queue_exception(vcpu, UD_VECTOR);
8202 if (vmx_get_cpl(vcpu)) {
8203 kvm_inject_gp(vcpu, 0);
8210 static void vmx_disable_shadow_vmcs(struct vcpu_vmx *vmx)
8212 vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL, SECONDARY_EXEC_SHADOW_VMCS);
8213 vmcs_write64(VMCS_LINK_POINTER, -1ull);
8216 static inline void nested_release_vmcs12(struct vcpu_vmx *vmx)
8218 if (vmx->nested.current_vmptr == -1ull)
8221 if (enable_shadow_vmcs) {
8222 /* copy to memory all shadowed fields in case
8223 they were modified */
8224 copy_shadow_to_vmcs12(vmx);
8225 vmx->nested.sync_shadow_vmcs = false;
8226 vmx_disable_shadow_vmcs(vmx);
8228 vmx->nested.posted_intr_nv = -1;
8230 /* Flush VMCS12 to guest memory */
8231 kvm_vcpu_write_guest_page(&vmx->vcpu,
8232 vmx->nested.current_vmptr >> PAGE_SHIFT,
8233 vmx->nested.cached_vmcs12, 0, VMCS12_SIZE);
8235 vmx->nested.current_vmptr = -1ull;
8239 * Free whatever needs to be freed from vmx->nested when L1 goes down, or
8240 * just stops using VMX.
8242 static void free_nested(struct vcpu_vmx *vmx)
8244 if (!vmx->nested.vmxon && !vmx->nested.smm.vmxon)
8247 vmx->nested.vmxon = false;
8248 vmx->nested.smm.vmxon = false;
8249 free_vpid(vmx->nested.vpid02);
8250 vmx->nested.posted_intr_nv = -1;
8251 vmx->nested.current_vmptr = -1ull;
8252 if (enable_shadow_vmcs) {
8253 vmx_disable_shadow_vmcs(vmx);
8254 vmcs_clear(vmx->vmcs01.shadow_vmcs);
8255 free_vmcs(vmx->vmcs01.shadow_vmcs);
8256 vmx->vmcs01.shadow_vmcs = NULL;
8258 kfree(vmx->nested.cached_vmcs12);
8259 kfree(vmx->nested.cached_shadow_vmcs12);
8260 /* Unpin physical memory we referred to in the vmcs02 */
8261 if (vmx->nested.apic_access_page) {
8262 kvm_release_page_dirty(vmx->nested.apic_access_page);
8263 vmx->nested.apic_access_page = NULL;
8265 if (vmx->nested.virtual_apic_page) {
8266 kvm_release_page_dirty(vmx->nested.virtual_apic_page);
8267 vmx->nested.virtual_apic_page = NULL;
8269 if (vmx->nested.pi_desc_page) {
8270 kunmap(vmx->nested.pi_desc_page);
8271 kvm_release_page_dirty(vmx->nested.pi_desc_page);
8272 vmx->nested.pi_desc_page = NULL;
8273 vmx->nested.pi_desc = NULL;
8276 free_loaded_vmcs(&vmx->nested.vmcs02);
8279 /* Emulate the VMXOFF instruction */
8280 static int handle_vmoff(struct kvm_vcpu *vcpu)
8282 if (!nested_vmx_check_permission(vcpu))
8284 free_nested(to_vmx(vcpu));
8285 nested_vmx_succeed(vcpu);
8286 return kvm_skip_emulated_instruction(vcpu);
8289 /* Emulate the VMCLEAR instruction */
8290 static int handle_vmclear(struct kvm_vcpu *vcpu)
8292 struct vcpu_vmx *vmx = to_vmx(vcpu);
8296 if (!nested_vmx_check_permission(vcpu))
8299 if (nested_vmx_get_vmptr(vcpu, &vmptr))
8302 if (!PAGE_ALIGNED(vmptr) || (vmptr >> cpuid_maxphyaddr(vcpu))) {
8303 nested_vmx_failValid(vcpu, VMXERR_VMCLEAR_INVALID_ADDRESS);
8304 return kvm_skip_emulated_instruction(vcpu);
8307 if (vmptr == vmx->nested.vmxon_ptr) {
8308 nested_vmx_failValid(vcpu, VMXERR_VMCLEAR_VMXON_POINTER);
8309 return kvm_skip_emulated_instruction(vcpu);
8312 if (vmptr == vmx->nested.current_vmptr)
8313 nested_release_vmcs12(vmx);
8315 kvm_vcpu_write_guest(vcpu,
8316 vmptr + offsetof(struct vmcs12, launch_state),
8317 &zero, sizeof(zero));
8319 nested_vmx_succeed(vcpu);
8320 return kvm_skip_emulated_instruction(vcpu);
8323 static int nested_vmx_run(struct kvm_vcpu *vcpu, bool launch);
8325 /* Emulate the VMLAUNCH instruction */
8326 static int handle_vmlaunch(struct kvm_vcpu *vcpu)
8328 return nested_vmx_run(vcpu, true);
8331 /* Emulate the VMRESUME instruction */
8332 static int handle_vmresume(struct kvm_vcpu *vcpu)
8335 return nested_vmx_run(vcpu, false);
8339 * Read a vmcs12 field. Since these can have varying lengths and we return
8340 * one type, we chose the biggest type (u64) and zero-extend the return value
8341 * to that size. Note that the caller, handle_vmread, might need to use only
8342 * some of the bits we return here (e.g., on 32-bit guests, only 32 bits of
8343 * 64-bit fields are to be returned).
8345 static inline int vmcs12_read_any(struct vmcs12 *vmcs12,
8346 unsigned long field, u64 *ret)
8348 short offset = vmcs_field_to_offset(field);
8354 p = (char *)vmcs12 + offset;
8356 switch (vmcs_field_width(field)) {
8357 case VMCS_FIELD_WIDTH_NATURAL_WIDTH:
8358 *ret = *((natural_width *)p);
8360 case VMCS_FIELD_WIDTH_U16:
8363 case VMCS_FIELD_WIDTH_U32:
8366 case VMCS_FIELD_WIDTH_U64:
8376 static inline int vmcs12_write_any(struct vmcs12 *vmcs12,
8377 unsigned long field, u64 field_value){
8378 short offset = vmcs_field_to_offset(field);
8379 char *p = (char *)vmcs12 + offset;
8383 switch (vmcs_field_width(field)) {
8384 case VMCS_FIELD_WIDTH_U16:
8385 *(u16 *)p = field_value;
8387 case VMCS_FIELD_WIDTH_U32:
8388 *(u32 *)p = field_value;
8390 case VMCS_FIELD_WIDTH_U64:
8391 *(u64 *)p = field_value;
8393 case VMCS_FIELD_WIDTH_NATURAL_WIDTH:
8394 *(natural_width *)p = field_value;
8404 * Copy the writable VMCS shadow fields back to the VMCS12, in case
8405 * they have been modified by the L1 guest. Note that the "read-only"
8406 * VM-exit information fields are actually writable if the vCPU is
8407 * configured to support "VMWRITE to any supported field in the VMCS."
8409 static void copy_shadow_to_vmcs12(struct vcpu_vmx *vmx)
8411 const u16 *fields[] = {
8412 shadow_read_write_fields,
8413 shadow_read_only_fields
8415 const int max_fields[] = {
8416 max_shadow_read_write_fields,
8417 max_shadow_read_only_fields
8420 unsigned long field;
8422 struct vmcs *shadow_vmcs = vmx->vmcs01.shadow_vmcs;
8426 vmcs_load(shadow_vmcs);
8428 for (q = 0; q < ARRAY_SIZE(fields); q++) {
8429 for (i = 0; i < max_fields[q]; i++) {
8430 field = fields[q][i];
8431 field_value = __vmcs_readl(field);
8432 vmcs12_write_any(get_vmcs12(&vmx->vcpu), field, field_value);
8435 * Skip the VM-exit information fields if they are read-only.
8437 if (!nested_cpu_has_vmwrite_any_field(&vmx->vcpu))
8441 vmcs_clear(shadow_vmcs);
8442 vmcs_load(vmx->loaded_vmcs->vmcs);
8447 static void copy_vmcs12_to_shadow(struct vcpu_vmx *vmx)
8449 const u16 *fields[] = {
8450 shadow_read_write_fields,
8451 shadow_read_only_fields
8453 const int max_fields[] = {
8454 max_shadow_read_write_fields,
8455 max_shadow_read_only_fields
8458 unsigned long field;
8459 u64 field_value = 0;
8460 struct vmcs *shadow_vmcs = vmx->vmcs01.shadow_vmcs;
8462 vmcs_load(shadow_vmcs);
8464 for (q = 0; q < ARRAY_SIZE(fields); q++) {
8465 for (i = 0; i < max_fields[q]; i++) {
8466 field = fields[q][i];
8467 vmcs12_read_any(get_vmcs12(&vmx->vcpu), field, &field_value);
8468 __vmcs_writel(field, field_value);
8472 vmcs_clear(shadow_vmcs);
8473 vmcs_load(vmx->loaded_vmcs->vmcs);
8477 * VMX instructions which assume a current vmcs12 (i.e., that VMPTRLD was
8478 * used before) all generate the same failure when it is missing.
8480 static int nested_vmx_check_vmcs12(struct kvm_vcpu *vcpu)
8482 struct vcpu_vmx *vmx = to_vmx(vcpu);
8483 if (vmx->nested.current_vmptr == -1ull) {
8484 nested_vmx_failInvalid(vcpu);
8490 static int handle_vmread(struct kvm_vcpu *vcpu)
8492 unsigned long field;
8494 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
8495 u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
8497 struct vmcs12 *vmcs12;
8499 if (!nested_vmx_check_permission(vcpu))
8502 if (!nested_vmx_check_vmcs12(vcpu))
8503 return kvm_skip_emulated_instruction(vcpu);
8505 if (!is_guest_mode(vcpu))
8506 vmcs12 = get_vmcs12(vcpu);
8509 * When vmcs->vmcs_link_pointer is -1ull, any VMREAD
8510 * to shadowed-field sets the ALU flags for VMfailInvalid.
8512 if (get_vmcs12(vcpu)->vmcs_link_pointer == -1ull) {
8513 nested_vmx_failInvalid(vcpu);
8514 return kvm_skip_emulated_instruction(vcpu);
8516 vmcs12 = get_shadow_vmcs12(vcpu);
8519 /* Decode instruction info and find the field to read */
8520 field = kvm_register_readl(vcpu, (((vmx_instruction_info) >> 28) & 0xf));
8521 /* Read the field, zero-extended to a u64 field_value */
8522 if (vmcs12_read_any(vmcs12, field, &field_value) < 0) {
8523 nested_vmx_failValid(vcpu, VMXERR_UNSUPPORTED_VMCS_COMPONENT);
8524 return kvm_skip_emulated_instruction(vcpu);
8527 * Now copy part of this value to register or memory, as requested.
8528 * Note that the number of bits actually copied is 32 or 64 depending
8529 * on the guest's mode (32 or 64 bit), not on the given field's length.
8531 if (vmx_instruction_info & (1u << 10)) {
8532 kvm_register_writel(vcpu, (((vmx_instruction_info) >> 3) & 0xf),
8535 if (get_vmx_mem_address(vcpu, exit_qualification,
8536 vmx_instruction_info, true, &gva))
8538 /* _system ok, nested_vmx_check_permission has verified cpl=0 */
8539 kvm_write_guest_virt_system(vcpu, gva, &field_value,
8540 (is_long_mode(vcpu) ? 8 : 4), NULL);
8543 nested_vmx_succeed(vcpu);
8544 return kvm_skip_emulated_instruction(vcpu);
8548 static int handle_vmwrite(struct kvm_vcpu *vcpu)
8550 unsigned long field;
8552 struct vcpu_vmx *vmx = to_vmx(vcpu);
8553 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
8554 u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
8556 /* The value to write might be 32 or 64 bits, depending on L1's long
8557 * mode, and eventually we need to write that into a field of several
8558 * possible lengths. The code below first zero-extends the value to 64
8559 * bit (field_value), and then copies only the appropriate number of
8560 * bits into the vmcs12 field.
8562 u64 field_value = 0;
8563 struct x86_exception e;
8564 struct vmcs12 *vmcs12;
8566 if (!nested_vmx_check_permission(vcpu))
8569 if (!nested_vmx_check_vmcs12(vcpu))
8570 return kvm_skip_emulated_instruction(vcpu);
8572 if (vmx_instruction_info & (1u << 10))
8573 field_value = kvm_register_readl(vcpu,
8574 (((vmx_instruction_info) >> 3) & 0xf));
8576 if (get_vmx_mem_address(vcpu, exit_qualification,
8577 vmx_instruction_info, false, &gva))
8579 if (kvm_read_guest_virt(vcpu, gva, &field_value,
8580 (is_64_bit_mode(vcpu) ? 8 : 4), &e)) {
8581 kvm_inject_page_fault(vcpu, &e);
8587 field = kvm_register_readl(vcpu, (((vmx_instruction_info) >> 28) & 0xf));
8589 * If the vCPU supports "VMWRITE to any supported field in the
8590 * VMCS," then the "read-only" fields are actually read/write.
8592 if (vmcs_field_readonly(field) &&
8593 !nested_cpu_has_vmwrite_any_field(vcpu)) {
8594 nested_vmx_failValid(vcpu,
8595 VMXERR_VMWRITE_READ_ONLY_VMCS_COMPONENT);
8596 return kvm_skip_emulated_instruction(vcpu);
8599 if (!is_guest_mode(vcpu))
8600 vmcs12 = get_vmcs12(vcpu);
8603 * When vmcs->vmcs_link_pointer is -1ull, any VMWRITE
8604 * to shadowed-field sets the ALU flags for VMfailInvalid.
8606 if (get_vmcs12(vcpu)->vmcs_link_pointer == -1ull) {
8607 nested_vmx_failInvalid(vcpu);
8608 return kvm_skip_emulated_instruction(vcpu);
8610 vmcs12 = get_shadow_vmcs12(vcpu);
8614 if (vmcs12_write_any(vmcs12, field, field_value) < 0) {
8615 nested_vmx_failValid(vcpu, VMXERR_UNSUPPORTED_VMCS_COMPONENT);
8616 return kvm_skip_emulated_instruction(vcpu);
8620 * Do not track vmcs12 dirty-state if in guest-mode
8621 * as we actually dirty shadow vmcs12 instead of vmcs12.
8623 if (!is_guest_mode(vcpu)) {
8625 #define SHADOW_FIELD_RW(x) case x:
8626 #include "vmx_shadow_fields.h"
8628 * The fields that can be updated by L1 without a vmexit are
8629 * always updated in the vmcs02, the others go down the slow
8630 * path of prepare_vmcs02.
8634 vmx->nested.dirty_vmcs12 = true;
8639 nested_vmx_succeed(vcpu);
8640 return kvm_skip_emulated_instruction(vcpu);
8643 static void set_current_vmptr(struct vcpu_vmx *vmx, gpa_t vmptr)
8645 vmx->nested.current_vmptr = vmptr;
8646 if (enable_shadow_vmcs) {
8647 vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL,
8648 SECONDARY_EXEC_SHADOW_VMCS);
8649 vmcs_write64(VMCS_LINK_POINTER,
8650 __pa(vmx->vmcs01.shadow_vmcs));
8651 vmx->nested.sync_shadow_vmcs = true;
8653 vmx->nested.dirty_vmcs12 = true;
8656 /* Emulate the VMPTRLD instruction */
8657 static int handle_vmptrld(struct kvm_vcpu *vcpu)
8659 struct vcpu_vmx *vmx = to_vmx(vcpu);
8662 if (!nested_vmx_check_permission(vcpu))
8665 if (nested_vmx_get_vmptr(vcpu, &vmptr))
8668 if (!PAGE_ALIGNED(vmptr) || (vmptr >> cpuid_maxphyaddr(vcpu))) {
8669 nested_vmx_failValid(vcpu, VMXERR_VMPTRLD_INVALID_ADDRESS);
8670 return kvm_skip_emulated_instruction(vcpu);
8673 if (vmptr == vmx->nested.vmxon_ptr) {
8674 nested_vmx_failValid(vcpu, VMXERR_VMPTRLD_VMXON_POINTER);
8675 return kvm_skip_emulated_instruction(vcpu);
8678 if (vmx->nested.current_vmptr != vmptr) {
8679 struct vmcs12 *new_vmcs12;
8681 page = kvm_vcpu_gpa_to_page(vcpu, vmptr);
8682 if (is_error_page(page)) {
8683 nested_vmx_failInvalid(vcpu);
8684 return kvm_skip_emulated_instruction(vcpu);
8686 new_vmcs12 = kmap(page);
8687 if (new_vmcs12->hdr.revision_id != VMCS12_REVISION ||
8688 (new_vmcs12->hdr.shadow_vmcs &&
8689 !nested_cpu_has_vmx_shadow_vmcs(vcpu))) {
8691 kvm_release_page_clean(page);
8692 nested_vmx_failValid(vcpu,
8693 VMXERR_VMPTRLD_INCORRECT_VMCS_REVISION_ID);
8694 return kvm_skip_emulated_instruction(vcpu);
8697 nested_release_vmcs12(vmx);
8699 * Load VMCS12 from guest memory since it is not already
8702 memcpy(vmx->nested.cached_vmcs12, new_vmcs12, VMCS12_SIZE);
8704 kvm_release_page_clean(page);
8706 set_current_vmptr(vmx, vmptr);
8709 nested_vmx_succeed(vcpu);
8710 return kvm_skip_emulated_instruction(vcpu);
8713 /* Emulate the VMPTRST instruction */
8714 static int handle_vmptrst(struct kvm_vcpu *vcpu)
8716 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
8717 u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
8719 struct x86_exception e;
8721 if (!nested_vmx_check_permission(vcpu))
8724 if (get_vmx_mem_address(vcpu, exit_qualification,
8725 vmx_instruction_info, true, &vmcs_gva))
8727 /* *_system ok, nested_vmx_check_permission has verified cpl=0 */
8728 if (kvm_write_guest_virt_system(vcpu, vmcs_gva,
8729 (void *)&to_vmx(vcpu)->nested.current_vmptr,
8731 kvm_inject_page_fault(vcpu, &e);
8734 nested_vmx_succeed(vcpu);
8735 return kvm_skip_emulated_instruction(vcpu);
8738 /* Emulate the INVEPT instruction */
8739 static int handle_invept(struct kvm_vcpu *vcpu)
8741 struct vcpu_vmx *vmx = to_vmx(vcpu);
8742 u32 vmx_instruction_info, types;
8745 struct x86_exception e;
8750 if (!(vmx->nested.msrs.secondary_ctls_high &
8751 SECONDARY_EXEC_ENABLE_EPT) ||
8752 !(vmx->nested.msrs.ept_caps & VMX_EPT_INVEPT_BIT)) {
8753 kvm_queue_exception(vcpu, UD_VECTOR);
8757 if (!nested_vmx_check_permission(vcpu))
8760 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
8761 type = kvm_register_readl(vcpu, (vmx_instruction_info >> 28) & 0xf);
8763 types = (vmx->nested.msrs.ept_caps >> VMX_EPT_EXTENT_SHIFT) & 6;
8765 if (type >= 32 || !(types & (1 << type))) {
8766 nested_vmx_failValid(vcpu,
8767 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
8768 return kvm_skip_emulated_instruction(vcpu);
8771 /* According to the Intel VMX instruction reference, the memory
8772 * operand is read even if it isn't needed (e.g., for type==global)
8774 if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
8775 vmx_instruction_info, false, &gva))
8777 if (kvm_read_guest_virt(vcpu, gva, &operand, sizeof(operand), &e)) {
8778 kvm_inject_page_fault(vcpu, &e);
8783 case VMX_EPT_EXTENT_GLOBAL:
8785 * TODO: track mappings and invalidate
8786 * single context requests appropriately
8788 case VMX_EPT_EXTENT_CONTEXT:
8789 kvm_mmu_sync_roots(vcpu);
8790 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
8791 nested_vmx_succeed(vcpu);
8798 return kvm_skip_emulated_instruction(vcpu);
8801 static int handle_invvpid(struct kvm_vcpu *vcpu)
8803 struct vcpu_vmx *vmx = to_vmx(vcpu);
8804 u32 vmx_instruction_info;
8805 unsigned long type, types;
8807 struct x86_exception e;
8813 if (!(vmx->nested.msrs.secondary_ctls_high &
8814 SECONDARY_EXEC_ENABLE_VPID) ||
8815 !(vmx->nested.msrs.vpid_caps & VMX_VPID_INVVPID_BIT)) {
8816 kvm_queue_exception(vcpu, UD_VECTOR);
8820 if (!nested_vmx_check_permission(vcpu))
8823 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
8824 type = kvm_register_readl(vcpu, (vmx_instruction_info >> 28) & 0xf);
8826 types = (vmx->nested.msrs.vpid_caps &
8827 VMX_VPID_EXTENT_SUPPORTED_MASK) >> 8;
8829 if (type >= 32 || !(types & (1 << type))) {
8830 nested_vmx_failValid(vcpu,
8831 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
8832 return kvm_skip_emulated_instruction(vcpu);
8835 /* according to the intel vmx instruction reference, the memory
8836 * operand is read even if it isn't needed (e.g., for type==global)
8838 if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
8839 vmx_instruction_info, false, &gva))
8841 if (kvm_read_guest_virt(vcpu, gva, &operand, sizeof(operand), &e)) {
8842 kvm_inject_page_fault(vcpu, &e);
8845 if (operand.vpid >> 16) {
8846 nested_vmx_failValid(vcpu,
8847 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
8848 return kvm_skip_emulated_instruction(vcpu);
8852 case VMX_VPID_EXTENT_INDIVIDUAL_ADDR:
8853 if (!operand.vpid ||
8854 is_noncanonical_address(operand.gla, vcpu)) {
8855 nested_vmx_failValid(vcpu,
8856 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
8857 return kvm_skip_emulated_instruction(vcpu);
8859 if (cpu_has_vmx_invvpid_individual_addr() &&
8860 vmx->nested.vpid02) {
8861 __invvpid(VMX_VPID_EXTENT_INDIVIDUAL_ADDR,
8862 vmx->nested.vpid02, operand.gla);
8864 __vmx_flush_tlb(vcpu, vmx->nested.vpid02, true);
8866 case VMX_VPID_EXTENT_SINGLE_CONTEXT:
8867 case VMX_VPID_EXTENT_SINGLE_NON_GLOBAL:
8868 if (!operand.vpid) {
8869 nested_vmx_failValid(vcpu,
8870 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
8871 return kvm_skip_emulated_instruction(vcpu);
8873 __vmx_flush_tlb(vcpu, vmx->nested.vpid02, true);
8875 case VMX_VPID_EXTENT_ALL_CONTEXT:
8876 __vmx_flush_tlb(vcpu, vmx->nested.vpid02, true);
8880 return kvm_skip_emulated_instruction(vcpu);
8883 nested_vmx_succeed(vcpu);
8885 return kvm_skip_emulated_instruction(vcpu);
8888 static int handle_invpcid(struct kvm_vcpu *vcpu)
8890 u32 vmx_instruction_info;
8894 struct x86_exception e;
8896 unsigned long roots_to_free = 0;
8902 if (!guest_cpuid_has(vcpu, X86_FEATURE_INVPCID)) {
8903 kvm_queue_exception(vcpu, UD_VECTOR);
8907 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
8908 type = kvm_register_readl(vcpu, (vmx_instruction_info >> 28) & 0xf);
8911 kvm_inject_gp(vcpu, 0);
8915 /* According to the Intel instruction reference, the memory operand
8916 * is read even if it isn't needed (e.g., for type==all)
8918 if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
8919 vmx_instruction_info, false, &gva))
8922 if (kvm_read_guest_virt(vcpu, gva, &operand, sizeof(operand), &e)) {
8923 kvm_inject_page_fault(vcpu, &e);
8927 if (operand.pcid >> 12 != 0) {
8928 kvm_inject_gp(vcpu, 0);
8932 pcid_enabled = kvm_read_cr4_bits(vcpu, X86_CR4_PCIDE);
8935 case INVPCID_TYPE_INDIV_ADDR:
8936 if ((!pcid_enabled && (operand.pcid != 0)) ||
8937 is_noncanonical_address(operand.gla, vcpu)) {
8938 kvm_inject_gp(vcpu, 0);
8941 kvm_mmu_invpcid_gva(vcpu, operand.gla, operand.pcid);
8942 return kvm_skip_emulated_instruction(vcpu);
8944 case INVPCID_TYPE_SINGLE_CTXT:
8945 if (!pcid_enabled && (operand.pcid != 0)) {
8946 kvm_inject_gp(vcpu, 0);
8950 if (kvm_get_active_pcid(vcpu) == operand.pcid) {
8951 kvm_mmu_sync_roots(vcpu);
8952 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
8955 for (i = 0; i < KVM_MMU_NUM_PREV_ROOTS; i++)
8956 if (kvm_get_pcid(vcpu, vcpu->arch.mmu.prev_roots[i].cr3)
8958 roots_to_free |= KVM_MMU_ROOT_PREVIOUS(i);
8960 kvm_mmu_free_roots(vcpu, roots_to_free);
8962 * If neither the current cr3 nor any of the prev_roots use the
8963 * given PCID, then nothing needs to be done here because a
8964 * resync will happen anyway before switching to any other CR3.
8967 return kvm_skip_emulated_instruction(vcpu);
8969 case INVPCID_TYPE_ALL_NON_GLOBAL:
8971 * Currently, KVM doesn't mark global entries in the shadow
8972 * page tables, so a non-global flush just degenerates to a
8973 * global flush. If needed, we could optimize this later by
8974 * keeping track of global entries in shadow page tables.
8978 case INVPCID_TYPE_ALL_INCL_GLOBAL:
8979 kvm_mmu_unload(vcpu);
8980 return kvm_skip_emulated_instruction(vcpu);
8983 BUG(); /* We have already checked above that type <= 3 */
8987 static int handle_pml_full(struct kvm_vcpu *vcpu)
8989 unsigned long exit_qualification;
8991 trace_kvm_pml_full(vcpu->vcpu_id);
8993 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
8996 * PML buffer FULL happened while executing iret from NMI,
8997 * "blocked by NMI" bit has to be set before next VM entry.
8999 if (!(to_vmx(vcpu)->idt_vectoring_info & VECTORING_INFO_VALID_MASK) &&
9001 (exit_qualification & INTR_INFO_UNBLOCK_NMI))
9002 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
9003 GUEST_INTR_STATE_NMI);
9006 * PML buffer already flushed at beginning of VMEXIT. Nothing to do
9007 * here.., and there's no userspace involvement needed for PML.
9012 static int handle_preemption_timer(struct kvm_vcpu *vcpu)
9014 kvm_lapic_expired_hv_timer(vcpu);
9018 static bool valid_ept_address(struct kvm_vcpu *vcpu, u64 address)
9020 struct vcpu_vmx *vmx = to_vmx(vcpu);
9021 int maxphyaddr = cpuid_maxphyaddr(vcpu);
9023 /* Check for memory type validity */
9024 switch (address & VMX_EPTP_MT_MASK) {
9025 case VMX_EPTP_MT_UC:
9026 if (!(vmx->nested.msrs.ept_caps & VMX_EPTP_UC_BIT))
9029 case VMX_EPTP_MT_WB:
9030 if (!(vmx->nested.msrs.ept_caps & VMX_EPTP_WB_BIT))
9037 /* only 4 levels page-walk length are valid */
9038 if ((address & VMX_EPTP_PWL_MASK) != VMX_EPTP_PWL_4)
9041 /* Reserved bits should not be set */
9042 if (address >> maxphyaddr || ((address >> 7) & 0x1f))
9045 /* AD, if set, should be supported */
9046 if (address & VMX_EPTP_AD_ENABLE_BIT) {
9047 if (!(vmx->nested.msrs.ept_caps & VMX_EPT_AD_BIT))
9054 static int nested_vmx_eptp_switching(struct kvm_vcpu *vcpu,
9055 struct vmcs12 *vmcs12)
9057 u32 index = vcpu->arch.regs[VCPU_REGS_RCX];
9059 bool accessed_dirty;
9060 struct kvm_mmu *mmu = vcpu->arch.walk_mmu;
9062 if (!nested_cpu_has_eptp_switching(vmcs12) ||
9063 !nested_cpu_has_ept(vmcs12))
9066 if (index >= VMFUNC_EPTP_ENTRIES)
9070 if (kvm_vcpu_read_guest_page(vcpu, vmcs12->eptp_list_address >> PAGE_SHIFT,
9071 &address, index * 8, 8))
9074 accessed_dirty = !!(address & VMX_EPTP_AD_ENABLE_BIT);
9077 * If the (L2) guest does a vmfunc to the currently
9078 * active ept pointer, we don't have to do anything else
9080 if (vmcs12->ept_pointer != address) {
9081 if (!valid_ept_address(vcpu, address))
9084 kvm_mmu_unload(vcpu);
9085 mmu->ept_ad = accessed_dirty;
9086 mmu->base_role.ad_disabled = !accessed_dirty;
9087 vmcs12->ept_pointer = address;
9089 * TODO: Check what's the correct approach in case
9090 * mmu reload fails. Currently, we just let the next
9091 * reload potentially fail
9093 kvm_mmu_reload(vcpu);
9099 static int handle_vmfunc(struct kvm_vcpu *vcpu)
9101 struct vcpu_vmx *vmx = to_vmx(vcpu);
9102 struct vmcs12 *vmcs12;
9103 u32 function = vcpu->arch.regs[VCPU_REGS_RAX];
9106 * VMFUNC is only supported for nested guests, but we always enable the
9107 * secondary control for simplicity; for non-nested mode, fake that we
9108 * didn't by injecting #UD.
9110 if (!is_guest_mode(vcpu)) {
9111 kvm_queue_exception(vcpu, UD_VECTOR);
9115 vmcs12 = get_vmcs12(vcpu);
9116 if ((vmcs12->vm_function_control & (1 << function)) == 0)
9121 if (nested_vmx_eptp_switching(vcpu, vmcs12))
9127 return kvm_skip_emulated_instruction(vcpu);
9130 nested_vmx_vmexit(vcpu, vmx->exit_reason,
9131 vmcs_read32(VM_EXIT_INTR_INFO),
9132 vmcs_readl(EXIT_QUALIFICATION));
9137 * The exit handlers return 1 if the exit was handled fully and guest execution
9138 * may resume. Otherwise they set the kvm_run parameter to indicate what needs
9139 * to be done to userspace and return 0.
9141 static int (*const kvm_vmx_exit_handlers[])(struct kvm_vcpu *vcpu) = {
9142 [EXIT_REASON_EXCEPTION_NMI] = handle_exception,
9143 [EXIT_REASON_EXTERNAL_INTERRUPT] = handle_external_interrupt,
9144 [EXIT_REASON_TRIPLE_FAULT] = handle_triple_fault,
9145 [EXIT_REASON_NMI_WINDOW] = handle_nmi_window,
9146 [EXIT_REASON_IO_INSTRUCTION] = handle_io,
9147 [EXIT_REASON_CR_ACCESS] = handle_cr,
9148 [EXIT_REASON_DR_ACCESS] = handle_dr,
9149 [EXIT_REASON_CPUID] = handle_cpuid,
9150 [EXIT_REASON_MSR_READ] = handle_rdmsr,
9151 [EXIT_REASON_MSR_WRITE] = handle_wrmsr,
9152 [EXIT_REASON_PENDING_INTERRUPT] = handle_interrupt_window,
9153 [EXIT_REASON_HLT] = handle_halt,
9154 [EXIT_REASON_INVD] = handle_invd,
9155 [EXIT_REASON_INVLPG] = handle_invlpg,
9156 [EXIT_REASON_RDPMC] = handle_rdpmc,
9157 [EXIT_REASON_VMCALL] = handle_vmcall,
9158 [EXIT_REASON_VMCLEAR] = handle_vmclear,
9159 [EXIT_REASON_VMLAUNCH] = handle_vmlaunch,
9160 [EXIT_REASON_VMPTRLD] = handle_vmptrld,
9161 [EXIT_REASON_VMPTRST] = handle_vmptrst,
9162 [EXIT_REASON_VMREAD] = handle_vmread,
9163 [EXIT_REASON_VMRESUME] = handle_vmresume,
9164 [EXIT_REASON_VMWRITE] = handle_vmwrite,
9165 [EXIT_REASON_VMOFF] = handle_vmoff,
9166 [EXIT_REASON_VMON] = handle_vmon,
9167 [EXIT_REASON_TPR_BELOW_THRESHOLD] = handle_tpr_below_threshold,
9168 [EXIT_REASON_APIC_ACCESS] = handle_apic_access,
9169 [EXIT_REASON_APIC_WRITE] = handle_apic_write,
9170 [EXIT_REASON_EOI_INDUCED] = handle_apic_eoi_induced,
9171 [EXIT_REASON_WBINVD] = handle_wbinvd,
9172 [EXIT_REASON_XSETBV] = handle_xsetbv,
9173 [EXIT_REASON_TASK_SWITCH] = handle_task_switch,
9174 [EXIT_REASON_MCE_DURING_VMENTRY] = handle_machine_check,
9175 [EXIT_REASON_GDTR_IDTR] = handle_desc,
9176 [EXIT_REASON_LDTR_TR] = handle_desc,
9177 [EXIT_REASON_EPT_VIOLATION] = handle_ept_violation,
9178 [EXIT_REASON_EPT_MISCONFIG] = handle_ept_misconfig,
9179 [EXIT_REASON_PAUSE_INSTRUCTION] = handle_pause,
9180 [EXIT_REASON_MWAIT_INSTRUCTION] = handle_mwait,
9181 [EXIT_REASON_MONITOR_TRAP_FLAG] = handle_monitor_trap,
9182 [EXIT_REASON_MONITOR_INSTRUCTION] = handle_monitor,
9183 [EXIT_REASON_INVEPT] = handle_invept,
9184 [EXIT_REASON_INVVPID] = handle_invvpid,
9185 [EXIT_REASON_RDRAND] = handle_invalid_op,
9186 [EXIT_REASON_RDSEED] = handle_invalid_op,
9187 [EXIT_REASON_XSAVES] = handle_xsaves,
9188 [EXIT_REASON_XRSTORS] = handle_xrstors,
9189 [EXIT_REASON_PML_FULL] = handle_pml_full,
9190 [EXIT_REASON_INVPCID] = handle_invpcid,
9191 [EXIT_REASON_VMFUNC] = handle_vmfunc,
9192 [EXIT_REASON_PREEMPTION_TIMER] = handle_preemption_timer,
9195 static const int kvm_vmx_max_exit_handlers =
9196 ARRAY_SIZE(kvm_vmx_exit_handlers);
9198 static bool nested_vmx_exit_handled_io(struct kvm_vcpu *vcpu,
9199 struct vmcs12 *vmcs12)
9201 unsigned long exit_qualification;
9202 gpa_t bitmap, last_bitmap;
9207 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_IO_BITMAPS))
9208 return nested_cpu_has(vmcs12, CPU_BASED_UNCOND_IO_EXITING);
9210 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
9212 port = exit_qualification >> 16;
9213 size = (exit_qualification & 7) + 1;
9215 last_bitmap = (gpa_t)-1;
9220 bitmap = vmcs12->io_bitmap_a;
9221 else if (port < 0x10000)
9222 bitmap = vmcs12->io_bitmap_b;
9225 bitmap += (port & 0x7fff) / 8;
9227 if (last_bitmap != bitmap)
9228 if (kvm_vcpu_read_guest(vcpu, bitmap, &b, 1))
9230 if (b & (1 << (port & 7)))
9235 last_bitmap = bitmap;
9242 * Return 1 if we should exit from L2 to L1 to handle an MSR access access,
9243 * rather than handle it ourselves in L0. I.e., check whether L1 expressed
9244 * disinterest in the current event (read or write a specific MSR) by using an
9245 * MSR bitmap. This may be the case even when L0 doesn't use MSR bitmaps.
9247 static bool nested_vmx_exit_handled_msr(struct kvm_vcpu *vcpu,
9248 struct vmcs12 *vmcs12, u32 exit_reason)
9250 u32 msr_index = vcpu->arch.regs[VCPU_REGS_RCX];
9253 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS))
9257 * The MSR_BITMAP page is divided into four 1024-byte bitmaps,
9258 * for the four combinations of read/write and low/high MSR numbers.
9259 * First we need to figure out which of the four to use:
9261 bitmap = vmcs12->msr_bitmap;
9262 if (exit_reason == EXIT_REASON_MSR_WRITE)
9264 if (msr_index >= 0xc0000000) {
9265 msr_index -= 0xc0000000;
9269 /* Then read the msr_index'th bit from this bitmap: */
9270 if (msr_index < 1024*8) {
9272 if (kvm_vcpu_read_guest(vcpu, bitmap + msr_index/8, &b, 1))
9274 return 1 & (b >> (msr_index & 7));
9276 return true; /* let L1 handle the wrong parameter */
9280 * Return 1 if we should exit from L2 to L1 to handle a CR access exit,
9281 * rather than handle it ourselves in L0. I.e., check if L1 wanted to
9282 * intercept (via guest_host_mask etc.) the current event.
9284 static bool nested_vmx_exit_handled_cr(struct kvm_vcpu *vcpu,
9285 struct vmcs12 *vmcs12)
9287 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
9288 int cr = exit_qualification & 15;
9292 switch ((exit_qualification >> 4) & 3) {
9293 case 0: /* mov to cr */
9294 reg = (exit_qualification >> 8) & 15;
9295 val = kvm_register_readl(vcpu, reg);
9298 if (vmcs12->cr0_guest_host_mask &
9299 (val ^ vmcs12->cr0_read_shadow))
9303 if ((vmcs12->cr3_target_count >= 1 &&
9304 vmcs12->cr3_target_value0 == val) ||
9305 (vmcs12->cr3_target_count >= 2 &&
9306 vmcs12->cr3_target_value1 == val) ||
9307 (vmcs12->cr3_target_count >= 3 &&
9308 vmcs12->cr3_target_value2 == val) ||
9309 (vmcs12->cr3_target_count >= 4 &&
9310 vmcs12->cr3_target_value3 == val))
9312 if (nested_cpu_has(vmcs12, CPU_BASED_CR3_LOAD_EXITING))
9316 if (vmcs12->cr4_guest_host_mask &
9317 (vmcs12->cr4_read_shadow ^ val))
9321 if (nested_cpu_has(vmcs12, CPU_BASED_CR8_LOAD_EXITING))
9327 if ((vmcs12->cr0_guest_host_mask & X86_CR0_TS) &&
9328 (vmcs12->cr0_read_shadow & X86_CR0_TS))
9331 case 1: /* mov from cr */
9334 if (vmcs12->cpu_based_vm_exec_control &
9335 CPU_BASED_CR3_STORE_EXITING)
9339 if (vmcs12->cpu_based_vm_exec_control &
9340 CPU_BASED_CR8_STORE_EXITING)
9347 * lmsw can change bits 1..3 of cr0, and only set bit 0 of
9348 * cr0. Other attempted changes are ignored, with no exit.
9350 val = (exit_qualification >> LMSW_SOURCE_DATA_SHIFT) & 0x0f;
9351 if (vmcs12->cr0_guest_host_mask & 0xe &
9352 (val ^ vmcs12->cr0_read_shadow))
9354 if ((vmcs12->cr0_guest_host_mask & 0x1) &&
9355 !(vmcs12->cr0_read_shadow & 0x1) &&
9363 static bool nested_vmx_exit_handled_vmcs_access(struct kvm_vcpu *vcpu,
9364 struct vmcs12 *vmcs12, gpa_t bitmap)
9366 u32 vmx_instruction_info;
9367 unsigned long field;
9370 if (!nested_cpu_has_shadow_vmcs(vmcs12))
9373 /* Decode instruction info and find the field to access */
9374 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
9375 field = kvm_register_read(vcpu, (((vmx_instruction_info) >> 28) & 0xf));
9377 /* Out-of-range fields always cause a VM exit from L2 to L1 */
9381 if (kvm_vcpu_read_guest(vcpu, bitmap + field/8, &b, 1))
9384 return 1 & (b >> (field & 7));
9388 * Return 1 if we should exit from L2 to L1 to handle an exit, or 0 if we
9389 * should handle it ourselves in L0 (and then continue L2). Only call this
9390 * when in is_guest_mode (L2).
9392 static bool nested_vmx_exit_reflected(struct kvm_vcpu *vcpu, u32 exit_reason)
9394 u32 intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
9395 struct vcpu_vmx *vmx = to_vmx(vcpu);
9396 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
9398 if (vmx->nested.nested_run_pending)
9401 if (unlikely(vmx->fail)) {
9402 pr_info_ratelimited("%s failed vm entry %x\n", __func__,
9403 vmcs_read32(VM_INSTRUCTION_ERROR));
9408 * The host physical addresses of some pages of guest memory
9409 * are loaded into the vmcs02 (e.g. vmcs12's Virtual APIC
9410 * Page). The CPU may write to these pages via their host
9411 * physical address while L2 is running, bypassing any
9412 * address-translation-based dirty tracking (e.g. EPT write
9415 * Mark them dirty on every exit from L2 to prevent them from
9416 * getting out of sync with dirty tracking.
9418 nested_mark_vmcs12_pages_dirty(vcpu);
9420 trace_kvm_nested_vmexit(kvm_rip_read(vcpu), exit_reason,
9421 vmcs_readl(EXIT_QUALIFICATION),
9422 vmx->idt_vectoring_info,
9424 vmcs_read32(VM_EXIT_INTR_ERROR_CODE),
9427 switch (exit_reason) {
9428 case EXIT_REASON_EXCEPTION_NMI:
9429 if (is_nmi(intr_info))
9431 else if (is_page_fault(intr_info))
9432 return !vmx->vcpu.arch.apf.host_apf_reason && enable_ept;
9433 else if (is_no_device(intr_info) &&
9434 !(vmcs12->guest_cr0 & X86_CR0_TS))
9436 else if (is_debug(intr_info) &&
9438 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))
9440 else if (is_breakpoint(intr_info) &&
9441 vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
9443 return vmcs12->exception_bitmap &
9444 (1u << (intr_info & INTR_INFO_VECTOR_MASK));
9445 case EXIT_REASON_EXTERNAL_INTERRUPT:
9447 case EXIT_REASON_TRIPLE_FAULT:
9449 case EXIT_REASON_PENDING_INTERRUPT:
9450 return nested_cpu_has(vmcs12, CPU_BASED_VIRTUAL_INTR_PENDING);
9451 case EXIT_REASON_NMI_WINDOW:
9452 return nested_cpu_has(vmcs12, CPU_BASED_VIRTUAL_NMI_PENDING);
9453 case EXIT_REASON_TASK_SWITCH:
9455 case EXIT_REASON_CPUID:
9457 case EXIT_REASON_HLT:
9458 return nested_cpu_has(vmcs12, CPU_BASED_HLT_EXITING);
9459 case EXIT_REASON_INVD:
9461 case EXIT_REASON_INVLPG:
9462 return nested_cpu_has(vmcs12, CPU_BASED_INVLPG_EXITING);
9463 case EXIT_REASON_RDPMC:
9464 return nested_cpu_has(vmcs12, CPU_BASED_RDPMC_EXITING);
9465 case EXIT_REASON_RDRAND:
9466 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_RDRAND_EXITING);
9467 case EXIT_REASON_RDSEED:
9468 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_RDSEED_EXITING);
9469 case EXIT_REASON_RDTSC: case EXIT_REASON_RDTSCP:
9470 return nested_cpu_has(vmcs12, CPU_BASED_RDTSC_EXITING);
9471 case EXIT_REASON_VMREAD:
9472 return nested_vmx_exit_handled_vmcs_access(vcpu, vmcs12,
9473 vmcs12->vmread_bitmap);
9474 case EXIT_REASON_VMWRITE:
9475 return nested_vmx_exit_handled_vmcs_access(vcpu, vmcs12,
9476 vmcs12->vmwrite_bitmap);
9477 case EXIT_REASON_VMCALL: case EXIT_REASON_VMCLEAR:
9478 case EXIT_REASON_VMLAUNCH: case EXIT_REASON_VMPTRLD:
9479 case EXIT_REASON_VMPTRST: case EXIT_REASON_VMRESUME:
9480 case EXIT_REASON_VMOFF: case EXIT_REASON_VMON:
9481 case EXIT_REASON_INVEPT: case EXIT_REASON_INVVPID:
9483 * VMX instructions trap unconditionally. This allows L1 to
9484 * emulate them for its L2 guest, i.e., allows 3-level nesting!
9487 case EXIT_REASON_CR_ACCESS:
9488 return nested_vmx_exit_handled_cr(vcpu, vmcs12);
9489 case EXIT_REASON_DR_ACCESS:
9490 return nested_cpu_has(vmcs12, CPU_BASED_MOV_DR_EXITING);
9491 case EXIT_REASON_IO_INSTRUCTION:
9492 return nested_vmx_exit_handled_io(vcpu, vmcs12);
9493 case EXIT_REASON_GDTR_IDTR: case EXIT_REASON_LDTR_TR:
9494 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_DESC);
9495 case EXIT_REASON_MSR_READ:
9496 case EXIT_REASON_MSR_WRITE:
9497 return nested_vmx_exit_handled_msr(vcpu, vmcs12, exit_reason);
9498 case EXIT_REASON_INVALID_STATE:
9500 case EXIT_REASON_MWAIT_INSTRUCTION:
9501 return nested_cpu_has(vmcs12, CPU_BASED_MWAIT_EXITING);
9502 case EXIT_REASON_MONITOR_TRAP_FLAG:
9503 return nested_cpu_has(vmcs12, CPU_BASED_MONITOR_TRAP_FLAG);
9504 case EXIT_REASON_MONITOR_INSTRUCTION:
9505 return nested_cpu_has(vmcs12, CPU_BASED_MONITOR_EXITING);
9506 case EXIT_REASON_PAUSE_INSTRUCTION:
9507 return nested_cpu_has(vmcs12, CPU_BASED_PAUSE_EXITING) ||
9508 nested_cpu_has2(vmcs12,
9509 SECONDARY_EXEC_PAUSE_LOOP_EXITING);
9510 case EXIT_REASON_MCE_DURING_VMENTRY:
9512 case EXIT_REASON_TPR_BELOW_THRESHOLD:
9513 return nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW);
9514 case EXIT_REASON_APIC_ACCESS:
9515 case EXIT_REASON_APIC_WRITE:
9516 case EXIT_REASON_EOI_INDUCED:
9518 * The controls for "virtualize APIC accesses," "APIC-
9519 * register virtualization," and "virtual-interrupt
9520 * delivery" only come from vmcs12.
9523 case EXIT_REASON_EPT_VIOLATION:
9525 * L0 always deals with the EPT violation. If nested EPT is
9526 * used, and the nested mmu code discovers that the address is
9527 * missing in the guest EPT table (EPT12), the EPT violation
9528 * will be injected with nested_ept_inject_page_fault()
9531 case EXIT_REASON_EPT_MISCONFIG:
9533 * L2 never uses directly L1's EPT, but rather L0's own EPT
9534 * table (shadow on EPT) or a merged EPT table that L0 built
9535 * (EPT on EPT). So any problems with the structure of the
9536 * table is L0's fault.
9539 case EXIT_REASON_INVPCID:
9541 nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_INVPCID) &&
9542 nested_cpu_has(vmcs12, CPU_BASED_INVLPG_EXITING);
9543 case EXIT_REASON_WBINVD:
9544 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_WBINVD_EXITING);
9545 case EXIT_REASON_XSETBV:
9547 case EXIT_REASON_XSAVES: case EXIT_REASON_XRSTORS:
9549 * This should never happen, since it is not possible to
9550 * set XSS to a non-zero value---neither in L1 nor in L2.
9551 * If if it were, XSS would have to be checked against
9552 * the XSS exit bitmap in vmcs12.
9554 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_XSAVES);
9555 case EXIT_REASON_PREEMPTION_TIMER:
9557 case EXIT_REASON_PML_FULL:
9558 /* We emulate PML support to L1. */
9560 case EXIT_REASON_VMFUNC:
9561 /* VM functions are emulated through L2->L0 vmexits. */
9568 static int nested_vmx_reflect_vmexit(struct kvm_vcpu *vcpu, u32 exit_reason)
9570 u32 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
9573 * At this point, the exit interruption info in exit_intr_info
9574 * is only valid for EXCEPTION_NMI exits. For EXTERNAL_INTERRUPT
9575 * we need to query the in-kernel LAPIC.
9577 WARN_ON(exit_reason == EXIT_REASON_EXTERNAL_INTERRUPT);
9578 if ((exit_intr_info &
9579 (INTR_INFO_VALID_MASK | INTR_INFO_DELIVER_CODE_MASK)) ==
9580 (INTR_INFO_VALID_MASK | INTR_INFO_DELIVER_CODE_MASK)) {
9581 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
9582 vmcs12->vm_exit_intr_error_code =
9583 vmcs_read32(VM_EXIT_INTR_ERROR_CODE);
9586 nested_vmx_vmexit(vcpu, exit_reason, exit_intr_info,
9587 vmcs_readl(EXIT_QUALIFICATION));
9591 static void vmx_get_exit_info(struct kvm_vcpu *vcpu, u64 *info1, u64 *info2)
9593 *info1 = vmcs_readl(EXIT_QUALIFICATION);
9594 *info2 = vmcs_read32(VM_EXIT_INTR_INFO);
9597 static void vmx_destroy_pml_buffer(struct vcpu_vmx *vmx)
9600 __free_page(vmx->pml_pg);
9605 static void vmx_flush_pml_buffer(struct kvm_vcpu *vcpu)
9607 struct vcpu_vmx *vmx = to_vmx(vcpu);
9611 pml_idx = vmcs_read16(GUEST_PML_INDEX);
9613 /* Do nothing if PML buffer is empty */
9614 if (pml_idx == (PML_ENTITY_NUM - 1))
9617 /* PML index always points to next available PML buffer entity */
9618 if (pml_idx >= PML_ENTITY_NUM)
9623 pml_buf = page_address(vmx->pml_pg);
9624 for (; pml_idx < PML_ENTITY_NUM; pml_idx++) {
9627 gpa = pml_buf[pml_idx];
9628 WARN_ON(gpa & (PAGE_SIZE - 1));
9629 kvm_vcpu_mark_page_dirty(vcpu, gpa >> PAGE_SHIFT);
9632 /* reset PML index */
9633 vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1);
9637 * Flush all vcpus' PML buffer and update logged GPAs to dirty_bitmap.
9638 * Called before reporting dirty_bitmap to userspace.
9640 static void kvm_flush_pml_buffers(struct kvm *kvm)
9643 struct kvm_vcpu *vcpu;
9645 * We only need to kick vcpu out of guest mode here, as PML buffer
9646 * is flushed at beginning of all VMEXITs, and it's obvious that only
9647 * vcpus running in guest are possible to have unflushed GPAs in PML
9650 kvm_for_each_vcpu(i, vcpu, kvm)
9651 kvm_vcpu_kick(vcpu);
9654 static void vmx_dump_sel(char *name, uint32_t sel)
9656 pr_err("%s sel=0x%04x, attr=0x%05x, limit=0x%08x, base=0x%016lx\n",
9657 name, vmcs_read16(sel),
9658 vmcs_read32(sel + GUEST_ES_AR_BYTES - GUEST_ES_SELECTOR),
9659 vmcs_read32(sel + GUEST_ES_LIMIT - GUEST_ES_SELECTOR),
9660 vmcs_readl(sel + GUEST_ES_BASE - GUEST_ES_SELECTOR));
9663 static void vmx_dump_dtsel(char *name, uint32_t limit)
9665 pr_err("%s limit=0x%08x, base=0x%016lx\n",
9666 name, vmcs_read32(limit),
9667 vmcs_readl(limit + GUEST_GDTR_BASE - GUEST_GDTR_LIMIT));
9670 static void dump_vmcs(void)
9672 u32 vmentry_ctl = vmcs_read32(VM_ENTRY_CONTROLS);
9673 u32 vmexit_ctl = vmcs_read32(VM_EXIT_CONTROLS);
9674 u32 cpu_based_exec_ctrl = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
9675 u32 pin_based_exec_ctrl = vmcs_read32(PIN_BASED_VM_EXEC_CONTROL);
9676 u32 secondary_exec_control = 0;
9677 unsigned long cr4 = vmcs_readl(GUEST_CR4);
9678 u64 efer = vmcs_read64(GUEST_IA32_EFER);
9681 if (cpu_has_secondary_exec_ctrls())
9682 secondary_exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
9684 pr_err("*** Guest State ***\n");
9685 pr_err("CR0: actual=0x%016lx, shadow=0x%016lx, gh_mask=%016lx\n",
9686 vmcs_readl(GUEST_CR0), vmcs_readl(CR0_READ_SHADOW),
9687 vmcs_readl(CR0_GUEST_HOST_MASK));
9688 pr_err("CR4: actual=0x%016lx, shadow=0x%016lx, gh_mask=%016lx\n",
9689 cr4, vmcs_readl(CR4_READ_SHADOW), vmcs_readl(CR4_GUEST_HOST_MASK));
9690 pr_err("CR3 = 0x%016lx\n", vmcs_readl(GUEST_CR3));
9691 if ((secondary_exec_control & SECONDARY_EXEC_ENABLE_EPT) &&
9692 (cr4 & X86_CR4_PAE) && !(efer & EFER_LMA))
9694 pr_err("PDPTR0 = 0x%016llx PDPTR1 = 0x%016llx\n",
9695 vmcs_read64(GUEST_PDPTR0), vmcs_read64(GUEST_PDPTR1));
9696 pr_err("PDPTR2 = 0x%016llx PDPTR3 = 0x%016llx\n",
9697 vmcs_read64(GUEST_PDPTR2), vmcs_read64(GUEST_PDPTR3));
9699 pr_err("RSP = 0x%016lx RIP = 0x%016lx\n",
9700 vmcs_readl(GUEST_RSP), vmcs_readl(GUEST_RIP));
9701 pr_err("RFLAGS=0x%08lx DR7 = 0x%016lx\n",
9702 vmcs_readl(GUEST_RFLAGS), vmcs_readl(GUEST_DR7));
9703 pr_err("Sysenter RSP=%016lx CS:RIP=%04x:%016lx\n",
9704 vmcs_readl(GUEST_SYSENTER_ESP),
9705 vmcs_read32(GUEST_SYSENTER_CS), vmcs_readl(GUEST_SYSENTER_EIP));
9706 vmx_dump_sel("CS: ", GUEST_CS_SELECTOR);
9707 vmx_dump_sel("DS: ", GUEST_DS_SELECTOR);
9708 vmx_dump_sel("SS: ", GUEST_SS_SELECTOR);
9709 vmx_dump_sel("ES: ", GUEST_ES_SELECTOR);
9710 vmx_dump_sel("FS: ", GUEST_FS_SELECTOR);
9711 vmx_dump_sel("GS: ", GUEST_GS_SELECTOR);
9712 vmx_dump_dtsel("GDTR:", GUEST_GDTR_LIMIT);
9713 vmx_dump_sel("LDTR:", GUEST_LDTR_SELECTOR);
9714 vmx_dump_dtsel("IDTR:", GUEST_IDTR_LIMIT);
9715 vmx_dump_sel("TR: ", GUEST_TR_SELECTOR);
9716 if ((vmexit_ctl & (VM_EXIT_SAVE_IA32_PAT | VM_EXIT_SAVE_IA32_EFER)) ||
9717 (vmentry_ctl & (VM_ENTRY_LOAD_IA32_PAT | VM_ENTRY_LOAD_IA32_EFER)))
9718 pr_err("EFER = 0x%016llx PAT = 0x%016llx\n",
9719 efer, vmcs_read64(GUEST_IA32_PAT));
9720 pr_err("DebugCtl = 0x%016llx DebugExceptions = 0x%016lx\n",
9721 vmcs_read64(GUEST_IA32_DEBUGCTL),
9722 vmcs_readl(GUEST_PENDING_DBG_EXCEPTIONS));
9723 if (cpu_has_load_perf_global_ctrl &&
9724 vmentry_ctl & VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL)
9725 pr_err("PerfGlobCtl = 0x%016llx\n",
9726 vmcs_read64(GUEST_IA32_PERF_GLOBAL_CTRL));
9727 if (vmentry_ctl & VM_ENTRY_LOAD_BNDCFGS)
9728 pr_err("BndCfgS = 0x%016llx\n", vmcs_read64(GUEST_BNDCFGS));
9729 pr_err("Interruptibility = %08x ActivityState = %08x\n",
9730 vmcs_read32(GUEST_INTERRUPTIBILITY_INFO),
9731 vmcs_read32(GUEST_ACTIVITY_STATE));
9732 if (secondary_exec_control & SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY)
9733 pr_err("InterruptStatus = %04x\n",
9734 vmcs_read16(GUEST_INTR_STATUS));
9736 pr_err("*** Host State ***\n");
9737 pr_err("RIP = 0x%016lx RSP = 0x%016lx\n",
9738 vmcs_readl(HOST_RIP), vmcs_readl(HOST_RSP));
9739 pr_err("CS=%04x SS=%04x DS=%04x ES=%04x FS=%04x GS=%04x TR=%04x\n",
9740 vmcs_read16(HOST_CS_SELECTOR), vmcs_read16(HOST_SS_SELECTOR),
9741 vmcs_read16(HOST_DS_SELECTOR), vmcs_read16(HOST_ES_SELECTOR),
9742 vmcs_read16(HOST_FS_SELECTOR), vmcs_read16(HOST_GS_SELECTOR),
9743 vmcs_read16(HOST_TR_SELECTOR));
9744 pr_err("FSBase=%016lx GSBase=%016lx TRBase=%016lx\n",
9745 vmcs_readl(HOST_FS_BASE), vmcs_readl(HOST_GS_BASE),
9746 vmcs_readl(HOST_TR_BASE));
9747 pr_err("GDTBase=%016lx IDTBase=%016lx\n",
9748 vmcs_readl(HOST_GDTR_BASE), vmcs_readl(HOST_IDTR_BASE));
9749 pr_err("CR0=%016lx CR3=%016lx CR4=%016lx\n",
9750 vmcs_readl(HOST_CR0), vmcs_readl(HOST_CR3),
9751 vmcs_readl(HOST_CR4));
9752 pr_err("Sysenter RSP=%016lx CS:RIP=%04x:%016lx\n",
9753 vmcs_readl(HOST_IA32_SYSENTER_ESP),
9754 vmcs_read32(HOST_IA32_SYSENTER_CS),
9755 vmcs_readl(HOST_IA32_SYSENTER_EIP));
9756 if (vmexit_ctl & (VM_EXIT_LOAD_IA32_PAT | VM_EXIT_LOAD_IA32_EFER))
9757 pr_err("EFER = 0x%016llx PAT = 0x%016llx\n",
9758 vmcs_read64(HOST_IA32_EFER),
9759 vmcs_read64(HOST_IA32_PAT));
9760 if (cpu_has_load_perf_global_ctrl &&
9761 vmexit_ctl & VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL)
9762 pr_err("PerfGlobCtl = 0x%016llx\n",
9763 vmcs_read64(HOST_IA32_PERF_GLOBAL_CTRL));
9765 pr_err("*** Control State ***\n");
9766 pr_err("PinBased=%08x CPUBased=%08x SecondaryExec=%08x\n",
9767 pin_based_exec_ctrl, cpu_based_exec_ctrl, secondary_exec_control);
9768 pr_err("EntryControls=%08x ExitControls=%08x\n", vmentry_ctl, vmexit_ctl);
9769 pr_err("ExceptionBitmap=%08x PFECmask=%08x PFECmatch=%08x\n",
9770 vmcs_read32(EXCEPTION_BITMAP),
9771 vmcs_read32(PAGE_FAULT_ERROR_CODE_MASK),
9772 vmcs_read32(PAGE_FAULT_ERROR_CODE_MATCH));
9773 pr_err("VMEntry: intr_info=%08x errcode=%08x ilen=%08x\n",
9774 vmcs_read32(VM_ENTRY_INTR_INFO_FIELD),
9775 vmcs_read32(VM_ENTRY_EXCEPTION_ERROR_CODE),
9776 vmcs_read32(VM_ENTRY_INSTRUCTION_LEN));
9777 pr_err("VMExit: intr_info=%08x errcode=%08x ilen=%08x\n",
9778 vmcs_read32(VM_EXIT_INTR_INFO),
9779 vmcs_read32(VM_EXIT_INTR_ERROR_CODE),
9780 vmcs_read32(VM_EXIT_INSTRUCTION_LEN));
9781 pr_err(" reason=%08x qualification=%016lx\n",
9782 vmcs_read32(VM_EXIT_REASON), vmcs_readl(EXIT_QUALIFICATION));
9783 pr_err("IDTVectoring: info=%08x errcode=%08x\n",
9784 vmcs_read32(IDT_VECTORING_INFO_FIELD),
9785 vmcs_read32(IDT_VECTORING_ERROR_CODE));
9786 pr_err("TSC Offset = 0x%016llx\n", vmcs_read64(TSC_OFFSET));
9787 if (secondary_exec_control & SECONDARY_EXEC_TSC_SCALING)
9788 pr_err("TSC Multiplier = 0x%016llx\n",
9789 vmcs_read64(TSC_MULTIPLIER));
9790 if (cpu_based_exec_ctrl & CPU_BASED_TPR_SHADOW)
9791 pr_err("TPR Threshold = 0x%02x\n", vmcs_read32(TPR_THRESHOLD));
9792 if (pin_based_exec_ctrl & PIN_BASED_POSTED_INTR)
9793 pr_err("PostedIntrVec = 0x%02x\n", vmcs_read16(POSTED_INTR_NV));
9794 if ((secondary_exec_control & SECONDARY_EXEC_ENABLE_EPT))
9795 pr_err("EPT pointer = 0x%016llx\n", vmcs_read64(EPT_POINTER));
9796 n = vmcs_read32(CR3_TARGET_COUNT);
9797 for (i = 0; i + 1 < n; i += 4)
9798 pr_err("CR3 target%u=%016lx target%u=%016lx\n",
9799 i, vmcs_readl(CR3_TARGET_VALUE0 + i * 2),
9800 i + 1, vmcs_readl(CR3_TARGET_VALUE0 + i * 2 + 2));
9802 pr_err("CR3 target%u=%016lx\n",
9803 i, vmcs_readl(CR3_TARGET_VALUE0 + i * 2));
9804 if (secondary_exec_control & SECONDARY_EXEC_PAUSE_LOOP_EXITING)
9805 pr_err("PLE Gap=%08x Window=%08x\n",
9806 vmcs_read32(PLE_GAP), vmcs_read32(PLE_WINDOW));
9807 if (secondary_exec_control & SECONDARY_EXEC_ENABLE_VPID)
9808 pr_err("Virtual processor ID = 0x%04x\n",
9809 vmcs_read16(VIRTUAL_PROCESSOR_ID));
9813 * The guest has exited. See if we can fix it or if we need userspace
9816 static int vmx_handle_exit(struct kvm_vcpu *vcpu)
9818 struct vcpu_vmx *vmx = to_vmx(vcpu);
9819 u32 exit_reason = vmx->exit_reason;
9820 u32 vectoring_info = vmx->idt_vectoring_info;
9822 trace_kvm_exit(exit_reason, vcpu, KVM_ISA_VMX);
9825 * Flush logged GPAs PML buffer, this will make dirty_bitmap more
9826 * updated. Another good is, in kvm_vm_ioctl_get_dirty_log, before
9827 * querying dirty_bitmap, we only need to kick all vcpus out of guest
9828 * mode as if vcpus is in root mode, the PML buffer must has been
9832 vmx_flush_pml_buffer(vcpu);
9834 /* If guest state is invalid, start emulating */
9835 if (vmx->emulation_required)
9836 return handle_invalid_guest_state(vcpu);
9838 if (is_guest_mode(vcpu) && nested_vmx_exit_reflected(vcpu, exit_reason))
9839 return nested_vmx_reflect_vmexit(vcpu, exit_reason);
9841 if (exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY) {
9843 vcpu->run->exit_reason = KVM_EXIT_FAIL_ENTRY;
9844 vcpu->run->fail_entry.hardware_entry_failure_reason
9849 if (unlikely(vmx->fail)) {
9850 vcpu->run->exit_reason = KVM_EXIT_FAIL_ENTRY;
9851 vcpu->run->fail_entry.hardware_entry_failure_reason
9852 = vmcs_read32(VM_INSTRUCTION_ERROR);
9858 * Do not try to fix EXIT_REASON_EPT_MISCONFIG if it caused by
9859 * delivery event since it indicates guest is accessing MMIO.
9860 * The vm-exit can be triggered again after return to guest that
9861 * will cause infinite loop.
9863 if ((vectoring_info & VECTORING_INFO_VALID_MASK) &&
9864 (exit_reason != EXIT_REASON_EXCEPTION_NMI &&
9865 exit_reason != EXIT_REASON_EPT_VIOLATION &&
9866 exit_reason != EXIT_REASON_PML_FULL &&
9867 exit_reason != EXIT_REASON_TASK_SWITCH)) {
9868 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
9869 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_DELIVERY_EV;
9870 vcpu->run->internal.ndata = 3;
9871 vcpu->run->internal.data[0] = vectoring_info;
9872 vcpu->run->internal.data[1] = exit_reason;
9873 vcpu->run->internal.data[2] = vcpu->arch.exit_qualification;
9874 if (exit_reason == EXIT_REASON_EPT_MISCONFIG) {
9875 vcpu->run->internal.ndata++;
9876 vcpu->run->internal.data[3] =
9877 vmcs_read64(GUEST_PHYSICAL_ADDRESS);
9882 if (unlikely(!enable_vnmi &&
9883 vmx->loaded_vmcs->soft_vnmi_blocked)) {
9884 if (vmx_interrupt_allowed(vcpu)) {
9885 vmx->loaded_vmcs->soft_vnmi_blocked = 0;
9886 } else if (vmx->loaded_vmcs->vnmi_blocked_time > 1000000000LL &&
9887 vcpu->arch.nmi_pending) {
9889 * This CPU don't support us in finding the end of an
9890 * NMI-blocked window if the guest runs with IRQs
9891 * disabled. So we pull the trigger after 1 s of
9892 * futile waiting, but inform the user about this.
9894 printk(KERN_WARNING "%s: Breaking out of NMI-blocked "
9895 "state on VCPU %d after 1 s timeout\n",
9896 __func__, vcpu->vcpu_id);
9897 vmx->loaded_vmcs->soft_vnmi_blocked = 0;
9901 if (exit_reason < kvm_vmx_max_exit_handlers
9902 && kvm_vmx_exit_handlers[exit_reason])
9903 return kvm_vmx_exit_handlers[exit_reason](vcpu);
9905 vcpu_unimpl(vcpu, "vmx: unexpected exit reason 0x%x\n",
9907 kvm_queue_exception(vcpu, UD_VECTOR);
9912 static void update_cr8_intercept(struct kvm_vcpu *vcpu, int tpr, int irr)
9914 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
9916 if (is_guest_mode(vcpu) &&
9917 nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW))
9920 if (irr == -1 || tpr < irr) {
9921 vmcs_write32(TPR_THRESHOLD, 0);
9925 vmcs_write32(TPR_THRESHOLD, irr);
9928 static void vmx_set_virtual_apic_mode(struct kvm_vcpu *vcpu)
9930 u32 sec_exec_control;
9932 if (!lapic_in_kernel(vcpu))
9935 /* Postpone execution until vmcs01 is the current VMCS. */
9936 if (is_guest_mode(vcpu)) {
9937 to_vmx(vcpu)->nested.change_vmcs01_virtual_apic_mode = true;
9941 if (!cpu_need_tpr_shadow(vcpu))
9944 sec_exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
9945 sec_exec_control &= ~(SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
9946 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE);
9948 switch (kvm_get_apic_mode(vcpu)) {
9949 case LAPIC_MODE_INVALID:
9950 WARN_ONCE(true, "Invalid local APIC state");
9951 case LAPIC_MODE_DISABLED:
9953 case LAPIC_MODE_XAPIC:
9954 if (flexpriority_enabled) {
9956 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
9957 vmx_flush_tlb(vcpu, true);
9960 case LAPIC_MODE_X2APIC:
9961 if (cpu_has_vmx_virtualize_x2apic_mode())
9963 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
9966 vmcs_write32(SECONDARY_VM_EXEC_CONTROL, sec_exec_control);
9968 vmx_update_msr_bitmap(vcpu);
9971 static void vmx_set_apic_access_page_addr(struct kvm_vcpu *vcpu, hpa_t hpa)
9973 if (!is_guest_mode(vcpu)) {
9974 vmcs_write64(APIC_ACCESS_ADDR, hpa);
9975 vmx_flush_tlb(vcpu, true);
9979 static void vmx_hwapic_isr_update(struct kvm_vcpu *vcpu, int max_isr)
9987 status = vmcs_read16(GUEST_INTR_STATUS);
9989 if (max_isr != old) {
9991 status |= max_isr << 8;
9992 vmcs_write16(GUEST_INTR_STATUS, status);
9996 static void vmx_set_rvi(int vector)
10004 status = vmcs_read16(GUEST_INTR_STATUS);
10005 old = (u8)status & 0xff;
10006 if ((u8)vector != old) {
10008 status |= (u8)vector;
10009 vmcs_write16(GUEST_INTR_STATUS, status);
10013 static void vmx_hwapic_irr_update(struct kvm_vcpu *vcpu, int max_irr)
10016 * When running L2, updating RVI is only relevant when
10017 * vmcs12 virtual-interrupt-delivery enabled.
10018 * However, it can be enabled only when L1 also
10019 * intercepts external-interrupts and in that case
10020 * we should not update vmcs02 RVI but instead intercept
10021 * interrupt. Therefore, do nothing when running L2.
10023 if (!is_guest_mode(vcpu))
10024 vmx_set_rvi(max_irr);
10027 static int vmx_sync_pir_to_irr(struct kvm_vcpu *vcpu)
10029 struct vcpu_vmx *vmx = to_vmx(vcpu);
10031 bool max_irr_updated;
10033 WARN_ON(!vcpu->arch.apicv_active);
10034 if (pi_test_on(&vmx->pi_desc)) {
10035 pi_clear_on(&vmx->pi_desc);
10037 * IOMMU can write to PIR.ON, so the barrier matters even on UP.
10038 * But on x86 this is just a compiler barrier anyway.
10040 smp_mb__after_atomic();
10042 kvm_apic_update_irr(vcpu, vmx->pi_desc.pir, &max_irr);
10045 * If we are running L2 and L1 has a new pending interrupt
10046 * which can be injected, we should re-evaluate
10047 * what should be done with this new L1 interrupt.
10048 * If L1 intercepts external-interrupts, we should
10049 * exit from L2 to L1. Otherwise, interrupt should be
10050 * delivered directly to L2.
10052 if (is_guest_mode(vcpu) && max_irr_updated) {
10053 if (nested_exit_on_intr(vcpu))
10054 kvm_vcpu_exiting_guest_mode(vcpu);
10056 kvm_make_request(KVM_REQ_EVENT, vcpu);
10059 max_irr = kvm_lapic_find_highest_irr(vcpu);
10061 vmx_hwapic_irr_update(vcpu, max_irr);
10065 static void vmx_load_eoi_exitmap(struct kvm_vcpu *vcpu, u64 *eoi_exit_bitmap)
10067 if (!kvm_vcpu_apicv_active(vcpu))
10070 vmcs_write64(EOI_EXIT_BITMAP0, eoi_exit_bitmap[0]);
10071 vmcs_write64(EOI_EXIT_BITMAP1, eoi_exit_bitmap[1]);
10072 vmcs_write64(EOI_EXIT_BITMAP2, eoi_exit_bitmap[2]);
10073 vmcs_write64(EOI_EXIT_BITMAP3, eoi_exit_bitmap[3]);
10076 static void vmx_apicv_post_state_restore(struct kvm_vcpu *vcpu)
10078 struct vcpu_vmx *vmx = to_vmx(vcpu);
10080 pi_clear_on(&vmx->pi_desc);
10081 memset(vmx->pi_desc.pir, 0, sizeof(vmx->pi_desc.pir));
10084 static void vmx_complete_atomic_exit(struct vcpu_vmx *vmx)
10086 u32 exit_intr_info = 0;
10087 u16 basic_exit_reason = (u16)vmx->exit_reason;
10089 if (!(basic_exit_reason == EXIT_REASON_MCE_DURING_VMENTRY
10090 || basic_exit_reason == EXIT_REASON_EXCEPTION_NMI))
10093 if (!(vmx->exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY))
10094 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
10095 vmx->exit_intr_info = exit_intr_info;
10097 /* if exit due to PF check for async PF */
10098 if (is_page_fault(exit_intr_info))
10099 vmx->vcpu.arch.apf.host_apf_reason = kvm_read_and_reset_pf_reason();
10101 /* Handle machine checks before interrupts are enabled */
10102 if (basic_exit_reason == EXIT_REASON_MCE_DURING_VMENTRY ||
10103 is_machine_check(exit_intr_info))
10104 kvm_machine_check();
10106 /* We need to handle NMIs before interrupts are enabled */
10107 if (is_nmi(exit_intr_info)) {
10108 kvm_before_interrupt(&vmx->vcpu);
10110 kvm_after_interrupt(&vmx->vcpu);
10114 static void vmx_handle_external_intr(struct kvm_vcpu *vcpu)
10116 u32 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
10118 if ((exit_intr_info & (INTR_INFO_VALID_MASK | INTR_INFO_INTR_TYPE_MASK))
10119 == (INTR_INFO_VALID_MASK | INTR_TYPE_EXT_INTR)) {
10120 unsigned int vector;
10121 unsigned long entry;
10123 struct vcpu_vmx *vmx = to_vmx(vcpu);
10124 #ifdef CONFIG_X86_64
10128 vector = exit_intr_info & INTR_INFO_VECTOR_MASK;
10129 desc = (gate_desc *)vmx->host_idt_base + vector;
10130 entry = gate_offset(desc);
10132 #ifdef CONFIG_X86_64
10133 "mov %%" _ASM_SP ", %[sp]\n\t"
10134 "and $0xfffffffffffffff0, %%" _ASM_SP "\n\t"
10139 __ASM_SIZE(push) " $%c[cs]\n\t"
10142 #ifdef CONFIG_X86_64
10145 ASM_CALL_CONSTRAINT
10147 THUNK_TARGET(entry),
10148 [ss]"i"(__KERNEL_DS),
10149 [cs]"i"(__KERNEL_CS)
10153 STACK_FRAME_NON_STANDARD(vmx_handle_external_intr);
10155 static bool vmx_has_emulated_msr(int index)
10158 case MSR_IA32_SMBASE:
10160 * We cannot do SMM unless we can run the guest in big
10163 return enable_unrestricted_guest || emulate_invalid_guest_state;
10164 case MSR_AMD64_VIRT_SPEC_CTRL:
10165 /* This is AMD only. */
10172 static bool vmx_mpx_supported(void)
10174 return (vmcs_config.vmexit_ctrl & VM_EXIT_CLEAR_BNDCFGS) &&
10175 (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_BNDCFGS);
10178 static bool vmx_xsaves_supported(void)
10180 return vmcs_config.cpu_based_2nd_exec_ctrl &
10181 SECONDARY_EXEC_XSAVES;
10184 static void vmx_recover_nmi_blocking(struct vcpu_vmx *vmx)
10186 u32 exit_intr_info;
10189 bool idtv_info_valid;
10191 idtv_info_valid = vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK;
10194 if (vmx->loaded_vmcs->nmi_known_unmasked)
10197 * Can't use vmx->exit_intr_info since we're not sure what
10198 * the exit reason is.
10200 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
10201 unblock_nmi = (exit_intr_info & INTR_INFO_UNBLOCK_NMI) != 0;
10202 vector = exit_intr_info & INTR_INFO_VECTOR_MASK;
10204 * SDM 3: 27.7.1.2 (September 2008)
10205 * Re-set bit "block by NMI" before VM entry if vmexit caused by
10206 * a guest IRET fault.
10207 * SDM 3: 23.2.2 (September 2008)
10208 * Bit 12 is undefined in any of the following cases:
10209 * If the VM exit sets the valid bit in the IDT-vectoring
10210 * information field.
10211 * If the VM exit is due to a double fault.
10213 if ((exit_intr_info & INTR_INFO_VALID_MASK) && unblock_nmi &&
10214 vector != DF_VECTOR && !idtv_info_valid)
10215 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
10216 GUEST_INTR_STATE_NMI);
10218 vmx->loaded_vmcs->nmi_known_unmasked =
10219 !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO)
10220 & GUEST_INTR_STATE_NMI);
10221 } else if (unlikely(vmx->loaded_vmcs->soft_vnmi_blocked))
10222 vmx->loaded_vmcs->vnmi_blocked_time +=
10223 ktime_to_ns(ktime_sub(ktime_get(),
10224 vmx->loaded_vmcs->entry_time));
10227 static void __vmx_complete_interrupts(struct kvm_vcpu *vcpu,
10228 u32 idt_vectoring_info,
10229 int instr_len_field,
10230 int error_code_field)
10234 bool idtv_info_valid;
10236 idtv_info_valid = idt_vectoring_info & VECTORING_INFO_VALID_MASK;
10238 vcpu->arch.nmi_injected = false;
10239 kvm_clear_exception_queue(vcpu);
10240 kvm_clear_interrupt_queue(vcpu);
10242 if (!idtv_info_valid)
10245 kvm_make_request(KVM_REQ_EVENT, vcpu);
10247 vector = idt_vectoring_info & VECTORING_INFO_VECTOR_MASK;
10248 type = idt_vectoring_info & VECTORING_INFO_TYPE_MASK;
10251 case INTR_TYPE_NMI_INTR:
10252 vcpu->arch.nmi_injected = true;
10254 * SDM 3: 27.7.1.2 (September 2008)
10255 * Clear bit "block by NMI" before VM entry if a NMI
10256 * delivery faulted.
10258 vmx_set_nmi_mask(vcpu, false);
10260 case INTR_TYPE_SOFT_EXCEPTION:
10261 vcpu->arch.event_exit_inst_len = vmcs_read32(instr_len_field);
10263 case INTR_TYPE_HARD_EXCEPTION:
10264 if (idt_vectoring_info & VECTORING_INFO_DELIVER_CODE_MASK) {
10265 u32 err = vmcs_read32(error_code_field);
10266 kvm_requeue_exception_e(vcpu, vector, err);
10268 kvm_requeue_exception(vcpu, vector);
10270 case INTR_TYPE_SOFT_INTR:
10271 vcpu->arch.event_exit_inst_len = vmcs_read32(instr_len_field);
10273 case INTR_TYPE_EXT_INTR:
10274 kvm_queue_interrupt(vcpu, vector, type == INTR_TYPE_SOFT_INTR);
10281 static void vmx_complete_interrupts(struct vcpu_vmx *vmx)
10283 __vmx_complete_interrupts(&vmx->vcpu, vmx->idt_vectoring_info,
10284 VM_EXIT_INSTRUCTION_LEN,
10285 IDT_VECTORING_ERROR_CODE);
10288 static void vmx_cancel_injection(struct kvm_vcpu *vcpu)
10290 __vmx_complete_interrupts(vcpu,
10291 vmcs_read32(VM_ENTRY_INTR_INFO_FIELD),
10292 VM_ENTRY_INSTRUCTION_LEN,
10293 VM_ENTRY_EXCEPTION_ERROR_CODE);
10295 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0);
10298 static void atomic_switch_perf_msrs(struct vcpu_vmx *vmx)
10301 struct perf_guest_switch_msr *msrs;
10303 msrs = perf_guest_get_msrs(&nr_msrs);
10308 for (i = 0; i < nr_msrs; i++)
10309 if (msrs[i].host == msrs[i].guest)
10310 clear_atomic_switch_msr(vmx, msrs[i].msr);
10312 add_atomic_switch_msr(vmx, msrs[i].msr, msrs[i].guest,
10316 static void vmx_arm_hv_timer(struct kvm_vcpu *vcpu)
10318 struct vcpu_vmx *vmx = to_vmx(vcpu);
10322 if (vmx->hv_deadline_tsc == -1)
10326 if (vmx->hv_deadline_tsc > tscl)
10327 /* sure to be 32 bit only because checked on set_hv_timer */
10328 delta_tsc = (u32)((vmx->hv_deadline_tsc - tscl) >>
10329 cpu_preemption_timer_multi);
10333 vmcs_write32(VMX_PREEMPTION_TIMER_VALUE, delta_tsc);
10336 static void __noclone vmx_vcpu_run(struct kvm_vcpu *vcpu)
10338 struct vcpu_vmx *vmx = to_vmx(vcpu);
10339 unsigned long cr3, cr4, evmcs_rsp;
10341 /* Record the guest's net vcpu time for enforced NMI injections. */
10342 if (unlikely(!enable_vnmi &&
10343 vmx->loaded_vmcs->soft_vnmi_blocked))
10344 vmx->loaded_vmcs->entry_time = ktime_get();
10346 /* Don't enter VMX if guest state is invalid, let the exit handler
10347 start emulation until we arrive back to a valid state */
10348 if (vmx->emulation_required)
10351 if (vmx->ple_window_dirty) {
10352 vmx->ple_window_dirty = false;
10353 vmcs_write32(PLE_WINDOW, vmx->ple_window);
10356 if (vmx->nested.sync_shadow_vmcs) {
10357 copy_vmcs12_to_shadow(vmx);
10358 vmx->nested.sync_shadow_vmcs = false;
10361 if (test_bit(VCPU_REGS_RSP, (unsigned long *)&vcpu->arch.regs_dirty))
10362 vmcs_writel(GUEST_RSP, vcpu->arch.regs[VCPU_REGS_RSP]);
10363 if (test_bit(VCPU_REGS_RIP, (unsigned long *)&vcpu->arch.regs_dirty))
10364 vmcs_writel(GUEST_RIP, vcpu->arch.regs[VCPU_REGS_RIP]);
10366 cr3 = __get_current_cr3_fast();
10367 if (unlikely(cr3 != vmx->loaded_vmcs->vmcs_host_cr3)) {
10368 vmcs_writel(HOST_CR3, cr3);
10369 vmx->loaded_vmcs->vmcs_host_cr3 = cr3;
10372 cr4 = cr4_read_shadow();
10373 if (unlikely(cr4 != vmx->loaded_vmcs->vmcs_host_cr4)) {
10374 vmcs_writel(HOST_CR4, cr4);
10375 vmx->loaded_vmcs->vmcs_host_cr4 = cr4;
10378 /* When single-stepping over STI and MOV SS, we must clear the
10379 * corresponding interruptibility bits in the guest state. Otherwise
10380 * vmentry fails as it then expects bit 14 (BS) in pending debug
10381 * exceptions being set, but that's not correct for the guest debugging
10383 if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP)
10384 vmx_set_interrupt_shadow(vcpu, 0);
10386 if (static_cpu_has(X86_FEATURE_PKU) &&
10387 kvm_read_cr4_bits(vcpu, X86_CR4_PKE) &&
10388 vcpu->arch.pkru != vmx->host_pkru)
10389 __write_pkru(vcpu->arch.pkru);
10391 atomic_switch_perf_msrs(vmx);
10393 vmx_arm_hv_timer(vcpu);
10396 * If this vCPU has touched SPEC_CTRL, restore the guest's value if
10397 * it's non-zero. Since vmentry is serialising on affected CPUs, there
10398 * is no need to worry about the conditional branch over the wrmsr
10399 * being speculatively taken.
10401 x86_spec_ctrl_set_guest(vmx->spec_ctrl, 0);
10403 vmx->__launched = vmx->loaded_vmcs->launched;
10405 evmcs_rsp = static_branch_unlikely(&enable_evmcs) ?
10406 (unsigned long)¤t_evmcs->host_rsp : 0;
10409 /* Store host registers */
10410 "push %%" _ASM_DX "; push %%" _ASM_BP ";"
10411 "push %%" _ASM_CX " \n\t" /* placeholder for guest rcx */
10412 "push %%" _ASM_CX " \n\t"
10413 "cmp %%" _ASM_SP ", %c[host_rsp](%0) \n\t"
10415 "mov %%" _ASM_SP ", %c[host_rsp](%0) \n\t"
10416 /* Avoid VMWRITE when Enlightened VMCS is in use */
10417 "test %%" _ASM_SI ", %%" _ASM_SI " \n\t"
10419 "mov %%" _ASM_SP ", (%%" _ASM_SI ") \n\t"
10422 __ex(ASM_VMX_VMWRITE_RSP_RDX) "\n\t"
10424 /* Reload cr2 if changed */
10425 "mov %c[cr2](%0), %%" _ASM_AX " \n\t"
10426 "mov %%cr2, %%" _ASM_DX " \n\t"
10427 "cmp %%" _ASM_AX ", %%" _ASM_DX " \n\t"
10429 "mov %%" _ASM_AX", %%cr2 \n\t"
10431 /* Check if vmlaunch of vmresume is needed */
10432 "cmpl $0, %c[launched](%0) \n\t"
10433 /* Load guest registers. Don't clobber flags. */
10434 "mov %c[rax](%0), %%" _ASM_AX " \n\t"
10435 "mov %c[rbx](%0), %%" _ASM_BX " \n\t"
10436 "mov %c[rdx](%0), %%" _ASM_DX " \n\t"
10437 "mov %c[rsi](%0), %%" _ASM_SI " \n\t"
10438 "mov %c[rdi](%0), %%" _ASM_DI " \n\t"
10439 "mov %c[rbp](%0), %%" _ASM_BP " \n\t"
10440 #ifdef CONFIG_X86_64
10441 "mov %c[r8](%0), %%r8 \n\t"
10442 "mov %c[r9](%0), %%r9 \n\t"
10443 "mov %c[r10](%0), %%r10 \n\t"
10444 "mov %c[r11](%0), %%r11 \n\t"
10445 "mov %c[r12](%0), %%r12 \n\t"
10446 "mov %c[r13](%0), %%r13 \n\t"
10447 "mov %c[r14](%0), %%r14 \n\t"
10448 "mov %c[r15](%0), %%r15 \n\t"
10450 "mov %c[rcx](%0), %%" _ASM_CX " \n\t" /* kills %0 (ecx) */
10452 /* Enter guest mode */
10454 __ex(ASM_VMX_VMLAUNCH) "\n\t"
10456 "1: " __ex(ASM_VMX_VMRESUME) "\n\t"
10458 /* Save guest registers, load host registers, keep flags */
10459 "mov %0, %c[wordsize](%%" _ASM_SP ") \n\t"
10461 "setbe %c[fail](%0)\n\t"
10462 "mov %%" _ASM_AX ", %c[rax](%0) \n\t"
10463 "mov %%" _ASM_BX ", %c[rbx](%0) \n\t"
10464 __ASM_SIZE(pop) " %c[rcx](%0) \n\t"
10465 "mov %%" _ASM_DX ", %c[rdx](%0) \n\t"
10466 "mov %%" _ASM_SI ", %c[rsi](%0) \n\t"
10467 "mov %%" _ASM_DI ", %c[rdi](%0) \n\t"
10468 "mov %%" _ASM_BP ", %c[rbp](%0) \n\t"
10469 #ifdef CONFIG_X86_64
10470 "mov %%r8, %c[r8](%0) \n\t"
10471 "mov %%r9, %c[r9](%0) \n\t"
10472 "mov %%r10, %c[r10](%0) \n\t"
10473 "mov %%r11, %c[r11](%0) \n\t"
10474 "mov %%r12, %c[r12](%0) \n\t"
10475 "mov %%r13, %c[r13](%0) \n\t"
10476 "mov %%r14, %c[r14](%0) \n\t"
10477 "mov %%r15, %c[r15](%0) \n\t"
10478 "xor %%r8d, %%r8d \n\t"
10479 "xor %%r9d, %%r9d \n\t"
10480 "xor %%r10d, %%r10d \n\t"
10481 "xor %%r11d, %%r11d \n\t"
10482 "xor %%r12d, %%r12d \n\t"
10483 "xor %%r13d, %%r13d \n\t"
10484 "xor %%r14d, %%r14d \n\t"
10485 "xor %%r15d, %%r15d \n\t"
10487 "mov %%cr2, %%" _ASM_AX " \n\t"
10488 "mov %%" _ASM_AX ", %c[cr2](%0) \n\t"
10490 "xor %%eax, %%eax \n\t"
10491 "xor %%ebx, %%ebx \n\t"
10492 "xor %%esi, %%esi \n\t"
10493 "xor %%edi, %%edi \n\t"
10494 "pop %%" _ASM_BP "; pop %%" _ASM_DX " \n\t"
10495 ".pushsection .rodata \n\t"
10496 ".global vmx_return \n\t"
10497 "vmx_return: " _ASM_PTR " 2b \n\t"
10499 : : "c"(vmx), "d"((unsigned long)HOST_RSP), "S"(evmcs_rsp),
10500 [launched]"i"(offsetof(struct vcpu_vmx, __launched)),
10501 [fail]"i"(offsetof(struct vcpu_vmx, fail)),
10502 [host_rsp]"i"(offsetof(struct vcpu_vmx, host_rsp)),
10503 [rax]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RAX])),
10504 [rbx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBX])),
10505 [rcx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RCX])),
10506 [rdx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDX])),
10507 [rsi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RSI])),
10508 [rdi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDI])),
10509 [rbp]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBP])),
10510 #ifdef CONFIG_X86_64
10511 [r8]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R8])),
10512 [r9]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R9])),
10513 [r10]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R10])),
10514 [r11]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R11])),
10515 [r12]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R12])),
10516 [r13]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R13])),
10517 [r14]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R14])),
10518 [r15]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R15])),
10520 [cr2]"i"(offsetof(struct vcpu_vmx, vcpu.arch.cr2)),
10521 [wordsize]"i"(sizeof(ulong))
10523 #ifdef CONFIG_X86_64
10524 , "rax", "rbx", "rdi"
10525 , "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15"
10527 , "eax", "ebx", "edi"
10532 * We do not use IBRS in the kernel. If this vCPU has used the
10533 * SPEC_CTRL MSR it may have left it on; save the value and
10534 * turn it off. This is much more efficient than blindly adding
10535 * it to the atomic save/restore list. Especially as the former
10536 * (Saving guest MSRs on vmexit) doesn't even exist in KVM.
10538 * For non-nested case:
10539 * If the L01 MSR bitmap does not intercept the MSR, then we need to
10543 * If the L02 MSR bitmap does not intercept the MSR, then we need to
10546 if (unlikely(!msr_write_intercepted(vcpu, MSR_IA32_SPEC_CTRL)))
10547 vmx->spec_ctrl = native_read_msr(MSR_IA32_SPEC_CTRL);
10549 x86_spec_ctrl_restore_host(vmx->spec_ctrl, 0);
10551 /* Eliminate branch target predictions from guest mode */
10554 /* All fields are clean at this point */
10555 if (static_branch_unlikely(&enable_evmcs))
10556 current_evmcs->hv_clean_fields |=
10557 HV_VMX_ENLIGHTENED_CLEAN_FIELD_ALL;
10559 /* MSR_IA32_DEBUGCTLMSR is zeroed on vmexit. Restore it if needed */
10560 if (vmx->host_debugctlmsr)
10561 update_debugctlmsr(vmx->host_debugctlmsr);
10563 #ifndef CONFIG_X86_64
10565 * The sysexit path does not restore ds/es, so we must set them to
10566 * a reasonable value ourselves.
10568 * We can't defer this to vmx_load_host_state() since that function
10569 * may be executed in interrupt context, which saves and restore segments
10570 * around it, nullifying its effect.
10572 loadsegment(ds, __USER_DS);
10573 loadsegment(es, __USER_DS);
10576 vcpu->arch.regs_avail = ~((1 << VCPU_REGS_RIP) | (1 << VCPU_REGS_RSP)
10577 | (1 << VCPU_EXREG_RFLAGS)
10578 | (1 << VCPU_EXREG_PDPTR)
10579 | (1 << VCPU_EXREG_SEGMENTS)
10580 | (1 << VCPU_EXREG_CR3));
10581 vcpu->arch.regs_dirty = 0;
10584 * eager fpu is enabled if PKEY is supported and CR4 is switched
10585 * back on host, so it is safe to read guest PKRU from current
10588 if (static_cpu_has(X86_FEATURE_PKU) &&
10589 kvm_read_cr4_bits(vcpu, X86_CR4_PKE)) {
10590 vcpu->arch.pkru = __read_pkru();
10591 if (vcpu->arch.pkru != vmx->host_pkru)
10592 __write_pkru(vmx->host_pkru);
10595 vmx->nested.nested_run_pending = 0;
10596 vmx->idt_vectoring_info = 0;
10598 vmx->exit_reason = vmx->fail ? 0xdead : vmcs_read32(VM_EXIT_REASON);
10599 if (vmx->fail || (vmx->exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY))
10602 vmx->loaded_vmcs->launched = 1;
10603 vmx->idt_vectoring_info = vmcs_read32(IDT_VECTORING_INFO_FIELD);
10605 vmx_complete_atomic_exit(vmx);
10606 vmx_recover_nmi_blocking(vmx);
10607 vmx_complete_interrupts(vmx);
10609 STACK_FRAME_NON_STANDARD(vmx_vcpu_run);
10611 static struct kvm *vmx_vm_alloc(void)
10613 struct kvm_vmx *kvm_vmx = vzalloc(sizeof(struct kvm_vmx));
10614 return &kvm_vmx->kvm;
10617 static void vmx_vm_free(struct kvm *kvm)
10619 vfree(to_kvm_vmx(kvm));
10622 static void vmx_switch_vmcs(struct kvm_vcpu *vcpu, struct loaded_vmcs *vmcs)
10624 struct vcpu_vmx *vmx = to_vmx(vcpu);
10627 if (vmx->loaded_vmcs == vmcs)
10631 vmx_vcpu_put(vcpu);
10632 vmx->loaded_vmcs = vmcs;
10633 vmx_vcpu_load(vcpu, cpu);
10638 * Ensure that the current vmcs of the logical processor is the
10639 * vmcs01 of the vcpu before calling free_nested().
10641 static void vmx_free_vcpu_nested(struct kvm_vcpu *vcpu)
10643 struct vcpu_vmx *vmx = to_vmx(vcpu);
10646 vmx_switch_vmcs(vcpu, &vmx->vmcs01);
10651 static void vmx_free_vcpu(struct kvm_vcpu *vcpu)
10653 struct vcpu_vmx *vmx = to_vmx(vcpu);
10656 vmx_destroy_pml_buffer(vmx);
10657 free_vpid(vmx->vpid);
10658 leave_guest_mode(vcpu);
10659 vmx_free_vcpu_nested(vcpu);
10660 free_loaded_vmcs(vmx->loaded_vmcs);
10661 kfree(vmx->guest_msrs);
10662 kvm_vcpu_uninit(vcpu);
10663 kmem_cache_free(kvm_vcpu_cache, vmx);
10666 static struct kvm_vcpu *vmx_create_vcpu(struct kvm *kvm, unsigned int id)
10669 struct vcpu_vmx *vmx = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
10670 unsigned long *msr_bitmap;
10674 return ERR_PTR(-ENOMEM);
10676 vmx->vpid = allocate_vpid();
10678 err = kvm_vcpu_init(&vmx->vcpu, kvm, id);
10685 * If PML is turned on, failure on enabling PML just results in failure
10686 * of creating the vcpu, therefore we can simplify PML logic (by
10687 * avoiding dealing with cases, such as enabling PML partially on vcpus
10688 * for the guest, etc.
10691 vmx->pml_pg = alloc_page(GFP_KERNEL | __GFP_ZERO);
10696 vmx->guest_msrs = kmalloc(PAGE_SIZE, GFP_KERNEL);
10697 BUILD_BUG_ON(ARRAY_SIZE(vmx_msr_index) * sizeof(vmx->guest_msrs[0])
10700 if (!vmx->guest_msrs)
10703 err = alloc_loaded_vmcs(&vmx->vmcs01);
10707 msr_bitmap = vmx->vmcs01.msr_bitmap;
10708 vmx_disable_intercept_for_msr(msr_bitmap, MSR_FS_BASE, MSR_TYPE_RW);
10709 vmx_disable_intercept_for_msr(msr_bitmap, MSR_GS_BASE, MSR_TYPE_RW);
10710 vmx_disable_intercept_for_msr(msr_bitmap, MSR_KERNEL_GS_BASE, MSR_TYPE_RW);
10711 vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_SYSENTER_CS, MSR_TYPE_RW);
10712 vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_SYSENTER_ESP, MSR_TYPE_RW);
10713 vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_SYSENTER_EIP, MSR_TYPE_RW);
10714 vmx->msr_bitmap_mode = 0;
10716 vmx->loaded_vmcs = &vmx->vmcs01;
10718 vmx_vcpu_load(&vmx->vcpu, cpu);
10719 vmx->vcpu.cpu = cpu;
10720 vmx_vcpu_setup(vmx);
10721 vmx_vcpu_put(&vmx->vcpu);
10723 if (cpu_need_virtualize_apic_accesses(&vmx->vcpu)) {
10724 err = alloc_apic_access_page(kvm);
10729 if (enable_ept && !enable_unrestricted_guest) {
10730 err = init_rmode_identity_map(kvm);
10736 nested_vmx_setup_ctls_msrs(&vmx->nested.msrs,
10737 kvm_vcpu_apicv_active(&vmx->vcpu));
10738 vmx->nested.vpid02 = allocate_vpid();
10741 vmx->nested.posted_intr_nv = -1;
10742 vmx->nested.current_vmptr = -1ull;
10744 vmx->msr_ia32_feature_control_valid_bits = FEATURE_CONTROL_LOCKED;
10747 * Enforce invariant: pi_desc.nv is always either POSTED_INTR_VECTOR
10748 * or POSTED_INTR_WAKEUP_VECTOR.
10750 vmx->pi_desc.nv = POSTED_INTR_VECTOR;
10751 vmx->pi_desc.sn = 1;
10756 free_vpid(vmx->nested.vpid02);
10757 free_loaded_vmcs(vmx->loaded_vmcs);
10759 kfree(vmx->guest_msrs);
10761 vmx_destroy_pml_buffer(vmx);
10763 kvm_vcpu_uninit(&vmx->vcpu);
10765 free_vpid(vmx->vpid);
10766 kmem_cache_free(kvm_vcpu_cache, vmx);
10767 return ERR_PTR(err);
10770 static int vmx_vm_init(struct kvm *kvm)
10772 spin_lock_init(&to_kvm_vmx(kvm)->ept_pointer_lock);
10775 kvm->arch.pause_in_guest = true;
10779 static void __init vmx_check_processor_compat(void *rtn)
10781 struct vmcs_config vmcs_conf;
10784 if (setup_vmcs_config(&vmcs_conf) < 0)
10785 *(int *)rtn = -EIO;
10786 nested_vmx_setup_ctls_msrs(&vmcs_conf.nested, enable_apicv);
10787 if (memcmp(&vmcs_config, &vmcs_conf, sizeof(struct vmcs_config)) != 0) {
10788 printk(KERN_ERR "kvm: CPU %d feature inconsistency!\n",
10789 smp_processor_id());
10790 *(int *)rtn = -EIO;
10794 static u64 vmx_get_mt_mask(struct kvm_vcpu *vcpu, gfn_t gfn, bool is_mmio)
10799 /* For VT-d and EPT combination
10800 * 1. MMIO: always map as UC
10801 * 2. EPT with VT-d:
10802 * a. VT-d without snooping control feature: can't guarantee the
10803 * result, try to trust guest.
10804 * b. VT-d with snooping control feature: snooping control feature of
10805 * VT-d engine can guarantee the cache correctness. Just set it
10806 * to WB to keep consistent with host. So the same as item 3.
10807 * 3. EPT without VT-d: always map as WB and set IPAT=1 to keep
10808 * consistent with host MTRR
10811 cache = MTRR_TYPE_UNCACHABLE;
10815 if (!kvm_arch_has_noncoherent_dma(vcpu->kvm)) {
10816 ipat = VMX_EPT_IPAT_BIT;
10817 cache = MTRR_TYPE_WRBACK;
10821 if (kvm_read_cr0(vcpu) & X86_CR0_CD) {
10822 ipat = VMX_EPT_IPAT_BIT;
10823 if (kvm_check_has_quirk(vcpu->kvm, KVM_X86_QUIRK_CD_NW_CLEARED))
10824 cache = MTRR_TYPE_WRBACK;
10826 cache = MTRR_TYPE_UNCACHABLE;
10830 cache = kvm_mtrr_get_guest_memory_type(vcpu, gfn);
10833 return (cache << VMX_EPT_MT_EPTE_SHIFT) | ipat;
10836 static int vmx_get_lpage_level(void)
10838 if (enable_ept && !cpu_has_vmx_ept_1g_page())
10839 return PT_DIRECTORY_LEVEL;
10841 /* For shadow and EPT supported 1GB page */
10842 return PT_PDPE_LEVEL;
10845 static void vmcs_set_secondary_exec_control(u32 new_ctl)
10848 * These bits in the secondary execution controls field
10849 * are dynamic, the others are mostly based on the hypervisor
10850 * architecture and the guest's CPUID. Do not touch the
10854 SECONDARY_EXEC_SHADOW_VMCS |
10855 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
10856 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
10857 SECONDARY_EXEC_DESC;
10859 u32 cur_ctl = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
10861 vmcs_write32(SECONDARY_VM_EXEC_CONTROL,
10862 (new_ctl & ~mask) | (cur_ctl & mask));
10866 * Generate MSR_IA32_VMX_CR{0,4}_FIXED1 according to CPUID. Only set bits
10867 * (indicating "allowed-1") if they are supported in the guest's CPUID.
10869 static void nested_vmx_cr_fixed1_bits_update(struct kvm_vcpu *vcpu)
10871 struct vcpu_vmx *vmx = to_vmx(vcpu);
10872 struct kvm_cpuid_entry2 *entry;
10874 vmx->nested.msrs.cr0_fixed1 = 0xffffffff;
10875 vmx->nested.msrs.cr4_fixed1 = X86_CR4_PCE;
10877 #define cr4_fixed1_update(_cr4_mask, _reg, _cpuid_mask) do { \
10878 if (entry && (entry->_reg & (_cpuid_mask))) \
10879 vmx->nested.msrs.cr4_fixed1 |= (_cr4_mask); \
10882 entry = kvm_find_cpuid_entry(vcpu, 0x1, 0);
10883 cr4_fixed1_update(X86_CR4_VME, edx, bit(X86_FEATURE_VME));
10884 cr4_fixed1_update(X86_CR4_PVI, edx, bit(X86_FEATURE_VME));
10885 cr4_fixed1_update(X86_CR4_TSD, edx, bit(X86_FEATURE_TSC));
10886 cr4_fixed1_update(X86_CR4_DE, edx, bit(X86_FEATURE_DE));
10887 cr4_fixed1_update(X86_CR4_PSE, edx, bit(X86_FEATURE_PSE));
10888 cr4_fixed1_update(X86_CR4_PAE, edx, bit(X86_FEATURE_PAE));
10889 cr4_fixed1_update(X86_CR4_MCE, edx, bit(X86_FEATURE_MCE));
10890 cr4_fixed1_update(X86_CR4_PGE, edx, bit(X86_FEATURE_PGE));
10891 cr4_fixed1_update(X86_CR4_OSFXSR, edx, bit(X86_FEATURE_FXSR));
10892 cr4_fixed1_update(X86_CR4_OSXMMEXCPT, edx, bit(X86_FEATURE_XMM));
10893 cr4_fixed1_update(X86_CR4_VMXE, ecx, bit(X86_FEATURE_VMX));
10894 cr4_fixed1_update(X86_CR4_SMXE, ecx, bit(X86_FEATURE_SMX));
10895 cr4_fixed1_update(X86_CR4_PCIDE, ecx, bit(X86_FEATURE_PCID));
10896 cr4_fixed1_update(X86_CR4_OSXSAVE, ecx, bit(X86_FEATURE_XSAVE));
10898 entry = kvm_find_cpuid_entry(vcpu, 0x7, 0);
10899 cr4_fixed1_update(X86_CR4_FSGSBASE, ebx, bit(X86_FEATURE_FSGSBASE));
10900 cr4_fixed1_update(X86_CR4_SMEP, ebx, bit(X86_FEATURE_SMEP));
10901 cr4_fixed1_update(X86_CR4_SMAP, ebx, bit(X86_FEATURE_SMAP));
10902 cr4_fixed1_update(X86_CR4_PKE, ecx, bit(X86_FEATURE_PKU));
10903 cr4_fixed1_update(X86_CR4_UMIP, ecx, bit(X86_FEATURE_UMIP));
10905 #undef cr4_fixed1_update
10908 static void vmx_cpuid_update(struct kvm_vcpu *vcpu)
10910 struct vcpu_vmx *vmx = to_vmx(vcpu);
10912 if (cpu_has_secondary_exec_ctrls()) {
10913 vmx_compute_secondary_exec_control(vmx);
10914 vmcs_set_secondary_exec_control(vmx->secondary_exec_control);
10917 if (nested_vmx_allowed(vcpu))
10918 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits |=
10919 FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
10921 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits &=
10922 ~FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
10924 if (nested_vmx_allowed(vcpu))
10925 nested_vmx_cr_fixed1_bits_update(vcpu);
10928 static void vmx_set_supported_cpuid(u32 func, struct kvm_cpuid_entry2 *entry)
10930 if (func == 1 && nested)
10931 entry->ecx |= bit(X86_FEATURE_VMX);
10934 static void nested_ept_inject_page_fault(struct kvm_vcpu *vcpu,
10935 struct x86_exception *fault)
10937 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
10938 struct vcpu_vmx *vmx = to_vmx(vcpu);
10940 unsigned long exit_qualification = vcpu->arch.exit_qualification;
10942 if (vmx->nested.pml_full) {
10943 exit_reason = EXIT_REASON_PML_FULL;
10944 vmx->nested.pml_full = false;
10945 exit_qualification &= INTR_INFO_UNBLOCK_NMI;
10946 } else if (fault->error_code & PFERR_RSVD_MASK)
10947 exit_reason = EXIT_REASON_EPT_MISCONFIG;
10949 exit_reason = EXIT_REASON_EPT_VIOLATION;
10951 nested_vmx_vmexit(vcpu, exit_reason, 0, exit_qualification);
10952 vmcs12->guest_physical_address = fault->address;
10955 static bool nested_ept_ad_enabled(struct kvm_vcpu *vcpu)
10957 return nested_ept_get_cr3(vcpu) & VMX_EPTP_AD_ENABLE_BIT;
10960 /* Callbacks for nested_ept_init_mmu_context: */
10962 static unsigned long nested_ept_get_cr3(struct kvm_vcpu *vcpu)
10964 /* return the page table to be shadowed - in our case, EPT12 */
10965 return get_vmcs12(vcpu)->ept_pointer;
10968 static int nested_ept_init_mmu_context(struct kvm_vcpu *vcpu)
10970 WARN_ON(mmu_is_nested(vcpu));
10971 if (!valid_ept_address(vcpu, nested_ept_get_cr3(vcpu)))
10974 kvm_init_shadow_ept_mmu(vcpu,
10975 to_vmx(vcpu)->nested.msrs.ept_caps &
10976 VMX_EPT_EXECUTE_ONLY_BIT,
10977 nested_ept_ad_enabled(vcpu),
10978 nested_ept_get_cr3(vcpu));
10979 vcpu->arch.mmu.set_cr3 = vmx_set_cr3;
10980 vcpu->arch.mmu.get_cr3 = nested_ept_get_cr3;
10981 vcpu->arch.mmu.inject_page_fault = nested_ept_inject_page_fault;
10983 vcpu->arch.walk_mmu = &vcpu->arch.nested_mmu;
10987 static void nested_ept_uninit_mmu_context(struct kvm_vcpu *vcpu)
10989 vcpu->arch.walk_mmu = &vcpu->arch.mmu;
10992 static bool nested_vmx_is_page_fault_vmexit(struct vmcs12 *vmcs12,
10995 bool inequality, bit;
10997 bit = (vmcs12->exception_bitmap & (1u << PF_VECTOR)) != 0;
10999 (error_code & vmcs12->page_fault_error_code_mask) !=
11000 vmcs12->page_fault_error_code_match;
11001 return inequality ^ bit;
11004 static void vmx_inject_page_fault_nested(struct kvm_vcpu *vcpu,
11005 struct x86_exception *fault)
11007 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
11009 WARN_ON(!is_guest_mode(vcpu));
11011 if (nested_vmx_is_page_fault_vmexit(vmcs12, fault->error_code) &&
11012 !to_vmx(vcpu)->nested.nested_run_pending) {
11013 vmcs12->vm_exit_intr_error_code = fault->error_code;
11014 nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI,
11015 PF_VECTOR | INTR_TYPE_HARD_EXCEPTION |
11016 INTR_INFO_DELIVER_CODE_MASK | INTR_INFO_VALID_MASK,
11019 kvm_inject_page_fault(vcpu, fault);
11023 static inline bool nested_vmx_prepare_msr_bitmap(struct kvm_vcpu *vcpu,
11024 struct vmcs12 *vmcs12);
11026 static void nested_get_vmcs12_pages(struct kvm_vcpu *vcpu)
11028 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
11029 struct vcpu_vmx *vmx = to_vmx(vcpu);
11033 if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) {
11035 * Translate L1 physical address to host physical
11036 * address for vmcs02. Keep the page pinned, so this
11037 * physical address remains valid. We keep a reference
11038 * to it so we can release it later.
11040 if (vmx->nested.apic_access_page) { /* shouldn't happen */
11041 kvm_release_page_dirty(vmx->nested.apic_access_page);
11042 vmx->nested.apic_access_page = NULL;
11044 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->apic_access_addr);
11046 * If translation failed, no matter: This feature asks
11047 * to exit when accessing the given address, and if it
11048 * can never be accessed, this feature won't do
11051 if (!is_error_page(page)) {
11052 vmx->nested.apic_access_page = page;
11053 hpa = page_to_phys(vmx->nested.apic_access_page);
11054 vmcs_write64(APIC_ACCESS_ADDR, hpa);
11056 vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL,
11057 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES);
11061 if (nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW)) {
11062 if (vmx->nested.virtual_apic_page) { /* shouldn't happen */
11063 kvm_release_page_dirty(vmx->nested.virtual_apic_page);
11064 vmx->nested.virtual_apic_page = NULL;
11066 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->virtual_apic_page_addr);
11069 * If translation failed, VM entry will fail because
11070 * prepare_vmcs02 set VIRTUAL_APIC_PAGE_ADDR to -1ull.
11071 * Failing the vm entry is _not_ what the processor
11072 * does but it's basically the only possibility we
11073 * have. We could still enter the guest if CR8 load
11074 * exits are enabled, CR8 store exits are enabled, and
11075 * virtualize APIC access is disabled; in this case
11076 * the processor would never use the TPR shadow and we
11077 * could simply clear the bit from the execution
11078 * control. But such a configuration is useless, so
11079 * let's keep the code simple.
11081 if (!is_error_page(page)) {
11082 vmx->nested.virtual_apic_page = page;
11083 hpa = page_to_phys(vmx->nested.virtual_apic_page);
11084 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, hpa);
11088 if (nested_cpu_has_posted_intr(vmcs12)) {
11089 if (vmx->nested.pi_desc_page) { /* shouldn't happen */
11090 kunmap(vmx->nested.pi_desc_page);
11091 kvm_release_page_dirty(vmx->nested.pi_desc_page);
11092 vmx->nested.pi_desc_page = NULL;
11094 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->posted_intr_desc_addr);
11095 if (is_error_page(page))
11097 vmx->nested.pi_desc_page = page;
11098 vmx->nested.pi_desc = kmap(vmx->nested.pi_desc_page);
11099 vmx->nested.pi_desc =
11100 (struct pi_desc *)((void *)vmx->nested.pi_desc +
11101 (unsigned long)(vmcs12->posted_intr_desc_addr &
11103 vmcs_write64(POSTED_INTR_DESC_ADDR,
11104 page_to_phys(vmx->nested.pi_desc_page) +
11105 (unsigned long)(vmcs12->posted_intr_desc_addr &
11108 if (nested_vmx_prepare_msr_bitmap(vcpu, vmcs12))
11109 vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL,
11110 CPU_BASED_USE_MSR_BITMAPS);
11112 vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL,
11113 CPU_BASED_USE_MSR_BITMAPS);
11116 static void vmx_start_preemption_timer(struct kvm_vcpu *vcpu)
11118 u64 preemption_timeout = get_vmcs12(vcpu)->vmx_preemption_timer_value;
11119 struct vcpu_vmx *vmx = to_vmx(vcpu);
11121 if (vcpu->arch.virtual_tsc_khz == 0)
11124 /* Make sure short timeouts reliably trigger an immediate vmexit.
11125 * hrtimer_start does not guarantee this. */
11126 if (preemption_timeout <= 1) {
11127 vmx_preemption_timer_fn(&vmx->nested.preemption_timer);
11131 preemption_timeout <<= VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE;
11132 preemption_timeout *= 1000000;
11133 do_div(preemption_timeout, vcpu->arch.virtual_tsc_khz);
11134 hrtimer_start(&vmx->nested.preemption_timer,
11135 ns_to_ktime(preemption_timeout), HRTIMER_MODE_REL);
11138 static int nested_vmx_check_io_bitmap_controls(struct kvm_vcpu *vcpu,
11139 struct vmcs12 *vmcs12)
11141 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_IO_BITMAPS))
11144 if (!page_address_valid(vcpu, vmcs12->io_bitmap_a) ||
11145 !page_address_valid(vcpu, vmcs12->io_bitmap_b))
11151 static int nested_vmx_check_msr_bitmap_controls(struct kvm_vcpu *vcpu,
11152 struct vmcs12 *vmcs12)
11154 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS))
11157 if (!page_address_valid(vcpu, vmcs12->msr_bitmap))
11163 static int nested_vmx_check_tpr_shadow_controls(struct kvm_vcpu *vcpu,
11164 struct vmcs12 *vmcs12)
11166 if (!nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW))
11169 if (!page_address_valid(vcpu, vmcs12->virtual_apic_page_addr))
11176 * Merge L0's and L1's MSR bitmap, return false to indicate that
11177 * we do not use the hardware.
11179 static inline bool nested_vmx_prepare_msr_bitmap(struct kvm_vcpu *vcpu,
11180 struct vmcs12 *vmcs12)
11184 unsigned long *msr_bitmap_l1;
11185 unsigned long *msr_bitmap_l0 = to_vmx(vcpu)->nested.vmcs02.msr_bitmap;
11187 * pred_cmd & spec_ctrl are trying to verify two things:
11189 * 1. L0 gave a permission to L1 to actually passthrough the MSR. This
11190 * ensures that we do not accidentally generate an L02 MSR bitmap
11191 * from the L12 MSR bitmap that is too permissive.
11192 * 2. That L1 or L2s have actually used the MSR. This avoids
11193 * unnecessarily merging of the bitmap if the MSR is unused. This
11194 * works properly because we only update the L01 MSR bitmap lazily.
11195 * So even if L0 should pass L1 these MSRs, the L01 bitmap is only
11196 * updated to reflect this when L1 (or its L2s) actually write to
11199 bool pred_cmd = !msr_write_intercepted_l01(vcpu, MSR_IA32_PRED_CMD);
11200 bool spec_ctrl = !msr_write_intercepted_l01(vcpu, MSR_IA32_SPEC_CTRL);
11202 /* Nothing to do if the MSR bitmap is not in use. */
11203 if (!cpu_has_vmx_msr_bitmap() ||
11204 !nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS))
11207 if (!nested_cpu_has_virt_x2apic_mode(vmcs12) &&
11208 !pred_cmd && !spec_ctrl)
11211 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->msr_bitmap);
11212 if (is_error_page(page))
11215 msr_bitmap_l1 = (unsigned long *)kmap(page);
11216 if (nested_cpu_has_apic_reg_virt(vmcs12)) {
11218 * L0 need not intercept reads for MSRs between 0x800 and 0x8ff, it
11219 * just lets the processor take the value from the virtual-APIC page;
11220 * take those 256 bits directly from the L1 bitmap.
11222 for (msr = 0x800; msr <= 0x8ff; msr += BITS_PER_LONG) {
11223 unsigned word = msr / BITS_PER_LONG;
11224 msr_bitmap_l0[word] = msr_bitmap_l1[word];
11225 msr_bitmap_l0[word + (0x800 / sizeof(long))] = ~0;
11228 for (msr = 0x800; msr <= 0x8ff; msr += BITS_PER_LONG) {
11229 unsigned word = msr / BITS_PER_LONG;
11230 msr_bitmap_l0[word] = ~0;
11231 msr_bitmap_l0[word + (0x800 / sizeof(long))] = ~0;
11235 nested_vmx_disable_intercept_for_msr(
11236 msr_bitmap_l1, msr_bitmap_l0,
11237 X2APIC_MSR(APIC_TASKPRI),
11240 if (nested_cpu_has_vid(vmcs12)) {
11241 nested_vmx_disable_intercept_for_msr(
11242 msr_bitmap_l1, msr_bitmap_l0,
11243 X2APIC_MSR(APIC_EOI),
11245 nested_vmx_disable_intercept_for_msr(
11246 msr_bitmap_l1, msr_bitmap_l0,
11247 X2APIC_MSR(APIC_SELF_IPI),
11252 nested_vmx_disable_intercept_for_msr(
11253 msr_bitmap_l1, msr_bitmap_l0,
11254 MSR_IA32_SPEC_CTRL,
11255 MSR_TYPE_R | MSR_TYPE_W);
11258 nested_vmx_disable_intercept_for_msr(
11259 msr_bitmap_l1, msr_bitmap_l0,
11264 kvm_release_page_clean(page);
11269 static void nested_cache_shadow_vmcs12(struct kvm_vcpu *vcpu,
11270 struct vmcs12 *vmcs12)
11272 struct vmcs12 *shadow;
11275 if (!nested_cpu_has_shadow_vmcs(vmcs12) ||
11276 vmcs12->vmcs_link_pointer == -1ull)
11279 shadow = get_shadow_vmcs12(vcpu);
11280 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->vmcs_link_pointer);
11282 memcpy(shadow, kmap(page), VMCS12_SIZE);
11285 kvm_release_page_clean(page);
11288 static void nested_flush_cached_shadow_vmcs12(struct kvm_vcpu *vcpu,
11289 struct vmcs12 *vmcs12)
11291 struct vcpu_vmx *vmx = to_vmx(vcpu);
11293 if (!nested_cpu_has_shadow_vmcs(vmcs12) ||
11294 vmcs12->vmcs_link_pointer == -1ull)
11297 kvm_write_guest(vmx->vcpu.kvm, vmcs12->vmcs_link_pointer,
11298 get_shadow_vmcs12(vcpu), VMCS12_SIZE);
11301 static int nested_vmx_check_apic_access_controls(struct kvm_vcpu *vcpu,
11302 struct vmcs12 *vmcs12)
11304 if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES) &&
11305 !page_address_valid(vcpu, vmcs12->apic_access_addr))
11311 static int nested_vmx_check_apicv_controls(struct kvm_vcpu *vcpu,
11312 struct vmcs12 *vmcs12)
11314 if (!nested_cpu_has_virt_x2apic_mode(vmcs12) &&
11315 !nested_cpu_has_apic_reg_virt(vmcs12) &&
11316 !nested_cpu_has_vid(vmcs12) &&
11317 !nested_cpu_has_posted_intr(vmcs12))
11321 * If virtualize x2apic mode is enabled,
11322 * virtualize apic access must be disabled.
11324 if (nested_cpu_has_virt_x2apic_mode(vmcs12) &&
11325 nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES))
11329 * If virtual interrupt delivery is enabled,
11330 * we must exit on external interrupts.
11332 if (nested_cpu_has_vid(vmcs12) &&
11333 !nested_exit_on_intr(vcpu))
11337 * bits 15:8 should be zero in posted_intr_nv,
11338 * the descriptor address has been already checked
11339 * in nested_get_vmcs12_pages.
11341 if (nested_cpu_has_posted_intr(vmcs12) &&
11342 (!nested_cpu_has_vid(vmcs12) ||
11343 !nested_exit_intr_ack_set(vcpu) ||
11344 vmcs12->posted_intr_nv & 0xff00))
11347 /* tpr shadow is needed by all apicv features. */
11348 if (!nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW))
11354 static int nested_vmx_check_msr_switch(struct kvm_vcpu *vcpu,
11355 unsigned long count_field,
11356 unsigned long addr_field)
11358 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
11362 if (vmcs12_read_any(vmcs12, count_field, &count) ||
11363 vmcs12_read_any(vmcs12, addr_field, &addr)) {
11369 maxphyaddr = cpuid_maxphyaddr(vcpu);
11370 if (!IS_ALIGNED(addr, 16) || addr >> maxphyaddr ||
11371 (addr + count * sizeof(struct vmx_msr_entry) - 1) >> maxphyaddr) {
11372 pr_debug_ratelimited(
11373 "nVMX: invalid MSR switch (0x%lx, %d, %llu, 0x%08llx)",
11374 addr_field, maxphyaddr, count, addr);
11380 static int nested_vmx_check_msr_switch_controls(struct kvm_vcpu *vcpu,
11381 struct vmcs12 *vmcs12)
11383 if (vmcs12->vm_exit_msr_load_count == 0 &&
11384 vmcs12->vm_exit_msr_store_count == 0 &&
11385 vmcs12->vm_entry_msr_load_count == 0)
11386 return 0; /* Fast path */
11387 if (nested_vmx_check_msr_switch(vcpu, VM_EXIT_MSR_LOAD_COUNT,
11388 VM_EXIT_MSR_LOAD_ADDR) ||
11389 nested_vmx_check_msr_switch(vcpu, VM_EXIT_MSR_STORE_COUNT,
11390 VM_EXIT_MSR_STORE_ADDR) ||
11391 nested_vmx_check_msr_switch(vcpu, VM_ENTRY_MSR_LOAD_COUNT,
11392 VM_ENTRY_MSR_LOAD_ADDR))
11397 static int nested_vmx_check_pml_controls(struct kvm_vcpu *vcpu,
11398 struct vmcs12 *vmcs12)
11400 u64 address = vmcs12->pml_address;
11401 int maxphyaddr = cpuid_maxphyaddr(vcpu);
11403 if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_PML)) {
11404 if (!nested_cpu_has_ept(vmcs12) ||
11405 !IS_ALIGNED(address, 4096) ||
11406 address >> maxphyaddr)
11413 static int nested_vmx_check_shadow_vmcs_controls(struct kvm_vcpu *vcpu,
11414 struct vmcs12 *vmcs12)
11416 if (!nested_cpu_has_shadow_vmcs(vmcs12))
11419 if (!page_address_valid(vcpu, vmcs12->vmread_bitmap) ||
11420 !page_address_valid(vcpu, vmcs12->vmwrite_bitmap))
11426 static int nested_vmx_msr_check_common(struct kvm_vcpu *vcpu,
11427 struct vmx_msr_entry *e)
11429 /* x2APIC MSR accesses are not allowed */
11430 if (vcpu->arch.apic_base & X2APIC_ENABLE && e->index >> 8 == 0x8)
11432 if (e->index == MSR_IA32_UCODE_WRITE || /* SDM Table 35-2 */
11433 e->index == MSR_IA32_UCODE_REV)
11435 if (e->reserved != 0)
11440 static int nested_vmx_load_msr_check(struct kvm_vcpu *vcpu,
11441 struct vmx_msr_entry *e)
11443 if (e->index == MSR_FS_BASE ||
11444 e->index == MSR_GS_BASE ||
11445 e->index == MSR_IA32_SMM_MONITOR_CTL || /* SMM is not supported */
11446 nested_vmx_msr_check_common(vcpu, e))
11451 static int nested_vmx_store_msr_check(struct kvm_vcpu *vcpu,
11452 struct vmx_msr_entry *e)
11454 if (e->index == MSR_IA32_SMBASE || /* SMM is not supported */
11455 nested_vmx_msr_check_common(vcpu, e))
11461 * Load guest's/host's msr at nested entry/exit.
11462 * return 0 for success, entry index for failure.
11464 static u32 nested_vmx_load_msr(struct kvm_vcpu *vcpu, u64 gpa, u32 count)
11467 struct vmx_msr_entry e;
11468 struct msr_data msr;
11470 msr.host_initiated = false;
11471 for (i = 0; i < count; i++) {
11472 if (kvm_vcpu_read_guest(vcpu, gpa + i * sizeof(e),
11474 pr_debug_ratelimited(
11475 "%s cannot read MSR entry (%u, 0x%08llx)\n",
11476 __func__, i, gpa + i * sizeof(e));
11479 if (nested_vmx_load_msr_check(vcpu, &e)) {
11480 pr_debug_ratelimited(
11481 "%s check failed (%u, 0x%x, 0x%x)\n",
11482 __func__, i, e.index, e.reserved);
11485 msr.index = e.index;
11486 msr.data = e.value;
11487 if (kvm_set_msr(vcpu, &msr)) {
11488 pr_debug_ratelimited(
11489 "%s cannot write MSR (%u, 0x%x, 0x%llx)\n",
11490 __func__, i, e.index, e.value);
11499 static int nested_vmx_store_msr(struct kvm_vcpu *vcpu, u64 gpa, u32 count)
11502 struct vmx_msr_entry e;
11504 for (i = 0; i < count; i++) {
11505 struct msr_data msr_info;
11506 if (kvm_vcpu_read_guest(vcpu,
11507 gpa + i * sizeof(e),
11508 &e, 2 * sizeof(u32))) {
11509 pr_debug_ratelimited(
11510 "%s cannot read MSR entry (%u, 0x%08llx)\n",
11511 __func__, i, gpa + i * sizeof(e));
11514 if (nested_vmx_store_msr_check(vcpu, &e)) {
11515 pr_debug_ratelimited(
11516 "%s check failed (%u, 0x%x, 0x%x)\n",
11517 __func__, i, e.index, e.reserved);
11520 msr_info.host_initiated = false;
11521 msr_info.index = e.index;
11522 if (kvm_get_msr(vcpu, &msr_info)) {
11523 pr_debug_ratelimited(
11524 "%s cannot read MSR (%u, 0x%x)\n",
11525 __func__, i, e.index);
11528 if (kvm_vcpu_write_guest(vcpu,
11529 gpa + i * sizeof(e) +
11530 offsetof(struct vmx_msr_entry, value),
11531 &msr_info.data, sizeof(msr_info.data))) {
11532 pr_debug_ratelimited(
11533 "%s cannot write MSR (%u, 0x%x, 0x%llx)\n",
11534 __func__, i, e.index, msr_info.data);
11541 static bool nested_cr3_valid(struct kvm_vcpu *vcpu, unsigned long val)
11543 unsigned long invalid_mask;
11545 invalid_mask = (~0ULL) << cpuid_maxphyaddr(vcpu);
11546 return (val & invalid_mask) == 0;
11550 * Load guest's/host's cr3 at nested entry/exit. nested_ept is true if we are
11551 * emulating VM entry into a guest with EPT enabled.
11552 * Returns 0 on success, 1 on failure. Invalid state exit qualification code
11553 * is assigned to entry_failure_code on failure.
11555 static int nested_vmx_load_cr3(struct kvm_vcpu *vcpu, unsigned long cr3, bool nested_ept,
11556 u32 *entry_failure_code)
11558 if (cr3 != kvm_read_cr3(vcpu) || (!nested_ept && pdptrs_changed(vcpu))) {
11559 if (!nested_cr3_valid(vcpu, cr3)) {
11560 *entry_failure_code = ENTRY_FAIL_DEFAULT;
11565 * If PAE paging and EPT are both on, CR3 is not used by the CPU and
11566 * must not be dereferenced.
11568 if (!is_long_mode(vcpu) && is_pae(vcpu) && is_paging(vcpu) &&
11570 if (!load_pdptrs(vcpu, vcpu->arch.walk_mmu, cr3)) {
11571 *entry_failure_code = ENTRY_FAIL_PDPTE;
11578 kvm_mmu_new_cr3(vcpu, cr3, false);
11580 vcpu->arch.cr3 = cr3;
11581 __set_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail);
11583 kvm_init_mmu(vcpu, false);
11588 static void prepare_vmcs02_full(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
11590 struct vcpu_vmx *vmx = to_vmx(vcpu);
11592 vmcs_write16(GUEST_ES_SELECTOR, vmcs12->guest_es_selector);
11593 vmcs_write16(GUEST_SS_SELECTOR, vmcs12->guest_ss_selector);
11594 vmcs_write16(GUEST_DS_SELECTOR, vmcs12->guest_ds_selector);
11595 vmcs_write16(GUEST_FS_SELECTOR, vmcs12->guest_fs_selector);
11596 vmcs_write16(GUEST_GS_SELECTOR, vmcs12->guest_gs_selector);
11597 vmcs_write16(GUEST_LDTR_SELECTOR, vmcs12->guest_ldtr_selector);
11598 vmcs_write16(GUEST_TR_SELECTOR, vmcs12->guest_tr_selector);
11599 vmcs_write32(GUEST_ES_LIMIT, vmcs12->guest_es_limit);
11600 vmcs_write32(GUEST_SS_LIMIT, vmcs12->guest_ss_limit);
11601 vmcs_write32(GUEST_DS_LIMIT, vmcs12->guest_ds_limit);
11602 vmcs_write32(GUEST_FS_LIMIT, vmcs12->guest_fs_limit);
11603 vmcs_write32(GUEST_GS_LIMIT, vmcs12->guest_gs_limit);
11604 vmcs_write32(GUEST_LDTR_LIMIT, vmcs12->guest_ldtr_limit);
11605 vmcs_write32(GUEST_TR_LIMIT, vmcs12->guest_tr_limit);
11606 vmcs_write32(GUEST_GDTR_LIMIT, vmcs12->guest_gdtr_limit);
11607 vmcs_write32(GUEST_IDTR_LIMIT, vmcs12->guest_idtr_limit);
11608 vmcs_write32(GUEST_ES_AR_BYTES, vmcs12->guest_es_ar_bytes);
11609 vmcs_write32(GUEST_SS_AR_BYTES, vmcs12->guest_ss_ar_bytes);
11610 vmcs_write32(GUEST_DS_AR_BYTES, vmcs12->guest_ds_ar_bytes);
11611 vmcs_write32(GUEST_FS_AR_BYTES, vmcs12->guest_fs_ar_bytes);
11612 vmcs_write32(GUEST_GS_AR_BYTES, vmcs12->guest_gs_ar_bytes);
11613 vmcs_write32(GUEST_LDTR_AR_BYTES, vmcs12->guest_ldtr_ar_bytes);
11614 vmcs_write32(GUEST_TR_AR_BYTES, vmcs12->guest_tr_ar_bytes);
11615 vmcs_writel(GUEST_SS_BASE, vmcs12->guest_ss_base);
11616 vmcs_writel(GUEST_DS_BASE, vmcs12->guest_ds_base);
11617 vmcs_writel(GUEST_FS_BASE, vmcs12->guest_fs_base);
11618 vmcs_writel(GUEST_GS_BASE, vmcs12->guest_gs_base);
11619 vmcs_writel(GUEST_LDTR_BASE, vmcs12->guest_ldtr_base);
11620 vmcs_writel(GUEST_TR_BASE, vmcs12->guest_tr_base);
11621 vmcs_writel(GUEST_GDTR_BASE, vmcs12->guest_gdtr_base);
11622 vmcs_writel(GUEST_IDTR_BASE, vmcs12->guest_idtr_base);
11624 vmcs_write32(GUEST_SYSENTER_CS, vmcs12->guest_sysenter_cs);
11625 vmcs_writel(GUEST_PENDING_DBG_EXCEPTIONS,
11626 vmcs12->guest_pending_dbg_exceptions);
11627 vmcs_writel(GUEST_SYSENTER_ESP, vmcs12->guest_sysenter_esp);
11628 vmcs_writel(GUEST_SYSENTER_EIP, vmcs12->guest_sysenter_eip);
11630 if (nested_cpu_has_xsaves(vmcs12))
11631 vmcs_write64(XSS_EXIT_BITMAP, vmcs12->xss_exit_bitmap);
11632 vmcs_write64(VMCS_LINK_POINTER, -1ull);
11634 if (cpu_has_vmx_posted_intr())
11635 vmcs_write16(POSTED_INTR_NV, POSTED_INTR_NESTED_VECTOR);
11638 * Whether page-faults are trapped is determined by a combination of
11639 * 3 settings: PFEC_MASK, PFEC_MATCH and EXCEPTION_BITMAP.PF.
11640 * If enable_ept, L0 doesn't care about page faults and we should
11641 * set all of these to L1's desires. However, if !enable_ept, L0 does
11642 * care about (at least some) page faults, and because it is not easy
11643 * (if at all possible?) to merge L0 and L1's desires, we simply ask
11644 * to exit on each and every L2 page fault. This is done by setting
11645 * MASK=MATCH=0 and (see below) EB.PF=1.
11646 * Note that below we don't need special code to set EB.PF beyond the
11647 * "or"ing of the EB of vmcs01 and vmcs12, because when enable_ept,
11648 * vmcs01's EB.PF is 0 so the "or" will take vmcs12's value, and when
11649 * !enable_ept, EB.PF is 1, so the "or" will always be 1.
11651 vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK,
11652 enable_ept ? vmcs12->page_fault_error_code_mask : 0);
11653 vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH,
11654 enable_ept ? vmcs12->page_fault_error_code_match : 0);
11656 /* All VMFUNCs are currently emulated through L0 vmexits. */
11657 if (cpu_has_vmx_vmfunc())
11658 vmcs_write64(VM_FUNCTION_CONTROL, 0);
11660 if (cpu_has_vmx_apicv()) {
11661 vmcs_write64(EOI_EXIT_BITMAP0, vmcs12->eoi_exit_bitmap0);
11662 vmcs_write64(EOI_EXIT_BITMAP1, vmcs12->eoi_exit_bitmap1);
11663 vmcs_write64(EOI_EXIT_BITMAP2, vmcs12->eoi_exit_bitmap2);
11664 vmcs_write64(EOI_EXIT_BITMAP3, vmcs12->eoi_exit_bitmap3);
11668 * Set host-state according to L0's settings (vmcs12 is irrelevant here)
11669 * Some constant fields are set here by vmx_set_constant_host_state().
11670 * Other fields are different per CPU, and will be set later when
11671 * vmx_vcpu_load() is called, and when vmx_save_host_state() is called.
11673 vmx_set_constant_host_state(vmx);
11676 * Set the MSR load/store lists to match L0's settings.
11678 vmcs_write32(VM_EXIT_MSR_STORE_COUNT, 0);
11679 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, vmx->msr_autoload.nr);
11680 vmcs_write64(VM_EXIT_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.host));
11681 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, vmx->msr_autoload.nr);
11682 vmcs_write64(VM_ENTRY_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.guest));
11684 set_cr4_guest_host_mask(vmx);
11686 if (vmx_mpx_supported())
11687 vmcs_write64(GUEST_BNDCFGS, vmcs12->guest_bndcfgs);
11690 if (nested_cpu_has_vpid(vmcs12) && vmx->nested.vpid02)
11691 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->nested.vpid02);
11693 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid);
11697 * L1 may access the L2's PDPTR, so save them to construct vmcs12
11700 vmcs_write64(GUEST_PDPTR0, vmcs12->guest_pdptr0);
11701 vmcs_write64(GUEST_PDPTR1, vmcs12->guest_pdptr1);
11702 vmcs_write64(GUEST_PDPTR2, vmcs12->guest_pdptr2);
11703 vmcs_write64(GUEST_PDPTR3, vmcs12->guest_pdptr3);
11706 if (cpu_has_vmx_msr_bitmap())
11707 vmcs_write64(MSR_BITMAP, __pa(vmx->nested.vmcs02.msr_bitmap));
11711 * prepare_vmcs02 is called when the L1 guest hypervisor runs its nested
11712 * L2 guest. L1 has a vmcs for L2 (vmcs12), and this function "merges" it
11713 * with L0's requirements for its guest (a.k.a. vmcs01), so we can run the L2
11714 * guest in a way that will both be appropriate to L1's requests, and our
11715 * needs. In addition to modifying the active vmcs (which is vmcs02), this
11716 * function also has additional necessary side-effects, like setting various
11717 * vcpu->arch fields.
11718 * Returns 0 on success, 1 on failure. Invalid state exit qualification code
11719 * is assigned to entry_failure_code on failure.
11721 static int prepare_vmcs02(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12,
11722 u32 *entry_failure_code)
11724 struct vcpu_vmx *vmx = to_vmx(vcpu);
11725 u32 exec_control, vmcs12_exec_ctrl;
11727 if (vmx->nested.dirty_vmcs12) {
11728 prepare_vmcs02_full(vcpu, vmcs12);
11729 vmx->nested.dirty_vmcs12 = false;
11733 * First, the fields that are shadowed. This must be kept in sync
11734 * with vmx_shadow_fields.h.
11737 vmcs_write16(GUEST_CS_SELECTOR, vmcs12->guest_cs_selector);
11738 vmcs_write32(GUEST_CS_LIMIT, vmcs12->guest_cs_limit);
11739 vmcs_write32(GUEST_CS_AR_BYTES, vmcs12->guest_cs_ar_bytes);
11740 vmcs_writel(GUEST_ES_BASE, vmcs12->guest_es_base);
11741 vmcs_writel(GUEST_CS_BASE, vmcs12->guest_cs_base);
11744 * Not in vmcs02: GUEST_PML_INDEX, HOST_FS_SELECTOR, HOST_GS_SELECTOR,
11745 * HOST_FS_BASE, HOST_GS_BASE.
11748 if (vmx->nested.nested_run_pending &&
11749 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS)) {
11750 kvm_set_dr(vcpu, 7, vmcs12->guest_dr7);
11751 vmcs_write64(GUEST_IA32_DEBUGCTL, vmcs12->guest_ia32_debugctl);
11753 kvm_set_dr(vcpu, 7, vcpu->arch.dr7);
11754 vmcs_write64(GUEST_IA32_DEBUGCTL, vmx->nested.vmcs01_debugctl);
11756 if (vmx->nested.nested_run_pending) {
11757 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
11758 vmcs12->vm_entry_intr_info_field);
11759 vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE,
11760 vmcs12->vm_entry_exception_error_code);
11761 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
11762 vmcs12->vm_entry_instruction_len);
11763 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO,
11764 vmcs12->guest_interruptibility_info);
11765 vmx->loaded_vmcs->nmi_known_unmasked =
11766 !(vmcs12->guest_interruptibility_info & GUEST_INTR_STATE_NMI);
11768 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0);
11770 vmx_set_rflags(vcpu, vmcs12->guest_rflags);
11772 exec_control = vmcs12->pin_based_vm_exec_control;
11774 /* Preemption timer setting is only taken from vmcs01. */
11775 exec_control &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
11776 exec_control |= vmcs_config.pin_based_exec_ctrl;
11777 if (vmx->hv_deadline_tsc == -1)
11778 exec_control &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
11780 /* Posted interrupts setting is only taken from vmcs12. */
11781 if (nested_cpu_has_posted_intr(vmcs12)) {
11782 vmx->nested.posted_intr_nv = vmcs12->posted_intr_nv;
11783 vmx->nested.pi_pending = false;
11785 exec_control &= ~PIN_BASED_POSTED_INTR;
11788 vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, exec_control);
11790 vmx->nested.preemption_timer_expired = false;
11791 if (nested_cpu_has_preemption_timer(vmcs12))
11792 vmx_start_preemption_timer(vcpu);
11794 if (cpu_has_secondary_exec_ctrls()) {
11795 exec_control = vmx->secondary_exec_control;
11797 /* Take the following fields only from vmcs12 */
11798 exec_control &= ~(SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
11799 SECONDARY_EXEC_ENABLE_INVPCID |
11800 SECONDARY_EXEC_RDTSCP |
11801 SECONDARY_EXEC_XSAVES |
11802 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY |
11803 SECONDARY_EXEC_APIC_REGISTER_VIRT |
11804 SECONDARY_EXEC_ENABLE_VMFUNC);
11805 if (nested_cpu_has(vmcs12,
11806 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS)) {
11807 vmcs12_exec_ctrl = vmcs12->secondary_vm_exec_control &
11808 ~SECONDARY_EXEC_ENABLE_PML;
11809 exec_control |= vmcs12_exec_ctrl;
11812 /* VMCS shadowing for L2 is emulated for now */
11813 exec_control &= ~SECONDARY_EXEC_SHADOW_VMCS;
11815 if (exec_control & SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY)
11816 vmcs_write16(GUEST_INTR_STATUS,
11817 vmcs12->guest_intr_status);
11820 * Write an illegal value to APIC_ACCESS_ADDR. Later,
11821 * nested_get_vmcs12_pages will either fix it up or
11822 * remove the VM execution control.
11824 if (exec_control & SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)
11825 vmcs_write64(APIC_ACCESS_ADDR, -1ull);
11827 vmcs_write32(SECONDARY_VM_EXEC_CONTROL, exec_control);
11831 * HOST_RSP is normally set correctly in vmx_vcpu_run() just before
11832 * entry, but only if the current (host) sp changed from the value
11833 * we wrote last (vmx->host_rsp). This cache is no longer relevant
11834 * if we switch vmcs, and rather than hold a separate cache per vmcs,
11835 * here we just force the write to happen on entry.
11839 exec_control = vmx_exec_control(vmx); /* L0's desires */
11840 exec_control &= ~CPU_BASED_VIRTUAL_INTR_PENDING;
11841 exec_control &= ~CPU_BASED_VIRTUAL_NMI_PENDING;
11842 exec_control &= ~CPU_BASED_TPR_SHADOW;
11843 exec_control |= vmcs12->cpu_based_vm_exec_control;
11846 * Write an illegal value to VIRTUAL_APIC_PAGE_ADDR. Later, if
11847 * nested_get_vmcs12_pages can't fix it up, the illegal value
11848 * will result in a VM entry failure.
11850 if (exec_control & CPU_BASED_TPR_SHADOW) {
11851 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, -1ull);
11852 vmcs_write32(TPR_THRESHOLD, vmcs12->tpr_threshold);
11854 #ifdef CONFIG_X86_64
11855 exec_control |= CPU_BASED_CR8_LOAD_EXITING |
11856 CPU_BASED_CR8_STORE_EXITING;
11861 * A vmexit (to either L1 hypervisor or L0 userspace) is always needed
11862 * for I/O port accesses.
11864 exec_control &= ~CPU_BASED_USE_IO_BITMAPS;
11865 exec_control |= CPU_BASED_UNCOND_IO_EXITING;
11867 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, exec_control);
11869 /* EXCEPTION_BITMAP and CR0_GUEST_HOST_MASK should basically be the
11870 * bitwise-or of what L1 wants to trap for L2, and what we want to
11871 * trap. Note that CR0.TS also needs updating - we do this later.
11873 update_exception_bitmap(vcpu);
11874 vcpu->arch.cr0_guest_owned_bits &= ~vmcs12->cr0_guest_host_mask;
11875 vmcs_writel(CR0_GUEST_HOST_MASK, ~vcpu->arch.cr0_guest_owned_bits);
11877 /* L2->L1 exit controls are emulated - the hardware exit is to L0 so
11878 * we should use its exit controls. Note that VM_EXIT_LOAD_IA32_EFER
11879 * bits are further modified by vmx_set_efer() below.
11881 vmcs_write32(VM_EXIT_CONTROLS, vmcs_config.vmexit_ctrl);
11883 /* vmcs12's VM_ENTRY_LOAD_IA32_EFER and VM_ENTRY_IA32E_MODE are
11884 * emulated by vmx_set_efer(), below.
11886 vm_entry_controls_init(vmx,
11887 (vmcs12->vm_entry_controls & ~VM_ENTRY_LOAD_IA32_EFER &
11888 ~VM_ENTRY_IA32E_MODE) |
11889 (vmcs_config.vmentry_ctrl & ~VM_ENTRY_IA32E_MODE));
11891 if (vmx->nested.nested_run_pending &&
11892 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_PAT)) {
11893 vmcs_write64(GUEST_IA32_PAT, vmcs12->guest_ia32_pat);
11894 vcpu->arch.pat = vmcs12->guest_ia32_pat;
11895 } else if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) {
11896 vmcs_write64(GUEST_IA32_PAT, vmx->vcpu.arch.pat);
11899 vmcs_write64(TSC_OFFSET, vcpu->arch.tsc_offset);
11901 if (kvm_has_tsc_control)
11902 decache_tsc_multiplier(vmx);
11906 * There is no direct mapping between vpid02 and vpid12, the
11907 * vpid02 is per-vCPU for L0 and reused while the value of
11908 * vpid12 is changed w/ one invvpid during nested vmentry.
11909 * The vpid12 is allocated by L1 for L2, so it will not
11910 * influence global bitmap(for vpid01 and vpid02 allocation)
11911 * even if spawn a lot of nested vCPUs.
11913 if (nested_cpu_has_vpid(vmcs12) && vmx->nested.vpid02) {
11914 if (vmcs12->virtual_processor_id != vmx->nested.last_vpid) {
11915 vmx->nested.last_vpid = vmcs12->virtual_processor_id;
11916 __vmx_flush_tlb(vcpu, vmx->nested.vpid02, true);
11919 vmx_flush_tlb(vcpu, true);
11925 * Conceptually we want to copy the PML address and index from
11926 * vmcs01 here, and then back to vmcs01 on nested vmexit. But,
11927 * since we always flush the log on each vmexit, this happens
11928 * to be equivalent to simply resetting the fields in vmcs02.
11930 ASSERT(vmx->pml_pg);
11931 vmcs_write64(PML_ADDRESS, page_to_phys(vmx->pml_pg));
11932 vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1);
11935 if (nested_cpu_has_ept(vmcs12)) {
11936 if (nested_ept_init_mmu_context(vcpu)) {
11937 *entry_failure_code = ENTRY_FAIL_DEFAULT;
11940 } else if (nested_cpu_has2(vmcs12,
11941 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) {
11942 vmx_flush_tlb(vcpu, true);
11946 * This sets GUEST_CR0 to vmcs12->guest_cr0, possibly modifying those
11947 * bits which we consider mandatory enabled.
11948 * The CR0_READ_SHADOW is what L2 should have expected to read given
11949 * the specifications by L1; It's not enough to take
11950 * vmcs12->cr0_read_shadow because on our cr0_guest_host_mask we we
11951 * have more bits than L1 expected.
11953 vmx_set_cr0(vcpu, vmcs12->guest_cr0);
11954 vmcs_writel(CR0_READ_SHADOW, nested_read_cr0(vmcs12));
11956 vmx_set_cr4(vcpu, vmcs12->guest_cr4);
11957 vmcs_writel(CR4_READ_SHADOW, nested_read_cr4(vmcs12));
11959 if (vmx->nested.nested_run_pending &&
11960 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_EFER))
11961 vcpu->arch.efer = vmcs12->guest_ia32_efer;
11962 else if (vmcs12->vm_entry_controls & VM_ENTRY_IA32E_MODE)
11963 vcpu->arch.efer |= (EFER_LMA | EFER_LME);
11965 vcpu->arch.efer &= ~(EFER_LMA | EFER_LME);
11966 /* Note: modifies VM_ENTRY/EXIT_CONTROLS and GUEST/HOST_IA32_EFER */
11967 vmx_set_efer(vcpu, vcpu->arch.efer);
11970 * Guest state is invalid and unrestricted guest is disabled,
11971 * which means L1 attempted VMEntry to L2 with invalid state.
11972 * Fail the VMEntry.
11974 if (vmx->emulation_required) {
11975 *entry_failure_code = ENTRY_FAIL_DEFAULT;
11979 /* Shadow page tables on either EPT or shadow page tables. */
11980 if (nested_vmx_load_cr3(vcpu, vmcs12->guest_cr3, nested_cpu_has_ept(vmcs12),
11981 entry_failure_code))
11985 vcpu->arch.walk_mmu->inject_page_fault = vmx_inject_page_fault_nested;
11987 kvm_register_write(vcpu, VCPU_REGS_RSP, vmcs12->guest_rsp);
11988 kvm_register_write(vcpu, VCPU_REGS_RIP, vmcs12->guest_rip);
11992 static int nested_vmx_check_nmi_controls(struct vmcs12 *vmcs12)
11994 if (!nested_cpu_has_nmi_exiting(vmcs12) &&
11995 nested_cpu_has_virtual_nmis(vmcs12))
11998 if (!nested_cpu_has_virtual_nmis(vmcs12) &&
11999 nested_cpu_has(vmcs12, CPU_BASED_VIRTUAL_NMI_PENDING))
12005 static int check_vmentry_prereqs(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
12007 struct vcpu_vmx *vmx = to_vmx(vcpu);
12009 if (vmcs12->guest_activity_state != GUEST_ACTIVITY_ACTIVE &&
12010 vmcs12->guest_activity_state != GUEST_ACTIVITY_HLT)
12011 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12013 if (nested_vmx_check_io_bitmap_controls(vcpu, vmcs12))
12014 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12016 if (nested_vmx_check_msr_bitmap_controls(vcpu, vmcs12))
12017 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12019 if (nested_vmx_check_apic_access_controls(vcpu, vmcs12))
12020 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12022 if (nested_vmx_check_tpr_shadow_controls(vcpu, vmcs12))
12023 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12025 if (nested_vmx_check_apicv_controls(vcpu, vmcs12))
12026 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12028 if (nested_vmx_check_msr_switch_controls(vcpu, vmcs12))
12029 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12031 if (nested_vmx_check_pml_controls(vcpu, vmcs12))
12032 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12034 if (nested_vmx_check_shadow_vmcs_controls(vcpu, vmcs12))
12035 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12037 if (!vmx_control_verify(vmcs12->cpu_based_vm_exec_control,
12038 vmx->nested.msrs.procbased_ctls_low,
12039 vmx->nested.msrs.procbased_ctls_high) ||
12040 (nested_cpu_has(vmcs12, CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) &&
12041 !vmx_control_verify(vmcs12->secondary_vm_exec_control,
12042 vmx->nested.msrs.secondary_ctls_low,
12043 vmx->nested.msrs.secondary_ctls_high)) ||
12044 !vmx_control_verify(vmcs12->pin_based_vm_exec_control,
12045 vmx->nested.msrs.pinbased_ctls_low,
12046 vmx->nested.msrs.pinbased_ctls_high) ||
12047 !vmx_control_verify(vmcs12->vm_exit_controls,
12048 vmx->nested.msrs.exit_ctls_low,
12049 vmx->nested.msrs.exit_ctls_high) ||
12050 !vmx_control_verify(vmcs12->vm_entry_controls,
12051 vmx->nested.msrs.entry_ctls_low,
12052 vmx->nested.msrs.entry_ctls_high))
12053 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12055 if (nested_vmx_check_nmi_controls(vmcs12))
12056 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12058 if (nested_cpu_has_vmfunc(vmcs12)) {
12059 if (vmcs12->vm_function_control &
12060 ~vmx->nested.msrs.vmfunc_controls)
12061 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12063 if (nested_cpu_has_eptp_switching(vmcs12)) {
12064 if (!nested_cpu_has_ept(vmcs12) ||
12065 !page_address_valid(vcpu, vmcs12->eptp_list_address))
12066 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12070 if (vmcs12->cr3_target_count > nested_cpu_vmx_misc_cr3_count(vcpu))
12071 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12073 if (!nested_host_cr0_valid(vcpu, vmcs12->host_cr0) ||
12074 !nested_host_cr4_valid(vcpu, vmcs12->host_cr4) ||
12075 !nested_cr3_valid(vcpu, vmcs12->host_cr3))
12076 return VMXERR_ENTRY_INVALID_HOST_STATE_FIELD;
12079 * From the Intel SDM, volume 3:
12080 * Fields relevant to VM-entry event injection must be set properly.
12081 * These fields are the VM-entry interruption-information field, the
12082 * VM-entry exception error code, and the VM-entry instruction length.
12084 if (vmcs12->vm_entry_intr_info_field & INTR_INFO_VALID_MASK) {
12085 u32 intr_info = vmcs12->vm_entry_intr_info_field;
12086 u8 vector = intr_info & INTR_INFO_VECTOR_MASK;
12087 u32 intr_type = intr_info & INTR_INFO_INTR_TYPE_MASK;
12088 bool has_error_code = intr_info & INTR_INFO_DELIVER_CODE_MASK;
12089 bool should_have_error_code;
12090 bool urg = nested_cpu_has2(vmcs12,
12091 SECONDARY_EXEC_UNRESTRICTED_GUEST);
12092 bool prot_mode = !urg || vmcs12->guest_cr0 & X86_CR0_PE;
12094 /* VM-entry interruption-info field: interruption type */
12095 if (intr_type == INTR_TYPE_RESERVED ||
12096 (intr_type == INTR_TYPE_OTHER_EVENT &&
12097 !nested_cpu_supports_monitor_trap_flag(vcpu)))
12098 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12100 /* VM-entry interruption-info field: vector */
12101 if ((intr_type == INTR_TYPE_NMI_INTR && vector != NMI_VECTOR) ||
12102 (intr_type == INTR_TYPE_HARD_EXCEPTION && vector > 31) ||
12103 (intr_type == INTR_TYPE_OTHER_EVENT && vector != 0))
12104 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12106 /* VM-entry interruption-info field: deliver error code */
12107 should_have_error_code =
12108 intr_type == INTR_TYPE_HARD_EXCEPTION && prot_mode &&
12109 x86_exception_has_error_code(vector);
12110 if (has_error_code != should_have_error_code)
12111 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12113 /* VM-entry exception error code */
12114 if (has_error_code &&
12115 vmcs12->vm_entry_exception_error_code & GENMASK(31, 15))
12116 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12118 /* VM-entry interruption-info field: reserved bits */
12119 if (intr_info & INTR_INFO_RESVD_BITS_MASK)
12120 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12122 /* VM-entry instruction length */
12123 switch (intr_type) {
12124 case INTR_TYPE_SOFT_EXCEPTION:
12125 case INTR_TYPE_SOFT_INTR:
12126 case INTR_TYPE_PRIV_SW_EXCEPTION:
12127 if ((vmcs12->vm_entry_instruction_len > 15) ||
12128 (vmcs12->vm_entry_instruction_len == 0 &&
12129 !nested_cpu_has_zero_length_injection(vcpu)))
12130 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12137 static int nested_vmx_check_vmcs_link_ptr(struct kvm_vcpu *vcpu,
12138 struct vmcs12 *vmcs12)
12142 struct vmcs12 *shadow;
12144 if (vmcs12->vmcs_link_pointer == -1ull)
12147 if (!page_address_valid(vcpu, vmcs12->vmcs_link_pointer))
12150 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->vmcs_link_pointer);
12151 if (is_error_page(page))
12155 shadow = kmap(page);
12156 if (shadow->hdr.revision_id != VMCS12_REVISION ||
12157 shadow->hdr.shadow_vmcs != nested_cpu_has_shadow_vmcs(vmcs12))
12160 kvm_release_page_clean(page);
12164 static int check_vmentry_postreqs(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12,
12169 *exit_qual = ENTRY_FAIL_DEFAULT;
12171 if (!nested_guest_cr0_valid(vcpu, vmcs12->guest_cr0) ||
12172 !nested_guest_cr4_valid(vcpu, vmcs12->guest_cr4))
12175 if (nested_vmx_check_vmcs_link_ptr(vcpu, vmcs12)) {
12176 *exit_qual = ENTRY_FAIL_VMCS_LINK_PTR;
12181 * If the load IA32_EFER VM-entry control is 1, the following checks
12182 * are performed on the field for the IA32_EFER MSR:
12183 * - Bits reserved in the IA32_EFER MSR must be 0.
12184 * - Bit 10 (corresponding to IA32_EFER.LMA) must equal the value of
12185 * the IA-32e mode guest VM-exit control. It must also be identical
12186 * to bit 8 (LME) if bit 31 in the CR0 field (corresponding to
12189 if (to_vmx(vcpu)->nested.nested_run_pending &&
12190 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_EFER)) {
12191 ia32e = (vmcs12->vm_entry_controls & VM_ENTRY_IA32E_MODE) != 0;
12192 if (!kvm_valid_efer(vcpu, vmcs12->guest_ia32_efer) ||
12193 ia32e != !!(vmcs12->guest_ia32_efer & EFER_LMA) ||
12194 ((vmcs12->guest_cr0 & X86_CR0_PG) &&
12195 ia32e != !!(vmcs12->guest_ia32_efer & EFER_LME)))
12200 * If the load IA32_EFER VM-exit control is 1, bits reserved in the
12201 * IA32_EFER MSR must be 0 in the field for that register. In addition,
12202 * the values of the LMA and LME bits in the field must each be that of
12203 * the host address-space size VM-exit control.
12205 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_EFER) {
12206 ia32e = (vmcs12->vm_exit_controls &
12207 VM_EXIT_HOST_ADDR_SPACE_SIZE) != 0;
12208 if (!kvm_valid_efer(vcpu, vmcs12->host_ia32_efer) ||
12209 ia32e != !!(vmcs12->host_ia32_efer & EFER_LMA) ||
12210 ia32e != !!(vmcs12->host_ia32_efer & EFER_LME))
12214 if ((vmcs12->vm_entry_controls & VM_ENTRY_LOAD_BNDCFGS) &&
12215 (is_noncanonical_address(vmcs12->guest_bndcfgs & PAGE_MASK, vcpu) ||
12216 (vmcs12->guest_bndcfgs & MSR_IA32_BNDCFGS_RSVD)))
12223 * If exit_qual is NULL, this is being called from state restore (either RSM
12224 * or KVM_SET_NESTED_STATE). Otherwise it's called from vmlaunch/vmresume.
12226 static int enter_vmx_non_root_mode(struct kvm_vcpu *vcpu, u32 *exit_qual)
12228 struct vcpu_vmx *vmx = to_vmx(vcpu);
12229 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
12230 bool from_vmentry = !!exit_qual;
12231 u32 dummy_exit_qual;
12234 enter_guest_mode(vcpu);
12236 if (!(vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS))
12237 vmx->nested.vmcs01_debugctl = vmcs_read64(GUEST_IA32_DEBUGCTL);
12239 vmx_switch_vmcs(vcpu, &vmx->nested.vmcs02);
12240 vmx_segment_cache_clear(vmx);
12242 if (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING)
12243 vcpu->arch.tsc_offset += vmcs12->tsc_offset;
12245 r = EXIT_REASON_INVALID_STATE;
12246 if (prepare_vmcs02(vcpu, vmcs12, from_vmentry ? exit_qual : &dummy_exit_qual))
12249 if (from_vmentry) {
12250 nested_get_vmcs12_pages(vcpu);
12252 r = EXIT_REASON_MSR_LOAD_FAIL;
12253 *exit_qual = nested_vmx_load_msr(vcpu,
12254 vmcs12->vm_entry_msr_load_addr,
12255 vmcs12->vm_entry_msr_load_count);
12260 * The MMU is not initialized to point at the right entities yet and
12261 * "get pages" would need to read data from the guest (i.e. we will
12262 * need to perform gpa to hpa translation). Request a call
12263 * to nested_get_vmcs12_pages before the next VM-entry. The MSRs
12264 * have already been set at vmentry time and should not be reset.
12266 kvm_make_request(KVM_REQ_GET_VMCS12_PAGES, vcpu);
12270 * Note no nested_vmx_succeed or nested_vmx_fail here. At this point
12271 * we are no longer running L1, and VMLAUNCH/VMRESUME has not yet
12272 * returned as far as L1 is concerned. It will only return (and set
12273 * the success flag) when L2 exits (see nested_vmx_vmexit()).
12278 if (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING)
12279 vcpu->arch.tsc_offset -= vmcs12->tsc_offset;
12280 leave_guest_mode(vcpu);
12281 vmx_switch_vmcs(vcpu, &vmx->vmcs01);
12286 * nested_vmx_run() handles a nested entry, i.e., a VMLAUNCH or VMRESUME on L1
12287 * for running an L2 nested guest.
12289 static int nested_vmx_run(struct kvm_vcpu *vcpu, bool launch)
12291 struct vmcs12 *vmcs12;
12292 struct vcpu_vmx *vmx = to_vmx(vcpu);
12293 u32 interrupt_shadow = vmx_get_interrupt_shadow(vcpu);
12297 if (!nested_vmx_check_permission(vcpu))
12300 if (!nested_vmx_check_vmcs12(vcpu))
12303 vmcs12 = get_vmcs12(vcpu);
12306 * Can't VMLAUNCH or VMRESUME a shadow VMCS. Despite the fact
12307 * that there *is* a valid VMCS pointer, RFLAGS.CF is set
12308 * rather than RFLAGS.ZF, and no error number is stored to the
12309 * VM-instruction error field.
12311 if (vmcs12->hdr.shadow_vmcs) {
12312 nested_vmx_failInvalid(vcpu);
12316 if (enable_shadow_vmcs)
12317 copy_shadow_to_vmcs12(vmx);
12320 * The nested entry process starts with enforcing various prerequisites
12321 * on vmcs12 as required by the Intel SDM, and act appropriately when
12322 * they fail: As the SDM explains, some conditions should cause the
12323 * instruction to fail, while others will cause the instruction to seem
12324 * to succeed, but return an EXIT_REASON_INVALID_STATE.
12325 * To speed up the normal (success) code path, we should avoid checking
12326 * for misconfigurations which will anyway be caught by the processor
12327 * when using the merged vmcs02.
12329 if (interrupt_shadow & KVM_X86_SHADOW_INT_MOV_SS) {
12330 nested_vmx_failValid(vcpu,
12331 VMXERR_ENTRY_EVENTS_BLOCKED_BY_MOV_SS);
12335 if (vmcs12->launch_state == launch) {
12336 nested_vmx_failValid(vcpu,
12337 launch ? VMXERR_VMLAUNCH_NONCLEAR_VMCS
12338 : VMXERR_VMRESUME_NONLAUNCHED_VMCS);
12342 ret = check_vmentry_prereqs(vcpu, vmcs12);
12344 nested_vmx_failValid(vcpu, ret);
12349 * After this point, the trap flag no longer triggers a singlestep trap
12350 * on the vm entry instructions; don't call kvm_skip_emulated_instruction.
12351 * This is not 100% correct; for performance reasons, we delegate most
12352 * of the checks on host state to the processor. If those fail,
12353 * the singlestep trap is missed.
12355 skip_emulated_instruction(vcpu);
12357 ret = check_vmentry_postreqs(vcpu, vmcs12, &exit_qual);
12359 nested_vmx_entry_failure(vcpu, vmcs12,
12360 EXIT_REASON_INVALID_STATE, exit_qual);
12365 * We're finally done with prerequisite checking, and can start with
12366 * the nested entry.
12369 vmx->nested.nested_run_pending = 1;
12370 ret = enter_vmx_non_root_mode(vcpu, &exit_qual);
12372 nested_vmx_entry_failure(vcpu, vmcs12, ret, exit_qual);
12373 vmx->nested.nested_run_pending = 0;
12378 * Must happen outside of enter_vmx_non_root_mode() as it will
12379 * also be used as part of restoring nVMX state for
12380 * snapshot restore (migration).
12382 * In this flow, it is assumed that vmcs12 cache was
12383 * trasferred as part of captured nVMX state and should
12384 * therefore not be read from guest memory (which may not
12385 * exist on destination host yet).
12387 nested_cache_shadow_vmcs12(vcpu, vmcs12);
12390 * If we're entering a halted L2 vcpu and the L2 vcpu won't be woken
12391 * by event injection, halt vcpu.
12393 if ((vmcs12->guest_activity_state == GUEST_ACTIVITY_HLT) &&
12394 !(vmcs12->vm_entry_intr_info_field & INTR_INFO_VALID_MASK)) {
12395 vmx->nested.nested_run_pending = 0;
12396 return kvm_vcpu_halt(vcpu);
12401 return kvm_skip_emulated_instruction(vcpu);
12405 * On a nested exit from L2 to L1, vmcs12.guest_cr0 might not be up-to-date
12406 * because L2 may have changed some cr0 bits directly (CRO_GUEST_HOST_MASK).
12407 * This function returns the new value we should put in vmcs12.guest_cr0.
12408 * It's not enough to just return the vmcs02 GUEST_CR0. Rather,
12409 * 1. Bits that neither L0 nor L1 trapped, were set directly by L2 and are now
12410 * available in vmcs02 GUEST_CR0. (Note: It's enough to check that L0
12411 * didn't trap the bit, because if L1 did, so would L0).
12412 * 2. Bits that L1 asked to trap (and therefore L0 also did) could not have
12413 * been modified by L2, and L1 knows it. So just leave the old value of
12414 * the bit from vmcs12.guest_cr0. Note that the bit from vmcs02 GUEST_CR0
12415 * isn't relevant, because if L0 traps this bit it can set it to anything.
12416 * 3. Bits that L1 didn't trap, but L0 did. L1 believes the guest could have
12417 * changed these bits, and therefore they need to be updated, but L0
12418 * didn't necessarily allow them to be changed in GUEST_CR0 - and rather
12419 * put them in vmcs02 CR0_READ_SHADOW. So take these bits from there.
12421 static inline unsigned long
12422 vmcs12_guest_cr0(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
12425 /*1*/ (vmcs_readl(GUEST_CR0) & vcpu->arch.cr0_guest_owned_bits) |
12426 /*2*/ (vmcs12->guest_cr0 & vmcs12->cr0_guest_host_mask) |
12427 /*3*/ (vmcs_readl(CR0_READ_SHADOW) & ~(vmcs12->cr0_guest_host_mask |
12428 vcpu->arch.cr0_guest_owned_bits));
12431 static inline unsigned long
12432 vmcs12_guest_cr4(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
12435 /*1*/ (vmcs_readl(GUEST_CR4) & vcpu->arch.cr4_guest_owned_bits) |
12436 /*2*/ (vmcs12->guest_cr4 & vmcs12->cr4_guest_host_mask) |
12437 /*3*/ (vmcs_readl(CR4_READ_SHADOW) & ~(vmcs12->cr4_guest_host_mask |
12438 vcpu->arch.cr4_guest_owned_bits));
12441 static void vmcs12_save_pending_event(struct kvm_vcpu *vcpu,
12442 struct vmcs12 *vmcs12)
12447 if (vcpu->arch.exception.injected) {
12448 nr = vcpu->arch.exception.nr;
12449 idt_vectoring = nr | VECTORING_INFO_VALID_MASK;
12451 if (kvm_exception_is_soft(nr)) {
12452 vmcs12->vm_exit_instruction_len =
12453 vcpu->arch.event_exit_inst_len;
12454 idt_vectoring |= INTR_TYPE_SOFT_EXCEPTION;
12456 idt_vectoring |= INTR_TYPE_HARD_EXCEPTION;
12458 if (vcpu->arch.exception.has_error_code) {
12459 idt_vectoring |= VECTORING_INFO_DELIVER_CODE_MASK;
12460 vmcs12->idt_vectoring_error_code =
12461 vcpu->arch.exception.error_code;
12464 vmcs12->idt_vectoring_info_field = idt_vectoring;
12465 } else if (vcpu->arch.nmi_injected) {
12466 vmcs12->idt_vectoring_info_field =
12467 INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK | NMI_VECTOR;
12468 } else if (vcpu->arch.interrupt.injected) {
12469 nr = vcpu->arch.interrupt.nr;
12470 idt_vectoring = nr | VECTORING_INFO_VALID_MASK;
12472 if (vcpu->arch.interrupt.soft) {
12473 idt_vectoring |= INTR_TYPE_SOFT_INTR;
12474 vmcs12->vm_entry_instruction_len =
12475 vcpu->arch.event_exit_inst_len;
12477 idt_vectoring |= INTR_TYPE_EXT_INTR;
12479 vmcs12->idt_vectoring_info_field = idt_vectoring;
12483 static int vmx_check_nested_events(struct kvm_vcpu *vcpu, bool external_intr)
12485 struct vcpu_vmx *vmx = to_vmx(vcpu);
12486 unsigned long exit_qual;
12487 bool block_nested_events =
12488 vmx->nested.nested_run_pending || kvm_event_needs_reinjection(vcpu);
12490 if (vcpu->arch.exception.pending &&
12491 nested_vmx_check_exception(vcpu, &exit_qual)) {
12492 if (block_nested_events)
12494 nested_vmx_inject_exception_vmexit(vcpu, exit_qual);
12498 if (nested_cpu_has_preemption_timer(get_vmcs12(vcpu)) &&
12499 vmx->nested.preemption_timer_expired) {
12500 if (block_nested_events)
12502 nested_vmx_vmexit(vcpu, EXIT_REASON_PREEMPTION_TIMER, 0, 0);
12506 if (vcpu->arch.nmi_pending && nested_exit_on_nmi(vcpu)) {
12507 if (block_nested_events)
12509 nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI,
12510 NMI_VECTOR | INTR_TYPE_NMI_INTR |
12511 INTR_INFO_VALID_MASK, 0);
12513 * The NMI-triggered VM exit counts as injection:
12514 * clear this one and block further NMIs.
12516 vcpu->arch.nmi_pending = 0;
12517 vmx_set_nmi_mask(vcpu, true);
12521 if ((kvm_cpu_has_interrupt(vcpu) || external_intr) &&
12522 nested_exit_on_intr(vcpu)) {
12523 if (block_nested_events)
12525 nested_vmx_vmexit(vcpu, EXIT_REASON_EXTERNAL_INTERRUPT, 0, 0);
12529 vmx_complete_nested_posted_interrupt(vcpu);
12533 static u32 vmx_get_preemption_timer_value(struct kvm_vcpu *vcpu)
12535 ktime_t remaining =
12536 hrtimer_get_remaining(&to_vmx(vcpu)->nested.preemption_timer);
12539 if (ktime_to_ns(remaining) <= 0)
12542 value = ktime_to_ns(remaining) * vcpu->arch.virtual_tsc_khz;
12543 do_div(value, 1000000);
12544 return value >> VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE;
12548 * Update the guest state fields of vmcs12 to reflect changes that
12549 * occurred while L2 was running. (The "IA-32e mode guest" bit of the
12550 * VM-entry controls is also updated, since this is really a guest
12553 static void sync_vmcs12(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
12555 vmcs12->guest_cr0 = vmcs12_guest_cr0(vcpu, vmcs12);
12556 vmcs12->guest_cr4 = vmcs12_guest_cr4(vcpu, vmcs12);
12558 vmcs12->guest_rsp = kvm_register_read(vcpu, VCPU_REGS_RSP);
12559 vmcs12->guest_rip = kvm_register_read(vcpu, VCPU_REGS_RIP);
12560 vmcs12->guest_rflags = vmcs_readl(GUEST_RFLAGS);
12562 vmcs12->guest_es_selector = vmcs_read16(GUEST_ES_SELECTOR);
12563 vmcs12->guest_cs_selector = vmcs_read16(GUEST_CS_SELECTOR);
12564 vmcs12->guest_ss_selector = vmcs_read16(GUEST_SS_SELECTOR);
12565 vmcs12->guest_ds_selector = vmcs_read16(GUEST_DS_SELECTOR);
12566 vmcs12->guest_fs_selector = vmcs_read16(GUEST_FS_SELECTOR);
12567 vmcs12->guest_gs_selector = vmcs_read16(GUEST_GS_SELECTOR);
12568 vmcs12->guest_ldtr_selector = vmcs_read16(GUEST_LDTR_SELECTOR);
12569 vmcs12->guest_tr_selector = vmcs_read16(GUEST_TR_SELECTOR);
12570 vmcs12->guest_es_limit = vmcs_read32(GUEST_ES_LIMIT);
12571 vmcs12->guest_cs_limit = vmcs_read32(GUEST_CS_LIMIT);
12572 vmcs12->guest_ss_limit = vmcs_read32(GUEST_SS_LIMIT);
12573 vmcs12->guest_ds_limit = vmcs_read32(GUEST_DS_LIMIT);
12574 vmcs12->guest_fs_limit = vmcs_read32(GUEST_FS_LIMIT);
12575 vmcs12->guest_gs_limit = vmcs_read32(GUEST_GS_LIMIT);
12576 vmcs12->guest_ldtr_limit = vmcs_read32(GUEST_LDTR_LIMIT);
12577 vmcs12->guest_tr_limit = vmcs_read32(GUEST_TR_LIMIT);
12578 vmcs12->guest_gdtr_limit = vmcs_read32(GUEST_GDTR_LIMIT);
12579 vmcs12->guest_idtr_limit = vmcs_read32(GUEST_IDTR_LIMIT);
12580 vmcs12->guest_es_ar_bytes = vmcs_read32(GUEST_ES_AR_BYTES);
12581 vmcs12->guest_cs_ar_bytes = vmcs_read32(GUEST_CS_AR_BYTES);
12582 vmcs12->guest_ss_ar_bytes = vmcs_read32(GUEST_SS_AR_BYTES);
12583 vmcs12->guest_ds_ar_bytes = vmcs_read32(GUEST_DS_AR_BYTES);
12584 vmcs12->guest_fs_ar_bytes = vmcs_read32(GUEST_FS_AR_BYTES);
12585 vmcs12->guest_gs_ar_bytes = vmcs_read32(GUEST_GS_AR_BYTES);
12586 vmcs12->guest_ldtr_ar_bytes = vmcs_read32(GUEST_LDTR_AR_BYTES);
12587 vmcs12->guest_tr_ar_bytes = vmcs_read32(GUEST_TR_AR_BYTES);
12588 vmcs12->guest_es_base = vmcs_readl(GUEST_ES_BASE);
12589 vmcs12->guest_cs_base = vmcs_readl(GUEST_CS_BASE);
12590 vmcs12->guest_ss_base = vmcs_readl(GUEST_SS_BASE);
12591 vmcs12->guest_ds_base = vmcs_readl(GUEST_DS_BASE);
12592 vmcs12->guest_fs_base = vmcs_readl(GUEST_FS_BASE);
12593 vmcs12->guest_gs_base = vmcs_readl(GUEST_GS_BASE);
12594 vmcs12->guest_ldtr_base = vmcs_readl(GUEST_LDTR_BASE);
12595 vmcs12->guest_tr_base = vmcs_readl(GUEST_TR_BASE);
12596 vmcs12->guest_gdtr_base = vmcs_readl(GUEST_GDTR_BASE);
12597 vmcs12->guest_idtr_base = vmcs_readl(GUEST_IDTR_BASE);
12599 vmcs12->guest_interruptibility_info =
12600 vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
12601 vmcs12->guest_pending_dbg_exceptions =
12602 vmcs_readl(GUEST_PENDING_DBG_EXCEPTIONS);
12603 if (vcpu->arch.mp_state == KVM_MP_STATE_HALTED)
12604 vmcs12->guest_activity_state = GUEST_ACTIVITY_HLT;
12606 vmcs12->guest_activity_state = GUEST_ACTIVITY_ACTIVE;
12608 if (nested_cpu_has_preemption_timer(vmcs12)) {
12609 if (vmcs12->vm_exit_controls &
12610 VM_EXIT_SAVE_VMX_PREEMPTION_TIMER)
12611 vmcs12->vmx_preemption_timer_value =
12612 vmx_get_preemption_timer_value(vcpu);
12613 hrtimer_cancel(&to_vmx(vcpu)->nested.preemption_timer);
12617 * In some cases (usually, nested EPT), L2 is allowed to change its
12618 * own CR3 without exiting. If it has changed it, we must keep it.
12619 * Of course, if L0 is using shadow page tables, GUEST_CR3 was defined
12620 * by L0, not L1 or L2, so we mustn't unconditionally copy it to vmcs12.
12622 * Additionally, restore L2's PDPTR to vmcs12.
12625 vmcs12->guest_cr3 = vmcs_readl(GUEST_CR3);
12626 vmcs12->guest_pdptr0 = vmcs_read64(GUEST_PDPTR0);
12627 vmcs12->guest_pdptr1 = vmcs_read64(GUEST_PDPTR1);
12628 vmcs12->guest_pdptr2 = vmcs_read64(GUEST_PDPTR2);
12629 vmcs12->guest_pdptr3 = vmcs_read64(GUEST_PDPTR3);
12632 vmcs12->guest_linear_address = vmcs_readl(GUEST_LINEAR_ADDRESS);
12634 if (nested_cpu_has_vid(vmcs12))
12635 vmcs12->guest_intr_status = vmcs_read16(GUEST_INTR_STATUS);
12637 vmcs12->vm_entry_controls =
12638 (vmcs12->vm_entry_controls & ~VM_ENTRY_IA32E_MODE) |
12639 (vm_entry_controls_get(to_vmx(vcpu)) & VM_ENTRY_IA32E_MODE);
12641 if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_DEBUG_CONTROLS) {
12642 kvm_get_dr(vcpu, 7, (unsigned long *)&vmcs12->guest_dr7);
12643 vmcs12->guest_ia32_debugctl = vmcs_read64(GUEST_IA32_DEBUGCTL);
12646 /* TODO: These cannot have changed unless we have MSR bitmaps and
12647 * the relevant bit asks not to trap the change */
12648 if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_IA32_PAT)
12649 vmcs12->guest_ia32_pat = vmcs_read64(GUEST_IA32_PAT);
12650 if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_IA32_EFER)
12651 vmcs12->guest_ia32_efer = vcpu->arch.efer;
12652 vmcs12->guest_sysenter_cs = vmcs_read32(GUEST_SYSENTER_CS);
12653 vmcs12->guest_sysenter_esp = vmcs_readl(GUEST_SYSENTER_ESP);
12654 vmcs12->guest_sysenter_eip = vmcs_readl(GUEST_SYSENTER_EIP);
12655 if (kvm_mpx_supported())
12656 vmcs12->guest_bndcfgs = vmcs_read64(GUEST_BNDCFGS);
12660 * prepare_vmcs12 is part of what we need to do when the nested L2 guest exits
12661 * and we want to prepare to run its L1 parent. L1 keeps a vmcs for L2 (vmcs12),
12662 * and this function updates it to reflect the changes to the guest state while
12663 * L2 was running (and perhaps made some exits which were handled directly by L0
12664 * without going back to L1), and to reflect the exit reason.
12665 * Note that we do not have to copy here all VMCS fields, just those that
12666 * could have changed by the L2 guest or the exit - i.e., the guest-state and
12667 * exit-information fields only. Other fields are modified by L1 with VMWRITE,
12668 * which already writes to vmcs12 directly.
12670 static void prepare_vmcs12(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12,
12671 u32 exit_reason, u32 exit_intr_info,
12672 unsigned long exit_qualification)
12674 /* update guest state fields: */
12675 sync_vmcs12(vcpu, vmcs12);
12677 /* update exit information fields: */
12679 vmcs12->vm_exit_reason = exit_reason;
12680 vmcs12->exit_qualification = exit_qualification;
12681 vmcs12->vm_exit_intr_info = exit_intr_info;
12683 vmcs12->idt_vectoring_info_field = 0;
12684 vmcs12->vm_exit_instruction_len = vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
12685 vmcs12->vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
12687 if (!(vmcs12->vm_exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY)) {
12688 vmcs12->launch_state = 1;
12690 /* vm_entry_intr_info_field is cleared on exit. Emulate this
12691 * instead of reading the real value. */
12692 vmcs12->vm_entry_intr_info_field &= ~INTR_INFO_VALID_MASK;
12695 * Transfer the event that L0 or L1 may wanted to inject into
12696 * L2 to IDT_VECTORING_INFO_FIELD.
12698 vmcs12_save_pending_event(vcpu, vmcs12);
12702 * Drop what we picked up for L2 via vmx_complete_interrupts. It is
12703 * preserved above and would only end up incorrectly in L1.
12705 vcpu->arch.nmi_injected = false;
12706 kvm_clear_exception_queue(vcpu);
12707 kvm_clear_interrupt_queue(vcpu);
12710 static void load_vmcs12_mmu_host_state(struct kvm_vcpu *vcpu,
12711 struct vmcs12 *vmcs12)
12713 u32 entry_failure_code;
12715 nested_ept_uninit_mmu_context(vcpu);
12718 * Only PDPTE load can fail as the value of cr3 was checked on entry and
12719 * couldn't have changed.
12721 if (nested_vmx_load_cr3(vcpu, vmcs12->host_cr3, false, &entry_failure_code))
12722 nested_vmx_abort(vcpu, VMX_ABORT_LOAD_HOST_PDPTE_FAIL);
12725 vcpu->arch.walk_mmu->inject_page_fault = kvm_inject_page_fault;
12729 * A part of what we need to when the nested L2 guest exits and we want to
12730 * run its L1 parent, is to reset L1's guest state to the host state specified
12732 * This function is to be called not only on normal nested exit, but also on
12733 * a nested entry failure, as explained in Intel's spec, 3B.23.7 ("VM-Entry
12734 * Failures During or After Loading Guest State").
12735 * This function should be called when the active VMCS is L1's (vmcs01).
12737 static void load_vmcs12_host_state(struct kvm_vcpu *vcpu,
12738 struct vmcs12 *vmcs12)
12740 struct kvm_segment seg;
12742 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_EFER)
12743 vcpu->arch.efer = vmcs12->host_ia32_efer;
12744 else if (vmcs12->vm_exit_controls & VM_EXIT_HOST_ADDR_SPACE_SIZE)
12745 vcpu->arch.efer |= (EFER_LMA | EFER_LME);
12747 vcpu->arch.efer &= ~(EFER_LMA | EFER_LME);
12748 vmx_set_efer(vcpu, vcpu->arch.efer);
12750 kvm_register_write(vcpu, VCPU_REGS_RSP, vmcs12->host_rsp);
12751 kvm_register_write(vcpu, VCPU_REGS_RIP, vmcs12->host_rip);
12752 vmx_set_rflags(vcpu, X86_EFLAGS_FIXED);
12754 * Note that calling vmx_set_cr0 is important, even if cr0 hasn't
12755 * actually changed, because vmx_set_cr0 refers to efer set above.
12757 * CR0_GUEST_HOST_MASK is already set in the original vmcs01
12758 * (KVM doesn't change it);
12760 vcpu->arch.cr0_guest_owned_bits = X86_CR0_TS;
12761 vmx_set_cr0(vcpu, vmcs12->host_cr0);
12763 /* Same as above - no reason to call set_cr4_guest_host_mask(). */
12764 vcpu->arch.cr4_guest_owned_bits = ~vmcs_readl(CR4_GUEST_HOST_MASK);
12765 vmx_set_cr4(vcpu, vmcs12->host_cr4);
12767 load_vmcs12_mmu_host_state(vcpu, vmcs12);
12770 * If vmcs01 don't use VPID, CPU flushes TLB on every
12771 * VMEntry/VMExit. Thus, no need to flush TLB.
12773 * If vmcs12 uses VPID, TLB entries populated by L2 are
12774 * tagged with vmx->nested.vpid02 while L1 entries are tagged
12775 * with vmx->vpid. Thus, no need to flush TLB.
12777 * Therefore, flush TLB only in case vmcs01 uses VPID and
12778 * vmcs12 don't use VPID as in this case L1 & L2 TLB entries
12779 * are both tagged with vmx->vpid.
12782 !(nested_cpu_has_vpid(vmcs12) && to_vmx(vcpu)->nested.vpid02)) {
12783 vmx_flush_tlb(vcpu, true);
12786 vmcs_write32(GUEST_SYSENTER_CS, vmcs12->host_ia32_sysenter_cs);
12787 vmcs_writel(GUEST_SYSENTER_ESP, vmcs12->host_ia32_sysenter_esp);
12788 vmcs_writel(GUEST_SYSENTER_EIP, vmcs12->host_ia32_sysenter_eip);
12789 vmcs_writel(GUEST_IDTR_BASE, vmcs12->host_idtr_base);
12790 vmcs_writel(GUEST_GDTR_BASE, vmcs12->host_gdtr_base);
12791 vmcs_write32(GUEST_IDTR_LIMIT, 0xFFFF);
12792 vmcs_write32(GUEST_GDTR_LIMIT, 0xFFFF);
12794 /* If not VM_EXIT_CLEAR_BNDCFGS, the L2 value propagates to L1. */
12795 if (vmcs12->vm_exit_controls & VM_EXIT_CLEAR_BNDCFGS)
12796 vmcs_write64(GUEST_BNDCFGS, 0);
12798 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PAT) {
12799 vmcs_write64(GUEST_IA32_PAT, vmcs12->host_ia32_pat);
12800 vcpu->arch.pat = vmcs12->host_ia32_pat;
12802 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL)
12803 vmcs_write64(GUEST_IA32_PERF_GLOBAL_CTRL,
12804 vmcs12->host_ia32_perf_global_ctrl);
12806 /* Set L1 segment info according to Intel SDM
12807 27.5.2 Loading Host Segment and Descriptor-Table Registers */
12808 seg = (struct kvm_segment) {
12810 .limit = 0xFFFFFFFF,
12811 .selector = vmcs12->host_cs_selector,
12817 if (vmcs12->vm_exit_controls & VM_EXIT_HOST_ADDR_SPACE_SIZE)
12821 vmx_set_segment(vcpu, &seg, VCPU_SREG_CS);
12822 seg = (struct kvm_segment) {
12824 .limit = 0xFFFFFFFF,
12831 seg.selector = vmcs12->host_ds_selector;
12832 vmx_set_segment(vcpu, &seg, VCPU_SREG_DS);
12833 seg.selector = vmcs12->host_es_selector;
12834 vmx_set_segment(vcpu, &seg, VCPU_SREG_ES);
12835 seg.selector = vmcs12->host_ss_selector;
12836 vmx_set_segment(vcpu, &seg, VCPU_SREG_SS);
12837 seg.selector = vmcs12->host_fs_selector;
12838 seg.base = vmcs12->host_fs_base;
12839 vmx_set_segment(vcpu, &seg, VCPU_SREG_FS);
12840 seg.selector = vmcs12->host_gs_selector;
12841 seg.base = vmcs12->host_gs_base;
12842 vmx_set_segment(vcpu, &seg, VCPU_SREG_GS);
12843 seg = (struct kvm_segment) {
12844 .base = vmcs12->host_tr_base,
12846 .selector = vmcs12->host_tr_selector,
12850 vmx_set_segment(vcpu, &seg, VCPU_SREG_TR);
12852 kvm_set_dr(vcpu, 7, 0x400);
12853 vmcs_write64(GUEST_IA32_DEBUGCTL, 0);
12855 if (cpu_has_vmx_msr_bitmap())
12856 vmx_update_msr_bitmap(vcpu);
12858 if (nested_vmx_load_msr(vcpu, vmcs12->vm_exit_msr_load_addr,
12859 vmcs12->vm_exit_msr_load_count))
12860 nested_vmx_abort(vcpu, VMX_ABORT_LOAD_HOST_MSR_FAIL);
12864 * Emulate an exit from nested guest (L2) to L1, i.e., prepare to run L1
12865 * and modify vmcs12 to make it see what it would expect to see there if
12866 * L2 was its real guest. Must only be called when in L2 (is_guest_mode())
12868 static void nested_vmx_vmexit(struct kvm_vcpu *vcpu, u32 exit_reason,
12869 u32 exit_intr_info,
12870 unsigned long exit_qualification)
12872 struct vcpu_vmx *vmx = to_vmx(vcpu);
12873 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
12875 /* trying to cancel vmlaunch/vmresume is a bug */
12876 WARN_ON_ONCE(vmx->nested.nested_run_pending);
12879 * The only expected VM-instruction error is "VM entry with
12880 * invalid control field(s)." Anything else indicates a
12883 WARN_ON_ONCE(vmx->fail && (vmcs_read32(VM_INSTRUCTION_ERROR) !=
12884 VMXERR_ENTRY_INVALID_CONTROL_FIELD));
12886 leave_guest_mode(vcpu);
12888 if (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING)
12889 vcpu->arch.tsc_offset -= vmcs12->tsc_offset;
12891 if (likely(!vmx->fail)) {
12892 if (exit_reason == -1)
12893 sync_vmcs12(vcpu, vmcs12);
12895 prepare_vmcs12(vcpu, vmcs12, exit_reason, exit_intr_info,
12896 exit_qualification);
12899 * Must happen outside of sync_vmcs12() as it will
12900 * also be used to capture vmcs12 cache as part of
12901 * capturing nVMX state for snapshot (migration).
12903 * Otherwise, this flush will dirty guest memory at a
12904 * point it is already assumed by user-space to be
12907 nested_flush_cached_shadow_vmcs12(vcpu, vmcs12);
12909 if (nested_vmx_store_msr(vcpu, vmcs12->vm_exit_msr_store_addr,
12910 vmcs12->vm_exit_msr_store_count))
12911 nested_vmx_abort(vcpu, VMX_ABORT_SAVE_GUEST_MSR_FAIL);
12914 vmx_switch_vmcs(vcpu, &vmx->vmcs01);
12915 vm_entry_controls_reset_shadow(vmx);
12916 vm_exit_controls_reset_shadow(vmx);
12917 vmx_segment_cache_clear(vmx);
12919 /* Update any VMCS fields that might have changed while L2 ran */
12920 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, vmx->msr_autoload.nr);
12921 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, vmx->msr_autoload.nr);
12922 vmcs_write64(TSC_OFFSET, vcpu->arch.tsc_offset);
12923 if (vmx->hv_deadline_tsc == -1)
12924 vmcs_clear_bits(PIN_BASED_VM_EXEC_CONTROL,
12925 PIN_BASED_VMX_PREEMPTION_TIMER);
12927 vmcs_set_bits(PIN_BASED_VM_EXEC_CONTROL,
12928 PIN_BASED_VMX_PREEMPTION_TIMER);
12929 if (kvm_has_tsc_control)
12930 decache_tsc_multiplier(vmx);
12932 if (vmx->nested.change_vmcs01_virtual_apic_mode) {
12933 vmx->nested.change_vmcs01_virtual_apic_mode = false;
12934 vmx_set_virtual_apic_mode(vcpu);
12935 } else if (!nested_cpu_has_ept(vmcs12) &&
12936 nested_cpu_has2(vmcs12,
12937 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) {
12938 vmx_flush_tlb(vcpu, true);
12941 /* This is needed for same reason as it was needed in prepare_vmcs02 */
12944 /* Unpin physical memory we referred to in vmcs02 */
12945 if (vmx->nested.apic_access_page) {
12946 kvm_release_page_dirty(vmx->nested.apic_access_page);
12947 vmx->nested.apic_access_page = NULL;
12949 if (vmx->nested.virtual_apic_page) {
12950 kvm_release_page_dirty(vmx->nested.virtual_apic_page);
12951 vmx->nested.virtual_apic_page = NULL;
12953 if (vmx->nested.pi_desc_page) {
12954 kunmap(vmx->nested.pi_desc_page);
12955 kvm_release_page_dirty(vmx->nested.pi_desc_page);
12956 vmx->nested.pi_desc_page = NULL;
12957 vmx->nested.pi_desc = NULL;
12961 * We are now running in L2, mmu_notifier will force to reload the
12962 * page's hpa for L2 vmcs. Need to reload it for L1 before entering L1.
12964 kvm_make_request(KVM_REQ_APIC_PAGE_RELOAD, vcpu);
12966 if (enable_shadow_vmcs && exit_reason != -1)
12967 vmx->nested.sync_shadow_vmcs = true;
12969 /* in case we halted in L2 */
12970 vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
12972 if (likely(!vmx->fail)) {
12974 * TODO: SDM says that with acknowledge interrupt on
12975 * exit, bit 31 of the VM-exit interrupt information
12976 * (valid interrupt) is always set to 1 on
12977 * EXIT_REASON_EXTERNAL_INTERRUPT, so we shouldn't
12978 * need kvm_cpu_has_interrupt(). See the commit
12979 * message for details.
12981 if (nested_exit_intr_ack_set(vcpu) &&
12982 exit_reason == EXIT_REASON_EXTERNAL_INTERRUPT &&
12983 kvm_cpu_has_interrupt(vcpu)) {
12984 int irq = kvm_cpu_get_interrupt(vcpu);
12986 vmcs12->vm_exit_intr_info = irq |
12987 INTR_INFO_VALID_MASK | INTR_TYPE_EXT_INTR;
12990 if (exit_reason != -1)
12991 trace_kvm_nested_vmexit_inject(vmcs12->vm_exit_reason,
12992 vmcs12->exit_qualification,
12993 vmcs12->idt_vectoring_info_field,
12994 vmcs12->vm_exit_intr_info,
12995 vmcs12->vm_exit_intr_error_code,
12998 load_vmcs12_host_state(vcpu, vmcs12);
13004 * After an early L2 VM-entry failure, we're now back
13005 * in L1 which thinks it just finished a VMLAUNCH or
13006 * VMRESUME instruction, so we need to set the failure
13007 * flag and the VM-instruction error field of the VMCS
13010 nested_vmx_failValid(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD);
13012 load_vmcs12_mmu_host_state(vcpu, vmcs12);
13015 * The emulated instruction was already skipped in
13016 * nested_vmx_run, but the updated RIP was never
13017 * written back to the vmcs01.
13019 skip_emulated_instruction(vcpu);
13024 * Forcibly leave nested mode in order to be able to reset the VCPU later on.
13026 static void vmx_leave_nested(struct kvm_vcpu *vcpu)
13028 if (is_guest_mode(vcpu)) {
13029 to_vmx(vcpu)->nested.nested_run_pending = 0;
13030 nested_vmx_vmexit(vcpu, -1, 0, 0);
13032 free_nested(to_vmx(vcpu));
13036 * L1's failure to enter L2 is a subset of a normal exit, as explained in
13037 * 23.7 "VM-entry failures during or after loading guest state" (this also
13038 * lists the acceptable exit-reason and exit-qualification parameters).
13039 * It should only be called before L2 actually succeeded to run, and when
13040 * vmcs01 is current (it doesn't leave_guest_mode() or switch vmcss).
13042 static void nested_vmx_entry_failure(struct kvm_vcpu *vcpu,
13043 struct vmcs12 *vmcs12,
13044 u32 reason, unsigned long qualification)
13046 load_vmcs12_host_state(vcpu, vmcs12);
13047 vmcs12->vm_exit_reason = reason | VMX_EXIT_REASONS_FAILED_VMENTRY;
13048 vmcs12->exit_qualification = qualification;
13049 nested_vmx_succeed(vcpu);
13050 if (enable_shadow_vmcs)
13051 to_vmx(vcpu)->nested.sync_shadow_vmcs = true;
13054 static int vmx_check_intercept(struct kvm_vcpu *vcpu,
13055 struct x86_instruction_info *info,
13056 enum x86_intercept_stage stage)
13058 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
13059 struct x86_emulate_ctxt *ctxt = &vcpu->arch.emulate_ctxt;
13062 * RDPID causes #UD if disabled through secondary execution controls.
13063 * Because it is marked as EmulateOnUD, we need to intercept it here.
13065 if (info->intercept == x86_intercept_rdtscp &&
13066 !nested_cpu_has2(vmcs12, SECONDARY_EXEC_RDTSCP)) {
13067 ctxt->exception.vector = UD_VECTOR;
13068 ctxt->exception.error_code_valid = false;
13069 return X86EMUL_PROPAGATE_FAULT;
13072 /* TODO: check more intercepts... */
13073 return X86EMUL_CONTINUE;
13076 #ifdef CONFIG_X86_64
13077 /* (a << shift) / divisor, return 1 if overflow otherwise 0 */
13078 static inline int u64_shl_div_u64(u64 a, unsigned int shift,
13079 u64 divisor, u64 *result)
13081 u64 low = a << shift, high = a >> (64 - shift);
13083 /* To avoid the overflow on divq */
13084 if (high >= divisor)
13087 /* Low hold the result, high hold rem which is discarded */
13088 asm("divq %2\n\t" : "=a" (low), "=d" (high) :
13089 "rm" (divisor), "0" (low), "1" (high));
13095 static int vmx_set_hv_timer(struct kvm_vcpu *vcpu, u64 guest_deadline_tsc)
13097 struct vcpu_vmx *vmx;
13098 u64 tscl, guest_tscl, delta_tsc, lapic_timer_advance_cycles;
13100 if (kvm_mwait_in_guest(vcpu->kvm))
13101 return -EOPNOTSUPP;
13103 vmx = to_vmx(vcpu);
13105 guest_tscl = kvm_read_l1_tsc(vcpu, tscl);
13106 delta_tsc = max(guest_deadline_tsc, guest_tscl) - guest_tscl;
13107 lapic_timer_advance_cycles = nsec_to_cycles(vcpu, lapic_timer_advance_ns);
13109 if (delta_tsc > lapic_timer_advance_cycles)
13110 delta_tsc -= lapic_timer_advance_cycles;
13114 /* Convert to host delta tsc if tsc scaling is enabled */
13115 if (vcpu->arch.tsc_scaling_ratio != kvm_default_tsc_scaling_ratio &&
13116 u64_shl_div_u64(delta_tsc,
13117 kvm_tsc_scaling_ratio_frac_bits,
13118 vcpu->arch.tsc_scaling_ratio,
13123 * If the delta tsc can't fit in the 32 bit after the multi shift,
13124 * we can't use the preemption timer.
13125 * It's possible that it fits on later vmentries, but checking
13126 * on every vmentry is costly so we just use an hrtimer.
13128 if (delta_tsc >> (cpu_preemption_timer_multi + 32))
13131 vmx->hv_deadline_tsc = tscl + delta_tsc;
13132 vmcs_set_bits(PIN_BASED_VM_EXEC_CONTROL,
13133 PIN_BASED_VMX_PREEMPTION_TIMER);
13135 return delta_tsc == 0;
13138 static void vmx_cancel_hv_timer(struct kvm_vcpu *vcpu)
13140 struct vcpu_vmx *vmx = to_vmx(vcpu);
13141 vmx->hv_deadline_tsc = -1;
13142 vmcs_clear_bits(PIN_BASED_VM_EXEC_CONTROL,
13143 PIN_BASED_VMX_PREEMPTION_TIMER);
13147 static void vmx_sched_in(struct kvm_vcpu *vcpu, int cpu)
13149 if (!kvm_pause_in_guest(vcpu->kvm))
13150 shrink_ple_window(vcpu);
13153 static void vmx_slot_enable_log_dirty(struct kvm *kvm,
13154 struct kvm_memory_slot *slot)
13156 kvm_mmu_slot_leaf_clear_dirty(kvm, slot);
13157 kvm_mmu_slot_largepage_remove_write_access(kvm, slot);
13160 static void vmx_slot_disable_log_dirty(struct kvm *kvm,
13161 struct kvm_memory_slot *slot)
13163 kvm_mmu_slot_set_dirty(kvm, slot);
13166 static void vmx_flush_log_dirty(struct kvm *kvm)
13168 kvm_flush_pml_buffers(kvm);
13171 static int vmx_write_pml_buffer(struct kvm_vcpu *vcpu)
13173 struct vmcs12 *vmcs12;
13174 struct vcpu_vmx *vmx = to_vmx(vcpu);
13176 struct page *page = NULL;
13179 if (is_guest_mode(vcpu)) {
13180 WARN_ON_ONCE(vmx->nested.pml_full);
13183 * Check if PML is enabled for the nested guest.
13184 * Whether eptp bit 6 is set is already checked
13185 * as part of A/D emulation.
13187 vmcs12 = get_vmcs12(vcpu);
13188 if (!nested_cpu_has_pml(vmcs12))
13191 if (vmcs12->guest_pml_index >= PML_ENTITY_NUM) {
13192 vmx->nested.pml_full = true;
13196 gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS) & ~0xFFFull;
13198 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->pml_address);
13199 if (is_error_page(page))
13202 pml_address = kmap(page);
13203 pml_address[vmcs12->guest_pml_index--] = gpa;
13205 kvm_release_page_clean(page);
13211 static void vmx_enable_log_dirty_pt_masked(struct kvm *kvm,
13212 struct kvm_memory_slot *memslot,
13213 gfn_t offset, unsigned long mask)
13215 kvm_mmu_clear_dirty_pt_masked(kvm, memslot, offset, mask);
13218 static void __pi_post_block(struct kvm_vcpu *vcpu)
13220 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
13221 struct pi_desc old, new;
13225 old.control = new.control = pi_desc->control;
13226 WARN(old.nv != POSTED_INTR_WAKEUP_VECTOR,
13227 "Wakeup handler not enabled while the VCPU is blocked\n");
13229 dest = cpu_physical_id(vcpu->cpu);
13231 if (x2apic_enabled())
13234 new.ndst = (dest << 8) & 0xFF00;
13236 /* set 'NV' to 'notification vector' */
13237 new.nv = POSTED_INTR_VECTOR;
13238 } while (cmpxchg64(&pi_desc->control, old.control,
13239 new.control) != old.control);
13241 if (!WARN_ON_ONCE(vcpu->pre_pcpu == -1)) {
13242 spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
13243 list_del(&vcpu->blocked_vcpu_list);
13244 spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
13245 vcpu->pre_pcpu = -1;
13250 * This routine does the following things for vCPU which is going
13251 * to be blocked if VT-d PI is enabled.
13252 * - Store the vCPU to the wakeup list, so when interrupts happen
13253 * we can find the right vCPU to wake up.
13254 * - Change the Posted-interrupt descriptor as below:
13255 * 'NDST' <-- vcpu->pre_pcpu
13256 * 'NV' <-- POSTED_INTR_WAKEUP_VECTOR
13257 * - If 'ON' is set during this process, which means at least one
13258 * interrupt is posted for this vCPU, we cannot block it, in
13259 * this case, return 1, otherwise, return 0.
13262 static int pi_pre_block(struct kvm_vcpu *vcpu)
13265 struct pi_desc old, new;
13266 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
13268 if (!kvm_arch_has_assigned_device(vcpu->kvm) ||
13269 !irq_remapping_cap(IRQ_POSTING_CAP) ||
13270 !kvm_vcpu_apicv_active(vcpu))
13273 WARN_ON(irqs_disabled());
13274 local_irq_disable();
13275 if (!WARN_ON_ONCE(vcpu->pre_pcpu != -1)) {
13276 vcpu->pre_pcpu = vcpu->cpu;
13277 spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
13278 list_add_tail(&vcpu->blocked_vcpu_list,
13279 &per_cpu(blocked_vcpu_on_cpu,
13281 spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
13285 old.control = new.control = pi_desc->control;
13287 WARN((pi_desc->sn == 1),
13288 "Warning: SN field of posted-interrupts "
13289 "is set before blocking\n");
13292 * Since vCPU can be preempted during this process,
13293 * vcpu->cpu could be different with pre_pcpu, we
13294 * need to set pre_pcpu as the destination of wakeup
13295 * notification event, then we can find the right vCPU
13296 * to wakeup in wakeup handler if interrupts happen
13297 * when the vCPU is in blocked state.
13299 dest = cpu_physical_id(vcpu->pre_pcpu);
13301 if (x2apic_enabled())
13304 new.ndst = (dest << 8) & 0xFF00;
13306 /* set 'NV' to 'wakeup vector' */
13307 new.nv = POSTED_INTR_WAKEUP_VECTOR;
13308 } while (cmpxchg64(&pi_desc->control, old.control,
13309 new.control) != old.control);
13311 /* We should not block the vCPU if an interrupt is posted for it. */
13312 if (pi_test_on(pi_desc) == 1)
13313 __pi_post_block(vcpu);
13315 local_irq_enable();
13316 return (vcpu->pre_pcpu == -1);
13319 static int vmx_pre_block(struct kvm_vcpu *vcpu)
13321 if (pi_pre_block(vcpu))
13324 if (kvm_lapic_hv_timer_in_use(vcpu))
13325 kvm_lapic_switch_to_sw_timer(vcpu);
13330 static void pi_post_block(struct kvm_vcpu *vcpu)
13332 if (vcpu->pre_pcpu == -1)
13335 WARN_ON(irqs_disabled());
13336 local_irq_disable();
13337 __pi_post_block(vcpu);
13338 local_irq_enable();
13341 static void vmx_post_block(struct kvm_vcpu *vcpu)
13343 if (kvm_x86_ops->set_hv_timer)
13344 kvm_lapic_switch_to_hv_timer(vcpu);
13346 pi_post_block(vcpu);
13350 * vmx_update_pi_irte - set IRTE for Posted-Interrupts
13353 * @host_irq: host irq of the interrupt
13354 * @guest_irq: gsi of the interrupt
13355 * @set: set or unset PI
13356 * returns 0 on success, < 0 on failure
13358 static int vmx_update_pi_irte(struct kvm *kvm, unsigned int host_irq,
13359 uint32_t guest_irq, bool set)
13361 struct kvm_kernel_irq_routing_entry *e;
13362 struct kvm_irq_routing_table *irq_rt;
13363 struct kvm_lapic_irq irq;
13364 struct kvm_vcpu *vcpu;
13365 struct vcpu_data vcpu_info;
13368 if (!kvm_arch_has_assigned_device(kvm) ||
13369 !irq_remapping_cap(IRQ_POSTING_CAP) ||
13370 !kvm_vcpu_apicv_active(kvm->vcpus[0]))
13373 idx = srcu_read_lock(&kvm->irq_srcu);
13374 irq_rt = srcu_dereference(kvm->irq_routing, &kvm->irq_srcu);
13375 if (guest_irq >= irq_rt->nr_rt_entries ||
13376 hlist_empty(&irq_rt->map[guest_irq])) {
13377 pr_warn_once("no route for guest_irq %u/%u (broken user space?)\n",
13378 guest_irq, irq_rt->nr_rt_entries);
13382 hlist_for_each_entry(e, &irq_rt->map[guest_irq], link) {
13383 if (e->type != KVM_IRQ_ROUTING_MSI)
13386 * VT-d PI cannot support posting multicast/broadcast
13387 * interrupts to a vCPU, we still use interrupt remapping
13388 * for these kind of interrupts.
13390 * For lowest-priority interrupts, we only support
13391 * those with single CPU as the destination, e.g. user
13392 * configures the interrupts via /proc/irq or uses
13393 * irqbalance to make the interrupts single-CPU.
13395 * We will support full lowest-priority interrupt later.
13398 kvm_set_msi_irq(kvm, e, &irq);
13399 if (!kvm_intr_is_single_vcpu(kvm, &irq, &vcpu)) {
13401 * Make sure the IRTE is in remapped mode if
13402 * we don't handle it in posted mode.
13404 ret = irq_set_vcpu_affinity(host_irq, NULL);
13407 "failed to back to remapped mode, irq: %u\n",
13415 vcpu_info.pi_desc_addr = __pa(vcpu_to_pi_desc(vcpu));
13416 vcpu_info.vector = irq.vector;
13418 trace_kvm_pi_irte_update(host_irq, vcpu->vcpu_id, e->gsi,
13419 vcpu_info.vector, vcpu_info.pi_desc_addr, set);
13422 ret = irq_set_vcpu_affinity(host_irq, &vcpu_info);
13424 ret = irq_set_vcpu_affinity(host_irq, NULL);
13427 printk(KERN_INFO "%s: failed to update PI IRTE\n",
13435 srcu_read_unlock(&kvm->irq_srcu, idx);
13439 static void vmx_setup_mce(struct kvm_vcpu *vcpu)
13441 if (vcpu->arch.mcg_cap & MCG_LMCE_P)
13442 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits |=
13443 FEATURE_CONTROL_LMCE;
13445 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits &=
13446 ~FEATURE_CONTROL_LMCE;
13449 static int vmx_smi_allowed(struct kvm_vcpu *vcpu)
13451 /* we need a nested vmexit to enter SMM, postpone if run is pending */
13452 if (to_vmx(vcpu)->nested.nested_run_pending)
13457 static int vmx_pre_enter_smm(struct kvm_vcpu *vcpu, char *smstate)
13459 struct vcpu_vmx *vmx = to_vmx(vcpu);
13461 vmx->nested.smm.guest_mode = is_guest_mode(vcpu);
13462 if (vmx->nested.smm.guest_mode)
13463 nested_vmx_vmexit(vcpu, -1, 0, 0);
13465 vmx->nested.smm.vmxon = vmx->nested.vmxon;
13466 vmx->nested.vmxon = false;
13467 vmx_clear_hlt(vcpu);
13471 static int vmx_pre_leave_smm(struct kvm_vcpu *vcpu, u64 smbase)
13473 struct vcpu_vmx *vmx = to_vmx(vcpu);
13476 if (vmx->nested.smm.vmxon) {
13477 vmx->nested.vmxon = true;
13478 vmx->nested.smm.vmxon = false;
13481 if (vmx->nested.smm.guest_mode) {
13482 vcpu->arch.hflags &= ~HF_SMM_MASK;
13483 ret = enter_vmx_non_root_mode(vcpu, NULL);
13484 vcpu->arch.hflags |= HF_SMM_MASK;
13488 vmx->nested.smm.guest_mode = false;
13493 static int enable_smi_window(struct kvm_vcpu *vcpu)
13498 static int vmx_get_nested_state(struct kvm_vcpu *vcpu,
13499 struct kvm_nested_state __user *user_kvm_nested_state,
13500 u32 user_data_size)
13502 struct vcpu_vmx *vmx;
13503 struct vmcs12 *vmcs12;
13504 struct kvm_nested_state kvm_state = {
13507 .size = sizeof(kvm_state),
13508 .vmx.vmxon_pa = -1ull,
13509 .vmx.vmcs_pa = -1ull,
13513 return kvm_state.size + 2 * VMCS12_SIZE;
13515 vmx = to_vmx(vcpu);
13516 vmcs12 = get_vmcs12(vcpu);
13517 if (nested_vmx_allowed(vcpu) &&
13518 (vmx->nested.vmxon || vmx->nested.smm.vmxon)) {
13519 kvm_state.vmx.vmxon_pa = vmx->nested.vmxon_ptr;
13520 kvm_state.vmx.vmcs_pa = vmx->nested.current_vmptr;
13522 if (vmx->nested.current_vmptr != -1ull) {
13523 kvm_state.size += VMCS12_SIZE;
13525 if (is_guest_mode(vcpu) &&
13526 nested_cpu_has_shadow_vmcs(vmcs12) &&
13527 vmcs12->vmcs_link_pointer != -1ull)
13528 kvm_state.size += VMCS12_SIZE;
13531 if (vmx->nested.smm.vmxon)
13532 kvm_state.vmx.smm.flags |= KVM_STATE_NESTED_SMM_VMXON;
13534 if (vmx->nested.smm.guest_mode)
13535 kvm_state.vmx.smm.flags |= KVM_STATE_NESTED_SMM_GUEST_MODE;
13537 if (is_guest_mode(vcpu)) {
13538 kvm_state.flags |= KVM_STATE_NESTED_GUEST_MODE;
13540 if (vmx->nested.nested_run_pending)
13541 kvm_state.flags |= KVM_STATE_NESTED_RUN_PENDING;
13545 if (user_data_size < kvm_state.size)
13548 if (copy_to_user(user_kvm_nested_state, &kvm_state, sizeof(kvm_state)))
13551 if (vmx->nested.current_vmptr == -1ull)
13555 * When running L2, the authoritative vmcs12 state is in the
13556 * vmcs02. When running L1, the authoritative vmcs12 state is
13557 * in the shadow vmcs linked to vmcs01, unless
13558 * sync_shadow_vmcs is set, in which case, the authoritative
13559 * vmcs12 state is in the vmcs12 already.
13561 if (is_guest_mode(vcpu))
13562 sync_vmcs12(vcpu, vmcs12);
13563 else if (enable_shadow_vmcs && !vmx->nested.sync_shadow_vmcs)
13564 copy_shadow_to_vmcs12(vmx);
13566 if (copy_to_user(user_kvm_nested_state->data, vmcs12, sizeof(*vmcs12)))
13569 if (nested_cpu_has_shadow_vmcs(vmcs12) &&
13570 vmcs12->vmcs_link_pointer != -1ull) {
13571 if (copy_to_user(user_kvm_nested_state->data + VMCS12_SIZE,
13572 get_shadow_vmcs12(vcpu), sizeof(*vmcs12)))
13577 return kvm_state.size;
13580 static int vmx_set_nested_state(struct kvm_vcpu *vcpu,
13581 struct kvm_nested_state __user *user_kvm_nested_state,
13582 struct kvm_nested_state *kvm_state)
13584 struct vcpu_vmx *vmx = to_vmx(vcpu);
13585 struct vmcs12 *vmcs12;
13589 if (kvm_state->format != 0)
13592 if (!nested_vmx_allowed(vcpu))
13593 return kvm_state->vmx.vmxon_pa == -1ull ? 0 : -EINVAL;
13595 if (kvm_state->vmx.vmxon_pa == -1ull) {
13596 if (kvm_state->vmx.smm.flags)
13599 if (kvm_state->vmx.vmcs_pa != -1ull)
13602 vmx_leave_nested(vcpu);
13606 if (!page_address_valid(vcpu, kvm_state->vmx.vmxon_pa))
13609 if (kvm_state->size < sizeof(kvm_state) + sizeof(*vmcs12))
13612 if (kvm_state->vmx.vmcs_pa == kvm_state->vmx.vmxon_pa ||
13613 !page_address_valid(vcpu, kvm_state->vmx.vmcs_pa))
13616 if ((kvm_state->vmx.smm.flags & KVM_STATE_NESTED_SMM_GUEST_MODE) &&
13617 (kvm_state->flags & KVM_STATE_NESTED_GUEST_MODE))
13620 if (kvm_state->vmx.smm.flags &
13621 ~(KVM_STATE_NESTED_SMM_GUEST_MODE | KVM_STATE_NESTED_SMM_VMXON))
13624 if ((kvm_state->vmx.smm.flags & KVM_STATE_NESTED_SMM_GUEST_MODE) &&
13625 !(kvm_state->vmx.smm.flags & KVM_STATE_NESTED_SMM_VMXON))
13628 vmx_leave_nested(vcpu);
13629 if (kvm_state->vmx.vmxon_pa == -1ull)
13632 vmx->nested.vmxon_ptr = kvm_state->vmx.vmxon_pa;
13633 ret = enter_vmx_operation(vcpu);
13637 set_current_vmptr(vmx, kvm_state->vmx.vmcs_pa);
13639 if (kvm_state->vmx.smm.flags & KVM_STATE_NESTED_SMM_VMXON) {
13640 vmx->nested.smm.vmxon = true;
13641 vmx->nested.vmxon = false;
13643 if (kvm_state->vmx.smm.flags & KVM_STATE_NESTED_SMM_GUEST_MODE)
13644 vmx->nested.smm.guest_mode = true;
13647 vmcs12 = get_vmcs12(vcpu);
13648 if (copy_from_user(vmcs12, user_kvm_nested_state->data, sizeof(*vmcs12)))
13651 if (vmcs12->hdr.revision_id != VMCS12_REVISION)
13654 if (!(kvm_state->flags & KVM_STATE_NESTED_GUEST_MODE))
13657 vmx->nested.nested_run_pending =
13658 !!(kvm_state->flags & KVM_STATE_NESTED_RUN_PENDING);
13660 if (nested_cpu_has_shadow_vmcs(vmcs12) &&
13661 vmcs12->vmcs_link_pointer != -1ull) {
13662 struct vmcs12 *shadow_vmcs12 = get_shadow_vmcs12(vcpu);
13663 if (kvm_state->size < sizeof(kvm_state) + 2 * sizeof(*vmcs12))
13666 if (copy_from_user(shadow_vmcs12,
13667 user_kvm_nested_state->data + VMCS12_SIZE,
13671 if (shadow_vmcs12->hdr.revision_id != VMCS12_REVISION ||
13672 !shadow_vmcs12->hdr.shadow_vmcs)
13676 if (check_vmentry_prereqs(vcpu, vmcs12) ||
13677 check_vmentry_postreqs(vcpu, vmcs12, &exit_qual))
13680 if (kvm_state->flags & KVM_STATE_NESTED_RUN_PENDING)
13681 vmx->nested.nested_run_pending = 1;
13683 vmx->nested.dirty_vmcs12 = true;
13684 ret = enter_vmx_non_root_mode(vcpu, NULL);
13691 static struct kvm_x86_ops vmx_x86_ops __ro_after_init = {
13692 .cpu_has_kvm_support = cpu_has_kvm_support,
13693 .disabled_by_bios = vmx_disabled_by_bios,
13694 .hardware_setup = hardware_setup,
13695 .hardware_unsetup = hardware_unsetup,
13696 .check_processor_compatibility = vmx_check_processor_compat,
13697 .hardware_enable = hardware_enable,
13698 .hardware_disable = hardware_disable,
13699 .cpu_has_accelerated_tpr = report_flexpriority,
13700 .has_emulated_msr = vmx_has_emulated_msr,
13702 .vm_init = vmx_vm_init,
13703 .vm_alloc = vmx_vm_alloc,
13704 .vm_free = vmx_vm_free,
13706 .vcpu_create = vmx_create_vcpu,
13707 .vcpu_free = vmx_free_vcpu,
13708 .vcpu_reset = vmx_vcpu_reset,
13710 .prepare_guest_switch = vmx_save_host_state,
13711 .vcpu_load = vmx_vcpu_load,
13712 .vcpu_put = vmx_vcpu_put,
13714 .update_bp_intercept = update_exception_bitmap,
13715 .get_msr_feature = vmx_get_msr_feature,
13716 .get_msr = vmx_get_msr,
13717 .set_msr = vmx_set_msr,
13718 .get_segment_base = vmx_get_segment_base,
13719 .get_segment = vmx_get_segment,
13720 .set_segment = vmx_set_segment,
13721 .get_cpl = vmx_get_cpl,
13722 .get_cs_db_l_bits = vmx_get_cs_db_l_bits,
13723 .decache_cr0_guest_bits = vmx_decache_cr0_guest_bits,
13724 .decache_cr3 = vmx_decache_cr3,
13725 .decache_cr4_guest_bits = vmx_decache_cr4_guest_bits,
13726 .set_cr0 = vmx_set_cr0,
13727 .set_cr3 = vmx_set_cr3,
13728 .set_cr4 = vmx_set_cr4,
13729 .set_efer = vmx_set_efer,
13730 .get_idt = vmx_get_idt,
13731 .set_idt = vmx_set_idt,
13732 .get_gdt = vmx_get_gdt,
13733 .set_gdt = vmx_set_gdt,
13734 .get_dr6 = vmx_get_dr6,
13735 .set_dr6 = vmx_set_dr6,
13736 .set_dr7 = vmx_set_dr7,
13737 .sync_dirty_debug_regs = vmx_sync_dirty_debug_regs,
13738 .cache_reg = vmx_cache_reg,
13739 .get_rflags = vmx_get_rflags,
13740 .set_rflags = vmx_set_rflags,
13742 .tlb_flush = vmx_flush_tlb,
13743 .tlb_flush_gva = vmx_flush_tlb_gva,
13745 .run = vmx_vcpu_run,
13746 .handle_exit = vmx_handle_exit,
13747 .skip_emulated_instruction = skip_emulated_instruction,
13748 .set_interrupt_shadow = vmx_set_interrupt_shadow,
13749 .get_interrupt_shadow = vmx_get_interrupt_shadow,
13750 .patch_hypercall = vmx_patch_hypercall,
13751 .set_irq = vmx_inject_irq,
13752 .set_nmi = vmx_inject_nmi,
13753 .queue_exception = vmx_queue_exception,
13754 .cancel_injection = vmx_cancel_injection,
13755 .interrupt_allowed = vmx_interrupt_allowed,
13756 .nmi_allowed = vmx_nmi_allowed,
13757 .get_nmi_mask = vmx_get_nmi_mask,
13758 .set_nmi_mask = vmx_set_nmi_mask,
13759 .enable_nmi_window = enable_nmi_window,
13760 .enable_irq_window = enable_irq_window,
13761 .update_cr8_intercept = update_cr8_intercept,
13762 .set_virtual_apic_mode = vmx_set_virtual_apic_mode,
13763 .set_apic_access_page_addr = vmx_set_apic_access_page_addr,
13764 .get_enable_apicv = vmx_get_enable_apicv,
13765 .refresh_apicv_exec_ctrl = vmx_refresh_apicv_exec_ctrl,
13766 .load_eoi_exitmap = vmx_load_eoi_exitmap,
13767 .apicv_post_state_restore = vmx_apicv_post_state_restore,
13768 .hwapic_irr_update = vmx_hwapic_irr_update,
13769 .hwapic_isr_update = vmx_hwapic_isr_update,
13770 .sync_pir_to_irr = vmx_sync_pir_to_irr,
13771 .deliver_posted_interrupt = vmx_deliver_posted_interrupt,
13773 .set_tss_addr = vmx_set_tss_addr,
13774 .set_identity_map_addr = vmx_set_identity_map_addr,
13775 .get_tdp_level = get_ept_level,
13776 .get_mt_mask = vmx_get_mt_mask,
13778 .get_exit_info = vmx_get_exit_info,
13780 .get_lpage_level = vmx_get_lpage_level,
13782 .cpuid_update = vmx_cpuid_update,
13784 .rdtscp_supported = vmx_rdtscp_supported,
13785 .invpcid_supported = vmx_invpcid_supported,
13787 .set_supported_cpuid = vmx_set_supported_cpuid,
13789 .has_wbinvd_exit = cpu_has_vmx_wbinvd_exit,
13791 .read_l1_tsc_offset = vmx_read_l1_tsc_offset,
13792 .write_tsc_offset = vmx_write_tsc_offset,
13794 .set_tdp_cr3 = vmx_set_cr3,
13796 .check_intercept = vmx_check_intercept,
13797 .handle_external_intr = vmx_handle_external_intr,
13798 .mpx_supported = vmx_mpx_supported,
13799 .xsaves_supported = vmx_xsaves_supported,
13800 .umip_emulated = vmx_umip_emulated,
13802 .check_nested_events = vmx_check_nested_events,
13804 .sched_in = vmx_sched_in,
13806 .slot_enable_log_dirty = vmx_slot_enable_log_dirty,
13807 .slot_disable_log_dirty = vmx_slot_disable_log_dirty,
13808 .flush_log_dirty = vmx_flush_log_dirty,
13809 .enable_log_dirty_pt_masked = vmx_enable_log_dirty_pt_masked,
13810 .write_log_dirty = vmx_write_pml_buffer,
13812 .pre_block = vmx_pre_block,
13813 .post_block = vmx_post_block,
13815 .pmu_ops = &intel_pmu_ops,
13817 .update_pi_irte = vmx_update_pi_irte,
13819 #ifdef CONFIG_X86_64
13820 .set_hv_timer = vmx_set_hv_timer,
13821 .cancel_hv_timer = vmx_cancel_hv_timer,
13824 .setup_mce = vmx_setup_mce,
13826 .get_nested_state = vmx_get_nested_state,
13827 .set_nested_state = vmx_set_nested_state,
13828 .get_vmcs12_pages = nested_get_vmcs12_pages,
13830 .smi_allowed = vmx_smi_allowed,
13831 .pre_enter_smm = vmx_pre_enter_smm,
13832 .pre_leave_smm = vmx_pre_leave_smm,
13833 .enable_smi_window = enable_smi_window,
13836 static int __init vmx_init(void)
13840 #if IS_ENABLED(CONFIG_HYPERV)
13842 * Enlightened VMCS usage should be recommended and the host needs
13843 * to support eVMCS v1 or above. We can also disable eVMCS support
13844 * with module parameter.
13846 if (enlightened_vmcs &&
13847 ms_hyperv.hints & HV_X64_ENLIGHTENED_VMCS_RECOMMENDED &&
13848 (ms_hyperv.nested_features & HV_X64_ENLIGHTENED_VMCS_VERSION) >=
13849 KVM_EVMCS_VERSION) {
13852 /* Check that we have assist pages on all online CPUs */
13853 for_each_online_cpu(cpu) {
13854 if (!hv_get_vp_assist_page(cpu)) {
13855 enlightened_vmcs = false;
13860 if (enlightened_vmcs) {
13861 pr_info("KVM: vmx: using Hyper-V Enlightened VMCS\n");
13862 static_branch_enable(&enable_evmcs);
13865 enlightened_vmcs = false;
13869 r = kvm_init(&vmx_x86_ops, sizeof(struct vcpu_vmx),
13870 __alignof__(struct vcpu_vmx), THIS_MODULE);
13874 #ifdef CONFIG_KEXEC_CORE
13875 rcu_assign_pointer(crash_vmclear_loaded_vmcss,
13876 crash_vmclear_local_loaded_vmcss);
13878 vmx_check_vmcs12_offsets();
13883 static void __exit vmx_exit(void)
13885 #ifdef CONFIG_KEXEC_CORE
13886 RCU_INIT_POINTER(crash_vmclear_loaded_vmcss, NULL);
13892 #if IS_ENABLED(CONFIG_HYPERV)
13893 if (static_branch_unlikely(&enable_evmcs)) {
13895 struct hv_vp_assist_page *vp_ap;
13897 * Reset everything to support using non-enlightened VMCS
13898 * access later (e.g. when we reload the module with
13899 * enlightened_vmcs=0)
13901 for_each_online_cpu(cpu) {
13902 vp_ap = hv_get_vp_assist_page(cpu);
13907 vp_ap->current_nested_vmcs = 0;
13908 vp_ap->enlighten_vmentry = 0;
13911 static_branch_disable(&enable_evmcs);
13916 module_init(vmx_init)
13917 module_exit(vmx_exit)