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