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/sched/smt.h>
31 #include <linux/moduleparam.h>
32 #include <linux/mod_devicetable.h>
33 #include <linux/trace_events.h>
34 #include <linux/slab.h>
35 #include <linux/tboot.h>
36 #include <linux/hrtimer.h>
37 #include <linux/frame.h>
38 #include <linux/nospec.h>
39 #include "kvm_cache_regs.h"
40 #include "x86.h"
41
42 #include <asm/asm.h>
43 #include <asm/cpu.h>
44 #include <asm/io.h>
45 #include <asm/desc.h>
46 #include <asm/vmx.h>
47 #include <asm/virtext.h>
48 #include <asm/mce.h>
49 #include <asm/fpu/internal.h>
50 #include <asm/perf_event.h>
51 #include <asm/debugreg.h>
52 #include <asm/kexec.h>
53 #include <asm/apic.h>
54 #include <asm/irq_remapping.h>
55 #include <asm/mmu_context.h>
56 #include <asm/spec-ctrl.h>
57 #include <asm/mshyperv.h>
58
59 #include "trace.h"
60 #include "pmu.h"
61 #include "vmx_evmcs.h"
62
63 #define __ex(x) __kvm_handle_fault_on_reboot(x)
64 #define __ex_clear(x, reg) \
65 ____kvm_handle_fault_on_reboot(x, "xor " reg " , " reg)
66
67 MODULE_AUTHOR("Qumranet");
68 MODULE_LICENSE("GPL");
69
70 static const struct x86_cpu_id vmx_cpu_id[] = {
71 X86_FEATURE_MATCH(X86_FEATURE_VMX),
72 {}
73 };
74 MODULE_DEVICE_TABLE(x86cpu, vmx_cpu_id);
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
126 #define KVM_VMX_TSC_MULTIPLIER_MAX 0xffffffffffffffffULL
127
128 /* Guest_tsc -> host_tsc conversion requires 64-bit division. */
129 static int __read_mostly cpu_preemption_timer_multi;
130 static bool __read_mostly enable_preemption_timer = 1;
131 #ifdef CONFIG_X86_64
132 module_param_named(preemption_timer, enable_preemption_timer, bool, S_IRUGO);
133 #endif
134
135 #define KVM_GUEST_CR0_MASK (X86_CR0_NW | X86_CR0_CD)
136 #define KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST X86_CR0_NE
137 #define KVM_VM_CR0_ALWAYS_ON \
138 (KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST | \
139 X86_CR0_WP | 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_VM_CR4_ALWAYS_ON_UNRESTRICTED_GUEST X86_CR4_VMXE
145 #define KVM_PMODE_VM_CR4_ALWAYS_ON (X86_CR4_PAE | X86_CR4_VMXE)
146 #define KVM_RMODE_VM_CR4_ALWAYS_ON (X86_CR4_VME | X86_CR4_PAE | X86_CR4_VMXE)
147
148 #define RMODE_GUEST_OWNED_EFLAGS_BITS (~(X86_EFLAGS_IOPL | X86_EFLAGS_VM))
149
150 #define VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE 5
151
152 /*
153 * Hyper-V requires all of these, so mark them as supported even though
154 * they are just treated the same as all-context.
155 */
156 #define VMX_VPID_EXTENT_SUPPORTED_MASK \
157 (VMX_VPID_EXTENT_INDIVIDUAL_ADDR_BIT | \
158 VMX_VPID_EXTENT_SINGLE_CONTEXT_BIT | \
159 VMX_VPID_EXTENT_GLOBAL_CONTEXT_BIT | \
160 VMX_VPID_EXTENT_SINGLE_NON_GLOBAL_BIT)
161
162 /*
163 * These 2 parameters are used to config the controls for Pause-Loop Exiting:
164 * ple_gap: upper bound on the amount of time between two successive
165 * executions of PAUSE in a loop. Also indicate if ple enabled.
166 * According to test, this time is usually smaller than 128 cycles.
167 * ple_window: upper bound on the amount of time a guest is allowed to execute
168 * in a PAUSE loop. Tests indicate that most spinlocks are held for
169 * less than 2^12 cycles
170 * Time is measured based on a counter that runs at the same rate as the TSC,
171 * refer SDM volume 3b section 21.6.13 & 22.1.3.
172 */
173 static unsigned int ple_gap = KVM_DEFAULT_PLE_GAP;
174 module_param(ple_gap, uint, 0444);
175
176 static unsigned int ple_window = KVM_VMX_DEFAULT_PLE_WINDOW;
177 module_param(ple_window, uint, 0444);
178
179 /* Default doubles per-vcpu window every exit. */
180 static unsigned int ple_window_grow = KVM_DEFAULT_PLE_WINDOW_GROW;
181 module_param(ple_window_grow, uint, 0444);
182
183 /* Default resets per-vcpu window every exit to ple_window. */
184 static unsigned int ple_window_shrink = KVM_DEFAULT_PLE_WINDOW_SHRINK;
185 module_param(ple_window_shrink, uint, 0444);
186
187 /* Default is to compute the maximum so we can never overflow. */
188 static unsigned int ple_window_max = KVM_VMX_DEFAULT_PLE_WINDOW_MAX;
189 module_param(ple_window_max, uint, 0444);
190
191 extern const ulong vmx_return;
192
193 static DEFINE_STATIC_KEY_FALSE(vmx_l1d_should_flush);
194 static DEFINE_STATIC_KEY_FALSE(vmx_l1d_flush_cond);
195 static DEFINE_MUTEX(vmx_l1d_flush_mutex);
196
197 /* Storage for pre module init parameter parsing */
198 static enum vmx_l1d_flush_state __read_mostly vmentry_l1d_flush_param = VMENTER_L1D_FLUSH_AUTO;
199
200 static const struct {
201 const char *option;
202 bool for_parse;
203 } vmentry_l1d_param[] = {
204 [VMENTER_L1D_FLUSH_AUTO] = {"auto", true},
205 [VMENTER_L1D_FLUSH_NEVER] = {"never", true},
206 [VMENTER_L1D_FLUSH_COND] = {"cond", true},
207 [VMENTER_L1D_FLUSH_ALWAYS] = {"always", true},
208 [VMENTER_L1D_FLUSH_EPT_DISABLED] = {"EPT disabled", false},
209 [VMENTER_L1D_FLUSH_NOT_REQUIRED] = {"not required", false},
210 };
211
212 #define L1D_CACHE_ORDER 4
213 static void *vmx_l1d_flush_pages;
214
vmx_setup_l1d_flush(enum vmx_l1d_flush_state l1tf)215 static int vmx_setup_l1d_flush(enum vmx_l1d_flush_state l1tf)
216 {
217 struct page *page;
218 unsigned int i;
219
220 if (!enable_ept) {
221 l1tf_vmx_mitigation = VMENTER_L1D_FLUSH_EPT_DISABLED;
222 return 0;
223 }
224
225 if (boot_cpu_has(X86_FEATURE_ARCH_CAPABILITIES)) {
226 u64 msr;
227
228 rdmsrl(MSR_IA32_ARCH_CAPABILITIES, msr);
229 if (msr & ARCH_CAP_SKIP_VMENTRY_L1DFLUSH) {
230 l1tf_vmx_mitigation = VMENTER_L1D_FLUSH_NOT_REQUIRED;
231 return 0;
232 }
233 }
234
235 /* If set to auto use the default l1tf mitigation method */
236 if (l1tf == VMENTER_L1D_FLUSH_AUTO) {
237 switch (l1tf_mitigation) {
238 case L1TF_MITIGATION_OFF:
239 l1tf = VMENTER_L1D_FLUSH_NEVER;
240 break;
241 case L1TF_MITIGATION_FLUSH_NOWARN:
242 case L1TF_MITIGATION_FLUSH:
243 case L1TF_MITIGATION_FLUSH_NOSMT:
244 l1tf = VMENTER_L1D_FLUSH_COND;
245 break;
246 case L1TF_MITIGATION_FULL:
247 case L1TF_MITIGATION_FULL_FORCE:
248 l1tf = VMENTER_L1D_FLUSH_ALWAYS;
249 break;
250 }
251 } else if (l1tf_mitigation == L1TF_MITIGATION_FULL_FORCE) {
252 l1tf = VMENTER_L1D_FLUSH_ALWAYS;
253 }
254
255 if (l1tf != VMENTER_L1D_FLUSH_NEVER && !vmx_l1d_flush_pages &&
256 !boot_cpu_has(X86_FEATURE_FLUSH_L1D)) {
257 page = alloc_pages(GFP_KERNEL, L1D_CACHE_ORDER);
258 if (!page)
259 return -ENOMEM;
260 vmx_l1d_flush_pages = page_address(page);
261
262 /*
263 * Initialize each page with a different pattern in
264 * order to protect against KSM in the nested
265 * virtualization case.
266 */
267 for (i = 0; i < 1u << L1D_CACHE_ORDER; ++i) {
268 memset(vmx_l1d_flush_pages + i * PAGE_SIZE, i + 1,
269 PAGE_SIZE);
270 }
271 }
272
273 l1tf_vmx_mitigation = l1tf;
274
275 if (l1tf != VMENTER_L1D_FLUSH_NEVER)
276 static_branch_enable(&vmx_l1d_should_flush);
277 else
278 static_branch_disable(&vmx_l1d_should_flush);
279
280 if (l1tf == VMENTER_L1D_FLUSH_COND)
281 static_branch_enable(&vmx_l1d_flush_cond);
282 else
283 static_branch_disable(&vmx_l1d_flush_cond);
284 return 0;
285 }
286
vmentry_l1d_flush_parse(const char * s)287 static int vmentry_l1d_flush_parse(const char *s)
288 {
289 unsigned int i;
290
291 if (s) {
292 for (i = 0; i < ARRAY_SIZE(vmentry_l1d_param); i++) {
293 if (vmentry_l1d_param[i].for_parse &&
294 sysfs_streq(s, vmentry_l1d_param[i].option))
295 return i;
296 }
297 }
298 return -EINVAL;
299 }
300
vmentry_l1d_flush_set(const char * s,const struct kernel_param * kp)301 static int vmentry_l1d_flush_set(const char *s, const struct kernel_param *kp)
302 {
303 int l1tf, ret;
304
305 l1tf = vmentry_l1d_flush_parse(s);
306 if (l1tf < 0)
307 return l1tf;
308
309 if (!boot_cpu_has(X86_BUG_L1TF))
310 return 0;
311
312 /*
313 * Has vmx_init() run already? If not then this is the pre init
314 * parameter parsing. In that case just store the value and let
315 * vmx_init() do the proper setup after enable_ept has been
316 * established.
317 */
318 if (l1tf_vmx_mitigation == VMENTER_L1D_FLUSH_AUTO) {
319 vmentry_l1d_flush_param = l1tf;
320 return 0;
321 }
322
323 mutex_lock(&vmx_l1d_flush_mutex);
324 ret = vmx_setup_l1d_flush(l1tf);
325 mutex_unlock(&vmx_l1d_flush_mutex);
326 return ret;
327 }
328
vmentry_l1d_flush_get(char * s,const struct kernel_param * kp)329 static int vmentry_l1d_flush_get(char *s, const struct kernel_param *kp)
330 {
331 if (WARN_ON_ONCE(l1tf_vmx_mitigation >= ARRAY_SIZE(vmentry_l1d_param)))
332 return sprintf(s, "???\n");
333
334 return sprintf(s, "%s\n", vmentry_l1d_param[l1tf_vmx_mitigation].option);
335 }
336
337 static const struct kernel_param_ops vmentry_l1d_flush_ops = {
338 .set = vmentry_l1d_flush_set,
339 .get = vmentry_l1d_flush_get,
340 };
341 module_param_cb(vmentry_l1d_flush, &vmentry_l1d_flush_ops, NULL, 0644);
342
343 enum ept_pointers_status {
344 EPT_POINTERS_CHECK = 0,
345 EPT_POINTERS_MATCH = 1,
346 EPT_POINTERS_MISMATCH = 2
347 };
348
349 struct kvm_vmx {
350 struct kvm kvm;
351
352 unsigned int tss_addr;
353 bool ept_identity_pagetable_done;
354 gpa_t ept_identity_map_addr;
355
356 enum ept_pointers_status ept_pointers_match;
357 spinlock_t ept_pointer_lock;
358 };
359
360 #define NR_AUTOLOAD_MSRS 8
361
362 struct vmcs_hdr {
363 u32 revision_id:31;
364 u32 shadow_vmcs:1;
365 };
366
367 struct vmcs {
368 struct vmcs_hdr hdr;
369 u32 abort;
370 char data[0];
371 };
372
373 /*
374 * vmcs_host_state tracks registers that are loaded from the VMCS on VMEXIT
375 * and whose values change infrequently, but are not constant. I.e. this is
376 * used as a write-through cache of the corresponding VMCS fields.
377 */
378 struct vmcs_host_state {
379 unsigned long cr3; /* May not match real cr3 */
380 unsigned long cr4; /* May not match real cr4 */
381 unsigned long gs_base;
382 unsigned long fs_base;
383
384 u16 fs_sel, gs_sel, ldt_sel;
385 #ifdef CONFIG_X86_64
386 u16 ds_sel, es_sel;
387 #endif
388 };
389
390 /*
391 * Track a VMCS that may be loaded on a certain CPU. If it is (cpu!=-1), also
392 * remember whether it was VMLAUNCHed, and maintain a linked list of all VMCSs
393 * loaded on this CPU (so we can clear them if the CPU goes down).
394 */
395 struct loaded_vmcs {
396 struct vmcs *vmcs;
397 struct vmcs *shadow_vmcs;
398 int cpu;
399 bool launched;
400 bool nmi_known_unmasked;
401 bool hv_timer_armed;
402 /* Support for vnmi-less CPUs */
403 int soft_vnmi_blocked;
404 ktime_t entry_time;
405 s64 vnmi_blocked_time;
406 unsigned long *msr_bitmap;
407 struct list_head loaded_vmcss_on_cpu_link;
408 struct vmcs_host_state host_state;
409 };
410
411 struct shared_msr_entry {
412 unsigned index;
413 u64 data;
414 u64 mask;
415 };
416
417 /*
418 * struct vmcs12 describes the state that our guest hypervisor (L1) keeps for a
419 * single nested guest (L2), hence the name vmcs12. Any VMX implementation has
420 * a VMCS structure, and vmcs12 is our emulated VMX's VMCS. This structure is
421 * stored in guest memory specified by VMPTRLD, but is opaque to the guest,
422 * which must access it using VMREAD/VMWRITE/VMCLEAR instructions.
423 * More than one of these structures may exist, if L1 runs multiple L2 guests.
424 * nested_vmx_run() will use the data here to build the vmcs02: a VMCS for the
425 * underlying hardware which will be used to run L2.
426 * This structure is packed to ensure that its layout is identical across
427 * machines (necessary for live migration).
428 *
429 * IMPORTANT: Changing the layout of existing fields in this structure
430 * will break save/restore compatibility with older kvm releases. When
431 * adding new fields, either use space in the reserved padding* arrays
432 * or add the new fields to the end of the structure.
433 */
434 typedef u64 natural_width;
435 struct __packed vmcs12 {
436 /* According to the Intel spec, a VMCS region must start with the
437 * following two fields. Then follow implementation-specific data.
438 */
439 struct vmcs_hdr hdr;
440 u32 abort;
441
442 u32 launch_state; /* set to 0 by VMCLEAR, to 1 by VMLAUNCH */
443 u32 padding[7]; /* room for future expansion */
444
445 u64 io_bitmap_a;
446 u64 io_bitmap_b;
447 u64 msr_bitmap;
448 u64 vm_exit_msr_store_addr;
449 u64 vm_exit_msr_load_addr;
450 u64 vm_entry_msr_load_addr;
451 u64 tsc_offset;
452 u64 virtual_apic_page_addr;
453 u64 apic_access_addr;
454 u64 posted_intr_desc_addr;
455 u64 ept_pointer;
456 u64 eoi_exit_bitmap0;
457 u64 eoi_exit_bitmap1;
458 u64 eoi_exit_bitmap2;
459 u64 eoi_exit_bitmap3;
460 u64 xss_exit_bitmap;
461 u64 guest_physical_address;
462 u64 vmcs_link_pointer;
463 u64 guest_ia32_debugctl;
464 u64 guest_ia32_pat;
465 u64 guest_ia32_efer;
466 u64 guest_ia32_perf_global_ctrl;
467 u64 guest_pdptr0;
468 u64 guest_pdptr1;
469 u64 guest_pdptr2;
470 u64 guest_pdptr3;
471 u64 guest_bndcfgs;
472 u64 host_ia32_pat;
473 u64 host_ia32_efer;
474 u64 host_ia32_perf_global_ctrl;
475 u64 vmread_bitmap;
476 u64 vmwrite_bitmap;
477 u64 vm_function_control;
478 u64 eptp_list_address;
479 u64 pml_address;
480 u64 padding64[3]; /* room for future expansion */
481 /*
482 * To allow migration of L1 (complete with its L2 guests) between
483 * machines of different natural widths (32 or 64 bit), we cannot have
484 * unsigned long fields with no explict size. We use u64 (aliased
485 * natural_width) instead. Luckily, x86 is little-endian.
486 */
487 natural_width cr0_guest_host_mask;
488 natural_width cr4_guest_host_mask;
489 natural_width cr0_read_shadow;
490 natural_width cr4_read_shadow;
491 natural_width cr3_target_value0;
492 natural_width cr3_target_value1;
493 natural_width cr3_target_value2;
494 natural_width cr3_target_value3;
495 natural_width exit_qualification;
496 natural_width guest_linear_address;
497 natural_width guest_cr0;
498 natural_width guest_cr3;
499 natural_width guest_cr4;
500 natural_width guest_es_base;
501 natural_width guest_cs_base;
502 natural_width guest_ss_base;
503 natural_width guest_ds_base;
504 natural_width guest_fs_base;
505 natural_width guest_gs_base;
506 natural_width guest_ldtr_base;
507 natural_width guest_tr_base;
508 natural_width guest_gdtr_base;
509 natural_width guest_idtr_base;
510 natural_width guest_dr7;
511 natural_width guest_rsp;
512 natural_width guest_rip;
513 natural_width guest_rflags;
514 natural_width guest_pending_dbg_exceptions;
515 natural_width guest_sysenter_esp;
516 natural_width guest_sysenter_eip;
517 natural_width host_cr0;
518 natural_width host_cr3;
519 natural_width host_cr4;
520 natural_width host_fs_base;
521 natural_width host_gs_base;
522 natural_width host_tr_base;
523 natural_width host_gdtr_base;
524 natural_width host_idtr_base;
525 natural_width host_ia32_sysenter_esp;
526 natural_width host_ia32_sysenter_eip;
527 natural_width host_rsp;
528 natural_width host_rip;
529 natural_width paddingl[8]; /* room for future expansion */
530 u32 pin_based_vm_exec_control;
531 u32 cpu_based_vm_exec_control;
532 u32 exception_bitmap;
533 u32 page_fault_error_code_mask;
534 u32 page_fault_error_code_match;
535 u32 cr3_target_count;
536 u32 vm_exit_controls;
537 u32 vm_exit_msr_store_count;
538 u32 vm_exit_msr_load_count;
539 u32 vm_entry_controls;
540 u32 vm_entry_msr_load_count;
541 u32 vm_entry_intr_info_field;
542 u32 vm_entry_exception_error_code;
543 u32 vm_entry_instruction_len;
544 u32 tpr_threshold;
545 u32 secondary_vm_exec_control;
546 u32 vm_instruction_error;
547 u32 vm_exit_reason;
548 u32 vm_exit_intr_info;
549 u32 vm_exit_intr_error_code;
550 u32 idt_vectoring_info_field;
551 u32 idt_vectoring_error_code;
552 u32 vm_exit_instruction_len;
553 u32 vmx_instruction_info;
554 u32 guest_es_limit;
555 u32 guest_cs_limit;
556 u32 guest_ss_limit;
557 u32 guest_ds_limit;
558 u32 guest_fs_limit;
559 u32 guest_gs_limit;
560 u32 guest_ldtr_limit;
561 u32 guest_tr_limit;
562 u32 guest_gdtr_limit;
563 u32 guest_idtr_limit;
564 u32 guest_es_ar_bytes;
565 u32 guest_cs_ar_bytes;
566 u32 guest_ss_ar_bytes;
567 u32 guest_ds_ar_bytes;
568 u32 guest_fs_ar_bytes;
569 u32 guest_gs_ar_bytes;
570 u32 guest_ldtr_ar_bytes;
571 u32 guest_tr_ar_bytes;
572 u32 guest_interruptibility_info;
573 u32 guest_activity_state;
574 u32 guest_sysenter_cs;
575 u32 host_ia32_sysenter_cs;
576 u32 vmx_preemption_timer_value;
577 u32 padding32[7]; /* room for future expansion */
578 u16 virtual_processor_id;
579 u16 posted_intr_nv;
580 u16 guest_es_selector;
581 u16 guest_cs_selector;
582 u16 guest_ss_selector;
583 u16 guest_ds_selector;
584 u16 guest_fs_selector;
585 u16 guest_gs_selector;
586 u16 guest_ldtr_selector;
587 u16 guest_tr_selector;
588 u16 guest_intr_status;
589 u16 host_es_selector;
590 u16 host_cs_selector;
591 u16 host_ss_selector;
592 u16 host_ds_selector;
593 u16 host_fs_selector;
594 u16 host_gs_selector;
595 u16 host_tr_selector;
596 u16 guest_pml_index;
597 };
598
599 /*
600 * For save/restore compatibility, the vmcs12 field offsets must not change.
601 */
602 #define CHECK_OFFSET(field, loc) \
603 BUILD_BUG_ON_MSG(offsetof(struct vmcs12, field) != (loc), \
604 "Offset of " #field " in struct vmcs12 has changed.")
605
vmx_check_vmcs12_offsets(void)606 static inline void vmx_check_vmcs12_offsets(void) {
607 CHECK_OFFSET(hdr, 0);
608 CHECK_OFFSET(abort, 4);
609 CHECK_OFFSET(launch_state, 8);
610 CHECK_OFFSET(io_bitmap_a, 40);
611 CHECK_OFFSET(io_bitmap_b, 48);
612 CHECK_OFFSET(msr_bitmap, 56);
613 CHECK_OFFSET(vm_exit_msr_store_addr, 64);
614 CHECK_OFFSET(vm_exit_msr_load_addr, 72);
615 CHECK_OFFSET(vm_entry_msr_load_addr, 80);
616 CHECK_OFFSET(tsc_offset, 88);
617 CHECK_OFFSET(virtual_apic_page_addr, 96);
618 CHECK_OFFSET(apic_access_addr, 104);
619 CHECK_OFFSET(posted_intr_desc_addr, 112);
620 CHECK_OFFSET(ept_pointer, 120);
621 CHECK_OFFSET(eoi_exit_bitmap0, 128);
622 CHECK_OFFSET(eoi_exit_bitmap1, 136);
623 CHECK_OFFSET(eoi_exit_bitmap2, 144);
624 CHECK_OFFSET(eoi_exit_bitmap3, 152);
625 CHECK_OFFSET(xss_exit_bitmap, 160);
626 CHECK_OFFSET(guest_physical_address, 168);
627 CHECK_OFFSET(vmcs_link_pointer, 176);
628 CHECK_OFFSET(guest_ia32_debugctl, 184);
629 CHECK_OFFSET(guest_ia32_pat, 192);
630 CHECK_OFFSET(guest_ia32_efer, 200);
631 CHECK_OFFSET(guest_ia32_perf_global_ctrl, 208);
632 CHECK_OFFSET(guest_pdptr0, 216);
633 CHECK_OFFSET(guest_pdptr1, 224);
634 CHECK_OFFSET(guest_pdptr2, 232);
635 CHECK_OFFSET(guest_pdptr3, 240);
636 CHECK_OFFSET(guest_bndcfgs, 248);
637 CHECK_OFFSET(host_ia32_pat, 256);
638 CHECK_OFFSET(host_ia32_efer, 264);
639 CHECK_OFFSET(host_ia32_perf_global_ctrl, 272);
640 CHECK_OFFSET(vmread_bitmap, 280);
641 CHECK_OFFSET(vmwrite_bitmap, 288);
642 CHECK_OFFSET(vm_function_control, 296);
643 CHECK_OFFSET(eptp_list_address, 304);
644 CHECK_OFFSET(pml_address, 312);
645 CHECK_OFFSET(cr0_guest_host_mask, 344);
646 CHECK_OFFSET(cr4_guest_host_mask, 352);
647 CHECK_OFFSET(cr0_read_shadow, 360);
648 CHECK_OFFSET(cr4_read_shadow, 368);
649 CHECK_OFFSET(cr3_target_value0, 376);
650 CHECK_OFFSET(cr3_target_value1, 384);
651 CHECK_OFFSET(cr3_target_value2, 392);
652 CHECK_OFFSET(cr3_target_value3, 400);
653 CHECK_OFFSET(exit_qualification, 408);
654 CHECK_OFFSET(guest_linear_address, 416);
655 CHECK_OFFSET(guest_cr0, 424);
656 CHECK_OFFSET(guest_cr3, 432);
657 CHECK_OFFSET(guest_cr4, 440);
658 CHECK_OFFSET(guest_es_base, 448);
659 CHECK_OFFSET(guest_cs_base, 456);
660 CHECK_OFFSET(guest_ss_base, 464);
661 CHECK_OFFSET(guest_ds_base, 472);
662 CHECK_OFFSET(guest_fs_base, 480);
663 CHECK_OFFSET(guest_gs_base, 488);
664 CHECK_OFFSET(guest_ldtr_base, 496);
665 CHECK_OFFSET(guest_tr_base, 504);
666 CHECK_OFFSET(guest_gdtr_base, 512);
667 CHECK_OFFSET(guest_idtr_base, 520);
668 CHECK_OFFSET(guest_dr7, 528);
669 CHECK_OFFSET(guest_rsp, 536);
670 CHECK_OFFSET(guest_rip, 544);
671 CHECK_OFFSET(guest_rflags, 552);
672 CHECK_OFFSET(guest_pending_dbg_exceptions, 560);
673 CHECK_OFFSET(guest_sysenter_esp, 568);
674 CHECK_OFFSET(guest_sysenter_eip, 576);
675 CHECK_OFFSET(host_cr0, 584);
676 CHECK_OFFSET(host_cr3, 592);
677 CHECK_OFFSET(host_cr4, 600);
678 CHECK_OFFSET(host_fs_base, 608);
679 CHECK_OFFSET(host_gs_base, 616);
680 CHECK_OFFSET(host_tr_base, 624);
681 CHECK_OFFSET(host_gdtr_base, 632);
682 CHECK_OFFSET(host_idtr_base, 640);
683 CHECK_OFFSET(host_ia32_sysenter_esp, 648);
684 CHECK_OFFSET(host_ia32_sysenter_eip, 656);
685 CHECK_OFFSET(host_rsp, 664);
686 CHECK_OFFSET(host_rip, 672);
687 CHECK_OFFSET(pin_based_vm_exec_control, 744);
688 CHECK_OFFSET(cpu_based_vm_exec_control, 748);
689 CHECK_OFFSET(exception_bitmap, 752);
690 CHECK_OFFSET(page_fault_error_code_mask, 756);
691 CHECK_OFFSET(page_fault_error_code_match, 760);
692 CHECK_OFFSET(cr3_target_count, 764);
693 CHECK_OFFSET(vm_exit_controls, 768);
694 CHECK_OFFSET(vm_exit_msr_store_count, 772);
695 CHECK_OFFSET(vm_exit_msr_load_count, 776);
696 CHECK_OFFSET(vm_entry_controls, 780);
697 CHECK_OFFSET(vm_entry_msr_load_count, 784);
698 CHECK_OFFSET(vm_entry_intr_info_field, 788);
699 CHECK_OFFSET(vm_entry_exception_error_code, 792);
700 CHECK_OFFSET(vm_entry_instruction_len, 796);
701 CHECK_OFFSET(tpr_threshold, 800);
702 CHECK_OFFSET(secondary_vm_exec_control, 804);
703 CHECK_OFFSET(vm_instruction_error, 808);
704 CHECK_OFFSET(vm_exit_reason, 812);
705 CHECK_OFFSET(vm_exit_intr_info, 816);
706 CHECK_OFFSET(vm_exit_intr_error_code, 820);
707 CHECK_OFFSET(idt_vectoring_info_field, 824);
708 CHECK_OFFSET(idt_vectoring_error_code, 828);
709 CHECK_OFFSET(vm_exit_instruction_len, 832);
710 CHECK_OFFSET(vmx_instruction_info, 836);
711 CHECK_OFFSET(guest_es_limit, 840);
712 CHECK_OFFSET(guest_cs_limit, 844);
713 CHECK_OFFSET(guest_ss_limit, 848);
714 CHECK_OFFSET(guest_ds_limit, 852);
715 CHECK_OFFSET(guest_fs_limit, 856);
716 CHECK_OFFSET(guest_gs_limit, 860);
717 CHECK_OFFSET(guest_ldtr_limit, 864);
718 CHECK_OFFSET(guest_tr_limit, 868);
719 CHECK_OFFSET(guest_gdtr_limit, 872);
720 CHECK_OFFSET(guest_idtr_limit, 876);
721 CHECK_OFFSET(guest_es_ar_bytes, 880);
722 CHECK_OFFSET(guest_cs_ar_bytes, 884);
723 CHECK_OFFSET(guest_ss_ar_bytes, 888);
724 CHECK_OFFSET(guest_ds_ar_bytes, 892);
725 CHECK_OFFSET(guest_fs_ar_bytes, 896);
726 CHECK_OFFSET(guest_gs_ar_bytes, 900);
727 CHECK_OFFSET(guest_ldtr_ar_bytes, 904);
728 CHECK_OFFSET(guest_tr_ar_bytes, 908);
729 CHECK_OFFSET(guest_interruptibility_info, 912);
730 CHECK_OFFSET(guest_activity_state, 916);
731 CHECK_OFFSET(guest_sysenter_cs, 920);
732 CHECK_OFFSET(host_ia32_sysenter_cs, 924);
733 CHECK_OFFSET(vmx_preemption_timer_value, 928);
734 CHECK_OFFSET(virtual_processor_id, 960);
735 CHECK_OFFSET(posted_intr_nv, 962);
736 CHECK_OFFSET(guest_es_selector, 964);
737 CHECK_OFFSET(guest_cs_selector, 966);
738 CHECK_OFFSET(guest_ss_selector, 968);
739 CHECK_OFFSET(guest_ds_selector, 970);
740 CHECK_OFFSET(guest_fs_selector, 972);
741 CHECK_OFFSET(guest_gs_selector, 974);
742 CHECK_OFFSET(guest_ldtr_selector, 976);
743 CHECK_OFFSET(guest_tr_selector, 978);
744 CHECK_OFFSET(guest_intr_status, 980);
745 CHECK_OFFSET(host_es_selector, 982);
746 CHECK_OFFSET(host_cs_selector, 984);
747 CHECK_OFFSET(host_ss_selector, 986);
748 CHECK_OFFSET(host_ds_selector, 988);
749 CHECK_OFFSET(host_fs_selector, 990);
750 CHECK_OFFSET(host_gs_selector, 992);
751 CHECK_OFFSET(host_tr_selector, 994);
752 CHECK_OFFSET(guest_pml_index, 996);
753 }
754
755 /*
756 * VMCS12_REVISION is an arbitrary id that should be changed if the content or
757 * layout of struct vmcs12 is changed. MSR_IA32_VMX_BASIC returns this id, and
758 * VMPTRLD verifies that the VMCS region that L1 is loading contains this id.
759 *
760 * IMPORTANT: Changing this value will break save/restore compatibility with
761 * older kvm releases.
762 */
763 #define VMCS12_REVISION 0x11e57ed0
764
765 /*
766 * VMCS12_SIZE is the number of bytes L1 should allocate for the VMXON region
767 * and any VMCS region. Although only sizeof(struct vmcs12) are used by the
768 * current implementation, 4K are reserved to avoid future complications.
769 */
770 #define VMCS12_SIZE 0x1000
771
772 /*
773 * VMCS12_MAX_FIELD_INDEX is the highest index value used in any
774 * supported VMCS12 field encoding.
775 */
776 #define VMCS12_MAX_FIELD_INDEX 0x17
777
778 struct nested_vmx_msrs {
779 /*
780 * We only store the "true" versions of the VMX capability MSRs. We
781 * generate the "non-true" versions by setting the must-be-1 bits
782 * according to the SDM.
783 */
784 u32 procbased_ctls_low;
785 u32 procbased_ctls_high;
786 u32 secondary_ctls_low;
787 u32 secondary_ctls_high;
788 u32 pinbased_ctls_low;
789 u32 pinbased_ctls_high;
790 u32 exit_ctls_low;
791 u32 exit_ctls_high;
792 u32 entry_ctls_low;
793 u32 entry_ctls_high;
794 u32 misc_low;
795 u32 misc_high;
796 u32 ept_caps;
797 u32 vpid_caps;
798 u64 basic;
799 u64 cr0_fixed0;
800 u64 cr0_fixed1;
801 u64 cr4_fixed0;
802 u64 cr4_fixed1;
803 u64 vmcs_enum;
804 u64 vmfunc_controls;
805 };
806
807 /*
808 * The nested_vmx structure is part of vcpu_vmx, and holds information we need
809 * for correct emulation of VMX (i.e., nested VMX) on this vcpu.
810 */
811 struct nested_vmx {
812 /* Has the level1 guest done vmxon? */
813 bool vmxon;
814 gpa_t vmxon_ptr;
815 bool pml_full;
816
817 /* The guest-physical address of the current VMCS L1 keeps for L2 */
818 gpa_t current_vmptr;
819 /*
820 * Cache of the guest's VMCS, existing outside of guest memory.
821 * Loaded from guest memory during VMPTRLD. Flushed to guest
822 * memory during VMCLEAR and VMPTRLD.
823 */
824 struct vmcs12 *cached_vmcs12;
825 /*
826 * Cache of the guest's shadow VMCS, existing outside of guest
827 * memory. Loaded from guest memory during VM entry. Flushed
828 * to guest memory during VM exit.
829 */
830 struct vmcs12 *cached_shadow_vmcs12;
831 /*
832 * Indicates if the shadow vmcs must be updated with the
833 * data hold by vmcs12
834 */
835 bool sync_shadow_vmcs;
836 bool dirty_vmcs12;
837
838 bool change_vmcs01_virtual_apic_mode;
839
840 /* L2 must run next, and mustn't decide to exit to L1. */
841 bool nested_run_pending;
842
843 struct loaded_vmcs vmcs02;
844
845 /*
846 * Guest pages referred to in the vmcs02 with host-physical
847 * pointers, so we must keep them pinned while L2 runs.
848 */
849 struct page *apic_access_page;
850 struct page *virtual_apic_page;
851 struct page *pi_desc_page;
852 struct pi_desc *pi_desc;
853 bool pi_pending;
854 u16 posted_intr_nv;
855
856 struct hrtimer preemption_timer;
857 bool preemption_timer_expired;
858
859 /* to migrate it to L2 if VM_ENTRY_LOAD_DEBUG_CONTROLS is off */
860 u64 vmcs01_debugctl;
861 u64 vmcs01_guest_bndcfgs;
862
863 u16 vpid02;
864 u16 last_vpid;
865
866 struct nested_vmx_msrs msrs;
867
868 /* SMM related state */
869 struct {
870 /* in VMX operation on SMM entry? */
871 bool vmxon;
872 /* in guest mode on SMM entry? */
873 bool guest_mode;
874 } smm;
875 };
876
877 #define POSTED_INTR_ON 0
878 #define POSTED_INTR_SN 1
879
880 /* Posted-Interrupt Descriptor */
881 struct pi_desc {
882 u32 pir[8]; /* Posted interrupt requested */
883 union {
884 struct {
885 /* bit 256 - Outstanding Notification */
886 u16 on : 1,
887 /* bit 257 - Suppress Notification */
888 sn : 1,
889 /* bit 271:258 - Reserved */
890 rsvd_1 : 14;
891 /* bit 279:272 - Notification Vector */
892 u8 nv;
893 /* bit 287:280 - Reserved */
894 u8 rsvd_2;
895 /* bit 319:288 - Notification Destination */
896 u32 ndst;
897 };
898 u64 control;
899 };
900 u32 rsvd[6];
901 } __aligned(64);
902
pi_test_and_set_on(struct pi_desc * pi_desc)903 static bool pi_test_and_set_on(struct pi_desc *pi_desc)
904 {
905 return test_and_set_bit(POSTED_INTR_ON,
906 (unsigned long *)&pi_desc->control);
907 }
908
pi_test_and_clear_on(struct pi_desc * pi_desc)909 static bool pi_test_and_clear_on(struct pi_desc *pi_desc)
910 {
911 return test_and_clear_bit(POSTED_INTR_ON,
912 (unsigned long *)&pi_desc->control);
913 }
914
pi_test_and_set_pir(int vector,struct pi_desc * pi_desc)915 static int pi_test_and_set_pir(int vector, struct pi_desc *pi_desc)
916 {
917 return test_and_set_bit(vector, (unsigned long *)pi_desc->pir);
918 }
919
pi_clear_sn(struct pi_desc * pi_desc)920 static inline void pi_clear_sn(struct pi_desc *pi_desc)
921 {
922 return clear_bit(POSTED_INTR_SN,
923 (unsigned long *)&pi_desc->control);
924 }
925
pi_set_sn(struct pi_desc * pi_desc)926 static inline void pi_set_sn(struct pi_desc *pi_desc)
927 {
928 return set_bit(POSTED_INTR_SN,
929 (unsigned long *)&pi_desc->control);
930 }
931
pi_clear_on(struct pi_desc * pi_desc)932 static inline void pi_clear_on(struct pi_desc *pi_desc)
933 {
934 clear_bit(POSTED_INTR_ON,
935 (unsigned long *)&pi_desc->control);
936 }
937
pi_test_on(struct pi_desc * pi_desc)938 static inline int pi_test_on(struct pi_desc *pi_desc)
939 {
940 return test_bit(POSTED_INTR_ON,
941 (unsigned long *)&pi_desc->control);
942 }
943
pi_test_sn(struct pi_desc * pi_desc)944 static inline int pi_test_sn(struct pi_desc *pi_desc)
945 {
946 return test_bit(POSTED_INTR_SN,
947 (unsigned long *)&pi_desc->control);
948 }
949
950 struct vmx_msrs {
951 unsigned int nr;
952 struct vmx_msr_entry val[NR_AUTOLOAD_MSRS];
953 };
954
955 struct vcpu_vmx {
956 struct kvm_vcpu vcpu;
957 unsigned long host_rsp;
958 u8 fail;
959 u8 msr_bitmap_mode;
960 u32 exit_intr_info;
961 u32 idt_vectoring_info;
962 ulong rflags;
963 struct shared_msr_entry *guest_msrs;
964 int nmsrs;
965 int save_nmsrs;
966 bool guest_msrs_dirty;
967 unsigned long host_idt_base;
968 #ifdef CONFIG_X86_64
969 u64 msr_host_kernel_gs_base;
970 u64 msr_guest_kernel_gs_base;
971 #endif
972
973 u64 spec_ctrl;
974
975 u32 vm_entry_controls_shadow;
976 u32 vm_exit_controls_shadow;
977 u32 secondary_exec_control;
978
979 /*
980 * loaded_vmcs points to the VMCS currently used in this vcpu. For a
981 * non-nested (L1) guest, it always points to vmcs01. For a nested
982 * guest (L2), it points to a different VMCS. loaded_cpu_state points
983 * to the VMCS whose state is loaded into the CPU registers that only
984 * need to be switched when transitioning to/from the kernel; a NULL
985 * value indicates that host state is loaded.
986 */
987 struct loaded_vmcs vmcs01;
988 struct loaded_vmcs *loaded_vmcs;
989 struct loaded_vmcs *loaded_cpu_state;
990 bool __launched; /* temporary, used in vmx_vcpu_run */
991 struct msr_autoload {
992 struct vmx_msrs guest;
993 struct vmx_msrs host;
994 } msr_autoload;
995
996 struct {
997 int vm86_active;
998 ulong save_rflags;
999 struct kvm_segment segs[8];
1000 } rmode;
1001 struct {
1002 u32 bitmask; /* 4 bits per segment (1 bit per field) */
1003 struct kvm_save_segment {
1004 u16 selector;
1005 unsigned long base;
1006 u32 limit;
1007 u32 ar;
1008 } seg[8];
1009 } segment_cache;
1010 int vpid;
1011 bool emulation_required;
1012
1013 u32 exit_reason;
1014
1015 /* Posted interrupt descriptor */
1016 struct pi_desc pi_desc;
1017
1018 /* Support for a guest hypervisor (nested VMX) */
1019 struct nested_vmx nested;
1020
1021 /* Dynamic PLE window. */
1022 int ple_window;
1023 bool ple_window_dirty;
1024
1025 bool req_immediate_exit;
1026
1027 /* Support for PML */
1028 #define PML_ENTITY_NUM 512
1029 struct page *pml_pg;
1030
1031 /* apic deadline value in host tsc */
1032 u64 hv_deadline_tsc;
1033
1034 u64 current_tsc_ratio;
1035
1036 u32 host_pkru;
1037
1038 unsigned long host_debugctlmsr;
1039
1040 /*
1041 * Only bits masked by msr_ia32_feature_control_valid_bits can be set in
1042 * msr_ia32_feature_control. FEATURE_CONTROL_LOCKED is always included
1043 * in msr_ia32_feature_control_valid_bits.
1044 */
1045 u64 msr_ia32_feature_control;
1046 u64 msr_ia32_feature_control_valid_bits;
1047 u64 ept_pointer;
1048 };
1049
1050 enum segment_cache_field {
1051 SEG_FIELD_SEL = 0,
1052 SEG_FIELD_BASE = 1,
1053 SEG_FIELD_LIMIT = 2,
1054 SEG_FIELD_AR = 3,
1055
1056 SEG_FIELD_NR = 4
1057 };
1058
to_kvm_vmx(struct kvm * kvm)1059 static inline struct kvm_vmx *to_kvm_vmx(struct kvm *kvm)
1060 {
1061 return container_of(kvm, struct kvm_vmx, kvm);
1062 }
1063
to_vmx(struct kvm_vcpu * vcpu)1064 static inline struct vcpu_vmx *to_vmx(struct kvm_vcpu *vcpu)
1065 {
1066 return container_of(vcpu, struct vcpu_vmx, vcpu);
1067 }
1068
vcpu_to_pi_desc(struct kvm_vcpu * vcpu)1069 static struct pi_desc *vcpu_to_pi_desc(struct kvm_vcpu *vcpu)
1070 {
1071 return &(to_vmx(vcpu)->pi_desc);
1072 }
1073
1074 #define ROL16(val, n) ((u16)(((u16)(val) << (n)) | ((u16)(val) >> (16 - (n)))))
1075 #define VMCS12_OFFSET(x) offsetof(struct vmcs12, x)
1076 #define FIELD(number, name) [ROL16(number, 6)] = VMCS12_OFFSET(name)
1077 #define FIELD64(number, name) \
1078 FIELD(number, name), \
1079 [ROL16(number##_HIGH, 6)] = VMCS12_OFFSET(name) + sizeof(u32)
1080
1081
1082 static u16 shadow_read_only_fields[] = {
1083 #define SHADOW_FIELD_RO(x) x,
1084 #include "vmx_shadow_fields.h"
1085 };
1086 static int max_shadow_read_only_fields =
1087 ARRAY_SIZE(shadow_read_only_fields);
1088
1089 static u16 shadow_read_write_fields[] = {
1090 #define SHADOW_FIELD_RW(x) x,
1091 #include "vmx_shadow_fields.h"
1092 };
1093 static int max_shadow_read_write_fields =
1094 ARRAY_SIZE(shadow_read_write_fields);
1095
1096 static const unsigned short vmcs_field_to_offset_table[] = {
1097 FIELD(VIRTUAL_PROCESSOR_ID, virtual_processor_id),
1098 FIELD(POSTED_INTR_NV, posted_intr_nv),
1099 FIELD(GUEST_ES_SELECTOR, guest_es_selector),
1100 FIELD(GUEST_CS_SELECTOR, guest_cs_selector),
1101 FIELD(GUEST_SS_SELECTOR, guest_ss_selector),
1102 FIELD(GUEST_DS_SELECTOR, guest_ds_selector),
1103 FIELD(GUEST_FS_SELECTOR, guest_fs_selector),
1104 FIELD(GUEST_GS_SELECTOR, guest_gs_selector),
1105 FIELD(GUEST_LDTR_SELECTOR, guest_ldtr_selector),
1106 FIELD(GUEST_TR_SELECTOR, guest_tr_selector),
1107 FIELD(GUEST_INTR_STATUS, guest_intr_status),
1108 FIELD(GUEST_PML_INDEX, guest_pml_index),
1109 FIELD(HOST_ES_SELECTOR, host_es_selector),
1110 FIELD(HOST_CS_SELECTOR, host_cs_selector),
1111 FIELD(HOST_SS_SELECTOR, host_ss_selector),
1112 FIELD(HOST_DS_SELECTOR, host_ds_selector),
1113 FIELD(HOST_FS_SELECTOR, host_fs_selector),
1114 FIELD(HOST_GS_SELECTOR, host_gs_selector),
1115 FIELD(HOST_TR_SELECTOR, host_tr_selector),
1116 FIELD64(IO_BITMAP_A, io_bitmap_a),
1117 FIELD64(IO_BITMAP_B, io_bitmap_b),
1118 FIELD64(MSR_BITMAP, msr_bitmap),
1119 FIELD64(VM_EXIT_MSR_STORE_ADDR, vm_exit_msr_store_addr),
1120 FIELD64(VM_EXIT_MSR_LOAD_ADDR, vm_exit_msr_load_addr),
1121 FIELD64(VM_ENTRY_MSR_LOAD_ADDR, vm_entry_msr_load_addr),
1122 FIELD64(PML_ADDRESS, pml_address),
1123 FIELD64(TSC_OFFSET, tsc_offset),
1124 FIELD64(VIRTUAL_APIC_PAGE_ADDR, virtual_apic_page_addr),
1125 FIELD64(APIC_ACCESS_ADDR, apic_access_addr),
1126 FIELD64(POSTED_INTR_DESC_ADDR, posted_intr_desc_addr),
1127 FIELD64(VM_FUNCTION_CONTROL, vm_function_control),
1128 FIELD64(EPT_POINTER, ept_pointer),
1129 FIELD64(EOI_EXIT_BITMAP0, eoi_exit_bitmap0),
1130 FIELD64(EOI_EXIT_BITMAP1, eoi_exit_bitmap1),
1131 FIELD64(EOI_EXIT_BITMAP2, eoi_exit_bitmap2),
1132 FIELD64(EOI_EXIT_BITMAP3, eoi_exit_bitmap3),
1133 FIELD64(EPTP_LIST_ADDRESS, eptp_list_address),
1134 FIELD64(VMREAD_BITMAP, vmread_bitmap),
1135 FIELD64(VMWRITE_BITMAP, vmwrite_bitmap),
1136 FIELD64(XSS_EXIT_BITMAP, xss_exit_bitmap),
1137 FIELD64(GUEST_PHYSICAL_ADDRESS, guest_physical_address),
1138 FIELD64(VMCS_LINK_POINTER, vmcs_link_pointer),
1139 FIELD64(GUEST_IA32_DEBUGCTL, guest_ia32_debugctl),
1140 FIELD64(GUEST_IA32_PAT, guest_ia32_pat),
1141 FIELD64(GUEST_IA32_EFER, guest_ia32_efer),
1142 FIELD64(GUEST_IA32_PERF_GLOBAL_CTRL, guest_ia32_perf_global_ctrl),
1143 FIELD64(GUEST_PDPTR0, guest_pdptr0),
1144 FIELD64(GUEST_PDPTR1, guest_pdptr1),
1145 FIELD64(GUEST_PDPTR2, guest_pdptr2),
1146 FIELD64(GUEST_PDPTR3, guest_pdptr3),
1147 FIELD64(GUEST_BNDCFGS, guest_bndcfgs),
1148 FIELD64(HOST_IA32_PAT, host_ia32_pat),
1149 FIELD64(HOST_IA32_EFER, host_ia32_efer),
1150 FIELD64(HOST_IA32_PERF_GLOBAL_CTRL, host_ia32_perf_global_ctrl),
1151 FIELD(PIN_BASED_VM_EXEC_CONTROL, pin_based_vm_exec_control),
1152 FIELD(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control),
1153 FIELD(EXCEPTION_BITMAP, exception_bitmap),
1154 FIELD(PAGE_FAULT_ERROR_CODE_MASK, page_fault_error_code_mask),
1155 FIELD(PAGE_FAULT_ERROR_CODE_MATCH, page_fault_error_code_match),
1156 FIELD(CR3_TARGET_COUNT, cr3_target_count),
1157 FIELD(VM_EXIT_CONTROLS, vm_exit_controls),
1158 FIELD(VM_EXIT_MSR_STORE_COUNT, vm_exit_msr_store_count),
1159 FIELD(VM_EXIT_MSR_LOAD_COUNT, vm_exit_msr_load_count),
1160 FIELD(VM_ENTRY_CONTROLS, vm_entry_controls),
1161 FIELD(VM_ENTRY_MSR_LOAD_COUNT, vm_entry_msr_load_count),
1162 FIELD(VM_ENTRY_INTR_INFO_FIELD, vm_entry_intr_info_field),
1163 FIELD(VM_ENTRY_EXCEPTION_ERROR_CODE, vm_entry_exception_error_code),
1164 FIELD(VM_ENTRY_INSTRUCTION_LEN, vm_entry_instruction_len),
1165 FIELD(TPR_THRESHOLD, tpr_threshold),
1166 FIELD(SECONDARY_VM_EXEC_CONTROL, secondary_vm_exec_control),
1167 FIELD(VM_INSTRUCTION_ERROR, vm_instruction_error),
1168 FIELD(VM_EXIT_REASON, vm_exit_reason),
1169 FIELD(VM_EXIT_INTR_INFO, vm_exit_intr_info),
1170 FIELD(VM_EXIT_INTR_ERROR_CODE, vm_exit_intr_error_code),
1171 FIELD(IDT_VECTORING_INFO_FIELD, idt_vectoring_info_field),
1172 FIELD(IDT_VECTORING_ERROR_CODE, idt_vectoring_error_code),
1173 FIELD(VM_EXIT_INSTRUCTION_LEN, vm_exit_instruction_len),
1174 FIELD(VMX_INSTRUCTION_INFO, vmx_instruction_info),
1175 FIELD(GUEST_ES_LIMIT, guest_es_limit),
1176 FIELD(GUEST_CS_LIMIT, guest_cs_limit),
1177 FIELD(GUEST_SS_LIMIT, guest_ss_limit),
1178 FIELD(GUEST_DS_LIMIT, guest_ds_limit),
1179 FIELD(GUEST_FS_LIMIT, guest_fs_limit),
1180 FIELD(GUEST_GS_LIMIT, guest_gs_limit),
1181 FIELD(GUEST_LDTR_LIMIT, guest_ldtr_limit),
1182 FIELD(GUEST_TR_LIMIT, guest_tr_limit),
1183 FIELD(GUEST_GDTR_LIMIT, guest_gdtr_limit),
1184 FIELD(GUEST_IDTR_LIMIT, guest_idtr_limit),
1185 FIELD(GUEST_ES_AR_BYTES, guest_es_ar_bytes),
1186 FIELD(GUEST_CS_AR_BYTES, guest_cs_ar_bytes),
1187 FIELD(GUEST_SS_AR_BYTES, guest_ss_ar_bytes),
1188 FIELD(GUEST_DS_AR_BYTES, guest_ds_ar_bytes),
1189 FIELD(GUEST_FS_AR_BYTES, guest_fs_ar_bytes),
1190 FIELD(GUEST_GS_AR_BYTES, guest_gs_ar_bytes),
1191 FIELD(GUEST_LDTR_AR_BYTES, guest_ldtr_ar_bytes),
1192 FIELD(GUEST_TR_AR_BYTES, guest_tr_ar_bytes),
1193 FIELD(GUEST_INTERRUPTIBILITY_INFO, guest_interruptibility_info),
1194 FIELD(GUEST_ACTIVITY_STATE, guest_activity_state),
1195 FIELD(GUEST_SYSENTER_CS, guest_sysenter_cs),
1196 FIELD(HOST_IA32_SYSENTER_CS, host_ia32_sysenter_cs),
1197 FIELD(VMX_PREEMPTION_TIMER_VALUE, vmx_preemption_timer_value),
1198 FIELD(CR0_GUEST_HOST_MASK, cr0_guest_host_mask),
1199 FIELD(CR4_GUEST_HOST_MASK, cr4_guest_host_mask),
1200 FIELD(CR0_READ_SHADOW, cr0_read_shadow),
1201 FIELD(CR4_READ_SHADOW, cr4_read_shadow),
1202 FIELD(CR3_TARGET_VALUE0, cr3_target_value0),
1203 FIELD(CR3_TARGET_VALUE1, cr3_target_value1),
1204 FIELD(CR3_TARGET_VALUE2, cr3_target_value2),
1205 FIELD(CR3_TARGET_VALUE3, cr3_target_value3),
1206 FIELD(EXIT_QUALIFICATION, exit_qualification),
1207 FIELD(GUEST_LINEAR_ADDRESS, guest_linear_address),
1208 FIELD(GUEST_CR0, guest_cr0),
1209 FIELD(GUEST_CR3, guest_cr3),
1210 FIELD(GUEST_CR4, guest_cr4),
1211 FIELD(GUEST_ES_BASE, guest_es_base),
1212 FIELD(GUEST_CS_BASE, guest_cs_base),
1213 FIELD(GUEST_SS_BASE, guest_ss_base),
1214 FIELD(GUEST_DS_BASE, guest_ds_base),
1215 FIELD(GUEST_FS_BASE, guest_fs_base),
1216 FIELD(GUEST_GS_BASE, guest_gs_base),
1217 FIELD(GUEST_LDTR_BASE, guest_ldtr_base),
1218 FIELD(GUEST_TR_BASE, guest_tr_base),
1219 FIELD(GUEST_GDTR_BASE, guest_gdtr_base),
1220 FIELD(GUEST_IDTR_BASE, guest_idtr_base),
1221 FIELD(GUEST_DR7, guest_dr7),
1222 FIELD(GUEST_RSP, guest_rsp),
1223 FIELD(GUEST_RIP, guest_rip),
1224 FIELD(GUEST_RFLAGS, guest_rflags),
1225 FIELD(GUEST_PENDING_DBG_EXCEPTIONS, guest_pending_dbg_exceptions),
1226 FIELD(GUEST_SYSENTER_ESP, guest_sysenter_esp),
1227 FIELD(GUEST_SYSENTER_EIP, guest_sysenter_eip),
1228 FIELD(HOST_CR0, host_cr0),
1229 FIELD(HOST_CR3, host_cr3),
1230 FIELD(HOST_CR4, host_cr4),
1231 FIELD(HOST_FS_BASE, host_fs_base),
1232 FIELD(HOST_GS_BASE, host_gs_base),
1233 FIELD(HOST_TR_BASE, host_tr_base),
1234 FIELD(HOST_GDTR_BASE, host_gdtr_base),
1235 FIELD(HOST_IDTR_BASE, host_idtr_base),
1236 FIELD(HOST_IA32_SYSENTER_ESP, host_ia32_sysenter_esp),
1237 FIELD(HOST_IA32_SYSENTER_EIP, host_ia32_sysenter_eip),
1238 FIELD(HOST_RSP, host_rsp),
1239 FIELD(HOST_RIP, host_rip),
1240 };
1241
vmcs_field_to_offset(unsigned long field)1242 static inline short vmcs_field_to_offset(unsigned long field)
1243 {
1244 const size_t size = ARRAY_SIZE(vmcs_field_to_offset_table);
1245 unsigned short offset;
1246 unsigned index;
1247
1248 if (field >> 15)
1249 return -ENOENT;
1250
1251 index = ROL16(field, 6);
1252 if (index >= size)
1253 return -ENOENT;
1254
1255 index = array_index_nospec(index, size);
1256 offset = vmcs_field_to_offset_table[index];
1257 if (offset == 0)
1258 return -ENOENT;
1259 return offset;
1260 }
1261
get_vmcs12(struct kvm_vcpu * vcpu)1262 static inline struct vmcs12 *get_vmcs12(struct kvm_vcpu *vcpu)
1263 {
1264 return to_vmx(vcpu)->nested.cached_vmcs12;
1265 }
1266
get_shadow_vmcs12(struct kvm_vcpu * vcpu)1267 static inline struct vmcs12 *get_shadow_vmcs12(struct kvm_vcpu *vcpu)
1268 {
1269 return to_vmx(vcpu)->nested.cached_shadow_vmcs12;
1270 }
1271
1272 static bool nested_ept_ad_enabled(struct kvm_vcpu *vcpu);
1273 static unsigned long nested_ept_get_cr3(struct kvm_vcpu *vcpu);
1274 static u64 construct_eptp(struct kvm_vcpu *vcpu, unsigned long root_hpa);
1275 static bool vmx_xsaves_supported(void);
1276 static void vmx_set_segment(struct kvm_vcpu *vcpu,
1277 struct kvm_segment *var, int seg);
1278 static void vmx_get_segment(struct kvm_vcpu *vcpu,
1279 struct kvm_segment *var, int seg);
1280 static bool guest_state_valid(struct kvm_vcpu *vcpu);
1281 static u32 vmx_segment_access_rights(struct kvm_segment *var);
1282 static void copy_shadow_to_vmcs12(struct vcpu_vmx *vmx);
1283 static bool vmx_get_nmi_mask(struct kvm_vcpu *vcpu);
1284 static void vmx_set_nmi_mask(struct kvm_vcpu *vcpu, bool masked);
1285 static bool nested_vmx_is_page_fault_vmexit(struct vmcs12 *vmcs12,
1286 u16 error_code);
1287 static void vmx_update_msr_bitmap(struct kvm_vcpu *vcpu);
1288 static __always_inline void vmx_disable_intercept_for_msr(unsigned long *msr_bitmap,
1289 u32 msr, int type);
1290
1291 static DEFINE_PER_CPU(struct vmcs *, vmxarea);
1292 static DEFINE_PER_CPU(struct vmcs *, current_vmcs);
1293 /*
1294 * We maintain a per-CPU linked-list of VMCS loaded on that CPU. This is needed
1295 * when a CPU is brought down, and we need to VMCLEAR all VMCSs loaded on it.
1296 */
1297 static DEFINE_PER_CPU(struct list_head, loaded_vmcss_on_cpu);
1298
1299 /*
1300 * We maintian a per-CPU linked-list of vCPU, so in wakeup_handler() we
1301 * can find which vCPU should be waken up.
1302 */
1303 static DEFINE_PER_CPU(struct list_head, blocked_vcpu_on_cpu);
1304 static DEFINE_PER_CPU(spinlock_t, blocked_vcpu_on_cpu_lock);
1305
1306 enum {
1307 VMX_VMREAD_BITMAP,
1308 VMX_VMWRITE_BITMAP,
1309 VMX_BITMAP_NR
1310 };
1311
1312 static unsigned long *vmx_bitmap[VMX_BITMAP_NR];
1313
1314 #define vmx_vmread_bitmap (vmx_bitmap[VMX_VMREAD_BITMAP])
1315 #define vmx_vmwrite_bitmap (vmx_bitmap[VMX_VMWRITE_BITMAP])
1316
1317 static bool cpu_has_load_ia32_efer;
1318 static bool cpu_has_load_perf_global_ctrl;
1319
1320 static DECLARE_BITMAP(vmx_vpid_bitmap, VMX_NR_VPIDS);
1321 static DEFINE_SPINLOCK(vmx_vpid_lock);
1322
1323 static struct vmcs_config {
1324 int size;
1325 int order;
1326 u32 basic_cap;
1327 u32 revision_id;
1328 u32 pin_based_exec_ctrl;
1329 u32 cpu_based_exec_ctrl;
1330 u32 cpu_based_2nd_exec_ctrl;
1331 u32 vmexit_ctrl;
1332 u32 vmentry_ctrl;
1333 struct nested_vmx_msrs nested;
1334 } vmcs_config;
1335
1336 static struct vmx_capability {
1337 u32 ept;
1338 u32 vpid;
1339 } vmx_capability;
1340
1341 #define VMX_SEGMENT_FIELD(seg) \
1342 [VCPU_SREG_##seg] = { \
1343 .selector = GUEST_##seg##_SELECTOR, \
1344 .base = GUEST_##seg##_BASE, \
1345 .limit = GUEST_##seg##_LIMIT, \
1346 .ar_bytes = GUEST_##seg##_AR_BYTES, \
1347 }
1348
1349 static const struct kvm_vmx_segment_field {
1350 unsigned selector;
1351 unsigned base;
1352 unsigned limit;
1353 unsigned ar_bytes;
1354 } kvm_vmx_segment_fields[] = {
1355 VMX_SEGMENT_FIELD(CS),
1356 VMX_SEGMENT_FIELD(DS),
1357 VMX_SEGMENT_FIELD(ES),
1358 VMX_SEGMENT_FIELD(FS),
1359 VMX_SEGMENT_FIELD(GS),
1360 VMX_SEGMENT_FIELD(SS),
1361 VMX_SEGMENT_FIELD(TR),
1362 VMX_SEGMENT_FIELD(LDTR),
1363 };
1364
1365 static u64 host_efer;
1366
1367 static void ept_save_pdptrs(struct kvm_vcpu *vcpu);
1368
1369 /*
1370 * Keep MSR_STAR at the end, as setup_msrs() will try to optimize it
1371 * away by decrementing the array size.
1372 */
1373 static const u32 vmx_msr_index[] = {
1374 #ifdef CONFIG_X86_64
1375 MSR_SYSCALL_MASK, MSR_LSTAR, MSR_CSTAR,
1376 #endif
1377 MSR_EFER, MSR_TSC_AUX, MSR_STAR,
1378 };
1379
1380 DEFINE_STATIC_KEY_FALSE(enable_evmcs);
1381
1382 #define current_evmcs ((struct hv_enlightened_vmcs *)this_cpu_read(current_vmcs))
1383
1384 #define KVM_EVMCS_VERSION 1
1385
1386 #if IS_ENABLED(CONFIG_HYPERV)
1387 static bool __read_mostly enlightened_vmcs = true;
1388 module_param(enlightened_vmcs, bool, 0444);
1389
evmcs_write64(unsigned long field,u64 value)1390 static inline void evmcs_write64(unsigned long field, u64 value)
1391 {
1392 u16 clean_field;
1393 int offset = get_evmcs_offset(field, &clean_field);
1394
1395 if (offset < 0)
1396 return;
1397
1398 *(u64 *)((char *)current_evmcs + offset) = value;
1399
1400 current_evmcs->hv_clean_fields &= ~clean_field;
1401 }
1402
evmcs_write32(unsigned long field,u32 value)1403 static inline void evmcs_write32(unsigned long field, u32 value)
1404 {
1405 u16 clean_field;
1406 int offset = get_evmcs_offset(field, &clean_field);
1407
1408 if (offset < 0)
1409 return;
1410
1411 *(u32 *)((char *)current_evmcs + offset) = value;
1412 current_evmcs->hv_clean_fields &= ~clean_field;
1413 }
1414
evmcs_write16(unsigned long field,u16 value)1415 static inline void evmcs_write16(unsigned long field, u16 value)
1416 {
1417 u16 clean_field;
1418 int offset = get_evmcs_offset(field, &clean_field);
1419
1420 if (offset < 0)
1421 return;
1422
1423 *(u16 *)((char *)current_evmcs + offset) = value;
1424 current_evmcs->hv_clean_fields &= ~clean_field;
1425 }
1426
evmcs_read64(unsigned long field)1427 static inline u64 evmcs_read64(unsigned long field)
1428 {
1429 int offset = get_evmcs_offset(field, NULL);
1430
1431 if (offset < 0)
1432 return 0;
1433
1434 return *(u64 *)((char *)current_evmcs + offset);
1435 }
1436
evmcs_read32(unsigned long field)1437 static inline u32 evmcs_read32(unsigned long field)
1438 {
1439 int offset = get_evmcs_offset(field, NULL);
1440
1441 if (offset < 0)
1442 return 0;
1443
1444 return *(u32 *)((char *)current_evmcs + offset);
1445 }
1446
evmcs_read16(unsigned long field)1447 static inline u16 evmcs_read16(unsigned long field)
1448 {
1449 int offset = get_evmcs_offset(field, NULL);
1450
1451 if (offset < 0)
1452 return 0;
1453
1454 return *(u16 *)((char *)current_evmcs + offset);
1455 }
1456
evmcs_touch_msr_bitmap(void)1457 static inline void evmcs_touch_msr_bitmap(void)
1458 {
1459 if (unlikely(!current_evmcs))
1460 return;
1461
1462 if (current_evmcs->hv_enlightenments_control.msr_bitmap)
1463 current_evmcs->hv_clean_fields &=
1464 ~HV_VMX_ENLIGHTENED_CLEAN_FIELD_MSR_BITMAP;
1465 }
1466
evmcs_load(u64 phys_addr)1467 static void evmcs_load(u64 phys_addr)
1468 {
1469 struct hv_vp_assist_page *vp_ap =
1470 hv_get_vp_assist_page(smp_processor_id());
1471
1472 vp_ap->current_nested_vmcs = phys_addr;
1473 vp_ap->enlighten_vmentry = 1;
1474 }
1475
evmcs_sanitize_exec_ctrls(struct vmcs_config * vmcs_conf)1476 static void evmcs_sanitize_exec_ctrls(struct vmcs_config *vmcs_conf)
1477 {
1478 /*
1479 * Enlightened VMCSv1 doesn't support these:
1480 *
1481 * POSTED_INTR_NV = 0x00000002,
1482 * GUEST_INTR_STATUS = 0x00000810,
1483 * APIC_ACCESS_ADDR = 0x00002014,
1484 * POSTED_INTR_DESC_ADDR = 0x00002016,
1485 * EOI_EXIT_BITMAP0 = 0x0000201c,
1486 * EOI_EXIT_BITMAP1 = 0x0000201e,
1487 * EOI_EXIT_BITMAP2 = 0x00002020,
1488 * EOI_EXIT_BITMAP3 = 0x00002022,
1489 */
1490 vmcs_conf->pin_based_exec_ctrl &= ~PIN_BASED_POSTED_INTR;
1491 vmcs_conf->cpu_based_2nd_exec_ctrl &=
1492 ~SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY;
1493 vmcs_conf->cpu_based_2nd_exec_ctrl &=
1494 ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
1495 vmcs_conf->cpu_based_2nd_exec_ctrl &=
1496 ~SECONDARY_EXEC_APIC_REGISTER_VIRT;
1497
1498 /*
1499 * GUEST_PML_INDEX = 0x00000812,
1500 * PML_ADDRESS = 0x0000200e,
1501 */
1502 vmcs_conf->cpu_based_2nd_exec_ctrl &= ~SECONDARY_EXEC_ENABLE_PML;
1503
1504 /* VM_FUNCTION_CONTROL = 0x00002018, */
1505 vmcs_conf->cpu_based_2nd_exec_ctrl &= ~SECONDARY_EXEC_ENABLE_VMFUNC;
1506
1507 /*
1508 * EPTP_LIST_ADDRESS = 0x00002024,
1509 * VMREAD_BITMAP = 0x00002026,
1510 * VMWRITE_BITMAP = 0x00002028,
1511 */
1512 vmcs_conf->cpu_based_2nd_exec_ctrl &= ~SECONDARY_EXEC_SHADOW_VMCS;
1513
1514 /*
1515 * TSC_MULTIPLIER = 0x00002032,
1516 */
1517 vmcs_conf->cpu_based_2nd_exec_ctrl &= ~SECONDARY_EXEC_TSC_SCALING;
1518
1519 /*
1520 * PLE_GAP = 0x00004020,
1521 * PLE_WINDOW = 0x00004022,
1522 */
1523 vmcs_conf->cpu_based_2nd_exec_ctrl &= ~SECONDARY_EXEC_PAUSE_LOOP_EXITING;
1524
1525 /*
1526 * VMX_PREEMPTION_TIMER_VALUE = 0x0000482E,
1527 */
1528 vmcs_conf->pin_based_exec_ctrl &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
1529
1530 /*
1531 * GUEST_IA32_PERF_GLOBAL_CTRL = 0x00002808,
1532 * HOST_IA32_PERF_GLOBAL_CTRL = 0x00002c04,
1533 */
1534 vmcs_conf->vmexit_ctrl &= ~VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL;
1535 vmcs_conf->vmentry_ctrl &= ~VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL;
1536
1537 /*
1538 * Currently unsupported in KVM:
1539 * GUEST_IA32_RTIT_CTL = 0x00002814,
1540 */
1541 }
1542
1543 /* check_ept_pointer() should be under protection of ept_pointer_lock. */
check_ept_pointer_match(struct kvm * kvm)1544 static void check_ept_pointer_match(struct kvm *kvm)
1545 {
1546 struct kvm_vcpu *vcpu;
1547 u64 tmp_eptp = INVALID_PAGE;
1548 int i;
1549
1550 kvm_for_each_vcpu(i, vcpu, kvm) {
1551 if (!VALID_PAGE(tmp_eptp)) {
1552 tmp_eptp = to_vmx(vcpu)->ept_pointer;
1553 } else if (tmp_eptp != to_vmx(vcpu)->ept_pointer) {
1554 to_kvm_vmx(kvm)->ept_pointers_match
1555 = EPT_POINTERS_MISMATCH;
1556 return;
1557 }
1558 }
1559
1560 to_kvm_vmx(kvm)->ept_pointers_match = EPT_POINTERS_MATCH;
1561 }
1562
vmx_hv_remote_flush_tlb(struct kvm * kvm)1563 static int vmx_hv_remote_flush_tlb(struct kvm *kvm)
1564 {
1565 int ret;
1566
1567 spin_lock(&to_kvm_vmx(kvm)->ept_pointer_lock);
1568
1569 if (to_kvm_vmx(kvm)->ept_pointers_match == EPT_POINTERS_CHECK)
1570 check_ept_pointer_match(kvm);
1571
1572 if (to_kvm_vmx(kvm)->ept_pointers_match != EPT_POINTERS_MATCH) {
1573 ret = -ENOTSUPP;
1574 goto out;
1575 }
1576
1577 /*
1578 * FLUSH_GUEST_PHYSICAL_ADDRESS_SPACE hypercall needs the address of the
1579 * base of EPT PML4 table, strip off EPT configuration information.
1580 */
1581 ret = hyperv_flush_guest_mapping(
1582 to_vmx(kvm_get_vcpu(kvm, 0))->ept_pointer & PAGE_MASK);
1583
1584 out:
1585 spin_unlock(&to_kvm_vmx(kvm)->ept_pointer_lock);
1586 return ret;
1587 }
1588 #else /* !IS_ENABLED(CONFIG_HYPERV) */
evmcs_write64(unsigned long field,u64 value)1589 static inline void evmcs_write64(unsigned long field, u64 value) {}
evmcs_write32(unsigned long field,u32 value)1590 static inline void evmcs_write32(unsigned long field, u32 value) {}
evmcs_write16(unsigned long field,u16 value)1591 static inline void evmcs_write16(unsigned long field, u16 value) {}
evmcs_read64(unsigned long field)1592 static inline u64 evmcs_read64(unsigned long field) { return 0; }
evmcs_read32(unsigned long field)1593 static inline u32 evmcs_read32(unsigned long field) { return 0; }
evmcs_read16(unsigned long field)1594 static inline u16 evmcs_read16(unsigned long field) { return 0; }
evmcs_load(u64 phys_addr)1595 static inline void evmcs_load(u64 phys_addr) {}
evmcs_sanitize_exec_ctrls(struct vmcs_config * vmcs_conf)1596 static inline void evmcs_sanitize_exec_ctrls(struct vmcs_config *vmcs_conf) {}
evmcs_touch_msr_bitmap(void)1597 static inline void evmcs_touch_msr_bitmap(void) {}
1598 #endif /* IS_ENABLED(CONFIG_HYPERV) */
1599
is_exception_n(u32 intr_info,u8 vector)1600 static inline bool is_exception_n(u32 intr_info, u8 vector)
1601 {
1602 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
1603 INTR_INFO_VALID_MASK)) ==
1604 (INTR_TYPE_HARD_EXCEPTION | vector | INTR_INFO_VALID_MASK);
1605 }
1606
is_debug(u32 intr_info)1607 static inline bool is_debug(u32 intr_info)
1608 {
1609 return is_exception_n(intr_info, DB_VECTOR);
1610 }
1611
is_breakpoint(u32 intr_info)1612 static inline bool is_breakpoint(u32 intr_info)
1613 {
1614 return is_exception_n(intr_info, BP_VECTOR);
1615 }
1616
is_page_fault(u32 intr_info)1617 static inline bool is_page_fault(u32 intr_info)
1618 {
1619 return is_exception_n(intr_info, PF_VECTOR);
1620 }
1621
is_no_device(u32 intr_info)1622 static inline bool is_no_device(u32 intr_info)
1623 {
1624 return is_exception_n(intr_info, NM_VECTOR);
1625 }
1626
is_invalid_opcode(u32 intr_info)1627 static inline bool is_invalid_opcode(u32 intr_info)
1628 {
1629 return is_exception_n(intr_info, UD_VECTOR);
1630 }
1631
is_gp_fault(u32 intr_info)1632 static inline bool is_gp_fault(u32 intr_info)
1633 {
1634 return is_exception_n(intr_info, GP_VECTOR);
1635 }
1636
is_external_interrupt(u32 intr_info)1637 static inline bool is_external_interrupt(u32 intr_info)
1638 {
1639 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK))
1640 == (INTR_TYPE_EXT_INTR | INTR_INFO_VALID_MASK);
1641 }
1642
is_machine_check(u32 intr_info)1643 static inline bool is_machine_check(u32 intr_info)
1644 {
1645 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
1646 INTR_INFO_VALID_MASK)) ==
1647 (INTR_TYPE_HARD_EXCEPTION | MC_VECTOR | INTR_INFO_VALID_MASK);
1648 }
1649
1650 /* Undocumented: icebp/int1 */
is_icebp(u32 intr_info)1651 static inline bool is_icebp(u32 intr_info)
1652 {
1653 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK))
1654 == (INTR_TYPE_PRIV_SW_EXCEPTION | INTR_INFO_VALID_MASK);
1655 }
1656
cpu_has_vmx_msr_bitmap(void)1657 static inline bool cpu_has_vmx_msr_bitmap(void)
1658 {
1659 return vmcs_config.cpu_based_exec_ctrl & CPU_BASED_USE_MSR_BITMAPS;
1660 }
1661
cpu_has_vmx_tpr_shadow(void)1662 static inline bool cpu_has_vmx_tpr_shadow(void)
1663 {
1664 return vmcs_config.cpu_based_exec_ctrl & CPU_BASED_TPR_SHADOW;
1665 }
1666
cpu_need_tpr_shadow(struct kvm_vcpu * vcpu)1667 static inline bool cpu_need_tpr_shadow(struct kvm_vcpu *vcpu)
1668 {
1669 return cpu_has_vmx_tpr_shadow() && lapic_in_kernel(vcpu);
1670 }
1671
cpu_has_secondary_exec_ctrls(void)1672 static inline bool cpu_has_secondary_exec_ctrls(void)
1673 {
1674 return vmcs_config.cpu_based_exec_ctrl &
1675 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
1676 }
1677
cpu_has_vmx_virtualize_apic_accesses(void)1678 static inline bool cpu_has_vmx_virtualize_apic_accesses(void)
1679 {
1680 return vmcs_config.cpu_based_2nd_exec_ctrl &
1681 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
1682 }
1683
cpu_has_vmx_virtualize_x2apic_mode(void)1684 static inline bool cpu_has_vmx_virtualize_x2apic_mode(void)
1685 {
1686 return vmcs_config.cpu_based_2nd_exec_ctrl &
1687 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
1688 }
1689
cpu_has_vmx_apic_register_virt(void)1690 static inline bool cpu_has_vmx_apic_register_virt(void)
1691 {
1692 return vmcs_config.cpu_based_2nd_exec_ctrl &
1693 SECONDARY_EXEC_APIC_REGISTER_VIRT;
1694 }
1695
cpu_has_vmx_virtual_intr_delivery(void)1696 static inline bool cpu_has_vmx_virtual_intr_delivery(void)
1697 {
1698 return vmcs_config.cpu_based_2nd_exec_ctrl &
1699 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY;
1700 }
1701
cpu_has_vmx_encls_vmexit(void)1702 static inline bool cpu_has_vmx_encls_vmexit(void)
1703 {
1704 return vmcs_config.cpu_based_2nd_exec_ctrl &
1705 SECONDARY_EXEC_ENCLS_EXITING;
1706 }
1707
1708 /*
1709 * Comment's format: document - errata name - stepping - processor name.
1710 * Refer from
1711 * https://www.virtualbox.org/svn/vbox/trunk/src/VBox/VMM/VMMR0/HMR0.cpp
1712 */
1713 static u32 vmx_preemption_cpu_tfms[] = {
1714 /* 323344.pdf - BA86 - D0 - Xeon 7500 Series */
1715 0x000206E6,
1716 /* 323056.pdf - AAX65 - C2 - Xeon L3406 */
1717 /* 322814.pdf - AAT59 - C2 - i7-600, i5-500, i5-400 and i3-300 Mobile */
1718 /* 322911.pdf - AAU65 - C2 - i5-600, i3-500 Desktop and Pentium G6950 */
1719 0x00020652,
1720 /* 322911.pdf - AAU65 - K0 - i5-600, i3-500 Desktop and Pentium G6950 */
1721 0x00020655,
1722 /* 322373.pdf - AAO95 - B1 - Xeon 3400 Series */
1723 /* 322166.pdf - AAN92 - B1 - i7-800 and i5-700 Desktop */
1724 /*
1725 * 320767.pdf - AAP86 - B1 -
1726 * i7-900 Mobile Extreme, i7-800 and i7-700 Mobile
1727 */
1728 0x000106E5,
1729 /* 321333.pdf - AAM126 - C0 - Xeon 3500 */
1730 0x000106A0,
1731 /* 321333.pdf - AAM126 - C1 - Xeon 3500 */
1732 0x000106A1,
1733 /* 320836.pdf - AAJ124 - C0 - i7-900 Desktop Extreme and i7-900 Desktop */
1734 0x000106A4,
1735 /* 321333.pdf - AAM126 - D0 - Xeon 3500 */
1736 /* 321324.pdf - AAK139 - D0 - Xeon 5500 */
1737 /* 320836.pdf - AAJ124 - D0 - i7-900 Extreme and i7-900 Desktop */
1738 0x000106A5,
1739 };
1740
cpu_has_broken_vmx_preemption_timer(void)1741 static inline bool cpu_has_broken_vmx_preemption_timer(void)
1742 {
1743 u32 eax = cpuid_eax(0x00000001), i;
1744
1745 /* Clear the reserved bits */
1746 eax &= ~(0x3U << 14 | 0xfU << 28);
1747 for (i = 0; i < ARRAY_SIZE(vmx_preemption_cpu_tfms); i++)
1748 if (eax == vmx_preemption_cpu_tfms[i])
1749 return true;
1750
1751 return false;
1752 }
1753
cpu_has_vmx_preemption_timer(void)1754 static inline bool cpu_has_vmx_preemption_timer(void)
1755 {
1756 return vmcs_config.pin_based_exec_ctrl &
1757 PIN_BASED_VMX_PREEMPTION_TIMER;
1758 }
1759
cpu_has_vmx_posted_intr(void)1760 static inline bool cpu_has_vmx_posted_intr(void)
1761 {
1762 return IS_ENABLED(CONFIG_X86_LOCAL_APIC) &&
1763 vmcs_config.pin_based_exec_ctrl & PIN_BASED_POSTED_INTR;
1764 }
1765
cpu_has_vmx_apicv(void)1766 static inline bool cpu_has_vmx_apicv(void)
1767 {
1768 return cpu_has_vmx_apic_register_virt() &&
1769 cpu_has_vmx_virtual_intr_delivery() &&
1770 cpu_has_vmx_posted_intr();
1771 }
1772
cpu_has_vmx_flexpriority(void)1773 static inline bool cpu_has_vmx_flexpriority(void)
1774 {
1775 return cpu_has_vmx_tpr_shadow() &&
1776 cpu_has_vmx_virtualize_apic_accesses();
1777 }
1778
cpu_has_vmx_ept_execute_only(void)1779 static inline bool cpu_has_vmx_ept_execute_only(void)
1780 {
1781 return vmx_capability.ept & VMX_EPT_EXECUTE_ONLY_BIT;
1782 }
1783
cpu_has_vmx_ept_2m_page(void)1784 static inline bool cpu_has_vmx_ept_2m_page(void)
1785 {
1786 return vmx_capability.ept & VMX_EPT_2MB_PAGE_BIT;
1787 }
1788
cpu_has_vmx_ept_1g_page(void)1789 static inline bool cpu_has_vmx_ept_1g_page(void)
1790 {
1791 return vmx_capability.ept & VMX_EPT_1GB_PAGE_BIT;
1792 }
1793
cpu_has_vmx_ept_4levels(void)1794 static inline bool cpu_has_vmx_ept_4levels(void)
1795 {
1796 return vmx_capability.ept & VMX_EPT_PAGE_WALK_4_BIT;
1797 }
1798
cpu_has_vmx_ept_mt_wb(void)1799 static inline bool cpu_has_vmx_ept_mt_wb(void)
1800 {
1801 return vmx_capability.ept & VMX_EPTP_WB_BIT;
1802 }
1803
cpu_has_vmx_ept_5levels(void)1804 static inline bool cpu_has_vmx_ept_5levels(void)
1805 {
1806 return vmx_capability.ept & VMX_EPT_PAGE_WALK_5_BIT;
1807 }
1808
cpu_has_vmx_ept_ad_bits(void)1809 static inline bool cpu_has_vmx_ept_ad_bits(void)
1810 {
1811 return vmx_capability.ept & VMX_EPT_AD_BIT;
1812 }
1813
cpu_has_vmx_invept_context(void)1814 static inline bool cpu_has_vmx_invept_context(void)
1815 {
1816 return vmx_capability.ept & VMX_EPT_EXTENT_CONTEXT_BIT;
1817 }
1818
cpu_has_vmx_invept_global(void)1819 static inline bool cpu_has_vmx_invept_global(void)
1820 {
1821 return vmx_capability.ept & VMX_EPT_EXTENT_GLOBAL_BIT;
1822 }
1823
cpu_has_vmx_invvpid_individual_addr(void)1824 static inline bool cpu_has_vmx_invvpid_individual_addr(void)
1825 {
1826 return vmx_capability.vpid & VMX_VPID_EXTENT_INDIVIDUAL_ADDR_BIT;
1827 }
1828
cpu_has_vmx_invvpid_single(void)1829 static inline bool cpu_has_vmx_invvpid_single(void)
1830 {
1831 return vmx_capability.vpid & VMX_VPID_EXTENT_SINGLE_CONTEXT_BIT;
1832 }
1833
cpu_has_vmx_invvpid_global(void)1834 static inline bool cpu_has_vmx_invvpid_global(void)
1835 {
1836 return vmx_capability.vpid & VMX_VPID_EXTENT_GLOBAL_CONTEXT_BIT;
1837 }
1838
cpu_has_vmx_invvpid(void)1839 static inline bool cpu_has_vmx_invvpid(void)
1840 {
1841 return vmx_capability.vpid & VMX_VPID_INVVPID_BIT;
1842 }
1843
cpu_has_vmx_ept(void)1844 static inline bool cpu_has_vmx_ept(void)
1845 {
1846 return vmcs_config.cpu_based_2nd_exec_ctrl &
1847 SECONDARY_EXEC_ENABLE_EPT;
1848 }
1849
cpu_has_vmx_unrestricted_guest(void)1850 static inline bool cpu_has_vmx_unrestricted_guest(void)
1851 {
1852 return vmcs_config.cpu_based_2nd_exec_ctrl &
1853 SECONDARY_EXEC_UNRESTRICTED_GUEST;
1854 }
1855
cpu_has_vmx_ple(void)1856 static inline bool cpu_has_vmx_ple(void)
1857 {
1858 return vmcs_config.cpu_based_2nd_exec_ctrl &
1859 SECONDARY_EXEC_PAUSE_LOOP_EXITING;
1860 }
1861
cpu_has_vmx_basic_inout(void)1862 static inline bool cpu_has_vmx_basic_inout(void)
1863 {
1864 return (((u64)vmcs_config.basic_cap << 32) & VMX_BASIC_INOUT);
1865 }
1866
cpu_need_virtualize_apic_accesses(struct kvm_vcpu * vcpu)1867 static inline bool cpu_need_virtualize_apic_accesses(struct kvm_vcpu *vcpu)
1868 {
1869 return flexpriority_enabled && lapic_in_kernel(vcpu);
1870 }
1871
cpu_has_vmx_vpid(void)1872 static inline bool cpu_has_vmx_vpid(void)
1873 {
1874 return vmcs_config.cpu_based_2nd_exec_ctrl &
1875 SECONDARY_EXEC_ENABLE_VPID;
1876 }
1877
cpu_has_vmx_rdtscp(void)1878 static inline bool cpu_has_vmx_rdtscp(void)
1879 {
1880 return vmcs_config.cpu_based_2nd_exec_ctrl &
1881 SECONDARY_EXEC_RDTSCP;
1882 }
1883
cpu_has_vmx_invpcid(void)1884 static inline bool cpu_has_vmx_invpcid(void)
1885 {
1886 return vmcs_config.cpu_based_2nd_exec_ctrl &
1887 SECONDARY_EXEC_ENABLE_INVPCID;
1888 }
1889
cpu_has_virtual_nmis(void)1890 static inline bool cpu_has_virtual_nmis(void)
1891 {
1892 return vmcs_config.pin_based_exec_ctrl & PIN_BASED_VIRTUAL_NMIS;
1893 }
1894
cpu_has_vmx_wbinvd_exit(void)1895 static inline bool cpu_has_vmx_wbinvd_exit(void)
1896 {
1897 return vmcs_config.cpu_based_2nd_exec_ctrl &
1898 SECONDARY_EXEC_WBINVD_EXITING;
1899 }
1900
cpu_has_vmx_shadow_vmcs(void)1901 static inline bool cpu_has_vmx_shadow_vmcs(void)
1902 {
1903 u64 vmx_msr;
1904 rdmsrl(MSR_IA32_VMX_MISC, vmx_msr);
1905 /* check if the cpu supports writing r/o exit information fields */
1906 if (!(vmx_msr & MSR_IA32_VMX_MISC_VMWRITE_SHADOW_RO_FIELDS))
1907 return false;
1908
1909 return vmcs_config.cpu_based_2nd_exec_ctrl &
1910 SECONDARY_EXEC_SHADOW_VMCS;
1911 }
1912
cpu_has_vmx_pml(void)1913 static inline bool cpu_has_vmx_pml(void)
1914 {
1915 return vmcs_config.cpu_based_2nd_exec_ctrl & SECONDARY_EXEC_ENABLE_PML;
1916 }
1917
cpu_has_vmx_tsc_scaling(void)1918 static inline bool cpu_has_vmx_tsc_scaling(void)
1919 {
1920 return vmcs_config.cpu_based_2nd_exec_ctrl &
1921 SECONDARY_EXEC_TSC_SCALING;
1922 }
1923
cpu_has_vmx_vmfunc(void)1924 static inline bool cpu_has_vmx_vmfunc(void)
1925 {
1926 return vmcs_config.cpu_based_2nd_exec_ctrl &
1927 SECONDARY_EXEC_ENABLE_VMFUNC;
1928 }
1929
vmx_umip_emulated(void)1930 static bool vmx_umip_emulated(void)
1931 {
1932 return vmcs_config.cpu_based_2nd_exec_ctrl &
1933 SECONDARY_EXEC_DESC;
1934 }
1935
report_flexpriority(void)1936 static inline bool report_flexpriority(void)
1937 {
1938 return flexpriority_enabled;
1939 }
1940
nested_cpu_vmx_misc_cr3_count(struct kvm_vcpu * vcpu)1941 static inline unsigned nested_cpu_vmx_misc_cr3_count(struct kvm_vcpu *vcpu)
1942 {
1943 return vmx_misc_cr3_count(to_vmx(vcpu)->nested.msrs.misc_low);
1944 }
1945
1946 /*
1947 * Do the virtual VMX capability MSRs specify that L1 can use VMWRITE
1948 * to modify any valid field of the VMCS, or are the VM-exit
1949 * information fields read-only?
1950 */
nested_cpu_has_vmwrite_any_field(struct kvm_vcpu * vcpu)1951 static inline bool nested_cpu_has_vmwrite_any_field(struct kvm_vcpu *vcpu)
1952 {
1953 return to_vmx(vcpu)->nested.msrs.misc_low &
1954 MSR_IA32_VMX_MISC_VMWRITE_SHADOW_RO_FIELDS;
1955 }
1956
nested_cpu_has_zero_length_injection(struct kvm_vcpu * vcpu)1957 static inline bool nested_cpu_has_zero_length_injection(struct kvm_vcpu *vcpu)
1958 {
1959 return to_vmx(vcpu)->nested.msrs.misc_low & VMX_MISC_ZERO_LEN_INS;
1960 }
1961
nested_cpu_supports_monitor_trap_flag(struct kvm_vcpu * vcpu)1962 static inline bool nested_cpu_supports_monitor_trap_flag(struct kvm_vcpu *vcpu)
1963 {
1964 return to_vmx(vcpu)->nested.msrs.procbased_ctls_high &
1965 CPU_BASED_MONITOR_TRAP_FLAG;
1966 }
1967
nested_cpu_has_vmx_shadow_vmcs(struct kvm_vcpu * vcpu)1968 static inline bool nested_cpu_has_vmx_shadow_vmcs(struct kvm_vcpu *vcpu)
1969 {
1970 return to_vmx(vcpu)->nested.msrs.secondary_ctls_high &
1971 SECONDARY_EXEC_SHADOW_VMCS;
1972 }
1973
nested_cpu_has(struct vmcs12 * vmcs12,u32 bit)1974 static inline bool nested_cpu_has(struct vmcs12 *vmcs12, u32 bit)
1975 {
1976 return vmcs12->cpu_based_vm_exec_control & bit;
1977 }
1978
nested_cpu_has2(struct vmcs12 * vmcs12,u32 bit)1979 static inline bool nested_cpu_has2(struct vmcs12 *vmcs12, u32 bit)
1980 {
1981 return (vmcs12->cpu_based_vm_exec_control &
1982 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) &&
1983 (vmcs12->secondary_vm_exec_control & bit);
1984 }
1985
nested_cpu_has_preemption_timer(struct vmcs12 * vmcs12)1986 static inline bool nested_cpu_has_preemption_timer(struct vmcs12 *vmcs12)
1987 {
1988 return vmcs12->pin_based_vm_exec_control &
1989 PIN_BASED_VMX_PREEMPTION_TIMER;
1990 }
1991
nested_cpu_has_nmi_exiting(struct vmcs12 * vmcs12)1992 static inline bool nested_cpu_has_nmi_exiting(struct vmcs12 *vmcs12)
1993 {
1994 return vmcs12->pin_based_vm_exec_control & PIN_BASED_NMI_EXITING;
1995 }
1996
nested_cpu_has_virtual_nmis(struct vmcs12 * vmcs12)1997 static inline bool nested_cpu_has_virtual_nmis(struct vmcs12 *vmcs12)
1998 {
1999 return vmcs12->pin_based_vm_exec_control & PIN_BASED_VIRTUAL_NMIS;
2000 }
2001
nested_cpu_has_ept(struct vmcs12 * vmcs12)2002 static inline int nested_cpu_has_ept(struct vmcs12 *vmcs12)
2003 {
2004 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_EPT);
2005 }
2006
nested_cpu_has_xsaves(struct vmcs12 * vmcs12)2007 static inline bool nested_cpu_has_xsaves(struct vmcs12 *vmcs12)
2008 {
2009 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_XSAVES);
2010 }
2011
nested_cpu_has_pml(struct vmcs12 * vmcs12)2012 static inline bool nested_cpu_has_pml(struct vmcs12 *vmcs12)
2013 {
2014 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_PML);
2015 }
2016
nested_cpu_has_virt_x2apic_mode(struct vmcs12 * vmcs12)2017 static inline bool nested_cpu_has_virt_x2apic_mode(struct vmcs12 *vmcs12)
2018 {
2019 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE);
2020 }
2021
nested_cpu_has_vpid(struct vmcs12 * vmcs12)2022 static inline bool nested_cpu_has_vpid(struct vmcs12 *vmcs12)
2023 {
2024 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_VPID);
2025 }
2026
nested_cpu_has_apic_reg_virt(struct vmcs12 * vmcs12)2027 static inline bool nested_cpu_has_apic_reg_virt(struct vmcs12 *vmcs12)
2028 {
2029 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_APIC_REGISTER_VIRT);
2030 }
2031
nested_cpu_has_vid(struct vmcs12 * vmcs12)2032 static inline bool nested_cpu_has_vid(struct vmcs12 *vmcs12)
2033 {
2034 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
2035 }
2036
nested_cpu_has_posted_intr(struct vmcs12 * vmcs12)2037 static inline bool nested_cpu_has_posted_intr(struct vmcs12 *vmcs12)
2038 {
2039 return vmcs12->pin_based_vm_exec_control & PIN_BASED_POSTED_INTR;
2040 }
2041
nested_cpu_has_vmfunc(struct vmcs12 * vmcs12)2042 static inline bool nested_cpu_has_vmfunc(struct vmcs12 *vmcs12)
2043 {
2044 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_VMFUNC);
2045 }
2046
nested_cpu_has_eptp_switching(struct vmcs12 * vmcs12)2047 static inline bool nested_cpu_has_eptp_switching(struct vmcs12 *vmcs12)
2048 {
2049 return nested_cpu_has_vmfunc(vmcs12) &&
2050 (vmcs12->vm_function_control &
2051 VMX_VMFUNC_EPTP_SWITCHING);
2052 }
2053
nested_cpu_has_shadow_vmcs(struct vmcs12 * vmcs12)2054 static inline bool nested_cpu_has_shadow_vmcs(struct vmcs12 *vmcs12)
2055 {
2056 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_SHADOW_VMCS);
2057 }
2058
is_nmi(u32 intr_info)2059 static inline bool is_nmi(u32 intr_info)
2060 {
2061 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK))
2062 == (INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK);
2063 }
2064
2065 static void nested_vmx_vmexit(struct kvm_vcpu *vcpu, u32 exit_reason,
2066 u32 exit_intr_info,
2067 unsigned long exit_qualification);
2068 static void nested_vmx_entry_failure(struct kvm_vcpu *vcpu,
2069 struct vmcs12 *vmcs12,
2070 u32 reason, unsigned long qualification);
2071
__find_msr_index(struct vcpu_vmx * vmx,u32 msr)2072 static int __find_msr_index(struct vcpu_vmx *vmx, u32 msr)
2073 {
2074 int i;
2075
2076 for (i = 0; i < vmx->nmsrs; ++i)
2077 if (vmx_msr_index[vmx->guest_msrs[i].index] == msr)
2078 return i;
2079 return -1;
2080 }
2081
__invvpid(unsigned long ext,u16 vpid,gva_t gva)2082 static inline void __invvpid(unsigned long ext, u16 vpid, gva_t gva)
2083 {
2084 struct {
2085 u64 vpid : 16;
2086 u64 rsvd : 48;
2087 u64 gva;
2088 } operand = { vpid, 0, gva };
2089 bool error;
2090
2091 asm volatile (__ex(ASM_VMX_INVVPID) CC_SET(na)
2092 : CC_OUT(na) (error) : "a"(&operand), "c"(ext)
2093 : "memory");
2094 BUG_ON(error);
2095 }
2096
__invept(unsigned long ext,u64 eptp,gpa_t gpa)2097 static inline void __invept(unsigned long ext, u64 eptp, gpa_t gpa)
2098 {
2099 struct {
2100 u64 eptp, gpa;
2101 } operand = {eptp, gpa};
2102 bool error;
2103
2104 asm volatile (__ex(ASM_VMX_INVEPT) CC_SET(na)
2105 : CC_OUT(na) (error) : "a" (&operand), "c" (ext)
2106 : "memory");
2107 BUG_ON(error);
2108 }
2109
find_msr_entry(struct vcpu_vmx * vmx,u32 msr)2110 static struct shared_msr_entry *find_msr_entry(struct vcpu_vmx *vmx, u32 msr)
2111 {
2112 int i;
2113
2114 i = __find_msr_index(vmx, msr);
2115 if (i >= 0)
2116 return &vmx->guest_msrs[i];
2117 return NULL;
2118 }
2119
vmcs_clear(struct vmcs * vmcs)2120 static void vmcs_clear(struct vmcs *vmcs)
2121 {
2122 u64 phys_addr = __pa(vmcs);
2123 bool error;
2124
2125 asm volatile (__ex(ASM_VMX_VMCLEAR_RAX) CC_SET(na)
2126 : CC_OUT(na) (error) : "a"(&phys_addr), "m"(phys_addr)
2127 : "memory");
2128 if (unlikely(error))
2129 printk(KERN_ERR "kvm: vmclear fail: %p/%llx\n",
2130 vmcs, phys_addr);
2131 }
2132
loaded_vmcs_init(struct loaded_vmcs * loaded_vmcs)2133 static inline void loaded_vmcs_init(struct loaded_vmcs *loaded_vmcs)
2134 {
2135 vmcs_clear(loaded_vmcs->vmcs);
2136 if (loaded_vmcs->shadow_vmcs && loaded_vmcs->launched)
2137 vmcs_clear(loaded_vmcs->shadow_vmcs);
2138 loaded_vmcs->cpu = -1;
2139 loaded_vmcs->launched = 0;
2140 }
2141
vmcs_load(struct vmcs * vmcs)2142 static void vmcs_load(struct vmcs *vmcs)
2143 {
2144 u64 phys_addr = __pa(vmcs);
2145 bool error;
2146
2147 if (static_branch_unlikely(&enable_evmcs))
2148 return evmcs_load(phys_addr);
2149
2150 asm volatile (__ex(ASM_VMX_VMPTRLD_RAX) CC_SET(na)
2151 : CC_OUT(na) (error) : "a"(&phys_addr), "m"(phys_addr)
2152 : "memory");
2153 if (unlikely(error))
2154 printk(KERN_ERR "kvm: vmptrld %p/%llx failed\n",
2155 vmcs, phys_addr);
2156 }
2157
2158 #ifdef CONFIG_KEXEC_CORE
crash_vmclear_local_loaded_vmcss(void)2159 static void crash_vmclear_local_loaded_vmcss(void)
2160 {
2161 int cpu = raw_smp_processor_id();
2162 struct loaded_vmcs *v;
2163
2164 list_for_each_entry(v, &per_cpu(loaded_vmcss_on_cpu, cpu),
2165 loaded_vmcss_on_cpu_link)
2166 vmcs_clear(v->vmcs);
2167 }
2168 #endif /* CONFIG_KEXEC_CORE */
2169
__loaded_vmcs_clear(void * arg)2170 static void __loaded_vmcs_clear(void *arg)
2171 {
2172 struct loaded_vmcs *loaded_vmcs = arg;
2173 int cpu = raw_smp_processor_id();
2174
2175 if (loaded_vmcs->cpu != cpu)
2176 return; /* vcpu migration can race with cpu offline */
2177 if (per_cpu(current_vmcs, cpu) == loaded_vmcs->vmcs)
2178 per_cpu(current_vmcs, cpu) = NULL;
2179
2180 vmcs_clear(loaded_vmcs->vmcs);
2181 if (loaded_vmcs->shadow_vmcs && loaded_vmcs->launched)
2182 vmcs_clear(loaded_vmcs->shadow_vmcs);
2183
2184 list_del(&loaded_vmcs->loaded_vmcss_on_cpu_link);
2185
2186 /*
2187 * Ensure all writes to loaded_vmcs, including deleting it from its
2188 * current percpu list, complete before setting loaded_vmcs->vcpu to
2189 * -1, otherwise a different cpu can see vcpu == -1 first and add
2190 * loaded_vmcs to its percpu list before it's deleted from this cpu's
2191 * list. Pairs with the smp_rmb() in vmx_vcpu_load_vmcs().
2192 */
2193 smp_wmb();
2194
2195 loaded_vmcs->cpu = -1;
2196 loaded_vmcs->launched = 0;
2197 }
2198
loaded_vmcs_clear(struct loaded_vmcs * loaded_vmcs)2199 static void loaded_vmcs_clear(struct loaded_vmcs *loaded_vmcs)
2200 {
2201 int cpu = loaded_vmcs->cpu;
2202
2203 if (cpu != -1)
2204 smp_call_function_single(cpu,
2205 __loaded_vmcs_clear, loaded_vmcs, 1);
2206 }
2207
vpid_sync_vcpu_addr(int vpid,gva_t addr)2208 static inline bool vpid_sync_vcpu_addr(int vpid, gva_t addr)
2209 {
2210 if (vpid == 0)
2211 return true;
2212
2213 if (cpu_has_vmx_invvpid_individual_addr()) {
2214 __invvpid(VMX_VPID_EXTENT_INDIVIDUAL_ADDR, vpid, addr);
2215 return true;
2216 }
2217
2218 return false;
2219 }
2220
vpid_sync_vcpu_single(int vpid)2221 static inline void vpid_sync_vcpu_single(int vpid)
2222 {
2223 if (vpid == 0)
2224 return;
2225
2226 if (cpu_has_vmx_invvpid_single())
2227 __invvpid(VMX_VPID_EXTENT_SINGLE_CONTEXT, vpid, 0);
2228 }
2229
vpid_sync_vcpu_global(void)2230 static inline void vpid_sync_vcpu_global(void)
2231 {
2232 if (cpu_has_vmx_invvpid_global())
2233 __invvpid(VMX_VPID_EXTENT_ALL_CONTEXT, 0, 0);
2234 }
2235
vpid_sync_context(int vpid)2236 static inline void vpid_sync_context(int vpid)
2237 {
2238 if (cpu_has_vmx_invvpid_single())
2239 vpid_sync_vcpu_single(vpid);
2240 else
2241 vpid_sync_vcpu_global();
2242 }
2243
ept_sync_global(void)2244 static inline void ept_sync_global(void)
2245 {
2246 __invept(VMX_EPT_EXTENT_GLOBAL, 0, 0);
2247 }
2248
ept_sync_context(u64 eptp)2249 static inline void ept_sync_context(u64 eptp)
2250 {
2251 if (cpu_has_vmx_invept_context())
2252 __invept(VMX_EPT_EXTENT_CONTEXT, eptp, 0);
2253 else
2254 ept_sync_global();
2255 }
2256
vmcs_check16(unsigned long field)2257 static __always_inline void vmcs_check16(unsigned long field)
2258 {
2259 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2000,
2260 "16-bit accessor invalid for 64-bit field");
2261 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2001,
2262 "16-bit accessor invalid for 64-bit high field");
2263 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x4000,
2264 "16-bit accessor invalid for 32-bit high field");
2265 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x6000,
2266 "16-bit accessor invalid for natural width field");
2267 }
2268
vmcs_check32(unsigned long field)2269 static __always_inline void vmcs_check32(unsigned long field)
2270 {
2271 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0,
2272 "32-bit accessor invalid for 16-bit field");
2273 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x6000,
2274 "32-bit accessor invalid for natural width field");
2275 }
2276
vmcs_check64(unsigned long field)2277 static __always_inline void vmcs_check64(unsigned long field)
2278 {
2279 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0,
2280 "64-bit accessor invalid for 16-bit field");
2281 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2001,
2282 "64-bit accessor invalid for 64-bit high field");
2283 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x4000,
2284 "64-bit accessor invalid for 32-bit field");
2285 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x6000,
2286 "64-bit accessor invalid for natural width field");
2287 }
2288
vmcs_checkl(unsigned long field)2289 static __always_inline void vmcs_checkl(unsigned long field)
2290 {
2291 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0,
2292 "Natural width accessor invalid for 16-bit field");
2293 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2000,
2294 "Natural width accessor invalid for 64-bit field");
2295 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2001,
2296 "Natural width accessor invalid for 64-bit high field");
2297 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x4000,
2298 "Natural width accessor invalid for 32-bit field");
2299 }
2300
__vmcs_readl(unsigned long field)2301 static __always_inline unsigned long __vmcs_readl(unsigned long field)
2302 {
2303 unsigned long value;
2304
2305 asm volatile (__ex_clear(ASM_VMX_VMREAD_RDX_RAX, "%0")
2306 : "=a"(value) : "d"(field) : "cc");
2307 return value;
2308 }
2309
vmcs_read16(unsigned long field)2310 static __always_inline u16 vmcs_read16(unsigned long field)
2311 {
2312 vmcs_check16(field);
2313 if (static_branch_unlikely(&enable_evmcs))
2314 return evmcs_read16(field);
2315 return __vmcs_readl(field);
2316 }
2317
vmcs_read32(unsigned long field)2318 static __always_inline u32 vmcs_read32(unsigned long field)
2319 {
2320 vmcs_check32(field);
2321 if (static_branch_unlikely(&enable_evmcs))
2322 return evmcs_read32(field);
2323 return __vmcs_readl(field);
2324 }
2325
vmcs_read64(unsigned long field)2326 static __always_inline u64 vmcs_read64(unsigned long field)
2327 {
2328 vmcs_check64(field);
2329 if (static_branch_unlikely(&enable_evmcs))
2330 return evmcs_read64(field);
2331 #ifdef CONFIG_X86_64
2332 return __vmcs_readl(field);
2333 #else
2334 return __vmcs_readl(field) | ((u64)__vmcs_readl(field+1) << 32);
2335 #endif
2336 }
2337
vmcs_readl(unsigned long field)2338 static __always_inline unsigned long vmcs_readl(unsigned long field)
2339 {
2340 vmcs_checkl(field);
2341 if (static_branch_unlikely(&enable_evmcs))
2342 return evmcs_read64(field);
2343 return __vmcs_readl(field);
2344 }
2345
vmwrite_error(unsigned long field,unsigned long value)2346 static noinline void vmwrite_error(unsigned long field, unsigned long value)
2347 {
2348 printk(KERN_ERR "vmwrite error: reg %lx value %lx (err %d)\n",
2349 field, value, vmcs_read32(VM_INSTRUCTION_ERROR));
2350 dump_stack();
2351 }
2352
__vmcs_writel(unsigned long field,unsigned long value)2353 static __always_inline void __vmcs_writel(unsigned long field, unsigned long value)
2354 {
2355 bool error;
2356
2357 asm volatile (__ex(ASM_VMX_VMWRITE_RAX_RDX) CC_SET(na)
2358 : CC_OUT(na) (error) : "a"(value), "d"(field));
2359 if (unlikely(error))
2360 vmwrite_error(field, value);
2361 }
2362
vmcs_write16(unsigned long field,u16 value)2363 static __always_inline void vmcs_write16(unsigned long field, u16 value)
2364 {
2365 vmcs_check16(field);
2366 if (static_branch_unlikely(&enable_evmcs))
2367 return evmcs_write16(field, value);
2368
2369 __vmcs_writel(field, value);
2370 }
2371
vmcs_write32(unsigned long field,u32 value)2372 static __always_inline void vmcs_write32(unsigned long field, u32 value)
2373 {
2374 vmcs_check32(field);
2375 if (static_branch_unlikely(&enable_evmcs))
2376 return evmcs_write32(field, value);
2377
2378 __vmcs_writel(field, value);
2379 }
2380
vmcs_write64(unsigned long field,u64 value)2381 static __always_inline void vmcs_write64(unsigned long field, u64 value)
2382 {
2383 vmcs_check64(field);
2384 if (static_branch_unlikely(&enable_evmcs))
2385 return evmcs_write64(field, value);
2386
2387 __vmcs_writel(field, value);
2388 #ifndef CONFIG_X86_64
2389 asm volatile ("");
2390 __vmcs_writel(field+1, value >> 32);
2391 #endif
2392 }
2393
vmcs_writel(unsigned long field,unsigned long value)2394 static __always_inline void vmcs_writel(unsigned long field, unsigned long value)
2395 {
2396 vmcs_checkl(field);
2397 if (static_branch_unlikely(&enable_evmcs))
2398 return evmcs_write64(field, value);
2399
2400 __vmcs_writel(field, value);
2401 }
2402
vmcs_clear_bits(unsigned long field,u32 mask)2403 static __always_inline void vmcs_clear_bits(unsigned long field, u32 mask)
2404 {
2405 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x2000,
2406 "vmcs_clear_bits does not support 64-bit fields");
2407 if (static_branch_unlikely(&enable_evmcs))
2408 return evmcs_write32(field, evmcs_read32(field) & ~mask);
2409
2410 __vmcs_writel(field, __vmcs_readl(field) & ~mask);
2411 }
2412
vmcs_set_bits(unsigned long field,u32 mask)2413 static __always_inline void vmcs_set_bits(unsigned long field, u32 mask)
2414 {
2415 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x2000,
2416 "vmcs_set_bits does not support 64-bit fields");
2417 if (static_branch_unlikely(&enable_evmcs))
2418 return evmcs_write32(field, evmcs_read32(field) | mask);
2419
2420 __vmcs_writel(field, __vmcs_readl(field) | mask);
2421 }
2422
vm_entry_controls_reset_shadow(struct vcpu_vmx * vmx)2423 static inline void vm_entry_controls_reset_shadow(struct vcpu_vmx *vmx)
2424 {
2425 vmx->vm_entry_controls_shadow = vmcs_read32(VM_ENTRY_CONTROLS);
2426 }
2427
vm_entry_controls_init(struct vcpu_vmx * vmx,u32 val)2428 static inline void vm_entry_controls_init(struct vcpu_vmx *vmx, u32 val)
2429 {
2430 vmcs_write32(VM_ENTRY_CONTROLS, val);
2431 vmx->vm_entry_controls_shadow = val;
2432 }
2433
vm_entry_controls_set(struct vcpu_vmx * vmx,u32 val)2434 static inline void vm_entry_controls_set(struct vcpu_vmx *vmx, u32 val)
2435 {
2436 if (vmx->vm_entry_controls_shadow != val)
2437 vm_entry_controls_init(vmx, val);
2438 }
2439
vm_entry_controls_get(struct vcpu_vmx * vmx)2440 static inline u32 vm_entry_controls_get(struct vcpu_vmx *vmx)
2441 {
2442 return vmx->vm_entry_controls_shadow;
2443 }
2444
2445
vm_entry_controls_setbit(struct vcpu_vmx * vmx,u32 val)2446 static inline void vm_entry_controls_setbit(struct vcpu_vmx *vmx, u32 val)
2447 {
2448 vm_entry_controls_set(vmx, vm_entry_controls_get(vmx) | val);
2449 }
2450
vm_entry_controls_clearbit(struct vcpu_vmx * vmx,u32 val)2451 static inline void vm_entry_controls_clearbit(struct vcpu_vmx *vmx, u32 val)
2452 {
2453 vm_entry_controls_set(vmx, vm_entry_controls_get(vmx) & ~val);
2454 }
2455
vm_exit_controls_reset_shadow(struct vcpu_vmx * vmx)2456 static inline void vm_exit_controls_reset_shadow(struct vcpu_vmx *vmx)
2457 {
2458 vmx->vm_exit_controls_shadow = vmcs_read32(VM_EXIT_CONTROLS);
2459 }
2460
vm_exit_controls_init(struct vcpu_vmx * vmx,u32 val)2461 static inline void vm_exit_controls_init(struct vcpu_vmx *vmx, u32 val)
2462 {
2463 vmcs_write32(VM_EXIT_CONTROLS, val);
2464 vmx->vm_exit_controls_shadow = val;
2465 }
2466
vm_exit_controls_set(struct vcpu_vmx * vmx,u32 val)2467 static inline void vm_exit_controls_set(struct vcpu_vmx *vmx, u32 val)
2468 {
2469 if (vmx->vm_exit_controls_shadow != val)
2470 vm_exit_controls_init(vmx, val);
2471 }
2472
vm_exit_controls_get(struct vcpu_vmx * vmx)2473 static inline u32 vm_exit_controls_get(struct vcpu_vmx *vmx)
2474 {
2475 return vmx->vm_exit_controls_shadow;
2476 }
2477
2478
vm_exit_controls_setbit(struct vcpu_vmx * vmx,u32 val)2479 static inline void vm_exit_controls_setbit(struct vcpu_vmx *vmx, u32 val)
2480 {
2481 vm_exit_controls_set(vmx, vm_exit_controls_get(vmx) | val);
2482 }
2483
vm_exit_controls_clearbit(struct vcpu_vmx * vmx,u32 val)2484 static inline void vm_exit_controls_clearbit(struct vcpu_vmx *vmx, u32 val)
2485 {
2486 vm_exit_controls_set(vmx, vm_exit_controls_get(vmx) & ~val);
2487 }
2488
vmx_segment_cache_clear(struct vcpu_vmx * vmx)2489 static void vmx_segment_cache_clear(struct vcpu_vmx *vmx)
2490 {
2491 vmx->segment_cache.bitmask = 0;
2492 }
2493
vmx_segment_cache_test_set(struct vcpu_vmx * vmx,unsigned seg,unsigned field)2494 static bool vmx_segment_cache_test_set(struct vcpu_vmx *vmx, unsigned seg,
2495 unsigned field)
2496 {
2497 bool ret;
2498 u32 mask = 1 << (seg * SEG_FIELD_NR + field);
2499
2500 if (!(vmx->vcpu.arch.regs_avail & (1 << VCPU_EXREG_SEGMENTS))) {
2501 vmx->vcpu.arch.regs_avail |= (1 << VCPU_EXREG_SEGMENTS);
2502 vmx->segment_cache.bitmask = 0;
2503 }
2504 ret = vmx->segment_cache.bitmask & mask;
2505 vmx->segment_cache.bitmask |= mask;
2506 return ret;
2507 }
2508
vmx_read_guest_seg_selector(struct vcpu_vmx * vmx,unsigned seg)2509 static u16 vmx_read_guest_seg_selector(struct vcpu_vmx *vmx, unsigned seg)
2510 {
2511 u16 *p = &vmx->segment_cache.seg[seg].selector;
2512
2513 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_SEL))
2514 *p = vmcs_read16(kvm_vmx_segment_fields[seg].selector);
2515 return *p;
2516 }
2517
vmx_read_guest_seg_base(struct vcpu_vmx * vmx,unsigned seg)2518 static ulong vmx_read_guest_seg_base(struct vcpu_vmx *vmx, unsigned seg)
2519 {
2520 ulong *p = &vmx->segment_cache.seg[seg].base;
2521
2522 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_BASE))
2523 *p = vmcs_readl(kvm_vmx_segment_fields[seg].base);
2524 return *p;
2525 }
2526
vmx_read_guest_seg_limit(struct vcpu_vmx * vmx,unsigned seg)2527 static u32 vmx_read_guest_seg_limit(struct vcpu_vmx *vmx, unsigned seg)
2528 {
2529 u32 *p = &vmx->segment_cache.seg[seg].limit;
2530
2531 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_LIMIT))
2532 *p = vmcs_read32(kvm_vmx_segment_fields[seg].limit);
2533 return *p;
2534 }
2535
vmx_read_guest_seg_ar(struct vcpu_vmx * vmx,unsigned seg)2536 static u32 vmx_read_guest_seg_ar(struct vcpu_vmx *vmx, unsigned seg)
2537 {
2538 u32 *p = &vmx->segment_cache.seg[seg].ar;
2539
2540 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_AR))
2541 *p = vmcs_read32(kvm_vmx_segment_fields[seg].ar_bytes);
2542 return *p;
2543 }
2544
update_exception_bitmap(struct kvm_vcpu * vcpu)2545 static void update_exception_bitmap(struct kvm_vcpu *vcpu)
2546 {
2547 u32 eb;
2548
2549 eb = (1u << PF_VECTOR) | (1u << UD_VECTOR) | (1u << MC_VECTOR) |
2550 (1u << DB_VECTOR) | (1u << AC_VECTOR);
2551 /*
2552 * Guest access to VMware backdoor ports could legitimately
2553 * trigger #GP because of TSS I/O permission bitmap.
2554 * We intercept those #GP and allow access to them anyway
2555 * as VMware does.
2556 */
2557 if (enable_vmware_backdoor)
2558 eb |= (1u << GP_VECTOR);
2559 if ((vcpu->guest_debug &
2560 (KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_SW_BP)) ==
2561 (KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_SW_BP))
2562 eb |= 1u << BP_VECTOR;
2563 if (to_vmx(vcpu)->rmode.vm86_active)
2564 eb = ~0;
2565 if (enable_ept)
2566 eb &= ~(1u << PF_VECTOR); /* bypass_guest_pf = 0 */
2567
2568 /* When we are running a nested L2 guest and L1 specified for it a
2569 * certain exception bitmap, we must trap the same exceptions and pass
2570 * them to L1. When running L2, we will only handle the exceptions
2571 * specified above if L1 did not want them.
2572 */
2573 if (is_guest_mode(vcpu))
2574 eb |= get_vmcs12(vcpu)->exception_bitmap;
2575
2576 vmcs_write32(EXCEPTION_BITMAP, eb);
2577 }
2578
2579 /*
2580 * Check if MSR is intercepted for currently loaded MSR bitmap.
2581 */
msr_write_intercepted(struct kvm_vcpu * vcpu,u32 msr)2582 static bool msr_write_intercepted(struct kvm_vcpu *vcpu, u32 msr)
2583 {
2584 unsigned long *msr_bitmap;
2585 int f = sizeof(unsigned long);
2586
2587 if (!cpu_has_vmx_msr_bitmap())
2588 return true;
2589
2590 msr_bitmap = to_vmx(vcpu)->loaded_vmcs->msr_bitmap;
2591
2592 if (msr <= 0x1fff) {
2593 return !!test_bit(msr, msr_bitmap + 0x800 / f);
2594 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
2595 msr &= 0x1fff;
2596 return !!test_bit(msr, msr_bitmap + 0xc00 / f);
2597 }
2598
2599 return true;
2600 }
2601
2602 /*
2603 * Check if MSR is intercepted for L01 MSR bitmap.
2604 */
msr_write_intercepted_l01(struct kvm_vcpu * vcpu,u32 msr)2605 static bool msr_write_intercepted_l01(struct kvm_vcpu *vcpu, u32 msr)
2606 {
2607 unsigned long *msr_bitmap;
2608 int f = sizeof(unsigned long);
2609
2610 if (!cpu_has_vmx_msr_bitmap())
2611 return true;
2612
2613 msr_bitmap = to_vmx(vcpu)->vmcs01.msr_bitmap;
2614
2615 if (msr <= 0x1fff) {
2616 return !!test_bit(msr, msr_bitmap + 0x800 / f);
2617 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
2618 msr &= 0x1fff;
2619 return !!test_bit(msr, msr_bitmap + 0xc00 / f);
2620 }
2621
2622 return true;
2623 }
2624
clear_atomic_switch_msr_special(struct vcpu_vmx * vmx,unsigned long entry,unsigned long exit)2625 static void clear_atomic_switch_msr_special(struct vcpu_vmx *vmx,
2626 unsigned long entry, unsigned long exit)
2627 {
2628 vm_entry_controls_clearbit(vmx, entry);
2629 vm_exit_controls_clearbit(vmx, exit);
2630 }
2631
find_msr(struct vmx_msrs * m,unsigned int msr)2632 static int find_msr(struct vmx_msrs *m, unsigned int msr)
2633 {
2634 unsigned int i;
2635
2636 for (i = 0; i < m->nr; ++i) {
2637 if (m->val[i].index == msr)
2638 return i;
2639 }
2640 return -ENOENT;
2641 }
2642
clear_atomic_switch_msr(struct vcpu_vmx * vmx,unsigned msr)2643 static void clear_atomic_switch_msr(struct vcpu_vmx *vmx, unsigned msr)
2644 {
2645 int i;
2646 struct msr_autoload *m = &vmx->msr_autoload;
2647
2648 switch (msr) {
2649 case MSR_EFER:
2650 if (cpu_has_load_ia32_efer) {
2651 clear_atomic_switch_msr_special(vmx,
2652 VM_ENTRY_LOAD_IA32_EFER,
2653 VM_EXIT_LOAD_IA32_EFER);
2654 return;
2655 }
2656 break;
2657 case MSR_CORE_PERF_GLOBAL_CTRL:
2658 if (cpu_has_load_perf_global_ctrl) {
2659 clear_atomic_switch_msr_special(vmx,
2660 VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL,
2661 VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL);
2662 return;
2663 }
2664 break;
2665 }
2666 i = find_msr(&m->guest, msr);
2667 if (i < 0)
2668 goto skip_guest;
2669 --m->guest.nr;
2670 m->guest.val[i] = m->guest.val[m->guest.nr];
2671 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, m->guest.nr);
2672
2673 skip_guest:
2674 i = find_msr(&m->host, msr);
2675 if (i < 0)
2676 return;
2677
2678 --m->host.nr;
2679 m->host.val[i] = m->host.val[m->host.nr];
2680 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, m->host.nr);
2681 }
2682
add_atomic_switch_msr_special(struct vcpu_vmx * vmx,unsigned long entry,unsigned long exit,unsigned long guest_val_vmcs,unsigned long host_val_vmcs,u64 guest_val,u64 host_val)2683 static void add_atomic_switch_msr_special(struct vcpu_vmx *vmx,
2684 unsigned long entry, unsigned long exit,
2685 unsigned long guest_val_vmcs, unsigned long host_val_vmcs,
2686 u64 guest_val, u64 host_val)
2687 {
2688 vmcs_write64(guest_val_vmcs, guest_val);
2689 vmcs_write64(host_val_vmcs, host_val);
2690 vm_entry_controls_setbit(vmx, entry);
2691 vm_exit_controls_setbit(vmx, exit);
2692 }
2693
add_atomic_switch_msr(struct vcpu_vmx * vmx,unsigned msr,u64 guest_val,u64 host_val,bool entry_only)2694 static void add_atomic_switch_msr(struct vcpu_vmx *vmx, unsigned msr,
2695 u64 guest_val, u64 host_val, bool entry_only)
2696 {
2697 int i, j = 0;
2698 struct msr_autoload *m = &vmx->msr_autoload;
2699
2700 switch (msr) {
2701 case MSR_EFER:
2702 if (cpu_has_load_ia32_efer) {
2703 add_atomic_switch_msr_special(vmx,
2704 VM_ENTRY_LOAD_IA32_EFER,
2705 VM_EXIT_LOAD_IA32_EFER,
2706 GUEST_IA32_EFER,
2707 HOST_IA32_EFER,
2708 guest_val, host_val);
2709 return;
2710 }
2711 break;
2712 case MSR_CORE_PERF_GLOBAL_CTRL:
2713 if (cpu_has_load_perf_global_ctrl) {
2714 add_atomic_switch_msr_special(vmx,
2715 VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL,
2716 VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL,
2717 GUEST_IA32_PERF_GLOBAL_CTRL,
2718 HOST_IA32_PERF_GLOBAL_CTRL,
2719 guest_val, host_val);
2720 return;
2721 }
2722 break;
2723 case MSR_IA32_PEBS_ENABLE:
2724 /* PEBS needs a quiescent period after being disabled (to write
2725 * a record). Disabling PEBS through VMX MSR swapping doesn't
2726 * provide that period, so a CPU could write host's record into
2727 * guest's memory.
2728 */
2729 wrmsrl(MSR_IA32_PEBS_ENABLE, 0);
2730 }
2731
2732 i = find_msr(&m->guest, msr);
2733 if (!entry_only)
2734 j = find_msr(&m->host, msr);
2735
2736 if ((i < 0 && m->guest.nr == NR_AUTOLOAD_MSRS) ||
2737 (j < 0 && m->host.nr == NR_AUTOLOAD_MSRS)) {
2738 printk_once(KERN_WARNING "Not enough msr switch entries. "
2739 "Can't add msr %x\n", msr);
2740 return;
2741 }
2742 if (i < 0) {
2743 i = m->guest.nr++;
2744 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, m->guest.nr);
2745 }
2746 m->guest.val[i].index = msr;
2747 m->guest.val[i].value = guest_val;
2748
2749 if (entry_only)
2750 return;
2751
2752 if (j < 0) {
2753 j = m->host.nr++;
2754 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, m->host.nr);
2755 }
2756 m->host.val[j].index = msr;
2757 m->host.val[j].value = host_val;
2758 }
2759
update_transition_efer(struct vcpu_vmx * vmx,int efer_offset)2760 static bool update_transition_efer(struct vcpu_vmx *vmx, int efer_offset)
2761 {
2762 u64 guest_efer = vmx->vcpu.arch.efer;
2763 u64 ignore_bits = 0;
2764
2765 /* Shadow paging assumes NX to be available. */
2766 if (!enable_ept)
2767 guest_efer |= EFER_NX;
2768
2769 /*
2770 * LMA and LME handled by hardware; SCE meaningless outside long mode.
2771 */
2772 ignore_bits |= EFER_SCE;
2773 #ifdef CONFIG_X86_64
2774 ignore_bits |= EFER_LMA | EFER_LME;
2775 /* SCE is meaningful only in long mode on Intel */
2776 if (guest_efer & EFER_LMA)
2777 ignore_bits &= ~(u64)EFER_SCE;
2778 #endif
2779
2780 clear_atomic_switch_msr(vmx, MSR_EFER);
2781
2782 /*
2783 * On EPT, we can't emulate NX, so we must switch EFER atomically.
2784 * On CPUs that support "load IA32_EFER", always switch EFER
2785 * atomically, since it's faster than switching it manually.
2786 */
2787 if (cpu_has_load_ia32_efer ||
2788 (enable_ept && ((vmx->vcpu.arch.efer ^ host_efer) & EFER_NX))) {
2789 if (!(guest_efer & EFER_LMA))
2790 guest_efer &= ~EFER_LME;
2791 if (guest_efer != host_efer)
2792 add_atomic_switch_msr(vmx, MSR_EFER,
2793 guest_efer, host_efer, false);
2794 return false;
2795 } else {
2796 guest_efer &= ~ignore_bits;
2797 guest_efer |= host_efer & ignore_bits;
2798
2799 vmx->guest_msrs[efer_offset].data = guest_efer;
2800 vmx->guest_msrs[efer_offset].mask = ~ignore_bits;
2801
2802 return true;
2803 }
2804 }
2805
2806 #ifdef CONFIG_X86_32
2807 /*
2808 * On 32-bit kernels, VM exits still load the FS and GS bases from the
2809 * VMCS rather than the segment table. KVM uses this helper to figure
2810 * out the current bases to poke them into the VMCS before entry.
2811 */
segment_base(u16 selector)2812 static unsigned long segment_base(u16 selector)
2813 {
2814 struct desc_struct *table;
2815 unsigned long v;
2816
2817 if (!(selector & ~SEGMENT_RPL_MASK))
2818 return 0;
2819
2820 table = get_current_gdt_ro();
2821
2822 if ((selector & SEGMENT_TI_MASK) == SEGMENT_LDT) {
2823 u16 ldt_selector = kvm_read_ldt();
2824
2825 if (!(ldt_selector & ~SEGMENT_RPL_MASK))
2826 return 0;
2827
2828 table = (struct desc_struct *)segment_base(ldt_selector);
2829 }
2830 v = get_desc_base(&table[selector >> 3]);
2831 return v;
2832 }
2833 #endif
2834
vmx_prepare_switch_to_guest(struct kvm_vcpu * vcpu)2835 static void vmx_prepare_switch_to_guest(struct kvm_vcpu *vcpu)
2836 {
2837 struct vcpu_vmx *vmx = to_vmx(vcpu);
2838 struct vmcs_host_state *host_state;
2839 #ifdef CONFIG_X86_64
2840 int cpu = raw_smp_processor_id();
2841 #endif
2842 unsigned long fs_base, gs_base;
2843 u16 fs_sel, gs_sel;
2844 int i;
2845
2846 vmx->req_immediate_exit = false;
2847
2848 /*
2849 * Note that guest MSRs to be saved/restored can also be changed
2850 * when guest state is loaded. This happens when guest transitions
2851 * to/from long-mode by setting MSR_EFER.LMA.
2852 */
2853 if (!vmx->loaded_cpu_state || vmx->guest_msrs_dirty) {
2854 vmx->guest_msrs_dirty = false;
2855 for (i = 0; i < vmx->save_nmsrs; ++i)
2856 kvm_set_shared_msr(vmx->guest_msrs[i].index,
2857 vmx->guest_msrs[i].data,
2858 vmx->guest_msrs[i].mask);
2859
2860 }
2861
2862 if (vmx->loaded_cpu_state)
2863 return;
2864
2865 vmx->loaded_cpu_state = vmx->loaded_vmcs;
2866 host_state = &vmx->loaded_cpu_state->host_state;
2867
2868 /*
2869 * Set host fs and gs selectors. Unfortunately, 22.2.3 does not
2870 * allow segment selectors with cpl > 0 or ti == 1.
2871 */
2872 host_state->ldt_sel = kvm_read_ldt();
2873
2874 #ifdef CONFIG_X86_64
2875 savesegment(ds, host_state->ds_sel);
2876 savesegment(es, host_state->es_sel);
2877
2878 gs_base = cpu_kernelmode_gs_base(cpu);
2879 if (likely(is_64bit_mm(current->mm))) {
2880 save_fsgs_for_kvm();
2881 fs_sel = current->thread.fsindex;
2882 gs_sel = current->thread.gsindex;
2883 fs_base = current->thread.fsbase;
2884 vmx->msr_host_kernel_gs_base = current->thread.gsbase;
2885 } else {
2886 savesegment(fs, fs_sel);
2887 savesegment(gs, gs_sel);
2888 fs_base = read_msr(MSR_FS_BASE);
2889 vmx->msr_host_kernel_gs_base = read_msr(MSR_KERNEL_GS_BASE);
2890 }
2891
2892 wrmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base);
2893 #else
2894 savesegment(fs, fs_sel);
2895 savesegment(gs, gs_sel);
2896 fs_base = segment_base(fs_sel);
2897 gs_base = segment_base(gs_sel);
2898 #endif
2899
2900 if (unlikely(fs_sel != host_state->fs_sel)) {
2901 if (!(fs_sel & 7))
2902 vmcs_write16(HOST_FS_SELECTOR, fs_sel);
2903 else
2904 vmcs_write16(HOST_FS_SELECTOR, 0);
2905 host_state->fs_sel = fs_sel;
2906 }
2907 if (unlikely(gs_sel != host_state->gs_sel)) {
2908 if (!(gs_sel & 7))
2909 vmcs_write16(HOST_GS_SELECTOR, gs_sel);
2910 else
2911 vmcs_write16(HOST_GS_SELECTOR, 0);
2912 host_state->gs_sel = gs_sel;
2913 }
2914 if (unlikely(fs_base != host_state->fs_base)) {
2915 vmcs_writel(HOST_FS_BASE, fs_base);
2916 host_state->fs_base = fs_base;
2917 }
2918 if (unlikely(gs_base != host_state->gs_base)) {
2919 vmcs_writel(HOST_GS_BASE, gs_base);
2920 host_state->gs_base = gs_base;
2921 }
2922 }
2923
vmx_prepare_switch_to_host(struct vcpu_vmx * vmx)2924 static void vmx_prepare_switch_to_host(struct vcpu_vmx *vmx)
2925 {
2926 struct vmcs_host_state *host_state;
2927
2928 if (!vmx->loaded_cpu_state)
2929 return;
2930
2931 WARN_ON_ONCE(vmx->loaded_cpu_state != vmx->loaded_vmcs);
2932 host_state = &vmx->loaded_cpu_state->host_state;
2933
2934 ++vmx->vcpu.stat.host_state_reload;
2935 vmx->loaded_cpu_state = NULL;
2936
2937 #ifdef CONFIG_X86_64
2938 rdmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base);
2939 #endif
2940 if (host_state->ldt_sel || (host_state->gs_sel & 7)) {
2941 kvm_load_ldt(host_state->ldt_sel);
2942 #ifdef CONFIG_X86_64
2943 load_gs_index(host_state->gs_sel);
2944 #else
2945 loadsegment(gs, host_state->gs_sel);
2946 #endif
2947 }
2948 if (host_state->fs_sel & 7)
2949 loadsegment(fs, host_state->fs_sel);
2950 #ifdef CONFIG_X86_64
2951 if (unlikely(host_state->ds_sel | host_state->es_sel)) {
2952 loadsegment(ds, host_state->ds_sel);
2953 loadsegment(es, host_state->es_sel);
2954 }
2955 #endif
2956 invalidate_tss_limit();
2957 #ifdef CONFIG_X86_64
2958 wrmsrl(MSR_KERNEL_GS_BASE, vmx->msr_host_kernel_gs_base);
2959 #endif
2960 load_fixmap_gdt(raw_smp_processor_id());
2961 }
2962
2963 #ifdef CONFIG_X86_64
vmx_read_guest_kernel_gs_base(struct vcpu_vmx * vmx)2964 static u64 vmx_read_guest_kernel_gs_base(struct vcpu_vmx *vmx)
2965 {
2966 preempt_disable();
2967 if (vmx->loaded_cpu_state)
2968 rdmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base);
2969 preempt_enable();
2970 return vmx->msr_guest_kernel_gs_base;
2971 }
2972
vmx_write_guest_kernel_gs_base(struct vcpu_vmx * vmx,u64 data)2973 static void vmx_write_guest_kernel_gs_base(struct vcpu_vmx *vmx, u64 data)
2974 {
2975 preempt_disable();
2976 if (vmx->loaded_cpu_state)
2977 wrmsrl(MSR_KERNEL_GS_BASE, data);
2978 preempt_enable();
2979 vmx->msr_guest_kernel_gs_base = data;
2980 }
2981 #endif
2982
vmx_vcpu_pi_load(struct kvm_vcpu * vcpu,int cpu)2983 static void vmx_vcpu_pi_load(struct kvm_vcpu *vcpu, int cpu)
2984 {
2985 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
2986 struct pi_desc old, new;
2987 unsigned int dest;
2988
2989 /*
2990 * In case of hot-plug or hot-unplug, we may have to undo
2991 * vmx_vcpu_pi_put even if there is no assigned device. And we
2992 * always keep PI.NDST up to date for simplicity: it makes the
2993 * code easier, and CPU migration is not a fast path.
2994 */
2995 if (!pi_test_sn(pi_desc) && vcpu->cpu == cpu)
2996 return;
2997
2998 /*
2999 * First handle the simple case where no cmpxchg is necessary; just
3000 * allow posting non-urgent interrupts.
3001 *
3002 * If the 'nv' field is POSTED_INTR_WAKEUP_VECTOR, do not change
3003 * PI.NDST: pi_post_block will do it for us and the wakeup_handler
3004 * expects the VCPU to be on the blocked_vcpu_list that matches
3005 * PI.NDST.
3006 */
3007 if (pi_desc->nv == POSTED_INTR_WAKEUP_VECTOR ||
3008 vcpu->cpu == cpu) {
3009 pi_clear_sn(pi_desc);
3010 return;
3011 }
3012
3013 /* The full case. */
3014 do {
3015 old.control = new.control = pi_desc->control;
3016
3017 dest = cpu_physical_id(cpu);
3018
3019 if (x2apic_enabled())
3020 new.ndst = dest;
3021 else
3022 new.ndst = (dest << 8) & 0xFF00;
3023
3024 new.sn = 0;
3025 } while (cmpxchg64(&pi_desc->control, old.control,
3026 new.control) != old.control);
3027 }
3028
decache_tsc_multiplier(struct vcpu_vmx * vmx)3029 static void decache_tsc_multiplier(struct vcpu_vmx *vmx)
3030 {
3031 vmx->current_tsc_ratio = vmx->vcpu.arch.tsc_scaling_ratio;
3032 vmcs_write64(TSC_MULTIPLIER, vmx->current_tsc_ratio);
3033 }
3034
3035 /*
3036 * Switches to specified vcpu, until a matching vcpu_put(), but assumes
3037 * vcpu mutex is already taken.
3038 */
vmx_vcpu_load(struct kvm_vcpu * vcpu,int cpu)3039 static void vmx_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
3040 {
3041 struct vcpu_vmx *vmx = to_vmx(vcpu);
3042 bool already_loaded = vmx->loaded_vmcs->cpu == cpu;
3043
3044 if (!already_loaded) {
3045 loaded_vmcs_clear(vmx->loaded_vmcs);
3046 local_irq_disable();
3047
3048 /*
3049 * Ensure loaded_vmcs->cpu is read before adding loaded_vmcs to
3050 * this cpu's percpu list, otherwise it may not yet be deleted
3051 * from its previous cpu's percpu list. Pairs with the
3052 * smb_wmb() in __loaded_vmcs_clear().
3053 */
3054 smp_rmb();
3055
3056 list_add(&vmx->loaded_vmcs->loaded_vmcss_on_cpu_link,
3057 &per_cpu(loaded_vmcss_on_cpu, cpu));
3058 local_irq_enable();
3059 }
3060
3061 if (per_cpu(current_vmcs, cpu) != vmx->loaded_vmcs->vmcs) {
3062 per_cpu(current_vmcs, cpu) = vmx->loaded_vmcs->vmcs;
3063 vmcs_load(vmx->loaded_vmcs->vmcs);
3064 indirect_branch_prediction_barrier();
3065 }
3066
3067 if (!already_loaded) {
3068 void *gdt = get_current_gdt_ro();
3069 unsigned long sysenter_esp;
3070
3071 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
3072
3073 /*
3074 * Linux uses per-cpu TSS and GDT, so set these when switching
3075 * processors. See 22.2.4.
3076 */
3077 vmcs_writel(HOST_TR_BASE,
3078 (unsigned long)&get_cpu_entry_area(cpu)->tss.x86_tss);
3079 vmcs_writel(HOST_GDTR_BASE, (unsigned long)gdt); /* 22.2.4 */
3080
3081 /*
3082 * VM exits change the host TR limit to 0x67 after a VM
3083 * exit. This is okay, since 0x67 covers everything except
3084 * the IO bitmap and have have code to handle the IO bitmap
3085 * being lost after a VM exit.
3086 */
3087 BUILD_BUG_ON(IO_BITMAP_OFFSET - 1 != 0x67);
3088
3089 rdmsrl(MSR_IA32_SYSENTER_ESP, sysenter_esp);
3090 vmcs_writel(HOST_IA32_SYSENTER_ESP, sysenter_esp); /* 22.2.3 */
3091
3092 vmx->loaded_vmcs->cpu = cpu;
3093 }
3094
3095 /* Setup TSC multiplier */
3096 if (kvm_has_tsc_control &&
3097 vmx->current_tsc_ratio != vcpu->arch.tsc_scaling_ratio)
3098 decache_tsc_multiplier(vmx);
3099
3100 vmx_vcpu_pi_load(vcpu, cpu);
3101 vmx->host_pkru = read_pkru();
3102 vmx->host_debugctlmsr = get_debugctlmsr();
3103 }
3104
vmx_vcpu_pi_put(struct kvm_vcpu * vcpu)3105 static void vmx_vcpu_pi_put(struct kvm_vcpu *vcpu)
3106 {
3107 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
3108
3109 if (!kvm_arch_has_assigned_device(vcpu->kvm) ||
3110 !irq_remapping_cap(IRQ_POSTING_CAP) ||
3111 !kvm_vcpu_apicv_active(vcpu))
3112 return;
3113
3114 /* Set SN when the vCPU is preempted */
3115 if (vcpu->preempted)
3116 pi_set_sn(pi_desc);
3117 }
3118
vmx_vcpu_put(struct kvm_vcpu * vcpu)3119 static void vmx_vcpu_put(struct kvm_vcpu *vcpu)
3120 {
3121 vmx_vcpu_pi_put(vcpu);
3122
3123 vmx_prepare_switch_to_host(to_vmx(vcpu));
3124 }
3125
emulation_required(struct kvm_vcpu * vcpu)3126 static bool emulation_required(struct kvm_vcpu *vcpu)
3127 {
3128 return emulate_invalid_guest_state && !guest_state_valid(vcpu);
3129 }
3130
3131 static void vmx_decache_cr0_guest_bits(struct kvm_vcpu *vcpu);
3132
3133 /*
3134 * Return the cr0 value that a nested guest would read. This is a combination
3135 * of the real cr0 used to run the guest (guest_cr0), and the bits shadowed by
3136 * its hypervisor (cr0_read_shadow).
3137 */
nested_read_cr0(struct vmcs12 * fields)3138 static inline unsigned long nested_read_cr0(struct vmcs12 *fields)
3139 {
3140 return (fields->guest_cr0 & ~fields->cr0_guest_host_mask) |
3141 (fields->cr0_read_shadow & fields->cr0_guest_host_mask);
3142 }
nested_read_cr4(struct vmcs12 * fields)3143 static inline unsigned long nested_read_cr4(struct vmcs12 *fields)
3144 {
3145 return (fields->guest_cr4 & ~fields->cr4_guest_host_mask) |
3146 (fields->cr4_read_shadow & fields->cr4_guest_host_mask);
3147 }
3148
vmx_get_rflags(struct kvm_vcpu * vcpu)3149 static unsigned long vmx_get_rflags(struct kvm_vcpu *vcpu)
3150 {
3151 unsigned long rflags, save_rflags;
3152
3153 if (!test_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail)) {
3154 __set_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail);
3155 rflags = vmcs_readl(GUEST_RFLAGS);
3156 if (to_vmx(vcpu)->rmode.vm86_active) {
3157 rflags &= RMODE_GUEST_OWNED_EFLAGS_BITS;
3158 save_rflags = to_vmx(vcpu)->rmode.save_rflags;
3159 rflags |= save_rflags & ~RMODE_GUEST_OWNED_EFLAGS_BITS;
3160 }
3161 to_vmx(vcpu)->rflags = rflags;
3162 }
3163 return to_vmx(vcpu)->rflags;
3164 }
3165
vmx_set_rflags(struct kvm_vcpu * vcpu,unsigned long rflags)3166 static void vmx_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags)
3167 {
3168 unsigned long old_rflags = vmx_get_rflags(vcpu);
3169
3170 __set_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail);
3171 to_vmx(vcpu)->rflags = rflags;
3172 if (to_vmx(vcpu)->rmode.vm86_active) {
3173 to_vmx(vcpu)->rmode.save_rflags = rflags;
3174 rflags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM;
3175 }
3176 vmcs_writel(GUEST_RFLAGS, rflags);
3177
3178 if ((old_rflags ^ to_vmx(vcpu)->rflags) & X86_EFLAGS_VM)
3179 to_vmx(vcpu)->emulation_required = emulation_required(vcpu);
3180 }
3181
vmx_get_interrupt_shadow(struct kvm_vcpu * vcpu)3182 static u32 vmx_get_interrupt_shadow(struct kvm_vcpu *vcpu)
3183 {
3184 u32 interruptibility = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
3185 int ret = 0;
3186
3187 if (interruptibility & GUEST_INTR_STATE_STI)
3188 ret |= KVM_X86_SHADOW_INT_STI;
3189 if (interruptibility & GUEST_INTR_STATE_MOV_SS)
3190 ret |= KVM_X86_SHADOW_INT_MOV_SS;
3191
3192 return ret;
3193 }
3194
vmx_set_interrupt_shadow(struct kvm_vcpu * vcpu,int mask)3195 static void vmx_set_interrupt_shadow(struct kvm_vcpu *vcpu, int mask)
3196 {
3197 u32 interruptibility_old = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
3198 u32 interruptibility = interruptibility_old;
3199
3200 interruptibility &= ~(GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS);
3201
3202 if (mask & KVM_X86_SHADOW_INT_MOV_SS)
3203 interruptibility |= GUEST_INTR_STATE_MOV_SS;
3204 else if (mask & KVM_X86_SHADOW_INT_STI)
3205 interruptibility |= GUEST_INTR_STATE_STI;
3206
3207 if ((interruptibility != interruptibility_old))
3208 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, interruptibility);
3209 }
3210
skip_emulated_instruction(struct kvm_vcpu * vcpu)3211 static void skip_emulated_instruction(struct kvm_vcpu *vcpu)
3212 {
3213 unsigned long rip;
3214
3215 rip = kvm_rip_read(vcpu);
3216 rip += vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
3217 kvm_rip_write(vcpu, rip);
3218
3219 /* skipping an emulated instruction also counts */
3220 vmx_set_interrupt_shadow(vcpu, 0);
3221 }
3222
nested_vmx_inject_exception_vmexit(struct kvm_vcpu * vcpu,unsigned long exit_qual)3223 static void nested_vmx_inject_exception_vmexit(struct kvm_vcpu *vcpu,
3224 unsigned long exit_qual)
3225 {
3226 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
3227 unsigned int nr = vcpu->arch.exception.nr;
3228 u32 intr_info = nr | INTR_INFO_VALID_MASK;
3229
3230 if (vcpu->arch.exception.has_error_code) {
3231 vmcs12->vm_exit_intr_error_code = vcpu->arch.exception.error_code;
3232 intr_info |= INTR_INFO_DELIVER_CODE_MASK;
3233 }
3234
3235 if (kvm_exception_is_soft(nr))
3236 intr_info |= INTR_TYPE_SOFT_EXCEPTION;
3237 else
3238 intr_info |= INTR_TYPE_HARD_EXCEPTION;
3239
3240 if (!(vmcs12->idt_vectoring_info_field & VECTORING_INFO_VALID_MASK) &&
3241 vmx_get_nmi_mask(vcpu))
3242 intr_info |= INTR_INFO_UNBLOCK_NMI;
3243
3244 nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI, intr_info, exit_qual);
3245 }
3246
3247 /*
3248 * KVM wants to inject page-faults which it got to the guest. This function
3249 * checks whether in a nested guest, we need to inject them to L1 or L2.
3250 */
nested_vmx_check_exception(struct kvm_vcpu * vcpu,unsigned long * exit_qual)3251 static int nested_vmx_check_exception(struct kvm_vcpu *vcpu, unsigned long *exit_qual)
3252 {
3253 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
3254 unsigned int nr = vcpu->arch.exception.nr;
3255
3256 if (nr == PF_VECTOR) {
3257 if (vcpu->arch.exception.nested_apf) {
3258 *exit_qual = vcpu->arch.apf.nested_apf_token;
3259 return 1;
3260 }
3261 /*
3262 * FIXME: we must not write CR2 when L1 intercepts an L2 #PF exception.
3263 * The fix is to add the ancillary datum (CR2 or DR6) to structs
3264 * kvm_queued_exception and kvm_vcpu_events, so that CR2 and DR6
3265 * can be written only when inject_pending_event runs. This should be
3266 * conditional on a new capability---if the capability is disabled,
3267 * kvm_multiple_exception would write the ancillary information to
3268 * CR2 or DR6, for backwards ABI-compatibility.
3269 */
3270 if (nested_vmx_is_page_fault_vmexit(vmcs12,
3271 vcpu->arch.exception.error_code)) {
3272 *exit_qual = vcpu->arch.cr2;
3273 return 1;
3274 }
3275 } else {
3276 if (vmcs12->exception_bitmap & (1u << nr)) {
3277 if (nr == DB_VECTOR) {
3278 *exit_qual = vcpu->arch.dr6;
3279 *exit_qual &= ~(DR6_FIXED_1 | DR6_BT);
3280 *exit_qual ^= DR6_RTM;
3281 } else {
3282 *exit_qual = 0;
3283 }
3284 return 1;
3285 }
3286 }
3287
3288 return 0;
3289 }
3290
vmx_clear_hlt(struct kvm_vcpu * vcpu)3291 static void vmx_clear_hlt(struct kvm_vcpu *vcpu)
3292 {
3293 /*
3294 * Ensure that we clear the HLT state in the VMCS. We don't need to
3295 * explicitly skip the instruction because if the HLT state is set,
3296 * then the instruction is already executing and RIP has already been
3297 * advanced.
3298 */
3299 if (kvm_hlt_in_guest(vcpu->kvm) &&
3300 vmcs_read32(GUEST_ACTIVITY_STATE) == GUEST_ACTIVITY_HLT)
3301 vmcs_write32(GUEST_ACTIVITY_STATE, GUEST_ACTIVITY_ACTIVE);
3302 }
3303
vmx_queue_exception(struct kvm_vcpu * vcpu)3304 static void vmx_queue_exception(struct kvm_vcpu *vcpu)
3305 {
3306 struct vcpu_vmx *vmx = to_vmx(vcpu);
3307 unsigned nr = vcpu->arch.exception.nr;
3308 bool has_error_code = vcpu->arch.exception.has_error_code;
3309 u32 error_code = vcpu->arch.exception.error_code;
3310 u32 intr_info = nr | INTR_INFO_VALID_MASK;
3311
3312 if (has_error_code) {
3313 vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE, error_code);
3314 intr_info |= INTR_INFO_DELIVER_CODE_MASK;
3315 }
3316
3317 if (vmx->rmode.vm86_active) {
3318 int inc_eip = 0;
3319 if (kvm_exception_is_soft(nr))
3320 inc_eip = vcpu->arch.event_exit_inst_len;
3321 if (kvm_inject_realmode_interrupt(vcpu, nr, inc_eip) != EMULATE_DONE)
3322 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
3323 return;
3324 }
3325
3326 WARN_ON_ONCE(vmx->emulation_required);
3327
3328 if (kvm_exception_is_soft(nr)) {
3329 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
3330 vmx->vcpu.arch.event_exit_inst_len);
3331 intr_info |= INTR_TYPE_SOFT_EXCEPTION;
3332 } else
3333 intr_info |= INTR_TYPE_HARD_EXCEPTION;
3334
3335 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr_info);
3336
3337 vmx_clear_hlt(vcpu);
3338 }
3339
vmx_rdtscp_supported(void)3340 static bool vmx_rdtscp_supported(void)
3341 {
3342 return cpu_has_vmx_rdtscp();
3343 }
3344
vmx_invpcid_supported(void)3345 static bool vmx_invpcid_supported(void)
3346 {
3347 return cpu_has_vmx_invpcid();
3348 }
3349
3350 /*
3351 * Swap MSR entry in host/guest MSR entry array.
3352 */
move_msr_up(struct vcpu_vmx * vmx,int from,int to)3353 static void move_msr_up(struct vcpu_vmx *vmx, int from, int to)
3354 {
3355 struct shared_msr_entry tmp;
3356
3357 tmp = vmx->guest_msrs[to];
3358 vmx->guest_msrs[to] = vmx->guest_msrs[from];
3359 vmx->guest_msrs[from] = tmp;
3360 }
3361
3362 /*
3363 * Set up the vmcs to automatically save and restore system
3364 * msrs. Don't touch the 64-bit msrs if the guest is in legacy
3365 * mode, as fiddling with msrs is very expensive.
3366 */
setup_msrs(struct vcpu_vmx * vmx)3367 static void setup_msrs(struct vcpu_vmx *vmx)
3368 {
3369 int save_nmsrs, index;
3370
3371 save_nmsrs = 0;
3372 #ifdef CONFIG_X86_64
3373 if (is_long_mode(&vmx->vcpu)) {
3374 index = __find_msr_index(vmx, MSR_SYSCALL_MASK);
3375 if (index >= 0)
3376 move_msr_up(vmx, index, save_nmsrs++);
3377 index = __find_msr_index(vmx, MSR_LSTAR);
3378 if (index >= 0)
3379 move_msr_up(vmx, index, save_nmsrs++);
3380 index = __find_msr_index(vmx, MSR_CSTAR);
3381 if (index >= 0)
3382 move_msr_up(vmx, index, save_nmsrs++);
3383 /*
3384 * MSR_STAR is only needed on long mode guests, and only
3385 * if efer.sce is enabled.
3386 */
3387 index = __find_msr_index(vmx, MSR_STAR);
3388 if ((index >= 0) && (vmx->vcpu.arch.efer & EFER_SCE))
3389 move_msr_up(vmx, index, save_nmsrs++);
3390 }
3391 #endif
3392 index = __find_msr_index(vmx, MSR_EFER);
3393 if (index >= 0 && update_transition_efer(vmx, index))
3394 move_msr_up(vmx, index, save_nmsrs++);
3395 index = __find_msr_index(vmx, MSR_TSC_AUX);
3396 if (index >= 0 && guest_cpuid_has(&vmx->vcpu, X86_FEATURE_RDTSCP))
3397 move_msr_up(vmx, index, save_nmsrs++);
3398
3399 vmx->save_nmsrs = save_nmsrs;
3400 vmx->guest_msrs_dirty = true;
3401
3402 if (cpu_has_vmx_msr_bitmap())
3403 vmx_update_msr_bitmap(&vmx->vcpu);
3404 }
3405
vmx_read_l1_tsc_offset(struct kvm_vcpu * vcpu)3406 static u64 vmx_read_l1_tsc_offset(struct kvm_vcpu *vcpu)
3407 {
3408 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
3409
3410 if (is_guest_mode(vcpu) &&
3411 (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING))
3412 return vcpu->arch.tsc_offset - vmcs12->tsc_offset;
3413
3414 return vcpu->arch.tsc_offset;
3415 }
3416
vmx_write_l1_tsc_offset(struct kvm_vcpu * vcpu,u64 offset)3417 static u64 vmx_write_l1_tsc_offset(struct kvm_vcpu *vcpu, u64 offset)
3418 {
3419 u64 active_offset = offset;
3420 if (is_guest_mode(vcpu)) {
3421 /*
3422 * We're here if L1 chose not to trap WRMSR to TSC. According
3423 * to the spec, this should set L1's TSC; The offset that L1
3424 * set for L2 remains unchanged, and still needs to be added
3425 * to the newly set TSC to get L2's TSC.
3426 */
3427 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
3428 if (nested_cpu_has(vmcs12, CPU_BASED_USE_TSC_OFFSETING))
3429 active_offset += vmcs12->tsc_offset;
3430 } else {
3431 trace_kvm_write_tsc_offset(vcpu->vcpu_id,
3432 vmcs_read64(TSC_OFFSET), offset);
3433 }
3434
3435 vmcs_write64(TSC_OFFSET, active_offset);
3436 return active_offset;
3437 }
3438
3439 /*
3440 * nested_vmx_allowed() checks whether a guest should be allowed to use VMX
3441 * instructions and MSRs (i.e., nested VMX). Nested VMX is disabled for
3442 * all guests if the "nested" module option is off, and can also be disabled
3443 * for a single guest by disabling its VMX cpuid bit.
3444 */
nested_vmx_allowed(struct kvm_vcpu * vcpu)3445 static inline bool nested_vmx_allowed(struct kvm_vcpu *vcpu)
3446 {
3447 return nested && guest_cpuid_has(vcpu, X86_FEATURE_VMX);
3448 }
3449
3450 /*
3451 * nested_vmx_setup_ctls_msrs() sets up variables containing the values to be
3452 * returned for the various VMX controls MSRs when nested VMX is enabled.
3453 * The same values should also be used to verify that vmcs12 control fields are
3454 * valid during nested entry from L1 to L2.
3455 * Each of these control msrs has a low and high 32-bit half: A low bit is on
3456 * if the corresponding bit in the (32-bit) control field *must* be on, and a
3457 * bit in the high half is on if the corresponding bit in the control field
3458 * may be on. See also vmx_control_verify().
3459 */
nested_vmx_setup_ctls_msrs(struct nested_vmx_msrs * msrs,bool apicv)3460 static void nested_vmx_setup_ctls_msrs(struct nested_vmx_msrs *msrs, bool apicv)
3461 {
3462 if (!nested) {
3463 memset(msrs, 0, sizeof(*msrs));
3464 return;
3465 }
3466
3467 /*
3468 * Note that as a general rule, the high half of the MSRs (bits in
3469 * the control fields which may be 1) should be initialized by the
3470 * intersection of the underlying hardware's MSR (i.e., features which
3471 * can be supported) and the list of features we want to expose -
3472 * because they are known to be properly supported in our code.
3473 * Also, usually, the low half of the MSRs (bits which must be 1) can
3474 * be set to 0, meaning that L1 may turn off any of these bits. The
3475 * reason is that if one of these bits is necessary, it will appear
3476 * in vmcs01 and prepare_vmcs02, when it bitwise-or's the control
3477 * fields of vmcs01 and vmcs02, will turn these bits off - and
3478 * nested_vmx_exit_reflected() will not pass related exits to L1.
3479 * These rules have exceptions below.
3480 */
3481
3482 /* pin-based controls */
3483 rdmsr(MSR_IA32_VMX_PINBASED_CTLS,
3484 msrs->pinbased_ctls_low,
3485 msrs->pinbased_ctls_high);
3486 msrs->pinbased_ctls_low |=
3487 PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
3488 msrs->pinbased_ctls_high &=
3489 PIN_BASED_EXT_INTR_MASK |
3490 PIN_BASED_NMI_EXITING |
3491 PIN_BASED_VIRTUAL_NMIS |
3492 (apicv ? PIN_BASED_POSTED_INTR : 0);
3493 msrs->pinbased_ctls_high |=
3494 PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR |
3495 PIN_BASED_VMX_PREEMPTION_TIMER;
3496
3497 /* exit controls */
3498 rdmsr(MSR_IA32_VMX_EXIT_CTLS,
3499 msrs->exit_ctls_low,
3500 msrs->exit_ctls_high);
3501 msrs->exit_ctls_low =
3502 VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR;
3503
3504 msrs->exit_ctls_high &=
3505 #ifdef CONFIG_X86_64
3506 VM_EXIT_HOST_ADDR_SPACE_SIZE |
3507 #endif
3508 VM_EXIT_LOAD_IA32_PAT | VM_EXIT_SAVE_IA32_PAT;
3509 msrs->exit_ctls_high |=
3510 VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR |
3511 VM_EXIT_LOAD_IA32_EFER | VM_EXIT_SAVE_IA32_EFER |
3512 VM_EXIT_SAVE_VMX_PREEMPTION_TIMER | VM_EXIT_ACK_INTR_ON_EXIT;
3513
3514 /* We support free control of debug control saving. */
3515 msrs->exit_ctls_low &= ~VM_EXIT_SAVE_DEBUG_CONTROLS;
3516
3517 /* entry controls */
3518 rdmsr(MSR_IA32_VMX_ENTRY_CTLS,
3519 msrs->entry_ctls_low,
3520 msrs->entry_ctls_high);
3521 msrs->entry_ctls_low =
3522 VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR;
3523 msrs->entry_ctls_high &=
3524 #ifdef CONFIG_X86_64
3525 VM_ENTRY_IA32E_MODE |
3526 #endif
3527 VM_ENTRY_LOAD_IA32_PAT;
3528 msrs->entry_ctls_high |=
3529 (VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR | VM_ENTRY_LOAD_IA32_EFER);
3530
3531 /* We support free control of debug control loading. */
3532 msrs->entry_ctls_low &= ~VM_ENTRY_LOAD_DEBUG_CONTROLS;
3533
3534 /* cpu-based controls */
3535 rdmsr(MSR_IA32_VMX_PROCBASED_CTLS,
3536 msrs->procbased_ctls_low,
3537 msrs->procbased_ctls_high);
3538 msrs->procbased_ctls_low =
3539 CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
3540 msrs->procbased_ctls_high &=
3541 CPU_BASED_VIRTUAL_INTR_PENDING |
3542 CPU_BASED_VIRTUAL_NMI_PENDING | CPU_BASED_USE_TSC_OFFSETING |
3543 CPU_BASED_HLT_EXITING | CPU_BASED_INVLPG_EXITING |
3544 CPU_BASED_MWAIT_EXITING | CPU_BASED_CR3_LOAD_EXITING |
3545 CPU_BASED_CR3_STORE_EXITING |
3546 #ifdef CONFIG_X86_64
3547 CPU_BASED_CR8_LOAD_EXITING | CPU_BASED_CR8_STORE_EXITING |
3548 #endif
3549 CPU_BASED_MOV_DR_EXITING | CPU_BASED_UNCOND_IO_EXITING |
3550 CPU_BASED_USE_IO_BITMAPS | CPU_BASED_MONITOR_TRAP_FLAG |
3551 CPU_BASED_MONITOR_EXITING | CPU_BASED_RDPMC_EXITING |
3552 CPU_BASED_RDTSC_EXITING | CPU_BASED_PAUSE_EXITING |
3553 CPU_BASED_TPR_SHADOW | CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
3554 /*
3555 * We can allow some features even when not supported by the
3556 * hardware. For example, L1 can specify an MSR bitmap - and we
3557 * can use it to avoid exits to L1 - even when L0 runs L2
3558 * without MSR bitmaps.
3559 */
3560 msrs->procbased_ctls_high |=
3561 CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR |
3562 CPU_BASED_USE_MSR_BITMAPS;
3563
3564 /* We support free control of CR3 access interception. */
3565 msrs->procbased_ctls_low &=
3566 ~(CPU_BASED_CR3_LOAD_EXITING | CPU_BASED_CR3_STORE_EXITING);
3567
3568 /*
3569 * secondary cpu-based controls. Do not include those that
3570 * depend on CPUID bits, they are added later by vmx_cpuid_update.
3571 */
3572 if (msrs->procbased_ctls_high & CPU_BASED_ACTIVATE_SECONDARY_CONTROLS)
3573 rdmsr(MSR_IA32_VMX_PROCBASED_CTLS2,
3574 msrs->secondary_ctls_low,
3575 msrs->secondary_ctls_high);
3576
3577 msrs->secondary_ctls_low = 0;
3578 msrs->secondary_ctls_high &=
3579 SECONDARY_EXEC_DESC |
3580 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
3581 SECONDARY_EXEC_APIC_REGISTER_VIRT |
3582 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY |
3583 SECONDARY_EXEC_WBINVD_EXITING;
3584
3585 /*
3586 * We can emulate "VMCS shadowing," even if the hardware
3587 * doesn't support it.
3588 */
3589 msrs->secondary_ctls_high |=
3590 SECONDARY_EXEC_SHADOW_VMCS;
3591
3592 if (enable_ept) {
3593 /* nested EPT: emulate EPT also to L1 */
3594 msrs->secondary_ctls_high |=
3595 SECONDARY_EXEC_ENABLE_EPT;
3596 msrs->ept_caps = VMX_EPT_PAGE_WALK_4_BIT |
3597 VMX_EPTP_WB_BIT | VMX_EPT_INVEPT_BIT;
3598 if (cpu_has_vmx_ept_execute_only())
3599 msrs->ept_caps |=
3600 VMX_EPT_EXECUTE_ONLY_BIT;
3601 msrs->ept_caps &= vmx_capability.ept;
3602 msrs->ept_caps |= VMX_EPT_EXTENT_GLOBAL_BIT |
3603 VMX_EPT_EXTENT_CONTEXT_BIT | VMX_EPT_2MB_PAGE_BIT |
3604 VMX_EPT_1GB_PAGE_BIT;
3605 if (enable_ept_ad_bits) {
3606 msrs->secondary_ctls_high |=
3607 SECONDARY_EXEC_ENABLE_PML;
3608 msrs->ept_caps |= VMX_EPT_AD_BIT;
3609 }
3610 }
3611
3612 if (cpu_has_vmx_vmfunc()) {
3613 msrs->secondary_ctls_high |=
3614 SECONDARY_EXEC_ENABLE_VMFUNC;
3615 /*
3616 * Advertise EPTP switching unconditionally
3617 * since we emulate it
3618 */
3619 if (enable_ept)
3620 msrs->vmfunc_controls =
3621 VMX_VMFUNC_EPTP_SWITCHING;
3622 }
3623
3624 /*
3625 * Old versions of KVM use the single-context version without
3626 * checking for support, so declare that it is supported even
3627 * though it is treated as global context. The alternative is
3628 * not failing the single-context invvpid, and it is worse.
3629 */
3630 if (enable_vpid) {
3631 msrs->secondary_ctls_high |=
3632 SECONDARY_EXEC_ENABLE_VPID;
3633 msrs->vpid_caps = VMX_VPID_INVVPID_BIT |
3634 VMX_VPID_EXTENT_SUPPORTED_MASK;
3635 }
3636
3637 if (enable_unrestricted_guest)
3638 msrs->secondary_ctls_high |=
3639 SECONDARY_EXEC_UNRESTRICTED_GUEST;
3640
3641 if (flexpriority_enabled)
3642 msrs->secondary_ctls_high |=
3643 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
3644
3645 /* miscellaneous data */
3646 rdmsr(MSR_IA32_VMX_MISC,
3647 msrs->misc_low,
3648 msrs->misc_high);
3649 msrs->misc_low &= VMX_MISC_SAVE_EFER_LMA;
3650 msrs->misc_low |=
3651 MSR_IA32_VMX_MISC_VMWRITE_SHADOW_RO_FIELDS |
3652 VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE |
3653 VMX_MISC_ACTIVITY_HLT;
3654 msrs->misc_high = 0;
3655
3656 /*
3657 * This MSR reports some information about VMX support. We
3658 * should return information about the VMX we emulate for the
3659 * guest, and the VMCS structure we give it - not about the
3660 * VMX support of the underlying hardware.
3661 */
3662 msrs->basic =
3663 VMCS12_REVISION |
3664 VMX_BASIC_TRUE_CTLS |
3665 ((u64)VMCS12_SIZE << VMX_BASIC_VMCS_SIZE_SHIFT) |
3666 (VMX_BASIC_MEM_TYPE_WB << VMX_BASIC_MEM_TYPE_SHIFT);
3667
3668 if (cpu_has_vmx_basic_inout())
3669 msrs->basic |= VMX_BASIC_INOUT;
3670
3671 /*
3672 * These MSRs specify bits which the guest must keep fixed on
3673 * while L1 is in VMXON mode (in L1's root mode, or running an L2).
3674 * We picked the standard core2 setting.
3675 */
3676 #define VMXON_CR0_ALWAYSON (X86_CR0_PE | X86_CR0_PG | X86_CR0_NE)
3677 #define VMXON_CR4_ALWAYSON X86_CR4_VMXE
3678 msrs->cr0_fixed0 = VMXON_CR0_ALWAYSON;
3679 msrs->cr4_fixed0 = VMXON_CR4_ALWAYSON;
3680
3681 /* These MSRs specify bits which the guest must keep fixed off. */
3682 rdmsrl(MSR_IA32_VMX_CR0_FIXED1, msrs->cr0_fixed1);
3683 rdmsrl(MSR_IA32_VMX_CR4_FIXED1, msrs->cr4_fixed1);
3684
3685 /* highest index: VMX_PREEMPTION_TIMER_VALUE */
3686 msrs->vmcs_enum = VMCS12_MAX_FIELD_INDEX << 1;
3687 }
3688
3689 /*
3690 * if fixed0[i] == 1: val[i] must be 1
3691 * if fixed1[i] == 0: val[i] must be 0
3692 */
fixed_bits_valid(u64 val,u64 fixed0,u64 fixed1)3693 static inline bool fixed_bits_valid(u64 val, u64 fixed0, u64 fixed1)
3694 {
3695 return ((val & fixed1) | fixed0) == val;
3696 }
3697
vmx_control_verify(u32 control,u32 low,u32 high)3698 static inline bool vmx_control_verify(u32 control, u32 low, u32 high)
3699 {
3700 return fixed_bits_valid(control, low, high);
3701 }
3702
vmx_control_msr(u32 low,u32 high)3703 static inline u64 vmx_control_msr(u32 low, u32 high)
3704 {
3705 return low | ((u64)high << 32);
3706 }
3707
is_bitwise_subset(u64 superset,u64 subset,u64 mask)3708 static bool is_bitwise_subset(u64 superset, u64 subset, u64 mask)
3709 {
3710 superset &= mask;
3711 subset &= mask;
3712
3713 return (superset | subset) == superset;
3714 }
3715
vmx_restore_vmx_basic(struct vcpu_vmx * vmx,u64 data)3716 static int vmx_restore_vmx_basic(struct vcpu_vmx *vmx, u64 data)
3717 {
3718 const u64 feature_and_reserved =
3719 /* feature (except bit 48; see below) */
3720 BIT_ULL(49) | BIT_ULL(54) | BIT_ULL(55) |
3721 /* reserved */
3722 BIT_ULL(31) | GENMASK_ULL(47, 45) | GENMASK_ULL(63, 56);
3723 u64 vmx_basic = vmx->nested.msrs.basic;
3724
3725 if (!is_bitwise_subset(vmx_basic, data, feature_and_reserved))
3726 return -EINVAL;
3727
3728 /*
3729 * KVM does not emulate a version of VMX that constrains physical
3730 * addresses of VMX structures (e.g. VMCS) to 32-bits.
3731 */
3732 if (data & BIT_ULL(48))
3733 return -EINVAL;
3734
3735 if (vmx_basic_vmcs_revision_id(vmx_basic) !=
3736 vmx_basic_vmcs_revision_id(data))
3737 return -EINVAL;
3738
3739 if (vmx_basic_vmcs_size(vmx_basic) > vmx_basic_vmcs_size(data))
3740 return -EINVAL;
3741
3742 vmx->nested.msrs.basic = data;
3743 return 0;
3744 }
3745
3746 static int
vmx_restore_control_msr(struct vcpu_vmx * vmx,u32 msr_index,u64 data)3747 vmx_restore_control_msr(struct vcpu_vmx *vmx, u32 msr_index, u64 data)
3748 {
3749 u64 supported;
3750 u32 *lowp, *highp;
3751
3752 switch (msr_index) {
3753 case MSR_IA32_VMX_TRUE_PINBASED_CTLS:
3754 lowp = &vmx->nested.msrs.pinbased_ctls_low;
3755 highp = &vmx->nested.msrs.pinbased_ctls_high;
3756 break;
3757 case MSR_IA32_VMX_TRUE_PROCBASED_CTLS:
3758 lowp = &vmx->nested.msrs.procbased_ctls_low;
3759 highp = &vmx->nested.msrs.procbased_ctls_high;
3760 break;
3761 case MSR_IA32_VMX_TRUE_EXIT_CTLS:
3762 lowp = &vmx->nested.msrs.exit_ctls_low;
3763 highp = &vmx->nested.msrs.exit_ctls_high;
3764 break;
3765 case MSR_IA32_VMX_TRUE_ENTRY_CTLS:
3766 lowp = &vmx->nested.msrs.entry_ctls_low;
3767 highp = &vmx->nested.msrs.entry_ctls_high;
3768 break;
3769 case MSR_IA32_VMX_PROCBASED_CTLS2:
3770 lowp = &vmx->nested.msrs.secondary_ctls_low;
3771 highp = &vmx->nested.msrs.secondary_ctls_high;
3772 break;
3773 default:
3774 BUG();
3775 }
3776
3777 supported = vmx_control_msr(*lowp, *highp);
3778
3779 /* Check must-be-1 bits are still 1. */
3780 if (!is_bitwise_subset(data, supported, GENMASK_ULL(31, 0)))
3781 return -EINVAL;
3782
3783 /* Check must-be-0 bits are still 0. */
3784 if (!is_bitwise_subset(supported, data, GENMASK_ULL(63, 32)))
3785 return -EINVAL;
3786
3787 *lowp = data;
3788 *highp = data >> 32;
3789 return 0;
3790 }
3791
vmx_restore_vmx_misc(struct vcpu_vmx * vmx,u64 data)3792 static int vmx_restore_vmx_misc(struct vcpu_vmx *vmx, u64 data)
3793 {
3794 const u64 feature_and_reserved_bits =
3795 /* feature */
3796 BIT_ULL(5) | GENMASK_ULL(8, 6) | BIT_ULL(14) | BIT_ULL(15) |
3797 BIT_ULL(28) | BIT_ULL(29) | BIT_ULL(30) |
3798 /* reserved */
3799 GENMASK_ULL(13, 9) | BIT_ULL(31);
3800 u64 vmx_misc;
3801
3802 vmx_misc = vmx_control_msr(vmx->nested.msrs.misc_low,
3803 vmx->nested.msrs.misc_high);
3804
3805 if (!is_bitwise_subset(vmx_misc, data, feature_and_reserved_bits))
3806 return -EINVAL;
3807
3808 if ((vmx->nested.msrs.pinbased_ctls_high &
3809 PIN_BASED_VMX_PREEMPTION_TIMER) &&
3810 vmx_misc_preemption_timer_rate(data) !=
3811 vmx_misc_preemption_timer_rate(vmx_misc))
3812 return -EINVAL;
3813
3814 if (vmx_misc_cr3_count(data) > vmx_misc_cr3_count(vmx_misc))
3815 return -EINVAL;
3816
3817 if (vmx_misc_max_msr(data) > vmx_misc_max_msr(vmx_misc))
3818 return -EINVAL;
3819
3820 if (vmx_misc_mseg_revid(data) != vmx_misc_mseg_revid(vmx_misc))
3821 return -EINVAL;
3822
3823 vmx->nested.msrs.misc_low = data;
3824 vmx->nested.msrs.misc_high = data >> 32;
3825
3826 /*
3827 * If L1 has read-only VM-exit information fields, use the
3828 * less permissive vmx_vmwrite_bitmap to specify write
3829 * permissions for the shadow VMCS.
3830 */
3831 if (enable_shadow_vmcs && !nested_cpu_has_vmwrite_any_field(&vmx->vcpu))
3832 vmcs_write64(VMWRITE_BITMAP, __pa(vmx_vmwrite_bitmap));
3833
3834 return 0;
3835 }
3836
vmx_restore_vmx_ept_vpid_cap(struct vcpu_vmx * vmx,u64 data)3837 static int vmx_restore_vmx_ept_vpid_cap(struct vcpu_vmx *vmx, u64 data)
3838 {
3839 u64 vmx_ept_vpid_cap;
3840
3841 vmx_ept_vpid_cap = vmx_control_msr(vmx->nested.msrs.ept_caps,
3842 vmx->nested.msrs.vpid_caps);
3843
3844 /* Every bit is either reserved or a feature bit. */
3845 if (!is_bitwise_subset(vmx_ept_vpid_cap, data, -1ULL))
3846 return -EINVAL;
3847
3848 vmx->nested.msrs.ept_caps = data;
3849 vmx->nested.msrs.vpid_caps = data >> 32;
3850 return 0;
3851 }
3852
vmx_restore_fixed0_msr(struct vcpu_vmx * vmx,u32 msr_index,u64 data)3853 static int vmx_restore_fixed0_msr(struct vcpu_vmx *vmx, u32 msr_index, u64 data)
3854 {
3855 u64 *msr;
3856
3857 switch (msr_index) {
3858 case MSR_IA32_VMX_CR0_FIXED0:
3859 msr = &vmx->nested.msrs.cr0_fixed0;
3860 break;
3861 case MSR_IA32_VMX_CR4_FIXED0:
3862 msr = &vmx->nested.msrs.cr4_fixed0;
3863 break;
3864 default:
3865 BUG();
3866 }
3867
3868 /*
3869 * 1 bits (which indicates bits which "must-be-1" during VMX operation)
3870 * must be 1 in the restored value.
3871 */
3872 if (!is_bitwise_subset(data, *msr, -1ULL))
3873 return -EINVAL;
3874
3875 *msr = data;
3876 return 0;
3877 }
3878
3879 /*
3880 * Called when userspace is restoring VMX MSRs.
3881 *
3882 * Returns 0 on success, non-0 otherwise.
3883 */
vmx_set_vmx_msr(struct kvm_vcpu * vcpu,u32 msr_index,u64 data)3884 static int vmx_set_vmx_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 data)
3885 {
3886 struct vcpu_vmx *vmx = to_vmx(vcpu);
3887
3888 /*
3889 * Don't allow changes to the VMX capability MSRs while the vCPU
3890 * is in VMX operation.
3891 */
3892 if (vmx->nested.vmxon)
3893 return -EBUSY;
3894
3895 switch (msr_index) {
3896 case MSR_IA32_VMX_BASIC:
3897 return vmx_restore_vmx_basic(vmx, data);
3898 case MSR_IA32_VMX_PINBASED_CTLS:
3899 case MSR_IA32_VMX_PROCBASED_CTLS:
3900 case MSR_IA32_VMX_EXIT_CTLS:
3901 case MSR_IA32_VMX_ENTRY_CTLS:
3902 /*
3903 * The "non-true" VMX capability MSRs are generated from the
3904 * "true" MSRs, so we do not support restoring them directly.
3905 *
3906 * If userspace wants to emulate VMX_BASIC[55]=0, userspace
3907 * should restore the "true" MSRs with the must-be-1 bits
3908 * set according to the SDM Vol 3. A.2 "RESERVED CONTROLS AND
3909 * DEFAULT SETTINGS".
3910 */
3911 return -EINVAL;
3912 case MSR_IA32_VMX_TRUE_PINBASED_CTLS:
3913 case MSR_IA32_VMX_TRUE_PROCBASED_CTLS:
3914 case MSR_IA32_VMX_TRUE_EXIT_CTLS:
3915 case MSR_IA32_VMX_TRUE_ENTRY_CTLS:
3916 case MSR_IA32_VMX_PROCBASED_CTLS2:
3917 return vmx_restore_control_msr(vmx, msr_index, data);
3918 case MSR_IA32_VMX_MISC:
3919 return vmx_restore_vmx_misc(vmx, data);
3920 case MSR_IA32_VMX_CR0_FIXED0:
3921 case MSR_IA32_VMX_CR4_FIXED0:
3922 return vmx_restore_fixed0_msr(vmx, msr_index, data);
3923 case MSR_IA32_VMX_CR0_FIXED1:
3924 case MSR_IA32_VMX_CR4_FIXED1:
3925 /*
3926 * These MSRs are generated based on the vCPU's CPUID, so we
3927 * do not support restoring them directly.
3928 */
3929 return -EINVAL;
3930 case MSR_IA32_VMX_EPT_VPID_CAP:
3931 return vmx_restore_vmx_ept_vpid_cap(vmx, data);
3932 case MSR_IA32_VMX_VMCS_ENUM:
3933 vmx->nested.msrs.vmcs_enum = data;
3934 return 0;
3935 default:
3936 /*
3937 * The rest of the VMX capability MSRs do not support restore.
3938 */
3939 return -EINVAL;
3940 }
3941 }
3942
3943 /* Returns 0 on success, non-0 otherwise. */
vmx_get_vmx_msr(struct nested_vmx_msrs * msrs,u32 msr_index,u64 * pdata)3944 static int vmx_get_vmx_msr(struct nested_vmx_msrs *msrs, u32 msr_index, u64 *pdata)
3945 {
3946 switch (msr_index) {
3947 case MSR_IA32_VMX_BASIC:
3948 *pdata = msrs->basic;
3949 break;
3950 case MSR_IA32_VMX_TRUE_PINBASED_CTLS:
3951 case MSR_IA32_VMX_PINBASED_CTLS:
3952 *pdata = vmx_control_msr(
3953 msrs->pinbased_ctls_low,
3954 msrs->pinbased_ctls_high);
3955 if (msr_index == MSR_IA32_VMX_PINBASED_CTLS)
3956 *pdata |= PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
3957 break;
3958 case MSR_IA32_VMX_TRUE_PROCBASED_CTLS:
3959 case MSR_IA32_VMX_PROCBASED_CTLS:
3960 *pdata = vmx_control_msr(
3961 msrs->procbased_ctls_low,
3962 msrs->procbased_ctls_high);
3963 if (msr_index == MSR_IA32_VMX_PROCBASED_CTLS)
3964 *pdata |= CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
3965 break;
3966 case MSR_IA32_VMX_TRUE_EXIT_CTLS:
3967 case MSR_IA32_VMX_EXIT_CTLS:
3968 *pdata = vmx_control_msr(
3969 msrs->exit_ctls_low,
3970 msrs->exit_ctls_high);
3971 if (msr_index == MSR_IA32_VMX_EXIT_CTLS)
3972 *pdata |= VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR;
3973 break;
3974 case MSR_IA32_VMX_TRUE_ENTRY_CTLS:
3975 case MSR_IA32_VMX_ENTRY_CTLS:
3976 *pdata = vmx_control_msr(
3977 msrs->entry_ctls_low,
3978 msrs->entry_ctls_high);
3979 if (msr_index == MSR_IA32_VMX_ENTRY_CTLS)
3980 *pdata |= VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR;
3981 break;
3982 case MSR_IA32_VMX_MISC:
3983 *pdata = vmx_control_msr(
3984 msrs->misc_low,
3985 msrs->misc_high);
3986 break;
3987 case MSR_IA32_VMX_CR0_FIXED0:
3988 *pdata = msrs->cr0_fixed0;
3989 break;
3990 case MSR_IA32_VMX_CR0_FIXED1:
3991 *pdata = msrs->cr0_fixed1;
3992 break;
3993 case MSR_IA32_VMX_CR4_FIXED0:
3994 *pdata = msrs->cr4_fixed0;
3995 break;
3996 case MSR_IA32_VMX_CR4_FIXED1:
3997 *pdata = msrs->cr4_fixed1;
3998 break;
3999 case MSR_IA32_VMX_VMCS_ENUM:
4000 *pdata = msrs->vmcs_enum;
4001 break;
4002 case MSR_IA32_VMX_PROCBASED_CTLS2:
4003 *pdata = vmx_control_msr(
4004 msrs->secondary_ctls_low,
4005 msrs->secondary_ctls_high);
4006 break;
4007 case MSR_IA32_VMX_EPT_VPID_CAP:
4008 *pdata = msrs->ept_caps |
4009 ((u64)msrs->vpid_caps << 32);
4010 break;
4011 case MSR_IA32_VMX_VMFUNC:
4012 *pdata = msrs->vmfunc_controls;
4013 break;
4014 default:
4015 return 1;
4016 }
4017
4018 return 0;
4019 }
4020
vmx_feature_control_msr_valid(struct kvm_vcpu * vcpu,uint64_t val)4021 static inline bool vmx_feature_control_msr_valid(struct kvm_vcpu *vcpu,
4022 uint64_t val)
4023 {
4024 uint64_t valid_bits = to_vmx(vcpu)->msr_ia32_feature_control_valid_bits;
4025
4026 return !(val & ~valid_bits);
4027 }
4028
vmx_get_msr_feature(struct kvm_msr_entry * msr)4029 static int vmx_get_msr_feature(struct kvm_msr_entry *msr)
4030 {
4031 switch (msr->index) {
4032 case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC:
4033 if (!nested)
4034 return 1;
4035 return vmx_get_vmx_msr(&vmcs_config.nested, msr->index, &msr->data);
4036 default:
4037 return 1;
4038 }
4039
4040 return 0;
4041 }
4042
4043 /*
4044 * Reads an msr value (of 'msr_index') into 'pdata'.
4045 * Returns 0 on success, non-0 otherwise.
4046 * Assumes vcpu_load() was already called.
4047 */
vmx_get_msr(struct kvm_vcpu * vcpu,struct msr_data * msr_info)4048 static int vmx_get_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
4049 {
4050 struct vcpu_vmx *vmx = to_vmx(vcpu);
4051 struct shared_msr_entry *msr;
4052
4053 switch (msr_info->index) {
4054 #ifdef CONFIG_X86_64
4055 case MSR_FS_BASE:
4056 msr_info->data = vmcs_readl(GUEST_FS_BASE);
4057 break;
4058 case MSR_GS_BASE:
4059 msr_info->data = vmcs_readl(GUEST_GS_BASE);
4060 break;
4061 case MSR_KERNEL_GS_BASE:
4062 msr_info->data = vmx_read_guest_kernel_gs_base(vmx);
4063 break;
4064 #endif
4065 case MSR_EFER:
4066 return kvm_get_msr_common(vcpu, msr_info);
4067 case MSR_IA32_SPEC_CTRL:
4068 if (!msr_info->host_initiated &&
4069 !guest_cpuid_has(vcpu, X86_FEATURE_SPEC_CTRL))
4070 return 1;
4071
4072 msr_info->data = to_vmx(vcpu)->spec_ctrl;
4073 break;
4074 case MSR_IA32_SYSENTER_CS:
4075 msr_info->data = vmcs_read32(GUEST_SYSENTER_CS);
4076 break;
4077 case MSR_IA32_SYSENTER_EIP:
4078 msr_info->data = vmcs_readl(GUEST_SYSENTER_EIP);
4079 break;
4080 case MSR_IA32_SYSENTER_ESP:
4081 msr_info->data = vmcs_readl(GUEST_SYSENTER_ESP);
4082 break;
4083 case MSR_IA32_BNDCFGS:
4084 if (!kvm_mpx_supported() ||
4085 (!msr_info->host_initiated &&
4086 !guest_cpuid_has(vcpu, X86_FEATURE_MPX)))
4087 return 1;
4088 msr_info->data = vmcs_read64(GUEST_BNDCFGS);
4089 break;
4090 case MSR_IA32_MCG_EXT_CTL:
4091 if (!msr_info->host_initiated &&
4092 !(vmx->msr_ia32_feature_control &
4093 FEATURE_CONTROL_LMCE))
4094 return 1;
4095 msr_info->data = vcpu->arch.mcg_ext_ctl;
4096 break;
4097 case MSR_IA32_FEATURE_CONTROL:
4098 msr_info->data = vmx->msr_ia32_feature_control;
4099 break;
4100 case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC:
4101 if (!nested_vmx_allowed(vcpu))
4102 return 1;
4103 return vmx_get_vmx_msr(&vmx->nested.msrs, msr_info->index,
4104 &msr_info->data);
4105 case MSR_IA32_XSS:
4106 if (!vmx_xsaves_supported() ||
4107 (!msr_info->host_initiated &&
4108 !(guest_cpuid_has(vcpu, X86_FEATURE_XSAVE) &&
4109 guest_cpuid_has(vcpu, X86_FEATURE_XSAVES))))
4110 return 1;
4111 msr_info->data = vcpu->arch.ia32_xss;
4112 break;
4113 case MSR_TSC_AUX:
4114 if (!msr_info->host_initiated &&
4115 !guest_cpuid_has(vcpu, X86_FEATURE_RDTSCP))
4116 return 1;
4117 /* Otherwise falls through */
4118 default:
4119 msr = find_msr_entry(vmx, msr_info->index);
4120 if (msr) {
4121 msr_info->data = msr->data;
4122 break;
4123 }
4124 return kvm_get_msr_common(vcpu, msr_info);
4125 }
4126
4127 return 0;
4128 }
4129
4130 static void vmx_leave_nested(struct kvm_vcpu *vcpu);
4131
4132 /*
4133 * Writes msr value into into the appropriate "register".
4134 * Returns 0 on success, non-0 otherwise.
4135 * Assumes vcpu_load() was already called.
4136 */
vmx_set_msr(struct kvm_vcpu * vcpu,struct msr_data * msr_info)4137 static int vmx_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
4138 {
4139 struct vcpu_vmx *vmx = to_vmx(vcpu);
4140 struct shared_msr_entry *msr;
4141 int ret = 0;
4142 u32 msr_index = msr_info->index;
4143 u64 data = msr_info->data;
4144
4145 switch (msr_index) {
4146 case MSR_EFER:
4147 ret = kvm_set_msr_common(vcpu, msr_info);
4148 break;
4149 #ifdef CONFIG_X86_64
4150 case MSR_FS_BASE:
4151 vmx_segment_cache_clear(vmx);
4152 vmcs_writel(GUEST_FS_BASE, data);
4153 break;
4154 case MSR_GS_BASE:
4155 vmx_segment_cache_clear(vmx);
4156 vmcs_writel(GUEST_GS_BASE, data);
4157 break;
4158 case MSR_KERNEL_GS_BASE:
4159 vmx_write_guest_kernel_gs_base(vmx, data);
4160 break;
4161 #endif
4162 case MSR_IA32_SYSENTER_CS:
4163 vmcs_write32(GUEST_SYSENTER_CS, data);
4164 break;
4165 case MSR_IA32_SYSENTER_EIP:
4166 vmcs_writel(GUEST_SYSENTER_EIP, data);
4167 break;
4168 case MSR_IA32_SYSENTER_ESP:
4169 vmcs_writel(GUEST_SYSENTER_ESP, data);
4170 break;
4171 case MSR_IA32_BNDCFGS:
4172 if (!kvm_mpx_supported() ||
4173 (!msr_info->host_initiated &&
4174 !guest_cpuid_has(vcpu, X86_FEATURE_MPX)))
4175 return 1;
4176 if (is_noncanonical_address(data & PAGE_MASK, vcpu) ||
4177 (data & MSR_IA32_BNDCFGS_RSVD))
4178 return 1;
4179 vmcs_write64(GUEST_BNDCFGS, data);
4180 break;
4181 case MSR_IA32_SPEC_CTRL:
4182 if (!msr_info->host_initiated &&
4183 !guest_cpuid_has(vcpu, X86_FEATURE_SPEC_CTRL))
4184 return 1;
4185
4186 /* The STIBP bit doesn't fault even if it's not advertised */
4187 if (data & ~(SPEC_CTRL_IBRS | SPEC_CTRL_STIBP | SPEC_CTRL_SSBD))
4188 return 1;
4189
4190 vmx->spec_ctrl = data;
4191
4192 if (!data)
4193 break;
4194
4195 /*
4196 * For non-nested:
4197 * When it's written (to non-zero) for the first time, pass
4198 * it through.
4199 *
4200 * For nested:
4201 * The handling of the MSR bitmap for L2 guests is done in
4202 * nested_vmx_merge_msr_bitmap. We should not touch the
4203 * vmcs02.msr_bitmap here since it gets completely overwritten
4204 * in the merging. We update the vmcs01 here for L1 as well
4205 * since it will end up touching the MSR anyway now.
4206 */
4207 vmx_disable_intercept_for_msr(vmx->vmcs01.msr_bitmap,
4208 MSR_IA32_SPEC_CTRL,
4209 MSR_TYPE_RW);
4210 break;
4211 case MSR_IA32_PRED_CMD:
4212 if (!msr_info->host_initiated &&
4213 !guest_cpuid_has(vcpu, X86_FEATURE_SPEC_CTRL))
4214 return 1;
4215
4216 if (data & ~PRED_CMD_IBPB)
4217 return 1;
4218
4219 if (!data)
4220 break;
4221
4222 wrmsrl(MSR_IA32_PRED_CMD, PRED_CMD_IBPB);
4223
4224 /*
4225 * For non-nested:
4226 * When it's written (to non-zero) for the first time, pass
4227 * it through.
4228 *
4229 * For nested:
4230 * The handling of the MSR bitmap for L2 guests is done in
4231 * nested_vmx_merge_msr_bitmap. We should not touch the
4232 * vmcs02.msr_bitmap here since it gets completely overwritten
4233 * in the merging.
4234 */
4235 vmx_disable_intercept_for_msr(vmx->vmcs01.msr_bitmap, MSR_IA32_PRED_CMD,
4236 MSR_TYPE_W);
4237 break;
4238 case MSR_IA32_CR_PAT:
4239 if (!kvm_pat_valid(data))
4240 return 1;
4241
4242 if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) {
4243 vmcs_write64(GUEST_IA32_PAT, data);
4244 vcpu->arch.pat = data;
4245 break;
4246 }
4247 ret = kvm_set_msr_common(vcpu, msr_info);
4248 break;
4249 case MSR_IA32_TSC_ADJUST:
4250 ret = kvm_set_msr_common(vcpu, msr_info);
4251 break;
4252 case MSR_IA32_MCG_EXT_CTL:
4253 if ((!msr_info->host_initiated &&
4254 !(to_vmx(vcpu)->msr_ia32_feature_control &
4255 FEATURE_CONTROL_LMCE)) ||
4256 (data & ~MCG_EXT_CTL_LMCE_EN))
4257 return 1;
4258 vcpu->arch.mcg_ext_ctl = data;
4259 break;
4260 case MSR_IA32_FEATURE_CONTROL:
4261 if (!vmx_feature_control_msr_valid(vcpu, data) ||
4262 (to_vmx(vcpu)->msr_ia32_feature_control &
4263 FEATURE_CONTROL_LOCKED && !msr_info->host_initiated))
4264 return 1;
4265 vmx->msr_ia32_feature_control = data;
4266 if (msr_info->host_initiated && data == 0)
4267 vmx_leave_nested(vcpu);
4268 break;
4269 case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC:
4270 if (!msr_info->host_initiated)
4271 return 1; /* they are read-only */
4272 if (!nested_vmx_allowed(vcpu))
4273 return 1;
4274 return vmx_set_vmx_msr(vcpu, msr_index, data);
4275 case MSR_IA32_XSS:
4276 if (!vmx_xsaves_supported() ||
4277 (!msr_info->host_initiated &&
4278 !(guest_cpuid_has(vcpu, X86_FEATURE_XSAVE) &&
4279 guest_cpuid_has(vcpu, X86_FEATURE_XSAVES))))
4280 return 1;
4281 /*
4282 * The only supported bit as of Skylake is bit 8, but
4283 * it is not supported on KVM.
4284 */
4285 if (data != 0)
4286 return 1;
4287 vcpu->arch.ia32_xss = data;
4288 if (vcpu->arch.ia32_xss != host_xss)
4289 add_atomic_switch_msr(vmx, MSR_IA32_XSS,
4290 vcpu->arch.ia32_xss, host_xss, false);
4291 else
4292 clear_atomic_switch_msr(vmx, MSR_IA32_XSS);
4293 break;
4294 case MSR_TSC_AUX:
4295 if (!msr_info->host_initiated &&
4296 !guest_cpuid_has(vcpu, X86_FEATURE_RDTSCP))
4297 return 1;
4298 /* Check reserved bit, higher 32 bits should be zero */
4299 if ((data >> 32) != 0)
4300 return 1;
4301 /* Otherwise falls through */
4302 default:
4303 msr = find_msr_entry(vmx, msr_index);
4304 if (msr) {
4305 u64 old_msr_data = msr->data;
4306 msr->data = data;
4307 if (msr - vmx->guest_msrs < vmx->save_nmsrs) {
4308 preempt_disable();
4309 ret = kvm_set_shared_msr(msr->index, msr->data,
4310 msr->mask);
4311 preempt_enable();
4312 if (ret)
4313 msr->data = old_msr_data;
4314 }
4315 break;
4316 }
4317 ret = kvm_set_msr_common(vcpu, msr_info);
4318 }
4319
4320 return ret;
4321 }
4322
vmx_cache_reg(struct kvm_vcpu * vcpu,enum kvm_reg reg)4323 static void vmx_cache_reg(struct kvm_vcpu *vcpu, enum kvm_reg reg)
4324 {
4325 __set_bit(reg, (unsigned long *)&vcpu->arch.regs_avail);
4326 switch (reg) {
4327 case VCPU_REGS_RSP:
4328 vcpu->arch.regs[VCPU_REGS_RSP] = vmcs_readl(GUEST_RSP);
4329 break;
4330 case VCPU_REGS_RIP:
4331 vcpu->arch.regs[VCPU_REGS_RIP] = vmcs_readl(GUEST_RIP);
4332 break;
4333 case VCPU_EXREG_PDPTR:
4334 if (enable_ept)
4335 ept_save_pdptrs(vcpu);
4336 break;
4337 default:
4338 break;
4339 }
4340 }
4341
cpu_has_kvm_support(void)4342 static __init int cpu_has_kvm_support(void)
4343 {
4344 return cpu_has_vmx();
4345 }
4346
vmx_disabled_by_bios(void)4347 static __init int vmx_disabled_by_bios(void)
4348 {
4349 u64 msr;
4350
4351 rdmsrl(MSR_IA32_FEATURE_CONTROL, msr);
4352 if (msr & FEATURE_CONTROL_LOCKED) {
4353 /* launched w/ TXT and VMX disabled */
4354 if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX)
4355 && tboot_enabled())
4356 return 1;
4357 /* launched w/o TXT and VMX only enabled w/ TXT */
4358 if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX)
4359 && (msr & FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX)
4360 && !tboot_enabled()) {
4361 printk(KERN_WARNING "kvm: disable TXT in the BIOS or "
4362 "activate TXT before enabling KVM\n");
4363 return 1;
4364 }
4365 /* launched w/o TXT and VMX disabled */
4366 if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX)
4367 && !tboot_enabled())
4368 return 1;
4369 }
4370
4371 return 0;
4372 }
4373
kvm_cpu_vmxon(u64 addr)4374 static void kvm_cpu_vmxon(u64 addr)
4375 {
4376 cr4_set_bits(X86_CR4_VMXE);
4377 intel_pt_handle_vmx(1);
4378
4379 asm volatile (ASM_VMX_VMXON_RAX
4380 : : "a"(&addr), "m"(addr)
4381 : "memory", "cc");
4382 }
4383
hardware_enable(void)4384 static int hardware_enable(void)
4385 {
4386 int cpu = raw_smp_processor_id();
4387 u64 phys_addr = __pa(per_cpu(vmxarea, cpu));
4388 u64 old, test_bits;
4389
4390 if (cr4_read_shadow() & X86_CR4_VMXE)
4391 return -EBUSY;
4392
4393 /*
4394 * This can happen if we hot-added a CPU but failed to allocate
4395 * VP assist page for it.
4396 */
4397 if (static_branch_unlikely(&enable_evmcs) &&
4398 !hv_get_vp_assist_page(cpu))
4399 return -EFAULT;
4400
4401 rdmsrl(MSR_IA32_FEATURE_CONTROL, old);
4402
4403 test_bits = FEATURE_CONTROL_LOCKED;
4404 test_bits |= FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
4405 if (tboot_enabled())
4406 test_bits |= FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX;
4407
4408 if ((old & test_bits) != test_bits) {
4409 /* enable and lock */
4410 wrmsrl(MSR_IA32_FEATURE_CONTROL, old | test_bits);
4411 }
4412 kvm_cpu_vmxon(phys_addr);
4413 if (enable_ept)
4414 ept_sync_global();
4415
4416 return 0;
4417 }
4418
vmclear_local_loaded_vmcss(void)4419 static void vmclear_local_loaded_vmcss(void)
4420 {
4421 int cpu = raw_smp_processor_id();
4422 struct loaded_vmcs *v, *n;
4423
4424 list_for_each_entry_safe(v, n, &per_cpu(loaded_vmcss_on_cpu, cpu),
4425 loaded_vmcss_on_cpu_link)
4426 __loaded_vmcs_clear(v);
4427 }
4428
4429
4430 /* Just like cpu_vmxoff(), but with the __kvm_handle_fault_on_reboot()
4431 * tricks.
4432 */
kvm_cpu_vmxoff(void)4433 static void kvm_cpu_vmxoff(void)
4434 {
4435 asm volatile (__ex(ASM_VMX_VMXOFF) : : : "cc");
4436
4437 intel_pt_handle_vmx(0);
4438 cr4_clear_bits(X86_CR4_VMXE);
4439 }
4440
hardware_disable(void)4441 static void hardware_disable(void)
4442 {
4443 vmclear_local_loaded_vmcss();
4444 kvm_cpu_vmxoff();
4445 }
4446
adjust_vmx_controls(u32 ctl_min,u32 ctl_opt,u32 msr,u32 * result)4447 static __init int adjust_vmx_controls(u32 ctl_min, u32 ctl_opt,
4448 u32 msr, u32 *result)
4449 {
4450 u32 vmx_msr_low, vmx_msr_high;
4451 u32 ctl = ctl_min | ctl_opt;
4452
4453 rdmsr(msr, vmx_msr_low, vmx_msr_high);
4454
4455 ctl &= vmx_msr_high; /* bit == 0 in high word ==> must be zero */
4456 ctl |= vmx_msr_low; /* bit == 1 in low word ==> must be one */
4457
4458 /* Ensure minimum (required) set of control bits are supported. */
4459 if (ctl_min & ~ctl)
4460 return -EIO;
4461
4462 *result = ctl;
4463 return 0;
4464 }
4465
allow_1_setting(u32 msr,u32 ctl)4466 static __init bool allow_1_setting(u32 msr, u32 ctl)
4467 {
4468 u32 vmx_msr_low, vmx_msr_high;
4469
4470 rdmsr(msr, vmx_msr_low, vmx_msr_high);
4471 return vmx_msr_high & ctl;
4472 }
4473
setup_vmcs_config(struct vmcs_config * vmcs_conf)4474 static __init int setup_vmcs_config(struct vmcs_config *vmcs_conf)
4475 {
4476 u32 vmx_msr_low, vmx_msr_high;
4477 u32 min, opt, min2, opt2;
4478 u32 _pin_based_exec_control = 0;
4479 u32 _cpu_based_exec_control = 0;
4480 u32 _cpu_based_2nd_exec_control = 0;
4481 u32 _vmexit_control = 0;
4482 u32 _vmentry_control = 0;
4483
4484 memset(vmcs_conf, 0, sizeof(*vmcs_conf));
4485 min = CPU_BASED_HLT_EXITING |
4486 #ifdef CONFIG_X86_64
4487 CPU_BASED_CR8_LOAD_EXITING |
4488 CPU_BASED_CR8_STORE_EXITING |
4489 #endif
4490 CPU_BASED_CR3_LOAD_EXITING |
4491 CPU_BASED_CR3_STORE_EXITING |
4492 CPU_BASED_UNCOND_IO_EXITING |
4493 CPU_BASED_MOV_DR_EXITING |
4494 CPU_BASED_USE_TSC_OFFSETING |
4495 CPU_BASED_MWAIT_EXITING |
4496 CPU_BASED_MONITOR_EXITING |
4497 CPU_BASED_INVLPG_EXITING |
4498 CPU_BASED_RDPMC_EXITING;
4499
4500 opt = CPU_BASED_TPR_SHADOW |
4501 CPU_BASED_USE_MSR_BITMAPS |
4502 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
4503 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PROCBASED_CTLS,
4504 &_cpu_based_exec_control) < 0)
4505 return -EIO;
4506 #ifdef CONFIG_X86_64
4507 if ((_cpu_based_exec_control & CPU_BASED_TPR_SHADOW))
4508 _cpu_based_exec_control &= ~CPU_BASED_CR8_LOAD_EXITING &
4509 ~CPU_BASED_CR8_STORE_EXITING;
4510 #endif
4511 if (_cpu_based_exec_control & CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) {
4512 min2 = 0;
4513 opt2 = SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
4514 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
4515 SECONDARY_EXEC_WBINVD_EXITING |
4516 SECONDARY_EXEC_ENABLE_VPID |
4517 SECONDARY_EXEC_ENABLE_EPT |
4518 SECONDARY_EXEC_UNRESTRICTED_GUEST |
4519 SECONDARY_EXEC_PAUSE_LOOP_EXITING |
4520 SECONDARY_EXEC_DESC |
4521 SECONDARY_EXEC_RDTSCP |
4522 SECONDARY_EXEC_ENABLE_INVPCID |
4523 SECONDARY_EXEC_APIC_REGISTER_VIRT |
4524 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY |
4525 SECONDARY_EXEC_SHADOW_VMCS |
4526 SECONDARY_EXEC_XSAVES |
4527 SECONDARY_EXEC_RDSEED_EXITING |
4528 SECONDARY_EXEC_RDRAND_EXITING |
4529 SECONDARY_EXEC_ENABLE_PML |
4530 SECONDARY_EXEC_TSC_SCALING |
4531 SECONDARY_EXEC_ENABLE_VMFUNC |
4532 SECONDARY_EXEC_ENCLS_EXITING;
4533 if (adjust_vmx_controls(min2, opt2,
4534 MSR_IA32_VMX_PROCBASED_CTLS2,
4535 &_cpu_based_2nd_exec_control) < 0)
4536 return -EIO;
4537 }
4538 #ifndef CONFIG_X86_64
4539 if (!(_cpu_based_2nd_exec_control &
4540 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES))
4541 _cpu_based_exec_control &= ~CPU_BASED_TPR_SHADOW;
4542 #endif
4543
4544 if (!(_cpu_based_exec_control & CPU_BASED_TPR_SHADOW))
4545 _cpu_based_2nd_exec_control &= ~(
4546 SECONDARY_EXEC_APIC_REGISTER_VIRT |
4547 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
4548 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
4549
4550 rdmsr_safe(MSR_IA32_VMX_EPT_VPID_CAP,
4551 &vmx_capability.ept, &vmx_capability.vpid);
4552
4553 if (_cpu_based_2nd_exec_control & SECONDARY_EXEC_ENABLE_EPT) {
4554 /* CR3 accesses and invlpg don't need to cause VM Exits when EPT
4555 enabled */
4556 _cpu_based_exec_control &= ~(CPU_BASED_CR3_LOAD_EXITING |
4557 CPU_BASED_CR3_STORE_EXITING |
4558 CPU_BASED_INVLPG_EXITING);
4559 } else if (vmx_capability.ept) {
4560 vmx_capability.ept = 0;
4561 pr_warn_once("EPT CAP should not exist if not support "
4562 "1-setting enable EPT VM-execution control\n");
4563 }
4564 if (!(_cpu_based_2nd_exec_control & SECONDARY_EXEC_ENABLE_VPID) &&
4565 vmx_capability.vpid) {
4566 vmx_capability.vpid = 0;
4567 pr_warn_once("VPID CAP should not exist if not support "
4568 "1-setting enable VPID VM-execution control\n");
4569 }
4570
4571 min = VM_EXIT_SAVE_DEBUG_CONTROLS | VM_EXIT_ACK_INTR_ON_EXIT;
4572 #ifdef CONFIG_X86_64
4573 min |= VM_EXIT_HOST_ADDR_SPACE_SIZE;
4574 #endif
4575 opt = VM_EXIT_SAVE_IA32_PAT | VM_EXIT_LOAD_IA32_PAT |
4576 VM_EXIT_CLEAR_BNDCFGS;
4577 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_EXIT_CTLS,
4578 &_vmexit_control) < 0)
4579 return -EIO;
4580
4581 min = PIN_BASED_EXT_INTR_MASK | PIN_BASED_NMI_EXITING;
4582 opt = PIN_BASED_VIRTUAL_NMIS | PIN_BASED_POSTED_INTR |
4583 PIN_BASED_VMX_PREEMPTION_TIMER;
4584 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PINBASED_CTLS,
4585 &_pin_based_exec_control) < 0)
4586 return -EIO;
4587
4588 if (cpu_has_broken_vmx_preemption_timer())
4589 _pin_based_exec_control &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
4590 if (!(_cpu_based_2nd_exec_control &
4591 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY))
4592 _pin_based_exec_control &= ~PIN_BASED_POSTED_INTR;
4593
4594 min = VM_ENTRY_LOAD_DEBUG_CONTROLS;
4595 opt = VM_ENTRY_LOAD_IA32_PAT | VM_ENTRY_LOAD_BNDCFGS;
4596 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_ENTRY_CTLS,
4597 &_vmentry_control) < 0)
4598 return -EIO;
4599
4600 rdmsr(MSR_IA32_VMX_BASIC, vmx_msr_low, vmx_msr_high);
4601
4602 /* IA-32 SDM Vol 3B: VMCS size is never greater than 4kB. */
4603 if ((vmx_msr_high & 0x1fff) > PAGE_SIZE)
4604 return -EIO;
4605
4606 #ifdef CONFIG_X86_64
4607 /* IA-32 SDM Vol 3B: 64-bit CPUs always have VMX_BASIC_MSR[48]==0. */
4608 if (vmx_msr_high & (1u<<16))
4609 return -EIO;
4610 #endif
4611
4612 /* Require Write-Back (WB) memory type for VMCS accesses. */
4613 if (((vmx_msr_high >> 18) & 15) != 6)
4614 return -EIO;
4615
4616 vmcs_conf->size = vmx_msr_high & 0x1fff;
4617 vmcs_conf->order = get_order(vmcs_conf->size);
4618 vmcs_conf->basic_cap = vmx_msr_high & ~0x1fff;
4619
4620 vmcs_conf->revision_id = vmx_msr_low;
4621
4622 vmcs_conf->pin_based_exec_ctrl = _pin_based_exec_control;
4623 vmcs_conf->cpu_based_exec_ctrl = _cpu_based_exec_control;
4624 vmcs_conf->cpu_based_2nd_exec_ctrl = _cpu_based_2nd_exec_control;
4625 vmcs_conf->vmexit_ctrl = _vmexit_control;
4626 vmcs_conf->vmentry_ctrl = _vmentry_control;
4627
4628 if (static_branch_unlikely(&enable_evmcs))
4629 evmcs_sanitize_exec_ctrls(vmcs_conf);
4630
4631 cpu_has_load_ia32_efer =
4632 allow_1_setting(MSR_IA32_VMX_ENTRY_CTLS,
4633 VM_ENTRY_LOAD_IA32_EFER)
4634 && allow_1_setting(MSR_IA32_VMX_EXIT_CTLS,
4635 VM_EXIT_LOAD_IA32_EFER);
4636
4637 cpu_has_load_perf_global_ctrl =
4638 allow_1_setting(MSR_IA32_VMX_ENTRY_CTLS,
4639 VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL)
4640 && allow_1_setting(MSR_IA32_VMX_EXIT_CTLS,
4641 VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL);
4642
4643 /*
4644 * Some cpus support VM_ENTRY_(LOAD|SAVE)_IA32_PERF_GLOBAL_CTRL
4645 * but due to errata below it can't be used. Workaround is to use
4646 * msr load mechanism to switch IA32_PERF_GLOBAL_CTRL.
4647 *
4648 * VM Exit May Incorrectly Clear IA32_PERF_GLOBAL_CTRL [34:32]
4649 *
4650 * AAK155 (model 26)
4651 * AAP115 (model 30)
4652 * AAT100 (model 37)
4653 * BC86,AAY89,BD102 (model 44)
4654 * BA97 (model 46)
4655 *
4656 */
4657 if (cpu_has_load_perf_global_ctrl && boot_cpu_data.x86 == 0x6) {
4658 switch (boot_cpu_data.x86_model) {
4659 case 26:
4660 case 30:
4661 case 37:
4662 case 44:
4663 case 46:
4664 cpu_has_load_perf_global_ctrl = false;
4665 printk_once(KERN_WARNING"kvm: VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL "
4666 "does not work properly. Using workaround\n");
4667 break;
4668 default:
4669 break;
4670 }
4671 }
4672
4673 if (boot_cpu_has(X86_FEATURE_XSAVES))
4674 rdmsrl(MSR_IA32_XSS, host_xss);
4675
4676 return 0;
4677 }
4678
alloc_vmcs_cpu(bool shadow,int cpu)4679 static struct vmcs *alloc_vmcs_cpu(bool shadow, int cpu)
4680 {
4681 int node = cpu_to_node(cpu);
4682 struct page *pages;
4683 struct vmcs *vmcs;
4684
4685 pages = __alloc_pages_node(node, GFP_KERNEL, vmcs_config.order);
4686 if (!pages)
4687 return NULL;
4688 vmcs = page_address(pages);
4689 memset(vmcs, 0, vmcs_config.size);
4690
4691 /* KVM supports Enlightened VMCS v1 only */
4692 if (static_branch_unlikely(&enable_evmcs))
4693 vmcs->hdr.revision_id = KVM_EVMCS_VERSION;
4694 else
4695 vmcs->hdr.revision_id = vmcs_config.revision_id;
4696
4697 if (shadow)
4698 vmcs->hdr.shadow_vmcs = 1;
4699 return vmcs;
4700 }
4701
free_vmcs(struct vmcs * vmcs)4702 static void free_vmcs(struct vmcs *vmcs)
4703 {
4704 free_pages((unsigned long)vmcs, vmcs_config.order);
4705 }
4706
4707 /*
4708 * Free a VMCS, but before that VMCLEAR it on the CPU where it was last loaded
4709 */
free_loaded_vmcs(struct loaded_vmcs * loaded_vmcs)4710 static void free_loaded_vmcs(struct loaded_vmcs *loaded_vmcs)
4711 {
4712 if (!loaded_vmcs->vmcs)
4713 return;
4714 loaded_vmcs_clear(loaded_vmcs);
4715 free_vmcs(loaded_vmcs->vmcs);
4716 loaded_vmcs->vmcs = NULL;
4717 if (loaded_vmcs->msr_bitmap)
4718 free_page((unsigned long)loaded_vmcs->msr_bitmap);
4719 WARN_ON(loaded_vmcs->shadow_vmcs != NULL);
4720 }
4721
alloc_vmcs(bool shadow)4722 static struct vmcs *alloc_vmcs(bool shadow)
4723 {
4724 return alloc_vmcs_cpu(shadow, raw_smp_processor_id());
4725 }
4726
alloc_loaded_vmcs(struct loaded_vmcs * loaded_vmcs)4727 static int alloc_loaded_vmcs(struct loaded_vmcs *loaded_vmcs)
4728 {
4729 loaded_vmcs->vmcs = alloc_vmcs(false);
4730 if (!loaded_vmcs->vmcs)
4731 return -ENOMEM;
4732
4733 loaded_vmcs->shadow_vmcs = NULL;
4734 loaded_vmcs_init(loaded_vmcs);
4735
4736 if (cpu_has_vmx_msr_bitmap()) {
4737 loaded_vmcs->msr_bitmap = (unsigned long *)__get_free_page(GFP_KERNEL);
4738 if (!loaded_vmcs->msr_bitmap)
4739 goto out_vmcs;
4740 memset(loaded_vmcs->msr_bitmap, 0xff, PAGE_SIZE);
4741
4742 if (IS_ENABLED(CONFIG_HYPERV) &&
4743 static_branch_unlikely(&enable_evmcs) &&
4744 (ms_hyperv.nested_features & HV_X64_NESTED_MSR_BITMAP)) {
4745 struct hv_enlightened_vmcs *evmcs =
4746 (struct hv_enlightened_vmcs *)loaded_vmcs->vmcs;
4747
4748 evmcs->hv_enlightenments_control.msr_bitmap = 1;
4749 }
4750 }
4751
4752 memset(&loaded_vmcs->host_state, 0, sizeof(struct vmcs_host_state));
4753
4754 return 0;
4755
4756 out_vmcs:
4757 free_loaded_vmcs(loaded_vmcs);
4758 return -ENOMEM;
4759 }
4760
free_kvm_area(void)4761 static void free_kvm_area(void)
4762 {
4763 int cpu;
4764
4765 for_each_possible_cpu(cpu) {
4766 free_vmcs(per_cpu(vmxarea, cpu));
4767 per_cpu(vmxarea, cpu) = NULL;
4768 }
4769 }
4770
4771 enum vmcs_field_width {
4772 VMCS_FIELD_WIDTH_U16 = 0,
4773 VMCS_FIELD_WIDTH_U64 = 1,
4774 VMCS_FIELD_WIDTH_U32 = 2,
4775 VMCS_FIELD_WIDTH_NATURAL_WIDTH = 3
4776 };
4777
vmcs_field_width(unsigned long field)4778 static inline int vmcs_field_width(unsigned long field)
4779 {
4780 if (0x1 & field) /* the *_HIGH fields are all 32 bit */
4781 return VMCS_FIELD_WIDTH_U32;
4782 return (field >> 13) & 0x3 ;
4783 }
4784
vmcs_field_readonly(unsigned long field)4785 static inline int vmcs_field_readonly(unsigned long field)
4786 {
4787 return (((field >> 10) & 0x3) == 1);
4788 }
4789
init_vmcs_shadow_fields(void)4790 static void init_vmcs_shadow_fields(void)
4791 {
4792 int i, j;
4793
4794 for (i = j = 0; i < max_shadow_read_only_fields; i++) {
4795 u16 field = shadow_read_only_fields[i];
4796 if (vmcs_field_width(field) == VMCS_FIELD_WIDTH_U64 &&
4797 (i + 1 == max_shadow_read_only_fields ||
4798 shadow_read_only_fields[i + 1] != field + 1))
4799 pr_err("Missing field from shadow_read_only_field %x\n",
4800 field + 1);
4801
4802 clear_bit(field, vmx_vmread_bitmap);
4803 #ifdef CONFIG_X86_64
4804 if (field & 1)
4805 continue;
4806 #endif
4807 if (j < i)
4808 shadow_read_only_fields[j] = field;
4809 j++;
4810 }
4811 max_shadow_read_only_fields = j;
4812
4813 for (i = j = 0; i < max_shadow_read_write_fields; i++) {
4814 u16 field = shadow_read_write_fields[i];
4815 if (vmcs_field_width(field) == VMCS_FIELD_WIDTH_U64 &&
4816 (i + 1 == max_shadow_read_write_fields ||
4817 shadow_read_write_fields[i + 1] != field + 1))
4818 pr_err("Missing field from shadow_read_write_field %x\n",
4819 field + 1);
4820
4821 /*
4822 * PML and the preemption timer can be emulated, but the
4823 * processor cannot vmwrite to fields that don't exist
4824 * on bare metal.
4825 */
4826 switch (field) {
4827 case GUEST_PML_INDEX:
4828 if (!cpu_has_vmx_pml())
4829 continue;
4830 break;
4831 case VMX_PREEMPTION_TIMER_VALUE:
4832 if (!cpu_has_vmx_preemption_timer())
4833 continue;
4834 break;
4835 case GUEST_INTR_STATUS:
4836 if (!cpu_has_vmx_apicv())
4837 continue;
4838 break;
4839 default:
4840 break;
4841 }
4842
4843 clear_bit(field, vmx_vmwrite_bitmap);
4844 clear_bit(field, vmx_vmread_bitmap);
4845 #ifdef CONFIG_X86_64
4846 if (field & 1)
4847 continue;
4848 #endif
4849 if (j < i)
4850 shadow_read_write_fields[j] = field;
4851 j++;
4852 }
4853 max_shadow_read_write_fields = j;
4854 }
4855
alloc_kvm_area(void)4856 static __init int alloc_kvm_area(void)
4857 {
4858 int cpu;
4859
4860 for_each_possible_cpu(cpu) {
4861 struct vmcs *vmcs;
4862
4863 vmcs = alloc_vmcs_cpu(false, cpu);
4864 if (!vmcs) {
4865 free_kvm_area();
4866 return -ENOMEM;
4867 }
4868
4869 /*
4870 * When eVMCS is enabled, alloc_vmcs_cpu() sets
4871 * vmcs->revision_id to KVM_EVMCS_VERSION instead of
4872 * revision_id reported by MSR_IA32_VMX_BASIC.
4873 *
4874 * However, even though not explictly documented by
4875 * TLFS, VMXArea passed as VMXON argument should
4876 * still be marked with revision_id reported by
4877 * physical CPU.
4878 */
4879 if (static_branch_unlikely(&enable_evmcs))
4880 vmcs->hdr.revision_id = vmcs_config.revision_id;
4881
4882 per_cpu(vmxarea, cpu) = vmcs;
4883 }
4884 return 0;
4885 }
4886
fix_pmode_seg(struct kvm_vcpu * vcpu,int seg,struct kvm_segment * save)4887 static void fix_pmode_seg(struct kvm_vcpu *vcpu, int seg,
4888 struct kvm_segment *save)
4889 {
4890 if (!emulate_invalid_guest_state) {
4891 /*
4892 * CS and SS RPL should be equal during guest entry according
4893 * to VMX spec, but in reality it is not always so. Since vcpu
4894 * is in the middle of the transition from real mode to
4895 * protected mode it is safe to assume that RPL 0 is a good
4896 * default value.
4897 */
4898 if (seg == VCPU_SREG_CS || seg == VCPU_SREG_SS)
4899 save->selector &= ~SEGMENT_RPL_MASK;
4900 save->dpl = save->selector & SEGMENT_RPL_MASK;
4901 save->s = 1;
4902 }
4903 vmx_set_segment(vcpu, save, seg);
4904 }
4905
enter_pmode(struct kvm_vcpu * vcpu)4906 static void enter_pmode(struct kvm_vcpu *vcpu)
4907 {
4908 unsigned long flags;
4909 struct vcpu_vmx *vmx = to_vmx(vcpu);
4910
4911 /*
4912 * Update real mode segment cache. It may be not up-to-date if sement
4913 * register was written while vcpu was in a guest mode.
4914 */
4915 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_ES], VCPU_SREG_ES);
4916 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_DS], VCPU_SREG_DS);
4917 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_FS], VCPU_SREG_FS);
4918 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_GS], VCPU_SREG_GS);
4919 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_SS], VCPU_SREG_SS);
4920 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_CS], VCPU_SREG_CS);
4921
4922 vmx->rmode.vm86_active = 0;
4923
4924 vmx_segment_cache_clear(vmx);
4925
4926 vmx_set_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_TR], VCPU_SREG_TR);
4927
4928 flags = vmcs_readl(GUEST_RFLAGS);
4929 flags &= RMODE_GUEST_OWNED_EFLAGS_BITS;
4930 flags |= vmx->rmode.save_rflags & ~RMODE_GUEST_OWNED_EFLAGS_BITS;
4931 vmcs_writel(GUEST_RFLAGS, flags);
4932
4933 vmcs_writel(GUEST_CR4, (vmcs_readl(GUEST_CR4) & ~X86_CR4_VME) |
4934 (vmcs_readl(CR4_READ_SHADOW) & X86_CR4_VME));
4935
4936 update_exception_bitmap(vcpu);
4937
4938 fix_pmode_seg(vcpu, VCPU_SREG_CS, &vmx->rmode.segs[VCPU_SREG_CS]);
4939 fix_pmode_seg(vcpu, VCPU_SREG_SS, &vmx->rmode.segs[VCPU_SREG_SS]);
4940 fix_pmode_seg(vcpu, VCPU_SREG_ES, &vmx->rmode.segs[VCPU_SREG_ES]);
4941 fix_pmode_seg(vcpu, VCPU_SREG_DS, &vmx->rmode.segs[VCPU_SREG_DS]);
4942 fix_pmode_seg(vcpu, VCPU_SREG_FS, &vmx->rmode.segs[VCPU_SREG_FS]);
4943 fix_pmode_seg(vcpu, VCPU_SREG_GS, &vmx->rmode.segs[VCPU_SREG_GS]);
4944 }
4945
fix_rmode_seg(int seg,struct kvm_segment * save)4946 static void fix_rmode_seg(int seg, struct kvm_segment *save)
4947 {
4948 const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
4949 struct kvm_segment var = *save;
4950
4951 var.dpl = 0x3;
4952 if (seg == VCPU_SREG_CS)
4953 var.type = 0x3;
4954
4955 if (!emulate_invalid_guest_state) {
4956 var.selector = var.base >> 4;
4957 var.base = var.base & 0xffff0;
4958 var.limit = 0xffff;
4959 var.g = 0;
4960 var.db = 0;
4961 var.present = 1;
4962 var.s = 1;
4963 var.l = 0;
4964 var.unusable = 0;
4965 var.type = 0x3;
4966 var.avl = 0;
4967 if (save->base & 0xf)
4968 printk_once(KERN_WARNING "kvm: segment base is not "
4969 "paragraph aligned when entering "
4970 "protected mode (seg=%d)", seg);
4971 }
4972
4973 vmcs_write16(sf->selector, var.selector);
4974 vmcs_writel(sf->base, var.base);
4975 vmcs_write32(sf->limit, var.limit);
4976 vmcs_write32(sf->ar_bytes, vmx_segment_access_rights(&var));
4977 }
4978
enter_rmode(struct kvm_vcpu * vcpu)4979 static void enter_rmode(struct kvm_vcpu *vcpu)
4980 {
4981 unsigned long flags;
4982 struct vcpu_vmx *vmx = to_vmx(vcpu);
4983 struct kvm_vmx *kvm_vmx = to_kvm_vmx(vcpu->kvm);
4984
4985 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_TR], VCPU_SREG_TR);
4986 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_ES], VCPU_SREG_ES);
4987 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_DS], VCPU_SREG_DS);
4988 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_FS], VCPU_SREG_FS);
4989 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_GS], VCPU_SREG_GS);
4990 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_SS], VCPU_SREG_SS);
4991 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_CS], VCPU_SREG_CS);
4992
4993 vmx->rmode.vm86_active = 1;
4994
4995 /*
4996 * Very old userspace does not call KVM_SET_TSS_ADDR before entering
4997 * vcpu. Warn the user that an update is overdue.
4998 */
4999 if (!kvm_vmx->tss_addr)
5000 printk_once(KERN_WARNING "kvm: KVM_SET_TSS_ADDR need to be "
5001 "called before entering vcpu\n");
5002
5003 vmx_segment_cache_clear(vmx);
5004
5005 vmcs_writel(GUEST_TR_BASE, kvm_vmx->tss_addr);
5006 vmcs_write32(GUEST_TR_LIMIT, RMODE_TSS_SIZE - 1);
5007 vmcs_write32(GUEST_TR_AR_BYTES, 0x008b);
5008
5009 flags = vmcs_readl(GUEST_RFLAGS);
5010 vmx->rmode.save_rflags = flags;
5011
5012 flags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM;
5013
5014 vmcs_writel(GUEST_RFLAGS, flags);
5015 vmcs_writel(GUEST_CR4, vmcs_readl(GUEST_CR4) | X86_CR4_VME);
5016 update_exception_bitmap(vcpu);
5017
5018 fix_rmode_seg(VCPU_SREG_SS, &vmx->rmode.segs[VCPU_SREG_SS]);
5019 fix_rmode_seg(VCPU_SREG_CS, &vmx->rmode.segs[VCPU_SREG_CS]);
5020 fix_rmode_seg(VCPU_SREG_ES, &vmx->rmode.segs[VCPU_SREG_ES]);
5021 fix_rmode_seg(VCPU_SREG_DS, &vmx->rmode.segs[VCPU_SREG_DS]);
5022 fix_rmode_seg(VCPU_SREG_GS, &vmx->rmode.segs[VCPU_SREG_GS]);
5023 fix_rmode_seg(VCPU_SREG_FS, &vmx->rmode.segs[VCPU_SREG_FS]);
5024
5025 kvm_mmu_reset_context(vcpu);
5026 }
5027
vmx_set_efer(struct kvm_vcpu * vcpu,u64 efer)5028 static void vmx_set_efer(struct kvm_vcpu *vcpu, u64 efer)
5029 {
5030 struct vcpu_vmx *vmx = to_vmx(vcpu);
5031 struct shared_msr_entry *msr = find_msr_entry(vmx, MSR_EFER);
5032
5033 if (!msr)
5034 return;
5035
5036 vcpu->arch.efer = efer;
5037 if (efer & EFER_LMA) {
5038 vm_entry_controls_setbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
5039 msr->data = efer;
5040 } else {
5041 vm_entry_controls_clearbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
5042
5043 msr->data = efer & ~EFER_LME;
5044 }
5045 setup_msrs(vmx);
5046 }
5047
5048 #ifdef CONFIG_X86_64
5049
enter_lmode(struct kvm_vcpu * vcpu)5050 static void enter_lmode(struct kvm_vcpu *vcpu)
5051 {
5052 u32 guest_tr_ar;
5053
5054 vmx_segment_cache_clear(to_vmx(vcpu));
5055
5056 guest_tr_ar = vmcs_read32(GUEST_TR_AR_BYTES);
5057 if ((guest_tr_ar & VMX_AR_TYPE_MASK) != VMX_AR_TYPE_BUSY_64_TSS) {
5058 pr_debug_ratelimited("%s: tss fixup for long mode. \n",
5059 __func__);
5060 vmcs_write32(GUEST_TR_AR_BYTES,
5061 (guest_tr_ar & ~VMX_AR_TYPE_MASK)
5062 | VMX_AR_TYPE_BUSY_64_TSS);
5063 }
5064 vmx_set_efer(vcpu, vcpu->arch.efer | EFER_LMA);
5065 }
5066
exit_lmode(struct kvm_vcpu * vcpu)5067 static void exit_lmode(struct kvm_vcpu *vcpu)
5068 {
5069 vm_entry_controls_clearbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
5070 vmx_set_efer(vcpu, vcpu->arch.efer & ~EFER_LMA);
5071 }
5072
5073 #endif
5074
__vmx_flush_tlb(struct kvm_vcpu * vcpu,int vpid,bool invalidate_gpa)5075 static inline void __vmx_flush_tlb(struct kvm_vcpu *vcpu, int vpid,
5076 bool invalidate_gpa)
5077 {
5078 if (enable_ept && (invalidate_gpa || !enable_vpid)) {
5079 if (!VALID_PAGE(vcpu->arch.mmu.root_hpa))
5080 return;
5081 ept_sync_context(construct_eptp(vcpu, vcpu->arch.mmu.root_hpa));
5082 } else {
5083 vpid_sync_context(vpid);
5084 }
5085 }
5086
vmx_flush_tlb(struct kvm_vcpu * vcpu,bool invalidate_gpa)5087 static void vmx_flush_tlb(struct kvm_vcpu *vcpu, bool invalidate_gpa)
5088 {
5089 __vmx_flush_tlb(vcpu, to_vmx(vcpu)->vpid, invalidate_gpa);
5090 }
5091
vmx_flush_tlb_gva(struct kvm_vcpu * vcpu,gva_t addr)5092 static void vmx_flush_tlb_gva(struct kvm_vcpu *vcpu, gva_t addr)
5093 {
5094 int vpid = to_vmx(vcpu)->vpid;
5095
5096 if (!vpid_sync_vcpu_addr(vpid, addr))
5097 vpid_sync_context(vpid);
5098
5099 /*
5100 * If VPIDs are not supported or enabled, then the above is a no-op.
5101 * But we don't really need a TLB flush in that case anyway, because
5102 * each VM entry/exit includes an implicit flush when VPID is 0.
5103 */
5104 }
5105
vmx_decache_cr0_guest_bits(struct kvm_vcpu * vcpu)5106 static void vmx_decache_cr0_guest_bits(struct kvm_vcpu *vcpu)
5107 {
5108 ulong cr0_guest_owned_bits = vcpu->arch.cr0_guest_owned_bits;
5109
5110 vcpu->arch.cr0 &= ~cr0_guest_owned_bits;
5111 vcpu->arch.cr0 |= vmcs_readl(GUEST_CR0) & cr0_guest_owned_bits;
5112 }
5113
vmx_decache_cr3(struct kvm_vcpu * vcpu)5114 static void vmx_decache_cr3(struct kvm_vcpu *vcpu)
5115 {
5116 if (enable_unrestricted_guest || (enable_ept && is_paging(vcpu)))
5117 vcpu->arch.cr3 = vmcs_readl(GUEST_CR3);
5118 __set_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail);
5119 }
5120
vmx_decache_cr4_guest_bits(struct kvm_vcpu * vcpu)5121 static void vmx_decache_cr4_guest_bits(struct kvm_vcpu *vcpu)
5122 {
5123 ulong cr4_guest_owned_bits = vcpu->arch.cr4_guest_owned_bits;
5124
5125 vcpu->arch.cr4 &= ~cr4_guest_owned_bits;
5126 vcpu->arch.cr4 |= vmcs_readl(GUEST_CR4) & cr4_guest_owned_bits;
5127 }
5128
ept_load_pdptrs(struct kvm_vcpu * vcpu)5129 static void ept_load_pdptrs(struct kvm_vcpu *vcpu)
5130 {
5131 struct kvm_mmu *mmu = vcpu->arch.walk_mmu;
5132
5133 if (!test_bit(VCPU_EXREG_PDPTR,
5134 (unsigned long *)&vcpu->arch.regs_dirty))
5135 return;
5136
5137 if (is_pae_paging(vcpu)) {
5138 vmcs_write64(GUEST_PDPTR0, mmu->pdptrs[0]);
5139 vmcs_write64(GUEST_PDPTR1, mmu->pdptrs[1]);
5140 vmcs_write64(GUEST_PDPTR2, mmu->pdptrs[2]);
5141 vmcs_write64(GUEST_PDPTR3, mmu->pdptrs[3]);
5142 }
5143 }
5144
ept_save_pdptrs(struct kvm_vcpu * vcpu)5145 static void ept_save_pdptrs(struct kvm_vcpu *vcpu)
5146 {
5147 struct kvm_mmu *mmu = vcpu->arch.walk_mmu;
5148
5149 if (is_pae_paging(vcpu)) {
5150 mmu->pdptrs[0] = vmcs_read64(GUEST_PDPTR0);
5151 mmu->pdptrs[1] = vmcs_read64(GUEST_PDPTR1);
5152 mmu->pdptrs[2] = vmcs_read64(GUEST_PDPTR2);
5153 mmu->pdptrs[3] = vmcs_read64(GUEST_PDPTR3);
5154 }
5155
5156 __set_bit(VCPU_EXREG_PDPTR,
5157 (unsigned long *)&vcpu->arch.regs_avail);
5158 __set_bit(VCPU_EXREG_PDPTR,
5159 (unsigned long *)&vcpu->arch.regs_dirty);
5160 }
5161
nested_guest_cr0_valid(struct kvm_vcpu * vcpu,unsigned long val)5162 static bool nested_guest_cr0_valid(struct kvm_vcpu *vcpu, unsigned long val)
5163 {
5164 u64 fixed0 = to_vmx(vcpu)->nested.msrs.cr0_fixed0;
5165 u64 fixed1 = to_vmx(vcpu)->nested.msrs.cr0_fixed1;
5166 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
5167
5168 if (to_vmx(vcpu)->nested.msrs.secondary_ctls_high &
5169 SECONDARY_EXEC_UNRESTRICTED_GUEST &&
5170 nested_cpu_has2(vmcs12, SECONDARY_EXEC_UNRESTRICTED_GUEST))
5171 fixed0 &= ~(X86_CR0_PE | X86_CR0_PG);
5172
5173 return fixed_bits_valid(val, fixed0, fixed1);
5174 }
5175
nested_host_cr0_valid(struct kvm_vcpu * vcpu,unsigned long val)5176 static bool nested_host_cr0_valid(struct kvm_vcpu *vcpu, unsigned long val)
5177 {
5178 u64 fixed0 = to_vmx(vcpu)->nested.msrs.cr0_fixed0;
5179 u64 fixed1 = to_vmx(vcpu)->nested.msrs.cr0_fixed1;
5180
5181 return fixed_bits_valid(val, fixed0, fixed1);
5182 }
5183
nested_cr4_valid(struct kvm_vcpu * vcpu,unsigned long val)5184 static bool nested_cr4_valid(struct kvm_vcpu *vcpu, unsigned long val)
5185 {
5186 u64 fixed0 = to_vmx(vcpu)->nested.msrs.cr4_fixed0;
5187 u64 fixed1 = to_vmx(vcpu)->nested.msrs.cr4_fixed1;
5188
5189 return fixed_bits_valid(val, fixed0, fixed1);
5190 }
5191
5192 /* No difference in the restrictions on guest and host CR4 in VMX operation. */
5193 #define nested_guest_cr4_valid nested_cr4_valid
5194 #define nested_host_cr4_valid nested_cr4_valid
5195
5196 static int vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4);
5197
ept_update_paging_mode_cr0(unsigned long * hw_cr0,unsigned long cr0,struct kvm_vcpu * vcpu)5198 static void ept_update_paging_mode_cr0(unsigned long *hw_cr0,
5199 unsigned long cr0,
5200 struct kvm_vcpu *vcpu)
5201 {
5202 if (!test_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail))
5203 vmx_decache_cr3(vcpu);
5204 if (!(cr0 & X86_CR0_PG)) {
5205 /* From paging/starting to nonpaging */
5206 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL,
5207 vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) |
5208 (CPU_BASED_CR3_LOAD_EXITING |
5209 CPU_BASED_CR3_STORE_EXITING));
5210 vcpu->arch.cr0 = cr0;
5211 vmx_set_cr4(vcpu, kvm_read_cr4(vcpu));
5212 } else if (!is_paging(vcpu)) {
5213 /* From nonpaging to paging */
5214 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL,
5215 vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) &
5216 ~(CPU_BASED_CR3_LOAD_EXITING |
5217 CPU_BASED_CR3_STORE_EXITING));
5218 vcpu->arch.cr0 = cr0;
5219 vmx_set_cr4(vcpu, kvm_read_cr4(vcpu));
5220 }
5221
5222 if (!(cr0 & X86_CR0_WP))
5223 *hw_cr0 &= ~X86_CR0_WP;
5224 }
5225
vmx_set_cr0(struct kvm_vcpu * vcpu,unsigned long cr0)5226 static void vmx_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
5227 {
5228 struct vcpu_vmx *vmx = to_vmx(vcpu);
5229 unsigned long hw_cr0;
5230
5231 hw_cr0 = (cr0 & ~KVM_GUEST_CR0_MASK);
5232 if (enable_unrestricted_guest)
5233 hw_cr0 |= KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST;
5234 else {
5235 hw_cr0 |= KVM_VM_CR0_ALWAYS_ON;
5236
5237 if (vmx->rmode.vm86_active && (cr0 & X86_CR0_PE))
5238 enter_pmode(vcpu);
5239
5240 if (!vmx->rmode.vm86_active && !(cr0 & X86_CR0_PE))
5241 enter_rmode(vcpu);
5242 }
5243
5244 #ifdef CONFIG_X86_64
5245 if (vcpu->arch.efer & EFER_LME) {
5246 if (!is_paging(vcpu) && (cr0 & X86_CR0_PG))
5247 enter_lmode(vcpu);
5248 if (is_paging(vcpu) && !(cr0 & X86_CR0_PG))
5249 exit_lmode(vcpu);
5250 }
5251 #endif
5252
5253 if (enable_ept && !enable_unrestricted_guest)
5254 ept_update_paging_mode_cr0(&hw_cr0, cr0, vcpu);
5255
5256 vmcs_writel(CR0_READ_SHADOW, cr0);
5257 vmcs_writel(GUEST_CR0, hw_cr0);
5258 vcpu->arch.cr0 = cr0;
5259
5260 /* depends on vcpu->arch.cr0 to be set to a new value */
5261 vmx->emulation_required = emulation_required(vcpu);
5262 }
5263
get_ept_level(struct kvm_vcpu * vcpu)5264 static int get_ept_level(struct kvm_vcpu *vcpu)
5265 {
5266 /* Nested EPT currently only supports 4-level walks. */
5267 if (is_guest_mode(vcpu) && nested_cpu_has_ept(get_vmcs12(vcpu)))
5268 return 4;
5269 if (cpu_has_vmx_ept_5levels() && (cpuid_maxphyaddr(vcpu) > 48))
5270 return 5;
5271 return 4;
5272 }
5273
construct_eptp(struct kvm_vcpu * vcpu,unsigned long root_hpa)5274 static u64 construct_eptp(struct kvm_vcpu *vcpu, unsigned long root_hpa)
5275 {
5276 u64 eptp = VMX_EPTP_MT_WB;
5277
5278 eptp |= (get_ept_level(vcpu) == 5) ? VMX_EPTP_PWL_5 : VMX_EPTP_PWL_4;
5279
5280 if (enable_ept_ad_bits &&
5281 (!is_guest_mode(vcpu) || nested_ept_ad_enabled(vcpu)))
5282 eptp |= VMX_EPTP_AD_ENABLE_BIT;
5283 eptp |= (root_hpa & PAGE_MASK);
5284
5285 return eptp;
5286 }
5287
vmx_set_cr3(struct kvm_vcpu * vcpu,unsigned long cr3)5288 static void vmx_set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3)
5289 {
5290 struct kvm *kvm = vcpu->kvm;
5291 unsigned long guest_cr3;
5292 u64 eptp;
5293
5294 guest_cr3 = cr3;
5295 if (enable_ept) {
5296 eptp = construct_eptp(vcpu, cr3);
5297 vmcs_write64(EPT_POINTER, eptp);
5298
5299 if (kvm_x86_ops->tlb_remote_flush) {
5300 spin_lock(&to_kvm_vmx(kvm)->ept_pointer_lock);
5301 to_vmx(vcpu)->ept_pointer = eptp;
5302 to_kvm_vmx(kvm)->ept_pointers_match
5303 = EPT_POINTERS_CHECK;
5304 spin_unlock(&to_kvm_vmx(kvm)->ept_pointer_lock);
5305 }
5306
5307 if (enable_unrestricted_guest || is_paging(vcpu) ||
5308 is_guest_mode(vcpu))
5309 guest_cr3 = kvm_read_cr3(vcpu);
5310 else
5311 guest_cr3 = to_kvm_vmx(kvm)->ept_identity_map_addr;
5312 ept_load_pdptrs(vcpu);
5313 }
5314
5315 vmcs_writel(GUEST_CR3, guest_cr3);
5316 }
5317
vmx_set_cr4(struct kvm_vcpu * vcpu,unsigned long cr4)5318 static int vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
5319 {
5320 /*
5321 * Pass through host's Machine Check Enable value to hw_cr4, which
5322 * is in force while we are in guest mode. Do not let guests control
5323 * this bit, even if host CR4.MCE == 0.
5324 */
5325 unsigned long hw_cr4;
5326
5327 hw_cr4 = (cr4_read_shadow() & X86_CR4_MCE) | (cr4 & ~X86_CR4_MCE);
5328 if (enable_unrestricted_guest)
5329 hw_cr4 |= KVM_VM_CR4_ALWAYS_ON_UNRESTRICTED_GUEST;
5330 else if (to_vmx(vcpu)->rmode.vm86_active)
5331 hw_cr4 |= KVM_RMODE_VM_CR4_ALWAYS_ON;
5332 else
5333 hw_cr4 |= KVM_PMODE_VM_CR4_ALWAYS_ON;
5334
5335 if (!boot_cpu_has(X86_FEATURE_UMIP) && vmx_umip_emulated()) {
5336 if (cr4 & X86_CR4_UMIP) {
5337 vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL,
5338 SECONDARY_EXEC_DESC);
5339 hw_cr4 &= ~X86_CR4_UMIP;
5340 } else if (!is_guest_mode(vcpu) ||
5341 !nested_cpu_has2(get_vmcs12(vcpu), SECONDARY_EXEC_DESC))
5342 vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL,
5343 SECONDARY_EXEC_DESC);
5344 }
5345
5346 if (cr4 & X86_CR4_VMXE) {
5347 /*
5348 * To use VMXON (and later other VMX instructions), a guest
5349 * must first be able to turn on cr4.VMXE (see handle_vmon()).
5350 * So basically the check on whether to allow nested VMX
5351 * is here. We operate under the default treatment of SMM,
5352 * so VMX cannot be enabled under SMM.
5353 */
5354 if (!nested_vmx_allowed(vcpu) || is_smm(vcpu))
5355 return 1;
5356 }
5357
5358 if (to_vmx(vcpu)->nested.vmxon && !nested_cr4_valid(vcpu, cr4))
5359 return 1;
5360
5361 vcpu->arch.cr4 = cr4;
5362
5363 if (!enable_unrestricted_guest) {
5364 if (enable_ept) {
5365 if (!is_paging(vcpu)) {
5366 hw_cr4 &= ~X86_CR4_PAE;
5367 hw_cr4 |= X86_CR4_PSE;
5368 } else if (!(cr4 & X86_CR4_PAE)) {
5369 hw_cr4 &= ~X86_CR4_PAE;
5370 }
5371 }
5372
5373 /*
5374 * SMEP/SMAP/PKU is disabled if CPU is in non-paging mode in
5375 * hardware. To emulate this behavior, SMEP/SMAP/PKU needs
5376 * to be manually disabled when guest switches to non-paging
5377 * mode.
5378 *
5379 * If !enable_unrestricted_guest, the CPU is always running
5380 * with CR0.PG=1 and CR4 needs to be modified.
5381 * If enable_unrestricted_guest, the CPU automatically
5382 * disables SMEP/SMAP/PKU when the guest sets CR0.PG=0.
5383 */
5384 if (!is_paging(vcpu))
5385 hw_cr4 &= ~(X86_CR4_SMEP | X86_CR4_SMAP | X86_CR4_PKE);
5386 }
5387
5388 vmcs_writel(CR4_READ_SHADOW, cr4);
5389 vmcs_writel(GUEST_CR4, hw_cr4);
5390 return 0;
5391 }
5392
vmx_get_segment(struct kvm_vcpu * vcpu,struct kvm_segment * var,int seg)5393 static void vmx_get_segment(struct kvm_vcpu *vcpu,
5394 struct kvm_segment *var, int seg)
5395 {
5396 struct vcpu_vmx *vmx = to_vmx(vcpu);
5397 u32 ar;
5398
5399 if (vmx->rmode.vm86_active && seg != VCPU_SREG_LDTR) {
5400 *var = vmx->rmode.segs[seg];
5401 if (seg == VCPU_SREG_TR
5402 || var->selector == vmx_read_guest_seg_selector(vmx, seg))
5403 return;
5404 var->base = vmx_read_guest_seg_base(vmx, seg);
5405 var->selector = vmx_read_guest_seg_selector(vmx, seg);
5406 return;
5407 }
5408 var->base = vmx_read_guest_seg_base(vmx, seg);
5409 var->limit = vmx_read_guest_seg_limit(vmx, seg);
5410 var->selector = vmx_read_guest_seg_selector(vmx, seg);
5411 ar = vmx_read_guest_seg_ar(vmx, seg);
5412 var->unusable = (ar >> 16) & 1;
5413 var->type = ar & 15;
5414 var->s = (ar >> 4) & 1;
5415 var->dpl = (ar >> 5) & 3;
5416 /*
5417 * Some userspaces do not preserve unusable property. Since usable
5418 * segment has to be present according to VMX spec we can use present
5419 * property to amend userspace bug by making unusable segment always
5420 * nonpresent. vmx_segment_access_rights() already marks nonpresent
5421 * segment as unusable.
5422 */
5423 var->present = !var->unusable;
5424 var->avl = (ar >> 12) & 1;
5425 var->l = (ar >> 13) & 1;
5426 var->db = (ar >> 14) & 1;
5427 var->g = (ar >> 15) & 1;
5428 }
5429
vmx_get_segment_base(struct kvm_vcpu * vcpu,int seg)5430 static u64 vmx_get_segment_base(struct kvm_vcpu *vcpu, int seg)
5431 {
5432 struct kvm_segment s;
5433
5434 if (to_vmx(vcpu)->rmode.vm86_active) {
5435 vmx_get_segment(vcpu, &s, seg);
5436 return s.base;
5437 }
5438 return vmx_read_guest_seg_base(to_vmx(vcpu), seg);
5439 }
5440
vmx_get_cpl(struct kvm_vcpu * vcpu)5441 static int vmx_get_cpl(struct kvm_vcpu *vcpu)
5442 {
5443 struct vcpu_vmx *vmx = to_vmx(vcpu);
5444
5445 if (unlikely(vmx->rmode.vm86_active))
5446 return 0;
5447 else {
5448 int ar = vmx_read_guest_seg_ar(vmx, VCPU_SREG_SS);
5449 return VMX_AR_DPL(ar);
5450 }
5451 }
5452
vmx_segment_access_rights(struct kvm_segment * var)5453 static u32 vmx_segment_access_rights(struct kvm_segment *var)
5454 {
5455 u32 ar;
5456
5457 if (var->unusable || !var->present)
5458 ar = 1 << 16;
5459 else {
5460 ar = var->type & 15;
5461 ar |= (var->s & 1) << 4;
5462 ar |= (var->dpl & 3) << 5;
5463 ar |= (var->present & 1) << 7;
5464 ar |= (var->avl & 1) << 12;
5465 ar |= (var->l & 1) << 13;
5466 ar |= (var->db & 1) << 14;
5467 ar |= (var->g & 1) << 15;
5468 }
5469
5470 return ar;
5471 }
5472
vmx_set_segment(struct kvm_vcpu * vcpu,struct kvm_segment * var,int seg)5473 static void vmx_set_segment(struct kvm_vcpu *vcpu,
5474 struct kvm_segment *var, int seg)
5475 {
5476 struct vcpu_vmx *vmx = to_vmx(vcpu);
5477 const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
5478
5479 vmx_segment_cache_clear(vmx);
5480
5481 if (vmx->rmode.vm86_active && seg != VCPU_SREG_LDTR) {
5482 vmx->rmode.segs[seg] = *var;
5483 if (seg == VCPU_SREG_TR)
5484 vmcs_write16(sf->selector, var->selector);
5485 else if (var->s)
5486 fix_rmode_seg(seg, &vmx->rmode.segs[seg]);
5487 goto out;
5488 }
5489
5490 vmcs_writel(sf->base, var->base);
5491 vmcs_write32(sf->limit, var->limit);
5492 vmcs_write16(sf->selector, var->selector);
5493
5494 /*
5495 * Fix the "Accessed" bit in AR field of segment registers for older
5496 * qemu binaries.
5497 * IA32 arch specifies that at the time of processor reset the
5498 * "Accessed" bit in the AR field of segment registers is 1. And qemu
5499 * is setting it to 0 in the userland code. This causes invalid guest
5500 * state vmexit when "unrestricted guest" mode is turned on.
5501 * Fix for this setup issue in cpu_reset is being pushed in the qemu
5502 * tree. Newer qemu binaries with that qemu fix would not need this
5503 * kvm hack.
5504 */
5505 if (enable_unrestricted_guest && (seg != VCPU_SREG_LDTR))
5506 var->type |= 0x1; /* Accessed */
5507
5508 vmcs_write32(sf->ar_bytes, vmx_segment_access_rights(var));
5509
5510 out:
5511 vmx->emulation_required = emulation_required(vcpu);
5512 }
5513
vmx_get_cs_db_l_bits(struct kvm_vcpu * vcpu,int * db,int * l)5514 static void vmx_get_cs_db_l_bits(struct kvm_vcpu *vcpu, int *db, int *l)
5515 {
5516 u32 ar = vmx_read_guest_seg_ar(to_vmx(vcpu), VCPU_SREG_CS);
5517
5518 *db = (ar >> 14) & 1;
5519 *l = (ar >> 13) & 1;
5520 }
5521
vmx_get_idt(struct kvm_vcpu * vcpu,struct desc_ptr * dt)5522 static void vmx_get_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
5523 {
5524 dt->size = vmcs_read32(GUEST_IDTR_LIMIT);
5525 dt->address = vmcs_readl(GUEST_IDTR_BASE);
5526 }
5527
vmx_set_idt(struct kvm_vcpu * vcpu,struct desc_ptr * dt)5528 static void vmx_set_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
5529 {
5530 vmcs_write32(GUEST_IDTR_LIMIT, dt->size);
5531 vmcs_writel(GUEST_IDTR_BASE, dt->address);
5532 }
5533
vmx_get_gdt(struct kvm_vcpu * vcpu,struct desc_ptr * dt)5534 static void vmx_get_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
5535 {
5536 dt->size = vmcs_read32(GUEST_GDTR_LIMIT);
5537 dt->address = vmcs_readl(GUEST_GDTR_BASE);
5538 }
5539
vmx_set_gdt(struct kvm_vcpu * vcpu,struct desc_ptr * dt)5540 static void vmx_set_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
5541 {
5542 vmcs_write32(GUEST_GDTR_LIMIT, dt->size);
5543 vmcs_writel(GUEST_GDTR_BASE, dt->address);
5544 }
5545
rmode_segment_valid(struct kvm_vcpu * vcpu,int seg)5546 static bool rmode_segment_valid(struct kvm_vcpu *vcpu, int seg)
5547 {
5548 struct kvm_segment var;
5549 u32 ar;
5550
5551 vmx_get_segment(vcpu, &var, seg);
5552 var.dpl = 0x3;
5553 if (seg == VCPU_SREG_CS)
5554 var.type = 0x3;
5555 ar = vmx_segment_access_rights(&var);
5556
5557 if (var.base != (var.selector << 4))
5558 return false;
5559 if (var.limit != 0xffff)
5560 return false;
5561 if (ar != 0xf3)
5562 return false;
5563
5564 return true;
5565 }
5566
code_segment_valid(struct kvm_vcpu * vcpu)5567 static bool code_segment_valid(struct kvm_vcpu *vcpu)
5568 {
5569 struct kvm_segment cs;
5570 unsigned int cs_rpl;
5571
5572 vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
5573 cs_rpl = cs.selector & SEGMENT_RPL_MASK;
5574
5575 if (cs.unusable)
5576 return false;
5577 if (~cs.type & (VMX_AR_TYPE_CODE_MASK|VMX_AR_TYPE_ACCESSES_MASK))
5578 return false;
5579 if (!cs.s)
5580 return false;
5581 if (cs.type & VMX_AR_TYPE_WRITEABLE_MASK) {
5582 if (cs.dpl > cs_rpl)
5583 return false;
5584 } else {
5585 if (cs.dpl != cs_rpl)
5586 return false;
5587 }
5588 if (!cs.present)
5589 return false;
5590
5591 /* TODO: Add Reserved field check, this'll require a new member in the kvm_segment_field structure */
5592 return true;
5593 }
5594
stack_segment_valid(struct kvm_vcpu * vcpu)5595 static bool stack_segment_valid(struct kvm_vcpu *vcpu)
5596 {
5597 struct kvm_segment ss;
5598 unsigned int ss_rpl;
5599
5600 vmx_get_segment(vcpu, &ss, VCPU_SREG_SS);
5601 ss_rpl = ss.selector & SEGMENT_RPL_MASK;
5602
5603 if (ss.unusable)
5604 return true;
5605 if (ss.type != 3 && ss.type != 7)
5606 return false;
5607 if (!ss.s)
5608 return false;
5609 if (ss.dpl != ss_rpl) /* DPL != RPL */
5610 return false;
5611 if (!ss.present)
5612 return false;
5613
5614 return true;
5615 }
5616
data_segment_valid(struct kvm_vcpu * vcpu,int seg)5617 static bool data_segment_valid(struct kvm_vcpu *vcpu, int seg)
5618 {
5619 struct kvm_segment var;
5620 unsigned int rpl;
5621
5622 vmx_get_segment(vcpu, &var, seg);
5623 rpl = var.selector & SEGMENT_RPL_MASK;
5624
5625 if (var.unusable)
5626 return true;
5627 if (!var.s)
5628 return false;
5629 if (!var.present)
5630 return false;
5631 if (~var.type & (VMX_AR_TYPE_CODE_MASK|VMX_AR_TYPE_WRITEABLE_MASK)) {
5632 if (var.dpl < rpl) /* DPL < RPL */
5633 return false;
5634 }
5635
5636 /* TODO: Add other members to kvm_segment_field to allow checking for other access
5637 * rights flags
5638 */
5639 return true;
5640 }
5641
tr_valid(struct kvm_vcpu * vcpu)5642 static bool tr_valid(struct kvm_vcpu *vcpu)
5643 {
5644 struct kvm_segment tr;
5645
5646 vmx_get_segment(vcpu, &tr, VCPU_SREG_TR);
5647
5648 if (tr.unusable)
5649 return false;
5650 if (tr.selector & SEGMENT_TI_MASK) /* TI = 1 */
5651 return false;
5652 if (tr.type != 3 && tr.type != 11) /* TODO: Check if guest is in IA32e mode */
5653 return false;
5654 if (!tr.present)
5655 return false;
5656
5657 return true;
5658 }
5659
ldtr_valid(struct kvm_vcpu * vcpu)5660 static bool ldtr_valid(struct kvm_vcpu *vcpu)
5661 {
5662 struct kvm_segment ldtr;
5663
5664 vmx_get_segment(vcpu, &ldtr, VCPU_SREG_LDTR);
5665
5666 if (ldtr.unusable)
5667 return true;
5668 if (ldtr.selector & SEGMENT_TI_MASK) /* TI = 1 */
5669 return false;
5670 if (ldtr.type != 2)
5671 return false;
5672 if (!ldtr.present)
5673 return false;
5674
5675 return true;
5676 }
5677
cs_ss_rpl_check(struct kvm_vcpu * vcpu)5678 static bool cs_ss_rpl_check(struct kvm_vcpu *vcpu)
5679 {
5680 struct kvm_segment cs, ss;
5681
5682 vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
5683 vmx_get_segment(vcpu, &ss, VCPU_SREG_SS);
5684
5685 return ((cs.selector & SEGMENT_RPL_MASK) ==
5686 (ss.selector & SEGMENT_RPL_MASK));
5687 }
5688
5689 static bool nested_vmx_check_io_bitmaps(struct kvm_vcpu *vcpu,
5690 unsigned int port, int size);
nested_vmx_exit_handled_io(struct kvm_vcpu * vcpu,struct vmcs12 * vmcs12)5691 static bool nested_vmx_exit_handled_io(struct kvm_vcpu *vcpu,
5692 struct vmcs12 *vmcs12)
5693 {
5694 unsigned long exit_qualification;
5695 unsigned short port;
5696 int size;
5697
5698 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_IO_BITMAPS))
5699 return nested_cpu_has(vmcs12, CPU_BASED_UNCOND_IO_EXITING);
5700
5701 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
5702
5703 port = exit_qualification >> 16;
5704 size = (exit_qualification & 7) + 1;
5705
5706 return nested_vmx_check_io_bitmaps(vcpu, port, size);
5707 }
5708
5709 /*
5710 * Check if guest state is valid. Returns true if valid, false if
5711 * not.
5712 * We assume that registers are always usable
5713 */
guest_state_valid(struct kvm_vcpu * vcpu)5714 static bool guest_state_valid(struct kvm_vcpu *vcpu)
5715 {
5716 if (enable_unrestricted_guest)
5717 return true;
5718
5719 /* real mode guest state checks */
5720 if (!is_protmode(vcpu) || (vmx_get_rflags(vcpu) & X86_EFLAGS_VM)) {
5721 if (!rmode_segment_valid(vcpu, VCPU_SREG_CS))
5722 return false;
5723 if (!rmode_segment_valid(vcpu, VCPU_SREG_SS))
5724 return false;
5725 if (!rmode_segment_valid(vcpu, VCPU_SREG_DS))
5726 return false;
5727 if (!rmode_segment_valid(vcpu, VCPU_SREG_ES))
5728 return false;
5729 if (!rmode_segment_valid(vcpu, VCPU_SREG_FS))
5730 return false;
5731 if (!rmode_segment_valid(vcpu, VCPU_SREG_GS))
5732 return false;
5733 } else {
5734 /* protected mode guest state checks */
5735 if (!cs_ss_rpl_check(vcpu))
5736 return false;
5737 if (!code_segment_valid(vcpu))
5738 return false;
5739 if (!stack_segment_valid(vcpu))
5740 return false;
5741 if (!data_segment_valid(vcpu, VCPU_SREG_DS))
5742 return false;
5743 if (!data_segment_valid(vcpu, VCPU_SREG_ES))
5744 return false;
5745 if (!data_segment_valid(vcpu, VCPU_SREG_FS))
5746 return false;
5747 if (!data_segment_valid(vcpu, VCPU_SREG_GS))
5748 return false;
5749 if (!tr_valid(vcpu))
5750 return false;
5751 if (!ldtr_valid(vcpu))
5752 return false;
5753 }
5754 /* TODO:
5755 * - Add checks on RIP
5756 * - Add checks on RFLAGS
5757 */
5758
5759 return true;
5760 }
5761
page_address_valid(struct kvm_vcpu * vcpu,gpa_t gpa)5762 static bool page_address_valid(struct kvm_vcpu *vcpu, gpa_t gpa)
5763 {
5764 return PAGE_ALIGNED(gpa) && !(gpa >> cpuid_maxphyaddr(vcpu));
5765 }
5766
init_rmode_tss(struct kvm * kvm)5767 static int init_rmode_tss(struct kvm *kvm)
5768 {
5769 gfn_t fn;
5770 u16 data = 0;
5771 int idx, r;
5772
5773 idx = srcu_read_lock(&kvm->srcu);
5774 fn = to_kvm_vmx(kvm)->tss_addr >> PAGE_SHIFT;
5775 r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE);
5776 if (r < 0)
5777 goto out;
5778 data = TSS_BASE_SIZE + TSS_REDIRECTION_SIZE;
5779 r = kvm_write_guest_page(kvm, fn++, &data,
5780 TSS_IOPB_BASE_OFFSET, sizeof(u16));
5781 if (r < 0)
5782 goto out;
5783 r = kvm_clear_guest_page(kvm, fn++, 0, PAGE_SIZE);
5784 if (r < 0)
5785 goto out;
5786 r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE);
5787 if (r < 0)
5788 goto out;
5789 data = ~0;
5790 r = kvm_write_guest_page(kvm, fn, &data,
5791 RMODE_TSS_SIZE - 2 * PAGE_SIZE - 1,
5792 sizeof(u8));
5793 out:
5794 srcu_read_unlock(&kvm->srcu, idx);
5795 return r;
5796 }
5797
init_rmode_identity_map(struct kvm * kvm)5798 static int init_rmode_identity_map(struct kvm *kvm)
5799 {
5800 struct kvm_vmx *kvm_vmx = to_kvm_vmx(kvm);
5801 int i, idx, r = 0;
5802 kvm_pfn_t identity_map_pfn;
5803 u32 tmp;
5804
5805 /* Protect kvm_vmx->ept_identity_pagetable_done. */
5806 mutex_lock(&kvm->slots_lock);
5807
5808 if (likely(kvm_vmx->ept_identity_pagetable_done))
5809 goto out2;
5810
5811 if (!kvm_vmx->ept_identity_map_addr)
5812 kvm_vmx->ept_identity_map_addr = VMX_EPT_IDENTITY_PAGETABLE_ADDR;
5813 identity_map_pfn = kvm_vmx->ept_identity_map_addr >> PAGE_SHIFT;
5814
5815 r = __x86_set_memory_region(kvm, IDENTITY_PAGETABLE_PRIVATE_MEMSLOT,
5816 kvm_vmx->ept_identity_map_addr, PAGE_SIZE);
5817 if (r < 0)
5818 goto out2;
5819
5820 idx = srcu_read_lock(&kvm->srcu);
5821 r = kvm_clear_guest_page(kvm, identity_map_pfn, 0, PAGE_SIZE);
5822 if (r < 0)
5823 goto out;
5824 /* Set up identity-mapping pagetable for EPT in real mode */
5825 for (i = 0; i < PT32_ENT_PER_PAGE; i++) {
5826 tmp = (i << 22) + (_PAGE_PRESENT | _PAGE_RW | _PAGE_USER |
5827 _PAGE_ACCESSED | _PAGE_DIRTY | _PAGE_PSE);
5828 r = kvm_write_guest_page(kvm, identity_map_pfn,
5829 &tmp, i * sizeof(tmp), sizeof(tmp));
5830 if (r < 0)
5831 goto out;
5832 }
5833 kvm_vmx->ept_identity_pagetable_done = true;
5834
5835 out:
5836 srcu_read_unlock(&kvm->srcu, idx);
5837
5838 out2:
5839 mutex_unlock(&kvm->slots_lock);
5840 return r;
5841 }
5842
seg_setup(int seg)5843 static void seg_setup(int seg)
5844 {
5845 const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
5846 unsigned int ar;
5847
5848 vmcs_write16(sf->selector, 0);
5849 vmcs_writel(sf->base, 0);
5850 vmcs_write32(sf->limit, 0xffff);
5851 ar = 0x93;
5852 if (seg == VCPU_SREG_CS)
5853 ar |= 0x08; /* code segment */
5854
5855 vmcs_write32(sf->ar_bytes, ar);
5856 }
5857
alloc_apic_access_page(struct kvm * kvm)5858 static int alloc_apic_access_page(struct kvm *kvm)
5859 {
5860 struct page *page;
5861 int r = 0;
5862
5863 mutex_lock(&kvm->slots_lock);
5864 if (kvm->arch.apic_access_page_done)
5865 goto out;
5866 r = __x86_set_memory_region(kvm, APIC_ACCESS_PAGE_PRIVATE_MEMSLOT,
5867 APIC_DEFAULT_PHYS_BASE, PAGE_SIZE);
5868 if (r)
5869 goto out;
5870
5871 page = gfn_to_page(kvm, APIC_DEFAULT_PHYS_BASE >> PAGE_SHIFT);
5872 if (is_error_page(page)) {
5873 r = -EFAULT;
5874 goto out;
5875 }
5876
5877 /*
5878 * Do not pin the page in memory, so that memory hot-unplug
5879 * is able to migrate it.
5880 */
5881 put_page(page);
5882 kvm->arch.apic_access_page_done = true;
5883 out:
5884 mutex_unlock(&kvm->slots_lock);
5885 return r;
5886 }
5887
allocate_vpid(void)5888 static int allocate_vpid(void)
5889 {
5890 int vpid;
5891
5892 if (!enable_vpid)
5893 return 0;
5894 spin_lock(&vmx_vpid_lock);
5895 vpid = find_first_zero_bit(vmx_vpid_bitmap, VMX_NR_VPIDS);
5896 if (vpid < VMX_NR_VPIDS)
5897 __set_bit(vpid, vmx_vpid_bitmap);
5898 else
5899 vpid = 0;
5900 spin_unlock(&vmx_vpid_lock);
5901 return vpid;
5902 }
5903
free_vpid(int vpid)5904 static void free_vpid(int vpid)
5905 {
5906 if (!enable_vpid || vpid == 0)
5907 return;
5908 spin_lock(&vmx_vpid_lock);
5909 __clear_bit(vpid, vmx_vpid_bitmap);
5910 spin_unlock(&vmx_vpid_lock);
5911 }
5912
vmx_disable_intercept_for_msr(unsigned long * msr_bitmap,u32 msr,int type)5913 static __always_inline void vmx_disable_intercept_for_msr(unsigned long *msr_bitmap,
5914 u32 msr, int type)
5915 {
5916 int f = sizeof(unsigned long);
5917
5918 if (!cpu_has_vmx_msr_bitmap())
5919 return;
5920
5921 if (static_branch_unlikely(&enable_evmcs))
5922 evmcs_touch_msr_bitmap();
5923
5924 /*
5925 * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
5926 * have the write-low and read-high bitmap offsets the wrong way round.
5927 * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
5928 */
5929 if (msr <= 0x1fff) {
5930 if (type & MSR_TYPE_R)
5931 /* read-low */
5932 __clear_bit(msr, msr_bitmap + 0x000 / f);
5933
5934 if (type & MSR_TYPE_W)
5935 /* write-low */
5936 __clear_bit(msr, msr_bitmap + 0x800 / f);
5937
5938 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
5939 msr &= 0x1fff;
5940 if (type & MSR_TYPE_R)
5941 /* read-high */
5942 __clear_bit(msr, msr_bitmap + 0x400 / f);
5943
5944 if (type & MSR_TYPE_W)
5945 /* write-high */
5946 __clear_bit(msr, msr_bitmap + 0xc00 / f);
5947
5948 }
5949 }
5950
vmx_enable_intercept_for_msr(unsigned long * msr_bitmap,u32 msr,int type)5951 static __always_inline void vmx_enable_intercept_for_msr(unsigned long *msr_bitmap,
5952 u32 msr, int type)
5953 {
5954 int f = sizeof(unsigned long);
5955
5956 if (!cpu_has_vmx_msr_bitmap())
5957 return;
5958
5959 if (static_branch_unlikely(&enable_evmcs))
5960 evmcs_touch_msr_bitmap();
5961
5962 /*
5963 * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
5964 * have the write-low and read-high bitmap offsets the wrong way round.
5965 * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
5966 */
5967 if (msr <= 0x1fff) {
5968 if (type & MSR_TYPE_R)
5969 /* read-low */
5970 __set_bit(msr, msr_bitmap + 0x000 / f);
5971
5972 if (type & MSR_TYPE_W)
5973 /* write-low */
5974 __set_bit(msr, msr_bitmap + 0x800 / f);
5975
5976 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
5977 msr &= 0x1fff;
5978 if (type & MSR_TYPE_R)
5979 /* read-high */
5980 __set_bit(msr, msr_bitmap + 0x400 / f);
5981
5982 if (type & MSR_TYPE_W)
5983 /* write-high */
5984 __set_bit(msr, msr_bitmap + 0xc00 / f);
5985
5986 }
5987 }
5988
vmx_set_intercept_for_msr(unsigned long * msr_bitmap,u32 msr,int type,bool value)5989 static __always_inline void vmx_set_intercept_for_msr(unsigned long *msr_bitmap,
5990 u32 msr, int type, bool value)
5991 {
5992 if (value)
5993 vmx_enable_intercept_for_msr(msr_bitmap, msr, type);
5994 else
5995 vmx_disable_intercept_for_msr(msr_bitmap, msr, type);
5996 }
5997
5998 /*
5999 * If a msr is allowed by L0, we should check whether it is allowed by L1.
6000 * The corresponding bit will be cleared unless both of L0 and L1 allow it.
6001 */
nested_vmx_disable_intercept_for_msr(unsigned long * msr_bitmap_l1,unsigned long * msr_bitmap_nested,u32 msr,int type)6002 static void nested_vmx_disable_intercept_for_msr(unsigned long *msr_bitmap_l1,
6003 unsigned long *msr_bitmap_nested,
6004 u32 msr, int type)
6005 {
6006 int f = sizeof(unsigned long);
6007
6008 /*
6009 * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
6010 * have the write-low and read-high bitmap offsets the wrong way round.
6011 * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
6012 */
6013 if (msr <= 0x1fff) {
6014 if (type & MSR_TYPE_R &&
6015 !test_bit(msr, msr_bitmap_l1 + 0x000 / f))
6016 /* read-low */
6017 __clear_bit(msr, msr_bitmap_nested + 0x000 / f);
6018
6019 if (type & MSR_TYPE_W &&
6020 !test_bit(msr, msr_bitmap_l1 + 0x800 / f))
6021 /* write-low */
6022 __clear_bit(msr, msr_bitmap_nested + 0x800 / f);
6023
6024 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
6025 msr &= 0x1fff;
6026 if (type & MSR_TYPE_R &&
6027 !test_bit(msr, msr_bitmap_l1 + 0x400 / f))
6028 /* read-high */
6029 __clear_bit(msr, msr_bitmap_nested + 0x400 / f);
6030
6031 if (type & MSR_TYPE_W &&
6032 !test_bit(msr, msr_bitmap_l1 + 0xc00 / f))
6033 /* write-high */
6034 __clear_bit(msr, msr_bitmap_nested + 0xc00 / f);
6035
6036 }
6037 }
6038
vmx_msr_bitmap_mode(struct kvm_vcpu * vcpu)6039 static u8 vmx_msr_bitmap_mode(struct kvm_vcpu *vcpu)
6040 {
6041 u8 mode = 0;
6042
6043 if (cpu_has_secondary_exec_ctrls() &&
6044 (vmcs_read32(SECONDARY_VM_EXEC_CONTROL) &
6045 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE)) {
6046 mode |= MSR_BITMAP_MODE_X2APIC;
6047 if (enable_apicv && kvm_vcpu_apicv_active(vcpu))
6048 mode |= MSR_BITMAP_MODE_X2APIC_APICV;
6049 }
6050
6051 return mode;
6052 }
6053
6054 #define X2APIC_MSR(r) (APIC_BASE_MSR + ((r) >> 4))
6055
vmx_update_msr_bitmap_x2apic(unsigned long * msr_bitmap,u8 mode)6056 static void vmx_update_msr_bitmap_x2apic(unsigned long *msr_bitmap,
6057 u8 mode)
6058 {
6059 int msr;
6060
6061 for (msr = 0x800; msr <= 0x8ff; msr += BITS_PER_LONG) {
6062 unsigned word = msr / BITS_PER_LONG;
6063 msr_bitmap[word] = (mode & MSR_BITMAP_MODE_X2APIC_APICV) ? 0 : ~0;
6064 msr_bitmap[word + (0x800 / sizeof(long))] = ~0;
6065 }
6066
6067 if (mode & MSR_BITMAP_MODE_X2APIC) {
6068 /*
6069 * TPR reads and writes can be virtualized even if virtual interrupt
6070 * delivery is not in use.
6071 */
6072 vmx_disable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_TASKPRI), MSR_TYPE_RW);
6073 if (mode & MSR_BITMAP_MODE_X2APIC_APICV) {
6074 vmx_enable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_TMCCT), MSR_TYPE_R);
6075 vmx_disable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_EOI), MSR_TYPE_W);
6076 vmx_disable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_SELF_IPI), MSR_TYPE_W);
6077 }
6078 }
6079 }
6080
vmx_update_msr_bitmap(struct kvm_vcpu * vcpu)6081 static void vmx_update_msr_bitmap(struct kvm_vcpu *vcpu)
6082 {
6083 struct vcpu_vmx *vmx = to_vmx(vcpu);
6084 unsigned long *msr_bitmap = vmx->vmcs01.msr_bitmap;
6085 u8 mode = vmx_msr_bitmap_mode(vcpu);
6086 u8 changed = mode ^ vmx->msr_bitmap_mode;
6087
6088 if (!changed)
6089 return;
6090
6091 if (changed & (MSR_BITMAP_MODE_X2APIC | MSR_BITMAP_MODE_X2APIC_APICV))
6092 vmx_update_msr_bitmap_x2apic(msr_bitmap, mode);
6093
6094 vmx->msr_bitmap_mode = mode;
6095 }
6096
vmx_get_enable_apicv(struct kvm_vcpu * vcpu)6097 static bool vmx_get_enable_apicv(struct kvm_vcpu *vcpu)
6098 {
6099 return enable_apicv;
6100 }
6101
nested_mark_vmcs12_pages_dirty(struct kvm_vcpu * vcpu)6102 static void nested_mark_vmcs12_pages_dirty(struct kvm_vcpu *vcpu)
6103 {
6104 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
6105 gfn_t gfn;
6106
6107 /*
6108 * Don't need to mark the APIC access page dirty; it is never
6109 * written to by the CPU during APIC virtualization.
6110 */
6111
6112 if (nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW)) {
6113 gfn = vmcs12->virtual_apic_page_addr >> PAGE_SHIFT;
6114 kvm_vcpu_mark_page_dirty(vcpu, gfn);
6115 }
6116
6117 if (nested_cpu_has_posted_intr(vmcs12)) {
6118 gfn = vmcs12->posted_intr_desc_addr >> PAGE_SHIFT;
6119 kvm_vcpu_mark_page_dirty(vcpu, gfn);
6120 }
6121 }
6122
6123
vmx_complete_nested_posted_interrupt(struct kvm_vcpu * vcpu)6124 static void vmx_complete_nested_posted_interrupt(struct kvm_vcpu *vcpu)
6125 {
6126 struct vcpu_vmx *vmx = to_vmx(vcpu);
6127 int max_irr;
6128 void *vapic_page;
6129 u16 status;
6130
6131 if (!vmx->nested.pi_desc || !vmx->nested.pi_pending)
6132 return;
6133
6134 vmx->nested.pi_pending = false;
6135 if (!pi_test_and_clear_on(vmx->nested.pi_desc))
6136 return;
6137
6138 max_irr = find_last_bit((unsigned long *)vmx->nested.pi_desc->pir, 256);
6139 if (max_irr != 256) {
6140 vapic_page = kmap(vmx->nested.virtual_apic_page);
6141 __kvm_apic_update_irr(vmx->nested.pi_desc->pir,
6142 vapic_page, &max_irr);
6143 kunmap(vmx->nested.virtual_apic_page);
6144
6145 status = vmcs_read16(GUEST_INTR_STATUS);
6146 if ((u8)max_irr > ((u8)status & 0xff)) {
6147 status &= ~0xff;
6148 status |= (u8)max_irr;
6149 vmcs_write16(GUEST_INTR_STATUS, status);
6150 }
6151 }
6152
6153 nested_mark_vmcs12_pages_dirty(vcpu);
6154 }
6155
vmx_get_rvi(void)6156 static u8 vmx_get_rvi(void)
6157 {
6158 return vmcs_read16(GUEST_INTR_STATUS) & 0xff;
6159 }
6160
vmx_guest_apic_has_interrupt(struct kvm_vcpu * vcpu)6161 static bool vmx_guest_apic_has_interrupt(struct kvm_vcpu *vcpu)
6162 {
6163 struct vcpu_vmx *vmx = to_vmx(vcpu);
6164 void *vapic_page;
6165 u32 vppr;
6166 int rvi;
6167
6168 if (WARN_ON_ONCE(!is_guest_mode(vcpu)) ||
6169 !nested_cpu_has_vid(get_vmcs12(vcpu)) ||
6170 WARN_ON_ONCE(!vmx->nested.virtual_apic_page))
6171 return false;
6172
6173 rvi = vmx_get_rvi();
6174
6175 vapic_page = kmap(vmx->nested.virtual_apic_page);
6176 vppr = *((u32 *)(vapic_page + APIC_PROCPRI));
6177 kunmap(vmx->nested.virtual_apic_page);
6178
6179 return ((rvi & 0xf0) > (vppr & 0xf0));
6180 }
6181
kvm_vcpu_trigger_posted_interrupt(struct kvm_vcpu * vcpu,bool nested)6182 static inline bool kvm_vcpu_trigger_posted_interrupt(struct kvm_vcpu *vcpu,
6183 bool nested)
6184 {
6185 #ifdef CONFIG_SMP
6186 int pi_vec = nested ? POSTED_INTR_NESTED_VECTOR : POSTED_INTR_VECTOR;
6187
6188 if (vcpu->mode == IN_GUEST_MODE) {
6189 /*
6190 * The vector of interrupt to be delivered to vcpu had
6191 * been set in PIR before this function.
6192 *
6193 * Following cases will be reached in this block, and
6194 * we always send a notification event in all cases as
6195 * explained below.
6196 *
6197 * Case 1: vcpu keeps in non-root mode. Sending a
6198 * notification event posts the interrupt to vcpu.
6199 *
6200 * Case 2: vcpu exits to root mode and is still
6201 * runnable. PIR will be synced to vIRR before the
6202 * next vcpu entry. Sending a notification event in
6203 * this case has no effect, as vcpu is not in root
6204 * mode.
6205 *
6206 * Case 3: vcpu exits to root mode and is blocked.
6207 * vcpu_block() has already synced PIR to vIRR and
6208 * never blocks vcpu if vIRR is not cleared. Therefore,
6209 * a blocked vcpu here does not wait for any requested
6210 * interrupts in PIR, and sending a notification event
6211 * which has no effect is safe here.
6212 */
6213
6214 apic->send_IPI_mask(get_cpu_mask(vcpu->cpu), pi_vec);
6215 return true;
6216 }
6217 #endif
6218 return false;
6219 }
6220
vmx_deliver_nested_posted_interrupt(struct kvm_vcpu * vcpu,int vector)6221 static int vmx_deliver_nested_posted_interrupt(struct kvm_vcpu *vcpu,
6222 int vector)
6223 {
6224 struct vcpu_vmx *vmx = to_vmx(vcpu);
6225
6226 if (is_guest_mode(vcpu) &&
6227 vector == vmx->nested.posted_intr_nv) {
6228 /*
6229 * If a posted intr is not recognized by hardware,
6230 * we will accomplish it in the next vmentry.
6231 */
6232 vmx->nested.pi_pending = true;
6233 kvm_make_request(KVM_REQ_EVENT, vcpu);
6234 /* the PIR and ON have been set by L1. */
6235 if (!kvm_vcpu_trigger_posted_interrupt(vcpu, true))
6236 kvm_vcpu_kick(vcpu);
6237 return 0;
6238 }
6239 return -1;
6240 }
6241 /*
6242 * Send interrupt to vcpu via posted interrupt way.
6243 * 1. If target vcpu is running(non-root mode), send posted interrupt
6244 * notification to vcpu and hardware will sync PIR to vIRR atomically.
6245 * 2. If target vcpu isn't running(root mode), kick it to pick up the
6246 * interrupt from PIR in next vmentry.
6247 */
vmx_deliver_posted_interrupt(struct kvm_vcpu * vcpu,int vector)6248 static int vmx_deliver_posted_interrupt(struct kvm_vcpu *vcpu, int vector)
6249 {
6250 struct vcpu_vmx *vmx = to_vmx(vcpu);
6251 int r;
6252
6253 r = vmx_deliver_nested_posted_interrupt(vcpu, vector);
6254 if (!r)
6255 return 0;
6256
6257 if (!vcpu->arch.apicv_active)
6258 return -1;
6259
6260 if (pi_test_and_set_pir(vector, &vmx->pi_desc))
6261 return 0;
6262
6263 /* If a previous notification has sent the IPI, nothing to do. */
6264 if (pi_test_and_set_on(&vmx->pi_desc))
6265 return 0;
6266
6267 if (!kvm_vcpu_trigger_posted_interrupt(vcpu, false))
6268 kvm_vcpu_kick(vcpu);
6269
6270 return 0;
6271 }
6272
6273 /*
6274 * Set up the vmcs's constant host-state fields, i.e., host-state fields that
6275 * will not change in the lifetime of the guest.
6276 * Note that host-state that does change is set elsewhere. E.g., host-state
6277 * that is set differently for each CPU is set in vmx_vcpu_load(), not here.
6278 */
vmx_set_constant_host_state(struct vcpu_vmx * vmx)6279 static void vmx_set_constant_host_state(struct vcpu_vmx *vmx)
6280 {
6281 u32 low32, high32;
6282 unsigned long tmpl;
6283 struct desc_ptr dt;
6284 unsigned long cr0, cr3, cr4;
6285
6286 cr0 = read_cr0();
6287 WARN_ON(cr0 & X86_CR0_TS);
6288 vmcs_writel(HOST_CR0, cr0); /* 22.2.3 */
6289
6290 /*
6291 * Save the most likely value for this task's CR3 in the VMCS.
6292 * We can't use __get_current_cr3_fast() because we're not atomic.
6293 */
6294 cr3 = __read_cr3();
6295 vmcs_writel(HOST_CR3, cr3); /* 22.2.3 FIXME: shadow tables */
6296 vmx->loaded_vmcs->host_state.cr3 = cr3;
6297
6298 /* Save the most likely value for this task's CR4 in the VMCS. */
6299 cr4 = cr4_read_shadow();
6300 vmcs_writel(HOST_CR4, cr4); /* 22.2.3, 22.2.5 */
6301 vmx->loaded_vmcs->host_state.cr4 = cr4;
6302
6303 vmcs_write16(HOST_CS_SELECTOR, __KERNEL_CS); /* 22.2.4 */
6304 #ifdef CONFIG_X86_64
6305 /*
6306 * Load null selectors, so we can avoid reloading them in
6307 * vmx_prepare_switch_to_host(), in case userspace uses
6308 * the null selectors too (the expected case).
6309 */
6310 vmcs_write16(HOST_DS_SELECTOR, 0);
6311 vmcs_write16(HOST_ES_SELECTOR, 0);
6312 #else
6313 vmcs_write16(HOST_DS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
6314 vmcs_write16(HOST_ES_SELECTOR, __KERNEL_DS); /* 22.2.4 */
6315 #endif
6316 vmcs_write16(HOST_SS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
6317 vmcs_write16(HOST_TR_SELECTOR, GDT_ENTRY_TSS*8); /* 22.2.4 */
6318
6319 store_idt(&dt);
6320 vmcs_writel(HOST_IDTR_BASE, dt.address); /* 22.2.4 */
6321 vmx->host_idt_base = dt.address;
6322
6323 vmcs_writel(HOST_RIP, vmx_return); /* 22.2.5 */
6324
6325 rdmsr(MSR_IA32_SYSENTER_CS, low32, high32);
6326 vmcs_write32(HOST_IA32_SYSENTER_CS, low32);
6327 rdmsrl(MSR_IA32_SYSENTER_EIP, tmpl);
6328 vmcs_writel(HOST_IA32_SYSENTER_EIP, tmpl); /* 22.2.3 */
6329
6330 if (vmcs_config.vmexit_ctrl & VM_EXIT_LOAD_IA32_PAT) {
6331 rdmsr(MSR_IA32_CR_PAT, low32, high32);
6332 vmcs_write64(HOST_IA32_PAT, low32 | ((u64) high32 << 32));
6333 }
6334 }
6335
set_cr4_guest_host_mask(struct vcpu_vmx * vmx)6336 static void set_cr4_guest_host_mask(struct vcpu_vmx *vmx)
6337 {
6338 BUILD_BUG_ON(KVM_CR4_GUEST_OWNED_BITS & ~KVM_POSSIBLE_CR4_GUEST_BITS);
6339
6340 vmx->vcpu.arch.cr4_guest_owned_bits = KVM_CR4_GUEST_OWNED_BITS;
6341 if (enable_ept)
6342 vmx->vcpu.arch.cr4_guest_owned_bits |= X86_CR4_PGE;
6343 if (is_guest_mode(&vmx->vcpu))
6344 vmx->vcpu.arch.cr4_guest_owned_bits &=
6345 ~get_vmcs12(&vmx->vcpu)->cr4_guest_host_mask;
6346 vmcs_writel(CR4_GUEST_HOST_MASK, ~vmx->vcpu.arch.cr4_guest_owned_bits);
6347 }
6348
vmx_pin_based_exec_ctrl(struct vcpu_vmx * vmx)6349 static u32 vmx_pin_based_exec_ctrl(struct vcpu_vmx *vmx)
6350 {
6351 u32 pin_based_exec_ctrl = vmcs_config.pin_based_exec_ctrl;
6352
6353 if (!kvm_vcpu_apicv_active(&vmx->vcpu))
6354 pin_based_exec_ctrl &= ~PIN_BASED_POSTED_INTR;
6355
6356 if (!enable_vnmi)
6357 pin_based_exec_ctrl &= ~PIN_BASED_VIRTUAL_NMIS;
6358
6359 /* Enable the preemption timer dynamically */
6360 pin_based_exec_ctrl &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
6361 return pin_based_exec_ctrl;
6362 }
6363
vmx_refresh_apicv_exec_ctrl(struct kvm_vcpu * vcpu)6364 static void vmx_refresh_apicv_exec_ctrl(struct kvm_vcpu *vcpu)
6365 {
6366 struct vcpu_vmx *vmx = to_vmx(vcpu);
6367
6368 vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, vmx_pin_based_exec_ctrl(vmx));
6369 if (cpu_has_secondary_exec_ctrls()) {
6370 if (kvm_vcpu_apicv_active(vcpu))
6371 vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL,
6372 SECONDARY_EXEC_APIC_REGISTER_VIRT |
6373 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
6374 else
6375 vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL,
6376 SECONDARY_EXEC_APIC_REGISTER_VIRT |
6377 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
6378 }
6379
6380 if (cpu_has_vmx_msr_bitmap())
6381 vmx_update_msr_bitmap(vcpu);
6382 }
6383
vmx_exec_control(struct vcpu_vmx * vmx)6384 static u32 vmx_exec_control(struct vcpu_vmx *vmx)
6385 {
6386 u32 exec_control = vmcs_config.cpu_based_exec_ctrl;
6387
6388 if (vmx->vcpu.arch.switch_db_regs & KVM_DEBUGREG_WONT_EXIT)
6389 exec_control &= ~CPU_BASED_MOV_DR_EXITING;
6390
6391 if (!cpu_need_tpr_shadow(&vmx->vcpu)) {
6392 exec_control &= ~CPU_BASED_TPR_SHADOW;
6393 #ifdef CONFIG_X86_64
6394 exec_control |= CPU_BASED_CR8_STORE_EXITING |
6395 CPU_BASED_CR8_LOAD_EXITING;
6396 #endif
6397 }
6398 if (!enable_ept)
6399 exec_control |= CPU_BASED_CR3_STORE_EXITING |
6400 CPU_BASED_CR3_LOAD_EXITING |
6401 CPU_BASED_INVLPG_EXITING;
6402 if (kvm_mwait_in_guest(vmx->vcpu.kvm))
6403 exec_control &= ~(CPU_BASED_MWAIT_EXITING |
6404 CPU_BASED_MONITOR_EXITING);
6405 if (kvm_hlt_in_guest(vmx->vcpu.kvm))
6406 exec_control &= ~CPU_BASED_HLT_EXITING;
6407 return exec_control;
6408 }
6409
vmx_rdrand_supported(void)6410 static bool vmx_rdrand_supported(void)
6411 {
6412 return vmcs_config.cpu_based_2nd_exec_ctrl &
6413 SECONDARY_EXEC_RDRAND_EXITING;
6414 }
6415
vmx_rdseed_supported(void)6416 static bool vmx_rdseed_supported(void)
6417 {
6418 return vmcs_config.cpu_based_2nd_exec_ctrl &
6419 SECONDARY_EXEC_RDSEED_EXITING;
6420 }
6421
vmx_compute_secondary_exec_control(struct vcpu_vmx * vmx)6422 static void vmx_compute_secondary_exec_control(struct vcpu_vmx *vmx)
6423 {
6424 struct kvm_vcpu *vcpu = &vmx->vcpu;
6425
6426 u32 exec_control = vmcs_config.cpu_based_2nd_exec_ctrl;
6427
6428 if (!cpu_need_virtualize_apic_accesses(vcpu))
6429 exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
6430 if (vmx->vpid == 0)
6431 exec_control &= ~SECONDARY_EXEC_ENABLE_VPID;
6432 if (!enable_ept) {
6433 exec_control &= ~SECONDARY_EXEC_ENABLE_EPT;
6434 enable_unrestricted_guest = 0;
6435 }
6436 if (!enable_unrestricted_guest)
6437 exec_control &= ~SECONDARY_EXEC_UNRESTRICTED_GUEST;
6438 if (kvm_pause_in_guest(vmx->vcpu.kvm))
6439 exec_control &= ~SECONDARY_EXEC_PAUSE_LOOP_EXITING;
6440 if (!kvm_vcpu_apicv_active(vcpu))
6441 exec_control &= ~(SECONDARY_EXEC_APIC_REGISTER_VIRT |
6442 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
6443 exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
6444
6445 /* SECONDARY_EXEC_DESC is enabled/disabled on writes to CR4.UMIP,
6446 * in vmx_set_cr4. */
6447 exec_control &= ~SECONDARY_EXEC_DESC;
6448
6449 /* SECONDARY_EXEC_SHADOW_VMCS is enabled when L1 executes VMPTRLD
6450 (handle_vmptrld).
6451 We can NOT enable shadow_vmcs here because we don't have yet
6452 a current VMCS12
6453 */
6454 exec_control &= ~SECONDARY_EXEC_SHADOW_VMCS;
6455
6456 if (!enable_pml)
6457 exec_control &= ~SECONDARY_EXEC_ENABLE_PML;
6458
6459 if (vmx_xsaves_supported()) {
6460 /* Exposing XSAVES only when XSAVE is exposed */
6461 bool xsaves_enabled =
6462 guest_cpuid_has(vcpu, X86_FEATURE_XSAVE) &&
6463 guest_cpuid_has(vcpu, X86_FEATURE_XSAVES);
6464
6465 if (!xsaves_enabled)
6466 exec_control &= ~SECONDARY_EXEC_XSAVES;
6467
6468 if (nested) {
6469 if (xsaves_enabled)
6470 vmx->nested.msrs.secondary_ctls_high |=
6471 SECONDARY_EXEC_XSAVES;
6472 else
6473 vmx->nested.msrs.secondary_ctls_high &=
6474 ~SECONDARY_EXEC_XSAVES;
6475 }
6476 }
6477
6478 if (vmx_rdtscp_supported()) {
6479 bool rdtscp_enabled = guest_cpuid_has(vcpu, X86_FEATURE_RDTSCP);
6480 if (!rdtscp_enabled)
6481 exec_control &= ~SECONDARY_EXEC_RDTSCP;
6482
6483 if (nested) {
6484 if (rdtscp_enabled)
6485 vmx->nested.msrs.secondary_ctls_high |=
6486 SECONDARY_EXEC_RDTSCP;
6487 else
6488 vmx->nested.msrs.secondary_ctls_high &=
6489 ~SECONDARY_EXEC_RDTSCP;
6490 }
6491 }
6492
6493 if (vmx_invpcid_supported()) {
6494 /* Exposing INVPCID only when PCID is exposed */
6495 bool invpcid_enabled =
6496 guest_cpuid_has(vcpu, X86_FEATURE_INVPCID) &&
6497 guest_cpuid_has(vcpu, X86_FEATURE_PCID);
6498
6499 if (!invpcid_enabled) {
6500 exec_control &= ~SECONDARY_EXEC_ENABLE_INVPCID;
6501 guest_cpuid_clear(vcpu, X86_FEATURE_INVPCID);
6502 }
6503
6504 if (nested) {
6505 if (invpcid_enabled)
6506 vmx->nested.msrs.secondary_ctls_high |=
6507 SECONDARY_EXEC_ENABLE_INVPCID;
6508 else
6509 vmx->nested.msrs.secondary_ctls_high &=
6510 ~SECONDARY_EXEC_ENABLE_INVPCID;
6511 }
6512 }
6513
6514 if (vmx_rdrand_supported()) {
6515 bool rdrand_enabled = guest_cpuid_has(vcpu, X86_FEATURE_RDRAND);
6516 if (rdrand_enabled)
6517 exec_control &= ~SECONDARY_EXEC_RDRAND_EXITING;
6518
6519 if (nested) {
6520 if (rdrand_enabled)
6521 vmx->nested.msrs.secondary_ctls_high |=
6522 SECONDARY_EXEC_RDRAND_EXITING;
6523 else
6524 vmx->nested.msrs.secondary_ctls_high &=
6525 ~SECONDARY_EXEC_RDRAND_EXITING;
6526 }
6527 }
6528
6529 if (vmx_rdseed_supported()) {
6530 bool rdseed_enabled = guest_cpuid_has(vcpu, X86_FEATURE_RDSEED);
6531 if (rdseed_enabled)
6532 exec_control &= ~SECONDARY_EXEC_RDSEED_EXITING;
6533
6534 if (nested) {
6535 if (rdseed_enabled)
6536 vmx->nested.msrs.secondary_ctls_high |=
6537 SECONDARY_EXEC_RDSEED_EXITING;
6538 else
6539 vmx->nested.msrs.secondary_ctls_high &=
6540 ~SECONDARY_EXEC_RDSEED_EXITING;
6541 }
6542 }
6543
6544 vmx->secondary_exec_control = exec_control;
6545 }
6546
ept_set_mmio_spte_mask(void)6547 static void ept_set_mmio_spte_mask(void)
6548 {
6549 /*
6550 * EPT Misconfigurations can be generated if the value of bits 2:0
6551 * of an EPT paging-structure entry is 110b (write/execute).
6552 */
6553 kvm_mmu_set_mmio_spte_mask(VMX_EPT_RWX_MASK,
6554 VMX_EPT_MISCONFIG_WX_VALUE);
6555 }
6556
6557 #define VMX_XSS_EXIT_BITMAP 0
6558 /*
6559 * Sets up the vmcs for emulated real mode.
6560 */
vmx_vcpu_setup(struct vcpu_vmx * vmx)6561 static void vmx_vcpu_setup(struct vcpu_vmx *vmx)
6562 {
6563 int i;
6564
6565 if (enable_shadow_vmcs) {
6566 /*
6567 * At vCPU creation, "VMWRITE to any supported field
6568 * in the VMCS" is supported, so use the more
6569 * permissive vmx_vmread_bitmap to specify both read
6570 * and write permissions for the shadow VMCS.
6571 */
6572 vmcs_write64(VMREAD_BITMAP, __pa(vmx_vmread_bitmap));
6573 vmcs_write64(VMWRITE_BITMAP, __pa(vmx_vmread_bitmap));
6574 }
6575 if (cpu_has_vmx_msr_bitmap())
6576 vmcs_write64(MSR_BITMAP, __pa(vmx->vmcs01.msr_bitmap));
6577
6578 vmcs_write64(VMCS_LINK_POINTER, -1ull); /* 22.3.1.5 */
6579
6580 /* Control */
6581 vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, vmx_pin_based_exec_ctrl(vmx));
6582 vmx->hv_deadline_tsc = -1;
6583
6584 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, vmx_exec_control(vmx));
6585
6586 if (cpu_has_secondary_exec_ctrls()) {
6587 vmx_compute_secondary_exec_control(vmx);
6588 vmcs_write32(SECONDARY_VM_EXEC_CONTROL,
6589 vmx->secondary_exec_control);
6590 }
6591
6592 if (kvm_vcpu_apicv_active(&vmx->vcpu)) {
6593 vmcs_write64(EOI_EXIT_BITMAP0, 0);
6594 vmcs_write64(EOI_EXIT_BITMAP1, 0);
6595 vmcs_write64(EOI_EXIT_BITMAP2, 0);
6596 vmcs_write64(EOI_EXIT_BITMAP3, 0);
6597
6598 vmcs_write16(GUEST_INTR_STATUS, 0);
6599
6600 vmcs_write16(POSTED_INTR_NV, POSTED_INTR_VECTOR);
6601 vmcs_write64(POSTED_INTR_DESC_ADDR, __pa((&vmx->pi_desc)));
6602 }
6603
6604 if (!kvm_pause_in_guest(vmx->vcpu.kvm)) {
6605 vmcs_write32(PLE_GAP, ple_gap);
6606 vmx->ple_window = ple_window;
6607 vmx->ple_window_dirty = true;
6608 }
6609
6610 vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK, 0);
6611 vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH, 0);
6612 vmcs_write32(CR3_TARGET_COUNT, 0); /* 22.2.1 */
6613
6614 vmcs_write16(HOST_FS_SELECTOR, 0); /* 22.2.4 */
6615 vmcs_write16(HOST_GS_SELECTOR, 0); /* 22.2.4 */
6616 vmx_set_constant_host_state(vmx);
6617 vmcs_writel(HOST_FS_BASE, 0); /* 22.2.4 */
6618 vmcs_writel(HOST_GS_BASE, 0); /* 22.2.4 */
6619
6620 if (cpu_has_vmx_vmfunc())
6621 vmcs_write64(VM_FUNCTION_CONTROL, 0);
6622
6623 vmcs_write32(VM_EXIT_MSR_STORE_COUNT, 0);
6624 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, 0);
6625 vmcs_write64(VM_EXIT_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.host.val));
6626 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, 0);
6627 vmcs_write64(VM_ENTRY_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.guest.val));
6628
6629 if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT)
6630 vmcs_write64(GUEST_IA32_PAT, vmx->vcpu.arch.pat);
6631
6632 for (i = 0; i < ARRAY_SIZE(vmx_msr_index); ++i) {
6633 u32 index = vmx_msr_index[i];
6634 u32 data_low, data_high;
6635 int j = vmx->nmsrs;
6636
6637 if (rdmsr_safe(index, &data_low, &data_high) < 0)
6638 continue;
6639 if (wrmsr_safe(index, data_low, data_high) < 0)
6640 continue;
6641 vmx->guest_msrs[j].index = i;
6642 vmx->guest_msrs[j].data = 0;
6643 vmx->guest_msrs[j].mask = -1ull;
6644 ++vmx->nmsrs;
6645 }
6646
6647 vm_exit_controls_init(vmx, vmcs_config.vmexit_ctrl);
6648
6649 /* 22.2.1, 20.8.1 */
6650 vm_entry_controls_init(vmx, vmcs_config.vmentry_ctrl);
6651
6652 vmx->vcpu.arch.cr0_guest_owned_bits = X86_CR0_TS;
6653 vmcs_writel(CR0_GUEST_HOST_MASK, ~X86_CR0_TS);
6654
6655 set_cr4_guest_host_mask(vmx);
6656
6657 if (vmx_xsaves_supported())
6658 vmcs_write64(XSS_EXIT_BITMAP, VMX_XSS_EXIT_BITMAP);
6659
6660 if (enable_pml) {
6661 ASSERT(vmx->pml_pg);
6662 vmcs_write64(PML_ADDRESS, page_to_phys(vmx->pml_pg));
6663 vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1);
6664 }
6665
6666 if (cpu_has_vmx_encls_vmexit())
6667 vmcs_write64(ENCLS_EXITING_BITMAP, -1ull);
6668 }
6669
vmx_vcpu_reset(struct kvm_vcpu * vcpu,bool init_event)6670 static void vmx_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event)
6671 {
6672 struct vcpu_vmx *vmx = to_vmx(vcpu);
6673 struct msr_data apic_base_msr;
6674 u64 cr0;
6675
6676 vmx->rmode.vm86_active = 0;
6677 vmx->spec_ctrl = 0;
6678
6679 vcpu->arch.microcode_version = 0x100000000ULL;
6680 vmx->vcpu.arch.regs[VCPU_REGS_RDX] = get_rdx_init_val();
6681 kvm_set_cr8(vcpu, 0);
6682
6683 if (!init_event) {
6684 apic_base_msr.data = APIC_DEFAULT_PHYS_BASE |
6685 MSR_IA32_APICBASE_ENABLE;
6686 if (kvm_vcpu_is_reset_bsp(vcpu))
6687 apic_base_msr.data |= MSR_IA32_APICBASE_BSP;
6688 apic_base_msr.host_initiated = true;
6689 kvm_set_apic_base(vcpu, &apic_base_msr);
6690 }
6691
6692 vmx_segment_cache_clear(vmx);
6693
6694 seg_setup(VCPU_SREG_CS);
6695 vmcs_write16(GUEST_CS_SELECTOR, 0xf000);
6696 vmcs_writel(GUEST_CS_BASE, 0xffff0000ul);
6697
6698 seg_setup(VCPU_SREG_DS);
6699 seg_setup(VCPU_SREG_ES);
6700 seg_setup(VCPU_SREG_FS);
6701 seg_setup(VCPU_SREG_GS);
6702 seg_setup(VCPU_SREG_SS);
6703
6704 vmcs_write16(GUEST_TR_SELECTOR, 0);
6705 vmcs_writel(GUEST_TR_BASE, 0);
6706 vmcs_write32(GUEST_TR_LIMIT, 0xffff);
6707 vmcs_write32(GUEST_TR_AR_BYTES, 0x008b);
6708
6709 vmcs_write16(GUEST_LDTR_SELECTOR, 0);
6710 vmcs_writel(GUEST_LDTR_BASE, 0);
6711 vmcs_write32(GUEST_LDTR_LIMIT, 0xffff);
6712 vmcs_write32(GUEST_LDTR_AR_BYTES, 0x00082);
6713
6714 if (!init_event) {
6715 vmcs_write32(GUEST_SYSENTER_CS, 0);
6716 vmcs_writel(GUEST_SYSENTER_ESP, 0);
6717 vmcs_writel(GUEST_SYSENTER_EIP, 0);
6718 vmcs_write64(GUEST_IA32_DEBUGCTL, 0);
6719 }
6720
6721 kvm_set_rflags(vcpu, X86_EFLAGS_FIXED);
6722 kvm_rip_write(vcpu, 0xfff0);
6723
6724 vmcs_writel(GUEST_GDTR_BASE, 0);
6725 vmcs_write32(GUEST_GDTR_LIMIT, 0xffff);
6726
6727 vmcs_writel(GUEST_IDTR_BASE, 0);
6728 vmcs_write32(GUEST_IDTR_LIMIT, 0xffff);
6729
6730 vmcs_write32(GUEST_ACTIVITY_STATE, GUEST_ACTIVITY_ACTIVE);
6731 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, 0);
6732 vmcs_writel(GUEST_PENDING_DBG_EXCEPTIONS, 0);
6733 if (kvm_mpx_supported())
6734 vmcs_write64(GUEST_BNDCFGS, 0);
6735
6736 setup_msrs(vmx);
6737
6738 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0); /* 22.2.1 */
6739
6740 if (cpu_has_vmx_tpr_shadow() && !init_event) {
6741 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, 0);
6742 if (cpu_need_tpr_shadow(vcpu))
6743 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR,
6744 __pa(vcpu->arch.apic->regs));
6745 vmcs_write32(TPR_THRESHOLD, 0);
6746 }
6747
6748 kvm_make_request(KVM_REQ_APIC_PAGE_RELOAD, vcpu);
6749
6750 if (vmx->vpid != 0)
6751 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid);
6752
6753 cr0 = X86_CR0_NW | X86_CR0_CD | X86_CR0_ET;
6754 vmx->vcpu.arch.cr0 = cr0;
6755 vmx_set_cr0(vcpu, cr0); /* enter rmode */
6756 vmx_set_cr4(vcpu, 0);
6757 vmx_set_efer(vcpu, 0);
6758
6759 update_exception_bitmap(vcpu);
6760
6761 vpid_sync_context(vmx->vpid);
6762 if (init_event)
6763 vmx_clear_hlt(vcpu);
6764 }
6765
6766 /*
6767 * In nested virtualization, check if L1 asked to exit on external interrupts.
6768 * For most existing hypervisors, this will always return true.
6769 */
nested_exit_on_intr(struct kvm_vcpu * vcpu)6770 static bool nested_exit_on_intr(struct kvm_vcpu *vcpu)
6771 {
6772 return get_vmcs12(vcpu)->pin_based_vm_exec_control &
6773 PIN_BASED_EXT_INTR_MASK;
6774 }
6775
6776 /*
6777 * In nested virtualization, check if L1 has set
6778 * VM_EXIT_ACK_INTR_ON_EXIT
6779 */
nested_exit_intr_ack_set(struct kvm_vcpu * vcpu)6780 static bool nested_exit_intr_ack_set(struct kvm_vcpu *vcpu)
6781 {
6782 return get_vmcs12(vcpu)->vm_exit_controls &
6783 VM_EXIT_ACK_INTR_ON_EXIT;
6784 }
6785
nested_exit_on_nmi(struct kvm_vcpu * vcpu)6786 static bool nested_exit_on_nmi(struct kvm_vcpu *vcpu)
6787 {
6788 return nested_cpu_has_nmi_exiting(get_vmcs12(vcpu));
6789 }
6790
enable_irq_window(struct kvm_vcpu * vcpu)6791 static void enable_irq_window(struct kvm_vcpu *vcpu)
6792 {
6793 vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL,
6794 CPU_BASED_VIRTUAL_INTR_PENDING);
6795 }
6796
enable_nmi_window(struct kvm_vcpu * vcpu)6797 static void enable_nmi_window(struct kvm_vcpu *vcpu)
6798 {
6799 if (!enable_vnmi ||
6800 vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & GUEST_INTR_STATE_STI) {
6801 enable_irq_window(vcpu);
6802 return;
6803 }
6804
6805 vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL,
6806 CPU_BASED_VIRTUAL_NMI_PENDING);
6807 }
6808
vmx_inject_irq(struct kvm_vcpu * vcpu)6809 static void vmx_inject_irq(struct kvm_vcpu *vcpu)
6810 {
6811 struct vcpu_vmx *vmx = to_vmx(vcpu);
6812 uint32_t intr;
6813 int irq = vcpu->arch.interrupt.nr;
6814
6815 trace_kvm_inj_virq(irq);
6816
6817 ++vcpu->stat.irq_injections;
6818 if (vmx->rmode.vm86_active) {
6819 int inc_eip = 0;
6820 if (vcpu->arch.interrupt.soft)
6821 inc_eip = vcpu->arch.event_exit_inst_len;
6822 if (kvm_inject_realmode_interrupt(vcpu, irq, inc_eip) != EMULATE_DONE)
6823 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
6824 return;
6825 }
6826 intr = irq | INTR_INFO_VALID_MASK;
6827 if (vcpu->arch.interrupt.soft) {
6828 intr |= INTR_TYPE_SOFT_INTR;
6829 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
6830 vmx->vcpu.arch.event_exit_inst_len);
6831 } else
6832 intr |= INTR_TYPE_EXT_INTR;
6833 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr);
6834
6835 vmx_clear_hlt(vcpu);
6836 }
6837
vmx_inject_nmi(struct kvm_vcpu * vcpu)6838 static void vmx_inject_nmi(struct kvm_vcpu *vcpu)
6839 {
6840 struct vcpu_vmx *vmx = to_vmx(vcpu);
6841
6842 if (!enable_vnmi) {
6843 /*
6844 * Tracking the NMI-blocked state in software is built upon
6845 * finding the next open IRQ window. This, in turn, depends on
6846 * well-behaving guests: They have to keep IRQs disabled at
6847 * least as long as the NMI handler runs. Otherwise we may
6848 * cause NMI nesting, maybe breaking the guest. But as this is
6849 * highly unlikely, we can live with the residual risk.
6850 */
6851 vmx->loaded_vmcs->soft_vnmi_blocked = 1;
6852 vmx->loaded_vmcs->vnmi_blocked_time = 0;
6853 }
6854
6855 ++vcpu->stat.nmi_injections;
6856 vmx->loaded_vmcs->nmi_known_unmasked = false;
6857
6858 if (vmx->rmode.vm86_active) {
6859 if (kvm_inject_realmode_interrupt(vcpu, NMI_VECTOR, 0) != EMULATE_DONE)
6860 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
6861 return;
6862 }
6863
6864 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
6865 INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK | NMI_VECTOR);
6866
6867 vmx_clear_hlt(vcpu);
6868 }
6869
vmx_get_nmi_mask(struct kvm_vcpu * vcpu)6870 static bool vmx_get_nmi_mask(struct kvm_vcpu *vcpu)
6871 {
6872 struct vcpu_vmx *vmx = to_vmx(vcpu);
6873 bool masked;
6874
6875 if (!enable_vnmi)
6876 return vmx->loaded_vmcs->soft_vnmi_blocked;
6877 if (vmx->loaded_vmcs->nmi_known_unmasked)
6878 return false;
6879 masked = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & GUEST_INTR_STATE_NMI;
6880 vmx->loaded_vmcs->nmi_known_unmasked = !masked;
6881 return masked;
6882 }
6883
vmx_set_nmi_mask(struct kvm_vcpu * vcpu,bool masked)6884 static void vmx_set_nmi_mask(struct kvm_vcpu *vcpu, bool masked)
6885 {
6886 struct vcpu_vmx *vmx = to_vmx(vcpu);
6887
6888 if (!enable_vnmi) {
6889 if (vmx->loaded_vmcs->soft_vnmi_blocked != masked) {
6890 vmx->loaded_vmcs->soft_vnmi_blocked = masked;
6891 vmx->loaded_vmcs->vnmi_blocked_time = 0;
6892 }
6893 } else {
6894 vmx->loaded_vmcs->nmi_known_unmasked = !masked;
6895 if (masked)
6896 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
6897 GUEST_INTR_STATE_NMI);
6898 else
6899 vmcs_clear_bits(GUEST_INTERRUPTIBILITY_INFO,
6900 GUEST_INTR_STATE_NMI);
6901 }
6902 }
6903
vmx_nmi_allowed(struct kvm_vcpu * vcpu)6904 static int vmx_nmi_allowed(struct kvm_vcpu *vcpu)
6905 {
6906 if (to_vmx(vcpu)->nested.nested_run_pending)
6907 return 0;
6908
6909 if (!enable_vnmi &&
6910 to_vmx(vcpu)->loaded_vmcs->soft_vnmi_blocked)
6911 return 0;
6912
6913 return !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) &
6914 (GUEST_INTR_STATE_MOV_SS | GUEST_INTR_STATE_STI
6915 | GUEST_INTR_STATE_NMI));
6916 }
6917
vmx_interrupt_allowed(struct kvm_vcpu * vcpu)6918 static int vmx_interrupt_allowed(struct kvm_vcpu *vcpu)
6919 {
6920 if (to_vmx(vcpu)->nested.nested_run_pending)
6921 return false;
6922
6923 if (is_guest_mode(vcpu) && nested_exit_on_intr(vcpu))
6924 return true;
6925
6926 return (vmcs_readl(GUEST_RFLAGS) & X86_EFLAGS_IF) &&
6927 !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) &
6928 (GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS));
6929 }
6930
vmx_set_tss_addr(struct kvm * kvm,unsigned int addr)6931 static int vmx_set_tss_addr(struct kvm *kvm, unsigned int addr)
6932 {
6933 int ret;
6934
6935 if (enable_unrestricted_guest)
6936 return 0;
6937
6938 ret = x86_set_memory_region(kvm, TSS_PRIVATE_MEMSLOT, addr,
6939 PAGE_SIZE * 3);
6940 if (ret)
6941 return ret;
6942 to_kvm_vmx(kvm)->tss_addr = addr;
6943 return init_rmode_tss(kvm);
6944 }
6945
vmx_set_identity_map_addr(struct kvm * kvm,u64 ident_addr)6946 static int vmx_set_identity_map_addr(struct kvm *kvm, u64 ident_addr)
6947 {
6948 to_kvm_vmx(kvm)->ept_identity_map_addr = ident_addr;
6949 return 0;
6950 }
6951
rmode_exception(struct kvm_vcpu * vcpu,int vec)6952 static bool rmode_exception(struct kvm_vcpu *vcpu, int vec)
6953 {
6954 switch (vec) {
6955 case BP_VECTOR:
6956 /*
6957 * Update instruction length as we may reinject the exception
6958 * from user space while in guest debugging mode.
6959 */
6960 to_vmx(vcpu)->vcpu.arch.event_exit_inst_len =
6961 vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
6962 if (vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
6963 return false;
6964 /* fall through */
6965 case DB_VECTOR:
6966 if (vcpu->guest_debug &
6967 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))
6968 return false;
6969 /* fall through */
6970 case DE_VECTOR:
6971 case OF_VECTOR:
6972 case BR_VECTOR:
6973 case UD_VECTOR:
6974 case DF_VECTOR:
6975 case SS_VECTOR:
6976 case GP_VECTOR:
6977 case MF_VECTOR:
6978 return true;
6979 break;
6980 }
6981 return false;
6982 }
6983
handle_rmode_exception(struct kvm_vcpu * vcpu,int vec,u32 err_code)6984 static int handle_rmode_exception(struct kvm_vcpu *vcpu,
6985 int vec, u32 err_code)
6986 {
6987 /*
6988 * Instruction with address size override prefix opcode 0x67
6989 * Cause the #SS fault with 0 error code in VM86 mode.
6990 */
6991 if (((vec == GP_VECTOR) || (vec == SS_VECTOR)) && err_code == 0) {
6992 if (kvm_emulate_instruction(vcpu, 0) == EMULATE_DONE) {
6993 if (vcpu->arch.halt_request) {
6994 vcpu->arch.halt_request = 0;
6995 return kvm_vcpu_halt(vcpu);
6996 }
6997 return 1;
6998 }
6999 return 0;
7000 }
7001
7002 /*
7003 * Forward all other exceptions that are valid in real mode.
7004 * FIXME: Breaks guest debugging in real mode, needs to be fixed with
7005 * the required debugging infrastructure rework.
7006 */
7007 kvm_queue_exception(vcpu, vec);
7008 return 1;
7009 }
7010
7011 /*
7012 * Trigger machine check on the host. We assume all the MSRs are already set up
7013 * by the CPU and that we still run on the same CPU as the MCE occurred on.
7014 * We pass a fake environment to the machine check handler because we want
7015 * the guest to be always treated like user space, no matter what context
7016 * it used internally.
7017 */
kvm_machine_check(void)7018 static void kvm_machine_check(void)
7019 {
7020 #if defined(CONFIG_X86_MCE)
7021 struct pt_regs regs = {
7022 .cs = 3, /* Fake ring 3 no matter what the guest ran on */
7023 .flags = X86_EFLAGS_IF,
7024 };
7025
7026 do_machine_check(®s, 0);
7027 #endif
7028 }
7029
handle_machine_check(struct kvm_vcpu * vcpu)7030 static int handle_machine_check(struct kvm_vcpu *vcpu)
7031 {
7032 /* already handled by vcpu_run */
7033 return 1;
7034 }
7035
handle_exception(struct kvm_vcpu * vcpu)7036 static int handle_exception(struct kvm_vcpu *vcpu)
7037 {
7038 struct vcpu_vmx *vmx = to_vmx(vcpu);
7039 struct kvm_run *kvm_run = vcpu->run;
7040 u32 intr_info, ex_no, error_code;
7041 unsigned long cr2, rip, dr6;
7042 u32 vect_info;
7043 enum emulation_result er;
7044
7045 vect_info = vmx->idt_vectoring_info;
7046 intr_info = vmx->exit_intr_info;
7047
7048 if (is_machine_check(intr_info))
7049 return handle_machine_check(vcpu);
7050
7051 if (is_nmi(intr_info))
7052 return 1; /* already handled by vmx_vcpu_run() */
7053
7054 if (is_invalid_opcode(intr_info))
7055 return handle_ud(vcpu);
7056
7057 error_code = 0;
7058 if (intr_info & INTR_INFO_DELIVER_CODE_MASK)
7059 error_code = vmcs_read32(VM_EXIT_INTR_ERROR_CODE);
7060
7061 if (!vmx->rmode.vm86_active && is_gp_fault(intr_info)) {
7062 WARN_ON_ONCE(!enable_vmware_backdoor);
7063 er = kvm_emulate_instruction(vcpu,
7064 EMULTYPE_VMWARE | EMULTYPE_NO_UD_ON_FAIL);
7065 if (er == EMULATE_USER_EXIT)
7066 return 0;
7067 else if (er != EMULATE_DONE)
7068 kvm_queue_exception_e(vcpu, GP_VECTOR, error_code);
7069 return 1;
7070 }
7071
7072 /*
7073 * The #PF with PFEC.RSVD = 1 indicates the guest is accessing
7074 * MMIO, it is better to report an internal error.
7075 * See the comments in vmx_handle_exit.
7076 */
7077 if ((vect_info & VECTORING_INFO_VALID_MASK) &&
7078 !(is_page_fault(intr_info) && !(error_code & PFERR_RSVD_MASK))) {
7079 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
7080 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_SIMUL_EX;
7081 vcpu->run->internal.ndata = 3;
7082 vcpu->run->internal.data[0] = vect_info;
7083 vcpu->run->internal.data[1] = intr_info;
7084 vcpu->run->internal.data[2] = error_code;
7085 return 0;
7086 }
7087
7088 if (is_page_fault(intr_info)) {
7089 cr2 = vmcs_readl(EXIT_QUALIFICATION);
7090 /* EPT won't cause page fault directly */
7091 WARN_ON_ONCE(!vcpu->arch.apf.host_apf_reason && enable_ept);
7092 return kvm_handle_page_fault(vcpu, error_code, cr2, NULL, 0);
7093 }
7094
7095 ex_no = intr_info & INTR_INFO_VECTOR_MASK;
7096
7097 if (vmx->rmode.vm86_active && rmode_exception(vcpu, ex_no))
7098 return handle_rmode_exception(vcpu, ex_no, error_code);
7099
7100 switch (ex_no) {
7101 case AC_VECTOR:
7102 kvm_queue_exception_e(vcpu, AC_VECTOR, error_code);
7103 return 1;
7104 case DB_VECTOR:
7105 dr6 = vmcs_readl(EXIT_QUALIFICATION);
7106 if (!(vcpu->guest_debug &
7107 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))) {
7108 vcpu->arch.dr6 &= ~15;
7109 vcpu->arch.dr6 |= dr6 | DR6_RTM;
7110 if (is_icebp(intr_info))
7111 skip_emulated_instruction(vcpu);
7112
7113 kvm_queue_exception(vcpu, DB_VECTOR);
7114 return 1;
7115 }
7116 kvm_run->debug.arch.dr6 = dr6 | DR6_FIXED_1;
7117 kvm_run->debug.arch.dr7 = vmcs_readl(GUEST_DR7);
7118 /* fall through */
7119 case BP_VECTOR:
7120 /*
7121 * Update instruction length as we may reinject #BP from
7122 * user space while in guest debugging mode. Reading it for
7123 * #DB as well causes no harm, it is not used in that case.
7124 */
7125 vmx->vcpu.arch.event_exit_inst_len =
7126 vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
7127 kvm_run->exit_reason = KVM_EXIT_DEBUG;
7128 rip = kvm_rip_read(vcpu);
7129 kvm_run->debug.arch.pc = vmcs_readl(GUEST_CS_BASE) + rip;
7130 kvm_run->debug.arch.exception = ex_no;
7131 break;
7132 default:
7133 kvm_run->exit_reason = KVM_EXIT_EXCEPTION;
7134 kvm_run->ex.exception = ex_no;
7135 kvm_run->ex.error_code = error_code;
7136 break;
7137 }
7138 return 0;
7139 }
7140
handle_external_interrupt(struct kvm_vcpu * vcpu)7141 static int handle_external_interrupt(struct kvm_vcpu *vcpu)
7142 {
7143 ++vcpu->stat.irq_exits;
7144 return 1;
7145 }
7146
handle_triple_fault(struct kvm_vcpu * vcpu)7147 static int handle_triple_fault(struct kvm_vcpu *vcpu)
7148 {
7149 vcpu->run->exit_reason = KVM_EXIT_SHUTDOWN;
7150 vcpu->mmio_needed = 0;
7151 return 0;
7152 }
7153
handle_io(struct kvm_vcpu * vcpu)7154 static int handle_io(struct kvm_vcpu *vcpu)
7155 {
7156 unsigned long exit_qualification;
7157 int size, in, string;
7158 unsigned port;
7159
7160 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7161 string = (exit_qualification & 16) != 0;
7162
7163 ++vcpu->stat.io_exits;
7164
7165 if (string)
7166 return kvm_emulate_instruction(vcpu, 0) == EMULATE_DONE;
7167
7168 port = exit_qualification >> 16;
7169 size = (exit_qualification & 7) + 1;
7170 in = (exit_qualification & 8) != 0;
7171
7172 return kvm_fast_pio(vcpu, size, port, in);
7173 }
7174
7175 static void
vmx_patch_hypercall(struct kvm_vcpu * vcpu,unsigned char * hypercall)7176 vmx_patch_hypercall(struct kvm_vcpu *vcpu, unsigned char *hypercall)
7177 {
7178 /*
7179 * Patch in the VMCALL instruction:
7180 */
7181 hypercall[0] = 0x0f;
7182 hypercall[1] = 0x01;
7183 hypercall[2] = 0xc1;
7184 }
7185
7186 /* called to set cr0 as appropriate for a mov-to-cr0 exit. */
handle_set_cr0(struct kvm_vcpu * vcpu,unsigned long val)7187 static int handle_set_cr0(struct kvm_vcpu *vcpu, unsigned long val)
7188 {
7189 if (is_guest_mode(vcpu)) {
7190 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
7191 unsigned long orig_val = val;
7192
7193 /*
7194 * We get here when L2 changed cr0 in a way that did not change
7195 * any of L1's shadowed bits (see nested_vmx_exit_handled_cr),
7196 * but did change L0 shadowed bits. So we first calculate the
7197 * effective cr0 value that L1 would like to write into the
7198 * hardware. It consists of the L2-owned bits from the new
7199 * value combined with the L1-owned bits from L1's guest_cr0.
7200 */
7201 val = (val & ~vmcs12->cr0_guest_host_mask) |
7202 (vmcs12->guest_cr0 & vmcs12->cr0_guest_host_mask);
7203
7204 if (!nested_guest_cr0_valid(vcpu, val))
7205 return 1;
7206
7207 if (kvm_set_cr0(vcpu, val))
7208 return 1;
7209 vmcs_writel(CR0_READ_SHADOW, orig_val);
7210 return 0;
7211 } else {
7212 if (to_vmx(vcpu)->nested.vmxon &&
7213 !nested_host_cr0_valid(vcpu, val))
7214 return 1;
7215
7216 return kvm_set_cr0(vcpu, val);
7217 }
7218 }
7219
handle_set_cr4(struct kvm_vcpu * vcpu,unsigned long val)7220 static int handle_set_cr4(struct kvm_vcpu *vcpu, unsigned long val)
7221 {
7222 if (is_guest_mode(vcpu)) {
7223 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
7224 unsigned long orig_val = val;
7225
7226 /* analogously to handle_set_cr0 */
7227 val = (val & ~vmcs12->cr4_guest_host_mask) |
7228 (vmcs12->guest_cr4 & vmcs12->cr4_guest_host_mask);
7229 if (kvm_set_cr4(vcpu, val))
7230 return 1;
7231 vmcs_writel(CR4_READ_SHADOW, orig_val);
7232 return 0;
7233 } else
7234 return kvm_set_cr4(vcpu, val);
7235 }
7236
handle_desc(struct kvm_vcpu * vcpu)7237 static int handle_desc(struct kvm_vcpu *vcpu)
7238 {
7239 WARN_ON(!(vcpu->arch.cr4 & X86_CR4_UMIP));
7240 return kvm_emulate_instruction(vcpu, 0) == EMULATE_DONE;
7241 }
7242
handle_cr(struct kvm_vcpu * vcpu)7243 static int handle_cr(struct kvm_vcpu *vcpu)
7244 {
7245 unsigned long exit_qualification, val;
7246 int cr;
7247 int reg;
7248 int err;
7249 int ret;
7250
7251 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7252 cr = exit_qualification & 15;
7253 reg = (exit_qualification >> 8) & 15;
7254 switch ((exit_qualification >> 4) & 3) {
7255 case 0: /* mov to cr */
7256 val = kvm_register_readl(vcpu, reg);
7257 trace_kvm_cr_write(cr, val);
7258 switch (cr) {
7259 case 0:
7260 err = handle_set_cr0(vcpu, val);
7261 return kvm_complete_insn_gp(vcpu, err);
7262 case 3:
7263 WARN_ON_ONCE(enable_unrestricted_guest);
7264 err = kvm_set_cr3(vcpu, val);
7265 return kvm_complete_insn_gp(vcpu, err);
7266 case 4:
7267 err = handle_set_cr4(vcpu, val);
7268 return kvm_complete_insn_gp(vcpu, err);
7269 case 8: {
7270 u8 cr8_prev = kvm_get_cr8(vcpu);
7271 u8 cr8 = (u8)val;
7272 err = kvm_set_cr8(vcpu, cr8);
7273 ret = kvm_complete_insn_gp(vcpu, err);
7274 if (lapic_in_kernel(vcpu))
7275 return ret;
7276 if (cr8_prev <= cr8)
7277 return ret;
7278 /*
7279 * TODO: we might be squashing a
7280 * KVM_GUESTDBG_SINGLESTEP-triggered
7281 * KVM_EXIT_DEBUG here.
7282 */
7283 vcpu->run->exit_reason = KVM_EXIT_SET_TPR;
7284 return 0;
7285 }
7286 }
7287 break;
7288 case 2: /* clts */
7289 WARN_ONCE(1, "Guest should always own CR0.TS");
7290 vmx_set_cr0(vcpu, kvm_read_cr0_bits(vcpu, ~X86_CR0_TS));
7291 trace_kvm_cr_write(0, kvm_read_cr0(vcpu));
7292 return kvm_skip_emulated_instruction(vcpu);
7293 case 1: /*mov from cr*/
7294 switch (cr) {
7295 case 3:
7296 WARN_ON_ONCE(enable_unrestricted_guest);
7297 val = kvm_read_cr3(vcpu);
7298 kvm_register_write(vcpu, reg, val);
7299 trace_kvm_cr_read(cr, val);
7300 return kvm_skip_emulated_instruction(vcpu);
7301 case 8:
7302 val = kvm_get_cr8(vcpu);
7303 kvm_register_write(vcpu, reg, val);
7304 trace_kvm_cr_read(cr, val);
7305 return kvm_skip_emulated_instruction(vcpu);
7306 }
7307 break;
7308 case 3: /* lmsw */
7309 val = (exit_qualification >> LMSW_SOURCE_DATA_SHIFT) & 0x0f;
7310 trace_kvm_cr_write(0, (kvm_read_cr0(vcpu) & ~0xful) | val);
7311 kvm_lmsw(vcpu, val);
7312
7313 return kvm_skip_emulated_instruction(vcpu);
7314 default:
7315 break;
7316 }
7317 vcpu->run->exit_reason = 0;
7318 vcpu_unimpl(vcpu, "unhandled control register: op %d cr %d\n",
7319 (int)(exit_qualification >> 4) & 3, cr);
7320 return 0;
7321 }
7322
handle_dr(struct kvm_vcpu * vcpu)7323 static int handle_dr(struct kvm_vcpu *vcpu)
7324 {
7325 unsigned long exit_qualification;
7326 int dr, dr7, reg;
7327
7328 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7329 dr = exit_qualification & DEBUG_REG_ACCESS_NUM;
7330
7331 /* First, if DR does not exist, trigger UD */
7332 if (!kvm_require_dr(vcpu, dr))
7333 return 1;
7334
7335 /* Do not handle if the CPL > 0, will trigger GP on re-entry */
7336 if (!kvm_require_cpl(vcpu, 0))
7337 return 1;
7338 dr7 = vmcs_readl(GUEST_DR7);
7339 if (dr7 & DR7_GD) {
7340 /*
7341 * As the vm-exit takes precedence over the debug trap, we
7342 * need to emulate the latter, either for the host or the
7343 * guest debugging itself.
7344 */
7345 if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP) {
7346 vcpu->run->debug.arch.dr6 = vcpu->arch.dr6;
7347 vcpu->run->debug.arch.dr7 = dr7;
7348 vcpu->run->debug.arch.pc = kvm_get_linear_rip(vcpu);
7349 vcpu->run->debug.arch.exception = DB_VECTOR;
7350 vcpu->run->exit_reason = KVM_EXIT_DEBUG;
7351 return 0;
7352 } else {
7353 vcpu->arch.dr6 &= ~15;
7354 vcpu->arch.dr6 |= DR6_BD | DR6_RTM;
7355 kvm_queue_exception(vcpu, DB_VECTOR);
7356 return 1;
7357 }
7358 }
7359
7360 if (vcpu->guest_debug == 0) {
7361 vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL,
7362 CPU_BASED_MOV_DR_EXITING);
7363
7364 /*
7365 * No more DR vmexits; force a reload of the debug registers
7366 * and reenter on this instruction. The next vmexit will
7367 * retrieve the full state of the debug registers.
7368 */
7369 vcpu->arch.switch_db_regs |= KVM_DEBUGREG_WONT_EXIT;
7370 return 1;
7371 }
7372
7373 reg = DEBUG_REG_ACCESS_REG(exit_qualification);
7374 if (exit_qualification & TYPE_MOV_FROM_DR) {
7375 unsigned long val;
7376
7377 if (kvm_get_dr(vcpu, dr, &val))
7378 return 1;
7379 kvm_register_write(vcpu, reg, val);
7380 } else
7381 if (kvm_set_dr(vcpu, dr, kvm_register_readl(vcpu, reg)))
7382 return 1;
7383
7384 return kvm_skip_emulated_instruction(vcpu);
7385 }
7386
vmx_get_dr6(struct kvm_vcpu * vcpu)7387 static u64 vmx_get_dr6(struct kvm_vcpu *vcpu)
7388 {
7389 return vcpu->arch.dr6;
7390 }
7391
vmx_set_dr6(struct kvm_vcpu * vcpu,unsigned long val)7392 static void vmx_set_dr6(struct kvm_vcpu *vcpu, unsigned long val)
7393 {
7394 }
7395
vmx_sync_dirty_debug_regs(struct kvm_vcpu * vcpu)7396 static void vmx_sync_dirty_debug_regs(struct kvm_vcpu *vcpu)
7397 {
7398 get_debugreg(vcpu->arch.db[0], 0);
7399 get_debugreg(vcpu->arch.db[1], 1);
7400 get_debugreg(vcpu->arch.db[2], 2);
7401 get_debugreg(vcpu->arch.db[3], 3);
7402 get_debugreg(vcpu->arch.dr6, 6);
7403 vcpu->arch.dr7 = vmcs_readl(GUEST_DR7);
7404
7405 vcpu->arch.switch_db_regs &= ~KVM_DEBUGREG_WONT_EXIT;
7406 vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL, CPU_BASED_MOV_DR_EXITING);
7407 }
7408
vmx_set_dr7(struct kvm_vcpu * vcpu,unsigned long val)7409 static void vmx_set_dr7(struct kvm_vcpu *vcpu, unsigned long val)
7410 {
7411 vmcs_writel(GUEST_DR7, val);
7412 }
7413
handle_cpuid(struct kvm_vcpu * vcpu)7414 static int handle_cpuid(struct kvm_vcpu *vcpu)
7415 {
7416 return kvm_emulate_cpuid(vcpu);
7417 }
7418
handle_rdmsr(struct kvm_vcpu * vcpu)7419 static int handle_rdmsr(struct kvm_vcpu *vcpu)
7420 {
7421 u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX];
7422 struct msr_data msr_info;
7423
7424 msr_info.index = ecx;
7425 msr_info.host_initiated = false;
7426 if (vmx_get_msr(vcpu, &msr_info)) {
7427 trace_kvm_msr_read_ex(ecx);
7428 kvm_inject_gp(vcpu, 0);
7429 return 1;
7430 }
7431
7432 trace_kvm_msr_read(ecx, msr_info.data);
7433
7434 /* FIXME: handling of bits 32:63 of rax, rdx */
7435 vcpu->arch.regs[VCPU_REGS_RAX] = msr_info.data & -1u;
7436 vcpu->arch.regs[VCPU_REGS_RDX] = (msr_info.data >> 32) & -1u;
7437 return kvm_skip_emulated_instruction(vcpu);
7438 }
7439
handle_wrmsr(struct kvm_vcpu * vcpu)7440 static int handle_wrmsr(struct kvm_vcpu *vcpu)
7441 {
7442 struct msr_data msr;
7443 u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX];
7444 u64 data = (vcpu->arch.regs[VCPU_REGS_RAX] & -1u)
7445 | ((u64)(vcpu->arch.regs[VCPU_REGS_RDX] & -1u) << 32);
7446
7447 msr.data = data;
7448 msr.index = ecx;
7449 msr.host_initiated = false;
7450 if (kvm_set_msr(vcpu, &msr) != 0) {
7451 trace_kvm_msr_write_ex(ecx, data);
7452 kvm_inject_gp(vcpu, 0);
7453 return 1;
7454 }
7455
7456 trace_kvm_msr_write(ecx, data);
7457 return kvm_skip_emulated_instruction(vcpu);
7458 }
7459
handle_tpr_below_threshold(struct kvm_vcpu * vcpu)7460 static int handle_tpr_below_threshold(struct kvm_vcpu *vcpu)
7461 {
7462 kvm_apic_update_ppr(vcpu);
7463 return 1;
7464 }
7465
handle_interrupt_window(struct kvm_vcpu * vcpu)7466 static int handle_interrupt_window(struct kvm_vcpu *vcpu)
7467 {
7468 vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL,
7469 CPU_BASED_VIRTUAL_INTR_PENDING);
7470
7471 kvm_make_request(KVM_REQ_EVENT, vcpu);
7472
7473 ++vcpu->stat.irq_window_exits;
7474 return 1;
7475 }
7476
handle_halt(struct kvm_vcpu * vcpu)7477 static int handle_halt(struct kvm_vcpu *vcpu)
7478 {
7479 return kvm_emulate_halt(vcpu);
7480 }
7481
handle_vmcall(struct kvm_vcpu * vcpu)7482 static int handle_vmcall(struct kvm_vcpu *vcpu)
7483 {
7484 return kvm_emulate_hypercall(vcpu);
7485 }
7486
handle_invd(struct kvm_vcpu * vcpu)7487 static int handle_invd(struct kvm_vcpu *vcpu)
7488 {
7489 return kvm_emulate_instruction(vcpu, 0) == EMULATE_DONE;
7490 }
7491
handle_invlpg(struct kvm_vcpu * vcpu)7492 static int handle_invlpg(struct kvm_vcpu *vcpu)
7493 {
7494 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7495
7496 kvm_mmu_invlpg(vcpu, exit_qualification);
7497 return kvm_skip_emulated_instruction(vcpu);
7498 }
7499
handle_rdpmc(struct kvm_vcpu * vcpu)7500 static int handle_rdpmc(struct kvm_vcpu *vcpu)
7501 {
7502 int err;
7503
7504 err = kvm_rdpmc(vcpu);
7505 return kvm_complete_insn_gp(vcpu, err);
7506 }
7507
handle_wbinvd(struct kvm_vcpu * vcpu)7508 static int handle_wbinvd(struct kvm_vcpu *vcpu)
7509 {
7510 return kvm_emulate_wbinvd(vcpu);
7511 }
7512
handle_xsetbv(struct kvm_vcpu * vcpu)7513 static int handle_xsetbv(struct kvm_vcpu *vcpu)
7514 {
7515 u64 new_bv = kvm_read_edx_eax(vcpu);
7516 u32 index = kvm_register_read(vcpu, VCPU_REGS_RCX);
7517
7518 if (kvm_set_xcr(vcpu, index, new_bv) == 0)
7519 return kvm_skip_emulated_instruction(vcpu);
7520 return 1;
7521 }
7522
handle_xsaves(struct kvm_vcpu * vcpu)7523 static int handle_xsaves(struct kvm_vcpu *vcpu)
7524 {
7525 kvm_skip_emulated_instruction(vcpu);
7526 WARN(1, "this should never happen\n");
7527 return 1;
7528 }
7529
handle_xrstors(struct kvm_vcpu * vcpu)7530 static int handle_xrstors(struct kvm_vcpu *vcpu)
7531 {
7532 kvm_skip_emulated_instruction(vcpu);
7533 WARN(1, "this should never happen\n");
7534 return 1;
7535 }
7536
handle_apic_access(struct kvm_vcpu * vcpu)7537 static int handle_apic_access(struct kvm_vcpu *vcpu)
7538 {
7539 if (likely(fasteoi)) {
7540 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7541 int access_type, offset;
7542
7543 access_type = exit_qualification & APIC_ACCESS_TYPE;
7544 offset = exit_qualification & APIC_ACCESS_OFFSET;
7545 /*
7546 * Sane guest uses MOV to write EOI, with written value
7547 * not cared. So make a short-circuit here by avoiding
7548 * heavy instruction emulation.
7549 */
7550 if ((access_type == TYPE_LINEAR_APIC_INST_WRITE) &&
7551 (offset == APIC_EOI)) {
7552 kvm_lapic_set_eoi(vcpu);
7553 return kvm_skip_emulated_instruction(vcpu);
7554 }
7555 }
7556 return kvm_emulate_instruction(vcpu, 0) == EMULATE_DONE;
7557 }
7558
handle_apic_eoi_induced(struct kvm_vcpu * vcpu)7559 static int handle_apic_eoi_induced(struct kvm_vcpu *vcpu)
7560 {
7561 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7562 int vector = exit_qualification & 0xff;
7563
7564 /* EOI-induced VM exit is trap-like and thus no need to adjust IP */
7565 kvm_apic_set_eoi_accelerated(vcpu, vector);
7566 return 1;
7567 }
7568
handle_apic_write(struct kvm_vcpu * vcpu)7569 static int handle_apic_write(struct kvm_vcpu *vcpu)
7570 {
7571 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7572 u32 offset = exit_qualification & 0xfff;
7573
7574 /* APIC-write VM exit is trap-like and thus no need to adjust IP */
7575 kvm_apic_write_nodecode(vcpu, offset);
7576 return 1;
7577 }
7578
handle_task_switch(struct kvm_vcpu * vcpu)7579 static int handle_task_switch(struct kvm_vcpu *vcpu)
7580 {
7581 struct vcpu_vmx *vmx = to_vmx(vcpu);
7582 unsigned long exit_qualification;
7583 bool has_error_code = false;
7584 u32 error_code = 0;
7585 u16 tss_selector;
7586 int reason, type, idt_v, idt_index;
7587
7588 idt_v = (vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK);
7589 idt_index = (vmx->idt_vectoring_info & VECTORING_INFO_VECTOR_MASK);
7590 type = (vmx->idt_vectoring_info & VECTORING_INFO_TYPE_MASK);
7591
7592 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7593
7594 reason = (u32)exit_qualification >> 30;
7595 if (reason == TASK_SWITCH_GATE && idt_v) {
7596 switch (type) {
7597 case INTR_TYPE_NMI_INTR:
7598 vcpu->arch.nmi_injected = false;
7599 vmx_set_nmi_mask(vcpu, true);
7600 break;
7601 case INTR_TYPE_EXT_INTR:
7602 case INTR_TYPE_SOFT_INTR:
7603 kvm_clear_interrupt_queue(vcpu);
7604 break;
7605 case INTR_TYPE_HARD_EXCEPTION:
7606 if (vmx->idt_vectoring_info &
7607 VECTORING_INFO_DELIVER_CODE_MASK) {
7608 has_error_code = true;
7609 error_code =
7610 vmcs_read32(IDT_VECTORING_ERROR_CODE);
7611 }
7612 /* fall through */
7613 case INTR_TYPE_SOFT_EXCEPTION:
7614 kvm_clear_exception_queue(vcpu);
7615 break;
7616 default:
7617 break;
7618 }
7619 }
7620 tss_selector = exit_qualification;
7621
7622 if (!idt_v || (type != INTR_TYPE_HARD_EXCEPTION &&
7623 type != INTR_TYPE_EXT_INTR &&
7624 type != INTR_TYPE_NMI_INTR))
7625 skip_emulated_instruction(vcpu);
7626
7627 if (kvm_task_switch(vcpu, tss_selector,
7628 type == INTR_TYPE_SOFT_INTR ? idt_index : -1, reason,
7629 has_error_code, error_code) == EMULATE_FAIL) {
7630 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
7631 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
7632 vcpu->run->internal.ndata = 0;
7633 return 0;
7634 }
7635
7636 /*
7637 * TODO: What about debug traps on tss switch?
7638 * Are we supposed to inject them and update dr6?
7639 */
7640
7641 return 1;
7642 }
7643
handle_ept_violation(struct kvm_vcpu * vcpu)7644 static int handle_ept_violation(struct kvm_vcpu *vcpu)
7645 {
7646 unsigned long exit_qualification;
7647 gpa_t gpa;
7648 u64 error_code;
7649
7650 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7651
7652 /*
7653 * EPT violation happened while executing iret from NMI,
7654 * "blocked by NMI" bit has to be set before next VM entry.
7655 * There are errata that may cause this bit to not be set:
7656 * AAK134, BY25.
7657 */
7658 if (!(to_vmx(vcpu)->idt_vectoring_info & VECTORING_INFO_VALID_MASK) &&
7659 enable_vnmi &&
7660 (exit_qualification & INTR_INFO_UNBLOCK_NMI))
7661 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO, GUEST_INTR_STATE_NMI);
7662
7663 gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS);
7664 trace_kvm_page_fault(gpa, exit_qualification);
7665
7666 /* Is it a read fault? */
7667 error_code = (exit_qualification & EPT_VIOLATION_ACC_READ)
7668 ? PFERR_USER_MASK : 0;
7669 /* Is it a write fault? */
7670 error_code |= (exit_qualification & EPT_VIOLATION_ACC_WRITE)
7671 ? PFERR_WRITE_MASK : 0;
7672 /* Is it a fetch fault? */
7673 error_code |= (exit_qualification & EPT_VIOLATION_ACC_INSTR)
7674 ? PFERR_FETCH_MASK : 0;
7675 /* ept page table entry is present? */
7676 error_code |= (exit_qualification &
7677 (EPT_VIOLATION_READABLE | EPT_VIOLATION_WRITABLE |
7678 EPT_VIOLATION_EXECUTABLE))
7679 ? PFERR_PRESENT_MASK : 0;
7680
7681 error_code |= (exit_qualification & 0x100) != 0 ?
7682 PFERR_GUEST_FINAL_MASK : PFERR_GUEST_PAGE_MASK;
7683
7684 vcpu->arch.exit_qualification = exit_qualification;
7685 return kvm_mmu_page_fault(vcpu, gpa, error_code, NULL, 0);
7686 }
7687
handle_ept_misconfig(struct kvm_vcpu * vcpu)7688 static int handle_ept_misconfig(struct kvm_vcpu *vcpu)
7689 {
7690 gpa_t gpa;
7691
7692 /*
7693 * A nested guest cannot optimize MMIO vmexits, because we have an
7694 * nGPA here instead of the required GPA.
7695 */
7696 gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS);
7697 if (!is_guest_mode(vcpu) &&
7698 !kvm_io_bus_write(vcpu, KVM_FAST_MMIO_BUS, gpa, 0, NULL)) {
7699 trace_kvm_fast_mmio(gpa);
7700 /*
7701 * Doing kvm_skip_emulated_instruction() depends on undefined
7702 * behavior: Intel's manual doesn't mandate
7703 * VM_EXIT_INSTRUCTION_LEN to be set in VMCS when EPT MISCONFIG
7704 * occurs and while on real hardware it was observed to be set,
7705 * other hypervisors (namely Hyper-V) don't set it, we end up
7706 * advancing IP with some random value. Disable fast mmio when
7707 * running nested and keep it for real hardware in hope that
7708 * VM_EXIT_INSTRUCTION_LEN will always be set correctly.
7709 */
7710 if (!static_cpu_has(X86_FEATURE_HYPERVISOR))
7711 return kvm_skip_emulated_instruction(vcpu);
7712 else
7713 return kvm_emulate_instruction(vcpu, EMULTYPE_SKIP) ==
7714 EMULATE_DONE;
7715 }
7716
7717 return kvm_mmu_page_fault(vcpu, gpa, PFERR_RSVD_MASK, NULL, 0);
7718 }
7719
handle_nmi_window(struct kvm_vcpu * vcpu)7720 static int handle_nmi_window(struct kvm_vcpu *vcpu)
7721 {
7722 WARN_ON_ONCE(!enable_vnmi);
7723 vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL,
7724 CPU_BASED_VIRTUAL_NMI_PENDING);
7725 ++vcpu->stat.nmi_window_exits;
7726 kvm_make_request(KVM_REQ_EVENT, vcpu);
7727
7728 return 1;
7729 }
7730
handle_invalid_guest_state(struct kvm_vcpu * vcpu)7731 static int handle_invalid_guest_state(struct kvm_vcpu *vcpu)
7732 {
7733 struct vcpu_vmx *vmx = to_vmx(vcpu);
7734 enum emulation_result err = EMULATE_DONE;
7735 int ret = 1;
7736 u32 cpu_exec_ctrl;
7737 bool intr_window_requested;
7738 unsigned count = 130;
7739
7740 /*
7741 * We should never reach the point where we are emulating L2
7742 * due to invalid guest state as that means we incorrectly
7743 * allowed a nested VMEntry with an invalid vmcs12.
7744 */
7745 WARN_ON_ONCE(vmx->emulation_required && vmx->nested.nested_run_pending);
7746
7747 cpu_exec_ctrl = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
7748 intr_window_requested = cpu_exec_ctrl & CPU_BASED_VIRTUAL_INTR_PENDING;
7749
7750 while (vmx->emulation_required && count-- != 0) {
7751 if (intr_window_requested && vmx_interrupt_allowed(vcpu))
7752 return handle_interrupt_window(&vmx->vcpu);
7753
7754 if (kvm_test_request(KVM_REQ_EVENT, vcpu))
7755 return 1;
7756
7757 err = kvm_emulate_instruction(vcpu, 0);
7758
7759 if (err == EMULATE_USER_EXIT) {
7760 ++vcpu->stat.mmio_exits;
7761 ret = 0;
7762 goto out;
7763 }
7764
7765 if (err != EMULATE_DONE)
7766 goto emulation_error;
7767
7768 if (vmx->emulation_required && !vmx->rmode.vm86_active &&
7769 vcpu->arch.exception.pending)
7770 goto emulation_error;
7771
7772 if (vcpu->arch.halt_request) {
7773 vcpu->arch.halt_request = 0;
7774 ret = kvm_vcpu_halt(vcpu);
7775 goto out;
7776 }
7777
7778 if (signal_pending(current))
7779 goto out;
7780 if (need_resched())
7781 schedule();
7782 }
7783
7784 out:
7785 return ret;
7786
7787 emulation_error:
7788 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
7789 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
7790 vcpu->run->internal.ndata = 0;
7791 return 0;
7792 }
7793
grow_ple_window(struct kvm_vcpu * vcpu)7794 static void grow_ple_window(struct kvm_vcpu *vcpu)
7795 {
7796 struct vcpu_vmx *vmx = to_vmx(vcpu);
7797 int old = vmx->ple_window;
7798
7799 vmx->ple_window = __grow_ple_window(old, ple_window,
7800 ple_window_grow,
7801 ple_window_max);
7802
7803 if (vmx->ple_window != old)
7804 vmx->ple_window_dirty = true;
7805
7806 trace_kvm_ple_window_grow(vcpu->vcpu_id, vmx->ple_window, old);
7807 }
7808
shrink_ple_window(struct kvm_vcpu * vcpu)7809 static void shrink_ple_window(struct kvm_vcpu *vcpu)
7810 {
7811 struct vcpu_vmx *vmx = to_vmx(vcpu);
7812 int old = vmx->ple_window;
7813
7814 vmx->ple_window = __shrink_ple_window(old, ple_window,
7815 ple_window_shrink,
7816 ple_window);
7817
7818 if (vmx->ple_window != old)
7819 vmx->ple_window_dirty = true;
7820
7821 trace_kvm_ple_window_shrink(vcpu->vcpu_id, vmx->ple_window, old);
7822 }
7823
7824 /*
7825 * Handler for POSTED_INTERRUPT_WAKEUP_VECTOR.
7826 */
wakeup_handler(void)7827 static void wakeup_handler(void)
7828 {
7829 struct kvm_vcpu *vcpu;
7830 int cpu = smp_processor_id();
7831
7832 spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, cpu));
7833 list_for_each_entry(vcpu, &per_cpu(blocked_vcpu_on_cpu, cpu),
7834 blocked_vcpu_list) {
7835 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
7836
7837 if (pi_test_on(pi_desc) == 1)
7838 kvm_vcpu_kick(vcpu);
7839 }
7840 spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, cpu));
7841 }
7842
vmx_enable_tdp(void)7843 static void vmx_enable_tdp(void)
7844 {
7845 kvm_mmu_set_mask_ptes(VMX_EPT_READABLE_MASK,
7846 enable_ept_ad_bits ? VMX_EPT_ACCESS_BIT : 0ull,
7847 enable_ept_ad_bits ? VMX_EPT_DIRTY_BIT : 0ull,
7848 0ull, VMX_EPT_EXECUTABLE_MASK,
7849 cpu_has_vmx_ept_execute_only() ? 0ull : VMX_EPT_READABLE_MASK,
7850 VMX_EPT_RWX_MASK, 0ull);
7851
7852 ept_set_mmio_spte_mask();
7853 kvm_enable_tdp();
7854 }
7855
hardware_setup(void)7856 static __init int hardware_setup(void)
7857 {
7858 unsigned long host_bndcfgs;
7859 int r = -ENOMEM, i;
7860
7861 rdmsrl_safe(MSR_EFER, &host_efer);
7862
7863 for (i = 0; i < ARRAY_SIZE(vmx_msr_index); ++i)
7864 kvm_define_shared_msr(i, vmx_msr_index[i]);
7865
7866 for (i = 0; i < VMX_BITMAP_NR; i++) {
7867 vmx_bitmap[i] = (unsigned long *)__get_free_page(GFP_KERNEL);
7868 if (!vmx_bitmap[i])
7869 goto out;
7870 }
7871
7872 memset(vmx_vmread_bitmap, 0xff, PAGE_SIZE);
7873 memset(vmx_vmwrite_bitmap, 0xff, PAGE_SIZE);
7874
7875 if (setup_vmcs_config(&vmcs_config) < 0) {
7876 r = -EIO;
7877 goto out;
7878 }
7879
7880 if (boot_cpu_has(X86_FEATURE_NX))
7881 kvm_enable_efer_bits(EFER_NX);
7882
7883 if (boot_cpu_has(X86_FEATURE_MPX)) {
7884 rdmsrl(MSR_IA32_BNDCFGS, host_bndcfgs);
7885 WARN_ONCE(host_bndcfgs, "KVM: BNDCFGS in host will be lost");
7886 }
7887
7888 if (!cpu_has_vmx_vpid() || !cpu_has_vmx_invvpid() ||
7889 !(cpu_has_vmx_invvpid_single() || cpu_has_vmx_invvpid_global()))
7890 enable_vpid = 0;
7891
7892 if (!cpu_has_vmx_ept() ||
7893 !cpu_has_vmx_ept_4levels() ||
7894 !cpu_has_vmx_ept_mt_wb() ||
7895 !cpu_has_vmx_invept_global())
7896 enable_ept = 0;
7897
7898 if (!cpu_has_vmx_ept_ad_bits() || !enable_ept)
7899 enable_ept_ad_bits = 0;
7900
7901 if (!cpu_has_vmx_unrestricted_guest() || !enable_ept)
7902 enable_unrestricted_guest = 0;
7903
7904 if (!cpu_has_vmx_flexpriority())
7905 flexpriority_enabled = 0;
7906
7907 if (!cpu_has_virtual_nmis())
7908 enable_vnmi = 0;
7909
7910 /*
7911 * set_apic_access_page_addr() is used to reload apic access
7912 * page upon invalidation. No need to do anything if not
7913 * using the APIC_ACCESS_ADDR VMCS field.
7914 */
7915 if (!flexpriority_enabled)
7916 kvm_x86_ops->set_apic_access_page_addr = NULL;
7917
7918 if (!cpu_has_vmx_tpr_shadow())
7919 kvm_x86_ops->update_cr8_intercept = NULL;
7920
7921 if (enable_ept && !cpu_has_vmx_ept_2m_page())
7922 kvm_disable_largepages();
7923
7924 #if IS_ENABLED(CONFIG_HYPERV)
7925 if (ms_hyperv.nested_features & HV_X64_NESTED_GUEST_MAPPING_FLUSH
7926 && enable_ept)
7927 kvm_x86_ops->tlb_remote_flush = vmx_hv_remote_flush_tlb;
7928 #endif
7929
7930 if (!cpu_has_vmx_ple()) {
7931 ple_gap = 0;
7932 ple_window = 0;
7933 ple_window_grow = 0;
7934 ple_window_max = 0;
7935 ple_window_shrink = 0;
7936 }
7937
7938 if (!cpu_has_vmx_apicv()) {
7939 enable_apicv = 0;
7940 kvm_x86_ops->sync_pir_to_irr = NULL;
7941 }
7942
7943 if (cpu_has_vmx_tsc_scaling()) {
7944 kvm_has_tsc_control = true;
7945 kvm_max_tsc_scaling_ratio = KVM_VMX_TSC_MULTIPLIER_MAX;
7946 kvm_tsc_scaling_ratio_frac_bits = 48;
7947 }
7948
7949 set_bit(0, vmx_vpid_bitmap); /* 0 is reserved for host */
7950
7951 if (enable_ept)
7952 vmx_enable_tdp();
7953 else
7954 kvm_disable_tdp();
7955
7956 if (!nested) {
7957 kvm_x86_ops->get_nested_state = NULL;
7958 kvm_x86_ops->set_nested_state = NULL;
7959 }
7960
7961 /*
7962 * Only enable PML when hardware supports PML feature, and both EPT
7963 * and EPT A/D bit features are enabled -- PML depends on them to work.
7964 */
7965 if (!enable_ept || !enable_ept_ad_bits || !cpu_has_vmx_pml())
7966 enable_pml = 0;
7967
7968 if (!enable_pml) {
7969 kvm_x86_ops->slot_enable_log_dirty = NULL;
7970 kvm_x86_ops->slot_disable_log_dirty = NULL;
7971 kvm_x86_ops->flush_log_dirty = NULL;
7972 kvm_x86_ops->enable_log_dirty_pt_masked = NULL;
7973 }
7974
7975 if (!cpu_has_vmx_preemption_timer())
7976 kvm_x86_ops->request_immediate_exit = __kvm_request_immediate_exit;
7977
7978 if (cpu_has_vmx_preemption_timer() && enable_preemption_timer) {
7979 u64 vmx_msr;
7980
7981 rdmsrl(MSR_IA32_VMX_MISC, vmx_msr);
7982 cpu_preemption_timer_multi =
7983 vmx_msr & VMX_MISC_PREEMPTION_TIMER_RATE_MASK;
7984 } else {
7985 kvm_x86_ops->set_hv_timer = NULL;
7986 kvm_x86_ops->cancel_hv_timer = NULL;
7987 }
7988
7989 if (!cpu_has_vmx_shadow_vmcs())
7990 enable_shadow_vmcs = 0;
7991 if (enable_shadow_vmcs)
7992 init_vmcs_shadow_fields();
7993
7994 kvm_set_posted_intr_wakeup_handler(wakeup_handler);
7995 nested_vmx_setup_ctls_msrs(&vmcs_config.nested, enable_apicv);
7996
7997 kvm_mce_cap_supported |= MCG_LMCE_P;
7998
7999 r = alloc_kvm_area();
8000 if (r)
8001 goto out;
8002 return 0;
8003
8004 out:
8005 for (i = 0; i < VMX_BITMAP_NR; i++)
8006 free_page((unsigned long)vmx_bitmap[i]);
8007
8008 return r;
8009 }
8010
hardware_unsetup(void)8011 static __exit void hardware_unsetup(void)
8012 {
8013 int i;
8014
8015 for (i = 0; i < VMX_BITMAP_NR; i++)
8016 free_page((unsigned long)vmx_bitmap[i]);
8017
8018 free_kvm_area();
8019 }
8020
8021 /*
8022 * Indicate a busy-waiting vcpu in spinlock. We do not enable the PAUSE
8023 * exiting, so only get here on cpu with PAUSE-Loop-Exiting.
8024 */
handle_pause(struct kvm_vcpu * vcpu)8025 static int handle_pause(struct kvm_vcpu *vcpu)
8026 {
8027 if (!kvm_pause_in_guest(vcpu->kvm))
8028 grow_ple_window(vcpu);
8029
8030 /*
8031 * Intel sdm vol3 ch-25.1.3 says: The "PAUSE-loop exiting"
8032 * VM-execution control is ignored if CPL > 0. OTOH, KVM
8033 * never set PAUSE_EXITING and just set PLE if supported,
8034 * so the vcpu must be CPL=0 if it gets a PAUSE exit.
8035 */
8036 kvm_vcpu_on_spin(vcpu, true);
8037 return kvm_skip_emulated_instruction(vcpu);
8038 }
8039
handle_nop(struct kvm_vcpu * vcpu)8040 static int handle_nop(struct kvm_vcpu *vcpu)
8041 {
8042 return kvm_skip_emulated_instruction(vcpu);
8043 }
8044
handle_mwait(struct kvm_vcpu * vcpu)8045 static int handle_mwait(struct kvm_vcpu *vcpu)
8046 {
8047 printk_once(KERN_WARNING "kvm: MWAIT instruction emulated as NOP!\n");
8048 return handle_nop(vcpu);
8049 }
8050
handle_invalid_op(struct kvm_vcpu * vcpu)8051 static int handle_invalid_op(struct kvm_vcpu *vcpu)
8052 {
8053 kvm_queue_exception(vcpu, UD_VECTOR);
8054 return 1;
8055 }
8056
handle_monitor_trap(struct kvm_vcpu * vcpu)8057 static int handle_monitor_trap(struct kvm_vcpu *vcpu)
8058 {
8059 return 1;
8060 }
8061
handle_monitor(struct kvm_vcpu * vcpu)8062 static int handle_monitor(struct kvm_vcpu *vcpu)
8063 {
8064 printk_once(KERN_WARNING "kvm: MONITOR instruction emulated as NOP!\n");
8065 return handle_nop(vcpu);
8066 }
8067
8068 /*
8069 * The following 3 functions, nested_vmx_succeed()/failValid()/failInvalid(),
8070 * set the success or error code of an emulated VMX instruction, as specified
8071 * by Vol 2B, VMX Instruction Reference, "Conventions".
8072 */
nested_vmx_succeed(struct kvm_vcpu * vcpu)8073 static void nested_vmx_succeed(struct kvm_vcpu *vcpu)
8074 {
8075 vmx_set_rflags(vcpu, vmx_get_rflags(vcpu)
8076 & ~(X86_EFLAGS_CF | X86_EFLAGS_PF | X86_EFLAGS_AF |
8077 X86_EFLAGS_ZF | X86_EFLAGS_SF | X86_EFLAGS_OF));
8078 }
8079
nested_vmx_failInvalid(struct kvm_vcpu * vcpu)8080 static void nested_vmx_failInvalid(struct kvm_vcpu *vcpu)
8081 {
8082 vmx_set_rflags(vcpu, (vmx_get_rflags(vcpu)
8083 & ~(X86_EFLAGS_PF | X86_EFLAGS_AF | X86_EFLAGS_ZF |
8084 X86_EFLAGS_SF | X86_EFLAGS_OF))
8085 | X86_EFLAGS_CF);
8086 }
8087
nested_vmx_failValid(struct kvm_vcpu * vcpu,u32 vm_instruction_error)8088 static void nested_vmx_failValid(struct kvm_vcpu *vcpu,
8089 u32 vm_instruction_error)
8090 {
8091 if (to_vmx(vcpu)->nested.current_vmptr == -1ull) {
8092 /*
8093 * failValid writes the error number to the current VMCS, which
8094 * can't be done there isn't a current VMCS.
8095 */
8096 nested_vmx_failInvalid(vcpu);
8097 return;
8098 }
8099 vmx_set_rflags(vcpu, (vmx_get_rflags(vcpu)
8100 & ~(X86_EFLAGS_CF | X86_EFLAGS_PF | X86_EFLAGS_AF |
8101 X86_EFLAGS_SF | X86_EFLAGS_OF))
8102 | X86_EFLAGS_ZF);
8103 get_vmcs12(vcpu)->vm_instruction_error = vm_instruction_error;
8104 /*
8105 * We don't need to force a shadow sync because
8106 * VM_INSTRUCTION_ERROR is not shadowed
8107 */
8108 }
8109
nested_vmx_abort(struct kvm_vcpu * vcpu,u32 indicator)8110 static void nested_vmx_abort(struct kvm_vcpu *vcpu, u32 indicator)
8111 {
8112 /* TODO: not to reset guest simply here. */
8113 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
8114 pr_debug_ratelimited("kvm: nested vmx abort, indicator %d\n", indicator);
8115 }
8116
vmx_preemption_timer_fn(struct hrtimer * timer)8117 static enum hrtimer_restart vmx_preemption_timer_fn(struct hrtimer *timer)
8118 {
8119 struct vcpu_vmx *vmx =
8120 container_of(timer, struct vcpu_vmx, nested.preemption_timer);
8121
8122 vmx->nested.preemption_timer_expired = true;
8123 kvm_make_request(KVM_REQ_EVENT, &vmx->vcpu);
8124 kvm_vcpu_kick(&vmx->vcpu);
8125
8126 return HRTIMER_NORESTART;
8127 }
8128
8129 /*
8130 * Decode the memory-address operand of a vmx instruction, as recorded on an
8131 * exit caused by such an instruction (run by a guest hypervisor).
8132 * On success, returns 0. When the operand is invalid, returns 1 and throws
8133 * #UD or #GP.
8134 */
get_vmx_mem_address(struct kvm_vcpu * vcpu,unsigned long exit_qualification,u32 vmx_instruction_info,bool wr,gva_t * ret)8135 static int get_vmx_mem_address(struct kvm_vcpu *vcpu,
8136 unsigned long exit_qualification,
8137 u32 vmx_instruction_info, bool wr, gva_t *ret)
8138 {
8139 gva_t off;
8140 bool exn;
8141 struct kvm_segment s;
8142
8143 /*
8144 * According to Vol. 3B, "Information for VM Exits Due to Instruction
8145 * Execution", on an exit, vmx_instruction_info holds most of the
8146 * addressing components of the operand. Only the displacement part
8147 * is put in exit_qualification (see 3B, "Basic VM-Exit Information").
8148 * For how an actual address is calculated from all these components,
8149 * refer to Vol. 1, "Operand Addressing".
8150 */
8151 int scaling = vmx_instruction_info & 3;
8152 int addr_size = (vmx_instruction_info >> 7) & 7;
8153 bool is_reg = vmx_instruction_info & (1u << 10);
8154 int seg_reg = (vmx_instruction_info >> 15) & 7;
8155 int index_reg = (vmx_instruction_info >> 18) & 0xf;
8156 bool index_is_valid = !(vmx_instruction_info & (1u << 22));
8157 int base_reg = (vmx_instruction_info >> 23) & 0xf;
8158 bool base_is_valid = !(vmx_instruction_info & (1u << 27));
8159
8160 if (is_reg) {
8161 kvm_queue_exception(vcpu, UD_VECTOR);
8162 return 1;
8163 }
8164
8165 /* Addr = segment_base + offset */
8166 /* offset = base + [index * scale] + displacement */
8167 off = exit_qualification; /* holds the displacement */
8168 if (addr_size == 1)
8169 off = (gva_t)sign_extend64(off, 31);
8170 else if (addr_size == 0)
8171 off = (gva_t)sign_extend64(off, 15);
8172 if (base_is_valid)
8173 off += kvm_register_read(vcpu, base_reg);
8174 if (index_is_valid)
8175 off += kvm_register_read(vcpu, index_reg)<<scaling;
8176 vmx_get_segment(vcpu, &s, seg_reg);
8177
8178 /*
8179 * The effective address, i.e. @off, of a memory operand is truncated
8180 * based on the address size of the instruction. Note that this is
8181 * the *effective address*, i.e. the address prior to accounting for
8182 * the segment's base.
8183 */
8184 if (addr_size == 1) /* 32 bit */
8185 off &= 0xffffffff;
8186 else if (addr_size == 0) /* 16 bit */
8187 off &= 0xffff;
8188
8189 /* Checks for #GP/#SS exceptions. */
8190 exn = false;
8191 if (is_long_mode(vcpu)) {
8192 /*
8193 * The virtual/linear address is never truncated in 64-bit
8194 * mode, e.g. a 32-bit address size can yield a 64-bit virtual
8195 * address when using FS/GS with a non-zero base.
8196 */
8197 *ret = s.base + off;
8198
8199 /* Long mode: #GP(0)/#SS(0) if the memory address is in a
8200 * non-canonical form. This is the only check on the memory
8201 * destination for long mode!
8202 */
8203 exn = is_noncanonical_address(*ret, vcpu);
8204 } else if (is_protmode(vcpu)) {
8205 /*
8206 * When not in long mode, the virtual/linear address is
8207 * unconditionally truncated to 32 bits regardless of the
8208 * address size.
8209 */
8210 *ret = (s.base + off) & 0xffffffff;
8211
8212 /* Protected mode: apply checks for segment validity in the
8213 * following order:
8214 * - segment type check (#GP(0) may be thrown)
8215 * - usability check (#GP(0)/#SS(0))
8216 * - limit check (#GP(0)/#SS(0))
8217 */
8218 if (wr)
8219 /* #GP(0) if the destination operand is located in a
8220 * read-only data segment or any code segment.
8221 */
8222 exn = ((s.type & 0xa) == 0 || (s.type & 8));
8223 else
8224 /* #GP(0) if the source operand is located in an
8225 * execute-only code segment
8226 */
8227 exn = ((s.type & 0xa) == 8);
8228 if (exn) {
8229 kvm_queue_exception_e(vcpu, GP_VECTOR, 0);
8230 return 1;
8231 }
8232 /* Protected mode: #GP(0)/#SS(0) if the segment is unusable.
8233 */
8234 exn = (s.unusable != 0);
8235
8236 /*
8237 * Protected mode: #GP(0)/#SS(0) if the memory operand is
8238 * outside the segment limit. All CPUs that support VMX ignore
8239 * limit checks for flat segments, i.e. segments with base==0,
8240 * limit==0xffffffff and of type expand-up data or code.
8241 */
8242 if (!(s.base == 0 && s.limit == 0xffffffff &&
8243 ((s.type & 8) || !(s.type & 4))))
8244 exn = exn || (off + sizeof(u64) > s.limit);
8245 }
8246 if (exn) {
8247 kvm_queue_exception_e(vcpu,
8248 seg_reg == VCPU_SREG_SS ?
8249 SS_VECTOR : GP_VECTOR,
8250 0);
8251 return 1;
8252 }
8253
8254 return 0;
8255 }
8256
nested_vmx_get_vmptr(struct kvm_vcpu * vcpu,gpa_t * vmpointer)8257 static int nested_vmx_get_vmptr(struct kvm_vcpu *vcpu, gpa_t *vmpointer)
8258 {
8259 gva_t gva;
8260 struct x86_exception e;
8261
8262 if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
8263 vmcs_read32(VMX_INSTRUCTION_INFO), false, &gva))
8264 return 1;
8265
8266 if (kvm_read_guest_virt(vcpu, gva, vmpointer, sizeof(*vmpointer), &e)) {
8267 kvm_inject_page_fault(vcpu, &e);
8268 return 1;
8269 }
8270
8271 return 0;
8272 }
8273
8274 /*
8275 * Allocate a shadow VMCS and associate it with the currently loaded
8276 * VMCS, unless such a shadow VMCS already exists. The newly allocated
8277 * VMCS is also VMCLEARed, so that it is ready for use.
8278 */
alloc_shadow_vmcs(struct kvm_vcpu * vcpu)8279 static struct vmcs *alloc_shadow_vmcs(struct kvm_vcpu *vcpu)
8280 {
8281 struct vcpu_vmx *vmx = to_vmx(vcpu);
8282 struct loaded_vmcs *loaded_vmcs = vmx->loaded_vmcs;
8283
8284 /*
8285 * We should allocate a shadow vmcs for vmcs01 only when L1
8286 * executes VMXON and free it when L1 executes VMXOFF.
8287 * As it is invalid to execute VMXON twice, we shouldn't reach
8288 * here when vmcs01 already have an allocated shadow vmcs.
8289 */
8290 WARN_ON(loaded_vmcs == &vmx->vmcs01 && loaded_vmcs->shadow_vmcs);
8291
8292 if (!loaded_vmcs->shadow_vmcs) {
8293 loaded_vmcs->shadow_vmcs = alloc_vmcs(true);
8294 if (loaded_vmcs->shadow_vmcs)
8295 vmcs_clear(loaded_vmcs->shadow_vmcs);
8296 }
8297 return loaded_vmcs->shadow_vmcs;
8298 }
8299
enter_vmx_operation(struct kvm_vcpu * vcpu)8300 static int enter_vmx_operation(struct kvm_vcpu *vcpu)
8301 {
8302 struct vcpu_vmx *vmx = to_vmx(vcpu);
8303 int r;
8304
8305 r = alloc_loaded_vmcs(&vmx->nested.vmcs02);
8306 if (r < 0)
8307 goto out_vmcs02;
8308
8309 vmx->nested.cached_vmcs12 = kzalloc(VMCS12_SIZE, GFP_KERNEL);
8310 if (!vmx->nested.cached_vmcs12)
8311 goto out_cached_vmcs12;
8312
8313 vmx->nested.cached_shadow_vmcs12 = kzalloc(VMCS12_SIZE, GFP_KERNEL);
8314 if (!vmx->nested.cached_shadow_vmcs12)
8315 goto out_cached_shadow_vmcs12;
8316
8317 if (enable_shadow_vmcs && !alloc_shadow_vmcs(vcpu))
8318 goto out_shadow_vmcs;
8319
8320 hrtimer_init(&vmx->nested.preemption_timer, CLOCK_MONOTONIC,
8321 HRTIMER_MODE_REL_PINNED);
8322 vmx->nested.preemption_timer.function = vmx_preemption_timer_fn;
8323
8324 vmx->nested.vpid02 = allocate_vpid();
8325
8326 vmx->nested.vmxon = true;
8327 return 0;
8328
8329 out_shadow_vmcs:
8330 kfree(vmx->nested.cached_shadow_vmcs12);
8331
8332 out_cached_shadow_vmcs12:
8333 kfree(vmx->nested.cached_vmcs12);
8334
8335 out_cached_vmcs12:
8336 free_loaded_vmcs(&vmx->nested.vmcs02);
8337
8338 out_vmcs02:
8339 return -ENOMEM;
8340 }
8341
8342 /*
8343 * Emulate the VMXON instruction.
8344 * Currently, we just remember that VMX is active, and do not save or even
8345 * inspect the argument to VMXON (the so-called "VMXON pointer") because we
8346 * do not currently need to store anything in that guest-allocated memory
8347 * region. Consequently, VMCLEAR and VMPTRLD also do not verify that the their
8348 * argument is different from the VMXON pointer (which the spec says they do).
8349 */
handle_vmon(struct kvm_vcpu * vcpu)8350 static int handle_vmon(struct kvm_vcpu *vcpu)
8351 {
8352 int ret;
8353 gpa_t vmptr;
8354 struct page *page;
8355 struct vcpu_vmx *vmx = to_vmx(vcpu);
8356 const u64 VMXON_NEEDED_FEATURES = FEATURE_CONTROL_LOCKED
8357 | FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
8358
8359 /*
8360 * The Intel VMX Instruction Reference lists a bunch of bits that are
8361 * prerequisite to running VMXON, most notably cr4.VMXE must be set to
8362 * 1 (see vmx_set_cr4() for when we allow the guest to set this).
8363 * Otherwise, we should fail with #UD. But most faulting conditions
8364 * have already been checked by hardware, prior to the VM-exit for
8365 * VMXON. We do test guest cr4.VMXE because processor CR4 always has
8366 * that bit set to 1 in non-root mode.
8367 */
8368 if (!kvm_read_cr4_bits(vcpu, X86_CR4_VMXE)) {
8369 kvm_queue_exception(vcpu, UD_VECTOR);
8370 return 1;
8371 }
8372
8373 /* CPL=0 must be checked manually. */
8374 if (vmx_get_cpl(vcpu)) {
8375 kvm_inject_gp(vcpu, 0);
8376 return 1;
8377 }
8378
8379 if (vmx->nested.vmxon) {
8380 nested_vmx_failValid(vcpu, VMXERR_VMXON_IN_VMX_ROOT_OPERATION);
8381 return kvm_skip_emulated_instruction(vcpu);
8382 }
8383
8384 if ((vmx->msr_ia32_feature_control & VMXON_NEEDED_FEATURES)
8385 != VMXON_NEEDED_FEATURES) {
8386 kvm_inject_gp(vcpu, 0);
8387 return 1;
8388 }
8389
8390 if (nested_vmx_get_vmptr(vcpu, &vmptr))
8391 return 1;
8392
8393 /*
8394 * SDM 3: 24.11.5
8395 * The first 4 bytes of VMXON region contain the supported
8396 * VMCS revision identifier
8397 *
8398 * Note - IA32_VMX_BASIC[48] will never be 1 for the nested case;
8399 * which replaces physical address width with 32
8400 */
8401 if (!PAGE_ALIGNED(vmptr) || (vmptr >> cpuid_maxphyaddr(vcpu))) {
8402 nested_vmx_failInvalid(vcpu);
8403 return kvm_skip_emulated_instruction(vcpu);
8404 }
8405
8406 page = kvm_vcpu_gpa_to_page(vcpu, vmptr);
8407 if (is_error_page(page)) {
8408 nested_vmx_failInvalid(vcpu);
8409 return kvm_skip_emulated_instruction(vcpu);
8410 }
8411 if (*(u32 *)kmap(page) != VMCS12_REVISION) {
8412 kunmap(page);
8413 kvm_release_page_clean(page);
8414 nested_vmx_failInvalid(vcpu);
8415 return kvm_skip_emulated_instruction(vcpu);
8416 }
8417 kunmap(page);
8418 kvm_release_page_clean(page);
8419
8420 vmx->nested.vmxon_ptr = vmptr;
8421 ret = enter_vmx_operation(vcpu);
8422 if (ret)
8423 return ret;
8424
8425 nested_vmx_succeed(vcpu);
8426 return kvm_skip_emulated_instruction(vcpu);
8427 }
8428
8429 /*
8430 * Intel's VMX Instruction Reference specifies a common set of prerequisites
8431 * for running VMX instructions (except VMXON, whose prerequisites are
8432 * slightly different). It also specifies what exception to inject otherwise.
8433 * Note that many of these exceptions have priority over VM exits, so they
8434 * don't have to be checked again here.
8435 */
nested_vmx_check_permission(struct kvm_vcpu * vcpu)8436 static int nested_vmx_check_permission(struct kvm_vcpu *vcpu)
8437 {
8438 if (!to_vmx(vcpu)->nested.vmxon) {
8439 kvm_queue_exception(vcpu, UD_VECTOR);
8440 return 0;
8441 }
8442
8443 if (vmx_get_cpl(vcpu)) {
8444 kvm_inject_gp(vcpu, 0);
8445 return 0;
8446 }
8447
8448 return 1;
8449 }
8450
vmx_disable_shadow_vmcs(struct vcpu_vmx * vmx)8451 static void vmx_disable_shadow_vmcs(struct vcpu_vmx *vmx)
8452 {
8453 vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL, SECONDARY_EXEC_SHADOW_VMCS);
8454 vmcs_write64(VMCS_LINK_POINTER, -1ull);
8455 vmx->nested.sync_shadow_vmcs = false;
8456 }
8457
nested_release_vmcs12(struct vcpu_vmx * vmx)8458 static inline void nested_release_vmcs12(struct vcpu_vmx *vmx)
8459 {
8460 if (vmx->nested.current_vmptr == -1ull)
8461 return;
8462
8463 if (enable_shadow_vmcs) {
8464 /* copy to memory all shadowed fields in case
8465 they were modified */
8466 copy_shadow_to_vmcs12(vmx);
8467 vmx_disable_shadow_vmcs(vmx);
8468 }
8469 vmx->nested.posted_intr_nv = -1;
8470
8471 /* Flush VMCS12 to guest memory */
8472 kvm_vcpu_write_guest_page(&vmx->vcpu,
8473 vmx->nested.current_vmptr >> PAGE_SHIFT,
8474 vmx->nested.cached_vmcs12, 0, VMCS12_SIZE);
8475
8476 vmx->nested.current_vmptr = -1ull;
8477 }
8478
8479 /*
8480 * Free whatever needs to be freed from vmx->nested when L1 goes down, or
8481 * just stops using VMX.
8482 */
free_nested(struct vcpu_vmx * vmx)8483 static void free_nested(struct vcpu_vmx *vmx)
8484 {
8485 if (!vmx->nested.vmxon && !vmx->nested.smm.vmxon)
8486 return;
8487
8488 kvm_clear_request(KVM_REQ_GET_VMCS12_PAGES, &vmx->vcpu);
8489
8490 hrtimer_cancel(&vmx->nested.preemption_timer);
8491 vmx->nested.vmxon = false;
8492 vmx->nested.smm.vmxon = false;
8493 free_vpid(vmx->nested.vpid02);
8494 vmx->nested.posted_intr_nv = -1;
8495 vmx->nested.current_vmptr = -1ull;
8496 if (enable_shadow_vmcs) {
8497 vmx_disable_shadow_vmcs(vmx);
8498 vmcs_clear(vmx->vmcs01.shadow_vmcs);
8499 free_vmcs(vmx->vmcs01.shadow_vmcs);
8500 vmx->vmcs01.shadow_vmcs = NULL;
8501 }
8502 kfree(vmx->nested.cached_vmcs12);
8503 kfree(vmx->nested.cached_shadow_vmcs12);
8504 /* Unpin physical memory we referred to in the vmcs02 */
8505 if (vmx->nested.apic_access_page) {
8506 kvm_release_page_dirty(vmx->nested.apic_access_page);
8507 vmx->nested.apic_access_page = NULL;
8508 }
8509 if (vmx->nested.virtual_apic_page) {
8510 kvm_release_page_dirty(vmx->nested.virtual_apic_page);
8511 vmx->nested.virtual_apic_page = NULL;
8512 }
8513 if (vmx->nested.pi_desc_page) {
8514 kunmap(vmx->nested.pi_desc_page);
8515 kvm_release_page_dirty(vmx->nested.pi_desc_page);
8516 vmx->nested.pi_desc_page = NULL;
8517 vmx->nested.pi_desc = NULL;
8518 }
8519
8520 free_loaded_vmcs(&vmx->nested.vmcs02);
8521 }
8522
8523 /* Emulate the VMXOFF instruction */
handle_vmoff(struct kvm_vcpu * vcpu)8524 static int handle_vmoff(struct kvm_vcpu *vcpu)
8525 {
8526 if (!nested_vmx_check_permission(vcpu))
8527 return 1;
8528 free_nested(to_vmx(vcpu));
8529 nested_vmx_succeed(vcpu);
8530 return kvm_skip_emulated_instruction(vcpu);
8531 }
8532
8533 /* Emulate the VMCLEAR instruction */
handle_vmclear(struct kvm_vcpu * vcpu)8534 static int handle_vmclear(struct kvm_vcpu *vcpu)
8535 {
8536 struct vcpu_vmx *vmx = to_vmx(vcpu);
8537 u32 zero = 0;
8538 gpa_t vmptr;
8539
8540 if (!nested_vmx_check_permission(vcpu))
8541 return 1;
8542
8543 if (nested_vmx_get_vmptr(vcpu, &vmptr))
8544 return 1;
8545
8546 if (!PAGE_ALIGNED(vmptr) || (vmptr >> cpuid_maxphyaddr(vcpu))) {
8547 nested_vmx_failValid(vcpu, VMXERR_VMCLEAR_INVALID_ADDRESS);
8548 return kvm_skip_emulated_instruction(vcpu);
8549 }
8550
8551 if (vmptr == vmx->nested.vmxon_ptr) {
8552 nested_vmx_failValid(vcpu, VMXERR_VMCLEAR_VMXON_POINTER);
8553 return kvm_skip_emulated_instruction(vcpu);
8554 }
8555
8556 if (vmptr == vmx->nested.current_vmptr)
8557 nested_release_vmcs12(vmx);
8558
8559 kvm_vcpu_write_guest(vcpu,
8560 vmptr + offsetof(struct vmcs12, launch_state),
8561 &zero, sizeof(zero));
8562
8563 nested_vmx_succeed(vcpu);
8564 return kvm_skip_emulated_instruction(vcpu);
8565 }
8566
8567 static int nested_vmx_run(struct kvm_vcpu *vcpu, bool launch);
8568
8569 /* Emulate the VMLAUNCH instruction */
handle_vmlaunch(struct kvm_vcpu * vcpu)8570 static int handle_vmlaunch(struct kvm_vcpu *vcpu)
8571 {
8572 return nested_vmx_run(vcpu, true);
8573 }
8574
8575 /* Emulate the VMRESUME instruction */
handle_vmresume(struct kvm_vcpu * vcpu)8576 static int handle_vmresume(struct kvm_vcpu *vcpu)
8577 {
8578
8579 return nested_vmx_run(vcpu, false);
8580 }
8581
8582 /*
8583 * Read a vmcs12 field. Since these can have varying lengths and we return
8584 * one type, we chose the biggest type (u64) and zero-extend the return value
8585 * to that size. Note that the caller, handle_vmread, might need to use only
8586 * some of the bits we return here (e.g., on 32-bit guests, only 32 bits of
8587 * 64-bit fields are to be returned).
8588 */
vmcs12_read_any(struct vmcs12 * vmcs12,unsigned long field,u64 * ret)8589 static inline int vmcs12_read_any(struct vmcs12 *vmcs12,
8590 unsigned long field, u64 *ret)
8591 {
8592 short offset = vmcs_field_to_offset(field);
8593 char *p;
8594
8595 if (offset < 0)
8596 return offset;
8597
8598 p = (char *)vmcs12 + offset;
8599
8600 switch (vmcs_field_width(field)) {
8601 case VMCS_FIELD_WIDTH_NATURAL_WIDTH:
8602 *ret = *((natural_width *)p);
8603 return 0;
8604 case VMCS_FIELD_WIDTH_U16:
8605 *ret = *((u16 *)p);
8606 return 0;
8607 case VMCS_FIELD_WIDTH_U32:
8608 *ret = *((u32 *)p);
8609 return 0;
8610 case VMCS_FIELD_WIDTH_U64:
8611 *ret = *((u64 *)p);
8612 return 0;
8613 default:
8614 WARN_ON(1);
8615 return -ENOENT;
8616 }
8617 }
8618
8619
vmcs12_write_any(struct vmcs12 * vmcs12,unsigned long field,u64 field_value)8620 static inline int vmcs12_write_any(struct vmcs12 *vmcs12,
8621 unsigned long field, u64 field_value){
8622 short offset = vmcs_field_to_offset(field);
8623 char *p = (char *)vmcs12 + offset;
8624 if (offset < 0)
8625 return offset;
8626
8627 switch (vmcs_field_width(field)) {
8628 case VMCS_FIELD_WIDTH_U16:
8629 *(u16 *)p = field_value;
8630 return 0;
8631 case VMCS_FIELD_WIDTH_U32:
8632 *(u32 *)p = field_value;
8633 return 0;
8634 case VMCS_FIELD_WIDTH_U64:
8635 *(u64 *)p = field_value;
8636 return 0;
8637 case VMCS_FIELD_WIDTH_NATURAL_WIDTH:
8638 *(natural_width *)p = field_value;
8639 return 0;
8640 default:
8641 WARN_ON(1);
8642 return -ENOENT;
8643 }
8644
8645 }
8646
8647 /*
8648 * Copy the writable VMCS shadow fields back to the VMCS12, in case
8649 * they have been modified by the L1 guest. Note that the "read-only"
8650 * VM-exit information fields are actually writable if the vCPU is
8651 * configured to support "VMWRITE to any supported field in the VMCS."
8652 */
copy_shadow_to_vmcs12(struct vcpu_vmx * vmx)8653 static void copy_shadow_to_vmcs12(struct vcpu_vmx *vmx)
8654 {
8655 const u16 *fields[] = {
8656 shadow_read_write_fields,
8657 shadow_read_only_fields
8658 };
8659 const int max_fields[] = {
8660 max_shadow_read_write_fields,
8661 max_shadow_read_only_fields
8662 };
8663 int i, q;
8664 unsigned long field;
8665 u64 field_value;
8666 struct vmcs *shadow_vmcs = vmx->vmcs01.shadow_vmcs;
8667
8668 if (WARN_ON(!shadow_vmcs))
8669 return;
8670
8671 preempt_disable();
8672
8673 vmcs_load(shadow_vmcs);
8674
8675 for (q = 0; q < ARRAY_SIZE(fields); q++) {
8676 for (i = 0; i < max_fields[q]; i++) {
8677 field = fields[q][i];
8678 field_value = __vmcs_readl(field);
8679 vmcs12_write_any(get_vmcs12(&vmx->vcpu), field, field_value);
8680 }
8681 /*
8682 * Skip the VM-exit information fields if they are read-only.
8683 */
8684 if (!nested_cpu_has_vmwrite_any_field(&vmx->vcpu))
8685 break;
8686 }
8687
8688 vmcs_clear(shadow_vmcs);
8689 vmcs_load(vmx->loaded_vmcs->vmcs);
8690
8691 preempt_enable();
8692 }
8693
copy_vmcs12_to_shadow(struct vcpu_vmx * vmx)8694 static void copy_vmcs12_to_shadow(struct vcpu_vmx *vmx)
8695 {
8696 const u16 *fields[] = {
8697 shadow_read_write_fields,
8698 shadow_read_only_fields
8699 };
8700 const int max_fields[] = {
8701 max_shadow_read_write_fields,
8702 max_shadow_read_only_fields
8703 };
8704 int i, q;
8705 unsigned long field;
8706 u64 field_value = 0;
8707 struct vmcs *shadow_vmcs = vmx->vmcs01.shadow_vmcs;
8708
8709 if (WARN_ON(!shadow_vmcs))
8710 return;
8711
8712 vmcs_load(shadow_vmcs);
8713
8714 for (q = 0; q < ARRAY_SIZE(fields); q++) {
8715 for (i = 0; i < max_fields[q]; i++) {
8716 field = fields[q][i];
8717 vmcs12_read_any(get_vmcs12(&vmx->vcpu), field, &field_value);
8718 __vmcs_writel(field, field_value);
8719 }
8720 }
8721
8722 vmcs_clear(shadow_vmcs);
8723 vmcs_load(vmx->loaded_vmcs->vmcs);
8724 }
8725
8726 /*
8727 * VMX instructions which assume a current vmcs12 (i.e., that VMPTRLD was
8728 * used before) all generate the same failure when it is missing.
8729 */
nested_vmx_check_vmcs12(struct kvm_vcpu * vcpu)8730 static int nested_vmx_check_vmcs12(struct kvm_vcpu *vcpu)
8731 {
8732 struct vcpu_vmx *vmx = to_vmx(vcpu);
8733 if (vmx->nested.current_vmptr == -1ull) {
8734 nested_vmx_failInvalid(vcpu);
8735 return 0;
8736 }
8737 return 1;
8738 }
8739
handle_vmread(struct kvm_vcpu * vcpu)8740 static int handle_vmread(struct kvm_vcpu *vcpu)
8741 {
8742 unsigned long field;
8743 u64 field_value;
8744 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
8745 u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
8746 gva_t gva = 0;
8747 struct vmcs12 *vmcs12;
8748 struct x86_exception e;
8749
8750 if (!nested_vmx_check_permission(vcpu))
8751 return 1;
8752
8753 if (!nested_vmx_check_vmcs12(vcpu))
8754 return kvm_skip_emulated_instruction(vcpu);
8755
8756 if (!is_guest_mode(vcpu))
8757 vmcs12 = get_vmcs12(vcpu);
8758 else {
8759 /*
8760 * When vmcs->vmcs_link_pointer is -1ull, any VMREAD
8761 * to shadowed-field sets the ALU flags for VMfailInvalid.
8762 */
8763 if (get_vmcs12(vcpu)->vmcs_link_pointer == -1ull) {
8764 nested_vmx_failInvalid(vcpu);
8765 return kvm_skip_emulated_instruction(vcpu);
8766 }
8767 vmcs12 = get_shadow_vmcs12(vcpu);
8768 }
8769
8770 /* Decode instruction info and find the field to read */
8771 field = kvm_register_readl(vcpu, (((vmx_instruction_info) >> 28) & 0xf));
8772 /* Read the field, zero-extended to a u64 field_value */
8773 if (vmcs12_read_any(vmcs12, field, &field_value) < 0) {
8774 nested_vmx_failValid(vcpu, VMXERR_UNSUPPORTED_VMCS_COMPONENT);
8775 return kvm_skip_emulated_instruction(vcpu);
8776 }
8777 /*
8778 * Now copy part of this value to register or memory, as requested.
8779 * Note that the number of bits actually copied is 32 or 64 depending
8780 * on the guest's mode (32 or 64 bit), not on the given field's length.
8781 */
8782 if (vmx_instruction_info & (1u << 10)) {
8783 kvm_register_writel(vcpu, (((vmx_instruction_info) >> 3) & 0xf),
8784 field_value);
8785 } else {
8786 if (get_vmx_mem_address(vcpu, exit_qualification,
8787 vmx_instruction_info, true, &gva))
8788 return 1;
8789 /* _system ok, nested_vmx_check_permission has verified cpl=0 */
8790 if (kvm_write_guest_virt_system(vcpu, gva, &field_value,
8791 (is_long_mode(vcpu) ? 8 : 4),
8792 &e)) {
8793 kvm_inject_page_fault(vcpu, &e);
8794 return 1;
8795 }
8796 }
8797
8798 nested_vmx_succeed(vcpu);
8799 return kvm_skip_emulated_instruction(vcpu);
8800 }
8801
8802
handle_vmwrite(struct kvm_vcpu * vcpu)8803 static int handle_vmwrite(struct kvm_vcpu *vcpu)
8804 {
8805 unsigned long field;
8806 gva_t gva;
8807 struct vcpu_vmx *vmx = to_vmx(vcpu);
8808 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
8809 u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
8810
8811 /* The value to write might be 32 or 64 bits, depending on L1's long
8812 * mode, and eventually we need to write that into a field of several
8813 * possible lengths. The code below first zero-extends the value to 64
8814 * bit (field_value), and then copies only the appropriate number of
8815 * bits into the vmcs12 field.
8816 */
8817 u64 field_value = 0;
8818 struct x86_exception e;
8819 struct vmcs12 *vmcs12;
8820
8821 if (!nested_vmx_check_permission(vcpu))
8822 return 1;
8823
8824 if (!nested_vmx_check_vmcs12(vcpu))
8825 return kvm_skip_emulated_instruction(vcpu);
8826
8827 if (vmx_instruction_info & (1u << 10))
8828 field_value = kvm_register_readl(vcpu,
8829 (((vmx_instruction_info) >> 3) & 0xf));
8830 else {
8831 if (get_vmx_mem_address(vcpu, exit_qualification,
8832 vmx_instruction_info, false, &gva))
8833 return 1;
8834 if (kvm_read_guest_virt(vcpu, gva, &field_value,
8835 (is_64_bit_mode(vcpu) ? 8 : 4), &e)) {
8836 kvm_inject_page_fault(vcpu, &e);
8837 return 1;
8838 }
8839 }
8840
8841
8842 field = kvm_register_readl(vcpu, (((vmx_instruction_info) >> 28) & 0xf));
8843 /*
8844 * If the vCPU supports "VMWRITE to any supported field in the
8845 * VMCS," then the "read-only" fields are actually read/write.
8846 */
8847 if (vmcs_field_readonly(field) &&
8848 !nested_cpu_has_vmwrite_any_field(vcpu)) {
8849 nested_vmx_failValid(vcpu,
8850 VMXERR_VMWRITE_READ_ONLY_VMCS_COMPONENT);
8851 return kvm_skip_emulated_instruction(vcpu);
8852 }
8853
8854 if (!is_guest_mode(vcpu))
8855 vmcs12 = get_vmcs12(vcpu);
8856 else {
8857 /*
8858 * When vmcs->vmcs_link_pointer is -1ull, any VMWRITE
8859 * to shadowed-field sets the ALU flags for VMfailInvalid.
8860 */
8861 if (get_vmcs12(vcpu)->vmcs_link_pointer == -1ull) {
8862 nested_vmx_failInvalid(vcpu);
8863 return kvm_skip_emulated_instruction(vcpu);
8864 }
8865 vmcs12 = get_shadow_vmcs12(vcpu);
8866
8867 }
8868
8869 if (vmcs12_write_any(vmcs12, field, field_value) < 0) {
8870 nested_vmx_failValid(vcpu, VMXERR_UNSUPPORTED_VMCS_COMPONENT);
8871 return kvm_skip_emulated_instruction(vcpu);
8872 }
8873
8874 /*
8875 * Do not track vmcs12 dirty-state if in guest-mode
8876 * as we actually dirty shadow vmcs12 instead of vmcs12.
8877 */
8878 if (!is_guest_mode(vcpu)) {
8879 switch (field) {
8880 #define SHADOW_FIELD_RW(x) case x:
8881 #include "vmx_shadow_fields.h"
8882 /*
8883 * The fields that can be updated by L1 without a vmexit are
8884 * always updated in the vmcs02, the others go down the slow
8885 * path of prepare_vmcs02.
8886 */
8887 break;
8888 default:
8889 vmx->nested.dirty_vmcs12 = true;
8890 break;
8891 }
8892 }
8893
8894 nested_vmx_succeed(vcpu);
8895 return kvm_skip_emulated_instruction(vcpu);
8896 }
8897
set_current_vmptr(struct vcpu_vmx * vmx,gpa_t vmptr)8898 static void set_current_vmptr(struct vcpu_vmx *vmx, gpa_t vmptr)
8899 {
8900 vmx->nested.current_vmptr = vmptr;
8901 if (enable_shadow_vmcs) {
8902 vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL,
8903 SECONDARY_EXEC_SHADOW_VMCS);
8904 vmcs_write64(VMCS_LINK_POINTER,
8905 __pa(vmx->vmcs01.shadow_vmcs));
8906 vmx->nested.sync_shadow_vmcs = true;
8907 }
8908 vmx->nested.dirty_vmcs12 = true;
8909 }
8910
8911 /* Emulate the VMPTRLD instruction */
handle_vmptrld(struct kvm_vcpu * vcpu)8912 static int handle_vmptrld(struct kvm_vcpu *vcpu)
8913 {
8914 struct vcpu_vmx *vmx = to_vmx(vcpu);
8915 gpa_t vmptr;
8916
8917 if (!nested_vmx_check_permission(vcpu))
8918 return 1;
8919
8920 if (nested_vmx_get_vmptr(vcpu, &vmptr))
8921 return 1;
8922
8923 if (!PAGE_ALIGNED(vmptr) || (vmptr >> cpuid_maxphyaddr(vcpu))) {
8924 nested_vmx_failValid(vcpu, VMXERR_VMPTRLD_INVALID_ADDRESS);
8925 return kvm_skip_emulated_instruction(vcpu);
8926 }
8927
8928 if (vmptr == vmx->nested.vmxon_ptr) {
8929 nested_vmx_failValid(vcpu, VMXERR_VMPTRLD_VMXON_POINTER);
8930 return kvm_skip_emulated_instruction(vcpu);
8931 }
8932
8933 if (vmx->nested.current_vmptr != vmptr) {
8934 struct vmcs12 *new_vmcs12;
8935 struct page *page;
8936 page = kvm_vcpu_gpa_to_page(vcpu, vmptr);
8937 if (is_error_page(page)) {
8938 nested_vmx_failInvalid(vcpu);
8939 return kvm_skip_emulated_instruction(vcpu);
8940 }
8941 new_vmcs12 = kmap(page);
8942 if (new_vmcs12->hdr.revision_id != VMCS12_REVISION ||
8943 (new_vmcs12->hdr.shadow_vmcs &&
8944 !nested_cpu_has_vmx_shadow_vmcs(vcpu))) {
8945 kunmap(page);
8946 kvm_release_page_clean(page);
8947 nested_vmx_failValid(vcpu,
8948 VMXERR_VMPTRLD_INCORRECT_VMCS_REVISION_ID);
8949 return kvm_skip_emulated_instruction(vcpu);
8950 }
8951
8952 nested_release_vmcs12(vmx);
8953 /*
8954 * Load VMCS12 from guest memory since it is not already
8955 * cached.
8956 */
8957 memcpy(vmx->nested.cached_vmcs12, new_vmcs12, VMCS12_SIZE);
8958 kunmap(page);
8959 kvm_release_page_clean(page);
8960
8961 set_current_vmptr(vmx, vmptr);
8962 }
8963
8964 nested_vmx_succeed(vcpu);
8965 return kvm_skip_emulated_instruction(vcpu);
8966 }
8967
8968 /* Emulate the VMPTRST instruction */
handle_vmptrst(struct kvm_vcpu * vcpu)8969 static int handle_vmptrst(struct kvm_vcpu *vcpu)
8970 {
8971 unsigned long exit_qual = vmcs_readl(EXIT_QUALIFICATION);
8972 u32 instr_info = vmcs_read32(VMX_INSTRUCTION_INFO);
8973 gpa_t current_vmptr = to_vmx(vcpu)->nested.current_vmptr;
8974 struct x86_exception e;
8975 gva_t gva;
8976
8977 if (!nested_vmx_check_permission(vcpu))
8978 return 1;
8979
8980 if (get_vmx_mem_address(vcpu, exit_qual, instr_info, true, &gva))
8981 return 1;
8982 /* *_system ok, nested_vmx_check_permission has verified cpl=0 */
8983 if (kvm_write_guest_virt_system(vcpu, gva, (void *)¤t_vmptr,
8984 sizeof(gpa_t), &e)) {
8985 kvm_inject_page_fault(vcpu, &e);
8986 return 1;
8987 }
8988 nested_vmx_succeed(vcpu);
8989 return kvm_skip_emulated_instruction(vcpu);
8990 }
8991
8992 /* Emulate the INVEPT instruction */
handle_invept(struct kvm_vcpu * vcpu)8993 static int handle_invept(struct kvm_vcpu *vcpu)
8994 {
8995 struct vcpu_vmx *vmx = to_vmx(vcpu);
8996 u32 vmx_instruction_info, types;
8997 unsigned long type;
8998 gva_t gva;
8999 struct x86_exception e;
9000 struct {
9001 u64 eptp, gpa;
9002 } operand;
9003
9004 if (!(vmx->nested.msrs.secondary_ctls_high &
9005 SECONDARY_EXEC_ENABLE_EPT) ||
9006 !(vmx->nested.msrs.ept_caps & VMX_EPT_INVEPT_BIT)) {
9007 kvm_queue_exception(vcpu, UD_VECTOR);
9008 return 1;
9009 }
9010
9011 if (!nested_vmx_check_permission(vcpu))
9012 return 1;
9013
9014 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
9015 type = kvm_register_readl(vcpu, (vmx_instruction_info >> 28) & 0xf);
9016
9017 types = (vmx->nested.msrs.ept_caps >> VMX_EPT_EXTENT_SHIFT) & 6;
9018
9019 if (type >= 32 || !(types & (1 << type))) {
9020 nested_vmx_failValid(vcpu,
9021 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
9022 return kvm_skip_emulated_instruction(vcpu);
9023 }
9024
9025 /* According to the Intel VMX instruction reference, the memory
9026 * operand is read even if it isn't needed (e.g., for type==global)
9027 */
9028 if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
9029 vmx_instruction_info, false, &gva))
9030 return 1;
9031 if (kvm_read_guest_virt(vcpu, gva, &operand, sizeof(operand), &e)) {
9032 kvm_inject_page_fault(vcpu, &e);
9033 return 1;
9034 }
9035
9036 switch (type) {
9037 case VMX_EPT_EXTENT_GLOBAL:
9038 /*
9039 * TODO: track mappings and invalidate
9040 * single context requests appropriately
9041 */
9042 case VMX_EPT_EXTENT_CONTEXT:
9043 kvm_mmu_sync_roots(vcpu);
9044 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
9045 nested_vmx_succeed(vcpu);
9046 break;
9047 default:
9048 BUG_ON(1);
9049 break;
9050 }
9051
9052 return kvm_skip_emulated_instruction(vcpu);
9053 }
9054
handle_invvpid(struct kvm_vcpu * vcpu)9055 static int handle_invvpid(struct kvm_vcpu *vcpu)
9056 {
9057 struct vcpu_vmx *vmx = to_vmx(vcpu);
9058 u32 vmx_instruction_info;
9059 unsigned long type, types;
9060 gva_t gva;
9061 struct x86_exception e;
9062 struct {
9063 u64 vpid;
9064 u64 gla;
9065 } operand;
9066
9067 if (!(vmx->nested.msrs.secondary_ctls_high &
9068 SECONDARY_EXEC_ENABLE_VPID) ||
9069 !(vmx->nested.msrs.vpid_caps & VMX_VPID_INVVPID_BIT)) {
9070 kvm_queue_exception(vcpu, UD_VECTOR);
9071 return 1;
9072 }
9073
9074 if (!nested_vmx_check_permission(vcpu))
9075 return 1;
9076
9077 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
9078 type = kvm_register_readl(vcpu, (vmx_instruction_info >> 28) & 0xf);
9079
9080 types = (vmx->nested.msrs.vpid_caps &
9081 VMX_VPID_EXTENT_SUPPORTED_MASK) >> 8;
9082
9083 if (type >= 32 || !(types & (1 << type))) {
9084 nested_vmx_failValid(vcpu,
9085 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
9086 return kvm_skip_emulated_instruction(vcpu);
9087 }
9088
9089 /* according to the intel vmx instruction reference, the memory
9090 * operand is read even if it isn't needed (e.g., for type==global)
9091 */
9092 if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
9093 vmx_instruction_info, false, &gva))
9094 return 1;
9095 if (kvm_read_guest_virt(vcpu, gva, &operand, sizeof(operand), &e)) {
9096 kvm_inject_page_fault(vcpu, &e);
9097 return 1;
9098 }
9099 if (operand.vpid >> 16) {
9100 nested_vmx_failValid(vcpu,
9101 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
9102 return kvm_skip_emulated_instruction(vcpu);
9103 }
9104
9105 switch (type) {
9106 case VMX_VPID_EXTENT_INDIVIDUAL_ADDR:
9107 if (!operand.vpid ||
9108 is_noncanonical_address(operand.gla, vcpu)) {
9109 nested_vmx_failValid(vcpu,
9110 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
9111 return kvm_skip_emulated_instruction(vcpu);
9112 }
9113 if (cpu_has_vmx_invvpid_individual_addr() &&
9114 vmx->nested.vpid02) {
9115 __invvpid(VMX_VPID_EXTENT_INDIVIDUAL_ADDR,
9116 vmx->nested.vpid02, operand.gla);
9117 } else
9118 __vmx_flush_tlb(vcpu, vmx->nested.vpid02, true);
9119 break;
9120 case VMX_VPID_EXTENT_SINGLE_CONTEXT:
9121 case VMX_VPID_EXTENT_SINGLE_NON_GLOBAL:
9122 if (!operand.vpid) {
9123 nested_vmx_failValid(vcpu,
9124 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
9125 return kvm_skip_emulated_instruction(vcpu);
9126 }
9127 __vmx_flush_tlb(vcpu, vmx->nested.vpid02, true);
9128 break;
9129 case VMX_VPID_EXTENT_ALL_CONTEXT:
9130 __vmx_flush_tlb(vcpu, vmx->nested.vpid02, true);
9131 break;
9132 default:
9133 WARN_ON_ONCE(1);
9134 return kvm_skip_emulated_instruction(vcpu);
9135 }
9136
9137 nested_vmx_succeed(vcpu);
9138
9139 return kvm_skip_emulated_instruction(vcpu);
9140 }
9141
handle_invpcid(struct kvm_vcpu * vcpu)9142 static int handle_invpcid(struct kvm_vcpu *vcpu)
9143 {
9144 u32 vmx_instruction_info;
9145 unsigned long type;
9146 bool pcid_enabled;
9147 gva_t gva;
9148 struct x86_exception e;
9149 unsigned i;
9150 unsigned long roots_to_free = 0;
9151 struct {
9152 u64 pcid;
9153 u64 gla;
9154 } operand;
9155
9156 if (!guest_cpuid_has(vcpu, X86_FEATURE_INVPCID)) {
9157 kvm_queue_exception(vcpu, UD_VECTOR);
9158 return 1;
9159 }
9160
9161 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
9162 type = kvm_register_readl(vcpu, (vmx_instruction_info >> 28) & 0xf);
9163
9164 if (type > 3) {
9165 kvm_inject_gp(vcpu, 0);
9166 return 1;
9167 }
9168
9169 /* According to the Intel instruction reference, the memory operand
9170 * is read even if it isn't needed (e.g., for type==all)
9171 */
9172 if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
9173 vmx_instruction_info, false, &gva))
9174 return 1;
9175
9176 if (kvm_read_guest_virt(vcpu, gva, &operand, sizeof(operand), &e)) {
9177 kvm_inject_page_fault(vcpu, &e);
9178 return 1;
9179 }
9180
9181 if (operand.pcid >> 12 != 0) {
9182 kvm_inject_gp(vcpu, 0);
9183 return 1;
9184 }
9185
9186 pcid_enabled = kvm_read_cr4_bits(vcpu, X86_CR4_PCIDE);
9187
9188 switch (type) {
9189 case INVPCID_TYPE_INDIV_ADDR:
9190 if ((!pcid_enabled && (operand.pcid != 0)) ||
9191 is_noncanonical_address(operand.gla, vcpu)) {
9192 kvm_inject_gp(vcpu, 0);
9193 return 1;
9194 }
9195 kvm_mmu_invpcid_gva(vcpu, operand.gla, operand.pcid);
9196 return kvm_skip_emulated_instruction(vcpu);
9197
9198 case INVPCID_TYPE_SINGLE_CTXT:
9199 if (!pcid_enabled && (operand.pcid != 0)) {
9200 kvm_inject_gp(vcpu, 0);
9201 return 1;
9202 }
9203
9204 if (kvm_get_active_pcid(vcpu) == operand.pcid) {
9205 kvm_mmu_sync_roots(vcpu);
9206 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
9207 }
9208
9209 for (i = 0; i < KVM_MMU_NUM_PREV_ROOTS; i++)
9210 if (kvm_get_pcid(vcpu, vcpu->arch.mmu.prev_roots[i].cr3)
9211 == operand.pcid)
9212 roots_to_free |= KVM_MMU_ROOT_PREVIOUS(i);
9213
9214 kvm_mmu_free_roots(vcpu, roots_to_free);
9215 /*
9216 * If neither the current cr3 nor any of the prev_roots use the
9217 * given PCID, then nothing needs to be done here because a
9218 * resync will happen anyway before switching to any other CR3.
9219 */
9220
9221 return kvm_skip_emulated_instruction(vcpu);
9222
9223 case INVPCID_TYPE_ALL_NON_GLOBAL:
9224 /*
9225 * Currently, KVM doesn't mark global entries in the shadow
9226 * page tables, so a non-global flush just degenerates to a
9227 * global flush. If needed, we could optimize this later by
9228 * keeping track of global entries in shadow page tables.
9229 */
9230
9231 /* fall-through */
9232 case INVPCID_TYPE_ALL_INCL_GLOBAL:
9233 kvm_mmu_unload(vcpu);
9234 return kvm_skip_emulated_instruction(vcpu);
9235
9236 default:
9237 BUG(); /* We have already checked above that type <= 3 */
9238 }
9239 }
9240
handle_pml_full(struct kvm_vcpu * vcpu)9241 static int handle_pml_full(struct kvm_vcpu *vcpu)
9242 {
9243 unsigned long exit_qualification;
9244
9245 trace_kvm_pml_full(vcpu->vcpu_id);
9246
9247 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
9248
9249 /*
9250 * PML buffer FULL happened while executing iret from NMI,
9251 * "blocked by NMI" bit has to be set before next VM entry.
9252 */
9253 if (!(to_vmx(vcpu)->idt_vectoring_info & VECTORING_INFO_VALID_MASK) &&
9254 enable_vnmi &&
9255 (exit_qualification & INTR_INFO_UNBLOCK_NMI))
9256 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
9257 GUEST_INTR_STATE_NMI);
9258
9259 /*
9260 * PML buffer already flushed at beginning of VMEXIT. Nothing to do
9261 * here.., and there's no userspace involvement needed for PML.
9262 */
9263 return 1;
9264 }
9265
handle_preemption_timer(struct kvm_vcpu * vcpu)9266 static int handle_preemption_timer(struct kvm_vcpu *vcpu)
9267 {
9268 if (!to_vmx(vcpu)->req_immediate_exit)
9269 kvm_lapic_expired_hv_timer(vcpu);
9270 return 1;
9271 }
9272
valid_ept_address(struct kvm_vcpu * vcpu,u64 address)9273 static bool valid_ept_address(struct kvm_vcpu *vcpu, u64 address)
9274 {
9275 struct vcpu_vmx *vmx = to_vmx(vcpu);
9276 int maxphyaddr = cpuid_maxphyaddr(vcpu);
9277
9278 /* Check for memory type validity */
9279 switch (address & VMX_EPTP_MT_MASK) {
9280 case VMX_EPTP_MT_UC:
9281 if (!(vmx->nested.msrs.ept_caps & VMX_EPTP_UC_BIT))
9282 return false;
9283 break;
9284 case VMX_EPTP_MT_WB:
9285 if (!(vmx->nested.msrs.ept_caps & VMX_EPTP_WB_BIT))
9286 return false;
9287 break;
9288 default:
9289 return false;
9290 }
9291
9292 /* only 4 levels page-walk length are valid */
9293 if ((address & VMX_EPTP_PWL_MASK) != VMX_EPTP_PWL_4)
9294 return false;
9295
9296 /* Reserved bits should not be set */
9297 if (address >> maxphyaddr || ((address >> 7) & 0x1f))
9298 return false;
9299
9300 /* AD, if set, should be supported */
9301 if (address & VMX_EPTP_AD_ENABLE_BIT) {
9302 if (!(vmx->nested.msrs.ept_caps & VMX_EPT_AD_BIT))
9303 return false;
9304 }
9305
9306 return true;
9307 }
9308
nested_vmx_eptp_switching(struct kvm_vcpu * vcpu,struct vmcs12 * vmcs12)9309 static int nested_vmx_eptp_switching(struct kvm_vcpu *vcpu,
9310 struct vmcs12 *vmcs12)
9311 {
9312 u32 index = vcpu->arch.regs[VCPU_REGS_RCX];
9313 u64 address;
9314 bool accessed_dirty;
9315 struct kvm_mmu *mmu = vcpu->arch.walk_mmu;
9316
9317 if (!nested_cpu_has_eptp_switching(vmcs12) ||
9318 !nested_cpu_has_ept(vmcs12))
9319 return 1;
9320
9321 if (index >= VMFUNC_EPTP_ENTRIES)
9322 return 1;
9323
9324
9325 if (kvm_vcpu_read_guest_page(vcpu, vmcs12->eptp_list_address >> PAGE_SHIFT,
9326 &address, index * 8, 8))
9327 return 1;
9328
9329 accessed_dirty = !!(address & VMX_EPTP_AD_ENABLE_BIT);
9330
9331 /*
9332 * If the (L2) guest does a vmfunc to the currently
9333 * active ept pointer, we don't have to do anything else
9334 */
9335 if (vmcs12->ept_pointer != address) {
9336 if (!valid_ept_address(vcpu, address))
9337 return 1;
9338
9339 kvm_mmu_unload(vcpu);
9340 mmu->ept_ad = accessed_dirty;
9341 mmu->base_role.ad_disabled = !accessed_dirty;
9342 vmcs12->ept_pointer = address;
9343 /*
9344 * TODO: Check what's the correct approach in case
9345 * mmu reload fails. Currently, we just let the next
9346 * reload potentially fail
9347 */
9348 kvm_mmu_reload(vcpu);
9349 }
9350
9351 return 0;
9352 }
9353
handle_vmfunc(struct kvm_vcpu * vcpu)9354 static int handle_vmfunc(struct kvm_vcpu *vcpu)
9355 {
9356 struct vcpu_vmx *vmx = to_vmx(vcpu);
9357 struct vmcs12 *vmcs12;
9358 u32 function = vcpu->arch.regs[VCPU_REGS_RAX];
9359
9360 /*
9361 * VMFUNC is only supported for nested guests, but we always enable the
9362 * secondary control for simplicity; for non-nested mode, fake that we
9363 * didn't by injecting #UD.
9364 */
9365 if (!is_guest_mode(vcpu)) {
9366 kvm_queue_exception(vcpu, UD_VECTOR);
9367 return 1;
9368 }
9369
9370 vmcs12 = get_vmcs12(vcpu);
9371 if ((vmcs12->vm_function_control & (1 << function)) == 0)
9372 goto fail;
9373
9374 switch (function) {
9375 case 0:
9376 if (nested_vmx_eptp_switching(vcpu, vmcs12))
9377 goto fail;
9378 break;
9379 default:
9380 goto fail;
9381 }
9382 return kvm_skip_emulated_instruction(vcpu);
9383
9384 fail:
9385 nested_vmx_vmexit(vcpu, vmx->exit_reason,
9386 vmcs_read32(VM_EXIT_INTR_INFO),
9387 vmcs_readl(EXIT_QUALIFICATION));
9388 return 1;
9389 }
9390
handle_encls(struct kvm_vcpu * vcpu)9391 static int handle_encls(struct kvm_vcpu *vcpu)
9392 {
9393 /*
9394 * SGX virtualization is not yet supported. There is no software
9395 * enable bit for SGX, so we have to trap ENCLS and inject a #UD
9396 * to prevent the guest from executing ENCLS.
9397 */
9398 kvm_queue_exception(vcpu, UD_VECTOR);
9399 return 1;
9400 }
9401
9402 /*
9403 * The exit handlers return 1 if the exit was handled fully and guest execution
9404 * may resume. Otherwise they set the kvm_run parameter to indicate what needs
9405 * to be done to userspace and return 0.
9406 */
9407 static int (*const kvm_vmx_exit_handlers[])(struct kvm_vcpu *vcpu) = {
9408 [EXIT_REASON_EXCEPTION_NMI] = handle_exception,
9409 [EXIT_REASON_EXTERNAL_INTERRUPT] = handle_external_interrupt,
9410 [EXIT_REASON_TRIPLE_FAULT] = handle_triple_fault,
9411 [EXIT_REASON_NMI_WINDOW] = handle_nmi_window,
9412 [EXIT_REASON_IO_INSTRUCTION] = handle_io,
9413 [EXIT_REASON_CR_ACCESS] = handle_cr,
9414 [EXIT_REASON_DR_ACCESS] = handle_dr,
9415 [EXIT_REASON_CPUID] = handle_cpuid,
9416 [EXIT_REASON_MSR_READ] = handle_rdmsr,
9417 [EXIT_REASON_MSR_WRITE] = handle_wrmsr,
9418 [EXIT_REASON_PENDING_INTERRUPT] = handle_interrupt_window,
9419 [EXIT_REASON_HLT] = handle_halt,
9420 [EXIT_REASON_INVD] = handle_invd,
9421 [EXIT_REASON_INVLPG] = handle_invlpg,
9422 [EXIT_REASON_RDPMC] = handle_rdpmc,
9423 [EXIT_REASON_VMCALL] = handle_vmcall,
9424 [EXIT_REASON_VMCLEAR] = handle_vmclear,
9425 [EXIT_REASON_VMLAUNCH] = handle_vmlaunch,
9426 [EXIT_REASON_VMPTRLD] = handle_vmptrld,
9427 [EXIT_REASON_VMPTRST] = handle_vmptrst,
9428 [EXIT_REASON_VMREAD] = handle_vmread,
9429 [EXIT_REASON_VMRESUME] = handle_vmresume,
9430 [EXIT_REASON_VMWRITE] = handle_vmwrite,
9431 [EXIT_REASON_VMOFF] = handle_vmoff,
9432 [EXIT_REASON_VMON] = handle_vmon,
9433 [EXIT_REASON_TPR_BELOW_THRESHOLD] = handle_tpr_below_threshold,
9434 [EXIT_REASON_APIC_ACCESS] = handle_apic_access,
9435 [EXIT_REASON_APIC_WRITE] = handle_apic_write,
9436 [EXIT_REASON_EOI_INDUCED] = handle_apic_eoi_induced,
9437 [EXIT_REASON_WBINVD] = handle_wbinvd,
9438 [EXIT_REASON_XSETBV] = handle_xsetbv,
9439 [EXIT_REASON_TASK_SWITCH] = handle_task_switch,
9440 [EXIT_REASON_MCE_DURING_VMENTRY] = handle_machine_check,
9441 [EXIT_REASON_GDTR_IDTR] = handle_desc,
9442 [EXIT_REASON_LDTR_TR] = handle_desc,
9443 [EXIT_REASON_EPT_VIOLATION] = handle_ept_violation,
9444 [EXIT_REASON_EPT_MISCONFIG] = handle_ept_misconfig,
9445 [EXIT_REASON_PAUSE_INSTRUCTION] = handle_pause,
9446 [EXIT_REASON_MWAIT_INSTRUCTION] = handle_mwait,
9447 [EXIT_REASON_MONITOR_TRAP_FLAG] = handle_monitor_trap,
9448 [EXIT_REASON_MONITOR_INSTRUCTION] = handle_monitor,
9449 [EXIT_REASON_INVEPT] = handle_invept,
9450 [EXIT_REASON_INVVPID] = handle_invvpid,
9451 [EXIT_REASON_RDRAND] = handle_invalid_op,
9452 [EXIT_REASON_RDSEED] = handle_invalid_op,
9453 [EXIT_REASON_XSAVES] = handle_xsaves,
9454 [EXIT_REASON_XRSTORS] = handle_xrstors,
9455 [EXIT_REASON_PML_FULL] = handle_pml_full,
9456 [EXIT_REASON_INVPCID] = handle_invpcid,
9457 [EXIT_REASON_VMFUNC] = handle_vmfunc,
9458 [EXIT_REASON_PREEMPTION_TIMER] = handle_preemption_timer,
9459 [EXIT_REASON_ENCLS] = handle_encls,
9460 };
9461
9462 static const int kvm_vmx_max_exit_handlers =
9463 ARRAY_SIZE(kvm_vmx_exit_handlers);
9464
9465 /*
9466 * Return true if an IO instruction with the specified port and size should cause
9467 * a VM-exit into L1.
9468 */
nested_vmx_check_io_bitmaps(struct kvm_vcpu * vcpu,unsigned int port,int size)9469 bool nested_vmx_check_io_bitmaps(struct kvm_vcpu *vcpu, unsigned int port,
9470 int size)
9471 {
9472 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
9473 gpa_t bitmap, last_bitmap;
9474 u8 b;
9475
9476 last_bitmap = (gpa_t)-1;
9477 b = -1;
9478
9479 while (size > 0) {
9480 if (port < 0x8000)
9481 bitmap = vmcs12->io_bitmap_a;
9482 else if (port < 0x10000)
9483 bitmap = vmcs12->io_bitmap_b;
9484 else
9485 return true;
9486 bitmap += (port & 0x7fff) / 8;
9487
9488 if (last_bitmap != bitmap)
9489 if (kvm_vcpu_read_guest(vcpu, bitmap, &b, 1))
9490 return true;
9491 if (b & (1 << (port & 7)))
9492 return true;
9493
9494 port++;
9495 size--;
9496 last_bitmap = bitmap;
9497 }
9498
9499 return false;
9500 }
9501
9502 /*
9503 * Return 1 if we should exit from L2 to L1 to handle an MSR access access,
9504 * rather than handle it ourselves in L0. I.e., check whether L1 expressed
9505 * disinterest in the current event (read or write a specific MSR) by using an
9506 * MSR bitmap. This may be the case even when L0 doesn't use MSR bitmaps.
9507 */
nested_vmx_exit_handled_msr(struct kvm_vcpu * vcpu,struct vmcs12 * vmcs12,u32 exit_reason)9508 static bool nested_vmx_exit_handled_msr(struct kvm_vcpu *vcpu,
9509 struct vmcs12 *vmcs12, u32 exit_reason)
9510 {
9511 u32 msr_index = vcpu->arch.regs[VCPU_REGS_RCX];
9512 gpa_t bitmap;
9513
9514 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS))
9515 return true;
9516
9517 /*
9518 * The MSR_BITMAP page is divided into four 1024-byte bitmaps,
9519 * for the four combinations of read/write and low/high MSR numbers.
9520 * First we need to figure out which of the four to use:
9521 */
9522 bitmap = vmcs12->msr_bitmap;
9523 if (exit_reason == EXIT_REASON_MSR_WRITE)
9524 bitmap += 2048;
9525 if (msr_index >= 0xc0000000) {
9526 msr_index -= 0xc0000000;
9527 bitmap += 1024;
9528 }
9529
9530 /* Then read the msr_index'th bit from this bitmap: */
9531 if (msr_index < 1024*8) {
9532 unsigned char b;
9533 if (kvm_vcpu_read_guest(vcpu, bitmap + msr_index/8, &b, 1))
9534 return true;
9535 return 1 & (b >> (msr_index & 7));
9536 } else
9537 return true; /* let L1 handle the wrong parameter */
9538 }
9539
9540 /*
9541 * Return 1 if we should exit from L2 to L1 to handle a CR access exit,
9542 * rather than handle it ourselves in L0. I.e., check if L1 wanted to
9543 * intercept (via guest_host_mask etc.) the current event.
9544 */
nested_vmx_exit_handled_cr(struct kvm_vcpu * vcpu,struct vmcs12 * vmcs12)9545 static bool nested_vmx_exit_handled_cr(struct kvm_vcpu *vcpu,
9546 struct vmcs12 *vmcs12)
9547 {
9548 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
9549 int cr = exit_qualification & 15;
9550 int reg;
9551 unsigned long val;
9552
9553 switch ((exit_qualification >> 4) & 3) {
9554 case 0: /* mov to cr */
9555 reg = (exit_qualification >> 8) & 15;
9556 val = kvm_register_readl(vcpu, reg);
9557 switch (cr) {
9558 case 0:
9559 if (vmcs12->cr0_guest_host_mask &
9560 (val ^ vmcs12->cr0_read_shadow))
9561 return true;
9562 break;
9563 case 3:
9564 if ((vmcs12->cr3_target_count >= 1 &&
9565 vmcs12->cr3_target_value0 == val) ||
9566 (vmcs12->cr3_target_count >= 2 &&
9567 vmcs12->cr3_target_value1 == val) ||
9568 (vmcs12->cr3_target_count >= 3 &&
9569 vmcs12->cr3_target_value2 == val) ||
9570 (vmcs12->cr3_target_count >= 4 &&
9571 vmcs12->cr3_target_value3 == val))
9572 return false;
9573 if (nested_cpu_has(vmcs12, CPU_BASED_CR3_LOAD_EXITING))
9574 return true;
9575 break;
9576 case 4:
9577 if (vmcs12->cr4_guest_host_mask &
9578 (vmcs12->cr4_read_shadow ^ val))
9579 return true;
9580 break;
9581 case 8:
9582 if (nested_cpu_has(vmcs12, CPU_BASED_CR8_LOAD_EXITING))
9583 return true;
9584 break;
9585 }
9586 break;
9587 case 2: /* clts */
9588 if ((vmcs12->cr0_guest_host_mask & X86_CR0_TS) &&
9589 (vmcs12->cr0_read_shadow & X86_CR0_TS))
9590 return true;
9591 break;
9592 case 1: /* mov from cr */
9593 switch (cr) {
9594 case 3:
9595 if (vmcs12->cpu_based_vm_exec_control &
9596 CPU_BASED_CR3_STORE_EXITING)
9597 return true;
9598 break;
9599 case 8:
9600 if (vmcs12->cpu_based_vm_exec_control &
9601 CPU_BASED_CR8_STORE_EXITING)
9602 return true;
9603 break;
9604 }
9605 break;
9606 case 3: /* lmsw */
9607 /*
9608 * lmsw can change bits 1..3 of cr0, and only set bit 0 of
9609 * cr0. Other attempted changes are ignored, with no exit.
9610 */
9611 val = (exit_qualification >> LMSW_SOURCE_DATA_SHIFT) & 0x0f;
9612 if (vmcs12->cr0_guest_host_mask & 0xe &
9613 (val ^ vmcs12->cr0_read_shadow))
9614 return true;
9615 if ((vmcs12->cr0_guest_host_mask & 0x1) &&
9616 !(vmcs12->cr0_read_shadow & 0x1) &&
9617 (val & 0x1))
9618 return true;
9619 break;
9620 }
9621 return false;
9622 }
9623
nested_vmx_exit_handled_vmcs_access(struct kvm_vcpu * vcpu,struct vmcs12 * vmcs12,gpa_t bitmap)9624 static bool nested_vmx_exit_handled_vmcs_access(struct kvm_vcpu *vcpu,
9625 struct vmcs12 *vmcs12, gpa_t bitmap)
9626 {
9627 u32 vmx_instruction_info;
9628 unsigned long field;
9629 u8 b;
9630
9631 if (!nested_cpu_has_shadow_vmcs(vmcs12))
9632 return true;
9633
9634 /* Decode instruction info and find the field to access */
9635 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
9636 field = kvm_register_read(vcpu, (((vmx_instruction_info) >> 28) & 0xf));
9637
9638 /* Out-of-range fields always cause a VM exit from L2 to L1 */
9639 if (field >> 15)
9640 return true;
9641
9642 if (kvm_vcpu_read_guest(vcpu, bitmap + field/8, &b, 1))
9643 return true;
9644
9645 return 1 & (b >> (field & 7));
9646 }
9647
9648 /*
9649 * Return 1 if we should exit from L2 to L1 to handle an exit, or 0 if we
9650 * should handle it ourselves in L0 (and then continue L2). Only call this
9651 * when in is_guest_mode (L2).
9652 */
nested_vmx_exit_reflected(struct kvm_vcpu * vcpu,u32 exit_reason)9653 static bool nested_vmx_exit_reflected(struct kvm_vcpu *vcpu, u32 exit_reason)
9654 {
9655 u32 intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
9656 struct vcpu_vmx *vmx = to_vmx(vcpu);
9657 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
9658
9659 if (vmx->nested.nested_run_pending)
9660 return false;
9661
9662 if (unlikely(vmx->fail)) {
9663 pr_info_ratelimited("%s failed vm entry %x\n", __func__,
9664 vmcs_read32(VM_INSTRUCTION_ERROR));
9665 return true;
9666 }
9667
9668 /*
9669 * The host physical addresses of some pages of guest memory
9670 * are loaded into the vmcs02 (e.g. vmcs12's Virtual APIC
9671 * Page). The CPU may write to these pages via their host
9672 * physical address while L2 is running, bypassing any
9673 * address-translation-based dirty tracking (e.g. EPT write
9674 * protection).
9675 *
9676 * Mark them dirty on every exit from L2 to prevent them from
9677 * getting out of sync with dirty tracking.
9678 */
9679 nested_mark_vmcs12_pages_dirty(vcpu);
9680
9681 trace_kvm_nested_vmexit(kvm_rip_read(vcpu), exit_reason,
9682 vmcs_readl(EXIT_QUALIFICATION),
9683 vmx->idt_vectoring_info,
9684 intr_info,
9685 vmcs_read32(VM_EXIT_INTR_ERROR_CODE),
9686 KVM_ISA_VMX);
9687
9688 switch ((u16)exit_reason) {
9689 case EXIT_REASON_EXCEPTION_NMI:
9690 if (is_nmi(intr_info))
9691 return false;
9692 else if (is_page_fault(intr_info))
9693 return !vmx->vcpu.arch.apf.host_apf_reason && enable_ept;
9694 else if (is_no_device(intr_info) &&
9695 !(vmcs12->guest_cr0 & X86_CR0_TS))
9696 return false;
9697 else if (is_debug(intr_info) &&
9698 vcpu->guest_debug &
9699 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))
9700 return false;
9701 else if (is_breakpoint(intr_info) &&
9702 vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
9703 return false;
9704 return vmcs12->exception_bitmap &
9705 (1u << (intr_info & INTR_INFO_VECTOR_MASK));
9706 case EXIT_REASON_EXTERNAL_INTERRUPT:
9707 return false;
9708 case EXIT_REASON_TRIPLE_FAULT:
9709 return true;
9710 case EXIT_REASON_PENDING_INTERRUPT:
9711 return nested_cpu_has(vmcs12, CPU_BASED_VIRTUAL_INTR_PENDING);
9712 case EXIT_REASON_NMI_WINDOW:
9713 return nested_cpu_has(vmcs12, CPU_BASED_VIRTUAL_NMI_PENDING);
9714 case EXIT_REASON_TASK_SWITCH:
9715 return true;
9716 case EXIT_REASON_CPUID:
9717 return true;
9718 case EXIT_REASON_HLT:
9719 return nested_cpu_has(vmcs12, CPU_BASED_HLT_EXITING);
9720 case EXIT_REASON_INVD:
9721 return true;
9722 case EXIT_REASON_INVLPG:
9723 return nested_cpu_has(vmcs12, CPU_BASED_INVLPG_EXITING);
9724 case EXIT_REASON_RDPMC:
9725 return nested_cpu_has(vmcs12, CPU_BASED_RDPMC_EXITING);
9726 case EXIT_REASON_RDRAND:
9727 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_RDRAND_EXITING);
9728 case EXIT_REASON_RDSEED:
9729 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_RDSEED_EXITING);
9730 case EXIT_REASON_RDTSC: case EXIT_REASON_RDTSCP:
9731 return nested_cpu_has(vmcs12, CPU_BASED_RDTSC_EXITING);
9732 case EXIT_REASON_VMREAD:
9733 return nested_vmx_exit_handled_vmcs_access(vcpu, vmcs12,
9734 vmcs12->vmread_bitmap);
9735 case EXIT_REASON_VMWRITE:
9736 return nested_vmx_exit_handled_vmcs_access(vcpu, vmcs12,
9737 vmcs12->vmwrite_bitmap);
9738 case EXIT_REASON_VMCALL: case EXIT_REASON_VMCLEAR:
9739 case EXIT_REASON_VMLAUNCH: case EXIT_REASON_VMPTRLD:
9740 case EXIT_REASON_VMPTRST: case EXIT_REASON_VMRESUME:
9741 case EXIT_REASON_VMOFF: case EXIT_REASON_VMON:
9742 case EXIT_REASON_INVEPT: case EXIT_REASON_INVVPID:
9743 /*
9744 * VMX instructions trap unconditionally. This allows L1 to
9745 * emulate them for its L2 guest, i.e., allows 3-level nesting!
9746 */
9747 return true;
9748 case EXIT_REASON_CR_ACCESS:
9749 return nested_vmx_exit_handled_cr(vcpu, vmcs12);
9750 case EXIT_REASON_DR_ACCESS:
9751 return nested_cpu_has(vmcs12, CPU_BASED_MOV_DR_EXITING);
9752 case EXIT_REASON_IO_INSTRUCTION:
9753 return nested_vmx_exit_handled_io(vcpu, vmcs12);
9754 case EXIT_REASON_GDTR_IDTR: case EXIT_REASON_LDTR_TR:
9755 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_DESC);
9756 case EXIT_REASON_MSR_READ:
9757 case EXIT_REASON_MSR_WRITE:
9758 return nested_vmx_exit_handled_msr(vcpu, vmcs12, exit_reason);
9759 case EXIT_REASON_INVALID_STATE:
9760 return true;
9761 case EXIT_REASON_MWAIT_INSTRUCTION:
9762 return nested_cpu_has(vmcs12, CPU_BASED_MWAIT_EXITING);
9763 case EXIT_REASON_MONITOR_TRAP_FLAG:
9764 return nested_cpu_has(vmcs12, CPU_BASED_MONITOR_TRAP_FLAG);
9765 case EXIT_REASON_MONITOR_INSTRUCTION:
9766 return nested_cpu_has(vmcs12, CPU_BASED_MONITOR_EXITING);
9767 case EXIT_REASON_PAUSE_INSTRUCTION:
9768 return nested_cpu_has(vmcs12, CPU_BASED_PAUSE_EXITING) ||
9769 nested_cpu_has2(vmcs12,
9770 SECONDARY_EXEC_PAUSE_LOOP_EXITING);
9771 case EXIT_REASON_MCE_DURING_VMENTRY:
9772 return false;
9773 case EXIT_REASON_TPR_BELOW_THRESHOLD:
9774 return nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW);
9775 case EXIT_REASON_APIC_ACCESS:
9776 case EXIT_REASON_APIC_WRITE:
9777 case EXIT_REASON_EOI_INDUCED:
9778 /*
9779 * The controls for "virtualize APIC accesses," "APIC-
9780 * register virtualization," and "virtual-interrupt
9781 * delivery" only come from vmcs12.
9782 */
9783 return true;
9784 case EXIT_REASON_EPT_VIOLATION:
9785 /*
9786 * L0 always deals with the EPT violation. If nested EPT is
9787 * used, and the nested mmu code discovers that the address is
9788 * missing in the guest EPT table (EPT12), the EPT violation
9789 * will be injected with nested_ept_inject_page_fault()
9790 */
9791 return false;
9792 case EXIT_REASON_EPT_MISCONFIG:
9793 /*
9794 * L2 never uses directly L1's EPT, but rather L0's own EPT
9795 * table (shadow on EPT) or a merged EPT table that L0 built
9796 * (EPT on EPT). So any problems with the structure of the
9797 * table is L0's fault.
9798 */
9799 return false;
9800 case EXIT_REASON_INVPCID:
9801 return
9802 nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_INVPCID) &&
9803 nested_cpu_has(vmcs12, CPU_BASED_INVLPG_EXITING);
9804 case EXIT_REASON_WBINVD:
9805 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_WBINVD_EXITING);
9806 case EXIT_REASON_XSETBV:
9807 return true;
9808 case EXIT_REASON_XSAVES: case EXIT_REASON_XRSTORS:
9809 /*
9810 * This should never happen, since it is not possible to
9811 * set XSS to a non-zero value---neither in L1 nor in L2.
9812 * If if it were, XSS would have to be checked against
9813 * the XSS exit bitmap in vmcs12.
9814 */
9815 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_XSAVES);
9816 case EXIT_REASON_PREEMPTION_TIMER:
9817 return false;
9818 case EXIT_REASON_PML_FULL:
9819 /* We emulate PML support to L1. */
9820 return false;
9821 case EXIT_REASON_VMFUNC:
9822 /* VM functions are emulated through L2->L0 vmexits. */
9823 return false;
9824 case EXIT_REASON_ENCLS:
9825 /* SGX is never exposed to L1 */
9826 return false;
9827 default:
9828 return true;
9829 }
9830 }
9831
nested_vmx_reflect_vmexit(struct kvm_vcpu * vcpu,u32 exit_reason)9832 static int nested_vmx_reflect_vmexit(struct kvm_vcpu *vcpu, u32 exit_reason)
9833 {
9834 u32 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
9835
9836 /*
9837 * At this point, the exit interruption info in exit_intr_info
9838 * is only valid for EXCEPTION_NMI exits. For EXTERNAL_INTERRUPT
9839 * we need to query the in-kernel LAPIC.
9840 */
9841 WARN_ON(exit_reason == EXIT_REASON_EXTERNAL_INTERRUPT);
9842 if ((exit_intr_info &
9843 (INTR_INFO_VALID_MASK | INTR_INFO_DELIVER_CODE_MASK)) ==
9844 (INTR_INFO_VALID_MASK | INTR_INFO_DELIVER_CODE_MASK)) {
9845 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
9846 vmcs12->vm_exit_intr_error_code =
9847 vmcs_read32(VM_EXIT_INTR_ERROR_CODE);
9848 }
9849
9850 nested_vmx_vmexit(vcpu, exit_reason, exit_intr_info,
9851 vmcs_readl(EXIT_QUALIFICATION));
9852 return 1;
9853 }
9854
vmx_get_exit_info(struct kvm_vcpu * vcpu,u64 * info1,u64 * info2)9855 static void vmx_get_exit_info(struct kvm_vcpu *vcpu, u64 *info1, u64 *info2)
9856 {
9857 *info1 = vmcs_readl(EXIT_QUALIFICATION);
9858 *info2 = vmcs_read32(VM_EXIT_INTR_INFO);
9859 }
9860
vmx_destroy_pml_buffer(struct vcpu_vmx * vmx)9861 static void vmx_destroy_pml_buffer(struct vcpu_vmx *vmx)
9862 {
9863 if (vmx->pml_pg) {
9864 __free_page(vmx->pml_pg);
9865 vmx->pml_pg = NULL;
9866 }
9867 }
9868
vmx_flush_pml_buffer(struct kvm_vcpu * vcpu)9869 static void vmx_flush_pml_buffer(struct kvm_vcpu *vcpu)
9870 {
9871 struct vcpu_vmx *vmx = to_vmx(vcpu);
9872 u64 *pml_buf;
9873 u16 pml_idx;
9874
9875 pml_idx = vmcs_read16(GUEST_PML_INDEX);
9876
9877 /* Do nothing if PML buffer is empty */
9878 if (pml_idx == (PML_ENTITY_NUM - 1))
9879 return;
9880
9881 /* PML index always points to next available PML buffer entity */
9882 if (pml_idx >= PML_ENTITY_NUM)
9883 pml_idx = 0;
9884 else
9885 pml_idx++;
9886
9887 pml_buf = page_address(vmx->pml_pg);
9888 for (; pml_idx < PML_ENTITY_NUM; pml_idx++) {
9889 u64 gpa;
9890
9891 gpa = pml_buf[pml_idx];
9892 WARN_ON(gpa & (PAGE_SIZE - 1));
9893 kvm_vcpu_mark_page_dirty(vcpu, gpa >> PAGE_SHIFT);
9894 }
9895
9896 /* reset PML index */
9897 vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1);
9898 }
9899
9900 /*
9901 * Flush all vcpus' PML buffer and update logged GPAs to dirty_bitmap.
9902 * Called before reporting dirty_bitmap to userspace.
9903 */
kvm_flush_pml_buffers(struct kvm * kvm)9904 static void kvm_flush_pml_buffers(struct kvm *kvm)
9905 {
9906 int i;
9907 struct kvm_vcpu *vcpu;
9908 /*
9909 * We only need to kick vcpu out of guest mode here, as PML buffer
9910 * is flushed at beginning of all VMEXITs, and it's obvious that only
9911 * vcpus running in guest are possible to have unflushed GPAs in PML
9912 * buffer.
9913 */
9914 kvm_for_each_vcpu(i, vcpu, kvm)
9915 kvm_vcpu_kick(vcpu);
9916 }
9917
vmx_dump_sel(char * name,uint32_t sel)9918 static void vmx_dump_sel(char *name, uint32_t sel)
9919 {
9920 pr_err("%s sel=0x%04x, attr=0x%05x, limit=0x%08x, base=0x%016lx\n",
9921 name, vmcs_read16(sel),
9922 vmcs_read32(sel + GUEST_ES_AR_BYTES - GUEST_ES_SELECTOR),
9923 vmcs_read32(sel + GUEST_ES_LIMIT - GUEST_ES_SELECTOR),
9924 vmcs_readl(sel + GUEST_ES_BASE - GUEST_ES_SELECTOR));
9925 }
9926
vmx_dump_dtsel(char * name,uint32_t limit)9927 static void vmx_dump_dtsel(char *name, uint32_t limit)
9928 {
9929 pr_err("%s limit=0x%08x, base=0x%016lx\n",
9930 name, vmcs_read32(limit),
9931 vmcs_readl(limit + GUEST_GDTR_BASE - GUEST_GDTR_LIMIT));
9932 }
9933
dump_vmcs(void)9934 static void dump_vmcs(void)
9935 {
9936 u32 vmentry_ctl = vmcs_read32(VM_ENTRY_CONTROLS);
9937 u32 vmexit_ctl = vmcs_read32(VM_EXIT_CONTROLS);
9938 u32 cpu_based_exec_ctrl = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
9939 u32 pin_based_exec_ctrl = vmcs_read32(PIN_BASED_VM_EXEC_CONTROL);
9940 u32 secondary_exec_control = 0;
9941 unsigned long cr4 = vmcs_readl(GUEST_CR4);
9942 u64 efer = vmcs_read64(GUEST_IA32_EFER);
9943 int i, n;
9944
9945 if (cpu_has_secondary_exec_ctrls())
9946 secondary_exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
9947
9948 pr_err("*** Guest State ***\n");
9949 pr_err("CR0: actual=0x%016lx, shadow=0x%016lx, gh_mask=%016lx\n",
9950 vmcs_readl(GUEST_CR0), vmcs_readl(CR0_READ_SHADOW),
9951 vmcs_readl(CR0_GUEST_HOST_MASK));
9952 pr_err("CR4: actual=0x%016lx, shadow=0x%016lx, gh_mask=%016lx\n",
9953 cr4, vmcs_readl(CR4_READ_SHADOW), vmcs_readl(CR4_GUEST_HOST_MASK));
9954 pr_err("CR3 = 0x%016lx\n", vmcs_readl(GUEST_CR3));
9955 if ((secondary_exec_control & SECONDARY_EXEC_ENABLE_EPT) &&
9956 (cr4 & X86_CR4_PAE) && !(efer & EFER_LMA))
9957 {
9958 pr_err("PDPTR0 = 0x%016llx PDPTR1 = 0x%016llx\n",
9959 vmcs_read64(GUEST_PDPTR0), vmcs_read64(GUEST_PDPTR1));
9960 pr_err("PDPTR2 = 0x%016llx PDPTR3 = 0x%016llx\n",
9961 vmcs_read64(GUEST_PDPTR2), vmcs_read64(GUEST_PDPTR3));
9962 }
9963 pr_err("RSP = 0x%016lx RIP = 0x%016lx\n",
9964 vmcs_readl(GUEST_RSP), vmcs_readl(GUEST_RIP));
9965 pr_err("RFLAGS=0x%08lx DR7 = 0x%016lx\n",
9966 vmcs_readl(GUEST_RFLAGS), vmcs_readl(GUEST_DR7));
9967 pr_err("Sysenter RSP=%016lx CS:RIP=%04x:%016lx\n",
9968 vmcs_readl(GUEST_SYSENTER_ESP),
9969 vmcs_read32(GUEST_SYSENTER_CS), vmcs_readl(GUEST_SYSENTER_EIP));
9970 vmx_dump_sel("CS: ", GUEST_CS_SELECTOR);
9971 vmx_dump_sel("DS: ", GUEST_DS_SELECTOR);
9972 vmx_dump_sel("SS: ", GUEST_SS_SELECTOR);
9973 vmx_dump_sel("ES: ", GUEST_ES_SELECTOR);
9974 vmx_dump_sel("FS: ", GUEST_FS_SELECTOR);
9975 vmx_dump_sel("GS: ", GUEST_GS_SELECTOR);
9976 vmx_dump_dtsel("GDTR:", GUEST_GDTR_LIMIT);
9977 vmx_dump_sel("LDTR:", GUEST_LDTR_SELECTOR);
9978 vmx_dump_dtsel("IDTR:", GUEST_IDTR_LIMIT);
9979 vmx_dump_sel("TR: ", GUEST_TR_SELECTOR);
9980 if ((vmexit_ctl & (VM_EXIT_SAVE_IA32_PAT | VM_EXIT_SAVE_IA32_EFER)) ||
9981 (vmentry_ctl & (VM_ENTRY_LOAD_IA32_PAT | VM_ENTRY_LOAD_IA32_EFER)))
9982 pr_err("EFER = 0x%016llx PAT = 0x%016llx\n",
9983 efer, vmcs_read64(GUEST_IA32_PAT));
9984 pr_err("DebugCtl = 0x%016llx DebugExceptions = 0x%016lx\n",
9985 vmcs_read64(GUEST_IA32_DEBUGCTL),
9986 vmcs_readl(GUEST_PENDING_DBG_EXCEPTIONS));
9987 if (cpu_has_load_perf_global_ctrl &&
9988 vmentry_ctl & VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL)
9989 pr_err("PerfGlobCtl = 0x%016llx\n",
9990 vmcs_read64(GUEST_IA32_PERF_GLOBAL_CTRL));
9991 if (vmentry_ctl & VM_ENTRY_LOAD_BNDCFGS)
9992 pr_err("BndCfgS = 0x%016llx\n", vmcs_read64(GUEST_BNDCFGS));
9993 pr_err("Interruptibility = %08x ActivityState = %08x\n",
9994 vmcs_read32(GUEST_INTERRUPTIBILITY_INFO),
9995 vmcs_read32(GUEST_ACTIVITY_STATE));
9996 if (secondary_exec_control & SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY)
9997 pr_err("InterruptStatus = %04x\n",
9998 vmcs_read16(GUEST_INTR_STATUS));
9999
10000 pr_err("*** Host State ***\n");
10001 pr_err("RIP = 0x%016lx RSP = 0x%016lx\n",
10002 vmcs_readl(HOST_RIP), vmcs_readl(HOST_RSP));
10003 pr_err("CS=%04x SS=%04x DS=%04x ES=%04x FS=%04x GS=%04x TR=%04x\n",
10004 vmcs_read16(HOST_CS_SELECTOR), vmcs_read16(HOST_SS_SELECTOR),
10005 vmcs_read16(HOST_DS_SELECTOR), vmcs_read16(HOST_ES_SELECTOR),
10006 vmcs_read16(HOST_FS_SELECTOR), vmcs_read16(HOST_GS_SELECTOR),
10007 vmcs_read16(HOST_TR_SELECTOR));
10008 pr_err("FSBase=%016lx GSBase=%016lx TRBase=%016lx\n",
10009 vmcs_readl(HOST_FS_BASE), vmcs_readl(HOST_GS_BASE),
10010 vmcs_readl(HOST_TR_BASE));
10011 pr_err("GDTBase=%016lx IDTBase=%016lx\n",
10012 vmcs_readl(HOST_GDTR_BASE), vmcs_readl(HOST_IDTR_BASE));
10013 pr_err("CR0=%016lx CR3=%016lx CR4=%016lx\n",
10014 vmcs_readl(HOST_CR0), vmcs_readl(HOST_CR3),
10015 vmcs_readl(HOST_CR4));
10016 pr_err("Sysenter RSP=%016lx CS:RIP=%04x:%016lx\n",
10017 vmcs_readl(HOST_IA32_SYSENTER_ESP),
10018 vmcs_read32(HOST_IA32_SYSENTER_CS),
10019 vmcs_readl(HOST_IA32_SYSENTER_EIP));
10020 if (vmexit_ctl & (VM_EXIT_LOAD_IA32_PAT | VM_EXIT_LOAD_IA32_EFER))
10021 pr_err("EFER = 0x%016llx PAT = 0x%016llx\n",
10022 vmcs_read64(HOST_IA32_EFER),
10023 vmcs_read64(HOST_IA32_PAT));
10024 if (cpu_has_load_perf_global_ctrl &&
10025 vmexit_ctl & VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL)
10026 pr_err("PerfGlobCtl = 0x%016llx\n",
10027 vmcs_read64(HOST_IA32_PERF_GLOBAL_CTRL));
10028
10029 pr_err("*** Control State ***\n");
10030 pr_err("PinBased=%08x CPUBased=%08x SecondaryExec=%08x\n",
10031 pin_based_exec_ctrl, cpu_based_exec_ctrl, secondary_exec_control);
10032 pr_err("EntryControls=%08x ExitControls=%08x\n", vmentry_ctl, vmexit_ctl);
10033 pr_err("ExceptionBitmap=%08x PFECmask=%08x PFECmatch=%08x\n",
10034 vmcs_read32(EXCEPTION_BITMAP),
10035 vmcs_read32(PAGE_FAULT_ERROR_CODE_MASK),
10036 vmcs_read32(PAGE_FAULT_ERROR_CODE_MATCH));
10037 pr_err("VMEntry: intr_info=%08x errcode=%08x ilen=%08x\n",
10038 vmcs_read32(VM_ENTRY_INTR_INFO_FIELD),
10039 vmcs_read32(VM_ENTRY_EXCEPTION_ERROR_CODE),
10040 vmcs_read32(VM_ENTRY_INSTRUCTION_LEN));
10041 pr_err("VMExit: intr_info=%08x errcode=%08x ilen=%08x\n",
10042 vmcs_read32(VM_EXIT_INTR_INFO),
10043 vmcs_read32(VM_EXIT_INTR_ERROR_CODE),
10044 vmcs_read32(VM_EXIT_INSTRUCTION_LEN));
10045 pr_err(" reason=%08x qualification=%016lx\n",
10046 vmcs_read32(VM_EXIT_REASON), vmcs_readl(EXIT_QUALIFICATION));
10047 pr_err("IDTVectoring: info=%08x errcode=%08x\n",
10048 vmcs_read32(IDT_VECTORING_INFO_FIELD),
10049 vmcs_read32(IDT_VECTORING_ERROR_CODE));
10050 pr_err("TSC Offset = 0x%016llx\n", vmcs_read64(TSC_OFFSET));
10051 if (secondary_exec_control & SECONDARY_EXEC_TSC_SCALING)
10052 pr_err("TSC Multiplier = 0x%016llx\n",
10053 vmcs_read64(TSC_MULTIPLIER));
10054 if (cpu_based_exec_ctrl & CPU_BASED_TPR_SHADOW)
10055 pr_err("TPR Threshold = 0x%02x\n", vmcs_read32(TPR_THRESHOLD));
10056 if (pin_based_exec_ctrl & PIN_BASED_POSTED_INTR)
10057 pr_err("PostedIntrVec = 0x%02x\n", vmcs_read16(POSTED_INTR_NV));
10058 if ((secondary_exec_control & SECONDARY_EXEC_ENABLE_EPT))
10059 pr_err("EPT pointer = 0x%016llx\n", vmcs_read64(EPT_POINTER));
10060 n = vmcs_read32(CR3_TARGET_COUNT);
10061 for (i = 0; i + 1 < n; i += 4)
10062 pr_err("CR3 target%u=%016lx target%u=%016lx\n",
10063 i, vmcs_readl(CR3_TARGET_VALUE0 + i * 2),
10064 i + 1, vmcs_readl(CR3_TARGET_VALUE0 + i * 2 + 2));
10065 if (i < n)
10066 pr_err("CR3 target%u=%016lx\n",
10067 i, vmcs_readl(CR3_TARGET_VALUE0 + i * 2));
10068 if (secondary_exec_control & SECONDARY_EXEC_PAUSE_LOOP_EXITING)
10069 pr_err("PLE Gap=%08x Window=%08x\n",
10070 vmcs_read32(PLE_GAP), vmcs_read32(PLE_WINDOW));
10071 if (secondary_exec_control & SECONDARY_EXEC_ENABLE_VPID)
10072 pr_err("Virtual processor ID = 0x%04x\n",
10073 vmcs_read16(VIRTUAL_PROCESSOR_ID));
10074 }
10075
10076 /*
10077 * The guest has exited. See if we can fix it or if we need userspace
10078 * assistance.
10079 */
vmx_handle_exit(struct kvm_vcpu * vcpu)10080 static int vmx_handle_exit(struct kvm_vcpu *vcpu)
10081 {
10082 struct vcpu_vmx *vmx = to_vmx(vcpu);
10083 u32 exit_reason = vmx->exit_reason;
10084 u32 vectoring_info = vmx->idt_vectoring_info;
10085
10086 trace_kvm_exit(exit_reason, vcpu, KVM_ISA_VMX);
10087
10088 /*
10089 * Flush logged GPAs PML buffer, this will make dirty_bitmap more
10090 * updated. Another good is, in kvm_vm_ioctl_get_dirty_log, before
10091 * querying dirty_bitmap, we only need to kick all vcpus out of guest
10092 * mode as if vcpus is in root mode, the PML buffer must has been
10093 * flushed already.
10094 */
10095 if (enable_pml)
10096 vmx_flush_pml_buffer(vcpu);
10097
10098 /* If guest state is invalid, start emulating */
10099 if (vmx->emulation_required)
10100 return handle_invalid_guest_state(vcpu);
10101
10102 if (is_guest_mode(vcpu) && nested_vmx_exit_reflected(vcpu, exit_reason))
10103 return nested_vmx_reflect_vmexit(vcpu, exit_reason);
10104
10105 if (exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY) {
10106 dump_vmcs();
10107 vcpu->run->exit_reason = KVM_EXIT_FAIL_ENTRY;
10108 vcpu->run->fail_entry.hardware_entry_failure_reason
10109 = exit_reason;
10110 return 0;
10111 }
10112
10113 if (unlikely(vmx->fail)) {
10114 vcpu->run->exit_reason = KVM_EXIT_FAIL_ENTRY;
10115 vcpu->run->fail_entry.hardware_entry_failure_reason
10116 = vmcs_read32(VM_INSTRUCTION_ERROR);
10117 return 0;
10118 }
10119
10120 /*
10121 * Note:
10122 * Do not try to fix EXIT_REASON_EPT_MISCONFIG if it caused by
10123 * delivery event since it indicates guest is accessing MMIO.
10124 * The vm-exit can be triggered again after return to guest that
10125 * will cause infinite loop.
10126 */
10127 if ((vectoring_info & VECTORING_INFO_VALID_MASK) &&
10128 (exit_reason != EXIT_REASON_EXCEPTION_NMI &&
10129 exit_reason != EXIT_REASON_EPT_VIOLATION &&
10130 exit_reason != EXIT_REASON_PML_FULL &&
10131 exit_reason != EXIT_REASON_APIC_ACCESS &&
10132 exit_reason != EXIT_REASON_TASK_SWITCH)) {
10133 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
10134 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_DELIVERY_EV;
10135 vcpu->run->internal.ndata = 3;
10136 vcpu->run->internal.data[0] = vectoring_info;
10137 vcpu->run->internal.data[1] = exit_reason;
10138 vcpu->run->internal.data[2] = vcpu->arch.exit_qualification;
10139 if (exit_reason == EXIT_REASON_EPT_MISCONFIG) {
10140 vcpu->run->internal.ndata++;
10141 vcpu->run->internal.data[3] =
10142 vmcs_read64(GUEST_PHYSICAL_ADDRESS);
10143 }
10144 return 0;
10145 }
10146
10147 if (unlikely(!enable_vnmi &&
10148 vmx->loaded_vmcs->soft_vnmi_blocked)) {
10149 if (vmx_interrupt_allowed(vcpu)) {
10150 vmx->loaded_vmcs->soft_vnmi_blocked = 0;
10151 } else if (vmx->loaded_vmcs->vnmi_blocked_time > 1000000000LL &&
10152 vcpu->arch.nmi_pending) {
10153 /*
10154 * This CPU don't support us in finding the end of an
10155 * NMI-blocked window if the guest runs with IRQs
10156 * disabled. So we pull the trigger after 1 s of
10157 * futile waiting, but inform the user about this.
10158 */
10159 printk(KERN_WARNING "%s: Breaking out of NMI-blocked "
10160 "state on VCPU %d after 1 s timeout\n",
10161 __func__, vcpu->vcpu_id);
10162 vmx->loaded_vmcs->soft_vnmi_blocked = 0;
10163 }
10164 }
10165
10166 if (exit_reason < kvm_vmx_max_exit_handlers
10167 && kvm_vmx_exit_handlers[exit_reason])
10168 return kvm_vmx_exit_handlers[exit_reason](vcpu);
10169 else {
10170 vcpu_unimpl(vcpu, "vmx: unexpected exit reason 0x%x\n",
10171 exit_reason);
10172 kvm_queue_exception(vcpu, UD_VECTOR);
10173 return 1;
10174 }
10175 }
10176
10177 /*
10178 * Software based L1D cache flush which is used when microcode providing
10179 * the cache control MSR is not loaded.
10180 *
10181 * The L1D cache is 32 KiB on Nehalem and later microarchitectures, but to
10182 * flush it is required to read in 64 KiB because the replacement algorithm
10183 * is not exactly LRU. This could be sized at runtime via topology
10184 * information but as all relevant affected CPUs have 32KiB L1D cache size
10185 * there is no point in doing so.
10186 */
vmx_l1d_flush(struct kvm_vcpu * vcpu)10187 static void vmx_l1d_flush(struct kvm_vcpu *vcpu)
10188 {
10189 int size = PAGE_SIZE << L1D_CACHE_ORDER;
10190
10191 /*
10192 * This code is only executed when the the flush mode is 'cond' or
10193 * 'always'
10194 */
10195 if (static_branch_likely(&vmx_l1d_flush_cond)) {
10196 bool flush_l1d;
10197
10198 /*
10199 * Clear the per-vcpu flush bit, it gets set again
10200 * either from vcpu_run() or from one of the unsafe
10201 * VMEXIT handlers.
10202 */
10203 flush_l1d = vcpu->arch.l1tf_flush_l1d;
10204 vcpu->arch.l1tf_flush_l1d = false;
10205
10206 /*
10207 * Clear the per-cpu flush bit, it gets set again from
10208 * the interrupt handlers.
10209 */
10210 flush_l1d |= kvm_get_cpu_l1tf_flush_l1d();
10211 kvm_clear_cpu_l1tf_flush_l1d();
10212
10213 if (!flush_l1d)
10214 return;
10215 }
10216
10217 vcpu->stat.l1d_flush++;
10218
10219 if (static_cpu_has(X86_FEATURE_FLUSH_L1D)) {
10220 wrmsrl(MSR_IA32_FLUSH_CMD, L1D_FLUSH);
10221 return;
10222 }
10223
10224 asm volatile(
10225 /* First ensure the pages are in the TLB */
10226 "xorl %%eax, %%eax\n"
10227 ".Lpopulate_tlb:\n\t"
10228 "movzbl (%[flush_pages], %%" _ASM_AX "), %%ecx\n\t"
10229 "addl $4096, %%eax\n\t"
10230 "cmpl %%eax, %[size]\n\t"
10231 "jne .Lpopulate_tlb\n\t"
10232 "xorl %%eax, %%eax\n\t"
10233 "cpuid\n\t"
10234 /* Now fill the cache */
10235 "xorl %%eax, %%eax\n"
10236 ".Lfill_cache:\n"
10237 "movzbl (%[flush_pages], %%" _ASM_AX "), %%ecx\n\t"
10238 "addl $64, %%eax\n\t"
10239 "cmpl %%eax, %[size]\n\t"
10240 "jne .Lfill_cache\n\t"
10241 "lfence\n"
10242 :: [flush_pages] "r" (vmx_l1d_flush_pages),
10243 [size] "r" (size)
10244 : "eax", "ebx", "ecx", "edx");
10245 }
10246
update_cr8_intercept(struct kvm_vcpu * vcpu,int tpr,int irr)10247 static void update_cr8_intercept(struct kvm_vcpu *vcpu, int tpr, int irr)
10248 {
10249 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
10250
10251 if (is_guest_mode(vcpu) &&
10252 nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW))
10253 return;
10254
10255 if (irr == -1 || tpr < irr) {
10256 vmcs_write32(TPR_THRESHOLD, 0);
10257 return;
10258 }
10259
10260 vmcs_write32(TPR_THRESHOLD, irr);
10261 }
10262
vmx_set_virtual_apic_mode(struct kvm_vcpu * vcpu)10263 static void vmx_set_virtual_apic_mode(struct kvm_vcpu *vcpu)
10264 {
10265 u32 sec_exec_control;
10266
10267 if (!lapic_in_kernel(vcpu))
10268 return;
10269
10270 if (!flexpriority_enabled &&
10271 !cpu_has_vmx_virtualize_x2apic_mode())
10272 return;
10273
10274 /* Postpone execution until vmcs01 is the current VMCS. */
10275 if (is_guest_mode(vcpu)) {
10276 to_vmx(vcpu)->nested.change_vmcs01_virtual_apic_mode = true;
10277 return;
10278 }
10279
10280 sec_exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
10281 sec_exec_control &= ~(SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
10282 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE);
10283
10284 switch (kvm_get_apic_mode(vcpu)) {
10285 case LAPIC_MODE_INVALID:
10286 WARN_ONCE(true, "Invalid local APIC state");
10287 case LAPIC_MODE_DISABLED:
10288 break;
10289 case LAPIC_MODE_XAPIC:
10290 if (flexpriority_enabled) {
10291 sec_exec_control |=
10292 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
10293 vmx_flush_tlb(vcpu, true);
10294 }
10295 break;
10296 case LAPIC_MODE_X2APIC:
10297 if (cpu_has_vmx_virtualize_x2apic_mode())
10298 sec_exec_control |=
10299 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
10300 break;
10301 }
10302 vmcs_write32(SECONDARY_VM_EXEC_CONTROL, sec_exec_control);
10303
10304 vmx_update_msr_bitmap(vcpu);
10305 }
10306
vmx_set_apic_access_page_addr(struct kvm_vcpu * vcpu,hpa_t hpa)10307 static void vmx_set_apic_access_page_addr(struct kvm_vcpu *vcpu, hpa_t hpa)
10308 {
10309 if (!is_guest_mode(vcpu)) {
10310 vmcs_write64(APIC_ACCESS_ADDR, hpa);
10311 vmx_flush_tlb(vcpu, true);
10312 }
10313 }
10314
vmx_hwapic_isr_update(struct kvm_vcpu * vcpu,int max_isr)10315 static void vmx_hwapic_isr_update(struct kvm_vcpu *vcpu, int max_isr)
10316 {
10317 u16 status;
10318 u8 old;
10319
10320 if (max_isr == -1)
10321 max_isr = 0;
10322
10323 status = vmcs_read16(GUEST_INTR_STATUS);
10324 old = status >> 8;
10325 if (max_isr != old) {
10326 status &= 0xff;
10327 status |= max_isr << 8;
10328 vmcs_write16(GUEST_INTR_STATUS, status);
10329 }
10330 }
10331
vmx_set_rvi(int vector)10332 static void vmx_set_rvi(int vector)
10333 {
10334 u16 status;
10335 u8 old;
10336
10337 if (vector == -1)
10338 vector = 0;
10339
10340 status = vmcs_read16(GUEST_INTR_STATUS);
10341 old = (u8)status & 0xff;
10342 if ((u8)vector != old) {
10343 status &= ~0xff;
10344 status |= (u8)vector;
10345 vmcs_write16(GUEST_INTR_STATUS, status);
10346 }
10347 }
10348
vmx_hwapic_irr_update(struct kvm_vcpu * vcpu,int max_irr)10349 static void vmx_hwapic_irr_update(struct kvm_vcpu *vcpu, int max_irr)
10350 {
10351 /*
10352 * When running L2, updating RVI is only relevant when
10353 * vmcs12 virtual-interrupt-delivery enabled.
10354 * However, it can be enabled only when L1 also
10355 * intercepts external-interrupts and in that case
10356 * we should not update vmcs02 RVI but instead intercept
10357 * interrupt. Therefore, do nothing when running L2.
10358 */
10359 if (!is_guest_mode(vcpu))
10360 vmx_set_rvi(max_irr);
10361 }
10362
vmx_sync_pir_to_irr(struct kvm_vcpu * vcpu)10363 static int vmx_sync_pir_to_irr(struct kvm_vcpu *vcpu)
10364 {
10365 struct vcpu_vmx *vmx = to_vmx(vcpu);
10366 int max_irr;
10367 bool max_irr_updated;
10368
10369 WARN_ON(!vcpu->arch.apicv_active);
10370 if (pi_test_on(&vmx->pi_desc)) {
10371 pi_clear_on(&vmx->pi_desc);
10372 /*
10373 * IOMMU can write to PIR.ON, so the barrier matters even on UP.
10374 * But on x86 this is just a compiler barrier anyway.
10375 */
10376 smp_mb__after_atomic();
10377 max_irr_updated =
10378 kvm_apic_update_irr(vcpu, vmx->pi_desc.pir, &max_irr);
10379
10380 /*
10381 * If we are running L2 and L1 has a new pending interrupt
10382 * which can be injected, we should re-evaluate
10383 * what should be done with this new L1 interrupt.
10384 * If L1 intercepts external-interrupts, we should
10385 * exit from L2 to L1. Otherwise, interrupt should be
10386 * delivered directly to L2.
10387 */
10388 if (is_guest_mode(vcpu) && max_irr_updated) {
10389 if (nested_exit_on_intr(vcpu))
10390 kvm_vcpu_exiting_guest_mode(vcpu);
10391 else
10392 kvm_make_request(KVM_REQ_EVENT, vcpu);
10393 }
10394 } else {
10395 max_irr = kvm_lapic_find_highest_irr(vcpu);
10396 }
10397 vmx_hwapic_irr_update(vcpu, max_irr);
10398 return max_irr;
10399 }
10400
vmx_has_apicv_interrupt(struct kvm_vcpu * vcpu)10401 static u8 vmx_has_apicv_interrupt(struct kvm_vcpu *vcpu)
10402 {
10403 u8 rvi = vmx_get_rvi();
10404 u8 vppr = kvm_lapic_get_reg(vcpu->arch.apic, APIC_PROCPRI);
10405
10406 return ((rvi & 0xf0) > (vppr & 0xf0));
10407 }
10408
vmx_dy_apicv_has_pending_interrupt(struct kvm_vcpu * vcpu)10409 static bool vmx_dy_apicv_has_pending_interrupt(struct kvm_vcpu *vcpu)
10410 {
10411 return pi_test_on(vcpu_to_pi_desc(vcpu));
10412 }
10413
vmx_load_eoi_exitmap(struct kvm_vcpu * vcpu,u64 * eoi_exit_bitmap)10414 static void vmx_load_eoi_exitmap(struct kvm_vcpu *vcpu, u64 *eoi_exit_bitmap)
10415 {
10416 if (!kvm_vcpu_apicv_active(vcpu))
10417 return;
10418
10419 vmcs_write64(EOI_EXIT_BITMAP0, eoi_exit_bitmap[0]);
10420 vmcs_write64(EOI_EXIT_BITMAP1, eoi_exit_bitmap[1]);
10421 vmcs_write64(EOI_EXIT_BITMAP2, eoi_exit_bitmap[2]);
10422 vmcs_write64(EOI_EXIT_BITMAP3, eoi_exit_bitmap[3]);
10423 }
10424
vmx_apicv_post_state_restore(struct kvm_vcpu * vcpu)10425 static void vmx_apicv_post_state_restore(struct kvm_vcpu *vcpu)
10426 {
10427 struct vcpu_vmx *vmx = to_vmx(vcpu);
10428
10429 pi_clear_on(&vmx->pi_desc);
10430 memset(vmx->pi_desc.pir, 0, sizeof(vmx->pi_desc.pir));
10431 }
10432
vmx_complete_atomic_exit(struct vcpu_vmx * vmx)10433 static void vmx_complete_atomic_exit(struct vcpu_vmx *vmx)
10434 {
10435 if (vmx->exit_reason != EXIT_REASON_EXCEPTION_NMI)
10436 return;
10437
10438 vmx->exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
10439
10440 /* if exit due to PF check for async PF */
10441 if (is_page_fault(vmx->exit_intr_info))
10442 vmx->vcpu.arch.apf.host_apf_reason = kvm_read_and_reset_pf_reason();
10443
10444 /* Handle machine checks before interrupts are enabled */
10445 if (is_machine_check(vmx->exit_intr_info))
10446 kvm_machine_check();
10447
10448 /* We need to handle NMIs before interrupts are enabled */
10449 if (is_nmi(vmx->exit_intr_info)) {
10450 kvm_before_interrupt(&vmx->vcpu);
10451 asm("int $2");
10452 kvm_after_interrupt(&vmx->vcpu);
10453 }
10454 }
10455
vmx_handle_external_intr(struct kvm_vcpu * vcpu)10456 static void vmx_handle_external_intr(struct kvm_vcpu *vcpu)
10457 {
10458 u32 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
10459
10460 if ((exit_intr_info & (INTR_INFO_VALID_MASK | INTR_INFO_INTR_TYPE_MASK))
10461 == (INTR_INFO_VALID_MASK | INTR_TYPE_EXT_INTR)) {
10462 unsigned int vector;
10463 unsigned long entry;
10464 gate_desc *desc;
10465 struct vcpu_vmx *vmx = to_vmx(vcpu);
10466 #ifdef CONFIG_X86_64
10467 unsigned long tmp;
10468 #endif
10469
10470 vector = exit_intr_info & INTR_INFO_VECTOR_MASK;
10471 desc = (gate_desc *)vmx->host_idt_base + vector;
10472 entry = gate_offset(desc);
10473 asm volatile(
10474 #ifdef CONFIG_X86_64
10475 "mov %%" _ASM_SP ", %[sp]\n\t"
10476 "and $0xfffffffffffffff0, %%" _ASM_SP "\n\t"
10477 "push $%c[ss]\n\t"
10478 "push %[sp]\n\t"
10479 #endif
10480 "pushf\n\t"
10481 __ASM_SIZE(push) " $%c[cs]\n\t"
10482 CALL_NOSPEC
10483 :
10484 #ifdef CONFIG_X86_64
10485 [sp]"=&r"(tmp),
10486 #endif
10487 ASM_CALL_CONSTRAINT
10488 :
10489 THUNK_TARGET(entry),
10490 [ss]"i"(__KERNEL_DS),
10491 [cs]"i"(__KERNEL_CS)
10492 );
10493 }
10494 }
10495 STACK_FRAME_NON_STANDARD(vmx_handle_external_intr);
10496
vmx_has_emulated_msr(int index)10497 static bool vmx_has_emulated_msr(int index)
10498 {
10499 switch (index) {
10500 case MSR_IA32_SMBASE:
10501 /*
10502 * We cannot do SMM unless we can run the guest in big
10503 * real mode.
10504 */
10505 return enable_unrestricted_guest || emulate_invalid_guest_state;
10506 case MSR_AMD64_VIRT_SPEC_CTRL:
10507 /* This is AMD only. */
10508 return false;
10509 default:
10510 return true;
10511 }
10512 }
10513
vmx_mpx_supported(void)10514 static bool vmx_mpx_supported(void)
10515 {
10516 return (vmcs_config.vmexit_ctrl & VM_EXIT_CLEAR_BNDCFGS) &&
10517 (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_BNDCFGS);
10518 }
10519
vmx_xsaves_supported(void)10520 static bool vmx_xsaves_supported(void)
10521 {
10522 return vmcs_config.cpu_based_2nd_exec_ctrl &
10523 SECONDARY_EXEC_XSAVES;
10524 }
10525
vmx_recover_nmi_blocking(struct vcpu_vmx * vmx)10526 static void vmx_recover_nmi_blocking(struct vcpu_vmx *vmx)
10527 {
10528 u32 exit_intr_info;
10529 bool unblock_nmi;
10530 u8 vector;
10531 bool idtv_info_valid;
10532
10533 idtv_info_valid = vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK;
10534
10535 if (enable_vnmi) {
10536 if (vmx->loaded_vmcs->nmi_known_unmasked)
10537 return;
10538 /*
10539 * Can't use vmx->exit_intr_info since we're not sure what
10540 * the exit reason is.
10541 */
10542 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
10543 unblock_nmi = (exit_intr_info & INTR_INFO_UNBLOCK_NMI) != 0;
10544 vector = exit_intr_info & INTR_INFO_VECTOR_MASK;
10545 /*
10546 * SDM 3: 27.7.1.2 (September 2008)
10547 * Re-set bit "block by NMI" before VM entry if vmexit caused by
10548 * a guest IRET fault.
10549 * SDM 3: 23.2.2 (September 2008)
10550 * Bit 12 is undefined in any of the following cases:
10551 * If the VM exit sets the valid bit in the IDT-vectoring
10552 * information field.
10553 * If the VM exit is due to a double fault.
10554 */
10555 if ((exit_intr_info & INTR_INFO_VALID_MASK) && unblock_nmi &&
10556 vector != DF_VECTOR && !idtv_info_valid)
10557 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
10558 GUEST_INTR_STATE_NMI);
10559 else
10560 vmx->loaded_vmcs->nmi_known_unmasked =
10561 !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO)
10562 & GUEST_INTR_STATE_NMI);
10563 } else if (unlikely(vmx->loaded_vmcs->soft_vnmi_blocked))
10564 vmx->loaded_vmcs->vnmi_blocked_time +=
10565 ktime_to_ns(ktime_sub(ktime_get(),
10566 vmx->loaded_vmcs->entry_time));
10567 }
10568
__vmx_complete_interrupts(struct kvm_vcpu * vcpu,u32 idt_vectoring_info,int instr_len_field,int error_code_field)10569 static void __vmx_complete_interrupts(struct kvm_vcpu *vcpu,
10570 u32 idt_vectoring_info,
10571 int instr_len_field,
10572 int error_code_field)
10573 {
10574 u8 vector;
10575 int type;
10576 bool idtv_info_valid;
10577
10578 idtv_info_valid = idt_vectoring_info & VECTORING_INFO_VALID_MASK;
10579
10580 vcpu->arch.nmi_injected = false;
10581 kvm_clear_exception_queue(vcpu);
10582 kvm_clear_interrupt_queue(vcpu);
10583
10584 if (!idtv_info_valid)
10585 return;
10586
10587 kvm_make_request(KVM_REQ_EVENT, vcpu);
10588
10589 vector = idt_vectoring_info & VECTORING_INFO_VECTOR_MASK;
10590 type = idt_vectoring_info & VECTORING_INFO_TYPE_MASK;
10591
10592 switch (type) {
10593 case INTR_TYPE_NMI_INTR:
10594 vcpu->arch.nmi_injected = true;
10595 /*
10596 * SDM 3: 27.7.1.2 (September 2008)
10597 * Clear bit "block by NMI" before VM entry if a NMI
10598 * delivery faulted.
10599 */
10600 vmx_set_nmi_mask(vcpu, false);
10601 break;
10602 case INTR_TYPE_SOFT_EXCEPTION:
10603 vcpu->arch.event_exit_inst_len = vmcs_read32(instr_len_field);
10604 /* fall through */
10605 case INTR_TYPE_HARD_EXCEPTION:
10606 if (idt_vectoring_info & VECTORING_INFO_DELIVER_CODE_MASK) {
10607 u32 err = vmcs_read32(error_code_field);
10608 kvm_requeue_exception_e(vcpu, vector, err);
10609 } else
10610 kvm_requeue_exception(vcpu, vector);
10611 break;
10612 case INTR_TYPE_SOFT_INTR:
10613 vcpu->arch.event_exit_inst_len = vmcs_read32(instr_len_field);
10614 /* fall through */
10615 case INTR_TYPE_EXT_INTR:
10616 kvm_queue_interrupt(vcpu, vector, type == INTR_TYPE_SOFT_INTR);
10617 break;
10618 default:
10619 break;
10620 }
10621 }
10622
vmx_complete_interrupts(struct vcpu_vmx * vmx)10623 static void vmx_complete_interrupts(struct vcpu_vmx *vmx)
10624 {
10625 __vmx_complete_interrupts(&vmx->vcpu, vmx->idt_vectoring_info,
10626 VM_EXIT_INSTRUCTION_LEN,
10627 IDT_VECTORING_ERROR_CODE);
10628 }
10629
vmx_cancel_injection(struct kvm_vcpu * vcpu)10630 static void vmx_cancel_injection(struct kvm_vcpu *vcpu)
10631 {
10632 __vmx_complete_interrupts(vcpu,
10633 vmcs_read32(VM_ENTRY_INTR_INFO_FIELD),
10634 VM_ENTRY_INSTRUCTION_LEN,
10635 VM_ENTRY_EXCEPTION_ERROR_CODE);
10636
10637 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0);
10638 }
10639
atomic_switch_perf_msrs(struct vcpu_vmx * vmx)10640 static void atomic_switch_perf_msrs(struct vcpu_vmx *vmx)
10641 {
10642 int i, nr_msrs;
10643 struct perf_guest_switch_msr *msrs;
10644
10645 msrs = perf_guest_get_msrs(&nr_msrs);
10646
10647 if (!msrs)
10648 return;
10649
10650 for (i = 0; i < nr_msrs; i++)
10651 if (msrs[i].host == msrs[i].guest)
10652 clear_atomic_switch_msr(vmx, msrs[i].msr);
10653 else
10654 add_atomic_switch_msr(vmx, msrs[i].msr, msrs[i].guest,
10655 msrs[i].host, false);
10656 }
10657
vmx_arm_hv_timer(struct vcpu_vmx * vmx,u32 val)10658 static void vmx_arm_hv_timer(struct vcpu_vmx *vmx, u32 val)
10659 {
10660 vmcs_write32(VMX_PREEMPTION_TIMER_VALUE, val);
10661 if (!vmx->loaded_vmcs->hv_timer_armed)
10662 vmcs_set_bits(PIN_BASED_VM_EXEC_CONTROL,
10663 PIN_BASED_VMX_PREEMPTION_TIMER);
10664 vmx->loaded_vmcs->hv_timer_armed = true;
10665 }
10666
vmx_update_hv_timer(struct kvm_vcpu * vcpu)10667 static void vmx_update_hv_timer(struct kvm_vcpu *vcpu)
10668 {
10669 struct vcpu_vmx *vmx = to_vmx(vcpu);
10670 u64 tscl;
10671 u32 delta_tsc;
10672
10673 if (vmx->req_immediate_exit) {
10674 vmx_arm_hv_timer(vmx, 0);
10675 return;
10676 }
10677
10678 if (vmx->hv_deadline_tsc != -1) {
10679 tscl = rdtsc();
10680 if (vmx->hv_deadline_tsc > tscl)
10681 /* set_hv_timer ensures the delta fits in 32-bits */
10682 delta_tsc = (u32)((vmx->hv_deadline_tsc - tscl) >>
10683 cpu_preemption_timer_multi);
10684 else
10685 delta_tsc = 0;
10686
10687 vmx_arm_hv_timer(vmx, delta_tsc);
10688 return;
10689 }
10690
10691 if (vmx->loaded_vmcs->hv_timer_armed)
10692 vmcs_clear_bits(PIN_BASED_VM_EXEC_CONTROL,
10693 PIN_BASED_VMX_PREEMPTION_TIMER);
10694 vmx->loaded_vmcs->hv_timer_armed = false;
10695 }
10696
vmx_vcpu_run(struct kvm_vcpu * vcpu)10697 static void __noclone vmx_vcpu_run(struct kvm_vcpu *vcpu)
10698 {
10699 struct vcpu_vmx *vmx = to_vmx(vcpu);
10700 unsigned long cr3, cr4, evmcs_rsp;
10701
10702 /* Record the guest's net vcpu time for enforced NMI injections. */
10703 if (unlikely(!enable_vnmi &&
10704 vmx->loaded_vmcs->soft_vnmi_blocked))
10705 vmx->loaded_vmcs->entry_time = ktime_get();
10706
10707 /* Don't enter VMX if guest state is invalid, let the exit handler
10708 start emulation until we arrive back to a valid state */
10709 if (vmx->emulation_required)
10710 return;
10711
10712 if (vmx->ple_window_dirty) {
10713 vmx->ple_window_dirty = false;
10714 vmcs_write32(PLE_WINDOW, vmx->ple_window);
10715 }
10716
10717 if (vmx->nested.sync_shadow_vmcs) {
10718 copy_vmcs12_to_shadow(vmx);
10719 vmx->nested.sync_shadow_vmcs = false;
10720 }
10721
10722 if (test_bit(VCPU_REGS_RSP, (unsigned long *)&vcpu->arch.regs_dirty))
10723 vmcs_writel(GUEST_RSP, vcpu->arch.regs[VCPU_REGS_RSP]);
10724 if (test_bit(VCPU_REGS_RIP, (unsigned long *)&vcpu->arch.regs_dirty))
10725 vmcs_writel(GUEST_RIP, vcpu->arch.regs[VCPU_REGS_RIP]);
10726
10727 cr3 = __get_current_cr3_fast();
10728 if (unlikely(cr3 != vmx->loaded_vmcs->host_state.cr3)) {
10729 vmcs_writel(HOST_CR3, cr3);
10730 vmx->loaded_vmcs->host_state.cr3 = cr3;
10731 }
10732
10733 cr4 = cr4_read_shadow();
10734 if (unlikely(cr4 != vmx->loaded_vmcs->host_state.cr4)) {
10735 vmcs_writel(HOST_CR4, cr4);
10736 vmx->loaded_vmcs->host_state.cr4 = cr4;
10737 }
10738
10739 /* When single-stepping over STI and MOV SS, we must clear the
10740 * corresponding interruptibility bits in the guest state. Otherwise
10741 * vmentry fails as it then expects bit 14 (BS) in pending debug
10742 * exceptions being set, but that's not correct for the guest debugging
10743 * case. */
10744 if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP)
10745 vmx_set_interrupt_shadow(vcpu, 0);
10746
10747 kvm_load_guest_xcr0(vcpu);
10748
10749 if (static_cpu_has(X86_FEATURE_PKU) &&
10750 kvm_read_cr4_bits(vcpu, X86_CR4_PKE) &&
10751 vcpu->arch.pkru != vmx->host_pkru)
10752 __write_pkru(vcpu->arch.pkru);
10753
10754 atomic_switch_perf_msrs(vmx);
10755
10756 vmx_update_hv_timer(vcpu);
10757
10758 /*
10759 * If this vCPU has touched SPEC_CTRL, restore the guest's value if
10760 * it's non-zero. Since vmentry is serialising on affected CPUs, there
10761 * is no need to worry about the conditional branch over the wrmsr
10762 * being speculatively taken.
10763 */
10764 x86_spec_ctrl_set_guest(vmx->spec_ctrl, 0);
10765
10766 vmx->__launched = vmx->loaded_vmcs->launched;
10767
10768 evmcs_rsp = static_branch_unlikely(&enable_evmcs) ?
10769 (unsigned long)¤t_evmcs->host_rsp : 0;
10770
10771 /* L1D Flush includes CPU buffer clear to mitigate MDS */
10772 if (static_branch_unlikely(&vmx_l1d_should_flush))
10773 vmx_l1d_flush(vcpu);
10774 else if (static_branch_unlikely(&mds_user_clear))
10775 mds_clear_cpu_buffers();
10776
10777 asm volatile (
10778 /* Store host registers */
10779 "push %%" _ASM_DX "; push %%" _ASM_BP ";"
10780 "push %%" _ASM_CX " \n\t" /* placeholder for guest rcx */
10781 "push %%" _ASM_CX " \n\t"
10782 "cmp %%" _ASM_SP ", %c[host_rsp](%%" _ASM_CX ") \n\t"
10783 "je 1f \n\t"
10784 "mov %%" _ASM_SP ", %c[host_rsp](%%" _ASM_CX ") \n\t"
10785 /* Avoid VMWRITE when Enlightened VMCS is in use */
10786 "test %%" _ASM_SI ", %%" _ASM_SI " \n\t"
10787 "jz 2f \n\t"
10788 "mov %%" _ASM_SP ", (%%" _ASM_SI ") \n\t"
10789 "jmp 1f \n\t"
10790 "2: \n\t"
10791 __ex(ASM_VMX_VMWRITE_RSP_RDX) "\n\t"
10792 "1: \n\t"
10793 /* Reload cr2 if changed */
10794 "mov %c[cr2](%%" _ASM_CX "), %%" _ASM_AX " \n\t"
10795 "mov %%cr2, %%" _ASM_DX " \n\t"
10796 "cmp %%" _ASM_AX ", %%" _ASM_DX " \n\t"
10797 "je 3f \n\t"
10798 "mov %%" _ASM_AX", %%cr2 \n\t"
10799 "3: \n\t"
10800 /* Check if vmlaunch of vmresume is needed */
10801 "cmpb $0, %c[launched](%%" _ASM_CX ") \n\t"
10802 /* Load guest registers. Don't clobber flags. */
10803 "mov %c[rax](%%" _ASM_CX "), %%" _ASM_AX " \n\t"
10804 "mov %c[rbx](%%" _ASM_CX "), %%" _ASM_BX " \n\t"
10805 "mov %c[rdx](%%" _ASM_CX "), %%" _ASM_DX " \n\t"
10806 "mov %c[rsi](%%" _ASM_CX "), %%" _ASM_SI " \n\t"
10807 "mov %c[rdi](%%" _ASM_CX "), %%" _ASM_DI " \n\t"
10808 "mov %c[rbp](%%" _ASM_CX "), %%" _ASM_BP " \n\t"
10809 #ifdef CONFIG_X86_64
10810 "mov %c[r8](%%" _ASM_CX "), %%r8 \n\t"
10811 "mov %c[r9](%%" _ASM_CX "), %%r9 \n\t"
10812 "mov %c[r10](%%" _ASM_CX "), %%r10 \n\t"
10813 "mov %c[r11](%%" _ASM_CX "), %%r11 \n\t"
10814 "mov %c[r12](%%" _ASM_CX "), %%r12 \n\t"
10815 "mov %c[r13](%%" _ASM_CX "), %%r13 \n\t"
10816 "mov %c[r14](%%" _ASM_CX "), %%r14 \n\t"
10817 "mov %c[r15](%%" _ASM_CX "), %%r15 \n\t"
10818 #endif
10819 /* Load guest RCX. This kills the vmx_vcpu pointer! */
10820 "mov %c[rcx](%%" _ASM_CX "), %%" _ASM_CX " \n\t"
10821
10822 /* Enter guest mode */
10823 "jne 1f \n\t"
10824 __ex(ASM_VMX_VMLAUNCH) "\n\t"
10825 "jmp 2f \n\t"
10826 "1: " __ex(ASM_VMX_VMRESUME) "\n\t"
10827 "2: "
10828
10829 /* Save guest's RCX to the stack placeholder (see above) */
10830 "mov %%" _ASM_CX ", %c[wordsize](%%" _ASM_SP ") \n\t"
10831
10832 /* Load host's RCX, i.e. the vmx_vcpu pointer */
10833 "pop %%" _ASM_CX " \n\t"
10834
10835 /* Set vmx->fail based on EFLAGS.{CF,ZF} */
10836 "setbe %c[fail](%%" _ASM_CX ")\n\t"
10837
10838 /* Save all guest registers, including RCX from the stack */
10839 "mov %%" _ASM_AX ", %c[rax](%%" _ASM_CX ") \n\t"
10840 "mov %%" _ASM_BX ", %c[rbx](%%" _ASM_CX ") \n\t"
10841 __ASM_SIZE(pop) " %c[rcx](%%" _ASM_CX ") \n\t"
10842 "mov %%" _ASM_DX ", %c[rdx](%%" _ASM_CX ") \n\t"
10843 "mov %%" _ASM_SI ", %c[rsi](%%" _ASM_CX ") \n\t"
10844 "mov %%" _ASM_DI ", %c[rdi](%%" _ASM_CX ") \n\t"
10845 "mov %%" _ASM_BP ", %c[rbp](%%" _ASM_CX ") \n\t"
10846 #ifdef CONFIG_X86_64
10847 "mov %%r8, %c[r8](%%" _ASM_CX ") \n\t"
10848 "mov %%r9, %c[r9](%%" _ASM_CX ") \n\t"
10849 "mov %%r10, %c[r10](%%" _ASM_CX ") \n\t"
10850 "mov %%r11, %c[r11](%%" _ASM_CX ") \n\t"
10851 "mov %%r12, %c[r12](%%" _ASM_CX ") \n\t"
10852 "mov %%r13, %c[r13](%%" _ASM_CX ") \n\t"
10853 "mov %%r14, %c[r14](%%" _ASM_CX ") \n\t"
10854 "mov %%r15, %c[r15](%%" _ASM_CX ") \n\t"
10855
10856 /*
10857 * Clear all general purpose registers (except RSP, which is loaded by
10858 * the CPU during VM-Exit) to prevent speculative use of the guest's
10859 * values, even those that are saved/loaded via the stack. In theory,
10860 * an L1 cache miss when restoring registers could lead to speculative
10861 * execution with the guest's values. Zeroing XORs are dirt cheap,
10862 * i.e. the extra paranoia is essentially free.
10863 */
10864 "xor %%r8d, %%r8d \n\t"
10865 "xor %%r9d, %%r9d \n\t"
10866 "xor %%r10d, %%r10d \n\t"
10867 "xor %%r11d, %%r11d \n\t"
10868 "xor %%r12d, %%r12d \n\t"
10869 "xor %%r13d, %%r13d \n\t"
10870 "xor %%r14d, %%r14d \n\t"
10871 "xor %%r15d, %%r15d \n\t"
10872 #endif
10873 "mov %%cr2, %%" _ASM_AX " \n\t"
10874 "mov %%" _ASM_AX ", %c[cr2](%%" _ASM_CX ") \n\t"
10875
10876 "xor %%eax, %%eax \n\t"
10877 "xor %%ebx, %%ebx \n\t"
10878 "xor %%ecx, %%ecx \n\t"
10879 "xor %%edx, %%edx \n\t"
10880 "xor %%esi, %%esi \n\t"
10881 "xor %%edi, %%edi \n\t"
10882 "xor %%ebp, %%ebp \n\t"
10883 "pop %%" _ASM_BP "; pop %%" _ASM_DX " \n\t"
10884 ".pushsection .rodata \n\t"
10885 ".global vmx_return \n\t"
10886 "vmx_return: " _ASM_PTR " 2b \n\t"
10887 ".popsection"
10888 : "=c"((int){0}), "=d"((int){0}), "=S"((int){0})
10889 : "c"(vmx), "d"((unsigned long)HOST_RSP), "S"(evmcs_rsp),
10890 [launched]"i"(offsetof(struct vcpu_vmx, __launched)),
10891 [fail]"i"(offsetof(struct vcpu_vmx, fail)),
10892 [host_rsp]"i"(offsetof(struct vcpu_vmx, host_rsp)),
10893 [rax]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RAX])),
10894 [rbx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBX])),
10895 [rcx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RCX])),
10896 [rdx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDX])),
10897 [rsi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RSI])),
10898 [rdi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDI])),
10899 [rbp]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBP])),
10900 #ifdef CONFIG_X86_64
10901 [r8]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R8])),
10902 [r9]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R9])),
10903 [r10]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R10])),
10904 [r11]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R11])),
10905 [r12]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R12])),
10906 [r13]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R13])),
10907 [r14]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R14])),
10908 [r15]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R15])),
10909 #endif
10910 [cr2]"i"(offsetof(struct vcpu_vmx, vcpu.arch.cr2)),
10911 [wordsize]"i"(sizeof(ulong))
10912 : "cc", "memory"
10913 #ifdef CONFIG_X86_64
10914 , "rax", "rbx", "rdi"
10915 , "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15"
10916 #else
10917 , "eax", "ebx", "edi"
10918 #endif
10919 );
10920
10921 /*
10922 * We do not use IBRS in the kernel. If this vCPU has used the
10923 * SPEC_CTRL MSR it may have left it on; save the value and
10924 * turn it off. This is much more efficient than blindly adding
10925 * it to the atomic save/restore list. Especially as the former
10926 * (Saving guest MSRs on vmexit) doesn't even exist in KVM.
10927 *
10928 * For non-nested case:
10929 * If the L01 MSR bitmap does not intercept the MSR, then we need to
10930 * save it.
10931 *
10932 * For nested case:
10933 * If the L02 MSR bitmap does not intercept the MSR, then we need to
10934 * save it.
10935 */
10936 if (unlikely(!msr_write_intercepted(vcpu, MSR_IA32_SPEC_CTRL)))
10937 vmx->spec_ctrl = native_read_msr(MSR_IA32_SPEC_CTRL);
10938
10939 x86_spec_ctrl_restore_host(vmx->spec_ctrl, 0);
10940
10941 /* Eliminate branch target predictions from guest mode */
10942 vmexit_fill_RSB();
10943
10944 /* All fields are clean at this point */
10945 if (static_branch_unlikely(&enable_evmcs))
10946 current_evmcs->hv_clean_fields |=
10947 HV_VMX_ENLIGHTENED_CLEAN_FIELD_ALL;
10948
10949 /* MSR_IA32_DEBUGCTLMSR is zeroed on vmexit. Restore it if needed */
10950 if (vmx->host_debugctlmsr)
10951 update_debugctlmsr(vmx->host_debugctlmsr);
10952
10953 #ifndef CONFIG_X86_64
10954 /*
10955 * The sysexit path does not restore ds/es, so we must set them to
10956 * a reasonable value ourselves.
10957 *
10958 * We can't defer this to vmx_prepare_switch_to_host() since that
10959 * function may be executed in interrupt context, which saves and
10960 * restore segments around it, nullifying its effect.
10961 */
10962 loadsegment(ds, __USER_DS);
10963 loadsegment(es, __USER_DS);
10964 #endif
10965
10966 vcpu->arch.regs_avail = ~((1 << VCPU_REGS_RIP) | (1 << VCPU_REGS_RSP)
10967 | (1 << VCPU_EXREG_RFLAGS)
10968 | (1 << VCPU_EXREG_PDPTR)
10969 | (1 << VCPU_EXREG_SEGMENTS)
10970 | (1 << VCPU_EXREG_CR3));
10971 vcpu->arch.regs_dirty = 0;
10972
10973 /*
10974 * eager fpu is enabled if PKEY is supported and CR4 is switched
10975 * back on host, so it is safe to read guest PKRU from current
10976 * XSAVE.
10977 */
10978 if (static_cpu_has(X86_FEATURE_PKU) &&
10979 kvm_read_cr4_bits(vcpu, X86_CR4_PKE)) {
10980 vcpu->arch.pkru = __read_pkru();
10981 if (vcpu->arch.pkru != vmx->host_pkru)
10982 __write_pkru(vmx->host_pkru);
10983 }
10984
10985 kvm_put_guest_xcr0(vcpu);
10986
10987 vmx->nested.nested_run_pending = 0;
10988 vmx->idt_vectoring_info = 0;
10989
10990 vmx->exit_reason = vmx->fail ? 0xdead : vmcs_read32(VM_EXIT_REASON);
10991 if ((u16)vmx->exit_reason == EXIT_REASON_MCE_DURING_VMENTRY)
10992 kvm_machine_check();
10993
10994 if (vmx->fail || (vmx->exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY))
10995 return;
10996
10997 vmx->loaded_vmcs->launched = 1;
10998 vmx->idt_vectoring_info = vmcs_read32(IDT_VECTORING_INFO_FIELD);
10999
11000 vmx_complete_atomic_exit(vmx);
11001 vmx_recover_nmi_blocking(vmx);
11002 vmx_complete_interrupts(vmx);
11003 }
11004 STACK_FRAME_NON_STANDARD(vmx_vcpu_run);
11005
vmx_vm_alloc(void)11006 static struct kvm *vmx_vm_alloc(void)
11007 {
11008 struct kvm_vmx *kvm_vmx = vzalloc(sizeof(struct kvm_vmx));
11009
11010 if (!kvm_vmx)
11011 return NULL;
11012
11013 return &kvm_vmx->kvm;
11014 }
11015
vmx_vm_free(struct kvm * kvm)11016 static void vmx_vm_free(struct kvm *kvm)
11017 {
11018 vfree(to_kvm_vmx(kvm));
11019 }
11020
vmx_switch_vmcs(struct kvm_vcpu * vcpu,struct loaded_vmcs * vmcs)11021 static void vmx_switch_vmcs(struct kvm_vcpu *vcpu, struct loaded_vmcs *vmcs)
11022 {
11023 struct vcpu_vmx *vmx = to_vmx(vcpu);
11024 int cpu;
11025
11026 if (vmx->loaded_vmcs == vmcs)
11027 return;
11028
11029 cpu = get_cpu();
11030 vmx_vcpu_put(vcpu);
11031 vmx->loaded_vmcs = vmcs;
11032 vmx_vcpu_load(vcpu, cpu);
11033 put_cpu();
11034 }
11035
11036 /*
11037 * Ensure that the current vmcs of the logical processor is the
11038 * vmcs01 of the vcpu before calling free_nested().
11039 */
vmx_free_vcpu_nested(struct kvm_vcpu * vcpu)11040 static void vmx_free_vcpu_nested(struct kvm_vcpu *vcpu)
11041 {
11042 struct vcpu_vmx *vmx = to_vmx(vcpu);
11043
11044 vcpu_load(vcpu);
11045 vmx_switch_vmcs(vcpu, &vmx->vmcs01);
11046 free_nested(vmx);
11047 vcpu_put(vcpu);
11048 }
11049
vmx_free_vcpu(struct kvm_vcpu * vcpu)11050 static void vmx_free_vcpu(struct kvm_vcpu *vcpu)
11051 {
11052 struct vcpu_vmx *vmx = to_vmx(vcpu);
11053
11054 if (enable_pml)
11055 vmx_destroy_pml_buffer(vmx);
11056 free_vpid(vmx->vpid);
11057 leave_guest_mode(vcpu);
11058 vmx_free_vcpu_nested(vcpu);
11059 free_loaded_vmcs(vmx->loaded_vmcs);
11060 kfree(vmx->guest_msrs);
11061 kvm_vcpu_uninit(vcpu);
11062 kmem_cache_free(kvm_vcpu_cache, vmx);
11063 }
11064
vmx_create_vcpu(struct kvm * kvm,unsigned int id)11065 static struct kvm_vcpu *vmx_create_vcpu(struct kvm *kvm, unsigned int id)
11066 {
11067 int err;
11068 struct vcpu_vmx *vmx = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
11069 unsigned long *msr_bitmap;
11070 int cpu;
11071
11072 if (!vmx)
11073 return ERR_PTR(-ENOMEM);
11074
11075 vmx->vpid = allocate_vpid();
11076
11077 err = kvm_vcpu_init(&vmx->vcpu, kvm, id);
11078 if (err)
11079 goto free_vcpu;
11080
11081 err = -ENOMEM;
11082
11083 /*
11084 * If PML is turned on, failure on enabling PML just results in failure
11085 * of creating the vcpu, therefore we can simplify PML logic (by
11086 * avoiding dealing with cases, such as enabling PML partially on vcpus
11087 * for the guest, etc.
11088 */
11089 if (enable_pml) {
11090 vmx->pml_pg = alloc_page(GFP_KERNEL | __GFP_ZERO);
11091 if (!vmx->pml_pg)
11092 goto uninit_vcpu;
11093 }
11094
11095 vmx->guest_msrs = kmalloc(PAGE_SIZE, GFP_KERNEL);
11096 BUILD_BUG_ON(ARRAY_SIZE(vmx_msr_index) * sizeof(vmx->guest_msrs[0])
11097 > PAGE_SIZE);
11098
11099 if (!vmx->guest_msrs)
11100 goto free_pml;
11101
11102 err = alloc_loaded_vmcs(&vmx->vmcs01);
11103 if (err < 0)
11104 goto free_msrs;
11105
11106 msr_bitmap = vmx->vmcs01.msr_bitmap;
11107 vmx_disable_intercept_for_msr(msr_bitmap, MSR_FS_BASE, MSR_TYPE_RW);
11108 vmx_disable_intercept_for_msr(msr_bitmap, MSR_GS_BASE, MSR_TYPE_RW);
11109 vmx_disable_intercept_for_msr(msr_bitmap, MSR_KERNEL_GS_BASE, MSR_TYPE_RW);
11110 vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_SYSENTER_CS, MSR_TYPE_RW);
11111 vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_SYSENTER_ESP, MSR_TYPE_RW);
11112 vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_SYSENTER_EIP, MSR_TYPE_RW);
11113 vmx->msr_bitmap_mode = 0;
11114
11115 vmx->loaded_vmcs = &vmx->vmcs01;
11116 cpu = get_cpu();
11117 vmx_vcpu_load(&vmx->vcpu, cpu);
11118 vmx->vcpu.cpu = cpu;
11119 vmx_vcpu_setup(vmx);
11120 vmx_vcpu_put(&vmx->vcpu);
11121 put_cpu();
11122 if (cpu_need_virtualize_apic_accesses(&vmx->vcpu)) {
11123 err = alloc_apic_access_page(kvm);
11124 if (err)
11125 goto free_vmcs;
11126 }
11127
11128 if (enable_ept && !enable_unrestricted_guest) {
11129 err = init_rmode_identity_map(kvm);
11130 if (err)
11131 goto free_vmcs;
11132 }
11133
11134 if (nested)
11135 nested_vmx_setup_ctls_msrs(&vmx->nested.msrs,
11136 kvm_vcpu_apicv_active(&vmx->vcpu));
11137
11138 vmx->nested.posted_intr_nv = -1;
11139 vmx->nested.current_vmptr = -1ull;
11140
11141 vmx->msr_ia32_feature_control_valid_bits = FEATURE_CONTROL_LOCKED;
11142
11143 /*
11144 * Enforce invariant: pi_desc.nv is always either POSTED_INTR_VECTOR
11145 * or POSTED_INTR_WAKEUP_VECTOR.
11146 */
11147 vmx->pi_desc.nv = POSTED_INTR_VECTOR;
11148 vmx->pi_desc.sn = 1;
11149
11150 return &vmx->vcpu;
11151
11152 free_vmcs:
11153 free_loaded_vmcs(vmx->loaded_vmcs);
11154 free_msrs:
11155 kfree(vmx->guest_msrs);
11156 free_pml:
11157 vmx_destroy_pml_buffer(vmx);
11158 uninit_vcpu:
11159 kvm_vcpu_uninit(&vmx->vcpu);
11160 free_vcpu:
11161 free_vpid(vmx->vpid);
11162 kmem_cache_free(kvm_vcpu_cache, vmx);
11163 return ERR_PTR(err);
11164 }
11165
11166 #define L1TF_MSG_SMT "L1TF CPU bug present and SMT on, data leak possible. See CVE-2018-3646 and https://www.kernel.org/doc/html/latest/admin-guide/hw-vuln/l1tf.html for details.\n"
11167 #define L1TF_MSG_L1D "L1TF CPU bug present and virtualization mitigation disabled, data leak possible. See CVE-2018-3646 and https://www.kernel.org/doc/html/latest/admin-guide/hw-vuln/l1tf.html for details.\n"
11168
vmx_vm_init(struct kvm * kvm)11169 static int vmx_vm_init(struct kvm *kvm)
11170 {
11171 spin_lock_init(&to_kvm_vmx(kvm)->ept_pointer_lock);
11172
11173 if (!ple_gap)
11174 kvm->arch.pause_in_guest = true;
11175
11176 if (boot_cpu_has(X86_BUG_L1TF) && enable_ept) {
11177 switch (l1tf_mitigation) {
11178 case L1TF_MITIGATION_OFF:
11179 case L1TF_MITIGATION_FLUSH_NOWARN:
11180 /* 'I explicitly don't care' is set */
11181 break;
11182 case L1TF_MITIGATION_FLUSH:
11183 case L1TF_MITIGATION_FLUSH_NOSMT:
11184 case L1TF_MITIGATION_FULL:
11185 /*
11186 * Warn upon starting the first VM in a potentially
11187 * insecure environment.
11188 */
11189 if (sched_smt_active())
11190 pr_warn_once(L1TF_MSG_SMT);
11191 if (l1tf_vmx_mitigation == VMENTER_L1D_FLUSH_NEVER)
11192 pr_warn_once(L1TF_MSG_L1D);
11193 break;
11194 case L1TF_MITIGATION_FULL_FORCE:
11195 /* Flush is enforced */
11196 break;
11197 }
11198 }
11199 return 0;
11200 }
11201
vmx_check_processor_compat(void * rtn)11202 static void __init vmx_check_processor_compat(void *rtn)
11203 {
11204 struct vmcs_config vmcs_conf;
11205
11206 *(int *)rtn = 0;
11207 if (setup_vmcs_config(&vmcs_conf) < 0)
11208 *(int *)rtn = -EIO;
11209 nested_vmx_setup_ctls_msrs(&vmcs_conf.nested, enable_apicv);
11210 if (memcmp(&vmcs_config, &vmcs_conf, sizeof(struct vmcs_config)) != 0) {
11211 printk(KERN_ERR "kvm: CPU %d feature inconsistency!\n",
11212 smp_processor_id());
11213 *(int *)rtn = -EIO;
11214 }
11215 }
11216
vmx_get_mt_mask(struct kvm_vcpu * vcpu,gfn_t gfn,bool is_mmio)11217 static u64 vmx_get_mt_mask(struct kvm_vcpu *vcpu, gfn_t gfn, bool is_mmio)
11218 {
11219 u8 cache;
11220 u64 ipat = 0;
11221
11222 /* For VT-d and EPT combination
11223 * 1. MMIO: always map as UC
11224 * 2. EPT with VT-d:
11225 * a. VT-d without snooping control feature: can't guarantee the
11226 * result, try to trust guest.
11227 * b. VT-d with snooping control feature: snooping control feature of
11228 * VT-d engine can guarantee the cache correctness. Just set it
11229 * to WB to keep consistent with host. So the same as item 3.
11230 * 3. EPT without VT-d: always map as WB and set IPAT=1 to keep
11231 * consistent with host MTRR
11232 */
11233 if (is_mmio) {
11234 cache = MTRR_TYPE_UNCACHABLE;
11235 goto exit;
11236 }
11237
11238 if (!kvm_arch_has_noncoherent_dma(vcpu->kvm)) {
11239 ipat = VMX_EPT_IPAT_BIT;
11240 cache = MTRR_TYPE_WRBACK;
11241 goto exit;
11242 }
11243
11244 if (kvm_read_cr0(vcpu) & X86_CR0_CD) {
11245 ipat = VMX_EPT_IPAT_BIT;
11246 if (kvm_check_has_quirk(vcpu->kvm, KVM_X86_QUIRK_CD_NW_CLEARED))
11247 cache = MTRR_TYPE_WRBACK;
11248 else
11249 cache = MTRR_TYPE_UNCACHABLE;
11250 goto exit;
11251 }
11252
11253 cache = kvm_mtrr_get_guest_memory_type(vcpu, gfn);
11254
11255 exit:
11256 return (cache << VMX_EPT_MT_EPTE_SHIFT) | ipat;
11257 }
11258
vmx_get_lpage_level(void)11259 static int vmx_get_lpage_level(void)
11260 {
11261 if (enable_ept && !cpu_has_vmx_ept_1g_page())
11262 return PT_DIRECTORY_LEVEL;
11263 else
11264 /* For shadow and EPT supported 1GB page */
11265 return PT_PDPE_LEVEL;
11266 }
11267
vmcs_set_secondary_exec_control(u32 new_ctl)11268 static void vmcs_set_secondary_exec_control(u32 new_ctl)
11269 {
11270 /*
11271 * These bits in the secondary execution controls field
11272 * are dynamic, the others are mostly based on the hypervisor
11273 * architecture and the guest's CPUID. Do not touch the
11274 * dynamic bits.
11275 */
11276 u32 mask =
11277 SECONDARY_EXEC_SHADOW_VMCS |
11278 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
11279 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
11280 SECONDARY_EXEC_DESC;
11281
11282 u32 cur_ctl = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
11283
11284 vmcs_write32(SECONDARY_VM_EXEC_CONTROL,
11285 (new_ctl & ~mask) | (cur_ctl & mask));
11286 }
11287
11288 /*
11289 * Generate MSR_IA32_VMX_CR{0,4}_FIXED1 according to CPUID. Only set bits
11290 * (indicating "allowed-1") if they are supported in the guest's CPUID.
11291 */
nested_vmx_cr_fixed1_bits_update(struct kvm_vcpu * vcpu)11292 static void nested_vmx_cr_fixed1_bits_update(struct kvm_vcpu *vcpu)
11293 {
11294 struct vcpu_vmx *vmx = to_vmx(vcpu);
11295 struct kvm_cpuid_entry2 *entry;
11296
11297 vmx->nested.msrs.cr0_fixed1 = 0xffffffff;
11298 vmx->nested.msrs.cr4_fixed1 = X86_CR4_PCE;
11299
11300 #define cr4_fixed1_update(_cr4_mask, _reg, _cpuid_mask) do { \
11301 if (entry && (entry->_reg & (_cpuid_mask))) \
11302 vmx->nested.msrs.cr4_fixed1 |= (_cr4_mask); \
11303 } while (0)
11304
11305 entry = kvm_find_cpuid_entry(vcpu, 0x1, 0);
11306 cr4_fixed1_update(X86_CR4_VME, edx, bit(X86_FEATURE_VME));
11307 cr4_fixed1_update(X86_CR4_PVI, edx, bit(X86_FEATURE_VME));
11308 cr4_fixed1_update(X86_CR4_TSD, edx, bit(X86_FEATURE_TSC));
11309 cr4_fixed1_update(X86_CR4_DE, edx, bit(X86_FEATURE_DE));
11310 cr4_fixed1_update(X86_CR4_PSE, edx, bit(X86_FEATURE_PSE));
11311 cr4_fixed1_update(X86_CR4_PAE, edx, bit(X86_FEATURE_PAE));
11312 cr4_fixed1_update(X86_CR4_MCE, edx, bit(X86_FEATURE_MCE));
11313 cr4_fixed1_update(X86_CR4_PGE, edx, bit(X86_FEATURE_PGE));
11314 cr4_fixed1_update(X86_CR4_OSFXSR, edx, bit(X86_FEATURE_FXSR));
11315 cr4_fixed1_update(X86_CR4_OSXMMEXCPT, edx, bit(X86_FEATURE_XMM));
11316 cr4_fixed1_update(X86_CR4_VMXE, ecx, bit(X86_FEATURE_VMX));
11317 cr4_fixed1_update(X86_CR4_SMXE, ecx, bit(X86_FEATURE_SMX));
11318 cr4_fixed1_update(X86_CR4_PCIDE, ecx, bit(X86_FEATURE_PCID));
11319 cr4_fixed1_update(X86_CR4_OSXSAVE, ecx, bit(X86_FEATURE_XSAVE));
11320
11321 entry = kvm_find_cpuid_entry(vcpu, 0x7, 0);
11322 cr4_fixed1_update(X86_CR4_FSGSBASE, ebx, bit(X86_FEATURE_FSGSBASE));
11323 cr4_fixed1_update(X86_CR4_SMEP, ebx, bit(X86_FEATURE_SMEP));
11324 cr4_fixed1_update(X86_CR4_SMAP, ebx, bit(X86_FEATURE_SMAP));
11325 cr4_fixed1_update(X86_CR4_PKE, ecx, bit(X86_FEATURE_PKU));
11326 cr4_fixed1_update(X86_CR4_UMIP, ecx, bit(X86_FEATURE_UMIP));
11327
11328 #undef cr4_fixed1_update
11329 }
11330
nested_vmx_entry_exit_ctls_update(struct kvm_vcpu * vcpu)11331 static void nested_vmx_entry_exit_ctls_update(struct kvm_vcpu *vcpu)
11332 {
11333 struct vcpu_vmx *vmx = to_vmx(vcpu);
11334
11335 if (kvm_mpx_supported()) {
11336 bool mpx_enabled = guest_cpuid_has(vcpu, X86_FEATURE_MPX);
11337
11338 if (mpx_enabled) {
11339 vmx->nested.msrs.entry_ctls_high |= VM_ENTRY_LOAD_BNDCFGS;
11340 vmx->nested.msrs.exit_ctls_high |= VM_EXIT_CLEAR_BNDCFGS;
11341 } else {
11342 vmx->nested.msrs.entry_ctls_high &= ~VM_ENTRY_LOAD_BNDCFGS;
11343 vmx->nested.msrs.exit_ctls_high &= ~VM_EXIT_CLEAR_BNDCFGS;
11344 }
11345 }
11346 }
11347
vmx_cpuid_update(struct kvm_vcpu * vcpu)11348 static void vmx_cpuid_update(struct kvm_vcpu *vcpu)
11349 {
11350 struct vcpu_vmx *vmx = to_vmx(vcpu);
11351
11352 if (cpu_has_secondary_exec_ctrls()) {
11353 vmx_compute_secondary_exec_control(vmx);
11354 vmcs_set_secondary_exec_control(vmx->secondary_exec_control);
11355 }
11356
11357 if (nested_vmx_allowed(vcpu))
11358 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits |=
11359 FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
11360 else
11361 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits &=
11362 ~FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
11363
11364 if (nested_vmx_allowed(vcpu)) {
11365 nested_vmx_cr_fixed1_bits_update(vcpu);
11366 nested_vmx_entry_exit_ctls_update(vcpu);
11367 }
11368 }
11369
vmx_set_supported_cpuid(u32 func,struct kvm_cpuid_entry2 * entry)11370 static void vmx_set_supported_cpuid(u32 func, struct kvm_cpuid_entry2 *entry)
11371 {
11372 if (func == 1 && nested)
11373 entry->ecx |= bit(X86_FEATURE_VMX);
11374 }
11375
nested_ept_inject_page_fault(struct kvm_vcpu * vcpu,struct x86_exception * fault)11376 static void nested_ept_inject_page_fault(struct kvm_vcpu *vcpu,
11377 struct x86_exception *fault)
11378 {
11379 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
11380 struct vcpu_vmx *vmx = to_vmx(vcpu);
11381 u32 exit_reason;
11382 unsigned long exit_qualification = vcpu->arch.exit_qualification;
11383
11384 if (vmx->nested.pml_full) {
11385 exit_reason = EXIT_REASON_PML_FULL;
11386 vmx->nested.pml_full = false;
11387 exit_qualification &= INTR_INFO_UNBLOCK_NMI;
11388 } else if (fault->error_code & PFERR_RSVD_MASK)
11389 exit_reason = EXIT_REASON_EPT_MISCONFIG;
11390 else
11391 exit_reason = EXIT_REASON_EPT_VIOLATION;
11392
11393 nested_vmx_vmexit(vcpu, exit_reason, 0, exit_qualification);
11394 vmcs12->guest_physical_address = fault->address;
11395 }
11396
nested_ept_ad_enabled(struct kvm_vcpu * vcpu)11397 static bool nested_ept_ad_enabled(struct kvm_vcpu *vcpu)
11398 {
11399 return nested_ept_get_cr3(vcpu) & VMX_EPTP_AD_ENABLE_BIT;
11400 }
11401
11402 /* Callbacks for nested_ept_init_mmu_context: */
11403
nested_ept_get_cr3(struct kvm_vcpu * vcpu)11404 static unsigned long nested_ept_get_cr3(struct kvm_vcpu *vcpu)
11405 {
11406 /* return the page table to be shadowed - in our case, EPT12 */
11407 return get_vmcs12(vcpu)->ept_pointer;
11408 }
11409
nested_ept_init_mmu_context(struct kvm_vcpu * vcpu)11410 static int nested_ept_init_mmu_context(struct kvm_vcpu *vcpu)
11411 {
11412 WARN_ON(mmu_is_nested(vcpu));
11413 if (!valid_ept_address(vcpu, nested_ept_get_cr3(vcpu)))
11414 return 1;
11415
11416 kvm_init_shadow_ept_mmu(vcpu,
11417 to_vmx(vcpu)->nested.msrs.ept_caps &
11418 VMX_EPT_EXECUTE_ONLY_BIT,
11419 nested_ept_ad_enabled(vcpu),
11420 nested_ept_get_cr3(vcpu));
11421 vcpu->arch.mmu.set_cr3 = vmx_set_cr3;
11422 vcpu->arch.mmu.get_cr3 = nested_ept_get_cr3;
11423 vcpu->arch.mmu.inject_page_fault = nested_ept_inject_page_fault;
11424
11425 vcpu->arch.walk_mmu = &vcpu->arch.nested_mmu;
11426 return 0;
11427 }
11428
nested_ept_uninit_mmu_context(struct kvm_vcpu * vcpu)11429 static void nested_ept_uninit_mmu_context(struct kvm_vcpu *vcpu)
11430 {
11431 vcpu->arch.walk_mmu = &vcpu->arch.mmu;
11432 }
11433
nested_vmx_is_page_fault_vmexit(struct vmcs12 * vmcs12,u16 error_code)11434 static bool nested_vmx_is_page_fault_vmexit(struct vmcs12 *vmcs12,
11435 u16 error_code)
11436 {
11437 bool inequality, bit;
11438
11439 bit = (vmcs12->exception_bitmap & (1u << PF_VECTOR)) != 0;
11440 inequality =
11441 (error_code & vmcs12->page_fault_error_code_mask) !=
11442 vmcs12->page_fault_error_code_match;
11443 return inequality ^ bit;
11444 }
11445
vmx_inject_page_fault_nested(struct kvm_vcpu * vcpu,struct x86_exception * fault)11446 static void vmx_inject_page_fault_nested(struct kvm_vcpu *vcpu,
11447 struct x86_exception *fault)
11448 {
11449 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
11450
11451 WARN_ON(!is_guest_mode(vcpu));
11452
11453 if (nested_vmx_is_page_fault_vmexit(vmcs12, fault->error_code) &&
11454 !to_vmx(vcpu)->nested.nested_run_pending) {
11455 vmcs12->vm_exit_intr_error_code = fault->error_code;
11456 nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI,
11457 PF_VECTOR | INTR_TYPE_HARD_EXCEPTION |
11458 INTR_INFO_DELIVER_CODE_MASK | INTR_INFO_VALID_MASK,
11459 fault->address);
11460 } else {
11461 kvm_inject_page_fault(vcpu, fault);
11462 }
11463 }
11464
11465 static inline bool nested_vmx_prepare_msr_bitmap(struct kvm_vcpu *vcpu,
11466 struct vmcs12 *vmcs12);
11467
nested_get_vmcs12_pages(struct kvm_vcpu * vcpu)11468 static void nested_get_vmcs12_pages(struct kvm_vcpu *vcpu)
11469 {
11470 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
11471 struct vcpu_vmx *vmx = to_vmx(vcpu);
11472 struct page *page;
11473 u64 hpa;
11474
11475 if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) {
11476 /*
11477 * Translate L1 physical address to host physical
11478 * address for vmcs02. Keep the page pinned, so this
11479 * physical address remains valid. We keep a reference
11480 * to it so we can release it later.
11481 */
11482 if (vmx->nested.apic_access_page) { /* shouldn't happen */
11483 kvm_release_page_dirty(vmx->nested.apic_access_page);
11484 vmx->nested.apic_access_page = NULL;
11485 }
11486 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->apic_access_addr);
11487 /*
11488 * If translation failed, no matter: This feature asks
11489 * to exit when accessing the given address, and if it
11490 * can never be accessed, this feature won't do
11491 * anything anyway.
11492 */
11493 if (!is_error_page(page)) {
11494 vmx->nested.apic_access_page = page;
11495 hpa = page_to_phys(vmx->nested.apic_access_page);
11496 vmcs_write64(APIC_ACCESS_ADDR, hpa);
11497 } else {
11498 vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL,
11499 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES);
11500 }
11501 }
11502
11503 if (nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW)) {
11504 if (vmx->nested.virtual_apic_page) { /* shouldn't happen */
11505 kvm_release_page_dirty(vmx->nested.virtual_apic_page);
11506 vmx->nested.virtual_apic_page = NULL;
11507 }
11508 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->virtual_apic_page_addr);
11509
11510 /*
11511 * If translation failed, VM entry will fail because
11512 * prepare_vmcs02 set VIRTUAL_APIC_PAGE_ADDR to -1ull.
11513 * Failing the vm entry is _not_ what the processor
11514 * does but it's basically the only possibility we
11515 * have. We could still enter the guest if CR8 load
11516 * exits are enabled, CR8 store exits are enabled, and
11517 * virtualize APIC access is disabled; in this case
11518 * the processor would never use the TPR shadow and we
11519 * could simply clear the bit from the execution
11520 * control. But such a configuration is useless, so
11521 * let's keep the code simple.
11522 */
11523 if (!is_error_page(page)) {
11524 vmx->nested.virtual_apic_page = page;
11525 hpa = page_to_phys(vmx->nested.virtual_apic_page);
11526 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, hpa);
11527 }
11528 }
11529
11530 if (nested_cpu_has_posted_intr(vmcs12)) {
11531 if (vmx->nested.pi_desc_page) { /* shouldn't happen */
11532 kunmap(vmx->nested.pi_desc_page);
11533 kvm_release_page_dirty(vmx->nested.pi_desc_page);
11534 vmx->nested.pi_desc_page = NULL;
11535 vmx->nested.pi_desc = NULL;
11536 vmcs_write64(POSTED_INTR_DESC_ADDR, -1ull);
11537 }
11538 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->posted_intr_desc_addr);
11539 if (is_error_page(page))
11540 return;
11541 vmx->nested.pi_desc_page = page;
11542 vmx->nested.pi_desc = kmap(vmx->nested.pi_desc_page);
11543 vmx->nested.pi_desc =
11544 (struct pi_desc *)((void *)vmx->nested.pi_desc +
11545 (unsigned long)(vmcs12->posted_intr_desc_addr &
11546 (PAGE_SIZE - 1)));
11547 vmcs_write64(POSTED_INTR_DESC_ADDR,
11548 page_to_phys(vmx->nested.pi_desc_page) +
11549 (unsigned long)(vmcs12->posted_intr_desc_addr &
11550 (PAGE_SIZE - 1)));
11551 }
11552 if (nested_vmx_prepare_msr_bitmap(vcpu, vmcs12))
11553 vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL,
11554 CPU_BASED_USE_MSR_BITMAPS);
11555 else
11556 vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL,
11557 CPU_BASED_USE_MSR_BITMAPS);
11558 }
11559
vmx_start_preemption_timer(struct kvm_vcpu * vcpu)11560 static void vmx_start_preemption_timer(struct kvm_vcpu *vcpu)
11561 {
11562 u64 preemption_timeout = get_vmcs12(vcpu)->vmx_preemption_timer_value;
11563 struct vcpu_vmx *vmx = to_vmx(vcpu);
11564
11565 /*
11566 * A timer value of zero is architecturally guaranteed to cause
11567 * a VMExit prior to executing any instructions in the guest.
11568 */
11569 if (preemption_timeout == 0) {
11570 vmx_preemption_timer_fn(&vmx->nested.preemption_timer);
11571 return;
11572 }
11573
11574 if (vcpu->arch.virtual_tsc_khz == 0)
11575 return;
11576
11577 preemption_timeout <<= VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE;
11578 preemption_timeout *= 1000000;
11579 do_div(preemption_timeout, vcpu->arch.virtual_tsc_khz);
11580 hrtimer_start(&vmx->nested.preemption_timer,
11581 ns_to_ktime(preemption_timeout), HRTIMER_MODE_REL);
11582 }
11583
nested_vmx_check_io_bitmap_controls(struct kvm_vcpu * vcpu,struct vmcs12 * vmcs12)11584 static int nested_vmx_check_io_bitmap_controls(struct kvm_vcpu *vcpu,
11585 struct vmcs12 *vmcs12)
11586 {
11587 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_IO_BITMAPS))
11588 return 0;
11589
11590 if (!page_address_valid(vcpu, vmcs12->io_bitmap_a) ||
11591 !page_address_valid(vcpu, vmcs12->io_bitmap_b))
11592 return -EINVAL;
11593
11594 return 0;
11595 }
11596
nested_vmx_check_msr_bitmap_controls(struct kvm_vcpu * vcpu,struct vmcs12 * vmcs12)11597 static int nested_vmx_check_msr_bitmap_controls(struct kvm_vcpu *vcpu,
11598 struct vmcs12 *vmcs12)
11599 {
11600 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS))
11601 return 0;
11602
11603 if (!page_address_valid(vcpu, vmcs12->msr_bitmap))
11604 return -EINVAL;
11605
11606 return 0;
11607 }
11608
nested_vmx_check_tpr_shadow_controls(struct kvm_vcpu * vcpu,struct vmcs12 * vmcs12)11609 static int nested_vmx_check_tpr_shadow_controls(struct kvm_vcpu *vcpu,
11610 struct vmcs12 *vmcs12)
11611 {
11612 if (!nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW))
11613 return 0;
11614
11615 if (!page_address_valid(vcpu, vmcs12->virtual_apic_page_addr))
11616 return -EINVAL;
11617
11618 return 0;
11619 }
11620
enable_x2apic_msr_intercepts(unsigned long * msr_bitmap)11621 static inline void enable_x2apic_msr_intercepts(unsigned long *msr_bitmap) {
11622 int msr;
11623
11624 for (msr = 0x800; msr <= 0x8ff; msr += BITS_PER_LONG) {
11625 unsigned word = msr / BITS_PER_LONG;
11626
11627 msr_bitmap[word] = ~0;
11628 msr_bitmap[word + (0x800 / sizeof(long))] = ~0;
11629 }
11630 }
11631
11632 /*
11633 * Merge L0's and L1's MSR bitmap, return false to indicate that
11634 * we do not use the hardware.
11635 */
nested_vmx_prepare_msr_bitmap(struct kvm_vcpu * vcpu,struct vmcs12 * vmcs12)11636 static inline bool nested_vmx_prepare_msr_bitmap(struct kvm_vcpu *vcpu,
11637 struct vmcs12 *vmcs12)
11638 {
11639 int msr;
11640 struct page *page;
11641 unsigned long *msr_bitmap_l1;
11642 unsigned long *msr_bitmap_l0 = to_vmx(vcpu)->nested.vmcs02.msr_bitmap;
11643 /*
11644 * pred_cmd & spec_ctrl are trying to verify two things:
11645 *
11646 * 1. L0 gave a permission to L1 to actually passthrough the MSR. This
11647 * ensures that we do not accidentally generate an L02 MSR bitmap
11648 * from the L12 MSR bitmap that is too permissive.
11649 * 2. That L1 or L2s have actually used the MSR. This avoids
11650 * unnecessarily merging of the bitmap if the MSR is unused. This
11651 * works properly because we only update the L01 MSR bitmap lazily.
11652 * So even if L0 should pass L1 these MSRs, the L01 bitmap is only
11653 * updated to reflect this when L1 (or its L2s) actually write to
11654 * the MSR.
11655 */
11656 bool pred_cmd = !msr_write_intercepted_l01(vcpu, MSR_IA32_PRED_CMD);
11657 bool spec_ctrl = !msr_write_intercepted_l01(vcpu, MSR_IA32_SPEC_CTRL);
11658
11659 /* Nothing to do if the MSR bitmap is not in use. */
11660 if (!cpu_has_vmx_msr_bitmap() ||
11661 !nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS))
11662 return false;
11663
11664 if (!nested_cpu_has_virt_x2apic_mode(vmcs12) &&
11665 !pred_cmd && !spec_ctrl)
11666 return false;
11667
11668 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->msr_bitmap);
11669 if (is_error_page(page))
11670 return false;
11671
11672 msr_bitmap_l1 = (unsigned long *)kmap(page);
11673
11674 /*
11675 * To keep the control flow simple, pay eight 8-byte writes (sixteen
11676 * 4-byte writes on 32-bit systems) up front to enable intercepts for
11677 * the x2APIC MSR range and selectively disable them below.
11678 */
11679 enable_x2apic_msr_intercepts(msr_bitmap_l0);
11680
11681 if (nested_cpu_has_virt_x2apic_mode(vmcs12)) {
11682 if (nested_cpu_has_apic_reg_virt(vmcs12)) {
11683 /*
11684 * L0 need not intercept reads for MSRs between 0x800
11685 * and 0x8ff, it just lets the processor take the value
11686 * from the virtual-APIC page; take those 256 bits
11687 * directly from the L1 bitmap.
11688 */
11689 for (msr = 0x800; msr <= 0x8ff; msr += BITS_PER_LONG) {
11690 unsigned word = msr / BITS_PER_LONG;
11691
11692 msr_bitmap_l0[word] = msr_bitmap_l1[word];
11693 }
11694 }
11695
11696 nested_vmx_disable_intercept_for_msr(
11697 msr_bitmap_l1, msr_bitmap_l0,
11698 X2APIC_MSR(APIC_TASKPRI),
11699 MSR_TYPE_R | MSR_TYPE_W);
11700
11701 if (nested_cpu_has_vid(vmcs12)) {
11702 nested_vmx_disable_intercept_for_msr(
11703 msr_bitmap_l1, msr_bitmap_l0,
11704 X2APIC_MSR(APIC_EOI),
11705 MSR_TYPE_W);
11706 nested_vmx_disable_intercept_for_msr(
11707 msr_bitmap_l1, msr_bitmap_l0,
11708 X2APIC_MSR(APIC_SELF_IPI),
11709 MSR_TYPE_W);
11710 }
11711 }
11712
11713 if (spec_ctrl)
11714 nested_vmx_disable_intercept_for_msr(
11715 msr_bitmap_l1, msr_bitmap_l0,
11716 MSR_IA32_SPEC_CTRL,
11717 MSR_TYPE_R | MSR_TYPE_W);
11718
11719 if (pred_cmd)
11720 nested_vmx_disable_intercept_for_msr(
11721 msr_bitmap_l1, msr_bitmap_l0,
11722 MSR_IA32_PRED_CMD,
11723 MSR_TYPE_W);
11724
11725 kunmap(page);
11726 kvm_release_page_clean(page);
11727
11728 return true;
11729 }
11730
nested_cache_shadow_vmcs12(struct kvm_vcpu * vcpu,struct vmcs12 * vmcs12)11731 static void nested_cache_shadow_vmcs12(struct kvm_vcpu *vcpu,
11732 struct vmcs12 *vmcs12)
11733 {
11734 struct vmcs12 *shadow;
11735 struct page *page;
11736
11737 if (!nested_cpu_has_shadow_vmcs(vmcs12) ||
11738 vmcs12->vmcs_link_pointer == -1ull)
11739 return;
11740
11741 shadow = get_shadow_vmcs12(vcpu);
11742 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->vmcs_link_pointer);
11743
11744 memcpy(shadow, kmap(page), VMCS12_SIZE);
11745
11746 kunmap(page);
11747 kvm_release_page_clean(page);
11748 }
11749
nested_flush_cached_shadow_vmcs12(struct kvm_vcpu * vcpu,struct vmcs12 * vmcs12)11750 static void nested_flush_cached_shadow_vmcs12(struct kvm_vcpu *vcpu,
11751 struct vmcs12 *vmcs12)
11752 {
11753 struct vcpu_vmx *vmx = to_vmx(vcpu);
11754
11755 if (!nested_cpu_has_shadow_vmcs(vmcs12) ||
11756 vmcs12->vmcs_link_pointer == -1ull)
11757 return;
11758
11759 kvm_write_guest(vmx->vcpu.kvm, vmcs12->vmcs_link_pointer,
11760 get_shadow_vmcs12(vcpu), VMCS12_SIZE);
11761 }
11762
nested_vmx_check_apic_access_controls(struct kvm_vcpu * vcpu,struct vmcs12 * vmcs12)11763 static int nested_vmx_check_apic_access_controls(struct kvm_vcpu *vcpu,
11764 struct vmcs12 *vmcs12)
11765 {
11766 if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES) &&
11767 !page_address_valid(vcpu, vmcs12->apic_access_addr))
11768 return -EINVAL;
11769 else
11770 return 0;
11771 }
11772
nested_vmx_check_apicv_controls(struct kvm_vcpu * vcpu,struct vmcs12 * vmcs12)11773 static int nested_vmx_check_apicv_controls(struct kvm_vcpu *vcpu,
11774 struct vmcs12 *vmcs12)
11775 {
11776 if (!nested_cpu_has_virt_x2apic_mode(vmcs12) &&
11777 !nested_cpu_has_apic_reg_virt(vmcs12) &&
11778 !nested_cpu_has_vid(vmcs12) &&
11779 !nested_cpu_has_posted_intr(vmcs12))
11780 return 0;
11781
11782 /*
11783 * If virtualize x2apic mode is enabled,
11784 * virtualize apic access must be disabled.
11785 */
11786 if (nested_cpu_has_virt_x2apic_mode(vmcs12) &&
11787 nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES))
11788 return -EINVAL;
11789
11790 /*
11791 * If virtual interrupt delivery is enabled,
11792 * we must exit on external interrupts.
11793 */
11794 if (nested_cpu_has_vid(vmcs12) &&
11795 !nested_exit_on_intr(vcpu))
11796 return -EINVAL;
11797
11798 /*
11799 * bits 15:8 should be zero in posted_intr_nv,
11800 * the descriptor address has been already checked
11801 * in nested_get_vmcs12_pages.
11802 *
11803 * bits 5:0 of posted_intr_desc_addr should be zero.
11804 */
11805 if (nested_cpu_has_posted_intr(vmcs12) &&
11806 (!nested_cpu_has_vid(vmcs12) ||
11807 !nested_exit_intr_ack_set(vcpu) ||
11808 (vmcs12->posted_intr_nv & 0xff00) ||
11809 (vmcs12->posted_intr_desc_addr & 0x3f) ||
11810 (vmcs12->posted_intr_desc_addr >> cpuid_maxphyaddr(vcpu))))
11811 return -EINVAL;
11812
11813 /* tpr shadow is needed by all apicv features. */
11814 if (!nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW))
11815 return -EINVAL;
11816
11817 return 0;
11818 }
11819
nested_vmx_check_msr_switch(struct kvm_vcpu * vcpu,unsigned long count_field,unsigned long addr_field)11820 static int nested_vmx_check_msr_switch(struct kvm_vcpu *vcpu,
11821 unsigned long count_field,
11822 unsigned long addr_field)
11823 {
11824 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
11825 int maxphyaddr;
11826 u64 count, addr;
11827
11828 if (vmcs12_read_any(vmcs12, count_field, &count) ||
11829 vmcs12_read_any(vmcs12, addr_field, &addr)) {
11830 WARN_ON(1);
11831 return -EINVAL;
11832 }
11833 if (count == 0)
11834 return 0;
11835 maxphyaddr = cpuid_maxphyaddr(vcpu);
11836 if (!IS_ALIGNED(addr, 16) || addr >> maxphyaddr ||
11837 (addr + count * sizeof(struct vmx_msr_entry) - 1) >> maxphyaddr) {
11838 pr_debug_ratelimited(
11839 "nVMX: invalid MSR switch (0x%lx, %d, %llu, 0x%08llx)",
11840 addr_field, maxphyaddr, count, addr);
11841 return -EINVAL;
11842 }
11843 return 0;
11844 }
11845
nested_vmx_check_msr_switch_controls(struct kvm_vcpu * vcpu,struct vmcs12 * vmcs12)11846 static int nested_vmx_check_msr_switch_controls(struct kvm_vcpu *vcpu,
11847 struct vmcs12 *vmcs12)
11848 {
11849 if (vmcs12->vm_exit_msr_load_count == 0 &&
11850 vmcs12->vm_exit_msr_store_count == 0 &&
11851 vmcs12->vm_entry_msr_load_count == 0)
11852 return 0; /* Fast path */
11853 if (nested_vmx_check_msr_switch(vcpu, VM_EXIT_MSR_LOAD_COUNT,
11854 VM_EXIT_MSR_LOAD_ADDR) ||
11855 nested_vmx_check_msr_switch(vcpu, VM_EXIT_MSR_STORE_COUNT,
11856 VM_EXIT_MSR_STORE_ADDR) ||
11857 nested_vmx_check_msr_switch(vcpu, VM_ENTRY_MSR_LOAD_COUNT,
11858 VM_ENTRY_MSR_LOAD_ADDR))
11859 return -EINVAL;
11860 return 0;
11861 }
11862
nested_vmx_check_pml_controls(struct kvm_vcpu * vcpu,struct vmcs12 * vmcs12)11863 static int nested_vmx_check_pml_controls(struct kvm_vcpu *vcpu,
11864 struct vmcs12 *vmcs12)
11865 {
11866 u64 address = vmcs12->pml_address;
11867 int maxphyaddr = cpuid_maxphyaddr(vcpu);
11868
11869 if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_PML)) {
11870 if (!nested_cpu_has_ept(vmcs12) ||
11871 !IS_ALIGNED(address, 4096) ||
11872 address >> maxphyaddr)
11873 return -EINVAL;
11874 }
11875
11876 return 0;
11877 }
11878
nested_vmx_check_shadow_vmcs_controls(struct kvm_vcpu * vcpu,struct vmcs12 * vmcs12)11879 static int nested_vmx_check_shadow_vmcs_controls(struct kvm_vcpu *vcpu,
11880 struct vmcs12 *vmcs12)
11881 {
11882 if (!nested_cpu_has_shadow_vmcs(vmcs12))
11883 return 0;
11884
11885 if (!page_address_valid(vcpu, vmcs12->vmread_bitmap) ||
11886 !page_address_valid(vcpu, vmcs12->vmwrite_bitmap))
11887 return -EINVAL;
11888
11889 return 0;
11890 }
11891
nested_vmx_msr_check_common(struct kvm_vcpu * vcpu,struct vmx_msr_entry * e)11892 static int nested_vmx_msr_check_common(struct kvm_vcpu *vcpu,
11893 struct vmx_msr_entry *e)
11894 {
11895 /* x2APIC MSR accesses are not allowed */
11896 if (vcpu->arch.apic_base & X2APIC_ENABLE && e->index >> 8 == 0x8)
11897 return -EINVAL;
11898 if (e->index == MSR_IA32_UCODE_WRITE || /* SDM Table 35-2 */
11899 e->index == MSR_IA32_UCODE_REV)
11900 return -EINVAL;
11901 if (e->reserved != 0)
11902 return -EINVAL;
11903 return 0;
11904 }
11905
nested_vmx_load_msr_check(struct kvm_vcpu * vcpu,struct vmx_msr_entry * e)11906 static int nested_vmx_load_msr_check(struct kvm_vcpu *vcpu,
11907 struct vmx_msr_entry *e)
11908 {
11909 if (e->index == MSR_FS_BASE ||
11910 e->index == MSR_GS_BASE ||
11911 e->index == MSR_IA32_SMM_MONITOR_CTL || /* SMM is not supported */
11912 nested_vmx_msr_check_common(vcpu, e))
11913 return -EINVAL;
11914 return 0;
11915 }
11916
nested_vmx_store_msr_check(struct kvm_vcpu * vcpu,struct vmx_msr_entry * e)11917 static int nested_vmx_store_msr_check(struct kvm_vcpu *vcpu,
11918 struct vmx_msr_entry *e)
11919 {
11920 if (e->index == MSR_IA32_SMBASE || /* SMM is not supported */
11921 nested_vmx_msr_check_common(vcpu, e))
11922 return -EINVAL;
11923 return 0;
11924 }
11925
11926 /*
11927 * Load guest's/host's msr at nested entry/exit.
11928 * return 0 for success, entry index for failure.
11929 */
nested_vmx_load_msr(struct kvm_vcpu * vcpu,u64 gpa,u32 count)11930 static u32 nested_vmx_load_msr(struct kvm_vcpu *vcpu, u64 gpa, u32 count)
11931 {
11932 u32 i;
11933 struct vmx_msr_entry e;
11934 struct msr_data msr;
11935
11936 msr.host_initiated = false;
11937 for (i = 0; i < count; i++) {
11938 if (kvm_vcpu_read_guest(vcpu, gpa + i * sizeof(e),
11939 &e, sizeof(e))) {
11940 pr_debug_ratelimited(
11941 "%s cannot read MSR entry (%u, 0x%08llx)\n",
11942 __func__, i, gpa + i * sizeof(e));
11943 goto fail;
11944 }
11945 if (nested_vmx_load_msr_check(vcpu, &e)) {
11946 pr_debug_ratelimited(
11947 "%s check failed (%u, 0x%x, 0x%x)\n",
11948 __func__, i, e.index, e.reserved);
11949 goto fail;
11950 }
11951 msr.index = e.index;
11952 msr.data = e.value;
11953 if (kvm_set_msr(vcpu, &msr)) {
11954 pr_debug_ratelimited(
11955 "%s cannot write MSR (%u, 0x%x, 0x%llx)\n",
11956 __func__, i, e.index, e.value);
11957 goto fail;
11958 }
11959 }
11960 return 0;
11961 fail:
11962 return i + 1;
11963 }
11964
nested_vmx_store_msr(struct kvm_vcpu * vcpu,u64 gpa,u32 count)11965 static int nested_vmx_store_msr(struct kvm_vcpu *vcpu, u64 gpa, u32 count)
11966 {
11967 u32 i;
11968 struct vmx_msr_entry e;
11969
11970 for (i = 0; i < count; i++) {
11971 struct msr_data msr_info;
11972 if (kvm_vcpu_read_guest(vcpu,
11973 gpa + i * sizeof(e),
11974 &e, 2 * sizeof(u32))) {
11975 pr_debug_ratelimited(
11976 "%s cannot read MSR entry (%u, 0x%08llx)\n",
11977 __func__, i, gpa + i * sizeof(e));
11978 return -EINVAL;
11979 }
11980 if (nested_vmx_store_msr_check(vcpu, &e)) {
11981 pr_debug_ratelimited(
11982 "%s check failed (%u, 0x%x, 0x%x)\n",
11983 __func__, i, e.index, e.reserved);
11984 return -EINVAL;
11985 }
11986 msr_info.host_initiated = false;
11987 msr_info.index = e.index;
11988 if (kvm_get_msr(vcpu, &msr_info)) {
11989 pr_debug_ratelimited(
11990 "%s cannot read MSR (%u, 0x%x)\n",
11991 __func__, i, e.index);
11992 return -EINVAL;
11993 }
11994 if (kvm_vcpu_write_guest(vcpu,
11995 gpa + i * sizeof(e) +
11996 offsetof(struct vmx_msr_entry, value),
11997 &msr_info.data, sizeof(msr_info.data))) {
11998 pr_debug_ratelimited(
11999 "%s cannot write MSR (%u, 0x%x, 0x%llx)\n",
12000 __func__, i, e.index, msr_info.data);
12001 return -EINVAL;
12002 }
12003 }
12004 return 0;
12005 }
12006
nested_cr3_valid(struct kvm_vcpu * vcpu,unsigned long val)12007 static bool nested_cr3_valid(struct kvm_vcpu *vcpu, unsigned long val)
12008 {
12009 unsigned long invalid_mask;
12010
12011 invalid_mask = (~0ULL) << cpuid_maxphyaddr(vcpu);
12012 return (val & invalid_mask) == 0;
12013 }
12014
12015 /*
12016 * Load guest's/host's cr3 at nested entry/exit. nested_ept is true if we are
12017 * emulating VM entry into a guest with EPT enabled.
12018 * Returns 0 on success, 1 on failure. Invalid state exit qualification code
12019 * is assigned to entry_failure_code on failure.
12020 */
nested_vmx_load_cr3(struct kvm_vcpu * vcpu,unsigned long cr3,bool nested_ept,u32 * entry_failure_code)12021 static int nested_vmx_load_cr3(struct kvm_vcpu *vcpu, unsigned long cr3, bool nested_ept,
12022 u32 *entry_failure_code)
12023 {
12024 if (cr3 != kvm_read_cr3(vcpu) || (!nested_ept && pdptrs_changed(vcpu))) {
12025 if (!nested_cr3_valid(vcpu, cr3)) {
12026 *entry_failure_code = ENTRY_FAIL_DEFAULT;
12027 return 1;
12028 }
12029
12030 /*
12031 * If PAE paging and EPT are both on, CR3 is not used by the CPU and
12032 * must not be dereferenced.
12033 */
12034 if (is_pae_paging(vcpu) && !nested_ept) {
12035 if (!load_pdptrs(vcpu, vcpu->arch.walk_mmu, cr3)) {
12036 *entry_failure_code = ENTRY_FAIL_PDPTE;
12037 return 1;
12038 }
12039 }
12040 }
12041
12042 if (!nested_ept)
12043 kvm_mmu_new_cr3(vcpu, cr3, false);
12044
12045 vcpu->arch.cr3 = cr3;
12046 __set_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail);
12047
12048 kvm_init_mmu(vcpu, false);
12049
12050 return 0;
12051 }
12052
prepare_vmcs02_full(struct kvm_vcpu * vcpu,struct vmcs12 * vmcs12)12053 static void prepare_vmcs02_full(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
12054 {
12055 struct vcpu_vmx *vmx = to_vmx(vcpu);
12056
12057 vmcs_write16(GUEST_ES_SELECTOR, vmcs12->guest_es_selector);
12058 vmcs_write16(GUEST_SS_SELECTOR, vmcs12->guest_ss_selector);
12059 vmcs_write16(GUEST_DS_SELECTOR, vmcs12->guest_ds_selector);
12060 vmcs_write16(GUEST_FS_SELECTOR, vmcs12->guest_fs_selector);
12061 vmcs_write16(GUEST_GS_SELECTOR, vmcs12->guest_gs_selector);
12062 vmcs_write16(GUEST_LDTR_SELECTOR, vmcs12->guest_ldtr_selector);
12063 vmcs_write16(GUEST_TR_SELECTOR, vmcs12->guest_tr_selector);
12064 vmcs_write32(GUEST_ES_LIMIT, vmcs12->guest_es_limit);
12065 vmcs_write32(GUEST_SS_LIMIT, vmcs12->guest_ss_limit);
12066 vmcs_write32(GUEST_DS_LIMIT, vmcs12->guest_ds_limit);
12067 vmcs_write32(GUEST_FS_LIMIT, vmcs12->guest_fs_limit);
12068 vmcs_write32(GUEST_GS_LIMIT, vmcs12->guest_gs_limit);
12069 vmcs_write32(GUEST_LDTR_LIMIT, vmcs12->guest_ldtr_limit);
12070 vmcs_write32(GUEST_TR_LIMIT, vmcs12->guest_tr_limit);
12071 vmcs_write32(GUEST_GDTR_LIMIT, vmcs12->guest_gdtr_limit);
12072 vmcs_write32(GUEST_IDTR_LIMIT, vmcs12->guest_idtr_limit);
12073 vmcs_write32(GUEST_ES_AR_BYTES, vmcs12->guest_es_ar_bytes);
12074 vmcs_write32(GUEST_SS_AR_BYTES, vmcs12->guest_ss_ar_bytes);
12075 vmcs_write32(GUEST_DS_AR_BYTES, vmcs12->guest_ds_ar_bytes);
12076 vmcs_write32(GUEST_FS_AR_BYTES, vmcs12->guest_fs_ar_bytes);
12077 vmcs_write32(GUEST_GS_AR_BYTES, vmcs12->guest_gs_ar_bytes);
12078 vmcs_write32(GUEST_LDTR_AR_BYTES, vmcs12->guest_ldtr_ar_bytes);
12079 vmcs_write32(GUEST_TR_AR_BYTES, vmcs12->guest_tr_ar_bytes);
12080 vmcs_writel(GUEST_SS_BASE, vmcs12->guest_ss_base);
12081 vmcs_writel(GUEST_DS_BASE, vmcs12->guest_ds_base);
12082 vmcs_writel(GUEST_FS_BASE, vmcs12->guest_fs_base);
12083 vmcs_writel(GUEST_GS_BASE, vmcs12->guest_gs_base);
12084 vmcs_writel(GUEST_LDTR_BASE, vmcs12->guest_ldtr_base);
12085 vmcs_writel(GUEST_TR_BASE, vmcs12->guest_tr_base);
12086 vmcs_writel(GUEST_GDTR_BASE, vmcs12->guest_gdtr_base);
12087 vmcs_writel(GUEST_IDTR_BASE, vmcs12->guest_idtr_base);
12088
12089 vmcs_write32(GUEST_SYSENTER_CS, vmcs12->guest_sysenter_cs);
12090 vmcs_writel(GUEST_PENDING_DBG_EXCEPTIONS,
12091 vmcs12->guest_pending_dbg_exceptions);
12092 vmcs_writel(GUEST_SYSENTER_ESP, vmcs12->guest_sysenter_esp);
12093 vmcs_writel(GUEST_SYSENTER_EIP, vmcs12->guest_sysenter_eip);
12094
12095 if (nested_cpu_has_xsaves(vmcs12))
12096 vmcs_write64(XSS_EXIT_BITMAP, vmcs12->xss_exit_bitmap);
12097 vmcs_write64(VMCS_LINK_POINTER, -1ull);
12098
12099 if (cpu_has_vmx_posted_intr())
12100 vmcs_write16(POSTED_INTR_NV, POSTED_INTR_NESTED_VECTOR);
12101
12102 /*
12103 * Whether page-faults are trapped is determined by a combination of
12104 * 3 settings: PFEC_MASK, PFEC_MATCH and EXCEPTION_BITMAP.PF.
12105 * If enable_ept, L0 doesn't care about page faults and we should
12106 * set all of these to L1's desires. However, if !enable_ept, L0 does
12107 * care about (at least some) page faults, and because it is not easy
12108 * (if at all possible?) to merge L0 and L1's desires, we simply ask
12109 * to exit on each and every L2 page fault. This is done by setting
12110 * MASK=MATCH=0 and (see below) EB.PF=1.
12111 * Note that below we don't need special code to set EB.PF beyond the
12112 * "or"ing of the EB of vmcs01 and vmcs12, because when enable_ept,
12113 * vmcs01's EB.PF is 0 so the "or" will take vmcs12's value, and when
12114 * !enable_ept, EB.PF is 1, so the "or" will always be 1.
12115 */
12116 vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK,
12117 enable_ept ? vmcs12->page_fault_error_code_mask : 0);
12118 vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH,
12119 enable_ept ? vmcs12->page_fault_error_code_match : 0);
12120
12121 /* All VMFUNCs are currently emulated through L0 vmexits. */
12122 if (cpu_has_vmx_vmfunc())
12123 vmcs_write64(VM_FUNCTION_CONTROL, 0);
12124
12125 if (cpu_has_vmx_apicv()) {
12126 vmcs_write64(EOI_EXIT_BITMAP0, vmcs12->eoi_exit_bitmap0);
12127 vmcs_write64(EOI_EXIT_BITMAP1, vmcs12->eoi_exit_bitmap1);
12128 vmcs_write64(EOI_EXIT_BITMAP2, vmcs12->eoi_exit_bitmap2);
12129 vmcs_write64(EOI_EXIT_BITMAP3, vmcs12->eoi_exit_bitmap3);
12130 }
12131
12132 /*
12133 * Set host-state according to L0's settings (vmcs12 is irrelevant here)
12134 * Some constant fields are set here by vmx_set_constant_host_state().
12135 * Other fields are different per CPU, and will be set later when
12136 * vmx_vcpu_load() is called, and when vmx_prepare_switch_to_guest()
12137 * is called.
12138 */
12139 vmx_set_constant_host_state(vmx);
12140
12141 /*
12142 * Set the MSR load/store lists to match L0's settings.
12143 */
12144 vmcs_write32(VM_EXIT_MSR_STORE_COUNT, 0);
12145 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, vmx->msr_autoload.host.nr);
12146 vmcs_write64(VM_EXIT_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.host.val));
12147 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, vmx->msr_autoload.guest.nr);
12148 vmcs_write64(VM_ENTRY_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.guest.val));
12149
12150 set_cr4_guest_host_mask(vmx);
12151
12152 if (kvm_mpx_supported() && vmx->nested.nested_run_pending &&
12153 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_BNDCFGS))
12154 vmcs_write64(GUEST_BNDCFGS, vmcs12->guest_bndcfgs);
12155
12156 if (enable_vpid) {
12157 if (nested_cpu_has_vpid(vmcs12) && vmx->nested.vpid02)
12158 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->nested.vpid02);
12159 else
12160 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid);
12161 }
12162
12163 /*
12164 * L1 may access the L2's PDPTR, so save them to construct vmcs12
12165 */
12166 if (enable_ept) {
12167 vmcs_write64(GUEST_PDPTR0, vmcs12->guest_pdptr0);
12168 vmcs_write64(GUEST_PDPTR1, vmcs12->guest_pdptr1);
12169 vmcs_write64(GUEST_PDPTR2, vmcs12->guest_pdptr2);
12170 vmcs_write64(GUEST_PDPTR3, vmcs12->guest_pdptr3);
12171 }
12172
12173 if (cpu_has_vmx_msr_bitmap())
12174 vmcs_write64(MSR_BITMAP, __pa(vmx->nested.vmcs02.msr_bitmap));
12175 }
12176
12177 /*
12178 * prepare_vmcs02 is called when the L1 guest hypervisor runs its nested
12179 * L2 guest. L1 has a vmcs for L2 (vmcs12), and this function "merges" it
12180 * with L0's requirements for its guest (a.k.a. vmcs01), so we can run the L2
12181 * guest in a way that will both be appropriate to L1's requests, and our
12182 * needs. In addition to modifying the active vmcs (which is vmcs02), this
12183 * function also has additional necessary side-effects, like setting various
12184 * vcpu->arch fields.
12185 * Returns 0 on success, 1 on failure. Invalid state exit qualification code
12186 * is assigned to entry_failure_code on failure.
12187 */
prepare_vmcs02(struct kvm_vcpu * vcpu,struct vmcs12 * vmcs12,u32 * entry_failure_code)12188 static int prepare_vmcs02(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12,
12189 u32 *entry_failure_code)
12190 {
12191 struct vcpu_vmx *vmx = to_vmx(vcpu);
12192 u32 exec_control, vmcs12_exec_ctrl;
12193
12194 if (vmx->nested.dirty_vmcs12) {
12195 prepare_vmcs02_full(vcpu, vmcs12);
12196 vmx->nested.dirty_vmcs12 = false;
12197 }
12198
12199 /*
12200 * First, the fields that are shadowed. This must be kept in sync
12201 * with vmx_shadow_fields.h.
12202 */
12203
12204 vmcs_write16(GUEST_CS_SELECTOR, vmcs12->guest_cs_selector);
12205 vmcs_write32(GUEST_CS_LIMIT, vmcs12->guest_cs_limit);
12206 vmcs_write32(GUEST_CS_AR_BYTES, vmcs12->guest_cs_ar_bytes);
12207 vmcs_writel(GUEST_ES_BASE, vmcs12->guest_es_base);
12208 vmcs_writel(GUEST_CS_BASE, vmcs12->guest_cs_base);
12209
12210 if (vmx->nested.nested_run_pending &&
12211 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS)) {
12212 kvm_set_dr(vcpu, 7, vmcs12->guest_dr7);
12213 vmcs_write64(GUEST_IA32_DEBUGCTL, vmcs12->guest_ia32_debugctl);
12214 } else {
12215 kvm_set_dr(vcpu, 7, vcpu->arch.dr7);
12216 vmcs_write64(GUEST_IA32_DEBUGCTL, vmx->nested.vmcs01_debugctl);
12217 }
12218 if (kvm_mpx_supported() && (!vmx->nested.nested_run_pending ||
12219 !(vmcs12->vm_entry_controls & VM_ENTRY_LOAD_BNDCFGS)))
12220 vmcs_write64(GUEST_BNDCFGS, vmx->nested.vmcs01_guest_bndcfgs);
12221 if (vmx->nested.nested_run_pending) {
12222 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
12223 vmcs12->vm_entry_intr_info_field);
12224 vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE,
12225 vmcs12->vm_entry_exception_error_code);
12226 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
12227 vmcs12->vm_entry_instruction_len);
12228 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO,
12229 vmcs12->guest_interruptibility_info);
12230 vmx->loaded_vmcs->nmi_known_unmasked =
12231 !(vmcs12->guest_interruptibility_info & GUEST_INTR_STATE_NMI);
12232 } else {
12233 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0);
12234 }
12235 vmx_set_rflags(vcpu, vmcs12->guest_rflags);
12236
12237 exec_control = vmcs12->pin_based_vm_exec_control;
12238
12239 /* Preemption timer setting is computed directly in vmx_vcpu_run. */
12240 exec_control |= vmcs_config.pin_based_exec_ctrl;
12241 exec_control &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
12242 vmx->loaded_vmcs->hv_timer_armed = false;
12243
12244 /* Posted interrupts setting is only taken from vmcs12. */
12245 if (nested_cpu_has_posted_intr(vmcs12)) {
12246 vmx->nested.posted_intr_nv = vmcs12->posted_intr_nv;
12247 vmx->nested.pi_pending = false;
12248 } else {
12249 exec_control &= ~PIN_BASED_POSTED_INTR;
12250 }
12251
12252 vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, exec_control);
12253
12254 vmx->nested.preemption_timer_expired = false;
12255 if (nested_cpu_has_preemption_timer(vmcs12))
12256 vmx_start_preemption_timer(vcpu);
12257
12258 if (cpu_has_secondary_exec_ctrls()) {
12259 exec_control = vmx->secondary_exec_control;
12260
12261 /* Take the following fields only from vmcs12 */
12262 exec_control &= ~(SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
12263 SECONDARY_EXEC_ENABLE_INVPCID |
12264 SECONDARY_EXEC_RDTSCP |
12265 SECONDARY_EXEC_XSAVES |
12266 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY |
12267 SECONDARY_EXEC_APIC_REGISTER_VIRT |
12268 SECONDARY_EXEC_ENABLE_VMFUNC);
12269 if (nested_cpu_has(vmcs12,
12270 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS)) {
12271 vmcs12_exec_ctrl = vmcs12->secondary_vm_exec_control &
12272 ~SECONDARY_EXEC_ENABLE_PML;
12273 exec_control |= vmcs12_exec_ctrl;
12274 }
12275
12276 /* VMCS shadowing for L2 is emulated for now */
12277 exec_control &= ~SECONDARY_EXEC_SHADOW_VMCS;
12278
12279 if (exec_control & SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY)
12280 vmcs_write16(GUEST_INTR_STATUS,
12281 vmcs12->guest_intr_status);
12282
12283 /*
12284 * Write an illegal value to APIC_ACCESS_ADDR. Later,
12285 * nested_get_vmcs12_pages will either fix it up or
12286 * remove the VM execution control.
12287 */
12288 if (exec_control & SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)
12289 vmcs_write64(APIC_ACCESS_ADDR, -1ull);
12290
12291 if (exec_control & SECONDARY_EXEC_ENCLS_EXITING)
12292 vmcs_write64(ENCLS_EXITING_BITMAP, -1ull);
12293
12294 vmcs_write32(SECONDARY_VM_EXEC_CONTROL, exec_control);
12295 }
12296
12297 /*
12298 * HOST_RSP is normally set correctly in vmx_vcpu_run() just before
12299 * entry, but only if the current (host) sp changed from the value
12300 * we wrote last (vmx->host_rsp). This cache is no longer relevant
12301 * if we switch vmcs, and rather than hold a separate cache per vmcs,
12302 * here we just force the write to happen on entry.
12303 */
12304 vmx->host_rsp = 0;
12305
12306 exec_control = vmx_exec_control(vmx); /* L0's desires */
12307 exec_control &= ~CPU_BASED_VIRTUAL_INTR_PENDING;
12308 exec_control &= ~CPU_BASED_VIRTUAL_NMI_PENDING;
12309 exec_control &= ~CPU_BASED_TPR_SHADOW;
12310 exec_control |= vmcs12->cpu_based_vm_exec_control;
12311
12312 /*
12313 * Write an illegal value to VIRTUAL_APIC_PAGE_ADDR. Later, if
12314 * nested_get_vmcs12_pages can't fix it up, the illegal value
12315 * will result in a VM entry failure.
12316 */
12317 if (exec_control & CPU_BASED_TPR_SHADOW) {
12318 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, -1ull);
12319 vmcs_write32(TPR_THRESHOLD, vmcs12->tpr_threshold);
12320 } else {
12321 #ifdef CONFIG_X86_64
12322 exec_control |= CPU_BASED_CR8_LOAD_EXITING |
12323 CPU_BASED_CR8_STORE_EXITING;
12324 #endif
12325 }
12326
12327 /*
12328 * A vmexit (to either L1 hypervisor or L0 userspace) is always needed
12329 * for I/O port accesses.
12330 */
12331 exec_control &= ~CPU_BASED_USE_IO_BITMAPS;
12332 exec_control |= CPU_BASED_UNCOND_IO_EXITING;
12333
12334 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, exec_control);
12335
12336 /* EXCEPTION_BITMAP and CR0_GUEST_HOST_MASK should basically be the
12337 * bitwise-or of what L1 wants to trap for L2, and what we want to
12338 * trap. Note that CR0.TS also needs updating - we do this later.
12339 */
12340 update_exception_bitmap(vcpu);
12341 vcpu->arch.cr0_guest_owned_bits &= ~vmcs12->cr0_guest_host_mask;
12342 vmcs_writel(CR0_GUEST_HOST_MASK, ~vcpu->arch.cr0_guest_owned_bits);
12343
12344 /* L2->L1 exit controls are emulated - the hardware exit is to L0 so
12345 * we should use its exit controls. Note that VM_EXIT_LOAD_IA32_EFER
12346 * bits are further modified by vmx_set_efer() below.
12347 */
12348 vmcs_write32(VM_EXIT_CONTROLS, vmcs_config.vmexit_ctrl);
12349
12350 /* vmcs12's VM_ENTRY_LOAD_IA32_EFER and VM_ENTRY_IA32E_MODE are
12351 * emulated by vmx_set_efer(), below.
12352 */
12353 vm_entry_controls_init(vmx,
12354 (vmcs12->vm_entry_controls & ~VM_ENTRY_LOAD_IA32_EFER &
12355 ~VM_ENTRY_IA32E_MODE) |
12356 (vmcs_config.vmentry_ctrl & ~VM_ENTRY_IA32E_MODE));
12357
12358 if (vmx->nested.nested_run_pending &&
12359 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_PAT)) {
12360 vmcs_write64(GUEST_IA32_PAT, vmcs12->guest_ia32_pat);
12361 vcpu->arch.pat = vmcs12->guest_ia32_pat;
12362 } else if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) {
12363 vmcs_write64(GUEST_IA32_PAT, vmx->vcpu.arch.pat);
12364 }
12365
12366 vmcs_write64(TSC_OFFSET, vcpu->arch.tsc_offset);
12367
12368 if (kvm_has_tsc_control)
12369 decache_tsc_multiplier(vmx);
12370
12371 if (enable_vpid) {
12372 /*
12373 * There is no direct mapping between vpid02 and vpid12, the
12374 * vpid02 is per-vCPU for L0 and reused while the value of
12375 * vpid12 is changed w/ one invvpid during nested vmentry.
12376 * The vpid12 is allocated by L1 for L2, so it will not
12377 * influence global bitmap(for vpid01 and vpid02 allocation)
12378 * even if spawn a lot of nested vCPUs.
12379 */
12380 if (nested_cpu_has_vpid(vmcs12) && vmx->nested.vpid02) {
12381 if (vmcs12->virtual_processor_id != vmx->nested.last_vpid) {
12382 vmx->nested.last_vpid = vmcs12->virtual_processor_id;
12383 __vmx_flush_tlb(vcpu, vmx->nested.vpid02, true);
12384 }
12385 } else {
12386 vmx_flush_tlb(vcpu, true);
12387 }
12388 }
12389
12390 if (enable_pml) {
12391 /*
12392 * Conceptually we want to copy the PML address and index from
12393 * vmcs01 here, and then back to vmcs01 on nested vmexit. But,
12394 * since we always flush the log on each vmexit, this happens
12395 * to be equivalent to simply resetting the fields in vmcs02.
12396 */
12397 ASSERT(vmx->pml_pg);
12398 vmcs_write64(PML_ADDRESS, page_to_phys(vmx->pml_pg));
12399 vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1);
12400 }
12401
12402 if (nested_cpu_has_ept(vmcs12)) {
12403 if (nested_ept_init_mmu_context(vcpu)) {
12404 *entry_failure_code = ENTRY_FAIL_DEFAULT;
12405 return 1;
12406 }
12407 } else if (nested_cpu_has2(vmcs12,
12408 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) {
12409 vmx_flush_tlb(vcpu, true);
12410 }
12411
12412 /*
12413 * This sets GUEST_CR0 to vmcs12->guest_cr0, possibly modifying those
12414 * bits which we consider mandatory enabled.
12415 * The CR0_READ_SHADOW is what L2 should have expected to read given
12416 * the specifications by L1; It's not enough to take
12417 * vmcs12->cr0_read_shadow because on our cr0_guest_host_mask we we
12418 * have more bits than L1 expected.
12419 */
12420 vmx_set_cr0(vcpu, vmcs12->guest_cr0);
12421 vmcs_writel(CR0_READ_SHADOW, nested_read_cr0(vmcs12));
12422
12423 vmx_set_cr4(vcpu, vmcs12->guest_cr4);
12424 vmcs_writel(CR4_READ_SHADOW, nested_read_cr4(vmcs12));
12425
12426 if (vmx->nested.nested_run_pending &&
12427 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_EFER))
12428 vcpu->arch.efer = vmcs12->guest_ia32_efer;
12429 else if (vmcs12->vm_entry_controls & VM_ENTRY_IA32E_MODE)
12430 vcpu->arch.efer |= (EFER_LMA | EFER_LME);
12431 else
12432 vcpu->arch.efer &= ~(EFER_LMA | EFER_LME);
12433 /* Note: modifies VM_ENTRY/EXIT_CONTROLS and GUEST/HOST_IA32_EFER */
12434 vmx_set_efer(vcpu, vcpu->arch.efer);
12435
12436 /*
12437 * Guest state is invalid and unrestricted guest is disabled,
12438 * which means L1 attempted VMEntry to L2 with invalid state.
12439 * Fail the VMEntry.
12440 */
12441 if (vmx->emulation_required) {
12442 *entry_failure_code = ENTRY_FAIL_DEFAULT;
12443 return 1;
12444 }
12445
12446 /* Shadow page tables on either EPT or shadow page tables. */
12447 if (nested_vmx_load_cr3(vcpu, vmcs12->guest_cr3, nested_cpu_has_ept(vmcs12),
12448 entry_failure_code))
12449 return 1;
12450
12451 if (!enable_ept)
12452 vcpu->arch.walk_mmu->inject_page_fault = vmx_inject_page_fault_nested;
12453
12454 kvm_register_write(vcpu, VCPU_REGS_RSP, vmcs12->guest_rsp);
12455 kvm_register_write(vcpu, VCPU_REGS_RIP, vmcs12->guest_rip);
12456 return 0;
12457 }
12458
nested_vmx_check_nmi_controls(struct vmcs12 * vmcs12)12459 static int nested_vmx_check_nmi_controls(struct vmcs12 *vmcs12)
12460 {
12461 if (!nested_cpu_has_nmi_exiting(vmcs12) &&
12462 nested_cpu_has_virtual_nmis(vmcs12))
12463 return -EINVAL;
12464
12465 if (!nested_cpu_has_virtual_nmis(vmcs12) &&
12466 nested_cpu_has(vmcs12, CPU_BASED_VIRTUAL_NMI_PENDING))
12467 return -EINVAL;
12468
12469 return 0;
12470 }
12471
check_vmentry_prereqs(struct kvm_vcpu * vcpu,struct vmcs12 * vmcs12)12472 static int check_vmentry_prereqs(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
12473 {
12474 struct vcpu_vmx *vmx = to_vmx(vcpu);
12475
12476 if (vmcs12->guest_activity_state != GUEST_ACTIVITY_ACTIVE &&
12477 vmcs12->guest_activity_state != GUEST_ACTIVITY_HLT)
12478 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12479
12480 if (nested_cpu_has_vpid(vmcs12) && !vmcs12->virtual_processor_id)
12481 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12482
12483 if (nested_vmx_check_io_bitmap_controls(vcpu, vmcs12))
12484 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12485
12486 if (nested_vmx_check_msr_bitmap_controls(vcpu, vmcs12))
12487 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12488
12489 if (nested_vmx_check_apic_access_controls(vcpu, vmcs12))
12490 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12491
12492 if (nested_vmx_check_tpr_shadow_controls(vcpu, vmcs12))
12493 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12494
12495 if (nested_vmx_check_apicv_controls(vcpu, vmcs12))
12496 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12497
12498 if (nested_vmx_check_msr_switch_controls(vcpu, vmcs12))
12499 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12500
12501 if (nested_vmx_check_pml_controls(vcpu, vmcs12))
12502 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12503
12504 if (nested_vmx_check_shadow_vmcs_controls(vcpu, vmcs12))
12505 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12506
12507 if (!vmx_control_verify(vmcs12->cpu_based_vm_exec_control,
12508 vmx->nested.msrs.procbased_ctls_low,
12509 vmx->nested.msrs.procbased_ctls_high) ||
12510 (nested_cpu_has(vmcs12, CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) &&
12511 !vmx_control_verify(vmcs12->secondary_vm_exec_control,
12512 vmx->nested.msrs.secondary_ctls_low,
12513 vmx->nested.msrs.secondary_ctls_high)) ||
12514 !vmx_control_verify(vmcs12->pin_based_vm_exec_control,
12515 vmx->nested.msrs.pinbased_ctls_low,
12516 vmx->nested.msrs.pinbased_ctls_high) ||
12517 !vmx_control_verify(vmcs12->vm_exit_controls,
12518 vmx->nested.msrs.exit_ctls_low,
12519 vmx->nested.msrs.exit_ctls_high) ||
12520 !vmx_control_verify(vmcs12->vm_entry_controls,
12521 vmx->nested.msrs.entry_ctls_low,
12522 vmx->nested.msrs.entry_ctls_high))
12523 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12524
12525 if (nested_vmx_check_nmi_controls(vmcs12))
12526 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12527
12528 if (nested_cpu_has_vmfunc(vmcs12)) {
12529 if (vmcs12->vm_function_control &
12530 ~vmx->nested.msrs.vmfunc_controls)
12531 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12532
12533 if (nested_cpu_has_eptp_switching(vmcs12)) {
12534 if (!nested_cpu_has_ept(vmcs12) ||
12535 !page_address_valid(vcpu, vmcs12->eptp_list_address))
12536 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12537 }
12538 }
12539
12540 if (vmcs12->cr3_target_count > nested_cpu_vmx_misc_cr3_count(vcpu))
12541 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12542
12543 if (!nested_host_cr0_valid(vcpu, vmcs12->host_cr0) ||
12544 !nested_host_cr4_valid(vcpu, vmcs12->host_cr4) ||
12545 !nested_cr3_valid(vcpu, vmcs12->host_cr3))
12546 return VMXERR_ENTRY_INVALID_HOST_STATE_FIELD;
12547
12548 /*
12549 * From the Intel SDM, volume 3:
12550 * Fields relevant to VM-entry event injection must be set properly.
12551 * These fields are the VM-entry interruption-information field, the
12552 * VM-entry exception error code, and the VM-entry instruction length.
12553 */
12554 if (vmcs12->vm_entry_intr_info_field & INTR_INFO_VALID_MASK) {
12555 u32 intr_info = vmcs12->vm_entry_intr_info_field;
12556 u8 vector = intr_info & INTR_INFO_VECTOR_MASK;
12557 u32 intr_type = intr_info & INTR_INFO_INTR_TYPE_MASK;
12558 bool has_error_code = intr_info & INTR_INFO_DELIVER_CODE_MASK;
12559 bool should_have_error_code;
12560 bool urg = nested_cpu_has2(vmcs12,
12561 SECONDARY_EXEC_UNRESTRICTED_GUEST);
12562 bool prot_mode = !urg || vmcs12->guest_cr0 & X86_CR0_PE;
12563
12564 /* VM-entry interruption-info field: interruption type */
12565 if (intr_type == INTR_TYPE_RESERVED ||
12566 (intr_type == INTR_TYPE_OTHER_EVENT &&
12567 !nested_cpu_supports_monitor_trap_flag(vcpu)))
12568 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12569
12570 /* VM-entry interruption-info field: vector */
12571 if ((intr_type == INTR_TYPE_NMI_INTR && vector != NMI_VECTOR) ||
12572 (intr_type == INTR_TYPE_HARD_EXCEPTION && vector > 31) ||
12573 (intr_type == INTR_TYPE_OTHER_EVENT && vector != 0))
12574 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12575
12576 /* VM-entry interruption-info field: deliver error code */
12577 should_have_error_code =
12578 intr_type == INTR_TYPE_HARD_EXCEPTION && prot_mode &&
12579 x86_exception_has_error_code(vector);
12580 if (has_error_code != should_have_error_code)
12581 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12582
12583 /* VM-entry exception error code */
12584 if (has_error_code &&
12585 vmcs12->vm_entry_exception_error_code & GENMASK(31, 16))
12586 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12587
12588 /* VM-entry interruption-info field: reserved bits */
12589 if (intr_info & INTR_INFO_RESVD_BITS_MASK)
12590 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12591
12592 /* VM-entry instruction length */
12593 switch (intr_type) {
12594 case INTR_TYPE_SOFT_EXCEPTION:
12595 case INTR_TYPE_SOFT_INTR:
12596 case INTR_TYPE_PRIV_SW_EXCEPTION:
12597 if ((vmcs12->vm_entry_instruction_len > 15) ||
12598 (vmcs12->vm_entry_instruction_len == 0 &&
12599 !nested_cpu_has_zero_length_injection(vcpu)))
12600 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12601 }
12602 }
12603
12604 return 0;
12605 }
12606
nested_vmx_check_vmcs_link_ptr(struct kvm_vcpu * vcpu,struct vmcs12 * vmcs12)12607 static int nested_vmx_check_vmcs_link_ptr(struct kvm_vcpu *vcpu,
12608 struct vmcs12 *vmcs12)
12609 {
12610 int r;
12611 struct page *page;
12612 struct vmcs12 *shadow;
12613
12614 if (vmcs12->vmcs_link_pointer == -1ull)
12615 return 0;
12616
12617 if (!page_address_valid(vcpu, vmcs12->vmcs_link_pointer))
12618 return -EINVAL;
12619
12620 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->vmcs_link_pointer);
12621 if (is_error_page(page))
12622 return -EINVAL;
12623
12624 r = 0;
12625 shadow = kmap(page);
12626 if (shadow->hdr.revision_id != VMCS12_REVISION ||
12627 shadow->hdr.shadow_vmcs != nested_cpu_has_shadow_vmcs(vmcs12))
12628 r = -EINVAL;
12629 kunmap(page);
12630 kvm_release_page_clean(page);
12631 return r;
12632 }
12633
check_vmentry_postreqs(struct kvm_vcpu * vcpu,struct vmcs12 * vmcs12,u32 * exit_qual)12634 static int check_vmentry_postreqs(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12,
12635 u32 *exit_qual)
12636 {
12637 bool ia32e;
12638
12639 *exit_qual = ENTRY_FAIL_DEFAULT;
12640
12641 if (!nested_guest_cr0_valid(vcpu, vmcs12->guest_cr0) ||
12642 !nested_guest_cr4_valid(vcpu, vmcs12->guest_cr4))
12643 return 1;
12644
12645 if (nested_vmx_check_vmcs_link_ptr(vcpu, vmcs12)) {
12646 *exit_qual = ENTRY_FAIL_VMCS_LINK_PTR;
12647 return 1;
12648 }
12649
12650 /*
12651 * If the load IA32_EFER VM-entry control is 1, the following checks
12652 * are performed on the field for the IA32_EFER MSR:
12653 * - Bits reserved in the IA32_EFER MSR must be 0.
12654 * - Bit 10 (corresponding to IA32_EFER.LMA) must equal the value of
12655 * the IA-32e mode guest VM-exit control. It must also be identical
12656 * to bit 8 (LME) if bit 31 in the CR0 field (corresponding to
12657 * CR0.PG) is 1.
12658 */
12659 if (to_vmx(vcpu)->nested.nested_run_pending &&
12660 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_EFER)) {
12661 ia32e = (vmcs12->vm_entry_controls & VM_ENTRY_IA32E_MODE) != 0;
12662 if (!kvm_valid_efer(vcpu, vmcs12->guest_ia32_efer) ||
12663 ia32e != !!(vmcs12->guest_ia32_efer & EFER_LMA) ||
12664 ((vmcs12->guest_cr0 & X86_CR0_PG) &&
12665 ia32e != !!(vmcs12->guest_ia32_efer & EFER_LME)))
12666 return 1;
12667 }
12668
12669 /*
12670 * If the load IA32_EFER VM-exit control is 1, bits reserved in the
12671 * IA32_EFER MSR must be 0 in the field for that register. In addition,
12672 * the values of the LMA and LME bits in the field must each be that of
12673 * the host address-space size VM-exit control.
12674 */
12675 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_EFER) {
12676 ia32e = (vmcs12->vm_exit_controls &
12677 VM_EXIT_HOST_ADDR_SPACE_SIZE) != 0;
12678 if (!kvm_valid_efer(vcpu, vmcs12->host_ia32_efer) ||
12679 ia32e != !!(vmcs12->host_ia32_efer & EFER_LMA) ||
12680 ia32e != !!(vmcs12->host_ia32_efer & EFER_LME))
12681 return 1;
12682 }
12683
12684 if ((vmcs12->vm_entry_controls & VM_ENTRY_LOAD_BNDCFGS) &&
12685 (is_noncanonical_address(vmcs12->guest_bndcfgs & PAGE_MASK, vcpu) ||
12686 (vmcs12->guest_bndcfgs & MSR_IA32_BNDCFGS_RSVD)))
12687 return 1;
12688
12689 return 0;
12690 }
12691
12692 /*
12693 * If exit_qual is NULL, this is being called from state restore (either RSM
12694 * or KVM_SET_NESTED_STATE). Otherwise it's called from vmlaunch/vmresume.
12695 */
enter_vmx_non_root_mode(struct kvm_vcpu * vcpu,u32 * exit_qual)12696 static int enter_vmx_non_root_mode(struct kvm_vcpu *vcpu, u32 *exit_qual)
12697 {
12698 struct vcpu_vmx *vmx = to_vmx(vcpu);
12699 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
12700 bool from_vmentry = !!exit_qual;
12701 u32 dummy_exit_qual;
12702 bool evaluate_pending_interrupts;
12703 int r = 0;
12704
12705 evaluate_pending_interrupts = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) &
12706 (CPU_BASED_VIRTUAL_INTR_PENDING | CPU_BASED_VIRTUAL_NMI_PENDING);
12707 if (likely(!evaluate_pending_interrupts) && kvm_vcpu_apicv_active(vcpu))
12708 evaluate_pending_interrupts |= vmx_has_apicv_interrupt(vcpu);
12709
12710 enter_guest_mode(vcpu);
12711
12712 if (!(vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS))
12713 vmx->nested.vmcs01_debugctl = vmcs_read64(GUEST_IA32_DEBUGCTL);
12714 if (kvm_mpx_supported() &&
12715 !(vmcs12->vm_entry_controls & VM_ENTRY_LOAD_BNDCFGS))
12716 vmx->nested.vmcs01_guest_bndcfgs = vmcs_read64(GUEST_BNDCFGS);
12717
12718 vmx_switch_vmcs(vcpu, &vmx->nested.vmcs02);
12719 vmx_segment_cache_clear(vmx);
12720
12721 if (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING)
12722 vcpu->arch.tsc_offset += vmcs12->tsc_offset;
12723
12724 r = EXIT_REASON_INVALID_STATE;
12725 if (prepare_vmcs02(vcpu, vmcs12, from_vmentry ? exit_qual : &dummy_exit_qual))
12726 goto fail;
12727
12728 if (from_vmentry) {
12729 nested_get_vmcs12_pages(vcpu);
12730
12731 r = EXIT_REASON_MSR_LOAD_FAIL;
12732 *exit_qual = nested_vmx_load_msr(vcpu,
12733 vmcs12->vm_entry_msr_load_addr,
12734 vmcs12->vm_entry_msr_load_count);
12735 if (*exit_qual)
12736 goto fail;
12737 } else {
12738 /*
12739 * The MMU is not initialized to point at the right entities yet and
12740 * "get pages" would need to read data from the guest (i.e. we will
12741 * need to perform gpa to hpa translation). Request a call
12742 * to nested_get_vmcs12_pages before the next VM-entry. The MSRs
12743 * have already been set at vmentry time and should not be reset.
12744 */
12745 kvm_make_request(KVM_REQ_GET_VMCS12_PAGES, vcpu);
12746 }
12747
12748 /*
12749 * If L1 had a pending IRQ/NMI until it executed
12750 * VMLAUNCH/VMRESUME which wasn't delivered because it was
12751 * disallowed (e.g. interrupts disabled), L0 needs to
12752 * evaluate if this pending event should cause an exit from L2
12753 * to L1 or delivered directly to L2 (e.g. In case L1 don't
12754 * intercept EXTERNAL_INTERRUPT).
12755 *
12756 * Usually this would be handled by the processor noticing an
12757 * IRQ/NMI window request, or checking RVI during evaluation of
12758 * pending virtual interrupts. However, this setting was done
12759 * on VMCS01 and now VMCS02 is active instead. Thus, we force L0
12760 * to perform pending event evaluation by requesting a KVM_REQ_EVENT.
12761 */
12762 if (unlikely(evaluate_pending_interrupts))
12763 kvm_make_request(KVM_REQ_EVENT, vcpu);
12764
12765 /*
12766 * Note no nested_vmx_succeed or nested_vmx_fail here. At this point
12767 * we are no longer running L1, and VMLAUNCH/VMRESUME has not yet
12768 * returned as far as L1 is concerned. It will only return (and set
12769 * the success flag) when L2 exits (see nested_vmx_vmexit()).
12770 */
12771 return 0;
12772
12773 fail:
12774 if (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING)
12775 vcpu->arch.tsc_offset -= vmcs12->tsc_offset;
12776 leave_guest_mode(vcpu);
12777 vmx_switch_vmcs(vcpu, &vmx->vmcs01);
12778 return r;
12779 }
12780
12781 /*
12782 * nested_vmx_run() handles a nested entry, i.e., a VMLAUNCH or VMRESUME on L1
12783 * for running an L2 nested guest.
12784 */
nested_vmx_run(struct kvm_vcpu * vcpu,bool launch)12785 static int nested_vmx_run(struct kvm_vcpu *vcpu, bool launch)
12786 {
12787 struct vmcs12 *vmcs12;
12788 struct vcpu_vmx *vmx = to_vmx(vcpu);
12789 u32 interrupt_shadow = vmx_get_interrupt_shadow(vcpu);
12790 u32 exit_qual;
12791 int ret;
12792
12793 if (!nested_vmx_check_permission(vcpu))
12794 return 1;
12795
12796 if (!nested_vmx_check_vmcs12(vcpu))
12797 goto out;
12798
12799 vmcs12 = get_vmcs12(vcpu);
12800
12801 /*
12802 * Can't VMLAUNCH or VMRESUME a shadow VMCS. Despite the fact
12803 * that there *is* a valid VMCS pointer, RFLAGS.CF is set
12804 * rather than RFLAGS.ZF, and no error number is stored to the
12805 * VM-instruction error field.
12806 */
12807 if (vmcs12->hdr.shadow_vmcs) {
12808 nested_vmx_failInvalid(vcpu);
12809 goto out;
12810 }
12811
12812 if (enable_shadow_vmcs)
12813 copy_shadow_to_vmcs12(vmx);
12814
12815 /*
12816 * The nested entry process starts with enforcing various prerequisites
12817 * on vmcs12 as required by the Intel SDM, and act appropriately when
12818 * they fail: As the SDM explains, some conditions should cause the
12819 * instruction to fail, while others will cause the instruction to seem
12820 * to succeed, but return an EXIT_REASON_INVALID_STATE.
12821 * To speed up the normal (success) code path, we should avoid checking
12822 * for misconfigurations which will anyway be caught by the processor
12823 * when using the merged vmcs02.
12824 */
12825 if (interrupt_shadow & KVM_X86_SHADOW_INT_MOV_SS) {
12826 nested_vmx_failValid(vcpu,
12827 VMXERR_ENTRY_EVENTS_BLOCKED_BY_MOV_SS);
12828 goto out;
12829 }
12830
12831 if (vmcs12->launch_state == launch) {
12832 nested_vmx_failValid(vcpu,
12833 launch ? VMXERR_VMLAUNCH_NONCLEAR_VMCS
12834 : VMXERR_VMRESUME_NONLAUNCHED_VMCS);
12835 goto out;
12836 }
12837
12838 ret = check_vmentry_prereqs(vcpu, vmcs12);
12839 if (ret) {
12840 nested_vmx_failValid(vcpu, ret);
12841 goto out;
12842 }
12843
12844 /*
12845 * After this point, the trap flag no longer triggers a singlestep trap
12846 * on the vm entry instructions; don't call kvm_skip_emulated_instruction.
12847 * This is not 100% correct; for performance reasons, we delegate most
12848 * of the checks on host state to the processor. If those fail,
12849 * the singlestep trap is missed.
12850 */
12851 skip_emulated_instruction(vcpu);
12852
12853 ret = check_vmentry_postreqs(vcpu, vmcs12, &exit_qual);
12854 if (ret) {
12855 nested_vmx_entry_failure(vcpu, vmcs12,
12856 EXIT_REASON_INVALID_STATE, exit_qual);
12857 return 1;
12858 }
12859
12860 /*
12861 * We're finally done with prerequisite checking, and can start with
12862 * the nested entry.
12863 */
12864
12865 vmx->nested.nested_run_pending = 1;
12866 ret = enter_vmx_non_root_mode(vcpu, &exit_qual);
12867 if (ret) {
12868 nested_vmx_entry_failure(vcpu, vmcs12, ret, exit_qual);
12869 vmx->nested.nested_run_pending = 0;
12870 return 1;
12871 }
12872
12873 /* Hide L1D cache contents from the nested guest. */
12874 vmx->vcpu.arch.l1tf_flush_l1d = true;
12875
12876 /*
12877 * Must happen outside of enter_vmx_non_root_mode() as it will
12878 * also be used as part of restoring nVMX state for
12879 * snapshot restore (migration).
12880 *
12881 * In this flow, it is assumed that vmcs12 cache was
12882 * trasferred as part of captured nVMX state and should
12883 * therefore not be read from guest memory (which may not
12884 * exist on destination host yet).
12885 */
12886 nested_cache_shadow_vmcs12(vcpu, vmcs12);
12887
12888 /*
12889 * If we're entering a halted L2 vcpu and the L2 vcpu won't be
12890 * awakened by event injection or by an NMI-window VM-exit or
12891 * by an interrupt-window VM-exit, halt the vcpu.
12892 */
12893 if ((vmcs12->guest_activity_state == GUEST_ACTIVITY_HLT) &&
12894 !(vmcs12->vm_entry_intr_info_field & INTR_INFO_VALID_MASK) &&
12895 !(vmcs12->cpu_based_vm_exec_control & CPU_BASED_VIRTUAL_NMI_PENDING) &&
12896 !((vmcs12->cpu_based_vm_exec_control & CPU_BASED_VIRTUAL_INTR_PENDING) &&
12897 (vmcs12->guest_rflags & X86_EFLAGS_IF))) {
12898 vmx->nested.nested_run_pending = 0;
12899 return kvm_vcpu_halt(vcpu);
12900 }
12901 return 1;
12902
12903 out:
12904 return kvm_skip_emulated_instruction(vcpu);
12905 }
12906
12907 /*
12908 * On a nested exit from L2 to L1, vmcs12.guest_cr0 might not be up-to-date
12909 * because L2 may have changed some cr0 bits directly (CRO_GUEST_HOST_MASK).
12910 * This function returns the new value we should put in vmcs12.guest_cr0.
12911 * It's not enough to just return the vmcs02 GUEST_CR0. Rather,
12912 * 1. Bits that neither L0 nor L1 trapped, were set directly by L2 and are now
12913 * available in vmcs02 GUEST_CR0. (Note: It's enough to check that L0
12914 * didn't trap the bit, because if L1 did, so would L0).
12915 * 2. Bits that L1 asked to trap (and therefore L0 also did) could not have
12916 * been modified by L2, and L1 knows it. So just leave the old value of
12917 * the bit from vmcs12.guest_cr0. Note that the bit from vmcs02 GUEST_CR0
12918 * isn't relevant, because if L0 traps this bit it can set it to anything.
12919 * 3. Bits that L1 didn't trap, but L0 did. L1 believes the guest could have
12920 * changed these bits, and therefore they need to be updated, but L0
12921 * didn't necessarily allow them to be changed in GUEST_CR0 - and rather
12922 * put them in vmcs02 CR0_READ_SHADOW. So take these bits from there.
12923 */
12924 static inline unsigned long
vmcs12_guest_cr0(struct kvm_vcpu * vcpu,struct vmcs12 * vmcs12)12925 vmcs12_guest_cr0(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
12926 {
12927 return
12928 /*1*/ (vmcs_readl(GUEST_CR0) & vcpu->arch.cr0_guest_owned_bits) |
12929 /*2*/ (vmcs12->guest_cr0 & vmcs12->cr0_guest_host_mask) |
12930 /*3*/ (vmcs_readl(CR0_READ_SHADOW) & ~(vmcs12->cr0_guest_host_mask |
12931 vcpu->arch.cr0_guest_owned_bits));
12932 }
12933
12934 static inline unsigned long
vmcs12_guest_cr4(struct kvm_vcpu * vcpu,struct vmcs12 * vmcs12)12935 vmcs12_guest_cr4(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
12936 {
12937 return
12938 /*1*/ (vmcs_readl(GUEST_CR4) & vcpu->arch.cr4_guest_owned_bits) |
12939 /*2*/ (vmcs12->guest_cr4 & vmcs12->cr4_guest_host_mask) |
12940 /*3*/ (vmcs_readl(CR4_READ_SHADOW) & ~(vmcs12->cr4_guest_host_mask |
12941 vcpu->arch.cr4_guest_owned_bits));
12942 }
12943
vmcs12_save_pending_event(struct kvm_vcpu * vcpu,struct vmcs12 * vmcs12)12944 static void vmcs12_save_pending_event(struct kvm_vcpu *vcpu,
12945 struct vmcs12 *vmcs12)
12946 {
12947 u32 idt_vectoring;
12948 unsigned int nr;
12949
12950 if (vcpu->arch.exception.injected) {
12951 nr = vcpu->arch.exception.nr;
12952 idt_vectoring = nr | VECTORING_INFO_VALID_MASK;
12953
12954 if (kvm_exception_is_soft(nr)) {
12955 vmcs12->vm_exit_instruction_len =
12956 vcpu->arch.event_exit_inst_len;
12957 idt_vectoring |= INTR_TYPE_SOFT_EXCEPTION;
12958 } else
12959 idt_vectoring |= INTR_TYPE_HARD_EXCEPTION;
12960
12961 if (vcpu->arch.exception.has_error_code) {
12962 idt_vectoring |= VECTORING_INFO_DELIVER_CODE_MASK;
12963 vmcs12->idt_vectoring_error_code =
12964 vcpu->arch.exception.error_code;
12965 }
12966
12967 vmcs12->idt_vectoring_info_field = idt_vectoring;
12968 } else if (vcpu->arch.nmi_injected) {
12969 vmcs12->idt_vectoring_info_field =
12970 INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK | NMI_VECTOR;
12971 } else if (vcpu->arch.interrupt.injected) {
12972 nr = vcpu->arch.interrupt.nr;
12973 idt_vectoring = nr | VECTORING_INFO_VALID_MASK;
12974
12975 if (vcpu->arch.interrupt.soft) {
12976 idt_vectoring |= INTR_TYPE_SOFT_INTR;
12977 vmcs12->vm_entry_instruction_len =
12978 vcpu->arch.event_exit_inst_len;
12979 } else
12980 idt_vectoring |= INTR_TYPE_EXT_INTR;
12981
12982 vmcs12->idt_vectoring_info_field = idt_vectoring;
12983 }
12984 }
12985
vmx_check_nested_events(struct kvm_vcpu * vcpu)12986 static int vmx_check_nested_events(struct kvm_vcpu *vcpu)
12987 {
12988 struct vcpu_vmx *vmx = to_vmx(vcpu);
12989 unsigned long exit_qual;
12990 bool block_nested_events =
12991 vmx->nested.nested_run_pending || kvm_event_needs_reinjection(vcpu);
12992
12993 if (vcpu->arch.exception.pending &&
12994 nested_vmx_check_exception(vcpu, &exit_qual)) {
12995 if (block_nested_events)
12996 return -EBUSY;
12997 nested_vmx_inject_exception_vmexit(vcpu, exit_qual);
12998 return 0;
12999 }
13000
13001 if (nested_cpu_has_preemption_timer(get_vmcs12(vcpu)) &&
13002 vmx->nested.preemption_timer_expired) {
13003 if (block_nested_events)
13004 return -EBUSY;
13005 nested_vmx_vmexit(vcpu, EXIT_REASON_PREEMPTION_TIMER, 0, 0);
13006 return 0;
13007 }
13008
13009 if (vcpu->arch.nmi_pending && nested_exit_on_nmi(vcpu)) {
13010 if (block_nested_events)
13011 return -EBUSY;
13012 nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI,
13013 NMI_VECTOR | INTR_TYPE_NMI_INTR |
13014 INTR_INFO_VALID_MASK, 0);
13015 /*
13016 * The NMI-triggered VM exit counts as injection:
13017 * clear this one and block further NMIs.
13018 */
13019 vcpu->arch.nmi_pending = 0;
13020 vmx_set_nmi_mask(vcpu, true);
13021 return 0;
13022 }
13023
13024 if (kvm_cpu_has_interrupt(vcpu) && nested_exit_on_intr(vcpu)) {
13025 if (block_nested_events)
13026 return -EBUSY;
13027 nested_vmx_vmexit(vcpu, EXIT_REASON_EXTERNAL_INTERRUPT, 0, 0);
13028 return 0;
13029 }
13030
13031 vmx_complete_nested_posted_interrupt(vcpu);
13032 return 0;
13033 }
13034
vmx_request_immediate_exit(struct kvm_vcpu * vcpu)13035 static void vmx_request_immediate_exit(struct kvm_vcpu *vcpu)
13036 {
13037 to_vmx(vcpu)->req_immediate_exit = true;
13038 }
13039
vmx_get_preemption_timer_value(struct kvm_vcpu * vcpu)13040 static u32 vmx_get_preemption_timer_value(struct kvm_vcpu *vcpu)
13041 {
13042 ktime_t remaining =
13043 hrtimer_get_remaining(&to_vmx(vcpu)->nested.preemption_timer);
13044 u64 value;
13045
13046 if (ktime_to_ns(remaining) <= 0)
13047 return 0;
13048
13049 value = ktime_to_ns(remaining) * vcpu->arch.virtual_tsc_khz;
13050 do_div(value, 1000000);
13051 return value >> VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE;
13052 }
13053
13054 /*
13055 * Update the guest state fields of vmcs12 to reflect changes that
13056 * occurred while L2 was running. (The "IA-32e mode guest" bit of the
13057 * VM-entry controls is also updated, since this is really a guest
13058 * state bit.)
13059 */
sync_vmcs12(struct kvm_vcpu * vcpu,struct vmcs12 * vmcs12)13060 static void sync_vmcs12(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
13061 {
13062 vmcs12->guest_cr0 = vmcs12_guest_cr0(vcpu, vmcs12);
13063 vmcs12->guest_cr4 = vmcs12_guest_cr4(vcpu, vmcs12);
13064
13065 vmcs12->guest_rsp = kvm_register_read(vcpu, VCPU_REGS_RSP);
13066 vmcs12->guest_rip = kvm_register_read(vcpu, VCPU_REGS_RIP);
13067 vmcs12->guest_rflags = vmcs_readl(GUEST_RFLAGS);
13068
13069 vmcs12->guest_es_selector = vmcs_read16(GUEST_ES_SELECTOR);
13070 vmcs12->guest_cs_selector = vmcs_read16(GUEST_CS_SELECTOR);
13071 vmcs12->guest_ss_selector = vmcs_read16(GUEST_SS_SELECTOR);
13072 vmcs12->guest_ds_selector = vmcs_read16(GUEST_DS_SELECTOR);
13073 vmcs12->guest_fs_selector = vmcs_read16(GUEST_FS_SELECTOR);
13074 vmcs12->guest_gs_selector = vmcs_read16(GUEST_GS_SELECTOR);
13075 vmcs12->guest_ldtr_selector = vmcs_read16(GUEST_LDTR_SELECTOR);
13076 vmcs12->guest_tr_selector = vmcs_read16(GUEST_TR_SELECTOR);
13077 vmcs12->guest_es_limit = vmcs_read32(GUEST_ES_LIMIT);
13078 vmcs12->guest_cs_limit = vmcs_read32(GUEST_CS_LIMIT);
13079 vmcs12->guest_ss_limit = vmcs_read32(GUEST_SS_LIMIT);
13080 vmcs12->guest_ds_limit = vmcs_read32(GUEST_DS_LIMIT);
13081 vmcs12->guest_fs_limit = vmcs_read32(GUEST_FS_LIMIT);
13082 vmcs12->guest_gs_limit = vmcs_read32(GUEST_GS_LIMIT);
13083 vmcs12->guest_ldtr_limit = vmcs_read32(GUEST_LDTR_LIMIT);
13084 vmcs12->guest_tr_limit = vmcs_read32(GUEST_TR_LIMIT);
13085 vmcs12->guest_gdtr_limit = vmcs_read32(GUEST_GDTR_LIMIT);
13086 vmcs12->guest_idtr_limit = vmcs_read32(GUEST_IDTR_LIMIT);
13087 vmcs12->guest_es_ar_bytes = vmcs_read32(GUEST_ES_AR_BYTES);
13088 vmcs12->guest_cs_ar_bytes = vmcs_read32(GUEST_CS_AR_BYTES);
13089 vmcs12->guest_ss_ar_bytes = vmcs_read32(GUEST_SS_AR_BYTES);
13090 vmcs12->guest_ds_ar_bytes = vmcs_read32(GUEST_DS_AR_BYTES);
13091 vmcs12->guest_fs_ar_bytes = vmcs_read32(GUEST_FS_AR_BYTES);
13092 vmcs12->guest_gs_ar_bytes = vmcs_read32(GUEST_GS_AR_BYTES);
13093 vmcs12->guest_ldtr_ar_bytes = vmcs_read32(GUEST_LDTR_AR_BYTES);
13094 vmcs12->guest_tr_ar_bytes = vmcs_read32(GUEST_TR_AR_BYTES);
13095 vmcs12->guest_es_base = vmcs_readl(GUEST_ES_BASE);
13096 vmcs12->guest_cs_base = vmcs_readl(GUEST_CS_BASE);
13097 vmcs12->guest_ss_base = vmcs_readl(GUEST_SS_BASE);
13098 vmcs12->guest_ds_base = vmcs_readl(GUEST_DS_BASE);
13099 vmcs12->guest_fs_base = vmcs_readl(GUEST_FS_BASE);
13100 vmcs12->guest_gs_base = vmcs_readl(GUEST_GS_BASE);
13101 vmcs12->guest_ldtr_base = vmcs_readl(GUEST_LDTR_BASE);
13102 vmcs12->guest_tr_base = vmcs_readl(GUEST_TR_BASE);
13103 vmcs12->guest_gdtr_base = vmcs_readl(GUEST_GDTR_BASE);
13104 vmcs12->guest_idtr_base = vmcs_readl(GUEST_IDTR_BASE);
13105
13106 vmcs12->guest_interruptibility_info =
13107 vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
13108 vmcs12->guest_pending_dbg_exceptions =
13109 vmcs_readl(GUEST_PENDING_DBG_EXCEPTIONS);
13110 if (vcpu->arch.mp_state == KVM_MP_STATE_HALTED)
13111 vmcs12->guest_activity_state = GUEST_ACTIVITY_HLT;
13112 else
13113 vmcs12->guest_activity_state = GUEST_ACTIVITY_ACTIVE;
13114
13115 if (nested_cpu_has_preemption_timer(vmcs12)) {
13116 if (vmcs12->vm_exit_controls &
13117 VM_EXIT_SAVE_VMX_PREEMPTION_TIMER)
13118 vmcs12->vmx_preemption_timer_value =
13119 vmx_get_preemption_timer_value(vcpu);
13120 hrtimer_cancel(&to_vmx(vcpu)->nested.preemption_timer);
13121 }
13122
13123 /*
13124 * In some cases (usually, nested EPT), L2 is allowed to change its
13125 * own CR3 without exiting. If it has changed it, we must keep it.
13126 * Of course, if L0 is using shadow page tables, GUEST_CR3 was defined
13127 * by L0, not L1 or L2, so we mustn't unconditionally copy it to vmcs12.
13128 *
13129 * Additionally, restore L2's PDPTR to vmcs12.
13130 */
13131 if (enable_ept) {
13132 vmcs12->guest_cr3 = vmcs_readl(GUEST_CR3);
13133 vmcs12->guest_pdptr0 = vmcs_read64(GUEST_PDPTR0);
13134 vmcs12->guest_pdptr1 = vmcs_read64(GUEST_PDPTR1);
13135 vmcs12->guest_pdptr2 = vmcs_read64(GUEST_PDPTR2);
13136 vmcs12->guest_pdptr3 = vmcs_read64(GUEST_PDPTR3);
13137 }
13138
13139 vmcs12->guest_linear_address = vmcs_readl(GUEST_LINEAR_ADDRESS);
13140
13141 if (nested_cpu_has_vid(vmcs12))
13142 vmcs12->guest_intr_status = vmcs_read16(GUEST_INTR_STATUS);
13143
13144 vmcs12->vm_entry_controls =
13145 (vmcs12->vm_entry_controls & ~VM_ENTRY_IA32E_MODE) |
13146 (vm_entry_controls_get(to_vmx(vcpu)) & VM_ENTRY_IA32E_MODE);
13147
13148 if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_DEBUG_CONTROLS) {
13149 kvm_get_dr(vcpu, 7, (unsigned long *)&vmcs12->guest_dr7);
13150 vmcs12->guest_ia32_debugctl = vmcs_read64(GUEST_IA32_DEBUGCTL);
13151 }
13152
13153 /* TODO: These cannot have changed unless we have MSR bitmaps and
13154 * the relevant bit asks not to trap the change */
13155 if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_IA32_PAT)
13156 vmcs12->guest_ia32_pat = vmcs_read64(GUEST_IA32_PAT);
13157 if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_IA32_EFER)
13158 vmcs12->guest_ia32_efer = vcpu->arch.efer;
13159 vmcs12->guest_sysenter_cs = vmcs_read32(GUEST_SYSENTER_CS);
13160 vmcs12->guest_sysenter_esp = vmcs_readl(GUEST_SYSENTER_ESP);
13161 vmcs12->guest_sysenter_eip = vmcs_readl(GUEST_SYSENTER_EIP);
13162 if (kvm_mpx_supported())
13163 vmcs12->guest_bndcfgs = vmcs_read64(GUEST_BNDCFGS);
13164 }
13165
13166 /*
13167 * prepare_vmcs12 is part of what we need to do when the nested L2 guest exits
13168 * and we want to prepare to run its L1 parent. L1 keeps a vmcs for L2 (vmcs12),
13169 * and this function updates it to reflect the changes to the guest state while
13170 * L2 was running (and perhaps made some exits which were handled directly by L0
13171 * without going back to L1), and to reflect the exit reason.
13172 * Note that we do not have to copy here all VMCS fields, just those that
13173 * could have changed by the L2 guest or the exit - i.e., the guest-state and
13174 * exit-information fields only. Other fields are modified by L1 with VMWRITE,
13175 * which already writes to vmcs12 directly.
13176 */
prepare_vmcs12(struct kvm_vcpu * vcpu,struct vmcs12 * vmcs12,u32 exit_reason,u32 exit_intr_info,unsigned long exit_qualification)13177 static void prepare_vmcs12(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12,
13178 u32 exit_reason, u32 exit_intr_info,
13179 unsigned long exit_qualification)
13180 {
13181 /* update guest state fields: */
13182 sync_vmcs12(vcpu, vmcs12);
13183
13184 /* update exit information fields: */
13185
13186 vmcs12->vm_exit_reason = exit_reason;
13187 vmcs12->exit_qualification = exit_qualification;
13188 vmcs12->vm_exit_intr_info = exit_intr_info;
13189
13190 vmcs12->idt_vectoring_info_field = 0;
13191 vmcs12->vm_exit_instruction_len = vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
13192 vmcs12->vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
13193
13194 if (!(vmcs12->vm_exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY)) {
13195 vmcs12->launch_state = 1;
13196
13197 /* vm_entry_intr_info_field is cleared on exit. Emulate this
13198 * instead of reading the real value. */
13199 vmcs12->vm_entry_intr_info_field &= ~INTR_INFO_VALID_MASK;
13200
13201 /*
13202 * Transfer the event that L0 or L1 may wanted to inject into
13203 * L2 to IDT_VECTORING_INFO_FIELD.
13204 */
13205 vmcs12_save_pending_event(vcpu, vmcs12);
13206 }
13207
13208 /*
13209 * Drop what we picked up for L2 via vmx_complete_interrupts. It is
13210 * preserved above and would only end up incorrectly in L1.
13211 */
13212 vcpu->arch.nmi_injected = false;
13213 kvm_clear_exception_queue(vcpu);
13214 kvm_clear_interrupt_queue(vcpu);
13215 }
13216
13217 /*
13218 * A part of what we need to when the nested L2 guest exits and we want to
13219 * run its L1 parent, is to reset L1's guest state to the host state specified
13220 * in vmcs12.
13221 * This function is to be called not only on normal nested exit, but also on
13222 * a nested entry failure, as explained in Intel's spec, 3B.23.7 ("VM-Entry
13223 * Failures During or After Loading Guest State").
13224 * This function should be called when the active VMCS is L1's (vmcs01).
13225 */
load_vmcs12_host_state(struct kvm_vcpu * vcpu,struct vmcs12 * vmcs12)13226 static void load_vmcs12_host_state(struct kvm_vcpu *vcpu,
13227 struct vmcs12 *vmcs12)
13228 {
13229 struct kvm_segment seg;
13230 u32 entry_failure_code;
13231
13232 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_EFER)
13233 vcpu->arch.efer = vmcs12->host_ia32_efer;
13234 else if (vmcs12->vm_exit_controls & VM_EXIT_HOST_ADDR_SPACE_SIZE)
13235 vcpu->arch.efer |= (EFER_LMA | EFER_LME);
13236 else
13237 vcpu->arch.efer &= ~(EFER_LMA | EFER_LME);
13238 vmx_set_efer(vcpu, vcpu->arch.efer);
13239
13240 kvm_register_write(vcpu, VCPU_REGS_RSP, vmcs12->host_rsp);
13241 kvm_register_write(vcpu, VCPU_REGS_RIP, vmcs12->host_rip);
13242 vmx_set_rflags(vcpu, X86_EFLAGS_FIXED);
13243 /*
13244 * Note that calling vmx_set_cr0 is important, even if cr0 hasn't
13245 * actually changed, because vmx_set_cr0 refers to efer set above.
13246 *
13247 * CR0_GUEST_HOST_MASK is already set in the original vmcs01
13248 * (KVM doesn't change it);
13249 */
13250 vcpu->arch.cr0_guest_owned_bits = X86_CR0_TS;
13251 vmx_set_cr0(vcpu, vmcs12->host_cr0);
13252
13253 /* Same as above - no reason to call set_cr4_guest_host_mask(). */
13254 vcpu->arch.cr4_guest_owned_bits = ~vmcs_readl(CR4_GUEST_HOST_MASK);
13255 vmx_set_cr4(vcpu, vmcs12->host_cr4);
13256
13257 nested_ept_uninit_mmu_context(vcpu);
13258
13259 /*
13260 * Only PDPTE load can fail as the value of cr3 was checked on entry and
13261 * couldn't have changed.
13262 */
13263 if (nested_vmx_load_cr3(vcpu, vmcs12->host_cr3, false, &entry_failure_code))
13264 nested_vmx_abort(vcpu, VMX_ABORT_LOAD_HOST_PDPTE_FAIL);
13265
13266 if (!enable_ept)
13267 vcpu->arch.walk_mmu->inject_page_fault = kvm_inject_page_fault;
13268
13269 /*
13270 * If vmcs01 don't use VPID, CPU flushes TLB on every
13271 * VMEntry/VMExit. Thus, no need to flush TLB.
13272 *
13273 * If vmcs12 uses VPID, TLB entries populated by L2 are
13274 * tagged with vmx->nested.vpid02 while L1 entries are tagged
13275 * with vmx->vpid. Thus, no need to flush TLB.
13276 *
13277 * Therefore, flush TLB only in case vmcs01 uses VPID and
13278 * vmcs12 don't use VPID as in this case L1 & L2 TLB entries
13279 * are both tagged with vmx->vpid.
13280 */
13281 if (enable_vpid &&
13282 !(nested_cpu_has_vpid(vmcs12) && to_vmx(vcpu)->nested.vpid02)) {
13283 vmx_flush_tlb(vcpu, true);
13284 }
13285
13286 vmcs_write32(GUEST_SYSENTER_CS, vmcs12->host_ia32_sysenter_cs);
13287 vmcs_writel(GUEST_SYSENTER_ESP, vmcs12->host_ia32_sysenter_esp);
13288 vmcs_writel(GUEST_SYSENTER_EIP, vmcs12->host_ia32_sysenter_eip);
13289 vmcs_writel(GUEST_IDTR_BASE, vmcs12->host_idtr_base);
13290 vmcs_writel(GUEST_GDTR_BASE, vmcs12->host_gdtr_base);
13291 vmcs_write32(GUEST_IDTR_LIMIT, 0xFFFF);
13292 vmcs_write32(GUEST_GDTR_LIMIT, 0xFFFF);
13293
13294 /* If not VM_EXIT_CLEAR_BNDCFGS, the L2 value propagates to L1. */
13295 if (vmcs12->vm_exit_controls & VM_EXIT_CLEAR_BNDCFGS)
13296 vmcs_write64(GUEST_BNDCFGS, 0);
13297
13298 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PAT) {
13299 vmcs_write64(GUEST_IA32_PAT, vmcs12->host_ia32_pat);
13300 vcpu->arch.pat = vmcs12->host_ia32_pat;
13301 }
13302 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL)
13303 vmcs_write64(GUEST_IA32_PERF_GLOBAL_CTRL,
13304 vmcs12->host_ia32_perf_global_ctrl);
13305
13306 /* Set L1 segment info according to Intel SDM
13307 27.5.2 Loading Host Segment and Descriptor-Table Registers */
13308 seg = (struct kvm_segment) {
13309 .base = 0,
13310 .limit = 0xFFFFFFFF,
13311 .selector = vmcs12->host_cs_selector,
13312 .type = 11,
13313 .present = 1,
13314 .s = 1,
13315 .g = 1
13316 };
13317 if (vmcs12->vm_exit_controls & VM_EXIT_HOST_ADDR_SPACE_SIZE)
13318 seg.l = 1;
13319 else
13320 seg.db = 1;
13321 vmx_set_segment(vcpu, &seg, VCPU_SREG_CS);
13322 seg = (struct kvm_segment) {
13323 .base = 0,
13324 .limit = 0xFFFFFFFF,
13325 .type = 3,
13326 .present = 1,
13327 .s = 1,
13328 .db = 1,
13329 .g = 1
13330 };
13331 seg.selector = vmcs12->host_ds_selector;
13332 vmx_set_segment(vcpu, &seg, VCPU_SREG_DS);
13333 seg.selector = vmcs12->host_es_selector;
13334 vmx_set_segment(vcpu, &seg, VCPU_SREG_ES);
13335 seg.selector = vmcs12->host_ss_selector;
13336 vmx_set_segment(vcpu, &seg, VCPU_SREG_SS);
13337 seg.selector = vmcs12->host_fs_selector;
13338 seg.base = vmcs12->host_fs_base;
13339 vmx_set_segment(vcpu, &seg, VCPU_SREG_FS);
13340 seg.selector = vmcs12->host_gs_selector;
13341 seg.base = vmcs12->host_gs_base;
13342 vmx_set_segment(vcpu, &seg, VCPU_SREG_GS);
13343 seg = (struct kvm_segment) {
13344 .base = vmcs12->host_tr_base,
13345 .limit = 0x67,
13346 .selector = vmcs12->host_tr_selector,
13347 .type = 11,
13348 .present = 1
13349 };
13350 vmx_set_segment(vcpu, &seg, VCPU_SREG_TR);
13351
13352 kvm_set_dr(vcpu, 7, 0x400);
13353 vmcs_write64(GUEST_IA32_DEBUGCTL, 0);
13354
13355 if (cpu_has_vmx_msr_bitmap())
13356 vmx_update_msr_bitmap(vcpu);
13357
13358 if (nested_vmx_load_msr(vcpu, vmcs12->vm_exit_msr_load_addr,
13359 vmcs12->vm_exit_msr_load_count))
13360 nested_vmx_abort(vcpu, VMX_ABORT_LOAD_HOST_MSR_FAIL);
13361 }
13362
nested_vmx_get_vmcs01_guest_efer(struct vcpu_vmx * vmx)13363 static inline u64 nested_vmx_get_vmcs01_guest_efer(struct vcpu_vmx *vmx)
13364 {
13365 struct shared_msr_entry *efer_msr;
13366 unsigned int i;
13367
13368 if (vm_entry_controls_get(vmx) & VM_ENTRY_LOAD_IA32_EFER)
13369 return vmcs_read64(GUEST_IA32_EFER);
13370
13371 if (cpu_has_load_ia32_efer)
13372 return host_efer;
13373
13374 for (i = 0; i < vmx->msr_autoload.guest.nr; ++i) {
13375 if (vmx->msr_autoload.guest.val[i].index == MSR_EFER)
13376 return vmx->msr_autoload.guest.val[i].value;
13377 }
13378
13379 efer_msr = find_msr_entry(vmx, MSR_EFER);
13380 if (efer_msr)
13381 return efer_msr->data;
13382
13383 return host_efer;
13384 }
13385
nested_vmx_restore_host_state(struct kvm_vcpu * vcpu)13386 static void nested_vmx_restore_host_state(struct kvm_vcpu *vcpu)
13387 {
13388 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
13389 struct vcpu_vmx *vmx = to_vmx(vcpu);
13390 struct vmx_msr_entry g, h;
13391 struct msr_data msr;
13392 gpa_t gpa;
13393 u32 i, j;
13394
13395 vcpu->arch.pat = vmcs_read64(GUEST_IA32_PAT);
13396
13397 if (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS) {
13398 /*
13399 * L1's host DR7 is lost if KVM_GUESTDBG_USE_HW_BP is set
13400 * as vmcs01.GUEST_DR7 contains a userspace defined value
13401 * and vcpu->arch.dr7 is not squirreled away before the
13402 * nested VMENTER (not worth adding a variable in nested_vmx).
13403 */
13404 if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP)
13405 kvm_set_dr(vcpu, 7, DR7_FIXED_1);
13406 else
13407 WARN_ON(kvm_set_dr(vcpu, 7, vmcs_readl(GUEST_DR7)));
13408 }
13409
13410 /*
13411 * Note that calling vmx_set_{efer,cr0,cr4} is important as they
13412 * handle a variety of side effects to KVM's software model.
13413 */
13414 vmx_set_efer(vcpu, nested_vmx_get_vmcs01_guest_efer(vmx));
13415
13416 vcpu->arch.cr0_guest_owned_bits = X86_CR0_TS;
13417 vmx_set_cr0(vcpu, vmcs_readl(CR0_READ_SHADOW));
13418
13419 vcpu->arch.cr4_guest_owned_bits = ~vmcs_readl(CR4_GUEST_HOST_MASK);
13420 vmx_set_cr4(vcpu, vmcs_readl(CR4_READ_SHADOW));
13421
13422 nested_ept_uninit_mmu_context(vcpu);
13423 vcpu->arch.cr3 = vmcs_readl(GUEST_CR3);
13424 __set_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail);
13425
13426 /*
13427 * Use ept_save_pdptrs(vcpu) to load the MMU's cached PDPTRs
13428 * from vmcs01 (if necessary). The PDPTRs are not loaded on
13429 * VMFail, like everything else we just need to ensure our
13430 * software model is up-to-date.
13431 */
13432 ept_save_pdptrs(vcpu);
13433
13434 kvm_mmu_reset_context(vcpu);
13435
13436 if (cpu_has_vmx_msr_bitmap())
13437 vmx_update_msr_bitmap(vcpu);
13438
13439 /*
13440 * This nasty bit of open coding is a compromise between blindly
13441 * loading L1's MSRs using the exit load lists (incorrect emulation
13442 * of VMFail), leaving the nested VM's MSRs in the software model
13443 * (incorrect behavior) and snapshotting the modified MSRs (too
13444 * expensive since the lists are unbound by hardware). For each
13445 * MSR that was (prematurely) loaded from the nested VMEntry load
13446 * list, reload it from the exit load list if it exists and differs
13447 * from the guest value. The intent is to stuff host state as
13448 * silently as possible, not to fully process the exit load list.
13449 */
13450 msr.host_initiated = false;
13451 for (i = 0; i < vmcs12->vm_entry_msr_load_count; i++) {
13452 gpa = vmcs12->vm_entry_msr_load_addr + (i * sizeof(g));
13453 if (kvm_vcpu_read_guest(vcpu, gpa, &g, sizeof(g))) {
13454 pr_debug_ratelimited(
13455 "%s read MSR index failed (%u, 0x%08llx)\n",
13456 __func__, i, gpa);
13457 goto vmabort;
13458 }
13459
13460 for (j = 0; j < vmcs12->vm_exit_msr_load_count; j++) {
13461 gpa = vmcs12->vm_exit_msr_load_addr + (j * sizeof(h));
13462 if (kvm_vcpu_read_guest(vcpu, gpa, &h, sizeof(h))) {
13463 pr_debug_ratelimited(
13464 "%s read MSR failed (%u, 0x%08llx)\n",
13465 __func__, j, gpa);
13466 goto vmabort;
13467 }
13468 if (h.index != g.index)
13469 continue;
13470 if (h.value == g.value)
13471 break;
13472
13473 if (nested_vmx_load_msr_check(vcpu, &h)) {
13474 pr_debug_ratelimited(
13475 "%s check failed (%u, 0x%x, 0x%x)\n",
13476 __func__, j, h.index, h.reserved);
13477 goto vmabort;
13478 }
13479
13480 msr.index = h.index;
13481 msr.data = h.value;
13482 if (kvm_set_msr(vcpu, &msr)) {
13483 pr_debug_ratelimited(
13484 "%s WRMSR failed (%u, 0x%x, 0x%llx)\n",
13485 __func__, j, h.index, h.value);
13486 goto vmabort;
13487 }
13488 }
13489 }
13490
13491 return;
13492
13493 vmabort:
13494 nested_vmx_abort(vcpu, VMX_ABORT_LOAD_HOST_MSR_FAIL);
13495 }
13496
13497 /*
13498 * Emulate an exit from nested guest (L2) to L1, i.e., prepare to run L1
13499 * and modify vmcs12 to make it see what it would expect to see there if
13500 * L2 was its real guest. Must only be called when in L2 (is_guest_mode())
13501 */
nested_vmx_vmexit(struct kvm_vcpu * vcpu,u32 exit_reason,u32 exit_intr_info,unsigned long exit_qualification)13502 static void nested_vmx_vmexit(struct kvm_vcpu *vcpu, u32 exit_reason,
13503 u32 exit_intr_info,
13504 unsigned long exit_qualification)
13505 {
13506 struct vcpu_vmx *vmx = to_vmx(vcpu);
13507 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
13508
13509 /* trying to cancel vmlaunch/vmresume is a bug */
13510 WARN_ON_ONCE(vmx->nested.nested_run_pending);
13511
13512 /*
13513 * The only expected VM-instruction error is "VM entry with
13514 * invalid control field(s)." Anything else indicates a
13515 * problem with L0.
13516 */
13517 WARN_ON_ONCE(vmx->fail && (vmcs_read32(VM_INSTRUCTION_ERROR) !=
13518 VMXERR_ENTRY_INVALID_CONTROL_FIELD));
13519
13520 leave_guest_mode(vcpu);
13521
13522 if (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING)
13523 vcpu->arch.tsc_offset -= vmcs12->tsc_offset;
13524
13525 if (likely(!vmx->fail)) {
13526 if (exit_reason == -1)
13527 sync_vmcs12(vcpu, vmcs12);
13528 else
13529 prepare_vmcs12(vcpu, vmcs12, exit_reason, exit_intr_info,
13530 exit_qualification);
13531
13532 /*
13533 * Must happen outside of sync_vmcs12() as it will
13534 * also be used to capture vmcs12 cache as part of
13535 * capturing nVMX state for snapshot (migration).
13536 *
13537 * Otherwise, this flush will dirty guest memory at a
13538 * point it is already assumed by user-space to be
13539 * immutable.
13540 */
13541 nested_flush_cached_shadow_vmcs12(vcpu, vmcs12);
13542
13543 if (nested_vmx_store_msr(vcpu, vmcs12->vm_exit_msr_store_addr,
13544 vmcs12->vm_exit_msr_store_count))
13545 nested_vmx_abort(vcpu, VMX_ABORT_SAVE_GUEST_MSR_FAIL);
13546 }
13547
13548 vmx_switch_vmcs(vcpu, &vmx->vmcs01);
13549 vm_entry_controls_reset_shadow(vmx);
13550 vm_exit_controls_reset_shadow(vmx);
13551 vmx_segment_cache_clear(vmx);
13552
13553 /* Update any VMCS fields that might have changed while L2 ran */
13554 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, vmx->msr_autoload.host.nr);
13555 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, vmx->msr_autoload.guest.nr);
13556 vmcs_write64(TSC_OFFSET, vcpu->arch.tsc_offset);
13557
13558 if (kvm_has_tsc_control)
13559 decache_tsc_multiplier(vmx);
13560
13561 if (vmx->nested.change_vmcs01_virtual_apic_mode) {
13562 vmx->nested.change_vmcs01_virtual_apic_mode = false;
13563 vmx_set_virtual_apic_mode(vcpu);
13564 } else if (!nested_cpu_has_ept(vmcs12) &&
13565 nested_cpu_has2(vmcs12,
13566 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) {
13567 vmx_flush_tlb(vcpu, true);
13568 }
13569
13570 /* This is needed for same reason as it was needed in prepare_vmcs02 */
13571 vmx->host_rsp = 0;
13572
13573 /* Unpin physical memory we referred to in vmcs02 */
13574 if (vmx->nested.apic_access_page) {
13575 kvm_release_page_dirty(vmx->nested.apic_access_page);
13576 vmx->nested.apic_access_page = NULL;
13577 }
13578 if (vmx->nested.virtual_apic_page) {
13579 kvm_release_page_dirty(vmx->nested.virtual_apic_page);
13580 vmx->nested.virtual_apic_page = NULL;
13581 }
13582 if (vmx->nested.pi_desc_page) {
13583 kunmap(vmx->nested.pi_desc_page);
13584 kvm_release_page_dirty(vmx->nested.pi_desc_page);
13585 vmx->nested.pi_desc_page = NULL;
13586 vmx->nested.pi_desc = NULL;
13587 }
13588
13589 /*
13590 * We are now running in L2, mmu_notifier will force to reload the
13591 * page's hpa for L2 vmcs. Need to reload it for L1 before entering L1.
13592 */
13593 kvm_make_request(KVM_REQ_APIC_PAGE_RELOAD, vcpu);
13594
13595 if (enable_shadow_vmcs && exit_reason != -1)
13596 vmx->nested.sync_shadow_vmcs = true;
13597
13598 /* in case we halted in L2 */
13599 vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
13600
13601 if (likely(!vmx->fail)) {
13602 if (exit_reason == EXIT_REASON_EXTERNAL_INTERRUPT &&
13603 nested_exit_intr_ack_set(vcpu)) {
13604 int irq = kvm_cpu_get_interrupt(vcpu);
13605 WARN_ON(irq < 0);
13606 vmcs12->vm_exit_intr_info = irq |
13607 INTR_INFO_VALID_MASK | INTR_TYPE_EXT_INTR;
13608 }
13609
13610 if (exit_reason != -1)
13611 trace_kvm_nested_vmexit_inject(vmcs12->vm_exit_reason,
13612 vmcs12->exit_qualification,
13613 vmcs12->idt_vectoring_info_field,
13614 vmcs12->vm_exit_intr_info,
13615 vmcs12->vm_exit_intr_error_code,
13616 KVM_ISA_VMX);
13617
13618 load_vmcs12_host_state(vcpu, vmcs12);
13619
13620 return;
13621 }
13622
13623 /*
13624 * After an early L2 VM-entry failure, we're now back
13625 * in L1 which thinks it just finished a VMLAUNCH or
13626 * VMRESUME instruction, so we need to set the failure
13627 * flag and the VM-instruction error field of the VMCS
13628 * accordingly.
13629 */
13630 nested_vmx_failValid(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD);
13631
13632 /*
13633 * Restore L1's host state to KVM's software model. We're here
13634 * because a consistency check was caught by hardware, which
13635 * means some amount of guest state has been propagated to KVM's
13636 * model and needs to be unwound to the host's state.
13637 */
13638 nested_vmx_restore_host_state(vcpu);
13639
13640 /*
13641 * The emulated instruction was already skipped in
13642 * nested_vmx_run, but the updated RIP was never
13643 * written back to the vmcs01.
13644 */
13645 skip_emulated_instruction(vcpu);
13646 vmx->fail = 0;
13647 }
13648
13649 /*
13650 * Forcibly leave nested mode in order to be able to reset the VCPU later on.
13651 */
vmx_leave_nested(struct kvm_vcpu * vcpu)13652 static void vmx_leave_nested(struct kvm_vcpu *vcpu)
13653 {
13654 if (is_guest_mode(vcpu)) {
13655 to_vmx(vcpu)->nested.nested_run_pending = 0;
13656 nested_vmx_vmexit(vcpu, -1, 0, 0);
13657 }
13658 free_nested(to_vmx(vcpu));
13659 }
13660
13661 /*
13662 * L1's failure to enter L2 is a subset of a normal exit, as explained in
13663 * 23.7 "VM-entry failures during or after loading guest state" (this also
13664 * lists the acceptable exit-reason and exit-qualification parameters).
13665 * It should only be called before L2 actually succeeded to run, and when
13666 * vmcs01 is current (it doesn't leave_guest_mode() or switch vmcss).
13667 */
nested_vmx_entry_failure(struct kvm_vcpu * vcpu,struct vmcs12 * vmcs12,u32 reason,unsigned long qualification)13668 static void nested_vmx_entry_failure(struct kvm_vcpu *vcpu,
13669 struct vmcs12 *vmcs12,
13670 u32 reason, unsigned long qualification)
13671 {
13672 load_vmcs12_host_state(vcpu, vmcs12);
13673 vmcs12->vm_exit_reason = reason | VMX_EXIT_REASONS_FAILED_VMENTRY;
13674 vmcs12->exit_qualification = qualification;
13675 nested_vmx_succeed(vcpu);
13676 if (enable_shadow_vmcs)
13677 to_vmx(vcpu)->nested.sync_shadow_vmcs = true;
13678 }
13679
vmx_check_intercept_io(struct kvm_vcpu * vcpu,struct x86_instruction_info * info)13680 static int vmx_check_intercept_io(struct kvm_vcpu *vcpu,
13681 struct x86_instruction_info *info)
13682 {
13683 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
13684 unsigned short port;
13685 bool intercept;
13686 int size;
13687
13688 if (info->intercept == x86_intercept_in ||
13689 info->intercept == x86_intercept_ins) {
13690 port = info->src_val;
13691 size = info->dst_bytes;
13692 } else {
13693 port = info->dst_val;
13694 size = info->src_bytes;
13695 }
13696
13697 /*
13698 * If the 'use IO bitmaps' VM-execution control is 0, IO instruction
13699 * VM-exits depend on the 'unconditional IO exiting' VM-execution
13700 * control.
13701 *
13702 * Otherwise, IO instruction VM-exits are controlled by the IO bitmaps.
13703 */
13704 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_IO_BITMAPS))
13705 intercept = nested_cpu_has(vmcs12,
13706 CPU_BASED_UNCOND_IO_EXITING);
13707 else
13708 intercept = nested_vmx_check_io_bitmaps(vcpu, port, size);
13709
13710 /* FIXME: produce nested vmexit and return X86EMUL_INTERCEPTED. */
13711 return intercept ? X86EMUL_UNHANDLEABLE : X86EMUL_CONTINUE;
13712 }
13713
vmx_check_intercept(struct kvm_vcpu * vcpu,struct x86_instruction_info * info,enum x86_intercept_stage stage)13714 static int vmx_check_intercept(struct kvm_vcpu *vcpu,
13715 struct x86_instruction_info *info,
13716 enum x86_intercept_stage stage)
13717 {
13718 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
13719 struct x86_emulate_ctxt *ctxt = &vcpu->arch.emulate_ctxt;
13720
13721 switch (info->intercept) {
13722 /*
13723 * RDPID causes #UD if disabled through secondary execution controls.
13724 * Because it is marked as EmulateOnUD, we need to intercept it here.
13725 */
13726 case x86_intercept_rdtscp:
13727 if (!nested_cpu_has2(vmcs12, SECONDARY_EXEC_RDTSCP)) {
13728 ctxt->exception.vector = UD_VECTOR;
13729 ctxt->exception.error_code_valid = false;
13730 return X86EMUL_PROPAGATE_FAULT;
13731 }
13732 break;
13733
13734 case x86_intercept_in:
13735 case x86_intercept_ins:
13736 case x86_intercept_out:
13737 case x86_intercept_outs:
13738 return vmx_check_intercept_io(vcpu, info);
13739
13740 case x86_intercept_lgdt:
13741 case x86_intercept_lidt:
13742 case x86_intercept_lldt:
13743 case x86_intercept_ltr:
13744 case x86_intercept_sgdt:
13745 case x86_intercept_sidt:
13746 case x86_intercept_sldt:
13747 case x86_intercept_str:
13748 if (!nested_cpu_has2(vmcs12, SECONDARY_EXEC_DESC))
13749 return X86EMUL_CONTINUE;
13750
13751 /* FIXME: produce nested vmexit and return X86EMUL_INTERCEPTED. */
13752 break;
13753
13754 /* TODO: check more intercepts... */
13755 default:
13756 break;
13757 }
13758
13759 return X86EMUL_UNHANDLEABLE;
13760 }
13761
13762 #ifdef CONFIG_X86_64
13763 /* (a << shift) / divisor, return 1 if overflow otherwise 0 */
u64_shl_div_u64(u64 a,unsigned int shift,u64 divisor,u64 * result)13764 static inline int u64_shl_div_u64(u64 a, unsigned int shift,
13765 u64 divisor, u64 *result)
13766 {
13767 u64 low = a << shift, high = a >> (64 - shift);
13768
13769 /* To avoid the overflow on divq */
13770 if (high >= divisor)
13771 return 1;
13772
13773 /* Low hold the result, high hold rem which is discarded */
13774 asm("divq %2\n\t" : "=a" (low), "=d" (high) :
13775 "rm" (divisor), "0" (low), "1" (high));
13776 *result = low;
13777
13778 return 0;
13779 }
13780
vmx_set_hv_timer(struct kvm_vcpu * vcpu,u64 guest_deadline_tsc)13781 static int vmx_set_hv_timer(struct kvm_vcpu *vcpu, u64 guest_deadline_tsc)
13782 {
13783 struct vcpu_vmx *vmx;
13784 u64 tscl, guest_tscl, delta_tsc, lapic_timer_advance_cycles;
13785
13786 if (kvm_mwait_in_guest(vcpu->kvm))
13787 return -EOPNOTSUPP;
13788
13789 vmx = to_vmx(vcpu);
13790 tscl = rdtsc();
13791 guest_tscl = kvm_read_l1_tsc(vcpu, tscl);
13792 delta_tsc = max(guest_deadline_tsc, guest_tscl) - guest_tscl;
13793 lapic_timer_advance_cycles = nsec_to_cycles(vcpu, lapic_timer_advance_ns);
13794
13795 if (delta_tsc > lapic_timer_advance_cycles)
13796 delta_tsc -= lapic_timer_advance_cycles;
13797 else
13798 delta_tsc = 0;
13799
13800 /* Convert to host delta tsc if tsc scaling is enabled */
13801 if (vcpu->arch.tsc_scaling_ratio != kvm_default_tsc_scaling_ratio &&
13802 u64_shl_div_u64(delta_tsc,
13803 kvm_tsc_scaling_ratio_frac_bits,
13804 vcpu->arch.tsc_scaling_ratio,
13805 &delta_tsc))
13806 return -ERANGE;
13807
13808 /*
13809 * If the delta tsc can't fit in the 32 bit after the multi shift,
13810 * we can't use the preemption timer.
13811 * It's possible that it fits on later vmentries, but checking
13812 * on every vmentry is costly so we just use an hrtimer.
13813 */
13814 if (delta_tsc >> (cpu_preemption_timer_multi + 32))
13815 return -ERANGE;
13816
13817 vmx->hv_deadline_tsc = tscl + delta_tsc;
13818 return delta_tsc == 0;
13819 }
13820
vmx_cancel_hv_timer(struct kvm_vcpu * vcpu)13821 static void vmx_cancel_hv_timer(struct kvm_vcpu *vcpu)
13822 {
13823 to_vmx(vcpu)->hv_deadline_tsc = -1;
13824 }
13825 #endif
13826
vmx_sched_in(struct kvm_vcpu * vcpu,int cpu)13827 static void vmx_sched_in(struct kvm_vcpu *vcpu, int cpu)
13828 {
13829 if (!kvm_pause_in_guest(vcpu->kvm))
13830 shrink_ple_window(vcpu);
13831 }
13832
vmx_slot_enable_log_dirty(struct kvm * kvm,struct kvm_memory_slot * slot)13833 static void vmx_slot_enable_log_dirty(struct kvm *kvm,
13834 struct kvm_memory_slot *slot)
13835 {
13836 kvm_mmu_slot_leaf_clear_dirty(kvm, slot);
13837 kvm_mmu_slot_largepage_remove_write_access(kvm, slot);
13838 }
13839
vmx_slot_disable_log_dirty(struct kvm * kvm,struct kvm_memory_slot * slot)13840 static void vmx_slot_disable_log_dirty(struct kvm *kvm,
13841 struct kvm_memory_slot *slot)
13842 {
13843 kvm_mmu_slot_set_dirty(kvm, slot);
13844 }
13845
vmx_flush_log_dirty(struct kvm * kvm)13846 static void vmx_flush_log_dirty(struct kvm *kvm)
13847 {
13848 kvm_flush_pml_buffers(kvm);
13849 }
13850
vmx_write_pml_buffer(struct kvm_vcpu * vcpu,gpa_t gpa)13851 static int vmx_write_pml_buffer(struct kvm_vcpu *vcpu, gpa_t gpa)
13852 {
13853 struct vmcs12 *vmcs12;
13854 struct vcpu_vmx *vmx = to_vmx(vcpu);
13855 struct page *page = NULL;
13856 u64 *pml_address;
13857
13858 if (is_guest_mode(vcpu)) {
13859 WARN_ON_ONCE(vmx->nested.pml_full);
13860
13861 /*
13862 * Check if PML is enabled for the nested guest.
13863 * Whether eptp bit 6 is set is already checked
13864 * as part of A/D emulation.
13865 */
13866 vmcs12 = get_vmcs12(vcpu);
13867 if (!nested_cpu_has_pml(vmcs12))
13868 return 0;
13869
13870 if (vmcs12->guest_pml_index >= PML_ENTITY_NUM) {
13871 vmx->nested.pml_full = true;
13872 return 1;
13873 }
13874
13875 gpa &= ~0xFFFull;
13876
13877 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->pml_address);
13878 if (is_error_page(page))
13879 return 0;
13880
13881 pml_address = kmap(page);
13882 pml_address[vmcs12->guest_pml_index--] = gpa;
13883 kunmap(page);
13884 kvm_release_page_clean(page);
13885 }
13886
13887 return 0;
13888 }
13889
vmx_enable_log_dirty_pt_masked(struct kvm * kvm,struct kvm_memory_slot * memslot,gfn_t offset,unsigned long mask)13890 static void vmx_enable_log_dirty_pt_masked(struct kvm *kvm,
13891 struct kvm_memory_slot *memslot,
13892 gfn_t offset, unsigned long mask)
13893 {
13894 kvm_mmu_clear_dirty_pt_masked(kvm, memslot, offset, mask);
13895 }
13896
__pi_post_block(struct kvm_vcpu * vcpu)13897 static void __pi_post_block(struct kvm_vcpu *vcpu)
13898 {
13899 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
13900 struct pi_desc old, new;
13901 unsigned int dest;
13902
13903 do {
13904 old.control = new.control = pi_desc->control;
13905 WARN(old.nv != POSTED_INTR_WAKEUP_VECTOR,
13906 "Wakeup handler not enabled while the VCPU is blocked\n");
13907
13908 dest = cpu_physical_id(vcpu->cpu);
13909
13910 if (x2apic_enabled())
13911 new.ndst = dest;
13912 else
13913 new.ndst = (dest << 8) & 0xFF00;
13914
13915 /* set 'NV' to 'notification vector' */
13916 new.nv = POSTED_INTR_VECTOR;
13917 } while (cmpxchg64(&pi_desc->control, old.control,
13918 new.control) != old.control);
13919
13920 if (!WARN_ON_ONCE(vcpu->pre_pcpu == -1)) {
13921 spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
13922 list_del(&vcpu->blocked_vcpu_list);
13923 spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
13924 vcpu->pre_pcpu = -1;
13925 }
13926 }
13927
13928 /*
13929 * This routine does the following things for vCPU which is going
13930 * to be blocked if VT-d PI is enabled.
13931 * - Store the vCPU to the wakeup list, so when interrupts happen
13932 * we can find the right vCPU to wake up.
13933 * - Change the Posted-interrupt descriptor as below:
13934 * 'NDST' <-- vcpu->pre_pcpu
13935 * 'NV' <-- POSTED_INTR_WAKEUP_VECTOR
13936 * - If 'ON' is set during this process, which means at least one
13937 * interrupt is posted for this vCPU, we cannot block it, in
13938 * this case, return 1, otherwise, return 0.
13939 *
13940 */
pi_pre_block(struct kvm_vcpu * vcpu)13941 static int pi_pre_block(struct kvm_vcpu *vcpu)
13942 {
13943 unsigned int dest;
13944 struct pi_desc old, new;
13945 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
13946
13947 if (!kvm_arch_has_assigned_device(vcpu->kvm) ||
13948 !irq_remapping_cap(IRQ_POSTING_CAP) ||
13949 !kvm_vcpu_apicv_active(vcpu))
13950 return 0;
13951
13952 WARN_ON(irqs_disabled());
13953 local_irq_disable();
13954 if (!WARN_ON_ONCE(vcpu->pre_pcpu != -1)) {
13955 vcpu->pre_pcpu = vcpu->cpu;
13956 spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
13957 list_add_tail(&vcpu->blocked_vcpu_list,
13958 &per_cpu(blocked_vcpu_on_cpu,
13959 vcpu->pre_pcpu));
13960 spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
13961 }
13962
13963 do {
13964 old.control = new.control = pi_desc->control;
13965
13966 WARN((pi_desc->sn == 1),
13967 "Warning: SN field of posted-interrupts "
13968 "is set before blocking\n");
13969
13970 /*
13971 * Since vCPU can be preempted during this process,
13972 * vcpu->cpu could be different with pre_pcpu, we
13973 * need to set pre_pcpu as the destination of wakeup
13974 * notification event, then we can find the right vCPU
13975 * to wakeup in wakeup handler if interrupts happen
13976 * when the vCPU is in blocked state.
13977 */
13978 dest = cpu_physical_id(vcpu->pre_pcpu);
13979
13980 if (x2apic_enabled())
13981 new.ndst = dest;
13982 else
13983 new.ndst = (dest << 8) & 0xFF00;
13984
13985 /* set 'NV' to 'wakeup vector' */
13986 new.nv = POSTED_INTR_WAKEUP_VECTOR;
13987 } while (cmpxchg64(&pi_desc->control, old.control,
13988 new.control) != old.control);
13989
13990 /* We should not block the vCPU if an interrupt is posted for it. */
13991 if (pi_test_on(pi_desc) == 1)
13992 __pi_post_block(vcpu);
13993
13994 local_irq_enable();
13995 return (vcpu->pre_pcpu == -1);
13996 }
13997
vmx_pre_block(struct kvm_vcpu * vcpu)13998 static int vmx_pre_block(struct kvm_vcpu *vcpu)
13999 {
14000 if (pi_pre_block(vcpu))
14001 return 1;
14002
14003 if (kvm_lapic_hv_timer_in_use(vcpu))
14004 kvm_lapic_switch_to_sw_timer(vcpu);
14005
14006 return 0;
14007 }
14008
pi_post_block(struct kvm_vcpu * vcpu)14009 static void pi_post_block(struct kvm_vcpu *vcpu)
14010 {
14011 if (vcpu->pre_pcpu == -1)
14012 return;
14013
14014 WARN_ON(irqs_disabled());
14015 local_irq_disable();
14016 __pi_post_block(vcpu);
14017 local_irq_enable();
14018 }
14019
vmx_post_block(struct kvm_vcpu * vcpu)14020 static void vmx_post_block(struct kvm_vcpu *vcpu)
14021 {
14022 if (kvm_x86_ops->set_hv_timer)
14023 kvm_lapic_switch_to_hv_timer(vcpu);
14024
14025 pi_post_block(vcpu);
14026 }
14027
14028 /*
14029 * vmx_update_pi_irte - set IRTE for Posted-Interrupts
14030 *
14031 * @kvm: kvm
14032 * @host_irq: host irq of the interrupt
14033 * @guest_irq: gsi of the interrupt
14034 * @set: set or unset PI
14035 * returns 0 on success, < 0 on failure
14036 */
vmx_update_pi_irte(struct kvm * kvm,unsigned int host_irq,uint32_t guest_irq,bool set)14037 static int vmx_update_pi_irte(struct kvm *kvm, unsigned int host_irq,
14038 uint32_t guest_irq, bool set)
14039 {
14040 struct kvm_kernel_irq_routing_entry *e;
14041 struct kvm_irq_routing_table *irq_rt;
14042 struct kvm_lapic_irq irq;
14043 struct kvm_vcpu *vcpu;
14044 struct vcpu_data vcpu_info;
14045 int idx, ret = 0;
14046
14047 if (!kvm_arch_has_assigned_device(kvm) ||
14048 !irq_remapping_cap(IRQ_POSTING_CAP) ||
14049 !kvm_vcpu_apicv_active(kvm->vcpus[0]))
14050 return 0;
14051
14052 idx = srcu_read_lock(&kvm->irq_srcu);
14053 irq_rt = srcu_dereference(kvm->irq_routing, &kvm->irq_srcu);
14054 if (guest_irq >= irq_rt->nr_rt_entries ||
14055 hlist_empty(&irq_rt->map[guest_irq])) {
14056 pr_warn_once("no route for guest_irq %u/%u (broken user space?)\n",
14057 guest_irq, irq_rt->nr_rt_entries);
14058 goto out;
14059 }
14060
14061 hlist_for_each_entry(e, &irq_rt->map[guest_irq], link) {
14062 if (e->type != KVM_IRQ_ROUTING_MSI)
14063 continue;
14064 /*
14065 * VT-d PI cannot support posting multicast/broadcast
14066 * interrupts to a vCPU, we still use interrupt remapping
14067 * for these kind of interrupts.
14068 *
14069 * For lowest-priority interrupts, we only support
14070 * those with single CPU as the destination, e.g. user
14071 * configures the interrupts via /proc/irq or uses
14072 * irqbalance to make the interrupts single-CPU.
14073 *
14074 * We will support full lowest-priority interrupt later.
14075 */
14076
14077 kvm_set_msi_irq(kvm, e, &irq);
14078 if (!kvm_intr_is_single_vcpu(kvm, &irq, &vcpu)) {
14079 /*
14080 * Make sure the IRTE is in remapped mode if
14081 * we don't handle it in posted mode.
14082 */
14083 ret = irq_set_vcpu_affinity(host_irq, NULL);
14084 if (ret < 0) {
14085 printk(KERN_INFO
14086 "failed to back to remapped mode, irq: %u\n",
14087 host_irq);
14088 goto out;
14089 }
14090
14091 continue;
14092 }
14093
14094 vcpu_info.pi_desc_addr = __pa(vcpu_to_pi_desc(vcpu));
14095 vcpu_info.vector = irq.vector;
14096
14097 trace_kvm_pi_irte_update(host_irq, vcpu->vcpu_id, e->gsi,
14098 vcpu_info.vector, vcpu_info.pi_desc_addr, set);
14099
14100 if (set)
14101 ret = irq_set_vcpu_affinity(host_irq, &vcpu_info);
14102 else
14103 ret = irq_set_vcpu_affinity(host_irq, NULL);
14104
14105 if (ret < 0) {
14106 printk(KERN_INFO "%s: failed to update PI IRTE\n",
14107 __func__);
14108 goto out;
14109 }
14110 }
14111
14112 ret = 0;
14113 out:
14114 srcu_read_unlock(&kvm->irq_srcu, idx);
14115 return ret;
14116 }
14117
vmx_setup_mce(struct kvm_vcpu * vcpu)14118 static void vmx_setup_mce(struct kvm_vcpu *vcpu)
14119 {
14120 if (vcpu->arch.mcg_cap & MCG_LMCE_P)
14121 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits |=
14122 FEATURE_CONTROL_LMCE;
14123 else
14124 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits &=
14125 ~FEATURE_CONTROL_LMCE;
14126 }
14127
vmx_smi_allowed(struct kvm_vcpu * vcpu)14128 static int vmx_smi_allowed(struct kvm_vcpu *vcpu)
14129 {
14130 /* we need a nested vmexit to enter SMM, postpone if run is pending */
14131 if (to_vmx(vcpu)->nested.nested_run_pending)
14132 return 0;
14133 return 1;
14134 }
14135
vmx_pre_enter_smm(struct kvm_vcpu * vcpu,char * smstate)14136 static int vmx_pre_enter_smm(struct kvm_vcpu *vcpu, char *smstate)
14137 {
14138 struct vcpu_vmx *vmx = to_vmx(vcpu);
14139
14140 vmx->nested.smm.guest_mode = is_guest_mode(vcpu);
14141 if (vmx->nested.smm.guest_mode)
14142 nested_vmx_vmexit(vcpu, -1, 0, 0);
14143
14144 vmx->nested.smm.vmxon = vmx->nested.vmxon;
14145 vmx->nested.vmxon = false;
14146 vmx_clear_hlt(vcpu);
14147 return 0;
14148 }
14149
vmx_pre_leave_smm(struct kvm_vcpu * vcpu,u64 smbase)14150 static int vmx_pre_leave_smm(struct kvm_vcpu *vcpu, u64 smbase)
14151 {
14152 struct vcpu_vmx *vmx = to_vmx(vcpu);
14153 int ret;
14154
14155 if (vmx->nested.smm.vmxon) {
14156 vmx->nested.vmxon = true;
14157 vmx->nested.smm.vmxon = false;
14158 }
14159
14160 if (vmx->nested.smm.guest_mode) {
14161 vcpu->arch.hflags &= ~HF_SMM_MASK;
14162 ret = enter_vmx_non_root_mode(vcpu, NULL);
14163 vcpu->arch.hflags |= HF_SMM_MASK;
14164 if (ret)
14165 return ret;
14166
14167 vmx->nested.smm.guest_mode = false;
14168 }
14169 return 0;
14170 }
14171
enable_smi_window(struct kvm_vcpu * vcpu)14172 static int enable_smi_window(struct kvm_vcpu *vcpu)
14173 {
14174 return 0;
14175 }
14176
vmx_get_nested_state(struct kvm_vcpu * vcpu,struct kvm_nested_state __user * user_kvm_nested_state,u32 user_data_size)14177 static int vmx_get_nested_state(struct kvm_vcpu *vcpu,
14178 struct kvm_nested_state __user *user_kvm_nested_state,
14179 u32 user_data_size)
14180 {
14181 struct vcpu_vmx *vmx;
14182 struct vmcs12 *vmcs12;
14183 struct kvm_nested_state kvm_state = {
14184 .flags = 0,
14185 .format = 0,
14186 .size = sizeof(kvm_state),
14187 .vmx.vmxon_pa = -1ull,
14188 .vmx.vmcs_pa = -1ull,
14189 };
14190
14191 if (!vcpu)
14192 return kvm_state.size + 2 * VMCS12_SIZE;
14193
14194 vmx = to_vmx(vcpu);
14195 vmcs12 = get_vmcs12(vcpu);
14196 if (nested_vmx_allowed(vcpu) &&
14197 (vmx->nested.vmxon || vmx->nested.smm.vmxon)) {
14198 kvm_state.vmx.vmxon_pa = vmx->nested.vmxon_ptr;
14199 kvm_state.vmx.vmcs_pa = vmx->nested.current_vmptr;
14200
14201 if (vmx->nested.current_vmptr != -1ull) {
14202 kvm_state.size += VMCS12_SIZE;
14203
14204 if (is_guest_mode(vcpu) &&
14205 nested_cpu_has_shadow_vmcs(vmcs12) &&
14206 vmcs12->vmcs_link_pointer != -1ull)
14207 kvm_state.size += VMCS12_SIZE;
14208 }
14209
14210 if (vmx->nested.smm.vmxon)
14211 kvm_state.vmx.smm.flags |= KVM_STATE_NESTED_SMM_VMXON;
14212
14213 if (vmx->nested.smm.guest_mode)
14214 kvm_state.vmx.smm.flags |= KVM_STATE_NESTED_SMM_GUEST_MODE;
14215
14216 if (is_guest_mode(vcpu)) {
14217 kvm_state.flags |= KVM_STATE_NESTED_GUEST_MODE;
14218
14219 if (vmx->nested.nested_run_pending)
14220 kvm_state.flags |= KVM_STATE_NESTED_RUN_PENDING;
14221 }
14222 }
14223
14224 if (user_data_size < kvm_state.size)
14225 goto out;
14226
14227 if (copy_to_user(user_kvm_nested_state, &kvm_state, sizeof(kvm_state)))
14228 return -EFAULT;
14229
14230 if (vmx->nested.current_vmptr == -1ull)
14231 goto out;
14232
14233 /*
14234 * When running L2, the authoritative vmcs12 state is in the
14235 * vmcs02. When running L1, the authoritative vmcs12 state is
14236 * in the shadow vmcs linked to vmcs01, unless
14237 * sync_shadow_vmcs is set, in which case, the authoritative
14238 * vmcs12 state is in the vmcs12 already.
14239 */
14240 if (is_guest_mode(vcpu))
14241 sync_vmcs12(vcpu, vmcs12);
14242 else if (enable_shadow_vmcs && !vmx->nested.sync_shadow_vmcs)
14243 copy_shadow_to_vmcs12(vmx);
14244
14245 /*
14246 * Copy over the full allocated size of vmcs12 rather than just the size
14247 * of the struct.
14248 */
14249 if (copy_to_user(user_kvm_nested_state->data, vmcs12, VMCS12_SIZE))
14250 return -EFAULT;
14251
14252 if (nested_cpu_has_shadow_vmcs(vmcs12) &&
14253 vmcs12->vmcs_link_pointer != -1ull) {
14254 if (copy_to_user(user_kvm_nested_state->data + VMCS12_SIZE,
14255 get_shadow_vmcs12(vcpu), VMCS12_SIZE))
14256 return -EFAULT;
14257 }
14258
14259 out:
14260 return kvm_state.size;
14261 }
14262
vmx_set_nested_state(struct kvm_vcpu * vcpu,struct kvm_nested_state __user * user_kvm_nested_state,struct kvm_nested_state * kvm_state)14263 static int vmx_set_nested_state(struct kvm_vcpu *vcpu,
14264 struct kvm_nested_state __user *user_kvm_nested_state,
14265 struct kvm_nested_state *kvm_state)
14266 {
14267 struct vcpu_vmx *vmx = to_vmx(vcpu);
14268 struct vmcs12 *vmcs12;
14269 u32 exit_qual;
14270 int ret;
14271
14272 if (kvm_state->format != 0)
14273 return -EINVAL;
14274
14275 if (!nested_vmx_allowed(vcpu))
14276 return kvm_state->vmx.vmxon_pa == -1ull ? 0 : -EINVAL;
14277
14278 if (kvm_state->vmx.vmxon_pa == -1ull) {
14279 if (kvm_state->vmx.smm.flags)
14280 return -EINVAL;
14281
14282 if (kvm_state->vmx.vmcs_pa != -1ull)
14283 return -EINVAL;
14284
14285 vmx_leave_nested(vcpu);
14286 return 0;
14287 }
14288
14289 if (!page_address_valid(vcpu, kvm_state->vmx.vmxon_pa))
14290 return -EINVAL;
14291
14292 if ((kvm_state->vmx.smm.flags & KVM_STATE_NESTED_SMM_GUEST_MODE) &&
14293 (kvm_state->flags & KVM_STATE_NESTED_GUEST_MODE))
14294 return -EINVAL;
14295
14296 if (kvm_state->vmx.smm.flags &
14297 ~(KVM_STATE_NESTED_SMM_GUEST_MODE | KVM_STATE_NESTED_SMM_VMXON))
14298 return -EINVAL;
14299
14300 /*
14301 * SMM temporarily disables VMX, so we cannot be in guest mode,
14302 * nor can VMLAUNCH/VMRESUME be pending. Outside SMM, SMM flags
14303 * must be zero.
14304 */
14305 if (is_smm(vcpu) ? kvm_state->flags : kvm_state->vmx.smm.flags)
14306 return -EINVAL;
14307
14308 if ((kvm_state->vmx.smm.flags & KVM_STATE_NESTED_SMM_GUEST_MODE) &&
14309 !(kvm_state->vmx.smm.flags & KVM_STATE_NESTED_SMM_VMXON))
14310 return -EINVAL;
14311
14312 vmx_leave_nested(vcpu);
14313 if (kvm_state->vmx.vmxon_pa == -1ull)
14314 return 0;
14315
14316 vmx->nested.vmxon_ptr = kvm_state->vmx.vmxon_pa;
14317 ret = enter_vmx_operation(vcpu);
14318 if (ret)
14319 return ret;
14320
14321 /* Empty 'VMXON' state is permitted */
14322 if (kvm_state->size < sizeof(*kvm_state) + sizeof(*vmcs12))
14323 return 0;
14324
14325 if (kvm_state->vmx.vmcs_pa == kvm_state->vmx.vmxon_pa ||
14326 !page_address_valid(vcpu, kvm_state->vmx.vmcs_pa))
14327 return -EINVAL;
14328
14329 set_current_vmptr(vmx, kvm_state->vmx.vmcs_pa);
14330
14331 if (kvm_state->vmx.smm.flags & KVM_STATE_NESTED_SMM_VMXON) {
14332 vmx->nested.smm.vmxon = true;
14333 vmx->nested.vmxon = false;
14334
14335 if (kvm_state->vmx.smm.flags & KVM_STATE_NESTED_SMM_GUEST_MODE)
14336 vmx->nested.smm.guest_mode = true;
14337 }
14338
14339 vmcs12 = get_vmcs12(vcpu);
14340 if (copy_from_user(vmcs12, user_kvm_nested_state->data, sizeof(*vmcs12)))
14341 return -EFAULT;
14342
14343 if (vmcs12->hdr.revision_id != VMCS12_REVISION)
14344 return -EINVAL;
14345
14346 if (!(kvm_state->flags & KVM_STATE_NESTED_GUEST_MODE))
14347 return 0;
14348
14349 vmx->nested.nested_run_pending =
14350 !!(kvm_state->flags & KVM_STATE_NESTED_RUN_PENDING);
14351
14352 if (nested_cpu_has_shadow_vmcs(vmcs12) &&
14353 vmcs12->vmcs_link_pointer != -1ull) {
14354 struct vmcs12 *shadow_vmcs12 = get_shadow_vmcs12(vcpu);
14355 if (kvm_state->size < sizeof(*kvm_state) + 2 * sizeof(*vmcs12))
14356 return -EINVAL;
14357
14358 if (copy_from_user(shadow_vmcs12,
14359 user_kvm_nested_state->data + VMCS12_SIZE,
14360 sizeof(*vmcs12)))
14361 return -EFAULT;
14362
14363 if (shadow_vmcs12->hdr.revision_id != VMCS12_REVISION ||
14364 !shadow_vmcs12->hdr.shadow_vmcs)
14365 return -EINVAL;
14366 }
14367
14368 if (check_vmentry_prereqs(vcpu, vmcs12) ||
14369 check_vmentry_postreqs(vcpu, vmcs12, &exit_qual))
14370 return -EINVAL;
14371
14372 vmx->nested.dirty_vmcs12 = true;
14373 ret = enter_vmx_non_root_mode(vcpu, NULL);
14374 if (ret)
14375 return -EINVAL;
14376
14377 return 0;
14378 }
14379
14380 static struct kvm_x86_ops vmx_x86_ops __ro_after_init = {
14381 .cpu_has_kvm_support = cpu_has_kvm_support,
14382 .disabled_by_bios = vmx_disabled_by_bios,
14383 .hardware_setup = hardware_setup,
14384 .hardware_unsetup = hardware_unsetup,
14385 .check_processor_compatibility = vmx_check_processor_compat,
14386 .hardware_enable = hardware_enable,
14387 .hardware_disable = hardware_disable,
14388 .cpu_has_accelerated_tpr = report_flexpriority,
14389 .has_emulated_msr = vmx_has_emulated_msr,
14390
14391 .vm_init = vmx_vm_init,
14392 .vm_alloc = vmx_vm_alloc,
14393 .vm_free = vmx_vm_free,
14394
14395 .vcpu_create = vmx_create_vcpu,
14396 .vcpu_free = vmx_free_vcpu,
14397 .vcpu_reset = vmx_vcpu_reset,
14398
14399 .prepare_guest_switch = vmx_prepare_switch_to_guest,
14400 .vcpu_load = vmx_vcpu_load,
14401 .vcpu_put = vmx_vcpu_put,
14402
14403 .update_bp_intercept = update_exception_bitmap,
14404 .get_msr_feature = vmx_get_msr_feature,
14405 .get_msr = vmx_get_msr,
14406 .set_msr = vmx_set_msr,
14407 .get_segment_base = vmx_get_segment_base,
14408 .get_segment = vmx_get_segment,
14409 .set_segment = vmx_set_segment,
14410 .get_cpl = vmx_get_cpl,
14411 .get_cs_db_l_bits = vmx_get_cs_db_l_bits,
14412 .decache_cr0_guest_bits = vmx_decache_cr0_guest_bits,
14413 .decache_cr3 = vmx_decache_cr3,
14414 .decache_cr4_guest_bits = vmx_decache_cr4_guest_bits,
14415 .set_cr0 = vmx_set_cr0,
14416 .set_cr3 = vmx_set_cr3,
14417 .set_cr4 = vmx_set_cr4,
14418 .set_efer = vmx_set_efer,
14419 .get_idt = vmx_get_idt,
14420 .set_idt = vmx_set_idt,
14421 .get_gdt = vmx_get_gdt,
14422 .set_gdt = vmx_set_gdt,
14423 .get_dr6 = vmx_get_dr6,
14424 .set_dr6 = vmx_set_dr6,
14425 .set_dr7 = vmx_set_dr7,
14426 .sync_dirty_debug_regs = vmx_sync_dirty_debug_regs,
14427 .cache_reg = vmx_cache_reg,
14428 .get_rflags = vmx_get_rflags,
14429 .set_rflags = vmx_set_rflags,
14430
14431 .tlb_flush = vmx_flush_tlb,
14432 .tlb_flush_gva = vmx_flush_tlb_gva,
14433
14434 .run = vmx_vcpu_run,
14435 .handle_exit = vmx_handle_exit,
14436 .skip_emulated_instruction = skip_emulated_instruction,
14437 .set_interrupt_shadow = vmx_set_interrupt_shadow,
14438 .get_interrupt_shadow = vmx_get_interrupt_shadow,
14439 .patch_hypercall = vmx_patch_hypercall,
14440 .set_irq = vmx_inject_irq,
14441 .set_nmi = vmx_inject_nmi,
14442 .queue_exception = vmx_queue_exception,
14443 .cancel_injection = vmx_cancel_injection,
14444 .interrupt_allowed = vmx_interrupt_allowed,
14445 .nmi_allowed = vmx_nmi_allowed,
14446 .get_nmi_mask = vmx_get_nmi_mask,
14447 .set_nmi_mask = vmx_set_nmi_mask,
14448 .enable_nmi_window = enable_nmi_window,
14449 .enable_irq_window = enable_irq_window,
14450 .update_cr8_intercept = update_cr8_intercept,
14451 .set_virtual_apic_mode = vmx_set_virtual_apic_mode,
14452 .set_apic_access_page_addr = vmx_set_apic_access_page_addr,
14453 .get_enable_apicv = vmx_get_enable_apicv,
14454 .refresh_apicv_exec_ctrl = vmx_refresh_apicv_exec_ctrl,
14455 .load_eoi_exitmap = vmx_load_eoi_exitmap,
14456 .apicv_post_state_restore = vmx_apicv_post_state_restore,
14457 .hwapic_irr_update = vmx_hwapic_irr_update,
14458 .hwapic_isr_update = vmx_hwapic_isr_update,
14459 .guest_apic_has_interrupt = vmx_guest_apic_has_interrupt,
14460 .sync_pir_to_irr = vmx_sync_pir_to_irr,
14461 .deliver_posted_interrupt = vmx_deliver_posted_interrupt,
14462 .dy_apicv_has_pending_interrupt = vmx_dy_apicv_has_pending_interrupt,
14463
14464 .set_tss_addr = vmx_set_tss_addr,
14465 .set_identity_map_addr = vmx_set_identity_map_addr,
14466 .get_tdp_level = get_ept_level,
14467 .get_mt_mask = vmx_get_mt_mask,
14468
14469 .get_exit_info = vmx_get_exit_info,
14470
14471 .get_lpage_level = vmx_get_lpage_level,
14472
14473 .cpuid_update = vmx_cpuid_update,
14474
14475 .rdtscp_supported = vmx_rdtscp_supported,
14476 .invpcid_supported = vmx_invpcid_supported,
14477
14478 .set_supported_cpuid = vmx_set_supported_cpuid,
14479
14480 .has_wbinvd_exit = cpu_has_vmx_wbinvd_exit,
14481
14482 .read_l1_tsc_offset = vmx_read_l1_tsc_offset,
14483 .write_l1_tsc_offset = vmx_write_l1_tsc_offset,
14484
14485 .set_tdp_cr3 = vmx_set_cr3,
14486
14487 .check_intercept = vmx_check_intercept,
14488 .handle_external_intr = vmx_handle_external_intr,
14489 .mpx_supported = vmx_mpx_supported,
14490 .xsaves_supported = vmx_xsaves_supported,
14491 .umip_emulated = vmx_umip_emulated,
14492
14493 .check_nested_events = vmx_check_nested_events,
14494 .request_immediate_exit = vmx_request_immediate_exit,
14495
14496 .sched_in = vmx_sched_in,
14497
14498 .slot_enable_log_dirty = vmx_slot_enable_log_dirty,
14499 .slot_disable_log_dirty = vmx_slot_disable_log_dirty,
14500 .flush_log_dirty = vmx_flush_log_dirty,
14501 .enable_log_dirty_pt_masked = vmx_enable_log_dirty_pt_masked,
14502 .write_log_dirty = vmx_write_pml_buffer,
14503
14504 .pre_block = vmx_pre_block,
14505 .post_block = vmx_post_block,
14506
14507 .pmu_ops = &intel_pmu_ops,
14508
14509 .update_pi_irte = vmx_update_pi_irte,
14510
14511 #ifdef CONFIG_X86_64
14512 .set_hv_timer = vmx_set_hv_timer,
14513 .cancel_hv_timer = vmx_cancel_hv_timer,
14514 #endif
14515
14516 .setup_mce = vmx_setup_mce,
14517
14518 .get_nested_state = vmx_get_nested_state,
14519 .set_nested_state = vmx_set_nested_state,
14520 .get_vmcs12_pages = nested_get_vmcs12_pages,
14521
14522 .smi_allowed = vmx_smi_allowed,
14523 .pre_enter_smm = vmx_pre_enter_smm,
14524 .pre_leave_smm = vmx_pre_leave_smm,
14525 .enable_smi_window = enable_smi_window,
14526 };
14527
vmx_cleanup_l1d_flush(void)14528 static void vmx_cleanup_l1d_flush(void)
14529 {
14530 if (vmx_l1d_flush_pages) {
14531 free_pages((unsigned long)vmx_l1d_flush_pages, L1D_CACHE_ORDER);
14532 vmx_l1d_flush_pages = NULL;
14533 }
14534 /* Restore state so sysfs ignores VMX */
14535 l1tf_vmx_mitigation = VMENTER_L1D_FLUSH_AUTO;
14536 }
14537
vmx_exit(void)14538 static void vmx_exit(void)
14539 {
14540 #ifdef CONFIG_KEXEC_CORE
14541 RCU_INIT_POINTER(crash_vmclear_loaded_vmcss, NULL);
14542 synchronize_rcu();
14543 #endif
14544
14545 kvm_exit();
14546
14547 #if IS_ENABLED(CONFIG_HYPERV)
14548 if (static_branch_unlikely(&enable_evmcs)) {
14549 int cpu;
14550 struct hv_vp_assist_page *vp_ap;
14551 /*
14552 * Reset everything to support using non-enlightened VMCS
14553 * access later (e.g. when we reload the module with
14554 * enlightened_vmcs=0)
14555 */
14556 for_each_online_cpu(cpu) {
14557 vp_ap = hv_get_vp_assist_page(cpu);
14558
14559 if (!vp_ap)
14560 continue;
14561
14562 vp_ap->current_nested_vmcs = 0;
14563 vp_ap->enlighten_vmentry = 0;
14564 }
14565
14566 static_branch_disable(&enable_evmcs);
14567 }
14568 #endif
14569 vmx_cleanup_l1d_flush();
14570 }
14571 module_exit(vmx_exit);
14572
vmx_init(void)14573 static int __init vmx_init(void)
14574 {
14575 int r, cpu;
14576
14577 #if IS_ENABLED(CONFIG_HYPERV)
14578 /*
14579 * Enlightened VMCS usage should be recommended and the host needs
14580 * to support eVMCS v1 or above. We can also disable eVMCS support
14581 * with module parameter.
14582 */
14583 if (enlightened_vmcs &&
14584 ms_hyperv.hints & HV_X64_ENLIGHTENED_VMCS_RECOMMENDED &&
14585 (ms_hyperv.nested_features & HV_X64_ENLIGHTENED_VMCS_VERSION) >=
14586 KVM_EVMCS_VERSION) {
14587 int cpu;
14588
14589 /* Check that we have assist pages on all online CPUs */
14590 for_each_online_cpu(cpu) {
14591 if (!hv_get_vp_assist_page(cpu)) {
14592 enlightened_vmcs = false;
14593 break;
14594 }
14595 }
14596
14597 if (enlightened_vmcs) {
14598 pr_info("KVM: vmx: using Hyper-V Enlightened VMCS\n");
14599 static_branch_enable(&enable_evmcs);
14600 }
14601 } else {
14602 enlightened_vmcs = false;
14603 }
14604 #endif
14605
14606 r = kvm_init(&vmx_x86_ops, sizeof(struct vcpu_vmx),
14607 __alignof__(struct vcpu_vmx), THIS_MODULE);
14608 if (r)
14609 return r;
14610
14611 /*
14612 * Must be called after kvm_init() so enable_ept is properly set
14613 * up. Hand the parameter mitigation value in which was stored in
14614 * the pre module init parser. If no parameter was given, it will
14615 * contain 'auto' which will be turned into the default 'cond'
14616 * mitigation mode.
14617 */
14618 if (boot_cpu_has(X86_BUG_L1TF)) {
14619 r = vmx_setup_l1d_flush(vmentry_l1d_flush_param);
14620 if (r) {
14621 vmx_exit();
14622 return r;
14623 }
14624 }
14625
14626 for_each_possible_cpu(cpu) {
14627 INIT_LIST_HEAD(&per_cpu(loaded_vmcss_on_cpu, cpu));
14628 INIT_LIST_HEAD(&per_cpu(blocked_vcpu_on_cpu, cpu));
14629 spin_lock_init(&per_cpu(blocked_vcpu_on_cpu_lock, cpu));
14630 }
14631
14632 #ifdef CONFIG_KEXEC_CORE
14633 rcu_assign_pointer(crash_vmclear_loaded_vmcss,
14634 crash_vmclear_local_loaded_vmcss);
14635 #endif
14636 vmx_check_vmcs12_offsets();
14637
14638 return 0;
14639 }
14640 module_init(vmx_init);
14641