2 * Kernel-based Virtual Machine driver for Linux
4 * This module enables machines with Intel VT-x extensions to run virtual
5 * machines without emulation or binary translation.
7 * Copyright (C) 2006 Qumranet, Inc.
8 * Copyright 2010 Red Hat, Inc. and/or its affiliates.
11 * Avi Kivity <avi@qumranet.com>
12 * Yaniv Kamay <yaniv@qumranet.com>
14 * This work is licensed under the terms of the GNU GPL, version 2. See
15 * the COPYING file in the top-level directory.
23 #include <linux/kvm_host.h>
24 #include <linux/module.h>
25 #include <linux/kernel.h>
27 #include <linux/highmem.h>
28 #include <linux/sched.h>
29 #include <linux/moduleparam.h>
30 #include <linux/mod_devicetable.h>
31 #include <linux/ftrace_event.h>
32 #include <linux/slab.h>
33 #include <linux/tboot.h>
34 #include "kvm_cache_regs.h"
40 #include <asm/virtext.h>
44 #include <asm/perf_event.h>
45 #include <asm/kexec.h>
49 #define __ex(x) __kvm_handle_fault_on_reboot(x)
50 #define __ex_clear(x, reg) \
51 ____kvm_handle_fault_on_reboot(x, "xor " reg " , " reg)
53 MODULE_AUTHOR("Qumranet");
54 MODULE_LICENSE("GPL");
56 static const struct x86_cpu_id vmx_cpu_id[] = {
57 X86_FEATURE_MATCH(X86_FEATURE_VMX),
60 MODULE_DEVICE_TABLE(x86cpu, vmx_cpu_id);
62 static bool __read_mostly enable_vpid = 1;
63 module_param_named(vpid, enable_vpid, bool, 0444);
65 static bool __read_mostly flexpriority_enabled = 1;
66 module_param_named(flexpriority, flexpriority_enabled, bool, S_IRUGO);
68 static bool __read_mostly enable_ept = 1;
69 module_param_named(ept, enable_ept, bool, S_IRUGO);
71 static bool __read_mostly enable_unrestricted_guest = 1;
72 module_param_named(unrestricted_guest,
73 enable_unrestricted_guest, bool, S_IRUGO);
75 static bool __read_mostly enable_ept_ad_bits = 1;
76 module_param_named(eptad, enable_ept_ad_bits, bool, S_IRUGO);
78 static bool __read_mostly emulate_invalid_guest_state = true;
79 module_param(emulate_invalid_guest_state, bool, S_IRUGO);
81 static bool __read_mostly vmm_exclusive = 1;
82 module_param(vmm_exclusive, bool, S_IRUGO);
84 static bool __read_mostly fasteoi = 1;
85 module_param(fasteoi, bool, S_IRUGO);
88 * If nested=1, nested virtualization is supported, i.e., guests may use
89 * VMX and be a hypervisor for its own guests. If nested=0, guests may not
90 * use VMX instructions.
92 static bool __read_mostly nested = 0;
93 module_param(nested, bool, S_IRUGO);
95 #define KVM_GUEST_CR0_MASK_UNRESTRICTED_GUEST \
96 (X86_CR0_WP | X86_CR0_NE | X86_CR0_NW | X86_CR0_CD)
97 #define KVM_GUEST_CR0_MASK \
98 (KVM_GUEST_CR0_MASK_UNRESTRICTED_GUEST | X86_CR0_PG | X86_CR0_PE)
99 #define KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST \
100 (X86_CR0_WP | X86_CR0_NE)
101 #define KVM_VM_CR0_ALWAYS_ON \
102 (KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST | X86_CR0_PG | X86_CR0_PE)
103 #define KVM_CR4_GUEST_OWNED_BITS \
104 (X86_CR4_PVI | X86_CR4_DE | X86_CR4_PCE | X86_CR4_OSFXSR \
105 | X86_CR4_OSXMMEXCPT)
107 #define KVM_PMODE_VM_CR4_ALWAYS_ON (X86_CR4_PAE | X86_CR4_VMXE)
108 #define KVM_RMODE_VM_CR4_ALWAYS_ON (X86_CR4_VME | X86_CR4_PAE | X86_CR4_VMXE)
110 #define RMODE_GUEST_OWNED_EFLAGS_BITS (~(X86_EFLAGS_IOPL | X86_EFLAGS_VM))
113 * These 2 parameters are used to config the controls for Pause-Loop Exiting:
114 * ple_gap: upper bound on the amount of time between two successive
115 * executions of PAUSE in a loop. Also indicate if ple enabled.
116 * According to test, this time is usually smaller than 128 cycles.
117 * ple_window: upper bound on the amount of time a guest is allowed to execute
118 * in a PAUSE loop. Tests indicate that most spinlocks are held for
119 * less than 2^12 cycles
120 * Time is measured based on a counter that runs at the same rate as the TSC,
121 * refer SDM volume 3b section 21.6.13 & 22.1.3.
123 #define KVM_VMX_DEFAULT_PLE_GAP 128
124 #define KVM_VMX_DEFAULT_PLE_WINDOW 4096
125 static int ple_gap = KVM_VMX_DEFAULT_PLE_GAP;
126 module_param(ple_gap, int, S_IRUGO);
128 static int ple_window = KVM_VMX_DEFAULT_PLE_WINDOW;
129 module_param(ple_window, int, S_IRUGO);
131 extern const ulong vmx_return;
133 #define NR_AUTOLOAD_MSRS 8
134 #define VMCS02_POOL_SIZE 1
143 * Track a VMCS that may be loaded on a certain CPU. If it is (cpu!=-1), also
144 * remember whether it was VMLAUNCHed, and maintain a linked list of all VMCSs
145 * loaded on this CPU (so we can clear them if the CPU goes down).
151 struct list_head loaded_vmcss_on_cpu_link;
154 struct shared_msr_entry {
161 * struct vmcs12 describes the state that our guest hypervisor (L1) keeps for a
162 * single nested guest (L2), hence the name vmcs12. Any VMX implementation has
163 * a VMCS structure, and vmcs12 is our emulated VMX's VMCS. This structure is
164 * stored in guest memory specified by VMPTRLD, but is opaque to the guest,
165 * which must access it using VMREAD/VMWRITE/VMCLEAR instructions.
166 * More than one of these structures may exist, if L1 runs multiple L2 guests.
167 * nested_vmx_run() will use the data here to build a vmcs02: a VMCS for the
168 * underlying hardware which will be used to run L2.
169 * This structure is packed to ensure that its layout is identical across
170 * machines (necessary for live migration).
171 * If there are changes in this struct, VMCS12_REVISION must be changed.
173 typedef u64 natural_width;
174 struct __packed vmcs12 {
175 /* According to the Intel spec, a VMCS region must start with the
176 * following two fields. Then follow implementation-specific data.
181 u32 launch_state; /* set to 0 by VMCLEAR, to 1 by VMLAUNCH */
182 u32 padding[7]; /* room for future expansion */
187 u64 vm_exit_msr_store_addr;
188 u64 vm_exit_msr_load_addr;
189 u64 vm_entry_msr_load_addr;
191 u64 virtual_apic_page_addr;
192 u64 apic_access_addr;
194 u64 guest_physical_address;
195 u64 vmcs_link_pointer;
196 u64 guest_ia32_debugctl;
199 u64 guest_ia32_perf_global_ctrl;
206 u64 host_ia32_perf_global_ctrl;
207 u64 padding64[8]; /* room for future expansion */
209 * To allow migration of L1 (complete with its L2 guests) between
210 * machines of different natural widths (32 or 64 bit), we cannot have
211 * unsigned long fields with no explict size. We use u64 (aliased
212 * natural_width) instead. Luckily, x86 is little-endian.
214 natural_width cr0_guest_host_mask;
215 natural_width cr4_guest_host_mask;
216 natural_width cr0_read_shadow;
217 natural_width cr4_read_shadow;
218 natural_width cr3_target_value0;
219 natural_width cr3_target_value1;
220 natural_width cr3_target_value2;
221 natural_width cr3_target_value3;
222 natural_width exit_qualification;
223 natural_width guest_linear_address;
224 natural_width guest_cr0;
225 natural_width guest_cr3;
226 natural_width guest_cr4;
227 natural_width guest_es_base;
228 natural_width guest_cs_base;
229 natural_width guest_ss_base;
230 natural_width guest_ds_base;
231 natural_width guest_fs_base;
232 natural_width guest_gs_base;
233 natural_width guest_ldtr_base;
234 natural_width guest_tr_base;
235 natural_width guest_gdtr_base;
236 natural_width guest_idtr_base;
237 natural_width guest_dr7;
238 natural_width guest_rsp;
239 natural_width guest_rip;
240 natural_width guest_rflags;
241 natural_width guest_pending_dbg_exceptions;
242 natural_width guest_sysenter_esp;
243 natural_width guest_sysenter_eip;
244 natural_width host_cr0;
245 natural_width host_cr3;
246 natural_width host_cr4;
247 natural_width host_fs_base;
248 natural_width host_gs_base;
249 natural_width host_tr_base;
250 natural_width host_gdtr_base;
251 natural_width host_idtr_base;
252 natural_width host_ia32_sysenter_esp;
253 natural_width host_ia32_sysenter_eip;
254 natural_width host_rsp;
255 natural_width host_rip;
256 natural_width paddingl[8]; /* room for future expansion */
257 u32 pin_based_vm_exec_control;
258 u32 cpu_based_vm_exec_control;
259 u32 exception_bitmap;
260 u32 page_fault_error_code_mask;
261 u32 page_fault_error_code_match;
262 u32 cr3_target_count;
263 u32 vm_exit_controls;
264 u32 vm_exit_msr_store_count;
265 u32 vm_exit_msr_load_count;
266 u32 vm_entry_controls;
267 u32 vm_entry_msr_load_count;
268 u32 vm_entry_intr_info_field;
269 u32 vm_entry_exception_error_code;
270 u32 vm_entry_instruction_len;
272 u32 secondary_vm_exec_control;
273 u32 vm_instruction_error;
275 u32 vm_exit_intr_info;
276 u32 vm_exit_intr_error_code;
277 u32 idt_vectoring_info_field;
278 u32 idt_vectoring_error_code;
279 u32 vm_exit_instruction_len;
280 u32 vmx_instruction_info;
287 u32 guest_ldtr_limit;
289 u32 guest_gdtr_limit;
290 u32 guest_idtr_limit;
291 u32 guest_es_ar_bytes;
292 u32 guest_cs_ar_bytes;
293 u32 guest_ss_ar_bytes;
294 u32 guest_ds_ar_bytes;
295 u32 guest_fs_ar_bytes;
296 u32 guest_gs_ar_bytes;
297 u32 guest_ldtr_ar_bytes;
298 u32 guest_tr_ar_bytes;
299 u32 guest_interruptibility_info;
300 u32 guest_activity_state;
301 u32 guest_sysenter_cs;
302 u32 host_ia32_sysenter_cs;
303 u32 padding32[8]; /* room for future expansion */
304 u16 virtual_processor_id;
305 u16 guest_es_selector;
306 u16 guest_cs_selector;
307 u16 guest_ss_selector;
308 u16 guest_ds_selector;
309 u16 guest_fs_selector;
310 u16 guest_gs_selector;
311 u16 guest_ldtr_selector;
312 u16 guest_tr_selector;
313 u16 host_es_selector;
314 u16 host_cs_selector;
315 u16 host_ss_selector;
316 u16 host_ds_selector;
317 u16 host_fs_selector;
318 u16 host_gs_selector;
319 u16 host_tr_selector;
323 * VMCS12_REVISION is an arbitrary id that should be changed if the content or
324 * layout of struct vmcs12 is changed. MSR_IA32_VMX_BASIC returns this id, and
325 * VMPTRLD verifies that the VMCS region that L1 is loading contains this id.
327 #define VMCS12_REVISION 0x11e57ed0
330 * VMCS12_SIZE is the number of bytes L1 should allocate for the VMXON region
331 * and any VMCS region. Although only sizeof(struct vmcs12) are used by the
332 * current implementation, 4K are reserved to avoid future complications.
334 #define VMCS12_SIZE 0x1000
336 /* Used to remember the last vmcs02 used for some recently used vmcs12s */
338 struct list_head list;
340 struct loaded_vmcs vmcs02;
344 * The nested_vmx structure is part of vcpu_vmx, and holds information we need
345 * for correct emulation of VMX (i.e., nested VMX) on this vcpu.
348 /* Has the level1 guest done vmxon? */
351 /* The guest-physical address of the current VMCS L1 keeps for L2 */
353 /* The host-usable pointer to the above */
354 struct page *current_vmcs12_page;
355 struct vmcs12 *current_vmcs12;
357 /* vmcs02_list cache of VMCSs recently used to run L2 guests */
358 struct list_head vmcs02_pool;
360 u64 vmcs01_tsc_offset;
361 /* L2 must run next, and mustn't decide to exit to L1. */
362 bool nested_run_pending;
364 * Guest pages referred to in vmcs02 with host-physical pointers, so
365 * we must keep them pinned while L2 runs.
367 struct page *apic_access_page;
371 struct kvm_vcpu vcpu;
372 unsigned long host_rsp;
375 bool nmi_known_unmasked;
377 u32 idt_vectoring_info;
379 struct shared_msr_entry *guest_msrs;
383 u64 msr_host_kernel_gs_base;
384 u64 msr_guest_kernel_gs_base;
387 * loaded_vmcs points to the VMCS currently used in this vcpu. For a
388 * non-nested (L1) guest, it always points to vmcs01. For a nested
389 * guest (L2), it points to a different VMCS.
391 struct loaded_vmcs vmcs01;
392 struct loaded_vmcs *loaded_vmcs;
393 bool __launched; /* temporary, used in vmx_vcpu_run */
394 struct msr_autoload {
396 struct vmx_msr_entry guest[NR_AUTOLOAD_MSRS];
397 struct vmx_msr_entry host[NR_AUTOLOAD_MSRS];
401 u16 fs_sel, gs_sel, ldt_sel;
405 int gs_ldt_reload_needed;
406 int fs_reload_needed;
411 struct kvm_segment segs[8];
414 u32 bitmask; /* 4 bits per segment (1 bit per field) */
415 struct kvm_save_segment {
423 bool emulation_required;
425 /* Support for vnmi-less CPUs */
426 int soft_vnmi_blocked;
428 s64 vnmi_blocked_time;
433 /* Support for a guest hypervisor (nested VMX) */
434 struct nested_vmx nested;
437 enum segment_cache_field {
446 static inline struct vcpu_vmx *to_vmx(struct kvm_vcpu *vcpu)
448 return container_of(vcpu, struct vcpu_vmx, vcpu);
451 #define VMCS12_OFFSET(x) offsetof(struct vmcs12, x)
452 #define FIELD(number, name) [number] = VMCS12_OFFSET(name)
453 #define FIELD64(number, name) [number] = VMCS12_OFFSET(name), \
454 [number##_HIGH] = VMCS12_OFFSET(name)+4
456 static const unsigned short vmcs_field_to_offset_table[] = {
457 FIELD(VIRTUAL_PROCESSOR_ID, virtual_processor_id),
458 FIELD(GUEST_ES_SELECTOR, guest_es_selector),
459 FIELD(GUEST_CS_SELECTOR, guest_cs_selector),
460 FIELD(GUEST_SS_SELECTOR, guest_ss_selector),
461 FIELD(GUEST_DS_SELECTOR, guest_ds_selector),
462 FIELD(GUEST_FS_SELECTOR, guest_fs_selector),
463 FIELD(GUEST_GS_SELECTOR, guest_gs_selector),
464 FIELD(GUEST_LDTR_SELECTOR, guest_ldtr_selector),
465 FIELD(GUEST_TR_SELECTOR, guest_tr_selector),
466 FIELD(HOST_ES_SELECTOR, host_es_selector),
467 FIELD(HOST_CS_SELECTOR, host_cs_selector),
468 FIELD(HOST_SS_SELECTOR, host_ss_selector),
469 FIELD(HOST_DS_SELECTOR, host_ds_selector),
470 FIELD(HOST_FS_SELECTOR, host_fs_selector),
471 FIELD(HOST_GS_SELECTOR, host_gs_selector),
472 FIELD(HOST_TR_SELECTOR, host_tr_selector),
473 FIELD64(IO_BITMAP_A, io_bitmap_a),
474 FIELD64(IO_BITMAP_B, io_bitmap_b),
475 FIELD64(MSR_BITMAP, msr_bitmap),
476 FIELD64(VM_EXIT_MSR_STORE_ADDR, vm_exit_msr_store_addr),
477 FIELD64(VM_EXIT_MSR_LOAD_ADDR, vm_exit_msr_load_addr),
478 FIELD64(VM_ENTRY_MSR_LOAD_ADDR, vm_entry_msr_load_addr),
479 FIELD64(TSC_OFFSET, tsc_offset),
480 FIELD64(VIRTUAL_APIC_PAGE_ADDR, virtual_apic_page_addr),
481 FIELD64(APIC_ACCESS_ADDR, apic_access_addr),
482 FIELD64(EPT_POINTER, ept_pointer),
483 FIELD64(GUEST_PHYSICAL_ADDRESS, guest_physical_address),
484 FIELD64(VMCS_LINK_POINTER, vmcs_link_pointer),
485 FIELD64(GUEST_IA32_DEBUGCTL, guest_ia32_debugctl),
486 FIELD64(GUEST_IA32_PAT, guest_ia32_pat),
487 FIELD64(GUEST_IA32_EFER, guest_ia32_efer),
488 FIELD64(GUEST_IA32_PERF_GLOBAL_CTRL, guest_ia32_perf_global_ctrl),
489 FIELD64(GUEST_PDPTR0, guest_pdptr0),
490 FIELD64(GUEST_PDPTR1, guest_pdptr1),
491 FIELD64(GUEST_PDPTR2, guest_pdptr2),
492 FIELD64(GUEST_PDPTR3, guest_pdptr3),
493 FIELD64(HOST_IA32_PAT, host_ia32_pat),
494 FIELD64(HOST_IA32_EFER, host_ia32_efer),
495 FIELD64(HOST_IA32_PERF_GLOBAL_CTRL, host_ia32_perf_global_ctrl),
496 FIELD(PIN_BASED_VM_EXEC_CONTROL, pin_based_vm_exec_control),
497 FIELD(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control),
498 FIELD(EXCEPTION_BITMAP, exception_bitmap),
499 FIELD(PAGE_FAULT_ERROR_CODE_MASK, page_fault_error_code_mask),
500 FIELD(PAGE_FAULT_ERROR_CODE_MATCH, page_fault_error_code_match),
501 FIELD(CR3_TARGET_COUNT, cr3_target_count),
502 FIELD(VM_EXIT_CONTROLS, vm_exit_controls),
503 FIELD(VM_EXIT_MSR_STORE_COUNT, vm_exit_msr_store_count),
504 FIELD(VM_EXIT_MSR_LOAD_COUNT, vm_exit_msr_load_count),
505 FIELD(VM_ENTRY_CONTROLS, vm_entry_controls),
506 FIELD(VM_ENTRY_MSR_LOAD_COUNT, vm_entry_msr_load_count),
507 FIELD(VM_ENTRY_INTR_INFO_FIELD, vm_entry_intr_info_field),
508 FIELD(VM_ENTRY_EXCEPTION_ERROR_CODE, vm_entry_exception_error_code),
509 FIELD(VM_ENTRY_INSTRUCTION_LEN, vm_entry_instruction_len),
510 FIELD(TPR_THRESHOLD, tpr_threshold),
511 FIELD(SECONDARY_VM_EXEC_CONTROL, secondary_vm_exec_control),
512 FIELD(VM_INSTRUCTION_ERROR, vm_instruction_error),
513 FIELD(VM_EXIT_REASON, vm_exit_reason),
514 FIELD(VM_EXIT_INTR_INFO, vm_exit_intr_info),
515 FIELD(VM_EXIT_INTR_ERROR_CODE, vm_exit_intr_error_code),
516 FIELD(IDT_VECTORING_INFO_FIELD, idt_vectoring_info_field),
517 FIELD(IDT_VECTORING_ERROR_CODE, idt_vectoring_error_code),
518 FIELD(VM_EXIT_INSTRUCTION_LEN, vm_exit_instruction_len),
519 FIELD(VMX_INSTRUCTION_INFO, vmx_instruction_info),
520 FIELD(GUEST_ES_LIMIT, guest_es_limit),
521 FIELD(GUEST_CS_LIMIT, guest_cs_limit),
522 FIELD(GUEST_SS_LIMIT, guest_ss_limit),
523 FIELD(GUEST_DS_LIMIT, guest_ds_limit),
524 FIELD(GUEST_FS_LIMIT, guest_fs_limit),
525 FIELD(GUEST_GS_LIMIT, guest_gs_limit),
526 FIELD(GUEST_LDTR_LIMIT, guest_ldtr_limit),
527 FIELD(GUEST_TR_LIMIT, guest_tr_limit),
528 FIELD(GUEST_GDTR_LIMIT, guest_gdtr_limit),
529 FIELD(GUEST_IDTR_LIMIT, guest_idtr_limit),
530 FIELD(GUEST_ES_AR_BYTES, guest_es_ar_bytes),
531 FIELD(GUEST_CS_AR_BYTES, guest_cs_ar_bytes),
532 FIELD(GUEST_SS_AR_BYTES, guest_ss_ar_bytes),
533 FIELD(GUEST_DS_AR_BYTES, guest_ds_ar_bytes),
534 FIELD(GUEST_FS_AR_BYTES, guest_fs_ar_bytes),
535 FIELD(GUEST_GS_AR_BYTES, guest_gs_ar_bytes),
536 FIELD(GUEST_LDTR_AR_BYTES, guest_ldtr_ar_bytes),
537 FIELD(GUEST_TR_AR_BYTES, guest_tr_ar_bytes),
538 FIELD(GUEST_INTERRUPTIBILITY_INFO, guest_interruptibility_info),
539 FIELD(GUEST_ACTIVITY_STATE, guest_activity_state),
540 FIELD(GUEST_SYSENTER_CS, guest_sysenter_cs),
541 FIELD(HOST_IA32_SYSENTER_CS, host_ia32_sysenter_cs),
542 FIELD(CR0_GUEST_HOST_MASK, cr0_guest_host_mask),
543 FIELD(CR4_GUEST_HOST_MASK, cr4_guest_host_mask),
544 FIELD(CR0_READ_SHADOW, cr0_read_shadow),
545 FIELD(CR4_READ_SHADOW, cr4_read_shadow),
546 FIELD(CR3_TARGET_VALUE0, cr3_target_value0),
547 FIELD(CR3_TARGET_VALUE1, cr3_target_value1),
548 FIELD(CR3_TARGET_VALUE2, cr3_target_value2),
549 FIELD(CR3_TARGET_VALUE3, cr3_target_value3),
550 FIELD(EXIT_QUALIFICATION, exit_qualification),
551 FIELD(GUEST_LINEAR_ADDRESS, guest_linear_address),
552 FIELD(GUEST_CR0, guest_cr0),
553 FIELD(GUEST_CR3, guest_cr3),
554 FIELD(GUEST_CR4, guest_cr4),
555 FIELD(GUEST_ES_BASE, guest_es_base),
556 FIELD(GUEST_CS_BASE, guest_cs_base),
557 FIELD(GUEST_SS_BASE, guest_ss_base),
558 FIELD(GUEST_DS_BASE, guest_ds_base),
559 FIELD(GUEST_FS_BASE, guest_fs_base),
560 FIELD(GUEST_GS_BASE, guest_gs_base),
561 FIELD(GUEST_LDTR_BASE, guest_ldtr_base),
562 FIELD(GUEST_TR_BASE, guest_tr_base),
563 FIELD(GUEST_GDTR_BASE, guest_gdtr_base),
564 FIELD(GUEST_IDTR_BASE, guest_idtr_base),
565 FIELD(GUEST_DR7, guest_dr7),
566 FIELD(GUEST_RSP, guest_rsp),
567 FIELD(GUEST_RIP, guest_rip),
568 FIELD(GUEST_RFLAGS, guest_rflags),
569 FIELD(GUEST_PENDING_DBG_EXCEPTIONS, guest_pending_dbg_exceptions),
570 FIELD(GUEST_SYSENTER_ESP, guest_sysenter_esp),
571 FIELD(GUEST_SYSENTER_EIP, guest_sysenter_eip),
572 FIELD(HOST_CR0, host_cr0),
573 FIELD(HOST_CR3, host_cr3),
574 FIELD(HOST_CR4, host_cr4),
575 FIELD(HOST_FS_BASE, host_fs_base),
576 FIELD(HOST_GS_BASE, host_gs_base),
577 FIELD(HOST_TR_BASE, host_tr_base),
578 FIELD(HOST_GDTR_BASE, host_gdtr_base),
579 FIELD(HOST_IDTR_BASE, host_idtr_base),
580 FIELD(HOST_IA32_SYSENTER_ESP, host_ia32_sysenter_esp),
581 FIELD(HOST_IA32_SYSENTER_EIP, host_ia32_sysenter_eip),
582 FIELD(HOST_RSP, host_rsp),
583 FIELD(HOST_RIP, host_rip),
585 static const int max_vmcs_field = ARRAY_SIZE(vmcs_field_to_offset_table);
587 static inline short vmcs_field_to_offset(unsigned long field)
589 if (field >= max_vmcs_field || vmcs_field_to_offset_table[field] == 0)
591 return vmcs_field_to_offset_table[field];
594 static inline struct vmcs12 *get_vmcs12(struct kvm_vcpu *vcpu)
596 return to_vmx(vcpu)->nested.current_vmcs12;
599 static struct page *nested_get_page(struct kvm_vcpu *vcpu, gpa_t addr)
601 struct page *page = gfn_to_page(vcpu->kvm, addr >> PAGE_SHIFT);
602 if (is_error_page(page))
608 static void nested_release_page(struct page *page)
610 kvm_release_page_dirty(page);
613 static void nested_release_page_clean(struct page *page)
615 kvm_release_page_clean(page);
618 static u64 construct_eptp(unsigned long root_hpa);
619 static void kvm_cpu_vmxon(u64 addr);
620 static void kvm_cpu_vmxoff(void);
621 static void vmx_set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3);
622 static int vmx_set_tss_addr(struct kvm *kvm, unsigned int addr);
623 static void vmx_set_segment(struct kvm_vcpu *vcpu,
624 struct kvm_segment *var, int seg);
625 static void vmx_get_segment(struct kvm_vcpu *vcpu,
626 struct kvm_segment *var, int seg);
627 static bool guest_state_valid(struct kvm_vcpu *vcpu);
628 static u32 vmx_segment_access_rights(struct kvm_segment *var);
630 static DEFINE_PER_CPU(struct vmcs *, vmxarea);
631 static DEFINE_PER_CPU(struct vmcs *, current_vmcs);
633 * We maintain a per-CPU linked-list of VMCS loaded on that CPU. This is needed
634 * when a CPU is brought down, and we need to VMCLEAR all VMCSs loaded on it.
636 static DEFINE_PER_CPU(struct list_head, loaded_vmcss_on_cpu);
637 static DEFINE_PER_CPU(struct desc_ptr, host_gdt);
639 static unsigned long *vmx_io_bitmap_a;
640 static unsigned long *vmx_io_bitmap_b;
641 static unsigned long *vmx_msr_bitmap_legacy;
642 static unsigned long *vmx_msr_bitmap_longmode;
644 static bool cpu_has_load_ia32_efer;
645 static bool cpu_has_load_perf_global_ctrl;
647 static DECLARE_BITMAP(vmx_vpid_bitmap, VMX_NR_VPIDS);
648 static DEFINE_SPINLOCK(vmx_vpid_lock);
650 static struct vmcs_config {
654 u32 pin_based_exec_ctrl;
655 u32 cpu_based_exec_ctrl;
656 u32 cpu_based_2nd_exec_ctrl;
661 static struct vmx_capability {
666 #define VMX_SEGMENT_FIELD(seg) \
667 [VCPU_SREG_##seg] = { \
668 .selector = GUEST_##seg##_SELECTOR, \
669 .base = GUEST_##seg##_BASE, \
670 .limit = GUEST_##seg##_LIMIT, \
671 .ar_bytes = GUEST_##seg##_AR_BYTES, \
674 static const struct kvm_vmx_segment_field {
679 } kvm_vmx_segment_fields[] = {
680 VMX_SEGMENT_FIELD(CS),
681 VMX_SEGMENT_FIELD(DS),
682 VMX_SEGMENT_FIELD(ES),
683 VMX_SEGMENT_FIELD(FS),
684 VMX_SEGMENT_FIELD(GS),
685 VMX_SEGMENT_FIELD(SS),
686 VMX_SEGMENT_FIELD(TR),
687 VMX_SEGMENT_FIELD(LDTR),
690 static u64 host_efer;
692 static void ept_save_pdptrs(struct kvm_vcpu *vcpu);
695 * Keep MSR_STAR at the end, as setup_msrs() will try to optimize it
696 * away by decrementing the array size.
698 static const u32 vmx_msr_index[] = {
700 MSR_SYSCALL_MASK, MSR_LSTAR, MSR_CSTAR,
702 MSR_EFER, MSR_TSC_AUX, MSR_STAR,
704 #define NR_VMX_MSR ARRAY_SIZE(vmx_msr_index)
706 static inline bool is_page_fault(u32 intr_info)
708 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
709 INTR_INFO_VALID_MASK)) ==
710 (INTR_TYPE_HARD_EXCEPTION | PF_VECTOR | INTR_INFO_VALID_MASK);
713 static inline bool is_no_device(u32 intr_info)
715 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
716 INTR_INFO_VALID_MASK)) ==
717 (INTR_TYPE_HARD_EXCEPTION | NM_VECTOR | INTR_INFO_VALID_MASK);
720 static inline bool is_invalid_opcode(u32 intr_info)
722 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
723 INTR_INFO_VALID_MASK)) ==
724 (INTR_TYPE_HARD_EXCEPTION | UD_VECTOR | INTR_INFO_VALID_MASK);
727 static inline bool is_external_interrupt(u32 intr_info)
729 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK))
730 == (INTR_TYPE_EXT_INTR | INTR_INFO_VALID_MASK);
733 static inline bool is_machine_check(u32 intr_info)
735 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
736 INTR_INFO_VALID_MASK)) ==
737 (INTR_TYPE_HARD_EXCEPTION | MC_VECTOR | INTR_INFO_VALID_MASK);
740 static inline bool cpu_has_vmx_msr_bitmap(void)
742 return vmcs_config.cpu_based_exec_ctrl & CPU_BASED_USE_MSR_BITMAPS;
745 static inline bool cpu_has_vmx_tpr_shadow(void)
747 return vmcs_config.cpu_based_exec_ctrl & CPU_BASED_TPR_SHADOW;
750 static inline bool vm_need_tpr_shadow(struct kvm *kvm)
752 return (cpu_has_vmx_tpr_shadow()) && (irqchip_in_kernel(kvm));
755 static inline bool cpu_has_secondary_exec_ctrls(void)
757 return vmcs_config.cpu_based_exec_ctrl &
758 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
761 static inline bool cpu_has_vmx_virtualize_apic_accesses(void)
763 return vmcs_config.cpu_based_2nd_exec_ctrl &
764 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
767 static inline bool cpu_has_vmx_flexpriority(void)
769 return cpu_has_vmx_tpr_shadow() &&
770 cpu_has_vmx_virtualize_apic_accesses();
773 static inline bool cpu_has_vmx_ept_execute_only(void)
775 return vmx_capability.ept & VMX_EPT_EXECUTE_ONLY_BIT;
778 static inline bool cpu_has_vmx_eptp_uncacheable(void)
780 return vmx_capability.ept & VMX_EPTP_UC_BIT;
783 static inline bool cpu_has_vmx_eptp_writeback(void)
785 return vmx_capability.ept & VMX_EPTP_WB_BIT;
788 static inline bool cpu_has_vmx_ept_2m_page(void)
790 return vmx_capability.ept & VMX_EPT_2MB_PAGE_BIT;
793 static inline bool cpu_has_vmx_ept_1g_page(void)
795 return vmx_capability.ept & VMX_EPT_1GB_PAGE_BIT;
798 static inline bool cpu_has_vmx_ept_4levels(void)
800 return vmx_capability.ept & VMX_EPT_PAGE_WALK_4_BIT;
803 static inline bool cpu_has_vmx_ept_ad_bits(void)
805 return vmx_capability.ept & VMX_EPT_AD_BIT;
808 static inline bool cpu_has_vmx_invept_context(void)
810 return vmx_capability.ept & VMX_EPT_EXTENT_CONTEXT_BIT;
813 static inline bool cpu_has_vmx_invept_global(void)
815 return vmx_capability.ept & VMX_EPT_EXTENT_GLOBAL_BIT;
818 static inline bool cpu_has_vmx_invvpid_single(void)
820 return vmx_capability.vpid & VMX_VPID_EXTENT_SINGLE_CONTEXT_BIT;
823 static inline bool cpu_has_vmx_invvpid_global(void)
825 return vmx_capability.vpid & VMX_VPID_EXTENT_GLOBAL_CONTEXT_BIT;
828 static inline bool cpu_has_vmx_ept(void)
830 return vmcs_config.cpu_based_2nd_exec_ctrl &
831 SECONDARY_EXEC_ENABLE_EPT;
834 static inline bool cpu_has_vmx_unrestricted_guest(void)
836 return vmcs_config.cpu_based_2nd_exec_ctrl &
837 SECONDARY_EXEC_UNRESTRICTED_GUEST;
840 static inline bool cpu_has_vmx_ple(void)
842 return vmcs_config.cpu_based_2nd_exec_ctrl &
843 SECONDARY_EXEC_PAUSE_LOOP_EXITING;
846 static inline bool vm_need_virtualize_apic_accesses(struct kvm *kvm)
848 return flexpriority_enabled && irqchip_in_kernel(kvm);
851 static inline bool cpu_has_vmx_vpid(void)
853 return vmcs_config.cpu_based_2nd_exec_ctrl &
854 SECONDARY_EXEC_ENABLE_VPID;
857 static inline bool cpu_has_vmx_rdtscp(void)
859 return vmcs_config.cpu_based_2nd_exec_ctrl &
860 SECONDARY_EXEC_RDTSCP;
863 static inline bool cpu_has_vmx_invpcid(void)
865 return vmcs_config.cpu_based_2nd_exec_ctrl &
866 SECONDARY_EXEC_ENABLE_INVPCID;
869 static inline bool cpu_has_virtual_nmis(void)
871 return vmcs_config.pin_based_exec_ctrl & PIN_BASED_VIRTUAL_NMIS;
874 static inline bool cpu_has_vmx_wbinvd_exit(void)
876 return vmcs_config.cpu_based_2nd_exec_ctrl &
877 SECONDARY_EXEC_WBINVD_EXITING;
880 static inline bool report_flexpriority(void)
882 return flexpriority_enabled;
885 static inline bool nested_cpu_has(struct vmcs12 *vmcs12, u32 bit)
887 return vmcs12->cpu_based_vm_exec_control & bit;
890 static inline bool nested_cpu_has2(struct vmcs12 *vmcs12, u32 bit)
892 return (vmcs12->cpu_based_vm_exec_control &
893 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) &&
894 (vmcs12->secondary_vm_exec_control & bit);
897 static inline bool nested_cpu_has_virtual_nmis(struct vmcs12 *vmcs12,
898 struct kvm_vcpu *vcpu)
900 return vmcs12->pin_based_vm_exec_control & PIN_BASED_VIRTUAL_NMIS;
903 static inline bool is_exception(u32 intr_info)
905 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK))
906 == (INTR_TYPE_HARD_EXCEPTION | INTR_INFO_VALID_MASK);
909 static void nested_vmx_vmexit(struct kvm_vcpu *vcpu);
910 static void nested_vmx_entry_failure(struct kvm_vcpu *vcpu,
911 struct vmcs12 *vmcs12,
912 u32 reason, unsigned long qualification);
914 static int __find_msr_index(struct vcpu_vmx *vmx, u32 msr)
918 for (i = 0; i < vmx->nmsrs; ++i)
919 if (vmx_msr_index[vmx->guest_msrs[i].index] == msr)
924 static inline void __invvpid(int ext, u16 vpid, gva_t gva)
930 } operand = { vpid, 0, gva };
932 asm volatile (__ex(ASM_VMX_INVVPID)
933 /* CF==1 or ZF==1 --> rc = -1 */
935 : : "a"(&operand), "c"(ext) : "cc", "memory");
938 static inline void __invept(int ext, u64 eptp, gpa_t gpa)
942 } operand = {eptp, gpa};
944 asm volatile (__ex(ASM_VMX_INVEPT)
945 /* CF==1 or ZF==1 --> rc = -1 */
946 "; ja 1f ; ud2 ; 1:\n"
947 : : "a" (&operand), "c" (ext) : "cc", "memory");
950 static struct shared_msr_entry *find_msr_entry(struct vcpu_vmx *vmx, u32 msr)
954 i = __find_msr_index(vmx, msr);
956 return &vmx->guest_msrs[i];
960 static void vmcs_clear(struct vmcs *vmcs)
962 u64 phys_addr = __pa(vmcs);
965 asm volatile (__ex(ASM_VMX_VMCLEAR_RAX) "; setna %0"
966 : "=qm"(error) : "a"(&phys_addr), "m"(phys_addr)
969 printk(KERN_ERR "kvm: vmclear fail: %p/%llx\n",
973 static inline void loaded_vmcs_init(struct loaded_vmcs *loaded_vmcs)
975 vmcs_clear(loaded_vmcs->vmcs);
976 loaded_vmcs->cpu = -1;
977 loaded_vmcs->launched = 0;
980 static void vmcs_load(struct vmcs *vmcs)
982 u64 phys_addr = __pa(vmcs);
985 asm volatile (__ex(ASM_VMX_VMPTRLD_RAX) "; setna %0"
986 : "=qm"(error) : "a"(&phys_addr), "m"(phys_addr)
989 printk(KERN_ERR "kvm: vmptrld %p/%llx failed\n",
995 * This bitmap is used to indicate whether the vmclear
996 * operation is enabled on all cpus. All disabled by
999 static cpumask_t crash_vmclear_enabled_bitmap = CPU_MASK_NONE;
1001 static inline void crash_enable_local_vmclear(int cpu)
1003 cpumask_set_cpu(cpu, &crash_vmclear_enabled_bitmap);
1006 static inline void crash_disable_local_vmclear(int cpu)
1008 cpumask_clear_cpu(cpu, &crash_vmclear_enabled_bitmap);
1011 static inline int crash_local_vmclear_enabled(int cpu)
1013 return cpumask_test_cpu(cpu, &crash_vmclear_enabled_bitmap);
1016 static void crash_vmclear_local_loaded_vmcss(void)
1018 int cpu = raw_smp_processor_id();
1019 struct loaded_vmcs *v;
1021 if (!crash_local_vmclear_enabled(cpu))
1024 list_for_each_entry(v, &per_cpu(loaded_vmcss_on_cpu, cpu),
1025 loaded_vmcss_on_cpu_link)
1026 vmcs_clear(v->vmcs);
1029 static inline void crash_enable_local_vmclear(int cpu) { }
1030 static inline void crash_disable_local_vmclear(int cpu) { }
1031 #endif /* CONFIG_KEXEC */
1033 static void __loaded_vmcs_clear(void *arg)
1035 struct loaded_vmcs *loaded_vmcs = arg;
1036 int cpu = raw_smp_processor_id();
1038 if (loaded_vmcs->cpu != cpu)
1039 return; /* vcpu migration can race with cpu offline */
1040 if (per_cpu(current_vmcs, cpu) == loaded_vmcs->vmcs)
1041 per_cpu(current_vmcs, cpu) = NULL;
1042 crash_disable_local_vmclear(cpu);
1043 list_del(&loaded_vmcs->loaded_vmcss_on_cpu_link);
1046 * we should ensure updating loaded_vmcs->loaded_vmcss_on_cpu_link
1047 * is before setting loaded_vmcs->vcpu to -1 which is done in
1048 * loaded_vmcs_init. Otherwise, other cpu can see vcpu = -1 fist
1049 * then adds the vmcs into percpu list before it is deleted.
1053 loaded_vmcs_init(loaded_vmcs);
1054 crash_enable_local_vmclear(cpu);
1057 static void loaded_vmcs_clear(struct loaded_vmcs *loaded_vmcs)
1059 int cpu = loaded_vmcs->cpu;
1062 smp_call_function_single(cpu,
1063 __loaded_vmcs_clear, loaded_vmcs, 1);
1066 static inline void vpid_sync_vcpu_single(struct vcpu_vmx *vmx)
1071 if (cpu_has_vmx_invvpid_single())
1072 __invvpid(VMX_VPID_EXTENT_SINGLE_CONTEXT, vmx->vpid, 0);
1075 static inline void vpid_sync_vcpu_global(void)
1077 if (cpu_has_vmx_invvpid_global())
1078 __invvpid(VMX_VPID_EXTENT_ALL_CONTEXT, 0, 0);
1081 static inline void vpid_sync_context(struct vcpu_vmx *vmx)
1083 if (cpu_has_vmx_invvpid_single())
1084 vpid_sync_vcpu_single(vmx);
1086 vpid_sync_vcpu_global();
1089 static inline void ept_sync_global(void)
1091 if (cpu_has_vmx_invept_global())
1092 __invept(VMX_EPT_EXTENT_GLOBAL, 0, 0);
1095 static inline void ept_sync_context(u64 eptp)
1098 if (cpu_has_vmx_invept_context())
1099 __invept(VMX_EPT_EXTENT_CONTEXT, eptp, 0);
1105 static __always_inline unsigned long vmcs_readl(unsigned long field)
1107 unsigned long value;
1109 asm volatile (__ex_clear(ASM_VMX_VMREAD_RDX_RAX, "%0")
1110 : "=a"(value) : "d"(field) : "cc");
1114 static __always_inline u16 vmcs_read16(unsigned long field)
1116 return vmcs_readl(field);
1119 static __always_inline u32 vmcs_read32(unsigned long field)
1121 return vmcs_readl(field);
1124 static __always_inline u64 vmcs_read64(unsigned long field)
1126 #ifdef CONFIG_X86_64
1127 return vmcs_readl(field);
1129 return vmcs_readl(field) | ((u64)vmcs_readl(field+1) << 32);
1133 static noinline void vmwrite_error(unsigned long field, unsigned long value)
1135 printk(KERN_ERR "vmwrite error: reg %lx value %lx (err %d)\n",
1136 field, value, vmcs_read32(VM_INSTRUCTION_ERROR));
1140 static void vmcs_writel(unsigned long field, unsigned long value)
1144 asm volatile (__ex(ASM_VMX_VMWRITE_RAX_RDX) "; setna %0"
1145 : "=q"(error) : "a"(value), "d"(field) : "cc");
1146 if (unlikely(error))
1147 vmwrite_error(field, value);
1150 static void vmcs_write16(unsigned long field, u16 value)
1152 vmcs_writel(field, value);
1155 static void vmcs_write32(unsigned long field, u32 value)
1157 vmcs_writel(field, value);
1160 static void vmcs_write64(unsigned long field, u64 value)
1162 vmcs_writel(field, value);
1163 #ifndef CONFIG_X86_64
1165 vmcs_writel(field+1, value >> 32);
1169 static void vmcs_clear_bits(unsigned long field, u32 mask)
1171 vmcs_writel(field, vmcs_readl(field) & ~mask);
1174 static void vmcs_set_bits(unsigned long field, u32 mask)
1176 vmcs_writel(field, vmcs_readl(field) | mask);
1179 static void vmx_segment_cache_clear(struct vcpu_vmx *vmx)
1181 vmx->segment_cache.bitmask = 0;
1184 static bool vmx_segment_cache_test_set(struct vcpu_vmx *vmx, unsigned seg,
1188 u32 mask = 1 << (seg * SEG_FIELD_NR + field);
1190 if (!(vmx->vcpu.arch.regs_avail & (1 << VCPU_EXREG_SEGMENTS))) {
1191 vmx->vcpu.arch.regs_avail |= (1 << VCPU_EXREG_SEGMENTS);
1192 vmx->segment_cache.bitmask = 0;
1194 ret = vmx->segment_cache.bitmask & mask;
1195 vmx->segment_cache.bitmask |= mask;
1199 static u16 vmx_read_guest_seg_selector(struct vcpu_vmx *vmx, unsigned seg)
1201 u16 *p = &vmx->segment_cache.seg[seg].selector;
1203 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_SEL))
1204 *p = vmcs_read16(kvm_vmx_segment_fields[seg].selector);
1208 static ulong vmx_read_guest_seg_base(struct vcpu_vmx *vmx, unsigned seg)
1210 ulong *p = &vmx->segment_cache.seg[seg].base;
1212 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_BASE))
1213 *p = vmcs_readl(kvm_vmx_segment_fields[seg].base);
1217 static u32 vmx_read_guest_seg_limit(struct vcpu_vmx *vmx, unsigned seg)
1219 u32 *p = &vmx->segment_cache.seg[seg].limit;
1221 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_LIMIT))
1222 *p = vmcs_read32(kvm_vmx_segment_fields[seg].limit);
1226 static u32 vmx_read_guest_seg_ar(struct vcpu_vmx *vmx, unsigned seg)
1228 u32 *p = &vmx->segment_cache.seg[seg].ar;
1230 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_AR))
1231 *p = vmcs_read32(kvm_vmx_segment_fields[seg].ar_bytes);
1235 static void update_exception_bitmap(struct kvm_vcpu *vcpu)
1239 eb = (1u << PF_VECTOR) | (1u << UD_VECTOR) | (1u << MC_VECTOR) |
1240 (1u << NM_VECTOR) | (1u << DB_VECTOR);
1241 if ((vcpu->guest_debug &
1242 (KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_SW_BP)) ==
1243 (KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_SW_BP))
1244 eb |= 1u << BP_VECTOR;
1245 if (to_vmx(vcpu)->rmode.vm86_active)
1248 eb &= ~(1u << PF_VECTOR); /* bypass_guest_pf = 0 */
1249 if (vcpu->fpu_active)
1250 eb &= ~(1u << NM_VECTOR);
1252 /* When we are running a nested L2 guest and L1 specified for it a
1253 * certain exception bitmap, we must trap the same exceptions and pass
1254 * them to L1. When running L2, we will only handle the exceptions
1255 * specified above if L1 did not want them.
1257 if (is_guest_mode(vcpu))
1258 eb |= get_vmcs12(vcpu)->exception_bitmap;
1260 vmcs_write32(EXCEPTION_BITMAP, eb);
1263 static void clear_atomic_switch_msr_special(unsigned long entry,
1266 vmcs_clear_bits(VM_ENTRY_CONTROLS, entry);
1267 vmcs_clear_bits(VM_EXIT_CONTROLS, exit);
1270 static void clear_atomic_switch_msr(struct vcpu_vmx *vmx, unsigned msr)
1273 struct msr_autoload *m = &vmx->msr_autoload;
1277 if (cpu_has_load_ia32_efer) {
1278 clear_atomic_switch_msr_special(VM_ENTRY_LOAD_IA32_EFER,
1279 VM_EXIT_LOAD_IA32_EFER);
1283 case MSR_CORE_PERF_GLOBAL_CTRL:
1284 if (cpu_has_load_perf_global_ctrl) {
1285 clear_atomic_switch_msr_special(
1286 VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL,
1287 VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL);
1293 for (i = 0; i < m->nr; ++i)
1294 if (m->guest[i].index == msr)
1300 m->guest[i] = m->guest[m->nr];
1301 m->host[i] = m->host[m->nr];
1302 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, m->nr);
1303 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, m->nr);
1306 static void add_atomic_switch_msr_special(unsigned long entry,
1307 unsigned long exit, unsigned long guest_val_vmcs,
1308 unsigned long host_val_vmcs, u64 guest_val, u64 host_val)
1310 vmcs_write64(guest_val_vmcs, guest_val);
1311 vmcs_write64(host_val_vmcs, host_val);
1312 vmcs_set_bits(VM_ENTRY_CONTROLS, entry);
1313 vmcs_set_bits(VM_EXIT_CONTROLS, exit);
1316 static void add_atomic_switch_msr(struct vcpu_vmx *vmx, unsigned msr,
1317 u64 guest_val, u64 host_val)
1320 struct msr_autoload *m = &vmx->msr_autoload;
1324 if (cpu_has_load_ia32_efer) {
1325 add_atomic_switch_msr_special(VM_ENTRY_LOAD_IA32_EFER,
1326 VM_EXIT_LOAD_IA32_EFER,
1329 guest_val, host_val);
1333 case MSR_CORE_PERF_GLOBAL_CTRL:
1334 if (cpu_has_load_perf_global_ctrl) {
1335 add_atomic_switch_msr_special(
1336 VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL,
1337 VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL,
1338 GUEST_IA32_PERF_GLOBAL_CTRL,
1339 HOST_IA32_PERF_GLOBAL_CTRL,
1340 guest_val, host_val);
1346 for (i = 0; i < m->nr; ++i)
1347 if (m->guest[i].index == msr)
1350 if (i == NR_AUTOLOAD_MSRS) {
1351 printk_once(KERN_WARNING"Not enough mst switch entries. "
1352 "Can't add msr %x\n", msr);
1354 } else if (i == m->nr) {
1356 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, m->nr);
1357 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, m->nr);
1360 m->guest[i].index = msr;
1361 m->guest[i].value = guest_val;
1362 m->host[i].index = msr;
1363 m->host[i].value = host_val;
1366 static void reload_tss(void)
1369 * VT restores TR but not its size. Useless.
1371 struct desc_ptr *gdt = &__get_cpu_var(host_gdt);
1372 struct desc_struct *descs;
1374 descs = (void *)gdt->address;
1375 descs[GDT_ENTRY_TSS].type = 9; /* available TSS */
1379 static bool update_transition_efer(struct vcpu_vmx *vmx, int efer_offset)
1384 guest_efer = vmx->vcpu.arch.efer;
1387 * NX is emulated; LMA and LME handled by hardware; SCE meaningless
1390 ignore_bits = EFER_NX | EFER_SCE;
1391 #ifdef CONFIG_X86_64
1392 ignore_bits |= EFER_LMA | EFER_LME;
1393 /* SCE is meaningful only in long mode on Intel */
1394 if (guest_efer & EFER_LMA)
1395 ignore_bits &= ~(u64)EFER_SCE;
1397 guest_efer &= ~ignore_bits;
1398 guest_efer |= host_efer & ignore_bits;
1399 vmx->guest_msrs[efer_offset].data = guest_efer;
1400 vmx->guest_msrs[efer_offset].mask = ~ignore_bits;
1402 clear_atomic_switch_msr(vmx, MSR_EFER);
1403 /* On ept, can't emulate nx, and must switch nx atomically */
1404 if (enable_ept && ((vmx->vcpu.arch.efer ^ host_efer) & EFER_NX)) {
1405 guest_efer = vmx->vcpu.arch.efer;
1406 if (!(guest_efer & EFER_LMA))
1407 guest_efer &= ~EFER_LME;
1408 add_atomic_switch_msr(vmx, MSR_EFER, guest_efer, host_efer);
1415 static unsigned long segment_base(u16 selector)
1417 struct desc_ptr *gdt = &__get_cpu_var(host_gdt);
1418 struct desc_struct *d;
1419 unsigned long table_base;
1422 if (!(selector & ~3))
1425 table_base = gdt->address;
1427 if (selector & 4) { /* from ldt */
1428 u16 ldt_selector = kvm_read_ldt();
1430 if (!(ldt_selector & ~3))
1433 table_base = segment_base(ldt_selector);
1435 d = (struct desc_struct *)(table_base + (selector & ~7));
1436 v = get_desc_base(d);
1437 #ifdef CONFIG_X86_64
1438 if (d->s == 0 && (d->type == 2 || d->type == 9 || d->type == 11))
1439 v |= ((unsigned long)((struct ldttss_desc64 *)d)->base3) << 32;
1444 static inline unsigned long kvm_read_tr_base(void)
1447 asm("str %0" : "=g"(tr));
1448 return segment_base(tr);
1451 static void vmx_save_host_state(struct kvm_vcpu *vcpu)
1453 struct vcpu_vmx *vmx = to_vmx(vcpu);
1456 if (vmx->host_state.loaded)
1459 vmx->host_state.loaded = 1;
1461 * Set host fs and gs selectors. Unfortunately, 22.2.3 does not
1462 * allow segment selectors with cpl > 0 or ti == 1.
1464 vmx->host_state.ldt_sel = kvm_read_ldt();
1465 vmx->host_state.gs_ldt_reload_needed = vmx->host_state.ldt_sel;
1466 savesegment(fs, vmx->host_state.fs_sel);
1467 if (!(vmx->host_state.fs_sel & 7)) {
1468 vmcs_write16(HOST_FS_SELECTOR, vmx->host_state.fs_sel);
1469 vmx->host_state.fs_reload_needed = 0;
1471 vmcs_write16(HOST_FS_SELECTOR, 0);
1472 vmx->host_state.fs_reload_needed = 1;
1474 savesegment(gs, vmx->host_state.gs_sel);
1475 if (!(vmx->host_state.gs_sel & 7))
1476 vmcs_write16(HOST_GS_SELECTOR, vmx->host_state.gs_sel);
1478 vmcs_write16(HOST_GS_SELECTOR, 0);
1479 vmx->host_state.gs_ldt_reload_needed = 1;
1482 #ifdef CONFIG_X86_64
1483 savesegment(ds, vmx->host_state.ds_sel);
1484 savesegment(es, vmx->host_state.es_sel);
1487 #ifdef CONFIG_X86_64
1488 vmcs_writel(HOST_FS_BASE, read_msr(MSR_FS_BASE));
1489 vmcs_writel(HOST_GS_BASE, read_msr(MSR_GS_BASE));
1491 vmcs_writel(HOST_FS_BASE, segment_base(vmx->host_state.fs_sel));
1492 vmcs_writel(HOST_GS_BASE, segment_base(vmx->host_state.gs_sel));
1495 #ifdef CONFIG_X86_64
1496 rdmsrl(MSR_KERNEL_GS_BASE, vmx->msr_host_kernel_gs_base);
1497 if (is_long_mode(&vmx->vcpu))
1498 wrmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base);
1500 for (i = 0; i < vmx->save_nmsrs; ++i)
1501 kvm_set_shared_msr(vmx->guest_msrs[i].index,
1502 vmx->guest_msrs[i].data,
1503 vmx->guest_msrs[i].mask);
1506 static void __vmx_load_host_state(struct vcpu_vmx *vmx)
1508 if (!vmx->host_state.loaded)
1511 ++vmx->vcpu.stat.host_state_reload;
1512 vmx->host_state.loaded = 0;
1513 #ifdef CONFIG_X86_64
1514 if (is_long_mode(&vmx->vcpu))
1515 rdmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base);
1517 if (vmx->host_state.gs_ldt_reload_needed) {
1518 kvm_load_ldt(vmx->host_state.ldt_sel);
1519 #ifdef CONFIG_X86_64
1520 load_gs_index(vmx->host_state.gs_sel);
1522 loadsegment(gs, vmx->host_state.gs_sel);
1525 if (vmx->host_state.fs_reload_needed)
1526 loadsegment(fs, vmx->host_state.fs_sel);
1527 #ifdef CONFIG_X86_64
1528 if (unlikely(vmx->host_state.ds_sel | vmx->host_state.es_sel)) {
1529 loadsegment(ds, vmx->host_state.ds_sel);
1530 loadsegment(es, vmx->host_state.es_sel);
1534 #ifdef CONFIG_X86_64
1535 wrmsrl(MSR_KERNEL_GS_BASE, vmx->msr_host_kernel_gs_base);
1538 * If the FPU is not active (through the host task or
1539 * the guest vcpu), then restore the cr0.TS bit.
1541 if (!user_has_fpu() && !vmx->vcpu.guest_fpu_loaded)
1543 load_gdt(&__get_cpu_var(host_gdt));
1546 static void vmx_load_host_state(struct vcpu_vmx *vmx)
1549 __vmx_load_host_state(vmx);
1554 * Switches to specified vcpu, until a matching vcpu_put(), but assumes
1555 * vcpu mutex is already taken.
1557 static void vmx_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
1559 struct vcpu_vmx *vmx = to_vmx(vcpu);
1560 u64 phys_addr = __pa(per_cpu(vmxarea, cpu));
1563 kvm_cpu_vmxon(phys_addr);
1564 else if (vmx->loaded_vmcs->cpu != cpu)
1565 loaded_vmcs_clear(vmx->loaded_vmcs);
1567 if (per_cpu(current_vmcs, cpu) != vmx->loaded_vmcs->vmcs) {
1568 per_cpu(current_vmcs, cpu) = vmx->loaded_vmcs->vmcs;
1569 vmcs_load(vmx->loaded_vmcs->vmcs);
1572 if (vmx->loaded_vmcs->cpu != cpu) {
1573 struct desc_ptr *gdt = &__get_cpu_var(host_gdt);
1574 unsigned long sysenter_esp;
1576 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
1577 local_irq_disable();
1578 crash_disable_local_vmclear(cpu);
1581 * Read loaded_vmcs->cpu should be before fetching
1582 * loaded_vmcs->loaded_vmcss_on_cpu_link.
1583 * See the comments in __loaded_vmcs_clear().
1587 list_add(&vmx->loaded_vmcs->loaded_vmcss_on_cpu_link,
1588 &per_cpu(loaded_vmcss_on_cpu, cpu));
1589 crash_enable_local_vmclear(cpu);
1593 * Linux uses per-cpu TSS and GDT, so set these when switching
1596 vmcs_writel(HOST_TR_BASE, kvm_read_tr_base()); /* 22.2.4 */
1597 vmcs_writel(HOST_GDTR_BASE, gdt->address); /* 22.2.4 */
1599 rdmsrl(MSR_IA32_SYSENTER_ESP, sysenter_esp);
1600 vmcs_writel(HOST_IA32_SYSENTER_ESP, sysenter_esp); /* 22.2.3 */
1601 vmx->loaded_vmcs->cpu = cpu;
1605 static void vmx_vcpu_put(struct kvm_vcpu *vcpu)
1607 __vmx_load_host_state(to_vmx(vcpu));
1608 if (!vmm_exclusive) {
1609 __loaded_vmcs_clear(to_vmx(vcpu)->loaded_vmcs);
1615 static void vmx_fpu_activate(struct kvm_vcpu *vcpu)
1619 if (vcpu->fpu_active)
1621 vcpu->fpu_active = 1;
1622 cr0 = vmcs_readl(GUEST_CR0);
1623 cr0 &= ~(X86_CR0_TS | X86_CR0_MP);
1624 cr0 |= kvm_read_cr0_bits(vcpu, X86_CR0_TS | X86_CR0_MP);
1625 vmcs_writel(GUEST_CR0, cr0);
1626 update_exception_bitmap(vcpu);
1627 vcpu->arch.cr0_guest_owned_bits = X86_CR0_TS;
1628 if (is_guest_mode(vcpu))
1629 vcpu->arch.cr0_guest_owned_bits &=
1630 ~get_vmcs12(vcpu)->cr0_guest_host_mask;
1631 vmcs_writel(CR0_GUEST_HOST_MASK, ~vcpu->arch.cr0_guest_owned_bits);
1634 static void vmx_decache_cr0_guest_bits(struct kvm_vcpu *vcpu);
1637 * Return the cr0 value that a nested guest would read. This is a combination
1638 * of the real cr0 used to run the guest (guest_cr0), and the bits shadowed by
1639 * its hypervisor (cr0_read_shadow).
1641 static inline unsigned long nested_read_cr0(struct vmcs12 *fields)
1643 return (fields->guest_cr0 & ~fields->cr0_guest_host_mask) |
1644 (fields->cr0_read_shadow & fields->cr0_guest_host_mask);
1646 static inline unsigned long nested_read_cr4(struct vmcs12 *fields)
1648 return (fields->guest_cr4 & ~fields->cr4_guest_host_mask) |
1649 (fields->cr4_read_shadow & fields->cr4_guest_host_mask);
1652 static void vmx_fpu_deactivate(struct kvm_vcpu *vcpu)
1654 /* Note that there is no vcpu->fpu_active = 0 here. The caller must
1655 * set this *before* calling this function.
1657 vmx_decache_cr0_guest_bits(vcpu);
1658 vmcs_set_bits(GUEST_CR0, X86_CR0_TS | X86_CR0_MP);
1659 update_exception_bitmap(vcpu);
1660 vcpu->arch.cr0_guest_owned_bits = 0;
1661 vmcs_writel(CR0_GUEST_HOST_MASK, ~vcpu->arch.cr0_guest_owned_bits);
1662 if (is_guest_mode(vcpu)) {
1664 * L1's specified read shadow might not contain the TS bit,
1665 * so now that we turned on shadowing of this bit, we need to
1666 * set this bit of the shadow. Like in nested_vmx_run we need
1667 * nested_read_cr0(vmcs12), but vmcs12->guest_cr0 is not yet
1668 * up-to-date here because we just decached cr0.TS (and we'll
1669 * only update vmcs12->guest_cr0 on nested exit).
1671 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
1672 vmcs12->guest_cr0 = (vmcs12->guest_cr0 & ~X86_CR0_TS) |
1673 (vcpu->arch.cr0 & X86_CR0_TS);
1674 vmcs_writel(CR0_READ_SHADOW, nested_read_cr0(vmcs12));
1676 vmcs_writel(CR0_READ_SHADOW, vcpu->arch.cr0);
1679 static unsigned long vmx_get_rflags(struct kvm_vcpu *vcpu)
1681 unsigned long rflags, save_rflags;
1683 if (!test_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail)) {
1684 __set_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail);
1685 rflags = vmcs_readl(GUEST_RFLAGS);
1686 if (to_vmx(vcpu)->rmode.vm86_active) {
1687 rflags &= RMODE_GUEST_OWNED_EFLAGS_BITS;
1688 save_rflags = to_vmx(vcpu)->rmode.save_rflags;
1689 rflags |= save_rflags & ~RMODE_GUEST_OWNED_EFLAGS_BITS;
1691 to_vmx(vcpu)->rflags = rflags;
1693 return to_vmx(vcpu)->rflags;
1696 static void vmx_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags)
1698 __set_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail);
1699 __clear_bit(VCPU_EXREG_CPL, (ulong *)&vcpu->arch.regs_avail);
1700 to_vmx(vcpu)->rflags = rflags;
1701 if (to_vmx(vcpu)->rmode.vm86_active) {
1702 to_vmx(vcpu)->rmode.save_rflags = rflags;
1703 rflags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM;
1705 vmcs_writel(GUEST_RFLAGS, rflags);
1708 static u32 vmx_get_interrupt_shadow(struct kvm_vcpu *vcpu, int mask)
1710 u32 interruptibility = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
1713 if (interruptibility & GUEST_INTR_STATE_STI)
1714 ret |= KVM_X86_SHADOW_INT_STI;
1715 if (interruptibility & GUEST_INTR_STATE_MOV_SS)
1716 ret |= KVM_X86_SHADOW_INT_MOV_SS;
1721 static void vmx_set_interrupt_shadow(struct kvm_vcpu *vcpu, int mask)
1723 u32 interruptibility_old = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
1724 u32 interruptibility = interruptibility_old;
1726 interruptibility &= ~(GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS);
1728 if (mask & KVM_X86_SHADOW_INT_MOV_SS)
1729 interruptibility |= GUEST_INTR_STATE_MOV_SS;
1730 else if (mask & KVM_X86_SHADOW_INT_STI)
1731 interruptibility |= GUEST_INTR_STATE_STI;
1733 if ((interruptibility != interruptibility_old))
1734 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, interruptibility);
1737 static void skip_emulated_instruction(struct kvm_vcpu *vcpu)
1741 rip = kvm_rip_read(vcpu);
1742 rip += vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
1743 kvm_rip_write(vcpu, rip);
1745 /* skipping an emulated instruction also counts */
1746 vmx_set_interrupt_shadow(vcpu, 0);
1750 * KVM wants to inject page-faults which it got to the guest. This function
1751 * checks whether in a nested guest, we need to inject them to L1 or L2.
1752 * This function assumes it is called with the exit reason in vmcs02 being
1753 * a #PF exception (this is the only case in which KVM injects a #PF when L2
1756 static int nested_pf_handled(struct kvm_vcpu *vcpu)
1758 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
1760 /* TODO: also check PFEC_MATCH/MASK, not just EB.PF. */
1761 if (!(vmcs12->exception_bitmap & (1u << PF_VECTOR)))
1764 nested_vmx_vmexit(vcpu);
1768 static void vmx_queue_exception(struct kvm_vcpu *vcpu, unsigned nr,
1769 bool has_error_code, u32 error_code,
1772 struct vcpu_vmx *vmx = to_vmx(vcpu);
1773 u32 intr_info = nr | INTR_INFO_VALID_MASK;
1775 if (nr == PF_VECTOR && is_guest_mode(vcpu) &&
1776 nested_pf_handled(vcpu))
1779 if (has_error_code) {
1780 vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE, error_code);
1781 intr_info |= INTR_INFO_DELIVER_CODE_MASK;
1784 if (vmx->rmode.vm86_active) {
1786 if (kvm_exception_is_soft(nr))
1787 inc_eip = vcpu->arch.event_exit_inst_len;
1788 if (kvm_inject_realmode_interrupt(vcpu, nr, inc_eip) != EMULATE_DONE)
1789 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
1793 if (kvm_exception_is_soft(nr)) {
1794 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
1795 vmx->vcpu.arch.event_exit_inst_len);
1796 intr_info |= INTR_TYPE_SOFT_EXCEPTION;
1798 intr_info |= INTR_TYPE_HARD_EXCEPTION;
1800 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr_info);
1803 static bool vmx_rdtscp_supported(void)
1805 return cpu_has_vmx_rdtscp();
1808 static bool vmx_invpcid_supported(void)
1810 return cpu_has_vmx_invpcid() && enable_ept;
1814 * Swap MSR entry in host/guest MSR entry array.
1816 static void move_msr_up(struct vcpu_vmx *vmx, int from, int to)
1818 struct shared_msr_entry tmp;
1820 tmp = vmx->guest_msrs[to];
1821 vmx->guest_msrs[to] = vmx->guest_msrs[from];
1822 vmx->guest_msrs[from] = tmp;
1826 * Set up the vmcs to automatically save and restore system
1827 * msrs. Don't touch the 64-bit msrs if the guest is in legacy
1828 * mode, as fiddling with msrs is very expensive.
1830 static void setup_msrs(struct vcpu_vmx *vmx)
1832 int save_nmsrs, index;
1833 unsigned long *msr_bitmap;
1836 #ifdef CONFIG_X86_64
1837 if (is_long_mode(&vmx->vcpu)) {
1838 index = __find_msr_index(vmx, MSR_SYSCALL_MASK);
1840 move_msr_up(vmx, index, save_nmsrs++);
1841 index = __find_msr_index(vmx, MSR_LSTAR);
1843 move_msr_up(vmx, index, save_nmsrs++);
1844 index = __find_msr_index(vmx, MSR_CSTAR);
1846 move_msr_up(vmx, index, save_nmsrs++);
1847 index = __find_msr_index(vmx, MSR_TSC_AUX);
1848 if (index >= 0 && vmx->rdtscp_enabled)
1849 move_msr_up(vmx, index, save_nmsrs++);
1851 * MSR_STAR is only needed on long mode guests, and only
1852 * if efer.sce is enabled.
1854 index = __find_msr_index(vmx, MSR_STAR);
1855 if ((index >= 0) && (vmx->vcpu.arch.efer & EFER_SCE))
1856 move_msr_up(vmx, index, save_nmsrs++);
1859 index = __find_msr_index(vmx, MSR_EFER);
1860 if (index >= 0 && update_transition_efer(vmx, index))
1861 move_msr_up(vmx, index, save_nmsrs++);
1863 vmx->save_nmsrs = save_nmsrs;
1865 if (cpu_has_vmx_msr_bitmap()) {
1866 if (is_long_mode(&vmx->vcpu))
1867 msr_bitmap = vmx_msr_bitmap_longmode;
1869 msr_bitmap = vmx_msr_bitmap_legacy;
1871 vmcs_write64(MSR_BITMAP, __pa(msr_bitmap));
1876 * reads and returns guest's timestamp counter "register"
1877 * guest_tsc = host_tsc + tsc_offset -- 21.3
1879 static u64 guest_read_tsc(void)
1881 u64 host_tsc, tsc_offset;
1884 tsc_offset = vmcs_read64(TSC_OFFSET);
1885 return host_tsc + tsc_offset;
1889 * Like guest_read_tsc, but always returns L1's notion of the timestamp
1890 * counter, even if a nested guest (L2) is currently running.
1892 u64 vmx_read_l1_tsc(struct kvm_vcpu *vcpu, u64 host_tsc)
1896 tsc_offset = is_guest_mode(vcpu) ?
1897 to_vmx(vcpu)->nested.vmcs01_tsc_offset :
1898 vmcs_read64(TSC_OFFSET);
1899 return host_tsc + tsc_offset;
1903 * Engage any workarounds for mis-matched TSC rates. Currently limited to
1904 * software catchup for faster rates on slower CPUs.
1906 static void vmx_set_tsc_khz(struct kvm_vcpu *vcpu, u32 user_tsc_khz, bool scale)
1911 if (user_tsc_khz > tsc_khz) {
1912 vcpu->arch.tsc_catchup = 1;
1913 vcpu->arch.tsc_always_catchup = 1;
1915 WARN(1, "user requested TSC rate below hardware speed\n");
1918 static u64 vmx_read_tsc_offset(struct kvm_vcpu *vcpu)
1920 return vmcs_read64(TSC_OFFSET);
1924 * writes 'offset' into guest's timestamp counter offset register
1926 static void vmx_write_tsc_offset(struct kvm_vcpu *vcpu, u64 offset)
1928 if (is_guest_mode(vcpu)) {
1930 * We're here if L1 chose not to trap WRMSR to TSC. According
1931 * to the spec, this should set L1's TSC; The offset that L1
1932 * set for L2 remains unchanged, and still needs to be added
1933 * to the newly set TSC to get L2's TSC.
1935 struct vmcs12 *vmcs12;
1936 to_vmx(vcpu)->nested.vmcs01_tsc_offset = offset;
1937 /* recalculate vmcs02.TSC_OFFSET: */
1938 vmcs12 = get_vmcs12(vcpu);
1939 vmcs_write64(TSC_OFFSET, offset +
1940 (nested_cpu_has(vmcs12, CPU_BASED_USE_TSC_OFFSETING) ?
1941 vmcs12->tsc_offset : 0));
1943 vmcs_write64(TSC_OFFSET, offset);
1947 static void vmx_adjust_tsc_offset(struct kvm_vcpu *vcpu, s64 adjustment, bool host)
1949 u64 offset = vmcs_read64(TSC_OFFSET);
1950 vmcs_write64(TSC_OFFSET, offset + adjustment);
1951 if (is_guest_mode(vcpu)) {
1952 /* Even when running L2, the adjustment needs to apply to L1 */
1953 to_vmx(vcpu)->nested.vmcs01_tsc_offset += adjustment;
1957 static u64 vmx_compute_tsc_offset(struct kvm_vcpu *vcpu, u64 target_tsc)
1959 return target_tsc - native_read_tsc();
1962 static bool guest_cpuid_has_vmx(struct kvm_vcpu *vcpu)
1964 struct kvm_cpuid_entry2 *best = kvm_find_cpuid_entry(vcpu, 1, 0);
1965 return best && (best->ecx & (1 << (X86_FEATURE_VMX & 31)));
1969 * nested_vmx_allowed() checks whether a guest should be allowed to use VMX
1970 * instructions and MSRs (i.e., nested VMX). Nested VMX is disabled for
1971 * all guests if the "nested" module option is off, and can also be disabled
1972 * for a single guest by disabling its VMX cpuid bit.
1974 static inline bool nested_vmx_allowed(struct kvm_vcpu *vcpu)
1976 return nested && guest_cpuid_has_vmx(vcpu);
1980 * nested_vmx_setup_ctls_msrs() sets up variables containing the values to be
1981 * returned for the various VMX controls MSRs when nested VMX is enabled.
1982 * The same values should also be used to verify that vmcs12 control fields are
1983 * valid during nested entry from L1 to L2.
1984 * Each of these control msrs has a low and high 32-bit half: A low bit is on
1985 * if the corresponding bit in the (32-bit) control field *must* be on, and a
1986 * bit in the high half is on if the corresponding bit in the control field
1987 * may be on. See also vmx_control_verify().
1988 * TODO: allow these variables to be modified (downgraded) by module options
1991 static u32 nested_vmx_procbased_ctls_low, nested_vmx_procbased_ctls_high;
1992 static u32 nested_vmx_secondary_ctls_low, nested_vmx_secondary_ctls_high;
1993 static u32 nested_vmx_pinbased_ctls_low, nested_vmx_pinbased_ctls_high;
1994 static u32 nested_vmx_exit_ctls_low, nested_vmx_exit_ctls_high;
1995 static u32 nested_vmx_entry_ctls_low, nested_vmx_entry_ctls_high;
1996 static __init void nested_vmx_setup_ctls_msrs(void)
1999 * Note that as a general rule, the high half of the MSRs (bits in
2000 * the control fields which may be 1) should be initialized by the
2001 * intersection of the underlying hardware's MSR (i.e., features which
2002 * can be supported) and the list of features we want to expose -
2003 * because they are known to be properly supported in our code.
2004 * Also, usually, the low half of the MSRs (bits which must be 1) can
2005 * be set to 0, meaning that L1 may turn off any of these bits. The
2006 * reason is that if one of these bits is necessary, it will appear
2007 * in vmcs01 and prepare_vmcs02, when it bitwise-or's the control
2008 * fields of vmcs01 and vmcs02, will turn these bits off - and
2009 * nested_vmx_exit_handled() will not pass related exits to L1.
2010 * These rules have exceptions below.
2013 /* pin-based controls */
2015 * According to the Intel spec, if bit 55 of VMX_BASIC is off (as it is
2016 * in our case), bits 1, 2 and 4 (i.e., 0x16) must be 1 in this MSR.
2018 nested_vmx_pinbased_ctls_low = 0x16 ;
2019 nested_vmx_pinbased_ctls_high = 0x16 |
2020 PIN_BASED_EXT_INTR_MASK | PIN_BASED_NMI_EXITING |
2021 PIN_BASED_VIRTUAL_NMIS;
2024 nested_vmx_exit_ctls_low = 0;
2025 /* Note that guest use of VM_EXIT_ACK_INTR_ON_EXIT is not supported. */
2026 #ifdef CONFIG_X86_64
2027 nested_vmx_exit_ctls_high = VM_EXIT_HOST_ADDR_SPACE_SIZE;
2029 nested_vmx_exit_ctls_high = 0;
2032 /* entry controls */
2033 rdmsr(MSR_IA32_VMX_ENTRY_CTLS,
2034 nested_vmx_entry_ctls_low, nested_vmx_entry_ctls_high);
2035 nested_vmx_entry_ctls_low = 0;
2036 nested_vmx_entry_ctls_high &=
2037 VM_ENTRY_LOAD_IA32_PAT | VM_ENTRY_IA32E_MODE;
2039 /* cpu-based controls */
2040 rdmsr(MSR_IA32_VMX_PROCBASED_CTLS,
2041 nested_vmx_procbased_ctls_low, nested_vmx_procbased_ctls_high);
2042 nested_vmx_procbased_ctls_low = 0;
2043 nested_vmx_procbased_ctls_high &=
2044 CPU_BASED_VIRTUAL_INTR_PENDING | CPU_BASED_USE_TSC_OFFSETING |
2045 CPU_BASED_HLT_EXITING | CPU_BASED_INVLPG_EXITING |
2046 CPU_BASED_MWAIT_EXITING | CPU_BASED_CR3_LOAD_EXITING |
2047 CPU_BASED_CR3_STORE_EXITING |
2048 #ifdef CONFIG_X86_64
2049 CPU_BASED_CR8_LOAD_EXITING | CPU_BASED_CR8_STORE_EXITING |
2051 CPU_BASED_MOV_DR_EXITING | CPU_BASED_UNCOND_IO_EXITING |
2052 CPU_BASED_USE_IO_BITMAPS | CPU_BASED_MONITOR_EXITING |
2053 CPU_BASED_RDPMC_EXITING | CPU_BASED_RDTSC_EXITING |
2054 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
2056 * We can allow some features even when not supported by the
2057 * hardware. For example, L1 can specify an MSR bitmap - and we
2058 * can use it to avoid exits to L1 - even when L0 runs L2
2059 * without MSR bitmaps.
2061 nested_vmx_procbased_ctls_high |= CPU_BASED_USE_MSR_BITMAPS;
2063 /* secondary cpu-based controls */
2064 rdmsr(MSR_IA32_VMX_PROCBASED_CTLS2,
2065 nested_vmx_secondary_ctls_low, nested_vmx_secondary_ctls_high);
2066 nested_vmx_secondary_ctls_low = 0;
2067 nested_vmx_secondary_ctls_high &=
2068 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
2071 static inline bool vmx_control_verify(u32 control, u32 low, u32 high)
2074 * Bits 0 in high must be 0, and bits 1 in low must be 1.
2076 return ((control & high) | low) == control;
2079 static inline u64 vmx_control_msr(u32 low, u32 high)
2081 return low | ((u64)high << 32);
2085 * If we allow our guest to use VMX instructions (i.e., nested VMX), we should
2086 * also let it use VMX-specific MSRs.
2087 * vmx_get_vmx_msr() and vmx_set_vmx_msr() return 1 when we handled a
2088 * VMX-specific MSR, or 0 when we haven't (and the caller should handle it
2089 * like all other MSRs).
2091 static int vmx_get_vmx_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 *pdata)
2093 if (!nested_vmx_allowed(vcpu) && msr_index >= MSR_IA32_VMX_BASIC &&
2094 msr_index <= MSR_IA32_VMX_TRUE_ENTRY_CTLS) {
2096 * According to the spec, processors which do not support VMX
2097 * should throw a #GP(0) when VMX capability MSRs are read.
2099 kvm_queue_exception_e(vcpu, GP_VECTOR, 0);
2103 switch (msr_index) {
2104 case MSR_IA32_FEATURE_CONTROL:
2107 case MSR_IA32_VMX_BASIC:
2109 * This MSR reports some information about VMX support. We
2110 * should return information about the VMX we emulate for the
2111 * guest, and the VMCS structure we give it - not about the
2112 * VMX support of the underlying hardware.
2114 *pdata = VMCS12_REVISION |
2115 ((u64)VMCS12_SIZE << VMX_BASIC_VMCS_SIZE_SHIFT) |
2116 (VMX_BASIC_MEM_TYPE_WB << VMX_BASIC_MEM_TYPE_SHIFT);
2118 case MSR_IA32_VMX_TRUE_PINBASED_CTLS:
2119 case MSR_IA32_VMX_PINBASED_CTLS:
2120 *pdata = vmx_control_msr(nested_vmx_pinbased_ctls_low,
2121 nested_vmx_pinbased_ctls_high);
2123 case MSR_IA32_VMX_TRUE_PROCBASED_CTLS:
2124 case MSR_IA32_VMX_PROCBASED_CTLS:
2125 *pdata = vmx_control_msr(nested_vmx_procbased_ctls_low,
2126 nested_vmx_procbased_ctls_high);
2128 case MSR_IA32_VMX_TRUE_EXIT_CTLS:
2129 case MSR_IA32_VMX_EXIT_CTLS:
2130 *pdata = vmx_control_msr(nested_vmx_exit_ctls_low,
2131 nested_vmx_exit_ctls_high);
2133 case MSR_IA32_VMX_TRUE_ENTRY_CTLS:
2134 case MSR_IA32_VMX_ENTRY_CTLS:
2135 *pdata = vmx_control_msr(nested_vmx_entry_ctls_low,
2136 nested_vmx_entry_ctls_high);
2138 case MSR_IA32_VMX_MISC:
2142 * These MSRs specify bits which the guest must keep fixed (on or off)
2143 * while L1 is in VMXON mode (in L1's root mode, or running an L2).
2144 * We picked the standard core2 setting.
2146 #define VMXON_CR0_ALWAYSON (X86_CR0_PE | X86_CR0_PG | X86_CR0_NE)
2147 #define VMXON_CR4_ALWAYSON X86_CR4_VMXE
2148 case MSR_IA32_VMX_CR0_FIXED0:
2149 *pdata = VMXON_CR0_ALWAYSON;
2151 case MSR_IA32_VMX_CR0_FIXED1:
2154 case MSR_IA32_VMX_CR4_FIXED0:
2155 *pdata = VMXON_CR4_ALWAYSON;
2157 case MSR_IA32_VMX_CR4_FIXED1:
2160 case MSR_IA32_VMX_VMCS_ENUM:
2163 case MSR_IA32_VMX_PROCBASED_CTLS2:
2164 *pdata = vmx_control_msr(nested_vmx_secondary_ctls_low,
2165 nested_vmx_secondary_ctls_high);
2167 case MSR_IA32_VMX_EPT_VPID_CAP:
2168 /* Currently, no nested ept or nested vpid */
2178 static int vmx_set_vmx_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 data)
2180 if (!nested_vmx_allowed(vcpu))
2183 if (msr_index == MSR_IA32_FEATURE_CONTROL)
2184 /* TODO: the right thing. */
2187 * No need to treat VMX capability MSRs specially: If we don't handle
2188 * them, handle_wrmsr will #GP(0), which is correct (they are readonly)
2194 * Reads an msr value (of 'msr_index') into 'pdata'.
2195 * Returns 0 on success, non-0 otherwise.
2196 * Assumes vcpu_load() was already called.
2198 static int vmx_get_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 *pdata)
2201 struct shared_msr_entry *msr;
2204 printk(KERN_ERR "BUG: get_msr called with NULL pdata\n");
2208 switch (msr_index) {
2209 #ifdef CONFIG_X86_64
2211 data = vmcs_readl(GUEST_FS_BASE);
2214 data = vmcs_readl(GUEST_GS_BASE);
2216 case MSR_KERNEL_GS_BASE:
2217 vmx_load_host_state(to_vmx(vcpu));
2218 data = to_vmx(vcpu)->msr_guest_kernel_gs_base;
2222 return kvm_get_msr_common(vcpu, msr_index, pdata);
2224 data = guest_read_tsc();
2226 case MSR_IA32_SYSENTER_CS:
2227 data = vmcs_read32(GUEST_SYSENTER_CS);
2229 case MSR_IA32_SYSENTER_EIP:
2230 data = vmcs_readl(GUEST_SYSENTER_EIP);
2232 case MSR_IA32_SYSENTER_ESP:
2233 data = vmcs_readl(GUEST_SYSENTER_ESP);
2236 if (!to_vmx(vcpu)->rdtscp_enabled)
2238 /* Otherwise falls through */
2240 if (vmx_get_vmx_msr(vcpu, msr_index, pdata))
2242 msr = find_msr_entry(to_vmx(vcpu), msr_index);
2247 return kvm_get_msr_common(vcpu, msr_index, pdata);
2255 * Writes msr value into into the appropriate "register".
2256 * Returns 0 on success, non-0 otherwise.
2257 * Assumes vcpu_load() was already called.
2259 static int vmx_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
2261 struct vcpu_vmx *vmx = to_vmx(vcpu);
2262 struct shared_msr_entry *msr;
2264 u32 msr_index = msr_info->index;
2265 u64 data = msr_info->data;
2267 switch (msr_index) {
2269 ret = kvm_set_msr_common(vcpu, msr_info);
2271 #ifdef CONFIG_X86_64
2273 vmx_segment_cache_clear(vmx);
2274 vmcs_writel(GUEST_FS_BASE, data);
2277 vmx_segment_cache_clear(vmx);
2278 vmcs_writel(GUEST_GS_BASE, data);
2280 case MSR_KERNEL_GS_BASE:
2281 vmx_load_host_state(vmx);
2282 vmx->msr_guest_kernel_gs_base = data;
2285 case MSR_IA32_SYSENTER_CS:
2286 vmcs_write32(GUEST_SYSENTER_CS, data);
2288 case MSR_IA32_SYSENTER_EIP:
2289 vmcs_writel(GUEST_SYSENTER_EIP, data);
2291 case MSR_IA32_SYSENTER_ESP:
2292 vmcs_writel(GUEST_SYSENTER_ESP, data);
2295 kvm_write_tsc(vcpu, msr_info);
2297 case MSR_IA32_CR_PAT:
2298 if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) {
2299 vmcs_write64(GUEST_IA32_PAT, data);
2300 vcpu->arch.pat = data;
2303 ret = kvm_set_msr_common(vcpu, msr_info);
2305 case MSR_IA32_TSC_ADJUST:
2306 ret = kvm_set_msr_common(vcpu, msr_info);
2309 if (!vmx->rdtscp_enabled)
2311 /* Check reserved bit, higher 32 bits should be zero */
2312 if ((data >> 32) != 0)
2314 /* Otherwise falls through */
2316 if (vmx_set_vmx_msr(vcpu, msr_index, data))
2318 msr = find_msr_entry(vmx, msr_index);
2321 if (msr - vmx->guest_msrs < vmx->save_nmsrs) {
2323 kvm_set_shared_msr(msr->index, msr->data,
2329 ret = kvm_set_msr_common(vcpu, msr_info);
2335 static void vmx_cache_reg(struct kvm_vcpu *vcpu, enum kvm_reg reg)
2337 __set_bit(reg, (unsigned long *)&vcpu->arch.regs_avail);
2340 vcpu->arch.regs[VCPU_REGS_RSP] = vmcs_readl(GUEST_RSP);
2343 vcpu->arch.regs[VCPU_REGS_RIP] = vmcs_readl(GUEST_RIP);
2345 case VCPU_EXREG_PDPTR:
2347 ept_save_pdptrs(vcpu);
2354 static __init int cpu_has_kvm_support(void)
2356 return cpu_has_vmx();
2359 static __init int vmx_disabled_by_bios(void)
2363 rdmsrl(MSR_IA32_FEATURE_CONTROL, msr);
2364 if (msr & FEATURE_CONTROL_LOCKED) {
2365 /* launched w/ TXT and VMX disabled */
2366 if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX)
2369 /* launched w/o TXT and VMX only enabled w/ TXT */
2370 if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX)
2371 && (msr & FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX)
2372 && !tboot_enabled()) {
2373 printk(KERN_WARNING "kvm: disable TXT in the BIOS or "
2374 "activate TXT before enabling KVM\n");
2377 /* launched w/o TXT and VMX disabled */
2378 if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX)
2379 && !tboot_enabled())
2386 static void kvm_cpu_vmxon(u64 addr)
2388 asm volatile (ASM_VMX_VMXON_RAX
2389 : : "a"(&addr), "m"(addr)
2393 static int hardware_enable(void *garbage)
2395 int cpu = raw_smp_processor_id();
2396 u64 phys_addr = __pa(per_cpu(vmxarea, cpu));
2399 if (read_cr4() & X86_CR4_VMXE)
2402 INIT_LIST_HEAD(&per_cpu(loaded_vmcss_on_cpu, cpu));
2405 * Now we can enable the vmclear operation in kdump
2406 * since the loaded_vmcss_on_cpu list on this cpu
2407 * has been initialized.
2409 * Though the cpu is not in VMX operation now, there
2410 * is no problem to enable the vmclear operation
2411 * for the loaded_vmcss_on_cpu list is empty!
2413 crash_enable_local_vmclear(cpu);
2415 rdmsrl(MSR_IA32_FEATURE_CONTROL, old);
2417 test_bits = FEATURE_CONTROL_LOCKED;
2418 test_bits |= FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
2419 if (tboot_enabled())
2420 test_bits |= FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX;
2422 if ((old & test_bits) != test_bits) {
2423 /* enable and lock */
2424 wrmsrl(MSR_IA32_FEATURE_CONTROL, old | test_bits);
2426 write_cr4(read_cr4() | X86_CR4_VMXE); /* FIXME: not cpu hotplug safe */
2428 if (vmm_exclusive) {
2429 kvm_cpu_vmxon(phys_addr);
2433 store_gdt(&__get_cpu_var(host_gdt));
2438 static void vmclear_local_loaded_vmcss(void)
2440 int cpu = raw_smp_processor_id();
2441 struct loaded_vmcs *v, *n;
2443 list_for_each_entry_safe(v, n, &per_cpu(loaded_vmcss_on_cpu, cpu),
2444 loaded_vmcss_on_cpu_link)
2445 __loaded_vmcs_clear(v);
2449 /* Just like cpu_vmxoff(), but with the __kvm_handle_fault_on_reboot()
2452 static void kvm_cpu_vmxoff(void)
2454 asm volatile (__ex(ASM_VMX_VMXOFF) : : : "cc");
2457 static void hardware_disable(void *garbage)
2459 if (vmm_exclusive) {
2460 vmclear_local_loaded_vmcss();
2463 write_cr4(read_cr4() & ~X86_CR4_VMXE);
2466 static __init int adjust_vmx_controls(u32 ctl_min, u32 ctl_opt,
2467 u32 msr, u32 *result)
2469 u32 vmx_msr_low, vmx_msr_high;
2470 u32 ctl = ctl_min | ctl_opt;
2472 rdmsr(msr, vmx_msr_low, vmx_msr_high);
2474 ctl &= vmx_msr_high; /* bit == 0 in high word ==> must be zero */
2475 ctl |= vmx_msr_low; /* bit == 1 in low word ==> must be one */
2477 /* Ensure minimum (required) set of control bits are supported. */
2485 static __init bool allow_1_setting(u32 msr, u32 ctl)
2487 u32 vmx_msr_low, vmx_msr_high;
2489 rdmsr(msr, vmx_msr_low, vmx_msr_high);
2490 return vmx_msr_high & ctl;
2493 static __init int setup_vmcs_config(struct vmcs_config *vmcs_conf)
2495 u32 vmx_msr_low, vmx_msr_high;
2496 u32 min, opt, min2, opt2;
2497 u32 _pin_based_exec_control = 0;
2498 u32 _cpu_based_exec_control = 0;
2499 u32 _cpu_based_2nd_exec_control = 0;
2500 u32 _vmexit_control = 0;
2501 u32 _vmentry_control = 0;
2503 min = PIN_BASED_EXT_INTR_MASK | PIN_BASED_NMI_EXITING;
2504 opt = PIN_BASED_VIRTUAL_NMIS;
2505 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PINBASED_CTLS,
2506 &_pin_based_exec_control) < 0)
2509 min = CPU_BASED_HLT_EXITING |
2510 #ifdef CONFIG_X86_64
2511 CPU_BASED_CR8_LOAD_EXITING |
2512 CPU_BASED_CR8_STORE_EXITING |
2514 CPU_BASED_CR3_LOAD_EXITING |
2515 CPU_BASED_CR3_STORE_EXITING |
2516 CPU_BASED_USE_IO_BITMAPS |
2517 CPU_BASED_MOV_DR_EXITING |
2518 CPU_BASED_USE_TSC_OFFSETING |
2519 CPU_BASED_MWAIT_EXITING |
2520 CPU_BASED_MONITOR_EXITING |
2521 CPU_BASED_INVLPG_EXITING |
2522 CPU_BASED_RDPMC_EXITING;
2524 opt = CPU_BASED_TPR_SHADOW |
2525 CPU_BASED_USE_MSR_BITMAPS |
2526 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
2527 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PROCBASED_CTLS,
2528 &_cpu_based_exec_control) < 0)
2530 #ifdef CONFIG_X86_64
2531 if ((_cpu_based_exec_control & CPU_BASED_TPR_SHADOW))
2532 _cpu_based_exec_control &= ~CPU_BASED_CR8_LOAD_EXITING &
2533 ~CPU_BASED_CR8_STORE_EXITING;
2535 if (_cpu_based_exec_control & CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) {
2537 opt2 = SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
2538 SECONDARY_EXEC_WBINVD_EXITING |
2539 SECONDARY_EXEC_ENABLE_VPID |
2540 SECONDARY_EXEC_ENABLE_EPT |
2541 SECONDARY_EXEC_UNRESTRICTED_GUEST |
2542 SECONDARY_EXEC_PAUSE_LOOP_EXITING |
2543 SECONDARY_EXEC_RDTSCP |
2544 SECONDARY_EXEC_ENABLE_INVPCID;
2545 if (adjust_vmx_controls(min2, opt2,
2546 MSR_IA32_VMX_PROCBASED_CTLS2,
2547 &_cpu_based_2nd_exec_control) < 0)
2550 #ifndef CONFIG_X86_64
2551 if (!(_cpu_based_2nd_exec_control &
2552 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES))
2553 _cpu_based_exec_control &= ~CPU_BASED_TPR_SHADOW;
2555 if (_cpu_based_2nd_exec_control & SECONDARY_EXEC_ENABLE_EPT) {
2556 /* CR3 accesses and invlpg don't need to cause VM Exits when EPT
2558 _cpu_based_exec_control &= ~(CPU_BASED_CR3_LOAD_EXITING |
2559 CPU_BASED_CR3_STORE_EXITING |
2560 CPU_BASED_INVLPG_EXITING);
2561 rdmsr(MSR_IA32_VMX_EPT_VPID_CAP,
2562 vmx_capability.ept, vmx_capability.vpid);
2566 #ifdef CONFIG_X86_64
2567 min |= VM_EXIT_HOST_ADDR_SPACE_SIZE;
2569 opt = VM_EXIT_SAVE_IA32_PAT | VM_EXIT_LOAD_IA32_PAT;
2570 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_EXIT_CTLS,
2571 &_vmexit_control) < 0)
2575 opt = VM_ENTRY_LOAD_IA32_PAT;
2576 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_ENTRY_CTLS,
2577 &_vmentry_control) < 0)
2580 rdmsr(MSR_IA32_VMX_BASIC, vmx_msr_low, vmx_msr_high);
2582 /* IA-32 SDM Vol 3B: VMCS size is never greater than 4kB. */
2583 if ((vmx_msr_high & 0x1fff) > PAGE_SIZE)
2586 #ifdef CONFIG_X86_64
2587 /* IA-32 SDM Vol 3B: 64-bit CPUs always have VMX_BASIC_MSR[48]==0. */
2588 if (vmx_msr_high & (1u<<16))
2592 /* Require Write-Back (WB) memory type for VMCS accesses. */
2593 if (((vmx_msr_high >> 18) & 15) != 6)
2596 vmcs_conf->size = vmx_msr_high & 0x1fff;
2597 vmcs_conf->order = get_order(vmcs_config.size);
2598 vmcs_conf->revision_id = vmx_msr_low;
2600 vmcs_conf->pin_based_exec_ctrl = _pin_based_exec_control;
2601 vmcs_conf->cpu_based_exec_ctrl = _cpu_based_exec_control;
2602 vmcs_conf->cpu_based_2nd_exec_ctrl = _cpu_based_2nd_exec_control;
2603 vmcs_conf->vmexit_ctrl = _vmexit_control;
2604 vmcs_conf->vmentry_ctrl = _vmentry_control;
2606 cpu_has_load_ia32_efer =
2607 allow_1_setting(MSR_IA32_VMX_ENTRY_CTLS,
2608 VM_ENTRY_LOAD_IA32_EFER)
2609 && allow_1_setting(MSR_IA32_VMX_EXIT_CTLS,
2610 VM_EXIT_LOAD_IA32_EFER);
2612 cpu_has_load_perf_global_ctrl =
2613 allow_1_setting(MSR_IA32_VMX_ENTRY_CTLS,
2614 VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL)
2615 && allow_1_setting(MSR_IA32_VMX_EXIT_CTLS,
2616 VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL);
2619 * Some cpus support VM_ENTRY_(LOAD|SAVE)_IA32_PERF_GLOBAL_CTRL
2620 * but due to arrata below it can't be used. Workaround is to use
2621 * msr load mechanism to switch IA32_PERF_GLOBAL_CTRL.
2623 * VM Exit May Incorrectly Clear IA32_PERF_GLOBAL_CTRL [34:32]
2628 * BC86,AAY89,BD102 (model 44)
2632 if (cpu_has_load_perf_global_ctrl && boot_cpu_data.x86 == 0x6) {
2633 switch (boot_cpu_data.x86_model) {
2639 cpu_has_load_perf_global_ctrl = false;
2640 printk_once(KERN_WARNING"kvm: VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL "
2641 "does not work properly. Using workaround\n");
2651 static struct vmcs *alloc_vmcs_cpu(int cpu)
2653 int node = cpu_to_node(cpu);
2657 pages = alloc_pages_exact_node(node, GFP_KERNEL, vmcs_config.order);
2660 vmcs = page_address(pages);
2661 memset(vmcs, 0, vmcs_config.size);
2662 vmcs->revision_id = vmcs_config.revision_id; /* vmcs revision id */
2666 static struct vmcs *alloc_vmcs(void)
2668 return alloc_vmcs_cpu(raw_smp_processor_id());
2671 static void free_vmcs(struct vmcs *vmcs)
2673 free_pages((unsigned long)vmcs, vmcs_config.order);
2677 * Free a VMCS, but before that VMCLEAR it on the CPU where it was last loaded
2679 static void free_loaded_vmcs(struct loaded_vmcs *loaded_vmcs)
2681 if (!loaded_vmcs->vmcs)
2683 loaded_vmcs_clear(loaded_vmcs);
2684 free_vmcs(loaded_vmcs->vmcs);
2685 loaded_vmcs->vmcs = NULL;
2688 static void free_kvm_area(void)
2692 for_each_possible_cpu(cpu) {
2693 free_vmcs(per_cpu(vmxarea, cpu));
2694 per_cpu(vmxarea, cpu) = NULL;
2698 static __init int alloc_kvm_area(void)
2702 for_each_possible_cpu(cpu) {
2705 vmcs = alloc_vmcs_cpu(cpu);
2711 per_cpu(vmxarea, cpu) = vmcs;
2716 static __init int hardware_setup(void)
2718 if (setup_vmcs_config(&vmcs_config) < 0)
2721 if (boot_cpu_has(X86_FEATURE_NX))
2722 kvm_enable_efer_bits(EFER_NX);
2724 if (!cpu_has_vmx_vpid())
2727 if (!cpu_has_vmx_ept() ||
2728 !cpu_has_vmx_ept_4levels()) {
2730 enable_unrestricted_guest = 0;
2731 enable_ept_ad_bits = 0;
2734 if (!cpu_has_vmx_ept_ad_bits())
2735 enable_ept_ad_bits = 0;
2737 if (!cpu_has_vmx_unrestricted_guest())
2738 enable_unrestricted_guest = 0;
2740 if (!cpu_has_vmx_flexpriority())
2741 flexpriority_enabled = 0;
2743 if (!cpu_has_vmx_tpr_shadow())
2744 kvm_x86_ops->update_cr8_intercept = NULL;
2746 if (enable_ept && !cpu_has_vmx_ept_2m_page())
2747 kvm_disable_largepages();
2749 if (!cpu_has_vmx_ple())
2753 nested_vmx_setup_ctls_msrs();
2755 return alloc_kvm_area();
2758 static __exit void hardware_unsetup(void)
2763 static void fix_pmode_dataseg(struct kvm_vcpu *vcpu, int seg,
2764 struct kvm_segment *save)
2766 if (!emulate_invalid_guest_state) {
2768 * CS and SS RPL should be equal during guest entry according
2769 * to VMX spec, but in reality it is not always so. Since vcpu
2770 * is in the middle of the transition from real mode to
2771 * protected mode it is safe to assume that RPL 0 is a good
2774 if (seg == VCPU_SREG_CS || seg == VCPU_SREG_SS)
2775 save->selector &= ~SELECTOR_RPL_MASK;
2776 save->dpl = save->selector & SELECTOR_RPL_MASK;
2779 vmx_set_segment(vcpu, save, seg);
2782 static void enter_pmode(struct kvm_vcpu *vcpu)
2784 unsigned long flags;
2785 struct vcpu_vmx *vmx = to_vmx(vcpu);
2788 * Update real mode segment cache. It may be not up-to-date if sement
2789 * register was written while vcpu was in a guest mode.
2791 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_ES], VCPU_SREG_ES);
2792 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_DS], VCPU_SREG_DS);
2793 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_FS], VCPU_SREG_FS);
2794 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_GS], VCPU_SREG_GS);
2795 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_SS], VCPU_SREG_SS);
2796 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_CS], VCPU_SREG_CS);
2798 vmx->emulation_required = 1;
2799 vmx->rmode.vm86_active = 0;
2801 vmx_segment_cache_clear(vmx);
2803 vmx_set_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_TR], VCPU_SREG_TR);
2805 flags = vmcs_readl(GUEST_RFLAGS);
2806 flags &= RMODE_GUEST_OWNED_EFLAGS_BITS;
2807 flags |= vmx->rmode.save_rflags & ~RMODE_GUEST_OWNED_EFLAGS_BITS;
2808 vmcs_writel(GUEST_RFLAGS, flags);
2810 vmcs_writel(GUEST_CR4, (vmcs_readl(GUEST_CR4) & ~X86_CR4_VME) |
2811 (vmcs_readl(CR4_READ_SHADOW) & X86_CR4_VME));
2813 update_exception_bitmap(vcpu);
2815 fix_pmode_dataseg(vcpu, VCPU_SREG_CS, &vmx->rmode.segs[VCPU_SREG_CS]);
2816 fix_pmode_dataseg(vcpu, VCPU_SREG_SS, &vmx->rmode.segs[VCPU_SREG_SS]);
2817 fix_pmode_dataseg(vcpu, VCPU_SREG_ES, &vmx->rmode.segs[VCPU_SREG_ES]);
2818 fix_pmode_dataseg(vcpu, VCPU_SREG_DS, &vmx->rmode.segs[VCPU_SREG_DS]);
2819 fix_pmode_dataseg(vcpu, VCPU_SREG_FS, &vmx->rmode.segs[VCPU_SREG_FS]);
2820 fix_pmode_dataseg(vcpu, VCPU_SREG_GS, &vmx->rmode.segs[VCPU_SREG_GS]);
2823 static gva_t rmode_tss_base(struct kvm *kvm)
2825 if (!kvm->arch.tss_addr) {
2826 struct kvm_memslots *slots;
2827 struct kvm_memory_slot *slot;
2830 slots = kvm_memslots(kvm);
2831 slot = id_to_memslot(slots, 0);
2832 base_gfn = slot->base_gfn + slot->npages - 3;
2834 return base_gfn << PAGE_SHIFT;
2836 return kvm->arch.tss_addr;
2839 static void fix_rmode_seg(int seg, struct kvm_segment *save)
2841 const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
2842 struct kvm_segment var = *save;
2845 if (seg == VCPU_SREG_CS)
2848 if (!emulate_invalid_guest_state) {
2849 var.selector = var.base >> 4;
2850 var.base = var.base & 0xffff0;
2860 if (save->base & 0xf)
2861 printk_once(KERN_WARNING "kvm: segment base is not "
2862 "paragraph aligned when entering "
2863 "protected mode (seg=%d)", seg);
2866 vmcs_write16(sf->selector, var.selector);
2867 vmcs_write32(sf->base, var.base);
2868 vmcs_write32(sf->limit, var.limit);
2869 vmcs_write32(sf->ar_bytes, vmx_segment_access_rights(&var));
2872 static void enter_rmode(struct kvm_vcpu *vcpu)
2874 unsigned long flags;
2875 struct vcpu_vmx *vmx = to_vmx(vcpu);
2877 if (enable_unrestricted_guest)
2880 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_TR], VCPU_SREG_TR);
2881 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_ES], VCPU_SREG_ES);
2882 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_DS], VCPU_SREG_DS);
2883 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_FS], VCPU_SREG_FS);
2884 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_GS], VCPU_SREG_GS);
2885 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_SS], VCPU_SREG_SS);
2886 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_CS], VCPU_SREG_CS);
2888 vmx->emulation_required = 1;
2889 vmx->rmode.vm86_active = 1;
2892 * Very old userspace does not call KVM_SET_TSS_ADDR before entering
2893 * vcpu. Call it here with phys address pointing 16M below 4G.
2895 if (!vcpu->kvm->arch.tss_addr) {
2896 printk_once(KERN_WARNING "kvm: KVM_SET_TSS_ADDR need to be "
2897 "called before entering vcpu\n");
2898 srcu_read_unlock(&vcpu->kvm->srcu, vcpu->srcu_idx);
2899 vmx_set_tss_addr(vcpu->kvm, 0xfeffd000);
2900 vcpu->srcu_idx = srcu_read_lock(&vcpu->kvm->srcu);
2903 vmx_segment_cache_clear(vmx);
2905 vmcs_writel(GUEST_TR_BASE, rmode_tss_base(vcpu->kvm));
2906 vmcs_write32(GUEST_TR_LIMIT, RMODE_TSS_SIZE - 1);
2907 vmcs_write32(GUEST_TR_AR_BYTES, 0x008b);
2909 flags = vmcs_readl(GUEST_RFLAGS);
2910 vmx->rmode.save_rflags = flags;
2912 flags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM;
2914 vmcs_writel(GUEST_RFLAGS, flags);
2915 vmcs_writel(GUEST_CR4, vmcs_readl(GUEST_CR4) | X86_CR4_VME);
2916 update_exception_bitmap(vcpu);
2918 fix_rmode_seg(VCPU_SREG_SS, &vmx->rmode.segs[VCPU_SREG_SS]);
2919 fix_rmode_seg(VCPU_SREG_CS, &vmx->rmode.segs[VCPU_SREG_CS]);
2920 fix_rmode_seg(VCPU_SREG_ES, &vmx->rmode.segs[VCPU_SREG_ES]);
2921 fix_rmode_seg(VCPU_SREG_DS, &vmx->rmode.segs[VCPU_SREG_DS]);
2922 fix_rmode_seg(VCPU_SREG_GS, &vmx->rmode.segs[VCPU_SREG_GS]);
2923 fix_rmode_seg(VCPU_SREG_FS, &vmx->rmode.segs[VCPU_SREG_FS]);
2925 kvm_mmu_reset_context(vcpu);
2928 static void vmx_set_efer(struct kvm_vcpu *vcpu, u64 efer)
2930 struct vcpu_vmx *vmx = to_vmx(vcpu);
2931 struct shared_msr_entry *msr = find_msr_entry(vmx, MSR_EFER);
2937 * Force kernel_gs_base reloading before EFER changes, as control
2938 * of this msr depends on is_long_mode().
2940 vmx_load_host_state(to_vmx(vcpu));
2941 vcpu->arch.efer = efer;
2942 if (efer & EFER_LMA) {
2943 vmcs_write32(VM_ENTRY_CONTROLS,
2944 vmcs_read32(VM_ENTRY_CONTROLS) |
2945 VM_ENTRY_IA32E_MODE);
2948 vmcs_write32(VM_ENTRY_CONTROLS,
2949 vmcs_read32(VM_ENTRY_CONTROLS) &
2950 ~VM_ENTRY_IA32E_MODE);
2952 msr->data = efer & ~EFER_LME;
2957 #ifdef CONFIG_X86_64
2959 static void enter_lmode(struct kvm_vcpu *vcpu)
2963 vmx_segment_cache_clear(to_vmx(vcpu));
2965 guest_tr_ar = vmcs_read32(GUEST_TR_AR_BYTES);
2966 if ((guest_tr_ar & AR_TYPE_MASK) != AR_TYPE_BUSY_64_TSS) {
2967 pr_debug_ratelimited("%s: tss fixup for long mode. \n",
2969 vmcs_write32(GUEST_TR_AR_BYTES,
2970 (guest_tr_ar & ~AR_TYPE_MASK)
2971 | AR_TYPE_BUSY_64_TSS);
2973 vmx_set_efer(vcpu, vcpu->arch.efer | EFER_LMA);
2976 static void exit_lmode(struct kvm_vcpu *vcpu)
2978 vmcs_write32(VM_ENTRY_CONTROLS,
2979 vmcs_read32(VM_ENTRY_CONTROLS)
2980 & ~VM_ENTRY_IA32E_MODE);
2981 vmx_set_efer(vcpu, vcpu->arch.efer & ~EFER_LMA);
2986 static void vmx_flush_tlb(struct kvm_vcpu *vcpu)
2988 vpid_sync_context(to_vmx(vcpu));
2990 if (!VALID_PAGE(vcpu->arch.mmu.root_hpa))
2992 ept_sync_context(construct_eptp(vcpu->arch.mmu.root_hpa));
2996 static void vmx_decache_cr0_guest_bits(struct kvm_vcpu *vcpu)
2998 ulong cr0_guest_owned_bits = vcpu->arch.cr0_guest_owned_bits;
3000 vcpu->arch.cr0 &= ~cr0_guest_owned_bits;
3001 vcpu->arch.cr0 |= vmcs_readl(GUEST_CR0) & cr0_guest_owned_bits;
3004 static void vmx_decache_cr3(struct kvm_vcpu *vcpu)
3006 if (enable_ept && is_paging(vcpu))
3007 vcpu->arch.cr3 = vmcs_readl(GUEST_CR3);
3008 __set_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail);
3011 static void vmx_decache_cr4_guest_bits(struct kvm_vcpu *vcpu)
3013 ulong cr4_guest_owned_bits = vcpu->arch.cr4_guest_owned_bits;
3015 vcpu->arch.cr4 &= ~cr4_guest_owned_bits;
3016 vcpu->arch.cr4 |= vmcs_readl(GUEST_CR4) & cr4_guest_owned_bits;
3019 static void ept_load_pdptrs(struct kvm_vcpu *vcpu)
3021 if (!test_bit(VCPU_EXREG_PDPTR,
3022 (unsigned long *)&vcpu->arch.regs_dirty))
3025 if (is_paging(vcpu) && is_pae(vcpu) && !is_long_mode(vcpu)) {
3026 vmcs_write64(GUEST_PDPTR0, vcpu->arch.mmu.pdptrs[0]);
3027 vmcs_write64(GUEST_PDPTR1, vcpu->arch.mmu.pdptrs[1]);
3028 vmcs_write64(GUEST_PDPTR2, vcpu->arch.mmu.pdptrs[2]);
3029 vmcs_write64(GUEST_PDPTR3, vcpu->arch.mmu.pdptrs[3]);
3033 static void ept_save_pdptrs(struct kvm_vcpu *vcpu)
3035 if (is_paging(vcpu) && is_pae(vcpu) && !is_long_mode(vcpu)) {
3036 vcpu->arch.mmu.pdptrs[0] = vmcs_read64(GUEST_PDPTR0);
3037 vcpu->arch.mmu.pdptrs[1] = vmcs_read64(GUEST_PDPTR1);
3038 vcpu->arch.mmu.pdptrs[2] = vmcs_read64(GUEST_PDPTR2);
3039 vcpu->arch.mmu.pdptrs[3] = vmcs_read64(GUEST_PDPTR3);
3042 __set_bit(VCPU_EXREG_PDPTR,
3043 (unsigned long *)&vcpu->arch.regs_avail);
3044 __set_bit(VCPU_EXREG_PDPTR,
3045 (unsigned long *)&vcpu->arch.regs_dirty);
3048 static int vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4);
3050 static void ept_update_paging_mode_cr0(unsigned long *hw_cr0,
3052 struct kvm_vcpu *vcpu)
3054 if (!test_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail))
3055 vmx_decache_cr3(vcpu);
3056 if (!(cr0 & X86_CR0_PG)) {
3057 /* From paging/starting to nonpaging */
3058 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL,
3059 vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) |
3060 (CPU_BASED_CR3_LOAD_EXITING |
3061 CPU_BASED_CR3_STORE_EXITING));
3062 vcpu->arch.cr0 = cr0;
3063 vmx_set_cr4(vcpu, kvm_read_cr4(vcpu));
3064 } else if (!is_paging(vcpu)) {
3065 /* From nonpaging to paging */
3066 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL,
3067 vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) &
3068 ~(CPU_BASED_CR3_LOAD_EXITING |
3069 CPU_BASED_CR3_STORE_EXITING));
3070 vcpu->arch.cr0 = cr0;
3071 vmx_set_cr4(vcpu, kvm_read_cr4(vcpu));
3074 if (!(cr0 & X86_CR0_WP))
3075 *hw_cr0 &= ~X86_CR0_WP;
3078 static void vmx_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
3080 struct vcpu_vmx *vmx = to_vmx(vcpu);
3081 unsigned long hw_cr0;
3083 if (enable_unrestricted_guest)
3084 hw_cr0 = (cr0 & ~KVM_GUEST_CR0_MASK_UNRESTRICTED_GUEST)
3085 | KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST;
3087 hw_cr0 = (cr0 & ~KVM_GUEST_CR0_MASK) | KVM_VM_CR0_ALWAYS_ON;
3089 if (vmx->rmode.vm86_active && (cr0 & X86_CR0_PE))
3092 if (!vmx->rmode.vm86_active && !(cr0 & X86_CR0_PE))
3095 #ifdef CONFIG_X86_64
3096 if (vcpu->arch.efer & EFER_LME) {
3097 if (!is_paging(vcpu) && (cr0 & X86_CR0_PG))
3099 if (is_paging(vcpu) && !(cr0 & X86_CR0_PG))
3105 ept_update_paging_mode_cr0(&hw_cr0, cr0, vcpu);
3107 if (!vcpu->fpu_active)
3108 hw_cr0 |= X86_CR0_TS | X86_CR0_MP;
3110 vmcs_writel(CR0_READ_SHADOW, cr0);
3111 vmcs_writel(GUEST_CR0, hw_cr0);
3112 vcpu->arch.cr0 = cr0;
3113 __clear_bit(VCPU_EXREG_CPL, (ulong *)&vcpu->arch.regs_avail);
3116 static u64 construct_eptp(unsigned long root_hpa)
3120 /* TODO write the value reading from MSR */
3121 eptp = VMX_EPT_DEFAULT_MT |
3122 VMX_EPT_DEFAULT_GAW << VMX_EPT_GAW_EPTP_SHIFT;
3123 if (enable_ept_ad_bits)
3124 eptp |= VMX_EPT_AD_ENABLE_BIT;
3125 eptp |= (root_hpa & PAGE_MASK);
3130 static void vmx_set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3)
3132 unsigned long guest_cr3;
3137 eptp = construct_eptp(cr3);
3138 vmcs_write64(EPT_POINTER, eptp);
3139 guest_cr3 = is_paging(vcpu) ? kvm_read_cr3(vcpu) :
3140 vcpu->kvm->arch.ept_identity_map_addr;
3141 ept_load_pdptrs(vcpu);
3144 vmx_flush_tlb(vcpu);
3145 vmcs_writel(GUEST_CR3, guest_cr3);
3148 static int vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
3150 unsigned long hw_cr4 = cr4 | (to_vmx(vcpu)->rmode.vm86_active ?
3151 KVM_RMODE_VM_CR4_ALWAYS_ON : KVM_PMODE_VM_CR4_ALWAYS_ON);
3153 if (cr4 & X86_CR4_VMXE) {
3155 * To use VMXON (and later other VMX instructions), a guest
3156 * must first be able to turn on cr4.VMXE (see handle_vmon()).
3157 * So basically the check on whether to allow nested VMX
3160 if (!nested_vmx_allowed(vcpu))
3162 } else if (to_vmx(vcpu)->nested.vmxon)
3165 vcpu->arch.cr4 = cr4;
3167 if (!is_paging(vcpu)) {
3168 hw_cr4 &= ~X86_CR4_PAE;
3169 hw_cr4 |= X86_CR4_PSE;
3170 } else if (!(cr4 & X86_CR4_PAE)) {
3171 hw_cr4 &= ~X86_CR4_PAE;
3175 vmcs_writel(CR4_READ_SHADOW, cr4);
3176 vmcs_writel(GUEST_CR4, hw_cr4);
3180 static void vmx_get_segment(struct kvm_vcpu *vcpu,
3181 struct kvm_segment *var, int seg)
3183 struct vcpu_vmx *vmx = to_vmx(vcpu);
3186 if (vmx->rmode.vm86_active && seg != VCPU_SREG_LDTR) {
3187 *var = vmx->rmode.segs[seg];
3188 if (seg == VCPU_SREG_TR
3189 || var->selector == vmx_read_guest_seg_selector(vmx, seg))
3191 var->base = vmx_read_guest_seg_base(vmx, seg);
3192 var->selector = vmx_read_guest_seg_selector(vmx, seg);
3195 var->base = vmx_read_guest_seg_base(vmx, seg);
3196 var->limit = vmx_read_guest_seg_limit(vmx, seg);
3197 var->selector = vmx_read_guest_seg_selector(vmx, seg);
3198 ar = vmx_read_guest_seg_ar(vmx, seg);
3199 if ((ar & AR_UNUSABLE_MASK) && !emulate_invalid_guest_state)
3201 var->type = ar & 15;
3202 var->s = (ar >> 4) & 1;
3203 var->dpl = (ar >> 5) & 3;
3204 var->present = (ar >> 7) & 1;
3205 var->avl = (ar >> 12) & 1;
3206 var->l = (ar >> 13) & 1;
3207 var->db = (ar >> 14) & 1;
3208 var->g = (ar >> 15) & 1;
3209 var->unusable = (ar >> 16) & 1;
3212 static u64 vmx_get_segment_base(struct kvm_vcpu *vcpu, int seg)
3214 struct kvm_segment s;
3216 if (to_vmx(vcpu)->rmode.vm86_active) {
3217 vmx_get_segment(vcpu, &s, seg);
3220 return vmx_read_guest_seg_base(to_vmx(vcpu), seg);
3223 static int __vmx_get_cpl(struct kvm_vcpu *vcpu)
3225 if (!is_protmode(vcpu))
3228 if (!is_long_mode(vcpu)
3229 && (kvm_get_rflags(vcpu) & X86_EFLAGS_VM)) /* if virtual 8086 */
3232 return vmx_read_guest_seg_selector(to_vmx(vcpu), VCPU_SREG_CS) & 3;
3235 static int vmx_get_cpl(struct kvm_vcpu *vcpu)
3237 struct vcpu_vmx *vmx = to_vmx(vcpu);
3240 * If we enter real mode with cs.sel & 3 != 0, the normal CPL calculations
3241 * fail; use the cache instead.
3243 if (unlikely(vmx->emulation_required && emulate_invalid_guest_state)) {
3247 if (!test_bit(VCPU_EXREG_CPL, (ulong *)&vcpu->arch.regs_avail)) {
3248 __set_bit(VCPU_EXREG_CPL, (ulong *)&vcpu->arch.regs_avail);
3249 vmx->cpl = __vmx_get_cpl(vcpu);
3256 static u32 vmx_segment_access_rights(struct kvm_segment *var)
3260 if (var->unusable || !var->present)
3263 ar = var->type & 15;
3264 ar |= (var->s & 1) << 4;
3265 ar |= (var->dpl & 3) << 5;
3266 ar |= (var->present & 1) << 7;
3267 ar |= (var->avl & 1) << 12;
3268 ar |= (var->l & 1) << 13;
3269 ar |= (var->db & 1) << 14;
3270 ar |= (var->g & 1) << 15;
3276 static void vmx_set_segment(struct kvm_vcpu *vcpu,
3277 struct kvm_segment *var, int seg)
3279 struct vcpu_vmx *vmx = to_vmx(vcpu);
3280 const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
3282 vmx_segment_cache_clear(vmx);
3283 __clear_bit(VCPU_EXREG_CPL, (ulong *)&vcpu->arch.regs_avail);
3285 if (vmx->rmode.vm86_active && seg != VCPU_SREG_LDTR) {
3286 vmx->rmode.segs[seg] = *var;
3287 if (seg == VCPU_SREG_TR)
3288 vmcs_write16(sf->selector, var->selector);
3290 fix_rmode_seg(seg, &vmx->rmode.segs[seg]);
3294 vmcs_writel(sf->base, var->base);
3295 vmcs_write32(sf->limit, var->limit);
3296 vmcs_write16(sf->selector, var->selector);
3299 * Fix the "Accessed" bit in AR field of segment registers for older
3301 * IA32 arch specifies that at the time of processor reset the
3302 * "Accessed" bit in the AR field of segment registers is 1. And qemu
3303 * is setting it to 0 in the userland code. This causes invalid guest
3304 * state vmexit when "unrestricted guest" mode is turned on.
3305 * Fix for this setup issue in cpu_reset is being pushed in the qemu
3306 * tree. Newer qemu binaries with that qemu fix would not need this
3309 if (enable_unrestricted_guest && (seg != VCPU_SREG_LDTR))
3310 var->type |= 0x1; /* Accessed */
3312 vmcs_write32(sf->ar_bytes, vmx_segment_access_rights(var));
3315 if (!vmx->emulation_required)
3316 vmx->emulation_required = !guest_state_valid(vcpu);
3319 static void vmx_get_cs_db_l_bits(struct kvm_vcpu *vcpu, int *db, int *l)
3321 u32 ar = vmx_read_guest_seg_ar(to_vmx(vcpu), VCPU_SREG_CS);
3323 *db = (ar >> 14) & 1;
3324 *l = (ar >> 13) & 1;
3327 static void vmx_get_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
3329 dt->size = vmcs_read32(GUEST_IDTR_LIMIT);
3330 dt->address = vmcs_readl(GUEST_IDTR_BASE);
3333 static void vmx_set_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
3335 vmcs_write32(GUEST_IDTR_LIMIT, dt->size);
3336 vmcs_writel(GUEST_IDTR_BASE, dt->address);
3339 static void vmx_get_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
3341 dt->size = vmcs_read32(GUEST_GDTR_LIMIT);
3342 dt->address = vmcs_readl(GUEST_GDTR_BASE);
3345 static void vmx_set_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
3347 vmcs_write32(GUEST_GDTR_LIMIT, dt->size);
3348 vmcs_writel(GUEST_GDTR_BASE, dt->address);
3351 static bool rmode_segment_valid(struct kvm_vcpu *vcpu, int seg)
3353 struct kvm_segment var;
3356 vmx_get_segment(vcpu, &var, seg);
3358 if (seg == VCPU_SREG_CS)
3360 ar = vmx_segment_access_rights(&var);
3362 if (var.base != (var.selector << 4))
3364 if (var.limit != 0xffff)
3372 static bool code_segment_valid(struct kvm_vcpu *vcpu)
3374 struct kvm_segment cs;
3375 unsigned int cs_rpl;
3377 vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
3378 cs_rpl = cs.selector & SELECTOR_RPL_MASK;
3382 if (~cs.type & (AR_TYPE_CODE_MASK|AR_TYPE_ACCESSES_MASK))
3386 if (cs.type & AR_TYPE_WRITEABLE_MASK) {
3387 if (cs.dpl > cs_rpl)
3390 if (cs.dpl != cs_rpl)
3396 /* TODO: Add Reserved field check, this'll require a new member in the kvm_segment_field structure */
3400 static bool stack_segment_valid(struct kvm_vcpu *vcpu)
3402 struct kvm_segment ss;
3403 unsigned int ss_rpl;
3405 vmx_get_segment(vcpu, &ss, VCPU_SREG_SS);
3406 ss_rpl = ss.selector & SELECTOR_RPL_MASK;
3410 if (ss.type != 3 && ss.type != 7)
3414 if (ss.dpl != ss_rpl) /* DPL != RPL */
3422 static bool data_segment_valid(struct kvm_vcpu *vcpu, int seg)
3424 struct kvm_segment var;
3427 vmx_get_segment(vcpu, &var, seg);
3428 rpl = var.selector & SELECTOR_RPL_MASK;
3436 if (~var.type & (AR_TYPE_CODE_MASK|AR_TYPE_WRITEABLE_MASK)) {
3437 if (var.dpl < rpl) /* DPL < RPL */
3441 /* TODO: Add other members to kvm_segment_field to allow checking for other access
3447 static bool tr_valid(struct kvm_vcpu *vcpu)
3449 struct kvm_segment tr;
3451 vmx_get_segment(vcpu, &tr, VCPU_SREG_TR);
3455 if (tr.selector & SELECTOR_TI_MASK) /* TI = 1 */
3457 if (tr.type != 3 && tr.type != 11) /* TODO: Check if guest is in IA32e mode */
3465 static bool ldtr_valid(struct kvm_vcpu *vcpu)
3467 struct kvm_segment ldtr;
3469 vmx_get_segment(vcpu, &ldtr, VCPU_SREG_LDTR);
3473 if (ldtr.selector & SELECTOR_TI_MASK) /* TI = 1 */
3483 static bool cs_ss_rpl_check(struct kvm_vcpu *vcpu)
3485 struct kvm_segment cs, ss;
3487 vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
3488 vmx_get_segment(vcpu, &ss, VCPU_SREG_SS);
3490 return ((cs.selector & SELECTOR_RPL_MASK) ==
3491 (ss.selector & SELECTOR_RPL_MASK));
3495 * Check if guest state is valid. Returns true if valid, false if
3497 * We assume that registers are always usable
3499 static bool guest_state_valid(struct kvm_vcpu *vcpu)
3501 /* real mode guest state checks */
3502 if (!is_protmode(vcpu)) {
3503 if (!rmode_segment_valid(vcpu, VCPU_SREG_CS))
3505 if (!rmode_segment_valid(vcpu, VCPU_SREG_SS))
3507 if (!rmode_segment_valid(vcpu, VCPU_SREG_DS))
3509 if (!rmode_segment_valid(vcpu, VCPU_SREG_ES))
3511 if (!rmode_segment_valid(vcpu, VCPU_SREG_FS))
3513 if (!rmode_segment_valid(vcpu, VCPU_SREG_GS))
3516 /* protected mode guest state checks */
3517 if (!cs_ss_rpl_check(vcpu))
3519 if (!code_segment_valid(vcpu))
3521 if (!stack_segment_valid(vcpu))
3523 if (!data_segment_valid(vcpu, VCPU_SREG_DS))
3525 if (!data_segment_valid(vcpu, VCPU_SREG_ES))
3527 if (!data_segment_valid(vcpu, VCPU_SREG_FS))
3529 if (!data_segment_valid(vcpu, VCPU_SREG_GS))
3531 if (!tr_valid(vcpu))
3533 if (!ldtr_valid(vcpu))
3537 * - Add checks on RIP
3538 * - Add checks on RFLAGS
3544 static int init_rmode_tss(struct kvm *kvm)
3548 int r, idx, ret = 0;
3550 idx = srcu_read_lock(&kvm->srcu);
3551 fn = rmode_tss_base(kvm) >> PAGE_SHIFT;
3552 r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE);
3555 data = TSS_BASE_SIZE + TSS_REDIRECTION_SIZE;
3556 r = kvm_write_guest_page(kvm, fn++, &data,
3557 TSS_IOPB_BASE_OFFSET, sizeof(u16));
3560 r = kvm_clear_guest_page(kvm, fn++, 0, PAGE_SIZE);
3563 r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE);
3567 r = kvm_write_guest_page(kvm, fn, &data,
3568 RMODE_TSS_SIZE - 2 * PAGE_SIZE - 1,
3575 srcu_read_unlock(&kvm->srcu, idx);
3579 static int init_rmode_identity_map(struct kvm *kvm)
3582 pfn_t identity_map_pfn;
3587 if (unlikely(!kvm->arch.ept_identity_pagetable)) {
3588 printk(KERN_ERR "EPT: identity-mapping pagetable "
3589 "haven't been allocated!\n");
3592 if (likely(kvm->arch.ept_identity_pagetable_done))
3595 identity_map_pfn = kvm->arch.ept_identity_map_addr >> PAGE_SHIFT;
3596 idx = srcu_read_lock(&kvm->srcu);
3597 r = kvm_clear_guest_page(kvm, identity_map_pfn, 0, PAGE_SIZE);
3600 /* Set up identity-mapping pagetable for EPT in real mode */
3601 for (i = 0; i < PT32_ENT_PER_PAGE; i++) {
3602 tmp = (i << 22) + (_PAGE_PRESENT | _PAGE_RW | _PAGE_USER |
3603 _PAGE_ACCESSED | _PAGE_DIRTY | _PAGE_PSE);
3604 r = kvm_write_guest_page(kvm, identity_map_pfn,
3605 &tmp, i * sizeof(tmp), sizeof(tmp));
3609 kvm->arch.ept_identity_pagetable_done = true;
3612 srcu_read_unlock(&kvm->srcu, idx);
3616 static void seg_setup(int seg)
3618 const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
3621 vmcs_write16(sf->selector, 0);
3622 vmcs_writel(sf->base, 0);
3623 vmcs_write32(sf->limit, 0xffff);
3625 if (seg == VCPU_SREG_CS)
3626 ar |= 0x08; /* code segment */
3628 vmcs_write32(sf->ar_bytes, ar);
3631 static int alloc_apic_access_page(struct kvm *kvm)
3634 struct kvm_userspace_memory_region kvm_userspace_mem;
3637 mutex_lock(&kvm->slots_lock);
3638 if (kvm->arch.apic_access_page)
3640 kvm_userspace_mem.slot = APIC_ACCESS_PAGE_PRIVATE_MEMSLOT;
3641 kvm_userspace_mem.flags = 0;
3642 kvm_userspace_mem.guest_phys_addr = 0xfee00000ULL;
3643 kvm_userspace_mem.memory_size = PAGE_SIZE;
3644 r = __kvm_set_memory_region(kvm, &kvm_userspace_mem, false);
3648 page = gfn_to_page(kvm, 0xfee00);
3649 if (is_error_page(page)) {
3654 kvm->arch.apic_access_page = page;
3656 mutex_unlock(&kvm->slots_lock);
3660 static int alloc_identity_pagetable(struct kvm *kvm)
3663 struct kvm_userspace_memory_region kvm_userspace_mem;
3666 mutex_lock(&kvm->slots_lock);
3667 if (kvm->arch.ept_identity_pagetable)
3669 kvm_userspace_mem.slot = IDENTITY_PAGETABLE_PRIVATE_MEMSLOT;
3670 kvm_userspace_mem.flags = 0;
3671 kvm_userspace_mem.guest_phys_addr =
3672 kvm->arch.ept_identity_map_addr;
3673 kvm_userspace_mem.memory_size = PAGE_SIZE;
3674 r = __kvm_set_memory_region(kvm, &kvm_userspace_mem, false);
3678 page = gfn_to_page(kvm, kvm->arch.ept_identity_map_addr >> PAGE_SHIFT);
3679 if (is_error_page(page)) {
3684 kvm->arch.ept_identity_pagetable = page;
3686 mutex_unlock(&kvm->slots_lock);
3690 static void allocate_vpid(struct vcpu_vmx *vmx)
3697 spin_lock(&vmx_vpid_lock);
3698 vpid = find_first_zero_bit(vmx_vpid_bitmap, VMX_NR_VPIDS);
3699 if (vpid < VMX_NR_VPIDS) {
3701 __set_bit(vpid, vmx_vpid_bitmap);
3703 spin_unlock(&vmx_vpid_lock);
3706 static void free_vpid(struct vcpu_vmx *vmx)
3710 spin_lock(&vmx_vpid_lock);
3712 __clear_bit(vmx->vpid, vmx_vpid_bitmap);
3713 spin_unlock(&vmx_vpid_lock);
3716 static void __vmx_disable_intercept_for_msr(unsigned long *msr_bitmap, u32 msr)
3718 int f = sizeof(unsigned long);
3720 if (!cpu_has_vmx_msr_bitmap())
3724 * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
3725 * have the write-low and read-high bitmap offsets the wrong way round.
3726 * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
3728 if (msr <= 0x1fff) {
3729 __clear_bit(msr, msr_bitmap + 0x000 / f); /* read-low */
3730 __clear_bit(msr, msr_bitmap + 0x800 / f); /* write-low */
3731 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
3733 __clear_bit(msr, msr_bitmap + 0x400 / f); /* read-high */
3734 __clear_bit(msr, msr_bitmap + 0xc00 / f); /* write-high */
3738 static void vmx_disable_intercept_for_msr(u32 msr, bool longmode_only)
3741 __vmx_disable_intercept_for_msr(vmx_msr_bitmap_legacy, msr);
3742 __vmx_disable_intercept_for_msr(vmx_msr_bitmap_longmode, msr);
3746 * Set up the vmcs's constant host-state fields, i.e., host-state fields that
3747 * will not change in the lifetime of the guest.
3748 * Note that host-state that does change is set elsewhere. E.g., host-state
3749 * that is set differently for each CPU is set in vmx_vcpu_load(), not here.
3751 static void vmx_set_constant_host_state(void)
3757 vmcs_writel(HOST_CR0, read_cr0() & ~X86_CR0_TS); /* 22.2.3 */
3758 vmcs_writel(HOST_CR4, read_cr4()); /* 22.2.3, 22.2.5 */
3759 vmcs_writel(HOST_CR3, read_cr3()); /* 22.2.3 FIXME: shadow tables */
3761 vmcs_write16(HOST_CS_SELECTOR, __KERNEL_CS); /* 22.2.4 */
3762 #ifdef CONFIG_X86_64
3764 * Load null selectors, so we can avoid reloading them in
3765 * __vmx_load_host_state(), in case userspace uses the null selectors
3766 * too (the expected case).
3768 vmcs_write16(HOST_DS_SELECTOR, 0);
3769 vmcs_write16(HOST_ES_SELECTOR, 0);
3771 vmcs_write16(HOST_DS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
3772 vmcs_write16(HOST_ES_SELECTOR, __KERNEL_DS); /* 22.2.4 */
3774 vmcs_write16(HOST_SS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
3775 vmcs_write16(HOST_TR_SELECTOR, GDT_ENTRY_TSS*8); /* 22.2.4 */
3777 native_store_idt(&dt);
3778 vmcs_writel(HOST_IDTR_BASE, dt.address); /* 22.2.4 */
3780 vmcs_writel(HOST_RIP, vmx_return); /* 22.2.5 */
3782 rdmsr(MSR_IA32_SYSENTER_CS, low32, high32);
3783 vmcs_write32(HOST_IA32_SYSENTER_CS, low32);
3784 rdmsrl(MSR_IA32_SYSENTER_EIP, tmpl);
3785 vmcs_writel(HOST_IA32_SYSENTER_EIP, tmpl); /* 22.2.3 */
3787 if (vmcs_config.vmexit_ctrl & VM_EXIT_LOAD_IA32_PAT) {
3788 rdmsr(MSR_IA32_CR_PAT, low32, high32);
3789 vmcs_write64(HOST_IA32_PAT, low32 | ((u64) high32 << 32));
3793 static void set_cr4_guest_host_mask(struct vcpu_vmx *vmx)
3795 vmx->vcpu.arch.cr4_guest_owned_bits = KVM_CR4_GUEST_OWNED_BITS;
3797 vmx->vcpu.arch.cr4_guest_owned_bits |= X86_CR4_PGE;
3798 if (is_guest_mode(&vmx->vcpu))
3799 vmx->vcpu.arch.cr4_guest_owned_bits &=
3800 ~get_vmcs12(&vmx->vcpu)->cr4_guest_host_mask;
3801 vmcs_writel(CR4_GUEST_HOST_MASK, ~vmx->vcpu.arch.cr4_guest_owned_bits);
3804 static u32 vmx_exec_control(struct vcpu_vmx *vmx)
3806 u32 exec_control = vmcs_config.cpu_based_exec_ctrl;
3807 if (!vm_need_tpr_shadow(vmx->vcpu.kvm)) {
3808 exec_control &= ~CPU_BASED_TPR_SHADOW;
3809 #ifdef CONFIG_X86_64
3810 exec_control |= CPU_BASED_CR8_STORE_EXITING |
3811 CPU_BASED_CR8_LOAD_EXITING;
3815 exec_control |= CPU_BASED_CR3_STORE_EXITING |
3816 CPU_BASED_CR3_LOAD_EXITING |
3817 CPU_BASED_INVLPG_EXITING;
3818 return exec_control;
3821 static u32 vmx_secondary_exec_control(struct vcpu_vmx *vmx)
3823 u32 exec_control = vmcs_config.cpu_based_2nd_exec_ctrl;
3824 if (!vm_need_virtualize_apic_accesses(vmx->vcpu.kvm))
3825 exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
3827 exec_control &= ~SECONDARY_EXEC_ENABLE_VPID;
3829 exec_control &= ~SECONDARY_EXEC_ENABLE_EPT;
3830 enable_unrestricted_guest = 0;
3831 /* Enable INVPCID for non-ept guests may cause performance regression. */
3832 exec_control &= ~SECONDARY_EXEC_ENABLE_INVPCID;
3834 if (!enable_unrestricted_guest)
3835 exec_control &= ~SECONDARY_EXEC_UNRESTRICTED_GUEST;
3837 exec_control &= ~SECONDARY_EXEC_PAUSE_LOOP_EXITING;
3838 return exec_control;
3841 static void ept_set_mmio_spte_mask(void)
3844 * EPT Misconfigurations can be generated if the value of bits 2:0
3845 * of an EPT paging-structure entry is 110b (write/execute).
3846 * Also, magic bits (0xffull << 49) is set to quickly identify mmio
3849 kvm_mmu_set_mmio_spte_mask(0xffull << 49 | 0x6ull);
3853 * Sets up the vmcs for emulated real mode.
3855 static int vmx_vcpu_setup(struct vcpu_vmx *vmx)
3857 #ifdef CONFIG_X86_64
3863 vmcs_write64(IO_BITMAP_A, __pa(vmx_io_bitmap_a));
3864 vmcs_write64(IO_BITMAP_B, __pa(vmx_io_bitmap_b));
3866 if (cpu_has_vmx_msr_bitmap())
3867 vmcs_write64(MSR_BITMAP, __pa(vmx_msr_bitmap_legacy));
3869 vmcs_write64(VMCS_LINK_POINTER, -1ull); /* 22.3.1.5 */
3872 vmcs_write32(PIN_BASED_VM_EXEC_CONTROL,
3873 vmcs_config.pin_based_exec_ctrl);
3875 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, vmx_exec_control(vmx));
3877 if (cpu_has_secondary_exec_ctrls()) {
3878 vmcs_write32(SECONDARY_VM_EXEC_CONTROL,
3879 vmx_secondary_exec_control(vmx));
3883 vmcs_write32(PLE_GAP, ple_gap);
3884 vmcs_write32(PLE_WINDOW, ple_window);
3887 vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK, 0);
3888 vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH, 0);
3889 vmcs_write32(CR3_TARGET_COUNT, 0); /* 22.2.1 */
3891 vmcs_write16(HOST_FS_SELECTOR, 0); /* 22.2.4 */
3892 vmcs_write16(HOST_GS_SELECTOR, 0); /* 22.2.4 */
3893 vmx_set_constant_host_state();
3894 #ifdef CONFIG_X86_64
3895 rdmsrl(MSR_FS_BASE, a);
3896 vmcs_writel(HOST_FS_BASE, a); /* 22.2.4 */
3897 rdmsrl(MSR_GS_BASE, a);
3898 vmcs_writel(HOST_GS_BASE, a); /* 22.2.4 */
3900 vmcs_writel(HOST_FS_BASE, 0); /* 22.2.4 */
3901 vmcs_writel(HOST_GS_BASE, 0); /* 22.2.4 */
3904 vmcs_write32(VM_EXIT_MSR_STORE_COUNT, 0);
3905 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, 0);
3906 vmcs_write64(VM_EXIT_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.host));
3907 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, 0);
3908 vmcs_write64(VM_ENTRY_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.guest));
3910 if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) {
3911 u32 msr_low, msr_high;
3913 rdmsr(MSR_IA32_CR_PAT, msr_low, msr_high);
3914 host_pat = msr_low | ((u64) msr_high << 32);
3915 /* Write the default value follow host pat */
3916 vmcs_write64(GUEST_IA32_PAT, host_pat);
3917 /* Keep arch.pat sync with GUEST_IA32_PAT */
3918 vmx->vcpu.arch.pat = host_pat;
3921 for (i = 0; i < NR_VMX_MSR; ++i) {
3922 u32 index = vmx_msr_index[i];
3923 u32 data_low, data_high;
3926 if (rdmsr_safe(index, &data_low, &data_high) < 0)
3928 if (wrmsr_safe(index, data_low, data_high) < 0)
3930 vmx->guest_msrs[j].index = i;
3931 vmx->guest_msrs[j].data = 0;
3932 vmx->guest_msrs[j].mask = -1ull;
3936 vmcs_write32(VM_EXIT_CONTROLS, vmcs_config.vmexit_ctrl);
3938 /* 22.2.1, 20.8.1 */
3939 vmcs_write32(VM_ENTRY_CONTROLS, vmcs_config.vmentry_ctrl);
3941 vmcs_writel(CR0_GUEST_HOST_MASK, ~0UL);
3942 set_cr4_guest_host_mask(vmx);
3947 static int vmx_vcpu_reset(struct kvm_vcpu *vcpu)
3949 struct vcpu_vmx *vmx = to_vmx(vcpu);
3953 vmx->rmode.vm86_active = 0;
3955 vmx->soft_vnmi_blocked = 0;
3957 vmx->vcpu.arch.regs[VCPU_REGS_RDX] = get_rdx_init_val();
3958 kvm_set_cr8(&vmx->vcpu, 0);
3959 msr = 0xfee00000 | MSR_IA32_APICBASE_ENABLE;
3960 if (kvm_vcpu_is_bsp(&vmx->vcpu))
3961 msr |= MSR_IA32_APICBASE_BSP;
3962 kvm_set_apic_base(&vmx->vcpu, msr);
3964 vmx_segment_cache_clear(vmx);
3966 seg_setup(VCPU_SREG_CS);
3967 if (kvm_vcpu_is_bsp(&vmx->vcpu))
3968 vmcs_write16(GUEST_CS_SELECTOR, 0xf000);
3970 vmcs_write16(GUEST_CS_SELECTOR, vmx->vcpu.arch.sipi_vector << 8);
3971 vmcs_writel(GUEST_CS_BASE, vmx->vcpu.arch.sipi_vector << 12);
3974 seg_setup(VCPU_SREG_DS);
3975 seg_setup(VCPU_SREG_ES);
3976 seg_setup(VCPU_SREG_FS);
3977 seg_setup(VCPU_SREG_GS);
3978 seg_setup(VCPU_SREG_SS);
3980 vmcs_write16(GUEST_TR_SELECTOR, 0);
3981 vmcs_writel(GUEST_TR_BASE, 0);
3982 vmcs_write32(GUEST_TR_LIMIT, 0xffff);
3983 vmcs_write32(GUEST_TR_AR_BYTES, 0x008b);
3985 vmcs_write16(GUEST_LDTR_SELECTOR, 0);
3986 vmcs_writel(GUEST_LDTR_BASE, 0);
3987 vmcs_write32(GUEST_LDTR_LIMIT, 0xffff);
3988 vmcs_write32(GUEST_LDTR_AR_BYTES, 0x00082);
3990 vmcs_write32(GUEST_SYSENTER_CS, 0);
3991 vmcs_writel(GUEST_SYSENTER_ESP, 0);
3992 vmcs_writel(GUEST_SYSENTER_EIP, 0);
3994 vmcs_writel(GUEST_RFLAGS, 0x02);
3995 if (kvm_vcpu_is_bsp(&vmx->vcpu))
3996 kvm_rip_write(vcpu, 0xfff0);
3998 kvm_rip_write(vcpu, 0);
4000 vmcs_writel(GUEST_GDTR_BASE, 0);
4001 vmcs_write32(GUEST_GDTR_LIMIT, 0xffff);
4003 vmcs_writel(GUEST_IDTR_BASE, 0);
4004 vmcs_write32(GUEST_IDTR_LIMIT, 0xffff);
4006 vmcs_write32(GUEST_ACTIVITY_STATE, GUEST_ACTIVITY_ACTIVE);
4007 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, 0);
4008 vmcs_write32(GUEST_PENDING_DBG_EXCEPTIONS, 0);
4010 /* Special registers */
4011 vmcs_write64(GUEST_IA32_DEBUGCTL, 0);
4015 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0); /* 22.2.1 */
4017 if (cpu_has_vmx_tpr_shadow()) {
4018 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, 0);
4019 if (vm_need_tpr_shadow(vmx->vcpu.kvm))
4020 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR,
4021 __pa(vmx->vcpu.arch.apic->regs));
4022 vmcs_write32(TPR_THRESHOLD, 0);
4025 if (vm_need_virtualize_apic_accesses(vmx->vcpu.kvm))
4026 vmcs_write64(APIC_ACCESS_ADDR,
4027 page_to_phys(vmx->vcpu.kvm->arch.apic_access_page));
4030 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid);
4032 vmx->vcpu.arch.cr0 = X86_CR0_NW | X86_CR0_CD | X86_CR0_ET;
4033 vcpu->srcu_idx = srcu_read_lock(&vcpu->kvm->srcu);
4034 vmx_set_cr0(&vmx->vcpu, kvm_read_cr0(vcpu)); /* enter rmode */
4035 srcu_read_unlock(&vcpu->kvm->srcu, vcpu->srcu_idx);
4036 vmx_set_cr4(&vmx->vcpu, 0);
4037 vmx_set_efer(&vmx->vcpu, 0);
4038 vmx_fpu_activate(&vmx->vcpu);
4039 update_exception_bitmap(&vmx->vcpu);
4041 vpid_sync_context(vmx);
4045 /* HACK: Don't enable emulation on guest boot/reset */
4046 vmx->emulation_required = 0;
4052 * In nested virtualization, check if L1 asked to exit on external interrupts.
4053 * For most existing hypervisors, this will always return true.
4055 static bool nested_exit_on_intr(struct kvm_vcpu *vcpu)
4057 return get_vmcs12(vcpu)->pin_based_vm_exec_control &
4058 PIN_BASED_EXT_INTR_MASK;
4061 static void enable_irq_window(struct kvm_vcpu *vcpu)
4063 u32 cpu_based_vm_exec_control;
4064 if (is_guest_mode(vcpu) && nested_exit_on_intr(vcpu)) {
4066 * We get here if vmx_interrupt_allowed() said we can't
4067 * inject to L1 now because L2 must run. Ask L2 to exit
4068 * right after entry, so we can inject to L1 more promptly.
4070 kvm_make_request(KVM_REQ_IMMEDIATE_EXIT, vcpu);
4074 cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
4075 cpu_based_vm_exec_control |= CPU_BASED_VIRTUAL_INTR_PENDING;
4076 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control);
4079 static void enable_nmi_window(struct kvm_vcpu *vcpu)
4081 u32 cpu_based_vm_exec_control;
4083 if (!cpu_has_virtual_nmis()) {
4084 enable_irq_window(vcpu);
4088 if (vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & GUEST_INTR_STATE_STI) {
4089 enable_irq_window(vcpu);
4092 cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
4093 cpu_based_vm_exec_control |= CPU_BASED_VIRTUAL_NMI_PENDING;
4094 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control);
4097 static void vmx_inject_irq(struct kvm_vcpu *vcpu)
4099 struct vcpu_vmx *vmx = to_vmx(vcpu);
4101 int irq = vcpu->arch.interrupt.nr;
4103 trace_kvm_inj_virq(irq);
4105 ++vcpu->stat.irq_injections;
4106 if (vmx->rmode.vm86_active) {
4108 if (vcpu->arch.interrupt.soft)
4109 inc_eip = vcpu->arch.event_exit_inst_len;
4110 if (kvm_inject_realmode_interrupt(vcpu, irq, inc_eip) != EMULATE_DONE)
4111 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
4114 intr = irq | INTR_INFO_VALID_MASK;
4115 if (vcpu->arch.interrupt.soft) {
4116 intr |= INTR_TYPE_SOFT_INTR;
4117 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
4118 vmx->vcpu.arch.event_exit_inst_len);
4120 intr |= INTR_TYPE_EXT_INTR;
4121 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr);
4124 static void vmx_inject_nmi(struct kvm_vcpu *vcpu)
4126 struct vcpu_vmx *vmx = to_vmx(vcpu);
4128 if (is_guest_mode(vcpu))
4131 if (!cpu_has_virtual_nmis()) {
4133 * Tracking the NMI-blocked state in software is built upon
4134 * finding the next open IRQ window. This, in turn, depends on
4135 * well-behaving guests: They have to keep IRQs disabled at
4136 * least as long as the NMI handler runs. Otherwise we may
4137 * cause NMI nesting, maybe breaking the guest. But as this is
4138 * highly unlikely, we can live with the residual risk.
4140 vmx->soft_vnmi_blocked = 1;
4141 vmx->vnmi_blocked_time = 0;
4144 ++vcpu->stat.nmi_injections;
4145 vmx->nmi_known_unmasked = false;
4146 if (vmx->rmode.vm86_active) {
4147 if (kvm_inject_realmode_interrupt(vcpu, NMI_VECTOR, 0) != EMULATE_DONE)
4148 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
4151 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
4152 INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK | NMI_VECTOR);
4155 static int vmx_nmi_allowed(struct kvm_vcpu *vcpu)
4157 if (!cpu_has_virtual_nmis() && to_vmx(vcpu)->soft_vnmi_blocked)
4160 return !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) &
4161 (GUEST_INTR_STATE_MOV_SS | GUEST_INTR_STATE_STI
4162 | GUEST_INTR_STATE_NMI));
4165 static bool vmx_get_nmi_mask(struct kvm_vcpu *vcpu)
4167 if (!cpu_has_virtual_nmis())
4168 return to_vmx(vcpu)->soft_vnmi_blocked;
4169 if (to_vmx(vcpu)->nmi_known_unmasked)
4171 return vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & GUEST_INTR_STATE_NMI;
4174 static void vmx_set_nmi_mask(struct kvm_vcpu *vcpu, bool masked)
4176 struct vcpu_vmx *vmx = to_vmx(vcpu);
4178 if (!cpu_has_virtual_nmis()) {
4179 if (vmx->soft_vnmi_blocked != masked) {
4180 vmx->soft_vnmi_blocked = masked;
4181 vmx->vnmi_blocked_time = 0;
4184 vmx->nmi_known_unmasked = !masked;
4186 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
4187 GUEST_INTR_STATE_NMI);
4189 vmcs_clear_bits(GUEST_INTERRUPTIBILITY_INFO,
4190 GUEST_INTR_STATE_NMI);
4194 static int vmx_interrupt_allowed(struct kvm_vcpu *vcpu)
4196 if (is_guest_mode(vcpu) && nested_exit_on_intr(vcpu)) {
4197 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
4198 if (to_vmx(vcpu)->nested.nested_run_pending ||
4199 (vmcs12->idt_vectoring_info_field &
4200 VECTORING_INFO_VALID_MASK))
4202 nested_vmx_vmexit(vcpu);
4203 vmcs12->vm_exit_reason = EXIT_REASON_EXTERNAL_INTERRUPT;
4204 vmcs12->vm_exit_intr_info = 0;
4205 /* fall through to normal code, but now in L1, not L2 */
4208 return (vmcs_readl(GUEST_RFLAGS) & X86_EFLAGS_IF) &&
4209 !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) &
4210 (GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS));
4213 static int vmx_set_tss_addr(struct kvm *kvm, unsigned int addr)
4216 struct kvm_userspace_memory_region tss_mem = {
4217 .slot = TSS_PRIVATE_MEMSLOT,
4218 .guest_phys_addr = addr,
4219 .memory_size = PAGE_SIZE * 3,
4223 ret = kvm_set_memory_region(kvm, &tss_mem, false);
4226 kvm->arch.tss_addr = addr;
4227 if (!init_rmode_tss(kvm))
4233 static int handle_rmode_exception(struct kvm_vcpu *vcpu,
4234 int vec, u32 err_code)
4237 * Instruction with address size override prefix opcode 0x67
4238 * Cause the #SS fault with 0 error code in VM86 mode.
4240 if (((vec == GP_VECTOR) || (vec == SS_VECTOR)) && err_code == 0)
4241 if (emulate_instruction(vcpu, 0) == EMULATE_DONE)
4244 * Forward all other exceptions that are valid in real mode.
4245 * FIXME: Breaks guest debugging in real mode, needs to be fixed with
4246 * the required debugging infrastructure rework.
4250 if (vcpu->guest_debug &
4251 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))
4253 kvm_queue_exception(vcpu, vec);
4257 * Update instruction length as we may reinject the exception
4258 * from user space while in guest debugging mode.
4260 to_vmx(vcpu)->vcpu.arch.event_exit_inst_len =
4261 vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
4262 if (vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
4273 kvm_queue_exception(vcpu, vec);
4280 * Trigger machine check on the host. We assume all the MSRs are already set up
4281 * by the CPU and that we still run on the same CPU as the MCE occurred on.
4282 * We pass a fake environment to the machine check handler because we want
4283 * the guest to be always treated like user space, no matter what context
4284 * it used internally.
4286 static void kvm_machine_check(void)
4288 #if defined(CONFIG_X86_MCE) && defined(CONFIG_X86_64)
4289 struct pt_regs regs = {
4290 .cs = 3, /* Fake ring 3 no matter what the guest ran on */
4291 .flags = X86_EFLAGS_IF,
4294 do_machine_check(®s, 0);
4298 static int handle_machine_check(struct kvm_vcpu *vcpu)
4300 /* already handled by vcpu_run */
4304 static int handle_exception(struct kvm_vcpu *vcpu)
4306 struct vcpu_vmx *vmx = to_vmx(vcpu);
4307 struct kvm_run *kvm_run = vcpu->run;
4308 u32 intr_info, ex_no, error_code;
4309 unsigned long cr2, rip, dr6;
4311 enum emulation_result er;
4313 vect_info = vmx->idt_vectoring_info;
4314 intr_info = vmx->exit_intr_info;
4316 if (is_machine_check(intr_info))
4317 return handle_machine_check(vcpu);
4319 if ((intr_info & INTR_INFO_INTR_TYPE_MASK) == INTR_TYPE_NMI_INTR)
4320 return 1; /* already handled by vmx_vcpu_run() */
4322 if (is_no_device(intr_info)) {
4323 vmx_fpu_activate(vcpu);
4327 if (is_invalid_opcode(intr_info)) {
4328 er = emulate_instruction(vcpu, EMULTYPE_TRAP_UD);
4329 if (er != EMULATE_DONE)
4330 kvm_queue_exception(vcpu, UD_VECTOR);
4335 if (intr_info & INTR_INFO_DELIVER_CODE_MASK)
4336 error_code = vmcs_read32(VM_EXIT_INTR_ERROR_CODE);
4339 * The #PF with PFEC.RSVD = 1 indicates the guest is accessing
4340 * MMIO, it is better to report an internal error.
4341 * See the comments in vmx_handle_exit.
4343 if ((vect_info & VECTORING_INFO_VALID_MASK) &&
4344 !(is_page_fault(intr_info) && !(error_code & PFERR_RSVD_MASK))) {
4345 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
4346 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_SIMUL_EX;
4347 vcpu->run->internal.ndata = 2;
4348 vcpu->run->internal.data[0] = vect_info;
4349 vcpu->run->internal.data[1] = intr_info;
4353 if (is_page_fault(intr_info)) {
4354 /* EPT won't cause page fault directly */
4356 cr2 = vmcs_readl(EXIT_QUALIFICATION);
4357 trace_kvm_page_fault(cr2, error_code);
4359 if (kvm_event_needs_reinjection(vcpu))
4360 kvm_mmu_unprotect_page_virt(vcpu, cr2);
4361 return kvm_mmu_page_fault(vcpu, cr2, error_code, NULL, 0);
4364 if (vmx->rmode.vm86_active &&
4365 handle_rmode_exception(vcpu, intr_info & INTR_INFO_VECTOR_MASK,
4367 if (vcpu->arch.halt_request) {
4368 vcpu->arch.halt_request = 0;
4369 return kvm_emulate_halt(vcpu);
4374 ex_no = intr_info & INTR_INFO_VECTOR_MASK;
4377 dr6 = vmcs_readl(EXIT_QUALIFICATION);
4378 if (!(vcpu->guest_debug &
4379 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))) {
4380 vcpu->arch.dr6 = dr6 | DR6_FIXED_1;
4381 kvm_queue_exception(vcpu, DB_VECTOR);
4384 kvm_run->debug.arch.dr6 = dr6 | DR6_FIXED_1;
4385 kvm_run->debug.arch.dr7 = vmcs_readl(GUEST_DR7);
4389 * Update instruction length as we may reinject #BP from
4390 * user space while in guest debugging mode. Reading it for
4391 * #DB as well causes no harm, it is not used in that case.
4393 vmx->vcpu.arch.event_exit_inst_len =
4394 vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
4395 kvm_run->exit_reason = KVM_EXIT_DEBUG;
4396 rip = kvm_rip_read(vcpu);
4397 kvm_run->debug.arch.pc = vmcs_readl(GUEST_CS_BASE) + rip;
4398 kvm_run->debug.arch.exception = ex_no;
4401 kvm_run->exit_reason = KVM_EXIT_EXCEPTION;
4402 kvm_run->ex.exception = ex_no;
4403 kvm_run->ex.error_code = error_code;
4409 static int handle_external_interrupt(struct kvm_vcpu *vcpu)
4411 ++vcpu->stat.irq_exits;
4415 static int handle_triple_fault(struct kvm_vcpu *vcpu)
4417 vcpu->run->exit_reason = KVM_EXIT_SHUTDOWN;
4421 static int handle_io(struct kvm_vcpu *vcpu)
4423 unsigned long exit_qualification;
4424 int size, in, string;
4427 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
4428 string = (exit_qualification & 16) != 0;
4429 in = (exit_qualification & 8) != 0;
4431 ++vcpu->stat.io_exits;
4434 return emulate_instruction(vcpu, 0) == EMULATE_DONE;
4436 port = exit_qualification >> 16;
4437 size = (exit_qualification & 7) + 1;
4438 skip_emulated_instruction(vcpu);
4440 return kvm_fast_pio_out(vcpu, size, port);
4444 vmx_patch_hypercall(struct kvm_vcpu *vcpu, unsigned char *hypercall)
4447 * Patch in the VMCALL instruction:
4449 hypercall[0] = 0x0f;
4450 hypercall[1] = 0x01;
4451 hypercall[2] = 0xc1;
4454 /* called to set cr0 as appropriate for a mov-to-cr0 exit. */
4455 static int handle_set_cr0(struct kvm_vcpu *vcpu, unsigned long val)
4457 if (to_vmx(vcpu)->nested.vmxon &&
4458 ((val & VMXON_CR0_ALWAYSON) != VMXON_CR0_ALWAYSON))
4461 if (is_guest_mode(vcpu)) {
4463 * We get here when L2 changed cr0 in a way that did not change
4464 * any of L1's shadowed bits (see nested_vmx_exit_handled_cr),
4465 * but did change L0 shadowed bits. This can currently happen
4466 * with the TS bit: L0 may want to leave TS on (for lazy fpu
4467 * loading) while pretending to allow the guest to change it.
4469 if (kvm_set_cr0(vcpu, (val & vcpu->arch.cr0_guest_owned_bits) |
4470 (vcpu->arch.cr0 & ~vcpu->arch.cr0_guest_owned_bits)))
4472 vmcs_writel(CR0_READ_SHADOW, val);
4475 return kvm_set_cr0(vcpu, val);
4478 static int handle_set_cr4(struct kvm_vcpu *vcpu, unsigned long val)
4480 if (is_guest_mode(vcpu)) {
4481 if (kvm_set_cr4(vcpu, (val & vcpu->arch.cr4_guest_owned_bits) |
4482 (vcpu->arch.cr4 & ~vcpu->arch.cr4_guest_owned_bits)))
4484 vmcs_writel(CR4_READ_SHADOW, val);
4487 return kvm_set_cr4(vcpu, val);
4490 /* called to set cr0 as approriate for clts instruction exit. */
4491 static void handle_clts(struct kvm_vcpu *vcpu)
4493 if (is_guest_mode(vcpu)) {
4495 * We get here when L2 did CLTS, and L1 didn't shadow CR0.TS
4496 * but we did (!fpu_active). We need to keep GUEST_CR0.TS on,
4497 * just pretend it's off (also in arch.cr0 for fpu_activate).
4499 vmcs_writel(CR0_READ_SHADOW,
4500 vmcs_readl(CR0_READ_SHADOW) & ~X86_CR0_TS);
4501 vcpu->arch.cr0 &= ~X86_CR0_TS;
4503 vmx_set_cr0(vcpu, kvm_read_cr0_bits(vcpu, ~X86_CR0_TS));
4506 static int handle_cr(struct kvm_vcpu *vcpu)
4508 unsigned long exit_qualification, val;
4513 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
4514 cr = exit_qualification & 15;
4515 reg = (exit_qualification >> 8) & 15;
4516 switch ((exit_qualification >> 4) & 3) {
4517 case 0: /* mov to cr */
4518 val = kvm_register_read(vcpu, reg);
4519 trace_kvm_cr_write(cr, val);
4522 err = handle_set_cr0(vcpu, val);
4523 kvm_complete_insn_gp(vcpu, err);
4526 err = kvm_set_cr3(vcpu, val);
4527 kvm_complete_insn_gp(vcpu, err);
4530 err = handle_set_cr4(vcpu, val);
4531 kvm_complete_insn_gp(vcpu, err);
4534 u8 cr8_prev = kvm_get_cr8(vcpu);
4535 u8 cr8 = kvm_register_read(vcpu, reg);
4536 err = kvm_set_cr8(vcpu, cr8);
4537 kvm_complete_insn_gp(vcpu, err);
4538 if (irqchip_in_kernel(vcpu->kvm))
4540 if (cr8_prev <= cr8)
4542 vcpu->run->exit_reason = KVM_EXIT_SET_TPR;
4549 trace_kvm_cr_write(0, kvm_read_cr0(vcpu));
4550 skip_emulated_instruction(vcpu);
4551 vmx_fpu_activate(vcpu);
4553 case 1: /*mov from cr*/
4556 val = kvm_read_cr3(vcpu);
4557 kvm_register_write(vcpu, reg, val);
4558 trace_kvm_cr_read(cr, val);
4559 skip_emulated_instruction(vcpu);
4562 val = kvm_get_cr8(vcpu);
4563 kvm_register_write(vcpu, reg, val);
4564 trace_kvm_cr_read(cr, val);
4565 skip_emulated_instruction(vcpu);
4570 val = (exit_qualification >> LMSW_SOURCE_DATA_SHIFT) & 0x0f;
4571 trace_kvm_cr_write(0, (kvm_read_cr0(vcpu) & ~0xful) | val);
4572 kvm_lmsw(vcpu, val);
4574 skip_emulated_instruction(vcpu);
4579 vcpu->run->exit_reason = 0;
4580 vcpu_unimpl(vcpu, "unhandled control register: op %d cr %d\n",
4581 (int)(exit_qualification >> 4) & 3, cr);
4585 static int handle_dr(struct kvm_vcpu *vcpu)
4587 unsigned long exit_qualification;
4590 /* Do not handle if the CPL > 0, will trigger GP on re-entry */
4591 if (!kvm_require_cpl(vcpu, 0))
4593 dr = vmcs_readl(GUEST_DR7);
4596 * As the vm-exit takes precedence over the debug trap, we
4597 * need to emulate the latter, either for the host or the
4598 * guest debugging itself.
4600 if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP) {
4601 vcpu->run->debug.arch.dr6 = vcpu->arch.dr6;
4602 vcpu->run->debug.arch.dr7 = dr;
4603 vcpu->run->debug.arch.pc =
4604 vmcs_readl(GUEST_CS_BASE) +
4605 vmcs_readl(GUEST_RIP);
4606 vcpu->run->debug.arch.exception = DB_VECTOR;
4607 vcpu->run->exit_reason = KVM_EXIT_DEBUG;
4610 vcpu->arch.dr7 &= ~DR7_GD;
4611 vcpu->arch.dr6 |= DR6_BD;
4612 vmcs_writel(GUEST_DR7, vcpu->arch.dr7);
4613 kvm_queue_exception(vcpu, DB_VECTOR);
4618 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
4619 dr = exit_qualification & DEBUG_REG_ACCESS_NUM;
4620 reg = DEBUG_REG_ACCESS_REG(exit_qualification);
4621 if (exit_qualification & TYPE_MOV_FROM_DR) {
4623 if (!kvm_get_dr(vcpu, dr, &val))
4624 kvm_register_write(vcpu, reg, val);
4626 kvm_set_dr(vcpu, dr, vcpu->arch.regs[reg]);
4627 skip_emulated_instruction(vcpu);
4631 static void vmx_set_dr7(struct kvm_vcpu *vcpu, unsigned long val)
4633 vmcs_writel(GUEST_DR7, val);
4636 static int handle_cpuid(struct kvm_vcpu *vcpu)
4638 kvm_emulate_cpuid(vcpu);
4642 static int handle_rdmsr(struct kvm_vcpu *vcpu)
4644 u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX];
4647 if (vmx_get_msr(vcpu, ecx, &data)) {
4648 trace_kvm_msr_read_ex(ecx);
4649 kvm_inject_gp(vcpu, 0);
4653 trace_kvm_msr_read(ecx, data);
4655 /* FIXME: handling of bits 32:63 of rax, rdx */
4656 vcpu->arch.regs[VCPU_REGS_RAX] = data & -1u;
4657 vcpu->arch.regs[VCPU_REGS_RDX] = (data >> 32) & -1u;
4658 skip_emulated_instruction(vcpu);
4662 static int handle_wrmsr(struct kvm_vcpu *vcpu)
4664 struct msr_data msr;
4665 u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX];
4666 u64 data = (vcpu->arch.regs[VCPU_REGS_RAX] & -1u)
4667 | ((u64)(vcpu->arch.regs[VCPU_REGS_RDX] & -1u) << 32);
4671 msr.host_initiated = false;
4672 if (vmx_set_msr(vcpu, &msr) != 0) {
4673 trace_kvm_msr_write_ex(ecx, data);
4674 kvm_inject_gp(vcpu, 0);
4678 trace_kvm_msr_write(ecx, data);
4679 skip_emulated_instruction(vcpu);
4683 static int handle_tpr_below_threshold(struct kvm_vcpu *vcpu)
4685 kvm_make_request(KVM_REQ_EVENT, vcpu);
4689 static int handle_interrupt_window(struct kvm_vcpu *vcpu)
4691 u32 cpu_based_vm_exec_control;
4693 /* clear pending irq */
4694 cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
4695 cpu_based_vm_exec_control &= ~CPU_BASED_VIRTUAL_INTR_PENDING;
4696 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control);
4698 kvm_make_request(KVM_REQ_EVENT, vcpu);
4700 ++vcpu->stat.irq_window_exits;
4703 * If the user space waits to inject interrupts, exit as soon as
4706 if (!irqchip_in_kernel(vcpu->kvm) &&
4707 vcpu->run->request_interrupt_window &&
4708 !kvm_cpu_has_interrupt(vcpu)) {
4709 vcpu->run->exit_reason = KVM_EXIT_IRQ_WINDOW_OPEN;
4715 static int handle_halt(struct kvm_vcpu *vcpu)
4717 skip_emulated_instruction(vcpu);
4718 return kvm_emulate_halt(vcpu);
4721 static int handle_vmcall(struct kvm_vcpu *vcpu)
4723 skip_emulated_instruction(vcpu);
4724 kvm_emulate_hypercall(vcpu);
4728 static int handle_invd(struct kvm_vcpu *vcpu)
4730 return emulate_instruction(vcpu, 0) == EMULATE_DONE;
4733 static int handle_invlpg(struct kvm_vcpu *vcpu)
4735 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
4737 kvm_mmu_invlpg(vcpu, exit_qualification);
4738 skip_emulated_instruction(vcpu);
4742 static int handle_rdpmc(struct kvm_vcpu *vcpu)
4746 err = kvm_rdpmc(vcpu);
4747 kvm_complete_insn_gp(vcpu, err);
4752 static int handle_wbinvd(struct kvm_vcpu *vcpu)
4754 skip_emulated_instruction(vcpu);
4755 kvm_emulate_wbinvd(vcpu);
4759 static int handle_xsetbv(struct kvm_vcpu *vcpu)
4761 u64 new_bv = kvm_read_edx_eax(vcpu);
4762 u32 index = kvm_register_read(vcpu, VCPU_REGS_RCX);
4764 if (kvm_set_xcr(vcpu, index, new_bv) == 0)
4765 skip_emulated_instruction(vcpu);
4769 static int handle_apic_access(struct kvm_vcpu *vcpu)
4771 if (likely(fasteoi)) {
4772 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
4773 int access_type, offset;
4775 access_type = exit_qualification & APIC_ACCESS_TYPE;
4776 offset = exit_qualification & APIC_ACCESS_OFFSET;
4778 * Sane guest uses MOV to write EOI, with written value
4779 * not cared. So make a short-circuit here by avoiding
4780 * heavy instruction emulation.
4782 if ((access_type == TYPE_LINEAR_APIC_INST_WRITE) &&
4783 (offset == APIC_EOI)) {
4784 kvm_lapic_set_eoi(vcpu);
4785 skip_emulated_instruction(vcpu);
4789 return emulate_instruction(vcpu, 0) == EMULATE_DONE;
4792 static int handle_task_switch(struct kvm_vcpu *vcpu)
4794 struct vcpu_vmx *vmx = to_vmx(vcpu);
4795 unsigned long exit_qualification;
4796 bool has_error_code = false;
4799 int reason, type, idt_v, idt_index;
4801 idt_v = (vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK);
4802 idt_index = (vmx->idt_vectoring_info & VECTORING_INFO_VECTOR_MASK);
4803 type = (vmx->idt_vectoring_info & VECTORING_INFO_TYPE_MASK);
4805 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
4807 reason = (u32)exit_qualification >> 30;
4808 if (reason == TASK_SWITCH_GATE && idt_v) {
4810 case INTR_TYPE_NMI_INTR:
4811 vcpu->arch.nmi_injected = false;
4812 vmx_set_nmi_mask(vcpu, true);
4814 case INTR_TYPE_EXT_INTR:
4815 case INTR_TYPE_SOFT_INTR:
4816 kvm_clear_interrupt_queue(vcpu);
4818 case INTR_TYPE_HARD_EXCEPTION:
4819 if (vmx->idt_vectoring_info &
4820 VECTORING_INFO_DELIVER_CODE_MASK) {
4821 has_error_code = true;
4823 vmcs_read32(IDT_VECTORING_ERROR_CODE);
4826 case INTR_TYPE_SOFT_EXCEPTION:
4827 kvm_clear_exception_queue(vcpu);
4833 tss_selector = exit_qualification;
4835 if (!idt_v || (type != INTR_TYPE_HARD_EXCEPTION &&
4836 type != INTR_TYPE_EXT_INTR &&
4837 type != INTR_TYPE_NMI_INTR))
4838 skip_emulated_instruction(vcpu);
4840 if (kvm_task_switch(vcpu, tss_selector,
4841 type == INTR_TYPE_SOFT_INTR ? idt_index : -1, reason,
4842 has_error_code, error_code) == EMULATE_FAIL) {
4843 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
4844 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
4845 vcpu->run->internal.ndata = 0;
4849 /* clear all local breakpoint enable flags */
4850 vmcs_writel(GUEST_DR7, vmcs_readl(GUEST_DR7) & ~55);
4853 * TODO: What about debug traps on tss switch?
4854 * Are we supposed to inject them and update dr6?
4860 static int handle_ept_violation(struct kvm_vcpu *vcpu)
4862 unsigned long exit_qualification;
4867 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
4869 gla_validity = (exit_qualification >> 7) & 0x3;
4870 if (gla_validity != 0x3 && gla_validity != 0x1 && gla_validity != 0) {
4871 printk(KERN_ERR "EPT: Handling EPT violation failed!\n");
4872 printk(KERN_ERR "EPT: GPA: 0x%lx, GVA: 0x%lx\n",
4873 (long unsigned int)vmcs_read64(GUEST_PHYSICAL_ADDRESS),
4874 vmcs_readl(GUEST_LINEAR_ADDRESS));
4875 printk(KERN_ERR "EPT: Exit qualification is 0x%lx\n",
4876 (long unsigned int)exit_qualification);
4877 vcpu->run->exit_reason = KVM_EXIT_UNKNOWN;
4878 vcpu->run->hw.hardware_exit_reason = EXIT_REASON_EPT_VIOLATION;
4882 gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS);
4883 trace_kvm_page_fault(gpa, exit_qualification);
4885 /* It is a write fault? */
4886 error_code = exit_qualification & (1U << 1);
4887 /* ept page table is present? */
4888 error_code |= (exit_qualification >> 3) & 0x1;
4890 return kvm_mmu_page_fault(vcpu, gpa, error_code, NULL, 0);
4893 static u64 ept_rsvd_mask(u64 spte, int level)
4898 for (i = 51; i > boot_cpu_data.x86_phys_bits; i--)
4899 mask |= (1ULL << i);
4902 /* bits 7:3 reserved */
4904 else if (level == 2) {
4905 if (spte & (1ULL << 7))
4906 /* 2MB ref, bits 20:12 reserved */
4909 /* bits 6:3 reserved */
4916 static void ept_misconfig_inspect_spte(struct kvm_vcpu *vcpu, u64 spte,
4919 printk(KERN_ERR "%s: spte 0x%llx level %d\n", __func__, spte, level);
4921 /* 010b (write-only) */
4922 WARN_ON((spte & 0x7) == 0x2);
4924 /* 110b (write/execute) */
4925 WARN_ON((spte & 0x7) == 0x6);
4927 /* 100b (execute-only) and value not supported by logical processor */
4928 if (!cpu_has_vmx_ept_execute_only())
4929 WARN_ON((spte & 0x7) == 0x4);
4933 u64 rsvd_bits = spte & ept_rsvd_mask(spte, level);
4935 if (rsvd_bits != 0) {
4936 printk(KERN_ERR "%s: rsvd_bits = 0x%llx\n",
4937 __func__, rsvd_bits);
4941 if (level == 1 || (level == 2 && (spte & (1ULL << 7)))) {
4942 u64 ept_mem_type = (spte & 0x38) >> 3;
4944 if (ept_mem_type == 2 || ept_mem_type == 3 ||
4945 ept_mem_type == 7) {
4946 printk(KERN_ERR "%s: ept_mem_type=0x%llx\n",
4947 __func__, ept_mem_type);
4954 static int handle_ept_misconfig(struct kvm_vcpu *vcpu)
4957 int nr_sptes, i, ret;
4960 gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS);
4962 ret = handle_mmio_page_fault_common(vcpu, gpa, true);
4963 if (likely(ret == 1))
4964 return x86_emulate_instruction(vcpu, gpa, 0, NULL, 0) ==
4969 /* It is the real ept misconfig */
4970 printk(KERN_ERR "EPT: Misconfiguration.\n");
4971 printk(KERN_ERR "EPT: GPA: 0x%llx\n", gpa);
4973 nr_sptes = kvm_mmu_get_spte_hierarchy(vcpu, gpa, sptes);
4975 for (i = PT64_ROOT_LEVEL; i > PT64_ROOT_LEVEL - nr_sptes; --i)
4976 ept_misconfig_inspect_spte(vcpu, sptes[i-1], i);
4978 vcpu->run->exit_reason = KVM_EXIT_UNKNOWN;
4979 vcpu->run->hw.hardware_exit_reason = EXIT_REASON_EPT_MISCONFIG;
4984 static int handle_nmi_window(struct kvm_vcpu *vcpu)
4986 u32 cpu_based_vm_exec_control;
4988 /* clear pending NMI */
4989 cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
4990 cpu_based_vm_exec_control &= ~CPU_BASED_VIRTUAL_NMI_PENDING;
4991 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control);
4992 ++vcpu->stat.nmi_window_exits;
4993 kvm_make_request(KVM_REQ_EVENT, vcpu);
4998 static int handle_invalid_guest_state(struct kvm_vcpu *vcpu)
5000 struct vcpu_vmx *vmx = to_vmx(vcpu);
5001 enum emulation_result err = EMULATE_DONE;
5004 bool intr_window_requested;
5005 unsigned count = 130;
5007 cpu_exec_ctrl = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
5008 intr_window_requested = cpu_exec_ctrl & CPU_BASED_VIRTUAL_INTR_PENDING;
5010 while (!guest_state_valid(vcpu) && count-- != 0) {
5011 if (intr_window_requested && vmx_interrupt_allowed(vcpu))
5012 return handle_interrupt_window(&vmx->vcpu);
5014 if (test_bit(KVM_REQ_EVENT, &vcpu->requests))
5017 err = emulate_instruction(vcpu, 0);
5019 if (err == EMULATE_DO_MMIO) {
5024 if (err != EMULATE_DONE) {
5025 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
5026 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
5027 vcpu->run->internal.ndata = 0;
5031 if (signal_pending(current))
5037 vmx->emulation_required = !guest_state_valid(vcpu);
5043 * Indicate a busy-waiting vcpu in spinlock. We do not enable the PAUSE
5044 * exiting, so only get here on cpu with PAUSE-Loop-Exiting.
5046 static int handle_pause(struct kvm_vcpu *vcpu)
5048 skip_emulated_instruction(vcpu);
5049 kvm_vcpu_on_spin(vcpu);
5054 static int handle_invalid_op(struct kvm_vcpu *vcpu)
5056 kvm_queue_exception(vcpu, UD_VECTOR);
5061 * To run an L2 guest, we need a vmcs02 based on the L1-specified vmcs12.
5062 * We could reuse a single VMCS for all the L2 guests, but we also want the
5063 * option to allocate a separate vmcs02 for each separate loaded vmcs12 - this
5064 * allows keeping them loaded on the processor, and in the future will allow
5065 * optimizations where prepare_vmcs02 doesn't need to set all the fields on
5066 * every entry if they never change.
5067 * So we keep, in vmx->nested.vmcs02_pool, a cache of size VMCS02_POOL_SIZE
5068 * (>=0) with a vmcs02 for each recently loaded vmcs12s, most recent first.
5070 * The following functions allocate and free a vmcs02 in this pool.
5073 /* Get a VMCS from the pool to use as vmcs02 for the current vmcs12. */
5074 static struct loaded_vmcs *nested_get_current_vmcs02(struct vcpu_vmx *vmx)
5076 struct vmcs02_list *item;
5077 list_for_each_entry(item, &vmx->nested.vmcs02_pool, list)
5078 if (item->vmptr == vmx->nested.current_vmptr) {
5079 list_move(&item->list, &vmx->nested.vmcs02_pool);
5080 return &item->vmcs02;
5083 if (vmx->nested.vmcs02_num >= max(VMCS02_POOL_SIZE, 1)) {
5084 /* Recycle the least recently used VMCS. */
5085 item = list_entry(vmx->nested.vmcs02_pool.prev,
5086 struct vmcs02_list, list);
5087 item->vmptr = vmx->nested.current_vmptr;
5088 list_move(&item->list, &vmx->nested.vmcs02_pool);
5089 return &item->vmcs02;
5092 /* Create a new VMCS */
5093 item = (struct vmcs02_list *)
5094 kmalloc(sizeof(struct vmcs02_list), GFP_KERNEL);
5097 item->vmcs02.vmcs = alloc_vmcs();
5098 if (!item->vmcs02.vmcs) {
5102 loaded_vmcs_init(&item->vmcs02);
5103 item->vmptr = vmx->nested.current_vmptr;
5104 list_add(&(item->list), &(vmx->nested.vmcs02_pool));
5105 vmx->nested.vmcs02_num++;
5106 return &item->vmcs02;
5109 /* Free and remove from pool a vmcs02 saved for a vmcs12 (if there is one) */
5110 static void nested_free_vmcs02(struct vcpu_vmx *vmx, gpa_t vmptr)
5112 struct vmcs02_list *item;
5113 list_for_each_entry(item, &vmx->nested.vmcs02_pool, list)
5114 if (item->vmptr == vmptr) {
5115 free_loaded_vmcs(&item->vmcs02);
5116 list_del(&item->list);
5118 vmx->nested.vmcs02_num--;
5124 * Free all VMCSs saved for this vcpu, except the one pointed by
5125 * vmx->loaded_vmcs. These include the VMCSs in vmcs02_pool (except the one
5126 * currently used, if running L2), and vmcs01 when running L2.
5128 static void nested_free_all_saved_vmcss(struct vcpu_vmx *vmx)
5130 struct vmcs02_list *item, *n;
5131 list_for_each_entry_safe(item, n, &vmx->nested.vmcs02_pool, list) {
5132 if (vmx->loaded_vmcs != &item->vmcs02)
5133 free_loaded_vmcs(&item->vmcs02);
5134 list_del(&item->list);
5137 vmx->nested.vmcs02_num = 0;
5139 if (vmx->loaded_vmcs != &vmx->vmcs01)
5140 free_loaded_vmcs(&vmx->vmcs01);
5144 * Emulate the VMXON instruction.
5145 * Currently, we just remember that VMX is active, and do not save or even
5146 * inspect the argument to VMXON (the so-called "VMXON pointer") because we
5147 * do not currently need to store anything in that guest-allocated memory
5148 * region. Consequently, VMCLEAR and VMPTRLD also do not verify that the their
5149 * argument is different from the VMXON pointer (which the spec says they do).
5151 static int handle_vmon(struct kvm_vcpu *vcpu)
5153 struct kvm_segment cs;
5154 struct vcpu_vmx *vmx = to_vmx(vcpu);
5156 /* The Intel VMX Instruction Reference lists a bunch of bits that
5157 * are prerequisite to running VMXON, most notably cr4.VMXE must be
5158 * set to 1 (see vmx_set_cr4() for when we allow the guest to set this).
5159 * Otherwise, we should fail with #UD. We test these now:
5161 if (!kvm_read_cr4_bits(vcpu, X86_CR4_VMXE) ||
5162 !kvm_read_cr0_bits(vcpu, X86_CR0_PE) ||
5163 (vmx_get_rflags(vcpu) & X86_EFLAGS_VM)) {
5164 kvm_queue_exception(vcpu, UD_VECTOR);
5168 vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
5169 if (is_long_mode(vcpu) && !cs.l) {
5170 kvm_queue_exception(vcpu, UD_VECTOR);
5174 if (vmx_get_cpl(vcpu)) {
5175 kvm_inject_gp(vcpu, 0);
5179 INIT_LIST_HEAD(&(vmx->nested.vmcs02_pool));
5180 vmx->nested.vmcs02_num = 0;
5182 vmx->nested.vmxon = true;
5184 skip_emulated_instruction(vcpu);
5189 * Intel's VMX Instruction Reference specifies a common set of prerequisites
5190 * for running VMX instructions (except VMXON, whose prerequisites are
5191 * slightly different). It also specifies what exception to inject otherwise.
5193 static int nested_vmx_check_permission(struct kvm_vcpu *vcpu)
5195 struct kvm_segment cs;
5196 struct vcpu_vmx *vmx = to_vmx(vcpu);
5198 if (!vmx->nested.vmxon) {
5199 kvm_queue_exception(vcpu, UD_VECTOR);
5203 vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
5204 if ((vmx_get_rflags(vcpu) & X86_EFLAGS_VM) ||
5205 (is_long_mode(vcpu) && !cs.l)) {
5206 kvm_queue_exception(vcpu, UD_VECTOR);
5210 if (vmx_get_cpl(vcpu)) {
5211 kvm_inject_gp(vcpu, 0);
5219 * Free whatever needs to be freed from vmx->nested when L1 goes down, or
5220 * just stops using VMX.
5222 static void free_nested(struct vcpu_vmx *vmx)
5224 if (!vmx->nested.vmxon)
5226 vmx->nested.vmxon = false;
5227 if (vmx->nested.current_vmptr != -1ull) {
5228 kunmap(vmx->nested.current_vmcs12_page);
5229 nested_release_page(vmx->nested.current_vmcs12_page);
5230 vmx->nested.current_vmptr = -1ull;
5231 vmx->nested.current_vmcs12 = NULL;
5233 /* Unpin physical memory we referred to in current vmcs02 */
5234 if (vmx->nested.apic_access_page) {
5235 nested_release_page(vmx->nested.apic_access_page);
5236 vmx->nested.apic_access_page = 0;
5239 nested_free_all_saved_vmcss(vmx);
5242 /* Emulate the VMXOFF instruction */
5243 static int handle_vmoff(struct kvm_vcpu *vcpu)
5245 if (!nested_vmx_check_permission(vcpu))
5247 free_nested(to_vmx(vcpu));
5248 skip_emulated_instruction(vcpu);
5253 * Decode the memory-address operand of a vmx instruction, as recorded on an
5254 * exit caused by such an instruction (run by a guest hypervisor).
5255 * On success, returns 0. When the operand is invalid, returns 1 and throws
5258 static int get_vmx_mem_address(struct kvm_vcpu *vcpu,
5259 unsigned long exit_qualification,
5260 u32 vmx_instruction_info, gva_t *ret)
5263 * According to Vol. 3B, "Information for VM Exits Due to Instruction
5264 * Execution", on an exit, vmx_instruction_info holds most of the
5265 * addressing components of the operand. Only the displacement part
5266 * is put in exit_qualification (see 3B, "Basic VM-Exit Information").
5267 * For how an actual address is calculated from all these components,
5268 * refer to Vol. 1, "Operand Addressing".
5270 int scaling = vmx_instruction_info & 3;
5271 int addr_size = (vmx_instruction_info >> 7) & 7;
5272 bool is_reg = vmx_instruction_info & (1u << 10);
5273 int seg_reg = (vmx_instruction_info >> 15) & 7;
5274 int index_reg = (vmx_instruction_info >> 18) & 0xf;
5275 bool index_is_valid = !(vmx_instruction_info & (1u << 22));
5276 int base_reg = (vmx_instruction_info >> 23) & 0xf;
5277 bool base_is_valid = !(vmx_instruction_info & (1u << 27));
5280 kvm_queue_exception(vcpu, UD_VECTOR);
5284 /* Addr = segment_base + offset */
5285 /* offset = base + [index * scale] + displacement */
5286 *ret = vmx_get_segment_base(vcpu, seg_reg);
5288 *ret += kvm_register_read(vcpu, base_reg);
5290 *ret += kvm_register_read(vcpu, index_reg)<<scaling;
5291 *ret += exit_qualification; /* holds the displacement */
5293 if (addr_size == 1) /* 32 bit */
5297 * TODO: throw #GP (and return 1) in various cases that the VM*
5298 * instructions require it - e.g., offset beyond segment limit,
5299 * unusable or unreadable/unwritable segment, non-canonical 64-bit
5300 * address, and so on. Currently these are not checked.
5306 * The following 3 functions, nested_vmx_succeed()/failValid()/failInvalid(),
5307 * set the success or error code of an emulated VMX instruction, as specified
5308 * by Vol 2B, VMX Instruction Reference, "Conventions".
5310 static void nested_vmx_succeed(struct kvm_vcpu *vcpu)
5312 vmx_set_rflags(vcpu, vmx_get_rflags(vcpu)
5313 & ~(X86_EFLAGS_CF | X86_EFLAGS_PF | X86_EFLAGS_AF |
5314 X86_EFLAGS_ZF | X86_EFLAGS_SF | X86_EFLAGS_OF));
5317 static void nested_vmx_failInvalid(struct kvm_vcpu *vcpu)
5319 vmx_set_rflags(vcpu, (vmx_get_rflags(vcpu)
5320 & ~(X86_EFLAGS_PF | X86_EFLAGS_AF | X86_EFLAGS_ZF |
5321 X86_EFLAGS_SF | X86_EFLAGS_OF))
5325 static void nested_vmx_failValid(struct kvm_vcpu *vcpu,
5326 u32 vm_instruction_error)
5328 if (to_vmx(vcpu)->nested.current_vmptr == -1ull) {
5330 * failValid writes the error number to the current VMCS, which
5331 * can't be done there isn't a current VMCS.
5333 nested_vmx_failInvalid(vcpu);
5336 vmx_set_rflags(vcpu, (vmx_get_rflags(vcpu)
5337 & ~(X86_EFLAGS_CF | X86_EFLAGS_PF | X86_EFLAGS_AF |
5338 X86_EFLAGS_SF | X86_EFLAGS_OF))
5340 get_vmcs12(vcpu)->vm_instruction_error = vm_instruction_error;
5343 /* Emulate the VMCLEAR instruction */
5344 static int handle_vmclear(struct kvm_vcpu *vcpu)
5346 struct vcpu_vmx *vmx = to_vmx(vcpu);
5349 struct vmcs12 *vmcs12;
5351 struct x86_exception e;
5353 if (!nested_vmx_check_permission(vcpu))
5356 if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
5357 vmcs_read32(VMX_INSTRUCTION_INFO), &gva))
5360 if (kvm_read_guest_virt(&vcpu->arch.emulate_ctxt, gva, &vmptr,
5361 sizeof(vmptr), &e)) {
5362 kvm_inject_page_fault(vcpu, &e);
5366 if (!IS_ALIGNED(vmptr, PAGE_SIZE)) {
5367 nested_vmx_failValid(vcpu, VMXERR_VMCLEAR_INVALID_ADDRESS);
5368 skip_emulated_instruction(vcpu);
5372 if (vmptr == vmx->nested.current_vmptr) {
5373 kunmap(vmx->nested.current_vmcs12_page);
5374 nested_release_page(vmx->nested.current_vmcs12_page);
5375 vmx->nested.current_vmptr = -1ull;
5376 vmx->nested.current_vmcs12 = NULL;
5379 page = nested_get_page(vcpu, vmptr);
5382 * For accurate processor emulation, VMCLEAR beyond available
5383 * physical memory should do nothing at all. However, it is
5384 * possible that a nested vmx bug, not a guest hypervisor bug,
5385 * resulted in this case, so let's shut down before doing any
5388 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
5391 vmcs12 = kmap(page);
5392 vmcs12->launch_state = 0;
5394 nested_release_page(page);
5396 nested_free_vmcs02(vmx, vmptr);
5398 skip_emulated_instruction(vcpu);
5399 nested_vmx_succeed(vcpu);
5403 static int nested_vmx_run(struct kvm_vcpu *vcpu, bool launch);
5405 /* Emulate the VMLAUNCH instruction */
5406 static int handle_vmlaunch(struct kvm_vcpu *vcpu)
5408 return nested_vmx_run(vcpu, true);
5411 /* Emulate the VMRESUME instruction */
5412 static int handle_vmresume(struct kvm_vcpu *vcpu)
5415 return nested_vmx_run(vcpu, false);
5418 enum vmcs_field_type {
5419 VMCS_FIELD_TYPE_U16 = 0,
5420 VMCS_FIELD_TYPE_U64 = 1,
5421 VMCS_FIELD_TYPE_U32 = 2,
5422 VMCS_FIELD_TYPE_NATURAL_WIDTH = 3
5425 static inline int vmcs_field_type(unsigned long field)
5427 if (0x1 & field) /* the *_HIGH fields are all 32 bit */
5428 return VMCS_FIELD_TYPE_U32;
5429 return (field >> 13) & 0x3 ;
5432 static inline int vmcs_field_readonly(unsigned long field)
5434 return (((field >> 10) & 0x3) == 1);
5438 * Read a vmcs12 field. Since these can have varying lengths and we return
5439 * one type, we chose the biggest type (u64) and zero-extend the return value
5440 * to that size. Note that the caller, handle_vmread, might need to use only
5441 * some of the bits we return here (e.g., on 32-bit guests, only 32 bits of
5442 * 64-bit fields are to be returned).
5444 static inline bool vmcs12_read_any(struct kvm_vcpu *vcpu,
5445 unsigned long field, u64 *ret)
5447 short offset = vmcs_field_to_offset(field);
5453 p = ((char *)(get_vmcs12(vcpu))) + offset;
5455 switch (vmcs_field_type(field)) {
5456 case VMCS_FIELD_TYPE_NATURAL_WIDTH:
5457 *ret = *((natural_width *)p);
5459 case VMCS_FIELD_TYPE_U16:
5462 case VMCS_FIELD_TYPE_U32:
5465 case VMCS_FIELD_TYPE_U64:
5469 return 0; /* can never happen. */
5474 * VMX instructions which assume a current vmcs12 (i.e., that VMPTRLD was
5475 * used before) all generate the same failure when it is missing.
5477 static int nested_vmx_check_vmcs12(struct kvm_vcpu *vcpu)
5479 struct vcpu_vmx *vmx = to_vmx(vcpu);
5480 if (vmx->nested.current_vmptr == -1ull) {
5481 nested_vmx_failInvalid(vcpu);
5482 skip_emulated_instruction(vcpu);
5488 static int handle_vmread(struct kvm_vcpu *vcpu)
5490 unsigned long field;
5492 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
5493 u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
5496 if (!nested_vmx_check_permission(vcpu) ||
5497 !nested_vmx_check_vmcs12(vcpu))
5500 /* Decode instruction info and find the field to read */
5501 field = kvm_register_read(vcpu, (((vmx_instruction_info) >> 28) & 0xf));
5502 /* Read the field, zero-extended to a u64 field_value */
5503 if (!vmcs12_read_any(vcpu, field, &field_value)) {
5504 nested_vmx_failValid(vcpu, VMXERR_UNSUPPORTED_VMCS_COMPONENT);
5505 skip_emulated_instruction(vcpu);
5509 * Now copy part of this value to register or memory, as requested.
5510 * Note that the number of bits actually copied is 32 or 64 depending
5511 * on the guest's mode (32 or 64 bit), not on the given field's length.
5513 if (vmx_instruction_info & (1u << 10)) {
5514 kvm_register_write(vcpu, (((vmx_instruction_info) >> 3) & 0xf),
5517 if (get_vmx_mem_address(vcpu, exit_qualification,
5518 vmx_instruction_info, &gva))
5520 /* _system ok, as nested_vmx_check_permission verified cpl=0 */
5521 kvm_write_guest_virt_system(&vcpu->arch.emulate_ctxt, gva,
5522 &field_value, (is_long_mode(vcpu) ? 8 : 4), NULL);
5525 nested_vmx_succeed(vcpu);
5526 skip_emulated_instruction(vcpu);
5531 static int handle_vmwrite(struct kvm_vcpu *vcpu)
5533 unsigned long field;
5535 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
5536 u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
5539 /* The value to write might be 32 or 64 bits, depending on L1's long
5540 * mode, and eventually we need to write that into a field of several
5541 * possible lengths. The code below first zero-extends the value to 64
5542 * bit (field_value), and then copies only the approriate number of
5543 * bits into the vmcs12 field.
5545 u64 field_value = 0;
5546 struct x86_exception e;
5548 if (!nested_vmx_check_permission(vcpu) ||
5549 !nested_vmx_check_vmcs12(vcpu))
5552 if (vmx_instruction_info & (1u << 10))
5553 field_value = kvm_register_read(vcpu,
5554 (((vmx_instruction_info) >> 3) & 0xf));
5556 if (get_vmx_mem_address(vcpu, exit_qualification,
5557 vmx_instruction_info, &gva))
5559 if (kvm_read_guest_virt(&vcpu->arch.emulate_ctxt, gva,
5560 &field_value, (is_long_mode(vcpu) ? 8 : 4), &e)) {
5561 kvm_inject_page_fault(vcpu, &e);
5567 field = kvm_register_read(vcpu, (((vmx_instruction_info) >> 28) & 0xf));
5568 if (vmcs_field_readonly(field)) {
5569 nested_vmx_failValid(vcpu,
5570 VMXERR_VMWRITE_READ_ONLY_VMCS_COMPONENT);
5571 skip_emulated_instruction(vcpu);
5575 offset = vmcs_field_to_offset(field);
5577 nested_vmx_failValid(vcpu, VMXERR_UNSUPPORTED_VMCS_COMPONENT);
5578 skip_emulated_instruction(vcpu);
5581 p = ((char *) get_vmcs12(vcpu)) + offset;
5583 switch (vmcs_field_type(field)) {
5584 case VMCS_FIELD_TYPE_U16:
5585 *(u16 *)p = field_value;
5587 case VMCS_FIELD_TYPE_U32:
5588 *(u32 *)p = field_value;
5590 case VMCS_FIELD_TYPE_U64:
5591 *(u64 *)p = field_value;
5593 case VMCS_FIELD_TYPE_NATURAL_WIDTH:
5594 *(natural_width *)p = field_value;
5597 nested_vmx_failValid(vcpu, VMXERR_UNSUPPORTED_VMCS_COMPONENT);
5598 skip_emulated_instruction(vcpu);
5602 nested_vmx_succeed(vcpu);
5603 skip_emulated_instruction(vcpu);
5607 /* Emulate the VMPTRLD instruction */
5608 static int handle_vmptrld(struct kvm_vcpu *vcpu)
5610 struct vcpu_vmx *vmx = to_vmx(vcpu);
5613 struct x86_exception e;
5615 if (!nested_vmx_check_permission(vcpu))
5618 if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
5619 vmcs_read32(VMX_INSTRUCTION_INFO), &gva))
5622 if (kvm_read_guest_virt(&vcpu->arch.emulate_ctxt, gva, &vmptr,
5623 sizeof(vmptr), &e)) {
5624 kvm_inject_page_fault(vcpu, &e);
5628 if (!IS_ALIGNED(vmptr, PAGE_SIZE)) {
5629 nested_vmx_failValid(vcpu, VMXERR_VMPTRLD_INVALID_ADDRESS);
5630 skip_emulated_instruction(vcpu);
5634 if (vmx->nested.current_vmptr != vmptr) {
5635 struct vmcs12 *new_vmcs12;
5637 page = nested_get_page(vcpu, vmptr);
5639 nested_vmx_failInvalid(vcpu);
5640 skip_emulated_instruction(vcpu);
5643 new_vmcs12 = kmap(page);
5644 if (new_vmcs12->revision_id != VMCS12_REVISION) {
5646 nested_release_page_clean(page);
5647 nested_vmx_failValid(vcpu,
5648 VMXERR_VMPTRLD_INCORRECT_VMCS_REVISION_ID);
5649 skip_emulated_instruction(vcpu);
5652 if (vmx->nested.current_vmptr != -1ull) {
5653 kunmap(vmx->nested.current_vmcs12_page);
5654 nested_release_page(vmx->nested.current_vmcs12_page);
5657 vmx->nested.current_vmptr = vmptr;
5658 vmx->nested.current_vmcs12 = new_vmcs12;
5659 vmx->nested.current_vmcs12_page = page;
5662 nested_vmx_succeed(vcpu);
5663 skip_emulated_instruction(vcpu);
5667 /* Emulate the VMPTRST instruction */
5668 static int handle_vmptrst(struct kvm_vcpu *vcpu)
5670 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
5671 u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
5673 struct x86_exception e;
5675 if (!nested_vmx_check_permission(vcpu))
5678 if (get_vmx_mem_address(vcpu, exit_qualification,
5679 vmx_instruction_info, &vmcs_gva))
5681 /* ok to use *_system, as nested_vmx_check_permission verified cpl=0 */
5682 if (kvm_write_guest_virt_system(&vcpu->arch.emulate_ctxt, vmcs_gva,
5683 (void *)&to_vmx(vcpu)->nested.current_vmptr,
5685 kvm_inject_page_fault(vcpu, &e);
5688 nested_vmx_succeed(vcpu);
5689 skip_emulated_instruction(vcpu);
5694 * The exit handlers return 1 if the exit was handled fully and guest execution
5695 * may resume. Otherwise they set the kvm_run parameter to indicate what needs
5696 * to be done to userspace and return 0.
5698 static int (*const kvm_vmx_exit_handlers[])(struct kvm_vcpu *vcpu) = {
5699 [EXIT_REASON_EXCEPTION_NMI] = handle_exception,
5700 [EXIT_REASON_EXTERNAL_INTERRUPT] = handle_external_interrupt,
5701 [EXIT_REASON_TRIPLE_FAULT] = handle_triple_fault,
5702 [EXIT_REASON_NMI_WINDOW] = handle_nmi_window,
5703 [EXIT_REASON_IO_INSTRUCTION] = handle_io,
5704 [EXIT_REASON_CR_ACCESS] = handle_cr,
5705 [EXIT_REASON_DR_ACCESS] = handle_dr,
5706 [EXIT_REASON_CPUID] = handle_cpuid,
5707 [EXIT_REASON_MSR_READ] = handle_rdmsr,
5708 [EXIT_REASON_MSR_WRITE] = handle_wrmsr,
5709 [EXIT_REASON_PENDING_INTERRUPT] = handle_interrupt_window,
5710 [EXIT_REASON_HLT] = handle_halt,
5711 [EXIT_REASON_INVD] = handle_invd,
5712 [EXIT_REASON_INVLPG] = handle_invlpg,
5713 [EXIT_REASON_RDPMC] = handle_rdpmc,
5714 [EXIT_REASON_VMCALL] = handle_vmcall,
5715 [EXIT_REASON_VMCLEAR] = handle_vmclear,
5716 [EXIT_REASON_VMLAUNCH] = handle_vmlaunch,
5717 [EXIT_REASON_VMPTRLD] = handle_vmptrld,
5718 [EXIT_REASON_VMPTRST] = handle_vmptrst,
5719 [EXIT_REASON_VMREAD] = handle_vmread,
5720 [EXIT_REASON_VMRESUME] = handle_vmresume,
5721 [EXIT_REASON_VMWRITE] = handle_vmwrite,
5722 [EXIT_REASON_VMOFF] = handle_vmoff,
5723 [EXIT_REASON_VMON] = handle_vmon,
5724 [EXIT_REASON_TPR_BELOW_THRESHOLD] = handle_tpr_below_threshold,
5725 [EXIT_REASON_APIC_ACCESS] = handle_apic_access,
5726 [EXIT_REASON_WBINVD] = handle_wbinvd,
5727 [EXIT_REASON_XSETBV] = handle_xsetbv,
5728 [EXIT_REASON_TASK_SWITCH] = handle_task_switch,
5729 [EXIT_REASON_MCE_DURING_VMENTRY] = handle_machine_check,
5730 [EXIT_REASON_EPT_VIOLATION] = handle_ept_violation,
5731 [EXIT_REASON_EPT_MISCONFIG] = handle_ept_misconfig,
5732 [EXIT_REASON_PAUSE_INSTRUCTION] = handle_pause,
5733 [EXIT_REASON_MWAIT_INSTRUCTION] = handle_invalid_op,
5734 [EXIT_REASON_MONITOR_INSTRUCTION] = handle_invalid_op,
5737 static const int kvm_vmx_max_exit_handlers =
5738 ARRAY_SIZE(kvm_vmx_exit_handlers);
5741 * Return 1 if we should exit from L2 to L1 to handle an MSR access access,
5742 * rather than handle it ourselves in L0. I.e., check whether L1 expressed
5743 * disinterest in the current event (read or write a specific MSR) by using an
5744 * MSR bitmap. This may be the case even when L0 doesn't use MSR bitmaps.
5746 static bool nested_vmx_exit_handled_msr(struct kvm_vcpu *vcpu,
5747 struct vmcs12 *vmcs12, u32 exit_reason)
5749 u32 msr_index = vcpu->arch.regs[VCPU_REGS_RCX];
5752 if (!nested_cpu_has(get_vmcs12(vcpu), CPU_BASED_USE_MSR_BITMAPS))
5756 * The MSR_BITMAP page is divided into four 1024-byte bitmaps,
5757 * for the four combinations of read/write and low/high MSR numbers.
5758 * First we need to figure out which of the four to use:
5760 bitmap = vmcs12->msr_bitmap;
5761 if (exit_reason == EXIT_REASON_MSR_WRITE)
5763 if (msr_index >= 0xc0000000) {
5764 msr_index -= 0xc0000000;
5768 /* Then read the msr_index'th bit from this bitmap: */
5769 if (msr_index < 1024*8) {
5771 kvm_read_guest(vcpu->kvm, bitmap + msr_index/8, &b, 1);
5772 return 1 & (b >> (msr_index & 7));
5774 return 1; /* let L1 handle the wrong parameter */
5778 * Return 1 if we should exit from L2 to L1 to handle a CR access exit,
5779 * rather than handle it ourselves in L0. I.e., check if L1 wanted to
5780 * intercept (via guest_host_mask etc.) the current event.
5782 static bool nested_vmx_exit_handled_cr(struct kvm_vcpu *vcpu,
5783 struct vmcs12 *vmcs12)
5785 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
5786 int cr = exit_qualification & 15;
5787 int reg = (exit_qualification >> 8) & 15;
5788 unsigned long val = kvm_register_read(vcpu, reg);
5790 switch ((exit_qualification >> 4) & 3) {
5791 case 0: /* mov to cr */
5794 if (vmcs12->cr0_guest_host_mask &
5795 (val ^ vmcs12->cr0_read_shadow))
5799 if ((vmcs12->cr3_target_count >= 1 &&
5800 vmcs12->cr3_target_value0 == val) ||
5801 (vmcs12->cr3_target_count >= 2 &&
5802 vmcs12->cr3_target_value1 == val) ||
5803 (vmcs12->cr3_target_count >= 3 &&
5804 vmcs12->cr3_target_value2 == val) ||
5805 (vmcs12->cr3_target_count >= 4 &&
5806 vmcs12->cr3_target_value3 == val))
5808 if (nested_cpu_has(vmcs12, CPU_BASED_CR3_LOAD_EXITING))
5812 if (vmcs12->cr4_guest_host_mask &
5813 (vmcs12->cr4_read_shadow ^ val))
5817 if (nested_cpu_has(vmcs12, CPU_BASED_CR8_LOAD_EXITING))
5823 if ((vmcs12->cr0_guest_host_mask & X86_CR0_TS) &&
5824 (vmcs12->cr0_read_shadow & X86_CR0_TS))
5827 case 1: /* mov from cr */
5830 if (vmcs12->cpu_based_vm_exec_control &
5831 CPU_BASED_CR3_STORE_EXITING)
5835 if (vmcs12->cpu_based_vm_exec_control &
5836 CPU_BASED_CR8_STORE_EXITING)
5843 * lmsw can change bits 1..3 of cr0, and only set bit 0 of
5844 * cr0. Other attempted changes are ignored, with no exit.
5846 if (vmcs12->cr0_guest_host_mask & 0xe &
5847 (val ^ vmcs12->cr0_read_shadow))
5849 if ((vmcs12->cr0_guest_host_mask & 0x1) &&
5850 !(vmcs12->cr0_read_shadow & 0x1) &&
5859 * Return 1 if we should exit from L2 to L1 to handle an exit, or 0 if we
5860 * should handle it ourselves in L0 (and then continue L2). Only call this
5861 * when in is_guest_mode (L2).
5863 static bool nested_vmx_exit_handled(struct kvm_vcpu *vcpu)
5865 u32 exit_reason = vmcs_read32(VM_EXIT_REASON);
5866 u32 intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
5867 struct vcpu_vmx *vmx = to_vmx(vcpu);
5868 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
5870 if (vmx->nested.nested_run_pending)
5873 if (unlikely(vmx->fail)) {
5874 pr_info_ratelimited("%s failed vm entry %x\n", __func__,
5875 vmcs_read32(VM_INSTRUCTION_ERROR));
5879 switch (exit_reason) {
5880 case EXIT_REASON_EXCEPTION_NMI:
5881 if (!is_exception(intr_info))
5883 else if (is_page_fault(intr_info))
5885 return vmcs12->exception_bitmap &
5886 (1u << (intr_info & INTR_INFO_VECTOR_MASK));
5887 case EXIT_REASON_EXTERNAL_INTERRUPT:
5889 case EXIT_REASON_TRIPLE_FAULT:
5891 case EXIT_REASON_PENDING_INTERRUPT:
5892 case EXIT_REASON_NMI_WINDOW:
5894 * prepare_vmcs02() set the CPU_BASED_VIRTUAL_INTR_PENDING bit
5895 * (aka Interrupt Window Exiting) only when L1 turned it on,
5896 * so if we got a PENDING_INTERRUPT exit, this must be for L1.
5897 * Same for NMI Window Exiting.
5900 case EXIT_REASON_TASK_SWITCH:
5902 case EXIT_REASON_CPUID:
5904 case EXIT_REASON_HLT:
5905 return nested_cpu_has(vmcs12, CPU_BASED_HLT_EXITING);
5906 case EXIT_REASON_INVD:
5908 case EXIT_REASON_INVLPG:
5909 return nested_cpu_has(vmcs12, CPU_BASED_INVLPG_EXITING);
5910 case EXIT_REASON_RDPMC:
5911 return nested_cpu_has(vmcs12, CPU_BASED_RDPMC_EXITING);
5912 case EXIT_REASON_RDTSC:
5913 return nested_cpu_has(vmcs12, CPU_BASED_RDTSC_EXITING);
5914 case EXIT_REASON_VMCALL: case EXIT_REASON_VMCLEAR:
5915 case EXIT_REASON_VMLAUNCH: case EXIT_REASON_VMPTRLD:
5916 case EXIT_REASON_VMPTRST: case EXIT_REASON_VMREAD:
5917 case EXIT_REASON_VMRESUME: case EXIT_REASON_VMWRITE:
5918 case EXIT_REASON_VMOFF: case EXIT_REASON_VMON:
5920 * VMX instructions trap unconditionally. This allows L1 to
5921 * emulate them for its L2 guest, i.e., allows 3-level nesting!
5924 case EXIT_REASON_CR_ACCESS:
5925 return nested_vmx_exit_handled_cr(vcpu, vmcs12);
5926 case EXIT_REASON_DR_ACCESS:
5927 return nested_cpu_has(vmcs12, CPU_BASED_MOV_DR_EXITING);
5928 case EXIT_REASON_IO_INSTRUCTION:
5929 /* TODO: support IO bitmaps */
5931 case EXIT_REASON_MSR_READ:
5932 case EXIT_REASON_MSR_WRITE:
5933 return nested_vmx_exit_handled_msr(vcpu, vmcs12, exit_reason);
5934 case EXIT_REASON_INVALID_STATE:
5936 case EXIT_REASON_MWAIT_INSTRUCTION:
5937 return nested_cpu_has(vmcs12, CPU_BASED_MWAIT_EXITING);
5938 case EXIT_REASON_MONITOR_INSTRUCTION:
5939 return nested_cpu_has(vmcs12, CPU_BASED_MONITOR_EXITING);
5940 case EXIT_REASON_PAUSE_INSTRUCTION:
5941 return nested_cpu_has(vmcs12, CPU_BASED_PAUSE_EXITING) ||
5942 nested_cpu_has2(vmcs12,
5943 SECONDARY_EXEC_PAUSE_LOOP_EXITING);
5944 case EXIT_REASON_MCE_DURING_VMENTRY:
5946 case EXIT_REASON_TPR_BELOW_THRESHOLD:
5948 case EXIT_REASON_APIC_ACCESS:
5949 return nested_cpu_has2(vmcs12,
5950 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES);
5951 case EXIT_REASON_EPT_VIOLATION:
5952 case EXIT_REASON_EPT_MISCONFIG:
5954 case EXIT_REASON_WBINVD:
5955 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_WBINVD_EXITING);
5956 case EXIT_REASON_XSETBV:
5963 static void vmx_get_exit_info(struct kvm_vcpu *vcpu, u64 *info1, u64 *info2)
5965 *info1 = vmcs_readl(EXIT_QUALIFICATION);
5966 *info2 = vmcs_read32(VM_EXIT_INTR_INFO);
5970 * The guest has exited. See if we can fix it or if we need userspace
5973 static int vmx_handle_exit(struct kvm_vcpu *vcpu)
5975 struct vcpu_vmx *vmx = to_vmx(vcpu);
5976 u32 exit_reason = vmx->exit_reason;
5977 u32 vectoring_info = vmx->idt_vectoring_info;
5979 /* If guest state is invalid, start emulating */
5980 if (vmx->emulation_required && emulate_invalid_guest_state)
5981 return handle_invalid_guest_state(vcpu);
5984 * the KVM_REQ_EVENT optimization bit is only on for one entry, and if
5985 * we did not inject a still-pending event to L1 now because of
5986 * nested_run_pending, we need to re-enable this bit.
5988 if (vmx->nested.nested_run_pending)
5989 kvm_make_request(KVM_REQ_EVENT, vcpu);
5991 if (!is_guest_mode(vcpu) && (exit_reason == EXIT_REASON_VMLAUNCH ||
5992 exit_reason == EXIT_REASON_VMRESUME))
5993 vmx->nested.nested_run_pending = 1;
5995 vmx->nested.nested_run_pending = 0;
5997 if (is_guest_mode(vcpu) && nested_vmx_exit_handled(vcpu)) {
5998 nested_vmx_vmexit(vcpu);
6002 if (exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY) {
6003 vcpu->run->exit_reason = KVM_EXIT_FAIL_ENTRY;
6004 vcpu->run->fail_entry.hardware_entry_failure_reason
6009 if (unlikely(vmx->fail)) {
6010 vcpu->run->exit_reason = KVM_EXIT_FAIL_ENTRY;
6011 vcpu->run->fail_entry.hardware_entry_failure_reason
6012 = vmcs_read32(VM_INSTRUCTION_ERROR);
6018 * Do not try to fix EXIT_REASON_EPT_MISCONFIG if it caused by
6019 * delivery event since it indicates guest is accessing MMIO.
6020 * The vm-exit can be triggered again after return to guest that
6021 * will cause infinite loop.
6023 if ((vectoring_info & VECTORING_INFO_VALID_MASK) &&
6024 (exit_reason != EXIT_REASON_EXCEPTION_NMI &&
6025 exit_reason != EXIT_REASON_EPT_VIOLATION &&
6026 exit_reason != EXIT_REASON_TASK_SWITCH)) {
6027 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
6028 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_DELIVERY_EV;
6029 vcpu->run->internal.ndata = 2;
6030 vcpu->run->internal.data[0] = vectoring_info;
6031 vcpu->run->internal.data[1] = exit_reason;
6035 if (unlikely(!cpu_has_virtual_nmis() && vmx->soft_vnmi_blocked &&
6036 !(is_guest_mode(vcpu) && nested_cpu_has_virtual_nmis(
6037 get_vmcs12(vcpu), vcpu)))) {
6038 if (vmx_interrupt_allowed(vcpu)) {
6039 vmx->soft_vnmi_blocked = 0;
6040 } else if (vmx->vnmi_blocked_time > 1000000000LL &&
6041 vcpu->arch.nmi_pending) {
6043 * This CPU don't support us in finding the end of an
6044 * NMI-blocked window if the guest runs with IRQs
6045 * disabled. So we pull the trigger after 1 s of
6046 * futile waiting, but inform the user about this.
6048 printk(KERN_WARNING "%s: Breaking out of NMI-blocked "
6049 "state on VCPU %d after 1 s timeout\n",
6050 __func__, vcpu->vcpu_id);
6051 vmx->soft_vnmi_blocked = 0;
6055 if (exit_reason < kvm_vmx_max_exit_handlers
6056 && kvm_vmx_exit_handlers[exit_reason])
6057 return kvm_vmx_exit_handlers[exit_reason](vcpu);
6059 vcpu->run->exit_reason = KVM_EXIT_UNKNOWN;
6060 vcpu->run->hw.hardware_exit_reason = exit_reason;
6065 static void update_cr8_intercept(struct kvm_vcpu *vcpu, int tpr, int irr)
6067 if (irr == -1 || tpr < irr) {
6068 vmcs_write32(TPR_THRESHOLD, 0);
6072 vmcs_write32(TPR_THRESHOLD, irr);
6075 static void vmx_complete_atomic_exit(struct vcpu_vmx *vmx)
6079 if (!(vmx->exit_reason == EXIT_REASON_MCE_DURING_VMENTRY
6080 || vmx->exit_reason == EXIT_REASON_EXCEPTION_NMI))
6083 vmx->exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
6084 exit_intr_info = vmx->exit_intr_info;
6086 /* Handle machine checks before interrupts are enabled */
6087 if (is_machine_check(exit_intr_info))
6088 kvm_machine_check();
6090 /* We need to handle NMIs before interrupts are enabled */
6091 if ((exit_intr_info & INTR_INFO_INTR_TYPE_MASK) == INTR_TYPE_NMI_INTR &&
6092 (exit_intr_info & INTR_INFO_VALID_MASK)) {
6093 kvm_before_handle_nmi(&vmx->vcpu);
6095 kvm_after_handle_nmi(&vmx->vcpu);
6099 static void vmx_recover_nmi_blocking(struct vcpu_vmx *vmx)
6104 bool idtv_info_valid;
6106 idtv_info_valid = vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK;
6108 if (cpu_has_virtual_nmis()) {
6109 if (vmx->nmi_known_unmasked)
6112 * Can't use vmx->exit_intr_info since we're not sure what
6113 * the exit reason is.
6115 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
6116 unblock_nmi = (exit_intr_info & INTR_INFO_UNBLOCK_NMI) != 0;
6117 vector = exit_intr_info & INTR_INFO_VECTOR_MASK;
6119 * SDM 3: 27.7.1.2 (September 2008)
6120 * Re-set bit "block by NMI" before VM entry if vmexit caused by
6121 * a guest IRET fault.
6122 * SDM 3: 23.2.2 (September 2008)
6123 * Bit 12 is undefined in any of the following cases:
6124 * If the VM exit sets the valid bit in the IDT-vectoring
6125 * information field.
6126 * If the VM exit is due to a double fault.
6128 if ((exit_intr_info & INTR_INFO_VALID_MASK) && unblock_nmi &&
6129 vector != DF_VECTOR && !idtv_info_valid)
6130 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
6131 GUEST_INTR_STATE_NMI);
6133 vmx->nmi_known_unmasked =
6134 !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO)
6135 & GUEST_INTR_STATE_NMI);
6136 } else if (unlikely(vmx->soft_vnmi_blocked))
6137 vmx->vnmi_blocked_time +=
6138 ktime_to_ns(ktime_sub(ktime_get(), vmx->entry_time));
6141 static void __vmx_complete_interrupts(struct vcpu_vmx *vmx,
6142 u32 idt_vectoring_info,
6143 int instr_len_field,
6144 int error_code_field)
6148 bool idtv_info_valid;
6150 idtv_info_valid = idt_vectoring_info & VECTORING_INFO_VALID_MASK;
6152 vmx->vcpu.arch.nmi_injected = false;
6153 kvm_clear_exception_queue(&vmx->vcpu);
6154 kvm_clear_interrupt_queue(&vmx->vcpu);
6156 if (!idtv_info_valid)
6159 kvm_make_request(KVM_REQ_EVENT, &vmx->vcpu);
6161 vector = idt_vectoring_info & VECTORING_INFO_VECTOR_MASK;
6162 type = idt_vectoring_info & VECTORING_INFO_TYPE_MASK;
6165 case INTR_TYPE_NMI_INTR:
6166 vmx->vcpu.arch.nmi_injected = true;
6168 * SDM 3: 27.7.1.2 (September 2008)
6169 * Clear bit "block by NMI" before VM entry if a NMI
6172 vmx_set_nmi_mask(&vmx->vcpu, false);
6174 case INTR_TYPE_SOFT_EXCEPTION:
6175 vmx->vcpu.arch.event_exit_inst_len =
6176 vmcs_read32(instr_len_field);
6178 case INTR_TYPE_HARD_EXCEPTION:
6179 if (idt_vectoring_info & VECTORING_INFO_DELIVER_CODE_MASK) {
6180 u32 err = vmcs_read32(error_code_field);
6181 kvm_queue_exception_e(&vmx->vcpu, vector, err);
6183 kvm_queue_exception(&vmx->vcpu, vector);
6185 case INTR_TYPE_SOFT_INTR:
6186 vmx->vcpu.arch.event_exit_inst_len =
6187 vmcs_read32(instr_len_field);
6189 case INTR_TYPE_EXT_INTR:
6190 kvm_queue_interrupt(&vmx->vcpu, vector,
6191 type == INTR_TYPE_SOFT_INTR);
6198 static void vmx_complete_interrupts(struct vcpu_vmx *vmx)
6200 if (is_guest_mode(&vmx->vcpu))
6202 __vmx_complete_interrupts(vmx, vmx->idt_vectoring_info,
6203 VM_EXIT_INSTRUCTION_LEN,
6204 IDT_VECTORING_ERROR_CODE);
6207 static void vmx_cancel_injection(struct kvm_vcpu *vcpu)
6209 if (is_guest_mode(vcpu))
6211 __vmx_complete_interrupts(to_vmx(vcpu),
6212 vmcs_read32(VM_ENTRY_INTR_INFO_FIELD),
6213 VM_ENTRY_INSTRUCTION_LEN,
6214 VM_ENTRY_EXCEPTION_ERROR_CODE);
6216 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0);
6219 static void atomic_switch_perf_msrs(struct vcpu_vmx *vmx)
6222 struct perf_guest_switch_msr *msrs;
6224 msrs = perf_guest_get_msrs(&nr_msrs);
6229 for (i = 0; i < nr_msrs; i++)
6230 if (msrs[i].host == msrs[i].guest)
6231 clear_atomic_switch_msr(vmx, msrs[i].msr);
6233 add_atomic_switch_msr(vmx, msrs[i].msr, msrs[i].guest,
6237 static void __noclone vmx_vcpu_run(struct kvm_vcpu *vcpu)
6239 struct vcpu_vmx *vmx = to_vmx(vcpu);
6240 unsigned long debugctlmsr;
6242 if (is_guest_mode(vcpu) && !vmx->nested.nested_run_pending) {
6243 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
6244 if (vmcs12->idt_vectoring_info_field &
6245 VECTORING_INFO_VALID_MASK) {
6246 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
6247 vmcs12->idt_vectoring_info_field);
6248 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
6249 vmcs12->vm_exit_instruction_len);
6250 if (vmcs12->idt_vectoring_info_field &
6251 VECTORING_INFO_DELIVER_CODE_MASK)
6252 vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE,
6253 vmcs12->idt_vectoring_error_code);
6257 /* Record the guest's net vcpu time for enforced NMI injections. */
6258 if (unlikely(!cpu_has_virtual_nmis() && vmx->soft_vnmi_blocked))
6259 vmx->entry_time = ktime_get();
6261 /* Don't enter VMX if guest state is invalid, let the exit handler
6262 start emulation until we arrive back to a valid state */
6263 if (vmx->emulation_required && emulate_invalid_guest_state)
6266 if (test_bit(VCPU_REGS_RSP, (unsigned long *)&vcpu->arch.regs_dirty))
6267 vmcs_writel(GUEST_RSP, vcpu->arch.regs[VCPU_REGS_RSP]);
6268 if (test_bit(VCPU_REGS_RIP, (unsigned long *)&vcpu->arch.regs_dirty))
6269 vmcs_writel(GUEST_RIP, vcpu->arch.regs[VCPU_REGS_RIP]);
6271 /* When single-stepping over STI and MOV SS, we must clear the
6272 * corresponding interruptibility bits in the guest state. Otherwise
6273 * vmentry fails as it then expects bit 14 (BS) in pending debug
6274 * exceptions being set, but that's not correct for the guest debugging
6276 if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP)
6277 vmx_set_interrupt_shadow(vcpu, 0);
6279 atomic_switch_perf_msrs(vmx);
6280 debugctlmsr = get_debugctlmsr();
6282 vmx->__launched = vmx->loaded_vmcs->launched;
6284 /* Store host registers */
6285 "push %%" _ASM_DX "; push %%" _ASM_BP ";"
6286 "push %%" _ASM_CX " \n\t" /* placeholder for guest rcx */
6287 "push %%" _ASM_CX " \n\t"
6288 "cmp %%" _ASM_SP ", %c[host_rsp](%0) \n\t"
6290 "mov %%" _ASM_SP ", %c[host_rsp](%0) \n\t"
6291 __ex(ASM_VMX_VMWRITE_RSP_RDX) "\n\t"
6293 /* Reload cr2 if changed */
6294 "mov %c[cr2](%0), %%" _ASM_AX " \n\t"
6295 "mov %%cr2, %%" _ASM_DX " \n\t"
6296 "cmp %%" _ASM_AX ", %%" _ASM_DX " \n\t"
6298 "mov %%" _ASM_AX", %%cr2 \n\t"
6300 /* Check if vmlaunch of vmresume is needed */
6301 "cmpl $0, %c[launched](%0) \n\t"
6302 /* Load guest registers. Don't clobber flags. */
6303 "mov %c[rax](%0), %%" _ASM_AX " \n\t"
6304 "mov %c[rbx](%0), %%" _ASM_BX " \n\t"
6305 "mov %c[rdx](%0), %%" _ASM_DX " \n\t"
6306 "mov %c[rsi](%0), %%" _ASM_SI " \n\t"
6307 "mov %c[rdi](%0), %%" _ASM_DI " \n\t"
6308 "mov %c[rbp](%0), %%" _ASM_BP " \n\t"
6309 #ifdef CONFIG_X86_64
6310 "mov %c[r8](%0), %%r8 \n\t"
6311 "mov %c[r9](%0), %%r9 \n\t"
6312 "mov %c[r10](%0), %%r10 \n\t"
6313 "mov %c[r11](%0), %%r11 \n\t"
6314 "mov %c[r12](%0), %%r12 \n\t"
6315 "mov %c[r13](%0), %%r13 \n\t"
6316 "mov %c[r14](%0), %%r14 \n\t"
6317 "mov %c[r15](%0), %%r15 \n\t"
6319 "mov %c[rcx](%0), %%" _ASM_CX " \n\t" /* kills %0 (ecx) */
6321 /* Enter guest mode */
6323 __ex(ASM_VMX_VMLAUNCH) "\n\t"
6325 "1: " __ex(ASM_VMX_VMRESUME) "\n\t"
6327 /* Save guest registers, load host registers, keep flags */
6328 "mov %0, %c[wordsize](%%" _ASM_SP ") \n\t"
6330 "mov %%" _ASM_AX ", %c[rax](%0) \n\t"
6331 "mov %%" _ASM_BX ", %c[rbx](%0) \n\t"
6332 __ASM_SIZE(pop) " %c[rcx](%0) \n\t"
6333 "mov %%" _ASM_DX ", %c[rdx](%0) \n\t"
6334 "mov %%" _ASM_SI ", %c[rsi](%0) \n\t"
6335 "mov %%" _ASM_DI ", %c[rdi](%0) \n\t"
6336 "mov %%" _ASM_BP ", %c[rbp](%0) \n\t"
6337 #ifdef CONFIG_X86_64
6338 "mov %%r8, %c[r8](%0) \n\t"
6339 "mov %%r9, %c[r9](%0) \n\t"
6340 "mov %%r10, %c[r10](%0) \n\t"
6341 "mov %%r11, %c[r11](%0) \n\t"
6342 "mov %%r12, %c[r12](%0) \n\t"
6343 "mov %%r13, %c[r13](%0) \n\t"
6344 "mov %%r14, %c[r14](%0) \n\t"
6345 "mov %%r15, %c[r15](%0) \n\t"
6347 "mov %%cr2, %%" _ASM_AX " \n\t"
6348 "mov %%" _ASM_AX ", %c[cr2](%0) \n\t"
6350 "pop %%" _ASM_BP "; pop %%" _ASM_DX " \n\t"
6351 "setbe %c[fail](%0) \n\t"
6352 ".pushsection .rodata \n\t"
6353 ".global vmx_return \n\t"
6354 "vmx_return: " _ASM_PTR " 2b \n\t"
6356 : : "c"(vmx), "d"((unsigned long)HOST_RSP),
6357 [launched]"i"(offsetof(struct vcpu_vmx, __launched)),
6358 [fail]"i"(offsetof(struct vcpu_vmx, fail)),
6359 [host_rsp]"i"(offsetof(struct vcpu_vmx, host_rsp)),
6360 [rax]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RAX])),
6361 [rbx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBX])),
6362 [rcx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RCX])),
6363 [rdx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDX])),
6364 [rsi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RSI])),
6365 [rdi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDI])),
6366 [rbp]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBP])),
6367 #ifdef CONFIG_X86_64
6368 [r8]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R8])),
6369 [r9]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R9])),
6370 [r10]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R10])),
6371 [r11]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R11])),
6372 [r12]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R12])),
6373 [r13]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R13])),
6374 [r14]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R14])),
6375 [r15]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R15])),
6377 [cr2]"i"(offsetof(struct vcpu_vmx, vcpu.arch.cr2)),
6378 [wordsize]"i"(sizeof(ulong))
6380 #ifdef CONFIG_X86_64
6381 , "rax", "rbx", "rdi", "rsi"
6382 , "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15"
6384 , "eax", "ebx", "edi", "esi"
6388 /* MSR_IA32_DEBUGCTLMSR is zeroed on vmexit. Restore it if needed */
6390 update_debugctlmsr(debugctlmsr);
6392 #ifndef CONFIG_X86_64
6394 * The sysexit path does not restore ds/es, so we must set them to
6395 * a reasonable value ourselves.
6397 * We can't defer this to vmx_load_host_state() since that function
6398 * may be executed in interrupt context, which saves and restore segments
6399 * around it, nullifying its effect.
6401 loadsegment(ds, __USER_DS);
6402 loadsegment(es, __USER_DS);
6405 vcpu->arch.regs_avail = ~((1 << VCPU_REGS_RIP) | (1 << VCPU_REGS_RSP)
6406 | (1 << VCPU_EXREG_RFLAGS)
6407 | (1 << VCPU_EXREG_CPL)
6408 | (1 << VCPU_EXREG_PDPTR)
6409 | (1 << VCPU_EXREG_SEGMENTS)
6410 | (1 << VCPU_EXREG_CR3));
6411 vcpu->arch.regs_dirty = 0;
6413 vmx->idt_vectoring_info = vmcs_read32(IDT_VECTORING_INFO_FIELD);
6415 if (is_guest_mode(vcpu)) {
6416 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
6417 vmcs12->idt_vectoring_info_field = vmx->idt_vectoring_info;
6418 if (vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK) {
6419 vmcs12->idt_vectoring_error_code =
6420 vmcs_read32(IDT_VECTORING_ERROR_CODE);
6421 vmcs12->vm_exit_instruction_len =
6422 vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
6426 vmx->loaded_vmcs->launched = 1;
6428 vmx->exit_reason = vmcs_read32(VM_EXIT_REASON);
6429 trace_kvm_exit(vmx->exit_reason, vcpu, KVM_ISA_VMX);
6431 vmx_complete_atomic_exit(vmx);
6432 vmx_recover_nmi_blocking(vmx);
6433 vmx_complete_interrupts(vmx);
6436 static void vmx_free_vcpu(struct kvm_vcpu *vcpu)
6438 struct vcpu_vmx *vmx = to_vmx(vcpu);
6442 free_loaded_vmcs(vmx->loaded_vmcs);
6443 kfree(vmx->guest_msrs);
6444 kvm_vcpu_uninit(vcpu);
6445 kmem_cache_free(kvm_vcpu_cache, vmx);
6448 static struct kvm_vcpu *vmx_create_vcpu(struct kvm *kvm, unsigned int id)
6451 struct vcpu_vmx *vmx = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
6455 return ERR_PTR(-ENOMEM);
6459 err = kvm_vcpu_init(&vmx->vcpu, kvm, id);
6463 vmx->guest_msrs = kmalloc(PAGE_SIZE, GFP_KERNEL);
6465 if (!vmx->guest_msrs) {
6469 vmx->loaded_vmcs = &vmx->vmcs01;
6470 vmx->loaded_vmcs->vmcs = alloc_vmcs();
6471 if (!vmx->loaded_vmcs->vmcs)
6474 kvm_cpu_vmxon(__pa(per_cpu(vmxarea, raw_smp_processor_id())));
6475 loaded_vmcs_init(vmx->loaded_vmcs);
6480 vmx_vcpu_load(&vmx->vcpu, cpu);
6481 vmx->vcpu.cpu = cpu;
6482 err = vmx_vcpu_setup(vmx);
6483 vmx_vcpu_put(&vmx->vcpu);
6487 if (vm_need_virtualize_apic_accesses(kvm))
6488 err = alloc_apic_access_page(kvm);
6493 if (!kvm->arch.ept_identity_map_addr)
6494 kvm->arch.ept_identity_map_addr =
6495 VMX_EPT_IDENTITY_PAGETABLE_ADDR;
6497 if (alloc_identity_pagetable(kvm) != 0)
6499 if (!init_rmode_identity_map(kvm))
6503 vmx->nested.current_vmptr = -1ull;
6504 vmx->nested.current_vmcs12 = NULL;
6509 free_loaded_vmcs(vmx->loaded_vmcs);
6511 kfree(vmx->guest_msrs);
6513 kvm_vcpu_uninit(&vmx->vcpu);
6516 kmem_cache_free(kvm_vcpu_cache, vmx);
6517 return ERR_PTR(err);
6520 static void __init vmx_check_processor_compat(void *rtn)
6522 struct vmcs_config vmcs_conf;
6525 if (setup_vmcs_config(&vmcs_conf) < 0)
6527 if (memcmp(&vmcs_config, &vmcs_conf, sizeof(struct vmcs_config)) != 0) {
6528 printk(KERN_ERR "kvm: CPU %d feature inconsistency!\n",
6529 smp_processor_id());
6534 static int get_ept_level(void)
6536 return VMX_EPT_DEFAULT_GAW + 1;
6539 static u64 vmx_get_mt_mask(struct kvm_vcpu *vcpu, gfn_t gfn, bool is_mmio)
6543 /* For VT-d and EPT combination
6544 * 1. MMIO: always map as UC
6546 * a. VT-d without snooping control feature: can't guarantee the
6547 * result, try to trust guest.
6548 * b. VT-d with snooping control feature: snooping control feature of
6549 * VT-d engine can guarantee the cache correctness. Just set it
6550 * to WB to keep consistent with host. So the same as item 3.
6551 * 3. EPT without VT-d: always map as WB and set IPAT=1 to keep
6552 * consistent with host MTRR
6555 ret = MTRR_TYPE_UNCACHABLE << VMX_EPT_MT_EPTE_SHIFT;
6556 else if (vcpu->kvm->arch.iommu_domain &&
6557 !(vcpu->kvm->arch.iommu_flags & KVM_IOMMU_CACHE_COHERENCY))
6558 ret = kvm_get_guest_memory_type(vcpu, gfn) <<
6559 VMX_EPT_MT_EPTE_SHIFT;
6561 ret = (MTRR_TYPE_WRBACK << VMX_EPT_MT_EPTE_SHIFT)
6567 static int vmx_get_lpage_level(void)
6569 if (enable_ept && !cpu_has_vmx_ept_1g_page())
6570 return PT_DIRECTORY_LEVEL;
6572 /* For shadow and EPT supported 1GB page */
6573 return PT_PDPE_LEVEL;
6576 static void vmx_cpuid_update(struct kvm_vcpu *vcpu)
6578 struct kvm_cpuid_entry2 *best;
6579 struct vcpu_vmx *vmx = to_vmx(vcpu);
6582 vmx->rdtscp_enabled = false;
6583 if (vmx_rdtscp_supported()) {
6584 exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
6585 if (exec_control & SECONDARY_EXEC_RDTSCP) {
6586 best = kvm_find_cpuid_entry(vcpu, 0x80000001, 0);
6587 if (best && (best->edx & bit(X86_FEATURE_RDTSCP)))
6588 vmx->rdtscp_enabled = true;
6590 exec_control &= ~SECONDARY_EXEC_RDTSCP;
6591 vmcs_write32(SECONDARY_VM_EXEC_CONTROL,
6597 /* Exposing INVPCID only when PCID is exposed */
6598 best = kvm_find_cpuid_entry(vcpu, 0x7, 0);
6599 if (vmx_invpcid_supported() &&
6600 best && (best->ebx & bit(X86_FEATURE_INVPCID)) &&
6601 guest_cpuid_has_pcid(vcpu)) {
6602 exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
6603 exec_control |= SECONDARY_EXEC_ENABLE_INVPCID;
6604 vmcs_write32(SECONDARY_VM_EXEC_CONTROL,
6607 if (cpu_has_secondary_exec_ctrls()) {
6608 exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
6609 exec_control &= ~SECONDARY_EXEC_ENABLE_INVPCID;
6610 vmcs_write32(SECONDARY_VM_EXEC_CONTROL,
6614 best->ebx &= ~bit(X86_FEATURE_INVPCID);
6618 static void vmx_set_supported_cpuid(u32 func, struct kvm_cpuid_entry2 *entry)
6620 if (func == 1 && nested)
6621 entry->ecx |= bit(X86_FEATURE_VMX);
6625 * prepare_vmcs02 is called when the L1 guest hypervisor runs its nested
6626 * L2 guest. L1 has a vmcs for L2 (vmcs12), and this function "merges" it
6627 * with L0's requirements for its guest (a.k.a. vmsc01), so we can run the L2
6628 * guest in a way that will both be appropriate to L1's requests, and our
6629 * needs. In addition to modifying the active vmcs (which is vmcs02), this
6630 * function also has additional necessary side-effects, like setting various
6631 * vcpu->arch fields.
6633 static void prepare_vmcs02(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
6635 struct vcpu_vmx *vmx = to_vmx(vcpu);
6638 vmcs_write16(GUEST_ES_SELECTOR, vmcs12->guest_es_selector);
6639 vmcs_write16(GUEST_CS_SELECTOR, vmcs12->guest_cs_selector);
6640 vmcs_write16(GUEST_SS_SELECTOR, vmcs12->guest_ss_selector);
6641 vmcs_write16(GUEST_DS_SELECTOR, vmcs12->guest_ds_selector);
6642 vmcs_write16(GUEST_FS_SELECTOR, vmcs12->guest_fs_selector);
6643 vmcs_write16(GUEST_GS_SELECTOR, vmcs12->guest_gs_selector);
6644 vmcs_write16(GUEST_LDTR_SELECTOR, vmcs12->guest_ldtr_selector);
6645 vmcs_write16(GUEST_TR_SELECTOR, vmcs12->guest_tr_selector);
6646 vmcs_write32(GUEST_ES_LIMIT, vmcs12->guest_es_limit);
6647 vmcs_write32(GUEST_CS_LIMIT, vmcs12->guest_cs_limit);
6648 vmcs_write32(GUEST_SS_LIMIT, vmcs12->guest_ss_limit);
6649 vmcs_write32(GUEST_DS_LIMIT, vmcs12->guest_ds_limit);
6650 vmcs_write32(GUEST_FS_LIMIT, vmcs12->guest_fs_limit);
6651 vmcs_write32(GUEST_GS_LIMIT, vmcs12->guest_gs_limit);
6652 vmcs_write32(GUEST_LDTR_LIMIT, vmcs12->guest_ldtr_limit);
6653 vmcs_write32(GUEST_TR_LIMIT, vmcs12->guest_tr_limit);
6654 vmcs_write32(GUEST_GDTR_LIMIT, vmcs12->guest_gdtr_limit);
6655 vmcs_write32(GUEST_IDTR_LIMIT, vmcs12->guest_idtr_limit);
6656 vmcs_write32(GUEST_ES_AR_BYTES, vmcs12->guest_es_ar_bytes);
6657 vmcs_write32(GUEST_CS_AR_BYTES, vmcs12->guest_cs_ar_bytes);
6658 vmcs_write32(GUEST_SS_AR_BYTES, vmcs12->guest_ss_ar_bytes);
6659 vmcs_write32(GUEST_DS_AR_BYTES, vmcs12->guest_ds_ar_bytes);
6660 vmcs_write32(GUEST_FS_AR_BYTES, vmcs12->guest_fs_ar_bytes);
6661 vmcs_write32(GUEST_GS_AR_BYTES, vmcs12->guest_gs_ar_bytes);
6662 vmcs_write32(GUEST_LDTR_AR_BYTES, vmcs12->guest_ldtr_ar_bytes);
6663 vmcs_write32(GUEST_TR_AR_BYTES, vmcs12->guest_tr_ar_bytes);
6664 vmcs_writel(GUEST_ES_BASE, vmcs12->guest_es_base);
6665 vmcs_writel(GUEST_CS_BASE, vmcs12->guest_cs_base);
6666 vmcs_writel(GUEST_SS_BASE, vmcs12->guest_ss_base);
6667 vmcs_writel(GUEST_DS_BASE, vmcs12->guest_ds_base);
6668 vmcs_writel(GUEST_FS_BASE, vmcs12->guest_fs_base);
6669 vmcs_writel(GUEST_GS_BASE, vmcs12->guest_gs_base);
6670 vmcs_writel(GUEST_LDTR_BASE, vmcs12->guest_ldtr_base);
6671 vmcs_writel(GUEST_TR_BASE, vmcs12->guest_tr_base);
6672 vmcs_writel(GUEST_GDTR_BASE, vmcs12->guest_gdtr_base);
6673 vmcs_writel(GUEST_IDTR_BASE, vmcs12->guest_idtr_base);
6675 vmcs_write64(GUEST_IA32_DEBUGCTL, vmcs12->guest_ia32_debugctl);
6676 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
6677 vmcs12->vm_entry_intr_info_field);
6678 vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE,
6679 vmcs12->vm_entry_exception_error_code);
6680 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
6681 vmcs12->vm_entry_instruction_len);
6682 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO,
6683 vmcs12->guest_interruptibility_info);
6684 vmcs_write32(GUEST_ACTIVITY_STATE, vmcs12->guest_activity_state);
6685 vmcs_write32(GUEST_SYSENTER_CS, vmcs12->guest_sysenter_cs);
6686 vmcs_writel(GUEST_DR7, vmcs12->guest_dr7);
6687 vmcs_writel(GUEST_RFLAGS, vmcs12->guest_rflags);
6688 vmcs_writel(GUEST_PENDING_DBG_EXCEPTIONS,
6689 vmcs12->guest_pending_dbg_exceptions);
6690 vmcs_writel(GUEST_SYSENTER_ESP, vmcs12->guest_sysenter_esp);
6691 vmcs_writel(GUEST_SYSENTER_EIP, vmcs12->guest_sysenter_eip);
6693 vmcs_write64(VMCS_LINK_POINTER, -1ull);
6695 vmcs_write32(PIN_BASED_VM_EXEC_CONTROL,
6696 (vmcs_config.pin_based_exec_ctrl |
6697 vmcs12->pin_based_vm_exec_control));
6700 * Whether page-faults are trapped is determined by a combination of
6701 * 3 settings: PFEC_MASK, PFEC_MATCH and EXCEPTION_BITMAP.PF.
6702 * If enable_ept, L0 doesn't care about page faults and we should
6703 * set all of these to L1's desires. However, if !enable_ept, L0 does
6704 * care about (at least some) page faults, and because it is not easy
6705 * (if at all possible?) to merge L0 and L1's desires, we simply ask
6706 * to exit on each and every L2 page fault. This is done by setting
6707 * MASK=MATCH=0 and (see below) EB.PF=1.
6708 * Note that below we don't need special code to set EB.PF beyond the
6709 * "or"ing of the EB of vmcs01 and vmcs12, because when enable_ept,
6710 * vmcs01's EB.PF is 0 so the "or" will take vmcs12's value, and when
6711 * !enable_ept, EB.PF is 1, so the "or" will always be 1.
6713 * A problem with this approach (when !enable_ept) is that L1 may be
6714 * injected with more page faults than it asked for. This could have
6715 * caused problems, but in practice existing hypervisors don't care.
6716 * To fix this, we will need to emulate the PFEC checking (on the L1
6717 * page tables), using walk_addr(), when injecting PFs to L1.
6719 vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK,
6720 enable_ept ? vmcs12->page_fault_error_code_mask : 0);
6721 vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH,
6722 enable_ept ? vmcs12->page_fault_error_code_match : 0);
6724 if (cpu_has_secondary_exec_ctrls()) {
6725 u32 exec_control = vmx_secondary_exec_control(vmx);
6726 if (!vmx->rdtscp_enabled)
6727 exec_control &= ~SECONDARY_EXEC_RDTSCP;
6728 /* Take the following fields only from vmcs12 */
6729 exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
6730 if (nested_cpu_has(vmcs12,
6731 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS))
6732 exec_control |= vmcs12->secondary_vm_exec_control;
6734 if (exec_control & SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES) {
6736 * Translate L1 physical address to host physical
6737 * address for vmcs02. Keep the page pinned, so this
6738 * physical address remains valid. We keep a reference
6739 * to it so we can release it later.
6741 if (vmx->nested.apic_access_page) /* shouldn't happen */
6742 nested_release_page(vmx->nested.apic_access_page);
6743 vmx->nested.apic_access_page =
6744 nested_get_page(vcpu, vmcs12->apic_access_addr);
6746 * If translation failed, no matter: This feature asks
6747 * to exit when accessing the given address, and if it
6748 * can never be accessed, this feature won't do
6751 if (!vmx->nested.apic_access_page)
6753 ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
6755 vmcs_write64(APIC_ACCESS_ADDR,
6756 page_to_phys(vmx->nested.apic_access_page));
6759 vmcs_write32(SECONDARY_VM_EXEC_CONTROL, exec_control);
6764 * Set host-state according to L0's settings (vmcs12 is irrelevant here)
6765 * Some constant fields are set here by vmx_set_constant_host_state().
6766 * Other fields are different per CPU, and will be set later when
6767 * vmx_vcpu_load() is called, and when vmx_save_host_state() is called.
6769 vmx_set_constant_host_state();
6772 * HOST_RSP is normally set correctly in vmx_vcpu_run() just before
6773 * entry, but only if the current (host) sp changed from the value
6774 * we wrote last (vmx->host_rsp). This cache is no longer relevant
6775 * if we switch vmcs, and rather than hold a separate cache per vmcs,
6776 * here we just force the write to happen on entry.
6780 exec_control = vmx_exec_control(vmx); /* L0's desires */
6781 exec_control &= ~CPU_BASED_VIRTUAL_INTR_PENDING;
6782 exec_control &= ~CPU_BASED_VIRTUAL_NMI_PENDING;
6783 exec_control &= ~CPU_BASED_TPR_SHADOW;
6784 exec_control |= vmcs12->cpu_based_vm_exec_control;
6786 * Merging of IO and MSR bitmaps not currently supported.
6787 * Rather, exit every time.
6789 exec_control &= ~CPU_BASED_USE_MSR_BITMAPS;
6790 exec_control &= ~CPU_BASED_USE_IO_BITMAPS;
6791 exec_control |= CPU_BASED_UNCOND_IO_EXITING;
6793 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, exec_control);
6795 /* EXCEPTION_BITMAP and CR0_GUEST_HOST_MASK should basically be the
6796 * bitwise-or of what L1 wants to trap for L2, and what we want to
6797 * trap. Note that CR0.TS also needs updating - we do this later.
6799 update_exception_bitmap(vcpu);
6800 vcpu->arch.cr0_guest_owned_bits &= ~vmcs12->cr0_guest_host_mask;
6801 vmcs_writel(CR0_GUEST_HOST_MASK, ~vcpu->arch.cr0_guest_owned_bits);
6803 /* Note: IA32_MODE, LOAD_IA32_EFER are modified by vmx_set_efer below */
6804 vmcs_write32(VM_EXIT_CONTROLS,
6805 vmcs12->vm_exit_controls | vmcs_config.vmexit_ctrl);
6806 vmcs_write32(VM_ENTRY_CONTROLS, vmcs12->vm_entry_controls |
6807 (vmcs_config.vmentry_ctrl & ~VM_ENTRY_IA32E_MODE));
6809 if (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_PAT)
6810 vmcs_write64(GUEST_IA32_PAT, vmcs12->guest_ia32_pat);
6811 else if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT)
6812 vmcs_write64(GUEST_IA32_PAT, vmx->vcpu.arch.pat);
6815 set_cr4_guest_host_mask(vmx);
6817 if (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING)
6818 vmcs_write64(TSC_OFFSET,
6819 vmx->nested.vmcs01_tsc_offset + vmcs12->tsc_offset);
6821 vmcs_write64(TSC_OFFSET, vmx->nested.vmcs01_tsc_offset);
6825 * Trivially support vpid by letting L2s share their parent
6826 * L1's vpid. TODO: move to a more elaborate solution, giving
6827 * each L2 its own vpid and exposing the vpid feature to L1.
6829 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid);
6830 vmx_flush_tlb(vcpu);
6833 if (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_EFER)
6834 vcpu->arch.efer = vmcs12->guest_ia32_efer;
6835 if (vmcs12->vm_entry_controls & VM_ENTRY_IA32E_MODE)
6836 vcpu->arch.efer |= (EFER_LMA | EFER_LME);
6838 vcpu->arch.efer &= ~(EFER_LMA | EFER_LME);
6839 /* Note: modifies VM_ENTRY/EXIT_CONTROLS and GUEST/HOST_IA32_EFER */
6840 vmx_set_efer(vcpu, vcpu->arch.efer);
6843 * This sets GUEST_CR0 to vmcs12->guest_cr0, with possibly a modified
6844 * TS bit (for lazy fpu) and bits which we consider mandatory enabled.
6845 * The CR0_READ_SHADOW is what L2 should have expected to read given
6846 * the specifications by L1; It's not enough to take
6847 * vmcs12->cr0_read_shadow because on our cr0_guest_host_mask we we
6848 * have more bits than L1 expected.
6850 vmx_set_cr0(vcpu, vmcs12->guest_cr0);
6851 vmcs_writel(CR0_READ_SHADOW, nested_read_cr0(vmcs12));
6853 vmx_set_cr4(vcpu, vmcs12->guest_cr4);
6854 vmcs_writel(CR4_READ_SHADOW, nested_read_cr4(vmcs12));
6856 /* shadow page tables on either EPT or shadow page tables */
6857 kvm_set_cr3(vcpu, vmcs12->guest_cr3);
6858 kvm_mmu_reset_context(vcpu);
6860 kvm_register_write(vcpu, VCPU_REGS_RSP, vmcs12->guest_rsp);
6861 kvm_register_write(vcpu, VCPU_REGS_RIP, vmcs12->guest_rip);
6865 * nested_vmx_run() handles a nested entry, i.e., a VMLAUNCH or VMRESUME on L1
6866 * for running an L2 nested guest.
6868 static int nested_vmx_run(struct kvm_vcpu *vcpu, bool launch)
6870 struct vmcs12 *vmcs12;
6871 struct vcpu_vmx *vmx = to_vmx(vcpu);
6873 struct loaded_vmcs *vmcs02;
6875 if (!nested_vmx_check_permission(vcpu) ||
6876 !nested_vmx_check_vmcs12(vcpu))
6879 skip_emulated_instruction(vcpu);
6880 vmcs12 = get_vmcs12(vcpu);
6883 * The nested entry process starts with enforcing various prerequisites
6884 * on vmcs12 as required by the Intel SDM, and act appropriately when
6885 * they fail: As the SDM explains, some conditions should cause the
6886 * instruction to fail, while others will cause the instruction to seem
6887 * to succeed, but return an EXIT_REASON_INVALID_STATE.
6888 * To speed up the normal (success) code path, we should avoid checking
6889 * for misconfigurations which will anyway be caught by the processor
6890 * when using the merged vmcs02.
6892 if (vmcs12->launch_state == launch) {
6893 nested_vmx_failValid(vcpu,
6894 launch ? VMXERR_VMLAUNCH_NONCLEAR_VMCS
6895 : VMXERR_VMRESUME_NONLAUNCHED_VMCS);
6899 if ((vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_MSR_BITMAPS) &&
6900 !IS_ALIGNED(vmcs12->msr_bitmap, PAGE_SIZE)) {
6901 /*TODO: Also verify bits beyond physical address width are 0*/
6902 nested_vmx_failValid(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD);
6906 if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES) &&
6907 !IS_ALIGNED(vmcs12->apic_access_addr, PAGE_SIZE)) {
6908 /*TODO: Also verify bits beyond physical address width are 0*/
6909 nested_vmx_failValid(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD);
6913 if (vmcs12->vm_entry_msr_load_count > 0 ||
6914 vmcs12->vm_exit_msr_load_count > 0 ||
6915 vmcs12->vm_exit_msr_store_count > 0) {
6916 pr_warn_ratelimited("%s: VMCS MSR_{LOAD,STORE} unsupported\n",
6918 nested_vmx_failValid(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD);
6922 if (!vmx_control_verify(vmcs12->cpu_based_vm_exec_control,
6923 nested_vmx_procbased_ctls_low, nested_vmx_procbased_ctls_high) ||
6924 !vmx_control_verify(vmcs12->secondary_vm_exec_control,
6925 nested_vmx_secondary_ctls_low, nested_vmx_secondary_ctls_high) ||
6926 !vmx_control_verify(vmcs12->pin_based_vm_exec_control,
6927 nested_vmx_pinbased_ctls_low, nested_vmx_pinbased_ctls_high) ||
6928 !vmx_control_verify(vmcs12->vm_exit_controls,
6929 nested_vmx_exit_ctls_low, nested_vmx_exit_ctls_high) ||
6930 !vmx_control_verify(vmcs12->vm_entry_controls,
6931 nested_vmx_entry_ctls_low, nested_vmx_entry_ctls_high))
6933 nested_vmx_failValid(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD);
6937 if (((vmcs12->host_cr0 & VMXON_CR0_ALWAYSON) != VMXON_CR0_ALWAYSON) ||
6938 ((vmcs12->host_cr4 & VMXON_CR4_ALWAYSON) != VMXON_CR4_ALWAYSON)) {
6939 nested_vmx_failValid(vcpu,
6940 VMXERR_ENTRY_INVALID_HOST_STATE_FIELD);
6944 if (((vmcs12->guest_cr0 & VMXON_CR0_ALWAYSON) != VMXON_CR0_ALWAYSON) ||
6945 ((vmcs12->guest_cr4 & VMXON_CR4_ALWAYSON) != VMXON_CR4_ALWAYSON)) {
6946 nested_vmx_entry_failure(vcpu, vmcs12,
6947 EXIT_REASON_INVALID_STATE, ENTRY_FAIL_DEFAULT);
6950 if (vmcs12->vmcs_link_pointer != -1ull) {
6951 nested_vmx_entry_failure(vcpu, vmcs12,
6952 EXIT_REASON_INVALID_STATE, ENTRY_FAIL_VMCS_LINK_PTR);
6957 * We're finally done with prerequisite checking, and can start with
6961 vmcs02 = nested_get_current_vmcs02(vmx);
6965 enter_guest_mode(vcpu);
6967 vmx->nested.vmcs01_tsc_offset = vmcs_read64(TSC_OFFSET);
6970 vmx->loaded_vmcs = vmcs02;
6972 vmx_vcpu_load(vcpu, cpu);
6976 vmcs12->launch_state = 1;
6978 prepare_vmcs02(vcpu, vmcs12);
6981 * Note no nested_vmx_succeed or nested_vmx_fail here. At this point
6982 * we are no longer running L1, and VMLAUNCH/VMRESUME has not yet
6983 * returned as far as L1 is concerned. It will only return (and set
6984 * the success flag) when L2 exits (see nested_vmx_vmexit()).
6990 * On a nested exit from L2 to L1, vmcs12.guest_cr0 might not be up-to-date
6991 * because L2 may have changed some cr0 bits directly (CRO_GUEST_HOST_MASK).
6992 * This function returns the new value we should put in vmcs12.guest_cr0.
6993 * It's not enough to just return the vmcs02 GUEST_CR0. Rather,
6994 * 1. Bits that neither L0 nor L1 trapped, were set directly by L2 and are now
6995 * available in vmcs02 GUEST_CR0. (Note: It's enough to check that L0
6996 * didn't trap the bit, because if L1 did, so would L0).
6997 * 2. Bits that L1 asked to trap (and therefore L0 also did) could not have
6998 * been modified by L2, and L1 knows it. So just leave the old value of
6999 * the bit from vmcs12.guest_cr0. Note that the bit from vmcs02 GUEST_CR0
7000 * isn't relevant, because if L0 traps this bit it can set it to anything.
7001 * 3. Bits that L1 didn't trap, but L0 did. L1 believes the guest could have
7002 * changed these bits, and therefore they need to be updated, but L0
7003 * didn't necessarily allow them to be changed in GUEST_CR0 - and rather
7004 * put them in vmcs02 CR0_READ_SHADOW. So take these bits from there.
7006 static inline unsigned long
7007 vmcs12_guest_cr0(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
7010 /*1*/ (vmcs_readl(GUEST_CR0) & vcpu->arch.cr0_guest_owned_bits) |
7011 /*2*/ (vmcs12->guest_cr0 & vmcs12->cr0_guest_host_mask) |
7012 /*3*/ (vmcs_readl(CR0_READ_SHADOW) & ~(vmcs12->cr0_guest_host_mask |
7013 vcpu->arch.cr0_guest_owned_bits));
7016 static inline unsigned long
7017 vmcs12_guest_cr4(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
7020 /*1*/ (vmcs_readl(GUEST_CR4) & vcpu->arch.cr4_guest_owned_bits) |
7021 /*2*/ (vmcs12->guest_cr4 & vmcs12->cr4_guest_host_mask) |
7022 /*3*/ (vmcs_readl(CR4_READ_SHADOW) & ~(vmcs12->cr4_guest_host_mask |
7023 vcpu->arch.cr4_guest_owned_bits));
7027 * prepare_vmcs12 is part of what we need to do when the nested L2 guest exits
7028 * and we want to prepare to run its L1 parent. L1 keeps a vmcs for L2 (vmcs12),
7029 * and this function updates it to reflect the changes to the guest state while
7030 * L2 was running (and perhaps made some exits which were handled directly by L0
7031 * without going back to L1), and to reflect the exit reason.
7032 * Note that we do not have to copy here all VMCS fields, just those that
7033 * could have changed by the L2 guest or the exit - i.e., the guest-state and
7034 * exit-information fields only. Other fields are modified by L1 with VMWRITE,
7035 * which already writes to vmcs12 directly.
7037 void prepare_vmcs12(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
7039 /* update guest state fields: */
7040 vmcs12->guest_cr0 = vmcs12_guest_cr0(vcpu, vmcs12);
7041 vmcs12->guest_cr4 = vmcs12_guest_cr4(vcpu, vmcs12);
7043 kvm_get_dr(vcpu, 7, (unsigned long *)&vmcs12->guest_dr7);
7044 vmcs12->guest_rsp = kvm_register_read(vcpu, VCPU_REGS_RSP);
7045 vmcs12->guest_rip = kvm_register_read(vcpu, VCPU_REGS_RIP);
7046 vmcs12->guest_rflags = vmcs_readl(GUEST_RFLAGS);
7048 vmcs12->guest_es_selector = vmcs_read16(GUEST_ES_SELECTOR);
7049 vmcs12->guest_cs_selector = vmcs_read16(GUEST_CS_SELECTOR);
7050 vmcs12->guest_ss_selector = vmcs_read16(GUEST_SS_SELECTOR);
7051 vmcs12->guest_ds_selector = vmcs_read16(GUEST_DS_SELECTOR);
7052 vmcs12->guest_fs_selector = vmcs_read16(GUEST_FS_SELECTOR);
7053 vmcs12->guest_gs_selector = vmcs_read16(GUEST_GS_SELECTOR);
7054 vmcs12->guest_ldtr_selector = vmcs_read16(GUEST_LDTR_SELECTOR);
7055 vmcs12->guest_tr_selector = vmcs_read16(GUEST_TR_SELECTOR);
7056 vmcs12->guest_es_limit = vmcs_read32(GUEST_ES_LIMIT);
7057 vmcs12->guest_cs_limit = vmcs_read32(GUEST_CS_LIMIT);
7058 vmcs12->guest_ss_limit = vmcs_read32(GUEST_SS_LIMIT);
7059 vmcs12->guest_ds_limit = vmcs_read32(GUEST_DS_LIMIT);
7060 vmcs12->guest_fs_limit = vmcs_read32(GUEST_FS_LIMIT);
7061 vmcs12->guest_gs_limit = vmcs_read32(GUEST_GS_LIMIT);
7062 vmcs12->guest_ldtr_limit = vmcs_read32(GUEST_LDTR_LIMIT);
7063 vmcs12->guest_tr_limit = vmcs_read32(GUEST_TR_LIMIT);
7064 vmcs12->guest_gdtr_limit = vmcs_read32(GUEST_GDTR_LIMIT);
7065 vmcs12->guest_idtr_limit = vmcs_read32(GUEST_IDTR_LIMIT);
7066 vmcs12->guest_es_ar_bytes = vmcs_read32(GUEST_ES_AR_BYTES);
7067 vmcs12->guest_cs_ar_bytes = vmcs_read32(GUEST_CS_AR_BYTES);
7068 vmcs12->guest_ss_ar_bytes = vmcs_read32(GUEST_SS_AR_BYTES);
7069 vmcs12->guest_ds_ar_bytes = vmcs_read32(GUEST_DS_AR_BYTES);
7070 vmcs12->guest_fs_ar_bytes = vmcs_read32(GUEST_FS_AR_BYTES);
7071 vmcs12->guest_gs_ar_bytes = vmcs_read32(GUEST_GS_AR_BYTES);
7072 vmcs12->guest_ldtr_ar_bytes = vmcs_read32(GUEST_LDTR_AR_BYTES);
7073 vmcs12->guest_tr_ar_bytes = vmcs_read32(GUEST_TR_AR_BYTES);
7074 vmcs12->guest_es_base = vmcs_readl(GUEST_ES_BASE);
7075 vmcs12->guest_cs_base = vmcs_readl(GUEST_CS_BASE);
7076 vmcs12->guest_ss_base = vmcs_readl(GUEST_SS_BASE);
7077 vmcs12->guest_ds_base = vmcs_readl(GUEST_DS_BASE);
7078 vmcs12->guest_fs_base = vmcs_readl(GUEST_FS_BASE);
7079 vmcs12->guest_gs_base = vmcs_readl(GUEST_GS_BASE);
7080 vmcs12->guest_ldtr_base = vmcs_readl(GUEST_LDTR_BASE);
7081 vmcs12->guest_tr_base = vmcs_readl(GUEST_TR_BASE);
7082 vmcs12->guest_gdtr_base = vmcs_readl(GUEST_GDTR_BASE);
7083 vmcs12->guest_idtr_base = vmcs_readl(GUEST_IDTR_BASE);
7085 vmcs12->guest_activity_state = vmcs_read32(GUEST_ACTIVITY_STATE);
7086 vmcs12->guest_interruptibility_info =
7087 vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
7088 vmcs12->guest_pending_dbg_exceptions =
7089 vmcs_readl(GUEST_PENDING_DBG_EXCEPTIONS);
7091 /* TODO: These cannot have changed unless we have MSR bitmaps and
7092 * the relevant bit asks not to trap the change */
7093 vmcs12->guest_ia32_debugctl = vmcs_read64(GUEST_IA32_DEBUGCTL);
7094 if (vmcs12->vm_entry_controls & VM_EXIT_SAVE_IA32_PAT)
7095 vmcs12->guest_ia32_pat = vmcs_read64(GUEST_IA32_PAT);
7096 vmcs12->guest_sysenter_cs = vmcs_read32(GUEST_SYSENTER_CS);
7097 vmcs12->guest_sysenter_esp = vmcs_readl(GUEST_SYSENTER_ESP);
7098 vmcs12->guest_sysenter_eip = vmcs_readl(GUEST_SYSENTER_EIP);
7100 /* update exit information fields: */
7102 vmcs12->vm_exit_reason = vmcs_read32(VM_EXIT_REASON);
7103 vmcs12->exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7105 vmcs12->vm_exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
7106 vmcs12->vm_exit_intr_error_code = vmcs_read32(VM_EXIT_INTR_ERROR_CODE);
7107 vmcs12->idt_vectoring_info_field =
7108 vmcs_read32(IDT_VECTORING_INFO_FIELD);
7109 vmcs12->idt_vectoring_error_code =
7110 vmcs_read32(IDT_VECTORING_ERROR_CODE);
7111 vmcs12->vm_exit_instruction_len = vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
7112 vmcs12->vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
7114 /* clear vm-entry fields which are to be cleared on exit */
7115 if (!(vmcs12->vm_exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY))
7116 vmcs12->vm_entry_intr_info_field &= ~INTR_INFO_VALID_MASK;
7120 * A part of what we need to when the nested L2 guest exits and we want to
7121 * run its L1 parent, is to reset L1's guest state to the host state specified
7123 * This function is to be called not only on normal nested exit, but also on
7124 * a nested entry failure, as explained in Intel's spec, 3B.23.7 ("VM-Entry
7125 * Failures During or After Loading Guest State").
7126 * This function should be called when the active VMCS is L1's (vmcs01).
7128 void load_vmcs12_host_state(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
7130 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_EFER)
7131 vcpu->arch.efer = vmcs12->host_ia32_efer;
7132 if (vmcs12->vm_exit_controls & VM_EXIT_HOST_ADDR_SPACE_SIZE)
7133 vcpu->arch.efer |= (EFER_LMA | EFER_LME);
7135 vcpu->arch.efer &= ~(EFER_LMA | EFER_LME);
7136 vmx_set_efer(vcpu, vcpu->arch.efer);
7138 kvm_register_write(vcpu, VCPU_REGS_RSP, vmcs12->host_rsp);
7139 kvm_register_write(vcpu, VCPU_REGS_RIP, vmcs12->host_rip);
7141 * Note that calling vmx_set_cr0 is important, even if cr0 hasn't
7142 * actually changed, because it depends on the current state of
7143 * fpu_active (which may have changed).
7144 * Note that vmx_set_cr0 refers to efer set above.
7146 kvm_set_cr0(vcpu, vmcs12->host_cr0);
7148 * If we did fpu_activate()/fpu_deactivate() during L2's run, we need
7149 * to apply the same changes to L1's vmcs. We just set cr0 correctly,
7150 * but we also need to update cr0_guest_host_mask and exception_bitmap.
7152 update_exception_bitmap(vcpu);
7153 vcpu->arch.cr0_guest_owned_bits = (vcpu->fpu_active ? X86_CR0_TS : 0);
7154 vmcs_writel(CR0_GUEST_HOST_MASK, ~vcpu->arch.cr0_guest_owned_bits);
7157 * Note that CR4_GUEST_HOST_MASK is already set in the original vmcs01
7158 * (KVM doesn't change it)- no reason to call set_cr4_guest_host_mask();
7160 vcpu->arch.cr4_guest_owned_bits = ~vmcs_readl(CR4_GUEST_HOST_MASK);
7161 kvm_set_cr4(vcpu, vmcs12->host_cr4);
7163 /* shadow page tables on either EPT or shadow page tables */
7164 kvm_set_cr3(vcpu, vmcs12->host_cr3);
7165 kvm_mmu_reset_context(vcpu);
7169 * Trivially support vpid by letting L2s share their parent
7170 * L1's vpid. TODO: move to a more elaborate solution, giving
7171 * each L2 its own vpid and exposing the vpid feature to L1.
7173 vmx_flush_tlb(vcpu);
7177 vmcs_write32(GUEST_SYSENTER_CS, vmcs12->host_ia32_sysenter_cs);
7178 vmcs_writel(GUEST_SYSENTER_ESP, vmcs12->host_ia32_sysenter_esp);
7179 vmcs_writel(GUEST_SYSENTER_EIP, vmcs12->host_ia32_sysenter_eip);
7180 vmcs_writel(GUEST_IDTR_BASE, vmcs12->host_idtr_base);
7181 vmcs_writel(GUEST_GDTR_BASE, vmcs12->host_gdtr_base);
7182 vmcs_writel(GUEST_TR_BASE, vmcs12->host_tr_base);
7183 vmcs_writel(GUEST_GS_BASE, vmcs12->host_gs_base);
7184 vmcs_writel(GUEST_FS_BASE, vmcs12->host_fs_base);
7185 vmcs_write16(GUEST_ES_SELECTOR, vmcs12->host_es_selector);
7186 vmcs_write16(GUEST_CS_SELECTOR, vmcs12->host_cs_selector);
7187 vmcs_write16(GUEST_SS_SELECTOR, vmcs12->host_ss_selector);
7188 vmcs_write16(GUEST_DS_SELECTOR, vmcs12->host_ds_selector);
7189 vmcs_write16(GUEST_FS_SELECTOR, vmcs12->host_fs_selector);
7190 vmcs_write16(GUEST_GS_SELECTOR, vmcs12->host_gs_selector);
7191 vmcs_write16(GUEST_TR_SELECTOR, vmcs12->host_tr_selector);
7193 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PAT)
7194 vmcs_write64(GUEST_IA32_PAT, vmcs12->host_ia32_pat);
7195 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL)
7196 vmcs_write64(GUEST_IA32_PERF_GLOBAL_CTRL,
7197 vmcs12->host_ia32_perf_global_ctrl);
7201 * Emulate an exit from nested guest (L2) to L1, i.e., prepare to run L1
7202 * and modify vmcs12 to make it see what it would expect to see there if
7203 * L2 was its real guest. Must only be called when in L2 (is_guest_mode())
7205 static void nested_vmx_vmexit(struct kvm_vcpu *vcpu)
7207 struct vcpu_vmx *vmx = to_vmx(vcpu);
7209 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
7211 leave_guest_mode(vcpu);
7212 prepare_vmcs12(vcpu, vmcs12);
7215 vmx->loaded_vmcs = &vmx->vmcs01;
7217 vmx_vcpu_load(vcpu, cpu);
7221 /* if no vmcs02 cache requested, remove the one we used */
7222 if (VMCS02_POOL_SIZE == 0)
7223 nested_free_vmcs02(vmx, vmx->nested.current_vmptr);
7225 load_vmcs12_host_state(vcpu, vmcs12);
7227 /* Update TSC_OFFSET if TSC was changed while L2 ran */
7228 vmcs_write64(TSC_OFFSET, vmx->nested.vmcs01_tsc_offset);
7230 /* This is needed for same reason as it was needed in prepare_vmcs02 */
7233 /* Unpin physical memory we referred to in vmcs02 */
7234 if (vmx->nested.apic_access_page) {
7235 nested_release_page(vmx->nested.apic_access_page);
7236 vmx->nested.apic_access_page = 0;
7240 * Exiting from L2 to L1, we're now back to L1 which thinks it just
7241 * finished a VMLAUNCH or VMRESUME instruction, so we need to set the
7242 * success or failure flag accordingly.
7244 if (unlikely(vmx->fail)) {
7246 nested_vmx_failValid(vcpu, vmcs_read32(VM_INSTRUCTION_ERROR));
7248 nested_vmx_succeed(vcpu);
7252 * L1's failure to enter L2 is a subset of a normal exit, as explained in
7253 * 23.7 "VM-entry failures during or after loading guest state" (this also
7254 * lists the acceptable exit-reason and exit-qualification parameters).
7255 * It should only be called before L2 actually succeeded to run, and when
7256 * vmcs01 is current (it doesn't leave_guest_mode() or switch vmcss).
7258 static void nested_vmx_entry_failure(struct kvm_vcpu *vcpu,
7259 struct vmcs12 *vmcs12,
7260 u32 reason, unsigned long qualification)
7262 load_vmcs12_host_state(vcpu, vmcs12);
7263 vmcs12->vm_exit_reason = reason | VMX_EXIT_REASONS_FAILED_VMENTRY;
7264 vmcs12->exit_qualification = qualification;
7265 nested_vmx_succeed(vcpu);
7268 static int vmx_check_intercept(struct kvm_vcpu *vcpu,
7269 struct x86_instruction_info *info,
7270 enum x86_intercept_stage stage)
7272 return X86EMUL_CONTINUE;
7275 static struct kvm_x86_ops vmx_x86_ops = {
7276 .cpu_has_kvm_support = cpu_has_kvm_support,
7277 .disabled_by_bios = vmx_disabled_by_bios,
7278 .hardware_setup = hardware_setup,
7279 .hardware_unsetup = hardware_unsetup,
7280 .check_processor_compatibility = vmx_check_processor_compat,
7281 .hardware_enable = hardware_enable,
7282 .hardware_disable = hardware_disable,
7283 .cpu_has_accelerated_tpr = report_flexpriority,
7285 .vcpu_create = vmx_create_vcpu,
7286 .vcpu_free = vmx_free_vcpu,
7287 .vcpu_reset = vmx_vcpu_reset,
7289 .prepare_guest_switch = vmx_save_host_state,
7290 .vcpu_load = vmx_vcpu_load,
7291 .vcpu_put = vmx_vcpu_put,
7293 .update_db_bp_intercept = update_exception_bitmap,
7294 .get_msr = vmx_get_msr,
7295 .set_msr = vmx_set_msr,
7296 .get_segment_base = vmx_get_segment_base,
7297 .get_segment = vmx_get_segment,
7298 .set_segment = vmx_set_segment,
7299 .get_cpl = vmx_get_cpl,
7300 .get_cs_db_l_bits = vmx_get_cs_db_l_bits,
7301 .decache_cr0_guest_bits = vmx_decache_cr0_guest_bits,
7302 .decache_cr3 = vmx_decache_cr3,
7303 .decache_cr4_guest_bits = vmx_decache_cr4_guest_bits,
7304 .set_cr0 = vmx_set_cr0,
7305 .set_cr3 = vmx_set_cr3,
7306 .set_cr4 = vmx_set_cr4,
7307 .set_efer = vmx_set_efer,
7308 .get_idt = vmx_get_idt,
7309 .set_idt = vmx_set_idt,
7310 .get_gdt = vmx_get_gdt,
7311 .set_gdt = vmx_set_gdt,
7312 .set_dr7 = vmx_set_dr7,
7313 .cache_reg = vmx_cache_reg,
7314 .get_rflags = vmx_get_rflags,
7315 .set_rflags = vmx_set_rflags,
7316 .fpu_activate = vmx_fpu_activate,
7317 .fpu_deactivate = vmx_fpu_deactivate,
7319 .tlb_flush = vmx_flush_tlb,
7321 .run = vmx_vcpu_run,
7322 .handle_exit = vmx_handle_exit,
7323 .skip_emulated_instruction = skip_emulated_instruction,
7324 .set_interrupt_shadow = vmx_set_interrupt_shadow,
7325 .get_interrupt_shadow = vmx_get_interrupt_shadow,
7326 .patch_hypercall = vmx_patch_hypercall,
7327 .set_irq = vmx_inject_irq,
7328 .set_nmi = vmx_inject_nmi,
7329 .queue_exception = vmx_queue_exception,
7330 .cancel_injection = vmx_cancel_injection,
7331 .interrupt_allowed = vmx_interrupt_allowed,
7332 .nmi_allowed = vmx_nmi_allowed,
7333 .get_nmi_mask = vmx_get_nmi_mask,
7334 .set_nmi_mask = vmx_set_nmi_mask,
7335 .enable_nmi_window = enable_nmi_window,
7336 .enable_irq_window = enable_irq_window,
7337 .update_cr8_intercept = update_cr8_intercept,
7339 .set_tss_addr = vmx_set_tss_addr,
7340 .get_tdp_level = get_ept_level,
7341 .get_mt_mask = vmx_get_mt_mask,
7343 .get_exit_info = vmx_get_exit_info,
7345 .get_lpage_level = vmx_get_lpage_level,
7347 .cpuid_update = vmx_cpuid_update,
7349 .rdtscp_supported = vmx_rdtscp_supported,
7350 .invpcid_supported = vmx_invpcid_supported,
7352 .set_supported_cpuid = vmx_set_supported_cpuid,
7354 .has_wbinvd_exit = cpu_has_vmx_wbinvd_exit,
7356 .set_tsc_khz = vmx_set_tsc_khz,
7357 .read_tsc_offset = vmx_read_tsc_offset,
7358 .write_tsc_offset = vmx_write_tsc_offset,
7359 .adjust_tsc_offset = vmx_adjust_tsc_offset,
7360 .compute_tsc_offset = vmx_compute_tsc_offset,
7361 .read_l1_tsc = vmx_read_l1_tsc,
7363 .set_tdp_cr3 = vmx_set_cr3,
7365 .check_intercept = vmx_check_intercept,
7368 static int __init vmx_init(void)
7372 rdmsrl_safe(MSR_EFER, &host_efer);
7374 for (i = 0; i < NR_VMX_MSR; ++i)
7375 kvm_define_shared_msr(i, vmx_msr_index[i]);
7377 vmx_io_bitmap_a = (unsigned long *)__get_free_page(GFP_KERNEL);
7378 if (!vmx_io_bitmap_a)
7383 vmx_io_bitmap_b = (unsigned long *)__get_free_page(GFP_KERNEL);
7384 if (!vmx_io_bitmap_b)
7387 vmx_msr_bitmap_legacy = (unsigned long *)__get_free_page(GFP_KERNEL);
7388 if (!vmx_msr_bitmap_legacy)
7392 vmx_msr_bitmap_longmode = (unsigned long *)__get_free_page(GFP_KERNEL);
7393 if (!vmx_msr_bitmap_longmode)
7398 * Allow direct access to the PC debug port (it is often used for I/O
7399 * delays, but the vmexits simply slow things down).
7401 memset(vmx_io_bitmap_a, 0xff, PAGE_SIZE);
7402 clear_bit(0x80, vmx_io_bitmap_a);
7404 memset(vmx_io_bitmap_b, 0xff, PAGE_SIZE);
7406 memset(vmx_msr_bitmap_legacy, 0xff, PAGE_SIZE);
7407 memset(vmx_msr_bitmap_longmode, 0xff, PAGE_SIZE);
7409 set_bit(0, vmx_vpid_bitmap); /* 0 is reserved for host */
7411 r = kvm_init(&vmx_x86_ops, sizeof(struct vcpu_vmx),
7412 __alignof__(struct vcpu_vmx), THIS_MODULE);
7417 rcu_assign_pointer(crash_vmclear_loaded_vmcss,
7418 crash_vmclear_local_loaded_vmcss);
7421 vmx_disable_intercept_for_msr(MSR_FS_BASE, false);
7422 vmx_disable_intercept_for_msr(MSR_GS_BASE, false);
7423 vmx_disable_intercept_for_msr(MSR_KERNEL_GS_BASE, true);
7424 vmx_disable_intercept_for_msr(MSR_IA32_SYSENTER_CS, false);
7425 vmx_disable_intercept_for_msr(MSR_IA32_SYSENTER_ESP, false);
7426 vmx_disable_intercept_for_msr(MSR_IA32_SYSENTER_EIP, false);
7429 kvm_mmu_set_mask_ptes(0ull,
7430 (enable_ept_ad_bits) ? VMX_EPT_ACCESS_BIT : 0ull,
7431 (enable_ept_ad_bits) ? VMX_EPT_DIRTY_BIT : 0ull,
7432 0ull, VMX_EPT_EXECUTABLE_MASK);
7433 ept_set_mmio_spte_mask();
7441 free_page((unsigned long)vmx_msr_bitmap_longmode);
7443 free_page((unsigned long)vmx_msr_bitmap_legacy);
7445 free_page((unsigned long)vmx_io_bitmap_b);
7447 free_page((unsigned long)vmx_io_bitmap_a);
7451 static void __exit vmx_exit(void)
7453 free_page((unsigned long)vmx_msr_bitmap_legacy);
7454 free_page((unsigned long)vmx_msr_bitmap_longmode);
7455 free_page((unsigned long)vmx_io_bitmap_b);
7456 free_page((unsigned long)vmx_io_bitmap_a);
7459 rcu_assign_pointer(crash_vmclear_loaded_vmcss, NULL);
7466 module_init(vmx_init)
7467 module_exit(vmx_exit)