]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blob - arch/x86/kvm/vmx.c
0510bc11beb253eabee22d6a85d7eff069b8ca9d
[mirror_ubuntu-artful-kernel.git] / arch / x86 / kvm / vmx.c
1 /*
2 * Kernel-based Virtual Machine driver for Linux
3 *
4 * This module enables machines with Intel VT-x extensions to run virtual
5 * machines without emulation or binary translation.
6 *
7 * Copyright (C) 2006 Qumranet, Inc.
8 * Copyright 2010 Red Hat, Inc. and/or its affiliates.
9 *
10 * Authors:
11 * Avi Kivity <avi@qumranet.com>
12 * Yaniv Kamay <yaniv@qumranet.com>
13 *
14 * This work is licensed under the terms of the GNU GPL, version 2. See
15 * the COPYING file in the top-level directory.
16 *
17 */
18
19 #include "irq.h"
20 #include "mmu.h"
21 #include "cpuid.h"
22 #include "lapic.h"
23
24 #include <linux/kvm_host.h>
25 #include <linux/module.h>
26 #include <linux/kernel.h>
27 #include <linux/mm.h>
28 #include <linux/highmem.h>
29 #include <linux/sched.h>
30 #include <linux/moduleparam.h>
31 #include <linux/mod_devicetable.h>
32 #include <linux/trace_events.h>
33 #include <linux/slab.h>
34 #include <linux/tboot.h>
35 #include <linux/hrtimer.h>
36 #include <linux/frame.h>
37 #include "kvm_cache_regs.h"
38 #include "x86.h"
39
40 #include <asm/cpu.h>
41 #include <asm/io.h>
42 #include <asm/desc.h>
43 #include <asm/vmx.h>
44 #include <asm/virtext.h>
45 #include <asm/mce.h>
46 #include <asm/fpu/internal.h>
47 #include <asm/perf_event.h>
48 #include <asm/debugreg.h>
49 #include <asm/kexec.h>
50 #include <asm/apic.h>
51 #include <asm/irq_remapping.h>
52 #include <asm/mmu_context.h>
53 #include <asm/microcode.h>
54 #include <asm/spec-ctrl.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 if (data & ~(SPEC_CTRL_IBRS | SPEC_CTRL_SSBD))
3371 return 1;
3372
3373 vcpu->arch.spec_ctrl = msr_info->data;
3374 break;
3375 case MSR_IA32_CR_PAT:
3376 if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) {
3377 if (!kvm_mtrr_valid(vcpu, MSR_IA32_CR_PAT, data))
3378 return 1;
3379 vmcs_write64(GUEST_IA32_PAT, data);
3380 vcpu->arch.pat = data;
3381 break;
3382 }
3383 ret = kvm_set_msr_common(vcpu, msr_info);
3384 break;
3385 case MSR_IA32_TSC_ADJUST:
3386 ret = kvm_set_msr_common(vcpu, msr_info);
3387 break;
3388 case MSR_IA32_MCG_EXT_CTL:
3389 if ((!msr_info->host_initiated &&
3390 !(to_vmx(vcpu)->msr_ia32_feature_control &
3391 FEATURE_CONTROL_LMCE)) ||
3392 (data & ~MCG_EXT_CTL_LMCE_EN))
3393 return 1;
3394 vcpu->arch.mcg_ext_ctl = data;
3395 break;
3396 case MSR_IA32_FEATURE_CONTROL:
3397 if (!vmx_feature_control_msr_valid(vcpu, data) ||
3398 (to_vmx(vcpu)->msr_ia32_feature_control &
3399 FEATURE_CONTROL_LOCKED && !msr_info->host_initiated))
3400 return 1;
3401 vmx->msr_ia32_feature_control = data;
3402 if (msr_info->host_initiated && data == 0)
3403 vmx_leave_nested(vcpu);
3404 break;
3405 case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC:
3406 if (!msr_info->host_initiated)
3407 return 1; /* they are read-only */
3408 if (!nested_vmx_allowed(vcpu))
3409 return 1;
3410 return vmx_set_vmx_msr(vcpu, msr_index, data);
3411 case MSR_IA32_XSS:
3412 if (!vmx_xsaves_supported())
3413 return 1;
3414 /*
3415 * The only supported bit as of Skylake is bit 8, but
3416 * it is not supported on KVM.
3417 */
3418 if (data != 0)
3419 return 1;
3420 vcpu->arch.ia32_xss = data;
3421 if (vcpu->arch.ia32_xss != host_xss)
3422 add_atomic_switch_msr(vmx, MSR_IA32_XSS,
3423 vcpu->arch.ia32_xss, host_xss);
3424 else
3425 clear_atomic_switch_msr(vmx, MSR_IA32_XSS);
3426 break;
3427 case MSR_TSC_AUX:
3428 if (!guest_cpuid_has_rdtscp(vcpu) && !msr_info->host_initiated)
3429 return 1;
3430 /* Check reserved bit, higher 32 bits should be zero */
3431 if ((data >> 32) != 0)
3432 return 1;
3433 /* Otherwise falls through */
3434 default:
3435 msr = find_msr_entry(vmx, msr_index);
3436 if (msr) {
3437 u64 old_msr_data = msr->data;
3438 msr->data = data;
3439 if (msr - vmx->guest_msrs < vmx->save_nmsrs) {
3440 preempt_disable();
3441 ret = kvm_set_shared_msr(msr->index, msr->data,
3442 msr->mask);
3443 preempt_enable();
3444 if (ret)
3445 msr->data = old_msr_data;
3446 }
3447 break;
3448 }
3449 ret = kvm_set_msr_common(vcpu, msr_info);
3450 }
3451
3452 return ret;
3453 }
3454
3455 static void vmx_cache_reg(struct kvm_vcpu *vcpu, enum kvm_reg reg)
3456 {
3457 __set_bit(reg, (unsigned long *)&vcpu->arch.regs_avail);
3458 switch (reg) {
3459 case VCPU_REGS_RSP:
3460 vcpu->arch.regs[VCPU_REGS_RSP] = vmcs_readl(GUEST_RSP);
3461 break;
3462 case VCPU_REGS_RIP:
3463 vcpu->arch.regs[VCPU_REGS_RIP] = vmcs_readl(GUEST_RIP);
3464 break;
3465 case VCPU_EXREG_PDPTR:
3466 if (enable_ept)
3467 ept_save_pdptrs(vcpu);
3468 break;
3469 default:
3470 break;
3471 }
3472 }
3473
3474 static __init int cpu_has_kvm_support(void)
3475 {
3476 return cpu_has_vmx();
3477 }
3478
3479 static __init int vmx_disabled_by_bios(void)
3480 {
3481 u64 msr;
3482
3483 rdmsrl(MSR_IA32_FEATURE_CONTROL, msr);
3484 if (msr & FEATURE_CONTROL_LOCKED) {
3485 /* launched w/ TXT and VMX disabled */
3486 if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX)
3487 && tboot_enabled())
3488 return 1;
3489 /* launched w/o TXT and VMX only enabled w/ TXT */
3490 if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX)
3491 && (msr & FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX)
3492 && !tboot_enabled()) {
3493 printk(KERN_WARNING "kvm: disable TXT in the BIOS or "
3494 "activate TXT before enabling KVM\n");
3495 return 1;
3496 }
3497 /* launched w/o TXT and VMX disabled */
3498 if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX)
3499 && !tboot_enabled())
3500 return 1;
3501 }
3502
3503 return 0;
3504 }
3505
3506 static void kvm_cpu_vmxon(u64 addr)
3507 {
3508 cr4_set_bits(X86_CR4_VMXE);
3509 intel_pt_handle_vmx(1);
3510
3511 asm volatile (ASM_VMX_VMXON_RAX
3512 : : "a"(&addr), "m"(addr)
3513 : "memory", "cc");
3514 }
3515
3516 static int hardware_enable(void)
3517 {
3518 int cpu = raw_smp_processor_id();
3519 u64 phys_addr = __pa(per_cpu(vmxarea, cpu));
3520 u64 old, test_bits;
3521
3522 if (cr4_read_shadow() & X86_CR4_VMXE)
3523 return -EBUSY;
3524
3525 INIT_LIST_HEAD(&per_cpu(loaded_vmcss_on_cpu, cpu));
3526 INIT_LIST_HEAD(&per_cpu(blocked_vcpu_on_cpu, cpu));
3527 spin_lock_init(&per_cpu(blocked_vcpu_on_cpu_lock, cpu));
3528
3529 /*
3530 * Now we can enable the vmclear operation in kdump
3531 * since the loaded_vmcss_on_cpu list on this cpu
3532 * has been initialized.
3533 *
3534 * Though the cpu is not in VMX operation now, there
3535 * is no problem to enable the vmclear operation
3536 * for the loaded_vmcss_on_cpu list is empty!
3537 */
3538 crash_enable_local_vmclear(cpu);
3539
3540 rdmsrl(MSR_IA32_FEATURE_CONTROL, old);
3541
3542 test_bits = FEATURE_CONTROL_LOCKED;
3543 test_bits |= FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
3544 if (tboot_enabled())
3545 test_bits |= FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX;
3546
3547 if ((old & test_bits) != test_bits) {
3548 /* enable and lock */
3549 wrmsrl(MSR_IA32_FEATURE_CONTROL, old | test_bits);
3550 }
3551 kvm_cpu_vmxon(phys_addr);
3552 ept_sync_global();
3553
3554 return 0;
3555 }
3556
3557 static void vmclear_local_loaded_vmcss(void)
3558 {
3559 int cpu = raw_smp_processor_id();
3560 struct loaded_vmcs *v, *n;
3561
3562 list_for_each_entry_safe(v, n, &per_cpu(loaded_vmcss_on_cpu, cpu),
3563 loaded_vmcss_on_cpu_link)
3564 __loaded_vmcs_clear(v);
3565 }
3566
3567
3568 /* Just like cpu_vmxoff(), but with the __kvm_handle_fault_on_reboot()
3569 * tricks.
3570 */
3571 static void kvm_cpu_vmxoff(void)
3572 {
3573 asm volatile (__ex(ASM_VMX_VMXOFF) : : : "cc");
3574
3575 intel_pt_handle_vmx(0);
3576 cr4_clear_bits(X86_CR4_VMXE);
3577 }
3578
3579 static void hardware_disable(void)
3580 {
3581 vmclear_local_loaded_vmcss();
3582 kvm_cpu_vmxoff();
3583 }
3584
3585 static __init int adjust_vmx_controls(u32 ctl_min, u32 ctl_opt,
3586 u32 msr, u32 *result)
3587 {
3588 u32 vmx_msr_low, vmx_msr_high;
3589 u32 ctl = ctl_min | ctl_opt;
3590
3591 rdmsr(msr, vmx_msr_low, vmx_msr_high);
3592
3593 ctl &= vmx_msr_high; /* bit == 0 in high word ==> must be zero */
3594 ctl |= vmx_msr_low; /* bit == 1 in low word ==> must be one */
3595
3596 /* Ensure minimum (required) set of control bits are supported. */
3597 if (ctl_min & ~ctl)
3598 return -EIO;
3599
3600 *result = ctl;
3601 return 0;
3602 }
3603
3604 static __init bool allow_1_setting(u32 msr, u32 ctl)
3605 {
3606 u32 vmx_msr_low, vmx_msr_high;
3607
3608 rdmsr(msr, vmx_msr_low, vmx_msr_high);
3609 return vmx_msr_high & ctl;
3610 }
3611
3612 static __init int setup_vmcs_config(struct vmcs_config *vmcs_conf)
3613 {
3614 u32 vmx_msr_low, vmx_msr_high;
3615 u32 min, opt, min2, opt2;
3616 u32 _pin_based_exec_control = 0;
3617 u32 _cpu_based_exec_control = 0;
3618 u32 _cpu_based_2nd_exec_control = 0;
3619 u32 _vmexit_control = 0;
3620 u32 _vmentry_control = 0;
3621
3622 min = CPU_BASED_HLT_EXITING |
3623 #ifdef CONFIG_X86_64
3624 CPU_BASED_CR8_LOAD_EXITING |
3625 CPU_BASED_CR8_STORE_EXITING |
3626 #endif
3627 CPU_BASED_CR3_LOAD_EXITING |
3628 CPU_BASED_CR3_STORE_EXITING |
3629 CPU_BASED_USE_IO_BITMAPS |
3630 CPU_BASED_MOV_DR_EXITING |
3631 CPU_BASED_USE_TSC_OFFSETING |
3632 CPU_BASED_INVLPG_EXITING |
3633 CPU_BASED_RDPMC_EXITING;
3634
3635 if (!kvm_mwait_in_guest())
3636 min |= CPU_BASED_MWAIT_EXITING |
3637 CPU_BASED_MONITOR_EXITING;
3638
3639 opt = CPU_BASED_TPR_SHADOW |
3640 CPU_BASED_USE_MSR_BITMAPS |
3641 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
3642 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PROCBASED_CTLS,
3643 &_cpu_based_exec_control) < 0)
3644 return -EIO;
3645 #ifdef CONFIG_X86_64
3646 if ((_cpu_based_exec_control & CPU_BASED_TPR_SHADOW))
3647 _cpu_based_exec_control &= ~CPU_BASED_CR8_LOAD_EXITING &
3648 ~CPU_BASED_CR8_STORE_EXITING;
3649 #endif
3650 if (_cpu_based_exec_control & CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) {
3651 min2 = 0;
3652 opt2 = SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
3653 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
3654 SECONDARY_EXEC_WBINVD_EXITING |
3655 SECONDARY_EXEC_ENABLE_VPID |
3656 SECONDARY_EXEC_ENABLE_EPT |
3657 SECONDARY_EXEC_UNRESTRICTED_GUEST |
3658 SECONDARY_EXEC_PAUSE_LOOP_EXITING |
3659 SECONDARY_EXEC_RDTSCP |
3660 SECONDARY_EXEC_ENABLE_INVPCID |
3661 SECONDARY_EXEC_APIC_REGISTER_VIRT |
3662 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY |
3663 SECONDARY_EXEC_SHADOW_VMCS |
3664 SECONDARY_EXEC_XSAVES |
3665 SECONDARY_EXEC_ENABLE_PML |
3666 SECONDARY_EXEC_TSC_SCALING;
3667 if (adjust_vmx_controls(min2, opt2,
3668 MSR_IA32_VMX_PROCBASED_CTLS2,
3669 &_cpu_based_2nd_exec_control) < 0)
3670 return -EIO;
3671 }
3672 #ifndef CONFIG_X86_64
3673 if (!(_cpu_based_2nd_exec_control &
3674 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES))
3675 _cpu_based_exec_control &= ~CPU_BASED_TPR_SHADOW;
3676 #endif
3677
3678 if (!(_cpu_based_exec_control & CPU_BASED_TPR_SHADOW))
3679 _cpu_based_2nd_exec_control &= ~(
3680 SECONDARY_EXEC_APIC_REGISTER_VIRT |
3681 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
3682 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
3683
3684 if (_cpu_based_2nd_exec_control & SECONDARY_EXEC_ENABLE_EPT) {
3685 /* CR3 accesses and invlpg don't need to cause VM Exits when EPT
3686 enabled */
3687 _cpu_based_exec_control &= ~(CPU_BASED_CR3_LOAD_EXITING |
3688 CPU_BASED_CR3_STORE_EXITING |
3689 CPU_BASED_INVLPG_EXITING);
3690 rdmsr(MSR_IA32_VMX_EPT_VPID_CAP,
3691 vmx_capability.ept, vmx_capability.vpid);
3692 }
3693
3694 min = VM_EXIT_SAVE_DEBUG_CONTROLS | VM_EXIT_ACK_INTR_ON_EXIT;
3695 #ifdef CONFIG_X86_64
3696 min |= VM_EXIT_HOST_ADDR_SPACE_SIZE;
3697 #endif
3698 opt = VM_EXIT_SAVE_IA32_PAT | VM_EXIT_LOAD_IA32_PAT |
3699 VM_EXIT_CLEAR_BNDCFGS;
3700 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_EXIT_CTLS,
3701 &_vmexit_control) < 0)
3702 return -EIO;
3703
3704 min = PIN_BASED_EXT_INTR_MASK | PIN_BASED_NMI_EXITING;
3705 opt = PIN_BASED_VIRTUAL_NMIS | PIN_BASED_POSTED_INTR |
3706 PIN_BASED_VMX_PREEMPTION_TIMER;
3707 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PINBASED_CTLS,
3708 &_pin_based_exec_control) < 0)
3709 return -EIO;
3710
3711 if (cpu_has_broken_vmx_preemption_timer())
3712 _pin_based_exec_control &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
3713 if (!(_cpu_based_2nd_exec_control &
3714 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY))
3715 _pin_based_exec_control &= ~PIN_BASED_POSTED_INTR;
3716
3717 min = VM_ENTRY_LOAD_DEBUG_CONTROLS;
3718 opt = VM_ENTRY_LOAD_IA32_PAT | VM_ENTRY_LOAD_BNDCFGS;
3719 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_ENTRY_CTLS,
3720 &_vmentry_control) < 0)
3721 return -EIO;
3722
3723 rdmsr(MSR_IA32_VMX_BASIC, vmx_msr_low, vmx_msr_high);
3724
3725 /* IA-32 SDM Vol 3B: VMCS size is never greater than 4kB. */
3726 if ((vmx_msr_high & 0x1fff) > PAGE_SIZE)
3727 return -EIO;
3728
3729 #ifdef CONFIG_X86_64
3730 /* IA-32 SDM Vol 3B: 64-bit CPUs always have VMX_BASIC_MSR[48]==0. */
3731 if (vmx_msr_high & (1u<<16))
3732 return -EIO;
3733 #endif
3734
3735 /* Require Write-Back (WB) memory type for VMCS accesses. */
3736 if (((vmx_msr_high >> 18) & 15) != 6)
3737 return -EIO;
3738
3739 vmcs_conf->size = vmx_msr_high & 0x1fff;
3740 vmcs_conf->order = get_order(vmcs_conf->size);
3741 vmcs_conf->basic_cap = vmx_msr_high & ~0x1fff;
3742 vmcs_conf->revision_id = vmx_msr_low;
3743
3744 vmcs_conf->pin_based_exec_ctrl = _pin_based_exec_control;
3745 vmcs_conf->cpu_based_exec_ctrl = _cpu_based_exec_control;
3746 vmcs_conf->cpu_based_2nd_exec_ctrl = _cpu_based_2nd_exec_control;
3747 vmcs_conf->vmexit_ctrl = _vmexit_control;
3748 vmcs_conf->vmentry_ctrl = _vmentry_control;
3749
3750 cpu_has_load_ia32_efer =
3751 allow_1_setting(MSR_IA32_VMX_ENTRY_CTLS,
3752 VM_ENTRY_LOAD_IA32_EFER)
3753 && allow_1_setting(MSR_IA32_VMX_EXIT_CTLS,
3754 VM_EXIT_LOAD_IA32_EFER);
3755
3756 cpu_has_load_perf_global_ctrl =
3757 allow_1_setting(MSR_IA32_VMX_ENTRY_CTLS,
3758 VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL)
3759 && allow_1_setting(MSR_IA32_VMX_EXIT_CTLS,
3760 VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL);
3761
3762 /*
3763 * Some cpus support VM_ENTRY_(LOAD|SAVE)_IA32_PERF_GLOBAL_CTRL
3764 * but due to errata below it can't be used. Workaround is to use
3765 * msr load mechanism to switch IA32_PERF_GLOBAL_CTRL.
3766 *
3767 * VM Exit May Incorrectly Clear IA32_PERF_GLOBAL_CTRL [34:32]
3768 *
3769 * AAK155 (model 26)
3770 * AAP115 (model 30)
3771 * AAT100 (model 37)
3772 * BC86,AAY89,BD102 (model 44)
3773 * BA97 (model 46)
3774 *
3775 */
3776 if (cpu_has_load_perf_global_ctrl && boot_cpu_data.x86 == 0x6) {
3777 switch (boot_cpu_data.x86_model) {
3778 case 26:
3779 case 30:
3780 case 37:
3781 case 44:
3782 case 46:
3783 cpu_has_load_perf_global_ctrl = false;
3784 printk_once(KERN_WARNING"kvm: VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL "
3785 "does not work properly. Using workaround\n");
3786 break;
3787 default:
3788 break;
3789 }
3790 }
3791
3792 if (boot_cpu_has(X86_FEATURE_XSAVES))
3793 rdmsrl(MSR_IA32_XSS, host_xss);
3794
3795 return 0;
3796 }
3797
3798 static struct vmcs *alloc_vmcs_cpu(int cpu)
3799 {
3800 int node = cpu_to_node(cpu);
3801 struct page *pages;
3802 struct vmcs *vmcs;
3803
3804 pages = __alloc_pages_node(node, GFP_KERNEL, vmcs_config.order);
3805 if (!pages)
3806 return NULL;
3807 vmcs = page_address(pages);
3808 memset(vmcs, 0, vmcs_config.size);
3809 vmcs->revision_id = vmcs_config.revision_id; /* vmcs revision id */
3810 return vmcs;
3811 }
3812
3813 static struct vmcs *alloc_vmcs(void)
3814 {
3815 return alloc_vmcs_cpu(raw_smp_processor_id());
3816 }
3817
3818 static void free_vmcs(struct vmcs *vmcs)
3819 {
3820 free_pages((unsigned long)vmcs, vmcs_config.order);
3821 }
3822
3823 /*
3824 * Free a VMCS, but before that VMCLEAR it on the CPU where it was last loaded
3825 */
3826 static void free_loaded_vmcs(struct loaded_vmcs *loaded_vmcs)
3827 {
3828 if (!loaded_vmcs->vmcs)
3829 return;
3830 loaded_vmcs_clear(loaded_vmcs);
3831 free_vmcs(loaded_vmcs->vmcs);
3832 loaded_vmcs->vmcs = NULL;
3833 WARN_ON(loaded_vmcs->shadow_vmcs != NULL);
3834 }
3835
3836 static void free_kvm_area(void)
3837 {
3838 int cpu;
3839
3840 for_each_possible_cpu(cpu) {
3841 free_vmcs(per_cpu(vmxarea, cpu));
3842 per_cpu(vmxarea, cpu) = NULL;
3843 }
3844 }
3845
3846 enum vmcs_field_type {
3847 VMCS_FIELD_TYPE_U16 = 0,
3848 VMCS_FIELD_TYPE_U64 = 1,
3849 VMCS_FIELD_TYPE_U32 = 2,
3850 VMCS_FIELD_TYPE_NATURAL_WIDTH = 3
3851 };
3852
3853 static inline int vmcs_field_type(unsigned long field)
3854 {
3855 if (0x1 & field) /* the *_HIGH fields are all 32 bit */
3856 return VMCS_FIELD_TYPE_U32;
3857 return (field >> 13) & 0x3 ;
3858 }
3859
3860 static inline int vmcs_field_readonly(unsigned long field)
3861 {
3862 return (((field >> 10) & 0x3) == 1);
3863 }
3864
3865 static void init_vmcs_shadow_fields(void)
3866 {
3867 int i, j;
3868
3869 /* No checks for read only fields yet */
3870
3871 for (i = j = 0; i < max_shadow_read_write_fields; i++) {
3872 switch (shadow_read_write_fields[i]) {
3873 case GUEST_BNDCFGS:
3874 if (!kvm_mpx_supported())
3875 continue;
3876 break;
3877 default:
3878 break;
3879 }
3880
3881 if (j < i)
3882 shadow_read_write_fields[j] =
3883 shadow_read_write_fields[i];
3884 j++;
3885 }
3886 max_shadow_read_write_fields = j;
3887
3888 /* shadowed fields guest access without vmexit */
3889 for (i = 0; i < max_shadow_read_write_fields; i++) {
3890 unsigned long field = shadow_read_write_fields[i];
3891
3892 clear_bit(field, vmx_vmwrite_bitmap);
3893 clear_bit(field, vmx_vmread_bitmap);
3894 if (vmcs_field_type(field) == VMCS_FIELD_TYPE_U64) {
3895 clear_bit(field + 1, vmx_vmwrite_bitmap);
3896 clear_bit(field + 1, vmx_vmread_bitmap);
3897 }
3898 }
3899 for (i = 0; i < max_shadow_read_only_fields; i++) {
3900 unsigned long field = shadow_read_only_fields[i];
3901
3902 clear_bit(field, vmx_vmread_bitmap);
3903 if (vmcs_field_type(field) == VMCS_FIELD_TYPE_U64)
3904 clear_bit(field + 1, vmx_vmread_bitmap);
3905 }
3906 }
3907
3908 static __init int alloc_kvm_area(void)
3909 {
3910 int cpu;
3911
3912 for_each_possible_cpu(cpu) {
3913 struct vmcs *vmcs;
3914
3915 vmcs = alloc_vmcs_cpu(cpu);
3916 if (!vmcs) {
3917 free_kvm_area();
3918 return -ENOMEM;
3919 }
3920
3921 per_cpu(vmxarea, cpu) = vmcs;
3922 }
3923 return 0;
3924 }
3925
3926 static void fix_pmode_seg(struct kvm_vcpu *vcpu, int seg,
3927 struct kvm_segment *save)
3928 {
3929 if (!emulate_invalid_guest_state) {
3930 /*
3931 * CS and SS RPL should be equal during guest entry according
3932 * to VMX spec, but in reality it is not always so. Since vcpu
3933 * is in the middle of the transition from real mode to
3934 * protected mode it is safe to assume that RPL 0 is a good
3935 * default value.
3936 */
3937 if (seg == VCPU_SREG_CS || seg == VCPU_SREG_SS)
3938 save->selector &= ~SEGMENT_RPL_MASK;
3939 save->dpl = save->selector & SEGMENT_RPL_MASK;
3940 save->s = 1;
3941 }
3942 vmx_set_segment(vcpu, save, seg);
3943 }
3944
3945 static void enter_pmode(struct kvm_vcpu *vcpu)
3946 {
3947 unsigned long flags;
3948 struct vcpu_vmx *vmx = to_vmx(vcpu);
3949
3950 /*
3951 * Update real mode segment cache. It may be not up-to-date if sement
3952 * register was written while vcpu was in a guest mode.
3953 */
3954 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_ES], VCPU_SREG_ES);
3955 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_DS], VCPU_SREG_DS);
3956 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_FS], VCPU_SREG_FS);
3957 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_GS], VCPU_SREG_GS);
3958 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_SS], VCPU_SREG_SS);
3959 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_CS], VCPU_SREG_CS);
3960
3961 vmx->rmode.vm86_active = 0;
3962
3963 vmx_segment_cache_clear(vmx);
3964
3965 vmx_set_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_TR], VCPU_SREG_TR);
3966
3967 flags = vmcs_readl(GUEST_RFLAGS);
3968 flags &= RMODE_GUEST_OWNED_EFLAGS_BITS;
3969 flags |= vmx->rmode.save_rflags & ~RMODE_GUEST_OWNED_EFLAGS_BITS;
3970 vmcs_writel(GUEST_RFLAGS, flags);
3971
3972 vmcs_writel(GUEST_CR4, (vmcs_readl(GUEST_CR4) & ~X86_CR4_VME) |
3973 (vmcs_readl(CR4_READ_SHADOW) & X86_CR4_VME));
3974
3975 update_exception_bitmap(vcpu);
3976
3977 fix_pmode_seg(vcpu, VCPU_SREG_CS, &vmx->rmode.segs[VCPU_SREG_CS]);
3978 fix_pmode_seg(vcpu, VCPU_SREG_SS, &vmx->rmode.segs[VCPU_SREG_SS]);
3979 fix_pmode_seg(vcpu, VCPU_SREG_ES, &vmx->rmode.segs[VCPU_SREG_ES]);
3980 fix_pmode_seg(vcpu, VCPU_SREG_DS, &vmx->rmode.segs[VCPU_SREG_DS]);
3981 fix_pmode_seg(vcpu, VCPU_SREG_FS, &vmx->rmode.segs[VCPU_SREG_FS]);
3982 fix_pmode_seg(vcpu, VCPU_SREG_GS, &vmx->rmode.segs[VCPU_SREG_GS]);
3983 }
3984
3985 static void fix_rmode_seg(int seg, struct kvm_segment *save)
3986 {
3987 const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
3988 struct kvm_segment var = *save;
3989
3990 var.dpl = 0x3;
3991 if (seg == VCPU_SREG_CS)
3992 var.type = 0x3;
3993
3994 if (!emulate_invalid_guest_state) {
3995 var.selector = var.base >> 4;
3996 var.base = var.base & 0xffff0;
3997 var.limit = 0xffff;
3998 var.g = 0;
3999 var.db = 0;
4000 var.present = 1;
4001 var.s = 1;
4002 var.l = 0;
4003 var.unusable = 0;
4004 var.type = 0x3;
4005 var.avl = 0;
4006 if (save->base & 0xf)
4007 printk_once(KERN_WARNING "kvm: segment base is not "
4008 "paragraph aligned when entering "
4009 "protected mode (seg=%d)", seg);
4010 }
4011
4012 vmcs_write16(sf->selector, var.selector);
4013 vmcs_writel(sf->base, var.base);
4014 vmcs_write32(sf->limit, var.limit);
4015 vmcs_write32(sf->ar_bytes, vmx_segment_access_rights(&var));
4016 }
4017
4018 static void enter_rmode(struct kvm_vcpu *vcpu)
4019 {
4020 unsigned long flags;
4021 struct vcpu_vmx *vmx = to_vmx(vcpu);
4022
4023 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_TR], VCPU_SREG_TR);
4024 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_ES], VCPU_SREG_ES);
4025 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_DS], VCPU_SREG_DS);
4026 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_FS], VCPU_SREG_FS);
4027 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_GS], VCPU_SREG_GS);
4028 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_SS], VCPU_SREG_SS);
4029 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_CS], VCPU_SREG_CS);
4030
4031 vmx->rmode.vm86_active = 1;
4032
4033 /*
4034 * Very old userspace does not call KVM_SET_TSS_ADDR before entering
4035 * vcpu. Warn the user that an update is overdue.
4036 */
4037 if (!vcpu->kvm->arch.tss_addr)
4038 printk_once(KERN_WARNING "kvm: KVM_SET_TSS_ADDR need to be "
4039 "called before entering vcpu\n");
4040
4041 vmx_segment_cache_clear(vmx);
4042
4043 vmcs_writel(GUEST_TR_BASE, vcpu->kvm->arch.tss_addr);
4044 vmcs_write32(GUEST_TR_LIMIT, RMODE_TSS_SIZE - 1);
4045 vmcs_write32(GUEST_TR_AR_BYTES, 0x008b);
4046
4047 flags = vmcs_readl(GUEST_RFLAGS);
4048 vmx->rmode.save_rflags = flags;
4049
4050 flags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM;
4051
4052 vmcs_writel(GUEST_RFLAGS, flags);
4053 vmcs_writel(GUEST_CR4, vmcs_readl(GUEST_CR4) | X86_CR4_VME);
4054 update_exception_bitmap(vcpu);
4055
4056 fix_rmode_seg(VCPU_SREG_SS, &vmx->rmode.segs[VCPU_SREG_SS]);
4057 fix_rmode_seg(VCPU_SREG_CS, &vmx->rmode.segs[VCPU_SREG_CS]);
4058 fix_rmode_seg(VCPU_SREG_ES, &vmx->rmode.segs[VCPU_SREG_ES]);
4059 fix_rmode_seg(VCPU_SREG_DS, &vmx->rmode.segs[VCPU_SREG_DS]);
4060 fix_rmode_seg(VCPU_SREG_GS, &vmx->rmode.segs[VCPU_SREG_GS]);
4061 fix_rmode_seg(VCPU_SREG_FS, &vmx->rmode.segs[VCPU_SREG_FS]);
4062
4063 kvm_mmu_reset_context(vcpu);
4064 }
4065
4066 static void vmx_set_efer(struct kvm_vcpu *vcpu, u64 efer)
4067 {
4068 struct vcpu_vmx *vmx = to_vmx(vcpu);
4069 struct shared_msr_entry *msr = find_msr_entry(vmx, MSR_EFER);
4070
4071 if (!msr)
4072 return;
4073
4074 /*
4075 * Force kernel_gs_base reloading before EFER changes, as control
4076 * of this msr depends on is_long_mode().
4077 */
4078 vmx_load_host_state(to_vmx(vcpu));
4079 vcpu->arch.efer = efer;
4080 if (efer & EFER_LMA) {
4081 vm_entry_controls_setbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
4082 msr->data = efer;
4083 } else {
4084 vm_entry_controls_clearbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
4085
4086 msr->data = efer & ~EFER_LME;
4087 }
4088 setup_msrs(vmx);
4089 }
4090
4091 #ifdef CONFIG_X86_64
4092
4093 static void enter_lmode(struct kvm_vcpu *vcpu)
4094 {
4095 u32 guest_tr_ar;
4096
4097 vmx_segment_cache_clear(to_vmx(vcpu));
4098
4099 guest_tr_ar = vmcs_read32(GUEST_TR_AR_BYTES);
4100 if ((guest_tr_ar & VMX_AR_TYPE_MASK) != VMX_AR_TYPE_BUSY_64_TSS) {
4101 pr_debug_ratelimited("%s: tss fixup for long mode. \n",
4102 __func__);
4103 vmcs_write32(GUEST_TR_AR_BYTES,
4104 (guest_tr_ar & ~VMX_AR_TYPE_MASK)
4105 | VMX_AR_TYPE_BUSY_64_TSS);
4106 }
4107 vmx_set_efer(vcpu, vcpu->arch.efer | EFER_LMA);
4108 }
4109
4110 static void exit_lmode(struct kvm_vcpu *vcpu)
4111 {
4112 vm_entry_controls_clearbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
4113 vmx_set_efer(vcpu, vcpu->arch.efer & ~EFER_LMA);
4114 }
4115
4116 #endif
4117
4118 static inline void __vmx_flush_tlb(struct kvm_vcpu *vcpu, int vpid)
4119 {
4120 if (enable_ept) {
4121 if (!VALID_PAGE(vcpu->arch.mmu.root_hpa))
4122 return;
4123 ept_sync_context(construct_eptp(vcpu, vcpu->arch.mmu.root_hpa));
4124 } else {
4125 vpid_sync_context(vpid);
4126 }
4127 }
4128
4129 static void vmx_flush_tlb(struct kvm_vcpu *vcpu)
4130 {
4131 __vmx_flush_tlb(vcpu, to_vmx(vcpu)->vpid);
4132 }
4133
4134 static void vmx_flush_tlb_ept_only(struct kvm_vcpu *vcpu)
4135 {
4136 if (enable_ept)
4137 vmx_flush_tlb(vcpu);
4138 }
4139
4140 static void vmx_decache_cr0_guest_bits(struct kvm_vcpu *vcpu)
4141 {
4142 ulong cr0_guest_owned_bits = vcpu->arch.cr0_guest_owned_bits;
4143
4144 vcpu->arch.cr0 &= ~cr0_guest_owned_bits;
4145 vcpu->arch.cr0 |= vmcs_readl(GUEST_CR0) & cr0_guest_owned_bits;
4146 }
4147
4148 static void vmx_decache_cr3(struct kvm_vcpu *vcpu)
4149 {
4150 if (enable_ept && is_paging(vcpu))
4151 vcpu->arch.cr3 = vmcs_readl(GUEST_CR3);
4152 __set_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail);
4153 }
4154
4155 static void vmx_decache_cr4_guest_bits(struct kvm_vcpu *vcpu)
4156 {
4157 ulong cr4_guest_owned_bits = vcpu->arch.cr4_guest_owned_bits;
4158
4159 vcpu->arch.cr4 &= ~cr4_guest_owned_bits;
4160 vcpu->arch.cr4 |= vmcs_readl(GUEST_CR4) & cr4_guest_owned_bits;
4161 }
4162
4163 static void ept_load_pdptrs(struct kvm_vcpu *vcpu)
4164 {
4165 struct kvm_mmu *mmu = vcpu->arch.walk_mmu;
4166
4167 if (!test_bit(VCPU_EXREG_PDPTR,
4168 (unsigned long *)&vcpu->arch.regs_dirty))
4169 return;
4170
4171 if (is_paging(vcpu) && is_pae(vcpu) && !is_long_mode(vcpu)) {
4172 vmcs_write64(GUEST_PDPTR0, mmu->pdptrs[0]);
4173 vmcs_write64(GUEST_PDPTR1, mmu->pdptrs[1]);
4174 vmcs_write64(GUEST_PDPTR2, mmu->pdptrs[2]);
4175 vmcs_write64(GUEST_PDPTR3, mmu->pdptrs[3]);
4176 }
4177 }
4178
4179 static void ept_save_pdptrs(struct kvm_vcpu *vcpu)
4180 {
4181 struct kvm_mmu *mmu = vcpu->arch.walk_mmu;
4182
4183 if (is_paging(vcpu) && is_pae(vcpu) && !is_long_mode(vcpu)) {
4184 mmu->pdptrs[0] = vmcs_read64(GUEST_PDPTR0);
4185 mmu->pdptrs[1] = vmcs_read64(GUEST_PDPTR1);
4186 mmu->pdptrs[2] = vmcs_read64(GUEST_PDPTR2);
4187 mmu->pdptrs[3] = vmcs_read64(GUEST_PDPTR3);
4188 }
4189
4190 __set_bit(VCPU_EXREG_PDPTR,
4191 (unsigned long *)&vcpu->arch.regs_avail);
4192 __set_bit(VCPU_EXREG_PDPTR,
4193 (unsigned long *)&vcpu->arch.regs_dirty);
4194 }
4195
4196 static bool nested_guest_cr0_valid(struct kvm_vcpu *vcpu, unsigned long val)
4197 {
4198 u64 fixed0 = to_vmx(vcpu)->nested.nested_vmx_cr0_fixed0;
4199 u64 fixed1 = to_vmx(vcpu)->nested.nested_vmx_cr0_fixed1;
4200 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
4201
4202 if (to_vmx(vcpu)->nested.nested_vmx_secondary_ctls_high &
4203 SECONDARY_EXEC_UNRESTRICTED_GUEST &&
4204 nested_cpu_has2(vmcs12, SECONDARY_EXEC_UNRESTRICTED_GUEST))
4205 fixed0 &= ~(X86_CR0_PE | X86_CR0_PG);
4206
4207 return fixed_bits_valid(val, fixed0, fixed1);
4208 }
4209
4210 static bool nested_host_cr0_valid(struct kvm_vcpu *vcpu, unsigned long val)
4211 {
4212 u64 fixed0 = to_vmx(vcpu)->nested.nested_vmx_cr0_fixed0;
4213 u64 fixed1 = to_vmx(vcpu)->nested.nested_vmx_cr0_fixed1;
4214
4215 return fixed_bits_valid(val, fixed0, fixed1);
4216 }
4217
4218 static bool nested_cr4_valid(struct kvm_vcpu *vcpu, unsigned long val)
4219 {
4220 u64 fixed0 = to_vmx(vcpu)->nested.nested_vmx_cr4_fixed0;
4221 u64 fixed1 = to_vmx(vcpu)->nested.nested_vmx_cr4_fixed1;
4222
4223 return fixed_bits_valid(val, fixed0, fixed1);
4224 }
4225
4226 /* No difference in the restrictions on guest and host CR4 in VMX operation. */
4227 #define nested_guest_cr4_valid nested_cr4_valid
4228 #define nested_host_cr4_valid nested_cr4_valid
4229
4230 static int vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4);
4231
4232 static void ept_update_paging_mode_cr0(unsigned long *hw_cr0,
4233 unsigned long cr0,
4234 struct kvm_vcpu *vcpu)
4235 {
4236 if (!test_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail))
4237 vmx_decache_cr3(vcpu);
4238 if (!(cr0 & X86_CR0_PG)) {
4239 /* From paging/starting to nonpaging */
4240 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL,
4241 vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) |
4242 (CPU_BASED_CR3_LOAD_EXITING |
4243 CPU_BASED_CR3_STORE_EXITING));
4244 vcpu->arch.cr0 = cr0;
4245 vmx_set_cr4(vcpu, kvm_read_cr4(vcpu));
4246 } else if (!is_paging(vcpu)) {
4247 /* From nonpaging to paging */
4248 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL,
4249 vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) &
4250 ~(CPU_BASED_CR3_LOAD_EXITING |
4251 CPU_BASED_CR3_STORE_EXITING));
4252 vcpu->arch.cr0 = cr0;
4253 vmx_set_cr4(vcpu, kvm_read_cr4(vcpu));
4254 }
4255
4256 if (!(cr0 & X86_CR0_WP))
4257 *hw_cr0 &= ~X86_CR0_WP;
4258 }
4259
4260 static void vmx_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
4261 {
4262 struct vcpu_vmx *vmx = to_vmx(vcpu);
4263 unsigned long hw_cr0;
4264
4265 hw_cr0 = (cr0 & ~KVM_GUEST_CR0_MASK);
4266 if (enable_unrestricted_guest)
4267 hw_cr0 |= KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST;
4268 else {
4269 hw_cr0 |= KVM_VM_CR0_ALWAYS_ON;
4270
4271 if (vmx->rmode.vm86_active && (cr0 & X86_CR0_PE))
4272 enter_pmode(vcpu);
4273
4274 if (!vmx->rmode.vm86_active && !(cr0 & X86_CR0_PE))
4275 enter_rmode(vcpu);
4276 }
4277
4278 #ifdef CONFIG_X86_64
4279 if (vcpu->arch.efer & EFER_LME) {
4280 if (!is_paging(vcpu) && (cr0 & X86_CR0_PG))
4281 enter_lmode(vcpu);
4282 if (is_paging(vcpu) && !(cr0 & X86_CR0_PG))
4283 exit_lmode(vcpu);
4284 }
4285 #endif
4286
4287 if (enable_ept)
4288 ept_update_paging_mode_cr0(&hw_cr0, cr0, vcpu);
4289
4290 vmcs_writel(CR0_READ_SHADOW, cr0);
4291 vmcs_writel(GUEST_CR0, hw_cr0);
4292 vcpu->arch.cr0 = cr0;
4293
4294 /* depends on vcpu->arch.cr0 to be set to a new value */
4295 vmx->emulation_required = emulation_required(vcpu);
4296 }
4297
4298 static u64 construct_eptp(struct kvm_vcpu *vcpu, unsigned long root_hpa)
4299 {
4300 u64 eptp;
4301
4302 /* TODO write the value reading from MSR */
4303 eptp = VMX_EPT_DEFAULT_MT |
4304 VMX_EPT_DEFAULT_GAW << VMX_EPT_GAW_EPTP_SHIFT;
4305 if (enable_ept_ad_bits &&
4306 (!is_guest_mode(vcpu) || nested_ept_ad_enabled(vcpu)))
4307 eptp |= VMX_EPT_AD_ENABLE_BIT;
4308 eptp |= (root_hpa & PAGE_MASK);
4309
4310 return eptp;
4311 }
4312
4313 static void vmx_set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3)
4314 {
4315 unsigned long guest_cr3;
4316 u64 eptp;
4317
4318 guest_cr3 = cr3;
4319 if (enable_ept) {
4320 eptp = construct_eptp(vcpu, cr3);
4321 vmcs_write64(EPT_POINTER, eptp);
4322 if (is_paging(vcpu) || is_guest_mode(vcpu))
4323 guest_cr3 = kvm_read_cr3(vcpu);
4324 else
4325 guest_cr3 = vcpu->kvm->arch.ept_identity_map_addr;
4326 ept_load_pdptrs(vcpu);
4327 }
4328
4329 vmx_flush_tlb(vcpu);
4330 vmcs_writel(GUEST_CR3, guest_cr3);
4331 }
4332
4333 static int vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
4334 {
4335 /*
4336 * Pass through host's Machine Check Enable value to hw_cr4, which
4337 * is in force while we are in guest mode. Do not let guests control
4338 * this bit, even if host CR4.MCE == 0.
4339 */
4340 unsigned long hw_cr4 =
4341 (cr4_read_shadow() & X86_CR4_MCE) |
4342 (cr4 & ~X86_CR4_MCE) |
4343 (to_vmx(vcpu)->rmode.vm86_active ?
4344 KVM_RMODE_VM_CR4_ALWAYS_ON : KVM_PMODE_VM_CR4_ALWAYS_ON);
4345
4346 if (cr4 & X86_CR4_VMXE) {
4347 /*
4348 * To use VMXON (and later other VMX instructions), a guest
4349 * must first be able to turn on cr4.VMXE (see handle_vmon()).
4350 * So basically the check on whether to allow nested VMX
4351 * is here.
4352 */
4353 if (!nested_vmx_allowed(vcpu))
4354 return 1;
4355 }
4356
4357 if (to_vmx(vcpu)->nested.vmxon && !nested_cr4_valid(vcpu, cr4))
4358 return 1;
4359
4360 vcpu->arch.cr4 = cr4;
4361 if (enable_ept) {
4362 if (!is_paging(vcpu)) {
4363 hw_cr4 &= ~X86_CR4_PAE;
4364 hw_cr4 |= X86_CR4_PSE;
4365 } else if (!(cr4 & X86_CR4_PAE)) {
4366 hw_cr4 &= ~X86_CR4_PAE;
4367 }
4368 }
4369
4370 if (!enable_unrestricted_guest && !is_paging(vcpu))
4371 /*
4372 * SMEP/SMAP/PKU is disabled if CPU is in non-paging mode in
4373 * hardware. To emulate this behavior, SMEP/SMAP/PKU needs
4374 * to be manually disabled when guest switches to non-paging
4375 * mode.
4376 *
4377 * If !enable_unrestricted_guest, the CPU is always running
4378 * with CR0.PG=1 and CR4 needs to be modified.
4379 * If enable_unrestricted_guest, the CPU automatically
4380 * disables SMEP/SMAP/PKU when the guest sets CR0.PG=0.
4381 */
4382 hw_cr4 &= ~(X86_CR4_SMEP | X86_CR4_SMAP | X86_CR4_PKE);
4383
4384 vmcs_writel(CR4_READ_SHADOW, cr4);
4385 vmcs_writel(GUEST_CR4, hw_cr4);
4386 return 0;
4387 }
4388
4389 static void vmx_get_segment(struct kvm_vcpu *vcpu,
4390 struct kvm_segment *var, int seg)
4391 {
4392 struct vcpu_vmx *vmx = to_vmx(vcpu);
4393 u32 ar;
4394
4395 if (vmx->rmode.vm86_active && seg != VCPU_SREG_LDTR) {
4396 *var = vmx->rmode.segs[seg];
4397 if (seg == VCPU_SREG_TR
4398 || var->selector == vmx_read_guest_seg_selector(vmx, seg))
4399 return;
4400 var->base = vmx_read_guest_seg_base(vmx, seg);
4401 var->selector = vmx_read_guest_seg_selector(vmx, seg);
4402 return;
4403 }
4404 var->base = vmx_read_guest_seg_base(vmx, seg);
4405 var->limit = vmx_read_guest_seg_limit(vmx, seg);
4406 var->selector = vmx_read_guest_seg_selector(vmx, seg);
4407 ar = vmx_read_guest_seg_ar(vmx, seg);
4408 var->unusable = (ar >> 16) & 1;
4409 var->type = ar & 15;
4410 var->s = (ar >> 4) & 1;
4411 var->dpl = (ar >> 5) & 3;
4412 /*
4413 * Some userspaces do not preserve unusable property. Since usable
4414 * segment has to be present according to VMX spec we can use present
4415 * property to amend userspace bug by making unusable segment always
4416 * nonpresent. vmx_segment_access_rights() already marks nonpresent
4417 * segment as unusable.
4418 */
4419 var->present = !var->unusable;
4420 var->avl = (ar >> 12) & 1;
4421 var->l = (ar >> 13) & 1;
4422 var->db = (ar >> 14) & 1;
4423 var->g = (ar >> 15) & 1;
4424 }
4425
4426 static u64 vmx_get_segment_base(struct kvm_vcpu *vcpu, int seg)
4427 {
4428 struct kvm_segment s;
4429
4430 if (to_vmx(vcpu)->rmode.vm86_active) {
4431 vmx_get_segment(vcpu, &s, seg);
4432 return s.base;
4433 }
4434 return vmx_read_guest_seg_base(to_vmx(vcpu), seg);
4435 }
4436
4437 static int vmx_get_cpl(struct kvm_vcpu *vcpu)
4438 {
4439 struct vcpu_vmx *vmx = to_vmx(vcpu);
4440
4441 if (unlikely(vmx->rmode.vm86_active))
4442 return 0;
4443 else {
4444 int ar = vmx_read_guest_seg_ar(vmx, VCPU_SREG_SS);
4445 return VMX_AR_DPL(ar);
4446 }
4447 }
4448
4449 static u32 vmx_segment_access_rights(struct kvm_segment *var)
4450 {
4451 u32 ar;
4452
4453 if (var->unusable || !var->present)
4454 ar = 1 << 16;
4455 else {
4456 ar = var->type & 15;
4457 ar |= (var->s & 1) << 4;
4458 ar |= (var->dpl & 3) << 5;
4459 ar |= (var->present & 1) << 7;
4460 ar |= (var->avl & 1) << 12;
4461 ar |= (var->l & 1) << 13;
4462 ar |= (var->db & 1) << 14;
4463 ar |= (var->g & 1) << 15;
4464 }
4465
4466 return ar;
4467 }
4468
4469 static void vmx_set_segment(struct kvm_vcpu *vcpu,
4470 struct kvm_segment *var, int seg)
4471 {
4472 struct vcpu_vmx *vmx = to_vmx(vcpu);
4473 const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
4474
4475 vmx_segment_cache_clear(vmx);
4476
4477 if (vmx->rmode.vm86_active && seg != VCPU_SREG_LDTR) {
4478 vmx->rmode.segs[seg] = *var;
4479 if (seg == VCPU_SREG_TR)
4480 vmcs_write16(sf->selector, var->selector);
4481 else if (var->s)
4482 fix_rmode_seg(seg, &vmx->rmode.segs[seg]);
4483 goto out;
4484 }
4485
4486 vmcs_writel(sf->base, var->base);
4487 vmcs_write32(sf->limit, var->limit);
4488 vmcs_write16(sf->selector, var->selector);
4489
4490 /*
4491 * Fix the "Accessed" bit in AR field of segment registers for older
4492 * qemu binaries.
4493 * IA32 arch specifies that at the time of processor reset the
4494 * "Accessed" bit in the AR field of segment registers is 1. And qemu
4495 * is setting it to 0 in the userland code. This causes invalid guest
4496 * state vmexit when "unrestricted guest" mode is turned on.
4497 * Fix for this setup issue in cpu_reset is being pushed in the qemu
4498 * tree. Newer qemu binaries with that qemu fix would not need this
4499 * kvm hack.
4500 */
4501 if (enable_unrestricted_guest && (seg != VCPU_SREG_LDTR))
4502 var->type |= 0x1; /* Accessed */
4503
4504 vmcs_write32(sf->ar_bytes, vmx_segment_access_rights(var));
4505
4506 out:
4507 vmx->emulation_required = emulation_required(vcpu);
4508 }
4509
4510 static void vmx_get_cs_db_l_bits(struct kvm_vcpu *vcpu, int *db, int *l)
4511 {
4512 u32 ar = vmx_read_guest_seg_ar(to_vmx(vcpu), VCPU_SREG_CS);
4513
4514 *db = (ar >> 14) & 1;
4515 *l = (ar >> 13) & 1;
4516 }
4517
4518 static void vmx_get_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
4519 {
4520 dt->size = vmcs_read32(GUEST_IDTR_LIMIT);
4521 dt->address = vmcs_readl(GUEST_IDTR_BASE);
4522 }
4523
4524 static void vmx_set_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
4525 {
4526 vmcs_write32(GUEST_IDTR_LIMIT, dt->size);
4527 vmcs_writel(GUEST_IDTR_BASE, dt->address);
4528 }
4529
4530 static void vmx_get_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
4531 {
4532 dt->size = vmcs_read32(GUEST_GDTR_LIMIT);
4533 dt->address = vmcs_readl(GUEST_GDTR_BASE);
4534 }
4535
4536 static void vmx_set_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
4537 {
4538 vmcs_write32(GUEST_GDTR_LIMIT, dt->size);
4539 vmcs_writel(GUEST_GDTR_BASE, dt->address);
4540 }
4541
4542 static bool rmode_segment_valid(struct kvm_vcpu *vcpu, int seg)
4543 {
4544 struct kvm_segment var;
4545 u32 ar;
4546
4547 vmx_get_segment(vcpu, &var, seg);
4548 var.dpl = 0x3;
4549 if (seg == VCPU_SREG_CS)
4550 var.type = 0x3;
4551 ar = vmx_segment_access_rights(&var);
4552
4553 if (var.base != (var.selector << 4))
4554 return false;
4555 if (var.limit != 0xffff)
4556 return false;
4557 if (ar != 0xf3)
4558 return false;
4559
4560 return true;
4561 }
4562
4563 static bool code_segment_valid(struct kvm_vcpu *vcpu)
4564 {
4565 struct kvm_segment cs;
4566 unsigned int cs_rpl;
4567
4568 vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
4569 cs_rpl = cs.selector & SEGMENT_RPL_MASK;
4570
4571 if (cs.unusable)
4572 return false;
4573 if (~cs.type & (VMX_AR_TYPE_CODE_MASK|VMX_AR_TYPE_ACCESSES_MASK))
4574 return false;
4575 if (!cs.s)
4576 return false;
4577 if (cs.type & VMX_AR_TYPE_WRITEABLE_MASK) {
4578 if (cs.dpl > cs_rpl)
4579 return false;
4580 } else {
4581 if (cs.dpl != cs_rpl)
4582 return false;
4583 }
4584 if (!cs.present)
4585 return false;
4586
4587 /* TODO: Add Reserved field check, this'll require a new member in the kvm_segment_field structure */
4588 return true;
4589 }
4590
4591 static bool stack_segment_valid(struct kvm_vcpu *vcpu)
4592 {
4593 struct kvm_segment ss;
4594 unsigned int ss_rpl;
4595
4596 vmx_get_segment(vcpu, &ss, VCPU_SREG_SS);
4597 ss_rpl = ss.selector & SEGMENT_RPL_MASK;
4598
4599 if (ss.unusable)
4600 return true;
4601 if (ss.type != 3 && ss.type != 7)
4602 return false;
4603 if (!ss.s)
4604 return false;
4605 if (ss.dpl != ss_rpl) /* DPL != RPL */
4606 return false;
4607 if (!ss.present)
4608 return false;
4609
4610 return true;
4611 }
4612
4613 static bool data_segment_valid(struct kvm_vcpu *vcpu, int seg)
4614 {
4615 struct kvm_segment var;
4616 unsigned int rpl;
4617
4618 vmx_get_segment(vcpu, &var, seg);
4619 rpl = var.selector & SEGMENT_RPL_MASK;
4620
4621 if (var.unusable)
4622 return true;
4623 if (!var.s)
4624 return false;
4625 if (!var.present)
4626 return false;
4627 if (~var.type & (VMX_AR_TYPE_CODE_MASK|VMX_AR_TYPE_WRITEABLE_MASK)) {
4628 if (var.dpl < rpl) /* DPL < RPL */
4629 return false;
4630 }
4631
4632 /* TODO: Add other members to kvm_segment_field to allow checking for other access
4633 * rights flags
4634 */
4635 return true;
4636 }
4637
4638 static bool tr_valid(struct kvm_vcpu *vcpu)
4639 {
4640 struct kvm_segment tr;
4641
4642 vmx_get_segment(vcpu, &tr, VCPU_SREG_TR);
4643
4644 if (tr.unusable)
4645 return false;
4646 if (tr.selector & SEGMENT_TI_MASK) /* TI = 1 */
4647 return false;
4648 if (tr.type != 3 && tr.type != 11) /* TODO: Check if guest is in IA32e mode */
4649 return false;
4650 if (!tr.present)
4651 return false;
4652
4653 return true;
4654 }
4655
4656 static bool ldtr_valid(struct kvm_vcpu *vcpu)
4657 {
4658 struct kvm_segment ldtr;
4659
4660 vmx_get_segment(vcpu, &ldtr, VCPU_SREG_LDTR);
4661
4662 if (ldtr.unusable)
4663 return true;
4664 if (ldtr.selector & SEGMENT_TI_MASK) /* TI = 1 */
4665 return false;
4666 if (ldtr.type != 2)
4667 return false;
4668 if (!ldtr.present)
4669 return false;
4670
4671 return true;
4672 }
4673
4674 static bool cs_ss_rpl_check(struct kvm_vcpu *vcpu)
4675 {
4676 struct kvm_segment cs, ss;
4677
4678 vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
4679 vmx_get_segment(vcpu, &ss, VCPU_SREG_SS);
4680
4681 return ((cs.selector & SEGMENT_RPL_MASK) ==
4682 (ss.selector & SEGMENT_RPL_MASK));
4683 }
4684
4685 /*
4686 * Check if guest state is valid. Returns true if valid, false if
4687 * not.
4688 * We assume that registers are always usable
4689 */
4690 static bool guest_state_valid(struct kvm_vcpu *vcpu)
4691 {
4692 if (enable_unrestricted_guest)
4693 return true;
4694
4695 /* real mode guest state checks */
4696 if (!is_protmode(vcpu) || (vmx_get_rflags(vcpu) & X86_EFLAGS_VM)) {
4697 if (!rmode_segment_valid(vcpu, VCPU_SREG_CS))
4698 return false;
4699 if (!rmode_segment_valid(vcpu, VCPU_SREG_SS))
4700 return false;
4701 if (!rmode_segment_valid(vcpu, VCPU_SREG_DS))
4702 return false;
4703 if (!rmode_segment_valid(vcpu, VCPU_SREG_ES))
4704 return false;
4705 if (!rmode_segment_valid(vcpu, VCPU_SREG_FS))
4706 return false;
4707 if (!rmode_segment_valid(vcpu, VCPU_SREG_GS))
4708 return false;
4709 } else {
4710 /* protected mode guest state checks */
4711 if (!cs_ss_rpl_check(vcpu))
4712 return false;
4713 if (!code_segment_valid(vcpu))
4714 return false;
4715 if (!stack_segment_valid(vcpu))
4716 return false;
4717 if (!data_segment_valid(vcpu, VCPU_SREG_DS))
4718 return false;
4719 if (!data_segment_valid(vcpu, VCPU_SREG_ES))
4720 return false;
4721 if (!data_segment_valid(vcpu, VCPU_SREG_FS))
4722 return false;
4723 if (!data_segment_valid(vcpu, VCPU_SREG_GS))
4724 return false;
4725 if (!tr_valid(vcpu))
4726 return false;
4727 if (!ldtr_valid(vcpu))
4728 return false;
4729 }
4730 /* TODO:
4731 * - Add checks on RIP
4732 * - Add checks on RFLAGS
4733 */
4734
4735 return true;
4736 }
4737
4738 static bool page_address_valid(struct kvm_vcpu *vcpu, gpa_t gpa)
4739 {
4740 return PAGE_ALIGNED(gpa) && !(gpa >> cpuid_maxphyaddr(vcpu));
4741 }
4742
4743 static int init_rmode_tss(struct kvm *kvm)
4744 {
4745 gfn_t fn;
4746 u16 data = 0;
4747 int idx, r;
4748
4749 idx = srcu_read_lock(&kvm->srcu);
4750 fn = kvm->arch.tss_addr >> PAGE_SHIFT;
4751 r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE);
4752 if (r < 0)
4753 goto out;
4754 data = TSS_BASE_SIZE + TSS_REDIRECTION_SIZE;
4755 r = kvm_write_guest_page(kvm, fn++, &data,
4756 TSS_IOPB_BASE_OFFSET, sizeof(u16));
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 r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE);
4763 if (r < 0)
4764 goto out;
4765 data = ~0;
4766 r = kvm_write_guest_page(kvm, fn, &data,
4767 RMODE_TSS_SIZE - 2 * PAGE_SIZE - 1,
4768 sizeof(u8));
4769 out:
4770 srcu_read_unlock(&kvm->srcu, idx);
4771 return r;
4772 }
4773
4774 static int init_rmode_identity_map(struct kvm *kvm)
4775 {
4776 int i, idx, r = 0;
4777 kvm_pfn_t identity_map_pfn;
4778 u32 tmp;
4779
4780 if (!enable_ept)
4781 return 0;
4782
4783 /* Protect kvm->arch.ept_identity_pagetable_done. */
4784 mutex_lock(&kvm->slots_lock);
4785
4786 if (likely(kvm->arch.ept_identity_pagetable_done))
4787 goto out2;
4788
4789 identity_map_pfn = kvm->arch.ept_identity_map_addr >> PAGE_SHIFT;
4790
4791 r = alloc_identity_pagetable(kvm);
4792 if (r < 0)
4793 goto out2;
4794
4795 idx = srcu_read_lock(&kvm->srcu);
4796 r = kvm_clear_guest_page(kvm, identity_map_pfn, 0, PAGE_SIZE);
4797 if (r < 0)
4798 goto out;
4799 /* Set up identity-mapping pagetable for EPT in real mode */
4800 for (i = 0; i < PT32_ENT_PER_PAGE; i++) {
4801 tmp = (i << 22) + (_PAGE_PRESENT | _PAGE_RW | _PAGE_USER |
4802 _PAGE_ACCESSED | _PAGE_DIRTY | _PAGE_PSE);
4803 r = kvm_write_guest_page(kvm, identity_map_pfn,
4804 &tmp, i * sizeof(tmp), sizeof(tmp));
4805 if (r < 0)
4806 goto out;
4807 }
4808 kvm->arch.ept_identity_pagetable_done = true;
4809
4810 out:
4811 srcu_read_unlock(&kvm->srcu, idx);
4812
4813 out2:
4814 mutex_unlock(&kvm->slots_lock);
4815 return r;
4816 }
4817
4818 static void seg_setup(int seg)
4819 {
4820 const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
4821 unsigned int ar;
4822
4823 vmcs_write16(sf->selector, 0);
4824 vmcs_writel(sf->base, 0);
4825 vmcs_write32(sf->limit, 0xffff);
4826 ar = 0x93;
4827 if (seg == VCPU_SREG_CS)
4828 ar |= 0x08; /* code segment */
4829
4830 vmcs_write32(sf->ar_bytes, ar);
4831 }
4832
4833 static int alloc_apic_access_page(struct kvm *kvm)
4834 {
4835 struct page *page;
4836 int r = 0;
4837
4838 mutex_lock(&kvm->slots_lock);
4839 if (kvm->arch.apic_access_page_done)
4840 goto out;
4841 r = __x86_set_memory_region(kvm, APIC_ACCESS_PAGE_PRIVATE_MEMSLOT,
4842 APIC_DEFAULT_PHYS_BASE, PAGE_SIZE);
4843 if (r)
4844 goto out;
4845
4846 page = gfn_to_page(kvm, APIC_DEFAULT_PHYS_BASE >> PAGE_SHIFT);
4847 if (is_error_page(page)) {
4848 r = -EFAULT;
4849 goto out;
4850 }
4851
4852 /*
4853 * Do not pin the page in memory, so that memory hot-unplug
4854 * is able to migrate it.
4855 */
4856 put_page(page);
4857 kvm->arch.apic_access_page_done = true;
4858 out:
4859 mutex_unlock(&kvm->slots_lock);
4860 return r;
4861 }
4862
4863 static int alloc_identity_pagetable(struct kvm *kvm)
4864 {
4865 /* Called with kvm->slots_lock held. */
4866
4867 int r = 0;
4868
4869 BUG_ON(kvm->arch.ept_identity_pagetable_done);
4870
4871 r = __x86_set_memory_region(kvm, IDENTITY_PAGETABLE_PRIVATE_MEMSLOT,
4872 kvm->arch.ept_identity_map_addr, PAGE_SIZE);
4873
4874 return r;
4875 }
4876
4877 static int allocate_vpid(void)
4878 {
4879 int vpid;
4880
4881 if (!enable_vpid)
4882 return 0;
4883 spin_lock(&vmx_vpid_lock);
4884 vpid = find_first_zero_bit(vmx_vpid_bitmap, VMX_NR_VPIDS);
4885 if (vpid < VMX_NR_VPIDS)
4886 __set_bit(vpid, vmx_vpid_bitmap);
4887 else
4888 vpid = 0;
4889 spin_unlock(&vmx_vpid_lock);
4890 return vpid;
4891 }
4892
4893 static void free_vpid(int vpid)
4894 {
4895 if (!enable_vpid || vpid == 0)
4896 return;
4897 spin_lock(&vmx_vpid_lock);
4898 __clear_bit(vpid, vmx_vpid_bitmap);
4899 spin_unlock(&vmx_vpid_lock);
4900 }
4901
4902 #define MSR_TYPE_R 1
4903 #define MSR_TYPE_W 2
4904 static void __vmx_disable_intercept_for_msr(unsigned long *msr_bitmap,
4905 u32 msr, int type)
4906 {
4907 int f = sizeof(unsigned long);
4908
4909 if (!cpu_has_vmx_msr_bitmap())
4910 return;
4911
4912 /*
4913 * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
4914 * have the write-low and read-high bitmap offsets the wrong way round.
4915 * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
4916 */
4917 if (msr <= 0x1fff) {
4918 if (type & MSR_TYPE_R)
4919 /* read-low */
4920 __clear_bit(msr, msr_bitmap + 0x000 / f);
4921
4922 if (type & MSR_TYPE_W)
4923 /* write-low */
4924 __clear_bit(msr, msr_bitmap + 0x800 / f);
4925
4926 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
4927 msr &= 0x1fff;
4928 if (type & MSR_TYPE_R)
4929 /* read-high */
4930 __clear_bit(msr, msr_bitmap + 0x400 / f);
4931
4932 if (type & MSR_TYPE_W)
4933 /* write-high */
4934 __clear_bit(msr, msr_bitmap + 0xc00 / f);
4935
4936 }
4937 }
4938
4939 /*
4940 * If a msr is allowed by L0, we should check whether it is allowed by L1.
4941 * The corresponding bit will be cleared unless both of L0 and L1 allow it.
4942 */
4943 static void nested_vmx_disable_intercept_for_msr(unsigned long *msr_bitmap_l1,
4944 unsigned long *msr_bitmap_nested,
4945 u32 msr, int type)
4946 {
4947 int f = sizeof(unsigned long);
4948
4949 if (!cpu_has_vmx_msr_bitmap()) {
4950 WARN_ON(1);
4951 return;
4952 }
4953
4954 /*
4955 * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
4956 * have the write-low and read-high bitmap offsets the wrong way round.
4957 * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
4958 */
4959 if (msr <= 0x1fff) {
4960 if (type & MSR_TYPE_R &&
4961 !test_bit(msr, msr_bitmap_l1 + 0x000 / f))
4962 /* read-low */
4963 __clear_bit(msr, msr_bitmap_nested + 0x000 / f);
4964
4965 if (type & MSR_TYPE_W &&
4966 !test_bit(msr, msr_bitmap_l1 + 0x800 / f))
4967 /* write-low */
4968 __clear_bit(msr, msr_bitmap_nested + 0x800 / f);
4969
4970 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
4971 msr &= 0x1fff;
4972 if (type & MSR_TYPE_R &&
4973 !test_bit(msr, msr_bitmap_l1 + 0x400 / f))
4974 /* read-high */
4975 __clear_bit(msr, msr_bitmap_nested + 0x400 / f);
4976
4977 if (type & MSR_TYPE_W &&
4978 !test_bit(msr, msr_bitmap_l1 + 0xc00 / f))
4979 /* write-high */
4980 __clear_bit(msr, msr_bitmap_nested + 0xc00 / f);
4981
4982 }
4983 }
4984
4985 static void vmx_disable_intercept_for_msr(u32 msr, bool longmode_only)
4986 {
4987 if (!longmode_only)
4988 __vmx_disable_intercept_for_msr(vmx_msr_bitmap_legacy,
4989 msr, MSR_TYPE_R | MSR_TYPE_W);
4990 __vmx_disable_intercept_for_msr(vmx_msr_bitmap_longmode,
4991 msr, MSR_TYPE_R | MSR_TYPE_W);
4992 }
4993
4994 static void vmx_disable_intercept_msr_x2apic(u32 msr, int type, bool apicv_active)
4995 {
4996 if (apicv_active) {
4997 __vmx_disable_intercept_for_msr(vmx_msr_bitmap_legacy_x2apic_apicv,
4998 msr, type);
4999 __vmx_disable_intercept_for_msr(vmx_msr_bitmap_longmode_x2apic_apicv,
5000 msr, type);
5001 } else {
5002 __vmx_disable_intercept_for_msr(vmx_msr_bitmap_legacy_x2apic,
5003 msr, type);
5004 __vmx_disable_intercept_for_msr(vmx_msr_bitmap_longmode_x2apic,
5005 msr, type);
5006 }
5007 }
5008
5009 static bool vmx_get_enable_apicv(void)
5010 {
5011 return enable_apicv;
5012 }
5013
5014 static void nested_mark_vmcs12_pages_dirty(struct kvm_vcpu *vcpu)
5015 {
5016 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
5017 gfn_t gfn;
5018
5019 /*
5020 * Don't need to mark the APIC access page dirty; it is never
5021 * written to by the CPU during APIC virtualization.
5022 */
5023
5024 if (nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW)) {
5025 gfn = vmcs12->virtual_apic_page_addr >> PAGE_SHIFT;
5026 kvm_vcpu_mark_page_dirty(vcpu, gfn);
5027 }
5028
5029 if (nested_cpu_has_posted_intr(vmcs12)) {
5030 gfn = vmcs12->posted_intr_desc_addr >> PAGE_SHIFT;
5031 kvm_vcpu_mark_page_dirty(vcpu, gfn);
5032 }
5033 }
5034
5035
5036 static void vmx_complete_nested_posted_interrupt(struct kvm_vcpu *vcpu)
5037 {
5038 struct vcpu_vmx *vmx = to_vmx(vcpu);
5039 int max_irr;
5040 void *vapic_page;
5041 u16 status;
5042
5043 if (!vmx->nested.pi_desc || !vmx->nested.pi_pending)
5044 return;
5045
5046 vmx->nested.pi_pending = false;
5047 if (!pi_test_and_clear_on(vmx->nested.pi_desc))
5048 return;
5049
5050 max_irr = find_last_bit((unsigned long *)vmx->nested.pi_desc->pir, 256);
5051 if (max_irr != 256) {
5052 vapic_page = kmap(vmx->nested.virtual_apic_page);
5053 __kvm_apic_update_irr(vmx->nested.pi_desc->pir, vapic_page);
5054 kunmap(vmx->nested.virtual_apic_page);
5055
5056 status = vmcs_read16(GUEST_INTR_STATUS);
5057 if ((u8)max_irr > ((u8)status & 0xff)) {
5058 status &= ~0xff;
5059 status |= (u8)max_irr;
5060 vmcs_write16(GUEST_INTR_STATUS, status);
5061 }
5062 }
5063
5064 nested_mark_vmcs12_pages_dirty(vcpu);
5065 }
5066
5067 static inline bool kvm_vcpu_trigger_posted_interrupt(struct kvm_vcpu *vcpu,
5068 bool nested)
5069 {
5070 #ifdef CONFIG_SMP
5071 int pi_vec = nested ? POSTED_INTR_NESTED_VECTOR : POSTED_INTR_VECTOR;
5072
5073 if (vcpu->mode == IN_GUEST_MODE) {
5074 /*
5075 * The vector of interrupt to be delivered to vcpu had
5076 * been set in PIR before this function.
5077 *
5078 * Following cases will be reached in this block, and
5079 * we always send a notification event in all cases as
5080 * explained below.
5081 *
5082 * Case 1: vcpu keeps in non-root mode. Sending a
5083 * notification event posts the interrupt to vcpu.
5084 *
5085 * Case 2: vcpu exits to root mode and is still
5086 * runnable. PIR will be synced to vIRR before the
5087 * next vcpu entry. Sending a notification event in
5088 * this case has no effect, as vcpu is not in root
5089 * mode.
5090 *
5091 * Case 3: vcpu exits to root mode and is blocked.
5092 * vcpu_block() has already synced PIR to vIRR and
5093 * never blocks vcpu if vIRR is not cleared. Therefore,
5094 * a blocked vcpu here does not wait for any requested
5095 * interrupts in PIR, and sending a notification event
5096 * which has no effect is safe here.
5097 */
5098
5099 apic->send_IPI_mask(get_cpu_mask(vcpu->cpu), pi_vec);
5100 return true;
5101 }
5102 #endif
5103 return false;
5104 }
5105
5106 static int vmx_deliver_nested_posted_interrupt(struct kvm_vcpu *vcpu,
5107 int vector)
5108 {
5109 struct vcpu_vmx *vmx = to_vmx(vcpu);
5110
5111 if (is_guest_mode(vcpu) &&
5112 vector == vmx->nested.posted_intr_nv) {
5113 /* the PIR and ON have been set by L1. */
5114 kvm_vcpu_trigger_posted_interrupt(vcpu, true);
5115 /*
5116 * If a posted intr is not recognized by hardware,
5117 * we will accomplish it in the next vmentry.
5118 */
5119 vmx->nested.pi_pending = true;
5120 kvm_make_request(KVM_REQ_EVENT, vcpu);
5121 return 0;
5122 }
5123 return -1;
5124 }
5125 /*
5126 * Send interrupt to vcpu via posted interrupt way.
5127 * 1. If target vcpu is running(non-root mode), send posted interrupt
5128 * notification to vcpu and hardware will sync PIR to vIRR atomically.
5129 * 2. If target vcpu isn't running(root mode), kick it to pick up the
5130 * interrupt from PIR in next vmentry.
5131 */
5132 static void vmx_deliver_posted_interrupt(struct kvm_vcpu *vcpu, int vector)
5133 {
5134 struct vcpu_vmx *vmx = to_vmx(vcpu);
5135 int r;
5136
5137 r = vmx_deliver_nested_posted_interrupt(vcpu, vector);
5138 if (!r)
5139 return;
5140
5141 if (pi_test_and_set_pir(vector, &vmx->pi_desc))
5142 return;
5143
5144 /* If a previous notification has sent the IPI, nothing to do. */
5145 if (pi_test_and_set_on(&vmx->pi_desc))
5146 return;
5147
5148 if (!kvm_vcpu_trigger_posted_interrupt(vcpu, false))
5149 kvm_vcpu_kick(vcpu);
5150 }
5151
5152 /*
5153 * Set up the vmcs's constant host-state fields, i.e., host-state fields that
5154 * will not change in the lifetime of the guest.
5155 * Note that host-state that does change is set elsewhere. E.g., host-state
5156 * that is set differently for each CPU is set in vmx_vcpu_load(), not here.
5157 */
5158 static void vmx_set_constant_host_state(struct vcpu_vmx *vmx)
5159 {
5160 u32 low32, high32;
5161 unsigned long tmpl;
5162 struct desc_ptr dt;
5163 unsigned long cr0, cr3, cr4;
5164
5165 cr0 = read_cr0();
5166 WARN_ON(cr0 & X86_CR0_TS);
5167 vmcs_writel(HOST_CR0, cr0); /* 22.2.3 */
5168
5169 /*
5170 * Save the most likely value for this task's CR3 in the VMCS.
5171 * We can't use __get_current_cr3_fast() because we're not atomic.
5172 */
5173 cr3 = __read_cr3();
5174 vmcs_writel(HOST_CR3, cr3); /* 22.2.3 FIXME: shadow tables */
5175 vmx->loaded_vmcs->vmcs_host_cr3 = cr3;
5176
5177 /* Save the most likely value for this task's CR4 in the VMCS. */
5178 cr4 = cr4_read_shadow();
5179 vmcs_writel(HOST_CR4, cr4); /* 22.2.3, 22.2.5 */
5180 vmx->loaded_vmcs->vmcs_host_cr4 = cr4;
5181
5182 vmcs_write16(HOST_CS_SELECTOR, __KERNEL_CS); /* 22.2.4 */
5183 #ifdef CONFIG_X86_64
5184 /*
5185 * Load null selectors, so we can avoid reloading them in
5186 * __vmx_load_host_state(), in case userspace uses the null selectors
5187 * too (the expected case).
5188 */
5189 vmcs_write16(HOST_DS_SELECTOR, 0);
5190 vmcs_write16(HOST_ES_SELECTOR, 0);
5191 #else
5192 vmcs_write16(HOST_DS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
5193 vmcs_write16(HOST_ES_SELECTOR, __KERNEL_DS); /* 22.2.4 */
5194 #endif
5195 vmcs_write16(HOST_SS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
5196 vmcs_write16(HOST_TR_SELECTOR, GDT_ENTRY_TSS*8); /* 22.2.4 */
5197
5198 store_idt(&dt);
5199 vmcs_writel(HOST_IDTR_BASE, dt.address); /* 22.2.4 */
5200 vmx->host_idt_base = dt.address;
5201
5202 vmcs_writel(HOST_RIP, vmx_return); /* 22.2.5 */
5203
5204 rdmsr(MSR_IA32_SYSENTER_CS, low32, high32);
5205 vmcs_write32(HOST_IA32_SYSENTER_CS, low32);
5206 rdmsrl(MSR_IA32_SYSENTER_EIP, tmpl);
5207 vmcs_writel(HOST_IA32_SYSENTER_EIP, tmpl); /* 22.2.3 */
5208
5209 if (vmcs_config.vmexit_ctrl & VM_EXIT_LOAD_IA32_PAT) {
5210 rdmsr(MSR_IA32_CR_PAT, low32, high32);
5211 vmcs_write64(HOST_IA32_PAT, low32 | ((u64) high32 << 32));
5212 }
5213 }
5214
5215 static void set_cr4_guest_host_mask(struct vcpu_vmx *vmx)
5216 {
5217 vmx->vcpu.arch.cr4_guest_owned_bits = KVM_CR4_GUEST_OWNED_BITS;
5218 if (enable_ept)
5219 vmx->vcpu.arch.cr4_guest_owned_bits |= X86_CR4_PGE;
5220 if (is_guest_mode(&vmx->vcpu))
5221 vmx->vcpu.arch.cr4_guest_owned_bits &=
5222 ~get_vmcs12(&vmx->vcpu)->cr4_guest_host_mask;
5223 vmcs_writel(CR4_GUEST_HOST_MASK, ~vmx->vcpu.arch.cr4_guest_owned_bits);
5224 }
5225
5226 static u32 vmx_pin_based_exec_ctrl(struct vcpu_vmx *vmx)
5227 {
5228 u32 pin_based_exec_ctrl = vmcs_config.pin_based_exec_ctrl;
5229
5230 if (!kvm_vcpu_apicv_active(&vmx->vcpu))
5231 pin_based_exec_ctrl &= ~PIN_BASED_POSTED_INTR;
5232 /* Enable the preemption timer dynamically */
5233 pin_based_exec_ctrl &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
5234 return pin_based_exec_ctrl;
5235 }
5236
5237 static void vmx_refresh_apicv_exec_ctrl(struct kvm_vcpu *vcpu)
5238 {
5239 struct vcpu_vmx *vmx = to_vmx(vcpu);
5240
5241 vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, vmx_pin_based_exec_ctrl(vmx));
5242 if (cpu_has_secondary_exec_ctrls()) {
5243 if (kvm_vcpu_apicv_active(vcpu))
5244 vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL,
5245 SECONDARY_EXEC_APIC_REGISTER_VIRT |
5246 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
5247 else
5248 vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL,
5249 SECONDARY_EXEC_APIC_REGISTER_VIRT |
5250 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
5251 }
5252
5253 if (cpu_has_vmx_msr_bitmap())
5254 vmx_set_msr_bitmap(vcpu);
5255 }
5256
5257 static u32 vmx_exec_control(struct vcpu_vmx *vmx)
5258 {
5259 u32 exec_control = vmcs_config.cpu_based_exec_ctrl;
5260
5261 if (vmx->vcpu.arch.switch_db_regs & KVM_DEBUGREG_WONT_EXIT)
5262 exec_control &= ~CPU_BASED_MOV_DR_EXITING;
5263
5264 if (!cpu_need_tpr_shadow(&vmx->vcpu)) {
5265 exec_control &= ~CPU_BASED_TPR_SHADOW;
5266 #ifdef CONFIG_X86_64
5267 exec_control |= CPU_BASED_CR8_STORE_EXITING |
5268 CPU_BASED_CR8_LOAD_EXITING;
5269 #endif
5270 }
5271 if (!enable_ept)
5272 exec_control |= CPU_BASED_CR3_STORE_EXITING |
5273 CPU_BASED_CR3_LOAD_EXITING |
5274 CPU_BASED_INVLPG_EXITING;
5275 return exec_control;
5276 }
5277
5278 static u32 vmx_secondary_exec_control(struct vcpu_vmx *vmx)
5279 {
5280 u32 exec_control = vmcs_config.cpu_based_2nd_exec_ctrl;
5281 if (!cpu_need_virtualize_apic_accesses(&vmx->vcpu))
5282 exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
5283 if (vmx->vpid == 0)
5284 exec_control &= ~SECONDARY_EXEC_ENABLE_VPID;
5285 if (!enable_ept) {
5286 exec_control &= ~SECONDARY_EXEC_ENABLE_EPT;
5287 enable_unrestricted_guest = 0;
5288 /* Enable INVPCID for non-ept guests may cause performance regression. */
5289 exec_control &= ~SECONDARY_EXEC_ENABLE_INVPCID;
5290 }
5291 if (!enable_unrestricted_guest)
5292 exec_control &= ~SECONDARY_EXEC_UNRESTRICTED_GUEST;
5293 if (!ple_gap)
5294 exec_control &= ~SECONDARY_EXEC_PAUSE_LOOP_EXITING;
5295 if (!kvm_vcpu_apicv_active(&vmx->vcpu))
5296 exec_control &= ~(SECONDARY_EXEC_APIC_REGISTER_VIRT |
5297 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
5298 exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
5299 /* SECONDARY_EXEC_SHADOW_VMCS is enabled when L1 executes VMPTRLD
5300 (handle_vmptrld).
5301 We can NOT enable shadow_vmcs here because we don't have yet
5302 a current VMCS12
5303 */
5304 exec_control &= ~SECONDARY_EXEC_SHADOW_VMCS;
5305
5306 if (!enable_pml)
5307 exec_control &= ~SECONDARY_EXEC_ENABLE_PML;
5308
5309 return exec_control;
5310 }
5311
5312 static void ept_set_mmio_spte_mask(void)
5313 {
5314 /*
5315 * EPT Misconfigurations can be generated if the value of bits 2:0
5316 * of an EPT paging-structure entry is 110b (write/execute).
5317 */
5318 kvm_mmu_set_mmio_spte_mask(VMX_EPT_RWX_MASK,
5319 VMX_EPT_MISCONFIG_WX_VALUE);
5320 }
5321
5322 #define VMX_XSS_EXIT_BITMAP 0
5323 /*
5324 * Sets up the vmcs for emulated real mode.
5325 */
5326 static int vmx_vcpu_setup(struct vcpu_vmx *vmx)
5327 {
5328 #ifdef CONFIG_X86_64
5329 unsigned long a;
5330 #endif
5331 int i;
5332
5333 /* I/O */
5334 vmcs_write64(IO_BITMAP_A, __pa(vmx_io_bitmap_a));
5335 vmcs_write64(IO_BITMAP_B, __pa(vmx_io_bitmap_b));
5336
5337 if (enable_shadow_vmcs) {
5338 vmcs_write64(VMREAD_BITMAP, __pa(vmx_vmread_bitmap));
5339 vmcs_write64(VMWRITE_BITMAP, __pa(vmx_vmwrite_bitmap));
5340 }
5341 if (cpu_has_vmx_msr_bitmap())
5342 vmcs_write64(MSR_BITMAP, __pa(vmx_msr_bitmap_legacy));
5343
5344 vmcs_write64(VMCS_LINK_POINTER, -1ull); /* 22.3.1.5 */
5345
5346 /* Control */
5347 vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, vmx_pin_based_exec_ctrl(vmx));
5348 vmx->hv_deadline_tsc = -1;
5349
5350 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, vmx_exec_control(vmx));
5351
5352 if (cpu_has_secondary_exec_ctrls()) {
5353 vmcs_write32(SECONDARY_VM_EXEC_CONTROL,
5354 vmx_secondary_exec_control(vmx));
5355 }
5356
5357 if (kvm_vcpu_apicv_active(&vmx->vcpu)) {
5358 vmcs_write64(EOI_EXIT_BITMAP0, 0);
5359 vmcs_write64(EOI_EXIT_BITMAP1, 0);
5360 vmcs_write64(EOI_EXIT_BITMAP2, 0);
5361 vmcs_write64(EOI_EXIT_BITMAP3, 0);
5362
5363 vmcs_write16(GUEST_INTR_STATUS, 0);
5364
5365 vmcs_write16(POSTED_INTR_NV, POSTED_INTR_VECTOR);
5366 vmcs_write64(POSTED_INTR_DESC_ADDR, __pa((&vmx->pi_desc)));
5367 }
5368
5369 if (ple_gap) {
5370 vmcs_write32(PLE_GAP, ple_gap);
5371 vmx->ple_window = ple_window;
5372 vmx->ple_window_dirty = true;
5373 }
5374
5375 vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK, 0);
5376 vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH, 0);
5377 vmcs_write32(CR3_TARGET_COUNT, 0); /* 22.2.1 */
5378
5379 vmcs_write16(HOST_FS_SELECTOR, 0); /* 22.2.4 */
5380 vmcs_write16(HOST_GS_SELECTOR, 0); /* 22.2.4 */
5381 vmx_set_constant_host_state(vmx);
5382 #ifdef CONFIG_X86_64
5383 rdmsrl(MSR_FS_BASE, a);
5384 vmcs_writel(HOST_FS_BASE, a); /* 22.2.4 */
5385 rdmsrl(MSR_GS_BASE, a);
5386 vmcs_writel(HOST_GS_BASE, a); /* 22.2.4 */
5387 #else
5388 vmcs_writel(HOST_FS_BASE, 0); /* 22.2.4 */
5389 vmcs_writel(HOST_GS_BASE, 0); /* 22.2.4 */
5390 #endif
5391
5392 vmcs_write32(VM_EXIT_MSR_STORE_COUNT, 0);
5393 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, 0);
5394 vmcs_write64(VM_EXIT_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.host));
5395 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, 0);
5396 vmcs_write64(VM_ENTRY_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.guest));
5397
5398 if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT)
5399 vmcs_write64(GUEST_IA32_PAT, vmx->vcpu.arch.pat);
5400
5401 for (i = 0; i < ARRAY_SIZE(vmx_msr_index); ++i) {
5402 u32 index = vmx_msr_index[i];
5403 u32 data_low, data_high;
5404 int j = vmx->nmsrs;
5405
5406 if (rdmsr_safe(index, &data_low, &data_high) < 0)
5407 continue;
5408 if (wrmsr_safe(index, data_low, data_high) < 0)
5409 continue;
5410 vmx->guest_msrs[j].index = i;
5411 vmx->guest_msrs[j].data = 0;
5412 vmx->guest_msrs[j].mask = -1ull;
5413 ++vmx->nmsrs;
5414 }
5415
5416
5417 vm_exit_controls_init(vmx, vmcs_config.vmexit_ctrl);
5418
5419 /* 22.2.1, 20.8.1 */
5420 vm_entry_controls_init(vmx, vmcs_config.vmentry_ctrl);
5421
5422 vmx->vcpu.arch.cr0_guest_owned_bits = X86_CR0_TS;
5423 vmcs_writel(CR0_GUEST_HOST_MASK, ~X86_CR0_TS);
5424
5425 set_cr4_guest_host_mask(vmx);
5426
5427 if (vmx_xsaves_supported())
5428 vmcs_write64(XSS_EXIT_BITMAP, VMX_XSS_EXIT_BITMAP);
5429
5430 if (enable_pml) {
5431 ASSERT(vmx->pml_pg);
5432 vmcs_write64(PML_ADDRESS, page_to_phys(vmx->pml_pg));
5433 vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1);
5434 }
5435
5436 return 0;
5437 }
5438
5439 static void vmx_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event)
5440 {
5441 struct vcpu_vmx *vmx = to_vmx(vcpu);
5442 struct msr_data apic_base_msr;
5443 u64 cr0;
5444
5445 vmx->rmode.vm86_active = 0;
5446
5447 vmx->vcpu.arch.regs[VCPU_REGS_RDX] = get_rdx_init_val();
5448 kvm_set_cr8(vcpu, 0);
5449
5450 if (!init_event) {
5451 apic_base_msr.data = APIC_DEFAULT_PHYS_BASE |
5452 MSR_IA32_APICBASE_ENABLE;
5453 if (kvm_vcpu_is_reset_bsp(vcpu))
5454 apic_base_msr.data |= MSR_IA32_APICBASE_BSP;
5455 apic_base_msr.host_initiated = true;
5456 kvm_set_apic_base(vcpu, &apic_base_msr);
5457 }
5458
5459 vmx_segment_cache_clear(vmx);
5460
5461 seg_setup(VCPU_SREG_CS);
5462 vmcs_write16(GUEST_CS_SELECTOR, 0xf000);
5463 vmcs_writel(GUEST_CS_BASE, 0xffff0000ul);
5464
5465 seg_setup(VCPU_SREG_DS);
5466 seg_setup(VCPU_SREG_ES);
5467 seg_setup(VCPU_SREG_FS);
5468 seg_setup(VCPU_SREG_GS);
5469 seg_setup(VCPU_SREG_SS);
5470
5471 vmcs_write16(GUEST_TR_SELECTOR, 0);
5472 vmcs_writel(GUEST_TR_BASE, 0);
5473 vmcs_write32(GUEST_TR_LIMIT, 0xffff);
5474 vmcs_write32(GUEST_TR_AR_BYTES, 0x008b);
5475
5476 vmcs_write16(GUEST_LDTR_SELECTOR, 0);
5477 vmcs_writel(GUEST_LDTR_BASE, 0);
5478 vmcs_write32(GUEST_LDTR_LIMIT, 0xffff);
5479 vmcs_write32(GUEST_LDTR_AR_BYTES, 0x00082);
5480
5481 if (!init_event) {
5482 vmcs_write32(GUEST_SYSENTER_CS, 0);
5483 vmcs_writel(GUEST_SYSENTER_ESP, 0);
5484 vmcs_writel(GUEST_SYSENTER_EIP, 0);
5485 vmcs_write64(GUEST_IA32_DEBUGCTL, 0);
5486 }
5487
5488 vmcs_writel(GUEST_RFLAGS, 0x02);
5489 kvm_rip_write(vcpu, 0xfff0);
5490
5491 vmcs_writel(GUEST_GDTR_BASE, 0);
5492 vmcs_write32(GUEST_GDTR_LIMIT, 0xffff);
5493
5494 vmcs_writel(GUEST_IDTR_BASE, 0);
5495 vmcs_write32(GUEST_IDTR_LIMIT, 0xffff);
5496
5497 vmcs_write32(GUEST_ACTIVITY_STATE, GUEST_ACTIVITY_ACTIVE);
5498 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, 0);
5499 vmcs_writel(GUEST_PENDING_DBG_EXCEPTIONS, 0);
5500
5501 setup_msrs(vmx);
5502
5503 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0); /* 22.2.1 */
5504
5505 if (cpu_has_vmx_tpr_shadow() && !init_event) {
5506 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, 0);
5507 if (cpu_need_tpr_shadow(vcpu))
5508 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR,
5509 __pa(vcpu->arch.apic->regs));
5510 vmcs_write32(TPR_THRESHOLD, 0);
5511 }
5512
5513 kvm_make_request(KVM_REQ_APIC_PAGE_RELOAD, vcpu);
5514
5515 if (kvm_vcpu_apicv_active(vcpu))
5516 memset(&vmx->pi_desc, 0, sizeof(struct pi_desc));
5517
5518 if (vmx->vpid != 0)
5519 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid);
5520
5521 cr0 = X86_CR0_NW | X86_CR0_CD | X86_CR0_ET;
5522 vmx->vcpu.arch.cr0 = cr0;
5523 vmx_set_cr0(vcpu, cr0); /* enter rmode */
5524 vmx_set_cr4(vcpu, 0);
5525 vmx_set_efer(vcpu, 0);
5526
5527 update_exception_bitmap(vcpu);
5528
5529 vpid_sync_context(vmx->vpid);
5530 }
5531
5532 /*
5533 * In nested virtualization, check if L1 asked to exit on external interrupts.
5534 * For most existing hypervisors, this will always return true.
5535 */
5536 static bool nested_exit_on_intr(struct kvm_vcpu *vcpu)
5537 {
5538 return get_vmcs12(vcpu)->pin_based_vm_exec_control &
5539 PIN_BASED_EXT_INTR_MASK;
5540 }
5541
5542 /*
5543 * In nested virtualization, check if L1 has set
5544 * VM_EXIT_ACK_INTR_ON_EXIT
5545 */
5546 static bool nested_exit_intr_ack_set(struct kvm_vcpu *vcpu)
5547 {
5548 return get_vmcs12(vcpu)->vm_exit_controls &
5549 VM_EXIT_ACK_INTR_ON_EXIT;
5550 }
5551
5552 static bool nested_exit_on_nmi(struct kvm_vcpu *vcpu)
5553 {
5554 return get_vmcs12(vcpu)->pin_based_vm_exec_control &
5555 PIN_BASED_NMI_EXITING;
5556 }
5557
5558 static void enable_irq_window(struct kvm_vcpu *vcpu)
5559 {
5560 vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL,
5561 CPU_BASED_VIRTUAL_INTR_PENDING);
5562 }
5563
5564 static void enable_nmi_window(struct kvm_vcpu *vcpu)
5565 {
5566 if (!cpu_has_virtual_nmis() ||
5567 vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & GUEST_INTR_STATE_STI) {
5568 enable_irq_window(vcpu);
5569 return;
5570 }
5571
5572 vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL,
5573 CPU_BASED_VIRTUAL_NMI_PENDING);
5574 }
5575
5576 static void vmx_inject_irq(struct kvm_vcpu *vcpu)
5577 {
5578 struct vcpu_vmx *vmx = to_vmx(vcpu);
5579 uint32_t intr;
5580 int irq = vcpu->arch.interrupt.nr;
5581
5582 trace_kvm_inj_virq(irq);
5583
5584 ++vcpu->stat.irq_injections;
5585 if (vmx->rmode.vm86_active) {
5586 int inc_eip = 0;
5587 if (vcpu->arch.interrupt.soft)
5588 inc_eip = vcpu->arch.event_exit_inst_len;
5589 if (kvm_inject_realmode_interrupt(vcpu, irq, inc_eip) != EMULATE_DONE)
5590 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
5591 return;
5592 }
5593 intr = irq | INTR_INFO_VALID_MASK;
5594 if (vcpu->arch.interrupt.soft) {
5595 intr |= INTR_TYPE_SOFT_INTR;
5596 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
5597 vmx->vcpu.arch.event_exit_inst_len);
5598 } else
5599 intr |= INTR_TYPE_EXT_INTR;
5600 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr);
5601 }
5602
5603 static void vmx_inject_nmi(struct kvm_vcpu *vcpu)
5604 {
5605 struct vcpu_vmx *vmx = to_vmx(vcpu);
5606
5607 if (!cpu_has_virtual_nmis()) {
5608 /*
5609 * Tracking the NMI-blocked state in software is built upon
5610 * finding the next open IRQ window. This, in turn, depends on
5611 * well-behaving guests: They have to keep IRQs disabled at
5612 * least as long as the NMI handler runs. Otherwise we may
5613 * cause NMI nesting, maybe breaking the guest. But as this is
5614 * highly unlikely, we can live with the residual risk.
5615 */
5616 vmx->loaded_vmcs->soft_vnmi_blocked = 1;
5617 vmx->loaded_vmcs->vnmi_blocked_time = 0;
5618 }
5619
5620 ++vcpu->stat.nmi_injections;
5621 vmx->loaded_vmcs->nmi_known_unmasked = false;
5622
5623 if (vmx->rmode.vm86_active) {
5624 if (kvm_inject_realmode_interrupt(vcpu, NMI_VECTOR, 0) != EMULATE_DONE)
5625 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
5626 return;
5627 }
5628
5629 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
5630 INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK | NMI_VECTOR);
5631 }
5632
5633 static bool vmx_get_nmi_mask(struct kvm_vcpu *vcpu)
5634 {
5635 struct vcpu_vmx *vmx = to_vmx(vcpu);
5636 bool masked;
5637
5638 if (!cpu_has_virtual_nmis())
5639 return vmx->loaded_vmcs->soft_vnmi_blocked;
5640 if (vmx->loaded_vmcs->nmi_known_unmasked)
5641 return false;
5642 masked = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & GUEST_INTR_STATE_NMI;
5643 vmx->loaded_vmcs->nmi_known_unmasked = !masked;
5644 return masked;
5645 }
5646
5647 static void vmx_set_nmi_mask(struct kvm_vcpu *vcpu, bool masked)
5648 {
5649 struct vcpu_vmx *vmx = to_vmx(vcpu);
5650
5651 if (!cpu_has_virtual_nmis()) {
5652 if (vmx->loaded_vmcs->soft_vnmi_blocked != masked) {
5653 vmx->loaded_vmcs->soft_vnmi_blocked = masked;
5654 vmx->loaded_vmcs->vnmi_blocked_time = 0;
5655 }
5656 } else {
5657 vmx->loaded_vmcs->nmi_known_unmasked = !masked;
5658 if (masked)
5659 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
5660 GUEST_INTR_STATE_NMI);
5661 else
5662 vmcs_clear_bits(GUEST_INTERRUPTIBILITY_INFO,
5663 GUEST_INTR_STATE_NMI);
5664 }
5665 }
5666
5667 static int vmx_nmi_allowed(struct kvm_vcpu *vcpu)
5668 {
5669 if (to_vmx(vcpu)->nested.nested_run_pending)
5670 return 0;
5671
5672 if (!cpu_has_virtual_nmis() &&
5673 to_vmx(vcpu)->loaded_vmcs->soft_vnmi_blocked)
5674 return 0;
5675
5676 return !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) &
5677 (GUEST_INTR_STATE_MOV_SS | GUEST_INTR_STATE_STI
5678 | GUEST_INTR_STATE_NMI));
5679 }
5680
5681 static int vmx_interrupt_allowed(struct kvm_vcpu *vcpu)
5682 {
5683 return (!to_vmx(vcpu)->nested.nested_run_pending &&
5684 vmcs_readl(GUEST_RFLAGS) & X86_EFLAGS_IF) &&
5685 !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) &
5686 (GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS));
5687 }
5688
5689 static int vmx_set_tss_addr(struct kvm *kvm, unsigned int addr)
5690 {
5691 int ret;
5692
5693 ret = x86_set_memory_region(kvm, TSS_PRIVATE_MEMSLOT, addr,
5694 PAGE_SIZE * 3);
5695 if (ret)
5696 return ret;
5697 kvm->arch.tss_addr = addr;
5698 return init_rmode_tss(kvm);
5699 }
5700
5701 static bool rmode_exception(struct kvm_vcpu *vcpu, int vec)
5702 {
5703 switch (vec) {
5704 case BP_VECTOR:
5705 /*
5706 * Update instruction length as we may reinject the exception
5707 * from user space while in guest debugging mode.
5708 */
5709 to_vmx(vcpu)->vcpu.arch.event_exit_inst_len =
5710 vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
5711 if (vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
5712 return false;
5713 /* fall through */
5714 case DB_VECTOR:
5715 if (vcpu->guest_debug &
5716 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))
5717 return false;
5718 /* fall through */
5719 case DE_VECTOR:
5720 case OF_VECTOR:
5721 case BR_VECTOR:
5722 case UD_VECTOR:
5723 case DF_VECTOR:
5724 case SS_VECTOR:
5725 case GP_VECTOR:
5726 case MF_VECTOR:
5727 return true;
5728 break;
5729 }
5730 return false;
5731 }
5732
5733 static int handle_rmode_exception(struct kvm_vcpu *vcpu,
5734 int vec, u32 err_code)
5735 {
5736 /*
5737 * Instruction with address size override prefix opcode 0x67
5738 * Cause the #SS fault with 0 error code in VM86 mode.
5739 */
5740 if (((vec == GP_VECTOR) || (vec == SS_VECTOR)) && err_code == 0) {
5741 if (emulate_instruction(vcpu, 0) == EMULATE_DONE) {
5742 if (vcpu->arch.halt_request) {
5743 vcpu->arch.halt_request = 0;
5744 return kvm_vcpu_halt(vcpu);
5745 }
5746 return 1;
5747 }
5748 return 0;
5749 }
5750
5751 /*
5752 * Forward all other exceptions that are valid in real mode.
5753 * FIXME: Breaks guest debugging in real mode, needs to be fixed with
5754 * the required debugging infrastructure rework.
5755 */
5756 kvm_queue_exception(vcpu, vec);
5757 return 1;
5758 }
5759
5760 /*
5761 * Trigger machine check on the host. We assume all the MSRs are already set up
5762 * by the CPU and that we still run on the same CPU as the MCE occurred on.
5763 * We pass a fake environment to the machine check handler because we want
5764 * the guest to be always treated like user space, no matter what context
5765 * it used internally.
5766 */
5767 static void kvm_machine_check(void)
5768 {
5769 #if defined(CONFIG_X86_MCE) && defined(CONFIG_X86_64)
5770 struct pt_regs regs = {
5771 .cs = 3, /* Fake ring 3 no matter what the guest ran on */
5772 .flags = X86_EFLAGS_IF,
5773 };
5774
5775 do_machine_check(&regs, 0);
5776 #endif
5777 }
5778
5779 static int handle_machine_check(struct kvm_vcpu *vcpu)
5780 {
5781 /* already handled by vcpu_run */
5782 return 1;
5783 }
5784
5785 static int handle_exception(struct kvm_vcpu *vcpu)
5786 {
5787 struct vcpu_vmx *vmx = to_vmx(vcpu);
5788 struct kvm_run *kvm_run = vcpu->run;
5789 u32 intr_info, ex_no, error_code;
5790 unsigned long cr2, rip, dr6;
5791 u32 vect_info;
5792 enum emulation_result er;
5793
5794 vect_info = vmx->idt_vectoring_info;
5795 intr_info = vmx->exit_intr_info;
5796
5797 if (is_machine_check(intr_info))
5798 return handle_machine_check(vcpu);
5799
5800 if (is_nmi(intr_info))
5801 return 1; /* already handled by vmx_vcpu_run() */
5802
5803 if (is_invalid_opcode(intr_info)) {
5804 if (is_guest_mode(vcpu)) {
5805 kvm_queue_exception(vcpu, UD_VECTOR);
5806 return 1;
5807 }
5808 er = emulate_instruction(vcpu, EMULTYPE_TRAP_UD);
5809 if (er != EMULATE_DONE)
5810 kvm_queue_exception(vcpu, UD_VECTOR);
5811 return 1;
5812 }
5813
5814 error_code = 0;
5815 if (intr_info & INTR_INFO_DELIVER_CODE_MASK)
5816 error_code = vmcs_read32(VM_EXIT_INTR_ERROR_CODE);
5817
5818 /*
5819 * The #PF with PFEC.RSVD = 1 indicates the guest is accessing
5820 * MMIO, it is better to report an internal error.
5821 * See the comments in vmx_handle_exit.
5822 */
5823 if ((vect_info & VECTORING_INFO_VALID_MASK) &&
5824 !(is_page_fault(intr_info) && !(error_code & PFERR_RSVD_MASK))) {
5825 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
5826 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_SIMUL_EX;
5827 vcpu->run->internal.ndata = 3;
5828 vcpu->run->internal.data[0] = vect_info;
5829 vcpu->run->internal.data[1] = intr_info;
5830 vcpu->run->internal.data[2] = error_code;
5831 return 0;
5832 }
5833
5834 if (is_page_fault(intr_info)) {
5835 cr2 = vmcs_readl(EXIT_QUALIFICATION);
5836 /* EPT won't cause page fault directly */
5837 WARN_ON_ONCE(!vcpu->arch.apf.host_apf_reason && enable_ept);
5838 return kvm_handle_page_fault(vcpu, error_code, cr2, NULL, 0,
5839 true);
5840 }
5841
5842 ex_no = intr_info & INTR_INFO_VECTOR_MASK;
5843
5844 if (vmx->rmode.vm86_active && rmode_exception(vcpu, ex_no))
5845 return handle_rmode_exception(vcpu, ex_no, error_code);
5846
5847 switch (ex_no) {
5848 case AC_VECTOR:
5849 kvm_queue_exception_e(vcpu, AC_VECTOR, error_code);
5850 return 1;
5851 case DB_VECTOR:
5852 dr6 = vmcs_readl(EXIT_QUALIFICATION);
5853 if (!(vcpu->guest_debug &
5854 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))) {
5855 vcpu->arch.dr6 &= ~15;
5856 vcpu->arch.dr6 |= dr6 | DR6_RTM;
5857 if (is_icebp(intr_info))
5858 skip_emulated_instruction(vcpu);
5859
5860 kvm_queue_exception(vcpu, DB_VECTOR);
5861 return 1;
5862 }
5863 kvm_run->debug.arch.dr6 = dr6 | DR6_FIXED_1;
5864 kvm_run->debug.arch.dr7 = vmcs_readl(GUEST_DR7);
5865 /* fall through */
5866 case BP_VECTOR:
5867 /*
5868 * Update instruction length as we may reinject #BP from
5869 * user space while in guest debugging mode. Reading it for
5870 * #DB as well causes no harm, it is not used in that case.
5871 */
5872 vmx->vcpu.arch.event_exit_inst_len =
5873 vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
5874 kvm_run->exit_reason = KVM_EXIT_DEBUG;
5875 rip = kvm_rip_read(vcpu);
5876 kvm_run->debug.arch.pc = vmcs_readl(GUEST_CS_BASE) + rip;
5877 kvm_run->debug.arch.exception = ex_no;
5878 break;
5879 default:
5880 kvm_run->exit_reason = KVM_EXIT_EXCEPTION;
5881 kvm_run->ex.exception = ex_no;
5882 kvm_run->ex.error_code = error_code;
5883 break;
5884 }
5885 return 0;
5886 }
5887
5888 static int handle_external_interrupt(struct kvm_vcpu *vcpu)
5889 {
5890 ++vcpu->stat.irq_exits;
5891 return 1;
5892 }
5893
5894 static int handle_triple_fault(struct kvm_vcpu *vcpu)
5895 {
5896 vcpu->run->exit_reason = KVM_EXIT_SHUTDOWN;
5897 return 0;
5898 }
5899
5900 static int handle_io(struct kvm_vcpu *vcpu)
5901 {
5902 unsigned long exit_qualification;
5903 int size, in, string, ret;
5904 unsigned port;
5905
5906 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
5907 string = (exit_qualification & 16) != 0;
5908 in = (exit_qualification & 8) != 0;
5909
5910 ++vcpu->stat.io_exits;
5911
5912 if (string || in)
5913 return emulate_instruction(vcpu, 0) == EMULATE_DONE;
5914
5915 port = exit_qualification >> 16;
5916 size = (exit_qualification & 7) + 1;
5917
5918 ret = kvm_skip_emulated_instruction(vcpu);
5919
5920 /*
5921 * TODO: we might be squashing a KVM_GUESTDBG_SINGLESTEP-triggered
5922 * KVM_EXIT_DEBUG here.
5923 */
5924 return kvm_fast_pio_out(vcpu, size, port) && ret;
5925 }
5926
5927 static void
5928 vmx_patch_hypercall(struct kvm_vcpu *vcpu, unsigned char *hypercall)
5929 {
5930 /*
5931 * Patch in the VMCALL instruction:
5932 */
5933 hypercall[0] = 0x0f;
5934 hypercall[1] = 0x01;
5935 hypercall[2] = 0xc1;
5936 }
5937
5938 /* called to set cr0 as appropriate for a mov-to-cr0 exit. */
5939 static int handle_set_cr0(struct kvm_vcpu *vcpu, unsigned long val)
5940 {
5941 if (is_guest_mode(vcpu)) {
5942 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
5943 unsigned long orig_val = val;
5944
5945 /*
5946 * We get here when L2 changed cr0 in a way that did not change
5947 * any of L1's shadowed bits (see nested_vmx_exit_handled_cr),
5948 * but did change L0 shadowed bits. So we first calculate the
5949 * effective cr0 value that L1 would like to write into the
5950 * hardware. It consists of the L2-owned bits from the new
5951 * value combined with the L1-owned bits from L1's guest_cr0.
5952 */
5953 val = (val & ~vmcs12->cr0_guest_host_mask) |
5954 (vmcs12->guest_cr0 & vmcs12->cr0_guest_host_mask);
5955
5956 if (!nested_guest_cr0_valid(vcpu, val))
5957 return 1;
5958
5959 if (kvm_set_cr0(vcpu, val))
5960 return 1;
5961 vmcs_writel(CR0_READ_SHADOW, orig_val);
5962 return 0;
5963 } else {
5964 if (to_vmx(vcpu)->nested.vmxon &&
5965 !nested_host_cr0_valid(vcpu, val))
5966 return 1;
5967
5968 return kvm_set_cr0(vcpu, val);
5969 }
5970 }
5971
5972 static int handle_set_cr4(struct kvm_vcpu *vcpu, unsigned long val)
5973 {
5974 if (is_guest_mode(vcpu)) {
5975 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
5976 unsigned long orig_val = val;
5977
5978 /* analogously to handle_set_cr0 */
5979 val = (val & ~vmcs12->cr4_guest_host_mask) |
5980 (vmcs12->guest_cr4 & vmcs12->cr4_guest_host_mask);
5981 if (kvm_set_cr4(vcpu, val))
5982 return 1;
5983 vmcs_writel(CR4_READ_SHADOW, orig_val);
5984 return 0;
5985 } else
5986 return kvm_set_cr4(vcpu, val);
5987 }
5988
5989 static int handle_cr(struct kvm_vcpu *vcpu)
5990 {
5991 unsigned long exit_qualification, val;
5992 int cr;
5993 int reg;
5994 int err;
5995 int ret;
5996
5997 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
5998 cr = exit_qualification & 15;
5999 reg = (exit_qualification >> 8) & 15;
6000 switch ((exit_qualification >> 4) & 3) {
6001 case 0: /* mov to cr */
6002 val = kvm_register_readl(vcpu, reg);
6003 trace_kvm_cr_write(cr, val);
6004 switch (cr) {
6005 case 0:
6006 err = handle_set_cr0(vcpu, val);
6007 return kvm_complete_insn_gp(vcpu, err);
6008 case 3:
6009 err = kvm_set_cr3(vcpu, val);
6010 return kvm_complete_insn_gp(vcpu, err);
6011 case 4:
6012 err = handle_set_cr4(vcpu, val);
6013 return kvm_complete_insn_gp(vcpu, err);
6014 case 8: {
6015 u8 cr8_prev = kvm_get_cr8(vcpu);
6016 u8 cr8 = (u8)val;
6017 err = kvm_set_cr8(vcpu, cr8);
6018 ret = kvm_complete_insn_gp(vcpu, err);
6019 if (lapic_in_kernel(vcpu))
6020 return ret;
6021 if (cr8_prev <= cr8)
6022 return ret;
6023 /*
6024 * TODO: we might be squashing a
6025 * KVM_GUESTDBG_SINGLESTEP-triggered
6026 * KVM_EXIT_DEBUG here.
6027 */
6028 vcpu->run->exit_reason = KVM_EXIT_SET_TPR;
6029 return 0;
6030 }
6031 }
6032 break;
6033 case 2: /* clts */
6034 WARN_ONCE(1, "Guest should always own CR0.TS");
6035 vmx_set_cr0(vcpu, kvm_read_cr0_bits(vcpu, ~X86_CR0_TS));
6036 trace_kvm_cr_write(0, kvm_read_cr0(vcpu));
6037 return kvm_skip_emulated_instruction(vcpu);
6038 case 1: /*mov from cr*/
6039 switch (cr) {
6040 case 3:
6041 val = kvm_read_cr3(vcpu);
6042 kvm_register_write(vcpu, reg, val);
6043 trace_kvm_cr_read(cr, val);
6044 return kvm_skip_emulated_instruction(vcpu);
6045 case 8:
6046 val = kvm_get_cr8(vcpu);
6047 kvm_register_write(vcpu, reg, val);
6048 trace_kvm_cr_read(cr, val);
6049 return kvm_skip_emulated_instruction(vcpu);
6050 }
6051 break;
6052 case 3: /* lmsw */
6053 val = (exit_qualification >> LMSW_SOURCE_DATA_SHIFT) & 0x0f;
6054 trace_kvm_cr_write(0, (kvm_read_cr0(vcpu) & ~0xful) | val);
6055 kvm_lmsw(vcpu, val);
6056
6057 return kvm_skip_emulated_instruction(vcpu);
6058 default:
6059 break;
6060 }
6061 vcpu->run->exit_reason = 0;
6062 vcpu_unimpl(vcpu, "unhandled control register: op %d cr %d\n",
6063 (int)(exit_qualification >> 4) & 3, cr);
6064 return 0;
6065 }
6066
6067 static int handle_dr(struct kvm_vcpu *vcpu)
6068 {
6069 unsigned long exit_qualification;
6070 int dr, dr7, reg;
6071
6072 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
6073 dr = exit_qualification & DEBUG_REG_ACCESS_NUM;
6074
6075 /* First, if DR does not exist, trigger UD */
6076 if (!kvm_require_dr(vcpu, dr))
6077 return 1;
6078
6079 /* Do not handle if the CPL > 0, will trigger GP on re-entry */
6080 if (!kvm_require_cpl(vcpu, 0))
6081 return 1;
6082 dr7 = vmcs_readl(GUEST_DR7);
6083 if (dr7 & DR7_GD) {
6084 /*
6085 * As the vm-exit takes precedence over the debug trap, we
6086 * need to emulate the latter, either for the host or the
6087 * guest debugging itself.
6088 */
6089 if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP) {
6090 vcpu->run->debug.arch.dr6 = vcpu->arch.dr6;
6091 vcpu->run->debug.arch.dr7 = dr7;
6092 vcpu->run->debug.arch.pc = kvm_get_linear_rip(vcpu);
6093 vcpu->run->debug.arch.exception = DB_VECTOR;
6094 vcpu->run->exit_reason = KVM_EXIT_DEBUG;
6095 return 0;
6096 } else {
6097 vcpu->arch.dr6 &= ~15;
6098 vcpu->arch.dr6 |= DR6_BD | DR6_RTM;
6099 kvm_queue_exception(vcpu, DB_VECTOR);
6100 return 1;
6101 }
6102 }
6103
6104 if (vcpu->guest_debug == 0) {
6105 vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL,
6106 CPU_BASED_MOV_DR_EXITING);
6107
6108 /*
6109 * No more DR vmexits; force a reload of the debug registers
6110 * and reenter on this instruction. The next vmexit will
6111 * retrieve the full state of the debug registers.
6112 */
6113 vcpu->arch.switch_db_regs |= KVM_DEBUGREG_WONT_EXIT;
6114 return 1;
6115 }
6116
6117 reg = DEBUG_REG_ACCESS_REG(exit_qualification);
6118 if (exit_qualification & TYPE_MOV_FROM_DR) {
6119 unsigned long val;
6120
6121 if (kvm_get_dr(vcpu, dr, &val))
6122 return 1;
6123 kvm_register_write(vcpu, reg, val);
6124 } else
6125 if (kvm_set_dr(vcpu, dr, kvm_register_readl(vcpu, reg)))
6126 return 1;
6127
6128 return kvm_skip_emulated_instruction(vcpu);
6129 }
6130
6131 static u64 vmx_get_dr6(struct kvm_vcpu *vcpu)
6132 {
6133 return vcpu->arch.dr6;
6134 }
6135
6136 static void vmx_set_dr6(struct kvm_vcpu *vcpu, unsigned long val)
6137 {
6138 }
6139
6140 static void vmx_sync_dirty_debug_regs(struct kvm_vcpu *vcpu)
6141 {
6142 get_debugreg(vcpu->arch.db[0], 0);
6143 get_debugreg(vcpu->arch.db[1], 1);
6144 get_debugreg(vcpu->arch.db[2], 2);
6145 get_debugreg(vcpu->arch.db[3], 3);
6146 get_debugreg(vcpu->arch.dr6, 6);
6147 vcpu->arch.dr7 = vmcs_readl(GUEST_DR7);
6148
6149 vcpu->arch.switch_db_regs &= ~KVM_DEBUGREG_WONT_EXIT;
6150 vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL, CPU_BASED_MOV_DR_EXITING);
6151 }
6152
6153 static void vmx_set_dr7(struct kvm_vcpu *vcpu, unsigned long val)
6154 {
6155 vmcs_writel(GUEST_DR7, val);
6156 }
6157
6158 static int handle_cpuid(struct kvm_vcpu *vcpu)
6159 {
6160 return kvm_emulate_cpuid(vcpu);
6161 }
6162
6163 static int handle_rdmsr(struct kvm_vcpu *vcpu)
6164 {
6165 u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX];
6166 struct msr_data msr_info;
6167
6168 msr_info.index = ecx;
6169 msr_info.host_initiated = false;
6170
6171 if (vmx_get_msr(vcpu, &msr_info)) {
6172 trace_kvm_msr_read_ex(ecx);
6173 kvm_inject_gp(vcpu, 0);
6174 return 1;
6175 }
6176
6177 trace_kvm_msr_read(ecx, msr_info.data);
6178
6179 /* FIXME: handling of bits 32:63 of rax, rdx */
6180 vcpu->arch.regs[VCPU_REGS_RAX] = msr_info.data & -1u;
6181 vcpu->arch.regs[VCPU_REGS_RDX] = (msr_info.data >> 32) & -1u;
6182 return kvm_skip_emulated_instruction(vcpu);
6183 }
6184
6185 static int handle_wrmsr(struct kvm_vcpu *vcpu)
6186 {
6187 struct msr_data msr;
6188 u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX];
6189 u64 data = (vcpu->arch.regs[VCPU_REGS_RAX] & -1u)
6190 | ((u64)(vcpu->arch.regs[VCPU_REGS_RDX] & -1u) << 32);
6191
6192 msr.data = data;
6193 msr.index = ecx;
6194 msr.host_initiated = false;
6195 if (kvm_set_msr(vcpu, &msr) != 0) {
6196 trace_kvm_msr_write_ex(ecx, data);
6197 kvm_inject_gp(vcpu, 0);
6198 return 1;
6199 }
6200
6201 trace_kvm_msr_write(ecx, data);
6202 return kvm_skip_emulated_instruction(vcpu);
6203 }
6204
6205 static int handle_tpr_below_threshold(struct kvm_vcpu *vcpu)
6206 {
6207 kvm_apic_update_ppr(vcpu);
6208 return 1;
6209 }
6210
6211 static int handle_interrupt_window(struct kvm_vcpu *vcpu)
6212 {
6213 vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL,
6214 CPU_BASED_VIRTUAL_INTR_PENDING);
6215
6216 kvm_make_request(KVM_REQ_EVENT, vcpu);
6217
6218 ++vcpu->stat.irq_window_exits;
6219 return 1;
6220 }
6221
6222 static int handle_halt(struct kvm_vcpu *vcpu)
6223 {
6224 return kvm_emulate_halt(vcpu);
6225 }
6226
6227 static int handle_vmcall(struct kvm_vcpu *vcpu)
6228 {
6229 return kvm_emulate_hypercall(vcpu);
6230 }
6231
6232 static int handle_invd(struct kvm_vcpu *vcpu)
6233 {
6234 return emulate_instruction(vcpu, 0) == EMULATE_DONE;
6235 }
6236
6237 static int handle_invlpg(struct kvm_vcpu *vcpu)
6238 {
6239 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
6240
6241 kvm_mmu_invlpg(vcpu, exit_qualification);
6242 return kvm_skip_emulated_instruction(vcpu);
6243 }
6244
6245 static int handle_rdpmc(struct kvm_vcpu *vcpu)
6246 {
6247 int err;
6248
6249 err = kvm_rdpmc(vcpu);
6250 return kvm_complete_insn_gp(vcpu, err);
6251 }
6252
6253 static int handle_wbinvd(struct kvm_vcpu *vcpu)
6254 {
6255 return kvm_emulate_wbinvd(vcpu);
6256 }
6257
6258 static int handle_xsetbv(struct kvm_vcpu *vcpu)
6259 {
6260 u64 new_bv = kvm_read_edx_eax(vcpu);
6261 u32 index = kvm_register_read(vcpu, VCPU_REGS_RCX);
6262
6263 if (kvm_set_xcr(vcpu, index, new_bv) == 0)
6264 return kvm_skip_emulated_instruction(vcpu);
6265 return 1;
6266 }
6267
6268 static int handle_xsaves(struct kvm_vcpu *vcpu)
6269 {
6270 kvm_skip_emulated_instruction(vcpu);
6271 WARN(1, "this should never happen\n");
6272 return 1;
6273 }
6274
6275 static int handle_xrstors(struct kvm_vcpu *vcpu)
6276 {
6277 kvm_skip_emulated_instruction(vcpu);
6278 WARN(1, "this should never happen\n");
6279 return 1;
6280 }
6281
6282 static int handle_apic_access(struct kvm_vcpu *vcpu)
6283 {
6284 if (likely(fasteoi)) {
6285 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
6286 int access_type, offset;
6287
6288 access_type = exit_qualification & APIC_ACCESS_TYPE;
6289 offset = exit_qualification & APIC_ACCESS_OFFSET;
6290 /*
6291 * Sane guest uses MOV to write EOI, with written value
6292 * not cared. So make a short-circuit here by avoiding
6293 * heavy instruction emulation.
6294 */
6295 if ((access_type == TYPE_LINEAR_APIC_INST_WRITE) &&
6296 (offset == APIC_EOI)) {
6297 kvm_lapic_set_eoi(vcpu);
6298 return kvm_skip_emulated_instruction(vcpu);
6299 }
6300 }
6301 return emulate_instruction(vcpu, 0) == EMULATE_DONE;
6302 }
6303
6304 static int handle_apic_eoi_induced(struct kvm_vcpu *vcpu)
6305 {
6306 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
6307 int vector = exit_qualification & 0xff;
6308
6309 /* EOI-induced VM exit is trap-like and thus no need to adjust IP */
6310 kvm_apic_set_eoi_accelerated(vcpu, vector);
6311 return 1;
6312 }
6313
6314 static int handle_apic_write(struct kvm_vcpu *vcpu)
6315 {
6316 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
6317 u32 offset = exit_qualification & 0xfff;
6318
6319 /* APIC-write VM exit is trap-like and thus no need to adjust IP */
6320 kvm_apic_write_nodecode(vcpu, offset);
6321 return 1;
6322 }
6323
6324 static int handle_task_switch(struct kvm_vcpu *vcpu)
6325 {
6326 struct vcpu_vmx *vmx = to_vmx(vcpu);
6327 unsigned long exit_qualification;
6328 bool has_error_code = false;
6329 u32 error_code = 0;
6330 u16 tss_selector;
6331 int reason, type, idt_v, idt_index;
6332
6333 idt_v = (vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK);
6334 idt_index = (vmx->idt_vectoring_info & VECTORING_INFO_VECTOR_MASK);
6335 type = (vmx->idt_vectoring_info & VECTORING_INFO_TYPE_MASK);
6336
6337 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
6338
6339 reason = (u32)exit_qualification >> 30;
6340 if (reason == TASK_SWITCH_GATE && idt_v) {
6341 switch (type) {
6342 case INTR_TYPE_NMI_INTR:
6343 vcpu->arch.nmi_injected = false;
6344 vmx_set_nmi_mask(vcpu, true);
6345 break;
6346 case INTR_TYPE_EXT_INTR:
6347 case INTR_TYPE_SOFT_INTR:
6348 kvm_clear_interrupt_queue(vcpu);
6349 break;
6350 case INTR_TYPE_HARD_EXCEPTION:
6351 if (vmx->idt_vectoring_info &
6352 VECTORING_INFO_DELIVER_CODE_MASK) {
6353 has_error_code = true;
6354 error_code =
6355 vmcs_read32(IDT_VECTORING_ERROR_CODE);
6356 }
6357 /* fall through */
6358 case INTR_TYPE_SOFT_EXCEPTION:
6359 kvm_clear_exception_queue(vcpu);
6360 break;
6361 default:
6362 break;
6363 }
6364 }
6365 tss_selector = exit_qualification;
6366
6367 if (!idt_v || (type != INTR_TYPE_HARD_EXCEPTION &&
6368 type != INTR_TYPE_EXT_INTR &&
6369 type != INTR_TYPE_NMI_INTR))
6370 skip_emulated_instruction(vcpu);
6371
6372 if (kvm_task_switch(vcpu, tss_selector,
6373 type == INTR_TYPE_SOFT_INTR ? idt_index : -1, reason,
6374 has_error_code, error_code) == EMULATE_FAIL) {
6375 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
6376 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
6377 vcpu->run->internal.ndata = 0;
6378 return 0;
6379 }
6380
6381 /*
6382 * TODO: What about debug traps on tss switch?
6383 * Are we supposed to inject them and update dr6?
6384 */
6385
6386 return 1;
6387 }
6388
6389 static int handle_ept_violation(struct kvm_vcpu *vcpu)
6390 {
6391 unsigned long exit_qualification;
6392 gpa_t gpa;
6393 u32 error_code;
6394
6395 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
6396
6397 /*
6398 * EPT violation happened while executing iret from NMI,
6399 * "blocked by NMI" bit has to be set before next VM entry.
6400 * There are errata that may cause this bit to not be set:
6401 * AAK134, BY25.
6402 */
6403 if (!(to_vmx(vcpu)->idt_vectoring_info & VECTORING_INFO_VALID_MASK) &&
6404 cpu_has_virtual_nmis() &&
6405 (exit_qualification & INTR_INFO_UNBLOCK_NMI))
6406 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO, GUEST_INTR_STATE_NMI);
6407
6408 gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS);
6409 trace_kvm_page_fault(gpa, exit_qualification);
6410
6411 /* Is it a read fault? */
6412 error_code = (exit_qualification & EPT_VIOLATION_ACC_READ)
6413 ? PFERR_USER_MASK : 0;
6414 /* Is it a write fault? */
6415 error_code |= (exit_qualification & EPT_VIOLATION_ACC_WRITE)
6416 ? PFERR_WRITE_MASK : 0;
6417 /* Is it a fetch fault? */
6418 error_code |= (exit_qualification & EPT_VIOLATION_ACC_INSTR)
6419 ? PFERR_FETCH_MASK : 0;
6420 /* ept page table entry is present? */
6421 error_code |= (exit_qualification &
6422 (EPT_VIOLATION_READABLE | EPT_VIOLATION_WRITABLE |
6423 EPT_VIOLATION_EXECUTABLE))
6424 ? PFERR_PRESENT_MASK : 0;
6425
6426 vcpu->arch.gpa_available = true;
6427 vcpu->arch.exit_qualification = exit_qualification;
6428
6429 return kvm_mmu_page_fault(vcpu, gpa, error_code, NULL, 0);
6430 }
6431
6432 static int handle_ept_misconfig(struct kvm_vcpu *vcpu)
6433 {
6434 int ret;
6435 gpa_t gpa;
6436
6437 gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS);
6438 if (!kvm_io_bus_write(vcpu, KVM_FAST_MMIO_BUS, gpa, 0, NULL)) {
6439 trace_kvm_fast_mmio(gpa);
6440 return kvm_skip_emulated_instruction(vcpu);
6441 }
6442
6443 ret = handle_mmio_page_fault(vcpu, gpa, true);
6444 vcpu->arch.gpa_available = true;
6445 if (likely(ret == RET_MMIO_PF_EMULATE))
6446 return x86_emulate_instruction(vcpu, gpa, 0, NULL, 0) ==
6447 EMULATE_DONE;
6448
6449 if (unlikely(ret == RET_MMIO_PF_INVALID))
6450 return kvm_mmu_page_fault(vcpu, gpa, 0, NULL, 0);
6451
6452 if (unlikely(ret == RET_MMIO_PF_RETRY))
6453 return 1;
6454
6455 /* It is the real ept misconfig */
6456 WARN_ON(1);
6457
6458 vcpu->run->exit_reason = KVM_EXIT_UNKNOWN;
6459 vcpu->run->hw.hardware_exit_reason = EXIT_REASON_EPT_MISCONFIG;
6460
6461 return 0;
6462 }
6463
6464 static int handle_nmi_window(struct kvm_vcpu *vcpu)
6465 {
6466 vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL,
6467 CPU_BASED_VIRTUAL_NMI_PENDING);
6468 ++vcpu->stat.nmi_window_exits;
6469 kvm_make_request(KVM_REQ_EVENT, vcpu);
6470
6471 return 1;
6472 }
6473
6474 static int handle_invalid_guest_state(struct kvm_vcpu *vcpu)
6475 {
6476 struct vcpu_vmx *vmx = to_vmx(vcpu);
6477 enum emulation_result err = EMULATE_DONE;
6478 int ret = 1;
6479 u32 cpu_exec_ctrl;
6480 bool intr_window_requested;
6481 unsigned count = 130;
6482
6483 cpu_exec_ctrl = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
6484 intr_window_requested = cpu_exec_ctrl & CPU_BASED_VIRTUAL_INTR_PENDING;
6485
6486 while (vmx->emulation_required && count-- != 0) {
6487 if (intr_window_requested && vmx_interrupt_allowed(vcpu))
6488 return handle_interrupt_window(&vmx->vcpu);
6489
6490 if (kvm_test_request(KVM_REQ_EVENT, vcpu))
6491 return 1;
6492
6493 err = emulate_instruction(vcpu, EMULTYPE_NO_REEXECUTE);
6494
6495 if (err == EMULATE_USER_EXIT) {
6496 ++vcpu->stat.mmio_exits;
6497 ret = 0;
6498 goto out;
6499 }
6500
6501 if (err != EMULATE_DONE) {
6502 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
6503 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
6504 vcpu->run->internal.ndata = 0;
6505 return 0;
6506 }
6507
6508 if (vcpu->arch.halt_request) {
6509 vcpu->arch.halt_request = 0;
6510 ret = kvm_vcpu_halt(vcpu);
6511 goto out;
6512 }
6513
6514 if (signal_pending(current))
6515 goto out;
6516 if (need_resched())
6517 schedule();
6518 }
6519
6520 out:
6521 return ret;
6522 }
6523
6524 static int __grow_ple_window(int val)
6525 {
6526 if (ple_window_grow < 1)
6527 return ple_window;
6528
6529 val = min(val, ple_window_actual_max);
6530
6531 if (ple_window_grow < ple_window)
6532 val *= ple_window_grow;
6533 else
6534 val += ple_window_grow;
6535
6536 return val;
6537 }
6538
6539 static int __shrink_ple_window(int val, int modifier, int minimum)
6540 {
6541 if (modifier < 1)
6542 return ple_window;
6543
6544 if (modifier < ple_window)
6545 val /= modifier;
6546 else
6547 val -= modifier;
6548
6549 return max(val, minimum);
6550 }
6551
6552 static void grow_ple_window(struct kvm_vcpu *vcpu)
6553 {
6554 struct vcpu_vmx *vmx = to_vmx(vcpu);
6555 int old = vmx->ple_window;
6556
6557 vmx->ple_window = __grow_ple_window(old);
6558
6559 if (vmx->ple_window != old)
6560 vmx->ple_window_dirty = true;
6561
6562 trace_kvm_ple_window_grow(vcpu->vcpu_id, vmx->ple_window, old);
6563 }
6564
6565 static void shrink_ple_window(struct kvm_vcpu *vcpu)
6566 {
6567 struct vcpu_vmx *vmx = to_vmx(vcpu);
6568 int old = vmx->ple_window;
6569
6570 vmx->ple_window = __shrink_ple_window(old,
6571 ple_window_shrink, ple_window);
6572
6573 if (vmx->ple_window != old)
6574 vmx->ple_window_dirty = true;
6575
6576 trace_kvm_ple_window_shrink(vcpu->vcpu_id, vmx->ple_window, old);
6577 }
6578
6579 /*
6580 * ple_window_actual_max is computed to be one grow_ple_window() below
6581 * ple_window_max. (See __grow_ple_window for the reason.)
6582 * This prevents overflows, because ple_window_max is int.
6583 * ple_window_max effectively rounded down to a multiple of ple_window_grow in
6584 * this process.
6585 * ple_window_max is also prevented from setting vmx->ple_window < ple_window.
6586 */
6587 static void update_ple_window_actual_max(void)
6588 {
6589 ple_window_actual_max =
6590 __shrink_ple_window(max(ple_window_max, ple_window),
6591 ple_window_grow, INT_MIN);
6592 }
6593
6594 /*
6595 * Handler for POSTED_INTERRUPT_WAKEUP_VECTOR.
6596 */
6597 static void wakeup_handler(void)
6598 {
6599 struct kvm_vcpu *vcpu;
6600 int cpu = smp_processor_id();
6601
6602 spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, cpu));
6603 list_for_each_entry(vcpu, &per_cpu(blocked_vcpu_on_cpu, cpu),
6604 blocked_vcpu_list) {
6605 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
6606
6607 if (pi_test_on(pi_desc) == 1)
6608 kvm_vcpu_kick(vcpu);
6609 }
6610 spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, cpu));
6611 }
6612
6613 void vmx_enable_tdp(void)
6614 {
6615 kvm_mmu_set_mask_ptes(VMX_EPT_READABLE_MASK,
6616 enable_ept_ad_bits ? VMX_EPT_ACCESS_BIT : 0ull,
6617 enable_ept_ad_bits ? VMX_EPT_DIRTY_BIT : 0ull,
6618 0ull, VMX_EPT_EXECUTABLE_MASK,
6619 cpu_has_vmx_ept_execute_only() ? 0ull : VMX_EPT_READABLE_MASK,
6620 VMX_EPT_RWX_MASK);
6621
6622 ept_set_mmio_spte_mask();
6623 kvm_enable_tdp();
6624 }
6625
6626 static __init int hardware_setup(void)
6627 {
6628 int r = -ENOMEM, i, msr;
6629
6630 rdmsrl_safe(MSR_EFER, &host_efer);
6631
6632 for (i = 0; i < ARRAY_SIZE(vmx_msr_index); ++i)
6633 kvm_define_shared_msr(i, vmx_msr_index[i]);
6634
6635 for (i = 0; i < VMX_BITMAP_NR; i++) {
6636 vmx_bitmap[i] = (unsigned long *)__get_free_page(GFP_KERNEL);
6637 if (!vmx_bitmap[i])
6638 goto out;
6639 }
6640
6641 vmx_io_bitmap_b = (unsigned long *)__get_free_page(GFP_KERNEL);
6642 memset(vmx_vmread_bitmap, 0xff, PAGE_SIZE);
6643 memset(vmx_vmwrite_bitmap, 0xff, PAGE_SIZE);
6644
6645 memset(vmx_io_bitmap_a, 0xff, PAGE_SIZE);
6646
6647 memset(vmx_io_bitmap_b, 0xff, PAGE_SIZE);
6648
6649 memset(vmx_msr_bitmap_legacy, 0xff, PAGE_SIZE);
6650 memset(vmx_msr_bitmap_longmode, 0xff, PAGE_SIZE);
6651
6652 if (setup_vmcs_config(&vmcs_config) < 0) {
6653 r = -EIO;
6654 goto out;
6655 }
6656
6657 if (boot_cpu_has(X86_FEATURE_NX))
6658 kvm_enable_efer_bits(EFER_NX);
6659
6660 if (!cpu_has_vmx_vpid() || !cpu_has_vmx_invvpid() ||
6661 !(cpu_has_vmx_invvpid_single() || cpu_has_vmx_invvpid_global()))
6662 enable_vpid = 0;
6663
6664 if (!cpu_has_vmx_shadow_vmcs())
6665 enable_shadow_vmcs = 0;
6666 if (enable_shadow_vmcs)
6667 init_vmcs_shadow_fields();
6668
6669 if (!cpu_has_vmx_ept() ||
6670 !cpu_has_vmx_ept_4levels()) {
6671 enable_ept = 0;
6672 enable_unrestricted_guest = 0;
6673 enable_ept_ad_bits = 0;
6674 }
6675
6676 if (!cpu_has_vmx_ept_ad_bits() || !enable_ept)
6677 enable_ept_ad_bits = 0;
6678
6679 if (!cpu_has_vmx_unrestricted_guest())
6680 enable_unrestricted_guest = 0;
6681
6682 if (!cpu_has_vmx_flexpriority())
6683 flexpriority_enabled = 0;
6684
6685 /*
6686 * set_apic_access_page_addr() is used to reload apic access
6687 * page upon invalidation. No need to do anything if not
6688 * using the APIC_ACCESS_ADDR VMCS field.
6689 */
6690 if (!flexpriority_enabled)
6691 kvm_x86_ops->set_apic_access_page_addr = NULL;
6692
6693 if (!cpu_has_vmx_tpr_shadow())
6694 kvm_x86_ops->update_cr8_intercept = NULL;
6695
6696 if (enable_ept && !cpu_has_vmx_ept_2m_page())
6697 kvm_disable_largepages();
6698
6699 if (!cpu_has_vmx_ple())
6700 ple_gap = 0;
6701
6702 if (!cpu_has_vmx_apicv()) {
6703 enable_apicv = 0;
6704 kvm_x86_ops->sync_pir_to_irr = NULL;
6705 }
6706
6707 if (cpu_has_vmx_tsc_scaling()) {
6708 kvm_has_tsc_control = true;
6709 kvm_max_tsc_scaling_ratio = KVM_VMX_TSC_MULTIPLIER_MAX;
6710 kvm_tsc_scaling_ratio_frac_bits = 48;
6711 }
6712
6713 vmx_disable_intercept_for_msr(MSR_FS_BASE, false);
6714 vmx_disable_intercept_for_msr(MSR_GS_BASE, false);
6715 vmx_disable_intercept_for_msr(MSR_KERNEL_GS_BASE, true);
6716 vmx_disable_intercept_for_msr(MSR_IA32_SYSENTER_CS, false);
6717 vmx_disable_intercept_for_msr(MSR_IA32_SYSENTER_ESP, false);
6718 vmx_disable_intercept_for_msr(MSR_IA32_SYSENTER_EIP, false);
6719 vmx_disable_intercept_for_msr(MSR_IA32_SPEC_CTRL, false);
6720 vmx_disable_intercept_for_msr(MSR_IA32_PRED_CMD, false);
6721
6722 memcpy(vmx_msr_bitmap_legacy_x2apic_apicv,
6723 vmx_msr_bitmap_legacy, PAGE_SIZE);
6724 memcpy(vmx_msr_bitmap_longmode_x2apic_apicv,
6725 vmx_msr_bitmap_longmode, PAGE_SIZE);
6726 memcpy(vmx_msr_bitmap_legacy_x2apic,
6727 vmx_msr_bitmap_legacy, PAGE_SIZE);
6728 memcpy(vmx_msr_bitmap_longmode_x2apic,
6729 vmx_msr_bitmap_longmode, PAGE_SIZE);
6730
6731 set_bit(0, vmx_vpid_bitmap); /* 0 is reserved for host */
6732
6733 for (msr = 0x800; msr <= 0x8ff; msr++) {
6734 if (msr == 0x839 /* TMCCT */)
6735 continue;
6736 vmx_disable_intercept_msr_x2apic(msr, MSR_TYPE_R, true);
6737 }
6738
6739 /*
6740 * TPR reads and writes can be virtualized even if virtual interrupt
6741 * delivery is not in use.
6742 */
6743 vmx_disable_intercept_msr_x2apic(0x808, MSR_TYPE_W, true);
6744 vmx_disable_intercept_msr_x2apic(0x808, MSR_TYPE_R | MSR_TYPE_W, false);
6745
6746 /* EOI */
6747 vmx_disable_intercept_msr_x2apic(0x80b, MSR_TYPE_W, true);
6748 /* SELF-IPI */
6749 vmx_disable_intercept_msr_x2apic(0x83f, MSR_TYPE_W, true);
6750
6751 if (enable_ept)
6752 vmx_enable_tdp();
6753 else
6754 kvm_disable_tdp();
6755
6756 update_ple_window_actual_max();
6757
6758 /*
6759 * Only enable PML when hardware supports PML feature, and both EPT
6760 * and EPT A/D bit features are enabled -- PML depends on them to work.
6761 */
6762 if (!enable_ept || !enable_ept_ad_bits || !cpu_has_vmx_pml())
6763 enable_pml = 0;
6764
6765 if (!enable_pml) {
6766 kvm_x86_ops->slot_enable_log_dirty = NULL;
6767 kvm_x86_ops->slot_disable_log_dirty = NULL;
6768 kvm_x86_ops->flush_log_dirty = NULL;
6769 kvm_x86_ops->enable_log_dirty_pt_masked = NULL;
6770 }
6771
6772 if (cpu_has_vmx_preemption_timer() && enable_preemption_timer) {
6773 u64 vmx_msr;
6774
6775 rdmsrl(MSR_IA32_VMX_MISC, vmx_msr);
6776 cpu_preemption_timer_multi =
6777 vmx_msr & VMX_MISC_PREEMPTION_TIMER_RATE_MASK;
6778 } else {
6779 kvm_x86_ops->set_hv_timer = NULL;
6780 kvm_x86_ops->cancel_hv_timer = NULL;
6781 }
6782
6783 kvm_set_posted_intr_wakeup_handler(wakeup_handler);
6784
6785 kvm_mce_cap_supported |= MCG_LMCE_P;
6786
6787 return alloc_kvm_area();
6788
6789 out:
6790 for (i = 0; i < VMX_BITMAP_NR; i++)
6791 free_page((unsigned long)vmx_bitmap[i]);
6792
6793 return r;
6794 }
6795
6796 static __exit void hardware_unsetup(void)
6797 {
6798 int i;
6799
6800 for (i = 0; i < VMX_BITMAP_NR; i++)
6801 free_page((unsigned long)vmx_bitmap[i]);
6802
6803 free_kvm_area();
6804 }
6805
6806 /*
6807 * Indicate a busy-waiting vcpu in spinlock. We do not enable the PAUSE
6808 * exiting, so only get here on cpu with PAUSE-Loop-Exiting.
6809 */
6810 static int handle_pause(struct kvm_vcpu *vcpu)
6811 {
6812 if (ple_gap)
6813 grow_ple_window(vcpu);
6814
6815 kvm_vcpu_on_spin(vcpu);
6816 return kvm_skip_emulated_instruction(vcpu);
6817 }
6818
6819 static int handle_nop(struct kvm_vcpu *vcpu)
6820 {
6821 return kvm_skip_emulated_instruction(vcpu);
6822 }
6823
6824 static int handle_mwait(struct kvm_vcpu *vcpu)
6825 {
6826 printk_once(KERN_WARNING "kvm: MWAIT instruction emulated as NOP!\n");
6827 return handle_nop(vcpu);
6828 }
6829
6830 static int handle_monitor_trap(struct kvm_vcpu *vcpu)
6831 {
6832 return 1;
6833 }
6834
6835 static int handle_monitor(struct kvm_vcpu *vcpu)
6836 {
6837 printk_once(KERN_WARNING "kvm: MONITOR instruction emulated as NOP!\n");
6838 return handle_nop(vcpu);
6839 }
6840
6841 /*
6842 * To run an L2 guest, we need a vmcs02 based on the L1-specified vmcs12.
6843 * We could reuse a single VMCS for all the L2 guests, but we also want the
6844 * option to allocate a separate vmcs02 for each separate loaded vmcs12 - this
6845 * allows keeping them loaded on the processor, and in the future will allow
6846 * optimizations where prepare_vmcs02 doesn't need to set all the fields on
6847 * every entry if they never change.
6848 * So we keep, in vmx->nested.vmcs02_pool, a cache of size VMCS02_POOL_SIZE
6849 * (>=0) with a vmcs02 for each recently loaded vmcs12s, most recent first.
6850 *
6851 * The following functions allocate and free a vmcs02 in this pool.
6852 */
6853
6854 /* Get a VMCS from the pool to use as vmcs02 for the current vmcs12. */
6855 static struct loaded_vmcs *nested_get_current_vmcs02(struct vcpu_vmx *vmx)
6856 {
6857 struct vmcs02_list *item;
6858 list_for_each_entry(item, &vmx->nested.vmcs02_pool, list)
6859 if (item->vmptr == vmx->nested.current_vmptr) {
6860 list_move(&item->list, &vmx->nested.vmcs02_pool);
6861 return &item->vmcs02;
6862 }
6863
6864 if (vmx->nested.vmcs02_num >= max(VMCS02_POOL_SIZE, 1)) {
6865 /* Recycle the least recently used VMCS. */
6866 item = list_last_entry(&vmx->nested.vmcs02_pool,
6867 struct vmcs02_list, list);
6868 item->vmptr = vmx->nested.current_vmptr;
6869 list_move(&item->list, &vmx->nested.vmcs02_pool);
6870 return &item->vmcs02;
6871 }
6872
6873 /* Create a new VMCS */
6874 item = kzalloc(sizeof(struct vmcs02_list), GFP_KERNEL);
6875 if (!item)
6876 return NULL;
6877 item->vmcs02.vmcs = alloc_vmcs();
6878 item->vmcs02.shadow_vmcs = NULL;
6879 if (!item->vmcs02.vmcs) {
6880 kfree(item);
6881 return NULL;
6882 }
6883 loaded_vmcs_init(&item->vmcs02);
6884 item->vmptr = vmx->nested.current_vmptr;
6885 list_add(&(item->list), &(vmx->nested.vmcs02_pool));
6886 vmx->nested.vmcs02_num++;
6887 return &item->vmcs02;
6888 }
6889
6890 /* Free and remove from pool a vmcs02 saved for a vmcs12 (if there is one) */
6891 static void nested_free_vmcs02(struct vcpu_vmx *vmx, gpa_t vmptr)
6892 {
6893 struct vmcs02_list *item;
6894 list_for_each_entry(item, &vmx->nested.vmcs02_pool, list)
6895 if (item->vmptr == vmptr) {
6896 free_loaded_vmcs(&item->vmcs02);
6897 list_del(&item->list);
6898 kfree(item);
6899 vmx->nested.vmcs02_num--;
6900 return;
6901 }
6902 }
6903
6904 /*
6905 * Free all VMCSs saved for this vcpu, except the one pointed by
6906 * vmx->loaded_vmcs. We must be running L1, so vmx->loaded_vmcs
6907 * must be &vmx->vmcs01.
6908 */
6909 static void nested_free_all_saved_vmcss(struct vcpu_vmx *vmx)
6910 {
6911 struct vmcs02_list *item, *n;
6912
6913 WARN_ON(vmx->loaded_vmcs != &vmx->vmcs01);
6914 list_for_each_entry_safe(item, n, &vmx->nested.vmcs02_pool, list) {
6915 /*
6916 * Something will leak if the above WARN triggers. Better than
6917 * a use-after-free.
6918 */
6919 if (vmx->loaded_vmcs == &item->vmcs02)
6920 continue;
6921
6922 free_loaded_vmcs(&item->vmcs02);
6923 list_del(&item->list);
6924 kfree(item);
6925 vmx->nested.vmcs02_num--;
6926 }
6927 }
6928
6929 /*
6930 * The following 3 functions, nested_vmx_succeed()/failValid()/failInvalid(),
6931 * set the success or error code of an emulated VMX instruction, as specified
6932 * by Vol 2B, VMX Instruction Reference, "Conventions".
6933 */
6934 static void nested_vmx_succeed(struct kvm_vcpu *vcpu)
6935 {
6936 vmx_set_rflags(vcpu, vmx_get_rflags(vcpu)
6937 & ~(X86_EFLAGS_CF | X86_EFLAGS_PF | X86_EFLAGS_AF |
6938 X86_EFLAGS_ZF | X86_EFLAGS_SF | X86_EFLAGS_OF));
6939 }
6940
6941 static void nested_vmx_failInvalid(struct kvm_vcpu *vcpu)
6942 {
6943 vmx_set_rflags(vcpu, (vmx_get_rflags(vcpu)
6944 & ~(X86_EFLAGS_PF | X86_EFLAGS_AF | X86_EFLAGS_ZF |
6945 X86_EFLAGS_SF | X86_EFLAGS_OF))
6946 | X86_EFLAGS_CF);
6947 }
6948
6949 static void nested_vmx_failValid(struct kvm_vcpu *vcpu,
6950 u32 vm_instruction_error)
6951 {
6952 if (to_vmx(vcpu)->nested.current_vmptr == -1ull) {
6953 /*
6954 * failValid writes the error number to the current VMCS, which
6955 * can't be done there isn't a current VMCS.
6956 */
6957 nested_vmx_failInvalid(vcpu);
6958 return;
6959 }
6960 vmx_set_rflags(vcpu, (vmx_get_rflags(vcpu)
6961 & ~(X86_EFLAGS_CF | X86_EFLAGS_PF | X86_EFLAGS_AF |
6962 X86_EFLAGS_SF | X86_EFLAGS_OF))
6963 | X86_EFLAGS_ZF);
6964 get_vmcs12(vcpu)->vm_instruction_error = vm_instruction_error;
6965 /*
6966 * We don't need to force a shadow sync because
6967 * VM_INSTRUCTION_ERROR is not shadowed
6968 */
6969 }
6970
6971 static void nested_vmx_abort(struct kvm_vcpu *vcpu, u32 indicator)
6972 {
6973 /* TODO: not to reset guest simply here. */
6974 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
6975 pr_debug_ratelimited("kvm: nested vmx abort, indicator %d\n", indicator);
6976 }
6977
6978 static enum hrtimer_restart vmx_preemption_timer_fn(struct hrtimer *timer)
6979 {
6980 struct vcpu_vmx *vmx =
6981 container_of(timer, struct vcpu_vmx, nested.preemption_timer);
6982
6983 vmx->nested.preemption_timer_expired = true;
6984 kvm_make_request(KVM_REQ_EVENT, &vmx->vcpu);
6985 kvm_vcpu_kick(&vmx->vcpu);
6986
6987 return HRTIMER_NORESTART;
6988 }
6989
6990 /*
6991 * Decode the memory-address operand of a vmx instruction, as recorded on an
6992 * exit caused by such an instruction (run by a guest hypervisor).
6993 * On success, returns 0. When the operand is invalid, returns 1 and throws
6994 * #UD or #GP.
6995 */
6996 static int get_vmx_mem_address(struct kvm_vcpu *vcpu,
6997 unsigned long exit_qualification,
6998 u32 vmx_instruction_info, bool wr, gva_t *ret)
6999 {
7000 gva_t off;
7001 bool exn;
7002 struct kvm_segment s;
7003
7004 /*
7005 * According to Vol. 3B, "Information for VM Exits Due to Instruction
7006 * Execution", on an exit, vmx_instruction_info holds most of the
7007 * addressing components of the operand. Only the displacement part
7008 * is put in exit_qualification (see 3B, "Basic VM-Exit Information").
7009 * For how an actual address is calculated from all these components,
7010 * refer to Vol. 1, "Operand Addressing".
7011 */
7012 int scaling = vmx_instruction_info & 3;
7013 int addr_size = (vmx_instruction_info >> 7) & 7;
7014 bool is_reg = vmx_instruction_info & (1u << 10);
7015 int seg_reg = (vmx_instruction_info >> 15) & 7;
7016 int index_reg = (vmx_instruction_info >> 18) & 0xf;
7017 bool index_is_valid = !(vmx_instruction_info & (1u << 22));
7018 int base_reg = (vmx_instruction_info >> 23) & 0xf;
7019 bool base_is_valid = !(vmx_instruction_info & (1u << 27));
7020
7021 if (is_reg) {
7022 kvm_queue_exception(vcpu, UD_VECTOR);
7023 return 1;
7024 }
7025
7026 /* Addr = segment_base + offset */
7027 /* offset = base + [index * scale] + displacement */
7028 off = exit_qualification; /* holds the displacement */
7029 if (base_is_valid)
7030 off += kvm_register_read(vcpu, base_reg);
7031 if (index_is_valid)
7032 off += kvm_register_read(vcpu, index_reg)<<scaling;
7033 vmx_get_segment(vcpu, &s, seg_reg);
7034 *ret = s.base + off;
7035
7036 if (addr_size == 1) /* 32 bit */
7037 *ret &= 0xffffffff;
7038
7039 /* Checks for #GP/#SS exceptions. */
7040 exn = false;
7041 if (is_long_mode(vcpu)) {
7042 /* Long mode: #GP(0)/#SS(0) if the memory address is in a
7043 * non-canonical form. This is the only check on the memory
7044 * destination for long mode!
7045 */
7046 exn = is_noncanonical_address(*ret);
7047 } else if (is_protmode(vcpu)) {
7048 /* Protected mode: apply checks for segment validity in the
7049 * following order:
7050 * - segment type check (#GP(0) may be thrown)
7051 * - usability check (#GP(0)/#SS(0))
7052 * - limit check (#GP(0)/#SS(0))
7053 */
7054 if (wr)
7055 /* #GP(0) if the destination operand is located in a
7056 * read-only data segment or any code segment.
7057 */
7058 exn = ((s.type & 0xa) == 0 || (s.type & 8));
7059 else
7060 /* #GP(0) if the source operand is located in an
7061 * execute-only code segment
7062 */
7063 exn = ((s.type & 0xa) == 8);
7064 if (exn) {
7065 kvm_queue_exception_e(vcpu, GP_VECTOR, 0);
7066 return 1;
7067 }
7068 /* Protected mode: #GP(0)/#SS(0) if the segment is unusable.
7069 */
7070 exn = (s.unusable != 0);
7071 /* Protected mode: #GP(0)/#SS(0) if the memory
7072 * operand is outside the segment limit.
7073 */
7074 exn = exn || (off + sizeof(u64) > s.limit);
7075 }
7076 if (exn) {
7077 kvm_queue_exception_e(vcpu,
7078 seg_reg == VCPU_SREG_SS ?
7079 SS_VECTOR : GP_VECTOR,
7080 0);
7081 return 1;
7082 }
7083
7084 return 0;
7085 }
7086
7087 static int nested_vmx_get_vmptr(struct kvm_vcpu *vcpu, gpa_t *vmpointer)
7088 {
7089 gva_t gva;
7090 struct x86_exception e;
7091
7092 if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
7093 vmcs_read32(VMX_INSTRUCTION_INFO), false, &gva))
7094 return 1;
7095
7096 if (kvm_read_guest_virt(&vcpu->arch.emulate_ctxt, gva, vmpointer,
7097 sizeof(*vmpointer), &e)) {
7098 kvm_inject_page_fault(vcpu, &e);
7099 return 1;
7100 }
7101
7102 return 0;
7103 }
7104
7105 static int enter_vmx_operation(struct kvm_vcpu *vcpu)
7106 {
7107 struct vcpu_vmx *vmx = to_vmx(vcpu);
7108 struct vmcs *shadow_vmcs;
7109
7110 if (cpu_has_vmx_msr_bitmap()) {
7111 vmx->nested.msr_bitmap =
7112 (unsigned long *)__get_free_page(GFP_KERNEL);
7113 if (!vmx->nested.msr_bitmap)
7114 goto out_msr_bitmap;
7115 }
7116
7117 vmx->nested.cached_vmcs12 = kmalloc(VMCS12_SIZE, GFP_KERNEL);
7118 if (!vmx->nested.cached_vmcs12)
7119 goto out_cached_vmcs12;
7120
7121 if (enable_shadow_vmcs) {
7122 shadow_vmcs = alloc_vmcs();
7123 if (!shadow_vmcs)
7124 goto out_shadow_vmcs;
7125 /* mark vmcs as shadow */
7126 shadow_vmcs->revision_id |= (1u << 31);
7127 /* init shadow vmcs */
7128 vmcs_clear(shadow_vmcs);
7129 vmx->vmcs01.shadow_vmcs = shadow_vmcs;
7130 }
7131
7132 INIT_LIST_HEAD(&(vmx->nested.vmcs02_pool));
7133 vmx->nested.vmcs02_num = 0;
7134
7135 hrtimer_init(&vmx->nested.preemption_timer, CLOCK_MONOTONIC,
7136 HRTIMER_MODE_REL_PINNED);
7137 vmx->nested.preemption_timer.function = vmx_preemption_timer_fn;
7138
7139 vmx->nested.vmxon = true;
7140 return 0;
7141
7142 out_shadow_vmcs:
7143 kfree(vmx->nested.cached_vmcs12);
7144
7145 out_cached_vmcs12:
7146 free_page((unsigned long)vmx->nested.msr_bitmap);
7147
7148 out_msr_bitmap:
7149 return -ENOMEM;
7150 }
7151
7152 /*
7153 * Emulate the VMXON instruction.
7154 * Currently, we just remember that VMX is active, and do not save or even
7155 * inspect the argument to VMXON (the so-called "VMXON pointer") because we
7156 * do not currently need to store anything in that guest-allocated memory
7157 * region. Consequently, VMCLEAR and VMPTRLD also do not verify that the their
7158 * argument is different from the VMXON pointer (which the spec says they do).
7159 */
7160 static int handle_vmon(struct kvm_vcpu *vcpu)
7161 {
7162 int ret;
7163 gpa_t vmptr;
7164 struct page *page;
7165 struct vcpu_vmx *vmx = to_vmx(vcpu);
7166 const u64 VMXON_NEEDED_FEATURES = FEATURE_CONTROL_LOCKED
7167 | FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
7168
7169 /*
7170 * The Intel VMX Instruction Reference lists a bunch of bits that are
7171 * prerequisite to running VMXON, most notably cr4.VMXE must be set to
7172 * 1 (see vmx_set_cr4() for when we allow the guest to set this).
7173 * Otherwise, we should fail with #UD. But most faulting conditions
7174 * have already been checked by hardware, prior to the VM-exit for
7175 * VMXON. We do test guest cr4.VMXE because processor CR4 always has
7176 * that bit set to 1 in non-root mode.
7177 */
7178 if (!kvm_read_cr4_bits(vcpu, X86_CR4_VMXE)) {
7179 kvm_queue_exception(vcpu, UD_VECTOR);
7180 return 1;
7181 }
7182
7183 if (vmx->nested.vmxon) {
7184 nested_vmx_failValid(vcpu, VMXERR_VMXON_IN_VMX_ROOT_OPERATION);
7185 return kvm_skip_emulated_instruction(vcpu);
7186 }
7187
7188 if ((vmx->msr_ia32_feature_control & VMXON_NEEDED_FEATURES)
7189 != VMXON_NEEDED_FEATURES) {
7190 kvm_inject_gp(vcpu, 0);
7191 return 1;
7192 }
7193
7194 if (nested_vmx_get_vmptr(vcpu, &vmptr))
7195 return 1;
7196
7197 /*
7198 * SDM 3: 24.11.5
7199 * The first 4 bytes of VMXON region contain the supported
7200 * VMCS revision identifier
7201 *
7202 * Note - IA32_VMX_BASIC[48] will never be 1 for the nested case;
7203 * which replaces physical address width with 32
7204 */
7205 if (!PAGE_ALIGNED(vmptr) || (vmptr >> cpuid_maxphyaddr(vcpu))) {
7206 nested_vmx_failInvalid(vcpu);
7207 return kvm_skip_emulated_instruction(vcpu);
7208 }
7209
7210 page = nested_get_page(vcpu, vmptr);
7211 if (page == NULL) {
7212 nested_vmx_failInvalid(vcpu);
7213 return kvm_skip_emulated_instruction(vcpu);
7214 }
7215 if (*(u32 *)kmap(page) != VMCS12_REVISION) {
7216 kunmap(page);
7217 nested_release_page_clean(page);
7218 nested_vmx_failInvalid(vcpu);
7219 return kvm_skip_emulated_instruction(vcpu);
7220 }
7221 kunmap(page);
7222 nested_release_page_clean(page);
7223
7224 vmx->nested.vmxon_ptr = vmptr;
7225 ret = enter_vmx_operation(vcpu);
7226 if (ret)
7227 return ret;
7228
7229 nested_vmx_succeed(vcpu);
7230 return kvm_skip_emulated_instruction(vcpu);
7231 }
7232
7233 /*
7234 * Intel's VMX Instruction Reference specifies a common set of prerequisites
7235 * for running VMX instructions (except VMXON, whose prerequisites are
7236 * slightly different). It also specifies what exception to inject otherwise.
7237 * Note that many of these exceptions have priority over VM exits, so they
7238 * don't have to be checked again here.
7239 */
7240 static int nested_vmx_check_permission(struct kvm_vcpu *vcpu)
7241 {
7242 if (!to_vmx(vcpu)->nested.vmxon) {
7243 kvm_queue_exception(vcpu, UD_VECTOR);
7244 return 0;
7245 }
7246 return 1;
7247 }
7248
7249 static void vmx_disable_shadow_vmcs(struct vcpu_vmx *vmx)
7250 {
7251 vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL, SECONDARY_EXEC_SHADOW_VMCS);
7252 vmcs_write64(VMCS_LINK_POINTER, -1ull);
7253 }
7254
7255 static inline void nested_release_vmcs12(struct vcpu_vmx *vmx)
7256 {
7257 if (vmx->nested.current_vmptr == -1ull)
7258 return;
7259
7260 if (enable_shadow_vmcs) {
7261 /* copy to memory all shadowed fields in case
7262 they were modified */
7263 copy_shadow_to_vmcs12(vmx);
7264 vmx->nested.sync_shadow_vmcs = false;
7265 vmx_disable_shadow_vmcs(vmx);
7266 }
7267 vmx->nested.posted_intr_nv = -1;
7268
7269 /* Flush VMCS12 to guest memory */
7270 kvm_vcpu_write_guest_page(&vmx->vcpu,
7271 vmx->nested.current_vmptr >> PAGE_SHIFT,
7272 vmx->nested.cached_vmcs12, 0, VMCS12_SIZE);
7273
7274 vmx->nested.current_vmptr = -1ull;
7275 }
7276
7277 /*
7278 * Free whatever needs to be freed from vmx->nested when L1 goes down, or
7279 * just stops using VMX.
7280 */
7281 static void free_nested(struct vcpu_vmx *vmx)
7282 {
7283 if (!vmx->nested.vmxon)
7284 return;
7285
7286 vmx->nested.vmxon = false;
7287 free_vpid(vmx->nested.vpid02);
7288 vmx->nested.posted_intr_nv = -1;
7289 vmx->nested.current_vmptr = -1ull;
7290 if (vmx->nested.msr_bitmap) {
7291 free_page((unsigned long)vmx->nested.msr_bitmap);
7292 vmx->nested.msr_bitmap = NULL;
7293 }
7294 if (enable_shadow_vmcs) {
7295 vmx_disable_shadow_vmcs(vmx);
7296 vmcs_clear(vmx->vmcs01.shadow_vmcs);
7297 free_vmcs(vmx->vmcs01.shadow_vmcs);
7298 vmx->vmcs01.shadow_vmcs = NULL;
7299 }
7300 kfree(vmx->nested.cached_vmcs12);
7301 /* Unpin physical memory we referred to in current vmcs02 */
7302 if (vmx->nested.apic_access_page) {
7303 nested_release_page(vmx->nested.apic_access_page);
7304 vmx->nested.apic_access_page = NULL;
7305 }
7306 if (vmx->nested.virtual_apic_page) {
7307 nested_release_page(vmx->nested.virtual_apic_page);
7308 vmx->nested.virtual_apic_page = NULL;
7309 }
7310 if (vmx->nested.pi_desc_page) {
7311 kunmap(vmx->nested.pi_desc_page);
7312 nested_release_page(vmx->nested.pi_desc_page);
7313 vmx->nested.pi_desc_page = NULL;
7314 vmx->nested.pi_desc = NULL;
7315 }
7316
7317 nested_free_all_saved_vmcss(vmx);
7318 }
7319
7320 /* Emulate the VMXOFF instruction */
7321 static int handle_vmoff(struct kvm_vcpu *vcpu)
7322 {
7323 if (!nested_vmx_check_permission(vcpu))
7324 return 1;
7325 free_nested(to_vmx(vcpu));
7326 nested_vmx_succeed(vcpu);
7327 return kvm_skip_emulated_instruction(vcpu);
7328 }
7329
7330 /* Emulate the VMCLEAR instruction */
7331 static int handle_vmclear(struct kvm_vcpu *vcpu)
7332 {
7333 struct vcpu_vmx *vmx = to_vmx(vcpu);
7334 u32 zero = 0;
7335 gpa_t vmptr;
7336
7337 if (!nested_vmx_check_permission(vcpu))
7338 return 1;
7339
7340 if (nested_vmx_get_vmptr(vcpu, &vmptr))
7341 return 1;
7342
7343 if (!PAGE_ALIGNED(vmptr) || (vmptr >> cpuid_maxphyaddr(vcpu))) {
7344 nested_vmx_failValid(vcpu, VMXERR_VMCLEAR_INVALID_ADDRESS);
7345 return kvm_skip_emulated_instruction(vcpu);
7346 }
7347
7348 if (vmptr == vmx->nested.vmxon_ptr) {
7349 nested_vmx_failValid(vcpu, VMXERR_VMCLEAR_VMXON_POINTER);
7350 return kvm_skip_emulated_instruction(vcpu);
7351 }
7352
7353 if (vmptr == vmx->nested.current_vmptr)
7354 nested_release_vmcs12(vmx);
7355
7356 kvm_vcpu_write_guest(vcpu,
7357 vmptr + offsetof(struct vmcs12, launch_state),
7358 &zero, sizeof(zero));
7359
7360 nested_free_vmcs02(vmx, vmptr);
7361
7362 nested_vmx_succeed(vcpu);
7363 return kvm_skip_emulated_instruction(vcpu);
7364 }
7365
7366 static int nested_vmx_run(struct kvm_vcpu *vcpu, bool launch);
7367
7368 /* Emulate the VMLAUNCH instruction */
7369 static int handle_vmlaunch(struct kvm_vcpu *vcpu)
7370 {
7371 return nested_vmx_run(vcpu, true);
7372 }
7373
7374 /* Emulate the VMRESUME instruction */
7375 static int handle_vmresume(struct kvm_vcpu *vcpu)
7376 {
7377
7378 return nested_vmx_run(vcpu, false);
7379 }
7380
7381 /*
7382 * Read a vmcs12 field. Since these can have varying lengths and we return
7383 * one type, we chose the biggest type (u64) and zero-extend the return value
7384 * to that size. Note that the caller, handle_vmread, might need to use only
7385 * some of the bits we return here (e.g., on 32-bit guests, only 32 bits of
7386 * 64-bit fields are to be returned).
7387 */
7388 static inline int vmcs12_read_any(struct kvm_vcpu *vcpu,
7389 unsigned long field, u64 *ret)
7390 {
7391 short offset = vmcs_field_to_offset(field);
7392 char *p;
7393
7394 if (offset < 0)
7395 return offset;
7396
7397 p = ((char *)(get_vmcs12(vcpu))) + offset;
7398
7399 switch (vmcs_field_type(field)) {
7400 case VMCS_FIELD_TYPE_NATURAL_WIDTH:
7401 *ret = *((natural_width *)p);
7402 return 0;
7403 case VMCS_FIELD_TYPE_U16:
7404 *ret = *((u16 *)p);
7405 return 0;
7406 case VMCS_FIELD_TYPE_U32:
7407 *ret = *((u32 *)p);
7408 return 0;
7409 case VMCS_FIELD_TYPE_U64:
7410 *ret = *((u64 *)p);
7411 return 0;
7412 default:
7413 WARN_ON(1);
7414 return -ENOENT;
7415 }
7416 }
7417
7418
7419 static inline int vmcs12_write_any(struct kvm_vcpu *vcpu,
7420 unsigned long field, u64 field_value){
7421 short offset = vmcs_field_to_offset(field);
7422 char *p = ((char *) get_vmcs12(vcpu)) + offset;
7423 if (offset < 0)
7424 return offset;
7425
7426 switch (vmcs_field_type(field)) {
7427 case VMCS_FIELD_TYPE_U16:
7428 *(u16 *)p = field_value;
7429 return 0;
7430 case VMCS_FIELD_TYPE_U32:
7431 *(u32 *)p = field_value;
7432 return 0;
7433 case VMCS_FIELD_TYPE_U64:
7434 *(u64 *)p = field_value;
7435 return 0;
7436 case VMCS_FIELD_TYPE_NATURAL_WIDTH:
7437 *(natural_width *)p = field_value;
7438 return 0;
7439 default:
7440 WARN_ON(1);
7441 return -ENOENT;
7442 }
7443
7444 }
7445
7446 static void copy_shadow_to_vmcs12(struct vcpu_vmx *vmx)
7447 {
7448 int i;
7449 unsigned long field;
7450 u64 field_value;
7451 struct vmcs *shadow_vmcs = vmx->vmcs01.shadow_vmcs;
7452 const unsigned long *fields = shadow_read_write_fields;
7453 const int num_fields = max_shadow_read_write_fields;
7454
7455 preempt_disable();
7456
7457 vmcs_load(shadow_vmcs);
7458
7459 for (i = 0; i < num_fields; i++) {
7460 field = fields[i];
7461 switch (vmcs_field_type(field)) {
7462 case VMCS_FIELD_TYPE_U16:
7463 field_value = vmcs_read16(field);
7464 break;
7465 case VMCS_FIELD_TYPE_U32:
7466 field_value = vmcs_read32(field);
7467 break;
7468 case VMCS_FIELD_TYPE_U64:
7469 field_value = vmcs_read64(field);
7470 break;
7471 case VMCS_FIELD_TYPE_NATURAL_WIDTH:
7472 field_value = vmcs_readl(field);
7473 break;
7474 default:
7475 WARN_ON(1);
7476 continue;
7477 }
7478 vmcs12_write_any(&vmx->vcpu, field, field_value);
7479 }
7480
7481 vmcs_clear(shadow_vmcs);
7482 vmcs_load(vmx->loaded_vmcs->vmcs);
7483
7484 preempt_enable();
7485 }
7486
7487 static void copy_vmcs12_to_shadow(struct vcpu_vmx *vmx)
7488 {
7489 const unsigned long *fields[] = {
7490 shadow_read_write_fields,
7491 shadow_read_only_fields
7492 };
7493 const int max_fields[] = {
7494 max_shadow_read_write_fields,
7495 max_shadow_read_only_fields
7496 };
7497 int i, q;
7498 unsigned long field;
7499 u64 field_value = 0;
7500 struct vmcs *shadow_vmcs = vmx->vmcs01.shadow_vmcs;
7501
7502 vmcs_load(shadow_vmcs);
7503
7504 for (q = 0; q < ARRAY_SIZE(fields); q++) {
7505 for (i = 0; i < max_fields[q]; i++) {
7506 field = fields[q][i];
7507 vmcs12_read_any(&vmx->vcpu, field, &field_value);
7508
7509 switch (vmcs_field_type(field)) {
7510 case VMCS_FIELD_TYPE_U16:
7511 vmcs_write16(field, (u16)field_value);
7512 break;
7513 case VMCS_FIELD_TYPE_U32:
7514 vmcs_write32(field, (u32)field_value);
7515 break;
7516 case VMCS_FIELD_TYPE_U64:
7517 vmcs_write64(field, (u64)field_value);
7518 break;
7519 case VMCS_FIELD_TYPE_NATURAL_WIDTH:
7520 vmcs_writel(field, (long)field_value);
7521 break;
7522 default:
7523 WARN_ON(1);
7524 break;
7525 }
7526 }
7527 }
7528
7529 vmcs_clear(shadow_vmcs);
7530 vmcs_load(vmx->loaded_vmcs->vmcs);
7531 }
7532
7533 /*
7534 * VMX instructions which assume a current vmcs12 (i.e., that VMPTRLD was
7535 * used before) all generate the same failure when it is missing.
7536 */
7537 static int nested_vmx_check_vmcs12(struct kvm_vcpu *vcpu)
7538 {
7539 struct vcpu_vmx *vmx = to_vmx(vcpu);
7540 if (vmx->nested.current_vmptr == -1ull) {
7541 nested_vmx_failInvalid(vcpu);
7542 return 0;
7543 }
7544 return 1;
7545 }
7546
7547 static int handle_vmread(struct kvm_vcpu *vcpu)
7548 {
7549 unsigned long field;
7550 u64 field_value;
7551 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7552 u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
7553 gva_t gva = 0;
7554
7555 if (!nested_vmx_check_permission(vcpu))
7556 return 1;
7557
7558 if (!nested_vmx_check_vmcs12(vcpu))
7559 return kvm_skip_emulated_instruction(vcpu);
7560
7561 /* Decode instruction info and find the field to read */
7562 field = kvm_register_readl(vcpu, (((vmx_instruction_info) >> 28) & 0xf));
7563 /* Read the field, zero-extended to a u64 field_value */
7564 if (vmcs12_read_any(vcpu, field, &field_value) < 0) {
7565 nested_vmx_failValid(vcpu, VMXERR_UNSUPPORTED_VMCS_COMPONENT);
7566 return kvm_skip_emulated_instruction(vcpu);
7567 }
7568 /*
7569 * Now copy part of this value to register or memory, as requested.
7570 * Note that the number of bits actually copied is 32 or 64 depending
7571 * on the guest's mode (32 or 64 bit), not on the given field's length.
7572 */
7573 if (vmx_instruction_info & (1u << 10)) {
7574 kvm_register_writel(vcpu, (((vmx_instruction_info) >> 3) & 0xf),
7575 field_value);
7576 } else {
7577 if (get_vmx_mem_address(vcpu, exit_qualification,
7578 vmx_instruction_info, true, &gva))
7579 return 1;
7580 /* _system ok, as hardware has verified cpl=0 */
7581 kvm_write_guest_virt_system(&vcpu->arch.emulate_ctxt, gva,
7582 &field_value, (is_long_mode(vcpu) ? 8 : 4), NULL);
7583 }
7584
7585 nested_vmx_succeed(vcpu);
7586 return kvm_skip_emulated_instruction(vcpu);
7587 }
7588
7589
7590 static int handle_vmwrite(struct kvm_vcpu *vcpu)
7591 {
7592 unsigned long field;
7593 gva_t gva;
7594 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7595 u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
7596 /* The value to write might be 32 or 64 bits, depending on L1's long
7597 * mode, and eventually we need to write that into a field of several
7598 * possible lengths. The code below first zero-extends the value to 64
7599 * bit (field_value), and then copies only the appropriate number of
7600 * bits into the vmcs12 field.
7601 */
7602 u64 field_value = 0;
7603 struct x86_exception e;
7604
7605 if (!nested_vmx_check_permission(vcpu))
7606 return 1;
7607
7608 if (!nested_vmx_check_vmcs12(vcpu))
7609 return kvm_skip_emulated_instruction(vcpu);
7610
7611 if (vmx_instruction_info & (1u << 10))
7612 field_value = kvm_register_readl(vcpu,
7613 (((vmx_instruction_info) >> 3) & 0xf));
7614 else {
7615 if (get_vmx_mem_address(vcpu, exit_qualification,
7616 vmx_instruction_info, false, &gva))
7617 return 1;
7618 if (kvm_read_guest_virt(&vcpu->arch.emulate_ctxt, gva,
7619 &field_value, (is_64_bit_mode(vcpu) ? 8 : 4), &e)) {
7620 kvm_inject_page_fault(vcpu, &e);
7621 return 1;
7622 }
7623 }
7624
7625
7626 field = kvm_register_readl(vcpu, (((vmx_instruction_info) >> 28) & 0xf));
7627 if (vmcs_field_readonly(field)) {
7628 nested_vmx_failValid(vcpu,
7629 VMXERR_VMWRITE_READ_ONLY_VMCS_COMPONENT);
7630 return kvm_skip_emulated_instruction(vcpu);
7631 }
7632
7633 if (vmcs12_write_any(vcpu, field, field_value) < 0) {
7634 nested_vmx_failValid(vcpu, VMXERR_UNSUPPORTED_VMCS_COMPONENT);
7635 return kvm_skip_emulated_instruction(vcpu);
7636 }
7637
7638 nested_vmx_succeed(vcpu);
7639 return kvm_skip_emulated_instruction(vcpu);
7640 }
7641
7642 static void set_current_vmptr(struct vcpu_vmx *vmx, gpa_t vmptr)
7643 {
7644 vmx->nested.current_vmptr = vmptr;
7645 if (enable_shadow_vmcs) {
7646 vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL,
7647 SECONDARY_EXEC_SHADOW_VMCS);
7648 vmcs_write64(VMCS_LINK_POINTER,
7649 __pa(vmx->vmcs01.shadow_vmcs));
7650 vmx->nested.sync_shadow_vmcs = true;
7651 }
7652 }
7653
7654 /* Emulate the VMPTRLD instruction */
7655 static int handle_vmptrld(struct kvm_vcpu *vcpu)
7656 {
7657 struct vcpu_vmx *vmx = to_vmx(vcpu);
7658 gpa_t vmptr;
7659
7660 if (!nested_vmx_check_permission(vcpu))
7661 return 1;
7662
7663 if (nested_vmx_get_vmptr(vcpu, &vmptr))
7664 return 1;
7665
7666 if (!PAGE_ALIGNED(vmptr) || (vmptr >> cpuid_maxphyaddr(vcpu))) {
7667 nested_vmx_failValid(vcpu, VMXERR_VMPTRLD_INVALID_ADDRESS);
7668 return kvm_skip_emulated_instruction(vcpu);
7669 }
7670
7671 if (vmptr == vmx->nested.vmxon_ptr) {
7672 nested_vmx_failValid(vcpu, VMXERR_VMPTRLD_VMXON_POINTER);
7673 return kvm_skip_emulated_instruction(vcpu);
7674 }
7675
7676 if (vmx->nested.current_vmptr != vmptr) {
7677 struct vmcs12 *new_vmcs12;
7678 struct page *page;
7679 page = nested_get_page(vcpu, vmptr);
7680 if (page == NULL) {
7681 nested_vmx_failInvalid(vcpu);
7682 return kvm_skip_emulated_instruction(vcpu);
7683 }
7684 new_vmcs12 = kmap(page);
7685 if (new_vmcs12->revision_id != VMCS12_REVISION) {
7686 kunmap(page);
7687 nested_release_page_clean(page);
7688 nested_vmx_failValid(vcpu,
7689 VMXERR_VMPTRLD_INCORRECT_VMCS_REVISION_ID);
7690 return kvm_skip_emulated_instruction(vcpu);
7691 }
7692
7693 nested_release_vmcs12(vmx);
7694 /*
7695 * Load VMCS12 from guest memory since it is not already
7696 * cached.
7697 */
7698 memcpy(vmx->nested.cached_vmcs12, new_vmcs12, VMCS12_SIZE);
7699 kunmap(page);
7700 nested_release_page_clean(page);
7701
7702 set_current_vmptr(vmx, vmptr);
7703 }
7704
7705 nested_vmx_succeed(vcpu);
7706 return kvm_skip_emulated_instruction(vcpu);
7707 }
7708
7709 /* Emulate the VMPTRST instruction */
7710 static int handle_vmptrst(struct kvm_vcpu *vcpu)
7711 {
7712 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7713 u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
7714 gva_t vmcs_gva;
7715 struct x86_exception e;
7716
7717 if (!nested_vmx_check_permission(vcpu))
7718 return 1;
7719
7720 if (get_vmx_mem_address(vcpu, exit_qualification,
7721 vmx_instruction_info, true, &vmcs_gva))
7722 return 1;
7723 /* ok to use *_system, as hardware has verified cpl=0 */
7724 if (kvm_write_guest_virt_system(&vcpu->arch.emulate_ctxt, vmcs_gva,
7725 (void *)&to_vmx(vcpu)->nested.current_vmptr,
7726 sizeof(u64), &e)) {
7727 kvm_inject_page_fault(vcpu, &e);
7728 return 1;
7729 }
7730 nested_vmx_succeed(vcpu);
7731 return kvm_skip_emulated_instruction(vcpu);
7732 }
7733
7734 /* Emulate the INVEPT instruction */
7735 static int handle_invept(struct kvm_vcpu *vcpu)
7736 {
7737 struct vcpu_vmx *vmx = to_vmx(vcpu);
7738 u32 vmx_instruction_info, types;
7739 unsigned long type;
7740 gva_t gva;
7741 struct x86_exception e;
7742 struct {
7743 u64 eptp, gpa;
7744 } operand;
7745
7746 if (!(vmx->nested.nested_vmx_secondary_ctls_high &
7747 SECONDARY_EXEC_ENABLE_EPT) ||
7748 !(vmx->nested.nested_vmx_ept_caps & VMX_EPT_INVEPT_BIT)) {
7749 kvm_queue_exception(vcpu, UD_VECTOR);
7750 return 1;
7751 }
7752
7753 if (!nested_vmx_check_permission(vcpu))
7754 return 1;
7755
7756 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
7757 type = kvm_register_readl(vcpu, (vmx_instruction_info >> 28) & 0xf);
7758
7759 types = (vmx->nested.nested_vmx_ept_caps >> VMX_EPT_EXTENT_SHIFT) & 6;
7760
7761 if (type >= 32 || !(types & (1 << type))) {
7762 nested_vmx_failValid(vcpu,
7763 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
7764 return kvm_skip_emulated_instruction(vcpu);
7765 }
7766
7767 /* According to the Intel VMX instruction reference, the memory
7768 * operand is read even if it isn't needed (e.g., for type==global)
7769 */
7770 if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
7771 vmx_instruction_info, false, &gva))
7772 return 1;
7773 if (kvm_read_guest_virt(&vcpu->arch.emulate_ctxt, gva, &operand,
7774 sizeof(operand), &e)) {
7775 kvm_inject_page_fault(vcpu, &e);
7776 return 1;
7777 }
7778
7779 switch (type) {
7780 case VMX_EPT_EXTENT_GLOBAL:
7781 /*
7782 * TODO: track mappings and invalidate
7783 * single context requests appropriately
7784 */
7785 case VMX_EPT_EXTENT_CONTEXT:
7786 kvm_mmu_sync_roots(vcpu);
7787 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
7788 nested_vmx_succeed(vcpu);
7789 break;
7790 default:
7791 BUG_ON(1);
7792 break;
7793 }
7794
7795 return kvm_skip_emulated_instruction(vcpu);
7796 }
7797
7798 static int handle_invvpid(struct kvm_vcpu *vcpu)
7799 {
7800 struct vcpu_vmx *vmx = to_vmx(vcpu);
7801 u32 vmx_instruction_info;
7802 unsigned long type, types;
7803 gva_t gva;
7804 struct x86_exception e;
7805 struct {
7806 u64 vpid;
7807 u64 gla;
7808 } operand;
7809
7810 if (!(vmx->nested.nested_vmx_secondary_ctls_high &
7811 SECONDARY_EXEC_ENABLE_VPID) ||
7812 !(vmx->nested.nested_vmx_vpid_caps & VMX_VPID_INVVPID_BIT)) {
7813 kvm_queue_exception(vcpu, UD_VECTOR);
7814 return 1;
7815 }
7816
7817 if (!nested_vmx_check_permission(vcpu))
7818 return 1;
7819
7820 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
7821 type = kvm_register_readl(vcpu, (vmx_instruction_info >> 28) & 0xf);
7822
7823 types = (vmx->nested.nested_vmx_vpid_caps &
7824 VMX_VPID_EXTENT_SUPPORTED_MASK) >> 8;
7825
7826 if (type >= 32 || !(types & (1 << type))) {
7827 nested_vmx_failValid(vcpu,
7828 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
7829 return kvm_skip_emulated_instruction(vcpu);
7830 }
7831
7832 /* according to the intel vmx instruction reference, the memory
7833 * operand is read even if it isn't needed (e.g., for type==global)
7834 */
7835 if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
7836 vmx_instruction_info, false, &gva))
7837 return 1;
7838 if (kvm_read_guest_virt(&vcpu->arch.emulate_ctxt, gva, &operand,
7839 sizeof(operand), &e)) {
7840 kvm_inject_page_fault(vcpu, &e);
7841 return 1;
7842 }
7843 if (operand.vpid >> 16) {
7844 nested_vmx_failValid(vcpu,
7845 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
7846 return kvm_skip_emulated_instruction(vcpu);
7847 }
7848
7849 switch (type) {
7850 case VMX_VPID_EXTENT_INDIVIDUAL_ADDR:
7851 if (is_noncanonical_address(operand.gla)) {
7852 nested_vmx_failValid(vcpu,
7853 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
7854 return kvm_skip_emulated_instruction(vcpu);
7855 }
7856 /* fall through */
7857 case VMX_VPID_EXTENT_SINGLE_CONTEXT:
7858 case VMX_VPID_EXTENT_SINGLE_NON_GLOBAL:
7859 if (!operand.vpid) {
7860 nested_vmx_failValid(vcpu,
7861 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
7862 return kvm_skip_emulated_instruction(vcpu);
7863 }
7864 break;
7865 case VMX_VPID_EXTENT_ALL_CONTEXT:
7866 break;
7867 default:
7868 WARN_ON_ONCE(1);
7869 return kvm_skip_emulated_instruction(vcpu);
7870 }
7871
7872 __vmx_flush_tlb(vcpu, vmx->nested.vpid02);
7873 nested_vmx_succeed(vcpu);
7874
7875 return kvm_skip_emulated_instruction(vcpu);
7876 }
7877
7878 static int handle_pml_full(struct kvm_vcpu *vcpu)
7879 {
7880 unsigned long exit_qualification;
7881
7882 trace_kvm_pml_full(vcpu->vcpu_id);
7883
7884 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7885
7886 /*
7887 * PML buffer FULL happened while executing iret from NMI,
7888 * "blocked by NMI" bit has to be set before next VM entry.
7889 */
7890 if (!(to_vmx(vcpu)->idt_vectoring_info & VECTORING_INFO_VALID_MASK) &&
7891 cpu_has_virtual_nmis() &&
7892 (exit_qualification & INTR_INFO_UNBLOCK_NMI))
7893 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
7894 GUEST_INTR_STATE_NMI);
7895
7896 /*
7897 * PML buffer already flushed at beginning of VMEXIT. Nothing to do
7898 * here.., and there's no userspace involvement needed for PML.
7899 */
7900 return 1;
7901 }
7902
7903 static int handle_preemption_timer(struct kvm_vcpu *vcpu)
7904 {
7905 kvm_lapic_expired_hv_timer(vcpu);
7906 return 1;
7907 }
7908
7909 /*
7910 * The exit handlers return 1 if the exit was handled fully and guest execution
7911 * may resume. Otherwise they set the kvm_run parameter to indicate what needs
7912 * to be done to userspace and return 0.
7913 */
7914 static int (*const kvm_vmx_exit_handlers[])(struct kvm_vcpu *vcpu) = {
7915 [EXIT_REASON_EXCEPTION_NMI] = handle_exception,
7916 [EXIT_REASON_EXTERNAL_INTERRUPT] = handle_external_interrupt,
7917 [EXIT_REASON_TRIPLE_FAULT] = handle_triple_fault,
7918 [EXIT_REASON_NMI_WINDOW] = handle_nmi_window,
7919 [EXIT_REASON_IO_INSTRUCTION] = handle_io,
7920 [EXIT_REASON_CR_ACCESS] = handle_cr,
7921 [EXIT_REASON_DR_ACCESS] = handle_dr,
7922 [EXIT_REASON_CPUID] = handle_cpuid,
7923 [EXIT_REASON_MSR_READ] = handle_rdmsr,
7924 [EXIT_REASON_MSR_WRITE] = handle_wrmsr,
7925 [EXIT_REASON_PENDING_INTERRUPT] = handle_interrupt_window,
7926 [EXIT_REASON_HLT] = handle_halt,
7927 [EXIT_REASON_INVD] = handle_invd,
7928 [EXIT_REASON_INVLPG] = handle_invlpg,
7929 [EXIT_REASON_RDPMC] = handle_rdpmc,
7930 [EXIT_REASON_VMCALL] = handle_vmcall,
7931 [EXIT_REASON_VMCLEAR] = handle_vmclear,
7932 [EXIT_REASON_VMLAUNCH] = handle_vmlaunch,
7933 [EXIT_REASON_VMPTRLD] = handle_vmptrld,
7934 [EXIT_REASON_VMPTRST] = handle_vmptrst,
7935 [EXIT_REASON_VMREAD] = handle_vmread,
7936 [EXIT_REASON_VMRESUME] = handle_vmresume,
7937 [EXIT_REASON_VMWRITE] = handle_vmwrite,
7938 [EXIT_REASON_VMOFF] = handle_vmoff,
7939 [EXIT_REASON_VMON] = handle_vmon,
7940 [EXIT_REASON_TPR_BELOW_THRESHOLD] = handle_tpr_below_threshold,
7941 [EXIT_REASON_APIC_ACCESS] = handle_apic_access,
7942 [EXIT_REASON_APIC_WRITE] = handle_apic_write,
7943 [EXIT_REASON_EOI_INDUCED] = handle_apic_eoi_induced,
7944 [EXIT_REASON_WBINVD] = handle_wbinvd,
7945 [EXIT_REASON_XSETBV] = handle_xsetbv,
7946 [EXIT_REASON_TASK_SWITCH] = handle_task_switch,
7947 [EXIT_REASON_MCE_DURING_VMENTRY] = handle_machine_check,
7948 [EXIT_REASON_EPT_VIOLATION] = handle_ept_violation,
7949 [EXIT_REASON_EPT_MISCONFIG] = handle_ept_misconfig,
7950 [EXIT_REASON_PAUSE_INSTRUCTION] = handle_pause,
7951 [EXIT_REASON_MWAIT_INSTRUCTION] = handle_mwait,
7952 [EXIT_REASON_MONITOR_TRAP_FLAG] = handle_monitor_trap,
7953 [EXIT_REASON_MONITOR_INSTRUCTION] = handle_monitor,
7954 [EXIT_REASON_INVEPT] = handle_invept,
7955 [EXIT_REASON_INVVPID] = handle_invvpid,
7956 [EXIT_REASON_XSAVES] = handle_xsaves,
7957 [EXIT_REASON_XRSTORS] = handle_xrstors,
7958 [EXIT_REASON_PML_FULL] = handle_pml_full,
7959 [EXIT_REASON_PREEMPTION_TIMER] = handle_preemption_timer,
7960 };
7961
7962 static const int kvm_vmx_max_exit_handlers =
7963 ARRAY_SIZE(kvm_vmx_exit_handlers);
7964
7965 static bool nested_vmx_exit_handled_io(struct kvm_vcpu *vcpu,
7966 struct vmcs12 *vmcs12)
7967 {
7968 unsigned long exit_qualification;
7969 gpa_t bitmap, last_bitmap;
7970 unsigned int port;
7971 int size;
7972 u8 b;
7973
7974 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_IO_BITMAPS))
7975 return nested_cpu_has(vmcs12, CPU_BASED_UNCOND_IO_EXITING);
7976
7977 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7978
7979 port = exit_qualification >> 16;
7980 size = (exit_qualification & 7) + 1;
7981
7982 last_bitmap = (gpa_t)-1;
7983 b = -1;
7984
7985 while (size > 0) {
7986 if (port < 0x8000)
7987 bitmap = vmcs12->io_bitmap_a;
7988 else if (port < 0x10000)
7989 bitmap = vmcs12->io_bitmap_b;
7990 else
7991 return true;
7992 bitmap += (port & 0x7fff) / 8;
7993
7994 if (last_bitmap != bitmap)
7995 if (kvm_vcpu_read_guest(vcpu, bitmap, &b, 1))
7996 return true;
7997 if (b & (1 << (port & 7)))
7998 return true;
7999
8000 port++;
8001 size--;
8002 last_bitmap = bitmap;
8003 }
8004
8005 return false;
8006 }
8007
8008 /*
8009 * Return 1 if we should exit from L2 to L1 to handle an MSR access access,
8010 * rather than handle it ourselves in L0. I.e., check whether L1 expressed
8011 * disinterest in the current event (read or write a specific MSR) by using an
8012 * MSR bitmap. This may be the case even when L0 doesn't use MSR bitmaps.
8013 */
8014 static bool nested_vmx_exit_handled_msr(struct kvm_vcpu *vcpu,
8015 struct vmcs12 *vmcs12, u32 exit_reason)
8016 {
8017 u32 msr_index = vcpu->arch.regs[VCPU_REGS_RCX];
8018 gpa_t bitmap;
8019
8020 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS))
8021 return true;
8022
8023 /*
8024 * The MSR_BITMAP page is divided into four 1024-byte bitmaps,
8025 * for the four combinations of read/write and low/high MSR numbers.
8026 * First we need to figure out which of the four to use:
8027 */
8028 bitmap = vmcs12->msr_bitmap;
8029 if (exit_reason == EXIT_REASON_MSR_WRITE)
8030 bitmap += 2048;
8031 if (msr_index >= 0xc0000000) {
8032 msr_index -= 0xc0000000;
8033 bitmap += 1024;
8034 }
8035
8036 /* Then read the msr_index'th bit from this bitmap: */
8037 if (msr_index < 1024*8) {
8038 unsigned char b;
8039 if (kvm_vcpu_read_guest(vcpu, bitmap + msr_index/8, &b, 1))
8040 return true;
8041 return 1 & (b >> (msr_index & 7));
8042 } else
8043 return true; /* let L1 handle the wrong parameter */
8044 }
8045
8046 /*
8047 * Return 1 if we should exit from L2 to L1 to handle a CR access exit,
8048 * rather than handle it ourselves in L0. I.e., check if L1 wanted to
8049 * intercept (via guest_host_mask etc.) the current event.
8050 */
8051 static bool nested_vmx_exit_handled_cr(struct kvm_vcpu *vcpu,
8052 struct vmcs12 *vmcs12)
8053 {
8054 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
8055 int cr = exit_qualification & 15;
8056 int reg;
8057 unsigned long val;
8058
8059 switch ((exit_qualification >> 4) & 3) {
8060 case 0: /* mov to cr */
8061 reg = (exit_qualification >> 8) & 15;
8062 val = kvm_register_readl(vcpu, reg);
8063 switch (cr) {
8064 case 0:
8065 if (vmcs12->cr0_guest_host_mask &
8066 (val ^ vmcs12->cr0_read_shadow))
8067 return true;
8068 break;
8069 case 3:
8070 if ((vmcs12->cr3_target_count >= 1 &&
8071 vmcs12->cr3_target_value0 == val) ||
8072 (vmcs12->cr3_target_count >= 2 &&
8073 vmcs12->cr3_target_value1 == val) ||
8074 (vmcs12->cr3_target_count >= 3 &&
8075 vmcs12->cr3_target_value2 == val) ||
8076 (vmcs12->cr3_target_count >= 4 &&
8077 vmcs12->cr3_target_value3 == val))
8078 return false;
8079 if (nested_cpu_has(vmcs12, CPU_BASED_CR3_LOAD_EXITING))
8080 return true;
8081 break;
8082 case 4:
8083 if (vmcs12->cr4_guest_host_mask &
8084 (vmcs12->cr4_read_shadow ^ val))
8085 return true;
8086 break;
8087 case 8:
8088 if (nested_cpu_has(vmcs12, CPU_BASED_CR8_LOAD_EXITING))
8089 return true;
8090 break;
8091 }
8092 break;
8093 case 2: /* clts */
8094 if ((vmcs12->cr0_guest_host_mask & X86_CR0_TS) &&
8095 (vmcs12->cr0_read_shadow & X86_CR0_TS))
8096 return true;
8097 break;
8098 case 1: /* mov from cr */
8099 switch (cr) {
8100 case 3:
8101 if (vmcs12->cpu_based_vm_exec_control &
8102 CPU_BASED_CR3_STORE_EXITING)
8103 return true;
8104 break;
8105 case 8:
8106 if (vmcs12->cpu_based_vm_exec_control &
8107 CPU_BASED_CR8_STORE_EXITING)
8108 return true;
8109 break;
8110 }
8111 break;
8112 case 3: /* lmsw */
8113 /*
8114 * lmsw can change bits 1..3 of cr0, and only set bit 0 of
8115 * cr0. Other attempted changes are ignored, with no exit.
8116 */
8117 val = (exit_qualification >> LMSW_SOURCE_DATA_SHIFT) & 0x0f;
8118 if (vmcs12->cr0_guest_host_mask & 0xe &
8119 (val ^ vmcs12->cr0_read_shadow))
8120 return true;
8121 if ((vmcs12->cr0_guest_host_mask & 0x1) &&
8122 !(vmcs12->cr0_read_shadow & 0x1) &&
8123 (val & 0x1))
8124 return true;
8125 break;
8126 }
8127 return false;
8128 }
8129
8130 /*
8131 * Return 1 if we should exit from L2 to L1 to handle an exit, or 0 if we
8132 * should handle it ourselves in L0 (and then continue L2). Only call this
8133 * when in is_guest_mode (L2).
8134 */
8135 static bool nested_vmx_exit_reflected(struct kvm_vcpu *vcpu, u32 exit_reason)
8136 {
8137 u32 intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
8138 struct vcpu_vmx *vmx = to_vmx(vcpu);
8139 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
8140
8141 trace_kvm_nested_vmexit(kvm_rip_read(vcpu), exit_reason,
8142 vmcs_readl(EXIT_QUALIFICATION),
8143 vmx->idt_vectoring_info,
8144 intr_info,
8145 vmcs_read32(VM_EXIT_INTR_ERROR_CODE),
8146 KVM_ISA_VMX);
8147
8148 /*
8149 * The host physical addresses of some pages of guest memory
8150 * are loaded into VMCS02 (e.g. L1's Virtual APIC Page). The CPU
8151 * may write to these pages via their host physical address while
8152 * L2 is running, bypassing any address-translation-based dirty
8153 * tracking (e.g. EPT write protection).
8154 *
8155 * Mark them dirty on every exit from L2 to prevent them from
8156 * getting out of sync with dirty tracking.
8157 */
8158 nested_mark_vmcs12_pages_dirty(vcpu);
8159
8160 if (vmx->nested.nested_run_pending)
8161 return false;
8162
8163 if (unlikely(vmx->fail)) {
8164 pr_info_ratelimited("%s failed vm entry %x\n", __func__,
8165 vmcs_read32(VM_INSTRUCTION_ERROR));
8166 return true;
8167 }
8168
8169 switch (exit_reason) {
8170 case EXIT_REASON_EXCEPTION_NMI:
8171 if (is_nmi(intr_info))
8172 return false;
8173 else if (is_page_fault(intr_info))
8174 return !vmx->vcpu.arch.apf.host_apf_reason && enable_ept;
8175 else if (is_no_device(intr_info) &&
8176 !(vmcs12->guest_cr0 & X86_CR0_TS))
8177 return false;
8178 else if (is_debug(intr_info) &&
8179 vcpu->guest_debug &
8180 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))
8181 return false;
8182 else if (is_breakpoint(intr_info) &&
8183 vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
8184 return false;
8185 return vmcs12->exception_bitmap &
8186 (1u << (intr_info & INTR_INFO_VECTOR_MASK));
8187 case EXIT_REASON_EXTERNAL_INTERRUPT:
8188 return false;
8189 case EXIT_REASON_TRIPLE_FAULT:
8190 return true;
8191 case EXIT_REASON_PENDING_INTERRUPT:
8192 return nested_cpu_has(vmcs12, CPU_BASED_VIRTUAL_INTR_PENDING);
8193 case EXIT_REASON_NMI_WINDOW:
8194 return nested_cpu_has(vmcs12, CPU_BASED_VIRTUAL_NMI_PENDING);
8195 case EXIT_REASON_TASK_SWITCH:
8196 return true;
8197 case EXIT_REASON_CPUID:
8198 return true;
8199 case EXIT_REASON_HLT:
8200 return nested_cpu_has(vmcs12, CPU_BASED_HLT_EXITING);
8201 case EXIT_REASON_INVD:
8202 return true;
8203 case EXIT_REASON_INVLPG:
8204 return nested_cpu_has(vmcs12, CPU_BASED_INVLPG_EXITING);
8205 case EXIT_REASON_RDPMC:
8206 return nested_cpu_has(vmcs12, CPU_BASED_RDPMC_EXITING);
8207 case EXIT_REASON_RDRAND:
8208 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_RDRAND);
8209 case EXIT_REASON_RDSEED:
8210 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_RDSEED);
8211 case EXIT_REASON_RDTSC: case EXIT_REASON_RDTSCP:
8212 return nested_cpu_has(vmcs12, CPU_BASED_RDTSC_EXITING);
8213 case EXIT_REASON_VMCALL: case EXIT_REASON_VMCLEAR:
8214 case EXIT_REASON_VMLAUNCH: case EXIT_REASON_VMPTRLD:
8215 case EXIT_REASON_VMPTRST: case EXIT_REASON_VMREAD:
8216 case EXIT_REASON_VMRESUME: case EXIT_REASON_VMWRITE:
8217 case EXIT_REASON_VMOFF: case EXIT_REASON_VMON:
8218 case EXIT_REASON_INVEPT: case EXIT_REASON_INVVPID:
8219 /*
8220 * VMX instructions trap unconditionally. This allows L1 to
8221 * emulate them for its L2 guest, i.e., allows 3-level nesting!
8222 */
8223 return true;
8224 case EXIT_REASON_CR_ACCESS:
8225 return nested_vmx_exit_handled_cr(vcpu, vmcs12);
8226 case EXIT_REASON_DR_ACCESS:
8227 return nested_cpu_has(vmcs12, CPU_BASED_MOV_DR_EXITING);
8228 case EXIT_REASON_IO_INSTRUCTION:
8229 return nested_vmx_exit_handled_io(vcpu, vmcs12);
8230 case EXIT_REASON_GDTR_IDTR: case EXIT_REASON_LDTR_TR:
8231 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_DESC);
8232 case EXIT_REASON_MSR_READ:
8233 case EXIT_REASON_MSR_WRITE:
8234 return nested_vmx_exit_handled_msr(vcpu, vmcs12, exit_reason);
8235 case EXIT_REASON_INVALID_STATE:
8236 return true;
8237 case EXIT_REASON_MWAIT_INSTRUCTION:
8238 return nested_cpu_has(vmcs12, CPU_BASED_MWAIT_EXITING);
8239 case EXIT_REASON_MONITOR_TRAP_FLAG:
8240 return nested_cpu_has(vmcs12, CPU_BASED_MONITOR_TRAP_FLAG);
8241 case EXIT_REASON_MONITOR_INSTRUCTION:
8242 return nested_cpu_has(vmcs12, CPU_BASED_MONITOR_EXITING);
8243 case EXIT_REASON_PAUSE_INSTRUCTION:
8244 return nested_cpu_has(vmcs12, CPU_BASED_PAUSE_EXITING) ||
8245 nested_cpu_has2(vmcs12,
8246 SECONDARY_EXEC_PAUSE_LOOP_EXITING);
8247 case EXIT_REASON_MCE_DURING_VMENTRY:
8248 return false;
8249 case EXIT_REASON_TPR_BELOW_THRESHOLD:
8250 return nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW);
8251 case EXIT_REASON_APIC_ACCESS:
8252 return nested_cpu_has2(vmcs12,
8253 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES);
8254 case EXIT_REASON_APIC_WRITE:
8255 case EXIT_REASON_EOI_INDUCED:
8256 /* apic_write and eoi_induced should exit unconditionally. */
8257 return true;
8258 case EXIT_REASON_EPT_VIOLATION:
8259 /*
8260 * L0 always deals with the EPT violation. If nested EPT is
8261 * used, and the nested mmu code discovers that the address is
8262 * missing in the guest EPT table (EPT12), the EPT violation
8263 * will be injected with nested_ept_inject_page_fault()
8264 */
8265 return false;
8266 case EXIT_REASON_EPT_MISCONFIG:
8267 /*
8268 * L2 never uses directly L1's EPT, but rather L0's own EPT
8269 * table (shadow on EPT) or a merged EPT table that L0 built
8270 * (EPT on EPT). So any problems with the structure of the
8271 * table is L0's fault.
8272 */
8273 return false;
8274 case EXIT_REASON_WBINVD:
8275 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_WBINVD_EXITING);
8276 case EXIT_REASON_XSETBV:
8277 return true;
8278 case EXIT_REASON_XSAVES: case EXIT_REASON_XRSTORS:
8279 /*
8280 * This should never happen, since it is not possible to
8281 * set XSS to a non-zero value---neither in L1 nor in L2.
8282 * If if it were, XSS would have to be checked against
8283 * the XSS exit bitmap in vmcs12.
8284 */
8285 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_XSAVES);
8286 case EXIT_REASON_PREEMPTION_TIMER:
8287 return false;
8288 case EXIT_REASON_PML_FULL:
8289 /* We emulate PML support to L1. */
8290 return false;
8291 default:
8292 return true;
8293 }
8294 }
8295
8296 static int nested_vmx_reflect_vmexit(struct kvm_vcpu *vcpu, u32 exit_reason)
8297 {
8298 u32 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
8299
8300 /*
8301 * At this point, the exit interruption info in exit_intr_info
8302 * is only valid for EXCEPTION_NMI exits. For EXTERNAL_INTERRUPT
8303 * we need to query the in-kernel LAPIC.
8304 */
8305 WARN_ON(exit_reason == EXIT_REASON_EXTERNAL_INTERRUPT);
8306 if ((exit_intr_info &
8307 (INTR_INFO_VALID_MASK | INTR_INFO_DELIVER_CODE_MASK)) ==
8308 (INTR_INFO_VALID_MASK | INTR_INFO_DELIVER_CODE_MASK)) {
8309 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
8310 vmcs12->vm_exit_intr_error_code =
8311 vmcs_read32(VM_EXIT_INTR_ERROR_CODE);
8312 }
8313
8314 nested_vmx_vmexit(vcpu, exit_reason, exit_intr_info,
8315 vmcs_readl(EXIT_QUALIFICATION));
8316 return 1;
8317 }
8318
8319 static void vmx_get_exit_info(struct kvm_vcpu *vcpu, u64 *info1, u64 *info2)
8320 {
8321 *info1 = vmcs_readl(EXIT_QUALIFICATION);
8322 *info2 = vmcs_read32(VM_EXIT_INTR_INFO);
8323 }
8324
8325 static void vmx_destroy_pml_buffer(struct vcpu_vmx *vmx)
8326 {
8327 if (vmx->pml_pg) {
8328 __free_page(vmx->pml_pg);
8329 vmx->pml_pg = NULL;
8330 }
8331 }
8332
8333 static void vmx_flush_pml_buffer(struct kvm_vcpu *vcpu)
8334 {
8335 struct vcpu_vmx *vmx = to_vmx(vcpu);
8336 u64 *pml_buf;
8337 u16 pml_idx;
8338
8339 pml_idx = vmcs_read16(GUEST_PML_INDEX);
8340
8341 /* Do nothing if PML buffer is empty */
8342 if (pml_idx == (PML_ENTITY_NUM - 1))
8343 return;
8344
8345 /* PML index always points to next available PML buffer entity */
8346 if (pml_idx >= PML_ENTITY_NUM)
8347 pml_idx = 0;
8348 else
8349 pml_idx++;
8350
8351 pml_buf = page_address(vmx->pml_pg);
8352 for (; pml_idx < PML_ENTITY_NUM; pml_idx++) {
8353 u64 gpa;
8354
8355 gpa = pml_buf[pml_idx];
8356 WARN_ON(gpa & (PAGE_SIZE - 1));
8357 kvm_vcpu_mark_page_dirty(vcpu, gpa >> PAGE_SHIFT);
8358 }
8359
8360 /* reset PML index */
8361 vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1);
8362 }
8363
8364 /*
8365 * Flush all vcpus' PML buffer and update logged GPAs to dirty_bitmap.
8366 * Called before reporting dirty_bitmap to userspace.
8367 */
8368 static void kvm_flush_pml_buffers(struct kvm *kvm)
8369 {
8370 int i;
8371 struct kvm_vcpu *vcpu;
8372 /*
8373 * We only need to kick vcpu out of guest mode here, as PML buffer
8374 * is flushed at beginning of all VMEXITs, and it's obvious that only
8375 * vcpus running in guest are possible to have unflushed GPAs in PML
8376 * buffer.
8377 */
8378 kvm_for_each_vcpu(i, vcpu, kvm)
8379 kvm_vcpu_kick(vcpu);
8380 }
8381
8382 static void vmx_dump_sel(char *name, uint32_t sel)
8383 {
8384 pr_err("%s sel=0x%04x, attr=0x%05x, limit=0x%08x, base=0x%016lx\n",
8385 name, vmcs_read16(sel),
8386 vmcs_read32(sel + GUEST_ES_AR_BYTES - GUEST_ES_SELECTOR),
8387 vmcs_read32(sel + GUEST_ES_LIMIT - GUEST_ES_SELECTOR),
8388 vmcs_readl(sel + GUEST_ES_BASE - GUEST_ES_SELECTOR));
8389 }
8390
8391 static void vmx_dump_dtsel(char *name, uint32_t limit)
8392 {
8393 pr_err("%s limit=0x%08x, base=0x%016lx\n",
8394 name, vmcs_read32(limit),
8395 vmcs_readl(limit + GUEST_GDTR_BASE - GUEST_GDTR_LIMIT));
8396 }
8397
8398 static void dump_vmcs(void)
8399 {
8400 u32 vmentry_ctl = vmcs_read32(VM_ENTRY_CONTROLS);
8401 u32 vmexit_ctl = vmcs_read32(VM_EXIT_CONTROLS);
8402 u32 cpu_based_exec_ctrl = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
8403 u32 pin_based_exec_ctrl = vmcs_read32(PIN_BASED_VM_EXEC_CONTROL);
8404 u32 secondary_exec_control = 0;
8405 unsigned long cr4 = vmcs_readl(GUEST_CR4);
8406 u64 efer = vmcs_read64(GUEST_IA32_EFER);
8407 int i, n;
8408
8409 if (cpu_has_secondary_exec_ctrls())
8410 secondary_exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
8411
8412 pr_err("*** Guest State ***\n");
8413 pr_err("CR0: actual=0x%016lx, shadow=0x%016lx, gh_mask=%016lx\n",
8414 vmcs_readl(GUEST_CR0), vmcs_readl(CR0_READ_SHADOW),
8415 vmcs_readl(CR0_GUEST_HOST_MASK));
8416 pr_err("CR4: actual=0x%016lx, shadow=0x%016lx, gh_mask=%016lx\n",
8417 cr4, vmcs_readl(CR4_READ_SHADOW), vmcs_readl(CR4_GUEST_HOST_MASK));
8418 pr_err("CR3 = 0x%016lx\n", vmcs_readl(GUEST_CR3));
8419 if ((secondary_exec_control & SECONDARY_EXEC_ENABLE_EPT) &&
8420 (cr4 & X86_CR4_PAE) && !(efer & EFER_LMA))
8421 {
8422 pr_err("PDPTR0 = 0x%016llx PDPTR1 = 0x%016llx\n",
8423 vmcs_read64(GUEST_PDPTR0), vmcs_read64(GUEST_PDPTR1));
8424 pr_err("PDPTR2 = 0x%016llx PDPTR3 = 0x%016llx\n",
8425 vmcs_read64(GUEST_PDPTR2), vmcs_read64(GUEST_PDPTR3));
8426 }
8427 pr_err("RSP = 0x%016lx RIP = 0x%016lx\n",
8428 vmcs_readl(GUEST_RSP), vmcs_readl(GUEST_RIP));
8429 pr_err("RFLAGS=0x%08lx DR7 = 0x%016lx\n",
8430 vmcs_readl(GUEST_RFLAGS), vmcs_readl(GUEST_DR7));
8431 pr_err("Sysenter RSP=%016lx CS:RIP=%04x:%016lx\n",
8432 vmcs_readl(GUEST_SYSENTER_ESP),
8433 vmcs_read32(GUEST_SYSENTER_CS), vmcs_readl(GUEST_SYSENTER_EIP));
8434 vmx_dump_sel("CS: ", GUEST_CS_SELECTOR);
8435 vmx_dump_sel("DS: ", GUEST_DS_SELECTOR);
8436 vmx_dump_sel("SS: ", GUEST_SS_SELECTOR);
8437 vmx_dump_sel("ES: ", GUEST_ES_SELECTOR);
8438 vmx_dump_sel("FS: ", GUEST_FS_SELECTOR);
8439 vmx_dump_sel("GS: ", GUEST_GS_SELECTOR);
8440 vmx_dump_dtsel("GDTR:", GUEST_GDTR_LIMIT);
8441 vmx_dump_sel("LDTR:", GUEST_LDTR_SELECTOR);
8442 vmx_dump_dtsel("IDTR:", GUEST_IDTR_LIMIT);
8443 vmx_dump_sel("TR: ", GUEST_TR_SELECTOR);
8444 if ((vmexit_ctl & (VM_EXIT_SAVE_IA32_PAT | VM_EXIT_SAVE_IA32_EFER)) ||
8445 (vmentry_ctl & (VM_ENTRY_LOAD_IA32_PAT | VM_ENTRY_LOAD_IA32_EFER)))
8446 pr_err("EFER = 0x%016llx PAT = 0x%016llx\n",
8447 efer, vmcs_read64(GUEST_IA32_PAT));
8448 pr_err("DebugCtl = 0x%016llx DebugExceptions = 0x%016lx\n",
8449 vmcs_read64(GUEST_IA32_DEBUGCTL),
8450 vmcs_readl(GUEST_PENDING_DBG_EXCEPTIONS));
8451 if (vmentry_ctl & VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL)
8452 pr_err("PerfGlobCtl = 0x%016llx\n",
8453 vmcs_read64(GUEST_IA32_PERF_GLOBAL_CTRL));
8454 if (vmentry_ctl & VM_ENTRY_LOAD_BNDCFGS)
8455 pr_err("BndCfgS = 0x%016llx\n", vmcs_read64(GUEST_BNDCFGS));
8456 pr_err("Interruptibility = %08x ActivityState = %08x\n",
8457 vmcs_read32(GUEST_INTERRUPTIBILITY_INFO),
8458 vmcs_read32(GUEST_ACTIVITY_STATE));
8459 if (secondary_exec_control & SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY)
8460 pr_err("InterruptStatus = %04x\n",
8461 vmcs_read16(GUEST_INTR_STATUS));
8462
8463 pr_err("*** Host State ***\n");
8464 pr_err("RIP = 0x%016lx RSP = 0x%016lx\n",
8465 vmcs_readl(HOST_RIP), vmcs_readl(HOST_RSP));
8466 pr_err("CS=%04x SS=%04x DS=%04x ES=%04x FS=%04x GS=%04x TR=%04x\n",
8467 vmcs_read16(HOST_CS_SELECTOR), vmcs_read16(HOST_SS_SELECTOR),
8468 vmcs_read16(HOST_DS_SELECTOR), vmcs_read16(HOST_ES_SELECTOR),
8469 vmcs_read16(HOST_FS_SELECTOR), vmcs_read16(HOST_GS_SELECTOR),
8470 vmcs_read16(HOST_TR_SELECTOR));
8471 pr_err("FSBase=%016lx GSBase=%016lx TRBase=%016lx\n",
8472 vmcs_readl(HOST_FS_BASE), vmcs_readl(HOST_GS_BASE),
8473 vmcs_readl(HOST_TR_BASE));
8474 pr_err("GDTBase=%016lx IDTBase=%016lx\n",
8475 vmcs_readl(HOST_GDTR_BASE), vmcs_readl(HOST_IDTR_BASE));
8476 pr_err("CR0=%016lx CR3=%016lx CR4=%016lx\n",
8477 vmcs_readl(HOST_CR0), vmcs_readl(HOST_CR3),
8478 vmcs_readl(HOST_CR4));
8479 pr_err("Sysenter RSP=%016lx CS:RIP=%04x:%016lx\n",
8480 vmcs_readl(HOST_IA32_SYSENTER_ESP),
8481 vmcs_read32(HOST_IA32_SYSENTER_CS),
8482 vmcs_readl(HOST_IA32_SYSENTER_EIP));
8483 if (vmexit_ctl & (VM_EXIT_LOAD_IA32_PAT | VM_EXIT_LOAD_IA32_EFER))
8484 pr_err("EFER = 0x%016llx PAT = 0x%016llx\n",
8485 vmcs_read64(HOST_IA32_EFER),
8486 vmcs_read64(HOST_IA32_PAT));
8487 if (vmexit_ctl & VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL)
8488 pr_err("PerfGlobCtl = 0x%016llx\n",
8489 vmcs_read64(HOST_IA32_PERF_GLOBAL_CTRL));
8490
8491 pr_err("*** Control State ***\n");
8492 pr_err("PinBased=%08x CPUBased=%08x SecondaryExec=%08x\n",
8493 pin_based_exec_ctrl, cpu_based_exec_ctrl, secondary_exec_control);
8494 pr_err("EntryControls=%08x ExitControls=%08x\n", vmentry_ctl, vmexit_ctl);
8495 pr_err("ExceptionBitmap=%08x PFECmask=%08x PFECmatch=%08x\n",
8496 vmcs_read32(EXCEPTION_BITMAP),
8497 vmcs_read32(PAGE_FAULT_ERROR_CODE_MASK),
8498 vmcs_read32(PAGE_FAULT_ERROR_CODE_MATCH));
8499 pr_err("VMEntry: intr_info=%08x errcode=%08x ilen=%08x\n",
8500 vmcs_read32(VM_ENTRY_INTR_INFO_FIELD),
8501 vmcs_read32(VM_ENTRY_EXCEPTION_ERROR_CODE),
8502 vmcs_read32(VM_ENTRY_INSTRUCTION_LEN));
8503 pr_err("VMExit: intr_info=%08x errcode=%08x ilen=%08x\n",
8504 vmcs_read32(VM_EXIT_INTR_INFO),
8505 vmcs_read32(VM_EXIT_INTR_ERROR_CODE),
8506 vmcs_read32(VM_EXIT_INSTRUCTION_LEN));
8507 pr_err(" reason=%08x qualification=%016lx\n",
8508 vmcs_read32(VM_EXIT_REASON), vmcs_readl(EXIT_QUALIFICATION));
8509 pr_err("IDTVectoring: info=%08x errcode=%08x\n",
8510 vmcs_read32(IDT_VECTORING_INFO_FIELD),
8511 vmcs_read32(IDT_VECTORING_ERROR_CODE));
8512 pr_err("TSC Offset = 0x%016llx\n", vmcs_read64(TSC_OFFSET));
8513 if (secondary_exec_control & SECONDARY_EXEC_TSC_SCALING)
8514 pr_err("TSC Multiplier = 0x%016llx\n",
8515 vmcs_read64(TSC_MULTIPLIER));
8516 if (cpu_based_exec_ctrl & CPU_BASED_TPR_SHADOW)
8517 pr_err("TPR Threshold = 0x%02x\n", vmcs_read32(TPR_THRESHOLD));
8518 if (pin_based_exec_ctrl & PIN_BASED_POSTED_INTR)
8519 pr_err("PostedIntrVec = 0x%02x\n", vmcs_read16(POSTED_INTR_NV));
8520 if ((secondary_exec_control & SECONDARY_EXEC_ENABLE_EPT))
8521 pr_err("EPT pointer = 0x%016llx\n", vmcs_read64(EPT_POINTER));
8522 n = vmcs_read32(CR3_TARGET_COUNT);
8523 for (i = 0; i + 1 < n; i += 4)
8524 pr_err("CR3 target%u=%016lx target%u=%016lx\n",
8525 i, vmcs_readl(CR3_TARGET_VALUE0 + i * 2),
8526 i + 1, vmcs_readl(CR3_TARGET_VALUE0 + i * 2 + 2));
8527 if (i < n)
8528 pr_err("CR3 target%u=%016lx\n",
8529 i, vmcs_readl(CR3_TARGET_VALUE0 + i * 2));
8530 if (secondary_exec_control & SECONDARY_EXEC_PAUSE_LOOP_EXITING)
8531 pr_err("PLE Gap=%08x Window=%08x\n",
8532 vmcs_read32(PLE_GAP), vmcs_read32(PLE_WINDOW));
8533 if (secondary_exec_control & SECONDARY_EXEC_ENABLE_VPID)
8534 pr_err("Virtual processor ID = 0x%04x\n",
8535 vmcs_read16(VIRTUAL_PROCESSOR_ID));
8536 }
8537
8538 /*
8539 * The guest has exited. See if we can fix it or if we need userspace
8540 * assistance.
8541 */
8542 static int vmx_handle_exit(struct kvm_vcpu *vcpu)
8543 {
8544 struct vcpu_vmx *vmx = to_vmx(vcpu);
8545 u32 exit_reason = vmx->exit_reason;
8546 u32 vectoring_info = vmx->idt_vectoring_info;
8547
8548 trace_kvm_exit(exit_reason, vcpu, KVM_ISA_VMX);
8549 vcpu->arch.gpa_available = false;
8550
8551 /*
8552 * Flush logged GPAs PML buffer, this will make dirty_bitmap more
8553 * updated. Another good is, in kvm_vm_ioctl_get_dirty_log, before
8554 * querying dirty_bitmap, we only need to kick all vcpus out of guest
8555 * mode as if vcpus is in root mode, the PML buffer must has been
8556 * flushed already.
8557 */
8558 if (enable_pml)
8559 vmx_flush_pml_buffer(vcpu);
8560
8561 /* If guest state is invalid, start emulating */
8562 if (vmx->emulation_required)
8563 return handle_invalid_guest_state(vcpu);
8564
8565 if (is_guest_mode(vcpu) && nested_vmx_exit_reflected(vcpu, exit_reason))
8566 return nested_vmx_reflect_vmexit(vcpu, exit_reason);
8567
8568 if (exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY) {
8569 dump_vmcs();
8570 vcpu->run->exit_reason = KVM_EXIT_FAIL_ENTRY;
8571 vcpu->run->fail_entry.hardware_entry_failure_reason
8572 = exit_reason;
8573 return 0;
8574 }
8575
8576 if (unlikely(vmx->fail)) {
8577 vcpu->run->exit_reason = KVM_EXIT_FAIL_ENTRY;
8578 vcpu->run->fail_entry.hardware_entry_failure_reason
8579 = vmcs_read32(VM_INSTRUCTION_ERROR);
8580 return 0;
8581 }
8582
8583 /*
8584 * Note:
8585 * Do not try to fix EXIT_REASON_EPT_MISCONFIG if it caused by
8586 * delivery event since it indicates guest is accessing MMIO.
8587 * The vm-exit can be triggered again after return to guest that
8588 * will cause infinite loop.
8589 */
8590 if ((vectoring_info & VECTORING_INFO_VALID_MASK) &&
8591 (exit_reason != EXIT_REASON_EXCEPTION_NMI &&
8592 exit_reason != EXIT_REASON_EPT_VIOLATION &&
8593 exit_reason != EXIT_REASON_PML_FULL &&
8594 exit_reason != EXIT_REASON_TASK_SWITCH)) {
8595 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
8596 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_DELIVERY_EV;
8597 vcpu->run->internal.ndata = 3;
8598 vcpu->run->internal.data[0] = vectoring_info;
8599 vcpu->run->internal.data[1] = exit_reason;
8600 vcpu->run->internal.data[2] = vcpu->arch.exit_qualification;
8601 if (exit_reason == EXIT_REASON_EPT_MISCONFIG) {
8602 vcpu->run->internal.ndata++;
8603 vcpu->run->internal.data[3] =
8604 vmcs_read64(GUEST_PHYSICAL_ADDRESS);
8605 }
8606 return 0;
8607 }
8608
8609 if (unlikely(!cpu_has_virtual_nmis() &&
8610 vmx->loaded_vmcs->soft_vnmi_blocked)) {
8611 if (vmx_interrupt_allowed(vcpu)) {
8612 vmx->loaded_vmcs->soft_vnmi_blocked = 0;
8613 } else if (vmx->loaded_vmcs->vnmi_blocked_time > 1000000000LL &&
8614 vcpu->arch.nmi_pending) {
8615 /*
8616 * This CPU don't support us in finding the end of an
8617 * NMI-blocked window if the guest runs with IRQs
8618 * disabled. So we pull the trigger after 1 s of
8619 * futile waiting, but inform the user about this.
8620 */
8621 printk(KERN_WARNING "%s: Breaking out of NMI-blocked "
8622 "state on VCPU %d after 1 s timeout\n",
8623 __func__, vcpu->vcpu_id);
8624 vmx->loaded_vmcs->soft_vnmi_blocked = 0;
8625 }
8626 }
8627
8628 if (exit_reason < kvm_vmx_max_exit_handlers
8629 && kvm_vmx_exit_handlers[exit_reason])
8630 return kvm_vmx_exit_handlers[exit_reason](vcpu);
8631 else {
8632 vcpu_unimpl(vcpu, "vmx: unexpected exit reason 0x%x\n",
8633 exit_reason);
8634 kvm_queue_exception(vcpu, UD_VECTOR);
8635 return 1;
8636 }
8637 }
8638
8639 static void update_cr8_intercept(struct kvm_vcpu *vcpu, int tpr, int irr)
8640 {
8641 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
8642
8643 if (is_guest_mode(vcpu) &&
8644 nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW))
8645 return;
8646
8647 if (irr == -1 || tpr < irr) {
8648 vmcs_write32(TPR_THRESHOLD, 0);
8649 return;
8650 }
8651
8652 vmcs_write32(TPR_THRESHOLD, irr);
8653 }
8654
8655 static void vmx_set_virtual_x2apic_mode(struct kvm_vcpu *vcpu, bool set)
8656 {
8657 u32 sec_exec_control;
8658
8659 /* Postpone execution until vmcs01 is the current VMCS. */
8660 if (is_guest_mode(vcpu)) {
8661 to_vmx(vcpu)->nested.change_vmcs01_virtual_x2apic_mode = true;
8662 return;
8663 }
8664
8665 if (!cpu_has_vmx_virtualize_x2apic_mode())
8666 return;
8667
8668 if (!cpu_need_tpr_shadow(vcpu))
8669 return;
8670
8671 sec_exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
8672
8673 if (set) {
8674 sec_exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
8675 sec_exec_control |= SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
8676 } else {
8677 sec_exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
8678 sec_exec_control |= SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
8679 vmx_flush_tlb_ept_only(vcpu);
8680 }
8681 vmcs_write32(SECONDARY_VM_EXEC_CONTROL, sec_exec_control);
8682
8683 vmx_set_msr_bitmap(vcpu);
8684 }
8685
8686 static void vmx_set_apic_access_page_addr(struct kvm_vcpu *vcpu, hpa_t hpa)
8687 {
8688 struct vcpu_vmx *vmx = to_vmx(vcpu);
8689
8690 /*
8691 * Currently we do not handle the nested case where L2 has an
8692 * APIC access page of its own; that page is still pinned.
8693 * Hence, we skip the case where the VCPU is in guest mode _and_
8694 * L1 prepared an APIC access page for L2.
8695 *
8696 * For the case where L1 and L2 share the same APIC access page
8697 * (flexpriority=Y but SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES clear
8698 * in the vmcs12), this function will only update either the vmcs01
8699 * or the vmcs02. If the former, the vmcs02 will be updated by
8700 * prepare_vmcs02. If the latter, the vmcs01 will be updated in
8701 * the next L2->L1 exit.
8702 */
8703 if (!is_guest_mode(vcpu) ||
8704 !nested_cpu_has2(get_vmcs12(&vmx->vcpu),
8705 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) {
8706 vmcs_write64(APIC_ACCESS_ADDR, hpa);
8707 vmx_flush_tlb_ept_only(vcpu);
8708 }
8709 }
8710
8711 static void vmx_hwapic_isr_update(struct kvm_vcpu *vcpu, int max_isr)
8712 {
8713 u16 status;
8714 u8 old;
8715
8716 if (max_isr == -1)
8717 max_isr = 0;
8718
8719 status = vmcs_read16(GUEST_INTR_STATUS);
8720 old = status >> 8;
8721 if (max_isr != old) {
8722 status &= 0xff;
8723 status |= max_isr << 8;
8724 vmcs_write16(GUEST_INTR_STATUS, status);
8725 }
8726 }
8727
8728 static void vmx_set_rvi(int vector)
8729 {
8730 u16 status;
8731 u8 old;
8732
8733 if (vector == -1)
8734 vector = 0;
8735
8736 status = vmcs_read16(GUEST_INTR_STATUS);
8737 old = (u8)status & 0xff;
8738 if ((u8)vector != old) {
8739 status &= ~0xff;
8740 status |= (u8)vector;
8741 vmcs_write16(GUEST_INTR_STATUS, status);
8742 }
8743 }
8744
8745 static void vmx_hwapic_irr_update(struct kvm_vcpu *vcpu, int max_irr)
8746 {
8747 if (!is_guest_mode(vcpu)) {
8748 vmx_set_rvi(max_irr);
8749 return;
8750 }
8751
8752 if (max_irr == -1)
8753 return;
8754
8755 /*
8756 * In guest mode. If a vmexit is needed, vmx_check_nested_events
8757 * handles it.
8758 */
8759 if (nested_exit_on_intr(vcpu))
8760 return;
8761
8762 /*
8763 * Else, fall back to pre-APICv interrupt injection since L2
8764 * is run without virtual interrupt delivery.
8765 */
8766 if (!kvm_event_needs_reinjection(vcpu) &&
8767 vmx_interrupt_allowed(vcpu)) {
8768 kvm_queue_interrupt(vcpu, max_irr, false);
8769 vmx_inject_irq(vcpu);
8770 }
8771 }
8772
8773 static int vmx_sync_pir_to_irr(struct kvm_vcpu *vcpu)
8774 {
8775 struct vcpu_vmx *vmx = to_vmx(vcpu);
8776 int max_irr;
8777
8778 WARN_ON(!vcpu->arch.apicv_active);
8779 if (pi_test_on(&vmx->pi_desc)) {
8780 pi_clear_on(&vmx->pi_desc);
8781 /*
8782 * IOMMU can write to PIR.ON, so the barrier matters even on UP.
8783 * But on x86 this is just a compiler barrier anyway.
8784 */
8785 smp_mb__after_atomic();
8786 max_irr = kvm_apic_update_irr(vcpu, vmx->pi_desc.pir);
8787 } else {
8788 max_irr = kvm_lapic_find_highest_irr(vcpu);
8789 }
8790 vmx_hwapic_irr_update(vcpu, max_irr);
8791 return max_irr;
8792 }
8793
8794 static void vmx_load_eoi_exitmap(struct kvm_vcpu *vcpu, u64 *eoi_exit_bitmap)
8795 {
8796 if (!kvm_vcpu_apicv_active(vcpu))
8797 return;
8798
8799 vmcs_write64(EOI_EXIT_BITMAP0, eoi_exit_bitmap[0]);
8800 vmcs_write64(EOI_EXIT_BITMAP1, eoi_exit_bitmap[1]);
8801 vmcs_write64(EOI_EXIT_BITMAP2, eoi_exit_bitmap[2]);
8802 vmcs_write64(EOI_EXIT_BITMAP3, eoi_exit_bitmap[3]);
8803 }
8804
8805 static void vmx_apicv_post_state_restore(struct kvm_vcpu *vcpu)
8806 {
8807 struct vcpu_vmx *vmx = to_vmx(vcpu);
8808
8809 pi_clear_on(&vmx->pi_desc);
8810 memset(vmx->pi_desc.pir, 0, sizeof(vmx->pi_desc.pir));
8811 }
8812
8813 static void vmx_complete_atomic_exit(struct vcpu_vmx *vmx)
8814 {
8815 u32 exit_intr_info = 0;
8816 u16 basic_exit_reason = (u16)vmx->exit_reason;
8817
8818 if (!(basic_exit_reason == EXIT_REASON_MCE_DURING_VMENTRY
8819 || basic_exit_reason == EXIT_REASON_EXCEPTION_NMI))
8820 return;
8821
8822 if (!(vmx->exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY))
8823 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
8824 vmx->exit_intr_info = exit_intr_info;
8825
8826 /* if exit due to PF check for async PF */
8827 if (is_page_fault(exit_intr_info))
8828 vmx->vcpu.arch.apf.host_apf_reason = kvm_read_and_reset_pf_reason();
8829
8830 /* Handle machine checks before interrupts are enabled */
8831 if (basic_exit_reason == EXIT_REASON_MCE_DURING_VMENTRY ||
8832 is_machine_check(exit_intr_info))
8833 kvm_machine_check();
8834
8835 /* We need to handle NMIs before interrupts are enabled */
8836 if (is_nmi(exit_intr_info)) {
8837 kvm_before_handle_nmi(&vmx->vcpu);
8838 asm("int $2");
8839 kvm_after_handle_nmi(&vmx->vcpu);
8840 }
8841 }
8842
8843 static void vmx_handle_external_intr(struct kvm_vcpu *vcpu)
8844 {
8845 u32 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
8846
8847 if ((exit_intr_info & (INTR_INFO_VALID_MASK | INTR_INFO_INTR_TYPE_MASK))
8848 == (INTR_INFO_VALID_MASK | INTR_TYPE_EXT_INTR)) {
8849 unsigned int vector;
8850 unsigned long entry;
8851 gate_desc *desc;
8852 struct vcpu_vmx *vmx = to_vmx(vcpu);
8853 #ifdef CONFIG_X86_64
8854 unsigned long tmp;
8855 #endif
8856
8857 vector = exit_intr_info & INTR_INFO_VECTOR_MASK;
8858 desc = (gate_desc *)vmx->host_idt_base + vector;
8859 entry = gate_offset(desc);
8860 asm volatile(
8861 #ifdef CONFIG_X86_64
8862 "mov %%" _ASM_SP ", %[sp]\n\t"
8863 "and $0xfffffffffffffff0, %%" _ASM_SP "\n\t"
8864 "push $%c[ss]\n\t"
8865 "push %[sp]\n\t"
8866 #endif
8867 "pushf\n\t"
8868 __ASM_SIZE(push) " $%c[cs]\n\t"
8869 CALL_NOSPEC
8870 :
8871 #ifdef CONFIG_X86_64
8872 [sp]"=&r"(tmp),
8873 #endif
8874 ASM_CALL_CONSTRAINT
8875 :
8876 THUNK_TARGET(entry),
8877 [ss]"i"(__KERNEL_DS),
8878 [cs]"i"(__KERNEL_CS)
8879 );
8880 }
8881 }
8882 STACK_FRAME_NON_STANDARD(vmx_handle_external_intr);
8883
8884 static bool vmx_has_high_real_mode_segbase(void)
8885 {
8886 return enable_unrestricted_guest || emulate_invalid_guest_state;
8887 }
8888
8889 static bool vmx_mpx_supported(void)
8890 {
8891 return (vmcs_config.vmexit_ctrl & VM_EXIT_CLEAR_BNDCFGS) &&
8892 (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_BNDCFGS);
8893 }
8894
8895 static bool vmx_xsaves_supported(void)
8896 {
8897 return vmcs_config.cpu_based_2nd_exec_ctrl &
8898 SECONDARY_EXEC_XSAVES;
8899 }
8900
8901 static void vmx_recover_nmi_blocking(struct vcpu_vmx *vmx)
8902 {
8903 u32 exit_intr_info;
8904 bool unblock_nmi;
8905 u8 vector;
8906 bool idtv_info_valid;
8907
8908 idtv_info_valid = vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK;
8909
8910 if (cpu_has_virtual_nmis()) {
8911 if (vmx->loaded_vmcs->nmi_known_unmasked)
8912 return;
8913 /*
8914 * Can't use vmx->exit_intr_info since we're not sure what
8915 * the exit reason is.
8916 */
8917 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
8918 unblock_nmi = (exit_intr_info & INTR_INFO_UNBLOCK_NMI) != 0;
8919 vector = exit_intr_info & INTR_INFO_VECTOR_MASK;
8920 /*
8921 * SDM 3: 27.7.1.2 (September 2008)
8922 * Re-set bit "block by NMI" before VM entry if vmexit caused by
8923 * a guest IRET fault.
8924 * SDM 3: 23.2.2 (September 2008)
8925 * Bit 12 is undefined in any of the following cases:
8926 * If the VM exit sets the valid bit in the IDT-vectoring
8927 * information field.
8928 * If the VM exit is due to a double fault.
8929 */
8930 if ((exit_intr_info & INTR_INFO_VALID_MASK) && unblock_nmi &&
8931 vector != DF_VECTOR && !idtv_info_valid)
8932 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
8933 GUEST_INTR_STATE_NMI);
8934 else
8935 vmx->loaded_vmcs->nmi_known_unmasked =
8936 !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO)
8937 & GUEST_INTR_STATE_NMI);
8938 } else if (unlikely(vmx->loaded_vmcs->soft_vnmi_blocked))
8939 vmx->loaded_vmcs->vnmi_blocked_time +=
8940 ktime_to_ns(ktime_sub(ktime_get(),
8941 vmx->loaded_vmcs->entry_time));
8942 }
8943
8944 static void __vmx_complete_interrupts(struct kvm_vcpu *vcpu,
8945 u32 idt_vectoring_info,
8946 int instr_len_field,
8947 int error_code_field)
8948 {
8949 u8 vector;
8950 int type;
8951 bool idtv_info_valid;
8952
8953 idtv_info_valid = idt_vectoring_info & VECTORING_INFO_VALID_MASK;
8954
8955 vcpu->arch.nmi_injected = false;
8956 kvm_clear_exception_queue(vcpu);
8957 kvm_clear_interrupt_queue(vcpu);
8958
8959 if (!idtv_info_valid)
8960 return;
8961
8962 kvm_make_request(KVM_REQ_EVENT, vcpu);
8963
8964 vector = idt_vectoring_info & VECTORING_INFO_VECTOR_MASK;
8965 type = idt_vectoring_info & VECTORING_INFO_TYPE_MASK;
8966
8967 switch (type) {
8968 case INTR_TYPE_NMI_INTR:
8969 vcpu->arch.nmi_injected = true;
8970 /*
8971 * SDM 3: 27.7.1.2 (September 2008)
8972 * Clear bit "block by NMI" before VM entry if a NMI
8973 * delivery faulted.
8974 */
8975 vmx_set_nmi_mask(vcpu, false);
8976 break;
8977 case INTR_TYPE_SOFT_EXCEPTION:
8978 vcpu->arch.event_exit_inst_len = vmcs_read32(instr_len_field);
8979 /* fall through */
8980 case INTR_TYPE_HARD_EXCEPTION:
8981 if (idt_vectoring_info & VECTORING_INFO_DELIVER_CODE_MASK) {
8982 u32 err = vmcs_read32(error_code_field);
8983 kvm_requeue_exception_e(vcpu, vector, err);
8984 } else
8985 kvm_requeue_exception(vcpu, vector);
8986 break;
8987 case INTR_TYPE_SOFT_INTR:
8988 vcpu->arch.event_exit_inst_len = vmcs_read32(instr_len_field);
8989 /* fall through */
8990 case INTR_TYPE_EXT_INTR:
8991 kvm_queue_interrupt(vcpu, vector, type == INTR_TYPE_SOFT_INTR);
8992 break;
8993 default:
8994 break;
8995 }
8996 }
8997
8998 static void vmx_complete_interrupts(struct vcpu_vmx *vmx)
8999 {
9000 __vmx_complete_interrupts(&vmx->vcpu, vmx->idt_vectoring_info,
9001 VM_EXIT_INSTRUCTION_LEN,
9002 IDT_VECTORING_ERROR_CODE);
9003 }
9004
9005 static void vmx_cancel_injection(struct kvm_vcpu *vcpu)
9006 {
9007 __vmx_complete_interrupts(vcpu,
9008 vmcs_read32(VM_ENTRY_INTR_INFO_FIELD),
9009 VM_ENTRY_INSTRUCTION_LEN,
9010 VM_ENTRY_EXCEPTION_ERROR_CODE);
9011
9012 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0);
9013 }
9014
9015 static void atomic_switch_perf_msrs(struct vcpu_vmx *vmx)
9016 {
9017 int i, nr_msrs;
9018 struct perf_guest_switch_msr *msrs;
9019
9020 msrs = perf_guest_get_msrs(&nr_msrs);
9021
9022 if (!msrs)
9023 return;
9024
9025 for (i = 0; i < nr_msrs; i++)
9026 if (msrs[i].host == msrs[i].guest)
9027 clear_atomic_switch_msr(vmx, msrs[i].msr);
9028 else
9029 add_atomic_switch_msr(vmx, msrs[i].msr, msrs[i].guest,
9030 msrs[i].host);
9031 }
9032
9033 static void vmx_arm_hv_timer(struct kvm_vcpu *vcpu)
9034 {
9035 struct vcpu_vmx *vmx = to_vmx(vcpu);
9036 u64 tscl;
9037 u32 delta_tsc;
9038
9039 if (vmx->hv_deadline_tsc == -1)
9040 return;
9041
9042 tscl = rdtsc();
9043 if (vmx->hv_deadline_tsc > tscl)
9044 /* sure to be 32 bit only because checked on set_hv_timer */
9045 delta_tsc = (u32)((vmx->hv_deadline_tsc - tscl) >>
9046 cpu_preemption_timer_multi);
9047 else
9048 delta_tsc = 0;
9049
9050 vmcs_write32(VMX_PREEMPTION_TIMER_VALUE, delta_tsc);
9051 }
9052
9053 static void __noclone vmx_vcpu_run(struct kvm_vcpu *vcpu)
9054 {
9055 struct vcpu_vmx *vmx = to_vmx(vcpu);
9056 unsigned long debugctlmsr, cr3, cr4;
9057
9058 /* Record the guest's net vcpu time for enforced NMI injections. */
9059 if (unlikely(!cpu_has_virtual_nmis() &&
9060 vmx->loaded_vmcs->soft_vnmi_blocked))
9061 vmx->loaded_vmcs->entry_time = ktime_get();
9062
9063 /* Don't enter VMX if guest state is invalid, let the exit handler
9064 start emulation until we arrive back to a valid state */
9065 if (vmx->emulation_required)
9066 return;
9067
9068 if (vmx->ple_window_dirty) {
9069 vmx->ple_window_dirty = false;
9070 vmcs_write32(PLE_WINDOW, vmx->ple_window);
9071 }
9072
9073 if (vmx->nested.sync_shadow_vmcs) {
9074 copy_vmcs12_to_shadow(vmx);
9075 vmx->nested.sync_shadow_vmcs = false;
9076 }
9077
9078 if (test_bit(VCPU_REGS_RSP, (unsigned long *)&vcpu->arch.regs_dirty))
9079 vmcs_writel(GUEST_RSP, vcpu->arch.regs[VCPU_REGS_RSP]);
9080 if (test_bit(VCPU_REGS_RIP, (unsigned long *)&vcpu->arch.regs_dirty))
9081 vmcs_writel(GUEST_RIP, vcpu->arch.regs[VCPU_REGS_RIP]);
9082
9083 cr3 = __get_current_cr3_fast();
9084 if (unlikely(cr3 != vmx->loaded_vmcs->vmcs_host_cr3)) {
9085 vmcs_writel(HOST_CR3, cr3);
9086 vmx->loaded_vmcs->vmcs_host_cr3 = cr3;
9087 }
9088
9089 cr4 = cr4_read_shadow();
9090 if (unlikely(cr4 != vmx->loaded_vmcs->vmcs_host_cr4)) {
9091 vmcs_writel(HOST_CR4, cr4);
9092 vmx->loaded_vmcs->vmcs_host_cr4 = cr4;
9093 }
9094
9095 /* When single-stepping over STI and MOV SS, we must clear the
9096 * corresponding interruptibility bits in the guest state. Otherwise
9097 * vmentry fails as it then expects bit 14 (BS) in pending debug
9098 * exceptions being set, but that's not correct for the guest debugging
9099 * case. */
9100 if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP)
9101 vmx_set_interrupt_shadow(vcpu, 0);
9102
9103 if (static_cpu_has(X86_FEATURE_PKU) &&
9104 kvm_read_cr4_bits(vcpu, X86_CR4_PKE) &&
9105 vcpu->arch.pkru != vmx->host_pkru)
9106 __write_pkru(vcpu->arch.pkru);
9107
9108 atomic_switch_perf_msrs(vmx);
9109
9110 /* SMB: Ignore ibrs_inuse but rely on vcpu value */
9111 x86_spec_ctrl_set_guest(vcpu->arch.spec_ctrl);
9112
9113 debugctlmsr = get_debugctlmsr();
9114
9115 vmx_arm_hv_timer(vcpu);
9116
9117 vmx->__launched = vmx->loaded_vmcs->launched;
9118 asm(
9119 /* Store host registers */
9120 "push %%" _ASM_DX "; push %%" _ASM_BP ";"
9121 "push %%" _ASM_CX " \n\t" /* placeholder for guest rcx */
9122 "push %%" _ASM_CX " \n\t"
9123 "cmp %%" _ASM_SP ", %c[host_rsp](%0) \n\t"
9124 "je 1f \n\t"
9125 "mov %%" _ASM_SP ", %c[host_rsp](%0) \n\t"
9126 __ex(ASM_VMX_VMWRITE_RSP_RDX) "\n\t"
9127 "1: \n\t"
9128 /* Reload cr2 if changed */
9129 "mov %c[cr2](%0), %%" _ASM_AX " \n\t"
9130 "mov %%cr2, %%" _ASM_DX " \n\t"
9131 "cmp %%" _ASM_AX ", %%" _ASM_DX " \n\t"
9132 "je 2f \n\t"
9133 "mov %%" _ASM_AX", %%cr2 \n\t"
9134 "2: \n\t"
9135 /* Check if vmlaunch of vmresume is needed */
9136 "cmpl $0, %c[launched](%0) \n\t"
9137 /* Load guest registers. Don't clobber flags. */
9138 "mov %c[rax](%0), %%" _ASM_AX " \n\t"
9139 "mov %c[rbx](%0), %%" _ASM_BX " \n\t"
9140 "mov %c[rdx](%0), %%" _ASM_DX " \n\t"
9141 "mov %c[rsi](%0), %%" _ASM_SI " \n\t"
9142 "mov %c[rdi](%0), %%" _ASM_DI " \n\t"
9143 "mov %c[rbp](%0), %%" _ASM_BP " \n\t"
9144 #ifdef CONFIG_X86_64
9145 "mov %c[r8](%0), %%r8 \n\t"
9146 "mov %c[r9](%0), %%r9 \n\t"
9147 "mov %c[r10](%0), %%r10 \n\t"
9148 "mov %c[r11](%0), %%r11 \n\t"
9149 "mov %c[r12](%0), %%r12 \n\t"
9150 "mov %c[r13](%0), %%r13 \n\t"
9151 "mov %c[r14](%0), %%r14 \n\t"
9152 "mov %c[r15](%0), %%r15 \n\t"
9153 #endif
9154 "mov %c[rcx](%0), %%" _ASM_CX " \n\t" /* kills %0 (ecx) */
9155
9156 /* Enter guest mode */
9157 "jne 1f \n\t"
9158 __ex(ASM_VMX_VMLAUNCH) "\n\t"
9159 "jmp 2f \n\t"
9160 "1: " __ex(ASM_VMX_VMRESUME) "\n\t"
9161 "2: "
9162 /* Save guest registers, load host registers, keep flags */
9163 "mov %0, %c[wordsize](%%" _ASM_SP ") \n\t"
9164 "pop %0 \n\t"
9165 "setbe %c[fail](%0)\n\t"
9166 "mov %%" _ASM_AX ", %c[rax](%0) \n\t"
9167 "mov %%" _ASM_BX ", %c[rbx](%0) \n\t"
9168 __ASM_SIZE(pop) " %c[rcx](%0) \n\t"
9169 "mov %%" _ASM_DX ", %c[rdx](%0) \n\t"
9170 "mov %%" _ASM_SI ", %c[rsi](%0) \n\t"
9171 "mov %%" _ASM_DI ", %c[rdi](%0) \n\t"
9172 "mov %%" _ASM_BP ", %c[rbp](%0) \n\t"
9173 #ifdef CONFIG_X86_64
9174 "mov %%r8, %c[r8](%0) \n\t"
9175 "mov %%r9, %c[r9](%0) \n\t"
9176 "mov %%r10, %c[r10](%0) \n\t"
9177 "mov %%r11, %c[r11](%0) \n\t"
9178 "mov %%r12, %c[r12](%0) \n\t"
9179 "mov %%r13, %c[r13](%0) \n\t"
9180 "mov %%r14, %c[r14](%0) \n\t"
9181 "mov %%r15, %c[r15](%0) \n\t"
9182 "xor %%r8d, %%r8d \n\t"
9183 "xor %%r9d, %%r9d \n\t"
9184 "xor %%r10d, %%r10d \n\t"
9185 "xor %%r11d, %%r11d \n\t"
9186 "xor %%r12d, %%r12d \n\t"
9187 "xor %%r13d, %%r13d \n\t"
9188 "xor %%r14d, %%r14d \n\t"
9189 "xor %%r15d, %%r15d \n\t"
9190 #endif
9191 "mov %%cr2, %%" _ASM_AX " \n\t"
9192 "mov %%" _ASM_AX ", %c[cr2](%0) \n\t"
9193
9194 "xor %%eax, %%eax \n\t"
9195 "xor %%ebx, %%ebx \n\t"
9196 "xor %%esi, %%esi \n\t"
9197 "xor %%edi, %%edi \n\t"
9198 "pop %%" _ASM_BP "; pop %%" _ASM_DX " \n\t"
9199 ".pushsection .rodata \n\t"
9200 ".global vmx_return \n\t"
9201 "vmx_return: " _ASM_PTR " 2b \n\t"
9202 ".popsection"
9203 : : "c"(vmx), "d"((unsigned long)HOST_RSP),
9204 [launched]"i"(offsetof(struct vcpu_vmx, __launched)),
9205 [fail]"i"(offsetof(struct vcpu_vmx, fail)),
9206 [host_rsp]"i"(offsetof(struct vcpu_vmx, host_rsp)),
9207 [rax]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RAX])),
9208 [rbx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBX])),
9209 [rcx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RCX])),
9210 [rdx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDX])),
9211 [rsi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RSI])),
9212 [rdi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDI])),
9213 [rbp]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBP])),
9214 #ifdef CONFIG_X86_64
9215 [r8]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R8])),
9216 [r9]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R9])),
9217 [r10]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R10])),
9218 [r11]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R11])),
9219 [r12]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R12])),
9220 [r13]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R13])),
9221 [r14]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R14])),
9222 [r15]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R15])),
9223 #endif
9224 [cr2]"i"(offsetof(struct vcpu_vmx, vcpu.arch.cr2)),
9225 [wordsize]"i"(sizeof(ulong))
9226 : "cc", "memory"
9227 #ifdef CONFIG_X86_64
9228 , "rax", "rbx", "rdi", "rsi"
9229 , "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15"
9230 #else
9231 , "eax", "ebx", "edi", "esi"
9232 #endif
9233 );
9234
9235 x86_spec_ctrl_restore_host(vcpu->arch.spec_ctrl);
9236
9237 /* Eliminate branch target predictions from guest mode */
9238 vmexit_fill_RSB();
9239
9240 /* MSR_IA32_DEBUGCTLMSR is zeroed on vmexit. Restore it if needed */
9241 if (debugctlmsr)
9242 update_debugctlmsr(debugctlmsr);
9243
9244 #ifndef CONFIG_X86_64
9245 /*
9246 * The sysexit path does not restore ds/es, so we must set them to
9247 * a reasonable value ourselves.
9248 *
9249 * We can't defer this to vmx_load_host_state() since that function
9250 * may be executed in interrupt context, which saves and restore segments
9251 * around it, nullifying its effect.
9252 */
9253 loadsegment(ds, __USER_DS);
9254 loadsegment(es, __USER_DS);
9255 #endif
9256
9257 vcpu->arch.regs_avail = ~((1 << VCPU_REGS_RIP) | (1 << VCPU_REGS_RSP)
9258 | (1 << VCPU_EXREG_RFLAGS)
9259 | (1 << VCPU_EXREG_PDPTR)
9260 | (1 << VCPU_EXREG_SEGMENTS)
9261 | (1 << VCPU_EXREG_CR3));
9262 vcpu->arch.regs_dirty = 0;
9263
9264 vmx->idt_vectoring_info = vmcs_read32(IDT_VECTORING_INFO_FIELD);
9265
9266 vmx->loaded_vmcs->launched = 1;
9267
9268 vmx->exit_reason = vmcs_read32(VM_EXIT_REASON);
9269
9270 /*
9271 * eager fpu is enabled if PKEY is supported and CR4 is switched
9272 * back on host, so it is safe to read guest PKRU from current
9273 * XSAVE.
9274 */
9275 if (static_cpu_has(X86_FEATURE_PKU) &&
9276 kvm_read_cr4_bits(vcpu, X86_CR4_PKE)) {
9277 vcpu->arch.pkru = __read_pkru();
9278 if (vcpu->arch.pkru != vmx->host_pkru)
9279 __write_pkru(vmx->host_pkru);
9280 }
9281
9282 /*
9283 * the KVM_REQ_EVENT optimization bit is only on for one entry, and if
9284 * we did not inject a still-pending event to L1 now because of
9285 * nested_run_pending, we need to re-enable this bit.
9286 */
9287 if (vmx->nested.nested_run_pending)
9288 kvm_make_request(KVM_REQ_EVENT, vcpu);
9289
9290 vmx->nested.nested_run_pending = 0;
9291
9292 vmx_complete_atomic_exit(vmx);
9293 vmx_recover_nmi_blocking(vmx);
9294 vmx_complete_interrupts(vmx);
9295 }
9296 STACK_FRAME_NON_STANDARD(vmx_vcpu_run);
9297
9298 static void vmx_switch_vmcs(struct kvm_vcpu *vcpu, struct loaded_vmcs *vmcs)
9299 {
9300 struct vcpu_vmx *vmx = to_vmx(vcpu);
9301 int cpu;
9302
9303 if (vmx->loaded_vmcs == vmcs)
9304 return;
9305
9306 cpu = get_cpu();
9307 vmx->loaded_vmcs = vmcs;
9308 vmx_vcpu_put(vcpu);
9309 vmx_vcpu_load(vcpu, cpu);
9310 vcpu->cpu = cpu;
9311 put_cpu();
9312 }
9313
9314 /*
9315 * Ensure that the current vmcs of the logical processor is the
9316 * vmcs01 of the vcpu before calling free_nested().
9317 */
9318 static void vmx_free_vcpu_nested(struct kvm_vcpu *vcpu)
9319 {
9320 struct vcpu_vmx *vmx = to_vmx(vcpu);
9321 int r;
9322
9323 r = vcpu_load(vcpu);
9324 BUG_ON(r);
9325 vmx_switch_vmcs(vcpu, &vmx->vmcs01);
9326 free_nested(vmx);
9327 vcpu_put(vcpu);
9328 }
9329
9330 static void vmx_free_vcpu(struct kvm_vcpu *vcpu)
9331 {
9332 struct vcpu_vmx *vmx = to_vmx(vcpu);
9333
9334 if (enable_pml)
9335 vmx_destroy_pml_buffer(vmx);
9336 free_vpid(vmx->vpid);
9337 leave_guest_mode(vcpu);
9338 vmx_free_vcpu_nested(vcpu);
9339 free_loaded_vmcs(vmx->loaded_vmcs);
9340 kfree(vmx->guest_msrs);
9341 kvm_vcpu_uninit(vcpu);
9342 kmem_cache_free(kvm_vcpu_cache, vmx);
9343 }
9344
9345 static struct kvm_vcpu *vmx_create_vcpu(struct kvm *kvm, unsigned int id)
9346 {
9347 int err;
9348 struct vcpu_vmx *vmx = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
9349 int cpu;
9350
9351 if (!vmx)
9352 return ERR_PTR(-ENOMEM);
9353
9354 vmx->vpid = allocate_vpid();
9355
9356 err = kvm_vcpu_init(&vmx->vcpu, kvm, id);
9357 if (err)
9358 goto free_vcpu;
9359
9360 err = -ENOMEM;
9361
9362 /*
9363 * If PML is turned on, failure on enabling PML just results in failure
9364 * of creating the vcpu, therefore we can simplify PML logic (by
9365 * avoiding dealing with cases, such as enabling PML partially on vcpus
9366 * for the guest, etc.
9367 */
9368 if (enable_pml) {
9369 vmx->pml_pg = alloc_page(GFP_KERNEL | __GFP_ZERO);
9370 if (!vmx->pml_pg)
9371 goto uninit_vcpu;
9372 }
9373
9374 vmx->guest_msrs = kmalloc(PAGE_SIZE, GFP_KERNEL);
9375 BUILD_BUG_ON(ARRAY_SIZE(vmx_msr_index) * sizeof(vmx->guest_msrs[0])
9376 > PAGE_SIZE);
9377
9378 if (!vmx->guest_msrs)
9379 goto free_pml;
9380
9381 vmx->loaded_vmcs = &vmx->vmcs01;
9382 vmx->loaded_vmcs->vmcs = alloc_vmcs();
9383 vmx->loaded_vmcs->shadow_vmcs = NULL;
9384 if (!vmx->loaded_vmcs->vmcs)
9385 goto free_msrs;
9386 loaded_vmcs_init(vmx->loaded_vmcs);
9387
9388 cpu = get_cpu();
9389 vmx_vcpu_load(&vmx->vcpu, cpu);
9390 vmx->vcpu.cpu = cpu;
9391 err = vmx_vcpu_setup(vmx);
9392 vmx_vcpu_put(&vmx->vcpu);
9393 put_cpu();
9394 if (err)
9395 goto free_vmcs;
9396 if (cpu_need_virtualize_apic_accesses(&vmx->vcpu)) {
9397 err = alloc_apic_access_page(kvm);
9398 if (err)
9399 goto free_vmcs;
9400 }
9401
9402 if (enable_ept) {
9403 if (!kvm->arch.ept_identity_map_addr)
9404 kvm->arch.ept_identity_map_addr =
9405 VMX_EPT_IDENTITY_PAGETABLE_ADDR;
9406 err = init_rmode_identity_map(kvm);
9407 if (err)
9408 goto free_vmcs;
9409 }
9410
9411 if (nested) {
9412 nested_vmx_setup_ctls_msrs(vmx);
9413 vmx->nested.vpid02 = allocate_vpid();
9414 }
9415
9416 vmx->nested.posted_intr_nv = -1;
9417 vmx->nested.current_vmptr = -1ull;
9418
9419 vmx->msr_ia32_feature_control_valid_bits = FEATURE_CONTROL_LOCKED;
9420
9421 /*
9422 * Enforce invariant: pi_desc.nv is always either POSTED_INTR_VECTOR
9423 * or POSTED_INTR_WAKEUP_VECTOR.
9424 */
9425 vmx->pi_desc.nv = POSTED_INTR_VECTOR;
9426 vmx->pi_desc.sn = 1;
9427
9428 return &vmx->vcpu;
9429
9430 free_vmcs:
9431 free_vpid(vmx->nested.vpid02);
9432 free_loaded_vmcs(vmx->loaded_vmcs);
9433 free_msrs:
9434 kfree(vmx->guest_msrs);
9435 free_pml:
9436 vmx_destroy_pml_buffer(vmx);
9437 uninit_vcpu:
9438 kvm_vcpu_uninit(&vmx->vcpu);
9439 free_vcpu:
9440 free_vpid(vmx->vpid);
9441 kmem_cache_free(kvm_vcpu_cache, vmx);
9442 return ERR_PTR(err);
9443 }
9444
9445 static void __init vmx_check_processor_compat(void *rtn)
9446 {
9447 struct vmcs_config vmcs_conf;
9448
9449 *(int *)rtn = 0;
9450 if (setup_vmcs_config(&vmcs_conf) < 0)
9451 *(int *)rtn = -EIO;
9452 if (memcmp(&vmcs_config, &vmcs_conf, sizeof(struct vmcs_config)) != 0) {
9453 printk(KERN_ERR "kvm: CPU %d feature inconsistency!\n",
9454 smp_processor_id());
9455 *(int *)rtn = -EIO;
9456 }
9457 }
9458
9459 static int get_ept_level(void)
9460 {
9461 return VMX_EPT_DEFAULT_GAW + 1;
9462 }
9463
9464 static u64 vmx_get_mt_mask(struct kvm_vcpu *vcpu, gfn_t gfn, bool is_mmio)
9465 {
9466 u8 cache;
9467 u64 ipat = 0;
9468
9469 /* For VT-d and EPT combination
9470 * 1. MMIO: always map as UC
9471 * 2. EPT with VT-d:
9472 * a. VT-d without snooping control feature: can't guarantee the
9473 * result, try to trust guest.
9474 * b. VT-d with snooping control feature: snooping control feature of
9475 * VT-d engine can guarantee the cache correctness. Just set it
9476 * to WB to keep consistent with host. So the same as item 3.
9477 * 3. EPT without VT-d: always map as WB and set IPAT=1 to keep
9478 * consistent with host MTRR
9479 */
9480 if (is_mmio) {
9481 cache = MTRR_TYPE_UNCACHABLE;
9482 goto exit;
9483 }
9484
9485 if (!kvm_arch_has_noncoherent_dma(vcpu->kvm)) {
9486 ipat = VMX_EPT_IPAT_BIT;
9487 cache = MTRR_TYPE_WRBACK;
9488 goto exit;
9489 }
9490
9491 if (kvm_read_cr0(vcpu) & X86_CR0_CD) {
9492 ipat = VMX_EPT_IPAT_BIT;
9493 if (kvm_check_has_quirk(vcpu->kvm, KVM_X86_QUIRK_CD_NW_CLEARED))
9494 cache = MTRR_TYPE_WRBACK;
9495 else
9496 cache = MTRR_TYPE_UNCACHABLE;
9497 goto exit;
9498 }
9499
9500 cache = kvm_mtrr_get_guest_memory_type(vcpu, gfn);
9501
9502 exit:
9503 return (cache << VMX_EPT_MT_EPTE_SHIFT) | ipat;
9504 }
9505
9506 static int vmx_get_lpage_level(void)
9507 {
9508 if (enable_ept && !cpu_has_vmx_ept_1g_page())
9509 return PT_DIRECTORY_LEVEL;
9510 else
9511 /* For shadow and EPT supported 1GB page */
9512 return PT_PDPE_LEVEL;
9513 }
9514
9515 static void vmcs_set_secondary_exec_control(u32 new_ctl)
9516 {
9517 /*
9518 * These bits in the secondary execution controls field
9519 * are dynamic, the others are mostly based on the hypervisor
9520 * architecture and the guest's CPUID. Do not touch the
9521 * dynamic bits.
9522 */
9523 u32 mask =
9524 SECONDARY_EXEC_SHADOW_VMCS |
9525 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
9526 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
9527
9528 u32 cur_ctl = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
9529
9530 vmcs_write32(SECONDARY_VM_EXEC_CONTROL,
9531 (new_ctl & ~mask) | (cur_ctl & mask));
9532 }
9533
9534 /*
9535 * Generate MSR_IA32_VMX_CR{0,4}_FIXED1 according to CPUID. Only set bits
9536 * (indicating "allowed-1") if they are supported in the guest's CPUID.
9537 */
9538 static void nested_vmx_cr_fixed1_bits_update(struct kvm_vcpu *vcpu)
9539 {
9540 struct vcpu_vmx *vmx = to_vmx(vcpu);
9541 struct kvm_cpuid_entry2 *entry;
9542
9543 vmx->nested.nested_vmx_cr0_fixed1 = 0xffffffff;
9544 vmx->nested.nested_vmx_cr4_fixed1 = X86_CR4_PCE;
9545
9546 #define cr4_fixed1_update(_cr4_mask, _reg, _cpuid_mask) do { \
9547 if (entry && (entry->_reg & (_cpuid_mask))) \
9548 vmx->nested.nested_vmx_cr4_fixed1 |= (_cr4_mask); \
9549 } while (0)
9550
9551 entry = kvm_find_cpuid_entry(vcpu, 0x1, 0);
9552 cr4_fixed1_update(X86_CR4_VME, edx, bit(X86_FEATURE_VME));
9553 cr4_fixed1_update(X86_CR4_PVI, edx, bit(X86_FEATURE_VME));
9554 cr4_fixed1_update(X86_CR4_TSD, edx, bit(X86_FEATURE_TSC));
9555 cr4_fixed1_update(X86_CR4_DE, edx, bit(X86_FEATURE_DE));
9556 cr4_fixed1_update(X86_CR4_PSE, edx, bit(X86_FEATURE_PSE));
9557 cr4_fixed1_update(X86_CR4_PAE, edx, bit(X86_FEATURE_PAE));
9558 cr4_fixed1_update(X86_CR4_MCE, edx, bit(X86_FEATURE_MCE));
9559 cr4_fixed1_update(X86_CR4_PGE, edx, bit(X86_FEATURE_PGE));
9560 cr4_fixed1_update(X86_CR4_OSFXSR, edx, bit(X86_FEATURE_FXSR));
9561 cr4_fixed1_update(X86_CR4_OSXMMEXCPT, edx, bit(X86_FEATURE_XMM));
9562 cr4_fixed1_update(X86_CR4_VMXE, ecx, bit(X86_FEATURE_VMX));
9563 cr4_fixed1_update(X86_CR4_SMXE, ecx, bit(X86_FEATURE_SMX));
9564 cr4_fixed1_update(X86_CR4_PCIDE, ecx, bit(X86_FEATURE_PCID));
9565 cr4_fixed1_update(X86_CR4_OSXSAVE, ecx, bit(X86_FEATURE_XSAVE));
9566
9567 entry = kvm_find_cpuid_entry(vcpu, 0x7, 0);
9568 cr4_fixed1_update(X86_CR4_FSGSBASE, ebx, bit(X86_FEATURE_FSGSBASE));
9569 cr4_fixed1_update(X86_CR4_SMEP, ebx, bit(X86_FEATURE_SMEP));
9570 cr4_fixed1_update(X86_CR4_SMAP, ebx, bit(X86_FEATURE_SMAP));
9571 cr4_fixed1_update(X86_CR4_PKE, ecx, bit(X86_FEATURE_PKU));
9572 /* TODO: Use X86_CR4_UMIP and X86_FEATURE_UMIP macros */
9573 cr4_fixed1_update(bit(11), ecx, bit(2));
9574
9575 #undef cr4_fixed1_update
9576 }
9577
9578 static void vmx_cpuid_update(struct kvm_vcpu *vcpu)
9579 {
9580 struct kvm_cpuid_entry2 *best;
9581 struct vcpu_vmx *vmx = to_vmx(vcpu);
9582 u32 secondary_exec_ctl = vmx_secondary_exec_control(vmx);
9583
9584 if (vmx_rdtscp_supported()) {
9585 bool rdtscp_enabled = guest_cpuid_has_rdtscp(vcpu);
9586 if (!rdtscp_enabled)
9587 secondary_exec_ctl &= ~SECONDARY_EXEC_RDTSCP;
9588
9589 if (nested) {
9590 if (rdtscp_enabled)
9591 vmx->nested.nested_vmx_secondary_ctls_high |=
9592 SECONDARY_EXEC_RDTSCP;
9593 else
9594 vmx->nested.nested_vmx_secondary_ctls_high &=
9595 ~SECONDARY_EXEC_RDTSCP;
9596 }
9597 }
9598
9599 /* Exposing INVPCID only when PCID is exposed */
9600 best = kvm_find_cpuid_entry(vcpu, 0x7, 0);
9601 if (vmx_invpcid_supported() &&
9602 (!best || !(best->ebx & bit(X86_FEATURE_INVPCID)) ||
9603 !guest_cpuid_has_pcid(vcpu))) {
9604 secondary_exec_ctl &= ~SECONDARY_EXEC_ENABLE_INVPCID;
9605
9606 if (best)
9607 best->ebx &= ~bit(X86_FEATURE_INVPCID);
9608 }
9609
9610 if (cpu_has_secondary_exec_ctrls())
9611 vmcs_set_secondary_exec_control(secondary_exec_ctl);
9612
9613 if (nested_vmx_allowed(vcpu))
9614 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits |=
9615 FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
9616 else
9617 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits &=
9618 ~FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
9619
9620 if (nested_vmx_allowed(vcpu))
9621 nested_vmx_cr_fixed1_bits_update(vcpu);
9622 }
9623
9624 static void vmx_set_supported_cpuid(u32 func, struct kvm_cpuid_entry2 *entry)
9625 {
9626 if (func == 1 && nested)
9627 entry->ecx |= bit(X86_FEATURE_VMX);
9628 }
9629
9630 static void nested_ept_inject_page_fault(struct kvm_vcpu *vcpu,
9631 struct x86_exception *fault)
9632 {
9633 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
9634 struct vcpu_vmx *vmx = to_vmx(vcpu);
9635 u32 exit_reason;
9636 unsigned long exit_qualification = vcpu->arch.exit_qualification;
9637
9638 if (vmx->nested.pml_full) {
9639 exit_reason = EXIT_REASON_PML_FULL;
9640 vmx->nested.pml_full = false;
9641 exit_qualification &= INTR_INFO_UNBLOCK_NMI;
9642 } else if (fault->error_code & PFERR_RSVD_MASK)
9643 exit_reason = EXIT_REASON_EPT_MISCONFIG;
9644 else
9645 exit_reason = EXIT_REASON_EPT_VIOLATION;
9646
9647 nested_vmx_vmexit(vcpu, exit_reason, 0, exit_qualification);
9648 vmcs12->guest_physical_address = fault->address;
9649 }
9650
9651 static bool nested_ept_ad_enabled(struct kvm_vcpu *vcpu)
9652 {
9653 return nested_ept_get_cr3(vcpu) & VMX_EPT_AD_ENABLE_BIT;
9654 }
9655
9656 /* Callbacks for nested_ept_init_mmu_context: */
9657
9658 static unsigned long nested_ept_get_cr3(struct kvm_vcpu *vcpu)
9659 {
9660 /* return the page table to be shadowed - in our case, EPT12 */
9661 return get_vmcs12(vcpu)->ept_pointer;
9662 }
9663
9664 static int nested_ept_init_mmu_context(struct kvm_vcpu *vcpu)
9665 {
9666 bool wants_ad;
9667
9668 WARN_ON(mmu_is_nested(vcpu));
9669 wants_ad = nested_ept_ad_enabled(vcpu);
9670 if (wants_ad && !enable_ept_ad_bits)
9671 return 1;
9672
9673 kvm_mmu_unload(vcpu);
9674 kvm_init_shadow_ept_mmu(vcpu,
9675 to_vmx(vcpu)->nested.nested_vmx_ept_caps &
9676 VMX_EPT_EXECUTE_ONLY_BIT,
9677 wants_ad);
9678 vcpu->arch.mmu.set_cr3 = vmx_set_cr3;
9679 vcpu->arch.mmu.get_cr3 = nested_ept_get_cr3;
9680 vcpu->arch.mmu.inject_page_fault = nested_ept_inject_page_fault;
9681
9682 vcpu->arch.walk_mmu = &vcpu->arch.nested_mmu;
9683 return 0;
9684 }
9685
9686 static void nested_ept_uninit_mmu_context(struct kvm_vcpu *vcpu)
9687 {
9688 vcpu->arch.walk_mmu = &vcpu->arch.mmu;
9689 }
9690
9691 static bool nested_vmx_is_page_fault_vmexit(struct vmcs12 *vmcs12,
9692 u16 error_code)
9693 {
9694 bool inequality, bit;
9695
9696 bit = (vmcs12->exception_bitmap & (1u << PF_VECTOR)) != 0;
9697 inequality =
9698 (error_code & vmcs12->page_fault_error_code_mask) !=
9699 vmcs12->page_fault_error_code_match;
9700 return inequality ^ bit;
9701 }
9702
9703 static void vmx_inject_page_fault_nested(struct kvm_vcpu *vcpu,
9704 struct x86_exception *fault)
9705 {
9706 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
9707
9708 WARN_ON(!is_guest_mode(vcpu));
9709
9710 if (nested_vmx_is_page_fault_vmexit(vmcs12, fault->error_code)) {
9711 vmcs12->vm_exit_intr_error_code = fault->error_code;
9712 nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI,
9713 PF_VECTOR | INTR_TYPE_HARD_EXCEPTION |
9714 INTR_INFO_DELIVER_CODE_MASK | INTR_INFO_VALID_MASK,
9715 fault->address);
9716 } else {
9717 kvm_inject_page_fault(vcpu, fault);
9718 }
9719 }
9720
9721 static inline bool nested_vmx_merge_msr_bitmap(struct kvm_vcpu *vcpu,
9722 struct vmcs12 *vmcs12);
9723
9724 static void nested_get_vmcs12_pages(struct kvm_vcpu *vcpu,
9725 struct vmcs12 *vmcs12)
9726 {
9727 struct vcpu_vmx *vmx = to_vmx(vcpu);
9728 u64 hpa;
9729
9730 if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) {
9731 /*
9732 * Translate L1 physical address to host physical
9733 * address for vmcs02. Keep the page pinned, so this
9734 * physical address remains valid. We keep a reference
9735 * to it so we can release it later.
9736 */
9737 if (vmx->nested.apic_access_page) /* shouldn't happen */
9738 nested_release_page(vmx->nested.apic_access_page);
9739 vmx->nested.apic_access_page =
9740 nested_get_page(vcpu, vmcs12->apic_access_addr);
9741 /*
9742 * If translation failed, no matter: This feature asks
9743 * to exit when accessing the given address, and if it
9744 * can never be accessed, this feature won't do
9745 * anything anyway.
9746 */
9747 if (vmx->nested.apic_access_page) {
9748 hpa = page_to_phys(vmx->nested.apic_access_page);
9749 vmcs_write64(APIC_ACCESS_ADDR, hpa);
9750 } else {
9751 vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL,
9752 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES);
9753 }
9754 } else if (!(nested_cpu_has_virt_x2apic_mode(vmcs12)) &&
9755 cpu_need_virtualize_apic_accesses(&vmx->vcpu)) {
9756 vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL,
9757 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES);
9758 kvm_vcpu_reload_apic_access_page(vcpu);
9759 }
9760
9761 if (nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW)) {
9762 if (vmx->nested.virtual_apic_page) /* shouldn't happen */
9763 nested_release_page(vmx->nested.virtual_apic_page);
9764 vmx->nested.virtual_apic_page =
9765 nested_get_page(vcpu, vmcs12->virtual_apic_page_addr);
9766
9767 /*
9768 * If translation failed, VM entry will fail because
9769 * prepare_vmcs02 set VIRTUAL_APIC_PAGE_ADDR to -1ull.
9770 * Failing the vm entry is _not_ what the processor
9771 * does but it's basically the only possibility we
9772 * have. We could still enter the guest if CR8 load
9773 * exits are enabled, CR8 store exits are enabled, and
9774 * virtualize APIC access is disabled; in this case
9775 * the processor would never use the TPR shadow and we
9776 * could simply clear the bit from the execution
9777 * control. But such a configuration is useless, so
9778 * let's keep the code simple.
9779 */
9780 if (vmx->nested.virtual_apic_page) {
9781 hpa = page_to_phys(vmx->nested.virtual_apic_page);
9782 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, hpa);
9783 }
9784 }
9785
9786 if (nested_cpu_has_posted_intr(vmcs12)) {
9787 if (vmx->nested.pi_desc_page) { /* shouldn't happen */
9788 kunmap(vmx->nested.pi_desc_page);
9789 nested_release_page(vmx->nested.pi_desc_page);
9790 }
9791 vmx->nested.pi_desc_page =
9792 nested_get_page(vcpu, vmcs12->posted_intr_desc_addr);
9793 vmx->nested.pi_desc =
9794 (struct pi_desc *)kmap(vmx->nested.pi_desc_page);
9795 if (!vmx->nested.pi_desc) {
9796 nested_release_page_clean(vmx->nested.pi_desc_page);
9797 return;
9798 }
9799 vmx->nested.pi_desc =
9800 (struct pi_desc *)((void *)vmx->nested.pi_desc +
9801 (unsigned long)(vmcs12->posted_intr_desc_addr &
9802 (PAGE_SIZE - 1)));
9803 vmcs_write64(POSTED_INTR_DESC_ADDR,
9804 page_to_phys(vmx->nested.pi_desc_page) +
9805 (unsigned long)(vmcs12->posted_intr_desc_addr &
9806 (PAGE_SIZE - 1)));
9807 }
9808 if (cpu_has_vmx_msr_bitmap() &&
9809 nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS) &&
9810 nested_vmx_merge_msr_bitmap(vcpu, vmcs12))
9811 ;
9812 else
9813 vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL,
9814 CPU_BASED_USE_MSR_BITMAPS);
9815 }
9816
9817 static void vmx_start_preemption_timer(struct kvm_vcpu *vcpu)
9818 {
9819 u64 preemption_timeout = get_vmcs12(vcpu)->vmx_preemption_timer_value;
9820 struct vcpu_vmx *vmx = to_vmx(vcpu);
9821
9822 if (vcpu->arch.virtual_tsc_khz == 0)
9823 return;
9824
9825 /* Make sure short timeouts reliably trigger an immediate vmexit.
9826 * hrtimer_start does not guarantee this. */
9827 if (preemption_timeout <= 1) {
9828 vmx_preemption_timer_fn(&vmx->nested.preemption_timer);
9829 return;
9830 }
9831
9832 preemption_timeout <<= VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE;
9833 preemption_timeout *= 1000000;
9834 do_div(preemption_timeout, vcpu->arch.virtual_tsc_khz);
9835 hrtimer_start(&vmx->nested.preemption_timer,
9836 ns_to_ktime(preemption_timeout), HRTIMER_MODE_REL);
9837 }
9838
9839 static int nested_vmx_check_io_bitmap_controls(struct kvm_vcpu *vcpu,
9840 struct vmcs12 *vmcs12)
9841 {
9842 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_IO_BITMAPS))
9843 return 0;
9844
9845 if (!page_address_valid(vcpu, vmcs12->io_bitmap_a) ||
9846 !page_address_valid(vcpu, vmcs12->io_bitmap_b))
9847 return -EINVAL;
9848
9849 return 0;
9850 }
9851
9852 static int nested_vmx_check_msr_bitmap_controls(struct kvm_vcpu *vcpu,
9853 struct vmcs12 *vmcs12)
9854 {
9855 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS))
9856 return 0;
9857
9858 if (!page_address_valid(vcpu, vmcs12->msr_bitmap))
9859 return -EINVAL;
9860
9861 return 0;
9862 }
9863
9864 /*
9865 * Merge L0's and L1's MSR bitmap, return false to indicate that
9866 * we do not use the hardware.
9867 */
9868 static inline bool nested_vmx_merge_msr_bitmap(struct kvm_vcpu *vcpu,
9869 struct vmcs12 *vmcs12)
9870 {
9871 int msr;
9872 struct page *page;
9873 unsigned long *msr_bitmap_l1;
9874 unsigned long *msr_bitmap_l0 = to_vmx(vcpu)->nested.msr_bitmap;
9875
9876 /* This shortcut is ok because we support only x2APIC MSRs so far. */
9877 if (!nested_cpu_has_virt_x2apic_mode(vmcs12))
9878 return false;
9879
9880 page = nested_get_page(vcpu, vmcs12->msr_bitmap);
9881 if (!page)
9882 return false;
9883 msr_bitmap_l1 = (unsigned long *)kmap(page);
9884
9885 memset(msr_bitmap_l0, 0xff, PAGE_SIZE);
9886
9887 if (nested_cpu_has_virt_x2apic_mode(vmcs12)) {
9888 if (nested_cpu_has_apic_reg_virt(vmcs12))
9889 for (msr = 0x800; msr <= 0x8ff; msr++)
9890 nested_vmx_disable_intercept_for_msr(
9891 msr_bitmap_l1, msr_bitmap_l0,
9892 msr, MSR_TYPE_R);
9893
9894 nested_vmx_disable_intercept_for_msr(
9895 msr_bitmap_l1, msr_bitmap_l0,
9896 APIC_BASE_MSR + (APIC_TASKPRI >> 4),
9897 MSR_TYPE_R | MSR_TYPE_W);
9898
9899 if (nested_cpu_has_vid(vmcs12)) {
9900 nested_vmx_disable_intercept_for_msr(
9901 msr_bitmap_l1, msr_bitmap_l0,
9902 APIC_BASE_MSR + (APIC_EOI >> 4),
9903 MSR_TYPE_W);
9904 nested_vmx_disable_intercept_for_msr(
9905 msr_bitmap_l1, msr_bitmap_l0,
9906 APIC_BASE_MSR + (APIC_SELF_IPI >> 4),
9907 MSR_TYPE_W);
9908 }
9909 }
9910 kunmap(page);
9911 nested_release_page_clean(page);
9912
9913 return true;
9914 }
9915
9916 static int nested_vmx_check_apicv_controls(struct kvm_vcpu *vcpu,
9917 struct vmcs12 *vmcs12)
9918 {
9919 if (!nested_cpu_has_virt_x2apic_mode(vmcs12) &&
9920 !nested_cpu_has_apic_reg_virt(vmcs12) &&
9921 !nested_cpu_has_vid(vmcs12) &&
9922 !nested_cpu_has_posted_intr(vmcs12))
9923 return 0;
9924
9925 /*
9926 * If virtualize x2apic mode is enabled,
9927 * virtualize apic access must be disabled.
9928 */
9929 if (nested_cpu_has_virt_x2apic_mode(vmcs12) &&
9930 nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES))
9931 return -EINVAL;
9932
9933 /*
9934 * If virtual interrupt delivery is enabled,
9935 * we must exit on external interrupts.
9936 */
9937 if (nested_cpu_has_vid(vmcs12) &&
9938 !nested_exit_on_intr(vcpu))
9939 return -EINVAL;
9940
9941 /*
9942 * bits 15:8 should be zero in posted_intr_nv,
9943 * the descriptor address has been already checked
9944 * in nested_get_vmcs12_pages.
9945 */
9946 if (nested_cpu_has_posted_intr(vmcs12) &&
9947 (!nested_cpu_has_vid(vmcs12) ||
9948 !nested_exit_intr_ack_set(vcpu) ||
9949 vmcs12->posted_intr_nv & 0xff00))
9950 return -EINVAL;
9951
9952 /* tpr shadow is needed by all apicv features. */
9953 if (!nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW))
9954 return -EINVAL;
9955
9956 return 0;
9957 }
9958
9959 static int nested_vmx_check_msr_switch(struct kvm_vcpu *vcpu,
9960 unsigned long count_field,
9961 unsigned long addr_field)
9962 {
9963 int maxphyaddr;
9964 u64 count, addr;
9965
9966 if (vmcs12_read_any(vcpu, count_field, &count) ||
9967 vmcs12_read_any(vcpu, addr_field, &addr)) {
9968 WARN_ON(1);
9969 return -EINVAL;
9970 }
9971 if (count == 0)
9972 return 0;
9973 maxphyaddr = cpuid_maxphyaddr(vcpu);
9974 if (!IS_ALIGNED(addr, 16) || addr >> maxphyaddr ||
9975 (addr + count * sizeof(struct vmx_msr_entry) - 1) >> maxphyaddr) {
9976 pr_debug_ratelimited(
9977 "nVMX: invalid MSR switch (0x%lx, %d, %llu, 0x%08llx)",
9978 addr_field, maxphyaddr, count, addr);
9979 return -EINVAL;
9980 }
9981 return 0;
9982 }
9983
9984 static int nested_vmx_check_msr_switch_controls(struct kvm_vcpu *vcpu,
9985 struct vmcs12 *vmcs12)
9986 {
9987 if (vmcs12->vm_exit_msr_load_count == 0 &&
9988 vmcs12->vm_exit_msr_store_count == 0 &&
9989 vmcs12->vm_entry_msr_load_count == 0)
9990 return 0; /* Fast path */
9991 if (nested_vmx_check_msr_switch(vcpu, VM_EXIT_MSR_LOAD_COUNT,
9992 VM_EXIT_MSR_LOAD_ADDR) ||
9993 nested_vmx_check_msr_switch(vcpu, VM_EXIT_MSR_STORE_COUNT,
9994 VM_EXIT_MSR_STORE_ADDR) ||
9995 nested_vmx_check_msr_switch(vcpu, VM_ENTRY_MSR_LOAD_COUNT,
9996 VM_ENTRY_MSR_LOAD_ADDR))
9997 return -EINVAL;
9998 return 0;
9999 }
10000
10001 static int nested_vmx_check_pml_controls(struct kvm_vcpu *vcpu,
10002 struct vmcs12 *vmcs12)
10003 {
10004 u64 address = vmcs12->pml_address;
10005 int maxphyaddr = cpuid_maxphyaddr(vcpu);
10006
10007 if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_PML)) {
10008 if (!nested_cpu_has_ept(vmcs12) ||
10009 !IS_ALIGNED(address, 4096) ||
10010 address >> maxphyaddr)
10011 return -EINVAL;
10012 }
10013
10014 return 0;
10015 }
10016
10017 static int nested_vmx_msr_check_common(struct kvm_vcpu *vcpu,
10018 struct vmx_msr_entry *e)
10019 {
10020 /* x2APIC MSR accesses are not allowed */
10021 if (vcpu->arch.apic_base & X2APIC_ENABLE && e->index >> 8 == 0x8)
10022 return -EINVAL;
10023 if (e->index == MSR_IA32_UCODE_WRITE || /* SDM Table 35-2 */
10024 e->index == MSR_IA32_UCODE_REV)
10025 return -EINVAL;
10026 if (e->reserved != 0)
10027 return -EINVAL;
10028 return 0;
10029 }
10030
10031 static int nested_vmx_load_msr_check(struct kvm_vcpu *vcpu,
10032 struct vmx_msr_entry *e)
10033 {
10034 if (e->index == MSR_FS_BASE ||
10035 e->index == MSR_GS_BASE ||
10036 e->index == MSR_IA32_SMM_MONITOR_CTL || /* SMM is not supported */
10037 nested_vmx_msr_check_common(vcpu, e))
10038 return -EINVAL;
10039 return 0;
10040 }
10041
10042 static int nested_vmx_store_msr_check(struct kvm_vcpu *vcpu,
10043 struct vmx_msr_entry *e)
10044 {
10045 if (e->index == MSR_IA32_SMBASE || /* SMM is not supported */
10046 nested_vmx_msr_check_common(vcpu, e))
10047 return -EINVAL;
10048 return 0;
10049 }
10050
10051 /*
10052 * Load guest's/host's msr at nested entry/exit.
10053 * return 0 for success, entry index for failure.
10054 */
10055 static u32 nested_vmx_load_msr(struct kvm_vcpu *vcpu, u64 gpa, u32 count)
10056 {
10057 u32 i;
10058 struct vmx_msr_entry e;
10059 struct msr_data msr;
10060
10061 msr.host_initiated = false;
10062 for (i = 0; i < count; i++) {
10063 if (kvm_vcpu_read_guest(vcpu, gpa + i * sizeof(e),
10064 &e, sizeof(e))) {
10065 pr_debug_ratelimited(
10066 "%s cannot read MSR entry (%u, 0x%08llx)\n",
10067 __func__, i, gpa + i * sizeof(e));
10068 goto fail;
10069 }
10070 if (nested_vmx_load_msr_check(vcpu, &e)) {
10071 pr_debug_ratelimited(
10072 "%s check failed (%u, 0x%x, 0x%x)\n",
10073 __func__, i, e.index, e.reserved);
10074 goto fail;
10075 }
10076 msr.index = e.index;
10077 msr.data = e.value;
10078 if (kvm_set_msr(vcpu, &msr)) {
10079 pr_debug_ratelimited(
10080 "%s cannot write MSR (%u, 0x%x, 0x%llx)\n",
10081 __func__, i, e.index, e.value);
10082 goto fail;
10083 }
10084 }
10085 return 0;
10086 fail:
10087 return i + 1;
10088 }
10089
10090 static int nested_vmx_store_msr(struct kvm_vcpu *vcpu, u64 gpa, u32 count)
10091 {
10092 u32 i;
10093 struct vmx_msr_entry e;
10094
10095 for (i = 0; i < count; i++) {
10096 struct msr_data msr_info;
10097 if (kvm_vcpu_read_guest(vcpu,
10098 gpa + i * sizeof(e),
10099 &e, 2 * sizeof(u32))) {
10100 pr_debug_ratelimited(
10101 "%s cannot read MSR entry (%u, 0x%08llx)\n",
10102 __func__, i, gpa + i * sizeof(e));
10103 return -EINVAL;
10104 }
10105 if (nested_vmx_store_msr_check(vcpu, &e)) {
10106 pr_debug_ratelimited(
10107 "%s check failed (%u, 0x%x, 0x%x)\n",
10108 __func__, i, e.index, e.reserved);
10109 return -EINVAL;
10110 }
10111 msr_info.host_initiated = false;
10112 msr_info.index = e.index;
10113 if (kvm_get_msr(vcpu, &msr_info)) {
10114 pr_debug_ratelimited(
10115 "%s cannot read MSR (%u, 0x%x)\n",
10116 __func__, i, e.index);
10117 return -EINVAL;
10118 }
10119 if (kvm_vcpu_write_guest(vcpu,
10120 gpa + i * sizeof(e) +
10121 offsetof(struct vmx_msr_entry, value),
10122 &msr_info.data, sizeof(msr_info.data))) {
10123 pr_debug_ratelimited(
10124 "%s cannot write MSR (%u, 0x%x, 0x%llx)\n",
10125 __func__, i, e.index, msr_info.data);
10126 return -EINVAL;
10127 }
10128 }
10129 return 0;
10130 }
10131
10132 static bool nested_cr3_valid(struct kvm_vcpu *vcpu, unsigned long val)
10133 {
10134 unsigned long invalid_mask;
10135
10136 invalid_mask = (~0ULL) << cpuid_maxphyaddr(vcpu);
10137 return (val & invalid_mask) == 0;
10138 }
10139
10140 /*
10141 * Load guest's/host's cr3 at nested entry/exit. nested_ept is true if we are
10142 * emulating VM entry into a guest with EPT enabled.
10143 * Returns 0 on success, 1 on failure. Invalid state exit qualification code
10144 * is assigned to entry_failure_code on failure.
10145 */
10146 static int nested_vmx_load_cr3(struct kvm_vcpu *vcpu, unsigned long cr3, bool nested_ept,
10147 u32 *entry_failure_code)
10148 {
10149 if (cr3 != kvm_read_cr3(vcpu) || (!nested_ept && pdptrs_changed(vcpu))) {
10150 if (!nested_cr3_valid(vcpu, cr3)) {
10151 *entry_failure_code = ENTRY_FAIL_DEFAULT;
10152 return 1;
10153 }
10154
10155 /*
10156 * If PAE paging and EPT are both on, CR3 is not used by the CPU and
10157 * must not be dereferenced.
10158 */
10159 if (!is_long_mode(vcpu) && is_pae(vcpu) && is_paging(vcpu) &&
10160 !nested_ept) {
10161 if (!load_pdptrs(vcpu, vcpu->arch.walk_mmu, cr3)) {
10162 *entry_failure_code = ENTRY_FAIL_PDPTE;
10163 return 1;
10164 }
10165 }
10166
10167 vcpu->arch.cr3 = cr3;
10168 __set_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail);
10169 }
10170
10171 kvm_mmu_reset_context(vcpu);
10172 return 0;
10173 }
10174
10175 /*
10176 * prepare_vmcs02 is called when the L1 guest hypervisor runs its nested
10177 * L2 guest. L1 has a vmcs for L2 (vmcs12), and this function "merges" it
10178 * with L0's requirements for its guest (a.k.a. vmcs01), so we can run the L2
10179 * guest in a way that will both be appropriate to L1's requests, and our
10180 * needs. In addition to modifying the active vmcs (which is vmcs02), this
10181 * function also has additional necessary side-effects, like setting various
10182 * vcpu->arch fields.
10183 * Returns 0 on success, 1 on failure. Invalid state exit qualification code
10184 * is assigned to entry_failure_code on failure.
10185 */
10186 static int prepare_vmcs02(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12,
10187 bool from_vmentry, u32 *entry_failure_code)
10188 {
10189 struct vcpu_vmx *vmx = to_vmx(vcpu);
10190 u32 exec_control, vmcs12_exec_ctrl;
10191
10192 vmcs_write16(GUEST_ES_SELECTOR, vmcs12->guest_es_selector);
10193 vmcs_write16(GUEST_CS_SELECTOR, vmcs12->guest_cs_selector);
10194 vmcs_write16(GUEST_SS_SELECTOR, vmcs12->guest_ss_selector);
10195 vmcs_write16(GUEST_DS_SELECTOR, vmcs12->guest_ds_selector);
10196 vmcs_write16(GUEST_FS_SELECTOR, vmcs12->guest_fs_selector);
10197 vmcs_write16(GUEST_GS_SELECTOR, vmcs12->guest_gs_selector);
10198 vmcs_write16(GUEST_LDTR_SELECTOR, vmcs12->guest_ldtr_selector);
10199 vmcs_write16(GUEST_TR_SELECTOR, vmcs12->guest_tr_selector);
10200 vmcs_write32(GUEST_ES_LIMIT, vmcs12->guest_es_limit);
10201 vmcs_write32(GUEST_CS_LIMIT, vmcs12->guest_cs_limit);
10202 vmcs_write32(GUEST_SS_LIMIT, vmcs12->guest_ss_limit);
10203 vmcs_write32(GUEST_DS_LIMIT, vmcs12->guest_ds_limit);
10204 vmcs_write32(GUEST_FS_LIMIT, vmcs12->guest_fs_limit);
10205 vmcs_write32(GUEST_GS_LIMIT, vmcs12->guest_gs_limit);
10206 vmcs_write32(GUEST_LDTR_LIMIT, vmcs12->guest_ldtr_limit);
10207 vmcs_write32(GUEST_TR_LIMIT, vmcs12->guest_tr_limit);
10208 vmcs_write32(GUEST_GDTR_LIMIT, vmcs12->guest_gdtr_limit);
10209 vmcs_write32(GUEST_IDTR_LIMIT, vmcs12->guest_idtr_limit);
10210 vmcs_write32(GUEST_ES_AR_BYTES, vmcs12->guest_es_ar_bytes);
10211 vmcs_write32(GUEST_CS_AR_BYTES, vmcs12->guest_cs_ar_bytes);
10212 vmcs_write32(GUEST_SS_AR_BYTES, vmcs12->guest_ss_ar_bytes);
10213 vmcs_write32(GUEST_DS_AR_BYTES, vmcs12->guest_ds_ar_bytes);
10214 vmcs_write32(GUEST_FS_AR_BYTES, vmcs12->guest_fs_ar_bytes);
10215 vmcs_write32(GUEST_GS_AR_BYTES, vmcs12->guest_gs_ar_bytes);
10216 vmcs_write32(GUEST_LDTR_AR_BYTES, vmcs12->guest_ldtr_ar_bytes);
10217 vmcs_write32(GUEST_TR_AR_BYTES, vmcs12->guest_tr_ar_bytes);
10218 vmcs_writel(GUEST_ES_BASE, vmcs12->guest_es_base);
10219 vmcs_writel(GUEST_CS_BASE, vmcs12->guest_cs_base);
10220 vmcs_writel(GUEST_SS_BASE, vmcs12->guest_ss_base);
10221 vmcs_writel(GUEST_DS_BASE, vmcs12->guest_ds_base);
10222 vmcs_writel(GUEST_FS_BASE, vmcs12->guest_fs_base);
10223 vmcs_writel(GUEST_GS_BASE, vmcs12->guest_gs_base);
10224 vmcs_writel(GUEST_LDTR_BASE, vmcs12->guest_ldtr_base);
10225 vmcs_writel(GUEST_TR_BASE, vmcs12->guest_tr_base);
10226 vmcs_writel(GUEST_GDTR_BASE, vmcs12->guest_gdtr_base);
10227 vmcs_writel(GUEST_IDTR_BASE, vmcs12->guest_idtr_base);
10228
10229 if (from_vmentry &&
10230 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS)) {
10231 kvm_set_dr(vcpu, 7, vmcs12->guest_dr7);
10232 vmcs_write64(GUEST_IA32_DEBUGCTL, vmcs12->guest_ia32_debugctl);
10233 } else {
10234 kvm_set_dr(vcpu, 7, vcpu->arch.dr7);
10235 vmcs_write64(GUEST_IA32_DEBUGCTL, vmx->nested.vmcs01_debugctl);
10236 }
10237 if (from_vmentry) {
10238 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
10239 vmcs12->vm_entry_intr_info_field);
10240 vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE,
10241 vmcs12->vm_entry_exception_error_code);
10242 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
10243 vmcs12->vm_entry_instruction_len);
10244 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO,
10245 vmcs12->guest_interruptibility_info);
10246 vmx->loaded_vmcs->nmi_known_unmasked =
10247 !(vmcs12->guest_interruptibility_info & GUEST_INTR_STATE_NMI);
10248 } else {
10249 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0);
10250 }
10251 vmcs_write32(GUEST_SYSENTER_CS, vmcs12->guest_sysenter_cs);
10252 vmx_set_rflags(vcpu, vmcs12->guest_rflags);
10253 vmcs_writel(GUEST_PENDING_DBG_EXCEPTIONS,
10254 vmcs12->guest_pending_dbg_exceptions);
10255 vmcs_writel(GUEST_SYSENTER_ESP, vmcs12->guest_sysenter_esp);
10256 vmcs_writel(GUEST_SYSENTER_EIP, vmcs12->guest_sysenter_eip);
10257
10258 if (nested_cpu_has_xsaves(vmcs12))
10259 vmcs_write64(XSS_EXIT_BITMAP, vmcs12->xss_exit_bitmap);
10260 vmcs_write64(VMCS_LINK_POINTER, -1ull);
10261
10262 exec_control = vmcs12->pin_based_vm_exec_control;
10263
10264 /* Preemption timer setting is only taken from vmcs01. */
10265 exec_control &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
10266 exec_control |= vmcs_config.pin_based_exec_ctrl;
10267 if (vmx->hv_deadline_tsc == -1)
10268 exec_control &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
10269
10270 /* Posted interrupts setting is only taken from vmcs12. */
10271 if (nested_cpu_has_posted_intr(vmcs12)) {
10272 vmx->nested.posted_intr_nv = vmcs12->posted_intr_nv;
10273 vmx->nested.pi_pending = false;
10274 vmcs_write16(POSTED_INTR_NV, POSTED_INTR_NESTED_VECTOR);
10275 } else {
10276 exec_control &= ~PIN_BASED_POSTED_INTR;
10277 }
10278
10279 vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, exec_control);
10280
10281 vmx->nested.preemption_timer_expired = false;
10282 if (nested_cpu_has_preemption_timer(vmcs12))
10283 vmx_start_preemption_timer(vcpu);
10284
10285 /*
10286 * Whether page-faults are trapped is determined by a combination of
10287 * 3 settings: PFEC_MASK, PFEC_MATCH and EXCEPTION_BITMAP.PF.
10288 * If enable_ept, L0 doesn't care about page faults and we should
10289 * set all of these to L1's desires. However, if !enable_ept, L0 does
10290 * care about (at least some) page faults, and because it is not easy
10291 * (if at all possible?) to merge L0 and L1's desires, we simply ask
10292 * to exit on each and every L2 page fault. This is done by setting
10293 * MASK=MATCH=0 and (see below) EB.PF=1.
10294 * Note that below we don't need special code to set EB.PF beyond the
10295 * "or"ing of the EB of vmcs01 and vmcs12, because when enable_ept,
10296 * vmcs01's EB.PF is 0 so the "or" will take vmcs12's value, and when
10297 * !enable_ept, EB.PF is 1, so the "or" will always be 1.
10298 */
10299 vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK,
10300 enable_ept ? vmcs12->page_fault_error_code_mask : 0);
10301 vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH,
10302 enable_ept ? vmcs12->page_fault_error_code_match : 0);
10303
10304 if (cpu_has_secondary_exec_ctrls()) {
10305 exec_control = vmx_secondary_exec_control(vmx);
10306
10307 /* Take the following fields only from vmcs12 */
10308 exec_control &= ~(SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
10309 SECONDARY_EXEC_RDTSCP |
10310 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY |
10311 SECONDARY_EXEC_APIC_REGISTER_VIRT);
10312 if (nested_cpu_has(vmcs12,
10313 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS)) {
10314 vmcs12_exec_ctrl = vmcs12->secondary_vm_exec_control &
10315 ~SECONDARY_EXEC_ENABLE_PML;
10316 exec_control |= vmcs12_exec_ctrl;
10317 }
10318
10319 if (exec_control & SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY) {
10320 vmcs_write64(EOI_EXIT_BITMAP0,
10321 vmcs12->eoi_exit_bitmap0);
10322 vmcs_write64(EOI_EXIT_BITMAP1,
10323 vmcs12->eoi_exit_bitmap1);
10324 vmcs_write64(EOI_EXIT_BITMAP2,
10325 vmcs12->eoi_exit_bitmap2);
10326 vmcs_write64(EOI_EXIT_BITMAP3,
10327 vmcs12->eoi_exit_bitmap3);
10328 vmcs_write16(GUEST_INTR_STATUS,
10329 vmcs12->guest_intr_status);
10330 }
10331
10332 /*
10333 * Write an illegal value to APIC_ACCESS_ADDR. Later,
10334 * nested_get_vmcs12_pages will either fix it up or
10335 * remove the VM execution control.
10336 */
10337 if (exec_control & SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)
10338 vmcs_write64(APIC_ACCESS_ADDR, -1ull);
10339
10340 vmcs_write32(SECONDARY_VM_EXEC_CONTROL, exec_control);
10341 }
10342
10343
10344 /*
10345 * Set host-state according to L0's settings (vmcs12 is irrelevant here)
10346 * Some constant fields are set here by vmx_set_constant_host_state().
10347 * Other fields are different per CPU, and will be set later when
10348 * vmx_vcpu_load() is called, and when vmx_save_host_state() is called.
10349 */
10350 vmx_set_constant_host_state(vmx);
10351
10352 /*
10353 * Set the MSR load/store lists to match L0's settings.
10354 */
10355 vmcs_write32(VM_EXIT_MSR_STORE_COUNT, 0);
10356 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, vmx->msr_autoload.nr);
10357 vmcs_write64(VM_EXIT_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.host));
10358 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, vmx->msr_autoload.nr);
10359 vmcs_write64(VM_ENTRY_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.guest));
10360
10361 /*
10362 * HOST_RSP is normally set correctly in vmx_vcpu_run() just before
10363 * entry, but only if the current (host) sp changed from the value
10364 * we wrote last (vmx->host_rsp). This cache is no longer relevant
10365 * if we switch vmcs, and rather than hold a separate cache per vmcs,
10366 * here we just force the write to happen on entry.
10367 */
10368 vmx->host_rsp = 0;
10369
10370 exec_control = vmx_exec_control(vmx); /* L0's desires */
10371 exec_control &= ~CPU_BASED_VIRTUAL_INTR_PENDING;
10372 exec_control &= ~CPU_BASED_VIRTUAL_NMI_PENDING;
10373 exec_control &= ~CPU_BASED_TPR_SHADOW;
10374 exec_control |= vmcs12->cpu_based_vm_exec_control;
10375
10376 /*
10377 * Write an illegal value to VIRTUAL_APIC_PAGE_ADDR. Later, if
10378 * nested_get_vmcs12_pages can't fix it up, the illegal value
10379 * will result in a VM entry failure.
10380 */
10381 if (exec_control & CPU_BASED_TPR_SHADOW) {
10382 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, -1ull);
10383 vmcs_write32(TPR_THRESHOLD, vmcs12->tpr_threshold);
10384 } else {
10385 #ifdef CONFIG_X86_64
10386 exec_control |= CPU_BASED_CR8_LOAD_EXITING |
10387 CPU_BASED_CR8_STORE_EXITING;
10388 #endif
10389 }
10390
10391 /*
10392 * Merging of IO bitmap not currently supported.
10393 * Rather, exit every time.
10394 */
10395 exec_control &= ~CPU_BASED_USE_IO_BITMAPS;
10396 exec_control |= CPU_BASED_UNCOND_IO_EXITING;
10397
10398 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, exec_control);
10399
10400 /* EXCEPTION_BITMAP and CR0_GUEST_HOST_MASK should basically be the
10401 * bitwise-or of what L1 wants to trap for L2, and what we want to
10402 * trap. Note that CR0.TS also needs updating - we do this later.
10403 */
10404 update_exception_bitmap(vcpu);
10405 vcpu->arch.cr0_guest_owned_bits &= ~vmcs12->cr0_guest_host_mask;
10406 vmcs_writel(CR0_GUEST_HOST_MASK, ~vcpu->arch.cr0_guest_owned_bits);
10407
10408 /* L2->L1 exit controls are emulated - the hardware exit is to L0 so
10409 * we should use its exit controls. Note that VM_EXIT_LOAD_IA32_EFER
10410 * bits are further modified by vmx_set_efer() below.
10411 */
10412 vmcs_write32(VM_EXIT_CONTROLS, vmcs_config.vmexit_ctrl);
10413
10414 /* vmcs12's VM_ENTRY_LOAD_IA32_EFER and VM_ENTRY_IA32E_MODE are
10415 * emulated by vmx_set_efer(), below.
10416 */
10417 vm_entry_controls_init(vmx,
10418 (vmcs12->vm_entry_controls & ~VM_ENTRY_LOAD_IA32_EFER &
10419 ~VM_ENTRY_IA32E_MODE) |
10420 (vmcs_config.vmentry_ctrl & ~VM_ENTRY_IA32E_MODE));
10421
10422 if (from_vmentry &&
10423 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_PAT)) {
10424 vmcs_write64(GUEST_IA32_PAT, vmcs12->guest_ia32_pat);
10425 vcpu->arch.pat = vmcs12->guest_ia32_pat;
10426 } else if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) {
10427 vmcs_write64(GUEST_IA32_PAT, vmx->vcpu.arch.pat);
10428 }
10429
10430 set_cr4_guest_host_mask(vmx);
10431
10432 if (from_vmentry &&
10433 vmcs12->vm_entry_controls & VM_ENTRY_LOAD_BNDCFGS)
10434 vmcs_write64(GUEST_BNDCFGS, vmcs12->guest_bndcfgs);
10435
10436 if (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING)
10437 vmcs_write64(TSC_OFFSET,
10438 vcpu->arch.tsc_offset + vmcs12->tsc_offset);
10439 else
10440 vmcs_write64(TSC_OFFSET, vcpu->arch.tsc_offset);
10441 if (kvm_has_tsc_control)
10442 decache_tsc_multiplier(vmx);
10443
10444 if (enable_vpid) {
10445 /*
10446 * There is no direct mapping between vpid02 and vpid12, the
10447 * vpid02 is per-vCPU for L0 and reused while the value of
10448 * vpid12 is changed w/ one invvpid during nested vmentry.
10449 * The vpid12 is allocated by L1 for L2, so it will not
10450 * influence global bitmap(for vpid01 and vpid02 allocation)
10451 * even if spawn a lot of nested vCPUs.
10452 */
10453 if (nested_cpu_has_vpid(vmcs12) && vmx->nested.vpid02) {
10454 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->nested.vpid02);
10455 if (vmcs12->virtual_processor_id != vmx->nested.last_vpid) {
10456 vmx->nested.last_vpid = vmcs12->virtual_processor_id;
10457 __vmx_flush_tlb(vcpu, to_vmx(vcpu)->nested.vpid02);
10458 }
10459 } else {
10460 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid);
10461 vmx_flush_tlb(vcpu);
10462 }
10463
10464 }
10465
10466 if (enable_pml) {
10467 /*
10468 * Conceptually we want to copy the PML address and index from
10469 * vmcs01 here, and then back to vmcs01 on nested vmexit. But,
10470 * since we always flush the log on each vmexit, this happens
10471 * to be equivalent to simply resetting the fields in vmcs02.
10472 */
10473 ASSERT(vmx->pml_pg);
10474 vmcs_write64(PML_ADDRESS, page_to_phys(vmx->pml_pg));
10475 vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1);
10476 }
10477
10478 if (nested_cpu_has_ept(vmcs12)) {
10479 if (nested_ept_init_mmu_context(vcpu)) {
10480 *entry_failure_code = ENTRY_FAIL_DEFAULT;
10481 return 1;
10482 }
10483 } else if (nested_cpu_has2(vmcs12,
10484 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) {
10485 vmx_flush_tlb_ept_only(vcpu);
10486 }
10487
10488 /*
10489 * This sets GUEST_CR0 to vmcs12->guest_cr0, possibly modifying those
10490 * bits which we consider mandatory enabled.
10491 * The CR0_READ_SHADOW is what L2 should have expected to read given
10492 * the specifications by L1; It's not enough to take
10493 * vmcs12->cr0_read_shadow because on our cr0_guest_host_mask we we
10494 * have more bits than L1 expected.
10495 */
10496 vmx_set_cr0(vcpu, vmcs12->guest_cr0);
10497 vmcs_writel(CR0_READ_SHADOW, nested_read_cr0(vmcs12));
10498
10499 vmx_set_cr4(vcpu, vmcs12->guest_cr4);
10500 vmcs_writel(CR4_READ_SHADOW, nested_read_cr4(vmcs12));
10501
10502 if (from_vmentry &&
10503 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_EFER))
10504 vcpu->arch.efer = vmcs12->guest_ia32_efer;
10505 else if (vmcs12->vm_entry_controls & VM_ENTRY_IA32E_MODE)
10506 vcpu->arch.efer |= (EFER_LMA | EFER_LME);
10507 else
10508 vcpu->arch.efer &= ~(EFER_LMA | EFER_LME);
10509 /* Note: modifies VM_ENTRY/EXIT_CONTROLS and GUEST/HOST_IA32_EFER */
10510 vmx_set_efer(vcpu, vcpu->arch.efer);
10511
10512 /* Shadow page tables on either EPT or shadow page tables. */
10513 if (nested_vmx_load_cr3(vcpu, vmcs12->guest_cr3, nested_cpu_has_ept(vmcs12),
10514 entry_failure_code))
10515 return 1;
10516
10517 if (!enable_ept)
10518 vcpu->arch.walk_mmu->inject_page_fault = vmx_inject_page_fault_nested;
10519
10520 /*
10521 * L1 may access the L2's PDPTR, so save them to construct vmcs12
10522 */
10523 if (enable_ept) {
10524 vmcs_write64(GUEST_PDPTR0, vmcs12->guest_pdptr0);
10525 vmcs_write64(GUEST_PDPTR1, vmcs12->guest_pdptr1);
10526 vmcs_write64(GUEST_PDPTR2, vmcs12->guest_pdptr2);
10527 vmcs_write64(GUEST_PDPTR3, vmcs12->guest_pdptr3);
10528 }
10529
10530 kvm_register_write(vcpu, VCPU_REGS_RSP, vmcs12->guest_rsp);
10531 kvm_register_write(vcpu, VCPU_REGS_RIP, vmcs12->guest_rip);
10532 return 0;
10533 }
10534
10535 static int check_vmentry_prereqs(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
10536 {
10537 struct vcpu_vmx *vmx = to_vmx(vcpu);
10538
10539 if (vmcs12->guest_activity_state != GUEST_ACTIVITY_ACTIVE &&
10540 vmcs12->guest_activity_state != GUEST_ACTIVITY_HLT)
10541 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
10542
10543 if (nested_vmx_check_io_bitmap_controls(vcpu, vmcs12))
10544 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
10545
10546 if (nested_vmx_check_msr_bitmap_controls(vcpu, vmcs12))
10547 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
10548
10549 if (nested_vmx_check_apicv_controls(vcpu, vmcs12))
10550 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
10551
10552 if (nested_vmx_check_msr_switch_controls(vcpu, vmcs12))
10553 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
10554
10555 if (nested_vmx_check_pml_controls(vcpu, vmcs12))
10556 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
10557
10558 if (!vmx_control_verify(vmcs12->cpu_based_vm_exec_control,
10559 vmx->nested.nested_vmx_procbased_ctls_low,
10560 vmx->nested.nested_vmx_procbased_ctls_high) ||
10561 (nested_cpu_has(vmcs12, CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) &&
10562 !vmx_control_verify(vmcs12->secondary_vm_exec_control,
10563 vmx->nested.nested_vmx_secondary_ctls_low,
10564 vmx->nested.nested_vmx_secondary_ctls_high)) ||
10565 !vmx_control_verify(vmcs12->pin_based_vm_exec_control,
10566 vmx->nested.nested_vmx_pinbased_ctls_low,
10567 vmx->nested.nested_vmx_pinbased_ctls_high) ||
10568 !vmx_control_verify(vmcs12->vm_exit_controls,
10569 vmx->nested.nested_vmx_exit_ctls_low,
10570 vmx->nested.nested_vmx_exit_ctls_high) ||
10571 !vmx_control_verify(vmcs12->vm_entry_controls,
10572 vmx->nested.nested_vmx_entry_ctls_low,
10573 vmx->nested.nested_vmx_entry_ctls_high))
10574 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
10575
10576 if (vmcs12->cr3_target_count > nested_cpu_vmx_misc_cr3_count(vcpu))
10577 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
10578
10579 if (!nested_host_cr0_valid(vcpu, vmcs12->host_cr0) ||
10580 !nested_host_cr4_valid(vcpu, vmcs12->host_cr4) ||
10581 !nested_cr3_valid(vcpu, vmcs12->host_cr3))
10582 return VMXERR_ENTRY_INVALID_HOST_STATE_FIELD;
10583
10584 return 0;
10585 }
10586
10587 static int check_vmentry_postreqs(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12,
10588 u32 *exit_qual)
10589 {
10590 bool ia32e;
10591
10592 *exit_qual = ENTRY_FAIL_DEFAULT;
10593
10594 if (!nested_guest_cr0_valid(vcpu, vmcs12->guest_cr0) ||
10595 !nested_guest_cr4_valid(vcpu, vmcs12->guest_cr4))
10596 return 1;
10597
10598 if (!nested_cpu_has2(vmcs12, SECONDARY_EXEC_SHADOW_VMCS) &&
10599 vmcs12->vmcs_link_pointer != -1ull) {
10600 *exit_qual = ENTRY_FAIL_VMCS_LINK_PTR;
10601 return 1;
10602 }
10603
10604 /*
10605 * If the load IA32_EFER VM-entry control is 1, the following checks
10606 * are performed on the field for the IA32_EFER MSR:
10607 * - Bits reserved in the IA32_EFER MSR must be 0.
10608 * - Bit 10 (corresponding to IA32_EFER.LMA) must equal the value of
10609 * the IA-32e mode guest VM-exit control. It must also be identical
10610 * to bit 8 (LME) if bit 31 in the CR0 field (corresponding to
10611 * CR0.PG) is 1.
10612 */
10613 if (to_vmx(vcpu)->nested.nested_run_pending &&
10614 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_EFER)) {
10615 ia32e = (vmcs12->vm_entry_controls & VM_ENTRY_IA32E_MODE) != 0;
10616 if (!kvm_valid_efer(vcpu, vmcs12->guest_ia32_efer) ||
10617 ia32e != !!(vmcs12->guest_ia32_efer & EFER_LMA) ||
10618 ((vmcs12->guest_cr0 & X86_CR0_PG) &&
10619 ia32e != !!(vmcs12->guest_ia32_efer & EFER_LME)))
10620 return 1;
10621 }
10622
10623 /*
10624 * If the load IA32_EFER VM-exit control is 1, bits reserved in the
10625 * IA32_EFER MSR must be 0 in the field for that register. In addition,
10626 * the values of the LMA and LME bits in the field must each be that of
10627 * the host address-space size VM-exit control.
10628 */
10629 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_EFER) {
10630 ia32e = (vmcs12->vm_exit_controls &
10631 VM_EXIT_HOST_ADDR_SPACE_SIZE) != 0;
10632 if (!kvm_valid_efer(vcpu, vmcs12->host_ia32_efer) ||
10633 ia32e != !!(vmcs12->host_ia32_efer & EFER_LMA) ||
10634 ia32e != !!(vmcs12->host_ia32_efer & EFER_LME))
10635 return 1;
10636 }
10637
10638 return 0;
10639 }
10640
10641 static int enter_vmx_non_root_mode(struct kvm_vcpu *vcpu, bool from_vmentry)
10642 {
10643 struct vcpu_vmx *vmx = to_vmx(vcpu);
10644 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
10645 struct loaded_vmcs *vmcs02;
10646 u32 msr_entry_idx;
10647 u32 exit_qual;
10648
10649 vmcs02 = nested_get_current_vmcs02(vmx);
10650 if (!vmcs02)
10651 return -ENOMEM;
10652
10653 enter_guest_mode(vcpu);
10654
10655 if (!(vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS))
10656 vmx->nested.vmcs01_debugctl = vmcs_read64(GUEST_IA32_DEBUGCTL);
10657
10658 vmx_switch_vmcs(vcpu, vmcs02);
10659 vmx_segment_cache_clear(vmx);
10660
10661 if (prepare_vmcs02(vcpu, vmcs12, from_vmentry, &exit_qual)) {
10662 leave_guest_mode(vcpu);
10663 vmx_switch_vmcs(vcpu, &vmx->vmcs01);
10664 nested_vmx_entry_failure(vcpu, vmcs12,
10665 EXIT_REASON_INVALID_STATE, exit_qual);
10666 return 1;
10667 }
10668
10669 nested_get_vmcs12_pages(vcpu, vmcs12);
10670
10671 msr_entry_idx = nested_vmx_load_msr(vcpu,
10672 vmcs12->vm_entry_msr_load_addr,
10673 vmcs12->vm_entry_msr_load_count);
10674 if (msr_entry_idx) {
10675 leave_guest_mode(vcpu);
10676 vmx_switch_vmcs(vcpu, &vmx->vmcs01);
10677 nested_vmx_entry_failure(vcpu, vmcs12,
10678 EXIT_REASON_MSR_LOAD_FAIL, msr_entry_idx);
10679 return 1;
10680 }
10681
10682 /*
10683 * Note no nested_vmx_succeed or nested_vmx_fail here. At this point
10684 * we are no longer running L1, and VMLAUNCH/VMRESUME has not yet
10685 * returned as far as L1 is concerned. It will only return (and set
10686 * the success flag) when L2 exits (see nested_vmx_vmexit()).
10687 */
10688 return 0;
10689 }
10690
10691 /*
10692 * nested_vmx_run() handles a nested entry, i.e., a VMLAUNCH or VMRESUME on L1
10693 * for running an L2 nested guest.
10694 */
10695 static int nested_vmx_run(struct kvm_vcpu *vcpu, bool launch)
10696 {
10697 struct vmcs12 *vmcs12;
10698 struct vcpu_vmx *vmx = to_vmx(vcpu);
10699 u32 interrupt_shadow = vmx_get_interrupt_shadow(vcpu);
10700 u32 exit_qual;
10701 int ret;
10702
10703 if (!nested_vmx_check_permission(vcpu))
10704 return 1;
10705
10706 if (!nested_vmx_check_vmcs12(vcpu))
10707 goto out;
10708
10709 vmcs12 = get_vmcs12(vcpu);
10710
10711 if (enable_shadow_vmcs)
10712 copy_shadow_to_vmcs12(vmx);
10713
10714 /*
10715 * The nested entry process starts with enforcing various prerequisites
10716 * on vmcs12 as required by the Intel SDM, and act appropriately when
10717 * they fail: As the SDM explains, some conditions should cause the
10718 * instruction to fail, while others will cause the instruction to seem
10719 * to succeed, but return an EXIT_REASON_INVALID_STATE.
10720 * To speed up the normal (success) code path, we should avoid checking
10721 * for misconfigurations which will anyway be caught by the processor
10722 * when using the merged vmcs02.
10723 */
10724 if (interrupt_shadow & KVM_X86_SHADOW_INT_MOV_SS) {
10725 nested_vmx_failValid(vcpu,
10726 VMXERR_ENTRY_EVENTS_BLOCKED_BY_MOV_SS);
10727 goto out;
10728 }
10729
10730 if (vmcs12->launch_state == launch) {
10731 nested_vmx_failValid(vcpu,
10732 launch ? VMXERR_VMLAUNCH_NONCLEAR_VMCS
10733 : VMXERR_VMRESUME_NONLAUNCHED_VMCS);
10734 goto out;
10735 }
10736
10737 ret = check_vmentry_prereqs(vcpu, vmcs12);
10738 if (ret) {
10739 nested_vmx_failValid(vcpu, ret);
10740 goto out;
10741 }
10742
10743 /*
10744 * After this point, the trap flag no longer triggers a singlestep trap
10745 * on the vm entry instructions; don't call kvm_skip_emulated_instruction.
10746 * This is not 100% correct; for performance reasons, we delegate most
10747 * of the checks on host state to the processor. If those fail,
10748 * the singlestep trap is missed.
10749 */
10750 skip_emulated_instruction(vcpu);
10751
10752 ret = check_vmentry_postreqs(vcpu, vmcs12, &exit_qual);
10753 if (ret) {
10754 nested_vmx_entry_failure(vcpu, vmcs12,
10755 EXIT_REASON_INVALID_STATE, exit_qual);
10756 return 1;
10757 }
10758
10759 /*
10760 * We're finally done with prerequisite checking, and can start with
10761 * the nested entry.
10762 */
10763
10764 ret = enter_vmx_non_root_mode(vcpu, true);
10765 if (ret)
10766 return ret;
10767
10768 if (vmcs12->guest_activity_state == GUEST_ACTIVITY_HLT)
10769 return kvm_vcpu_halt(vcpu);
10770
10771 vmx->nested.nested_run_pending = 1;
10772
10773 return 1;
10774
10775 out:
10776 return kvm_skip_emulated_instruction(vcpu);
10777 }
10778
10779 /*
10780 * On a nested exit from L2 to L1, vmcs12.guest_cr0 might not be up-to-date
10781 * because L2 may have changed some cr0 bits directly (CRO_GUEST_HOST_MASK).
10782 * This function returns the new value we should put in vmcs12.guest_cr0.
10783 * It's not enough to just return the vmcs02 GUEST_CR0. Rather,
10784 * 1. Bits that neither L0 nor L1 trapped, were set directly by L2 and are now
10785 * available in vmcs02 GUEST_CR0. (Note: It's enough to check that L0
10786 * didn't trap the bit, because if L1 did, so would L0).
10787 * 2. Bits that L1 asked to trap (and therefore L0 also did) could not have
10788 * been modified by L2, and L1 knows it. So just leave the old value of
10789 * the bit from vmcs12.guest_cr0. Note that the bit from vmcs02 GUEST_CR0
10790 * isn't relevant, because if L0 traps this bit it can set it to anything.
10791 * 3. Bits that L1 didn't trap, but L0 did. L1 believes the guest could have
10792 * changed these bits, and therefore they need to be updated, but L0
10793 * didn't necessarily allow them to be changed in GUEST_CR0 - and rather
10794 * put them in vmcs02 CR0_READ_SHADOW. So take these bits from there.
10795 */
10796 static inline unsigned long
10797 vmcs12_guest_cr0(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
10798 {
10799 return
10800 /*1*/ (vmcs_readl(GUEST_CR0) & vcpu->arch.cr0_guest_owned_bits) |
10801 /*2*/ (vmcs12->guest_cr0 & vmcs12->cr0_guest_host_mask) |
10802 /*3*/ (vmcs_readl(CR0_READ_SHADOW) & ~(vmcs12->cr0_guest_host_mask |
10803 vcpu->arch.cr0_guest_owned_bits));
10804 }
10805
10806 static inline unsigned long
10807 vmcs12_guest_cr4(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
10808 {
10809 return
10810 /*1*/ (vmcs_readl(GUEST_CR4) & vcpu->arch.cr4_guest_owned_bits) |
10811 /*2*/ (vmcs12->guest_cr4 & vmcs12->cr4_guest_host_mask) |
10812 /*3*/ (vmcs_readl(CR4_READ_SHADOW) & ~(vmcs12->cr4_guest_host_mask |
10813 vcpu->arch.cr4_guest_owned_bits));
10814 }
10815
10816 static void vmcs12_save_pending_event(struct kvm_vcpu *vcpu,
10817 struct vmcs12 *vmcs12)
10818 {
10819 u32 idt_vectoring;
10820 unsigned int nr;
10821
10822 if (vcpu->arch.exception.pending && vcpu->arch.exception.reinject) {
10823 nr = vcpu->arch.exception.nr;
10824 idt_vectoring = nr | VECTORING_INFO_VALID_MASK;
10825
10826 if (kvm_exception_is_soft(nr)) {
10827 vmcs12->vm_exit_instruction_len =
10828 vcpu->arch.event_exit_inst_len;
10829 idt_vectoring |= INTR_TYPE_SOFT_EXCEPTION;
10830 } else
10831 idt_vectoring |= INTR_TYPE_HARD_EXCEPTION;
10832
10833 if (vcpu->arch.exception.has_error_code) {
10834 idt_vectoring |= VECTORING_INFO_DELIVER_CODE_MASK;
10835 vmcs12->idt_vectoring_error_code =
10836 vcpu->arch.exception.error_code;
10837 }
10838
10839 vmcs12->idt_vectoring_info_field = idt_vectoring;
10840 } else if (vcpu->arch.nmi_injected) {
10841 vmcs12->idt_vectoring_info_field =
10842 INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK | NMI_VECTOR;
10843 } else if (vcpu->arch.interrupt.pending) {
10844 nr = vcpu->arch.interrupt.nr;
10845 idt_vectoring = nr | VECTORING_INFO_VALID_MASK;
10846
10847 if (vcpu->arch.interrupt.soft) {
10848 idt_vectoring |= INTR_TYPE_SOFT_INTR;
10849 vmcs12->vm_entry_instruction_len =
10850 vcpu->arch.event_exit_inst_len;
10851 } else
10852 idt_vectoring |= INTR_TYPE_EXT_INTR;
10853
10854 vmcs12->idt_vectoring_info_field = idt_vectoring;
10855 }
10856 }
10857
10858 static int vmx_check_nested_events(struct kvm_vcpu *vcpu, bool external_intr)
10859 {
10860 struct vcpu_vmx *vmx = to_vmx(vcpu);
10861
10862 if (vcpu->arch.exception.pending ||
10863 vcpu->arch.nmi_injected ||
10864 vcpu->arch.interrupt.pending)
10865 return -EBUSY;
10866
10867 if (nested_cpu_has_preemption_timer(get_vmcs12(vcpu)) &&
10868 vmx->nested.preemption_timer_expired) {
10869 if (vmx->nested.nested_run_pending)
10870 return -EBUSY;
10871 nested_vmx_vmexit(vcpu, EXIT_REASON_PREEMPTION_TIMER, 0, 0);
10872 return 0;
10873 }
10874
10875 if (vcpu->arch.nmi_pending && nested_exit_on_nmi(vcpu)) {
10876 if (vmx->nested.nested_run_pending)
10877 return -EBUSY;
10878 nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI,
10879 NMI_VECTOR | INTR_TYPE_NMI_INTR |
10880 INTR_INFO_VALID_MASK, 0);
10881 /*
10882 * The NMI-triggered VM exit counts as injection:
10883 * clear this one and block further NMIs.
10884 */
10885 vcpu->arch.nmi_pending = 0;
10886 vmx_set_nmi_mask(vcpu, true);
10887 return 0;
10888 }
10889
10890 if ((kvm_cpu_has_interrupt(vcpu) || external_intr) &&
10891 nested_exit_on_intr(vcpu)) {
10892 if (vmx->nested.nested_run_pending)
10893 return -EBUSY;
10894 nested_vmx_vmexit(vcpu, EXIT_REASON_EXTERNAL_INTERRUPT, 0, 0);
10895 return 0;
10896 }
10897
10898 vmx_complete_nested_posted_interrupt(vcpu);
10899 return 0;
10900 }
10901
10902 static u32 vmx_get_preemption_timer_value(struct kvm_vcpu *vcpu)
10903 {
10904 ktime_t remaining =
10905 hrtimer_get_remaining(&to_vmx(vcpu)->nested.preemption_timer);
10906 u64 value;
10907
10908 if (ktime_to_ns(remaining) <= 0)
10909 return 0;
10910
10911 value = ktime_to_ns(remaining) * vcpu->arch.virtual_tsc_khz;
10912 do_div(value, 1000000);
10913 return value >> VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE;
10914 }
10915
10916 /*
10917 * Update the guest state fields of vmcs12 to reflect changes that
10918 * occurred while L2 was running. (The "IA-32e mode guest" bit of the
10919 * VM-entry controls is also updated, since this is really a guest
10920 * state bit.)
10921 */
10922 static void sync_vmcs12(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
10923 {
10924 vmcs12->guest_cr0 = vmcs12_guest_cr0(vcpu, vmcs12);
10925 vmcs12->guest_cr4 = vmcs12_guest_cr4(vcpu, vmcs12);
10926
10927 vmcs12->guest_rsp = kvm_register_read(vcpu, VCPU_REGS_RSP);
10928 vmcs12->guest_rip = kvm_register_read(vcpu, VCPU_REGS_RIP);
10929 vmcs12->guest_rflags = vmcs_readl(GUEST_RFLAGS);
10930
10931 vmcs12->guest_es_selector = vmcs_read16(GUEST_ES_SELECTOR);
10932 vmcs12->guest_cs_selector = vmcs_read16(GUEST_CS_SELECTOR);
10933 vmcs12->guest_ss_selector = vmcs_read16(GUEST_SS_SELECTOR);
10934 vmcs12->guest_ds_selector = vmcs_read16(GUEST_DS_SELECTOR);
10935 vmcs12->guest_fs_selector = vmcs_read16(GUEST_FS_SELECTOR);
10936 vmcs12->guest_gs_selector = vmcs_read16(GUEST_GS_SELECTOR);
10937 vmcs12->guest_ldtr_selector = vmcs_read16(GUEST_LDTR_SELECTOR);
10938 vmcs12->guest_tr_selector = vmcs_read16(GUEST_TR_SELECTOR);
10939 vmcs12->guest_es_limit = vmcs_read32(GUEST_ES_LIMIT);
10940 vmcs12->guest_cs_limit = vmcs_read32(GUEST_CS_LIMIT);
10941 vmcs12->guest_ss_limit = vmcs_read32(GUEST_SS_LIMIT);
10942 vmcs12->guest_ds_limit = vmcs_read32(GUEST_DS_LIMIT);
10943 vmcs12->guest_fs_limit = vmcs_read32(GUEST_FS_LIMIT);
10944 vmcs12->guest_gs_limit = vmcs_read32(GUEST_GS_LIMIT);
10945 vmcs12->guest_ldtr_limit = vmcs_read32(GUEST_LDTR_LIMIT);
10946 vmcs12->guest_tr_limit = vmcs_read32(GUEST_TR_LIMIT);
10947 vmcs12->guest_gdtr_limit = vmcs_read32(GUEST_GDTR_LIMIT);
10948 vmcs12->guest_idtr_limit = vmcs_read32(GUEST_IDTR_LIMIT);
10949 vmcs12->guest_es_ar_bytes = vmcs_read32(GUEST_ES_AR_BYTES);
10950 vmcs12->guest_cs_ar_bytes = vmcs_read32(GUEST_CS_AR_BYTES);
10951 vmcs12->guest_ss_ar_bytes = vmcs_read32(GUEST_SS_AR_BYTES);
10952 vmcs12->guest_ds_ar_bytes = vmcs_read32(GUEST_DS_AR_BYTES);
10953 vmcs12->guest_fs_ar_bytes = vmcs_read32(GUEST_FS_AR_BYTES);
10954 vmcs12->guest_gs_ar_bytes = vmcs_read32(GUEST_GS_AR_BYTES);
10955 vmcs12->guest_ldtr_ar_bytes = vmcs_read32(GUEST_LDTR_AR_BYTES);
10956 vmcs12->guest_tr_ar_bytes = vmcs_read32(GUEST_TR_AR_BYTES);
10957 vmcs12->guest_es_base = vmcs_readl(GUEST_ES_BASE);
10958 vmcs12->guest_cs_base = vmcs_readl(GUEST_CS_BASE);
10959 vmcs12->guest_ss_base = vmcs_readl(GUEST_SS_BASE);
10960 vmcs12->guest_ds_base = vmcs_readl(GUEST_DS_BASE);
10961 vmcs12->guest_fs_base = vmcs_readl(GUEST_FS_BASE);
10962 vmcs12->guest_gs_base = vmcs_readl(GUEST_GS_BASE);
10963 vmcs12->guest_ldtr_base = vmcs_readl(GUEST_LDTR_BASE);
10964 vmcs12->guest_tr_base = vmcs_readl(GUEST_TR_BASE);
10965 vmcs12->guest_gdtr_base = vmcs_readl(GUEST_GDTR_BASE);
10966 vmcs12->guest_idtr_base = vmcs_readl(GUEST_IDTR_BASE);
10967
10968 vmcs12->guest_interruptibility_info =
10969 vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
10970 vmcs12->guest_pending_dbg_exceptions =
10971 vmcs_readl(GUEST_PENDING_DBG_EXCEPTIONS);
10972 if (vcpu->arch.mp_state == KVM_MP_STATE_HALTED)
10973 vmcs12->guest_activity_state = GUEST_ACTIVITY_HLT;
10974 else
10975 vmcs12->guest_activity_state = GUEST_ACTIVITY_ACTIVE;
10976
10977 if (nested_cpu_has_preemption_timer(vmcs12)) {
10978 if (vmcs12->vm_exit_controls &
10979 VM_EXIT_SAVE_VMX_PREEMPTION_TIMER)
10980 vmcs12->vmx_preemption_timer_value =
10981 vmx_get_preemption_timer_value(vcpu);
10982 hrtimer_cancel(&to_vmx(vcpu)->nested.preemption_timer);
10983 }
10984
10985 /*
10986 * In some cases (usually, nested EPT), L2 is allowed to change its
10987 * own CR3 without exiting. If it has changed it, we must keep it.
10988 * Of course, if L0 is using shadow page tables, GUEST_CR3 was defined
10989 * by L0, not L1 or L2, so we mustn't unconditionally copy it to vmcs12.
10990 *
10991 * Additionally, restore L2's PDPTR to vmcs12.
10992 */
10993 if (enable_ept) {
10994 vmcs12->guest_cr3 = vmcs_readl(GUEST_CR3);
10995 vmcs12->guest_pdptr0 = vmcs_read64(GUEST_PDPTR0);
10996 vmcs12->guest_pdptr1 = vmcs_read64(GUEST_PDPTR1);
10997 vmcs12->guest_pdptr2 = vmcs_read64(GUEST_PDPTR2);
10998 vmcs12->guest_pdptr3 = vmcs_read64(GUEST_PDPTR3);
10999 }
11000
11001 vmcs12->guest_linear_address = vmcs_readl(GUEST_LINEAR_ADDRESS);
11002
11003 if (nested_cpu_has_vid(vmcs12))
11004 vmcs12->guest_intr_status = vmcs_read16(GUEST_INTR_STATUS);
11005
11006 vmcs12->vm_entry_controls =
11007 (vmcs12->vm_entry_controls & ~VM_ENTRY_IA32E_MODE) |
11008 (vm_entry_controls_get(to_vmx(vcpu)) & VM_ENTRY_IA32E_MODE);
11009
11010 if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_DEBUG_CONTROLS) {
11011 kvm_get_dr(vcpu, 7, (unsigned long *)&vmcs12->guest_dr7);
11012 vmcs12->guest_ia32_debugctl = vmcs_read64(GUEST_IA32_DEBUGCTL);
11013 }
11014
11015 /* TODO: These cannot have changed unless we have MSR bitmaps and
11016 * the relevant bit asks not to trap the change */
11017 if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_IA32_PAT)
11018 vmcs12->guest_ia32_pat = vmcs_read64(GUEST_IA32_PAT);
11019 if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_IA32_EFER)
11020 vmcs12->guest_ia32_efer = vcpu->arch.efer;
11021 vmcs12->guest_sysenter_cs = vmcs_read32(GUEST_SYSENTER_CS);
11022 vmcs12->guest_sysenter_esp = vmcs_readl(GUEST_SYSENTER_ESP);
11023 vmcs12->guest_sysenter_eip = vmcs_readl(GUEST_SYSENTER_EIP);
11024 if (kvm_mpx_supported())
11025 vmcs12->guest_bndcfgs = vmcs_read64(GUEST_BNDCFGS);
11026 }
11027
11028 /*
11029 * prepare_vmcs12 is part of what we need to do when the nested L2 guest exits
11030 * and we want to prepare to run its L1 parent. L1 keeps a vmcs for L2 (vmcs12),
11031 * and this function updates it to reflect the changes to the guest state while
11032 * L2 was running (and perhaps made some exits which were handled directly by L0
11033 * without going back to L1), and to reflect the exit reason.
11034 * Note that we do not have to copy here all VMCS fields, just those that
11035 * could have changed by the L2 guest or the exit - i.e., the guest-state and
11036 * exit-information fields only. Other fields are modified by L1 with VMWRITE,
11037 * which already writes to vmcs12 directly.
11038 */
11039 static void prepare_vmcs12(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12,
11040 u32 exit_reason, u32 exit_intr_info,
11041 unsigned long exit_qualification)
11042 {
11043 /* update guest state fields: */
11044 sync_vmcs12(vcpu, vmcs12);
11045
11046 /* update exit information fields: */
11047
11048 vmcs12->vm_exit_reason = exit_reason;
11049 vmcs12->exit_qualification = exit_qualification;
11050 vmcs12->vm_exit_intr_info = exit_intr_info;
11051
11052 vmcs12->idt_vectoring_info_field = 0;
11053 vmcs12->vm_exit_instruction_len = vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
11054 vmcs12->vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
11055
11056 if (!(vmcs12->vm_exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY)) {
11057 vmcs12->launch_state = 1;
11058
11059 /* vm_entry_intr_info_field is cleared on exit. Emulate this
11060 * instead of reading the real value. */
11061 vmcs12->vm_entry_intr_info_field &= ~INTR_INFO_VALID_MASK;
11062
11063 /*
11064 * Transfer the event that L0 or L1 may wanted to inject into
11065 * L2 to IDT_VECTORING_INFO_FIELD.
11066 */
11067 vmcs12_save_pending_event(vcpu, vmcs12);
11068 }
11069
11070 /*
11071 * Drop what we picked up for L2 via vmx_complete_interrupts. It is
11072 * preserved above and would only end up incorrectly in L1.
11073 */
11074 vcpu->arch.nmi_injected = false;
11075 kvm_clear_exception_queue(vcpu);
11076 kvm_clear_interrupt_queue(vcpu);
11077 }
11078
11079 /*
11080 * A part of what we need to when the nested L2 guest exits and we want to
11081 * run its L1 parent, is to reset L1's guest state to the host state specified
11082 * in vmcs12.
11083 * This function is to be called not only on normal nested exit, but also on
11084 * a nested entry failure, as explained in Intel's spec, 3B.23.7 ("VM-Entry
11085 * Failures During or After Loading Guest State").
11086 * This function should be called when the active VMCS is L1's (vmcs01).
11087 */
11088 static void load_vmcs12_host_state(struct kvm_vcpu *vcpu,
11089 struct vmcs12 *vmcs12)
11090 {
11091 struct kvm_segment seg;
11092 u32 entry_failure_code;
11093
11094 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_EFER)
11095 vcpu->arch.efer = vmcs12->host_ia32_efer;
11096 else if (vmcs12->vm_exit_controls & VM_EXIT_HOST_ADDR_SPACE_SIZE)
11097 vcpu->arch.efer |= (EFER_LMA | EFER_LME);
11098 else
11099 vcpu->arch.efer &= ~(EFER_LMA | EFER_LME);
11100 vmx_set_efer(vcpu, vcpu->arch.efer);
11101
11102 kvm_register_write(vcpu, VCPU_REGS_RSP, vmcs12->host_rsp);
11103 kvm_register_write(vcpu, VCPU_REGS_RIP, vmcs12->host_rip);
11104 vmx_set_rflags(vcpu, X86_EFLAGS_FIXED);
11105 /*
11106 * Note that calling vmx_set_cr0 is important, even if cr0 hasn't
11107 * actually changed, because vmx_set_cr0 refers to efer set above.
11108 *
11109 * CR0_GUEST_HOST_MASK is already set in the original vmcs01
11110 * (KVM doesn't change it);
11111 */
11112 vcpu->arch.cr0_guest_owned_bits = X86_CR0_TS;
11113 vmx_set_cr0(vcpu, vmcs12->host_cr0);
11114
11115 /* Same as above - no reason to call set_cr4_guest_host_mask(). */
11116 vcpu->arch.cr4_guest_owned_bits = ~vmcs_readl(CR4_GUEST_HOST_MASK);
11117 vmx_set_cr4(vcpu, vmcs12->host_cr4);
11118
11119 nested_ept_uninit_mmu_context(vcpu);
11120
11121 /*
11122 * Only PDPTE load can fail as the value of cr3 was checked on entry and
11123 * couldn't have changed.
11124 */
11125 if (nested_vmx_load_cr3(vcpu, vmcs12->host_cr3, false, &entry_failure_code))
11126 nested_vmx_abort(vcpu, VMX_ABORT_LOAD_HOST_PDPTE_FAIL);
11127
11128 if (!enable_ept)
11129 vcpu->arch.walk_mmu->inject_page_fault = kvm_inject_page_fault;
11130
11131 if (enable_vpid) {
11132 /*
11133 * Trivially support vpid by letting L2s share their parent
11134 * L1's vpid. TODO: move to a more elaborate solution, giving
11135 * each L2 its own vpid and exposing the vpid feature to L1.
11136 */
11137 vmx_flush_tlb(vcpu);
11138 }
11139 /* Restore posted intr vector. */
11140 if (nested_cpu_has_posted_intr(vmcs12))
11141 vmcs_write16(POSTED_INTR_NV, POSTED_INTR_VECTOR);
11142
11143 vmcs_write32(GUEST_SYSENTER_CS, vmcs12->host_ia32_sysenter_cs);
11144 vmcs_writel(GUEST_SYSENTER_ESP, vmcs12->host_ia32_sysenter_esp);
11145 vmcs_writel(GUEST_SYSENTER_EIP, vmcs12->host_ia32_sysenter_eip);
11146 vmcs_writel(GUEST_IDTR_BASE, vmcs12->host_idtr_base);
11147 vmcs_writel(GUEST_GDTR_BASE, vmcs12->host_gdtr_base);
11148
11149 /* If not VM_EXIT_CLEAR_BNDCFGS, the L2 value propagates to L1. */
11150 if (vmcs12->vm_exit_controls & VM_EXIT_CLEAR_BNDCFGS)
11151 vmcs_write64(GUEST_BNDCFGS, 0);
11152
11153 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PAT) {
11154 vmcs_write64(GUEST_IA32_PAT, vmcs12->host_ia32_pat);
11155 vcpu->arch.pat = vmcs12->host_ia32_pat;
11156 }
11157 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL)
11158 vmcs_write64(GUEST_IA32_PERF_GLOBAL_CTRL,
11159 vmcs12->host_ia32_perf_global_ctrl);
11160
11161 /* Set L1 segment info according to Intel SDM
11162 27.5.2 Loading Host Segment and Descriptor-Table Registers */
11163 seg = (struct kvm_segment) {
11164 .base = 0,
11165 .limit = 0xFFFFFFFF,
11166 .selector = vmcs12->host_cs_selector,
11167 .type = 11,
11168 .present = 1,
11169 .s = 1,
11170 .g = 1
11171 };
11172 if (vmcs12->vm_exit_controls & VM_EXIT_HOST_ADDR_SPACE_SIZE)
11173 seg.l = 1;
11174 else
11175 seg.db = 1;
11176 vmx_set_segment(vcpu, &seg, VCPU_SREG_CS);
11177 seg = (struct kvm_segment) {
11178 .base = 0,
11179 .limit = 0xFFFFFFFF,
11180 .type = 3,
11181 .present = 1,
11182 .s = 1,
11183 .db = 1,
11184 .g = 1
11185 };
11186 seg.selector = vmcs12->host_ds_selector;
11187 vmx_set_segment(vcpu, &seg, VCPU_SREG_DS);
11188 seg.selector = vmcs12->host_es_selector;
11189 vmx_set_segment(vcpu, &seg, VCPU_SREG_ES);
11190 seg.selector = vmcs12->host_ss_selector;
11191 vmx_set_segment(vcpu, &seg, VCPU_SREG_SS);
11192 seg.selector = vmcs12->host_fs_selector;
11193 seg.base = vmcs12->host_fs_base;
11194 vmx_set_segment(vcpu, &seg, VCPU_SREG_FS);
11195 seg.selector = vmcs12->host_gs_selector;
11196 seg.base = vmcs12->host_gs_base;
11197 vmx_set_segment(vcpu, &seg, VCPU_SREG_GS);
11198 seg = (struct kvm_segment) {
11199 .base = vmcs12->host_tr_base,
11200 .limit = 0x67,
11201 .selector = vmcs12->host_tr_selector,
11202 .type = 11,
11203 .present = 1
11204 };
11205 vmx_set_segment(vcpu, &seg, VCPU_SREG_TR);
11206
11207 kvm_set_dr(vcpu, 7, 0x400);
11208 vmcs_write64(GUEST_IA32_DEBUGCTL, 0);
11209
11210 if (cpu_has_vmx_msr_bitmap())
11211 vmx_set_msr_bitmap(vcpu);
11212
11213 if (nested_vmx_load_msr(vcpu, vmcs12->vm_exit_msr_load_addr,
11214 vmcs12->vm_exit_msr_load_count))
11215 nested_vmx_abort(vcpu, VMX_ABORT_LOAD_HOST_MSR_FAIL);
11216 }
11217
11218 /*
11219 * Emulate an exit from nested guest (L2) to L1, i.e., prepare to run L1
11220 * and modify vmcs12 to make it see what it would expect to see there if
11221 * L2 was its real guest. Must only be called when in L2 (is_guest_mode())
11222 */
11223 static void nested_vmx_vmexit(struct kvm_vcpu *vcpu, u32 exit_reason,
11224 u32 exit_intr_info,
11225 unsigned long exit_qualification)
11226 {
11227 struct vcpu_vmx *vmx = to_vmx(vcpu);
11228 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
11229 u32 vm_inst_error = 0;
11230
11231 /* trying to cancel vmlaunch/vmresume is a bug */
11232 WARN_ON_ONCE(vmx->nested.nested_run_pending);
11233
11234 leave_guest_mode(vcpu);
11235 prepare_vmcs12(vcpu, vmcs12, exit_reason, exit_intr_info,
11236 exit_qualification);
11237
11238 if (nested_vmx_store_msr(vcpu, vmcs12->vm_exit_msr_store_addr,
11239 vmcs12->vm_exit_msr_store_count))
11240 nested_vmx_abort(vcpu, VMX_ABORT_SAVE_GUEST_MSR_FAIL);
11241
11242 if (unlikely(vmx->fail))
11243 vm_inst_error = vmcs_read32(VM_INSTRUCTION_ERROR);
11244
11245 vmx_switch_vmcs(vcpu, &vmx->vmcs01);
11246
11247 /*
11248 * TODO: SDM says that with acknowledge interrupt on exit, bit 31 of
11249 * the VM-exit interrupt information (valid interrupt) is always set to
11250 * 1 on EXIT_REASON_EXTERNAL_INTERRUPT, so we shouldn't need
11251 * kvm_cpu_has_interrupt(). See the commit message for details.
11252 */
11253 if (nested_exit_intr_ack_set(vcpu) &&
11254 exit_reason == EXIT_REASON_EXTERNAL_INTERRUPT &&
11255 kvm_cpu_has_interrupt(vcpu)) {
11256 int irq = kvm_cpu_get_interrupt(vcpu);
11257 WARN_ON(irq < 0);
11258 vmcs12->vm_exit_intr_info = irq |
11259 INTR_INFO_VALID_MASK | INTR_TYPE_EXT_INTR;
11260 }
11261
11262 trace_kvm_nested_vmexit_inject(vmcs12->vm_exit_reason,
11263 vmcs12->exit_qualification,
11264 vmcs12->idt_vectoring_info_field,
11265 vmcs12->vm_exit_intr_info,
11266 vmcs12->vm_exit_intr_error_code,
11267 KVM_ISA_VMX);
11268
11269 vm_entry_controls_reset_shadow(vmx);
11270 vm_exit_controls_reset_shadow(vmx);
11271 vmx_segment_cache_clear(vmx);
11272
11273 /* if no vmcs02 cache requested, remove the one we used */
11274 if (VMCS02_POOL_SIZE == 0)
11275 nested_free_vmcs02(vmx, vmx->nested.current_vmptr);
11276
11277 load_vmcs12_host_state(vcpu, vmcs12);
11278
11279 /* Update any VMCS fields that might have changed while L2 ran */
11280 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, vmx->msr_autoload.nr);
11281 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, vmx->msr_autoload.nr);
11282 vmcs_write64(TSC_OFFSET, vcpu->arch.tsc_offset);
11283 if (vmx->hv_deadline_tsc == -1)
11284 vmcs_clear_bits(PIN_BASED_VM_EXEC_CONTROL,
11285 PIN_BASED_VMX_PREEMPTION_TIMER);
11286 else
11287 vmcs_set_bits(PIN_BASED_VM_EXEC_CONTROL,
11288 PIN_BASED_VMX_PREEMPTION_TIMER);
11289 if (kvm_has_tsc_control)
11290 decache_tsc_multiplier(vmx);
11291
11292 if (vmx->nested.change_vmcs01_virtual_x2apic_mode) {
11293 vmx->nested.change_vmcs01_virtual_x2apic_mode = false;
11294 vmx_set_virtual_x2apic_mode(vcpu,
11295 vcpu->arch.apic_base & X2APIC_ENABLE);
11296 } else if (!nested_cpu_has_ept(vmcs12) &&
11297 nested_cpu_has2(vmcs12,
11298 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) {
11299 vmx_flush_tlb_ept_only(vcpu);
11300 }
11301
11302 /* This is needed for same reason as it was needed in prepare_vmcs02 */
11303 vmx->host_rsp = 0;
11304
11305 /* Unpin physical memory we referred to in vmcs02 */
11306 if (vmx->nested.apic_access_page) {
11307 nested_release_page(vmx->nested.apic_access_page);
11308 vmx->nested.apic_access_page = NULL;
11309 }
11310 if (vmx->nested.virtual_apic_page) {
11311 nested_release_page(vmx->nested.virtual_apic_page);
11312 vmx->nested.virtual_apic_page = NULL;
11313 }
11314 if (vmx->nested.pi_desc_page) {
11315 kunmap(vmx->nested.pi_desc_page);
11316 nested_release_page(vmx->nested.pi_desc_page);
11317 vmx->nested.pi_desc_page = NULL;
11318 vmx->nested.pi_desc = NULL;
11319 }
11320
11321 /*
11322 * We are now running in L2, mmu_notifier will force to reload the
11323 * page's hpa for L2 vmcs. Need to reload it for L1 before entering L1.
11324 */
11325 kvm_make_request(KVM_REQ_APIC_PAGE_RELOAD, vcpu);
11326
11327 /*
11328 * Exiting from L2 to L1, we're now back to L1 which thinks it just
11329 * finished a VMLAUNCH or VMRESUME instruction, so we need to set the
11330 * success or failure flag accordingly.
11331 */
11332 if (unlikely(vmx->fail)) {
11333 vmx->fail = 0;
11334 nested_vmx_failValid(vcpu, vm_inst_error);
11335 } else
11336 nested_vmx_succeed(vcpu);
11337 if (enable_shadow_vmcs)
11338 vmx->nested.sync_shadow_vmcs = true;
11339
11340 /* in case we halted in L2 */
11341 vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
11342 }
11343
11344 /*
11345 * Forcibly leave nested mode in order to be able to reset the VCPU later on.
11346 */
11347 static void vmx_leave_nested(struct kvm_vcpu *vcpu)
11348 {
11349 if (is_guest_mode(vcpu)) {
11350 to_vmx(vcpu)->nested.nested_run_pending = 0;
11351 nested_vmx_vmexit(vcpu, -1, 0, 0);
11352 }
11353 free_nested(to_vmx(vcpu));
11354 }
11355
11356 /*
11357 * L1's failure to enter L2 is a subset of a normal exit, as explained in
11358 * 23.7 "VM-entry failures during or after loading guest state" (this also
11359 * lists the acceptable exit-reason and exit-qualification parameters).
11360 * It should only be called before L2 actually succeeded to run, and when
11361 * vmcs01 is current (it doesn't leave_guest_mode() or switch vmcss).
11362 */
11363 static void nested_vmx_entry_failure(struct kvm_vcpu *vcpu,
11364 struct vmcs12 *vmcs12,
11365 u32 reason, unsigned long qualification)
11366 {
11367 load_vmcs12_host_state(vcpu, vmcs12);
11368 vmcs12->vm_exit_reason = reason | VMX_EXIT_REASONS_FAILED_VMENTRY;
11369 vmcs12->exit_qualification = qualification;
11370 nested_vmx_succeed(vcpu);
11371 if (enable_shadow_vmcs)
11372 to_vmx(vcpu)->nested.sync_shadow_vmcs = true;
11373 }
11374
11375 static int vmx_check_intercept(struct kvm_vcpu *vcpu,
11376 struct x86_instruction_info *info,
11377 enum x86_intercept_stage stage)
11378 {
11379 return X86EMUL_CONTINUE;
11380 }
11381
11382 #ifdef CONFIG_X86_64
11383 /* (a << shift) / divisor, return 1 if overflow otherwise 0 */
11384 static inline int u64_shl_div_u64(u64 a, unsigned int shift,
11385 u64 divisor, u64 *result)
11386 {
11387 u64 low = a << shift, high = a >> (64 - shift);
11388
11389 /* To avoid the overflow on divq */
11390 if (high >= divisor)
11391 return 1;
11392
11393 /* Low hold the result, high hold rem which is discarded */
11394 asm("divq %2\n\t" : "=a" (low), "=d" (high) :
11395 "rm" (divisor), "0" (low), "1" (high));
11396 *result = low;
11397
11398 return 0;
11399 }
11400
11401 static int vmx_set_hv_timer(struct kvm_vcpu *vcpu, u64 guest_deadline_tsc)
11402 {
11403 struct vcpu_vmx *vmx = to_vmx(vcpu);
11404 u64 tscl = rdtsc();
11405 u64 guest_tscl = kvm_read_l1_tsc(vcpu, tscl);
11406 u64 delta_tsc = max(guest_deadline_tsc, guest_tscl) - guest_tscl;
11407
11408 /* Convert to host delta tsc if tsc scaling is enabled */
11409 if (vcpu->arch.tsc_scaling_ratio != kvm_default_tsc_scaling_ratio &&
11410 u64_shl_div_u64(delta_tsc,
11411 kvm_tsc_scaling_ratio_frac_bits,
11412 vcpu->arch.tsc_scaling_ratio,
11413 &delta_tsc))
11414 return -ERANGE;
11415
11416 /*
11417 * If the delta tsc can't fit in the 32 bit after the multi shift,
11418 * we can't use the preemption timer.
11419 * It's possible that it fits on later vmentries, but checking
11420 * on every vmentry is costly so we just use an hrtimer.
11421 */
11422 if (delta_tsc >> (cpu_preemption_timer_multi + 32))
11423 return -ERANGE;
11424
11425 vmx->hv_deadline_tsc = tscl + delta_tsc;
11426 vmcs_set_bits(PIN_BASED_VM_EXEC_CONTROL,
11427 PIN_BASED_VMX_PREEMPTION_TIMER);
11428
11429 return delta_tsc == 0;
11430 }
11431
11432 static void vmx_cancel_hv_timer(struct kvm_vcpu *vcpu)
11433 {
11434 struct vcpu_vmx *vmx = to_vmx(vcpu);
11435 vmx->hv_deadline_tsc = -1;
11436 vmcs_clear_bits(PIN_BASED_VM_EXEC_CONTROL,
11437 PIN_BASED_VMX_PREEMPTION_TIMER);
11438 }
11439 #endif
11440
11441 static void vmx_sched_in(struct kvm_vcpu *vcpu, int cpu)
11442 {
11443 if (ple_gap)
11444 shrink_ple_window(vcpu);
11445 }
11446
11447 static void vmx_slot_enable_log_dirty(struct kvm *kvm,
11448 struct kvm_memory_slot *slot)
11449 {
11450 kvm_mmu_slot_leaf_clear_dirty(kvm, slot);
11451 kvm_mmu_slot_largepage_remove_write_access(kvm, slot);
11452 }
11453
11454 static void vmx_slot_disable_log_dirty(struct kvm *kvm,
11455 struct kvm_memory_slot *slot)
11456 {
11457 kvm_mmu_slot_set_dirty(kvm, slot);
11458 }
11459
11460 static void vmx_flush_log_dirty(struct kvm *kvm)
11461 {
11462 kvm_flush_pml_buffers(kvm);
11463 }
11464
11465 static int vmx_write_pml_buffer(struct kvm_vcpu *vcpu)
11466 {
11467 struct vmcs12 *vmcs12;
11468 struct vcpu_vmx *vmx = to_vmx(vcpu);
11469 gpa_t gpa;
11470 struct page *page = NULL;
11471 u64 *pml_address;
11472
11473 if (is_guest_mode(vcpu)) {
11474 WARN_ON_ONCE(vmx->nested.pml_full);
11475
11476 /*
11477 * Check if PML is enabled for the nested guest.
11478 * Whether eptp bit 6 is set is already checked
11479 * as part of A/D emulation.
11480 */
11481 vmcs12 = get_vmcs12(vcpu);
11482 if (!nested_cpu_has_pml(vmcs12))
11483 return 0;
11484
11485 if (vmcs12->guest_pml_index >= PML_ENTITY_NUM) {
11486 vmx->nested.pml_full = true;
11487 return 1;
11488 }
11489
11490 gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS) & ~0xFFFull;
11491
11492 page = nested_get_page(vcpu, vmcs12->pml_address);
11493 if (!page)
11494 return 0;
11495
11496 pml_address = kmap(page);
11497 pml_address[vmcs12->guest_pml_index--] = gpa;
11498 kunmap(page);
11499 nested_release_page_clean(page);
11500 }
11501
11502 return 0;
11503 }
11504
11505 static void vmx_enable_log_dirty_pt_masked(struct kvm *kvm,
11506 struct kvm_memory_slot *memslot,
11507 gfn_t offset, unsigned long mask)
11508 {
11509 kvm_mmu_clear_dirty_pt_masked(kvm, memslot, offset, mask);
11510 }
11511
11512 static void __pi_post_block(struct kvm_vcpu *vcpu)
11513 {
11514 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
11515 struct pi_desc old, new;
11516 unsigned int dest;
11517
11518 do {
11519 old.control = new.control = pi_desc->control;
11520 WARN(old.nv != POSTED_INTR_WAKEUP_VECTOR,
11521 "Wakeup handler not enabled while the VCPU is blocked\n");
11522
11523 dest = cpu_physical_id(vcpu->cpu);
11524
11525 if (x2apic_enabled())
11526 new.ndst = dest;
11527 else
11528 new.ndst = (dest << 8) & 0xFF00;
11529
11530 /* set 'NV' to 'notification vector' */
11531 new.nv = POSTED_INTR_VECTOR;
11532 } while (cmpxchg64(&pi_desc->control, old.control,
11533 new.control) != old.control);
11534
11535 if (!WARN_ON_ONCE(vcpu->pre_pcpu == -1)) {
11536 spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
11537 list_del(&vcpu->blocked_vcpu_list);
11538 spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
11539 vcpu->pre_pcpu = -1;
11540 }
11541 }
11542
11543 /*
11544 * This routine does the following things for vCPU which is going
11545 * to be blocked if VT-d PI is enabled.
11546 * - Store the vCPU to the wakeup list, so when interrupts happen
11547 * we can find the right vCPU to wake up.
11548 * - Change the Posted-interrupt descriptor as below:
11549 * 'NDST' <-- vcpu->pre_pcpu
11550 * 'NV' <-- POSTED_INTR_WAKEUP_VECTOR
11551 * - If 'ON' is set during this process, which means at least one
11552 * interrupt is posted for this vCPU, we cannot block it, in
11553 * this case, return 1, otherwise, return 0.
11554 *
11555 */
11556 static int pi_pre_block(struct kvm_vcpu *vcpu)
11557 {
11558 unsigned int dest;
11559 struct pi_desc old, new;
11560 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
11561
11562 if (!kvm_arch_has_assigned_device(vcpu->kvm) ||
11563 !irq_remapping_cap(IRQ_POSTING_CAP) ||
11564 !kvm_vcpu_apicv_active(vcpu))
11565 return 0;
11566
11567 WARN_ON(irqs_disabled());
11568 local_irq_disable();
11569 if (!WARN_ON_ONCE(vcpu->pre_pcpu != -1)) {
11570 vcpu->pre_pcpu = vcpu->cpu;
11571 spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
11572 list_add_tail(&vcpu->blocked_vcpu_list,
11573 &per_cpu(blocked_vcpu_on_cpu,
11574 vcpu->pre_pcpu));
11575 spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
11576 }
11577
11578 do {
11579 old.control = new.control = pi_desc->control;
11580
11581 WARN((pi_desc->sn == 1),
11582 "Warning: SN field of posted-interrupts "
11583 "is set before blocking\n");
11584
11585 /*
11586 * Since vCPU can be preempted during this process,
11587 * vcpu->cpu could be different with pre_pcpu, we
11588 * need to set pre_pcpu as the destination of wakeup
11589 * notification event, then we can find the right vCPU
11590 * to wakeup in wakeup handler if interrupts happen
11591 * when the vCPU is in blocked state.
11592 */
11593 dest = cpu_physical_id(vcpu->pre_pcpu);
11594
11595 if (x2apic_enabled())
11596 new.ndst = dest;
11597 else
11598 new.ndst = (dest << 8) & 0xFF00;
11599
11600 /* set 'NV' to 'wakeup vector' */
11601 new.nv = POSTED_INTR_WAKEUP_VECTOR;
11602 } while (cmpxchg64(&pi_desc->control, old.control,
11603 new.control) != old.control);
11604
11605 /* We should not block the vCPU if an interrupt is posted for it. */
11606 if (pi_test_on(pi_desc) == 1)
11607 __pi_post_block(vcpu);
11608
11609 local_irq_enable();
11610 return (vcpu->pre_pcpu == -1);
11611 }
11612
11613 static int vmx_pre_block(struct kvm_vcpu *vcpu)
11614 {
11615 if (pi_pre_block(vcpu))
11616 return 1;
11617
11618 if (kvm_lapic_hv_timer_in_use(vcpu))
11619 kvm_lapic_switch_to_sw_timer(vcpu);
11620
11621 return 0;
11622 }
11623
11624 static void pi_post_block(struct kvm_vcpu *vcpu)
11625 {
11626 if (vcpu->pre_pcpu == -1)
11627 return;
11628
11629 WARN_ON(irqs_disabled());
11630 local_irq_disable();
11631 __pi_post_block(vcpu);
11632 local_irq_enable();
11633 }
11634
11635 static void vmx_post_block(struct kvm_vcpu *vcpu)
11636 {
11637 if (kvm_x86_ops->set_hv_timer)
11638 kvm_lapic_switch_to_hv_timer(vcpu);
11639
11640 pi_post_block(vcpu);
11641 }
11642
11643 /*
11644 * vmx_update_pi_irte - set IRTE for Posted-Interrupts
11645 *
11646 * @kvm: kvm
11647 * @host_irq: host irq of the interrupt
11648 * @guest_irq: gsi of the interrupt
11649 * @set: set or unset PI
11650 * returns 0 on success, < 0 on failure
11651 */
11652 static int vmx_update_pi_irte(struct kvm *kvm, unsigned int host_irq,
11653 uint32_t guest_irq, bool set)
11654 {
11655 struct kvm_kernel_irq_routing_entry *e;
11656 struct kvm_irq_routing_table *irq_rt;
11657 struct kvm_lapic_irq irq;
11658 struct kvm_vcpu *vcpu;
11659 struct vcpu_data vcpu_info;
11660 int idx, ret = 0;
11661
11662 if (!kvm_arch_has_assigned_device(kvm) ||
11663 !irq_remapping_cap(IRQ_POSTING_CAP) ||
11664 !kvm_vcpu_apicv_active(kvm->vcpus[0]))
11665 return 0;
11666
11667 idx = srcu_read_lock(&kvm->irq_srcu);
11668 irq_rt = srcu_dereference(kvm->irq_routing, &kvm->irq_srcu);
11669 if (guest_irq >= irq_rt->nr_rt_entries ||
11670 hlist_empty(&irq_rt->map[guest_irq])) {
11671 pr_warn_once("no route for guest_irq %u/%u (broken user space?)\n",
11672 guest_irq, irq_rt->nr_rt_entries);
11673 goto out;
11674 }
11675
11676 hlist_for_each_entry(e, &irq_rt->map[guest_irq], link) {
11677 if (e->type != KVM_IRQ_ROUTING_MSI)
11678 continue;
11679 /*
11680 * VT-d PI cannot support posting multicast/broadcast
11681 * interrupts to a vCPU, we still use interrupt remapping
11682 * for these kind of interrupts.
11683 *
11684 * For lowest-priority interrupts, we only support
11685 * those with single CPU as the destination, e.g. user
11686 * configures the interrupts via /proc/irq or uses
11687 * irqbalance to make the interrupts single-CPU.
11688 *
11689 * We will support full lowest-priority interrupt later.
11690 */
11691
11692 kvm_set_msi_irq(kvm, e, &irq);
11693 if (!kvm_intr_is_single_vcpu(kvm, &irq, &vcpu)) {
11694 /*
11695 * Make sure the IRTE is in remapped mode if
11696 * we don't handle it in posted mode.
11697 */
11698 ret = irq_set_vcpu_affinity(host_irq, NULL);
11699 if (ret < 0) {
11700 printk(KERN_INFO
11701 "failed to back to remapped mode, irq: %u\n",
11702 host_irq);
11703 goto out;
11704 }
11705
11706 continue;
11707 }
11708
11709 vcpu_info.pi_desc_addr = __pa(vcpu_to_pi_desc(vcpu));
11710 vcpu_info.vector = irq.vector;
11711
11712 trace_kvm_pi_irte_update(vcpu->vcpu_id, host_irq, e->gsi,
11713 vcpu_info.vector, vcpu_info.pi_desc_addr, set);
11714
11715 if (set)
11716 ret = irq_set_vcpu_affinity(host_irq, &vcpu_info);
11717 else
11718 ret = irq_set_vcpu_affinity(host_irq, NULL);
11719
11720 if (ret < 0) {
11721 printk(KERN_INFO "%s: failed to update PI IRTE\n",
11722 __func__);
11723 goto out;
11724 }
11725 }
11726
11727 ret = 0;
11728 out:
11729 srcu_read_unlock(&kvm->irq_srcu, idx);
11730 return ret;
11731 }
11732
11733 static void vmx_setup_mce(struct kvm_vcpu *vcpu)
11734 {
11735 if (vcpu->arch.mcg_cap & MCG_LMCE_P)
11736 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits |=
11737 FEATURE_CONTROL_LMCE;
11738 else
11739 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits &=
11740 ~FEATURE_CONTROL_LMCE;
11741 }
11742
11743 static struct kvm_x86_ops vmx_x86_ops __ro_after_init = {
11744 .cpu_has_kvm_support = cpu_has_kvm_support,
11745 .disabled_by_bios = vmx_disabled_by_bios,
11746 .hardware_setup = hardware_setup,
11747 .hardware_unsetup = hardware_unsetup,
11748 .check_processor_compatibility = vmx_check_processor_compat,
11749 .hardware_enable = hardware_enable,
11750 .hardware_disable = hardware_disable,
11751 .cpu_has_accelerated_tpr = report_flexpriority,
11752 .cpu_has_high_real_mode_segbase = vmx_has_high_real_mode_segbase,
11753
11754 .vcpu_create = vmx_create_vcpu,
11755 .vcpu_free = vmx_free_vcpu,
11756 .vcpu_reset = vmx_vcpu_reset,
11757
11758 .prepare_guest_switch = vmx_save_host_state,
11759 .vcpu_load = vmx_vcpu_load,
11760 .vcpu_put = vmx_vcpu_put,
11761
11762 .update_bp_intercept = update_exception_bitmap,
11763 .get_msr = vmx_get_msr,
11764 .set_msr = vmx_set_msr,
11765 .get_segment_base = vmx_get_segment_base,
11766 .get_segment = vmx_get_segment,
11767 .set_segment = vmx_set_segment,
11768 .get_cpl = vmx_get_cpl,
11769 .get_cs_db_l_bits = vmx_get_cs_db_l_bits,
11770 .decache_cr0_guest_bits = vmx_decache_cr0_guest_bits,
11771 .decache_cr3 = vmx_decache_cr3,
11772 .decache_cr4_guest_bits = vmx_decache_cr4_guest_bits,
11773 .set_cr0 = vmx_set_cr0,
11774 .set_cr3 = vmx_set_cr3,
11775 .set_cr4 = vmx_set_cr4,
11776 .set_efer = vmx_set_efer,
11777 .get_idt = vmx_get_idt,
11778 .set_idt = vmx_set_idt,
11779 .get_gdt = vmx_get_gdt,
11780 .set_gdt = vmx_set_gdt,
11781 .get_dr6 = vmx_get_dr6,
11782 .set_dr6 = vmx_set_dr6,
11783 .set_dr7 = vmx_set_dr7,
11784 .sync_dirty_debug_regs = vmx_sync_dirty_debug_regs,
11785 .cache_reg = vmx_cache_reg,
11786 .get_rflags = vmx_get_rflags,
11787 .set_rflags = vmx_set_rflags,
11788
11789 .tlb_flush = vmx_flush_tlb,
11790
11791 .run = vmx_vcpu_run,
11792 .handle_exit = vmx_handle_exit,
11793 .skip_emulated_instruction = skip_emulated_instruction,
11794 .set_interrupt_shadow = vmx_set_interrupt_shadow,
11795 .get_interrupt_shadow = vmx_get_interrupt_shadow,
11796 .patch_hypercall = vmx_patch_hypercall,
11797 .set_irq = vmx_inject_irq,
11798 .set_nmi = vmx_inject_nmi,
11799 .queue_exception = vmx_queue_exception,
11800 .cancel_injection = vmx_cancel_injection,
11801 .interrupt_allowed = vmx_interrupt_allowed,
11802 .nmi_allowed = vmx_nmi_allowed,
11803 .get_nmi_mask = vmx_get_nmi_mask,
11804 .set_nmi_mask = vmx_set_nmi_mask,
11805 .enable_nmi_window = enable_nmi_window,
11806 .enable_irq_window = enable_irq_window,
11807 .update_cr8_intercept = update_cr8_intercept,
11808 .set_virtual_x2apic_mode = vmx_set_virtual_x2apic_mode,
11809 .set_apic_access_page_addr = vmx_set_apic_access_page_addr,
11810 .get_enable_apicv = vmx_get_enable_apicv,
11811 .refresh_apicv_exec_ctrl = vmx_refresh_apicv_exec_ctrl,
11812 .load_eoi_exitmap = vmx_load_eoi_exitmap,
11813 .apicv_post_state_restore = vmx_apicv_post_state_restore,
11814 .hwapic_irr_update = vmx_hwapic_irr_update,
11815 .hwapic_isr_update = vmx_hwapic_isr_update,
11816 .sync_pir_to_irr = vmx_sync_pir_to_irr,
11817 .deliver_posted_interrupt = vmx_deliver_posted_interrupt,
11818
11819 .set_tss_addr = vmx_set_tss_addr,
11820 .get_tdp_level = get_ept_level,
11821 .get_mt_mask = vmx_get_mt_mask,
11822
11823 .get_exit_info = vmx_get_exit_info,
11824
11825 .get_lpage_level = vmx_get_lpage_level,
11826
11827 .cpuid_update = vmx_cpuid_update,
11828
11829 .rdtscp_supported = vmx_rdtscp_supported,
11830 .invpcid_supported = vmx_invpcid_supported,
11831
11832 .set_supported_cpuid = vmx_set_supported_cpuid,
11833
11834 .has_wbinvd_exit = cpu_has_vmx_wbinvd_exit,
11835
11836 .write_tsc_offset = vmx_write_tsc_offset,
11837
11838 .set_tdp_cr3 = vmx_set_cr3,
11839
11840 .check_intercept = vmx_check_intercept,
11841 .handle_external_intr = vmx_handle_external_intr,
11842 .mpx_supported = vmx_mpx_supported,
11843 .xsaves_supported = vmx_xsaves_supported,
11844
11845 .check_nested_events = vmx_check_nested_events,
11846
11847 .sched_in = vmx_sched_in,
11848
11849 .slot_enable_log_dirty = vmx_slot_enable_log_dirty,
11850 .slot_disable_log_dirty = vmx_slot_disable_log_dirty,
11851 .flush_log_dirty = vmx_flush_log_dirty,
11852 .enable_log_dirty_pt_masked = vmx_enable_log_dirty_pt_masked,
11853 .write_log_dirty = vmx_write_pml_buffer,
11854
11855 .pre_block = vmx_pre_block,
11856 .post_block = vmx_post_block,
11857
11858 .pmu_ops = &intel_pmu_ops,
11859
11860 .update_pi_irte = vmx_update_pi_irte,
11861
11862 #ifdef CONFIG_X86_64
11863 .set_hv_timer = vmx_set_hv_timer,
11864 .cancel_hv_timer = vmx_cancel_hv_timer,
11865 #endif
11866
11867 .setup_mce = vmx_setup_mce,
11868 };
11869
11870 static int __init vmx_init(void)
11871 {
11872 int r = kvm_init(&vmx_x86_ops, sizeof(struct vcpu_vmx),
11873 __alignof__(struct vcpu_vmx), THIS_MODULE);
11874 if (r)
11875 return r;
11876
11877 #ifdef CONFIG_KEXEC_CORE
11878 rcu_assign_pointer(crash_vmclear_loaded_vmcss,
11879 crash_vmclear_local_loaded_vmcss);
11880 #endif
11881
11882 return 0;
11883 }
11884
11885 static void __exit vmx_exit(void)
11886 {
11887 #ifdef CONFIG_KEXEC_CORE
11888 RCU_INIT_POINTER(crash_vmclear_loaded_vmcss, NULL);
11889 synchronize_rcu();
11890 #endif
11891
11892 kvm_exit();
11893 }
11894
11895 module_init(vmx_init)
11896 module_exit(vmx_exit)