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