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