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