blob: 1e2ca9e8662ff2bdd4ea673b031ad96f1eda0af4 [file] [log] [blame]
Avi Kivity6aa8b732006-12-10 02:21:36 -08001/*
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.
Nicolas Kaiser9611c182010-10-06 14:23:22 +02008 * Copyright 2010 Red Hat, Inc. and/or its affiliates.
Avi Kivity6aa8b732006-12-10 02:21:36 -08009 *
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
Eddie Dong85f455f2007-07-06 12:20:49 +030019#include "irq.h"
Zhang Xiantao1d737c82007-12-14 09:35:10 +080020#include "mmu.h"
Avi Kivity00b27a32011-11-23 16:30:32 +020021#include "cpuid.h"
Andrey Smetanind62caab2015-11-10 15:36:33 +030022#include "lapic.h"
Avi Kivitye4956062007-06-28 14:15:57 -040023
Avi Kivityedf88412007-12-16 11:02:48 +020024#include <linux/kvm_host.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080025#include <linux/module.h>
Ahmed S. Darwish9d8f5492007-02-19 14:37:46 +020026#include <linux/kernel.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080027#include <linux/mm.h>
28#include <linux/highmem.h>
Alexey Dobriyane8edc6e2007-05-21 01:22:52 +040029#include <linux/sched.h>
Avi Kivityc7addb92007-09-16 18:58:32 +020030#include <linux/moduleparam.h>
Josh Triplette9bda3b2012-03-20 23:33:51 -070031#include <linux/mod_devicetable.h>
Steven Rostedt (Red Hat)af658dc2015-04-29 14:36:05 -040032#include <linux/trace_events.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090033#include <linux/slab.h>
Shane Wangcafd6652010-04-29 12:09:01 -040034#include <linux/tboot.h>
Jan Kiszkaf41245002014-03-07 20:03:13 +010035#include <linux/hrtimer.h>
Josh Poimboeufc207aee2017-06-28 10:11:06 -050036#include <linux/frame.h>
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -030037#include "kvm_cache_regs.h"
Avi Kivity35920a32008-07-03 14:50:12 +030038#include "x86.h"
Avi Kivitye4956062007-06-28 14:15:57 -040039
Feng Wu28b835d2015-09-18 22:29:54 +080040#include <asm/cpu.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080041#include <asm/io.h>
Anthony Liguori3b3be0d2006-12-13 00:33:43 -080042#include <asm/desc.h>
Eduardo Habkost13673a92008-11-17 19:03:13 -020043#include <asm/vmx.h>
Eduardo Habkost6210e372008-11-17 19:03:16 -020044#include <asm/virtext.h>
Andi Kleena0861c02009-06-08 17:37:09 +080045#include <asm/mce.h>
Ingo Molnar952f07e2015-04-26 16:56:05 +020046#include <asm/fpu/internal.h>
Gleb Natapovd7cd9792011-10-05 14:01:23 +020047#include <asm/perf_event.h>
Paolo Bonzini81908bf2014-02-21 10:32:27 +010048#include <asm/debugreg.h>
Zhang Yanfei8f536b72012-12-06 23:43:34 +080049#include <asm/kexec.h>
Radim Krčmářdab20872015-02-09 22:44:07 +010050#include <asm/apic.h>
Feng Wuefc64402015-09-18 22:29:51 +080051#include <asm/irq_remapping.h>
Andy Lutomirskid6e41f12017-05-28 10:00:17 -070052#include <asm/mmu_context.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080053
Marcelo Tosatti229456f2009-06-17 09:22:14 -030054#include "trace.h"
Wei Huang25462f72015-06-19 15:45:05 +020055#include "pmu.h"
Marcelo Tosatti229456f2009-06-17 09:22:14 -030056
Avi Kivity4ecac3f2008-05-13 13:23:38 +030057#define __ex(x) __kvm_handle_fault_on_reboot(x)
Avi Kivity5e520e62011-05-15 10:13:12 -040058#define __ex_clear(x, reg) \
59 ____kvm_handle_fault_on_reboot(x, "xor " reg " , " reg)
Avi Kivity4ecac3f2008-05-13 13:23:38 +030060
Avi Kivity6aa8b732006-12-10 02:21:36 -080061MODULE_AUTHOR("Qumranet");
62MODULE_LICENSE("GPL");
63
Josh Triplette9bda3b2012-03-20 23:33:51 -070064static const struct x86_cpu_id vmx_cpu_id[] = {
65 X86_FEATURE_MATCH(X86_FEATURE_VMX),
66 {}
67};
68MODULE_DEVICE_TABLE(x86cpu, vmx_cpu_id);
69
Rusty Russell476bc002012-01-13 09:32:18 +103070static bool __read_mostly enable_vpid = 1;
Avi Kivity736caef2009-03-23 17:39:48 +020071module_param_named(vpid, enable_vpid, bool, 0444);
Sheng Yang2384d2b2008-01-17 15:14:33 +080072
Paolo Bonzinid02fcf52017-11-06 13:31:13 +010073static bool __read_mostly enable_vnmi = 1;
74module_param_named(vnmi, enable_vnmi, bool, S_IRUGO);
75
Rusty Russell476bc002012-01-13 09:32:18 +103076static bool __read_mostly flexpriority_enabled = 1;
Avi Kivity736caef2009-03-23 17:39:48 +020077module_param_named(flexpriority, flexpriority_enabled, bool, S_IRUGO);
Avi Kivity4c9fc8e2008-03-24 18:15:14 +020078
Rusty Russell476bc002012-01-13 09:32:18 +103079static bool __read_mostly enable_ept = 1;
Avi Kivity736caef2009-03-23 17:39:48 +020080module_param_named(ept, enable_ept, bool, S_IRUGO);
Sheng Yangd56f5462008-04-25 10:13:16 +080081
Rusty Russell476bc002012-01-13 09:32:18 +103082static bool __read_mostly enable_unrestricted_guest = 1;
Nitin A Kamble3a624e22009-06-08 11:34:16 -070083module_param_named(unrestricted_guest,
84 enable_unrestricted_guest, bool, S_IRUGO);
85
Xudong Hao83c3a332012-05-28 19:33:35 +080086static bool __read_mostly enable_ept_ad_bits = 1;
87module_param_named(eptad, enable_ept_ad_bits, bool, S_IRUGO);
88
Avi Kivitya27685c2012-06-12 20:30:18 +030089static bool __read_mostly emulate_invalid_guest_state = true;
Avi Kivityc1f8bc02009-03-23 15:41:17 +020090module_param(emulate_invalid_guest_state, bool, S_IRUGO);
Mohammed Gamal04fa4d32008-08-17 16:39:48 +030091
Rusty Russell476bc002012-01-13 09:32:18 +103092static bool __read_mostly fasteoi = 1;
Kevin Tian58fbbf22011-08-30 13:56:17 +030093module_param(fasteoi, bool, S_IRUGO);
94
Yang Zhang5a717852013-04-11 19:25:16 +080095static bool __read_mostly enable_apicv = 1;
Yang Zhang01e439b2013-04-11 19:25:12 +080096module_param(enable_apicv, bool, S_IRUGO);
Yang Zhang83d4c282013-01-25 10:18:49 +080097
Abel Gordonabc4fc52013-04-18 14:35:25 +030098static bool __read_mostly enable_shadow_vmcs = 1;
99module_param_named(enable_shadow_vmcs, enable_shadow_vmcs, bool, S_IRUGO);
Nadav Har'El801d3422011-05-25 23:02:23 +0300100/*
101 * If nested=1, nested virtualization is supported, i.e., guests may use
102 * VMX and be a hypervisor for its own guests. If nested=0, guests may not
103 * use VMX instructions.
104 */
Rusty Russell476bc002012-01-13 09:32:18 +1030105static bool __read_mostly nested = 0;
Nadav Har'El801d3422011-05-25 23:02:23 +0300106module_param(nested, bool, S_IRUGO);
107
Wanpeng Li20300092014-12-02 19:14:59 +0800108static u64 __read_mostly host_xss;
109
Kai Huang843e4332015-01-28 10:54:28 +0800110static bool __read_mostly enable_pml = 1;
111module_param_named(pml, enable_pml, bool, S_IRUGO);
112
Haozhong Zhang64903d62015-10-20 15:39:09 +0800113#define KVM_VMX_TSC_MULTIPLIER_MAX 0xffffffffffffffffULL
114
Yunhong Jiang64672c92016-06-13 14:19:59 -0700115/* Guest_tsc -> host_tsc conversion requires 64-bit division. */
116static int __read_mostly cpu_preemption_timer_multi;
117static bool __read_mostly enable_preemption_timer = 1;
118#ifdef CONFIG_X86_64
119module_param_named(preemption_timer, enable_preemption_timer, bool, S_IRUGO);
120#endif
121
Gleb Natapov50378782013-02-04 16:00:28 +0200122#define KVM_GUEST_CR0_MASK (X86_CR0_NW | X86_CR0_CD)
123#define KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST (X86_CR0_WP | X86_CR0_NE)
Avi Kivitycdc0e242009-12-06 17:21:14 +0200124#define KVM_VM_CR0_ALWAYS_ON \
125 (KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST | X86_CR0_PG | X86_CR0_PE)
Avi Kivity4c386092009-12-07 12:26:18 +0200126#define KVM_CR4_GUEST_OWNED_BITS \
127 (X86_CR4_PVI | X86_CR4_DE | X86_CR4_PCE | X86_CR4_OSFXSR \
Yu Zhangfd8cb432017-08-24 20:27:56 +0800128 | X86_CR4_OSXMMEXCPT | X86_CR4_LA57 | X86_CR4_TSD)
Avi Kivity4c386092009-12-07 12:26:18 +0200129
Avi Kivitycdc0e242009-12-06 17:21:14 +0200130#define KVM_PMODE_VM_CR4_ALWAYS_ON (X86_CR4_PAE | X86_CR4_VMXE)
131#define KVM_RMODE_VM_CR4_ALWAYS_ON (X86_CR4_VME | X86_CR4_PAE | X86_CR4_VMXE)
132
Avi Kivity78ac8b42010-04-08 18:19:35 +0300133#define RMODE_GUEST_OWNED_EFLAGS_BITS (~(X86_EFLAGS_IOPL | X86_EFLAGS_VM))
134
Jan Kiszkaf41245002014-03-07 20:03:13 +0100135#define VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE 5
136
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +0800137/*
Jan Dakinevich16c2aec2016-10-28 07:00:30 +0300138 * Hyper-V requires all of these, so mark them as supported even though
139 * they are just treated the same as all-context.
140 */
141#define VMX_VPID_EXTENT_SUPPORTED_MASK \
142 (VMX_VPID_EXTENT_INDIVIDUAL_ADDR_BIT | \
143 VMX_VPID_EXTENT_SINGLE_CONTEXT_BIT | \
144 VMX_VPID_EXTENT_GLOBAL_CONTEXT_BIT | \
145 VMX_VPID_EXTENT_SINGLE_NON_GLOBAL_BIT)
146
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +0800147/*
148 * These 2 parameters are used to config the controls for Pause-Loop Exiting:
149 * ple_gap: upper bound on the amount of time between two successive
150 * executions of PAUSE in a loop. Also indicate if ple enabled.
Rik van Riel00c25bc2011-01-04 09:51:33 -0500151 * According to test, this time is usually smaller than 128 cycles.
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +0800152 * ple_window: upper bound on the amount of time a guest is allowed to execute
153 * in a PAUSE loop. Tests indicate that most spinlocks are held for
154 * less than 2^12 cycles
155 * Time is measured based on a counter that runs at the same rate as the TSC,
156 * refer SDM volume 3b section 21.6.13 & 22.1.3.
157 */
Radim Krčmářb4a2d312014-08-21 18:08:08 +0200158#define KVM_VMX_DEFAULT_PLE_GAP 128
159#define KVM_VMX_DEFAULT_PLE_WINDOW 4096
160#define KVM_VMX_DEFAULT_PLE_WINDOW_GROW 2
161#define KVM_VMX_DEFAULT_PLE_WINDOW_SHRINK 0
162#define KVM_VMX_DEFAULT_PLE_WINDOW_MAX \
163 INT_MAX / KVM_VMX_DEFAULT_PLE_WINDOW_GROW
164
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +0800165static int ple_gap = KVM_VMX_DEFAULT_PLE_GAP;
166module_param(ple_gap, int, S_IRUGO);
167
168static int ple_window = KVM_VMX_DEFAULT_PLE_WINDOW;
169module_param(ple_window, int, S_IRUGO);
170
Radim Krčmářb4a2d312014-08-21 18:08:08 +0200171/* Default doubles per-vcpu window every exit. */
172static int ple_window_grow = KVM_VMX_DEFAULT_PLE_WINDOW_GROW;
173module_param(ple_window_grow, int, S_IRUGO);
174
175/* Default resets per-vcpu window every exit to ple_window. */
176static int ple_window_shrink = KVM_VMX_DEFAULT_PLE_WINDOW_SHRINK;
177module_param(ple_window_shrink, int, S_IRUGO);
178
179/* Default is to compute the maximum so we can never overflow. */
180static int ple_window_actual_max = KVM_VMX_DEFAULT_PLE_WINDOW_MAX;
181static int ple_window_max = KVM_VMX_DEFAULT_PLE_WINDOW_MAX;
182module_param(ple_window_max, int, S_IRUGO);
183
Avi Kivity83287ea422012-09-16 15:10:57 +0300184extern const ulong vmx_return;
185
Gleb Natapov8bf00a52011-10-05 14:01:22 +0200186#define NR_AUTOLOAD_MSRS 8
Avi Kivity61d2ef22010-04-28 16:40:38 +0300187
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400188struct vmcs {
189 u32 revision_id;
190 u32 abort;
191 char data[0];
192};
193
Nadav Har'Eld462b812011-05-24 15:26:10 +0300194/*
195 * Track a VMCS that may be loaded on a certain CPU. If it is (cpu!=-1), also
196 * remember whether it was VMLAUNCHed, and maintain a linked list of all VMCSs
197 * loaded on this CPU (so we can clear them if the CPU goes down).
198 */
199struct loaded_vmcs {
200 struct vmcs *vmcs;
Jim Mattson355f4fb2016-10-28 08:29:39 -0700201 struct vmcs *shadow_vmcs;
Nadav Har'Eld462b812011-05-24 15:26:10 +0300202 int cpu;
Paolo Bonzini4c4a6f72017-07-14 13:36:11 +0200203 bool launched;
204 bool nmi_known_unmasked;
Ladi Prosek44889942017-09-22 07:53:15 +0200205 unsigned long vmcs_host_cr3; /* May not match real cr3 */
206 unsigned long vmcs_host_cr4; /* May not match real cr4 */
Paolo Bonzini8a1b4392017-11-06 13:31:12 +0100207 /* Support for vnmi-less CPUs */
208 int soft_vnmi_blocked;
209 ktime_t entry_time;
210 s64 vnmi_blocked_time;
Nadav Har'Eld462b812011-05-24 15:26:10 +0300211 struct list_head loaded_vmcss_on_cpu_link;
212};
213
Avi Kivity26bb0982009-09-07 11:14:12 +0300214struct shared_msr_entry {
215 unsigned index;
216 u64 data;
Avi Kivityd5696722009-12-02 12:28:47 +0200217 u64 mask;
Avi Kivity26bb0982009-09-07 11:14:12 +0300218};
219
Nadav Har'Elec378ae2011-05-25 23:02:54 +0300220/*
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300221 * struct vmcs12 describes the state that our guest hypervisor (L1) keeps for a
222 * single nested guest (L2), hence the name vmcs12. Any VMX implementation has
223 * a VMCS structure, and vmcs12 is our emulated VMX's VMCS. This structure is
224 * stored in guest memory specified by VMPTRLD, but is opaque to the guest,
225 * which must access it using VMREAD/VMWRITE/VMCLEAR instructions.
226 * More than one of these structures may exist, if L1 runs multiple L2 guests.
Jim Mattson00647b42017-11-27 17:22:25 -0600227 * nested_vmx_run() will use the data here to build the vmcs02: a VMCS for the
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300228 * underlying hardware which will be used to run L2.
229 * This structure is packed to ensure that its layout is identical across
230 * machines (necessary for live migration).
231 * If there are changes in this struct, VMCS12_REVISION must be changed.
232 */
Nadav Har'El22bd0352011-05-25 23:05:57 +0300233typedef u64 natural_width;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300234struct __packed vmcs12 {
235 /* According to the Intel spec, a VMCS region must start with the
236 * following two fields. Then follow implementation-specific data.
237 */
238 u32 revision_id;
239 u32 abort;
Nadav Har'El22bd0352011-05-25 23:05:57 +0300240
Nadav Har'El27d6c862011-05-25 23:06:59 +0300241 u32 launch_state; /* set to 0 by VMCLEAR, to 1 by VMLAUNCH */
242 u32 padding[7]; /* room for future expansion */
243
Nadav Har'El22bd0352011-05-25 23:05:57 +0300244 u64 io_bitmap_a;
245 u64 io_bitmap_b;
246 u64 msr_bitmap;
247 u64 vm_exit_msr_store_addr;
248 u64 vm_exit_msr_load_addr;
249 u64 vm_entry_msr_load_addr;
250 u64 tsc_offset;
251 u64 virtual_apic_page_addr;
252 u64 apic_access_addr;
Wincy Van705699a2015-02-03 23:58:17 +0800253 u64 posted_intr_desc_addr;
Bandan Das27c42a12017-08-03 15:54:42 -0400254 u64 vm_function_control;
Nadav Har'El22bd0352011-05-25 23:05:57 +0300255 u64 ept_pointer;
Wincy Van608406e2015-02-03 23:57:51 +0800256 u64 eoi_exit_bitmap0;
257 u64 eoi_exit_bitmap1;
258 u64 eoi_exit_bitmap2;
259 u64 eoi_exit_bitmap3;
Bandan Das41ab9372017-08-03 15:54:43 -0400260 u64 eptp_list_address;
Wanpeng Li81dc01f2014-12-04 19:11:07 +0800261 u64 xss_exit_bitmap;
Nadav Har'El22bd0352011-05-25 23:05:57 +0300262 u64 guest_physical_address;
263 u64 vmcs_link_pointer;
Bandan Dasc5f983f2017-05-05 15:25:14 -0400264 u64 pml_address;
Nadav Har'El22bd0352011-05-25 23:05:57 +0300265 u64 guest_ia32_debugctl;
266 u64 guest_ia32_pat;
267 u64 guest_ia32_efer;
268 u64 guest_ia32_perf_global_ctrl;
269 u64 guest_pdptr0;
270 u64 guest_pdptr1;
271 u64 guest_pdptr2;
272 u64 guest_pdptr3;
Paolo Bonzini36be0b92014-02-24 12:30:04 +0100273 u64 guest_bndcfgs;
Nadav Har'El22bd0352011-05-25 23:05:57 +0300274 u64 host_ia32_pat;
275 u64 host_ia32_efer;
276 u64 host_ia32_perf_global_ctrl;
277 u64 padding64[8]; /* room for future expansion */
278 /*
279 * To allow migration of L1 (complete with its L2 guests) between
280 * machines of different natural widths (32 or 64 bit), we cannot have
281 * unsigned long fields with no explict size. We use u64 (aliased
282 * natural_width) instead. Luckily, x86 is little-endian.
283 */
284 natural_width cr0_guest_host_mask;
285 natural_width cr4_guest_host_mask;
286 natural_width cr0_read_shadow;
287 natural_width cr4_read_shadow;
288 natural_width cr3_target_value0;
289 natural_width cr3_target_value1;
290 natural_width cr3_target_value2;
291 natural_width cr3_target_value3;
292 natural_width exit_qualification;
293 natural_width guest_linear_address;
294 natural_width guest_cr0;
295 natural_width guest_cr3;
296 natural_width guest_cr4;
297 natural_width guest_es_base;
298 natural_width guest_cs_base;
299 natural_width guest_ss_base;
300 natural_width guest_ds_base;
301 natural_width guest_fs_base;
302 natural_width guest_gs_base;
303 natural_width guest_ldtr_base;
304 natural_width guest_tr_base;
305 natural_width guest_gdtr_base;
306 natural_width guest_idtr_base;
307 natural_width guest_dr7;
308 natural_width guest_rsp;
309 natural_width guest_rip;
310 natural_width guest_rflags;
311 natural_width guest_pending_dbg_exceptions;
312 natural_width guest_sysenter_esp;
313 natural_width guest_sysenter_eip;
314 natural_width host_cr0;
315 natural_width host_cr3;
316 natural_width host_cr4;
317 natural_width host_fs_base;
318 natural_width host_gs_base;
319 natural_width host_tr_base;
320 natural_width host_gdtr_base;
321 natural_width host_idtr_base;
322 natural_width host_ia32_sysenter_esp;
323 natural_width host_ia32_sysenter_eip;
324 natural_width host_rsp;
325 natural_width host_rip;
326 natural_width paddingl[8]; /* room for future expansion */
327 u32 pin_based_vm_exec_control;
328 u32 cpu_based_vm_exec_control;
329 u32 exception_bitmap;
330 u32 page_fault_error_code_mask;
331 u32 page_fault_error_code_match;
332 u32 cr3_target_count;
333 u32 vm_exit_controls;
334 u32 vm_exit_msr_store_count;
335 u32 vm_exit_msr_load_count;
336 u32 vm_entry_controls;
337 u32 vm_entry_msr_load_count;
338 u32 vm_entry_intr_info_field;
339 u32 vm_entry_exception_error_code;
340 u32 vm_entry_instruction_len;
341 u32 tpr_threshold;
342 u32 secondary_vm_exec_control;
343 u32 vm_instruction_error;
344 u32 vm_exit_reason;
345 u32 vm_exit_intr_info;
346 u32 vm_exit_intr_error_code;
347 u32 idt_vectoring_info_field;
348 u32 idt_vectoring_error_code;
349 u32 vm_exit_instruction_len;
350 u32 vmx_instruction_info;
351 u32 guest_es_limit;
352 u32 guest_cs_limit;
353 u32 guest_ss_limit;
354 u32 guest_ds_limit;
355 u32 guest_fs_limit;
356 u32 guest_gs_limit;
357 u32 guest_ldtr_limit;
358 u32 guest_tr_limit;
359 u32 guest_gdtr_limit;
360 u32 guest_idtr_limit;
361 u32 guest_es_ar_bytes;
362 u32 guest_cs_ar_bytes;
363 u32 guest_ss_ar_bytes;
364 u32 guest_ds_ar_bytes;
365 u32 guest_fs_ar_bytes;
366 u32 guest_gs_ar_bytes;
367 u32 guest_ldtr_ar_bytes;
368 u32 guest_tr_ar_bytes;
369 u32 guest_interruptibility_info;
370 u32 guest_activity_state;
371 u32 guest_sysenter_cs;
372 u32 host_ia32_sysenter_cs;
Jan Kiszka0238ea92013-03-13 11:31:24 +0100373 u32 vmx_preemption_timer_value;
374 u32 padding32[7]; /* room for future expansion */
Nadav Har'El22bd0352011-05-25 23:05:57 +0300375 u16 virtual_processor_id;
Wincy Van705699a2015-02-03 23:58:17 +0800376 u16 posted_intr_nv;
Nadav Har'El22bd0352011-05-25 23:05:57 +0300377 u16 guest_es_selector;
378 u16 guest_cs_selector;
379 u16 guest_ss_selector;
380 u16 guest_ds_selector;
381 u16 guest_fs_selector;
382 u16 guest_gs_selector;
383 u16 guest_ldtr_selector;
384 u16 guest_tr_selector;
Wincy Van608406e2015-02-03 23:57:51 +0800385 u16 guest_intr_status;
Bandan Dasc5f983f2017-05-05 15:25:14 -0400386 u16 guest_pml_index;
Nadav Har'El22bd0352011-05-25 23:05:57 +0300387 u16 host_es_selector;
388 u16 host_cs_selector;
389 u16 host_ss_selector;
390 u16 host_ds_selector;
391 u16 host_fs_selector;
392 u16 host_gs_selector;
393 u16 host_tr_selector;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300394};
395
396/*
397 * VMCS12_REVISION is an arbitrary id that should be changed if the content or
398 * layout of struct vmcs12 is changed. MSR_IA32_VMX_BASIC returns this id, and
399 * VMPTRLD verifies that the VMCS region that L1 is loading contains this id.
400 */
401#define VMCS12_REVISION 0x11e57ed0
402
403/*
404 * VMCS12_SIZE is the number of bytes L1 should allocate for the VMXON region
405 * and any VMCS region. Although only sizeof(struct vmcs12) are used by the
406 * current implementation, 4K are reserved to avoid future complications.
407 */
408#define VMCS12_SIZE 0x1000
409
410/*
Jim Mattson5b157062017-12-22 12:11:12 -0800411 * VMCS12_MAX_FIELD_INDEX is the highest index value used in any
412 * supported VMCS12 field encoding.
413 */
414#define VMCS12_MAX_FIELD_INDEX 0x17
415
416/*
Nadav Har'Elec378ae2011-05-25 23:02:54 +0300417 * The nested_vmx structure is part of vcpu_vmx, and holds information we need
418 * for correct emulation of VMX (i.e., nested VMX) on this vcpu.
419 */
420struct nested_vmx {
421 /* Has the level1 guest done vmxon? */
422 bool vmxon;
Bandan Das3573e222014-05-06 02:19:16 -0400423 gpa_t vmxon_ptr;
Bandan Dasc5f983f2017-05-05 15:25:14 -0400424 bool pml_full;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300425
426 /* The guest-physical address of the current VMCS L1 keeps for L2 */
427 gpa_t current_vmptr;
David Matlack4f2777b2016-07-13 17:16:37 -0700428 /*
429 * Cache of the guest's VMCS, existing outside of guest memory.
430 * Loaded from guest memory during VMPTRLD. Flushed to guest
David Matlack8ca44e82017-08-01 14:00:39 -0700431 * memory during VMCLEAR and VMPTRLD.
David Matlack4f2777b2016-07-13 17:16:37 -0700432 */
433 struct vmcs12 *cached_vmcs12;
Abel Gordon012f83c2013-04-18 14:39:25 +0300434 /*
435 * Indicates if the shadow vmcs must be updated with the
436 * data hold by vmcs12
437 */
438 bool sync_shadow_vmcs;
Paolo Bonzini74a497f2017-12-20 13:55:39 +0100439 bool dirty_vmcs12;
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +0300440
Radim Krčmářdccbfcf2016-08-08 20:16:23 +0200441 bool change_vmcs01_virtual_x2apic_mode;
Nadav Har'El644d7112011-05-25 23:12:35 +0300442 /* L2 must run next, and mustn't decide to exit to L1. */
443 bool nested_run_pending;
Jim Mattson00647b42017-11-27 17:22:25 -0600444
445 struct loaded_vmcs vmcs02;
446
Nadav Har'Elfe3ef052011-05-25 23:10:02 +0300447 /*
Jim Mattson00647b42017-11-27 17:22:25 -0600448 * Guest pages referred to in the vmcs02 with host-physical
449 * pointers, so we must keep them pinned while L2 runs.
Nadav Har'Elfe3ef052011-05-25 23:10:02 +0300450 */
451 struct page *apic_access_page;
Wanpeng Lia7c0b072014-08-21 19:46:50 +0800452 struct page *virtual_apic_page;
Wincy Van705699a2015-02-03 23:58:17 +0800453 struct page *pi_desc_page;
454 struct pi_desc *pi_desc;
455 bool pi_pending;
456 u16 posted_intr_nv;
Jan Kiszkaf41245002014-03-07 20:03:13 +0100457
Radim Krčmářd048c092016-08-08 20:16:22 +0200458 unsigned long *msr_bitmap;
459
Jan Kiszkaf41245002014-03-07 20:03:13 +0100460 struct hrtimer preemption_timer;
461 bool preemption_timer_expired;
Jan Kiszka2996fca2014-06-16 13:59:43 +0200462
463 /* to migrate it to L2 if VM_ENTRY_LOAD_DEBUG_CONTROLS is off */
464 u64 vmcs01_debugctl;
Wincy Vanb9c237b2015-02-03 23:56:30 +0800465
Wanpeng Li5c614b32015-10-13 09:18:36 -0700466 u16 vpid02;
467 u16 last_vpid;
468
David Matlack0115f9c2016-11-29 18:14:06 -0800469 /*
470 * We only store the "true" versions of the VMX capability MSRs. We
471 * generate the "non-true" versions by setting the must-be-1 bits
472 * according to the SDM.
473 */
Wincy Vanb9c237b2015-02-03 23:56:30 +0800474 u32 nested_vmx_procbased_ctls_low;
475 u32 nested_vmx_procbased_ctls_high;
Wincy Vanb9c237b2015-02-03 23:56:30 +0800476 u32 nested_vmx_secondary_ctls_low;
477 u32 nested_vmx_secondary_ctls_high;
478 u32 nested_vmx_pinbased_ctls_low;
479 u32 nested_vmx_pinbased_ctls_high;
480 u32 nested_vmx_exit_ctls_low;
481 u32 nested_vmx_exit_ctls_high;
Wincy Vanb9c237b2015-02-03 23:56:30 +0800482 u32 nested_vmx_entry_ctls_low;
483 u32 nested_vmx_entry_ctls_high;
Wincy Vanb9c237b2015-02-03 23:56:30 +0800484 u32 nested_vmx_misc_low;
485 u32 nested_vmx_misc_high;
486 u32 nested_vmx_ept_caps;
Wanpeng Li99b83ac2015-10-13 09:12:21 -0700487 u32 nested_vmx_vpid_caps;
David Matlack62cc6b9d2016-11-29 18:14:07 -0800488 u64 nested_vmx_basic;
489 u64 nested_vmx_cr0_fixed0;
490 u64 nested_vmx_cr0_fixed1;
491 u64 nested_vmx_cr4_fixed0;
492 u64 nested_vmx_cr4_fixed1;
493 u64 nested_vmx_vmcs_enum;
Bandan Das27c42a12017-08-03 15:54:42 -0400494 u64 nested_vmx_vmfunc_controls;
Ladi Prosek72e9cbd2017-10-11 16:54:43 +0200495
496 /* SMM related state */
497 struct {
498 /* in VMX operation on SMM entry? */
499 bool vmxon;
500 /* in guest mode on SMM entry? */
501 bool guest_mode;
502 } smm;
Nadav Har'Elec378ae2011-05-25 23:02:54 +0300503};
504
Yang Zhang01e439b2013-04-11 19:25:12 +0800505#define POSTED_INTR_ON 0
Feng Wuebbfc762015-09-18 22:29:46 +0800506#define POSTED_INTR_SN 1
507
Yang Zhang01e439b2013-04-11 19:25:12 +0800508/* Posted-Interrupt Descriptor */
509struct pi_desc {
510 u32 pir[8]; /* Posted interrupt requested */
Feng Wu6ef15222015-09-18 22:29:45 +0800511 union {
512 struct {
513 /* bit 256 - Outstanding Notification */
514 u16 on : 1,
515 /* bit 257 - Suppress Notification */
516 sn : 1,
517 /* bit 271:258 - Reserved */
518 rsvd_1 : 14;
519 /* bit 279:272 - Notification Vector */
520 u8 nv;
521 /* bit 287:280 - Reserved */
522 u8 rsvd_2;
523 /* bit 319:288 - Notification Destination */
524 u32 ndst;
525 };
526 u64 control;
527 };
528 u32 rsvd[6];
Yang Zhang01e439b2013-04-11 19:25:12 +0800529} __aligned(64);
530
Yang Zhanga20ed542013-04-11 19:25:15 +0800531static bool pi_test_and_set_on(struct pi_desc *pi_desc)
532{
533 return test_and_set_bit(POSTED_INTR_ON,
534 (unsigned long *)&pi_desc->control);
535}
536
537static bool pi_test_and_clear_on(struct pi_desc *pi_desc)
538{
539 return test_and_clear_bit(POSTED_INTR_ON,
540 (unsigned long *)&pi_desc->control);
541}
542
543static int pi_test_and_set_pir(int vector, struct pi_desc *pi_desc)
544{
545 return test_and_set_bit(vector, (unsigned long *)pi_desc->pir);
546}
547
Feng Wuebbfc762015-09-18 22:29:46 +0800548static inline void pi_clear_sn(struct pi_desc *pi_desc)
549{
550 return clear_bit(POSTED_INTR_SN,
551 (unsigned long *)&pi_desc->control);
552}
553
554static inline void pi_set_sn(struct pi_desc *pi_desc)
555{
556 return set_bit(POSTED_INTR_SN,
557 (unsigned long *)&pi_desc->control);
558}
559
Paolo Bonziniad361092016-09-20 16:15:05 +0200560static inline void pi_clear_on(struct pi_desc *pi_desc)
561{
562 clear_bit(POSTED_INTR_ON,
563 (unsigned long *)&pi_desc->control);
564}
565
Feng Wuebbfc762015-09-18 22:29:46 +0800566static inline int pi_test_on(struct pi_desc *pi_desc)
567{
568 return test_bit(POSTED_INTR_ON,
569 (unsigned long *)&pi_desc->control);
570}
571
572static inline int pi_test_sn(struct pi_desc *pi_desc)
573{
574 return test_bit(POSTED_INTR_SN,
575 (unsigned long *)&pi_desc->control);
576}
577
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400578struct vcpu_vmx {
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000579 struct kvm_vcpu vcpu;
Avi Kivity313dbd492008-07-17 18:04:30 +0300580 unsigned long host_rsp;
Avi Kivity29bd8a72007-09-10 17:27:03 +0300581 u8 fail;
Avi Kivity51aa01d2010-07-20 14:31:20 +0300582 u32 exit_intr_info;
Avi Kivity1155f762007-11-22 11:30:47 +0200583 u32 idt_vectoring_info;
Avi Kivity6de12732011-03-07 12:51:22 +0200584 ulong rflags;
Avi Kivity26bb0982009-09-07 11:14:12 +0300585 struct shared_msr_entry *guest_msrs;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400586 int nmsrs;
587 int save_nmsrs;
Yang Zhanga547c6d2013-04-11 19:25:10 +0800588 unsigned long host_idt_base;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400589#ifdef CONFIG_X86_64
Avi Kivity44ea2b12009-09-06 15:55:37 +0300590 u64 msr_host_kernel_gs_base;
591 u64 msr_guest_kernel_gs_base;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400592#endif
Gleb Natapov2961e8762013-11-25 15:37:13 +0200593 u32 vm_entry_controls_shadow;
594 u32 vm_exit_controls_shadow;
Paolo Bonzini80154d72017-08-24 13:55:35 +0200595 u32 secondary_exec_control;
596
Nadav Har'Eld462b812011-05-24 15:26:10 +0300597 /*
598 * loaded_vmcs points to the VMCS currently used in this vcpu. For a
599 * non-nested (L1) guest, it always points to vmcs01. For a nested
600 * guest (L2), it points to a different VMCS.
601 */
602 struct loaded_vmcs vmcs01;
603 struct loaded_vmcs *loaded_vmcs;
604 bool __launched; /* temporary, used in vmx_vcpu_run */
Avi Kivity61d2ef22010-04-28 16:40:38 +0300605 struct msr_autoload {
606 unsigned nr;
607 struct vmx_msr_entry guest[NR_AUTOLOAD_MSRS];
608 struct vmx_msr_entry host[NR_AUTOLOAD_MSRS];
609 } msr_autoload;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400610 struct {
611 int loaded;
612 u16 fs_sel, gs_sel, ldt_sel;
Avi Kivityb2da15a2012-05-13 19:53:24 +0300613#ifdef CONFIG_X86_64
614 u16 ds_sel, es_sel;
615#endif
Laurent Vivier152d3f22007-08-23 16:33:11 +0200616 int gs_ldt_reload_needed;
617 int fs_reload_needed;
Liu, Jinsongda8999d2014-02-24 10:55:46 +0000618 u64 msr_host_bndcfgs;
Mike Dayd77c26f2007-10-08 09:02:08 -0400619 } host_state;
Avi Kivity9c8cba32007-11-22 11:42:59 +0200620 struct {
Avi Kivity7ffd92c2009-06-09 14:10:45 +0300621 int vm86_active;
Avi Kivity78ac8b42010-04-08 18:19:35 +0300622 ulong save_rflags;
Avi Kivityf5f7b2f2012-08-21 17:07:00 +0300623 struct kvm_segment segs[8];
624 } rmode;
625 struct {
626 u32 bitmask; /* 4 bits per segment (1 bit per field) */
Avi Kivity7ffd92c2009-06-09 14:10:45 +0300627 struct kvm_save_segment {
628 u16 selector;
629 unsigned long base;
630 u32 limit;
631 u32 ar;
Avi Kivityf5f7b2f2012-08-21 17:07:00 +0300632 } seg[8];
Avi Kivity2fb92db2011-04-27 19:42:18 +0300633 } segment_cache;
Sheng Yang2384d2b2008-01-17 15:14:33 +0800634 int vpid;
Mohammed Gamal04fa4d32008-08-17 16:39:48 +0300635 bool emulation_required;
Jan Kiszka3b86cd92008-09-26 09:30:57 +0200636
Andi Kleena0861c02009-06-08 17:37:09 +0800637 u32 exit_reason;
Sheng Yang4e47c7a2009-12-18 16:48:47 +0800638
Yang Zhang01e439b2013-04-11 19:25:12 +0800639 /* Posted interrupt descriptor */
640 struct pi_desc pi_desc;
641
Nadav Har'Elec378ae2011-05-25 23:02:54 +0300642 /* Support for a guest hypervisor (nested VMX) */
643 struct nested_vmx nested;
Radim Krčmářa7653ec2014-08-21 18:08:07 +0200644
645 /* Dynamic PLE window. */
646 int ple_window;
647 bool ple_window_dirty;
Kai Huang843e4332015-01-28 10:54:28 +0800648
649 /* Support for PML */
650#define PML_ENTITY_NUM 512
651 struct page *pml_pg;
Owen Hofmann2680d6d2016-03-01 13:36:13 -0800652
Yunhong Jiang64672c92016-06-13 14:19:59 -0700653 /* apic deadline value in host tsc */
654 u64 hv_deadline_tsc;
655
Owen Hofmann2680d6d2016-03-01 13:36:13 -0800656 u64 current_tsc_ratio;
Xiao Guangrong1be0e612016-03-22 16:51:18 +0800657
Xiao Guangrong1be0e612016-03-22 16:51:18 +0800658 u32 host_pkru;
Haozhong Zhang3b840802016-06-22 14:59:54 +0800659
Wanpeng Li74c55932017-11-29 01:31:20 -0800660 unsigned long host_debugctlmsr;
661
Haozhong Zhang37e4c992016-06-22 14:59:55 +0800662 /*
663 * Only bits masked by msr_ia32_feature_control_valid_bits can be set in
664 * msr_ia32_feature_control. FEATURE_CONTROL_LOCKED is always included
665 * in msr_ia32_feature_control_valid_bits.
666 */
Haozhong Zhang3b840802016-06-22 14:59:54 +0800667 u64 msr_ia32_feature_control;
Haozhong Zhang37e4c992016-06-22 14:59:55 +0800668 u64 msr_ia32_feature_control_valid_bits;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400669};
670
Avi Kivity2fb92db2011-04-27 19:42:18 +0300671enum segment_cache_field {
672 SEG_FIELD_SEL = 0,
673 SEG_FIELD_BASE = 1,
674 SEG_FIELD_LIMIT = 2,
675 SEG_FIELD_AR = 3,
676
677 SEG_FIELD_NR = 4
678};
679
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400680static inline struct vcpu_vmx *to_vmx(struct kvm_vcpu *vcpu)
681{
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000682 return container_of(vcpu, struct vcpu_vmx, vcpu);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400683}
684
Feng Wuefc64402015-09-18 22:29:51 +0800685static struct pi_desc *vcpu_to_pi_desc(struct kvm_vcpu *vcpu)
686{
687 return &(to_vmx(vcpu)->pi_desc);
688}
689
Jim Mattson58e9ffa2017-12-22 12:13:13 -0800690#define ROL16(val, n) ((u16)(((u16)(val) << (n)) | ((u16)(val) >> (16 - (n)))))
Nadav Har'El22bd0352011-05-25 23:05:57 +0300691#define VMCS12_OFFSET(x) offsetof(struct vmcs12, x)
Jim Mattson58e9ffa2017-12-22 12:13:13 -0800692#define FIELD(number, name) [ROL16(number, 6)] = VMCS12_OFFSET(name)
693#define FIELD64(number, name) \
694 FIELD(number, name), \
695 [ROL16(number##_HIGH, 6)] = VMCS12_OFFSET(name) + sizeof(u32)
Nadav Har'El22bd0352011-05-25 23:05:57 +0300696
Abel Gordon4607c2d2013-04-18 14:35:55 +0300697
Paolo Bonzini44900ba2017-12-13 12:58:02 +0100698static u16 shadow_read_only_fields[] = {
Paolo Bonzinic9e9dea2017-12-20 13:16:29 +0100699#define SHADOW_FIELD_RO(x) x,
700#include "vmx_shadow_fields.h"
Abel Gordon4607c2d2013-04-18 14:35:55 +0300701};
Bandan Dasfe2b2012014-04-21 15:20:14 -0400702static int max_shadow_read_only_fields =
Abel Gordon4607c2d2013-04-18 14:35:55 +0300703 ARRAY_SIZE(shadow_read_only_fields);
704
Paolo Bonzini44900ba2017-12-13 12:58:02 +0100705static u16 shadow_read_write_fields[] = {
Paolo Bonzinic9e9dea2017-12-20 13:16:29 +0100706#define SHADOW_FIELD_RW(x) x,
707#include "vmx_shadow_fields.h"
Abel Gordon4607c2d2013-04-18 14:35:55 +0300708};
Bandan Dasfe2b2012014-04-21 15:20:14 -0400709static int max_shadow_read_write_fields =
Abel Gordon4607c2d2013-04-18 14:35:55 +0300710 ARRAY_SIZE(shadow_read_write_fields);
711
Mathias Krause772e0312012-08-30 01:30:19 +0200712static const unsigned short vmcs_field_to_offset_table[] = {
Nadav Har'El22bd0352011-05-25 23:05:57 +0300713 FIELD(VIRTUAL_PROCESSOR_ID, virtual_processor_id),
Wincy Van705699a2015-02-03 23:58:17 +0800714 FIELD(POSTED_INTR_NV, posted_intr_nv),
Nadav Har'El22bd0352011-05-25 23:05:57 +0300715 FIELD(GUEST_ES_SELECTOR, guest_es_selector),
716 FIELD(GUEST_CS_SELECTOR, guest_cs_selector),
717 FIELD(GUEST_SS_SELECTOR, guest_ss_selector),
718 FIELD(GUEST_DS_SELECTOR, guest_ds_selector),
719 FIELD(GUEST_FS_SELECTOR, guest_fs_selector),
720 FIELD(GUEST_GS_SELECTOR, guest_gs_selector),
721 FIELD(GUEST_LDTR_SELECTOR, guest_ldtr_selector),
722 FIELD(GUEST_TR_SELECTOR, guest_tr_selector),
Wincy Van608406e2015-02-03 23:57:51 +0800723 FIELD(GUEST_INTR_STATUS, guest_intr_status),
Bandan Dasc5f983f2017-05-05 15:25:14 -0400724 FIELD(GUEST_PML_INDEX, guest_pml_index),
Nadav Har'El22bd0352011-05-25 23:05:57 +0300725 FIELD(HOST_ES_SELECTOR, host_es_selector),
726 FIELD(HOST_CS_SELECTOR, host_cs_selector),
727 FIELD(HOST_SS_SELECTOR, host_ss_selector),
728 FIELD(HOST_DS_SELECTOR, host_ds_selector),
729 FIELD(HOST_FS_SELECTOR, host_fs_selector),
730 FIELD(HOST_GS_SELECTOR, host_gs_selector),
731 FIELD(HOST_TR_SELECTOR, host_tr_selector),
732 FIELD64(IO_BITMAP_A, io_bitmap_a),
733 FIELD64(IO_BITMAP_B, io_bitmap_b),
734 FIELD64(MSR_BITMAP, msr_bitmap),
735 FIELD64(VM_EXIT_MSR_STORE_ADDR, vm_exit_msr_store_addr),
736 FIELD64(VM_EXIT_MSR_LOAD_ADDR, vm_exit_msr_load_addr),
737 FIELD64(VM_ENTRY_MSR_LOAD_ADDR, vm_entry_msr_load_addr),
738 FIELD64(TSC_OFFSET, tsc_offset),
739 FIELD64(VIRTUAL_APIC_PAGE_ADDR, virtual_apic_page_addr),
740 FIELD64(APIC_ACCESS_ADDR, apic_access_addr),
Wincy Van705699a2015-02-03 23:58:17 +0800741 FIELD64(POSTED_INTR_DESC_ADDR, posted_intr_desc_addr),
Bandan Das27c42a12017-08-03 15:54:42 -0400742 FIELD64(VM_FUNCTION_CONTROL, vm_function_control),
Nadav Har'El22bd0352011-05-25 23:05:57 +0300743 FIELD64(EPT_POINTER, ept_pointer),
Wincy Van608406e2015-02-03 23:57:51 +0800744 FIELD64(EOI_EXIT_BITMAP0, eoi_exit_bitmap0),
745 FIELD64(EOI_EXIT_BITMAP1, eoi_exit_bitmap1),
746 FIELD64(EOI_EXIT_BITMAP2, eoi_exit_bitmap2),
747 FIELD64(EOI_EXIT_BITMAP3, eoi_exit_bitmap3),
Bandan Das41ab9372017-08-03 15:54:43 -0400748 FIELD64(EPTP_LIST_ADDRESS, eptp_list_address),
Wanpeng Li81dc01f2014-12-04 19:11:07 +0800749 FIELD64(XSS_EXIT_BITMAP, xss_exit_bitmap),
Nadav Har'El22bd0352011-05-25 23:05:57 +0300750 FIELD64(GUEST_PHYSICAL_ADDRESS, guest_physical_address),
751 FIELD64(VMCS_LINK_POINTER, vmcs_link_pointer),
Bandan Dasc5f983f2017-05-05 15:25:14 -0400752 FIELD64(PML_ADDRESS, pml_address),
Nadav Har'El22bd0352011-05-25 23:05:57 +0300753 FIELD64(GUEST_IA32_DEBUGCTL, guest_ia32_debugctl),
754 FIELD64(GUEST_IA32_PAT, guest_ia32_pat),
755 FIELD64(GUEST_IA32_EFER, guest_ia32_efer),
756 FIELD64(GUEST_IA32_PERF_GLOBAL_CTRL, guest_ia32_perf_global_ctrl),
757 FIELD64(GUEST_PDPTR0, guest_pdptr0),
758 FIELD64(GUEST_PDPTR1, guest_pdptr1),
759 FIELD64(GUEST_PDPTR2, guest_pdptr2),
760 FIELD64(GUEST_PDPTR3, guest_pdptr3),
Paolo Bonzini36be0b92014-02-24 12:30:04 +0100761 FIELD64(GUEST_BNDCFGS, guest_bndcfgs),
Nadav Har'El22bd0352011-05-25 23:05:57 +0300762 FIELD64(HOST_IA32_PAT, host_ia32_pat),
763 FIELD64(HOST_IA32_EFER, host_ia32_efer),
764 FIELD64(HOST_IA32_PERF_GLOBAL_CTRL, host_ia32_perf_global_ctrl),
765 FIELD(PIN_BASED_VM_EXEC_CONTROL, pin_based_vm_exec_control),
766 FIELD(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control),
767 FIELD(EXCEPTION_BITMAP, exception_bitmap),
768 FIELD(PAGE_FAULT_ERROR_CODE_MASK, page_fault_error_code_mask),
769 FIELD(PAGE_FAULT_ERROR_CODE_MATCH, page_fault_error_code_match),
770 FIELD(CR3_TARGET_COUNT, cr3_target_count),
771 FIELD(VM_EXIT_CONTROLS, vm_exit_controls),
772 FIELD(VM_EXIT_MSR_STORE_COUNT, vm_exit_msr_store_count),
773 FIELD(VM_EXIT_MSR_LOAD_COUNT, vm_exit_msr_load_count),
774 FIELD(VM_ENTRY_CONTROLS, vm_entry_controls),
775 FIELD(VM_ENTRY_MSR_LOAD_COUNT, vm_entry_msr_load_count),
776 FIELD(VM_ENTRY_INTR_INFO_FIELD, vm_entry_intr_info_field),
777 FIELD(VM_ENTRY_EXCEPTION_ERROR_CODE, vm_entry_exception_error_code),
778 FIELD(VM_ENTRY_INSTRUCTION_LEN, vm_entry_instruction_len),
779 FIELD(TPR_THRESHOLD, tpr_threshold),
780 FIELD(SECONDARY_VM_EXEC_CONTROL, secondary_vm_exec_control),
781 FIELD(VM_INSTRUCTION_ERROR, vm_instruction_error),
782 FIELD(VM_EXIT_REASON, vm_exit_reason),
783 FIELD(VM_EXIT_INTR_INFO, vm_exit_intr_info),
784 FIELD(VM_EXIT_INTR_ERROR_CODE, vm_exit_intr_error_code),
785 FIELD(IDT_VECTORING_INFO_FIELD, idt_vectoring_info_field),
786 FIELD(IDT_VECTORING_ERROR_CODE, idt_vectoring_error_code),
787 FIELD(VM_EXIT_INSTRUCTION_LEN, vm_exit_instruction_len),
788 FIELD(VMX_INSTRUCTION_INFO, vmx_instruction_info),
789 FIELD(GUEST_ES_LIMIT, guest_es_limit),
790 FIELD(GUEST_CS_LIMIT, guest_cs_limit),
791 FIELD(GUEST_SS_LIMIT, guest_ss_limit),
792 FIELD(GUEST_DS_LIMIT, guest_ds_limit),
793 FIELD(GUEST_FS_LIMIT, guest_fs_limit),
794 FIELD(GUEST_GS_LIMIT, guest_gs_limit),
795 FIELD(GUEST_LDTR_LIMIT, guest_ldtr_limit),
796 FIELD(GUEST_TR_LIMIT, guest_tr_limit),
797 FIELD(GUEST_GDTR_LIMIT, guest_gdtr_limit),
798 FIELD(GUEST_IDTR_LIMIT, guest_idtr_limit),
799 FIELD(GUEST_ES_AR_BYTES, guest_es_ar_bytes),
800 FIELD(GUEST_CS_AR_BYTES, guest_cs_ar_bytes),
801 FIELD(GUEST_SS_AR_BYTES, guest_ss_ar_bytes),
802 FIELD(GUEST_DS_AR_BYTES, guest_ds_ar_bytes),
803 FIELD(GUEST_FS_AR_BYTES, guest_fs_ar_bytes),
804 FIELD(GUEST_GS_AR_BYTES, guest_gs_ar_bytes),
805 FIELD(GUEST_LDTR_AR_BYTES, guest_ldtr_ar_bytes),
806 FIELD(GUEST_TR_AR_BYTES, guest_tr_ar_bytes),
807 FIELD(GUEST_INTERRUPTIBILITY_INFO, guest_interruptibility_info),
808 FIELD(GUEST_ACTIVITY_STATE, guest_activity_state),
809 FIELD(GUEST_SYSENTER_CS, guest_sysenter_cs),
810 FIELD(HOST_IA32_SYSENTER_CS, host_ia32_sysenter_cs),
Jan Kiszka0238ea92013-03-13 11:31:24 +0100811 FIELD(VMX_PREEMPTION_TIMER_VALUE, vmx_preemption_timer_value),
Nadav Har'El22bd0352011-05-25 23:05:57 +0300812 FIELD(CR0_GUEST_HOST_MASK, cr0_guest_host_mask),
813 FIELD(CR4_GUEST_HOST_MASK, cr4_guest_host_mask),
814 FIELD(CR0_READ_SHADOW, cr0_read_shadow),
815 FIELD(CR4_READ_SHADOW, cr4_read_shadow),
816 FIELD(CR3_TARGET_VALUE0, cr3_target_value0),
817 FIELD(CR3_TARGET_VALUE1, cr3_target_value1),
818 FIELD(CR3_TARGET_VALUE2, cr3_target_value2),
819 FIELD(CR3_TARGET_VALUE3, cr3_target_value3),
820 FIELD(EXIT_QUALIFICATION, exit_qualification),
821 FIELD(GUEST_LINEAR_ADDRESS, guest_linear_address),
822 FIELD(GUEST_CR0, guest_cr0),
823 FIELD(GUEST_CR3, guest_cr3),
824 FIELD(GUEST_CR4, guest_cr4),
825 FIELD(GUEST_ES_BASE, guest_es_base),
826 FIELD(GUEST_CS_BASE, guest_cs_base),
827 FIELD(GUEST_SS_BASE, guest_ss_base),
828 FIELD(GUEST_DS_BASE, guest_ds_base),
829 FIELD(GUEST_FS_BASE, guest_fs_base),
830 FIELD(GUEST_GS_BASE, guest_gs_base),
831 FIELD(GUEST_LDTR_BASE, guest_ldtr_base),
832 FIELD(GUEST_TR_BASE, guest_tr_base),
833 FIELD(GUEST_GDTR_BASE, guest_gdtr_base),
834 FIELD(GUEST_IDTR_BASE, guest_idtr_base),
835 FIELD(GUEST_DR7, guest_dr7),
836 FIELD(GUEST_RSP, guest_rsp),
837 FIELD(GUEST_RIP, guest_rip),
838 FIELD(GUEST_RFLAGS, guest_rflags),
839 FIELD(GUEST_PENDING_DBG_EXCEPTIONS, guest_pending_dbg_exceptions),
840 FIELD(GUEST_SYSENTER_ESP, guest_sysenter_esp),
841 FIELD(GUEST_SYSENTER_EIP, guest_sysenter_eip),
842 FIELD(HOST_CR0, host_cr0),
843 FIELD(HOST_CR3, host_cr3),
844 FIELD(HOST_CR4, host_cr4),
845 FIELD(HOST_FS_BASE, host_fs_base),
846 FIELD(HOST_GS_BASE, host_gs_base),
847 FIELD(HOST_TR_BASE, host_tr_base),
848 FIELD(HOST_GDTR_BASE, host_gdtr_base),
849 FIELD(HOST_IDTR_BASE, host_idtr_base),
850 FIELD(HOST_IA32_SYSENTER_ESP, host_ia32_sysenter_esp),
851 FIELD(HOST_IA32_SYSENTER_EIP, host_ia32_sysenter_eip),
852 FIELD(HOST_RSP, host_rsp),
853 FIELD(HOST_RIP, host_rip),
854};
Nadav Har'El22bd0352011-05-25 23:05:57 +0300855
856static inline short vmcs_field_to_offset(unsigned long field)
857{
Jim Mattson58e9ffa2017-12-22 12:13:13 -0800858 unsigned index;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +0100859
Jim Mattson58e9ffa2017-12-22 12:13:13 -0800860 if (field >> 15)
861 return -ENOENT;
862
863 index = ROL16(field, 6);
864 if (index >= ARRAY_SIZE(vmcs_field_to_offset_table))
Andrew Honig75f139a2018-01-10 10:12:03 -0800865 return -ENOENT;
866
867 /*
868 * FIXME: Mitigation for CVE-2017-5753. To be replaced with a
869 * generic mechanism.
870 */
871 asm("lfence");
872
Jim Mattson58e9ffa2017-12-22 12:13:13 -0800873 if (vmcs_field_to_offset_table[index] == 0)
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +0100874 return -ENOENT;
875
Jim Mattson58e9ffa2017-12-22 12:13:13 -0800876 return vmcs_field_to_offset_table[index];
Nadav Har'El22bd0352011-05-25 23:05:57 +0300877}
878
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300879static inline struct vmcs12 *get_vmcs12(struct kvm_vcpu *vcpu)
880{
David Matlack4f2777b2016-07-13 17:16:37 -0700881 return to_vmx(vcpu)->nested.cached_vmcs12;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300882}
883
Peter Feiner995f00a2017-06-30 17:26:32 -0700884static bool nested_ept_ad_enabled(struct kvm_vcpu *vcpu);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +0300885static unsigned long nested_ept_get_cr3(struct kvm_vcpu *vcpu);
Peter Feiner995f00a2017-06-30 17:26:32 -0700886static u64 construct_eptp(struct kvm_vcpu *vcpu, unsigned long root_hpa);
Wanpeng Lif53cd632014-12-02 19:14:58 +0800887static bool vmx_xsaves_supported(void);
Orit Wassermanb246dd52012-05-31 14:49:22 +0300888static void vmx_set_segment(struct kvm_vcpu *vcpu,
889 struct kvm_segment *var, int seg);
890static void vmx_get_segment(struct kvm_vcpu *vcpu,
891 struct kvm_segment *var, int seg);
Gleb Natapovd99e4152012-12-20 16:57:45 +0200892static bool guest_state_valid(struct kvm_vcpu *vcpu);
893static u32 vmx_segment_access_rights(struct kvm_segment *var);
Abel Gordon16f5b902013-04-18 14:38:25 +0300894static void copy_shadow_to_vmcs12(struct vcpu_vmx *vmx);
Paolo Bonzinib96fb432017-07-27 12:29:32 +0200895static bool vmx_get_nmi_mask(struct kvm_vcpu *vcpu);
896static void vmx_set_nmi_mask(struct kvm_vcpu *vcpu, bool masked);
897static bool nested_vmx_is_page_fault_vmexit(struct vmcs12 *vmcs12,
898 u16 error_code);
Avi Kivity75880a02007-06-20 11:20:04 +0300899
Avi Kivity6aa8b732006-12-10 02:21:36 -0800900static DEFINE_PER_CPU(struct vmcs *, vmxarea);
901static DEFINE_PER_CPU(struct vmcs *, current_vmcs);
Nadav Har'Eld462b812011-05-24 15:26:10 +0300902/*
903 * We maintain a per-CPU linked-list of VMCS loaded on that CPU. This is needed
904 * when a CPU is brought down, and we need to VMCLEAR all VMCSs loaded on it.
905 */
906static DEFINE_PER_CPU(struct list_head, loaded_vmcss_on_cpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800907
Feng Wubf9f6ac2015-09-18 22:29:55 +0800908/*
909 * We maintian a per-CPU linked-list of vCPU, so in wakeup_handler() we
910 * can find which vCPU should be waken up.
911 */
912static DEFINE_PER_CPU(struct list_head, blocked_vcpu_on_cpu);
913static DEFINE_PER_CPU(spinlock_t, blocked_vcpu_on_cpu_lock);
914
Radim Krčmář23611332016-09-29 22:41:33 +0200915enum {
Radim Krčmář23611332016-09-29 22:41:33 +0200916 VMX_MSR_BITMAP_LEGACY,
917 VMX_MSR_BITMAP_LONGMODE,
918 VMX_MSR_BITMAP_LEGACY_X2APIC_APICV,
919 VMX_MSR_BITMAP_LONGMODE_X2APIC_APICV,
920 VMX_MSR_BITMAP_LEGACY_X2APIC,
921 VMX_MSR_BITMAP_LONGMODE_X2APIC,
922 VMX_VMREAD_BITMAP,
923 VMX_VMWRITE_BITMAP,
924 VMX_BITMAP_NR
925};
926
927static unsigned long *vmx_bitmap[VMX_BITMAP_NR];
928
Radim Krčmář23611332016-09-29 22:41:33 +0200929#define vmx_msr_bitmap_legacy (vmx_bitmap[VMX_MSR_BITMAP_LEGACY])
930#define vmx_msr_bitmap_longmode (vmx_bitmap[VMX_MSR_BITMAP_LONGMODE])
931#define vmx_msr_bitmap_legacy_x2apic_apicv (vmx_bitmap[VMX_MSR_BITMAP_LEGACY_X2APIC_APICV])
932#define vmx_msr_bitmap_longmode_x2apic_apicv (vmx_bitmap[VMX_MSR_BITMAP_LONGMODE_X2APIC_APICV])
933#define vmx_msr_bitmap_legacy_x2apic (vmx_bitmap[VMX_MSR_BITMAP_LEGACY_X2APIC])
934#define vmx_msr_bitmap_longmode_x2apic (vmx_bitmap[VMX_MSR_BITMAP_LONGMODE_X2APIC])
935#define vmx_vmread_bitmap (vmx_bitmap[VMX_VMREAD_BITMAP])
936#define vmx_vmwrite_bitmap (vmx_bitmap[VMX_VMWRITE_BITMAP])
He, Qingfdef3ad2007-04-30 09:45:24 +0300937
Avi Kivity110312c2010-12-21 12:54:20 +0200938static bool cpu_has_load_ia32_efer;
Gleb Natapov8bf00a52011-10-05 14:01:22 +0200939static bool cpu_has_load_perf_global_ctrl;
Avi Kivity110312c2010-12-21 12:54:20 +0200940
Sheng Yang2384d2b2008-01-17 15:14:33 +0800941static DECLARE_BITMAP(vmx_vpid_bitmap, VMX_NR_VPIDS);
942static DEFINE_SPINLOCK(vmx_vpid_lock);
943
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +0300944static struct vmcs_config {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800945 int size;
946 int order;
Jan Dakinevich9ac7e3e2016-09-04 21:23:15 +0300947 u32 basic_cap;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800948 u32 revision_id;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +0300949 u32 pin_based_exec_ctrl;
950 u32 cpu_based_exec_ctrl;
Sheng Yangf78e0e22007-10-29 09:40:42 +0800951 u32 cpu_based_2nd_exec_ctrl;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +0300952 u32 vmexit_ctrl;
953 u32 vmentry_ctrl;
954} vmcs_config;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800955
Hannes Ederefff9e52008-11-28 17:02:06 +0100956static struct vmx_capability {
Sheng Yangd56f5462008-04-25 10:13:16 +0800957 u32 ept;
958 u32 vpid;
959} vmx_capability;
960
Avi Kivity6aa8b732006-12-10 02:21:36 -0800961#define VMX_SEGMENT_FIELD(seg) \
962 [VCPU_SREG_##seg] = { \
963 .selector = GUEST_##seg##_SELECTOR, \
964 .base = GUEST_##seg##_BASE, \
965 .limit = GUEST_##seg##_LIMIT, \
966 .ar_bytes = GUEST_##seg##_AR_BYTES, \
967 }
968
Mathias Krause772e0312012-08-30 01:30:19 +0200969static const struct kvm_vmx_segment_field {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800970 unsigned selector;
971 unsigned base;
972 unsigned limit;
973 unsigned ar_bytes;
974} kvm_vmx_segment_fields[] = {
975 VMX_SEGMENT_FIELD(CS),
976 VMX_SEGMENT_FIELD(DS),
977 VMX_SEGMENT_FIELD(ES),
978 VMX_SEGMENT_FIELD(FS),
979 VMX_SEGMENT_FIELD(GS),
980 VMX_SEGMENT_FIELD(SS),
981 VMX_SEGMENT_FIELD(TR),
982 VMX_SEGMENT_FIELD(LDTR),
983};
984
Avi Kivity26bb0982009-09-07 11:14:12 +0300985static u64 host_efer;
986
Avi Kivity6de4f3a2009-05-31 22:58:47 +0300987static void ept_save_pdptrs(struct kvm_vcpu *vcpu);
988
Avi Kivity4d56c8a2007-04-19 14:28:44 +0300989/*
Brian Gerst8c065852010-07-17 09:03:26 -0400990 * Keep MSR_STAR at the end, as setup_msrs() will try to optimize it
Avi Kivity4d56c8a2007-04-19 14:28:44 +0300991 * away by decrementing the array size.
992 */
Avi Kivity6aa8b732006-12-10 02:21:36 -0800993static const u32 vmx_msr_index[] = {
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800994#ifdef CONFIG_X86_64
Avi Kivity44ea2b12009-09-06 15:55:37 +0300995 MSR_SYSCALL_MASK, MSR_LSTAR, MSR_CSTAR,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800996#endif
Brian Gerst8c065852010-07-17 09:03:26 -0400997 MSR_EFER, MSR_TSC_AUX, MSR_STAR,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800998};
Avi Kivity6aa8b732006-12-10 02:21:36 -0800999
Jan Kiszka5bb16012016-02-09 20:14:21 +01001000static inline bool is_exception_n(u32 intr_info, u8 vector)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001001{
1002 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
1003 INTR_INFO_VALID_MASK)) ==
Jan Kiszka5bb16012016-02-09 20:14:21 +01001004 (INTR_TYPE_HARD_EXCEPTION | vector | INTR_INFO_VALID_MASK);
1005}
1006
Jan Kiszka6f054852016-02-09 20:15:18 +01001007static inline bool is_debug(u32 intr_info)
1008{
1009 return is_exception_n(intr_info, DB_VECTOR);
1010}
1011
1012static inline bool is_breakpoint(u32 intr_info)
1013{
1014 return is_exception_n(intr_info, BP_VECTOR);
1015}
1016
Jan Kiszka5bb16012016-02-09 20:14:21 +01001017static inline bool is_page_fault(u32 intr_info)
1018{
1019 return is_exception_n(intr_info, PF_VECTOR);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001020}
1021
Gui Jianfeng31299942010-03-15 17:29:09 +08001022static inline bool is_no_device(u32 intr_info)
Anthony Liguori2ab455c2007-04-27 09:29:49 +03001023{
Jan Kiszka5bb16012016-02-09 20:14:21 +01001024 return is_exception_n(intr_info, NM_VECTOR);
Anthony Liguori2ab455c2007-04-27 09:29:49 +03001025}
1026
Gui Jianfeng31299942010-03-15 17:29:09 +08001027static inline bool is_invalid_opcode(u32 intr_info)
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001028{
Jan Kiszka5bb16012016-02-09 20:14:21 +01001029 return is_exception_n(intr_info, UD_VECTOR);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001030}
1031
Gui Jianfeng31299942010-03-15 17:29:09 +08001032static inline bool is_external_interrupt(u32 intr_info)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001033{
1034 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK))
1035 == (INTR_TYPE_EXT_INTR | INTR_INFO_VALID_MASK);
1036}
1037
Gui Jianfeng31299942010-03-15 17:29:09 +08001038static inline bool is_machine_check(u32 intr_info)
Andi Kleena0861c02009-06-08 17:37:09 +08001039{
1040 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
1041 INTR_INFO_VALID_MASK)) ==
1042 (INTR_TYPE_HARD_EXCEPTION | MC_VECTOR | INTR_INFO_VALID_MASK);
1043}
1044
Gui Jianfeng31299942010-03-15 17:29:09 +08001045static inline bool cpu_has_vmx_msr_bitmap(void)
Sheng Yang25c5f222008-03-28 13:18:56 +08001046{
Sheng Yang04547152009-04-01 15:52:31 +08001047 return vmcs_config.cpu_based_exec_ctrl & CPU_BASED_USE_MSR_BITMAPS;
Sheng Yang25c5f222008-03-28 13:18:56 +08001048}
1049
Gui Jianfeng31299942010-03-15 17:29:09 +08001050static inline bool cpu_has_vmx_tpr_shadow(void)
Yang, Sheng6e5d8652007-09-12 18:03:11 +08001051{
Sheng Yang04547152009-04-01 15:52:31 +08001052 return vmcs_config.cpu_based_exec_ctrl & CPU_BASED_TPR_SHADOW;
Yang, Sheng6e5d8652007-09-12 18:03:11 +08001053}
1054
Paolo Bonzini35754c92015-07-29 12:05:37 +02001055static inline bool cpu_need_tpr_shadow(struct kvm_vcpu *vcpu)
Yang, Sheng6e5d8652007-09-12 18:03:11 +08001056{
Paolo Bonzini35754c92015-07-29 12:05:37 +02001057 return cpu_has_vmx_tpr_shadow() && lapic_in_kernel(vcpu);
Yang, Sheng6e5d8652007-09-12 18:03:11 +08001058}
1059
Gui Jianfeng31299942010-03-15 17:29:09 +08001060static inline bool cpu_has_secondary_exec_ctrls(void)
Sheng Yangf78e0e22007-10-29 09:40:42 +08001061{
Sheng Yang04547152009-04-01 15:52:31 +08001062 return vmcs_config.cpu_based_exec_ctrl &
1063 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
Sheng Yangf78e0e22007-10-29 09:40:42 +08001064}
1065
Avi Kivity774ead32007-12-26 13:57:04 +02001066static inline bool cpu_has_vmx_virtualize_apic_accesses(void)
Sheng Yangf78e0e22007-10-29 09:40:42 +08001067{
Sheng Yang04547152009-04-01 15:52:31 +08001068 return vmcs_config.cpu_based_2nd_exec_ctrl &
1069 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
1070}
1071
Yang Zhang8d146952013-01-25 10:18:50 +08001072static inline bool cpu_has_vmx_virtualize_x2apic_mode(void)
1073{
1074 return vmcs_config.cpu_based_2nd_exec_ctrl &
1075 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
1076}
1077
Yang Zhang83d4c282013-01-25 10:18:49 +08001078static inline bool cpu_has_vmx_apic_register_virt(void)
1079{
1080 return vmcs_config.cpu_based_2nd_exec_ctrl &
1081 SECONDARY_EXEC_APIC_REGISTER_VIRT;
1082}
1083
Yang Zhangc7c9c562013-01-25 10:18:51 +08001084static inline bool cpu_has_vmx_virtual_intr_delivery(void)
1085{
1086 return vmcs_config.cpu_based_2nd_exec_ctrl &
1087 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY;
1088}
1089
Yunhong Jiang64672c92016-06-13 14:19:59 -07001090/*
1091 * Comment's format: document - errata name - stepping - processor name.
1092 * Refer from
1093 * https://www.virtualbox.org/svn/vbox/trunk/src/VBox/VMM/VMMR0/HMR0.cpp
1094 */
1095static u32 vmx_preemption_cpu_tfms[] = {
1096/* 323344.pdf - BA86 - D0 - Xeon 7500 Series */
10970x000206E6,
1098/* 323056.pdf - AAX65 - C2 - Xeon L3406 */
1099/* 322814.pdf - AAT59 - C2 - i7-600, i5-500, i5-400 and i3-300 Mobile */
1100/* 322911.pdf - AAU65 - C2 - i5-600, i3-500 Desktop and Pentium G6950 */
11010x00020652,
1102/* 322911.pdf - AAU65 - K0 - i5-600, i3-500 Desktop and Pentium G6950 */
11030x00020655,
1104/* 322373.pdf - AAO95 - B1 - Xeon 3400 Series */
1105/* 322166.pdf - AAN92 - B1 - i7-800 and i5-700 Desktop */
1106/*
1107 * 320767.pdf - AAP86 - B1 -
1108 * i7-900 Mobile Extreme, i7-800 and i7-700 Mobile
1109 */
11100x000106E5,
1111/* 321333.pdf - AAM126 - C0 - Xeon 3500 */
11120x000106A0,
1113/* 321333.pdf - AAM126 - C1 - Xeon 3500 */
11140x000106A1,
1115/* 320836.pdf - AAJ124 - C0 - i7-900 Desktop Extreme and i7-900 Desktop */
11160x000106A4,
1117 /* 321333.pdf - AAM126 - D0 - Xeon 3500 */
1118 /* 321324.pdf - AAK139 - D0 - Xeon 5500 */
1119 /* 320836.pdf - AAJ124 - D0 - i7-900 Extreme and i7-900 Desktop */
11200x000106A5,
1121};
1122
1123static inline bool cpu_has_broken_vmx_preemption_timer(void)
1124{
1125 u32 eax = cpuid_eax(0x00000001), i;
1126
1127 /* Clear the reserved bits */
1128 eax &= ~(0x3U << 14 | 0xfU << 28);
Wei Yongjun03f6a222016-07-04 15:13:07 +00001129 for (i = 0; i < ARRAY_SIZE(vmx_preemption_cpu_tfms); i++)
Yunhong Jiang64672c92016-06-13 14:19:59 -07001130 if (eax == vmx_preemption_cpu_tfms[i])
1131 return true;
1132
1133 return false;
1134}
1135
1136static inline bool cpu_has_vmx_preemption_timer(void)
1137{
Yunhong Jiang64672c92016-06-13 14:19:59 -07001138 return vmcs_config.pin_based_exec_ctrl &
1139 PIN_BASED_VMX_PREEMPTION_TIMER;
1140}
1141
Yang Zhang01e439b2013-04-11 19:25:12 +08001142static inline bool cpu_has_vmx_posted_intr(void)
1143{
Paolo Bonzinid6a858d2015-09-28 11:58:14 +02001144 return IS_ENABLED(CONFIG_X86_LOCAL_APIC) &&
1145 vmcs_config.pin_based_exec_ctrl & PIN_BASED_POSTED_INTR;
Yang Zhang01e439b2013-04-11 19:25:12 +08001146}
1147
1148static inline bool cpu_has_vmx_apicv(void)
1149{
1150 return cpu_has_vmx_apic_register_virt() &&
1151 cpu_has_vmx_virtual_intr_delivery() &&
1152 cpu_has_vmx_posted_intr();
1153}
1154
Sheng Yang04547152009-04-01 15:52:31 +08001155static inline bool cpu_has_vmx_flexpriority(void)
1156{
1157 return cpu_has_vmx_tpr_shadow() &&
1158 cpu_has_vmx_virtualize_apic_accesses();
Sheng Yangf78e0e22007-10-29 09:40:42 +08001159}
1160
Marcelo Tosattie7997942009-06-11 12:07:40 -03001161static inline bool cpu_has_vmx_ept_execute_only(void)
1162{
Gui Jianfeng31299942010-03-15 17:29:09 +08001163 return vmx_capability.ept & VMX_EPT_EXECUTE_ONLY_BIT;
Marcelo Tosattie7997942009-06-11 12:07:40 -03001164}
1165
Marcelo Tosattie7997942009-06-11 12:07:40 -03001166static inline bool cpu_has_vmx_ept_2m_page(void)
1167{
Gui Jianfeng31299942010-03-15 17:29:09 +08001168 return vmx_capability.ept & VMX_EPT_2MB_PAGE_BIT;
Marcelo Tosattie7997942009-06-11 12:07:40 -03001169}
1170
Sheng Yang878403b2010-01-05 19:02:29 +08001171static inline bool cpu_has_vmx_ept_1g_page(void)
1172{
Gui Jianfeng31299942010-03-15 17:29:09 +08001173 return vmx_capability.ept & VMX_EPT_1GB_PAGE_BIT;
Sheng Yang878403b2010-01-05 19:02:29 +08001174}
1175
Sheng Yang4bc9b982010-06-02 14:05:24 +08001176static inline bool cpu_has_vmx_ept_4levels(void)
1177{
1178 return vmx_capability.ept & VMX_EPT_PAGE_WALK_4_BIT;
1179}
1180
David Hildenbrand42aa53b2017-08-10 23:15:29 +02001181static inline bool cpu_has_vmx_ept_mt_wb(void)
1182{
1183 return vmx_capability.ept & VMX_EPTP_WB_BIT;
1184}
1185
Yu Zhang855feb62017-08-24 20:27:55 +08001186static inline bool cpu_has_vmx_ept_5levels(void)
1187{
1188 return vmx_capability.ept & VMX_EPT_PAGE_WALK_5_BIT;
1189}
1190
Xudong Hao83c3a332012-05-28 19:33:35 +08001191static inline bool cpu_has_vmx_ept_ad_bits(void)
1192{
1193 return vmx_capability.ept & VMX_EPT_AD_BIT;
1194}
1195
Gui Jianfeng31299942010-03-15 17:29:09 +08001196static inline bool cpu_has_vmx_invept_context(void)
Sheng Yangd56f5462008-04-25 10:13:16 +08001197{
Gui Jianfeng31299942010-03-15 17:29:09 +08001198 return vmx_capability.ept & VMX_EPT_EXTENT_CONTEXT_BIT;
Sheng Yangd56f5462008-04-25 10:13:16 +08001199}
1200
Gui Jianfeng31299942010-03-15 17:29:09 +08001201static inline bool cpu_has_vmx_invept_global(void)
Sheng Yangd56f5462008-04-25 10:13:16 +08001202{
Gui Jianfeng31299942010-03-15 17:29:09 +08001203 return vmx_capability.ept & VMX_EPT_EXTENT_GLOBAL_BIT;
Sheng Yangd56f5462008-04-25 10:13:16 +08001204}
1205
Gui Jianfeng518c8ae2010-06-04 08:51:39 +08001206static inline bool cpu_has_vmx_invvpid_single(void)
1207{
1208 return vmx_capability.vpid & VMX_VPID_EXTENT_SINGLE_CONTEXT_BIT;
1209}
1210
Gui Jianfengb9d762f2010-06-07 10:32:29 +08001211static inline bool cpu_has_vmx_invvpid_global(void)
1212{
1213 return vmx_capability.vpid & VMX_VPID_EXTENT_GLOBAL_CONTEXT_BIT;
1214}
1215
Wanpeng Li08d839c2017-03-23 05:30:08 -07001216static inline bool cpu_has_vmx_invvpid(void)
1217{
1218 return vmx_capability.vpid & VMX_VPID_INVVPID_BIT;
1219}
1220
Gui Jianfeng31299942010-03-15 17:29:09 +08001221static inline bool cpu_has_vmx_ept(void)
Sheng Yangd56f5462008-04-25 10:13:16 +08001222{
Sheng Yang04547152009-04-01 15:52:31 +08001223 return vmcs_config.cpu_based_2nd_exec_ctrl &
1224 SECONDARY_EXEC_ENABLE_EPT;
Sheng Yangd56f5462008-04-25 10:13:16 +08001225}
1226
Gui Jianfeng31299942010-03-15 17:29:09 +08001227static inline bool cpu_has_vmx_unrestricted_guest(void)
Nitin A Kamble3a624e22009-06-08 11:34:16 -07001228{
1229 return vmcs_config.cpu_based_2nd_exec_ctrl &
1230 SECONDARY_EXEC_UNRESTRICTED_GUEST;
1231}
1232
Gui Jianfeng31299942010-03-15 17:29:09 +08001233static inline bool cpu_has_vmx_ple(void)
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08001234{
1235 return vmcs_config.cpu_based_2nd_exec_ctrl &
1236 SECONDARY_EXEC_PAUSE_LOOP_EXITING;
1237}
1238
Jan Dakinevich9ac7e3e2016-09-04 21:23:15 +03001239static inline bool cpu_has_vmx_basic_inout(void)
1240{
1241 return (((u64)vmcs_config.basic_cap << 32) & VMX_BASIC_INOUT);
1242}
1243
Paolo Bonzini35754c92015-07-29 12:05:37 +02001244static inline bool cpu_need_virtualize_apic_accesses(struct kvm_vcpu *vcpu)
Sheng Yangf78e0e22007-10-29 09:40:42 +08001245{
Paolo Bonzini35754c92015-07-29 12:05:37 +02001246 return flexpriority_enabled && lapic_in_kernel(vcpu);
Sheng Yangf78e0e22007-10-29 09:40:42 +08001247}
1248
Gui Jianfeng31299942010-03-15 17:29:09 +08001249static inline bool cpu_has_vmx_vpid(void)
Sheng Yang2384d2b2008-01-17 15:14:33 +08001250{
Sheng Yang04547152009-04-01 15:52:31 +08001251 return vmcs_config.cpu_based_2nd_exec_ctrl &
1252 SECONDARY_EXEC_ENABLE_VPID;
Sheng Yang2384d2b2008-01-17 15:14:33 +08001253}
1254
Gui Jianfeng31299942010-03-15 17:29:09 +08001255static inline bool cpu_has_vmx_rdtscp(void)
Sheng Yang4e47c7a2009-12-18 16:48:47 +08001256{
1257 return vmcs_config.cpu_based_2nd_exec_ctrl &
1258 SECONDARY_EXEC_RDTSCP;
1259}
1260
Mao, Junjiead756a12012-07-02 01:18:48 +00001261static inline bool cpu_has_vmx_invpcid(void)
1262{
1263 return vmcs_config.cpu_based_2nd_exec_ctrl &
1264 SECONDARY_EXEC_ENABLE_INVPCID;
1265}
1266
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01001267static inline bool cpu_has_virtual_nmis(void)
1268{
1269 return vmcs_config.pin_based_exec_ctrl & PIN_BASED_VIRTUAL_NMIS;
1270}
1271
Sheng Yangf5f48ee2010-06-30 12:25:15 +08001272static inline bool cpu_has_vmx_wbinvd_exit(void)
1273{
1274 return vmcs_config.cpu_based_2nd_exec_ctrl &
1275 SECONDARY_EXEC_WBINVD_EXITING;
1276}
1277
Abel Gordonabc4fc52013-04-18 14:35:25 +03001278static inline bool cpu_has_vmx_shadow_vmcs(void)
1279{
1280 u64 vmx_msr;
1281 rdmsrl(MSR_IA32_VMX_MISC, vmx_msr);
1282 /* check if the cpu supports writing r/o exit information fields */
1283 if (!(vmx_msr & MSR_IA32_VMX_MISC_VMWRITE_SHADOW_RO_FIELDS))
1284 return false;
1285
1286 return vmcs_config.cpu_based_2nd_exec_ctrl &
1287 SECONDARY_EXEC_SHADOW_VMCS;
1288}
1289
Kai Huang843e4332015-01-28 10:54:28 +08001290static inline bool cpu_has_vmx_pml(void)
1291{
1292 return vmcs_config.cpu_based_2nd_exec_ctrl & SECONDARY_EXEC_ENABLE_PML;
1293}
1294
Haozhong Zhang64903d62015-10-20 15:39:09 +08001295static inline bool cpu_has_vmx_tsc_scaling(void)
1296{
1297 return vmcs_config.cpu_based_2nd_exec_ctrl &
1298 SECONDARY_EXEC_TSC_SCALING;
1299}
1300
Bandan Das2a499e42017-08-03 15:54:41 -04001301static inline bool cpu_has_vmx_vmfunc(void)
1302{
1303 return vmcs_config.cpu_based_2nd_exec_ctrl &
1304 SECONDARY_EXEC_ENABLE_VMFUNC;
1305}
1306
Sheng Yang04547152009-04-01 15:52:31 +08001307static inline bool report_flexpriority(void)
1308{
1309 return flexpriority_enabled;
1310}
1311
Jim Mattsonc7c2c7092017-05-05 11:28:09 -07001312static inline unsigned nested_cpu_vmx_misc_cr3_count(struct kvm_vcpu *vcpu)
1313{
1314 return vmx_misc_cr3_count(to_vmx(vcpu)->nested.nested_vmx_misc_low);
1315}
1316
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03001317static inline bool nested_cpu_has(struct vmcs12 *vmcs12, u32 bit)
1318{
1319 return vmcs12->cpu_based_vm_exec_control & bit;
1320}
1321
1322static inline bool nested_cpu_has2(struct vmcs12 *vmcs12, u32 bit)
1323{
1324 return (vmcs12->cpu_based_vm_exec_control &
1325 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) &&
1326 (vmcs12->secondary_vm_exec_control & bit);
1327}
1328
Jan Kiszkaf41245002014-03-07 20:03:13 +01001329static inline bool nested_cpu_has_preemption_timer(struct vmcs12 *vmcs12)
1330{
1331 return vmcs12->pin_based_vm_exec_control &
1332 PIN_BASED_VMX_PREEMPTION_TIMER;
1333}
1334
Nadav Har'El155a97a2013-08-05 11:07:16 +03001335static inline int nested_cpu_has_ept(struct vmcs12 *vmcs12)
1336{
1337 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_EPT);
1338}
1339
Wanpeng Li81dc01f2014-12-04 19:11:07 +08001340static inline bool nested_cpu_has_xsaves(struct vmcs12 *vmcs12)
1341{
Paolo Bonzini3db13482017-08-24 14:48:03 +02001342 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_XSAVES);
Wanpeng Li81dc01f2014-12-04 19:11:07 +08001343}
1344
Bandan Dasc5f983f2017-05-05 15:25:14 -04001345static inline bool nested_cpu_has_pml(struct vmcs12 *vmcs12)
1346{
1347 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_PML);
1348}
1349
Wincy Vanf2b93282015-02-03 23:56:03 +08001350static inline bool nested_cpu_has_virt_x2apic_mode(struct vmcs12 *vmcs12)
1351{
1352 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE);
1353}
1354
Wanpeng Li5c614b32015-10-13 09:18:36 -07001355static inline bool nested_cpu_has_vpid(struct vmcs12 *vmcs12)
1356{
1357 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_VPID);
1358}
1359
Wincy Van82f0dd42015-02-03 23:57:18 +08001360static inline bool nested_cpu_has_apic_reg_virt(struct vmcs12 *vmcs12)
1361{
1362 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_APIC_REGISTER_VIRT);
1363}
1364
Wincy Van608406e2015-02-03 23:57:51 +08001365static inline bool nested_cpu_has_vid(struct vmcs12 *vmcs12)
1366{
1367 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
1368}
1369
Wincy Van705699a2015-02-03 23:58:17 +08001370static inline bool nested_cpu_has_posted_intr(struct vmcs12 *vmcs12)
1371{
1372 return vmcs12->pin_based_vm_exec_control & PIN_BASED_POSTED_INTR;
1373}
1374
Bandan Das27c42a12017-08-03 15:54:42 -04001375static inline bool nested_cpu_has_vmfunc(struct vmcs12 *vmcs12)
1376{
1377 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_VMFUNC);
1378}
1379
Bandan Das41ab9372017-08-03 15:54:43 -04001380static inline bool nested_cpu_has_eptp_switching(struct vmcs12 *vmcs12)
1381{
1382 return nested_cpu_has_vmfunc(vmcs12) &&
1383 (vmcs12->vm_function_control &
1384 VMX_VMFUNC_EPTP_SWITCHING);
1385}
1386
Jim Mattsonef85b672016-12-12 11:01:37 -08001387static inline bool is_nmi(u32 intr_info)
Nadav Har'El644d7112011-05-25 23:12:35 +03001388{
1389 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK))
Jim Mattsonef85b672016-12-12 11:01:37 -08001390 == (INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK);
Nadav Har'El644d7112011-05-25 23:12:35 +03001391}
1392
Jan Kiszka533558b2014-01-04 18:47:20 +01001393static void nested_vmx_vmexit(struct kvm_vcpu *vcpu, u32 exit_reason,
1394 u32 exit_intr_info,
1395 unsigned long exit_qualification);
Nadav Har'El7c177932011-05-25 23:12:04 +03001396static void nested_vmx_entry_failure(struct kvm_vcpu *vcpu,
1397 struct vmcs12 *vmcs12,
1398 u32 reason, unsigned long qualification);
1399
Rusty Russell8b9cf982007-07-30 16:31:43 +10001400static int __find_msr_index(struct vcpu_vmx *vmx, u32 msr)
Avi Kivity7725f0b2006-12-13 00:34:01 -08001401{
1402 int i;
1403
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001404 for (i = 0; i < vmx->nmsrs; ++i)
Avi Kivity26bb0982009-09-07 11:14:12 +03001405 if (vmx_msr_index[vmx->guest_msrs[i].index] == msr)
Eddie Donga75beee2007-05-17 18:55:15 +03001406 return i;
1407 return -1;
1408}
1409
Sheng Yang2384d2b2008-01-17 15:14:33 +08001410static inline void __invvpid(int ext, u16 vpid, gva_t gva)
1411{
1412 struct {
1413 u64 vpid : 16;
1414 u64 rsvd : 48;
1415 u64 gva;
1416 } operand = { vpid, 0, gva };
1417
Avi Kivity4ecac3f2008-05-13 13:23:38 +03001418 asm volatile (__ex(ASM_VMX_INVVPID)
Sheng Yang2384d2b2008-01-17 15:14:33 +08001419 /* CF==1 or ZF==1 --> rc = -1 */
1420 "; ja 1f ; ud2 ; 1:"
1421 : : "a"(&operand), "c"(ext) : "cc", "memory");
1422}
1423
Sheng Yang14394422008-04-28 12:24:45 +08001424static inline void __invept(int ext, u64 eptp, gpa_t gpa)
1425{
1426 struct {
1427 u64 eptp, gpa;
1428 } operand = {eptp, gpa};
1429
Avi Kivity4ecac3f2008-05-13 13:23:38 +03001430 asm volatile (__ex(ASM_VMX_INVEPT)
Sheng Yang14394422008-04-28 12:24:45 +08001431 /* CF==1 or ZF==1 --> rc = -1 */
1432 "; ja 1f ; ud2 ; 1:\n"
1433 : : "a" (&operand), "c" (ext) : "cc", "memory");
1434}
1435
Avi Kivity26bb0982009-09-07 11:14:12 +03001436static struct shared_msr_entry *find_msr_entry(struct vcpu_vmx *vmx, u32 msr)
Eddie Donga75beee2007-05-17 18:55:15 +03001437{
1438 int i;
1439
Rusty Russell8b9cf982007-07-30 16:31:43 +10001440 i = __find_msr_index(vmx, msr);
Eddie Donga75beee2007-05-17 18:55:15 +03001441 if (i >= 0)
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001442 return &vmx->guest_msrs[i];
Al Viro8b6d44c2007-02-09 16:38:40 +00001443 return NULL;
Avi Kivity7725f0b2006-12-13 00:34:01 -08001444}
1445
Avi Kivity6aa8b732006-12-10 02:21:36 -08001446static void vmcs_clear(struct vmcs *vmcs)
1447{
1448 u64 phys_addr = __pa(vmcs);
1449 u8 error;
1450
Avi Kivity4ecac3f2008-05-13 13:23:38 +03001451 asm volatile (__ex(ASM_VMX_VMCLEAR_RAX) "; setna %0"
Avi Kivity16d8f722010-12-21 16:51:50 +02001452 : "=qm"(error) : "a"(&phys_addr), "m"(phys_addr)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001453 : "cc", "memory");
1454 if (error)
1455 printk(KERN_ERR "kvm: vmclear fail: %p/%llx\n",
1456 vmcs, phys_addr);
1457}
1458
Nadav Har'Eld462b812011-05-24 15:26:10 +03001459static inline void loaded_vmcs_init(struct loaded_vmcs *loaded_vmcs)
1460{
1461 vmcs_clear(loaded_vmcs->vmcs);
Jim Mattson355f4fb2016-10-28 08:29:39 -07001462 if (loaded_vmcs->shadow_vmcs && loaded_vmcs->launched)
1463 vmcs_clear(loaded_vmcs->shadow_vmcs);
Nadav Har'Eld462b812011-05-24 15:26:10 +03001464 loaded_vmcs->cpu = -1;
1465 loaded_vmcs->launched = 0;
1466}
1467
Dongxiao Xu7725b892010-05-11 18:29:38 +08001468static void vmcs_load(struct vmcs *vmcs)
1469{
1470 u64 phys_addr = __pa(vmcs);
1471 u8 error;
1472
1473 asm volatile (__ex(ASM_VMX_VMPTRLD_RAX) "; setna %0"
Avi Kivity16d8f722010-12-21 16:51:50 +02001474 : "=qm"(error) : "a"(&phys_addr), "m"(phys_addr)
Dongxiao Xu7725b892010-05-11 18:29:38 +08001475 : "cc", "memory");
1476 if (error)
Nadav Har'El2844d842011-05-25 23:16:40 +03001477 printk(KERN_ERR "kvm: vmptrld %p/%llx failed\n",
Dongxiao Xu7725b892010-05-11 18:29:38 +08001478 vmcs, phys_addr);
1479}
1480
Dave Young2965faa2015-09-09 15:38:55 -07001481#ifdef CONFIG_KEXEC_CORE
Zhang Yanfei8f536b72012-12-06 23:43:34 +08001482/*
1483 * This bitmap is used to indicate whether the vmclear
1484 * operation is enabled on all cpus. All disabled by
1485 * default.
1486 */
1487static cpumask_t crash_vmclear_enabled_bitmap = CPU_MASK_NONE;
1488
1489static inline void crash_enable_local_vmclear(int cpu)
1490{
1491 cpumask_set_cpu(cpu, &crash_vmclear_enabled_bitmap);
1492}
1493
1494static inline void crash_disable_local_vmclear(int cpu)
1495{
1496 cpumask_clear_cpu(cpu, &crash_vmclear_enabled_bitmap);
1497}
1498
1499static inline int crash_local_vmclear_enabled(int cpu)
1500{
1501 return cpumask_test_cpu(cpu, &crash_vmclear_enabled_bitmap);
1502}
1503
1504static void crash_vmclear_local_loaded_vmcss(void)
1505{
1506 int cpu = raw_smp_processor_id();
1507 struct loaded_vmcs *v;
1508
1509 if (!crash_local_vmclear_enabled(cpu))
1510 return;
1511
1512 list_for_each_entry(v, &per_cpu(loaded_vmcss_on_cpu, cpu),
1513 loaded_vmcss_on_cpu_link)
1514 vmcs_clear(v->vmcs);
1515}
1516#else
1517static inline void crash_enable_local_vmclear(int cpu) { }
1518static inline void crash_disable_local_vmclear(int cpu) { }
Dave Young2965faa2015-09-09 15:38:55 -07001519#endif /* CONFIG_KEXEC_CORE */
Zhang Yanfei8f536b72012-12-06 23:43:34 +08001520
Nadav Har'Eld462b812011-05-24 15:26:10 +03001521static void __loaded_vmcs_clear(void *arg)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001522{
Nadav Har'Eld462b812011-05-24 15:26:10 +03001523 struct loaded_vmcs *loaded_vmcs = arg;
Ingo Molnard3b2c332007-01-05 16:36:23 -08001524 int cpu = raw_smp_processor_id();
Avi Kivity6aa8b732006-12-10 02:21:36 -08001525
Nadav Har'Eld462b812011-05-24 15:26:10 +03001526 if (loaded_vmcs->cpu != cpu)
1527 return; /* vcpu migration can race with cpu offline */
1528 if (per_cpu(current_vmcs, cpu) == loaded_vmcs->vmcs)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001529 per_cpu(current_vmcs, cpu) = NULL;
Zhang Yanfei8f536b72012-12-06 23:43:34 +08001530 crash_disable_local_vmclear(cpu);
Nadav Har'Eld462b812011-05-24 15:26:10 +03001531 list_del(&loaded_vmcs->loaded_vmcss_on_cpu_link);
Xiao Guangrong5a560f82012-11-28 20:54:14 +08001532
1533 /*
1534 * we should ensure updating loaded_vmcs->loaded_vmcss_on_cpu_link
1535 * is before setting loaded_vmcs->vcpu to -1 which is done in
1536 * loaded_vmcs_init. Otherwise, other cpu can see vcpu = -1 fist
1537 * then adds the vmcs into percpu list before it is deleted.
1538 */
1539 smp_wmb();
1540
Nadav Har'Eld462b812011-05-24 15:26:10 +03001541 loaded_vmcs_init(loaded_vmcs);
Zhang Yanfei8f536b72012-12-06 23:43:34 +08001542 crash_enable_local_vmclear(cpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001543}
1544
Nadav Har'Eld462b812011-05-24 15:26:10 +03001545static void loaded_vmcs_clear(struct loaded_vmcs *loaded_vmcs)
Avi Kivity8d0be2b2007-02-12 00:54:46 -08001546{
Xiao Guangronge6c7d322012-11-28 20:53:15 +08001547 int cpu = loaded_vmcs->cpu;
1548
1549 if (cpu != -1)
1550 smp_call_function_single(cpu,
1551 __loaded_vmcs_clear, loaded_vmcs, 1);
Avi Kivity8d0be2b2007-02-12 00:54:46 -08001552}
1553
Wanpeng Lidd5f5342015-09-23 18:26:57 +08001554static inline void vpid_sync_vcpu_single(int vpid)
Sheng Yang2384d2b2008-01-17 15:14:33 +08001555{
Wanpeng Lidd5f5342015-09-23 18:26:57 +08001556 if (vpid == 0)
Sheng Yang2384d2b2008-01-17 15:14:33 +08001557 return;
1558
Gui Jianfeng518c8ae2010-06-04 08:51:39 +08001559 if (cpu_has_vmx_invvpid_single())
Wanpeng Lidd5f5342015-09-23 18:26:57 +08001560 __invvpid(VMX_VPID_EXTENT_SINGLE_CONTEXT, vpid, 0);
Sheng Yang2384d2b2008-01-17 15:14:33 +08001561}
1562
Gui Jianfengb9d762f2010-06-07 10:32:29 +08001563static inline void vpid_sync_vcpu_global(void)
1564{
1565 if (cpu_has_vmx_invvpid_global())
1566 __invvpid(VMX_VPID_EXTENT_ALL_CONTEXT, 0, 0);
1567}
1568
Wanpeng Lidd5f5342015-09-23 18:26:57 +08001569static inline void vpid_sync_context(int vpid)
Gui Jianfengb9d762f2010-06-07 10:32:29 +08001570{
1571 if (cpu_has_vmx_invvpid_single())
Wanpeng Lidd5f5342015-09-23 18:26:57 +08001572 vpid_sync_vcpu_single(vpid);
Gui Jianfengb9d762f2010-06-07 10:32:29 +08001573 else
1574 vpid_sync_vcpu_global();
1575}
1576
Sheng Yang14394422008-04-28 12:24:45 +08001577static inline void ept_sync_global(void)
1578{
David Hildenbrandf5f51582017-08-24 20:51:30 +02001579 __invept(VMX_EPT_EXTENT_GLOBAL, 0, 0);
Sheng Yang14394422008-04-28 12:24:45 +08001580}
1581
1582static inline void ept_sync_context(u64 eptp)
1583{
David Hildenbrand0e1252d2017-08-24 20:51:28 +02001584 if (cpu_has_vmx_invept_context())
1585 __invept(VMX_EPT_EXTENT_CONTEXT, eptp, 0);
1586 else
1587 ept_sync_global();
Sheng Yang14394422008-04-28 12:24:45 +08001588}
1589
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001590static __always_inline void vmcs_check16(unsigned long field)
1591{
1592 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2000,
1593 "16-bit accessor invalid for 64-bit field");
1594 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2001,
1595 "16-bit accessor invalid for 64-bit high field");
1596 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x4000,
1597 "16-bit accessor invalid for 32-bit high field");
1598 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x6000,
1599 "16-bit accessor invalid for natural width field");
1600}
1601
1602static __always_inline void vmcs_check32(unsigned long field)
1603{
1604 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0,
1605 "32-bit accessor invalid for 16-bit field");
1606 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x6000,
1607 "32-bit accessor invalid for natural width field");
1608}
1609
1610static __always_inline void vmcs_check64(unsigned long field)
1611{
1612 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0,
1613 "64-bit accessor invalid for 16-bit field");
1614 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2001,
1615 "64-bit accessor invalid for 64-bit high field");
1616 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x4000,
1617 "64-bit accessor invalid for 32-bit field");
1618 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x6000,
1619 "64-bit accessor invalid for natural width field");
1620}
1621
1622static __always_inline void vmcs_checkl(unsigned long field)
1623{
1624 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0,
1625 "Natural width accessor invalid for 16-bit field");
1626 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2000,
1627 "Natural width accessor invalid for 64-bit field");
1628 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2001,
1629 "Natural width accessor invalid for 64-bit high field");
1630 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x4000,
1631 "Natural width accessor invalid for 32-bit field");
1632}
1633
1634static __always_inline unsigned long __vmcs_readl(unsigned long field)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001635{
Avi Kivity5e520e62011-05-15 10:13:12 -04001636 unsigned long value;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001637
Avi Kivity5e520e62011-05-15 10:13:12 -04001638 asm volatile (__ex_clear(ASM_VMX_VMREAD_RDX_RAX, "%0")
1639 : "=a"(value) : "d"(field) : "cc");
Avi Kivity6aa8b732006-12-10 02:21:36 -08001640 return value;
1641}
1642
Avi Kivity96304212011-05-15 10:13:13 -04001643static __always_inline u16 vmcs_read16(unsigned long field)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001644{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001645 vmcs_check16(field);
1646 return __vmcs_readl(field);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001647}
1648
Avi Kivity96304212011-05-15 10:13:13 -04001649static __always_inline u32 vmcs_read32(unsigned long field)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001650{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001651 vmcs_check32(field);
1652 return __vmcs_readl(field);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001653}
1654
Avi Kivity96304212011-05-15 10:13:13 -04001655static __always_inline u64 vmcs_read64(unsigned long field)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001656{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001657 vmcs_check64(field);
Avi Kivity05b3e0c2006-12-13 00:33:45 -08001658#ifdef CONFIG_X86_64
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001659 return __vmcs_readl(field);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001660#else
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001661 return __vmcs_readl(field) | ((u64)__vmcs_readl(field+1) << 32);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001662#endif
1663}
1664
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001665static __always_inline unsigned long vmcs_readl(unsigned long field)
1666{
1667 vmcs_checkl(field);
1668 return __vmcs_readl(field);
1669}
1670
Avi Kivitye52de1b2007-01-05 16:36:56 -08001671static noinline void vmwrite_error(unsigned long field, unsigned long value)
1672{
1673 printk(KERN_ERR "vmwrite error: reg %lx value %lx (err %d)\n",
1674 field, value, vmcs_read32(VM_INSTRUCTION_ERROR));
1675 dump_stack();
1676}
1677
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001678static __always_inline void __vmcs_writel(unsigned long field, unsigned long value)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001679{
1680 u8 error;
1681
Avi Kivity4ecac3f2008-05-13 13:23:38 +03001682 asm volatile (__ex(ASM_VMX_VMWRITE_RAX_RDX) "; setna %0"
Mike Dayd77c26f2007-10-08 09:02:08 -04001683 : "=q"(error) : "a"(value), "d"(field) : "cc");
Avi Kivitye52de1b2007-01-05 16:36:56 -08001684 if (unlikely(error))
1685 vmwrite_error(field, value);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001686}
1687
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001688static __always_inline void vmcs_write16(unsigned long field, u16 value)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001689{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001690 vmcs_check16(field);
1691 __vmcs_writel(field, value);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001692}
1693
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001694static __always_inline void vmcs_write32(unsigned long field, u32 value)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001695{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001696 vmcs_check32(field);
1697 __vmcs_writel(field, value);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001698}
1699
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001700static __always_inline void vmcs_write64(unsigned long field, u64 value)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001701{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001702 vmcs_check64(field);
1703 __vmcs_writel(field, value);
Avi Kivity7682f2d2008-05-12 19:25:43 +03001704#ifndef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08001705 asm volatile ("");
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001706 __vmcs_writel(field+1, value >> 32);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001707#endif
1708}
1709
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001710static __always_inline void vmcs_writel(unsigned long field, unsigned long value)
Anthony Liguori2ab455c2007-04-27 09:29:49 +03001711{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001712 vmcs_checkl(field);
1713 __vmcs_writel(field, value);
Anthony Liguori2ab455c2007-04-27 09:29:49 +03001714}
1715
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001716static __always_inline void vmcs_clear_bits(unsigned long field, u32 mask)
Anthony Liguori2ab455c2007-04-27 09:29:49 +03001717{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001718 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x2000,
1719 "vmcs_clear_bits does not support 64-bit fields");
1720 __vmcs_writel(field, __vmcs_readl(field) & ~mask);
1721}
1722
1723static __always_inline void vmcs_set_bits(unsigned long field, u32 mask)
1724{
1725 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x2000,
1726 "vmcs_set_bits does not support 64-bit fields");
1727 __vmcs_writel(field, __vmcs_readl(field) | mask);
Anthony Liguori2ab455c2007-04-27 09:29:49 +03001728}
1729
Paolo Bonzini8391ce42016-07-07 14:58:33 +02001730static inline void vm_entry_controls_reset_shadow(struct vcpu_vmx *vmx)
1731{
1732 vmx->vm_entry_controls_shadow = vmcs_read32(VM_ENTRY_CONTROLS);
1733}
1734
Gleb Natapov2961e8762013-11-25 15:37:13 +02001735static inline void vm_entry_controls_init(struct vcpu_vmx *vmx, u32 val)
1736{
1737 vmcs_write32(VM_ENTRY_CONTROLS, val);
1738 vmx->vm_entry_controls_shadow = val;
1739}
1740
1741static inline void vm_entry_controls_set(struct vcpu_vmx *vmx, u32 val)
1742{
1743 if (vmx->vm_entry_controls_shadow != val)
1744 vm_entry_controls_init(vmx, val);
1745}
1746
1747static inline u32 vm_entry_controls_get(struct vcpu_vmx *vmx)
1748{
1749 return vmx->vm_entry_controls_shadow;
1750}
1751
1752
1753static inline void vm_entry_controls_setbit(struct vcpu_vmx *vmx, u32 val)
1754{
1755 vm_entry_controls_set(vmx, vm_entry_controls_get(vmx) | val);
1756}
1757
1758static inline void vm_entry_controls_clearbit(struct vcpu_vmx *vmx, u32 val)
1759{
1760 vm_entry_controls_set(vmx, vm_entry_controls_get(vmx) & ~val);
1761}
1762
Paolo Bonzini8391ce42016-07-07 14:58:33 +02001763static inline void vm_exit_controls_reset_shadow(struct vcpu_vmx *vmx)
1764{
1765 vmx->vm_exit_controls_shadow = vmcs_read32(VM_EXIT_CONTROLS);
1766}
1767
Gleb Natapov2961e8762013-11-25 15:37:13 +02001768static inline void vm_exit_controls_init(struct vcpu_vmx *vmx, u32 val)
1769{
1770 vmcs_write32(VM_EXIT_CONTROLS, val);
1771 vmx->vm_exit_controls_shadow = val;
1772}
1773
1774static inline void vm_exit_controls_set(struct vcpu_vmx *vmx, u32 val)
1775{
1776 if (vmx->vm_exit_controls_shadow != val)
1777 vm_exit_controls_init(vmx, val);
1778}
1779
1780static inline u32 vm_exit_controls_get(struct vcpu_vmx *vmx)
1781{
1782 return vmx->vm_exit_controls_shadow;
1783}
1784
1785
1786static inline void vm_exit_controls_setbit(struct vcpu_vmx *vmx, u32 val)
1787{
1788 vm_exit_controls_set(vmx, vm_exit_controls_get(vmx) | val);
1789}
1790
1791static inline void vm_exit_controls_clearbit(struct vcpu_vmx *vmx, u32 val)
1792{
1793 vm_exit_controls_set(vmx, vm_exit_controls_get(vmx) & ~val);
1794}
1795
Avi Kivity2fb92db2011-04-27 19:42:18 +03001796static void vmx_segment_cache_clear(struct vcpu_vmx *vmx)
1797{
1798 vmx->segment_cache.bitmask = 0;
1799}
1800
1801static bool vmx_segment_cache_test_set(struct vcpu_vmx *vmx, unsigned seg,
1802 unsigned field)
1803{
1804 bool ret;
1805 u32 mask = 1 << (seg * SEG_FIELD_NR + field);
1806
1807 if (!(vmx->vcpu.arch.regs_avail & (1 << VCPU_EXREG_SEGMENTS))) {
1808 vmx->vcpu.arch.regs_avail |= (1 << VCPU_EXREG_SEGMENTS);
1809 vmx->segment_cache.bitmask = 0;
1810 }
1811 ret = vmx->segment_cache.bitmask & mask;
1812 vmx->segment_cache.bitmask |= mask;
1813 return ret;
1814}
1815
1816static u16 vmx_read_guest_seg_selector(struct vcpu_vmx *vmx, unsigned seg)
1817{
1818 u16 *p = &vmx->segment_cache.seg[seg].selector;
1819
1820 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_SEL))
1821 *p = vmcs_read16(kvm_vmx_segment_fields[seg].selector);
1822 return *p;
1823}
1824
1825static ulong vmx_read_guest_seg_base(struct vcpu_vmx *vmx, unsigned seg)
1826{
1827 ulong *p = &vmx->segment_cache.seg[seg].base;
1828
1829 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_BASE))
1830 *p = vmcs_readl(kvm_vmx_segment_fields[seg].base);
1831 return *p;
1832}
1833
1834static u32 vmx_read_guest_seg_limit(struct vcpu_vmx *vmx, unsigned seg)
1835{
1836 u32 *p = &vmx->segment_cache.seg[seg].limit;
1837
1838 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_LIMIT))
1839 *p = vmcs_read32(kvm_vmx_segment_fields[seg].limit);
1840 return *p;
1841}
1842
1843static u32 vmx_read_guest_seg_ar(struct vcpu_vmx *vmx, unsigned seg)
1844{
1845 u32 *p = &vmx->segment_cache.seg[seg].ar;
1846
1847 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_AR))
1848 *p = vmcs_read32(kvm_vmx_segment_fields[seg].ar_bytes);
1849 return *p;
1850}
1851
Avi Kivityabd3f2d2007-05-02 17:57:40 +03001852static void update_exception_bitmap(struct kvm_vcpu *vcpu)
1853{
1854 u32 eb;
1855
Liran Alonac9b3052017-11-06 16:15:10 +02001856 eb = (1u << PF_VECTOR) | (1u << MC_VECTOR) |
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -08001857 (1u << DB_VECTOR) | (1u << AC_VECTOR);
Jan Kiszkafd7373c2010-01-20 18:20:20 +01001858 if ((vcpu->guest_debug &
1859 (KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_SW_BP)) ==
1860 (KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_SW_BP))
1861 eb |= 1u << BP_VECTOR;
Avi Kivity7ffd92c2009-06-09 14:10:45 +03001862 if (to_vmx(vcpu)->rmode.vm86_active)
Avi Kivityabd3f2d2007-05-02 17:57:40 +03001863 eb = ~0;
Avi Kivity089d0342009-03-23 18:26:32 +02001864 if (enable_ept)
Sheng Yang14394422008-04-28 12:24:45 +08001865 eb &= ~(1u << PF_VECTOR); /* bypass_guest_pf = 0 */
Nadav Har'El36cf24e2011-05-25 23:15:08 +03001866
1867 /* When we are running a nested L2 guest and L1 specified for it a
1868 * certain exception bitmap, we must trap the same exceptions and pass
1869 * them to L1. When running L2, we will only handle the exceptions
1870 * specified above if L1 did not want them.
1871 */
1872 if (is_guest_mode(vcpu))
1873 eb |= get_vmcs12(vcpu)->exception_bitmap;
Liran Alonac9b3052017-11-06 16:15:10 +02001874 else
1875 eb |= 1u << UD_VECTOR;
Nadav Har'El36cf24e2011-05-25 23:15:08 +03001876
Avi Kivityabd3f2d2007-05-02 17:57:40 +03001877 vmcs_write32(EXCEPTION_BITMAP, eb);
1878}
1879
Gleb Natapov2961e8762013-11-25 15:37:13 +02001880static void clear_atomic_switch_msr_special(struct vcpu_vmx *vmx,
1881 unsigned long entry, unsigned long exit)
Gleb Natapov8bf00a52011-10-05 14:01:22 +02001882{
Gleb Natapov2961e8762013-11-25 15:37:13 +02001883 vm_entry_controls_clearbit(vmx, entry);
1884 vm_exit_controls_clearbit(vmx, exit);
Gleb Natapov8bf00a52011-10-05 14:01:22 +02001885}
1886
Avi Kivity61d2ef22010-04-28 16:40:38 +03001887static void clear_atomic_switch_msr(struct vcpu_vmx *vmx, unsigned msr)
1888{
1889 unsigned i;
1890 struct msr_autoload *m = &vmx->msr_autoload;
1891
Gleb Natapov8bf00a52011-10-05 14:01:22 +02001892 switch (msr) {
1893 case MSR_EFER:
1894 if (cpu_has_load_ia32_efer) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02001895 clear_atomic_switch_msr_special(vmx,
1896 VM_ENTRY_LOAD_IA32_EFER,
Gleb Natapov8bf00a52011-10-05 14:01:22 +02001897 VM_EXIT_LOAD_IA32_EFER);
1898 return;
1899 }
1900 break;
1901 case MSR_CORE_PERF_GLOBAL_CTRL:
1902 if (cpu_has_load_perf_global_ctrl) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02001903 clear_atomic_switch_msr_special(vmx,
Gleb Natapov8bf00a52011-10-05 14:01:22 +02001904 VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL,
1905 VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL);
1906 return;
1907 }
1908 break;
Avi Kivity110312c2010-12-21 12:54:20 +02001909 }
1910
Avi Kivity61d2ef22010-04-28 16:40:38 +03001911 for (i = 0; i < m->nr; ++i)
1912 if (m->guest[i].index == msr)
1913 break;
1914
1915 if (i == m->nr)
1916 return;
1917 --m->nr;
1918 m->guest[i] = m->guest[m->nr];
1919 m->host[i] = m->host[m->nr];
1920 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, m->nr);
1921 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, m->nr);
1922}
1923
Gleb Natapov2961e8762013-11-25 15:37:13 +02001924static void add_atomic_switch_msr_special(struct vcpu_vmx *vmx,
1925 unsigned long entry, unsigned long exit,
1926 unsigned long guest_val_vmcs, unsigned long host_val_vmcs,
1927 u64 guest_val, u64 host_val)
Gleb Natapov8bf00a52011-10-05 14:01:22 +02001928{
1929 vmcs_write64(guest_val_vmcs, guest_val);
1930 vmcs_write64(host_val_vmcs, host_val);
Gleb Natapov2961e8762013-11-25 15:37:13 +02001931 vm_entry_controls_setbit(vmx, entry);
1932 vm_exit_controls_setbit(vmx, exit);
Gleb Natapov8bf00a52011-10-05 14:01:22 +02001933}
1934
Avi Kivity61d2ef22010-04-28 16:40:38 +03001935static void add_atomic_switch_msr(struct vcpu_vmx *vmx, unsigned msr,
1936 u64 guest_val, u64 host_val)
1937{
1938 unsigned i;
1939 struct msr_autoload *m = &vmx->msr_autoload;
1940
Gleb Natapov8bf00a52011-10-05 14:01:22 +02001941 switch (msr) {
1942 case MSR_EFER:
1943 if (cpu_has_load_ia32_efer) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02001944 add_atomic_switch_msr_special(vmx,
1945 VM_ENTRY_LOAD_IA32_EFER,
Gleb Natapov8bf00a52011-10-05 14:01:22 +02001946 VM_EXIT_LOAD_IA32_EFER,
1947 GUEST_IA32_EFER,
1948 HOST_IA32_EFER,
1949 guest_val, host_val);
1950 return;
1951 }
1952 break;
1953 case MSR_CORE_PERF_GLOBAL_CTRL:
1954 if (cpu_has_load_perf_global_ctrl) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02001955 add_atomic_switch_msr_special(vmx,
Gleb Natapov8bf00a52011-10-05 14:01:22 +02001956 VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL,
1957 VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL,
1958 GUEST_IA32_PERF_GLOBAL_CTRL,
1959 HOST_IA32_PERF_GLOBAL_CTRL,
1960 guest_val, host_val);
1961 return;
1962 }
1963 break;
Radim Krčmář7099e2e2016-03-04 15:08:42 +01001964 case MSR_IA32_PEBS_ENABLE:
1965 /* PEBS needs a quiescent period after being disabled (to write
1966 * a record). Disabling PEBS through VMX MSR swapping doesn't
1967 * provide that period, so a CPU could write host's record into
1968 * guest's memory.
1969 */
1970 wrmsrl(MSR_IA32_PEBS_ENABLE, 0);
Avi Kivity110312c2010-12-21 12:54:20 +02001971 }
1972
Avi Kivity61d2ef22010-04-28 16:40:38 +03001973 for (i = 0; i < m->nr; ++i)
1974 if (m->guest[i].index == msr)
1975 break;
1976
Gleb Natapove7fc6f93b2011-10-05 14:01:24 +02001977 if (i == NR_AUTOLOAD_MSRS) {
Michael S. Tsirkin60266202013-10-31 00:34:56 +02001978 printk_once(KERN_WARNING "Not enough msr switch entries. "
Gleb Natapove7fc6f93b2011-10-05 14:01:24 +02001979 "Can't add msr %x\n", msr);
1980 return;
1981 } else if (i == m->nr) {
Avi Kivity61d2ef22010-04-28 16:40:38 +03001982 ++m->nr;
1983 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, m->nr);
1984 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, m->nr);
1985 }
1986
1987 m->guest[i].index = msr;
1988 m->guest[i].value = guest_val;
1989 m->host[i].index = msr;
1990 m->host[i].value = host_val;
1991}
1992
Avi Kivity92c0d902009-10-29 11:00:16 +02001993static bool update_transition_efer(struct vcpu_vmx *vmx, int efer_offset)
Eddie Dong2cc51562007-05-21 07:28:09 +03001994{
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01001995 u64 guest_efer = vmx->vcpu.arch.efer;
1996 u64 ignore_bits = 0;
Eddie Dong2cc51562007-05-21 07:28:09 +03001997
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01001998 if (!enable_ept) {
1999 /*
2000 * NX is needed to handle CR0.WP=1, CR4.SMEP=1. Testing
2001 * host CPUID is more efficient than testing guest CPUID
2002 * or CR4. Host SMEP is anyway a requirement for guest SMEP.
2003 */
2004 if (boot_cpu_has(X86_FEATURE_SMEP))
2005 guest_efer |= EFER_NX;
2006 else if (!(guest_efer & EFER_NX))
2007 ignore_bits |= EFER_NX;
2008 }
Roel Kluin3a34a882009-08-04 02:08:45 -07002009
Avi Kivity51c6cf62007-08-29 03:48:05 +03002010 /*
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002011 * LMA and LME handled by hardware; SCE meaningless outside long mode.
Avi Kivity51c6cf62007-08-29 03:48:05 +03002012 */
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002013 ignore_bits |= EFER_SCE;
Avi Kivity51c6cf62007-08-29 03:48:05 +03002014#ifdef CONFIG_X86_64
2015 ignore_bits |= EFER_LMA | EFER_LME;
2016 /* SCE is meaningful only in long mode on Intel */
2017 if (guest_efer & EFER_LMA)
2018 ignore_bits &= ~(u64)EFER_SCE;
2019#endif
Avi Kivity84ad33e2010-04-28 16:42:29 +03002020
2021 clear_atomic_switch_msr(vmx, MSR_EFER);
Andy Lutomirskif6577a5f2014-11-07 18:25:18 -08002022
2023 /*
2024 * On EPT, we can't emulate NX, so we must switch EFER atomically.
2025 * On CPUs that support "load IA32_EFER", always switch EFER
2026 * atomically, since it's faster than switching it manually.
2027 */
2028 if (cpu_has_load_ia32_efer ||
2029 (enable_ept && ((vmx->vcpu.arch.efer ^ host_efer) & EFER_NX))) {
Avi Kivity84ad33e2010-04-28 16:42:29 +03002030 if (!(guest_efer & EFER_LMA))
2031 guest_efer &= ~EFER_LME;
Andy Lutomirski54b98bf2014-11-10 11:19:15 -08002032 if (guest_efer != host_efer)
2033 add_atomic_switch_msr(vmx, MSR_EFER,
2034 guest_efer, host_efer);
Avi Kivity84ad33e2010-04-28 16:42:29 +03002035 return false;
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002036 } else {
2037 guest_efer &= ~ignore_bits;
2038 guest_efer |= host_efer & ignore_bits;
Avi Kivity84ad33e2010-04-28 16:42:29 +03002039
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002040 vmx->guest_msrs[efer_offset].data = guest_efer;
2041 vmx->guest_msrs[efer_offset].mask = ~ignore_bits;
2042
2043 return true;
2044 }
Avi Kivity51c6cf62007-08-29 03:48:05 +03002045}
2046
Andy Lutomirskie28baea2017-02-20 08:56:11 -08002047#ifdef CONFIG_X86_32
2048/*
2049 * On 32-bit kernels, VM exits still load the FS and GS bases from the
2050 * VMCS rather than the segment table. KVM uses this helper to figure
2051 * out the current bases to poke them into the VMCS before entry.
2052 */
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002053static unsigned long segment_base(u16 selector)
2054{
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08002055 struct desc_struct *table;
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002056 unsigned long v;
2057
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08002058 if (!(selector & ~SEGMENT_RPL_MASK))
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002059 return 0;
2060
Thomas Garnier45fc8752017-03-14 10:05:08 -07002061 table = get_current_gdt_ro();
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002062
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08002063 if ((selector & SEGMENT_TI_MASK) == SEGMENT_LDT) {
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002064 u16 ldt_selector = kvm_read_ldt();
2065
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08002066 if (!(ldt_selector & ~SEGMENT_RPL_MASK))
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002067 return 0;
2068
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08002069 table = (struct desc_struct *)segment_base(ldt_selector);
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002070 }
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08002071 v = get_desc_base(&table[selector >> 3]);
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002072 return v;
2073}
Andy Lutomirskie28baea2017-02-20 08:56:11 -08002074#endif
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002075
Avi Kivity04d2cc72007-09-10 18:10:54 +03002076static void vmx_save_host_state(struct kvm_vcpu *vcpu)
Avi Kivity33ed6322007-05-02 16:54:03 +03002077{
Avi Kivity04d2cc72007-09-10 18:10:54 +03002078 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity26bb0982009-09-07 11:14:12 +03002079 int i;
Avi Kivity04d2cc72007-09-10 18:10:54 +03002080
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002081 if (vmx->host_state.loaded)
Avi Kivity33ed6322007-05-02 16:54:03 +03002082 return;
2083
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002084 vmx->host_state.loaded = 1;
Avi Kivity33ed6322007-05-02 16:54:03 +03002085 /*
2086 * Set host fs and gs selectors. Unfortunately, 22.2.3 does not
2087 * allow segment selectors with cpl > 0 or ti == 1.
2088 */
Avi Kivityd6e88ae2008-07-10 16:53:33 +03002089 vmx->host_state.ldt_sel = kvm_read_ldt();
Laurent Vivier152d3f22007-08-23 16:33:11 +02002090 vmx->host_state.gs_ldt_reload_needed = vmx->host_state.ldt_sel;
Avi Kivity9581d442010-10-19 16:46:55 +02002091 savesegment(fs, vmx->host_state.fs_sel);
Laurent Vivier152d3f22007-08-23 16:33:11 +02002092 if (!(vmx->host_state.fs_sel & 7)) {
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002093 vmcs_write16(HOST_FS_SELECTOR, vmx->host_state.fs_sel);
Laurent Vivier152d3f22007-08-23 16:33:11 +02002094 vmx->host_state.fs_reload_needed = 0;
2095 } else {
Avi Kivity33ed6322007-05-02 16:54:03 +03002096 vmcs_write16(HOST_FS_SELECTOR, 0);
Laurent Vivier152d3f22007-08-23 16:33:11 +02002097 vmx->host_state.fs_reload_needed = 1;
Avi Kivity33ed6322007-05-02 16:54:03 +03002098 }
Avi Kivity9581d442010-10-19 16:46:55 +02002099 savesegment(gs, vmx->host_state.gs_sel);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002100 if (!(vmx->host_state.gs_sel & 7))
2101 vmcs_write16(HOST_GS_SELECTOR, vmx->host_state.gs_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +03002102 else {
2103 vmcs_write16(HOST_GS_SELECTOR, 0);
Laurent Vivier152d3f22007-08-23 16:33:11 +02002104 vmx->host_state.gs_ldt_reload_needed = 1;
Avi Kivity33ed6322007-05-02 16:54:03 +03002105 }
2106
2107#ifdef CONFIG_X86_64
Avi Kivityb2da15a2012-05-13 19:53:24 +03002108 savesegment(ds, vmx->host_state.ds_sel);
2109 savesegment(es, vmx->host_state.es_sel);
2110#endif
2111
2112#ifdef CONFIG_X86_64
Avi Kivity33ed6322007-05-02 16:54:03 +03002113 vmcs_writel(HOST_FS_BASE, read_msr(MSR_FS_BASE));
2114 vmcs_writel(HOST_GS_BASE, read_msr(MSR_GS_BASE));
2115#else
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002116 vmcs_writel(HOST_FS_BASE, segment_base(vmx->host_state.fs_sel));
2117 vmcs_writel(HOST_GS_BASE, segment_base(vmx->host_state.gs_sel));
Avi Kivity33ed6322007-05-02 16:54:03 +03002118#endif
Avi Kivity707c0872007-05-02 17:33:43 +03002119
2120#ifdef CONFIG_X86_64
Avi Kivityc8770e72010-11-11 12:37:26 +02002121 rdmsrl(MSR_KERNEL_GS_BASE, vmx->msr_host_kernel_gs_base);
2122 if (is_long_mode(&vmx->vcpu))
Avi Kivity44ea2b12009-09-06 15:55:37 +03002123 wrmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base);
Avi Kivity707c0872007-05-02 17:33:43 +03002124#endif
Liu, Jinsongda8999d2014-02-24 10:55:46 +00002125 if (boot_cpu_has(X86_FEATURE_MPX))
2126 rdmsrl(MSR_IA32_BNDCFGS, vmx->host_state.msr_host_bndcfgs);
Avi Kivity26bb0982009-09-07 11:14:12 +03002127 for (i = 0; i < vmx->save_nmsrs; ++i)
2128 kvm_set_shared_msr(vmx->guest_msrs[i].index,
Avi Kivityd5696722009-12-02 12:28:47 +02002129 vmx->guest_msrs[i].data,
2130 vmx->guest_msrs[i].mask);
Avi Kivity33ed6322007-05-02 16:54:03 +03002131}
2132
Avi Kivitya9b21b62008-06-24 11:48:49 +03002133static void __vmx_load_host_state(struct vcpu_vmx *vmx)
Avi Kivity33ed6322007-05-02 16:54:03 +03002134{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002135 if (!vmx->host_state.loaded)
Avi Kivity33ed6322007-05-02 16:54:03 +03002136 return;
2137
Avi Kivitye1beb1d2007-11-18 13:50:24 +02002138 ++vmx->vcpu.stat.host_state_reload;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002139 vmx->host_state.loaded = 0;
Avi Kivityc8770e72010-11-11 12:37:26 +02002140#ifdef CONFIG_X86_64
2141 if (is_long_mode(&vmx->vcpu))
2142 rdmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base);
2143#endif
Laurent Vivier152d3f22007-08-23 16:33:11 +02002144 if (vmx->host_state.gs_ldt_reload_needed) {
Avi Kivityd6e88ae2008-07-10 16:53:33 +03002145 kvm_load_ldt(vmx->host_state.ldt_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +03002146#ifdef CONFIG_X86_64
Avi Kivity9581d442010-10-19 16:46:55 +02002147 load_gs_index(vmx->host_state.gs_sel);
Avi Kivity9581d442010-10-19 16:46:55 +02002148#else
2149 loadsegment(gs, vmx->host_state.gs_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +03002150#endif
Avi Kivity33ed6322007-05-02 16:54:03 +03002151 }
Avi Kivity0a77fe42010-10-19 18:48:35 +02002152 if (vmx->host_state.fs_reload_needed)
2153 loadsegment(fs, vmx->host_state.fs_sel);
Avi Kivityb2da15a2012-05-13 19:53:24 +03002154#ifdef CONFIG_X86_64
2155 if (unlikely(vmx->host_state.ds_sel | vmx->host_state.es_sel)) {
2156 loadsegment(ds, vmx->host_state.ds_sel);
2157 loadsegment(es, vmx->host_state.es_sel);
2158 }
Avi Kivityb2da15a2012-05-13 19:53:24 +03002159#endif
Andy Lutomirskib7ffc442017-02-20 08:56:14 -08002160 invalidate_tss_limit();
Avi Kivity44ea2b12009-09-06 15:55:37 +03002161#ifdef CONFIG_X86_64
Avi Kivityc8770e72010-11-11 12:37:26 +02002162 wrmsrl(MSR_KERNEL_GS_BASE, vmx->msr_host_kernel_gs_base);
Avi Kivity44ea2b12009-09-06 15:55:37 +03002163#endif
Liu, Jinsongda8999d2014-02-24 10:55:46 +00002164 if (vmx->host_state.msr_host_bndcfgs)
2165 wrmsrl(MSR_IA32_BNDCFGS, vmx->host_state.msr_host_bndcfgs);
Thomas Garnier45fc8752017-03-14 10:05:08 -07002166 load_fixmap_gdt(raw_smp_processor_id());
Avi Kivity33ed6322007-05-02 16:54:03 +03002167}
2168
Avi Kivitya9b21b62008-06-24 11:48:49 +03002169static void vmx_load_host_state(struct vcpu_vmx *vmx)
2170{
2171 preempt_disable();
2172 __vmx_load_host_state(vmx);
2173 preempt_enable();
2174}
2175
Feng Wu28b835d2015-09-18 22:29:54 +08002176static void vmx_vcpu_pi_load(struct kvm_vcpu *vcpu, int cpu)
2177{
2178 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
2179 struct pi_desc old, new;
2180 unsigned int dest;
2181
Paolo Bonzini31afb2e2017-06-06 12:57:06 +02002182 /*
2183 * In case of hot-plug or hot-unplug, we may have to undo
2184 * vmx_vcpu_pi_put even if there is no assigned device. And we
2185 * always keep PI.NDST up to date for simplicity: it makes the
2186 * code easier, and CPU migration is not a fast path.
2187 */
2188 if (!pi_test_sn(pi_desc) && vcpu->cpu == cpu)
Feng Wu28b835d2015-09-18 22:29:54 +08002189 return;
2190
Paolo Bonzini31afb2e2017-06-06 12:57:06 +02002191 /*
2192 * First handle the simple case where no cmpxchg is necessary; just
2193 * allow posting non-urgent interrupts.
2194 *
2195 * If the 'nv' field is POSTED_INTR_WAKEUP_VECTOR, do not change
2196 * PI.NDST: pi_post_block will do it for us and the wakeup_handler
2197 * expects the VCPU to be on the blocked_vcpu_list that matches
2198 * PI.NDST.
2199 */
2200 if (pi_desc->nv == POSTED_INTR_WAKEUP_VECTOR ||
2201 vcpu->cpu == cpu) {
2202 pi_clear_sn(pi_desc);
2203 return;
2204 }
2205
2206 /* The full case. */
Feng Wu28b835d2015-09-18 22:29:54 +08002207 do {
2208 old.control = new.control = pi_desc->control;
2209
Paolo Bonzini31afb2e2017-06-06 12:57:06 +02002210 dest = cpu_physical_id(cpu);
Feng Wu28b835d2015-09-18 22:29:54 +08002211
Paolo Bonzini31afb2e2017-06-06 12:57:06 +02002212 if (x2apic_enabled())
2213 new.ndst = dest;
2214 else
2215 new.ndst = (dest << 8) & 0xFF00;
Feng Wu28b835d2015-09-18 22:29:54 +08002216
Feng Wu28b835d2015-09-18 22:29:54 +08002217 new.sn = 0;
Paolo Bonzinic0a16662017-09-28 17:58:41 +02002218 } while (cmpxchg64(&pi_desc->control, old.control,
2219 new.control) != old.control);
Feng Wu28b835d2015-09-18 22:29:54 +08002220}
Xiao Guangrong1be0e612016-03-22 16:51:18 +08002221
Peter Feinerc95ba922016-08-17 09:36:47 -07002222static void decache_tsc_multiplier(struct vcpu_vmx *vmx)
2223{
2224 vmx->current_tsc_ratio = vmx->vcpu.arch.tsc_scaling_ratio;
2225 vmcs_write64(TSC_MULTIPLIER, vmx->current_tsc_ratio);
2226}
2227
Avi Kivity6aa8b732006-12-10 02:21:36 -08002228/*
2229 * Switches to specified vcpu, until a matching vcpu_put(), but assumes
2230 * vcpu mutex is already taken.
2231 */
Avi Kivity15ad7142007-07-11 18:17:21 +03002232static void vmx_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002233{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002234 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jim Mattsonb80c76e2016-07-29 18:56:53 -07002235 bool already_loaded = vmx->loaded_vmcs->cpu == cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002236
Jim Mattsonb80c76e2016-07-29 18:56:53 -07002237 if (!already_loaded) {
David Hildenbrandfe0e80b2017-03-10 12:47:13 +01002238 loaded_vmcs_clear(vmx->loaded_vmcs);
Dongxiao Xu92fe13b2010-05-11 18:29:42 +08002239 local_irq_disable();
Zhang Yanfei8f536b72012-12-06 23:43:34 +08002240 crash_disable_local_vmclear(cpu);
Xiao Guangrong5a560f82012-11-28 20:54:14 +08002241
2242 /*
2243 * Read loaded_vmcs->cpu should be before fetching
2244 * loaded_vmcs->loaded_vmcss_on_cpu_link.
2245 * See the comments in __loaded_vmcs_clear().
2246 */
2247 smp_rmb();
2248
Nadav Har'Eld462b812011-05-24 15:26:10 +03002249 list_add(&vmx->loaded_vmcs->loaded_vmcss_on_cpu_link,
2250 &per_cpu(loaded_vmcss_on_cpu, cpu));
Zhang Yanfei8f536b72012-12-06 23:43:34 +08002251 crash_enable_local_vmclear(cpu);
Dongxiao Xu92fe13b2010-05-11 18:29:42 +08002252 local_irq_enable();
Jim Mattsonb80c76e2016-07-29 18:56:53 -07002253 }
2254
2255 if (per_cpu(current_vmcs, cpu) != vmx->loaded_vmcs->vmcs) {
2256 per_cpu(current_vmcs, cpu) = vmx->loaded_vmcs->vmcs;
2257 vmcs_load(vmx->loaded_vmcs->vmcs);
2258 }
2259
2260 if (!already_loaded) {
Andy Lutomirski59c58ceb2017-03-22 14:32:33 -07002261 void *gdt = get_current_gdt_ro();
Jim Mattsonb80c76e2016-07-29 18:56:53 -07002262 unsigned long sysenter_esp;
2263
2264 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
Dongxiao Xu92fe13b2010-05-11 18:29:42 +08002265
Avi Kivity6aa8b732006-12-10 02:21:36 -08002266 /*
2267 * Linux uses per-cpu TSS and GDT, so set these when switching
Andy Lutomirskie0c23062017-02-20 08:56:10 -08002268 * processors. See 22.2.4.
Avi Kivity6aa8b732006-12-10 02:21:36 -08002269 */
Andy Lutomirskie0c23062017-02-20 08:56:10 -08002270 vmcs_writel(HOST_TR_BASE,
2271 (unsigned long)this_cpu_ptr(&cpu_tss));
Andy Lutomirski59c58ceb2017-03-22 14:32:33 -07002272 vmcs_writel(HOST_GDTR_BASE, (unsigned long)gdt); /* 22.2.4 */
Avi Kivity6aa8b732006-12-10 02:21:36 -08002273
Andy Lutomirskib7ffc442017-02-20 08:56:14 -08002274 /*
2275 * VM exits change the host TR limit to 0x67 after a VM
2276 * exit. This is okay, since 0x67 covers everything except
2277 * the IO bitmap and have have code to handle the IO bitmap
2278 * being lost after a VM exit.
2279 */
2280 BUILD_BUG_ON(IO_BITMAP_OFFSET - 1 != 0x67);
2281
Avi Kivity6aa8b732006-12-10 02:21:36 -08002282 rdmsrl(MSR_IA32_SYSENTER_ESP, sysenter_esp);
2283 vmcs_writel(HOST_IA32_SYSENTER_ESP, sysenter_esp); /* 22.2.3 */
Haozhong Zhangff2c3a12015-10-20 15:39:10 +08002284
Nadav Har'Eld462b812011-05-24 15:26:10 +03002285 vmx->loaded_vmcs->cpu = cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002286 }
Feng Wu28b835d2015-09-18 22:29:54 +08002287
Owen Hofmann2680d6d2016-03-01 13:36:13 -08002288 /* Setup TSC multiplier */
2289 if (kvm_has_tsc_control &&
Peter Feinerc95ba922016-08-17 09:36:47 -07002290 vmx->current_tsc_ratio != vcpu->arch.tsc_scaling_ratio)
2291 decache_tsc_multiplier(vmx);
Owen Hofmann2680d6d2016-03-01 13:36:13 -08002292
Feng Wu28b835d2015-09-18 22:29:54 +08002293 vmx_vcpu_pi_load(vcpu, cpu);
Xiao Guangrong1be0e612016-03-22 16:51:18 +08002294 vmx->host_pkru = read_pkru();
Wanpeng Li74c55932017-11-29 01:31:20 -08002295 vmx->host_debugctlmsr = get_debugctlmsr();
Feng Wu28b835d2015-09-18 22:29:54 +08002296}
2297
2298static void vmx_vcpu_pi_put(struct kvm_vcpu *vcpu)
2299{
2300 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
2301
2302 if (!kvm_arch_has_assigned_device(vcpu->kvm) ||
Yang Zhanga0052192016-06-13 09:56:56 +08002303 !irq_remapping_cap(IRQ_POSTING_CAP) ||
2304 !kvm_vcpu_apicv_active(vcpu))
Feng Wu28b835d2015-09-18 22:29:54 +08002305 return;
2306
2307 /* Set SN when the vCPU is preempted */
2308 if (vcpu->preempted)
2309 pi_set_sn(pi_desc);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002310}
2311
2312static void vmx_vcpu_put(struct kvm_vcpu *vcpu)
2313{
Feng Wu28b835d2015-09-18 22:29:54 +08002314 vmx_vcpu_pi_put(vcpu);
2315
Avi Kivitya9b21b62008-06-24 11:48:49 +03002316 __vmx_load_host_state(to_vmx(vcpu));
Avi Kivity6aa8b732006-12-10 02:21:36 -08002317}
2318
Wanpeng Lif244dee2017-07-20 01:11:54 -07002319static bool emulation_required(struct kvm_vcpu *vcpu)
2320{
2321 return emulate_invalid_guest_state && !guest_state_valid(vcpu);
2322}
2323
Avi Kivityedcafe32009-12-30 18:07:40 +02002324static void vmx_decache_cr0_guest_bits(struct kvm_vcpu *vcpu);
2325
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03002326/*
2327 * Return the cr0 value that a nested guest would read. This is a combination
2328 * of the real cr0 used to run the guest (guest_cr0), and the bits shadowed by
2329 * its hypervisor (cr0_read_shadow).
2330 */
2331static inline unsigned long nested_read_cr0(struct vmcs12 *fields)
2332{
2333 return (fields->guest_cr0 & ~fields->cr0_guest_host_mask) |
2334 (fields->cr0_read_shadow & fields->cr0_guest_host_mask);
2335}
2336static inline unsigned long nested_read_cr4(struct vmcs12 *fields)
2337{
2338 return (fields->guest_cr4 & ~fields->cr4_guest_host_mask) |
2339 (fields->cr4_read_shadow & fields->cr4_guest_host_mask);
2340}
2341
Avi Kivity6aa8b732006-12-10 02:21:36 -08002342static unsigned long vmx_get_rflags(struct kvm_vcpu *vcpu)
2343{
Avi Kivity78ac8b42010-04-08 18:19:35 +03002344 unsigned long rflags, save_rflags;
Avi Kivity345dcaa2009-08-12 15:29:37 +03002345
Avi Kivity6de12732011-03-07 12:51:22 +02002346 if (!test_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail)) {
2347 __set_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail);
2348 rflags = vmcs_readl(GUEST_RFLAGS);
2349 if (to_vmx(vcpu)->rmode.vm86_active) {
2350 rflags &= RMODE_GUEST_OWNED_EFLAGS_BITS;
2351 save_rflags = to_vmx(vcpu)->rmode.save_rflags;
2352 rflags |= save_rflags & ~RMODE_GUEST_OWNED_EFLAGS_BITS;
2353 }
2354 to_vmx(vcpu)->rflags = rflags;
Avi Kivity78ac8b42010-04-08 18:19:35 +03002355 }
Avi Kivity6de12732011-03-07 12:51:22 +02002356 return to_vmx(vcpu)->rflags;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002357}
2358
2359static void vmx_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags)
2360{
Wanpeng Lif244dee2017-07-20 01:11:54 -07002361 unsigned long old_rflags = vmx_get_rflags(vcpu);
2362
Avi Kivity6de12732011-03-07 12:51:22 +02002363 __set_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail);
2364 to_vmx(vcpu)->rflags = rflags;
Avi Kivity78ac8b42010-04-08 18:19:35 +03002365 if (to_vmx(vcpu)->rmode.vm86_active) {
2366 to_vmx(vcpu)->rmode.save_rflags = rflags;
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +01002367 rflags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM;
Avi Kivity78ac8b42010-04-08 18:19:35 +03002368 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002369 vmcs_writel(GUEST_RFLAGS, rflags);
Wanpeng Lif244dee2017-07-20 01:11:54 -07002370
2371 if ((old_rflags ^ to_vmx(vcpu)->rflags) & X86_EFLAGS_VM)
2372 to_vmx(vcpu)->emulation_required = emulation_required(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002373}
2374
Paolo Bonzini37ccdcb2014-05-20 14:29:47 +02002375static u32 vmx_get_interrupt_shadow(struct kvm_vcpu *vcpu)
Glauber Costa2809f5d2009-05-12 16:21:05 -04002376{
2377 u32 interruptibility = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
2378 int ret = 0;
2379
2380 if (interruptibility & GUEST_INTR_STATE_STI)
Jan Kiszka48005f62010-02-19 19:38:07 +01002381 ret |= KVM_X86_SHADOW_INT_STI;
Glauber Costa2809f5d2009-05-12 16:21:05 -04002382 if (interruptibility & GUEST_INTR_STATE_MOV_SS)
Jan Kiszka48005f62010-02-19 19:38:07 +01002383 ret |= KVM_X86_SHADOW_INT_MOV_SS;
Glauber Costa2809f5d2009-05-12 16:21:05 -04002384
Paolo Bonzini37ccdcb2014-05-20 14:29:47 +02002385 return ret;
Glauber Costa2809f5d2009-05-12 16:21:05 -04002386}
2387
2388static void vmx_set_interrupt_shadow(struct kvm_vcpu *vcpu, int mask)
2389{
2390 u32 interruptibility_old = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
2391 u32 interruptibility = interruptibility_old;
2392
2393 interruptibility &= ~(GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS);
2394
Jan Kiszka48005f62010-02-19 19:38:07 +01002395 if (mask & KVM_X86_SHADOW_INT_MOV_SS)
Glauber Costa2809f5d2009-05-12 16:21:05 -04002396 interruptibility |= GUEST_INTR_STATE_MOV_SS;
Jan Kiszka48005f62010-02-19 19:38:07 +01002397 else if (mask & KVM_X86_SHADOW_INT_STI)
Glauber Costa2809f5d2009-05-12 16:21:05 -04002398 interruptibility |= GUEST_INTR_STATE_STI;
2399
2400 if ((interruptibility != interruptibility_old))
2401 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, interruptibility);
2402}
2403
Avi Kivity6aa8b732006-12-10 02:21:36 -08002404static void skip_emulated_instruction(struct kvm_vcpu *vcpu)
2405{
2406 unsigned long rip;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002407
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002408 rip = kvm_rip_read(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002409 rip += vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002410 kvm_rip_write(vcpu, rip);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002411
Glauber Costa2809f5d2009-05-12 16:21:05 -04002412 /* skipping an emulated instruction also counts */
2413 vmx_set_interrupt_shadow(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002414}
2415
Paolo Bonzinib96fb432017-07-27 12:29:32 +02002416static void nested_vmx_inject_exception_vmexit(struct kvm_vcpu *vcpu,
2417 unsigned long exit_qual)
2418{
2419 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
2420 unsigned int nr = vcpu->arch.exception.nr;
2421 u32 intr_info = nr | INTR_INFO_VALID_MASK;
2422
2423 if (vcpu->arch.exception.has_error_code) {
2424 vmcs12->vm_exit_intr_error_code = vcpu->arch.exception.error_code;
2425 intr_info |= INTR_INFO_DELIVER_CODE_MASK;
2426 }
2427
2428 if (kvm_exception_is_soft(nr))
2429 intr_info |= INTR_TYPE_SOFT_EXCEPTION;
2430 else
2431 intr_info |= INTR_TYPE_HARD_EXCEPTION;
2432
2433 if (!(vmcs12->idt_vectoring_info_field & VECTORING_INFO_VALID_MASK) &&
2434 vmx_get_nmi_mask(vcpu))
2435 intr_info |= INTR_INFO_UNBLOCK_NMI;
2436
2437 nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI, intr_info, exit_qual);
2438}
2439
Nadav Har'El0b6ac342011-05-25 23:13:36 +03002440/*
2441 * KVM wants to inject page-faults which it got to the guest. This function
2442 * checks whether in a nested guest, we need to inject them to L1 or L2.
Nadav Har'El0b6ac342011-05-25 23:13:36 +03002443 */
Wanpeng Libfcf83b2017-08-24 03:35:11 -07002444static int nested_vmx_check_exception(struct kvm_vcpu *vcpu, unsigned long *exit_qual)
Nadav Har'El0b6ac342011-05-25 23:13:36 +03002445{
2446 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Wanpeng Liadfe20f2017-07-13 18:30:41 -07002447 unsigned int nr = vcpu->arch.exception.nr;
Nadav Har'El0b6ac342011-05-25 23:13:36 +03002448
Paolo Bonzinib96fb432017-07-27 12:29:32 +02002449 if (nr == PF_VECTOR) {
2450 if (vcpu->arch.exception.nested_apf) {
Wanpeng Libfcf83b2017-08-24 03:35:11 -07002451 *exit_qual = vcpu->arch.apf.nested_apf_token;
Paolo Bonzinib96fb432017-07-27 12:29:32 +02002452 return 1;
2453 }
2454 /*
2455 * FIXME: we must not write CR2 when L1 intercepts an L2 #PF exception.
2456 * The fix is to add the ancillary datum (CR2 or DR6) to structs
2457 * kvm_queued_exception and kvm_vcpu_events, so that CR2 and DR6
2458 * can be written only when inject_pending_event runs. This should be
2459 * conditional on a new capability---if the capability is disabled,
2460 * kvm_multiple_exception would write the ancillary information to
2461 * CR2 or DR6, for backwards ABI-compatibility.
2462 */
2463 if (nested_vmx_is_page_fault_vmexit(vmcs12,
2464 vcpu->arch.exception.error_code)) {
Wanpeng Libfcf83b2017-08-24 03:35:11 -07002465 *exit_qual = vcpu->arch.cr2;
Paolo Bonzinib96fb432017-07-27 12:29:32 +02002466 return 1;
2467 }
2468 } else {
Paolo Bonzinib96fb432017-07-27 12:29:32 +02002469 if (vmcs12->exception_bitmap & (1u << nr)) {
Wanpeng Libfcf83b2017-08-24 03:35:11 -07002470 if (nr == DB_VECTOR)
2471 *exit_qual = vcpu->arch.dr6;
2472 else
2473 *exit_qual = 0;
Paolo Bonzinib96fb432017-07-27 12:29:32 +02002474 return 1;
2475 }
Wanpeng Liadfe20f2017-07-13 18:30:41 -07002476 }
2477
Paolo Bonzinib96fb432017-07-27 12:29:32 +02002478 return 0;
Nadav Har'El0b6ac342011-05-25 23:13:36 +03002479}
2480
Wanpeng Licfcd20e2017-07-13 18:30:39 -07002481static void vmx_queue_exception(struct kvm_vcpu *vcpu)
Avi Kivity298101d2007-11-25 13:41:11 +02002482{
Jan Kiszka77ab6db2008-07-14 12:28:51 +02002483 struct vcpu_vmx *vmx = to_vmx(vcpu);
Wanpeng Licfcd20e2017-07-13 18:30:39 -07002484 unsigned nr = vcpu->arch.exception.nr;
2485 bool has_error_code = vcpu->arch.exception.has_error_code;
Wanpeng Licfcd20e2017-07-13 18:30:39 -07002486 u32 error_code = vcpu->arch.exception.error_code;
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01002487 u32 intr_info = nr | INTR_INFO_VALID_MASK;
Jan Kiszka77ab6db2008-07-14 12:28:51 +02002488
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01002489 if (has_error_code) {
Jan Kiszka77ab6db2008-07-14 12:28:51 +02002490 vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE, error_code);
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01002491 intr_info |= INTR_INFO_DELIVER_CODE_MASK;
2492 }
Jan Kiszka77ab6db2008-07-14 12:28:51 +02002493
Avi Kivity7ffd92c2009-06-09 14:10:45 +03002494 if (vmx->rmode.vm86_active) {
Serge E. Hallyn71f98332011-04-13 09:12:54 -05002495 int inc_eip = 0;
2496 if (kvm_exception_is_soft(nr))
2497 inc_eip = vcpu->arch.event_exit_inst_len;
2498 if (kvm_inject_realmode_interrupt(vcpu, nr, inc_eip) != EMULATE_DONE)
Mohammed Gamala92601b2010-09-19 14:34:07 +02002499 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Jan Kiszka77ab6db2008-07-14 12:28:51 +02002500 return;
2501 }
2502
Gleb Natapov66fd3f72009-05-11 13:35:50 +03002503 if (kvm_exception_is_soft(nr)) {
2504 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
2505 vmx->vcpu.arch.event_exit_inst_len);
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01002506 intr_info |= INTR_TYPE_SOFT_EXCEPTION;
2507 } else
2508 intr_info |= INTR_TYPE_HARD_EXCEPTION;
2509
2510 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr_info);
Avi Kivity298101d2007-11-25 13:41:11 +02002511}
2512
Sheng Yang4e47c7a2009-12-18 16:48:47 +08002513static bool vmx_rdtscp_supported(void)
2514{
2515 return cpu_has_vmx_rdtscp();
2516}
2517
Mao, Junjiead756a12012-07-02 01:18:48 +00002518static bool vmx_invpcid_supported(void)
2519{
2520 return cpu_has_vmx_invpcid() && enable_ept;
2521}
2522
Avi Kivity6aa8b732006-12-10 02:21:36 -08002523/*
Eddie Donga75beee2007-05-17 18:55:15 +03002524 * Swap MSR entry in host/guest MSR entry array.
2525 */
Rusty Russell8b9cf982007-07-30 16:31:43 +10002526static void move_msr_up(struct vcpu_vmx *vmx, int from, int to)
Eddie Donga75beee2007-05-17 18:55:15 +03002527{
Avi Kivity26bb0982009-09-07 11:14:12 +03002528 struct shared_msr_entry tmp;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002529
2530 tmp = vmx->guest_msrs[to];
2531 vmx->guest_msrs[to] = vmx->guest_msrs[from];
2532 vmx->guest_msrs[from] = tmp;
Eddie Donga75beee2007-05-17 18:55:15 +03002533}
2534
Yang Zhang8d146952013-01-25 10:18:50 +08002535static void vmx_set_msr_bitmap(struct kvm_vcpu *vcpu)
2536{
2537 unsigned long *msr_bitmap;
2538
Wincy Van670125b2015-03-04 14:31:56 +08002539 if (is_guest_mode(vcpu))
Radim Krčmářd048c092016-08-08 20:16:22 +02002540 msr_bitmap = to_vmx(vcpu)->nested.msr_bitmap;
Roman Kagan3ce424e2016-05-18 17:48:20 +03002541 else if (cpu_has_secondary_exec_ctrls() &&
2542 (vmcs_read32(SECONDARY_VM_EXEC_CONTROL) &
2543 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE)) {
Wanpeng Lif6e90f92016-09-22 07:43:25 +08002544 if (enable_apicv && kvm_vcpu_apicv_active(vcpu)) {
2545 if (is_long_mode(vcpu))
Wanpeng Lic63e4562016-09-23 19:17:16 +08002546 msr_bitmap = vmx_msr_bitmap_longmode_x2apic_apicv;
2547 else
2548 msr_bitmap = vmx_msr_bitmap_legacy_x2apic_apicv;
2549 } else {
2550 if (is_long_mode(vcpu))
Wanpeng Lif6e90f92016-09-22 07:43:25 +08002551 msr_bitmap = vmx_msr_bitmap_longmode_x2apic;
2552 else
2553 msr_bitmap = vmx_msr_bitmap_legacy_x2apic;
Wanpeng Lif6e90f92016-09-22 07:43:25 +08002554 }
Yang Zhang8d146952013-01-25 10:18:50 +08002555 } else {
2556 if (is_long_mode(vcpu))
2557 msr_bitmap = vmx_msr_bitmap_longmode;
2558 else
2559 msr_bitmap = vmx_msr_bitmap_legacy;
2560 }
2561
2562 vmcs_write64(MSR_BITMAP, __pa(msr_bitmap));
2563}
2564
Eddie Donga75beee2007-05-17 18:55:15 +03002565/*
Avi Kivitye38aea32007-04-19 13:22:48 +03002566 * Set up the vmcs to automatically save and restore system
2567 * msrs. Don't touch the 64-bit msrs if the guest is in legacy
2568 * mode, as fiddling with msrs is very expensive.
2569 */
Rusty Russell8b9cf982007-07-30 16:31:43 +10002570static void setup_msrs(struct vcpu_vmx *vmx)
Avi Kivitye38aea32007-04-19 13:22:48 +03002571{
Avi Kivity26bb0982009-09-07 11:14:12 +03002572 int save_nmsrs, index;
Avi Kivitye38aea32007-04-19 13:22:48 +03002573
Eddie Donga75beee2007-05-17 18:55:15 +03002574 save_nmsrs = 0;
Avi Kivity4d56c8a2007-04-19 14:28:44 +03002575#ifdef CONFIG_X86_64
Rusty Russell8b9cf982007-07-30 16:31:43 +10002576 if (is_long_mode(&vmx->vcpu)) {
Rusty Russell8b9cf982007-07-30 16:31:43 +10002577 index = __find_msr_index(vmx, MSR_SYSCALL_MASK);
Eddie Donga75beee2007-05-17 18:55:15 +03002578 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +10002579 move_msr_up(vmx, index, save_nmsrs++);
2580 index = __find_msr_index(vmx, MSR_LSTAR);
Eddie Donga75beee2007-05-17 18:55:15 +03002581 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +10002582 move_msr_up(vmx, index, save_nmsrs++);
2583 index = __find_msr_index(vmx, MSR_CSTAR);
Eddie Donga75beee2007-05-17 18:55:15 +03002584 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +10002585 move_msr_up(vmx, index, save_nmsrs++);
Sheng Yang4e47c7a2009-12-18 16:48:47 +08002586 index = __find_msr_index(vmx, MSR_TSC_AUX);
Radim Krčmářd6321d42017-08-05 00:12:49 +02002587 if (index >= 0 && guest_cpuid_has(&vmx->vcpu, X86_FEATURE_RDTSCP))
Sheng Yang4e47c7a2009-12-18 16:48:47 +08002588 move_msr_up(vmx, index, save_nmsrs++);
Eddie Donga75beee2007-05-17 18:55:15 +03002589 /*
Brian Gerst8c065852010-07-17 09:03:26 -04002590 * MSR_STAR is only needed on long mode guests, and only
Eddie Donga75beee2007-05-17 18:55:15 +03002591 * if efer.sce is enabled.
2592 */
Brian Gerst8c065852010-07-17 09:03:26 -04002593 index = __find_msr_index(vmx, MSR_STAR);
Avi Kivityf6801df2010-01-21 15:31:50 +02002594 if ((index >= 0) && (vmx->vcpu.arch.efer & EFER_SCE))
Rusty Russell8b9cf982007-07-30 16:31:43 +10002595 move_msr_up(vmx, index, save_nmsrs++);
Avi Kivity4d56c8a2007-04-19 14:28:44 +03002596 }
Eddie Donga75beee2007-05-17 18:55:15 +03002597#endif
Avi Kivity92c0d902009-10-29 11:00:16 +02002598 index = __find_msr_index(vmx, MSR_EFER);
2599 if (index >= 0 && update_transition_efer(vmx, index))
Avi Kivity26bb0982009-09-07 11:14:12 +03002600 move_msr_up(vmx, index, save_nmsrs++);
Avi Kivity4d56c8a2007-04-19 14:28:44 +03002601
Avi Kivity26bb0982009-09-07 11:14:12 +03002602 vmx->save_nmsrs = save_nmsrs;
Avi Kivity58972972009-02-24 22:26:47 +02002603
Yang Zhang8d146952013-01-25 10:18:50 +08002604 if (cpu_has_vmx_msr_bitmap())
2605 vmx_set_msr_bitmap(&vmx->vcpu);
Avi Kivitye38aea32007-04-19 13:22:48 +03002606}
2607
2608/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08002609 * reads and returns guest's timestamp counter "register"
Haozhong Zhangbe7b2632015-10-20 15:39:11 +08002610 * guest_tsc = (host_tsc * tsc multiplier) >> 48 + tsc_offset
2611 * -- Intel TSC Scaling for Virtualization White Paper, sec 1.3
Avi Kivity6aa8b732006-12-10 02:21:36 -08002612 */
Haozhong Zhangbe7b2632015-10-20 15:39:11 +08002613static u64 guest_read_tsc(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002614{
2615 u64 host_tsc, tsc_offset;
2616
Andy Lutomirski4ea16362015-06-25 18:44:07 +02002617 host_tsc = rdtsc();
Avi Kivity6aa8b732006-12-10 02:21:36 -08002618 tsc_offset = vmcs_read64(TSC_OFFSET);
Haozhong Zhangbe7b2632015-10-20 15:39:11 +08002619 return kvm_scale_tsc(vcpu, host_tsc) + tsc_offset;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002620}
2621
2622/*
Zachary Amsden99e3e302010-08-19 22:07:17 -10002623 * writes 'offset' into guest's timestamp counter offset register
Avi Kivity6aa8b732006-12-10 02:21:36 -08002624 */
Zachary Amsden99e3e302010-08-19 22:07:17 -10002625static void vmx_write_tsc_offset(struct kvm_vcpu *vcpu, u64 offset)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002626{
Nadav Har'El27fc51b2011-08-02 15:54:52 +03002627 if (is_guest_mode(vcpu)) {
Nadav Har'El79918252011-05-25 23:15:39 +03002628 /*
Nadav Har'El27fc51b2011-08-02 15:54:52 +03002629 * We're here if L1 chose not to trap WRMSR to TSC. According
2630 * to the spec, this should set L1's TSC; The offset that L1
2631 * set for L2 remains unchanged, and still needs to be added
2632 * to the newly set TSC to get L2's TSC.
Nadav Har'El79918252011-05-25 23:15:39 +03002633 */
Nadav Har'El27fc51b2011-08-02 15:54:52 +03002634 struct vmcs12 *vmcs12;
Nadav Har'El27fc51b2011-08-02 15:54:52 +03002635 /* recalculate vmcs02.TSC_OFFSET: */
2636 vmcs12 = get_vmcs12(vcpu);
2637 vmcs_write64(TSC_OFFSET, offset +
2638 (nested_cpu_has(vmcs12, CPU_BASED_USE_TSC_OFFSETING) ?
2639 vmcs12->tsc_offset : 0));
2640 } else {
Yoshihiro YUNOMAE489223e2013-06-12 16:43:44 +09002641 trace_kvm_write_tsc_offset(vcpu->vcpu_id,
2642 vmcs_read64(TSC_OFFSET), offset);
Nadav Har'El27fc51b2011-08-02 15:54:52 +03002643 vmcs_write64(TSC_OFFSET, offset);
2644 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002645}
2646
Nadav Har'El801d3422011-05-25 23:02:23 +03002647/*
2648 * nested_vmx_allowed() checks whether a guest should be allowed to use VMX
2649 * instructions and MSRs (i.e., nested VMX). Nested VMX is disabled for
2650 * all guests if the "nested" module option is off, and can also be disabled
2651 * for a single guest by disabling its VMX cpuid bit.
2652 */
2653static inline bool nested_vmx_allowed(struct kvm_vcpu *vcpu)
2654{
Radim Krčmářd6321d42017-08-05 00:12:49 +02002655 return nested && guest_cpuid_has(vcpu, X86_FEATURE_VMX);
Nadav Har'El801d3422011-05-25 23:02:23 +03002656}
2657
Avi Kivity6aa8b732006-12-10 02:21:36 -08002658/*
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002659 * nested_vmx_setup_ctls_msrs() sets up variables containing the values to be
2660 * returned for the various VMX controls MSRs when nested VMX is enabled.
2661 * The same values should also be used to verify that vmcs12 control fields are
2662 * valid during nested entry from L1 to L2.
2663 * Each of these control msrs has a low and high 32-bit half: A low bit is on
2664 * if the corresponding bit in the (32-bit) control field *must* be on, and a
2665 * bit in the high half is on if the corresponding bit in the control field
2666 * may be on. See also vmx_control_verify().
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002667 */
Wincy Vanb9c237b2015-02-03 23:56:30 +08002668static void nested_vmx_setup_ctls_msrs(struct vcpu_vmx *vmx)
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002669{
2670 /*
2671 * Note that as a general rule, the high half of the MSRs (bits in
2672 * the control fields which may be 1) should be initialized by the
2673 * intersection of the underlying hardware's MSR (i.e., features which
2674 * can be supported) and the list of features we want to expose -
2675 * because they are known to be properly supported in our code.
2676 * Also, usually, the low half of the MSRs (bits which must be 1) can
2677 * be set to 0, meaning that L1 may turn off any of these bits. The
2678 * reason is that if one of these bits is necessary, it will appear
2679 * in vmcs01 and prepare_vmcs02, when it bitwise-or's the control
2680 * fields of vmcs01 and vmcs02, will turn these bits off - and
Paolo Bonzini7313c692017-07-27 10:31:25 +02002681 * nested_vmx_exit_reflected() will not pass related exits to L1.
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002682 * These rules have exceptions below.
2683 */
2684
2685 /* pin-based controls */
Jan Kiszkaeabeaac2013-03-13 11:30:50 +01002686 rdmsr(MSR_IA32_VMX_PINBASED_CTLS,
Wincy Vanb9c237b2015-02-03 23:56:30 +08002687 vmx->nested.nested_vmx_pinbased_ctls_low,
2688 vmx->nested.nested_vmx_pinbased_ctls_high);
2689 vmx->nested.nested_vmx_pinbased_ctls_low |=
2690 PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
2691 vmx->nested.nested_vmx_pinbased_ctls_high &=
2692 PIN_BASED_EXT_INTR_MASK |
2693 PIN_BASED_NMI_EXITING |
2694 PIN_BASED_VIRTUAL_NMIS;
2695 vmx->nested.nested_vmx_pinbased_ctls_high |=
2696 PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR |
Jan Kiszka0238ea92013-03-13 11:31:24 +01002697 PIN_BASED_VMX_PREEMPTION_TIMER;
Andrey Smetanind62caab2015-11-10 15:36:33 +03002698 if (kvm_vcpu_apicv_active(&vmx->vcpu))
Wincy Van705699a2015-02-03 23:58:17 +08002699 vmx->nested.nested_vmx_pinbased_ctls_high |=
2700 PIN_BASED_POSTED_INTR;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002701
Jan Kiszka3dbcd8d2014-06-16 13:59:40 +02002702 /* exit controls */
Arthur Chunqi Lic0dfee52013-08-06 18:41:45 +08002703 rdmsr(MSR_IA32_VMX_EXIT_CTLS,
Wincy Vanb9c237b2015-02-03 23:56:30 +08002704 vmx->nested.nested_vmx_exit_ctls_low,
2705 vmx->nested.nested_vmx_exit_ctls_high);
2706 vmx->nested.nested_vmx_exit_ctls_low =
2707 VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR;
Bandan Dase0ba1a62014-04-19 18:17:46 -04002708
Wincy Vanb9c237b2015-02-03 23:56:30 +08002709 vmx->nested.nested_vmx_exit_ctls_high &=
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002710#ifdef CONFIG_X86_64
Arthur Chunqi Lic0dfee52013-08-06 18:41:45 +08002711 VM_EXIT_HOST_ADDR_SPACE_SIZE |
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002712#endif
Jan Kiszkaf41245002014-03-07 20:03:13 +01002713 VM_EXIT_LOAD_IA32_PAT | VM_EXIT_SAVE_IA32_PAT;
Wincy Vanb9c237b2015-02-03 23:56:30 +08002714 vmx->nested.nested_vmx_exit_ctls_high |=
2715 VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR |
Jan Kiszkaf41245002014-03-07 20:03:13 +01002716 VM_EXIT_LOAD_IA32_EFER | VM_EXIT_SAVE_IA32_EFER |
Bandan Dase0ba1a62014-04-19 18:17:46 -04002717 VM_EXIT_SAVE_VMX_PREEMPTION_TIMER | VM_EXIT_ACK_INTR_ON_EXIT;
2718
Paolo Bonzinia87036a2016-03-08 09:52:13 +01002719 if (kvm_mpx_supported())
Wincy Vanb9c237b2015-02-03 23:56:30 +08002720 vmx->nested.nested_vmx_exit_ctls_high |= VM_EXIT_CLEAR_BNDCFGS;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002721
Jan Kiszka2996fca2014-06-16 13:59:43 +02002722 /* We support free control of debug control saving. */
David Matlack0115f9c2016-11-29 18:14:06 -08002723 vmx->nested.nested_vmx_exit_ctls_low &= ~VM_EXIT_SAVE_DEBUG_CONTROLS;
Jan Kiszka2996fca2014-06-16 13:59:43 +02002724
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002725 /* entry controls */
2726 rdmsr(MSR_IA32_VMX_ENTRY_CTLS,
Wincy Vanb9c237b2015-02-03 23:56:30 +08002727 vmx->nested.nested_vmx_entry_ctls_low,
2728 vmx->nested.nested_vmx_entry_ctls_high);
2729 vmx->nested.nested_vmx_entry_ctls_low =
2730 VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR;
2731 vmx->nested.nested_vmx_entry_ctls_high &=
Jan Kiszka57435342013-08-06 10:39:56 +02002732#ifdef CONFIG_X86_64
2733 VM_ENTRY_IA32E_MODE |
2734#endif
2735 VM_ENTRY_LOAD_IA32_PAT;
Wincy Vanb9c237b2015-02-03 23:56:30 +08002736 vmx->nested.nested_vmx_entry_ctls_high |=
2737 (VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR | VM_ENTRY_LOAD_IA32_EFER);
Paolo Bonzinia87036a2016-03-08 09:52:13 +01002738 if (kvm_mpx_supported())
Wincy Vanb9c237b2015-02-03 23:56:30 +08002739 vmx->nested.nested_vmx_entry_ctls_high |= VM_ENTRY_LOAD_BNDCFGS;
Jan Kiszka57435342013-08-06 10:39:56 +02002740
Jan Kiszka2996fca2014-06-16 13:59:43 +02002741 /* We support free control of debug control loading. */
David Matlack0115f9c2016-11-29 18:14:06 -08002742 vmx->nested.nested_vmx_entry_ctls_low &= ~VM_ENTRY_LOAD_DEBUG_CONTROLS;
Jan Kiszka2996fca2014-06-16 13:59:43 +02002743
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002744 /* cpu-based controls */
2745 rdmsr(MSR_IA32_VMX_PROCBASED_CTLS,
Wincy Vanb9c237b2015-02-03 23:56:30 +08002746 vmx->nested.nested_vmx_procbased_ctls_low,
2747 vmx->nested.nested_vmx_procbased_ctls_high);
2748 vmx->nested.nested_vmx_procbased_ctls_low =
2749 CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
2750 vmx->nested.nested_vmx_procbased_ctls_high &=
Jan Kiszkaa294c9b2013-10-23 17:43:09 +01002751 CPU_BASED_VIRTUAL_INTR_PENDING |
2752 CPU_BASED_VIRTUAL_NMI_PENDING | CPU_BASED_USE_TSC_OFFSETING |
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002753 CPU_BASED_HLT_EXITING | CPU_BASED_INVLPG_EXITING |
2754 CPU_BASED_MWAIT_EXITING | CPU_BASED_CR3_LOAD_EXITING |
2755 CPU_BASED_CR3_STORE_EXITING |
2756#ifdef CONFIG_X86_64
2757 CPU_BASED_CR8_LOAD_EXITING | CPU_BASED_CR8_STORE_EXITING |
2758#endif
2759 CPU_BASED_MOV_DR_EXITING | CPU_BASED_UNCOND_IO_EXITING |
Mihai Donțu5f3d45e2015-07-05 20:08:57 +03002760 CPU_BASED_USE_IO_BITMAPS | CPU_BASED_MONITOR_TRAP_FLAG |
2761 CPU_BASED_MONITOR_EXITING | CPU_BASED_RDPMC_EXITING |
2762 CPU_BASED_RDTSC_EXITING | CPU_BASED_PAUSE_EXITING |
2763 CPU_BASED_TPR_SHADOW | CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002764 /*
2765 * We can allow some features even when not supported by the
2766 * hardware. For example, L1 can specify an MSR bitmap - and we
2767 * can use it to avoid exits to L1 - even when L0 runs L2
2768 * without MSR bitmaps.
2769 */
Wincy Vanb9c237b2015-02-03 23:56:30 +08002770 vmx->nested.nested_vmx_procbased_ctls_high |=
2771 CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR |
Jan Kiszka560b7ee2014-06-16 13:59:42 +02002772 CPU_BASED_USE_MSR_BITMAPS;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002773
Jan Kiszka3dcdf3ec2014-06-16 13:59:41 +02002774 /* We support free control of CR3 access interception. */
David Matlack0115f9c2016-11-29 18:14:06 -08002775 vmx->nested.nested_vmx_procbased_ctls_low &=
Jan Kiszka3dcdf3ec2014-06-16 13:59:41 +02002776 ~(CPU_BASED_CR3_LOAD_EXITING | CPU_BASED_CR3_STORE_EXITING);
2777
Paolo Bonzini80154d72017-08-24 13:55:35 +02002778 /*
2779 * secondary cpu-based controls. Do not include those that
2780 * depend on CPUID bits, they are added later by vmx_cpuid_update.
2781 */
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002782 rdmsr(MSR_IA32_VMX_PROCBASED_CTLS2,
Wincy Vanb9c237b2015-02-03 23:56:30 +08002783 vmx->nested.nested_vmx_secondary_ctls_low,
2784 vmx->nested.nested_vmx_secondary_ctls_high);
2785 vmx->nested.nested_vmx_secondary_ctls_low = 0;
2786 vmx->nested.nested_vmx_secondary_ctls_high &=
Jan Kiszkad6851fb2013-02-23 22:34:39 +01002787 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
Paolo Bonzini1b073042016-10-25 16:06:30 +02002788 SECONDARY_EXEC_DESC |
Wincy Vanf2b93282015-02-03 23:56:03 +08002789 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
Wincy Van82f0dd42015-02-03 23:57:18 +08002790 SECONDARY_EXEC_APIC_REGISTER_VIRT |
Wincy Van608406e2015-02-03 23:57:51 +08002791 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY |
Paolo Bonzini3db13482017-08-24 14:48:03 +02002792 SECONDARY_EXEC_WBINVD_EXITING;
Jan Kiszkac18911a2013-03-13 16:06:41 +01002793
Nadav Har'Elafa61f7522013-08-07 14:59:22 +02002794 if (enable_ept) {
2795 /* nested EPT: emulate EPT also to L1 */
Wincy Vanb9c237b2015-02-03 23:56:30 +08002796 vmx->nested.nested_vmx_secondary_ctls_high |=
Radim Krčmář0790ec12015-03-17 14:02:32 +01002797 SECONDARY_EXEC_ENABLE_EPT;
Wincy Vanb9c237b2015-02-03 23:56:30 +08002798 vmx->nested.nested_vmx_ept_caps = VMX_EPT_PAGE_WALK_4_BIT |
Paolo Bonzini7db74262017-03-08 10:49:19 +01002799 VMX_EPTP_WB_BIT | VMX_EPT_INVEPT_BIT;
Bandan Das02120c42016-07-12 18:18:52 -04002800 if (cpu_has_vmx_ept_execute_only())
2801 vmx->nested.nested_vmx_ept_caps |=
2802 VMX_EPT_EXECUTE_ONLY_BIT;
Wincy Vanb9c237b2015-02-03 23:56:30 +08002803 vmx->nested.nested_vmx_ept_caps &= vmx_capability.ept;
Bandan Das45e11812016-08-02 16:32:36 -04002804 vmx->nested.nested_vmx_ept_caps |= VMX_EPT_EXTENT_GLOBAL_BIT |
Paolo Bonzini7db74262017-03-08 10:49:19 +01002805 VMX_EPT_EXTENT_CONTEXT_BIT | VMX_EPT_2MB_PAGE_BIT |
2806 VMX_EPT_1GB_PAGE_BIT;
Bandan Das03efce62017-05-05 15:25:15 -04002807 if (enable_ept_ad_bits) {
2808 vmx->nested.nested_vmx_secondary_ctls_high |=
2809 SECONDARY_EXEC_ENABLE_PML;
Dan Carpenter7461fbc2017-05-18 10:41:15 +03002810 vmx->nested.nested_vmx_ept_caps |= VMX_EPT_AD_BIT;
Bandan Das03efce62017-05-05 15:25:15 -04002811 }
David Hildenbrand1c13bff2017-08-24 20:51:33 +02002812 }
Nadav Har'Elafa61f7522013-08-07 14:59:22 +02002813
Bandan Das27c42a12017-08-03 15:54:42 -04002814 if (cpu_has_vmx_vmfunc()) {
2815 vmx->nested.nested_vmx_secondary_ctls_high |=
2816 SECONDARY_EXEC_ENABLE_VMFUNC;
Bandan Das41ab9372017-08-03 15:54:43 -04002817 /*
2818 * Advertise EPTP switching unconditionally
2819 * since we emulate it
2820 */
Wanpeng Li575b3a22017-10-19 07:00:34 +08002821 if (enable_ept)
2822 vmx->nested.nested_vmx_vmfunc_controls =
2823 VMX_VMFUNC_EPTP_SWITCHING;
Bandan Das27c42a12017-08-03 15:54:42 -04002824 }
2825
Paolo Bonzinief697a72016-03-18 16:58:38 +01002826 /*
2827 * Old versions of KVM use the single-context version without
2828 * checking for support, so declare that it is supported even
2829 * though it is treated as global context. The alternative is
2830 * not failing the single-context invvpid, and it is worse.
2831 */
Wanpeng Li63cb6d52017-03-20 21:18:53 -07002832 if (enable_vpid) {
2833 vmx->nested.nested_vmx_secondary_ctls_high |=
2834 SECONDARY_EXEC_ENABLE_VPID;
Wanpeng Li089d7b62015-10-13 09:18:37 -07002835 vmx->nested.nested_vmx_vpid_caps = VMX_VPID_INVVPID_BIT |
Jan Dakinevichbcdde302016-10-28 07:00:30 +03002836 VMX_VPID_EXTENT_SUPPORTED_MASK;
David Hildenbrand1c13bff2017-08-24 20:51:33 +02002837 }
Wanpeng Li99b83ac2015-10-13 09:12:21 -07002838
Radim Krčmář0790ec12015-03-17 14:02:32 +01002839 if (enable_unrestricted_guest)
2840 vmx->nested.nested_vmx_secondary_ctls_high |=
2841 SECONDARY_EXEC_UNRESTRICTED_GUEST;
2842
Jan Kiszkac18911a2013-03-13 16:06:41 +01002843 /* miscellaneous data */
Wincy Vanb9c237b2015-02-03 23:56:30 +08002844 rdmsr(MSR_IA32_VMX_MISC,
2845 vmx->nested.nested_vmx_misc_low,
2846 vmx->nested.nested_vmx_misc_high);
2847 vmx->nested.nested_vmx_misc_low &= VMX_MISC_SAVE_EFER_LMA;
2848 vmx->nested.nested_vmx_misc_low |=
2849 VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE |
Jan Kiszkaf41245002014-03-07 20:03:13 +01002850 VMX_MISC_ACTIVITY_HLT;
Wincy Vanb9c237b2015-02-03 23:56:30 +08002851 vmx->nested.nested_vmx_misc_high = 0;
David Matlack62cc6b9d2016-11-29 18:14:07 -08002852
2853 /*
2854 * This MSR reports some information about VMX support. We
2855 * should return information about the VMX we emulate for the
2856 * guest, and the VMCS structure we give it - not about the
2857 * VMX support of the underlying hardware.
2858 */
2859 vmx->nested.nested_vmx_basic =
2860 VMCS12_REVISION |
2861 VMX_BASIC_TRUE_CTLS |
2862 ((u64)VMCS12_SIZE << VMX_BASIC_VMCS_SIZE_SHIFT) |
2863 (VMX_BASIC_MEM_TYPE_WB << VMX_BASIC_MEM_TYPE_SHIFT);
2864
2865 if (cpu_has_vmx_basic_inout())
2866 vmx->nested.nested_vmx_basic |= VMX_BASIC_INOUT;
2867
2868 /*
David Matlack8322ebb2016-11-29 18:14:09 -08002869 * These MSRs specify bits which the guest must keep fixed on
David Matlack62cc6b9d2016-11-29 18:14:07 -08002870 * while L1 is in VMXON mode (in L1's root mode, or running an L2).
2871 * We picked the standard core2 setting.
2872 */
2873#define VMXON_CR0_ALWAYSON (X86_CR0_PE | X86_CR0_PG | X86_CR0_NE)
2874#define VMXON_CR4_ALWAYSON X86_CR4_VMXE
2875 vmx->nested.nested_vmx_cr0_fixed0 = VMXON_CR0_ALWAYSON;
David Matlack62cc6b9d2016-11-29 18:14:07 -08002876 vmx->nested.nested_vmx_cr4_fixed0 = VMXON_CR4_ALWAYSON;
David Matlack8322ebb2016-11-29 18:14:09 -08002877
2878 /* These MSRs specify bits which the guest must keep fixed off. */
2879 rdmsrl(MSR_IA32_VMX_CR0_FIXED1, vmx->nested.nested_vmx_cr0_fixed1);
2880 rdmsrl(MSR_IA32_VMX_CR4_FIXED1, vmx->nested.nested_vmx_cr4_fixed1);
David Matlack62cc6b9d2016-11-29 18:14:07 -08002881
2882 /* highest index: VMX_PREEMPTION_TIMER_VALUE */
Jim Mattson5b157062017-12-22 12:11:12 -08002883 vmx->nested.nested_vmx_vmcs_enum = VMCS12_MAX_FIELD_INDEX << 1;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002884}
2885
David Matlack38991522016-11-29 18:14:08 -08002886/*
2887 * if fixed0[i] == 1: val[i] must be 1
2888 * if fixed1[i] == 0: val[i] must be 0
2889 */
2890static inline bool fixed_bits_valid(u64 val, u64 fixed0, u64 fixed1)
2891{
2892 return ((val & fixed1) | fixed0) == val;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002893}
2894
2895static inline bool vmx_control_verify(u32 control, u32 low, u32 high)
2896{
David Matlack38991522016-11-29 18:14:08 -08002897 return fixed_bits_valid(control, low, high);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002898}
2899
2900static inline u64 vmx_control_msr(u32 low, u32 high)
2901{
2902 return low | ((u64)high << 32);
2903}
2904
David Matlack62cc6b9d2016-11-29 18:14:07 -08002905static bool is_bitwise_subset(u64 superset, u64 subset, u64 mask)
2906{
2907 superset &= mask;
2908 subset &= mask;
2909
2910 return (superset | subset) == superset;
2911}
2912
2913static int vmx_restore_vmx_basic(struct vcpu_vmx *vmx, u64 data)
2914{
2915 const u64 feature_and_reserved =
2916 /* feature (except bit 48; see below) */
2917 BIT_ULL(49) | BIT_ULL(54) | BIT_ULL(55) |
2918 /* reserved */
2919 BIT_ULL(31) | GENMASK_ULL(47, 45) | GENMASK_ULL(63, 56);
2920 u64 vmx_basic = vmx->nested.nested_vmx_basic;
2921
2922 if (!is_bitwise_subset(vmx_basic, data, feature_and_reserved))
2923 return -EINVAL;
2924
2925 /*
2926 * KVM does not emulate a version of VMX that constrains physical
2927 * addresses of VMX structures (e.g. VMCS) to 32-bits.
2928 */
2929 if (data & BIT_ULL(48))
2930 return -EINVAL;
2931
2932 if (vmx_basic_vmcs_revision_id(vmx_basic) !=
2933 vmx_basic_vmcs_revision_id(data))
2934 return -EINVAL;
2935
2936 if (vmx_basic_vmcs_size(vmx_basic) > vmx_basic_vmcs_size(data))
2937 return -EINVAL;
2938
2939 vmx->nested.nested_vmx_basic = data;
2940 return 0;
2941}
2942
2943static int
2944vmx_restore_control_msr(struct vcpu_vmx *vmx, u32 msr_index, u64 data)
2945{
2946 u64 supported;
2947 u32 *lowp, *highp;
2948
2949 switch (msr_index) {
2950 case MSR_IA32_VMX_TRUE_PINBASED_CTLS:
2951 lowp = &vmx->nested.nested_vmx_pinbased_ctls_low;
2952 highp = &vmx->nested.nested_vmx_pinbased_ctls_high;
2953 break;
2954 case MSR_IA32_VMX_TRUE_PROCBASED_CTLS:
2955 lowp = &vmx->nested.nested_vmx_procbased_ctls_low;
2956 highp = &vmx->nested.nested_vmx_procbased_ctls_high;
2957 break;
2958 case MSR_IA32_VMX_TRUE_EXIT_CTLS:
2959 lowp = &vmx->nested.nested_vmx_exit_ctls_low;
2960 highp = &vmx->nested.nested_vmx_exit_ctls_high;
2961 break;
2962 case MSR_IA32_VMX_TRUE_ENTRY_CTLS:
2963 lowp = &vmx->nested.nested_vmx_entry_ctls_low;
2964 highp = &vmx->nested.nested_vmx_entry_ctls_high;
2965 break;
2966 case MSR_IA32_VMX_PROCBASED_CTLS2:
2967 lowp = &vmx->nested.nested_vmx_secondary_ctls_low;
2968 highp = &vmx->nested.nested_vmx_secondary_ctls_high;
2969 break;
2970 default:
2971 BUG();
2972 }
2973
2974 supported = vmx_control_msr(*lowp, *highp);
2975
2976 /* Check must-be-1 bits are still 1. */
2977 if (!is_bitwise_subset(data, supported, GENMASK_ULL(31, 0)))
2978 return -EINVAL;
2979
2980 /* Check must-be-0 bits are still 0. */
2981 if (!is_bitwise_subset(supported, data, GENMASK_ULL(63, 32)))
2982 return -EINVAL;
2983
2984 *lowp = data;
2985 *highp = data >> 32;
2986 return 0;
2987}
2988
2989static int vmx_restore_vmx_misc(struct vcpu_vmx *vmx, u64 data)
2990{
2991 const u64 feature_and_reserved_bits =
2992 /* feature */
2993 BIT_ULL(5) | GENMASK_ULL(8, 6) | BIT_ULL(14) | BIT_ULL(15) |
2994 BIT_ULL(28) | BIT_ULL(29) | BIT_ULL(30) |
2995 /* reserved */
2996 GENMASK_ULL(13, 9) | BIT_ULL(31);
2997 u64 vmx_misc;
2998
2999 vmx_misc = vmx_control_msr(vmx->nested.nested_vmx_misc_low,
3000 vmx->nested.nested_vmx_misc_high);
3001
3002 if (!is_bitwise_subset(vmx_misc, data, feature_and_reserved_bits))
3003 return -EINVAL;
3004
3005 if ((vmx->nested.nested_vmx_pinbased_ctls_high &
3006 PIN_BASED_VMX_PREEMPTION_TIMER) &&
3007 vmx_misc_preemption_timer_rate(data) !=
3008 vmx_misc_preemption_timer_rate(vmx_misc))
3009 return -EINVAL;
3010
3011 if (vmx_misc_cr3_count(data) > vmx_misc_cr3_count(vmx_misc))
3012 return -EINVAL;
3013
3014 if (vmx_misc_max_msr(data) > vmx_misc_max_msr(vmx_misc))
3015 return -EINVAL;
3016
3017 if (vmx_misc_mseg_revid(data) != vmx_misc_mseg_revid(vmx_misc))
3018 return -EINVAL;
3019
3020 vmx->nested.nested_vmx_misc_low = data;
3021 vmx->nested.nested_vmx_misc_high = data >> 32;
3022 return 0;
3023}
3024
3025static int vmx_restore_vmx_ept_vpid_cap(struct vcpu_vmx *vmx, u64 data)
3026{
3027 u64 vmx_ept_vpid_cap;
3028
3029 vmx_ept_vpid_cap = vmx_control_msr(vmx->nested.nested_vmx_ept_caps,
3030 vmx->nested.nested_vmx_vpid_caps);
3031
3032 /* Every bit is either reserved or a feature bit. */
3033 if (!is_bitwise_subset(vmx_ept_vpid_cap, data, -1ULL))
3034 return -EINVAL;
3035
3036 vmx->nested.nested_vmx_ept_caps = data;
3037 vmx->nested.nested_vmx_vpid_caps = data >> 32;
3038 return 0;
3039}
3040
3041static int vmx_restore_fixed0_msr(struct vcpu_vmx *vmx, u32 msr_index, u64 data)
3042{
3043 u64 *msr;
3044
3045 switch (msr_index) {
3046 case MSR_IA32_VMX_CR0_FIXED0:
3047 msr = &vmx->nested.nested_vmx_cr0_fixed0;
3048 break;
3049 case MSR_IA32_VMX_CR4_FIXED0:
3050 msr = &vmx->nested.nested_vmx_cr4_fixed0;
3051 break;
3052 default:
3053 BUG();
3054 }
3055
3056 /*
3057 * 1 bits (which indicates bits which "must-be-1" during VMX operation)
3058 * must be 1 in the restored value.
3059 */
3060 if (!is_bitwise_subset(data, *msr, -1ULL))
3061 return -EINVAL;
3062
3063 *msr = data;
3064 return 0;
3065}
3066
3067/*
3068 * Called when userspace is restoring VMX MSRs.
3069 *
3070 * Returns 0 on success, non-0 otherwise.
3071 */
3072static int vmx_set_vmx_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 data)
3073{
3074 struct vcpu_vmx *vmx = to_vmx(vcpu);
3075
3076 switch (msr_index) {
3077 case MSR_IA32_VMX_BASIC:
3078 return vmx_restore_vmx_basic(vmx, data);
3079 case MSR_IA32_VMX_PINBASED_CTLS:
3080 case MSR_IA32_VMX_PROCBASED_CTLS:
3081 case MSR_IA32_VMX_EXIT_CTLS:
3082 case MSR_IA32_VMX_ENTRY_CTLS:
3083 /*
3084 * The "non-true" VMX capability MSRs are generated from the
3085 * "true" MSRs, so we do not support restoring them directly.
3086 *
3087 * If userspace wants to emulate VMX_BASIC[55]=0, userspace
3088 * should restore the "true" MSRs with the must-be-1 bits
3089 * set according to the SDM Vol 3. A.2 "RESERVED CONTROLS AND
3090 * DEFAULT SETTINGS".
3091 */
3092 return -EINVAL;
3093 case MSR_IA32_VMX_TRUE_PINBASED_CTLS:
3094 case MSR_IA32_VMX_TRUE_PROCBASED_CTLS:
3095 case MSR_IA32_VMX_TRUE_EXIT_CTLS:
3096 case MSR_IA32_VMX_TRUE_ENTRY_CTLS:
3097 case MSR_IA32_VMX_PROCBASED_CTLS2:
3098 return vmx_restore_control_msr(vmx, msr_index, data);
3099 case MSR_IA32_VMX_MISC:
3100 return vmx_restore_vmx_misc(vmx, data);
3101 case MSR_IA32_VMX_CR0_FIXED0:
3102 case MSR_IA32_VMX_CR4_FIXED0:
3103 return vmx_restore_fixed0_msr(vmx, msr_index, data);
3104 case MSR_IA32_VMX_CR0_FIXED1:
3105 case MSR_IA32_VMX_CR4_FIXED1:
3106 /*
3107 * These MSRs are generated based on the vCPU's CPUID, so we
3108 * do not support restoring them directly.
3109 */
3110 return -EINVAL;
3111 case MSR_IA32_VMX_EPT_VPID_CAP:
3112 return vmx_restore_vmx_ept_vpid_cap(vmx, data);
3113 case MSR_IA32_VMX_VMCS_ENUM:
3114 vmx->nested.nested_vmx_vmcs_enum = data;
3115 return 0;
3116 default:
3117 /*
3118 * The rest of the VMX capability MSRs do not support restore.
3119 */
3120 return -EINVAL;
3121 }
3122}
3123
Jan Kiszkacae50132014-01-04 18:47:22 +01003124/* Returns 0 on success, non-0 otherwise. */
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003125static int vmx_get_vmx_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 *pdata)
3126{
Wincy Vanb9c237b2015-02-03 23:56:30 +08003127 struct vcpu_vmx *vmx = to_vmx(vcpu);
3128
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003129 switch (msr_index) {
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003130 case MSR_IA32_VMX_BASIC:
David Matlack62cc6b9d2016-11-29 18:14:07 -08003131 *pdata = vmx->nested.nested_vmx_basic;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003132 break;
3133 case MSR_IA32_VMX_TRUE_PINBASED_CTLS:
3134 case MSR_IA32_VMX_PINBASED_CTLS:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003135 *pdata = vmx_control_msr(
3136 vmx->nested.nested_vmx_pinbased_ctls_low,
3137 vmx->nested.nested_vmx_pinbased_ctls_high);
David Matlack0115f9c2016-11-29 18:14:06 -08003138 if (msr_index == MSR_IA32_VMX_PINBASED_CTLS)
3139 *pdata |= PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003140 break;
3141 case MSR_IA32_VMX_TRUE_PROCBASED_CTLS:
3142 case MSR_IA32_VMX_PROCBASED_CTLS:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003143 *pdata = vmx_control_msr(
3144 vmx->nested.nested_vmx_procbased_ctls_low,
3145 vmx->nested.nested_vmx_procbased_ctls_high);
David Matlack0115f9c2016-11-29 18:14:06 -08003146 if (msr_index == MSR_IA32_VMX_PROCBASED_CTLS)
3147 *pdata |= CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003148 break;
3149 case MSR_IA32_VMX_TRUE_EXIT_CTLS:
3150 case MSR_IA32_VMX_EXIT_CTLS:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003151 *pdata = vmx_control_msr(
3152 vmx->nested.nested_vmx_exit_ctls_low,
3153 vmx->nested.nested_vmx_exit_ctls_high);
David Matlack0115f9c2016-11-29 18:14:06 -08003154 if (msr_index == MSR_IA32_VMX_EXIT_CTLS)
3155 *pdata |= VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003156 break;
3157 case MSR_IA32_VMX_TRUE_ENTRY_CTLS:
3158 case MSR_IA32_VMX_ENTRY_CTLS:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003159 *pdata = vmx_control_msr(
3160 vmx->nested.nested_vmx_entry_ctls_low,
3161 vmx->nested.nested_vmx_entry_ctls_high);
David Matlack0115f9c2016-11-29 18:14:06 -08003162 if (msr_index == MSR_IA32_VMX_ENTRY_CTLS)
3163 *pdata |= VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003164 break;
3165 case MSR_IA32_VMX_MISC:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003166 *pdata = vmx_control_msr(
3167 vmx->nested.nested_vmx_misc_low,
3168 vmx->nested.nested_vmx_misc_high);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003169 break;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003170 case MSR_IA32_VMX_CR0_FIXED0:
David Matlack62cc6b9d2016-11-29 18:14:07 -08003171 *pdata = vmx->nested.nested_vmx_cr0_fixed0;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003172 break;
3173 case MSR_IA32_VMX_CR0_FIXED1:
David Matlack62cc6b9d2016-11-29 18:14:07 -08003174 *pdata = vmx->nested.nested_vmx_cr0_fixed1;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003175 break;
3176 case MSR_IA32_VMX_CR4_FIXED0:
David Matlack62cc6b9d2016-11-29 18:14:07 -08003177 *pdata = vmx->nested.nested_vmx_cr4_fixed0;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003178 break;
3179 case MSR_IA32_VMX_CR4_FIXED1:
David Matlack62cc6b9d2016-11-29 18:14:07 -08003180 *pdata = vmx->nested.nested_vmx_cr4_fixed1;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003181 break;
3182 case MSR_IA32_VMX_VMCS_ENUM:
David Matlack62cc6b9d2016-11-29 18:14:07 -08003183 *pdata = vmx->nested.nested_vmx_vmcs_enum;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003184 break;
3185 case MSR_IA32_VMX_PROCBASED_CTLS2:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003186 *pdata = vmx_control_msr(
3187 vmx->nested.nested_vmx_secondary_ctls_low,
3188 vmx->nested.nested_vmx_secondary_ctls_high);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003189 break;
3190 case MSR_IA32_VMX_EPT_VPID_CAP:
Wanpeng Li089d7b62015-10-13 09:18:37 -07003191 *pdata = vmx->nested.nested_vmx_ept_caps |
3192 ((u64)vmx->nested.nested_vmx_vpid_caps << 32);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003193 break;
Bandan Das27c42a12017-08-03 15:54:42 -04003194 case MSR_IA32_VMX_VMFUNC:
3195 *pdata = vmx->nested.nested_vmx_vmfunc_controls;
3196 break;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003197 default:
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003198 return 1;
Nadav Har'Elb3897a42013-07-08 19:12:35 +08003199 }
3200
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003201 return 0;
3202}
3203
Haozhong Zhang37e4c992016-06-22 14:59:55 +08003204static inline bool vmx_feature_control_msr_valid(struct kvm_vcpu *vcpu,
3205 uint64_t val)
3206{
3207 uint64_t valid_bits = to_vmx(vcpu)->msr_ia32_feature_control_valid_bits;
3208
3209 return !(val & ~valid_bits);
3210}
3211
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003212/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08003213 * Reads an msr value (of 'msr_index') into 'pdata'.
3214 * Returns 0 on success, non-0 otherwise.
3215 * Assumes vcpu_load() was already called.
3216 */
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003217static int vmx_get_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003218{
Borislav Petkova6cb0992017-12-20 12:50:28 +01003219 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity26bb0982009-09-07 11:14:12 +03003220 struct shared_msr_entry *msr;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003221
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003222 switch (msr_info->index) {
Avi Kivity05b3e0c2006-12-13 00:33:45 -08003223#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08003224 case MSR_FS_BASE:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003225 msr_info->data = vmcs_readl(GUEST_FS_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003226 break;
3227 case MSR_GS_BASE:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003228 msr_info->data = vmcs_readl(GUEST_GS_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003229 break;
Avi Kivity44ea2b12009-09-06 15:55:37 +03003230 case MSR_KERNEL_GS_BASE:
Borislav Petkova6cb0992017-12-20 12:50:28 +01003231 vmx_load_host_state(vmx);
3232 msr_info->data = vmx->msr_guest_kernel_gs_base;
Avi Kivity44ea2b12009-09-06 15:55:37 +03003233 break;
Avi Kivity26bb0982009-09-07 11:14:12 +03003234#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -08003235 case MSR_EFER:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003236 return kvm_get_msr_common(vcpu, msr_info);
Jaswinder Singh Rajputaf24a4e2009-05-15 18:42:05 +05303237 case MSR_IA32_TSC:
Haozhong Zhangbe7b2632015-10-20 15:39:11 +08003238 msr_info->data = guest_read_tsc(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003239 break;
3240 case MSR_IA32_SYSENTER_CS:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003241 msr_info->data = vmcs_read32(GUEST_SYSENTER_CS);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003242 break;
3243 case MSR_IA32_SYSENTER_EIP:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003244 msr_info->data = vmcs_readl(GUEST_SYSENTER_EIP);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003245 break;
3246 case MSR_IA32_SYSENTER_ESP:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003247 msr_info->data = vmcs_readl(GUEST_SYSENTER_ESP);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003248 break;
Liu, Jinsong0dd376e2014-02-24 10:56:53 +00003249 case MSR_IA32_BNDCFGS:
Haozhong Zhang691bd432017-07-04 10:27:41 +08003250 if (!kvm_mpx_supported() ||
Radim Krčmářd6321d42017-08-05 00:12:49 +02003251 (!msr_info->host_initiated &&
3252 !guest_cpuid_has(vcpu, X86_FEATURE_MPX)))
Paolo Bonzini93c4adc2014-03-05 23:19:52 +01003253 return 1;
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003254 msr_info->data = vmcs_read64(GUEST_BNDCFGS);
Liu, Jinsong0dd376e2014-02-24 10:56:53 +00003255 break;
Ashok Rajc45dcc72016-06-22 14:59:56 +08003256 case MSR_IA32_MCG_EXT_CTL:
3257 if (!msr_info->host_initiated &&
Borislav Petkova6cb0992017-12-20 12:50:28 +01003258 !(vmx->msr_ia32_feature_control &
Ashok Rajc45dcc72016-06-22 14:59:56 +08003259 FEATURE_CONTROL_LMCE))
Jan Kiszkacae50132014-01-04 18:47:22 +01003260 return 1;
Ashok Rajc45dcc72016-06-22 14:59:56 +08003261 msr_info->data = vcpu->arch.mcg_ext_ctl;
3262 break;
Jan Kiszkacae50132014-01-04 18:47:22 +01003263 case MSR_IA32_FEATURE_CONTROL:
Borislav Petkova6cb0992017-12-20 12:50:28 +01003264 msr_info->data = vmx->msr_ia32_feature_control;
Jan Kiszkacae50132014-01-04 18:47:22 +01003265 break;
3266 case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC:
3267 if (!nested_vmx_allowed(vcpu))
3268 return 1;
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003269 return vmx_get_vmx_msr(vcpu, msr_info->index, &msr_info->data);
Wanpeng Li20300092014-12-02 19:14:59 +08003270 case MSR_IA32_XSS:
3271 if (!vmx_xsaves_supported())
3272 return 1;
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003273 msr_info->data = vcpu->arch.ia32_xss;
Wanpeng Li20300092014-12-02 19:14:59 +08003274 break;
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003275 case MSR_TSC_AUX:
Radim Krčmářd6321d42017-08-05 00:12:49 +02003276 if (!msr_info->host_initiated &&
3277 !guest_cpuid_has(vcpu, X86_FEATURE_RDTSCP))
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003278 return 1;
3279 /* Otherwise falls through */
Avi Kivity6aa8b732006-12-10 02:21:36 -08003280 default:
Borislav Petkova6cb0992017-12-20 12:50:28 +01003281 msr = find_msr_entry(vmx, msr_info->index);
Avi Kivity3bab1f52006-12-29 16:49:48 -08003282 if (msr) {
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003283 msr_info->data = msr->data;
Avi Kivity3bab1f52006-12-29 16:49:48 -08003284 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003285 }
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003286 return kvm_get_msr_common(vcpu, msr_info);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003287 }
3288
Avi Kivity6aa8b732006-12-10 02:21:36 -08003289 return 0;
3290}
3291
Jan Kiszkacae50132014-01-04 18:47:22 +01003292static void vmx_leave_nested(struct kvm_vcpu *vcpu);
3293
Avi Kivity6aa8b732006-12-10 02:21:36 -08003294/*
3295 * Writes msr value into into the appropriate "register".
3296 * Returns 0 on success, non-0 otherwise.
3297 * Assumes vcpu_load() was already called.
3298 */
Will Auld8fe8ab42012-11-29 12:42:12 -08003299static int vmx_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003300{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003301 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity26bb0982009-09-07 11:14:12 +03003302 struct shared_msr_entry *msr;
Eddie Dong2cc51562007-05-21 07:28:09 +03003303 int ret = 0;
Will Auld8fe8ab42012-11-29 12:42:12 -08003304 u32 msr_index = msr_info->index;
3305 u64 data = msr_info->data;
Eddie Dong2cc51562007-05-21 07:28:09 +03003306
Avi Kivity6aa8b732006-12-10 02:21:36 -08003307 switch (msr_index) {
Avi Kivity3bab1f52006-12-29 16:49:48 -08003308 case MSR_EFER:
Will Auld8fe8ab42012-11-29 12:42:12 -08003309 ret = kvm_set_msr_common(vcpu, msr_info);
Eddie Dong2cc51562007-05-21 07:28:09 +03003310 break;
Avi Kivity16175a72009-03-23 22:13:44 +02003311#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08003312 case MSR_FS_BASE:
Avi Kivity2fb92db2011-04-27 19:42:18 +03003313 vmx_segment_cache_clear(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003314 vmcs_writel(GUEST_FS_BASE, data);
3315 break;
3316 case MSR_GS_BASE:
Avi Kivity2fb92db2011-04-27 19:42:18 +03003317 vmx_segment_cache_clear(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003318 vmcs_writel(GUEST_GS_BASE, data);
3319 break;
Avi Kivity44ea2b12009-09-06 15:55:37 +03003320 case MSR_KERNEL_GS_BASE:
3321 vmx_load_host_state(vmx);
3322 vmx->msr_guest_kernel_gs_base = data;
3323 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003324#endif
3325 case MSR_IA32_SYSENTER_CS:
3326 vmcs_write32(GUEST_SYSENTER_CS, data);
3327 break;
3328 case MSR_IA32_SYSENTER_EIP:
Avi Kivityf5b42c32007-03-06 12:05:53 +02003329 vmcs_writel(GUEST_SYSENTER_EIP, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003330 break;
3331 case MSR_IA32_SYSENTER_ESP:
Avi Kivityf5b42c32007-03-06 12:05:53 +02003332 vmcs_writel(GUEST_SYSENTER_ESP, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003333 break;
Liu, Jinsong0dd376e2014-02-24 10:56:53 +00003334 case MSR_IA32_BNDCFGS:
Haozhong Zhang691bd432017-07-04 10:27:41 +08003335 if (!kvm_mpx_supported() ||
Radim Krčmářd6321d42017-08-05 00:12:49 +02003336 (!msr_info->host_initiated &&
3337 !guest_cpuid_has(vcpu, X86_FEATURE_MPX)))
Paolo Bonzini93c4adc2014-03-05 23:19:52 +01003338 return 1;
Yu Zhangfd8cb432017-08-24 20:27:56 +08003339 if (is_noncanonical_address(data & PAGE_MASK, vcpu) ||
Jim Mattson45316622017-05-23 11:52:54 -07003340 (data & MSR_IA32_BNDCFGS_RSVD))
Avi Kivity6aa8b732006-12-10 02:21:36 -08003341 return 1;
Sheng Yang468d4722008-10-09 16:01:55 +08003342 vmcs_write64(GUEST_BNDCFGS, data);
3343 break;
3344 case MSR_IA32_TSC:
3345 kvm_write_tsc(vcpu, msr_info);
3346 break;
3347 case MSR_IA32_CR_PAT:
Will Auld8fe8ab42012-11-29 12:42:12 -08003348 if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) {
Nadav Amit45666542014-09-18 22:39:44 +03003349 if (!kvm_mtrr_valid(vcpu, MSR_IA32_CR_PAT, data))
3350 return 1;
Sheng Yang468d4722008-10-09 16:01:55 +08003351 vmcs_write64(GUEST_IA32_PAT, data);
3352 vcpu->arch.pat = data;
3353 break;
3354 }
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003355 ret = kvm_set_msr_common(vcpu, msr_info);
3356 break;
Will Auldba904632012-11-29 12:42:50 -08003357 case MSR_IA32_TSC_ADJUST:
3358 ret = kvm_set_msr_common(vcpu, msr_info);
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003359 break;
Ashok Rajc45dcc72016-06-22 14:59:56 +08003360 case MSR_IA32_MCG_EXT_CTL:
3361 if ((!msr_info->host_initiated &&
3362 !(to_vmx(vcpu)->msr_ia32_feature_control &
3363 FEATURE_CONTROL_LMCE)) ||
3364 (data & ~MCG_EXT_CTL_LMCE_EN))
3365 return 1;
3366 vcpu->arch.mcg_ext_ctl = data;
3367 break;
Jan Kiszkacae50132014-01-04 18:47:22 +01003368 case MSR_IA32_FEATURE_CONTROL:
Haozhong Zhang37e4c992016-06-22 14:59:55 +08003369 if (!vmx_feature_control_msr_valid(vcpu, data) ||
Haozhong Zhang3b840802016-06-22 14:59:54 +08003370 (to_vmx(vcpu)->msr_ia32_feature_control &
Jan Kiszkacae50132014-01-04 18:47:22 +01003371 FEATURE_CONTROL_LOCKED && !msr_info->host_initiated))
3372 return 1;
Haozhong Zhang3b840802016-06-22 14:59:54 +08003373 vmx->msr_ia32_feature_control = data;
Jan Kiszkacae50132014-01-04 18:47:22 +01003374 if (msr_info->host_initiated && data == 0)
3375 vmx_leave_nested(vcpu);
3376 break;
3377 case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC:
David Matlack62cc6b9d2016-11-29 18:14:07 -08003378 if (!msr_info->host_initiated)
3379 return 1; /* they are read-only */
3380 if (!nested_vmx_allowed(vcpu))
3381 return 1;
3382 return vmx_set_vmx_msr(vcpu, msr_index, data);
Wanpeng Li20300092014-12-02 19:14:59 +08003383 case MSR_IA32_XSS:
3384 if (!vmx_xsaves_supported())
3385 return 1;
3386 /*
3387 * The only supported bit as of Skylake is bit 8, but
3388 * it is not supported on KVM.
3389 */
3390 if (data != 0)
3391 return 1;
3392 vcpu->arch.ia32_xss = data;
3393 if (vcpu->arch.ia32_xss != host_xss)
3394 add_atomic_switch_msr(vmx, MSR_IA32_XSS,
3395 vcpu->arch.ia32_xss, host_xss);
3396 else
3397 clear_atomic_switch_msr(vmx, MSR_IA32_XSS);
3398 break;
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003399 case MSR_TSC_AUX:
Radim Krčmářd6321d42017-08-05 00:12:49 +02003400 if (!msr_info->host_initiated &&
3401 !guest_cpuid_has(vcpu, X86_FEATURE_RDTSCP))
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003402 return 1;
3403 /* Check reserved bit, higher 32 bits should be zero */
3404 if ((data >> 32) != 0)
3405 return 1;
3406 /* Otherwise falls through */
Avi Kivity6aa8b732006-12-10 02:21:36 -08003407 default:
Rusty Russell8b9cf982007-07-30 16:31:43 +10003408 msr = find_msr_entry(vmx, msr_index);
Avi Kivity3bab1f52006-12-29 16:49:48 -08003409 if (msr) {
Andy Honig8b3c3102014-08-27 11:16:44 -07003410 u64 old_msr_data = msr->data;
Avi Kivity3bab1f52006-12-29 16:49:48 -08003411 msr->data = data;
Avi Kivity2225fd52012-04-18 15:03:04 +03003412 if (msr - vmx->guest_msrs < vmx->save_nmsrs) {
3413 preempt_disable();
Andy Honig8b3c3102014-08-27 11:16:44 -07003414 ret = kvm_set_shared_msr(msr->index, msr->data,
3415 msr->mask);
Avi Kivity2225fd52012-04-18 15:03:04 +03003416 preempt_enable();
Andy Honig8b3c3102014-08-27 11:16:44 -07003417 if (ret)
3418 msr->data = old_msr_data;
Avi Kivity2225fd52012-04-18 15:03:04 +03003419 }
Avi Kivity3bab1f52006-12-29 16:49:48 -08003420 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003421 }
Will Auld8fe8ab42012-11-29 12:42:12 -08003422 ret = kvm_set_msr_common(vcpu, msr_info);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003423 }
3424
Eddie Dong2cc51562007-05-21 07:28:09 +03003425 return ret;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003426}
3427
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003428static void vmx_cache_reg(struct kvm_vcpu *vcpu, enum kvm_reg reg)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003429{
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003430 __set_bit(reg, (unsigned long *)&vcpu->arch.regs_avail);
3431 switch (reg) {
3432 case VCPU_REGS_RSP:
3433 vcpu->arch.regs[VCPU_REGS_RSP] = vmcs_readl(GUEST_RSP);
3434 break;
3435 case VCPU_REGS_RIP:
3436 vcpu->arch.regs[VCPU_REGS_RIP] = vmcs_readl(GUEST_RIP);
3437 break;
Avi Kivity6de4f3a2009-05-31 22:58:47 +03003438 case VCPU_EXREG_PDPTR:
3439 if (enable_ept)
3440 ept_save_pdptrs(vcpu);
3441 break;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003442 default:
3443 break;
3444 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08003445}
3446
Avi Kivity6aa8b732006-12-10 02:21:36 -08003447static __init int cpu_has_kvm_support(void)
3448{
Eduardo Habkost6210e372008-11-17 19:03:16 -02003449 return cpu_has_vmx();
Avi Kivity6aa8b732006-12-10 02:21:36 -08003450}
3451
3452static __init int vmx_disabled_by_bios(void)
3453{
3454 u64 msr;
3455
3456 rdmsrl(MSR_IA32_FEATURE_CONTROL, msr);
Shane Wangcafd6652010-04-29 12:09:01 -04003457 if (msr & FEATURE_CONTROL_LOCKED) {
Joseph Cihula23f3e992011-02-08 11:45:56 -08003458 /* launched w/ TXT and VMX disabled */
Shane Wangcafd6652010-04-29 12:09:01 -04003459 if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX)
3460 && tboot_enabled())
3461 return 1;
Joseph Cihula23f3e992011-02-08 11:45:56 -08003462 /* launched w/o TXT and VMX only enabled w/ TXT */
Shane Wangcafd6652010-04-29 12:09:01 -04003463 if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX)
Joseph Cihula23f3e992011-02-08 11:45:56 -08003464 && (msr & FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX)
Shane Wangf9335af2010-11-17 11:40:17 +08003465 && !tboot_enabled()) {
3466 printk(KERN_WARNING "kvm: disable TXT in the BIOS or "
Joseph Cihula23f3e992011-02-08 11:45:56 -08003467 "activate TXT before enabling KVM\n");
Shane Wangcafd6652010-04-29 12:09:01 -04003468 return 1;
Shane Wangf9335af2010-11-17 11:40:17 +08003469 }
Joseph Cihula23f3e992011-02-08 11:45:56 -08003470 /* launched w/o TXT and VMX disabled */
3471 if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX)
3472 && !tboot_enabled())
3473 return 1;
Shane Wangcafd6652010-04-29 12:09:01 -04003474 }
3475
3476 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003477}
3478
Dongxiao Xu7725b892010-05-11 18:29:38 +08003479static void kvm_cpu_vmxon(u64 addr)
3480{
David Hildenbrandfe0e80b2017-03-10 12:47:13 +01003481 cr4_set_bits(X86_CR4_VMXE);
Alexander Shishkin1c5ac212016-03-29 17:43:10 +03003482 intel_pt_handle_vmx(1);
3483
Dongxiao Xu7725b892010-05-11 18:29:38 +08003484 asm volatile (ASM_VMX_VMXON_RAX
3485 : : "a"(&addr), "m"(addr)
3486 : "memory", "cc");
3487}
3488
Radim Krčmář13a34e02014-08-28 15:13:03 +02003489static int hardware_enable(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003490{
3491 int cpu = raw_smp_processor_id();
3492 u64 phys_addr = __pa(per_cpu(vmxarea, cpu));
Shane Wangcafd6652010-04-29 12:09:01 -04003493 u64 old, test_bits;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003494
Andy Lutomirski1e02ce42014-10-24 15:58:08 -07003495 if (cr4_read_shadow() & X86_CR4_VMXE)
Alexander Graf10474ae2009-09-15 11:37:46 +02003496 return -EBUSY;
3497
Nadav Har'Eld462b812011-05-24 15:26:10 +03003498 INIT_LIST_HEAD(&per_cpu(loaded_vmcss_on_cpu, cpu));
Feng Wubf9f6ac2015-09-18 22:29:55 +08003499 INIT_LIST_HEAD(&per_cpu(blocked_vcpu_on_cpu, cpu));
3500 spin_lock_init(&per_cpu(blocked_vcpu_on_cpu_lock, cpu));
Zhang Yanfei8f536b72012-12-06 23:43:34 +08003501
3502 /*
3503 * Now we can enable the vmclear operation in kdump
3504 * since the loaded_vmcss_on_cpu list on this cpu
3505 * has been initialized.
3506 *
3507 * Though the cpu is not in VMX operation now, there
3508 * is no problem to enable the vmclear operation
3509 * for the loaded_vmcss_on_cpu list is empty!
3510 */
3511 crash_enable_local_vmclear(cpu);
3512
Avi Kivity6aa8b732006-12-10 02:21:36 -08003513 rdmsrl(MSR_IA32_FEATURE_CONTROL, old);
Shane Wangcafd6652010-04-29 12:09:01 -04003514
3515 test_bits = FEATURE_CONTROL_LOCKED;
3516 test_bits |= FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
3517 if (tboot_enabled())
3518 test_bits |= FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX;
3519
3520 if ((old & test_bits) != test_bits) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003521 /* enable and lock */
Shane Wangcafd6652010-04-29 12:09:01 -04003522 wrmsrl(MSR_IA32_FEATURE_CONTROL, old | test_bits);
3523 }
David Hildenbrandfe0e80b2017-03-10 12:47:13 +01003524 kvm_cpu_vmxon(phys_addr);
David Hildenbrandfdf288b2017-08-24 20:51:29 +02003525 if (enable_ept)
3526 ept_sync_global();
Alexander Graf10474ae2009-09-15 11:37:46 +02003527
3528 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003529}
3530
Nadav Har'Eld462b812011-05-24 15:26:10 +03003531static void vmclear_local_loaded_vmcss(void)
Avi Kivity543e4242008-05-13 16:22:47 +03003532{
3533 int cpu = raw_smp_processor_id();
Nadav Har'Eld462b812011-05-24 15:26:10 +03003534 struct loaded_vmcs *v, *n;
Avi Kivity543e4242008-05-13 16:22:47 +03003535
Nadav Har'Eld462b812011-05-24 15:26:10 +03003536 list_for_each_entry_safe(v, n, &per_cpu(loaded_vmcss_on_cpu, cpu),
3537 loaded_vmcss_on_cpu_link)
3538 __loaded_vmcs_clear(v);
Avi Kivity543e4242008-05-13 16:22:47 +03003539}
3540
Eduardo Habkost710ff4a2008-11-17 19:03:18 -02003541
3542/* Just like cpu_vmxoff(), but with the __kvm_handle_fault_on_reboot()
3543 * tricks.
3544 */
3545static void kvm_cpu_vmxoff(void)
3546{
3547 asm volatile (__ex(ASM_VMX_VMXOFF) : : : "cc");
Alexander Shishkin1c5ac212016-03-29 17:43:10 +03003548
3549 intel_pt_handle_vmx(0);
David Hildenbrandfe0e80b2017-03-10 12:47:13 +01003550 cr4_clear_bits(X86_CR4_VMXE);
Eduardo Habkost710ff4a2008-11-17 19:03:18 -02003551}
3552
Radim Krčmář13a34e02014-08-28 15:13:03 +02003553static void hardware_disable(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003554{
David Hildenbrandfe0e80b2017-03-10 12:47:13 +01003555 vmclear_local_loaded_vmcss();
3556 kvm_cpu_vmxoff();
Avi Kivity6aa8b732006-12-10 02:21:36 -08003557}
3558
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003559static __init int adjust_vmx_controls(u32 ctl_min, u32 ctl_opt,
Mike Dayd77c26f2007-10-08 09:02:08 -04003560 u32 msr, u32 *result)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003561{
3562 u32 vmx_msr_low, vmx_msr_high;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003563 u32 ctl = ctl_min | ctl_opt;
3564
3565 rdmsr(msr, vmx_msr_low, vmx_msr_high);
3566
3567 ctl &= vmx_msr_high; /* bit == 0 in high word ==> must be zero */
3568 ctl |= vmx_msr_low; /* bit == 1 in low word ==> must be one */
3569
3570 /* Ensure minimum (required) set of control bits are supported. */
3571 if (ctl_min & ~ctl)
Yang, Sheng002c7f72007-07-31 14:23:01 +03003572 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003573
3574 *result = ctl;
3575 return 0;
3576}
3577
Avi Kivity110312c2010-12-21 12:54:20 +02003578static __init bool allow_1_setting(u32 msr, u32 ctl)
3579{
3580 u32 vmx_msr_low, vmx_msr_high;
3581
3582 rdmsr(msr, vmx_msr_low, vmx_msr_high);
3583 return vmx_msr_high & ctl;
3584}
3585
Yang, Sheng002c7f72007-07-31 14:23:01 +03003586static __init int setup_vmcs_config(struct vmcs_config *vmcs_conf)
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003587{
3588 u32 vmx_msr_low, vmx_msr_high;
Sheng Yangd56f5462008-04-25 10:13:16 +08003589 u32 min, opt, min2, opt2;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003590 u32 _pin_based_exec_control = 0;
3591 u32 _cpu_based_exec_control = 0;
Sheng Yangf78e0e22007-10-29 09:40:42 +08003592 u32 _cpu_based_2nd_exec_control = 0;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003593 u32 _vmexit_control = 0;
3594 u32 _vmentry_control = 0;
3595
Raghavendra K T10166742012-02-07 23:19:20 +05303596 min = CPU_BASED_HLT_EXITING |
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003597#ifdef CONFIG_X86_64
3598 CPU_BASED_CR8_LOAD_EXITING |
3599 CPU_BASED_CR8_STORE_EXITING |
3600#endif
Sheng Yangd56f5462008-04-25 10:13:16 +08003601 CPU_BASED_CR3_LOAD_EXITING |
3602 CPU_BASED_CR3_STORE_EXITING |
Quan Xu8eb73e22017-12-12 16:44:21 +08003603 CPU_BASED_UNCOND_IO_EXITING |
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003604 CPU_BASED_MOV_DR_EXITING |
Marcelo Tosattia7052892008-09-23 13:18:35 -03003605 CPU_BASED_USE_TSC_OFFSETING |
Avi Kivityfee84b02011-11-10 14:57:25 +02003606 CPU_BASED_INVLPG_EXITING |
3607 CPU_BASED_RDPMC_EXITING;
Anthony Liguori443381a2010-12-06 10:53:38 -06003608
Michael S. Tsirkin668fffa2017-04-21 12:27:17 +02003609 if (!kvm_mwait_in_guest())
3610 min |= CPU_BASED_MWAIT_EXITING |
3611 CPU_BASED_MONITOR_EXITING;
3612
Sheng Yangf78e0e22007-10-29 09:40:42 +08003613 opt = CPU_BASED_TPR_SHADOW |
Sheng Yang25c5f222008-03-28 13:18:56 +08003614 CPU_BASED_USE_MSR_BITMAPS |
Sheng Yangf78e0e22007-10-29 09:40:42 +08003615 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003616 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PROCBASED_CTLS,
3617 &_cpu_based_exec_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03003618 return -EIO;
Yang, Sheng6e5d8652007-09-12 18:03:11 +08003619#ifdef CONFIG_X86_64
3620 if ((_cpu_based_exec_control & CPU_BASED_TPR_SHADOW))
3621 _cpu_based_exec_control &= ~CPU_BASED_CR8_LOAD_EXITING &
3622 ~CPU_BASED_CR8_STORE_EXITING;
3623#endif
Sheng Yangf78e0e22007-10-29 09:40:42 +08003624 if (_cpu_based_exec_control & CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) {
Sheng Yangd56f5462008-04-25 10:13:16 +08003625 min2 = 0;
3626 opt2 = SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
Yang Zhang8d146952013-01-25 10:18:50 +08003627 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
Sheng Yang2384d2b2008-01-17 15:14:33 +08003628 SECONDARY_EXEC_WBINVD_EXITING |
Sheng Yangd56f5462008-04-25 10:13:16 +08003629 SECONDARY_EXEC_ENABLE_VPID |
Nitin A Kamble3a624e22009-06-08 11:34:16 -07003630 SECONDARY_EXEC_ENABLE_EPT |
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08003631 SECONDARY_EXEC_UNRESTRICTED_GUEST |
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003632 SECONDARY_EXEC_PAUSE_LOOP_EXITING |
Paolo Bonzini0367f202016-07-12 10:44:55 +02003633 SECONDARY_EXEC_DESC |
Mao, Junjiead756a12012-07-02 01:18:48 +00003634 SECONDARY_EXEC_RDTSCP |
Yang Zhang83d4c282013-01-25 10:18:49 +08003635 SECONDARY_EXEC_ENABLE_INVPCID |
Yang Zhangc7c9c562013-01-25 10:18:51 +08003636 SECONDARY_EXEC_APIC_REGISTER_VIRT |
Abel Gordonabc4fc52013-04-18 14:35:25 +03003637 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY |
Wanpeng Li20300092014-12-02 19:14:59 +08003638 SECONDARY_EXEC_SHADOW_VMCS |
Kai Huang843e4332015-01-28 10:54:28 +08003639 SECONDARY_EXEC_XSAVES |
David Hildenbrand736fdf72017-08-24 20:51:37 +02003640 SECONDARY_EXEC_RDSEED_EXITING |
3641 SECONDARY_EXEC_RDRAND_EXITING |
Xiao Guangrong8b3e34e2015-09-09 14:05:51 +08003642 SECONDARY_EXEC_ENABLE_PML |
Bandan Das2a499e42017-08-03 15:54:41 -04003643 SECONDARY_EXEC_TSC_SCALING |
3644 SECONDARY_EXEC_ENABLE_VMFUNC;
Sheng Yangd56f5462008-04-25 10:13:16 +08003645 if (adjust_vmx_controls(min2, opt2,
3646 MSR_IA32_VMX_PROCBASED_CTLS2,
Sheng Yangf78e0e22007-10-29 09:40:42 +08003647 &_cpu_based_2nd_exec_control) < 0)
3648 return -EIO;
3649 }
3650#ifndef CONFIG_X86_64
3651 if (!(_cpu_based_2nd_exec_control &
3652 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES))
3653 _cpu_based_exec_control &= ~CPU_BASED_TPR_SHADOW;
3654#endif
Yang Zhang83d4c282013-01-25 10:18:49 +08003655
3656 if (!(_cpu_based_exec_control & CPU_BASED_TPR_SHADOW))
3657 _cpu_based_2nd_exec_control &= ~(
Yang Zhang8d146952013-01-25 10:18:50 +08003658 SECONDARY_EXEC_APIC_REGISTER_VIRT |
Yang Zhangc7c9c562013-01-25 10:18:51 +08003659 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
3660 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
Yang Zhang83d4c282013-01-25 10:18:49 +08003661
Wanpeng Li61f1dd92017-10-18 16:02:19 -07003662 rdmsr_safe(MSR_IA32_VMX_EPT_VPID_CAP,
3663 &vmx_capability.ept, &vmx_capability.vpid);
3664
Sheng Yangd56f5462008-04-25 10:13:16 +08003665 if (_cpu_based_2nd_exec_control & SECONDARY_EXEC_ENABLE_EPT) {
Marcelo Tosattia7052892008-09-23 13:18:35 -03003666 /* CR3 accesses and invlpg don't need to cause VM Exits when EPT
3667 enabled */
Gleb Natapov5fff7d22009-08-27 18:41:30 +03003668 _cpu_based_exec_control &= ~(CPU_BASED_CR3_LOAD_EXITING |
3669 CPU_BASED_CR3_STORE_EXITING |
3670 CPU_BASED_INVLPG_EXITING);
Wanpeng Li61f1dd92017-10-18 16:02:19 -07003671 } else if (vmx_capability.ept) {
3672 vmx_capability.ept = 0;
3673 pr_warn_once("EPT CAP should not exist if not support "
3674 "1-setting enable EPT VM-execution control\n");
3675 }
3676 if (!(_cpu_based_2nd_exec_control & SECONDARY_EXEC_ENABLE_VPID) &&
3677 vmx_capability.vpid) {
3678 vmx_capability.vpid = 0;
3679 pr_warn_once("VPID CAP should not exist if not support "
3680 "1-setting enable VPID VM-execution control\n");
Sheng Yangd56f5462008-04-25 10:13:16 +08003681 }
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003682
Paolo Bonzini91fa0f82016-06-15 20:55:08 +02003683 min = VM_EXIT_SAVE_DEBUG_CONTROLS | VM_EXIT_ACK_INTR_ON_EXIT;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003684#ifdef CONFIG_X86_64
3685 min |= VM_EXIT_HOST_ADDR_SPACE_SIZE;
3686#endif
Yang Zhanga547c6d2013-04-11 19:25:10 +08003687 opt = VM_EXIT_SAVE_IA32_PAT | VM_EXIT_LOAD_IA32_PAT |
Paolo Bonzini91fa0f82016-06-15 20:55:08 +02003688 VM_EXIT_CLEAR_BNDCFGS;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003689 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_EXIT_CTLS,
3690 &_vmexit_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03003691 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003692
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01003693 min = PIN_BASED_EXT_INTR_MASK | PIN_BASED_NMI_EXITING;
3694 opt = PIN_BASED_VIRTUAL_NMIS | PIN_BASED_POSTED_INTR |
3695 PIN_BASED_VMX_PREEMPTION_TIMER;
Yang Zhang01e439b2013-04-11 19:25:12 +08003696 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PINBASED_CTLS,
3697 &_pin_based_exec_control) < 0)
3698 return -EIO;
3699
Paolo Bonzini1c17c3e2016-07-08 11:53:38 +02003700 if (cpu_has_broken_vmx_preemption_timer())
3701 _pin_based_exec_control &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
Yang Zhang01e439b2013-04-11 19:25:12 +08003702 if (!(_cpu_based_2nd_exec_control &
Paolo Bonzini91fa0f82016-06-15 20:55:08 +02003703 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY))
Yang Zhang01e439b2013-04-11 19:25:12 +08003704 _pin_based_exec_control &= ~PIN_BASED_POSTED_INTR;
3705
Paolo Bonzinic845f9c2014-02-21 10:55:44 +01003706 min = VM_ENTRY_LOAD_DEBUG_CONTROLS;
Liu, Jinsongda8999d2014-02-24 10:55:46 +00003707 opt = VM_ENTRY_LOAD_IA32_PAT | VM_ENTRY_LOAD_BNDCFGS;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003708 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_ENTRY_CTLS,
3709 &_vmentry_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03003710 return -EIO;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003711
Nguyen Anh Quynhc68876f2006-12-29 16:49:54 -08003712 rdmsr(MSR_IA32_VMX_BASIC, vmx_msr_low, vmx_msr_high);
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003713
3714 /* IA-32 SDM Vol 3B: VMCS size is never greater than 4kB. */
3715 if ((vmx_msr_high & 0x1fff) > PAGE_SIZE)
Yang, Sheng002c7f72007-07-31 14:23:01 +03003716 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003717
3718#ifdef CONFIG_X86_64
3719 /* IA-32 SDM Vol 3B: 64-bit CPUs always have VMX_BASIC_MSR[48]==0. */
3720 if (vmx_msr_high & (1u<<16))
Yang, Sheng002c7f72007-07-31 14:23:01 +03003721 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003722#endif
3723
3724 /* Require Write-Back (WB) memory type for VMCS accesses. */
3725 if (((vmx_msr_high >> 18) & 15) != 6)
Yang, Sheng002c7f72007-07-31 14:23:01 +03003726 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003727
Yang, Sheng002c7f72007-07-31 14:23:01 +03003728 vmcs_conf->size = vmx_msr_high & 0x1fff;
Paolo Bonzini16cb0252016-09-05 15:57:00 +02003729 vmcs_conf->order = get_order(vmcs_conf->size);
Jan Dakinevich9ac7e3e2016-09-04 21:23:15 +03003730 vmcs_conf->basic_cap = vmx_msr_high & ~0x1fff;
Yang, Sheng002c7f72007-07-31 14:23:01 +03003731 vmcs_conf->revision_id = vmx_msr_low;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003732
Yang, Sheng002c7f72007-07-31 14:23:01 +03003733 vmcs_conf->pin_based_exec_ctrl = _pin_based_exec_control;
3734 vmcs_conf->cpu_based_exec_ctrl = _cpu_based_exec_control;
Sheng Yangf78e0e22007-10-29 09:40:42 +08003735 vmcs_conf->cpu_based_2nd_exec_ctrl = _cpu_based_2nd_exec_control;
Yang, Sheng002c7f72007-07-31 14:23:01 +03003736 vmcs_conf->vmexit_ctrl = _vmexit_control;
3737 vmcs_conf->vmentry_ctrl = _vmentry_control;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003738
Avi Kivity110312c2010-12-21 12:54:20 +02003739 cpu_has_load_ia32_efer =
3740 allow_1_setting(MSR_IA32_VMX_ENTRY_CTLS,
3741 VM_ENTRY_LOAD_IA32_EFER)
3742 && allow_1_setting(MSR_IA32_VMX_EXIT_CTLS,
3743 VM_EXIT_LOAD_IA32_EFER);
3744
Gleb Natapov8bf00a52011-10-05 14:01:22 +02003745 cpu_has_load_perf_global_ctrl =
3746 allow_1_setting(MSR_IA32_VMX_ENTRY_CTLS,
3747 VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL)
3748 && allow_1_setting(MSR_IA32_VMX_EXIT_CTLS,
3749 VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL);
3750
3751 /*
3752 * Some cpus support VM_ENTRY_(LOAD|SAVE)_IA32_PERF_GLOBAL_CTRL
Andrea Gelminibb3541f2016-05-21 14:14:44 +02003753 * but due to errata below it can't be used. Workaround is to use
Gleb Natapov8bf00a52011-10-05 14:01:22 +02003754 * msr load mechanism to switch IA32_PERF_GLOBAL_CTRL.
3755 *
3756 * VM Exit May Incorrectly Clear IA32_PERF_GLOBAL_CTRL [34:32]
3757 *
3758 * AAK155 (model 26)
3759 * AAP115 (model 30)
3760 * AAT100 (model 37)
3761 * BC86,AAY89,BD102 (model 44)
3762 * BA97 (model 46)
3763 *
3764 */
3765 if (cpu_has_load_perf_global_ctrl && boot_cpu_data.x86 == 0x6) {
3766 switch (boot_cpu_data.x86_model) {
3767 case 26:
3768 case 30:
3769 case 37:
3770 case 44:
3771 case 46:
3772 cpu_has_load_perf_global_ctrl = false;
3773 printk_once(KERN_WARNING"kvm: VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL "
3774 "does not work properly. Using workaround\n");
3775 break;
3776 default:
3777 break;
3778 }
3779 }
3780
Borislav Petkov782511b2016-04-04 22:25:03 +02003781 if (boot_cpu_has(X86_FEATURE_XSAVES))
Wanpeng Li20300092014-12-02 19:14:59 +08003782 rdmsrl(MSR_IA32_XSS, host_xss);
3783
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003784 return 0;
Nguyen Anh Quynhc68876f2006-12-29 16:49:54 -08003785}
Avi Kivity6aa8b732006-12-10 02:21:36 -08003786
3787static struct vmcs *alloc_vmcs_cpu(int cpu)
3788{
3789 int node = cpu_to_node(cpu);
3790 struct page *pages;
3791 struct vmcs *vmcs;
3792
Vlastimil Babka96db8002015-09-08 15:03:50 -07003793 pages = __alloc_pages_node(node, GFP_KERNEL, vmcs_config.order);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003794 if (!pages)
3795 return NULL;
3796 vmcs = page_address(pages);
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003797 memset(vmcs, 0, vmcs_config.size);
3798 vmcs->revision_id = vmcs_config.revision_id; /* vmcs revision id */
Avi Kivity6aa8b732006-12-10 02:21:36 -08003799 return vmcs;
3800}
3801
3802static struct vmcs *alloc_vmcs(void)
3803{
Ingo Molnard3b2c332007-01-05 16:36:23 -08003804 return alloc_vmcs_cpu(raw_smp_processor_id());
Avi Kivity6aa8b732006-12-10 02:21:36 -08003805}
3806
3807static void free_vmcs(struct vmcs *vmcs)
3808{
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003809 free_pages((unsigned long)vmcs, vmcs_config.order);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003810}
3811
Nadav Har'Eld462b812011-05-24 15:26:10 +03003812/*
3813 * Free a VMCS, but before that VMCLEAR it on the CPU where it was last loaded
3814 */
3815static void free_loaded_vmcs(struct loaded_vmcs *loaded_vmcs)
3816{
3817 if (!loaded_vmcs->vmcs)
3818 return;
3819 loaded_vmcs_clear(loaded_vmcs);
3820 free_vmcs(loaded_vmcs->vmcs);
3821 loaded_vmcs->vmcs = NULL;
Jim Mattson355f4fb2016-10-28 08:29:39 -07003822 WARN_ON(loaded_vmcs->shadow_vmcs != NULL);
Nadav Har'Eld462b812011-05-24 15:26:10 +03003823}
3824
Mark Kanda276c7962017-11-27 17:22:26 -06003825static void vmx_nested_free_vmcs02(struct vcpu_vmx *vmx)
3826{
3827 struct loaded_vmcs *loaded_vmcs = &vmx->nested.vmcs02;
3828
3829 /*
3830 * Just leak the VMCS02 if the WARN triggers. Better than
3831 * a use-after-free.
3832 */
3833 if (WARN_ON(vmx->loaded_vmcs == loaded_vmcs))
3834 return;
3835 free_loaded_vmcs(loaded_vmcs);
3836}
3837
Sam Ravnborg39959582007-06-01 00:47:13 -07003838static void free_kvm_area(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003839{
3840 int cpu;
3841
Zachary Amsden3230bb42009-09-29 11:38:37 -10003842 for_each_possible_cpu(cpu) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003843 free_vmcs(per_cpu(vmxarea, cpu));
Zachary Amsden3230bb42009-09-29 11:38:37 -10003844 per_cpu(vmxarea, cpu) = NULL;
3845 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08003846}
3847
Jim Mattsond37f4262017-12-22 12:12:16 -08003848enum vmcs_field_width {
3849 VMCS_FIELD_WIDTH_U16 = 0,
3850 VMCS_FIELD_WIDTH_U64 = 1,
3851 VMCS_FIELD_WIDTH_U32 = 2,
3852 VMCS_FIELD_WIDTH_NATURAL_WIDTH = 3
Jim Mattson85fd5142017-07-07 12:51:41 -07003853};
3854
Jim Mattsond37f4262017-12-22 12:12:16 -08003855static inline int vmcs_field_width(unsigned long field)
Jim Mattson85fd5142017-07-07 12:51:41 -07003856{
3857 if (0x1 & field) /* the *_HIGH fields are all 32 bit */
Jim Mattsond37f4262017-12-22 12:12:16 -08003858 return VMCS_FIELD_WIDTH_U32;
Jim Mattson85fd5142017-07-07 12:51:41 -07003859 return (field >> 13) & 0x3 ;
3860}
3861
3862static inline int vmcs_field_readonly(unsigned long field)
3863{
3864 return (((field >> 10) & 0x3) == 1);
3865}
3866
Bandan Dasfe2b2012014-04-21 15:20:14 -04003867static void init_vmcs_shadow_fields(void)
3868{
3869 int i, j;
3870
Paolo Bonzini44900ba2017-12-13 12:58:02 +01003871 for (i = j = 0; i < max_shadow_read_only_fields; i++) {
3872 u16 field = shadow_read_only_fields[i];
Jim Mattsond37f4262017-12-22 12:12:16 -08003873 if (vmcs_field_width(field) == VMCS_FIELD_WIDTH_U64 &&
Paolo Bonzini44900ba2017-12-13 12:58:02 +01003874 (i + 1 == max_shadow_read_only_fields ||
3875 shadow_read_only_fields[i + 1] != field + 1))
3876 pr_err("Missing field from shadow_read_only_field %x\n",
3877 field + 1);
3878
3879 clear_bit(field, vmx_vmread_bitmap);
3880#ifdef CONFIG_X86_64
3881 if (field & 1)
3882 continue;
3883#endif
3884 if (j < i)
3885 shadow_read_only_fields[j] = field;
3886 j++;
3887 }
3888 max_shadow_read_only_fields = j;
Bandan Dasfe2b2012014-04-21 15:20:14 -04003889
3890 for (i = j = 0; i < max_shadow_read_write_fields; i++) {
Paolo Bonzini44900ba2017-12-13 12:58:02 +01003891 u16 field = shadow_read_write_fields[i];
Jim Mattsond37f4262017-12-22 12:12:16 -08003892 if (vmcs_field_width(field) == VMCS_FIELD_WIDTH_U64 &&
Paolo Bonzini44900ba2017-12-13 12:58:02 +01003893 (i + 1 == max_shadow_read_write_fields ||
3894 shadow_read_write_fields[i + 1] != field + 1))
3895 pr_err("Missing field from shadow_read_write_field %x\n",
3896 field + 1);
3897
Paolo Bonzinic5d167b2017-12-13 11:05:19 +01003898 /*
3899 * PML and the preemption timer can be emulated, but the
3900 * processor cannot vmwrite to fields that don't exist
3901 * on bare metal.
3902 */
Paolo Bonzini44900ba2017-12-13 12:58:02 +01003903 switch (field) {
Paolo Bonzinic5d167b2017-12-13 11:05:19 +01003904 case GUEST_PML_INDEX:
3905 if (!cpu_has_vmx_pml())
3906 continue;
3907 break;
3908 case VMX_PREEMPTION_TIMER_VALUE:
3909 if (!cpu_has_vmx_preemption_timer())
3910 continue;
3911 break;
3912 case GUEST_INTR_STATUS:
3913 if (!cpu_has_vmx_apicv())
Bandan Dasfe2b2012014-04-21 15:20:14 -04003914 continue;
3915 break;
3916 default:
3917 break;
3918 }
3919
Paolo Bonzini44900ba2017-12-13 12:58:02 +01003920 clear_bit(field, vmx_vmwrite_bitmap);
3921 clear_bit(field, vmx_vmread_bitmap);
3922#ifdef CONFIG_X86_64
3923 if (field & 1)
3924 continue;
3925#endif
Bandan Dasfe2b2012014-04-21 15:20:14 -04003926 if (j < i)
Paolo Bonzini44900ba2017-12-13 12:58:02 +01003927 shadow_read_write_fields[j] = field;
Bandan Dasfe2b2012014-04-21 15:20:14 -04003928 j++;
3929 }
3930 max_shadow_read_write_fields = j;
Bandan Dasfe2b2012014-04-21 15:20:14 -04003931}
3932
Avi Kivity6aa8b732006-12-10 02:21:36 -08003933static __init int alloc_kvm_area(void)
3934{
3935 int cpu;
3936
Zachary Amsden3230bb42009-09-29 11:38:37 -10003937 for_each_possible_cpu(cpu) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003938 struct vmcs *vmcs;
3939
3940 vmcs = alloc_vmcs_cpu(cpu);
3941 if (!vmcs) {
3942 free_kvm_area();
3943 return -ENOMEM;
3944 }
3945
3946 per_cpu(vmxarea, cpu) = vmcs;
3947 }
3948 return 0;
3949}
3950
Gleb Natapov91b0aa22013-01-21 15:36:47 +02003951static void fix_pmode_seg(struct kvm_vcpu *vcpu, int seg,
Gleb Natapovd99e4152012-12-20 16:57:45 +02003952 struct kvm_segment *save)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003953{
Gleb Natapovd99e4152012-12-20 16:57:45 +02003954 if (!emulate_invalid_guest_state) {
3955 /*
3956 * CS and SS RPL should be equal during guest entry according
3957 * to VMX spec, but in reality it is not always so. Since vcpu
3958 * is in the middle of the transition from real mode to
3959 * protected mode it is safe to assume that RPL 0 is a good
3960 * default value.
3961 */
3962 if (seg == VCPU_SREG_CS || seg == VCPU_SREG_SS)
Nadav Amitb32a9912015-03-29 16:33:04 +03003963 save->selector &= ~SEGMENT_RPL_MASK;
3964 save->dpl = save->selector & SEGMENT_RPL_MASK;
Gleb Natapovd99e4152012-12-20 16:57:45 +02003965 save->s = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003966 }
Gleb Natapovd99e4152012-12-20 16:57:45 +02003967 vmx_set_segment(vcpu, save, seg);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003968}
3969
3970static void enter_pmode(struct kvm_vcpu *vcpu)
3971{
3972 unsigned long flags;
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03003973 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003974
Gleb Natapovd99e4152012-12-20 16:57:45 +02003975 /*
3976 * Update real mode segment cache. It may be not up-to-date if sement
3977 * register was written while vcpu was in a guest mode.
3978 */
3979 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_ES], VCPU_SREG_ES);
3980 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_DS], VCPU_SREG_DS);
3981 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_FS], VCPU_SREG_FS);
3982 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_GS], VCPU_SREG_GS);
3983 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_SS], VCPU_SREG_SS);
3984 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_CS], VCPU_SREG_CS);
3985
Avi Kivity7ffd92c2009-06-09 14:10:45 +03003986 vmx->rmode.vm86_active = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003987
Avi Kivity2fb92db2011-04-27 19:42:18 +03003988 vmx_segment_cache_clear(vmx);
3989
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03003990 vmx_set_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_TR], VCPU_SREG_TR);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003991
3992 flags = vmcs_readl(GUEST_RFLAGS);
Avi Kivity78ac8b42010-04-08 18:19:35 +03003993 flags &= RMODE_GUEST_OWNED_EFLAGS_BITS;
3994 flags |= vmx->rmode.save_rflags & ~RMODE_GUEST_OWNED_EFLAGS_BITS;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003995 vmcs_writel(GUEST_RFLAGS, flags);
3996
Rusty Russell66aee912007-07-17 23:34:16 +10003997 vmcs_writel(GUEST_CR4, (vmcs_readl(GUEST_CR4) & ~X86_CR4_VME) |
3998 (vmcs_readl(CR4_READ_SHADOW) & X86_CR4_VME));
Avi Kivity6aa8b732006-12-10 02:21:36 -08003999
4000 update_exception_bitmap(vcpu);
4001
Gleb Natapov91b0aa22013-01-21 15:36:47 +02004002 fix_pmode_seg(vcpu, VCPU_SREG_CS, &vmx->rmode.segs[VCPU_SREG_CS]);
4003 fix_pmode_seg(vcpu, VCPU_SREG_SS, &vmx->rmode.segs[VCPU_SREG_SS]);
4004 fix_pmode_seg(vcpu, VCPU_SREG_ES, &vmx->rmode.segs[VCPU_SREG_ES]);
4005 fix_pmode_seg(vcpu, VCPU_SREG_DS, &vmx->rmode.segs[VCPU_SREG_DS]);
4006 fix_pmode_seg(vcpu, VCPU_SREG_FS, &vmx->rmode.segs[VCPU_SREG_FS]);
4007 fix_pmode_seg(vcpu, VCPU_SREG_GS, &vmx->rmode.segs[VCPU_SREG_GS]);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004008}
4009
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03004010static void fix_rmode_seg(int seg, struct kvm_segment *save)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004011{
Mathias Krause772e0312012-08-30 01:30:19 +02004012 const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
Gleb Natapovd99e4152012-12-20 16:57:45 +02004013 struct kvm_segment var = *save;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004014
Gleb Natapovd99e4152012-12-20 16:57:45 +02004015 var.dpl = 0x3;
4016 if (seg == VCPU_SREG_CS)
4017 var.type = 0x3;
4018
4019 if (!emulate_invalid_guest_state) {
4020 var.selector = var.base >> 4;
4021 var.base = var.base & 0xffff0;
4022 var.limit = 0xffff;
4023 var.g = 0;
4024 var.db = 0;
4025 var.present = 1;
4026 var.s = 1;
4027 var.l = 0;
4028 var.unusable = 0;
4029 var.type = 0x3;
4030 var.avl = 0;
4031 if (save->base & 0xf)
4032 printk_once(KERN_WARNING "kvm: segment base is not "
4033 "paragraph aligned when entering "
4034 "protected mode (seg=%d)", seg);
4035 }
4036
4037 vmcs_write16(sf->selector, var.selector);
Chao Peng96794e42017-02-21 03:50:01 -05004038 vmcs_writel(sf->base, var.base);
Gleb Natapovd99e4152012-12-20 16:57:45 +02004039 vmcs_write32(sf->limit, var.limit);
4040 vmcs_write32(sf->ar_bytes, vmx_segment_access_rights(&var));
Avi Kivity6aa8b732006-12-10 02:21:36 -08004041}
4042
4043static void enter_rmode(struct kvm_vcpu *vcpu)
4044{
4045 unsigned long flags;
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03004046 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004047
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03004048 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_TR], VCPU_SREG_TR);
4049 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_ES], VCPU_SREG_ES);
4050 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_DS], VCPU_SREG_DS);
4051 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_FS], VCPU_SREG_FS);
4052 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_GS], VCPU_SREG_GS);
Gleb Natapovc6ad11532012-12-12 19:10:51 +02004053 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_SS], VCPU_SREG_SS);
4054 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_CS], VCPU_SREG_CS);
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03004055
Avi Kivity7ffd92c2009-06-09 14:10:45 +03004056 vmx->rmode.vm86_active = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004057
Gleb Natapov776e58e2011-03-13 12:34:27 +02004058 /*
4059 * Very old userspace does not call KVM_SET_TSS_ADDR before entering
Jan Kiszka4918c6c2013-03-15 08:38:56 +01004060 * vcpu. Warn the user that an update is overdue.
Gleb Natapov776e58e2011-03-13 12:34:27 +02004061 */
Jan Kiszka4918c6c2013-03-15 08:38:56 +01004062 if (!vcpu->kvm->arch.tss_addr)
Gleb Natapov776e58e2011-03-13 12:34:27 +02004063 printk_once(KERN_WARNING "kvm: KVM_SET_TSS_ADDR need to be "
4064 "called before entering vcpu\n");
Gleb Natapov776e58e2011-03-13 12:34:27 +02004065
Avi Kivity2fb92db2011-04-27 19:42:18 +03004066 vmx_segment_cache_clear(vmx);
4067
Jan Kiszka4918c6c2013-03-15 08:38:56 +01004068 vmcs_writel(GUEST_TR_BASE, vcpu->kvm->arch.tss_addr);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004069 vmcs_write32(GUEST_TR_LIMIT, RMODE_TSS_SIZE - 1);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004070 vmcs_write32(GUEST_TR_AR_BYTES, 0x008b);
4071
4072 flags = vmcs_readl(GUEST_RFLAGS);
Avi Kivity78ac8b42010-04-08 18:19:35 +03004073 vmx->rmode.save_rflags = flags;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004074
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +01004075 flags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004076
4077 vmcs_writel(GUEST_RFLAGS, flags);
Rusty Russell66aee912007-07-17 23:34:16 +10004078 vmcs_writel(GUEST_CR4, vmcs_readl(GUEST_CR4) | X86_CR4_VME);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004079 update_exception_bitmap(vcpu);
4080
Gleb Natapovd99e4152012-12-20 16:57:45 +02004081 fix_rmode_seg(VCPU_SREG_SS, &vmx->rmode.segs[VCPU_SREG_SS]);
4082 fix_rmode_seg(VCPU_SREG_CS, &vmx->rmode.segs[VCPU_SREG_CS]);
4083 fix_rmode_seg(VCPU_SREG_ES, &vmx->rmode.segs[VCPU_SREG_ES]);
4084 fix_rmode_seg(VCPU_SREG_DS, &vmx->rmode.segs[VCPU_SREG_DS]);
4085 fix_rmode_seg(VCPU_SREG_GS, &vmx->rmode.segs[VCPU_SREG_GS]);
4086 fix_rmode_seg(VCPU_SREG_FS, &vmx->rmode.segs[VCPU_SREG_FS]);
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03004087
Eddie Dong8668a3c2007-10-10 14:26:45 +08004088 kvm_mmu_reset_context(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004089}
4090
Amit Shah401d10d2009-02-20 22:53:37 +05304091static void vmx_set_efer(struct kvm_vcpu *vcpu, u64 efer)
4092{
4093 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity26bb0982009-09-07 11:14:12 +03004094 struct shared_msr_entry *msr = find_msr_entry(vmx, MSR_EFER);
4095
4096 if (!msr)
4097 return;
Amit Shah401d10d2009-02-20 22:53:37 +05304098
Avi Kivity44ea2b12009-09-06 15:55:37 +03004099 /*
4100 * Force kernel_gs_base reloading before EFER changes, as control
4101 * of this msr depends on is_long_mode().
4102 */
4103 vmx_load_host_state(to_vmx(vcpu));
Avi Kivityf6801df2010-01-21 15:31:50 +02004104 vcpu->arch.efer = efer;
Amit Shah401d10d2009-02-20 22:53:37 +05304105 if (efer & EFER_LMA) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02004106 vm_entry_controls_setbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
Amit Shah401d10d2009-02-20 22:53:37 +05304107 msr->data = efer;
4108 } else {
Gleb Natapov2961e8762013-11-25 15:37:13 +02004109 vm_entry_controls_clearbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
Amit Shah401d10d2009-02-20 22:53:37 +05304110
4111 msr->data = efer & ~EFER_LME;
4112 }
4113 setup_msrs(vmx);
4114}
4115
Avi Kivity05b3e0c2006-12-13 00:33:45 -08004116#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08004117
4118static void enter_lmode(struct kvm_vcpu *vcpu)
4119{
4120 u32 guest_tr_ar;
4121
Avi Kivity2fb92db2011-04-27 19:42:18 +03004122 vmx_segment_cache_clear(to_vmx(vcpu));
4123
Avi Kivity6aa8b732006-12-10 02:21:36 -08004124 guest_tr_ar = vmcs_read32(GUEST_TR_AR_BYTES);
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07004125 if ((guest_tr_ar & VMX_AR_TYPE_MASK) != VMX_AR_TYPE_BUSY_64_TSS) {
Jan Kiszkabd801582011-09-12 11:26:22 +02004126 pr_debug_ratelimited("%s: tss fixup for long mode. \n",
4127 __func__);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004128 vmcs_write32(GUEST_TR_AR_BYTES,
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07004129 (guest_tr_ar & ~VMX_AR_TYPE_MASK)
4130 | VMX_AR_TYPE_BUSY_64_TSS);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004131 }
Avi Kivityda38f432010-07-06 11:30:49 +03004132 vmx_set_efer(vcpu, vcpu->arch.efer | EFER_LMA);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004133}
4134
4135static void exit_lmode(struct kvm_vcpu *vcpu)
4136{
Gleb Natapov2961e8762013-11-25 15:37:13 +02004137 vm_entry_controls_clearbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
Avi Kivityda38f432010-07-06 11:30:49 +03004138 vmx_set_efer(vcpu, vcpu->arch.efer & ~EFER_LMA);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004139}
4140
4141#endif
4142
Wanpeng Lic2ba05c2017-12-12 17:33:03 -08004143static inline void __vmx_flush_tlb(struct kvm_vcpu *vcpu, int vpid,
4144 bool invalidate_gpa)
Sheng Yang2384d2b2008-01-17 15:14:33 +08004145{
Wanpeng Lic2ba05c2017-12-12 17:33:03 -08004146 if (enable_ept && (invalidate_gpa || !enable_vpid)) {
Xiao Guangrongdd180b32010-07-03 16:02:42 +08004147 if (!VALID_PAGE(vcpu->arch.mmu.root_hpa))
4148 return;
Peter Feiner995f00a2017-06-30 17:26:32 -07004149 ept_sync_context(construct_eptp(vcpu, vcpu->arch.mmu.root_hpa));
Jim Mattsonf0b98c02017-03-15 07:56:11 -07004150 } else {
4151 vpid_sync_context(vpid);
Xiao Guangrongdd180b32010-07-03 16:02:42 +08004152 }
Sheng Yang2384d2b2008-01-17 15:14:33 +08004153}
4154
Wanpeng Lic2ba05c2017-12-12 17:33:03 -08004155static void vmx_flush_tlb(struct kvm_vcpu *vcpu, bool invalidate_gpa)
Wanpeng Lidd5f5342015-09-23 18:26:57 +08004156{
Wanpeng Lic2ba05c2017-12-12 17:33:03 -08004157 __vmx_flush_tlb(vcpu, to_vmx(vcpu)->vpid, invalidate_gpa);
Wanpeng Lidd5f5342015-09-23 18:26:57 +08004158}
4159
Jim Mattsonfb6c8192017-03-16 13:53:59 -07004160static void vmx_flush_tlb_ept_only(struct kvm_vcpu *vcpu)
4161{
4162 if (enable_ept)
Wanpeng Lic2ba05c2017-12-12 17:33:03 -08004163 vmx_flush_tlb(vcpu, true);
Jim Mattsonfb6c8192017-03-16 13:53:59 -07004164}
4165
Avi Kivitye8467fd2009-12-29 18:43:06 +02004166static void vmx_decache_cr0_guest_bits(struct kvm_vcpu *vcpu)
4167{
4168 ulong cr0_guest_owned_bits = vcpu->arch.cr0_guest_owned_bits;
4169
4170 vcpu->arch.cr0 &= ~cr0_guest_owned_bits;
4171 vcpu->arch.cr0 |= vmcs_readl(GUEST_CR0) & cr0_guest_owned_bits;
4172}
4173
Avi Kivityaff48ba2010-12-05 18:56:11 +02004174static void vmx_decache_cr3(struct kvm_vcpu *vcpu)
4175{
4176 if (enable_ept && is_paging(vcpu))
4177 vcpu->arch.cr3 = vmcs_readl(GUEST_CR3);
4178 __set_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail);
4179}
4180
Anthony Liguori25c4c272007-04-27 09:29:21 +03004181static void vmx_decache_cr4_guest_bits(struct kvm_vcpu *vcpu)
Avi Kivity399badf2007-01-05 16:36:38 -08004182{
Avi Kivityfc78f512009-12-07 12:16:48 +02004183 ulong cr4_guest_owned_bits = vcpu->arch.cr4_guest_owned_bits;
4184
4185 vcpu->arch.cr4 &= ~cr4_guest_owned_bits;
4186 vcpu->arch.cr4 |= vmcs_readl(GUEST_CR4) & cr4_guest_owned_bits;
Avi Kivity399badf2007-01-05 16:36:38 -08004187}
4188
Sheng Yang14394422008-04-28 12:24:45 +08004189static void ept_load_pdptrs(struct kvm_vcpu *vcpu)
4190{
Gleb Natapovd0d538b2013-10-09 19:13:19 +03004191 struct kvm_mmu *mmu = vcpu->arch.walk_mmu;
4192
Avi Kivity6de4f3a2009-05-31 22:58:47 +03004193 if (!test_bit(VCPU_EXREG_PDPTR,
4194 (unsigned long *)&vcpu->arch.regs_dirty))
4195 return;
4196
Sheng Yang14394422008-04-28 12:24:45 +08004197 if (is_paging(vcpu) && is_pae(vcpu) && !is_long_mode(vcpu)) {
Gleb Natapovd0d538b2013-10-09 19:13:19 +03004198 vmcs_write64(GUEST_PDPTR0, mmu->pdptrs[0]);
4199 vmcs_write64(GUEST_PDPTR1, mmu->pdptrs[1]);
4200 vmcs_write64(GUEST_PDPTR2, mmu->pdptrs[2]);
4201 vmcs_write64(GUEST_PDPTR3, mmu->pdptrs[3]);
Sheng Yang14394422008-04-28 12:24:45 +08004202 }
4203}
4204
Avi Kivity8f5d5492009-05-31 18:41:29 +03004205static void ept_save_pdptrs(struct kvm_vcpu *vcpu)
4206{
Gleb Natapovd0d538b2013-10-09 19:13:19 +03004207 struct kvm_mmu *mmu = vcpu->arch.walk_mmu;
4208
Avi Kivity8f5d5492009-05-31 18:41:29 +03004209 if (is_paging(vcpu) && is_pae(vcpu) && !is_long_mode(vcpu)) {
Gleb Natapovd0d538b2013-10-09 19:13:19 +03004210 mmu->pdptrs[0] = vmcs_read64(GUEST_PDPTR0);
4211 mmu->pdptrs[1] = vmcs_read64(GUEST_PDPTR1);
4212 mmu->pdptrs[2] = vmcs_read64(GUEST_PDPTR2);
4213 mmu->pdptrs[3] = vmcs_read64(GUEST_PDPTR3);
Avi Kivity8f5d5492009-05-31 18:41:29 +03004214 }
Avi Kivity6de4f3a2009-05-31 22:58:47 +03004215
4216 __set_bit(VCPU_EXREG_PDPTR,
4217 (unsigned long *)&vcpu->arch.regs_avail);
4218 __set_bit(VCPU_EXREG_PDPTR,
4219 (unsigned long *)&vcpu->arch.regs_dirty);
Avi Kivity8f5d5492009-05-31 18:41:29 +03004220}
4221
David Matlack38991522016-11-29 18:14:08 -08004222static bool nested_guest_cr0_valid(struct kvm_vcpu *vcpu, unsigned long val)
4223{
4224 u64 fixed0 = to_vmx(vcpu)->nested.nested_vmx_cr0_fixed0;
4225 u64 fixed1 = to_vmx(vcpu)->nested.nested_vmx_cr0_fixed1;
4226 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
4227
4228 if (to_vmx(vcpu)->nested.nested_vmx_secondary_ctls_high &
4229 SECONDARY_EXEC_UNRESTRICTED_GUEST &&
4230 nested_cpu_has2(vmcs12, SECONDARY_EXEC_UNRESTRICTED_GUEST))
4231 fixed0 &= ~(X86_CR0_PE | X86_CR0_PG);
4232
4233 return fixed_bits_valid(val, fixed0, fixed1);
4234}
4235
4236static bool nested_host_cr0_valid(struct kvm_vcpu *vcpu, unsigned long val)
4237{
4238 u64 fixed0 = to_vmx(vcpu)->nested.nested_vmx_cr0_fixed0;
4239 u64 fixed1 = to_vmx(vcpu)->nested.nested_vmx_cr0_fixed1;
4240
4241 return fixed_bits_valid(val, fixed0, fixed1);
4242}
4243
4244static bool nested_cr4_valid(struct kvm_vcpu *vcpu, unsigned long val)
4245{
4246 u64 fixed0 = to_vmx(vcpu)->nested.nested_vmx_cr4_fixed0;
4247 u64 fixed1 = to_vmx(vcpu)->nested.nested_vmx_cr4_fixed1;
4248
4249 return fixed_bits_valid(val, fixed0, fixed1);
4250}
4251
4252/* No difference in the restrictions on guest and host CR4 in VMX operation. */
4253#define nested_guest_cr4_valid nested_cr4_valid
4254#define nested_host_cr4_valid nested_cr4_valid
4255
Nadav Har'El5e1746d2011-05-25 23:03:24 +03004256static int vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4);
Sheng Yang14394422008-04-28 12:24:45 +08004257
4258static void ept_update_paging_mode_cr0(unsigned long *hw_cr0,
4259 unsigned long cr0,
4260 struct kvm_vcpu *vcpu)
4261{
Marcelo Tosatti5233dd52011-06-06 14:27:47 -03004262 if (!test_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail))
4263 vmx_decache_cr3(vcpu);
Sheng Yang14394422008-04-28 12:24:45 +08004264 if (!(cr0 & X86_CR0_PG)) {
4265 /* From paging/starting to nonpaging */
4266 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL,
Sheng Yang65267ea2008-06-18 14:43:38 +08004267 vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) |
Sheng Yang14394422008-04-28 12:24:45 +08004268 (CPU_BASED_CR3_LOAD_EXITING |
4269 CPU_BASED_CR3_STORE_EXITING));
4270 vcpu->arch.cr0 = cr0;
Avi Kivityfc78f512009-12-07 12:16:48 +02004271 vmx_set_cr4(vcpu, kvm_read_cr4(vcpu));
Sheng Yang14394422008-04-28 12:24:45 +08004272 } else if (!is_paging(vcpu)) {
4273 /* From nonpaging to paging */
4274 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL,
Sheng Yang65267ea2008-06-18 14:43:38 +08004275 vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) &
Sheng Yang14394422008-04-28 12:24:45 +08004276 ~(CPU_BASED_CR3_LOAD_EXITING |
4277 CPU_BASED_CR3_STORE_EXITING));
4278 vcpu->arch.cr0 = cr0;
Avi Kivityfc78f512009-12-07 12:16:48 +02004279 vmx_set_cr4(vcpu, kvm_read_cr4(vcpu));
Sheng Yang14394422008-04-28 12:24:45 +08004280 }
Sheng Yang95eb84a2009-08-19 09:52:18 +08004281
4282 if (!(cr0 & X86_CR0_WP))
4283 *hw_cr0 &= ~X86_CR0_WP;
Sheng Yang14394422008-04-28 12:24:45 +08004284}
4285
Avi Kivity6aa8b732006-12-10 02:21:36 -08004286static void vmx_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
4287{
Avi Kivity7ffd92c2009-06-09 14:10:45 +03004288 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nitin A Kamble3a624e22009-06-08 11:34:16 -07004289 unsigned long hw_cr0;
4290
Gleb Natapov50378782013-02-04 16:00:28 +02004291 hw_cr0 = (cr0 & ~KVM_GUEST_CR0_MASK);
Nitin A Kamble3a624e22009-06-08 11:34:16 -07004292 if (enable_unrestricted_guest)
Gleb Natapov50378782013-02-04 16:00:28 +02004293 hw_cr0 |= KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST;
Gleb Natapov218e7632013-01-21 15:36:45 +02004294 else {
Gleb Natapov50378782013-02-04 16:00:28 +02004295 hw_cr0 |= KVM_VM_CR0_ALWAYS_ON;
Sheng Yang14394422008-04-28 12:24:45 +08004296
Gleb Natapov218e7632013-01-21 15:36:45 +02004297 if (vmx->rmode.vm86_active && (cr0 & X86_CR0_PE))
4298 enter_pmode(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004299
Gleb Natapov218e7632013-01-21 15:36:45 +02004300 if (!vmx->rmode.vm86_active && !(cr0 & X86_CR0_PE))
4301 enter_rmode(vcpu);
4302 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08004303
Avi Kivity05b3e0c2006-12-13 00:33:45 -08004304#ifdef CONFIG_X86_64
Avi Kivityf6801df2010-01-21 15:31:50 +02004305 if (vcpu->arch.efer & EFER_LME) {
Rusty Russell707d92fa2007-07-17 23:19:08 +10004306 if (!is_paging(vcpu) && (cr0 & X86_CR0_PG))
Avi Kivity6aa8b732006-12-10 02:21:36 -08004307 enter_lmode(vcpu);
Rusty Russell707d92fa2007-07-17 23:19:08 +10004308 if (is_paging(vcpu) && !(cr0 & X86_CR0_PG))
Avi Kivity6aa8b732006-12-10 02:21:36 -08004309 exit_lmode(vcpu);
4310 }
4311#endif
4312
Avi Kivity089d0342009-03-23 18:26:32 +02004313 if (enable_ept)
Sheng Yang14394422008-04-28 12:24:45 +08004314 ept_update_paging_mode_cr0(&hw_cr0, cr0, vcpu);
4315
Avi Kivity6aa8b732006-12-10 02:21:36 -08004316 vmcs_writel(CR0_READ_SHADOW, cr0);
Sheng Yang14394422008-04-28 12:24:45 +08004317 vmcs_writel(GUEST_CR0, hw_cr0);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08004318 vcpu->arch.cr0 = cr0;
Gleb Natapov14168782013-01-21 15:36:49 +02004319
4320 /* depends on vcpu->arch.cr0 to be set to a new value */
4321 vmx->emulation_required = emulation_required(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004322}
4323
Yu Zhang855feb62017-08-24 20:27:55 +08004324static int get_ept_level(struct kvm_vcpu *vcpu)
4325{
4326 if (cpu_has_vmx_ept_5levels() && (cpuid_maxphyaddr(vcpu) > 48))
4327 return 5;
4328 return 4;
4329}
4330
Peter Feiner995f00a2017-06-30 17:26:32 -07004331static u64 construct_eptp(struct kvm_vcpu *vcpu, unsigned long root_hpa)
Sheng Yang14394422008-04-28 12:24:45 +08004332{
Yu Zhang855feb62017-08-24 20:27:55 +08004333 u64 eptp = VMX_EPTP_MT_WB;
Sheng Yang14394422008-04-28 12:24:45 +08004334
Yu Zhang855feb62017-08-24 20:27:55 +08004335 eptp |= (get_ept_level(vcpu) == 5) ? VMX_EPTP_PWL_5 : VMX_EPTP_PWL_4;
Sheng Yang14394422008-04-28 12:24:45 +08004336
Peter Feiner995f00a2017-06-30 17:26:32 -07004337 if (enable_ept_ad_bits &&
4338 (!is_guest_mode(vcpu) || nested_ept_ad_enabled(vcpu)))
David Hildenbrandbb97a012017-08-10 23:15:28 +02004339 eptp |= VMX_EPTP_AD_ENABLE_BIT;
Sheng Yang14394422008-04-28 12:24:45 +08004340 eptp |= (root_hpa & PAGE_MASK);
4341
4342 return eptp;
4343}
4344
Avi Kivity6aa8b732006-12-10 02:21:36 -08004345static void vmx_set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3)
4346{
Sheng Yang14394422008-04-28 12:24:45 +08004347 unsigned long guest_cr3;
4348 u64 eptp;
4349
4350 guest_cr3 = cr3;
Avi Kivity089d0342009-03-23 18:26:32 +02004351 if (enable_ept) {
Peter Feiner995f00a2017-06-30 17:26:32 -07004352 eptp = construct_eptp(vcpu, cr3);
Sheng Yang14394422008-04-28 12:24:45 +08004353 vmcs_write64(EPT_POINTER, eptp);
Jan Kiszka59ab5a82013-08-08 16:26:29 +02004354 if (is_paging(vcpu) || is_guest_mode(vcpu))
4355 guest_cr3 = kvm_read_cr3(vcpu);
4356 else
4357 guest_cr3 = vcpu->kvm->arch.ept_identity_map_addr;
Marcelo Tosatti7c93be442009-10-26 16:48:33 -02004358 ept_load_pdptrs(vcpu);
Sheng Yang14394422008-04-28 12:24:45 +08004359 }
4360
Wanpeng Lic2ba05c2017-12-12 17:33:03 -08004361 vmx_flush_tlb(vcpu, true);
Sheng Yang14394422008-04-28 12:24:45 +08004362 vmcs_writel(GUEST_CR3, guest_cr3);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004363}
4364
Nadav Har'El5e1746d2011-05-25 23:03:24 +03004365static int vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004366{
Ben Serebrin085e68e2015-04-16 11:58:05 -07004367 /*
4368 * Pass through host's Machine Check Enable value to hw_cr4, which
4369 * is in force while we are in guest mode. Do not let guests control
4370 * this bit, even if host CR4.MCE == 0.
4371 */
4372 unsigned long hw_cr4 =
4373 (cr4_read_shadow() & X86_CR4_MCE) |
4374 (cr4 & ~X86_CR4_MCE) |
4375 (to_vmx(vcpu)->rmode.vm86_active ?
4376 KVM_RMODE_VM_CR4_ALWAYS_ON : KVM_PMODE_VM_CR4_ALWAYS_ON);
Sheng Yang14394422008-04-28 12:24:45 +08004377
Paolo Bonzini0367f202016-07-12 10:44:55 +02004378 if ((cr4 & X86_CR4_UMIP) && !boot_cpu_has(X86_FEATURE_UMIP)) {
4379 vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL,
4380 SECONDARY_EXEC_DESC);
4381 hw_cr4 &= ~X86_CR4_UMIP;
4382 } else
4383 vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL,
4384 SECONDARY_EXEC_DESC);
4385
Nadav Har'El5e1746d2011-05-25 23:03:24 +03004386 if (cr4 & X86_CR4_VMXE) {
4387 /*
4388 * To use VMXON (and later other VMX instructions), a guest
4389 * must first be able to turn on cr4.VMXE (see handle_vmon()).
4390 * So basically the check on whether to allow nested VMX
4391 * is here.
4392 */
4393 if (!nested_vmx_allowed(vcpu))
4394 return 1;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01004395 }
David Matlack38991522016-11-29 18:14:08 -08004396
4397 if (to_vmx(vcpu)->nested.vmxon && !nested_cr4_valid(vcpu, cr4))
Nadav Har'El5e1746d2011-05-25 23:03:24 +03004398 return 1;
4399
Zhang Xiantaoad312c72007-12-13 23:50:52 +08004400 vcpu->arch.cr4 = cr4;
Avi Kivitybc230082009-12-08 12:14:42 +02004401 if (enable_ept) {
4402 if (!is_paging(vcpu)) {
4403 hw_cr4 &= ~X86_CR4_PAE;
4404 hw_cr4 |= X86_CR4_PSE;
4405 } else if (!(cr4 & X86_CR4_PAE)) {
4406 hw_cr4 &= ~X86_CR4_PAE;
4407 }
4408 }
Sheng Yang14394422008-04-28 12:24:45 +08004409
Radim Krčmář656ec4a2015-11-02 22:20:00 +01004410 if (!enable_unrestricted_guest && !is_paging(vcpu))
4411 /*
Huaitong Handdba2622016-03-22 16:51:15 +08004412 * SMEP/SMAP/PKU is disabled if CPU is in non-paging mode in
4413 * hardware. To emulate this behavior, SMEP/SMAP/PKU needs
4414 * to be manually disabled when guest switches to non-paging
4415 * mode.
4416 *
4417 * If !enable_unrestricted_guest, the CPU is always running
4418 * with CR0.PG=1 and CR4 needs to be modified.
4419 * If enable_unrestricted_guest, the CPU automatically
4420 * disables SMEP/SMAP/PKU when the guest sets CR0.PG=0.
Radim Krčmář656ec4a2015-11-02 22:20:00 +01004421 */
Huaitong Handdba2622016-03-22 16:51:15 +08004422 hw_cr4 &= ~(X86_CR4_SMEP | X86_CR4_SMAP | X86_CR4_PKE);
Radim Krčmář656ec4a2015-11-02 22:20:00 +01004423
Sheng Yang14394422008-04-28 12:24:45 +08004424 vmcs_writel(CR4_READ_SHADOW, cr4);
4425 vmcs_writel(GUEST_CR4, hw_cr4);
Nadav Har'El5e1746d2011-05-25 23:03:24 +03004426 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004427}
4428
Avi Kivity6aa8b732006-12-10 02:21:36 -08004429static void vmx_get_segment(struct kvm_vcpu *vcpu,
4430 struct kvm_segment *var, int seg)
4431{
Avi Kivitya9179492011-01-03 14:28:52 +02004432 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004433 u32 ar;
4434
Gleb Natapovc6ad11532012-12-12 19:10:51 +02004435 if (vmx->rmode.vm86_active && seg != VCPU_SREG_LDTR) {
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03004436 *var = vmx->rmode.segs[seg];
Avi Kivitya9179492011-01-03 14:28:52 +02004437 if (seg == VCPU_SREG_TR
Avi Kivity2fb92db2011-04-27 19:42:18 +03004438 || var->selector == vmx_read_guest_seg_selector(vmx, seg))
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03004439 return;
Avi Kivity1390a282012-08-21 17:07:08 +03004440 var->base = vmx_read_guest_seg_base(vmx, seg);
4441 var->selector = vmx_read_guest_seg_selector(vmx, seg);
4442 return;
Avi Kivitya9179492011-01-03 14:28:52 +02004443 }
Avi Kivity2fb92db2011-04-27 19:42:18 +03004444 var->base = vmx_read_guest_seg_base(vmx, seg);
4445 var->limit = vmx_read_guest_seg_limit(vmx, seg);
4446 var->selector = vmx_read_guest_seg_selector(vmx, seg);
4447 ar = vmx_read_guest_seg_ar(vmx, seg);
Gleb Natapov03617c12013-06-28 13:17:18 +03004448 var->unusable = (ar >> 16) & 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004449 var->type = ar & 15;
4450 var->s = (ar >> 4) & 1;
4451 var->dpl = (ar >> 5) & 3;
Gleb Natapov03617c12013-06-28 13:17:18 +03004452 /*
4453 * Some userspaces do not preserve unusable property. Since usable
4454 * segment has to be present according to VMX spec we can use present
4455 * property to amend userspace bug by making unusable segment always
4456 * nonpresent. vmx_segment_access_rights() already marks nonpresent
4457 * segment as unusable.
4458 */
4459 var->present = !var->unusable;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004460 var->avl = (ar >> 12) & 1;
4461 var->l = (ar >> 13) & 1;
4462 var->db = (ar >> 14) & 1;
4463 var->g = (ar >> 15) & 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004464}
4465
Avi Kivitya9179492011-01-03 14:28:52 +02004466static u64 vmx_get_segment_base(struct kvm_vcpu *vcpu, int seg)
4467{
Avi Kivitya9179492011-01-03 14:28:52 +02004468 struct kvm_segment s;
4469
4470 if (to_vmx(vcpu)->rmode.vm86_active) {
4471 vmx_get_segment(vcpu, &s, seg);
4472 return s.base;
4473 }
Avi Kivity2fb92db2011-04-27 19:42:18 +03004474 return vmx_read_guest_seg_base(to_vmx(vcpu), seg);
Avi Kivitya9179492011-01-03 14:28:52 +02004475}
4476
Marcelo Tosattib09408d2013-01-07 19:27:06 -02004477static int vmx_get_cpl(struct kvm_vcpu *vcpu)
Izik Eidus2e4d2652008-03-24 19:38:34 +02004478{
Marcelo Tosattib09408d2013-01-07 19:27:06 -02004479 struct vcpu_vmx *vmx = to_vmx(vcpu);
4480
Paolo Bonziniae9fedc2014-05-14 09:39:49 +02004481 if (unlikely(vmx->rmode.vm86_active))
Izik Eidus2e4d2652008-03-24 19:38:34 +02004482 return 0;
Paolo Bonziniae9fedc2014-05-14 09:39:49 +02004483 else {
4484 int ar = vmx_read_guest_seg_ar(vmx, VCPU_SREG_SS);
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07004485 return VMX_AR_DPL(ar);
Avi Kivity69c73022011-03-07 15:26:44 +02004486 }
Avi Kivity69c73022011-03-07 15:26:44 +02004487}
4488
Avi Kivity653e3102007-05-07 10:55:37 +03004489static u32 vmx_segment_access_rights(struct kvm_segment *var)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004490{
Avi Kivity6aa8b732006-12-10 02:21:36 -08004491 u32 ar;
4492
Avi Kivityf0495f92012-06-07 17:06:10 +03004493 if (var->unusable || !var->present)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004494 ar = 1 << 16;
4495 else {
4496 ar = var->type & 15;
4497 ar |= (var->s & 1) << 4;
4498 ar |= (var->dpl & 3) << 5;
4499 ar |= (var->present & 1) << 7;
4500 ar |= (var->avl & 1) << 12;
4501 ar |= (var->l & 1) << 13;
4502 ar |= (var->db & 1) << 14;
4503 ar |= (var->g & 1) << 15;
4504 }
Avi Kivity653e3102007-05-07 10:55:37 +03004505
4506 return ar;
4507}
4508
4509static void vmx_set_segment(struct kvm_vcpu *vcpu,
4510 struct kvm_segment *var, int seg)
4511{
Avi Kivity7ffd92c2009-06-09 14:10:45 +03004512 struct vcpu_vmx *vmx = to_vmx(vcpu);
Mathias Krause772e0312012-08-30 01:30:19 +02004513 const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
Avi Kivity653e3102007-05-07 10:55:37 +03004514
Avi Kivity2fb92db2011-04-27 19:42:18 +03004515 vmx_segment_cache_clear(vmx);
4516
Gleb Natapov1ecd50a2012-12-12 19:10:54 +02004517 if (vmx->rmode.vm86_active && seg != VCPU_SREG_LDTR) {
4518 vmx->rmode.segs[seg] = *var;
4519 if (seg == VCPU_SREG_TR)
4520 vmcs_write16(sf->selector, var->selector);
4521 else if (var->s)
4522 fix_rmode_seg(seg, &vmx->rmode.segs[seg]);
Gleb Natapovd99e4152012-12-20 16:57:45 +02004523 goto out;
Avi Kivity653e3102007-05-07 10:55:37 +03004524 }
Gleb Natapov1ecd50a2012-12-12 19:10:54 +02004525
Avi Kivity653e3102007-05-07 10:55:37 +03004526 vmcs_writel(sf->base, var->base);
4527 vmcs_write32(sf->limit, var->limit);
4528 vmcs_write16(sf->selector, var->selector);
Nitin A Kamble3a624e22009-06-08 11:34:16 -07004529
4530 /*
4531 * Fix the "Accessed" bit in AR field of segment registers for older
4532 * qemu binaries.
4533 * IA32 arch specifies that at the time of processor reset the
4534 * "Accessed" bit in the AR field of segment registers is 1. And qemu
Guo Chao0fa06072012-06-28 15:16:19 +08004535 * is setting it to 0 in the userland code. This causes invalid guest
Nitin A Kamble3a624e22009-06-08 11:34:16 -07004536 * state vmexit when "unrestricted guest" mode is turned on.
4537 * Fix for this setup issue in cpu_reset is being pushed in the qemu
4538 * tree. Newer qemu binaries with that qemu fix would not need this
4539 * kvm hack.
4540 */
4541 if (enable_unrestricted_guest && (seg != VCPU_SREG_LDTR))
Gleb Natapovf924d662012-12-12 19:10:55 +02004542 var->type |= 0x1; /* Accessed */
Nitin A Kamble3a624e22009-06-08 11:34:16 -07004543
Gleb Natapovf924d662012-12-12 19:10:55 +02004544 vmcs_write32(sf->ar_bytes, vmx_segment_access_rights(var));
Gleb Natapovd99e4152012-12-20 16:57:45 +02004545
4546out:
Paolo Bonzini98eb2f82014-03-27 09:51:52 +01004547 vmx->emulation_required = emulation_required(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004548}
4549
Avi Kivity6aa8b732006-12-10 02:21:36 -08004550static void vmx_get_cs_db_l_bits(struct kvm_vcpu *vcpu, int *db, int *l)
4551{
Avi Kivity2fb92db2011-04-27 19:42:18 +03004552 u32 ar = vmx_read_guest_seg_ar(to_vmx(vcpu), VCPU_SREG_CS);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004553
4554 *db = (ar >> 14) & 1;
4555 *l = (ar >> 13) & 1;
4556}
4557
Gleb Natapov89a27f42010-02-16 10:51:48 +02004558static void vmx_get_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004559{
Gleb Natapov89a27f42010-02-16 10:51:48 +02004560 dt->size = vmcs_read32(GUEST_IDTR_LIMIT);
4561 dt->address = vmcs_readl(GUEST_IDTR_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004562}
4563
Gleb Natapov89a27f42010-02-16 10:51:48 +02004564static void vmx_set_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004565{
Gleb Natapov89a27f42010-02-16 10:51:48 +02004566 vmcs_write32(GUEST_IDTR_LIMIT, dt->size);
4567 vmcs_writel(GUEST_IDTR_BASE, dt->address);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004568}
4569
Gleb Natapov89a27f42010-02-16 10:51:48 +02004570static void vmx_get_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004571{
Gleb Natapov89a27f42010-02-16 10:51:48 +02004572 dt->size = vmcs_read32(GUEST_GDTR_LIMIT);
4573 dt->address = vmcs_readl(GUEST_GDTR_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004574}
4575
Gleb Natapov89a27f42010-02-16 10:51:48 +02004576static void vmx_set_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004577{
Gleb Natapov89a27f42010-02-16 10:51:48 +02004578 vmcs_write32(GUEST_GDTR_LIMIT, dt->size);
4579 vmcs_writel(GUEST_GDTR_BASE, dt->address);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004580}
4581
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004582static bool rmode_segment_valid(struct kvm_vcpu *vcpu, int seg)
4583{
4584 struct kvm_segment var;
4585 u32 ar;
4586
4587 vmx_get_segment(vcpu, &var, seg);
Gleb Natapov07f42f52012-12-12 19:10:49 +02004588 var.dpl = 0x3;
Gleb Natapov0647f4a2012-12-12 19:10:50 +02004589 if (seg == VCPU_SREG_CS)
4590 var.type = 0x3;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004591 ar = vmx_segment_access_rights(&var);
4592
4593 if (var.base != (var.selector << 4))
4594 return false;
Gleb Natapov89efbed2012-12-20 16:57:44 +02004595 if (var.limit != 0xffff)
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004596 return false;
Gleb Natapov07f42f52012-12-12 19:10:49 +02004597 if (ar != 0xf3)
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004598 return false;
4599
4600 return true;
4601}
4602
4603static bool code_segment_valid(struct kvm_vcpu *vcpu)
4604{
4605 struct kvm_segment cs;
4606 unsigned int cs_rpl;
4607
4608 vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
Nadav Amitb32a9912015-03-29 16:33:04 +03004609 cs_rpl = cs.selector & SEGMENT_RPL_MASK;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004610
Avi Kivity1872a3f2009-01-04 23:26:52 +02004611 if (cs.unusable)
4612 return false;
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07004613 if (~cs.type & (VMX_AR_TYPE_CODE_MASK|VMX_AR_TYPE_ACCESSES_MASK))
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004614 return false;
4615 if (!cs.s)
4616 return false;
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07004617 if (cs.type & VMX_AR_TYPE_WRITEABLE_MASK) {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004618 if (cs.dpl > cs_rpl)
4619 return false;
Avi Kivity1872a3f2009-01-04 23:26:52 +02004620 } else {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004621 if (cs.dpl != cs_rpl)
4622 return false;
4623 }
4624 if (!cs.present)
4625 return false;
4626
4627 /* TODO: Add Reserved field check, this'll require a new member in the kvm_segment_field structure */
4628 return true;
4629}
4630
4631static bool stack_segment_valid(struct kvm_vcpu *vcpu)
4632{
4633 struct kvm_segment ss;
4634 unsigned int ss_rpl;
4635
4636 vmx_get_segment(vcpu, &ss, VCPU_SREG_SS);
Nadav Amitb32a9912015-03-29 16:33:04 +03004637 ss_rpl = ss.selector & SEGMENT_RPL_MASK;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004638
Avi Kivity1872a3f2009-01-04 23:26:52 +02004639 if (ss.unusable)
4640 return true;
4641 if (ss.type != 3 && ss.type != 7)
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004642 return false;
4643 if (!ss.s)
4644 return false;
4645 if (ss.dpl != ss_rpl) /* DPL != RPL */
4646 return false;
4647 if (!ss.present)
4648 return false;
4649
4650 return true;
4651}
4652
4653static bool data_segment_valid(struct kvm_vcpu *vcpu, int seg)
4654{
4655 struct kvm_segment var;
4656 unsigned int rpl;
4657
4658 vmx_get_segment(vcpu, &var, seg);
Nadav Amitb32a9912015-03-29 16:33:04 +03004659 rpl = var.selector & SEGMENT_RPL_MASK;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004660
Avi Kivity1872a3f2009-01-04 23:26:52 +02004661 if (var.unusable)
4662 return true;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004663 if (!var.s)
4664 return false;
4665 if (!var.present)
4666 return false;
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07004667 if (~var.type & (VMX_AR_TYPE_CODE_MASK|VMX_AR_TYPE_WRITEABLE_MASK)) {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004668 if (var.dpl < rpl) /* DPL < RPL */
4669 return false;
4670 }
4671
4672 /* TODO: Add other members to kvm_segment_field to allow checking for other access
4673 * rights flags
4674 */
4675 return true;
4676}
4677
4678static bool tr_valid(struct kvm_vcpu *vcpu)
4679{
4680 struct kvm_segment tr;
4681
4682 vmx_get_segment(vcpu, &tr, VCPU_SREG_TR);
4683
Avi Kivity1872a3f2009-01-04 23:26:52 +02004684 if (tr.unusable)
4685 return false;
Nadav Amitb32a9912015-03-29 16:33:04 +03004686 if (tr.selector & SEGMENT_TI_MASK) /* TI = 1 */
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004687 return false;
Avi Kivity1872a3f2009-01-04 23:26:52 +02004688 if (tr.type != 3 && tr.type != 11) /* TODO: Check if guest is in IA32e mode */
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004689 return false;
4690 if (!tr.present)
4691 return false;
4692
4693 return true;
4694}
4695
4696static bool ldtr_valid(struct kvm_vcpu *vcpu)
4697{
4698 struct kvm_segment ldtr;
4699
4700 vmx_get_segment(vcpu, &ldtr, VCPU_SREG_LDTR);
4701
Avi Kivity1872a3f2009-01-04 23:26:52 +02004702 if (ldtr.unusable)
4703 return true;
Nadav Amitb32a9912015-03-29 16:33:04 +03004704 if (ldtr.selector & SEGMENT_TI_MASK) /* TI = 1 */
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004705 return false;
4706 if (ldtr.type != 2)
4707 return false;
4708 if (!ldtr.present)
4709 return false;
4710
4711 return true;
4712}
4713
4714static bool cs_ss_rpl_check(struct kvm_vcpu *vcpu)
4715{
4716 struct kvm_segment cs, ss;
4717
4718 vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
4719 vmx_get_segment(vcpu, &ss, VCPU_SREG_SS);
4720
Nadav Amitb32a9912015-03-29 16:33:04 +03004721 return ((cs.selector & SEGMENT_RPL_MASK) ==
4722 (ss.selector & SEGMENT_RPL_MASK));
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004723}
4724
4725/*
4726 * Check if guest state is valid. Returns true if valid, false if
4727 * not.
4728 * We assume that registers are always usable
4729 */
4730static bool guest_state_valid(struct kvm_vcpu *vcpu)
4731{
Gleb Natapovc5e97c82013-01-21 15:36:43 +02004732 if (enable_unrestricted_guest)
4733 return true;
4734
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004735 /* real mode guest state checks */
Gleb Natapovf13882d2013-04-14 16:07:37 +03004736 if (!is_protmode(vcpu) || (vmx_get_rflags(vcpu) & X86_EFLAGS_VM)) {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004737 if (!rmode_segment_valid(vcpu, VCPU_SREG_CS))
4738 return false;
4739 if (!rmode_segment_valid(vcpu, VCPU_SREG_SS))
4740 return false;
4741 if (!rmode_segment_valid(vcpu, VCPU_SREG_DS))
4742 return false;
4743 if (!rmode_segment_valid(vcpu, VCPU_SREG_ES))
4744 return false;
4745 if (!rmode_segment_valid(vcpu, VCPU_SREG_FS))
4746 return false;
4747 if (!rmode_segment_valid(vcpu, VCPU_SREG_GS))
4748 return false;
4749 } else {
4750 /* protected mode guest state checks */
4751 if (!cs_ss_rpl_check(vcpu))
4752 return false;
4753 if (!code_segment_valid(vcpu))
4754 return false;
4755 if (!stack_segment_valid(vcpu))
4756 return false;
4757 if (!data_segment_valid(vcpu, VCPU_SREG_DS))
4758 return false;
4759 if (!data_segment_valid(vcpu, VCPU_SREG_ES))
4760 return false;
4761 if (!data_segment_valid(vcpu, VCPU_SREG_FS))
4762 return false;
4763 if (!data_segment_valid(vcpu, VCPU_SREG_GS))
4764 return false;
4765 if (!tr_valid(vcpu))
4766 return false;
4767 if (!ldtr_valid(vcpu))
4768 return false;
4769 }
4770 /* TODO:
4771 * - Add checks on RIP
4772 * - Add checks on RFLAGS
4773 */
4774
4775 return true;
4776}
4777
Jim Mattson5fa99cb2017-07-06 16:33:07 -07004778static bool page_address_valid(struct kvm_vcpu *vcpu, gpa_t gpa)
4779{
4780 return PAGE_ALIGNED(gpa) && !(gpa >> cpuid_maxphyaddr(vcpu));
4781}
4782
Mike Dayd77c26f2007-10-08 09:02:08 -04004783static int init_rmode_tss(struct kvm *kvm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004784{
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08004785 gfn_t fn;
Izik Eidus195aefd2007-10-01 22:14:18 +02004786 u16 data = 0;
Paolo Bonzini1f755a82014-09-16 13:37:40 +02004787 int idx, r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004788
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08004789 idx = srcu_read_lock(&kvm->srcu);
Jan Kiszka4918c6c2013-03-15 08:38:56 +01004790 fn = kvm->arch.tss_addr >> PAGE_SHIFT;
Izik Eidus195aefd2007-10-01 22:14:18 +02004791 r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE);
4792 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05004793 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02004794 data = TSS_BASE_SIZE + TSS_REDIRECTION_SIZE;
Sheng Yang464d17c2008-08-13 14:10:33 +08004795 r = kvm_write_guest_page(kvm, fn++, &data,
4796 TSS_IOPB_BASE_OFFSET, sizeof(u16));
Izik Eidus195aefd2007-10-01 22:14:18 +02004797 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05004798 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02004799 r = kvm_clear_guest_page(kvm, fn++, 0, PAGE_SIZE);
4800 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05004801 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02004802 r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE);
4803 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05004804 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02004805 data = ~0;
Marcelo Tosatti10589a42007-12-20 19:18:22 -05004806 r = kvm_write_guest_page(kvm, fn, &data,
4807 RMODE_TSS_SIZE - 2 * PAGE_SIZE - 1,
4808 sizeof(u8));
Marcelo Tosatti10589a42007-12-20 19:18:22 -05004809out:
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08004810 srcu_read_unlock(&kvm->srcu, idx);
Paolo Bonzini1f755a82014-09-16 13:37:40 +02004811 return r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004812}
4813
Sheng Yangb7ebfb02008-04-25 21:44:52 +08004814static int init_rmode_identity_map(struct kvm *kvm)
4815{
Tang Chenf51770e2014-09-16 18:41:59 +08004816 int i, idx, r = 0;
Dan Williamsba049e92016-01-15 16:56:11 -08004817 kvm_pfn_t identity_map_pfn;
Sheng Yangb7ebfb02008-04-25 21:44:52 +08004818 u32 tmp;
4819
Tang Chena255d472014-09-16 18:41:58 +08004820 /* Protect kvm->arch.ept_identity_pagetable_done. */
4821 mutex_lock(&kvm->slots_lock);
4822
Tang Chenf51770e2014-09-16 18:41:59 +08004823 if (likely(kvm->arch.ept_identity_pagetable_done))
Tang Chena255d472014-09-16 18:41:58 +08004824 goto out2;
Tang Chena255d472014-09-16 18:41:58 +08004825
David Hildenbrandd8a6e362017-08-24 20:51:34 +02004826 if (!kvm->arch.ept_identity_map_addr)
4827 kvm->arch.ept_identity_map_addr = VMX_EPT_IDENTITY_PAGETABLE_ADDR;
Sheng Yangb927a3c2009-07-21 10:42:48 +08004828 identity_map_pfn = kvm->arch.ept_identity_map_addr >> PAGE_SHIFT;
Tang Chena255d472014-09-16 18:41:58 +08004829
David Hildenbrandd8a6e362017-08-24 20:51:34 +02004830 r = __x86_set_memory_region(kvm, IDENTITY_PAGETABLE_PRIVATE_MEMSLOT,
4831 kvm->arch.ept_identity_map_addr, PAGE_SIZE);
Tang Chenf51770e2014-09-16 18:41:59 +08004832 if (r < 0)
Tang Chena255d472014-09-16 18:41:58 +08004833 goto out2;
4834
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08004835 idx = srcu_read_lock(&kvm->srcu);
Sheng Yangb7ebfb02008-04-25 21:44:52 +08004836 r = kvm_clear_guest_page(kvm, identity_map_pfn, 0, PAGE_SIZE);
4837 if (r < 0)
4838 goto out;
4839 /* Set up identity-mapping pagetable for EPT in real mode */
4840 for (i = 0; i < PT32_ENT_PER_PAGE; i++) {
4841 tmp = (i << 22) + (_PAGE_PRESENT | _PAGE_RW | _PAGE_USER |
4842 _PAGE_ACCESSED | _PAGE_DIRTY | _PAGE_PSE);
4843 r = kvm_write_guest_page(kvm, identity_map_pfn,
4844 &tmp, i * sizeof(tmp), sizeof(tmp));
4845 if (r < 0)
4846 goto out;
4847 }
4848 kvm->arch.ept_identity_pagetable_done = true;
Tang Chenf51770e2014-09-16 18:41:59 +08004849
Sheng Yangb7ebfb02008-04-25 21:44:52 +08004850out:
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08004851 srcu_read_unlock(&kvm->srcu, idx);
Tang Chena255d472014-09-16 18:41:58 +08004852
4853out2:
4854 mutex_unlock(&kvm->slots_lock);
Tang Chenf51770e2014-09-16 18:41:59 +08004855 return r;
Sheng Yangb7ebfb02008-04-25 21:44:52 +08004856}
4857
Avi Kivity6aa8b732006-12-10 02:21:36 -08004858static void seg_setup(int seg)
4859{
Mathias Krause772e0312012-08-30 01:30:19 +02004860 const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
Nitin A Kamble3a624e22009-06-08 11:34:16 -07004861 unsigned int ar;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004862
4863 vmcs_write16(sf->selector, 0);
4864 vmcs_writel(sf->base, 0);
4865 vmcs_write32(sf->limit, 0xffff);
Gleb Natapovd54d07b2012-12-20 16:57:46 +02004866 ar = 0x93;
4867 if (seg == VCPU_SREG_CS)
4868 ar |= 0x08; /* code segment */
Nitin A Kamble3a624e22009-06-08 11:34:16 -07004869
4870 vmcs_write32(sf->ar_bytes, ar);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004871}
4872
Sheng Yangf78e0e22007-10-29 09:40:42 +08004873static int alloc_apic_access_page(struct kvm *kvm)
4874{
Xiao Guangrong44841412012-09-07 14:14:20 +08004875 struct page *page;
Sheng Yangf78e0e22007-10-29 09:40:42 +08004876 int r = 0;
4877
Marcelo Tosatti79fac952009-12-23 14:35:26 -02004878 mutex_lock(&kvm->slots_lock);
Tang Chenc24ae0d2014-09-24 15:57:58 +08004879 if (kvm->arch.apic_access_page_done)
Sheng Yangf78e0e22007-10-29 09:40:42 +08004880 goto out;
Paolo Bonzini1d8007b2015-10-12 13:38:32 +02004881 r = __x86_set_memory_region(kvm, APIC_ACCESS_PAGE_PRIVATE_MEMSLOT,
4882 APIC_DEFAULT_PHYS_BASE, PAGE_SIZE);
Sheng Yangf78e0e22007-10-29 09:40:42 +08004883 if (r)
4884 goto out;
Izik Eidus72dc67a2008-02-10 18:04:15 +02004885
Tang Chen73a6d942014-09-11 13:38:00 +08004886 page = gfn_to_page(kvm, APIC_DEFAULT_PHYS_BASE >> PAGE_SHIFT);
Xiao Guangrong44841412012-09-07 14:14:20 +08004887 if (is_error_page(page)) {
4888 r = -EFAULT;
4889 goto out;
4890 }
4891
Tang Chenc24ae0d2014-09-24 15:57:58 +08004892 /*
4893 * Do not pin the page in memory, so that memory hot-unplug
4894 * is able to migrate it.
4895 */
4896 put_page(page);
4897 kvm->arch.apic_access_page_done = true;
Sheng Yangf78e0e22007-10-29 09:40:42 +08004898out:
Marcelo Tosatti79fac952009-12-23 14:35:26 -02004899 mutex_unlock(&kvm->slots_lock);
Sheng Yangf78e0e22007-10-29 09:40:42 +08004900 return r;
4901}
4902
Wanpeng Li991e7a02015-09-16 17:30:05 +08004903static int allocate_vpid(void)
Sheng Yang2384d2b2008-01-17 15:14:33 +08004904{
4905 int vpid;
4906
Avi Kivity919818a2009-03-23 18:01:29 +02004907 if (!enable_vpid)
Wanpeng Li991e7a02015-09-16 17:30:05 +08004908 return 0;
Sheng Yang2384d2b2008-01-17 15:14:33 +08004909 spin_lock(&vmx_vpid_lock);
4910 vpid = find_first_zero_bit(vmx_vpid_bitmap, VMX_NR_VPIDS);
Wanpeng Li991e7a02015-09-16 17:30:05 +08004911 if (vpid < VMX_NR_VPIDS)
Sheng Yang2384d2b2008-01-17 15:14:33 +08004912 __set_bit(vpid, vmx_vpid_bitmap);
Wanpeng Li991e7a02015-09-16 17:30:05 +08004913 else
4914 vpid = 0;
Sheng Yang2384d2b2008-01-17 15:14:33 +08004915 spin_unlock(&vmx_vpid_lock);
Wanpeng Li991e7a02015-09-16 17:30:05 +08004916 return vpid;
Sheng Yang2384d2b2008-01-17 15:14:33 +08004917}
4918
Wanpeng Li991e7a02015-09-16 17:30:05 +08004919static void free_vpid(int vpid)
Lai Jiangshancdbecfc2010-04-17 16:41:47 +08004920{
Wanpeng Li991e7a02015-09-16 17:30:05 +08004921 if (!enable_vpid || vpid == 0)
Lai Jiangshancdbecfc2010-04-17 16:41:47 +08004922 return;
4923 spin_lock(&vmx_vpid_lock);
Wanpeng Li991e7a02015-09-16 17:30:05 +08004924 __clear_bit(vpid, vmx_vpid_bitmap);
Lai Jiangshancdbecfc2010-04-17 16:41:47 +08004925 spin_unlock(&vmx_vpid_lock);
4926}
4927
Yang Zhang8d146952013-01-25 10:18:50 +08004928#define MSR_TYPE_R 1
4929#define MSR_TYPE_W 2
4930static void __vmx_disable_intercept_for_msr(unsigned long *msr_bitmap,
4931 u32 msr, int type)
Sheng Yang25c5f222008-03-28 13:18:56 +08004932{
Avi Kivity3e7c73e2009-02-24 21:46:19 +02004933 int f = sizeof(unsigned long);
Sheng Yang25c5f222008-03-28 13:18:56 +08004934
4935 if (!cpu_has_vmx_msr_bitmap())
4936 return;
4937
4938 /*
4939 * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
4940 * have the write-low and read-high bitmap offsets the wrong way round.
4941 * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
4942 */
Sheng Yang25c5f222008-03-28 13:18:56 +08004943 if (msr <= 0x1fff) {
Yang Zhang8d146952013-01-25 10:18:50 +08004944 if (type & MSR_TYPE_R)
4945 /* read-low */
4946 __clear_bit(msr, msr_bitmap + 0x000 / f);
4947
4948 if (type & MSR_TYPE_W)
4949 /* write-low */
4950 __clear_bit(msr, msr_bitmap + 0x800 / f);
4951
Sheng Yang25c5f222008-03-28 13:18:56 +08004952 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
4953 msr &= 0x1fff;
Yang Zhang8d146952013-01-25 10:18:50 +08004954 if (type & MSR_TYPE_R)
4955 /* read-high */
4956 __clear_bit(msr, msr_bitmap + 0x400 / f);
4957
4958 if (type & MSR_TYPE_W)
4959 /* write-high */
4960 __clear_bit(msr, msr_bitmap + 0xc00 / f);
4961
4962 }
4963}
4964
Wincy Vanf2b93282015-02-03 23:56:03 +08004965/*
4966 * If a msr is allowed by L0, we should check whether it is allowed by L1.
4967 * The corresponding bit will be cleared unless both of L0 and L1 allow it.
4968 */
4969static void nested_vmx_disable_intercept_for_msr(unsigned long *msr_bitmap_l1,
4970 unsigned long *msr_bitmap_nested,
4971 u32 msr, int type)
4972{
4973 int f = sizeof(unsigned long);
4974
Wincy Vanf2b93282015-02-03 23:56:03 +08004975 /*
4976 * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
4977 * have the write-low and read-high bitmap offsets the wrong way round.
4978 * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
4979 */
4980 if (msr <= 0x1fff) {
4981 if (type & MSR_TYPE_R &&
4982 !test_bit(msr, msr_bitmap_l1 + 0x000 / f))
4983 /* read-low */
4984 __clear_bit(msr, msr_bitmap_nested + 0x000 / f);
4985
4986 if (type & MSR_TYPE_W &&
4987 !test_bit(msr, msr_bitmap_l1 + 0x800 / f))
4988 /* write-low */
4989 __clear_bit(msr, msr_bitmap_nested + 0x800 / f);
4990
4991 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
4992 msr &= 0x1fff;
4993 if (type & MSR_TYPE_R &&
4994 !test_bit(msr, msr_bitmap_l1 + 0x400 / f))
4995 /* read-high */
4996 __clear_bit(msr, msr_bitmap_nested + 0x400 / f);
4997
4998 if (type & MSR_TYPE_W &&
4999 !test_bit(msr, msr_bitmap_l1 + 0xc00 / f))
5000 /* write-high */
5001 __clear_bit(msr, msr_bitmap_nested + 0xc00 / f);
5002
5003 }
5004}
5005
Avi Kivity58972972009-02-24 22:26:47 +02005006static void vmx_disable_intercept_for_msr(u32 msr, bool longmode_only)
5007{
5008 if (!longmode_only)
Yang Zhang8d146952013-01-25 10:18:50 +08005009 __vmx_disable_intercept_for_msr(vmx_msr_bitmap_legacy,
5010 msr, MSR_TYPE_R | MSR_TYPE_W);
5011 __vmx_disable_intercept_for_msr(vmx_msr_bitmap_longmode,
5012 msr, MSR_TYPE_R | MSR_TYPE_W);
5013}
5014
Paolo Bonzinid7231e72017-12-21 00:47:55 +01005015#define X2APIC_MSR(r) (APIC_BASE_MSR + ((r) >> 4))
5016
Paolo Bonzini1f6e5b22017-12-20 11:32:16 +01005017static void vmx_disable_intercept_msr_x2apic(u32 msr, int type, bool apicv_only)
Yang Zhang8d146952013-01-25 10:18:50 +08005018{
Paolo Bonzini1f6e5b22017-12-20 11:32:16 +01005019 __vmx_disable_intercept_for_msr(vmx_msr_bitmap_legacy_x2apic_apicv,
5020 msr, type);
5021 __vmx_disable_intercept_for_msr(vmx_msr_bitmap_longmode_x2apic_apicv,
5022 msr, type);
5023 if (!apicv_only) {
Wanpeng Lif6e90f92016-09-22 07:43:25 +08005024 __vmx_disable_intercept_for_msr(vmx_msr_bitmap_legacy_x2apic,
Radim Krčmář2e69f862016-09-29 22:41:32 +02005025 msr, type);
Wanpeng Lif6e90f92016-09-22 07:43:25 +08005026 __vmx_disable_intercept_for_msr(vmx_msr_bitmap_longmode_x2apic,
Radim Krčmář2e69f862016-09-29 22:41:32 +02005027 msr, type);
Wanpeng Lif6e90f92016-09-22 07:43:25 +08005028 }
Avi Kivity58972972009-02-24 22:26:47 +02005029}
5030
Suravee Suthikulpanitb2a05fe2017-09-12 10:42:41 -05005031static bool vmx_get_enable_apicv(struct kvm_vcpu *vcpu)
Paolo Bonzinid50ab6c2015-07-29 11:49:59 +02005032{
Andrey Smetanind62caab2015-11-10 15:36:33 +03005033 return enable_apicv;
Paolo Bonzinid50ab6c2015-07-29 11:49:59 +02005034}
5035
David Matlackc9f04402017-08-01 14:00:40 -07005036static void nested_mark_vmcs12_pages_dirty(struct kvm_vcpu *vcpu)
5037{
5038 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
5039 gfn_t gfn;
5040
5041 /*
5042 * Don't need to mark the APIC access page dirty; it is never
5043 * written to by the CPU during APIC virtualization.
5044 */
5045
5046 if (nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW)) {
5047 gfn = vmcs12->virtual_apic_page_addr >> PAGE_SHIFT;
5048 kvm_vcpu_mark_page_dirty(vcpu, gfn);
5049 }
5050
5051 if (nested_cpu_has_posted_intr(vmcs12)) {
5052 gfn = vmcs12->posted_intr_desc_addr >> PAGE_SHIFT;
5053 kvm_vcpu_mark_page_dirty(vcpu, gfn);
5054 }
5055}
5056
5057
David Hildenbrand6342c502017-01-25 11:58:58 +01005058static void vmx_complete_nested_posted_interrupt(struct kvm_vcpu *vcpu)
Wincy Van705699a2015-02-03 23:58:17 +08005059{
5060 struct vcpu_vmx *vmx = to_vmx(vcpu);
5061 int max_irr;
5062 void *vapic_page;
5063 u16 status;
5064
David Matlackc9f04402017-08-01 14:00:40 -07005065 if (!vmx->nested.pi_desc || !vmx->nested.pi_pending)
5066 return;
Wincy Van705699a2015-02-03 23:58:17 +08005067
David Matlackc9f04402017-08-01 14:00:40 -07005068 vmx->nested.pi_pending = false;
5069 if (!pi_test_and_clear_on(vmx->nested.pi_desc))
5070 return;
Wincy Van705699a2015-02-03 23:58:17 +08005071
David Matlackc9f04402017-08-01 14:00:40 -07005072 max_irr = find_last_bit((unsigned long *)vmx->nested.pi_desc->pir, 256);
5073 if (max_irr != 256) {
Wincy Van705699a2015-02-03 23:58:17 +08005074 vapic_page = kmap(vmx->nested.virtual_apic_page);
Liran Alone7387b02017-12-24 18:12:54 +02005075 __kvm_apic_update_irr(vmx->nested.pi_desc->pir,
5076 vapic_page, &max_irr);
Wincy Van705699a2015-02-03 23:58:17 +08005077 kunmap(vmx->nested.virtual_apic_page);
5078
5079 status = vmcs_read16(GUEST_INTR_STATUS);
5080 if ((u8)max_irr > ((u8)status & 0xff)) {
5081 status &= ~0xff;
5082 status |= (u8)max_irr;
5083 vmcs_write16(GUEST_INTR_STATUS, status);
5084 }
5085 }
David Matlackc9f04402017-08-01 14:00:40 -07005086
5087 nested_mark_vmcs12_pages_dirty(vcpu);
Wincy Van705699a2015-02-03 23:58:17 +08005088}
5089
Wincy Van06a55242017-04-28 13:13:59 +08005090static inline bool kvm_vcpu_trigger_posted_interrupt(struct kvm_vcpu *vcpu,
5091 bool nested)
Radim Krčmář21bc8dc2015-02-16 15:36:33 +01005092{
5093#ifdef CONFIG_SMP
Wincy Van06a55242017-04-28 13:13:59 +08005094 int pi_vec = nested ? POSTED_INTR_NESTED_VECTOR : POSTED_INTR_VECTOR;
5095
Radim Krčmář21bc8dc2015-02-16 15:36:33 +01005096 if (vcpu->mode == IN_GUEST_MODE) {
Feng Wu28b835d2015-09-18 22:29:54 +08005097 /*
Haozhong Zhang5753743f2017-09-18 09:56:50 +08005098 * The vector of interrupt to be delivered to vcpu had
5099 * been set in PIR before this function.
Feng Wu28b835d2015-09-18 22:29:54 +08005100 *
Haozhong Zhang5753743f2017-09-18 09:56:50 +08005101 * Following cases will be reached in this block, and
5102 * we always send a notification event in all cases as
5103 * explained below.
5104 *
5105 * Case 1: vcpu keeps in non-root mode. Sending a
5106 * notification event posts the interrupt to vcpu.
5107 *
5108 * Case 2: vcpu exits to root mode and is still
5109 * runnable. PIR will be synced to vIRR before the
5110 * next vcpu entry. Sending a notification event in
5111 * this case has no effect, as vcpu is not in root
5112 * mode.
5113 *
5114 * Case 3: vcpu exits to root mode and is blocked.
5115 * vcpu_block() has already synced PIR to vIRR and
5116 * never blocks vcpu if vIRR is not cleared. Therefore,
5117 * a blocked vcpu here does not wait for any requested
5118 * interrupts in PIR, and sending a notification event
5119 * which has no effect is safe here.
Feng Wu28b835d2015-09-18 22:29:54 +08005120 */
Feng Wu28b835d2015-09-18 22:29:54 +08005121
Wincy Van06a55242017-04-28 13:13:59 +08005122 apic->send_IPI_mask(get_cpu_mask(vcpu->cpu), pi_vec);
Radim Krčmář21bc8dc2015-02-16 15:36:33 +01005123 return true;
5124 }
5125#endif
5126 return false;
5127}
5128
Wincy Van705699a2015-02-03 23:58:17 +08005129static int vmx_deliver_nested_posted_interrupt(struct kvm_vcpu *vcpu,
5130 int vector)
5131{
5132 struct vcpu_vmx *vmx = to_vmx(vcpu);
5133
5134 if (is_guest_mode(vcpu) &&
5135 vector == vmx->nested.posted_intr_nv) {
Wincy Van705699a2015-02-03 23:58:17 +08005136 /*
5137 * If a posted intr is not recognized by hardware,
5138 * we will accomplish it in the next vmentry.
5139 */
5140 vmx->nested.pi_pending = true;
5141 kvm_make_request(KVM_REQ_EVENT, vcpu);
Liran Alon6b697712017-11-09 20:27:20 +02005142 /* the PIR and ON have been set by L1. */
5143 if (!kvm_vcpu_trigger_posted_interrupt(vcpu, true))
5144 kvm_vcpu_kick(vcpu);
Wincy Van705699a2015-02-03 23:58:17 +08005145 return 0;
5146 }
5147 return -1;
5148}
Avi Kivity6aa8b732006-12-10 02:21:36 -08005149/*
Yang Zhanga20ed542013-04-11 19:25:15 +08005150 * Send interrupt to vcpu via posted interrupt way.
5151 * 1. If target vcpu is running(non-root mode), send posted interrupt
5152 * notification to vcpu and hardware will sync PIR to vIRR atomically.
5153 * 2. If target vcpu isn't running(root mode), kick it to pick up the
5154 * interrupt from PIR in next vmentry.
5155 */
5156static void vmx_deliver_posted_interrupt(struct kvm_vcpu *vcpu, int vector)
5157{
5158 struct vcpu_vmx *vmx = to_vmx(vcpu);
5159 int r;
5160
Wincy Van705699a2015-02-03 23:58:17 +08005161 r = vmx_deliver_nested_posted_interrupt(vcpu, vector);
5162 if (!r)
5163 return;
5164
Yang Zhanga20ed542013-04-11 19:25:15 +08005165 if (pi_test_and_set_pir(vector, &vmx->pi_desc))
5166 return;
5167
Paolo Bonzinib95234c2016-12-19 13:57:33 +01005168 /* If a previous notification has sent the IPI, nothing to do. */
5169 if (pi_test_and_set_on(&vmx->pi_desc))
5170 return;
5171
Wincy Van06a55242017-04-28 13:13:59 +08005172 if (!kvm_vcpu_trigger_posted_interrupt(vcpu, false))
Yang Zhanga20ed542013-04-11 19:25:15 +08005173 kvm_vcpu_kick(vcpu);
5174}
5175
Avi Kivity6aa8b732006-12-10 02:21:36 -08005176/*
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005177 * Set up the vmcs's constant host-state fields, i.e., host-state fields that
5178 * will not change in the lifetime of the guest.
5179 * Note that host-state that does change is set elsewhere. E.g., host-state
5180 * that is set differently for each CPU is set in vmx_vcpu_load(), not here.
5181 */
Yang Zhanga547c6d2013-04-11 19:25:10 +08005182static void vmx_set_constant_host_state(struct vcpu_vmx *vmx)
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005183{
5184 u32 low32, high32;
5185 unsigned long tmpl;
5186 struct desc_ptr dt;
Andy Lutomirskid6e41f12017-05-28 10:00:17 -07005187 unsigned long cr0, cr3, cr4;
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005188
Andy Lutomirski04ac88a2016-10-31 15:18:45 -07005189 cr0 = read_cr0();
5190 WARN_ON(cr0 & X86_CR0_TS);
5191 vmcs_writel(HOST_CR0, cr0); /* 22.2.3 */
Andy Lutomirskid6e41f12017-05-28 10:00:17 -07005192
5193 /*
5194 * Save the most likely value for this task's CR3 in the VMCS.
5195 * We can't use __get_current_cr3_fast() because we're not atomic.
5196 */
Andy Lutomirski6c690ee2017-06-12 10:26:14 -07005197 cr3 = __read_cr3();
Andy Lutomirskid6e41f12017-05-28 10:00:17 -07005198 vmcs_writel(HOST_CR3, cr3); /* 22.2.3 FIXME: shadow tables */
Ladi Prosek44889942017-09-22 07:53:15 +02005199 vmx->loaded_vmcs->vmcs_host_cr3 = cr3;
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005200
Andy Lutomirskid974baa2014-10-08 09:02:13 -07005201 /* Save the most likely value for this task's CR4 in the VMCS. */
Andy Lutomirski1e02ce42014-10-24 15:58:08 -07005202 cr4 = cr4_read_shadow();
Andy Lutomirskid974baa2014-10-08 09:02:13 -07005203 vmcs_writel(HOST_CR4, cr4); /* 22.2.3, 22.2.5 */
Ladi Prosek44889942017-09-22 07:53:15 +02005204 vmx->loaded_vmcs->vmcs_host_cr4 = cr4;
Andy Lutomirskid974baa2014-10-08 09:02:13 -07005205
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005206 vmcs_write16(HOST_CS_SELECTOR, __KERNEL_CS); /* 22.2.4 */
Avi Kivityb2da15a2012-05-13 19:53:24 +03005207#ifdef CONFIG_X86_64
5208 /*
5209 * Load null selectors, so we can avoid reloading them in
5210 * __vmx_load_host_state(), in case userspace uses the null selectors
5211 * too (the expected case).
5212 */
5213 vmcs_write16(HOST_DS_SELECTOR, 0);
5214 vmcs_write16(HOST_ES_SELECTOR, 0);
5215#else
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005216 vmcs_write16(HOST_DS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
5217 vmcs_write16(HOST_ES_SELECTOR, __KERNEL_DS); /* 22.2.4 */
Avi Kivityb2da15a2012-05-13 19:53:24 +03005218#endif
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005219 vmcs_write16(HOST_SS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
5220 vmcs_write16(HOST_TR_SELECTOR, GDT_ENTRY_TSS*8); /* 22.2.4 */
5221
Juergen Gross87930012017-09-04 12:25:27 +02005222 store_idt(&dt);
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005223 vmcs_writel(HOST_IDTR_BASE, dt.address); /* 22.2.4 */
Yang Zhanga547c6d2013-04-11 19:25:10 +08005224 vmx->host_idt_base = dt.address;
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005225
Avi Kivity83287ea422012-09-16 15:10:57 +03005226 vmcs_writel(HOST_RIP, vmx_return); /* 22.2.5 */
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005227
5228 rdmsr(MSR_IA32_SYSENTER_CS, low32, high32);
5229 vmcs_write32(HOST_IA32_SYSENTER_CS, low32);
5230 rdmsrl(MSR_IA32_SYSENTER_EIP, tmpl);
5231 vmcs_writel(HOST_IA32_SYSENTER_EIP, tmpl); /* 22.2.3 */
5232
5233 if (vmcs_config.vmexit_ctrl & VM_EXIT_LOAD_IA32_PAT) {
5234 rdmsr(MSR_IA32_CR_PAT, low32, high32);
5235 vmcs_write64(HOST_IA32_PAT, low32 | ((u64) high32 << 32));
5236 }
5237}
5238
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005239static void set_cr4_guest_host_mask(struct vcpu_vmx *vmx)
5240{
5241 vmx->vcpu.arch.cr4_guest_owned_bits = KVM_CR4_GUEST_OWNED_BITS;
5242 if (enable_ept)
5243 vmx->vcpu.arch.cr4_guest_owned_bits |= X86_CR4_PGE;
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03005244 if (is_guest_mode(&vmx->vcpu))
5245 vmx->vcpu.arch.cr4_guest_owned_bits &=
5246 ~get_vmcs12(&vmx->vcpu)->cr4_guest_host_mask;
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005247 vmcs_writel(CR4_GUEST_HOST_MASK, ~vmx->vcpu.arch.cr4_guest_owned_bits);
5248}
5249
Yang Zhang01e439b2013-04-11 19:25:12 +08005250static u32 vmx_pin_based_exec_ctrl(struct vcpu_vmx *vmx)
5251{
5252 u32 pin_based_exec_ctrl = vmcs_config.pin_based_exec_ctrl;
5253
Andrey Smetanind62caab2015-11-10 15:36:33 +03005254 if (!kvm_vcpu_apicv_active(&vmx->vcpu))
Yang Zhang01e439b2013-04-11 19:25:12 +08005255 pin_based_exec_ctrl &= ~PIN_BASED_POSTED_INTR;
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01005256
5257 if (!enable_vnmi)
5258 pin_based_exec_ctrl &= ~PIN_BASED_VIRTUAL_NMIS;
5259
Yunhong Jiang64672c92016-06-13 14:19:59 -07005260 /* Enable the preemption timer dynamically */
5261 pin_based_exec_ctrl &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
Yang Zhang01e439b2013-04-11 19:25:12 +08005262 return pin_based_exec_ctrl;
5263}
5264
Andrey Smetanind62caab2015-11-10 15:36:33 +03005265static void vmx_refresh_apicv_exec_ctrl(struct kvm_vcpu *vcpu)
5266{
5267 struct vcpu_vmx *vmx = to_vmx(vcpu);
5268
5269 vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, vmx_pin_based_exec_ctrl(vmx));
Roman Kagan3ce424e2016-05-18 17:48:20 +03005270 if (cpu_has_secondary_exec_ctrls()) {
5271 if (kvm_vcpu_apicv_active(vcpu))
5272 vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL,
5273 SECONDARY_EXEC_APIC_REGISTER_VIRT |
5274 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
5275 else
5276 vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL,
5277 SECONDARY_EXEC_APIC_REGISTER_VIRT |
5278 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
5279 }
5280
5281 if (cpu_has_vmx_msr_bitmap())
5282 vmx_set_msr_bitmap(vcpu);
Andrey Smetanind62caab2015-11-10 15:36:33 +03005283}
5284
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005285static u32 vmx_exec_control(struct vcpu_vmx *vmx)
5286{
5287 u32 exec_control = vmcs_config.cpu_based_exec_ctrl;
Paolo Bonzinid16c2932014-02-21 10:36:37 +01005288
5289 if (vmx->vcpu.arch.switch_db_regs & KVM_DEBUGREG_WONT_EXIT)
5290 exec_control &= ~CPU_BASED_MOV_DR_EXITING;
5291
Paolo Bonzini35754c92015-07-29 12:05:37 +02005292 if (!cpu_need_tpr_shadow(&vmx->vcpu)) {
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005293 exec_control &= ~CPU_BASED_TPR_SHADOW;
5294#ifdef CONFIG_X86_64
5295 exec_control |= CPU_BASED_CR8_STORE_EXITING |
5296 CPU_BASED_CR8_LOAD_EXITING;
5297#endif
5298 }
5299 if (!enable_ept)
5300 exec_control |= CPU_BASED_CR3_STORE_EXITING |
5301 CPU_BASED_CR3_LOAD_EXITING |
5302 CPU_BASED_INVLPG_EXITING;
5303 return exec_control;
5304}
5305
Jim Mattson45ec3682017-08-23 16:32:04 -07005306static bool vmx_rdrand_supported(void)
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005307{
Jim Mattson45ec3682017-08-23 16:32:04 -07005308 return vmcs_config.cpu_based_2nd_exec_ctrl &
David Hildenbrand736fdf72017-08-24 20:51:37 +02005309 SECONDARY_EXEC_RDRAND_EXITING;
Jim Mattson45ec3682017-08-23 16:32:04 -07005310}
5311
Jim Mattson75f4fc82017-08-23 16:32:03 -07005312static bool vmx_rdseed_supported(void)
5313{
5314 return vmcs_config.cpu_based_2nd_exec_ctrl &
David Hildenbrand736fdf72017-08-24 20:51:37 +02005315 SECONDARY_EXEC_RDSEED_EXITING;
Jim Mattson75f4fc82017-08-23 16:32:03 -07005316}
5317
Paolo Bonzini80154d72017-08-24 13:55:35 +02005318static void vmx_compute_secondary_exec_control(struct vcpu_vmx *vmx)
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005319{
Paolo Bonzini80154d72017-08-24 13:55:35 +02005320 struct kvm_vcpu *vcpu = &vmx->vcpu;
5321
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005322 u32 exec_control = vmcs_config.cpu_based_2nd_exec_ctrl;
Paolo Bonzini0367f202016-07-12 10:44:55 +02005323
Paolo Bonzini80154d72017-08-24 13:55:35 +02005324 if (!cpu_need_virtualize_apic_accesses(vcpu))
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005325 exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
5326 if (vmx->vpid == 0)
5327 exec_control &= ~SECONDARY_EXEC_ENABLE_VPID;
5328 if (!enable_ept) {
5329 exec_control &= ~SECONDARY_EXEC_ENABLE_EPT;
5330 enable_unrestricted_guest = 0;
Mao, Junjiead756a12012-07-02 01:18:48 +00005331 /* Enable INVPCID for non-ept guests may cause performance regression. */
5332 exec_control &= ~SECONDARY_EXEC_ENABLE_INVPCID;
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005333 }
5334 if (!enable_unrestricted_guest)
5335 exec_control &= ~SECONDARY_EXEC_UNRESTRICTED_GUEST;
5336 if (!ple_gap)
5337 exec_control &= ~SECONDARY_EXEC_PAUSE_LOOP_EXITING;
Paolo Bonzini80154d72017-08-24 13:55:35 +02005338 if (!kvm_vcpu_apicv_active(vcpu))
Yang Zhangc7c9c562013-01-25 10:18:51 +08005339 exec_control &= ~(SECONDARY_EXEC_APIC_REGISTER_VIRT |
5340 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
Yang Zhang8d146952013-01-25 10:18:50 +08005341 exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
Paolo Bonzini0367f202016-07-12 10:44:55 +02005342
5343 /* SECONDARY_EXEC_DESC is enabled/disabled on writes to CR4.UMIP,
5344 * in vmx_set_cr4. */
5345 exec_control &= ~SECONDARY_EXEC_DESC;
5346
Abel Gordonabc4fc52013-04-18 14:35:25 +03005347 /* SECONDARY_EXEC_SHADOW_VMCS is enabled when L1 executes VMPTRLD
5348 (handle_vmptrld).
5349 We can NOT enable shadow_vmcs here because we don't have yet
5350 a current VMCS12
5351 */
5352 exec_control &= ~SECONDARY_EXEC_SHADOW_VMCS;
Kai Huanga3eaa862015-11-04 13:46:05 +08005353
5354 if (!enable_pml)
5355 exec_control &= ~SECONDARY_EXEC_ENABLE_PML;
Kai Huang843e4332015-01-28 10:54:28 +08005356
Paolo Bonzini3db13482017-08-24 14:48:03 +02005357 if (vmx_xsaves_supported()) {
5358 /* Exposing XSAVES only when XSAVE is exposed */
5359 bool xsaves_enabled =
5360 guest_cpuid_has(vcpu, X86_FEATURE_XSAVE) &&
5361 guest_cpuid_has(vcpu, X86_FEATURE_XSAVES);
5362
5363 if (!xsaves_enabled)
5364 exec_control &= ~SECONDARY_EXEC_XSAVES;
5365
5366 if (nested) {
5367 if (xsaves_enabled)
5368 vmx->nested.nested_vmx_secondary_ctls_high |=
5369 SECONDARY_EXEC_XSAVES;
5370 else
5371 vmx->nested.nested_vmx_secondary_ctls_high &=
5372 ~SECONDARY_EXEC_XSAVES;
5373 }
5374 }
5375
Paolo Bonzini80154d72017-08-24 13:55:35 +02005376 if (vmx_rdtscp_supported()) {
5377 bool rdtscp_enabled = guest_cpuid_has(vcpu, X86_FEATURE_RDTSCP);
5378 if (!rdtscp_enabled)
5379 exec_control &= ~SECONDARY_EXEC_RDTSCP;
5380
5381 if (nested) {
5382 if (rdtscp_enabled)
5383 vmx->nested.nested_vmx_secondary_ctls_high |=
5384 SECONDARY_EXEC_RDTSCP;
5385 else
5386 vmx->nested.nested_vmx_secondary_ctls_high &=
5387 ~SECONDARY_EXEC_RDTSCP;
5388 }
5389 }
5390
5391 if (vmx_invpcid_supported()) {
5392 /* Exposing INVPCID only when PCID is exposed */
5393 bool invpcid_enabled =
5394 guest_cpuid_has(vcpu, X86_FEATURE_INVPCID) &&
5395 guest_cpuid_has(vcpu, X86_FEATURE_PCID);
5396
5397 if (!invpcid_enabled) {
5398 exec_control &= ~SECONDARY_EXEC_ENABLE_INVPCID;
5399 guest_cpuid_clear(vcpu, X86_FEATURE_INVPCID);
5400 }
5401
5402 if (nested) {
5403 if (invpcid_enabled)
5404 vmx->nested.nested_vmx_secondary_ctls_high |=
5405 SECONDARY_EXEC_ENABLE_INVPCID;
5406 else
5407 vmx->nested.nested_vmx_secondary_ctls_high &=
5408 ~SECONDARY_EXEC_ENABLE_INVPCID;
5409 }
5410 }
5411
Jim Mattson45ec3682017-08-23 16:32:04 -07005412 if (vmx_rdrand_supported()) {
5413 bool rdrand_enabled = guest_cpuid_has(vcpu, X86_FEATURE_RDRAND);
5414 if (rdrand_enabled)
David Hildenbrand736fdf72017-08-24 20:51:37 +02005415 exec_control &= ~SECONDARY_EXEC_RDRAND_EXITING;
Jim Mattson45ec3682017-08-23 16:32:04 -07005416
5417 if (nested) {
5418 if (rdrand_enabled)
5419 vmx->nested.nested_vmx_secondary_ctls_high |=
David Hildenbrand736fdf72017-08-24 20:51:37 +02005420 SECONDARY_EXEC_RDRAND_EXITING;
Jim Mattson45ec3682017-08-23 16:32:04 -07005421 else
5422 vmx->nested.nested_vmx_secondary_ctls_high &=
David Hildenbrand736fdf72017-08-24 20:51:37 +02005423 ~SECONDARY_EXEC_RDRAND_EXITING;
Jim Mattson45ec3682017-08-23 16:32:04 -07005424 }
5425 }
5426
Jim Mattson75f4fc82017-08-23 16:32:03 -07005427 if (vmx_rdseed_supported()) {
5428 bool rdseed_enabled = guest_cpuid_has(vcpu, X86_FEATURE_RDSEED);
5429 if (rdseed_enabled)
David Hildenbrand736fdf72017-08-24 20:51:37 +02005430 exec_control &= ~SECONDARY_EXEC_RDSEED_EXITING;
Jim Mattson75f4fc82017-08-23 16:32:03 -07005431
5432 if (nested) {
5433 if (rdseed_enabled)
5434 vmx->nested.nested_vmx_secondary_ctls_high |=
David Hildenbrand736fdf72017-08-24 20:51:37 +02005435 SECONDARY_EXEC_RDSEED_EXITING;
Jim Mattson75f4fc82017-08-23 16:32:03 -07005436 else
5437 vmx->nested.nested_vmx_secondary_ctls_high &=
David Hildenbrand736fdf72017-08-24 20:51:37 +02005438 ~SECONDARY_EXEC_RDSEED_EXITING;
Jim Mattson75f4fc82017-08-23 16:32:03 -07005439 }
5440 }
5441
Paolo Bonzini80154d72017-08-24 13:55:35 +02005442 vmx->secondary_exec_control = exec_control;
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005443}
5444
Xiao Guangrongce88dec2011-07-12 03:33:44 +08005445static void ept_set_mmio_spte_mask(void)
5446{
5447 /*
5448 * EPT Misconfigurations can be generated if the value of bits 2:0
5449 * of an EPT paging-structure entry is 110b (write/execute).
Xiao Guangrongce88dec2011-07-12 03:33:44 +08005450 */
Peter Feinerdcdca5f2017-06-30 17:26:30 -07005451 kvm_mmu_set_mmio_spte_mask(VMX_EPT_RWX_MASK,
5452 VMX_EPT_MISCONFIG_WX_VALUE);
Xiao Guangrongce88dec2011-07-12 03:33:44 +08005453}
5454
Wanpeng Lif53cd632014-12-02 19:14:58 +08005455#define VMX_XSS_EXIT_BITMAP 0
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005456/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08005457 * Sets up the vmcs for emulated real mode.
5458 */
David Hildenbrand12d79912017-08-24 20:51:26 +02005459static void vmx_vcpu_setup(struct vcpu_vmx *vmx)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005460{
Jan Kiszka2e4ce7f2011-06-01 12:57:30 +02005461#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08005462 unsigned long a;
Jan Kiszka2e4ce7f2011-06-01 12:57:30 +02005463#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -08005464 int i;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005465
Abel Gordon4607c2d2013-04-18 14:35:55 +03005466 if (enable_shadow_vmcs) {
5467 vmcs_write64(VMREAD_BITMAP, __pa(vmx_vmread_bitmap));
5468 vmcs_write64(VMWRITE_BITMAP, __pa(vmx_vmwrite_bitmap));
5469 }
Sheng Yang25c5f222008-03-28 13:18:56 +08005470 if (cpu_has_vmx_msr_bitmap())
Avi Kivity58972972009-02-24 22:26:47 +02005471 vmcs_write64(MSR_BITMAP, __pa(vmx_msr_bitmap_legacy));
Sheng Yang25c5f222008-03-28 13:18:56 +08005472
Avi Kivity6aa8b732006-12-10 02:21:36 -08005473 vmcs_write64(VMCS_LINK_POINTER, -1ull); /* 22.3.1.5 */
5474
Avi Kivity6aa8b732006-12-10 02:21:36 -08005475 /* Control */
Yang Zhang01e439b2013-04-11 19:25:12 +08005476 vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, vmx_pin_based_exec_ctrl(vmx));
Yunhong Jiang64672c92016-06-13 14:19:59 -07005477 vmx->hv_deadline_tsc = -1;
Yang, Sheng6e5d8652007-09-12 18:03:11 +08005478
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005479 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, vmx_exec_control(vmx));
Avi Kivity6aa8b732006-12-10 02:21:36 -08005480
Dan Williamsdfa169b2016-06-02 11:17:24 -07005481 if (cpu_has_secondary_exec_ctrls()) {
Paolo Bonzini80154d72017-08-24 13:55:35 +02005482 vmx_compute_secondary_exec_control(vmx);
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005483 vmcs_write32(SECONDARY_VM_EXEC_CONTROL,
Paolo Bonzini80154d72017-08-24 13:55:35 +02005484 vmx->secondary_exec_control);
Dan Williamsdfa169b2016-06-02 11:17:24 -07005485 }
Sheng Yangf78e0e22007-10-29 09:40:42 +08005486
Andrey Smetanind62caab2015-11-10 15:36:33 +03005487 if (kvm_vcpu_apicv_active(&vmx->vcpu)) {
Yang Zhangc7c9c562013-01-25 10:18:51 +08005488 vmcs_write64(EOI_EXIT_BITMAP0, 0);
5489 vmcs_write64(EOI_EXIT_BITMAP1, 0);
5490 vmcs_write64(EOI_EXIT_BITMAP2, 0);
5491 vmcs_write64(EOI_EXIT_BITMAP3, 0);
5492
5493 vmcs_write16(GUEST_INTR_STATUS, 0);
Yang Zhang01e439b2013-04-11 19:25:12 +08005494
Li RongQing0bcf2612015-12-03 13:29:34 +08005495 vmcs_write16(POSTED_INTR_NV, POSTED_INTR_VECTOR);
Yang Zhang01e439b2013-04-11 19:25:12 +08005496 vmcs_write64(POSTED_INTR_DESC_ADDR, __pa((&vmx->pi_desc)));
Yang Zhangc7c9c562013-01-25 10:18:51 +08005497 }
5498
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08005499 if (ple_gap) {
5500 vmcs_write32(PLE_GAP, ple_gap);
Radim Krčmářa7653ec2014-08-21 18:08:07 +02005501 vmx->ple_window = ple_window;
5502 vmx->ple_window_dirty = true;
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08005503 }
5504
Xiao Guangrongc3707952011-07-12 03:28:04 +08005505 vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK, 0);
5506 vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005507 vmcs_write32(CR3_TARGET_COUNT, 0); /* 22.2.1 */
5508
Avi Kivity9581d442010-10-19 16:46:55 +02005509 vmcs_write16(HOST_FS_SELECTOR, 0); /* 22.2.4 */
5510 vmcs_write16(HOST_GS_SELECTOR, 0); /* 22.2.4 */
Yang Zhanga547c6d2013-04-11 19:25:10 +08005511 vmx_set_constant_host_state(vmx);
Avi Kivity05b3e0c2006-12-13 00:33:45 -08005512#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08005513 rdmsrl(MSR_FS_BASE, a);
5514 vmcs_writel(HOST_FS_BASE, a); /* 22.2.4 */
5515 rdmsrl(MSR_GS_BASE, a);
5516 vmcs_writel(HOST_GS_BASE, a); /* 22.2.4 */
5517#else
5518 vmcs_writel(HOST_FS_BASE, 0); /* 22.2.4 */
5519 vmcs_writel(HOST_GS_BASE, 0); /* 22.2.4 */
5520#endif
5521
Bandan Das2a499e42017-08-03 15:54:41 -04005522 if (cpu_has_vmx_vmfunc())
5523 vmcs_write64(VM_FUNCTION_CONTROL, 0);
5524
Eddie Dong2cc51562007-05-21 07:28:09 +03005525 vmcs_write32(VM_EXIT_MSR_STORE_COUNT, 0);
5526 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, 0);
Avi Kivity61d2ef22010-04-28 16:40:38 +03005527 vmcs_write64(VM_EXIT_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.host));
Eddie Dong2cc51562007-05-21 07:28:09 +03005528 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, 0);
Avi Kivity61d2ef22010-04-28 16:40:38 +03005529 vmcs_write64(VM_ENTRY_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.guest));
Avi Kivity6aa8b732006-12-10 02:21:36 -08005530
Radim Krčmář74545702015-04-27 15:11:25 +02005531 if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT)
5532 vmcs_write64(GUEST_IA32_PAT, vmx->vcpu.arch.pat);
Sheng Yang468d4722008-10-09 16:01:55 +08005533
Paolo Bonzini03916db2014-07-24 14:21:57 +02005534 for (i = 0; i < ARRAY_SIZE(vmx_msr_index); ++i) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08005535 u32 index = vmx_msr_index[i];
5536 u32 data_low, data_high;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04005537 int j = vmx->nmsrs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005538
5539 if (rdmsr_safe(index, &data_low, &data_high) < 0)
5540 continue;
Avi Kivity432bd6c2007-01-31 23:48:13 -08005541 if (wrmsr_safe(index, data_low, data_high) < 0)
5542 continue;
Avi Kivity26bb0982009-09-07 11:14:12 +03005543 vmx->guest_msrs[j].index = i;
5544 vmx->guest_msrs[j].data = 0;
Avi Kivityd5696722009-12-02 12:28:47 +02005545 vmx->guest_msrs[j].mask = -1ull;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04005546 ++vmx->nmsrs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005547 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08005548
Gleb Natapov2961e8762013-11-25 15:37:13 +02005549
5550 vm_exit_controls_init(vmx, vmcs_config.vmexit_ctrl);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005551
5552 /* 22.2.1, 20.8.1 */
Gleb Natapov2961e8762013-11-25 15:37:13 +02005553 vm_entry_controls_init(vmx, vmcs_config.vmentry_ctrl);
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03005554
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -08005555 vmx->vcpu.arch.cr0_guest_owned_bits = X86_CR0_TS;
5556 vmcs_writel(CR0_GUEST_HOST_MASK, ~X86_CR0_TS);
5557
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005558 set_cr4_guest_host_mask(vmx);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005559
Wanpeng Lif53cd632014-12-02 19:14:58 +08005560 if (vmx_xsaves_supported())
5561 vmcs_write64(XSS_EXIT_BITMAP, VMX_XSS_EXIT_BITMAP);
5562
Peter Feiner4e595162016-07-07 14:49:58 -07005563 if (enable_pml) {
5564 ASSERT(vmx->pml_pg);
5565 vmcs_write64(PML_ADDRESS, page_to_phys(vmx->pml_pg));
5566 vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1);
5567 }
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005568}
5569
Nadav Amitd28bc9d2015-04-13 14:34:08 +03005570static void vmx_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event)
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005571{
5572 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jan Kiszka58cb6282014-01-24 16:48:44 +01005573 struct msr_data apic_base_msr;
Nadav Amitd28bc9d2015-04-13 14:34:08 +03005574 u64 cr0;
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005575
Avi Kivity7ffd92c2009-06-09 14:10:45 +03005576 vmx->rmode.vm86_active = 0;
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005577
Zhang Xiantaoad312c72007-12-13 23:50:52 +08005578 vmx->vcpu.arch.regs[VCPU_REGS_RDX] = get_rdx_init_val();
Nadav Amitd28bc9d2015-04-13 14:34:08 +03005579 kvm_set_cr8(vcpu, 0);
5580
5581 if (!init_event) {
5582 apic_base_msr.data = APIC_DEFAULT_PHYS_BASE |
5583 MSR_IA32_APICBASE_ENABLE;
5584 if (kvm_vcpu_is_reset_bsp(vcpu))
5585 apic_base_msr.data |= MSR_IA32_APICBASE_BSP;
5586 apic_base_msr.host_initiated = true;
5587 kvm_set_apic_base(vcpu, &apic_base_msr);
5588 }
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005589
Avi Kivity2fb92db2011-04-27 19:42:18 +03005590 vmx_segment_cache_clear(vmx);
5591
Avi Kivity5706be02008-08-20 15:07:31 +03005592 seg_setup(VCPU_SREG_CS);
Jan Kiszka66450a22013-03-13 12:42:34 +01005593 vmcs_write16(GUEST_CS_SELECTOR, 0xf000);
Paolo Bonzinif3531052015-12-03 15:49:56 +01005594 vmcs_writel(GUEST_CS_BASE, 0xffff0000ul);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005595
5596 seg_setup(VCPU_SREG_DS);
5597 seg_setup(VCPU_SREG_ES);
5598 seg_setup(VCPU_SREG_FS);
5599 seg_setup(VCPU_SREG_GS);
5600 seg_setup(VCPU_SREG_SS);
5601
5602 vmcs_write16(GUEST_TR_SELECTOR, 0);
5603 vmcs_writel(GUEST_TR_BASE, 0);
5604 vmcs_write32(GUEST_TR_LIMIT, 0xffff);
5605 vmcs_write32(GUEST_TR_AR_BYTES, 0x008b);
5606
5607 vmcs_write16(GUEST_LDTR_SELECTOR, 0);
5608 vmcs_writel(GUEST_LDTR_BASE, 0);
5609 vmcs_write32(GUEST_LDTR_LIMIT, 0xffff);
5610 vmcs_write32(GUEST_LDTR_AR_BYTES, 0x00082);
5611
Nadav Amitd28bc9d2015-04-13 14:34:08 +03005612 if (!init_event) {
5613 vmcs_write32(GUEST_SYSENTER_CS, 0);
5614 vmcs_writel(GUEST_SYSENTER_ESP, 0);
5615 vmcs_writel(GUEST_SYSENTER_EIP, 0);
5616 vmcs_write64(GUEST_IA32_DEBUGCTL, 0);
5617 }
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005618
Wanpeng Lic37c2872017-11-20 14:52:21 -08005619 kvm_set_rflags(vcpu, X86_EFLAGS_FIXED);
Jan Kiszka66450a22013-03-13 12:42:34 +01005620 kvm_rip_write(vcpu, 0xfff0);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005621
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005622 vmcs_writel(GUEST_GDTR_BASE, 0);
5623 vmcs_write32(GUEST_GDTR_LIMIT, 0xffff);
5624
5625 vmcs_writel(GUEST_IDTR_BASE, 0);
5626 vmcs_write32(GUEST_IDTR_LIMIT, 0xffff);
5627
Anthony Liguori443381a2010-12-06 10:53:38 -06005628 vmcs_write32(GUEST_ACTIVITY_STATE, GUEST_ACTIVITY_ACTIVE);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005629 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, 0);
Paolo Bonzinif3531052015-12-03 15:49:56 +01005630 vmcs_writel(GUEST_PENDING_DBG_EXCEPTIONS, 0);
Wanpeng Lia554d202017-10-11 05:10:19 -07005631 if (kvm_mpx_supported())
5632 vmcs_write64(GUEST_BNDCFGS, 0);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005633
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005634 setup_msrs(vmx);
5635
Avi Kivity6aa8b732006-12-10 02:21:36 -08005636 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0); /* 22.2.1 */
5637
Nadav Amitd28bc9d2015-04-13 14:34:08 +03005638 if (cpu_has_vmx_tpr_shadow() && !init_event) {
Sheng Yangf78e0e22007-10-29 09:40:42 +08005639 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, 0);
Paolo Bonzini35754c92015-07-29 12:05:37 +02005640 if (cpu_need_tpr_shadow(vcpu))
Sheng Yangf78e0e22007-10-29 09:40:42 +08005641 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR,
Nadav Amitd28bc9d2015-04-13 14:34:08 +03005642 __pa(vcpu->arch.apic->regs));
Sheng Yangf78e0e22007-10-29 09:40:42 +08005643 vmcs_write32(TPR_THRESHOLD, 0);
5644 }
5645
Paolo Bonzinia73896c2014-11-02 07:54:30 +01005646 kvm_make_request(KVM_REQ_APIC_PAGE_RELOAD, vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005647
Sheng Yang2384d2b2008-01-17 15:14:33 +08005648 if (vmx->vpid != 0)
5649 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid);
5650
Nadav Amitd28bc9d2015-04-13 14:34:08 +03005651 cr0 = X86_CR0_NW | X86_CR0_CD | X86_CR0_ET;
Nadav Amitd28bc9d2015-04-13 14:34:08 +03005652 vmx->vcpu.arch.cr0 = cr0;
Bruce Rogersf2463242016-04-28 14:49:21 -06005653 vmx_set_cr0(vcpu, cr0); /* enter rmode */
Nadav Amitd28bc9d2015-04-13 14:34:08 +03005654 vmx_set_cr4(vcpu, 0);
Paolo Bonzini56908912015-10-19 11:30:19 +02005655 vmx_set_efer(vcpu, 0);
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -08005656
Nadav Amitd28bc9d2015-04-13 14:34:08 +03005657 update_exception_bitmap(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005658
Wanpeng Lidd5f5342015-09-23 18:26:57 +08005659 vpid_sync_context(vmx->vpid);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005660}
5661
Nadav Har'Elb6f12502011-05-25 23:13:06 +03005662/*
5663 * In nested virtualization, check if L1 asked to exit on external interrupts.
5664 * For most existing hypervisors, this will always return true.
5665 */
5666static bool nested_exit_on_intr(struct kvm_vcpu *vcpu)
5667{
5668 return get_vmcs12(vcpu)->pin_based_vm_exec_control &
5669 PIN_BASED_EXT_INTR_MASK;
5670}
5671
Bandan Das77b0f5d2014-04-19 18:17:45 -04005672/*
5673 * In nested virtualization, check if L1 has set
5674 * VM_EXIT_ACK_INTR_ON_EXIT
5675 */
5676static bool nested_exit_intr_ack_set(struct kvm_vcpu *vcpu)
5677{
5678 return get_vmcs12(vcpu)->vm_exit_controls &
5679 VM_EXIT_ACK_INTR_ON_EXIT;
5680}
5681
Jan Kiszkaea8ceb82013-04-14 21:04:26 +02005682static bool nested_exit_on_nmi(struct kvm_vcpu *vcpu)
5683{
5684 return get_vmcs12(vcpu)->pin_based_vm_exec_control &
5685 PIN_BASED_NMI_EXITING;
5686}
5687
Jan Kiszkac9a79532014-03-07 20:03:15 +01005688static void enable_irq_window(struct kvm_vcpu *vcpu)
Jan Kiszka3b86cd92008-09-26 09:30:57 +02005689{
Paolo Bonzini47c01522016-12-19 11:44:07 +01005690 vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL,
5691 CPU_BASED_VIRTUAL_INTR_PENDING);
Jan Kiszka3b86cd92008-09-26 09:30:57 +02005692}
5693
Jan Kiszkac9a79532014-03-07 20:03:15 +01005694static void enable_nmi_window(struct kvm_vcpu *vcpu)
Jan Kiszka3b86cd92008-09-26 09:30:57 +02005695{
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01005696 if (!enable_vnmi ||
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01005697 vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & GUEST_INTR_STATE_STI) {
Jan Kiszkac9a79532014-03-07 20:03:15 +01005698 enable_irq_window(vcpu);
5699 return;
5700 }
Jan Kiszka03b28f82013-04-29 16:46:42 +02005701
Paolo Bonzini47c01522016-12-19 11:44:07 +01005702 vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL,
5703 CPU_BASED_VIRTUAL_NMI_PENDING);
Jan Kiszka3b86cd92008-09-26 09:30:57 +02005704}
5705
Gleb Natapov66fd3f72009-05-11 13:35:50 +03005706static void vmx_inject_irq(struct kvm_vcpu *vcpu)
Eddie Dong85f455f2007-07-06 12:20:49 +03005707{
Avi Kivity9c8cba32007-11-22 11:42:59 +02005708 struct vcpu_vmx *vmx = to_vmx(vcpu);
Gleb Natapov66fd3f72009-05-11 13:35:50 +03005709 uint32_t intr;
5710 int irq = vcpu->arch.interrupt.nr;
Avi Kivity9c8cba32007-11-22 11:42:59 +02005711
Marcelo Tosatti229456f2009-06-17 09:22:14 -03005712 trace_kvm_inj_virq(irq);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04005713
Avi Kivityfa89a812008-09-01 15:57:51 +03005714 ++vcpu->stat.irq_injections;
Avi Kivity7ffd92c2009-06-09 14:10:45 +03005715 if (vmx->rmode.vm86_active) {
Serge E. Hallyn71f98332011-04-13 09:12:54 -05005716 int inc_eip = 0;
5717 if (vcpu->arch.interrupt.soft)
5718 inc_eip = vcpu->arch.event_exit_inst_len;
5719 if (kvm_inject_realmode_interrupt(vcpu, irq, inc_eip) != EMULATE_DONE)
Mohammed Gamala92601b2010-09-19 14:34:07 +02005720 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Eddie Dong85f455f2007-07-06 12:20:49 +03005721 return;
5722 }
Gleb Natapov66fd3f72009-05-11 13:35:50 +03005723 intr = irq | INTR_INFO_VALID_MASK;
5724 if (vcpu->arch.interrupt.soft) {
5725 intr |= INTR_TYPE_SOFT_INTR;
5726 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
5727 vmx->vcpu.arch.event_exit_inst_len);
5728 } else
5729 intr |= INTR_TYPE_EXT_INTR;
5730 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr);
Eddie Dong85f455f2007-07-06 12:20:49 +03005731}
5732
Sheng Yangf08864b2008-05-15 18:23:25 +08005733static void vmx_inject_nmi(struct kvm_vcpu *vcpu)
5734{
Jan Kiszka66a5a342008-09-26 09:30:51 +02005735 struct vcpu_vmx *vmx = to_vmx(vcpu);
5736
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01005737 if (!enable_vnmi) {
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01005738 /*
5739 * Tracking the NMI-blocked state in software is built upon
5740 * finding the next open IRQ window. This, in turn, depends on
5741 * well-behaving guests: They have to keep IRQs disabled at
5742 * least as long as the NMI handler runs. Otherwise we may
5743 * cause NMI nesting, maybe breaking the guest. But as this is
5744 * highly unlikely, we can live with the residual risk.
5745 */
5746 vmx->loaded_vmcs->soft_vnmi_blocked = 1;
5747 vmx->loaded_vmcs->vnmi_blocked_time = 0;
5748 }
5749
Paolo Bonzini4c4a6f72017-07-14 13:36:11 +02005750 ++vcpu->stat.nmi_injections;
5751 vmx->loaded_vmcs->nmi_known_unmasked = false;
Jan Kiszka3b86cd92008-09-26 09:30:57 +02005752
Avi Kivity7ffd92c2009-06-09 14:10:45 +03005753 if (vmx->rmode.vm86_active) {
Serge E. Hallyn71f98332011-04-13 09:12:54 -05005754 if (kvm_inject_realmode_interrupt(vcpu, NMI_VECTOR, 0) != EMULATE_DONE)
Mohammed Gamala92601b2010-09-19 14:34:07 +02005755 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Jan Kiszka66a5a342008-09-26 09:30:51 +02005756 return;
5757 }
Wanpeng Lic5a6d5f2016-09-22 17:55:54 +08005758
Sheng Yangf08864b2008-05-15 18:23:25 +08005759 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
5760 INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK | NMI_VECTOR);
Sheng Yangf08864b2008-05-15 18:23:25 +08005761}
5762
Jan Kiszka3cfc3092009-11-12 01:04:25 +01005763static bool vmx_get_nmi_mask(struct kvm_vcpu *vcpu)
5764{
Paolo Bonzini4c4a6f72017-07-14 13:36:11 +02005765 struct vcpu_vmx *vmx = to_vmx(vcpu);
5766 bool masked;
5767
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01005768 if (!enable_vnmi)
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01005769 return vmx->loaded_vmcs->soft_vnmi_blocked;
Paolo Bonzini4c4a6f72017-07-14 13:36:11 +02005770 if (vmx->loaded_vmcs->nmi_known_unmasked)
Avi Kivity9d58b932011-03-07 16:52:07 +02005771 return false;
Paolo Bonzini4c4a6f72017-07-14 13:36:11 +02005772 masked = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & GUEST_INTR_STATE_NMI;
5773 vmx->loaded_vmcs->nmi_known_unmasked = !masked;
5774 return masked;
Jan Kiszka3cfc3092009-11-12 01:04:25 +01005775}
5776
5777static void vmx_set_nmi_mask(struct kvm_vcpu *vcpu, bool masked)
5778{
5779 struct vcpu_vmx *vmx = to_vmx(vcpu);
5780
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01005781 if (!enable_vnmi) {
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01005782 if (vmx->loaded_vmcs->soft_vnmi_blocked != masked) {
5783 vmx->loaded_vmcs->soft_vnmi_blocked = masked;
5784 vmx->loaded_vmcs->vnmi_blocked_time = 0;
5785 }
5786 } else {
5787 vmx->loaded_vmcs->nmi_known_unmasked = !masked;
5788 if (masked)
5789 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
5790 GUEST_INTR_STATE_NMI);
5791 else
5792 vmcs_clear_bits(GUEST_INTERRUPTIBILITY_INFO,
5793 GUEST_INTR_STATE_NMI);
5794 }
Jan Kiszka3cfc3092009-11-12 01:04:25 +01005795}
5796
Jan Kiszka2505dc92013-04-14 12:12:47 +02005797static int vmx_nmi_allowed(struct kvm_vcpu *vcpu)
5798{
Jan Kiszkab6b8a142014-03-07 20:03:12 +01005799 if (to_vmx(vcpu)->nested.nested_run_pending)
5800 return 0;
Jan Kiszkaea8ceb82013-04-14 21:04:26 +02005801
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01005802 if (!enable_vnmi &&
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01005803 to_vmx(vcpu)->loaded_vmcs->soft_vnmi_blocked)
5804 return 0;
5805
Jan Kiszka2505dc92013-04-14 12:12:47 +02005806 return !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) &
5807 (GUEST_INTR_STATE_MOV_SS | GUEST_INTR_STATE_STI
5808 | GUEST_INTR_STATE_NMI));
5809}
5810
Gleb Natapov78646122009-03-23 12:12:11 +02005811static int vmx_interrupt_allowed(struct kvm_vcpu *vcpu)
5812{
Jan Kiszkab6b8a142014-03-07 20:03:12 +01005813 return (!to_vmx(vcpu)->nested.nested_run_pending &&
5814 vmcs_readl(GUEST_RFLAGS) & X86_EFLAGS_IF) &&
Gleb Natapovc4282df2009-04-21 17:45:07 +03005815 !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) &
5816 (GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS));
Gleb Natapov78646122009-03-23 12:12:11 +02005817}
5818
Izik Eiduscbc94022007-10-25 00:29:55 +02005819static int vmx_set_tss_addr(struct kvm *kvm, unsigned int addr)
5820{
5821 int ret;
Izik Eiduscbc94022007-10-25 00:29:55 +02005822
Paolo Bonzini1d8007b2015-10-12 13:38:32 +02005823 ret = x86_set_memory_region(kvm, TSS_PRIVATE_MEMSLOT, addr,
5824 PAGE_SIZE * 3);
Izik Eiduscbc94022007-10-25 00:29:55 +02005825 if (ret)
5826 return ret;
Zhang Xiantaobfc6d222007-12-14 10:20:16 +08005827 kvm->arch.tss_addr = addr;
Paolo Bonzini1f755a82014-09-16 13:37:40 +02005828 return init_rmode_tss(kvm);
Izik Eiduscbc94022007-10-25 00:29:55 +02005829}
5830
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02005831static bool rmode_exception(struct kvm_vcpu *vcpu, int vec)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005832{
Jan Kiszka77ab6db2008-07-14 12:28:51 +02005833 switch (vec) {
Jan Kiszka77ab6db2008-07-14 12:28:51 +02005834 case BP_VECTOR:
Jan Kiszkac573cd22010-02-23 17:47:53 +01005835 /*
5836 * Update instruction length as we may reinject the exception
5837 * from user space while in guest debugging mode.
5838 */
5839 to_vmx(vcpu)->vcpu.arch.event_exit_inst_len =
5840 vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
Jan Kiszkad0bfb942008-12-15 13:52:10 +01005841 if (vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02005842 return false;
5843 /* fall through */
5844 case DB_VECTOR:
5845 if (vcpu->guest_debug &
5846 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))
5847 return false;
Jan Kiszkad0bfb942008-12-15 13:52:10 +01005848 /* fall through */
5849 case DE_VECTOR:
Jan Kiszka77ab6db2008-07-14 12:28:51 +02005850 case OF_VECTOR:
5851 case BR_VECTOR:
5852 case UD_VECTOR:
5853 case DF_VECTOR:
5854 case SS_VECTOR:
5855 case GP_VECTOR:
5856 case MF_VECTOR:
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02005857 return true;
5858 break;
Jan Kiszka77ab6db2008-07-14 12:28:51 +02005859 }
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02005860 return false;
5861}
5862
5863static int handle_rmode_exception(struct kvm_vcpu *vcpu,
5864 int vec, u32 err_code)
5865{
5866 /*
5867 * Instruction with address size override prefix opcode 0x67
5868 * Cause the #SS fault with 0 error code in VM86 mode.
5869 */
5870 if (((vec == GP_VECTOR) || (vec == SS_VECTOR)) && err_code == 0) {
5871 if (emulate_instruction(vcpu, 0) == EMULATE_DONE) {
5872 if (vcpu->arch.halt_request) {
5873 vcpu->arch.halt_request = 0;
Joel Schopp5cb56052015-03-02 13:43:31 -06005874 return kvm_vcpu_halt(vcpu);
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02005875 }
5876 return 1;
5877 }
5878 return 0;
5879 }
5880
5881 /*
5882 * Forward all other exceptions that are valid in real mode.
5883 * FIXME: Breaks guest debugging in real mode, needs to be fixed with
5884 * the required debugging infrastructure rework.
5885 */
5886 kvm_queue_exception(vcpu, vec);
5887 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005888}
5889
Andi Kleena0861c02009-06-08 17:37:09 +08005890/*
5891 * Trigger machine check on the host. We assume all the MSRs are already set up
5892 * by the CPU and that we still run on the same CPU as the MCE occurred on.
5893 * We pass a fake environment to the machine check handler because we want
5894 * the guest to be always treated like user space, no matter what context
5895 * it used internally.
5896 */
5897static void kvm_machine_check(void)
5898{
5899#if defined(CONFIG_X86_MCE) && defined(CONFIG_X86_64)
5900 struct pt_regs regs = {
5901 .cs = 3, /* Fake ring 3 no matter what the guest ran on */
5902 .flags = X86_EFLAGS_IF,
5903 };
5904
5905 do_machine_check(&regs, 0);
5906#endif
5907}
5908
Avi Kivity851ba692009-08-24 11:10:17 +03005909static int handle_machine_check(struct kvm_vcpu *vcpu)
Andi Kleena0861c02009-06-08 17:37:09 +08005910{
5911 /* already handled by vcpu_run */
5912 return 1;
5913}
5914
Avi Kivity851ba692009-08-24 11:10:17 +03005915static int handle_exception(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005916{
Avi Kivity1155f762007-11-22 11:30:47 +02005917 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity851ba692009-08-24 11:10:17 +03005918 struct kvm_run *kvm_run = vcpu->run;
Jan Kiszkad0bfb942008-12-15 13:52:10 +01005919 u32 intr_info, ex_no, error_code;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01005920 unsigned long cr2, rip, dr6;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005921 u32 vect_info;
5922 enum emulation_result er;
5923
Avi Kivity1155f762007-11-22 11:30:47 +02005924 vect_info = vmx->idt_vectoring_info;
Avi Kivity88786472011-03-07 17:39:45 +02005925 intr_info = vmx->exit_intr_info;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005926
Andi Kleena0861c02009-06-08 17:37:09 +08005927 if (is_machine_check(intr_info))
Avi Kivity851ba692009-08-24 11:10:17 +03005928 return handle_machine_check(vcpu);
Andi Kleena0861c02009-06-08 17:37:09 +08005929
Jim Mattsonef85b672016-12-12 11:01:37 -08005930 if (is_nmi(intr_info))
Avi Kivity1b6269d2007-10-09 12:12:19 +02005931 return 1; /* already handled by vmx_vcpu_run() */
Anthony Liguori2ab455c2007-04-27 09:29:49 +03005932
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05005933 if (is_invalid_opcode(intr_info)) {
Liran Alonac9b3052017-11-06 16:15:10 +02005934 WARN_ON_ONCE(is_guest_mode(vcpu));
Andre Przywara51d8b662010-12-21 11:12:02 +01005935 er = emulate_instruction(vcpu, EMULTYPE_TRAP_UD);
Liran Alon61cb57c2017-11-05 16:56:32 +02005936 if (er == EMULATE_USER_EXIT)
5937 return 0;
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05005938 if (er != EMULATE_DONE)
Avi Kivity7ee5d9402007-11-25 15:22:50 +02005939 kvm_queue_exception(vcpu, UD_VECTOR);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05005940 return 1;
5941 }
5942
Avi Kivity6aa8b732006-12-10 02:21:36 -08005943 error_code = 0;
Ryan Harper2e113842008-02-11 10:26:38 -06005944 if (intr_info & INTR_INFO_DELIVER_CODE_MASK)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005945 error_code = vmcs_read32(VM_EXIT_INTR_ERROR_CODE);
Xiao Guangrongbf4ca232012-10-17 13:48:06 +08005946
5947 /*
5948 * The #PF with PFEC.RSVD = 1 indicates the guest is accessing
5949 * MMIO, it is better to report an internal error.
5950 * See the comments in vmx_handle_exit.
5951 */
5952 if ((vect_info & VECTORING_INFO_VALID_MASK) &&
5953 !(is_page_fault(intr_info) && !(error_code & PFERR_RSVD_MASK))) {
5954 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
5955 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_SIMUL_EX;
Radim Krčmář80f0e952015-04-02 21:11:05 +02005956 vcpu->run->internal.ndata = 3;
Xiao Guangrongbf4ca232012-10-17 13:48:06 +08005957 vcpu->run->internal.data[0] = vect_info;
5958 vcpu->run->internal.data[1] = intr_info;
Radim Krčmář80f0e952015-04-02 21:11:05 +02005959 vcpu->run->internal.data[2] = error_code;
Xiao Guangrongbf4ca232012-10-17 13:48:06 +08005960 return 0;
5961 }
5962
Avi Kivity6aa8b732006-12-10 02:21:36 -08005963 if (is_page_fault(intr_info)) {
5964 cr2 = vmcs_readl(EXIT_QUALIFICATION);
Wanpeng Li1261bfa2017-07-13 18:30:40 -07005965 /* EPT won't cause page fault directly */
5966 WARN_ON_ONCE(!vcpu->arch.apf.host_apf_reason && enable_ept);
Paolo Bonzinid0006532017-08-11 18:36:43 +02005967 return kvm_handle_page_fault(vcpu, error_code, cr2, NULL, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005968 }
5969
Jan Kiszkad0bfb942008-12-15 13:52:10 +01005970 ex_no = intr_info & INTR_INFO_VECTOR_MASK;
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02005971
5972 if (vmx->rmode.vm86_active && rmode_exception(vcpu, ex_no))
5973 return handle_rmode_exception(vcpu, ex_no, error_code);
5974
Jan Kiszka42dbaa52008-12-15 13:52:10 +01005975 switch (ex_no) {
Eric Northup54a20552015-11-03 18:03:53 +01005976 case AC_VECTOR:
5977 kvm_queue_exception_e(vcpu, AC_VECTOR, error_code);
5978 return 1;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01005979 case DB_VECTOR:
5980 dr6 = vmcs_readl(EXIT_QUALIFICATION);
5981 if (!(vcpu->guest_debug &
5982 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))) {
Jan Kiszka8246bf52014-01-04 18:47:17 +01005983 vcpu->arch.dr6 &= ~15;
Nadav Amit6f43ed02014-07-15 17:37:46 +03005984 vcpu->arch.dr6 |= dr6 | DR6_RTM;
Huw Daviesfd2a4452014-04-16 10:02:51 +01005985 if (!(dr6 & ~DR6_RESERVED)) /* icebp */
5986 skip_emulated_instruction(vcpu);
5987
Jan Kiszka42dbaa52008-12-15 13:52:10 +01005988 kvm_queue_exception(vcpu, DB_VECTOR);
5989 return 1;
5990 }
5991 kvm_run->debug.arch.dr6 = dr6 | DR6_FIXED_1;
5992 kvm_run->debug.arch.dr7 = vmcs_readl(GUEST_DR7);
5993 /* fall through */
5994 case BP_VECTOR:
Jan Kiszkac573cd22010-02-23 17:47:53 +01005995 /*
5996 * Update instruction length as we may reinject #BP from
5997 * user space while in guest debugging mode. Reading it for
5998 * #DB as well causes no harm, it is not used in that case.
5999 */
6000 vmx->vcpu.arch.event_exit_inst_len =
6001 vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006002 kvm_run->exit_reason = KVM_EXIT_DEBUG;
Avi Kivity0a434bb2011-04-28 15:59:33 +03006003 rip = kvm_rip_read(vcpu);
Jan Kiszkad0bfb942008-12-15 13:52:10 +01006004 kvm_run->debug.arch.pc = vmcs_readl(GUEST_CS_BASE) + rip;
6005 kvm_run->debug.arch.exception = ex_no;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006006 break;
6007 default:
Jan Kiszkad0bfb942008-12-15 13:52:10 +01006008 kvm_run->exit_reason = KVM_EXIT_EXCEPTION;
6009 kvm_run->ex.exception = ex_no;
6010 kvm_run->ex.error_code = error_code;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006011 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006012 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08006013 return 0;
6014}
6015
Avi Kivity851ba692009-08-24 11:10:17 +03006016static int handle_external_interrupt(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006017{
Avi Kivity1165f5f2007-04-19 17:27:43 +03006018 ++vcpu->stat.irq_exits;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006019 return 1;
6020}
6021
Avi Kivity851ba692009-08-24 11:10:17 +03006022static int handle_triple_fault(struct kvm_vcpu *vcpu)
Avi Kivity988ad742007-02-12 00:54:36 -08006023{
Avi Kivity851ba692009-08-24 11:10:17 +03006024 vcpu->run->exit_reason = KVM_EXIT_SHUTDOWN;
Wanpeng Libbeac282017-08-09 22:33:12 -07006025 vcpu->mmio_needed = 0;
Avi Kivity988ad742007-02-12 00:54:36 -08006026 return 0;
6027}
Avi Kivity6aa8b732006-12-10 02:21:36 -08006028
Avi Kivity851ba692009-08-24 11:10:17 +03006029static int handle_io(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006030{
He, Qingbfdaab02007-09-12 14:18:28 +08006031 unsigned long exit_qualification;
Kyle Huey6affcbe2016-11-29 12:40:40 -08006032 int size, in, string, ret;
Avi Kivity039576c2007-03-20 12:46:50 +02006033 unsigned port;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006034
He, Qingbfdaab02007-09-12 14:18:28 +08006035 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Avi Kivity039576c2007-03-20 12:46:50 +02006036 string = (exit_qualification & 16) != 0;
Laurent Viviere70669a2007-08-05 10:36:40 +03006037 in = (exit_qualification & 8) != 0;
Laurent Viviere70669a2007-08-05 10:36:40 +03006038
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02006039 ++vcpu->stat.io_exits;
6040
6041 if (string || in)
Andre Przywara51d8b662010-12-21 11:12:02 +01006042 return emulate_instruction(vcpu, 0) == EMULATE_DONE;
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02006043
6044 port = exit_qualification >> 16;
6045 size = (exit_qualification & 7) + 1;
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02006046
Kyle Huey6affcbe2016-11-29 12:40:40 -08006047 ret = kvm_skip_emulated_instruction(vcpu);
6048
6049 /*
6050 * TODO: we might be squashing a KVM_GUESTDBG_SINGLESTEP-triggered
6051 * KVM_EXIT_DEBUG here.
6052 */
6053 return kvm_fast_pio_out(vcpu, size, port) && ret;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006054}
6055
Ingo Molnar102d8322007-02-19 14:37:47 +02006056static void
6057vmx_patch_hypercall(struct kvm_vcpu *vcpu, unsigned char *hypercall)
6058{
6059 /*
6060 * Patch in the VMCALL instruction:
6061 */
6062 hypercall[0] = 0x0f;
6063 hypercall[1] = 0x01;
6064 hypercall[2] = 0xc1;
Ingo Molnar102d8322007-02-19 14:37:47 +02006065}
6066
Guo Chao0fa06072012-06-28 15:16:19 +08006067/* called to set cr0 as appropriate for a mov-to-cr0 exit. */
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006068static int handle_set_cr0(struct kvm_vcpu *vcpu, unsigned long val)
6069{
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006070 if (is_guest_mode(vcpu)) {
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006071 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
6072 unsigned long orig_val = val;
6073
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006074 /*
6075 * We get here when L2 changed cr0 in a way that did not change
6076 * any of L1's shadowed bits (see nested_vmx_exit_handled_cr),
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006077 * but did change L0 shadowed bits. So we first calculate the
6078 * effective cr0 value that L1 would like to write into the
6079 * hardware. It consists of the L2-owned bits from the new
6080 * value combined with the L1-owned bits from L1's guest_cr0.
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006081 */
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006082 val = (val & ~vmcs12->cr0_guest_host_mask) |
6083 (vmcs12->guest_cr0 & vmcs12->cr0_guest_host_mask);
6084
David Matlack38991522016-11-29 18:14:08 -08006085 if (!nested_guest_cr0_valid(vcpu, val))
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006086 return 1;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006087
6088 if (kvm_set_cr0(vcpu, val))
6089 return 1;
6090 vmcs_writel(CR0_READ_SHADOW, orig_val);
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006091 return 0;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006092 } else {
6093 if (to_vmx(vcpu)->nested.vmxon &&
David Matlack38991522016-11-29 18:14:08 -08006094 !nested_host_cr0_valid(vcpu, val))
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006095 return 1;
David Matlack38991522016-11-29 18:14:08 -08006096
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006097 return kvm_set_cr0(vcpu, val);
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006098 }
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006099}
6100
6101static int handle_set_cr4(struct kvm_vcpu *vcpu, unsigned long val)
6102{
6103 if (is_guest_mode(vcpu)) {
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006104 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
6105 unsigned long orig_val = val;
6106
6107 /* analogously to handle_set_cr0 */
6108 val = (val & ~vmcs12->cr4_guest_host_mask) |
6109 (vmcs12->guest_cr4 & vmcs12->cr4_guest_host_mask);
6110 if (kvm_set_cr4(vcpu, val))
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006111 return 1;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006112 vmcs_writel(CR4_READ_SHADOW, orig_val);
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006113 return 0;
6114 } else
6115 return kvm_set_cr4(vcpu, val);
6116}
6117
Paolo Bonzini0367f202016-07-12 10:44:55 +02006118static int handle_desc(struct kvm_vcpu *vcpu)
6119{
6120 WARN_ON(!(vcpu->arch.cr4 & X86_CR4_UMIP));
6121 return emulate_instruction(vcpu, 0) == EMULATE_DONE;
6122}
6123
Avi Kivity851ba692009-08-24 11:10:17 +03006124static int handle_cr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006125{
Marcelo Tosatti229456f2009-06-17 09:22:14 -03006126 unsigned long exit_qualification, val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006127 int cr;
6128 int reg;
Avi Kivity49a9b072010-06-10 17:02:14 +03006129 int err;
Kyle Huey6affcbe2016-11-29 12:40:40 -08006130 int ret;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006131
He, Qingbfdaab02007-09-12 14:18:28 +08006132 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006133 cr = exit_qualification & 15;
6134 reg = (exit_qualification >> 8) & 15;
6135 switch ((exit_qualification >> 4) & 3) {
6136 case 0: /* mov to cr */
Nadav Amit1e32c072014-06-18 17:19:25 +03006137 val = kvm_register_readl(vcpu, reg);
Marcelo Tosatti229456f2009-06-17 09:22:14 -03006138 trace_kvm_cr_write(cr, val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006139 switch (cr) {
6140 case 0:
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006141 err = handle_set_cr0(vcpu, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08006142 return kvm_complete_insn_gp(vcpu, err);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006143 case 3:
Avi Kivity23902182010-06-10 17:02:16 +03006144 err = kvm_set_cr3(vcpu, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08006145 return kvm_complete_insn_gp(vcpu, err);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006146 case 4:
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006147 err = handle_set_cr4(vcpu, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08006148 return kvm_complete_insn_gp(vcpu, err);
Gleb Natapov0a5fff192009-04-21 17:45:06 +03006149 case 8: {
6150 u8 cr8_prev = kvm_get_cr8(vcpu);
Nadav Amit1e32c072014-06-18 17:19:25 +03006151 u8 cr8 = (u8)val;
Andre Przywaraeea1cff2010-12-21 11:12:00 +01006152 err = kvm_set_cr8(vcpu, cr8);
Kyle Huey6affcbe2016-11-29 12:40:40 -08006153 ret = kvm_complete_insn_gp(vcpu, err);
Paolo Bonzini35754c92015-07-29 12:05:37 +02006154 if (lapic_in_kernel(vcpu))
Kyle Huey6affcbe2016-11-29 12:40:40 -08006155 return ret;
Gleb Natapov0a5fff192009-04-21 17:45:06 +03006156 if (cr8_prev <= cr8)
Kyle Huey6affcbe2016-11-29 12:40:40 -08006157 return ret;
6158 /*
6159 * TODO: we might be squashing a
6160 * KVM_GUESTDBG_SINGLESTEP-triggered
6161 * KVM_EXIT_DEBUG here.
6162 */
Avi Kivity851ba692009-08-24 11:10:17 +03006163 vcpu->run->exit_reason = KVM_EXIT_SET_TPR;
Gleb Natapov0a5fff192009-04-21 17:45:06 +03006164 return 0;
6165 }
Peter Senna Tschudin4b8073e2012-09-18 18:36:14 +02006166 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08006167 break;
Anthony Liguori25c4c272007-04-27 09:29:21 +03006168 case 2: /* clts */
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -08006169 WARN_ONCE(1, "Guest should always own CR0.TS");
6170 vmx_set_cr0(vcpu, kvm_read_cr0_bits(vcpu, ~X86_CR0_TS));
Avi Kivity4d4ec082009-12-29 18:07:30 +02006171 trace_kvm_cr_write(0, kvm_read_cr0(vcpu));
Kyle Huey6affcbe2016-11-29 12:40:40 -08006172 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006173 case 1: /*mov from cr*/
6174 switch (cr) {
6175 case 3:
Avi Kivity9f8fe502010-12-05 17:30:00 +02006176 val = kvm_read_cr3(vcpu);
6177 kvm_register_write(vcpu, reg, val);
6178 trace_kvm_cr_read(cr, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08006179 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006180 case 8:
Marcelo Tosatti229456f2009-06-17 09:22:14 -03006181 val = kvm_get_cr8(vcpu);
6182 kvm_register_write(vcpu, reg, val);
6183 trace_kvm_cr_read(cr, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08006184 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006185 }
6186 break;
6187 case 3: /* lmsw */
Avi Kivitya1f83a72009-12-29 17:33:58 +02006188 val = (exit_qualification >> LMSW_SOURCE_DATA_SHIFT) & 0x0f;
Avi Kivity4d4ec082009-12-29 18:07:30 +02006189 trace_kvm_cr_write(0, (kvm_read_cr0(vcpu) & ~0xful) | val);
Avi Kivitya1f83a72009-12-29 17:33:58 +02006190 kvm_lmsw(vcpu, val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006191
Kyle Huey6affcbe2016-11-29 12:40:40 -08006192 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006193 default:
6194 break;
6195 }
Avi Kivity851ba692009-08-24 11:10:17 +03006196 vcpu->run->exit_reason = 0;
Christoffer Dalla737f252012-06-03 21:17:48 +03006197 vcpu_unimpl(vcpu, "unhandled control register: op %d cr %d\n",
Avi Kivity6aa8b732006-12-10 02:21:36 -08006198 (int)(exit_qualification >> 4) & 3, cr);
6199 return 0;
6200}
6201
Avi Kivity851ba692009-08-24 11:10:17 +03006202static int handle_dr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006203{
He, Qingbfdaab02007-09-12 14:18:28 +08006204 unsigned long exit_qualification;
Nadav Amit16f8a6f2014-10-03 01:10:05 +03006205 int dr, dr7, reg;
6206
6207 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
6208 dr = exit_qualification & DEBUG_REG_ACCESS_NUM;
6209
6210 /* First, if DR does not exist, trigger UD */
6211 if (!kvm_require_dr(vcpu, dr))
6212 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006213
Jan Kiszkaf2483412010-01-20 18:20:20 +01006214 /* Do not handle if the CPL > 0, will trigger GP on re-entry */
Avi Kivity0a79b002009-09-01 12:03:25 +03006215 if (!kvm_require_cpl(vcpu, 0))
6216 return 1;
Nadav Amit16f8a6f2014-10-03 01:10:05 +03006217 dr7 = vmcs_readl(GUEST_DR7);
6218 if (dr7 & DR7_GD) {
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006219 /*
6220 * As the vm-exit takes precedence over the debug trap, we
6221 * need to emulate the latter, either for the host or the
6222 * guest debugging itself.
6223 */
6224 if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP) {
Avi Kivity851ba692009-08-24 11:10:17 +03006225 vcpu->run->debug.arch.dr6 = vcpu->arch.dr6;
Nadav Amit16f8a6f2014-10-03 01:10:05 +03006226 vcpu->run->debug.arch.dr7 = dr7;
Nadav Amit82b32772014-11-02 11:54:45 +02006227 vcpu->run->debug.arch.pc = kvm_get_linear_rip(vcpu);
Avi Kivity851ba692009-08-24 11:10:17 +03006228 vcpu->run->debug.arch.exception = DB_VECTOR;
6229 vcpu->run->exit_reason = KVM_EXIT_DEBUG;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006230 return 0;
6231 } else {
Nadav Amit7305eb52014-11-02 11:54:44 +02006232 vcpu->arch.dr6 &= ~15;
Nadav Amit6f43ed02014-07-15 17:37:46 +03006233 vcpu->arch.dr6 |= DR6_BD | DR6_RTM;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006234 kvm_queue_exception(vcpu, DB_VECTOR);
6235 return 1;
6236 }
6237 }
6238
Paolo Bonzini81908bf2014-02-21 10:32:27 +01006239 if (vcpu->guest_debug == 0) {
Paolo Bonzini8f223722016-02-26 12:09:49 +01006240 vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL,
6241 CPU_BASED_MOV_DR_EXITING);
Paolo Bonzini81908bf2014-02-21 10:32:27 +01006242
6243 /*
6244 * No more DR vmexits; force a reload of the debug registers
6245 * and reenter on this instruction. The next vmexit will
6246 * retrieve the full state of the debug registers.
6247 */
6248 vcpu->arch.switch_db_regs |= KVM_DEBUGREG_WONT_EXIT;
6249 return 1;
6250 }
6251
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006252 reg = DEBUG_REG_ACCESS_REG(exit_qualification);
6253 if (exit_qualification & TYPE_MOV_FROM_DR) {
Gleb Natapov020df072010-04-13 10:05:23 +03006254 unsigned long val;
Jan Kiszka4c4d5632013-12-18 19:16:24 +01006255
6256 if (kvm_get_dr(vcpu, dr, &val))
6257 return 1;
6258 kvm_register_write(vcpu, reg, val);
Gleb Natapov020df072010-04-13 10:05:23 +03006259 } else
Nadav Amit57773922014-06-18 17:19:23 +03006260 if (kvm_set_dr(vcpu, dr, kvm_register_readl(vcpu, reg)))
Jan Kiszka4c4d5632013-12-18 19:16:24 +01006261 return 1;
6262
Kyle Huey6affcbe2016-11-29 12:40:40 -08006263 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006264}
6265
Jan Kiszka73aaf249e2014-01-04 18:47:16 +01006266static u64 vmx_get_dr6(struct kvm_vcpu *vcpu)
6267{
6268 return vcpu->arch.dr6;
6269}
6270
6271static void vmx_set_dr6(struct kvm_vcpu *vcpu, unsigned long val)
6272{
6273}
6274
Paolo Bonzini81908bf2014-02-21 10:32:27 +01006275static void vmx_sync_dirty_debug_regs(struct kvm_vcpu *vcpu)
6276{
Paolo Bonzini81908bf2014-02-21 10:32:27 +01006277 get_debugreg(vcpu->arch.db[0], 0);
6278 get_debugreg(vcpu->arch.db[1], 1);
6279 get_debugreg(vcpu->arch.db[2], 2);
6280 get_debugreg(vcpu->arch.db[3], 3);
6281 get_debugreg(vcpu->arch.dr6, 6);
6282 vcpu->arch.dr7 = vmcs_readl(GUEST_DR7);
6283
6284 vcpu->arch.switch_db_regs &= ~KVM_DEBUGREG_WONT_EXIT;
Paolo Bonzini8f223722016-02-26 12:09:49 +01006285 vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL, CPU_BASED_MOV_DR_EXITING);
Paolo Bonzini81908bf2014-02-21 10:32:27 +01006286}
6287
Gleb Natapov020df072010-04-13 10:05:23 +03006288static void vmx_set_dr7(struct kvm_vcpu *vcpu, unsigned long val)
6289{
6290 vmcs_writel(GUEST_DR7, val);
6291}
6292
Avi Kivity851ba692009-08-24 11:10:17 +03006293static int handle_cpuid(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006294{
Kyle Huey6a908b62016-11-29 12:40:37 -08006295 return kvm_emulate_cpuid(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006296}
6297
Avi Kivity851ba692009-08-24 11:10:17 +03006298static int handle_rdmsr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006299{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08006300 u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX];
Paolo Bonzini609e36d2015-04-08 15:30:38 +02006301 struct msr_data msr_info;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006302
Paolo Bonzini609e36d2015-04-08 15:30:38 +02006303 msr_info.index = ecx;
6304 msr_info.host_initiated = false;
6305 if (vmx_get_msr(vcpu, &msr_info)) {
Avi Kivity59200272010-01-25 19:47:02 +02006306 trace_kvm_msr_read_ex(ecx);
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02006307 kvm_inject_gp(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006308 return 1;
6309 }
6310
Paolo Bonzini609e36d2015-04-08 15:30:38 +02006311 trace_kvm_msr_read(ecx, msr_info.data);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04006312
Avi Kivity6aa8b732006-12-10 02:21:36 -08006313 /* FIXME: handling of bits 32:63 of rax, rdx */
Paolo Bonzini609e36d2015-04-08 15:30:38 +02006314 vcpu->arch.regs[VCPU_REGS_RAX] = msr_info.data & -1u;
6315 vcpu->arch.regs[VCPU_REGS_RDX] = (msr_info.data >> 32) & -1u;
Kyle Huey6affcbe2016-11-29 12:40:40 -08006316 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006317}
6318
Avi Kivity851ba692009-08-24 11:10:17 +03006319static int handle_wrmsr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006320{
Will Auld8fe8ab42012-11-29 12:42:12 -08006321 struct msr_data msr;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08006322 u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX];
6323 u64 data = (vcpu->arch.regs[VCPU_REGS_RAX] & -1u)
6324 | ((u64)(vcpu->arch.regs[VCPU_REGS_RDX] & -1u) << 32);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006325
Will Auld8fe8ab42012-11-29 12:42:12 -08006326 msr.data = data;
6327 msr.index = ecx;
6328 msr.host_initiated = false;
Nadav Amit854e8bb2014-09-16 03:24:05 +03006329 if (kvm_set_msr(vcpu, &msr) != 0) {
Avi Kivity59200272010-01-25 19:47:02 +02006330 trace_kvm_msr_write_ex(ecx, data);
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02006331 kvm_inject_gp(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006332 return 1;
6333 }
6334
Avi Kivity59200272010-01-25 19:47:02 +02006335 trace_kvm_msr_write(ecx, data);
Kyle Huey6affcbe2016-11-29 12:40:40 -08006336 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006337}
6338
Avi Kivity851ba692009-08-24 11:10:17 +03006339static int handle_tpr_below_threshold(struct kvm_vcpu *vcpu)
Yang, Sheng6e5d8652007-09-12 18:03:11 +08006340{
Paolo Bonzinieb90f342016-12-18 14:02:21 +01006341 kvm_apic_update_ppr(vcpu);
Yang, Sheng6e5d8652007-09-12 18:03:11 +08006342 return 1;
6343}
6344
Avi Kivity851ba692009-08-24 11:10:17 +03006345static int handle_interrupt_window(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006346{
Paolo Bonzini47c01522016-12-19 11:44:07 +01006347 vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL,
6348 CPU_BASED_VIRTUAL_INTR_PENDING);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04006349
Avi Kivity3842d132010-07-27 12:30:24 +03006350 kvm_make_request(KVM_REQ_EVENT, vcpu);
6351
Jan Kiszkaa26bf122008-09-26 09:30:45 +02006352 ++vcpu->stat.irq_window_exits;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006353 return 1;
6354}
6355
Avi Kivity851ba692009-08-24 11:10:17 +03006356static int handle_halt(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006357{
Avi Kivityd3bef152007-06-05 15:53:05 +03006358 return kvm_emulate_halt(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006359}
6360
Avi Kivity851ba692009-08-24 11:10:17 +03006361static int handle_vmcall(struct kvm_vcpu *vcpu)
Ingo Molnarc21415e2007-02-19 14:37:47 +02006362{
Andrey Smetanin0d9c0552016-02-11 16:44:59 +03006363 return kvm_emulate_hypercall(vcpu);
Ingo Molnarc21415e2007-02-19 14:37:47 +02006364}
6365
Gleb Natapovec25d5e2010-11-01 15:35:01 +02006366static int handle_invd(struct kvm_vcpu *vcpu)
6367{
Andre Przywara51d8b662010-12-21 11:12:02 +01006368 return emulate_instruction(vcpu, 0) == EMULATE_DONE;
Gleb Natapovec25d5e2010-11-01 15:35:01 +02006369}
6370
Avi Kivity851ba692009-08-24 11:10:17 +03006371static int handle_invlpg(struct kvm_vcpu *vcpu)
Marcelo Tosattia7052892008-09-23 13:18:35 -03006372{
Sheng Yangf9c617f2009-03-25 10:08:52 +08006373 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Marcelo Tosattia7052892008-09-23 13:18:35 -03006374
6375 kvm_mmu_invlpg(vcpu, exit_qualification);
Kyle Huey6affcbe2016-11-29 12:40:40 -08006376 return kvm_skip_emulated_instruction(vcpu);
Marcelo Tosattia7052892008-09-23 13:18:35 -03006377}
6378
Avi Kivityfee84b02011-11-10 14:57:25 +02006379static int handle_rdpmc(struct kvm_vcpu *vcpu)
6380{
6381 int err;
6382
6383 err = kvm_rdpmc(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08006384 return kvm_complete_insn_gp(vcpu, err);
Avi Kivityfee84b02011-11-10 14:57:25 +02006385}
6386
Avi Kivity851ba692009-08-24 11:10:17 +03006387static int handle_wbinvd(struct kvm_vcpu *vcpu)
Eddie Donge5edaa02007-11-11 12:28:35 +02006388{
Kyle Huey6affcbe2016-11-29 12:40:40 -08006389 return kvm_emulate_wbinvd(vcpu);
Eddie Donge5edaa02007-11-11 12:28:35 +02006390}
6391
Dexuan Cui2acf9232010-06-10 11:27:12 +08006392static int handle_xsetbv(struct kvm_vcpu *vcpu)
6393{
6394 u64 new_bv = kvm_read_edx_eax(vcpu);
6395 u32 index = kvm_register_read(vcpu, VCPU_REGS_RCX);
6396
6397 if (kvm_set_xcr(vcpu, index, new_bv) == 0)
Kyle Huey6affcbe2016-11-29 12:40:40 -08006398 return kvm_skip_emulated_instruction(vcpu);
Dexuan Cui2acf9232010-06-10 11:27:12 +08006399 return 1;
6400}
6401
Wanpeng Lif53cd632014-12-02 19:14:58 +08006402static int handle_xsaves(struct kvm_vcpu *vcpu)
6403{
Kyle Huey6affcbe2016-11-29 12:40:40 -08006404 kvm_skip_emulated_instruction(vcpu);
Wanpeng Lif53cd632014-12-02 19:14:58 +08006405 WARN(1, "this should never happen\n");
6406 return 1;
6407}
6408
6409static int handle_xrstors(struct kvm_vcpu *vcpu)
6410{
Kyle Huey6affcbe2016-11-29 12:40:40 -08006411 kvm_skip_emulated_instruction(vcpu);
Wanpeng Lif53cd632014-12-02 19:14:58 +08006412 WARN(1, "this should never happen\n");
6413 return 1;
6414}
6415
Avi Kivity851ba692009-08-24 11:10:17 +03006416static int handle_apic_access(struct kvm_vcpu *vcpu)
Sheng Yangf78e0e22007-10-29 09:40:42 +08006417{
Kevin Tian58fbbf22011-08-30 13:56:17 +03006418 if (likely(fasteoi)) {
6419 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
6420 int access_type, offset;
6421
6422 access_type = exit_qualification & APIC_ACCESS_TYPE;
6423 offset = exit_qualification & APIC_ACCESS_OFFSET;
6424 /*
6425 * Sane guest uses MOV to write EOI, with written value
6426 * not cared. So make a short-circuit here by avoiding
6427 * heavy instruction emulation.
6428 */
6429 if ((access_type == TYPE_LINEAR_APIC_INST_WRITE) &&
6430 (offset == APIC_EOI)) {
6431 kvm_lapic_set_eoi(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08006432 return kvm_skip_emulated_instruction(vcpu);
Kevin Tian58fbbf22011-08-30 13:56:17 +03006433 }
6434 }
Andre Przywara51d8b662010-12-21 11:12:02 +01006435 return emulate_instruction(vcpu, 0) == EMULATE_DONE;
Sheng Yangf78e0e22007-10-29 09:40:42 +08006436}
6437
Yang Zhangc7c9c562013-01-25 10:18:51 +08006438static int handle_apic_eoi_induced(struct kvm_vcpu *vcpu)
6439{
6440 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
6441 int vector = exit_qualification & 0xff;
6442
6443 /* EOI-induced VM exit is trap-like and thus no need to adjust IP */
6444 kvm_apic_set_eoi_accelerated(vcpu, vector);
6445 return 1;
6446}
6447
Yang Zhang83d4c282013-01-25 10:18:49 +08006448static int handle_apic_write(struct kvm_vcpu *vcpu)
6449{
6450 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
6451 u32 offset = exit_qualification & 0xfff;
6452
6453 /* APIC-write VM exit is trap-like and thus no need to adjust IP */
6454 kvm_apic_write_nodecode(vcpu, offset);
6455 return 1;
6456}
6457
Avi Kivity851ba692009-08-24 11:10:17 +03006458static int handle_task_switch(struct kvm_vcpu *vcpu)
Izik Eidus37817f22008-03-24 23:14:53 +02006459{
Jan Kiszka60637aa2008-09-26 09:30:47 +02006460 struct vcpu_vmx *vmx = to_vmx(vcpu);
Izik Eidus37817f22008-03-24 23:14:53 +02006461 unsigned long exit_qualification;
Jan Kiszkae269fb22010-04-14 15:51:09 +02006462 bool has_error_code = false;
6463 u32 error_code = 0;
Izik Eidus37817f22008-03-24 23:14:53 +02006464 u16 tss_selector;
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01006465 int reason, type, idt_v, idt_index;
Gleb Natapov64a7ec02009-03-30 16:03:29 +03006466
6467 idt_v = (vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK);
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01006468 idt_index = (vmx->idt_vectoring_info & VECTORING_INFO_VECTOR_MASK);
Gleb Natapov64a7ec02009-03-30 16:03:29 +03006469 type = (vmx->idt_vectoring_info & VECTORING_INFO_TYPE_MASK);
Izik Eidus37817f22008-03-24 23:14:53 +02006470
6471 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
6472
6473 reason = (u32)exit_qualification >> 30;
Gleb Natapov64a7ec02009-03-30 16:03:29 +03006474 if (reason == TASK_SWITCH_GATE && idt_v) {
6475 switch (type) {
6476 case INTR_TYPE_NMI_INTR:
6477 vcpu->arch.nmi_injected = false;
Avi Kivity654f06f2011-03-23 15:02:47 +02006478 vmx_set_nmi_mask(vcpu, true);
Gleb Natapov64a7ec02009-03-30 16:03:29 +03006479 break;
6480 case INTR_TYPE_EXT_INTR:
Gleb Natapov66fd3f72009-05-11 13:35:50 +03006481 case INTR_TYPE_SOFT_INTR:
Gleb Natapov64a7ec02009-03-30 16:03:29 +03006482 kvm_clear_interrupt_queue(vcpu);
6483 break;
6484 case INTR_TYPE_HARD_EXCEPTION:
Jan Kiszkae269fb22010-04-14 15:51:09 +02006485 if (vmx->idt_vectoring_info &
6486 VECTORING_INFO_DELIVER_CODE_MASK) {
6487 has_error_code = true;
6488 error_code =
6489 vmcs_read32(IDT_VECTORING_ERROR_CODE);
6490 }
6491 /* fall through */
Gleb Natapov64a7ec02009-03-30 16:03:29 +03006492 case INTR_TYPE_SOFT_EXCEPTION:
6493 kvm_clear_exception_queue(vcpu);
6494 break;
6495 default:
6496 break;
6497 }
Jan Kiszka60637aa2008-09-26 09:30:47 +02006498 }
Izik Eidus37817f22008-03-24 23:14:53 +02006499 tss_selector = exit_qualification;
6500
Gleb Natapov64a7ec02009-03-30 16:03:29 +03006501 if (!idt_v || (type != INTR_TYPE_HARD_EXCEPTION &&
6502 type != INTR_TYPE_EXT_INTR &&
6503 type != INTR_TYPE_NMI_INTR))
6504 skip_emulated_instruction(vcpu);
6505
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01006506 if (kvm_task_switch(vcpu, tss_selector,
6507 type == INTR_TYPE_SOFT_INTR ? idt_index : -1, reason,
6508 has_error_code, error_code) == EMULATE_FAIL) {
Gleb Natapovacb54512010-04-15 21:03:50 +03006509 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
6510 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
6511 vcpu->run->internal.ndata = 0;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006512 return 0;
Gleb Natapovacb54512010-04-15 21:03:50 +03006513 }
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006514
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006515 /*
6516 * TODO: What about debug traps on tss switch?
6517 * Are we supposed to inject them and update dr6?
6518 */
6519
6520 return 1;
Izik Eidus37817f22008-03-24 23:14:53 +02006521}
6522
Avi Kivity851ba692009-08-24 11:10:17 +03006523static int handle_ept_violation(struct kvm_vcpu *vcpu)
Sheng Yang14394422008-04-28 12:24:45 +08006524{
Sheng Yangf9c617f2009-03-25 10:08:52 +08006525 unsigned long exit_qualification;
Sheng Yang14394422008-04-28 12:24:45 +08006526 gpa_t gpa;
Paolo Bonzinieebed242016-11-28 14:39:58 +01006527 u64 error_code;
Sheng Yang14394422008-04-28 12:24:45 +08006528
Sheng Yangf9c617f2009-03-25 10:08:52 +08006529 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Sheng Yang14394422008-04-28 12:24:45 +08006530
Gleb Natapov0be9c7a2013-09-15 11:07:23 +03006531 /*
6532 * EPT violation happened while executing iret from NMI,
6533 * "blocked by NMI" bit has to be set before next VM entry.
6534 * There are errata that may cause this bit to not be set:
6535 * AAK134, BY25.
6536 */
Gleb Natapovbcd1c292013-09-25 10:58:22 +03006537 if (!(to_vmx(vcpu)->idt_vectoring_info & VECTORING_INFO_VALID_MASK) &&
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01006538 enable_vnmi &&
Gleb Natapovbcd1c292013-09-25 10:58:22 +03006539 (exit_qualification & INTR_INFO_UNBLOCK_NMI))
Gleb Natapov0be9c7a2013-09-15 11:07:23 +03006540 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO, GUEST_INTR_STATE_NMI);
6541
Sheng Yang14394422008-04-28 12:24:45 +08006542 gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS);
Marcelo Tosatti229456f2009-06-17 09:22:14 -03006543 trace_kvm_page_fault(gpa, exit_qualification);
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08006544
Junaid Shahid27959a42016-12-06 16:46:10 -08006545 /* Is it a read fault? */
Junaid Shahidab22a472016-12-21 20:29:28 -08006546 error_code = (exit_qualification & EPT_VIOLATION_ACC_READ)
Junaid Shahid27959a42016-12-06 16:46:10 -08006547 ? PFERR_USER_MASK : 0;
6548 /* Is it a write fault? */
Junaid Shahidab22a472016-12-21 20:29:28 -08006549 error_code |= (exit_qualification & EPT_VIOLATION_ACC_WRITE)
Junaid Shahid27959a42016-12-06 16:46:10 -08006550 ? PFERR_WRITE_MASK : 0;
6551 /* Is it a fetch fault? */
Junaid Shahidab22a472016-12-21 20:29:28 -08006552 error_code |= (exit_qualification & EPT_VIOLATION_ACC_INSTR)
Junaid Shahid27959a42016-12-06 16:46:10 -08006553 ? PFERR_FETCH_MASK : 0;
6554 /* ept page table entry is present? */
6555 error_code |= (exit_qualification &
6556 (EPT_VIOLATION_READABLE | EPT_VIOLATION_WRITABLE |
6557 EPT_VIOLATION_EXECUTABLE))
6558 ? PFERR_PRESENT_MASK : 0;
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08006559
Paolo Bonzinieebed242016-11-28 14:39:58 +01006560 error_code |= (exit_qualification & 0x100) != 0 ?
6561 PFERR_GUEST_FINAL_MASK : PFERR_GUEST_PAGE_MASK;
Yang Zhang25d92082013-08-06 12:00:32 +03006562
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08006563 vcpu->arch.exit_qualification = exit_qualification;
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08006564 return kvm_mmu_page_fault(vcpu, gpa, error_code, NULL, 0);
Sheng Yang14394422008-04-28 12:24:45 +08006565}
6566
Avi Kivity851ba692009-08-24 11:10:17 +03006567static int handle_ept_misconfig(struct kvm_vcpu *vcpu)
Marcelo Tosatti68f89402009-06-11 12:07:43 -03006568{
Xiao Guangrongf735d4a2015-08-05 12:04:27 +08006569 int ret;
Marcelo Tosatti68f89402009-06-11 12:07:43 -03006570 gpa_t gpa;
6571
Paolo Bonzini9034e6e2017-08-17 18:36:58 +02006572 /*
6573 * A nested guest cannot optimize MMIO vmexits, because we have an
6574 * nGPA here instead of the required GPA.
6575 */
Marcelo Tosatti68f89402009-06-11 12:07:43 -03006576 gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS);
Paolo Bonzini9034e6e2017-08-17 18:36:58 +02006577 if (!is_guest_mode(vcpu) &&
6578 !kvm_io_bus_write(vcpu, KVM_FAST_MMIO_BUS, gpa, 0, NULL)) {
Jason Wang931c33b2015-09-15 14:41:58 +08006579 trace_kvm_fast_mmio(gpa);
Kyle Huey6affcbe2016-11-29 12:40:40 -08006580 return kvm_skip_emulated_instruction(vcpu);
Michael S. Tsirkin68c3b4d2014-03-31 21:50:44 +03006581 }
Marcelo Tosatti68f89402009-06-11 12:07:43 -03006582
Paolo Bonzinie08d26f2017-08-17 18:36:56 +02006583 ret = kvm_mmu_page_fault(vcpu, gpa, PFERR_RSVD_MASK, NULL, 0);
6584 if (ret >= 0)
6585 return ret;
Xiao Guangrongce88dec2011-07-12 03:33:44 +08006586
6587 /* It is the real ept misconfig */
Xiao Guangrongf735d4a2015-08-05 12:04:27 +08006588 WARN_ON(1);
Marcelo Tosatti68f89402009-06-11 12:07:43 -03006589
Avi Kivity851ba692009-08-24 11:10:17 +03006590 vcpu->run->exit_reason = KVM_EXIT_UNKNOWN;
6591 vcpu->run->hw.hardware_exit_reason = EXIT_REASON_EPT_MISCONFIG;
Marcelo Tosatti68f89402009-06-11 12:07:43 -03006592
6593 return 0;
6594}
6595
Avi Kivity851ba692009-08-24 11:10:17 +03006596static int handle_nmi_window(struct kvm_vcpu *vcpu)
Sheng Yangf08864b2008-05-15 18:23:25 +08006597{
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01006598 WARN_ON_ONCE(!enable_vnmi);
Paolo Bonzini47c01522016-12-19 11:44:07 +01006599 vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL,
6600 CPU_BASED_VIRTUAL_NMI_PENDING);
Sheng Yangf08864b2008-05-15 18:23:25 +08006601 ++vcpu->stat.nmi_window_exits;
Avi Kivity3842d132010-07-27 12:30:24 +03006602 kvm_make_request(KVM_REQ_EVENT, vcpu);
Sheng Yangf08864b2008-05-15 18:23:25 +08006603
6604 return 1;
6605}
6606
Mohammed Gamal80ced182009-09-01 12:48:18 +02006607static int handle_invalid_guest_state(struct kvm_vcpu *vcpu)
Mohammed Gamalea953ef2008-08-17 16:47:05 +03006608{
Avi Kivity8b3079a2009-01-05 12:10:54 +02006609 struct vcpu_vmx *vmx = to_vmx(vcpu);
6610 enum emulation_result err = EMULATE_DONE;
Mohammed Gamal80ced182009-09-01 12:48:18 +02006611 int ret = 1;
Avi Kivity49e9d552010-09-19 14:34:08 +02006612 u32 cpu_exec_ctrl;
6613 bool intr_window_requested;
Avi Kivityb8405c12012-06-07 17:08:48 +03006614 unsigned count = 130;
Avi Kivity49e9d552010-09-19 14:34:08 +02006615
6616 cpu_exec_ctrl = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
6617 intr_window_requested = cpu_exec_ctrl & CPU_BASED_VIRTUAL_INTR_PENDING;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03006618
Paolo Bonzini98eb2f82014-03-27 09:51:52 +01006619 while (vmx->emulation_required && count-- != 0) {
Avi Kivitybdea48e2012-06-10 18:07:57 +03006620 if (intr_window_requested && vmx_interrupt_allowed(vcpu))
Avi Kivity49e9d552010-09-19 14:34:08 +02006621 return handle_interrupt_window(&vmx->vcpu);
6622
Radim Krčmář72875d82017-04-26 22:32:19 +02006623 if (kvm_test_request(KVM_REQ_EVENT, vcpu))
Avi Kivityde87dcdd2012-06-12 20:21:38 +03006624 return 1;
6625
Liran Alon9b8ae632017-11-05 16:56:34 +02006626 err = emulate_instruction(vcpu, 0);
Mohammed Gamalea953ef2008-08-17 16:47:05 +03006627
Paolo Bonziniac0a48c2013-06-25 18:24:41 +02006628 if (err == EMULATE_USER_EXIT) {
Paolo Bonzini94452b92013-08-27 15:41:42 +02006629 ++vcpu->stat.mmio_exits;
Mohammed Gamal80ced182009-09-01 12:48:18 +02006630 ret = 0;
6631 goto out;
6632 }
Guillaume Thouvenin1d5a4d92008-10-29 09:39:42 +01006633
Avi Kivityde5f70e2012-06-12 20:22:28 +03006634 if (err != EMULATE_DONE) {
6635 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
6636 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
6637 vcpu->run->internal.ndata = 0;
Gleb Natapov6d77dbf2010-05-10 11:16:56 +03006638 return 0;
Avi Kivityde5f70e2012-06-12 20:22:28 +03006639 }
Mohammed Gamalea953ef2008-08-17 16:47:05 +03006640
Gleb Natapov8d76c492013-05-08 18:38:44 +03006641 if (vcpu->arch.halt_request) {
6642 vcpu->arch.halt_request = 0;
Joel Schopp5cb56052015-03-02 13:43:31 -06006643 ret = kvm_vcpu_halt(vcpu);
Gleb Natapov8d76c492013-05-08 18:38:44 +03006644 goto out;
6645 }
6646
Mohammed Gamalea953ef2008-08-17 16:47:05 +03006647 if (signal_pending(current))
Mohammed Gamal80ced182009-09-01 12:48:18 +02006648 goto out;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03006649 if (need_resched())
6650 schedule();
6651 }
6652
Mohammed Gamal80ced182009-09-01 12:48:18 +02006653out:
6654 return ret;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03006655}
6656
Radim Krčmářb4a2d312014-08-21 18:08:08 +02006657static int __grow_ple_window(int val)
6658{
6659 if (ple_window_grow < 1)
6660 return ple_window;
6661
6662 val = min(val, ple_window_actual_max);
6663
6664 if (ple_window_grow < ple_window)
6665 val *= ple_window_grow;
6666 else
6667 val += ple_window_grow;
6668
6669 return val;
6670}
6671
6672static int __shrink_ple_window(int val, int modifier, int minimum)
6673{
6674 if (modifier < 1)
6675 return ple_window;
6676
6677 if (modifier < ple_window)
6678 val /= modifier;
6679 else
6680 val -= modifier;
6681
6682 return max(val, minimum);
6683}
6684
6685static void grow_ple_window(struct kvm_vcpu *vcpu)
6686{
6687 struct vcpu_vmx *vmx = to_vmx(vcpu);
6688 int old = vmx->ple_window;
6689
6690 vmx->ple_window = __grow_ple_window(old);
6691
6692 if (vmx->ple_window != old)
6693 vmx->ple_window_dirty = true;
Radim Krčmář7b462682014-08-21 18:08:09 +02006694
6695 trace_kvm_ple_window_grow(vcpu->vcpu_id, vmx->ple_window, old);
Radim Krčmářb4a2d312014-08-21 18:08:08 +02006696}
6697
6698static void shrink_ple_window(struct kvm_vcpu *vcpu)
6699{
6700 struct vcpu_vmx *vmx = to_vmx(vcpu);
6701 int old = vmx->ple_window;
6702
6703 vmx->ple_window = __shrink_ple_window(old,
6704 ple_window_shrink, ple_window);
6705
6706 if (vmx->ple_window != old)
6707 vmx->ple_window_dirty = true;
Radim Krčmář7b462682014-08-21 18:08:09 +02006708
6709 trace_kvm_ple_window_shrink(vcpu->vcpu_id, vmx->ple_window, old);
Radim Krčmářb4a2d312014-08-21 18:08:08 +02006710}
6711
6712/*
6713 * ple_window_actual_max is computed to be one grow_ple_window() below
6714 * ple_window_max. (See __grow_ple_window for the reason.)
6715 * This prevents overflows, because ple_window_max is int.
6716 * ple_window_max effectively rounded down to a multiple of ple_window_grow in
6717 * this process.
6718 * ple_window_max is also prevented from setting vmx->ple_window < ple_window.
6719 */
6720static void update_ple_window_actual_max(void)
6721{
6722 ple_window_actual_max =
6723 __shrink_ple_window(max(ple_window_max, ple_window),
6724 ple_window_grow, INT_MIN);
6725}
6726
Feng Wubf9f6ac2015-09-18 22:29:55 +08006727/*
6728 * Handler for POSTED_INTERRUPT_WAKEUP_VECTOR.
6729 */
6730static void wakeup_handler(void)
6731{
6732 struct kvm_vcpu *vcpu;
6733 int cpu = smp_processor_id();
6734
6735 spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, cpu));
6736 list_for_each_entry(vcpu, &per_cpu(blocked_vcpu_on_cpu, cpu),
6737 blocked_vcpu_list) {
6738 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
6739
6740 if (pi_test_on(pi_desc) == 1)
6741 kvm_vcpu_kick(vcpu);
6742 }
6743 spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, cpu));
6744}
6745
Junaid Shahidf160c7b2016-12-06 16:46:16 -08006746void vmx_enable_tdp(void)
6747{
6748 kvm_mmu_set_mask_ptes(VMX_EPT_READABLE_MASK,
6749 enable_ept_ad_bits ? VMX_EPT_ACCESS_BIT : 0ull,
6750 enable_ept_ad_bits ? VMX_EPT_DIRTY_BIT : 0ull,
6751 0ull, VMX_EPT_EXECUTABLE_MASK,
6752 cpu_has_vmx_ept_execute_only() ? 0ull : VMX_EPT_READABLE_MASK,
Tom Lendackyd0ec49d2017-07-17 16:10:27 -05006753 VMX_EPT_RWX_MASK, 0ull);
Junaid Shahidf160c7b2016-12-06 16:46:16 -08006754
6755 ept_set_mmio_spte_mask();
6756 kvm_enable_tdp();
6757}
6758
Tiejun Chenf2c76482014-10-28 10:14:47 +08006759static __init int hardware_setup(void)
6760{
Tiejun Chen34a1cd62014-10-28 10:14:48 +08006761 int r = -ENOMEM, i, msr;
6762
6763 rdmsrl_safe(MSR_EFER, &host_efer);
6764
6765 for (i = 0; i < ARRAY_SIZE(vmx_msr_index); ++i)
6766 kvm_define_shared_msr(i, vmx_msr_index[i]);
6767
Radim Krčmář23611332016-09-29 22:41:33 +02006768 for (i = 0; i < VMX_BITMAP_NR; i++) {
6769 vmx_bitmap[i] = (unsigned long *)__get_free_page(GFP_KERNEL);
6770 if (!vmx_bitmap[i])
6771 goto out;
6772 }
Tiejun Chen34a1cd62014-10-28 10:14:48 +08006773
Tiejun Chen34a1cd62014-10-28 10:14:48 +08006774 memset(vmx_vmread_bitmap, 0xff, PAGE_SIZE);
6775 memset(vmx_vmwrite_bitmap, 0xff, PAGE_SIZE);
6776
Tiejun Chen34a1cd62014-10-28 10:14:48 +08006777 memset(vmx_msr_bitmap_legacy, 0xff, PAGE_SIZE);
6778 memset(vmx_msr_bitmap_longmode, 0xff, PAGE_SIZE);
6779
Tiejun Chen34a1cd62014-10-28 10:14:48 +08006780 if (setup_vmcs_config(&vmcs_config) < 0) {
6781 r = -EIO;
Radim Krčmář23611332016-09-29 22:41:33 +02006782 goto out;
Tiejun Chenbaa03522014-12-23 16:21:11 +08006783 }
Tiejun Chenf2c76482014-10-28 10:14:47 +08006784
6785 if (boot_cpu_has(X86_FEATURE_NX))
6786 kvm_enable_efer_bits(EFER_NX);
6787
Wanpeng Li08d839c2017-03-23 05:30:08 -07006788 if (!cpu_has_vmx_vpid() || !cpu_has_vmx_invvpid() ||
6789 !(cpu_has_vmx_invvpid_single() || cpu_has_vmx_invvpid_global()))
Tiejun Chenf2c76482014-10-28 10:14:47 +08006790 enable_vpid = 0;
Wanpeng Li08d839c2017-03-23 05:30:08 -07006791
Tiejun Chenf2c76482014-10-28 10:14:47 +08006792 if (!cpu_has_vmx_ept() ||
David Hildenbrand42aa53b2017-08-10 23:15:29 +02006793 !cpu_has_vmx_ept_4levels() ||
David Hildenbrandf5f51582017-08-24 20:51:30 +02006794 !cpu_has_vmx_ept_mt_wb() ||
Wanpeng Li8ad81822017-10-09 15:51:53 -07006795 !cpu_has_vmx_invept_global())
Tiejun Chenf2c76482014-10-28 10:14:47 +08006796 enable_ept = 0;
Tiejun Chenf2c76482014-10-28 10:14:47 +08006797
Wanpeng Lifce6ac42017-05-11 02:58:56 -07006798 if (!cpu_has_vmx_ept_ad_bits() || !enable_ept)
Tiejun Chenf2c76482014-10-28 10:14:47 +08006799 enable_ept_ad_bits = 0;
6800
Wanpeng Li8ad81822017-10-09 15:51:53 -07006801 if (!cpu_has_vmx_unrestricted_guest() || !enable_ept)
Tiejun Chenf2c76482014-10-28 10:14:47 +08006802 enable_unrestricted_guest = 0;
6803
Paolo Bonziniad15a292015-01-30 16:18:49 +01006804 if (!cpu_has_vmx_flexpriority())
Tiejun Chenf2c76482014-10-28 10:14:47 +08006805 flexpriority_enabled = 0;
6806
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01006807 if (!cpu_has_virtual_nmis())
6808 enable_vnmi = 0;
6809
Paolo Bonziniad15a292015-01-30 16:18:49 +01006810 /*
6811 * set_apic_access_page_addr() is used to reload apic access
6812 * page upon invalidation. No need to do anything if not
6813 * using the APIC_ACCESS_ADDR VMCS field.
6814 */
6815 if (!flexpriority_enabled)
Tiejun Chenf2c76482014-10-28 10:14:47 +08006816 kvm_x86_ops->set_apic_access_page_addr = NULL;
Tiejun Chenf2c76482014-10-28 10:14:47 +08006817
6818 if (!cpu_has_vmx_tpr_shadow())
6819 kvm_x86_ops->update_cr8_intercept = NULL;
6820
6821 if (enable_ept && !cpu_has_vmx_ept_2m_page())
6822 kvm_disable_largepages();
6823
Wanpeng Li0f107682017-09-28 18:06:24 -07006824 if (!cpu_has_vmx_ple()) {
Tiejun Chenf2c76482014-10-28 10:14:47 +08006825 ple_gap = 0;
Wanpeng Li0f107682017-09-28 18:06:24 -07006826 ple_window = 0;
6827 ple_window_grow = 0;
6828 ple_window_max = 0;
6829 ple_window_shrink = 0;
6830 }
Tiejun Chenf2c76482014-10-28 10:14:47 +08006831
Paolo Bonzini76dfafd52016-12-19 17:17:11 +01006832 if (!cpu_has_vmx_apicv()) {
Tiejun Chenf2c76482014-10-28 10:14:47 +08006833 enable_apicv = 0;
Paolo Bonzini76dfafd52016-12-19 17:17:11 +01006834 kvm_x86_ops->sync_pir_to_irr = NULL;
6835 }
Tiejun Chenf2c76482014-10-28 10:14:47 +08006836
Haozhong Zhang64903d62015-10-20 15:39:09 +08006837 if (cpu_has_vmx_tsc_scaling()) {
6838 kvm_has_tsc_control = true;
6839 kvm_max_tsc_scaling_ratio = KVM_VMX_TSC_MULTIPLIER_MAX;
6840 kvm_tsc_scaling_ratio_frac_bits = 48;
6841 }
6842
Tiejun Chenbaa03522014-12-23 16:21:11 +08006843 vmx_disable_intercept_for_msr(MSR_FS_BASE, false);
6844 vmx_disable_intercept_for_msr(MSR_GS_BASE, false);
6845 vmx_disable_intercept_for_msr(MSR_KERNEL_GS_BASE, true);
6846 vmx_disable_intercept_for_msr(MSR_IA32_SYSENTER_CS, false);
6847 vmx_disable_intercept_for_msr(MSR_IA32_SYSENTER_ESP, false);
6848 vmx_disable_intercept_for_msr(MSR_IA32_SYSENTER_EIP, false);
Tiejun Chenbaa03522014-12-23 16:21:11 +08006849
Wanpeng Lic63e4562016-09-23 19:17:16 +08006850 memcpy(vmx_msr_bitmap_legacy_x2apic_apicv,
6851 vmx_msr_bitmap_legacy, PAGE_SIZE);
6852 memcpy(vmx_msr_bitmap_longmode_x2apic_apicv,
6853 vmx_msr_bitmap_longmode, PAGE_SIZE);
Tiejun Chenbaa03522014-12-23 16:21:11 +08006854 memcpy(vmx_msr_bitmap_legacy_x2apic,
6855 vmx_msr_bitmap_legacy, PAGE_SIZE);
6856 memcpy(vmx_msr_bitmap_longmode_x2apic,
6857 vmx_msr_bitmap_longmode, PAGE_SIZE);
6858
Wanpeng Li04bb92e2015-09-16 19:31:11 +08006859 set_bit(0, vmx_vpid_bitmap); /* 0 is reserved for host */
6860
Radim Krčmář40d83382016-09-29 22:41:31 +02006861 for (msr = 0x800; msr <= 0x8ff; msr++) {
Paolo Bonzinid7231e72017-12-21 00:47:55 +01006862 if (msr == X2APIC_MSR(APIC_TMCCT))
Radim Krčmář40d83382016-09-29 22:41:31 +02006863 continue;
Radim Krčmář2e69f862016-09-29 22:41:32 +02006864 vmx_disable_intercept_msr_x2apic(msr, MSR_TYPE_R, true);
Radim Krčmář40d83382016-09-29 22:41:31 +02006865 }
Tiejun Chenbaa03522014-12-23 16:21:11 +08006866
Wanpeng Lif6e90f92016-09-22 07:43:25 +08006867 /*
Radim Krčmář2e69f862016-09-29 22:41:32 +02006868 * TPR reads and writes can be virtualized even if virtual interrupt
6869 * delivery is not in use.
Wanpeng Lif6e90f92016-09-22 07:43:25 +08006870 */
Paolo Bonzinid7231e72017-12-21 00:47:55 +01006871 vmx_disable_intercept_msr_x2apic(X2APIC_MSR(APIC_TASKPRI), MSR_TYPE_R | MSR_TYPE_W, false);
6872 vmx_disable_intercept_msr_x2apic(X2APIC_MSR(APIC_EOI), MSR_TYPE_W, true);
6873 vmx_disable_intercept_msr_x2apic(X2APIC_MSR(APIC_SELF_IPI), MSR_TYPE_W, true);
Tiejun Chenbaa03522014-12-23 16:21:11 +08006874
Junaid Shahidf160c7b2016-12-06 16:46:16 -08006875 if (enable_ept)
6876 vmx_enable_tdp();
6877 else
Tiejun Chenbaa03522014-12-23 16:21:11 +08006878 kvm_disable_tdp();
6879
6880 update_ple_window_actual_max();
6881
Kai Huang843e4332015-01-28 10:54:28 +08006882 /*
6883 * Only enable PML when hardware supports PML feature, and both EPT
6884 * and EPT A/D bit features are enabled -- PML depends on them to work.
6885 */
6886 if (!enable_ept || !enable_ept_ad_bits || !cpu_has_vmx_pml())
6887 enable_pml = 0;
6888
6889 if (!enable_pml) {
6890 kvm_x86_ops->slot_enable_log_dirty = NULL;
6891 kvm_x86_ops->slot_disable_log_dirty = NULL;
6892 kvm_x86_ops->flush_log_dirty = NULL;
6893 kvm_x86_ops->enable_log_dirty_pt_masked = NULL;
6894 }
6895
Yunhong Jiang64672c92016-06-13 14:19:59 -07006896 if (cpu_has_vmx_preemption_timer() && enable_preemption_timer) {
6897 u64 vmx_msr;
6898
6899 rdmsrl(MSR_IA32_VMX_MISC, vmx_msr);
6900 cpu_preemption_timer_multi =
6901 vmx_msr & VMX_MISC_PREEMPTION_TIMER_RATE_MASK;
6902 } else {
6903 kvm_x86_ops->set_hv_timer = NULL;
6904 kvm_x86_ops->cancel_hv_timer = NULL;
6905 }
6906
Paolo Bonzinic5d167b2017-12-13 11:05:19 +01006907 if (!cpu_has_vmx_shadow_vmcs())
6908 enable_shadow_vmcs = 0;
6909 if (enable_shadow_vmcs)
6910 init_vmcs_shadow_fields();
6911
Feng Wubf9f6ac2015-09-18 22:29:55 +08006912 kvm_set_posted_intr_wakeup_handler(wakeup_handler);
6913
Ashok Rajc45dcc72016-06-22 14:59:56 +08006914 kvm_mce_cap_supported |= MCG_LMCE_P;
6915
Tiejun Chenf2c76482014-10-28 10:14:47 +08006916 return alloc_kvm_area();
Tiejun Chen34a1cd62014-10-28 10:14:48 +08006917
Tiejun Chen34a1cd62014-10-28 10:14:48 +08006918out:
Radim Krčmář23611332016-09-29 22:41:33 +02006919 for (i = 0; i < VMX_BITMAP_NR; i++)
6920 free_page((unsigned long)vmx_bitmap[i]);
Tiejun Chen34a1cd62014-10-28 10:14:48 +08006921
6922 return r;
Tiejun Chenf2c76482014-10-28 10:14:47 +08006923}
6924
6925static __exit void hardware_unsetup(void)
6926{
Radim Krčmář23611332016-09-29 22:41:33 +02006927 int i;
6928
6929 for (i = 0; i < VMX_BITMAP_NR; i++)
6930 free_page((unsigned long)vmx_bitmap[i]);
Tiejun Chen34a1cd62014-10-28 10:14:48 +08006931
Tiejun Chenf2c76482014-10-28 10:14:47 +08006932 free_kvm_area();
6933}
6934
Avi Kivity6aa8b732006-12-10 02:21:36 -08006935/*
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08006936 * Indicate a busy-waiting vcpu in spinlock. We do not enable the PAUSE
6937 * exiting, so only get here on cpu with PAUSE-Loop-Exiting.
6938 */
Marcelo Tosatti9fb41ba2009-10-12 19:37:31 -03006939static int handle_pause(struct kvm_vcpu *vcpu)
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08006940{
Radim Krčmářb4a2d312014-08-21 18:08:08 +02006941 if (ple_gap)
6942 grow_ple_window(vcpu);
6943
Longpeng(Mike)de63ad42017-08-08 12:05:33 +08006944 /*
6945 * Intel sdm vol3 ch-25.1.3 says: The "PAUSE-loop exiting"
6946 * VM-execution control is ignored if CPL > 0. OTOH, KVM
6947 * never set PAUSE_EXITING and just set PLE if supported,
6948 * so the vcpu must be CPL=0 if it gets a PAUSE exit.
6949 */
6950 kvm_vcpu_on_spin(vcpu, true);
Kyle Huey6affcbe2016-11-29 12:40:40 -08006951 return kvm_skip_emulated_instruction(vcpu);
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08006952}
6953
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04006954static int handle_nop(struct kvm_vcpu *vcpu)
Sheng Yang59708672009-12-15 13:29:54 +08006955{
Kyle Huey6affcbe2016-11-29 12:40:40 -08006956 return kvm_skip_emulated_instruction(vcpu);
Sheng Yang59708672009-12-15 13:29:54 +08006957}
6958
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04006959static int handle_mwait(struct kvm_vcpu *vcpu)
6960{
6961 printk_once(KERN_WARNING "kvm: MWAIT instruction emulated as NOP!\n");
6962 return handle_nop(vcpu);
6963}
6964
Jim Mattson45ec3682017-08-23 16:32:04 -07006965static int handle_invalid_op(struct kvm_vcpu *vcpu)
6966{
6967 kvm_queue_exception(vcpu, UD_VECTOR);
6968 return 1;
6969}
6970
Mihai Donțu5f3d45e2015-07-05 20:08:57 +03006971static int handle_monitor_trap(struct kvm_vcpu *vcpu)
6972{
6973 return 1;
6974}
6975
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04006976static int handle_monitor(struct kvm_vcpu *vcpu)
6977{
6978 printk_once(KERN_WARNING "kvm: MONITOR instruction emulated as NOP!\n");
6979 return handle_nop(vcpu);
6980}
6981
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08006982/*
Arthur Chunqi Li0658fba2013-07-04 15:03:32 +08006983 * The following 3 functions, nested_vmx_succeed()/failValid()/failInvalid(),
6984 * set the success or error code of an emulated VMX instruction, as specified
6985 * by Vol 2B, VMX Instruction Reference, "Conventions".
6986 */
6987static void nested_vmx_succeed(struct kvm_vcpu *vcpu)
6988{
6989 vmx_set_rflags(vcpu, vmx_get_rflags(vcpu)
6990 & ~(X86_EFLAGS_CF | X86_EFLAGS_PF | X86_EFLAGS_AF |
6991 X86_EFLAGS_ZF | X86_EFLAGS_SF | X86_EFLAGS_OF));
6992}
6993
6994static void nested_vmx_failInvalid(struct kvm_vcpu *vcpu)
6995{
6996 vmx_set_rflags(vcpu, (vmx_get_rflags(vcpu)
6997 & ~(X86_EFLAGS_PF | X86_EFLAGS_AF | X86_EFLAGS_ZF |
6998 X86_EFLAGS_SF | X86_EFLAGS_OF))
6999 | X86_EFLAGS_CF);
7000}
7001
Abel Gordon145c28d2013-04-18 14:36:55 +03007002static void nested_vmx_failValid(struct kvm_vcpu *vcpu,
Arthur Chunqi Li0658fba2013-07-04 15:03:32 +08007003 u32 vm_instruction_error)
7004{
7005 if (to_vmx(vcpu)->nested.current_vmptr == -1ull) {
7006 /*
7007 * failValid writes the error number to the current VMCS, which
7008 * can't be done there isn't a current VMCS.
7009 */
7010 nested_vmx_failInvalid(vcpu);
7011 return;
7012 }
7013 vmx_set_rflags(vcpu, (vmx_get_rflags(vcpu)
7014 & ~(X86_EFLAGS_CF | X86_EFLAGS_PF | X86_EFLAGS_AF |
7015 X86_EFLAGS_SF | X86_EFLAGS_OF))
7016 | X86_EFLAGS_ZF);
7017 get_vmcs12(vcpu)->vm_instruction_error = vm_instruction_error;
7018 /*
7019 * We don't need to force a shadow sync because
7020 * VM_INSTRUCTION_ERROR is not shadowed
7021 */
7022}
Abel Gordon145c28d2013-04-18 14:36:55 +03007023
Wincy Vanff651cb2014-12-11 08:52:58 +03007024static void nested_vmx_abort(struct kvm_vcpu *vcpu, u32 indicator)
7025{
7026 /* TODO: not to reset guest simply here. */
7027 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Paolo Bonzinibbe41b92016-08-19 17:51:20 +02007028 pr_debug_ratelimited("kvm: nested vmx abort, indicator %d\n", indicator);
Wincy Vanff651cb2014-12-11 08:52:58 +03007029}
7030
Jan Kiszkaf41245002014-03-07 20:03:13 +01007031static enum hrtimer_restart vmx_preemption_timer_fn(struct hrtimer *timer)
7032{
7033 struct vcpu_vmx *vmx =
7034 container_of(timer, struct vcpu_vmx, nested.preemption_timer);
7035
7036 vmx->nested.preemption_timer_expired = true;
7037 kvm_make_request(KVM_REQ_EVENT, &vmx->vcpu);
7038 kvm_vcpu_kick(&vmx->vcpu);
7039
7040 return HRTIMER_NORESTART;
7041}
7042
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +03007043/*
Bandan Das19677e32014-05-06 02:19:15 -04007044 * Decode the memory-address operand of a vmx instruction, as recorded on an
7045 * exit caused by such an instruction (run by a guest hypervisor).
7046 * On success, returns 0. When the operand is invalid, returns 1 and throws
7047 * #UD or #GP.
7048 */
7049static int get_vmx_mem_address(struct kvm_vcpu *vcpu,
7050 unsigned long exit_qualification,
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007051 u32 vmx_instruction_info, bool wr, gva_t *ret)
Bandan Das19677e32014-05-06 02:19:15 -04007052{
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007053 gva_t off;
7054 bool exn;
7055 struct kvm_segment s;
7056
Bandan Das19677e32014-05-06 02:19:15 -04007057 /*
7058 * According to Vol. 3B, "Information for VM Exits Due to Instruction
7059 * Execution", on an exit, vmx_instruction_info holds most of the
7060 * addressing components of the operand. Only the displacement part
7061 * is put in exit_qualification (see 3B, "Basic VM-Exit Information").
7062 * For how an actual address is calculated from all these components,
7063 * refer to Vol. 1, "Operand Addressing".
7064 */
7065 int scaling = vmx_instruction_info & 3;
7066 int addr_size = (vmx_instruction_info >> 7) & 7;
7067 bool is_reg = vmx_instruction_info & (1u << 10);
7068 int seg_reg = (vmx_instruction_info >> 15) & 7;
7069 int index_reg = (vmx_instruction_info >> 18) & 0xf;
7070 bool index_is_valid = !(vmx_instruction_info & (1u << 22));
7071 int base_reg = (vmx_instruction_info >> 23) & 0xf;
7072 bool base_is_valid = !(vmx_instruction_info & (1u << 27));
7073
7074 if (is_reg) {
7075 kvm_queue_exception(vcpu, UD_VECTOR);
7076 return 1;
7077 }
7078
7079 /* Addr = segment_base + offset */
7080 /* offset = base + [index * scale] + displacement */
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007081 off = exit_qualification; /* holds the displacement */
Bandan Das19677e32014-05-06 02:19:15 -04007082 if (base_is_valid)
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007083 off += kvm_register_read(vcpu, base_reg);
Bandan Das19677e32014-05-06 02:19:15 -04007084 if (index_is_valid)
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007085 off += kvm_register_read(vcpu, index_reg)<<scaling;
7086 vmx_get_segment(vcpu, &s, seg_reg);
7087 *ret = s.base + off;
Bandan Das19677e32014-05-06 02:19:15 -04007088
7089 if (addr_size == 1) /* 32 bit */
7090 *ret &= 0xffffffff;
7091
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007092 /* Checks for #GP/#SS exceptions. */
7093 exn = false;
Quentin Casasnovasff30ef42016-06-18 11:01:05 +02007094 if (is_long_mode(vcpu)) {
7095 /* Long mode: #GP(0)/#SS(0) if the memory address is in a
7096 * non-canonical form. This is the only check on the memory
7097 * destination for long mode!
7098 */
Yu Zhangfd8cb432017-08-24 20:27:56 +08007099 exn = is_noncanonical_address(*ret, vcpu);
Quentin Casasnovasff30ef42016-06-18 11:01:05 +02007100 } else if (is_protmode(vcpu)) {
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007101 /* Protected mode: apply checks for segment validity in the
7102 * following order:
7103 * - segment type check (#GP(0) may be thrown)
7104 * - usability check (#GP(0)/#SS(0))
7105 * - limit check (#GP(0)/#SS(0))
7106 */
7107 if (wr)
7108 /* #GP(0) if the destination operand is located in a
7109 * read-only data segment or any code segment.
7110 */
7111 exn = ((s.type & 0xa) == 0 || (s.type & 8));
7112 else
7113 /* #GP(0) if the source operand is located in an
7114 * execute-only code segment
7115 */
7116 exn = ((s.type & 0xa) == 8);
Quentin Casasnovasff30ef42016-06-18 11:01:05 +02007117 if (exn) {
7118 kvm_queue_exception_e(vcpu, GP_VECTOR, 0);
7119 return 1;
7120 }
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007121 /* Protected mode: #GP(0)/#SS(0) if the segment is unusable.
7122 */
7123 exn = (s.unusable != 0);
7124 /* Protected mode: #GP(0)/#SS(0) if the memory
7125 * operand is outside the segment limit.
7126 */
7127 exn = exn || (off + sizeof(u64) > s.limit);
7128 }
7129 if (exn) {
7130 kvm_queue_exception_e(vcpu,
7131 seg_reg == VCPU_SREG_SS ?
7132 SS_VECTOR : GP_VECTOR,
7133 0);
7134 return 1;
7135 }
7136
Bandan Das19677e32014-05-06 02:19:15 -04007137 return 0;
7138}
7139
Radim Krčmářcbf71272017-05-19 15:48:51 +02007140static int nested_vmx_get_vmptr(struct kvm_vcpu *vcpu, gpa_t *vmpointer)
Bandan Das3573e222014-05-06 02:19:16 -04007141{
7142 gva_t gva;
Bandan Das3573e222014-05-06 02:19:16 -04007143 struct x86_exception e;
Bandan Das3573e222014-05-06 02:19:16 -04007144
7145 if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007146 vmcs_read32(VMX_INSTRUCTION_INFO), false, &gva))
Bandan Das3573e222014-05-06 02:19:16 -04007147 return 1;
7148
Radim Krčmářcbf71272017-05-19 15:48:51 +02007149 if (kvm_read_guest_virt(&vcpu->arch.emulate_ctxt, gva, vmpointer,
7150 sizeof(*vmpointer), &e)) {
Bandan Das3573e222014-05-06 02:19:16 -04007151 kvm_inject_page_fault(vcpu, &e);
7152 return 1;
7153 }
7154
Bandan Das3573e222014-05-06 02:19:16 -04007155 return 0;
7156}
7157
Jim Mattsone29acc52016-11-30 12:03:43 -08007158static int enter_vmx_operation(struct kvm_vcpu *vcpu)
7159{
7160 struct vcpu_vmx *vmx = to_vmx(vcpu);
7161 struct vmcs *shadow_vmcs;
7162
Jim Mattson00647b42017-11-27 17:22:25 -06007163 vmx->nested.vmcs02.vmcs = alloc_vmcs();
7164 vmx->nested.vmcs02.shadow_vmcs = NULL;
7165 if (!vmx->nested.vmcs02.vmcs)
7166 goto out_vmcs02;
7167 loaded_vmcs_init(&vmx->nested.vmcs02);
7168
Jim Mattsone29acc52016-11-30 12:03:43 -08007169 if (cpu_has_vmx_msr_bitmap()) {
7170 vmx->nested.msr_bitmap =
7171 (unsigned long *)__get_free_page(GFP_KERNEL);
7172 if (!vmx->nested.msr_bitmap)
7173 goto out_msr_bitmap;
Paolo Bonzinic9923842017-12-13 14:16:30 +01007174 memset(vmx->nested.msr_bitmap, 0xff, PAGE_SIZE);
Jim Mattsone29acc52016-11-30 12:03:43 -08007175 }
7176
7177 vmx->nested.cached_vmcs12 = kmalloc(VMCS12_SIZE, GFP_KERNEL);
7178 if (!vmx->nested.cached_vmcs12)
7179 goto out_cached_vmcs12;
7180
7181 if (enable_shadow_vmcs) {
7182 shadow_vmcs = alloc_vmcs();
7183 if (!shadow_vmcs)
7184 goto out_shadow_vmcs;
7185 /* mark vmcs as shadow */
7186 shadow_vmcs->revision_id |= (1u << 31);
7187 /* init shadow vmcs */
7188 vmcs_clear(shadow_vmcs);
7189 vmx->vmcs01.shadow_vmcs = shadow_vmcs;
7190 }
7191
Jim Mattsone29acc52016-11-30 12:03:43 -08007192 hrtimer_init(&vmx->nested.preemption_timer, CLOCK_MONOTONIC,
7193 HRTIMER_MODE_REL_PINNED);
7194 vmx->nested.preemption_timer.function = vmx_preemption_timer_fn;
7195
7196 vmx->nested.vmxon = true;
7197 return 0;
7198
7199out_shadow_vmcs:
7200 kfree(vmx->nested.cached_vmcs12);
7201
7202out_cached_vmcs12:
7203 free_page((unsigned long)vmx->nested.msr_bitmap);
7204
7205out_msr_bitmap:
Mark Kanda276c7962017-11-27 17:22:26 -06007206 vmx_nested_free_vmcs02(vmx);
Jim Mattson00647b42017-11-27 17:22:25 -06007207
7208out_vmcs02:
Jim Mattsone29acc52016-11-30 12:03:43 -08007209 return -ENOMEM;
7210}
7211
Bandan Das3573e222014-05-06 02:19:16 -04007212/*
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007213 * Emulate the VMXON instruction.
7214 * Currently, we just remember that VMX is active, and do not save or even
7215 * inspect the argument to VMXON (the so-called "VMXON pointer") because we
7216 * do not currently need to store anything in that guest-allocated memory
7217 * region. Consequently, VMCLEAR and VMPTRLD also do not verify that the their
7218 * argument is different from the VMXON pointer (which the spec says they do).
7219 */
7220static int handle_vmon(struct kvm_vcpu *vcpu)
7221{
Jim Mattsone29acc52016-11-30 12:03:43 -08007222 int ret;
Radim Krčmářcbf71272017-05-19 15:48:51 +02007223 gpa_t vmptr;
7224 struct page *page;
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007225 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'Elb3897a42013-07-08 19:12:35 +08007226 const u64 VMXON_NEEDED_FEATURES = FEATURE_CONTROL_LOCKED
7227 | FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007228
Jim Mattson70f3aac2017-04-26 08:53:46 -07007229 /*
7230 * The Intel VMX Instruction Reference lists a bunch of bits that are
7231 * prerequisite to running VMXON, most notably cr4.VMXE must be set to
7232 * 1 (see vmx_set_cr4() for when we allow the guest to set this).
7233 * Otherwise, we should fail with #UD. But most faulting conditions
7234 * have already been checked by hardware, prior to the VM-exit for
7235 * VMXON. We do test guest cr4.VMXE because processor CR4 always has
7236 * that bit set to 1 in non-root mode.
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007237 */
Jim Mattson70f3aac2017-04-26 08:53:46 -07007238 if (!kvm_read_cr4_bits(vcpu, X86_CR4_VMXE)) {
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007239 kvm_queue_exception(vcpu, UD_VECTOR);
7240 return 1;
7241 }
7242
Abel Gordon145c28d2013-04-18 14:36:55 +03007243 if (vmx->nested.vmxon) {
7244 nested_vmx_failValid(vcpu, VMXERR_VMXON_IN_VMX_ROOT_OPERATION);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007245 return kvm_skip_emulated_instruction(vcpu);
Abel Gordon145c28d2013-04-18 14:36:55 +03007246 }
Nadav Har'Elb3897a42013-07-08 19:12:35 +08007247
Haozhong Zhang3b840802016-06-22 14:59:54 +08007248 if ((vmx->msr_ia32_feature_control & VMXON_NEEDED_FEATURES)
Nadav Har'Elb3897a42013-07-08 19:12:35 +08007249 != VMXON_NEEDED_FEATURES) {
7250 kvm_inject_gp(vcpu, 0);
7251 return 1;
7252 }
7253
Radim Krčmářcbf71272017-05-19 15:48:51 +02007254 if (nested_vmx_get_vmptr(vcpu, &vmptr))
Jim Mattson21e7fbe2016-12-22 15:49:55 -08007255 return 1;
Radim Krčmářcbf71272017-05-19 15:48:51 +02007256
7257 /*
7258 * SDM 3: 24.11.5
7259 * The first 4 bytes of VMXON region contain the supported
7260 * VMCS revision identifier
7261 *
7262 * Note - IA32_VMX_BASIC[48] will never be 1 for the nested case;
7263 * which replaces physical address width with 32
7264 */
7265 if (!PAGE_ALIGNED(vmptr) || (vmptr >> cpuid_maxphyaddr(vcpu))) {
7266 nested_vmx_failInvalid(vcpu);
7267 return kvm_skip_emulated_instruction(vcpu);
7268 }
7269
David Hildenbrand5e2f30b2017-08-03 18:11:04 +02007270 page = kvm_vcpu_gpa_to_page(vcpu, vmptr);
7271 if (is_error_page(page)) {
Radim Krčmářcbf71272017-05-19 15:48:51 +02007272 nested_vmx_failInvalid(vcpu);
7273 return kvm_skip_emulated_instruction(vcpu);
7274 }
7275 if (*(u32 *)kmap(page) != VMCS12_REVISION) {
7276 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +02007277 kvm_release_page_clean(page);
Radim Krčmářcbf71272017-05-19 15:48:51 +02007278 nested_vmx_failInvalid(vcpu);
7279 return kvm_skip_emulated_instruction(vcpu);
7280 }
7281 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +02007282 kvm_release_page_clean(page);
Radim Krčmářcbf71272017-05-19 15:48:51 +02007283
7284 vmx->nested.vmxon_ptr = vmptr;
Jim Mattsone29acc52016-11-30 12:03:43 -08007285 ret = enter_vmx_operation(vcpu);
7286 if (ret)
7287 return ret;
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007288
Arthur Chunqi Lia25eb112013-07-04 15:03:33 +08007289 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007290 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007291}
7292
7293/*
7294 * Intel's VMX Instruction Reference specifies a common set of prerequisites
7295 * for running VMX instructions (except VMXON, whose prerequisites are
7296 * slightly different). It also specifies what exception to inject otherwise.
Jim Mattson70f3aac2017-04-26 08:53:46 -07007297 * Note that many of these exceptions have priority over VM exits, so they
7298 * don't have to be checked again here.
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007299 */
7300static int nested_vmx_check_permission(struct kvm_vcpu *vcpu)
7301{
Jim Mattson70f3aac2017-04-26 08:53:46 -07007302 if (!to_vmx(vcpu)->nested.vmxon) {
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007303 kvm_queue_exception(vcpu, UD_VECTOR);
7304 return 0;
7305 }
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007306 return 1;
7307}
7308
David Matlack8ca44e82017-08-01 14:00:39 -07007309static void vmx_disable_shadow_vmcs(struct vcpu_vmx *vmx)
7310{
7311 vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL, SECONDARY_EXEC_SHADOW_VMCS);
7312 vmcs_write64(VMCS_LINK_POINTER, -1ull);
7313}
7314
Abel Gordone7953d72013-04-18 14:37:55 +03007315static inline void nested_release_vmcs12(struct vcpu_vmx *vmx)
7316{
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02007317 if (vmx->nested.current_vmptr == -1ull)
7318 return;
7319
Abel Gordon012f83c2013-04-18 14:39:25 +03007320 if (enable_shadow_vmcs) {
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02007321 /* copy to memory all shadowed fields in case
7322 they were modified */
7323 copy_shadow_to_vmcs12(vmx);
7324 vmx->nested.sync_shadow_vmcs = false;
David Matlack8ca44e82017-08-01 14:00:39 -07007325 vmx_disable_shadow_vmcs(vmx);
Abel Gordon012f83c2013-04-18 14:39:25 +03007326 }
Wincy Van705699a2015-02-03 23:58:17 +08007327 vmx->nested.posted_intr_nv = -1;
David Matlack4f2777b2016-07-13 17:16:37 -07007328
7329 /* Flush VMCS12 to guest memory */
Paolo Bonzini9f744c52017-07-27 15:54:46 +02007330 kvm_vcpu_write_guest_page(&vmx->vcpu,
7331 vmx->nested.current_vmptr >> PAGE_SHIFT,
7332 vmx->nested.cached_vmcs12, 0, VMCS12_SIZE);
David Matlack4f2777b2016-07-13 17:16:37 -07007333
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02007334 vmx->nested.current_vmptr = -1ull;
Abel Gordone7953d72013-04-18 14:37:55 +03007335}
7336
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007337/*
7338 * Free whatever needs to be freed from vmx->nested when L1 goes down, or
7339 * just stops using VMX.
7340 */
7341static void free_nested(struct vcpu_vmx *vmx)
7342{
Wanpeng Lib7455822017-11-22 14:04:00 -08007343 if (!vmx->nested.vmxon && !vmx->nested.smm.vmxon)
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007344 return;
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02007345
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007346 vmx->nested.vmxon = false;
Wanpeng Lib7455822017-11-22 14:04:00 -08007347 vmx->nested.smm.vmxon = false;
Wanpeng Li5c614b32015-10-13 09:18:36 -07007348 free_vpid(vmx->nested.vpid02);
David Matlack8ca44e82017-08-01 14:00:39 -07007349 vmx->nested.posted_intr_nv = -1;
7350 vmx->nested.current_vmptr = -1ull;
Radim Krčmářd048c092016-08-08 20:16:22 +02007351 if (vmx->nested.msr_bitmap) {
7352 free_page((unsigned long)vmx->nested.msr_bitmap);
7353 vmx->nested.msr_bitmap = NULL;
7354 }
Jim Mattson355f4fb2016-10-28 08:29:39 -07007355 if (enable_shadow_vmcs) {
David Matlack8ca44e82017-08-01 14:00:39 -07007356 vmx_disable_shadow_vmcs(vmx);
Jim Mattson355f4fb2016-10-28 08:29:39 -07007357 vmcs_clear(vmx->vmcs01.shadow_vmcs);
7358 free_vmcs(vmx->vmcs01.shadow_vmcs);
7359 vmx->vmcs01.shadow_vmcs = NULL;
7360 }
David Matlack4f2777b2016-07-13 17:16:37 -07007361 kfree(vmx->nested.cached_vmcs12);
Jim Mattson00647b42017-11-27 17:22:25 -06007362 /* Unpin physical memory we referred to in the vmcs02 */
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03007363 if (vmx->nested.apic_access_page) {
David Hildenbrand53a70da2017-08-03 18:11:05 +02007364 kvm_release_page_dirty(vmx->nested.apic_access_page);
Paolo Bonzini48d89b92014-08-26 13:27:46 +02007365 vmx->nested.apic_access_page = NULL;
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03007366 }
Wanpeng Lia7c0b072014-08-21 19:46:50 +08007367 if (vmx->nested.virtual_apic_page) {
David Hildenbrand53a70da2017-08-03 18:11:05 +02007368 kvm_release_page_dirty(vmx->nested.virtual_apic_page);
Paolo Bonzini48d89b92014-08-26 13:27:46 +02007369 vmx->nested.virtual_apic_page = NULL;
Wanpeng Lia7c0b072014-08-21 19:46:50 +08007370 }
Wincy Van705699a2015-02-03 23:58:17 +08007371 if (vmx->nested.pi_desc_page) {
7372 kunmap(vmx->nested.pi_desc_page);
David Hildenbrand53a70da2017-08-03 18:11:05 +02007373 kvm_release_page_dirty(vmx->nested.pi_desc_page);
Wincy Van705699a2015-02-03 23:58:17 +08007374 vmx->nested.pi_desc_page = NULL;
7375 vmx->nested.pi_desc = NULL;
7376 }
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +03007377
Mark Kanda276c7962017-11-27 17:22:26 -06007378 vmx_nested_free_vmcs02(vmx);
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007379}
7380
7381/* Emulate the VMXOFF instruction */
7382static int handle_vmoff(struct kvm_vcpu *vcpu)
7383{
7384 if (!nested_vmx_check_permission(vcpu))
7385 return 1;
7386 free_nested(to_vmx(vcpu));
Arthur Chunqi Lia25eb112013-07-04 15:03:33 +08007387 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007388 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007389}
7390
Nadav Har'El27d6c862011-05-25 23:06:59 +03007391/* Emulate the VMCLEAR instruction */
7392static int handle_vmclear(struct kvm_vcpu *vcpu)
7393{
7394 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jim Mattson587d7e722017-03-02 12:41:48 -08007395 u32 zero = 0;
Nadav Har'El27d6c862011-05-25 23:06:59 +03007396 gpa_t vmptr;
Nadav Har'El27d6c862011-05-25 23:06:59 +03007397
7398 if (!nested_vmx_check_permission(vcpu))
7399 return 1;
7400
Radim Krčmářcbf71272017-05-19 15:48:51 +02007401 if (nested_vmx_get_vmptr(vcpu, &vmptr))
Nadav Har'El27d6c862011-05-25 23:06:59 +03007402 return 1;
7403
Radim Krčmářcbf71272017-05-19 15:48:51 +02007404 if (!PAGE_ALIGNED(vmptr) || (vmptr >> cpuid_maxphyaddr(vcpu))) {
7405 nested_vmx_failValid(vcpu, VMXERR_VMCLEAR_INVALID_ADDRESS);
7406 return kvm_skip_emulated_instruction(vcpu);
7407 }
7408
7409 if (vmptr == vmx->nested.vmxon_ptr) {
7410 nested_vmx_failValid(vcpu, VMXERR_VMCLEAR_VMXON_POINTER);
7411 return kvm_skip_emulated_instruction(vcpu);
7412 }
7413
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02007414 if (vmptr == vmx->nested.current_vmptr)
Abel Gordone7953d72013-04-18 14:37:55 +03007415 nested_release_vmcs12(vmx);
Nadav Har'El27d6c862011-05-25 23:06:59 +03007416
Jim Mattson587d7e722017-03-02 12:41:48 -08007417 kvm_vcpu_write_guest(vcpu,
7418 vmptr + offsetof(struct vmcs12, launch_state),
7419 &zero, sizeof(zero));
Nadav Har'El27d6c862011-05-25 23:06:59 +03007420
Nadav Har'El27d6c862011-05-25 23:06:59 +03007421 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007422 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El27d6c862011-05-25 23:06:59 +03007423}
7424
Nadav Har'Elcd232ad2011-05-25 23:10:33 +03007425static int nested_vmx_run(struct kvm_vcpu *vcpu, bool launch);
7426
7427/* Emulate the VMLAUNCH instruction */
7428static int handle_vmlaunch(struct kvm_vcpu *vcpu)
7429{
7430 return nested_vmx_run(vcpu, true);
7431}
7432
7433/* Emulate the VMRESUME instruction */
7434static int handle_vmresume(struct kvm_vcpu *vcpu)
7435{
7436
7437 return nested_vmx_run(vcpu, false);
7438}
7439
Nadav Har'El49f705c2011-05-25 23:08:30 +03007440/*
7441 * Read a vmcs12 field. Since these can have varying lengths and we return
7442 * one type, we chose the biggest type (u64) and zero-extend the return value
7443 * to that size. Note that the caller, handle_vmread, might need to use only
7444 * some of the bits we return here (e.g., on 32-bit guests, only 32 bits of
7445 * 64-bit fields are to be returned).
7446 */
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007447static inline int vmcs12_read_any(struct kvm_vcpu *vcpu,
7448 unsigned long field, u64 *ret)
Nadav Har'El49f705c2011-05-25 23:08:30 +03007449{
7450 short offset = vmcs_field_to_offset(field);
7451 char *p;
7452
7453 if (offset < 0)
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007454 return offset;
Nadav Har'El49f705c2011-05-25 23:08:30 +03007455
7456 p = ((char *)(get_vmcs12(vcpu))) + offset;
7457
Jim Mattsond37f4262017-12-22 12:12:16 -08007458 switch (vmcs_field_width(field)) {
7459 case VMCS_FIELD_WIDTH_NATURAL_WIDTH:
Nadav Har'El49f705c2011-05-25 23:08:30 +03007460 *ret = *((natural_width *)p);
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007461 return 0;
Jim Mattsond37f4262017-12-22 12:12:16 -08007462 case VMCS_FIELD_WIDTH_U16:
Nadav Har'El49f705c2011-05-25 23:08:30 +03007463 *ret = *((u16 *)p);
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007464 return 0;
Jim Mattsond37f4262017-12-22 12:12:16 -08007465 case VMCS_FIELD_WIDTH_U32:
Nadav Har'El49f705c2011-05-25 23:08:30 +03007466 *ret = *((u32 *)p);
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007467 return 0;
Jim Mattsond37f4262017-12-22 12:12:16 -08007468 case VMCS_FIELD_WIDTH_U64:
Nadav Har'El49f705c2011-05-25 23:08:30 +03007469 *ret = *((u64 *)p);
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007470 return 0;
Nadav Har'El49f705c2011-05-25 23:08:30 +03007471 default:
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007472 WARN_ON(1);
7473 return -ENOENT;
Nadav Har'El49f705c2011-05-25 23:08:30 +03007474 }
7475}
7476
Abel Gordon20b97fe2013-04-18 14:36:25 +03007477
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007478static inline int vmcs12_write_any(struct kvm_vcpu *vcpu,
7479 unsigned long field, u64 field_value){
Abel Gordon20b97fe2013-04-18 14:36:25 +03007480 short offset = vmcs_field_to_offset(field);
7481 char *p = ((char *) get_vmcs12(vcpu)) + offset;
7482 if (offset < 0)
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007483 return offset;
Abel Gordon20b97fe2013-04-18 14:36:25 +03007484
Jim Mattsond37f4262017-12-22 12:12:16 -08007485 switch (vmcs_field_width(field)) {
7486 case VMCS_FIELD_WIDTH_U16:
Abel Gordon20b97fe2013-04-18 14:36:25 +03007487 *(u16 *)p = field_value;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007488 return 0;
Jim Mattsond37f4262017-12-22 12:12:16 -08007489 case VMCS_FIELD_WIDTH_U32:
Abel Gordon20b97fe2013-04-18 14:36:25 +03007490 *(u32 *)p = field_value;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007491 return 0;
Jim Mattsond37f4262017-12-22 12:12:16 -08007492 case VMCS_FIELD_WIDTH_U64:
Abel Gordon20b97fe2013-04-18 14:36:25 +03007493 *(u64 *)p = field_value;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007494 return 0;
Jim Mattsond37f4262017-12-22 12:12:16 -08007495 case VMCS_FIELD_WIDTH_NATURAL_WIDTH:
Abel Gordon20b97fe2013-04-18 14:36:25 +03007496 *(natural_width *)p = field_value;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007497 return 0;
Abel Gordon20b97fe2013-04-18 14:36:25 +03007498 default:
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007499 WARN_ON(1);
7500 return -ENOENT;
Abel Gordon20b97fe2013-04-18 14:36:25 +03007501 }
7502
7503}
7504
Abel Gordon16f5b902013-04-18 14:38:25 +03007505static void copy_shadow_to_vmcs12(struct vcpu_vmx *vmx)
7506{
7507 int i;
7508 unsigned long field;
7509 u64 field_value;
Jim Mattson355f4fb2016-10-28 08:29:39 -07007510 struct vmcs *shadow_vmcs = vmx->vmcs01.shadow_vmcs;
Paolo Bonzini44900ba2017-12-13 12:58:02 +01007511 const u16 *fields = shadow_read_write_fields;
Mathias Krausec2bae892013-06-26 20:36:21 +02007512 const int num_fields = max_shadow_read_write_fields;
Abel Gordon16f5b902013-04-18 14:38:25 +03007513
Jan Kiszka282da872014-10-08 18:05:39 +02007514 preempt_disable();
7515
Abel Gordon16f5b902013-04-18 14:38:25 +03007516 vmcs_load(shadow_vmcs);
7517
7518 for (i = 0; i < num_fields; i++) {
7519 field = fields[i];
Paolo Bonzini44900ba2017-12-13 12:58:02 +01007520 field_value = __vmcs_readl(field);
Abel Gordon16f5b902013-04-18 14:38:25 +03007521 vmcs12_write_any(&vmx->vcpu, field, field_value);
7522 }
7523
7524 vmcs_clear(shadow_vmcs);
7525 vmcs_load(vmx->loaded_vmcs->vmcs);
Jan Kiszka282da872014-10-08 18:05:39 +02007526
7527 preempt_enable();
Abel Gordon16f5b902013-04-18 14:38:25 +03007528}
7529
Abel Gordonc3114422013-04-18 14:38:55 +03007530static void copy_vmcs12_to_shadow(struct vcpu_vmx *vmx)
7531{
Paolo Bonzini44900ba2017-12-13 12:58:02 +01007532 const u16 *fields[] = {
Mathias Krausec2bae892013-06-26 20:36:21 +02007533 shadow_read_write_fields,
7534 shadow_read_only_fields
Abel Gordonc3114422013-04-18 14:38:55 +03007535 };
Mathias Krausec2bae892013-06-26 20:36:21 +02007536 const int max_fields[] = {
Abel Gordonc3114422013-04-18 14:38:55 +03007537 max_shadow_read_write_fields,
7538 max_shadow_read_only_fields
7539 };
7540 int i, q;
7541 unsigned long field;
7542 u64 field_value = 0;
Jim Mattson355f4fb2016-10-28 08:29:39 -07007543 struct vmcs *shadow_vmcs = vmx->vmcs01.shadow_vmcs;
Abel Gordonc3114422013-04-18 14:38:55 +03007544
7545 vmcs_load(shadow_vmcs);
7546
Mathias Krausec2bae892013-06-26 20:36:21 +02007547 for (q = 0; q < ARRAY_SIZE(fields); q++) {
Abel Gordonc3114422013-04-18 14:38:55 +03007548 for (i = 0; i < max_fields[q]; i++) {
7549 field = fields[q][i];
7550 vmcs12_read_any(&vmx->vcpu, field, &field_value);
Paolo Bonzini44900ba2017-12-13 12:58:02 +01007551 __vmcs_writel(field, field_value);
Abel Gordonc3114422013-04-18 14:38:55 +03007552 }
7553 }
7554
7555 vmcs_clear(shadow_vmcs);
7556 vmcs_load(vmx->loaded_vmcs->vmcs);
7557}
7558
Nadav Har'El49f705c2011-05-25 23:08:30 +03007559/*
7560 * VMX instructions which assume a current vmcs12 (i.e., that VMPTRLD was
7561 * used before) all generate the same failure when it is missing.
7562 */
7563static int nested_vmx_check_vmcs12(struct kvm_vcpu *vcpu)
7564{
7565 struct vcpu_vmx *vmx = to_vmx(vcpu);
7566 if (vmx->nested.current_vmptr == -1ull) {
7567 nested_vmx_failInvalid(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03007568 return 0;
7569 }
7570 return 1;
7571}
7572
7573static int handle_vmread(struct kvm_vcpu *vcpu)
7574{
7575 unsigned long field;
7576 u64 field_value;
7577 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7578 u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
7579 gva_t gva = 0;
7580
Kyle Hueyeb277562016-11-29 12:40:39 -08007581 if (!nested_vmx_check_permission(vcpu))
Nadav Har'El49f705c2011-05-25 23:08:30 +03007582 return 1;
7583
Kyle Huey6affcbe2016-11-29 12:40:40 -08007584 if (!nested_vmx_check_vmcs12(vcpu))
7585 return kvm_skip_emulated_instruction(vcpu);
Kyle Hueyeb277562016-11-29 12:40:39 -08007586
Nadav Har'El49f705c2011-05-25 23:08:30 +03007587 /* Decode instruction info and find the field to read */
Nadav Amit27e6fb52014-06-18 17:19:26 +03007588 field = kvm_register_readl(vcpu, (((vmx_instruction_info) >> 28) & 0xf));
Nadav Har'El49f705c2011-05-25 23:08:30 +03007589 /* Read the field, zero-extended to a u64 field_value */
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007590 if (vmcs12_read_any(vcpu, field, &field_value) < 0) {
Nadav Har'El49f705c2011-05-25 23:08:30 +03007591 nested_vmx_failValid(vcpu, VMXERR_UNSUPPORTED_VMCS_COMPONENT);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007592 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03007593 }
7594 /*
7595 * Now copy part of this value to register or memory, as requested.
7596 * Note that the number of bits actually copied is 32 or 64 depending
7597 * on the guest's mode (32 or 64 bit), not on the given field's length.
7598 */
7599 if (vmx_instruction_info & (1u << 10)) {
Nadav Amit27e6fb52014-06-18 17:19:26 +03007600 kvm_register_writel(vcpu, (((vmx_instruction_info) >> 3) & 0xf),
Nadav Har'El49f705c2011-05-25 23:08:30 +03007601 field_value);
7602 } else {
7603 if (get_vmx_mem_address(vcpu, exit_qualification,
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007604 vmx_instruction_info, true, &gva))
Nadav Har'El49f705c2011-05-25 23:08:30 +03007605 return 1;
Jim Mattson70f3aac2017-04-26 08:53:46 -07007606 /* _system ok, as hardware has verified cpl=0 */
Nadav Har'El49f705c2011-05-25 23:08:30 +03007607 kvm_write_guest_virt_system(&vcpu->arch.emulate_ctxt, gva,
7608 &field_value, (is_long_mode(vcpu) ? 8 : 4), NULL);
7609 }
7610
7611 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007612 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03007613}
7614
7615
7616static int handle_vmwrite(struct kvm_vcpu *vcpu)
7617{
7618 unsigned long field;
7619 gva_t gva;
Paolo Bonzini74a497f2017-12-20 13:55:39 +01007620 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03007621 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7622 u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
Paolo Bonzini74a497f2017-12-20 13:55:39 +01007623
Nadav Har'El49f705c2011-05-25 23:08:30 +03007624 /* The value to write might be 32 or 64 bits, depending on L1's long
7625 * mode, and eventually we need to write that into a field of several
7626 * possible lengths. The code below first zero-extends the value to 64
Adam Buchbinder6a6256f2016-02-23 15:34:30 -08007627 * bit (field_value), and then copies only the appropriate number of
Nadav Har'El49f705c2011-05-25 23:08:30 +03007628 * bits into the vmcs12 field.
7629 */
7630 u64 field_value = 0;
7631 struct x86_exception e;
7632
Kyle Hueyeb277562016-11-29 12:40:39 -08007633 if (!nested_vmx_check_permission(vcpu))
Nadav Har'El49f705c2011-05-25 23:08:30 +03007634 return 1;
7635
Kyle Huey6affcbe2016-11-29 12:40:40 -08007636 if (!nested_vmx_check_vmcs12(vcpu))
7637 return kvm_skip_emulated_instruction(vcpu);
Kyle Hueyeb277562016-11-29 12:40:39 -08007638
Nadav Har'El49f705c2011-05-25 23:08:30 +03007639 if (vmx_instruction_info & (1u << 10))
Nadav Amit27e6fb52014-06-18 17:19:26 +03007640 field_value = kvm_register_readl(vcpu,
Nadav Har'El49f705c2011-05-25 23:08:30 +03007641 (((vmx_instruction_info) >> 3) & 0xf));
7642 else {
7643 if (get_vmx_mem_address(vcpu, exit_qualification,
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007644 vmx_instruction_info, false, &gva))
Nadav Har'El49f705c2011-05-25 23:08:30 +03007645 return 1;
7646 if (kvm_read_guest_virt(&vcpu->arch.emulate_ctxt, gva,
Nadav Amit27e6fb52014-06-18 17:19:26 +03007647 &field_value, (is_64_bit_mode(vcpu) ? 8 : 4), &e)) {
Nadav Har'El49f705c2011-05-25 23:08:30 +03007648 kvm_inject_page_fault(vcpu, &e);
7649 return 1;
7650 }
7651 }
7652
7653
Nadav Amit27e6fb52014-06-18 17:19:26 +03007654 field = kvm_register_readl(vcpu, (((vmx_instruction_info) >> 28) & 0xf));
Nadav Har'El49f705c2011-05-25 23:08:30 +03007655 if (vmcs_field_readonly(field)) {
7656 nested_vmx_failValid(vcpu,
7657 VMXERR_VMWRITE_READ_ONLY_VMCS_COMPONENT);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007658 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03007659 }
7660
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007661 if (vmcs12_write_any(vcpu, field, field_value) < 0) {
Nadav Har'El49f705c2011-05-25 23:08:30 +03007662 nested_vmx_failValid(vcpu, VMXERR_UNSUPPORTED_VMCS_COMPONENT);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007663 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03007664 }
7665
Paolo Bonzini74a497f2017-12-20 13:55:39 +01007666 switch (field) {
7667#define SHADOW_FIELD_RW(x) case x:
7668#include "vmx_shadow_fields.h"
7669 /*
7670 * The fields that can be updated by L1 without a vmexit are
7671 * always updated in the vmcs02, the others go down the slow
7672 * path of prepare_vmcs02.
7673 */
7674 break;
7675 default:
7676 vmx->nested.dirty_vmcs12 = true;
7677 break;
7678 }
7679
Nadav Har'El49f705c2011-05-25 23:08:30 +03007680 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007681 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03007682}
7683
Jim Mattsona8bc2842016-11-30 12:03:44 -08007684static void set_current_vmptr(struct vcpu_vmx *vmx, gpa_t vmptr)
7685{
7686 vmx->nested.current_vmptr = vmptr;
7687 if (enable_shadow_vmcs) {
7688 vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL,
7689 SECONDARY_EXEC_SHADOW_VMCS);
7690 vmcs_write64(VMCS_LINK_POINTER,
7691 __pa(vmx->vmcs01.shadow_vmcs));
7692 vmx->nested.sync_shadow_vmcs = true;
7693 }
Paolo Bonzini74a497f2017-12-20 13:55:39 +01007694 vmx->nested.dirty_vmcs12 = true;
Jim Mattsona8bc2842016-11-30 12:03:44 -08007695}
7696
Nadav Har'El63846662011-05-25 23:07:29 +03007697/* Emulate the VMPTRLD instruction */
7698static int handle_vmptrld(struct kvm_vcpu *vcpu)
7699{
7700 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'El63846662011-05-25 23:07:29 +03007701 gpa_t vmptr;
Nadav Har'El63846662011-05-25 23:07:29 +03007702
7703 if (!nested_vmx_check_permission(vcpu))
7704 return 1;
7705
Radim Krčmářcbf71272017-05-19 15:48:51 +02007706 if (nested_vmx_get_vmptr(vcpu, &vmptr))
Nadav Har'El63846662011-05-25 23:07:29 +03007707 return 1;
7708
Radim Krčmářcbf71272017-05-19 15:48:51 +02007709 if (!PAGE_ALIGNED(vmptr) || (vmptr >> cpuid_maxphyaddr(vcpu))) {
7710 nested_vmx_failValid(vcpu, VMXERR_VMPTRLD_INVALID_ADDRESS);
7711 return kvm_skip_emulated_instruction(vcpu);
7712 }
7713
7714 if (vmptr == vmx->nested.vmxon_ptr) {
7715 nested_vmx_failValid(vcpu, VMXERR_VMPTRLD_VMXON_POINTER);
7716 return kvm_skip_emulated_instruction(vcpu);
7717 }
7718
Nadav Har'El63846662011-05-25 23:07:29 +03007719 if (vmx->nested.current_vmptr != vmptr) {
7720 struct vmcs12 *new_vmcs12;
7721 struct page *page;
David Hildenbrand5e2f30b2017-08-03 18:11:04 +02007722 page = kvm_vcpu_gpa_to_page(vcpu, vmptr);
7723 if (is_error_page(page)) {
Nadav Har'El63846662011-05-25 23:07:29 +03007724 nested_vmx_failInvalid(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007725 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El63846662011-05-25 23:07:29 +03007726 }
7727 new_vmcs12 = kmap(page);
7728 if (new_vmcs12->revision_id != VMCS12_REVISION) {
7729 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +02007730 kvm_release_page_clean(page);
Nadav Har'El63846662011-05-25 23:07:29 +03007731 nested_vmx_failValid(vcpu,
7732 VMXERR_VMPTRLD_INCORRECT_VMCS_REVISION_ID);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007733 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El63846662011-05-25 23:07:29 +03007734 }
Nadav Har'El63846662011-05-25 23:07:29 +03007735
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02007736 nested_release_vmcs12(vmx);
David Matlack4f2777b2016-07-13 17:16:37 -07007737 /*
7738 * Load VMCS12 from guest memory since it is not already
7739 * cached.
7740 */
Paolo Bonzini9f744c52017-07-27 15:54:46 +02007741 memcpy(vmx->nested.cached_vmcs12, new_vmcs12, VMCS12_SIZE);
7742 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +02007743 kvm_release_page_clean(page);
Paolo Bonzini9f744c52017-07-27 15:54:46 +02007744
Jim Mattsona8bc2842016-11-30 12:03:44 -08007745 set_current_vmptr(vmx, vmptr);
Nadav Har'El63846662011-05-25 23:07:29 +03007746 }
7747
7748 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007749 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El63846662011-05-25 23:07:29 +03007750}
7751
Nadav Har'El6a4d7552011-05-25 23:08:00 +03007752/* Emulate the VMPTRST instruction */
7753static int handle_vmptrst(struct kvm_vcpu *vcpu)
7754{
7755 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7756 u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
7757 gva_t vmcs_gva;
7758 struct x86_exception e;
7759
7760 if (!nested_vmx_check_permission(vcpu))
7761 return 1;
7762
7763 if (get_vmx_mem_address(vcpu, exit_qualification,
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007764 vmx_instruction_info, true, &vmcs_gva))
Nadav Har'El6a4d7552011-05-25 23:08:00 +03007765 return 1;
Jim Mattson70f3aac2017-04-26 08:53:46 -07007766 /* ok to use *_system, as hardware has verified cpl=0 */
Nadav Har'El6a4d7552011-05-25 23:08:00 +03007767 if (kvm_write_guest_virt_system(&vcpu->arch.emulate_ctxt, vmcs_gva,
7768 (void *)&to_vmx(vcpu)->nested.current_vmptr,
7769 sizeof(u64), &e)) {
7770 kvm_inject_page_fault(vcpu, &e);
7771 return 1;
7772 }
7773 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007774 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El6a4d7552011-05-25 23:08:00 +03007775}
7776
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007777/* Emulate the INVEPT instruction */
7778static int handle_invept(struct kvm_vcpu *vcpu)
7779{
Wincy Vanb9c237b2015-02-03 23:56:30 +08007780 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007781 u32 vmx_instruction_info, types;
7782 unsigned long type;
7783 gva_t gva;
7784 struct x86_exception e;
7785 struct {
7786 u64 eptp, gpa;
7787 } operand;
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007788
Wincy Vanb9c237b2015-02-03 23:56:30 +08007789 if (!(vmx->nested.nested_vmx_secondary_ctls_high &
7790 SECONDARY_EXEC_ENABLE_EPT) ||
7791 !(vmx->nested.nested_vmx_ept_caps & VMX_EPT_INVEPT_BIT)) {
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007792 kvm_queue_exception(vcpu, UD_VECTOR);
7793 return 1;
7794 }
7795
7796 if (!nested_vmx_check_permission(vcpu))
7797 return 1;
7798
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007799 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
Nadav Amit27e6fb52014-06-18 17:19:26 +03007800 type = kvm_register_readl(vcpu, (vmx_instruction_info >> 28) & 0xf);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007801
Wincy Vanb9c237b2015-02-03 23:56:30 +08007802 types = (vmx->nested.nested_vmx_ept_caps >> VMX_EPT_EXTENT_SHIFT) & 6;
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007803
Jim Mattson85c856b2016-10-26 08:38:38 -07007804 if (type >= 32 || !(types & (1 << type))) {
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007805 nested_vmx_failValid(vcpu,
7806 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007807 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007808 }
7809
7810 /* According to the Intel VMX instruction reference, the memory
7811 * operand is read even if it isn't needed (e.g., for type==global)
7812 */
7813 if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007814 vmx_instruction_info, false, &gva))
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007815 return 1;
7816 if (kvm_read_guest_virt(&vcpu->arch.emulate_ctxt, gva, &operand,
7817 sizeof(operand), &e)) {
7818 kvm_inject_page_fault(vcpu, &e);
7819 return 1;
7820 }
7821
7822 switch (type) {
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007823 case VMX_EPT_EXTENT_GLOBAL:
Bandan Das45e11812016-08-02 16:32:36 -04007824 /*
7825 * TODO: track mappings and invalidate
7826 * single context requests appropriately
7827 */
7828 case VMX_EPT_EXTENT_CONTEXT:
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007829 kvm_mmu_sync_roots(vcpu);
Liang Chen77c39132014-09-18 12:38:37 -04007830 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007831 nested_vmx_succeed(vcpu);
7832 break;
7833 default:
7834 BUG_ON(1);
7835 break;
7836 }
7837
Kyle Huey6affcbe2016-11-29 12:40:40 -08007838 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007839}
7840
Petr Matouseka642fc32014-09-23 20:22:30 +02007841static int handle_invvpid(struct kvm_vcpu *vcpu)
7842{
Wanpeng Li99b83ac2015-10-13 09:12:21 -07007843 struct vcpu_vmx *vmx = to_vmx(vcpu);
7844 u32 vmx_instruction_info;
7845 unsigned long type, types;
7846 gva_t gva;
7847 struct x86_exception e;
Jim Mattson40352602017-06-28 09:37:37 -07007848 struct {
7849 u64 vpid;
7850 u64 gla;
7851 } operand;
Wanpeng Li99b83ac2015-10-13 09:12:21 -07007852
7853 if (!(vmx->nested.nested_vmx_secondary_ctls_high &
7854 SECONDARY_EXEC_ENABLE_VPID) ||
7855 !(vmx->nested.nested_vmx_vpid_caps & VMX_VPID_INVVPID_BIT)) {
7856 kvm_queue_exception(vcpu, UD_VECTOR);
7857 return 1;
7858 }
7859
7860 if (!nested_vmx_check_permission(vcpu))
7861 return 1;
7862
7863 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
7864 type = kvm_register_readl(vcpu, (vmx_instruction_info >> 28) & 0xf);
7865
Jan Dakinevichbcdde302016-10-28 07:00:30 +03007866 types = (vmx->nested.nested_vmx_vpid_caps &
7867 VMX_VPID_EXTENT_SUPPORTED_MASK) >> 8;
Wanpeng Li99b83ac2015-10-13 09:12:21 -07007868
Jim Mattson85c856b2016-10-26 08:38:38 -07007869 if (type >= 32 || !(types & (1 << type))) {
Wanpeng Li99b83ac2015-10-13 09:12:21 -07007870 nested_vmx_failValid(vcpu,
7871 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007872 return kvm_skip_emulated_instruction(vcpu);
Wanpeng Li99b83ac2015-10-13 09:12:21 -07007873 }
7874
7875 /* according to the intel vmx instruction reference, the memory
7876 * operand is read even if it isn't needed (e.g., for type==global)
7877 */
7878 if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
7879 vmx_instruction_info, false, &gva))
7880 return 1;
Jim Mattson40352602017-06-28 09:37:37 -07007881 if (kvm_read_guest_virt(&vcpu->arch.emulate_ctxt, gva, &operand,
7882 sizeof(operand), &e)) {
Wanpeng Li99b83ac2015-10-13 09:12:21 -07007883 kvm_inject_page_fault(vcpu, &e);
7884 return 1;
7885 }
Jim Mattson40352602017-06-28 09:37:37 -07007886 if (operand.vpid >> 16) {
7887 nested_vmx_failValid(vcpu,
7888 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
7889 return kvm_skip_emulated_instruction(vcpu);
7890 }
Wanpeng Li99b83ac2015-10-13 09:12:21 -07007891
7892 switch (type) {
Jan Dakinevichbcdde302016-10-28 07:00:30 +03007893 case VMX_VPID_EXTENT_INDIVIDUAL_ADDR:
Yu Zhangfd8cb432017-08-24 20:27:56 +08007894 if (is_noncanonical_address(operand.gla, vcpu)) {
Jim Mattson40352602017-06-28 09:37:37 -07007895 nested_vmx_failValid(vcpu,
7896 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
7897 return kvm_skip_emulated_instruction(vcpu);
7898 }
7899 /* fall through */
Paolo Bonzinief697a72016-03-18 16:58:38 +01007900 case VMX_VPID_EXTENT_SINGLE_CONTEXT:
Jan Dakinevichbcdde302016-10-28 07:00:30 +03007901 case VMX_VPID_EXTENT_SINGLE_NON_GLOBAL:
Jim Mattson40352602017-06-28 09:37:37 -07007902 if (!operand.vpid) {
Jan Dakinevichbcdde302016-10-28 07:00:30 +03007903 nested_vmx_failValid(vcpu,
7904 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007905 return kvm_skip_emulated_instruction(vcpu);
Jan Dakinevichbcdde302016-10-28 07:00:30 +03007906 }
7907 break;
Wanpeng Li99b83ac2015-10-13 09:12:21 -07007908 case VMX_VPID_EXTENT_ALL_CONTEXT:
Wanpeng Li99b83ac2015-10-13 09:12:21 -07007909 break;
7910 default:
Jan Dakinevichbcdde302016-10-28 07:00:30 +03007911 WARN_ON_ONCE(1);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007912 return kvm_skip_emulated_instruction(vcpu);
Wanpeng Li99b83ac2015-10-13 09:12:21 -07007913 }
7914
Wanpeng Lic2ba05c2017-12-12 17:33:03 -08007915 __vmx_flush_tlb(vcpu, vmx->nested.vpid02, true);
Jan Dakinevichbcdde302016-10-28 07:00:30 +03007916 nested_vmx_succeed(vcpu);
7917
Kyle Huey6affcbe2016-11-29 12:40:40 -08007918 return kvm_skip_emulated_instruction(vcpu);
Petr Matouseka642fc32014-09-23 20:22:30 +02007919}
7920
Kai Huang843e4332015-01-28 10:54:28 +08007921static int handle_pml_full(struct kvm_vcpu *vcpu)
7922{
7923 unsigned long exit_qualification;
7924
7925 trace_kvm_pml_full(vcpu->vcpu_id);
7926
7927 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7928
7929 /*
7930 * PML buffer FULL happened while executing iret from NMI,
7931 * "blocked by NMI" bit has to be set before next VM entry.
7932 */
7933 if (!(to_vmx(vcpu)->idt_vectoring_info & VECTORING_INFO_VALID_MASK) &&
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01007934 enable_vnmi &&
Kai Huang843e4332015-01-28 10:54:28 +08007935 (exit_qualification & INTR_INFO_UNBLOCK_NMI))
7936 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
7937 GUEST_INTR_STATE_NMI);
7938
7939 /*
7940 * PML buffer already flushed at beginning of VMEXIT. Nothing to do
7941 * here.., and there's no userspace involvement needed for PML.
7942 */
7943 return 1;
7944}
7945
Yunhong Jiang64672c92016-06-13 14:19:59 -07007946static int handle_preemption_timer(struct kvm_vcpu *vcpu)
7947{
7948 kvm_lapic_expired_hv_timer(vcpu);
7949 return 1;
7950}
7951
Bandan Das41ab9372017-08-03 15:54:43 -04007952static bool valid_ept_address(struct kvm_vcpu *vcpu, u64 address)
7953{
7954 struct vcpu_vmx *vmx = to_vmx(vcpu);
Bandan Das41ab9372017-08-03 15:54:43 -04007955 int maxphyaddr = cpuid_maxphyaddr(vcpu);
7956
7957 /* Check for memory type validity */
David Hildenbrandbb97a012017-08-10 23:15:28 +02007958 switch (address & VMX_EPTP_MT_MASK) {
7959 case VMX_EPTP_MT_UC:
Bandan Das41ab9372017-08-03 15:54:43 -04007960 if (!(vmx->nested.nested_vmx_ept_caps & VMX_EPTP_UC_BIT))
7961 return false;
7962 break;
David Hildenbrandbb97a012017-08-10 23:15:28 +02007963 case VMX_EPTP_MT_WB:
Bandan Das41ab9372017-08-03 15:54:43 -04007964 if (!(vmx->nested.nested_vmx_ept_caps & VMX_EPTP_WB_BIT))
7965 return false;
7966 break;
7967 default:
7968 return false;
7969 }
7970
David Hildenbrandbb97a012017-08-10 23:15:28 +02007971 /* only 4 levels page-walk length are valid */
7972 if ((address & VMX_EPTP_PWL_MASK) != VMX_EPTP_PWL_4)
Bandan Das41ab9372017-08-03 15:54:43 -04007973 return false;
7974
7975 /* Reserved bits should not be set */
7976 if (address >> maxphyaddr || ((address >> 7) & 0x1f))
7977 return false;
7978
7979 /* AD, if set, should be supported */
David Hildenbrandbb97a012017-08-10 23:15:28 +02007980 if (address & VMX_EPTP_AD_ENABLE_BIT) {
Bandan Das41ab9372017-08-03 15:54:43 -04007981 if (!(vmx->nested.nested_vmx_ept_caps & VMX_EPT_AD_BIT))
7982 return false;
7983 }
7984
7985 return true;
7986}
7987
7988static int nested_vmx_eptp_switching(struct kvm_vcpu *vcpu,
7989 struct vmcs12 *vmcs12)
7990{
7991 u32 index = vcpu->arch.regs[VCPU_REGS_RCX];
7992 u64 address;
7993 bool accessed_dirty;
7994 struct kvm_mmu *mmu = vcpu->arch.walk_mmu;
7995
7996 if (!nested_cpu_has_eptp_switching(vmcs12) ||
7997 !nested_cpu_has_ept(vmcs12))
7998 return 1;
7999
8000 if (index >= VMFUNC_EPTP_ENTRIES)
8001 return 1;
8002
8003
8004 if (kvm_vcpu_read_guest_page(vcpu, vmcs12->eptp_list_address >> PAGE_SHIFT,
8005 &address, index * 8, 8))
8006 return 1;
8007
David Hildenbrandbb97a012017-08-10 23:15:28 +02008008 accessed_dirty = !!(address & VMX_EPTP_AD_ENABLE_BIT);
Bandan Das41ab9372017-08-03 15:54:43 -04008009
8010 /*
8011 * If the (L2) guest does a vmfunc to the currently
8012 * active ept pointer, we don't have to do anything else
8013 */
8014 if (vmcs12->ept_pointer != address) {
8015 if (!valid_ept_address(vcpu, address))
8016 return 1;
8017
8018 kvm_mmu_unload(vcpu);
8019 mmu->ept_ad = accessed_dirty;
8020 mmu->base_role.ad_disabled = !accessed_dirty;
8021 vmcs12->ept_pointer = address;
8022 /*
8023 * TODO: Check what's the correct approach in case
8024 * mmu reload fails. Currently, we just let the next
8025 * reload potentially fail
8026 */
8027 kvm_mmu_reload(vcpu);
8028 }
8029
8030 return 0;
8031}
8032
Bandan Das2a499e42017-08-03 15:54:41 -04008033static int handle_vmfunc(struct kvm_vcpu *vcpu)
8034{
Bandan Das27c42a12017-08-03 15:54:42 -04008035 struct vcpu_vmx *vmx = to_vmx(vcpu);
8036 struct vmcs12 *vmcs12;
8037 u32 function = vcpu->arch.regs[VCPU_REGS_RAX];
8038
8039 /*
8040 * VMFUNC is only supported for nested guests, but we always enable the
8041 * secondary control for simplicity; for non-nested mode, fake that we
8042 * didn't by injecting #UD.
8043 */
8044 if (!is_guest_mode(vcpu)) {
8045 kvm_queue_exception(vcpu, UD_VECTOR);
8046 return 1;
8047 }
8048
8049 vmcs12 = get_vmcs12(vcpu);
8050 if ((vmcs12->vm_function_control & (1 << function)) == 0)
8051 goto fail;
Bandan Das41ab9372017-08-03 15:54:43 -04008052
8053 switch (function) {
8054 case 0:
8055 if (nested_vmx_eptp_switching(vcpu, vmcs12))
8056 goto fail;
8057 break;
8058 default:
8059 goto fail;
8060 }
8061 return kvm_skip_emulated_instruction(vcpu);
Bandan Das27c42a12017-08-03 15:54:42 -04008062
8063fail:
8064 nested_vmx_vmexit(vcpu, vmx->exit_reason,
8065 vmcs_read32(VM_EXIT_INTR_INFO),
8066 vmcs_readl(EXIT_QUALIFICATION));
Bandan Das2a499e42017-08-03 15:54:41 -04008067 return 1;
8068}
8069
Nadav Har'El0140cae2011-05-25 23:06:28 +03008070/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08008071 * The exit handlers return 1 if the exit was handled fully and guest execution
8072 * may resume. Otherwise they set the kvm_run parameter to indicate what needs
8073 * to be done to userspace and return 0.
8074 */
Mathias Krause772e0312012-08-30 01:30:19 +02008075static int (*const kvm_vmx_exit_handlers[])(struct kvm_vcpu *vcpu) = {
Avi Kivity6aa8b732006-12-10 02:21:36 -08008076 [EXIT_REASON_EXCEPTION_NMI] = handle_exception,
8077 [EXIT_REASON_EXTERNAL_INTERRUPT] = handle_external_interrupt,
Avi Kivity988ad742007-02-12 00:54:36 -08008078 [EXIT_REASON_TRIPLE_FAULT] = handle_triple_fault,
Sheng Yangf08864b2008-05-15 18:23:25 +08008079 [EXIT_REASON_NMI_WINDOW] = handle_nmi_window,
Avi Kivity6aa8b732006-12-10 02:21:36 -08008080 [EXIT_REASON_IO_INSTRUCTION] = handle_io,
Avi Kivity6aa8b732006-12-10 02:21:36 -08008081 [EXIT_REASON_CR_ACCESS] = handle_cr,
8082 [EXIT_REASON_DR_ACCESS] = handle_dr,
8083 [EXIT_REASON_CPUID] = handle_cpuid,
8084 [EXIT_REASON_MSR_READ] = handle_rdmsr,
8085 [EXIT_REASON_MSR_WRITE] = handle_wrmsr,
8086 [EXIT_REASON_PENDING_INTERRUPT] = handle_interrupt_window,
8087 [EXIT_REASON_HLT] = handle_halt,
Gleb Natapovec25d5e2010-11-01 15:35:01 +02008088 [EXIT_REASON_INVD] = handle_invd,
Marcelo Tosattia7052892008-09-23 13:18:35 -03008089 [EXIT_REASON_INVLPG] = handle_invlpg,
Avi Kivityfee84b02011-11-10 14:57:25 +02008090 [EXIT_REASON_RDPMC] = handle_rdpmc,
Ingo Molnarc21415e2007-02-19 14:37:47 +02008091 [EXIT_REASON_VMCALL] = handle_vmcall,
Nadav Har'El27d6c862011-05-25 23:06:59 +03008092 [EXIT_REASON_VMCLEAR] = handle_vmclear,
Nadav Har'Elcd232ad2011-05-25 23:10:33 +03008093 [EXIT_REASON_VMLAUNCH] = handle_vmlaunch,
Nadav Har'El63846662011-05-25 23:07:29 +03008094 [EXIT_REASON_VMPTRLD] = handle_vmptrld,
Nadav Har'El6a4d7552011-05-25 23:08:00 +03008095 [EXIT_REASON_VMPTRST] = handle_vmptrst,
Nadav Har'El49f705c2011-05-25 23:08:30 +03008096 [EXIT_REASON_VMREAD] = handle_vmread,
Nadav Har'Elcd232ad2011-05-25 23:10:33 +03008097 [EXIT_REASON_VMRESUME] = handle_vmresume,
Nadav Har'El49f705c2011-05-25 23:08:30 +03008098 [EXIT_REASON_VMWRITE] = handle_vmwrite,
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008099 [EXIT_REASON_VMOFF] = handle_vmoff,
8100 [EXIT_REASON_VMON] = handle_vmon,
Sheng Yangf78e0e22007-10-29 09:40:42 +08008101 [EXIT_REASON_TPR_BELOW_THRESHOLD] = handle_tpr_below_threshold,
8102 [EXIT_REASON_APIC_ACCESS] = handle_apic_access,
Yang Zhang83d4c282013-01-25 10:18:49 +08008103 [EXIT_REASON_APIC_WRITE] = handle_apic_write,
Yang Zhangc7c9c562013-01-25 10:18:51 +08008104 [EXIT_REASON_EOI_INDUCED] = handle_apic_eoi_induced,
Eddie Donge5edaa02007-11-11 12:28:35 +02008105 [EXIT_REASON_WBINVD] = handle_wbinvd,
Dexuan Cui2acf9232010-06-10 11:27:12 +08008106 [EXIT_REASON_XSETBV] = handle_xsetbv,
Izik Eidus37817f22008-03-24 23:14:53 +02008107 [EXIT_REASON_TASK_SWITCH] = handle_task_switch,
Andi Kleena0861c02009-06-08 17:37:09 +08008108 [EXIT_REASON_MCE_DURING_VMENTRY] = handle_machine_check,
Paolo Bonzini0367f202016-07-12 10:44:55 +02008109 [EXIT_REASON_GDTR_IDTR] = handle_desc,
8110 [EXIT_REASON_LDTR_TR] = handle_desc,
Marcelo Tosatti68f89402009-06-11 12:07:43 -03008111 [EXIT_REASON_EPT_VIOLATION] = handle_ept_violation,
8112 [EXIT_REASON_EPT_MISCONFIG] = handle_ept_misconfig,
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08008113 [EXIT_REASON_PAUSE_INSTRUCTION] = handle_pause,
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04008114 [EXIT_REASON_MWAIT_INSTRUCTION] = handle_mwait,
Mihai Donțu5f3d45e2015-07-05 20:08:57 +03008115 [EXIT_REASON_MONITOR_TRAP_FLAG] = handle_monitor_trap,
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04008116 [EXIT_REASON_MONITOR_INSTRUCTION] = handle_monitor,
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008117 [EXIT_REASON_INVEPT] = handle_invept,
Petr Matouseka642fc32014-09-23 20:22:30 +02008118 [EXIT_REASON_INVVPID] = handle_invvpid,
Jim Mattson45ec3682017-08-23 16:32:04 -07008119 [EXIT_REASON_RDRAND] = handle_invalid_op,
Jim Mattson75f4fc82017-08-23 16:32:03 -07008120 [EXIT_REASON_RDSEED] = handle_invalid_op,
Wanpeng Lif53cd632014-12-02 19:14:58 +08008121 [EXIT_REASON_XSAVES] = handle_xsaves,
8122 [EXIT_REASON_XRSTORS] = handle_xrstors,
Kai Huang843e4332015-01-28 10:54:28 +08008123 [EXIT_REASON_PML_FULL] = handle_pml_full,
Bandan Das2a499e42017-08-03 15:54:41 -04008124 [EXIT_REASON_VMFUNC] = handle_vmfunc,
Yunhong Jiang64672c92016-06-13 14:19:59 -07008125 [EXIT_REASON_PREEMPTION_TIMER] = handle_preemption_timer,
Avi Kivity6aa8b732006-12-10 02:21:36 -08008126};
8127
8128static const int kvm_vmx_max_exit_handlers =
Robert P. J. Day50a34852007-06-03 13:35:29 -04008129 ARRAY_SIZE(kvm_vmx_exit_handlers);
Avi Kivity6aa8b732006-12-10 02:21:36 -08008130
Jan Kiszka908a7bd2013-02-18 11:21:16 +01008131static bool nested_vmx_exit_handled_io(struct kvm_vcpu *vcpu,
8132 struct vmcs12 *vmcs12)
8133{
8134 unsigned long exit_qualification;
8135 gpa_t bitmap, last_bitmap;
8136 unsigned int port;
8137 int size;
8138 u8 b;
8139
Jan Kiszka908a7bd2013-02-18 11:21:16 +01008140 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_IO_BITMAPS))
Zhihui Zhang2f0a6392013-12-30 15:56:29 -05008141 return nested_cpu_has(vmcs12, CPU_BASED_UNCOND_IO_EXITING);
Jan Kiszka908a7bd2013-02-18 11:21:16 +01008142
8143 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
8144
8145 port = exit_qualification >> 16;
8146 size = (exit_qualification & 7) + 1;
8147
8148 last_bitmap = (gpa_t)-1;
8149 b = -1;
8150
8151 while (size > 0) {
8152 if (port < 0x8000)
8153 bitmap = vmcs12->io_bitmap_a;
8154 else if (port < 0x10000)
8155 bitmap = vmcs12->io_bitmap_b;
8156 else
Joe Perches1d804d02015-03-30 16:46:09 -07008157 return true;
Jan Kiszka908a7bd2013-02-18 11:21:16 +01008158 bitmap += (port & 0x7fff) / 8;
8159
8160 if (last_bitmap != bitmap)
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02008161 if (kvm_vcpu_read_guest(vcpu, bitmap, &b, 1))
Joe Perches1d804d02015-03-30 16:46:09 -07008162 return true;
Jan Kiszka908a7bd2013-02-18 11:21:16 +01008163 if (b & (1 << (port & 7)))
Joe Perches1d804d02015-03-30 16:46:09 -07008164 return true;
Jan Kiszka908a7bd2013-02-18 11:21:16 +01008165
8166 port++;
8167 size--;
8168 last_bitmap = bitmap;
8169 }
8170
Joe Perches1d804d02015-03-30 16:46:09 -07008171 return false;
Jan Kiszka908a7bd2013-02-18 11:21:16 +01008172}
8173
Nadav Har'El644d7112011-05-25 23:12:35 +03008174/*
8175 * Return 1 if we should exit from L2 to L1 to handle an MSR access access,
8176 * rather than handle it ourselves in L0. I.e., check whether L1 expressed
8177 * disinterest in the current event (read or write a specific MSR) by using an
8178 * MSR bitmap. This may be the case even when L0 doesn't use MSR bitmaps.
8179 */
8180static bool nested_vmx_exit_handled_msr(struct kvm_vcpu *vcpu,
8181 struct vmcs12 *vmcs12, u32 exit_reason)
8182{
8183 u32 msr_index = vcpu->arch.regs[VCPU_REGS_RCX];
8184 gpa_t bitmap;
8185
Jan Kiszkacbd29cb2013-02-11 12:19:28 +01008186 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS))
Joe Perches1d804d02015-03-30 16:46:09 -07008187 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008188
8189 /*
8190 * The MSR_BITMAP page is divided into four 1024-byte bitmaps,
8191 * for the four combinations of read/write and low/high MSR numbers.
8192 * First we need to figure out which of the four to use:
8193 */
8194 bitmap = vmcs12->msr_bitmap;
8195 if (exit_reason == EXIT_REASON_MSR_WRITE)
8196 bitmap += 2048;
8197 if (msr_index >= 0xc0000000) {
8198 msr_index -= 0xc0000000;
8199 bitmap += 1024;
8200 }
8201
8202 /* Then read the msr_index'th bit from this bitmap: */
8203 if (msr_index < 1024*8) {
8204 unsigned char b;
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02008205 if (kvm_vcpu_read_guest(vcpu, bitmap + msr_index/8, &b, 1))
Joe Perches1d804d02015-03-30 16:46:09 -07008206 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008207 return 1 & (b >> (msr_index & 7));
8208 } else
Joe Perches1d804d02015-03-30 16:46:09 -07008209 return true; /* let L1 handle the wrong parameter */
Nadav Har'El644d7112011-05-25 23:12:35 +03008210}
8211
8212/*
8213 * Return 1 if we should exit from L2 to L1 to handle a CR access exit,
8214 * rather than handle it ourselves in L0. I.e., check if L1 wanted to
8215 * intercept (via guest_host_mask etc.) the current event.
8216 */
8217static bool nested_vmx_exit_handled_cr(struct kvm_vcpu *vcpu,
8218 struct vmcs12 *vmcs12)
8219{
8220 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
8221 int cr = exit_qualification & 15;
Jan H. Schönherre1d39b12017-05-20 13:22:56 +02008222 int reg;
8223 unsigned long val;
Nadav Har'El644d7112011-05-25 23:12:35 +03008224
8225 switch ((exit_qualification >> 4) & 3) {
8226 case 0: /* mov to cr */
Jan H. Schönherre1d39b12017-05-20 13:22:56 +02008227 reg = (exit_qualification >> 8) & 15;
8228 val = kvm_register_readl(vcpu, reg);
Nadav Har'El644d7112011-05-25 23:12:35 +03008229 switch (cr) {
8230 case 0:
8231 if (vmcs12->cr0_guest_host_mask &
8232 (val ^ vmcs12->cr0_read_shadow))
Joe Perches1d804d02015-03-30 16:46:09 -07008233 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008234 break;
8235 case 3:
8236 if ((vmcs12->cr3_target_count >= 1 &&
8237 vmcs12->cr3_target_value0 == val) ||
8238 (vmcs12->cr3_target_count >= 2 &&
8239 vmcs12->cr3_target_value1 == val) ||
8240 (vmcs12->cr3_target_count >= 3 &&
8241 vmcs12->cr3_target_value2 == val) ||
8242 (vmcs12->cr3_target_count >= 4 &&
8243 vmcs12->cr3_target_value3 == val))
Joe Perches1d804d02015-03-30 16:46:09 -07008244 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03008245 if (nested_cpu_has(vmcs12, CPU_BASED_CR3_LOAD_EXITING))
Joe Perches1d804d02015-03-30 16:46:09 -07008246 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008247 break;
8248 case 4:
8249 if (vmcs12->cr4_guest_host_mask &
8250 (vmcs12->cr4_read_shadow ^ val))
Joe Perches1d804d02015-03-30 16:46:09 -07008251 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008252 break;
8253 case 8:
8254 if (nested_cpu_has(vmcs12, CPU_BASED_CR8_LOAD_EXITING))
Joe Perches1d804d02015-03-30 16:46:09 -07008255 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008256 break;
8257 }
8258 break;
8259 case 2: /* clts */
8260 if ((vmcs12->cr0_guest_host_mask & X86_CR0_TS) &&
8261 (vmcs12->cr0_read_shadow & X86_CR0_TS))
Joe Perches1d804d02015-03-30 16:46:09 -07008262 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008263 break;
8264 case 1: /* mov from cr */
8265 switch (cr) {
8266 case 3:
8267 if (vmcs12->cpu_based_vm_exec_control &
8268 CPU_BASED_CR3_STORE_EXITING)
Joe Perches1d804d02015-03-30 16:46:09 -07008269 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008270 break;
8271 case 8:
8272 if (vmcs12->cpu_based_vm_exec_control &
8273 CPU_BASED_CR8_STORE_EXITING)
Joe Perches1d804d02015-03-30 16:46:09 -07008274 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008275 break;
8276 }
8277 break;
8278 case 3: /* lmsw */
8279 /*
8280 * lmsw can change bits 1..3 of cr0, and only set bit 0 of
8281 * cr0. Other attempted changes are ignored, with no exit.
8282 */
Jan H. Schönherre1d39b12017-05-20 13:22:56 +02008283 val = (exit_qualification >> LMSW_SOURCE_DATA_SHIFT) & 0x0f;
Nadav Har'El644d7112011-05-25 23:12:35 +03008284 if (vmcs12->cr0_guest_host_mask & 0xe &
8285 (val ^ vmcs12->cr0_read_shadow))
Joe Perches1d804d02015-03-30 16:46:09 -07008286 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008287 if ((vmcs12->cr0_guest_host_mask & 0x1) &&
8288 !(vmcs12->cr0_read_shadow & 0x1) &&
8289 (val & 0x1))
Joe Perches1d804d02015-03-30 16:46:09 -07008290 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008291 break;
8292 }
Joe Perches1d804d02015-03-30 16:46:09 -07008293 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03008294}
8295
8296/*
8297 * Return 1 if we should exit from L2 to L1 to handle an exit, or 0 if we
8298 * should handle it ourselves in L0 (and then continue L2). Only call this
8299 * when in is_guest_mode (L2).
8300 */
Paolo Bonzini7313c692017-07-27 10:31:25 +02008301static bool nested_vmx_exit_reflected(struct kvm_vcpu *vcpu, u32 exit_reason)
Nadav Har'El644d7112011-05-25 23:12:35 +03008302{
Nadav Har'El644d7112011-05-25 23:12:35 +03008303 u32 intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
8304 struct vcpu_vmx *vmx = to_vmx(vcpu);
8305 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
8306
Jim Mattson4f350c62017-09-14 16:31:44 -07008307 if (vmx->nested.nested_run_pending)
8308 return false;
8309
8310 if (unlikely(vmx->fail)) {
8311 pr_info_ratelimited("%s failed vm entry %x\n", __func__,
8312 vmcs_read32(VM_INSTRUCTION_ERROR));
8313 return true;
8314 }
Jan Kiszka542060e2014-01-04 18:47:21 +01008315
David Matlackc9f04402017-08-01 14:00:40 -07008316 /*
8317 * The host physical addresses of some pages of guest memory
Jim Mattson00647b42017-11-27 17:22:25 -06008318 * are loaded into the vmcs02 (e.g. vmcs12's Virtual APIC
8319 * Page). The CPU may write to these pages via their host
8320 * physical address while L2 is running, bypassing any
8321 * address-translation-based dirty tracking (e.g. EPT write
8322 * protection).
David Matlackc9f04402017-08-01 14:00:40 -07008323 *
8324 * Mark them dirty on every exit from L2 to prevent them from
8325 * getting out of sync with dirty tracking.
8326 */
8327 nested_mark_vmcs12_pages_dirty(vcpu);
8328
Jim Mattson4f350c62017-09-14 16:31:44 -07008329 trace_kvm_nested_vmexit(kvm_rip_read(vcpu), exit_reason,
8330 vmcs_readl(EXIT_QUALIFICATION),
8331 vmx->idt_vectoring_info,
8332 intr_info,
8333 vmcs_read32(VM_EXIT_INTR_ERROR_CODE),
8334 KVM_ISA_VMX);
Nadav Har'El644d7112011-05-25 23:12:35 +03008335
8336 switch (exit_reason) {
8337 case EXIT_REASON_EXCEPTION_NMI:
Jim Mattsonef85b672016-12-12 11:01:37 -08008338 if (is_nmi(intr_info))
Joe Perches1d804d02015-03-30 16:46:09 -07008339 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03008340 else if (is_page_fault(intr_info))
Wanpeng Li52a5c152017-07-13 18:30:42 -07008341 return !vmx->vcpu.arch.apf.host_apf_reason && enable_ept;
Anthoine Bourgeoise504c902013-11-13 11:45:37 +01008342 else if (is_no_device(intr_info) &&
Paolo Bonziniccf98442014-02-27 22:54:11 +01008343 !(vmcs12->guest_cr0 & X86_CR0_TS))
Joe Perches1d804d02015-03-30 16:46:09 -07008344 return false;
Jan Kiszka6f054852016-02-09 20:15:18 +01008345 else if (is_debug(intr_info) &&
8346 vcpu->guest_debug &
8347 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))
8348 return false;
8349 else if (is_breakpoint(intr_info) &&
8350 vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
8351 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03008352 return vmcs12->exception_bitmap &
8353 (1u << (intr_info & INTR_INFO_VECTOR_MASK));
8354 case EXIT_REASON_EXTERNAL_INTERRUPT:
Joe Perches1d804d02015-03-30 16:46:09 -07008355 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03008356 case EXIT_REASON_TRIPLE_FAULT:
Joe Perches1d804d02015-03-30 16:46:09 -07008357 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008358 case EXIT_REASON_PENDING_INTERRUPT:
Jan Kiszka3b656cf2013-04-14 12:12:45 +02008359 return nested_cpu_has(vmcs12, CPU_BASED_VIRTUAL_INTR_PENDING);
Nadav Har'El644d7112011-05-25 23:12:35 +03008360 case EXIT_REASON_NMI_WINDOW:
Jan Kiszka3b656cf2013-04-14 12:12:45 +02008361 return nested_cpu_has(vmcs12, CPU_BASED_VIRTUAL_NMI_PENDING);
Nadav Har'El644d7112011-05-25 23:12:35 +03008362 case EXIT_REASON_TASK_SWITCH:
Joe Perches1d804d02015-03-30 16:46:09 -07008363 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008364 case EXIT_REASON_CPUID:
Joe Perches1d804d02015-03-30 16:46:09 -07008365 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008366 case EXIT_REASON_HLT:
8367 return nested_cpu_has(vmcs12, CPU_BASED_HLT_EXITING);
8368 case EXIT_REASON_INVD:
Joe Perches1d804d02015-03-30 16:46:09 -07008369 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008370 case EXIT_REASON_INVLPG:
8371 return nested_cpu_has(vmcs12, CPU_BASED_INVLPG_EXITING);
8372 case EXIT_REASON_RDPMC:
8373 return nested_cpu_has(vmcs12, CPU_BASED_RDPMC_EXITING);
Paolo Bonzinia5f46452017-03-30 11:55:32 +02008374 case EXIT_REASON_RDRAND:
David Hildenbrand736fdf72017-08-24 20:51:37 +02008375 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_RDRAND_EXITING);
Paolo Bonzinia5f46452017-03-30 11:55:32 +02008376 case EXIT_REASON_RDSEED:
David Hildenbrand736fdf72017-08-24 20:51:37 +02008377 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_RDSEED_EXITING);
Jan Kiszkab3a2a902015-03-23 19:27:19 +01008378 case EXIT_REASON_RDTSC: case EXIT_REASON_RDTSCP:
Nadav Har'El644d7112011-05-25 23:12:35 +03008379 return nested_cpu_has(vmcs12, CPU_BASED_RDTSC_EXITING);
8380 case EXIT_REASON_VMCALL: case EXIT_REASON_VMCLEAR:
8381 case EXIT_REASON_VMLAUNCH: case EXIT_REASON_VMPTRLD:
8382 case EXIT_REASON_VMPTRST: case EXIT_REASON_VMREAD:
8383 case EXIT_REASON_VMRESUME: case EXIT_REASON_VMWRITE:
8384 case EXIT_REASON_VMOFF: case EXIT_REASON_VMON:
Petr Matouseka642fc32014-09-23 20:22:30 +02008385 case EXIT_REASON_INVEPT: case EXIT_REASON_INVVPID:
Nadav Har'El644d7112011-05-25 23:12:35 +03008386 /*
8387 * VMX instructions trap unconditionally. This allows L1 to
8388 * emulate them for its L2 guest, i.e., allows 3-level nesting!
8389 */
Joe Perches1d804d02015-03-30 16:46:09 -07008390 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008391 case EXIT_REASON_CR_ACCESS:
8392 return nested_vmx_exit_handled_cr(vcpu, vmcs12);
8393 case EXIT_REASON_DR_ACCESS:
8394 return nested_cpu_has(vmcs12, CPU_BASED_MOV_DR_EXITING);
8395 case EXIT_REASON_IO_INSTRUCTION:
Jan Kiszka908a7bd2013-02-18 11:21:16 +01008396 return nested_vmx_exit_handled_io(vcpu, vmcs12);
Paolo Bonzini1b073042016-10-25 16:06:30 +02008397 case EXIT_REASON_GDTR_IDTR: case EXIT_REASON_LDTR_TR:
8398 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_DESC);
Nadav Har'El644d7112011-05-25 23:12:35 +03008399 case EXIT_REASON_MSR_READ:
8400 case EXIT_REASON_MSR_WRITE:
8401 return nested_vmx_exit_handled_msr(vcpu, vmcs12, exit_reason);
8402 case EXIT_REASON_INVALID_STATE:
Joe Perches1d804d02015-03-30 16:46:09 -07008403 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008404 case EXIT_REASON_MWAIT_INSTRUCTION:
8405 return nested_cpu_has(vmcs12, CPU_BASED_MWAIT_EXITING);
Mihai Donțu5f3d45e2015-07-05 20:08:57 +03008406 case EXIT_REASON_MONITOR_TRAP_FLAG:
8407 return nested_cpu_has(vmcs12, CPU_BASED_MONITOR_TRAP_FLAG);
Nadav Har'El644d7112011-05-25 23:12:35 +03008408 case EXIT_REASON_MONITOR_INSTRUCTION:
8409 return nested_cpu_has(vmcs12, CPU_BASED_MONITOR_EXITING);
8410 case EXIT_REASON_PAUSE_INSTRUCTION:
8411 return nested_cpu_has(vmcs12, CPU_BASED_PAUSE_EXITING) ||
8412 nested_cpu_has2(vmcs12,
8413 SECONDARY_EXEC_PAUSE_LOOP_EXITING);
8414 case EXIT_REASON_MCE_DURING_VMENTRY:
Joe Perches1d804d02015-03-30 16:46:09 -07008415 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03008416 case EXIT_REASON_TPR_BELOW_THRESHOLD:
Wanpeng Lia7c0b072014-08-21 19:46:50 +08008417 return nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW);
Nadav Har'El644d7112011-05-25 23:12:35 +03008418 case EXIT_REASON_APIC_ACCESS:
8419 return nested_cpu_has2(vmcs12,
8420 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES);
Wincy Van82f0dd42015-02-03 23:57:18 +08008421 case EXIT_REASON_APIC_WRITE:
Wincy Van608406e2015-02-03 23:57:51 +08008422 case EXIT_REASON_EOI_INDUCED:
8423 /* apic_write and eoi_induced should exit unconditionally. */
Joe Perches1d804d02015-03-30 16:46:09 -07008424 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008425 case EXIT_REASON_EPT_VIOLATION:
Nadav Har'El2b1be672013-08-05 11:07:19 +03008426 /*
8427 * L0 always deals with the EPT violation. If nested EPT is
8428 * used, and the nested mmu code discovers that the address is
8429 * missing in the guest EPT table (EPT12), the EPT violation
8430 * will be injected with nested_ept_inject_page_fault()
8431 */
Joe Perches1d804d02015-03-30 16:46:09 -07008432 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03008433 case EXIT_REASON_EPT_MISCONFIG:
Nadav Har'El2b1be672013-08-05 11:07:19 +03008434 /*
8435 * L2 never uses directly L1's EPT, but rather L0's own EPT
8436 * table (shadow on EPT) or a merged EPT table that L0 built
8437 * (EPT on EPT). So any problems with the structure of the
8438 * table is L0's fault.
8439 */
Joe Perches1d804d02015-03-30 16:46:09 -07008440 return false;
Paolo Bonzini90a2db62017-07-27 13:22:13 +02008441 case EXIT_REASON_INVPCID:
8442 return
8443 nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_INVPCID) &&
8444 nested_cpu_has(vmcs12, CPU_BASED_INVLPG_EXITING);
Nadav Har'El644d7112011-05-25 23:12:35 +03008445 case EXIT_REASON_WBINVD:
8446 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_WBINVD_EXITING);
8447 case EXIT_REASON_XSETBV:
Joe Perches1d804d02015-03-30 16:46:09 -07008448 return true;
Wanpeng Li81dc01f2014-12-04 19:11:07 +08008449 case EXIT_REASON_XSAVES: case EXIT_REASON_XRSTORS:
8450 /*
8451 * This should never happen, since it is not possible to
8452 * set XSS to a non-zero value---neither in L1 nor in L2.
8453 * If if it were, XSS would have to be checked against
8454 * the XSS exit bitmap in vmcs12.
8455 */
8456 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_XSAVES);
Wanpeng Li55123e32016-07-06 18:29:58 +08008457 case EXIT_REASON_PREEMPTION_TIMER:
8458 return false;
Ladi Prosekab007cc2017-03-31 10:19:26 +02008459 case EXIT_REASON_PML_FULL:
Bandan Das03efce62017-05-05 15:25:15 -04008460 /* We emulate PML support to L1. */
Ladi Prosekab007cc2017-03-31 10:19:26 +02008461 return false;
Bandan Das2a499e42017-08-03 15:54:41 -04008462 case EXIT_REASON_VMFUNC:
8463 /* VM functions are emulated through L2->L0 vmexits. */
8464 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03008465 default:
Joe Perches1d804d02015-03-30 16:46:09 -07008466 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008467 }
8468}
8469
Paolo Bonzini7313c692017-07-27 10:31:25 +02008470static int nested_vmx_reflect_vmexit(struct kvm_vcpu *vcpu, u32 exit_reason)
8471{
8472 u32 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
8473
8474 /*
8475 * At this point, the exit interruption info in exit_intr_info
8476 * is only valid for EXCEPTION_NMI exits. For EXTERNAL_INTERRUPT
8477 * we need to query the in-kernel LAPIC.
8478 */
8479 WARN_ON(exit_reason == EXIT_REASON_EXTERNAL_INTERRUPT);
8480 if ((exit_intr_info &
8481 (INTR_INFO_VALID_MASK | INTR_INFO_DELIVER_CODE_MASK)) ==
8482 (INTR_INFO_VALID_MASK | INTR_INFO_DELIVER_CODE_MASK)) {
8483 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
8484 vmcs12->vm_exit_intr_error_code =
8485 vmcs_read32(VM_EXIT_INTR_ERROR_CODE);
8486 }
8487
8488 nested_vmx_vmexit(vcpu, exit_reason, exit_intr_info,
8489 vmcs_readl(EXIT_QUALIFICATION));
8490 return 1;
8491}
8492
Avi Kivity586f9602010-11-18 13:09:54 +02008493static void vmx_get_exit_info(struct kvm_vcpu *vcpu, u64 *info1, u64 *info2)
8494{
8495 *info1 = vmcs_readl(EXIT_QUALIFICATION);
8496 *info2 = vmcs_read32(VM_EXIT_INTR_INFO);
8497}
8498
Kai Huanga3eaa862015-11-04 13:46:05 +08008499static void vmx_destroy_pml_buffer(struct vcpu_vmx *vmx)
Kai Huang843e4332015-01-28 10:54:28 +08008500{
Kai Huanga3eaa862015-11-04 13:46:05 +08008501 if (vmx->pml_pg) {
8502 __free_page(vmx->pml_pg);
8503 vmx->pml_pg = NULL;
8504 }
Kai Huang843e4332015-01-28 10:54:28 +08008505}
8506
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02008507static void vmx_flush_pml_buffer(struct kvm_vcpu *vcpu)
Kai Huang843e4332015-01-28 10:54:28 +08008508{
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02008509 struct vcpu_vmx *vmx = to_vmx(vcpu);
Kai Huang843e4332015-01-28 10:54:28 +08008510 u64 *pml_buf;
8511 u16 pml_idx;
8512
8513 pml_idx = vmcs_read16(GUEST_PML_INDEX);
8514
8515 /* Do nothing if PML buffer is empty */
8516 if (pml_idx == (PML_ENTITY_NUM - 1))
8517 return;
8518
8519 /* PML index always points to next available PML buffer entity */
8520 if (pml_idx >= PML_ENTITY_NUM)
8521 pml_idx = 0;
8522 else
8523 pml_idx++;
8524
8525 pml_buf = page_address(vmx->pml_pg);
8526 for (; pml_idx < PML_ENTITY_NUM; pml_idx++) {
8527 u64 gpa;
8528
8529 gpa = pml_buf[pml_idx];
8530 WARN_ON(gpa & (PAGE_SIZE - 1));
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02008531 kvm_vcpu_mark_page_dirty(vcpu, gpa >> PAGE_SHIFT);
Kai Huang843e4332015-01-28 10:54:28 +08008532 }
8533
8534 /* reset PML index */
8535 vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1);
8536}
8537
8538/*
8539 * Flush all vcpus' PML buffer and update logged GPAs to dirty_bitmap.
8540 * Called before reporting dirty_bitmap to userspace.
8541 */
8542static void kvm_flush_pml_buffers(struct kvm *kvm)
8543{
8544 int i;
8545 struct kvm_vcpu *vcpu;
8546 /*
8547 * We only need to kick vcpu out of guest mode here, as PML buffer
8548 * is flushed at beginning of all VMEXITs, and it's obvious that only
8549 * vcpus running in guest are possible to have unflushed GPAs in PML
8550 * buffer.
8551 */
8552 kvm_for_each_vcpu(i, vcpu, kvm)
8553 kvm_vcpu_kick(vcpu);
8554}
8555
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02008556static void vmx_dump_sel(char *name, uint32_t sel)
8557{
8558 pr_err("%s sel=0x%04x, attr=0x%05x, limit=0x%08x, base=0x%016lx\n",
Chao Peng96794e42017-02-21 03:50:01 -05008559 name, vmcs_read16(sel),
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02008560 vmcs_read32(sel + GUEST_ES_AR_BYTES - GUEST_ES_SELECTOR),
8561 vmcs_read32(sel + GUEST_ES_LIMIT - GUEST_ES_SELECTOR),
8562 vmcs_readl(sel + GUEST_ES_BASE - GUEST_ES_SELECTOR));
8563}
8564
8565static void vmx_dump_dtsel(char *name, uint32_t limit)
8566{
8567 pr_err("%s limit=0x%08x, base=0x%016lx\n",
8568 name, vmcs_read32(limit),
8569 vmcs_readl(limit + GUEST_GDTR_BASE - GUEST_GDTR_LIMIT));
8570}
8571
8572static void dump_vmcs(void)
8573{
8574 u32 vmentry_ctl = vmcs_read32(VM_ENTRY_CONTROLS);
8575 u32 vmexit_ctl = vmcs_read32(VM_EXIT_CONTROLS);
8576 u32 cpu_based_exec_ctrl = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
8577 u32 pin_based_exec_ctrl = vmcs_read32(PIN_BASED_VM_EXEC_CONTROL);
8578 u32 secondary_exec_control = 0;
8579 unsigned long cr4 = vmcs_readl(GUEST_CR4);
Paolo Bonzinif3531052015-12-03 15:49:56 +01008580 u64 efer = vmcs_read64(GUEST_IA32_EFER);
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02008581 int i, n;
8582
8583 if (cpu_has_secondary_exec_ctrls())
8584 secondary_exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
8585
8586 pr_err("*** Guest State ***\n");
8587 pr_err("CR0: actual=0x%016lx, shadow=0x%016lx, gh_mask=%016lx\n",
8588 vmcs_readl(GUEST_CR0), vmcs_readl(CR0_READ_SHADOW),
8589 vmcs_readl(CR0_GUEST_HOST_MASK));
8590 pr_err("CR4: actual=0x%016lx, shadow=0x%016lx, gh_mask=%016lx\n",
8591 cr4, vmcs_readl(CR4_READ_SHADOW), vmcs_readl(CR4_GUEST_HOST_MASK));
8592 pr_err("CR3 = 0x%016lx\n", vmcs_readl(GUEST_CR3));
8593 if ((secondary_exec_control & SECONDARY_EXEC_ENABLE_EPT) &&
8594 (cr4 & X86_CR4_PAE) && !(efer & EFER_LMA))
8595 {
Paolo Bonzini845c5b402015-12-03 15:51:00 +01008596 pr_err("PDPTR0 = 0x%016llx PDPTR1 = 0x%016llx\n",
8597 vmcs_read64(GUEST_PDPTR0), vmcs_read64(GUEST_PDPTR1));
8598 pr_err("PDPTR2 = 0x%016llx PDPTR3 = 0x%016llx\n",
8599 vmcs_read64(GUEST_PDPTR2), vmcs_read64(GUEST_PDPTR3));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02008600 }
8601 pr_err("RSP = 0x%016lx RIP = 0x%016lx\n",
8602 vmcs_readl(GUEST_RSP), vmcs_readl(GUEST_RIP));
8603 pr_err("RFLAGS=0x%08lx DR7 = 0x%016lx\n",
8604 vmcs_readl(GUEST_RFLAGS), vmcs_readl(GUEST_DR7));
8605 pr_err("Sysenter RSP=%016lx CS:RIP=%04x:%016lx\n",
8606 vmcs_readl(GUEST_SYSENTER_ESP),
8607 vmcs_read32(GUEST_SYSENTER_CS), vmcs_readl(GUEST_SYSENTER_EIP));
8608 vmx_dump_sel("CS: ", GUEST_CS_SELECTOR);
8609 vmx_dump_sel("DS: ", GUEST_DS_SELECTOR);
8610 vmx_dump_sel("SS: ", GUEST_SS_SELECTOR);
8611 vmx_dump_sel("ES: ", GUEST_ES_SELECTOR);
8612 vmx_dump_sel("FS: ", GUEST_FS_SELECTOR);
8613 vmx_dump_sel("GS: ", GUEST_GS_SELECTOR);
8614 vmx_dump_dtsel("GDTR:", GUEST_GDTR_LIMIT);
8615 vmx_dump_sel("LDTR:", GUEST_LDTR_SELECTOR);
8616 vmx_dump_dtsel("IDTR:", GUEST_IDTR_LIMIT);
8617 vmx_dump_sel("TR: ", GUEST_TR_SELECTOR);
8618 if ((vmexit_ctl & (VM_EXIT_SAVE_IA32_PAT | VM_EXIT_SAVE_IA32_EFER)) ||
8619 (vmentry_ctl & (VM_ENTRY_LOAD_IA32_PAT | VM_ENTRY_LOAD_IA32_EFER)))
Paolo Bonzini845c5b402015-12-03 15:51:00 +01008620 pr_err("EFER = 0x%016llx PAT = 0x%016llx\n",
8621 efer, vmcs_read64(GUEST_IA32_PAT));
8622 pr_err("DebugCtl = 0x%016llx DebugExceptions = 0x%016lx\n",
8623 vmcs_read64(GUEST_IA32_DEBUGCTL),
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02008624 vmcs_readl(GUEST_PENDING_DBG_EXCEPTIONS));
8625 if (vmentry_ctl & VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL)
Paolo Bonzini845c5b402015-12-03 15:51:00 +01008626 pr_err("PerfGlobCtl = 0x%016llx\n",
8627 vmcs_read64(GUEST_IA32_PERF_GLOBAL_CTRL));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02008628 if (vmentry_ctl & VM_ENTRY_LOAD_BNDCFGS)
Paolo Bonzini845c5b402015-12-03 15:51:00 +01008629 pr_err("BndCfgS = 0x%016llx\n", vmcs_read64(GUEST_BNDCFGS));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02008630 pr_err("Interruptibility = %08x ActivityState = %08x\n",
8631 vmcs_read32(GUEST_INTERRUPTIBILITY_INFO),
8632 vmcs_read32(GUEST_ACTIVITY_STATE));
8633 if (secondary_exec_control & SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY)
8634 pr_err("InterruptStatus = %04x\n",
8635 vmcs_read16(GUEST_INTR_STATUS));
8636
8637 pr_err("*** Host State ***\n");
8638 pr_err("RIP = 0x%016lx RSP = 0x%016lx\n",
8639 vmcs_readl(HOST_RIP), vmcs_readl(HOST_RSP));
8640 pr_err("CS=%04x SS=%04x DS=%04x ES=%04x FS=%04x GS=%04x TR=%04x\n",
8641 vmcs_read16(HOST_CS_SELECTOR), vmcs_read16(HOST_SS_SELECTOR),
8642 vmcs_read16(HOST_DS_SELECTOR), vmcs_read16(HOST_ES_SELECTOR),
8643 vmcs_read16(HOST_FS_SELECTOR), vmcs_read16(HOST_GS_SELECTOR),
8644 vmcs_read16(HOST_TR_SELECTOR));
8645 pr_err("FSBase=%016lx GSBase=%016lx TRBase=%016lx\n",
8646 vmcs_readl(HOST_FS_BASE), vmcs_readl(HOST_GS_BASE),
8647 vmcs_readl(HOST_TR_BASE));
8648 pr_err("GDTBase=%016lx IDTBase=%016lx\n",
8649 vmcs_readl(HOST_GDTR_BASE), vmcs_readl(HOST_IDTR_BASE));
8650 pr_err("CR0=%016lx CR3=%016lx CR4=%016lx\n",
8651 vmcs_readl(HOST_CR0), vmcs_readl(HOST_CR3),
8652 vmcs_readl(HOST_CR4));
8653 pr_err("Sysenter RSP=%016lx CS:RIP=%04x:%016lx\n",
8654 vmcs_readl(HOST_IA32_SYSENTER_ESP),
8655 vmcs_read32(HOST_IA32_SYSENTER_CS),
8656 vmcs_readl(HOST_IA32_SYSENTER_EIP));
8657 if (vmexit_ctl & (VM_EXIT_LOAD_IA32_PAT | VM_EXIT_LOAD_IA32_EFER))
Paolo Bonzini845c5b402015-12-03 15:51:00 +01008658 pr_err("EFER = 0x%016llx PAT = 0x%016llx\n",
8659 vmcs_read64(HOST_IA32_EFER),
8660 vmcs_read64(HOST_IA32_PAT));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02008661 if (vmexit_ctl & VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL)
Paolo Bonzini845c5b402015-12-03 15:51:00 +01008662 pr_err("PerfGlobCtl = 0x%016llx\n",
8663 vmcs_read64(HOST_IA32_PERF_GLOBAL_CTRL));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02008664
8665 pr_err("*** Control State ***\n");
8666 pr_err("PinBased=%08x CPUBased=%08x SecondaryExec=%08x\n",
8667 pin_based_exec_ctrl, cpu_based_exec_ctrl, secondary_exec_control);
8668 pr_err("EntryControls=%08x ExitControls=%08x\n", vmentry_ctl, vmexit_ctl);
8669 pr_err("ExceptionBitmap=%08x PFECmask=%08x PFECmatch=%08x\n",
8670 vmcs_read32(EXCEPTION_BITMAP),
8671 vmcs_read32(PAGE_FAULT_ERROR_CODE_MASK),
8672 vmcs_read32(PAGE_FAULT_ERROR_CODE_MATCH));
8673 pr_err("VMEntry: intr_info=%08x errcode=%08x ilen=%08x\n",
8674 vmcs_read32(VM_ENTRY_INTR_INFO_FIELD),
8675 vmcs_read32(VM_ENTRY_EXCEPTION_ERROR_CODE),
8676 vmcs_read32(VM_ENTRY_INSTRUCTION_LEN));
8677 pr_err("VMExit: intr_info=%08x errcode=%08x ilen=%08x\n",
8678 vmcs_read32(VM_EXIT_INTR_INFO),
8679 vmcs_read32(VM_EXIT_INTR_ERROR_CODE),
8680 vmcs_read32(VM_EXIT_INSTRUCTION_LEN));
8681 pr_err(" reason=%08x qualification=%016lx\n",
8682 vmcs_read32(VM_EXIT_REASON), vmcs_readl(EXIT_QUALIFICATION));
8683 pr_err("IDTVectoring: info=%08x errcode=%08x\n",
8684 vmcs_read32(IDT_VECTORING_INFO_FIELD),
8685 vmcs_read32(IDT_VECTORING_ERROR_CODE));
Paolo Bonzini845c5b402015-12-03 15:51:00 +01008686 pr_err("TSC Offset = 0x%016llx\n", vmcs_read64(TSC_OFFSET));
Haozhong Zhang8cfe9862015-10-20 15:39:12 +08008687 if (secondary_exec_control & SECONDARY_EXEC_TSC_SCALING)
Paolo Bonzini845c5b402015-12-03 15:51:00 +01008688 pr_err("TSC Multiplier = 0x%016llx\n",
8689 vmcs_read64(TSC_MULTIPLIER));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02008690 if (cpu_based_exec_ctrl & CPU_BASED_TPR_SHADOW)
8691 pr_err("TPR Threshold = 0x%02x\n", vmcs_read32(TPR_THRESHOLD));
8692 if (pin_based_exec_ctrl & PIN_BASED_POSTED_INTR)
8693 pr_err("PostedIntrVec = 0x%02x\n", vmcs_read16(POSTED_INTR_NV));
8694 if ((secondary_exec_control & SECONDARY_EXEC_ENABLE_EPT))
Paolo Bonzini845c5b402015-12-03 15:51:00 +01008695 pr_err("EPT pointer = 0x%016llx\n", vmcs_read64(EPT_POINTER));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02008696 n = vmcs_read32(CR3_TARGET_COUNT);
8697 for (i = 0; i + 1 < n; i += 4)
8698 pr_err("CR3 target%u=%016lx target%u=%016lx\n",
8699 i, vmcs_readl(CR3_TARGET_VALUE0 + i * 2),
8700 i + 1, vmcs_readl(CR3_TARGET_VALUE0 + i * 2 + 2));
8701 if (i < n)
8702 pr_err("CR3 target%u=%016lx\n",
8703 i, vmcs_readl(CR3_TARGET_VALUE0 + i * 2));
8704 if (secondary_exec_control & SECONDARY_EXEC_PAUSE_LOOP_EXITING)
8705 pr_err("PLE Gap=%08x Window=%08x\n",
8706 vmcs_read32(PLE_GAP), vmcs_read32(PLE_WINDOW));
8707 if (secondary_exec_control & SECONDARY_EXEC_ENABLE_VPID)
8708 pr_err("Virtual processor ID = 0x%04x\n",
8709 vmcs_read16(VIRTUAL_PROCESSOR_ID));
8710}
8711
Avi Kivity6aa8b732006-12-10 02:21:36 -08008712/*
8713 * The guest has exited. See if we can fix it or if we need userspace
8714 * assistance.
8715 */
Avi Kivity851ba692009-08-24 11:10:17 +03008716static int vmx_handle_exit(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08008717{
Avi Kivity29bd8a72007-09-10 17:27:03 +03008718 struct vcpu_vmx *vmx = to_vmx(vcpu);
Andi Kleena0861c02009-06-08 17:37:09 +08008719 u32 exit_reason = vmx->exit_reason;
Avi Kivity1155f762007-11-22 11:30:47 +02008720 u32 vectoring_info = vmx->idt_vectoring_info;
Avi Kivity29bd8a72007-09-10 17:27:03 +03008721
Paolo Bonzini8b89fe12015-12-10 18:37:32 +01008722 trace_kvm_exit(exit_reason, vcpu, KVM_ISA_VMX);
8723
Kai Huang843e4332015-01-28 10:54:28 +08008724 /*
8725 * Flush logged GPAs PML buffer, this will make dirty_bitmap more
8726 * updated. Another good is, in kvm_vm_ioctl_get_dirty_log, before
8727 * querying dirty_bitmap, we only need to kick all vcpus out of guest
8728 * mode as if vcpus is in root mode, the PML buffer must has been
8729 * flushed already.
8730 */
8731 if (enable_pml)
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02008732 vmx_flush_pml_buffer(vcpu);
Kai Huang843e4332015-01-28 10:54:28 +08008733
Mohammed Gamal80ced182009-09-01 12:48:18 +02008734 /* If guest state is invalid, start emulating */
Gleb Natapov14168782013-01-21 15:36:49 +02008735 if (vmx->emulation_required)
Mohammed Gamal80ced182009-09-01 12:48:18 +02008736 return handle_invalid_guest_state(vcpu);
Guillaume Thouvenin1d5a4d92008-10-29 09:39:42 +01008737
Paolo Bonzini7313c692017-07-27 10:31:25 +02008738 if (is_guest_mode(vcpu) && nested_vmx_exit_reflected(vcpu, exit_reason))
8739 return nested_vmx_reflect_vmexit(vcpu, exit_reason);
Nadav Har'El644d7112011-05-25 23:12:35 +03008740
Mohammed Gamal51207022010-05-31 22:40:54 +03008741 if (exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY) {
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02008742 dump_vmcs();
Mohammed Gamal51207022010-05-31 22:40:54 +03008743 vcpu->run->exit_reason = KVM_EXIT_FAIL_ENTRY;
8744 vcpu->run->fail_entry.hardware_entry_failure_reason
8745 = exit_reason;
8746 return 0;
8747 }
8748
Avi Kivity29bd8a72007-09-10 17:27:03 +03008749 if (unlikely(vmx->fail)) {
Avi Kivity851ba692009-08-24 11:10:17 +03008750 vcpu->run->exit_reason = KVM_EXIT_FAIL_ENTRY;
8751 vcpu->run->fail_entry.hardware_entry_failure_reason
Avi Kivity29bd8a72007-09-10 17:27:03 +03008752 = vmcs_read32(VM_INSTRUCTION_ERROR);
8753 return 0;
8754 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08008755
Xiao Guangrongb9bf6882012-10-17 13:46:52 +08008756 /*
8757 * Note:
8758 * Do not try to fix EXIT_REASON_EPT_MISCONFIG if it caused by
8759 * delivery event since it indicates guest is accessing MMIO.
8760 * The vm-exit can be triggered again after return to guest that
8761 * will cause infinite loop.
8762 */
Mike Dayd77c26f2007-10-08 09:02:08 -04008763 if ((vectoring_info & VECTORING_INFO_VALID_MASK) &&
Sheng Yang14394422008-04-28 12:24:45 +08008764 (exit_reason != EXIT_REASON_EXCEPTION_NMI &&
Jan Kiszka60637aa2008-09-26 09:30:47 +02008765 exit_reason != EXIT_REASON_EPT_VIOLATION &&
Cao, Leib244c9f2016-07-15 13:54:04 +00008766 exit_reason != EXIT_REASON_PML_FULL &&
Xiao Guangrongb9bf6882012-10-17 13:46:52 +08008767 exit_reason != EXIT_REASON_TASK_SWITCH)) {
8768 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
8769 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_DELIVERY_EV;
Paolo Bonzini70bcd702017-07-05 12:38:06 +02008770 vcpu->run->internal.ndata = 3;
Xiao Guangrongb9bf6882012-10-17 13:46:52 +08008771 vcpu->run->internal.data[0] = vectoring_info;
8772 vcpu->run->internal.data[1] = exit_reason;
Paolo Bonzini70bcd702017-07-05 12:38:06 +02008773 vcpu->run->internal.data[2] = vcpu->arch.exit_qualification;
8774 if (exit_reason == EXIT_REASON_EPT_MISCONFIG) {
8775 vcpu->run->internal.ndata++;
8776 vcpu->run->internal.data[3] =
8777 vmcs_read64(GUEST_PHYSICAL_ADDRESS);
8778 }
Xiao Guangrongb9bf6882012-10-17 13:46:52 +08008779 return 0;
8780 }
Jan Kiszka3b86cd92008-09-26 09:30:57 +02008781
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01008782 if (unlikely(!enable_vnmi &&
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01008783 vmx->loaded_vmcs->soft_vnmi_blocked)) {
8784 if (vmx_interrupt_allowed(vcpu)) {
8785 vmx->loaded_vmcs->soft_vnmi_blocked = 0;
8786 } else if (vmx->loaded_vmcs->vnmi_blocked_time > 1000000000LL &&
8787 vcpu->arch.nmi_pending) {
8788 /*
8789 * This CPU don't support us in finding the end of an
8790 * NMI-blocked window if the guest runs with IRQs
8791 * disabled. So we pull the trigger after 1 s of
8792 * futile waiting, but inform the user about this.
8793 */
8794 printk(KERN_WARNING "%s: Breaking out of NMI-blocked "
8795 "state on VCPU %d after 1 s timeout\n",
8796 __func__, vcpu->vcpu_id);
8797 vmx->loaded_vmcs->soft_vnmi_blocked = 0;
8798 }
8799 }
8800
Avi Kivity6aa8b732006-12-10 02:21:36 -08008801 if (exit_reason < kvm_vmx_max_exit_handlers
8802 && kvm_vmx_exit_handlers[exit_reason])
Avi Kivity851ba692009-08-24 11:10:17 +03008803 return kvm_vmx_exit_handlers[exit_reason](vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08008804 else {
Radim Krčmář6c6c5e02017-01-13 18:59:04 +01008805 vcpu_unimpl(vcpu, "vmx: unexpected exit reason 0x%x\n",
8806 exit_reason);
Michael S. Tsirkin2bc19dc2014-09-18 16:21:16 +03008807 kvm_queue_exception(vcpu, UD_VECTOR);
8808 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08008809 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08008810}
8811
Gleb Natapov95ba8273132009-04-21 17:45:08 +03008812static void update_cr8_intercept(struct kvm_vcpu *vcpu, int tpr, int irr)
Yang, Sheng6e5d8652007-09-12 18:03:11 +08008813{
Wanpeng Lia7c0b072014-08-21 19:46:50 +08008814 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
8815
8816 if (is_guest_mode(vcpu) &&
8817 nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW))
8818 return;
8819
Gleb Natapov95ba8273132009-04-21 17:45:08 +03008820 if (irr == -1 || tpr < irr) {
Yang, Sheng6e5d8652007-09-12 18:03:11 +08008821 vmcs_write32(TPR_THRESHOLD, 0);
8822 return;
8823 }
8824
Gleb Natapov95ba8273132009-04-21 17:45:08 +03008825 vmcs_write32(TPR_THRESHOLD, irr);
Yang, Sheng6e5d8652007-09-12 18:03:11 +08008826}
8827
Yang Zhang8d146952013-01-25 10:18:50 +08008828static void vmx_set_virtual_x2apic_mode(struct kvm_vcpu *vcpu, bool set)
8829{
8830 u32 sec_exec_control;
8831
Radim Krčmářdccbfcf2016-08-08 20:16:23 +02008832 /* Postpone execution until vmcs01 is the current VMCS. */
8833 if (is_guest_mode(vcpu)) {
8834 to_vmx(vcpu)->nested.change_vmcs01_virtual_x2apic_mode = true;
8835 return;
8836 }
8837
Wanpeng Lif6e90f92016-09-22 07:43:25 +08008838 if (!cpu_has_vmx_virtualize_x2apic_mode())
Yang Zhang8d146952013-01-25 10:18:50 +08008839 return;
8840
Paolo Bonzini35754c92015-07-29 12:05:37 +02008841 if (!cpu_need_tpr_shadow(vcpu))
Yang Zhang8d146952013-01-25 10:18:50 +08008842 return;
8843
8844 sec_exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
8845
8846 if (set) {
8847 sec_exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
8848 sec_exec_control |= SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
8849 } else {
8850 sec_exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
8851 sec_exec_control |= SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
Jim Mattsonfb6c8192017-03-16 13:53:59 -07008852 vmx_flush_tlb_ept_only(vcpu);
Yang Zhang8d146952013-01-25 10:18:50 +08008853 }
8854 vmcs_write32(SECONDARY_VM_EXEC_CONTROL, sec_exec_control);
8855
8856 vmx_set_msr_bitmap(vcpu);
8857}
8858
Tang Chen38b99172014-09-24 15:57:54 +08008859static void vmx_set_apic_access_page_addr(struct kvm_vcpu *vcpu, hpa_t hpa)
8860{
8861 struct vcpu_vmx *vmx = to_vmx(vcpu);
8862
8863 /*
8864 * Currently we do not handle the nested case where L2 has an
8865 * APIC access page of its own; that page is still pinned.
8866 * Hence, we skip the case where the VCPU is in guest mode _and_
8867 * L1 prepared an APIC access page for L2.
8868 *
8869 * For the case where L1 and L2 share the same APIC access page
8870 * (flexpriority=Y but SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES clear
8871 * in the vmcs12), this function will only update either the vmcs01
8872 * or the vmcs02. If the former, the vmcs02 will be updated by
8873 * prepare_vmcs02. If the latter, the vmcs01 will be updated in
8874 * the next L2->L1 exit.
8875 */
8876 if (!is_guest_mode(vcpu) ||
David Matlack4f2777b2016-07-13 17:16:37 -07008877 !nested_cpu_has2(get_vmcs12(&vmx->vcpu),
Jim Mattsonfb6c8192017-03-16 13:53:59 -07008878 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) {
Tang Chen38b99172014-09-24 15:57:54 +08008879 vmcs_write64(APIC_ACCESS_ADDR, hpa);
Jim Mattsonfb6c8192017-03-16 13:53:59 -07008880 vmx_flush_tlb_ept_only(vcpu);
8881 }
Tang Chen38b99172014-09-24 15:57:54 +08008882}
8883
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +02008884static void vmx_hwapic_isr_update(struct kvm_vcpu *vcpu, int max_isr)
Yang Zhangc7c9c562013-01-25 10:18:51 +08008885{
8886 u16 status;
8887 u8 old;
8888
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +02008889 if (max_isr == -1)
8890 max_isr = 0;
Yang Zhangc7c9c562013-01-25 10:18:51 +08008891
8892 status = vmcs_read16(GUEST_INTR_STATUS);
8893 old = status >> 8;
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +02008894 if (max_isr != old) {
Yang Zhangc7c9c562013-01-25 10:18:51 +08008895 status &= 0xff;
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +02008896 status |= max_isr << 8;
Yang Zhangc7c9c562013-01-25 10:18:51 +08008897 vmcs_write16(GUEST_INTR_STATUS, status);
8898 }
8899}
8900
8901static void vmx_set_rvi(int vector)
8902{
8903 u16 status;
8904 u8 old;
8905
Wei Wang4114c272014-11-05 10:53:43 +08008906 if (vector == -1)
8907 vector = 0;
8908
Yang Zhangc7c9c562013-01-25 10:18:51 +08008909 status = vmcs_read16(GUEST_INTR_STATUS);
8910 old = (u8)status & 0xff;
8911 if ((u8)vector != old) {
8912 status &= ~0xff;
8913 status |= (u8)vector;
8914 vmcs_write16(GUEST_INTR_STATUS, status);
8915 }
8916}
8917
8918static void vmx_hwapic_irr_update(struct kvm_vcpu *vcpu, int max_irr)
8919{
Liran Alon851c1a182017-12-24 18:12:56 +02008920 /*
8921 * When running L2, updating RVI is only relevant when
8922 * vmcs12 virtual-interrupt-delivery enabled.
8923 * However, it can be enabled only when L1 also
8924 * intercepts external-interrupts and in that case
8925 * we should not update vmcs02 RVI but instead intercept
8926 * interrupt. Therefore, do nothing when running L2.
8927 */
8928 if (!is_guest_mode(vcpu))
Wanpeng Li963fee12014-07-17 19:03:00 +08008929 vmx_set_rvi(max_irr);
Yang Zhangc7c9c562013-01-25 10:18:51 +08008930}
8931
Paolo Bonzini76dfafd52016-12-19 17:17:11 +01008932static int vmx_sync_pir_to_irr(struct kvm_vcpu *vcpu)
Paolo Bonzini810e6de2016-12-19 13:05:46 +01008933{
8934 struct vcpu_vmx *vmx = to_vmx(vcpu);
Paolo Bonzini76dfafd52016-12-19 17:17:11 +01008935 int max_irr;
Liran Alonf27a85c2017-12-24 18:12:55 +02008936 bool max_irr_updated;
Paolo Bonzini810e6de2016-12-19 13:05:46 +01008937
Paolo Bonzini76dfafd52016-12-19 17:17:11 +01008938 WARN_ON(!vcpu->arch.apicv_active);
8939 if (pi_test_on(&vmx->pi_desc)) {
8940 pi_clear_on(&vmx->pi_desc);
8941 /*
8942 * IOMMU can write to PIR.ON, so the barrier matters even on UP.
8943 * But on x86 this is just a compiler barrier anyway.
8944 */
8945 smp_mb__after_atomic();
Liran Alonf27a85c2017-12-24 18:12:55 +02008946 max_irr_updated =
8947 kvm_apic_update_irr(vcpu, vmx->pi_desc.pir, &max_irr);
8948
8949 /*
8950 * If we are running L2 and L1 has a new pending interrupt
8951 * which can be injected, we should re-evaluate
8952 * what should be done with this new L1 interrupt.
Liran Alon851c1a182017-12-24 18:12:56 +02008953 * If L1 intercepts external-interrupts, we should
8954 * exit from L2 to L1. Otherwise, interrupt should be
8955 * delivered directly to L2.
Liran Alonf27a85c2017-12-24 18:12:55 +02008956 */
Liran Alon851c1a182017-12-24 18:12:56 +02008957 if (is_guest_mode(vcpu) && max_irr_updated) {
8958 if (nested_exit_on_intr(vcpu))
8959 kvm_vcpu_exiting_guest_mode(vcpu);
8960 else
8961 kvm_make_request(KVM_REQ_EVENT, vcpu);
8962 }
Paolo Bonzini76dfafd52016-12-19 17:17:11 +01008963 } else {
8964 max_irr = kvm_lapic_find_highest_irr(vcpu);
8965 }
8966 vmx_hwapic_irr_update(vcpu, max_irr);
8967 return max_irr;
Paolo Bonzini810e6de2016-12-19 13:05:46 +01008968}
8969
Andrey Smetanin63086302015-11-10 15:36:32 +03008970static void vmx_load_eoi_exitmap(struct kvm_vcpu *vcpu, u64 *eoi_exit_bitmap)
Yang Zhangc7c9c562013-01-25 10:18:51 +08008971{
Andrey Smetanind62caab2015-11-10 15:36:33 +03008972 if (!kvm_vcpu_apicv_active(vcpu))
Yang Zhang3d81bc72013-04-11 19:25:13 +08008973 return;
8974
Yang Zhangc7c9c562013-01-25 10:18:51 +08008975 vmcs_write64(EOI_EXIT_BITMAP0, eoi_exit_bitmap[0]);
8976 vmcs_write64(EOI_EXIT_BITMAP1, eoi_exit_bitmap[1]);
8977 vmcs_write64(EOI_EXIT_BITMAP2, eoi_exit_bitmap[2]);
8978 vmcs_write64(EOI_EXIT_BITMAP3, eoi_exit_bitmap[3]);
8979}
8980
Paolo Bonzini967235d2016-12-19 14:03:45 +01008981static void vmx_apicv_post_state_restore(struct kvm_vcpu *vcpu)
8982{
8983 struct vcpu_vmx *vmx = to_vmx(vcpu);
8984
8985 pi_clear_on(&vmx->pi_desc);
8986 memset(vmx->pi_desc.pir, 0, sizeof(vmx->pi_desc.pir));
8987}
8988
Avi Kivity51aa01d2010-07-20 14:31:20 +03008989static void vmx_complete_atomic_exit(struct vcpu_vmx *vmx)
Avi Kivitycf393f72008-07-01 16:20:21 +03008990{
Jim Mattson48ae0fb2017-05-22 09:48:33 -07008991 u32 exit_intr_info = 0;
8992 u16 basic_exit_reason = (u16)vmx->exit_reason;
Avi Kivity00eba012011-03-07 17:24:54 +02008993
Jim Mattson48ae0fb2017-05-22 09:48:33 -07008994 if (!(basic_exit_reason == EXIT_REASON_MCE_DURING_VMENTRY
8995 || basic_exit_reason == EXIT_REASON_EXCEPTION_NMI))
Avi Kivity00eba012011-03-07 17:24:54 +02008996 return;
8997
Jim Mattson48ae0fb2017-05-22 09:48:33 -07008998 if (!(vmx->exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY))
8999 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
9000 vmx->exit_intr_info = exit_intr_info;
Andi Kleena0861c02009-06-08 17:37:09 +08009001
Wanpeng Li1261bfa2017-07-13 18:30:40 -07009002 /* if exit due to PF check for async PF */
9003 if (is_page_fault(exit_intr_info))
9004 vmx->vcpu.arch.apf.host_apf_reason = kvm_read_and_reset_pf_reason();
9005
Andi Kleena0861c02009-06-08 17:37:09 +08009006 /* Handle machine checks before interrupts are enabled */
Jim Mattson48ae0fb2017-05-22 09:48:33 -07009007 if (basic_exit_reason == EXIT_REASON_MCE_DURING_VMENTRY ||
9008 is_machine_check(exit_intr_info))
Andi Kleena0861c02009-06-08 17:37:09 +08009009 kvm_machine_check();
9010
Gleb Natapov20f65982009-05-11 13:35:55 +03009011 /* We need to handle NMIs before interrupts are enabled */
Jim Mattsonef85b672016-12-12 11:01:37 -08009012 if (is_nmi(exit_intr_info)) {
Zhang, Yanminff9d07a2010-04-19 13:32:45 +08009013 kvm_before_handle_nmi(&vmx->vcpu);
Gleb Natapov20f65982009-05-11 13:35:55 +03009014 asm("int $2");
Zhang, Yanminff9d07a2010-04-19 13:32:45 +08009015 kvm_after_handle_nmi(&vmx->vcpu);
9016 }
Avi Kivity51aa01d2010-07-20 14:31:20 +03009017}
Gleb Natapov20f65982009-05-11 13:35:55 +03009018
Yang Zhanga547c6d2013-04-11 19:25:10 +08009019static void vmx_handle_external_intr(struct kvm_vcpu *vcpu)
9020{
9021 u32 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
9022
Yang Zhanga547c6d2013-04-11 19:25:10 +08009023 if ((exit_intr_info & (INTR_INFO_VALID_MASK | INTR_INFO_INTR_TYPE_MASK))
9024 == (INTR_INFO_VALID_MASK | INTR_TYPE_EXT_INTR)) {
9025 unsigned int vector;
9026 unsigned long entry;
9027 gate_desc *desc;
9028 struct vcpu_vmx *vmx = to_vmx(vcpu);
9029#ifdef CONFIG_X86_64
9030 unsigned long tmp;
9031#endif
9032
9033 vector = exit_intr_info & INTR_INFO_VECTOR_MASK;
9034 desc = (gate_desc *)vmx->host_idt_base + vector;
Thomas Gleixner64b163f2017-08-28 08:47:37 +02009035 entry = gate_offset(desc);
Yang Zhanga547c6d2013-04-11 19:25:10 +08009036 asm volatile(
9037#ifdef CONFIG_X86_64
9038 "mov %%" _ASM_SP ", %[sp]\n\t"
9039 "and $0xfffffffffffffff0, %%" _ASM_SP "\n\t"
9040 "push $%c[ss]\n\t"
9041 "push %[sp]\n\t"
9042#endif
9043 "pushf\n\t"
Yang Zhanga547c6d2013-04-11 19:25:10 +08009044 __ASM_SIZE(push) " $%c[cs]\n\t"
9045 "call *%[entry]\n\t"
9046 :
9047#ifdef CONFIG_X86_64
Chris J Arges3f62de52016-01-22 15:44:38 -06009048 [sp]"=&r"(tmp),
Yang Zhanga547c6d2013-04-11 19:25:10 +08009049#endif
Josh Poimboeuff5caf622017-09-20 16:24:33 -05009050 ASM_CALL_CONSTRAINT
Yang Zhanga547c6d2013-04-11 19:25:10 +08009051 :
9052 [entry]"r"(entry),
9053 [ss]"i"(__KERNEL_DS),
9054 [cs]"i"(__KERNEL_CS)
9055 );
Paolo Bonzinif2485b32016-06-15 15:23:11 +02009056 }
Yang Zhanga547c6d2013-04-11 19:25:10 +08009057}
Josh Poimboeufc207aee2017-06-28 10:11:06 -05009058STACK_FRAME_NON_STANDARD(vmx_handle_external_intr);
Yang Zhanga547c6d2013-04-11 19:25:10 +08009059
Paolo Bonzini6d396b52015-04-01 14:25:33 +02009060static bool vmx_has_high_real_mode_segbase(void)
9061{
9062 return enable_unrestricted_guest || emulate_invalid_guest_state;
9063}
9064
Liu, Jinsongda8999d2014-02-24 10:55:46 +00009065static bool vmx_mpx_supported(void)
9066{
9067 return (vmcs_config.vmexit_ctrl & VM_EXIT_CLEAR_BNDCFGS) &&
9068 (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_BNDCFGS);
9069}
9070
Wanpeng Li55412b22014-12-02 19:21:30 +08009071static bool vmx_xsaves_supported(void)
9072{
9073 return vmcs_config.cpu_based_2nd_exec_ctrl &
9074 SECONDARY_EXEC_XSAVES;
9075}
9076
Paolo Bonzini66336ca2016-07-12 10:36:41 +02009077static bool vmx_umip_emulated(void)
9078{
Paolo Bonzini0367f202016-07-12 10:44:55 +02009079 return vmcs_config.cpu_based_2nd_exec_ctrl &
9080 SECONDARY_EXEC_DESC;
Paolo Bonzini66336ca2016-07-12 10:36:41 +02009081}
9082
Avi Kivity51aa01d2010-07-20 14:31:20 +03009083static void vmx_recover_nmi_blocking(struct vcpu_vmx *vmx)
9084{
Avi Kivityc5ca8e52011-03-07 17:37:37 +02009085 u32 exit_intr_info;
Avi Kivity51aa01d2010-07-20 14:31:20 +03009086 bool unblock_nmi;
9087 u8 vector;
9088 bool idtv_info_valid;
9089
9090 idtv_info_valid = vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK;
Gleb Natapov20f65982009-05-11 13:35:55 +03009091
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01009092 if (enable_vnmi) {
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01009093 if (vmx->loaded_vmcs->nmi_known_unmasked)
9094 return;
9095 /*
9096 * Can't use vmx->exit_intr_info since we're not sure what
9097 * the exit reason is.
9098 */
9099 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
9100 unblock_nmi = (exit_intr_info & INTR_INFO_UNBLOCK_NMI) != 0;
9101 vector = exit_intr_info & INTR_INFO_VECTOR_MASK;
9102 /*
9103 * SDM 3: 27.7.1.2 (September 2008)
9104 * Re-set bit "block by NMI" before VM entry if vmexit caused by
9105 * a guest IRET fault.
9106 * SDM 3: 23.2.2 (September 2008)
9107 * Bit 12 is undefined in any of the following cases:
9108 * If the VM exit sets the valid bit in the IDT-vectoring
9109 * information field.
9110 * If the VM exit is due to a double fault.
9111 */
9112 if ((exit_intr_info & INTR_INFO_VALID_MASK) && unblock_nmi &&
9113 vector != DF_VECTOR && !idtv_info_valid)
9114 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
9115 GUEST_INTR_STATE_NMI);
9116 else
9117 vmx->loaded_vmcs->nmi_known_unmasked =
9118 !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO)
9119 & GUEST_INTR_STATE_NMI);
9120 } else if (unlikely(vmx->loaded_vmcs->soft_vnmi_blocked))
9121 vmx->loaded_vmcs->vnmi_blocked_time +=
9122 ktime_to_ns(ktime_sub(ktime_get(),
9123 vmx->loaded_vmcs->entry_time));
Avi Kivity51aa01d2010-07-20 14:31:20 +03009124}
9125
Jan Kiszka3ab66e82013-02-20 14:03:24 +01009126static void __vmx_complete_interrupts(struct kvm_vcpu *vcpu,
Avi Kivity83422e12010-07-20 14:43:23 +03009127 u32 idt_vectoring_info,
9128 int instr_len_field,
9129 int error_code_field)
Avi Kivity51aa01d2010-07-20 14:31:20 +03009130{
Avi Kivity51aa01d2010-07-20 14:31:20 +03009131 u8 vector;
9132 int type;
9133 bool idtv_info_valid;
9134
9135 idtv_info_valid = idt_vectoring_info & VECTORING_INFO_VALID_MASK;
Avi Kivity668f6122008-07-02 09:28:55 +03009136
Jan Kiszka3ab66e82013-02-20 14:03:24 +01009137 vcpu->arch.nmi_injected = false;
9138 kvm_clear_exception_queue(vcpu);
9139 kvm_clear_interrupt_queue(vcpu);
Gleb Natapov37b96e92009-03-30 16:03:13 +03009140
9141 if (!idtv_info_valid)
9142 return;
9143
Jan Kiszka3ab66e82013-02-20 14:03:24 +01009144 kvm_make_request(KVM_REQ_EVENT, vcpu);
Avi Kivity3842d132010-07-27 12:30:24 +03009145
Avi Kivity668f6122008-07-02 09:28:55 +03009146 vector = idt_vectoring_info & VECTORING_INFO_VECTOR_MASK;
9147 type = idt_vectoring_info & VECTORING_INFO_TYPE_MASK;
Gleb Natapov37b96e92009-03-30 16:03:13 +03009148
Gleb Natapov64a7ec02009-03-30 16:03:29 +03009149 switch (type) {
Gleb Natapov37b96e92009-03-30 16:03:13 +03009150 case INTR_TYPE_NMI_INTR:
Jan Kiszka3ab66e82013-02-20 14:03:24 +01009151 vcpu->arch.nmi_injected = true;
Avi Kivity668f6122008-07-02 09:28:55 +03009152 /*
Gleb Natapov7b4a25c2009-03-30 16:03:08 +03009153 * SDM 3: 27.7.1.2 (September 2008)
Gleb Natapov37b96e92009-03-30 16:03:13 +03009154 * Clear bit "block by NMI" before VM entry if a NMI
9155 * delivery faulted.
Avi Kivity668f6122008-07-02 09:28:55 +03009156 */
Jan Kiszka3ab66e82013-02-20 14:03:24 +01009157 vmx_set_nmi_mask(vcpu, false);
Gleb Natapov37b96e92009-03-30 16:03:13 +03009158 break;
Gleb Natapov37b96e92009-03-30 16:03:13 +03009159 case INTR_TYPE_SOFT_EXCEPTION:
Jan Kiszka3ab66e82013-02-20 14:03:24 +01009160 vcpu->arch.event_exit_inst_len = vmcs_read32(instr_len_field);
Gleb Natapov66fd3f72009-05-11 13:35:50 +03009161 /* fall through */
9162 case INTR_TYPE_HARD_EXCEPTION:
Avi Kivity35920a32008-07-03 14:50:12 +03009163 if (idt_vectoring_info & VECTORING_INFO_DELIVER_CODE_MASK) {
Avi Kivity83422e12010-07-20 14:43:23 +03009164 u32 err = vmcs_read32(error_code_field);
Gleb Natapov851eb6672013-09-25 12:51:34 +03009165 kvm_requeue_exception_e(vcpu, vector, err);
Avi Kivity35920a32008-07-03 14:50:12 +03009166 } else
Gleb Natapov851eb6672013-09-25 12:51:34 +03009167 kvm_requeue_exception(vcpu, vector);
Gleb Natapov37b96e92009-03-30 16:03:13 +03009168 break;
Gleb Natapov66fd3f72009-05-11 13:35:50 +03009169 case INTR_TYPE_SOFT_INTR:
Jan Kiszka3ab66e82013-02-20 14:03:24 +01009170 vcpu->arch.event_exit_inst_len = vmcs_read32(instr_len_field);
Gleb Natapov66fd3f72009-05-11 13:35:50 +03009171 /* fall through */
Gleb Natapov37b96e92009-03-30 16:03:13 +03009172 case INTR_TYPE_EXT_INTR:
Jan Kiszka3ab66e82013-02-20 14:03:24 +01009173 kvm_queue_interrupt(vcpu, vector, type == INTR_TYPE_SOFT_INTR);
Gleb Natapov37b96e92009-03-30 16:03:13 +03009174 break;
9175 default:
9176 break;
Avi Kivityf7d92382008-07-03 16:14:28 +03009177 }
Avi Kivitycf393f72008-07-01 16:20:21 +03009178}
9179
Avi Kivity83422e12010-07-20 14:43:23 +03009180static void vmx_complete_interrupts(struct vcpu_vmx *vmx)
9181{
Jan Kiszka3ab66e82013-02-20 14:03:24 +01009182 __vmx_complete_interrupts(&vmx->vcpu, vmx->idt_vectoring_info,
Avi Kivity83422e12010-07-20 14:43:23 +03009183 VM_EXIT_INSTRUCTION_LEN,
9184 IDT_VECTORING_ERROR_CODE);
9185}
9186
Avi Kivityb463a6f2010-07-20 15:06:17 +03009187static void vmx_cancel_injection(struct kvm_vcpu *vcpu)
9188{
Jan Kiszka3ab66e82013-02-20 14:03:24 +01009189 __vmx_complete_interrupts(vcpu,
Avi Kivityb463a6f2010-07-20 15:06:17 +03009190 vmcs_read32(VM_ENTRY_INTR_INFO_FIELD),
9191 VM_ENTRY_INSTRUCTION_LEN,
9192 VM_ENTRY_EXCEPTION_ERROR_CODE);
9193
9194 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0);
9195}
9196
Gleb Natapovd7cd9792011-10-05 14:01:23 +02009197static void atomic_switch_perf_msrs(struct vcpu_vmx *vmx)
9198{
9199 int i, nr_msrs;
9200 struct perf_guest_switch_msr *msrs;
9201
9202 msrs = perf_guest_get_msrs(&nr_msrs);
9203
9204 if (!msrs)
9205 return;
9206
9207 for (i = 0; i < nr_msrs; i++)
9208 if (msrs[i].host == msrs[i].guest)
9209 clear_atomic_switch_msr(vmx, msrs[i].msr);
9210 else
9211 add_atomic_switch_msr(vmx, msrs[i].msr, msrs[i].guest,
9212 msrs[i].host);
9213}
9214
Jiang Biao33365e72016-11-03 15:03:37 +08009215static void vmx_arm_hv_timer(struct kvm_vcpu *vcpu)
Yunhong Jiang64672c92016-06-13 14:19:59 -07009216{
9217 struct vcpu_vmx *vmx = to_vmx(vcpu);
9218 u64 tscl;
9219 u32 delta_tsc;
9220
9221 if (vmx->hv_deadline_tsc == -1)
9222 return;
9223
9224 tscl = rdtsc();
9225 if (vmx->hv_deadline_tsc > tscl)
9226 /* sure to be 32 bit only because checked on set_hv_timer */
9227 delta_tsc = (u32)((vmx->hv_deadline_tsc - tscl) >>
9228 cpu_preemption_timer_multi);
9229 else
9230 delta_tsc = 0;
9231
9232 vmcs_write32(VMX_PREEMPTION_TIMER_VALUE, delta_tsc);
9233}
9234
Lai Jiangshana3b5ba42011-02-11 14:29:40 +08009235static void __noclone vmx_vcpu_run(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08009236{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04009237 struct vcpu_vmx *vmx = to_vmx(vcpu);
Wanpeng Li74c55932017-11-29 01:31:20 -08009238 unsigned long cr3, cr4;
Avi Kivity104f2262010-11-18 13:12:52 +02009239
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01009240 /* Record the guest's net vcpu time for enforced NMI injections. */
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01009241 if (unlikely(!enable_vnmi &&
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01009242 vmx->loaded_vmcs->soft_vnmi_blocked))
9243 vmx->loaded_vmcs->entry_time = ktime_get();
9244
Avi Kivity104f2262010-11-18 13:12:52 +02009245 /* Don't enter VMX if guest state is invalid, let the exit handler
9246 start emulation until we arrive back to a valid state */
Gleb Natapov14168782013-01-21 15:36:49 +02009247 if (vmx->emulation_required)
Avi Kivity104f2262010-11-18 13:12:52 +02009248 return;
9249
Radim Krčmářa7653ec2014-08-21 18:08:07 +02009250 if (vmx->ple_window_dirty) {
9251 vmx->ple_window_dirty = false;
9252 vmcs_write32(PLE_WINDOW, vmx->ple_window);
9253 }
9254
Abel Gordon012f83c2013-04-18 14:39:25 +03009255 if (vmx->nested.sync_shadow_vmcs) {
9256 copy_vmcs12_to_shadow(vmx);
9257 vmx->nested.sync_shadow_vmcs = false;
9258 }
9259
Avi Kivity104f2262010-11-18 13:12:52 +02009260 if (test_bit(VCPU_REGS_RSP, (unsigned long *)&vcpu->arch.regs_dirty))
9261 vmcs_writel(GUEST_RSP, vcpu->arch.regs[VCPU_REGS_RSP]);
9262 if (test_bit(VCPU_REGS_RIP, (unsigned long *)&vcpu->arch.regs_dirty))
9263 vmcs_writel(GUEST_RIP, vcpu->arch.regs[VCPU_REGS_RIP]);
9264
Andy Lutomirskid6e41f12017-05-28 10:00:17 -07009265 cr3 = __get_current_cr3_fast();
Ladi Prosek44889942017-09-22 07:53:15 +02009266 if (unlikely(cr3 != vmx->loaded_vmcs->vmcs_host_cr3)) {
Andy Lutomirskid6e41f12017-05-28 10:00:17 -07009267 vmcs_writel(HOST_CR3, cr3);
Ladi Prosek44889942017-09-22 07:53:15 +02009268 vmx->loaded_vmcs->vmcs_host_cr3 = cr3;
Andy Lutomirskid6e41f12017-05-28 10:00:17 -07009269 }
9270
Andy Lutomirski1e02ce42014-10-24 15:58:08 -07009271 cr4 = cr4_read_shadow();
Ladi Prosek44889942017-09-22 07:53:15 +02009272 if (unlikely(cr4 != vmx->loaded_vmcs->vmcs_host_cr4)) {
Andy Lutomirskid974baa2014-10-08 09:02:13 -07009273 vmcs_writel(HOST_CR4, cr4);
Ladi Prosek44889942017-09-22 07:53:15 +02009274 vmx->loaded_vmcs->vmcs_host_cr4 = cr4;
Andy Lutomirskid974baa2014-10-08 09:02:13 -07009275 }
9276
Avi Kivity104f2262010-11-18 13:12:52 +02009277 /* When single-stepping over STI and MOV SS, we must clear the
9278 * corresponding interruptibility bits in the guest state. Otherwise
9279 * vmentry fails as it then expects bit 14 (BS) in pending debug
9280 * exceptions being set, but that's not correct for the guest debugging
9281 * case. */
9282 if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP)
9283 vmx_set_interrupt_shadow(vcpu, 0);
9284
Paolo Bonzinib9dd21e2017-08-23 23:14:38 +02009285 if (static_cpu_has(X86_FEATURE_PKU) &&
9286 kvm_read_cr4_bits(vcpu, X86_CR4_PKE) &&
9287 vcpu->arch.pkru != vmx->host_pkru)
9288 __write_pkru(vcpu->arch.pkru);
Xiao Guangrong1be0e612016-03-22 16:51:18 +08009289
Gleb Natapovd7cd9792011-10-05 14:01:23 +02009290 atomic_switch_perf_msrs(vmx);
9291
Yunhong Jiang64672c92016-06-13 14:19:59 -07009292 vmx_arm_hv_timer(vcpu);
9293
Nadav Har'Eld462b812011-05-24 15:26:10 +03009294 vmx->__launched = vmx->loaded_vmcs->launched;
Avi Kivity104f2262010-11-18 13:12:52 +02009295 asm(
Avi Kivity6aa8b732006-12-10 02:21:36 -08009296 /* Store host registers */
Avi Kivityb188c81f2012-09-16 15:10:58 +03009297 "push %%" _ASM_DX "; push %%" _ASM_BP ";"
9298 "push %%" _ASM_CX " \n\t" /* placeholder for guest rcx */
9299 "push %%" _ASM_CX " \n\t"
9300 "cmp %%" _ASM_SP ", %c[host_rsp](%0) \n\t"
Avi Kivity313dbd492008-07-17 18:04:30 +03009301 "je 1f \n\t"
Avi Kivityb188c81f2012-09-16 15:10:58 +03009302 "mov %%" _ASM_SP ", %c[host_rsp](%0) \n\t"
Avi Kivity4ecac3f2008-05-13 13:23:38 +03009303 __ex(ASM_VMX_VMWRITE_RSP_RDX) "\n\t"
Avi Kivity313dbd492008-07-17 18:04:30 +03009304 "1: \n\t"
Avi Kivityd3edefc2009-06-16 12:33:56 +03009305 /* Reload cr2 if changed */
Avi Kivityb188c81f2012-09-16 15:10:58 +03009306 "mov %c[cr2](%0), %%" _ASM_AX " \n\t"
9307 "mov %%cr2, %%" _ASM_DX " \n\t"
9308 "cmp %%" _ASM_AX ", %%" _ASM_DX " \n\t"
Avi Kivityd3edefc2009-06-16 12:33:56 +03009309 "je 2f \n\t"
Avi Kivityb188c81f2012-09-16 15:10:58 +03009310 "mov %%" _ASM_AX", %%cr2 \n\t"
Avi Kivityd3edefc2009-06-16 12:33:56 +03009311 "2: \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08009312 /* Check if vmlaunch of vmresume is needed */
Avi Kivitye08aa782007-11-15 18:06:18 +02009313 "cmpl $0, %c[launched](%0) \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08009314 /* Load guest registers. Don't clobber flags. */
Avi Kivityb188c81f2012-09-16 15:10:58 +03009315 "mov %c[rax](%0), %%" _ASM_AX " \n\t"
9316 "mov %c[rbx](%0), %%" _ASM_BX " \n\t"
9317 "mov %c[rdx](%0), %%" _ASM_DX " \n\t"
9318 "mov %c[rsi](%0), %%" _ASM_SI " \n\t"
9319 "mov %c[rdi](%0), %%" _ASM_DI " \n\t"
9320 "mov %c[rbp](%0), %%" _ASM_BP " \n\t"
Avi Kivity05b3e0c2006-12-13 00:33:45 -08009321#ifdef CONFIG_X86_64
Avi Kivitye08aa782007-11-15 18:06:18 +02009322 "mov %c[r8](%0), %%r8 \n\t"
9323 "mov %c[r9](%0), %%r9 \n\t"
9324 "mov %c[r10](%0), %%r10 \n\t"
9325 "mov %c[r11](%0), %%r11 \n\t"
9326 "mov %c[r12](%0), %%r12 \n\t"
9327 "mov %c[r13](%0), %%r13 \n\t"
9328 "mov %c[r14](%0), %%r14 \n\t"
9329 "mov %c[r15](%0), %%r15 \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08009330#endif
Avi Kivityb188c81f2012-09-16 15:10:58 +03009331 "mov %c[rcx](%0), %%" _ASM_CX " \n\t" /* kills %0 (ecx) */
Avi Kivityc8019492008-07-14 14:44:59 +03009332
Avi Kivity6aa8b732006-12-10 02:21:36 -08009333 /* Enter guest mode */
Avi Kivity83287ea422012-09-16 15:10:57 +03009334 "jne 1f \n\t"
Avi Kivity4ecac3f2008-05-13 13:23:38 +03009335 __ex(ASM_VMX_VMLAUNCH) "\n\t"
Avi Kivity83287ea422012-09-16 15:10:57 +03009336 "jmp 2f \n\t"
9337 "1: " __ex(ASM_VMX_VMRESUME) "\n\t"
9338 "2: "
Avi Kivity6aa8b732006-12-10 02:21:36 -08009339 /* Save guest registers, load host registers, keep flags */
Avi Kivityb188c81f2012-09-16 15:10:58 +03009340 "mov %0, %c[wordsize](%%" _ASM_SP ") \n\t"
Avi Kivity40712fa2011-01-06 18:09:12 +02009341 "pop %0 \n\t"
Avi Kivityb188c81f2012-09-16 15:10:58 +03009342 "mov %%" _ASM_AX ", %c[rax](%0) \n\t"
9343 "mov %%" _ASM_BX ", %c[rbx](%0) \n\t"
9344 __ASM_SIZE(pop) " %c[rcx](%0) \n\t"
9345 "mov %%" _ASM_DX ", %c[rdx](%0) \n\t"
9346 "mov %%" _ASM_SI ", %c[rsi](%0) \n\t"
9347 "mov %%" _ASM_DI ", %c[rdi](%0) \n\t"
9348 "mov %%" _ASM_BP ", %c[rbp](%0) \n\t"
Avi Kivity05b3e0c2006-12-13 00:33:45 -08009349#ifdef CONFIG_X86_64
Avi Kivitye08aa782007-11-15 18:06:18 +02009350 "mov %%r8, %c[r8](%0) \n\t"
9351 "mov %%r9, %c[r9](%0) \n\t"
9352 "mov %%r10, %c[r10](%0) \n\t"
9353 "mov %%r11, %c[r11](%0) \n\t"
9354 "mov %%r12, %c[r12](%0) \n\t"
9355 "mov %%r13, %c[r13](%0) \n\t"
9356 "mov %%r14, %c[r14](%0) \n\t"
9357 "mov %%r15, %c[r15](%0) \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08009358#endif
Avi Kivityb188c81f2012-09-16 15:10:58 +03009359 "mov %%cr2, %%" _ASM_AX " \n\t"
9360 "mov %%" _ASM_AX ", %c[cr2](%0) \n\t"
Avi Kivityc8019492008-07-14 14:44:59 +03009361
Avi Kivityb188c81f2012-09-16 15:10:58 +03009362 "pop %%" _ASM_BP "; pop %%" _ASM_DX " \n\t"
Avi Kivitye08aa782007-11-15 18:06:18 +02009363 "setbe %c[fail](%0) \n\t"
Avi Kivity83287ea422012-09-16 15:10:57 +03009364 ".pushsection .rodata \n\t"
9365 ".global vmx_return \n\t"
9366 "vmx_return: " _ASM_PTR " 2b \n\t"
9367 ".popsection"
Avi Kivitye08aa782007-11-15 18:06:18 +02009368 : : "c"(vmx), "d"((unsigned long)HOST_RSP),
Nadav Har'Eld462b812011-05-24 15:26:10 +03009369 [launched]"i"(offsetof(struct vcpu_vmx, __launched)),
Avi Kivitye08aa782007-11-15 18:06:18 +02009370 [fail]"i"(offsetof(struct vcpu_vmx, fail)),
Avi Kivity313dbd492008-07-17 18:04:30 +03009371 [host_rsp]"i"(offsetof(struct vcpu_vmx, host_rsp)),
Zhang Xiantaoad312c72007-12-13 23:50:52 +08009372 [rax]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RAX])),
9373 [rbx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBX])),
9374 [rcx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RCX])),
9375 [rdx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDX])),
9376 [rsi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RSI])),
9377 [rdi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDI])),
9378 [rbp]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBP])),
Avi Kivity05b3e0c2006-12-13 00:33:45 -08009379#ifdef CONFIG_X86_64
Zhang Xiantaoad312c72007-12-13 23:50:52 +08009380 [r8]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R8])),
9381 [r9]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R9])),
9382 [r10]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R10])),
9383 [r11]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R11])),
9384 [r12]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R12])),
9385 [r13]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R13])),
9386 [r14]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R14])),
9387 [r15]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R15])),
Avi Kivity6aa8b732006-12-10 02:21:36 -08009388#endif
Avi Kivity40712fa2011-01-06 18:09:12 +02009389 [cr2]"i"(offsetof(struct vcpu_vmx, vcpu.arch.cr2)),
9390 [wordsize]"i"(sizeof(ulong))
Laurent Vivierc2036302007-10-25 14:18:52 +02009391 : "cc", "memory"
9392#ifdef CONFIG_X86_64
Avi Kivityb188c81f2012-09-16 15:10:58 +03009393 , "rax", "rbx", "rdi", "rsi"
Laurent Vivierc2036302007-10-25 14:18:52 +02009394 , "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15"
Avi Kivityb188c81f2012-09-16 15:10:58 +03009395#else
9396 , "eax", "ebx", "edi", "esi"
Laurent Vivierc2036302007-10-25 14:18:52 +02009397#endif
9398 );
Avi Kivity6aa8b732006-12-10 02:21:36 -08009399
Gleb Natapov2a7921b2012-08-12 16:12:29 +03009400 /* MSR_IA32_DEBUGCTLMSR is zeroed on vmexit. Restore it if needed */
Wanpeng Li74c55932017-11-29 01:31:20 -08009401 if (vmx->host_debugctlmsr)
9402 update_debugctlmsr(vmx->host_debugctlmsr);
Gleb Natapov2a7921b2012-08-12 16:12:29 +03009403
Avi Kivityaa67f602012-08-01 16:48:03 +03009404#ifndef CONFIG_X86_64
9405 /*
9406 * The sysexit path does not restore ds/es, so we must set them to
9407 * a reasonable value ourselves.
9408 *
9409 * We can't defer this to vmx_load_host_state() since that function
9410 * may be executed in interrupt context, which saves and restore segments
9411 * around it, nullifying its effect.
9412 */
9413 loadsegment(ds, __USER_DS);
9414 loadsegment(es, __USER_DS);
9415#endif
9416
Avi Kivity6de4f3a2009-05-31 22:58:47 +03009417 vcpu->arch.regs_avail = ~((1 << VCPU_REGS_RIP) | (1 << VCPU_REGS_RSP)
Avi Kivity6de12732011-03-07 12:51:22 +02009418 | (1 << VCPU_EXREG_RFLAGS)
Avi Kivityaff48ba2010-12-05 18:56:11 +02009419 | (1 << VCPU_EXREG_PDPTR)
Avi Kivity2fb92db2011-04-27 19:42:18 +03009420 | (1 << VCPU_EXREG_SEGMENTS)
Avi Kivityaff48ba2010-12-05 18:56:11 +02009421 | (1 << VCPU_EXREG_CR3));
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03009422 vcpu->arch.regs_dirty = 0;
9423
Gleb Natapove0b890d2013-09-25 12:51:33 +03009424 /*
Xiao Guangrong1be0e612016-03-22 16:51:18 +08009425 * eager fpu is enabled if PKEY is supported and CR4 is switched
9426 * back on host, so it is safe to read guest PKRU from current
9427 * XSAVE.
9428 */
Paolo Bonzinib9dd21e2017-08-23 23:14:38 +02009429 if (static_cpu_has(X86_FEATURE_PKU) &&
9430 kvm_read_cr4_bits(vcpu, X86_CR4_PKE)) {
9431 vcpu->arch.pkru = __read_pkru();
9432 if (vcpu->arch.pkru != vmx->host_pkru)
Xiao Guangrong1be0e612016-03-22 16:51:18 +08009433 __write_pkru(vmx->host_pkru);
Xiao Guangrong1be0e612016-03-22 16:51:18 +08009434 }
9435
9436 /*
Gleb Natapove0b890d2013-09-25 12:51:33 +03009437 * the KVM_REQ_EVENT optimization bit is only on for one entry, and if
9438 * we did not inject a still-pending event to L1 now because of
9439 * nested_run_pending, we need to re-enable this bit.
9440 */
9441 if (vmx->nested.nested_run_pending)
9442 kvm_make_request(KVM_REQ_EVENT, vcpu);
9443
9444 vmx->nested.nested_run_pending = 0;
Jim Mattsonb060ca32017-09-14 16:31:42 -07009445 vmx->idt_vectoring_info = 0;
9446
9447 vmx->exit_reason = vmx->fail ? 0xdead : vmcs_read32(VM_EXIT_REASON);
9448 if (vmx->fail || (vmx->exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY))
9449 return;
9450
9451 vmx->loaded_vmcs->launched = 1;
9452 vmx->idt_vectoring_info = vmcs_read32(IDT_VECTORING_INFO_FIELD);
Gleb Natapove0b890d2013-09-25 12:51:33 +03009453
Avi Kivity51aa01d2010-07-20 14:31:20 +03009454 vmx_complete_atomic_exit(vmx);
9455 vmx_recover_nmi_blocking(vmx);
Avi Kivitycf393f72008-07-01 16:20:21 +03009456 vmx_complete_interrupts(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08009457}
Josh Poimboeufc207aee2017-06-28 10:11:06 -05009458STACK_FRAME_NON_STANDARD(vmx_vcpu_run);
Avi Kivity6aa8b732006-12-10 02:21:36 -08009459
David Hildenbrand1279a6b12017-03-20 10:00:08 +01009460static void vmx_switch_vmcs(struct kvm_vcpu *vcpu, struct loaded_vmcs *vmcs)
Paolo Bonzini4fa77342014-07-17 12:25:16 +02009461{
9462 struct vcpu_vmx *vmx = to_vmx(vcpu);
9463 int cpu;
9464
David Hildenbrand1279a6b12017-03-20 10:00:08 +01009465 if (vmx->loaded_vmcs == vmcs)
Paolo Bonzini4fa77342014-07-17 12:25:16 +02009466 return;
9467
9468 cpu = get_cpu();
David Hildenbrand1279a6b12017-03-20 10:00:08 +01009469 vmx->loaded_vmcs = vmcs;
Paolo Bonzini4fa77342014-07-17 12:25:16 +02009470 vmx_vcpu_put(vcpu);
9471 vmx_vcpu_load(vcpu, cpu);
Paolo Bonzini4fa77342014-07-17 12:25:16 +02009472 put_cpu();
9473}
9474
Jim Mattson2f1fe812016-07-08 15:36:06 -07009475/*
9476 * Ensure that the current vmcs of the logical processor is the
9477 * vmcs01 of the vcpu before calling free_nested().
9478 */
9479static void vmx_free_vcpu_nested(struct kvm_vcpu *vcpu)
9480{
9481 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jim Mattson2f1fe812016-07-08 15:36:06 -07009482
Christoffer Dallec7660c2017-12-04 21:35:23 +01009483 vcpu_load(vcpu);
David Hildenbrand1279a6b12017-03-20 10:00:08 +01009484 vmx_switch_vmcs(vcpu, &vmx->vmcs01);
Jim Mattson2f1fe812016-07-08 15:36:06 -07009485 free_nested(vmx);
9486 vcpu_put(vcpu);
9487}
9488
Avi Kivity6aa8b732006-12-10 02:21:36 -08009489static void vmx_free_vcpu(struct kvm_vcpu *vcpu)
9490{
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009491 struct vcpu_vmx *vmx = to_vmx(vcpu);
9492
Kai Huang843e4332015-01-28 10:54:28 +08009493 if (enable_pml)
Kai Huanga3eaa862015-11-04 13:46:05 +08009494 vmx_destroy_pml_buffer(vmx);
Wanpeng Li991e7a02015-09-16 17:30:05 +08009495 free_vpid(vmx->vpid);
Paolo Bonzini4fa77342014-07-17 12:25:16 +02009496 leave_guest_mode(vcpu);
Jim Mattson2f1fe812016-07-08 15:36:06 -07009497 vmx_free_vcpu_nested(vcpu);
Paolo Bonzini4fa77342014-07-17 12:25:16 +02009498 free_loaded_vmcs(vmx->loaded_vmcs);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009499 kfree(vmx->guest_msrs);
9500 kvm_vcpu_uninit(vcpu);
Rusty Russella4770342007-08-01 14:46:11 +10009501 kmem_cache_free(kvm_vcpu_cache, vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08009502}
9503
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009504static struct kvm_vcpu *vmx_create_vcpu(struct kvm *kvm, unsigned int id)
Avi Kivity6aa8b732006-12-10 02:21:36 -08009505{
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009506 int err;
Rusty Russellc16f8622007-07-30 21:12:19 +10009507 struct vcpu_vmx *vmx = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
Avi Kivity15ad7142007-07-11 18:17:21 +03009508 int cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08009509
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04009510 if (!vmx)
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009511 return ERR_PTR(-ENOMEM);
9512
Wanpeng Li991e7a02015-09-16 17:30:05 +08009513 vmx->vpid = allocate_vpid();
Sheng Yang2384d2b2008-01-17 15:14:33 +08009514
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009515 err = kvm_vcpu_init(&vmx->vcpu, kvm, id);
9516 if (err)
9517 goto free_vcpu;
Ingo Molnar965b58a2007-01-05 16:36:23 -08009518
Peter Feiner4e595162016-07-07 14:49:58 -07009519 err = -ENOMEM;
9520
9521 /*
9522 * If PML is turned on, failure on enabling PML just results in failure
9523 * of creating the vcpu, therefore we can simplify PML logic (by
9524 * avoiding dealing with cases, such as enabling PML partially on vcpus
9525 * for the guest, etc.
9526 */
9527 if (enable_pml) {
9528 vmx->pml_pg = alloc_page(GFP_KERNEL | __GFP_ZERO);
9529 if (!vmx->pml_pg)
9530 goto uninit_vcpu;
9531 }
9532
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04009533 vmx->guest_msrs = kmalloc(PAGE_SIZE, GFP_KERNEL);
Paolo Bonzini03916db2014-07-24 14:21:57 +02009534 BUILD_BUG_ON(ARRAY_SIZE(vmx_msr_index) * sizeof(vmx->guest_msrs[0])
9535 > PAGE_SIZE);
Nadav Amit0123be42014-07-24 15:06:56 +03009536
Peter Feiner4e595162016-07-07 14:49:58 -07009537 if (!vmx->guest_msrs)
9538 goto free_pml;
Ingo Molnar965b58a2007-01-05 16:36:23 -08009539
Nadav Har'Eld462b812011-05-24 15:26:10 +03009540 vmx->loaded_vmcs = &vmx->vmcs01;
9541 vmx->loaded_vmcs->vmcs = alloc_vmcs();
Jim Mattson355f4fb2016-10-28 08:29:39 -07009542 vmx->loaded_vmcs->shadow_vmcs = NULL;
Nadav Har'Eld462b812011-05-24 15:26:10 +03009543 if (!vmx->loaded_vmcs->vmcs)
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009544 goto free_msrs;
Nadav Har'Eld462b812011-05-24 15:26:10 +03009545 loaded_vmcs_init(vmx->loaded_vmcs);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04009546
Avi Kivity15ad7142007-07-11 18:17:21 +03009547 cpu = get_cpu();
9548 vmx_vcpu_load(&vmx->vcpu, cpu);
Zachary Amsdene48672f2010-08-19 22:07:23 -10009549 vmx->vcpu.cpu = cpu;
David Hildenbrand12d79912017-08-24 20:51:26 +02009550 vmx_vcpu_setup(vmx);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009551 vmx_vcpu_put(&vmx->vcpu);
Avi Kivity15ad7142007-07-11 18:17:21 +03009552 put_cpu();
Paolo Bonzini35754c92015-07-29 12:05:37 +02009553 if (cpu_need_virtualize_apic_accesses(&vmx->vcpu)) {
Jan Kiszkabe6d05c2011-04-13 01:27:55 +02009554 err = alloc_apic_access_page(kvm);
9555 if (err)
Marcelo Tosatti5e4a0b32008-02-14 21:21:43 -02009556 goto free_vmcs;
Jan Kiszkaa63cb562013-04-08 11:07:46 +02009557 }
Ingo Molnar965b58a2007-01-05 16:36:23 -08009558
Sheng Yangb927a3c2009-07-21 10:42:48 +08009559 if (enable_ept) {
Tang Chenf51770e2014-09-16 18:41:59 +08009560 err = init_rmode_identity_map(kvm);
9561 if (err)
Gleb Natapov93ea5382011-02-21 12:07:59 +02009562 goto free_vmcs;
Sheng Yangb927a3c2009-07-21 10:42:48 +08009563 }
Sheng Yangb7ebfb02008-04-25 21:44:52 +08009564
Wanpeng Li5c614b32015-10-13 09:18:36 -07009565 if (nested) {
Wincy Vanb9c237b2015-02-03 23:56:30 +08009566 nested_vmx_setup_ctls_msrs(vmx);
Wanpeng Li5c614b32015-10-13 09:18:36 -07009567 vmx->nested.vpid02 = allocate_vpid();
9568 }
Wincy Vanb9c237b2015-02-03 23:56:30 +08009569
Wincy Van705699a2015-02-03 23:58:17 +08009570 vmx->nested.posted_intr_nv = -1;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +03009571 vmx->nested.current_vmptr = -1ull;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +03009572
Haozhong Zhang37e4c992016-06-22 14:59:55 +08009573 vmx->msr_ia32_feature_control_valid_bits = FEATURE_CONTROL_LOCKED;
9574
Paolo Bonzini31afb2e2017-06-06 12:57:06 +02009575 /*
9576 * Enforce invariant: pi_desc.nv is always either POSTED_INTR_VECTOR
9577 * or POSTED_INTR_WAKEUP_VECTOR.
9578 */
9579 vmx->pi_desc.nv = POSTED_INTR_VECTOR;
9580 vmx->pi_desc.sn = 1;
9581
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009582 return &vmx->vcpu;
Ingo Molnar965b58a2007-01-05 16:36:23 -08009583
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009584free_vmcs:
Wanpeng Li5c614b32015-10-13 09:18:36 -07009585 free_vpid(vmx->nested.vpid02);
Xiao Guangrong5f3fbc32012-05-14 14:58:58 +08009586 free_loaded_vmcs(vmx->loaded_vmcs);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009587free_msrs:
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009588 kfree(vmx->guest_msrs);
Peter Feiner4e595162016-07-07 14:49:58 -07009589free_pml:
9590 vmx_destroy_pml_buffer(vmx);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009591uninit_vcpu:
9592 kvm_vcpu_uninit(&vmx->vcpu);
9593free_vcpu:
Wanpeng Li991e7a02015-09-16 17:30:05 +08009594 free_vpid(vmx->vpid);
Rusty Russella4770342007-08-01 14:46:11 +10009595 kmem_cache_free(kvm_vcpu_cache, vmx);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009596 return ERR_PTR(err);
Avi Kivity6aa8b732006-12-10 02:21:36 -08009597}
9598
Yang, Sheng002c7f72007-07-31 14:23:01 +03009599static void __init vmx_check_processor_compat(void *rtn)
9600{
9601 struct vmcs_config vmcs_conf;
9602
9603 *(int *)rtn = 0;
9604 if (setup_vmcs_config(&vmcs_conf) < 0)
9605 *(int *)rtn = -EIO;
9606 if (memcmp(&vmcs_config, &vmcs_conf, sizeof(struct vmcs_config)) != 0) {
9607 printk(KERN_ERR "kvm: CPU %d feature inconsistency!\n",
9608 smp_processor_id());
9609 *(int *)rtn = -EIO;
9610 }
9611}
9612
Sheng Yang4b12f0d2009-04-27 20:35:42 +08009613static u64 vmx_get_mt_mask(struct kvm_vcpu *vcpu, gfn_t gfn, bool is_mmio)
Sheng Yang64d4d522008-10-09 16:01:57 +08009614{
Xiao Guangrongb18d5432015-06-15 16:55:21 +08009615 u8 cache;
9616 u64 ipat = 0;
Sheng Yang4b12f0d2009-04-27 20:35:42 +08009617
Sheng Yang522c68c2009-04-27 20:35:43 +08009618 /* For VT-d and EPT combination
Paolo Bonzini606decd2015-10-01 13:12:47 +02009619 * 1. MMIO: always map as UC
Sheng Yang522c68c2009-04-27 20:35:43 +08009620 * 2. EPT with VT-d:
9621 * a. VT-d without snooping control feature: can't guarantee the
Paolo Bonzini606decd2015-10-01 13:12:47 +02009622 * result, try to trust guest.
Sheng Yang522c68c2009-04-27 20:35:43 +08009623 * b. VT-d with snooping control feature: snooping control feature of
9624 * VT-d engine can guarantee the cache correctness. Just set it
9625 * to WB to keep consistent with host. So the same as item 3.
Sheng Yanga19a6d12010-02-09 16:41:53 +08009626 * 3. EPT without VT-d: always map as WB and set IPAT=1 to keep
Sheng Yang522c68c2009-04-27 20:35:43 +08009627 * consistent with host MTRR
9628 */
Paolo Bonzini606decd2015-10-01 13:12:47 +02009629 if (is_mmio) {
9630 cache = MTRR_TYPE_UNCACHABLE;
9631 goto exit;
9632 }
9633
9634 if (!kvm_arch_has_noncoherent_dma(vcpu->kvm)) {
Xiao Guangrongb18d5432015-06-15 16:55:21 +08009635 ipat = VMX_EPT_IPAT_BIT;
9636 cache = MTRR_TYPE_WRBACK;
9637 goto exit;
9638 }
9639
9640 if (kvm_read_cr0(vcpu) & X86_CR0_CD) {
9641 ipat = VMX_EPT_IPAT_BIT;
Paolo Bonzini0da029e2015-07-23 08:24:42 +02009642 if (kvm_check_has_quirk(vcpu->kvm, KVM_X86_QUIRK_CD_NW_CLEARED))
Xiao Guangrongfb2799502015-07-16 03:25:56 +08009643 cache = MTRR_TYPE_WRBACK;
9644 else
9645 cache = MTRR_TYPE_UNCACHABLE;
Xiao Guangrongb18d5432015-06-15 16:55:21 +08009646 goto exit;
9647 }
9648
Xiao Guangrongff536042015-06-15 16:55:22 +08009649 cache = kvm_mtrr_get_guest_memory_type(vcpu, gfn);
Xiao Guangrongb18d5432015-06-15 16:55:21 +08009650
9651exit:
9652 return (cache << VMX_EPT_MT_EPTE_SHIFT) | ipat;
Sheng Yang64d4d522008-10-09 16:01:57 +08009653}
9654
Sheng Yang17cc3932010-01-05 19:02:27 +08009655static int vmx_get_lpage_level(void)
Joerg Roedel344f4142009-07-27 16:30:48 +02009656{
Sheng Yang878403b2010-01-05 19:02:29 +08009657 if (enable_ept && !cpu_has_vmx_ept_1g_page())
9658 return PT_DIRECTORY_LEVEL;
9659 else
9660 /* For shadow and EPT supported 1GB page */
9661 return PT_PDPE_LEVEL;
Joerg Roedel344f4142009-07-27 16:30:48 +02009662}
9663
Xiao Guangrongfeda8052015-09-09 14:05:55 +08009664static void vmcs_set_secondary_exec_control(u32 new_ctl)
9665{
9666 /*
9667 * These bits in the secondary execution controls field
9668 * are dynamic, the others are mostly based on the hypervisor
9669 * architecture and the guest's CPUID. Do not touch the
9670 * dynamic bits.
9671 */
9672 u32 mask =
9673 SECONDARY_EXEC_SHADOW_VMCS |
9674 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
Paolo Bonzini0367f202016-07-12 10:44:55 +02009675 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
9676 SECONDARY_EXEC_DESC;
Xiao Guangrongfeda8052015-09-09 14:05:55 +08009677
9678 u32 cur_ctl = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
9679
9680 vmcs_write32(SECONDARY_VM_EXEC_CONTROL,
9681 (new_ctl & ~mask) | (cur_ctl & mask));
9682}
9683
David Matlack8322ebb2016-11-29 18:14:09 -08009684/*
9685 * Generate MSR_IA32_VMX_CR{0,4}_FIXED1 according to CPUID. Only set bits
9686 * (indicating "allowed-1") if they are supported in the guest's CPUID.
9687 */
9688static void nested_vmx_cr_fixed1_bits_update(struct kvm_vcpu *vcpu)
9689{
9690 struct vcpu_vmx *vmx = to_vmx(vcpu);
9691 struct kvm_cpuid_entry2 *entry;
9692
9693 vmx->nested.nested_vmx_cr0_fixed1 = 0xffffffff;
9694 vmx->nested.nested_vmx_cr4_fixed1 = X86_CR4_PCE;
9695
9696#define cr4_fixed1_update(_cr4_mask, _reg, _cpuid_mask) do { \
9697 if (entry && (entry->_reg & (_cpuid_mask))) \
9698 vmx->nested.nested_vmx_cr4_fixed1 |= (_cr4_mask); \
9699} while (0)
9700
9701 entry = kvm_find_cpuid_entry(vcpu, 0x1, 0);
9702 cr4_fixed1_update(X86_CR4_VME, edx, bit(X86_FEATURE_VME));
9703 cr4_fixed1_update(X86_CR4_PVI, edx, bit(X86_FEATURE_VME));
9704 cr4_fixed1_update(X86_CR4_TSD, edx, bit(X86_FEATURE_TSC));
9705 cr4_fixed1_update(X86_CR4_DE, edx, bit(X86_FEATURE_DE));
9706 cr4_fixed1_update(X86_CR4_PSE, edx, bit(X86_FEATURE_PSE));
9707 cr4_fixed1_update(X86_CR4_PAE, edx, bit(X86_FEATURE_PAE));
9708 cr4_fixed1_update(X86_CR4_MCE, edx, bit(X86_FEATURE_MCE));
9709 cr4_fixed1_update(X86_CR4_PGE, edx, bit(X86_FEATURE_PGE));
9710 cr4_fixed1_update(X86_CR4_OSFXSR, edx, bit(X86_FEATURE_FXSR));
9711 cr4_fixed1_update(X86_CR4_OSXMMEXCPT, edx, bit(X86_FEATURE_XMM));
9712 cr4_fixed1_update(X86_CR4_VMXE, ecx, bit(X86_FEATURE_VMX));
9713 cr4_fixed1_update(X86_CR4_SMXE, ecx, bit(X86_FEATURE_SMX));
9714 cr4_fixed1_update(X86_CR4_PCIDE, ecx, bit(X86_FEATURE_PCID));
9715 cr4_fixed1_update(X86_CR4_OSXSAVE, ecx, bit(X86_FEATURE_XSAVE));
9716
9717 entry = kvm_find_cpuid_entry(vcpu, 0x7, 0);
9718 cr4_fixed1_update(X86_CR4_FSGSBASE, ebx, bit(X86_FEATURE_FSGSBASE));
9719 cr4_fixed1_update(X86_CR4_SMEP, ebx, bit(X86_FEATURE_SMEP));
9720 cr4_fixed1_update(X86_CR4_SMAP, ebx, bit(X86_FEATURE_SMAP));
9721 cr4_fixed1_update(X86_CR4_PKE, ecx, bit(X86_FEATURE_PKU));
Paolo Bonzinic4ad77e2017-11-13 14:23:59 +01009722 cr4_fixed1_update(X86_CR4_UMIP, ecx, bit(X86_FEATURE_UMIP));
David Matlack8322ebb2016-11-29 18:14:09 -08009723
9724#undef cr4_fixed1_update
9725}
9726
Sheng Yang0e851882009-12-18 16:48:46 +08009727static void vmx_cpuid_update(struct kvm_vcpu *vcpu)
9728{
Sheng Yang4e47c7a2009-12-18 16:48:47 +08009729 struct vcpu_vmx *vmx = to_vmx(vcpu);
Sheng Yang4e47c7a2009-12-18 16:48:47 +08009730
Paolo Bonzini80154d72017-08-24 13:55:35 +02009731 if (cpu_has_secondary_exec_ctrls()) {
9732 vmx_compute_secondary_exec_control(vmx);
9733 vmcs_set_secondary_exec_control(vmx->secondary_exec_control);
Sheng Yang4e47c7a2009-12-18 16:48:47 +08009734 }
Mao, Junjiead756a12012-07-02 01:18:48 +00009735
Haozhong Zhang37e4c992016-06-22 14:59:55 +08009736 if (nested_vmx_allowed(vcpu))
9737 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits |=
9738 FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
9739 else
9740 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits &=
9741 ~FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
David Matlack8322ebb2016-11-29 18:14:09 -08009742
9743 if (nested_vmx_allowed(vcpu))
9744 nested_vmx_cr_fixed1_bits_update(vcpu);
Sheng Yang0e851882009-12-18 16:48:46 +08009745}
9746
Joerg Roedeld4330ef2010-04-22 12:33:11 +02009747static void vmx_set_supported_cpuid(u32 func, struct kvm_cpuid_entry2 *entry)
9748{
Nadav Har'El7b8050f2011-05-25 23:16:10 +03009749 if (func == 1 && nested)
9750 entry->ecx |= bit(X86_FEATURE_VMX);
Joerg Roedeld4330ef2010-04-22 12:33:11 +02009751}
9752
Yang Zhang25d92082013-08-06 12:00:32 +03009753static void nested_ept_inject_page_fault(struct kvm_vcpu *vcpu,
9754 struct x86_exception *fault)
9755{
Jan Kiszka533558b2014-01-04 18:47:20 +01009756 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Bandan Dasc5f983f2017-05-05 15:25:14 -04009757 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jan Kiszka533558b2014-01-04 18:47:20 +01009758 u32 exit_reason;
Bandan Dasc5f983f2017-05-05 15:25:14 -04009759 unsigned long exit_qualification = vcpu->arch.exit_qualification;
Yang Zhang25d92082013-08-06 12:00:32 +03009760
Bandan Dasc5f983f2017-05-05 15:25:14 -04009761 if (vmx->nested.pml_full) {
9762 exit_reason = EXIT_REASON_PML_FULL;
9763 vmx->nested.pml_full = false;
9764 exit_qualification &= INTR_INFO_UNBLOCK_NMI;
9765 } else if (fault->error_code & PFERR_RSVD_MASK)
Jan Kiszka533558b2014-01-04 18:47:20 +01009766 exit_reason = EXIT_REASON_EPT_MISCONFIG;
Yang Zhang25d92082013-08-06 12:00:32 +03009767 else
Jan Kiszka533558b2014-01-04 18:47:20 +01009768 exit_reason = EXIT_REASON_EPT_VIOLATION;
Bandan Dasc5f983f2017-05-05 15:25:14 -04009769
9770 nested_vmx_vmexit(vcpu, exit_reason, 0, exit_qualification);
Yang Zhang25d92082013-08-06 12:00:32 +03009771 vmcs12->guest_physical_address = fault->address;
9772}
9773
Peter Feiner995f00a2017-06-30 17:26:32 -07009774static bool nested_ept_ad_enabled(struct kvm_vcpu *vcpu)
9775{
David Hildenbrandbb97a012017-08-10 23:15:28 +02009776 return nested_ept_get_cr3(vcpu) & VMX_EPTP_AD_ENABLE_BIT;
Peter Feiner995f00a2017-06-30 17:26:32 -07009777}
9778
Nadav Har'El155a97a2013-08-05 11:07:16 +03009779/* Callbacks for nested_ept_init_mmu_context: */
9780
9781static unsigned long nested_ept_get_cr3(struct kvm_vcpu *vcpu)
9782{
9783 /* return the page table to be shadowed - in our case, EPT12 */
9784 return get_vmcs12(vcpu)->ept_pointer;
9785}
9786
Paolo Bonziniae1e2d12017-03-30 11:55:30 +02009787static int nested_ept_init_mmu_context(struct kvm_vcpu *vcpu)
Nadav Har'El155a97a2013-08-05 11:07:16 +03009788{
Paolo Bonziniad896af2013-10-02 16:56:14 +02009789 WARN_ON(mmu_is_nested(vcpu));
David Hildenbranda057e0e2017-08-10 23:36:54 +02009790 if (!valid_ept_address(vcpu, nested_ept_get_cr3(vcpu)))
Paolo Bonziniae1e2d12017-03-30 11:55:30 +02009791 return 1;
9792
9793 kvm_mmu_unload(vcpu);
Paolo Bonziniad896af2013-10-02 16:56:14 +02009794 kvm_init_shadow_ept_mmu(vcpu,
Wincy Vanb9c237b2015-02-03 23:56:30 +08009795 to_vmx(vcpu)->nested.nested_vmx_ept_caps &
Paolo Bonziniae1e2d12017-03-30 11:55:30 +02009796 VMX_EPT_EXECUTE_ONLY_BIT,
David Hildenbranda057e0e2017-08-10 23:36:54 +02009797 nested_ept_ad_enabled(vcpu));
Nadav Har'El155a97a2013-08-05 11:07:16 +03009798 vcpu->arch.mmu.set_cr3 = vmx_set_cr3;
9799 vcpu->arch.mmu.get_cr3 = nested_ept_get_cr3;
9800 vcpu->arch.mmu.inject_page_fault = nested_ept_inject_page_fault;
9801
9802 vcpu->arch.walk_mmu = &vcpu->arch.nested_mmu;
Paolo Bonziniae1e2d12017-03-30 11:55:30 +02009803 return 0;
Nadav Har'El155a97a2013-08-05 11:07:16 +03009804}
9805
9806static void nested_ept_uninit_mmu_context(struct kvm_vcpu *vcpu)
9807{
9808 vcpu->arch.walk_mmu = &vcpu->arch.mmu;
9809}
9810
Eugene Korenevsky19d5f102014-12-16 22:35:53 +03009811static bool nested_vmx_is_page_fault_vmexit(struct vmcs12 *vmcs12,
9812 u16 error_code)
9813{
9814 bool inequality, bit;
9815
9816 bit = (vmcs12->exception_bitmap & (1u << PF_VECTOR)) != 0;
9817 inequality =
9818 (error_code & vmcs12->page_fault_error_code_mask) !=
9819 vmcs12->page_fault_error_code_match;
9820 return inequality ^ bit;
9821}
9822
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +03009823static void vmx_inject_page_fault_nested(struct kvm_vcpu *vcpu,
9824 struct x86_exception *fault)
9825{
9826 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
9827
9828 WARN_ON(!is_guest_mode(vcpu));
9829
Wanpeng Li305d0ab2017-09-28 18:16:44 -07009830 if (nested_vmx_is_page_fault_vmexit(vmcs12, fault->error_code) &&
9831 !to_vmx(vcpu)->nested.nested_run_pending) {
Paolo Bonzinib96fb432017-07-27 12:29:32 +02009832 vmcs12->vm_exit_intr_error_code = fault->error_code;
9833 nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI,
9834 PF_VECTOR | INTR_TYPE_HARD_EXCEPTION |
9835 INTR_INFO_DELIVER_CODE_MASK | INTR_INFO_VALID_MASK,
9836 fault->address);
Paolo Bonzini7313c692017-07-27 10:31:25 +02009837 } else {
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +03009838 kvm_inject_page_fault(vcpu, fault);
Paolo Bonzini7313c692017-07-27 10:31:25 +02009839 }
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +03009840}
9841
Paolo Bonzinic9923842017-12-13 14:16:30 +01009842static inline bool nested_vmx_prepare_msr_bitmap(struct kvm_vcpu *vcpu,
9843 struct vmcs12 *vmcs12);
Jim Mattson6beb7bd2016-11-30 12:03:45 -08009844
9845static void nested_get_vmcs12_pages(struct kvm_vcpu *vcpu,
Wanpeng Lia2bcba52014-08-21 19:46:49 +08009846 struct vmcs12 *vmcs12)
9847{
9848 struct vcpu_vmx *vmx = to_vmx(vcpu);
David Hildenbrand5e2f30b2017-08-03 18:11:04 +02009849 struct page *page;
Jim Mattson6beb7bd2016-11-30 12:03:45 -08009850 u64 hpa;
Wanpeng Lia2bcba52014-08-21 19:46:49 +08009851
9852 if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) {
Wanpeng Lia2bcba52014-08-21 19:46:49 +08009853 /*
9854 * Translate L1 physical address to host physical
9855 * address for vmcs02. Keep the page pinned, so this
9856 * physical address remains valid. We keep a reference
9857 * to it so we can release it later.
9858 */
David Hildenbrand5e2f30b2017-08-03 18:11:04 +02009859 if (vmx->nested.apic_access_page) { /* shouldn't happen */
David Hildenbrand53a70da2017-08-03 18:11:05 +02009860 kvm_release_page_dirty(vmx->nested.apic_access_page);
David Hildenbrand5e2f30b2017-08-03 18:11:04 +02009861 vmx->nested.apic_access_page = NULL;
9862 }
9863 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->apic_access_addr);
Jim Mattson6beb7bd2016-11-30 12:03:45 -08009864 /*
9865 * If translation failed, no matter: This feature asks
9866 * to exit when accessing the given address, and if it
9867 * can never be accessed, this feature won't do
9868 * anything anyway.
9869 */
David Hildenbrand5e2f30b2017-08-03 18:11:04 +02009870 if (!is_error_page(page)) {
9871 vmx->nested.apic_access_page = page;
Jim Mattson6beb7bd2016-11-30 12:03:45 -08009872 hpa = page_to_phys(vmx->nested.apic_access_page);
9873 vmcs_write64(APIC_ACCESS_ADDR, hpa);
9874 } else {
9875 vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL,
9876 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES);
9877 }
9878 } else if (!(nested_cpu_has_virt_x2apic_mode(vmcs12)) &&
9879 cpu_need_virtualize_apic_accesses(&vmx->vcpu)) {
9880 vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL,
9881 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES);
9882 kvm_vcpu_reload_apic_access_page(vcpu);
Wanpeng Lia2bcba52014-08-21 19:46:49 +08009883 }
Wanpeng Lia7c0b072014-08-21 19:46:50 +08009884
9885 if (nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW)) {
David Hildenbrand5e2f30b2017-08-03 18:11:04 +02009886 if (vmx->nested.virtual_apic_page) { /* shouldn't happen */
David Hildenbrand53a70da2017-08-03 18:11:05 +02009887 kvm_release_page_dirty(vmx->nested.virtual_apic_page);
David Hildenbrand5e2f30b2017-08-03 18:11:04 +02009888 vmx->nested.virtual_apic_page = NULL;
9889 }
9890 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->virtual_apic_page_addr);
Wanpeng Lia7c0b072014-08-21 19:46:50 +08009891
9892 /*
Jim Mattson6beb7bd2016-11-30 12:03:45 -08009893 * If translation failed, VM entry will fail because
9894 * prepare_vmcs02 set VIRTUAL_APIC_PAGE_ADDR to -1ull.
9895 * Failing the vm entry is _not_ what the processor
9896 * does but it's basically the only possibility we
9897 * have. We could still enter the guest if CR8 load
9898 * exits are enabled, CR8 store exits are enabled, and
9899 * virtualize APIC access is disabled; in this case
9900 * the processor would never use the TPR shadow and we
9901 * could simply clear the bit from the execution
9902 * control. But such a configuration is useless, so
9903 * let's keep the code simple.
Wanpeng Lia7c0b072014-08-21 19:46:50 +08009904 */
David Hildenbrand5e2f30b2017-08-03 18:11:04 +02009905 if (!is_error_page(page)) {
9906 vmx->nested.virtual_apic_page = page;
Jim Mattson6beb7bd2016-11-30 12:03:45 -08009907 hpa = page_to_phys(vmx->nested.virtual_apic_page);
9908 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, hpa);
9909 }
Wanpeng Lia7c0b072014-08-21 19:46:50 +08009910 }
9911
Wincy Van705699a2015-02-03 23:58:17 +08009912 if (nested_cpu_has_posted_intr(vmcs12)) {
Wincy Van705699a2015-02-03 23:58:17 +08009913 if (vmx->nested.pi_desc_page) { /* shouldn't happen */
9914 kunmap(vmx->nested.pi_desc_page);
David Hildenbrand53a70da2017-08-03 18:11:05 +02009915 kvm_release_page_dirty(vmx->nested.pi_desc_page);
David Hildenbrand5e2f30b2017-08-03 18:11:04 +02009916 vmx->nested.pi_desc_page = NULL;
Wincy Van705699a2015-02-03 23:58:17 +08009917 }
David Hildenbrand5e2f30b2017-08-03 18:11:04 +02009918 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->posted_intr_desc_addr);
9919 if (is_error_page(page))
Jim Mattson6beb7bd2016-11-30 12:03:45 -08009920 return;
David Hildenbrand5e2f30b2017-08-03 18:11:04 +02009921 vmx->nested.pi_desc_page = page;
9922 vmx->nested.pi_desc = kmap(vmx->nested.pi_desc_page);
Wincy Van705699a2015-02-03 23:58:17 +08009923 vmx->nested.pi_desc =
9924 (struct pi_desc *)((void *)vmx->nested.pi_desc +
9925 (unsigned long)(vmcs12->posted_intr_desc_addr &
9926 (PAGE_SIZE - 1)));
Jim Mattson6beb7bd2016-11-30 12:03:45 -08009927 vmcs_write64(POSTED_INTR_DESC_ADDR,
9928 page_to_phys(vmx->nested.pi_desc_page) +
9929 (unsigned long)(vmcs12->posted_intr_desc_addr &
9930 (PAGE_SIZE - 1)));
Wincy Van705699a2015-02-03 23:58:17 +08009931 }
Paolo Bonzinic9923842017-12-13 14:16:30 +01009932 if (!nested_vmx_prepare_msr_bitmap(vcpu, vmcs12))
Jim Mattson6beb7bd2016-11-30 12:03:45 -08009933 vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL,
9934 CPU_BASED_USE_MSR_BITMAPS);
Wanpeng Lia2bcba52014-08-21 19:46:49 +08009935}
9936
Jan Kiszkaf41245002014-03-07 20:03:13 +01009937static void vmx_start_preemption_timer(struct kvm_vcpu *vcpu)
9938{
9939 u64 preemption_timeout = get_vmcs12(vcpu)->vmx_preemption_timer_value;
9940 struct vcpu_vmx *vmx = to_vmx(vcpu);
9941
9942 if (vcpu->arch.virtual_tsc_khz == 0)
9943 return;
9944
9945 /* Make sure short timeouts reliably trigger an immediate vmexit.
9946 * hrtimer_start does not guarantee this. */
9947 if (preemption_timeout <= 1) {
9948 vmx_preemption_timer_fn(&vmx->nested.preemption_timer);
9949 return;
9950 }
9951
9952 preemption_timeout <<= VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE;
9953 preemption_timeout *= 1000000;
9954 do_div(preemption_timeout, vcpu->arch.virtual_tsc_khz);
9955 hrtimer_start(&vmx->nested.preemption_timer,
9956 ns_to_ktime(preemption_timeout), HRTIMER_MODE_REL);
9957}
9958
Jim Mattson56a20512017-07-06 16:33:06 -07009959static int nested_vmx_check_io_bitmap_controls(struct kvm_vcpu *vcpu,
9960 struct vmcs12 *vmcs12)
9961{
9962 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_IO_BITMAPS))
9963 return 0;
9964
9965 if (!page_address_valid(vcpu, vmcs12->io_bitmap_a) ||
9966 !page_address_valid(vcpu, vmcs12->io_bitmap_b))
9967 return -EINVAL;
9968
9969 return 0;
9970}
9971
Wincy Van3af18d92015-02-03 23:49:31 +08009972static int nested_vmx_check_msr_bitmap_controls(struct kvm_vcpu *vcpu,
9973 struct vmcs12 *vmcs12)
9974{
Wincy Van3af18d92015-02-03 23:49:31 +08009975 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS))
9976 return 0;
9977
Jim Mattson5fa99cb2017-07-06 16:33:07 -07009978 if (!page_address_valid(vcpu, vmcs12->msr_bitmap))
Wincy Van3af18d92015-02-03 23:49:31 +08009979 return -EINVAL;
9980
9981 return 0;
9982}
9983
Jim Mattson712b12d2017-08-24 13:24:47 -07009984static int nested_vmx_check_tpr_shadow_controls(struct kvm_vcpu *vcpu,
9985 struct vmcs12 *vmcs12)
9986{
9987 if (!nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW))
9988 return 0;
9989
9990 if (!page_address_valid(vcpu, vmcs12->virtual_apic_page_addr))
9991 return -EINVAL;
9992
9993 return 0;
9994}
9995
Wincy Van3af18d92015-02-03 23:49:31 +08009996/*
9997 * Merge L0's and L1's MSR bitmap, return false to indicate that
9998 * we do not use the hardware.
9999 */
Paolo Bonzinic9923842017-12-13 14:16:30 +010010000static inline bool nested_vmx_prepare_msr_bitmap(struct kvm_vcpu *vcpu,
10001 struct vmcs12 *vmcs12)
Wincy Van3af18d92015-02-03 23:49:31 +080010002{
Wincy Van82f0dd42015-02-03 23:57:18 +080010003 int msr;
Wincy Vanf2b93282015-02-03 23:56:03 +080010004 struct page *page;
Radim Krčmářd048c092016-08-08 20:16:22 +020010005 unsigned long *msr_bitmap_l1;
10006 unsigned long *msr_bitmap_l0 = to_vmx(vcpu)->nested.msr_bitmap;
Wincy Vanf2b93282015-02-03 23:56:03 +080010007
Paolo Bonzinic9923842017-12-13 14:16:30 +010010008 /* Nothing to do if the MSR bitmap is not in use. */
10009 if (!cpu_has_vmx_msr_bitmap() ||
10010 !nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS))
10011 return false;
10012
Radim Krčmářd048c092016-08-08 20:16:22 +020010013 /* This shortcut is ok because we support only x2APIC MSRs so far. */
Wincy Vanf2b93282015-02-03 23:56:03 +080010014 if (!nested_cpu_has_virt_x2apic_mode(vmcs12))
10015 return false;
10016
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020010017 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->msr_bitmap);
10018 if (is_error_page(page))
Wincy Vanf2b93282015-02-03 23:56:03 +080010019 return false;
Paolo Bonzinic9923842017-12-13 14:16:30 +010010020
Radim Krčmářd048c092016-08-08 20:16:22 +020010021 msr_bitmap_l1 = (unsigned long *)kmap(page);
Paolo Bonzinic9923842017-12-13 14:16:30 +010010022 if (nested_cpu_has_apic_reg_virt(vmcs12)) {
10023 /*
10024 * L0 need not intercept reads for MSRs between 0x800 and 0x8ff, it
10025 * just lets the processor take the value from the virtual-APIC page;
10026 * take those 256 bits directly from the L1 bitmap.
10027 */
10028 for (msr = 0x800; msr <= 0x8ff; msr += BITS_PER_LONG) {
10029 unsigned word = msr / BITS_PER_LONG;
10030 msr_bitmap_l0[word] = msr_bitmap_l1[word];
10031 msr_bitmap_l0[word + (0x800 / sizeof(long))] = ~0;
Wincy Van608406e2015-02-03 23:57:51 +080010032 }
Paolo Bonzinic9923842017-12-13 14:16:30 +010010033 } else {
10034 for (msr = 0x800; msr <= 0x8ff; msr += BITS_PER_LONG) {
10035 unsigned word = msr / BITS_PER_LONG;
10036 msr_bitmap_l0[word] = ~0;
10037 msr_bitmap_l0[word + (0x800 / sizeof(long))] = ~0;
10038 }
10039 }
10040
10041 nested_vmx_disable_intercept_for_msr(
10042 msr_bitmap_l1, msr_bitmap_l0,
Paolo Bonzinid7231e72017-12-21 00:47:55 +010010043 X2APIC_MSR(APIC_TASKPRI),
Paolo Bonzinic9923842017-12-13 14:16:30 +010010044 MSR_TYPE_W);
10045
10046 if (nested_cpu_has_vid(vmcs12)) {
10047 nested_vmx_disable_intercept_for_msr(
10048 msr_bitmap_l1, msr_bitmap_l0,
Paolo Bonzinid7231e72017-12-21 00:47:55 +010010049 X2APIC_MSR(APIC_EOI),
Paolo Bonzinic9923842017-12-13 14:16:30 +010010050 MSR_TYPE_W);
10051 nested_vmx_disable_intercept_for_msr(
10052 msr_bitmap_l1, msr_bitmap_l0,
Paolo Bonzinid7231e72017-12-21 00:47:55 +010010053 X2APIC_MSR(APIC_SELF_IPI),
Paolo Bonzinic9923842017-12-13 14:16:30 +010010054 MSR_TYPE_W);
Wincy Van82f0dd42015-02-03 23:57:18 +080010055 }
Wincy Vanf2b93282015-02-03 23:56:03 +080010056 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +020010057 kvm_release_page_clean(page);
Wincy Vanf2b93282015-02-03 23:56:03 +080010058
10059 return true;
10060}
10061
10062static int nested_vmx_check_apicv_controls(struct kvm_vcpu *vcpu,
10063 struct vmcs12 *vmcs12)
10064{
Wincy Van82f0dd42015-02-03 23:57:18 +080010065 if (!nested_cpu_has_virt_x2apic_mode(vmcs12) &&
Wincy Van608406e2015-02-03 23:57:51 +080010066 !nested_cpu_has_apic_reg_virt(vmcs12) &&
Wincy Van705699a2015-02-03 23:58:17 +080010067 !nested_cpu_has_vid(vmcs12) &&
10068 !nested_cpu_has_posted_intr(vmcs12))
Wincy Vanf2b93282015-02-03 23:56:03 +080010069 return 0;
10070
10071 /*
10072 * If virtualize x2apic mode is enabled,
10073 * virtualize apic access must be disabled.
10074 */
Wincy Van82f0dd42015-02-03 23:57:18 +080010075 if (nested_cpu_has_virt_x2apic_mode(vmcs12) &&
10076 nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES))
Wincy Vanf2b93282015-02-03 23:56:03 +080010077 return -EINVAL;
10078
Wincy Van608406e2015-02-03 23:57:51 +080010079 /*
10080 * If virtual interrupt delivery is enabled,
10081 * we must exit on external interrupts.
10082 */
10083 if (nested_cpu_has_vid(vmcs12) &&
10084 !nested_exit_on_intr(vcpu))
10085 return -EINVAL;
10086
Wincy Van705699a2015-02-03 23:58:17 +080010087 /*
10088 * bits 15:8 should be zero in posted_intr_nv,
10089 * the descriptor address has been already checked
10090 * in nested_get_vmcs12_pages.
10091 */
10092 if (nested_cpu_has_posted_intr(vmcs12) &&
10093 (!nested_cpu_has_vid(vmcs12) ||
10094 !nested_exit_intr_ack_set(vcpu) ||
10095 vmcs12->posted_intr_nv & 0xff00))
10096 return -EINVAL;
10097
Wincy Vanf2b93282015-02-03 23:56:03 +080010098 /* tpr shadow is needed by all apicv features. */
10099 if (!nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW))
10100 return -EINVAL;
10101
10102 return 0;
Wincy Van3af18d92015-02-03 23:49:31 +080010103}
10104
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010105static int nested_vmx_check_msr_switch(struct kvm_vcpu *vcpu,
10106 unsigned long count_field,
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030010107 unsigned long addr_field)
Wincy Vanff651cb2014-12-11 08:52:58 +030010108{
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030010109 int maxphyaddr;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010110 u64 count, addr;
10111
10112 if (vmcs12_read_any(vcpu, count_field, &count) ||
10113 vmcs12_read_any(vcpu, addr_field, &addr)) {
10114 WARN_ON(1);
10115 return -EINVAL;
10116 }
10117 if (count == 0)
10118 return 0;
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030010119 maxphyaddr = cpuid_maxphyaddr(vcpu);
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010120 if (!IS_ALIGNED(addr, 16) || addr >> maxphyaddr ||
10121 (addr + count * sizeof(struct vmx_msr_entry) - 1) >> maxphyaddr) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020010122 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010123 "nVMX: invalid MSR switch (0x%lx, %d, %llu, 0x%08llx)",
10124 addr_field, maxphyaddr, count, addr);
10125 return -EINVAL;
10126 }
10127 return 0;
10128}
10129
10130static int nested_vmx_check_msr_switch_controls(struct kvm_vcpu *vcpu,
10131 struct vmcs12 *vmcs12)
10132{
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010133 if (vmcs12->vm_exit_msr_load_count == 0 &&
10134 vmcs12->vm_exit_msr_store_count == 0 &&
10135 vmcs12->vm_entry_msr_load_count == 0)
10136 return 0; /* Fast path */
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010137 if (nested_vmx_check_msr_switch(vcpu, VM_EXIT_MSR_LOAD_COUNT,
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030010138 VM_EXIT_MSR_LOAD_ADDR) ||
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010139 nested_vmx_check_msr_switch(vcpu, VM_EXIT_MSR_STORE_COUNT,
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030010140 VM_EXIT_MSR_STORE_ADDR) ||
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010141 nested_vmx_check_msr_switch(vcpu, VM_ENTRY_MSR_LOAD_COUNT,
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030010142 VM_ENTRY_MSR_LOAD_ADDR))
Wincy Vanff651cb2014-12-11 08:52:58 +030010143 return -EINVAL;
10144 return 0;
10145}
10146
Bandan Dasc5f983f2017-05-05 15:25:14 -040010147static int nested_vmx_check_pml_controls(struct kvm_vcpu *vcpu,
10148 struct vmcs12 *vmcs12)
10149{
10150 u64 address = vmcs12->pml_address;
10151 int maxphyaddr = cpuid_maxphyaddr(vcpu);
10152
10153 if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_PML)) {
10154 if (!nested_cpu_has_ept(vmcs12) ||
10155 !IS_ALIGNED(address, 4096) ||
10156 address >> maxphyaddr)
10157 return -EINVAL;
10158 }
10159
10160 return 0;
10161}
10162
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010163static int nested_vmx_msr_check_common(struct kvm_vcpu *vcpu,
10164 struct vmx_msr_entry *e)
10165{
10166 /* x2APIC MSR accesses are not allowed */
Jan Kiszka8a9781f2015-05-04 08:32:32 +020010167 if (vcpu->arch.apic_base & X2APIC_ENABLE && e->index >> 8 == 0x8)
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010168 return -EINVAL;
10169 if (e->index == MSR_IA32_UCODE_WRITE || /* SDM Table 35-2 */
10170 e->index == MSR_IA32_UCODE_REV)
10171 return -EINVAL;
10172 if (e->reserved != 0)
10173 return -EINVAL;
10174 return 0;
10175}
10176
10177static int nested_vmx_load_msr_check(struct kvm_vcpu *vcpu,
10178 struct vmx_msr_entry *e)
Wincy Vanff651cb2014-12-11 08:52:58 +030010179{
10180 if (e->index == MSR_FS_BASE ||
10181 e->index == MSR_GS_BASE ||
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010182 e->index == MSR_IA32_SMM_MONITOR_CTL || /* SMM is not supported */
10183 nested_vmx_msr_check_common(vcpu, e))
10184 return -EINVAL;
10185 return 0;
10186}
10187
10188static int nested_vmx_store_msr_check(struct kvm_vcpu *vcpu,
10189 struct vmx_msr_entry *e)
10190{
10191 if (e->index == MSR_IA32_SMBASE || /* SMM is not supported */
10192 nested_vmx_msr_check_common(vcpu, e))
Wincy Vanff651cb2014-12-11 08:52:58 +030010193 return -EINVAL;
10194 return 0;
10195}
10196
10197/*
10198 * Load guest's/host's msr at nested entry/exit.
10199 * return 0 for success, entry index for failure.
10200 */
10201static u32 nested_vmx_load_msr(struct kvm_vcpu *vcpu, u64 gpa, u32 count)
10202{
10203 u32 i;
10204 struct vmx_msr_entry e;
10205 struct msr_data msr;
10206
10207 msr.host_initiated = false;
10208 for (i = 0; i < count; i++) {
Paolo Bonzini54bf36a2015-04-08 15:39:23 +020010209 if (kvm_vcpu_read_guest(vcpu, gpa + i * sizeof(e),
10210 &e, sizeof(e))) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020010211 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010212 "%s cannot read MSR entry (%u, 0x%08llx)\n",
10213 __func__, i, gpa + i * sizeof(e));
Wincy Vanff651cb2014-12-11 08:52:58 +030010214 goto fail;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010215 }
10216 if (nested_vmx_load_msr_check(vcpu, &e)) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020010217 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010218 "%s check failed (%u, 0x%x, 0x%x)\n",
10219 __func__, i, e.index, e.reserved);
10220 goto fail;
10221 }
Wincy Vanff651cb2014-12-11 08:52:58 +030010222 msr.index = e.index;
10223 msr.data = e.value;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010224 if (kvm_set_msr(vcpu, &msr)) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020010225 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010226 "%s cannot write MSR (%u, 0x%x, 0x%llx)\n",
10227 __func__, i, e.index, e.value);
Wincy Vanff651cb2014-12-11 08:52:58 +030010228 goto fail;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010229 }
Wincy Vanff651cb2014-12-11 08:52:58 +030010230 }
10231 return 0;
10232fail:
10233 return i + 1;
10234}
10235
10236static int nested_vmx_store_msr(struct kvm_vcpu *vcpu, u64 gpa, u32 count)
10237{
10238 u32 i;
10239 struct vmx_msr_entry e;
10240
10241 for (i = 0; i < count; i++) {
Paolo Bonzini609e36d2015-04-08 15:30:38 +020010242 struct msr_data msr_info;
Paolo Bonzini54bf36a2015-04-08 15:39:23 +020010243 if (kvm_vcpu_read_guest(vcpu,
10244 gpa + i * sizeof(e),
10245 &e, 2 * sizeof(u32))) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020010246 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010247 "%s cannot read MSR entry (%u, 0x%08llx)\n",
10248 __func__, i, gpa + i * sizeof(e));
Wincy Vanff651cb2014-12-11 08:52:58 +030010249 return -EINVAL;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010250 }
10251 if (nested_vmx_store_msr_check(vcpu, &e)) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020010252 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010253 "%s check failed (%u, 0x%x, 0x%x)\n",
10254 __func__, i, e.index, e.reserved);
Wincy Vanff651cb2014-12-11 08:52:58 +030010255 return -EINVAL;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010256 }
Paolo Bonzini609e36d2015-04-08 15:30:38 +020010257 msr_info.host_initiated = false;
10258 msr_info.index = e.index;
10259 if (kvm_get_msr(vcpu, &msr_info)) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020010260 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010261 "%s cannot read MSR (%u, 0x%x)\n",
10262 __func__, i, e.index);
10263 return -EINVAL;
10264 }
Paolo Bonzini54bf36a2015-04-08 15:39:23 +020010265 if (kvm_vcpu_write_guest(vcpu,
10266 gpa + i * sizeof(e) +
10267 offsetof(struct vmx_msr_entry, value),
10268 &msr_info.data, sizeof(msr_info.data))) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020010269 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010270 "%s cannot write MSR (%u, 0x%x, 0x%llx)\n",
Paolo Bonzini609e36d2015-04-08 15:30:38 +020010271 __func__, i, e.index, msr_info.data);
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010272 return -EINVAL;
10273 }
Wincy Vanff651cb2014-12-11 08:52:58 +030010274 }
10275 return 0;
10276}
10277
Ladi Prosek1dc35da2016-11-30 16:03:11 +010010278static bool nested_cr3_valid(struct kvm_vcpu *vcpu, unsigned long val)
10279{
10280 unsigned long invalid_mask;
10281
10282 invalid_mask = (~0ULL) << cpuid_maxphyaddr(vcpu);
10283 return (val & invalid_mask) == 0;
10284}
10285
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010286/*
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010010287 * Load guest's/host's cr3 at nested entry/exit. nested_ept is true if we are
10288 * emulating VM entry into a guest with EPT enabled.
10289 * Returns 0 on success, 1 on failure. Invalid state exit qualification code
10290 * is assigned to entry_failure_code on failure.
10291 */
10292static int nested_vmx_load_cr3(struct kvm_vcpu *vcpu, unsigned long cr3, bool nested_ept,
Jim Mattsonca0bde22016-11-30 12:03:46 -080010293 u32 *entry_failure_code)
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010010294{
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010010295 if (cr3 != kvm_read_cr3(vcpu) || (!nested_ept && pdptrs_changed(vcpu))) {
Ladi Prosek1dc35da2016-11-30 16:03:11 +010010296 if (!nested_cr3_valid(vcpu, cr3)) {
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010010297 *entry_failure_code = ENTRY_FAIL_DEFAULT;
10298 return 1;
10299 }
10300
10301 /*
10302 * If PAE paging and EPT are both on, CR3 is not used by the CPU and
10303 * must not be dereferenced.
10304 */
10305 if (!is_long_mode(vcpu) && is_pae(vcpu) && is_paging(vcpu) &&
10306 !nested_ept) {
10307 if (!load_pdptrs(vcpu, vcpu->arch.walk_mmu, cr3)) {
10308 *entry_failure_code = ENTRY_FAIL_PDPTE;
10309 return 1;
10310 }
10311 }
10312
10313 vcpu->arch.cr3 = cr3;
10314 __set_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail);
10315 }
10316
10317 kvm_mmu_reset_context(vcpu);
10318 return 0;
10319}
10320
Paolo Bonzini74a497f2017-12-20 13:55:39 +010010321static void prepare_vmcs02_full(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12,
10322 bool from_vmentry)
10323{
Paolo Bonzini8665c3f2017-12-20 13:56:53 +010010324 struct vcpu_vmx *vmx = to_vmx(vcpu);
10325
10326 vmcs_write16(GUEST_ES_SELECTOR, vmcs12->guest_es_selector);
10327 vmcs_write16(GUEST_SS_SELECTOR, vmcs12->guest_ss_selector);
10328 vmcs_write16(GUEST_DS_SELECTOR, vmcs12->guest_ds_selector);
10329 vmcs_write16(GUEST_FS_SELECTOR, vmcs12->guest_fs_selector);
10330 vmcs_write16(GUEST_GS_SELECTOR, vmcs12->guest_gs_selector);
10331 vmcs_write16(GUEST_LDTR_SELECTOR, vmcs12->guest_ldtr_selector);
10332 vmcs_write16(GUEST_TR_SELECTOR, vmcs12->guest_tr_selector);
10333 vmcs_write32(GUEST_ES_LIMIT, vmcs12->guest_es_limit);
10334 vmcs_write32(GUEST_SS_LIMIT, vmcs12->guest_ss_limit);
10335 vmcs_write32(GUEST_DS_LIMIT, vmcs12->guest_ds_limit);
10336 vmcs_write32(GUEST_FS_LIMIT, vmcs12->guest_fs_limit);
10337 vmcs_write32(GUEST_GS_LIMIT, vmcs12->guest_gs_limit);
10338 vmcs_write32(GUEST_LDTR_LIMIT, vmcs12->guest_ldtr_limit);
10339 vmcs_write32(GUEST_TR_LIMIT, vmcs12->guest_tr_limit);
10340 vmcs_write32(GUEST_GDTR_LIMIT, vmcs12->guest_gdtr_limit);
10341 vmcs_write32(GUEST_IDTR_LIMIT, vmcs12->guest_idtr_limit);
10342 vmcs_write32(GUEST_ES_AR_BYTES, vmcs12->guest_es_ar_bytes);
10343 vmcs_write32(GUEST_SS_AR_BYTES, vmcs12->guest_ss_ar_bytes);
10344 vmcs_write32(GUEST_DS_AR_BYTES, vmcs12->guest_ds_ar_bytes);
10345 vmcs_write32(GUEST_FS_AR_BYTES, vmcs12->guest_fs_ar_bytes);
10346 vmcs_write32(GUEST_GS_AR_BYTES, vmcs12->guest_gs_ar_bytes);
10347 vmcs_write32(GUEST_LDTR_AR_BYTES, vmcs12->guest_ldtr_ar_bytes);
10348 vmcs_write32(GUEST_TR_AR_BYTES, vmcs12->guest_tr_ar_bytes);
10349 vmcs_writel(GUEST_SS_BASE, vmcs12->guest_ss_base);
10350 vmcs_writel(GUEST_DS_BASE, vmcs12->guest_ds_base);
10351 vmcs_writel(GUEST_FS_BASE, vmcs12->guest_fs_base);
10352 vmcs_writel(GUEST_GS_BASE, vmcs12->guest_gs_base);
10353 vmcs_writel(GUEST_LDTR_BASE, vmcs12->guest_ldtr_base);
10354 vmcs_writel(GUEST_TR_BASE, vmcs12->guest_tr_base);
10355 vmcs_writel(GUEST_GDTR_BASE, vmcs12->guest_gdtr_base);
10356 vmcs_writel(GUEST_IDTR_BASE, vmcs12->guest_idtr_base);
Paolo Bonzini25a2e4f2017-12-20 14:05:21 +010010357
10358 vmcs_write32(GUEST_SYSENTER_CS, vmcs12->guest_sysenter_cs);
10359 vmcs_writel(GUEST_PENDING_DBG_EXCEPTIONS,
10360 vmcs12->guest_pending_dbg_exceptions);
10361 vmcs_writel(GUEST_SYSENTER_ESP, vmcs12->guest_sysenter_esp);
10362 vmcs_writel(GUEST_SYSENTER_EIP, vmcs12->guest_sysenter_eip);
10363
10364 if (nested_cpu_has_xsaves(vmcs12))
10365 vmcs_write64(XSS_EXIT_BITMAP, vmcs12->xss_exit_bitmap);
10366 vmcs_write64(VMCS_LINK_POINTER, -1ull);
10367
10368 if (cpu_has_vmx_posted_intr())
10369 vmcs_write16(POSTED_INTR_NV, POSTED_INTR_NESTED_VECTOR);
10370
10371 /*
10372 * Whether page-faults are trapped is determined by a combination of
10373 * 3 settings: PFEC_MASK, PFEC_MATCH and EXCEPTION_BITMAP.PF.
10374 * If enable_ept, L0 doesn't care about page faults and we should
10375 * set all of these to L1's desires. However, if !enable_ept, L0 does
10376 * care about (at least some) page faults, and because it is not easy
10377 * (if at all possible?) to merge L0 and L1's desires, we simply ask
10378 * to exit on each and every L2 page fault. This is done by setting
10379 * MASK=MATCH=0 and (see below) EB.PF=1.
10380 * Note that below we don't need special code to set EB.PF beyond the
10381 * "or"ing of the EB of vmcs01 and vmcs12, because when enable_ept,
10382 * vmcs01's EB.PF is 0 so the "or" will take vmcs12's value, and when
10383 * !enable_ept, EB.PF is 1, so the "or" will always be 1.
10384 */
10385 vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK,
10386 enable_ept ? vmcs12->page_fault_error_code_mask : 0);
10387 vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH,
10388 enable_ept ? vmcs12->page_fault_error_code_match : 0);
10389
10390 /* All VMFUNCs are currently emulated through L0 vmexits. */
10391 if (cpu_has_vmx_vmfunc())
10392 vmcs_write64(VM_FUNCTION_CONTROL, 0);
10393
10394 if (cpu_has_vmx_apicv()) {
10395 vmcs_write64(EOI_EXIT_BITMAP0, vmcs12->eoi_exit_bitmap0);
10396 vmcs_write64(EOI_EXIT_BITMAP1, vmcs12->eoi_exit_bitmap1);
10397 vmcs_write64(EOI_EXIT_BITMAP2, vmcs12->eoi_exit_bitmap2);
10398 vmcs_write64(EOI_EXIT_BITMAP3, vmcs12->eoi_exit_bitmap3);
10399 }
10400
10401 /*
10402 * Set host-state according to L0's settings (vmcs12 is irrelevant here)
10403 * Some constant fields are set here by vmx_set_constant_host_state().
10404 * Other fields are different per CPU, and will be set later when
10405 * vmx_vcpu_load() is called, and when vmx_save_host_state() is called.
10406 */
10407 vmx_set_constant_host_state(vmx);
10408
10409 /*
10410 * Set the MSR load/store lists to match L0's settings.
10411 */
10412 vmcs_write32(VM_EXIT_MSR_STORE_COUNT, 0);
10413 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, vmx->msr_autoload.nr);
10414 vmcs_write64(VM_EXIT_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.host));
10415 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, vmx->msr_autoload.nr);
10416 vmcs_write64(VM_ENTRY_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.guest));
10417
10418 set_cr4_guest_host_mask(vmx);
10419
10420 if (vmx_mpx_supported())
10421 vmcs_write64(GUEST_BNDCFGS, vmcs12->guest_bndcfgs);
10422
10423 if (enable_vpid) {
10424 if (nested_cpu_has_vpid(vmcs12) && vmx->nested.vpid02)
10425 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->nested.vpid02);
10426 else
10427 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid);
10428 }
10429
10430 /*
10431 * L1 may access the L2's PDPTR, so save them to construct vmcs12
10432 */
10433 if (enable_ept) {
10434 vmcs_write64(GUEST_PDPTR0, vmcs12->guest_pdptr0);
10435 vmcs_write64(GUEST_PDPTR1, vmcs12->guest_pdptr1);
10436 vmcs_write64(GUEST_PDPTR2, vmcs12->guest_pdptr2);
10437 vmcs_write64(GUEST_PDPTR3, vmcs12->guest_pdptr3);
10438 }
Paolo Bonzini74a497f2017-12-20 13:55:39 +010010439}
10440
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010441/*
10442 * prepare_vmcs02 is called when the L1 guest hypervisor runs its nested
10443 * L2 guest. L1 has a vmcs for L2 (vmcs12), and this function "merges" it
Tiejun Chenb4619662014-09-22 10:31:38 +080010444 * with L0's requirements for its guest (a.k.a. vmcs01), so we can run the L2
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010445 * guest in a way that will both be appropriate to L1's requests, and our
10446 * needs. In addition to modifying the active vmcs (which is vmcs02), this
10447 * function also has additional necessary side-effects, like setting various
10448 * vcpu->arch fields.
Ladi Prosekee146c12016-11-30 16:03:09 +010010449 * Returns 0 on success, 1 on failure. Invalid state exit qualification code
10450 * is assigned to entry_failure_code on failure.
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010451 */
Ladi Prosekee146c12016-11-30 16:03:09 +010010452static int prepare_vmcs02(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12,
Jim Mattsonca0bde22016-11-30 12:03:46 -080010453 bool from_vmentry, u32 *entry_failure_code)
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010454{
10455 struct vcpu_vmx *vmx = to_vmx(vcpu);
Bandan Das03efce62017-05-05 15:25:15 -040010456 u32 exec_control, vmcs12_exec_ctrl;
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010457
Paolo Bonzini8665c3f2017-12-20 13:56:53 +010010458 /*
10459 * First, the fields that are shadowed. This must be kept in sync
10460 * with vmx_shadow_fields.h.
10461 */
10462
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010463 vmcs_write16(GUEST_CS_SELECTOR, vmcs12->guest_cs_selector);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010464 vmcs_write32(GUEST_CS_LIMIT, vmcs12->guest_cs_limit);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010465 vmcs_write32(GUEST_CS_AR_BYTES, vmcs12->guest_cs_ar_bytes);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010466 vmcs_writel(GUEST_ES_BASE, vmcs12->guest_es_base);
10467 vmcs_writel(GUEST_CS_BASE, vmcs12->guest_cs_base);
Paolo Bonzini8665c3f2017-12-20 13:56:53 +010010468
10469 /*
10470 * Not in vmcs02: GUEST_PML_INDEX, HOST_FS_SELECTOR, HOST_GS_SELECTOR,
10471 * HOST_FS_BASE, HOST_GS_BASE.
10472 */
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010473
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080010474 if (from_vmentry &&
10475 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS)) {
Jan Kiszka2996fca2014-06-16 13:59:43 +020010476 kvm_set_dr(vcpu, 7, vmcs12->guest_dr7);
10477 vmcs_write64(GUEST_IA32_DEBUGCTL, vmcs12->guest_ia32_debugctl);
10478 } else {
10479 kvm_set_dr(vcpu, 7, vcpu->arch.dr7);
10480 vmcs_write64(GUEST_IA32_DEBUGCTL, vmx->nested.vmcs01_debugctl);
10481 }
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080010482 if (from_vmentry) {
10483 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
10484 vmcs12->vm_entry_intr_info_field);
10485 vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE,
10486 vmcs12->vm_entry_exception_error_code);
10487 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
10488 vmcs12->vm_entry_instruction_len);
10489 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO,
10490 vmcs12->guest_interruptibility_info);
Wanpeng Li2d6144e2017-07-25 03:40:46 -070010491 vmx->loaded_vmcs->nmi_known_unmasked =
10492 !(vmcs12->guest_interruptibility_info & GUEST_INTR_STATE_NMI);
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080010493 } else {
10494 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0);
10495 }
Gleb Natapov63fbf592013-07-28 18:31:06 +030010496 vmx_set_rflags(vcpu, vmcs12->guest_rflags);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010497
Jan Kiszkaf41245002014-03-07 20:03:13 +010010498 exec_control = vmcs12->pin_based_vm_exec_control;
Wincy Van705699a2015-02-03 23:58:17 +080010499
Paolo Bonzini9314006db2016-07-06 13:23:51 +020010500 /* Preemption timer setting is only taken from vmcs01. */
10501 exec_control &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
10502 exec_control |= vmcs_config.pin_based_exec_ctrl;
10503 if (vmx->hv_deadline_tsc == -1)
10504 exec_control &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
10505
10506 /* Posted interrupts setting is only taken from vmcs12. */
Wincy Van705699a2015-02-03 23:58:17 +080010507 if (nested_cpu_has_posted_intr(vmcs12)) {
Wincy Van705699a2015-02-03 23:58:17 +080010508 vmx->nested.posted_intr_nv = vmcs12->posted_intr_nv;
10509 vmx->nested.pi_pending = false;
Jim Mattson6beb7bd2016-11-30 12:03:45 -080010510 } else {
Wincy Van705699a2015-02-03 23:58:17 +080010511 exec_control &= ~PIN_BASED_POSTED_INTR;
Jim Mattson6beb7bd2016-11-30 12:03:45 -080010512 }
Wincy Van705699a2015-02-03 23:58:17 +080010513
Jan Kiszkaf41245002014-03-07 20:03:13 +010010514 vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, exec_control);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010515
Jan Kiszkaf41245002014-03-07 20:03:13 +010010516 vmx->nested.preemption_timer_expired = false;
10517 if (nested_cpu_has_preemption_timer(vmcs12))
10518 vmx_start_preemption_timer(vcpu);
Jan Kiszka0238ea92013-03-13 11:31:24 +010010519
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010520 if (cpu_has_secondary_exec_ctrls()) {
Paolo Bonzini80154d72017-08-24 13:55:35 +020010521 exec_control = vmx->secondary_exec_control;
Xiao Guangronge2821622015-09-09 14:05:52 +080010522
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010523 /* Take the following fields only from vmcs12 */
Paolo Bonzini696dfd92014-05-07 11:20:54 +020010524 exec_control &= ~(SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
Paolo Bonzini90a2db62017-07-27 13:22:13 +020010525 SECONDARY_EXEC_ENABLE_INVPCID |
Jan Kiszkab3a2a902015-03-23 19:27:19 +010010526 SECONDARY_EXEC_RDTSCP |
Paolo Bonzini3db13482017-08-24 14:48:03 +020010527 SECONDARY_EXEC_XSAVES |
Paolo Bonzini696dfd92014-05-07 11:20:54 +020010528 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY |
Bandan Das27c42a12017-08-03 15:54:42 -040010529 SECONDARY_EXEC_APIC_REGISTER_VIRT |
10530 SECONDARY_EXEC_ENABLE_VMFUNC);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010531 if (nested_cpu_has(vmcs12,
Bandan Das03efce62017-05-05 15:25:15 -040010532 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS)) {
10533 vmcs12_exec_ctrl = vmcs12->secondary_vm_exec_control &
10534 ~SECONDARY_EXEC_ENABLE_PML;
10535 exec_control |= vmcs12_exec_ctrl;
10536 }
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010537
Paolo Bonzini25a2e4f2017-12-20 14:05:21 +010010538 if (exec_control & SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY)
Wincy Van608406e2015-02-03 23:57:51 +080010539 vmcs_write16(GUEST_INTR_STATUS,
10540 vmcs12->guest_intr_status);
Wincy Van608406e2015-02-03 23:57:51 +080010541
Jim Mattson6beb7bd2016-11-30 12:03:45 -080010542 /*
10543 * Write an illegal value to APIC_ACCESS_ADDR. Later,
10544 * nested_get_vmcs12_pages will either fix it up or
10545 * remove the VM execution control.
10546 */
10547 if (exec_control & SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)
10548 vmcs_write64(APIC_ACCESS_ADDR, -1ull);
10549
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010550 vmcs_write32(SECONDARY_VM_EXEC_CONTROL, exec_control);
10551 }
10552
Jim Mattson83bafef2016-10-04 10:48:38 -070010553 /*
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010554 * HOST_RSP is normally set correctly in vmx_vcpu_run() just before
10555 * entry, but only if the current (host) sp changed from the value
10556 * we wrote last (vmx->host_rsp). This cache is no longer relevant
10557 * if we switch vmcs, and rather than hold a separate cache per vmcs,
10558 * here we just force the write to happen on entry.
10559 */
10560 vmx->host_rsp = 0;
10561
10562 exec_control = vmx_exec_control(vmx); /* L0's desires */
10563 exec_control &= ~CPU_BASED_VIRTUAL_INTR_PENDING;
10564 exec_control &= ~CPU_BASED_VIRTUAL_NMI_PENDING;
10565 exec_control &= ~CPU_BASED_TPR_SHADOW;
10566 exec_control |= vmcs12->cpu_based_vm_exec_control;
Wanpeng Lia7c0b072014-08-21 19:46:50 +080010567
Jim Mattson6beb7bd2016-11-30 12:03:45 -080010568 /*
10569 * Write an illegal value to VIRTUAL_APIC_PAGE_ADDR. Later, if
10570 * nested_get_vmcs12_pages can't fix it up, the illegal value
10571 * will result in a VM entry failure.
10572 */
Wanpeng Lia7c0b072014-08-21 19:46:50 +080010573 if (exec_control & CPU_BASED_TPR_SHADOW) {
Jim Mattson6beb7bd2016-11-30 12:03:45 -080010574 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, -1ull);
Wanpeng Lia7c0b072014-08-21 19:46:50 +080010575 vmcs_write32(TPR_THRESHOLD, vmcs12->tpr_threshold);
Jim Mattson51aa68e2017-09-12 13:02:54 -070010576 } else {
10577#ifdef CONFIG_X86_64
10578 exec_control |= CPU_BASED_CR8_LOAD_EXITING |
10579 CPU_BASED_CR8_STORE_EXITING;
10580#endif
Wanpeng Lia7c0b072014-08-21 19:46:50 +080010581 }
10582
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010583 /*
Quan Xu8eb73e22017-12-12 16:44:21 +080010584 * A vmexit (to either L1 hypervisor or L0 userspace) is always needed
10585 * for I/O port accesses.
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010586 */
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010587 exec_control &= ~CPU_BASED_USE_IO_BITMAPS;
10588 exec_control |= CPU_BASED_UNCOND_IO_EXITING;
10589
10590 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, exec_control);
10591
10592 /* EXCEPTION_BITMAP and CR0_GUEST_HOST_MASK should basically be the
10593 * bitwise-or of what L1 wants to trap for L2, and what we want to
10594 * trap. Note that CR0.TS also needs updating - we do this later.
10595 */
10596 update_exception_bitmap(vcpu);
10597 vcpu->arch.cr0_guest_owned_bits &= ~vmcs12->cr0_guest_host_mask;
10598 vmcs_writel(CR0_GUEST_HOST_MASK, ~vcpu->arch.cr0_guest_owned_bits);
10599
Nadav Har'El8049d652013-08-05 11:07:06 +030010600 /* L2->L1 exit controls are emulated - the hardware exit is to L0 so
10601 * we should use its exit controls. Note that VM_EXIT_LOAD_IA32_EFER
10602 * bits are further modified by vmx_set_efer() below.
10603 */
Jan Kiszkaf41245002014-03-07 20:03:13 +010010604 vmcs_write32(VM_EXIT_CONTROLS, vmcs_config.vmexit_ctrl);
Nadav Har'El8049d652013-08-05 11:07:06 +030010605
10606 /* vmcs12's VM_ENTRY_LOAD_IA32_EFER and VM_ENTRY_IA32E_MODE are
10607 * emulated by vmx_set_efer(), below.
10608 */
Gleb Natapov2961e8762013-11-25 15:37:13 +020010609 vm_entry_controls_init(vmx,
Nadav Har'El8049d652013-08-05 11:07:06 +030010610 (vmcs12->vm_entry_controls & ~VM_ENTRY_LOAD_IA32_EFER &
10611 ~VM_ENTRY_IA32E_MODE) |
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010612 (vmcs_config.vmentry_ctrl & ~VM_ENTRY_IA32E_MODE));
10613
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080010614 if (from_vmentry &&
10615 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_PAT)) {
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010616 vmcs_write64(GUEST_IA32_PAT, vmcs12->guest_ia32_pat);
Jan Kiszka44811c02013-08-04 17:17:27 +020010617 vcpu->arch.pat = vmcs12->guest_ia32_pat;
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080010618 } else if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) {
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010619 vmcs_write64(GUEST_IA32_PAT, vmx->vcpu.arch.pat);
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080010620 }
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010621
Nadav Har'El27fc51b2011-08-02 15:54:52 +030010622 if (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING)
10623 vmcs_write64(TSC_OFFSET,
Paolo Bonziniea26e4e2016-11-01 00:39:48 +010010624 vcpu->arch.tsc_offset + vmcs12->tsc_offset);
Nadav Har'El27fc51b2011-08-02 15:54:52 +030010625 else
Paolo Bonziniea26e4e2016-11-01 00:39:48 +010010626 vmcs_write64(TSC_OFFSET, vcpu->arch.tsc_offset);
Peter Feinerc95ba922016-08-17 09:36:47 -070010627 if (kvm_has_tsc_control)
10628 decache_tsc_multiplier(vmx);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010629
10630 if (enable_vpid) {
10631 /*
Wanpeng Li5c614b32015-10-13 09:18:36 -070010632 * There is no direct mapping between vpid02 and vpid12, the
10633 * vpid02 is per-vCPU for L0 and reused while the value of
10634 * vpid12 is changed w/ one invvpid during nested vmentry.
10635 * The vpid12 is allocated by L1 for L2, so it will not
10636 * influence global bitmap(for vpid01 and vpid02 allocation)
10637 * even if spawn a lot of nested vCPUs.
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010638 */
Wanpeng Li5c614b32015-10-13 09:18:36 -070010639 if (nested_cpu_has_vpid(vmcs12) && vmx->nested.vpid02) {
Wanpeng Li5c614b32015-10-13 09:18:36 -070010640 if (vmcs12->virtual_processor_id != vmx->nested.last_vpid) {
10641 vmx->nested.last_vpid = vmcs12->virtual_processor_id;
Wanpeng Lic2ba05c2017-12-12 17:33:03 -080010642 __vmx_flush_tlb(vcpu, to_vmx(vcpu)->nested.vpid02, true);
Wanpeng Li5c614b32015-10-13 09:18:36 -070010643 }
10644 } else {
Wanpeng Lic2ba05c2017-12-12 17:33:03 -080010645 vmx_flush_tlb(vcpu, true);
Wanpeng Li5c614b32015-10-13 09:18:36 -070010646 }
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010647 }
10648
Ladi Prosek1fb883b2017-04-04 14:18:53 +020010649 if (enable_pml) {
10650 /*
10651 * Conceptually we want to copy the PML address and index from
10652 * vmcs01 here, and then back to vmcs01 on nested vmexit. But,
10653 * since we always flush the log on each vmexit, this happens
10654 * to be equivalent to simply resetting the fields in vmcs02.
10655 */
10656 ASSERT(vmx->pml_pg);
10657 vmcs_write64(PML_ADDRESS, page_to_phys(vmx->pml_pg));
10658 vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1);
10659 }
10660
Nadav Har'El155a97a2013-08-05 11:07:16 +030010661 if (nested_cpu_has_ept(vmcs12)) {
Paolo Bonziniae1e2d12017-03-30 11:55:30 +020010662 if (nested_ept_init_mmu_context(vcpu)) {
10663 *entry_failure_code = ENTRY_FAIL_DEFAULT;
10664 return 1;
10665 }
Jim Mattsonfb6c8192017-03-16 13:53:59 -070010666 } else if (nested_cpu_has2(vmcs12,
10667 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) {
10668 vmx_flush_tlb_ept_only(vcpu);
Nadav Har'El155a97a2013-08-05 11:07:16 +030010669 }
10670
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010671 /*
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -080010672 * This sets GUEST_CR0 to vmcs12->guest_cr0, possibly modifying those
10673 * bits which we consider mandatory enabled.
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010674 * The CR0_READ_SHADOW is what L2 should have expected to read given
10675 * the specifications by L1; It's not enough to take
10676 * vmcs12->cr0_read_shadow because on our cr0_guest_host_mask we we
10677 * have more bits than L1 expected.
10678 */
10679 vmx_set_cr0(vcpu, vmcs12->guest_cr0);
10680 vmcs_writel(CR0_READ_SHADOW, nested_read_cr0(vmcs12));
10681
10682 vmx_set_cr4(vcpu, vmcs12->guest_cr4);
10683 vmcs_writel(CR4_READ_SHADOW, nested_read_cr4(vmcs12));
10684
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080010685 if (from_vmentry &&
10686 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_EFER))
David Matlack5a6a9742016-11-29 18:14:10 -080010687 vcpu->arch.efer = vmcs12->guest_ia32_efer;
10688 else if (vmcs12->vm_entry_controls & VM_ENTRY_IA32E_MODE)
10689 vcpu->arch.efer |= (EFER_LMA | EFER_LME);
10690 else
10691 vcpu->arch.efer &= ~(EFER_LMA | EFER_LME);
10692 /* Note: modifies VM_ENTRY/EXIT_CONTROLS and GUEST/HOST_IA32_EFER */
10693 vmx_set_efer(vcpu, vcpu->arch.efer);
10694
Paolo Bonzini74a497f2017-12-20 13:55:39 +010010695 if (vmx->nested.dirty_vmcs12) {
10696 prepare_vmcs02_full(vcpu, vmcs12, from_vmentry);
10697 vmx->nested.dirty_vmcs12 = false;
10698 }
10699
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010010700 /* Shadow page tables on either EPT or shadow page tables. */
Ladi Prosek7ad658b2017-03-23 07:18:08 +010010701 if (nested_vmx_load_cr3(vcpu, vmcs12->guest_cr3, nested_cpu_has_ept(vmcs12),
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010010702 entry_failure_code))
10703 return 1;
Ladi Prosek7ca29de2016-11-30 16:03:08 +010010704
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +030010705 if (!enable_ept)
10706 vcpu->arch.walk_mmu->inject_page_fault = vmx_inject_page_fault_nested;
10707
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010708 kvm_register_write(vcpu, VCPU_REGS_RSP, vmcs12->guest_rsp);
10709 kvm_register_write(vcpu, VCPU_REGS_RIP, vmcs12->guest_rip);
Ladi Prosekee146c12016-11-30 16:03:09 +010010710 return 0;
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010711}
10712
Jim Mattsonca0bde22016-11-30 12:03:46 -080010713static int check_vmentry_prereqs(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
10714{
10715 struct vcpu_vmx *vmx = to_vmx(vcpu);
10716
10717 if (vmcs12->guest_activity_state != GUEST_ACTIVITY_ACTIVE &&
10718 vmcs12->guest_activity_state != GUEST_ACTIVITY_HLT)
10719 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
10720
Jim Mattson56a20512017-07-06 16:33:06 -070010721 if (nested_vmx_check_io_bitmap_controls(vcpu, vmcs12))
10722 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
10723
Jim Mattsonca0bde22016-11-30 12:03:46 -080010724 if (nested_vmx_check_msr_bitmap_controls(vcpu, vmcs12))
10725 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
10726
Jim Mattson712b12d2017-08-24 13:24:47 -070010727 if (nested_vmx_check_tpr_shadow_controls(vcpu, vmcs12))
10728 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
10729
Jim Mattsonca0bde22016-11-30 12:03:46 -080010730 if (nested_vmx_check_apicv_controls(vcpu, vmcs12))
10731 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
10732
10733 if (nested_vmx_check_msr_switch_controls(vcpu, vmcs12))
10734 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
10735
Bandan Dasc5f983f2017-05-05 15:25:14 -040010736 if (nested_vmx_check_pml_controls(vcpu, vmcs12))
10737 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
10738
Jim Mattsonca0bde22016-11-30 12:03:46 -080010739 if (!vmx_control_verify(vmcs12->cpu_based_vm_exec_control,
10740 vmx->nested.nested_vmx_procbased_ctls_low,
10741 vmx->nested.nested_vmx_procbased_ctls_high) ||
Jim Mattson2e5b0bd2017-05-04 11:51:58 -070010742 (nested_cpu_has(vmcs12, CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) &&
10743 !vmx_control_verify(vmcs12->secondary_vm_exec_control,
10744 vmx->nested.nested_vmx_secondary_ctls_low,
10745 vmx->nested.nested_vmx_secondary_ctls_high)) ||
Jim Mattsonca0bde22016-11-30 12:03:46 -080010746 !vmx_control_verify(vmcs12->pin_based_vm_exec_control,
10747 vmx->nested.nested_vmx_pinbased_ctls_low,
10748 vmx->nested.nested_vmx_pinbased_ctls_high) ||
10749 !vmx_control_verify(vmcs12->vm_exit_controls,
10750 vmx->nested.nested_vmx_exit_ctls_low,
10751 vmx->nested.nested_vmx_exit_ctls_high) ||
10752 !vmx_control_verify(vmcs12->vm_entry_controls,
10753 vmx->nested.nested_vmx_entry_ctls_low,
10754 vmx->nested.nested_vmx_entry_ctls_high))
10755 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
10756
Bandan Das41ab9372017-08-03 15:54:43 -040010757 if (nested_cpu_has_vmfunc(vmcs12)) {
10758 if (vmcs12->vm_function_control &
10759 ~vmx->nested.nested_vmx_vmfunc_controls)
10760 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
10761
10762 if (nested_cpu_has_eptp_switching(vmcs12)) {
10763 if (!nested_cpu_has_ept(vmcs12) ||
10764 !page_address_valid(vcpu, vmcs12->eptp_list_address))
10765 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
10766 }
10767 }
Bandan Das27c42a12017-08-03 15:54:42 -040010768
Jim Mattsonc7c2c7092017-05-05 11:28:09 -070010769 if (vmcs12->cr3_target_count > nested_cpu_vmx_misc_cr3_count(vcpu))
10770 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
10771
Jim Mattsonca0bde22016-11-30 12:03:46 -080010772 if (!nested_host_cr0_valid(vcpu, vmcs12->host_cr0) ||
10773 !nested_host_cr4_valid(vcpu, vmcs12->host_cr4) ||
10774 !nested_cr3_valid(vcpu, vmcs12->host_cr3))
10775 return VMXERR_ENTRY_INVALID_HOST_STATE_FIELD;
10776
10777 return 0;
10778}
10779
10780static int check_vmentry_postreqs(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12,
10781 u32 *exit_qual)
10782{
10783 bool ia32e;
10784
10785 *exit_qual = ENTRY_FAIL_DEFAULT;
10786
10787 if (!nested_guest_cr0_valid(vcpu, vmcs12->guest_cr0) ||
10788 !nested_guest_cr4_valid(vcpu, vmcs12->guest_cr4))
10789 return 1;
10790
10791 if (!nested_cpu_has2(vmcs12, SECONDARY_EXEC_SHADOW_VMCS) &&
10792 vmcs12->vmcs_link_pointer != -1ull) {
10793 *exit_qual = ENTRY_FAIL_VMCS_LINK_PTR;
10794 return 1;
10795 }
10796
10797 /*
10798 * If the load IA32_EFER VM-entry control is 1, the following checks
10799 * are performed on the field for the IA32_EFER MSR:
10800 * - Bits reserved in the IA32_EFER MSR must be 0.
10801 * - Bit 10 (corresponding to IA32_EFER.LMA) must equal the value of
10802 * the IA-32e mode guest VM-exit control. It must also be identical
10803 * to bit 8 (LME) if bit 31 in the CR0 field (corresponding to
10804 * CR0.PG) is 1.
10805 */
10806 if (to_vmx(vcpu)->nested.nested_run_pending &&
10807 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_EFER)) {
10808 ia32e = (vmcs12->vm_entry_controls & VM_ENTRY_IA32E_MODE) != 0;
10809 if (!kvm_valid_efer(vcpu, vmcs12->guest_ia32_efer) ||
10810 ia32e != !!(vmcs12->guest_ia32_efer & EFER_LMA) ||
10811 ((vmcs12->guest_cr0 & X86_CR0_PG) &&
10812 ia32e != !!(vmcs12->guest_ia32_efer & EFER_LME)))
10813 return 1;
10814 }
10815
10816 /*
10817 * If the load IA32_EFER VM-exit control is 1, bits reserved in the
10818 * IA32_EFER MSR must be 0 in the field for that register. In addition,
10819 * the values of the LMA and LME bits in the field must each be that of
10820 * the host address-space size VM-exit control.
10821 */
10822 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_EFER) {
10823 ia32e = (vmcs12->vm_exit_controls &
10824 VM_EXIT_HOST_ADDR_SPACE_SIZE) != 0;
10825 if (!kvm_valid_efer(vcpu, vmcs12->host_ia32_efer) ||
10826 ia32e != !!(vmcs12->host_ia32_efer & EFER_LMA) ||
10827 ia32e != !!(vmcs12->host_ia32_efer & EFER_LME))
10828 return 1;
10829 }
10830
Wanpeng Lif1b026a2017-11-05 16:54:48 -080010831 if ((vmcs12->vm_entry_controls & VM_ENTRY_LOAD_BNDCFGS) &&
10832 (is_noncanonical_address(vmcs12->guest_bndcfgs & PAGE_MASK, vcpu) ||
10833 (vmcs12->guest_bndcfgs & MSR_IA32_BNDCFGS_RSVD)))
10834 return 1;
10835
Jim Mattsonca0bde22016-11-30 12:03:46 -080010836 return 0;
10837}
10838
Jim Mattson858e25c2016-11-30 12:03:47 -080010839static int enter_vmx_non_root_mode(struct kvm_vcpu *vcpu, bool from_vmentry)
10840{
10841 struct vcpu_vmx *vmx = to_vmx(vcpu);
10842 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Jim Mattson858e25c2016-11-30 12:03:47 -080010843 u32 msr_entry_idx;
10844 u32 exit_qual;
10845
Jim Mattson858e25c2016-11-30 12:03:47 -080010846 enter_guest_mode(vcpu);
10847
10848 if (!(vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS))
10849 vmx->nested.vmcs01_debugctl = vmcs_read64(GUEST_IA32_DEBUGCTL);
10850
Jim Mattson00647b42017-11-27 17:22:25 -060010851 vmx_switch_vmcs(vcpu, &vmx->nested.vmcs02);
Jim Mattson858e25c2016-11-30 12:03:47 -080010852 vmx_segment_cache_clear(vmx);
10853
10854 if (prepare_vmcs02(vcpu, vmcs12, from_vmentry, &exit_qual)) {
10855 leave_guest_mode(vcpu);
David Hildenbrand1279a6b12017-03-20 10:00:08 +010010856 vmx_switch_vmcs(vcpu, &vmx->vmcs01);
Jim Mattson858e25c2016-11-30 12:03:47 -080010857 nested_vmx_entry_failure(vcpu, vmcs12,
10858 EXIT_REASON_INVALID_STATE, exit_qual);
10859 return 1;
10860 }
10861
10862 nested_get_vmcs12_pages(vcpu, vmcs12);
10863
10864 msr_entry_idx = nested_vmx_load_msr(vcpu,
10865 vmcs12->vm_entry_msr_load_addr,
10866 vmcs12->vm_entry_msr_load_count);
10867 if (msr_entry_idx) {
10868 leave_guest_mode(vcpu);
David Hildenbrand1279a6b12017-03-20 10:00:08 +010010869 vmx_switch_vmcs(vcpu, &vmx->vmcs01);
Jim Mattson858e25c2016-11-30 12:03:47 -080010870 nested_vmx_entry_failure(vcpu, vmcs12,
10871 EXIT_REASON_MSR_LOAD_FAIL, msr_entry_idx);
10872 return 1;
10873 }
10874
Jim Mattson858e25c2016-11-30 12:03:47 -080010875 /*
10876 * Note no nested_vmx_succeed or nested_vmx_fail here. At this point
10877 * we are no longer running L1, and VMLAUNCH/VMRESUME has not yet
10878 * returned as far as L1 is concerned. It will only return (and set
10879 * the success flag) when L2 exits (see nested_vmx_vmexit()).
10880 */
10881 return 0;
10882}
10883
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030010884/*
10885 * nested_vmx_run() handles a nested entry, i.e., a VMLAUNCH or VMRESUME on L1
10886 * for running an L2 nested guest.
10887 */
10888static int nested_vmx_run(struct kvm_vcpu *vcpu, bool launch)
10889{
10890 struct vmcs12 *vmcs12;
10891 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jim Mattsonb3f1dfb2017-07-17 12:00:34 -070010892 u32 interrupt_shadow = vmx_get_interrupt_shadow(vcpu);
Jim Mattsonca0bde22016-11-30 12:03:46 -080010893 u32 exit_qual;
10894 int ret;
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030010895
Kyle Hueyeb277562016-11-29 12:40:39 -080010896 if (!nested_vmx_check_permission(vcpu))
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030010897 return 1;
10898
Kyle Hueyeb277562016-11-29 12:40:39 -080010899 if (!nested_vmx_check_vmcs12(vcpu))
10900 goto out;
10901
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030010902 vmcs12 = get_vmcs12(vcpu);
10903
Abel Gordon012f83c2013-04-18 14:39:25 +030010904 if (enable_shadow_vmcs)
10905 copy_shadow_to_vmcs12(vmx);
10906
Nadav Har'El7c177932011-05-25 23:12:04 +030010907 /*
10908 * The nested entry process starts with enforcing various prerequisites
10909 * on vmcs12 as required by the Intel SDM, and act appropriately when
10910 * they fail: As the SDM explains, some conditions should cause the
10911 * instruction to fail, while others will cause the instruction to seem
10912 * to succeed, but return an EXIT_REASON_INVALID_STATE.
10913 * To speed up the normal (success) code path, we should avoid checking
10914 * for misconfigurations which will anyway be caught by the processor
10915 * when using the merged vmcs02.
10916 */
Jim Mattsonb3f1dfb2017-07-17 12:00:34 -070010917 if (interrupt_shadow & KVM_X86_SHADOW_INT_MOV_SS) {
10918 nested_vmx_failValid(vcpu,
10919 VMXERR_ENTRY_EVENTS_BLOCKED_BY_MOV_SS);
10920 goto out;
10921 }
10922
Nadav Har'El7c177932011-05-25 23:12:04 +030010923 if (vmcs12->launch_state == launch) {
10924 nested_vmx_failValid(vcpu,
10925 launch ? VMXERR_VMLAUNCH_NONCLEAR_VMCS
10926 : VMXERR_VMRESUME_NONLAUNCHED_VMCS);
Kyle Hueyeb277562016-11-29 12:40:39 -080010927 goto out;
Nadav Har'El7c177932011-05-25 23:12:04 +030010928 }
10929
Jim Mattsonca0bde22016-11-30 12:03:46 -080010930 ret = check_vmentry_prereqs(vcpu, vmcs12);
10931 if (ret) {
10932 nested_vmx_failValid(vcpu, ret);
Kyle Hueyeb277562016-11-29 12:40:39 -080010933 goto out;
Paolo Bonzini26539bd2013-04-15 15:00:27 +020010934 }
10935
Nadav Har'El7c177932011-05-25 23:12:04 +030010936 /*
Jim Mattsonca0bde22016-11-30 12:03:46 -080010937 * After this point, the trap flag no longer triggers a singlestep trap
10938 * on the vm entry instructions; don't call kvm_skip_emulated_instruction.
10939 * This is not 100% correct; for performance reasons, we delegate most
10940 * of the checks on host state to the processor. If those fail,
10941 * the singlestep trap is missed.
Jan Kiszka384bb782013-04-20 10:52:36 +020010942 */
Jim Mattsonca0bde22016-11-30 12:03:46 -080010943 skip_emulated_instruction(vcpu);
Jan Kiszka384bb782013-04-20 10:52:36 +020010944
Jim Mattsonca0bde22016-11-30 12:03:46 -080010945 ret = check_vmentry_postreqs(vcpu, vmcs12, &exit_qual);
10946 if (ret) {
10947 nested_vmx_entry_failure(vcpu, vmcs12,
10948 EXIT_REASON_INVALID_STATE, exit_qual);
10949 return 1;
Jan Kiszka384bb782013-04-20 10:52:36 +020010950 }
10951
10952 /*
Nadav Har'El7c177932011-05-25 23:12:04 +030010953 * We're finally done with prerequisite checking, and can start with
10954 * the nested entry.
10955 */
10956
Jim Mattson858e25c2016-11-30 12:03:47 -080010957 ret = enter_vmx_non_root_mode(vcpu, true);
10958 if (ret)
10959 return ret;
Wincy Vanff651cb2014-12-11 08:52:58 +030010960
Jan Kiszka6dfacad2013-12-04 08:58:54 +010010961 if (vmcs12->guest_activity_state == GUEST_ACTIVITY_HLT)
Joel Schopp5cb56052015-03-02 13:43:31 -060010962 return kvm_vcpu_halt(vcpu);
Jan Kiszka6dfacad2013-12-04 08:58:54 +010010963
Jan Kiszka7af40ad32014-01-04 18:47:23 +010010964 vmx->nested.nested_run_pending = 1;
10965
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030010966 return 1;
Kyle Hueyeb277562016-11-29 12:40:39 -080010967
10968out:
Kyle Huey6affcbe2016-11-29 12:40:40 -080010969 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030010970}
10971
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010972/*
10973 * On a nested exit from L2 to L1, vmcs12.guest_cr0 might not be up-to-date
10974 * because L2 may have changed some cr0 bits directly (CRO_GUEST_HOST_MASK).
10975 * This function returns the new value we should put in vmcs12.guest_cr0.
10976 * It's not enough to just return the vmcs02 GUEST_CR0. Rather,
10977 * 1. Bits that neither L0 nor L1 trapped, were set directly by L2 and are now
10978 * available in vmcs02 GUEST_CR0. (Note: It's enough to check that L0
10979 * didn't trap the bit, because if L1 did, so would L0).
10980 * 2. Bits that L1 asked to trap (and therefore L0 also did) could not have
10981 * been modified by L2, and L1 knows it. So just leave the old value of
10982 * the bit from vmcs12.guest_cr0. Note that the bit from vmcs02 GUEST_CR0
10983 * isn't relevant, because if L0 traps this bit it can set it to anything.
10984 * 3. Bits that L1 didn't trap, but L0 did. L1 believes the guest could have
10985 * changed these bits, and therefore they need to be updated, but L0
10986 * didn't necessarily allow them to be changed in GUEST_CR0 - and rather
10987 * put them in vmcs02 CR0_READ_SHADOW. So take these bits from there.
10988 */
10989static inline unsigned long
10990vmcs12_guest_cr0(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
10991{
10992 return
10993 /*1*/ (vmcs_readl(GUEST_CR0) & vcpu->arch.cr0_guest_owned_bits) |
10994 /*2*/ (vmcs12->guest_cr0 & vmcs12->cr0_guest_host_mask) |
10995 /*3*/ (vmcs_readl(CR0_READ_SHADOW) & ~(vmcs12->cr0_guest_host_mask |
10996 vcpu->arch.cr0_guest_owned_bits));
10997}
10998
10999static inline unsigned long
11000vmcs12_guest_cr4(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
11001{
11002 return
11003 /*1*/ (vmcs_readl(GUEST_CR4) & vcpu->arch.cr4_guest_owned_bits) |
11004 /*2*/ (vmcs12->guest_cr4 & vmcs12->cr4_guest_host_mask) |
11005 /*3*/ (vmcs_readl(CR4_READ_SHADOW) & ~(vmcs12->cr4_guest_host_mask |
11006 vcpu->arch.cr4_guest_owned_bits));
11007}
11008
Jan Kiszka5f3d5792013-04-14 12:12:46 +020011009static void vmcs12_save_pending_event(struct kvm_vcpu *vcpu,
11010 struct vmcs12 *vmcs12)
11011{
11012 u32 idt_vectoring;
11013 unsigned int nr;
11014
Wanpeng Li664f8e22017-08-24 03:35:09 -070011015 if (vcpu->arch.exception.injected) {
Jan Kiszka5f3d5792013-04-14 12:12:46 +020011016 nr = vcpu->arch.exception.nr;
11017 idt_vectoring = nr | VECTORING_INFO_VALID_MASK;
11018
11019 if (kvm_exception_is_soft(nr)) {
11020 vmcs12->vm_exit_instruction_len =
11021 vcpu->arch.event_exit_inst_len;
11022 idt_vectoring |= INTR_TYPE_SOFT_EXCEPTION;
11023 } else
11024 idt_vectoring |= INTR_TYPE_HARD_EXCEPTION;
11025
11026 if (vcpu->arch.exception.has_error_code) {
11027 idt_vectoring |= VECTORING_INFO_DELIVER_CODE_MASK;
11028 vmcs12->idt_vectoring_error_code =
11029 vcpu->arch.exception.error_code;
11030 }
11031
11032 vmcs12->idt_vectoring_info_field = idt_vectoring;
Jan Kiszkacd2633c2013-10-23 17:42:15 +010011033 } else if (vcpu->arch.nmi_injected) {
Jan Kiszka5f3d5792013-04-14 12:12:46 +020011034 vmcs12->idt_vectoring_info_field =
11035 INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK | NMI_VECTOR;
11036 } else if (vcpu->arch.interrupt.pending) {
11037 nr = vcpu->arch.interrupt.nr;
11038 idt_vectoring = nr | VECTORING_INFO_VALID_MASK;
11039
11040 if (vcpu->arch.interrupt.soft) {
11041 idt_vectoring |= INTR_TYPE_SOFT_INTR;
11042 vmcs12->vm_entry_instruction_len =
11043 vcpu->arch.event_exit_inst_len;
11044 } else
11045 idt_vectoring |= INTR_TYPE_EXT_INTR;
11046
11047 vmcs12->idt_vectoring_info_field = idt_vectoring;
11048 }
11049}
11050
Jan Kiszkab6b8a142014-03-07 20:03:12 +010011051static int vmx_check_nested_events(struct kvm_vcpu *vcpu, bool external_intr)
11052{
11053 struct vcpu_vmx *vmx = to_vmx(vcpu);
Wanpeng Libfcf83b2017-08-24 03:35:11 -070011054 unsigned long exit_qual;
Liran Alon917dc602017-11-05 16:07:43 +020011055 bool block_nested_events =
11056 vmx->nested.nested_run_pending || kvm_event_needs_reinjection(vcpu);
Wanpeng Liacc9ab62017-02-27 04:24:39 -080011057
Wanpeng Libfcf83b2017-08-24 03:35:11 -070011058 if (vcpu->arch.exception.pending &&
11059 nested_vmx_check_exception(vcpu, &exit_qual)) {
Liran Alon917dc602017-11-05 16:07:43 +020011060 if (block_nested_events)
Wanpeng Libfcf83b2017-08-24 03:35:11 -070011061 return -EBUSY;
11062 nested_vmx_inject_exception_vmexit(vcpu, exit_qual);
Wanpeng Libfcf83b2017-08-24 03:35:11 -070011063 return 0;
11064 }
11065
Jan Kiszkaf41245002014-03-07 20:03:13 +010011066 if (nested_cpu_has_preemption_timer(get_vmcs12(vcpu)) &&
11067 vmx->nested.preemption_timer_expired) {
Liran Alon917dc602017-11-05 16:07:43 +020011068 if (block_nested_events)
Jan Kiszkaf41245002014-03-07 20:03:13 +010011069 return -EBUSY;
11070 nested_vmx_vmexit(vcpu, EXIT_REASON_PREEMPTION_TIMER, 0, 0);
11071 return 0;
11072 }
11073
Jan Kiszkab6b8a142014-03-07 20:03:12 +010011074 if (vcpu->arch.nmi_pending && nested_exit_on_nmi(vcpu)) {
Liran Alon917dc602017-11-05 16:07:43 +020011075 if (block_nested_events)
Jan Kiszkab6b8a142014-03-07 20:03:12 +010011076 return -EBUSY;
11077 nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI,
11078 NMI_VECTOR | INTR_TYPE_NMI_INTR |
11079 INTR_INFO_VALID_MASK, 0);
11080 /*
11081 * The NMI-triggered VM exit counts as injection:
11082 * clear this one and block further NMIs.
11083 */
11084 vcpu->arch.nmi_pending = 0;
11085 vmx_set_nmi_mask(vcpu, true);
11086 return 0;
11087 }
11088
11089 if ((kvm_cpu_has_interrupt(vcpu) || external_intr) &&
11090 nested_exit_on_intr(vcpu)) {
Liran Alon917dc602017-11-05 16:07:43 +020011091 if (block_nested_events)
Jan Kiszkab6b8a142014-03-07 20:03:12 +010011092 return -EBUSY;
11093 nested_vmx_vmexit(vcpu, EXIT_REASON_EXTERNAL_INTERRUPT, 0, 0);
Wincy Van705699a2015-02-03 23:58:17 +080011094 return 0;
Jan Kiszkab6b8a142014-03-07 20:03:12 +010011095 }
11096
David Hildenbrand6342c502017-01-25 11:58:58 +010011097 vmx_complete_nested_posted_interrupt(vcpu);
11098 return 0;
Jan Kiszkab6b8a142014-03-07 20:03:12 +010011099}
11100
Jan Kiszkaf41245002014-03-07 20:03:13 +010011101static u32 vmx_get_preemption_timer_value(struct kvm_vcpu *vcpu)
11102{
11103 ktime_t remaining =
11104 hrtimer_get_remaining(&to_vmx(vcpu)->nested.preemption_timer);
11105 u64 value;
11106
11107 if (ktime_to_ns(remaining) <= 0)
11108 return 0;
11109
11110 value = ktime_to_ns(remaining) * vcpu->arch.virtual_tsc_khz;
11111 do_div(value, 1000000);
11112 return value >> VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE;
11113}
11114
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011115/*
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080011116 * Update the guest state fields of vmcs12 to reflect changes that
11117 * occurred while L2 was running. (The "IA-32e mode guest" bit of the
11118 * VM-entry controls is also updated, since this is really a guest
11119 * state bit.)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011120 */
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080011121static void sync_vmcs12(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011122{
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011123 vmcs12->guest_cr0 = vmcs12_guest_cr0(vcpu, vmcs12);
11124 vmcs12->guest_cr4 = vmcs12_guest_cr4(vcpu, vmcs12);
11125
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011126 vmcs12->guest_rsp = kvm_register_read(vcpu, VCPU_REGS_RSP);
11127 vmcs12->guest_rip = kvm_register_read(vcpu, VCPU_REGS_RIP);
11128 vmcs12->guest_rflags = vmcs_readl(GUEST_RFLAGS);
11129
11130 vmcs12->guest_es_selector = vmcs_read16(GUEST_ES_SELECTOR);
11131 vmcs12->guest_cs_selector = vmcs_read16(GUEST_CS_SELECTOR);
11132 vmcs12->guest_ss_selector = vmcs_read16(GUEST_SS_SELECTOR);
11133 vmcs12->guest_ds_selector = vmcs_read16(GUEST_DS_SELECTOR);
11134 vmcs12->guest_fs_selector = vmcs_read16(GUEST_FS_SELECTOR);
11135 vmcs12->guest_gs_selector = vmcs_read16(GUEST_GS_SELECTOR);
11136 vmcs12->guest_ldtr_selector = vmcs_read16(GUEST_LDTR_SELECTOR);
11137 vmcs12->guest_tr_selector = vmcs_read16(GUEST_TR_SELECTOR);
11138 vmcs12->guest_es_limit = vmcs_read32(GUEST_ES_LIMIT);
11139 vmcs12->guest_cs_limit = vmcs_read32(GUEST_CS_LIMIT);
11140 vmcs12->guest_ss_limit = vmcs_read32(GUEST_SS_LIMIT);
11141 vmcs12->guest_ds_limit = vmcs_read32(GUEST_DS_LIMIT);
11142 vmcs12->guest_fs_limit = vmcs_read32(GUEST_FS_LIMIT);
11143 vmcs12->guest_gs_limit = vmcs_read32(GUEST_GS_LIMIT);
11144 vmcs12->guest_ldtr_limit = vmcs_read32(GUEST_LDTR_LIMIT);
11145 vmcs12->guest_tr_limit = vmcs_read32(GUEST_TR_LIMIT);
11146 vmcs12->guest_gdtr_limit = vmcs_read32(GUEST_GDTR_LIMIT);
11147 vmcs12->guest_idtr_limit = vmcs_read32(GUEST_IDTR_LIMIT);
11148 vmcs12->guest_es_ar_bytes = vmcs_read32(GUEST_ES_AR_BYTES);
11149 vmcs12->guest_cs_ar_bytes = vmcs_read32(GUEST_CS_AR_BYTES);
11150 vmcs12->guest_ss_ar_bytes = vmcs_read32(GUEST_SS_AR_BYTES);
11151 vmcs12->guest_ds_ar_bytes = vmcs_read32(GUEST_DS_AR_BYTES);
11152 vmcs12->guest_fs_ar_bytes = vmcs_read32(GUEST_FS_AR_BYTES);
11153 vmcs12->guest_gs_ar_bytes = vmcs_read32(GUEST_GS_AR_BYTES);
11154 vmcs12->guest_ldtr_ar_bytes = vmcs_read32(GUEST_LDTR_AR_BYTES);
11155 vmcs12->guest_tr_ar_bytes = vmcs_read32(GUEST_TR_AR_BYTES);
11156 vmcs12->guest_es_base = vmcs_readl(GUEST_ES_BASE);
11157 vmcs12->guest_cs_base = vmcs_readl(GUEST_CS_BASE);
11158 vmcs12->guest_ss_base = vmcs_readl(GUEST_SS_BASE);
11159 vmcs12->guest_ds_base = vmcs_readl(GUEST_DS_BASE);
11160 vmcs12->guest_fs_base = vmcs_readl(GUEST_FS_BASE);
11161 vmcs12->guest_gs_base = vmcs_readl(GUEST_GS_BASE);
11162 vmcs12->guest_ldtr_base = vmcs_readl(GUEST_LDTR_BASE);
11163 vmcs12->guest_tr_base = vmcs_readl(GUEST_TR_BASE);
11164 vmcs12->guest_gdtr_base = vmcs_readl(GUEST_GDTR_BASE);
11165 vmcs12->guest_idtr_base = vmcs_readl(GUEST_IDTR_BASE);
11166
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011167 vmcs12->guest_interruptibility_info =
11168 vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
11169 vmcs12->guest_pending_dbg_exceptions =
11170 vmcs_readl(GUEST_PENDING_DBG_EXCEPTIONS);
Jan Kiszka3edf1e62014-01-04 18:47:24 +010011171 if (vcpu->arch.mp_state == KVM_MP_STATE_HALTED)
11172 vmcs12->guest_activity_state = GUEST_ACTIVITY_HLT;
11173 else
11174 vmcs12->guest_activity_state = GUEST_ACTIVITY_ACTIVE;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011175
Jan Kiszkaf41245002014-03-07 20:03:13 +010011176 if (nested_cpu_has_preemption_timer(vmcs12)) {
11177 if (vmcs12->vm_exit_controls &
11178 VM_EXIT_SAVE_VMX_PREEMPTION_TIMER)
11179 vmcs12->vmx_preemption_timer_value =
11180 vmx_get_preemption_timer_value(vcpu);
11181 hrtimer_cancel(&to_vmx(vcpu)->nested.preemption_timer);
11182 }
Arthur Chunqi Li7854cbc2013-09-16 16:11:44 +080011183
Nadav Har'El3633cfc2013-08-05 11:07:07 +030011184 /*
11185 * In some cases (usually, nested EPT), L2 is allowed to change its
11186 * own CR3 without exiting. If it has changed it, we must keep it.
11187 * Of course, if L0 is using shadow page tables, GUEST_CR3 was defined
11188 * by L0, not L1 or L2, so we mustn't unconditionally copy it to vmcs12.
11189 *
11190 * Additionally, restore L2's PDPTR to vmcs12.
11191 */
11192 if (enable_ept) {
Paolo Bonzinif3531052015-12-03 15:49:56 +010011193 vmcs12->guest_cr3 = vmcs_readl(GUEST_CR3);
Nadav Har'El3633cfc2013-08-05 11:07:07 +030011194 vmcs12->guest_pdptr0 = vmcs_read64(GUEST_PDPTR0);
11195 vmcs12->guest_pdptr1 = vmcs_read64(GUEST_PDPTR1);
11196 vmcs12->guest_pdptr2 = vmcs_read64(GUEST_PDPTR2);
11197 vmcs12->guest_pdptr3 = vmcs_read64(GUEST_PDPTR3);
11198 }
11199
Jim Mattsond281e132017-06-01 12:44:46 -070011200 vmcs12->guest_linear_address = vmcs_readl(GUEST_LINEAR_ADDRESS);
Jan Dakinevich119a9c02016-09-04 21:22:47 +030011201
Wincy Van608406e2015-02-03 23:57:51 +080011202 if (nested_cpu_has_vid(vmcs12))
11203 vmcs12->guest_intr_status = vmcs_read16(GUEST_INTR_STATUS);
11204
Jan Kiszkac18911a2013-03-13 16:06:41 +010011205 vmcs12->vm_entry_controls =
11206 (vmcs12->vm_entry_controls & ~VM_ENTRY_IA32E_MODE) |
Gleb Natapov2961e8762013-11-25 15:37:13 +020011207 (vm_entry_controls_get(to_vmx(vcpu)) & VM_ENTRY_IA32E_MODE);
Jan Kiszkac18911a2013-03-13 16:06:41 +010011208
Jan Kiszka2996fca2014-06-16 13:59:43 +020011209 if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_DEBUG_CONTROLS) {
11210 kvm_get_dr(vcpu, 7, (unsigned long *)&vmcs12->guest_dr7);
11211 vmcs12->guest_ia32_debugctl = vmcs_read64(GUEST_IA32_DEBUGCTL);
11212 }
11213
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011214 /* TODO: These cannot have changed unless we have MSR bitmaps and
11215 * the relevant bit asks not to trap the change */
Jan Kiszkab8c07d52013-04-06 13:51:21 +020011216 if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_IA32_PAT)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011217 vmcs12->guest_ia32_pat = vmcs_read64(GUEST_IA32_PAT);
Jan Kiszka10ba54a2013-08-08 16:26:31 +020011218 if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_IA32_EFER)
11219 vmcs12->guest_ia32_efer = vcpu->arch.efer;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011220 vmcs12->guest_sysenter_cs = vmcs_read32(GUEST_SYSENTER_CS);
11221 vmcs12->guest_sysenter_esp = vmcs_readl(GUEST_SYSENTER_ESP);
11222 vmcs12->guest_sysenter_eip = vmcs_readl(GUEST_SYSENTER_EIP);
Paolo Bonzinia87036a2016-03-08 09:52:13 +010011223 if (kvm_mpx_supported())
Paolo Bonzini36be0b92014-02-24 12:30:04 +010011224 vmcs12->guest_bndcfgs = vmcs_read64(GUEST_BNDCFGS);
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080011225}
11226
11227/*
11228 * prepare_vmcs12 is part of what we need to do when the nested L2 guest exits
11229 * and we want to prepare to run its L1 parent. L1 keeps a vmcs for L2 (vmcs12),
11230 * and this function updates it to reflect the changes to the guest state while
11231 * L2 was running (and perhaps made some exits which were handled directly by L0
11232 * without going back to L1), and to reflect the exit reason.
11233 * Note that we do not have to copy here all VMCS fields, just those that
11234 * could have changed by the L2 guest or the exit - i.e., the guest-state and
11235 * exit-information fields only. Other fields are modified by L1 with VMWRITE,
11236 * which already writes to vmcs12 directly.
11237 */
11238static void prepare_vmcs12(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12,
11239 u32 exit_reason, u32 exit_intr_info,
11240 unsigned long exit_qualification)
11241{
11242 /* update guest state fields: */
11243 sync_vmcs12(vcpu, vmcs12);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011244
11245 /* update exit information fields: */
11246
Jan Kiszka533558b2014-01-04 18:47:20 +010011247 vmcs12->vm_exit_reason = exit_reason;
11248 vmcs12->exit_qualification = exit_qualification;
Jan Kiszka533558b2014-01-04 18:47:20 +010011249 vmcs12->vm_exit_intr_info = exit_intr_info;
Paolo Bonzini7313c692017-07-27 10:31:25 +020011250
Jan Kiszka5f3d5792013-04-14 12:12:46 +020011251 vmcs12->idt_vectoring_info_field = 0;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011252 vmcs12->vm_exit_instruction_len = vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
11253 vmcs12->vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
11254
Jan Kiszka5f3d5792013-04-14 12:12:46 +020011255 if (!(vmcs12->vm_exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY)) {
Jim Mattson7cdc2d62017-07-06 16:33:05 -070011256 vmcs12->launch_state = 1;
11257
Jan Kiszka5f3d5792013-04-14 12:12:46 +020011258 /* vm_entry_intr_info_field is cleared on exit. Emulate this
11259 * instead of reading the real value. */
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011260 vmcs12->vm_entry_intr_info_field &= ~INTR_INFO_VALID_MASK;
Jan Kiszka5f3d5792013-04-14 12:12:46 +020011261
11262 /*
11263 * Transfer the event that L0 or L1 may wanted to inject into
11264 * L2 to IDT_VECTORING_INFO_FIELD.
11265 */
11266 vmcs12_save_pending_event(vcpu, vmcs12);
11267 }
11268
11269 /*
11270 * Drop what we picked up for L2 via vmx_complete_interrupts. It is
11271 * preserved above and would only end up incorrectly in L1.
11272 */
11273 vcpu->arch.nmi_injected = false;
11274 kvm_clear_exception_queue(vcpu);
11275 kvm_clear_interrupt_queue(vcpu);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011276}
11277
Wanpeng Li5af41572017-11-05 16:54:49 -080011278static void load_vmcs12_mmu_host_state(struct kvm_vcpu *vcpu,
11279 struct vmcs12 *vmcs12)
11280{
11281 u32 entry_failure_code;
11282
11283 nested_ept_uninit_mmu_context(vcpu);
11284
11285 /*
11286 * Only PDPTE load can fail as the value of cr3 was checked on entry and
11287 * couldn't have changed.
11288 */
11289 if (nested_vmx_load_cr3(vcpu, vmcs12->host_cr3, false, &entry_failure_code))
11290 nested_vmx_abort(vcpu, VMX_ABORT_LOAD_HOST_PDPTE_FAIL);
11291
11292 if (!enable_ept)
11293 vcpu->arch.walk_mmu->inject_page_fault = kvm_inject_page_fault;
11294}
11295
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011296/*
11297 * A part of what we need to when the nested L2 guest exits and we want to
11298 * run its L1 parent, is to reset L1's guest state to the host state specified
11299 * in vmcs12.
11300 * This function is to be called not only on normal nested exit, but also on
11301 * a nested entry failure, as explained in Intel's spec, 3B.23.7 ("VM-Entry
11302 * Failures During or After Loading Guest State").
11303 * This function should be called when the active VMCS is L1's (vmcs01).
11304 */
Jan Kiszka733568f2013-02-23 15:07:47 +010011305static void load_vmcs12_host_state(struct kvm_vcpu *vcpu,
11306 struct vmcs12 *vmcs12)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011307{
Arthur Chunqi Li21feb4e2013-07-15 16:04:08 +080011308 struct kvm_segment seg;
11309
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011310 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_EFER)
11311 vcpu->arch.efer = vmcs12->host_ia32_efer;
Jan Kiszkad1fa0352013-04-14 12:44:54 +020011312 else if (vmcs12->vm_exit_controls & VM_EXIT_HOST_ADDR_SPACE_SIZE)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011313 vcpu->arch.efer |= (EFER_LMA | EFER_LME);
11314 else
11315 vcpu->arch.efer &= ~(EFER_LMA | EFER_LME);
11316 vmx_set_efer(vcpu, vcpu->arch.efer);
11317
11318 kvm_register_write(vcpu, VCPU_REGS_RSP, vmcs12->host_rsp);
11319 kvm_register_write(vcpu, VCPU_REGS_RIP, vmcs12->host_rip);
H. Peter Anvin1adfa762013-04-27 16:10:11 -070011320 vmx_set_rflags(vcpu, X86_EFLAGS_FIXED);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011321 /*
11322 * Note that calling vmx_set_cr0 is important, even if cr0 hasn't
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -080011323 * actually changed, because vmx_set_cr0 refers to efer set above.
11324 *
11325 * CR0_GUEST_HOST_MASK is already set in the original vmcs01
11326 * (KVM doesn't change it);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011327 */
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -080011328 vcpu->arch.cr0_guest_owned_bits = X86_CR0_TS;
Jan Kiszka9e3e4db2013-09-03 21:11:45 +020011329 vmx_set_cr0(vcpu, vmcs12->host_cr0);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011330
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -080011331 /* Same as above - no reason to call set_cr4_guest_host_mask(). */
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011332 vcpu->arch.cr4_guest_owned_bits = ~vmcs_readl(CR4_GUEST_HOST_MASK);
Haozhong Zhang8eb3f872017-10-10 15:01:22 +080011333 vmx_set_cr4(vcpu, vmcs12->host_cr4);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011334
Wanpeng Li5af41572017-11-05 16:54:49 -080011335 load_vmcs12_mmu_host_state(vcpu, vmcs12);
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +030011336
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011337 if (enable_vpid) {
11338 /*
11339 * Trivially support vpid by letting L2s share their parent
11340 * L1's vpid. TODO: move to a more elaborate solution, giving
11341 * each L2 its own vpid and exposing the vpid feature to L1.
11342 */
Wanpeng Lic2ba05c2017-12-12 17:33:03 -080011343 vmx_flush_tlb(vcpu, true);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011344 }
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011345
11346 vmcs_write32(GUEST_SYSENTER_CS, vmcs12->host_ia32_sysenter_cs);
11347 vmcs_writel(GUEST_SYSENTER_ESP, vmcs12->host_ia32_sysenter_esp);
11348 vmcs_writel(GUEST_SYSENTER_EIP, vmcs12->host_ia32_sysenter_eip);
11349 vmcs_writel(GUEST_IDTR_BASE, vmcs12->host_idtr_base);
11350 vmcs_writel(GUEST_GDTR_BASE, vmcs12->host_gdtr_base);
Ladi Prosek21f2d5512017-10-11 16:54:42 +020011351 vmcs_write32(GUEST_IDTR_LIMIT, 0xFFFF);
11352 vmcs_write32(GUEST_GDTR_LIMIT, 0xFFFF);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011353
Paolo Bonzini36be0b92014-02-24 12:30:04 +010011354 /* If not VM_EXIT_CLEAR_BNDCFGS, the L2 value propagates to L1. */
11355 if (vmcs12->vm_exit_controls & VM_EXIT_CLEAR_BNDCFGS)
11356 vmcs_write64(GUEST_BNDCFGS, 0);
11357
Jan Kiszka44811c02013-08-04 17:17:27 +020011358 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PAT) {
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011359 vmcs_write64(GUEST_IA32_PAT, vmcs12->host_ia32_pat);
Jan Kiszka44811c02013-08-04 17:17:27 +020011360 vcpu->arch.pat = vmcs12->host_ia32_pat;
11361 }
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011362 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL)
11363 vmcs_write64(GUEST_IA32_PERF_GLOBAL_CTRL,
11364 vmcs12->host_ia32_perf_global_ctrl);
Jan Kiszka503cd0c2013-03-03 13:05:44 +010011365
Arthur Chunqi Li21feb4e2013-07-15 16:04:08 +080011366 /* Set L1 segment info according to Intel SDM
11367 27.5.2 Loading Host Segment and Descriptor-Table Registers */
11368 seg = (struct kvm_segment) {
11369 .base = 0,
11370 .limit = 0xFFFFFFFF,
11371 .selector = vmcs12->host_cs_selector,
11372 .type = 11,
11373 .present = 1,
11374 .s = 1,
11375 .g = 1
11376 };
11377 if (vmcs12->vm_exit_controls & VM_EXIT_HOST_ADDR_SPACE_SIZE)
11378 seg.l = 1;
11379 else
11380 seg.db = 1;
11381 vmx_set_segment(vcpu, &seg, VCPU_SREG_CS);
11382 seg = (struct kvm_segment) {
11383 .base = 0,
11384 .limit = 0xFFFFFFFF,
11385 .type = 3,
11386 .present = 1,
11387 .s = 1,
11388 .db = 1,
11389 .g = 1
11390 };
11391 seg.selector = vmcs12->host_ds_selector;
11392 vmx_set_segment(vcpu, &seg, VCPU_SREG_DS);
11393 seg.selector = vmcs12->host_es_selector;
11394 vmx_set_segment(vcpu, &seg, VCPU_SREG_ES);
11395 seg.selector = vmcs12->host_ss_selector;
11396 vmx_set_segment(vcpu, &seg, VCPU_SREG_SS);
11397 seg.selector = vmcs12->host_fs_selector;
11398 seg.base = vmcs12->host_fs_base;
11399 vmx_set_segment(vcpu, &seg, VCPU_SREG_FS);
11400 seg.selector = vmcs12->host_gs_selector;
11401 seg.base = vmcs12->host_gs_base;
11402 vmx_set_segment(vcpu, &seg, VCPU_SREG_GS);
11403 seg = (struct kvm_segment) {
Gleb Natapov205befd2013-08-04 15:08:06 +030011404 .base = vmcs12->host_tr_base,
Arthur Chunqi Li21feb4e2013-07-15 16:04:08 +080011405 .limit = 0x67,
11406 .selector = vmcs12->host_tr_selector,
11407 .type = 11,
11408 .present = 1
11409 };
11410 vmx_set_segment(vcpu, &seg, VCPU_SREG_TR);
11411
Jan Kiszka503cd0c2013-03-03 13:05:44 +010011412 kvm_set_dr(vcpu, 7, 0x400);
11413 vmcs_write64(GUEST_IA32_DEBUGCTL, 0);
Wincy Vanff651cb2014-12-11 08:52:58 +030011414
Wincy Van3af18d92015-02-03 23:49:31 +080011415 if (cpu_has_vmx_msr_bitmap())
11416 vmx_set_msr_bitmap(vcpu);
11417
Wincy Vanff651cb2014-12-11 08:52:58 +030011418 if (nested_vmx_load_msr(vcpu, vmcs12->vm_exit_msr_load_addr,
11419 vmcs12->vm_exit_msr_load_count))
11420 nested_vmx_abort(vcpu, VMX_ABORT_LOAD_HOST_MSR_FAIL);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011421}
11422
11423/*
11424 * Emulate an exit from nested guest (L2) to L1, i.e., prepare to run L1
11425 * and modify vmcs12 to make it see what it would expect to see there if
11426 * L2 was its real guest. Must only be called when in L2 (is_guest_mode())
11427 */
Jan Kiszka533558b2014-01-04 18:47:20 +010011428static void nested_vmx_vmexit(struct kvm_vcpu *vcpu, u32 exit_reason,
11429 u32 exit_intr_info,
11430 unsigned long exit_qualification)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011431{
11432 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011433 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
11434
Jan Kiszka5f3d5792013-04-14 12:12:46 +020011435 /* trying to cancel vmlaunch/vmresume is a bug */
11436 WARN_ON_ONCE(vmx->nested.nested_run_pending);
11437
Wanpeng Li6550c4d2017-07-31 19:25:27 -070011438 /*
Jim Mattson4f350c62017-09-14 16:31:44 -070011439 * The only expected VM-instruction error is "VM entry with
11440 * invalid control field(s)." Anything else indicates a
11441 * problem with L0.
Wanpeng Li6550c4d2017-07-31 19:25:27 -070011442 */
Jim Mattson4f350c62017-09-14 16:31:44 -070011443 WARN_ON_ONCE(vmx->fail && (vmcs_read32(VM_INSTRUCTION_ERROR) !=
11444 VMXERR_ENTRY_INVALID_CONTROL_FIELD));
11445
11446 leave_guest_mode(vcpu);
11447
11448 if (likely(!vmx->fail)) {
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020011449 if (exit_reason == -1)
11450 sync_vmcs12(vcpu, vmcs12);
11451 else
11452 prepare_vmcs12(vcpu, vmcs12, exit_reason, exit_intr_info,
11453 exit_qualification);
Jim Mattson4f350c62017-09-14 16:31:44 -070011454
11455 if (nested_vmx_store_msr(vcpu, vmcs12->vm_exit_msr_store_addr,
11456 vmcs12->vm_exit_msr_store_count))
11457 nested_vmx_abort(vcpu, VMX_ABORT_SAVE_GUEST_MSR_FAIL);
Bandan Das77b0f5d2014-04-19 18:17:45 -040011458 }
11459
Jim Mattson4f350c62017-09-14 16:31:44 -070011460 vmx_switch_vmcs(vcpu, &vmx->vmcs01);
Paolo Bonzini8391ce42016-07-07 14:58:33 +020011461 vm_entry_controls_reset_shadow(vmx);
11462 vm_exit_controls_reset_shadow(vmx);
Jan Kiszka36c3cc42013-02-23 22:35:37 +010011463 vmx_segment_cache_clear(vmx);
11464
Paolo Bonzini9314006db2016-07-06 13:23:51 +020011465 /* Update any VMCS fields that might have changed while L2 ran */
Jim Mattson83bafef2016-10-04 10:48:38 -070011466 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, vmx->msr_autoload.nr);
11467 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, vmx->msr_autoload.nr);
Paolo Bonziniea26e4e2016-11-01 00:39:48 +010011468 vmcs_write64(TSC_OFFSET, vcpu->arch.tsc_offset);
Paolo Bonzini9314006db2016-07-06 13:23:51 +020011469 if (vmx->hv_deadline_tsc == -1)
11470 vmcs_clear_bits(PIN_BASED_VM_EXEC_CONTROL,
11471 PIN_BASED_VMX_PREEMPTION_TIMER);
11472 else
11473 vmcs_set_bits(PIN_BASED_VM_EXEC_CONTROL,
11474 PIN_BASED_VMX_PREEMPTION_TIMER);
Peter Feinerc95ba922016-08-17 09:36:47 -070011475 if (kvm_has_tsc_control)
11476 decache_tsc_multiplier(vmx);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011477
Radim Krčmářdccbfcf2016-08-08 20:16:23 +020011478 if (vmx->nested.change_vmcs01_virtual_x2apic_mode) {
11479 vmx->nested.change_vmcs01_virtual_x2apic_mode = false;
11480 vmx_set_virtual_x2apic_mode(vcpu,
11481 vcpu->arch.apic_base & X2APIC_ENABLE);
Jim Mattsonfb6c8192017-03-16 13:53:59 -070011482 } else if (!nested_cpu_has_ept(vmcs12) &&
11483 nested_cpu_has2(vmcs12,
11484 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) {
11485 vmx_flush_tlb_ept_only(vcpu);
Radim Krčmářdccbfcf2016-08-08 20:16:23 +020011486 }
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011487
11488 /* This is needed for same reason as it was needed in prepare_vmcs02 */
11489 vmx->host_rsp = 0;
11490
11491 /* Unpin physical memory we referred to in vmcs02 */
11492 if (vmx->nested.apic_access_page) {
David Hildenbrand53a70da2017-08-03 18:11:05 +020011493 kvm_release_page_dirty(vmx->nested.apic_access_page);
Paolo Bonzini48d89b92014-08-26 13:27:46 +020011494 vmx->nested.apic_access_page = NULL;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011495 }
Wanpeng Lia7c0b072014-08-21 19:46:50 +080011496 if (vmx->nested.virtual_apic_page) {
David Hildenbrand53a70da2017-08-03 18:11:05 +020011497 kvm_release_page_dirty(vmx->nested.virtual_apic_page);
Paolo Bonzini48d89b92014-08-26 13:27:46 +020011498 vmx->nested.virtual_apic_page = NULL;
Wanpeng Lia7c0b072014-08-21 19:46:50 +080011499 }
Wincy Van705699a2015-02-03 23:58:17 +080011500 if (vmx->nested.pi_desc_page) {
11501 kunmap(vmx->nested.pi_desc_page);
David Hildenbrand53a70da2017-08-03 18:11:05 +020011502 kvm_release_page_dirty(vmx->nested.pi_desc_page);
Wincy Van705699a2015-02-03 23:58:17 +080011503 vmx->nested.pi_desc_page = NULL;
11504 vmx->nested.pi_desc = NULL;
11505 }
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011506
11507 /*
Tang Chen38b99172014-09-24 15:57:54 +080011508 * We are now running in L2, mmu_notifier will force to reload the
11509 * page's hpa for L2 vmcs. Need to reload it for L1 before entering L1.
11510 */
Wanpeng Lic83b6d12016-09-06 17:20:33 +080011511 kvm_make_request(KVM_REQ_APIC_PAGE_RELOAD, vcpu);
Tang Chen38b99172014-09-24 15:57:54 +080011512
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020011513 if (enable_shadow_vmcs && exit_reason != -1)
Abel Gordon012f83c2013-04-18 14:39:25 +030011514 vmx->nested.sync_shadow_vmcs = true;
Jan Kiszkab6b8a142014-03-07 20:03:12 +010011515
11516 /* in case we halted in L2 */
11517 vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
Jim Mattson4f350c62017-09-14 16:31:44 -070011518
11519 if (likely(!vmx->fail)) {
11520 /*
11521 * TODO: SDM says that with acknowledge interrupt on
11522 * exit, bit 31 of the VM-exit interrupt information
11523 * (valid interrupt) is always set to 1 on
11524 * EXIT_REASON_EXTERNAL_INTERRUPT, so we shouldn't
11525 * need kvm_cpu_has_interrupt(). See the commit
11526 * message for details.
11527 */
11528 if (nested_exit_intr_ack_set(vcpu) &&
11529 exit_reason == EXIT_REASON_EXTERNAL_INTERRUPT &&
11530 kvm_cpu_has_interrupt(vcpu)) {
11531 int irq = kvm_cpu_get_interrupt(vcpu);
11532 WARN_ON(irq < 0);
11533 vmcs12->vm_exit_intr_info = irq |
11534 INTR_INFO_VALID_MASK | INTR_TYPE_EXT_INTR;
11535 }
11536
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020011537 if (exit_reason != -1)
11538 trace_kvm_nested_vmexit_inject(vmcs12->vm_exit_reason,
11539 vmcs12->exit_qualification,
11540 vmcs12->idt_vectoring_info_field,
11541 vmcs12->vm_exit_intr_info,
11542 vmcs12->vm_exit_intr_error_code,
11543 KVM_ISA_VMX);
Jim Mattson4f350c62017-09-14 16:31:44 -070011544
11545 load_vmcs12_host_state(vcpu, vmcs12);
11546
11547 return;
11548 }
11549
11550 /*
11551 * After an early L2 VM-entry failure, we're now back
11552 * in L1 which thinks it just finished a VMLAUNCH or
11553 * VMRESUME instruction, so we need to set the failure
11554 * flag and the VM-instruction error field of the VMCS
11555 * accordingly.
11556 */
11557 nested_vmx_failValid(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD);
Wanpeng Li5af41572017-11-05 16:54:49 -080011558
11559 load_vmcs12_mmu_host_state(vcpu, vmcs12);
11560
Jim Mattson4f350c62017-09-14 16:31:44 -070011561 /*
11562 * The emulated instruction was already skipped in
11563 * nested_vmx_run, but the updated RIP was never
11564 * written back to the vmcs01.
11565 */
11566 skip_emulated_instruction(vcpu);
11567 vmx->fail = 0;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011568}
11569
Nadav Har'El7c177932011-05-25 23:12:04 +030011570/*
Jan Kiszka42124922014-01-04 18:47:19 +010011571 * Forcibly leave nested mode in order to be able to reset the VCPU later on.
11572 */
11573static void vmx_leave_nested(struct kvm_vcpu *vcpu)
11574{
Wanpeng Li2f707d92017-03-06 04:03:28 -080011575 if (is_guest_mode(vcpu)) {
11576 to_vmx(vcpu)->nested.nested_run_pending = 0;
Jan Kiszka533558b2014-01-04 18:47:20 +010011577 nested_vmx_vmexit(vcpu, -1, 0, 0);
Wanpeng Li2f707d92017-03-06 04:03:28 -080011578 }
Jan Kiszka42124922014-01-04 18:47:19 +010011579 free_nested(to_vmx(vcpu));
11580}
11581
11582/*
Nadav Har'El7c177932011-05-25 23:12:04 +030011583 * L1's failure to enter L2 is a subset of a normal exit, as explained in
11584 * 23.7 "VM-entry failures during or after loading guest state" (this also
11585 * lists the acceptable exit-reason and exit-qualification parameters).
11586 * It should only be called before L2 actually succeeded to run, and when
11587 * vmcs01 is current (it doesn't leave_guest_mode() or switch vmcss).
11588 */
11589static void nested_vmx_entry_failure(struct kvm_vcpu *vcpu,
11590 struct vmcs12 *vmcs12,
11591 u32 reason, unsigned long qualification)
11592{
11593 load_vmcs12_host_state(vcpu, vmcs12);
11594 vmcs12->vm_exit_reason = reason | VMX_EXIT_REASONS_FAILED_VMENTRY;
11595 vmcs12->exit_qualification = qualification;
11596 nested_vmx_succeed(vcpu);
Abel Gordon012f83c2013-04-18 14:39:25 +030011597 if (enable_shadow_vmcs)
11598 to_vmx(vcpu)->nested.sync_shadow_vmcs = true;
Nadav Har'El7c177932011-05-25 23:12:04 +030011599}
11600
Joerg Roedel8a76d7f2011-04-04 12:39:27 +020011601static int vmx_check_intercept(struct kvm_vcpu *vcpu,
11602 struct x86_instruction_info *info,
11603 enum x86_intercept_stage stage)
11604{
Paolo Bonzinifb6d4d32016-07-12 11:04:26 +020011605 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
11606 struct x86_emulate_ctxt *ctxt = &vcpu->arch.emulate_ctxt;
11607
11608 /*
11609 * RDPID causes #UD if disabled through secondary execution controls.
11610 * Because it is marked as EmulateOnUD, we need to intercept it here.
11611 */
11612 if (info->intercept == x86_intercept_rdtscp &&
11613 !nested_cpu_has2(vmcs12, SECONDARY_EXEC_RDTSCP)) {
11614 ctxt->exception.vector = UD_VECTOR;
11615 ctxt->exception.error_code_valid = false;
11616 return X86EMUL_PROPAGATE_FAULT;
11617 }
11618
11619 /* TODO: check more intercepts... */
Joerg Roedel8a76d7f2011-04-04 12:39:27 +020011620 return X86EMUL_CONTINUE;
11621}
11622
Yunhong Jiang64672c92016-06-13 14:19:59 -070011623#ifdef CONFIG_X86_64
11624/* (a << shift) / divisor, return 1 if overflow otherwise 0 */
11625static inline int u64_shl_div_u64(u64 a, unsigned int shift,
11626 u64 divisor, u64 *result)
11627{
11628 u64 low = a << shift, high = a >> (64 - shift);
11629
11630 /* To avoid the overflow on divq */
11631 if (high >= divisor)
11632 return 1;
11633
11634 /* Low hold the result, high hold rem which is discarded */
11635 asm("divq %2\n\t" : "=a" (low), "=d" (high) :
11636 "rm" (divisor), "0" (low), "1" (high));
11637 *result = low;
11638
11639 return 0;
11640}
11641
11642static int vmx_set_hv_timer(struct kvm_vcpu *vcpu, u64 guest_deadline_tsc)
11643{
11644 struct vcpu_vmx *vmx = to_vmx(vcpu);
Paolo Bonzini9175d2e2016-06-27 15:08:01 +020011645 u64 tscl = rdtsc();
11646 u64 guest_tscl = kvm_read_l1_tsc(vcpu, tscl);
11647 u64 delta_tsc = max(guest_deadline_tsc, guest_tscl) - guest_tscl;
Yunhong Jiang64672c92016-06-13 14:19:59 -070011648
11649 /* Convert to host delta tsc if tsc scaling is enabled */
11650 if (vcpu->arch.tsc_scaling_ratio != kvm_default_tsc_scaling_ratio &&
11651 u64_shl_div_u64(delta_tsc,
11652 kvm_tsc_scaling_ratio_frac_bits,
11653 vcpu->arch.tsc_scaling_ratio,
11654 &delta_tsc))
11655 return -ERANGE;
11656
11657 /*
11658 * If the delta tsc can't fit in the 32 bit after the multi shift,
11659 * we can't use the preemption timer.
11660 * It's possible that it fits on later vmentries, but checking
11661 * on every vmentry is costly so we just use an hrtimer.
11662 */
11663 if (delta_tsc >> (cpu_preemption_timer_multi + 32))
11664 return -ERANGE;
11665
11666 vmx->hv_deadline_tsc = tscl + delta_tsc;
11667 vmcs_set_bits(PIN_BASED_VM_EXEC_CONTROL,
11668 PIN_BASED_VMX_PREEMPTION_TIMER);
Wanpeng Lic8533542017-06-29 06:28:09 -070011669
11670 return delta_tsc == 0;
Yunhong Jiang64672c92016-06-13 14:19:59 -070011671}
11672
11673static void vmx_cancel_hv_timer(struct kvm_vcpu *vcpu)
11674{
11675 struct vcpu_vmx *vmx = to_vmx(vcpu);
11676 vmx->hv_deadline_tsc = -1;
11677 vmcs_clear_bits(PIN_BASED_VM_EXEC_CONTROL,
11678 PIN_BASED_VMX_PREEMPTION_TIMER);
11679}
11680#endif
11681
Paolo Bonzini48d89b92014-08-26 13:27:46 +020011682static void vmx_sched_in(struct kvm_vcpu *vcpu, int cpu)
Radim Krčmářae97a3b2014-08-21 18:08:06 +020011683{
Radim Krčmářb4a2d312014-08-21 18:08:08 +020011684 if (ple_gap)
11685 shrink_ple_window(vcpu);
Radim Krčmářae97a3b2014-08-21 18:08:06 +020011686}
11687
Kai Huang843e4332015-01-28 10:54:28 +080011688static void vmx_slot_enable_log_dirty(struct kvm *kvm,
11689 struct kvm_memory_slot *slot)
11690{
11691 kvm_mmu_slot_leaf_clear_dirty(kvm, slot);
11692 kvm_mmu_slot_largepage_remove_write_access(kvm, slot);
11693}
11694
11695static void vmx_slot_disable_log_dirty(struct kvm *kvm,
11696 struct kvm_memory_slot *slot)
11697{
11698 kvm_mmu_slot_set_dirty(kvm, slot);
11699}
11700
11701static void vmx_flush_log_dirty(struct kvm *kvm)
11702{
11703 kvm_flush_pml_buffers(kvm);
11704}
11705
Bandan Dasc5f983f2017-05-05 15:25:14 -040011706static int vmx_write_pml_buffer(struct kvm_vcpu *vcpu)
11707{
11708 struct vmcs12 *vmcs12;
11709 struct vcpu_vmx *vmx = to_vmx(vcpu);
11710 gpa_t gpa;
11711 struct page *page = NULL;
11712 u64 *pml_address;
11713
11714 if (is_guest_mode(vcpu)) {
11715 WARN_ON_ONCE(vmx->nested.pml_full);
11716
11717 /*
11718 * Check if PML is enabled for the nested guest.
11719 * Whether eptp bit 6 is set is already checked
11720 * as part of A/D emulation.
11721 */
11722 vmcs12 = get_vmcs12(vcpu);
11723 if (!nested_cpu_has_pml(vmcs12))
11724 return 0;
11725
Dan Carpenter47698862017-05-10 22:43:17 +030011726 if (vmcs12->guest_pml_index >= PML_ENTITY_NUM) {
Bandan Dasc5f983f2017-05-05 15:25:14 -040011727 vmx->nested.pml_full = true;
11728 return 1;
11729 }
11730
11731 gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS) & ~0xFFFull;
11732
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020011733 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->pml_address);
11734 if (is_error_page(page))
Bandan Dasc5f983f2017-05-05 15:25:14 -040011735 return 0;
11736
11737 pml_address = kmap(page);
11738 pml_address[vmcs12->guest_pml_index--] = gpa;
11739 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +020011740 kvm_release_page_clean(page);
Bandan Dasc5f983f2017-05-05 15:25:14 -040011741 }
11742
11743 return 0;
11744}
11745
Kai Huang843e4332015-01-28 10:54:28 +080011746static void vmx_enable_log_dirty_pt_masked(struct kvm *kvm,
11747 struct kvm_memory_slot *memslot,
11748 gfn_t offset, unsigned long mask)
11749{
11750 kvm_mmu_clear_dirty_pt_masked(kvm, memslot, offset, mask);
11751}
11752
Paolo Bonzinicd39e112017-06-06 12:57:04 +020011753static void __pi_post_block(struct kvm_vcpu *vcpu)
11754{
11755 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
11756 struct pi_desc old, new;
11757 unsigned int dest;
Paolo Bonzinicd39e112017-06-06 12:57:04 +020011758
11759 do {
11760 old.control = new.control = pi_desc->control;
Paolo Bonzini8b306e22017-06-06 12:57:05 +020011761 WARN(old.nv != POSTED_INTR_WAKEUP_VECTOR,
11762 "Wakeup handler not enabled while the VCPU is blocked\n");
Paolo Bonzinicd39e112017-06-06 12:57:04 +020011763
11764 dest = cpu_physical_id(vcpu->cpu);
11765
11766 if (x2apic_enabled())
11767 new.ndst = dest;
11768 else
11769 new.ndst = (dest << 8) & 0xFF00;
11770
Paolo Bonzinicd39e112017-06-06 12:57:04 +020011771 /* set 'NV' to 'notification vector' */
11772 new.nv = POSTED_INTR_VECTOR;
Paolo Bonzinic0a16662017-09-28 17:58:41 +020011773 } while (cmpxchg64(&pi_desc->control, old.control,
11774 new.control) != old.control);
Paolo Bonzinicd39e112017-06-06 12:57:04 +020011775
Paolo Bonzini8b306e22017-06-06 12:57:05 +020011776 if (!WARN_ON_ONCE(vcpu->pre_pcpu == -1)) {
11777 spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
Paolo Bonzinicd39e112017-06-06 12:57:04 +020011778 list_del(&vcpu->blocked_vcpu_list);
Paolo Bonzini8b306e22017-06-06 12:57:05 +020011779 spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
Paolo Bonzinicd39e112017-06-06 12:57:04 +020011780 vcpu->pre_pcpu = -1;
11781 }
11782}
11783
Feng Wuefc64402015-09-18 22:29:51 +080011784/*
Feng Wubf9f6ac2015-09-18 22:29:55 +080011785 * This routine does the following things for vCPU which is going
11786 * to be blocked if VT-d PI is enabled.
11787 * - Store the vCPU to the wakeup list, so when interrupts happen
11788 * we can find the right vCPU to wake up.
11789 * - Change the Posted-interrupt descriptor as below:
11790 * 'NDST' <-- vcpu->pre_pcpu
11791 * 'NV' <-- POSTED_INTR_WAKEUP_VECTOR
11792 * - If 'ON' is set during this process, which means at least one
11793 * interrupt is posted for this vCPU, we cannot block it, in
11794 * this case, return 1, otherwise, return 0.
11795 *
11796 */
Yunhong Jiangbc225122016-06-13 14:19:58 -070011797static int pi_pre_block(struct kvm_vcpu *vcpu)
Feng Wubf9f6ac2015-09-18 22:29:55 +080011798{
Feng Wubf9f6ac2015-09-18 22:29:55 +080011799 unsigned int dest;
11800 struct pi_desc old, new;
11801 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
11802
11803 if (!kvm_arch_has_assigned_device(vcpu->kvm) ||
Yang Zhanga0052192016-06-13 09:56:56 +080011804 !irq_remapping_cap(IRQ_POSTING_CAP) ||
11805 !kvm_vcpu_apicv_active(vcpu))
Feng Wubf9f6ac2015-09-18 22:29:55 +080011806 return 0;
11807
Paolo Bonzini8b306e22017-06-06 12:57:05 +020011808 WARN_ON(irqs_disabled());
11809 local_irq_disable();
11810 if (!WARN_ON_ONCE(vcpu->pre_pcpu != -1)) {
11811 vcpu->pre_pcpu = vcpu->cpu;
11812 spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
11813 list_add_tail(&vcpu->blocked_vcpu_list,
11814 &per_cpu(blocked_vcpu_on_cpu,
11815 vcpu->pre_pcpu));
11816 spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
11817 }
Feng Wubf9f6ac2015-09-18 22:29:55 +080011818
11819 do {
11820 old.control = new.control = pi_desc->control;
11821
Feng Wubf9f6ac2015-09-18 22:29:55 +080011822 WARN((pi_desc->sn == 1),
11823 "Warning: SN field of posted-interrupts "
11824 "is set before blocking\n");
11825
11826 /*
11827 * Since vCPU can be preempted during this process,
11828 * vcpu->cpu could be different with pre_pcpu, we
11829 * need to set pre_pcpu as the destination of wakeup
11830 * notification event, then we can find the right vCPU
11831 * to wakeup in wakeup handler if interrupts happen
11832 * when the vCPU is in blocked state.
11833 */
11834 dest = cpu_physical_id(vcpu->pre_pcpu);
11835
11836 if (x2apic_enabled())
11837 new.ndst = dest;
11838 else
11839 new.ndst = (dest << 8) & 0xFF00;
11840
11841 /* set 'NV' to 'wakeup vector' */
11842 new.nv = POSTED_INTR_WAKEUP_VECTOR;
Paolo Bonzinic0a16662017-09-28 17:58:41 +020011843 } while (cmpxchg64(&pi_desc->control, old.control,
11844 new.control) != old.control);
Feng Wubf9f6ac2015-09-18 22:29:55 +080011845
Paolo Bonzini8b306e22017-06-06 12:57:05 +020011846 /* We should not block the vCPU if an interrupt is posted for it. */
11847 if (pi_test_on(pi_desc) == 1)
11848 __pi_post_block(vcpu);
11849
11850 local_irq_enable();
11851 return (vcpu->pre_pcpu == -1);
Feng Wubf9f6ac2015-09-18 22:29:55 +080011852}
11853
Yunhong Jiangbc225122016-06-13 14:19:58 -070011854static int vmx_pre_block(struct kvm_vcpu *vcpu)
11855{
11856 if (pi_pre_block(vcpu))
11857 return 1;
11858
Yunhong Jiang64672c92016-06-13 14:19:59 -070011859 if (kvm_lapic_hv_timer_in_use(vcpu))
11860 kvm_lapic_switch_to_sw_timer(vcpu);
11861
Yunhong Jiangbc225122016-06-13 14:19:58 -070011862 return 0;
11863}
11864
11865static void pi_post_block(struct kvm_vcpu *vcpu)
Feng Wubf9f6ac2015-09-18 22:29:55 +080011866{
Paolo Bonzini8b306e22017-06-06 12:57:05 +020011867 if (vcpu->pre_pcpu == -1)
Feng Wubf9f6ac2015-09-18 22:29:55 +080011868 return;
11869
Paolo Bonzini8b306e22017-06-06 12:57:05 +020011870 WARN_ON(irqs_disabled());
11871 local_irq_disable();
Paolo Bonzinicd39e112017-06-06 12:57:04 +020011872 __pi_post_block(vcpu);
Paolo Bonzini8b306e22017-06-06 12:57:05 +020011873 local_irq_enable();
Feng Wubf9f6ac2015-09-18 22:29:55 +080011874}
11875
Yunhong Jiangbc225122016-06-13 14:19:58 -070011876static void vmx_post_block(struct kvm_vcpu *vcpu)
11877{
Yunhong Jiang64672c92016-06-13 14:19:59 -070011878 if (kvm_x86_ops->set_hv_timer)
11879 kvm_lapic_switch_to_hv_timer(vcpu);
11880
Yunhong Jiangbc225122016-06-13 14:19:58 -070011881 pi_post_block(vcpu);
11882}
11883
Feng Wubf9f6ac2015-09-18 22:29:55 +080011884/*
Feng Wuefc64402015-09-18 22:29:51 +080011885 * vmx_update_pi_irte - set IRTE for Posted-Interrupts
11886 *
11887 * @kvm: kvm
11888 * @host_irq: host irq of the interrupt
11889 * @guest_irq: gsi of the interrupt
11890 * @set: set or unset PI
11891 * returns 0 on success, < 0 on failure
11892 */
11893static int vmx_update_pi_irte(struct kvm *kvm, unsigned int host_irq,
11894 uint32_t guest_irq, bool set)
11895{
11896 struct kvm_kernel_irq_routing_entry *e;
11897 struct kvm_irq_routing_table *irq_rt;
11898 struct kvm_lapic_irq irq;
11899 struct kvm_vcpu *vcpu;
11900 struct vcpu_data vcpu_info;
Jan H. Schönherr3a8b0672017-09-07 19:02:30 +010011901 int idx, ret = 0;
Feng Wuefc64402015-09-18 22:29:51 +080011902
11903 if (!kvm_arch_has_assigned_device(kvm) ||
Yang Zhanga0052192016-06-13 09:56:56 +080011904 !irq_remapping_cap(IRQ_POSTING_CAP) ||
11905 !kvm_vcpu_apicv_active(kvm->vcpus[0]))
Feng Wuefc64402015-09-18 22:29:51 +080011906 return 0;
11907
11908 idx = srcu_read_lock(&kvm->irq_srcu);
11909 irq_rt = srcu_dereference(kvm->irq_routing, &kvm->irq_srcu);
Jan H. Schönherr3a8b0672017-09-07 19:02:30 +010011910 if (guest_irq >= irq_rt->nr_rt_entries ||
11911 hlist_empty(&irq_rt->map[guest_irq])) {
11912 pr_warn_once("no route for guest_irq %u/%u (broken user space?)\n",
11913 guest_irq, irq_rt->nr_rt_entries);
11914 goto out;
11915 }
Feng Wuefc64402015-09-18 22:29:51 +080011916
11917 hlist_for_each_entry(e, &irq_rt->map[guest_irq], link) {
11918 if (e->type != KVM_IRQ_ROUTING_MSI)
11919 continue;
11920 /*
11921 * VT-d PI cannot support posting multicast/broadcast
11922 * interrupts to a vCPU, we still use interrupt remapping
11923 * for these kind of interrupts.
11924 *
11925 * For lowest-priority interrupts, we only support
11926 * those with single CPU as the destination, e.g. user
11927 * configures the interrupts via /proc/irq or uses
11928 * irqbalance to make the interrupts single-CPU.
11929 *
11930 * We will support full lowest-priority interrupt later.
11931 */
11932
Radim Krčmář371313132016-07-12 22:09:27 +020011933 kvm_set_msi_irq(kvm, e, &irq);
Feng Wu23a1c252016-01-25 16:53:32 +080011934 if (!kvm_intr_is_single_vcpu(kvm, &irq, &vcpu)) {
11935 /*
11936 * Make sure the IRTE is in remapped mode if
11937 * we don't handle it in posted mode.
11938 */
11939 ret = irq_set_vcpu_affinity(host_irq, NULL);
11940 if (ret < 0) {
11941 printk(KERN_INFO
11942 "failed to back to remapped mode, irq: %u\n",
11943 host_irq);
11944 goto out;
11945 }
11946
Feng Wuefc64402015-09-18 22:29:51 +080011947 continue;
Feng Wu23a1c252016-01-25 16:53:32 +080011948 }
Feng Wuefc64402015-09-18 22:29:51 +080011949
11950 vcpu_info.pi_desc_addr = __pa(vcpu_to_pi_desc(vcpu));
11951 vcpu_info.vector = irq.vector;
11952
Feng Wub6ce9782016-01-25 16:53:35 +080011953 trace_kvm_pi_irte_update(vcpu->vcpu_id, host_irq, e->gsi,
Feng Wuefc64402015-09-18 22:29:51 +080011954 vcpu_info.vector, vcpu_info.pi_desc_addr, set);
11955
11956 if (set)
11957 ret = irq_set_vcpu_affinity(host_irq, &vcpu_info);
Haozhong Zhangdc91f2eb2017-09-18 09:56:49 +080011958 else
Feng Wuefc64402015-09-18 22:29:51 +080011959 ret = irq_set_vcpu_affinity(host_irq, NULL);
Feng Wuefc64402015-09-18 22:29:51 +080011960
11961 if (ret < 0) {
11962 printk(KERN_INFO "%s: failed to update PI IRTE\n",
11963 __func__);
11964 goto out;
11965 }
11966 }
11967
11968 ret = 0;
11969out:
11970 srcu_read_unlock(&kvm->irq_srcu, idx);
11971 return ret;
11972}
11973
Ashok Rajc45dcc72016-06-22 14:59:56 +080011974static void vmx_setup_mce(struct kvm_vcpu *vcpu)
11975{
11976 if (vcpu->arch.mcg_cap & MCG_LMCE_P)
11977 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits |=
11978 FEATURE_CONTROL_LMCE;
11979 else
11980 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits &=
11981 ~FEATURE_CONTROL_LMCE;
11982}
11983
Ladi Prosek72d7b372017-10-11 16:54:41 +020011984static int vmx_smi_allowed(struct kvm_vcpu *vcpu)
11985{
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020011986 /* we need a nested vmexit to enter SMM, postpone if run is pending */
11987 if (to_vmx(vcpu)->nested.nested_run_pending)
11988 return 0;
Ladi Prosek72d7b372017-10-11 16:54:41 +020011989 return 1;
11990}
11991
Ladi Prosek0234bf82017-10-11 16:54:40 +020011992static int vmx_pre_enter_smm(struct kvm_vcpu *vcpu, char *smstate)
11993{
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020011994 struct vcpu_vmx *vmx = to_vmx(vcpu);
11995
11996 vmx->nested.smm.guest_mode = is_guest_mode(vcpu);
11997 if (vmx->nested.smm.guest_mode)
11998 nested_vmx_vmexit(vcpu, -1, 0, 0);
11999
12000 vmx->nested.smm.vmxon = vmx->nested.vmxon;
12001 vmx->nested.vmxon = false;
Ladi Prosek0234bf82017-10-11 16:54:40 +020012002 return 0;
12003}
12004
12005static int vmx_pre_leave_smm(struct kvm_vcpu *vcpu, u64 smbase)
12006{
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020012007 struct vcpu_vmx *vmx = to_vmx(vcpu);
12008 int ret;
12009
12010 if (vmx->nested.smm.vmxon) {
12011 vmx->nested.vmxon = true;
12012 vmx->nested.smm.vmxon = false;
12013 }
12014
12015 if (vmx->nested.smm.guest_mode) {
12016 vcpu->arch.hflags &= ~HF_SMM_MASK;
12017 ret = enter_vmx_non_root_mode(vcpu, false);
12018 vcpu->arch.hflags |= HF_SMM_MASK;
12019 if (ret)
12020 return ret;
12021
12022 vmx->nested.smm.guest_mode = false;
12023 }
Ladi Prosek0234bf82017-10-11 16:54:40 +020012024 return 0;
12025}
12026
Ladi Prosekcc3d9672017-10-17 16:02:39 +020012027static int enable_smi_window(struct kvm_vcpu *vcpu)
12028{
12029 return 0;
12030}
12031
Kees Cook404f6aa2016-08-08 16:29:06 -070012032static struct kvm_x86_ops vmx_x86_ops __ro_after_init = {
Avi Kivity6aa8b732006-12-10 02:21:36 -080012033 .cpu_has_kvm_support = cpu_has_kvm_support,
12034 .disabled_by_bios = vmx_disabled_by_bios,
12035 .hardware_setup = hardware_setup,
12036 .hardware_unsetup = hardware_unsetup,
Yang, Sheng002c7f72007-07-31 14:23:01 +030012037 .check_processor_compatibility = vmx_check_processor_compat,
Avi Kivity6aa8b732006-12-10 02:21:36 -080012038 .hardware_enable = hardware_enable,
12039 .hardware_disable = hardware_disable,
Sheng Yang04547152009-04-01 15:52:31 +080012040 .cpu_has_accelerated_tpr = report_flexpriority,
Paolo Bonzini6d396b52015-04-01 14:25:33 +020012041 .cpu_has_high_real_mode_segbase = vmx_has_high_real_mode_segbase,
Avi Kivity6aa8b732006-12-10 02:21:36 -080012042
12043 .vcpu_create = vmx_create_vcpu,
12044 .vcpu_free = vmx_free_vcpu,
Avi Kivity04d2cc72007-09-10 18:10:54 +030012045 .vcpu_reset = vmx_vcpu_reset,
Avi Kivity6aa8b732006-12-10 02:21:36 -080012046
Avi Kivity04d2cc72007-09-10 18:10:54 +030012047 .prepare_guest_switch = vmx_save_host_state,
Avi Kivity6aa8b732006-12-10 02:21:36 -080012048 .vcpu_load = vmx_vcpu_load,
12049 .vcpu_put = vmx_vcpu_put,
12050
Paolo Bonzinia96036b2015-11-10 11:55:36 +010012051 .update_bp_intercept = update_exception_bitmap,
Avi Kivity6aa8b732006-12-10 02:21:36 -080012052 .get_msr = vmx_get_msr,
12053 .set_msr = vmx_set_msr,
12054 .get_segment_base = vmx_get_segment_base,
12055 .get_segment = vmx_get_segment,
12056 .set_segment = vmx_set_segment,
Izik Eidus2e4d2652008-03-24 19:38:34 +020012057 .get_cpl = vmx_get_cpl,
Avi Kivity6aa8b732006-12-10 02:21:36 -080012058 .get_cs_db_l_bits = vmx_get_cs_db_l_bits,
Avi Kivitye8467fd2009-12-29 18:43:06 +020012059 .decache_cr0_guest_bits = vmx_decache_cr0_guest_bits,
Avi Kivityaff48ba2010-12-05 18:56:11 +020012060 .decache_cr3 = vmx_decache_cr3,
Anthony Liguori25c4c272007-04-27 09:29:21 +030012061 .decache_cr4_guest_bits = vmx_decache_cr4_guest_bits,
Avi Kivity6aa8b732006-12-10 02:21:36 -080012062 .set_cr0 = vmx_set_cr0,
Avi Kivity6aa8b732006-12-10 02:21:36 -080012063 .set_cr3 = vmx_set_cr3,
12064 .set_cr4 = vmx_set_cr4,
Avi Kivity6aa8b732006-12-10 02:21:36 -080012065 .set_efer = vmx_set_efer,
Avi Kivity6aa8b732006-12-10 02:21:36 -080012066 .get_idt = vmx_get_idt,
12067 .set_idt = vmx_set_idt,
12068 .get_gdt = vmx_get_gdt,
12069 .set_gdt = vmx_set_gdt,
Jan Kiszka73aaf249e2014-01-04 18:47:16 +010012070 .get_dr6 = vmx_get_dr6,
12071 .set_dr6 = vmx_set_dr6,
Gleb Natapov020df072010-04-13 10:05:23 +030012072 .set_dr7 = vmx_set_dr7,
Paolo Bonzini81908bf2014-02-21 10:32:27 +010012073 .sync_dirty_debug_regs = vmx_sync_dirty_debug_regs,
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -030012074 .cache_reg = vmx_cache_reg,
Avi Kivity6aa8b732006-12-10 02:21:36 -080012075 .get_rflags = vmx_get_rflags,
12076 .set_rflags = vmx_set_rflags,
Huaitong Hanbe94f6b2016-03-22 16:51:20 +080012077
Avi Kivity6aa8b732006-12-10 02:21:36 -080012078 .tlb_flush = vmx_flush_tlb,
Avi Kivity6aa8b732006-12-10 02:21:36 -080012079
Avi Kivity6aa8b732006-12-10 02:21:36 -080012080 .run = vmx_vcpu_run,
Avi Kivity6062d012009-03-23 17:35:17 +020012081 .handle_exit = vmx_handle_exit,
Avi Kivity6aa8b732006-12-10 02:21:36 -080012082 .skip_emulated_instruction = skip_emulated_instruction,
Glauber Costa2809f5d2009-05-12 16:21:05 -040012083 .set_interrupt_shadow = vmx_set_interrupt_shadow,
12084 .get_interrupt_shadow = vmx_get_interrupt_shadow,
Ingo Molnar102d8322007-02-19 14:37:47 +020012085 .patch_hypercall = vmx_patch_hypercall,
Eddie Dong2a8067f2007-08-06 16:29:07 +030012086 .set_irq = vmx_inject_irq,
Gleb Natapov95ba8273132009-04-21 17:45:08 +030012087 .set_nmi = vmx_inject_nmi,
Avi Kivity298101d2007-11-25 13:41:11 +020012088 .queue_exception = vmx_queue_exception,
Avi Kivityb463a6f2010-07-20 15:06:17 +030012089 .cancel_injection = vmx_cancel_injection,
Gleb Natapov78646122009-03-23 12:12:11 +020012090 .interrupt_allowed = vmx_interrupt_allowed,
Gleb Natapov95ba8273132009-04-21 17:45:08 +030012091 .nmi_allowed = vmx_nmi_allowed,
Jan Kiszka3cfc3092009-11-12 01:04:25 +010012092 .get_nmi_mask = vmx_get_nmi_mask,
12093 .set_nmi_mask = vmx_set_nmi_mask,
Gleb Natapov95ba8273132009-04-21 17:45:08 +030012094 .enable_nmi_window = enable_nmi_window,
12095 .enable_irq_window = enable_irq_window,
12096 .update_cr8_intercept = update_cr8_intercept,
Yang Zhang8d146952013-01-25 10:18:50 +080012097 .set_virtual_x2apic_mode = vmx_set_virtual_x2apic_mode,
Tang Chen38b99172014-09-24 15:57:54 +080012098 .set_apic_access_page_addr = vmx_set_apic_access_page_addr,
Andrey Smetanind62caab2015-11-10 15:36:33 +030012099 .get_enable_apicv = vmx_get_enable_apicv,
12100 .refresh_apicv_exec_ctrl = vmx_refresh_apicv_exec_ctrl,
Yang Zhangc7c9c562013-01-25 10:18:51 +080012101 .load_eoi_exitmap = vmx_load_eoi_exitmap,
Paolo Bonzini967235d2016-12-19 14:03:45 +010012102 .apicv_post_state_restore = vmx_apicv_post_state_restore,
Yang Zhangc7c9c562013-01-25 10:18:51 +080012103 .hwapic_irr_update = vmx_hwapic_irr_update,
12104 .hwapic_isr_update = vmx_hwapic_isr_update,
Yang Zhanga20ed542013-04-11 19:25:15 +080012105 .sync_pir_to_irr = vmx_sync_pir_to_irr,
12106 .deliver_posted_interrupt = vmx_deliver_posted_interrupt,
Gleb Natapov95ba8273132009-04-21 17:45:08 +030012107
Izik Eiduscbc94022007-10-25 00:29:55 +020012108 .set_tss_addr = vmx_set_tss_addr,
Sheng Yang67253af2008-04-25 10:20:22 +080012109 .get_tdp_level = get_ept_level,
Sheng Yang4b12f0d2009-04-27 20:35:42 +080012110 .get_mt_mask = vmx_get_mt_mask,
Marcelo Tosatti229456f2009-06-17 09:22:14 -030012111
Avi Kivity586f9602010-11-18 13:09:54 +020012112 .get_exit_info = vmx_get_exit_info,
Avi Kivity586f9602010-11-18 13:09:54 +020012113
Sheng Yang17cc3932010-01-05 19:02:27 +080012114 .get_lpage_level = vmx_get_lpage_level,
Sheng Yang0e851882009-12-18 16:48:46 +080012115
12116 .cpuid_update = vmx_cpuid_update,
Sheng Yang4e47c7a2009-12-18 16:48:47 +080012117
12118 .rdtscp_supported = vmx_rdtscp_supported,
Mao, Junjiead756a12012-07-02 01:18:48 +000012119 .invpcid_supported = vmx_invpcid_supported,
Joerg Roedeld4330ef2010-04-22 12:33:11 +020012120
12121 .set_supported_cpuid = vmx_set_supported_cpuid,
Sheng Yangf5f48ee2010-06-30 12:25:15 +080012122
12123 .has_wbinvd_exit = cpu_has_vmx_wbinvd_exit,
Zachary Amsden99e3e302010-08-19 22:07:17 -100012124
12125 .write_tsc_offset = vmx_write_tsc_offset,
Joerg Roedel1c97f0a2010-09-10 17:30:41 +020012126
12127 .set_tdp_cr3 = vmx_set_cr3,
Joerg Roedel8a76d7f2011-04-04 12:39:27 +020012128
12129 .check_intercept = vmx_check_intercept,
Yang Zhanga547c6d2013-04-11 19:25:10 +080012130 .handle_external_intr = vmx_handle_external_intr,
Liu, Jinsongda8999d2014-02-24 10:55:46 +000012131 .mpx_supported = vmx_mpx_supported,
Wanpeng Li55412b22014-12-02 19:21:30 +080012132 .xsaves_supported = vmx_xsaves_supported,
Paolo Bonzini66336ca2016-07-12 10:36:41 +020012133 .umip_emulated = vmx_umip_emulated,
Jan Kiszkab6b8a142014-03-07 20:03:12 +010012134
12135 .check_nested_events = vmx_check_nested_events,
Radim Krčmářae97a3b2014-08-21 18:08:06 +020012136
12137 .sched_in = vmx_sched_in,
Kai Huang843e4332015-01-28 10:54:28 +080012138
12139 .slot_enable_log_dirty = vmx_slot_enable_log_dirty,
12140 .slot_disable_log_dirty = vmx_slot_disable_log_dirty,
12141 .flush_log_dirty = vmx_flush_log_dirty,
12142 .enable_log_dirty_pt_masked = vmx_enable_log_dirty_pt_masked,
Bandan Dasc5f983f2017-05-05 15:25:14 -040012143 .write_log_dirty = vmx_write_pml_buffer,
Wei Huang25462f72015-06-19 15:45:05 +020012144
Feng Wubf9f6ac2015-09-18 22:29:55 +080012145 .pre_block = vmx_pre_block,
12146 .post_block = vmx_post_block,
12147
Wei Huang25462f72015-06-19 15:45:05 +020012148 .pmu_ops = &intel_pmu_ops,
Feng Wuefc64402015-09-18 22:29:51 +080012149
12150 .update_pi_irte = vmx_update_pi_irte,
Yunhong Jiang64672c92016-06-13 14:19:59 -070012151
12152#ifdef CONFIG_X86_64
12153 .set_hv_timer = vmx_set_hv_timer,
12154 .cancel_hv_timer = vmx_cancel_hv_timer,
12155#endif
Ashok Rajc45dcc72016-06-22 14:59:56 +080012156
12157 .setup_mce = vmx_setup_mce,
Ladi Prosek0234bf82017-10-11 16:54:40 +020012158
Ladi Prosek72d7b372017-10-11 16:54:41 +020012159 .smi_allowed = vmx_smi_allowed,
Ladi Prosek0234bf82017-10-11 16:54:40 +020012160 .pre_enter_smm = vmx_pre_enter_smm,
12161 .pre_leave_smm = vmx_pre_leave_smm,
Ladi Prosekcc3d9672017-10-17 16:02:39 +020012162 .enable_smi_window = enable_smi_window,
Avi Kivity6aa8b732006-12-10 02:21:36 -080012163};
12164
12165static int __init vmx_init(void)
12166{
Tiejun Chen34a1cd62014-10-28 10:14:48 +080012167 int r = kvm_init(&vmx_x86_ops, sizeof(struct vcpu_vmx),
12168 __alignof__(struct vcpu_vmx), THIS_MODULE);
He, Qingfdef3ad2007-04-30 09:45:24 +030012169 if (r)
Tiejun Chen34a1cd62014-10-28 10:14:48 +080012170 return r;
Sheng Yang25c5f222008-03-28 13:18:56 +080012171
Dave Young2965faa2015-09-09 15:38:55 -070012172#ifdef CONFIG_KEXEC_CORE
Zhang Yanfei8f536b72012-12-06 23:43:34 +080012173 rcu_assign_pointer(crash_vmclear_loaded_vmcss,
12174 crash_vmclear_local_loaded_vmcss);
12175#endif
12176
He, Qingfdef3ad2007-04-30 09:45:24 +030012177 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -080012178}
12179
12180static void __exit vmx_exit(void)
12181{
Dave Young2965faa2015-09-09 15:38:55 -070012182#ifdef CONFIG_KEXEC_CORE
Monam Agarwal3b63a432014-03-22 12:28:10 +053012183 RCU_INIT_POINTER(crash_vmclear_loaded_vmcss, NULL);
Zhang Yanfei8f536b72012-12-06 23:43:34 +080012184 synchronize_rcu();
12185#endif
12186
Zhang Xiantaocb498ea2007-11-14 20:39:31 +080012187 kvm_exit();
Avi Kivity6aa8b732006-12-10 02:21:36 -080012188}
12189
12190module_init(vmx_init)
12191module_exit(vmx_exit)