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