]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blob - arch/x86/kvm/vmx.c
KVM: x86: Add kvm_skip_emulated_instruction and use it.
[mirror_ubuntu-bionic-kernel.git] / arch / x86 / kvm / vmx.c
1 /*
2 * Kernel-based Virtual Machine driver for Linux
3 *
4 * This module enables machines with Intel VT-x extensions to run virtual
5 * machines without emulation or binary translation.
6 *
7 * Copyright (C) 2006 Qumranet, Inc.
8 * Copyright 2010 Red Hat, Inc. and/or its affiliates.
9 *
10 * Authors:
11 * Avi Kivity <avi@qumranet.com>
12 * Yaniv Kamay <yaniv@qumranet.com>
13 *
14 * This work is licensed under the terms of the GNU GPL, version 2. See
15 * the COPYING file in the top-level directory.
16 *
17 */
18
19 #include "irq.h"
20 #include "mmu.h"
21 #include "cpuid.h"
22 #include "lapic.h"
23
24 #include <linux/kvm_host.h>
25 #include <linux/module.h>
26 #include <linux/kernel.h>
27 #include <linux/mm.h>
28 #include <linux/highmem.h>
29 #include <linux/sched.h>
30 #include <linux/moduleparam.h>
31 #include <linux/mod_devicetable.h>
32 #include <linux/trace_events.h>
33 #include <linux/slab.h>
34 #include <linux/tboot.h>
35 #include <linux/hrtimer.h>
36 #include "kvm_cache_regs.h"
37 #include "x86.h"
38
39 #include <asm/cpu.h>
40 #include <asm/io.h>
41 #include <asm/desc.h>
42 #include <asm/vmx.h>
43 #include <asm/virtext.h>
44 #include <asm/mce.h>
45 #include <asm/fpu/internal.h>
46 #include <asm/perf_event.h>
47 #include <asm/debugreg.h>
48 #include <asm/kexec.h>
49 #include <asm/apic.h>
50 #include <asm/irq_remapping.h>
51
52 #include "trace.h"
53 #include "pmu.h"
54
55 #define __ex(x) __kvm_handle_fault_on_reboot(x)
56 #define __ex_clear(x, reg) \
57 ____kvm_handle_fault_on_reboot(x, "xor " reg " , " reg)
58
59 MODULE_AUTHOR("Qumranet");
60 MODULE_LICENSE("GPL");
61
62 static const struct x86_cpu_id vmx_cpu_id[] = {
63 X86_FEATURE_MATCH(X86_FEATURE_VMX),
64 {}
65 };
66 MODULE_DEVICE_TABLE(x86cpu, vmx_cpu_id);
67
68 static bool __read_mostly enable_vpid = 1;
69 module_param_named(vpid, enable_vpid, bool, 0444);
70
71 static bool __read_mostly flexpriority_enabled = 1;
72 module_param_named(flexpriority, flexpriority_enabled, bool, S_IRUGO);
73
74 static bool __read_mostly enable_ept = 1;
75 module_param_named(ept, enable_ept, bool, S_IRUGO);
76
77 static bool __read_mostly enable_unrestricted_guest = 1;
78 module_param_named(unrestricted_guest,
79 enable_unrestricted_guest, bool, S_IRUGO);
80
81 static bool __read_mostly enable_ept_ad_bits = 1;
82 module_param_named(eptad, enable_ept_ad_bits, bool, S_IRUGO);
83
84 static bool __read_mostly emulate_invalid_guest_state = true;
85 module_param(emulate_invalid_guest_state, bool, S_IRUGO);
86
87 static bool __read_mostly vmm_exclusive = 1;
88 module_param(vmm_exclusive, bool, S_IRUGO);
89
90 static bool __read_mostly fasteoi = 1;
91 module_param(fasteoi, bool, S_IRUGO);
92
93 static bool __read_mostly enable_apicv = 1;
94 module_param(enable_apicv, bool, S_IRUGO);
95
96 static bool __read_mostly enable_shadow_vmcs = 1;
97 module_param_named(enable_shadow_vmcs, enable_shadow_vmcs, bool, S_IRUGO);
98 /*
99 * If nested=1, nested virtualization is supported, i.e., guests may use
100 * VMX and be a hypervisor for its own guests. If nested=0, guests may not
101 * use VMX instructions.
102 */
103 static bool __read_mostly nested = 0;
104 module_param(nested, bool, S_IRUGO);
105
106 static u64 __read_mostly host_xss;
107
108 static bool __read_mostly enable_pml = 1;
109 module_param_named(pml, enable_pml, bool, S_IRUGO);
110
111 #define KVM_VMX_TSC_MULTIPLIER_MAX 0xffffffffffffffffULL
112
113 /* Guest_tsc -> host_tsc conversion requires 64-bit division. */
114 static int __read_mostly cpu_preemption_timer_multi;
115 static bool __read_mostly enable_preemption_timer = 1;
116 #ifdef CONFIG_X86_64
117 module_param_named(preemption_timer, enable_preemption_timer, bool, S_IRUGO);
118 #endif
119
120 #define KVM_GUEST_CR0_MASK (X86_CR0_NW | X86_CR0_CD)
121 #define KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST (X86_CR0_WP | X86_CR0_NE)
122 #define KVM_VM_CR0_ALWAYS_ON \
123 (KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST | X86_CR0_PG | X86_CR0_PE)
124 #define KVM_CR4_GUEST_OWNED_BITS \
125 (X86_CR4_PVI | X86_CR4_DE | X86_CR4_PCE | X86_CR4_OSFXSR \
126 | X86_CR4_OSXMMEXCPT | X86_CR4_TSD)
127
128 #define KVM_PMODE_VM_CR4_ALWAYS_ON (X86_CR4_PAE | X86_CR4_VMXE)
129 #define KVM_RMODE_VM_CR4_ALWAYS_ON (X86_CR4_VME | X86_CR4_PAE | X86_CR4_VMXE)
130
131 #define RMODE_GUEST_OWNED_EFLAGS_BITS (~(X86_EFLAGS_IOPL | X86_EFLAGS_VM))
132
133 #define VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE 5
134
135 #define VMX_VPID_EXTENT_SUPPORTED_MASK \
136 (VMX_VPID_EXTENT_INDIVIDUAL_ADDR_BIT | \
137 VMX_VPID_EXTENT_SINGLE_CONTEXT_BIT | \
138 VMX_VPID_EXTENT_GLOBAL_CONTEXT_BIT | \
139 VMX_VPID_EXTENT_SINGLE_NON_GLOBAL_BIT)
140
141 /*
142 * These 2 parameters are used to config the controls for Pause-Loop Exiting:
143 * ple_gap: upper bound on the amount of time between two successive
144 * executions of PAUSE in a loop. Also indicate if ple enabled.
145 * According to test, this time is usually smaller than 128 cycles.
146 * ple_window: upper bound on the amount of time a guest is allowed to execute
147 * in a PAUSE loop. Tests indicate that most spinlocks are held for
148 * less than 2^12 cycles
149 * Time is measured based on a counter that runs at the same rate as the TSC,
150 * refer SDM volume 3b section 21.6.13 & 22.1.3.
151 */
152 #define KVM_VMX_DEFAULT_PLE_GAP 128
153 #define KVM_VMX_DEFAULT_PLE_WINDOW 4096
154 #define KVM_VMX_DEFAULT_PLE_WINDOW_GROW 2
155 #define KVM_VMX_DEFAULT_PLE_WINDOW_SHRINK 0
156 #define KVM_VMX_DEFAULT_PLE_WINDOW_MAX \
157 INT_MAX / KVM_VMX_DEFAULT_PLE_WINDOW_GROW
158
159 static int ple_gap = KVM_VMX_DEFAULT_PLE_GAP;
160 module_param(ple_gap, int, S_IRUGO);
161
162 static int ple_window = KVM_VMX_DEFAULT_PLE_WINDOW;
163 module_param(ple_window, int, S_IRUGO);
164
165 /* Default doubles per-vcpu window every exit. */
166 static int ple_window_grow = KVM_VMX_DEFAULT_PLE_WINDOW_GROW;
167 module_param(ple_window_grow, int, S_IRUGO);
168
169 /* Default resets per-vcpu window every exit to ple_window. */
170 static int ple_window_shrink = KVM_VMX_DEFAULT_PLE_WINDOW_SHRINK;
171 module_param(ple_window_shrink, int, S_IRUGO);
172
173 /* Default is to compute the maximum so we can never overflow. */
174 static int ple_window_actual_max = KVM_VMX_DEFAULT_PLE_WINDOW_MAX;
175 static int ple_window_max = KVM_VMX_DEFAULT_PLE_WINDOW_MAX;
176 module_param(ple_window_max, int, S_IRUGO);
177
178 extern const ulong vmx_return;
179
180 #define NR_AUTOLOAD_MSRS 8
181 #define VMCS02_POOL_SIZE 1
182
183 struct vmcs {
184 u32 revision_id;
185 u32 abort;
186 char data[0];
187 };
188
189 /*
190 * Track a VMCS that may be loaded on a certain CPU. If it is (cpu!=-1), also
191 * remember whether it was VMLAUNCHed, and maintain a linked list of all VMCSs
192 * loaded on this CPU (so we can clear them if the CPU goes down).
193 */
194 struct loaded_vmcs {
195 struct vmcs *vmcs;
196 struct vmcs *shadow_vmcs;
197 int cpu;
198 int launched;
199 struct list_head loaded_vmcss_on_cpu_link;
200 };
201
202 struct shared_msr_entry {
203 unsigned index;
204 u64 data;
205 u64 mask;
206 };
207
208 /*
209 * struct vmcs12 describes the state that our guest hypervisor (L1) keeps for a
210 * single nested guest (L2), hence the name vmcs12. Any VMX implementation has
211 * a VMCS structure, and vmcs12 is our emulated VMX's VMCS. This structure is
212 * stored in guest memory specified by VMPTRLD, but is opaque to the guest,
213 * which must access it using VMREAD/VMWRITE/VMCLEAR instructions.
214 * More than one of these structures may exist, if L1 runs multiple L2 guests.
215 * nested_vmx_run() will use the data here to build a vmcs02: a VMCS for the
216 * underlying hardware which will be used to run L2.
217 * This structure is packed to ensure that its layout is identical across
218 * machines (necessary for live migration).
219 * If there are changes in this struct, VMCS12_REVISION must be changed.
220 */
221 typedef u64 natural_width;
222 struct __packed vmcs12 {
223 /* According to the Intel spec, a VMCS region must start with the
224 * following two fields. Then follow implementation-specific data.
225 */
226 u32 revision_id;
227 u32 abort;
228
229 u32 launch_state; /* set to 0 by VMCLEAR, to 1 by VMLAUNCH */
230 u32 padding[7]; /* room for future expansion */
231
232 u64 io_bitmap_a;
233 u64 io_bitmap_b;
234 u64 msr_bitmap;
235 u64 vm_exit_msr_store_addr;
236 u64 vm_exit_msr_load_addr;
237 u64 vm_entry_msr_load_addr;
238 u64 tsc_offset;
239 u64 virtual_apic_page_addr;
240 u64 apic_access_addr;
241 u64 posted_intr_desc_addr;
242 u64 ept_pointer;
243 u64 eoi_exit_bitmap0;
244 u64 eoi_exit_bitmap1;
245 u64 eoi_exit_bitmap2;
246 u64 eoi_exit_bitmap3;
247 u64 xss_exit_bitmap;
248 u64 guest_physical_address;
249 u64 vmcs_link_pointer;
250 u64 guest_ia32_debugctl;
251 u64 guest_ia32_pat;
252 u64 guest_ia32_efer;
253 u64 guest_ia32_perf_global_ctrl;
254 u64 guest_pdptr0;
255 u64 guest_pdptr1;
256 u64 guest_pdptr2;
257 u64 guest_pdptr3;
258 u64 guest_bndcfgs;
259 u64 host_ia32_pat;
260 u64 host_ia32_efer;
261 u64 host_ia32_perf_global_ctrl;
262 u64 padding64[8]; /* room for future expansion */
263 /*
264 * To allow migration of L1 (complete with its L2 guests) between
265 * machines of different natural widths (32 or 64 bit), we cannot have
266 * unsigned long fields with no explict size. We use u64 (aliased
267 * natural_width) instead. Luckily, x86 is little-endian.
268 */
269 natural_width cr0_guest_host_mask;
270 natural_width cr4_guest_host_mask;
271 natural_width cr0_read_shadow;
272 natural_width cr4_read_shadow;
273 natural_width cr3_target_value0;
274 natural_width cr3_target_value1;
275 natural_width cr3_target_value2;
276 natural_width cr3_target_value3;
277 natural_width exit_qualification;
278 natural_width guest_linear_address;
279 natural_width guest_cr0;
280 natural_width guest_cr3;
281 natural_width guest_cr4;
282 natural_width guest_es_base;
283 natural_width guest_cs_base;
284 natural_width guest_ss_base;
285 natural_width guest_ds_base;
286 natural_width guest_fs_base;
287 natural_width guest_gs_base;
288 natural_width guest_ldtr_base;
289 natural_width guest_tr_base;
290 natural_width guest_gdtr_base;
291 natural_width guest_idtr_base;
292 natural_width guest_dr7;
293 natural_width guest_rsp;
294 natural_width guest_rip;
295 natural_width guest_rflags;
296 natural_width guest_pending_dbg_exceptions;
297 natural_width guest_sysenter_esp;
298 natural_width guest_sysenter_eip;
299 natural_width host_cr0;
300 natural_width host_cr3;
301 natural_width host_cr4;
302 natural_width host_fs_base;
303 natural_width host_gs_base;
304 natural_width host_tr_base;
305 natural_width host_gdtr_base;
306 natural_width host_idtr_base;
307 natural_width host_ia32_sysenter_esp;
308 natural_width host_ia32_sysenter_eip;
309 natural_width host_rsp;
310 natural_width host_rip;
311 natural_width paddingl[8]; /* room for future expansion */
312 u32 pin_based_vm_exec_control;
313 u32 cpu_based_vm_exec_control;
314 u32 exception_bitmap;
315 u32 page_fault_error_code_mask;
316 u32 page_fault_error_code_match;
317 u32 cr3_target_count;
318 u32 vm_exit_controls;
319 u32 vm_exit_msr_store_count;
320 u32 vm_exit_msr_load_count;
321 u32 vm_entry_controls;
322 u32 vm_entry_msr_load_count;
323 u32 vm_entry_intr_info_field;
324 u32 vm_entry_exception_error_code;
325 u32 vm_entry_instruction_len;
326 u32 tpr_threshold;
327 u32 secondary_vm_exec_control;
328 u32 vm_instruction_error;
329 u32 vm_exit_reason;
330 u32 vm_exit_intr_info;
331 u32 vm_exit_intr_error_code;
332 u32 idt_vectoring_info_field;
333 u32 idt_vectoring_error_code;
334 u32 vm_exit_instruction_len;
335 u32 vmx_instruction_info;
336 u32 guest_es_limit;
337 u32 guest_cs_limit;
338 u32 guest_ss_limit;
339 u32 guest_ds_limit;
340 u32 guest_fs_limit;
341 u32 guest_gs_limit;
342 u32 guest_ldtr_limit;
343 u32 guest_tr_limit;
344 u32 guest_gdtr_limit;
345 u32 guest_idtr_limit;
346 u32 guest_es_ar_bytes;
347 u32 guest_cs_ar_bytes;
348 u32 guest_ss_ar_bytes;
349 u32 guest_ds_ar_bytes;
350 u32 guest_fs_ar_bytes;
351 u32 guest_gs_ar_bytes;
352 u32 guest_ldtr_ar_bytes;
353 u32 guest_tr_ar_bytes;
354 u32 guest_interruptibility_info;
355 u32 guest_activity_state;
356 u32 guest_sysenter_cs;
357 u32 host_ia32_sysenter_cs;
358 u32 vmx_preemption_timer_value;
359 u32 padding32[7]; /* room for future expansion */
360 u16 virtual_processor_id;
361 u16 posted_intr_nv;
362 u16 guest_es_selector;
363 u16 guest_cs_selector;
364 u16 guest_ss_selector;
365 u16 guest_ds_selector;
366 u16 guest_fs_selector;
367 u16 guest_gs_selector;
368 u16 guest_ldtr_selector;
369 u16 guest_tr_selector;
370 u16 guest_intr_status;
371 u16 host_es_selector;
372 u16 host_cs_selector;
373 u16 host_ss_selector;
374 u16 host_ds_selector;
375 u16 host_fs_selector;
376 u16 host_gs_selector;
377 u16 host_tr_selector;
378 };
379
380 /*
381 * VMCS12_REVISION is an arbitrary id that should be changed if the content or
382 * layout of struct vmcs12 is changed. MSR_IA32_VMX_BASIC returns this id, and
383 * VMPTRLD verifies that the VMCS region that L1 is loading contains this id.
384 */
385 #define VMCS12_REVISION 0x11e57ed0
386
387 /*
388 * VMCS12_SIZE is the number of bytes L1 should allocate for the VMXON region
389 * and any VMCS region. Although only sizeof(struct vmcs12) are used by the
390 * current implementation, 4K are reserved to avoid future complications.
391 */
392 #define VMCS12_SIZE 0x1000
393
394 /* Used to remember the last vmcs02 used for some recently used vmcs12s */
395 struct vmcs02_list {
396 struct list_head list;
397 gpa_t vmptr;
398 struct loaded_vmcs vmcs02;
399 };
400
401 /*
402 * The nested_vmx structure is part of vcpu_vmx, and holds information we need
403 * for correct emulation of VMX (i.e., nested VMX) on this vcpu.
404 */
405 struct nested_vmx {
406 /* Has the level1 guest done vmxon? */
407 bool vmxon;
408 gpa_t vmxon_ptr;
409
410 /* The guest-physical address of the current VMCS L1 keeps for L2 */
411 gpa_t current_vmptr;
412 /* The host-usable pointer to the above */
413 struct page *current_vmcs12_page;
414 struct vmcs12 *current_vmcs12;
415 /*
416 * Cache of the guest's VMCS, existing outside of guest memory.
417 * Loaded from guest memory during VMPTRLD. Flushed to guest
418 * memory during VMXOFF, VMCLEAR, VMPTRLD.
419 */
420 struct vmcs12 *cached_vmcs12;
421 /*
422 * Indicates if the shadow vmcs must be updated with the
423 * data hold by vmcs12
424 */
425 bool sync_shadow_vmcs;
426
427 /* vmcs02_list cache of VMCSs recently used to run L2 guests */
428 struct list_head vmcs02_pool;
429 int vmcs02_num;
430 bool change_vmcs01_virtual_x2apic_mode;
431 /* L2 must run next, and mustn't decide to exit to L1. */
432 bool nested_run_pending;
433 /*
434 * Guest pages referred to in vmcs02 with host-physical pointers, so
435 * we must keep them pinned while L2 runs.
436 */
437 struct page *apic_access_page;
438 struct page *virtual_apic_page;
439 struct page *pi_desc_page;
440 struct pi_desc *pi_desc;
441 bool pi_pending;
442 u16 posted_intr_nv;
443
444 unsigned long *msr_bitmap;
445
446 struct hrtimer preemption_timer;
447 bool preemption_timer_expired;
448
449 /* to migrate it to L2 if VM_ENTRY_LOAD_DEBUG_CONTROLS is off */
450 u64 vmcs01_debugctl;
451
452 u16 vpid02;
453 u16 last_vpid;
454
455 u32 nested_vmx_procbased_ctls_low;
456 u32 nested_vmx_procbased_ctls_high;
457 u32 nested_vmx_true_procbased_ctls_low;
458 u32 nested_vmx_secondary_ctls_low;
459 u32 nested_vmx_secondary_ctls_high;
460 u32 nested_vmx_pinbased_ctls_low;
461 u32 nested_vmx_pinbased_ctls_high;
462 u32 nested_vmx_exit_ctls_low;
463 u32 nested_vmx_exit_ctls_high;
464 u32 nested_vmx_true_exit_ctls_low;
465 u32 nested_vmx_entry_ctls_low;
466 u32 nested_vmx_entry_ctls_high;
467 u32 nested_vmx_true_entry_ctls_low;
468 u32 nested_vmx_misc_low;
469 u32 nested_vmx_misc_high;
470 u32 nested_vmx_ept_caps;
471 u32 nested_vmx_vpid_caps;
472 };
473
474 #define POSTED_INTR_ON 0
475 #define POSTED_INTR_SN 1
476
477 /* Posted-Interrupt Descriptor */
478 struct pi_desc {
479 u32 pir[8]; /* Posted interrupt requested */
480 union {
481 struct {
482 /* bit 256 - Outstanding Notification */
483 u16 on : 1,
484 /* bit 257 - Suppress Notification */
485 sn : 1,
486 /* bit 271:258 - Reserved */
487 rsvd_1 : 14;
488 /* bit 279:272 - Notification Vector */
489 u8 nv;
490 /* bit 287:280 - Reserved */
491 u8 rsvd_2;
492 /* bit 319:288 - Notification Destination */
493 u32 ndst;
494 };
495 u64 control;
496 };
497 u32 rsvd[6];
498 } __aligned(64);
499
500 static bool pi_test_and_set_on(struct pi_desc *pi_desc)
501 {
502 return test_and_set_bit(POSTED_INTR_ON,
503 (unsigned long *)&pi_desc->control);
504 }
505
506 static bool pi_test_and_clear_on(struct pi_desc *pi_desc)
507 {
508 return test_and_clear_bit(POSTED_INTR_ON,
509 (unsigned long *)&pi_desc->control);
510 }
511
512 static int pi_test_and_set_pir(int vector, struct pi_desc *pi_desc)
513 {
514 return test_and_set_bit(vector, (unsigned long *)pi_desc->pir);
515 }
516
517 static inline void pi_clear_sn(struct pi_desc *pi_desc)
518 {
519 return clear_bit(POSTED_INTR_SN,
520 (unsigned long *)&pi_desc->control);
521 }
522
523 static inline void pi_set_sn(struct pi_desc *pi_desc)
524 {
525 return set_bit(POSTED_INTR_SN,
526 (unsigned long *)&pi_desc->control);
527 }
528
529 static inline void pi_clear_on(struct pi_desc *pi_desc)
530 {
531 clear_bit(POSTED_INTR_ON,
532 (unsigned long *)&pi_desc->control);
533 }
534
535 static inline int pi_test_on(struct pi_desc *pi_desc)
536 {
537 return test_bit(POSTED_INTR_ON,
538 (unsigned long *)&pi_desc->control);
539 }
540
541 static inline int pi_test_sn(struct pi_desc *pi_desc)
542 {
543 return test_bit(POSTED_INTR_SN,
544 (unsigned long *)&pi_desc->control);
545 }
546
547 struct vcpu_vmx {
548 struct kvm_vcpu vcpu;
549 unsigned long host_rsp;
550 u8 fail;
551 bool nmi_known_unmasked;
552 u32 exit_intr_info;
553 u32 idt_vectoring_info;
554 ulong rflags;
555 struct shared_msr_entry *guest_msrs;
556 int nmsrs;
557 int save_nmsrs;
558 unsigned long host_idt_base;
559 #ifdef CONFIG_X86_64
560 u64 msr_host_kernel_gs_base;
561 u64 msr_guest_kernel_gs_base;
562 #endif
563 u32 vm_entry_controls_shadow;
564 u32 vm_exit_controls_shadow;
565 /*
566 * loaded_vmcs points to the VMCS currently used in this vcpu. For a
567 * non-nested (L1) guest, it always points to vmcs01. For a nested
568 * guest (L2), it points to a different VMCS.
569 */
570 struct loaded_vmcs vmcs01;
571 struct loaded_vmcs *loaded_vmcs;
572 bool __launched; /* temporary, used in vmx_vcpu_run */
573 struct msr_autoload {
574 unsigned nr;
575 struct vmx_msr_entry guest[NR_AUTOLOAD_MSRS];
576 struct vmx_msr_entry host[NR_AUTOLOAD_MSRS];
577 } msr_autoload;
578 struct {
579 int loaded;
580 u16 fs_sel, gs_sel, ldt_sel;
581 #ifdef CONFIG_X86_64
582 u16 ds_sel, es_sel;
583 #endif
584 int gs_ldt_reload_needed;
585 int fs_reload_needed;
586 u64 msr_host_bndcfgs;
587 unsigned long vmcs_host_cr4; /* May not match real cr4 */
588 } host_state;
589 struct {
590 int vm86_active;
591 ulong save_rflags;
592 struct kvm_segment segs[8];
593 } rmode;
594 struct {
595 u32 bitmask; /* 4 bits per segment (1 bit per field) */
596 struct kvm_save_segment {
597 u16 selector;
598 unsigned long base;
599 u32 limit;
600 u32 ar;
601 } seg[8];
602 } segment_cache;
603 int vpid;
604 bool emulation_required;
605
606 /* Support for vnmi-less CPUs */
607 int soft_vnmi_blocked;
608 ktime_t entry_time;
609 s64 vnmi_blocked_time;
610 u32 exit_reason;
611
612 /* Posted interrupt descriptor */
613 struct pi_desc pi_desc;
614
615 /* Support for a guest hypervisor (nested VMX) */
616 struct nested_vmx nested;
617
618 /* Dynamic PLE window. */
619 int ple_window;
620 bool ple_window_dirty;
621
622 /* Support for PML */
623 #define PML_ENTITY_NUM 512
624 struct page *pml_pg;
625
626 /* apic deadline value in host tsc */
627 u64 hv_deadline_tsc;
628
629 u64 current_tsc_ratio;
630
631 bool guest_pkru_valid;
632 u32 guest_pkru;
633 u32 host_pkru;
634
635 /*
636 * Only bits masked by msr_ia32_feature_control_valid_bits can be set in
637 * msr_ia32_feature_control. FEATURE_CONTROL_LOCKED is always included
638 * in msr_ia32_feature_control_valid_bits.
639 */
640 u64 msr_ia32_feature_control;
641 u64 msr_ia32_feature_control_valid_bits;
642 };
643
644 enum segment_cache_field {
645 SEG_FIELD_SEL = 0,
646 SEG_FIELD_BASE = 1,
647 SEG_FIELD_LIMIT = 2,
648 SEG_FIELD_AR = 3,
649
650 SEG_FIELD_NR = 4
651 };
652
653 static inline struct vcpu_vmx *to_vmx(struct kvm_vcpu *vcpu)
654 {
655 return container_of(vcpu, struct vcpu_vmx, vcpu);
656 }
657
658 static struct pi_desc *vcpu_to_pi_desc(struct kvm_vcpu *vcpu)
659 {
660 return &(to_vmx(vcpu)->pi_desc);
661 }
662
663 #define VMCS12_OFFSET(x) offsetof(struct vmcs12, x)
664 #define FIELD(number, name) [number] = VMCS12_OFFSET(name)
665 #define FIELD64(number, name) [number] = VMCS12_OFFSET(name), \
666 [number##_HIGH] = VMCS12_OFFSET(name)+4
667
668
669 static unsigned long shadow_read_only_fields[] = {
670 /*
671 * We do NOT shadow fields that are modified when L0
672 * traps and emulates any vmx instruction (e.g. VMPTRLD,
673 * VMXON...) executed by L1.
674 * For example, VM_INSTRUCTION_ERROR is read
675 * by L1 if a vmx instruction fails (part of the error path).
676 * Note the code assumes this logic. If for some reason
677 * we start shadowing these fields then we need to
678 * force a shadow sync when L0 emulates vmx instructions
679 * (e.g. force a sync if VM_INSTRUCTION_ERROR is modified
680 * by nested_vmx_failValid)
681 */
682 VM_EXIT_REASON,
683 VM_EXIT_INTR_INFO,
684 VM_EXIT_INSTRUCTION_LEN,
685 IDT_VECTORING_INFO_FIELD,
686 IDT_VECTORING_ERROR_CODE,
687 VM_EXIT_INTR_ERROR_CODE,
688 EXIT_QUALIFICATION,
689 GUEST_LINEAR_ADDRESS,
690 GUEST_PHYSICAL_ADDRESS
691 };
692 static int max_shadow_read_only_fields =
693 ARRAY_SIZE(shadow_read_only_fields);
694
695 static unsigned long shadow_read_write_fields[] = {
696 TPR_THRESHOLD,
697 GUEST_RIP,
698 GUEST_RSP,
699 GUEST_CR0,
700 GUEST_CR3,
701 GUEST_CR4,
702 GUEST_INTERRUPTIBILITY_INFO,
703 GUEST_RFLAGS,
704 GUEST_CS_SELECTOR,
705 GUEST_CS_AR_BYTES,
706 GUEST_CS_LIMIT,
707 GUEST_CS_BASE,
708 GUEST_ES_BASE,
709 GUEST_BNDCFGS,
710 CR0_GUEST_HOST_MASK,
711 CR0_READ_SHADOW,
712 CR4_READ_SHADOW,
713 TSC_OFFSET,
714 EXCEPTION_BITMAP,
715 CPU_BASED_VM_EXEC_CONTROL,
716 VM_ENTRY_EXCEPTION_ERROR_CODE,
717 VM_ENTRY_INTR_INFO_FIELD,
718 VM_ENTRY_INSTRUCTION_LEN,
719 VM_ENTRY_EXCEPTION_ERROR_CODE,
720 HOST_FS_BASE,
721 HOST_GS_BASE,
722 HOST_FS_SELECTOR,
723 HOST_GS_SELECTOR
724 };
725 static int max_shadow_read_write_fields =
726 ARRAY_SIZE(shadow_read_write_fields);
727
728 static const unsigned short vmcs_field_to_offset_table[] = {
729 FIELD(VIRTUAL_PROCESSOR_ID, virtual_processor_id),
730 FIELD(POSTED_INTR_NV, posted_intr_nv),
731 FIELD(GUEST_ES_SELECTOR, guest_es_selector),
732 FIELD(GUEST_CS_SELECTOR, guest_cs_selector),
733 FIELD(GUEST_SS_SELECTOR, guest_ss_selector),
734 FIELD(GUEST_DS_SELECTOR, guest_ds_selector),
735 FIELD(GUEST_FS_SELECTOR, guest_fs_selector),
736 FIELD(GUEST_GS_SELECTOR, guest_gs_selector),
737 FIELD(GUEST_LDTR_SELECTOR, guest_ldtr_selector),
738 FIELD(GUEST_TR_SELECTOR, guest_tr_selector),
739 FIELD(GUEST_INTR_STATUS, guest_intr_status),
740 FIELD(HOST_ES_SELECTOR, host_es_selector),
741 FIELD(HOST_CS_SELECTOR, host_cs_selector),
742 FIELD(HOST_SS_SELECTOR, host_ss_selector),
743 FIELD(HOST_DS_SELECTOR, host_ds_selector),
744 FIELD(HOST_FS_SELECTOR, host_fs_selector),
745 FIELD(HOST_GS_SELECTOR, host_gs_selector),
746 FIELD(HOST_TR_SELECTOR, host_tr_selector),
747 FIELD64(IO_BITMAP_A, io_bitmap_a),
748 FIELD64(IO_BITMAP_B, io_bitmap_b),
749 FIELD64(MSR_BITMAP, msr_bitmap),
750 FIELD64(VM_EXIT_MSR_STORE_ADDR, vm_exit_msr_store_addr),
751 FIELD64(VM_EXIT_MSR_LOAD_ADDR, vm_exit_msr_load_addr),
752 FIELD64(VM_ENTRY_MSR_LOAD_ADDR, vm_entry_msr_load_addr),
753 FIELD64(TSC_OFFSET, tsc_offset),
754 FIELD64(VIRTUAL_APIC_PAGE_ADDR, virtual_apic_page_addr),
755 FIELD64(APIC_ACCESS_ADDR, apic_access_addr),
756 FIELD64(POSTED_INTR_DESC_ADDR, posted_intr_desc_addr),
757 FIELD64(EPT_POINTER, ept_pointer),
758 FIELD64(EOI_EXIT_BITMAP0, eoi_exit_bitmap0),
759 FIELD64(EOI_EXIT_BITMAP1, eoi_exit_bitmap1),
760 FIELD64(EOI_EXIT_BITMAP2, eoi_exit_bitmap2),
761 FIELD64(EOI_EXIT_BITMAP3, eoi_exit_bitmap3),
762 FIELD64(XSS_EXIT_BITMAP, xss_exit_bitmap),
763 FIELD64(GUEST_PHYSICAL_ADDRESS, guest_physical_address),
764 FIELD64(VMCS_LINK_POINTER, vmcs_link_pointer),
765 FIELD64(GUEST_IA32_DEBUGCTL, guest_ia32_debugctl),
766 FIELD64(GUEST_IA32_PAT, guest_ia32_pat),
767 FIELD64(GUEST_IA32_EFER, guest_ia32_efer),
768 FIELD64(GUEST_IA32_PERF_GLOBAL_CTRL, guest_ia32_perf_global_ctrl),
769 FIELD64(GUEST_PDPTR0, guest_pdptr0),
770 FIELD64(GUEST_PDPTR1, guest_pdptr1),
771 FIELD64(GUEST_PDPTR2, guest_pdptr2),
772 FIELD64(GUEST_PDPTR3, guest_pdptr3),
773 FIELD64(GUEST_BNDCFGS, guest_bndcfgs),
774 FIELD64(HOST_IA32_PAT, host_ia32_pat),
775 FIELD64(HOST_IA32_EFER, host_ia32_efer),
776 FIELD64(HOST_IA32_PERF_GLOBAL_CTRL, host_ia32_perf_global_ctrl),
777 FIELD(PIN_BASED_VM_EXEC_CONTROL, pin_based_vm_exec_control),
778 FIELD(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control),
779 FIELD(EXCEPTION_BITMAP, exception_bitmap),
780 FIELD(PAGE_FAULT_ERROR_CODE_MASK, page_fault_error_code_mask),
781 FIELD(PAGE_FAULT_ERROR_CODE_MATCH, page_fault_error_code_match),
782 FIELD(CR3_TARGET_COUNT, cr3_target_count),
783 FIELD(VM_EXIT_CONTROLS, vm_exit_controls),
784 FIELD(VM_EXIT_MSR_STORE_COUNT, vm_exit_msr_store_count),
785 FIELD(VM_EXIT_MSR_LOAD_COUNT, vm_exit_msr_load_count),
786 FIELD(VM_ENTRY_CONTROLS, vm_entry_controls),
787 FIELD(VM_ENTRY_MSR_LOAD_COUNT, vm_entry_msr_load_count),
788 FIELD(VM_ENTRY_INTR_INFO_FIELD, vm_entry_intr_info_field),
789 FIELD(VM_ENTRY_EXCEPTION_ERROR_CODE, vm_entry_exception_error_code),
790 FIELD(VM_ENTRY_INSTRUCTION_LEN, vm_entry_instruction_len),
791 FIELD(TPR_THRESHOLD, tpr_threshold),
792 FIELD(SECONDARY_VM_EXEC_CONTROL, secondary_vm_exec_control),
793 FIELD(VM_INSTRUCTION_ERROR, vm_instruction_error),
794 FIELD(VM_EXIT_REASON, vm_exit_reason),
795 FIELD(VM_EXIT_INTR_INFO, vm_exit_intr_info),
796 FIELD(VM_EXIT_INTR_ERROR_CODE, vm_exit_intr_error_code),
797 FIELD(IDT_VECTORING_INFO_FIELD, idt_vectoring_info_field),
798 FIELD(IDT_VECTORING_ERROR_CODE, idt_vectoring_error_code),
799 FIELD(VM_EXIT_INSTRUCTION_LEN, vm_exit_instruction_len),
800 FIELD(VMX_INSTRUCTION_INFO, vmx_instruction_info),
801 FIELD(GUEST_ES_LIMIT, guest_es_limit),
802 FIELD(GUEST_CS_LIMIT, guest_cs_limit),
803 FIELD(GUEST_SS_LIMIT, guest_ss_limit),
804 FIELD(GUEST_DS_LIMIT, guest_ds_limit),
805 FIELD(GUEST_FS_LIMIT, guest_fs_limit),
806 FIELD(GUEST_GS_LIMIT, guest_gs_limit),
807 FIELD(GUEST_LDTR_LIMIT, guest_ldtr_limit),
808 FIELD(GUEST_TR_LIMIT, guest_tr_limit),
809 FIELD(GUEST_GDTR_LIMIT, guest_gdtr_limit),
810 FIELD(GUEST_IDTR_LIMIT, guest_idtr_limit),
811 FIELD(GUEST_ES_AR_BYTES, guest_es_ar_bytes),
812 FIELD(GUEST_CS_AR_BYTES, guest_cs_ar_bytes),
813 FIELD(GUEST_SS_AR_BYTES, guest_ss_ar_bytes),
814 FIELD(GUEST_DS_AR_BYTES, guest_ds_ar_bytes),
815 FIELD(GUEST_FS_AR_BYTES, guest_fs_ar_bytes),
816 FIELD(GUEST_GS_AR_BYTES, guest_gs_ar_bytes),
817 FIELD(GUEST_LDTR_AR_BYTES, guest_ldtr_ar_bytes),
818 FIELD(GUEST_TR_AR_BYTES, guest_tr_ar_bytes),
819 FIELD(GUEST_INTERRUPTIBILITY_INFO, guest_interruptibility_info),
820 FIELD(GUEST_ACTIVITY_STATE, guest_activity_state),
821 FIELD(GUEST_SYSENTER_CS, guest_sysenter_cs),
822 FIELD(HOST_IA32_SYSENTER_CS, host_ia32_sysenter_cs),
823 FIELD(VMX_PREEMPTION_TIMER_VALUE, vmx_preemption_timer_value),
824 FIELD(CR0_GUEST_HOST_MASK, cr0_guest_host_mask),
825 FIELD(CR4_GUEST_HOST_MASK, cr4_guest_host_mask),
826 FIELD(CR0_READ_SHADOW, cr0_read_shadow),
827 FIELD(CR4_READ_SHADOW, cr4_read_shadow),
828 FIELD(CR3_TARGET_VALUE0, cr3_target_value0),
829 FIELD(CR3_TARGET_VALUE1, cr3_target_value1),
830 FIELD(CR3_TARGET_VALUE2, cr3_target_value2),
831 FIELD(CR3_TARGET_VALUE3, cr3_target_value3),
832 FIELD(EXIT_QUALIFICATION, exit_qualification),
833 FIELD(GUEST_LINEAR_ADDRESS, guest_linear_address),
834 FIELD(GUEST_CR0, guest_cr0),
835 FIELD(GUEST_CR3, guest_cr3),
836 FIELD(GUEST_CR4, guest_cr4),
837 FIELD(GUEST_ES_BASE, guest_es_base),
838 FIELD(GUEST_CS_BASE, guest_cs_base),
839 FIELD(GUEST_SS_BASE, guest_ss_base),
840 FIELD(GUEST_DS_BASE, guest_ds_base),
841 FIELD(GUEST_FS_BASE, guest_fs_base),
842 FIELD(GUEST_GS_BASE, guest_gs_base),
843 FIELD(GUEST_LDTR_BASE, guest_ldtr_base),
844 FIELD(GUEST_TR_BASE, guest_tr_base),
845 FIELD(GUEST_GDTR_BASE, guest_gdtr_base),
846 FIELD(GUEST_IDTR_BASE, guest_idtr_base),
847 FIELD(GUEST_DR7, guest_dr7),
848 FIELD(GUEST_RSP, guest_rsp),
849 FIELD(GUEST_RIP, guest_rip),
850 FIELD(GUEST_RFLAGS, guest_rflags),
851 FIELD(GUEST_PENDING_DBG_EXCEPTIONS, guest_pending_dbg_exceptions),
852 FIELD(GUEST_SYSENTER_ESP, guest_sysenter_esp),
853 FIELD(GUEST_SYSENTER_EIP, guest_sysenter_eip),
854 FIELD(HOST_CR0, host_cr0),
855 FIELD(HOST_CR3, host_cr3),
856 FIELD(HOST_CR4, host_cr4),
857 FIELD(HOST_FS_BASE, host_fs_base),
858 FIELD(HOST_GS_BASE, host_gs_base),
859 FIELD(HOST_TR_BASE, host_tr_base),
860 FIELD(HOST_GDTR_BASE, host_gdtr_base),
861 FIELD(HOST_IDTR_BASE, host_idtr_base),
862 FIELD(HOST_IA32_SYSENTER_ESP, host_ia32_sysenter_esp),
863 FIELD(HOST_IA32_SYSENTER_EIP, host_ia32_sysenter_eip),
864 FIELD(HOST_RSP, host_rsp),
865 FIELD(HOST_RIP, host_rip),
866 };
867
868 static inline short vmcs_field_to_offset(unsigned long field)
869 {
870 BUILD_BUG_ON(ARRAY_SIZE(vmcs_field_to_offset_table) > SHRT_MAX);
871
872 if (field >= ARRAY_SIZE(vmcs_field_to_offset_table) ||
873 vmcs_field_to_offset_table[field] == 0)
874 return -ENOENT;
875
876 return vmcs_field_to_offset_table[field];
877 }
878
879 static inline struct vmcs12 *get_vmcs12(struct kvm_vcpu *vcpu)
880 {
881 return to_vmx(vcpu)->nested.cached_vmcs12;
882 }
883
884 static struct page *nested_get_page(struct kvm_vcpu *vcpu, gpa_t addr)
885 {
886 struct page *page = kvm_vcpu_gfn_to_page(vcpu, addr >> PAGE_SHIFT);
887 if (is_error_page(page))
888 return NULL;
889
890 return page;
891 }
892
893 static void nested_release_page(struct page *page)
894 {
895 kvm_release_page_dirty(page);
896 }
897
898 static void nested_release_page_clean(struct page *page)
899 {
900 kvm_release_page_clean(page);
901 }
902
903 static unsigned long nested_ept_get_cr3(struct kvm_vcpu *vcpu);
904 static u64 construct_eptp(unsigned long root_hpa);
905 static void kvm_cpu_vmxon(u64 addr);
906 static void kvm_cpu_vmxoff(void);
907 static bool vmx_xsaves_supported(void);
908 static int vmx_set_tss_addr(struct kvm *kvm, unsigned int addr);
909 static void vmx_set_segment(struct kvm_vcpu *vcpu,
910 struct kvm_segment *var, int seg);
911 static void vmx_get_segment(struct kvm_vcpu *vcpu,
912 struct kvm_segment *var, int seg);
913 static bool guest_state_valid(struct kvm_vcpu *vcpu);
914 static u32 vmx_segment_access_rights(struct kvm_segment *var);
915 static void copy_vmcs12_to_shadow(struct vcpu_vmx *vmx);
916 static void copy_shadow_to_vmcs12(struct vcpu_vmx *vmx);
917 static int alloc_identity_pagetable(struct kvm *kvm);
918
919 static DEFINE_PER_CPU(struct vmcs *, vmxarea);
920 static DEFINE_PER_CPU(struct vmcs *, current_vmcs);
921 /*
922 * We maintain a per-CPU linked-list of VMCS loaded on that CPU. This is needed
923 * when a CPU is brought down, and we need to VMCLEAR all VMCSs loaded on it.
924 */
925 static DEFINE_PER_CPU(struct list_head, loaded_vmcss_on_cpu);
926 static DEFINE_PER_CPU(struct desc_ptr, host_gdt);
927
928 /*
929 * We maintian a per-CPU linked-list of vCPU, so in wakeup_handler() we
930 * can find which vCPU should be waken up.
931 */
932 static DEFINE_PER_CPU(struct list_head, blocked_vcpu_on_cpu);
933 static DEFINE_PER_CPU(spinlock_t, blocked_vcpu_on_cpu_lock);
934
935 enum {
936 VMX_IO_BITMAP_A,
937 VMX_IO_BITMAP_B,
938 VMX_MSR_BITMAP_LEGACY,
939 VMX_MSR_BITMAP_LONGMODE,
940 VMX_MSR_BITMAP_LEGACY_X2APIC_APICV,
941 VMX_MSR_BITMAP_LONGMODE_X2APIC_APICV,
942 VMX_MSR_BITMAP_LEGACY_X2APIC,
943 VMX_MSR_BITMAP_LONGMODE_X2APIC,
944 VMX_VMREAD_BITMAP,
945 VMX_VMWRITE_BITMAP,
946 VMX_BITMAP_NR
947 };
948
949 static unsigned long *vmx_bitmap[VMX_BITMAP_NR];
950
951 #define vmx_io_bitmap_a (vmx_bitmap[VMX_IO_BITMAP_A])
952 #define vmx_io_bitmap_b (vmx_bitmap[VMX_IO_BITMAP_B])
953 #define vmx_msr_bitmap_legacy (vmx_bitmap[VMX_MSR_BITMAP_LEGACY])
954 #define vmx_msr_bitmap_longmode (vmx_bitmap[VMX_MSR_BITMAP_LONGMODE])
955 #define vmx_msr_bitmap_legacy_x2apic_apicv (vmx_bitmap[VMX_MSR_BITMAP_LEGACY_X2APIC_APICV])
956 #define vmx_msr_bitmap_longmode_x2apic_apicv (vmx_bitmap[VMX_MSR_BITMAP_LONGMODE_X2APIC_APICV])
957 #define vmx_msr_bitmap_legacy_x2apic (vmx_bitmap[VMX_MSR_BITMAP_LEGACY_X2APIC])
958 #define vmx_msr_bitmap_longmode_x2apic (vmx_bitmap[VMX_MSR_BITMAP_LONGMODE_X2APIC])
959 #define vmx_vmread_bitmap (vmx_bitmap[VMX_VMREAD_BITMAP])
960 #define vmx_vmwrite_bitmap (vmx_bitmap[VMX_VMWRITE_BITMAP])
961
962 static bool cpu_has_load_ia32_efer;
963 static bool cpu_has_load_perf_global_ctrl;
964
965 static DECLARE_BITMAP(vmx_vpid_bitmap, VMX_NR_VPIDS);
966 static DEFINE_SPINLOCK(vmx_vpid_lock);
967
968 static struct vmcs_config {
969 int size;
970 int order;
971 u32 basic_cap;
972 u32 revision_id;
973 u32 pin_based_exec_ctrl;
974 u32 cpu_based_exec_ctrl;
975 u32 cpu_based_2nd_exec_ctrl;
976 u32 vmexit_ctrl;
977 u32 vmentry_ctrl;
978 } vmcs_config;
979
980 static struct vmx_capability {
981 u32 ept;
982 u32 vpid;
983 } vmx_capability;
984
985 #define VMX_SEGMENT_FIELD(seg) \
986 [VCPU_SREG_##seg] = { \
987 .selector = GUEST_##seg##_SELECTOR, \
988 .base = GUEST_##seg##_BASE, \
989 .limit = GUEST_##seg##_LIMIT, \
990 .ar_bytes = GUEST_##seg##_AR_BYTES, \
991 }
992
993 static const struct kvm_vmx_segment_field {
994 unsigned selector;
995 unsigned base;
996 unsigned limit;
997 unsigned ar_bytes;
998 } kvm_vmx_segment_fields[] = {
999 VMX_SEGMENT_FIELD(CS),
1000 VMX_SEGMENT_FIELD(DS),
1001 VMX_SEGMENT_FIELD(ES),
1002 VMX_SEGMENT_FIELD(FS),
1003 VMX_SEGMENT_FIELD(GS),
1004 VMX_SEGMENT_FIELD(SS),
1005 VMX_SEGMENT_FIELD(TR),
1006 VMX_SEGMENT_FIELD(LDTR),
1007 };
1008
1009 static u64 host_efer;
1010
1011 static void ept_save_pdptrs(struct kvm_vcpu *vcpu);
1012
1013 /*
1014 * Keep MSR_STAR at the end, as setup_msrs() will try to optimize it
1015 * away by decrementing the array size.
1016 */
1017 static const u32 vmx_msr_index[] = {
1018 #ifdef CONFIG_X86_64
1019 MSR_SYSCALL_MASK, MSR_LSTAR, MSR_CSTAR,
1020 #endif
1021 MSR_EFER, MSR_TSC_AUX, MSR_STAR,
1022 };
1023
1024 static inline bool is_exception_n(u32 intr_info, u8 vector)
1025 {
1026 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
1027 INTR_INFO_VALID_MASK)) ==
1028 (INTR_TYPE_HARD_EXCEPTION | vector | INTR_INFO_VALID_MASK);
1029 }
1030
1031 static inline bool is_debug(u32 intr_info)
1032 {
1033 return is_exception_n(intr_info, DB_VECTOR);
1034 }
1035
1036 static inline bool is_breakpoint(u32 intr_info)
1037 {
1038 return is_exception_n(intr_info, BP_VECTOR);
1039 }
1040
1041 static inline bool is_page_fault(u32 intr_info)
1042 {
1043 return is_exception_n(intr_info, PF_VECTOR);
1044 }
1045
1046 static inline bool is_no_device(u32 intr_info)
1047 {
1048 return is_exception_n(intr_info, NM_VECTOR);
1049 }
1050
1051 static inline bool is_invalid_opcode(u32 intr_info)
1052 {
1053 return is_exception_n(intr_info, UD_VECTOR);
1054 }
1055
1056 static inline bool is_external_interrupt(u32 intr_info)
1057 {
1058 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK))
1059 == (INTR_TYPE_EXT_INTR | INTR_INFO_VALID_MASK);
1060 }
1061
1062 static inline bool is_machine_check(u32 intr_info)
1063 {
1064 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
1065 INTR_INFO_VALID_MASK)) ==
1066 (INTR_TYPE_HARD_EXCEPTION | MC_VECTOR | INTR_INFO_VALID_MASK);
1067 }
1068
1069 static inline bool cpu_has_vmx_msr_bitmap(void)
1070 {
1071 return vmcs_config.cpu_based_exec_ctrl & CPU_BASED_USE_MSR_BITMAPS;
1072 }
1073
1074 static inline bool cpu_has_vmx_tpr_shadow(void)
1075 {
1076 return vmcs_config.cpu_based_exec_ctrl & CPU_BASED_TPR_SHADOW;
1077 }
1078
1079 static inline bool cpu_need_tpr_shadow(struct kvm_vcpu *vcpu)
1080 {
1081 return cpu_has_vmx_tpr_shadow() && lapic_in_kernel(vcpu);
1082 }
1083
1084 static inline bool cpu_has_secondary_exec_ctrls(void)
1085 {
1086 return vmcs_config.cpu_based_exec_ctrl &
1087 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
1088 }
1089
1090 static inline bool cpu_has_vmx_virtualize_apic_accesses(void)
1091 {
1092 return vmcs_config.cpu_based_2nd_exec_ctrl &
1093 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
1094 }
1095
1096 static inline bool cpu_has_vmx_virtualize_x2apic_mode(void)
1097 {
1098 return vmcs_config.cpu_based_2nd_exec_ctrl &
1099 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
1100 }
1101
1102 static inline bool cpu_has_vmx_apic_register_virt(void)
1103 {
1104 return vmcs_config.cpu_based_2nd_exec_ctrl &
1105 SECONDARY_EXEC_APIC_REGISTER_VIRT;
1106 }
1107
1108 static inline bool cpu_has_vmx_virtual_intr_delivery(void)
1109 {
1110 return vmcs_config.cpu_based_2nd_exec_ctrl &
1111 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY;
1112 }
1113
1114 /*
1115 * Comment's format: document - errata name - stepping - processor name.
1116 * Refer from
1117 * https://www.virtualbox.org/svn/vbox/trunk/src/VBox/VMM/VMMR0/HMR0.cpp
1118 */
1119 static u32 vmx_preemption_cpu_tfms[] = {
1120 /* 323344.pdf - BA86 - D0 - Xeon 7500 Series */
1121 0x000206E6,
1122 /* 323056.pdf - AAX65 - C2 - Xeon L3406 */
1123 /* 322814.pdf - AAT59 - C2 - i7-600, i5-500, i5-400 and i3-300 Mobile */
1124 /* 322911.pdf - AAU65 - C2 - i5-600, i3-500 Desktop and Pentium G6950 */
1125 0x00020652,
1126 /* 322911.pdf - AAU65 - K0 - i5-600, i3-500 Desktop and Pentium G6950 */
1127 0x00020655,
1128 /* 322373.pdf - AAO95 - B1 - Xeon 3400 Series */
1129 /* 322166.pdf - AAN92 - B1 - i7-800 and i5-700 Desktop */
1130 /*
1131 * 320767.pdf - AAP86 - B1 -
1132 * i7-900 Mobile Extreme, i7-800 and i7-700 Mobile
1133 */
1134 0x000106E5,
1135 /* 321333.pdf - AAM126 - C0 - Xeon 3500 */
1136 0x000106A0,
1137 /* 321333.pdf - AAM126 - C1 - Xeon 3500 */
1138 0x000106A1,
1139 /* 320836.pdf - AAJ124 - C0 - i7-900 Desktop Extreme and i7-900 Desktop */
1140 0x000106A4,
1141 /* 321333.pdf - AAM126 - D0 - Xeon 3500 */
1142 /* 321324.pdf - AAK139 - D0 - Xeon 5500 */
1143 /* 320836.pdf - AAJ124 - D0 - i7-900 Extreme and i7-900 Desktop */
1144 0x000106A5,
1145 };
1146
1147 static inline bool cpu_has_broken_vmx_preemption_timer(void)
1148 {
1149 u32 eax = cpuid_eax(0x00000001), i;
1150
1151 /* Clear the reserved bits */
1152 eax &= ~(0x3U << 14 | 0xfU << 28);
1153 for (i = 0; i < ARRAY_SIZE(vmx_preemption_cpu_tfms); i++)
1154 if (eax == vmx_preemption_cpu_tfms[i])
1155 return true;
1156
1157 return false;
1158 }
1159
1160 static inline bool cpu_has_vmx_preemption_timer(void)
1161 {
1162 return vmcs_config.pin_based_exec_ctrl &
1163 PIN_BASED_VMX_PREEMPTION_TIMER;
1164 }
1165
1166 static inline bool cpu_has_vmx_posted_intr(void)
1167 {
1168 return IS_ENABLED(CONFIG_X86_LOCAL_APIC) &&
1169 vmcs_config.pin_based_exec_ctrl & PIN_BASED_POSTED_INTR;
1170 }
1171
1172 static inline bool cpu_has_vmx_apicv(void)
1173 {
1174 return cpu_has_vmx_apic_register_virt() &&
1175 cpu_has_vmx_virtual_intr_delivery() &&
1176 cpu_has_vmx_posted_intr();
1177 }
1178
1179 static inline bool cpu_has_vmx_flexpriority(void)
1180 {
1181 return cpu_has_vmx_tpr_shadow() &&
1182 cpu_has_vmx_virtualize_apic_accesses();
1183 }
1184
1185 static inline bool cpu_has_vmx_ept_execute_only(void)
1186 {
1187 return vmx_capability.ept & VMX_EPT_EXECUTE_ONLY_BIT;
1188 }
1189
1190 static inline bool cpu_has_vmx_ept_2m_page(void)
1191 {
1192 return vmx_capability.ept & VMX_EPT_2MB_PAGE_BIT;
1193 }
1194
1195 static inline bool cpu_has_vmx_ept_1g_page(void)
1196 {
1197 return vmx_capability.ept & VMX_EPT_1GB_PAGE_BIT;
1198 }
1199
1200 static inline bool cpu_has_vmx_ept_4levels(void)
1201 {
1202 return vmx_capability.ept & VMX_EPT_PAGE_WALK_4_BIT;
1203 }
1204
1205 static inline bool cpu_has_vmx_ept_ad_bits(void)
1206 {
1207 return vmx_capability.ept & VMX_EPT_AD_BIT;
1208 }
1209
1210 static inline bool cpu_has_vmx_invept_context(void)
1211 {
1212 return vmx_capability.ept & VMX_EPT_EXTENT_CONTEXT_BIT;
1213 }
1214
1215 static inline bool cpu_has_vmx_invept_global(void)
1216 {
1217 return vmx_capability.ept & VMX_EPT_EXTENT_GLOBAL_BIT;
1218 }
1219
1220 static inline bool cpu_has_vmx_invvpid_single(void)
1221 {
1222 return vmx_capability.vpid & VMX_VPID_EXTENT_SINGLE_CONTEXT_BIT;
1223 }
1224
1225 static inline bool cpu_has_vmx_invvpid_global(void)
1226 {
1227 return vmx_capability.vpid & VMX_VPID_EXTENT_GLOBAL_CONTEXT_BIT;
1228 }
1229
1230 static inline bool cpu_has_vmx_ept(void)
1231 {
1232 return vmcs_config.cpu_based_2nd_exec_ctrl &
1233 SECONDARY_EXEC_ENABLE_EPT;
1234 }
1235
1236 static inline bool cpu_has_vmx_unrestricted_guest(void)
1237 {
1238 return vmcs_config.cpu_based_2nd_exec_ctrl &
1239 SECONDARY_EXEC_UNRESTRICTED_GUEST;
1240 }
1241
1242 static inline bool cpu_has_vmx_ple(void)
1243 {
1244 return vmcs_config.cpu_based_2nd_exec_ctrl &
1245 SECONDARY_EXEC_PAUSE_LOOP_EXITING;
1246 }
1247
1248 static inline bool cpu_has_vmx_basic_inout(void)
1249 {
1250 return (((u64)vmcs_config.basic_cap << 32) & VMX_BASIC_INOUT);
1251 }
1252
1253 static inline bool cpu_need_virtualize_apic_accesses(struct kvm_vcpu *vcpu)
1254 {
1255 return flexpriority_enabled && lapic_in_kernel(vcpu);
1256 }
1257
1258 static inline bool cpu_has_vmx_vpid(void)
1259 {
1260 return vmcs_config.cpu_based_2nd_exec_ctrl &
1261 SECONDARY_EXEC_ENABLE_VPID;
1262 }
1263
1264 static inline bool cpu_has_vmx_rdtscp(void)
1265 {
1266 return vmcs_config.cpu_based_2nd_exec_ctrl &
1267 SECONDARY_EXEC_RDTSCP;
1268 }
1269
1270 static inline bool cpu_has_vmx_invpcid(void)
1271 {
1272 return vmcs_config.cpu_based_2nd_exec_ctrl &
1273 SECONDARY_EXEC_ENABLE_INVPCID;
1274 }
1275
1276 static inline bool cpu_has_virtual_nmis(void)
1277 {
1278 return vmcs_config.pin_based_exec_ctrl & PIN_BASED_VIRTUAL_NMIS;
1279 }
1280
1281 static inline bool cpu_has_vmx_wbinvd_exit(void)
1282 {
1283 return vmcs_config.cpu_based_2nd_exec_ctrl &
1284 SECONDARY_EXEC_WBINVD_EXITING;
1285 }
1286
1287 static inline bool cpu_has_vmx_shadow_vmcs(void)
1288 {
1289 u64 vmx_msr;
1290 rdmsrl(MSR_IA32_VMX_MISC, vmx_msr);
1291 /* check if the cpu supports writing r/o exit information fields */
1292 if (!(vmx_msr & MSR_IA32_VMX_MISC_VMWRITE_SHADOW_RO_FIELDS))
1293 return false;
1294
1295 return vmcs_config.cpu_based_2nd_exec_ctrl &
1296 SECONDARY_EXEC_SHADOW_VMCS;
1297 }
1298
1299 static inline bool cpu_has_vmx_pml(void)
1300 {
1301 return vmcs_config.cpu_based_2nd_exec_ctrl & SECONDARY_EXEC_ENABLE_PML;
1302 }
1303
1304 static inline bool cpu_has_vmx_tsc_scaling(void)
1305 {
1306 return vmcs_config.cpu_based_2nd_exec_ctrl &
1307 SECONDARY_EXEC_TSC_SCALING;
1308 }
1309
1310 static inline bool report_flexpriority(void)
1311 {
1312 return flexpriority_enabled;
1313 }
1314
1315 static inline bool nested_cpu_has(struct vmcs12 *vmcs12, u32 bit)
1316 {
1317 return vmcs12->cpu_based_vm_exec_control & bit;
1318 }
1319
1320 static inline bool nested_cpu_has2(struct vmcs12 *vmcs12, u32 bit)
1321 {
1322 return (vmcs12->cpu_based_vm_exec_control &
1323 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) &&
1324 (vmcs12->secondary_vm_exec_control & bit);
1325 }
1326
1327 static inline bool nested_cpu_has_virtual_nmis(struct vmcs12 *vmcs12)
1328 {
1329 return vmcs12->pin_based_vm_exec_control & PIN_BASED_VIRTUAL_NMIS;
1330 }
1331
1332 static inline bool nested_cpu_has_preemption_timer(struct vmcs12 *vmcs12)
1333 {
1334 return vmcs12->pin_based_vm_exec_control &
1335 PIN_BASED_VMX_PREEMPTION_TIMER;
1336 }
1337
1338 static inline int nested_cpu_has_ept(struct vmcs12 *vmcs12)
1339 {
1340 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_EPT);
1341 }
1342
1343 static inline bool nested_cpu_has_xsaves(struct vmcs12 *vmcs12)
1344 {
1345 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_XSAVES) &&
1346 vmx_xsaves_supported();
1347 }
1348
1349 static inline bool nested_cpu_has_virt_x2apic_mode(struct vmcs12 *vmcs12)
1350 {
1351 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE);
1352 }
1353
1354 static inline bool nested_cpu_has_vpid(struct vmcs12 *vmcs12)
1355 {
1356 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_VPID);
1357 }
1358
1359 static inline bool nested_cpu_has_apic_reg_virt(struct vmcs12 *vmcs12)
1360 {
1361 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_APIC_REGISTER_VIRT);
1362 }
1363
1364 static inline bool nested_cpu_has_vid(struct vmcs12 *vmcs12)
1365 {
1366 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
1367 }
1368
1369 static inline bool nested_cpu_has_posted_intr(struct vmcs12 *vmcs12)
1370 {
1371 return vmcs12->pin_based_vm_exec_control & PIN_BASED_POSTED_INTR;
1372 }
1373
1374 static inline bool is_exception(u32 intr_info)
1375 {
1376 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK))
1377 == (INTR_TYPE_HARD_EXCEPTION | INTR_INFO_VALID_MASK);
1378 }
1379
1380 static void nested_vmx_vmexit(struct kvm_vcpu *vcpu, u32 exit_reason,
1381 u32 exit_intr_info,
1382 unsigned long exit_qualification);
1383 static void nested_vmx_entry_failure(struct kvm_vcpu *vcpu,
1384 struct vmcs12 *vmcs12,
1385 u32 reason, unsigned long qualification);
1386
1387 static int __find_msr_index(struct vcpu_vmx *vmx, u32 msr)
1388 {
1389 int i;
1390
1391 for (i = 0; i < vmx->nmsrs; ++i)
1392 if (vmx_msr_index[vmx->guest_msrs[i].index] == msr)
1393 return i;
1394 return -1;
1395 }
1396
1397 static inline void __invvpid(int ext, u16 vpid, gva_t gva)
1398 {
1399 struct {
1400 u64 vpid : 16;
1401 u64 rsvd : 48;
1402 u64 gva;
1403 } operand = { vpid, 0, gva };
1404
1405 asm volatile (__ex(ASM_VMX_INVVPID)
1406 /* CF==1 or ZF==1 --> rc = -1 */
1407 "; ja 1f ; ud2 ; 1:"
1408 : : "a"(&operand), "c"(ext) : "cc", "memory");
1409 }
1410
1411 static inline void __invept(int ext, u64 eptp, gpa_t gpa)
1412 {
1413 struct {
1414 u64 eptp, gpa;
1415 } operand = {eptp, gpa};
1416
1417 asm volatile (__ex(ASM_VMX_INVEPT)
1418 /* CF==1 or ZF==1 --> rc = -1 */
1419 "; ja 1f ; ud2 ; 1:\n"
1420 : : "a" (&operand), "c" (ext) : "cc", "memory");
1421 }
1422
1423 static struct shared_msr_entry *find_msr_entry(struct vcpu_vmx *vmx, u32 msr)
1424 {
1425 int i;
1426
1427 i = __find_msr_index(vmx, msr);
1428 if (i >= 0)
1429 return &vmx->guest_msrs[i];
1430 return NULL;
1431 }
1432
1433 static void vmcs_clear(struct vmcs *vmcs)
1434 {
1435 u64 phys_addr = __pa(vmcs);
1436 u8 error;
1437
1438 asm volatile (__ex(ASM_VMX_VMCLEAR_RAX) "; setna %0"
1439 : "=qm"(error) : "a"(&phys_addr), "m"(phys_addr)
1440 : "cc", "memory");
1441 if (error)
1442 printk(KERN_ERR "kvm: vmclear fail: %p/%llx\n",
1443 vmcs, phys_addr);
1444 }
1445
1446 static inline void loaded_vmcs_init(struct loaded_vmcs *loaded_vmcs)
1447 {
1448 vmcs_clear(loaded_vmcs->vmcs);
1449 if (loaded_vmcs->shadow_vmcs && loaded_vmcs->launched)
1450 vmcs_clear(loaded_vmcs->shadow_vmcs);
1451 loaded_vmcs->cpu = -1;
1452 loaded_vmcs->launched = 0;
1453 }
1454
1455 static void vmcs_load(struct vmcs *vmcs)
1456 {
1457 u64 phys_addr = __pa(vmcs);
1458 u8 error;
1459
1460 asm volatile (__ex(ASM_VMX_VMPTRLD_RAX) "; setna %0"
1461 : "=qm"(error) : "a"(&phys_addr), "m"(phys_addr)
1462 : "cc", "memory");
1463 if (error)
1464 printk(KERN_ERR "kvm: vmptrld %p/%llx failed\n",
1465 vmcs, phys_addr);
1466 }
1467
1468 #ifdef CONFIG_KEXEC_CORE
1469 /*
1470 * This bitmap is used to indicate whether the vmclear
1471 * operation is enabled on all cpus. All disabled by
1472 * default.
1473 */
1474 static cpumask_t crash_vmclear_enabled_bitmap = CPU_MASK_NONE;
1475
1476 static inline void crash_enable_local_vmclear(int cpu)
1477 {
1478 cpumask_set_cpu(cpu, &crash_vmclear_enabled_bitmap);
1479 }
1480
1481 static inline void crash_disable_local_vmclear(int cpu)
1482 {
1483 cpumask_clear_cpu(cpu, &crash_vmclear_enabled_bitmap);
1484 }
1485
1486 static inline int crash_local_vmclear_enabled(int cpu)
1487 {
1488 return cpumask_test_cpu(cpu, &crash_vmclear_enabled_bitmap);
1489 }
1490
1491 static void crash_vmclear_local_loaded_vmcss(void)
1492 {
1493 int cpu = raw_smp_processor_id();
1494 struct loaded_vmcs *v;
1495
1496 if (!crash_local_vmclear_enabled(cpu))
1497 return;
1498
1499 list_for_each_entry(v, &per_cpu(loaded_vmcss_on_cpu, cpu),
1500 loaded_vmcss_on_cpu_link)
1501 vmcs_clear(v->vmcs);
1502 }
1503 #else
1504 static inline void crash_enable_local_vmclear(int cpu) { }
1505 static inline void crash_disable_local_vmclear(int cpu) { }
1506 #endif /* CONFIG_KEXEC_CORE */
1507
1508 static void __loaded_vmcs_clear(void *arg)
1509 {
1510 struct loaded_vmcs *loaded_vmcs = arg;
1511 int cpu = raw_smp_processor_id();
1512
1513 if (loaded_vmcs->cpu != cpu)
1514 return; /* vcpu migration can race with cpu offline */
1515 if (per_cpu(current_vmcs, cpu) == loaded_vmcs->vmcs)
1516 per_cpu(current_vmcs, cpu) = NULL;
1517 crash_disable_local_vmclear(cpu);
1518 list_del(&loaded_vmcs->loaded_vmcss_on_cpu_link);
1519
1520 /*
1521 * we should ensure updating loaded_vmcs->loaded_vmcss_on_cpu_link
1522 * is before setting loaded_vmcs->vcpu to -1 which is done in
1523 * loaded_vmcs_init. Otherwise, other cpu can see vcpu = -1 fist
1524 * then adds the vmcs into percpu list before it is deleted.
1525 */
1526 smp_wmb();
1527
1528 loaded_vmcs_init(loaded_vmcs);
1529 crash_enable_local_vmclear(cpu);
1530 }
1531
1532 static void loaded_vmcs_clear(struct loaded_vmcs *loaded_vmcs)
1533 {
1534 int cpu = loaded_vmcs->cpu;
1535
1536 if (cpu != -1)
1537 smp_call_function_single(cpu,
1538 __loaded_vmcs_clear, loaded_vmcs, 1);
1539 }
1540
1541 static inline void vpid_sync_vcpu_single(int vpid)
1542 {
1543 if (vpid == 0)
1544 return;
1545
1546 if (cpu_has_vmx_invvpid_single())
1547 __invvpid(VMX_VPID_EXTENT_SINGLE_CONTEXT, vpid, 0);
1548 }
1549
1550 static inline void vpid_sync_vcpu_global(void)
1551 {
1552 if (cpu_has_vmx_invvpid_global())
1553 __invvpid(VMX_VPID_EXTENT_ALL_CONTEXT, 0, 0);
1554 }
1555
1556 static inline void vpid_sync_context(int vpid)
1557 {
1558 if (cpu_has_vmx_invvpid_single())
1559 vpid_sync_vcpu_single(vpid);
1560 else
1561 vpid_sync_vcpu_global();
1562 }
1563
1564 static inline void ept_sync_global(void)
1565 {
1566 if (cpu_has_vmx_invept_global())
1567 __invept(VMX_EPT_EXTENT_GLOBAL, 0, 0);
1568 }
1569
1570 static inline void ept_sync_context(u64 eptp)
1571 {
1572 if (enable_ept) {
1573 if (cpu_has_vmx_invept_context())
1574 __invept(VMX_EPT_EXTENT_CONTEXT, eptp, 0);
1575 else
1576 ept_sync_global();
1577 }
1578 }
1579
1580 static __always_inline void vmcs_check16(unsigned long field)
1581 {
1582 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2000,
1583 "16-bit accessor invalid for 64-bit field");
1584 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2001,
1585 "16-bit accessor invalid for 64-bit high field");
1586 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x4000,
1587 "16-bit accessor invalid for 32-bit high field");
1588 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x6000,
1589 "16-bit accessor invalid for natural width field");
1590 }
1591
1592 static __always_inline void vmcs_check32(unsigned long field)
1593 {
1594 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0,
1595 "32-bit accessor invalid for 16-bit field");
1596 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x6000,
1597 "32-bit accessor invalid for natural width field");
1598 }
1599
1600 static __always_inline void vmcs_check64(unsigned long field)
1601 {
1602 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0,
1603 "64-bit accessor invalid for 16-bit field");
1604 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2001,
1605 "64-bit accessor invalid for 64-bit high field");
1606 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x4000,
1607 "64-bit accessor invalid for 32-bit field");
1608 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x6000,
1609 "64-bit accessor invalid for natural width field");
1610 }
1611
1612 static __always_inline void vmcs_checkl(unsigned long field)
1613 {
1614 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0,
1615 "Natural width accessor invalid for 16-bit field");
1616 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2000,
1617 "Natural width accessor invalid for 64-bit field");
1618 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2001,
1619 "Natural width accessor invalid for 64-bit high field");
1620 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x4000,
1621 "Natural width accessor invalid for 32-bit field");
1622 }
1623
1624 static __always_inline unsigned long __vmcs_readl(unsigned long field)
1625 {
1626 unsigned long value;
1627
1628 asm volatile (__ex_clear(ASM_VMX_VMREAD_RDX_RAX, "%0")
1629 : "=a"(value) : "d"(field) : "cc");
1630 return value;
1631 }
1632
1633 static __always_inline u16 vmcs_read16(unsigned long field)
1634 {
1635 vmcs_check16(field);
1636 return __vmcs_readl(field);
1637 }
1638
1639 static __always_inline u32 vmcs_read32(unsigned long field)
1640 {
1641 vmcs_check32(field);
1642 return __vmcs_readl(field);
1643 }
1644
1645 static __always_inline u64 vmcs_read64(unsigned long field)
1646 {
1647 vmcs_check64(field);
1648 #ifdef CONFIG_X86_64
1649 return __vmcs_readl(field);
1650 #else
1651 return __vmcs_readl(field) | ((u64)__vmcs_readl(field+1) << 32);
1652 #endif
1653 }
1654
1655 static __always_inline unsigned long vmcs_readl(unsigned long field)
1656 {
1657 vmcs_checkl(field);
1658 return __vmcs_readl(field);
1659 }
1660
1661 static noinline void vmwrite_error(unsigned long field, unsigned long value)
1662 {
1663 printk(KERN_ERR "vmwrite error: reg %lx value %lx (err %d)\n",
1664 field, value, vmcs_read32(VM_INSTRUCTION_ERROR));
1665 dump_stack();
1666 }
1667
1668 static __always_inline void __vmcs_writel(unsigned long field, unsigned long value)
1669 {
1670 u8 error;
1671
1672 asm volatile (__ex(ASM_VMX_VMWRITE_RAX_RDX) "; setna %0"
1673 : "=q"(error) : "a"(value), "d"(field) : "cc");
1674 if (unlikely(error))
1675 vmwrite_error(field, value);
1676 }
1677
1678 static __always_inline void vmcs_write16(unsigned long field, u16 value)
1679 {
1680 vmcs_check16(field);
1681 __vmcs_writel(field, value);
1682 }
1683
1684 static __always_inline void vmcs_write32(unsigned long field, u32 value)
1685 {
1686 vmcs_check32(field);
1687 __vmcs_writel(field, value);
1688 }
1689
1690 static __always_inline void vmcs_write64(unsigned long field, u64 value)
1691 {
1692 vmcs_check64(field);
1693 __vmcs_writel(field, value);
1694 #ifndef CONFIG_X86_64
1695 asm volatile ("");
1696 __vmcs_writel(field+1, value >> 32);
1697 #endif
1698 }
1699
1700 static __always_inline void vmcs_writel(unsigned long field, unsigned long value)
1701 {
1702 vmcs_checkl(field);
1703 __vmcs_writel(field, value);
1704 }
1705
1706 static __always_inline void vmcs_clear_bits(unsigned long field, u32 mask)
1707 {
1708 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x2000,
1709 "vmcs_clear_bits does not support 64-bit fields");
1710 __vmcs_writel(field, __vmcs_readl(field) & ~mask);
1711 }
1712
1713 static __always_inline void vmcs_set_bits(unsigned long field, u32 mask)
1714 {
1715 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x2000,
1716 "vmcs_set_bits does not support 64-bit fields");
1717 __vmcs_writel(field, __vmcs_readl(field) | mask);
1718 }
1719
1720 static inline void vm_entry_controls_reset_shadow(struct vcpu_vmx *vmx)
1721 {
1722 vmx->vm_entry_controls_shadow = vmcs_read32(VM_ENTRY_CONTROLS);
1723 }
1724
1725 static inline void vm_entry_controls_init(struct vcpu_vmx *vmx, u32 val)
1726 {
1727 vmcs_write32(VM_ENTRY_CONTROLS, val);
1728 vmx->vm_entry_controls_shadow = val;
1729 }
1730
1731 static inline void vm_entry_controls_set(struct vcpu_vmx *vmx, u32 val)
1732 {
1733 if (vmx->vm_entry_controls_shadow != val)
1734 vm_entry_controls_init(vmx, val);
1735 }
1736
1737 static inline u32 vm_entry_controls_get(struct vcpu_vmx *vmx)
1738 {
1739 return vmx->vm_entry_controls_shadow;
1740 }
1741
1742
1743 static inline void vm_entry_controls_setbit(struct vcpu_vmx *vmx, u32 val)
1744 {
1745 vm_entry_controls_set(vmx, vm_entry_controls_get(vmx) | val);
1746 }
1747
1748 static inline void vm_entry_controls_clearbit(struct vcpu_vmx *vmx, u32 val)
1749 {
1750 vm_entry_controls_set(vmx, vm_entry_controls_get(vmx) & ~val);
1751 }
1752
1753 static inline void vm_exit_controls_reset_shadow(struct vcpu_vmx *vmx)
1754 {
1755 vmx->vm_exit_controls_shadow = vmcs_read32(VM_EXIT_CONTROLS);
1756 }
1757
1758 static inline void vm_exit_controls_init(struct vcpu_vmx *vmx, u32 val)
1759 {
1760 vmcs_write32(VM_EXIT_CONTROLS, val);
1761 vmx->vm_exit_controls_shadow = val;
1762 }
1763
1764 static inline void vm_exit_controls_set(struct vcpu_vmx *vmx, u32 val)
1765 {
1766 if (vmx->vm_exit_controls_shadow != val)
1767 vm_exit_controls_init(vmx, val);
1768 }
1769
1770 static inline u32 vm_exit_controls_get(struct vcpu_vmx *vmx)
1771 {
1772 return vmx->vm_exit_controls_shadow;
1773 }
1774
1775
1776 static inline void vm_exit_controls_setbit(struct vcpu_vmx *vmx, u32 val)
1777 {
1778 vm_exit_controls_set(vmx, vm_exit_controls_get(vmx) | val);
1779 }
1780
1781 static inline void vm_exit_controls_clearbit(struct vcpu_vmx *vmx, u32 val)
1782 {
1783 vm_exit_controls_set(vmx, vm_exit_controls_get(vmx) & ~val);
1784 }
1785
1786 static void vmx_segment_cache_clear(struct vcpu_vmx *vmx)
1787 {
1788 vmx->segment_cache.bitmask = 0;
1789 }
1790
1791 static bool vmx_segment_cache_test_set(struct vcpu_vmx *vmx, unsigned seg,
1792 unsigned field)
1793 {
1794 bool ret;
1795 u32 mask = 1 << (seg * SEG_FIELD_NR + field);
1796
1797 if (!(vmx->vcpu.arch.regs_avail & (1 << VCPU_EXREG_SEGMENTS))) {
1798 vmx->vcpu.arch.regs_avail |= (1 << VCPU_EXREG_SEGMENTS);
1799 vmx->segment_cache.bitmask = 0;
1800 }
1801 ret = vmx->segment_cache.bitmask & mask;
1802 vmx->segment_cache.bitmask |= mask;
1803 return ret;
1804 }
1805
1806 static u16 vmx_read_guest_seg_selector(struct vcpu_vmx *vmx, unsigned seg)
1807 {
1808 u16 *p = &vmx->segment_cache.seg[seg].selector;
1809
1810 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_SEL))
1811 *p = vmcs_read16(kvm_vmx_segment_fields[seg].selector);
1812 return *p;
1813 }
1814
1815 static ulong vmx_read_guest_seg_base(struct vcpu_vmx *vmx, unsigned seg)
1816 {
1817 ulong *p = &vmx->segment_cache.seg[seg].base;
1818
1819 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_BASE))
1820 *p = vmcs_readl(kvm_vmx_segment_fields[seg].base);
1821 return *p;
1822 }
1823
1824 static u32 vmx_read_guest_seg_limit(struct vcpu_vmx *vmx, unsigned seg)
1825 {
1826 u32 *p = &vmx->segment_cache.seg[seg].limit;
1827
1828 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_LIMIT))
1829 *p = vmcs_read32(kvm_vmx_segment_fields[seg].limit);
1830 return *p;
1831 }
1832
1833 static u32 vmx_read_guest_seg_ar(struct vcpu_vmx *vmx, unsigned seg)
1834 {
1835 u32 *p = &vmx->segment_cache.seg[seg].ar;
1836
1837 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_AR))
1838 *p = vmcs_read32(kvm_vmx_segment_fields[seg].ar_bytes);
1839 return *p;
1840 }
1841
1842 static void update_exception_bitmap(struct kvm_vcpu *vcpu)
1843 {
1844 u32 eb;
1845
1846 eb = (1u << PF_VECTOR) | (1u << UD_VECTOR) | (1u << MC_VECTOR) |
1847 (1u << NM_VECTOR) | (1u << DB_VECTOR) | (1u << AC_VECTOR);
1848 if ((vcpu->guest_debug &
1849 (KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_SW_BP)) ==
1850 (KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_SW_BP))
1851 eb |= 1u << BP_VECTOR;
1852 if (to_vmx(vcpu)->rmode.vm86_active)
1853 eb = ~0;
1854 if (enable_ept)
1855 eb &= ~(1u << PF_VECTOR); /* bypass_guest_pf = 0 */
1856 if (vcpu->fpu_active)
1857 eb &= ~(1u << NM_VECTOR);
1858
1859 /* When we are running a nested L2 guest and L1 specified for it a
1860 * certain exception bitmap, we must trap the same exceptions and pass
1861 * them to L1. When running L2, we will only handle the exceptions
1862 * specified above if L1 did not want them.
1863 */
1864 if (is_guest_mode(vcpu))
1865 eb |= get_vmcs12(vcpu)->exception_bitmap;
1866
1867 vmcs_write32(EXCEPTION_BITMAP, eb);
1868 }
1869
1870 static void clear_atomic_switch_msr_special(struct vcpu_vmx *vmx,
1871 unsigned long entry, unsigned long exit)
1872 {
1873 vm_entry_controls_clearbit(vmx, entry);
1874 vm_exit_controls_clearbit(vmx, exit);
1875 }
1876
1877 static void clear_atomic_switch_msr(struct vcpu_vmx *vmx, unsigned msr)
1878 {
1879 unsigned i;
1880 struct msr_autoload *m = &vmx->msr_autoload;
1881
1882 switch (msr) {
1883 case MSR_EFER:
1884 if (cpu_has_load_ia32_efer) {
1885 clear_atomic_switch_msr_special(vmx,
1886 VM_ENTRY_LOAD_IA32_EFER,
1887 VM_EXIT_LOAD_IA32_EFER);
1888 return;
1889 }
1890 break;
1891 case MSR_CORE_PERF_GLOBAL_CTRL:
1892 if (cpu_has_load_perf_global_ctrl) {
1893 clear_atomic_switch_msr_special(vmx,
1894 VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL,
1895 VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL);
1896 return;
1897 }
1898 break;
1899 }
1900
1901 for (i = 0; i < m->nr; ++i)
1902 if (m->guest[i].index == msr)
1903 break;
1904
1905 if (i == m->nr)
1906 return;
1907 --m->nr;
1908 m->guest[i] = m->guest[m->nr];
1909 m->host[i] = m->host[m->nr];
1910 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, m->nr);
1911 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, m->nr);
1912 }
1913
1914 static void add_atomic_switch_msr_special(struct vcpu_vmx *vmx,
1915 unsigned long entry, unsigned long exit,
1916 unsigned long guest_val_vmcs, unsigned long host_val_vmcs,
1917 u64 guest_val, u64 host_val)
1918 {
1919 vmcs_write64(guest_val_vmcs, guest_val);
1920 vmcs_write64(host_val_vmcs, host_val);
1921 vm_entry_controls_setbit(vmx, entry);
1922 vm_exit_controls_setbit(vmx, exit);
1923 }
1924
1925 static void add_atomic_switch_msr(struct vcpu_vmx *vmx, unsigned msr,
1926 u64 guest_val, u64 host_val)
1927 {
1928 unsigned i;
1929 struct msr_autoload *m = &vmx->msr_autoload;
1930
1931 switch (msr) {
1932 case MSR_EFER:
1933 if (cpu_has_load_ia32_efer) {
1934 add_atomic_switch_msr_special(vmx,
1935 VM_ENTRY_LOAD_IA32_EFER,
1936 VM_EXIT_LOAD_IA32_EFER,
1937 GUEST_IA32_EFER,
1938 HOST_IA32_EFER,
1939 guest_val, host_val);
1940 return;
1941 }
1942 break;
1943 case MSR_CORE_PERF_GLOBAL_CTRL:
1944 if (cpu_has_load_perf_global_ctrl) {
1945 add_atomic_switch_msr_special(vmx,
1946 VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL,
1947 VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL,
1948 GUEST_IA32_PERF_GLOBAL_CTRL,
1949 HOST_IA32_PERF_GLOBAL_CTRL,
1950 guest_val, host_val);
1951 return;
1952 }
1953 break;
1954 case MSR_IA32_PEBS_ENABLE:
1955 /* PEBS needs a quiescent period after being disabled (to write
1956 * a record). Disabling PEBS through VMX MSR swapping doesn't
1957 * provide that period, so a CPU could write host's record into
1958 * guest's memory.
1959 */
1960 wrmsrl(MSR_IA32_PEBS_ENABLE, 0);
1961 }
1962
1963 for (i = 0; i < m->nr; ++i)
1964 if (m->guest[i].index == msr)
1965 break;
1966
1967 if (i == NR_AUTOLOAD_MSRS) {
1968 printk_once(KERN_WARNING "Not enough msr switch entries. "
1969 "Can't add msr %x\n", msr);
1970 return;
1971 } else if (i == m->nr) {
1972 ++m->nr;
1973 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, m->nr);
1974 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, m->nr);
1975 }
1976
1977 m->guest[i].index = msr;
1978 m->guest[i].value = guest_val;
1979 m->host[i].index = msr;
1980 m->host[i].value = host_val;
1981 }
1982
1983 static void reload_tss(void)
1984 {
1985 /*
1986 * VT restores TR but not its size. Useless.
1987 */
1988 struct desc_ptr *gdt = this_cpu_ptr(&host_gdt);
1989 struct desc_struct *descs;
1990
1991 descs = (void *)gdt->address;
1992 descs[GDT_ENTRY_TSS].type = 9; /* available TSS */
1993 load_TR_desc();
1994 }
1995
1996 static bool update_transition_efer(struct vcpu_vmx *vmx, int efer_offset)
1997 {
1998 u64 guest_efer = vmx->vcpu.arch.efer;
1999 u64 ignore_bits = 0;
2000
2001 if (!enable_ept) {
2002 /*
2003 * NX is needed to handle CR0.WP=1, CR4.SMEP=1. Testing
2004 * host CPUID is more efficient than testing guest CPUID
2005 * or CR4. Host SMEP is anyway a requirement for guest SMEP.
2006 */
2007 if (boot_cpu_has(X86_FEATURE_SMEP))
2008 guest_efer |= EFER_NX;
2009 else if (!(guest_efer & EFER_NX))
2010 ignore_bits |= EFER_NX;
2011 }
2012
2013 /*
2014 * LMA and LME handled by hardware; SCE meaningless outside long mode.
2015 */
2016 ignore_bits |= EFER_SCE;
2017 #ifdef CONFIG_X86_64
2018 ignore_bits |= EFER_LMA | EFER_LME;
2019 /* SCE is meaningful only in long mode on Intel */
2020 if (guest_efer & EFER_LMA)
2021 ignore_bits &= ~(u64)EFER_SCE;
2022 #endif
2023
2024 clear_atomic_switch_msr(vmx, MSR_EFER);
2025
2026 /*
2027 * On EPT, we can't emulate NX, so we must switch EFER atomically.
2028 * On CPUs that support "load IA32_EFER", always switch EFER
2029 * atomically, since it's faster than switching it manually.
2030 */
2031 if (cpu_has_load_ia32_efer ||
2032 (enable_ept && ((vmx->vcpu.arch.efer ^ host_efer) & EFER_NX))) {
2033 if (!(guest_efer & EFER_LMA))
2034 guest_efer &= ~EFER_LME;
2035 if (guest_efer != host_efer)
2036 add_atomic_switch_msr(vmx, MSR_EFER,
2037 guest_efer, host_efer);
2038 return false;
2039 } else {
2040 guest_efer &= ~ignore_bits;
2041 guest_efer |= host_efer & ignore_bits;
2042
2043 vmx->guest_msrs[efer_offset].data = guest_efer;
2044 vmx->guest_msrs[efer_offset].mask = ~ignore_bits;
2045
2046 return true;
2047 }
2048 }
2049
2050 static unsigned long segment_base(u16 selector)
2051 {
2052 struct desc_ptr *gdt = this_cpu_ptr(&host_gdt);
2053 struct desc_struct *d;
2054 unsigned long table_base;
2055 unsigned long v;
2056
2057 if (!(selector & ~3))
2058 return 0;
2059
2060 table_base = gdt->address;
2061
2062 if (selector & 4) { /* from ldt */
2063 u16 ldt_selector = kvm_read_ldt();
2064
2065 if (!(ldt_selector & ~3))
2066 return 0;
2067
2068 table_base = segment_base(ldt_selector);
2069 }
2070 d = (struct desc_struct *)(table_base + (selector & ~7));
2071 v = get_desc_base(d);
2072 #ifdef CONFIG_X86_64
2073 if (d->s == 0 && (d->type == 2 || d->type == 9 || d->type == 11))
2074 v |= ((unsigned long)((struct ldttss_desc64 *)d)->base3) << 32;
2075 #endif
2076 return v;
2077 }
2078
2079 static inline unsigned long kvm_read_tr_base(void)
2080 {
2081 u16 tr;
2082 asm("str %0" : "=g"(tr));
2083 return segment_base(tr);
2084 }
2085
2086 static void vmx_save_host_state(struct kvm_vcpu *vcpu)
2087 {
2088 struct vcpu_vmx *vmx = to_vmx(vcpu);
2089 int i;
2090
2091 if (vmx->host_state.loaded)
2092 return;
2093
2094 vmx->host_state.loaded = 1;
2095 /*
2096 * Set host fs and gs selectors. Unfortunately, 22.2.3 does not
2097 * allow segment selectors with cpl > 0 or ti == 1.
2098 */
2099 vmx->host_state.ldt_sel = kvm_read_ldt();
2100 vmx->host_state.gs_ldt_reload_needed = vmx->host_state.ldt_sel;
2101 savesegment(fs, vmx->host_state.fs_sel);
2102 if (!(vmx->host_state.fs_sel & 7)) {
2103 vmcs_write16(HOST_FS_SELECTOR, vmx->host_state.fs_sel);
2104 vmx->host_state.fs_reload_needed = 0;
2105 } else {
2106 vmcs_write16(HOST_FS_SELECTOR, 0);
2107 vmx->host_state.fs_reload_needed = 1;
2108 }
2109 savesegment(gs, vmx->host_state.gs_sel);
2110 if (!(vmx->host_state.gs_sel & 7))
2111 vmcs_write16(HOST_GS_SELECTOR, vmx->host_state.gs_sel);
2112 else {
2113 vmcs_write16(HOST_GS_SELECTOR, 0);
2114 vmx->host_state.gs_ldt_reload_needed = 1;
2115 }
2116
2117 #ifdef CONFIG_X86_64
2118 savesegment(ds, vmx->host_state.ds_sel);
2119 savesegment(es, vmx->host_state.es_sel);
2120 #endif
2121
2122 #ifdef CONFIG_X86_64
2123 vmcs_writel(HOST_FS_BASE, read_msr(MSR_FS_BASE));
2124 vmcs_writel(HOST_GS_BASE, read_msr(MSR_GS_BASE));
2125 #else
2126 vmcs_writel(HOST_FS_BASE, segment_base(vmx->host_state.fs_sel));
2127 vmcs_writel(HOST_GS_BASE, segment_base(vmx->host_state.gs_sel));
2128 #endif
2129
2130 #ifdef CONFIG_X86_64
2131 rdmsrl(MSR_KERNEL_GS_BASE, vmx->msr_host_kernel_gs_base);
2132 if (is_long_mode(&vmx->vcpu))
2133 wrmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base);
2134 #endif
2135 if (boot_cpu_has(X86_FEATURE_MPX))
2136 rdmsrl(MSR_IA32_BNDCFGS, vmx->host_state.msr_host_bndcfgs);
2137 for (i = 0; i < vmx->save_nmsrs; ++i)
2138 kvm_set_shared_msr(vmx->guest_msrs[i].index,
2139 vmx->guest_msrs[i].data,
2140 vmx->guest_msrs[i].mask);
2141 }
2142
2143 static void __vmx_load_host_state(struct vcpu_vmx *vmx)
2144 {
2145 if (!vmx->host_state.loaded)
2146 return;
2147
2148 ++vmx->vcpu.stat.host_state_reload;
2149 vmx->host_state.loaded = 0;
2150 #ifdef CONFIG_X86_64
2151 if (is_long_mode(&vmx->vcpu))
2152 rdmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base);
2153 #endif
2154 if (vmx->host_state.gs_ldt_reload_needed) {
2155 kvm_load_ldt(vmx->host_state.ldt_sel);
2156 #ifdef CONFIG_X86_64
2157 load_gs_index(vmx->host_state.gs_sel);
2158 #else
2159 loadsegment(gs, vmx->host_state.gs_sel);
2160 #endif
2161 }
2162 if (vmx->host_state.fs_reload_needed)
2163 loadsegment(fs, vmx->host_state.fs_sel);
2164 #ifdef CONFIG_X86_64
2165 if (unlikely(vmx->host_state.ds_sel | vmx->host_state.es_sel)) {
2166 loadsegment(ds, vmx->host_state.ds_sel);
2167 loadsegment(es, vmx->host_state.es_sel);
2168 }
2169 #endif
2170 reload_tss();
2171 #ifdef CONFIG_X86_64
2172 wrmsrl(MSR_KERNEL_GS_BASE, vmx->msr_host_kernel_gs_base);
2173 #endif
2174 if (vmx->host_state.msr_host_bndcfgs)
2175 wrmsrl(MSR_IA32_BNDCFGS, vmx->host_state.msr_host_bndcfgs);
2176 /*
2177 * If the FPU is not active (through the host task or
2178 * the guest vcpu), then restore the cr0.TS bit.
2179 */
2180 if (!fpregs_active() && !vmx->vcpu.guest_fpu_loaded)
2181 stts();
2182 load_gdt(this_cpu_ptr(&host_gdt));
2183 }
2184
2185 static void vmx_load_host_state(struct vcpu_vmx *vmx)
2186 {
2187 preempt_disable();
2188 __vmx_load_host_state(vmx);
2189 preempt_enable();
2190 }
2191
2192 static void vmx_vcpu_pi_load(struct kvm_vcpu *vcpu, int cpu)
2193 {
2194 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
2195 struct pi_desc old, new;
2196 unsigned int dest;
2197
2198 if (!kvm_arch_has_assigned_device(vcpu->kvm) ||
2199 !irq_remapping_cap(IRQ_POSTING_CAP) ||
2200 !kvm_vcpu_apicv_active(vcpu))
2201 return;
2202
2203 do {
2204 old.control = new.control = pi_desc->control;
2205
2206 /*
2207 * If 'nv' field is POSTED_INTR_WAKEUP_VECTOR, there
2208 * are two possible cases:
2209 * 1. After running 'pre_block', context switch
2210 * happened. For this case, 'sn' was set in
2211 * vmx_vcpu_put(), so we need to clear it here.
2212 * 2. After running 'pre_block', we were blocked,
2213 * and woken up by some other guy. For this case,
2214 * we don't need to do anything, 'pi_post_block'
2215 * will do everything for us. However, we cannot
2216 * check whether it is case #1 or case #2 here
2217 * (maybe, not needed), so we also clear sn here,
2218 * I think it is not a big deal.
2219 */
2220 if (pi_desc->nv != POSTED_INTR_WAKEUP_VECTOR) {
2221 if (vcpu->cpu != cpu) {
2222 dest = cpu_physical_id(cpu);
2223
2224 if (x2apic_enabled())
2225 new.ndst = dest;
2226 else
2227 new.ndst = (dest << 8) & 0xFF00;
2228 }
2229
2230 /* set 'NV' to 'notification vector' */
2231 new.nv = POSTED_INTR_VECTOR;
2232 }
2233
2234 /* Allow posting non-urgent interrupts */
2235 new.sn = 0;
2236 } while (cmpxchg(&pi_desc->control, old.control,
2237 new.control) != old.control);
2238 }
2239
2240 static void decache_tsc_multiplier(struct vcpu_vmx *vmx)
2241 {
2242 vmx->current_tsc_ratio = vmx->vcpu.arch.tsc_scaling_ratio;
2243 vmcs_write64(TSC_MULTIPLIER, vmx->current_tsc_ratio);
2244 }
2245
2246 /*
2247 * Switches to specified vcpu, until a matching vcpu_put(), but assumes
2248 * vcpu mutex is already taken.
2249 */
2250 static void vmx_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
2251 {
2252 struct vcpu_vmx *vmx = to_vmx(vcpu);
2253 u64 phys_addr = __pa(per_cpu(vmxarea, cpu));
2254 bool already_loaded = vmx->loaded_vmcs->cpu == cpu;
2255
2256 if (!vmm_exclusive)
2257 kvm_cpu_vmxon(phys_addr);
2258 else if (!already_loaded)
2259 loaded_vmcs_clear(vmx->loaded_vmcs);
2260
2261 if (!already_loaded) {
2262 local_irq_disable();
2263 crash_disable_local_vmclear(cpu);
2264
2265 /*
2266 * Read loaded_vmcs->cpu should be before fetching
2267 * loaded_vmcs->loaded_vmcss_on_cpu_link.
2268 * See the comments in __loaded_vmcs_clear().
2269 */
2270 smp_rmb();
2271
2272 list_add(&vmx->loaded_vmcs->loaded_vmcss_on_cpu_link,
2273 &per_cpu(loaded_vmcss_on_cpu, cpu));
2274 crash_enable_local_vmclear(cpu);
2275 local_irq_enable();
2276 }
2277
2278 if (per_cpu(current_vmcs, cpu) != vmx->loaded_vmcs->vmcs) {
2279 per_cpu(current_vmcs, cpu) = vmx->loaded_vmcs->vmcs;
2280 vmcs_load(vmx->loaded_vmcs->vmcs);
2281 }
2282
2283 if (!already_loaded) {
2284 struct desc_ptr *gdt = this_cpu_ptr(&host_gdt);
2285 unsigned long sysenter_esp;
2286
2287 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
2288
2289 /*
2290 * Linux uses per-cpu TSS and GDT, so set these when switching
2291 * processors.
2292 */
2293 vmcs_writel(HOST_TR_BASE, kvm_read_tr_base()); /* 22.2.4 */
2294 vmcs_writel(HOST_GDTR_BASE, gdt->address); /* 22.2.4 */
2295
2296 rdmsrl(MSR_IA32_SYSENTER_ESP, sysenter_esp);
2297 vmcs_writel(HOST_IA32_SYSENTER_ESP, sysenter_esp); /* 22.2.3 */
2298
2299 vmx->loaded_vmcs->cpu = cpu;
2300 }
2301
2302 /* Setup TSC multiplier */
2303 if (kvm_has_tsc_control &&
2304 vmx->current_tsc_ratio != vcpu->arch.tsc_scaling_ratio)
2305 decache_tsc_multiplier(vmx);
2306
2307 vmx_vcpu_pi_load(vcpu, cpu);
2308 vmx->host_pkru = read_pkru();
2309 }
2310
2311 static void vmx_vcpu_pi_put(struct kvm_vcpu *vcpu)
2312 {
2313 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
2314
2315 if (!kvm_arch_has_assigned_device(vcpu->kvm) ||
2316 !irq_remapping_cap(IRQ_POSTING_CAP) ||
2317 !kvm_vcpu_apicv_active(vcpu))
2318 return;
2319
2320 /* Set SN when the vCPU is preempted */
2321 if (vcpu->preempted)
2322 pi_set_sn(pi_desc);
2323 }
2324
2325 static void vmx_vcpu_put(struct kvm_vcpu *vcpu)
2326 {
2327 vmx_vcpu_pi_put(vcpu);
2328
2329 __vmx_load_host_state(to_vmx(vcpu));
2330 if (!vmm_exclusive) {
2331 __loaded_vmcs_clear(to_vmx(vcpu)->loaded_vmcs);
2332 vcpu->cpu = -1;
2333 kvm_cpu_vmxoff();
2334 }
2335 }
2336
2337 static void vmx_fpu_activate(struct kvm_vcpu *vcpu)
2338 {
2339 ulong cr0;
2340
2341 if (vcpu->fpu_active)
2342 return;
2343 vcpu->fpu_active = 1;
2344 cr0 = vmcs_readl(GUEST_CR0);
2345 cr0 &= ~(X86_CR0_TS | X86_CR0_MP);
2346 cr0 |= kvm_read_cr0_bits(vcpu, X86_CR0_TS | X86_CR0_MP);
2347 vmcs_writel(GUEST_CR0, cr0);
2348 update_exception_bitmap(vcpu);
2349 vcpu->arch.cr0_guest_owned_bits = X86_CR0_TS;
2350 if (is_guest_mode(vcpu))
2351 vcpu->arch.cr0_guest_owned_bits &=
2352 ~get_vmcs12(vcpu)->cr0_guest_host_mask;
2353 vmcs_writel(CR0_GUEST_HOST_MASK, ~vcpu->arch.cr0_guest_owned_bits);
2354 }
2355
2356 static void vmx_decache_cr0_guest_bits(struct kvm_vcpu *vcpu);
2357
2358 /*
2359 * Return the cr0 value that a nested guest would read. This is a combination
2360 * of the real cr0 used to run the guest (guest_cr0), and the bits shadowed by
2361 * its hypervisor (cr0_read_shadow).
2362 */
2363 static inline unsigned long nested_read_cr0(struct vmcs12 *fields)
2364 {
2365 return (fields->guest_cr0 & ~fields->cr0_guest_host_mask) |
2366 (fields->cr0_read_shadow & fields->cr0_guest_host_mask);
2367 }
2368 static inline unsigned long nested_read_cr4(struct vmcs12 *fields)
2369 {
2370 return (fields->guest_cr4 & ~fields->cr4_guest_host_mask) |
2371 (fields->cr4_read_shadow & fields->cr4_guest_host_mask);
2372 }
2373
2374 static void vmx_fpu_deactivate(struct kvm_vcpu *vcpu)
2375 {
2376 /* Note that there is no vcpu->fpu_active = 0 here. The caller must
2377 * set this *before* calling this function.
2378 */
2379 vmx_decache_cr0_guest_bits(vcpu);
2380 vmcs_set_bits(GUEST_CR0, X86_CR0_TS | X86_CR0_MP);
2381 update_exception_bitmap(vcpu);
2382 vcpu->arch.cr0_guest_owned_bits = 0;
2383 vmcs_writel(CR0_GUEST_HOST_MASK, ~vcpu->arch.cr0_guest_owned_bits);
2384 if (is_guest_mode(vcpu)) {
2385 /*
2386 * L1's specified read shadow might not contain the TS bit,
2387 * so now that we turned on shadowing of this bit, we need to
2388 * set this bit of the shadow. Like in nested_vmx_run we need
2389 * nested_read_cr0(vmcs12), but vmcs12->guest_cr0 is not yet
2390 * up-to-date here because we just decached cr0.TS (and we'll
2391 * only update vmcs12->guest_cr0 on nested exit).
2392 */
2393 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
2394 vmcs12->guest_cr0 = (vmcs12->guest_cr0 & ~X86_CR0_TS) |
2395 (vcpu->arch.cr0 & X86_CR0_TS);
2396 vmcs_writel(CR0_READ_SHADOW, nested_read_cr0(vmcs12));
2397 } else
2398 vmcs_writel(CR0_READ_SHADOW, vcpu->arch.cr0);
2399 }
2400
2401 static unsigned long vmx_get_rflags(struct kvm_vcpu *vcpu)
2402 {
2403 unsigned long rflags, save_rflags;
2404
2405 if (!test_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail)) {
2406 __set_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail);
2407 rflags = vmcs_readl(GUEST_RFLAGS);
2408 if (to_vmx(vcpu)->rmode.vm86_active) {
2409 rflags &= RMODE_GUEST_OWNED_EFLAGS_BITS;
2410 save_rflags = to_vmx(vcpu)->rmode.save_rflags;
2411 rflags |= save_rflags & ~RMODE_GUEST_OWNED_EFLAGS_BITS;
2412 }
2413 to_vmx(vcpu)->rflags = rflags;
2414 }
2415 return to_vmx(vcpu)->rflags;
2416 }
2417
2418 static void vmx_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags)
2419 {
2420 __set_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail);
2421 to_vmx(vcpu)->rflags = rflags;
2422 if (to_vmx(vcpu)->rmode.vm86_active) {
2423 to_vmx(vcpu)->rmode.save_rflags = rflags;
2424 rflags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM;
2425 }
2426 vmcs_writel(GUEST_RFLAGS, rflags);
2427 }
2428
2429 static u32 vmx_get_pkru(struct kvm_vcpu *vcpu)
2430 {
2431 return to_vmx(vcpu)->guest_pkru;
2432 }
2433
2434 static u32 vmx_get_interrupt_shadow(struct kvm_vcpu *vcpu)
2435 {
2436 u32 interruptibility = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
2437 int ret = 0;
2438
2439 if (interruptibility & GUEST_INTR_STATE_STI)
2440 ret |= KVM_X86_SHADOW_INT_STI;
2441 if (interruptibility & GUEST_INTR_STATE_MOV_SS)
2442 ret |= KVM_X86_SHADOW_INT_MOV_SS;
2443
2444 return ret;
2445 }
2446
2447 static void vmx_set_interrupt_shadow(struct kvm_vcpu *vcpu, int mask)
2448 {
2449 u32 interruptibility_old = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
2450 u32 interruptibility = interruptibility_old;
2451
2452 interruptibility &= ~(GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS);
2453
2454 if (mask & KVM_X86_SHADOW_INT_MOV_SS)
2455 interruptibility |= GUEST_INTR_STATE_MOV_SS;
2456 else if (mask & KVM_X86_SHADOW_INT_STI)
2457 interruptibility |= GUEST_INTR_STATE_STI;
2458
2459 if ((interruptibility != interruptibility_old))
2460 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, interruptibility);
2461 }
2462
2463 static void skip_emulated_instruction(struct kvm_vcpu *vcpu)
2464 {
2465 unsigned long rip;
2466
2467 rip = kvm_rip_read(vcpu);
2468 rip += vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
2469 kvm_rip_write(vcpu, rip);
2470
2471 /* skipping an emulated instruction also counts */
2472 vmx_set_interrupt_shadow(vcpu, 0);
2473 }
2474
2475 /*
2476 * KVM wants to inject page-faults which it got to the guest. This function
2477 * checks whether in a nested guest, we need to inject them to L1 or L2.
2478 */
2479 static int nested_vmx_check_exception(struct kvm_vcpu *vcpu, unsigned nr)
2480 {
2481 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
2482
2483 if (!(vmcs12->exception_bitmap & (1u << nr)))
2484 return 0;
2485
2486 nested_vmx_vmexit(vcpu, to_vmx(vcpu)->exit_reason,
2487 vmcs_read32(VM_EXIT_INTR_INFO),
2488 vmcs_readl(EXIT_QUALIFICATION));
2489 return 1;
2490 }
2491
2492 static void vmx_queue_exception(struct kvm_vcpu *vcpu, unsigned nr,
2493 bool has_error_code, u32 error_code,
2494 bool reinject)
2495 {
2496 struct vcpu_vmx *vmx = to_vmx(vcpu);
2497 u32 intr_info = nr | INTR_INFO_VALID_MASK;
2498
2499 if (!reinject && is_guest_mode(vcpu) &&
2500 nested_vmx_check_exception(vcpu, nr))
2501 return;
2502
2503 if (has_error_code) {
2504 vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE, error_code);
2505 intr_info |= INTR_INFO_DELIVER_CODE_MASK;
2506 }
2507
2508 if (vmx->rmode.vm86_active) {
2509 int inc_eip = 0;
2510 if (kvm_exception_is_soft(nr))
2511 inc_eip = vcpu->arch.event_exit_inst_len;
2512 if (kvm_inject_realmode_interrupt(vcpu, nr, inc_eip) != EMULATE_DONE)
2513 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
2514 return;
2515 }
2516
2517 if (kvm_exception_is_soft(nr)) {
2518 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
2519 vmx->vcpu.arch.event_exit_inst_len);
2520 intr_info |= INTR_TYPE_SOFT_EXCEPTION;
2521 } else
2522 intr_info |= INTR_TYPE_HARD_EXCEPTION;
2523
2524 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr_info);
2525 }
2526
2527 static bool vmx_rdtscp_supported(void)
2528 {
2529 return cpu_has_vmx_rdtscp();
2530 }
2531
2532 static bool vmx_invpcid_supported(void)
2533 {
2534 return cpu_has_vmx_invpcid() && enable_ept;
2535 }
2536
2537 /*
2538 * Swap MSR entry in host/guest MSR entry array.
2539 */
2540 static void move_msr_up(struct vcpu_vmx *vmx, int from, int to)
2541 {
2542 struct shared_msr_entry tmp;
2543
2544 tmp = vmx->guest_msrs[to];
2545 vmx->guest_msrs[to] = vmx->guest_msrs[from];
2546 vmx->guest_msrs[from] = tmp;
2547 }
2548
2549 static void vmx_set_msr_bitmap(struct kvm_vcpu *vcpu)
2550 {
2551 unsigned long *msr_bitmap;
2552
2553 if (is_guest_mode(vcpu))
2554 msr_bitmap = to_vmx(vcpu)->nested.msr_bitmap;
2555 else if (cpu_has_secondary_exec_ctrls() &&
2556 (vmcs_read32(SECONDARY_VM_EXEC_CONTROL) &
2557 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE)) {
2558 if (enable_apicv && kvm_vcpu_apicv_active(vcpu)) {
2559 if (is_long_mode(vcpu))
2560 msr_bitmap = vmx_msr_bitmap_longmode_x2apic_apicv;
2561 else
2562 msr_bitmap = vmx_msr_bitmap_legacy_x2apic_apicv;
2563 } else {
2564 if (is_long_mode(vcpu))
2565 msr_bitmap = vmx_msr_bitmap_longmode_x2apic;
2566 else
2567 msr_bitmap = vmx_msr_bitmap_legacy_x2apic;
2568 }
2569 } else {
2570 if (is_long_mode(vcpu))
2571 msr_bitmap = vmx_msr_bitmap_longmode;
2572 else
2573 msr_bitmap = vmx_msr_bitmap_legacy;
2574 }
2575
2576 vmcs_write64(MSR_BITMAP, __pa(msr_bitmap));
2577 }
2578
2579 /*
2580 * Set up the vmcs to automatically save and restore system
2581 * msrs. Don't touch the 64-bit msrs if the guest is in legacy
2582 * mode, as fiddling with msrs is very expensive.
2583 */
2584 static void setup_msrs(struct vcpu_vmx *vmx)
2585 {
2586 int save_nmsrs, index;
2587
2588 save_nmsrs = 0;
2589 #ifdef CONFIG_X86_64
2590 if (is_long_mode(&vmx->vcpu)) {
2591 index = __find_msr_index(vmx, MSR_SYSCALL_MASK);
2592 if (index >= 0)
2593 move_msr_up(vmx, index, save_nmsrs++);
2594 index = __find_msr_index(vmx, MSR_LSTAR);
2595 if (index >= 0)
2596 move_msr_up(vmx, index, save_nmsrs++);
2597 index = __find_msr_index(vmx, MSR_CSTAR);
2598 if (index >= 0)
2599 move_msr_up(vmx, index, save_nmsrs++);
2600 index = __find_msr_index(vmx, MSR_TSC_AUX);
2601 if (index >= 0 && guest_cpuid_has_rdtscp(&vmx->vcpu))
2602 move_msr_up(vmx, index, save_nmsrs++);
2603 /*
2604 * MSR_STAR is only needed on long mode guests, and only
2605 * if efer.sce is enabled.
2606 */
2607 index = __find_msr_index(vmx, MSR_STAR);
2608 if ((index >= 0) && (vmx->vcpu.arch.efer & EFER_SCE))
2609 move_msr_up(vmx, index, save_nmsrs++);
2610 }
2611 #endif
2612 index = __find_msr_index(vmx, MSR_EFER);
2613 if (index >= 0 && update_transition_efer(vmx, index))
2614 move_msr_up(vmx, index, save_nmsrs++);
2615
2616 vmx->save_nmsrs = save_nmsrs;
2617
2618 if (cpu_has_vmx_msr_bitmap())
2619 vmx_set_msr_bitmap(&vmx->vcpu);
2620 }
2621
2622 /*
2623 * reads and returns guest's timestamp counter "register"
2624 * guest_tsc = (host_tsc * tsc multiplier) >> 48 + tsc_offset
2625 * -- Intel TSC Scaling for Virtualization White Paper, sec 1.3
2626 */
2627 static u64 guest_read_tsc(struct kvm_vcpu *vcpu)
2628 {
2629 u64 host_tsc, tsc_offset;
2630
2631 host_tsc = rdtsc();
2632 tsc_offset = vmcs_read64(TSC_OFFSET);
2633 return kvm_scale_tsc(vcpu, host_tsc) + tsc_offset;
2634 }
2635
2636 /*
2637 * writes 'offset' into guest's timestamp counter offset register
2638 */
2639 static void vmx_write_tsc_offset(struct kvm_vcpu *vcpu, u64 offset)
2640 {
2641 if (is_guest_mode(vcpu)) {
2642 /*
2643 * We're here if L1 chose not to trap WRMSR to TSC. According
2644 * to the spec, this should set L1's TSC; The offset that L1
2645 * set for L2 remains unchanged, and still needs to be added
2646 * to the newly set TSC to get L2's TSC.
2647 */
2648 struct vmcs12 *vmcs12;
2649 /* recalculate vmcs02.TSC_OFFSET: */
2650 vmcs12 = get_vmcs12(vcpu);
2651 vmcs_write64(TSC_OFFSET, offset +
2652 (nested_cpu_has(vmcs12, CPU_BASED_USE_TSC_OFFSETING) ?
2653 vmcs12->tsc_offset : 0));
2654 } else {
2655 trace_kvm_write_tsc_offset(vcpu->vcpu_id,
2656 vmcs_read64(TSC_OFFSET), offset);
2657 vmcs_write64(TSC_OFFSET, offset);
2658 }
2659 }
2660
2661 static bool guest_cpuid_has_vmx(struct kvm_vcpu *vcpu)
2662 {
2663 struct kvm_cpuid_entry2 *best = kvm_find_cpuid_entry(vcpu, 1, 0);
2664 return best && (best->ecx & (1 << (X86_FEATURE_VMX & 31)));
2665 }
2666
2667 /*
2668 * nested_vmx_allowed() checks whether a guest should be allowed to use VMX
2669 * instructions and MSRs (i.e., nested VMX). Nested VMX is disabled for
2670 * all guests if the "nested" module option is off, and can also be disabled
2671 * for a single guest by disabling its VMX cpuid bit.
2672 */
2673 static inline bool nested_vmx_allowed(struct kvm_vcpu *vcpu)
2674 {
2675 return nested && guest_cpuid_has_vmx(vcpu);
2676 }
2677
2678 /*
2679 * nested_vmx_setup_ctls_msrs() sets up variables containing the values to be
2680 * returned for the various VMX controls MSRs when nested VMX is enabled.
2681 * The same values should also be used to verify that vmcs12 control fields are
2682 * valid during nested entry from L1 to L2.
2683 * Each of these control msrs has a low and high 32-bit half: A low bit is on
2684 * if the corresponding bit in the (32-bit) control field *must* be on, and a
2685 * bit in the high half is on if the corresponding bit in the control field
2686 * may be on. See also vmx_control_verify().
2687 */
2688 static void nested_vmx_setup_ctls_msrs(struct vcpu_vmx *vmx)
2689 {
2690 /*
2691 * Note that as a general rule, the high half of the MSRs (bits in
2692 * the control fields which may be 1) should be initialized by the
2693 * intersection of the underlying hardware's MSR (i.e., features which
2694 * can be supported) and the list of features we want to expose -
2695 * because they are known to be properly supported in our code.
2696 * Also, usually, the low half of the MSRs (bits which must be 1) can
2697 * be set to 0, meaning that L1 may turn off any of these bits. The
2698 * reason is that if one of these bits is necessary, it will appear
2699 * in vmcs01 and prepare_vmcs02, when it bitwise-or's the control
2700 * fields of vmcs01 and vmcs02, will turn these bits off - and
2701 * nested_vmx_exit_handled() will not pass related exits to L1.
2702 * These rules have exceptions below.
2703 */
2704
2705 /* pin-based controls */
2706 rdmsr(MSR_IA32_VMX_PINBASED_CTLS,
2707 vmx->nested.nested_vmx_pinbased_ctls_low,
2708 vmx->nested.nested_vmx_pinbased_ctls_high);
2709 vmx->nested.nested_vmx_pinbased_ctls_low |=
2710 PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
2711 vmx->nested.nested_vmx_pinbased_ctls_high &=
2712 PIN_BASED_EXT_INTR_MASK |
2713 PIN_BASED_NMI_EXITING |
2714 PIN_BASED_VIRTUAL_NMIS;
2715 vmx->nested.nested_vmx_pinbased_ctls_high |=
2716 PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR |
2717 PIN_BASED_VMX_PREEMPTION_TIMER;
2718 if (kvm_vcpu_apicv_active(&vmx->vcpu))
2719 vmx->nested.nested_vmx_pinbased_ctls_high |=
2720 PIN_BASED_POSTED_INTR;
2721
2722 /* exit controls */
2723 rdmsr(MSR_IA32_VMX_EXIT_CTLS,
2724 vmx->nested.nested_vmx_exit_ctls_low,
2725 vmx->nested.nested_vmx_exit_ctls_high);
2726 vmx->nested.nested_vmx_exit_ctls_low =
2727 VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR;
2728
2729 vmx->nested.nested_vmx_exit_ctls_high &=
2730 #ifdef CONFIG_X86_64
2731 VM_EXIT_HOST_ADDR_SPACE_SIZE |
2732 #endif
2733 VM_EXIT_LOAD_IA32_PAT | VM_EXIT_SAVE_IA32_PAT;
2734 vmx->nested.nested_vmx_exit_ctls_high |=
2735 VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR |
2736 VM_EXIT_LOAD_IA32_EFER | VM_EXIT_SAVE_IA32_EFER |
2737 VM_EXIT_SAVE_VMX_PREEMPTION_TIMER | VM_EXIT_ACK_INTR_ON_EXIT;
2738
2739 if (kvm_mpx_supported())
2740 vmx->nested.nested_vmx_exit_ctls_high |= VM_EXIT_CLEAR_BNDCFGS;
2741
2742 /* We support free control of debug control saving. */
2743 vmx->nested.nested_vmx_true_exit_ctls_low =
2744 vmx->nested.nested_vmx_exit_ctls_low &
2745 ~VM_EXIT_SAVE_DEBUG_CONTROLS;
2746
2747 /* entry controls */
2748 rdmsr(MSR_IA32_VMX_ENTRY_CTLS,
2749 vmx->nested.nested_vmx_entry_ctls_low,
2750 vmx->nested.nested_vmx_entry_ctls_high);
2751 vmx->nested.nested_vmx_entry_ctls_low =
2752 VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR;
2753 vmx->nested.nested_vmx_entry_ctls_high &=
2754 #ifdef CONFIG_X86_64
2755 VM_ENTRY_IA32E_MODE |
2756 #endif
2757 VM_ENTRY_LOAD_IA32_PAT;
2758 vmx->nested.nested_vmx_entry_ctls_high |=
2759 (VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR | VM_ENTRY_LOAD_IA32_EFER);
2760 if (kvm_mpx_supported())
2761 vmx->nested.nested_vmx_entry_ctls_high |= VM_ENTRY_LOAD_BNDCFGS;
2762
2763 /* We support free control of debug control loading. */
2764 vmx->nested.nested_vmx_true_entry_ctls_low =
2765 vmx->nested.nested_vmx_entry_ctls_low &
2766 ~VM_ENTRY_LOAD_DEBUG_CONTROLS;
2767
2768 /* cpu-based controls */
2769 rdmsr(MSR_IA32_VMX_PROCBASED_CTLS,
2770 vmx->nested.nested_vmx_procbased_ctls_low,
2771 vmx->nested.nested_vmx_procbased_ctls_high);
2772 vmx->nested.nested_vmx_procbased_ctls_low =
2773 CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
2774 vmx->nested.nested_vmx_procbased_ctls_high &=
2775 CPU_BASED_VIRTUAL_INTR_PENDING |
2776 CPU_BASED_VIRTUAL_NMI_PENDING | CPU_BASED_USE_TSC_OFFSETING |
2777 CPU_BASED_HLT_EXITING | CPU_BASED_INVLPG_EXITING |
2778 CPU_BASED_MWAIT_EXITING | CPU_BASED_CR3_LOAD_EXITING |
2779 CPU_BASED_CR3_STORE_EXITING |
2780 #ifdef CONFIG_X86_64
2781 CPU_BASED_CR8_LOAD_EXITING | CPU_BASED_CR8_STORE_EXITING |
2782 #endif
2783 CPU_BASED_MOV_DR_EXITING | CPU_BASED_UNCOND_IO_EXITING |
2784 CPU_BASED_USE_IO_BITMAPS | CPU_BASED_MONITOR_TRAP_FLAG |
2785 CPU_BASED_MONITOR_EXITING | CPU_BASED_RDPMC_EXITING |
2786 CPU_BASED_RDTSC_EXITING | CPU_BASED_PAUSE_EXITING |
2787 CPU_BASED_TPR_SHADOW | CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
2788 /*
2789 * We can allow some features even when not supported by the
2790 * hardware. For example, L1 can specify an MSR bitmap - and we
2791 * can use it to avoid exits to L1 - even when L0 runs L2
2792 * without MSR bitmaps.
2793 */
2794 vmx->nested.nested_vmx_procbased_ctls_high |=
2795 CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR |
2796 CPU_BASED_USE_MSR_BITMAPS;
2797
2798 /* We support free control of CR3 access interception. */
2799 vmx->nested.nested_vmx_true_procbased_ctls_low =
2800 vmx->nested.nested_vmx_procbased_ctls_low &
2801 ~(CPU_BASED_CR3_LOAD_EXITING | CPU_BASED_CR3_STORE_EXITING);
2802
2803 /* secondary cpu-based controls */
2804 rdmsr(MSR_IA32_VMX_PROCBASED_CTLS2,
2805 vmx->nested.nested_vmx_secondary_ctls_low,
2806 vmx->nested.nested_vmx_secondary_ctls_high);
2807 vmx->nested.nested_vmx_secondary_ctls_low = 0;
2808 vmx->nested.nested_vmx_secondary_ctls_high &=
2809 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
2810 SECONDARY_EXEC_RDTSCP |
2811 SECONDARY_EXEC_DESC |
2812 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
2813 SECONDARY_EXEC_ENABLE_VPID |
2814 SECONDARY_EXEC_APIC_REGISTER_VIRT |
2815 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY |
2816 SECONDARY_EXEC_WBINVD_EXITING |
2817 SECONDARY_EXEC_XSAVES;
2818
2819 if (enable_ept) {
2820 /* nested EPT: emulate EPT also to L1 */
2821 vmx->nested.nested_vmx_secondary_ctls_high |=
2822 SECONDARY_EXEC_ENABLE_EPT;
2823 vmx->nested.nested_vmx_ept_caps = VMX_EPT_PAGE_WALK_4_BIT |
2824 VMX_EPTP_WB_BIT | VMX_EPT_2MB_PAGE_BIT |
2825 VMX_EPT_INVEPT_BIT;
2826 if (cpu_has_vmx_ept_execute_only())
2827 vmx->nested.nested_vmx_ept_caps |=
2828 VMX_EPT_EXECUTE_ONLY_BIT;
2829 vmx->nested.nested_vmx_ept_caps &= vmx_capability.ept;
2830 vmx->nested.nested_vmx_ept_caps |= VMX_EPT_EXTENT_GLOBAL_BIT |
2831 VMX_EPT_EXTENT_CONTEXT_BIT;
2832 } else
2833 vmx->nested.nested_vmx_ept_caps = 0;
2834
2835 /*
2836 * Old versions of KVM use the single-context version without
2837 * checking for support, so declare that it is supported even
2838 * though it is treated as global context. The alternative is
2839 * not failing the single-context invvpid, and it is worse.
2840 */
2841 if (enable_vpid)
2842 vmx->nested.nested_vmx_vpid_caps = VMX_VPID_INVVPID_BIT |
2843 VMX_VPID_EXTENT_SUPPORTED_MASK;
2844 else
2845 vmx->nested.nested_vmx_vpid_caps = 0;
2846
2847 if (enable_unrestricted_guest)
2848 vmx->nested.nested_vmx_secondary_ctls_high |=
2849 SECONDARY_EXEC_UNRESTRICTED_GUEST;
2850
2851 /* miscellaneous data */
2852 rdmsr(MSR_IA32_VMX_MISC,
2853 vmx->nested.nested_vmx_misc_low,
2854 vmx->nested.nested_vmx_misc_high);
2855 vmx->nested.nested_vmx_misc_low &= VMX_MISC_SAVE_EFER_LMA;
2856 vmx->nested.nested_vmx_misc_low |=
2857 VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE |
2858 VMX_MISC_ACTIVITY_HLT;
2859 vmx->nested.nested_vmx_misc_high = 0;
2860 }
2861
2862 static inline bool vmx_control_verify(u32 control, u32 low, u32 high)
2863 {
2864 /*
2865 * Bits 0 in high must be 0, and bits 1 in low must be 1.
2866 */
2867 return ((control & high) | low) == control;
2868 }
2869
2870 static inline u64 vmx_control_msr(u32 low, u32 high)
2871 {
2872 return low | ((u64)high << 32);
2873 }
2874
2875 /* Returns 0 on success, non-0 otherwise. */
2876 static int vmx_get_vmx_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 *pdata)
2877 {
2878 struct vcpu_vmx *vmx = to_vmx(vcpu);
2879
2880 switch (msr_index) {
2881 case MSR_IA32_VMX_BASIC:
2882 /*
2883 * This MSR reports some information about VMX support. We
2884 * should return information about the VMX we emulate for the
2885 * guest, and the VMCS structure we give it - not about the
2886 * VMX support of the underlying hardware.
2887 */
2888 *pdata = VMCS12_REVISION | VMX_BASIC_TRUE_CTLS |
2889 ((u64)VMCS12_SIZE << VMX_BASIC_VMCS_SIZE_SHIFT) |
2890 (VMX_BASIC_MEM_TYPE_WB << VMX_BASIC_MEM_TYPE_SHIFT);
2891 if (cpu_has_vmx_basic_inout())
2892 *pdata |= VMX_BASIC_INOUT;
2893 break;
2894 case MSR_IA32_VMX_TRUE_PINBASED_CTLS:
2895 case MSR_IA32_VMX_PINBASED_CTLS:
2896 *pdata = vmx_control_msr(
2897 vmx->nested.nested_vmx_pinbased_ctls_low,
2898 vmx->nested.nested_vmx_pinbased_ctls_high);
2899 break;
2900 case MSR_IA32_VMX_TRUE_PROCBASED_CTLS:
2901 *pdata = vmx_control_msr(
2902 vmx->nested.nested_vmx_true_procbased_ctls_low,
2903 vmx->nested.nested_vmx_procbased_ctls_high);
2904 break;
2905 case MSR_IA32_VMX_PROCBASED_CTLS:
2906 *pdata = vmx_control_msr(
2907 vmx->nested.nested_vmx_procbased_ctls_low,
2908 vmx->nested.nested_vmx_procbased_ctls_high);
2909 break;
2910 case MSR_IA32_VMX_TRUE_EXIT_CTLS:
2911 *pdata = vmx_control_msr(
2912 vmx->nested.nested_vmx_true_exit_ctls_low,
2913 vmx->nested.nested_vmx_exit_ctls_high);
2914 break;
2915 case MSR_IA32_VMX_EXIT_CTLS:
2916 *pdata = vmx_control_msr(
2917 vmx->nested.nested_vmx_exit_ctls_low,
2918 vmx->nested.nested_vmx_exit_ctls_high);
2919 break;
2920 case MSR_IA32_VMX_TRUE_ENTRY_CTLS:
2921 *pdata = vmx_control_msr(
2922 vmx->nested.nested_vmx_true_entry_ctls_low,
2923 vmx->nested.nested_vmx_entry_ctls_high);
2924 break;
2925 case MSR_IA32_VMX_ENTRY_CTLS:
2926 *pdata = vmx_control_msr(
2927 vmx->nested.nested_vmx_entry_ctls_low,
2928 vmx->nested.nested_vmx_entry_ctls_high);
2929 break;
2930 case MSR_IA32_VMX_MISC:
2931 *pdata = vmx_control_msr(
2932 vmx->nested.nested_vmx_misc_low,
2933 vmx->nested.nested_vmx_misc_high);
2934 break;
2935 /*
2936 * These MSRs specify bits which the guest must keep fixed (on or off)
2937 * while L1 is in VMXON mode (in L1's root mode, or running an L2).
2938 * We picked the standard core2 setting.
2939 */
2940 #define VMXON_CR0_ALWAYSON (X86_CR0_PE | X86_CR0_PG | X86_CR0_NE)
2941 #define VMXON_CR4_ALWAYSON X86_CR4_VMXE
2942 case MSR_IA32_VMX_CR0_FIXED0:
2943 *pdata = VMXON_CR0_ALWAYSON;
2944 break;
2945 case MSR_IA32_VMX_CR0_FIXED1:
2946 *pdata = -1ULL;
2947 break;
2948 case MSR_IA32_VMX_CR4_FIXED0:
2949 *pdata = VMXON_CR4_ALWAYSON;
2950 break;
2951 case MSR_IA32_VMX_CR4_FIXED1:
2952 *pdata = -1ULL;
2953 break;
2954 case MSR_IA32_VMX_VMCS_ENUM:
2955 *pdata = 0x2e; /* highest index: VMX_PREEMPTION_TIMER_VALUE */
2956 break;
2957 case MSR_IA32_VMX_PROCBASED_CTLS2:
2958 *pdata = vmx_control_msr(
2959 vmx->nested.nested_vmx_secondary_ctls_low,
2960 vmx->nested.nested_vmx_secondary_ctls_high);
2961 break;
2962 case MSR_IA32_VMX_EPT_VPID_CAP:
2963 *pdata = vmx->nested.nested_vmx_ept_caps |
2964 ((u64)vmx->nested.nested_vmx_vpid_caps << 32);
2965 break;
2966 default:
2967 return 1;
2968 }
2969
2970 return 0;
2971 }
2972
2973 static inline bool vmx_feature_control_msr_valid(struct kvm_vcpu *vcpu,
2974 uint64_t val)
2975 {
2976 uint64_t valid_bits = to_vmx(vcpu)->msr_ia32_feature_control_valid_bits;
2977
2978 return !(val & ~valid_bits);
2979 }
2980
2981 /*
2982 * Reads an msr value (of 'msr_index') into 'pdata'.
2983 * Returns 0 on success, non-0 otherwise.
2984 * Assumes vcpu_load() was already called.
2985 */
2986 static int vmx_get_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
2987 {
2988 struct shared_msr_entry *msr;
2989
2990 switch (msr_info->index) {
2991 #ifdef CONFIG_X86_64
2992 case MSR_FS_BASE:
2993 msr_info->data = vmcs_readl(GUEST_FS_BASE);
2994 break;
2995 case MSR_GS_BASE:
2996 msr_info->data = vmcs_readl(GUEST_GS_BASE);
2997 break;
2998 case MSR_KERNEL_GS_BASE:
2999 vmx_load_host_state(to_vmx(vcpu));
3000 msr_info->data = to_vmx(vcpu)->msr_guest_kernel_gs_base;
3001 break;
3002 #endif
3003 case MSR_EFER:
3004 return kvm_get_msr_common(vcpu, msr_info);
3005 case MSR_IA32_TSC:
3006 msr_info->data = guest_read_tsc(vcpu);
3007 break;
3008 case MSR_IA32_SYSENTER_CS:
3009 msr_info->data = vmcs_read32(GUEST_SYSENTER_CS);
3010 break;
3011 case MSR_IA32_SYSENTER_EIP:
3012 msr_info->data = vmcs_readl(GUEST_SYSENTER_EIP);
3013 break;
3014 case MSR_IA32_SYSENTER_ESP:
3015 msr_info->data = vmcs_readl(GUEST_SYSENTER_ESP);
3016 break;
3017 case MSR_IA32_BNDCFGS:
3018 if (!kvm_mpx_supported())
3019 return 1;
3020 msr_info->data = vmcs_read64(GUEST_BNDCFGS);
3021 break;
3022 case MSR_IA32_MCG_EXT_CTL:
3023 if (!msr_info->host_initiated &&
3024 !(to_vmx(vcpu)->msr_ia32_feature_control &
3025 FEATURE_CONTROL_LMCE))
3026 return 1;
3027 msr_info->data = vcpu->arch.mcg_ext_ctl;
3028 break;
3029 case MSR_IA32_FEATURE_CONTROL:
3030 msr_info->data = to_vmx(vcpu)->msr_ia32_feature_control;
3031 break;
3032 case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC:
3033 if (!nested_vmx_allowed(vcpu))
3034 return 1;
3035 return vmx_get_vmx_msr(vcpu, msr_info->index, &msr_info->data);
3036 case MSR_IA32_XSS:
3037 if (!vmx_xsaves_supported())
3038 return 1;
3039 msr_info->data = vcpu->arch.ia32_xss;
3040 break;
3041 case MSR_TSC_AUX:
3042 if (!guest_cpuid_has_rdtscp(vcpu) && !msr_info->host_initiated)
3043 return 1;
3044 /* Otherwise falls through */
3045 default:
3046 msr = find_msr_entry(to_vmx(vcpu), msr_info->index);
3047 if (msr) {
3048 msr_info->data = msr->data;
3049 break;
3050 }
3051 return kvm_get_msr_common(vcpu, msr_info);
3052 }
3053
3054 return 0;
3055 }
3056
3057 static void vmx_leave_nested(struct kvm_vcpu *vcpu);
3058
3059 /*
3060 * Writes msr value into into the appropriate "register".
3061 * Returns 0 on success, non-0 otherwise.
3062 * Assumes vcpu_load() was already called.
3063 */
3064 static int vmx_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
3065 {
3066 struct vcpu_vmx *vmx = to_vmx(vcpu);
3067 struct shared_msr_entry *msr;
3068 int ret = 0;
3069 u32 msr_index = msr_info->index;
3070 u64 data = msr_info->data;
3071
3072 switch (msr_index) {
3073 case MSR_EFER:
3074 ret = kvm_set_msr_common(vcpu, msr_info);
3075 break;
3076 #ifdef CONFIG_X86_64
3077 case MSR_FS_BASE:
3078 vmx_segment_cache_clear(vmx);
3079 vmcs_writel(GUEST_FS_BASE, data);
3080 break;
3081 case MSR_GS_BASE:
3082 vmx_segment_cache_clear(vmx);
3083 vmcs_writel(GUEST_GS_BASE, data);
3084 break;
3085 case MSR_KERNEL_GS_BASE:
3086 vmx_load_host_state(vmx);
3087 vmx->msr_guest_kernel_gs_base = data;
3088 break;
3089 #endif
3090 case MSR_IA32_SYSENTER_CS:
3091 vmcs_write32(GUEST_SYSENTER_CS, data);
3092 break;
3093 case MSR_IA32_SYSENTER_EIP:
3094 vmcs_writel(GUEST_SYSENTER_EIP, data);
3095 break;
3096 case MSR_IA32_SYSENTER_ESP:
3097 vmcs_writel(GUEST_SYSENTER_ESP, data);
3098 break;
3099 case MSR_IA32_BNDCFGS:
3100 if (!kvm_mpx_supported())
3101 return 1;
3102 vmcs_write64(GUEST_BNDCFGS, data);
3103 break;
3104 case MSR_IA32_TSC:
3105 kvm_write_tsc(vcpu, msr_info);
3106 break;
3107 case MSR_IA32_CR_PAT:
3108 if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) {
3109 if (!kvm_mtrr_valid(vcpu, MSR_IA32_CR_PAT, data))
3110 return 1;
3111 vmcs_write64(GUEST_IA32_PAT, data);
3112 vcpu->arch.pat = data;
3113 break;
3114 }
3115 ret = kvm_set_msr_common(vcpu, msr_info);
3116 break;
3117 case MSR_IA32_TSC_ADJUST:
3118 ret = kvm_set_msr_common(vcpu, msr_info);
3119 break;
3120 case MSR_IA32_MCG_EXT_CTL:
3121 if ((!msr_info->host_initiated &&
3122 !(to_vmx(vcpu)->msr_ia32_feature_control &
3123 FEATURE_CONTROL_LMCE)) ||
3124 (data & ~MCG_EXT_CTL_LMCE_EN))
3125 return 1;
3126 vcpu->arch.mcg_ext_ctl = data;
3127 break;
3128 case MSR_IA32_FEATURE_CONTROL:
3129 if (!vmx_feature_control_msr_valid(vcpu, data) ||
3130 (to_vmx(vcpu)->msr_ia32_feature_control &
3131 FEATURE_CONTROL_LOCKED && !msr_info->host_initiated))
3132 return 1;
3133 vmx->msr_ia32_feature_control = data;
3134 if (msr_info->host_initiated && data == 0)
3135 vmx_leave_nested(vcpu);
3136 break;
3137 case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC:
3138 return 1; /* they are read-only */
3139 case MSR_IA32_XSS:
3140 if (!vmx_xsaves_supported())
3141 return 1;
3142 /*
3143 * The only supported bit as of Skylake is bit 8, but
3144 * it is not supported on KVM.
3145 */
3146 if (data != 0)
3147 return 1;
3148 vcpu->arch.ia32_xss = data;
3149 if (vcpu->arch.ia32_xss != host_xss)
3150 add_atomic_switch_msr(vmx, MSR_IA32_XSS,
3151 vcpu->arch.ia32_xss, host_xss);
3152 else
3153 clear_atomic_switch_msr(vmx, MSR_IA32_XSS);
3154 break;
3155 case MSR_TSC_AUX:
3156 if (!guest_cpuid_has_rdtscp(vcpu) && !msr_info->host_initiated)
3157 return 1;
3158 /* Check reserved bit, higher 32 bits should be zero */
3159 if ((data >> 32) != 0)
3160 return 1;
3161 /* Otherwise falls through */
3162 default:
3163 msr = find_msr_entry(vmx, msr_index);
3164 if (msr) {
3165 u64 old_msr_data = msr->data;
3166 msr->data = data;
3167 if (msr - vmx->guest_msrs < vmx->save_nmsrs) {
3168 preempt_disable();
3169 ret = kvm_set_shared_msr(msr->index, msr->data,
3170 msr->mask);
3171 preempt_enable();
3172 if (ret)
3173 msr->data = old_msr_data;
3174 }
3175 break;
3176 }
3177 ret = kvm_set_msr_common(vcpu, msr_info);
3178 }
3179
3180 return ret;
3181 }
3182
3183 static void vmx_cache_reg(struct kvm_vcpu *vcpu, enum kvm_reg reg)
3184 {
3185 __set_bit(reg, (unsigned long *)&vcpu->arch.regs_avail);
3186 switch (reg) {
3187 case VCPU_REGS_RSP:
3188 vcpu->arch.regs[VCPU_REGS_RSP] = vmcs_readl(GUEST_RSP);
3189 break;
3190 case VCPU_REGS_RIP:
3191 vcpu->arch.regs[VCPU_REGS_RIP] = vmcs_readl(GUEST_RIP);
3192 break;
3193 case VCPU_EXREG_PDPTR:
3194 if (enable_ept)
3195 ept_save_pdptrs(vcpu);
3196 break;
3197 default:
3198 break;
3199 }
3200 }
3201
3202 static __init int cpu_has_kvm_support(void)
3203 {
3204 return cpu_has_vmx();
3205 }
3206
3207 static __init int vmx_disabled_by_bios(void)
3208 {
3209 u64 msr;
3210
3211 rdmsrl(MSR_IA32_FEATURE_CONTROL, msr);
3212 if (msr & FEATURE_CONTROL_LOCKED) {
3213 /* launched w/ TXT and VMX disabled */
3214 if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX)
3215 && tboot_enabled())
3216 return 1;
3217 /* launched w/o TXT and VMX only enabled w/ TXT */
3218 if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX)
3219 && (msr & FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX)
3220 && !tboot_enabled()) {
3221 printk(KERN_WARNING "kvm: disable TXT in the BIOS or "
3222 "activate TXT before enabling KVM\n");
3223 return 1;
3224 }
3225 /* launched w/o TXT and VMX disabled */
3226 if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX)
3227 && !tboot_enabled())
3228 return 1;
3229 }
3230
3231 return 0;
3232 }
3233
3234 static void kvm_cpu_vmxon(u64 addr)
3235 {
3236 intel_pt_handle_vmx(1);
3237
3238 asm volatile (ASM_VMX_VMXON_RAX
3239 : : "a"(&addr), "m"(addr)
3240 : "memory", "cc");
3241 }
3242
3243 static int hardware_enable(void)
3244 {
3245 int cpu = raw_smp_processor_id();
3246 u64 phys_addr = __pa(per_cpu(vmxarea, cpu));
3247 u64 old, test_bits;
3248
3249 if (cr4_read_shadow() & X86_CR4_VMXE)
3250 return -EBUSY;
3251
3252 INIT_LIST_HEAD(&per_cpu(loaded_vmcss_on_cpu, cpu));
3253 INIT_LIST_HEAD(&per_cpu(blocked_vcpu_on_cpu, cpu));
3254 spin_lock_init(&per_cpu(blocked_vcpu_on_cpu_lock, cpu));
3255
3256 /*
3257 * Now we can enable the vmclear operation in kdump
3258 * since the loaded_vmcss_on_cpu list on this cpu
3259 * has been initialized.
3260 *
3261 * Though the cpu is not in VMX operation now, there
3262 * is no problem to enable the vmclear operation
3263 * for the loaded_vmcss_on_cpu list is empty!
3264 */
3265 crash_enable_local_vmclear(cpu);
3266
3267 rdmsrl(MSR_IA32_FEATURE_CONTROL, old);
3268
3269 test_bits = FEATURE_CONTROL_LOCKED;
3270 test_bits |= FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
3271 if (tboot_enabled())
3272 test_bits |= FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX;
3273
3274 if ((old & test_bits) != test_bits) {
3275 /* enable and lock */
3276 wrmsrl(MSR_IA32_FEATURE_CONTROL, old | test_bits);
3277 }
3278 cr4_set_bits(X86_CR4_VMXE);
3279
3280 if (vmm_exclusive) {
3281 kvm_cpu_vmxon(phys_addr);
3282 ept_sync_global();
3283 }
3284
3285 native_store_gdt(this_cpu_ptr(&host_gdt));
3286
3287 return 0;
3288 }
3289
3290 static void vmclear_local_loaded_vmcss(void)
3291 {
3292 int cpu = raw_smp_processor_id();
3293 struct loaded_vmcs *v, *n;
3294
3295 list_for_each_entry_safe(v, n, &per_cpu(loaded_vmcss_on_cpu, cpu),
3296 loaded_vmcss_on_cpu_link)
3297 __loaded_vmcs_clear(v);
3298 }
3299
3300
3301 /* Just like cpu_vmxoff(), but with the __kvm_handle_fault_on_reboot()
3302 * tricks.
3303 */
3304 static void kvm_cpu_vmxoff(void)
3305 {
3306 asm volatile (__ex(ASM_VMX_VMXOFF) : : : "cc");
3307
3308 intel_pt_handle_vmx(0);
3309 }
3310
3311 static void hardware_disable(void)
3312 {
3313 if (vmm_exclusive) {
3314 vmclear_local_loaded_vmcss();
3315 kvm_cpu_vmxoff();
3316 }
3317 cr4_clear_bits(X86_CR4_VMXE);
3318 }
3319
3320 static __init int adjust_vmx_controls(u32 ctl_min, u32 ctl_opt,
3321 u32 msr, u32 *result)
3322 {
3323 u32 vmx_msr_low, vmx_msr_high;
3324 u32 ctl = ctl_min | ctl_opt;
3325
3326 rdmsr(msr, vmx_msr_low, vmx_msr_high);
3327
3328 ctl &= vmx_msr_high; /* bit == 0 in high word ==> must be zero */
3329 ctl |= vmx_msr_low; /* bit == 1 in low word ==> must be one */
3330
3331 /* Ensure minimum (required) set of control bits are supported. */
3332 if (ctl_min & ~ctl)
3333 return -EIO;
3334
3335 *result = ctl;
3336 return 0;
3337 }
3338
3339 static __init bool allow_1_setting(u32 msr, u32 ctl)
3340 {
3341 u32 vmx_msr_low, vmx_msr_high;
3342
3343 rdmsr(msr, vmx_msr_low, vmx_msr_high);
3344 return vmx_msr_high & ctl;
3345 }
3346
3347 static __init int setup_vmcs_config(struct vmcs_config *vmcs_conf)
3348 {
3349 u32 vmx_msr_low, vmx_msr_high;
3350 u32 min, opt, min2, opt2;
3351 u32 _pin_based_exec_control = 0;
3352 u32 _cpu_based_exec_control = 0;
3353 u32 _cpu_based_2nd_exec_control = 0;
3354 u32 _vmexit_control = 0;
3355 u32 _vmentry_control = 0;
3356
3357 min = CPU_BASED_HLT_EXITING |
3358 #ifdef CONFIG_X86_64
3359 CPU_BASED_CR8_LOAD_EXITING |
3360 CPU_BASED_CR8_STORE_EXITING |
3361 #endif
3362 CPU_BASED_CR3_LOAD_EXITING |
3363 CPU_BASED_CR3_STORE_EXITING |
3364 CPU_BASED_USE_IO_BITMAPS |
3365 CPU_BASED_MOV_DR_EXITING |
3366 CPU_BASED_USE_TSC_OFFSETING |
3367 CPU_BASED_MWAIT_EXITING |
3368 CPU_BASED_MONITOR_EXITING |
3369 CPU_BASED_INVLPG_EXITING |
3370 CPU_BASED_RDPMC_EXITING;
3371
3372 opt = CPU_BASED_TPR_SHADOW |
3373 CPU_BASED_USE_MSR_BITMAPS |
3374 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
3375 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PROCBASED_CTLS,
3376 &_cpu_based_exec_control) < 0)
3377 return -EIO;
3378 #ifdef CONFIG_X86_64
3379 if ((_cpu_based_exec_control & CPU_BASED_TPR_SHADOW))
3380 _cpu_based_exec_control &= ~CPU_BASED_CR8_LOAD_EXITING &
3381 ~CPU_BASED_CR8_STORE_EXITING;
3382 #endif
3383 if (_cpu_based_exec_control & CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) {
3384 min2 = 0;
3385 opt2 = SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
3386 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
3387 SECONDARY_EXEC_WBINVD_EXITING |
3388 SECONDARY_EXEC_ENABLE_VPID |
3389 SECONDARY_EXEC_ENABLE_EPT |
3390 SECONDARY_EXEC_UNRESTRICTED_GUEST |
3391 SECONDARY_EXEC_PAUSE_LOOP_EXITING |
3392 SECONDARY_EXEC_RDTSCP |
3393 SECONDARY_EXEC_ENABLE_INVPCID |
3394 SECONDARY_EXEC_APIC_REGISTER_VIRT |
3395 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY |
3396 SECONDARY_EXEC_SHADOW_VMCS |
3397 SECONDARY_EXEC_XSAVES |
3398 SECONDARY_EXEC_ENABLE_PML |
3399 SECONDARY_EXEC_TSC_SCALING;
3400 if (adjust_vmx_controls(min2, opt2,
3401 MSR_IA32_VMX_PROCBASED_CTLS2,
3402 &_cpu_based_2nd_exec_control) < 0)
3403 return -EIO;
3404 }
3405 #ifndef CONFIG_X86_64
3406 if (!(_cpu_based_2nd_exec_control &
3407 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES))
3408 _cpu_based_exec_control &= ~CPU_BASED_TPR_SHADOW;
3409 #endif
3410
3411 if (!(_cpu_based_exec_control & CPU_BASED_TPR_SHADOW))
3412 _cpu_based_2nd_exec_control &= ~(
3413 SECONDARY_EXEC_APIC_REGISTER_VIRT |
3414 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
3415 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
3416
3417 if (_cpu_based_2nd_exec_control & SECONDARY_EXEC_ENABLE_EPT) {
3418 /* CR3 accesses and invlpg don't need to cause VM Exits when EPT
3419 enabled */
3420 _cpu_based_exec_control &= ~(CPU_BASED_CR3_LOAD_EXITING |
3421 CPU_BASED_CR3_STORE_EXITING |
3422 CPU_BASED_INVLPG_EXITING);
3423 rdmsr(MSR_IA32_VMX_EPT_VPID_CAP,
3424 vmx_capability.ept, vmx_capability.vpid);
3425 }
3426
3427 min = VM_EXIT_SAVE_DEBUG_CONTROLS | VM_EXIT_ACK_INTR_ON_EXIT;
3428 #ifdef CONFIG_X86_64
3429 min |= VM_EXIT_HOST_ADDR_SPACE_SIZE;
3430 #endif
3431 opt = VM_EXIT_SAVE_IA32_PAT | VM_EXIT_LOAD_IA32_PAT |
3432 VM_EXIT_CLEAR_BNDCFGS;
3433 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_EXIT_CTLS,
3434 &_vmexit_control) < 0)
3435 return -EIO;
3436
3437 min = PIN_BASED_EXT_INTR_MASK | PIN_BASED_NMI_EXITING;
3438 opt = PIN_BASED_VIRTUAL_NMIS | PIN_BASED_POSTED_INTR |
3439 PIN_BASED_VMX_PREEMPTION_TIMER;
3440 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PINBASED_CTLS,
3441 &_pin_based_exec_control) < 0)
3442 return -EIO;
3443
3444 if (cpu_has_broken_vmx_preemption_timer())
3445 _pin_based_exec_control &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
3446 if (!(_cpu_based_2nd_exec_control &
3447 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY))
3448 _pin_based_exec_control &= ~PIN_BASED_POSTED_INTR;
3449
3450 min = VM_ENTRY_LOAD_DEBUG_CONTROLS;
3451 opt = VM_ENTRY_LOAD_IA32_PAT | VM_ENTRY_LOAD_BNDCFGS;
3452 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_ENTRY_CTLS,
3453 &_vmentry_control) < 0)
3454 return -EIO;
3455
3456 rdmsr(MSR_IA32_VMX_BASIC, vmx_msr_low, vmx_msr_high);
3457
3458 /* IA-32 SDM Vol 3B: VMCS size is never greater than 4kB. */
3459 if ((vmx_msr_high & 0x1fff) > PAGE_SIZE)
3460 return -EIO;
3461
3462 #ifdef CONFIG_X86_64
3463 /* IA-32 SDM Vol 3B: 64-bit CPUs always have VMX_BASIC_MSR[48]==0. */
3464 if (vmx_msr_high & (1u<<16))
3465 return -EIO;
3466 #endif
3467
3468 /* Require Write-Back (WB) memory type for VMCS accesses. */
3469 if (((vmx_msr_high >> 18) & 15) != 6)
3470 return -EIO;
3471
3472 vmcs_conf->size = vmx_msr_high & 0x1fff;
3473 vmcs_conf->order = get_order(vmcs_conf->size);
3474 vmcs_conf->basic_cap = vmx_msr_high & ~0x1fff;
3475 vmcs_conf->revision_id = vmx_msr_low;
3476
3477 vmcs_conf->pin_based_exec_ctrl = _pin_based_exec_control;
3478 vmcs_conf->cpu_based_exec_ctrl = _cpu_based_exec_control;
3479 vmcs_conf->cpu_based_2nd_exec_ctrl = _cpu_based_2nd_exec_control;
3480 vmcs_conf->vmexit_ctrl = _vmexit_control;
3481 vmcs_conf->vmentry_ctrl = _vmentry_control;
3482
3483 cpu_has_load_ia32_efer =
3484 allow_1_setting(MSR_IA32_VMX_ENTRY_CTLS,
3485 VM_ENTRY_LOAD_IA32_EFER)
3486 && allow_1_setting(MSR_IA32_VMX_EXIT_CTLS,
3487 VM_EXIT_LOAD_IA32_EFER);
3488
3489 cpu_has_load_perf_global_ctrl =
3490 allow_1_setting(MSR_IA32_VMX_ENTRY_CTLS,
3491 VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL)
3492 && allow_1_setting(MSR_IA32_VMX_EXIT_CTLS,
3493 VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL);
3494
3495 /*
3496 * Some cpus support VM_ENTRY_(LOAD|SAVE)_IA32_PERF_GLOBAL_CTRL
3497 * but due to errata below it can't be used. Workaround is to use
3498 * msr load mechanism to switch IA32_PERF_GLOBAL_CTRL.
3499 *
3500 * VM Exit May Incorrectly Clear IA32_PERF_GLOBAL_CTRL [34:32]
3501 *
3502 * AAK155 (model 26)
3503 * AAP115 (model 30)
3504 * AAT100 (model 37)
3505 * BC86,AAY89,BD102 (model 44)
3506 * BA97 (model 46)
3507 *
3508 */
3509 if (cpu_has_load_perf_global_ctrl && boot_cpu_data.x86 == 0x6) {
3510 switch (boot_cpu_data.x86_model) {
3511 case 26:
3512 case 30:
3513 case 37:
3514 case 44:
3515 case 46:
3516 cpu_has_load_perf_global_ctrl = false;
3517 printk_once(KERN_WARNING"kvm: VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL "
3518 "does not work properly. Using workaround\n");
3519 break;
3520 default:
3521 break;
3522 }
3523 }
3524
3525 if (boot_cpu_has(X86_FEATURE_XSAVES))
3526 rdmsrl(MSR_IA32_XSS, host_xss);
3527
3528 return 0;
3529 }
3530
3531 static struct vmcs *alloc_vmcs_cpu(int cpu)
3532 {
3533 int node = cpu_to_node(cpu);
3534 struct page *pages;
3535 struct vmcs *vmcs;
3536
3537 pages = __alloc_pages_node(node, GFP_KERNEL, vmcs_config.order);
3538 if (!pages)
3539 return NULL;
3540 vmcs = page_address(pages);
3541 memset(vmcs, 0, vmcs_config.size);
3542 vmcs->revision_id = vmcs_config.revision_id; /* vmcs revision id */
3543 return vmcs;
3544 }
3545
3546 static struct vmcs *alloc_vmcs(void)
3547 {
3548 return alloc_vmcs_cpu(raw_smp_processor_id());
3549 }
3550
3551 static void free_vmcs(struct vmcs *vmcs)
3552 {
3553 free_pages((unsigned long)vmcs, vmcs_config.order);
3554 }
3555
3556 /*
3557 * Free a VMCS, but before that VMCLEAR it on the CPU where it was last loaded
3558 */
3559 static void free_loaded_vmcs(struct loaded_vmcs *loaded_vmcs)
3560 {
3561 if (!loaded_vmcs->vmcs)
3562 return;
3563 loaded_vmcs_clear(loaded_vmcs);
3564 free_vmcs(loaded_vmcs->vmcs);
3565 loaded_vmcs->vmcs = NULL;
3566 WARN_ON(loaded_vmcs->shadow_vmcs != NULL);
3567 }
3568
3569 static void free_kvm_area(void)
3570 {
3571 int cpu;
3572
3573 for_each_possible_cpu(cpu) {
3574 free_vmcs(per_cpu(vmxarea, cpu));
3575 per_cpu(vmxarea, cpu) = NULL;
3576 }
3577 }
3578
3579 static void init_vmcs_shadow_fields(void)
3580 {
3581 int i, j;
3582
3583 /* No checks for read only fields yet */
3584
3585 for (i = j = 0; i < max_shadow_read_write_fields; i++) {
3586 switch (shadow_read_write_fields[i]) {
3587 case GUEST_BNDCFGS:
3588 if (!kvm_mpx_supported())
3589 continue;
3590 break;
3591 default:
3592 break;
3593 }
3594
3595 if (j < i)
3596 shadow_read_write_fields[j] =
3597 shadow_read_write_fields[i];
3598 j++;
3599 }
3600 max_shadow_read_write_fields = j;
3601
3602 /* shadowed fields guest access without vmexit */
3603 for (i = 0; i < max_shadow_read_write_fields; i++) {
3604 clear_bit(shadow_read_write_fields[i],
3605 vmx_vmwrite_bitmap);
3606 clear_bit(shadow_read_write_fields[i],
3607 vmx_vmread_bitmap);
3608 }
3609 for (i = 0; i < max_shadow_read_only_fields; i++)
3610 clear_bit(shadow_read_only_fields[i],
3611 vmx_vmread_bitmap);
3612 }
3613
3614 static __init int alloc_kvm_area(void)
3615 {
3616 int cpu;
3617
3618 for_each_possible_cpu(cpu) {
3619 struct vmcs *vmcs;
3620
3621 vmcs = alloc_vmcs_cpu(cpu);
3622 if (!vmcs) {
3623 free_kvm_area();
3624 return -ENOMEM;
3625 }
3626
3627 per_cpu(vmxarea, cpu) = vmcs;
3628 }
3629 return 0;
3630 }
3631
3632 static bool emulation_required(struct kvm_vcpu *vcpu)
3633 {
3634 return emulate_invalid_guest_state && !guest_state_valid(vcpu);
3635 }
3636
3637 static void fix_pmode_seg(struct kvm_vcpu *vcpu, int seg,
3638 struct kvm_segment *save)
3639 {
3640 if (!emulate_invalid_guest_state) {
3641 /*
3642 * CS and SS RPL should be equal during guest entry according
3643 * to VMX spec, but in reality it is not always so. Since vcpu
3644 * is in the middle of the transition from real mode to
3645 * protected mode it is safe to assume that RPL 0 is a good
3646 * default value.
3647 */
3648 if (seg == VCPU_SREG_CS || seg == VCPU_SREG_SS)
3649 save->selector &= ~SEGMENT_RPL_MASK;
3650 save->dpl = save->selector & SEGMENT_RPL_MASK;
3651 save->s = 1;
3652 }
3653 vmx_set_segment(vcpu, save, seg);
3654 }
3655
3656 static void enter_pmode(struct kvm_vcpu *vcpu)
3657 {
3658 unsigned long flags;
3659 struct vcpu_vmx *vmx = to_vmx(vcpu);
3660
3661 /*
3662 * Update real mode segment cache. It may be not up-to-date if sement
3663 * register was written while vcpu was in a guest mode.
3664 */
3665 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_ES], VCPU_SREG_ES);
3666 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_DS], VCPU_SREG_DS);
3667 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_FS], VCPU_SREG_FS);
3668 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_GS], VCPU_SREG_GS);
3669 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_SS], VCPU_SREG_SS);
3670 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_CS], VCPU_SREG_CS);
3671
3672 vmx->rmode.vm86_active = 0;
3673
3674 vmx_segment_cache_clear(vmx);
3675
3676 vmx_set_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_TR], VCPU_SREG_TR);
3677
3678 flags = vmcs_readl(GUEST_RFLAGS);
3679 flags &= RMODE_GUEST_OWNED_EFLAGS_BITS;
3680 flags |= vmx->rmode.save_rflags & ~RMODE_GUEST_OWNED_EFLAGS_BITS;
3681 vmcs_writel(GUEST_RFLAGS, flags);
3682
3683 vmcs_writel(GUEST_CR4, (vmcs_readl(GUEST_CR4) & ~X86_CR4_VME) |
3684 (vmcs_readl(CR4_READ_SHADOW) & X86_CR4_VME));
3685
3686 update_exception_bitmap(vcpu);
3687
3688 fix_pmode_seg(vcpu, VCPU_SREG_CS, &vmx->rmode.segs[VCPU_SREG_CS]);
3689 fix_pmode_seg(vcpu, VCPU_SREG_SS, &vmx->rmode.segs[VCPU_SREG_SS]);
3690 fix_pmode_seg(vcpu, VCPU_SREG_ES, &vmx->rmode.segs[VCPU_SREG_ES]);
3691 fix_pmode_seg(vcpu, VCPU_SREG_DS, &vmx->rmode.segs[VCPU_SREG_DS]);
3692 fix_pmode_seg(vcpu, VCPU_SREG_FS, &vmx->rmode.segs[VCPU_SREG_FS]);
3693 fix_pmode_seg(vcpu, VCPU_SREG_GS, &vmx->rmode.segs[VCPU_SREG_GS]);
3694 }
3695
3696 static void fix_rmode_seg(int seg, struct kvm_segment *save)
3697 {
3698 const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
3699 struct kvm_segment var = *save;
3700
3701 var.dpl = 0x3;
3702 if (seg == VCPU_SREG_CS)
3703 var.type = 0x3;
3704
3705 if (!emulate_invalid_guest_state) {
3706 var.selector = var.base >> 4;
3707 var.base = var.base & 0xffff0;
3708 var.limit = 0xffff;
3709 var.g = 0;
3710 var.db = 0;
3711 var.present = 1;
3712 var.s = 1;
3713 var.l = 0;
3714 var.unusable = 0;
3715 var.type = 0x3;
3716 var.avl = 0;
3717 if (save->base & 0xf)
3718 printk_once(KERN_WARNING "kvm: segment base is not "
3719 "paragraph aligned when entering "
3720 "protected mode (seg=%d)", seg);
3721 }
3722
3723 vmcs_write16(sf->selector, var.selector);
3724 vmcs_write32(sf->base, var.base);
3725 vmcs_write32(sf->limit, var.limit);
3726 vmcs_write32(sf->ar_bytes, vmx_segment_access_rights(&var));
3727 }
3728
3729 static void enter_rmode(struct kvm_vcpu *vcpu)
3730 {
3731 unsigned long flags;
3732 struct vcpu_vmx *vmx = to_vmx(vcpu);
3733
3734 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_TR], VCPU_SREG_TR);
3735 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_ES], VCPU_SREG_ES);
3736 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_DS], VCPU_SREG_DS);
3737 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_FS], VCPU_SREG_FS);
3738 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_GS], VCPU_SREG_GS);
3739 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_SS], VCPU_SREG_SS);
3740 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_CS], VCPU_SREG_CS);
3741
3742 vmx->rmode.vm86_active = 1;
3743
3744 /*
3745 * Very old userspace does not call KVM_SET_TSS_ADDR before entering
3746 * vcpu. Warn the user that an update is overdue.
3747 */
3748 if (!vcpu->kvm->arch.tss_addr)
3749 printk_once(KERN_WARNING "kvm: KVM_SET_TSS_ADDR need to be "
3750 "called before entering vcpu\n");
3751
3752 vmx_segment_cache_clear(vmx);
3753
3754 vmcs_writel(GUEST_TR_BASE, vcpu->kvm->arch.tss_addr);
3755 vmcs_write32(GUEST_TR_LIMIT, RMODE_TSS_SIZE - 1);
3756 vmcs_write32(GUEST_TR_AR_BYTES, 0x008b);
3757
3758 flags = vmcs_readl(GUEST_RFLAGS);
3759 vmx->rmode.save_rflags = flags;
3760
3761 flags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM;
3762
3763 vmcs_writel(GUEST_RFLAGS, flags);
3764 vmcs_writel(GUEST_CR4, vmcs_readl(GUEST_CR4) | X86_CR4_VME);
3765 update_exception_bitmap(vcpu);
3766
3767 fix_rmode_seg(VCPU_SREG_SS, &vmx->rmode.segs[VCPU_SREG_SS]);
3768 fix_rmode_seg(VCPU_SREG_CS, &vmx->rmode.segs[VCPU_SREG_CS]);
3769 fix_rmode_seg(VCPU_SREG_ES, &vmx->rmode.segs[VCPU_SREG_ES]);
3770 fix_rmode_seg(VCPU_SREG_DS, &vmx->rmode.segs[VCPU_SREG_DS]);
3771 fix_rmode_seg(VCPU_SREG_GS, &vmx->rmode.segs[VCPU_SREG_GS]);
3772 fix_rmode_seg(VCPU_SREG_FS, &vmx->rmode.segs[VCPU_SREG_FS]);
3773
3774 kvm_mmu_reset_context(vcpu);
3775 }
3776
3777 static void vmx_set_efer(struct kvm_vcpu *vcpu, u64 efer)
3778 {
3779 struct vcpu_vmx *vmx = to_vmx(vcpu);
3780 struct shared_msr_entry *msr = find_msr_entry(vmx, MSR_EFER);
3781
3782 if (!msr)
3783 return;
3784
3785 /*
3786 * Force kernel_gs_base reloading before EFER changes, as control
3787 * of this msr depends on is_long_mode().
3788 */
3789 vmx_load_host_state(to_vmx(vcpu));
3790 vcpu->arch.efer = efer;
3791 if (efer & EFER_LMA) {
3792 vm_entry_controls_setbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
3793 msr->data = efer;
3794 } else {
3795 vm_entry_controls_clearbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
3796
3797 msr->data = efer & ~EFER_LME;
3798 }
3799 setup_msrs(vmx);
3800 }
3801
3802 #ifdef CONFIG_X86_64
3803
3804 static void enter_lmode(struct kvm_vcpu *vcpu)
3805 {
3806 u32 guest_tr_ar;
3807
3808 vmx_segment_cache_clear(to_vmx(vcpu));
3809
3810 guest_tr_ar = vmcs_read32(GUEST_TR_AR_BYTES);
3811 if ((guest_tr_ar & VMX_AR_TYPE_MASK) != VMX_AR_TYPE_BUSY_64_TSS) {
3812 pr_debug_ratelimited("%s: tss fixup for long mode. \n",
3813 __func__);
3814 vmcs_write32(GUEST_TR_AR_BYTES,
3815 (guest_tr_ar & ~VMX_AR_TYPE_MASK)
3816 | VMX_AR_TYPE_BUSY_64_TSS);
3817 }
3818 vmx_set_efer(vcpu, vcpu->arch.efer | EFER_LMA);
3819 }
3820
3821 static void exit_lmode(struct kvm_vcpu *vcpu)
3822 {
3823 vm_entry_controls_clearbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
3824 vmx_set_efer(vcpu, vcpu->arch.efer & ~EFER_LMA);
3825 }
3826
3827 #endif
3828
3829 static inline void __vmx_flush_tlb(struct kvm_vcpu *vcpu, int vpid)
3830 {
3831 vpid_sync_context(vpid);
3832 if (enable_ept) {
3833 if (!VALID_PAGE(vcpu->arch.mmu.root_hpa))
3834 return;
3835 ept_sync_context(construct_eptp(vcpu->arch.mmu.root_hpa));
3836 }
3837 }
3838
3839 static void vmx_flush_tlb(struct kvm_vcpu *vcpu)
3840 {
3841 __vmx_flush_tlb(vcpu, to_vmx(vcpu)->vpid);
3842 }
3843
3844 static void vmx_decache_cr0_guest_bits(struct kvm_vcpu *vcpu)
3845 {
3846 ulong cr0_guest_owned_bits = vcpu->arch.cr0_guest_owned_bits;
3847
3848 vcpu->arch.cr0 &= ~cr0_guest_owned_bits;
3849 vcpu->arch.cr0 |= vmcs_readl(GUEST_CR0) & cr0_guest_owned_bits;
3850 }
3851
3852 static void vmx_decache_cr3(struct kvm_vcpu *vcpu)
3853 {
3854 if (enable_ept && is_paging(vcpu))
3855 vcpu->arch.cr3 = vmcs_readl(GUEST_CR3);
3856 __set_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail);
3857 }
3858
3859 static void vmx_decache_cr4_guest_bits(struct kvm_vcpu *vcpu)
3860 {
3861 ulong cr4_guest_owned_bits = vcpu->arch.cr4_guest_owned_bits;
3862
3863 vcpu->arch.cr4 &= ~cr4_guest_owned_bits;
3864 vcpu->arch.cr4 |= vmcs_readl(GUEST_CR4) & cr4_guest_owned_bits;
3865 }
3866
3867 static void ept_load_pdptrs(struct kvm_vcpu *vcpu)
3868 {
3869 struct kvm_mmu *mmu = vcpu->arch.walk_mmu;
3870
3871 if (!test_bit(VCPU_EXREG_PDPTR,
3872 (unsigned long *)&vcpu->arch.regs_dirty))
3873 return;
3874
3875 if (is_paging(vcpu) && is_pae(vcpu) && !is_long_mode(vcpu)) {
3876 vmcs_write64(GUEST_PDPTR0, mmu->pdptrs[0]);
3877 vmcs_write64(GUEST_PDPTR1, mmu->pdptrs[1]);
3878 vmcs_write64(GUEST_PDPTR2, mmu->pdptrs[2]);
3879 vmcs_write64(GUEST_PDPTR3, mmu->pdptrs[3]);
3880 }
3881 }
3882
3883 static void ept_save_pdptrs(struct kvm_vcpu *vcpu)
3884 {
3885 struct kvm_mmu *mmu = vcpu->arch.walk_mmu;
3886
3887 if (is_paging(vcpu) && is_pae(vcpu) && !is_long_mode(vcpu)) {
3888 mmu->pdptrs[0] = vmcs_read64(GUEST_PDPTR0);
3889 mmu->pdptrs[1] = vmcs_read64(GUEST_PDPTR1);
3890 mmu->pdptrs[2] = vmcs_read64(GUEST_PDPTR2);
3891 mmu->pdptrs[3] = vmcs_read64(GUEST_PDPTR3);
3892 }
3893
3894 __set_bit(VCPU_EXREG_PDPTR,
3895 (unsigned long *)&vcpu->arch.regs_avail);
3896 __set_bit(VCPU_EXREG_PDPTR,
3897 (unsigned long *)&vcpu->arch.regs_dirty);
3898 }
3899
3900 static int vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4);
3901
3902 static void ept_update_paging_mode_cr0(unsigned long *hw_cr0,
3903 unsigned long cr0,
3904 struct kvm_vcpu *vcpu)
3905 {
3906 if (!test_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail))
3907 vmx_decache_cr3(vcpu);
3908 if (!(cr0 & X86_CR0_PG)) {
3909 /* From paging/starting to nonpaging */
3910 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL,
3911 vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) |
3912 (CPU_BASED_CR3_LOAD_EXITING |
3913 CPU_BASED_CR3_STORE_EXITING));
3914 vcpu->arch.cr0 = cr0;
3915 vmx_set_cr4(vcpu, kvm_read_cr4(vcpu));
3916 } else if (!is_paging(vcpu)) {
3917 /* From nonpaging to paging */
3918 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL,
3919 vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) &
3920 ~(CPU_BASED_CR3_LOAD_EXITING |
3921 CPU_BASED_CR3_STORE_EXITING));
3922 vcpu->arch.cr0 = cr0;
3923 vmx_set_cr4(vcpu, kvm_read_cr4(vcpu));
3924 }
3925
3926 if (!(cr0 & X86_CR0_WP))
3927 *hw_cr0 &= ~X86_CR0_WP;
3928 }
3929
3930 static void vmx_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
3931 {
3932 struct vcpu_vmx *vmx = to_vmx(vcpu);
3933 unsigned long hw_cr0;
3934
3935 hw_cr0 = (cr0 & ~KVM_GUEST_CR0_MASK);
3936 if (enable_unrestricted_guest)
3937 hw_cr0 |= KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST;
3938 else {
3939 hw_cr0 |= KVM_VM_CR0_ALWAYS_ON;
3940
3941 if (vmx->rmode.vm86_active && (cr0 & X86_CR0_PE))
3942 enter_pmode(vcpu);
3943
3944 if (!vmx->rmode.vm86_active && !(cr0 & X86_CR0_PE))
3945 enter_rmode(vcpu);
3946 }
3947
3948 #ifdef CONFIG_X86_64
3949 if (vcpu->arch.efer & EFER_LME) {
3950 if (!is_paging(vcpu) && (cr0 & X86_CR0_PG))
3951 enter_lmode(vcpu);
3952 if (is_paging(vcpu) && !(cr0 & X86_CR0_PG))
3953 exit_lmode(vcpu);
3954 }
3955 #endif
3956
3957 if (enable_ept)
3958 ept_update_paging_mode_cr0(&hw_cr0, cr0, vcpu);
3959
3960 if (!vcpu->fpu_active)
3961 hw_cr0 |= X86_CR0_TS | X86_CR0_MP;
3962
3963 vmcs_writel(CR0_READ_SHADOW, cr0);
3964 vmcs_writel(GUEST_CR0, hw_cr0);
3965 vcpu->arch.cr0 = cr0;
3966
3967 /* depends on vcpu->arch.cr0 to be set to a new value */
3968 vmx->emulation_required = emulation_required(vcpu);
3969 }
3970
3971 static u64 construct_eptp(unsigned long root_hpa)
3972 {
3973 u64 eptp;
3974
3975 /* TODO write the value reading from MSR */
3976 eptp = VMX_EPT_DEFAULT_MT |
3977 VMX_EPT_DEFAULT_GAW << VMX_EPT_GAW_EPTP_SHIFT;
3978 if (enable_ept_ad_bits)
3979 eptp |= VMX_EPT_AD_ENABLE_BIT;
3980 eptp |= (root_hpa & PAGE_MASK);
3981
3982 return eptp;
3983 }
3984
3985 static void vmx_set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3)
3986 {
3987 unsigned long guest_cr3;
3988 u64 eptp;
3989
3990 guest_cr3 = cr3;
3991 if (enable_ept) {
3992 eptp = construct_eptp(cr3);
3993 vmcs_write64(EPT_POINTER, eptp);
3994 if (is_paging(vcpu) || is_guest_mode(vcpu))
3995 guest_cr3 = kvm_read_cr3(vcpu);
3996 else
3997 guest_cr3 = vcpu->kvm->arch.ept_identity_map_addr;
3998 ept_load_pdptrs(vcpu);
3999 }
4000
4001 vmx_flush_tlb(vcpu);
4002 vmcs_writel(GUEST_CR3, guest_cr3);
4003 }
4004
4005 static int vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
4006 {
4007 /*
4008 * Pass through host's Machine Check Enable value to hw_cr4, which
4009 * is in force while we are in guest mode. Do not let guests control
4010 * this bit, even if host CR4.MCE == 0.
4011 */
4012 unsigned long hw_cr4 =
4013 (cr4_read_shadow() & X86_CR4_MCE) |
4014 (cr4 & ~X86_CR4_MCE) |
4015 (to_vmx(vcpu)->rmode.vm86_active ?
4016 KVM_RMODE_VM_CR4_ALWAYS_ON : KVM_PMODE_VM_CR4_ALWAYS_ON);
4017
4018 if (cr4 & X86_CR4_VMXE) {
4019 /*
4020 * To use VMXON (and later other VMX instructions), a guest
4021 * must first be able to turn on cr4.VMXE (see handle_vmon()).
4022 * So basically the check on whether to allow nested VMX
4023 * is here.
4024 */
4025 if (!nested_vmx_allowed(vcpu))
4026 return 1;
4027 }
4028 if (to_vmx(vcpu)->nested.vmxon &&
4029 ((cr4 & VMXON_CR4_ALWAYSON) != VMXON_CR4_ALWAYSON))
4030 return 1;
4031
4032 vcpu->arch.cr4 = cr4;
4033 if (enable_ept) {
4034 if (!is_paging(vcpu)) {
4035 hw_cr4 &= ~X86_CR4_PAE;
4036 hw_cr4 |= X86_CR4_PSE;
4037 } else if (!(cr4 & X86_CR4_PAE)) {
4038 hw_cr4 &= ~X86_CR4_PAE;
4039 }
4040 }
4041
4042 if (!enable_unrestricted_guest && !is_paging(vcpu))
4043 /*
4044 * SMEP/SMAP/PKU is disabled if CPU is in non-paging mode in
4045 * hardware. To emulate this behavior, SMEP/SMAP/PKU needs
4046 * to be manually disabled when guest switches to non-paging
4047 * mode.
4048 *
4049 * If !enable_unrestricted_guest, the CPU is always running
4050 * with CR0.PG=1 and CR4 needs to be modified.
4051 * If enable_unrestricted_guest, the CPU automatically
4052 * disables SMEP/SMAP/PKU when the guest sets CR0.PG=0.
4053 */
4054 hw_cr4 &= ~(X86_CR4_SMEP | X86_CR4_SMAP | X86_CR4_PKE);
4055
4056 vmcs_writel(CR4_READ_SHADOW, cr4);
4057 vmcs_writel(GUEST_CR4, hw_cr4);
4058 return 0;
4059 }
4060
4061 static void vmx_get_segment(struct kvm_vcpu *vcpu,
4062 struct kvm_segment *var, int seg)
4063 {
4064 struct vcpu_vmx *vmx = to_vmx(vcpu);
4065 u32 ar;
4066
4067 if (vmx->rmode.vm86_active && seg != VCPU_SREG_LDTR) {
4068 *var = vmx->rmode.segs[seg];
4069 if (seg == VCPU_SREG_TR
4070 || var->selector == vmx_read_guest_seg_selector(vmx, seg))
4071 return;
4072 var->base = vmx_read_guest_seg_base(vmx, seg);
4073 var->selector = vmx_read_guest_seg_selector(vmx, seg);
4074 return;
4075 }
4076 var->base = vmx_read_guest_seg_base(vmx, seg);
4077 var->limit = vmx_read_guest_seg_limit(vmx, seg);
4078 var->selector = vmx_read_guest_seg_selector(vmx, seg);
4079 ar = vmx_read_guest_seg_ar(vmx, seg);
4080 var->unusable = (ar >> 16) & 1;
4081 var->type = ar & 15;
4082 var->s = (ar >> 4) & 1;
4083 var->dpl = (ar >> 5) & 3;
4084 /*
4085 * Some userspaces do not preserve unusable property. Since usable
4086 * segment has to be present according to VMX spec we can use present
4087 * property to amend userspace bug by making unusable segment always
4088 * nonpresent. vmx_segment_access_rights() already marks nonpresent
4089 * segment as unusable.
4090 */
4091 var->present = !var->unusable;
4092 var->avl = (ar >> 12) & 1;
4093 var->l = (ar >> 13) & 1;
4094 var->db = (ar >> 14) & 1;
4095 var->g = (ar >> 15) & 1;
4096 }
4097
4098 static u64 vmx_get_segment_base(struct kvm_vcpu *vcpu, int seg)
4099 {
4100 struct kvm_segment s;
4101
4102 if (to_vmx(vcpu)->rmode.vm86_active) {
4103 vmx_get_segment(vcpu, &s, seg);
4104 return s.base;
4105 }
4106 return vmx_read_guest_seg_base(to_vmx(vcpu), seg);
4107 }
4108
4109 static int vmx_get_cpl(struct kvm_vcpu *vcpu)
4110 {
4111 struct vcpu_vmx *vmx = to_vmx(vcpu);
4112
4113 if (unlikely(vmx->rmode.vm86_active))
4114 return 0;
4115 else {
4116 int ar = vmx_read_guest_seg_ar(vmx, VCPU_SREG_SS);
4117 return VMX_AR_DPL(ar);
4118 }
4119 }
4120
4121 static u32 vmx_segment_access_rights(struct kvm_segment *var)
4122 {
4123 u32 ar;
4124
4125 if (var->unusable || !var->present)
4126 ar = 1 << 16;
4127 else {
4128 ar = var->type & 15;
4129 ar |= (var->s & 1) << 4;
4130 ar |= (var->dpl & 3) << 5;
4131 ar |= (var->present & 1) << 7;
4132 ar |= (var->avl & 1) << 12;
4133 ar |= (var->l & 1) << 13;
4134 ar |= (var->db & 1) << 14;
4135 ar |= (var->g & 1) << 15;
4136 }
4137
4138 return ar;
4139 }
4140
4141 static void vmx_set_segment(struct kvm_vcpu *vcpu,
4142 struct kvm_segment *var, int seg)
4143 {
4144 struct vcpu_vmx *vmx = to_vmx(vcpu);
4145 const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
4146
4147 vmx_segment_cache_clear(vmx);
4148
4149 if (vmx->rmode.vm86_active && seg != VCPU_SREG_LDTR) {
4150 vmx->rmode.segs[seg] = *var;
4151 if (seg == VCPU_SREG_TR)
4152 vmcs_write16(sf->selector, var->selector);
4153 else if (var->s)
4154 fix_rmode_seg(seg, &vmx->rmode.segs[seg]);
4155 goto out;
4156 }
4157
4158 vmcs_writel(sf->base, var->base);
4159 vmcs_write32(sf->limit, var->limit);
4160 vmcs_write16(sf->selector, var->selector);
4161
4162 /*
4163 * Fix the "Accessed" bit in AR field of segment registers for older
4164 * qemu binaries.
4165 * IA32 arch specifies that at the time of processor reset the
4166 * "Accessed" bit in the AR field of segment registers is 1. And qemu
4167 * is setting it to 0 in the userland code. This causes invalid guest
4168 * state vmexit when "unrestricted guest" mode is turned on.
4169 * Fix for this setup issue in cpu_reset is being pushed in the qemu
4170 * tree. Newer qemu binaries with that qemu fix would not need this
4171 * kvm hack.
4172 */
4173 if (enable_unrestricted_guest && (seg != VCPU_SREG_LDTR))
4174 var->type |= 0x1; /* Accessed */
4175
4176 vmcs_write32(sf->ar_bytes, vmx_segment_access_rights(var));
4177
4178 out:
4179 vmx->emulation_required = emulation_required(vcpu);
4180 }
4181
4182 static void vmx_get_cs_db_l_bits(struct kvm_vcpu *vcpu, int *db, int *l)
4183 {
4184 u32 ar = vmx_read_guest_seg_ar(to_vmx(vcpu), VCPU_SREG_CS);
4185
4186 *db = (ar >> 14) & 1;
4187 *l = (ar >> 13) & 1;
4188 }
4189
4190 static void vmx_get_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
4191 {
4192 dt->size = vmcs_read32(GUEST_IDTR_LIMIT);
4193 dt->address = vmcs_readl(GUEST_IDTR_BASE);
4194 }
4195
4196 static void vmx_set_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
4197 {
4198 vmcs_write32(GUEST_IDTR_LIMIT, dt->size);
4199 vmcs_writel(GUEST_IDTR_BASE, dt->address);
4200 }
4201
4202 static void vmx_get_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
4203 {
4204 dt->size = vmcs_read32(GUEST_GDTR_LIMIT);
4205 dt->address = vmcs_readl(GUEST_GDTR_BASE);
4206 }
4207
4208 static void vmx_set_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
4209 {
4210 vmcs_write32(GUEST_GDTR_LIMIT, dt->size);
4211 vmcs_writel(GUEST_GDTR_BASE, dt->address);
4212 }
4213
4214 static bool rmode_segment_valid(struct kvm_vcpu *vcpu, int seg)
4215 {
4216 struct kvm_segment var;
4217 u32 ar;
4218
4219 vmx_get_segment(vcpu, &var, seg);
4220 var.dpl = 0x3;
4221 if (seg == VCPU_SREG_CS)
4222 var.type = 0x3;
4223 ar = vmx_segment_access_rights(&var);
4224
4225 if (var.base != (var.selector << 4))
4226 return false;
4227 if (var.limit != 0xffff)
4228 return false;
4229 if (ar != 0xf3)
4230 return false;
4231
4232 return true;
4233 }
4234
4235 static bool code_segment_valid(struct kvm_vcpu *vcpu)
4236 {
4237 struct kvm_segment cs;
4238 unsigned int cs_rpl;
4239
4240 vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
4241 cs_rpl = cs.selector & SEGMENT_RPL_MASK;
4242
4243 if (cs.unusable)
4244 return false;
4245 if (~cs.type & (VMX_AR_TYPE_CODE_MASK|VMX_AR_TYPE_ACCESSES_MASK))
4246 return false;
4247 if (!cs.s)
4248 return false;
4249 if (cs.type & VMX_AR_TYPE_WRITEABLE_MASK) {
4250 if (cs.dpl > cs_rpl)
4251 return false;
4252 } else {
4253 if (cs.dpl != cs_rpl)
4254 return false;
4255 }
4256 if (!cs.present)
4257 return false;
4258
4259 /* TODO: Add Reserved field check, this'll require a new member in the kvm_segment_field structure */
4260 return true;
4261 }
4262
4263 static bool stack_segment_valid(struct kvm_vcpu *vcpu)
4264 {
4265 struct kvm_segment ss;
4266 unsigned int ss_rpl;
4267
4268 vmx_get_segment(vcpu, &ss, VCPU_SREG_SS);
4269 ss_rpl = ss.selector & SEGMENT_RPL_MASK;
4270
4271 if (ss.unusable)
4272 return true;
4273 if (ss.type != 3 && ss.type != 7)
4274 return false;
4275 if (!ss.s)
4276 return false;
4277 if (ss.dpl != ss_rpl) /* DPL != RPL */
4278 return false;
4279 if (!ss.present)
4280 return false;
4281
4282 return true;
4283 }
4284
4285 static bool data_segment_valid(struct kvm_vcpu *vcpu, int seg)
4286 {
4287 struct kvm_segment var;
4288 unsigned int rpl;
4289
4290 vmx_get_segment(vcpu, &var, seg);
4291 rpl = var.selector & SEGMENT_RPL_MASK;
4292
4293 if (var.unusable)
4294 return true;
4295 if (!var.s)
4296 return false;
4297 if (!var.present)
4298 return false;
4299 if (~var.type & (VMX_AR_TYPE_CODE_MASK|VMX_AR_TYPE_WRITEABLE_MASK)) {
4300 if (var.dpl < rpl) /* DPL < RPL */
4301 return false;
4302 }
4303
4304 /* TODO: Add other members to kvm_segment_field to allow checking for other access
4305 * rights flags
4306 */
4307 return true;
4308 }
4309
4310 static bool tr_valid(struct kvm_vcpu *vcpu)
4311 {
4312 struct kvm_segment tr;
4313
4314 vmx_get_segment(vcpu, &tr, VCPU_SREG_TR);
4315
4316 if (tr.unusable)
4317 return false;
4318 if (tr.selector & SEGMENT_TI_MASK) /* TI = 1 */
4319 return false;
4320 if (tr.type != 3 && tr.type != 11) /* TODO: Check if guest is in IA32e mode */
4321 return false;
4322 if (!tr.present)
4323 return false;
4324
4325 return true;
4326 }
4327
4328 static bool ldtr_valid(struct kvm_vcpu *vcpu)
4329 {
4330 struct kvm_segment ldtr;
4331
4332 vmx_get_segment(vcpu, &ldtr, VCPU_SREG_LDTR);
4333
4334 if (ldtr.unusable)
4335 return true;
4336 if (ldtr.selector & SEGMENT_TI_MASK) /* TI = 1 */
4337 return false;
4338 if (ldtr.type != 2)
4339 return false;
4340 if (!ldtr.present)
4341 return false;
4342
4343 return true;
4344 }
4345
4346 static bool cs_ss_rpl_check(struct kvm_vcpu *vcpu)
4347 {
4348 struct kvm_segment cs, ss;
4349
4350 vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
4351 vmx_get_segment(vcpu, &ss, VCPU_SREG_SS);
4352
4353 return ((cs.selector & SEGMENT_RPL_MASK) ==
4354 (ss.selector & SEGMENT_RPL_MASK));
4355 }
4356
4357 /*
4358 * Check if guest state is valid. Returns true if valid, false if
4359 * not.
4360 * We assume that registers are always usable
4361 */
4362 static bool guest_state_valid(struct kvm_vcpu *vcpu)
4363 {
4364 if (enable_unrestricted_guest)
4365 return true;
4366
4367 /* real mode guest state checks */
4368 if (!is_protmode(vcpu) || (vmx_get_rflags(vcpu) & X86_EFLAGS_VM)) {
4369 if (!rmode_segment_valid(vcpu, VCPU_SREG_CS))
4370 return false;
4371 if (!rmode_segment_valid(vcpu, VCPU_SREG_SS))
4372 return false;
4373 if (!rmode_segment_valid(vcpu, VCPU_SREG_DS))
4374 return false;
4375 if (!rmode_segment_valid(vcpu, VCPU_SREG_ES))
4376 return false;
4377 if (!rmode_segment_valid(vcpu, VCPU_SREG_FS))
4378 return false;
4379 if (!rmode_segment_valid(vcpu, VCPU_SREG_GS))
4380 return false;
4381 } else {
4382 /* protected mode guest state checks */
4383 if (!cs_ss_rpl_check(vcpu))
4384 return false;
4385 if (!code_segment_valid(vcpu))
4386 return false;
4387 if (!stack_segment_valid(vcpu))
4388 return false;
4389 if (!data_segment_valid(vcpu, VCPU_SREG_DS))
4390 return false;
4391 if (!data_segment_valid(vcpu, VCPU_SREG_ES))
4392 return false;
4393 if (!data_segment_valid(vcpu, VCPU_SREG_FS))
4394 return false;
4395 if (!data_segment_valid(vcpu, VCPU_SREG_GS))
4396 return false;
4397 if (!tr_valid(vcpu))
4398 return false;
4399 if (!ldtr_valid(vcpu))
4400 return false;
4401 }
4402 /* TODO:
4403 * - Add checks on RIP
4404 * - Add checks on RFLAGS
4405 */
4406
4407 return true;
4408 }
4409
4410 static int init_rmode_tss(struct kvm *kvm)
4411 {
4412 gfn_t fn;
4413 u16 data = 0;
4414 int idx, r;
4415
4416 idx = srcu_read_lock(&kvm->srcu);
4417 fn = kvm->arch.tss_addr >> PAGE_SHIFT;
4418 r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE);
4419 if (r < 0)
4420 goto out;
4421 data = TSS_BASE_SIZE + TSS_REDIRECTION_SIZE;
4422 r = kvm_write_guest_page(kvm, fn++, &data,
4423 TSS_IOPB_BASE_OFFSET, sizeof(u16));
4424 if (r < 0)
4425 goto out;
4426 r = kvm_clear_guest_page(kvm, fn++, 0, PAGE_SIZE);
4427 if (r < 0)
4428 goto out;
4429 r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE);
4430 if (r < 0)
4431 goto out;
4432 data = ~0;
4433 r = kvm_write_guest_page(kvm, fn, &data,
4434 RMODE_TSS_SIZE - 2 * PAGE_SIZE - 1,
4435 sizeof(u8));
4436 out:
4437 srcu_read_unlock(&kvm->srcu, idx);
4438 return r;
4439 }
4440
4441 static int init_rmode_identity_map(struct kvm *kvm)
4442 {
4443 int i, idx, r = 0;
4444 kvm_pfn_t identity_map_pfn;
4445 u32 tmp;
4446
4447 if (!enable_ept)
4448 return 0;
4449
4450 /* Protect kvm->arch.ept_identity_pagetable_done. */
4451 mutex_lock(&kvm->slots_lock);
4452
4453 if (likely(kvm->arch.ept_identity_pagetable_done))
4454 goto out2;
4455
4456 identity_map_pfn = kvm->arch.ept_identity_map_addr >> PAGE_SHIFT;
4457
4458 r = alloc_identity_pagetable(kvm);
4459 if (r < 0)
4460 goto out2;
4461
4462 idx = srcu_read_lock(&kvm->srcu);
4463 r = kvm_clear_guest_page(kvm, identity_map_pfn, 0, PAGE_SIZE);
4464 if (r < 0)
4465 goto out;
4466 /* Set up identity-mapping pagetable for EPT in real mode */
4467 for (i = 0; i < PT32_ENT_PER_PAGE; i++) {
4468 tmp = (i << 22) + (_PAGE_PRESENT | _PAGE_RW | _PAGE_USER |
4469 _PAGE_ACCESSED | _PAGE_DIRTY | _PAGE_PSE);
4470 r = kvm_write_guest_page(kvm, identity_map_pfn,
4471 &tmp, i * sizeof(tmp), sizeof(tmp));
4472 if (r < 0)
4473 goto out;
4474 }
4475 kvm->arch.ept_identity_pagetable_done = true;
4476
4477 out:
4478 srcu_read_unlock(&kvm->srcu, idx);
4479
4480 out2:
4481 mutex_unlock(&kvm->slots_lock);
4482 return r;
4483 }
4484
4485 static void seg_setup(int seg)
4486 {
4487 const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
4488 unsigned int ar;
4489
4490 vmcs_write16(sf->selector, 0);
4491 vmcs_writel(sf->base, 0);
4492 vmcs_write32(sf->limit, 0xffff);
4493 ar = 0x93;
4494 if (seg == VCPU_SREG_CS)
4495 ar |= 0x08; /* code segment */
4496
4497 vmcs_write32(sf->ar_bytes, ar);
4498 }
4499
4500 static int alloc_apic_access_page(struct kvm *kvm)
4501 {
4502 struct page *page;
4503 int r = 0;
4504
4505 mutex_lock(&kvm->slots_lock);
4506 if (kvm->arch.apic_access_page_done)
4507 goto out;
4508 r = __x86_set_memory_region(kvm, APIC_ACCESS_PAGE_PRIVATE_MEMSLOT,
4509 APIC_DEFAULT_PHYS_BASE, PAGE_SIZE);
4510 if (r)
4511 goto out;
4512
4513 page = gfn_to_page(kvm, APIC_DEFAULT_PHYS_BASE >> PAGE_SHIFT);
4514 if (is_error_page(page)) {
4515 r = -EFAULT;
4516 goto out;
4517 }
4518
4519 /*
4520 * Do not pin the page in memory, so that memory hot-unplug
4521 * is able to migrate it.
4522 */
4523 put_page(page);
4524 kvm->arch.apic_access_page_done = true;
4525 out:
4526 mutex_unlock(&kvm->slots_lock);
4527 return r;
4528 }
4529
4530 static int alloc_identity_pagetable(struct kvm *kvm)
4531 {
4532 /* Called with kvm->slots_lock held. */
4533
4534 int r = 0;
4535
4536 BUG_ON(kvm->arch.ept_identity_pagetable_done);
4537
4538 r = __x86_set_memory_region(kvm, IDENTITY_PAGETABLE_PRIVATE_MEMSLOT,
4539 kvm->arch.ept_identity_map_addr, PAGE_SIZE);
4540
4541 return r;
4542 }
4543
4544 static int allocate_vpid(void)
4545 {
4546 int vpid;
4547
4548 if (!enable_vpid)
4549 return 0;
4550 spin_lock(&vmx_vpid_lock);
4551 vpid = find_first_zero_bit(vmx_vpid_bitmap, VMX_NR_VPIDS);
4552 if (vpid < VMX_NR_VPIDS)
4553 __set_bit(vpid, vmx_vpid_bitmap);
4554 else
4555 vpid = 0;
4556 spin_unlock(&vmx_vpid_lock);
4557 return vpid;
4558 }
4559
4560 static void free_vpid(int vpid)
4561 {
4562 if (!enable_vpid || vpid == 0)
4563 return;
4564 spin_lock(&vmx_vpid_lock);
4565 __clear_bit(vpid, vmx_vpid_bitmap);
4566 spin_unlock(&vmx_vpid_lock);
4567 }
4568
4569 #define MSR_TYPE_R 1
4570 #define MSR_TYPE_W 2
4571 static void __vmx_disable_intercept_for_msr(unsigned long *msr_bitmap,
4572 u32 msr, int type)
4573 {
4574 int f = sizeof(unsigned long);
4575
4576 if (!cpu_has_vmx_msr_bitmap())
4577 return;
4578
4579 /*
4580 * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
4581 * have the write-low and read-high bitmap offsets the wrong way round.
4582 * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
4583 */
4584 if (msr <= 0x1fff) {
4585 if (type & MSR_TYPE_R)
4586 /* read-low */
4587 __clear_bit(msr, msr_bitmap + 0x000 / f);
4588
4589 if (type & MSR_TYPE_W)
4590 /* write-low */
4591 __clear_bit(msr, msr_bitmap + 0x800 / f);
4592
4593 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
4594 msr &= 0x1fff;
4595 if (type & MSR_TYPE_R)
4596 /* read-high */
4597 __clear_bit(msr, msr_bitmap + 0x400 / f);
4598
4599 if (type & MSR_TYPE_W)
4600 /* write-high */
4601 __clear_bit(msr, msr_bitmap + 0xc00 / f);
4602
4603 }
4604 }
4605
4606 /*
4607 * If a msr is allowed by L0, we should check whether it is allowed by L1.
4608 * The corresponding bit will be cleared unless both of L0 and L1 allow it.
4609 */
4610 static void nested_vmx_disable_intercept_for_msr(unsigned long *msr_bitmap_l1,
4611 unsigned long *msr_bitmap_nested,
4612 u32 msr, int type)
4613 {
4614 int f = sizeof(unsigned long);
4615
4616 if (!cpu_has_vmx_msr_bitmap()) {
4617 WARN_ON(1);
4618 return;
4619 }
4620
4621 /*
4622 * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
4623 * have the write-low and read-high bitmap offsets the wrong way round.
4624 * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
4625 */
4626 if (msr <= 0x1fff) {
4627 if (type & MSR_TYPE_R &&
4628 !test_bit(msr, msr_bitmap_l1 + 0x000 / f))
4629 /* read-low */
4630 __clear_bit(msr, msr_bitmap_nested + 0x000 / f);
4631
4632 if (type & MSR_TYPE_W &&
4633 !test_bit(msr, msr_bitmap_l1 + 0x800 / f))
4634 /* write-low */
4635 __clear_bit(msr, msr_bitmap_nested + 0x800 / f);
4636
4637 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
4638 msr &= 0x1fff;
4639 if (type & MSR_TYPE_R &&
4640 !test_bit(msr, msr_bitmap_l1 + 0x400 / f))
4641 /* read-high */
4642 __clear_bit(msr, msr_bitmap_nested + 0x400 / f);
4643
4644 if (type & MSR_TYPE_W &&
4645 !test_bit(msr, msr_bitmap_l1 + 0xc00 / f))
4646 /* write-high */
4647 __clear_bit(msr, msr_bitmap_nested + 0xc00 / f);
4648
4649 }
4650 }
4651
4652 static void vmx_disable_intercept_for_msr(u32 msr, bool longmode_only)
4653 {
4654 if (!longmode_only)
4655 __vmx_disable_intercept_for_msr(vmx_msr_bitmap_legacy,
4656 msr, MSR_TYPE_R | MSR_TYPE_W);
4657 __vmx_disable_intercept_for_msr(vmx_msr_bitmap_longmode,
4658 msr, MSR_TYPE_R | MSR_TYPE_W);
4659 }
4660
4661 static void vmx_disable_intercept_msr_x2apic(u32 msr, int type, bool apicv_active)
4662 {
4663 if (apicv_active) {
4664 __vmx_disable_intercept_for_msr(vmx_msr_bitmap_legacy_x2apic_apicv,
4665 msr, type);
4666 __vmx_disable_intercept_for_msr(vmx_msr_bitmap_longmode_x2apic_apicv,
4667 msr, type);
4668 } else {
4669 __vmx_disable_intercept_for_msr(vmx_msr_bitmap_legacy_x2apic,
4670 msr, type);
4671 __vmx_disable_intercept_for_msr(vmx_msr_bitmap_longmode_x2apic,
4672 msr, type);
4673 }
4674 }
4675
4676 static bool vmx_get_enable_apicv(void)
4677 {
4678 return enable_apicv;
4679 }
4680
4681 static int vmx_complete_nested_posted_interrupt(struct kvm_vcpu *vcpu)
4682 {
4683 struct vcpu_vmx *vmx = to_vmx(vcpu);
4684 int max_irr;
4685 void *vapic_page;
4686 u16 status;
4687
4688 if (vmx->nested.pi_desc &&
4689 vmx->nested.pi_pending) {
4690 vmx->nested.pi_pending = false;
4691 if (!pi_test_and_clear_on(vmx->nested.pi_desc))
4692 return 0;
4693
4694 max_irr = find_last_bit(
4695 (unsigned long *)vmx->nested.pi_desc->pir, 256);
4696
4697 if (max_irr == 256)
4698 return 0;
4699
4700 vapic_page = kmap(vmx->nested.virtual_apic_page);
4701 if (!vapic_page) {
4702 WARN_ON(1);
4703 return -ENOMEM;
4704 }
4705 __kvm_apic_update_irr(vmx->nested.pi_desc->pir, vapic_page);
4706 kunmap(vmx->nested.virtual_apic_page);
4707
4708 status = vmcs_read16(GUEST_INTR_STATUS);
4709 if ((u8)max_irr > ((u8)status & 0xff)) {
4710 status &= ~0xff;
4711 status |= (u8)max_irr;
4712 vmcs_write16(GUEST_INTR_STATUS, status);
4713 }
4714 }
4715 return 0;
4716 }
4717
4718 static inline bool kvm_vcpu_trigger_posted_interrupt(struct kvm_vcpu *vcpu)
4719 {
4720 #ifdef CONFIG_SMP
4721 if (vcpu->mode == IN_GUEST_MODE) {
4722 struct vcpu_vmx *vmx = to_vmx(vcpu);
4723
4724 /*
4725 * Currently, we don't support urgent interrupt,
4726 * all interrupts are recognized as non-urgent
4727 * interrupt, so we cannot post interrupts when
4728 * 'SN' is set.
4729 *
4730 * If the vcpu is in guest mode, it means it is
4731 * running instead of being scheduled out and
4732 * waiting in the run queue, and that's the only
4733 * case when 'SN' is set currently, warning if
4734 * 'SN' is set.
4735 */
4736 WARN_ON_ONCE(pi_test_sn(&vmx->pi_desc));
4737
4738 apic->send_IPI_mask(get_cpu_mask(vcpu->cpu),
4739 POSTED_INTR_VECTOR);
4740 return true;
4741 }
4742 #endif
4743 return false;
4744 }
4745
4746 static int vmx_deliver_nested_posted_interrupt(struct kvm_vcpu *vcpu,
4747 int vector)
4748 {
4749 struct vcpu_vmx *vmx = to_vmx(vcpu);
4750
4751 if (is_guest_mode(vcpu) &&
4752 vector == vmx->nested.posted_intr_nv) {
4753 /* the PIR and ON have been set by L1. */
4754 kvm_vcpu_trigger_posted_interrupt(vcpu);
4755 /*
4756 * If a posted intr is not recognized by hardware,
4757 * we will accomplish it in the next vmentry.
4758 */
4759 vmx->nested.pi_pending = true;
4760 kvm_make_request(KVM_REQ_EVENT, vcpu);
4761 return 0;
4762 }
4763 return -1;
4764 }
4765 /*
4766 * Send interrupt to vcpu via posted interrupt way.
4767 * 1. If target vcpu is running(non-root mode), send posted interrupt
4768 * notification to vcpu and hardware will sync PIR to vIRR atomically.
4769 * 2. If target vcpu isn't running(root mode), kick it to pick up the
4770 * interrupt from PIR in next vmentry.
4771 */
4772 static void vmx_deliver_posted_interrupt(struct kvm_vcpu *vcpu, int vector)
4773 {
4774 struct vcpu_vmx *vmx = to_vmx(vcpu);
4775 int r;
4776
4777 r = vmx_deliver_nested_posted_interrupt(vcpu, vector);
4778 if (!r)
4779 return;
4780
4781 if (pi_test_and_set_pir(vector, &vmx->pi_desc))
4782 return;
4783
4784 r = pi_test_and_set_on(&vmx->pi_desc);
4785 kvm_make_request(KVM_REQ_EVENT, vcpu);
4786 if (r || !kvm_vcpu_trigger_posted_interrupt(vcpu))
4787 kvm_vcpu_kick(vcpu);
4788 }
4789
4790 static void vmx_sync_pir_to_irr(struct kvm_vcpu *vcpu)
4791 {
4792 struct vcpu_vmx *vmx = to_vmx(vcpu);
4793
4794 if (!pi_test_on(&vmx->pi_desc))
4795 return;
4796
4797 pi_clear_on(&vmx->pi_desc);
4798 /*
4799 * IOMMU can write to PIR.ON, so the barrier matters even on UP.
4800 * But on x86 this is just a compiler barrier anyway.
4801 */
4802 smp_mb__after_atomic();
4803 kvm_apic_update_irr(vcpu, vmx->pi_desc.pir);
4804 }
4805
4806 /*
4807 * Set up the vmcs's constant host-state fields, i.e., host-state fields that
4808 * will not change in the lifetime of the guest.
4809 * Note that host-state that does change is set elsewhere. E.g., host-state
4810 * that is set differently for each CPU is set in vmx_vcpu_load(), not here.
4811 */
4812 static void vmx_set_constant_host_state(struct vcpu_vmx *vmx)
4813 {
4814 u32 low32, high32;
4815 unsigned long tmpl;
4816 struct desc_ptr dt;
4817 unsigned long cr4;
4818
4819 vmcs_writel(HOST_CR0, read_cr0() & ~X86_CR0_TS); /* 22.2.3 */
4820 vmcs_writel(HOST_CR3, read_cr3()); /* 22.2.3 FIXME: shadow tables */
4821
4822 /* Save the most likely value for this task's CR4 in the VMCS. */
4823 cr4 = cr4_read_shadow();
4824 vmcs_writel(HOST_CR4, cr4); /* 22.2.3, 22.2.5 */
4825 vmx->host_state.vmcs_host_cr4 = cr4;
4826
4827 vmcs_write16(HOST_CS_SELECTOR, __KERNEL_CS); /* 22.2.4 */
4828 #ifdef CONFIG_X86_64
4829 /*
4830 * Load null selectors, so we can avoid reloading them in
4831 * __vmx_load_host_state(), in case userspace uses the null selectors
4832 * too (the expected case).
4833 */
4834 vmcs_write16(HOST_DS_SELECTOR, 0);
4835 vmcs_write16(HOST_ES_SELECTOR, 0);
4836 #else
4837 vmcs_write16(HOST_DS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
4838 vmcs_write16(HOST_ES_SELECTOR, __KERNEL_DS); /* 22.2.4 */
4839 #endif
4840 vmcs_write16(HOST_SS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
4841 vmcs_write16(HOST_TR_SELECTOR, GDT_ENTRY_TSS*8); /* 22.2.4 */
4842
4843 native_store_idt(&dt);
4844 vmcs_writel(HOST_IDTR_BASE, dt.address); /* 22.2.4 */
4845 vmx->host_idt_base = dt.address;
4846
4847 vmcs_writel(HOST_RIP, vmx_return); /* 22.2.5 */
4848
4849 rdmsr(MSR_IA32_SYSENTER_CS, low32, high32);
4850 vmcs_write32(HOST_IA32_SYSENTER_CS, low32);
4851 rdmsrl(MSR_IA32_SYSENTER_EIP, tmpl);
4852 vmcs_writel(HOST_IA32_SYSENTER_EIP, tmpl); /* 22.2.3 */
4853
4854 if (vmcs_config.vmexit_ctrl & VM_EXIT_LOAD_IA32_PAT) {
4855 rdmsr(MSR_IA32_CR_PAT, low32, high32);
4856 vmcs_write64(HOST_IA32_PAT, low32 | ((u64) high32 << 32));
4857 }
4858 }
4859
4860 static void set_cr4_guest_host_mask(struct vcpu_vmx *vmx)
4861 {
4862 vmx->vcpu.arch.cr4_guest_owned_bits = KVM_CR4_GUEST_OWNED_BITS;
4863 if (enable_ept)
4864 vmx->vcpu.arch.cr4_guest_owned_bits |= X86_CR4_PGE;
4865 if (is_guest_mode(&vmx->vcpu))
4866 vmx->vcpu.arch.cr4_guest_owned_bits &=
4867 ~get_vmcs12(&vmx->vcpu)->cr4_guest_host_mask;
4868 vmcs_writel(CR4_GUEST_HOST_MASK, ~vmx->vcpu.arch.cr4_guest_owned_bits);
4869 }
4870
4871 static u32 vmx_pin_based_exec_ctrl(struct vcpu_vmx *vmx)
4872 {
4873 u32 pin_based_exec_ctrl = vmcs_config.pin_based_exec_ctrl;
4874
4875 if (!kvm_vcpu_apicv_active(&vmx->vcpu))
4876 pin_based_exec_ctrl &= ~PIN_BASED_POSTED_INTR;
4877 /* Enable the preemption timer dynamically */
4878 pin_based_exec_ctrl &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
4879 return pin_based_exec_ctrl;
4880 }
4881
4882 static void vmx_refresh_apicv_exec_ctrl(struct kvm_vcpu *vcpu)
4883 {
4884 struct vcpu_vmx *vmx = to_vmx(vcpu);
4885
4886 vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, vmx_pin_based_exec_ctrl(vmx));
4887 if (cpu_has_secondary_exec_ctrls()) {
4888 if (kvm_vcpu_apicv_active(vcpu))
4889 vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL,
4890 SECONDARY_EXEC_APIC_REGISTER_VIRT |
4891 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
4892 else
4893 vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL,
4894 SECONDARY_EXEC_APIC_REGISTER_VIRT |
4895 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
4896 }
4897
4898 if (cpu_has_vmx_msr_bitmap())
4899 vmx_set_msr_bitmap(vcpu);
4900 }
4901
4902 static u32 vmx_exec_control(struct vcpu_vmx *vmx)
4903 {
4904 u32 exec_control = vmcs_config.cpu_based_exec_ctrl;
4905
4906 if (vmx->vcpu.arch.switch_db_regs & KVM_DEBUGREG_WONT_EXIT)
4907 exec_control &= ~CPU_BASED_MOV_DR_EXITING;
4908
4909 if (!cpu_need_tpr_shadow(&vmx->vcpu)) {
4910 exec_control &= ~CPU_BASED_TPR_SHADOW;
4911 #ifdef CONFIG_X86_64
4912 exec_control |= CPU_BASED_CR8_STORE_EXITING |
4913 CPU_BASED_CR8_LOAD_EXITING;
4914 #endif
4915 }
4916 if (!enable_ept)
4917 exec_control |= CPU_BASED_CR3_STORE_EXITING |
4918 CPU_BASED_CR3_LOAD_EXITING |
4919 CPU_BASED_INVLPG_EXITING;
4920 return exec_control;
4921 }
4922
4923 static u32 vmx_secondary_exec_control(struct vcpu_vmx *vmx)
4924 {
4925 u32 exec_control = vmcs_config.cpu_based_2nd_exec_ctrl;
4926 if (!cpu_need_virtualize_apic_accesses(&vmx->vcpu))
4927 exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
4928 if (vmx->vpid == 0)
4929 exec_control &= ~SECONDARY_EXEC_ENABLE_VPID;
4930 if (!enable_ept) {
4931 exec_control &= ~SECONDARY_EXEC_ENABLE_EPT;
4932 enable_unrestricted_guest = 0;
4933 /* Enable INVPCID for non-ept guests may cause performance regression. */
4934 exec_control &= ~SECONDARY_EXEC_ENABLE_INVPCID;
4935 }
4936 if (!enable_unrestricted_guest)
4937 exec_control &= ~SECONDARY_EXEC_UNRESTRICTED_GUEST;
4938 if (!ple_gap)
4939 exec_control &= ~SECONDARY_EXEC_PAUSE_LOOP_EXITING;
4940 if (!kvm_vcpu_apicv_active(&vmx->vcpu))
4941 exec_control &= ~(SECONDARY_EXEC_APIC_REGISTER_VIRT |
4942 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
4943 exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
4944 /* SECONDARY_EXEC_SHADOW_VMCS is enabled when L1 executes VMPTRLD
4945 (handle_vmptrld).
4946 We can NOT enable shadow_vmcs here because we don't have yet
4947 a current VMCS12
4948 */
4949 exec_control &= ~SECONDARY_EXEC_SHADOW_VMCS;
4950
4951 if (!enable_pml)
4952 exec_control &= ~SECONDARY_EXEC_ENABLE_PML;
4953
4954 return exec_control;
4955 }
4956
4957 static void ept_set_mmio_spte_mask(void)
4958 {
4959 /*
4960 * EPT Misconfigurations can be generated if the value of bits 2:0
4961 * of an EPT paging-structure entry is 110b (write/execute).
4962 * Also, magic bits (0x3ull << 62) is set to quickly identify mmio
4963 * spte.
4964 */
4965 kvm_mmu_set_mmio_spte_mask((0x3ull << 62) | 0x6ull);
4966 }
4967
4968 #define VMX_XSS_EXIT_BITMAP 0
4969 /*
4970 * Sets up the vmcs for emulated real mode.
4971 */
4972 static int vmx_vcpu_setup(struct vcpu_vmx *vmx)
4973 {
4974 #ifdef CONFIG_X86_64
4975 unsigned long a;
4976 #endif
4977 int i;
4978
4979 /* I/O */
4980 vmcs_write64(IO_BITMAP_A, __pa(vmx_io_bitmap_a));
4981 vmcs_write64(IO_BITMAP_B, __pa(vmx_io_bitmap_b));
4982
4983 if (enable_shadow_vmcs) {
4984 vmcs_write64(VMREAD_BITMAP, __pa(vmx_vmread_bitmap));
4985 vmcs_write64(VMWRITE_BITMAP, __pa(vmx_vmwrite_bitmap));
4986 }
4987 if (cpu_has_vmx_msr_bitmap())
4988 vmcs_write64(MSR_BITMAP, __pa(vmx_msr_bitmap_legacy));
4989
4990 vmcs_write64(VMCS_LINK_POINTER, -1ull); /* 22.3.1.5 */
4991
4992 /* Control */
4993 vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, vmx_pin_based_exec_ctrl(vmx));
4994 vmx->hv_deadline_tsc = -1;
4995
4996 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, vmx_exec_control(vmx));
4997
4998 if (cpu_has_secondary_exec_ctrls()) {
4999 vmcs_write32(SECONDARY_VM_EXEC_CONTROL,
5000 vmx_secondary_exec_control(vmx));
5001 }
5002
5003 if (kvm_vcpu_apicv_active(&vmx->vcpu)) {
5004 vmcs_write64(EOI_EXIT_BITMAP0, 0);
5005 vmcs_write64(EOI_EXIT_BITMAP1, 0);
5006 vmcs_write64(EOI_EXIT_BITMAP2, 0);
5007 vmcs_write64(EOI_EXIT_BITMAP3, 0);
5008
5009 vmcs_write16(GUEST_INTR_STATUS, 0);
5010
5011 vmcs_write16(POSTED_INTR_NV, POSTED_INTR_VECTOR);
5012 vmcs_write64(POSTED_INTR_DESC_ADDR, __pa((&vmx->pi_desc)));
5013 }
5014
5015 if (ple_gap) {
5016 vmcs_write32(PLE_GAP, ple_gap);
5017 vmx->ple_window = ple_window;
5018 vmx->ple_window_dirty = true;
5019 }
5020
5021 vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK, 0);
5022 vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH, 0);
5023 vmcs_write32(CR3_TARGET_COUNT, 0); /* 22.2.1 */
5024
5025 vmcs_write16(HOST_FS_SELECTOR, 0); /* 22.2.4 */
5026 vmcs_write16(HOST_GS_SELECTOR, 0); /* 22.2.4 */
5027 vmx_set_constant_host_state(vmx);
5028 #ifdef CONFIG_X86_64
5029 rdmsrl(MSR_FS_BASE, a);
5030 vmcs_writel(HOST_FS_BASE, a); /* 22.2.4 */
5031 rdmsrl(MSR_GS_BASE, a);
5032 vmcs_writel(HOST_GS_BASE, a); /* 22.2.4 */
5033 #else
5034 vmcs_writel(HOST_FS_BASE, 0); /* 22.2.4 */
5035 vmcs_writel(HOST_GS_BASE, 0); /* 22.2.4 */
5036 #endif
5037
5038 vmcs_write32(VM_EXIT_MSR_STORE_COUNT, 0);
5039 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, 0);
5040 vmcs_write64(VM_EXIT_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.host));
5041 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, 0);
5042 vmcs_write64(VM_ENTRY_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.guest));
5043
5044 if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT)
5045 vmcs_write64(GUEST_IA32_PAT, vmx->vcpu.arch.pat);
5046
5047 for (i = 0; i < ARRAY_SIZE(vmx_msr_index); ++i) {
5048 u32 index = vmx_msr_index[i];
5049 u32 data_low, data_high;
5050 int j = vmx->nmsrs;
5051
5052 if (rdmsr_safe(index, &data_low, &data_high) < 0)
5053 continue;
5054 if (wrmsr_safe(index, data_low, data_high) < 0)
5055 continue;
5056 vmx->guest_msrs[j].index = i;
5057 vmx->guest_msrs[j].data = 0;
5058 vmx->guest_msrs[j].mask = -1ull;
5059 ++vmx->nmsrs;
5060 }
5061
5062
5063 vm_exit_controls_init(vmx, vmcs_config.vmexit_ctrl);
5064
5065 /* 22.2.1, 20.8.1 */
5066 vm_entry_controls_init(vmx, vmcs_config.vmentry_ctrl);
5067
5068 vmcs_writel(CR0_GUEST_HOST_MASK, ~0UL);
5069 set_cr4_guest_host_mask(vmx);
5070
5071 if (vmx_xsaves_supported())
5072 vmcs_write64(XSS_EXIT_BITMAP, VMX_XSS_EXIT_BITMAP);
5073
5074 if (enable_pml) {
5075 ASSERT(vmx->pml_pg);
5076 vmcs_write64(PML_ADDRESS, page_to_phys(vmx->pml_pg));
5077 vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1);
5078 }
5079
5080 return 0;
5081 }
5082
5083 static void vmx_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event)
5084 {
5085 struct vcpu_vmx *vmx = to_vmx(vcpu);
5086 struct msr_data apic_base_msr;
5087 u64 cr0;
5088
5089 vmx->rmode.vm86_active = 0;
5090
5091 vmx->soft_vnmi_blocked = 0;
5092
5093 vmx->vcpu.arch.regs[VCPU_REGS_RDX] = get_rdx_init_val();
5094 kvm_set_cr8(vcpu, 0);
5095
5096 if (!init_event) {
5097 apic_base_msr.data = APIC_DEFAULT_PHYS_BASE |
5098 MSR_IA32_APICBASE_ENABLE;
5099 if (kvm_vcpu_is_reset_bsp(vcpu))
5100 apic_base_msr.data |= MSR_IA32_APICBASE_BSP;
5101 apic_base_msr.host_initiated = true;
5102 kvm_set_apic_base(vcpu, &apic_base_msr);
5103 }
5104
5105 vmx_segment_cache_clear(vmx);
5106
5107 seg_setup(VCPU_SREG_CS);
5108 vmcs_write16(GUEST_CS_SELECTOR, 0xf000);
5109 vmcs_writel(GUEST_CS_BASE, 0xffff0000ul);
5110
5111 seg_setup(VCPU_SREG_DS);
5112 seg_setup(VCPU_SREG_ES);
5113 seg_setup(VCPU_SREG_FS);
5114 seg_setup(VCPU_SREG_GS);
5115 seg_setup(VCPU_SREG_SS);
5116
5117 vmcs_write16(GUEST_TR_SELECTOR, 0);
5118 vmcs_writel(GUEST_TR_BASE, 0);
5119 vmcs_write32(GUEST_TR_LIMIT, 0xffff);
5120 vmcs_write32(GUEST_TR_AR_BYTES, 0x008b);
5121
5122 vmcs_write16(GUEST_LDTR_SELECTOR, 0);
5123 vmcs_writel(GUEST_LDTR_BASE, 0);
5124 vmcs_write32(GUEST_LDTR_LIMIT, 0xffff);
5125 vmcs_write32(GUEST_LDTR_AR_BYTES, 0x00082);
5126
5127 if (!init_event) {
5128 vmcs_write32(GUEST_SYSENTER_CS, 0);
5129 vmcs_writel(GUEST_SYSENTER_ESP, 0);
5130 vmcs_writel(GUEST_SYSENTER_EIP, 0);
5131 vmcs_write64(GUEST_IA32_DEBUGCTL, 0);
5132 }
5133
5134 vmcs_writel(GUEST_RFLAGS, 0x02);
5135 kvm_rip_write(vcpu, 0xfff0);
5136
5137 vmcs_writel(GUEST_GDTR_BASE, 0);
5138 vmcs_write32(GUEST_GDTR_LIMIT, 0xffff);
5139
5140 vmcs_writel(GUEST_IDTR_BASE, 0);
5141 vmcs_write32(GUEST_IDTR_LIMIT, 0xffff);
5142
5143 vmcs_write32(GUEST_ACTIVITY_STATE, GUEST_ACTIVITY_ACTIVE);
5144 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, 0);
5145 vmcs_writel(GUEST_PENDING_DBG_EXCEPTIONS, 0);
5146
5147 setup_msrs(vmx);
5148
5149 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0); /* 22.2.1 */
5150
5151 if (cpu_has_vmx_tpr_shadow() && !init_event) {
5152 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, 0);
5153 if (cpu_need_tpr_shadow(vcpu))
5154 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR,
5155 __pa(vcpu->arch.apic->regs));
5156 vmcs_write32(TPR_THRESHOLD, 0);
5157 }
5158
5159 kvm_make_request(KVM_REQ_APIC_PAGE_RELOAD, vcpu);
5160
5161 if (kvm_vcpu_apicv_active(vcpu))
5162 memset(&vmx->pi_desc, 0, sizeof(struct pi_desc));
5163
5164 if (vmx->vpid != 0)
5165 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid);
5166
5167 cr0 = X86_CR0_NW | X86_CR0_CD | X86_CR0_ET;
5168 vmx->vcpu.arch.cr0 = cr0;
5169 vmx_set_cr0(vcpu, cr0); /* enter rmode */
5170 vmx_set_cr4(vcpu, 0);
5171 vmx_set_efer(vcpu, 0);
5172 vmx_fpu_activate(vcpu);
5173 update_exception_bitmap(vcpu);
5174
5175 vpid_sync_context(vmx->vpid);
5176 }
5177
5178 /*
5179 * In nested virtualization, check if L1 asked to exit on external interrupts.
5180 * For most existing hypervisors, this will always return true.
5181 */
5182 static bool nested_exit_on_intr(struct kvm_vcpu *vcpu)
5183 {
5184 return get_vmcs12(vcpu)->pin_based_vm_exec_control &
5185 PIN_BASED_EXT_INTR_MASK;
5186 }
5187
5188 /*
5189 * In nested virtualization, check if L1 has set
5190 * VM_EXIT_ACK_INTR_ON_EXIT
5191 */
5192 static bool nested_exit_intr_ack_set(struct kvm_vcpu *vcpu)
5193 {
5194 return get_vmcs12(vcpu)->vm_exit_controls &
5195 VM_EXIT_ACK_INTR_ON_EXIT;
5196 }
5197
5198 static bool nested_exit_on_nmi(struct kvm_vcpu *vcpu)
5199 {
5200 return get_vmcs12(vcpu)->pin_based_vm_exec_control &
5201 PIN_BASED_NMI_EXITING;
5202 }
5203
5204 static void enable_irq_window(struct kvm_vcpu *vcpu)
5205 {
5206 u32 cpu_based_vm_exec_control;
5207
5208 cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
5209 cpu_based_vm_exec_control |= CPU_BASED_VIRTUAL_INTR_PENDING;
5210 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control);
5211 }
5212
5213 static void enable_nmi_window(struct kvm_vcpu *vcpu)
5214 {
5215 u32 cpu_based_vm_exec_control;
5216
5217 if (!cpu_has_virtual_nmis() ||
5218 vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & GUEST_INTR_STATE_STI) {
5219 enable_irq_window(vcpu);
5220 return;
5221 }
5222
5223 cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
5224 cpu_based_vm_exec_control |= CPU_BASED_VIRTUAL_NMI_PENDING;
5225 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control);
5226 }
5227
5228 static void vmx_inject_irq(struct kvm_vcpu *vcpu)
5229 {
5230 struct vcpu_vmx *vmx = to_vmx(vcpu);
5231 uint32_t intr;
5232 int irq = vcpu->arch.interrupt.nr;
5233
5234 trace_kvm_inj_virq(irq);
5235
5236 ++vcpu->stat.irq_injections;
5237 if (vmx->rmode.vm86_active) {
5238 int inc_eip = 0;
5239 if (vcpu->arch.interrupt.soft)
5240 inc_eip = vcpu->arch.event_exit_inst_len;
5241 if (kvm_inject_realmode_interrupt(vcpu, irq, inc_eip) != EMULATE_DONE)
5242 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
5243 return;
5244 }
5245 intr = irq | INTR_INFO_VALID_MASK;
5246 if (vcpu->arch.interrupt.soft) {
5247 intr |= INTR_TYPE_SOFT_INTR;
5248 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
5249 vmx->vcpu.arch.event_exit_inst_len);
5250 } else
5251 intr |= INTR_TYPE_EXT_INTR;
5252 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr);
5253 }
5254
5255 static void vmx_inject_nmi(struct kvm_vcpu *vcpu)
5256 {
5257 struct vcpu_vmx *vmx = to_vmx(vcpu);
5258
5259 if (!is_guest_mode(vcpu)) {
5260 if (!cpu_has_virtual_nmis()) {
5261 /*
5262 * Tracking the NMI-blocked state in software is built upon
5263 * finding the next open IRQ window. This, in turn, depends on
5264 * well-behaving guests: They have to keep IRQs disabled at
5265 * least as long as the NMI handler runs. Otherwise we may
5266 * cause NMI nesting, maybe breaking the guest. But as this is
5267 * highly unlikely, we can live with the residual risk.
5268 */
5269 vmx->soft_vnmi_blocked = 1;
5270 vmx->vnmi_blocked_time = 0;
5271 }
5272
5273 ++vcpu->stat.nmi_injections;
5274 vmx->nmi_known_unmasked = false;
5275 }
5276
5277 if (vmx->rmode.vm86_active) {
5278 if (kvm_inject_realmode_interrupt(vcpu, NMI_VECTOR, 0) != EMULATE_DONE)
5279 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
5280 return;
5281 }
5282
5283 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
5284 INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK | NMI_VECTOR);
5285 }
5286
5287 static bool vmx_get_nmi_mask(struct kvm_vcpu *vcpu)
5288 {
5289 if (!cpu_has_virtual_nmis())
5290 return to_vmx(vcpu)->soft_vnmi_blocked;
5291 if (to_vmx(vcpu)->nmi_known_unmasked)
5292 return false;
5293 return vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & GUEST_INTR_STATE_NMI;
5294 }
5295
5296 static void vmx_set_nmi_mask(struct kvm_vcpu *vcpu, bool masked)
5297 {
5298 struct vcpu_vmx *vmx = to_vmx(vcpu);
5299
5300 if (!cpu_has_virtual_nmis()) {
5301 if (vmx->soft_vnmi_blocked != masked) {
5302 vmx->soft_vnmi_blocked = masked;
5303 vmx->vnmi_blocked_time = 0;
5304 }
5305 } else {
5306 vmx->nmi_known_unmasked = !masked;
5307 if (masked)
5308 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
5309 GUEST_INTR_STATE_NMI);
5310 else
5311 vmcs_clear_bits(GUEST_INTERRUPTIBILITY_INFO,
5312 GUEST_INTR_STATE_NMI);
5313 }
5314 }
5315
5316 static int vmx_nmi_allowed(struct kvm_vcpu *vcpu)
5317 {
5318 if (to_vmx(vcpu)->nested.nested_run_pending)
5319 return 0;
5320
5321 if (!cpu_has_virtual_nmis() && to_vmx(vcpu)->soft_vnmi_blocked)
5322 return 0;
5323
5324 return !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) &
5325 (GUEST_INTR_STATE_MOV_SS | GUEST_INTR_STATE_STI
5326 | GUEST_INTR_STATE_NMI));
5327 }
5328
5329 static int vmx_interrupt_allowed(struct kvm_vcpu *vcpu)
5330 {
5331 return (!to_vmx(vcpu)->nested.nested_run_pending &&
5332 vmcs_readl(GUEST_RFLAGS) & X86_EFLAGS_IF) &&
5333 !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) &
5334 (GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS));
5335 }
5336
5337 static int vmx_set_tss_addr(struct kvm *kvm, unsigned int addr)
5338 {
5339 int ret;
5340
5341 ret = x86_set_memory_region(kvm, TSS_PRIVATE_MEMSLOT, addr,
5342 PAGE_SIZE * 3);
5343 if (ret)
5344 return ret;
5345 kvm->arch.tss_addr = addr;
5346 return init_rmode_tss(kvm);
5347 }
5348
5349 static bool rmode_exception(struct kvm_vcpu *vcpu, int vec)
5350 {
5351 switch (vec) {
5352 case BP_VECTOR:
5353 /*
5354 * Update instruction length as we may reinject the exception
5355 * from user space while in guest debugging mode.
5356 */
5357 to_vmx(vcpu)->vcpu.arch.event_exit_inst_len =
5358 vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
5359 if (vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
5360 return false;
5361 /* fall through */
5362 case DB_VECTOR:
5363 if (vcpu->guest_debug &
5364 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))
5365 return false;
5366 /* fall through */
5367 case DE_VECTOR:
5368 case OF_VECTOR:
5369 case BR_VECTOR:
5370 case UD_VECTOR:
5371 case DF_VECTOR:
5372 case SS_VECTOR:
5373 case GP_VECTOR:
5374 case MF_VECTOR:
5375 return true;
5376 break;
5377 }
5378 return false;
5379 }
5380
5381 static int handle_rmode_exception(struct kvm_vcpu *vcpu,
5382 int vec, u32 err_code)
5383 {
5384 /*
5385 * Instruction with address size override prefix opcode 0x67
5386 * Cause the #SS fault with 0 error code in VM86 mode.
5387 */
5388 if (((vec == GP_VECTOR) || (vec == SS_VECTOR)) && err_code == 0) {
5389 if (emulate_instruction(vcpu, 0) == EMULATE_DONE) {
5390 if (vcpu->arch.halt_request) {
5391 vcpu->arch.halt_request = 0;
5392 return kvm_vcpu_halt(vcpu);
5393 }
5394 return 1;
5395 }
5396 return 0;
5397 }
5398
5399 /*
5400 * Forward all other exceptions that are valid in real mode.
5401 * FIXME: Breaks guest debugging in real mode, needs to be fixed with
5402 * the required debugging infrastructure rework.
5403 */
5404 kvm_queue_exception(vcpu, vec);
5405 return 1;
5406 }
5407
5408 /*
5409 * Trigger machine check on the host. We assume all the MSRs are already set up
5410 * by the CPU and that we still run on the same CPU as the MCE occurred on.
5411 * We pass a fake environment to the machine check handler because we want
5412 * the guest to be always treated like user space, no matter what context
5413 * it used internally.
5414 */
5415 static void kvm_machine_check(void)
5416 {
5417 #if defined(CONFIG_X86_MCE) && defined(CONFIG_X86_64)
5418 struct pt_regs regs = {
5419 .cs = 3, /* Fake ring 3 no matter what the guest ran on */
5420 .flags = X86_EFLAGS_IF,
5421 };
5422
5423 do_machine_check(&regs, 0);
5424 #endif
5425 }
5426
5427 static int handle_machine_check(struct kvm_vcpu *vcpu)
5428 {
5429 /* already handled by vcpu_run */
5430 return 1;
5431 }
5432
5433 static int handle_exception(struct kvm_vcpu *vcpu)
5434 {
5435 struct vcpu_vmx *vmx = to_vmx(vcpu);
5436 struct kvm_run *kvm_run = vcpu->run;
5437 u32 intr_info, ex_no, error_code;
5438 unsigned long cr2, rip, dr6;
5439 u32 vect_info;
5440 enum emulation_result er;
5441
5442 vect_info = vmx->idt_vectoring_info;
5443 intr_info = vmx->exit_intr_info;
5444
5445 if (is_machine_check(intr_info))
5446 return handle_machine_check(vcpu);
5447
5448 if ((intr_info & INTR_INFO_INTR_TYPE_MASK) == INTR_TYPE_NMI_INTR)
5449 return 1; /* already handled by vmx_vcpu_run() */
5450
5451 if (is_no_device(intr_info)) {
5452 vmx_fpu_activate(vcpu);
5453 return 1;
5454 }
5455
5456 if (is_invalid_opcode(intr_info)) {
5457 if (is_guest_mode(vcpu)) {
5458 kvm_queue_exception(vcpu, UD_VECTOR);
5459 return 1;
5460 }
5461 er = emulate_instruction(vcpu, EMULTYPE_TRAP_UD);
5462 if (er != EMULATE_DONE)
5463 kvm_queue_exception(vcpu, UD_VECTOR);
5464 return 1;
5465 }
5466
5467 error_code = 0;
5468 if (intr_info & INTR_INFO_DELIVER_CODE_MASK)
5469 error_code = vmcs_read32(VM_EXIT_INTR_ERROR_CODE);
5470
5471 /*
5472 * The #PF with PFEC.RSVD = 1 indicates the guest is accessing
5473 * MMIO, it is better to report an internal error.
5474 * See the comments in vmx_handle_exit.
5475 */
5476 if ((vect_info & VECTORING_INFO_VALID_MASK) &&
5477 !(is_page_fault(intr_info) && !(error_code & PFERR_RSVD_MASK))) {
5478 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
5479 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_SIMUL_EX;
5480 vcpu->run->internal.ndata = 3;
5481 vcpu->run->internal.data[0] = vect_info;
5482 vcpu->run->internal.data[1] = intr_info;
5483 vcpu->run->internal.data[2] = error_code;
5484 return 0;
5485 }
5486
5487 if (is_page_fault(intr_info)) {
5488 /* EPT won't cause page fault directly */
5489 BUG_ON(enable_ept);
5490 cr2 = vmcs_readl(EXIT_QUALIFICATION);
5491 trace_kvm_page_fault(cr2, error_code);
5492
5493 if (kvm_event_needs_reinjection(vcpu))
5494 kvm_mmu_unprotect_page_virt(vcpu, cr2);
5495 return kvm_mmu_page_fault(vcpu, cr2, error_code, NULL, 0);
5496 }
5497
5498 ex_no = intr_info & INTR_INFO_VECTOR_MASK;
5499
5500 if (vmx->rmode.vm86_active && rmode_exception(vcpu, ex_no))
5501 return handle_rmode_exception(vcpu, ex_no, error_code);
5502
5503 switch (ex_no) {
5504 case AC_VECTOR:
5505 kvm_queue_exception_e(vcpu, AC_VECTOR, error_code);
5506 return 1;
5507 case DB_VECTOR:
5508 dr6 = vmcs_readl(EXIT_QUALIFICATION);
5509 if (!(vcpu->guest_debug &
5510 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))) {
5511 vcpu->arch.dr6 &= ~15;
5512 vcpu->arch.dr6 |= dr6 | DR6_RTM;
5513 if (!(dr6 & ~DR6_RESERVED)) /* icebp */
5514 skip_emulated_instruction(vcpu);
5515
5516 kvm_queue_exception(vcpu, DB_VECTOR);
5517 return 1;
5518 }
5519 kvm_run->debug.arch.dr6 = dr6 | DR6_FIXED_1;
5520 kvm_run->debug.arch.dr7 = vmcs_readl(GUEST_DR7);
5521 /* fall through */
5522 case BP_VECTOR:
5523 /*
5524 * Update instruction length as we may reinject #BP from
5525 * user space while in guest debugging mode. Reading it for
5526 * #DB as well causes no harm, it is not used in that case.
5527 */
5528 vmx->vcpu.arch.event_exit_inst_len =
5529 vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
5530 kvm_run->exit_reason = KVM_EXIT_DEBUG;
5531 rip = kvm_rip_read(vcpu);
5532 kvm_run->debug.arch.pc = vmcs_readl(GUEST_CS_BASE) + rip;
5533 kvm_run->debug.arch.exception = ex_no;
5534 break;
5535 default:
5536 kvm_run->exit_reason = KVM_EXIT_EXCEPTION;
5537 kvm_run->ex.exception = ex_no;
5538 kvm_run->ex.error_code = error_code;
5539 break;
5540 }
5541 return 0;
5542 }
5543
5544 static int handle_external_interrupt(struct kvm_vcpu *vcpu)
5545 {
5546 ++vcpu->stat.irq_exits;
5547 return 1;
5548 }
5549
5550 static int handle_triple_fault(struct kvm_vcpu *vcpu)
5551 {
5552 vcpu->run->exit_reason = KVM_EXIT_SHUTDOWN;
5553 return 0;
5554 }
5555
5556 static int handle_io(struct kvm_vcpu *vcpu)
5557 {
5558 unsigned long exit_qualification;
5559 int size, in, string, ret;
5560 unsigned port;
5561
5562 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
5563 string = (exit_qualification & 16) != 0;
5564 in = (exit_qualification & 8) != 0;
5565
5566 ++vcpu->stat.io_exits;
5567
5568 if (string || in)
5569 return emulate_instruction(vcpu, 0) == EMULATE_DONE;
5570
5571 port = exit_qualification >> 16;
5572 size = (exit_qualification & 7) + 1;
5573
5574 ret = kvm_skip_emulated_instruction(vcpu);
5575
5576 /*
5577 * TODO: we might be squashing a KVM_GUESTDBG_SINGLESTEP-triggered
5578 * KVM_EXIT_DEBUG here.
5579 */
5580 return kvm_fast_pio_out(vcpu, size, port) && ret;
5581 }
5582
5583 static void
5584 vmx_patch_hypercall(struct kvm_vcpu *vcpu, unsigned char *hypercall)
5585 {
5586 /*
5587 * Patch in the VMCALL instruction:
5588 */
5589 hypercall[0] = 0x0f;
5590 hypercall[1] = 0x01;
5591 hypercall[2] = 0xc1;
5592 }
5593
5594 static bool nested_cr0_valid(struct kvm_vcpu *vcpu, unsigned long val)
5595 {
5596 unsigned long always_on = VMXON_CR0_ALWAYSON;
5597 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
5598
5599 if (to_vmx(vcpu)->nested.nested_vmx_secondary_ctls_high &
5600 SECONDARY_EXEC_UNRESTRICTED_GUEST &&
5601 nested_cpu_has2(vmcs12, SECONDARY_EXEC_UNRESTRICTED_GUEST))
5602 always_on &= ~(X86_CR0_PE | X86_CR0_PG);
5603 return (val & always_on) == always_on;
5604 }
5605
5606 /* called to set cr0 as appropriate for a mov-to-cr0 exit. */
5607 static int handle_set_cr0(struct kvm_vcpu *vcpu, unsigned long val)
5608 {
5609 if (is_guest_mode(vcpu)) {
5610 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
5611 unsigned long orig_val = val;
5612
5613 /*
5614 * We get here when L2 changed cr0 in a way that did not change
5615 * any of L1's shadowed bits (see nested_vmx_exit_handled_cr),
5616 * but did change L0 shadowed bits. So we first calculate the
5617 * effective cr0 value that L1 would like to write into the
5618 * hardware. It consists of the L2-owned bits from the new
5619 * value combined with the L1-owned bits from L1's guest_cr0.
5620 */
5621 val = (val & ~vmcs12->cr0_guest_host_mask) |
5622 (vmcs12->guest_cr0 & vmcs12->cr0_guest_host_mask);
5623
5624 if (!nested_cr0_valid(vcpu, val))
5625 return 1;
5626
5627 if (kvm_set_cr0(vcpu, val))
5628 return 1;
5629 vmcs_writel(CR0_READ_SHADOW, orig_val);
5630 return 0;
5631 } else {
5632 if (to_vmx(vcpu)->nested.vmxon &&
5633 ((val & VMXON_CR0_ALWAYSON) != VMXON_CR0_ALWAYSON))
5634 return 1;
5635 return kvm_set_cr0(vcpu, val);
5636 }
5637 }
5638
5639 static int handle_set_cr4(struct kvm_vcpu *vcpu, unsigned long val)
5640 {
5641 if (is_guest_mode(vcpu)) {
5642 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
5643 unsigned long orig_val = val;
5644
5645 /* analogously to handle_set_cr0 */
5646 val = (val & ~vmcs12->cr4_guest_host_mask) |
5647 (vmcs12->guest_cr4 & vmcs12->cr4_guest_host_mask);
5648 if (kvm_set_cr4(vcpu, val))
5649 return 1;
5650 vmcs_writel(CR4_READ_SHADOW, orig_val);
5651 return 0;
5652 } else
5653 return kvm_set_cr4(vcpu, val);
5654 }
5655
5656 /* called to set cr0 as appropriate for clts instruction exit. */
5657 static void handle_clts(struct kvm_vcpu *vcpu)
5658 {
5659 if (is_guest_mode(vcpu)) {
5660 /*
5661 * We get here when L2 did CLTS, and L1 didn't shadow CR0.TS
5662 * but we did (!fpu_active). We need to keep GUEST_CR0.TS on,
5663 * just pretend it's off (also in arch.cr0 for fpu_activate).
5664 */
5665 vmcs_writel(CR0_READ_SHADOW,
5666 vmcs_readl(CR0_READ_SHADOW) & ~X86_CR0_TS);
5667 vcpu->arch.cr0 &= ~X86_CR0_TS;
5668 } else
5669 vmx_set_cr0(vcpu, kvm_read_cr0_bits(vcpu, ~X86_CR0_TS));
5670 }
5671
5672 static int handle_cr(struct kvm_vcpu *vcpu)
5673 {
5674 unsigned long exit_qualification, val;
5675 int cr;
5676 int reg;
5677 int err;
5678 int ret;
5679
5680 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
5681 cr = exit_qualification & 15;
5682 reg = (exit_qualification >> 8) & 15;
5683 switch ((exit_qualification >> 4) & 3) {
5684 case 0: /* mov to cr */
5685 val = kvm_register_readl(vcpu, reg);
5686 trace_kvm_cr_write(cr, val);
5687 switch (cr) {
5688 case 0:
5689 err = handle_set_cr0(vcpu, val);
5690 return kvm_complete_insn_gp(vcpu, err);
5691 case 3:
5692 err = kvm_set_cr3(vcpu, val);
5693 return kvm_complete_insn_gp(vcpu, err);
5694 case 4:
5695 err = handle_set_cr4(vcpu, val);
5696 return kvm_complete_insn_gp(vcpu, err);
5697 case 8: {
5698 u8 cr8_prev = kvm_get_cr8(vcpu);
5699 u8 cr8 = (u8)val;
5700 err = kvm_set_cr8(vcpu, cr8);
5701 ret = kvm_complete_insn_gp(vcpu, err);
5702 if (lapic_in_kernel(vcpu))
5703 return ret;
5704 if (cr8_prev <= cr8)
5705 return ret;
5706 /*
5707 * TODO: we might be squashing a
5708 * KVM_GUESTDBG_SINGLESTEP-triggered
5709 * KVM_EXIT_DEBUG here.
5710 */
5711 vcpu->run->exit_reason = KVM_EXIT_SET_TPR;
5712 return 0;
5713 }
5714 }
5715 break;
5716 case 2: /* clts */
5717 handle_clts(vcpu);
5718 trace_kvm_cr_write(0, kvm_read_cr0(vcpu));
5719 vmx_fpu_activate(vcpu);
5720 return kvm_skip_emulated_instruction(vcpu);
5721 case 1: /*mov from cr*/
5722 switch (cr) {
5723 case 3:
5724 val = kvm_read_cr3(vcpu);
5725 kvm_register_write(vcpu, reg, val);
5726 trace_kvm_cr_read(cr, val);
5727 return kvm_skip_emulated_instruction(vcpu);
5728 case 8:
5729 val = kvm_get_cr8(vcpu);
5730 kvm_register_write(vcpu, reg, val);
5731 trace_kvm_cr_read(cr, val);
5732 return kvm_skip_emulated_instruction(vcpu);
5733 }
5734 break;
5735 case 3: /* lmsw */
5736 val = (exit_qualification >> LMSW_SOURCE_DATA_SHIFT) & 0x0f;
5737 trace_kvm_cr_write(0, (kvm_read_cr0(vcpu) & ~0xful) | val);
5738 kvm_lmsw(vcpu, val);
5739
5740 return kvm_skip_emulated_instruction(vcpu);
5741 default:
5742 break;
5743 }
5744 vcpu->run->exit_reason = 0;
5745 vcpu_unimpl(vcpu, "unhandled control register: op %d cr %d\n",
5746 (int)(exit_qualification >> 4) & 3, cr);
5747 return 0;
5748 }
5749
5750 static int handle_dr(struct kvm_vcpu *vcpu)
5751 {
5752 unsigned long exit_qualification;
5753 int dr, dr7, reg;
5754
5755 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
5756 dr = exit_qualification & DEBUG_REG_ACCESS_NUM;
5757
5758 /* First, if DR does not exist, trigger UD */
5759 if (!kvm_require_dr(vcpu, dr))
5760 return 1;
5761
5762 /* Do not handle if the CPL > 0, will trigger GP on re-entry */
5763 if (!kvm_require_cpl(vcpu, 0))
5764 return 1;
5765 dr7 = vmcs_readl(GUEST_DR7);
5766 if (dr7 & DR7_GD) {
5767 /*
5768 * As the vm-exit takes precedence over the debug trap, we
5769 * need to emulate the latter, either for the host or the
5770 * guest debugging itself.
5771 */
5772 if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP) {
5773 vcpu->run->debug.arch.dr6 = vcpu->arch.dr6;
5774 vcpu->run->debug.arch.dr7 = dr7;
5775 vcpu->run->debug.arch.pc = kvm_get_linear_rip(vcpu);
5776 vcpu->run->debug.arch.exception = DB_VECTOR;
5777 vcpu->run->exit_reason = KVM_EXIT_DEBUG;
5778 return 0;
5779 } else {
5780 vcpu->arch.dr6 &= ~15;
5781 vcpu->arch.dr6 |= DR6_BD | DR6_RTM;
5782 kvm_queue_exception(vcpu, DB_VECTOR);
5783 return 1;
5784 }
5785 }
5786
5787 if (vcpu->guest_debug == 0) {
5788 vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL,
5789 CPU_BASED_MOV_DR_EXITING);
5790
5791 /*
5792 * No more DR vmexits; force a reload of the debug registers
5793 * and reenter on this instruction. The next vmexit will
5794 * retrieve the full state of the debug registers.
5795 */
5796 vcpu->arch.switch_db_regs |= KVM_DEBUGREG_WONT_EXIT;
5797 return 1;
5798 }
5799
5800 reg = DEBUG_REG_ACCESS_REG(exit_qualification);
5801 if (exit_qualification & TYPE_MOV_FROM_DR) {
5802 unsigned long val;
5803
5804 if (kvm_get_dr(vcpu, dr, &val))
5805 return 1;
5806 kvm_register_write(vcpu, reg, val);
5807 } else
5808 if (kvm_set_dr(vcpu, dr, kvm_register_readl(vcpu, reg)))
5809 return 1;
5810
5811 return kvm_skip_emulated_instruction(vcpu);
5812 }
5813
5814 static u64 vmx_get_dr6(struct kvm_vcpu *vcpu)
5815 {
5816 return vcpu->arch.dr6;
5817 }
5818
5819 static void vmx_set_dr6(struct kvm_vcpu *vcpu, unsigned long val)
5820 {
5821 }
5822
5823 static void vmx_sync_dirty_debug_regs(struct kvm_vcpu *vcpu)
5824 {
5825 get_debugreg(vcpu->arch.db[0], 0);
5826 get_debugreg(vcpu->arch.db[1], 1);
5827 get_debugreg(vcpu->arch.db[2], 2);
5828 get_debugreg(vcpu->arch.db[3], 3);
5829 get_debugreg(vcpu->arch.dr6, 6);
5830 vcpu->arch.dr7 = vmcs_readl(GUEST_DR7);
5831
5832 vcpu->arch.switch_db_regs &= ~KVM_DEBUGREG_WONT_EXIT;
5833 vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL, CPU_BASED_MOV_DR_EXITING);
5834 }
5835
5836 static void vmx_set_dr7(struct kvm_vcpu *vcpu, unsigned long val)
5837 {
5838 vmcs_writel(GUEST_DR7, val);
5839 }
5840
5841 static int handle_cpuid(struct kvm_vcpu *vcpu)
5842 {
5843 return kvm_emulate_cpuid(vcpu);
5844 }
5845
5846 static int handle_rdmsr(struct kvm_vcpu *vcpu)
5847 {
5848 u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX];
5849 struct msr_data msr_info;
5850
5851 msr_info.index = ecx;
5852 msr_info.host_initiated = false;
5853 if (vmx_get_msr(vcpu, &msr_info)) {
5854 trace_kvm_msr_read_ex(ecx);
5855 kvm_inject_gp(vcpu, 0);
5856 return 1;
5857 }
5858
5859 trace_kvm_msr_read(ecx, msr_info.data);
5860
5861 /* FIXME: handling of bits 32:63 of rax, rdx */
5862 vcpu->arch.regs[VCPU_REGS_RAX] = msr_info.data & -1u;
5863 vcpu->arch.regs[VCPU_REGS_RDX] = (msr_info.data >> 32) & -1u;
5864 return kvm_skip_emulated_instruction(vcpu);
5865 }
5866
5867 static int handle_wrmsr(struct kvm_vcpu *vcpu)
5868 {
5869 struct msr_data msr;
5870 u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX];
5871 u64 data = (vcpu->arch.regs[VCPU_REGS_RAX] & -1u)
5872 | ((u64)(vcpu->arch.regs[VCPU_REGS_RDX] & -1u) << 32);
5873
5874 msr.data = data;
5875 msr.index = ecx;
5876 msr.host_initiated = false;
5877 if (kvm_set_msr(vcpu, &msr) != 0) {
5878 trace_kvm_msr_write_ex(ecx, data);
5879 kvm_inject_gp(vcpu, 0);
5880 return 1;
5881 }
5882
5883 trace_kvm_msr_write(ecx, data);
5884 return kvm_skip_emulated_instruction(vcpu);
5885 }
5886
5887 static int handle_tpr_below_threshold(struct kvm_vcpu *vcpu)
5888 {
5889 kvm_make_request(KVM_REQ_EVENT, vcpu);
5890 return 1;
5891 }
5892
5893 static int handle_interrupt_window(struct kvm_vcpu *vcpu)
5894 {
5895 u32 cpu_based_vm_exec_control;
5896
5897 /* clear pending irq */
5898 cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
5899 cpu_based_vm_exec_control &= ~CPU_BASED_VIRTUAL_INTR_PENDING;
5900 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control);
5901
5902 kvm_make_request(KVM_REQ_EVENT, vcpu);
5903
5904 ++vcpu->stat.irq_window_exits;
5905 return 1;
5906 }
5907
5908 static int handle_halt(struct kvm_vcpu *vcpu)
5909 {
5910 return kvm_emulate_halt(vcpu);
5911 }
5912
5913 static int handle_vmcall(struct kvm_vcpu *vcpu)
5914 {
5915 return kvm_emulate_hypercall(vcpu);
5916 }
5917
5918 static int handle_invd(struct kvm_vcpu *vcpu)
5919 {
5920 return emulate_instruction(vcpu, 0) == EMULATE_DONE;
5921 }
5922
5923 static int handle_invlpg(struct kvm_vcpu *vcpu)
5924 {
5925 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
5926
5927 kvm_mmu_invlpg(vcpu, exit_qualification);
5928 return kvm_skip_emulated_instruction(vcpu);
5929 }
5930
5931 static int handle_rdpmc(struct kvm_vcpu *vcpu)
5932 {
5933 int err;
5934
5935 err = kvm_rdpmc(vcpu);
5936 return kvm_complete_insn_gp(vcpu, err);
5937 }
5938
5939 static int handle_wbinvd(struct kvm_vcpu *vcpu)
5940 {
5941 return kvm_emulate_wbinvd(vcpu);
5942 }
5943
5944 static int handle_xsetbv(struct kvm_vcpu *vcpu)
5945 {
5946 u64 new_bv = kvm_read_edx_eax(vcpu);
5947 u32 index = kvm_register_read(vcpu, VCPU_REGS_RCX);
5948
5949 if (kvm_set_xcr(vcpu, index, new_bv) == 0)
5950 return kvm_skip_emulated_instruction(vcpu);
5951 return 1;
5952 }
5953
5954 static int handle_xsaves(struct kvm_vcpu *vcpu)
5955 {
5956 kvm_skip_emulated_instruction(vcpu);
5957 WARN(1, "this should never happen\n");
5958 return 1;
5959 }
5960
5961 static int handle_xrstors(struct kvm_vcpu *vcpu)
5962 {
5963 kvm_skip_emulated_instruction(vcpu);
5964 WARN(1, "this should never happen\n");
5965 return 1;
5966 }
5967
5968 static int handle_apic_access(struct kvm_vcpu *vcpu)
5969 {
5970 if (likely(fasteoi)) {
5971 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
5972 int access_type, offset;
5973
5974 access_type = exit_qualification & APIC_ACCESS_TYPE;
5975 offset = exit_qualification & APIC_ACCESS_OFFSET;
5976 /*
5977 * Sane guest uses MOV to write EOI, with written value
5978 * not cared. So make a short-circuit here by avoiding
5979 * heavy instruction emulation.
5980 */
5981 if ((access_type == TYPE_LINEAR_APIC_INST_WRITE) &&
5982 (offset == APIC_EOI)) {
5983 kvm_lapic_set_eoi(vcpu);
5984 return kvm_skip_emulated_instruction(vcpu);
5985 }
5986 }
5987 return emulate_instruction(vcpu, 0) == EMULATE_DONE;
5988 }
5989
5990 static int handle_apic_eoi_induced(struct kvm_vcpu *vcpu)
5991 {
5992 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
5993 int vector = exit_qualification & 0xff;
5994
5995 /* EOI-induced VM exit is trap-like and thus no need to adjust IP */
5996 kvm_apic_set_eoi_accelerated(vcpu, vector);
5997 return 1;
5998 }
5999
6000 static int handle_apic_write(struct kvm_vcpu *vcpu)
6001 {
6002 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
6003 u32 offset = exit_qualification & 0xfff;
6004
6005 /* APIC-write VM exit is trap-like and thus no need to adjust IP */
6006 kvm_apic_write_nodecode(vcpu, offset);
6007 return 1;
6008 }
6009
6010 static int handle_task_switch(struct kvm_vcpu *vcpu)
6011 {
6012 struct vcpu_vmx *vmx = to_vmx(vcpu);
6013 unsigned long exit_qualification;
6014 bool has_error_code = false;
6015 u32 error_code = 0;
6016 u16 tss_selector;
6017 int reason, type, idt_v, idt_index;
6018
6019 idt_v = (vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK);
6020 idt_index = (vmx->idt_vectoring_info & VECTORING_INFO_VECTOR_MASK);
6021 type = (vmx->idt_vectoring_info & VECTORING_INFO_TYPE_MASK);
6022
6023 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
6024
6025 reason = (u32)exit_qualification >> 30;
6026 if (reason == TASK_SWITCH_GATE && idt_v) {
6027 switch (type) {
6028 case INTR_TYPE_NMI_INTR:
6029 vcpu->arch.nmi_injected = false;
6030 vmx_set_nmi_mask(vcpu, true);
6031 break;
6032 case INTR_TYPE_EXT_INTR:
6033 case INTR_TYPE_SOFT_INTR:
6034 kvm_clear_interrupt_queue(vcpu);
6035 break;
6036 case INTR_TYPE_HARD_EXCEPTION:
6037 if (vmx->idt_vectoring_info &
6038 VECTORING_INFO_DELIVER_CODE_MASK) {
6039 has_error_code = true;
6040 error_code =
6041 vmcs_read32(IDT_VECTORING_ERROR_CODE);
6042 }
6043 /* fall through */
6044 case INTR_TYPE_SOFT_EXCEPTION:
6045 kvm_clear_exception_queue(vcpu);
6046 break;
6047 default:
6048 break;
6049 }
6050 }
6051 tss_selector = exit_qualification;
6052
6053 if (!idt_v || (type != INTR_TYPE_HARD_EXCEPTION &&
6054 type != INTR_TYPE_EXT_INTR &&
6055 type != INTR_TYPE_NMI_INTR))
6056 skip_emulated_instruction(vcpu);
6057
6058 if (kvm_task_switch(vcpu, tss_selector,
6059 type == INTR_TYPE_SOFT_INTR ? idt_index : -1, reason,
6060 has_error_code, error_code) == EMULATE_FAIL) {
6061 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
6062 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
6063 vcpu->run->internal.ndata = 0;
6064 return 0;
6065 }
6066
6067 /*
6068 * TODO: What about debug traps on tss switch?
6069 * Are we supposed to inject them and update dr6?
6070 */
6071
6072 return 1;
6073 }
6074
6075 static int handle_ept_violation(struct kvm_vcpu *vcpu)
6076 {
6077 unsigned long exit_qualification;
6078 gpa_t gpa;
6079 u32 error_code;
6080 int gla_validity;
6081
6082 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
6083
6084 gla_validity = (exit_qualification >> 7) & 0x3;
6085 if (gla_validity == 0x2) {
6086 printk(KERN_ERR "EPT: Handling EPT violation failed!\n");
6087 printk(KERN_ERR "EPT: GPA: 0x%lx, GVA: 0x%lx\n",
6088 (long unsigned int)vmcs_read64(GUEST_PHYSICAL_ADDRESS),
6089 vmcs_readl(GUEST_LINEAR_ADDRESS));
6090 printk(KERN_ERR "EPT: Exit qualification is 0x%lx\n",
6091 (long unsigned int)exit_qualification);
6092 vcpu->run->exit_reason = KVM_EXIT_UNKNOWN;
6093 vcpu->run->hw.hardware_exit_reason = EXIT_REASON_EPT_VIOLATION;
6094 return 0;
6095 }
6096
6097 /*
6098 * EPT violation happened while executing iret from NMI,
6099 * "blocked by NMI" bit has to be set before next VM entry.
6100 * There are errata that may cause this bit to not be set:
6101 * AAK134, BY25.
6102 */
6103 if (!(to_vmx(vcpu)->idt_vectoring_info & VECTORING_INFO_VALID_MASK) &&
6104 cpu_has_virtual_nmis() &&
6105 (exit_qualification & INTR_INFO_UNBLOCK_NMI))
6106 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO, GUEST_INTR_STATE_NMI);
6107
6108 gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS);
6109 trace_kvm_page_fault(gpa, exit_qualification);
6110
6111 /* it is a read fault? */
6112 error_code = (exit_qualification << 2) & PFERR_USER_MASK;
6113 /* it is a write fault? */
6114 error_code |= exit_qualification & PFERR_WRITE_MASK;
6115 /* It is a fetch fault? */
6116 error_code |= (exit_qualification << 2) & PFERR_FETCH_MASK;
6117 /* ept page table is present? */
6118 error_code |= (exit_qualification & 0x38) != 0;
6119
6120 vcpu->arch.exit_qualification = exit_qualification;
6121
6122 return kvm_mmu_page_fault(vcpu, gpa, error_code, NULL, 0);
6123 }
6124
6125 static int handle_ept_misconfig(struct kvm_vcpu *vcpu)
6126 {
6127 int ret;
6128 gpa_t gpa;
6129
6130 gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS);
6131 if (!kvm_io_bus_write(vcpu, KVM_FAST_MMIO_BUS, gpa, 0, NULL)) {
6132 trace_kvm_fast_mmio(gpa);
6133 return kvm_skip_emulated_instruction(vcpu);
6134 }
6135
6136 ret = handle_mmio_page_fault(vcpu, gpa, true);
6137 if (likely(ret == RET_MMIO_PF_EMULATE))
6138 return x86_emulate_instruction(vcpu, gpa, 0, NULL, 0) ==
6139 EMULATE_DONE;
6140
6141 if (unlikely(ret == RET_MMIO_PF_INVALID))
6142 return kvm_mmu_page_fault(vcpu, gpa, 0, NULL, 0);
6143
6144 if (unlikely(ret == RET_MMIO_PF_RETRY))
6145 return 1;
6146
6147 /* It is the real ept misconfig */
6148 WARN_ON(1);
6149
6150 vcpu->run->exit_reason = KVM_EXIT_UNKNOWN;
6151 vcpu->run->hw.hardware_exit_reason = EXIT_REASON_EPT_MISCONFIG;
6152
6153 return 0;
6154 }
6155
6156 static int handle_nmi_window(struct kvm_vcpu *vcpu)
6157 {
6158 u32 cpu_based_vm_exec_control;
6159
6160 /* clear pending NMI */
6161 cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
6162 cpu_based_vm_exec_control &= ~CPU_BASED_VIRTUAL_NMI_PENDING;
6163 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control);
6164 ++vcpu->stat.nmi_window_exits;
6165 kvm_make_request(KVM_REQ_EVENT, vcpu);
6166
6167 return 1;
6168 }
6169
6170 static int handle_invalid_guest_state(struct kvm_vcpu *vcpu)
6171 {
6172 struct vcpu_vmx *vmx = to_vmx(vcpu);
6173 enum emulation_result err = EMULATE_DONE;
6174 int ret = 1;
6175 u32 cpu_exec_ctrl;
6176 bool intr_window_requested;
6177 unsigned count = 130;
6178
6179 cpu_exec_ctrl = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
6180 intr_window_requested = cpu_exec_ctrl & CPU_BASED_VIRTUAL_INTR_PENDING;
6181
6182 while (vmx->emulation_required && count-- != 0) {
6183 if (intr_window_requested && vmx_interrupt_allowed(vcpu))
6184 return handle_interrupt_window(&vmx->vcpu);
6185
6186 if (test_bit(KVM_REQ_EVENT, &vcpu->requests))
6187 return 1;
6188
6189 err = emulate_instruction(vcpu, EMULTYPE_NO_REEXECUTE);
6190
6191 if (err == EMULATE_USER_EXIT) {
6192 ++vcpu->stat.mmio_exits;
6193 ret = 0;
6194 goto out;
6195 }
6196
6197 if (err != EMULATE_DONE) {
6198 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
6199 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
6200 vcpu->run->internal.ndata = 0;
6201 return 0;
6202 }
6203
6204 if (vcpu->arch.halt_request) {
6205 vcpu->arch.halt_request = 0;
6206 ret = kvm_vcpu_halt(vcpu);
6207 goto out;
6208 }
6209
6210 if (signal_pending(current))
6211 goto out;
6212 if (need_resched())
6213 schedule();
6214 }
6215
6216 out:
6217 return ret;
6218 }
6219
6220 static int __grow_ple_window(int val)
6221 {
6222 if (ple_window_grow < 1)
6223 return ple_window;
6224
6225 val = min(val, ple_window_actual_max);
6226
6227 if (ple_window_grow < ple_window)
6228 val *= ple_window_grow;
6229 else
6230 val += ple_window_grow;
6231
6232 return val;
6233 }
6234
6235 static int __shrink_ple_window(int val, int modifier, int minimum)
6236 {
6237 if (modifier < 1)
6238 return ple_window;
6239
6240 if (modifier < ple_window)
6241 val /= modifier;
6242 else
6243 val -= modifier;
6244
6245 return max(val, minimum);
6246 }
6247
6248 static void grow_ple_window(struct kvm_vcpu *vcpu)
6249 {
6250 struct vcpu_vmx *vmx = to_vmx(vcpu);
6251 int old = vmx->ple_window;
6252
6253 vmx->ple_window = __grow_ple_window(old);
6254
6255 if (vmx->ple_window != old)
6256 vmx->ple_window_dirty = true;
6257
6258 trace_kvm_ple_window_grow(vcpu->vcpu_id, vmx->ple_window, old);
6259 }
6260
6261 static void shrink_ple_window(struct kvm_vcpu *vcpu)
6262 {
6263 struct vcpu_vmx *vmx = to_vmx(vcpu);
6264 int old = vmx->ple_window;
6265
6266 vmx->ple_window = __shrink_ple_window(old,
6267 ple_window_shrink, ple_window);
6268
6269 if (vmx->ple_window != old)
6270 vmx->ple_window_dirty = true;
6271
6272 trace_kvm_ple_window_shrink(vcpu->vcpu_id, vmx->ple_window, old);
6273 }
6274
6275 /*
6276 * ple_window_actual_max is computed to be one grow_ple_window() below
6277 * ple_window_max. (See __grow_ple_window for the reason.)
6278 * This prevents overflows, because ple_window_max is int.
6279 * ple_window_max effectively rounded down to a multiple of ple_window_grow in
6280 * this process.
6281 * ple_window_max is also prevented from setting vmx->ple_window < ple_window.
6282 */
6283 static void update_ple_window_actual_max(void)
6284 {
6285 ple_window_actual_max =
6286 __shrink_ple_window(max(ple_window_max, ple_window),
6287 ple_window_grow, INT_MIN);
6288 }
6289
6290 /*
6291 * Handler for POSTED_INTERRUPT_WAKEUP_VECTOR.
6292 */
6293 static void wakeup_handler(void)
6294 {
6295 struct kvm_vcpu *vcpu;
6296 int cpu = smp_processor_id();
6297
6298 spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, cpu));
6299 list_for_each_entry(vcpu, &per_cpu(blocked_vcpu_on_cpu, cpu),
6300 blocked_vcpu_list) {
6301 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
6302
6303 if (pi_test_on(pi_desc) == 1)
6304 kvm_vcpu_kick(vcpu);
6305 }
6306 spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, cpu));
6307 }
6308
6309 static __init int hardware_setup(void)
6310 {
6311 int r = -ENOMEM, i, msr;
6312
6313 rdmsrl_safe(MSR_EFER, &host_efer);
6314
6315 for (i = 0; i < ARRAY_SIZE(vmx_msr_index); ++i)
6316 kvm_define_shared_msr(i, vmx_msr_index[i]);
6317
6318 for (i = 0; i < VMX_BITMAP_NR; i++) {
6319 vmx_bitmap[i] = (unsigned long *)__get_free_page(GFP_KERNEL);
6320 if (!vmx_bitmap[i])
6321 goto out;
6322 }
6323
6324 vmx_io_bitmap_b = (unsigned long *)__get_free_page(GFP_KERNEL);
6325 memset(vmx_vmread_bitmap, 0xff, PAGE_SIZE);
6326 memset(vmx_vmwrite_bitmap, 0xff, PAGE_SIZE);
6327
6328 /*
6329 * Allow direct access to the PC debug port (it is often used for I/O
6330 * delays, but the vmexits simply slow things down).
6331 */
6332 memset(vmx_io_bitmap_a, 0xff, PAGE_SIZE);
6333 clear_bit(0x80, vmx_io_bitmap_a);
6334
6335 memset(vmx_io_bitmap_b, 0xff, PAGE_SIZE);
6336
6337 memset(vmx_msr_bitmap_legacy, 0xff, PAGE_SIZE);
6338 memset(vmx_msr_bitmap_longmode, 0xff, PAGE_SIZE);
6339
6340 if (setup_vmcs_config(&vmcs_config) < 0) {
6341 r = -EIO;
6342 goto out;
6343 }
6344
6345 if (boot_cpu_has(X86_FEATURE_NX))
6346 kvm_enable_efer_bits(EFER_NX);
6347
6348 if (!cpu_has_vmx_vpid())
6349 enable_vpid = 0;
6350 if (!cpu_has_vmx_shadow_vmcs())
6351 enable_shadow_vmcs = 0;
6352 if (enable_shadow_vmcs)
6353 init_vmcs_shadow_fields();
6354
6355 if (!cpu_has_vmx_ept() ||
6356 !cpu_has_vmx_ept_4levels()) {
6357 enable_ept = 0;
6358 enable_unrestricted_guest = 0;
6359 enable_ept_ad_bits = 0;
6360 }
6361
6362 if (!cpu_has_vmx_ept_ad_bits())
6363 enable_ept_ad_bits = 0;
6364
6365 if (!cpu_has_vmx_unrestricted_guest())
6366 enable_unrestricted_guest = 0;
6367
6368 if (!cpu_has_vmx_flexpriority())
6369 flexpriority_enabled = 0;
6370
6371 /*
6372 * set_apic_access_page_addr() is used to reload apic access
6373 * page upon invalidation. No need to do anything if not
6374 * using the APIC_ACCESS_ADDR VMCS field.
6375 */
6376 if (!flexpriority_enabled)
6377 kvm_x86_ops->set_apic_access_page_addr = NULL;
6378
6379 if (!cpu_has_vmx_tpr_shadow())
6380 kvm_x86_ops->update_cr8_intercept = NULL;
6381
6382 if (enable_ept && !cpu_has_vmx_ept_2m_page())
6383 kvm_disable_largepages();
6384
6385 if (!cpu_has_vmx_ple())
6386 ple_gap = 0;
6387
6388 if (!cpu_has_vmx_apicv())
6389 enable_apicv = 0;
6390
6391 if (cpu_has_vmx_tsc_scaling()) {
6392 kvm_has_tsc_control = true;
6393 kvm_max_tsc_scaling_ratio = KVM_VMX_TSC_MULTIPLIER_MAX;
6394 kvm_tsc_scaling_ratio_frac_bits = 48;
6395 }
6396
6397 vmx_disable_intercept_for_msr(MSR_FS_BASE, false);
6398 vmx_disable_intercept_for_msr(MSR_GS_BASE, false);
6399 vmx_disable_intercept_for_msr(MSR_KERNEL_GS_BASE, true);
6400 vmx_disable_intercept_for_msr(MSR_IA32_SYSENTER_CS, false);
6401 vmx_disable_intercept_for_msr(MSR_IA32_SYSENTER_ESP, false);
6402 vmx_disable_intercept_for_msr(MSR_IA32_SYSENTER_EIP, false);
6403 vmx_disable_intercept_for_msr(MSR_IA32_BNDCFGS, true);
6404
6405 memcpy(vmx_msr_bitmap_legacy_x2apic_apicv,
6406 vmx_msr_bitmap_legacy, PAGE_SIZE);
6407 memcpy(vmx_msr_bitmap_longmode_x2apic_apicv,
6408 vmx_msr_bitmap_longmode, PAGE_SIZE);
6409 memcpy(vmx_msr_bitmap_legacy_x2apic,
6410 vmx_msr_bitmap_legacy, PAGE_SIZE);
6411 memcpy(vmx_msr_bitmap_longmode_x2apic,
6412 vmx_msr_bitmap_longmode, PAGE_SIZE);
6413
6414 set_bit(0, vmx_vpid_bitmap); /* 0 is reserved for host */
6415
6416 for (msr = 0x800; msr <= 0x8ff; msr++) {
6417 if (msr == 0x839 /* TMCCT */)
6418 continue;
6419 vmx_disable_intercept_msr_x2apic(msr, MSR_TYPE_R, true);
6420 }
6421
6422 /*
6423 * TPR reads and writes can be virtualized even if virtual interrupt
6424 * delivery is not in use.
6425 */
6426 vmx_disable_intercept_msr_x2apic(0x808, MSR_TYPE_W, true);
6427 vmx_disable_intercept_msr_x2apic(0x808, MSR_TYPE_R | MSR_TYPE_W, false);
6428
6429 /* EOI */
6430 vmx_disable_intercept_msr_x2apic(0x80b, MSR_TYPE_W, true);
6431 /* SELF-IPI */
6432 vmx_disable_intercept_msr_x2apic(0x83f, MSR_TYPE_W, true);
6433
6434 if (enable_ept) {
6435 kvm_mmu_set_mask_ptes(VMX_EPT_READABLE_MASK,
6436 (enable_ept_ad_bits) ? VMX_EPT_ACCESS_BIT : 0ull,
6437 (enable_ept_ad_bits) ? VMX_EPT_DIRTY_BIT : 0ull,
6438 0ull, VMX_EPT_EXECUTABLE_MASK,
6439 cpu_has_vmx_ept_execute_only() ?
6440 0ull : VMX_EPT_READABLE_MASK);
6441 ept_set_mmio_spte_mask();
6442 kvm_enable_tdp();
6443 } else
6444 kvm_disable_tdp();
6445
6446 update_ple_window_actual_max();
6447
6448 /*
6449 * Only enable PML when hardware supports PML feature, and both EPT
6450 * and EPT A/D bit features are enabled -- PML depends on them to work.
6451 */
6452 if (!enable_ept || !enable_ept_ad_bits || !cpu_has_vmx_pml())
6453 enable_pml = 0;
6454
6455 if (!enable_pml) {
6456 kvm_x86_ops->slot_enable_log_dirty = NULL;
6457 kvm_x86_ops->slot_disable_log_dirty = NULL;
6458 kvm_x86_ops->flush_log_dirty = NULL;
6459 kvm_x86_ops->enable_log_dirty_pt_masked = NULL;
6460 }
6461
6462 if (cpu_has_vmx_preemption_timer() && enable_preemption_timer) {
6463 u64 vmx_msr;
6464
6465 rdmsrl(MSR_IA32_VMX_MISC, vmx_msr);
6466 cpu_preemption_timer_multi =
6467 vmx_msr & VMX_MISC_PREEMPTION_TIMER_RATE_MASK;
6468 } else {
6469 kvm_x86_ops->set_hv_timer = NULL;
6470 kvm_x86_ops->cancel_hv_timer = NULL;
6471 }
6472
6473 kvm_set_posted_intr_wakeup_handler(wakeup_handler);
6474
6475 kvm_mce_cap_supported |= MCG_LMCE_P;
6476
6477 return alloc_kvm_area();
6478
6479 out:
6480 for (i = 0; i < VMX_BITMAP_NR; i++)
6481 free_page((unsigned long)vmx_bitmap[i]);
6482
6483 return r;
6484 }
6485
6486 static __exit void hardware_unsetup(void)
6487 {
6488 int i;
6489
6490 for (i = 0; i < VMX_BITMAP_NR; i++)
6491 free_page((unsigned long)vmx_bitmap[i]);
6492
6493 free_kvm_area();
6494 }
6495
6496 /*
6497 * Indicate a busy-waiting vcpu in spinlock. We do not enable the PAUSE
6498 * exiting, so only get here on cpu with PAUSE-Loop-Exiting.
6499 */
6500 static int handle_pause(struct kvm_vcpu *vcpu)
6501 {
6502 if (ple_gap)
6503 grow_ple_window(vcpu);
6504
6505 kvm_vcpu_on_spin(vcpu);
6506 return kvm_skip_emulated_instruction(vcpu);
6507 }
6508
6509 static int handle_nop(struct kvm_vcpu *vcpu)
6510 {
6511 return kvm_skip_emulated_instruction(vcpu);
6512 }
6513
6514 static int handle_mwait(struct kvm_vcpu *vcpu)
6515 {
6516 printk_once(KERN_WARNING "kvm: MWAIT instruction emulated as NOP!\n");
6517 return handle_nop(vcpu);
6518 }
6519
6520 static int handle_monitor_trap(struct kvm_vcpu *vcpu)
6521 {
6522 return 1;
6523 }
6524
6525 static int handle_monitor(struct kvm_vcpu *vcpu)
6526 {
6527 printk_once(KERN_WARNING "kvm: MONITOR instruction emulated as NOP!\n");
6528 return handle_nop(vcpu);
6529 }
6530
6531 /*
6532 * To run an L2 guest, we need a vmcs02 based on the L1-specified vmcs12.
6533 * We could reuse a single VMCS for all the L2 guests, but we also want the
6534 * option to allocate a separate vmcs02 for each separate loaded vmcs12 - this
6535 * allows keeping them loaded on the processor, and in the future will allow
6536 * optimizations where prepare_vmcs02 doesn't need to set all the fields on
6537 * every entry if they never change.
6538 * So we keep, in vmx->nested.vmcs02_pool, a cache of size VMCS02_POOL_SIZE
6539 * (>=0) with a vmcs02 for each recently loaded vmcs12s, most recent first.
6540 *
6541 * The following functions allocate and free a vmcs02 in this pool.
6542 */
6543
6544 /* Get a VMCS from the pool to use as vmcs02 for the current vmcs12. */
6545 static struct loaded_vmcs *nested_get_current_vmcs02(struct vcpu_vmx *vmx)
6546 {
6547 struct vmcs02_list *item;
6548 list_for_each_entry(item, &vmx->nested.vmcs02_pool, list)
6549 if (item->vmptr == vmx->nested.current_vmptr) {
6550 list_move(&item->list, &vmx->nested.vmcs02_pool);
6551 return &item->vmcs02;
6552 }
6553
6554 if (vmx->nested.vmcs02_num >= max(VMCS02_POOL_SIZE, 1)) {
6555 /* Recycle the least recently used VMCS. */
6556 item = list_last_entry(&vmx->nested.vmcs02_pool,
6557 struct vmcs02_list, list);
6558 item->vmptr = vmx->nested.current_vmptr;
6559 list_move(&item->list, &vmx->nested.vmcs02_pool);
6560 return &item->vmcs02;
6561 }
6562
6563 /* Create a new VMCS */
6564 item = kmalloc(sizeof(struct vmcs02_list), GFP_KERNEL);
6565 if (!item)
6566 return NULL;
6567 item->vmcs02.vmcs = alloc_vmcs();
6568 item->vmcs02.shadow_vmcs = NULL;
6569 if (!item->vmcs02.vmcs) {
6570 kfree(item);
6571 return NULL;
6572 }
6573 loaded_vmcs_init(&item->vmcs02);
6574 item->vmptr = vmx->nested.current_vmptr;
6575 list_add(&(item->list), &(vmx->nested.vmcs02_pool));
6576 vmx->nested.vmcs02_num++;
6577 return &item->vmcs02;
6578 }
6579
6580 /* Free and remove from pool a vmcs02 saved for a vmcs12 (if there is one) */
6581 static void nested_free_vmcs02(struct vcpu_vmx *vmx, gpa_t vmptr)
6582 {
6583 struct vmcs02_list *item;
6584 list_for_each_entry(item, &vmx->nested.vmcs02_pool, list)
6585 if (item->vmptr == vmptr) {
6586 free_loaded_vmcs(&item->vmcs02);
6587 list_del(&item->list);
6588 kfree(item);
6589 vmx->nested.vmcs02_num--;
6590 return;
6591 }
6592 }
6593
6594 /*
6595 * Free all VMCSs saved for this vcpu, except the one pointed by
6596 * vmx->loaded_vmcs. We must be running L1, so vmx->loaded_vmcs
6597 * must be &vmx->vmcs01.
6598 */
6599 static void nested_free_all_saved_vmcss(struct vcpu_vmx *vmx)
6600 {
6601 struct vmcs02_list *item, *n;
6602
6603 WARN_ON(vmx->loaded_vmcs != &vmx->vmcs01);
6604 list_for_each_entry_safe(item, n, &vmx->nested.vmcs02_pool, list) {
6605 /*
6606 * Something will leak if the above WARN triggers. Better than
6607 * a use-after-free.
6608 */
6609 if (vmx->loaded_vmcs == &item->vmcs02)
6610 continue;
6611
6612 free_loaded_vmcs(&item->vmcs02);
6613 list_del(&item->list);
6614 kfree(item);
6615 vmx->nested.vmcs02_num--;
6616 }
6617 }
6618
6619 /*
6620 * The following 3 functions, nested_vmx_succeed()/failValid()/failInvalid(),
6621 * set the success or error code of an emulated VMX instruction, as specified
6622 * by Vol 2B, VMX Instruction Reference, "Conventions".
6623 */
6624 static void nested_vmx_succeed(struct kvm_vcpu *vcpu)
6625 {
6626 vmx_set_rflags(vcpu, vmx_get_rflags(vcpu)
6627 & ~(X86_EFLAGS_CF | X86_EFLAGS_PF | X86_EFLAGS_AF |
6628 X86_EFLAGS_ZF | X86_EFLAGS_SF | X86_EFLAGS_OF));
6629 }
6630
6631 static void nested_vmx_failInvalid(struct kvm_vcpu *vcpu)
6632 {
6633 vmx_set_rflags(vcpu, (vmx_get_rflags(vcpu)
6634 & ~(X86_EFLAGS_PF | X86_EFLAGS_AF | X86_EFLAGS_ZF |
6635 X86_EFLAGS_SF | X86_EFLAGS_OF))
6636 | X86_EFLAGS_CF);
6637 }
6638
6639 static void nested_vmx_failValid(struct kvm_vcpu *vcpu,
6640 u32 vm_instruction_error)
6641 {
6642 if (to_vmx(vcpu)->nested.current_vmptr == -1ull) {
6643 /*
6644 * failValid writes the error number to the current VMCS, which
6645 * can't be done there isn't a current VMCS.
6646 */
6647 nested_vmx_failInvalid(vcpu);
6648 return;
6649 }
6650 vmx_set_rflags(vcpu, (vmx_get_rflags(vcpu)
6651 & ~(X86_EFLAGS_CF | X86_EFLAGS_PF | X86_EFLAGS_AF |
6652 X86_EFLAGS_SF | X86_EFLAGS_OF))
6653 | X86_EFLAGS_ZF);
6654 get_vmcs12(vcpu)->vm_instruction_error = vm_instruction_error;
6655 /*
6656 * We don't need to force a shadow sync because
6657 * VM_INSTRUCTION_ERROR is not shadowed
6658 */
6659 }
6660
6661 static void nested_vmx_abort(struct kvm_vcpu *vcpu, u32 indicator)
6662 {
6663 /* TODO: not to reset guest simply here. */
6664 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
6665 pr_debug_ratelimited("kvm: nested vmx abort, indicator %d\n", indicator);
6666 }
6667
6668 static enum hrtimer_restart vmx_preemption_timer_fn(struct hrtimer *timer)
6669 {
6670 struct vcpu_vmx *vmx =
6671 container_of(timer, struct vcpu_vmx, nested.preemption_timer);
6672
6673 vmx->nested.preemption_timer_expired = true;
6674 kvm_make_request(KVM_REQ_EVENT, &vmx->vcpu);
6675 kvm_vcpu_kick(&vmx->vcpu);
6676
6677 return HRTIMER_NORESTART;
6678 }
6679
6680 /*
6681 * Decode the memory-address operand of a vmx instruction, as recorded on an
6682 * exit caused by such an instruction (run by a guest hypervisor).
6683 * On success, returns 0. When the operand is invalid, returns 1 and throws
6684 * #UD or #GP.
6685 */
6686 static int get_vmx_mem_address(struct kvm_vcpu *vcpu,
6687 unsigned long exit_qualification,
6688 u32 vmx_instruction_info, bool wr, gva_t *ret)
6689 {
6690 gva_t off;
6691 bool exn;
6692 struct kvm_segment s;
6693
6694 /*
6695 * According to Vol. 3B, "Information for VM Exits Due to Instruction
6696 * Execution", on an exit, vmx_instruction_info holds most of the
6697 * addressing components of the operand. Only the displacement part
6698 * is put in exit_qualification (see 3B, "Basic VM-Exit Information").
6699 * For how an actual address is calculated from all these components,
6700 * refer to Vol. 1, "Operand Addressing".
6701 */
6702 int scaling = vmx_instruction_info & 3;
6703 int addr_size = (vmx_instruction_info >> 7) & 7;
6704 bool is_reg = vmx_instruction_info & (1u << 10);
6705 int seg_reg = (vmx_instruction_info >> 15) & 7;
6706 int index_reg = (vmx_instruction_info >> 18) & 0xf;
6707 bool index_is_valid = !(vmx_instruction_info & (1u << 22));
6708 int base_reg = (vmx_instruction_info >> 23) & 0xf;
6709 bool base_is_valid = !(vmx_instruction_info & (1u << 27));
6710
6711 if (is_reg) {
6712 kvm_queue_exception(vcpu, UD_VECTOR);
6713 return 1;
6714 }
6715
6716 /* Addr = segment_base + offset */
6717 /* offset = base + [index * scale] + displacement */
6718 off = exit_qualification; /* holds the displacement */
6719 if (base_is_valid)
6720 off += kvm_register_read(vcpu, base_reg);
6721 if (index_is_valid)
6722 off += kvm_register_read(vcpu, index_reg)<<scaling;
6723 vmx_get_segment(vcpu, &s, seg_reg);
6724 *ret = s.base + off;
6725
6726 if (addr_size == 1) /* 32 bit */
6727 *ret &= 0xffffffff;
6728
6729 /* Checks for #GP/#SS exceptions. */
6730 exn = false;
6731 if (is_long_mode(vcpu)) {
6732 /* Long mode: #GP(0)/#SS(0) if the memory address is in a
6733 * non-canonical form. This is the only check on the memory
6734 * destination for long mode!
6735 */
6736 exn = is_noncanonical_address(*ret);
6737 } else if (is_protmode(vcpu)) {
6738 /* Protected mode: apply checks for segment validity in the
6739 * following order:
6740 * - segment type check (#GP(0) may be thrown)
6741 * - usability check (#GP(0)/#SS(0))
6742 * - limit check (#GP(0)/#SS(0))
6743 */
6744 if (wr)
6745 /* #GP(0) if the destination operand is located in a
6746 * read-only data segment or any code segment.
6747 */
6748 exn = ((s.type & 0xa) == 0 || (s.type & 8));
6749 else
6750 /* #GP(0) if the source operand is located in an
6751 * execute-only code segment
6752 */
6753 exn = ((s.type & 0xa) == 8);
6754 if (exn) {
6755 kvm_queue_exception_e(vcpu, GP_VECTOR, 0);
6756 return 1;
6757 }
6758 /* Protected mode: #GP(0)/#SS(0) if the segment is unusable.
6759 */
6760 exn = (s.unusable != 0);
6761 /* Protected mode: #GP(0)/#SS(0) if the memory
6762 * operand is outside the segment limit.
6763 */
6764 exn = exn || (off + sizeof(u64) > s.limit);
6765 }
6766 if (exn) {
6767 kvm_queue_exception_e(vcpu,
6768 seg_reg == VCPU_SREG_SS ?
6769 SS_VECTOR : GP_VECTOR,
6770 0);
6771 return 1;
6772 }
6773
6774 return 0;
6775 }
6776
6777 /*
6778 * This function performs the various checks including
6779 * - if it's 4KB aligned
6780 * - No bits beyond the physical address width are set
6781 * - Returns 0 on success or else 1
6782 * (Intel SDM Section 30.3)
6783 */
6784 static int nested_vmx_check_vmptr(struct kvm_vcpu *vcpu, int exit_reason,
6785 gpa_t *vmpointer)
6786 {
6787 gva_t gva;
6788 gpa_t vmptr;
6789 struct x86_exception e;
6790 struct page *page;
6791 struct vcpu_vmx *vmx = to_vmx(vcpu);
6792 int maxphyaddr = cpuid_maxphyaddr(vcpu);
6793
6794 if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
6795 vmcs_read32(VMX_INSTRUCTION_INFO), false, &gva))
6796 return 1;
6797
6798 if (kvm_read_guest_virt(&vcpu->arch.emulate_ctxt, gva, &vmptr,
6799 sizeof(vmptr), &e)) {
6800 kvm_inject_page_fault(vcpu, &e);
6801 return 1;
6802 }
6803
6804 switch (exit_reason) {
6805 case EXIT_REASON_VMON:
6806 /*
6807 * SDM 3: 24.11.5
6808 * The first 4 bytes of VMXON region contain the supported
6809 * VMCS revision identifier
6810 *
6811 * Note - IA32_VMX_BASIC[48] will never be 1
6812 * for the nested case;
6813 * which replaces physical address width with 32
6814 *
6815 */
6816 if (!PAGE_ALIGNED(vmptr) || (vmptr >> maxphyaddr)) {
6817 nested_vmx_failInvalid(vcpu);
6818 return kvm_skip_emulated_instruction(vcpu);
6819 }
6820
6821 page = nested_get_page(vcpu, vmptr);
6822 if (page == NULL ||
6823 *(u32 *)kmap(page) != VMCS12_REVISION) {
6824 nested_vmx_failInvalid(vcpu);
6825 kunmap(page);
6826 return kvm_skip_emulated_instruction(vcpu);
6827 }
6828 kunmap(page);
6829 vmx->nested.vmxon_ptr = vmptr;
6830 break;
6831 case EXIT_REASON_VMCLEAR:
6832 if (!PAGE_ALIGNED(vmptr) || (vmptr >> maxphyaddr)) {
6833 nested_vmx_failValid(vcpu,
6834 VMXERR_VMCLEAR_INVALID_ADDRESS);
6835 return kvm_skip_emulated_instruction(vcpu);
6836 }
6837
6838 if (vmptr == vmx->nested.vmxon_ptr) {
6839 nested_vmx_failValid(vcpu,
6840 VMXERR_VMCLEAR_VMXON_POINTER);
6841 return kvm_skip_emulated_instruction(vcpu);
6842 }
6843 break;
6844 case EXIT_REASON_VMPTRLD:
6845 if (!PAGE_ALIGNED(vmptr) || (vmptr >> maxphyaddr)) {
6846 nested_vmx_failValid(vcpu,
6847 VMXERR_VMPTRLD_INVALID_ADDRESS);
6848 return kvm_skip_emulated_instruction(vcpu);
6849 }
6850
6851 if (vmptr == vmx->nested.vmxon_ptr) {
6852 nested_vmx_failValid(vcpu,
6853 VMXERR_VMCLEAR_VMXON_POINTER);
6854 return kvm_skip_emulated_instruction(vcpu);
6855 }
6856 break;
6857 default:
6858 return 1; /* shouldn't happen */
6859 }
6860
6861 if (vmpointer)
6862 *vmpointer = vmptr;
6863 return 0;
6864 }
6865
6866 /*
6867 * Emulate the VMXON instruction.
6868 * Currently, we just remember that VMX is active, and do not save or even
6869 * inspect the argument to VMXON (the so-called "VMXON pointer") because we
6870 * do not currently need to store anything in that guest-allocated memory
6871 * region. Consequently, VMCLEAR and VMPTRLD also do not verify that the their
6872 * argument is different from the VMXON pointer (which the spec says they do).
6873 */
6874 static int handle_vmon(struct kvm_vcpu *vcpu)
6875 {
6876 struct kvm_segment cs;
6877 struct vcpu_vmx *vmx = to_vmx(vcpu);
6878 struct vmcs *shadow_vmcs;
6879 const u64 VMXON_NEEDED_FEATURES = FEATURE_CONTROL_LOCKED
6880 | FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
6881
6882 /* The Intel VMX Instruction Reference lists a bunch of bits that
6883 * are prerequisite to running VMXON, most notably cr4.VMXE must be
6884 * set to 1 (see vmx_set_cr4() for when we allow the guest to set this).
6885 * Otherwise, we should fail with #UD. We test these now:
6886 */
6887 if (!kvm_read_cr4_bits(vcpu, X86_CR4_VMXE) ||
6888 !kvm_read_cr0_bits(vcpu, X86_CR0_PE) ||
6889 (vmx_get_rflags(vcpu) & X86_EFLAGS_VM)) {
6890 kvm_queue_exception(vcpu, UD_VECTOR);
6891 return 1;
6892 }
6893
6894 vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
6895 if (is_long_mode(vcpu) && !cs.l) {
6896 kvm_queue_exception(vcpu, UD_VECTOR);
6897 return 1;
6898 }
6899
6900 if (vmx_get_cpl(vcpu)) {
6901 kvm_inject_gp(vcpu, 0);
6902 return 1;
6903 }
6904
6905 if (nested_vmx_check_vmptr(vcpu, EXIT_REASON_VMON, NULL))
6906 return 1;
6907
6908 if (vmx->nested.vmxon) {
6909 nested_vmx_failValid(vcpu, VMXERR_VMXON_IN_VMX_ROOT_OPERATION);
6910 return kvm_skip_emulated_instruction(vcpu);
6911 }
6912
6913 if ((vmx->msr_ia32_feature_control & VMXON_NEEDED_FEATURES)
6914 != VMXON_NEEDED_FEATURES) {
6915 kvm_inject_gp(vcpu, 0);
6916 return 1;
6917 }
6918
6919 if (cpu_has_vmx_msr_bitmap()) {
6920 vmx->nested.msr_bitmap =
6921 (unsigned long *)__get_free_page(GFP_KERNEL);
6922 if (!vmx->nested.msr_bitmap)
6923 goto out_msr_bitmap;
6924 }
6925
6926 vmx->nested.cached_vmcs12 = kmalloc(VMCS12_SIZE, GFP_KERNEL);
6927 if (!vmx->nested.cached_vmcs12)
6928 goto out_cached_vmcs12;
6929
6930 if (enable_shadow_vmcs) {
6931 shadow_vmcs = alloc_vmcs();
6932 if (!shadow_vmcs)
6933 goto out_shadow_vmcs;
6934 /* mark vmcs as shadow */
6935 shadow_vmcs->revision_id |= (1u << 31);
6936 /* init shadow vmcs */
6937 vmcs_clear(shadow_vmcs);
6938 vmx->vmcs01.shadow_vmcs = shadow_vmcs;
6939 }
6940
6941 INIT_LIST_HEAD(&(vmx->nested.vmcs02_pool));
6942 vmx->nested.vmcs02_num = 0;
6943
6944 hrtimer_init(&vmx->nested.preemption_timer, CLOCK_MONOTONIC,
6945 HRTIMER_MODE_REL_PINNED);
6946 vmx->nested.preemption_timer.function = vmx_preemption_timer_fn;
6947
6948 vmx->nested.vmxon = true;
6949
6950 nested_vmx_succeed(vcpu);
6951 return kvm_skip_emulated_instruction(vcpu);
6952
6953 out_shadow_vmcs:
6954 kfree(vmx->nested.cached_vmcs12);
6955
6956 out_cached_vmcs12:
6957 free_page((unsigned long)vmx->nested.msr_bitmap);
6958
6959 out_msr_bitmap:
6960 return -ENOMEM;
6961 }
6962
6963 /*
6964 * Intel's VMX Instruction Reference specifies a common set of prerequisites
6965 * for running VMX instructions (except VMXON, whose prerequisites are
6966 * slightly different). It also specifies what exception to inject otherwise.
6967 */
6968 static int nested_vmx_check_permission(struct kvm_vcpu *vcpu)
6969 {
6970 struct kvm_segment cs;
6971 struct vcpu_vmx *vmx = to_vmx(vcpu);
6972
6973 if (!vmx->nested.vmxon) {
6974 kvm_queue_exception(vcpu, UD_VECTOR);
6975 return 0;
6976 }
6977
6978 vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
6979 if ((vmx_get_rflags(vcpu) & X86_EFLAGS_VM) ||
6980 (is_long_mode(vcpu) && !cs.l)) {
6981 kvm_queue_exception(vcpu, UD_VECTOR);
6982 return 0;
6983 }
6984
6985 if (vmx_get_cpl(vcpu)) {
6986 kvm_inject_gp(vcpu, 0);
6987 return 0;
6988 }
6989
6990 return 1;
6991 }
6992
6993 static inline void nested_release_vmcs12(struct vcpu_vmx *vmx)
6994 {
6995 if (vmx->nested.current_vmptr == -1ull)
6996 return;
6997
6998 /* current_vmptr and current_vmcs12 are always set/reset together */
6999 if (WARN_ON(vmx->nested.current_vmcs12 == NULL))
7000 return;
7001
7002 if (enable_shadow_vmcs) {
7003 /* copy to memory all shadowed fields in case
7004 they were modified */
7005 copy_shadow_to_vmcs12(vmx);
7006 vmx->nested.sync_shadow_vmcs = false;
7007 vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL,
7008 SECONDARY_EXEC_SHADOW_VMCS);
7009 vmcs_write64(VMCS_LINK_POINTER, -1ull);
7010 }
7011 vmx->nested.posted_intr_nv = -1;
7012
7013 /* Flush VMCS12 to guest memory */
7014 memcpy(vmx->nested.current_vmcs12, vmx->nested.cached_vmcs12,
7015 VMCS12_SIZE);
7016
7017 kunmap(vmx->nested.current_vmcs12_page);
7018 nested_release_page(vmx->nested.current_vmcs12_page);
7019 vmx->nested.current_vmptr = -1ull;
7020 vmx->nested.current_vmcs12 = NULL;
7021 }
7022
7023 /*
7024 * Free whatever needs to be freed from vmx->nested when L1 goes down, or
7025 * just stops using VMX.
7026 */
7027 static void free_nested(struct vcpu_vmx *vmx)
7028 {
7029 if (!vmx->nested.vmxon)
7030 return;
7031
7032 vmx->nested.vmxon = false;
7033 free_vpid(vmx->nested.vpid02);
7034 nested_release_vmcs12(vmx);
7035 if (vmx->nested.msr_bitmap) {
7036 free_page((unsigned long)vmx->nested.msr_bitmap);
7037 vmx->nested.msr_bitmap = NULL;
7038 }
7039 if (enable_shadow_vmcs) {
7040 vmcs_clear(vmx->vmcs01.shadow_vmcs);
7041 free_vmcs(vmx->vmcs01.shadow_vmcs);
7042 vmx->vmcs01.shadow_vmcs = NULL;
7043 }
7044 kfree(vmx->nested.cached_vmcs12);
7045 /* Unpin physical memory we referred to in current vmcs02 */
7046 if (vmx->nested.apic_access_page) {
7047 nested_release_page(vmx->nested.apic_access_page);
7048 vmx->nested.apic_access_page = NULL;
7049 }
7050 if (vmx->nested.virtual_apic_page) {
7051 nested_release_page(vmx->nested.virtual_apic_page);
7052 vmx->nested.virtual_apic_page = NULL;
7053 }
7054 if (vmx->nested.pi_desc_page) {
7055 kunmap(vmx->nested.pi_desc_page);
7056 nested_release_page(vmx->nested.pi_desc_page);
7057 vmx->nested.pi_desc_page = NULL;
7058 vmx->nested.pi_desc = NULL;
7059 }
7060
7061 nested_free_all_saved_vmcss(vmx);
7062 }
7063
7064 /* Emulate the VMXOFF instruction */
7065 static int handle_vmoff(struct kvm_vcpu *vcpu)
7066 {
7067 if (!nested_vmx_check_permission(vcpu))
7068 return 1;
7069 free_nested(to_vmx(vcpu));
7070 nested_vmx_succeed(vcpu);
7071 return kvm_skip_emulated_instruction(vcpu);
7072 }
7073
7074 /* Emulate the VMCLEAR instruction */
7075 static int handle_vmclear(struct kvm_vcpu *vcpu)
7076 {
7077 struct vcpu_vmx *vmx = to_vmx(vcpu);
7078 gpa_t vmptr;
7079 struct vmcs12 *vmcs12;
7080 struct page *page;
7081
7082 if (!nested_vmx_check_permission(vcpu))
7083 return 1;
7084
7085 if (nested_vmx_check_vmptr(vcpu, EXIT_REASON_VMCLEAR, &vmptr))
7086 return 1;
7087
7088 if (vmptr == vmx->nested.current_vmptr)
7089 nested_release_vmcs12(vmx);
7090
7091 page = nested_get_page(vcpu, vmptr);
7092 if (page == NULL) {
7093 /*
7094 * For accurate processor emulation, VMCLEAR beyond available
7095 * physical memory should do nothing at all. However, it is
7096 * possible that a nested vmx bug, not a guest hypervisor bug,
7097 * resulted in this case, so let's shut down before doing any
7098 * more damage:
7099 */
7100 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
7101 return 1;
7102 }
7103 vmcs12 = kmap(page);
7104 vmcs12->launch_state = 0;
7105 kunmap(page);
7106 nested_release_page(page);
7107
7108 nested_free_vmcs02(vmx, vmptr);
7109
7110 nested_vmx_succeed(vcpu);
7111 return kvm_skip_emulated_instruction(vcpu);
7112 }
7113
7114 static int nested_vmx_run(struct kvm_vcpu *vcpu, bool launch);
7115
7116 /* Emulate the VMLAUNCH instruction */
7117 static int handle_vmlaunch(struct kvm_vcpu *vcpu)
7118 {
7119 return nested_vmx_run(vcpu, true);
7120 }
7121
7122 /* Emulate the VMRESUME instruction */
7123 static int handle_vmresume(struct kvm_vcpu *vcpu)
7124 {
7125
7126 return nested_vmx_run(vcpu, false);
7127 }
7128
7129 enum vmcs_field_type {
7130 VMCS_FIELD_TYPE_U16 = 0,
7131 VMCS_FIELD_TYPE_U64 = 1,
7132 VMCS_FIELD_TYPE_U32 = 2,
7133 VMCS_FIELD_TYPE_NATURAL_WIDTH = 3
7134 };
7135
7136 static inline int vmcs_field_type(unsigned long field)
7137 {
7138 if (0x1 & field) /* the *_HIGH fields are all 32 bit */
7139 return VMCS_FIELD_TYPE_U32;
7140 return (field >> 13) & 0x3 ;
7141 }
7142
7143 static inline int vmcs_field_readonly(unsigned long field)
7144 {
7145 return (((field >> 10) & 0x3) == 1);
7146 }
7147
7148 /*
7149 * Read a vmcs12 field. Since these can have varying lengths and we return
7150 * one type, we chose the biggest type (u64) and zero-extend the return value
7151 * to that size. Note that the caller, handle_vmread, might need to use only
7152 * some of the bits we return here (e.g., on 32-bit guests, only 32 bits of
7153 * 64-bit fields are to be returned).
7154 */
7155 static inline int vmcs12_read_any(struct kvm_vcpu *vcpu,
7156 unsigned long field, u64 *ret)
7157 {
7158 short offset = vmcs_field_to_offset(field);
7159 char *p;
7160
7161 if (offset < 0)
7162 return offset;
7163
7164 p = ((char *)(get_vmcs12(vcpu))) + offset;
7165
7166 switch (vmcs_field_type(field)) {
7167 case VMCS_FIELD_TYPE_NATURAL_WIDTH:
7168 *ret = *((natural_width *)p);
7169 return 0;
7170 case VMCS_FIELD_TYPE_U16:
7171 *ret = *((u16 *)p);
7172 return 0;
7173 case VMCS_FIELD_TYPE_U32:
7174 *ret = *((u32 *)p);
7175 return 0;
7176 case VMCS_FIELD_TYPE_U64:
7177 *ret = *((u64 *)p);
7178 return 0;
7179 default:
7180 WARN_ON(1);
7181 return -ENOENT;
7182 }
7183 }
7184
7185
7186 static inline int vmcs12_write_any(struct kvm_vcpu *vcpu,
7187 unsigned long field, u64 field_value){
7188 short offset = vmcs_field_to_offset(field);
7189 char *p = ((char *) get_vmcs12(vcpu)) + offset;
7190 if (offset < 0)
7191 return offset;
7192
7193 switch (vmcs_field_type(field)) {
7194 case VMCS_FIELD_TYPE_U16:
7195 *(u16 *)p = field_value;
7196 return 0;
7197 case VMCS_FIELD_TYPE_U32:
7198 *(u32 *)p = field_value;
7199 return 0;
7200 case VMCS_FIELD_TYPE_U64:
7201 *(u64 *)p = field_value;
7202 return 0;
7203 case VMCS_FIELD_TYPE_NATURAL_WIDTH:
7204 *(natural_width *)p = field_value;
7205 return 0;
7206 default:
7207 WARN_ON(1);
7208 return -ENOENT;
7209 }
7210
7211 }
7212
7213 static void copy_shadow_to_vmcs12(struct vcpu_vmx *vmx)
7214 {
7215 int i;
7216 unsigned long field;
7217 u64 field_value;
7218 struct vmcs *shadow_vmcs = vmx->vmcs01.shadow_vmcs;
7219 const unsigned long *fields = shadow_read_write_fields;
7220 const int num_fields = max_shadow_read_write_fields;
7221
7222 preempt_disable();
7223
7224 vmcs_load(shadow_vmcs);
7225
7226 for (i = 0; i < num_fields; i++) {
7227 field = fields[i];
7228 switch (vmcs_field_type(field)) {
7229 case VMCS_FIELD_TYPE_U16:
7230 field_value = vmcs_read16(field);
7231 break;
7232 case VMCS_FIELD_TYPE_U32:
7233 field_value = vmcs_read32(field);
7234 break;
7235 case VMCS_FIELD_TYPE_U64:
7236 field_value = vmcs_read64(field);
7237 break;
7238 case VMCS_FIELD_TYPE_NATURAL_WIDTH:
7239 field_value = vmcs_readl(field);
7240 break;
7241 default:
7242 WARN_ON(1);
7243 continue;
7244 }
7245 vmcs12_write_any(&vmx->vcpu, field, field_value);
7246 }
7247
7248 vmcs_clear(shadow_vmcs);
7249 vmcs_load(vmx->loaded_vmcs->vmcs);
7250
7251 preempt_enable();
7252 }
7253
7254 static void copy_vmcs12_to_shadow(struct vcpu_vmx *vmx)
7255 {
7256 const unsigned long *fields[] = {
7257 shadow_read_write_fields,
7258 shadow_read_only_fields
7259 };
7260 const int max_fields[] = {
7261 max_shadow_read_write_fields,
7262 max_shadow_read_only_fields
7263 };
7264 int i, q;
7265 unsigned long field;
7266 u64 field_value = 0;
7267 struct vmcs *shadow_vmcs = vmx->vmcs01.shadow_vmcs;
7268
7269 vmcs_load(shadow_vmcs);
7270
7271 for (q = 0; q < ARRAY_SIZE(fields); q++) {
7272 for (i = 0; i < max_fields[q]; i++) {
7273 field = fields[q][i];
7274 vmcs12_read_any(&vmx->vcpu, field, &field_value);
7275
7276 switch (vmcs_field_type(field)) {
7277 case VMCS_FIELD_TYPE_U16:
7278 vmcs_write16(field, (u16)field_value);
7279 break;
7280 case VMCS_FIELD_TYPE_U32:
7281 vmcs_write32(field, (u32)field_value);
7282 break;
7283 case VMCS_FIELD_TYPE_U64:
7284 vmcs_write64(field, (u64)field_value);
7285 break;
7286 case VMCS_FIELD_TYPE_NATURAL_WIDTH:
7287 vmcs_writel(field, (long)field_value);
7288 break;
7289 default:
7290 WARN_ON(1);
7291 break;
7292 }
7293 }
7294 }
7295
7296 vmcs_clear(shadow_vmcs);
7297 vmcs_load(vmx->loaded_vmcs->vmcs);
7298 }
7299
7300 /*
7301 * VMX instructions which assume a current vmcs12 (i.e., that VMPTRLD was
7302 * used before) all generate the same failure when it is missing.
7303 */
7304 static int nested_vmx_check_vmcs12(struct kvm_vcpu *vcpu)
7305 {
7306 struct vcpu_vmx *vmx = to_vmx(vcpu);
7307 if (vmx->nested.current_vmptr == -1ull) {
7308 nested_vmx_failInvalid(vcpu);
7309 return 0;
7310 }
7311 return 1;
7312 }
7313
7314 static int handle_vmread(struct kvm_vcpu *vcpu)
7315 {
7316 unsigned long field;
7317 u64 field_value;
7318 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7319 u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
7320 gva_t gva = 0;
7321
7322 if (!nested_vmx_check_permission(vcpu))
7323 return 1;
7324
7325 if (!nested_vmx_check_vmcs12(vcpu))
7326 return kvm_skip_emulated_instruction(vcpu);
7327
7328 /* Decode instruction info and find the field to read */
7329 field = kvm_register_readl(vcpu, (((vmx_instruction_info) >> 28) & 0xf));
7330 /* Read the field, zero-extended to a u64 field_value */
7331 if (vmcs12_read_any(vcpu, field, &field_value) < 0) {
7332 nested_vmx_failValid(vcpu, VMXERR_UNSUPPORTED_VMCS_COMPONENT);
7333 return kvm_skip_emulated_instruction(vcpu);
7334 }
7335 /*
7336 * Now copy part of this value to register or memory, as requested.
7337 * Note that the number of bits actually copied is 32 or 64 depending
7338 * on the guest's mode (32 or 64 bit), not on the given field's length.
7339 */
7340 if (vmx_instruction_info & (1u << 10)) {
7341 kvm_register_writel(vcpu, (((vmx_instruction_info) >> 3) & 0xf),
7342 field_value);
7343 } else {
7344 if (get_vmx_mem_address(vcpu, exit_qualification,
7345 vmx_instruction_info, true, &gva))
7346 return 1;
7347 /* _system ok, as nested_vmx_check_permission verified cpl=0 */
7348 kvm_write_guest_virt_system(&vcpu->arch.emulate_ctxt, gva,
7349 &field_value, (is_long_mode(vcpu) ? 8 : 4), NULL);
7350 }
7351
7352 nested_vmx_succeed(vcpu);
7353 return kvm_skip_emulated_instruction(vcpu);
7354 }
7355
7356
7357 static int handle_vmwrite(struct kvm_vcpu *vcpu)
7358 {
7359 unsigned long field;
7360 gva_t gva;
7361 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7362 u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
7363 /* The value to write might be 32 or 64 bits, depending on L1's long
7364 * mode, and eventually we need to write that into a field of several
7365 * possible lengths. The code below first zero-extends the value to 64
7366 * bit (field_value), and then copies only the appropriate number of
7367 * bits into the vmcs12 field.
7368 */
7369 u64 field_value = 0;
7370 struct x86_exception e;
7371
7372 if (!nested_vmx_check_permission(vcpu))
7373 return 1;
7374
7375 if (!nested_vmx_check_vmcs12(vcpu))
7376 return kvm_skip_emulated_instruction(vcpu);
7377
7378 if (vmx_instruction_info & (1u << 10))
7379 field_value = kvm_register_readl(vcpu,
7380 (((vmx_instruction_info) >> 3) & 0xf));
7381 else {
7382 if (get_vmx_mem_address(vcpu, exit_qualification,
7383 vmx_instruction_info, false, &gva))
7384 return 1;
7385 if (kvm_read_guest_virt(&vcpu->arch.emulate_ctxt, gva,
7386 &field_value, (is_64_bit_mode(vcpu) ? 8 : 4), &e)) {
7387 kvm_inject_page_fault(vcpu, &e);
7388 return 1;
7389 }
7390 }
7391
7392
7393 field = kvm_register_readl(vcpu, (((vmx_instruction_info) >> 28) & 0xf));
7394 if (vmcs_field_readonly(field)) {
7395 nested_vmx_failValid(vcpu,
7396 VMXERR_VMWRITE_READ_ONLY_VMCS_COMPONENT);
7397 return kvm_skip_emulated_instruction(vcpu);
7398 }
7399
7400 if (vmcs12_write_any(vcpu, field, field_value) < 0) {
7401 nested_vmx_failValid(vcpu, VMXERR_UNSUPPORTED_VMCS_COMPONENT);
7402 return kvm_skip_emulated_instruction(vcpu);
7403 }
7404
7405 nested_vmx_succeed(vcpu);
7406 return kvm_skip_emulated_instruction(vcpu);
7407 }
7408
7409 /* Emulate the VMPTRLD instruction */
7410 static int handle_vmptrld(struct kvm_vcpu *vcpu)
7411 {
7412 struct vcpu_vmx *vmx = to_vmx(vcpu);
7413 gpa_t vmptr;
7414
7415 if (!nested_vmx_check_permission(vcpu))
7416 return 1;
7417
7418 if (nested_vmx_check_vmptr(vcpu, EXIT_REASON_VMPTRLD, &vmptr))
7419 return 1;
7420
7421 if (vmx->nested.current_vmptr != vmptr) {
7422 struct vmcs12 *new_vmcs12;
7423 struct page *page;
7424 page = nested_get_page(vcpu, vmptr);
7425 if (page == NULL) {
7426 nested_vmx_failInvalid(vcpu);
7427 return kvm_skip_emulated_instruction(vcpu);
7428 }
7429 new_vmcs12 = kmap(page);
7430 if (new_vmcs12->revision_id != VMCS12_REVISION) {
7431 kunmap(page);
7432 nested_release_page_clean(page);
7433 nested_vmx_failValid(vcpu,
7434 VMXERR_VMPTRLD_INCORRECT_VMCS_REVISION_ID);
7435 return kvm_skip_emulated_instruction(vcpu);
7436 }
7437
7438 nested_release_vmcs12(vmx);
7439 vmx->nested.current_vmptr = vmptr;
7440 vmx->nested.current_vmcs12 = new_vmcs12;
7441 vmx->nested.current_vmcs12_page = page;
7442 /*
7443 * Load VMCS12 from guest memory since it is not already
7444 * cached.
7445 */
7446 memcpy(vmx->nested.cached_vmcs12,
7447 vmx->nested.current_vmcs12, VMCS12_SIZE);
7448
7449 if (enable_shadow_vmcs) {
7450 vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL,
7451 SECONDARY_EXEC_SHADOW_VMCS);
7452 vmcs_write64(VMCS_LINK_POINTER,
7453 __pa(vmx->vmcs01.shadow_vmcs));
7454 vmx->nested.sync_shadow_vmcs = true;
7455 }
7456 }
7457
7458 nested_vmx_succeed(vcpu);
7459 return kvm_skip_emulated_instruction(vcpu);
7460 }
7461
7462 /* Emulate the VMPTRST instruction */
7463 static int handle_vmptrst(struct kvm_vcpu *vcpu)
7464 {
7465 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7466 u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
7467 gva_t vmcs_gva;
7468 struct x86_exception e;
7469
7470 if (!nested_vmx_check_permission(vcpu))
7471 return 1;
7472
7473 if (get_vmx_mem_address(vcpu, exit_qualification,
7474 vmx_instruction_info, true, &vmcs_gva))
7475 return 1;
7476 /* ok to use *_system, as nested_vmx_check_permission verified cpl=0 */
7477 if (kvm_write_guest_virt_system(&vcpu->arch.emulate_ctxt, vmcs_gva,
7478 (void *)&to_vmx(vcpu)->nested.current_vmptr,
7479 sizeof(u64), &e)) {
7480 kvm_inject_page_fault(vcpu, &e);
7481 return 1;
7482 }
7483 nested_vmx_succeed(vcpu);
7484 return kvm_skip_emulated_instruction(vcpu);
7485 }
7486
7487 /* Emulate the INVEPT instruction */
7488 static int handle_invept(struct kvm_vcpu *vcpu)
7489 {
7490 struct vcpu_vmx *vmx = to_vmx(vcpu);
7491 u32 vmx_instruction_info, types;
7492 unsigned long type;
7493 gva_t gva;
7494 struct x86_exception e;
7495 struct {
7496 u64 eptp, gpa;
7497 } operand;
7498
7499 if (!(vmx->nested.nested_vmx_secondary_ctls_high &
7500 SECONDARY_EXEC_ENABLE_EPT) ||
7501 !(vmx->nested.nested_vmx_ept_caps & VMX_EPT_INVEPT_BIT)) {
7502 kvm_queue_exception(vcpu, UD_VECTOR);
7503 return 1;
7504 }
7505
7506 if (!nested_vmx_check_permission(vcpu))
7507 return 1;
7508
7509 if (!kvm_read_cr0_bits(vcpu, X86_CR0_PE)) {
7510 kvm_queue_exception(vcpu, UD_VECTOR);
7511 return 1;
7512 }
7513
7514 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
7515 type = kvm_register_readl(vcpu, (vmx_instruction_info >> 28) & 0xf);
7516
7517 types = (vmx->nested.nested_vmx_ept_caps >> VMX_EPT_EXTENT_SHIFT) & 6;
7518
7519 if (type >= 32 || !(types & (1 << type))) {
7520 nested_vmx_failValid(vcpu,
7521 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
7522 return kvm_skip_emulated_instruction(vcpu);
7523 }
7524
7525 /* According to the Intel VMX instruction reference, the memory
7526 * operand is read even if it isn't needed (e.g., for type==global)
7527 */
7528 if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
7529 vmx_instruction_info, false, &gva))
7530 return 1;
7531 if (kvm_read_guest_virt(&vcpu->arch.emulate_ctxt, gva, &operand,
7532 sizeof(operand), &e)) {
7533 kvm_inject_page_fault(vcpu, &e);
7534 return 1;
7535 }
7536
7537 switch (type) {
7538 case VMX_EPT_EXTENT_GLOBAL:
7539 /*
7540 * TODO: track mappings and invalidate
7541 * single context requests appropriately
7542 */
7543 case VMX_EPT_EXTENT_CONTEXT:
7544 kvm_mmu_sync_roots(vcpu);
7545 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
7546 nested_vmx_succeed(vcpu);
7547 break;
7548 default:
7549 BUG_ON(1);
7550 break;
7551 }
7552
7553 return kvm_skip_emulated_instruction(vcpu);
7554 }
7555
7556 static int handle_invvpid(struct kvm_vcpu *vcpu)
7557 {
7558 struct vcpu_vmx *vmx = to_vmx(vcpu);
7559 u32 vmx_instruction_info;
7560 unsigned long type, types;
7561 gva_t gva;
7562 struct x86_exception e;
7563 int vpid;
7564
7565 if (!(vmx->nested.nested_vmx_secondary_ctls_high &
7566 SECONDARY_EXEC_ENABLE_VPID) ||
7567 !(vmx->nested.nested_vmx_vpid_caps & VMX_VPID_INVVPID_BIT)) {
7568 kvm_queue_exception(vcpu, UD_VECTOR);
7569 return 1;
7570 }
7571
7572 if (!nested_vmx_check_permission(vcpu))
7573 return 1;
7574
7575 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
7576 type = kvm_register_readl(vcpu, (vmx_instruction_info >> 28) & 0xf);
7577
7578 types = (vmx->nested.nested_vmx_vpid_caps &
7579 VMX_VPID_EXTENT_SUPPORTED_MASK) >> 8;
7580
7581 if (type >= 32 || !(types & (1 << type))) {
7582 nested_vmx_failValid(vcpu,
7583 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
7584 return kvm_skip_emulated_instruction(vcpu);
7585 }
7586
7587 /* according to the intel vmx instruction reference, the memory
7588 * operand is read even if it isn't needed (e.g., for type==global)
7589 */
7590 if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
7591 vmx_instruction_info, false, &gva))
7592 return 1;
7593 if (kvm_read_guest_virt(&vcpu->arch.emulate_ctxt, gva, &vpid,
7594 sizeof(u32), &e)) {
7595 kvm_inject_page_fault(vcpu, &e);
7596 return 1;
7597 }
7598
7599 switch (type) {
7600 case VMX_VPID_EXTENT_INDIVIDUAL_ADDR:
7601 case VMX_VPID_EXTENT_SINGLE_CONTEXT:
7602 case VMX_VPID_EXTENT_SINGLE_NON_GLOBAL:
7603 if (!vpid) {
7604 nested_vmx_failValid(vcpu,
7605 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
7606 return kvm_skip_emulated_instruction(vcpu);
7607 }
7608 break;
7609 case VMX_VPID_EXTENT_ALL_CONTEXT:
7610 break;
7611 default:
7612 WARN_ON_ONCE(1);
7613 return kvm_skip_emulated_instruction(vcpu);
7614 }
7615
7616 __vmx_flush_tlb(vcpu, vmx->nested.vpid02);
7617 nested_vmx_succeed(vcpu);
7618
7619 return kvm_skip_emulated_instruction(vcpu);
7620 }
7621
7622 static int handle_pml_full(struct kvm_vcpu *vcpu)
7623 {
7624 unsigned long exit_qualification;
7625
7626 trace_kvm_pml_full(vcpu->vcpu_id);
7627
7628 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7629
7630 /*
7631 * PML buffer FULL happened while executing iret from NMI,
7632 * "blocked by NMI" bit has to be set before next VM entry.
7633 */
7634 if (!(to_vmx(vcpu)->idt_vectoring_info & VECTORING_INFO_VALID_MASK) &&
7635 cpu_has_virtual_nmis() &&
7636 (exit_qualification & INTR_INFO_UNBLOCK_NMI))
7637 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
7638 GUEST_INTR_STATE_NMI);
7639
7640 /*
7641 * PML buffer already flushed at beginning of VMEXIT. Nothing to do
7642 * here.., and there's no userspace involvement needed for PML.
7643 */
7644 return 1;
7645 }
7646
7647 static int handle_preemption_timer(struct kvm_vcpu *vcpu)
7648 {
7649 kvm_lapic_expired_hv_timer(vcpu);
7650 return 1;
7651 }
7652
7653 /*
7654 * The exit handlers return 1 if the exit was handled fully and guest execution
7655 * may resume. Otherwise they set the kvm_run parameter to indicate what needs
7656 * to be done to userspace and return 0.
7657 */
7658 static int (*const kvm_vmx_exit_handlers[])(struct kvm_vcpu *vcpu) = {
7659 [EXIT_REASON_EXCEPTION_NMI] = handle_exception,
7660 [EXIT_REASON_EXTERNAL_INTERRUPT] = handle_external_interrupt,
7661 [EXIT_REASON_TRIPLE_FAULT] = handle_triple_fault,
7662 [EXIT_REASON_NMI_WINDOW] = handle_nmi_window,
7663 [EXIT_REASON_IO_INSTRUCTION] = handle_io,
7664 [EXIT_REASON_CR_ACCESS] = handle_cr,
7665 [EXIT_REASON_DR_ACCESS] = handle_dr,
7666 [EXIT_REASON_CPUID] = handle_cpuid,
7667 [EXIT_REASON_MSR_READ] = handle_rdmsr,
7668 [EXIT_REASON_MSR_WRITE] = handle_wrmsr,
7669 [EXIT_REASON_PENDING_INTERRUPT] = handle_interrupt_window,
7670 [EXIT_REASON_HLT] = handle_halt,
7671 [EXIT_REASON_INVD] = handle_invd,
7672 [EXIT_REASON_INVLPG] = handle_invlpg,
7673 [EXIT_REASON_RDPMC] = handle_rdpmc,
7674 [EXIT_REASON_VMCALL] = handle_vmcall,
7675 [EXIT_REASON_VMCLEAR] = handle_vmclear,
7676 [EXIT_REASON_VMLAUNCH] = handle_vmlaunch,
7677 [EXIT_REASON_VMPTRLD] = handle_vmptrld,
7678 [EXIT_REASON_VMPTRST] = handle_vmptrst,
7679 [EXIT_REASON_VMREAD] = handle_vmread,
7680 [EXIT_REASON_VMRESUME] = handle_vmresume,
7681 [EXIT_REASON_VMWRITE] = handle_vmwrite,
7682 [EXIT_REASON_VMOFF] = handle_vmoff,
7683 [EXIT_REASON_VMON] = handle_vmon,
7684 [EXIT_REASON_TPR_BELOW_THRESHOLD] = handle_tpr_below_threshold,
7685 [EXIT_REASON_APIC_ACCESS] = handle_apic_access,
7686 [EXIT_REASON_APIC_WRITE] = handle_apic_write,
7687 [EXIT_REASON_EOI_INDUCED] = handle_apic_eoi_induced,
7688 [EXIT_REASON_WBINVD] = handle_wbinvd,
7689 [EXIT_REASON_XSETBV] = handle_xsetbv,
7690 [EXIT_REASON_TASK_SWITCH] = handle_task_switch,
7691 [EXIT_REASON_MCE_DURING_VMENTRY] = handle_machine_check,
7692 [EXIT_REASON_EPT_VIOLATION] = handle_ept_violation,
7693 [EXIT_REASON_EPT_MISCONFIG] = handle_ept_misconfig,
7694 [EXIT_REASON_PAUSE_INSTRUCTION] = handle_pause,
7695 [EXIT_REASON_MWAIT_INSTRUCTION] = handle_mwait,
7696 [EXIT_REASON_MONITOR_TRAP_FLAG] = handle_monitor_trap,
7697 [EXIT_REASON_MONITOR_INSTRUCTION] = handle_monitor,
7698 [EXIT_REASON_INVEPT] = handle_invept,
7699 [EXIT_REASON_INVVPID] = handle_invvpid,
7700 [EXIT_REASON_XSAVES] = handle_xsaves,
7701 [EXIT_REASON_XRSTORS] = handle_xrstors,
7702 [EXIT_REASON_PML_FULL] = handle_pml_full,
7703 [EXIT_REASON_PREEMPTION_TIMER] = handle_preemption_timer,
7704 };
7705
7706 static const int kvm_vmx_max_exit_handlers =
7707 ARRAY_SIZE(kvm_vmx_exit_handlers);
7708
7709 static bool nested_vmx_exit_handled_io(struct kvm_vcpu *vcpu,
7710 struct vmcs12 *vmcs12)
7711 {
7712 unsigned long exit_qualification;
7713 gpa_t bitmap, last_bitmap;
7714 unsigned int port;
7715 int size;
7716 u8 b;
7717
7718 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_IO_BITMAPS))
7719 return nested_cpu_has(vmcs12, CPU_BASED_UNCOND_IO_EXITING);
7720
7721 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7722
7723 port = exit_qualification >> 16;
7724 size = (exit_qualification & 7) + 1;
7725
7726 last_bitmap = (gpa_t)-1;
7727 b = -1;
7728
7729 while (size > 0) {
7730 if (port < 0x8000)
7731 bitmap = vmcs12->io_bitmap_a;
7732 else if (port < 0x10000)
7733 bitmap = vmcs12->io_bitmap_b;
7734 else
7735 return true;
7736 bitmap += (port & 0x7fff) / 8;
7737
7738 if (last_bitmap != bitmap)
7739 if (kvm_vcpu_read_guest(vcpu, bitmap, &b, 1))
7740 return true;
7741 if (b & (1 << (port & 7)))
7742 return true;
7743
7744 port++;
7745 size--;
7746 last_bitmap = bitmap;
7747 }
7748
7749 return false;
7750 }
7751
7752 /*
7753 * Return 1 if we should exit from L2 to L1 to handle an MSR access access,
7754 * rather than handle it ourselves in L0. I.e., check whether L1 expressed
7755 * disinterest in the current event (read or write a specific MSR) by using an
7756 * MSR bitmap. This may be the case even when L0 doesn't use MSR bitmaps.
7757 */
7758 static bool nested_vmx_exit_handled_msr(struct kvm_vcpu *vcpu,
7759 struct vmcs12 *vmcs12, u32 exit_reason)
7760 {
7761 u32 msr_index = vcpu->arch.regs[VCPU_REGS_RCX];
7762 gpa_t bitmap;
7763
7764 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS))
7765 return true;
7766
7767 /*
7768 * The MSR_BITMAP page is divided into four 1024-byte bitmaps,
7769 * for the four combinations of read/write and low/high MSR numbers.
7770 * First we need to figure out which of the four to use:
7771 */
7772 bitmap = vmcs12->msr_bitmap;
7773 if (exit_reason == EXIT_REASON_MSR_WRITE)
7774 bitmap += 2048;
7775 if (msr_index >= 0xc0000000) {
7776 msr_index -= 0xc0000000;
7777 bitmap += 1024;
7778 }
7779
7780 /* Then read the msr_index'th bit from this bitmap: */
7781 if (msr_index < 1024*8) {
7782 unsigned char b;
7783 if (kvm_vcpu_read_guest(vcpu, bitmap + msr_index/8, &b, 1))
7784 return true;
7785 return 1 & (b >> (msr_index & 7));
7786 } else
7787 return true; /* let L1 handle the wrong parameter */
7788 }
7789
7790 /*
7791 * Return 1 if we should exit from L2 to L1 to handle a CR access exit,
7792 * rather than handle it ourselves in L0. I.e., check if L1 wanted to
7793 * intercept (via guest_host_mask etc.) the current event.
7794 */
7795 static bool nested_vmx_exit_handled_cr(struct kvm_vcpu *vcpu,
7796 struct vmcs12 *vmcs12)
7797 {
7798 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7799 int cr = exit_qualification & 15;
7800 int reg = (exit_qualification >> 8) & 15;
7801 unsigned long val = kvm_register_readl(vcpu, reg);
7802
7803 switch ((exit_qualification >> 4) & 3) {
7804 case 0: /* mov to cr */
7805 switch (cr) {
7806 case 0:
7807 if (vmcs12->cr0_guest_host_mask &
7808 (val ^ vmcs12->cr0_read_shadow))
7809 return true;
7810 break;
7811 case 3:
7812 if ((vmcs12->cr3_target_count >= 1 &&
7813 vmcs12->cr3_target_value0 == val) ||
7814 (vmcs12->cr3_target_count >= 2 &&
7815 vmcs12->cr3_target_value1 == val) ||
7816 (vmcs12->cr3_target_count >= 3 &&
7817 vmcs12->cr3_target_value2 == val) ||
7818 (vmcs12->cr3_target_count >= 4 &&
7819 vmcs12->cr3_target_value3 == val))
7820 return false;
7821 if (nested_cpu_has(vmcs12, CPU_BASED_CR3_LOAD_EXITING))
7822 return true;
7823 break;
7824 case 4:
7825 if (vmcs12->cr4_guest_host_mask &
7826 (vmcs12->cr4_read_shadow ^ val))
7827 return true;
7828 break;
7829 case 8:
7830 if (nested_cpu_has(vmcs12, CPU_BASED_CR8_LOAD_EXITING))
7831 return true;
7832 break;
7833 }
7834 break;
7835 case 2: /* clts */
7836 if ((vmcs12->cr0_guest_host_mask & X86_CR0_TS) &&
7837 (vmcs12->cr0_read_shadow & X86_CR0_TS))
7838 return true;
7839 break;
7840 case 1: /* mov from cr */
7841 switch (cr) {
7842 case 3:
7843 if (vmcs12->cpu_based_vm_exec_control &
7844 CPU_BASED_CR3_STORE_EXITING)
7845 return true;
7846 break;
7847 case 8:
7848 if (vmcs12->cpu_based_vm_exec_control &
7849 CPU_BASED_CR8_STORE_EXITING)
7850 return true;
7851 break;
7852 }
7853 break;
7854 case 3: /* lmsw */
7855 /*
7856 * lmsw can change bits 1..3 of cr0, and only set bit 0 of
7857 * cr0. Other attempted changes are ignored, with no exit.
7858 */
7859 if (vmcs12->cr0_guest_host_mask & 0xe &
7860 (val ^ vmcs12->cr0_read_shadow))
7861 return true;
7862 if ((vmcs12->cr0_guest_host_mask & 0x1) &&
7863 !(vmcs12->cr0_read_shadow & 0x1) &&
7864 (val & 0x1))
7865 return true;
7866 break;
7867 }
7868 return false;
7869 }
7870
7871 /*
7872 * Return 1 if we should exit from L2 to L1 to handle an exit, or 0 if we
7873 * should handle it ourselves in L0 (and then continue L2). Only call this
7874 * when in is_guest_mode (L2).
7875 */
7876 static bool nested_vmx_exit_handled(struct kvm_vcpu *vcpu)
7877 {
7878 u32 intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
7879 struct vcpu_vmx *vmx = to_vmx(vcpu);
7880 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
7881 u32 exit_reason = vmx->exit_reason;
7882
7883 trace_kvm_nested_vmexit(kvm_rip_read(vcpu), exit_reason,
7884 vmcs_readl(EXIT_QUALIFICATION),
7885 vmx->idt_vectoring_info,
7886 intr_info,
7887 vmcs_read32(VM_EXIT_INTR_ERROR_CODE),
7888 KVM_ISA_VMX);
7889
7890 if (vmx->nested.nested_run_pending)
7891 return false;
7892
7893 if (unlikely(vmx->fail)) {
7894 pr_info_ratelimited("%s failed vm entry %x\n", __func__,
7895 vmcs_read32(VM_INSTRUCTION_ERROR));
7896 return true;
7897 }
7898
7899 switch (exit_reason) {
7900 case EXIT_REASON_EXCEPTION_NMI:
7901 if (!is_exception(intr_info))
7902 return false;
7903 else if (is_page_fault(intr_info))
7904 return enable_ept;
7905 else if (is_no_device(intr_info) &&
7906 !(vmcs12->guest_cr0 & X86_CR0_TS))
7907 return false;
7908 else if (is_debug(intr_info) &&
7909 vcpu->guest_debug &
7910 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))
7911 return false;
7912 else if (is_breakpoint(intr_info) &&
7913 vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
7914 return false;
7915 return vmcs12->exception_bitmap &
7916 (1u << (intr_info & INTR_INFO_VECTOR_MASK));
7917 case EXIT_REASON_EXTERNAL_INTERRUPT:
7918 return false;
7919 case EXIT_REASON_TRIPLE_FAULT:
7920 return true;
7921 case EXIT_REASON_PENDING_INTERRUPT:
7922 return nested_cpu_has(vmcs12, CPU_BASED_VIRTUAL_INTR_PENDING);
7923 case EXIT_REASON_NMI_WINDOW:
7924 return nested_cpu_has(vmcs12, CPU_BASED_VIRTUAL_NMI_PENDING);
7925 case EXIT_REASON_TASK_SWITCH:
7926 return true;
7927 case EXIT_REASON_CPUID:
7928 if (kvm_register_read(vcpu, VCPU_REGS_RAX) == 0xa)
7929 return false;
7930 return true;
7931 case EXIT_REASON_HLT:
7932 return nested_cpu_has(vmcs12, CPU_BASED_HLT_EXITING);
7933 case EXIT_REASON_INVD:
7934 return true;
7935 case EXIT_REASON_INVLPG:
7936 return nested_cpu_has(vmcs12, CPU_BASED_INVLPG_EXITING);
7937 case EXIT_REASON_RDPMC:
7938 return nested_cpu_has(vmcs12, CPU_BASED_RDPMC_EXITING);
7939 case EXIT_REASON_RDTSC: case EXIT_REASON_RDTSCP:
7940 return nested_cpu_has(vmcs12, CPU_BASED_RDTSC_EXITING);
7941 case EXIT_REASON_VMCALL: case EXIT_REASON_VMCLEAR:
7942 case EXIT_REASON_VMLAUNCH: case EXIT_REASON_VMPTRLD:
7943 case EXIT_REASON_VMPTRST: case EXIT_REASON_VMREAD:
7944 case EXIT_REASON_VMRESUME: case EXIT_REASON_VMWRITE:
7945 case EXIT_REASON_VMOFF: case EXIT_REASON_VMON:
7946 case EXIT_REASON_INVEPT: case EXIT_REASON_INVVPID:
7947 /*
7948 * VMX instructions trap unconditionally. This allows L1 to
7949 * emulate them for its L2 guest, i.e., allows 3-level nesting!
7950 */
7951 return true;
7952 case EXIT_REASON_CR_ACCESS:
7953 return nested_vmx_exit_handled_cr(vcpu, vmcs12);
7954 case EXIT_REASON_DR_ACCESS:
7955 return nested_cpu_has(vmcs12, CPU_BASED_MOV_DR_EXITING);
7956 case EXIT_REASON_IO_INSTRUCTION:
7957 return nested_vmx_exit_handled_io(vcpu, vmcs12);
7958 case EXIT_REASON_GDTR_IDTR: case EXIT_REASON_LDTR_TR:
7959 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_DESC);
7960 case EXIT_REASON_MSR_READ:
7961 case EXIT_REASON_MSR_WRITE:
7962 return nested_vmx_exit_handled_msr(vcpu, vmcs12, exit_reason);
7963 case EXIT_REASON_INVALID_STATE:
7964 return true;
7965 case EXIT_REASON_MWAIT_INSTRUCTION:
7966 return nested_cpu_has(vmcs12, CPU_BASED_MWAIT_EXITING);
7967 case EXIT_REASON_MONITOR_TRAP_FLAG:
7968 return nested_cpu_has(vmcs12, CPU_BASED_MONITOR_TRAP_FLAG);
7969 case EXIT_REASON_MONITOR_INSTRUCTION:
7970 return nested_cpu_has(vmcs12, CPU_BASED_MONITOR_EXITING);
7971 case EXIT_REASON_PAUSE_INSTRUCTION:
7972 return nested_cpu_has(vmcs12, CPU_BASED_PAUSE_EXITING) ||
7973 nested_cpu_has2(vmcs12,
7974 SECONDARY_EXEC_PAUSE_LOOP_EXITING);
7975 case EXIT_REASON_MCE_DURING_VMENTRY:
7976 return false;
7977 case EXIT_REASON_TPR_BELOW_THRESHOLD:
7978 return nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW);
7979 case EXIT_REASON_APIC_ACCESS:
7980 return nested_cpu_has2(vmcs12,
7981 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES);
7982 case EXIT_REASON_APIC_WRITE:
7983 case EXIT_REASON_EOI_INDUCED:
7984 /* apic_write and eoi_induced should exit unconditionally. */
7985 return true;
7986 case EXIT_REASON_EPT_VIOLATION:
7987 /*
7988 * L0 always deals with the EPT violation. If nested EPT is
7989 * used, and the nested mmu code discovers that the address is
7990 * missing in the guest EPT table (EPT12), the EPT violation
7991 * will be injected with nested_ept_inject_page_fault()
7992 */
7993 return false;
7994 case EXIT_REASON_EPT_MISCONFIG:
7995 /*
7996 * L2 never uses directly L1's EPT, but rather L0's own EPT
7997 * table (shadow on EPT) or a merged EPT table that L0 built
7998 * (EPT on EPT). So any problems with the structure of the
7999 * table is L0's fault.
8000 */
8001 return false;
8002 case EXIT_REASON_WBINVD:
8003 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_WBINVD_EXITING);
8004 case EXIT_REASON_XSETBV:
8005 return true;
8006 case EXIT_REASON_XSAVES: case EXIT_REASON_XRSTORS:
8007 /*
8008 * This should never happen, since it is not possible to
8009 * set XSS to a non-zero value---neither in L1 nor in L2.
8010 * If if it were, XSS would have to be checked against
8011 * the XSS exit bitmap in vmcs12.
8012 */
8013 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_XSAVES);
8014 case EXIT_REASON_PREEMPTION_TIMER:
8015 return false;
8016 default:
8017 return true;
8018 }
8019 }
8020
8021 static void vmx_get_exit_info(struct kvm_vcpu *vcpu, u64 *info1, u64 *info2)
8022 {
8023 *info1 = vmcs_readl(EXIT_QUALIFICATION);
8024 *info2 = vmcs_read32(VM_EXIT_INTR_INFO);
8025 }
8026
8027 static void vmx_destroy_pml_buffer(struct vcpu_vmx *vmx)
8028 {
8029 if (vmx->pml_pg) {
8030 __free_page(vmx->pml_pg);
8031 vmx->pml_pg = NULL;
8032 }
8033 }
8034
8035 static void vmx_flush_pml_buffer(struct kvm_vcpu *vcpu)
8036 {
8037 struct vcpu_vmx *vmx = to_vmx(vcpu);
8038 u64 *pml_buf;
8039 u16 pml_idx;
8040
8041 pml_idx = vmcs_read16(GUEST_PML_INDEX);
8042
8043 /* Do nothing if PML buffer is empty */
8044 if (pml_idx == (PML_ENTITY_NUM - 1))
8045 return;
8046
8047 /* PML index always points to next available PML buffer entity */
8048 if (pml_idx >= PML_ENTITY_NUM)
8049 pml_idx = 0;
8050 else
8051 pml_idx++;
8052
8053 pml_buf = page_address(vmx->pml_pg);
8054 for (; pml_idx < PML_ENTITY_NUM; pml_idx++) {
8055 u64 gpa;
8056
8057 gpa = pml_buf[pml_idx];
8058 WARN_ON(gpa & (PAGE_SIZE - 1));
8059 kvm_vcpu_mark_page_dirty(vcpu, gpa >> PAGE_SHIFT);
8060 }
8061
8062 /* reset PML index */
8063 vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1);
8064 }
8065
8066 /*
8067 * Flush all vcpus' PML buffer and update logged GPAs to dirty_bitmap.
8068 * Called before reporting dirty_bitmap to userspace.
8069 */
8070 static void kvm_flush_pml_buffers(struct kvm *kvm)
8071 {
8072 int i;
8073 struct kvm_vcpu *vcpu;
8074 /*
8075 * We only need to kick vcpu out of guest mode here, as PML buffer
8076 * is flushed at beginning of all VMEXITs, and it's obvious that only
8077 * vcpus running in guest are possible to have unflushed GPAs in PML
8078 * buffer.
8079 */
8080 kvm_for_each_vcpu(i, vcpu, kvm)
8081 kvm_vcpu_kick(vcpu);
8082 }
8083
8084 static void vmx_dump_sel(char *name, uint32_t sel)
8085 {
8086 pr_err("%s sel=0x%04x, attr=0x%05x, limit=0x%08x, base=0x%016lx\n",
8087 name, vmcs_read32(sel),
8088 vmcs_read32(sel + GUEST_ES_AR_BYTES - GUEST_ES_SELECTOR),
8089 vmcs_read32(sel + GUEST_ES_LIMIT - GUEST_ES_SELECTOR),
8090 vmcs_readl(sel + GUEST_ES_BASE - GUEST_ES_SELECTOR));
8091 }
8092
8093 static void vmx_dump_dtsel(char *name, uint32_t limit)
8094 {
8095 pr_err("%s limit=0x%08x, base=0x%016lx\n",
8096 name, vmcs_read32(limit),
8097 vmcs_readl(limit + GUEST_GDTR_BASE - GUEST_GDTR_LIMIT));
8098 }
8099
8100 static void dump_vmcs(void)
8101 {
8102 u32 vmentry_ctl = vmcs_read32(VM_ENTRY_CONTROLS);
8103 u32 vmexit_ctl = vmcs_read32(VM_EXIT_CONTROLS);
8104 u32 cpu_based_exec_ctrl = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
8105 u32 pin_based_exec_ctrl = vmcs_read32(PIN_BASED_VM_EXEC_CONTROL);
8106 u32 secondary_exec_control = 0;
8107 unsigned long cr4 = vmcs_readl(GUEST_CR4);
8108 u64 efer = vmcs_read64(GUEST_IA32_EFER);
8109 int i, n;
8110
8111 if (cpu_has_secondary_exec_ctrls())
8112 secondary_exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
8113
8114 pr_err("*** Guest State ***\n");
8115 pr_err("CR0: actual=0x%016lx, shadow=0x%016lx, gh_mask=%016lx\n",
8116 vmcs_readl(GUEST_CR0), vmcs_readl(CR0_READ_SHADOW),
8117 vmcs_readl(CR0_GUEST_HOST_MASK));
8118 pr_err("CR4: actual=0x%016lx, shadow=0x%016lx, gh_mask=%016lx\n",
8119 cr4, vmcs_readl(CR4_READ_SHADOW), vmcs_readl(CR4_GUEST_HOST_MASK));
8120 pr_err("CR3 = 0x%016lx\n", vmcs_readl(GUEST_CR3));
8121 if ((secondary_exec_control & SECONDARY_EXEC_ENABLE_EPT) &&
8122 (cr4 & X86_CR4_PAE) && !(efer & EFER_LMA))
8123 {
8124 pr_err("PDPTR0 = 0x%016llx PDPTR1 = 0x%016llx\n",
8125 vmcs_read64(GUEST_PDPTR0), vmcs_read64(GUEST_PDPTR1));
8126 pr_err("PDPTR2 = 0x%016llx PDPTR3 = 0x%016llx\n",
8127 vmcs_read64(GUEST_PDPTR2), vmcs_read64(GUEST_PDPTR3));
8128 }
8129 pr_err("RSP = 0x%016lx RIP = 0x%016lx\n",
8130 vmcs_readl(GUEST_RSP), vmcs_readl(GUEST_RIP));
8131 pr_err("RFLAGS=0x%08lx DR7 = 0x%016lx\n",
8132 vmcs_readl(GUEST_RFLAGS), vmcs_readl(GUEST_DR7));
8133 pr_err("Sysenter RSP=%016lx CS:RIP=%04x:%016lx\n",
8134 vmcs_readl(GUEST_SYSENTER_ESP),
8135 vmcs_read32(GUEST_SYSENTER_CS), vmcs_readl(GUEST_SYSENTER_EIP));
8136 vmx_dump_sel("CS: ", GUEST_CS_SELECTOR);
8137 vmx_dump_sel("DS: ", GUEST_DS_SELECTOR);
8138 vmx_dump_sel("SS: ", GUEST_SS_SELECTOR);
8139 vmx_dump_sel("ES: ", GUEST_ES_SELECTOR);
8140 vmx_dump_sel("FS: ", GUEST_FS_SELECTOR);
8141 vmx_dump_sel("GS: ", GUEST_GS_SELECTOR);
8142 vmx_dump_dtsel("GDTR:", GUEST_GDTR_LIMIT);
8143 vmx_dump_sel("LDTR:", GUEST_LDTR_SELECTOR);
8144 vmx_dump_dtsel("IDTR:", GUEST_IDTR_LIMIT);
8145 vmx_dump_sel("TR: ", GUEST_TR_SELECTOR);
8146 if ((vmexit_ctl & (VM_EXIT_SAVE_IA32_PAT | VM_EXIT_SAVE_IA32_EFER)) ||
8147 (vmentry_ctl & (VM_ENTRY_LOAD_IA32_PAT | VM_ENTRY_LOAD_IA32_EFER)))
8148 pr_err("EFER = 0x%016llx PAT = 0x%016llx\n",
8149 efer, vmcs_read64(GUEST_IA32_PAT));
8150 pr_err("DebugCtl = 0x%016llx DebugExceptions = 0x%016lx\n",
8151 vmcs_read64(GUEST_IA32_DEBUGCTL),
8152 vmcs_readl(GUEST_PENDING_DBG_EXCEPTIONS));
8153 if (vmentry_ctl & VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL)
8154 pr_err("PerfGlobCtl = 0x%016llx\n",
8155 vmcs_read64(GUEST_IA32_PERF_GLOBAL_CTRL));
8156 if (vmentry_ctl & VM_ENTRY_LOAD_BNDCFGS)
8157 pr_err("BndCfgS = 0x%016llx\n", vmcs_read64(GUEST_BNDCFGS));
8158 pr_err("Interruptibility = %08x ActivityState = %08x\n",
8159 vmcs_read32(GUEST_INTERRUPTIBILITY_INFO),
8160 vmcs_read32(GUEST_ACTIVITY_STATE));
8161 if (secondary_exec_control & SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY)
8162 pr_err("InterruptStatus = %04x\n",
8163 vmcs_read16(GUEST_INTR_STATUS));
8164
8165 pr_err("*** Host State ***\n");
8166 pr_err("RIP = 0x%016lx RSP = 0x%016lx\n",
8167 vmcs_readl(HOST_RIP), vmcs_readl(HOST_RSP));
8168 pr_err("CS=%04x SS=%04x DS=%04x ES=%04x FS=%04x GS=%04x TR=%04x\n",
8169 vmcs_read16(HOST_CS_SELECTOR), vmcs_read16(HOST_SS_SELECTOR),
8170 vmcs_read16(HOST_DS_SELECTOR), vmcs_read16(HOST_ES_SELECTOR),
8171 vmcs_read16(HOST_FS_SELECTOR), vmcs_read16(HOST_GS_SELECTOR),
8172 vmcs_read16(HOST_TR_SELECTOR));
8173 pr_err("FSBase=%016lx GSBase=%016lx TRBase=%016lx\n",
8174 vmcs_readl(HOST_FS_BASE), vmcs_readl(HOST_GS_BASE),
8175 vmcs_readl(HOST_TR_BASE));
8176 pr_err("GDTBase=%016lx IDTBase=%016lx\n",
8177 vmcs_readl(HOST_GDTR_BASE), vmcs_readl(HOST_IDTR_BASE));
8178 pr_err("CR0=%016lx CR3=%016lx CR4=%016lx\n",
8179 vmcs_readl(HOST_CR0), vmcs_readl(HOST_CR3),
8180 vmcs_readl(HOST_CR4));
8181 pr_err("Sysenter RSP=%016lx CS:RIP=%04x:%016lx\n",
8182 vmcs_readl(HOST_IA32_SYSENTER_ESP),
8183 vmcs_read32(HOST_IA32_SYSENTER_CS),
8184 vmcs_readl(HOST_IA32_SYSENTER_EIP));
8185 if (vmexit_ctl & (VM_EXIT_LOAD_IA32_PAT | VM_EXIT_LOAD_IA32_EFER))
8186 pr_err("EFER = 0x%016llx PAT = 0x%016llx\n",
8187 vmcs_read64(HOST_IA32_EFER),
8188 vmcs_read64(HOST_IA32_PAT));
8189 if (vmexit_ctl & VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL)
8190 pr_err("PerfGlobCtl = 0x%016llx\n",
8191 vmcs_read64(HOST_IA32_PERF_GLOBAL_CTRL));
8192
8193 pr_err("*** Control State ***\n");
8194 pr_err("PinBased=%08x CPUBased=%08x SecondaryExec=%08x\n",
8195 pin_based_exec_ctrl, cpu_based_exec_ctrl, secondary_exec_control);
8196 pr_err("EntryControls=%08x ExitControls=%08x\n", vmentry_ctl, vmexit_ctl);
8197 pr_err("ExceptionBitmap=%08x PFECmask=%08x PFECmatch=%08x\n",
8198 vmcs_read32(EXCEPTION_BITMAP),
8199 vmcs_read32(PAGE_FAULT_ERROR_CODE_MASK),
8200 vmcs_read32(PAGE_FAULT_ERROR_CODE_MATCH));
8201 pr_err("VMEntry: intr_info=%08x errcode=%08x ilen=%08x\n",
8202 vmcs_read32(VM_ENTRY_INTR_INFO_FIELD),
8203 vmcs_read32(VM_ENTRY_EXCEPTION_ERROR_CODE),
8204 vmcs_read32(VM_ENTRY_INSTRUCTION_LEN));
8205 pr_err("VMExit: intr_info=%08x errcode=%08x ilen=%08x\n",
8206 vmcs_read32(VM_EXIT_INTR_INFO),
8207 vmcs_read32(VM_EXIT_INTR_ERROR_CODE),
8208 vmcs_read32(VM_EXIT_INSTRUCTION_LEN));
8209 pr_err(" reason=%08x qualification=%016lx\n",
8210 vmcs_read32(VM_EXIT_REASON), vmcs_readl(EXIT_QUALIFICATION));
8211 pr_err("IDTVectoring: info=%08x errcode=%08x\n",
8212 vmcs_read32(IDT_VECTORING_INFO_FIELD),
8213 vmcs_read32(IDT_VECTORING_ERROR_CODE));
8214 pr_err("TSC Offset = 0x%016llx\n", vmcs_read64(TSC_OFFSET));
8215 if (secondary_exec_control & SECONDARY_EXEC_TSC_SCALING)
8216 pr_err("TSC Multiplier = 0x%016llx\n",
8217 vmcs_read64(TSC_MULTIPLIER));
8218 if (cpu_based_exec_ctrl & CPU_BASED_TPR_SHADOW)
8219 pr_err("TPR Threshold = 0x%02x\n", vmcs_read32(TPR_THRESHOLD));
8220 if (pin_based_exec_ctrl & PIN_BASED_POSTED_INTR)
8221 pr_err("PostedIntrVec = 0x%02x\n", vmcs_read16(POSTED_INTR_NV));
8222 if ((secondary_exec_control & SECONDARY_EXEC_ENABLE_EPT))
8223 pr_err("EPT pointer = 0x%016llx\n", vmcs_read64(EPT_POINTER));
8224 n = vmcs_read32(CR3_TARGET_COUNT);
8225 for (i = 0; i + 1 < n; i += 4)
8226 pr_err("CR3 target%u=%016lx target%u=%016lx\n",
8227 i, vmcs_readl(CR3_TARGET_VALUE0 + i * 2),
8228 i + 1, vmcs_readl(CR3_TARGET_VALUE0 + i * 2 + 2));
8229 if (i < n)
8230 pr_err("CR3 target%u=%016lx\n",
8231 i, vmcs_readl(CR3_TARGET_VALUE0 + i * 2));
8232 if (secondary_exec_control & SECONDARY_EXEC_PAUSE_LOOP_EXITING)
8233 pr_err("PLE Gap=%08x Window=%08x\n",
8234 vmcs_read32(PLE_GAP), vmcs_read32(PLE_WINDOW));
8235 if (secondary_exec_control & SECONDARY_EXEC_ENABLE_VPID)
8236 pr_err("Virtual processor ID = 0x%04x\n",
8237 vmcs_read16(VIRTUAL_PROCESSOR_ID));
8238 }
8239
8240 /*
8241 * The guest has exited. See if we can fix it or if we need userspace
8242 * assistance.
8243 */
8244 static int vmx_handle_exit(struct kvm_vcpu *vcpu)
8245 {
8246 struct vcpu_vmx *vmx = to_vmx(vcpu);
8247 u32 exit_reason = vmx->exit_reason;
8248 u32 vectoring_info = vmx->idt_vectoring_info;
8249
8250 trace_kvm_exit(exit_reason, vcpu, KVM_ISA_VMX);
8251
8252 /*
8253 * Flush logged GPAs PML buffer, this will make dirty_bitmap more
8254 * updated. Another good is, in kvm_vm_ioctl_get_dirty_log, before
8255 * querying dirty_bitmap, we only need to kick all vcpus out of guest
8256 * mode as if vcpus is in root mode, the PML buffer must has been
8257 * flushed already.
8258 */
8259 if (enable_pml)
8260 vmx_flush_pml_buffer(vcpu);
8261
8262 /* If guest state is invalid, start emulating */
8263 if (vmx->emulation_required)
8264 return handle_invalid_guest_state(vcpu);
8265
8266 if (is_guest_mode(vcpu) && nested_vmx_exit_handled(vcpu)) {
8267 nested_vmx_vmexit(vcpu, exit_reason,
8268 vmcs_read32(VM_EXIT_INTR_INFO),
8269 vmcs_readl(EXIT_QUALIFICATION));
8270 return 1;
8271 }
8272
8273 if (exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY) {
8274 dump_vmcs();
8275 vcpu->run->exit_reason = KVM_EXIT_FAIL_ENTRY;
8276 vcpu->run->fail_entry.hardware_entry_failure_reason
8277 = exit_reason;
8278 return 0;
8279 }
8280
8281 if (unlikely(vmx->fail)) {
8282 vcpu->run->exit_reason = KVM_EXIT_FAIL_ENTRY;
8283 vcpu->run->fail_entry.hardware_entry_failure_reason
8284 = vmcs_read32(VM_INSTRUCTION_ERROR);
8285 return 0;
8286 }
8287
8288 /*
8289 * Note:
8290 * Do not try to fix EXIT_REASON_EPT_MISCONFIG if it caused by
8291 * delivery event since it indicates guest is accessing MMIO.
8292 * The vm-exit can be triggered again after return to guest that
8293 * will cause infinite loop.
8294 */
8295 if ((vectoring_info & VECTORING_INFO_VALID_MASK) &&
8296 (exit_reason != EXIT_REASON_EXCEPTION_NMI &&
8297 exit_reason != EXIT_REASON_EPT_VIOLATION &&
8298 exit_reason != EXIT_REASON_PML_FULL &&
8299 exit_reason != EXIT_REASON_TASK_SWITCH)) {
8300 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
8301 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_DELIVERY_EV;
8302 vcpu->run->internal.ndata = 2;
8303 vcpu->run->internal.data[0] = vectoring_info;
8304 vcpu->run->internal.data[1] = exit_reason;
8305 return 0;
8306 }
8307
8308 if (unlikely(!cpu_has_virtual_nmis() && vmx->soft_vnmi_blocked &&
8309 !(is_guest_mode(vcpu) && nested_cpu_has_virtual_nmis(
8310 get_vmcs12(vcpu))))) {
8311 if (vmx_interrupt_allowed(vcpu)) {
8312 vmx->soft_vnmi_blocked = 0;
8313 } else if (vmx->vnmi_blocked_time > 1000000000LL &&
8314 vcpu->arch.nmi_pending) {
8315 /*
8316 * This CPU don't support us in finding the end of an
8317 * NMI-blocked window if the guest runs with IRQs
8318 * disabled. So we pull the trigger after 1 s of
8319 * futile waiting, but inform the user about this.
8320 */
8321 printk(KERN_WARNING "%s: Breaking out of NMI-blocked "
8322 "state on VCPU %d after 1 s timeout\n",
8323 __func__, vcpu->vcpu_id);
8324 vmx->soft_vnmi_blocked = 0;
8325 }
8326 }
8327
8328 if (exit_reason < kvm_vmx_max_exit_handlers
8329 && kvm_vmx_exit_handlers[exit_reason])
8330 return kvm_vmx_exit_handlers[exit_reason](vcpu);
8331 else {
8332 WARN_ONCE(1, "vmx: unexpected exit reason 0x%x\n", exit_reason);
8333 kvm_queue_exception(vcpu, UD_VECTOR);
8334 return 1;
8335 }
8336 }
8337
8338 static void update_cr8_intercept(struct kvm_vcpu *vcpu, int tpr, int irr)
8339 {
8340 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
8341
8342 if (is_guest_mode(vcpu) &&
8343 nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW))
8344 return;
8345
8346 if (irr == -1 || tpr < irr) {
8347 vmcs_write32(TPR_THRESHOLD, 0);
8348 return;
8349 }
8350
8351 vmcs_write32(TPR_THRESHOLD, irr);
8352 }
8353
8354 static void vmx_set_virtual_x2apic_mode(struct kvm_vcpu *vcpu, bool set)
8355 {
8356 u32 sec_exec_control;
8357
8358 /* Postpone execution until vmcs01 is the current VMCS. */
8359 if (is_guest_mode(vcpu)) {
8360 to_vmx(vcpu)->nested.change_vmcs01_virtual_x2apic_mode = true;
8361 return;
8362 }
8363
8364 if (!cpu_has_vmx_virtualize_x2apic_mode())
8365 return;
8366
8367 if (!cpu_need_tpr_shadow(vcpu))
8368 return;
8369
8370 sec_exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
8371
8372 if (set) {
8373 sec_exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
8374 sec_exec_control |= SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
8375 } else {
8376 sec_exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
8377 sec_exec_control |= SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
8378 }
8379 vmcs_write32(SECONDARY_VM_EXEC_CONTROL, sec_exec_control);
8380
8381 vmx_set_msr_bitmap(vcpu);
8382 }
8383
8384 static void vmx_set_apic_access_page_addr(struct kvm_vcpu *vcpu, hpa_t hpa)
8385 {
8386 struct vcpu_vmx *vmx = to_vmx(vcpu);
8387
8388 /*
8389 * Currently we do not handle the nested case where L2 has an
8390 * APIC access page of its own; that page is still pinned.
8391 * Hence, we skip the case where the VCPU is in guest mode _and_
8392 * L1 prepared an APIC access page for L2.
8393 *
8394 * For the case where L1 and L2 share the same APIC access page
8395 * (flexpriority=Y but SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES clear
8396 * in the vmcs12), this function will only update either the vmcs01
8397 * or the vmcs02. If the former, the vmcs02 will be updated by
8398 * prepare_vmcs02. If the latter, the vmcs01 will be updated in
8399 * the next L2->L1 exit.
8400 */
8401 if (!is_guest_mode(vcpu) ||
8402 !nested_cpu_has2(get_vmcs12(&vmx->vcpu),
8403 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES))
8404 vmcs_write64(APIC_ACCESS_ADDR, hpa);
8405 }
8406
8407 static void vmx_hwapic_isr_update(struct kvm_vcpu *vcpu, int max_isr)
8408 {
8409 u16 status;
8410 u8 old;
8411
8412 if (max_isr == -1)
8413 max_isr = 0;
8414
8415 status = vmcs_read16(GUEST_INTR_STATUS);
8416 old = status >> 8;
8417 if (max_isr != old) {
8418 status &= 0xff;
8419 status |= max_isr << 8;
8420 vmcs_write16(GUEST_INTR_STATUS, status);
8421 }
8422 }
8423
8424 static void vmx_set_rvi(int vector)
8425 {
8426 u16 status;
8427 u8 old;
8428
8429 if (vector == -1)
8430 vector = 0;
8431
8432 status = vmcs_read16(GUEST_INTR_STATUS);
8433 old = (u8)status & 0xff;
8434 if ((u8)vector != old) {
8435 status &= ~0xff;
8436 status |= (u8)vector;
8437 vmcs_write16(GUEST_INTR_STATUS, status);
8438 }
8439 }
8440
8441 static void vmx_hwapic_irr_update(struct kvm_vcpu *vcpu, int max_irr)
8442 {
8443 if (!is_guest_mode(vcpu)) {
8444 vmx_set_rvi(max_irr);
8445 return;
8446 }
8447
8448 if (max_irr == -1)
8449 return;
8450
8451 /*
8452 * In guest mode. If a vmexit is needed, vmx_check_nested_events
8453 * handles it.
8454 */
8455 if (nested_exit_on_intr(vcpu))
8456 return;
8457
8458 /*
8459 * Else, fall back to pre-APICv interrupt injection since L2
8460 * is run without virtual interrupt delivery.
8461 */
8462 if (!kvm_event_needs_reinjection(vcpu) &&
8463 vmx_interrupt_allowed(vcpu)) {
8464 kvm_queue_interrupt(vcpu, max_irr, false);
8465 vmx_inject_irq(vcpu);
8466 }
8467 }
8468
8469 static void vmx_load_eoi_exitmap(struct kvm_vcpu *vcpu, u64 *eoi_exit_bitmap)
8470 {
8471 if (!kvm_vcpu_apicv_active(vcpu))
8472 return;
8473
8474 vmcs_write64(EOI_EXIT_BITMAP0, eoi_exit_bitmap[0]);
8475 vmcs_write64(EOI_EXIT_BITMAP1, eoi_exit_bitmap[1]);
8476 vmcs_write64(EOI_EXIT_BITMAP2, eoi_exit_bitmap[2]);
8477 vmcs_write64(EOI_EXIT_BITMAP3, eoi_exit_bitmap[3]);
8478 }
8479
8480 static void vmx_complete_atomic_exit(struct vcpu_vmx *vmx)
8481 {
8482 u32 exit_intr_info;
8483
8484 if (!(vmx->exit_reason == EXIT_REASON_MCE_DURING_VMENTRY
8485 || vmx->exit_reason == EXIT_REASON_EXCEPTION_NMI))
8486 return;
8487
8488 vmx->exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
8489 exit_intr_info = vmx->exit_intr_info;
8490
8491 /* Handle machine checks before interrupts are enabled */
8492 if (is_machine_check(exit_intr_info))
8493 kvm_machine_check();
8494
8495 /* We need to handle NMIs before interrupts are enabled */
8496 if ((exit_intr_info & INTR_INFO_INTR_TYPE_MASK) == INTR_TYPE_NMI_INTR &&
8497 (exit_intr_info & INTR_INFO_VALID_MASK)) {
8498 kvm_before_handle_nmi(&vmx->vcpu);
8499 asm("int $2");
8500 kvm_after_handle_nmi(&vmx->vcpu);
8501 }
8502 }
8503
8504 static void vmx_handle_external_intr(struct kvm_vcpu *vcpu)
8505 {
8506 u32 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
8507 register void *__sp asm(_ASM_SP);
8508
8509 if ((exit_intr_info & (INTR_INFO_VALID_MASK | INTR_INFO_INTR_TYPE_MASK))
8510 == (INTR_INFO_VALID_MASK | INTR_TYPE_EXT_INTR)) {
8511 unsigned int vector;
8512 unsigned long entry;
8513 gate_desc *desc;
8514 struct vcpu_vmx *vmx = to_vmx(vcpu);
8515 #ifdef CONFIG_X86_64
8516 unsigned long tmp;
8517 #endif
8518
8519 vector = exit_intr_info & INTR_INFO_VECTOR_MASK;
8520 desc = (gate_desc *)vmx->host_idt_base + vector;
8521 entry = gate_offset(*desc);
8522 asm volatile(
8523 #ifdef CONFIG_X86_64
8524 "mov %%" _ASM_SP ", %[sp]\n\t"
8525 "and $0xfffffffffffffff0, %%" _ASM_SP "\n\t"
8526 "push $%c[ss]\n\t"
8527 "push %[sp]\n\t"
8528 #endif
8529 "pushf\n\t"
8530 __ASM_SIZE(push) " $%c[cs]\n\t"
8531 "call *%[entry]\n\t"
8532 :
8533 #ifdef CONFIG_X86_64
8534 [sp]"=&r"(tmp),
8535 #endif
8536 "+r"(__sp)
8537 :
8538 [entry]"r"(entry),
8539 [ss]"i"(__KERNEL_DS),
8540 [cs]"i"(__KERNEL_CS)
8541 );
8542 }
8543 }
8544
8545 static bool vmx_has_high_real_mode_segbase(void)
8546 {
8547 return enable_unrestricted_guest || emulate_invalid_guest_state;
8548 }
8549
8550 static bool vmx_mpx_supported(void)
8551 {
8552 return (vmcs_config.vmexit_ctrl & VM_EXIT_CLEAR_BNDCFGS) &&
8553 (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_BNDCFGS);
8554 }
8555
8556 static bool vmx_xsaves_supported(void)
8557 {
8558 return vmcs_config.cpu_based_2nd_exec_ctrl &
8559 SECONDARY_EXEC_XSAVES;
8560 }
8561
8562 static void vmx_recover_nmi_blocking(struct vcpu_vmx *vmx)
8563 {
8564 u32 exit_intr_info;
8565 bool unblock_nmi;
8566 u8 vector;
8567 bool idtv_info_valid;
8568
8569 idtv_info_valid = vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK;
8570
8571 if (cpu_has_virtual_nmis()) {
8572 if (vmx->nmi_known_unmasked)
8573 return;
8574 /*
8575 * Can't use vmx->exit_intr_info since we're not sure what
8576 * the exit reason is.
8577 */
8578 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
8579 unblock_nmi = (exit_intr_info & INTR_INFO_UNBLOCK_NMI) != 0;
8580 vector = exit_intr_info & INTR_INFO_VECTOR_MASK;
8581 /*
8582 * SDM 3: 27.7.1.2 (September 2008)
8583 * Re-set bit "block by NMI" before VM entry if vmexit caused by
8584 * a guest IRET fault.
8585 * SDM 3: 23.2.2 (September 2008)
8586 * Bit 12 is undefined in any of the following cases:
8587 * If the VM exit sets the valid bit in the IDT-vectoring
8588 * information field.
8589 * If the VM exit is due to a double fault.
8590 */
8591 if ((exit_intr_info & INTR_INFO_VALID_MASK) && unblock_nmi &&
8592 vector != DF_VECTOR && !idtv_info_valid)
8593 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
8594 GUEST_INTR_STATE_NMI);
8595 else
8596 vmx->nmi_known_unmasked =
8597 !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO)
8598 & GUEST_INTR_STATE_NMI);
8599 } else if (unlikely(vmx->soft_vnmi_blocked))
8600 vmx->vnmi_blocked_time +=
8601 ktime_to_ns(ktime_sub(ktime_get(), vmx->entry_time));
8602 }
8603
8604 static void __vmx_complete_interrupts(struct kvm_vcpu *vcpu,
8605 u32 idt_vectoring_info,
8606 int instr_len_field,
8607 int error_code_field)
8608 {
8609 u8 vector;
8610 int type;
8611 bool idtv_info_valid;
8612
8613 idtv_info_valid = idt_vectoring_info & VECTORING_INFO_VALID_MASK;
8614
8615 vcpu->arch.nmi_injected = false;
8616 kvm_clear_exception_queue(vcpu);
8617 kvm_clear_interrupt_queue(vcpu);
8618
8619 if (!idtv_info_valid)
8620 return;
8621
8622 kvm_make_request(KVM_REQ_EVENT, vcpu);
8623
8624 vector = idt_vectoring_info & VECTORING_INFO_VECTOR_MASK;
8625 type = idt_vectoring_info & VECTORING_INFO_TYPE_MASK;
8626
8627 switch (type) {
8628 case INTR_TYPE_NMI_INTR:
8629 vcpu->arch.nmi_injected = true;
8630 /*
8631 * SDM 3: 27.7.1.2 (September 2008)
8632 * Clear bit "block by NMI" before VM entry if a NMI
8633 * delivery faulted.
8634 */
8635 vmx_set_nmi_mask(vcpu, false);
8636 break;
8637 case INTR_TYPE_SOFT_EXCEPTION:
8638 vcpu->arch.event_exit_inst_len = vmcs_read32(instr_len_field);
8639 /* fall through */
8640 case INTR_TYPE_HARD_EXCEPTION:
8641 if (idt_vectoring_info & VECTORING_INFO_DELIVER_CODE_MASK) {
8642 u32 err = vmcs_read32(error_code_field);
8643 kvm_requeue_exception_e(vcpu, vector, err);
8644 } else
8645 kvm_requeue_exception(vcpu, vector);
8646 break;
8647 case INTR_TYPE_SOFT_INTR:
8648 vcpu->arch.event_exit_inst_len = vmcs_read32(instr_len_field);
8649 /* fall through */
8650 case INTR_TYPE_EXT_INTR:
8651 kvm_queue_interrupt(vcpu, vector, type == INTR_TYPE_SOFT_INTR);
8652 break;
8653 default:
8654 break;
8655 }
8656 }
8657
8658 static void vmx_complete_interrupts(struct vcpu_vmx *vmx)
8659 {
8660 __vmx_complete_interrupts(&vmx->vcpu, vmx->idt_vectoring_info,
8661 VM_EXIT_INSTRUCTION_LEN,
8662 IDT_VECTORING_ERROR_CODE);
8663 }
8664
8665 static void vmx_cancel_injection(struct kvm_vcpu *vcpu)
8666 {
8667 __vmx_complete_interrupts(vcpu,
8668 vmcs_read32(VM_ENTRY_INTR_INFO_FIELD),
8669 VM_ENTRY_INSTRUCTION_LEN,
8670 VM_ENTRY_EXCEPTION_ERROR_CODE);
8671
8672 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0);
8673 }
8674
8675 static void atomic_switch_perf_msrs(struct vcpu_vmx *vmx)
8676 {
8677 int i, nr_msrs;
8678 struct perf_guest_switch_msr *msrs;
8679
8680 msrs = perf_guest_get_msrs(&nr_msrs);
8681
8682 if (!msrs)
8683 return;
8684
8685 for (i = 0; i < nr_msrs; i++)
8686 if (msrs[i].host == msrs[i].guest)
8687 clear_atomic_switch_msr(vmx, msrs[i].msr);
8688 else
8689 add_atomic_switch_msr(vmx, msrs[i].msr, msrs[i].guest,
8690 msrs[i].host);
8691 }
8692
8693 static void vmx_arm_hv_timer(struct kvm_vcpu *vcpu)
8694 {
8695 struct vcpu_vmx *vmx = to_vmx(vcpu);
8696 u64 tscl;
8697 u32 delta_tsc;
8698
8699 if (vmx->hv_deadline_tsc == -1)
8700 return;
8701
8702 tscl = rdtsc();
8703 if (vmx->hv_deadline_tsc > tscl)
8704 /* sure to be 32 bit only because checked on set_hv_timer */
8705 delta_tsc = (u32)((vmx->hv_deadline_tsc - tscl) >>
8706 cpu_preemption_timer_multi);
8707 else
8708 delta_tsc = 0;
8709
8710 vmcs_write32(VMX_PREEMPTION_TIMER_VALUE, delta_tsc);
8711 }
8712
8713 static void __noclone vmx_vcpu_run(struct kvm_vcpu *vcpu)
8714 {
8715 struct vcpu_vmx *vmx = to_vmx(vcpu);
8716 unsigned long debugctlmsr, cr4;
8717
8718 /* Record the guest's net vcpu time for enforced NMI injections. */
8719 if (unlikely(!cpu_has_virtual_nmis() && vmx->soft_vnmi_blocked))
8720 vmx->entry_time = ktime_get();
8721
8722 /* Don't enter VMX if guest state is invalid, let the exit handler
8723 start emulation until we arrive back to a valid state */
8724 if (vmx->emulation_required)
8725 return;
8726
8727 if (vmx->ple_window_dirty) {
8728 vmx->ple_window_dirty = false;
8729 vmcs_write32(PLE_WINDOW, vmx->ple_window);
8730 }
8731
8732 if (vmx->nested.sync_shadow_vmcs) {
8733 copy_vmcs12_to_shadow(vmx);
8734 vmx->nested.sync_shadow_vmcs = false;
8735 }
8736
8737 if (test_bit(VCPU_REGS_RSP, (unsigned long *)&vcpu->arch.regs_dirty))
8738 vmcs_writel(GUEST_RSP, vcpu->arch.regs[VCPU_REGS_RSP]);
8739 if (test_bit(VCPU_REGS_RIP, (unsigned long *)&vcpu->arch.regs_dirty))
8740 vmcs_writel(GUEST_RIP, vcpu->arch.regs[VCPU_REGS_RIP]);
8741
8742 cr4 = cr4_read_shadow();
8743 if (unlikely(cr4 != vmx->host_state.vmcs_host_cr4)) {
8744 vmcs_writel(HOST_CR4, cr4);
8745 vmx->host_state.vmcs_host_cr4 = cr4;
8746 }
8747
8748 /* When single-stepping over STI and MOV SS, we must clear the
8749 * corresponding interruptibility bits in the guest state. Otherwise
8750 * vmentry fails as it then expects bit 14 (BS) in pending debug
8751 * exceptions being set, but that's not correct for the guest debugging
8752 * case. */
8753 if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP)
8754 vmx_set_interrupt_shadow(vcpu, 0);
8755
8756 if (vmx->guest_pkru_valid)
8757 __write_pkru(vmx->guest_pkru);
8758
8759 atomic_switch_perf_msrs(vmx);
8760 debugctlmsr = get_debugctlmsr();
8761
8762 vmx_arm_hv_timer(vcpu);
8763
8764 vmx->__launched = vmx->loaded_vmcs->launched;
8765 asm(
8766 /* Store host registers */
8767 "push %%" _ASM_DX "; push %%" _ASM_BP ";"
8768 "push %%" _ASM_CX " \n\t" /* placeholder for guest rcx */
8769 "push %%" _ASM_CX " \n\t"
8770 "cmp %%" _ASM_SP ", %c[host_rsp](%0) \n\t"
8771 "je 1f \n\t"
8772 "mov %%" _ASM_SP ", %c[host_rsp](%0) \n\t"
8773 __ex(ASM_VMX_VMWRITE_RSP_RDX) "\n\t"
8774 "1: \n\t"
8775 /* Reload cr2 if changed */
8776 "mov %c[cr2](%0), %%" _ASM_AX " \n\t"
8777 "mov %%cr2, %%" _ASM_DX " \n\t"
8778 "cmp %%" _ASM_AX ", %%" _ASM_DX " \n\t"
8779 "je 2f \n\t"
8780 "mov %%" _ASM_AX", %%cr2 \n\t"
8781 "2: \n\t"
8782 /* Check if vmlaunch of vmresume is needed */
8783 "cmpl $0, %c[launched](%0) \n\t"
8784 /* Load guest registers. Don't clobber flags. */
8785 "mov %c[rax](%0), %%" _ASM_AX " \n\t"
8786 "mov %c[rbx](%0), %%" _ASM_BX " \n\t"
8787 "mov %c[rdx](%0), %%" _ASM_DX " \n\t"
8788 "mov %c[rsi](%0), %%" _ASM_SI " \n\t"
8789 "mov %c[rdi](%0), %%" _ASM_DI " \n\t"
8790 "mov %c[rbp](%0), %%" _ASM_BP " \n\t"
8791 #ifdef CONFIG_X86_64
8792 "mov %c[r8](%0), %%r8 \n\t"
8793 "mov %c[r9](%0), %%r9 \n\t"
8794 "mov %c[r10](%0), %%r10 \n\t"
8795 "mov %c[r11](%0), %%r11 \n\t"
8796 "mov %c[r12](%0), %%r12 \n\t"
8797 "mov %c[r13](%0), %%r13 \n\t"
8798 "mov %c[r14](%0), %%r14 \n\t"
8799 "mov %c[r15](%0), %%r15 \n\t"
8800 #endif
8801 "mov %c[rcx](%0), %%" _ASM_CX " \n\t" /* kills %0 (ecx) */
8802
8803 /* Enter guest mode */
8804 "jne 1f \n\t"
8805 __ex(ASM_VMX_VMLAUNCH) "\n\t"
8806 "jmp 2f \n\t"
8807 "1: " __ex(ASM_VMX_VMRESUME) "\n\t"
8808 "2: "
8809 /* Save guest registers, load host registers, keep flags */
8810 "mov %0, %c[wordsize](%%" _ASM_SP ") \n\t"
8811 "pop %0 \n\t"
8812 "mov %%" _ASM_AX ", %c[rax](%0) \n\t"
8813 "mov %%" _ASM_BX ", %c[rbx](%0) \n\t"
8814 __ASM_SIZE(pop) " %c[rcx](%0) \n\t"
8815 "mov %%" _ASM_DX ", %c[rdx](%0) \n\t"
8816 "mov %%" _ASM_SI ", %c[rsi](%0) \n\t"
8817 "mov %%" _ASM_DI ", %c[rdi](%0) \n\t"
8818 "mov %%" _ASM_BP ", %c[rbp](%0) \n\t"
8819 #ifdef CONFIG_X86_64
8820 "mov %%r8, %c[r8](%0) \n\t"
8821 "mov %%r9, %c[r9](%0) \n\t"
8822 "mov %%r10, %c[r10](%0) \n\t"
8823 "mov %%r11, %c[r11](%0) \n\t"
8824 "mov %%r12, %c[r12](%0) \n\t"
8825 "mov %%r13, %c[r13](%0) \n\t"
8826 "mov %%r14, %c[r14](%0) \n\t"
8827 "mov %%r15, %c[r15](%0) \n\t"
8828 #endif
8829 "mov %%cr2, %%" _ASM_AX " \n\t"
8830 "mov %%" _ASM_AX ", %c[cr2](%0) \n\t"
8831
8832 "pop %%" _ASM_BP "; pop %%" _ASM_DX " \n\t"
8833 "setbe %c[fail](%0) \n\t"
8834 ".pushsection .rodata \n\t"
8835 ".global vmx_return \n\t"
8836 "vmx_return: " _ASM_PTR " 2b \n\t"
8837 ".popsection"
8838 : : "c"(vmx), "d"((unsigned long)HOST_RSP),
8839 [launched]"i"(offsetof(struct vcpu_vmx, __launched)),
8840 [fail]"i"(offsetof(struct vcpu_vmx, fail)),
8841 [host_rsp]"i"(offsetof(struct vcpu_vmx, host_rsp)),
8842 [rax]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RAX])),
8843 [rbx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBX])),
8844 [rcx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RCX])),
8845 [rdx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDX])),
8846 [rsi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RSI])),
8847 [rdi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDI])),
8848 [rbp]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBP])),
8849 #ifdef CONFIG_X86_64
8850 [r8]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R8])),
8851 [r9]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R9])),
8852 [r10]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R10])),
8853 [r11]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R11])),
8854 [r12]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R12])),
8855 [r13]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R13])),
8856 [r14]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R14])),
8857 [r15]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R15])),
8858 #endif
8859 [cr2]"i"(offsetof(struct vcpu_vmx, vcpu.arch.cr2)),
8860 [wordsize]"i"(sizeof(ulong))
8861 : "cc", "memory"
8862 #ifdef CONFIG_X86_64
8863 , "rax", "rbx", "rdi", "rsi"
8864 , "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15"
8865 #else
8866 , "eax", "ebx", "edi", "esi"
8867 #endif
8868 );
8869
8870 /* MSR_IA32_DEBUGCTLMSR is zeroed on vmexit. Restore it if needed */
8871 if (debugctlmsr)
8872 update_debugctlmsr(debugctlmsr);
8873
8874 #ifndef CONFIG_X86_64
8875 /*
8876 * The sysexit path does not restore ds/es, so we must set them to
8877 * a reasonable value ourselves.
8878 *
8879 * We can't defer this to vmx_load_host_state() since that function
8880 * may be executed in interrupt context, which saves and restore segments
8881 * around it, nullifying its effect.
8882 */
8883 loadsegment(ds, __USER_DS);
8884 loadsegment(es, __USER_DS);
8885 #endif
8886
8887 vcpu->arch.regs_avail = ~((1 << VCPU_REGS_RIP) | (1 << VCPU_REGS_RSP)
8888 | (1 << VCPU_EXREG_RFLAGS)
8889 | (1 << VCPU_EXREG_PDPTR)
8890 | (1 << VCPU_EXREG_SEGMENTS)
8891 | (1 << VCPU_EXREG_CR3));
8892 vcpu->arch.regs_dirty = 0;
8893
8894 vmx->idt_vectoring_info = vmcs_read32(IDT_VECTORING_INFO_FIELD);
8895
8896 vmx->loaded_vmcs->launched = 1;
8897
8898 vmx->exit_reason = vmcs_read32(VM_EXIT_REASON);
8899
8900 /*
8901 * eager fpu is enabled if PKEY is supported and CR4 is switched
8902 * back on host, so it is safe to read guest PKRU from current
8903 * XSAVE.
8904 */
8905 if (boot_cpu_has(X86_FEATURE_OSPKE)) {
8906 vmx->guest_pkru = __read_pkru();
8907 if (vmx->guest_pkru != vmx->host_pkru) {
8908 vmx->guest_pkru_valid = true;
8909 __write_pkru(vmx->host_pkru);
8910 } else
8911 vmx->guest_pkru_valid = false;
8912 }
8913
8914 /*
8915 * the KVM_REQ_EVENT optimization bit is only on for one entry, and if
8916 * we did not inject a still-pending event to L1 now because of
8917 * nested_run_pending, we need to re-enable this bit.
8918 */
8919 if (vmx->nested.nested_run_pending)
8920 kvm_make_request(KVM_REQ_EVENT, vcpu);
8921
8922 vmx->nested.nested_run_pending = 0;
8923
8924 vmx_complete_atomic_exit(vmx);
8925 vmx_recover_nmi_blocking(vmx);
8926 vmx_complete_interrupts(vmx);
8927 }
8928
8929 static void vmx_load_vmcs01(struct kvm_vcpu *vcpu)
8930 {
8931 struct vcpu_vmx *vmx = to_vmx(vcpu);
8932 int cpu;
8933
8934 if (vmx->loaded_vmcs == &vmx->vmcs01)
8935 return;
8936
8937 cpu = get_cpu();
8938 vmx->loaded_vmcs = &vmx->vmcs01;
8939 vmx_vcpu_put(vcpu);
8940 vmx_vcpu_load(vcpu, cpu);
8941 vcpu->cpu = cpu;
8942 put_cpu();
8943 }
8944
8945 /*
8946 * Ensure that the current vmcs of the logical processor is the
8947 * vmcs01 of the vcpu before calling free_nested().
8948 */
8949 static void vmx_free_vcpu_nested(struct kvm_vcpu *vcpu)
8950 {
8951 struct vcpu_vmx *vmx = to_vmx(vcpu);
8952 int r;
8953
8954 r = vcpu_load(vcpu);
8955 BUG_ON(r);
8956 vmx_load_vmcs01(vcpu);
8957 free_nested(vmx);
8958 vcpu_put(vcpu);
8959 }
8960
8961 static void vmx_free_vcpu(struct kvm_vcpu *vcpu)
8962 {
8963 struct vcpu_vmx *vmx = to_vmx(vcpu);
8964
8965 if (enable_pml)
8966 vmx_destroy_pml_buffer(vmx);
8967 free_vpid(vmx->vpid);
8968 leave_guest_mode(vcpu);
8969 vmx_free_vcpu_nested(vcpu);
8970 free_loaded_vmcs(vmx->loaded_vmcs);
8971 kfree(vmx->guest_msrs);
8972 kvm_vcpu_uninit(vcpu);
8973 kmem_cache_free(kvm_vcpu_cache, vmx);
8974 }
8975
8976 static struct kvm_vcpu *vmx_create_vcpu(struct kvm *kvm, unsigned int id)
8977 {
8978 int err;
8979 struct vcpu_vmx *vmx = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
8980 int cpu;
8981
8982 if (!vmx)
8983 return ERR_PTR(-ENOMEM);
8984
8985 vmx->vpid = allocate_vpid();
8986
8987 err = kvm_vcpu_init(&vmx->vcpu, kvm, id);
8988 if (err)
8989 goto free_vcpu;
8990
8991 err = -ENOMEM;
8992
8993 /*
8994 * If PML is turned on, failure on enabling PML just results in failure
8995 * of creating the vcpu, therefore we can simplify PML logic (by
8996 * avoiding dealing with cases, such as enabling PML partially on vcpus
8997 * for the guest, etc.
8998 */
8999 if (enable_pml) {
9000 vmx->pml_pg = alloc_page(GFP_KERNEL | __GFP_ZERO);
9001 if (!vmx->pml_pg)
9002 goto uninit_vcpu;
9003 }
9004
9005 vmx->guest_msrs = kmalloc(PAGE_SIZE, GFP_KERNEL);
9006 BUILD_BUG_ON(ARRAY_SIZE(vmx_msr_index) * sizeof(vmx->guest_msrs[0])
9007 > PAGE_SIZE);
9008
9009 if (!vmx->guest_msrs)
9010 goto free_pml;
9011
9012 vmx->loaded_vmcs = &vmx->vmcs01;
9013 vmx->loaded_vmcs->vmcs = alloc_vmcs();
9014 vmx->loaded_vmcs->shadow_vmcs = NULL;
9015 if (!vmx->loaded_vmcs->vmcs)
9016 goto free_msrs;
9017 if (!vmm_exclusive)
9018 kvm_cpu_vmxon(__pa(per_cpu(vmxarea, raw_smp_processor_id())));
9019 loaded_vmcs_init(vmx->loaded_vmcs);
9020 if (!vmm_exclusive)
9021 kvm_cpu_vmxoff();
9022
9023 cpu = get_cpu();
9024 vmx_vcpu_load(&vmx->vcpu, cpu);
9025 vmx->vcpu.cpu = cpu;
9026 err = vmx_vcpu_setup(vmx);
9027 vmx_vcpu_put(&vmx->vcpu);
9028 put_cpu();
9029 if (err)
9030 goto free_vmcs;
9031 if (cpu_need_virtualize_apic_accesses(&vmx->vcpu)) {
9032 err = alloc_apic_access_page(kvm);
9033 if (err)
9034 goto free_vmcs;
9035 }
9036
9037 if (enable_ept) {
9038 if (!kvm->arch.ept_identity_map_addr)
9039 kvm->arch.ept_identity_map_addr =
9040 VMX_EPT_IDENTITY_PAGETABLE_ADDR;
9041 err = init_rmode_identity_map(kvm);
9042 if (err)
9043 goto free_vmcs;
9044 }
9045
9046 if (nested) {
9047 nested_vmx_setup_ctls_msrs(vmx);
9048 vmx->nested.vpid02 = allocate_vpid();
9049 }
9050
9051 vmx->nested.posted_intr_nv = -1;
9052 vmx->nested.current_vmptr = -1ull;
9053 vmx->nested.current_vmcs12 = NULL;
9054
9055 vmx->msr_ia32_feature_control_valid_bits = FEATURE_CONTROL_LOCKED;
9056
9057 return &vmx->vcpu;
9058
9059 free_vmcs:
9060 free_vpid(vmx->nested.vpid02);
9061 free_loaded_vmcs(vmx->loaded_vmcs);
9062 free_msrs:
9063 kfree(vmx->guest_msrs);
9064 free_pml:
9065 vmx_destroy_pml_buffer(vmx);
9066 uninit_vcpu:
9067 kvm_vcpu_uninit(&vmx->vcpu);
9068 free_vcpu:
9069 free_vpid(vmx->vpid);
9070 kmem_cache_free(kvm_vcpu_cache, vmx);
9071 return ERR_PTR(err);
9072 }
9073
9074 static void __init vmx_check_processor_compat(void *rtn)
9075 {
9076 struct vmcs_config vmcs_conf;
9077
9078 *(int *)rtn = 0;
9079 if (setup_vmcs_config(&vmcs_conf) < 0)
9080 *(int *)rtn = -EIO;
9081 if (memcmp(&vmcs_config, &vmcs_conf, sizeof(struct vmcs_config)) != 0) {
9082 printk(KERN_ERR "kvm: CPU %d feature inconsistency!\n",
9083 smp_processor_id());
9084 *(int *)rtn = -EIO;
9085 }
9086 }
9087
9088 static int get_ept_level(void)
9089 {
9090 return VMX_EPT_DEFAULT_GAW + 1;
9091 }
9092
9093 static u64 vmx_get_mt_mask(struct kvm_vcpu *vcpu, gfn_t gfn, bool is_mmio)
9094 {
9095 u8 cache;
9096 u64 ipat = 0;
9097
9098 /* For VT-d and EPT combination
9099 * 1. MMIO: always map as UC
9100 * 2. EPT with VT-d:
9101 * a. VT-d without snooping control feature: can't guarantee the
9102 * result, try to trust guest.
9103 * b. VT-d with snooping control feature: snooping control feature of
9104 * VT-d engine can guarantee the cache correctness. Just set it
9105 * to WB to keep consistent with host. So the same as item 3.
9106 * 3. EPT without VT-d: always map as WB and set IPAT=1 to keep
9107 * consistent with host MTRR
9108 */
9109 if (is_mmio) {
9110 cache = MTRR_TYPE_UNCACHABLE;
9111 goto exit;
9112 }
9113
9114 if (!kvm_arch_has_noncoherent_dma(vcpu->kvm)) {
9115 ipat = VMX_EPT_IPAT_BIT;
9116 cache = MTRR_TYPE_WRBACK;
9117 goto exit;
9118 }
9119
9120 if (kvm_read_cr0(vcpu) & X86_CR0_CD) {
9121 ipat = VMX_EPT_IPAT_BIT;
9122 if (kvm_check_has_quirk(vcpu->kvm, KVM_X86_QUIRK_CD_NW_CLEARED))
9123 cache = MTRR_TYPE_WRBACK;
9124 else
9125 cache = MTRR_TYPE_UNCACHABLE;
9126 goto exit;
9127 }
9128
9129 cache = kvm_mtrr_get_guest_memory_type(vcpu, gfn);
9130
9131 exit:
9132 return (cache << VMX_EPT_MT_EPTE_SHIFT) | ipat;
9133 }
9134
9135 static int vmx_get_lpage_level(void)
9136 {
9137 if (enable_ept && !cpu_has_vmx_ept_1g_page())
9138 return PT_DIRECTORY_LEVEL;
9139 else
9140 /* For shadow and EPT supported 1GB page */
9141 return PT_PDPE_LEVEL;
9142 }
9143
9144 static void vmcs_set_secondary_exec_control(u32 new_ctl)
9145 {
9146 /*
9147 * These bits in the secondary execution controls field
9148 * are dynamic, the others are mostly based on the hypervisor
9149 * architecture and the guest's CPUID. Do not touch the
9150 * dynamic bits.
9151 */
9152 u32 mask =
9153 SECONDARY_EXEC_SHADOW_VMCS |
9154 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
9155 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
9156
9157 u32 cur_ctl = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
9158
9159 vmcs_write32(SECONDARY_VM_EXEC_CONTROL,
9160 (new_ctl & ~mask) | (cur_ctl & mask));
9161 }
9162
9163 static void vmx_cpuid_update(struct kvm_vcpu *vcpu)
9164 {
9165 struct kvm_cpuid_entry2 *best;
9166 struct vcpu_vmx *vmx = to_vmx(vcpu);
9167 u32 secondary_exec_ctl = vmx_secondary_exec_control(vmx);
9168
9169 if (vmx_rdtscp_supported()) {
9170 bool rdtscp_enabled = guest_cpuid_has_rdtscp(vcpu);
9171 if (!rdtscp_enabled)
9172 secondary_exec_ctl &= ~SECONDARY_EXEC_RDTSCP;
9173
9174 if (nested) {
9175 if (rdtscp_enabled)
9176 vmx->nested.nested_vmx_secondary_ctls_high |=
9177 SECONDARY_EXEC_RDTSCP;
9178 else
9179 vmx->nested.nested_vmx_secondary_ctls_high &=
9180 ~SECONDARY_EXEC_RDTSCP;
9181 }
9182 }
9183
9184 /* Exposing INVPCID only when PCID is exposed */
9185 best = kvm_find_cpuid_entry(vcpu, 0x7, 0);
9186 if (vmx_invpcid_supported() &&
9187 (!best || !(best->ebx & bit(X86_FEATURE_INVPCID)) ||
9188 !guest_cpuid_has_pcid(vcpu))) {
9189 secondary_exec_ctl &= ~SECONDARY_EXEC_ENABLE_INVPCID;
9190
9191 if (best)
9192 best->ebx &= ~bit(X86_FEATURE_INVPCID);
9193 }
9194
9195 if (cpu_has_secondary_exec_ctrls())
9196 vmcs_set_secondary_exec_control(secondary_exec_ctl);
9197
9198 if (nested_vmx_allowed(vcpu))
9199 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits |=
9200 FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
9201 else
9202 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits &=
9203 ~FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
9204 }
9205
9206 static void vmx_set_supported_cpuid(u32 func, struct kvm_cpuid_entry2 *entry)
9207 {
9208 if (func == 1 && nested)
9209 entry->ecx |= bit(X86_FEATURE_VMX);
9210 }
9211
9212 static void nested_ept_inject_page_fault(struct kvm_vcpu *vcpu,
9213 struct x86_exception *fault)
9214 {
9215 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
9216 u32 exit_reason;
9217
9218 if (fault->error_code & PFERR_RSVD_MASK)
9219 exit_reason = EXIT_REASON_EPT_MISCONFIG;
9220 else
9221 exit_reason = EXIT_REASON_EPT_VIOLATION;
9222 nested_vmx_vmexit(vcpu, exit_reason, 0, vcpu->arch.exit_qualification);
9223 vmcs12->guest_physical_address = fault->address;
9224 }
9225
9226 /* Callbacks for nested_ept_init_mmu_context: */
9227
9228 static unsigned long nested_ept_get_cr3(struct kvm_vcpu *vcpu)
9229 {
9230 /* return the page table to be shadowed - in our case, EPT12 */
9231 return get_vmcs12(vcpu)->ept_pointer;
9232 }
9233
9234 static void nested_ept_init_mmu_context(struct kvm_vcpu *vcpu)
9235 {
9236 WARN_ON(mmu_is_nested(vcpu));
9237 kvm_init_shadow_ept_mmu(vcpu,
9238 to_vmx(vcpu)->nested.nested_vmx_ept_caps &
9239 VMX_EPT_EXECUTE_ONLY_BIT);
9240 vcpu->arch.mmu.set_cr3 = vmx_set_cr3;
9241 vcpu->arch.mmu.get_cr3 = nested_ept_get_cr3;
9242 vcpu->arch.mmu.inject_page_fault = nested_ept_inject_page_fault;
9243
9244 vcpu->arch.walk_mmu = &vcpu->arch.nested_mmu;
9245 }
9246
9247 static void nested_ept_uninit_mmu_context(struct kvm_vcpu *vcpu)
9248 {
9249 vcpu->arch.walk_mmu = &vcpu->arch.mmu;
9250 }
9251
9252 static bool nested_vmx_is_page_fault_vmexit(struct vmcs12 *vmcs12,
9253 u16 error_code)
9254 {
9255 bool inequality, bit;
9256
9257 bit = (vmcs12->exception_bitmap & (1u << PF_VECTOR)) != 0;
9258 inequality =
9259 (error_code & vmcs12->page_fault_error_code_mask) !=
9260 vmcs12->page_fault_error_code_match;
9261 return inequality ^ bit;
9262 }
9263
9264 static void vmx_inject_page_fault_nested(struct kvm_vcpu *vcpu,
9265 struct x86_exception *fault)
9266 {
9267 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
9268
9269 WARN_ON(!is_guest_mode(vcpu));
9270
9271 if (nested_vmx_is_page_fault_vmexit(vmcs12, fault->error_code))
9272 nested_vmx_vmexit(vcpu, to_vmx(vcpu)->exit_reason,
9273 vmcs_read32(VM_EXIT_INTR_INFO),
9274 vmcs_readl(EXIT_QUALIFICATION));
9275 else
9276 kvm_inject_page_fault(vcpu, fault);
9277 }
9278
9279 static bool nested_get_vmcs12_pages(struct kvm_vcpu *vcpu,
9280 struct vmcs12 *vmcs12)
9281 {
9282 struct vcpu_vmx *vmx = to_vmx(vcpu);
9283 int maxphyaddr = cpuid_maxphyaddr(vcpu);
9284
9285 if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) {
9286 if (!PAGE_ALIGNED(vmcs12->apic_access_addr) ||
9287 vmcs12->apic_access_addr >> maxphyaddr)
9288 return false;
9289
9290 /*
9291 * Translate L1 physical address to host physical
9292 * address for vmcs02. Keep the page pinned, so this
9293 * physical address remains valid. We keep a reference
9294 * to it so we can release it later.
9295 */
9296 if (vmx->nested.apic_access_page) /* shouldn't happen */
9297 nested_release_page(vmx->nested.apic_access_page);
9298 vmx->nested.apic_access_page =
9299 nested_get_page(vcpu, vmcs12->apic_access_addr);
9300 }
9301
9302 if (nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW)) {
9303 if (!PAGE_ALIGNED(vmcs12->virtual_apic_page_addr) ||
9304 vmcs12->virtual_apic_page_addr >> maxphyaddr)
9305 return false;
9306
9307 if (vmx->nested.virtual_apic_page) /* shouldn't happen */
9308 nested_release_page(vmx->nested.virtual_apic_page);
9309 vmx->nested.virtual_apic_page =
9310 nested_get_page(vcpu, vmcs12->virtual_apic_page_addr);
9311
9312 /*
9313 * Failing the vm entry is _not_ what the processor does
9314 * but it's basically the only possibility we have.
9315 * We could still enter the guest if CR8 load exits are
9316 * enabled, CR8 store exits are enabled, and virtualize APIC
9317 * access is disabled; in this case the processor would never
9318 * use the TPR shadow and we could simply clear the bit from
9319 * the execution control. But such a configuration is useless,
9320 * so let's keep the code simple.
9321 */
9322 if (!vmx->nested.virtual_apic_page)
9323 return false;
9324 }
9325
9326 if (nested_cpu_has_posted_intr(vmcs12)) {
9327 if (!IS_ALIGNED(vmcs12->posted_intr_desc_addr, 64) ||
9328 vmcs12->posted_intr_desc_addr >> maxphyaddr)
9329 return false;
9330
9331 if (vmx->nested.pi_desc_page) { /* shouldn't happen */
9332 kunmap(vmx->nested.pi_desc_page);
9333 nested_release_page(vmx->nested.pi_desc_page);
9334 }
9335 vmx->nested.pi_desc_page =
9336 nested_get_page(vcpu, vmcs12->posted_intr_desc_addr);
9337 if (!vmx->nested.pi_desc_page)
9338 return false;
9339
9340 vmx->nested.pi_desc =
9341 (struct pi_desc *)kmap(vmx->nested.pi_desc_page);
9342 if (!vmx->nested.pi_desc) {
9343 nested_release_page_clean(vmx->nested.pi_desc_page);
9344 return false;
9345 }
9346 vmx->nested.pi_desc =
9347 (struct pi_desc *)((void *)vmx->nested.pi_desc +
9348 (unsigned long)(vmcs12->posted_intr_desc_addr &
9349 (PAGE_SIZE - 1)));
9350 }
9351
9352 return true;
9353 }
9354
9355 static void vmx_start_preemption_timer(struct kvm_vcpu *vcpu)
9356 {
9357 u64 preemption_timeout = get_vmcs12(vcpu)->vmx_preemption_timer_value;
9358 struct vcpu_vmx *vmx = to_vmx(vcpu);
9359
9360 if (vcpu->arch.virtual_tsc_khz == 0)
9361 return;
9362
9363 /* Make sure short timeouts reliably trigger an immediate vmexit.
9364 * hrtimer_start does not guarantee this. */
9365 if (preemption_timeout <= 1) {
9366 vmx_preemption_timer_fn(&vmx->nested.preemption_timer);
9367 return;
9368 }
9369
9370 preemption_timeout <<= VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE;
9371 preemption_timeout *= 1000000;
9372 do_div(preemption_timeout, vcpu->arch.virtual_tsc_khz);
9373 hrtimer_start(&vmx->nested.preemption_timer,
9374 ns_to_ktime(preemption_timeout), HRTIMER_MODE_REL);
9375 }
9376
9377 static int nested_vmx_check_msr_bitmap_controls(struct kvm_vcpu *vcpu,
9378 struct vmcs12 *vmcs12)
9379 {
9380 int maxphyaddr;
9381 u64 addr;
9382
9383 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS))
9384 return 0;
9385
9386 if (vmcs12_read_any(vcpu, MSR_BITMAP, &addr)) {
9387 WARN_ON(1);
9388 return -EINVAL;
9389 }
9390 maxphyaddr = cpuid_maxphyaddr(vcpu);
9391
9392 if (!PAGE_ALIGNED(vmcs12->msr_bitmap) ||
9393 ((addr + PAGE_SIZE) >> maxphyaddr))
9394 return -EINVAL;
9395
9396 return 0;
9397 }
9398
9399 /*
9400 * Merge L0's and L1's MSR bitmap, return false to indicate that
9401 * we do not use the hardware.
9402 */
9403 static inline bool nested_vmx_merge_msr_bitmap(struct kvm_vcpu *vcpu,
9404 struct vmcs12 *vmcs12)
9405 {
9406 int msr;
9407 struct page *page;
9408 unsigned long *msr_bitmap_l1;
9409 unsigned long *msr_bitmap_l0 = to_vmx(vcpu)->nested.msr_bitmap;
9410
9411 /* This shortcut is ok because we support only x2APIC MSRs so far. */
9412 if (!nested_cpu_has_virt_x2apic_mode(vmcs12))
9413 return false;
9414
9415 page = nested_get_page(vcpu, vmcs12->msr_bitmap);
9416 if (!page) {
9417 WARN_ON(1);
9418 return false;
9419 }
9420 msr_bitmap_l1 = (unsigned long *)kmap(page);
9421 if (!msr_bitmap_l1) {
9422 nested_release_page_clean(page);
9423 WARN_ON(1);
9424 return false;
9425 }
9426
9427 memset(msr_bitmap_l0, 0xff, PAGE_SIZE);
9428
9429 if (nested_cpu_has_virt_x2apic_mode(vmcs12)) {
9430 if (nested_cpu_has_apic_reg_virt(vmcs12))
9431 for (msr = 0x800; msr <= 0x8ff; msr++)
9432 nested_vmx_disable_intercept_for_msr(
9433 msr_bitmap_l1, msr_bitmap_l0,
9434 msr, MSR_TYPE_R);
9435
9436 nested_vmx_disable_intercept_for_msr(
9437 msr_bitmap_l1, msr_bitmap_l0,
9438 APIC_BASE_MSR + (APIC_TASKPRI >> 4),
9439 MSR_TYPE_R | MSR_TYPE_W);
9440
9441 if (nested_cpu_has_vid(vmcs12)) {
9442 nested_vmx_disable_intercept_for_msr(
9443 msr_bitmap_l1, msr_bitmap_l0,
9444 APIC_BASE_MSR + (APIC_EOI >> 4),
9445 MSR_TYPE_W);
9446 nested_vmx_disable_intercept_for_msr(
9447 msr_bitmap_l1, msr_bitmap_l0,
9448 APIC_BASE_MSR + (APIC_SELF_IPI >> 4),
9449 MSR_TYPE_W);
9450 }
9451 }
9452 kunmap(page);
9453 nested_release_page_clean(page);
9454
9455 return true;
9456 }
9457
9458 static int nested_vmx_check_apicv_controls(struct kvm_vcpu *vcpu,
9459 struct vmcs12 *vmcs12)
9460 {
9461 if (!nested_cpu_has_virt_x2apic_mode(vmcs12) &&
9462 !nested_cpu_has_apic_reg_virt(vmcs12) &&
9463 !nested_cpu_has_vid(vmcs12) &&
9464 !nested_cpu_has_posted_intr(vmcs12))
9465 return 0;
9466
9467 /*
9468 * If virtualize x2apic mode is enabled,
9469 * virtualize apic access must be disabled.
9470 */
9471 if (nested_cpu_has_virt_x2apic_mode(vmcs12) &&
9472 nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES))
9473 return -EINVAL;
9474
9475 /*
9476 * If virtual interrupt delivery is enabled,
9477 * we must exit on external interrupts.
9478 */
9479 if (nested_cpu_has_vid(vmcs12) &&
9480 !nested_exit_on_intr(vcpu))
9481 return -EINVAL;
9482
9483 /*
9484 * bits 15:8 should be zero in posted_intr_nv,
9485 * the descriptor address has been already checked
9486 * in nested_get_vmcs12_pages.
9487 */
9488 if (nested_cpu_has_posted_intr(vmcs12) &&
9489 (!nested_cpu_has_vid(vmcs12) ||
9490 !nested_exit_intr_ack_set(vcpu) ||
9491 vmcs12->posted_intr_nv & 0xff00))
9492 return -EINVAL;
9493
9494 /* tpr shadow is needed by all apicv features. */
9495 if (!nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW))
9496 return -EINVAL;
9497
9498 return 0;
9499 }
9500
9501 static int nested_vmx_check_msr_switch(struct kvm_vcpu *vcpu,
9502 unsigned long count_field,
9503 unsigned long addr_field)
9504 {
9505 int maxphyaddr;
9506 u64 count, addr;
9507
9508 if (vmcs12_read_any(vcpu, count_field, &count) ||
9509 vmcs12_read_any(vcpu, addr_field, &addr)) {
9510 WARN_ON(1);
9511 return -EINVAL;
9512 }
9513 if (count == 0)
9514 return 0;
9515 maxphyaddr = cpuid_maxphyaddr(vcpu);
9516 if (!IS_ALIGNED(addr, 16) || addr >> maxphyaddr ||
9517 (addr + count * sizeof(struct vmx_msr_entry) - 1) >> maxphyaddr) {
9518 pr_debug_ratelimited(
9519 "nVMX: invalid MSR switch (0x%lx, %d, %llu, 0x%08llx)",
9520 addr_field, maxphyaddr, count, addr);
9521 return -EINVAL;
9522 }
9523 return 0;
9524 }
9525
9526 static int nested_vmx_check_msr_switch_controls(struct kvm_vcpu *vcpu,
9527 struct vmcs12 *vmcs12)
9528 {
9529 if (vmcs12->vm_exit_msr_load_count == 0 &&
9530 vmcs12->vm_exit_msr_store_count == 0 &&
9531 vmcs12->vm_entry_msr_load_count == 0)
9532 return 0; /* Fast path */
9533 if (nested_vmx_check_msr_switch(vcpu, VM_EXIT_MSR_LOAD_COUNT,
9534 VM_EXIT_MSR_LOAD_ADDR) ||
9535 nested_vmx_check_msr_switch(vcpu, VM_EXIT_MSR_STORE_COUNT,
9536 VM_EXIT_MSR_STORE_ADDR) ||
9537 nested_vmx_check_msr_switch(vcpu, VM_ENTRY_MSR_LOAD_COUNT,
9538 VM_ENTRY_MSR_LOAD_ADDR))
9539 return -EINVAL;
9540 return 0;
9541 }
9542
9543 static int nested_vmx_msr_check_common(struct kvm_vcpu *vcpu,
9544 struct vmx_msr_entry *e)
9545 {
9546 /* x2APIC MSR accesses are not allowed */
9547 if (vcpu->arch.apic_base & X2APIC_ENABLE && e->index >> 8 == 0x8)
9548 return -EINVAL;
9549 if (e->index == MSR_IA32_UCODE_WRITE || /* SDM Table 35-2 */
9550 e->index == MSR_IA32_UCODE_REV)
9551 return -EINVAL;
9552 if (e->reserved != 0)
9553 return -EINVAL;
9554 return 0;
9555 }
9556
9557 static int nested_vmx_load_msr_check(struct kvm_vcpu *vcpu,
9558 struct vmx_msr_entry *e)
9559 {
9560 if (e->index == MSR_FS_BASE ||
9561 e->index == MSR_GS_BASE ||
9562 e->index == MSR_IA32_SMM_MONITOR_CTL || /* SMM is not supported */
9563 nested_vmx_msr_check_common(vcpu, e))
9564 return -EINVAL;
9565 return 0;
9566 }
9567
9568 static int nested_vmx_store_msr_check(struct kvm_vcpu *vcpu,
9569 struct vmx_msr_entry *e)
9570 {
9571 if (e->index == MSR_IA32_SMBASE || /* SMM is not supported */
9572 nested_vmx_msr_check_common(vcpu, e))
9573 return -EINVAL;
9574 return 0;
9575 }
9576
9577 /*
9578 * Load guest's/host's msr at nested entry/exit.
9579 * return 0 for success, entry index for failure.
9580 */
9581 static u32 nested_vmx_load_msr(struct kvm_vcpu *vcpu, u64 gpa, u32 count)
9582 {
9583 u32 i;
9584 struct vmx_msr_entry e;
9585 struct msr_data msr;
9586
9587 msr.host_initiated = false;
9588 for (i = 0; i < count; i++) {
9589 if (kvm_vcpu_read_guest(vcpu, gpa + i * sizeof(e),
9590 &e, sizeof(e))) {
9591 pr_debug_ratelimited(
9592 "%s cannot read MSR entry (%u, 0x%08llx)\n",
9593 __func__, i, gpa + i * sizeof(e));
9594 goto fail;
9595 }
9596 if (nested_vmx_load_msr_check(vcpu, &e)) {
9597 pr_debug_ratelimited(
9598 "%s check failed (%u, 0x%x, 0x%x)\n",
9599 __func__, i, e.index, e.reserved);
9600 goto fail;
9601 }
9602 msr.index = e.index;
9603 msr.data = e.value;
9604 if (kvm_set_msr(vcpu, &msr)) {
9605 pr_debug_ratelimited(
9606 "%s cannot write MSR (%u, 0x%x, 0x%llx)\n",
9607 __func__, i, e.index, e.value);
9608 goto fail;
9609 }
9610 }
9611 return 0;
9612 fail:
9613 return i + 1;
9614 }
9615
9616 static int nested_vmx_store_msr(struct kvm_vcpu *vcpu, u64 gpa, u32 count)
9617 {
9618 u32 i;
9619 struct vmx_msr_entry e;
9620
9621 for (i = 0; i < count; i++) {
9622 struct msr_data msr_info;
9623 if (kvm_vcpu_read_guest(vcpu,
9624 gpa + i * sizeof(e),
9625 &e, 2 * sizeof(u32))) {
9626 pr_debug_ratelimited(
9627 "%s cannot read MSR entry (%u, 0x%08llx)\n",
9628 __func__, i, gpa + i * sizeof(e));
9629 return -EINVAL;
9630 }
9631 if (nested_vmx_store_msr_check(vcpu, &e)) {
9632 pr_debug_ratelimited(
9633 "%s check failed (%u, 0x%x, 0x%x)\n",
9634 __func__, i, e.index, e.reserved);
9635 return -EINVAL;
9636 }
9637 msr_info.host_initiated = false;
9638 msr_info.index = e.index;
9639 if (kvm_get_msr(vcpu, &msr_info)) {
9640 pr_debug_ratelimited(
9641 "%s cannot read MSR (%u, 0x%x)\n",
9642 __func__, i, e.index);
9643 return -EINVAL;
9644 }
9645 if (kvm_vcpu_write_guest(vcpu,
9646 gpa + i * sizeof(e) +
9647 offsetof(struct vmx_msr_entry, value),
9648 &msr_info.data, sizeof(msr_info.data))) {
9649 pr_debug_ratelimited(
9650 "%s cannot write MSR (%u, 0x%x, 0x%llx)\n",
9651 __func__, i, e.index, msr_info.data);
9652 return -EINVAL;
9653 }
9654 }
9655 return 0;
9656 }
9657
9658 /*
9659 * prepare_vmcs02 is called when the L1 guest hypervisor runs its nested
9660 * L2 guest. L1 has a vmcs for L2 (vmcs12), and this function "merges" it
9661 * with L0's requirements for its guest (a.k.a. vmcs01), so we can run the L2
9662 * guest in a way that will both be appropriate to L1's requests, and our
9663 * needs. In addition to modifying the active vmcs (which is vmcs02), this
9664 * function also has additional necessary side-effects, like setting various
9665 * vcpu->arch fields.
9666 */
9667 static void prepare_vmcs02(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
9668 {
9669 struct vcpu_vmx *vmx = to_vmx(vcpu);
9670 u32 exec_control;
9671
9672 vmcs_write16(GUEST_ES_SELECTOR, vmcs12->guest_es_selector);
9673 vmcs_write16(GUEST_CS_SELECTOR, vmcs12->guest_cs_selector);
9674 vmcs_write16(GUEST_SS_SELECTOR, vmcs12->guest_ss_selector);
9675 vmcs_write16(GUEST_DS_SELECTOR, vmcs12->guest_ds_selector);
9676 vmcs_write16(GUEST_FS_SELECTOR, vmcs12->guest_fs_selector);
9677 vmcs_write16(GUEST_GS_SELECTOR, vmcs12->guest_gs_selector);
9678 vmcs_write16(GUEST_LDTR_SELECTOR, vmcs12->guest_ldtr_selector);
9679 vmcs_write16(GUEST_TR_SELECTOR, vmcs12->guest_tr_selector);
9680 vmcs_write32(GUEST_ES_LIMIT, vmcs12->guest_es_limit);
9681 vmcs_write32(GUEST_CS_LIMIT, vmcs12->guest_cs_limit);
9682 vmcs_write32(GUEST_SS_LIMIT, vmcs12->guest_ss_limit);
9683 vmcs_write32(GUEST_DS_LIMIT, vmcs12->guest_ds_limit);
9684 vmcs_write32(GUEST_FS_LIMIT, vmcs12->guest_fs_limit);
9685 vmcs_write32(GUEST_GS_LIMIT, vmcs12->guest_gs_limit);
9686 vmcs_write32(GUEST_LDTR_LIMIT, vmcs12->guest_ldtr_limit);
9687 vmcs_write32(GUEST_TR_LIMIT, vmcs12->guest_tr_limit);
9688 vmcs_write32(GUEST_GDTR_LIMIT, vmcs12->guest_gdtr_limit);
9689 vmcs_write32(GUEST_IDTR_LIMIT, vmcs12->guest_idtr_limit);
9690 vmcs_write32(GUEST_ES_AR_BYTES, vmcs12->guest_es_ar_bytes);
9691 vmcs_write32(GUEST_CS_AR_BYTES, vmcs12->guest_cs_ar_bytes);
9692 vmcs_write32(GUEST_SS_AR_BYTES, vmcs12->guest_ss_ar_bytes);
9693 vmcs_write32(GUEST_DS_AR_BYTES, vmcs12->guest_ds_ar_bytes);
9694 vmcs_write32(GUEST_FS_AR_BYTES, vmcs12->guest_fs_ar_bytes);
9695 vmcs_write32(GUEST_GS_AR_BYTES, vmcs12->guest_gs_ar_bytes);
9696 vmcs_write32(GUEST_LDTR_AR_BYTES, vmcs12->guest_ldtr_ar_bytes);
9697 vmcs_write32(GUEST_TR_AR_BYTES, vmcs12->guest_tr_ar_bytes);
9698 vmcs_writel(GUEST_ES_BASE, vmcs12->guest_es_base);
9699 vmcs_writel(GUEST_CS_BASE, vmcs12->guest_cs_base);
9700 vmcs_writel(GUEST_SS_BASE, vmcs12->guest_ss_base);
9701 vmcs_writel(GUEST_DS_BASE, vmcs12->guest_ds_base);
9702 vmcs_writel(GUEST_FS_BASE, vmcs12->guest_fs_base);
9703 vmcs_writel(GUEST_GS_BASE, vmcs12->guest_gs_base);
9704 vmcs_writel(GUEST_LDTR_BASE, vmcs12->guest_ldtr_base);
9705 vmcs_writel(GUEST_TR_BASE, vmcs12->guest_tr_base);
9706 vmcs_writel(GUEST_GDTR_BASE, vmcs12->guest_gdtr_base);
9707 vmcs_writel(GUEST_IDTR_BASE, vmcs12->guest_idtr_base);
9708
9709 if (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS) {
9710 kvm_set_dr(vcpu, 7, vmcs12->guest_dr7);
9711 vmcs_write64(GUEST_IA32_DEBUGCTL, vmcs12->guest_ia32_debugctl);
9712 } else {
9713 kvm_set_dr(vcpu, 7, vcpu->arch.dr7);
9714 vmcs_write64(GUEST_IA32_DEBUGCTL, vmx->nested.vmcs01_debugctl);
9715 }
9716 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
9717 vmcs12->vm_entry_intr_info_field);
9718 vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE,
9719 vmcs12->vm_entry_exception_error_code);
9720 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
9721 vmcs12->vm_entry_instruction_len);
9722 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO,
9723 vmcs12->guest_interruptibility_info);
9724 vmcs_write32(GUEST_SYSENTER_CS, vmcs12->guest_sysenter_cs);
9725 vmx_set_rflags(vcpu, vmcs12->guest_rflags);
9726 vmcs_writel(GUEST_PENDING_DBG_EXCEPTIONS,
9727 vmcs12->guest_pending_dbg_exceptions);
9728 vmcs_writel(GUEST_SYSENTER_ESP, vmcs12->guest_sysenter_esp);
9729 vmcs_writel(GUEST_SYSENTER_EIP, vmcs12->guest_sysenter_eip);
9730
9731 if (nested_cpu_has_xsaves(vmcs12))
9732 vmcs_write64(XSS_EXIT_BITMAP, vmcs12->xss_exit_bitmap);
9733 vmcs_write64(VMCS_LINK_POINTER, -1ull);
9734
9735 exec_control = vmcs12->pin_based_vm_exec_control;
9736
9737 /* Preemption timer setting is only taken from vmcs01. */
9738 exec_control &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
9739 exec_control |= vmcs_config.pin_based_exec_ctrl;
9740 if (vmx->hv_deadline_tsc == -1)
9741 exec_control &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
9742
9743 /* Posted interrupts setting is only taken from vmcs12. */
9744 if (nested_cpu_has_posted_intr(vmcs12)) {
9745 /*
9746 * Note that we use L0's vector here and in
9747 * vmx_deliver_nested_posted_interrupt.
9748 */
9749 vmx->nested.posted_intr_nv = vmcs12->posted_intr_nv;
9750 vmx->nested.pi_pending = false;
9751 vmcs_write16(POSTED_INTR_NV, POSTED_INTR_VECTOR);
9752 vmcs_write64(POSTED_INTR_DESC_ADDR,
9753 page_to_phys(vmx->nested.pi_desc_page) +
9754 (unsigned long)(vmcs12->posted_intr_desc_addr &
9755 (PAGE_SIZE - 1)));
9756 } else
9757 exec_control &= ~PIN_BASED_POSTED_INTR;
9758
9759 vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, exec_control);
9760
9761 vmx->nested.preemption_timer_expired = false;
9762 if (nested_cpu_has_preemption_timer(vmcs12))
9763 vmx_start_preemption_timer(vcpu);
9764
9765 /*
9766 * Whether page-faults are trapped is determined by a combination of
9767 * 3 settings: PFEC_MASK, PFEC_MATCH and EXCEPTION_BITMAP.PF.
9768 * If enable_ept, L0 doesn't care about page faults and we should
9769 * set all of these to L1's desires. However, if !enable_ept, L0 does
9770 * care about (at least some) page faults, and because it is not easy
9771 * (if at all possible?) to merge L0 and L1's desires, we simply ask
9772 * to exit on each and every L2 page fault. This is done by setting
9773 * MASK=MATCH=0 and (see below) EB.PF=1.
9774 * Note that below we don't need special code to set EB.PF beyond the
9775 * "or"ing of the EB of vmcs01 and vmcs12, because when enable_ept,
9776 * vmcs01's EB.PF is 0 so the "or" will take vmcs12's value, and when
9777 * !enable_ept, EB.PF is 1, so the "or" will always be 1.
9778 *
9779 * A problem with this approach (when !enable_ept) is that L1 may be
9780 * injected with more page faults than it asked for. This could have
9781 * caused problems, but in practice existing hypervisors don't care.
9782 * To fix this, we will need to emulate the PFEC checking (on the L1
9783 * page tables), using walk_addr(), when injecting PFs to L1.
9784 */
9785 vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK,
9786 enable_ept ? vmcs12->page_fault_error_code_mask : 0);
9787 vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH,
9788 enable_ept ? vmcs12->page_fault_error_code_match : 0);
9789
9790 if (cpu_has_secondary_exec_ctrls()) {
9791 exec_control = vmx_secondary_exec_control(vmx);
9792
9793 /* Take the following fields only from vmcs12 */
9794 exec_control &= ~(SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
9795 SECONDARY_EXEC_RDTSCP |
9796 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY |
9797 SECONDARY_EXEC_APIC_REGISTER_VIRT);
9798 if (nested_cpu_has(vmcs12,
9799 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS))
9800 exec_control |= vmcs12->secondary_vm_exec_control;
9801
9802 if (exec_control & SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES) {
9803 /*
9804 * If translation failed, no matter: This feature asks
9805 * to exit when accessing the given address, and if it
9806 * can never be accessed, this feature won't do
9807 * anything anyway.
9808 */
9809 if (!vmx->nested.apic_access_page)
9810 exec_control &=
9811 ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
9812 else
9813 vmcs_write64(APIC_ACCESS_ADDR,
9814 page_to_phys(vmx->nested.apic_access_page));
9815 } else if (!(nested_cpu_has_virt_x2apic_mode(vmcs12)) &&
9816 cpu_need_virtualize_apic_accesses(&vmx->vcpu)) {
9817 exec_control |=
9818 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
9819 kvm_vcpu_reload_apic_access_page(vcpu);
9820 }
9821
9822 if (exec_control & SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY) {
9823 vmcs_write64(EOI_EXIT_BITMAP0,
9824 vmcs12->eoi_exit_bitmap0);
9825 vmcs_write64(EOI_EXIT_BITMAP1,
9826 vmcs12->eoi_exit_bitmap1);
9827 vmcs_write64(EOI_EXIT_BITMAP2,
9828 vmcs12->eoi_exit_bitmap2);
9829 vmcs_write64(EOI_EXIT_BITMAP3,
9830 vmcs12->eoi_exit_bitmap3);
9831 vmcs_write16(GUEST_INTR_STATUS,
9832 vmcs12->guest_intr_status);
9833 }
9834
9835 vmcs_write32(SECONDARY_VM_EXEC_CONTROL, exec_control);
9836 }
9837
9838
9839 /*
9840 * Set host-state according to L0's settings (vmcs12 is irrelevant here)
9841 * Some constant fields are set here by vmx_set_constant_host_state().
9842 * Other fields are different per CPU, and will be set later when
9843 * vmx_vcpu_load() is called, and when vmx_save_host_state() is called.
9844 */
9845 vmx_set_constant_host_state(vmx);
9846
9847 /*
9848 * Set the MSR load/store lists to match L0's settings.
9849 */
9850 vmcs_write32(VM_EXIT_MSR_STORE_COUNT, 0);
9851 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, vmx->msr_autoload.nr);
9852 vmcs_write64(VM_EXIT_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.host));
9853 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, vmx->msr_autoload.nr);
9854 vmcs_write64(VM_ENTRY_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.guest));
9855
9856 /*
9857 * HOST_RSP is normally set correctly in vmx_vcpu_run() just before
9858 * entry, but only if the current (host) sp changed from the value
9859 * we wrote last (vmx->host_rsp). This cache is no longer relevant
9860 * if we switch vmcs, and rather than hold a separate cache per vmcs,
9861 * here we just force the write to happen on entry.
9862 */
9863 vmx->host_rsp = 0;
9864
9865 exec_control = vmx_exec_control(vmx); /* L0's desires */
9866 exec_control &= ~CPU_BASED_VIRTUAL_INTR_PENDING;
9867 exec_control &= ~CPU_BASED_VIRTUAL_NMI_PENDING;
9868 exec_control &= ~CPU_BASED_TPR_SHADOW;
9869 exec_control |= vmcs12->cpu_based_vm_exec_control;
9870
9871 if (exec_control & CPU_BASED_TPR_SHADOW) {
9872 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR,
9873 page_to_phys(vmx->nested.virtual_apic_page));
9874 vmcs_write32(TPR_THRESHOLD, vmcs12->tpr_threshold);
9875 }
9876
9877 if (cpu_has_vmx_msr_bitmap() &&
9878 exec_control & CPU_BASED_USE_MSR_BITMAPS &&
9879 nested_vmx_merge_msr_bitmap(vcpu, vmcs12))
9880 ; /* MSR_BITMAP will be set by following vmx_set_efer. */
9881 else
9882 exec_control &= ~CPU_BASED_USE_MSR_BITMAPS;
9883
9884 /*
9885 * Merging of IO bitmap not currently supported.
9886 * Rather, exit every time.
9887 */
9888 exec_control &= ~CPU_BASED_USE_IO_BITMAPS;
9889 exec_control |= CPU_BASED_UNCOND_IO_EXITING;
9890
9891 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, exec_control);
9892
9893 /* EXCEPTION_BITMAP and CR0_GUEST_HOST_MASK should basically be the
9894 * bitwise-or of what L1 wants to trap for L2, and what we want to
9895 * trap. Note that CR0.TS also needs updating - we do this later.
9896 */
9897 update_exception_bitmap(vcpu);
9898 vcpu->arch.cr0_guest_owned_bits &= ~vmcs12->cr0_guest_host_mask;
9899 vmcs_writel(CR0_GUEST_HOST_MASK, ~vcpu->arch.cr0_guest_owned_bits);
9900
9901 /* L2->L1 exit controls are emulated - the hardware exit is to L0 so
9902 * we should use its exit controls. Note that VM_EXIT_LOAD_IA32_EFER
9903 * bits are further modified by vmx_set_efer() below.
9904 */
9905 vmcs_write32(VM_EXIT_CONTROLS, vmcs_config.vmexit_ctrl);
9906
9907 /* vmcs12's VM_ENTRY_LOAD_IA32_EFER and VM_ENTRY_IA32E_MODE are
9908 * emulated by vmx_set_efer(), below.
9909 */
9910 vm_entry_controls_init(vmx,
9911 (vmcs12->vm_entry_controls & ~VM_ENTRY_LOAD_IA32_EFER &
9912 ~VM_ENTRY_IA32E_MODE) |
9913 (vmcs_config.vmentry_ctrl & ~VM_ENTRY_IA32E_MODE));
9914
9915 if (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_PAT) {
9916 vmcs_write64(GUEST_IA32_PAT, vmcs12->guest_ia32_pat);
9917 vcpu->arch.pat = vmcs12->guest_ia32_pat;
9918 } else if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT)
9919 vmcs_write64(GUEST_IA32_PAT, vmx->vcpu.arch.pat);
9920
9921
9922 set_cr4_guest_host_mask(vmx);
9923
9924 if (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_BNDCFGS)
9925 vmcs_write64(GUEST_BNDCFGS, vmcs12->guest_bndcfgs);
9926
9927 if (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING)
9928 vmcs_write64(TSC_OFFSET,
9929 vcpu->arch.tsc_offset + vmcs12->tsc_offset);
9930 else
9931 vmcs_write64(TSC_OFFSET, vcpu->arch.tsc_offset);
9932 if (kvm_has_tsc_control)
9933 decache_tsc_multiplier(vmx);
9934
9935 if (enable_vpid) {
9936 /*
9937 * There is no direct mapping between vpid02 and vpid12, the
9938 * vpid02 is per-vCPU for L0 and reused while the value of
9939 * vpid12 is changed w/ one invvpid during nested vmentry.
9940 * The vpid12 is allocated by L1 for L2, so it will not
9941 * influence global bitmap(for vpid01 and vpid02 allocation)
9942 * even if spawn a lot of nested vCPUs.
9943 */
9944 if (nested_cpu_has_vpid(vmcs12) && vmx->nested.vpid02) {
9945 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->nested.vpid02);
9946 if (vmcs12->virtual_processor_id != vmx->nested.last_vpid) {
9947 vmx->nested.last_vpid = vmcs12->virtual_processor_id;
9948 __vmx_flush_tlb(vcpu, to_vmx(vcpu)->nested.vpid02);
9949 }
9950 } else {
9951 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid);
9952 vmx_flush_tlb(vcpu);
9953 }
9954
9955 }
9956
9957 if (nested_cpu_has_ept(vmcs12)) {
9958 kvm_mmu_unload(vcpu);
9959 nested_ept_init_mmu_context(vcpu);
9960 }
9961
9962 if (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_EFER)
9963 vcpu->arch.efer = vmcs12->guest_ia32_efer;
9964 else if (vmcs12->vm_entry_controls & VM_ENTRY_IA32E_MODE)
9965 vcpu->arch.efer |= (EFER_LMA | EFER_LME);
9966 else
9967 vcpu->arch.efer &= ~(EFER_LMA | EFER_LME);
9968 /* Note: modifies VM_ENTRY/EXIT_CONTROLS and GUEST/HOST_IA32_EFER */
9969 vmx_set_efer(vcpu, vcpu->arch.efer);
9970
9971 /*
9972 * This sets GUEST_CR0 to vmcs12->guest_cr0, with possibly a modified
9973 * TS bit (for lazy fpu) and bits which we consider mandatory enabled.
9974 * The CR0_READ_SHADOW is what L2 should have expected to read given
9975 * the specifications by L1; It's not enough to take
9976 * vmcs12->cr0_read_shadow because on our cr0_guest_host_mask we we
9977 * have more bits than L1 expected.
9978 */
9979 vmx_set_cr0(vcpu, vmcs12->guest_cr0);
9980 vmcs_writel(CR0_READ_SHADOW, nested_read_cr0(vmcs12));
9981
9982 vmx_set_cr4(vcpu, vmcs12->guest_cr4);
9983 vmcs_writel(CR4_READ_SHADOW, nested_read_cr4(vmcs12));
9984
9985 /* shadow page tables on either EPT or shadow page tables */
9986 kvm_set_cr3(vcpu, vmcs12->guest_cr3);
9987 kvm_mmu_reset_context(vcpu);
9988
9989 if (!enable_ept)
9990 vcpu->arch.walk_mmu->inject_page_fault = vmx_inject_page_fault_nested;
9991
9992 /*
9993 * L1 may access the L2's PDPTR, so save them to construct vmcs12
9994 */
9995 if (enable_ept) {
9996 vmcs_write64(GUEST_PDPTR0, vmcs12->guest_pdptr0);
9997 vmcs_write64(GUEST_PDPTR1, vmcs12->guest_pdptr1);
9998 vmcs_write64(GUEST_PDPTR2, vmcs12->guest_pdptr2);
9999 vmcs_write64(GUEST_PDPTR3, vmcs12->guest_pdptr3);
10000 }
10001
10002 kvm_register_write(vcpu, VCPU_REGS_RSP, vmcs12->guest_rsp);
10003 kvm_register_write(vcpu, VCPU_REGS_RIP, vmcs12->guest_rip);
10004 }
10005
10006 /*
10007 * nested_vmx_run() handles a nested entry, i.e., a VMLAUNCH or VMRESUME on L1
10008 * for running an L2 nested guest.
10009 */
10010 static int nested_vmx_run(struct kvm_vcpu *vcpu, bool launch)
10011 {
10012 struct vmcs12 *vmcs12;
10013 struct vcpu_vmx *vmx = to_vmx(vcpu);
10014 int cpu;
10015 struct loaded_vmcs *vmcs02;
10016 bool ia32e;
10017 u32 msr_entry_idx;
10018
10019 if (!nested_vmx_check_permission(vcpu))
10020 return 1;
10021
10022 if (!nested_vmx_check_vmcs12(vcpu))
10023 goto out;
10024
10025 vmcs12 = get_vmcs12(vcpu);
10026
10027 if (enable_shadow_vmcs)
10028 copy_shadow_to_vmcs12(vmx);
10029
10030 /*
10031 * The nested entry process starts with enforcing various prerequisites
10032 * on vmcs12 as required by the Intel SDM, and act appropriately when
10033 * they fail: As the SDM explains, some conditions should cause the
10034 * instruction to fail, while others will cause the instruction to seem
10035 * to succeed, but return an EXIT_REASON_INVALID_STATE.
10036 * To speed up the normal (success) code path, we should avoid checking
10037 * for misconfigurations which will anyway be caught by the processor
10038 * when using the merged vmcs02.
10039 */
10040 if (vmcs12->launch_state == launch) {
10041 nested_vmx_failValid(vcpu,
10042 launch ? VMXERR_VMLAUNCH_NONCLEAR_VMCS
10043 : VMXERR_VMRESUME_NONLAUNCHED_VMCS);
10044 goto out;
10045 }
10046
10047 if (vmcs12->guest_activity_state != GUEST_ACTIVITY_ACTIVE &&
10048 vmcs12->guest_activity_state != GUEST_ACTIVITY_HLT) {
10049 nested_vmx_failValid(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD);
10050 goto out;
10051 }
10052
10053 if (!nested_get_vmcs12_pages(vcpu, vmcs12)) {
10054 nested_vmx_failValid(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD);
10055 goto out;
10056 }
10057
10058 if (nested_vmx_check_msr_bitmap_controls(vcpu, vmcs12)) {
10059 nested_vmx_failValid(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD);
10060 goto out;
10061 }
10062
10063 if (nested_vmx_check_apicv_controls(vcpu, vmcs12)) {
10064 nested_vmx_failValid(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD);
10065 goto out;
10066 }
10067
10068 if (nested_vmx_check_msr_switch_controls(vcpu, vmcs12)) {
10069 nested_vmx_failValid(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD);
10070 goto out;
10071 }
10072
10073 if (!vmx_control_verify(vmcs12->cpu_based_vm_exec_control,
10074 vmx->nested.nested_vmx_true_procbased_ctls_low,
10075 vmx->nested.nested_vmx_procbased_ctls_high) ||
10076 !vmx_control_verify(vmcs12->secondary_vm_exec_control,
10077 vmx->nested.nested_vmx_secondary_ctls_low,
10078 vmx->nested.nested_vmx_secondary_ctls_high) ||
10079 !vmx_control_verify(vmcs12->pin_based_vm_exec_control,
10080 vmx->nested.nested_vmx_pinbased_ctls_low,
10081 vmx->nested.nested_vmx_pinbased_ctls_high) ||
10082 !vmx_control_verify(vmcs12->vm_exit_controls,
10083 vmx->nested.nested_vmx_true_exit_ctls_low,
10084 vmx->nested.nested_vmx_exit_ctls_high) ||
10085 !vmx_control_verify(vmcs12->vm_entry_controls,
10086 vmx->nested.nested_vmx_true_entry_ctls_low,
10087 vmx->nested.nested_vmx_entry_ctls_high))
10088 {
10089 nested_vmx_failValid(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD);
10090 goto out;
10091 }
10092
10093 if (((vmcs12->host_cr0 & VMXON_CR0_ALWAYSON) != VMXON_CR0_ALWAYSON) ||
10094 ((vmcs12->host_cr4 & VMXON_CR4_ALWAYSON) != VMXON_CR4_ALWAYSON)) {
10095 nested_vmx_failValid(vcpu,
10096 VMXERR_ENTRY_INVALID_HOST_STATE_FIELD);
10097 goto out;
10098 }
10099
10100 if (!nested_cr0_valid(vcpu, vmcs12->guest_cr0) ||
10101 ((vmcs12->guest_cr4 & VMXON_CR4_ALWAYSON) != VMXON_CR4_ALWAYSON)) {
10102 nested_vmx_entry_failure(vcpu, vmcs12,
10103 EXIT_REASON_INVALID_STATE, ENTRY_FAIL_DEFAULT);
10104 goto out;
10105 }
10106 if (vmcs12->vmcs_link_pointer != -1ull) {
10107 nested_vmx_entry_failure(vcpu, vmcs12,
10108 EXIT_REASON_INVALID_STATE, ENTRY_FAIL_VMCS_LINK_PTR);
10109 goto out;
10110 }
10111
10112 /*
10113 * If the load IA32_EFER VM-entry control is 1, the following checks
10114 * are performed on the field for the IA32_EFER MSR:
10115 * - Bits reserved in the IA32_EFER MSR must be 0.
10116 * - Bit 10 (corresponding to IA32_EFER.LMA) must equal the value of
10117 * the IA-32e mode guest VM-exit control. It must also be identical
10118 * to bit 8 (LME) if bit 31 in the CR0 field (corresponding to
10119 * CR0.PG) is 1.
10120 */
10121 if (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_EFER) {
10122 ia32e = (vmcs12->vm_entry_controls & VM_ENTRY_IA32E_MODE) != 0;
10123 if (!kvm_valid_efer(vcpu, vmcs12->guest_ia32_efer) ||
10124 ia32e != !!(vmcs12->guest_ia32_efer & EFER_LMA) ||
10125 ((vmcs12->guest_cr0 & X86_CR0_PG) &&
10126 ia32e != !!(vmcs12->guest_ia32_efer & EFER_LME))) {
10127 nested_vmx_entry_failure(vcpu, vmcs12,
10128 EXIT_REASON_INVALID_STATE, ENTRY_FAIL_DEFAULT);
10129 goto out;
10130 }
10131 }
10132
10133 /*
10134 * If the load IA32_EFER VM-exit control is 1, bits reserved in the
10135 * IA32_EFER MSR must be 0 in the field for that register. In addition,
10136 * the values of the LMA and LME bits in the field must each be that of
10137 * the host address-space size VM-exit control.
10138 */
10139 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_EFER) {
10140 ia32e = (vmcs12->vm_exit_controls &
10141 VM_EXIT_HOST_ADDR_SPACE_SIZE) != 0;
10142 if (!kvm_valid_efer(vcpu, vmcs12->host_ia32_efer) ||
10143 ia32e != !!(vmcs12->host_ia32_efer & EFER_LMA) ||
10144 ia32e != !!(vmcs12->host_ia32_efer & EFER_LME)) {
10145 nested_vmx_entry_failure(vcpu, vmcs12,
10146 EXIT_REASON_INVALID_STATE, ENTRY_FAIL_DEFAULT);
10147 goto out;
10148 }
10149 }
10150
10151 /*
10152 * We're finally done with prerequisite checking, and can start with
10153 * the nested entry.
10154 */
10155
10156 vmcs02 = nested_get_current_vmcs02(vmx);
10157 if (!vmcs02)
10158 return -ENOMEM;
10159
10160 /*
10161 * After this point, the trap flag no longer triggers a singlestep trap
10162 * on the vm entry instructions. Don't call
10163 * kvm_skip_emulated_instruction.
10164 */
10165 skip_emulated_instruction(vcpu);
10166 enter_guest_mode(vcpu);
10167
10168 if (!(vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS))
10169 vmx->nested.vmcs01_debugctl = vmcs_read64(GUEST_IA32_DEBUGCTL);
10170
10171 cpu = get_cpu();
10172 vmx->loaded_vmcs = vmcs02;
10173 vmx_vcpu_put(vcpu);
10174 vmx_vcpu_load(vcpu, cpu);
10175 vcpu->cpu = cpu;
10176 put_cpu();
10177
10178 vmx_segment_cache_clear(vmx);
10179
10180 prepare_vmcs02(vcpu, vmcs12);
10181
10182 msr_entry_idx = nested_vmx_load_msr(vcpu,
10183 vmcs12->vm_entry_msr_load_addr,
10184 vmcs12->vm_entry_msr_load_count);
10185 if (msr_entry_idx) {
10186 leave_guest_mode(vcpu);
10187 vmx_load_vmcs01(vcpu);
10188 nested_vmx_entry_failure(vcpu, vmcs12,
10189 EXIT_REASON_MSR_LOAD_FAIL, msr_entry_idx);
10190 return 1;
10191 }
10192
10193 vmcs12->launch_state = 1;
10194
10195 if (vmcs12->guest_activity_state == GUEST_ACTIVITY_HLT)
10196 return kvm_vcpu_halt(vcpu);
10197
10198 vmx->nested.nested_run_pending = 1;
10199
10200 /*
10201 * Note no nested_vmx_succeed or nested_vmx_fail here. At this point
10202 * we are no longer running L1, and VMLAUNCH/VMRESUME has not yet
10203 * returned as far as L1 is concerned. It will only return (and set
10204 * the success flag) when L2 exits (see nested_vmx_vmexit()).
10205 */
10206 return 1;
10207
10208 out:
10209 return kvm_skip_emulated_instruction(vcpu);
10210 }
10211
10212 /*
10213 * On a nested exit from L2 to L1, vmcs12.guest_cr0 might not be up-to-date
10214 * because L2 may have changed some cr0 bits directly (CRO_GUEST_HOST_MASK).
10215 * This function returns the new value we should put in vmcs12.guest_cr0.
10216 * It's not enough to just return the vmcs02 GUEST_CR0. Rather,
10217 * 1. Bits that neither L0 nor L1 trapped, were set directly by L2 and are now
10218 * available in vmcs02 GUEST_CR0. (Note: It's enough to check that L0
10219 * didn't trap the bit, because if L1 did, so would L0).
10220 * 2. Bits that L1 asked to trap (and therefore L0 also did) could not have
10221 * been modified by L2, and L1 knows it. So just leave the old value of
10222 * the bit from vmcs12.guest_cr0. Note that the bit from vmcs02 GUEST_CR0
10223 * isn't relevant, because if L0 traps this bit it can set it to anything.
10224 * 3. Bits that L1 didn't trap, but L0 did. L1 believes the guest could have
10225 * changed these bits, and therefore they need to be updated, but L0
10226 * didn't necessarily allow them to be changed in GUEST_CR0 - and rather
10227 * put them in vmcs02 CR0_READ_SHADOW. So take these bits from there.
10228 */
10229 static inline unsigned long
10230 vmcs12_guest_cr0(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
10231 {
10232 return
10233 /*1*/ (vmcs_readl(GUEST_CR0) & vcpu->arch.cr0_guest_owned_bits) |
10234 /*2*/ (vmcs12->guest_cr0 & vmcs12->cr0_guest_host_mask) |
10235 /*3*/ (vmcs_readl(CR0_READ_SHADOW) & ~(vmcs12->cr0_guest_host_mask |
10236 vcpu->arch.cr0_guest_owned_bits));
10237 }
10238
10239 static inline unsigned long
10240 vmcs12_guest_cr4(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
10241 {
10242 return
10243 /*1*/ (vmcs_readl(GUEST_CR4) & vcpu->arch.cr4_guest_owned_bits) |
10244 /*2*/ (vmcs12->guest_cr4 & vmcs12->cr4_guest_host_mask) |
10245 /*3*/ (vmcs_readl(CR4_READ_SHADOW) & ~(vmcs12->cr4_guest_host_mask |
10246 vcpu->arch.cr4_guest_owned_bits));
10247 }
10248
10249 static void vmcs12_save_pending_event(struct kvm_vcpu *vcpu,
10250 struct vmcs12 *vmcs12)
10251 {
10252 u32 idt_vectoring;
10253 unsigned int nr;
10254
10255 if (vcpu->arch.exception.pending && vcpu->arch.exception.reinject) {
10256 nr = vcpu->arch.exception.nr;
10257 idt_vectoring = nr | VECTORING_INFO_VALID_MASK;
10258
10259 if (kvm_exception_is_soft(nr)) {
10260 vmcs12->vm_exit_instruction_len =
10261 vcpu->arch.event_exit_inst_len;
10262 idt_vectoring |= INTR_TYPE_SOFT_EXCEPTION;
10263 } else
10264 idt_vectoring |= INTR_TYPE_HARD_EXCEPTION;
10265
10266 if (vcpu->arch.exception.has_error_code) {
10267 idt_vectoring |= VECTORING_INFO_DELIVER_CODE_MASK;
10268 vmcs12->idt_vectoring_error_code =
10269 vcpu->arch.exception.error_code;
10270 }
10271
10272 vmcs12->idt_vectoring_info_field = idt_vectoring;
10273 } else if (vcpu->arch.nmi_injected) {
10274 vmcs12->idt_vectoring_info_field =
10275 INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK | NMI_VECTOR;
10276 } else if (vcpu->arch.interrupt.pending) {
10277 nr = vcpu->arch.interrupt.nr;
10278 idt_vectoring = nr | VECTORING_INFO_VALID_MASK;
10279
10280 if (vcpu->arch.interrupt.soft) {
10281 idt_vectoring |= INTR_TYPE_SOFT_INTR;
10282 vmcs12->vm_entry_instruction_len =
10283 vcpu->arch.event_exit_inst_len;
10284 } else
10285 idt_vectoring |= INTR_TYPE_EXT_INTR;
10286
10287 vmcs12->idt_vectoring_info_field = idt_vectoring;
10288 }
10289 }
10290
10291 static int vmx_check_nested_events(struct kvm_vcpu *vcpu, bool external_intr)
10292 {
10293 struct vcpu_vmx *vmx = to_vmx(vcpu);
10294
10295 if (nested_cpu_has_preemption_timer(get_vmcs12(vcpu)) &&
10296 vmx->nested.preemption_timer_expired) {
10297 if (vmx->nested.nested_run_pending)
10298 return -EBUSY;
10299 nested_vmx_vmexit(vcpu, EXIT_REASON_PREEMPTION_TIMER, 0, 0);
10300 return 0;
10301 }
10302
10303 if (vcpu->arch.nmi_pending && nested_exit_on_nmi(vcpu)) {
10304 if (vmx->nested.nested_run_pending ||
10305 vcpu->arch.interrupt.pending)
10306 return -EBUSY;
10307 nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI,
10308 NMI_VECTOR | INTR_TYPE_NMI_INTR |
10309 INTR_INFO_VALID_MASK, 0);
10310 /*
10311 * The NMI-triggered VM exit counts as injection:
10312 * clear this one and block further NMIs.
10313 */
10314 vcpu->arch.nmi_pending = 0;
10315 vmx_set_nmi_mask(vcpu, true);
10316 return 0;
10317 }
10318
10319 if ((kvm_cpu_has_interrupt(vcpu) || external_intr) &&
10320 nested_exit_on_intr(vcpu)) {
10321 if (vmx->nested.nested_run_pending)
10322 return -EBUSY;
10323 nested_vmx_vmexit(vcpu, EXIT_REASON_EXTERNAL_INTERRUPT, 0, 0);
10324 return 0;
10325 }
10326
10327 return vmx_complete_nested_posted_interrupt(vcpu);
10328 }
10329
10330 static u32 vmx_get_preemption_timer_value(struct kvm_vcpu *vcpu)
10331 {
10332 ktime_t remaining =
10333 hrtimer_get_remaining(&to_vmx(vcpu)->nested.preemption_timer);
10334 u64 value;
10335
10336 if (ktime_to_ns(remaining) <= 0)
10337 return 0;
10338
10339 value = ktime_to_ns(remaining) * vcpu->arch.virtual_tsc_khz;
10340 do_div(value, 1000000);
10341 return value >> VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE;
10342 }
10343
10344 /*
10345 * prepare_vmcs12 is part of what we need to do when the nested L2 guest exits
10346 * and we want to prepare to run its L1 parent. L1 keeps a vmcs for L2 (vmcs12),
10347 * and this function updates it to reflect the changes to the guest state while
10348 * L2 was running (and perhaps made some exits which were handled directly by L0
10349 * without going back to L1), and to reflect the exit reason.
10350 * Note that we do not have to copy here all VMCS fields, just those that
10351 * could have changed by the L2 guest or the exit - i.e., the guest-state and
10352 * exit-information fields only. Other fields are modified by L1 with VMWRITE,
10353 * which already writes to vmcs12 directly.
10354 */
10355 static void prepare_vmcs12(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12,
10356 u32 exit_reason, u32 exit_intr_info,
10357 unsigned long exit_qualification)
10358 {
10359 /* update guest state fields: */
10360 vmcs12->guest_cr0 = vmcs12_guest_cr0(vcpu, vmcs12);
10361 vmcs12->guest_cr4 = vmcs12_guest_cr4(vcpu, vmcs12);
10362
10363 vmcs12->guest_rsp = kvm_register_read(vcpu, VCPU_REGS_RSP);
10364 vmcs12->guest_rip = kvm_register_read(vcpu, VCPU_REGS_RIP);
10365 vmcs12->guest_rflags = vmcs_readl(GUEST_RFLAGS);
10366
10367 vmcs12->guest_es_selector = vmcs_read16(GUEST_ES_SELECTOR);
10368 vmcs12->guest_cs_selector = vmcs_read16(GUEST_CS_SELECTOR);
10369 vmcs12->guest_ss_selector = vmcs_read16(GUEST_SS_SELECTOR);
10370 vmcs12->guest_ds_selector = vmcs_read16(GUEST_DS_SELECTOR);
10371 vmcs12->guest_fs_selector = vmcs_read16(GUEST_FS_SELECTOR);
10372 vmcs12->guest_gs_selector = vmcs_read16(GUEST_GS_SELECTOR);
10373 vmcs12->guest_ldtr_selector = vmcs_read16(GUEST_LDTR_SELECTOR);
10374 vmcs12->guest_tr_selector = vmcs_read16(GUEST_TR_SELECTOR);
10375 vmcs12->guest_es_limit = vmcs_read32(GUEST_ES_LIMIT);
10376 vmcs12->guest_cs_limit = vmcs_read32(GUEST_CS_LIMIT);
10377 vmcs12->guest_ss_limit = vmcs_read32(GUEST_SS_LIMIT);
10378 vmcs12->guest_ds_limit = vmcs_read32(GUEST_DS_LIMIT);
10379 vmcs12->guest_fs_limit = vmcs_read32(GUEST_FS_LIMIT);
10380 vmcs12->guest_gs_limit = vmcs_read32(GUEST_GS_LIMIT);
10381 vmcs12->guest_ldtr_limit = vmcs_read32(GUEST_LDTR_LIMIT);
10382 vmcs12->guest_tr_limit = vmcs_read32(GUEST_TR_LIMIT);
10383 vmcs12->guest_gdtr_limit = vmcs_read32(GUEST_GDTR_LIMIT);
10384 vmcs12->guest_idtr_limit = vmcs_read32(GUEST_IDTR_LIMIT);
10385 vmcs12->guest_es_ar_bytes = vmcs_read32(GUEST_ES_AR_BYTES);
10386 vmcs12->guest_cs_ar_bytes = vmcs_read32(GUEST_CS_AR_BYTES);
10387 vmcs12->guest_ss_ar_bytes = vmcs_read32(GUEST_SS_AR_BYTES);
10388 vmcs12->guest_ds_ar_bytes = vmcs_read32(GUEST_DS_AR_BYTES);
10389 vmcs12->guest_fs_ar_bytes = vmcs_read32(GUEST_FS_AR_BYTES);
10390 vmcs12->guest_gs_ar_bytes = vmcs_read32(GUEST_GS_AR_BYTES);
10391 vmcs12->guest_ldtr_ar_bytes = vmcs_read32(GUEST_LDTR_AR_BYTES);
10392 vmcs12->guest_tr_ar_bytes = vmcs_read32(GUEST_TR_AR_BYTES);
10393 vmcs12->guest_es_base = vmcs_readl(GUEST_ES_BASE);
10394 vmcs12->guest_cs_base = vmcs_readl(GUEST_CS_BASE);
10395 vmcs12->guest_ss_base = vmcs_readl(GUEST_SS_BASE);
10396 vmcs12->guest_ds_base = vmcs_readl(GUEST_DS_BASE);
10397 vmcs12->guest_fs_base = vmcs_readl(GUEST_FS_BASE);
10398 vmcs12->guest_gs_base = vmcs_readl(GUEST_GS_BASE);
10399 vmcs12->guest_ldtr_base = vmcs_readl(GUEST_LDTR_BASE);
10400 vmcs12->guest_tr_base = vmcs_readl(GUEST_TR_BASE);
10401 vmcs12->guest_gdtr_base = vmcs_readl(GUEST_GDTR_BASE);
10402 vmcs12->guest_idtr_base = vmcs_readl(GUEST_IDTR_BASE);
10403
10404 vmcs12->guest_interruptibility_info =
10405 vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
10406 vmcs12->guest_pending_dbg_exceptions =
10407 vmcs_readl(GUEST_PENDING_DBG_EXCEPTIONS);
10408 if (vcpu->arch.mp_state == KVM_MP_STATE_HALTED)
10409 vmcs12->guest_activity_state = GUEST_ACTIVITY_HLT;
10410 else
10411 vmcs12->guest_activity_state = GUEST_ACTIVITY_ACTIVE;
10412
10413 if (nested_cpu_has_preemption_timer(vmcs12)) {
10414 if (vmcs12->vm_exit_controls &
10415 VM_EXIT_SAVE_VMX_PREEMPTION_TIMER)
10416 vmcs12->vmx_preemption_timer_value =
10417 vmx_get_preemption_timer_value(vcpu);
10418 hrtimer_cancel(&to_vmx(vcpu)->nested.preemption_timer);
10419 }
10420
10421 /*
10422 * In some cases (usually, nested EPT), L2 is allowed to change its
10423 * own CR3 without exiting. If it has changed it, we must keep it.
10424 * Of course, if L0 is using shadow page tables, GUEST_CR3 was defined
10425 * by L0, not L1 or L2, so we mustn't unconditionally copy it to vmcs12.
10426 *
10427 * Additionally, restore L2's PDPTR to vmcs12.
10428 */
10429 if (enable_ept) {
10430 vmcs12->guest_cr3 = vmcs_readl(GUEST_CR3);
10431 vmcs12->guest_pdptr0 = vmcs_read64(GUEST_PDPTR0);
10432 vmcs12->guest_pdptr1 = vmcs_read64(GUEST_PDPTR1);
10433 vmcs12->guest_pdptr2 = vmcs_read64(GUEST_PDPTR2);
10434 vmcs12->guest_pdptr3 = vmcs_read64(GUEST_PDPTR3);
10435 }
10436
10437 if (nested_cpu_has_ept(vmcs12))
10438 vmcs12->guest_linear_address = vmcs_readl(GUEST_LINEAR_ADDRESS);
10439
10440 if (nested_cpu_has_vid(vmcs12))
10441 vmcs12->guest_intr_status = vmcs_read16(GUEST_INTR_STATUS);
10442
10443 vmcs12->vm_entry_controls =
10444 (vmcs12->vm_entry_controls & ~VM_ENTRY_IA32E_MODE) |
10445 (vm_entry_controls_get(to_vmx(vcpu)) & VM_ENTRY_IA32E_MODE);
10446
10447 if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_DEBUG_CONTROLS) {
10448 kvm_get_dr(vcpu, 7, (unsigned long *)&vmcs12->guest_dr7);
10449 vmcs12->guest_ia32_debugctl = vmcs_read64(GUEST_IA32_DEBUGCTL);
10450 }
10451
10452 /* TODO: These cannot have changed unless we have MSR bitmaps and
10453 * the relevant bit asks not to trap the change */
10454 if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_IA32_PAT)
10455 vmcs12->guest_ia32_pat = vmcs_read64(GUEST_IA32_PAT);
10456 if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_IA32_EFER)
10457 vmcs12->guest_ia32_efer = vcpu->arch.efer;
10458 vmcs12->guest_sysenter_cs = vmcs_read32(GUEST_SYSENTER_CS);
10459 vmcs12->guest_sysenter_esp = vmcs_readl(GUEST_SYSENTER_ESP);
10460 vmcs12->guest_sysenter_eip = vmcs_readl(GUEST_SYSENTER_EIP);
10461 if (kvm_mpx_supported())
10462 vmcs12->guest_bndcfgs = vmcs_read64(GUEST_BNDCFGS);
10463 if (nested_cpu_has_xsaves(vmcs12))
10464 vmcs12->xss_exit_bitmap = vmcs_read64(XSS_EXIT_BITMAP);
10465
10466 /* update exit information fields: */
10467
10468 vmcs12->vm_exit_reason = exit_reason;
10469 vmcs12->exit_qualification = exit_qualification;
10470
10471 vmcs12->vm_exit_intr_info = exit_intr_info;
10472 if ((vmcs12->vm_exit_intr_info &
10473 (INTR_INFO_VALID_MASK | INTR_INFO_DELIVER_CODE_MASK)) ==
10474 (INTR_INFO_VALID_MASK | INTR_INFO_DELIVER_CODE_MASK))
10475 vmcs12->vm_exit_intr_error_code =
10476 vmcs_read32(VM_EXIT_INTR_ERROR_CODE);
10477 vmcs12->idt_vectoring_info_field = 0;
10478 vmcs12->vm_exit_instruction_len = vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
10479 vmcs12->vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
10480
10481 if (!(vmcs12->vm_exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY)) {
10482 /* vm_entry_intr_info_field is cleared on exit. Emulate this
10483 * instead of reading the real value. */
10484 vmcs12->vm_entry_intr_info_field &= ~INTR_INFO_VALID_MASK;
10485
10486 /*
10487 * Transfer the event that L0 or L1 may wanted to inject into
10488 * L2 to IDT_VECTORING_INFO_FIELD.
10489 */
10490 vmcs12_save_pending_event(vcpu, vmcs12);
10491 }
10492
10493 /*
10494 * Drop what we picked up for L2 via vmx_complete_interrupts. It is
10495 * preserved above and would only end up incorrectly in L1.
10496 */
10497 vcpu->arch.nmi_injected = false;
10498 kvm_clear_exception_queue(vcpu);
10499 kvm_clear_interrupt_queue(vcpu);
10500 }
10501
10502 /*
10503 * A part of what we need to when the nested L2 guest exits and we want to
10504 * run its L1 parent, is to reset L1's guest state to the host state specified
10505 * in vmcs12.
10506 * This function is to be called not only on normal nested exit, but also on
10507 * a nested entry failure, as explained in Intel's spec, 3B.23.7 ("VM-Entry
10508 * Failures During or After Loading Guest State").
10509 * This function should be called when the active VMCS is L1's (vmcs01).
10510 */
10511 static void load_vmcs12_host_state(struct kvm_vcpu *vcpu,
10512 struct vmcs12 *vmcs12)
10513 {
10514 struct kvm_segment seg;
10515
10516 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_EFER)
10517 vcpu->arch.efer = vmcs12->host_ia32_efer;
10518 else if (vmcs12->vm_exit_controls & VM_EXIT_HOST_ADDR_SPACE_SIZE)
10519 vcpu->arch.efer |= (EFER_LMA | EFER_LME);
10520 else
10521 vcpu->arch.efer &= ~(EFER_LMA | EFER_LME);
10522 vmx_set_efer(vcpu, vcpu->arch.efer);
10523
10524 kvm_register_write(vcpu, VCPU_REGS_RSP, vmcs12->host_rsp);
10525 kvm_register_write(vcpu, VCPU_REGS_RIP, vmcs12->host_rip);
10526 vmx_set_rflags(vcpu, X86_EFLAGS_FIXED);
10527 /*
10528 * Note that calling vmx_set_cr0 is important, even if cr0 hasn't
10529 * actually changed, because it depends on the current state of
10530 * fpu_active (which may have changed).
10531 * Note that vmx_set_cr0 refers to efer set above.
10532 */
10533 vmx_set_cr0(vcpu, vmcs12->host_cr0);
10534 /*
10535 * If we did fpu_activate()/fpu_deactivate() during L2's run, we need
10536 * to apply the same changes to L1's vmcs. We just set cr0 correctly,
10537 * but we also need to update cr0_guest_host_mask and exception_bitmap.
10538 */
10539 update_exception_bitmap(vcpu);
10540 vcpu->arch.cr0_guest_owned_bits = (vcpu->fpu_active ? X86_CR0_TS : 0);
10541 vmcs_writel(CR0_GUEST_HOST_MASK, ~vcpu->arch.cr0_guest_owned_bits);
10542
10543 /*
10544 * Note that CR4_GUEST_HOST_MASK is already set in the original vmcs01
10545 * (KVM doesn't change it)- no reason to call set_cr4_guest_host_mask();
10546 */
10547 vcpu->arch.cr4_guest_owned_bits = ~vmcs_readl(CR4_GUEST_HOST_MASK);
10548 kvm_set_cr4(vcpu, vmcs12->host_cr4);
10549
10550 nested_ept_uninit_mmu_context(vcpu);
10551
10552 kvm_set_cr3(vcpu, vmcs12->host_cr3);
10553 kvm_mmu_reset_context(vcpu);
10554
10555 if (!enable_ept)
10556 vcpu->arch.walk_mmu->inject_page_fault = kvm_inject_page_fault;
10557
10558 if (enable_vpid) {
10559 /*
10560 * Trivially support vpid by letting L2s share their parent
10561 * L1's vpid. TODO: move to a more elaborate solution, giving
10562 * each L2 its own vpid and exposing the vpid feature to L1.
10563 */
10564 vmx_flush_tlb(vcpu);
10565 }
10566
10567
10568 vmcs_write32(GUEST_SYSENTER_CS, vmcs12->host_ia32_sysenter_cs);
10569 vmcs_writel(GUEST_SYSENTER_ESP, vmcs12->host_ia32_sysenter_esp);
10570 vmcs_writel(GUEST_SYSENTER_EIP, vmcs12->host_ia32_sysenter_eip);
10571 vmcs_writel(GUEST_IDTR_BASE, vmcs12->host_idtr_base);
10572 vmcs_writel(GUEST_GDTR_BASE, vmcs12->host_gdtr_base);
10573
10574 /* If not VM_EXIT_CLEAR_BNDCFGS, the L2 value propagates to L1. */
10575 if (vmcs12->vm_exit_controls & VM_EXIT_CLEAR_BNDCFGS)
10576 vmcs_write64(GUEST_BNDCFGS, 0);
10577
10578 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PAT) {
10579 vmcs_write64(GUEST_IA32_PAT, vmcs12->host_ia32_pat);
10580 vcpu->arch.pat = vmcs12->host_ia32_pat;
10581 }
10582 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL)
10583 vmcs_write64(GUEST_IA32_PERF_GLOBAL_CTRL,
10584 vmcs12->host_ia32_perf_global_ctrl);
10585
10586 /* Set L1 segment info according to Intel SDM
10587 27.5.2 Loading Host Segment and Descriptor-Table Registers */
10588 seg = (struct kvm_segment) {
10589 .base = 0,
10590 .limit = 0xFFFFFFFF,
10591 .selector = vmcs12->host_cs_selector,
10592 .type = 11,
10593 .present = 1,
10594 .s = 1,
10595 .g = 1
10596 };
10597 if (vmcs12->vm_exit_controls & VM_EXIT_HOST_ADDR_SPACE_SIZE)
10598 seg.l = 1;
10599 else
10600 seg.db = 1;
10601 vmx_set_segment(vcpu, &seg, VCPU_SREG_CS);
10602 seg = (struct kvm_segment) {
10603 .base = 0,
10604 .limit = 0xFFFFFFFF,
10605 .type = 3,
10606 .present = 1,
10607 .s = 1,
10608 .db = 1,
10609 .g = 1
10610 };
10611 seg.selector = vmcs12->host_ds_selector;
10612 vmx_set_segment(vcpu, &seg, VCPU_SREG_DS);
10613 seg.selector = vmcs12->host_es_selector;
10614 vmx_set_segment(vcpu, &seg, VCPU_SREG_ES);
10615 seg.selector = vmcs12->host_ss_selector;
10616 vmx_set_segment(vcpu, &seg, VCPU_SREG_SS);
10617 seg.selector = vmcs12->host_fs_selector;
10618 seg.base = vmcs12->host_fs_base;
10619 vmx_set_segment(vcpu, &seg, VCPU_SREG_FS);
10620 seg.selector = vmcs12->host_gs_selector;
10621 seg.base = vmcs12->host_gs_base;
10622 vmx_set_segment(vcpu, &seg, VCPU_SREG_GS);
10623 seg = (struct kvm_segment) {
10624 .base = vmcs12->host_tr_base,
10625 .limit = 0x67,
10626 .selector = vmcs12->host_tr_selector,
10627 .type = 11,
10628 .present = 1
10629 };
10630 vmx_set_segment(vcpu, &seg, VCPU_SREG_TR);
10631
10632 kvm_set_dr(vcpu, 7, 0x400);
10633 vmcs_write64(GUEST_IA32_DEBUGCTL, 0);
10634
10635 if (cpu_has_vmx_msr_bitmap())
10636 vmx_set_msr_bitmap(vcpu);
10637
10638 if (nested_vmx_load_msr(vcpu, vmcs12->vm_exit_msr_load_addr,
10639 vmcs12->vm_exit_msr_load_count))
10640 nested_vmx_abort(vcpu, VMX_ABORT_LOAD_HOST_MSR_FAIL);
10641 }
10642
10643 /*
10644 * Emulate an exit from nested guest (L2) to L1, i.e., prepare to run L1
10645 * and modify vmcs12 to make it see what it would expect to see there if
10646 * L2 was its real guest. Must only be called when in L2 (is_guest_mode())
10647 */
10648 static void nested_vmx_vmexit(struct kvm_vcpu *vcpu, u32 exit_reason,
10649 u32 exit_intr_info,
10650 unsigned long exit_qualification)
10651 {
10652 struct vcpu_vmx *vmx = to_vmx(vcpu);
10653 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
10654 u32 vm_inst_error = 0;
10655
10656 /* trying to cancel vmlaunch/vmresume is a bug */
10657 WARN_ON_ONCE(vmx->nested.nested_run_pending);
10658
10659 leave_guest_mode(vcpu);
10660 prepare_vmcs12(vcpu, vmcs12, exit_reason, exit_intr_info,
10661 exit_qualification);
10662
10663 if (nested_vmx_store_msr(vcpu, vmcs12->vm_exit_msr_store_addr,
10664 vmcs12->vm_exit_msr_store_count))
10665 nested_vmx_abort(vcpu, VMX_ABORT_SAVE_GUEST_MSR_FAIL);
10666
10667 if (unlikely(vmx->fail))
10668 vm_inst_error = vmcs_read32(VM_INSTRUCTION_ERROR);
10669
10670 vmx_load_vmcs01(vcpu);
10671
10672 if ((exit_reason == EXIT_REASON_EXTERNAL_INTERRUPT)
10673 && nested_exit_intr_ack_set(vcpu)) {
10674 int irq = kvm_cpu_get_interrupt(vcpu);
10675 WARN_ON(irq < 0);
10676 vmcs12->vm_exit_intr_info = irq |
10677 INTR_INFO_VALID_MASK | INTR_TYPE_EXT_INTR;
10678 }
10679
10680 trace_kvm_nested_vmexit_inject(vmcs12->vm_exit_reason,
10681 vmcs12->exit_qualification,
10682 vmcs12->idt_vectoring_info_field,
10683 vmcs12->vm_exit_intr_info,
10684 vmcs12->vm_exit_intr_error_code,
10685 KVM_ISA_VMX);
10686
10687 vm_entry_controls_reset_shadow(vmx);
10688 vm_exit_controls_reset_shadow(vmx);
10689 vmx_segment_cache_clear(vmx);
10690
10691 /* if no vmcs02 cache requested, remove the one we used */
10692 if (VMCS02_POOL_SIZE == 0)
10693 nested_free_vmcs02(vmx, vmx->nested.current_vmptr);
10694
10695 load_vmcs12_host_state(vcpu, vmcs12);
10696
10697 /* Update any VMCS fields that might have changed while L2 ran */
10698 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, vmx->msr_autoload.nr);
10699 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, vmx->msr_autoload.nr);
10700 vmcs_write64(TSC_OFFSET, vcpu->arch.tsc_offset);
10701 if (vmx->hv_deadline_tsc == -1)
10702 vmcs_clear_bits(PIN_BASED_VM_EXEC_CONTROL,
10703 PIN_BASED_VMX_PREEMPTION_TIMER);
10704 else
10705 vmcs_set_bits(PIN_BASED_VM_EXEC_CONTROL,
10706 PIN_BASED_VMX_PREEMPTION_TIMER);
10707 if (kvm_has_tsc_control)
10708 decache_tsc_multiplier(vmx);
10709
10710 if (vmx->nested.change_vmcs01_virtual_x2apic_mode) {
10711 vmx->nested.change_vmcs01_virtual_x2apic_mode = false;
10712 vmx_set_virtual_x2apic_mode(vcpu,
10713 vcpu->arch.apic_base & X2APIC_ENABLE);
10714 }
10715
10716 /* This is needed for same reason as it was needed in prepare_vmcs02 */
10717 vmx->host_rsp = 0;
10718
10719 /* Unpin physical memory we referred to in vmcs02 */
10720 if (vmx->nested.apic_access_page) {
10721 nested_release_page(vmx->nested.apic_access_page);
10722 vmx->nested.apic_access_page = NULL;
10723 }
10724 if (vmx->nested.virtual_apic_page) {
10725 nested_release_page(vmx->nested.virtual_apic_page);
10726 vmx->nested.virtual_apic_page = NULL;
10727 }
10728 if (vmx->nested.pi_desc_page) {
10729 kunmap(vmx->nested.pi_desc_page);
10730 nested_release_page(vmx->nested.pi_desc_page);
10731 vmx->nested.pi_desc_page = NULL;
10732 vmx->nested.pi_desc = NULL;
10733 }
10734
10735 /*
10736 * We are now running in L2, mmu_notifier will force to reload the
10737 * page's hpa for L2 vmcs. Need to reload it for L1 before entering L1.
10738 */
10739 kvm_make_request(KVM_REQ_APIC_PAGE_RELOAD, vcpu);
10740
10741 /*
10742 * Exiting from L2 to L1, we're now back to L1 which thinks it just
10743 * finished a VMLAUNCH or VMRESUME instruction, so we need to set the
10744 * success or failure flag accordingly.
10745 */
10746 if (unlikely(vmx->fail)) {
10747 vmx->fail = 0;
10748 nested_vmx_failValid(vcpu, vm_inst_error);
10749 } else
10750 nested_vmx_succeed(vcpu);
10751 if (enable_shadow_vmcs)
10752 vmx->nested.sync_shadow_vmcs = true;
10753
10754 /* in case we halted in L2 */
10755 vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
10756 }
10757
10758 /*
10759 * Forcibly leave nested mode in order to be able to reset the VCPU later on.
10760 */
10761 static void vmx_leave_nested(struct kvm_vcpu *vcpu)
10762 {
10763 if (is_guest_mode(vcpu))
10764 nested_vmx_vmexit(vcpu, -1, 0, 0);
10765 free_nested(to_vmx(vcpu));
10766 }
10767
10768 /*
10769 * L1's failure to enter L2 is a subset of a normal exit, as explained in
10770 * 23.7 "VM-entry failures during or after loading guest state" (this also
10771 * lists the acceptable exit-reason and exit-qualification parameters).
10772 * It should only be called before L2 actually succeeded to run, and when
10773 * vmcs01 is current (it doesn't leave_guest_mode() or switch vmcss).
10774 */
10775 static void nested_vmx_entry_failure(struct kvm_vcpu *vcpu,
10776 struct vmcs12 *vmcs12,
10777 u32 reason, unsigned long qualification)
10778 {
10779 load_vmcs12_host_state(vcpu, vmcs12);
10780 vmcs12->vm_exit_reason = reason | VMX_EXIT_REASONS_FAILED_VMENTRY;
10781 vmcs12->exit_qualification = qualification;
10782 nested_vmx_succeed(vcpu);
10783 if (enable_shadow_vmcs)
10784 to_vmx(vcpu)->nested.sync_shadow_vmcs = true;
10785 }
10786
10787 static int vmx_check_intercept(struct kvm_vcpu *vcpu,
10788 struct x86_instruction_info *info,
10789 enum x86_intercept_stage stage)
10790 {
10791 return X86EMUL_CONTINUE;
10792 }
10793
10794 #ifdef CONFIG_X86_64
10795 /* (a << shift) / divisor, return 1 if overflow otherwise 0 */
10796 static inline int u64_shl_div_u64(u64 a, unsigned int shift,
10797 u64 divisor, u64 *result)
10798 {
10799 u64 low = a << shift, high = a >> (64 - shift);
10800
10801 /* To avoid the overflow on divq */
10802 if (high >= divisor)
10803 return 1;
10804
10805 /* Low hold the result, high hold rem which is discarded */
10806 asm("divq %2\n\t" : "=a" (low), "=d" (high) :
10807 "rm" (divisor), "0" (low), "1" (high));
10808 *result = low;
10809
10810 return 0;
10811 }
10812
10813 static int vmx_set_hv_timer(struct kvm_vcpu *vcpu, u64 guest_deadline_tsc)
10814 {
10815 struct vcpu_vmx *vmx = to_vmx(vcpu);
10816 u64 tscl = rdtsc();
10817 u64 guest_tscl = kvm_read_l1_tsc(vcpu, tscl);
10818 u64 delta_tsc = max(guest_deadline_tsc, guest_tscl) - guest_tscl;
10819
10820 /* Convert to host delta tsc if tsc scaling is enabled */
10821 if (vcpu->arch.tsc_scaling_ratio != kvm_default_tsc_scaling_ratio &&
10822 u64_shl_div_u64(delta_tsc,
10823 kvm_tsc_scaling_ratio_frac_bits,
10824 vcpu->arch.tsc_scaling_ratio,
10825 &delta_tsc))
10826 return -ERANGE;
10827
10828 /*
10829 * If the delta tsc can't fit in the 32 bit after the multi shift,
10830 * we can't use the preemption timer.
10831 * It's possible that it fits on later vmentries, but checking
10832 * on every vmentry is costly so we just use an hrtimer.
10833 */
10834 if (delta_tsc >> (cpu_preemption_timer_multi + 32))
10835 return -ERANGE;
10836
10837 vmx->hv_deadline_tsc = tscl + delta_tsc;
10838 vmcs_set_bits(PIN_BASED_VM_EXEC_CONTROL,
10839 PIN_BASED_VMX_PREEMPTION_TIMER);
10840 return 0;
10841 }
10842
10843 static void vmx_cancel_hv_timer(struct kvm_vcpu *vcpu)
10844 {
10845 struct vcpu_vmx *vmx = to_vmx(vcpu);
10846 vmx->hv_deadline_tsc = -1;
10847 vmcs_clear_bits(PIN_BASED_VM_EXEC_CONTROL,
10848 PIN_BASED_VMX_PREEMPTION_TIMER);
10849 }
10850 #endif
10851
10852 static void vmx_sched_in(struct kvm_vcpu *vcpu, int cpu)
10853 {
10854 if (ple_gap)
10855 shrink_ple_window(vcpu);
10856 }
10857
10858 static void vmx_slot_enable_log_dirty(struct kvm *kvm,
10859 struct kvm_memory_slot *slot)
10860 {
10861 kvm_mmu_slot_leaf_clear_dirty(kvm, slot);
10862 kvm_mmu_slot_largepage_remove_write_access(kvm, slot);
10863 }
10864
10865 static void vmx_slot_disable_log_dirty(struct kvm *kvm,
10866 struct kvm_memory_slot *slot)
10867 {
10868 kvm_mmu_slot_set_dirty(kvm, slot);
10869 }
10870
10871 static void vmx_flush_log_dirty(struct kvm *kvm)
10872 {
10873 kvm_flush_pml_buffers(kvm);
10874 }
10875
10876 static void vmx_enable_log_dirty_pt_masked(struct kvm *kvm,
10877 struct kvm_memory_slot *memslot,
10878 gfn_t offset, unsigned long mask)
10879 {
10880 kvm_mmu_clear_dirty_pt_masked(kvm, memslot, offset, mask);
10881 }
10882
10883 /*
10884 * This routine does the following things for vCPU which is going
10885 * to be blocked if VT-d PI is enabled.
10886 * - Store the vCPU to the wakeup list, so when interrupts happen
10887 * we can find the right vCPU to wake up.
10888 * - Change the Posted-interrupt descriptor as below:
10889 * 'NDST' <-- vcpu->pre_pcpu
10890 * 'NV' <-- POSTED_INTR_WAKEUP_VECTOR
10891 * - If 'ON' is set during this process, which means at least one
10892 * interrupt is posted for this vCPU, we cannot block it, in
10893 * this case, return 1, otherwise, return 0.
10894 *
10895 */
10896 static int pi_pre_block(struct kvm_vcpu *vcpu)
10897 {
10898 unsigned long flags;
10899 unsigned int dest;
10900 struct pi_desc old, new;
10901 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
10902
10903 if (!kvm_arch_has_assigned_device(vcpu->kvm) ||
10904 !irq_remapping_cap(IRQ_POSTING_CAP) ||
10905 !kvm_vcpu_apicv_active(vcpu))
10906 return 0;
10907
10908 vcpu->pre_pcpu = vcpu->cpu;
10909 spin_lock_irqsave(&per_cpu(blocked_vcpu_on_cpu_lock,
10910 vcpu->pre_pcpu), flags);
10911 list_add_tail(&vcpu->blocked_vcpu_list,
10912 &per_cpu(blocked_vcpu_on_cpu,
10913 vcpu->pre_pcpu));
10914 spin_unlock_irqrestore(&per_cpu(blocked_vcpu_on_cpu_lock,
10915 vcpu->pre_pcpu), flags);
10916
10917 do {
10918 old.control = new.control = pi_desc->control;
10919
10920 /*
10921 * We should not block the vCPU if
10922 * an interrupt is posted for it.
10923 */
10924 if (pi_test_on(pi_desc) == 1) {
10925 spin_lock_irqsave(&per_cpu(blocked_vcpu_on_cpu_lock,
10926 vcpu->pre_pcpu), flags);
10927 list_del(&vcpu->blocked_vcpu_list);
10928 spin_unlock_irqrestore(
10929 &per_cpu(blocked_vcpu_on_cpu_lock,
10930 vcpu->pre_pcpu), flags);
10931 vcpu->pre_pcpu = -1;
10932
10933 return 1;
10934 }
10935
10936 WARN((pi_desc->sn == 1),
10937 "Warning: SN field of posted-interrupts "
10938 "is set before blocking\n");
10939
10940 /*
10941 * Since vCPU can be preempted during this process,
10942 * vcpu->cpu could be different with pre_pcpu, we
10943 * need to set pre_pcpu as the destination of wakeup
10944 * notification event, then we can find the right vCPU
10945 * to wakeup in wakeup handler if interrupts happen
10946 * when the vCPU is in blocked state.
10947 */
10948 dest = cpu_physical_id(vcpu->pre_pcpu);
10949
10950 if (x2apic_enabled())
10951 new.ndst = dest;
10952 else
10953 new.ndst = (dest << 8) & 0xFF00;
10954
10955 /* set 'NV' to 'wakeup vector' */
10956 new.nv = POSTED_INTR_WAKEUP_VECTOR;
10957 } while (cmpxchg(&pi_desc->control, old.control,
10958 new.control) != old.control);
10959
10960 return 0;
10961 }
10962
10963 static int vmx_pre_block(struct kvm_vcpu *vcpu)
10964 {
10965 if (pi_pre_block(vcpu))
10966 return 1;
10967
10968 if (kvm_lapic_hv_timer_in_use(vcpu))
10969 kvm_lapic_switch_to_sw_timer(vcpu);
10970
10971 return 0;
10972 }
10973
10974 static void pi_post_block(struct kvm_vcpu *vcpu)
10975 {
10976 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
10977 struct pi_desc old, new;
10978 unsigned int dest;
10979 unsigned long flags;
10980
10981 if (!kvm_arch_has_assigned_device(vcpu->kvm) ||
10982 !irq_remapping_cap(IRQ_POSTING_CAP) ||
10983 !kvm_vcpu_apicv_active(vcpu))
10984 return;
10985
10986 do {
10987 old.control = new.control = pi_desc->control;
10988
10989 dest = cpu_physical_id(vcpu->cpu);
10990
10991 if (x2apic_enabled())
10992 new.ndst = dest;
10993 else
10994 new.ndst = (dest << 8) & 0xFF00;
10995
10996 /* Allow posting non-urgent interrupts */
10997 new.sn = 0;
10998
10999 /* set 'NV' to 'notification vector' */
11000 new.nv = POSTED_INTR_VECTOR;
11001 } while (cmpxchg(&pi_desc->control, old.control,
11002 new.control) != old.control);
11003
11004 if(vcpu->pre_pcpu != -1) {
11005 spin_lock_irqsave(
11006 &per_cpu(blocked_vcpu_on_cpu_lock,
11007 vcpu->pre_pcpu), flags);
11008 list_del(&vcpu->blocked_vcpu_list);
11009 spin_unlock_irqrestore(
11010 &per_cpu(blocked_vcpu_on_cpu_lock,
11011 vcpu->pre_pcpu), flags);
11012 vcpu->pre_pcpu = -1;
11013 }
11014 }
11015
11016 static void vmx_post_block(struct kvm_vcpu *vcpu)
11017 {
11018 if (kvm_x86_ops->set_hv_timer)
11019 kvm_lapic_switch_to_hv_timer(vcpu);
11020
11021 pi_post_block(vcpu);
11022 }
11023
11024 /*
11025 * vmx_update_pi_irte - set IRTE for Posted-Interrupts
11026 *
11027 * @kvm: kvm
11028 * @host_irq: host irq of the interrupt
11029 * @guest_irq: gsi of the interrupt
11030 * @set: set or unset PI
11031 * returns 0 on success, < 0 on failure
11032 */
11033 static int vmx_update_pi_irte(struct kvm *kvm, unsigned int host_irq,
11034 uint32_t guest_irq, bool set)
11035 {
11036 struct kvm_kernel_irq_routing_entry *e;
11037 struct kvm_irq_routing_table *irq_rt;
11038 struct kvm_lapic_irq irq;
11039 struct kvm_vcpu *vcpu;
11040 struct vcpu_data vcpu_info;
11041 int idx, ret = -EINVAL;
11042
11043 if (!kvm_arch_has_assigned_device(kvm) ||
11044 !irq_remapping_cap(IRQ_POSTING_CAP) ||
11045 !kvm_vcpu_apicv_active(kvm->vcpus[0]))
11046 return 0;
11047
11048 idx = srcu_read_lock(&kvm->irq_srcu);
11049 irq_rt = srcu_dereference(kvm->irq_routing, &kvm->irq_srcu);
11050 BUG_ON(guest_irq >= irq_rt->nr_rt_entries);
11051
11052 hlist_for_each_entry(e, &irq_rt->map[guest_irq], link) {
11053 if (e->type != KVM_IRQ_ROUTING_MSI)
11054 continue;
11055 /*
11056 * VT-d PI cannot support posting multicast/broadcast
11057 * interrupts to a vCPU, we still use interrupt remapping
11058 * for these kind of interrupts.
11059 *
11060 * For lowest-priority interrupts, we only support
11061 * those with single CPU as the destination, e.g. user
11062 * configures the interrupts via /proc/irq or uses
11063 * irqbalance to make the interrupts single-CPU.
11064 *
11065 * We will support full lowest-priority interrupt later.
11066 */
11067
11068 kvm_set_msi_irq(kvm, e, &irq);
11069 if (!kvm_intr_is_single_vcpu(kvm, &irq, &vcpu)) {
11070 /*
11071 * Make sure the IRTE is in remapped mode if
11072 * we don't handle it in posted mode.
11073 */
11074 ret = irq_set_vcpu_affinity(host_irq, NULL);
11075 if (ret < 0) {
11076 printk(KERN_INFO
11077 "failed to back to remapped mode, irq: %u\n",
11078 host_irq);
11079 goto out;
11080 }
11081
11082 continue;
11083 }
11084
11085 vcpu_info.pi_desc_addr = __pa(vcpu_to_pi_desc(vcpu));
11086 vcpu_info.vector = irq.vector;
11087
11088 trace_kvm_pi_irte_update(vcpu->vcpu_id, host_irq, e->gsi,
11089 vcpu_info.vector, vcpu_info.pi_desc_addr, set);
11090
11091 if (set)
11092 ret = irq_set_vcpu_affinity(host_irq, &vcpu_info);
11093 else {
11094 /* suppress notification event before unposting */
11095 pi_set_sn(vcpu_to_pi_desc(vcpu));
11096 ret = irq_set_vcpu_affinity(host_irq, NULL);
11097 pi_clear_sn(vcpu_to_pi_desc(vcpu));
11098 }
11099
11100 if (ret < 0) {
11101 printk(KERN_INFO "%s: failed to update PI IRTE\n",
11102 __func__);
11103 goto out;
11104 }
11105 }
11106
11107 ret = 0;
11108 out:
11109 srcu_read_unlock(&kvm->irq_srcu, idx);
11110 return ret;
11111 }
11112
11113 static void vmx_setup_mce(struct kvm_vcpu *vcpu)
11114 {
11115 if (vcpu->arch.mcg_cap & MCG_LMCE_P)
11116 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits |=
11117 FEATURE_CONTROL_LMCE;
11118 else
11119 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits &=
11120 ~FEATURE_CONTROL_LMCE;
11121 }
11122
11123 static struct kvm_x86_ops vmx_x86_ops __ro_after_init = {
11124 .cpu_has_kvm_support = cpu_has_kvm_support,
11125 .disabled_by_bios = vmx_disabled_by_bios,
11126 .hardware_setup = hardware_setup,
11127 .hardware_unsetup = hardware_unsetup,
11128 .check_processor_compatibility = vmx_check_processor_compat,
11129 .hardware_enable = hardware_enable,
11130 .hardware_disable = hardware_disable,
11131 .cpu_has_accelerated_tpr = report_flexpriority,
11132 .cpu_has_high_real_mode_segbase = vmx_has_high_real_mode_segbase,
11133
11134 .vcpu_create = vmx_create_vcpu,
11135 .vcpu_free = vmx_free_vcpu,
11136 .vcpu_reset = vmx_vcpu_reset,
11137
11138 .prepare_guest_switch = vmx_save_host_state,
11139 .vcpu_load = vmx_vcpu_load,
11140 .vcpu_put = vmx_vcpu_put,
11141
11142 .update_bp_intercept = update_exception_bitmap,
11143 .get_msr = vmx_get_msr,
11144 .set_msr = vmx_set_msr,
11145 .get_segment_base = vmx_get_segment_base,
11146 .get_segment = vmx_get_segment,
11147 .set_segment = vmx_set_segment,
11148 .get_cpl = vmx_get_cpl,
11149 .get_cs_db_l_bits = vmx_get_cs_db_l_bits,
11150 .decache_cr0_guest_bits = vmx_decache_cr0_guest_bits,
11151 .decache_cr3 = vmx_decache_cr3,
11152 .decache_cr4_guest_bits = vmx_decache_cr4_guest_bits,
11153 .set_cr0 = vmx_set_cr0,
11154 .set_cr3 = vmx_set_cr3,
11155 .set_cr4 = vmx_set_cr4,
11156 .set_efer = vmx_set_efer,
11157 .get_idt = vmx_get_idt,
11158 .set_idt = vmx_set_idt,
11159 .get_gdt = vmx_get_gdt,
11160 .set_gdt = vmx_set_gdt,
11161 .get_dr6 = vmx_get_dr6,
11162 .set_dr6 = vmx_set_dr6,
11163 .set_dr7 = vmx_set_dr7,
11164 .sync_dirty_debug_regs = vmx_sync_dirty_debug_regs,
11165 .cache_reg = vmx_cache_reg,
11166 .get_rflags = vmx_get_rflags,
11167 .set_rflags = vmx_set_rflags,
11168
11169 .get_pkru = vmx_get_pkru,
11170
11171 .fpu_activate = vmx_fpu_activate,
11172 .fpu_deactivate = vmx_fpu_deactivate,
11173
11174 .tlb_flush = vmx_flush_tlb,
11175
11176 .run = vmx_vcpu_run,
11177 .handle_exit = vmx_handle_exit,
11178 .skip_emulated_instruction = skip_emulated_instruction,
11179 .set_interrupt_shadow = vmx_set_interrupt_shadow,
11180 .get_interrupt_shadow = vmx_get_interrupt_shadow,
11181 .patch_hypercall = vmx_patch_hypercall,
11182 .set_irq = vmx_inject_irq,
11183 .set_nmi = vmx_inject_nmi,
11184 .queue_exception = vmx_queue_exception,
11185 .cancel_injection = vmx_cancel_injection,
11186 .interrupt_allowed = vmx_interrupt_allowed,
11187 .nmi_allowed = vmx_nmi_allowed,
11188 .get_nmi_mask = vmx_get_nmi_mask,
11189 .set_nmi_mask = vmx_set_nmi_mask,
11190 .enable_nmi_window = enable_nmi_window,
11191 .enable_irq_window = enable_irq_window,
11192 .update_cr8_intercept = update_cr8_intercept,
11193 .set_virtual_x2apic_mode = vmx_set_virtual_x2apic_mode,
11194 .set_apic_access_page_addr = vmx_set_apic_access_page_addr,
11195 .get_enable_apicv = vmx_get_enable_apicv,
11196 .refresh_apicv_exec_ctrl = vmx_refresh_apicv_exec_ctrl,
11197 .load_eoi_exitmap = vmx_load_eoi_exitmap,
11198 .hwapic_irr_update = vmx_hwapic_irr_update,
11199 .hwapic_isr_update = vmx_hwapic_isr_update,
11200 .sync_pir_to_irr = vmx_sync_pir_to_irr,
11201 .deliver_posted_interrupt = vmx_deliver_posted_interrupt,
11202
11203 .set_tss_addr = vmx_set_tss_addr,
11204 .get_tdp_level = get_ept_level,
11205 .get_mt_mask = vmx_get_mt_mask,
11206
11207 .get_exit_info = vmx_get_exit_info,
11208
11209 .get_lpage_level = vmx_get_lpage_level,
11210
11211 .cpuid_update = vmx_cpuid_update,
11212
11213 .rdtscp_supported = vmx_rdtscp_supported,
11214 .invpcid_supported = vmx_invpcid_supported,
11215
11216 .set_supported_cpuid = vmx_set_supported_cpuid,
11217
11218 .has_wbinvd_exit = cpu_has_vmx_wbinvd_exit,
11219
11220 .write_tsc_offset = vmx_write_tsc_offset,
11221
11222 .set_tdp_cr3 = vmx_set_cr3,
11223
11224 .check_intercept = vmx_check_intercept,
11225 .handle_external_intr = vmx_handle_external_intr,
11226 .mpx_supported = vmx_mpx_supported,
11227 .xsaves_supported = vmx_xsaves_supported,
11228
11229 .check_nested_events = vmx_check_nested_events,
11230
11231 .sched_in = vmx_sched_in,
11232
11233 .slot_enable_log_dirty = vmx_slot_enable_log_dirty,
11234 .slot_disable_log_dirty = vmx_slot_disable_log_dirty,
11235 .flush_log_dirty = vmx_flush_log_dirty,
11236 .enable_log_dirty_pt_masked = vmx_enable_log_dirty_pt_masked,
11237
11238 .pre_block = vmx_pre_block,
11239 .post_block = vmx_post_block,
11240
11241 .pmu_ops = &intel_pmu_ops,
11242
11243 .update_pi_irte = vmx_update_pi_irte,
11244
11245 #ifdef CONFIG_X86_64
11246 .set_hv_timer = vmx_set_hv_timer,
11247 .cancel_hv_timer = vmx_cancel_hv_timer,
11248 #endif
11249
11250 .setup_mce = vmx_setup_mce,
11251 };
11252
11253 static int __init vmx_init(void)
11254 {
11255 int r = kvm_init(&vmx_x86_ops, sizeof(struct vcpu_vmx),
11256 __alignof__(struct vcpu_vmx), THIS_MODULE);
11257 if (r)
11258 return r;
11259
11260 #ifdef CONFIG_KEXEC_CORE
11261 rcu_assign_pointer(crash_vmclear_loaded_vmcss,
11262 crash_vmclear_local_loaded_vmcss);
11263 #endif
11264
11265 return 0;
11266 }
11267
11268 static void __exit vmx_exit(void)
11269 {
11270 #ifdef CONFIG_KEXEC_CORE
11271 RCU_INIT_POINTER(crash_vmclear_loaded_vmcss, NULL);
11272 synchronize_rcu();
11273 #endif
11274
11275 kvm_exit();
11276 }
11277
11278 module_init(vmx_init)
11279 module_exit(vmx_exit)