blob: e04b2a8dd9b0066468f753f4f0cfafdd6827fa17 [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
Rusty Russell476bc002012-01-13 09:32:18 +103073static bool __read_mostly flexpriority_enabled = 1;
Avi Kivity736caef2009-03-23 17:39:48 +020074module_param_named(flexpriority, flexpriority_enabled, bool, S_IRUGO);
Avi Kivity4c9fc8e2008-03-24 18:15:14 +020075
Rusty Russell476bc002012-01-13 09:32:18 +103076static bool __read_mostly enable_ept = 1;
Avi Kivity736caef2009-03-23 17:39:48 +020077module_param_named(ept, enable_ept, bool, S_IRUGO);
Sheng Yangd56f5462008-04-25 10:13:16 +080078
Rusty Russell476bc002012-01-13 09:32:18 +103079static bool __read_mostly enable_unrestricted_guest = 1;
Nitin A Kamble3a624e22009-06-08 11:34:16 -070080module_param_named(unrestricted_guest,
81 enable_unrestricted_guest, bool, S_IRUGO);
82
Xudong Hao83c3a332012-05-28 19:33:35 +080083static bool __read_mostly enable_ept_ad_bits = 1;
84module_param_named(eptad, enable_ept_ad_bits, bool, S_IRUGO);
85
Avi Kivitya27685c2012-06-12 20:30:18 +030086static bool __read_mostly emulate_invalid_guest_state = true;
Avi Kivityc1f8bc02009-03-23 15:41:17 +020087module_param(emulate_invalid_guest_state, bool, S_IRUGO);
Mohammed Gamal04fa4d32008-08-17 16:39:48 +030088
Rusty Russell476bc002012-01-13 09:32:18 +103089static bool __read_mostly fasteoi = 1;
Kevin Tian58fbbf22011-08-30 13:56:17 +030090module_param(fasteoi, bool, S_IRUGO);
91
Yang Zhang5a717852013-04-11 19:25:16 +080092static bool __read_mostly enable_apicv = 1;
Yang Zhang01e439b2013-04-11 19:25:12 +080093module_param(enable_apicv, bool, S_IRUGO);
Yang Zhang83d4c282013-01-25 10:18:49 +080094
Abel Gordonabc4fc52013-04-18 14:35:25 +030095static bool __read_mostly enable_shadow_vmcs = 1;
96module_param_named(enable_shadow_vmcs, enable_shadow_vmcs, bool, S_IRUGO);
Nadav Har'El801d3422011-05-25 23:02:23 +030097/*
98 * If nested=1, nested virtualization is supported, i.e., guests may use
99 * VMX and be a hypervisor for its own guests. If nested=0, guests may not
100 * use VMX instructions.
101 */
Rusty Russell476bc002012-01-13 09:32:18 +1030102static bool __read_mostly nested = 0;
Nadav Har'El801d3422011-05-25 23:02:23 +0300103module_param(nested, bool, S_IRUGO);
104
Wanpeng Li20300092014-12-02 19:14:59 +0800105static u64 __read_mostly host_xss;
106
Kai Huang843e4332015-01-28 10:54:28 +0800107static bool __read_mostly enable_pml = 1;
108module_param_named(pml, enable_pml, bool, S_IRUGO);
109
Haozhong Zhang64903d62015-10-20 15:39:09 +0800110#define KVM_VMX_TSC_MULTIPLIER_MAX 0xffffffffffffffffULL
111
Yunhong Jiang64672c92016-06-13 14:19:59 -0700112/* Guest_tsc -> host_tsc conversion requires 64-bit division. */
113static int __read_mostly cpu_preemption_timer_multi;
114static bool __read_mostly enable_preemption_timer = 1;
115#ifdef CONFIG_X86_64
116module_param_named(preemption_timer, enable_preemption_timer, bool, S_IRUGO);
117#endif
118
Gleb Natapov50378782013-02-04 16:00:28 +0200119#define KVM_GUEST_CR0_MASK (X86_CR0_NW | X86_CR0_CD)
120#define KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST (X86_CR0_WP | X86_CR0_NE)
Avi Kivitycdc0e242009-12-06 17:21:14 +0200121#define KVM_VM_CR0_ALWAYS_ON \
122 (KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST | X86_CR0_PG | X86_CR0_PE)
Avi Kivity4c386092009-12-07 12:26:18 +0200123#define KVM_CR4_GUEST_OWNED_BITS \
124 (X86_CR4_PVI | X86_CR4_DE | X86_CR4_PCE | X86_CR4_OSFXSR \
Andy Lutomirski52ce3c22014-10-07 17:16:21 -0700125 | X86_CR4_OSXMMEXCPT | X86_CR4_TSD)
Avi Kivity4c386092009-12-07 12:26:18 +0200126
Avi Kivitycdc0e242009-12-06 17:21:14 +0200127#define KVM_PMODE_VM_CR4_ALWAYS_ON (X86_CR4_PAE | X86_CR4_VMXE)
128#define KVM_RMODE_VM_CR4_ALWAYS_ON (X86_CR4_VME | X86_CR4_PAE | X86_CR4_VMXE)
129
Avi Kivity78ac8b42010-04-08 18:19:35 +0300130#define RMODE_GUEST_OWNED_EFLAGS_BITS (~(X86_EFLAGS_IOPL | X86_EFLAGS_VM))
131
Jan Kiszkaf41245002014-03-07 20:03:13 +0100132#define VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE 5
133
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +0800134/*
Jan Dakinevich16c2aec2016-10-28 07:00:30 +0300135 * Hyper-V requires all of these, so mark them as supported even though
136 * they are just treated the same as all-context.
137 */
138#define VMX_VPID_EXTENT_SUPPORTED_MASK \
139 (VMX_VPID_EXTENT_INDIVIDUAL_ADDR_BIT | \
140 VMX_VPID_EXTENT_SINGLE_CONTEXT_BIT | \
141 VMX_VPID_EXTENT_GLOBAL_CONTEXT_BIT | \
142 VMX_VPID_EXTENT_SINGLE_NON_GLOBAL_BIT)
143
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +0800144/*
145 * These 2 parameters are used to config the controls for Pause-Loop Exiting:
146 * ple_gap: upper bound on the amount of time between two successive
147 * executions of PAUSE in a loop. Also indicate if ple enabled.
Rik van Riel00c25bc2011-01-04 09:51:33 -0500148 * According to test, this time is usually smaller than 128 cycles.
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +0800149 * ple_window: upper bound on the amount of time a guest is allowed to execute
150 * in a PAUSE loop. Tests indicate that most spinlocks are held for
151 * less than 2^12 cycles
152 * Time is measured based on a counter that runs at the same rate as the TSC,
153 * refer SDM volume 3b section 21.6.13 & 22.1.3.
154 */
Radim Krčmářb4a2d312014-08-21 18:08:08 +0200155#define KVM_VMX_DEFAULT_PLE_GAP 128
156#define KVM_VMX_DEFAULT_PLE_WINDOW 4096
157#define KVM_VMX_DEFAULT_PLE_WINDOW_GROW 2
158#define KVM_VMX_DEFAULT_PLE_WINDOW_SHRINK 0
159#define KVM_VMX_DEFAULT_PLE_WINDOW_MAX \
160 INT_MAX / KVM_VMX_DEFAULT_PLE_WINDOW_GROW
161
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +0800162static int ple_gap = KVM_VMX_DEFAULT_PLE_GAP;
163module_param(ple_gap, int, S_IRUGO);
164
165static int ple_window = KVM_VMX_DEFAULT_PLE_WINDOW;
166module_param(ple_window, int, S_IRUGO);
167
Radim Krčmářb4a2d312014-08-21 18:08:08 +0200168/* Default doubles per-vcpu window every exit. */
169static int ple_window_grow = KVM_VMX_DEFAULT_PLE_WINDOW_GROW;
170module_param(ple_window_grow, int, S_IRUGO);
171
172/* Default resets per-vcpu window every exit to ple_window. */
173static int ple_window_shrink = KVM_VMX_DEFAULT_PLE_WINDOW_SHRINK;
174module_param(ple_window_shrink, int, S_IRUGO);
175
176/* Default is to compute the maximum so we can never overflow. */
177static int ple_window_actual_max = KVM_VMX_DEFAULT_PLE_WINDOW_MAX;
178static int ple_window_max = KVM_VMX_DEFAULT_PLE_WINDOW_MAX;
179module_param(ple_window_max, int, S_IRUGO);
180
Avi Kivity83287ea422012-09-16 15:10:57 +0300181extern const ulong vmx_return;
182
Gleb Natapov8bf00a52011-10-05 14:01:22 +0200183#define NR_AUTOLOAD_MSRS 8
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +0300184#define VMCS02_POOL_SIZE 1
Avi Kivity61d2ef22010-04-28 16:40:38 +0300185
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400186struct vmcs {
187 u32 revision_id;
188 u32 abort;
189 char data[0];
190};
191
Nadav Har'Eld462b812011-05-24 15:26:10 +0300192/*
193 * Track a VMCS that may be loaded on a certain CPU. If it is (cpu!=-1), also
194 * remember whether it was VMLAUNCHed, and maintain a linked list of all VMCSs
195 * loaded on this CPU (so we can clear them if the CPU goes down).
196 */
197struct loaded_vmcs {
198 struct vmcs *vmcs;
Jim Mattson355f4fb2016-10-28 08:29:39 -0700199 struct vmcs *shadow_vmcs;
Nadav Har'Eld462b812011-05-24 15:26:10 +0300200 int cpu;
Paolo Bonzini4c4a6f72017-07-14 13:36:11 +0200201 bool launched;
202 bool nmi_known_unmasked;
Nadav Har'Eld462b812011-05-24 15:26:10 +0300203 struct list_head loaded_vmcss_on_cpu_link;
204};
205
Avi Kivity26bb0982009-09-07 11:14:12 +0300206struct shared_msr_entry {
207 unsigned index;
208 u64 data;
Avi Kivityd5696722009-12-02 12:28:47 +0200209 u64 mask;
Avi Kivity26bb0982009-09-07 11:14:12 +0300210};
211
Nadav Har'Elec378ae2011-05-25 23:02:54 +0300212/*
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300213 * struct vmcs12 describes the state that our guest hypervisor (L1) keeps for a
214 * single nested guest (L2), hence the name vmcs12. Any VMX implementation has
215 * a VMCS structure, and vmcs12 is our emulated VMX's VMCS. This structure is
216 * stored in guest memory specified by VMPTRLD, but is opaque to the guest,
217 * which must access it using VMREAD/VMWRITE/VMCLEAR instructions.
218 * More than one of these structures may exist, if L1 runs multiple L2 guests.
219 * nested_vmx_run() will use the data here to build a vmcs02: a VMCS for the
220 * underlying hardware which will be used to run L2.
221 * This structure is packed to ensure that its layout is identical across
222 * machines (necessary for live migration).
223 * If there are changes in this struct, VMCS12_REVISION must be changed.
224 */
Nadav Har'El22bd0352011-05-25 23:05:57 +0300225typedef u64 natural_width;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300226struct __packed vmcs12 {
227 /* According to the Intel spec, a VMCS region must start with the
228 * following two fields. Then follow implementation-specific data.
229 */
230 u32 revision_id;
231 u32 abort;
Nadav Har'El22bd0352011-05-25 23:05:57 +0300232
Nadav Har'El27d6c862011-05-25 23:06:59 +0300233 u32 launch_state; /* set to 0 by VMCLEAR, to 1 by VMLAUNCH */
234 u32 padding[7]; /* room for future expansion */
235
Nadav Har'El22bd0352011-05-25 23:05:57 +0300236 u64 io_bitmap_a;
237 u64 io_bitmap_b;
238 u64 msr_bitmap;
239 u64 vm_exit_msr_store_addr;
240 u64 vm_exit_msr_load_addr;
241 u64 vm_entry_msr_load_addr;
242 u64 tsc_offset;
243 u64 virtual_apic_page_addr;
244 u64 apic_access_addr;
Wincy Van705699a2015-02-03 23:58:17 +0800245 u64 posted_intr_desc_addr;
Bandan Das27c42a12017-08-03 15:54:42 -0400246 u64 vm_function_control;
Nadav Har'El22bd0352011-05-25 23:05:57 +0300247 u64 ept_pointer;
Wincy Van608406e2015-02-03 23:57:51 +0800248 u64 eoi_exit_bitmap0;
249 u64 eoi_exit_bitmap1;
250 u64 eoi_exit_bitmap2;
251 u64 eoi_exit_bitmap3;
Wanpeng Li81dc01f2014-12-04 19:11:07 +0800252 u64 xss_exit_bitmap;
Nadav Har'El22bd0352011-05-25 23:05:57 +0300253 u64 guest_physical_address;
254 u64 vmcs_link_pointer;
Bandan Dasc5f983f2017-05-05 15:25:14 -0400255 u64 pml_address;
Nadav Har'El22bd0352011-05-25 23:05:57 +0300256 u64 guest_ia32_debugctl;
257 u64 guest_ia32_pat;
258 u64 guest_ia32_efer;
259 u64 guest_ia32_perf_global_ctrl;
260 u64 guest_pdptr0;
261 u64 guest_pdptr1;
262 u64 guest_pdptr2;
263 u64 guest_pdptr3;
Paolo Bonzini36be0b92014-02-24 12:30:04 +0100264 u64 guest_bndcfgs;
Nadav Har'El22bd0352011-05-25 23:05:57 +0300265 u64 host_ia32_pat;
266 u64 host_ia32_efer;
267 u64 host_ia32_perf_global_ctrl;
268 u64 padding64[8]; /* room for future expansion */
269 /*
270 * To allow migration of L1 (complete with its L2 guests) between
271 * machines of different natural widths (32 or 64 bit), we cannot have
272 * unsigned long fields with no explict size. We use u64 (aliased
273 * natural_width) instead. Luckily, x86 is little-endian.
274 */
275 natural_width cr0_guest_host_mask;
276 natural_width cr4_guest_host_mask;
277 natural_width cr0_read_shadow;
278 natural_width cr4_read_shadow;
279 natural_width cr3_target_value0;
280 natural_width cr3_target_value1;
281 natural_width cr3_target_value2;
282 natural_width cr3_target_value3;
283 natural_width exit_qualification;
284 natural_width guest_linear_address;
285 natural_width guest_cr0;
286 natural_width guest_cr3;
287 natural_width guest_cr4;
288 natural_width guest_es_base;
289 natural_width guest_cs_base;
290 natural_width guest_ss_base;
291 natural_width guest_ds_base;
292 natural_width guest_fs_base;
293 natural_width guest_gs_base;
294 natural_width guest_ldtr_base;
295 natural_width guest_tr_base;
296 natural_width guest_gdtr_base;
297 natural_width guest_idtr_base;
298 natural_width guest_dr7;
299 natural_width guest_rsp;
300 natural_width guest_rip;
301 natural_width guest_rflags;
302 natural_width guest_pending_dbg_exceptions;
303 natural_width guest_sysenter_esp;
304 natural_width guest_sysenter_eip;
305 natural_width host_cr0;
306 natural_width host_cr3;
307 natural_width host_cr4;
308 natural_width host_fs_base;
309 natural_width host_gs_base;
310 natural_width host_tr_base;
311 natural_width host_gdtr_base;
312 natural_width host_idtr_base;
313 natural_width host_ia32_sysenter_esp;
314 natural_width host_ia32_sysenter_eip;
315 natural_width host_rsp;
316 natural_width host_rip;
317 natural_width paddingl[8]; /* room for future expansion */
318 u32 pin_based_vm_exec_control;
319 u32 cpu_based_vm_exec_control;
320 u32 exception_bitmap;
321 u32 page_fault_error_code_mask;
322 u32 page_fault_error_code_match;
323 u32 cr3_target_count;
324 u32 vm_exit_controls;
325 u32 vm_exit_msr_store_count;
326 u32 vm_exit_msr_load_count;
327 u32 vm_entry_controls;
328 u32 vm_entry_msr_load_count;
329 u32 vm_entry_intr_info_field;
330 u32 vm_entry_exception_error_code;
331 u32 vm_entry_instruction_len;
332 u32 tpr_threshold;
333 u32 secondary_vm_exec_control;
334 u32 vm_instruction_error;
335 u32 vm_exit_reason;
336 u32 vm_exit_intr_info;
337 u32 vm_exit_intr_error_code;
338 u32 idt_vectoring_info_field;
339 u32 idt_vectoring_error_code;
340 u32 vm_exit_instruction_len;
341 u32 vmx_instruction_info;
342 u32 guest_es_limit;
343 u32 guest_cs_limit;
344 u32 guest_ss_limit;
345 u32 guest_ds_limit;
346 u32 guest_fs_limit;
347 u32 guest_gs_limit;
348 u32 guest_ldtr_limit;
349 u32 guest_tr_limit;
350 u32 guest_gdtr_limit;
351 u32 guest_idtr_limit;
352 u32 guest_es_ar_bytes;
353 u32 guest_cs_ar_bytes;
354 u32 guest_ss_ar_bytes;
355 u32 guest_ds_ar_bytes;
356 u32 guest_fs_ar_bytes;
357 u32 guest_gs_ar_bytes;
358 u32 guest_ldtr_ar_bytes;
359 u32 guest_tr_ar_bytes;
360 u32 guest_interruptibility_info;
361 u32 guest_activity_state;
362 u32 guest_sysenter_cs;
363 u32 host_ia32_sysenter_cs;
Jan Kiszka0238ea92013-03-13 11:31:24 +0100364 u32 vmx_preemption_timer_value;
365 u32 padding32[7]; /* room for future expansion */
Nadav Har'El22bd0352011-05-25 23:05:57 +0300366 u16 virtual_processor_id;
Wincy Van705699a2015-02-03 23:58:17 +0800367 u16 posted_intr_nv;
Nadav Har'El22bd0352011-05-25 23:05:57 +0300368 u16 guest_es_selector;
369 u16 guest_cs_selector;
370 u16 guest_ss_selector;
371 u16 guest_ds_selector;
372 u16 guest_fs_selector;
373 u16 guest_gs_selector;
374 u16 guest_ldtr_selector;
375 u16 guest_tr_selector;
Wincy Van608406e2015-02-03 23:57:51 +0800376 u16 guest_intr_status;
Bandan Dasc5f983f2017-05-05 15:25:14 -0400377 u16 guest_pml_index;
Nadav Har'El22bd0352011-05-25 23:05:57 +0300378 u16 host_es_selector;
379 u16 host_cs_selector;
380 u16 host_ss_selector;
381 u16 host_ds_selector;
382 u16 host_fs_selector;
383 u16 host_gs_selector;
384 u16 host_tr_selector;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300385};
386
387/*
388 * VMCS12_REVISION is an arbitrary id that should be changed if the content or
389 * layout of struct vmcs12 is changed. MSR_IA32_VMX_BASIC returns this id, and
390 * VMPTRLD verifies that the VMCS region that L1 is loading contains this id.
391 */
392#define VMCS12_REVISION 0x11e57ed0
393
394/*
395 * VMCS12_SIZE is the number of bytes L1 should allocate for the VMXON region
396 * and any VMCS region. Although only sizeof(struct vmcs12) are used by the
397 * current implementation, 4K are reserved to avoid future complications.
398 */
399#define VMCS12_SIZE 0x1000
400
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +0300401/* Used to remember the last vmcs02 used for some recently used vmcs12s */
402struct vmcs02_list {
403 struct list_head list;
404 gpa_t vmptr;
405 struct loaded_vmcs vmcs02;
406};
407
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300408/*
Nadav Har'Elec378ae2011-05-25 23:02:54 +0300409 * The nested_vmx structure is part of vcpu_vmx, and holds information we need
410 * for correct emulation of VMX (i.e., nested VMX) on this vcpu.
411 */
412struct nested_vmx {
413 /* Has the level1 guest done vmxon? */
414 bool vmxon;
Bandan Das3573e222014-05-06 02:19:16 -0400415 gpa_t vmxon_ptr;
Bandan Dasc5f983f2017-05-05 15:25:14 -0400416 bool pml_full;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300417
418 /* The guest-physical address of the current VMCS L1 keeps for L2 */
419 gpa_t current_vmptr;
David Matlack4f2777b2016-07-13 17:16:37 -0700420 /*
421 * Cache of the guest's VMCS, existing outside of guest memory.
422 * Loaded from guest memory during VMPTRLD. Flushed to guest
David Matlack8ca44e82017-08-01 14:00:39 -0700423 * memory during VMCLEAR and VMPTRLD.
David Matlack4f2777b2016-07-13 17:16:37 -0700424 */
425 struct vmcs12 *cached_vmcs12;
Abel Gordon012f83c2013-04-18 14:39:25 +0300426 /*
427 * Indicates if the shadow vmcs must be updated with the
428 * data hold by vmcs12
429 */
430 bool sync_shadow_vmcs;
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +0300431
432 /* vmcs02_list cache of VMCSs recently used to run L2 guests */
433 struct list_head vmcs02_pool;
434 int vmcs02_num;
Radim Krčmářdccbfcf2016-08-08 20:16:23 +0200435 bool change_vmcs01_virtual_x2apic_mode;
Nadav Har'El644d7112011-05-25 23:12:35 +0300436 /* L2 must run next, and mustn't decide to exit to L1. */
437 bool nested_run_pending;
Nadav Har'Elfe3ef052011-05-25 23:10:02 +0300438 /*
439 * Guest pages referred to in vmcs02 with host-physical pointers, so
440 * we must keep them pinned while L2 runs.
441 */
442 struct page *apic_access_page;
Wanpeng Lia7c0b072014-08-21 19:46:50 +0800443 struct page *virtual_apic_page;
Wincy Van705699a2015-02-03 23:58:17 +0800444 struct page *pi_desc_page;
445 struct pi_desc *pi_desc;
446 bool pi_pending;
447 u16 posted_intr_nv;
Jan Kiszkaf41245002014-03-07 20:03:13 +0100448
Radim Krčmářd048c092016-08-08 20:16:22 +0200449 unsigned long *msr_bitmap;
450
Jan Kiszkaf41245002014-03-07 20:03:13 +0100451 struct hrtimer preemption_timer;
452 bool preemption_timer_expired;
Jan Kiszka2996fca2014-06-16 13:59:43 +0200453
454 /* to migrate it to L2 if VM_ENTRY_LOAD_DEBUG_CONTROLS is off */
455 u64 vmcs01_debugctl;
Wincy Vanb9c237b2015-02-03 23:56:30 +0800456
Wanpeng Li5c614b32015-10-13 09:18:36 -0700457 u16 vpid02;
458 u16 last_vpid;
459
David Matlack0115f9c2016-11-29 18:14:06 -0800460 /*
461 * We only store the "true" versions of the VMX capability MSRs. We
462 * generate the "non-true" versions by setting the must-be-1 bits
463 * according to the SDM.
464 */
Wincy Vanb9c237b2015-02-03 23:56:30 +0800465 u32 nested_vmx_procbased_ctls_low;
466 u32 nested_vmx_procbased_ctls_high;
Wincy Vanb9c237b2015-02-03 23:56:30 +0800467 u32 nested_vmx_secondary_ctls_low;
468 u32 nested_vmx_secondary_ctls_high;
469 u32 nested_vmx_pinbased_ctls_low;
470 u32 nested_vmx_pinbased_ctls_high;
471 u32 nested_vmx_exit_ctls_low;
472 u32 nested_vmx_exit_ctls_high;
Wincy Vanb9c237b2015-02-03 23:56:30 +0800473 u32 nested_vmx_entry_ctls_low;
474 u32 nested_vmx_entry_ctls_high;
Wincy Vanb9c237b2015-02-03 23:56:30 +0800475 u32 nested_vmx_misc_low;
476 u32 nested_vmx_misc_high;
477 u32 nested_vmx_ept_caps;
Wanpeng Li99b83ac2015-10-13 09:12:21 -0700478 u32 nested_vmx_vpid_caps;
David Matlack62cc6b9d2016-11-29 18:14:07 -0800479 u64 nested_vmx_basic;
480 u64 nested_vmx_cr0_fixed0;
481 u64 nested_vmx_cr0_fixed1;
482 u64 nested_vmx_cr4_fixed0;
483 u64 nested_vmx_cr4_fixed1;
484 u64 nested_vmx_vmcs_enum;
Bandan Das27c42a12017-08-03 15:54:42 -0400485 u64 nested_vmx_vmfunc_controls;
Nadav Har'Elec378ae2011-05-25 23:02:54 +0300486};
487
Yang Zhang01e439b2013-04-11 19:25:12 +0800488#define POSTED_INTR_ON 0
Feng Wuebbfc762015-09-18 22:29:46 +0800489#define POSTED_INTR_SN 1
490
Yang Zhang01e439b2013-04-11 19:25:12 +0800491/* Posted-Interrupt Descriptor */
492struct pi_desc {
493 u32 pir[8]; /* Posted interrupt requested */
Feng Wu6ef15222015-09-18 22:29:45 +0800494 union {
495 struct {
496 /* bit 256 - Outstanding Notification */
497 u16 on : 1,
498 /* bit 257 - Suppress Notification */
499 sn : 1,
500 /* bit 271:258 - Reserved */
501 rsvd_1 : 14;
502 /* bit 279:272 - Notification Vector */
503 u8 nv;
504 /* bit 287:280 - Reserved */
505 u8 rsvd_2;
506 /* bit 319:288 - Notification Destination */
507 u32 ndst;
508 };
509 u64 control;
510 };
511 u32 rsvd[6];
Yang Zhang01e439b2013-04-11 19:25:12 +0800512} __aligned(64);
513
Yang Zhanga20ed542013-04-11 19:25:15 +0800514static bool pi_test_and_set_on(struct pi_desc *pi_desc)
515{
516 return test_and_set_bit(POSTED_INTR_ON,
517 (unsigned long *)&pi_desc->control);
518}
519
520static bool pi_test_and_clear_on(struct pi_desc *pi_desc)
521{
522 return test_and_clear_bit(POSTED_INTR_ON,
523 (unsigned long *)&pi_desc->control);
524}
525
526static int pi_test_and_set_pir(int vector, struct pi_desc *pi_desc)
527{
528 return test_and_set_bit(vector, (unsigned long *)pi_desc->pir);
529}
530
Feng Wuebbfc762015-09-18 22:29:46 +0800531static inline void pi_clear_sn(struct pi_desc *pi_desc)
532{
533 return clear_bit(POSTED_INTR_SN,
534 (unsigned long *)&pi_desc->control);
535}
536
537static inline void pi_set_sn(struct pi_desc *pi_desc)
538{
539 return set_bit(POSTED_INTR_SN,
540 (unsigned long *)&pi_desc->control);
541}
542
Paolo Bonziniad361092016-09-20 16:15:05 +0200543static inline void pi_clear_on(struct pi_desc *pi_desc)
544{
545 clear_bit(POSTED_INTR_ON,
546 (unsigned long *)&pi_desc->control);
547}
548
Feng Wuebbfc762015-09-18 22:29:46 +0800549static inline int pi_test_on(struct pi_desc *pi_desc)
550{
551 return test_bit(POSTED_INTR_ON,
552 (unsigned long *)&pi_desc->control);
553}
554
555static inline int pi_test_sn(struct pi_desc *pi_desc)
556{
557 return test_bit(POSTED_INTR_SN,
558 (unsigned long *)&pi_desc->control);
559}
560
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400561struct vcpu_vmx {
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000562 struct kvm_vcpu vcpu;
Avi Kivity313dbd42008-07-17 18:04:30 +0300563 unsigned long host_rsp;
Avi Kivity29bd8a72007-09-10 17:27:03 +0300564 u8 fail;
Avi Kivity51aa01d2010-07-20 14:31:20 +0300565 u32 exit_intr_info;
Avi Kivity1155f762007-11-22 11:30:47 +0200566 u32 idt_vectoring_info;
Avi Kivity6de12732011-03-07 12:51:22 +0200567 ulong rflags;
Avi Kivity26bb0982009-09-07 11:14:12 +0300568 struct shared_msr_entry *guest_msrs;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400569 int nmsrs;
570 int save_nmsrs;
Yang Zhanga547c6d2013-04-11 19:25:10 +0800571 unsigned long host_idt_base;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400572#ifdef CONFIG_X86_64
Avi Kivity44ea2b12009-09-06 15:55:37 +0300573 u64 msr_host_kernel_gs_base;
574 u64 msr_guest_kernel_gs_base;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400575#endif
Gleb Natapov2961e8762013-11-25 15:37:13 +0200576 u32 vm_entry_controls_shadow;
577 u32 vm_exit_controls_shadow;
Nadav Har'Eld462b812011-05-24 15:26:10 +0300578 /*
579 * loaded_vmcs points to the VMCS currently used in this vcpu. For a
580 * non-nested (L1) guest, it always points to vmcs01. For a nested
581 * guest (L2), it points to a different VMCS.
582 */
583 struct loaded_vmcs vmcs01;
584 struct loaded_vmcs *loaded_vmcs;
585 bool __launched; /* temporary, used in vmx_vcpu_run */
Avi Kivity61d2ef22010-04-28 16:40:38 +0300586 struct msr_autoload {
587 unsigned nr;
588 struct vmx_msr_entry guest[NR_AUTOLOAD_MSRS];
589 struct vmx_msr_entry host[NR_AUTOLOAD_MSRS];
590 } msr_autoload;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400591 struct {
592 int loaded;
593 u16 fs_sel, gs_sel, ldt_sel;
Avi Kivityb2da15a2012-05-13 19:53:24 +0300594#ifdef CONFIG_X86_64
595 u16 ds_sel, es_sel;
596#endif
Laurent Vivier152d3f22007-08-23 16:33:11 +0200597 int gs_ldt_reload_needed;
598 int fs_reload_needed;
Liu, Jinsongda8999d2014-02-24 10:55:46 +0000599 u64 msr_host_bndcfgs;
Andy Lutomirskid6e41f12017-05-28 10:00:17 -0700600 unsigned long vmcs_host_cr3; /* May not match real cr3 */
Andy Lutomirskid974baa2014-10-08 09:02:13 -0700601 unsigned long vmcs_host_cr4; /* May not match real cr4 */
Mike Dayd77c26f2007-10-08 09:02:08 -0400602 } host_state;
Avi Kivity9c8cba32007-11-22 11:42:59 +0200603 struct {
Avi Kivity7ffd92c2009-06-09 14:10:45 +0300604 int vm86_active;
Avi Kivity78ac8b42010-04-08 18:19:35 +0300605 ulong save_rflags;
Avi Kivityf5f7b2f2012-08-21 17:07:00 +0300606 struct kvm_segment segs[8];
607 } rmode;
608 struct {
609 u32 bitmask; /* 4 bits per segment (1 bit per field) */
Avi Kivity7ffd92c2009-06-09 14:10:45 +0300610 struct kvm_save_segment {
611 u16 selector;
612 unsigned long base;
613 u32 limit;
614 u32 ar;
Avi Kivityf5f7b2f2012-08-21 17:07:00 +0300615 } seg[8];
Avi Kivity2fb92db2011-04-27 19:42:18 +0300616 } segment_cache;
Sheng Yang2384d2b2008-01-17 15:14:33 +0800617 int vpid;
Mohammed Gamal04fa4d32008-08-17 16:39:48 +0300618 bool emulation_required;
Jan Kiszka3b86cd92008-09-26 09:30:57 +0200619
Andi Kleena0861c02009-06-08 17:37:09 +0800620 u32 exit_reason;
Sheng Yang4e47c7a2009-12-18 16:48:47 +0800621
Yang Zhang01e439b2013-04-11 19:25:12 +0800622 /* Posted interrupt descriptor */
623 struct pi_desc pi_desc;
624
Nadav Har'Elec378ae2011-05-25 23:02:54 +0300625 /* Support for a guest hypervisor (nested VMX) */
626 struct nested_vmx nested;
Radim Krčmářa7653ec2014-08-21 18:08:07 +0200627
628 /* Dynamic PLE window. */
629 int ple_window;
630 bool ple_window_dirty;
Kai Huang843e4332015-01-28 10:54:28 +0800631
632 /* Support for PML */
633#define PML_ENTITY_NUM 512
634 struct page *pml_pg;
Owen Hofmann2680d6d2016-03-01 13:36:13 -0800635
Yunhong Jiang64672c92016-06-13 14:19:59 -0700636 /* apic deadline value in host tsc */
637 u64 hv_deadline_tsc;
638
Owen Hofmann2680d6d2016-03-01 13:36:13 -0800639 u64 current_tsc_ratio;
Xiao Guangrong1be0e612016-03-22 16:51:18 +0800640
641 bool guest_pkru_valid;
642 u32 guest_pkru;
643 u32 host_pkru;
Haozhong Zhang3b840802016-06-22 14:59:54 +0800644
Haozhong Zhang37e4c992016-06-22 14:59:55 +0800645 /*
646 * Only bits masked by msr_ia32_feature_control_valid_bits can be set in
647 * msr_ia32_feature_control. FEATURE_CONTROL_LOCKED is always included
648 * in msr_ia32_feature_control_valid_bits.
649 */
Haozhong Zhang3b840802016-06-22 14:59:54 +0800650 u64 msr_ia32_feature_control;
Haozhong Zhang37e4c992016-06-22 14:59:55 +0800651 u64 msr_ia32_feature_control_valid_bits;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400652};
653
Avi Kivity2fb92db2011-04-27 19:42:18 +0300654enum segment_cache_field {
655 SEG_FIELD_SEL = 0,
656 SEG_FIELD_BASE = 1,
657 SEG_FIELD_LIMIT = 2,
658 SEG_FIELD_AR = 3,
659
660 SEG_FIELD_NR = 4
661};
662
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400663static inline struct vcpu_vmx *to_vmx(struct kvm_vcpu *vcpu)
664{
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000665 return container_of(vcpu, struct vcpu_vmx, vcpu);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400666}
667
Feng Wuefc64402015-09-18 22:29:51 +0800668static struct pi_desc *vcpu_to_pi_desc(struct kvm_vcpu *vcpu)
669{
670 return &(to_vmx(vcpu)->pi_desc);
671}
672
Nadav Har'El22bd0352011-05-25 23:05:57 +0300673#define VMCS12_OFFSET(x) offsetof(struct vmcs12, x)
674#define FIELD(number, name) [number] = VMCS12_OFFSET(name)
675#define FIELD64(number, name) [number] = VMCS12_OFFSET(name), \
676 [number##_HIGH] = VMCS12_OFFSET(name)+4
677
Abel Gordon4607c2d2013-04-18 14:35:55 +0300678
Bandan Dasfe2b2012014-04-21 15:20:14 -0400679static unsigned long shadow_read_only_fields[] = {
Abel Gordon4607c2d2013-04-18 14:35:55 +0300680 /*
681 * We do NOT shadow fields that are modified when L0
682 * traps and emulates any vmx instruction (e.g. VMPTRLD,
683 * VMXON...) executed by L1.
684 * For example, VM_INSTRUCTION_ERROR is read
685 * by L1 if a vmx instruction fails (part of the error path).
686 * Note the code assumes this logic. If for some reason
687 * we start shadowing these fields then we need to
688 * force a shadow sync when L0 emulates vmx instructions
689 * (e.g. force a sync if VM_INSTRUCTION_ERROR is modified
690 * by nested_vmx_failValid)
691 */
692 VM_EXIT_REASON,
693 VM_EXIT_INTR_INFO,
694 VM_EXIT_INSTRUCTION_LEN,
695 IDT_VECTORING_INFO_FIELD,
696 IDT_VECTORING_ERROR_CODE,
697 VM_EXIT_INTR_ERROR_CODE,
698 EXIT_QUALIFICATION,
699 GUEST_LINEAR_ADDRESS,
700 GUEST_PHYSICAL_ADDRESS
701};
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
Bandan Dasfe2b2012014-04-21 15:20:14 -0400705static unsigned long shadow_read_write_fields[] = {
Wanpeng Lia7c0b072014-08-21 19:46:50 +0800706 TPR_THRESHOLD,
Abel Gordon4607c2d2013-04-18 14:35:55 +0300707 GUEST_RIP,
708 GUEST_RSP,
709 GUEST_CR0,
710 GUEST_CR3,
711 GUEST_CR4,
712 GUEST_INTERRUPTIBILITY_INFO,
713 GUEST_RFLAGS,
714 GUEST_CS_SELECTOR,
715 GUEST_CS_AR_BYTES,
716 GUEST_CS_LIMIT,
717 GUEST_CS_BASE,
718 GUEST_ES_BASE,
Paolo Bonzini36be0b92014-02-24 12:30:04 +0100719 GUEST_BNDCFGS,
Abel Gordon4607c2d2013-04-18 14:35:55 +0300720 CR0_GUEST_HOST_MASK,
721 CR0_READ_SHADOW,
722 CR4_READ_SHADOW,
723 TSC_OFFSET,
724 EXCEPTION_BITMAP,
725 CPU_BASED_VM_EXEC_CONTROL,
726 VM_ENTRY_EXCEPTION_ERROR_CODE,
727 VM_ENTRY_INTR_INFO_FIELD,
728 VM_ENTRY_INSTRUCTION_LEN,
729 VM_ENTRY_EXCEPTION_ERROR_CODE,
730 HOST_FS_BASE,
731 HOST_GS_BASE,
732 HOST_FS_SELECTOR,
733 HOST_GS_SELECTOR
734};
Bandan Dasfe2b2012014-04-21 15:20:14 -0400735static int max_shadow_read_write_fields =
Abel Gordon4607c2d2013-04-18 14:35:55 +0300736 ARRAY_SIZE(shadow_read_write_fields);
737
Mathias Krause772e0312012-08-30 01:30:19 +0200738static const unsigned short vmcs_field_to_offset_table[] = {
Nadav Har'El22bd0352011-05-25 23:05:57 +0300739 FIELD(VIRTUAL_PROCESSOR_ID, virtual_processor_id),
Wincy Van705699a2015-02-03 23:58:17 +0800740 FIELD(POSTED_INTR_NV, posted_intr_nv),
Nadav Har'El22bd0352011-05-25 23:05:57 +0300741 FIELD(GUEST_ES_SELECTOR, guest_es_selector),
742 FIELD(GUEST_CS_SELECTOR, guest_cs_selector),
743 FIELD(GUEST_SS_SELECTOR, guest_ss_selector),
744 FIELD(GUEST_DS_SELECTOR, guest_ds_selector),
745 FIELD(GUEST_FS_SELECTOR, guest_fs_selector),
746 FIELD(GUEST_GS_SELECTOR, guest_gs_selector),
747 FIELD(GUEST_LDTR_SELECTOR, guest_ldtr_selector),
748 FIELD(GUEST_TR_SELECTOR, guest_tr_selector),
Wincy Van608406e2015-02-03 23:57:51 +0800749 FIELD(GUEST_INTR_STATUS, guest_intr_status),
Bandan Dasc5f983f2017-05-05 15:25:14 -0400750 FIELD(GUEST_PML_INDEX, guest_pml_index),
Nadav Har'El22bd0352011-05-25 23:05:57 +0300751 FIELD(HOST_ES_SELECTOR, host_es_selector),
752 FIELD(HOST_CS_SELECTOR, host_cs_selector),
753 FIELD(HOST_SS_SELECTOR, host_ss_selector),
754 FIELD(HOST_DS_SELECTOR, host_ds_selector),
755 FIELD(HOST_FS_SELECTOR, host_fs_selector),
756 FIELD(HOST_GS_SELECTOR, host_gs_selector),
757 FIELD(HOST_TR_SELECTOR, host_tr_selector),
758 FIELD64(IO_BITMAP_A, io_bitmap_a),
759 FIELD64(IO_BITMAP_B, io_bitmap_b),
760 FIELD64(MSR_BITMAP, msr_bitmap),
761 FIELD64(VM_EXIT_MSR_STORE_ADDR, vm_exit_msr_store_addr),
762 FIELD64(VM_EXIT_MSR_LOAD_ADDR, vm_exit_msr_load_addr),
763 FIELD64(VM_ENTRY_MSR_LOAD_ADDR, vm_entry_msr_load_addr),
764 FIELD64(TSC_OFFSET, tsc_offset),
765 FIELD64(VIRTUAL_APIC_PAGE_ADDR, virtual_apic_page_addr),
766 FIELD64(APIC_ACCESS_ADDR, apic_access_addr),
Wincy Van705699a2015-02-03 23:58:17 +0800767 FIELD64(POSTED_INTR_DESC_ADDR, posted_intr_desc_addr),
Bandan Das27c42a12017-08-03 15:54:42 -0400768 FIELD64(VM_FUNCTION_CONTROL, vm_function_control),
Nadav Har'El22bd0352011-05-25 23:05:57 +0300769 FIELD64(EPT_POINTER, ept_pointer),
Wincy Van608406e2015-02-03 23:57:51 +0800770 FIELD64(EOI_EXIT_BITMAP0, eoi_exit_bitmap0),
771 FIELD64(EOI_EXIT_BITMAP1, eoi_exit_bitmap1),
772 FIELD64(EOI_EXIT_BITMAP2, eoi_exit_bitmap2),
773 FIELD64(EOI_EXIT_BITMAP3, eoi_exit_bitmap3),
Wanpeng Li81dc01f2014-12-04 19:11:07 +0800774 FIELD64(XSS_EXIT_BITMAP, xss_exit_bitmap),
Nadav Har'El22bd0352011-05-25 23:05:57 +0300775 FIELD64(GUEST_PHYSICAL_ADDRESS, guest_physical_address),
776 FIELD64(VMCS_LINK_POINTER, vmcs_link_pointer),
Bandan Dasc5f983f2017-05-05 15:25:14 -0400777 FIELD64(PML_ADDRESS, pml_address),
Nadav Har'El22bd0352011-05-25 23:05:57 +0300778 FIELD64(GUEST_IA32_DEBUGCTL, guest_ia32_debugctl),
779 FIELD64(GUEST_IA32_PAT, guest_ia32_pat),
780 FIELD64(GUEST_IA32_EFER, guest_ia32_efer),
781 FIELD64(GUEST_IA32_PERF_GLOBAL_CTRL, guest_ia32_perf_global_ctrl),
782 FIELD64(GUEST_PDPTR0, guest_pdptr0),
783 FIELD64(GUEST_PDPTR1, guest_pdptr1),
784 FIELD64(GUEST_PDPTR2, guest_pdptr2),
785 FIELD64(GUEST_PDPTR3, guest_pdptr3),
Paolo Bonzini36be0b92014-02-24 12:30:04 +0100786 FIELD64(GUEST_BNDCFGS, guest_bndcfgs),
Nadav Har'El22bd0352011-05-25 23:05:57 +0300787 FIELD64(HOST_IA32_PAT, host_ia32_pat),
788 FIELD64(HOST_IA32_EFER, host_ia32_efer),
789 FIELD64(HOST_IA32_PERF_GLOBAL_CTRL, host_ia32_perf_global_ctrl),
790 FIELD(PIN_BASED_VM_EXEC_CONTROL, pin_based_vm_exec_control),
791 FIELD(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control),
792 FIELD(EXCEPTION_BITMAP, exception_bitmap),
793 FIELD(PAGE_FAULT_ERROR_CODE_MASK, page_fault_error_code_mask),
794 FIELD(PAGE_FAULT_ERROR_CODE_MATCH, page_fault_error_code_match),
795 FIELD(CR3_TARGET_COUNT, cr3_target_count),
796 FIELD(VM_EXIT_CONTROLS, vm_exit_controls),
797 FIELD(VM_EXIT_MSR_STORE_COUNT, vm_exit_msr_store_count),
798 FIELD(VM_EXIT_MSR_LOAD_COUNT, vm_exit_msr_load_count),
799 FIELD(VM_ENTRY_CONTROLS, vm_entry_controls),
800 FIELD(VM_ENTRY_MSR_LOAD_COUNT, vm_entry_msr_load_count),
801 FIELD(VM_ENTRY_INTR_INFO_FIELD, vm_entry_intr_info_field),
802 FIELD(VM_ENTRY_EXCEPTION_ERROR_CODE, vm_entry_exception_error_code),
803 FIELD(VM_ENTRY_INSTRUCTION_LEN, vm_entry_instruction_len),
804 FIELD(TPR_THRESHOLD, tpr_threshold),
805 FIELD(SECONDARY_VM_EXEC_CONTROL, secondary_vm_exec_control),
806 FIELD(VM_INSTRUCTION_ERROR, vm_instruction_error),
807 FIELD(VM_EXIT_REASON, vm_exit_reason),
808 FIELD(VM_EXIT_INTR_INFO, vm_exit_intr_info),
809 FIELD(VM_EXIT_INTR_ERROR_CODE, vm_exit_intr_error_code),
810 FIELD(IDT_VECTORING_INFO_FIELD, idt_vectoring_info_field),
811 FIELD(IDT_VECTORING_ERROR_CODE, idt_vectoring_error_code),
812 FIELD(VM_EXIT_INSTRUCTION_LEN, vm_exit_instruction_len),
813 FIELD(VMX_INSTRUCTION_INFO, vmx_instruction_info),
814 FIELD(GUEST_ES_LIMIT, guest_es_limit),
815 FIELD(GUEST_CS_LIMIT, guest_cs_limit),
816 FIELD(GUEST_SS_LIMIT, guest_ss_limit),
817 FIELD(GUEST_DS_LIMIT, guest_ds_limit),
818 FIELD(GUEST_FS_LIMIT, guest_fs_limit),
819 FIELD(GUEST_GS_LIMIT, guest_gs_limit),
820 FIELD(GUEST_LDTR_LIMIT, guest_ldtr_limit),
821 FIELD(GUEST_TR_LIMIT, guest_tr_limit),
822 FIELD(GUEST_GDTR_LIMIT, guest_gdtr_limit),
823 FIELD(GUEST_IDTR_LIMIT, guest_idtr_limit),
824 FIELD(GUEST_ES_AR_BYTES, guest_es_ar_bytes),
825 FIELD(GUEST_CS_AR_BYTES, guest_cs_ar_bytes),
826 FIELD(GUEST_SS_AR_BYTES, guest_ss_ar_bytes),
827 FIELD(GUEST_DS_AR_BYTES, guest_ds_ar_bytes),
828 FIELD(GUEST_FS_AR_BYTES, guest_fs_ar_bytes),
829 FIELD(GUEST_GS_AR_BYTES, guest_gs_ar_bytes),
830 FIELD(GUEST_LDTR_AR_BYTES, guest_ldtr_ar_bytes),
831 FIELD(GUEST_TR_AR_BYTES, guest_tr_ar_bytes),
832 FIELD(GUEST_INTERRUPTIBILITY_INFO, guest_interruptibility_info),
833 FIELD(GUEST_ACTIVITY_STATE, guest_activity_state),
834 FIELD(GUEST_SYSENTER_CS, guest_sysenter_cs),
835 FIELD(HOST_IA32_SYSENTER_CS, host_ia32_sysenter_cs),
Jan Kiszka0238ea92013-03-13 11:31:24 +0100836 FIELD(VMX_PREEMPTION_TIMER_VALUE, vmx_preemption_timer_value),
Nadav Har'El22bd0352011-05-25 23:05:57 +0300837 FIELD(CR0_GUEST_HOST_MASK, cr0_guest_host_mask),
838 FIELD(CR4_GUEST_HOST_MASK, cr4_guest_host_mask),
839 FIELD(CR0_READ_SHADOW, cr0_read_shadow),
840 FIELD(CR4_READ_SHADOW, cr4_read_shadow),
841 FIELD(CR3_TARGET_VALUE0, cr3_target_value0),
842 FIELD(CR3_TARGET_VALUE1, cr3_target_value1),
843 FIELD(CR3_TARGET_VALUE2, cr3_target_value2),
844 FIELD(CR3_TARGET_VALUE3, cr3_target_value3),
845 FIELD(EXIT_QUALIFICATION, exit_qualification),
846 FIELD(GUEST_LINEAR_ADDRESS, guest_linear_address),
847 FIELD(GUEST_CR0, guest_cr0),
848 FIELD(GUEST_CR3, guest_cr3),
849 FIELD(GUEST_CR4, guest_cr4),
850 FIELD(GUEST_ES_BASE, guest_es_base),
851 FIELD(GUEST_CS_BASE, guest_cs_base),
852 FIELD(GUEST_SS_BASE, guest_ss_base),
853 FIELD(GUEST_DS_BASE, guest_ds_base),
854 FIELD(GUEST_FS_BASE, guest_fs_base),
855 FIELD(GUEST_GS_BASE, guest_gs_base),
856 FIELD(GUEST_LDTR_BASE, guest_ldtr_base),
857 FIELD(GUEST_TR_BASE, guest_tr_base),
858 FIELD(GUEST_GDTR_BASE, guest_gdtr_base),
859 FIELD(GUEST_IDTR_BASE, guest_idtr_base),
860 FIELD(GUEST_DR7, guest_dr7),
861 FIELD(GUEST_RSP, guest_rsp),
862 FIELD(GUEST_RIP, guest_rip),
863 FIELD(GUEST_RFLAGS, guest_rflags),
864 FIELD(GUEST_PENDING_DBG_EXCEPTIONS, guest_pending_dbg_exceptions),
865 FIELD(GUEST_SYSENTER_ESP, guest_sysenter_esp),
866 FIELD(GUEST_SYSENTER_EIP, guest_sysenter_eip),
867 FIELD(HOST_CR0, host_cr0),
868 FIELD(HOST_CR3, host_cr3),
869 FIELD(HOST_CR4, host_cr4),
870 FIELD(HOST_FS_BASE, host_fs_base),
871 FIELD(HOST_GS_BASE, host_gs_base),
872 FIELD(HOST_TR_BASE, host_tr_base),
873 FIELD(HOST_GDTR_BASE, host_gdtr_base),
874 FIELD(HOST_IDTR_BASE, host_idtr_base),
875 FIELD(HOST_IA32_SYSENTER_ESP, host_ia32_sysenter_esp),
876 FIELD(HOST_IA32_SYSENTER_EIP, host_ia32_sysenter_eip),
877 FIELD(HOST_RSP, host_rsp),
878 FIELD(HOST_RIP, host_rip),
879};
Nadav Har'El22bd0352011-05-25 23:05:57 +0300880
881static inline short vmcs_field_to_offset(unsigned long field)
882{
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +0100883 BUILD_BUG_ON(ARRAY_SIZE(vmcs_field_to_offset_table) > SHRT_MAX);
884
885 if (field >= ARRAY_SIZE(vmcs_field_to_offset_table) ||
886 vmcs_field_to_offset_table[field] == 0)
887 return -ENOENT;
888
Nadav Har'El22bd0352011-05-25 23:05:57 +0300889 return vmcs_field_to_offset_table[field];
890}
891
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300892static inline struct vmcs12 *get_vmcs12(struct kvm_vcpu *vcpu)
893{
David Matlack4f2777b2016-07-13 17:16:37 -0700894 return to_vmx(vcpu)->nested.cached_vmcs12;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300895}
896
Peter Feiner995f00a2017-06-30 17:26:32 -0700897static bool nested_ept_ad_enabled(struct kvm_vcpu *vcpu);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +0300898static unsigned long nested_ept_get_cr3(struct kvm_vcpu *vcpu);
Peter Feiner995f00a2017-06-30 17:26:32 -0700899static u64 construct_eptp(struct kvm_vcpu *vcpu, unsigned long root_hpa);
Wanpeng Lif53cd632014-12-02 19:14:58 +0800900static bool vmx_xsaves_supported(void);
Gleb Natapov776e58e2011-03-13 12:34:27 +0200901static int vmx_set_tss_addr(struct kvm *kvm, unsigned int addr);
Orit Wassermanb246dd52012-05-31 14:49:22 +0300902static void vmx_set_segment(struct kvm_vcpu *vcpu,
903 struct kvm_segment *var, int seg);
904static void vmx_get_segment(struct kvm_vcpu *vcpu,
905 struct kvm_segment *var, int seg);
Gleb Natapovd99e4152012-12-20 16:57:45 +0200906static bool guest_state_valid(struct kvm_vcpu *vcpu);
907static u32 vmx_segment_access_rights(struct kvm_segment *var);
Abel Gordonc3114422013-04-18 14:38:55 +0300908static void copy_vmcs12_to_shadow(struct vcpu_vmx *vmx);
Abel Gordon16f5b902013-04-18 14:38:25 +0300909static void copy_shadow_to_vmcs12(struct vcpu_vmx *vmx);
Tang Chena255d472014-09-16 18:41:58 +0800910static int alloc_identity_pagetable(struct kvm *kvm);
Paolo Bonzinib96fb432017-07-27 12:29:32 +0200911static bool vmx_get_nmi_mask(struct kvm_vcpu *vcpu);
912static void vmx_set_nmi_mask(struct kvm_vcpu *vcpu, bool masked);
913static bool nested_vmx_is_page_fault_vmexit(struct vmcs12 *vmcs12,
914 u16 error_code);
Avi Kivity75880a02007-06-20 11:20:04 +0300915
Avi Kivity6aa8b732006-12-10 02:21:36 -0800916static DEFINE_PER_CPU(struct vmcs *, vmxarea);
917static DEFINE_PER_CPU(struct vmcs *, current_vmcs);
Nadav Har'Eld462b812011-05-24 15:26:10 +0300918/*
919 * We maintain a per-CPU linked-list of VMCS loaded on that CPU. This is needed
920 * when a CPU is brought down, and we need to VMCLEAR all VMCSs loaded on it.
921 */
922static DEFINE_PER_CPU(struct list_head, loaded_vmcss_on_cpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800923
Feng Wubf9f6ac2015-09-18 22:29:55 +0800924/*
925 * We maintian a per-CPU linked-list of vCPU, so in wakeup_handler() we
926 * can find which vCPU should be waken up.
927 */
928static DEFINE_PER_CPU(struct list_head, blocked_vcpu_on_cpu);
929static DEFINE_PER_CPU(spinlock_t, blocked_vcpu_on_cpu_lock);
930
Radim Krčmář23611332016-09-29 22:41:33 +0200931enum {
932 VMX_IO_BITMAP_A,
933 VMX_IO_BITMAP_B,
934 VMX_MSR_BITMAP_LEGACY,
935 VMX_MSR_BITMAP_LONGMODE,
936 VMX_MSR_BITMAP_LEGACY_X2APIC_APICV,
937 VMX_MSR_BITMAP_LONGMODE_X2APIC_APICV,
938 VMX_MSR_BITMAP_LEGACY_X2APIC,
939 VMX_MSR_BITMAP_LONGMODE_X2APIC,
940 VMX_VMREAD_BITMAP,
941 VMX_VMWRITE_BITMAP,
942 VMX_BITMAP_NR
943};
944
945static unsigned long *vmx_bitmap[VMX_BITMAP_NR];
946
947#define vmx_io_bitmap_a (vmx_bitmap[VMX_IO_BITMAP_A])
948#define vmx_io_bitmap_b (vmx_bitmap[VMX_IO_BITMAP_B])
949#define vmx_msr_bitmap_legacy (vmx_bitmap[VMX_MSR_BITMAP_LEGACY])
950#define vmx_msr_bitmap_longmode (vmx_bitmap[VMX_MSR_BITMAP_LONGMODE])
951#define vmx_msr_bitmap_legacy_x2apic_apicv (vmx_bitmap[VMX_MSR_BITMAP_LEGACY_X2APIC_APICV])
952#define vmx_msr_bitmap_longmode_x2apic_apicv (vmx_bitmap[VMX_MSR_BITMAP_LONGMODE_X2APIC_APICV])
953#define vmx_msr_bitmap_legacy_x2apic (vmx_bitmap[VMX_MSR_BITMAP_LEGACY_X2APIC])
954#define vmx_msr_bitmap_longmode_x2apic (vmx_bitmap[VMX_MSR_BITMAP_LONGMODE_X2APIC])
955#define vmx_vmread_bitmap (vmx_bitmap[VMX_VMREAD_BITMAP])
956#define vmx_vmwrite_bitmap (vmx_bitmap[VMX_VMWRITE_BITMAP])
He, Qingfdef3ad2007-04-30 09:45:24 +0300957
Avi Kivity110312c2010-12-21 12:54:20 +0200958static bool cpu_has_load_ia32_efer;
Gleb Natapov8bf00a52011-10-05 14:01:22 +0200959static bool cpu_has_load_perf_global_ctrl;
Avi Kivity110312c2010-12-21 12:54:20 +0200960
Sheng Yang2384d2b2008-01-17 15:14:33 +0800961static DECLARE_BITMAP(vmx_vpid_bitmap, VMX_NR_VPIDS);
962static DEFINE_SPINLOCK(vmx_vpid_lock);
963
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +0300964static struct vmcs_config {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800965 int size;
966 int order;
Jan Dakinevich9ac7e3e2016-09-04 21:23:15 +0300967 u32 basic_cap;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800968 u32 revision_id;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +0300969 u32 pin_based_exec_ctrl;
970 u32 cpu_based_exec_ctrl;
Sheng Yangf78e0e22007-10-29 09:40:42 +0800971 u32 cpu_based_2nd_exec_ctrl;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +0300972 u32 vmexit_ctrl;
973 u32 vmentry_ctrl;
974} vmcs_config;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800975
Hannes Ederefff9e52008-11-28 17:02:06 +0100976static struct vmx_capability {
Sheng Yangd56f5462008-04-25 10:13:16 +0800977 u32 ept;
978 u32 vpid;
979} vmx_capability;
980
Avi Kivity6aa8b732006-12-10 02:21:36 -0800981#define VMX_SEGMENT_FIELD(seg) \
982 [VCPU_SREG_##seg] = { \
983 .selector = GUEST_##seg##_SELECTOR, \
984 .base = GUEST_##seg##_BASE, \
985 .limit = GUEST_##seg##_LIMIT, \
986 .ar_bytes = GUEST_##seg##_AR_BYTES, \
987 }
988
Mathias Krause772e0312012-08-30 01:30:19 +0200989static const struct kvm_vmx_segment_field {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800990 unsigned selector;
991 unsigned base;
992 unsigned limit;
993 unsigned ar_bytes;
994} kvm_vmx_segment_fields[] = {
995 VMX_SEGMENT_FIELD(CS),
996 VMX_SEGMENT_FIELD(DS),
997 VMX_SEGMENT_FIELD(ES),
998 VMX_SEGMENT_FIELD(FS),
999 VMX_SEGMENT_FIELD(GS),
1000 VMX_SEGMENT_FIELD(SS),
1001 VMX_SEGMENT_FIELD(TR),
1002 VMX_SEGMENT_FIELD(LDTR),
1003};
1004
Avi Kivity26bb0982009-09-07 11:14:12 +03001005static u64 host_efer;
1006
Avi Kivity6de4f3a2009-05-31 22:58:47 +03001007static void ept_save_pdptrs(struct kvm_vcpu *vcpu);
1008
Avi Kivity4d56c8a2007-04-19 14:28:44 +03001009/*
Brian Gerst8c065852010-07-17 09:03:26 -04001010 * Keep MSR_STAR at the end, as setup_msrs() will try to optimize it
Avi Kivity4d56c8a2007-04-19 14:28:44 +03001011 * away by decrementing the array size.
1012 */
Avi Kivity6aa8b732006-12-10 02:21:36 -08001013static const u32 vmx_msr_index[] = {
Avi Kivity05b3e0c2006-12-13 00:33:45 -08001014#ifdef CONFIG_X86_64
Avi Kivity44ea2b12009-09-06 15:55:37 +03001015 MSR_SYSCALL_MASK, MSR_LSTAR, MSR_CSTAR,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001016#endif
Brian Gerst8c065852010-07-17 09:03:26 -04001017 MSR_EFER, MSR_TSC_AUX, MSR_STAR,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001018};
Avi Kivity6aa8b732006-12-10 02:21:36 -08001019
Jan Kiszka5bb16012016-02-09 20:14:21 +01001020static inline bool is_exception_n(u32 intr_info, u8 vector)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001021{
1022 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
1023 INTR_INFO_VALID_MASK)) ==
Jan Kiszka5bb16012016-02-09 20:14:21 +01001024 (INTR_TYPE_HARD_EXCEPTION | vector | INTR_INFO_VALID_MASK);
1025}
1026
Jan Kiszka6f054852016-02-09 20:15:18 +01001027static inline bool is_debug(u32 intr_info)
1028{
1029 return is_exception_n(intr_info, DB_VECTOR);
1030}
1031
1032static inline bool is_breakpoint(u32 intr_info)
1033{
1034 return is_exception_n(intr_info, BP_VECTOR);
1035}
1036
Jan Kiszka5bb16012016-02-09 20:14:21 +01001037static inline bool is_page_fault(u32 intr_info)
1038{
1039 return is_exception_n(intr_info, PF_VECTOR);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001040}
1041
Gui Jianfeng31299942010-03-15 17:29:09 +08001042static inline bool is_no_device(u32 intr_info)
Anthony Liguori2ab455c2007-04-27 09:29:49 +03001043{
Jan Kiszka5bb16012016-02-09 20:14:21 +01001044 return is_exception_n(intr_info, NM_VECTOR);
Anthony Liguori2ab455c2007-04-27 09:29:49 +03001045}
1046
Gui Jianfeng31299942010-03-15 17:29:09 +08001047static inline bool is_invalid_opcode(u32 intr_info)
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001048{
Jan Kiszka5bb16012016-02-09 20:14:21 +01001049 return is_exception_n(intr_info, UD_VECTOR);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001050}
1051
Gui Jianfeng31299942010-03-15 17:29:09 +08001052static inline bool is_external_interrupt(u32 intr_info)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001053{
1054 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK))
1055 == (INTR_TYPE_EXT_INTR | INTR_INFO_VALID_MASK);
1056}
1057
Gui Jianfeng31299942010-03-15 17:29:09 +08001058static inline bool is_machine_check(u32 intr_info)
Andi Kleena0861c02009-06-08 17:37:09 +08001059{
1060 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
1061 INTR_INFO_VALID_MASK)) ==
1062 (INTR_TYPE_HARD_EXCEPTION | MC_VECTOR | INTR_INFO_VALID_MASK);
1063}
1064
Gui Jianfeng31299942010-03-15 17:29:09 +08001065static inline bool cpu_has_vmx_msr_bitmap(void)
Sheng Yang25c5f222008-03-28 13:18:56 +08001066{
Sheng Yang04547152009-04-01 15:52:31 +08001067 return vmcs_config.cpu_based_exec_ctrl & CPU_BASED_USE_MSR_BITMAPS;
Sheng Yang25c5f222008-03-28 13:18:56 +08001068}
1069
Gui Jianfeng31299942010-03-15 17:29:09 +08001070static inline bool cpu_has_vmx_tpr_shadow(void)
Yang, Sheng6e5d8652007-09-12 18:03:11 +08001071{
Sheng Yang04547152009-04-01 15:52:31 +08001072 return vmcs_config.cpu_based_exec_ctrl & CPU_BASED_TPR_SHADOW;
Yang, Sheng6e5d8652007-09-12 18:03:11 +08001073}
1074
Paolo Bonzini35754c92015-07-29 12:05:37 +02001075static inline bool cpu_need_tpr_shadow(struct kvm_vcpu *vcpu)
Yang, Sheng6e5d8652007-09-12 18:03:11 +08001076{
Paolo Bonzini35754c92015-07-29 12:05:37 +02001077 return cpu_has_vmx_tpr_shadow() && lapic_in_kernel(vcpu);
Yang, Sheng6e5d8652007-09-12 18:03:11 +08001078}
1079
Gui Jianfeng31299942010-03-15 17:29:09 +08001080static inline bool cpu_has_secondary_exec_ctrls(void)
Sheng Yangf78e0e22007-10-29 09:40:42 +08001081{
Sheng Yang04547152009-04-01 15:52:31 +08001082 return vmcs_config.cpu_based_exec_ctrl &
1083 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
Sheng Yangf78e0e22007-10-29 09:40:42 +08001084}
1085
Avi Kivity774ead32007-12-26 13:57:04 +02001086static inline bool cpu_has_vmx_virtualize_apic_accesses(void)
Sheng Yangf78e0e22007-10-29 09:40:42 +08001087{
Sheng Yang04547152009-04-01 15:52:31 +08001088 return vmcs_config.cpu_based_2nd_exec_ctrl &
1089 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
1090}
1091
Yang Zhang8d146952013-01-25 10:18:50 +08001092static inline bool cpu_has_vmx_virtualize_x2apic_mode(void)
1093{
1094 return vmcs_config.cpu_based_2nd_exec_ctrl &
1095 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
1096}
1097
Yang Zhang83d4c282013-01-25 10:18:49 +08001098static inline bool cpu_has_vmx_apic_register_virt(void)
1099{
1100 return vmcs_config.cpu_based_2nd_exec_ctrl &
1101 SECONDARY_EXEC_APIC_REGISTER_VIRT;
1102}
1103
Yang Zhangc7c9c562013-01-25 10:18:51 +08001104static inline bool cpu_has_vmx_virtual_intr_delivery(void)
1105{
1106 return vmcs_config.cpu_based_2nd_exec_ctrl &
1107 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY;
1108}
1109
Yunhong Jiang64672c92016-06-13 14:19:59 -07001110/*
1111 * Comment's format: document - errata name - stepping - processor name.
1112 * Refer from
1113 * https://www.virtualbox.org/svn/vbox/trunk/src/VBox/VMM/VMMR0/HMR0.cpp
1114 */
1115static u32 vmx_preemption_cpu_tfms[] = {
1116/* 323344.pdf - BA86 - D0 - Xeon 7500 Series */
11170x000206E6,
1118/* 323056.pdf - AAX65 - C2 - Xeon L3406 */
1119/* 322814.pdf - AAT59 - C2 - i7-600, i5-500, i5-400 and i3-300 Mobile */
1120/* 322911.pdf - AAU65 - C2 - i5-600, i3-500 Desktop and Pentium G6950 */
11210x00020652,
1122/* 322911.pdf - AAU65 - K0 - i5-600, i3-500 Desktop and Pentium G6950 */
11230x00020655,
1124/* 322373.pdf - AAO95 - B1 - Xeon 3400 Series */
1125/* 322166.pdf - AAN92 - B1 - i7-800 and i5-700 Desktop */
1126/*
1127 * 320767.pdf - AAP86 - B1 -
1128 * i7-900 Mobile Extreme, i7-800 and i7-700 Mobile
1129 */
11300x000106E5,
1131/* 321333.pdf - AAM126 - C0 - Xeon 3500 */
11320x000106A0,
1133/* 321333.pdf - AAM126 - C1 - Xeon 3500 */
11340x000106A1,
1135/* 320836.pdf - AAJ124 - C0 - i7-900 Desktop Extreme and i7-900 Desktop */
11360x000106A4,
1137 /* 321333.pdf - AAM126 - D0 - Xeon 3500 */
1138 /* 321324.pdf - AAK139 - D0 - Xeon 5500 */
1139 /* 320836.pdf - AAJ124 - D0 - i7-900 Extreme and i7-900 Desktop */
11400x000106A5,
1141};
1142
1143static inline bool cpu_has_broken_vmx_preemption_timer(void)
1144{
1145 u32 eax = cpuid_eax(0x00000001), i;
1146
1147 /* Clear the reserved bits */
1148 eax &= ~(0x3U << 14 | 0xfU << 28);
Wei Yongjun03f6a222016-07-04 15:13:07 +00001149 for (i = 0; i < ARRAY_SIZE(vmx_preemption_cpu_tfms); i++)
Yunhong Jiang64672c92016-06-13 14:19:59 -07001150 if (eax == vmx_preemption_cpu_tfms[i])
1151 return true;
1152
1153 return false;
1154}
1155
1156static inline bool cpu_has_vmx_preemption_timer(void)
1157{
Yunhong Jiang64672c92016-06-13 14:19:59 -07001158 return vmcs_config.pin_based_exec_ctrl &
1159 PIN_BASED_VMX_PREEMPTION_TIMER;
1160}
1161
Yang Zhang01e439b2013-04-11 19:25:12 +08001162static inline bool cpu_has_vmx_posted_intr(void)
1163{
Paolo Bonzinid6a858d2015-09-28 11:58:14 +02001164 return IS_ENABLED(CONFIG_X86_LOCAL_APIC) &&
1165 vmcs_config.pin_based_exec_ctrl & PIN_BASED_POSTED_INTR;
Yang Zhang01e439b2013-04-11 19:25:12 +08001166}
1167
1168static inline bool cpu_has_vmx_apicv(void)
1169{
1170 return cpu_has_vmx_apic_register_virt() &&
1171 cpu_has_vmx_virtual_intr_delivery() &&
1172 cpu_has_vmx_posted_intr();
1173}
1174
Sheng Yang04547152009-04-01 15:52:31 +08001175static inline bool cpu_has_vmx_flexpriority(void)
1176{
1177 return cpu_has_vmx_tpr_shadow() &&
1178 cpu_has_vmx_virtualize_apic_accesses();
Sheng Yangf78e0e22007-10-29 09:40:42 +08001179}
1180
Marcelo Tosattie7997942009-06-11 12:07:40 -03001181static inline bool cpu_has_vmx_ept_execute_only(void)
1182{
Gui Jianfeng31299942010-03-15 17:29:09 +08001183 return vmx_capability.ept & VMX_EPT_EXECUTE_ONLY_BIT;
Marcelo Tosattie7997942009-06-11 12:07:40 -03001184}
1185
Marcelo Tosattie7997942009-06-11 12:07:40 -03001186static inline bool cpu_has_vmx_ept_2m_page(void)
1187{
Gui Jianfeng31299942010-03-15 17:29:09 +08001188 return vmx_capability.ept & VMX_EPT_2MB_PAGE_BIT;
Marcelo Tosattie7997942009-06-11 12:07:40 -03001189}
1190
Sheng Yang878403b2010-01-05 19:02:29 +08001191static inline bool cpu_has_vmx_ept_1g_page(void)
1192{
Gui Jianfeng31299942010-03-15 17:29:09 +08001193 return vmx_capability.ept & VMX_EPT_1GB_PAGE_BIT;
Sheng Yang878403b2010-01-05 19:02:29 +08001194}
1195
Sheng Yang4bc9b982010-06-02 14:05:24 +08001196static inline bool cpu_has_vmx_ept_4levels(void)
1197{
1198 return vmx_capability.ept & VMX_EPT_PAGE_WALK_4_BIT;
1199}
1200
Xudong Hao83c3a332012-05-28 19:33:35 +08001201static inline bool cpu_has_vmx_ept_ad_bits(void)
1202{
1203 return vmx_capability.ept & VMX_EPT_AD_BIT;
1204}
1205
Gui Jianfeng31299942010-03-15 17:29:09 +08001206static inline bool cpu_has_vmx_invept_context(void)
Sheng Yangd56f5462008-04-25 10:13:16 +08001207{
Gui Jianfeng31299942010-03-15 17:29:09 +08001208 return vmx_capability.ept & VMX_EPT_EXTENT_CONTEXT_BIT;
Sheng Yangd56f5462008-04-25 10:13:16 +08001209}
1210
Gui Jianfeng31299942010-03-15 17:29:09 +08001211static inline bool cpu_has_vmx_invept_global(void)
Sheng Yangd56f5462008-04-25 10:13:16 +08001212{
Gui Jianfeng31299942010-03-15 17:29:09 +08001213 return vmx_capability.ept & VMX_EPT_EXTENT_GLOBAL_BIT;
Sheng Yangd56f5462008-04-25 10:13:16 +08001214}
1215
Gui Jianfeng518c8ae2010-06-04 08:51:39 +08001216static inline bool cpu_has_vmx_invvpid_single(void)
1217{
1218 return vmx_capability.vpid & VMX_VPID_EXTENT_SINGLE_CONTEXT_BIT;
1219}
1220
Gui Jianfengb9d762f2010-06-07 10:32:29 +08001221static inline bool cpu_has_vmx_invvpid_global(void)
1222{
1223 return vmx_capability.vpid & VMX_VPID_EXTENT_GLOBAL_CONTEXT_BIT;
1224}
1225
Wanpeng Li08d839c2017-03-23 05:30:08 -07001226static inline bool cpu_has_vmx_invvpid(void)
1227{
1228 return vmx_capability.vpid & VMX_VPID_INVVPID_BIT;
1229}
1230
Gui Jianfeng31299942010-03-15 17:29:09 +08001231static inline bool cpu_has_vmx_ept(void)
Sheng Yangd56f5462008-04-25 10:13:16 +08001232{
Sheng Yang04547152009-04-01 15:52:31 +08001233 return vmcs_config.cpu_based_2nd_exec_ctrl &
1234 SECONDARY_EXEC_ENABLE_EPT;
Sheng Yangd56f5462008-04-25 10:13:16 +08001235}
1236
Gui Jianfeng31299942010-03-15 17:29:09 +08001237static inline bool cpu_has_vmx_unrestricted_guest(void)
Nitin A Kamble3a624e22009-06-08 11:34:16 -07001238{
1239 return vmcs_config.cpu_based_2nd_exec_ctrl &
1240 SECONDARY_EXEC_UNRESTRICTED_GUEST;
1241}
1242
Gui Jianfeng31299942010-03-15 17:29:09 +08001243static inline bool cpu_has_vmx_ple(void)
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08001244{
1245 return vmcs_config.cpu_based_2nd_exec_ctrl &
1246 SECONDARY_EXEC_PAUSE_LOOP_EXITING;
1247}
1248
Jan Dakinevich9ac7e3e2016-09-04 21:23:15 +03001249static inline bool cpu_has_vmx_basic_inout(void)
1250{
1251 return (((u64)vmcs_config.basic_cap << 32) & VMX_BASIC_INOUT);
1252}
1253
Paolo Bonzini35754c92015-07-29 12:05:37 +02001254static inline bool cpu_need_virtualize_apic_accesses(struct kvm_vcpu *vcpu)
Sheng Yangf78e0e22007-10-29 09:40:42 +08001255{
Paolo Bonzini35754c92015-07-29 12:05:37 +02001256 return flexpriority_enabled && lapic_in_kernel(vcpu);
Sheng Yangf78e0e22007-10-29 09:40:42 +08001257}
1258
Gui Jianfeng31299942010-03-15 17:29:09 +08001259static inline bool cpu_has_vmx_vpid(void)
Sheng Yang2384d2b2008-01-17 15:14:33 +08001260{
Sheng Yang04547152009-04-01 15:52:31 +08001261 return vmcs_config.cpu_based_2nd_exec_ctrl &
1262 SECONDARY_EXEC_ENABLE_VPID;
Sheng Yang2384d2b2008-01-17 15:14:33 +08001263}
1264
Gui Jianfeng31299942010-03-15 17:29:09 +08001265static inline bool cpu_has_vmx_rdtscp(void)
Sheng Yang4e47c7a2009-12-18 16:48:47 +08001266{
1267 return vmcs_config.cpu_based_2nd_exec_ctrl &
1268 SECONDARY_EXEC_RDTSCP;
1269}
1270
Mao, Junjiead756a12012-07-02 01:18:48 +00001271static inline bool cpu_has_vmx_invpcid(void)
1272{
1273 return vmcs_config.cpu_based_2nd_exec_ctrl &
1274 SECONDARY_EXEC_ENABLE_INVPCID;
1275}
1276
Sheng Yangf5f48ee2010-06-30 12:25:15 +08001277static inline bool cpu_has_vmx_wbinvd_exit(void)
1278{
1279 return vmcs_config.cpu_based_2nd_exec_ctrl &
1280 SECONDARY_EXEC_WBINVD_EXITING;
1281}
1282
Abel Gordonabc4fc52013-04-18 14:35:25 +03001283static inline bool cpu_has_vmx_shadow_vmcs(void)
1284{
1285 u64 vmx_msr;
1286 rdmsrl(MSR_IA32_VMX_MISC, vmx_msr);
1287 /* check if the cpu supports writing r/o exit information fields */
1288 if (!(vmx_msr & MSR_IA32_VMX_MISC_VMWRITE_SHADOW_RO_FIELDS))
1289 return false;
1290
1291 return vmcs_config.cpu_based_2nd_exec_ctrl &
1292 SECONDARY_EXEC_SHADOW_VMCS;
1293}
1294
Kai Huang843e4332015-01-28 10:54:28 +08001295static inline bool cpu_has_vmx_pml(void)
1296{
1297 return vmcs_config.cpu_based_2nd_exec_ctrl & SECONDARY_EXEC_ENABLE_PML;
1298}
1299
Haozhong Zhang64903d62015-10-20 15:39:09 +08001300static inline bool cpu_has_vmx_tsc_scaling(void)
1301{
1302 return vmcs_config.cpu_based_2nd_exec_ctrl &
1303 SECONDARY_EXEC_TSC_SCALING;
1304}
1305
Bandan Das2a499e42017-08-03 15:54:41 -04001306static inline bool cpu_has_vmx_vmfunc(void)
1307{
1308 return vmcs_config.cpu_based_2nd_exec_ctrl &
1309 SECONDARY_EXEC_ENABLE_VMFUNC;
1310}
1311
Sheng Yang04547152009-04-01 15:52:31 +08001312static inline bool report_flexpriority(void)
1313{
1314 return flexpriority_enabled;
1315}
1316
Jim Mattsonc7c2c7092017-05-05 11:28:09 -07001317static inline unsigned nested_cpu_vmx_misc_cr3_count(struct kvm_vcpu *vcpu)
1318{
1319 return vmx_misc_cr3_count(to_vmx(vcpu)->nested.nested_vmx_misc_low);
1320}
1321
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03001322static inline bool nested_cpu_has(struct vmcs12 *vmcs12, u32 bit)
1323{
1324 return vmcs12->cpu_based_vm_exec_control & bit;
1325}
1326
1327static inline bool nested_cpu_has2(struct vmcs12 *vmcs12, u32 bit)
1328{
1329 return (vmcs12->cpu_based_vm_exec_control &
1330 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) &&
1331 (vmcs12->secondary_vm_exec_control & bit);
1332}
1333
Nadav Har'Elf5c43682013-08-05 11:07:20 +03001334static inline bool nested_cpu_has_virtual_nmis(struct vmcs12 *vmcs12)
Nadav Har'El644d7112011-05-25 23:12:35 +03001335{
1336 return vmcs12->pin_based_vm_exec_control & PIN_BASED_VIRTUAL_NMIS;
1337}
1338
Jan Kiszkaf41245002014-03-07 20:03:13 +01001339static inline bool nested_cpu_has_preemption_timer(struct vmcs12 *vmcs12)
1340{
1341 return vmcs12->pin_based_vm_exec_control &
1342 PIN_BASED_VMX_PREEMPTION_TIMER;
1343}
1344
Nadav Har'El155a97a2013-08-05 11:07:16 +03001345static inline int nested_cpu_has_ept(struct vmcs12 *vmcs12)
1346{
1347 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_EPT);
1348}
1349
Wanpeng Li81dc01f2014-12-04 19:11:07 +08001350static inline bool nested_cpu_has_xsaves(struct vmcs12 *vmcs12)
1351{
1352 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_XSAVES) &&
1353 vmx_xsaves_supported();
1354}
1355
Bandan Dasc5f983f2017-05-05 15:25:14 -04001356static inline bool nested_cpu_has_pml(struct vmcs12 *vmcs12)
1357{
1358 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_PML);
1359}
1360
Wincy Vanf2b93282015-02-03 23:56:03 +08001361static inline bool nested_cpu_has_virt_x2apic_mode(struct vmcs12 *vmcs12)
1362{
1363 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE);
1364}
1365
Wanpeng Li5c614b32015-10-13 09:18:36 -07001366static inline bool nested_cpu_has_vpid(struct vmcs12 *vmcs12)
1367{
1368 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_VPID);
1369}
1370
Wincy Van82f0dd42015-02-03 23:57:18 +08001371static inline bool nested_cpu_has_apic_reg_virt(struct vmcs12 *vmcs12)
1372{
1373 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_APIC_REGISTER_VIRT);
1374}
1375
Wincy Van608406e2015-02-03 23:57:51 +08001376static inline bool nested_cpu_has_vid(struct vmcs12 *vmcs12)
1377{
1378 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
1379}
1380
Wincy Van705699a2015-02-03 23:58:17 +08001381static inline bool nested_cpu_has_posted_intr(struct vmcs12 *vmcs12)
1382{
1383 return vmcs12->pin_based_vm_exec_control & PIN_BASED_POSTED_INTR;
1384}
1385
Bandan Das27c42a12017-08-03 15:54:42 -04001386static inline bool nested_cpu_has_vmfunc(struct vmcs12 *vmcs12)
1387{
1388 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_VMFUNC);
1389}
1390
Jim Mattsonef85b672016-12-12 11:01:37 -08001391static inline bool is_nmi(u32 intr_info)
Nadav Har'El644d7112011-05-25 23:12:35 +03001392{
1393 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK))
Jim Mattsonef85b672016-12-12 11:01:37 -08001394 == (INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK);
Nadav Har'El644d7112011-05-25 23:12:35 +03001395}
1396
Jan Kiszka533558b2014-01-04 18:47:20 +01001397static void nested_vmx_vmexit(struct kvm_vcpu *vcpu, u32 exit_reason,
1398 u32 exit_intr_info,
1399 unsigned long exit_qualification);
Nadav Har'El7c177932011-05-25 23:12:04 +03001400static void nested_vmx_entry_failure(struct kvm_vcpu *vcpu,
1401 struct vmcs12 *vmcs12,
1402 u32 reason, unsigned long qualification);
1403
Rusty Russell8b9cf982007-07-30 16:31:43 +10001404static int __find_msr_index(struct vcpu_vmx *vmx, u32 msr)
Avi Kivity7725f0b2006-12-13 00:34:01 -08001405{
1406 int i;
1407
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001408 for (i = 0; i < vmx->nmsrs; ++i)
Avi Kivity26bb0982009-09-07 11:14:12 +03001409 if (vmx_msr_index[vmx->guest_msrs[i].index] == msr)
Eddie Donga75beee2007-05-17 18:55:15 +03001410 return i;
1411 return -1;
1412}
1413
Sheng Yang2384d2b2008-01-17 15:14:33 +08001414static inline void __invvpid(int ext, u16 vpid, gva_t gva)
1415{
1416 struct {
1417 u64 vpid : 16;
1418 u64 rsvd : 48;
1419 u64 gva;
1420 } operand = { vpid, 0, gva };
1421
Avi Kivity4ecac3f2008-05-13 13:23:38 +03001422 asm volatile (__ex(ASM_VMX_INVVPID)
Sheng Yang2384d2b2008-01-17 15:14:33 +08001423 /* CF==1 or ZF==1 --> rc = -1 */
1424 "; ja 1f ; ud2 ; 1:"
1425 : : "a"(&operand), "c"(ext) : "cc", "memory");
1426}
1427
Sheng Yang14394422008-04-28 12:24:45 +08001428static inline void __invept(int ext, u64 eptp, gpa_t gpa)
1429{
1430 struct {
1431 u64 eptp, gpa;
1432 } operand = {eptp, gpa};
1433
Avi Kivity4ecac3f2008-05-13 13:23:38 +03001434 asm volatile (__ex(ASM_VMX_INVEPT)
Sheng Yang14394422008-04-28 12:24:45 +08001435 /* CF==1 or ZF==1 --> rc = -1 */
1436 "; ja 1f ; ud2 ; 1:\n"
1437 : : "a" (&operand), "c" (ext) : "cc", "memory");
1438}
1439
Avi Kivity26bb0982009-09-07 11:14:12 +03001440static struct shared_msr_entry *find_msr_entry(struct vcpu_vmx *vmx, u32 msr)
Eddie Donga75beee2007-05-17 18:55:15 +03001441{
1442 int i;
1443
Rusty Russell8b9cf982007-07-30 16:31:43 +10001444 i = __find_msr_index(vmx, msr);
Eddie Donga75beee2007-05-17 18:55:15 +03001445 if (i >= 0)
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001446 return &vmx->guest_msrs[i];
Al Viro8b6d44c2007-02-09 16:38:40 +00001447 return NULL;
Avi Kivity7725f0b2006-12-13 00:34:01 -08001448}
1449
Avi Kivity6aa8b732006-12-10 02:21:36 -08001450static void vmcs_clear(struct vmcs *vmcs)
1451{
1452 u64 phys_addr = __pa(vmcs);
1453 u8 error;
1454
Avi Kivity4ecac3f2008-05-13 13:23:38 +03001455 asm volatile (__ex(ASM_VMX_VMCLEAR_RAX) "; setna %0"
Avi Kivity16d8f722010-12-21 16:51:50 +02001456 : "=qm"(error) : "a"(&phys_addr), "m"(phys_addr)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001457 : "cc", "memory");
1458 if (error)
1459 printk(KERN_ERR "kvm: vmclear fail: %p/%llx\n",
1460 vmcs, phys_addr);
1461}
1462
Nadav Har'Eld462b812011-05-24 15:26:10 +03001463static inline void loaded_vmcs_init(struct loaded_vmcs *loaded_vmcs)
1464{
1465 vmcs_clear(loaded_vmcs->vmcs);
Jim Mattson355f4fb2016-10-28 08:29:39 -07001466 if (loaded_vmcs->shadow_vmcs && loaded_vmcs->launched)
1467 vmcs_clear(loaded_vmcs->shadow_vmcs);
Nadav Har'Eld462b812011-05-24 15:26:10 +03001468 loaded_vmcs->cpu = -1;
1469 loaded_vmcs->launched = 0;
1470}
1471
Dongxiao Xu7725b892010-05-11 18:29:38 +08001472static void vmcs_load(struct vmcs *vmcs)
1473{
1474 u64 phys_addr = __pa(vmcs);
1475 u8 error;
1476
1477 asm volatile (__ex(ASM_VMX_VMPTRLD_RAX) "; setna %0"
Avi Kivity16d8f722010-12-21 16:51:50 +02001478 : "=qm"(error) : "a"(&phys_addr), "m"(phys_addr)
Dongxiao Xu7725b892010-05-11 18:29:38 +08001479 : "cc", "memory");
1480 if (error)
Nadav Har'El2844d842011-05-25 23:16:40 +03001481 printk(KERN_ERR "kvm: vmptrld %p/%llx failed\n",
Dongxiao Xu7725b892010-05-11 18:29:38 +08001482 vmcs, phys_addr);
1483}
1484
Dave Young2965faa2015-09-09 15:38:55 -07001485#ifdef CONFIG_KEXEC_CORE
Zhang Yanfei8f536b72012-12-06 23:43:34 +08001486/*
1487 * This bitmap is used to indicate whether the vmclear
1488 * operation is enabled on all cpus. All disabled by
1489 * default.
1490 */
1491static cpumask_t crash_vmclear_enabled_bitmap = CPU_MASK_NONE;
1492
1493static inline void crash_enable_local_vmclear(int cpu)
1494{
1495 cpumask_set_cpu(cpu, &crash_vmclear_enabled_bitmap);
1496}
1497
1498static inline void crash_disable_local_vmclear(int cpu)
1499{
1500 cpumask_clear_cpu(cpu, &crash_vmclear_enabled_bitmap);
1501}
1502
1503static inline int crash_local_vmclear_enabled(int cpu)
1504{
1505 return cpumask_test_cpu(cpu, &crash_vmclear_enabled_bitmap);
1506}
1507
1508static void crash_vmclear_local_loaded_vmcss(void)
1509{
1510 int cpu = raw_smp_processor_id();
1511 struct loaded_vmcs *v;
1512
1513 if (!crash_local_vmclear_enabled(cpu))
1514 return;
1515
1516 list_for_each_entry(v, &per_cpu(loaded_vmcss_on_cpu, cpu),
1517 loaded_vmcss_on_cpu_link)
1518 vmcs_clear(v->vmcs);
1519}
1520#else
1521static inline void crash_enable_local_vmclear(int cpu) { }
1522static inline void crash_disable_local_vmclear(int cpu) { }
Dave Young2965faa2015-09-09 15:38:55 -07001523#endif /* CONFIG_KEXEC_CORE */
Zhang Yanfei8f536b72012-12-06 23:43:34 +08001524
Nadav Har'Eld462b812011-05-24 15:26:10 +03001525static void __loaded_vmcs_clear(void *arg)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001526{
Nadav Har'Eld462b812011-05-24 15:26:10 +03001527 struct loaded_vmcs *loaded_vmcs = arg;
Ingo Molnard3b2c332007-01-05 16:36:23 -08001528 int cpu = raw_smp_processor_id();
Avi Kivity6aa8b732006-12-10 02:21:36 -08001529
Nadav Har'Eld462b812011-05-24 15:26:10 +03001530 if (loaded_vmcs->cpu != cpu)
1531 return; /* vcpu migration can race with cpu offline */
1532 if (per_cpu(current_vmcs, cpu) == loaded_vmcs->vmcs)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001533 per_cpu(current_vmcs, cpu) = NULL;
Zhang Yanfei8f536b72012-12-06 23:43:34 +08001534 crash_disable_local_vmclear(cpu);
Nadav Har'Eld462b812011-05-24 15:26:10 +03001535 list_del(&loaded_vmcs->loaded_vmcss_on_cpu_link);
Xiao Guangrong5a560f82012-11-28 20:54:14 +08001536
1537 /*
1538 * we should ensure updating loaded_vmcs->loaded_vmcss_on_cpu_link
1539 * is before setting loaded_vmcs->vcpu to -1 which is done in
1540 * loaded_vmcs_init. Otherwise, other cpu can see vcpu = -1 fist
1541 * then adds the vmcs into percpu list before it is deleted.
1542 */
1543 smp_wmb();
1544
Nadav Har'Eld462b812011-05-24 15:26:10 +03001545 loaded_vmcs_init(loaded_vmcs);
Zhang Yanfei8f536b72012-12-06 23:43:34 +08001546 crash_enable_local_vmclear(cpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001547}
1548
Nadav Har'Eld462b812011-05-24 15:26:10 +03001549static void loaded_vmcs_clear(struct loaded_vmcs *loaded_vmcs)
Avi Kivity8d0be2b2007-02-12 00:54:46 -08001550{
Xiao Guangronge6c7d322012-11-28 20:53:15 +08001551 int cpu = loaded_vmcs->cpu;
1552
1553 if (cpu != -1)
1554 smp_call_function_single(cpu,
1555 __loaded_vmcs_clear, loaded_vmcs, 1);
Avi Kivity8d0be2b2007-02-12 00:54:46 -08001556}
1557
Wanpeng Lidd5f5342015-09-23 18:26:57 +08001558static inline void vpid_sync_vcpu_single(int vpid)
Sheng Yang2384d2b2008-01-17 15:14:33 +08001559{
Wanpeng Lidd5f5342015-09-23 18:26:57 +08001560 if (vpid == 0)
Sheng Yang2384d2b2008-01-17 15:14:33 +08001561 return;
1562
Gui Jianfeng518c8ae2010-06-04 08:51:39 +08001563 if (cpu_has_vmx_invvpid_single())
Wanpeng Lidd5f5342015-09-23 18:26:57 +08001564 __invvpid(VMX_VPID_EXTENT_SINGLE_CONTEXT, vpid, 0);
Sheng Yang2384d2b2008-01-17 15:14:33 +08001565}
1566
Gui Jianfengb9d762f2010-06-07 10:32:29 +08001567static inline void vpid_sync_vcpu_global(void)
1568{
1569 if (cpu_has_vmx_invvpid_global())
1570 __invvpid(VMX_VPID_EXTENT_ALL_CONTEXT, 0, 0);
1571}
1572
Wanpeng Lidd5f5342015-09-23 18:26:57 +08001573static inline void vpid_sync_context(int vpid)
Gui Jianfengb9d762f2010-06-07 10:32:29 +08001574{
1575 if (cpu_has_vmx_invvpid_single())
Wanpeng Lidd5f5342015-09-23 18:26:57 +08001576 vpid_sync_vcpu_single(vpid);
Gui Jianfengb9d762f2010-06-07 10:32:29 +08001577 else
1578 vpid_sync_vcpu_global();
1579}
1580
Sheng Yang14394422008-04-28 12:24:45 +08001581static inline void ept_sync_global(void)
1582{
1583 if (cpu_has_vmx_invept_global())
1584 __invept(VMX_EPT_EXTENT_GLOBAL, 0, 0);
1585}
1586
1587static inline void ept_sync_context(u64 eptp)
1588{
Avi Kivity089d0342009-03-23 18:26:32 +02001589 if (enable_ept) {
Sheng Yang14394422008-04-28 12:24:45 +08001590 if (cpu_has_vmx_invept_context())
1591 __invept(VMX_EPT_EXTENT_CONTEXT, eptp, 0);
1592 else
1593 ept_sync_global();
1594 }
1595}
1596
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001597static __always_inline void vmcs_check16(unsigned long field)
1598{
1599 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2000,
1600 "16-bit accessor invalid for 64-bit field");
1601 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2001,
1602 "16-bit accessor invalid for 64-bit high field");
1603 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x4000,
1604 "16-bit accessor invalid for 32-bit high field");
1605 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x6000,
1606 "16-bit accessor invalid for natural width field");
1607}
1608
1609static __always_inline void vmcs_check32(unsigned long field)
1610{
1611 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0,
1612 "32-bit accessor invalid for 16-bit field");
1613 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x6000,
1614 "32-bit accessor invalid for natural width field");
1615}
1616
1617static __always_inline void vmcs_check64(unsigned long field)
1618{
1619 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0,
1620 "64-bit accessor invalid for 16-bit field");
1621 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2001,
1622 "64-bit accessor invalid for 64-bit high field");
1623 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x4000,
1624 "64-bit accessor invalid for 32-bit field");
1625 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x6000,
1626 "64-bit accessor invalid for natural width field");
1627}
1628
1629static __always_inline void vmcs_checkl(unsigned long field)
1630{
1631 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0,
1632 "Natural width accessor invalid for 16-bit field");
1633 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2000,
1634 "Natural width accessor invalid for 64-bit field");
1635 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2001,
1636 "Natural width accessor invalid for 64-bit high field");
1637 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x4000,
1638 "Natural width accessor invalid for 32-bit field");
1639}
1640
1641static __always_inline unsigned long __vmcs_readl(unsigned long field)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001642{
Avi Kivity5e520e62011-05-15 10:13:12 -04001643 unsigned long value;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001644
Avi Kivity5e520e62011-05-15 10:13:12 -04001645 asm volatile (__ex_clear(ASM_VMX_VMREAD_RDX_RAX, "%0")
1646 : "=a"(value) : "d"(field) : "cc");
Avi Kivity6aa8b732006-12-10 02:21:36 -08001647 return value;
1648}
1649
Avi Kivity96304212011-05-15 10:13:13 -04001650static __always_inline u16 vmcs_read16(unsigned long field)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001651{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001652 vmcs_check16(field);
1653 return __vmcs_readl(field);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001654}
1655
Avi Kivity96304212011-05-15 10:13:13 -04001656static __always_inline u32 vmcs_read32(unsigned long field)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001657{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001658 vmcs_check32(field);
1659 return __vmcs_readl(field);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001660}
1661
Avi Kivity96304212011-05-15 10:13:13 -04001662static __always_inline u64 vmcs_read64(unsigned long field)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001663{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001664 vmcs_check64(field);
Avi Kivity05b3e0c2006-12-13 00:33:45 -08001665#ifdef CONFIG_X86_64
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001666 return __vmcs_readl(field);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001667#else
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001668 return __vmcs_readl(field) | ((u64)__vmcs_readl(field+1) << 32);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001669#endif
1670}
1671
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001672static __always_inline unsigned long vmcs_readl(unsigned long field)
1673{
1674 vmcs_checkl(field);
1675 return __vmcs_readl(field);
1676}
1677
Avi Kivitye52de1b2007-01-05 16:36:56 -08001678static noinline void vmwrite_error(unsigned long field, unsigned long value)
1679{
1680 printk(KERN_ERR "vmwrite error: reg %lx value %lx (err %d)\n",
1681 field, value, vmcs_read32(VM_INSTRUCTION_ERROR));
1682 dump_stack();
1683}
1684
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001685static __always_inline void __vmcs_writel(unsigned long field, unsigned long value)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001686{
1687 u8 error;
1688
Avi Kivity4ecac3f2008-05-13 13:23:38 +03001689 asm volatile (__ex(ASM_VMX_VMWRITE_RAX_RDX) "; setna %0"
Mike Dayd77c26f2007-10-08 09:02:08 -04001690 : "=q"(error) : "a"(value), "d"(field) : "cc");
Avi Kivitye52de1b2007-01-05 16:36:56 -08001691 if (unlikely(error))
1692 vmwrite_error(field, value);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001693}
1694
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001695static __always_inline void vmcs_write16(unsigned long field, u16 value)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001696{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001697 vmcs_check16(field);
1698 __vmcs_writel(field, value);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001699}
1700
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001701static __always_inline void vmcs_write32(unsigned long field, u32 value)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001702{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001703 vmcs_check32(field);
1704 __vmcs_writel(field, value);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001705}
1706
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001707static __always_inline void vmcs_write64(unsigned long field, u64 value)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001708{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001709 vmcs_check64(field);
1710 __vmcs_writel(field, value);
Avi Kivity7682f2d2008-05-12 19:25:43 +03001711#ifndef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08001712 asm volatile ("");
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001713 __vmcs_writel(field+1, value >> 32);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001714#endif
1715}
1716
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001717static __always_inline void vmcs_writel(unsigned long field, unsigned long value)
Anthony Liguori2ab455c2007-04-27 09:29:49 +03001718{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001719 vmcs_checkl(field);
1720 __vmcs_writel(field, value);
Anthony Liguori2ab455c2007-04-27 09:29:49 +03001721}
1722
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001723static __always_inline void vmcs_clear_bits(unsigned long field, u32 mask)
Anthony Liguori2ab455c2007-04-27 09:29:49 +03001724{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001725 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x2000,
1726 "vmcs_clear_bits does not support 64-bit fields");
1727 __vmcs_writel(field, __vmcs_readl(field) & ~mask);
1728}
1729
1730static __always_inline void vmcs_set_bits(unsigned long field, u32 mask)
1731{
1732 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x2000,
1733 "vmcs_set_bits does not support 64-bit fields");
1734 __vmcs_writel(field, __vmcs_readl(field) | mask);
Anthony Liguori2ab455c2007-04-27 09:29:49 +03001735}
1736
Paolo Bonzini8391ce42016-07-07 14:58:33 +02001737static inline void vm_entry_controls_reset_shadow(struct vcpu_vmx *vmx)
1738{
1739 vmx->vm_entry_controls_shadow = vmcs_read32(VM_ENTRY_CONTROLS);
1740}
1741
Gleb Natapov2961e8762013-11-25 15:37:13 +02001742static inline void vm_entry_controls_init(struct vcpu_vmx *vmx, u32 val)
1743{
1744 vmcs_write32(VM_ENTRY_CONTROLS, val);
1745 vmx->vm_entry_controls_shadow = val;
1746}
1747
1748static inline void vm_entry_controls_set(struct vcpu_vmx *vmx, u32 val)
1749{
1750 if (vmx->vm_entry_controls_shadow != val)
1751 vm_entry_controls_init(vmx, val);
1752}
1753
1754static inline u32 vm_entry_controls_get(struct vcpu_vmx *vmx)
1755{
1756 return vmx->vm_entry_controls_shadow;
1757}
1758
1759
1760static inline void vm_entry_controls_setbit(struct vcpu_vmx *vmx, u32 val)
1761{
1762 vm_entry_controls_set(vmx, vm_entry_controls_get(vmx) | val);
1763}
1764
1765static inline void vm_entry_controls_clearbit(struct vcpu_vmx *vmx, u32 val)
1766{
1767 vm_entry_controls_set(vmx, vm_entry_controls_get(vmx) & ~val);
1768}
1769
Paolo Bonzini8391ce42016-07-07 14:58:33 +02001770static inline void vm_exit_controls_reset_shadow(struct vcpu_vmx *vmx)
1771{
1772 vmx->vm_exit_controls_shadow = vmcs_read32(VM_EXIT_CONTROLS);
1773}
1774
Gleb Natapov2961e8762013-11-25 15:37:13 +02001775static inline void vm_exit_controls_init(struct vcpu_vmx *vmx, u32 val)
1776{
1777 vmcs_write32(VM_EXIT_CONTROLS, val);
1778 vmx->vm_exit_controls_shadow = val;
1779}
1780
1781static inline void vm_exit_controls_set(struct vcpu_vmx *vmx, u32 val)
1782{
1783 if (vmx->vm_exit_controls_shadow != val)
1784 vm_exit_controls_init(vmx, val);
1785}
1786
1787static inline u32 vm_exit_controls_get(struct vcpu_vmx *vmx)
1788{
1789 return vmx->vm_exit_controls_shadow;
1790}
1791
1792
1793static inline void vm_exit_controls_setbit(struct vcpu_vmx *vmx, u32 val)
1794{
1795 vm_exit_controls_set(vmx, vm_exit_controls_get(vmx) | val);
1796}
1797
1798static inline void vm_exit_controls_clearbit(struct vcpu_vmx *vmx, u32 val)
1799{
1800 vm_exit_controls_set(vmx, vm_exit_controls_get(vmx) & ~val);
1801}
1802
Avi Kivity2fb92db2011-04-27 19:42:18 +03001803static void vmx_segment_cache_clear(struct vcpu_vmx *vmx)
1804{
1805 vmx->segment_cache.bitmask = 0;
1806}
1807
1808static bool vmx_segment_cache_test_set(struct vcpu_vmx *vmx, unsigned seg,
1809 unsigned field)
1810{
1811 bool ret;
1812 u32 mask = 1 << (seg * SEG_FIELD_NR + field);
1813
1814 if (!(vmx->vcpu.arch.regs_avail & (1 << VCPU_EXREG_SEGMENTS))) {
1815 vmx->vcpu.arch.regs_avail |= (1 << VCPU_EXREG_SEGMENTS);
1816 vmx->segment_cache.bitmask = 0;
1817 }
1818 ret = vmx->segment_cache.bitmask & mask;
1819 vmx->segment_cache.bitmask |= mask;
1820 return ret;
1821}
1822
1823static u16 vmx_read_guest_seg_selector(struct vcpu_vmx *vmx, unsigned seg)
1824{
1825 u16 *p = &vmx->segment_cache.seg[seg].selector;
1826
1827 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_SEL))
1828 *p = vmcs_read16(kvm_vmx_segment_fields[seg].selector);
1829 return *p;
1830}
1831
1832static ulong vmx_read_guest_seg_base(struct vcpu_vmx *vmx, unsigned seg)
1833{
1834 ulong *p = &vmx->segment_cache.seg[seg].base;
1835
1836 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_BASE))
1837 *p = vmcs_readl(kvm_vmx_segment_fields[seg].base);
1838 return *p;
1839}
1840
1841static u32 vmx_read_guest_seg_limit(struct vcpu_vmx *vmx, unsigned seg)
1842{
1843 u32 *p = &vmx->segment_cache.seg[seg].limit;
1844
1845 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_LIMIT))
1846 *p = vmcs_read32(kvm_vmx_segment_fields[seg].limit);
1847 return *p;
1848}
1849
1850static u32 vmx_read_guest_seg_ar(struct vcpu_vmx *vmx, unsigned seg)
1851{
1852 u32 *p = &vmx->segment_cache.seg[seg].ar;
1853
1854 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_AR))
1855 *p = vmcs_read32(kvm_vmx_segment_fields[seg].ar_bytes);
1856 return *p;
1857}
1858
Avi Kivityabd3f2d2007-05-02 17:57:40 +03001859static void update_exception_bitmap(struct kvm_vcpu *vcpu)
1860{
1861 u32 eb;
1862
Jan Kiszkafd7373c2010-01-20 18:20:20 +01001863 eb = (1u << PF_VECTOR) | (1u << UD_VECTOR) | (1u << MC_VECTOR) |
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -08001864 (1u << DB_VECTOR) | (1u << AC_VECTOR);
Jan Kiszkafd7373c2010-01-20 18:20:20 +01001865 if ((vcpu->guest_debug &
1866 (KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_SW_BP)) ==
1867 (KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_SW_BP))
1868 eb |= 1u << BP_VECTOR;
Avi Kivity7ffd92c2009-06-09 14:10:45 +03001869 if (to_vmx(vcpu)->rmode.vm86_active)
Avi Kivityabd3f2d2007-05-02 17:57:40 +03001870 eb = ~0;
Avi Kivity089d0342009-03-23 18:26:32 +02001871 if (enable_ept)
Sheng Yang14394422008-04-28 12:24:45 +08001872 eb &= ~(1u << PF_VECTOR); /* bypass_guest_pf = 0 */
Nadav Har'El36cf24e2011-05-25 23:15:08 +03001873
1874 /* When we are running a nested L2 guest and L1 specified for it a
1875 * certain exception bitmap, we must trap the same exceptions and pass
1876 * them to L1. When running L2, we will only handle the exceptions
1877 * specified above if L1 did not want them.
1878 */
1879 if (is_guest_mode(vcpu))
1880 eb |= get_vmcs12(vcpu)->exception_bitmap;
1881
Avi Kivityabd3f2d2007-05-02 17:57:40 +03001882 vmcs_write32(EXCEPTION_BITMAP, eb);
1883}
1884
Gleb Natapov2961e8762013-11-25 15:37:13 +02001885static void clear_atomic_switch_msr_special(struct vcpu_vmx *vmx,
1886 unsigned long entry, unsigned long exit)
Gleb Natapov8bf00a52011-10-05 14:01:22 +02001887{
Gleb Natapov2961e8762013-11-25 15:37:13 +02001888 vm_entry_controls_clearbit(vmx, entry);
1889 vm_exit_controls_clearbit(vmx, exit);
Gleb Natapov8bf00a52011-10-05 14:01:22 +02001890}
1891
Avi Kivity61d2ef22010-04-28 16:40:38 +03001892static void clear_atomic_switch_msr(struct vcpu_vmx *vmx, unsigned msr)
1893{
1894 unsigned i;
1895 struct msr_autoload *m = &vmx->msr_autoload;
1896
Gleb Natapov8bf00a52011-10-05 14:01:22 +02001897 switch (msr) {
1898 case MSR_EFER:
1899 if (cpu_has_load_ia32_efer) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02001900 clear_atomic_switch_msr_special(vmx,
1901 VM_ENTRY_LOAD_IA32_EFER,
Gleb Natapov8bf00a52011-10-05 14:01:22 +02001902 VM_EXIT_LOAD_IA32_EFER);
1903 return;
1904 }
1905 break;
1906 case MSR_CORE_PERF_GLOBAL_CTRL:
1907 if (cpu_has_load_perf_global_ctrl) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02001908 clear_atomic_switch_msr_special(vmx,
Gleb Natapov8bf00a52011-10-05 14:01:22 +02001909 VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL,
1910 VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL);
1911 return;
1912 }
1913 break;
Avi Kivity110312c2010-12-21 12:54:20 +02001914 }
1915
Avi Kivity61d2ef22010-04-28 16:40:38 +03001916 for (i = 0; i < m->nr; ++i)
1917 if (m->guest[i].index == msr)
1918 break;
1919
1920 if (i == m->nr)
1921 return;
1922 --m->nr;
1923 m->guest[i] = m->guest[m->nr];
1924 m->host[i] = m->host[m->nr];
1925 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, m->nr);
1926 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, m->nr);
1927}
1928
Gleb Natapov2961e8762013-11-25 15:37:13 +02001929static void add_atomic_switch_msr_special(struct vcpu_vmx *vmx,
1930 unsigned long entry, unsigned long exit,
1931 unsigned long guest_val_vmcs, unsigned long host_val_vmcs,
1932 u64 guest_val, u64 host_val)
Gleb Natapov8bf00a52011-10-05 14:01:22 +02001933{
1934 vmcs_write64(guest_val_vmcs, guest_val);
1935 vmcs_write64(host_val_vmcs, host_val);
Gleb Natapov2961e8762013-11-25 15:37:13 +02001936 vm_entry_controls_setbit(vmx, entry);
1937 vm_exit_controls_setbit(vmx, exit);
Gleb Natapov8bf00a52011-10-05 14:01:22 +02001938}
1939
Avi Kivity61d2ef22010-04-28 16:40:38 +03001940static void add_atomic_switch_msr(struct vcpu_vmx *vmx, unsigned msr,
1941 u64 guest_val, u64 host_val)
1942{
1943 unsigned i;
1944 struct msr_autoload *m = &vmx->msr_autoload;
1945
Gleb Natapov8bf00a52011-10-05 14:01:22 +02001946 switch (msr) {
1947 case MSR_EFER:
1948 if (cpu_has_load_ia32_efer) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02001949 add_atomic_switch_msr_special(vmx,
1950 VM_ENTRY_LOAD_IA32_EFER,
Gleb Natapov8bf00a52011-10-05 14:01:22 +02001951 VM_EXIT_LOAD_IA32_EFER,
1952 GUEST_IA32_EFER,
1953 HOST_IA32_EFER,
1954 guest_val, host_val);
1955 return;
1956 }
1957 break;
1958 case MSR_CORE_PERF_GLOBAL_CTRL:
1959 if (cpu_has_load_perf_global_ctrl) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02001960 add_atomic_switch_msr_special(vmx,
Gleb Natapov8bf00a52011-10-05 14:01:22 +02001961 VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL,
1962 VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL,
1963 GUEST_IA32_PERF_GLOBAL_CTRL,
1964 HOST_IA32_PERF_GLOBAL_CTRL,
1965 guest_val, host_val);
1966 return;
1967 }
1968 break;
Radim Krčmář7099e2e2016-03-04 15:08:42 +01001969 case MSR_IA32_PEBS_ENABLE:
1970 /* PEBS needs a quiescent period after being disabled (to write
1971 * a record). Disabling PEBS through VMX MSR swapping doesn't
1972 * provide that period, so a CPU could write host's record into
1973 * guest's memory.
1974 */
1975 wrmsrl(MSR_IA32_PEBS_ENABLE, 0);
Avi Kivity110312c2010-12-21 12:54:20 +02001976 }
1977
Avi Kivity61d2ef22010-04-28 16:40:38 +03001978 for (i = 0; i < m->nr; ++i)
1979 if (m->guest[i].index == msr)
1980 break;
1981
Gleb Natapove7fc6f93b2011-10-05 14:01:24 +02001982 if (i == NR_AUTOLOAD_MSRS) {
Michael S. Tsirkin60266202013-10-31 00:34:56 +02001983 printk_once(KERN_WARNING "Not enough msr switch entries. "
Gleb Natapove7fc6f93b2011-10-05 14:01:24 +02001984 "Can't add msr %x\n", msr);
1985 return;
1986 } else if (i == m->nr) {
Avi Kivity61d2ef22010-04-28 16:40:38 +03001987 ++m->nr;
1988 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, m->nr);
1989 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, m->nr);
1990 }
1991
1992 m->guest[i].index = msr;
1993 m->guest[i].value = guest_val;
1994 m->host[i].index = msr;
1995 m->host[i].value = host_val;
1996}
1997
Avi Kivity92c0d902009-10-29 11:00:16 +02001998static bool update_transition_efer(struct vcpu_vmx *vmx, int efer_offset)
Eddie Dong2cc51562007-05-21 07:28:09 +03001999{
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002000 u64 guest_efer = vmx->vcpu.arch.efer;
2001 u64 ignore_bits = 0;
Eddie Dong2cc51562007-05-21 07:28:09 +03002002
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002003 if (!enable_ept) {
2004 /*
2005 * NX is needed to handle CR0.WP=1, CR4.SMEP=1. Testing
2006 * host CPUID is more efficient than testing guest CPUID
2007 * or CR4. Host SMEP is anyway a requirement for guest SMEP.
2008 */
2009 if (boot_cpu_has(X86_FEATURE_SMEP))
2010 guest_efer |= EFER_NX;
2011 else if (!(guest_efer & EFER_NX))
2012 ignore_bits |= EFER_NX;
2013 }
Roel Kluin3a34a882009-08-04 02:08:45 -07002014
Avi Kivity51c6cf62007-08-29 03:48:05 +03002015 /*
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002016 * LMA and LME handled by hardware; SCE meaningless outside long mode.
Avi Kivity51c6cf62007-08-29 03:48:05 +03002017 */
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002018 ignore_bits |= EFER_SCE;
Avi Kivity51c6cf62007-08-29 03:48:05 +03002019#ifdef CONFIG_X86_64
2020 ignore_bits |= EFER_LMA | EFER_LME;
2021 /* SCE is meaningful only in long mode on Intel */
2022 if (guest_efer & EFER_LMA)
2023 ignore_bits &= ~(u64)EFER_SCE;
2024#endif
Avi Kivity84ad33e2010-04-28 16:42:29 +03002025
2026 clear_atomic_switch_msr(vmx, MSR_EFER);
Andy Lutomirskif6577a5f2014-11-07 18:25:18 -08002027
2028 /*
2029 * On EPT, we can't emulate NX, so we must switch EFER atomically.
2030 * On CPUs that support "load IA32_EFER", always switch EFER
2031 * atomically, since it's faster than switching it manually.
2032 */
2033 if (cpu_has_load_ia32_efer ||
2034 (enable_ept && ((vmx->vcpu.arch.efer ^ host_efer) & EFER_NX))) {
Avi Kivity84ad33e2010-04-28 16:42:29 +03002035 if (!(guest_efer & EFER_LMA))
2036 guest_efer &= ~EFER_LME;
Andy Lutomirski54b98bf2014-11-10 11:19:15 -08002037 if (guest_efer != host_efer)
2038 add_atomic_switch_msr(vmx, MSR_EFER,
2039 guest_efer, host_efer);
Avi Kivity84ad33e2010-04-28 16:42:29 +03002040 return false;
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002041 } else {
2042 guest_efer &= ~ignore_bits;
2043 guest_efer |= host_efer & ignore_bits;
Avi Kivity84ad33e2010-04-28 16:42:29 +03002044
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002045 vmx->guest_msrs[efer_offset].data = guest_efer;
2046 vmx->guest_msrs[efer_offset].mask = ~ignore_bits;
2047
2048 return true;
2049 }
Avi Kivity51c6cf62007-08-29 03:48:05 +03002050}
2051
Andy Lutomirskie28baea2017-02-20 08:56:11 -08002052#ifdef CONFIG_X86_32
2053/*
2054 * On 32-bit kernels, VM exits still load the FS and GS bases from the
2055 * VMCS rather than the segment table. KVM uses this helper to figure
2056 * out the current bases to poke them into the VMCS before entry.
2057 */
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002058static unsigned long segment_base(u16 selector)
2059{
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08002060 struct desc_struct *table;
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002061 unsigned long v;
2062
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08002063 if (!(selector & ~SEGMENT_RPL_MASK))
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002064 return 0;
2065
Thomas Garnier45fc8752017-03-14 10:05:08 -07002066 table = get_current_gdt_ro();
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002067
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08002068 if ((selector & SEGMENT_TI_MASK) == SEGMENT_LDT) {
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002069 u16 ldt_selector = kvm_read_ldt();
2070
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08002071 if (!(ldt_selector & ~SEGMENT_RPL_MASK))
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002072 return 0;
2073
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08002074 table = (struct desc_struct *)segment_base(ldt_selector);
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002075 }
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08002076 v = get_desc_base(&table[selector >> 3]);
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002077 return v;
2078}
Andy Lutomirskie28baea2017-02-20 08:56:11 -08002079#endif
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002080
Avi Kivity04d2cc72007-09-10 18:10:54 +03002081static void vmx_save_host_state(struct kvm_vcpu *vcpu)
Avi Kivity33ed6322007-05-02 16:54:03 +03002082{
Avi Kivity04d2cc72007-09-10 18:10:54 +03002083 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity26bb0982009-09-07 11:14:12 +03002084 int i;
Avi Kivity04d2cc72007-09-10 18:10:54 +03002085
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002086 if (vmx->host_state.loaded)
Avi Kivity33ed6322007-05-02 16:54:03 +03002087 return;
2088
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002089 vmx->host_state.loaded = 1;
Avi Kivity33ed6322007-05-02 16:54:03 +03002090 /*
2091 * Set host fs and gs selectors. Unfortunately, 22.2.3 does not
2092 * allow segment selectors with cpl > 0 or ti == 1.
2093 */
Avi Kivityd6e88ae2008-07-10 16:53:33 +03002094 vmx->host_state.ldt_sel = kvm_read_ldt();
Laurent Vivier152d3f22007-08-23 16:33:11 +02002095 vmx->host_state.gs_ldt_reload_needed = vmx->host_state.ldt_sel;
Avi Kivity9581d442010-10-19 16:46:55 +02002096 savesegment(fs, vmx->host_state.fs_sel);
Laurent Vivier152d3f22007-08-23 16:33:11 +02002097 if (!(vmx->host_state.fs_sel & 7)) {
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002098 vmcs_write16(HOST_FS_SELECTOR, vmx->host_state.fs_sel);
Laurent Vivier152d3f22007-08-23 16:33:11 +02002099 vmx->host_state.fs_reload_needed = 0;
2100 } else {
Avi Kivity33ed6322007-05-02 16:54:03 +03002101 vmcs_write16(HOST_FS_SELECTOR, 0);
Laurent Vivier152d3f22007-08-23 16:33:11 +02002102 vmx->host_state.fs_reload_needed = 1;
Avi Kivity33ed6322007-05-02 16:54:03 +03002103 }
Avi Kivity9581d442010-10-19 16:46:55 +02002104 savesegment(gs, vmx->host_state.gs_sel);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002105 if (!(vmx->host_state.gs_sel & 7))
2106 vmcs_write16(HOST_GS_SELECTOR, vmx->host_state.gs_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +03002107 else {
2108 vmcs_write16(HOST_GS_SELECTOR, 0);
Laurent Vivier152d3f22007-08-23 16:33:11 +02002109 vmx->host_state.gs_ldt_reload_needed = 1;
Avi Kivity33ed6322007-05-02 16:54:03 +03002110 }
2111
2112#ifdef CONFIG_X86_64
Avi Kivityb2da15a2012-05-13 19:53:24 +03002113 savesegment(ds, vmx->host_state.ds_sel);
2114 savesegment(es, vmx->host_state.es_sel);
2115#endif
2116
2117#ifdef CONFIG_X86_64
Avi Kivity33ed6322007-05-02 16:54:03 +03002118 vmcs_writel(HOST_FS_BASE, read_msr(MSR_FS_BASE));
2119 vmcs_writel(HOST_GS_BASE, read_msr(MSR_GS_BASE));
2120#else
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002121 vmcs_writel(HOST_FS_BASE, segment_base(vmx->host_state.fs_sel));
2122 vmcs_writel(HOST_GS_BASE, segment_base(vmx->host_state.gs_sel));
Avi Kivity33ed6322007-05-02 16:54:03 +03002123#endif
Avi Kivity707c0872007-05-02 17:33:43 +03002124
2125#ifdef CONFIG_X86_64
Avi Kivityc8770e72010-11-11 12:37:26 +02002126 rdmsrl(MSR_KERNEL_GS_BASE, vmx->msr_host_kernel_gs_base);
2127 if (is_long_mode(&vmx->vcpu))
Avi Kivity44ea2b12009-09-06 15:55:37 +03002128 wrmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base);
Avi Kivity707c0872007-05-02 17:33:43 +03002129#endif
Liu, Jinsongda8999d2014-02-24 10:55:46 +00002130 if (boot_cpu_has(X86_FEATURE_MPX))
2131 rdmsrl(MSR_IA32_BNDCFGS, vmx->host_state.msr_host_bndcfgs);
Avi Kivity26bb0982009-09-07 11:14:12 +03002132 for (i = 0; i < vmx->save_nmsrs; ++i)
2133 kvm_set_shared_msr(vmx->guest_msrs[i].index,
Avi Kivityd5696722009-12-02 12:28:47 +02002134 vmx->guest_msrs[i].data,
2135 vmx->guest_msrs[i].mask);
Avi Kivity33ed6322007-05-02 16:54:03 +03002136}
2137
Avi Kivitya9b21b62008-06-24 11:48:49 +03002138static void __vmx_load_host_state(struct vcpu_vmx *vmx)
Avi Kivity33ed6322007-05-02 16:54:03 +03002139{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002140 if (!vmx->host_state.loaded)
Avi Kivity33ed6322007-05-02 16:54:03 +03002141 return;
2142
Avi Kivitye1beb1d2007-11-18 13:50:24 +02002143 ++vmx->vcpu.stat.host_state_reload;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002144 vmx->host_state.loaded = 0;
Avi Kivityc8770e72010-11-11 12:37:26 +02002145#ifdef CONFIG_X86_64
2146 if (is_long_mode(&vmx->vcpu))
2147 rdmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base);
2148#endif
Laurent Vivier152d3f22007-08-23 16:33:11 +02002149 if (vmx->host_state.gs_ldt_reload_needed) {
Avi Kivityd6e88ae2008-07-10 16:53:33 +03002150 kvm_load_ldt(vmx->host_state.ldt_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +03002151#ifdef CONFIG_X86_64
Avi Kivity9581d442010-10-19 16:46:55 +02002152 load_gs_index(vmx->host_state.gs_sel);
Avi Kivity9581d442010-10-19 16:46:55 +02002153#else
2154 loadsegment(gs, vmx->host_state.gs_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +03002155#endif
Avi Kivity33ed6322007-05-02 16:54:03 +03002156 }
Avi Kivity0a77fe42010-10-19 18:48:35 +02002157 if (vmx->host_state.fs_reload_needed)
2158 loadsegment(fs, vmx->host_state.fs_sel);
Avi Kivityb2da15a2012-05-13 19:53:24 +03002159#ifdef CONFIG_X86_64
2160 if (unlikely(vmx->host_state.ds_sel | vmx->host_state.es_sel)) {
2161 loadsegment(ds, vmx->host_state.ds_sel);
2162 loadsegment(es, vmx->host_state.es_sel);
2163 }
Avi Kivityb2da15a2012-05-13 19:53:24 +03002164#endif
Andy Lutomirskib7ffc442017-02-20 08:56:14 -08002165 invalidate_tss_limit();
Avi Kivity44ea2b12009-09-06 15:55:37 +03002166#ifdef CONFIG_X86_64
Avi Kivityc8770e72010-11-11 12:37:26 +02002167 wrmsrl(MSR_KERNEL_GS_BASE, vmx->msr_host_kernel_gs_base);
Avi Kivity44ea2b12009-09-06 15:55:37 +03002168#endif
Liu, Jinsongda8999d2014-02-24 10:55:46 +00002169 if (vmx->host_state.msr_host_bndcfgs)
2170 wrmsrl(MSR_IA32_BNDCFGS, vmx->host_state.msr_host_bndcfgs);
Thomas Garnier45fc8752017-03-14 10:05:08 -07002171 load_fixmap_gdt(raw_smp_processor_id());
Avi Kivity33ed6322007-05-02 16:54:03 +03002172}
2173
Avi Kivitya9b21b62008-06-24 11:48:49 +03002174static void vmx_load_host_state(struct vcpu_vmx *vmx)
2175{
2176 preempt_disable();
2177 __vmx_load_host_state(vmx);
2178 preempt_enable();
2179}
2180
Feng Wu28b835d2015-09-18 22:29:54 +08002181static void vmx_vcpu_pi_load(struct kvm_vcpu *vcpu, int cpu)
2182{
2183 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
2184 struct pi_desc old, new;
2185 unsigned int dest;
2186
2187 if (!kvm_arch_has_assigned_device(vcpu->kvm) ||
Yang Zhanga0052192016-06-13 09:56:56 +08002188 !irq_remapping_cap(IRQ_POSTING_CAP) ||
2189 !kvm_vcpu_apicv_active(vcpu))
Feng Wu28b835d2015-09-18 22:29:54 +08002190 return;
2191
2192 do {
2193 old.control = new.control = pi_desc->control;
2194
2195 /*
2196 * If 'nv' field is POSTED_INTR_WAKEUP_VECTOR, there
2197 * are two possible cases:
2198 * 1. After running 'pre_block', context switch
2199 * happened. For this case, 'sn' was set in
2200 * vmx_vcpu_put(), so we need to clear it here.
2201 * 2. After running 'pre_block', we were blocked,
2202 * and woken up by some other guy. For this case,
2203 * we don't need to do anything, 'pi_post_block'
2204 * will do everything for us. However, we cannot
2205 * check whether it is case #1 or case #2 here
2206 * (maybe, not needed), so we also clear sn here,
2207 * I think it is not a big deal.
2208 */
2209 if (pi_desc->nv != POSTED_INTR_WAKEUP_VECTOR) {
2210 if (vcpu->cpu != cpu) {
2211 dest = cpu_physical_id(cpu);
2212
2213 if (x2apic_enabled())
2214 new.ndst = dest;
2215 else
2216 new.ndst = (dest << 8) & 0xFF00;
2217 }
2218
2219 /* set 'NV' to 'notification vector' */
2220 new.nv = POSTED_INTR_VECTOR;
2221 }
2222
2223 /* Allow posting non-urgent interrupts */
2224 new.sn = 0;
2225 } while (cmpxchg(&pi_desc->control, old.control,
2226 new.control) != old.control);
2227}
Xiao Guangrong1be0e612016-03-22 16:51:18 +08002228
Peter Feinerc95ba922016-08-17 09:36:47 -07002229static void decache_tsc_multiplier(struct vcpu_vmx *vmx)
2230{
2231 vmx->current_tsc_ratio = vmx->vcpu.arch.tsc_scaling_ratio;
2232 vmcs_write64(TSC_MULTIPLIER, vmx->current_tsc_ratio);
2233}
2234
Avi Kivity6aa8b732006-12-10 02:21:36 -08002235/*
2236 * Switches to specified vcpu, until a matching vcpu_put(), but assumes
2237 * vcpu mutex is already taken.
2238 */
Avi Kivity15ad7142007-07-11 18:17:21 +03002239static void vmx_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002240{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002241 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jim Mattsonb80c76e2016-07-29 18:56:53 -07002242 bool already_loaded = vmx->loaded_vmcs->cpu == cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002243
Jim Mattsonb80c76e2016-07-29 18:56:53 -07002244 if (!already_loaded) {
David Hildenbrandfe0e80b2017-03-10 12:47:13 +01002245 loaded_vmcs_clear(vmx->loaded_vmcs);
Dongxiao Xu92fe13b2010-05-11 18:29:42 +08002246 local_irq_disable();
Zhang Yanfei8f536b72012-12-06 23:43:34 +08002247 crash_disable_local_vmclear(cpu);
Xiao Guangrong5a560f82012-11-28 20:54:14 +08002248
2249 /*
2250 * Read loaded_vmcs->cpu should be before fetching
2251 * loaded_vmcs->loaded_vmcss_on_cpu_link.
2252 * See the comments in __loaded_vmcs_clear().
2253 */
2254 smp_rmb();
2255
Nadav Har'Eld462b812011-05-24 15:26:10 +03002256 list_add(&vmx->loaded_vmcs->loaded_vmcss_on_cpu_link,
2257 &per_cpu(loaded_vmcss_on_cpu, cpu));
Zhang Yanfei8f536b72012-12-06 23:43:34 +08002258 crash_enable_local_vmclear(cpu);
Dongxiao Xu92fe13b2010-05-11 18:29:42 +08002259 local_irq_enable();
Jim Mattsonb80c76e2016-07-29 18:56:53 -07002260 }
2261
2262 if (per_cpu(current_vmcs, cpu) != vmx->loaded_vmcs->vmcs) {
2263 per_cpu(current_vmcs, cpu) = vmx->loaded_vmcs->vmcs;
2264 vmcs_load(vmx->loaded_vmcs->vmcs);
2265 }
2266
2267 if (!already_loaded) {
Andy Lutomirski59c58ceb2017-03-22 14:32:33 -07002268 void *gdt = get_current_gdt_ro();
Jim Mattsonb80c76e2016-07-29 18:56:53 -07002269 unsigned long sysenter_esp;
2270
2271 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
Dongxiao Xu92fe13b2010-05-11 18:29:42 +08002272
Avi Kivity6aa8b732006-12-10 02:21:36 -08002273 /*
2274 * Linux uses per-cpu TSS and GDT, so set these when switching
Andy Lutomirskie0c23062017-02-20 08:56:10 -08002275 * processors. See 22.2.4.
Avi Kivity6aa8b732006-12-10 02:21:36 -08002276 */
Andy Lutomirskie0c23062017-02-20 08:56:10 -08002277 vmcs_writel(HOST_TR_BASE,
2278 (unsigned long)this_cpu_ptr(&cpu_tss));
Andy Lutomirski59c58ceb2017-03-22 14:32:33 -07002279 vmcs_writel(HOST_GDTR_BASE, (unsigned long)gdt); /* 22.2.4 */
Avi Kivity6aa8b732006-12-10 02:21:36 -08002280
Andy Lutomirskib7ffc442017-02-20 08:56:14 -08002281 /*
2282 * VM exits change the host TR limit to 0x67 after a VM
2283 * exit. This is okay, since 0x67 covers everything except
2284 * the IO bitmap and have have code to handle the IO bitmap
2285 * being lost after a VM exit.
2286 */
2287 BUILD_BUG_ON(IO_BITMAP_OFFSET - 1 != 0x67);
2288
Avi Kivity6aa8b732006-12-10 02:21:36 -08002289 rdmsrl(MSR_IA32_SYSENTER_ESP, sysenter_esp);
2290 vmcs_writel(HOST_IA32_SYSENTER_ESP, sysenter_esp); /* 22.2.3 */
Haozhong Zhangff2c3a12015-10-20 15:39:10 +08002291
Nadav Har'Eld462b812011-05-24 15:26:10 +03002292 vmx->loaded_vmcs->cpu = cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002293 }
Feng Wu28b835d2015-09-18 22:29:54 +08002294
Owen Hofmann2680d6d2016-03-01 13:36:13 -08002295 /* Setup TSC multiplier */
2296 if (kvm_has_tsc_control &&
Peter Feinerc95ba922016-08-17 09:36:47 -07002297 vmx->current_tsc_ratio != vcpu->arch.tsc_scaling_ratio)
2298 decache_tsc_multiplier(vmx);
Owen Hofmann2680d6d2016-03-01 13:36:13 -08002299
Feng Wu28b835d2015-09-18 22:29:54 +08002300 vmx_vcpu_pi_load(vcpu, cpu);
Xiao Guangrong1be0e612016-03-22 16:51:18 +08002301 vmx->host_pkru = read_pkru();
Feng Wu28b835d2015-09-18 22:29:54 +08002302}
2303
2304static void vmx_vcpu_pi_put(struct kvm_vcpu *vcpu)
2305{
2306 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
2307
2308 if (!kvm_arch_has_assigned_device(vcpu->kvm) ||
Yang Zhanga0052192016-06-13 09:56:56 +08002309 !irq_remapping_cap(IRQ_POSTING_CAP) ||
2310 !kvm_vcpu_apicv_active(vcpu))
Feng Wu28b835d2015-09-18 22:29:54 +08002311 return;
2312
2313 /* Set SN when the vCPU is preempted */
2314 if (vcpu->preempted)
2315 pi_set_sn(pi_desc);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002316}
2317
2318static void vmx_vcpu_put(struct kvm_vcpu *vcpu)
2319{
Feng Wu28b835d2015-09-18 22:29:54 +08002320 vmx_vcpu_pi_put(vcpu);
2321
Avi Kivitya9b21b62008-06-24 11:48:49 +03002322 __vmx_load_host_state(to_vmx(vcpu));
Avi Kivity6aa8b732006-12-10 02:21:36 -08002323}
2324
Wanpeng Lif244dee2017-07-20 01:11:54 -07002325static bool emulation_required(struct kvm_vcpu *vcpu)
2326{
2327 return emulate_invalid_guest_state && !guest_state_valid(vcpu);
2328}
2329
Avi Kivityedcafe32009-12-30 18:07:40 +02002330static void vmx_decache_cr0_guest_bits(struct kvm_vcpu *vcpu);
2331
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03002332/*
2333 * Return the cr0 value that a nested guest would read. This is a combination
2334 * of the real cr0 used to run the guest (guest_cr0), and the bits shadowed by
2335 * its hypervisor (cr0_read_shadow).
2336 */
2337static inline unsigned long nested_read_cr0(struct vmcs12 *fields)
2338{
2339 return (fields->guest_cr0 & ~fields->cr0_guest_host_mask) |
2340 (fields->cr0_read_shadow & fields->cr0_guest_host_mask);
2341}
2342static inline unsigned long nested_read_cr4(struct vmcs12 *fields)
2343{
2344 return (fields->guest_cr4 & ~fields->cr4_guest_host_mask) |
2345 (fields->cr4_read_shadow & fields->cr4_guest_host_mask);
2346}
2347
Avi Kivity6aa8b732006-12-10 02:21:36 -08002348static unsigned long vmx_get_rflags(struct kvm_vcpu *vcpu)
2349{
Avi Kivity78ac8b42010-04-08 18:19:35 +03002350 unsigned long rflags, save_rflags;
Avi Kivity345dcaa2009-08-12 15:29:37 +03002351
Avi Kivity6de12732011-03-07 12:51:22 +02002352 if (!test_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail)) {
2353 __set_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail);
2354 rflags = vmcs_readl(GUEST_RFLAGS);
2355 if (to_vmx(vcpu)->rmode.vm86_active) {
2356 rflags &= RMODE_GUEST_OWNED_EFLAGS_BITS;
2357 save_rflags = to_vmx(vcpu)->rmode.save_rflags;
2358 rflags |= save_rflags & ~RMODE_GUEST_OWNED_EFLAGS_BITS;
2359 }
2360 to_vmx(vcpu)->rflags = rflags;
Avi Kivity78ac8b42010-04-08 18:19:35 +03002361 }
Avi Kivity6de12732011-03-07 12:51:22 +02002362 return to_vmx(vcpu)->rflags;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002363}
2364
2365static void vmx_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags)
2366{
Wanpeng Lif244dee2017-07-20 01:11:54 -07002367 unsigned long old_rflags = vmx_get_rflags(vcpu);
2368
Avi Kivity6de12732011-03-07 12:51:22 +02002369 __set_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail);
2370 to_vmx(vcpu)->rflags = rflags;
Avi Kivity78ac8b42010-04-08 18:19:35 +03002371 if (to_vmx(vcpu)->rmode.vm86_active) {
2372 to_vmx(vcpu)->rmode.save_rflags = rflags;
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +01002373 rflags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM;
Avi Kivity78ac8b42010-04-08 18:19:35 +03002374 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002375 vmcs_writel(GUEST_RFLAGS, rflags);
Wanpeng Lif244dee2017-07-20 01:11:54 -07002376
2377 if ((old_rflags ^ to_vmx(vcpu)->rflags) & X86_EFLAGS_VM)
2378 to_vmx(vcpu)->emulation_required = emulation_required(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002379}
2380
Huaitong Hanbe94f6b2016-03-22 16:51:20 +08002381static u32 vmx_get_pkru(struct kvm_vcpu *vcpu)
2382{
2383 return to_vmx(vcpu)->guest_pkru;
2384}
2385
Paolo Bonzini37ccdcb2014-05-20 14:29:47 +02002386static u32 vmx_get_interrupt_shadow(struct kvm_vcpu *vcpu)
Glauber Costa2809f5d2009-05-12 16:21:05 -04002387{
2388 u32 interruptibility = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
2389 int ret = 0;
2390
2391 if (interruptibility & GUEST_INTR_STATE_STI)
Jan Kiszka48005f62010-02-19 19:38:07 +01002392 ret |= KVM_X86_SHADOW_INT_STI;
Glauber Costa2809f5d2009-05-12 16:21:05 -04002393 if (interruptibility & GUEST_INTR_STATE_MOV_SS)
Jan Kiszka48005f62010-02-19 19:38:07 +01002394 ret |= KVM_X86_SHADOW_INT_MOV_SS;
Glauber Costa2809f5d2009-05-12 16:21:05 -04002395
Paolo Bonzini37ccdcb2014-05-20 14:29:47 +02002396 return ret;
Glauber Costa2809f5d2009-05-12 16:21:05 -04002397}
2398
2399static void vmx_set_interrupt_shadow(struct kvm_vcpu *vcpu, int mask)
2400{
2401 u32 interruptibility_old = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
2402 u32 interruptibility = interruptibility_old;
2403
2404 interruptibility &= ~(GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS);
2405
Jan Kiszka48005f62010-02-19 19:38:07 +01002406 if (mask & KVM_X86_SHADOW_INT_MOV_SS)
Glauber Costa2809f5d2009-05-12 16:21:05 -04002407 interruptibility |= GUEST_INTR_STATE_MOV_SS;
Jan Kiszka48005f62010-02-19 19:38:07 +01002408 else if (mask & KVM_X86_SHADOW_INT_STI)
Glauber Costa2809f5d2009-05-12 16:21:05 -04002409 interruptibility |= GUEST_INTR_STATE_STI;
2410
2411 if ((interruptibility != interruptibility_old))
2412 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, interruptibility);
2413}
2414
Avi Kivity6aa8b732006-12-10 02:21:36 -08002415static void skip_emulated_instruction(struct kvm_vcpu *vcpu)
2416{
2417 unsigned long rip;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002418
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002419 rip = kvm_rip_read(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002420 rip += vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002421 kvm_rip_write(vcpu, rip);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002422
Glauber Costa2809f5d2009-05-12 16:21:05 -04002423 /* skipping an emulated instruction also counts */
2424 vmx_set_interrupt_shadow(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002425}
2426
Paolo Bonzinib96fb432017-07-27 12:29:32 +02002427static void nested_vmx_inject_exception_vmexit(struct kvm_vcpu *vcpu,
2428 unsigned long exit_qual)
2429{
2430 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
2431 unsigned int nr = vcpu->arch.exception.nr;
2432 u32 intr_info = nr | INTR_INFO_VALID_MASK;
2433
2434 if (vcpu->arch.exception.has_error_code) {
2435 vmcs12->vm_exit_intr_error_code = vcpu->arch.exception.error_code;
2436 intr_info |= INTR_INFO_DELIVER_CODE_MASK;
2437 }
2438
2439 if (kvm_exception_is_soft(nr))
2440 intr_info |= INTR_TYPE_SOFT_EXCEPTION;
2441 else
2442 intr_info |= INTR_TYPE_HARD_EXCEPTION;
2443
2444 if (!(vmcs12->idt_vectoring_info_field & VECTORING_INFO_VALID_MASK) &&
2445 vmx_get_nmi_mask(vcpu))
2446 intr_info |= INTR_INFO_UNBLOCK_NMI;
2447
2448 nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI, intr_info, exit_qual);
2449}
2450
Nadav Har'El0b6ac342011-05-25 23:13:36 +03002451/*
2452 * KVM wants to inject page-faults which it got to the guest. This function
2453 * checks whether in a nested guest, we need to inject them to L1 or L2.
Nadav Har'El0b6ac342011-05-25 23:13:36 +03002454 */
Wanpeng Liadfe20f2017-07-13 18:30:41 -07002455static int nested_vmx_check_exception(struct kvm_vcpu *vcpu)
Nadav Har'El0b6ac342011-05-25 23:13:36 +03002456{
2457 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Wanpeng Liadfe20f2017-07-13 18:30:41 -07002458 unsigned int nr = vcpu->arch.exception.nr;
Nadav Har'El0b6ac342011-05-25 23:13:36 +03002459
Paolo Bonzinib96fb432017-07-27 12:29:32 +02002460 if (nr == PF_VECTOR) {
2461 if (vcpu->arch.exception.nested_apf) {
2462 nested_vmx_inject_exception_vmexit(vcpu,
2463 vcpu->arch.apf.nested_apf_token);
2464 return 1;
2465 }
2466 /*
2467 * FIXME: we must not write CR2 when L1 intercepts an L2 #PF exception.
2468 * The fix is to add the ancillary datum (CR2 or DR6) to structs
2469 * kvm_queued_exception and kvm_vcpu_events, so that CR2 and DR6
2470 * can be written only when inject_pending_event runs. This should be
2471 * conditional on a new capability---if the capability is disabled,
2472 * kvm_multiple_exception would write the ancillary information to
2473 * CR2 or DR6, for backwards ABI-compatibility.
2474 */
2475 if (nested_vmx_is_page_fault_vmexit(vmcs12,
2476 vcpu->arch.exception.error_code)) {
2477 nested_vmx_inject_exception_vmexit(vcpu, vcpu->arch.cr2);
2478 return 1;
2479 }
2480 } else {
2481 unsigned long exit_qual = 0;
2482 if (nr == DB_VECTOR)
2483 exit_qual = vcpu->arch.dr6;
Nadav Har'El0b6ac342011-05-25 23:13:36 +03002484
Paolo Bonzinib96fb432017-07-27 12:29:32 +02002485 if (vmcs12->exception_bitmap & (1u << nr)) {
2486 nested_vmx_inject_exception_vmexit(vcpu, exit_qual);
2487 return 1;
2488 }
Wanpeng Liadfe20f2017-07-13 18:30:41 -07002489 }
2490
Paolo Bonzinib96fb432017-07-27 12:29:32 +02002491 return 0;
Nadav Har'El0b6ac342011-05-25 23:13:36 +03002492}
2493
Wanpeng Licfcd20e2017-07-13 18:30:39 -07002494static void vmx_queue_exception(struct kvm_vcpu *vcpu)
Avi Kivity298101d2007-11-25 13:41:11 +02002495{
Jan Kiszka77ab6db2008-07-14 12:28:51 +02002496 struct vcpu_vmx *vmx = to_vmx(vcpu);
Wanpeng Licfcd20e2017-07-13 18:30:39 -07002497 unsigned nr = vcpu->arch.exception.nr;
2498 bool has_error_code = vcpu->arch.exception.has_error_code;
2499 bool reinject = vcpu->arch.exception.reinject;
2500 u32 error_code = vcpu->arch.exception.error_code;
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01002501 u32 intr_info = nr | INTR_INFO_VALID_MASK;
Jan Kiszka77ab6db2008-07-14 12:28:51 +02002502
Gleb Natapove011c662013-09-25 12:51:35 +03002503 if (!reinject && is_guest_mode(vcpu) &&
Wanpeng Liadfe20f2017-07-13 18:30:41 -07002504 nested_vmx_check_exception(vcpu))
Nadav Har'El0b6ac342011-05-25 23:13:36 +03002505 return;
2506
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01002507 if (has_error_code) {
Jan Kiszka77ab6db2008-07-14 12:28:51 +02002508 vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE, error_code);
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01002509 intr_info |= INTR_INFO_DELIVER_CODE_MASK;
2510 }
Jan Kiszka77ab6db2008-07-14 12:28:51 +02002511
Avi Kivity7ffd92c2009-06-09 14:10:45 +03002512 if (vmx->rmode.vm86_active) {
Serge E. Hallyn71f98332011-04-13 09:12:54 -05002513 int inc_eip = 0;
2514 if (kvm_exception_is_soft(nr))
2515 inc_eip = vcpu->arch.event_exit_inst_len;
2516 if (kvm_inject_realmode_interrupt(vcpu, nr, inc_eip) != EMULATE_DONE)
Mohammed Gamala92601b2010-09-19 14:34:07 +02002517 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Jan Kiszka77ab6db2008-07-14 12:28:51 +02002518 return;
2519 }
2520
Gleb Natapov66fd3f72009-05-11 13:35:50 +03002521 if (kvm_exception_is_soft(nr)) {
2522 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
2523 vmx->vcpu.arch.event_exit_inst_len);
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01002524 intr_info |= INTR_TYPE_SOFT_EXCEPTION;
2525 } else
2526 intr_info |= INTR_TYPE_HARD_EXCEPTION;
2527
2528 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr_info);
Avi Kivity298101d2007-11-25 13:41:11 +02002529}
2530
Sheng Yang4e47c7a2009-12-18 16:48:47 +08002531static bool vmx_rdtscp_supported(void)
2532{
2533 return cpu_has_vmx_rdtscp();
2534}
2535
Mao, Junjiead756a12012-07-02 01:18:48 +00002536static bool vmx_invpcid_supported(void)
2537{
2538 return cpu_has_vmx_invpcid() && enable_ept;
2539}
2540
Avi Kivity6aa8b732006-12-10 02:21:36 -08002541/*
Eddie Donga75beee2007-05-17 18:55:15 +03002542 * Swap MSR entry in host/guest MSR entry array.
2543 */
Rusty Russell8b9cf982007-07-30 16:31:43 +10002544static void move_msr_up(struct vcpu_vmx *vmx, int from, int to)
Eddie Donga75beee2007-05-17 18:55:15 +03002545{
Avi Kivity26bb0982009-09-07 11:14:12 +03002546 struct shared_msr_entry tmp;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002547
2548 tmp = vmx->guest_msrs[to];
2549 vmx->guest_msrs[to] = vmx->guest_msrs[from];
2550 vmx->guest_msrs[from] = tmp;
Eddie Donga75beee2007-05-17 18:55:15 +03002551}
2552
Yang Zhang8d146952013-01-25 10:18:50 +08002553static void vmx_set_msr_bitmap(struct kvm_vcpu *vcpu)
2554{
2555 unsigned long *msr_bitmap;
2556
Wincy Van670125b2015-03-04 14:31:56 +08002557 if (is_guest_mode(vcpu))
Radim Krčmářd048c092016-08-08 20:16:22 +02002558 msr_bitmap = to_vmx(vcpu)->nested.msr_bitmap;
Roman Kagan3ce424e2016-05-18 17:48:20 +03002559 else if (cpu_has_secondary_exec_ctrls() &&
2560 (vmcs_read32(SECONDARY_VM_EXEC_CONTROL) &
2561 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE)) {
Wanpeng Lif6e90f92016-09-22 07:43:25 +08002562 if (enable_apicv && kvm_vcpu_apicv_active(vcpu)) {
2563 if (is_long_mode(vcpu))
Wanpeng Lic63e4562016-09-23 19:17:16 +08002564 msr_bitmap = vmx_msr_bitmap_longmode_x2apic_apicv;
2565 else
2566 msr_bitmap = vmx_msr_bitmap_legacy_x2apic_apicv;
2567 } else {
2568 if (is_long_mode(vcpu))
Wanpeng Lif6e90f92016-09-22 07:43:25 +08002569 msr_bitmap = vmx_msr_bitmap_longmode_x2apic;
2570 else
2571 msr_bitmap = vmx_msr_bitmap_legacy_x2apic;
Wanpeng Lif6e90f92016-09-22 07:43:25 +08002572 }
Yang Zhang8d146952013-01-25 10:18:50 +08002573 } else {
2574 if (is_long_mode(vcpu))
2575 msr_bitmap = vmx_msr_bitmap_longmode;
2576 else
2577 msr_bitmap = vmx_msr_bitmap_legacy;
2578 }
2579
2580 vmcs_write64(MSR_BITMAP, __pa(msr_bitmap));
2581}
2582
Eddie Donga75beee2007-05-17 18:55:15 +03002583/*
Avi Kivitye38aea32007-04-19 13:22:48 +03002584 * Set up the vmcs to automatically save and restore system
2585 * msrs. Don't touch the 64-bit msrs if the guest is in legacy
2586 * mode, as fiddling with msrs is very expensive.
2587 */
Rusty Russell8b9cf982007-07-30 16:31:43 +10002588static void setup_msrs(struct vcpu_vmx *vmx)
Avi Kivitye38aea32007-04-19 13:22:48 +03002589{
Avi Kivity26bb0982009-09-07 11:14:12 +03002590 int save_nmsrs, index;
Avi Kivitye38aea32007-04-19 13:22:48 +03002591
Eddie Donga75beee2007-05-17 18:55:15 +03002592 save_nmsrs = 0;
Avi Kivity4d56c8a2007-04-19 14:28:44 +03002593#ifdef CONFIG_X86_64
Rusty Russell8b9cf982007-07-30 16:31:43 +10002594 if (is_long_mode(&vmx->vcpu)) {
Rusty Russell8b9cf982007-07-30 16:31:43 +10002595 index = __find_msr_index(vmx, MSR_SYSCALL_MASK);
Eddie Donga75beee2007-05-17 18:55:15 +03002596 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +10002597 move_msr_up(vmx, index, save_nmsrs++);
2598 index = __find_msr_index(vmx, MSR_LSTAR);
Eddie Donga75beee2007-05-17 18:55:15 +03002599 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +10002600 move_msr_up(vmx, index, save_nmsrs++);
2601 index = __find_msr_index(vmx, MSR_CSTAR);
Eddie Donga75beee2007-05-17 18:55:15 +03002602 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +10002603 move_msr_up(vmx, index, save_nmsrs++);
Sheng Yang4e47c7a2009-12-18 16:48:47 +08002604 index = __find_msr_index(vmx, MSR_TSC_AUX);
Xiao Guangrong1cea0ce2015-09-09 14:05:57 +08002605 if (index >= 0 && guest_cpuid_has_rdtscp(&vmx->vcpu))
Sheng Yang4e47c7a2009-12-18 16:48:47 +08002606 move_msr_up(vmx, index, save_nmsrs++);
Eddie Donga75beee2007-05-17 18:55:15 +03002607 /*
Brian Gerst8c065852010-07-17 09:03:26 -04002608 * MSR_STAR is only needed on long mode guests, and only
Eddie Donga75beee2007-05-17 18:55:15 +03002609 * if efer.sce is enabled.
2610 */
Brian Gerst8c065852010-07-17 09:03:26 -04002611 index = __find_msr_index(vmx, MSR_STAR);
Avi Kivityf6801df2010-01-21 15:31:50 +02002612 if ((index >= 0) && (vmx->vcpu.arch.efer & EFER_SCE))
Rusty Russell8b9cf982007-07-30 16:31:43 +10002613 move_msr_up(vmx, index, save_nmsrs++);
Avi Kivity4d56c8a2007-04-19 14:28:44 +03002614 }
Eddie Donga75beee2007-05-17 18:55:15 +03002615#endif
Avi Kivity92c0d902009-10-29 11:00:16 +02002616 index = __find_msr_index(vmx, MSR_EFER);
2617 if (index >= 0 && update_transition_efer(vmx, index))
Avi Kivity26bb0982009-09-07 11:14:12 +03002618 move_msr_up(vmx, index, save_nmsrs++);
Avi Kivity4d56c8a2007-04-19 14:28:44 +03002619
Avi Kivity26bb0982009-09-07 11:14:12 +03002620 vmx->save_nmsrs = save_nmsrs;
Avi Kivity58972972009-02-24 22:26:47 +02002621
Yang Zhang8d146952013-01-25 10:18:50 +08002622 if (cpu_has_vmx_msr_bitmap())
2623 vmx_set_msr_bitmap(&vmx->vcpu);
Avi Kivitye38aea32007-04-19 13:22:48 +03002624}
2625
2626/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08002627 * reads and returns guest's timestamp counter "register"
Haozhong Zhangbe7b2632015-10-20 15:39:11 +08002628 * guest_tsc = (host_tsc * tsc multiplier) >> 48 + tsc_offset
2629 * -- Intel TSC Scaling for Virtualization White Paper, sec 1.3
Avi Kivity6aa8b732006-12-10 02:21:36 -08002630 */
Haozhong Zhangbe7b2632015-10-20 15:39:11 +08002631static u64 guest_read_tsc(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002632{
2633 u64 host_tsc, tsc_offset;
2634
Andy Lutomirski4ea16362015-06-25 18:44:07 +02002635 host_tsc = rdtsc();
Avi Kivity6aa8b732006-12-10 02:21:36 -08002636 tsc_offset = vmcs_read64(TSC_OFFSET);
Haozhong Zhangbe7b2632015-10-20 15:39:11 +08002637 return kvm_scale_tsc(vcpu, host_tsc) + tsc_offset;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002638}
2639
2640/*
Zachary Amsden99e3e302010-08-19 22:07:17 -10002641 * writes 'offset' into guest's timestamp counter offset register
Avi Kivity6aa8b732006-12-10 02:21:36 -08002642 */
Zachary Amsden99e3e302010-08-19 22:07:17 -10002643static void vmx_write_tsc_offset(struct kvm_vcpu *vcpu, u64 offset)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002644{
Nadav Har'El27fc51b2011-08-02 15:54:52 +03002645 if (is_guest_mode(vcpu)) {
Nadav Har'El79918252011-05-25 23:15:39 +03002646 /*
Nadav Har'El27fc51b2011-08-02 15:54:52 +03002647 * We're here if L1 chose not to trap WRMSR to TSC. According
2648 * to the spec, this should set L1's TSC; The offset that L1
2649 * set for L2 remains unchanged, and still needs to be added
2650 * to the newly set TSC to get L2's TSC.
Nadav Har'El79918252011-05-25 23:15:39 +03002651 */
Nadav Har'El27fc51b2011-08-02 15:54:52 +03002652 struct vmcs12 *vmcs12;
Nadav Har'El27fc51b2011-08-02 15:54:52 +03002653 /* recalculate vmcs02.TSC_OFFSET: */
2654 vmcs12 = get_vmcs12(vcpu);
2655 vmcs_write64(TSC_OFFSET, offset +
2656 (nested_cpu_has(vmcs12, CPU_BASED_USE_TSC_OFFSETING) ?
2657 vmcs12->tsc_offset : 0));
2658 } else {
Yoshihiro YUNOMAE489223e2013-06-12 16:43:44 +09002659 trace_kvm_write_tsc_offset(vcpu->vcpu_id,
2660 vmcs_read64(TSC_OFFSET), offset);
Nadav Har'El27fc51b2011-08-02 15:54:52 +03002661 vmcs_write64(TSC_OFFSET, offset);
2662 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002663}
2664
Nadav Har'El801d3422011-05-25 23:02:23 +03002665static bool guest_cpuid_has_vmx(struct kvm_vcpu *vcpu)
2666{
2667 struct kvm_cpuid_entry2 *best = kvm_find_cpuid_entry(vcpu, 1, 0);
2668 return best && (best->ecx & (1 << (X86_FEATURE_VMX & 31)));
2669}
2670
2671/*
2672 * nested_vmx_allowed() checks whether a guest should be allowed to use VMX
2673 * instructions and MSRs (i.e., nested VMX). Nested VMX is disabled for
2674 * all guests if the "nested" module option is off, and can also be disabled
2675 * for a single guest by disabling its VMX cpuid bit.
2676 */
2677static inline bool nested_vmx_allowed(struct kvm_vcpu *vcpu)
2678{
2679 return nested && guest_cpuid_has_vmx(vcpu);
2680}
2681
Avi Kivity6aa8b732006-12-10 02:21:36 -08002682/*
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002683 * nested_vmx_setup_ctls_msrs() sets up variables containing the values to be
2684 * returned for the various VMX controls MSRs when nested VMX is enabled.
2685 * The same values should also be used to verify that vmcs12 control fields are
2686 * valid during nested entry from L1 to L2.
2687 * Each of these control msrs has a low and high 32-bit half: A low bit is on
2688 * if the corresponding bit in the (32-bit) control field *must* be on, and a
2689 * bit in the high half is on if the corresponding bit in the control field
2690 * may be on. See also vmx_control_verify().
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002691 */
Wincy Vanb9c237b2015-02-03 23:56:30 +08002692static void nested_vmx_setup_ctls_msrs(struct vcpu_vmx *vmx)
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002693{
2694 /*
2695 * Note that as a general rule, the high half of the MSRs (bits in
2696 * the control fields which may be 1) should be initialized by the
2697 * intersection of the underlying hardware's MSR (i.e., features which
2698 * can be supported) and the list of features we want to expose -
2699 * because they are known to be properly supported in our code.
2700 * Also, usually, the low half of the MSRs (bits which must be 1) can
2701 * be set to 0, meaning that L1 may turn off any of these bits. The
2702 * reason is that if one of these bits is necessary, it will appear
2703 * in vmcs01 and prepare_vmcs02, when it bitwise-or's the control
2704 * fields of vmcs01 and vmcs02, will turn these bits off - and
Paolo Bonzini7313c692017-07-27 10:31:25 +02002705 * nested_vmx_exit_reflected() will not pass related exits to L1.
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002706 * These rules have exceptions below.
2707 */
2708
2709 /* pin-based controls */
Jan Kiszkaeabeaac2013-03-13 11:30:50 +01002710 rdmsr(MSR_IA32_VMX_PINBASED_CTLS,
Wincy Vanb9c237b2015-02-03 23:56:30 +08002711 vmx->nested.nested_vmx_pinbased_ctls_low,
2712 vmx->nested.nested_vmx_pinbased_ctls_high);
2713 vmx->nested.nested_vmx_pinbased_ctls_low |=
2714 PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
2715 vmx->nested.nested_vmx_pinbased_ctls_high &=
2716 PIN_BASED_EXT_INTR_MASK |
2717 PIN_BASED_NMI_EXITING |
2718 PIN_BASED_VIRTUAL_NMIS;
2719 vmx->nested.nested_vmx_pinbased_ctls_high |=
2720 PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR |
Jan Kiszka0238ea92013-03-13 11:31:24 +01002721 PIN_BASED_VMX_PREEMPTION_TIMER;
Andrey Smetanind62caab2015-11-10 15:36:33 +03002722 if (kvm_vcpu_apicv_active(&vmx->vcpu))
Wincy Van705699a2015-02-03 23:58:17 +08002723 vmx->nested.nested_vmx_pinbased_ctls_high |=
2724 PIN_BASED_POSTED_INTR;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002725
Jan Kiszka3dbcd8d2014-06-16 13:59:40 +02002726 /* exit controls */
Arthur Chunqi Lic0dfee52013-08-06 18:41:45 +08002727 rdmsr(MSR_IA32_VMX_EXIT_CTLS,
Wincy Vanb9c237b2015-02-03 23:56:30 +08002728 vmx->nested.nested_vmx_exit_ctls_low,
2729 vmx->nested.nested_vmx_exit_ctls_high);
2730 vmx->nested.nested_vmx_exit_ctls_low =
2731 VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR;
Bandan Dase0ba1a62014-04-19 18:17:46 -04002732
Wincy Vanb9c237b2015-02-03 23:56:30 +08002733 vmx->nested.nested_vmx_exit_ctls_high &=
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002734#ifdef CONFIG_X86_64
Arthur Chunqi Lic0dfee52013-08-06 18:41:45 +08002735 VM_EXIT_HOST_ADDR_SPACE_SIZE |
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002736#endif
Jan Kiszkaf41245002014-03-07 20:03:13 +01002737 VM_EXIT_LOAD_IA32_PAT | VM_EXIT_SAVE_IA32_PAT;
Wincy Vanb9c237b2015-02-03 23:56:30 +08002738 vmx->nested.nested_vmx_exit_ctls_high |=
2739 VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR |
Jan Kiszkaf41245002014-03-07 20:03:13 +01002740 VM_EXIT_LOAD_IA32_EFER | VM_EXIT_SAVE_IA32_EFER |
Bandan Dase0ba1a62014-04-19 18:17:46 -04002741 VM_EXIT_SAVE_VMX_PREEMPTION_TIMER | VM_EXIT_ACK_INTR_ON_EXIT;
2742
Paolo Bonzinia87036a2016-03-08 09:52:13 +01002743 if (kvm_mpx_supported())
Wincy Vanb9c237b2015-02-03 23:56:30 +08002744 vmx->nested.nested_vmx_exit_ctls_high |= VM_EXIT_CLEAR_BNDCFGS;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002745
Jan Kiszka2996fca2014-06-16 13:59:43 +02002746 /* We support free control of debug control saving. */
David Matlack0115f9c2016-11-29 18:14:06 -08002747 vmx->nested.nested_vmx_exit_ctls_low &= ~VM_EXIT_SAVE_DEBUG_CONTROLS;
Jan Kiszka2996fca2014-06-16 13:59:43 +02002748
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002749 /* entry controls */
2750 rdmsr(MSR_IA32_VMX_ENTRY_CTLS,
Wincy Vanb9c237b2015-02-03 23:56:30 +08002751 vmx->nested.nested_vmx_entry_ctls_low,
2752 vmx->nested.nested_vmx_entry_ctls_high);
2753 vmx->nested.nested_vmx_entry_ctls_low =
2754 VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR;
2755 vmx->nested.nested_vmx_entry_ctls_high &=
Jan Kiszka57435342013-08-06 10:39:56 +02002756#ifdef CONFIG_X86_64
2757 VM_ENTRY_IA32E_MODE |
2758#endif
2759 VM_ENTRY_LOAD_IA32_PAT;
Wincy Vanb9c237b2015-02-03 23:56:30 +08002760 vmx->nested.nested_vmx_entry_ctls_high |=
2761 (VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR | VM_ENTRY_LOAD_IA32_EFER);
Paolo Bonzinia87036a2016-03-08 09:52:13 +01002762 if (kvm_mpx_supported())
Wincy Vanb9c237b2015-02-03 23:56:30 +08002763 vmx->nested.nested_vmx_entry_ctls_high |= VM_ENTRY_LOAD_BNDCFGS;
Jan Kiszka57435342013-08-06 10:39:56 +02002764
Jan Kiszka2996fca2014-06-16 13:59:43 +02002765 /* We support free control of debug control loading. */
David Matlack0115f9c2016-11-29 18:14:06 -08002766 vmx->nested.nested_vmx_entry_ctls_low &= ~VM_ENTRY_LOAD_DEBUG_CONTROLS;
Jan Kiszka2996fca2014-06-16 13:59:43 +02002767
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002768 /* cpu-based controls */
2769 rdmsr(MSR_IA32_VMX_PROCBASED_CTLS,
Wincy Vanb9c237b2015-02-03 23:56:30 +08002770 vmx->nested.nested_vmx_procbased_ctls_low,
2771 vmx->nested.nested_vmx_procbased_ctls_high);
2772 vmx->nested.nested_vmx_procbased_ctls_low =
2773 CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
2774 vmx->nested.nested_vmx_procbased_ctls_high &=
Jan Kiszkaa294c9b2013-10-23 17:43:09 +01002775 CPU_BASED_VIRTUAL_INTR_PENDING |
2776 CPU_BASED_VIRTUAL_NMI_PENDING | CPU_BASED_USE_TSC_OFFSETING |
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002777 CPU_BASED_HLT_EXITING | CPU_BASED_INVLPG_EXITING |
2778 CPU_BASED_MWAIT_EXITING | CPU_BASED_CR3_LOAD_EXITING |
2779 CPU_BASED_CR3_STORE_EXITING |
2780#ifdef CONFIG_X86_64
2781 CPU_BASED_CR8_LOAD_EXITING | CPU_BASED_CR8_STORE_EXITING |
2782#endif
2783 CPU_BASED_MOV_DR_EXITING | CPU_BASED_UNCOND_IO_EXITING |
Mihai Donțu5f3d45e2015-07-05 20:08:57 +03002784 CPU_BASED_USE_IO_BITMAPS | CPU_BASED_MONITOR_TRAP_FLAG |
2785 CPU_BASED_MONITOR_EXITING | CPU_BASED_RDPMC_EXITING |
2786 CPU_BASED_RDTSC_EXITING | CPU_BASED_PAUSE_EXITING |
2787 CPU_BASED_TPR_SHADOW | CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002788 /*
2789 * We can allow some features even when not supported by the
2790 * hardware. For example, L1 can specify an MSR bitmap - and we
2791 * can use it to avoid exits to L1 - even when L0 runs L2
2792 * without MSR bitmaps.
2793 */
Wincy Vanb9c237b2015-02-03 23:56:30 +08002794 vmx->nested.nested_vmx_procbased_ctls_high |=
2795 CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR |
Jan Kiszka560b7ee2014-06-16 13:59:42 +02002796 CPU_BASED_USE_MSR_BITMAPS;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002797
Jan Kiszka3dcdf3ec2014-06-16 13:59:41 +02002798 /* We support free control of CR3 access interception. */
David Matlack0115f9c2016-11-29 18:14:06 -08002799 vmx->nested.nested_vmx_procbased_ctls_low &=
Jan Kiszka3dcdf3ec2014-06-16 13:59:41 +02002800 ~(CPU_BASED_CR3_LOAD_EXITING | CPU_BASED_CR3_STORE_EXITING);
2801
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002802 /* secondary cpu-based controls */
2803 rdmsr(MSR_IA32_VMX_PROCBASED_CTLS2,
Wincy Vanb9c237b2015-02-03 23:56:30 +08002804 vmx->nested.nested_vmx_secondary_ctls_low,
2805 vmx->nested.nested_vmx_secondary_ctls_high);
2806 vmx->nested.nested_vmx_secondary_ctls_low = 0;
2807 vmx->nested.nested_vmx_secondary_ctls_high &=
Paolo Bonzinia5f46452017-03-30 11:55:32 +02002808 SECONDARY_EXEC_RDRAND | SECONDARY_EXEC_RDSEED |
Jan Kiszkad6851fb2013-02-23 22:34:39 +01002809 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
Jan Kiszkab3a2a902015-03-23 19:27:19 +01002810 SECONDARY_EXEC_RDTSCP |
Paolo Bonzini1b073042016-10-25 16:06:30 +02002811 SECONDARY_EXEC_DESC |
Wincy Vanf2b93282015-02-03 23:56:03 +08002812 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
Wincy Van82f0dd42015-02-03 23:57:18 +08002813 SECONDARY_EXEC_APIC_REGISTER_VIRT |
Wincy Van608406e2015-02-03 23:57:51 +08002814 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY |
Wanpeng Li81dc01f2014-12-04 19:11:07 +08002815 SECONDARY_EXEC_WBINVD_EXITING |
Dan Williamsdfa169b2016-06-02 11:17:24 -07002816 SECONDARY_EXEC_XSAVES;
Jan Kiszkac18911a2013-03-13 16:06:41 +01002817
Nadav Har'Elafa61f7522013-08-07 14:59:22 +02002818 if (enable_ept) {
2819 /* nested EPT: emulate EPT also to L1 */
Wincy Vanb9c237b2015-02-03 23:56:30 +08002820 vmx->nested.nested_vmx_secondary_ctls_high |=
Radim Krčmář0790ec12015-03-17 14:02:32 +01002821 SECONDARY_EXEC_ENABLE_EPT;
Wincy Vanb9c237b2015-02-03 23:56:30 +08002822 vmx->nested.nested_vmx_ept_caps = VMX_EPT_PAGE_WALK_4_BIT |
Paolo Bonzini7db74262017-03-08 10:49:19 +01002823 VMX_EPTP_WB_BIT | VMX_EPT_INVEPT_BIT;
Bandan Das02120c42016-07-12 18:18:52 -04002824 if (cpu_has_vmx_ept_execute_only())
2825 vmx->nested.nested_vmx_ept_caps |=
2826 VMX_EPT_EXECUTE_ONLY_BIT;
Wincy Vanb9c237b2015-02-03 23:56:30 +08002827 vmx->nested.nested_vmx_ept_caps &= vmx_capability.ept;
Bandan Das45e11812016-08-02 16:32:36 -04002828 vmx->nested.nested_vmx_ept_caps |= VMX_EPT_EXTENT_GLOBAL_BIT |
Paolo Bonzini7db74262017-03-08 10:49:19 +01002829 VMX_EPT_EXTENT_CONTEXT_BIT | VMX_EPT_2MB_PAGE_BIT |
2830 VMX_EPT_1GB_PAGE_BIT;
Bandan Das03efce62017-05-05 15:25:15 -04002831 if (enable_ept_ad_bits) {
2832 vmx->nested.nested_vmx_secondary_ctls_high |=
2833 SECONDARY_EXEC_ENABLE_PML;
Dan Carpenter7461fbc2017-05-18 10:41:15 +03002834 vmx->nested.nested_vmx_ept_caps |= VMX_EPT_AD_BIT;
Bandan Das03efce62017-05-05 15:25:15 -04002835 }
Nadav Har'Elafa61f7522013-08-07 14:59:22 +02002836 } else
Wincy Vanb9c237b2015-02-03 23:56:30 +08002837 vmx->nested.nested_vmx_ept_caps = 0;
Nadav Har'Elafa61f7522013-08-07 14:59:22 +02002838
Bandan Das27c42a12017-08-03 15:54:42 -04002839 if (cpu_has_vmx_vmfunc()) {
2840 vmx->nested.nested_vmx_secondary_ctls_high |=
2841 SECONDARY_EXEC_ENABLE_VMFUNC;
2842 vmx->nested.nested_vmx_vmfunc_controls = 0;
2843 }
2844
Paolo Bonzinief697a72016-03-18 16:58:38 +01002845 /*
2846 * Old versions of KVM use the single-context version without
2847 * checking for support, so declare that it is supported even
2848 * though it is treated as global context. The alternative is
2849 * not failing the single-context invvpid, and it is worse.
2850 */
Wanpeng Li63cb6d52017-03-20 21:18:53 -07002851 if (enable_vpid) {
2852 vmx->nested.nested_vmx_secondary_ctls_high |=
2853 SECONDARY_EXEC_ENABLE_VPID;
Wanpeng Li089d7b62015-10-13 09:18:37 -07002854 vmx->nested.nested_vmx_vpid_caps = VMX_VPID_INVVPID_BIT |
Jan Dakinevichbcdde302016-10-28 07:00:30 +03002855 VMX_VPID_EXTENT_SUPPORTED_MASK;
Wanpeng Li63cb6d52017-03-20 21:18:53 -07002856 } else
Wanpeng Li089d7b62015-10-13 09:18:37 -07002857 vmx->nested.nested_vmx_vpid_caps = 0;
Wanpeng Li99b83ac2015-10-13 09:12:21 -07002858
Radim Krčmář0790ec12015-03-17 14:02:32 +01002859 if (enable_unrestricted_guest)
2860 vmx->nested.nested_vmx_secondary_ctls_high |=
2861 SECONDARY_EXEC_UNRESTRICTED_GUEST;
2862
Jan Kiszkac18911a2013-03-13 16:06:41 +01002863 /* miscellaneous data */
Wincy Vanb9c237b2015-02-03 23:56:30 +08002864 rdmsr(MSR_IA32_VMX_MISC,
2865 vmx->nested.nested_vmx_misc_low,
2866 vmx->nested.nested_vmx_misc_high);
2867 vmx->nested.nested_vmx_misc_low &= VMX_MISC_SAVE_EFER_LMA;
2868 vmx->nested.nested_vmx_misc_low |=
2869 VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE |
Jan Kiszkaf41245002014-03-07 20:03:13 +01002870 VMX_MISC_ACTIVITY_HLT;
Wincy Vanb9c237b2015-02-03 23:56:30 +08002871 vmx->nested.nested_vmx_misc_high = 0;
David Matlack62cc6b9d2016-11-29 18:14:07 -08002872
2873 /*
2874 * This MSR reports some information about VMX support. We
2875 * should return information about the VMX we emulate for the
2876 * guest, and the VMCS structure we give it - not about the
2877 * VMX support of the underlying hardware.
2878 */
2879 vmx->nested.nested_vmx_basic =
2880 VMCS12_REVISION |
2881 VMX_BASIC_TRUE_CTLS |
2882 ((u64)VMCS12_SIZE << VMX_BASIC_VMCS_SIZE_SHIFT) |
2883 (VMX_BASIC_MEM_TYPE_WB << VMX_BASIC_MEM_TYPE_SHIFT);
2884
2885 if (cpu_has_vmx_basic_inout())
2886 vmx->nested.nested_vmx_basic |= VMX_BASIC_INOUT;
2887
2888 /*
David Matlack8322ebb2016-11-29 18:14:09 -08002889 * These MSRs specify bits which the guest must keep fixed on
David Matlack62cc6b9d2016-11-29 18:14:07 -08002890 * while L1 is in VMXON mode (in L1's root mode, or running an L2).
2891 * We picked the standard core2 setting.
2892 */
2893#define VMXON_CR0_ALWAYSON (X86_CR0_PE | X86_CR0_PG | X86_CR0_NE)
2894#define VMXON_CR4_ALWAYSON X86_CR4_VMXE
2895 vmx->nested.nested_vmx_cr0_fixed0 = VMXON_CR0_ALWAYSON;
David Matlack62cc6b9d2016-11-29 18:14:07 -08002896 vmx->nested.nested_vmx_cr4_fixed0 = VMXON_CR4_ALWAYSON;
David Matlack8322ebb2016-11-29 18:14:09 -08002897
2898 /* These MSRs specify bits which the guest must keep fixed off. */
2899 rdmsrl(MSR_IA32_VMX_CR0_FIXED1, vmx->nested.nested_vmx_cr0_fixed1);
2900 rdmsrl(MSR_IA32_VMX_CR4_FIXED1, vmx->nested.nested_vmx_cr4_fixed1);
David Matlack62cc6b9d2016-11-29 18:14:07 -08002901
2902 /* highest index: VMX_PREEMPTION_TIMER_VALUE */
2903 vmx->nested.nested_vmx_vmcs_enum = 0x2e;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002904}
2905
David Matlack38991522016-11-29 18:14:08 -08002906/*
2907 * if fixed0[i] == 1: val[i] must be 1
2908 * if fixed1[i] == 0: val[i] must be 0
2909 */
2910static inline bool fixed_bits_valid(u64 val, u64 fixed0, u64 fixed1)
2911{
2912 return ((val & fixed1) | fixed0) == val;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002913}
2914
2915static inline bool vmx_control_verify(u32 control, u32 low, u32 high)
2916{
David Matlack38991522016-11-29 18:14:08 -08002917 return fixed_bits_valid(control, low, high);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002918}
2919
2920static inline u64 vmx_control_msr(u32 low, u32 high)
2921{
2922 return low | ((u64)high << 32);
2923}
2924
David Matlack62cc6b9d2016-11-29 18:14:07 -08002925static bool is_bitwise_subset(u64 superset, u64 subset, u64 mask)
2926{
2927 superset &= mask;
2928 subset &= mask;
2929
2930 return (superset | subset) == superset;
2931}
2932
2933static int vmx_restore_vmx_basic(struct vcpu_vmx *vmx, u64 data)
2934{
2935 const u64 feature_and_reserved =
2936 /* feature (except bit 48; see below) */
2937 BIT_ULL(49) | BIT_ULL(54) | BIT_ULL(55) |
2938 /* reserved */
2939 BIT_ULL(31) | GENMASK_ULL(47, 45) | GENMASK_ULL(63, 56);
2940 u64 vmx_basic = vmx->nested.nested_vmx_basic;
2941
2942 if (!is_bitwise_subset(vmx_basic, data, feature_and_reserved))
2943 return -EINVAL;
2944
2945 /*
2946 * KVM does not emulate a version of VMX that constrains physical
2947 * addresses of VMX structures (e.g. VMCS) to 32-bits.
2948 */
2949 if (data & BIT_ULL(48))
2950 return -EINVAL;
2951
2952 if (vmx_basic_vmcs_revision_id(vmx_basic) !=
2953 vmx_basic_vmcs_revision_id(data))
2954 return -EINVAL;
2955
2956 if (vmx_basic_vmcs_size(vmx_basic) > vmx_basic_vmcs_size(data))
2957 return -EINVAL;
2958
2959 vmx->nested.nested_vmx_basic = data;
2960 return 0;
2961}
2962
2963static int
2964vmx_restore_control_msr(struct vcpu_vmx *vmx, u32 msr_index, u64 data)
2965{
2966 u64 supported;
2967 u32 *lowp, *highp;
2968
2969 switch (msr_index) {
2970 case MSR_IA32_VMX_TRUE_PINBASED_CTLS:
2971 lowp = &vmx->nested.nested_vmx_pinbased_ctls_low;
2972 highp = &vmx->nested.nested_vmx_pinbased_ctls_high;
2973 break;
2974 case MSR_IA32_VMX_TRUE_PROCBASED_CTLS:
2975 lowp = &vmx->nested.nested_vmx_procbased_ctls_low;
2976 highp = &vmx->nested.nested_vmx_procbased_ctls_high;
2977 break;
2978 case MSR_IA32_VMX_TRUE_EXIT_CTLS:
2979 lowp = &vmx->nested.nested_vmx_exit_ctls_low;
2980 highp = &vmx->nested.nested_vmx_exit_ctls_high;
2981 break;
2982 case MSR_IA32_VMX_TRUE_ENTRY_CTLS:
2983 lowp = &vmx->nested.nested_vmx_entry_ctls_low;
2984 highp = &vmx->nested.nested_vmx_entry_ctls_high;
2985 break;
2986 case MSR_IA32_VMX_PROCBASED_CTLS2:
2987 lowp = &vmx->nested.nested_vmx_secondary_ctls_low;
2988 highp = &vmx->nested.nested_vmx_secondary_ctls_high;
2989 break;
2990 default:
2991 BUG();
2992 }
2993
2994 supported = vmx_control_msr(*lowp, *highp);
2995
2996 /* Check must-be-1 bits are still 1. */
2997 if (!is_bitwise_subset(data, supported, GENMASK_ULL(31, 0)))
2998 return -EINVAL;
2999
3000 /* Check must-be-0 bits are still 0. */
3001 if (!is_bitwise_subset(supported, data, GENMASK_ULL(63, 32)))
3002 return -EINVAL;
3003
3004 *lowp = data;
3005 *highp = data >> 32;
3006 return 0;
3007}
3008
3009static int vmx_restore_vmx_misc(struct vcpu_vmx *vmx, u64 data)
3010{
3011 const u64 feature_and_reserved_bits =
3012 /* feature */
3013 BIT_ULL(5) | GENMASK_ULL(8, 6) | BIT_ULL(14) | BIT_ULL(15) |
3014 BIT_ULL(28) | BIT_ULL(29) | BIT_ULL(30) |
3015 /* reserved */
3016 GENMASK_ULL(13, 9) | BIT_ULL(31);
3017 u64 vmx_misc;
3018
3019 vmx_misc = vmx_control_msr(vmx->nested.nested_vmx_misc_low,
3020 vmx->nested.nested_vmx_misc_high);
3021
3022 if (!is_bitwise_subset(vmx_misc, data, feature_and_reserved_bits))
3023 return -EINVAL;
3024
3025 if ((vmx->nested.nested_vmx_pinbased_ctls_high &
3026 PIN_BASED_VMX_PREEMPTION_TIMER) &&
3027 vmx_misc_preemption_timer_rate(data) !=
3028 vmx_misc_preemption_timer_rate(vmx_misc))
3029 return -EINVAL;
3030
3031 if (vmx_misc_cr3_count(data) > vmx_misc_cr3_count(vmx_misc))
3032 return -EINVAL;
3033
3034 if (vmx_misc_max_msr(data) > vmx_misc_max_msr(vmx_misc))
3035 return -EINVAL;
3036
3037 if (vmx_misc_mseg_revid(data) != vmx_misc_mseg_revid(vmx_misc))
3038 return -EINVAL;
3039
3040 vmx->nested.nested_vmx_misc_low = data;
3041 vmx->nested.nested_vmx_misc_high = data >> 32;
3042 return 0;
3043}
3044
3045static int vmx_restore_vmx_ept_vpid_cap(struct vcpu_vmx *vmx, u64 data)
3046{
3047 u64 vmx_ept_vpid_cap;
3048
3049 vmx_ept_vpid_cap = vmx_control_msr(vmx->nested.nested_vmx_ept_caps,
3050 vmx->nested.nested_vmx_vpid_caps);
3051
3052 /* Every bit is either reserved or a feature bit. */
3053 if (!is_bitwise_subset(vmx_ept_vpid_cap, data, -1ULL))
3054 return -EINVAL;
3055
3056 vmx->nested.nested_vmx_ept_caps = data;
3057 vmx->nested.nested_vmx_vpid_caps = data >> 32;
3058 return 0;
3059}
3060
3061static int vmx_restore_fixed0_msr(struct vcpu_vmx *vmx, u32 msr_index, u64 data)
3062{
3063 u64 *msr;
3064
3065 switch (msr_index) {
3066 case MSR_IA32_VMX_CR0_FIXED0:
3067 msr = &vmx->nested.nested_vmx_cr0_fixed0;
3068 break;
3069 case MSR_IA32_VMX_CR4_FIXED0:
3070 msr = &vmx->nested.nested_vmx_cr4_fixed0;
3071 break;
3072 default:
3073 BUG();
3074 }
3075
3076 /*
3077 * 1 bits (which indicates bits which "must-be-1" during VMX operation)
3078 * must be 1 in the restored value.
3079 */
3080 if (!is_bitwise_subset(data, *msr, -1ULL))
3081 return -EINVAL;
3082
3083 *msr = data;
3084 return 0;
3085}
3086
3087/*
3088 * Called when userspace is restoring VMX MSRs.
3089 *
3090 * Returns 0 on success, non-0 otherwise.
3091 */
3092static int vmx_set_vmx_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 data)
3093{
3094 struct vcpu_vmx *vmx = to_vmx(vcpu);
3095
3096 switch (msr_index) {
3097 case MSR_IA32_VMX_BASIC:
3098 return vmx_restore_vmx_basic(vmx, data);
3099 case MSR_IA32_VMX_PINBASED_CTLS:
3100 case MSR_IA32_VMX_PROCBASED_CTLS:
3101 case MSR_IA32_VMX_EXIT_CTLS:
3102 case MSR_IA32_VMX_ENTRY_CTLS:
3103 /*
3104 * The "non-true" VMX capability MSRs are generated from the
3105 * "true" MSRs, so we do not support restoring them directly.
3106 *
3107 * If userspace wants to emulate VMX_BASIC[55]=0, userspace
3108 * should restore the "true" MSRs with the must-be-1 bits
3109 * set according to the SDM Vol 3. A.2 "RESERVED CONTROLS AND
3110 * DEFAULT SETTINGS".
3111 */
3112 return -EINVAL;
3113 case MSR_IA32_VMX_TRUE_PINBASED_CTLS:
3114 case MSR_IA32_VMX_TRUE_PROCBASED_CTLS:
3115 case MSR_IA32_VMX_TRUE_EXIT_CTLS:
3116 case MSR_IA32_VMX_TRUE_ENTRY_CTLS:
3117 case MSR_IA32_VMX_PROCBASED_CTLS2:
3118 return vmx_restore_control_msr(vmx, msr_index, data);
3119 case MSR_IA32_VMX_MISC:
3120 return vmx_restore_vmx_misc(vmx, data);
3121 case MSR_IA32_VMX_CR0_FIXED0:
3122 case MSR_IA32_VMX_CR4_FIXED0:
3123 return vmx_restore_fixed0_msr(vmx, msr_index, data);
3124 case MSR_IA32_VMX_CR0_FIXED1:
3125 case MSR_IA32_VMX_CR4_FIXED1:
3126 /*
3127 * These MSRs are generated based on the vCPU's CPUID, so we
3128 * do not support restoring them directly.
3129 */
3130 return -EINVAL;
3131 case MSR_IA32_VMX_EPT_VPID_CAP:
3132 return vmx_restore_vmx_ept_vpid_cap(vmx, data);
3133 case MSR_IA32_VMX_VMCS_ENUM:
3134 vmx->nested.nested_vmx_vmcs_enum = data;
3135 return 0;
3136 default:
3137 /*
3138 * The rest of the VMX capability MSRs do not support restore.
3139 */
3140 return -EINVAL;
3141 }
3142}
3143
Jan Kiszkacae50132014-01-04 18:47:22 +01003144/* Returns 0 on success, non-0 otherwise. */
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003145static int vmx_get_vmx_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 *pdata)
3146{
Wincy Vanb9c237b2015-02-03 23:56:30 +08003147 struct vcpu_vmx *vmx = to_vmx(vcpu);
3148
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003149 switch (msr_index) {
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003150 case MSR_IA32_VMX_BASIC:
David Matlack62cc6b9d2016-11-29 18:14:07 -08003151 *pdata = vmx->nested.nested_vmx_basic;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003152 break;
3153 case MSR_IA32_VMX_TRUE_PINBASED_CTLS:
3154 case MSR_IA32_VMX_PINBASED_CTLS:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003155 *pdata = vmx_control_msr(
3156 vmx->nested.nested_vmx_pinbased_ctls_low,
3157 vmx->nested.nested_vmx_pinbased_ctls_high);
David Matlack0115f9c2016-11-29 18:14:06 -08003158 if (msr_index == MSR_IA32_VMX_PINBASED_CTLS)
3159 *pdata |= PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003160 break;
3161 case MSR_IA32_VMX_TRUE_PROCBASED_CTLS:
3162 case MSR_IA32_VMX_PROCBASED_CTLS:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003163 *pdata = vmx_control_msr(
3164 vmx->nested.nested_vmx_procbased_ctls_low,
3165 vmx->nested.nested_vmx_procbased_ctls_high);
David Matlack0115f9c2016-11-29 18:14:06 -08003166 if (msr_index == MSR_IA32_VMX_PROCBASED_CTLS)
3167 *pdata |= CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003168 break;
3169 case MSR_IA32_VMX_TRUE_EXIT_CTLS:
3170 case MSR_IA32_VMX_EXIT_CTLS:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003171 *pdata = vmx_control_msr(
3172 vmx->nested.nested_vmx_exit_ctls_low,
3173 vmx->nested.nested_vmx_exit_ctls_high);
David Matlack0115f9c2016-11-29 18:14:06 -08003174 if (msr_index == MSR_IA32_VMX_EXIT_CTLS)
3175 *pdata |= VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003176 break;
3177 case MSR_IA32_VMX_TRUE_ENTRY_CTLS:
3178 case MSR_IA32_VMX_ENTRY_CTLS:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003179 *pdata = vmx_control_msr(
3180 vmx->nested.nested_vmx_entry_ctls_low,
3181 vmx->nested.nested_vmx_entry_ctls_high);
David Matlack0115f9c2016-11-29 18:14:06 -08003182 if (msr_index == MSR_IA32_VMX_ENTRY_CTLS)
3183 *pdata |= VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003184 break;
3185 case MSR_IA32_VMX_MISC:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003186 *pdata = vmx_control_msr(
3187 vmx->nested.nested_vmx_misc_low,
3188 vmx->nested.nested_vmx_misc_high);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003189 break;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003190 case MSR_IA32_VMX_CR0_FIXED0:
David Matlack62cc6b9d2016-11-29 18:14:07 -08003191 *pdata = vmx->nested.nested_vmx_cr0_fixed0;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003192 break;
3193 case MSR_IA32_VMX_CR0_FIXED1:
David Matlack62cc6b9d2016-11-29 18:14:07 -08003194 *pdata = vmx->nested.nested_vmx_cr0_fixed1;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003195 break;
3196 case MSR_IA32_VMX_CR4_FIXED0:
David Matlack62cc6b9d2016-11-29 18:14:07 -08003197 *pdata = vmx->nested.nested_vmx_cr4_fixed0;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003198 break;
3199 case MSR_IA32_VMX_CR4_FIXED1:
David Matlack62cc6b9d2016-11-29 18:14:07 -08003200 *pdata = vmx->nested.nested_vmx_cr4_fixed1;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003201 break;
3202 case MSR_IA32_VMX_VMCS_ENUM:
David Matlack62cc6b9d2016-11-29 18:14:07 -08003203 *pdata = vmx->nested.nested_vmx_vmcs_enum;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003204 break;
3205 case MSR_IA32_VMX_PROCBASED_CTLS2:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003206 *pdata = vmx_control_msr(
3207 vmx->nested.nested_vmx_secondary_ctls_low,
3208 vmx->nested.nested_vmx_secondary_ctls_high);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003209 break;
3210 case MSR_IA32_VMX_EPT_VPID_CAP:
Wanpeng Li089d7b62015-10-13 09:18:37 -07003211 *pdata = vmx->nested.nested_vmx_ept_caps |
3212 ((u64)vmx->nested.nested_vmx_vpid_caps << 32);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003213 break;
Bandan Das27c42a12017-08-03 15:54:42 -04003214 case MSR_IA32_VMX_VMFUNC:
3215 *pdata = vmx->nested.nested_vmx_vmfunc_controls;
3216 break;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003217 default:
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003218 return 1;
Nadav Har'Elb3897a42013-07-08 19:12:35 +08003219 }
3220
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003221 return 0;
3222}
3223
Haozhong Zhang37e4c992016-06-22 14:59:55 +08003224static inline bool vmx_feature_control_msr_valid(struct kvm_vcpu *vcpu,
3225 uint64_t val)
3226{
3227 uint64_t valid_bits = to_vmx(vcpu)->msr_ia32_feature_control_valid_bits;
3228
3229 return !(val & ~valid_bits);
3230}
3231
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003232/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08003233 * Reads an msr value (of 'msr_index') into 'pdata'.
3234 * Returns 0 on success, non-0 otherwise.
3235 * Assumes vcpu_load() was already called.
3236 */
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003237static int vmx_get_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003238{
Avi Kivity26bb0982009-09-07 11:14:12 +03003239 struct shared_msr_entry *msr;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003240
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003241 switch (msr_info->index) {
Avi Kivity05b3e0c2006-12-13 00:33:45 -08003242#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08003243 case MSR_FS_BASE:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003244 msr_info->data = vmcs_readl(GUEST_FS_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003245 break;
3246 case MSR_GS_BASE:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003247 msr_info->data = vmcs_readl(GUEST_GS_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003248 break;
Avi Kivity44ea2b12009-09-06 15:55:37 +03003249 case MSR_KERNEL_GS_BASE:
3250 vmx_load_host_state(to_vmx(vcpu));
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003251 msr_info->data = to_vmx(vcpu)->msr_guest_kernel_gs_base;
Avi Kivity44ea2b12009-09-06 15:55:37 +03003252 break;
Avi Kivity26bb0982009-09-07 11:14:12 +03003253#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -08003254 case MSR_EFER:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003255 return kvm_get_msr_common(vcpu, msr_info);
Jaswinder Singh Rajputaf24a4e2009-05-15 18:42:05 +05303256 case MSR_IA32_TSC:
Haozhong Zhangbe7b2632015-10-20 15:39:11 +08003257 msr_info->data = guest_read_tsc(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003258 break;
3259 case MSR_IA32_SYSENTER_CS:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003260 msr_info->data = vmcs_read32(GUEST_SYSENTER_CS);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003261 break;
3262 case MSR_IA32_SYSENTER_EIP:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003263 msr_info->data = vmcs_readl(GUEST_SYSENTER_EIP);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003264 break;
3265 case MSR_IA32_SYSENTER_ESP:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003266 msr_info->data = vmcs_readl(GUEST_SYSENTER_ESP);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003267 break;
Liu, Jinsong0dd376e2014-02-24 10:56:53 +00003268 case MSR_IA32_BNDCFGS:
Haozhong Zhang691bd432017-07-04 10:27:41 +08003269 if (!kvm_mpx_supported() ||
3270 (!msr_info->host_initiated && !guest_cpuid_has_mpx(vcpu)))
Paolo Bonzini93c4adc2014-03-05 23:19:52 +01003271 return 1;
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003272 msr_info->data = vmcs_read64(GUEST_BNDCFGS);
Liu, Jinsong0dd376e2014-02-24 10:56:53 +00003273 break;
Ashok Rajc45dcc72016-06-22 14:59:56 +08003274 case MSR_IA32_MCG_EXT_CTL:
3275 if (!msr_info->host_initiated &&
3276 !(to_vmx(vcpu)->msr_ia32_feature_control &
3277 FEATURE_CONTROL_LMCE))
Jan Kiszkacae50132014-01-04 18:47:22 +01003278 return 1;
Ashok Rajc45dcc72016-06-22 14:59:56 +08003279 msr_info->data = vcpu->arch.mcg_ext_ctl;
3280 break;
Jan Kiszkacae50132014-01-04 18:47:22 +01003281 case MSR_IA32_FEATURE_CONTROL:
Haozhong Zhang3b840802016-06-22 14:59:54 +08003282 msr_info->data = to_vmx(vcpu)->msr_ia32_feature_control;
Jan Kiszkacae50132014-01-04 18:47:22 +01003283 break;
3284 case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC:
3285 if (!nested_vmx_allowed(vcpu))
3286 return 1;
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003287 return vmx_get_vmx_msr(vcpu, msr_info->index, &msr_info->data);
Wanpeng Li20300092014-12-02 19:14:59 +08003288 case MSR_IA32_XSS:
3289 if (!vmx_xsaves_supported())
3290 return 1;
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003291 msr_info->data = vcpu->arch.ia32_xss;
Wanpeng Li20300092014-12-02 19:14:59 +08003292 break;
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003293 case MSR_TSC_AUX:
Haozhong Zhang81b1b9c2015-12-14 23:13:38 +08003294 if (!guest_cpuid_has_rdtscp(vcpu) && !msr_info->host_initiated)
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003295 return 1;
3296 /* Otherwise falls through */
Avi Kivity6aa8b732006-12-10 02:21:36 -08003297 default:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003298 msr = find_msr_entry(to_vmx(vcpu), msr_info->index);
Avi Kivity3bab1f52006-12-29 16:49:48 -08003299 if (msr) {
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003300 msr_info->data = msr->data;
Avi Kivity3bab1f52006-12-29 16:49:48 -08003301 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003302 }
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003303 return kvm_get_msr_common(vcpu, msr_info);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003304 }
3305
Avi Kivity6aa8b732006-12-10 02:21:36 -08003306 return 0;
3307}
3308
Jan Kiszkacae50132014-01-04 18:47:22 +01003309static void vmx_leave_nested(struct kvm_vcpu *vcpu);
3310
Avi Kivity6aa8b732006-12-10 02:21:36 -08003311/*
3312 * Writes msr value into into the appropriate "register".
3313 * Returns 0 on success, non-0 otherwise.
3314 * Assumes vcpu_load() was already called.
3315 */
Will Auld8fe8ab42012-11-29 12:42:12 -08003316static int vmx_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003317{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003318 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity26bb0982009-09-07 11:14:12 +03003319 struct shared_msr_entry *msr;
Eddie Dong2cc51562007-05-21 07:28:09 +03003320 int ret = 0;
Will Auld8fe8ab42012-11-29 12:42:12 -08003321 u32 msr_index = msr_info->index;
3322 u64 data = msr_info->data;
Eddie Dong2cc51562007-05-21 07:28:09 +03003323
Avi Kivity6aa8b732006-12-10 02:21:36 -08003324 switch (msr_index) {
Avi Kivity3bab1f52006-12-29 16:49:48 -08003325 case MSR_EFER:
Will Auld8fe8ab42012-11-29 12:42:12 -08003326 ret = kvm_set_msr_common(vcpu, msr_info);
Eddie Dong2cc51562007-05-21 07:28:09 +03003327 break;
Avi Kivity16175a72009-03-23 22:13:44 +02003328#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08003329 case MSR_FS_BASE:
Avi Kivity2fb92db2011-04-27 19:42:18 +03003330 vmx_segment_cache_clear(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003331 vmcs_writel(GUEST_FS_BASE, data);
3332 break;
3333 case MSR_GS_BASE:
Avi Kivity2fb92db2011-04-27 19:42:18 +03003334 vmx_segment_cache_clear(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003335 vmcs_writel(GUEST_GS_BASE, data);
3336 break;
Avi Kivity44ea2b12009-09-06 15:55:37 +03003337 case MSR_KERNEL_GS_BASE:
3338 vmx_load_host_state(vmx);
3339 vmx->msr_guest_kernel_gs_base = data;
3340 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003341#endif
3342 case MSR_IA32_SYSENTER_CS:
3343 vmcs_write32(GUEST_SYSENTER_CS, data);
3344 break;
3345 case MSR_IA32_SYSENTER_EIP:
Avi Kivityf5b42c32007-03-06 12:05:53 +02003346 vmcs_writel(GUEST_SYSENTER_EIP, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003347 break;
3348 case MSR_IA32_SYSENTER_ESP:
Avi Kivityf5b42c32007-03-06 12:05:53 +02003349 vmcs_writel(GUEST_SYSENTER_ESP, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003350 break;
Liu, Jinsong0dd376e2014-02-24 10:56:53 +00003351 case MSR_IA32_BNDCFGS:
Haozhong Zhang691bd432017-07-04 10:27:41 +08003352 if (!kvm_mpx_supported() ||
3353 (!msr_info->host_initiated && !guest_cpuid_has_mpx(vcpu)))
Paolo Bonzini93c4adc2014-03-05 23:19:52 +01003354 return 1;
Jim Mattson45316622017-05-23 11:52:54 -07003355 if (is_noncanonical_address(data & PAGE_MASK) ||
3356 (data & MSR_IA32_BNDCFGS_RSVD))
Avi Kivity6aa8b732006-12-10 02:21:36 -08003357 return 1;
Sheng Yang468d4722008-10-09 16:01:55 +08003358 vmcs_write64(GUEST_BNDCFGS, data);
3359 break;
3360 case MSR_IA32_TSC:
3361 kvm_write_tsc(vcpu, msr_info);
3362 break;
3363 case MSR_IA32_CR_PAT:
Will Auld8fe8ab42012-11-29 12:42:12 -08003364 if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) {
Nadav Amit45666542014-09-18 22:39:44 +03003365 if (!kvm_mtrr_valid(vcpu, MSR_IA32_CR_PAT, data))
3366 return 1;
Sheng Yang468d4722008-10-09 16:01:55 +08003367 vmcs_write64(GUEST_IA32_PAT, data);
3368 vcpu->arch.pat = data;
3369 break;
3370 }
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003371 ret = kvm_set_msr_common(vcpu, msr_info);
3372 break;
Will Auldba904632012-11-29 12:42:50 -08003373 case MSR_IA32_TSC_ADJUST:
3374 ret = kvm_set_msr_common(vcpu, msr_info);
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003375 break;
Ashok Rajc45dcc72016-06-22 14:59:56 +08003376 case MSR_IA32_MCG_EXT_CTL:
3377 if ((!msr_info->host_initiated &&
3378 !(to_vmx(vcpu)->msr_ia32_feature_control &
3379 FEATURE_CONTROL_LMCE)) ||
3380 (data & ~MCG_EXT_CTL_LMCE_EN))
3381 return 1;
3382 vcpu->arch.mcg_ext_ctl = data;
3383 break;
Jan Kiszkacae50132014-01-04 18:47:22 +01003384 case MSR_IA32_FEATURE_CONTROL:
Haozhong Zhang37e4c992016-06-22 14:59:55 +08003385 if (!vmx_feature_control_msr_valid(vcpu, data) ||
Haozhong Zhang3b840802016-06-22 14:59:54 +08003386 (to_vmx(vcpu)->msr_ia32_feature_control &
Jan Kiszkacae50132014-01-04 18:47:22 +01003387 FEATURE_CONTROL_LOCKED && !msr_info->host_initiated))
3388 return 1;
Haozhong Zhang3b840802016-06-22 14:59:54 +08003389 vmx->msr_ia32_feature_control = data;
Jan Kiszkacae50132014-01-04 18:47:22 +01003390 if (msr_info->host_initiated && data == 0)
3391 vmx_leave_nested(vcpu);
3392 break;
3393 case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC:
David Matlack62cc6b9d2016-11-29 18:14:07 -08003394 if (!msr_info->host_initiated)
3395 return 1; /* they are read-only */
3396 if (!nested_vmx_allowed(vcpu))
3397 return 1;
3398 return vmx_set_vmx_msr(vcpu, msr_index, data);
Wanpeng Li20300092014-12-02 19:14:59 +08003399 case MSR_IA32_XSS:
3400 if (!vmx_xsaves_supported())
3401 return 1;
3402 /*
3403 * The only supported bit as of Skylake is bit 8, but
3404 * it is not supported on KVM.
3405 */
3406 if (data != 0)
3407 return 1;
3408 vcpu->arch.ia32_xss = data;
3409 if (vcpu->arch.ia32_xss != host_xss)
3410 add_atomic_switch_msr(vmx, MSR_IA32_XSS,
3411 vcpu->arch.ia32_xss, host_xss);
3412 else
3413 clear_atomic_switch_msr(vmx, MSR_IA32_XSS);
3414 break;
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003415 case MSR_TSC_AUX:
Haozhong Zhang81b1b9c2015-12-14 23:13:38 +08003416 if (!guest_cpuid_has_rdtscp(vcpu) && !msr_info->host_initiated)
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003417 return 1;
3418 /* Check reserved bit, higher 32 bits should be zero */
3419 if ((data >> 32) != 0)
3420 return 1;
3421 /* Otherwise falls through */
Avi Kivity6aa8b732006-12-10 02:21:36 -08003422 default:
Rusty Russell8b9cf982007-07-30 16:31:43 +10003423 msr = find_msr_entry(vmx, msr_index);
Avi Kivity3bab1f52006-12-29 16:49:48 -08003424 if (msr) {
Andy Honig8b3c3102014-08-27 11:16:44 -07003425 u64 old_msr_data = msr->data;
Avi Kivity3bab1f52006-12-29 16:49:48 -08003426 msr->data = data;
Avi Kivity2225fd52012-04-18 15:03:04 +03003427 if (msr - vmx->guest_msrs < vmx->save_nmsrs) {
3428 preempt_disable();
Andy Honig8b3c3102014-08-27 11:16:44 -07003429 ret = kvm_set_shared_msr(msr->index, msr->data,
3430 msr->mask);
Avi Kivity2225fd52012-04-18 15:03:04 +03003431 preempt_enable();
Andy Honig8b3c3102014-08-27 11:16:44 -07003432 if (ret)
3433 msr->data = old_msr_data;
Avi Kivity2225fd52012-04-18 15:03:04 +03003434 }
Avi Kivity3bab1f52006-12-29 16:49:48 -08003435 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003436 }
Will Auld8fe8ab42012-11-29 12:42:12 -08003437 ret = kvm_set_msr_common(vcpu, msr_info);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003438 }
3439
Eddie Dong2cc51562007-05-21 07:28:09 +03003440 return ret;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003441}
3442
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003443static void vmx_cache_reg(struct kvm_vcpu *vcpu, enum kvm_reg reg)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003444{
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003445 __set_bit(reg, (unsigned long *)&vcpu->arch.regs_avail);
3446 switch (reg) {
3447 case VCPU_REGS_RSP:
3448 vcpu->arch.regs[VCPU_REGS_RSP] = vmcs_readl(GUEST_RSP);
3449 break;
3450 case VCPU_REGS_RIP:
3451 vcpu->arch.regs[VCPU_REGS_RIP] = vmcs_readl(GUEST_RIP);
3452 break;
Avi Kivity6de4f3a2009-05-31 22:58:47 +03003453 case VCPU_EXREG_PDPTR:
3454 if (enable_ept)
3455 ept_save_pdptrs(vcpu);
3456 break;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003457 default:
3458 break;
3459 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08003460}
3461
Avi Kivity6aa8b732006-12-10 02:21:36 -08003462static __init int cpu_has_kvm_support(void)
3463{
Eduardo Habkost6210e372008-11-17 19:03:16 -02003464 return cpu_has_vmx();
Avi Kivity6aa8b732006-12-10 02:21:36 -08003465}
3466
3467static __init int vmx_disabled_by_bios(void)
3468{
3469 u64 msr;
3470
3471 rdmsrl(MSR_IA32_FEATURE_CONTROL, msr);
Shane Wangcafd6652010-04-29 12:09:01 -04003472 if (msr & FEATURE_CONTROL_LOCKED) {
Joseph Cihula23f3e992011-02-08 11:45:56 -08003473 /* launched w/ TXT and VMX disabled */
Shane Wangcafd6652010-04-29 12:09:01 -04003474 if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX)
3475 && tboot_enabled())
3476 return 1;
Joseph Cihula23f3e992011-02-08 11:45:56 -08003477 /* launched w/o TXT and VMX only enabled w/ TXT */
Shane Wangcafd6652010-04-29 12:09:01 -04003478 if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX)
Joseph Cihula23f3e992011-02-08 11:45:56 -08003479 && (msr & FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX)
Shane Wangf9335af2010-11-17 11:40:17 +08003480 && !tboot_enabled()) {
3481 printk(KERN_WARNING "kvm: disable TXT in the BIOS or "
Joseph Cihula23f3e992011-02-08 11:45:56 -08003482 "activate TXT before enabling KVM\n");
Shane Wangcafd6652010-04-29 12:09:01 -04003483 return 1;
Shane Wangf9335af2010-11-17 11:40:17 +08003484 }
Joseph Cihula23f3e992011-02-08 11:45:56 -08003485 /* launched w/o TXT and VMX disabled */
3486 if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX)
3487 && !tboot_enabled())
3488 return 1;
Shane Wangcafd6652010-04-29 12:09:01 -04003489 }
3490
3491 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003492}
3493
Dongxiao Xu7725b892010-05-11 18:29:38 +08003494static void kvm_cpu_vmxon(u64 addr)
3495{
David Hildenbrandfe0e80b2017-03-10 12:47:13 +01003496 cr4_set_bits(X86_CR4_VMXE);
Alexander Shishkin1c5ac212016-03-29 17:43:10 +03003497 intel_pt_handle_vmx(1);
3498
Dongxiao Xu7725b892010-05-11 18:29:38 +08003499 asm volatile (ASM_VMX_VMXON_RAX
3500 : : "a"(&addr), "m"(addr)
3501 : "memory", "cc");
3502}
3503
Radim Krčmář13a34e02014-08-28 15:13:03 +02003504static int hardware_enable(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003505{
3506 int cpu = raw_smp_processor_id();
3507 u64 phys_addr = __pa(per_cpu(vmxarea, cpu));
Shane Wangcafd6652010-04-29 12:09:01 -04003508 u64 old, test_bits;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003509
Andy Lutomirski1e02ce42014-10-24 15:58:08 -07003510 if (cr4_read_shadow() & X86_CR4_VMXE)
Alexander Graf10474ae2009-09-15 11:37:46 +02003511 return -EBUSY;
3512
Nadav Har'Eld462b812011-05-24 15:26:10 +03003513 INIT_LIST_HEAD(&per_cpu(loaded_vmcss_on_cpu, cpu));
Feng Wubf9f6ac2015-09-18 22:29:55 +08003514 INIT_LIST_HEAD(&per_cpu(blocked_vcpu_on_cpu, cpu));
3515 spin_lock_init(&per_cpu(blocked_vcpu_on_cpu_lock, cpu));
Zhang Yanfei8f536b72012-12-06 23:43:34 +08003516
3517 /*
3518 * Now we can enable the vmclear operation in kdump
3519 * since the loaded_vmcss_on_cpu list on this cpu
3520 * has been initialized.
3521 *
3522 * Though the cpu is not in VMX operation now, there
3523 * is no problem to enable the vmclear operation
3524 * for the loaded_vmcss_on_cpu list is empty!
3525 */
3526 crash_enable_local_vmclear(cpu);
3527
Avi Kivity6aa8b732006-12-10 02:21:36 -08003528 rdmsrl(MSR_IA32_FEATURE_CONTROL, old);
Shane Wangcafd6652010-04-29 12:09:01 -04003529
3530 test_bits = FEATURE_CONTROL_LOCKED;
3531 test_bits |= FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
3532 if (tboot_enabled())
3533 test_bits |= FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX;
3534
3535 if ((old & test_bits) != test_bits) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003536 /* enable and lock */
Shane Wangcafd6652010-04-29 12:09:01 -04003537 wrmsrl(MSR_IA32_FEATURE_CONTROL, old | test_bits);
3538 }
David Hildenbrandfe0e80b2017-03-10 12:47:13 +01003539 kvm_cpu_vmxon(phys_addr);
3540 ept_sync_global();
Alexander Graf10474ae2009-09-15 11:37:46 +02003541
3542 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003543}
3544
Nadav Har'Eld462b812011-05-24 15:26:10 +03003545static void vmclear_local_loaded_vmcss(void)
Avi Kivity543e4242008-05-13 16:22:47 +03003546{
3547 int cpu = raw_smp_processor_id();
Nadav Har'Eld462b812011-05-24 15:26:10 +03003548 struct loaded_vmcs *v, *n;
Avi Kivity543e4242008-05-13 16:22:47 +03003549
Nadav Har'Eld462b812011-05-24 15:26:10 +03003550 list_for_each_entry_safe(v, n, &per_cpu(loaded_vmcss_on_cpu, cpu),
3551 loaded_vmcss_on_cpu_link)
3552 __loaded_vmcs_clear(v);
Avi Kivity543e4242008-05-13 16:22:47 +03003553}
3554
Eduardo Habkost710ff4a2008-11-17 19:03:18 -02003555
3556/* Just like cpu_vmxoff(), but with the __kvm_handle_fault_on_reboot()
3557 * tricks.
3558 */
3559static void kvm_cpu_vmxoff(void)
3560{
3561 asm volatile (__ex(ASM_VMX_VMXOFF) : : : "cc");
Alexander Shishkin1c5ac212016-03-29 17:43:10 +03003562
3563 intel_pt_handle_vmx(0);
David Hildenbrandfe0e80b2017-03-10 12:47:13 +01003564 cr4_clear_bits(X86_CR4_VMXE);
Eduardo Habkost710ff4a2008-11-17 19:03:18 -02003565}
3566
Radim Krčmář13a34e02014-08-28 15:13:03 +02003567static void hardware_disable(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003568{
David Hildenbrandfe0e80b2017-03-10 12:47:13 +01003569 vmclear_local_loaded_vmcss();
3570 kvm_cpu_vmxoff();
Avi Kivity6aa8b732006-12-10 02:21:36 -08003571}
3572
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003573static __init int adjust_vmx_controls(u32 ctl_min, u32 ctl_opt,
Mike Dayd77c26f2007-10-08 09:02:08 -04003574 u32 msr, u32 *result)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003575{
3576 u32 vmx_msr_low, vmx_msr_high;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003577 u32 ctl = ctl_min | ctl_opt;
3578
3579 rdmsr(msr, vmx_msr_low, vmx_msr_high);
3580
3581 ctl &= vmx_msr_high; /* bit == 0 in high word ==> must be zero */
3582 ctl |= vmx_msr_low; /* bit == 1 in low word ==> must be one */
3583
3584 /* Ensure minimum (required) set of control bits are supported. */
3585 if (ctl_min & ~ctl)
Yang, Sheng002c7f72007-07-31 14:23:01 +03003586 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003587
3588 *result = ctl;
3589 return 0;
3590}
3591
Avi Kivity110312c2010-12-21 12:54:20 +02003592static __init bool allow_1_setting(u32 msr, u32 ctl)
3593{
3594 u32 vmx_msr_low, vmx_msr_high;
3595
3596 rdmsr(msr, vmx_msr_low, vmx_msr_high);
3597 return vmx_msr_high & ctl;
3598}
3599
Yang, Sheng002c7f72007-07-31 14:23:01 +03003600static __init int setup_vmcs_config(struct vmcs_config *vmcs_conf)
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003601{
3602 u32 vmx_msr_low, vmx_msr_high;
Sheng Yangd56f5462008-04-25 10:13:16 +08003603 u32 min, opt, min2, opt2;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003604 u32 _pin_based_exec_control = 0;
3605 u32 _cpu_based_exec_control = 0;
Sheng Yangf78e0e22007-10-29 09:40:42 +08003606 u32 _cpu_based_2nd_exec_control = 0;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003607 u32 _vmexit_control = 0;
3608 u32 _vmentry_control = 0;
3609
Raghavendra K T10166742012-02-07 23:19:20 +05303610 min = CPU_BASED_HLT_EXITING |
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003611#ifdef CONFIG_X86_64
3612 CPU_BASED_CR8_LOAD_EXITING |
3613 CPU_BASED_CR8_STORE_EXITING |
3614#endif
Sheng Yangd56f5462008-04-25 10:13:16 +08003615 CPU_BASED_CR3_LOAD_EXITING |
3616 CPU_BASED_CR3_STORE_EXITING |
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003617 CPU_BASED_USE_IO_BITMAPS |
3618 CPU_BASED_MOV_DR_EXITING |
Marcelo Tosattia7052892008-09-23 13:18:35 -03003619 CPU_BASED_USE_TSC_OFFSETING |
Avi Kivityfee84b02011-11-10 14:57:25 +02003620 CPU_BASED_INVLPG_EXITING |
3621 CPU_BASED_RDPMC_EXITING;
Anthony Liguori443381a2010-12-06 10:53:38 -06003622
Michael S. Tsirkin668fffa2017-04-21 12:27:17 +02003623 if (!kvm_mwait_in_guest())
3624 min |= CPU_BASED_MWAIT_EXITING |
3625 CPU_BASED_MONITOR_EXITING;
3626
Sheng Yangf78e0e22007-10-29 09:40:42 +08003627 opt = CPU_BASED_TPR_SHADOW |
Sheng Yang25c5f222008-03-28 13:18:56 +08003628 CPU_BASED_USE_MSR_BITMAPS |
Sheng Yangf78e0e22007-10-29 09:40:42 +08003629 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003630 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PROCBASED_CTLS,
3631 &_cpu_based_exec_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03003632 return -EIO;
Yang, Sheng6e5d8652007-09-12 18:03:11 +08003633#ifdef CONFIG_X86_64
3634 if ((_cpu_based_exec_control & CPU_BASED_TPR_SHADOW))
3635 _cpu_based_exec_control &= ~CPU_BASED_CR8_LOAD_EXITING &
3636 ~CPU_BASED_CR8_STORE_EXITING;
3637#endif
Sheng Yangf78e0e22007-10-29 09:40:42 +08003638 if (_cpu_based_exec_control & CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) {
Sheng Yangd56f5462008-04-25 10:13:16 +08003639 min2 = 0;
3640 opt2 = SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
Yang Zhang8d146952013-01-25 10:18:50 +08003641 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
Sheng Yang2384d2b2008-01-17 15:14:33 +08003642 SECONDARY_EXEC_WBINVD_EXITING |
Sheng Yangd56f5462008-04-25 10:13:16 +08003643 SECONDARY_EXEC_ENABLE_VPID |
Nitin A Kamble3a624e22009-06-08 11:34:16 -07003644 SECONDARY_EXEC_ENABLE_EPT |
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08003645 SECONDARY_EXEC_UNRESTRICTED_GUEST |
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003646 SECONDARY_EXEC_PAUSE_LOOP_EXITING |
Mao, Junjiead756a12012-07-02 01:18:48 +00003647 SECONDARY_EXEC_RDTSCP |
Yang Zhang83d4c282013-01-25 10:18:49 +08003648 SECONDARY_EXEC_ENABLE_INVPCID |
Yang Zhangc7c9c562013-01-25 10:18:51 +08003649 SECONDARY_EXEC_APIC_REGISTER_VIRT |
Abel Gordonabc4fc52013-04-18 14:35:25 +03003650 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY |
Wanpeng Li20300092014-12-02 19:14:59 +08003651 SECONDARY_EXEC_SHADOW_VMCS |
Kai Huang843e4332015-01-28 10:54:28 +08003652 SECONDARY_EXEC_XSAVES |
Xiao Guangrong8b3e34e2015-09-09 14:05:51 +08003653 SECONDARY_EXEC_ENABLE_PML |
Bandan Das2a499e42017-08-03 15:54:41 -04003654 SECONDARY_EXEC_TSC_SCALING |
3655 SECONDARY_EXEC_ENABLE_VMFUNC;
Sheng Yangd56f5462008-04-25 10:13:16 +08003656 if (adjust_vmx_controls(min2, opt2,
3657 MSR_IA32_VMX_PROCBASED_CTLS2,
Sheng Yangf78e0e22007-10-29 09:40:42 +08003658 &_cpu_based_2nd_exec_control) < 0)
3659 return -EIO;
3660 }
3661#ifndef CONFIG_X86_64
3662 if (!(_cpu_based_2nd_exec_control &
3663 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES))
3664 _cpu_based_exec_control &= ~CPU_BASED_TPR_SHADOW;
3665#endif
Yang Zhang83d4c282013-01-25 10:18:49 +08003666
3667 if (!(_cpu_based_exec_control & CPU_BASED_TPR_SHADOW))
3668 _cpu_based_2nd_exec_control &= ~(
Yang Zhang8d146952013-01-25 10:18:50 +08003669 SECONDARY_EXEC_APIC_REGISTER_VIRT |
Yang Zhangc7c9c562013-01-25 10:18:51 +08003670 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
3671 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
Yang Zhang83d4c282013-01-25 10:18:49 +08003672
Sheng Yangd56f5462008-04-25 10:13:16 +08003673 if (_cpu_based_2nd_exec_control & SECONDARY_EXEC_ENABLE_EPT) {
Marcelo Tosattia7052892008-09-23 13:18:35 -03003674 /* CR3 accesses and invlpg don't need to cause VM Exits when EPT
3675 enabled */
Gleb Natapov5fff7d22009-08-27 18:41:30 +03003676 _cpu_based_exec_control &= ~(CPU_BASED_CR3_LOAD_EXITING |
3677 CPU_BASED_CR3_STORE_EXITING |
3678 CPU_BASED_INVLPG_EXITING);
Sheng Yangd56f5462008-04-25 10:13:16 +08003679 rdmsr(MSR_IA32_VMX_EPT_VPID_CAP,
3680 vmx_capability.ept, vmx_capability.vpid);
3681 }
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 Bonzini2c828782017-03-27 14:37:28 +02003693 min = PIN_BASED_EXT_INTR_MASK | PIN_BASED_NMI_EXITING |
3694 PIN_BASED_VIRTUAL_NMIS;
3695 opt = PIN_BASED_POSTED_INTR | 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
Sam Ravnborg39959582007-06-01 00:47:13 -07003825static void free_kvm_area(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003826{
3827 int cpu;
3828
Zachary Amsden3230bb42009-09-29 11:38:37 -10003829 for_each_possible_cpu(cpu) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003830 free_vmcs(per_cpu(vmxarea, cpu));
Zachary Amsden3230bb42009-09-29 11:38:37 -10003831 per_cpu(vmxarea, cpu) = NULL;
3832 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08003833}
3834
Jim Mattson85fd5142017-07-07 12:51:41 -07003835enum vmcs_field_type {
3836 VMCS_FIELD_TYPE_U16 = 0,
3837 VMCS_FIELD_TYPE_U64 = 1,
3838 VMCS_FIELD_TYPE_U32 = 2,
3839 VMCS_FIELD_TYPE_NATURAL_WIDTH = 3
3840};
3841
3842static inline int vmcs_field_type(unsigned long field)
3843{
3844 if (0x1 & field) /* the *_HIGH fields are all 32 bit */
3845 return VMCS_FIELD_TYPE_U32;
3846 return (field >> 13) & 0x3 ;
3847}
3848
3849static inline int vmcs_field_readonly(unsigned long field)
3850{
3851 return (((field >> 10) & 0x3) == 1);
3852}
3853
Bandan Dasfe2b2012014-04-21 15:20:14 -04003854static void init_vmcs_shadow_fields(void)
3855{
3856 int i, j;
3857
3858 /* No checks for read only fields yet */
3859
3860 for (i = j = 0; i < max_shadow_read_write_fields; i++) {
3861 switch (shadow_read_write_fields[i]) {
3862 case GUEST_BNDCFGS:
Paolo Bonzinia87036a2016-03-08 09:52:13 +01003863 if (!kvm_mpx_supported())
Bandan Dasfe2b2012014-04-21 15:20:14 -04003864 continue;
3865 break;
3866 default:
3867 break;
3868 }
3869
3870 if (j < i)
3871 shadow_read_write_fields[j] =
3872 shadow_read_write_fields[i];
3873 j++;
3874 }
3875 max_shadow_read_write_fields = j;
3876
3877 /* shadowed fields guest access without vmexit */
3878 for (i = 0; i < max_shadow_read_write_fields; i++) {
Jim Mattson85fd5142017-07-07 12:51:41 -07003879 unsigned long field = shadow_read_write_fields[i];
3880
3881 clear_bit(field, vmx_vmwrite_bitmap);
3882 clear_bit(field, vmx_vmread_bitmap);
3883 if (vmcs_field_type(field) == VMCS_FIELD_TYPE_U64) {
3884 clear_bit(field + 1, vmx_vmwrite_bitmap);
3885 clear_bit(field + 1, vmx_vmread_bitmap);
3886 }
Bandan Dasfe2b2012014-04-21 15:20:14 -04003887 }
Jim Mattson85fd5142017-07-07 12:51:41 -07003888 for (i = 0; i < max_shadow_read_only_fields; i++) {
3889 unsigned long field = shadow_read_only_fields[i];
3890
3891 clear_bit(field, vmx_vmread_bitmap);
3892 if (vmcs_field_type(field) == VMCS_FIELD_TYPE_U64)
3893 clear_bit(field + 1, vmx_vmread_bitmap);
3894 }
Bandan Dasfe2b2012014-04-21 15:20:14 -04003895}
3896
Avi Kivity6aa8b732006-12-10 02:21:36 -08003897static __init int alloc_kvm_area(void)
3898{
3899 int cpu;
3900
Zachary Amsden3230bb42009-09-29 11:38:37 -10003901 for_each_possible_cpu(cpu) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003902 struct vmcs *vmcs;
3903
3904 vmcs = alloc_vmcs_cpu(cpu);
3905 if (!vmcs) {
3906 free_kvm_area();
3907 return -ENOMEM;
3908 }
3909
3910 per_cpu(vmxarea, cpu) = vmcs;
3911 }
3912 return 0;
3913}
3914
Gleb Natapov91b0aa22013-01-21 15:36:47 +02003915static void fix_pmode_seg(struct kvm_vcpu *vcpu, int seg,
Gleb Natapovd99e4152012-12-20 16:57:45 +02003916 struct kvm_segment *save)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003917{
Gleb Natapovd99e4152012-12-20 16:57:45 +02003918 if (!emulate_invalid_guest_state) {
3919 /*
3920 * CS and SS RPL should be equal during guest entry according
3921 * to VMX spec, but in reality it is not always so. Since vcpu
3922 * is in the middle of the transition from real mode to
3923 * protected mode it is safe to assume that RPL 0 is a good
3924 * default value.
3925 */
3926 if (seg == VCPU_SREG_CS || seg == VCPU_SREG_SS)
Nadav Amitb32a9912015-03-29 16:33:04 +03003927 save->selector &= ~SEGMENT_RPL_MASK;
3928 save->dpl = save->selector & SEGMENT_RPL_MASK;
Gleb Natapovd99e4152012-12-20 16:57:45 +02003929 save->s = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003930 }
Gleb Natapovd99e4152012-12-20 16:57:45 +02003931 vmx_set_segment(vcpu, save, seg);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003932}
3933
3934static void enter_pmode(struct kvm_vcpu *vcpu)
3935{
3936 unsigned long flags;
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03003937 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003938
Gleb Natapovd99e4152012-12-20 16:57:45 +02003939 /*
3940 * Update real mode segment cache. It may be not up-to-date if sement
3941 * register was written while vcpu was in a guest mode.
3942 */
3943 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_ES], VCPU_SREG_ES);
3944 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_DS], VCPU_SREG_DS);
3945 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_FS], VCPU_SREG_FS);
3946 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_GS], VCPU_SREG_GS);
3947 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_SS], VCPU_SREG_SS);
3948 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_CS], VCPU_SREG_CS);
3949
Avi Kivity7ffd92c2009-06-09 14:10:45 +03003950 vmx->rmode.vm86_active = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003951
Avi Kivity2fb92db2011-04-27 19:42:18 +03003952 vmx_segment_cache_clear(vmx);
3953
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03003954 vmx_set_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_TR], VCPU_SREG_TR);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003955
3956 flags = vmcs_readl(GUEST_RFLAGS);
Avi Kivity78ac8b42010-04-08 18:19:35 +03003957 flags &= RMODE_GUEST_OWNED_EFLAGS_BITS;
3958 flags |= vmx->rmode.save_rflags & ~RMODE_GUEST_OWNED_EFLAGS_BITS;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003959 vmcs_writel(GUEST_RFLAGS, flags);
3960
Rusty Russell66aee912007-07-17 23:34:16 +10003961 vmcs_writel(GUEST_CR4, (vmcs_readl(GUEST_CR4) & ~X86_CR4_VME) |
3962 (vmcs_readl(CR4_READ_SHADOW) & X86_CR4_VME));
Avi Kivity6aa8b732006-12-10 02:21:36 -08003963
3964 update_exception_bitmap(vcpu);
3965
Gleb Natapov91b0aa22013-01-21 15:36:47 +02003966 fix_pmode_seg(vcpu, VCPU_SREG_CS, &vmx->rmode.segs[VCPU_SREG_CS]);
3967 fix_pmode_seg(vcpu, VCPU_SREG_SS, &vmx->rmode.segs[VCPU_SREG_SS]);
3968 fix_pmode_seg(vcpu, VCPU_SREG_ES, &vmx->rmode.segs[VCPU_SREG_ES]);
3969 fix_pmode_seg(vcpu, VCPU_SREG_DS, &vmx->rmode.segs[VCPU_SREG_DS]);
3970 fix_pmode_seg(vcpu, VCPU_SREG_FS, &vmx->rmode.segs[VCPU_SREG_FS]);
3971 fix_pmode_seg(vcpu, VCPU_SREG_GS, &vmx->rmode.segs[VCPU_SREG_GS]);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003972}
3973
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03003974static void fix_rmode_seg(int seg, struct kvm_segment *save)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003975{
Mathias Krause772e0312012-08-30 01:30:19 +02003976 const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
Gleb Natapovd99e4152012-12-20 16:57:45 +02003977 struct kvm_segment var = *save;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003978
Gleb Natapovd99e4152012-12-20 16:57:45 +02003979 var.dpl = 0x3;
3980 if (seg == VCPU_SREG_CS)
3981 var.type = 0x3;
3982
3983 if (!emulate_invalid_guest_state) {
3984 var.selector = var.base >> 4;
3985 var.base = var.base & 0xffff0;
3986 var.limit = 0xffff;
3987 var.g = 0;
3988 var.db = 0;
3989 var.present = 1;
3990 var.s = 1;
3991 var.l = 0;
3992 var.unusable = 0;
3993 var.type = 0x3;
3994 var.avl = 0;
3995 if (save->base & 0xf)
3996 printk_once(KERN_WARNING "kvm: segment base is not "
3997 "paragraph aligned when entering "
3998 "protected mode (seg=%d)", seg);
3999 }
4000
4001 vmcs_write16(sf->selector, var.selector);
Chao Peng96794e42017-02-21 03:50:01 -05004002 vmcs_writel(sf->base, var.base);
Gleb Natapovd99e4152012-12-20 16:57:45 +02004003 vmcs_write32(sf->limit, var.limit);
4004 vmcs_write32(sf->ar_bytes, vmx_segment_access_rights(&var));
Avi Kivity6aa8b732006-12-10 02:21:36 -08004005}
4006
4007static void enter_rmode(struct kvm_vcpu *vcpu)
4008{
4009 unsigned long flags;
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03004010 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004011
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03004012 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_TR], VCPU_SREG_TR);
4013 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_ES], VCPU_SREG_ES);
4014 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_DS], VCPU_SREG_DS);
4015 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_FS], VCPU_SREG_FS);
4016 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_GS], VCPU_SREG_GS);
Gleb Natapovc6ad11532012-12-12 19:10:51 +02004017 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_SS], VCPU_SREG_SS);
4018 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_CS], VCPU_SREG_CS);
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03004019
Avi Kivity7ffd92c2009-06-09 14:10:45 +03004020 vmx->rmode.vm86_active = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004021
Gleb Natapov776e58e2011-03-13 12:34:27 +02004022 /*
4023 * Very old userspace does not call KVM_SET_TSS_ADDR before entering
Jan Kiszka4918c6c2013-03-15 08:38:56 +01004024 * vcpu. Warn the user that an update is overdue.
Gleb Natapov776e58e2011-03-13 12:34:27 +02004025 */
Jan Kiszka4918c6c2013-03-15 08:38:56 +01004026 if (!vcpu->kvm->arch.tss_addr)
Gleb Natapov776e58e2011-03-13 12:34:27 +02004027 printk_once(KERN_WARNING "kvm: KVM_SET_TSS_ADDR need to be "
4028 "called before entering vcpu\n");
Gleb Natapov776e58e2011-03-13 12:34:27 +02004029
Avi Kivity2fb92db2011-04-27 19:42:18 +03004030 vmx_segment_cache_clear(vmx);
4031
Jan Kiszka4918c6c2013-03-15 08:38:56 +01004032 vmcs_writel(GUEST_TR_BASE, vcpu->kvm->arch.tss_addr);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004033 vmcs_write32(GUEST_TR_LIMIT, RMODE_TSS_SIZE - 1);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004034 vmcs_write32(GUEST_TR_AR_BYTES, 0x008b);
4035
4036 flags = vmcs_readl(GUEST_RFLAGS);
Avi Kivity78ac8b42010-04-08 18:19:35 +03004037 vmx->rmode.save_rflags = flags;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004038
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +01004039 flags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004040
4041 vmcs_writel(GUEST_RFLAGS, flags);
Rusty Russell66aee912007-07-17 23:34:16 +10004042 vmcs_writel(GUEST_CR4, vmcs_readl(GUEST_CR4) | X86_CR4_VME);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004043 update_exception_bitmap(vcpu);
4044
Gleb Natapovd99e4152012-12-20 16:57:45 +02004045 fix_rmode_seg(VCPU_SREG_SS, &vmx->rmode.segs[VCPU_SREG_SS]);
4046 fix_rmode_seg(VCPU_SREG_CS, &vmx->rmode.segs[VCPU_SREG_CS]);
4047 fix_rmode_seg(VCPU_SREG_ES, &vmx->rmode.segs[VCPU_SREG_ES]);
4048 fix_rmode_seg(VCPU_SREG_DS, &vmx->rmode.segs[VCPU_SREG_DS]);
4049 fix_rmode_seg(VCPU_SREG_GS, &vmx->rmode.segs[VCPU_SREG_GS]);
4050 fix_rmode_seg(VCPU_SREG_FS, &vmx->rmode.segs[VCPU_SREG_FS]);
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03004051
Eddie Dong8668a3c2007-10-10 14:26:45 +08004052 kvm_mmu_reset_context(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004053}
4054
Amit Shah401d10d2009-02-20 22:53:37 +05304055static void vmx_set_efer(struct kvm_vcpu *vcpu, u64 efer)
4056{
4057 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity26bb0982009-09-07 11:14:12 +03004058 struct shared_msr_entry *msr = find_msr_entry(vmx, MSR_EFER);
4059
4060 if (!msr)
4061 return;
Amit Shah401d10d2009-02-20 22:53:37 +05304062
Avi Kivity44ea2b12009-09-06 15:55:37 +03004063 /*
4064 * Force kernel_gs_base reloading before EFER changes, as control
4065 * of this msr depends on is_long_mode().
4066 */
4067 vmx_load_host_state(to_vmx(vcpu));
Avi Kivityf6801df2010-01-21 15:31:50 +02004068 vcpu->arch.efer = efer;
Amit Shah401d10d2009-02-20 22:53:37 +05304069 if (efer & EFER_LMA) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02004070 vm_entry_controls_setbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
Amit Shah401d10d2009-02-20 22:53:37 +05304071 msr->data = efer;
4072 } else {
Gleb Natapov2961e8762013-11-25 15:37:13 +02004073 vm_entry_controls_clearbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
Amit Shah401d10d2009-02-20 22:53:37 +05304074
4075 msr->data = efer & ~EFER_LME;
4076 }
4077 setup_msrs(vmx);
4078}
4079
Avi Kivity05b3e0c2006-12-13 00:33:45 -08004080#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08004081
4082static void enter_lmode(struct kvm_vcpu *vcpu)
4083{
4084 u32 guest_tr_ar;
4085
Avi Kivity2fb92db2011-04-27 19:42:18 +03004086 vmx_segment_cache_clear(to_vmx(vcpu));
4087
Avi Kivity6aa8b732006-12-10 02:21:36 -08004088 guest_tr_ar = vmcs_read32(GUEST_TR_AR_BYTES);
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07004089 if ((guest_tr_ar & VMX_AR_TYPE_MASK) != VMX_AR_TYPE_BUSY_64_TSS) {
Jan Kiszkabd801582011-09-12 11:26:22 +02004090 pr_debug_ratelimited("%s: tss fixup for long mode. \n",
4091 __func__);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004092 vmcs_write32(GUEST_TR_AR_BYTES,
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07004093 (guest_tr_ar & ~VMX_AR_TYPE_MASK)
4094 | VMX_AR_TYPE_BUSY_64_TSS);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004095 }
Avi Kivityda38f432010-07-06 11:30:49 +03004096 vmx_set_efer(vcpu, vcpu->arch.efer | EFER_LMA);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004097}
4098
4099static void exit_lmode(struct kvm_vcpu *vcpu)
4100{
Gleb Natapov2961e8762013-11-25 15:37:13 +02004101 vm_entry_controls_clearbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
Avi Kivityda38f432010-07-06 11:30:49 +03004102 vmx_set_efer(vcpu, vcpu->arch.efer & ~EFER_LMA);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004103}
4104
4105#endif
4106
Wanpeng Lidd5f5342015-09-23 18:26:57 +08004107static inline void __vmx_flush_tlb(struct kvm_vcpu *vcpu, int vpid)
Sheng Yang2384d2b2008-01-17 15:14:33 +08004108{
Xiao Guangrongdd180b32010-07-03 16:02:42 +08004109 if (enable_ept) {
4110 if (!VALID_PAGE(vcpu->arch.mmu.root_hpa))
4111 return;
Peter Feiner995f00a2017-06-30 17:26:32 -07004112 ept_sync_context(construct_eptp(vcpu, vcpu->arch.mmu.root_hpa));
Jim Mattsonf0b98c02017-03-15 07:56:11 -07004113 } else {
4114 vpid_sync_context(vpid);
Xiao Guangrongdd180b32010-07-03 16:02:42 +08004115 }
Sheng Yang2384d2b2008-01-17 15:14:33 +08004116}
4117
Wanpeng Lidd5f5342015-09-23 18:26:57 +08004118static void vmx_flush_tlb(struct kvm_vcpu *vcpu)
4119{
4120 __vmx_flush_tlb(vcpu, to_vmx(vcpu)->vpid);
4121}
4122
Jim Mattsonfb6c8192017-03-16 13:53:59 -07004123static void vmx_flush_tlb_ept_only(struct kvm_vcpu *vcpu)
4124{
4125 if (enable_ept)
4126 vmx_flush_tlb(vcpu);
4127}
4128
Avi Kivitye8467fd2009-12-29 18:43:06 +02004129static void vmx_decache_cr0_guest_bits(struct kvm_vcpu *vcpu)
4130{
4131 ulong cr0_guest_owned_bits = vcpu->arch.cr0_guest_owned_bits;
4132
4133 vcpu->arch.cr0 &= ~cr0_guest_owned_bits;
4134 vcpu->arch.cr0 |= vmcs_readl(GUEST_CR0) & cr0_guest_owned_bits;
4135}
4136
Avi Kivityaff48ba2010-12-05 18:56:11 +02004137static void vmx_decache_cr3(struct kvm_vcpu *vcpu)
4138{
4139 if (enable_ept && is_paging(vcpu))
4140 vcpu->arch.cr3 = vmcs_readl(GUEST_CR3);
4141 __set_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail);
4142}
4143
Anthony Liguori25c4c272007-04-27 09:29:21 +03004144static void vmx_decache_cr4_guest_bits(struct kvm_vcpu *vcpu)
Avi Kivity399badf2007-01-05 16:36:38 -08004145{
Avi Kivityfc78f512009-12-07 12:16:48 +02004146 ulong cr4_guest_owned_bits = vcpu->arch.cr4_guest_owned_bits;
4147
4148 vcpu->arch.cr4 &= ~cr4_guest_owned_bits;
4149 vcpu->arch.cr4 |= vmcs_readl(GUEST_CR4) & cr4_guest_owned_bits;
Avi Kivity399badf2007-01-05 16:36:38 -08004150}
4151
Sheng Yang14394422008-04-28 12:24:45 +08004152static void ept_load_pdptrs(struct kvm_vcpu *vcpu)
4153{
Gleb Natapovd0d538b2013-10-09 19:13:19 +03004154 struct kvm_mmu *mmu = vcpu->arch.walk_mmu;
4155
Avi Kivity6de4f3a2009-05-31 22:58:47 +03004156 if (!test_bit(VCPU_EXREG_PDPTR,
4157 (unsigned long *)&vcpu->arch.regs_dirty))
4158 return;
4159
Sheng Yang14394422008-04-28 12:24:45 +08004160 if (is_paging(vcpu) && is_pae(vcpu) && !is_long_mode(vcpu)) {
Gleb Natapovd0d538b2013-10-09 19:13:19 +03004161 vmcs_write64(GUEST_PDPTR0, mmu->pdptrs[0]);
4162 vmcs_write64(GUEST_PDPTR1, mmu->pdptrs[1]);
4163 vmcs_write64(GUEST_PDPTR2, mmu->pdptrs[2]);
4164 vmcs_write64(GUEST_PDPTR3, mmu->pdptrs[3]);
Sheng Yang14394422008-04-28 12:24:45 +08004165 }
4166}
4167
Avi Kivity8f5d5492009-05-31 18:41:29 +03004168static void ept_save_pdptrs(struct kvm_vcpu *vcpu)
4169{
Gleb Natapovd0d538b2013-10-09 19:13:19 +03004170 struct kvm_mmu *mmu = vcpu->arch.walk_mmu;
4171
Avi Kivity8f5d5492009-05-31 18:41:29 +03004172 if (is_paging(vcpu) && is_pae(vcpu) && !is_long_mode(vcpu)) {
Gleb Natapovd0d538b2013-10-09 19:13:19 +03004173 mmu->pdptrs[0] = vmcs_read64(GUEST_PDPTR0);
4174 mmu->pdptrs[1] = vmcs_read64(GUEST_PDPTR1);
4175 mmu->pdptrs[2] = vmcs_read64(GUEST_PDPTR2);
4176 mmu->pdptrs[3] = vmcs_read64(GUEST_PDPTR3);
Avi Kivity8f5d5492009-05-31 18:41:29 +03004177 }
Avi Kivity6de4f3a2009-05-31 22:58:47 +03004178
4179 __set_bit(VCPU_EXREG_PDPTR,
4180 (unsigned long *)&vcpu->arch.regs_avail);
4181 __set_bit(VCPU_EXREG_PDPTR,
4182 (unsigned long *)&vcpu->arch.regs_dirty);
Avi Kivity8f5d5492009-05-31 18:41:29 +03004183}
4184
David Matlack38991522016-11-29 18:14:08 -08004185static bool nested_guest_cr0_valid(struct kvm_vcpu *vcpu, unsigned long val)
4186{
4187 u64 fixed0 = to_vmx(vcpu)->nested.nested_vmx_cr0_fixed0;
4188 u64 fixed1 = to_vmx(vcpu)->nested.nested_vmx_cr0_fixed1;
4189 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
4190
4191 if (to_vmx(vcpu)->nested.nested_vmx_secondary_ctls_high &
4192 SECONDARY_EXEC_UNRESTRICTED_GUEST &&
4193 nested_cpu_has2(vmcs12, SECONDARY_EXEC_UNRESTRICTED_GUEST))
4194 fixed0 &= ~(X86_CR0_PE | X86_CR0_PG);
4195
4196 return fixed_bits_valid(val, fixed0, fixed1);
4197}
4198
4199static bool nested_host_cr0_valid(struct kvm_vcpu *vcpu, unsigned long val)
4200{
4201 u64 fixed0 = to_vmx(vcpu)->nested.nested_vmx_cr0_fixed0;
4202 u64 fixed1 = to_vmx(vcpu)->nested.nested_vmx_cr0_fixed1;
4203
4204 return fixed_bits_valid(val, fixed0, fixed1);
4205}
4206
4207static bool nested_cr4_valid(struct kvm_vcpu *vcpu, unsigned long val)
4208{
4209 u64 fixed0 = to_vmx(vcpu)->nested.nested_vmx_cr4_fixed0;
4210 u64 fixed1 = to_vmx(vcpu)->nested.nested_vmx_cr4_fixed1;
4211
4212 return fixed_bits_valid(val, fixed0, fixed1);
4213}
4214
4215/* No difference in the restrictions on guest and host CR4 in VMX operation. */
4216#define nested_guest_cr4_valid nested_cr4_valid
4217#define nested_host_cr4_valid nested_cr4_valid
4218
Nadav Har'El5e1746d2011-05-25 23:03:24 +03004219static int vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4);
Sheng Yang14394422008-04-28 12:24:45 +08004220
4221static void ept_update_paging_mode_cr0(unsigned long *hw_cr0,
4222 unsigned long cr0,
4223 struct kvm_vcpu *vcpu)
4224{
Marcelo Tosatti5233dd52011-06-06 14:27:47 -03004225 if (!test_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail))
4226 vmx_decache_cr3(vcpu);
Sheng Yang14394422008-04-28 12:24:45 +08004227 if (!(cr0 & X86_CR0_PG)) {
4228 /* From paging/starting to nonpaging */
4229 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL,
Sheng Yang65267ea2008-06-18 14:43:38 +08004230 vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) |
Sheng Yang14394422008-04-28 12:24:45 +08004231 (CPU_BASED_CR3_LOAD_EXITING |
4232 CPU_BASED_CR3_STORE_EXITING));
4233 vcpu->arch.cr0 = cr0;
Avi Kivityfc78f512009-12-07 12:16:48 +02004234 vmx_set_cr4(vcpu, kvm_read_cr4(vcpu));
Sheng Yang14394422008-04-28 12:24:45 +08004235 } else if (!is_paging(vcpu)) {
4236 /* From nonpaging to paging */
4237 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL,
Sheng Yang65267ea2008-06-18 14:43:38 +08004238 vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) &
Sheng Yang14394422008-04-28 12:24:45 +08004239 ~(CPU_BASED_CR3_LOAD_EXITING |
4240 CPU_BASED_CR3_STORE_EXITING));
4241 vcpu->arch.cr0 = cr0;
Avi Kivityfc78f512009-12-07 12:16:48 +02004242 vmx_set_cr4(vcpu, kvm_read_cr4(vcpu));
Sheng Yang14394422008-04-28 12:24:45 +08004243 }
Sheng Yang95eb84a2009-08-19 09:52:18 +08004244
4245 if (!(cr0 & X86_CR0_WP))
4246 *hw_cr0 &= ~X86_CR0_WP;
Sheng Yang14394422008-04-28 12:24:45 +08004247}
4248
Avi Kivity6aa8b732006-12-10 02:21:36 -08004249static void vmx_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
4250{
Avi Kivity7ffd92c2009-06-09 14:10:45 +03004251 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nitin A Kamble3a624e22009-06-08 11:34:16 -07004252 unsigned long hw_cr0;
4253
Gleb Natapov50378782013-02-04 16:00:28 +02004254 hw_cr0 = (cr0 & ~KVM_GUEST_CR0_MASK);
Nitin A Kamble3a624e22009-06-08 11:34:16 -07004255 if (enable_unrestricted_guest)
Gleb Natapov50378782013-02-04 16:00:28 +02004256 hw_cr0 |= KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST;
Gleb Natapov218e7632013-01-21 15:36:45 +02004257 else {
Gleb Natapov50378782013-02-04 16:00:28 +02004258 hw_cr0 |= KVM_VM_CR0_ALWAYS_ON;
Sheng Yang14394422008-04-28 12:24:45 +08004259
Gleb Natapov218e7632013-01-21 15:36:45 +02004260 if (vmx->rmode.vm86_active && (cr0 & X86_CR0_PE))
4261 enter_pmode(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004262
Gleb Natapov218e7632013-01-21 15:36:45 +02004263 if (!vmx->rmode.vm86_active && !(cr0 & X86_CR0_PE))
4264 enter_rmode(vcpu);
4265 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08004266
Avi Kivity05b3e0c2006-12-13 00:33:45 -08004267#ifdef CONFIG_X86_64
Avi Kivityf6801df2010-01-21 15:31:50 +02004268 if (vcpu->arch.efer & EFER_LME) {
Rusty Russell707d92fa2007-07-17 23:19:08 +10004269 if (!is_paging(vcpu) && (cr0 & X86_CR0_PG))
Avi Kivity6aa8b732006-12-10 02:21:36 -08004270 enter_lmode(vcpu);
Rusty Russell707d92fa2007-07-17 23:19:08 +10004271 if (is_paging(vcpu) && !(cr0 & X86_CR0_PG))
Avi Kivity6aa8b732006-12-10 02:21:36 -08004272 exit_lmode(vcpu);
4273 }
4274#endif
4275
Avi Kivity089d0342009-03-23 18:26:32 +02004276 if (enable_ept)
Sheng Yang14394422008-04-28 12:24:45 +08004277 ept_update_paging_mode_cr0(&hw_cr0, cr0, vcpu);
4278
Avi Kivity6aa8b732006-12-10 02:21:36 -08004279 vmcs_writel(CR0_READ_SHADOW, cr0);
Sheng Yang14394422008-04-28 12:24:45 +08004280 vmcs_writel(GUEST_CR0, hw_cr0);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08004281 vcpu->arch.cr0 = cr0;
Gleb Natapov14168782013-01-21 15:36:49 +02004282
4283 /* depends on vcpu->arch.cr0 to be set to a new value */
4284 vmx->emulation_required = emulation_required(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004285}
4286
Peter Feiner995f00a2017-06-30 17:26:32 -07004287static u64 construct_eptp(struct kvm_vcpu *vcpu, unsigned long root_hpa)
Sheng Yang14394422008-04-28 12:24:45 +08004288{
4289 u64 eptp;
4290
4291 /* TODO write the value reading from MSR */
4292 eptp = VMX_EPT_DEFAULT_MT |
4293 VMX_EPT_DEFAULT_GAW << VMX_EPT_GAW_EPTP_SHIFT;
Peter Feiner995f00a2017-06-30 17:26:32 -07004294 if (enable_ept_ad_bits &&
4295 (!is_guest_mode(vcpu) || nested_ept_ad_enabled(vcpu)))
Xudong Haob38f9932012-05-28 19:33:36 +08004296 eptp |= VMX_EPT_AD_ENABLE_BIT;
Sheng Yang14394422008-04-28 12:24:45 +08004297 eptp |= (root_hpa & PAGE_MASK);
4298
4299 return eptp;
4300}
4301
Avi Kivity6aa8b732006-12-10 02:21:36 -08004302static void vmx_set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3)
4303{
Sheng Yang14394422008-04-28 12:24:45 +08004304 unsigned long guest_cr3;
4305 u64 eptp;
4306
4307 guest_cr3 = cr3;
Avi Kivity089d0342009-03-23 18:26:32 +02004308 if (enable_ept) {
Peter Feiner995f00a2017-06-30 17:26:32 -07004309 eptp = construct_eptp(vcpu, cr3);
Sheng Yang14394422008-04-28 12:24:45 +08004310 vmcs_write64(EPT_POINTER, eptp);
Jan Kiszka59ab5a82013-08-08 16:26:29 +02004311 if (is_paging(vcpu) || is_guest_mode(vcpu))
4312 guest_cr3 = kvm_read_cr3(vcpu);
4313 else
4314 guest_cr3 = vcpu->kvm->arch.ept_identity_map_addr;
Marcelo Tosatti7c93be442009-10-26 16:48:33 -02004315 ept_load_pdptrs(vcpu);
Sheng Yang14394422008-04-28 12:24:45 +08004316 }
4317
Sheng Yang2384d2b2008-01-17 15:14:33 +08004318 vmx_flush_tlb(vcpu);
Sheng Yang14394422008-04-28 12:24:45 +08004319 vmcs_writel(GUEST_CR3, guest_cr3);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004320}
4321
Nadav Har'El5e1746d2011-05-25 23:03:24 +03004322static int vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004323{
Ben Serebrin085e68e2015-04-16 11:58:05 -07004324 /*
4325 * Pass through host's Machine Check Enable value to hw_cr4, which
4326 * is in force while we are in guest mode. Do not let guests control
4327 * this bit, even if host CR4.MCE == 0.
4328 */
4329 unsigned long hw_cr4 =
4330 (cr4_read_shadow() & X86_CR4_MCE) |
4331 (cr4 & ~X86_CR4_MCE) |
4332 (to_vmx(vcpu)->rmode.vm86_active ?
4333 KVM_RMODE_VM_CR4_ALWAYS_ON : KVM_PMODE_VM_CR4_ALWAYS_ON);
Sheng Yang14394422008-04-28 12:24:45 +08004334
Nadav Har'El5e1746d2011-05-25 23:03:24 +03004335 if (cr4 & X86_CR4_VMXE) {
4336 /*
4337 * To use VMXON (and later other VMX instructions), a guest
4338 * must first be able to turn on cr4.VMXE (see handle_vmon()).
4339 * So basically the check on whether to allow nested VMX
4340 * is here.
4341 */
4342 if (!nested_vmx_allowed(vcpu))
4343 return 1;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01004344 }
David Matlack38991522016-11-29 18:14:08 -08004345
4346 if (to_vmx(vcpu)->nested.vmxon && !nested_cr4_valid(vcpu, cr4))
Nadav Har'El5e1746d2011-05-25 23:03:24 +03004347 return 1;
4348
Zhang Xiantaoad312c72007-12-13 23:50:52 +08004349 vcpu->arch.cr4 = cr4;
Avi Kivitybc230082009-12-08 12:14:42 +02004350 if (enable_ept) {
4351 if (!is_paging(vcpu)) {
4352 hw_cr4 &= ~X86_CR4_PAE;
4353 hw_cr4 |= X86_CR4_PSE;
4354 } else if (!(cr4 & X86_CR4_PAE)) {
4355 hw_cr4 &= ~X86_CR4_PAE;
4356 }
4357 }
Sheng Yang14394422008-04-28 12:24:45 +08004358
Radim Krčmář656ec4a2015-11-02 22:20:00 +01004359 if (!enable_unrestricted_guest && !is_paging(vcpu))
4360 /*
Huaitong Handdba2622016-03-22 16:51:15 +08004361 * SMEP/SMAP/PKU is disabled if CPU is in non-paging mode in
4362 * hardware. To emulate this behavior, SMEP/SMAP/PKU needs
4363 * to be manually disabled when guest switches to non-paging
4364 * mode.
4365 *
4366 * If !enable_unrestricted_guest, the CPU is always running
4367 * with CR0.PG=1 and CR4 needs to be modified.
4368 * If enable_unrestricted_guest, the CPU automatically
4369 * disables SMEP/SMAP/PKU when the guest sets CR0.PG=0.
Radim Krčmář656ec4a2015-11-02 22:20:00 +01004370 */
Huaitong Handdba2622016-03-22 16:51:15 +08004371 hw_cr4 &= ~(X86_CR4_SMEP | X86_CR4_SMAP | X86_CR4_PKE);
Radim Krčmář656ec4a2015-11-02 22:20:00 +01004372
Sheng Yang14394422008-04-28 12:24:45 +08004373 vmcs_writel(CR4_READ_SHADOW, cr4);
4374 vmcs_writel(GUEST_CR4, hw_cr4);
Nadav Har'El5e1746d2011-05-25 23:03:24 +03004375 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004376}
4377
Avi Kivity6aa8b732006-12-10 02:21:36 -08004378static void vmx_get_segment(struct kvm_vcpu *vcpu,
4379 struct kvm_segment *var, int seg)
4380{
Avi Kivitya9179492011-01-03 14:28:52 +02004381 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004382 u32 ar;
4383
Gleb Natapovc6ad11532012-12-12 19:10:51 +02004384 if (vmx->rmode.vm86_active && seg != VCPU_SREG_LDTR) {
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03004385 *var = vmx->rmode.segs[seg];
Avi Kivitya9179492011-01-03 14:28:52 +02004386 if (seg == VCPU_SREG_TR
Avi Kivity2fb92db2011-04-27 19:42:18 +03004387 || var->selector == vmx_read_guest_seg_selector(vmx, seg))
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03004388 return;
Avi Kivity1390a282012-08-21 17:07:08 +03004389 var->base = vmx_read_guest_seg_base(vmx, seg);
4390 var->selector = vmx_read_guest_seg_selector(vmx, seg);
4391 return;
Avi Kivitya9179492011-01-03 14:28:52 +02004392 }
Avi Kivity2fb92db2011-04-27 19:42:18 +03004393 var->base = vmx_read_guest_seg_base(vmx, seg);
4394 var->limit = vmx_read_guest_seg_limit(vmx, seg);
4395 var->selector = vmx_read_guest_seg_selector(vmx, seg);
4396 ar = vmx_read_guest_seg_ar(vmx, seg);
Gleb Natapov03617c12013-06-28 13:17:18 +03004397 var->unusable = (ar >> 16) & 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004398 var->type = ar & 15;
4399 var->s = (ar >> 4) & 1;
4400 var->dpl = (ar >> 5) & 3;
Gleb Natapov03617c12013-06-28 13:17:18 +03004401 /*
4402 * Some userspaces do not preserve unusable property. Since usable
4403 * segment has to be present according to VMX spec we can use present
4404 * property to amend userspace bug by making unusable segment always
4405 * nonpresent. vmx_segment_access_rights() already marks nonpresent
4406 * segment as unusable.
4407 */
4408 var->present = !var->unusable;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004409 var->avl = (ar >> 12) & 1;
4410 var->l = (ar >> 13) & 1;
4411 var->db = (ar >> 14) & 1;
4412 var->g = (ar >> 15) & 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004413}
4414
Avi Kivitya9179492011-01-03 14:28:52 +02004415static u64 vmx_get_segment_base(struct kvm_vcpu *vcpu, int seg)
4416{
Avi Kivitya9179492011-01-03 14:28:52 +02004417 struct kvm_segment s;
4418
4419 if (to_vmx(vcpu)->rmode.vm86_active) {
4420 vmx_get_segment(vcpu, &s, seg);
4421 return s.base;
4422 }
Avi Kivity2fb92db2011-04-27 19:42:18 +03004423 return vmx_read_guest_seg_base(to_vmx(vcpu), seg);
Avi Kivitya9179492011-01-03 14:28:52 +02004424}
4425
Marcelo Tosattib09408d2013-01-07 19:27:06 -02004426static int vmx_get_cpl(struct kvm_vcpu *vcpu)
Izik Eidus2e4d2652008-03-24 19:38:34 +02004427{
Marcelo Tosattib09408d2013-01-07 19:27:06 -02004428 struct vcpu_vmx *vmx = to_vmx(vcpu);
4429
Paolo Bonziniae9fedc2014-05-14 09:39:49 +02004430 if (unlikely(vmx->rmode.vm86_active))
Izik Eidus2e4d2652008-03-24 19:38:34 +02004431 return 0;
Paolo Bonziniae9fedc2014-05-14 09:39:49 +02004432 else {
4433 int ar = vmx_read_guest_seg_ar(vmx, VCPU_SREG_SS);
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07004434 return VMX_AR_DPL(ar);
Avi Kivity69c73022011-03-07 15:26:44 +02004435 }
Avi Kivity69c73022011-03-07 15:26:44 +02004436}
4437
Avi Kivity653e3102007-05-07 10:55:37 +03004438static u32 vmx_segment_access_rights(struct kvm_segment *var)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004439{
Avi Kivity6aa8b732006-12-10 02:21:36 -08004440 u32 ar;
4441
Avi Kivityf0495f92012-06-07 17:06:10 +03004442 if (var->unusable || !var->present)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004443 ar = 1 << 16;
4444 else {
4445 ar = var->type & 15;
4446 ar |= (var->s & 1) << 4;
4447 ar |= (var->dpl & 3) << 5;
4448 ar |= (var->present & 1) << 7;
4449 ar |= (var->avl & 1) << 12;
4450 ar |= (var->l & 1) << 13;
4451 ar |= (var->db & 1) << 14;
4452 ar |= (var->g & 1) << 15;
4453 }
Avi Kivity653e3102007-05-07 10:55:37 +03004454
4455 return ar;
4456}
4457
4458static void vmx_set_segment(struct kvm_vcpu *vcpu,
4459 struct kvm_segment *var, int seg)
4460{
Avi Kivity7ffd92c2009-06-09 14:10:45 +03004461 struct vcpu_vmx *vmx = to_vmx(vcpu);
Mathias Krause772e0312012-08-30 01:30:19 +02004462 const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
Avi Kivity653e3102007-05-07 10:55:37 +03004463
Avi Kivity2fb92db2011-04-27 19:42:18 +03004464 vmx_segment_cache_clear(vmx);
4465
Gleb Natapov1ecd50a2012-12-12 19:10:54 +02004466 if (vmx->rmode.vm86_active && seg != VCPU_SREG_LDTR) {
4467 vmx->rmode.segs[seg] = *var;
4468 if (seg == VCPU_SREG_TR)
4469 vmcs_write16(sf->selector, var->selector);
4470 else if (var->s)
4471 fix_rmode_seg(seg, &vmx->rmode.segs[seg]);
Gleb Natapovd99e4152012-12-20 16:57:45 +02004472 goto out;
Avi Kivity653e3102007-05-07 10:55:37 +03004473 }
Gleb Natapov1ecd50a2012-12-12 19:10:54 +02004474
Avi Kivity653e3102007-05-07 10:55:37 +03004475 vmcs_writel(sf->base, var->base);
4476 vmcs_write32(sf->limit, var->limit);
4477 vmcs_write16(sf->selector, var->selector);
Nitin A Kamble3a624e22009-06-08 11:34:16 -07004478
4479 /*
4480 * Fix the "Accessed" bit in AR field of segment registers for older
4481 * qemu binaries.
4482 * IA32 arch specifies that at the time of processor reset the
4483 * "Accessed" bit in the AR field of segment registers is 1. And qemu
Guo Chao0fa06072012-06-28 15:16:19 +08004484 * is setting it to 0 in the userland code. This causes invalid guest
Nitin A Kamble3a624e22009-06-08 11:34:16 -07004485 * state vmexit when "unrestricted guest" mode is turned on.
4486 * Fix for this setup issue in cpu_reset is being pushed in the qemu
4487 * tree. Newer qemu binaries with that qemu fix would not need this
4488 * kvm hack.
4489 */
4490 if (enable_unrestricted_guest && (seg != VCPU_SREG_LDTR))
Gleb Natapovf924d662012-12-12 19:10:55 +02004491 var->type |= 0x1; /* Accessed */
Nitin A Kamble3a624e22009-06-08 11:34:16 -07004492
Gleb Natapovf924d662012-12-12 19:10:55 +02004493 vmcs_write32(sf->ar_bytes, vmx_segment_access_rights(var));
Gleb Natapovd99e4152012-12-20 16:57:45 +02004494
4495out:
Paolo Bonzini98eb2f82014-03-27 09:51:52 +01004496 vmx->emulation_required = emulation_required(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004497}
4498
Avi Kivity6aa8b732006-12-10 02:21:36 -08004499static void vmx_get_cs_db_l_bits(struct kvm_vcpu *vcpu, int *db, int *l)
4500{
Avi Kivity2fb92db2011-04-27 19:42:18 +03004501 u32 ar = vmx_read_guest_seg_ar(to_vmx(vcpu), VCPU_SREG_CS);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004502
4503 *db = (ar >> 14) & 1;
4504 *l = (ar >> 13) & 1;
4505}
4506
Gleb Natapov89a27f42010-02-16 10:51:48 +02004507static void vmx_get_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004508{
Gleb Natapov89a27f42010-02-16 10:51:48 +02004509 dt->size = vmcs_read32(GUEST_IDTR_LIMIT);
4510 dt->address = vmcs_readl(GUEST_IDTR_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004511}
4512
Gleb Natapov89a27f42010-02-16 10:51:48 +02004513static void vmx_set_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004514{
Gleb Natapov89a27f42010-02-16 10:51:48 +02004515 vmcs_write32(GUEST_IDTR_LIMIT, dt->size);
4516 vmcs_writel(GUEST_IDTR_BASE, dt->address);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004517}
4518
Gleb Natapov89a27f42010-02-16 10:51:48 +02004519static void vmx_get_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004520{
Gleb Natapov89a27f42010-02-16 10:51:48 +02004521 dt->size = vmcs_read32(GUEST_GDTR_LIMIT);
4522 dt->address = vmcs_readl(GUEST_GDTR_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004523}
4524
Gleb Natapov89a27f42010-02-16 10:51:48 +02004525static void vmx_set_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004526{
Gleb Natapov89a27f42010-02-16 10:51:48 +02004527 vmcs_write32(GUEST_GDTR_LIMIT, dt->size);
4528 vmcs_writel(GUEST_GDTR_BASE, dt->address);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004529}
4530
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004531static bool rmode_segment_valid(struct kvm_vcpu *vcpu, int seg)
4532{
4533 struct kvm_segment var;
4534 u32 ar;
4535
4536 vmx_get_segment(vcpu, &var, seg);
Gleb Natapov07f42f52012-12-12 19:10:49 +02004537 var.dpl = 0x3;
Gleb Natapov0647f4a2012-12-12 19:10:50 +02004538 if (seg == VCPU_SREG_CS)
4539 var.type = 0x3;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004540 ar = vmx_segment_access_rights(&var);
4541
4542 if (var.base != (var.selector << 4))
4543 return false;
Gleb Natapov89efbed2012-12-20 16:57:44 +02004544 if (var.limit != 0xffff)
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004545 return false;
Gleb Natapov07f42f52012-12-12 19:10:49 +02004546 if (ar != 0xf3)
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004547 return false;
4548
4549 return true;
4550}
4551
4552static bool code_segment_valid(struct kvm_vcpu *vcpu)
4553{
4554 struct kvm_segment cs;
4555 unsigned int cs_rpl;
4556
4557 vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
Nadav Amitb32a9912015-03-29 16:33:04 +03004558 cs_rpl = cs.selector & SEGMENT_RPL_MASK;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004559
Avi Kivity1872a3f2009-01-04 23:26:52 +02004560 if (cs.unusable)
4561 return false;
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07004562 if (~cs.type & (VMX_AR_TYPE_CODE_MASK|VMX_AR_TYPE_ACCESSES_MASK))
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004563 return false;
4564 if (!cs.s)
4565 return false;
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07004566 if (cs.type & VMX_AR_TYPE_WRITEABLE_MASK) {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004567 if (cs.dpl > cs_rpl)
4568 return false;
Avi Kivity1872a3f2009-01-04 23:26:52 +02004569 } else {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004570 if (cs.dpl != cs_rpl)
4571 return false;
4572 }
4573 if (!cs.present)
4574 return false;
4575
4576 /* TODO: Add Reserved field check, this'll require a new member in the kvm_segment_field structure */
4577 return true;
4578}
4579
4580static bool stack_segment_valid(struct kvm_vcpu *vcpu)
4581{
4582 struct kvm_segment ss;
4583 unsigned int ss_rpl;
4584
4585 vmx_get_segment(vcpu, &ss, VCPU_SREG_SS);
Nadav Amitb32a9912015-03-29 16:33:04 +03004586 ss_rpl = ss.selector & SEGMENT_RPL_MASK;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004587
Avi Kivity1872a3f2009-01-04 23:26:52 +02004588 if (ss.unusable)
4589 return true;
4590 if (ss.type != 3 && ss.type != 7)
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004591 return false;
4592 if (!ss.s)
4593 return false;
4594 if (ss.dpl != ss_rpl) /* DPL != RPL */
4595 return false;
4596 if (!ss.present)
4597 return false;
4598
4599 return true;
4600}
4601
4602static bool data_segment_valid(struct kvm_vcpu *vcpu, int seg)
4603{
4604 struct kvm_segment var;
4605 unsigned int rpl;
4606
4607 vmx_get_segment(vcpu, &var, seg);
Nadav Amitb32a9912015-03-29 16:33:04 +03004608 rpl = var.selector & SEGMENT_RPL_MASK;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004609
Avi Kivity1872a3f2009-01-04 23:26:52 +02004610 if (var.unusable)
4611 return true;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004612 if (!var.s)
4613 return false;
4614 if (!var.present)
4615 return false;
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07004616 if (~var.type & (VMX_AR_TYPE_CODE_MASK|VMX_AR_TYPE_WRITEABLE_MASK)) {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004617 if (var.dpl < rpl) /* DPL < RPL */
4618 return false;
4619 }
4620
4621 /* TODO: Add other members to kvm_segment_field to allow checking for other access
4622 * rights flags
4623 */
4624 return true;
4625}
4626
4627static bool tr_valid(struct kvm_vcpu *vcpu)
4628{
4629 struct kvm_segment tr;
4630
4631 vmx_get_segment(vcpu, &tr, VCPU_SREG_TR);
4632
Avi Kivity1872a3f2009-01-04 23:26:52 +02004633 if (tr.unusable)
4634 return false;
Nadav Amitb32a9912015-03-29 16:33:04 +03004635 if (tr.selector & SEGMENT_TI_MASK) /* TI = 1 */
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004636 return false;
Avi Kivity1872a3f2009-01-04 23:26:52 +02004637 if (tr.type != 3 && tr.type != 11) /* TODO: Check if guest is in IA32e mode */
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004638 return false;
4639 if (!tr.present)
4640 return false;
4641
4642 return true;
4643}
4644
4645static bool ldtr_valid(struct kvm_vcpu *vcpu)
4646{
4647 struct kvm_segment ldtr;
4648
4649 vmx_get_segment(vcpu, &ldtr, VCPU_SREG_LDTR);
4650
Avi Kivity1872a3f2009-01-04 23:26:52 +02004651 if (ldtr.unusable)
4652 return true;
Nadav Amitb32a9912015-03-29 16:33:04 +03004653 if (ldtr.selector & SEGMENT_TI_MASK) /* TI = 1 */
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004654 return false;
4655 if (ldtr.type != 2)
4656 return false;
4657 if (!ldtr.present)
4658 return false;
4659
4660 return true;
4661}
4662
4663static bool cs_ss_rpl_check(struct kvm_vcpu *vcpu)
4664{
4665 struct kvm_segment cs, ss;
4666
4667 vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
4668 vmx_get_segment(vcpu, &ss, VCPU_SREG_SS);
4669
Nadav Amitb32a9912015-03-29 16:33:04 +03004670 return ((cs.selector & SEGMENT_RPL_MASK) ==
4671 (ss.selector & SEGMENT_RPL_MASK));
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004672}
4673
4674/*
4675 * Check if guest state is valid. Returns true if valid, false if
4676 * not.
4677 * We assume that registers are always usable
4678 */
4679static bool guest_state_valid(struct kvm_vcpu *vcpu)
4680{
Gleb Natapovc5e97c82013-01-21 15:36:43 +02004681 if (enable_unrestricted_guest)
4682 return true;
4683
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004684 /* real mode guest state checks */
Gleb Natapovf13882d2013-04-14 16:07:37 +03004685 if (!is_protmode(vcpu) || (vmx_get_rflags(vcpu) & X86_EFLAGS_VM)) {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004686 if (!rmode_segment_valid(vcpu, VCPU_SREG_CS))
4687 return false;
4688 if (!rmode_segment_valid(vcpu, VCPU_SREG_SS))
4689 return false;
4690 if (!rmode_segment_valid(vcpu, VCPU_SREG_DS))
4691 return false;
4692 if (!rmode_segment_valid(vcpu, VCPU_SREG_ES))
4693 return false;
4694 if (!rmode_segment_valid(vcpu, VCPU_SREG_FS))
4695 return false;
4696 if (!rmode_segment_valid(vcpu, VCPU_SREG_GS))
4697 return false;
4698 } else {
4699 /* protected mode guest state checks */
4700 if (!cs_ss_rpl_check(vcpu))
4701 return false;
4702 if (!code_segment_valid(vcpu))
4703 return false;
4704 if (!stack_segment_valid(vcpu))
4705 return false;
4706 if (!data_segment_valid(vcpu, VCPU_SREG_DS))
4707 return false;
4708 if (!data_segment_valid(vcpu, VCPU_SREG_ES))
4709 return false;
4710 if (!data_segment_valid(vcpu, VCPU_SREG_FS))
4711 return false;
4712 if (!data_segment_valid(vcpu, VCPU_SREG_GS))
4713 return false;
4714 if (!tr_valid(vcpu))
4715 return false;
4716 if (!ldtr_valid(vcpu))
4717 return false;
4718 }
4719 /* TODO:
4720 * - Add checks on RIP
4721 * - Add checks on RFLAGS
4722 */
4723
4724 return true;
4725}
4726
Jim Mattson5fa99cb2017-07-06 16:33:07 -07004727static bool page_address_valid(struct kvm_vcpu *vcpu, gpa_t gpa)
4728{
4729 return PAGE_ALIGNED(gpa) && !(gpa >> cpuid_maxphyaddr(vcpu));
4730}
4731
Mike Dayd77c26f2007-10-08 09:02:08 -04004732static int init_rmode_tss(struct kvm *kvm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004733{
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08004734 gfn_t fn;
Izik Eidus195aefd2007-10-01 22:14:18 +02004735 u16 data = 0;
Paolo Bonzini1f755a82014-09-16 13:37:40 +02004736 int idx, r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004737
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08004738 idx = srcu_read_lock(&kvm->srcu);
Jan Kiszka4918c6c2013-03-15 08:38:56 +01004739 fn = kvm->arch.tss_addr >> PAGE_SHIFT;
Izik Eidus195aefd2007-10-01 22:14:18 +02004740 r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE);
4741 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05004742 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02004743 data = TSS_BASE_SIZE + TSS_REDIRECTION_SIZE;
Sheng Yang464d17c2008-08-13 14:10:33 +08004744 r = kvm_write_guest_page(kvm, fn++, &data,
4745 TSS_IOPB_BASE_OFFSET, sizeof(u16));
Izik Eidus195aefd2007-10-01 22:14:18 +02004746 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05004747 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02004748 r = kvm_clear_guest_page(kvm, fn++, 0, PAGE_SIZE);
4749 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05004750 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02004751 r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE);
4752 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05004753 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02004754 data = ~0;
Marcelo Tosatti10589a42007-12-20 19:18:22 -05004755 r = kvm_write_guest_page(kvm, fn, &data,
4756 RMODE_TSS_SIZE - 2 * PAGE_SIZE - 1,
4757 sizeof(u8));
Marcelo Tosatti10589a42007-12-20 19:18:22 -05004758out:
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08004759 srcu_read_unlock(&kvm->srcu, idx);
Paolo Bonzini1f755a82014-09-16 13:37:40 +02004760 return r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004761}
4762
Sheng Yangb7ebfb02008-04-25 21:44:52 +08004763static int init_rmode_identity_map(struct kvm *kvm)
4764{
Tang Chenf51770e2014-09-16 18:41:59 +08004765 int i, idx, r = 0;
Dan Williamsba049e92016-01-15 16:56:11 -08004766 kvm_pfn_t identity_map_pfn;
Sheng Yangb7ebfb02008-04-25 21:44:52 +08004767 u32 tmp;
4768
Avi Kivity089d0342009-03-23 18:26:32 +02004769 if (!enable_ept)
Tang Chenf51770e2014-09-16 18:41:59 +08004770 return 0;
Tang Chena255d472014-09-16 18:41:58 +08004771
4772 /* Protect kvm->arch.ept_identity_pagetable_done. */
4773 mutex_lock(&kvm->slots_lock);
4774
Tang Chenf51770e2014-09-16 18:41:59 +08004775 if (likely(kvm->arch.ept_identity_pagetable_done))
Tang Chena255d472014-09-16 18:41:58 +08004776 goto out2;
Tang Chena255d472014-09-16 18:41:58 +08004777
Sheng Yangb927a3c2009-07-21 10:42:48 +08004778 identity_map_pfn = kvm->arch.ept_identity_map_addr >> PAGE_SHIFT;
Tang Chena255d472014-09-16 18:41:58 +08004779
4780 r = alloc_identity_pagetable(kvm);
Tang Chenf51770e2014-09-16 18:41:59 +08004781 if (r < 0)
Tang Chena255d472014-09-16 18:41:58 +08004782 goto out2;
4783
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08004784 idx = srcu_read_lock(&kvm->srcu);
Sheng Yangb7ebfb02008-04-25 21:44:52 +08004785 r = kvm_clear_guest_page(kvm, identity_map_pfn, 0, PAGE_SIZE);
4786 if (r < 0)
4787 goto out;
4788 /* Set up identity-mapping pagetable for EPT in real mode */
4789 for (i = 0; i < PT32_ENT_PER_PAGE; i++) {
4790 tmp = (i << 22) + (_PAGE_PRESENT | _PAGE_RW | _PAGE_USER |
4791 _PAGE_ACCESSED | _PAGE_DIRTY | _PAGE_PSE);
4792 r = kvm_write_guest_page(kvm, identity_map_pfn,
4793 &tmp, i * sizeof(tmp), sizeof(tmp));
4794 if (r < 0)
4795 goto out;
4796 }
4797 kvm->arch.ept_identity_pagetable_done = true;
Tang Chenf51770e2014-09-16 18:41:59 +08004798
Sheng Yangb7ebfb02008-04-25 21:44:52 +08004799out:
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08004800 srcu_read_unlock(&kvm->srcu, idx);
Tang Chena255d472014-09-16 18:41:58 +08004801
4802out2:
4803 mutex_unlock(&kvm->slots_lock);
Tang Chenf51770e2014-09-16 18:41:59 +08004804 return r;
Sheng Yangb7ebfb02008-04-25 21:44:52 +08004805}
4806
Avi Kivity6aa8b732006-12-10 02:21:36 -08004807static void seg_setup(int seg)
4808{
Mathias Krause772e0312012-08-30 01:30:19 +02004809 const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
Nitin A Kamble3a624e22009-06-08 11:34:16 -07004810 unsigned int ar;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004811
4812 vmcs_write16(sf->selector, 0);
4813 vmcs_writel(sf->base, 0);
4814 vmcs_write32(sf->limit, 0xffff);
Gleb Natapovd54d07b2012-12-20 16:57:46 +02004815 ar = 0x93;
4816 if (seg == VCPU_SREG_CS)
4817 ar |= 0x08; /* code segment */
Nitin A Kamble3a624e22009-06-08 11:34:16 -07004818
4819 vmcs_write32(sf->ar_bytes, ar);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004820}
4821
Sheng Yangf78e0e22007-10-29 09:40:42 +08004822static int alloc_apic_access_page(struct kvm *kvm)
4823{
Xiao Guangrong44841412012-09-07 14:14:20 +08004824 struct page *page;
Sheng Yangf78e0e22007-10-29 09:40:42 +08004825 int r = 0;
4826
Marcelo Tosatti79fac952009-12-23 14:35:26 -02004827 mutex_lock(&kvm->slots_lock);
Tang Chenc24ae0d2014-09-24 15:57:58 +08004828 if (kvm->arch.apic_access_page_done)
Sheng Yangf78e0e22007-10-29 09:40:42 +08004829 goto out;
Paolo Bonzini1d8007b2015-10-12 13:38:32 +02004830 r = __x86_set_memory_region(kvm, APIC_ACCESS_PAGE_PRIVATE_MEMSLOT,
4831 APIC_DEFAULT_PHYS_BASE, PAGE_SIZE);
Sheng Yangf78e0e22007-10-29 09:40:42 +08004832 if (r)
4833 goto out;
Izik Eidus72dc67a2008-02-10 18:04:15 +02004834
Tang Chen73a6d942014-09-11 13:38:00 +08004835 page = gfn_to_page(kvm, APIC_DEFAULT_PHYS_BASE >> PAGE_SHIFT);
Xiao Guangrong44841412012-09-07 14:14:20 +08004836 if (is_error_page(page)) {
4837 r = -EFAULT;
4838 goto out;
4839 }
4840
Tang Chenc24ae0d2014-09-24 15:57:58 +08004841 /*
4842 * Do not pin the page in memory, so that memory hot-unplug
4843 * is able to migrate it.
4844 */
4845 put_page(page);
4846 kvm->arch.apic_access_page_done = true;
Sheng Yangf78e0e22007-10-29 09:40:42 +08004847out:
Marcelo Tosatti79fac952009-12-23 14:35:26 -02004848 mutex_unlock(&kvm->slots_lock);
Sheng Yangf78e0e22007-10-29 09:40:42 +08004849 return r;
4850}
4851
Sheng Yangb7ebfb02008-04-25 21:44:52 +08004852static int alloc_identity_pagetable(struct kvm *kvm)
4853{
Tang Chena255d472014-09-16 18:41:58 +08004854 /* Called with kvm->slots_lock held. */
4855
Sheng Yangb7ebfb02008-04-25 21:44:52 +08004856 int r = 0;
4857
Tang Chena255d472014-09-16 18:41:58 +08004858 BUG_ON(kvm->arch.ept_identity_pagetable_done);
4859
Paolo Bonzini1d8007b2015-10-12 13:38:32 +02004860 r = __x86_set_memory_region(kvm, IDENTITY_PAGETABLE_PRIVATE_MEMSLOT,
4861 kvm->arch.ept_identity_map_addr, PAGE_SIZE);
Sheng Yangb7ebfb02008-04-25 21:44:52 +08004862
Sheng Yangb7ebfb02008-04-25 21:44:52 +08004863 return r;
4864}
4865
Wanpeng Li991e7a02015-09-16 17:30:05 +08004866static int allocate_vpid(void)
Sheng Yang2384d2b2008-01-17 15:14:33 +08004867{
4868 int vpid;
4869
Avi Kivity919818a2009-03-23 18:01:29 +02004870 if (!enable_vpid)
Wanpeng Li991e7a02015-09-16 17:30:05 +08004871 return 0;
Sheng Yang2384d2b2008-01-17 15:14:33 +08004872 spin_lock(&vmx_vpid_lock);
4873 vpid = find_first_zero_bit(vmx_vpid_bitmap, VMX_NR_VPIDS);
Wanpeng Li991e7a02015-09-16 17:30:05 +08004874 if (vpid < VMX_NR_VPIDS)
Sheng Yang2384d2b2008-01-17 15:14:33 +08004875 __set_bit(vpid, vmx_vpid_bitmap);
Wanpeng Li991e7a02015-09-16 17:30:05 +08004876 else
4877 vpid = 0;
Sheng Yang2384d2b2008-01-17 15:14:33 +08004878 spin_unlock(&vmx_vpid_lock);
Wanpeng Li991e7a02015-09-16 17:30:05 +08004879 return vpid;
Sheng Yang2384d2b2008-01-17 15:14:33 +08004880}
4881
Wanpeng Li991e7a02015-09-16 17:30:05 +08004882static void free_vpid(int vpid)
Lai Jiangshancdbecfc2010-04-17 16:41:47 +08004883{
Wanpeng Li991e7a02015-09-16 17:30:05 +08004884 if (!enable_vpid || vpid == 0)
Lai Jiangshancdbecfc2010-04-17 16:41:47 +08004885 return;
4886 spin_lock(&vmx_vpid_lock);
Wanpeng Li991e7a02015-09-16 17:30:05 +08004887 __clear_bit(vpid, vmx_vpid_bitmap);
Lai Jiangshancdbecfc2010-04-17 16:41:47 +08004888 spin_unlock(&vmx_vpid_lock);
4889}
4890
Yang Zhang8d146952013-01-25 10:18:50 +08004891#define MSR_TYPE_R 1
4892#define MSR_TYPE_W 2
4893static void __vmx_disable_intercept_for_msr(unsigned long *msr_bitmap,
4894 u32 msr, int type)
Sheng Yang25c5f222008-03-28 13:18:56 +08004895{
Avi Kivity3e7c73e2009-02-24 21:46:19 +02004896 int f = sizeof(unsigned long);
Sheng Yang25c5f222008-03-28 13:18:56 +08004897
4898 if (!cpu_has_vmx_msr_bitmap())
4899 return;
4900
4901 /*
4902 * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
4903 * have the write-low and read-high bitmap offsets the wrong way round.
4904 * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
4905 */
Sheng Yang25c5f222008-03-28 13:18:56 +08004906 if (msr <= 0x1fff) {
Yang Zhang8d146952013-01-25 10:18:50 +08004907 if (type & MSR_TYPE_R)
4908 /* read-low */
4909 __clear_bit(msr, msr_bitmap + 0x000 / f);
4910
4911 if (type & MSR_TYPE_W)
4912 /* write-low */
4913 __clear_bit(msr, msr_bitmap + 0x800 / f);
4914
Sheng Yang25c5f222008-03-28 13:18:56 +08004915 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
4916 msr &= 0x1fff;
Yang Zhang8d146952013-01-25 10:18:50 +08004917 if (type & MSR_TYPE_R)
4918 /* read-high */
4919 __clear_bit(msr, msr_bitmap + 0x400 / f);
4920
4921 if (type & MSR_TYPE_W)
4922 /* write-high */
4923 __clear_bit(msr, msr_bitmap + 0xc00 / f);
4924
4925 }
4926}
4927
Wincy Vanf2b93282015-02-03 23:56:03 +08004928/*
4929 * If a msr is allowed by L0, we should check whether it is allowed by L1.
4930 * The corresponding bit will be cleared unless both of L0 and L1 allow it.
4931 */
4932static void nested_vmx_disable_intercept_for_msr(unsigned long *msr_bitmap_l1,
4933 unsigned long *msr_bitmap_nested,
4934 u32 msr, int type)
4935{
4936 int f = sizeof(unsigned long);
4937
4938 if (!cpu_has_vmx_msr_bitmap()) {
4939 WARN_ON(1);
4940 return;
4941 }
4942
4943 /*
4944 * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
4945 * have the write-low and read-high bitmap offsets the wrong way round.
4946 * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
4947 */
4948 if (msr <= 0x1fff) {
4949 if (type & MSR_TYPE_R &&
4950 !test_bit(msr, msr_bitmap_l1 + 0x000 / f))
4951 /* read-low */
4952 __clear_bit(msr, msr_bitmap_nested + 0x000 / f);
4953
4954 if (type & MSR_TYPE_W &&
4955 !test_bit(msr, msr_bitmap_l1 + 0x800 / f))
4956 /* write-low */
4957 __clear_bit(msr, msr_bitmap_nested + 0x800 / f);
4958
4959 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
4960 msr &= 0x1fff;
4961 if (type & MSR_TYPE_R &&
4962 !test_bit(msr, msr_bitmap_l1 + 0x400 / f))
4963 /* read-high */
4964 __clear_bit(msr, msr_bitmap_nested + 0x400 / f);
4965
4966 if (type & MSR_TYPE_W &&
4967 !test_bit(msr, msr_bitmap_l1 + 0xc00 / f))
4968 /* write-high */
4969 __clear_bit(msr, msr_bitmap_nested + 0xc00 / f);
4970
4971 }
4972}
4973
Avi Kivity58972972009-02-24 22:26:47 +02004974static void vmx_disable_intercept_for_msr(u32 msr, bool longmode_only)
4975{
4976 if (!longmode_only)
Yang Zhang8d146952013-01-25 10:18:50 +08004977 __vmx_disable_intercept_for_msr(vmx_msr_bitmap_legacy,
4978 msr, MSR_TYPE_R | MSR_TYPE_W);
4979 __vmx_disable_intercept_for_msr(vmx_msr_bitmap_longmode,
4980 msr, MSR_TYPE_R | MSR_TYPE_W);
4981}
4982
Radim Krčmář2e69f862016-09-29 22:41:32 +02004983static void vmx_disable_intercept_msr_x2apic(u32 msr, int type, bool apicv_active)
Yang Zhang8d146952013-01-25 10:18:50 +08004984{
Wanpeng Lif6e90f92016-09-22 07:43:25 +08004985 if (apicv_active) {
Wanpeng Lic63e4562016-09-23 19:17:16 +08004986 __vmx_disable_intercept_for_msr(vmx_msr_bitmap_legacy_x2apic_apicv,
Radim Krčmář2e69f862016-09-29 22:41:32 +02004987 msr, type);
Wanpeng Lic63e4562016-09-23 19:17:16 +08004988 __vmx_disable_intercept_for_msr(vmx_msr_bitmap_longmode_x2apic_apicv,
Radim Krčmář2e69f862016-09-29 22:41:32 +02004989 msr, type);
Wanpeng Lif6e90f92016-09-22 07:43:25 +08004990 } else {
Wanpeng Lif6e90f92016-09-22 07:43:25 +08004991 __vmx_disable_intercept_for_msr(vmx_msr_bitmap_legacy_x2apic,
Radim Krčmář2e69f862016-09-29 22:41:32 +02004992 msr, type);
Wanpeng Lif6e90f92016-09-22 07:43:25 +08004993 __vmx_disable_intercept_for_msr(vmx_msr_bitmap_longmode_x2apic,
Radim Krčmář2e69f862016-09-29 22:41:32 +02004994 msr, type);
Wanpeng Lif6e90f92016-09-22 07:43:25 +08004995 }
Avi Kivity58972972009-02-24 22:26:47 +02004996}
4997
Andrey Smetanind62caab2015-11-10 15:36:33 +03004998static bool vmx_get_enable_apicv(void)
Paolo Bonzinid50ab6c2015-07-29 11:49:59 +02004999{
Andrey Smetanind62caab2015-11-10 15:36:33 +03005000 return enable_apicv;
Paolo Bonzinid50ab6c2015-07-29 11:49:59 +02005001}
5002
David Matlackc9f04402017-08-01 14:00:40 -07005003static void nested_mark_vmcs12_pages_dirty(struct kvm_vcpu *vcpu)
5004{
5005 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
5006 gfn_t gfn;
5007
5008 /*
5009 * Don't need to mark the APIC access page dirty; it is never
5010 * written to by the CPU during APIC virtualization.
5011 */
5012
5013 if (nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW)) {
5014 gfn = vmcs12->virtual_apic_page_addr >> PAGE_SHIFT;
5015 kvm_vcpu_mark_page_dirty(vcpu, gfn);
5016 }
5017
5018 if (nested_cpu_has_posted_intr(vmcs12)) {
5019 gfn = vmcs12->posted_intr_desc_addr >> PAGE_SHIFT;
5020 kvm_vcpu_mark_page_dirty(vcpu, gfn);
5021 }
5022}
5023
5024
David Hildenbrand6342c502017-01-25 11:58:58 +01005025static void vmx_complete_nested_posted_interrupt(struct kvm_vcpu *vcpu)
Wincy Van705699a2015-02-03 23:58:17 +08005026{
5027 struct vcpu_vmx *vmx = to_vmx(vcpu);
5028 int max_irr;
5029 void *vapic_page;
5030 u16 status;
5031
David Matlackc9f04402017-08-01 14:00:40 -07005032 if (!vmx->nested.pi_desc || !vmx->nested.pi_pending)
5033 return;
Wincy Van705699a2015-02-03 23:58:17 +08005034
David Matlackc9f04402017-08-01 14:00:40 -07005035 vmx->nested.pi_pending = false;
5036 if (!pi_test_and_clear_on(vmx->nested.pi_desc))
5037 return;
Wincy Van705699a2015-02-03 23:58:17 +08005038
David Matlackc9f04402017-08-01 14:00:40 -07005039 max_irr = find_last_bit((unsigned long *)vmx->nested.pi_desc->pir, 256);
5040 if (max_irr != 256) {
Wincy Van705699a2015-02-03 23:58:17 +08005041 vapic_page = kmap(vmx->nested.virtual_apic_page);
Wincy Van705699a2015-02-03 23:58:17 +08005042 __kvm_apic_update_irr(vmx->nested.pi_desc->pir, vapic_page);
5043 kunmap(vmx->nested.virtual_apic_page);
5044
5045 status = vmcs_read16(GUEST_INTR_STATUS);
5046 if ((u8)max_irr > ((u8)status & 0xff)) {
5047 status &= ~0xff;
5048 status |= (u8)max_irr;
5049 vmcs_write16(GUEST_INTR_STATUS, status);
5050 }
5051 }
David Matlackc9f04402017-08-01 14:00:40 -07005052
5053 nested_mark_vmcs12_pages_dirty(vcpu);
Wincy Van705699a2015-02-03 23:58:17 +08005054}
5055
Wincy Van06a55242017-04-28 13:13:59 +08005056static inline bool kvm_vcpu_trigger_posted_interrupt(struct kvm_vcpu *vcpu,
5057 bool nested)
Radim Krčmář21bc8dc2015-02-16 15:36:33 +01005058{
5059#ifdef CONFIG_SMP
Wincy Van06a55242017-04-28 13:13:59 +08005060 int pi_vec = nested ? POSTED_INTR_NESTED_VECTOR : POSTED_INTR_VECTOR;
5061
Radim Krčmář21bc8dc2015-02-16 15:36:33 +01005062 if (vcpu->mode == IN_GUEST_MODE) {
Feng Wu28b835d2015-09-18 22:29:54 +08005063 struct vcpu_vmx *vmx = to_vmx(vcpu);
5064
5065 /*
5066 * Currently, we don't support urgent interrupt,
5067 * all interrupts are recognized as non-urgent
5068 * interrupt, so we cannot post interrupts when
5069 * 'SN' is set.
5070 *
5071 * If the vcpu is in guest mode, it means it is
5072 * running instead of being scheduled out and
5073 * waiting in the run queue, and that's the only
5074 * case when 'SN' is set currently, warning if
5075 * 'SN' is set.
5076 */
5077 WARN_ON_ONCE(pi_test_sn(&vmx->pi_desc));
5078
Wincy Van06a55242017-04-28 13:13:59 +08005079 apic->send_IPI_mask(get_cpu_mask(vcpu->cpu), pi_vec);
Radim Krčmář21bc8dc2015-02-16 15:36:33 +01005080 return true;
5081 }
5082#endif
5083 return false;
5084}
5085
Wincy Van705699a2015-02-03 23:58:17 +08005086static int vmx_deliver_nested_posted_interrupt(struct kvm_vcpu *vcpu,
5087 int vector)
5088{
5089 struct vcpu_vmx *vmx = to_vmx(vcpu);
5090
5091 if (is_guest_mode(vcpu) &&
5092 vector == vmx->nested.posted_intr_nv) {
5093 /* the PIR and ON have been set by L1. */
Wincy Van06a55242017-04-28 13:13:59 +08005094 kvm_vcpu_trigger_posted_interrupt(vcpu, true);
Wincy Van705699a2015-02-03 23:58:17 +08005095 /*
5096 * If a posted intr is not recognized by hardware,
5097 * we will accomplish it in the next vmentry.
5098 */
5099 vmx->nested.pi_pending = true;
5100 kvm_make_request(KVM_REQ_EVENT, vcpu);
5101 return 0;
5102 }
5103 return -1;
5104}
Avi Kivity6aa8b732006-12-10 02:21:36 -08005105/*
Yang Zhanga20ed542013-04-11 19:25:15 +08005106 * Send interrupt to vcpu via posted interrupt way.
5107 * 1. If target vcpu is running(non-root mode), send posted interrupt
5108 * notification to vcpu and hardware will sync PIR to vIRR atomically.
5109 * 2. If target vcpu isn't running(root mode), kick it to pick up the
5110 * interrupt from PIR in next vmentry.
5111 */
5112static void vmx_deliver_posted_interrupt(struct kvm_vcpu *vcpu, int vector)
5113{
5114 struct vcpu_vmx *vmx = to_vmx(vcpu);
5115 int r;
5116
Wincy Van705699a2015-02-03 23:58:17 +08005117 r = vmx_deliver_nested_posted_interrupt(vcpu, vector);
5118 if (!r)
5119 return;
5120
Yang Zhanga20ed542013-04-11 19:25:15 +08005121 if (pi_test_and_set_pir(vector, &vmx->pi_desc))
5122 return;
5123
Paolo Bonzinib95234c2016-12-19 13:57:33 +01005124 /* If a previous notification has sent the IPI, nothing to do. */
5125 if (pi_test_and_set_on(&vmx->pi_desc))
5126 return;
5127
Wincy Van06a55242017-04-28 13:13:59 +08005128 if (!kvm_vcpu_trigger_posted_interrupt(vcpu, false))
Yang Zhanga20ed542013-04-11 19:25:15 +08005129 kvm_vcpu_kick(vcpu);
5130}
5131
Avi Kivity6aa8b732006-12-10 02:21:36 -08005132/*
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005133 * Set up the vmcs's constant host-state fields, i.e., host-state fields that
5134 * will not change in the lifetime of the guest.
5135 * Note that host-state that does change is set elsewhere. E.g., host-state
5136 * that is set differently for each CPU is set in vmx_vcpu_load(), not here.
5137 */
Yang Zhanga547c6d2013-04-11 19:25:10 +08005138static void vmx_set_constant_host_state(struct vcpu_vmx *vmx)
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005139{
5140 u32 low32, high32;
5141 unsigned long tmpl;
5142 struct desc_ptr dt;
Andy Lutomirskid6e41f12017-05-28 10:00:17 -07005143 unsigned long cr0, cr3, cr4;
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005144
Andy Lutomirski04ac88a2016-10-31 15:18:45 -07005145 cr0 = read_cr0();
5146 WARN_ON(cr0 & X86_CR0_TS);
5147 vmcs_writel(HOST_CR0, cr0); /* 22.2.3 */
Andy Lutomirskid6e41f12017-05-28 10:00:17 -07005148
5149 /*
5150 * Save the most likely value for this task's CR3 in the VMCS.
5151 * We can't use __get_current_cr3_fast() because we're not atomic.
5152 */
Andy Lutomirski6c690ee2017-06-12 10:26:14 -07005153 cr3 = __read_cr3();
Andy Lutomirskid6e41f12017-05-28 10:00:17 -07005154 vmcs_writel(HOST_CR3, cr3); /* 22.2.3 FIXME: shadow tables */
5155 vmx->host_state.vmcs_host_cr3 = cr3;
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005156
Andy Lutomirskid974baa2014-10-08 09:02:13 -07005157 /* Save the most likely value for this task's CR4 in the VMCS. */
Andy Lutomirski1e02ce42014-10-24 15:58:08 -07005158 cr4 = cr4_read_shadow();
Andy Lutomirskid974baa2014-10-08 09:02:13 -07005159 vmcs_writel(HOST_CR4, cr4); /* 22.2.3, 22.2.5 */
5160 vmx->host_state.vmcs_host_cr4 = cr4;
5161
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005162 vmcs_write16(HOST_CS_SELECTOR, __KERNEL_CS); /* 22.2.4 */
Avi Kivityb2da15a2012-05-13 19:53:24 +03005163#ifdef CONFIG_X86_64
5164 /*
5165 * Load null selectors, so we can avoid reloading them in
5166 * __vmx_load_host_state(), in case userspace uses the null selectors
5167 * too (the expected case).
5168 */
5169 vmcs_write16(HOST_DS_SELECTOR, 0);
5170 vmcs_write16(HOST_ES_SELECTOR, 0);
5171#else
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005172 vmcs_write16(HOST_DS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
5173 vmcs_write16(HOST_ES_SELECTOR, __KERNEL_DS); /* 22.2.4 */
Avi Kivityb2da15a2012-05-13 19:53:24 +03005174#endif
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005175 vmcs_write16(HOST_SS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
5176 vmcs_write16(HOST_TR_SELECTOR, GDT_ENTRY_TSS*8); /* 22.2.4 */
5177
5178 native_store_idt(&dt);
5179 vmcs_writel(HOST_IDTR_BASE, dt.address); /* 22.2.4 */
Yang Zhanga547c6d2013-04-11 19:25:10 +08005180 vmx->host_idt_base = dt.address;
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005181
Avi Kivity83287ea422012-09-16 15:10:57 +03005182 vmcs_writel(HOST_RIP, vmx_return); /* 22.2.5 */
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005183
5184 rdmsr(MSR_IA32_SYSENTER_CS, low32, high32);
5185 vmcs_write32(HOST_IA32_SYSENTER_CS, low32);
5186 rdmsrl(MSR_IA32_SYSENTER_EIP, tmpl);
5187 vmcs_writel(HOST_IA32_SYSENTER_EIP, tmpl); /* 22.2.3 */
5188
5189 if (vmcs_config.vmexit_ctrl & VM_EXIT_LOAD_IA32_PAT) {
5190 rdmsr(MSR_IA32_CR_PAT, low32, high32);
5191 vmcs_write64(HOST_IA32_PAT, low32 | ((u64) high32 << 32));
5192 }
5193}
5194
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005195static void set_cr4_guest_host_mask(struct vcpu_vmx *vmx)
5196{
5197 vmx->vcpu.arch.cr4_guest_owned_bits = KVM_CR4_GUEST_OWNED_BITS;
5198 if (enable_ept)
5199 vmx->vcpu.arch.cr4_guest_owned_bits |= X86_CR4_PGE;
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03005200 if (is_guest_mode(&vmx->vcpu))
5201 vmx->vcpu.arch.cr4_guest_owned_bits &=
5202 ~get_vmcs12(&vmx->vcpu)->cr4_guest_host_mask;
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005203 vmcs_writel(CR4_GUEST_HOST_MASK, ~vmx->vcpu.arch.cr4_guest_owned_bits);
5204}
5205
Yang Zhang01e439b2013-04-11 19:25:12 +08005206static u32 vmx_pin_based_exec_ctrl(struct vcpu_vmx *vmx)
5207{
5208 u32 pin_based_exec_ctrl = vmcs_config.pin_based_exec_ctrl;
5209
Andrey Smetanind62caab2015-11-10 15:36:33 +03005210 if (!kvm_vcpu_apicv_active(&vmx->vcpu))
Yang Zhang01e439b2013-04-11 19:25:12 +08005211 pin_based_exec_ctrl &= ~PIN_BASED_POSTED_INTR;
Yunhong Jiang64672c92016-06-13 14:19:59 -07005212 /* Enable the preemption timer dynamically */
5213 pin_based_exec_ctrl &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
Yang Zhang01e439b2013-04-11 19:25:12 +08005214 return pin_based_exec_ctrl;
5215}
5216
Andrey Smetanind62caab2015-11-10 15:36:33 +03005217static void vmx_refresh_apicv_exec_ctrl(struct kvm_vcpu *vcpu)
5218{
5219 struct vcpu_vmx *vmx = to_vmx(vcpu);
5220
5221 vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, vmx_pin_based_exec_ctrl(vmx));
Roman Kagan3ce424e2016-05-18 17:48:20 +03005222 if (cpu_has_secondary_exec_ctrls()) {
5223 if (kvm_vcpu_apicv_active(vcpu))
5224 vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL,
5225 SECONDARY_EXEC_APIC_REGISTER_VIRT |
5226 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
5227 else
5228 vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL,
5229 SECONDARY_EXEC_APIC_REGISTER_VIRT |
5230 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
5231 }
5232
5233 if (cpu_has_vmx_msr_bitmap())
5234 vmx_set_msr_bitmap(vcpu);
Andrey Smetanind62caab2015-11-10 15:36:33 +03005235}
5236
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005237static u32 vmx_exec_control(struct vcpu_vmx *vmx)
5238{
5239 u32 exec_control = vmcs_config.cpu_based_exec_ctrl;
Paolo Bonzinid16c2932014-02-21 10:36:37 +01005240
5241 if (vmx->vcpu.arch.switch_db_regs & KVM_DEBUGREG_WONT_EXIT)
5242 exec_control &= ~CPU_BASED_MOV_DR_EXITING;
5243
Paolo Bonzini35754c92015-07-29 12:05:37 +02005244 if (!cpu_need_tpr_shadow(&vmx->vcpu)) {
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005245 exec_control &= ~CPU_BASED_TPR_SHADOW;
5246#ifdef CONFIG_X86_64
5247 exec_control |= CPU_BASED_CR8_STORE_EXITING |
5248 CPU_BASED_CR8_LOAD_EXITING;
5249#endif
5250 }
5251 if (!enable_ept)
5252 exec_control |= CPU_BASED_CR3_STORE_EXITING |
5253 CPU_BASED_CR3_LOAD_EXITING |
5254 CPU_BASED_INVLPG_EXITING;
5255 return exec_control;
5256}
5257
5258static u32 vmx_secondary_exec_control(struct vcpu_vmx *vmx)
5259{
5260 u32 exec_control = vmcs_config.cpu_based_2nd_exec_ctrl;
Paolo Bonzini35754c92015-07-29 12:05:37 +02005261 if (!cpu_need_virtualize_apic_accesses(&vmx->vcpu))
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005262 exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
5263 if (vmx->vpid == 0)
5264 exec_control &= ~SECONDARY_EXEC_ENABLE_VPID;
5265 if (!enable_ept) {
5266 exec_control &= ~SECONDARY_EXEC_ENABLE_EPT;
5267 enable_unrestricted_guest = 0;
Mao, Junjiead756a12012-07-02 01:18:48 +00005268 /* Enable INVPCID for non-ept guests may cause performance regression. */
5269 exec_control &= ~SECONDARY_EXEC_ENABLE_INVPCID;
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005270 }
5271 if (!enable_unrestricted_guest)
5272 exec_control &= ~SECONDARY_EXEC_UNRESTRICTED_GUEST;
5273 if (!ple_gap)
5274 exec_control &= ~SECONDARY_EXEC_PAUSE_LOOP_EXITING;
Andrey Smetanind62caab2015-11-10 15:36:33 +03005275 if (!kvm_vcpu_apicv_active(&vmx->vcpu))
Yang Zhangc7c9c562013-01-25 10:18:51 +08005276 exec_control &= ~(SECONDARY_EXEC_APIC_REGISTER_VIRT |
5277 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
Yang Zhang8d146952013-01-25 10:18:50 +08005278 exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
Abel Gordonabc4fc52013-04-18 14:35:25 +03005279 /* SECONDARY_EXEC_SHADOW_VMCS is enabled when L1 executes VMPTRLD
5280 (handle_vmptrld).
5281 We can NOT enable shadow_vmcs here because we don't have yet
5282 a current VMCS12
5283 */
5284 exec_control &= ~SECONDARY_EXEC_SHADOW_VMCS;
Kai Huanga3eaa862015-11-04 13:46:05 +08005285
5286 if (!enable_pml)
5287 exec_control &= ~SECONDARY_EXEC_ENABLE_PML;
Kai Huang843e4332015-01-28 10:54:28 +08005288
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005289 return exec_control;
5290}
5291
Xiao Guangrongce88dec2011-07-12 03:33:44 +08005292static void ept_set_mmio_spte_mask(void)
5293{
5294 /*
5295 * EPT Misconfigurations can be generated if the value of bits 2:0
5296 * of an EPT paging-structure entry is 110b (write/execute).
Xiao Guangrongce88dec2011-07-12 03:33:44 +08005297 */
Peter Feinerdcdca5f2017-06-30 17:26:30 -07005298 kvm_mmu_set_mmio_spte_mask(VMX_EPT_RWX_MASK,
5299 VMX_EPT_MISCONFIG_WX_VALUE);
Xiao Guangrongce88dec2011-07-12 03:33:44 +08005300}
5301
Wanpeng Lif53cd632014-12-02 19:14:58 +08005302#define VMX_XSS_EXIT_BITMAP 0
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005303/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08005304 * Sets up the vmcs for emulated real mode.
5305 */
Rusty Russell8b9cf982007-07-30 16:31:43 +10005306static int vmx_vcpu_setup(struct vcpu_vmx *vmx)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005307{
Jan Kiszka2e4ce7f2011-06-01 12:57:30 +02005308#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08005309 unsigned long a;
Jan Kiszka2e4ce7f2011-06-01 12:57:30 +02005310#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -08005311 int i;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005312
Avi Kivity6aa8b732006-12-10 02:21:36 -08005313 /* I/O */
Avi Kivity3e7c73e2009-02-24 21:46:19 +02005314 vmcs_write64(IO_BITMAP_A, __pa(vmx_io_bitmap_a));
5315 vmcs_write64(IO_BITMAP_B, __pa(vmx_io_bitmap_b));
Avi Kivity6aa8b732006-12-10 02:21:36 -08005316
Abel Gordon4607c2d2013-04-18 14:35:55 +03005317 if (enable_shadow_vmcs) {
5318 vmcs_write64(VMREAD_BITMAP, __pa(vmx_vmread_bitmap));
5319 vmcs_write64(VMWRITE_BITMAP, __pa(vmx_vmwrite_bitmap));
5320 }
Sheng Yang25c5f222008-03-28 13:18:56 +08005321 if (cpu_has_vmx_msr_bitmap())
Avi Kivity58972972009-02-24 22:26:47 +02005322 vmcs_write64(MSR_BITMAP, __pa(vmx_msr_bitmap_legacy));
Sheng Yang25c5f222008-03-28 13:18:56 +08005323
Avi Kivity6aa8b732006-12-10 02:21:36 -08005324 vmcs_write64(VMCS_LINK_POINTER, -1ull); /* 22.3.1.5 */
5325
Avi Kivity6aa8b732006-12-10 02:21:36 -08005326 /* Control */
Yang Zhang01e439b2013-04-11 19:25:12 +08005327 vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, vmx_pin_based_exec_ctrl(vmx));
Yunhong Jiang64672c92016-06-13 14:19:59 -07005328 vmx->hv_deadline_tsc = -1;
Yang, Sheng6e5d8652007-09-12 18:03:11 +08005329
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005330 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, vmx_exec_control(vmx));
Avi Kivity6aa8b732006-12-10 02:21:36 -08005331
Dan Williamsdfa169b2016-06-02 11:17:24 -07005332 if (cpu_has_secondary_exec_ctrls()) {
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005333 vmcs_write32(SECONDARY_VM_EXEC_CONTROL,
5334 vmx_secondary_exec_control(vmx));
Dan Williamsdfa169b2016-06-02 11:17:24 -07005335 }
Sheng Yangf78e0e22007-10-29 09:40:42 +08005336
Andrey Smetanind62caab2015-11-10 15:36:33 +03005337 if (kvm_vcpu_apicv_active(&vmx->vcpu)) {
Yang Zhangc7c9c562013-01-25 10:18:51 +08005338 vmcs_write64(EOI_EXIT_BITMAP0, 0);
5339 vmcs_write64(EOI_EXIT_BITMAP1, 0);
5340 vmcs_write64(EOI_EXIT_BITMAP2, 0);
5341 vmcs_write64(EOI_EXIT_BITMAP3, 0);
5342
5343 vmcs_write16(GUEST_INTR_STATUS, 0);
Yang Zhang01e439b2013-04-11 19:25:12 +08005344
Li RongQing0bcf2612015-12-03 13:29:34 +08005345 vmcs_write16(POSTED_INTR_NV, POSTED_INTR_VECTOR);
Yang Zhang01e439b2013-04-11 19:25:12 +08005346 vmcs_write64(POSTED_INTR_DESC_ADDR, __pa((&vmx->pi_desc)));
Yang Zhangc7c9c562013-01-25 10:18:51 +08005347 }
5348
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08005349 if (ple_gap) {
5350 vmcs_write32(PLE_GAP, ple_gap);
Radim Krčmářa7653ec2014-08-21 18:08:07 +02005351 vmx->ple_window = ple_window;
5352 vmx->ple_window_dirty = true;
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08005353 }
5354
Xiao Guangrongc3707952011-07-12 03:28:04 +08005355 vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK, 0);
5356 vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005357 vmcs_write32(CR3_TARGET_COUNT, 0); /* 22.2.1 */
5358
Avi Kivity9581d442010-10-19 16:46:55 +02005359 vmcs_write16(HOST_FS_SELECTOR, 0); /* 22.2.4 */
5360 vmcs_write16(HOST_GS_SELECTOR, 0); /* 22.2.4 */
Yang Zhanga547c6d2013-04-11 19:25:10 +08005361 vmx_set_constant_host_state(vmx);
Avi Kivity05b3e0c2006-12-13 00:33:45 -08005362#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08005363 rdmsrl(MSR_FS_BASE, a);
5364 vmcs_writel(HOST_FS_BASE, a); /* 22.2.4 */
5365 rdmsrl(MSR_GS_BASE, a);
5366 vmcs_writel(HOST_GS_BASE, a); /* 22.2.4 */
5367#else
5368 vmcs_writel(HOST_FS_BASE, 0); /* 22.2.4 */
5369 vmcs_writel(HOST_GS_BASE, 0); /* 22.2.4 */
5370#endif
5371
Bandan Das2a499e42017-08-03 15:54:41 -04005372 if (cpu_has_vmx_vmfunc())
5373 vmcs_write64(VM_FUNCTION_CONTROL, 0);
5374
Eddie Dong2cc51562007-05-21 07:28:09 +03005375 vmcs_write32(VM_EXIT_MSR_STORE_COUNT, 0);
5376 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, 0);
Avi Kivity61d2ef22010-04-28 16:40:38 +03005377 vmcs_write64(VM_EXIT_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.host));
Eddie Dong2cc51562007-05-21 07:28:09 +03005378 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, 0);
Avi Kivity61d2ef22010-04-28 16:40:38 +03005379 vmcs_write64(VM_ENTRY_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.guest));
Avi Kivity6aa8b732006-12-10 02:21:36 -08005380
Radim Krčmář74545702015-04-27 15:11:25 +02005381 if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT)
5382 vmcs_write64(GUEST_IA32_PAT, vmx->vcpu.arch.pat);
Sheng Yang468d4722008-10-09 16:01:55 +08005383
Paolo Bonzini03916db2014-07-24 14:21:57 +02005384 for (i = 0; i < ARRAY_SIZE(vmx_msr_index); ++i) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08005385 u32 index = vmx_msr_index[i];
5386 u32 data_low, data_high;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04005387 int j = vmx->nmsrs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005388
5389 if (rdmsr_safe(index, &data_low, &data_high) < 0)
5390 continue;
Avi Kivity432bd6c2007-01-31 23:48:13 -08005391 if (wrmsr_safe(index, data_low, data_high) < 0)
5392 continue;
Avi Kivity26bb0982009-09-07 11:14:12 +03005393 vmx->guest_msrs[j].index = i;
5394 vmx->guest_msrs[j].data = 0;
Avi Kivityd5696722009-12-02 12:28:47 +02005395 vmx->guest_msrs[j].mask = -1ull;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04005396 ++vmx->nmsrs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005397 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08005398
Gleb Natapov2961e8762013-11-25 15:37:13 +02005399
5400 vm_exit_controls_init(vmx, vmcs_config.vmexit_ctrl);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005401
5402 /* 22.2.1, 20.8.1 */
Gleb Natapov2961e8762013-11-25 15:37:13 +02005403 vm_entry_controls_init(vmx, vmcs_config.vmentry_ctrl);
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03005404
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -08005405 vmx->vcpu.arch.cr0_guest_owned_bits = X86_CR0_TS;
5406 vmcs_writel(CR0_GUEST_HOST_MASK, ~X86_CR0_TS);
5407
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005408 set_cr4_guest_host_mask(vmx);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005409
Wanpeng Lif53cd632014-12-02 19:14:58 +08005410 if (vmx_xsaves_supported())
5411 vmcs_write64(XSS_EXIT_BITMAP, VMX_XSS_EXIT_BITMAP);
5412
Peter Feiner4e595162016-07-07 14:49:58 -07005413 if (enable_pml) {
5414 ASSERT(vmx->pml_pg);
5415 vmcs_write64(PML_ADDRESS, page_to_phys(vmx->pml_pg));
5416 vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1);
5417 }
5418
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005419 return 0;
5420}
5421
Nadav Amitd28bc9d2015-04-13 14:34:08 +03005422static void vmx_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event)
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005423{
5424 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jan Kiszka58cb6282014-01-24 16:48:44 +01005425 struct msr_data apic_base_msr;
Nadav Amitd28bc9d2015-04-13 14:34:08 +03005426 u64 cr0;
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005427
Avi Kivity7ffd92c2009-06-09 14:10:45 +03005428 vmx->rmode.vm86_active = 0;
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005429
Zhang Xiantaoad312c72007-12-13 23:50:52 +08005430 vmx->vcpu.arch.regs[VCPU_REGS_RDX] = get_rdx_init_val();
Nadav Amitd28bc9d2015-04-13 14:34:08 +03005431 kvm_set_cr8(vcpu, 0);
5432
5433 if (!init_event) {
5434 apic_base_msr.data = APIC_DEFAULT_PHYS_BASE |
5435 MSR_IA32_APICBASE_ENABLE;
5436 if (kvm_vcpu_is_reset_bsp(vcpu))
5437 apic_base_msr.data |= MSR_IA32_APICBASE_BSP;
5438 apic_base_msr.host_initiated = true;
5439 kvm_set_apic_base(vcpu, &apic_base_msr);
5440 }
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005441
Avi Kivity2fb92db2011-04-27 19:42:18 +03005442 vmx_segment_cache_clear(vmx);
5443
Avi Kivity5706be02008-08-20 15:07:31 +03005444 seg_setup(VCPU_SREG_CS);
Jan Kiszka66450a22013-03-13 12:42:34 +01005445 vmcs_write16(GUEST_CS_SELECTOR, 0xf000);
Paolo Bonzinif3531052015-12-03 15:49:56 +01005446 vmcs_writel(GUEST_CS_BASE, 0xffff0000ul);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005447
5448 seg_setup(VCPU_SREG_DS);
5449 seg_setup(VCPU_SREG_ES);
5450 seg_setup(VCPU_SREG_FS);
5451 seg_setup(VCPU_SREG_GS);
5452 seg_setup(VCPU_SREG_SS);
5453
5454 vmcs_write16(GUEST_TR_SELECTOR, 0);
5455 vmcs_writel(GUEST_TR_BASE, 0);
5456 vmcs_write32(GUEST_TR_LIMIT, 0xffff);
5457 vmcs_write32(GUEST_TR_AR_BYTES, 0x008b);
5458
5459 vmcs_write16(GUEST_LDTR_SELECTOR, 0);
5460 vmcs_writel(GUEST_LDTR_BASE, 0);
5461 vmcs_write32(GUEST_LDTR_LIMIT, 0xffff);
5462 vmcs_write32(GUEST_LDTR_AR_BYTES, 0x00082);
5463
Nadav Amitd28bc9d2015-04-13 14:34:08 +03005464 if (!init_event) {
5465 vmcs_write32(GUEST_SYSENTER_CS, 0);
5466 vmcs_writel(GUEST_SYSENTER_ESP, 0);
5467 vmcs_writel(GUEST_SYSENTER_EIP, 0);
5468 vmcs_write64(GUEST_IA32_DEBUGCTL, 0);
5469 }
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005470
5471 vmcs_writel(GUEST_RFLAGS, 0x02);
Jan Kiszka66450a22013-03-13 12:42:34 +01005472 kvm_rip_write(vcpu, 0xfff0);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005473
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005474 vmcs_writel(GUEST_GDTR_BASE, 0);
5475 vmcs_write32(GUEST_GDTR_LIMIT, 0xffff);
5476
5477 vmcs_writel(GUEST_IDTR_BASE, 0);
5478 vmcs_write32(GUEST_IDTR_LIMIT, 0xffff);
5479
Anthony Liguori443381a2010-12-06 10:53:38 -06005480 vmcs_write32(GUEST_ACTIVITY_STATE, GUEST_ACTIVITY_ACTIVE);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005481 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, 0);
Paolo Bonzinif3531052015-12-03 15:49:56 +01005482 vmcs_writel(GUEST_PENDING_DBG_EXCEPTIONS, 0);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005483
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005484 setup_msrs(vmx);
5485
Avi Kivity6aa8b732006-12-10 02:21:36 -08005486 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0); /* 22.2.1 */
5487
Nadav Amitd28bc9d2015-04-13 14:34:08 +03005488 if (cpu_has_vmx_tpr_shadow() && !init_event) {
Sheng Yangf78e0e22007-10-29 09:40:42 +08005489 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, 0);
Paolo Bonzini35754c92015-07-29 12:05:37 +02005490 if (cpu_need_tpr_shadow(vcpu))
Sheng Yangf78e0e22007-10-29 09:40:42 +08005491 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR,
Nadav Amitd28bc9d2015-04-13 14:34:08 +03005492 __pa(vcpu->arch.apic->regs));
Sheng Yangf78e0e22007-10-29 09:40:42 +08005493 vmcs_write32(TPR_THRESHOLD, 0);
5494 }
5495
Paolo Bonzinia73896c2014-11-02 07:54:30 +01005496 kvm_make_request(KVM_REQ_APIC_PAGE_RELOAD, vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005497
Andrey Smetanind62caab2015-11-10 15:36:33 +03005498 if (kvm_vcpu_apicv_active(vcpu))
Yang Zhang01e439b2013-04-11 19:25:12 +08005499 memset(&vmx->pi_desc, 0, sizeof(struct pi_desc));
5500
Sheng Yang2384d2b2008-01-17 15:14:33 +08005501 if (vmx->vpid != 0)
5502 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid);
5503
Nadav Amitd28bc9d2015-04-13 14:34:08 +03005504 cr0 = X86_CR0_NW | X86_CR0_CD | X86_CR0_ET;
Nadav Amitd28bc9d2015-04-13 14:34:08 +03005505 vmx->vcpu.arch.cr0 = cr0;
Bruce Rogersf2463242016-04-28 14:49:21 -06005506 vmx_set_cr0(vcpu, cr0); /* enter rmode */
Nadav Amitd28bc9d2015-04-13 14:34:08 +03005507 vmx_set_cr4(vcpu, 0);
Paolo Bonzini56908912015-10-19 11:30:19 +02005508 vmx_set_efer(vcpu, 0);
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -08005509
Nadav Amitd28bc9d2015-04-13 14:34:08 +03005510 update_exception_bitmap(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005511
Wanpeng Lidd5f5342015-09-23 18:26:57 +08005512 vpid_sync_context(vmx->vpid);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005513}
5514
Nadav Har'Elb6f12502011-05-25 23:13:06 +03005515/*
5516 * In nested virtualization, check if L1 asked to exit on external interrupts.
5517 * For most existing hypervisors, this will always return true.
5518 */
5519static bool nested_exit_on_intr(struct kvm_vcpu *vcpu)
5520{
5521 return get_vmcs12(vcpu)->pin_based_vm_exec_control &
5522 PIN_BASED_EXT_INTR_MASK;
5523}
5524
Bandan Das77b0f5d2014-04-19 18:17:45 -04005525/*
5526 * In nested virtualization, check if L1 has set
5527 * VM_EXIT_ACK_INTR_ON_EXIT
5528 */
5529static bool nested_exit_intr_ack_set(struct kvm_vcpu *vcpu)
5530{
5531 return get_vmcs12(vcpu)->vm_exit_controls &
5532 VM_EXIT_ACK_INTR_ON_EXIT;
5533}
5534
Jan Kiszkaea8ceb82013-04-14 21:04:26 +02005535static bool nested_exit_on_nmi(struct kvm_vcpu *vcpu)
5536{
5537 return get_vmcs12(vcpu)->pin_based_vm_exec_control &
5538 PIN_BASED_NMI_EXITING;
5539}
5540
Jan Kiszkac9a79532014-03-07 20:03:15 +01005541static void enable_irq_window(struct kvm_vcpu *vcpu)
Jan Kiszka3b86cd92008-09-26 09:30:57 +02005542{
Paolo Bonzini47c01522016-12-19 11:44:07 +01005543 vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL,
5544 CPU_BASED_VIRTUAL_INTR_PENDING);
Jan Kiszka3b86cd92008-09-26 09:30:57 +02005545}
5546
Jan Kiszkac9a79532014-03-07 20:03:15 +01005547static void enable_nmi_window(struct kvm_vcpu *vcpu)
Jan Kiszka3b86cd92008-09-26 09:30:57 +02005548{
Paolo Bonzini2c828782017-03-27 14:37:28 +02005549 if (vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & GUEST_INTR_STATE_STI) {
Jan Kiszkac9a79532014-03-07 20:03:15 +01005550 enable_irq_window(vcpu);
5551 return;
5552 }
Jan Kiszka03b28f82013-04-29 16:46:42 +02005553
Paolo Bonzini47c01522016-12-19 11:44:07 +01005554 vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL,
5555 CPU_BASED_VIRTUAL_NMI_PENDING);
Jan Kiszka3b86cd92008-09-26 09:30:57 +02005556}
5557
Gleb Natapov66fd3f72009-05-11 13:35:50 +03005558static void vmx_inject_irq(struct kvm_vcpu *vcpu)
Eddie Dong85f455f2007-07-06 12:20:49 +03005559{
Avi Kivity9c8cba32007-11-22 11:42:59 +02005560 struct vcpu_vmx *vmx = to_vmx(vcpu);
Gleb Natapov66fd3f72009-05-11 13:35:50 +03005561 uint32_t intr;
5562 int irq = vcpu->arch.interrupt.nr;
Avi Kivity9c8cba32007-11-22 11:42:59 +02005563
Marcelo Tosatti229456f2009-06-17 09:22:14 -03005564 trace_kvm_inj_virq(irq);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04005565
Avi Kivityfa89a812008-09-01 15:57:51 +03005566 ++vcpu->stat.irq_injections;
Avi Kivity7ffd92c2009-06-09 14:10:45 +03005567 if (vmx->rmode.vm86_active) {
Serge E. Hallyn71f98332011-04-13 09:12:54 -05005568 int inc_eip = 0;
5569 if (vcpu->arch.interrupt.soft)
5570 inc_eip = vcpu->arch.event_exit_inst_len;
5571 if (kvm_inject_realmode_interrupt(vcpu, irq, inc_eip) != EMULATE_DONE)
Mohammed Gamala92601b2010-09-19 14:34:07 +02005572 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Eddie Dong85f455f2007-07-06 12:20:49 +03005573 return;
5574 }
Gleb Natapov66fd3f72009-05-11 13:35:50 +03005575 intr = irq | INTR_INFO_VALID_MASK;
5576 if (vcpu->arch.interrupt.soft) {
5577 intr |= INTR_TYPE_SOFT_INTR;
5578 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
5579 vmx->vcpu.arch.event_exit_inst_len);
5580 } else
5581 intr |= INTR_TYPE_EXT_INTR;
5582 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr);
Eddie Dong85f455f2007-07-06 12:20:49 +03005583}
5584
Sheng Yangf08864b2008-05-15 18:23:25 +08005585static void vmx_inject_nmi(struct kvm_vcpu *vcpu)
5586{
Jan Kiszka66a5a342008-09-26 09:30:51 +02005587 struct vcpu_vmx *vmx = to_vmx(vcpu);
5588
Paolo Bonzini4c4a6f72017-07-14 13:36:11 +02005589 ++vcpu->stat.nmi_injections;
5590 vmx->loaded_vmcs->nmi_known_unmasked = false;
Jan Kiszka3b86cd92008-09-26 09:30:57 +02005591
Avi Kivity7ffd92c2009-06-09 14:10:45 +03005592 if (vmx->rmode.vm86_active) {
Serge E. Hallyn71f98332011-04-13 09:12:54 -05005593 if (kvm_inject_realmode_interrupt(vcpu, NMI_VECTOR, 0) != EMULATE_DONE)
Mohammed Gamala92601b2010-09-19 14:34:07 +02005594 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Jan Kiszka66a5a342008-09-26 09:30:51 +02005595 return;
5596 }
Wanpeng Lic5a6d5f2016-09-22 17:55:54 +08005597
Sheng Yangf08864b2008-05-15 18:23:25 +08005598 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
5599 INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK | NMI_VECTOR);
Sheng Yangf08864b2008-05-15 18:23:25 +08005600}
5601
Jan Kiszka3cfc3092009-11-12 01:04:25 +01005602static bool vmx_get_nmi_mask(struct kvm_vcpu *vcpu)
5603{
Paolo Bonzini4c4a6f72017-07-14 13:36:11 +02005604 struct vcpu_vmx *vmx = to_vmx(vcpu);
5605 bool masked;
5606
5607 if (vmx->loaded_vmcs->nmi_known_unmasked)
Avi Kivity9d58b932011-03-07 16:52:07 +02005608 return false;
Paolo Bonzini4c4a6f72017-07-14 13:36:11 +02005609 masked = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & GUEST_INTR_STATE_NMI;
5610 vmx->loaded_vmcs->nmi_known_unmasked = !masked;
5611 return masked;
Jan Kiszka3cfc3092009-11-12 01:04:25 +01005612}
5613
5614static void vmx_set_nmi_mask(struct kvm_vcpu *vcpu, bool masked)
5615{
5616 struct vcpu_vmx *vmx = to_vmx(vcpu);
5617
Paolo Bonzini4c4a6f72017-07-14 13:36:11 +02005618 vmx->loaded_vmcs->nmi_known_unmasked = !masked;
Paolo Bonzini2c828782017-03-27 14:37:28 +02005619 if (masked)
5620 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
5621 GUEST_INTR_STATE_NMI);
5622 else
5623 vmcs_clear_bits(GUEST_INTERRUPTIBILITY_INFO,
5624 GUEST_INTR_STATE_NMI);
Jan Kiszka3cfc3092009-11-12 01:04:25 +01005625}
5626
Jan Kiszka2505dc92013-04-14 12:12:47 +02005627static int vmx_nmi_allowed(struct kvm_vcpu *vcpu)
5628{
Jan Kiszkab6b8a142014-03-07 20:03:12 +01005629 if (to_vmx(vcpu)->nested.nested_run_pending)
5630 return 0;
Jan Kiszkaea8ceb82013-04-14 21:04:26 +02005631
Jan Kiszka2505dc92013-04-14 12:12:47 +02005632 return !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) &
5633 (GUEST_INTR_STATE_MOV_SS | GUEST_INTR_STATE_STI
5634 | GUEST_INTR_STATE_NMI));
5635}
5636
Gleb Natapov78646122009-03-23 12:12:11 +02005637static int vmx_interrupt_allowed(struct kvm_vcpu *vcpu)
5638{
Jan Kiszkab6b8a142014-03-07 20:03:12 +01005639 return (!to_vmx(vcpu)->nested.nested_run_pending &&
5640 vmcs_readl(GUEST_RFLAGS) & X86_EFLAGS_IF) &&
Gleb Natapovc4282df2009-04-21 17:45:07 +03005641 !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) &
5642 (GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS));
Gleb Natapov78646122009-03-23 12:12:11 +02005643}
5644
Izik Eiduscbc94022007-10-25 00:29:55 +02005645static int vmx_set_tss_addr(struct kvm *kvm, unsigned int addr)
5646{
5647 int ret;
Izik Eiduscbc94022007-10-25 00:29:55 +02005648
Paolo Bonzini1d8007b2015-10-12 13:38:32 +02005649 ret = x86_set_memory_region(kvm, TSS_PRIVATE_MEMSLOT, addr,
5650 PAGE_SIZE * 3);
Izik Eiduscbc94022007-10-25 00:29:55 +02005651 if (ret)
5652 return ret;
Zhang Xiantaobfc6d222007-12-14 10:20:16 +08005653 kvm->arch.tss_addr = addr;
Paolo Bonzini1f755a82014-09-16 13:37:40 +02005654 return init_rmode_tss(kvm);
Izik Eiduscbc94022007-10-25 00:29:55 +02005655}
5656
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02005657static bool rmode_exception(struct kvm_vcpu *vcpu, int vec)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005658{
Jan Kiszka77ab6db2008-07-14 12:28:51 +02005659 switch (vec) {
Jan Kiszka77ab6db2008-07-14 12:28:51 +02005660 case BP_VECTOR:
Jan Kiszkac573cd222010-02-23 17:47:53 +01005661 /*
5662 * Update instruction length as we may reinject the exception
5663 * from user space while in guest debugging mode.
5664 */
5665 to_vmx(vcpu)->vcpu.arch.event_exit_inst_len =
5666 vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
Jan Kiszkad0bfb942008-12-15 13:52:10 +01005667 if (vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02005668 return false;
5669 /* fall through */
5670 case DB_VECTOR:
5671 if (vcpu->guest_debug &
5672 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))
5673 return false;
Jan Kiszkad0bfb942008-12-15 13:52:10 +01005674 /* fall through */
5675 case DE_VECTOR:
Jan Kiszka77ab6db2008-07-14 12:28:51 +02005676 case OF_VECTOR:
5677 case BR_VECTOR:
5678 case UD_VECTOR:
5679 case DF_VECTOR:
5680 case SS_VECTOR:
5681 case GP_VECTOR:
5682 case MF_VECTOR:
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02005683 return true;
5684 break;
Jan Kiszka77ab6db2008-07-14 12:28:51 +02005685 }
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02005686 return false;
5687}
5688
5689static int handle_rmode_exception(struct kvm_vcpu *vcpu,
5690 int vec, u32 err_code)
5691{
5692 /*
5693 * Instruction with address size override prefix opcode 0x67
5694 * Cause the #SS fault with 0 error code in VM86 mode.
5695 */
5696 if (((vec == GP_VECTOR) || (vec == SS_VECTOR)) && err_code == 0) {
5697 if (emulate_instruction(vcpu, 0) == EMULATE_DONE) {
5698 if (vcpu->arch.halt_request) {
5699 vcpu->arch.halt_request = 0;
Joel Schopp5cb56052015-03-02 13:43:31 -06005700 return kvm_vcpu_halt(vcpu);
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02005701 }
5702 return 1;
5703 }
5704 return 0;
5705 }
5706
5707 /*
5708 * Forward all other exceptions that are valid in real mode.
5709 * FIXME: Breaks guest debugging in real mode, needs to be fixed with
5710 * the required debugging infrastructure rework.
5711 */
5712 kvm_queue_exception(vcpu, vec);
5713 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005714}
5715
Andi Kleena0861c02009-06-08 17:37:09 +08005716/*
5717 * Trigger machine check on the host. We assume all the MSRs are already set up
5718 * by the CPU and that we still run on the same CPU as the MCE occurred on.
5719 * We pass a fake environment to the machine check handler because we want
5720 * the guest to be always treated like user space, no matter what context
5721 * it used internally.
5722 */
5723static void kvm_machine_check(void)
5724{
5725#if defined(CONFIG_X86_MCE) && defined(CONFIG_X86_64)
5726 struct pt_regs regs = {
5727 .cs = 3, /* Fake ring 3 no matter what the guest ran on */
5728 .flags = X86_EFLAGS_IF,
5729 };
5730
5731 do_machine_check(&regs, 0);
5732#endif
5733}
5734
Avi Kivity851ba692009-08-24 11:10:17 +03005735static int handle_machine_check(struct kvm_vcpu *vcpu)
Andi Kleena0861c02009-06-08 17:37:09 +08005736{
5737 /* already handled by vcpu_run */
5738 return 1;
5739}
5740
Avi Kivity851ba692009-08-24 11:10:17 +03005741static int handle_exception(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005742{
Avi Kivity1155f762007-11-22 11:30:47 +02005743 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity851ba692009-08-24 11:10:17 +03005744 struct kvm_run *kvm_run = vcpu->run;
Jan Kiszkad0bfb942008-12-15 13:52:10 +01005745 u32 intr_info, ex_no, error_code;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01005746 unsigned long cr2, rip, dr6;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005747 u32 vect_info;
5748 enum emulation_result er;
5749
Avi Kivity1155f762007-11-22 11:30:47 +02005750 vect_info = vmx->idt_vectoring_info;
Avi Kivity88786472011-03-07 17:39:45 +02005751 intr_info = vmx->exit_intr_info;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005752
Andi Kleena0861c02009-06-08 17:37:09 +08005753 if (is_machine_check(intr_info))
Avi Kivity851ba692009-08-24 11:10:17 +03005754 return handle_machine_check(vcpu);
Andi Kleena0861c02009-06-08 17:37:09 +08005755
Jim Mattsonef85b672016-12-12 11:01:37 -08005756 if (is_nmi(intr_info))
Avi Kivity1b6269d2007-10-09 12:12:19 +02005757 return 1; /* already handled by vmx_vcpu_run() */
Anthony Liguori2ab455c2007-04-27 09:29:49 +03005758
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05005759 if (is_invalid_opcode(intr_info)) {
Jan Kiszkaae1f5762015-03-09 20:56:43 +01005760 if (is_guest_mode(vcpu)) {
5761 kvm_queue_exception(vcpu, UD_VECTOR);
5762 return 1;
5763 }
Andre Przywara51d8b662010-12-21 11:12:02 +01005764 er = emulate_instruction(vcpu, EMULTYPE_TRAP_UD);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05005765 if (er != EMULATE_DONE)
Avi Kivity7ee5d9402007-11-25 15:22:50 +02005766 kvm_queue_exception(vcpu, UD_VECTOR);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05005767 return 1;
5768 }
5769
Avi Kivity6aa8b732006-12-10 02:21:36 -08005770 error_code = 0;
Ryan Harper2e113842008-02-11 10:26:38 -06005771 if (intr_info & INTR_INFO_DELIVER_CODE_MASK)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005772 error_code = vmcs_read32(VM_EXIT_INTR_ERROR_CODE);
Xiao Guangrongbf4ca232012-10-17 13:48:06 +08005773
5774 /*
5775 * The #PF with PFEC.RSVD = 1 indicates the guest is accessing
5776 * MMIO, it is better to report an internal error.
5777 * See the comments in vmx_handle_exit.
5778 */
5779 if ((vect_info & VECTORING_INFO_VALID_MASK) &&
5780 !(is_page_fault(intr_info) && !(error_code & PFERR_RSVD_MASK))) {
5781 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
5782 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_SIMUL_EX;
Radim Krčmář80f0e952015-04-02 21:11:05 +02005783 vcpu->run->internal.ndata = 3;
Xiao Guangrongbf4ca232012-10-17 13:48:06 +08005784 vcpu->run->internal.data[0] = vect_info;
5785 vcpu->run->internal.data[1] = intr_info;
Radim Krčmář80f0e952015-04-02 21:11:05 +02005786 vcpu->run->internal.data[2] = error_code;
Xiao Guangrongbf4ca232012-10-17 13:48:06 +08005787 return 0;
5788 }
5789
Avi Kivity6aa8b732006-12-10 02:21:36 -08005790 if (is_page_fault(intr_info)) {
5791 cr2 = vmcs_readl(EXIT_QUALIFICATION);
Wanpeng Li1261bfa2017-07-13 18:30:40 -07005792 /* EPT won't cause page fault directly */
5793 WARN_ON_ONCE(!vcpu->arch.apf.host_apf_reason && enable_ept);
5794 return kvm_handle_page_fault(vcpu, error_code, cr2, NULL, 0,
5795 true);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005796 }
5797
Jan Kiszkad0bfb942008-12-15 13:52:10 +01005798 ex_no = intr_info & INTR_INFO_VECTOR_MASK;
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02005799
5800 if (vmx->rmode.vm86_active && rmode_exception(vcpu, ex_no))
5801 return handle_rmode_exception(vcpu, ex_no, error_code);
5802
Jan Kiszka42dbaa52008-12-15 13:52:10 +01005803 switch (ex_no) {
Eric Northup54a20552015-11-03 18:03:53 +01005804 case AC_VECTOR:
5805 kvm_queue_exception_e(vcpu, AC_VECTOR, error_code);
5806 return 1;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01005807 case DB_VECTOR:
5808 dr6 = vmcs_readl(EXIT_QUALIFICATION);
5809 if (!(vcpu->guest_debug &
5810 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))) {
Jan Kiszka8246bf52014-01-04 18:47:17 +01005811 vcpu->arch.dr6 &= ~15;
Nadav Amit6f43ed02014-07-15 17:37:46 +03005812 vcpu->arch.dr6 |= dr6 | DR6_RTM;
Huw Daviesfd2a4452014-04-16 10:02:51 +01005813 if (!(dr6 & ~DR6_RESERVED)) /* icebp */
5814 skip_emulated_instruction(vcpu);
5815
Jan Kiszka42dbaa52008-12-15 13:52:10 +01005816 kvm_queue_exception(vcpu, DB_VECTOR);
5817 return 1;
5818 }
5819 kvm_run->debug.arch.dr6 = dr6 | DR6_FIXED_1;
5820 kvm_run->debug.arch.dr7 = vmcs_readl(GUEST_DR7);
5821 /* fall through */
5822 case BP_VECTOR:
Jan Kiszkac573cd222010-02-23 17:47:53 +01005823 /*
5824 * Update instruction length as we may reinject #BP from
5825 * user space while in guest debugging mode. Reading it for
5826 * #DB as well causes no harm, it is not used in that case.
5827 */
5828 vmx->vcpu.arch.event_exit_inst_len =
5829 vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005830 kvm_run->exit_reason = KVM_EXIT_DEBUG;
Avi Kivity0a434bb2011-04-28 15:59:33 +03005831 rip = kvm_rip_read(vcpu);
Jan Kiszkad0bfb942008-12-15 13:52:10 +01005832 kvm_run->debug.arch.pc = vmcs_readl(GUEST_CS_BASE) + rip;
5833 kvm_run->debug.arch.exception = ex_no;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01005834 break;
5835 default:
Jan Kiszkad0bfb942008-12-15 13:52:10 +01005836 kvm_run->exit_reason = KVM_EXIT_EXCEPTION;
5837 kvm_run->ex.exception = ex_no;
5838 kvm_run->ex.error_code = error_code;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01005839 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005840 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08005841 return 0;
5842}
5843
Avi Kivity851ba692009-08-24 11:10:17 +03005844static int handle_external_interrupt(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005845{
Avi Kivity1165f5f2007-04-19 17:27:43 +03005846 ++vcpu->stat.irq_exits;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005847 return 1;
5848}
5849
Avi Kivity851ba692009-08-24 11:10:17 +03005850static int handle_triple_fault(struct kvm_vcpu *vcpu)
Avi Kivity988ad742007-02-12 00:54:36 -08005851{
Avi Kivity851ba692009-08-24 11:10:17 +03005852 vcpu->run->exit_reason = KVM_EXIT_SHUTDOWN;
Avi Kivity988ad742007-02-12 00:54:36 -08005853 return 0;
5854}
Avi Kivity6aa8b732006-12-10 02:21:36 -08005855
Avi Kivity851ba692009-08-24 11:10:17 +03005856static int handle_io(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005857{
He, Qingbfdaab02007-09-12 14:18:28 +08005858 unsigned long exit_qualification;
Kyle Huey6affcbe2016-11-29 12:40:40 -08005859 int size, in, string, ret;
Avi Kivity039576c2007-03-20 12:46:50 +02005860 unsigned port;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005861
He, Qingbfdaab02007-09-12 14:18:28 +08005862 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Avi Kivity039576c2007-03-20 12:46:50 +02005863 string = (exit_qualification & 16) != 0;
Laurent Viviere70669a2007-08-05 10:36:40 +03005864 in = (exit_qualification & 8) != 0;
Laurent Viviere70669a2007-08-05 10:36:40 +03005865
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02005866 ++vcpu->stat.io_exits;
5867
5868 if (string || in)
Andre Przywara51d8b662010-12-21 11:12:02 +01005869 return emulate_instruction(vcpu, 0) == EMULATE_DONE;
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02005870
5871 port = exit_qualification >> 16;
5872 size = (exit_qualification & 7) + 1;
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02005873
Kyle Huey6affcbe2016-11-29 12:40:40 -08005874 ret = kvm_skip_emulated_instruction(vcpu);
5875
5876 /*
5877 * TODO: we might be squashing a KVM_GUESTDBG_SINGLESTEP-triggered
5878 * KVM_EXIT_DEBUG here.
5879 */
5880 return kvm_fast_pio_out(vcpu, size, port) && ret;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005881}
5882
Ingo Molnar102d8322007-02-19 14:37:47 +02005883static void
5884vmx_patch_hypercall(struct kvm_vcpu *vcpu, unsigned char *hypercall)
5885{
5886 /*
5887 * Patch in the VMCALL instruction:
5888 */
5889 hypercall[0] = 0x0f;
5890 hypercall[1] = 0x01;
5891 hypercall[2] = 0xc1;
Ingo Molnar102d8322007-02-19 14:37:47 +02005892}
5893
Guo Chao0fa06072012-06-28 15:16:19 +08005894/* called to set cr0 as appropriate for a mov-to-cr0 exit. */
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03005895static int handle_set_cr0(struct kvm_vcpu *vcpu, unsigned long val)
5896{
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03005897 if (is_guest_mode(vcpu)) {
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01005898 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
5899 unsigned long orig_val = val;
5900
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03005901 /*
5902 * We get here when L2 changed cr0 in a way that did not change
5903 * any of L1's shadowed bits (see nested_vmx_exit_handled_cr),
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01005904 * but did change L0 shadowed bits. So we first calculate the
5905 * effective cr0 value that L1 would like to write into the
5906 * hardware. It consists of the L2-owned bits from the new
5907 * value combined with the L1-owned bits from L1's guest_cr0.
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03005908 */
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01005909 val = (val & ~vmcs12->cr0_guest_host_mask) |
5910 (vmcs12->guest_cr0 & vmcs12->cr0_guest_host_mask);
5911
David Matlack38991522016-11-29 18:14:08 -08005912 if (!nested_guest_cr0_valid(vcpu, val))
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03005913 return 1;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01005914
5915 if (kvm_set_cr0(vcpu, val))
5916 return 1;
5917 vmcs_writel(CR0_READ_SHADOW, orig_val);
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03005918 return 0;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01005919 } else {
5920 if (to_vmx(vcpu)->nested.vmxon &&
David Matlack38991522016-11-29 18:14:08 -08005921 !nested_host_cr0_valid(vcpu, val))
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01005922 return 1;
David Matlack38991522016-11-29 18:14:08 -08005923
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03005924 return kvm_set_cr0(vcpu, val);
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01005925 }
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03005926}
5927
5928static int handle_set_cr4(struct kvm_vcpu *vcpu, unsigned long val)
5929{
5930 if (is_guest_mode(vcpu)) {
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01005931 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
5932 unsigned long orig_val = val;
5933
5934 /* analogously to handle_set_cr0 */
5935 val = (val & ~vmcs12->cr4_guest_host_mask) |
5936 (vmcs12->guest_cr4 & vmcs12->cr4_guest_host_mask);
5937 if (kvm_set_cr4(vcpu, val))
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03005938 return 1;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01005939 vmcs_writel(CR4_READ_SHADOW, orig_val);
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03005940 return 0;
5941 } else
5942 return kvm_set_cr4(vcpu, val);
5943}
5944
Avi Kivity851ba692009-08-24 11:10:17 +03005945static int handle_cr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005946{
Marcelo Tosatti229456f2009-06-17 09:22:14 -03005947 unsigned long exit_qualification, val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005948 int cr;
5949 int reg;
Avi Kivity49a9b072010-06-10 17:02:14 +03005950 int err;
Kyle Huey6affcbe2016-11-29 12:40:40 -08005951 int ret;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005952
He, Qingbfdaab02007-09-12 14:18:28 +08005953 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005954 cr = exit_qualification & 15;
5955 reg = (exit_qualification >> 8) & 15;
5956 switch ((exit_qualification >> 4) & 3) {
5957 case 0: /* mov to cr */
Nadav Amit1e32c072014-06-18 17:19:25 +03005958 val = kvm_register_readl(vcpu, reg);
Marcelo Tosatti229456f2009-06-17 09:22:14 -03005959 trace_kvm_cr_write(cr, val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005960 switch (cr) {
5961 case 0:
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03005962 err = handle_set_cr0(vcpu, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08005963 return kvm_complete_insn_gp(vcpu, err);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005964 case 3:
Avi Kivity23902182010-06-10 17:02:16 +03005965 err = kvm_set_cr3(vcpu, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08005966 return kvm_complete_insn_gp(vcpu, err);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005967 case 4:
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03005968 err = handle_set_cr4(vcpu, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08005969 return kvm_complete_insn_gp(vcpu, err);
Gleb Natapov0a5fff192009-04-21 17:45:06 +03005970 case 8: {
5971 u8 cr8_prev = kvm_get_cr8(vcpu);
Nadav Amit1e32c072014-06-18 17:19:25 +03005972 u8 cr8 = (u8)val;
Andre Przywaraeea1cff2010-12-21 11:12:00 +01005973 err = kvm_set_cr8(vcpu, cr8);
Kyle Huey6affcbe2016-11-29 12:40:40 -08005974 ret = kvm_complete_insn_gp(vcpu, err);
Paolo Bonzini35754c92015-07-29 12:05:37 +02005975 if (lapic_in_kernel(vcpu))
Kyle Huey6affcbe2016-11-29 12:40:40 -08005976 return ret;
Gleb Natapov0a5fff192009-04-21 17:45:06 +03005977 if (cr8_prev <= cr8)
Kyle Huey6affcbe2016-11-29 12:40:40 -08005978 return ret;
5979 /*
5980 * TODO: we might be squashing a
5981 * KVM_GUESTDBG_SINGLESTEP-triggered
5982 * KVM_EXIT_DEBUG here.
5983 */
Avi Kivity851ba692009-08-24 11:10:17 +03005984 vcpu->run->exit_reason = KVM_EXIT_SET_TPR;
Gleb Natapov0a5fff192009-04-21 17:45:06 +03005985 return 0;
5986 }
Peter Senna Tschudin4b8073e2012-09-18 18:36:14 +02005987 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08005988 break;
Anthony Liguori25c4c272007-04-27 09:29:21 +03005989 case 2: /* clts */
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -08005990 WARN_ONCE(1, "Guest should always own CR0.TS");
5991 vmx_set_cr0(vcpu, kvm_read_cr0_bits(vcpu, ~X86_CR0_TS));
Avi Kivity4d4ec082009-12-29 18:07:30 +02005992 trace_kvm_cr_write(0, kvm_read_cr0(vcpu));
Kyle Huey6affcbe2016-11-29 12:40:40 -08005993 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005994 case 1: /*mov from cr*/
5995 switch (cr) {
5996 case 3:
Avi Kivity9f8fe502010-12-05 17:30:00 +02005997 val = kvm_read_cr3(vcpu);
5998 kvm_register_write(vcpu, reg, val);
5999 trace_kvm_cr_read(cr, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08006000 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006001 case 8:
Marcelo Tosatti229456f2009-06-17 09:22:14 -03006002 val = kvm_get_cr8(vcpu);
6003 kvm_register_write(vcpu, reg, val);
6004 trace_kvm_cr_read(cr, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08006005 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006006 }
6007 break;
6008 case 3: /* lmsw */
Avi Kivitya1f83a72009-12-29 17:33:58 +02006009 val = (exit_qualification >> LMSW_SOURCE_DATA_SHIFT) & 0x0f;
Avi Kivity4d4ec082009-12-29 18:07:30 +02006010 trace_kvm_cr_write(0, (kvm_read_cr0(vcpu) & ~0xful) | val);
Avi Kivitya1f83a72009-12-29 17:33:58 +02006011 kvm_lmsw(vcpu, val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006012
Kyle Huey6affcbe2016-11-29 12:40:40 -08006013 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006014 default:
6015 break;
6016 }
Avi Kivity851ba692009-08-24 11:10:17 +03006017 vcpu->run->exit_reason = 0;
Christoffer Dalla737f252012-06-03 21:17:48 +03006018 vcpu_unimpl(vcpu, "unhandled control register: op %d cr %d\n",
Avi Kivity6aa8b732006-12-10 02:21:36 -08006019 (int)(exit_qualification >> 4) & 3, cr);
6020 return 0;
6021}
6022
Avi Kivity851ba692009-08-24 11:10:17 +03006023static int handle_dr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006024{
He, Qingbfdaab02007-09-12 14:18:28 +08006025 unsigned long exit_qualification;
Nadav Amit16f8a6f2014-10-03 01:10:05 +03006026 int dr, dr7, reg;
6027
6028 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
6029 dr = exit_qualification & DEBUG_REG_ACCESS_NUM;
6030
6031 /* First, if DR does not exist, trigger UD */
6032 if (!kvm_require_dr(vcpu, dr))
6033 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006034
Jan Kiszkaf2483412010-01-20 18:20:20 +01006035 /* Do not handle if the CPL > 0, will trigger GP on re-entry */
Avi Kivity0a79b002009-09-01 12:03:25 +03006036 if (!kvm_require_cpl(vcpu, 0))
6037 return 1;
Nadav Amit16f8a6f2014-10-03 01:10:05 +03006038 dr7 = vmcs_readl(GUEST_DR7);
6039 if (dr7 & DR7_GD) {
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006040 /*
6041 * As the vm-exit takes precedence over the debug trap, we
6042 * need to emulate the latter, either for the host or the
6043 * guest debugging itself.
6044 */
6045 if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP) {
Avi Kivity851ba692009-08-24 11:10:17 +03006046 vcpu->run->debug.arch.dr6 = vcpu->arch.dr6;
Nadav Amit16f8a6f2014-10-03 01:10:05 +03006047 vcpu->run->debug.arch.dr7 = dr7;
Nadav Amit82b32772014-11-02 11:54:45 +02006048 vcpu->run->debug.arch.pc = kvm_get_linear_rip(vcpu);
Avi Kivity851ba692009-08-24 11:10:17 +03006049 vcpu->run->debug.arch.exception = DB_VECTOR;
6050 vcpu->run->exit_reason = KVM_EXIT_DEBUG;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006051 return 0;
6052 } else {
Nadav Amit7305eb52014-11-02 11:54:44 +02006053 vcpu->arch.dr6 &= ~15;
Nadav Amit6f43ed02014-07-15 17:37:46 +03006054 vcpu->arch.dr6 |= DR6_BD | DR6_RTM;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006055 kvm_queue_exception(vcpu, DB_VECTOR);
6056 return 1;
6057 }
6058 }
6059
Paolo Bonzini81908bf2014-02-21 10:32:27 +01006060 if (vcpu->guest_debug == 0) {
Paolo Bonzini8f223722016-02-26 12:09:49 +01006061 vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL,
6062 CPU_BASED_MOV_DR_EXITING);
Paolo Bonzini81908bf2014-02-21 10:32:27 +01006063
6064 /*
6065 * No more DR vmexits; force a reload of the debug registers
6066 * and reenter on this instruction. The next vmexit will
6067 * retrieve the full state of the debug registers.
6068 */
6069 vcpu->arch.switch_db_regs |= KVM_DEBUGREG_WONT_EXIT;
6070 return 1;
6071 }
6072
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006073 reg = DEBUG_REG_ACCESS_REG(exit_qualification);
6074 if (exit_qualification & TYPE_MOV_FROM_DR) {
Gleb Natapov020df072010-04-13 10:05:23 +03006075 unsigned long val;
Jan Kiszka4c4d5632013-12-18 19:16:24 +01006076
6077 if (kvm_get_dr(vcpu, dr, &val))
6078 return 1;
6079 kvm_register_write(vcpu, reg, val);
Gleb Natapov020df072010-04-13 10:05:23 +03006080 } else
Nadav Amit57773922014-06-18 17:19:23 +03006081 if (kvm_set_dr(vcpu, dr, kvm_register_readl(vcpu, reg)))
Jan Kiszka4c4d5632013-12-18 19:16:24 +01006082 return 1;
6083
Kyle Huey6affcbe2016-11-29 12:40:40 -08006084 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006085}
6086
Jan Kiszka73aaf249e2014-01-04 18:47:16 +01006087static u64 vmx_get_dr6(struct kvm_vcpu *vcpu)
6088{
6089 return vcpu->arch.dr6;
6090}
6091
6092static void vmx_set_dr6(struct kvm_vcpu *vcpu, unsigned long val)
6093{
6094}
6095
Paolo Bonzini81908bf2014-02-21 10:32:27 +01006096static void vmx_sync_dirty_debug_regs(struct kvm_vcpu *vcpu)
6097{
Paolo Bonzini81908bf2014-02-21 10:32:27 +01006098 get_debugreg(vcpu->arch.db[0], 0);
6099 get_debugreg(vcpu->arch.db[1], 1);
6100 get_debugreg(vcpu->arch.db[2], 2);
6101 get_debugreg(vcpu->arch.db[3], 3);
6102 get_debugreg(vcpu->arch.dr6, 6);
6103 vcpu->arch.dr7 = vmcs_readl(GUEST_DR7);
6104
6105 vcpu->arch.switch_db_regs &= ~KVM_DEBUGREG_WONT_EXIT;
Paolo Bonzini8f223722016-02-26 12:09:49 +01006106 vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL, CPU_BASED_MOV_DR_EXITING);
Paolo Bonzini81908bf2014-02-21 10:32:27 +01006107}
6108
Gleb Natapov020df072010-04-13 10:05:23 +03006109static void vmx_set_dr7(struct kvm_vcpu *vcpu, unsigned long val)
6110{
6111 vmcs_writel(GUEST_DR7, val);
6112}
6113
Avi Kivity851ba692009-08-24 11:10:17 +03006114static int handle_cpuid(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006115{
Kyle Huey6a908b62016-11-29 12:40:37 -08006116 return kvm_emulate_cpuid(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006117}
6118
Avi Kivity851ba692009-08-24 11:10:17 +03006119static int handle_rdmsr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006120{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08006121 u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX];
Paolo Bonzini609e36d2015-04-08 15:30:38 +02006122 struct msr_data msr_info;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006123
Paolo Bonzini609e36d2015-04-08 15:30:38 +02006124 msr_info.index = ecx;
6125 msr_info.host_initiated = false;
6126 if (vmx_get_msr(vcpu, &msr_info)) {
Avi Kivity59200272010-01-25 19:47:02 +02006127 trace_kvm_msr_read_ex(ecx);
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02006128 kvm_inject_gp(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006129 return 1;
6130 }
6131
Paolo Bonzini609e36d2015-04-08 15:30:38 +02006132 trace_kvm_msr_read(ecx, msr_info.data);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04006133
Avi Kivity6aa8b732006-12-10 02:21:36 -08006134 /* FIXME: handling of bits 32:63 of rax, rdx */
Paolo Bonzini609e36d2015-04-08 15:30:38 +02006135 vcpu->arch.regs[VCPU_REGS_RAX] = msr_info.data & -1u;
6136 vcpu->arch.regs[VCPU_REGS_RDX] = (msr_info.data >> 32) & -1u;
Kyle Huey6affcbe2016-11-29 12:40:40 -08006137 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006138}
6139
Avi Kivity851ba692009-08-24 11:10:17 +03006140static int handle_wrmsr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006141{
Will Auld8fe8ab42012-11-29 12:42:12 -08006142 struct msr_data msr;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08006143 u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX];
6144 u64 data = (vcpu->arch.regs[VCPU_REGS_RAX] & -1u)
6145 | ((u64)(vcpu->arch.regs[VCPU_REGS_RDX] & -1u) << 32);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006146
Will Auld8fe8ab42012-11-29 12:42:12 -08006147 msr.data = data;
6148 msr.index = ecx;
6149 msr.host_initiated = false;
Nadav Amit854e8bb2014-09-16 03:24:05 +03006150 if (kvm_set_msr(vcpu, &msr) != 0) {
Avi Kivity59200272010-01-25 19:47:02 +02006151 trace_kvm_msr_write_ex(ecx, data);
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02006152 kvm_inject_gp(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006153 return 1;
6154 }
6155
Avi Kivity59200272010-01-25 19:47:02 +02006156 trace_kvm_msr_write(ecx, data);
Kyle Huey6affcbe2016-11-29 12:40:40 -08006157 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006158}
6159
Avi Kivity851ba692009-08-24 11:10:17 +03006160static int handle_tpr_below_threshold(struct kvm_vcpu *vcpu)
Yang, Sheng6e5d8652007-09-12 18:03:11 +08006161{
Paolo Bonzinieb90f342016-12-18 14:02:21 +01006162 kvm_apic_update_ppr(vcpu);
Yang, Sheng6e5d8652007-09-12 18:03:11 +08006163 return 1;
6164}
6165
Avi Kivity851ba692009-08-24 11:10:17 +03006166static int handle_interrupt_window(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006167{
Paolo Bonzini47c01522016-12-19 11:44:07 +01006168 vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL,
6169 CPU_BASED_VIRTUAL_INTR_PENDING);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04006170
Avi Kivity3842d132010-07-27 12:30:24 +03006171 kvm_make_request(KVM_REQ_EVENT, vcpu);
6172
Jan Kiszkaa26bf122008-09-26 09:30:45 +02006173 ++vcpu->stat.irq_window_exits;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006174 return 1;
6175}
6176
Avi Kivity851ba692009-08-24 11:10:17 +03006177static int handle_halt(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006178{
Avi Kivityd3bef152007-06-05 15:53:05 +03006179 return kvm_emulate_halt(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006180}
6181
Avi Kivity851ba692009-08-24 11:10:17 +03006182static int handle_vmcall(struct kvm_vcpu *vcpu)
Ingo Molnarc21415e2007-02-19 14:37:47 +02006183{
Andrey Smetanin0d9c0552016-02-11 16:44:59 +03006184 return kvm_emulate_hypercall(vcpu);
Ingo Molnarc21415e2007-02-19 14:37:47 +02006185}
6186
Gleb Natapovec25d5e2010-11-01 15:35:01 +02006187static int handle_invd(struct kvm_vcpu *vcpu)
6188{
Andre Przywara51d8b662010-12-21 11:12:02 +01006189 return emulate_instruction(vcpu, 0) == EMULATE_DONE;
Gleb Natapovec25d5e2010-11-01 15:35:01 +02006190}
6191
Avi Kivity851ba692009-08-24 11:10:17 +03006192static int handle_invlpg(struct kvm_vcpu *vcpu)
Marcelo Tosattia7052892008-09-23 13:18:35 -03006193{
Sheng Yangf9c617f2009-03-25 10:08:52 +08006194 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Marcelo Tosattia7052892008-09-23 13:18:35 -03006195
6196 kvm_mmu_invlpg(vcpu, exit_qualification);
Kyle Huey6affcbe2016-11-29 12:40:40 -08006197 return kvm_skip_emulated_instruction(vcpu);
Marcelo Tosattia7052892008-09-23 13:18:35 -03006198}
6199
Avi Kivityfee84b02011-11-10 14:57:25 +02006200static int handle_rdpmc(struct kvm_vcpu *vcpu)
6201{
6202 int err;
6203
6204 err = kvm_rdpmc(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08006205 return kvm_complete_insn_gp(vcpu, err);
Avi Kivityfee84b02011-11-10 14:57:25 +02006206}
6207
Avi Kivity851ba692009-08-24 11:10:17 +03006208static int handle_wbinvd(struct kvm_vcpu *vcpu)
Eddie Donge5edaa02007-11-11 12:28:35 +02006209{
Kyle Huey6affcbe2016-11-29 12:40:40 -08006210 return kvm_emulate_wbinvd(vcpu);
Eddie Donge5edaa02007-11-11 12:28:35 +02006211}
6212
Dexuan Cui2acf9232010-06-10 11:27:12 +08006213static int handle_xsetbv(struct kvm_vcpu *vcpu)
6214{
6215 u64 new_bv = kvm_read_edx_eax(vcpu);
6216 u32 index = kvm_register_read(vcpu, VCPU_REGS_RCX);
6217
6218 if (kvm_set_xcr(vcpu, index, new_bv) == 0)
Kyle Huey6affcbe2016-11-29 12:40:40 -08006219 return kvm_skip_emulated_instruction(vcpu);
Dexuan Cui2acf9232010-06-10 11:27:12 +08006220 return 1;
6221}
6222
Wanpeng Lif53cd632014-12-02 19:14:58 +08006223static int handle_xsaves(struct kvm_vcpu *vcpu)
6224{
Kyle Huey6affcbe2016-11-29 12:40:40 -08006225 kvm_skip_emulated_instruction(vcpu);
Wanpeng Lif53cd632014-12-02 19:14:58 +08006226 WARN(1, "this should never happen\n");
6227 return 1;
6228}
6229
6230static int handle_xrstors(struct kvm_vcpu *vcpu)
6231{
Kyle Huey6affcbe2016-11-29 12:40:40 -08006232 kvm_skip_emulated_instruction(vcpu);
Wanpeng Lif53cd632014-12-02 19:14:58 +08006233 WARN(1, "this should never happen\n");
6234 return 1;
6235}
6236
Avi Kivity851ba692009-08-24 11:10:17 +03006237static int handle_apic_access(struct kvm_vcpu *vcpu)
Sheng Yangf78e0e22007-10-29 09:40:42 +08006238{
Kevin Tian58fbbf22011-08-30 13:56:17 +03006239 if (likely(fasteoi)) {
6240 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
6241 int access_type, offset;
6242
6243 access_type = exit_qualification & APIC_ACCESS_TYPE;
6244 offset = exit_qualification & APIC_ACCESS_OFFSET;
6245 /*
6246 * Sane guest uses MOV to write EOI, with written value
6247 * not cared. So make a short-circuit here by avoiding
6248 * heavy instruction emulation.
6249 */
6250 if ((access_type == TYPE_LINEAR_APIC_INST_WRITE) &&
6251 (offset == APIC_EOI)) {
6252 kvm_lapic_set_eoi(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08006253 return kvm_skip_emulated_instruction(vcpu);
Kevin Tian58fbbf22011-08-30 13:56:17 +03006254 }
6255 }
Andre Przywara51d8b662010-12-21 11:12:02 +01006256 return emulate_instruction(vcpu, 0) == EMULATE_DONE;
Sheng Yangf78e0e22007-10-29 09:40:42 +08006257}
6258
Yang Zhangc7c9c562013-01-25 10:18:51 +08006259static int handle_apic_eoi_induced(struct kvm_vcpu *vcpu)
6260{
6261 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
6262 int vector = exit_qualification & 0xff;
6263
6264 /* EOI-induced VM exit is trap-like and thus no need to adjust IP */
6265 kvm_apic_set_eoi_accelerated(vcpu, vector);
6266 return 1;
6267}
6268
Yang Zhang83d4c282013-01-25 10:18:49 +08006269static int handle_apic_write(struct kvm_vcpu *vcpu)
6270{
6271 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
6272 u32 offset = exit_qualification & 0xfff;
6273
6274 /* APIC-write VM exit is trap-like and thus no need to adjust IP */
6275 kvm_apic_write_nodecode(vcpu, offset);
6276 return 1;
6277}
6278
Avi Kivity851ba692009-08-24 11:10:17 +03006279static int handle_task_switch(struct kvm_vcpu *vcpu)
Izik Eidus37817f22008-03-24 23:14:53 +02006280{
Jan Kiszka60637aa2008-09-26 09:30:47 +02006281 struct vcpu_vmx *vmx = to_vmx(vcpu);
Izik Eidus37817f22008-03-24 23:14:53 +02006282 unsigned long exit_qualification;
Jan Kiszkae269fb22010-04-14 15:51:09 +02006283 bool has_error_code = false;
6284 u32 error_code = 0;
Izik Eidus37817f22008-03-24 23:14:53 +02006285 u16 tss_selector;
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01006286 int reason, type, idt_v, idt_index;
Gleb Natapov64a7ec02009-03-30 16:03:29 +03006287
6288 idt_v = (vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK);
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01006289 idt_index = (vmx->idt_vectoring_info & VECTORING_INFO_VECTOR_MASK);
Gleb Natapov64a7ec02009-03-30 16:03:29 +03006290 type = (vmx->idt_vectoring_info & VECTORING_INFO_TYPE_MASK);
Izik Eidus37817f22008-03-24 23:14:53 +02006291
6292 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
6293
6294 reason = (u32)exit_qualification >> 30;
Gleb Natapov64a7ec02009-03-30 16:03:29 +03006295 if (reason == TASK_SWITCH_GATE && idt_v) {
6296 switch (type) {
6297 case INTR_TYPE_NMI_INTR:
6298 vcpu->arch.nmi_injected = false;
Avi Kivity654f06f2011-03-23 15:02:47 +02006299 vmx_set_nmi_mask(vcpu, true);
Gleb Natapov64a7ec02009-03-30 16:03:29 +03006300 break;
6301 case INTR_TYPE_EXT_INTR:
Gleb Natapov66fd3f72009-05-11 13:35:50 +03006302 case INTR_TYPE_SOFT_INTR:
Gleb Natapov64a7ec02009-03-30 16:03:29 +03006303 kvm_clear_interrupt_queue(vcpu);
6304 break;
6305 case INTR_TYPE_HARD_EXCEPTION:
Jan Kiszkae269fb22010-04-14 15:51:09 +02006306 if (vmx->idt_vectoring_info &
6307 VECTORING_INFO_DELIVER_CODE_MASK) {
6308 has_error_code = true;
6309 error_code =
6310 vmcs_read32(IDT_VECTORING_ERROR_CODE);
6311 }
6312 /* fall through */
Gleb Natapov64a7ec02009-03-30 16:03:29 +03006313 case INTR_TYPE_SOFT_EXCEPTION:
6314 kvm_clear_exception_queue(vcpu);
6315 break;
6316 default:
6317 break;
6318 }
Jan Kiszka60637aa2008-09-26 09:30:47 +02006319 }
Izik Eidus37817f22008-03-24 23:14:53 +02006320 tss_selector = exit_qualification;
6321
Gleb Natapov64a7ec02009-03-30 16:03:29 +03006322 if (!idt_v || (type != INTR_TYPE_HARD_EXCEPTION &&
6323 type != INTR_TYPE_EXT_INTR &&
6324 type != INTR_TYPE_NMI_INTR))
6325 skip_emulated_instruction(vcpu);
6326
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01006327 if (kvm_task_switch(vcpu, tss_selector,
6328 type == INTR_TYPE_SOFT_INTR ? idt_index : -1, reason,
6329 has_error_code, error_code) == EMULATE_FAIL) {
Gleb Natapovacb54512010-04-15 21:03:50 +03006330 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
6331 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
6332 vcpu->run->internal.ndata = 0;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006333 return 0;
Gleb Natapovacb54512010-04-15 21:03:50 +03006334 }
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006335
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006336 /*
6337 * TODO: What about debug traps on tss switch?
6338 * Are we supposed to inject them and update dr6?
6339 */
6340
6341 return 1;
Izik Eidus37817f22008-03-24 23:14:53 +02006342}
6343
Avi Kivity851ba692009-08-24 11:10:17 +03006344static int handle_ept_violation(struct kvm_vcpu *vcpu)
Sheng Yang14394422008-04-28 12:24:45 +08006345{
Sheng Yangf9c617f2009-03-25 10:08:52 +08006346 unsigned long exit_qualification;
Sheng Yang14394422008-04-28 12:24:45 +08006347 gpa_t gpa;
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08006348 u32 error_code;
Sheng Yang14394422008-04-28 12:24:45 +08006349
Sheng Yangf9c617f2009-03-25 10:08:52 +08006350 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Sheng Yang14394422008-04-28 12:24:45 +08006351
Gleb Natapov0be9c7a2013-09-15 11:07:23 +03006352 /*
6353 * EPT violation happened while executing iret from NMI,
6354 * "blocked by NMI" bit has to be set before next VM entry.
6355 * There are errata that may cause this bit to not be set:
6356 * AAK134, BY25.
6357 */
Gleb Natapovbcd1c292013-09-25 10:58:22 +03006358 if (!(to_vmx(vcpu)->idt_vectoring_info & VECTORING_INFO_VALID_MASK) &&
Gleb Natapovbcd1c292013-09-25 10:58:22 +03006359 (exit_qualification & INTR_INFO_UNBLOCK_NMI))
Gleb Natapov0be9c7a2013-09-15 11:07:23 +03006360 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO, GUEST_INTR_STATE_NMI);
6361
Sheng Yang14394422008-04-28 12:24:45 +08006362 gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS);
Marcelo Tosatti229456f2009-06-17 09:22:14 -03006363 trace_kvm_page_fault(gpa, exit_qualification);
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08006364
Junaid Shahid27959a42016-12-06 16:46:10 -08006365 /* Is it a read fault? */
Junaid Shahidab22a472016-12-21 20:29:28 -08006366 error_code = (exit_qualification & EPT_VIOLATION_ACC_READ)
Junaid Shahid27959a42016-12-06 16:46:10 -08006367 ? PFERR_USER_MASK : 0;
6368 /* Is it a write fault? */
Junaid Shahidab22a472016-12-21 20:29:28 -08006369 error_code |= (exit_qualification & EPT_VIOLATION_ACC_WRITE)
Junaid Shahid27959a42016-12-06 16:46:10 -08006370 ? PFERR_WRITE_MASK : 0;
6371 /* Is it a fetch fault? */
Junaid Shahidab22a472016-12-21 20:29:28 -08006372 error_code |= (exit_qualification & EPT_VIOLATION_ACC_INSTR)
Junaid Shahid27959a42016-12-06 16:46:10 -08006373 ? PFERR_FETCH_MASK : 0;
6374 /* ept page table entry is present? */
6375 error_code |= (exit_qualification &
6376 (EPT_VIOLATION_READABLE | EPT_VIOLATION_WRITABLE |
6377 EPT_VIOLATION_EXECUTABLE))
6378 ? PFERR_PRESENT_MASK : 0;
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08006379
Paolo Bonzinidb1c0562016-12-08 15:31:41 +01006380 vcpu->arch.gpa_available = true;
Yang Zhang25d92082013-08-06 12:00:32 +03006381 vcpu->arch.exit_qualification = exit_qualification;
6382
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08006383 return kvm_mmu_page_fault(vcpu, gpa, error_code, NULL, 0);
Sheng Yang14394422008-04-28 12:24:45 +08006384}
6385
Avi Kivity851ba692009-08-24 11:10:17 +03006386static int handle_ept_misconfig(struct kvm_vcpu *vcpu)
Marcelo Tosatti68f89402009-06-11 12:07:43 -03006387{
Xiao Guangrongf735d4a2015-08-05 12:04:27 +08006388 int ret;
Marcelo Tosatti68f89402009-06-11 12:07:43 -03006389 gpa_t gpa;
6390
6391 gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS);
Nikolay Nikolaeve32edf42015-03-26 14:39:28 +00006392 if (!kvm_io_bus_write(vcpu, KVM_FAST_MMIO_BUS, gpa, 0, NULL)) {
Jason Wang931c33b2015-09-15 14:41:58 +08006393 trace_kvm_fast_mmio(gpa);
Kyle Huey6affcbe2016-11-29 12:40:40 -08006394 return kvm_skip_emulated_instruction(vcpu);
Michael S. Tsirkin68c3b4d2014-03-31 21:50:44 +03006395 }
Marcelo Tosatti68f89402009-06-11 12:07:43 -03006396
Paolo Bonzini450869d2015-11-04 13:41:21 +01006397 ret = handle_mmio_page_fault(vcpu, gpa, true);
Paolo Bonzinidb1c0562016-12-08 15:31:41 +01006398 vcpu->arch.gpa_available = true;
Xiao Guangrongb37fbea2013-06-07 16:51:25 +08006399 if (likely(ret == RET_MMIO_PF_EMULATE))
Xiao Guangrongce88dec2011-07-12 03:33:44 +08006400 return x86_emulate_instruction(vcpu, gpa, 0, NULL, 0) ==
6401 EMULATE_DONE;
Xiao Guangrongf8f55942013-06-07 16:51:26 +08006402
6403 if (unlikely(ret == RET_MMIO_PF_INVALID))
6404 return kvm_mmu_page_fault(vcpu, gpa, 0, NULL, 0);
6405
Xiao Guangrongb37fbea2013-06-07 16:51:25 +08006406 if (unlikely(ret == RET_MMIO_PF_RETRY))
Xiao Guangrongce88dec2011-07-12 03:33:44 +08006407 return 1;
6408
6409 /* It is the real ept misconfig */
Xiao Guangrongf735d4a2015-08-05 12:04:27 +08006410 WARN_ON(1);
Marcelo Tosatti68f89402009-06-11 12:07:43 -03006411
Avi Kivity851ba692009-08-24 11:10:17 +03006412 vcpu->run->exit_reason = KVM_EXIT_UNKNOWN;
6413 vcpu->run->hw.hardware_exit_reason = EXIT_REASON_EPT_MISCONFIG;
Marcelo Tosatti68f89402009-06-11 12:07:43 -03006414
6415 return 0;
6416}
6417
Avi Kivity851ba692009-08-24 11:10:17 +03006418static int handle_nmi_window(struct kvm_vcpu *vcpu)
Sheng Yangf08864b2008-05-15 18:23:25 +08006419{
Paolo Bonzini47c01522016-12-19 11:44:07 +01006420 vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL,
6421 CPU_BASED_VIRTUAL_NMI_PENDING);
Sheng Yangf08864b2008-05-15 18:23:25 +08006422 ++vcpu->stat.nmi_window_exits;
Avi Kivity3842d132010-07-27 12:30:24 +03006423 kvm_make_request(KVM_REQ_EVENT, vcpu);
Sheng Yangf08864b2008-05-15 18:23:25 +08006424
6425 return 1;
6426}
6427
Mohammed Gamal80ced182009-09-01 12:48:18 +02006428static int handle_invalid_guest_state(struct kvm_vcpu *vcpu)
Mohammed Gamalea953ef2008-08-17 16:47:05 +03006429{
Avi Kivity8b3079a2009-01-05 12:10:54 +02006430 struct vcpu_vmx *vmx = to_vmx(vcpu);
6431 enum emulation_result err = EMULATE_DONE;
Mohammed Gamal80ced182009-09-01 12:48:18 +02006432 int ret = 1;
Avi Kivity49e9d552010-09-19 14:34:08 +02006433 u32 cpu_exec_ctrl;
6434 bool intr_window_requested;
Avi Kivityb8405c12012-06-07 17:08:48 +03006435 unsigned count = 130;
Avi Kivity49e9d552010-09-19 14:34:08 +02006436
6437 cpu_exec_ctrl = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
6438 intr_window_requested = cpu_exec_ctrl & CPU_BASED_VIRTUAL_INTR_PENDING;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03006439
Paolo Bonzini98eb2f82014-03-27 09:51:52 +01006440 while (vmx->emulation_required && count-- != 0) {
Avi Kivitybdea48e2012-06-10 18:07:57 +03006441 if (intr_window_requested && vmx_interrupt_allowed(vcpu))
Avi Kivity49e9d552010-09-19 14:34:08 +02006442 return handle_interrupt_window(&vmx->vcpu);
6443
Radim Krčmář72875d82017-04-26 22:32:19 +02006444 if (kvm_test_request(KVM_REQ_EVENT, vcpu))
Avi Kivityde87dcdd2012-06-12 20:21:38 +03006445 return 1;
6446
Gleb Natapov991eebf2013-04-11 12:10:51 +03006447 err = emulate_instruction(vcpu, EMULTYPE_NO_REEXECUTE);
Mohammed Gamalea953ef2008-08-17 16:47:05 +03006448
Paolo Bonziniac0a48c2013-06-25 18:24:41 +02006449 if (err == EMULATE_USER_EXIT) {
Paolo Bonzini94452b92013-08-27 15:41:42 +02006450 ++vcpu->stat.mmio_exits;
Mohammed Gamal80ced182009-09-01 12:48:18 +02006451 ret = 0;
6452 goto out;
6453 }
Guillaume Thouvenin1d5a4d92008-10-29 09:39:42 +01006454
Avi Kivityde5f70e2012-06-12 20:22:28 +03006455 if (err != EMULATE_DONE) {
6456 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
6457 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
6458 vcpu->run->internal.ndata = 0;
Gleb Natapov6d77dbf2010-05-10 11:16:56 +03006459 return 0;
Avi Kivityde5f70e2012-06-12 20:22:28 +03006460 }
Mohammed Gamalea953ef2008-08-17 16:47:05 +03006461
Gleb Natapov8d76c492013-05-08 18:38:44 +03006462 if (vcpu->arch.halt_request) {
6463 vcpu->arch.halt_request = 0;
Joel Schopp5cb56052015-03-02 13:43:31 -06006464 ret = kvm_vcpu_halt(vcpu);
Gleb Natapov8d76c492013-05-08 18:38:44 +03006465 goto out;
6466 }
6467
Mohammed Gamalea953ef2008-08-17 16:47:05 +03006468 if (signal_pending(current))
Mohammed Gamal80ced182009-09-01 12:48:18 +02006469 goto out;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03006470 if (need_resched())
6471 schedule();
6472 }
6473
Mohammed Gamal80ced182009-09-01 12:48:18 +02006474out:
6475 return ret;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03006476}
6477
Radim Krčmářb4a2d312014-08-21 18:08:08 +02006478static int __grow_ple_window(int val)
6479{
6480 if (ple_window_grow < 1)
6481 return ple_window;
6482
6483 val = min(val, ple_window_actual_max);
6484
6485 if (ple_window_grow < ple_window)
6486 val *= ple_window_grow;
6487 else
6488 val += ple_window_grow;
6489
6490 return val;
6491}
6492
6493static int __shrink_ple_window(int val, int modifier, int minimum)
6494{
6495 if (modifier < 1)
6496 return ple_window;
6497
6498 if (modifier < ple_window)
6499 val /= modifier;
6500 else
6501 val -= modifier;
6502
6503 return max(val, minimum);
6504}
6505
6506static void grow_ple_window(struct kvm_vcpu *vcpu)
6507{
6508 struct vcpu_vmx *vmx = to_vmx(vcpu);
6509 int old = vmx->ple_window;
6510
6511 vmx->ple_window = __grow_ple_window(old);
6512
6513 if (vmx->ple_window != old)
6514 vmx->ple_window_dirty = true;
Radim Krčmář7b462682014-08-21 18:08:09 +02006515
6516 trace_kvm_ple_window_grow(vcpu->vcpu_id, vmx->ple_window, old);
Radim Krčmářb4a2d312014-08-21 18:08:08 +02006517}
6518
6519static void shrink_ple_window(struct kvm_vcpu *vcpu)
6520{
6521 struct vcpu_vmx *vmx = to_vmx(vcpu);
6522 int old = vmx->ple_window;
6523
6524 vmx->ple_window = __shrink_ple_window(old,
6525 ple_window_shrink, ple_window);
6526
6527 if (vmx->ple_window != old)
6528 vmx->ple_window_dirty = true;
Radim Krčmář7b462682014-08-21 18:08:09 +02006529
6530 trace_kvm_ple_window_shrink(vcpu->vcpu_id, vmx->ple_window, old);
Radim Krčmářb4a2d312014-08-21 18:08:08 +02006531}
6532
6533/*
6534 * ple_window_actual_max is computed to be one grow_ple_window() below
6535 * ple_window_max. (See __grow_ple_window for the reason.)
6536 * This prevents overflows, because ple_window_max is int.
6537 * ple_window_max effectively rounded down to a multiple of ple_window_grow in
6538 * this process.
6539 * ple_window_max is also prevented from setting vmx->ple_window < ple_window.
6540 */
6541static void update_ple_window_actual_max(void)
6542{
6543 ple_window_actual_max =
6544 __shrink_ple_window(max(ple_window_max, ple_window),
6545 ple_window_grow, INT_MIN);
6546}
6547
Feng Wubf9f6ac2015-09-18 22:29:55 +08006548/*
6549 * Handler for POSTED_INTERRUPT_WAKEUP_VECTOR.
6550 */
6551static void wakeup_handler(void)
6552{
6553 struct kvm_vcpu *vcpu;
6554 int cpu = smp_processor_id();
6555
6556 spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, cpu));
6557 list_for_each_entry(vcpu, &per_cpu(blocked_vcpu_on_cpu, cpu),
6558 blocked_vcpu_list) {
6559 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
6560
6561 if (pi_test_on(pi_desc) == 1)
6562 kvm_vcpu_kick(vcpu);
6563 }
6564 spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, cpu));
6565}
6566
Junaid Shahidf160c7b2016-12-06 16:46:16 -08006567void vmx_enable_tdp(void)
6568{
6569 kvm_mmu_set_mask_ptes(VMX_EPT_READABLE_MASK,
6570 enable_ept_ad_bits ? VMX_EPT_ACCESS_BIT : 0ull,
6571 enable_ept_ad_bits ? VMX_EPT_DIRTY_BIT : 0ull,
6572 0ull, VMX_EPT_EXECUTABLE_MASK,
6573 cpu_has_vmx_ept_execute_only() ? 0ull : VMX_EPT_READABLE_MASK,
Peter Feiner995f00a2017-06-30 17:26:32 -07006574 VMX_EPT_RWX_MASK);
Junaid Shahidf160c7b2016-12-06 16:46:16 -08006575
6576 ept_set_mmio_spte_mask();
6577 kvm_enable_tdp();
6578}
6579
Tiejun Chenf2c76482014-10-28 10:14:47 +08006580static __init int hardware_setup(void)
6581{
Tiejun Chen34a1cd62014-10-28 10:14:48 +08006582 int r = -ENOMEM, i, msr;
6583
6584 rdmsrl_safe(MSR_EFER, &host_efer);
6585
6586 for (i = 0; i < ARRAY_SIZE(vmx_msr_index); ++i)
6587 kvm_define_shared_msr(i, vmx_msr_index[i]);
6588
Radim Krčmář23611332016-09-29 22:41:33 +02006589 for (i = 0; i < VMX_BITMAP_NR; i++) {
6590 vmx_bitmap[i] = (unsigned long *)__get_free_page(GFP_KERNEL);
6591 if (!vmx_bitmap[i])
6592 goto out;
6593 }
Tiejun Chen34a1cd62014-10-28 10:14:48 +08006594
6595 vmx_io_bitmap_b = (unsigned long *)__get_free_page(GFP_KERNEL);
Tiejun Chen34a1cd62014-10-28 10:14:48 +08006596 memset(vmx_vmread_bitmap, 0xff, PAGE_SIZE);
6597 memset(vmx_vmwrite_bitmap, 0xff, PAGE_SIZE);
6598
6599 /*
6600 * Allow direct access to the PC debug port (it is often used for I/O
6601 * delays, but the vmexits simply slow things down).
6602 */
6603 memset(vmx_io_bitmap_a, 0xff, PAGE_SIZE);
6604 clear_bit(0x80, vmx_io_bitmap_a);
6605
6606 memset(vmx_io_bitmap_b, 0xff, PAGE_SIZE);
6607
6608 memset(vmx_msr_bitmap_legacy, 0xff, PAGE_SIZE);
6609 memset(vmx_msr_bitmap_longmode, 0xff, PAGE_SIZE);
6610
Tiejun Chen34a1cd62014-10-28 10:14:48 +08006611 if (setup_vmcs_config(&vmcs_config) < 0) {
6612 r = -EIO;
Radim Krčmář23611332016-09-29 22:41:33 +02006613 goto out;
Tiejun Chenbaa03522014-12-23 16:21:11 +08006614 }
Tiejun Chenf2c76482014-10-28 10:14:47 +08006615
6616 if (boot_cpu_has(X86_FEATURE_NX))
6617 kvm_enable_efer_bits(EFER_NX);
6618
Wanpeng Li08d839c2017-03-23 05:30:08 -07006619 if (!cpu_has_vmx_vpid() || !cpu_has_vmx_invvpid() ||
6620 !(cpu_has_vmx_invvpid_single() || cpu_has_vmx_invvpid_global()))
Tiejun Chenf2c76482014-10-28 10:14:47 +08006621 enable_vpid = 0;
Wanpeng Li08d839c2017-03-23 05:30:08 -07006622
Tiejun Chenf2c76482014-10-28 10:14:47 +08006623 if (!cpu_has_vmx_shadow_vmcs())
6624 enable_shadow_vmcs = 0;
6625 if (enable_shadow_vmcs)
6626 init_vmcs_shadow_fields();
6627
6628 if (!cpu_has_vmx_ept() ||
6629 !cpu_has_vmx_ept_4levels()) {
6630 enable_ept = 0;
6631 enable_unrestricted_guest = 0;
6632 enable_ept_ad_bits = 0;
6633 }
6634
Wanpeng Lifce6ac42017-05-11 02:58:56 -07006635 if (!cpu_has_vmx_ept_ad_bits() || !enable_ept)
Tiejun Chenf2c76482014-10-28 10:14:47 +08006636 enable_ept_ad_bits = 0;
6637
6638 if (!cpu_has_vmx_unrestricted_guest())
6639 enable_unrestricted_guest = 0;
6640
Paolo Bonziniad15a292015-01-30 16:18:49 +01006641 if (!cpu_has_vmx_flexpriority())
Tiejun Chenf2c76482014-10-28 10:14:47 +08006642 flexpriority_enabled = 0;
6643
Paolo Bonziniad15a292015-01-30 16:18:49 +01006644 /*
6645 * set_apic_access_page_addr() is used to reload apic access
6646 * page upon invalidation. No need to do anything if not
6647 * using the APIC_ACCESS_ADDR VMCS field.
6648 */
6649 if (!flexpriority_enabled)
Tiejun Chenf2c76482014-10-28 10:14:47 +08006650 kvm_x86_ops->set_apic_access_page_addr = NULL;
Tiejun Chenf2c76482014-10-28 10:14:47 +08006651
6652 if (!cpu_has_vmx_tpr_shadow())
6653 kvm_x86_ops->update_cr8_intercept = NULL;
6654
6655 if (enable_ept && !cpu_has_vmx_ept_2m_page())
6656 kvm_disable_largepages();
6657
6658 if (!cpu_has_vmx_ple())
6659 ple_gap = 0;
6660
Paolo Bonzini76dfafd52016-12-19 17:17:11 +01006661 if (!cpu_has_vmx_apicv()) {
Tiejun Chenf2c76482014-10-28 10:14:47 +08006662 enable_apicv = 0;
Paolo Bonzini76dfafd52016-12-19 17:17:11 +01006663 kvm_x86_ops->sync_pir_to_irr = NULL;
6664 }
Tiejun Chenf2c76482014-10-28 10:14:47 +08006665
Haozhong Zhang64903d62015-10-20 15:39:09 +08006666 if (cpu_has_vmx_tsc_scaling()) {
6667 kvm_has_tsc_control = true;
6668 kvm_max_tsc_scaling_ratio = KVM_VMX_TSC_MULTIPLIER_MAX;
6669 kvm_tsc_scaling_ratio_frac_bits = 48;
6670 }
6671
Tiejun Chenbaa03522014-12-23 16:21:11 +08006672 vmx_disable_intercept_for_msr(MSR_FS_BASE, false);
6673 vmx_disable_intercept_for_msr(MSR_GS_BASE, false);
6674 vmx_disable_intercept_for_msr(MSR_KERNEL_GS_BASE, true);
6675 vmx_disable_intercept_for_msr(MSR_IA32_SYSENTER_CS, false);
6676 vmx_disable_intercept_for_msr(MSR_IA32_SYSENTER_ESP, false);
6677 vmx_disable_intercept_for_msr(MSR_IA32_SYSENTER_EIP, false);
Tiejun Chenbaa03522014-12-23 16:21:11 +08006678
Wanpeng Lic63e4562016-09-23 19:17:16 +08006679 memcpy(vmx_msr_bitmap_legacy_x2apic_apicv,
6680 vmx_msr_bitmap_legacy, PAGE_SIZE);
6681 memcpy(vmx_msr_bitmap_longmode_x2apic_apicv,
6682 vmx_msr_bitmap_longmode, PAGE_SIZE);
Tiejun Chenbaa03522014-12-23 16:21:11 +08006683 memcpy(vmx_msr_bitmap_legacy_x2apic,
6684 vmx_msr_bitmap_legacy, PAGE_SIZE);
6685 memcpy(vmx_msr_bitmap_longmode_x2apic,
6686 vmx_msr_bitmap_longmode, PAGE_SIZE);
6687
Wanpeng Li04bb92e2015-09-16 19:31:11 +08006688 set_bit(0, vmx_vpid_bitmap); /* 0 is reserved for host */
6689
Radim Krčmář40d83382016-09-29 22:41:31 +02006690 for (msr = 0x800; msr <= 0x8ff; msr++) {
6691 if (msr == 0x839 /* TMCCT */)
6692 continue;
Radim Krčmář2e69f862016-09-29 22:41:32 +02006693 vmx_disable_intercept_msr_x2apic(msr, MSR_TYPE_R, true);
Radim Krčmář40d83382016-09-29 22:41:31 +02006694 }
Tiejun Chenbaa03522014-12-23 16:21:11 +08006695
Wanpeng Lif6e90f92016-09-22 07:43:25 +08006696 /*
Radim Krčmář2e69f862016-09-29 22:41:32 +02006697 * TPR reads and writes can be virtualized even if virtual interrupt
6698 * delivery is not in use.
Wanpeng Lif6e90f92016-09-22 07:43:25 +08006699 */
Radim Krčmář2e69f862016-09-29 22:41:32 +02006700 vmx_disable_intercept_msr_x2apic(0x808, MSR_TYPE_W, true);
6701 vmx_disable_intercept_msr_x2apic(0x808, MSR_TYPE_R | MSR_TYPE_W, false);
6702
Roman Kagan3ce424e2016-05-18 17:48:20 +03006703 /* EOI */
Radim Krčmář2e69f862016-09-29 22:41:32 +02006704 vmx_disable_intercept_msr_x2apic(0x80b, MSR_TYPE_W, true);
Roman Kagan3ce424e2016-05-18 17:48:20 +03006705 /* SELF-IPI */
Radim Krčmář2e69f862016-09-29 22:41:32 +02006706 vmx_disable_intercept_msr_x2apic(0x83f, MSR_TYPE_W, true);
Tiejun Chenbaa03522014-12-23 16:21:11 +08006707
Junaid Shahidf160c7b2016-12-06 16:46:16 -08006708 if (enable_ept)
6709 vmx_enable_tdp();
6710 else
Tiejun Chenbaa03522014-12-23 16:21:11 +08006711 kvm_disable_tdp();
6712
6713 update_ple_window_actual_max();
6714
Kai Huang843e4332015-01-28 10:54:28 +08006715 /*
6716 * Only enable PML when hardware supports PML feature, and both EPT
6717 * and EPT A/D bit features are enabled -- PML depends on them to work.
6718 */
6719 if (!enable_ept || !enable_ept_ad_bits || !cpu_has_vmx_pml())
6720 enable_pml = 0;
6721
6722 if (!enable_pml) {
6723 kvm_x86_ops->slot_enable_log_dirty = NULL;
6724 kvm_x86_ops->slot_disable_log_dirty = NULL;
6725 kvm_x86_ops->flush_log_dirty = NULL;
6726 kvm_x86_ops->enable_log_dirty_pt_masked = NULL;
6727 }
6728
Yunhong Jiang64672c92016-06-13 14:19:59 -07006729 if (cpu_has_vmx_preemption_timer() && enable_preemption_timer) {
6730 u64 vmx_msr;
6731
6732 rdmsrl(MSR_IA32_VMX_MISC, vmx_msr);
6733 cpu_preemption_timer_multi =
6734 vmx_msr & VMX_MISC_PREEMPTION_TIMER_RATE_MASK;
6735 } else {
6736 kvm_x86_ops->set_hv_timer = NULL;
6737 kvm_x86_ops->cancel_hv_timer = NULL;
6738 }
6739
Feng Wubf9f6ac2015-09-18 22:29:55 +08006740 kvm_set_posted_intr_wakeup_handler(wakeup_handler);
6741
Ashok Rajc45dcc72016-06-22 14:59:56 +08006742 kvm_mce_cap_supported |= MCG_LMCE_P;
6743
Tiejun Chenf2c76482014-10-28 10:14:47 +08006744 return alloc_kvm_area();
Tiejun Chen34a1cd62014-10-28 10:14:48 +08006745
Tiejun Chen34a1cd62014-10-28 10:14:48 +08006746out:
Radim Krčmář23611332016-09-29 22:41:33 +02006747 for (i = 0; i < VMX_BITMAP_NR; i++)
6748 free_page((unsigned long)vmx_bitmap[i]);
Tiejun Chen34a1cd62014-10-28 10:14:48 +08006749
6750 return r;
Tiejun Chenf2c76482014-10-28 10:14:47 +08006751}
6752
6753static __exit void hardware_unsetup(void)
6754{
Radim Krčmář23611332016-09-29 22:41:33 +02006755 int i;
6756
6757 for (i = 0; i < VMX_BITMAP_NR; i++)
6758 free_page((unsigned long)vmx_bitmap[i]);
Tiejun Chen34a1cd62014-10-28 10:14:48 +08006759
Tiejun Chenf2c76482014-10-28 10:14:47 +08006760 free_kvm_area();
6761}
6762
Avi Kivity6aa8b732006-12-10 02:21:36 -08006763/*
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08006764 * Indicate a busy-waiting vcpu in spinlock. We do not enable the PAUSE
6765 * exiting, so only get here on cpu with PAUSE-Loop-Exiting.
6766 */
Marcelo Tosatti9fb41ba2009-10-12 19:37:31 -03006767static int handle_pause(struct kvm_vcpu *vcpu)
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08006768{
Radim Krčmářb4a2d312014-08-21 18:08:08 +02006769 if (ple_gap)
6770 grow_ple_window(vcpu);
6771
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08006772 kvm_vcpu_on_spin(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08006773 return kvm_skip_emulated_instruction(vcpu);
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08006774}
6775
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04006776static int handle_nop(struct kvm_vcpu *vcpu)
Sheng Yang59708672009-12-15 13:29:54 +08006777{
Kyle Huey6affcbe2016-11-29 12:40:40 -08006778 return kvm_skip_emulated_instruction(vcpu);
Sheng Yang59708672009-12-15 13:29:54 +08006779}
6780
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04006781static int handle_mwait(struct kvm_vcpu *vcpu)
6782{
6783 printk_once(KERN_WARNING "kvm: MWAIT instruction emulated as NOP!\n");
6784 return handle_nop(vcpu);
6785}
6786
Mihai Donțu5f3d45e2015-07-05 20:08:57 +03006787static int handle_monitor_trap(struct kvm_vcpu *vcpu)
6788{
6789 return 1;
6790}
6791
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04006792static int handle_monitor(struct kvm_vcpu *vcpu)
6793{
6794 printk_once(KERN_WARNING "kvm: MONITOR instruction emulated as NOP!\n");
6795 return handle_nop(vcpu);
6796}
6797
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08006798/*
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +03006799 * To run an L2 guest, we need a vmcs02 based on the L1-specified vmcs12.
6800 * We could reuse a single VMCS for all the L2 guests, but we also want the
6801 * option to allocate a separate vmcs02 for each separate loaded vmcs12 - this
6802 * allows keeping them loaded on the processor, and in the future will allow
6803 * optimizations where prepare_vmcs02 doesn't need to set all the fields on
6804 * every entry if they never change.
6805 * So we keep, in vmx->nested.vmcs02_pool, a cache of size VMCS02_POOL_SIZE
6806 * (>=0) with a vmcs02 for each recently loaded vmcs12s, most recent first.
6807 *
6808 * The following functions allocate and free a vmcs02 in this pool.
6809 */
6810
6811/* Get a VMCS from the pool to use as vmcs02 for the current vmcs12. */
6812static struct loaded_vmcs *nested_get_current_vmcs02(struct vcpu_vmx *vmx)
6813{
6814 struct vmcs02_list *item;
6815 list_for_each_entry(item, &vmx->nested.vmcs02_pool, list)
6816 if (item->vmptr == vmx->nested.current_vmptr) {
6817 list_move(&item->list, &vmx->nested.vmcs02_pool);
6818 return &item->vmcs02;
6819 }
6820
6821 if (vmx->nested.vmcs02_num >= max(VMCS02_POOL_SIZE, 1)) {
6822 /* Recycle the least recently used VMCS. */
Geliang Tangd74c0e62016-01-01 19:47:14 +08006823 item = list_last_entry(&vmx->nested.vmcs02_pool,
6824 struct vmcs02_list, list);
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +03006825 item->vmptr = vmx->nested.current_vmptr;
6826 list_move(&item->list, &vmx->nested.vmcs02_pool);
6827 return &item->vmcs02;
6828 }
6829
6830 /* Create a new VMCS */
Ioan Orghici0fa24ce2013-03-10 15:46:00 +02006831 item = kmalloc(sizeof(struct vmcs02_list), GFP_KERNEL);
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +03006832 if (!item)
6833 return NULL;
6834 item->vmcs02.vmcs = alloc_vmcs();
Jim Mattson355f4fb2016-10-28 08:29:39 -07006835 item->vmcs02.shadow_vmcs = NULL;
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +03006836 if (!item->vmcs02.vmcs) {
6837 kfree(item);
6838 return NULL;
6839 }
6840 loaded_vmcs_init(&item->vmcs02);
6841 item->vmptr = vmx->nested.current_vmptr;
6842 list_add(&(item->list), &(vmx->nested.vmcs02_pool));
6843 vmx->nested.vmcs02_num++;
6844 return &item->vmcs02;
6845}
6846
6847/* Free and remove from pool a vmcs02 saved for a vmcs12 (if there is one) */
6848static void nested_free_vmcs02(struct vcpu_vmx *vmx, gpa_t vmptr)
6849{
6850 struct vmcs02_list *item;
6851 list_for_each_entry(item, &vmx->nested.vmcs02_pool, list)
6852 if (item->vmptr == vmptr) {
6853 free_loaded_vmcs(&item->vmcs02);
6854 list_del(&item->list);
6855 kfree(item);
6856 vmx->nested.vmcs02_num--;
6857 return;
6858 }
6859}
6860
6861/*
6862 * Free all VMCSs saved for this vcpu, except the one pointed by
Paolo Bonzini4fa77342014-07-17 12:25:16 +02006863 * vmx->loaded_vmcs. We must be running L1, so vmx->loaded_vmcs
6864 * must be &vmx->vmcs01.
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +03006865 */
6866static void nested_free_all_saved_vmcss(struct vcpu_vmx *vmx)
6867{
6868 struct vmcs02_list *item, *n;
Paolo Bonzini4fa77342014-07-17 12:25:16 +02006869
6870 WARN_ON(vmx->loaded_vmcs != &vmx->vmcs01);
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +03006871 list_for_each_entry_safe(item, n, &vmx->nested.vmcs02_pool, list) {
Paolo Bonzini4fa77342014-07-17 12:25:16 +02006872 /*
6873 * Something will leak if the above WARN triggers. Better than
6874 * a use-after-free.
6875 */
6876 if (vmx->loaded_vmcs == &item->vmcs02)
6877 continue;
6878
6879 free_loaded_vmcs(&item->vmcs02);
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +03006880 list_del(&item->list);
6881 kfree(item);
Paolo Bonzini4fa77342014-07-17 12:25:16 +02006882 vmx->nested.vmcs02_num--;
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +03006883 }
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +03006884}
6885
Arthur Chunqi Li0658fba2013-07-04 15:03:32 +08006886/*
6887 * The following 3 functions, nested_vmx_succeed()/failValid()/failInvalid(),
6888 * set the success or error code of an emulated VMX instruction, as specified
6889 * by Vol 2B, VMX Instruction Reference, "Conventions".
6890 */
6891static void nested_vmx_succeed(struct kvm_vcpu *vcpu)
6892{
6893 vmx_set_rflags(vcpu, vmx_get_rflags(vcpu)
6894 & ~(X86_EFLAGS_CF | X86_EFLAGS_PF | X86_EFLAGS_AF |
6895 X86_EFLAGS_ZF | X86_EFLAGS_SF | X86_EFLAGS_OF));
6896}
6897
6898static void nested_vmx_failInvalid(struct kvm_vcpu *vcpu)
6899{
6900 vmx_set_rflags(vcpu, (vmx_get_rflags(vcpu)
6901 & ~(X86_EFLAGS_PF | X86_EFLAGS_AF | X86_EFLAGS_ZF |
6902 X86_EFLAGS_SF | X86_EFLAGS_OF))
6903 | X86_EFLAGS_CF);
6904}
6905
Abel Gordon145c28d2013-04-18 14:36:55 +03006906static void nested_vmx_failValid(struct kvm_vcpu *vcpu,
Arthur Chunqi Li0658fba2013-07-04 15:03:32 +08006907 u32 vm_instruction_error)
6908{
6909 if (to_vmx(vcpu)->nested.current_vmptr == -1ull) {
6910 /*
6911 * failValid writes the error number to the current VMCS, which
6912 * can't be done there isn't a current VMCS.
6913 */
6914 nested_vmx_failInvalid(vcpu);
6915 return;
6916 }
6917 vmx_set_rflags(vcpu, (vmx_get_rflags(vcpu)
6918 & ~(X86_EFLAGS_CF | X86_EFLAGS_PF | X86_EFLAGS_AF |
6919 X86_EFLAGS_SF | X86_EFLAGS_OF))
6920 | X86_EFLAGS_ZF);
6921 get_vmcs12(vcpu)->vm_instruction_error = vm_instruction_error;
6922 /*
6923 * We don't need to force a shadow sync because
6924 * VM_INSTRUCTION_ERROR is not shadowed
6925 */
6926}
Abel Gordon145c28d2013-04-18 14:36:55 +03006927
Wincy Vanff651cb2014-12-11 08:52:58 +03006928static void nested_vmx_abort(struct kvm_vcpu *vcpu, u32 indicator)
6929{
6930 /* TODO: not to reset guest simply here. */
6931 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Paolo Bonzinibbe41b92016-08-19 17:51:20 +02006932 pr_debug_ratelimited("kvm: nested vmx abort, indicator %d\n", indicator);
Wincy Vanff651cb2014-12-11 08:52:58 +03006933}
6934
Jan Kiszkaf41245002014-03-07 20:03:13 +01006935static enum hrtimer_restart vmx_preemption_timer_fn(struct hrtimer *timer)
6936{
6937 struct vcpu_vmx *vmx =
6938 container_of(timer, struct vcpu_vmx, nested.preemption_timer);
6939
6940 vmx->nested.preemption_timer_expired = true;
6941 kvm_make_request(KVM_REQ_EVENT, &vmx->vcpu);
6942 kvm_vcpu_kick(&vmx->vcpu);
6943
6944 return HRTIMER_NORESTART;
6945}
6946
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +03006947/*
Bandan Das19677e32014-05-06 02:19:15 -04006948 * Decode the memory-address operand of a vmx instruction, as recorded on an
6949 * exit caused by such an instruction (run by a guest hypervisor).
6950 * On success, returns 0. When the operand is invalid, returns 1 and throws
6951 * #UD or #GP.
6952 */
6953static int get_vmx_mem_address(struct kvm_vcpu *vcpu,
6954 unsigned long exit_qualification,
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00006955 u32 vmx_instruction_info, bool wr, gva_t *ret)
Bandan Das19677e32014-05-06 02:19:15 -04006956{
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00006957 gva_t off;
6958 bool exn;
6959 struct kvm_segment s;
6960
Bandan Das19677e32014-05-06 02:19:15 -04006961 /*
6962 * According to Vol. 3B, "Information for VM Exits Due to Instruction
6963 * Execution", on an exit, vmx_instruction_info holds most of the
6964 * addressing components of the operand. Only the displacement part
6965 * is put in exit_qualification (see 3B, "Basic VM-Exit Information").
6966 * For how an actual address is calculated from all these components,
6967 * refer to Vol. 1, "Operand Addressing".
6968 */
6969 int scaling = vmx_instruction_info & 3;
6970 int addr_size = (vmx_instruction_info >> 7) & 7;
6971 bool is_reg = vmx_instruction_info & (1u << 10);
6972 int seg_reg = (vmx_instruction_info >> 15) & 7;
6973 int index_reg = (vmx_instruction_info >> 18) & 0xf;
6974 bool index_is_valid = !(vmx_instruction_info & (1u << 22));
6975 int base_reg = (vmx_instruction_info >> 23) & 0xf;
6976 bool base_is_valid = !(vmx_instruction_info & (1u << 27));
6977
6978 if (is_reg) {
6979 kvm_queue_exception(vcpu, UD_VECTOR);
6980 return 1;
6981 }
6982
6983 /* Addr = segment_base + offset */
6984 /* offset = base + [index * scale] + displacement */
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00006985 off = exit_qualification; /* holds the displacement */
Bandan Das19677e32014-05-06 02:19:15 -04006986 if (base_is_valid)
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00006987 off += kvm_register_read(vcpu, base_reg);
Bandan Das19677e32014-05-06 02:19:15 -04006988 if (index_is_valid)
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00006989 off += kvm_register_read(vcpu, index_reg)<<scaling;
6990 vmx_get_segment(vcpu, &s, seg_reg);
6991 *ret = s.base + off;
Bandan Das19677e32014-05-06 02:19:15 -04006992
6993 if (addr_size == 1) /* 32 bit */
6994 *ret &= 0xffffffff;
6995
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00006996 /* Checks for #GP/#SS exceptions. */
6997 exn = false;
Quentin Casasnovasff30ef42016-06-18 11:01:05 +02006998 if (is_long_mode(vcpu)) {
6999 /* Long mode: #GP(0)/#SS(0) if the memory address is in a
7000 * non-canonical form. This is the only check on the memory
7001 * destination for long mode!
7002 */
7003 exn = is_noncanonical_address(*ret);
7004 } else if (is_protmode(vcpu)) {
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007005 /* Protected mode: apply checks for segment validity in the
7006 * following order:
7007 * - segment type check (#GP(0) may be thrown)
7008 * - usability check (#GP(0)/#SS(0))
7009 * - limit check (#GP(0)/#SS(0))
7010 */
7011 if (wr)
7012 /* #GP(0) if the destination operand is located in a
7013 * read-only data segment or any code segment.
7014 */
7015 exn = ((s.type & 0xa) == 0 || (s.type & 8));
7016 else
7017 /* #GP(0) if the source operand is located in an
7018 * execute-only code segment
7019 */
7020 exn = ((s.type & 0xa) == 8);
Quentin Casasnovasff30ef42016-06-18 11:01:05 +02007021 if (exn) {
7022 kvm_queue_exception_e(vcpu, GP_VECTOR, 0);
7023 return 1;
7024 }
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007025 /* Protected mode: #GP(0)/#SS(0) if the segment is unusable.
7026 */
7027 exn = (s.unusable != 0);
7028 /* Protected mode: #GP(0)/#SS(0) if the memory
7029 * operand is outside the segment limit.
7030 */
7031 exn = exn || (off + sizeof(u64) > s.limit);
7032 }
7033 if (exn) {
7034 kvm_queue_exception_e(vcpu,
7035 seg_reg == VCPU_SREG_SS ?
7036 SS_VECTOR : GP_VECTOR,
7037 0);
7038 return 1;
7039 }
7040
Bandan Das19677e32014-05-06 02:19:15 -04007041 return 0;
7042}
7043
Radim Krčmářcbf71272017-05-19 15:48:51 +02007044static int nested_vmx_get_vmptr(struct kvm_vcpu *vcpu, gpa_t *vmpointer)
Bandan Das3573e222014-05-06 02:19:16 -04007045{
7046 gva_t gva;
Bandan Das3573e222014-05-06 02:19:16 -04007047 struct x86_exception e;
Bandan Das3573e222014-05-06 02:19:16 -04007048
7049 if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007050 vmcs_read32(VMX_INSTRUCTION_INFO), false, &gva))
Bandan Das3573e222014-05-06 02:19:16 -04007051 return 1;
7052
Radim Krčmářcbf71272017-05-19 15:48:51 +02007053 if (kvm_read_guest_virt(&vcpu->arch.emulate_ctxt, gva, vmpointer,
7054 sizeof(*vmpointer), &e)) {
Bandan Das3573e222014-05-06 02:19:16 -04007055 kvm_inject_page_fault(vcpu, &e);
7056 return 1;
7057 }
7058
Bandan Das3573e222014-05-06 02:19:16 -04007059 return 0;
7060}
7061
Jim Mattsone29acc52016-11-30 12:03:43 -08007062static int enter_vmx_operation(struct kvm_vcpu *vcpu)
7063{
7064 struct vcpu_vmx *vmx = to_vmx(vcpu);
7065 struct vmcs *shadow_vmcs;
7066
7067 if (cpu_has_vmx_msr_bitmap()) {
7068 vmx->nested.msr_bitmap =
7069 (unsigned long *)__get_free_page(GFP_KERNEL);
7070 if (!vmx->nested.msr_bitmap)
7071 goto out_msr_bitmap;
7072 }
7073
7074 vmx->nested.cached_vmcs12 = kmalloc(VMCS12_SIZE, GFP_KERNEL);
7075 if (!vmx->nested.cached_vmcs12)
7076 goto out_cached_vmcs12;
7077
7078 if (enable_shadow_vmcs) {
7079 shadow_vmcs = alloc_vmcs();
7080 if (!shadow_vmcs)
7081 goto out_shadow_vmcs;
7082 /* mark vmcs as shadow */
7083 shadow_vmcs->revision_id |= (1u << 31);
7084 /* init shadow vmcs */
7085 vmcs_clear(shadow_vmcs);
7086 vmx->vmcs01.shadow_vmcs = shadow_vmcs;
7087 }
7088
7089 INIT_LIST_HEAD(&(vmx->nested.vmcs02_pool));
7090 vmx->nested.vmcs02_num = 0;
7091
7092 hrtimer_init(&vmx->nested.preemption_timer, CLOCK_MONOTONIC,
7093 HRTIMER_MODE_REL_PINNED);
7094 vmx->nested.preemption_timer.function = vmx_preemption_timer_fn;
7095
7096 vmx->nested.vmxon = true;
7097 return 0;
7098
7099out_shadow_vmcs:
7100 kfree(vmx->nested.cached_vmcs12);
7101
7102out_cached_vmcs12:
7103 free_page((unsigned long)vmx->nested.msr_bitmap);
7104
7105out_msr_bitmap:
7106 return -ENOMEM;
7107}
7108
Bandan Das3573e222014-05-06 02:19:16 -04007109/*
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007110 * Emulate the VMXON instruction.
7111 * Currently, we just remember that VMX is active, and do not save or even
7112 * inspect the argument to VMXON (the so-called "VMXON pointer") because we
7113 * do not currently need to store anything in that guest-allocated memory
7114 * region. Consequently, VMCLEAR and VMPTRLD also do not verify that the their
7115 * argument is different from the VMXON pointer (which the spec says they do).
7116 */
7117static int handle_vmon(struct kvm_vcpu *vcpu)
7118{
Jim Mattsone29acc52016-11-30 12:03:43 -08007119 int ret;
Radim Krčmářcbf71272017-05-19 15:48:51 +02007120 gpa_t vmptr;
7121 struct page *page;
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007122 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'Elb3897a42013-07-08 19:12:35 +08007123 const u64 VMXON_NEEDED_FEATURES = FEATURE_CONTROL_LOCKED
7124 | FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007125
Jim Mattson70f3aac2017-04-26 08:53:46 -07007126 /*
7127 * The Intel VMX Instruction Reference lists a bunch of bits that are
7128 * prerequisite to running VMXON, most notably cr4.VMXE must be set to
7129 * 1 (see vmx_set_cr4() for when we allow the guest to set this).
7130 * Otherwise, we should fail with #UD. But most faulting conditions
7131 * have already been checked by hardware, prior to the VM-exit for
7132 * VMXON. We do test guest cr4.VMXE because processor CR4 always has
7133 * that bit set to 1 in non-root mode.
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007134 */
Jim Mattson70f3aac2017-04-26 08:53:46 -07007135 if (!kvm_read_cr4_bits(vcpu, X86_CR4_VMXE)) {
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007136 kvm_queue_exception(vcpu, UD_VECTOR);
7137 return 1;
7138 }
7139
Abel Gordon145c28d2013-04-18 14:36:55 +03007140 if (vmx->nested.vmxon) {
7141 nested_vmx_failValid(vcpu, VMXERR_VMXON_IN_VMX_ROOT_OPERATION);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007142 return kvm_skip_emulated_instruction(vcpu);
Abel Gordon145c28d2013-04-18 14:36:55 +03007143 }
Nadav Har'Elb3897a42013-07-08 19:12:35 +08007144
Haozhong Zhang3b840802016-06-22 14:59:54 +08007145 if ((vmx->msr_ia32_feature_control & VMXON_NEEDED_FEATURES)
Nadav Har'Elb3897a42013-07-08 19:12:35 +08007146 != VMXON_NEEDED_FEATURES) {
7147 kvm_inject_gp(vcpu, 0);
7148 return 1;
7149 }
7150
Radim Krčmářcbf71272017-05-19 15:48:51 +02007151 if (nested_vmx_get_vmptr(vcpu, &vmptr))
Jim Mattson21e7fbe2016-12-22 15:49:55 -08007152 return 1;
Radim Krčmářcbf71272017-05-19 15:48:51 +02007153
7154 /*
7155 * SDM 3: 24.11.5
7156 * The first 4 bytes of VMXON region contain the supported
7157 * VMCS revision identifier
7158 *
7159 * Note - IA32_VMX_BASIC[48] will never be 1 for the nested case;
7160 * which replaces physical address width with 32
7161 */
7162 if (!PAGE_ALIGNED(vmptr) || (vmptr >> cpuid_maxphyaddr(vcpu))) {
7163 nested_vmx_failInvalid(vcpu);
7164 return kvm_skip_emulated_instruction(vcpu);
7165 }
7166
David Hildenbrand5e2f30b2017-08-03 18:11:04 +02007167 page = kvm_vcpu_gpa_to_page(vcpu, vmptr);
7168 if (is_error_page(page)) {
Radim Krčmářcbf71272017-05-19 15:48:51 +02007169 nested_vmx_failInvalid(vcpu);
7170 return kvm_skip_emulated_instruction(vcpu);
7171 }
7172 if (*(u32 *)kmap(page) != VMCS12_REVISION) {
7173 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +02007174 kvm_release_page_clean(page);
Radim Krčmářcbf71272017-05-19 15:48:51 +02007175 nested_vmx_failInvalid(vcpu);
7176 return kvm_skip_emulated_instruction(vcpu);
7177 }
7178 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +02007179 kvm_release_page_clean(page);
Radim Krčmářcbf71272017-05-19 15:48:51 +02007180
7181 vmx->nested.vmxon_ptr = vmptr;
Jim Mattsone29acc52016-11-30 12:03:43 -08007182 ret = enter_vmx_operation(vcpu);
7183 if (ret)
7184 return ret;
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007185
Arthur Chunqi Lia25eb112013-07-04 15:03:33 +08007186 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007187 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007188}
7189
7190/*
7191 * Intel's VMX Instruction Reference specifies a common set of prerequisites
7192 * for running VMX instructions (except VMXON, whose prerequisites are
7193 * slightly different). It also specifies what exception to inject otherwise.
Jim Mattson70f3aac2017-04-26 08:53:46 -07007194 * Note that many of these exceptions have priority over VM exits, so they
7195 * don't have to be checked again here.
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007196 */
7197static int nested_vmx_check_permission(struct kvm_vcpu *vcpu)
7198{
Jim Mattson70f3aac2017-04-26 08:53:46 -07007199 if (!to_vmx(vcpu)->nested.vmxon) {
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007200 kvm_queue_exception(vcpu, UD_VECTOR);
7201 return 0;
7202 }
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007203 return 1;
7204}
7205
David Matlack8ca44e82017-08-01 14:00:39 -07007206static void vmx_disable_shadow_vmcs(struct vcpu_vmx *vmx)
7207{
7208 vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL, SECONDARY_EXEC_SHADOW_VMCS);
7209 vmcs_write64(VMCS_LINK_POINTER, -1ull);
7210}
7211
Abel Gordone7953d72013-04-18 14:37:55 +03007212static inline void nested_release_vmcs12(struct vcpu_vmx *vmx)
7213{
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02007214 if (vmx->nested.current_vmptr == -1ull)
7215 return;
7216
Abel Gordon012f83c2013-04-18 14:39:25 +03007217 if (enable_shadow_vmcs) {
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02007218 /* copy to memory all shadowed fields in case
7219 they were modified */
7220 copy_shadow_to_vmcs12(vmx);
7221 vmx->nested.sync_shadow_vmcs = false;
David Matlack8ca44e82017-08-01 14:00:39 -07007222 vmx_disable_shadow_vmcs(vmx);
Abel Gordon012f83c2013-04-18 14:39:25 +03007223 }
Wincy Van705699a2015-02-03 23:58:17 +08007224 vmx->nested.posted_intr_nv = -1;
David Matlack4f2777b2016-07-13 17:16:37 -07007225
7226 /* Flush VMCS12 to guest memory */
Paolo Bonzini9f744c52017-07-27 15:54:46 +02007227 kvm_vcpu_write_guest_page(&vmx->vcpu,
7228 vmx->nested.current_vmptr >> PAGE_SHIFT,
7229 vmx->nested.cached_vmcs12, 0, VMCS12_SIZE);
David Matlack4f2777b2016-07-13 17:16:37 -07007230
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02007231 vmx->nested.current_vmptr = -1ull;
Abel Gordone7953d72013-04-18 14:37:55 +03007232}
7233
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007234/*
7235 * Free whatever needs to be freed from vmx->nested when L1 goes down, or
7236 * just stops using VMX.
7237 */
7238static void free_nested(struct vcpu_vmx *vmx)
7239{
7240 if (!vmx->nested.vmxon)
7241 return;
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02007242
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007243 vmx->nested.vmxon = false;
Wanpeng Li5c614b32015-10-13 09:18:36 -07007244 free_vpid(vmx->nested.vpid02);
David Matlack8ca44e82017-08-01 14:00:39 -07007245 vmx->nested.posted_intr_nv = -1;
7246 vmx->nested.current_vmptr = -1ull;
Radim Krčmářd048c092016-08-08 20:16:22 +02007247 if (vmx->nested.msr_bitmap) {
7248 free_page((unsigned long)vmx->nested.msr_bitmap);
7249 vmx->nested.msr_bitmap = NULL;
7250 }
Jim Mattson355f4fb2016-10-28 08:29:39 -07007251 if (enable_shadow_vmcs) {
David Matlack8ca44e82017-08-01 14:00:39 -07007252 vmx_disable_shadow_vmcs(vmx);
Jim Mattson355f4fb2016-10-28 08:29:39 -07007253 vmcs_clear(vmx->vmcs01.shadow_vmcs);
7254 free_vmcs(vmx->vmcs01.shadow_vmcs);
7255 vmx->vmcs01.shadow_vmcs = NULL;
7256 }
David Matlack4f2777b2016-07-13 17:16:37 -07007257 kfree(vmx->nested.cached_vmcs12);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03007258 /* Unpin physical memory we referred to in current vmcs02 */
7259 if (vmx->nested.apic_access_page) {
David Hildenbrand53a70da2017-08-03 18:11:05 +02007260 kvm_release_page_dirty(vmx->nested.apic_access_page);
Paolo Bonzini48d89b92014-08-26 13:27:46 +02007261 vmx->nested.apic_access_page = NULL;
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03007262 }
Wanpeng Lia7c0b072014-08-21 19:46:50 +08007263 if (vmx->nested.virtual_apic_page) {
David Hildenbrand53a70da2017-08-03 18:11:05 +02007264 kvm_release_page_dirty(vmx->nested.virtual_apic_page);
Paolo Bonzini48d89b92014-08-26 13:27:46 +02007265 vmx->nested.virtual_apic_page = NULL;
Wanpeng Lia7c0b072014-08-21 19:46:50 +08007266 }
Wincy Van705699a2015-02-03 23:58:17 +08007267 if (vmx->nested.pi_desc_page) {
7268 kunmap(vmx->nested.pi_desc_page);
David Hildenbrand53a70da2017-08-03 18:11:05 +02007269 kvm_release_page_dirty(vmx->nested.pi_desc_page);
Wincy Van705699a2015-02-03 23:58:17 +08007270 vmx->nested.pi_desc_page = NULL;
7271 vmx->nested.pi_desc = NULL;
7272 }
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +03007273
7274 nested_free_all_saved_vmcss(vmx);
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007275}
7276
7277/* Emulate the VMXOFF instruction */
7278static int handle_vmoff(struct kvm_vcpu *vcpu)
7279{
7280 if (!nested_vmx_check_permission(vcpu))
7281 return 1;
7282 free_nested(to_vmx(vcpu));
Arthur Chunqi Lia25eb112013-07-04 15:03:33 +08007283 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007284 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007285}
7286
Nadav Har'El27d6c862011-05-25 23:06:59 +03007287/* Emulate the VMCLEAR instruction */
7288static int handle_vmclear(struct kvm_vcpu *vcpu)
7289{
7290 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jim Mattson587d7e722017-03-02 12:41:48 -08007291 u32 zero = 0;
Nadav Har'El27d6c862011-05-25 23:06:59 +03007292 gpa_t vmptr;
Nadav Har'El27d6c862011-05-25 23:06:59 +03007293
7294 if (!nested_vmx_check_permission(vcpu))
7295 return 1;
7296
Radim Krčmářcbf71272017-05-19 15:48:51 +02007297 if (nested_vmx_get_vmptr(vcpu, &vmptr))
Nadav Har'El27d6c862011-05-25 23:06:59 +03007298 return 1;
7299
Radim Krčmářcbf71272017-05-19 15:48:51 +02007300 if (!PAGE_ALIGNED(vmptr) || (vmptr >> cpuid_maxphyaddr(vcpu))) {
7301 nested_vmx_failValid(vcpu, VMXERR_VMCLEAR_INVALID_ADDRESS);
7302 return kvm_skip_emulated_instruction(vcpu);
7303 }
7304
7305 if (vmptr == vmx->nested.vmxon_ptr) {
7306 nested_vmx_failValid(vcpu, VMXERR_VMCLEAR_VMXON_POINTER);
7307 return kvm_skip_emulated_instruction(vcpu);
7308 }
7309
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02007310 if (vmptr == vmx->nested.current_vmptr)
Abel Gordone7953d72013-04-18 14:37:55 +03007311 nested_release_vmcs12(vmx);
Nadav Har'El27d6c862011-05-25 23:06:59 +03007312
Jim Mattson587d7e722017-03-02 12:41:48 -08007313 kvm_vcpu_write_guest(vcpu,
7314 vmptr + offsetof(struct vmcs12, launch_state),
7315 &zero, sizeof(zero));
Nadav Har'El27d6c862011-05-25 23:06:59 +03007316
7317 nested_free_vmcs02(vmx, vmptr);
7318
Nadav Har'El27d6c862011-05-25 23:06:59 +03007319 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007320 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El27d6c862011-05-25 23:06:59 +03007321}
7322
Nadav Har'Elcd232ad2011-05-25 23:10:33 +03007323static int nested_vmx_run(struct kvm_vcpu *vcpu, bool launch);
7324
7325/* Emulate the VMLAUNCH instruction */
7326static int handle_vmlaunch(struct kvm_vcpu *vcpu)
7327{
7328 return nested_vmx_run(vcpu, true);
7329}
7330
7331/* Emulate the VMRESUME instruction */
7332static int handle_vmresume(struct kvm_vcpu *vcpu)
7333{
7334
7335 return nested_vmx_run(vcpu, false);
7336}
7337
Nadav Har'El49f705c2011-05-25 23:08:30 +03007338/*
7339 * Read a vmcs12 field. Since these can have varying lengths and we return
7340 * one type, we chose the biggest type (u64) and zero-extend the return value
7341 * to that size. Note that the caller, handle_vmread, might need to use only
7342 * some of the bits we return here (e.g., on 32-bit guests, only 32 bits of
7343 * 64-bit fields are to be returned).
7344 */
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007345static inline int vmcs12_read_any(struct kvm_vcpu *vcpu,
7346 unsigned long field, u64 *ret)
Nadav Har'El49f705c2011-05-25 23:08:30 +03007347{
7348 short offset = vmcs_field_to_offset(field);
7349 char *p;
7350
7351 if (offset < 0)
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007352 return offset;
Nadav Har'El49f705c2011-05-25 23:08:30 +03007353
7354 p = ((char *)(get_vmcs12(vcpu))) + offset;
7355
7356 switch (vmcs_field_type(field)) {
7357 case VMCS_FIELD_TYPE_NATURAL_WIDTH:
7358 *ret = *((natural_width *)p);
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007359 return 0;
Nadav Har'El49f705c2011-05-25 23:08:30 +03007360 case VMCS_FIELD_TYPE_U16:
7361 *ret = *((u16 *)p);
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007362 return 0;
Nadav Har'El49f705c2011-05-25 23:08:30 +03007363 case VMCS_FIELD_TYPE_U32:
7364 *ret = *((u32 *)p);
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007365 return 0;
Nadav Har'El49f705c2011-05-25 23:08:30 +03007366 case VMCS_FIELD_TYPE_U64:
7367 *ret = *((u64 *)p);
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007368 return 0;
Nadav Har'El49f705c2011-05-25 23:08:30 +03007369 default:
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007370 WARN_ON(1);
7371 return -ENOENT;
Nadav Har'El49f705c2011-05-25 23:08:30 +03007372 }
7373}
7374
Abel Gordon20b97fe2013-04-18 14:36:25 +03007375
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007376static inline int vmcs12_write_any(struct kvm_vcpu *vcpu,
7377 unsigned long field, u64 field_value){
Abel Gordon20b97fe2013-04-18 14:36:25 +03007378 short offset = vmcs_field_to_offset(field);
7379 char *p = ((char *) get_vmcs12(vcpu)) + offset;
7380 if (offset < 0)
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007381 return offset;
Abel Gordon20b97fe2013-04-18 14:36:25 +03007382
7383 switch (vmcs_field_type(field)) {
7384 case VMCS_FIELD_TYPE_U16:
7385 *(u16 *)p = field_value;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007386 return 0;
Abel Gordon20b97fe2013-04-18 14:36:25 +03007387 case VMCS_FIELD_TYPE_U32:
7388 *(u32 *)p = field_value;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007389 return 0;
Abel Gordon20b97fe2013-04-18 14:36:25 +03007390 case VMCS_FIELD_TYPE_U64:
7391 *(u64 *)p = field_value;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007392 return 0;
Abel Gordon20b97fe2013-04-18 14:36:25 +03007393 case VMCS_FIELD_TYPE_NATURAL_WIDTH:
7394 *(natural_width *)p = field_value;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007395 return 0;
Abel Gordon20b97fe2013-04-18 14:36:25 +03007396 default:
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007397 WARN_ON(1);
7398 return -ENOENT;
Abel Gordon20b97fe2013-04-18 14:36:25 +03007399 }
7400
7401}
7402
Abel Gordon16f5b902013-04-18 14:38:25 +03007403static void copy_shadow_to_vmcs12(struct vcpu_vmx *vmx)
7404{
7405 int i;
7406 unsigned long field;
7407 u64 field_value;
Jim Mattson355f4fb2016-10-28 08:29:39 -07007408 struct vmcs *shadow_vmcs = vmx->vmcs01.shadow_vmcs;
Mathias Krausec2bae892013-06-26 20:36:21 +02007409 const unsigned long *fields = shadow_read_write_fields;
7410 const int num_fields = max_shadow_read_write_fields;
Abel Gordon16f5b902013-04-18 14:38:25 +03007411
Jan Kiszka282da872014-10-08 18:05:39 +02007412 preempt_disable();
7413
Abel Gordon16f5b902013-04-18 14:38:25 +03007414 vmcs_load(shadow_vmcs);
7415
7416 for (i = 0; i < num_fields; i++) {
7417 field = fields[i];
7418 switch (vmcs_field_type(field)) {
7419 case VMCS_FIELD_TYPE_U16:
7420 field_value = vmcs_read16(field);
7421 break;
7422 case VMCS_FIELD_TYPE_U32:
7423 field_value = vmcs_read32(field);
7424 break;
7425 case VMCS_FIELD_TYPE_U64:
7426 field_value = vmcs_read64(field);
7427 break;
7428 case VMCS_FIELD_TYPE_NATURAL_WIDTH:
7429 field_value = vmcs_readl(field);
7430 break;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007431 default:
7432 WARN_ON(1);
7433 continue;
Abel Gordon16f5b902013-04-18 14:38:25 +03007434 }
7435 vmcs12_write_any(&vmx->vcpu, field, field_value);
7436 }
7437
7438 vmcs_clear(shadow_vmcs);
7439 vmcs_load(vmx->loaded_vmcs->vmcs);
Jan Kiszka282da872014-10-08 18:05:39 +02007440
7441 preempt_enable();
Abel Gordon16f5b902013-04-18 14:38:25 +03007442}
7443
Abel Gordonc3114422013-04-18 14:38:55 +03007444static void copy_vmcs12_to_shadow(struct vcpu_vmx *vmx)
7445{
Mathias Krausec2bae892013-06-26 20:36:21 +02007446 const unsigned long *fields[] = {
7447 shadow_read_write_fields,
7448 shadow_read_only_fields
Abel Gordonc3114422013-04-18 14:38:55 +03007449 };
Mathias Krausec2bae892013-06-26 20:36:21 +02007450 const int max_fields[] = {
Abel Gordonc3114422013-04-18 14:38:55 +03007451 max_shadow_read_write_fields,
7452 max_shadow_read_only_fields
7453 };
7454 int i, q;
7455 unsigned long field;
7456 u64 field_value = 0;
Jim Mattson355f4fb2016-10-28 08:29:39 -07007457 struct vmcs *shadow_vmcs = vmx->vmcs01.shadow_vmcs;
Abel Gordonc3114422013-04-18 14:38:55 +03007458
7459 vmcs_load(shadow_vmcs);
7460
Mathias Krausec2bae892013-06-26 20:36:21 +02007461 for (q = 0; q < ARRAY_SIZE(fields); q++) {
Abel Gordonc3114422013-04-18 14:38:55 +03007462 for (i = 0; i < max_fields[q]; i++) {
7463 field = fields[q][i];
7464 vmcs12_read_any(&vmx->vcpu, field, &field_value);
7465
7466 switch (vmcs_field_type(field)) {
7467 case VMCS_FIELD_TYPE_U16:
7468 vmcs_write16(field, (u16)field_value);
7469 break;
7470 case VMCS_FIELD_TYPE_U32:
7471 vmcs_write32(field, (u32)field_value);
7472 break;
7473 case VMCS_FIELD_TYPE_U64:
7474 vmcs_write64(field, (u64)field_value);
7475 break;
7476 case VMCS_FIELD_TYPE_NATURAL_WIDTH:
7477 vmcs_writel(field, (long)field_value);
7478 break;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007479 default:
7480 WARN_ON(1);
7481 break;
Abel Gordonc3114422013-04-18 14:38:55 +03007482 }
7483 }
7484 }
7485
7486 vmcs_clear(shadow_vmcs);
7487 vmcs_load(vmx->loaded_vmcs->vmcs);
7488}
7489
Nadav Har'El49f705c2011-05-25 23:08:30 +03007490/*
7491 * VMX instructions which assume a current vmcs12 (i.e., that VMPTRLD was
7492 * used before) all generate the same failure when it is missing.
7493 */
7494static int nested_vmx_check_vmcs12(struct kvm_vcpu *vcpu)
7495{
7496 struct vcpu_vmx *vmx = to_vmx(vcpu);
7497 if (vmx->nested.current_vmptr == -1ull) {
7498 nested_vmx_failInvalid(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03007499 return 0;
7500 }
7501 return 1;
7502}
7503
7504static int handle_vmread(struct kvm_vcpu *vcpu)
7505{
7506 unsigned long field;
7507 u64 field_value;
7508 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7509 u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
7510 gva_t gva = 0;
7511
Kyle Hueyeb277562016-11-29 12:40:39 -08007512 if (!nested_vmx_check_permission(vcpu))
Nadav Har'El49f705c2011-05-25 23:08:30 +03007513 return 1;
7514
Kyle Huey6affcbe2016-11-29 12:40:40 -08007515 if (!nested_vmx_check_vmcs12(vcpu))
7516 return kvm_skip_emulated_instruction(vcpu);
Kyle Hueyeb277562016-11-29 12:40:39 -08007517
Nadav Har'El49f705c2011-05-25 23:08:30 +03007518 /* Decode instruction info and find the field to read */
Nadav Amit27e6fb52014-06-18 17:19:26 +03007519 field = kvm_register_readl(vcpu, (((vmx_instruction_info) >> 28) & 0xf));
Nadav Har'El49f705c2011-05-25 23:08:30 +03007520 /* Read the field, zero-extended to a u64 field_value */
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007521 if (vmcs12_read_any(vcpu, field, &field_value) < 0) {
Nadav Har'El49f705c2011-05-25 23:08:30 +03007522 nested_vmx_failValid(vcpu, VMXERR_UNSUPPORTED_VMCS_COMPONENT);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007523 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03007524 }
7525 /*
7526 * Now copy part of this value to register or memory, as requested.
7527 * Note that the number of bits actually copied is 32 or 64 depending
7528 * on the guest's mode (32 or 64 bit), not on the given field's length.
7529 */
7530 if (vmx_instruction_info & (1u << 10)) {
Nadav Amit27e6fb52014-06-18 17:19:26 +03007531 kvm_register_writel(vcpu, (((vmx_instruction_info) >> 3) & 0xf),
Nadav Har'El49f705c2011-05-25 23:08:30 +03007532 field_value);
7533 } else {
7534 if (get_vmx_mem_address(vcpu, exit_qualification,
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007535 vmx_instruction_info, true, &gva))
Nadav Har'El49f705c2011-05-25 23:08:30 +03007536 return 1;
Jim Mattson70f3aac2017-04-26 08:53:46 -07007537 /* _system ok, as hardware has verified cpl=0 */
Nadav Har'El49f705c2011-05-25 23:08:30 +03007538 kvm_write_guest_virt_system(&vcpu->arch.emulate_ctxt, gva,
7539 &field_value, (is_long_mode(vcpu) ? 8 : 4), NULL);
7540 }
7541
7542 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007543 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03007544}
7545
7546
7547static int handle_vmwrite(struct kvm_vcpu *vcpu)
7548{
7549 unsigned long field;
7550 gva_t gva;
7551 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7552 u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
Nadav Har'El49f705c2011-05-25 23:08:30 +03007553 /* The value to write might be 32 or 64 bits, depending on L1's long
7554 * mode, and eventually we need to write that into a field of several
7555 * possible lengths. The code below first zero-extends the value to 64
Adam Buchbinder6a6256f2016-02-23 15:34:30 -08007556 * bit (field_value), and then copies only the appropriate number of
Nadav Har'El49f705c2011-05-25 23:08:30 +03007557 * bits into the vmcs12 field.
7558 */
7559 u64 field_value = 0;
7560 struct x86_exception e;
7561
Kyle Hueyeb277562016-11-29 12:40:39 -08007562 if (!nested_vmx_check_permission(vcpu))
Nadav Har'El49f705c2011-05-25 23:08:30 +03007563 return 1;
7564
Kyle Huey6affcbe2016-11-29 12:40:40 -08007565 if (!nested_vmx_check_vmcs12(vcpu))
7566 return kvm_skip_emulated_instruction(vcpu);
Kyle Hueyeb277562016-11-29 12:40:39 -08007567
Nadav Har'El49f705c2011-05-25 23:08:30 +03007568 if (vmx_instruction_info & (1u << 10))
Nadav Amit27e6fb52014-06-18 17:19:26 +03007569 field_value = kvm_register_readl(vcpu,
Nadav Har'El49f705c2011-05-25 23:08:30 +03007570 (((vmx_instruction_info) >> 3) & 0xf));
7571 else {
7572 if (get_vmx_mem_address(vcpu, exit_qualification,
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007573 vmx_instruction_info, false, &gva))
Nadav Har'El49f705c2011-05-25 23:08:30 +03007574 return 1;
7575 if (kvm_read_guest_virt(&vcpu->arch.emulate_ctxt, gva,
Nadav Amit27e6fb52014-06-18 17:19:26 +03007576 &field_value, (is_64_bit_mode(vcpu) ? 8 : 4), &e)) {
Nadav Har'El49f705c2011-05-25 23:08:30 +03007577 kvm_inject_page_fault(vcpu, &e);
7578 return 1;
7579 }
7580 }
7581
7582
Nadav Amit27e6fb52014-06-18 17:19:26 +03007583 field = kvm_register_readl(vcpu, (((vmx_instruction_info) >> 28) & 0xf));
Nadav Har'El49f705c2011-05-25 23:08:30 +03007584 if (vmcs_field_readonly(field)) {
7585 nested_vmx_failValid(vcpu,
7586 VMXERR_VMWRITE_READ_ONLY_VMCS_COMPONENT);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007587 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03007588 }
7589
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007590 if (vmcs12_write_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 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007596 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03007597}
7598
Jim Mattsona8bc2842016-11-30 12:03:44 -08007599static void set_current_vmptr(struct vcpu_vmx *vmx, gpa_t vmptr)
7600{
7601 vmx->nested.current_vmptr = vmptr;
7602 if (enable_shadow_vmcs) {
7603 vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL,
7604 SECONDARY_EXEC_SHADOW_VMCS);
7605 vmcs_write64(VMCS_LINK_POINTER,
7606 __pa(vmx->vmcs01.shadow_vmcs));
7607 vmx->nested.sync_shadow_vmcs = true;
7608 }
7609}
7610
Nadav Har'El63846662011-05-25 23:07:29 +03007611/* Emulate the VMPTRLD instruction */
7612static int handle_vmptrld(struct kvm_vcpu *vcpu)
7613{
7614 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'El63846662011-05-25 23:07:29 +03007615 gpa_t vmptr;
Nadav Har'El63846662011-05-25 23:07:29 +03007616
7617 if (!nested_vmx_check_permission(vcpu))
7618 return 1;
7619
Radim Krčmářcbf71272017-05-19 15:48:51 +02007620 if (nested_vmx_get_vmptr(vcpu, &vmptr))
Nadav Har'El63846662011-05-25 23:07:29 +03007621 return 1;
7622
Radim Krčmářcbf71272017-05-19 15:48:51 +02007623 if (!PAGE_ALIGNED(vmptr) || (vmptr >> cpuid_maxphyaddr(vcpu))) {
7624 nested_vmx_failValid(vcpu, VMXERR_VMPTRLD_INVALID_ADDRESS);
7625 return kvm_skip_emulated_instruction(vcpu);
7626 }
7627
7628 if (vmptr == vmx->nested.vmxon_ptr) {
7629 nested_vmx_failValid(vcpu, VMXERR_VMPTRLD_VMXON_POINTER);
7630 return kvm_skip_emulated_instruction(vcpu);
7631 }
7632
Nadav Har'El63846662011-05-25 23:07:29 +03007633 if (vmx->nested.current_vmptr != vmptr) {
7634 struct vmcs12 *new_vmcs12;
7635 struct page *page;
David Hildenbrand5e2f30b2017-08-03 18:11:04 +02007636 page = kvm_vcpu_gpa_to_page(vcpu, vmptr);
7637 if (is_error_page(page)) {
Nadav Har'El63846662011-05-25 23:07:29 +03007638 nested_vmx_failInvalid(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007639 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El63846662011-05-25 23:07:29 +03007640 }
7641 new_vmcs12 = kmap(page);
7642 if (new_vmcs12->revision_id != VMCS12_REVISION) {
7643 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +02007644 kvm_release_page_clean(page);
Nadav Har'El63846662011-05-25 23:07:29 +03007645 nested_vmx_failValid(vcpu,
7646 VMXERR_VMPTRLD_INCORRECT_VMCS_REVISION_ID);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007647 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El63846662011-05-25 23:07:29 +03007648 }
Nadav Har'El63846662011-05-25 23:07:29 +03007649
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02007650 nested_release_vmcs12(vmx);
David Matlack4f2777b2016-07-13 17:16:37 -07007651 /*
7652 * Load VMCS12 from guest memory since it is not already
7653 * cached.
7654 */
Paolo Bonzini9f744c52017-07-27 15:54:46 +02007655 memcpy(vmx->nested.cached_vmcs12, new_vmcs12, VMCS12_SIZE);
7656 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +02007657 kvm_release_page_clean(page);
Paolo Bonzini9f744c52017-07-27 15:54:46 +02007658
Jim Mattsona8bc2842016-11-30 12:03:44 -08007659 set_current_vmptr(vmx, vmptr);
Nadav Har'El63846662011-05-25 23:07:29 +03007660 }
7661
7662 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007663 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El63846662011-05-25 23:07:29 +03007664}
7665
Nadav Har'El6a4d7552011-05-25 23:08:00 +03007666/* Emulate the VMPTRST instruction */
7667static int handle_vmptrst(struct kvm_vcpu *vcpu)
7668{
7669 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7670 u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
7671 gva_t vmcs_gva;
7672 struct x86_exception e;
7673
7674 if (!nested_vmx_check_permission(vcpu))
7675 return 1;
7676
7677 if (get_vmx_mem_address(vcpu, exit_qualification,
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007678 vmx_instruction_info, true, &vmcs_gva))
Nadav Har'El6a4d7552011-05-25 23:08:00 +03007679 return 1;
Jim Mattson70f3aac2017-04-26 08:53:46 -07007680 /* ok to use *_system, as hardware has verified cpl=0 */
Nadav Har'El6a4d7552011-05-25 23:08:00 +03007681 if (kvm_write_guest_virt_system(&vcpu->arch.emulate_ctxt, vmcs_gva,
7682 (void *)&to_vmx(vcpu)->nested.current_vmptr,
7683 sizeof(u64), &e)) {
7684 kvm_inject_page_fault(vcpu, &e);
7685 return 1;
7686 }
7687 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007688 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El6a4d7552011-05-25 23:08:00 +03007689}
7690
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007691/* Emulate the INVEPT instruction */
7692static int handle_invept(struct kvm_vcpu *vcpu)
7693{
Wincy Vanb9c237b2015-02-03 23:56:30 +08007694 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007695 u32 vmx_instruction_info, types;
7696 unsigned long type;
7697 gva_t gva;
7698 struct x86_exception e;
7699 struct {
7700 u64 eptp, gpa;
7701 } operand;
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007702
Wincy Vanb9c237b2015-02-03 23:56:30 +08007703 if (!(vmx->nested.nested_vmx_secondary_ctls_high &
7704 SECONDARY_EXEC_ENABLE_EPT) ||
7705 !(vmx->nested.nested_vmx_ept_caps & VMX_EPT_INVEPT_BIT)) {
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007706 kvm_queue_exception(vcpu, UD_VECTOR);
7707 return 1;
7708 }
7709
7710 if (!nested_vmx_check_permission(vcpu))
7711 return 1;
7712
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007713 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
Nadav Amit27e6fb52014-06-18 17:19:26 +03007714 type = kvm_register_readl(vcpu, (vmx_instruction_info >> 28) & 0xf);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007715
Wincy Vanb9c237b2015-02-03 23:56:30 +08007716 types = (vmx->nested.nested_vmx_ept_caps >> VMX_EPT_EXTENT_SHIFT) & 6;
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007717
Jim Mattson85c856b2016-10-26 08:38:38 -07007718 if (type >= 32 || !(types & (1 << type))) {
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007719 nested_vmx_failValid(vcpu,
7720 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007721 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007722 }
7723
7724 /* According to the Intel VMX instruction reference, the memory
7725 * operand is read even if it isn't needed (e.g., for type==global)
7726 */
7727 if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007728 vmx_instruction_info, false, &gva))
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007729 return 1;
7730 if (kvm_read_guest_virt(&vcpu->arch.emulate_ctxt, gva, &operand,
7731 sizeof(operand), &e)) {
7732 kvm_inject_page_fault(vcpu, &e);
7733 return 1;
7734 }
7735
7736 switch (type) {
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007737 case VMX_EPT_EXTENT_GLOBAL:
Bandan Das45e11812016-08-02 16:32:36 -04007738 /*
7739 * TODO: track mappings and invalidate
7740 * single context requests appropriately
7741 */
7742 case VMX_EPT_EXTENT_CONTEXT:
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007743 kvm_mmu_sync_roots(vcpu);
Liang Chen77c39132014-09-18 12:38:37 -04007744 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007745 nested_vmx_succeed(vcpu);
7746 break;
7747 default:
7748 BUG_ON(1);
7749 break;
7750 }
7751
Kyle Huey6affcbe2016-11-29 12:40:40 -08007752 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007753}
7754
Petr Matouseka642fc32014-09-23 20:22:30 +02007755static int handle_invvpid(struct kvm_vcpu *vcpu)
7756{
Wanpeng Li99b83ac2015-10-13 09:12:21 -07007757 struct vcpu_vmx *vmx = to_vmx(vcpu);
7758 u32 vmx_instruction_info;
7759 unsigned long type, types;
7760 gva_t gva;
7761 struct x86_exception e;
Jim Mattson40352602017-06-28 09:37:37 -07007762 struct {
7763 u64 vpid;
7764 u64 gla;
7765 } operand;
Wanpeng Li99b83ac2015-10-13 09:12:21 -07007766
7767 if (!(vmx->nested.nested_vmx_secondary_ctls_high &
7768 SECONDARY_EXEC_ENABLE_VPID) ||
7769 !(vmx->nested.nested_vmx_vpid_caps & VMX_VPID_INVVPID_BIT)) {
7770 kvm_queue_exception(vcpu, UD_VECTOR);
7771 return 1;
7772 }
7773
7774 if (!nested_vmx_check_permission(vcpu))
7775 return 1;
7776
7777 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
7778 type = kvm_register_readl(vcpu, (vmx_instruction_info >> 28) & 0xf);
7779
Jan Dakinevichbcdde302016-10-28 07:00:30 +03007780 types = (vmx->nested.nested_vmx_vpid_caps &
7781 VMX_VPID_EXTENT_SUPPORTED_MASK) >> 8;
Wanpeng Li99b83ac2015-10-13 09:12:21 -07007782
Jim Mattson85c856b2016-10-26 08:38:38 -07007783 if (type >= 32 || !(types & (1 << type))) {
Wanpeng Li99b83ac2015-10-13 09:12:21 -07007784 nested_vmx_failValid(vcpu,
7785 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007786 return kvm_skip_emulated_instruction(vcpu);
Wanpeng Li99b83ac2015-10-13 09:12:21 -07007787 }
7788
7789 /* according to the intel vmx instruction reference, the memory
7790 * operand is read even if it isn't needed (e.g., for type==global)
7791 */
7792 if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
7793 vmx_instruction_info, false, &gva))
7794 return 1;
Jim Mattson40352602017-06-28 09:37:37 -07007795 if (kvm_read_guest_virt(&vcpu->arch.emulate_ctxt, gva, &operand,
7796 sizeof(operand), &e)) {
Wanpeng Li99b83ac2015-10-13 09:12:21 -07007797 kvm_inject_page_fault(vcpu, &e);
7798 return 1;
7799 }
Jim Mattson40352602017-06-28 09:37:37 -07007800 if (operand.vpid >> 16) {
7801 nested_vmx_failValid(vcpu,
7802 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
7803 return kvm_skip_emulated_instruction(vcpu);
7804 }
Wanpeng Li99b83ac2015-10-13 09:12:21 -07007805
7806 switch (type) {
Jan Dakinevichbcdde302016-10-28 07:00:30 +03007807 case VMX_VPID_EXTENT_INDIVIDUAL_ADDR:
Jim Mattson40352602017-06-28 09:37:37 -07007808 if (is_noncanonical_address(operand.gla)) {
7809 nested_vmx_failValid(vcpu,
7810 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
7811 return kvm_skip_emulated_instruction(vcpu);
7812 }
7813 /* fall through */
Paolo Bonzinief697a72016-03-18 16:58:38 +01007814 case VMX_VPID_EXTENT_SINGLE_CONTEXT:
Jan Dakinevichbcdde302016-10-28 07:00:30 +03007815 case VMX_VPID_EXTENT_SINGLE_NON_GLOBAL:
Jim Mattson40352602017-06-28 09:37:37 -07007816 if (!operand.vpid) {
Jan Dakinevichbcdde302016-10-28 07:00:30 +03007817 nested_vmx_failValid(vcpu,
7818 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007819 return kvm_skip_emulated_instruction(vcpu);
Jan Dakinevichbcdde302016-10-28 07:00:30 +03007820 }
7821 break;
Wanpeng Li99b83ac2015-10-13 09:12:21 -07007822 case VMX_VPID_EXTENT_ALL_CONTEXT:
Wanpeng Li99b83ac2015-10-13 09:12:21 -07007823 break;
7824 default:
Jan Dakinevichbcdde302016-10-28 07:00:30 +03007825 WARN_ON_ONCE(1);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007826 return kvm_skip_emulated_instruction(vcpu);
Wanpeng Li99b83ac2015-10-13 09:12:21 -07007827 }
7828
Jan Dakinevichbcdde302016-10-28 07:00:30 +03007829 __vmx_flush_tlb(vcpu, vmx->nested.vpid02);
7830 nested_vmx_succeed(vcpu);
7831
Kyle Huey6affcbe2016-11-29 12:40:40 -08007832 return kvm_skip_emulated_instruction(vcpu);
Petr Matouseka642fc32014-09-23 20:22:30 +02007833}
7834
Kai Huang843e4332015-01-28 10:54:28 +08007835static int handle_pml_full(struct kvm_vcpu *vcpu)
7836{
7837 unsigned long exit_qualification;
7838
7839 trace_kvm_pml_full(vcpu->vcpu_id);
7840
7841 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7842
7843 /*
7844 * PML buffer FULL happened while executing iret from NMI,
7845 * "blocked by NMI" bit has to be set before next VM entry.
7846 */
7847 if (!(to_vmx(vcpu)->idt_vectoring_info & VECTORING_INFO_VALID_MASK) &&
Kai Huang843e4332015-01-28 10:54:28 +08007848 (exit_qualification & INTR_INFO_UNBLOCK_NMI))
7849 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
7850 GUEST_INTR_STATE_NMI);
7851
7852 /*
7853 * PML buffer already flushed at beginning of VMEXIT. Nothing to do
7854 * here.., and there's no userspace involvement needed for PML.
7855 */
7856 return 1;
7857}
7858
Yunhong Jiang64672c92016-06-13 14:19:59 -07007859static int handle_preemption_timer(struct kvm_vcpu *vcpu)
7860{
7861 kvm_lapic_expired_hv_timer(vcpu);
7862 return 1;
7863}
7864
Bandan Das2a499e42017-08-03 15:54:41 -04007865static int handle_vmfunc(struct kvm_vcpu *vcpu)
7866{
Bandan Das27c42a12017-08-03 15:54:42 -04007867 struct vcpu_vmx *vmx = to_vmx(vcpu);
7868 struct vmcs12 *vmcs12;
7869 u32 function = vcpu->arch.regs[VCPU_REGS_RAX];
7870
7871 /*
7872 * VMFUNC is only supported for nested guests, but we always enable the
7873 * secondary control for simplicity; for non-nested mode, fake that we
7874 * didn't by injecting #UD.
7875 */
7876 if (!is_guest_mode(vcpu)) {
7877 kvm_queue_exception(vcpu, UD_VECTOR);
7878 return 1;
7879 }
7880
7881 vmcs12 = get_vmcs12(vcpu);
7882 if ((vmcs12->vm_function_control & (1 << function)) == 0)
7883 goto fail;
7884 WARN_ONCE(1, "VMCS12 VM function control should have been zero");
7885
7886fail:
7887 nested_vmx_vmexit(vcpu, vmx->exit_reason,
7888 vmcs_read32(VM_EXIT_INTR_INFO),
7889 vmcs_readl(EXIT_QUALIFICATION));
Bandan Das2a499e42017-08-03 15:54:41 -04007890 return 1;
7891}
7892
Nadav Har'El0140cae2011-05-25 23:06:28 +03007893/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08007894 * The exit handlers return 1 if the exit was handled fully and guest execution
7895 * may resume. Otherwise they set the kvm_run parameter to indicate what needs
7896 * to be done to userspace and return 0.
7897 */
Mathias Krause772e0312012-08-30 01:30:19 +02007898static int (*const kvm_vmx_exit_handlers[])(struct kvm_vcpu *vcpu) = {
Avi Kivity6aa8b732006-12-10 02:21:36 -08007899 [EXIT_REASON_EXCEPTION_NMI] = handle_exception,
7900 [EXIT_REASON_EXTERNAL_INTERRUPT] = handle_external_interrupt,
Avi Kivity988ad742007-02-12 00:54:36 -08007901 [EXIT_REASON_TRIPLE_FAULT] = handle_triple_fault,
Sheng Yangf08864b2008-05-15 18:23:25 +08007902 [EXIT_REASON_NMI_WINDOW] = handle_nmi_window,
Avi Kivity6aa8b732006-12-10 02:21:36 -08007903 [EXIT_REASON_IO_INSTRUCTION] = handle_io,
Avi Kivity6aa8b732006-12-10 02:21:36 -08007904 [EXIT_REASON_CR_ACCESS] = handle_cr,
7905 [EXIT_REASON_DR_ACCESS] = handle_dr,
7906 [EXIT_REASON_CPUID] = handle_cpuid,
7907 [EXIT_REASON_MSR_READ] = handle_rdmsr,
7908 [EXIT_REASON_MSR_WRITE] = handle_wrmsr,
7909 [EXIT_REASON_PENDING_INTERRUPT] = handle_interrupt_window,
7910 [EXIT_REASON_HLT] = handle_halt,
Gleb Natapovec25d5e2010-11-01 15:35:01 +02007911 [EXIT_REASON_INVD] = handle_invd,
Marcelo Tosattia7052892008-09-23 13:18:35 -03007912 [EXIT_REASON_INVLPG] = handle_invlpg,
Avi Kivityfee84b02011-11-10 14:57:25 +02007913 [EXIT_REASON_RDPMC] = handle_rdpmc,
Ingo Molnarc21415e2007-02-19 14:37:47 +02007914 [EXIT_REASON_VMCALL] = handle_vmcall,
Nadav Har'El27d6c862011-05-25 23:06:59 +03007915 [EXIT_REASON_VMCLEAR] = handle_vmclear,
Nadav Har'Elcd232ad2011-05-25 23:10:33 +03007916 [EXIT_REASON_VMLAUNCH] = handle_vmlaunch,
Nadav Har'El63846662011-05-25 23:07:29 +03007917 [EXIT_REASON_VMPTRLD] = handle_vmptrld,
Nadav Har'El6a4d7552011-05-25 23:08:00 +03007918 [EXIT_REASON_VMPTRST] = handle_vmptrst,
Nadav Har'El49f705c2011-05-25 23:08:30 +03007919 [EXIT_REASON_VMREAD] = handle_vmread,
Nadav Har'Elcd232ad2011-05-25 23:10:33 +03007920 [EXIT_REASON_VMRESUME] = handle_vmresume,
Nadav Har'El49f705c2011-05-25 23:08:30 +03007921 [EXIT_REASON_VMWRITE] = handle_vmwrite,
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007922 [EXIT_REASON_VMOFF] = handle_vmoff,
7923 [EXIT_REASON_VMON] = handle_vmon,
Sheng Yangf78e0e22007-10-29 09:40:42 +08007924 [EXIT_REASON_TPR_BELOW_THRESHOLD] = handle_tpr_below_threshold,
7925 [EXIT_REASON_APIC_ACCESS] = handle_apic_access,
Yang Zhang83d4c282013-01-25 10:18:49 +08007926 [EXIT_REASON_APIC_WRITE] = handle_apic_write,
Yang Zhangc7c9c562013-01-25 10:18:51 +08007927 [EXIT_REASON_EOI_INDUCED] = handle_apic_eoi_induced,
Eddie Donge5edaa02007-11-11 12:28:35 +02007928 [EXIT_REASON_WBINVD] = handle_wbinvd,
Dexuan Cui2acf9232010-06-10 11:27:12 +08007929 [EXIT_REASON_XSETBV] = handle_xsetbv,
Izik Eidus37817f22008-03-24 23:14:53 +02007930 [EXIT_REASON_TASK_SWITCH] = handle_task_switch,
Andi Kleena0861c02009-06-08 17:37:09 +08007931 [EXIT_REASON_MCE_DURING_VMENTRY] = handle_machine_check,
Marcelo Tosatti68f89402009-06-11 12:07:43 -03007932 [EXIT_REASON_EPT_VIOLATION] = handle_ept_violation,
7933 [EXIT_REASON_EPT_MISCONFIG] = handle_ept_misconfig,
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08007934 [EXIT_REASON_PAUSE_INSTRUCTION] = handle_pause,
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04007935 [EXIT_REASON_MWAIT_INSTRUCTION] = handle_mwait,
Mihai Donțu5f3d45e2015-07-05 20:08:57 +03007936 [EXIT_REASON_MONITOR_TRAP_FLAG] = handle_monitor_trap,
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04007937 [EXIT_REASON_MONITOR_INSTRUCTION] = handle_monitor,
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007938 [EXIT_REASON_INVEPT] = handle_invept,
Petr Matouseka642fc32014-09-23 20:22:30 +02007939 [EXIT_REASON_INVVPID] = handle_invvpid,
Wanpeng Lif53cd632014-12-02 19:14:58 +08007940 [EXIT_REASON_XSAVES] = handle_xsaves,
7941 [EXIT_REASON_XRSTORS] = handle_xrstors,
Kai Huang843e4332015-01-28 10:54:28 +08007942 [EXIT_REASON_PML_FULL] = handle_pml_full,
Bandan Das2a499e42017-08-03 15:54:41 -04007943 [EXIT_REASON_VMFUNC] = handle_vmfunc,
Yunhong Jiang64672c92016-06-13 14:19:59 -07007944 [EXIT_REASON_PREEMPTION_TIMER] = handle_preemption_timer,
Avi Kivity6aa8b732006-12-10 02:21:36 -08007945};
7946
7947static const int kvm_vmx_max_exit_handlers =
Robert P. J. Day50a34852007-06-03 13:35:29 -04007948 ARRAY_SIZE(kvm_vmx_exit_handlers);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007949
Jan Kiszka908a7bd2013-02-18 11:21:16 +01007950static bool nested_vmx_exit_handled_io(struct kvm_vcpu *vcpu,
7951 struct vmcs12 *vmcs12)
7952{
7953 unsigned long exit_qualification;
7954 gpa_t bitmap, last_bitmap;
7955 unsigned int port;
7956 int size;
7957 u8 b;
7958
Jan Kiszka908a7bd2013-02-18 11:21:16 +01007959 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_IO_BITMAPS))
Zhihui Zhang2f0a6392013-12-30 15:56:29 -05007960 return nested_cpu_has(vmcs12, CPU_BASED_UNCOND_IO_EXITING);
Jan Kiszka908a7bd2013-02-18 11:21:16 +01007961
7962 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7963
7964 port = exit_qualification >> 16;
7965 size = (exit_qualification & 7) + 1;
7966
7967 last_bitmap = (gpa_t)-1;
7968 b = -1;
7969
7970 while (size > 0) {
7971 if (port < 0x8000)
7972 bitmap = vmcs12->io_bitmap_a;
7973 else if (port < 0x10000)
7974 bitmap = vmcs12->io_bitmap_b;
7975 else
Joe Perches1d804d02015-03-30 16:46:09 -07007976 return true;
Jan Kiszka908a7bd2013-02-18 11:21:16 +01007977 bitmap += (port & 0x7fff) / 8;
7978
7979 if (last_bitmap != bitmap)
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02007980 if (kvm_vcpu_read_guest(vcpu, bitmap, &b, 1))
Joe Perches1d804d02015-03-30 16:46:09 -07007981 return true;
Jan Kiszka908a7bd2013-02-18 11:21:16 +01007982 if (b & (1 << (port & 7)))
Joe Perches1d804d02015-03-30 16:46:09 -07007983 return true;
Jan Kiszka908a7bd2013-02-18 11:21:16 +01007984
7985 port++;
7986 size--;
7987 last_bitmap = bitmap;
7988 }
7989
Joe Perches1d804d02015-03-30 16:46:09 -07007990 return false;
Jan Kiszka908a7bd2013-02-18 11:21:16 +01007991}
7992
Nadav Har'El644d7112011-05-25 23:12:35 +03007993/*
7994 * Return 1 if we should exit from L2 to L1 to handle an MSR access access,
7995 * rather than handle it ourselves in L0. I.e., check whether L1 expressed
7996 * disinterest in the current event (read or write a specific MSR) by using an
7997 * MSR bitmap. This may be the case even when L0 doesn't use MSR bitmaps.
7998 */
7999static bool nested_vmx_exit_handled_msr(struct kvm_vcpu *vcpu,
8000 struct vmcs12 *vmcs12, u32 exit_reason)
8001{
8002 u32 msr_index = vcpu->arch.regs[VCPU_REGS_RCX];
8003 gpa_t bitmap;
8004
Jan Kiszkacbd29cb2013-02-11 12:19:28 +01008005 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS))
Joe Perches1d804d02015-03-30 16:46:09 -07008006 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008007
8008 /*
8009 * The MSR_BITMAP page is divided into four 1024-byte bitmaps,
8010 * for the four combinations of read/write and low/high MSR numbers.
8011 * First we need to figure out which of the four to use:
8012 */
8013 bitmap = vmcs12->msr_bitmap;
8014 if (exit_reason == EXIT_REASON_MSR_WRITE)
8015 bitmap += 2048;
8016 if (msr_index >= 0xc0000000) {
8017 msr_index -= 0xc0000000;
8018 bitmap += 1024;
8019 }
8020
8021 /* Then read the msr_index'th bit from this bitmap: */
8022 if (msr_index < 1024*8) {
8023 unsigned char b;
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02008024 if (kvm_vcpu_read_guest(vcpu, bitmap + msr_index/8, &b, 1))
Joe Perches1d804d02015-03-30 16:46:09 -07008025 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008026 return 1 & (b >> (msr_index & 7));
8027 } else
Joe Perches1d804d02015-03-30 16:46:09 -07008028 return true; /* let L1 handle the wrong parameter */
Nadav Har'El644d7112011-05-25 23:12:35 +03008029}
8030
8031/*
8032 * Return 1 if we should exit from L2 to L1 to handle a CR access exit,
8033 * rather than handle it ourselves in L0. I.e., check if L1 wanted to
8034 * intercept (via guest_host_mask etc.) the current event.
8035 */
8036static bool nested_vmx_exit_handled_cr(struct kvm_vcpu *vcpu,
8037 struct vmcs12 *vmcs12)
8038{
8039 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
8040 int cr = exit_qualification & 15;
Jan H. Schönherre1d39b12017-05-20 13:22:56 +02008041 int reg;
8042 unsigned long val;
Nadav Har'El644d7112011-05-25 23:12:35 +03008043
8044 switch ((exit_qualification >> 4) & 3) {
8045 case 0: /* mov to cr */
Jan H. Schönherre1d39b12017-05-20 13:22:56 +02008046 reg = (exit_qualification >> 8) & 15;
8047 val = kvm_register_readl(vcpu, reg);
Nadav Har'El644d7112011-05-25 23:12:35 +03008048 switch (cr) {
8049 case 0:
8050 if (vmcs12->cr0_guest_host_mask &
8051 (val ^ vmcs12->cr0_read_shadow))
Joe Perches1d804d02015-03-30 16:46:09 -07008052 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008053 break;
8054 case 3:
8055 if ((vmcs12->cr3_target_count >= 1 &&
8056 vmcs12->cr3_target_value0 == val) ||
8057 (vmcs12->cr3_target_count >= 2 &&
8058 vmcs12->cr3_target_value1 == val) ||
8059 (vmcs12->cr3_target_count >= 3 &&
8060 vmcs12->cr3_target_value2 == val) ||
8061 (vmcs12->cr3_target_count >= 4 &&
8062 vmcs12->cr3_target_value3 == val))
Joe Perches1d804d02015-03-30 16:46:09 -07008063 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03008064 if (nested_cpu_has(vmcs12, CPU_BASED_CR3_LOAD_EXITING))
Joe Perches1d804d02015-03-30 16:46:09 -07008065 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008066 break;
8067 case 4:
8068 if (vmcs12->cr4_guest_host_mask &
8069 (vmcs12->cr4_read_shadow ^ val))
Joe Perches1d804d02015-03-30 16:46:09 -07008070 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008071 break;
8072 case 8:
8073 if (nested_cpu_has(vmcs12, CPU_BASED_CR8_LOAD_EXITING))
Joe Perches1d804d02015-03-30 16:46:09 -07008074 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008075 break;
8076 }
8077 break;
8078 case 2: /* clts */
8079 if ((vmcs12->cr0_guest_host_mask & X86_CR0_TS) &&
8080 (vmcs12->cr0_read_shadow & X86_CR0_TS))
Joe Perches1d804d02015-03-30 16:46:09 -07008081 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008082 break;
8083 case 1: /* mov from cr */
8084 switch (cr) {
8085 case 3:
8086 if (vmcs12->cpu_based_vm_exec_control &
8087 CPU_BASED_CR3_STORE_EXITING)
Joe Perches1d804d02015-03-30 16:46:09 -07008088 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008089 break;
8090 case 8:
8091 if (vmcs12->cpu_based_vm_exec_control &
8092 CPU_BASED_CR8_STORE_EXITING)
Joe Perches1d804d02015-03-30 16:46:09 -07008093 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008094 break;
8095 }
8096 break;
8097 case 3: /* lmsw */
8098 /*
8099 * lmsw can change bits 1..3 of cr0, and only set bit 0 of
8100 * cr0. Other attempted changes are ignored, with no exit.
8101 */
Jan H. Schönherre1d39b12017-05-20 13:22:56 +02008102 val = (exit_qualification >> LMSW_SOURCE_DATA_SHIFT) & 0x0f;
Nadav Har'El644d7112011-05-25 23:12:35 +03008103 if (vmcs12->cr0_guest_host_mask & 0xe &
8104 (val ^ vmcs12->cr0_read_shadow))
Joe Perches1d804d02015-03-30 16:46:09 -07008105 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008106 if ((vmcs12->cr0_guest_host_mask & 0x1) &&
8107 !(vmcs12->cr0_read_shadow & 0x1) &&
8108 (val & 0x1))
Joe Perches1d804d02015-03-30 16:46:09 -07008109 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008110 break;
8111 }
Joe Perches1d804d02015-03-30 16:46:09 -07008112 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03008113}
8114
8115/*
8116 * Return 1 if we should exit from L2 to L1 to handle an exit, or 0 if we
8117 * should handle it ourselves in L0 (and then continue L2). Only call this
8118 * when in is_guest_mode (L2).
8119 */
Paolo Bonzini7313c692017-07-27 10:31:25 +02008120static bool nested_vmx_exit_reflected(struct kvm_vcpu *vcpu, u32 exit_reason)
Nadav Har'El644d7112011-05-25 23:12:35 +03008121{
Nadav Har'El644d7112011-05-25 23:12:35 +03008122 u32 intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
8123 struct vcpu_vmx *vmx = to_vmx(vcpu);
8124 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
8125
Jan Kiszka542060e2014-01-04 18:47:21 +01008126 trace_kvm_nested_vmexit(kvm_rip_read(vcpu), exit_reason,
8127 vmcs_readl(EXIT_QUALIFICATION),
8128 vmx->idt_vectoring_info,
8129 intr_info,
8130 vmcs_read32(VM_EXIT_INTR_ERROR_CODE),
8131 KVM_ISA_VMX);
8132
David Matlackc9f04402017-08-01 14:00:40 -07008133 /*
8134 * The host physical addresses of some pages of guest memory
8135 * are loaded into VMCS02 (e.g. L1's Virtual APIC Page). The CPU
8136 * may write to these pages via their host physical address while
8137 * L2 is running, bypassing any address-translation-based dirty
8138 * tracking (e.g. EPT write protection).
8139 *
8140 * Mark them dirty on every exit from L2 to prevent them from
8141 * getting out of sync with dirty tracking.
8142 */
8143 nested_mark_vmcs12_pages_dirty(vcpu);
8144
Nadav Har'El644d7112011-05-25 23:12:35 +03008145 if (vmx->nested.nested_run_pending)
Joe Perches1d804d02015-03-30 16:46:09 -07008146 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03008147
8148 if (unlikely(vmx->fail)) {
Jan Kiszkabd801582011-09-12 11:26:22 +02008149 pr_info_ratelimited("%s failed vm entry %x\n", __func__,
8150 vmcs_read32(VM_INSTRUCTION_ERROR));
Joe Perches1d804d02015-03-30 16:46:09 -07008151 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008152 }
8153
8154 switch (exit_reason) {
8155 case EXIT_REASON_EXCEPTION_NMI:
Jim Mattsonef85b672016-12-12 11:01:37 -08008156 if (is_nmi(intr_info))
Joe Perches1d804d02015-03-30 16:46:09 -07008157 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03008158 else if (is_page_fault(intr_info))
Wanpeng Li52a5c152017-07-13 18:30:42 -07008159 return !vmx->vcpu.arch.apf.host_apf_reason && enable_ept;
Anthoine Bourgeoise504c902013-11-13 11:45:37 +01008160 else if (is_no_device(intr_info) &&
Paolo Bonziniccf98442014-02-27 22:54:11 +01008161 !(vmcs12->guest_cr0 & X86_CR0_TS))
Joe Perches1d804d02015-03-30 16:46:09 -07008162 return false;
Jan Kiszka6f054852016-02-09 20:15:18 +01008163 else if (is_debug(intr_info) &&
8164 vcpu->guest_debug &
8165 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))
8166 return false;
8167 else if (is_breakpoint(intr_info) &&
8168 vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
8169 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03008170 return vmcs12->exception_bitmap &
8171 (1u << (intr_info & INTR_INFO_VECTOR_MASK));
8172 case EXIT_REASON_EXTERNAL_INTERRUPT:
Joe Perches1d804d02015-03-30 16:46:09 -07008173 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03008174 case EXIT_REASON_TRIPLE_FAULT:
Joe Perches1d804d02015-03-30 16:46:09 -07008175 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008176 case EXIT_REASON_PENDING_INTERRUPT:
Jan Kiszka3b656cf2013-04-14 12:12:45 +02008177 return nested_cpu_has(vmcs12, CPU_BASED_VIRTUAL_INTR_PENDING);
Nadav Har'El644d7112011-05-25 23:12:35 +03008178 case EXIT_REASON_NMI_WINDOW:
Jan Kiszka3b656cf2013-04-14 12:12:45 +02008179 return nested_cpu_has(vmcs12, CPU_BASED_VIRTUAL_NMI_PENDING);
Nadav Har'El644d7112011-05-25 23:12:35 +03008180 case EXIT_REASON_TASK_SWITCH:
Joe Perches1d804d02015-03-30 16:46:09 -07008181 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008182 case EXIT_REASON_CPUID:
Joe Perches1d804d02015-03-30 16:46:09 -07008183 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008184 case EXIT_REASON_HLT:
8185 return nested_cpu_has(vmcs12, CPU_BASED_HLT_EXITING);
8186 case EXIT_REASON_INVD:
Joe Perches1d804d02015-03-30 16:46:09 -07008187 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008188 case EXIT_REASON_INVLPG:
8189 return nested_cpu_has(vmcs12, CPU_BASED_INVLPG_EXITING);
8190 case EXIT_REASON_RDPMC:
8191 return nested_cpu_has(vmcs12, CPU_BASED_RDPMC_EXITING);
Paolo Bonzinia5f46452017-03-30 11:55:32 +02008192 case EXIT_REASON_RDRAND:
8193 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_RDRAND);
8194 case EXIT_REASON_RDSEED:
8195 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_RDSEED);
Jan Kiszkab3a2a902015-03-23 19:27:19 +01008196 case EXIT_REASON_RDTSC: case EXIT_REASON_RDTSCP:
Nadav Har'El644d7112011-05-25 23:12:35 +03008197 return nested_cpu_has(vmcs12, CPU_BASED_RDTSC_EXITING);
8198 case EXIT_REASON_VMCALL: case EXIT_REASON_VMCLEAR:
8199 case EXIT_REASON_VMLAUNCH: case EXIT_REASON_VMPTRLD:
8200 case EXIT_REASON_VMPTRST: case EXIT_REASON_VMREAD:
8201 case EXIT_REASON_VMRESUME: case EXIT_REASON_VMWRITE:
8202 case EXIT_REASON_VMOFF: case EXIT_REASON_VMON:
Petr Matouseka642fc32014-09-23 20:22:30 +02008203 case EXIT_REASON_INVEPT: case EXIT_REASON_INVVPID:
Nadav Har'El644d7112011-05-25 23:12:35 +03008204 /*
8205 * VMX instructions trap unconditionally. This allows L1 to
8206 * emulate them for its L2 guest, i.e., allows 3-level nesting!
8207 */
Joe Perches1d804d02015-03-30 16:46:09 -07008208 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008209 case EXIT_REASON_CR_ACCESS:
8210 return nested_vmx_exit_handled_cr(vcpu, vmcs12);
8211 case EXIT_REASON_DR_ACCESS:
8212 return nested_cpu_has(vmcs12, CPU_BASED_MOV_DR_EXITING);
8213 case EXIT_REASON_IO_INSTRUCTION:
Jan Kiszka908a7bd2013-02-18 11:21:16 +01008214 return nested_vmx_exit_handled_io(vcpu, vmcs12);
Paolo Bonzini1b073042016-10-25 16:06:30 +02008215 case EXIT_REASON_GDTR_IDTR: case EXIT_REASON_LDTR_TR:
8216 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_DESC);
Nadav Har'El644d7112011-05-25 23:12:35 +03008217 case EXIT_REASON_MSR_READ:
8218 case EXIT_REASON_MSR_WRITE:
8219 return nested_vmx_exit_handled_msr(vcpu, vmcs12, exit_reason);
8220 case EXIT_REASON_INVALID_STATE:
Joe Perches1d804d02015-03-30 16:46:09 -07008221 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008222 case EXIT_REASON_MWAIT_INSTRUCTION:
8223 return nested_cpu_has(vmcs12, CPU_BASED_MWAIT_EXITING);
Mihai Donțu5f3d45e2015-07-05 20:08:57 +03008224 case EXIT_REASON_MONITOR_TRAP_FLAG:
8225 return nested_cpu_has(vmcs12, CPU_BASED_MONITOR_TRAP_FLAG);
Nadav Har'El644d7112011-05-25 23:12:35 +03008226 case EXIT_REASON_MONITOR_INSTRUCTION:
8227 return nested_cpu_has(vmcs12, CPU_BASED_MONITOR_EXITING);
8228 case EXIT_REASON_PAUSE_INSTRUCTION:
8229 return nested_cpu_has(vmcs12, CPU_BASED_PAUSE_EXITING) ||
8230 nested_cpu_has2(vmcs12,
8231 SECONDARY_EXEC_PAUSE_LOOP_EXITING);
8232 case EXIT_REASON_MCE_DURING_VMENTRY:
Joe Perches1d804d02015-03-30 16:46:09 -07008233 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03008234 case EXIT_REASON_TPR_BELOW_THRESHOLD:
Wanpeng Lia7c0b072014-08-21 19:46:50 +08008235 return nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW);
Nadav Har'El644d7112011-05-25 23:12:35 +03008236 case EXIT_REASON_APIC_ACCESS:
8237 return nested_cpu_has2(vmcs12,
8238 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES);
Wincy Van82f0dd42015-02-03 23:57:18 +08008239 case EXIT_REASON_APIC_WRITE:
Wincy Van608406e2015-02-03 23:57:51 +08008240 case EXIT_REASON_EOI_INDUCED:
8241 /* apic_write and eoi_induced should exit unconditionally. */
Joe Perches1d804d02015-03-30 16:46:09 -07008242 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008243 case EXIT_REASON_EPT_VIOLATION:
Nadav Har'El2b1be672013-08-05 11:07:19 +03008244 /*
8245 * L0 always deals with the EPT violation. If nested EPT is
8246 * used, and the nested mmu code discovers that the address is
8247 * missing in the guest EPT table (EPT12), the EPT violation
8248 * will be injected with nested_ept_inject_page_fault()
8249 */
Joe Perches1d804d02015-03-30 16:46:09 -07008250 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03008251 case EXIT_REASON_EPT_MISCONFIG:
Nadav Har'El2b1be672013-08-05 11:07:19 +03008252 /*
8253 * L2 never uses directly L1's EPT, but rather L0's own EPT
8254 * table (shadow on EPT) or a merged EPT table that L0 built
8255 * (EPT on EPT). So any problems with the structure of the
8256 * table is L0's fault.
8257 */
Joe Perches1d804d02015-03-30 16:46:09 -07008258 return false;
Paolo Bonzini90a2db62017-07-27 13:22:13 +02008259 case EXIT_REASON_INVPCID:
8260 return
8261 nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_INVPCID) &&
8262 nested_cpu_has(vmcs12, CPU_BASED_INVLPG_EXITING);
Nadav Har'El644d7112011-05-25 23:12:35 +03008263 case EXIT_REASON_WBINVD:
8264 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_WBINVD_EXITING);
8265 case EXIT_REASON_XSETBV:
Joe Perches1d804d02015-03-30 16:46:09 -07008266 return true;
Wanpeng Li81dc01f2014-12-04 19:11:07 +08008267 case EXIT_REASON_XSAVES: case EXIT_REASON_XRSTORS:
8268 /*
8269 * This should never happen, since it is not possible to
8270 * set XSS to a non-zero value---neither in L1 nor in L2.
8271 * If if it were, XSS would have to be checked against
8272 * the XSS exit bitmap in vmcs12.
8273 */
8274 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_XSAVES);
Wanpeng Li55123e32016-07-06 18:29:58 +08008275 case EXIT_REASON_PREEMPTION_TIMER:
8276 return false;
Ladi Prosekab007cc2017-03-31 10:19:26 +02008277 case EXIT_REASON_PML_FULL:
Bandan Das03efce62017-05-05 15:25:15 -04008278 /* We emulate PML support to L1. */
Ladi Prosekab007cc2017-03-31 10:19:26 +02008279 return false;
Bandan Das2a499e42017-08-03 15:54:41 -04008280 case EXIT_REASON_VMFUNC:
8281 /* VM functions are emulated through L2->L0 vmexits. */
8282 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03008283 default:
Joe Perches1d804d02015-03-30 16:46:09 -07008284 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008285 }
8286}
8287
Paolo Bonzini7313c692017-07-27 10:31:25 +02008288static int nested_vmx_reflect_vmexit(struct kvm_vcpu *vcpu, u32 exit_reason)
8289{
8290 u32 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
8291
8292 /*
8293 * At this point, the exit interruption info in exit_intr_info
8294 * is only valid for EXCEPTION_NMI exits. For EXTERNAL_INTERRUPT
8295 * we need to query the in-kernel LAPIC.
8296 */
8297 WARN_ON(exit_reason == EXIT_REASON_EXTERNAL_INTERRUPT);
8298 if ((exit_intr_info &
8299 (INTR_INFO_VALID_MASK | INTR_INFO_DELIVER_CODE_MASK)) ==
8300 (INTR_INFO_VALID_MASK | INTR_INFO_DELIVER_CODE_MASK)) {
8301 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
8302 vmcs12->vm_exit_intr_error_code =
8303 vmcs_read32(VM_EXIT_INTR_ERROR_CODE);
8304 }
8305
8306 nested_vmx_vmexit(vcpu, exit_reason, exit_intr_info,
8307 vmcs_readl(EXIT_QUALIFICATION));
8308 return 1;
8309}
8310
Avi Kivity586f9602010-11-18 13:09:54 +02008311static void vmx_get_exit_info(struct kvm_vcpu *vcpu, u64 *info1, u64 *info2)
8312{
8313 *info1 = vmcs_readl(EXIT_QUALIFICATION);
8314 *info2 = vmcs_read32(VM_EXIT_INTR_INFO);
8315}
8316
Kai Huanga3eaa862015-11-04 13:46:05 +08008317static void vmx_destroy_pml_buffer(struct vcpu_vmx *vmx)
Kai Huang843e4332015-01-28 10:54:28 +08008318{
Kai Huanga3eaa862015-11-04 13:46:05 +08008319 if (vmx->pml_pg) {
8320 __free_page(vmx->pml_pg);
8321 vmx->pml_pg = NULL;
8322 }
Kai Huang843e4332015-01-28 10:54:28 +08008323}
8324
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02008325static void vmx_flush_pml_buffer(struct kvm_vcpu *vcpu)
Kai Huang843e4332015-01-28 10:54:28 +08008326{
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02008327 struct vcpu_vmx *vmx = to_vmx(vcpu);
Kai Huang843e4332015-01-28 10:54:28 +08008328 u64 *pml_buf;
8329 u16 pml_idx;
8330
8331 pml_idx = vmcs_read16(GUEST_PML_INDEX);
8332
8333 /* Do nothing if PML buffer is empty */
8334 if (pml_idx == (PML_ENTITY_NUM - 1))
8335 return;
8336
8337 /* PML index always points to next available PML buffer entity */
8338 if (pml_idx >= PML_ENTITY_NUM)
8339 pml_idx = 0;
8340 else
8341 pml_idx++;
8342
8343 pml_buf = page_address(vmx->pml_pg);
8344 for (; pml_idx < PML_ENTITY_NUM; pml_idx++) {
8345 u64 gpa;
8346
8347 gpa = pml_buf[pml_idx];
8348 WARN_ON(gpa & (PAGE_SIZE - 1));
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02008349 kvm_vcpu_mark_page_dirty(vcpu, gpa >> PAGE_SHIFT);
Kai Huang843e4332015-01-28 10:54:28 +08008350 }
8351
8352 /* reset PML index */
8353 vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1);
8354}
8355
8356/*
8357 * Flush all vcpus' PML buffer and update logged GPAs to dirty_bitmap.
8358 * Called before reporting dirty_bitmap to userspace.
8359 */
8360static void kvm_flush_pml_buffers(struct kvm *kvm)
8361{
8362 int i;
8363 struct kvm_vcpu *vcpu;
8364 /*
8365 * We only need to kick vcpu out of guest mode here, as PML buffer
8366 * is flushed at beginning of all VMEXITs, and it's obvious that only
8367 * vcpus running in guest are possible to have unflushed GPAs in PML
8368 * buffer.
8369 */
8370 kvm_for_each_vcpu(i, vcpu, kvm)
8371 kvm_vcpu_kick(vcpu);
8372}
8373
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02008374static void vmx_dump_sel(char *name, uint32_t sel)
8375{
8376 pr_err("%s sel=0x%04x, attr=0x%05x, limit=0x%08x, base=0x%016lx\n",
Chao Peng96794e42017-02-21 03:50:01 -05008377 name, vmcs_read16(sel),
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02008378 vmcs_read32(sel + GUEST_ES_AR_BYTES - GUEST_ES_SELECTOR),
8379 vmcs_read32(sel + GUEST_ES_LIMIT - GUEST_ES_SELECTOR),
8380 vmcs_readl(sel + GUEST_ES_BASE - GUEST_ES_SELECTOR));
8381}
8382
8383static void vmx_dump_dtsel(char *name, uint32_t limit)
8384{
8385 pr_err("%s limit=0x%08x, base=0x%016lx\n",
8386 name, vmcs_read32(limit),
8387 vmcs_readl(limit + GUEST_GDTR_BASE - GUEST_GDTR_LIMIT));
8388}
8389
8390static void dump_vmcs(void)
8391{
8392 u32 vmentry_ctl = vmcs_read32(VM_ENTRY_CONTROLS);
8393 u32 vmexit_ctl = vmcs_read32(VM_EXIT_CONTROLS);
8394 u32 cpu_based_exec_ctrl = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
8395 u32 pin_based_exec_ctrl = vmcs_read32(PIN_BASED_VM_EXEC_CONTROL);
8396 u32 secondary_exec_control = 0;
8397 unsigned long cr4 = vmcs_readl(GUEST_CR4);
Paolo Bonzinif3531052015-12-03 15:49:56 +01008398 u64 efer = vmcs_read64(GUEST_IA32_EFER);
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02008399 int i, n;
8400
8401 if (cpu_has_secondary_exec_ctrls())
8402 secondary_exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
8403
8404 pr_err("*** Guest State ***\n");
8405 pr_err("CR0: actual=0x%016lx, shadow=0x%016lx, gh_mask=%016lx\n",
8406 vmcs_readl(GUEST_CR0), vmcs_readl(CR0_READ_SHADOW),
8407 vmcs_readl(CR0_GUEST_HOST_MASK));
8408 pr_err("CR4: actual=0x%016lx, shadow=0x%016lx, gh_mask=%016lx\n",
8409 cr4, vmcs_readl(CR4_READ_SHADOW), vmcs_readl(CR4_GUEST_HOST_MASK));
8410 pr_err("CR3 = 0x%016lx\n", vmcs_readl(GUEST_CR3));
8411 if ((secondary_exec_control & SECONDARY_EXEC_ENABLE_EPT) &&
8412 (cr4 & X86_CR4_PAE) && !(efer & EFER_LMA))
8413 {
Paolo Bonzini845c5b402015-12-03 15:51:00 +01008414 pr_err("PDPTR0 = 0x%016llx PDPTR1 = 0x%016llx\n",
8415 vmcs_read64(GUEST_PDPTR0), vmcs_read64(GUEST_PDPTR1));
8416 pr_err("PDPTR2 = 0x%016llx PDPTR3 = 0x%016llx\n",
8417 vmcs_read64(GUEST_PDPTR2), vmcs_read64(GUEST_PDPTR3));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02008418 }
8419 pr_err("RSP = 0x%016lx RIP = 0x%016lx\n",
8420 vmcs_readl(GUEST_RSP), vmcs_readl(GUEST_RIP));
8421 pr_err("RFLAGS=0x%08lx DR7 = 0x%016lx\n",
8422 vmcs_readl(GUEST_RFLAGS), vmcs_readl(GUEST_DR7));
8423 pr_err("Sysenter RSP=%016lx CS:RIP=%04x:%016lx\n",
8424 vmcs_readl(GUEST_SYSENTER_ESP),
8425 vmcs_read32(GUEST_SYSENTER_CS), vmcs_readl(GUEST_SYSENTER_EIP));
8426 vmx_dump_sel("CS: ", GUEST_CS_SELECTOR);
8427 vmx_dump_sel("DS: ", GUEST_DS_SELECTOR);
8428 vmx_dump_sel("SS: ", GUEST_SS_SELECTOR);
8429 vmx_dump_sel("ES: ", GUEST_ES_SELECTOR);
8430 vmx_dump_sel("FS: ", GUEST_FS_SELECTOR);
8431 vmx_dump_sel("GS: ", GUEST_GS_SELECTOR);
8432 vmx_dump_dtsel("GDTR:", GUEST_GDTR_LIMIT);
8433 vmx_dump_sel("LDTR:", GUEST_LDTR_SELECTOR);
8434 vmx_dump_dtsel("IDTR:", GUEST_IDTR_LIMIT);
8435 vmx_dump_sel("TR: ", GUEST_TR_SELECTOR);
8436 if ((vmexit_ctl & (VM_EXIT_SAVE_IA32_PAT | VM_EXIT_SAVE_IA32_EFER)) ||
8437 (vmentry_ctl & (VM_ENTRY_LOAD_IA32_PAT | VM_ENTRY_LOAD_IA32_EFER)))
Paolo Bonzini845c5b402015-12-03 15:51:00 +01008438 pr_err("EFER = 0x%016llx PAT = 0x%016llx\n",
8439 efer, vmcs_read64(GUEST_IA32_PAT));
8440 pr_err("DebugCtl = 0x%016llx DebugExceptions = 0x%016lx\n",
8441 vmcs_read64(GUEST_IA32_DEBUGCTL),
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02008442 vmcs_readl(GUEST_PENDING_DBG_EXCEPTIONS));
8443 if (vmentry_ctl & VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL)
Paolo Bonzini845c5b402015-12-03 15:51:00 +01008444 pr_err("PerfGlobCtl = 0x%016llx\n",
8445 vmcs_read64(GUEST_IA32_PERF_GLOBAL_CTRL));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02008446 if (vmentry_ctl & VM_ENTRY_LOAD_BNDCFGS)
Paolo Bonzini845c5b402015-12-03 15:51:00 +01008447 pr_err("BndCfgS = 0x%016llx\n", vmcs_read64(GUEST_BNDCFGS));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02008448 pr_err("Interruptibility = %08x ActivityState = %08x\n",
8449 vmcs_read32(GUEST_INTERRUPTIBILITY_INFO),
8450 vmcs_read32(GUEST_ACTIVITY_STATE));
8451 if (secondary_exec_control & SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY)
8452 pr_err("InterruptStatus = %04x\n",
8453 vmcs_read16(GUEST_INTR_STATUS));
8454
8455 pr_err("*** Host State ***\n");
8456 pr_err("RIP = 0x%016lx RSP = 0x%016lx\n",
8457 vmcs_readl(HOST_RIP), vmcs_readl(HOST_RSP));
8458 pr_err("CS=%04x SS=%04x DS=%04x ES=%04x FS=%04x GS=%04x TR=%04x\n",
8459 vmcs_read16(HOST_CS_SELECTOR), vmcs_read16(HOST_SS_SELECTOR),
8460 vmcs_read16(HOST_DS_SELECTOR), vmcs_read16(HOST_ES_SELECTOR),
8461 vmcs_read16(HOST_FS_SELECTOR), vmcs_read16(HOST_GS_SELECTOR),
8462 vmcs_read16(HOST_TR_SELECTOR));
8463 pr_err("FSBase=%016lx GSBase=%016lx TRBase=%016lx\n",
8464 vmcs_readl(HOST_FS_BASE), vmcs_readl(HOST_GS_BASE),
8465 vmcs_readl(HOST_TR_BASE));
8466 pr_err("GDTBase=%016lx IDTBase=%016lx\n",
8467 vmcs_readl(HOST_GDTR_BASE), vmcs_readl(HOST_IDTR_BASE));
8468 pr_err("CR0=%016lx CR3=%016lx CR4=%016lx\n",
8469 vmcs_readl(HOST_CR0), vmcs_readl(HOST_CR3),
8470 vmcs_readl(HOST_CR4));
8471 pr_err("Sysenter RSP=%016lx CS:RIP=%04x:%016lx\n",
8472 vmcs_readl(HOST_IA32_SYSENTER_ESP),
8473 vmcs_read32(HOST_IA32_SYSENTER_CS),
8474 vmcs_readl(HOST_IA32_SYSENTER_EIP));
8475 if (vmexit_ctl & (VM_EXIT_LOAD_IA32_PAT | VM_EXIT_LOAD_IA32_EFER))
Paolo Bonzini845c5b402015-12-03 15:51:00 +01008476 pr_err("EFER = 0x%016llx PAT = 0x%016llx\n",
8477 vmcs_read64(HOST_IA32_EFER),
8478 vmcs_read64(HOST_IA32_PAT));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02008479 if (vmexit_ctl & VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL)
Paolo Bonzini845c5b402015-12-03 15:51:00 +01008480 pr_err("PerfGlobCtl = 0x%016llx\n",
8481 vmcs_read64(HOST_IA32_PERF_GLOBAL_CTRL));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02008482
8483 pr_err("*** Control State ***\n");
8484 pr_err("PinBased=%08x CPUBased=%08x SecondaryExec=%08x\n",
8485 pin_based_exec_ctrl, cpu_based_exec_ctrl, secondary_exec_control);
8486 pr_err("EntryControls=%08x ExitControls=%08x\n", vmentry_ctl, vmexit_ctl);
8487 pr_err("ExceptionBitmap=%08x PFECmask=%08x PFECmatch=%08x\n",
8488 vmcs_read32(EXCEPTION_BITMAP),
8489 vmcs_read32(PAGE_FAULT_ERROR_CODE_MASK),
8490 vmcs_read32(PAGE_FAULT_ERROR_CODE_MATCH));
8491 pr_err("VMEntry: intr_info=%08x errcode=%08x ilen=%08x\n",
8492 vmcs_read32(VM_ENTRY_INTR_INFO_FIELD),
8493 vmcs_read32(VM_ENTRY_EXCEPTION_ERROR_CODE),
8494 vmcs_read32(VM_ENTRY_INSTRUCTION_LEN));
8495 pr_err("VMExit: intr_info=%08x errcode=%08x ilen=%08x\n",
8496 vmcs_read32(VM_EXIT_INTR_INFO),
8497 vmcs_read32(VM_EXIT_INTR_ERROR_CODE),
8498 vmcs_read32(VM_EXIT_INSTRUCTION_LEN));
8499 pr_err(" reason=%08x qualification=%016lx\n",
8500 vmcs_read32(VM_EXIT_REASON), vmcs_readl(EXIT_QUALIFICATION));
8501 pr_err("IDTVectoring: info=%08x errcode=%08x\n",
8502 vmcs_read32(IDT_VECTORING_INFO_FIELD),
8503 vmcs_read32(IDT_VECTORING_ERROR_CODE));
Paolo Bonzini845c5b402015-12-03 15:51:00 +01008504 pr_err("TSC Offset = 0x%016llx\n", vmcs_read64(TSC_OFFSET));
Haozhong Zhang8cfe9862015-10-20 15:39:12 +08008505 if (secondary_exec_control & SECONDARY_EXEC_TSC_SCALING)
Paolo Bonzini845c5b402015-12-03 15:51:00 +01008506 pr_err("TSC Multiplier = 0x%016llx\n",
8507 vmcs_read64(TSC_MULTIPLIER));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02008508 if (cpu_based_exec_ctrl & CPU_BASED_TPR_SHADOW)
8509 pr_err("TPR Threshold = 0x%02x\n", vmcs_read32(TPR_THRESHOLD));
8510 if (pin_based_exec_ctrl & PIN_BASED_POSTED_INTR)
8511 pr_err("PostedIntrVec = 0x%02x\n", vmcs_read16(POSTED_INTR_NV));
8512 if ((secondary_exec_control & SECONDARY_EXEC_ENABLE_EPT))
Paolo Bonzini845c5b402015-12-03 15:51:00 +01008513 pr_err("EPT pointer = 0x%016llx\n", vmcs_read64(EPT_POINTER));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02008514 n = vmcs_read32(CR3_TARGET_COUNT);
8515 for (i = 0; i + 1 < n; i += 4)
8516 pr_err("CR3 target%u=%016lx target%u=%016lx\n",
8517 i, vmcs_readl(CR3_TARGET_VALUE0 + i * 2),
8518 i + 1, vmcs_readl(CR3_TARGET_VALUE0 + i * 2 + 2));
8519 if (i < n)
8520 pr_err("CR3 target%u=%016lx\n",
8521 i, vmcs_readl(CR3_TARGET_VALUE0 + i * 2));
8522 if (secondary_exec_control & SECONDARY_EXEC_PAUSE_LOOP_EXITING)
8523 pr_err("PLE Gap=%08x Window=%08x\n",
8524 vmcs_read32(PLE_GAP), vmcs_read32(PLE_WINDOW));
8525 if (secondary_exec_control & SECONDARY_EXEC_ENABLE_VPID)
8526 pr_err("Virtual processor ID = 0x%04x\n",
8527 vmcs_read16(VIRTUAL_PROCESSOR_ID));
8528}
8529
Avi Kivity6aa8b732006-12-10 02:21:36 -08008530/*
8531 * The guest has exited. See if we can fix it or if we need userspace
8532 * assistance.
8533 */
Avi Kivity851ba692009-08-24 11:10:17 +03008534static int vmx_handle_exit(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08008535{
Avi Kivity29bd8a72007-09-10 17:27:03 +03008536 struct vcpu_vmx *vmx = to_vmx(vcpu);
Andi Kleena0861c02009-06-08 17:37:09 +08008537 u32 exit_reason = vmx->exit_reason;
Avi Kivity1155f762007-11-22 11:30:47 +02008538 u32 vectoring_info = vmx->idt_vectoring_info;
Avi Kivity29bd8a72007-09-10 17:27:03 +03008539
Paolo Bonzini8b89fe12015-12-10 18:37:32 +01008540 trace_kvm_exit(exit_reason, vcpu, KVM_ISA_VMX);
Paolo Bonzinidb1c0562016-12-08 15:31:41 +01008541 vcpu->arch.gpa_available = false;
Paolo Bonzini8b89fe12015-12-10 18:37:32 +01008542
Kai Huang843e4332015-01-28 10:54:28 +08008543 /*
8544 * Flush logged GPAs PML buffer, this will make dirty_bitmap more
8545 * updated. Another good is, in kvm_vm_ioctl_get_dirty_log, before
8546 * querying dirty_bitmap, we only need to kick all vcpus out of guest
8547 * mode as if vcpus is in root mode, the PML buffer must has been
8548 * flushed already.
8549 */
8550 if (enable_pml)
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02008551 vmx_flush_pml_buffer(vcpu);
Kai Huang843e4332015-01-28 10:54:28 +08008552
Mohammed Gamal80ced182009-09-01 12:48:18 +02008553 /* If guest state is invalid, start emulating */
Gleb Natapov14168782013-01-21 15:36:49 +02008554 if (vmx->emulation_required)
Mohammed Gamal80ced182009-09-01 12:48:18 +02008555 return handle_invalid_guest_state(vcpu);
Guillaume Thouvenin1d5a4d92008-10-29 09:39:42 +01008556
Paolo Bonzini7313c692017-07-27 10:31:25 +02008557 if (is_guest_mode(vcpu) && nested_vmx_exit_reflected(vcpu, exit_reason))
8558 return nested_vmx_reflect_vmexit(vcpu, exit_reason);
Nadav Har'El644d7112011-05-25 23:12:35 +03008559
Mohammed Gamal51207022010-05-31 22:40:54 +03008560 if (exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY) {
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02008561 dump_vmcs();
Mohammed Gamal51207022010-05-31 22:40:54 +03008562 vcpu->run->exit_reason = KVM_EXIT_FAIL_ENTRY;
8563 vcpu->run->fail_entry.hardware_entry_failure_reason
8564 = exit_reason;
8565 return 0;
8566 }
8567
Avi Kivity29bd8a72007-09-10 17:27:03 +03008568 if (unlikely(vmx->fail)) {
Avi Kivity851ba692009-08-24 11:10:17 +03008569 vcpu->run->exit_reason = KVM_EXIT_FAIL_ENTRY;
8570 vcpu->run->fail_entry.hardware_entry_failure_reason
Avi Kivity29bd8a72007-09-10 17:27:03 +03008571 = vmcs_read32(VM_INSTRUCTION_ERROR);
8572 return 0;
8573 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08008574
Xiao Guangrongb9bf6882012-10-17 13:46:52 +08008575 /*
8576 * Note:
8577 * Do not try to fix EXIT_REASON_EPT_MISCONFIG if it caused by
8578 * delivery event since it indicates guest is accessing MMIO.
8579 * The vm-exit can be triggered again after return to guest that
8580 * will cause infinite loop.
8581 */
Mike Dayd77c26f2007-10-08 09:02:08 -04008582 if ((vectoring_info & VECTORING_INFO_VALID_MASK) &&
Sheng Yang14394422008-04-28 12:24:45 +08008583 (exit_reason != EXIT_REASON_EXCEPTION_NMI &&
Jan Kiszka60637aa2008-09-26 09:30:47 +02008584 exit_reason != EXIT_REASON_EPT_VIOLATION &&
Cao, Leib244c9f2016-07-15 13:54:04 +00008585 exit_reason != EXIT_REASON_PML_FULL &&
Xiao Guangrongb9bf6882012-10-17 13:46:52 +08008586 exit_reason != EXIT_REASON_TASK_SWITCH)) {
8587 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
8588 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_DELIVERY_EV;
Paolo Bonzini70bcd702017-07-05 12:38:06 +02008589 vcpu->run->internal.ndata = 3;
Xiao Guangrongb9bf6882012-10-17 13:46:52 +08008590 vcpu->run->internal.data[0] = vectoring_info;
8591 vcpu->run->internal.data[1] = exit_reason;
Paolo Bonzini70bcd702017-07-05 12:38:06 +02008592 vcpu->run->internal.data[2] = vcpu->arch.exit_qualification;
8593 if (exit_reason == EXIT_REASON_EPT_MISCONFIG) {
8594 vcpu->run->internal.ndata++;
8595 vcpu->run->internal.data[3] =
8596 vmcs_read64(GUEST_PHYSICAL_ADDRESS);
8597 }
Xiao Guangrongb9bf6882012-10-17 13:46:52 +08008598 return 0;
8599 }
Jan Kiszka3b86cd92008-09-26 09:30:57 +02008600
Avi Kivity6aa8b732006-12-10 02:21:36 -08008601 if (exit_reason < kvm_vmx_max_exit_handlers
8602 && kvm_vmx_exit_handlers[exit_reason])
Avi Kivity851ba692009-08-24 11:10:17 +03008603 return kvm_vmx_exit_handlers[exit_reason](vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08008604 else {
Radim Krčmář6c6c5e02017-01-13 18:59:04 +01008605 vcpu_unimpl(vcpu, "vmx: unexpected exit reason 0x%x\n",
8606 exit_reason);
Michael S. Tsirkin2bc19dc2014-09-18 16:21:16 +03008607 kvm_queue_exception(vcpu, UD_VECTOR);
8608 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08008609 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08008610}
8611
Gleb Natapov95ba8273132009-04-21 17:45:08 +03008612static void update_cr8_intercept(struct kvm_vcpu *vcpu, int tpr, int irr)
Yang, Sheng6e5d8652007-09-12 18:03:11 +08008613{
Wanpeng Lia7c0b072014-08-21 19:46:50 +08008614 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
8615
8616 if (is_guest_mode(vcpu) &&
8617 nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW))
8618 return;
8619
Gleb Natapov95ba8273132009-04-21 17:45:08 +03008620 if (irr == -1 || tpr < irr) {
Yang, Sheng6e5d8652007-09-12 18:03:11 +08008621 vmcs_write32(TPR_THRESHOLD, 0);
8622 return;
8623 }
8624
Gleb Natapov95ba8273132009-04-21 17:45:08 +03008625 vmcs_write32(TPR_THRESHOLD, irr);
Yang, Sheng6e5d8652007-09-12 18:03:11 +08008626}
8627
Yang Zhang8d146952013-01-25 10:18:50 +08008628static void vmx_set_virtual_x2apic_mode(struct kvm_vcpu *vcpu, bool set)
8629{
8630 u32 sec_exec_control;
8631
Radim Krčmářdccbfcf2016-08-08 20:16:23 +02008632 /* Postpone execution until vmcs01 is the current VMCS. */
8633 if (is_guest_mode(vcpu)) {
8634 to_vmx(vcpu)->nested.change_vmcs01_virtual_x2apic_mode = true;
8635 return;
8636 }
8637
Wanpeng Lif6e90f92016-09-22 07:43:25 +08008638 if (!cpu_has_vmx_virtualize_x2apic_mode())
Yang Zhang8d146952013-01-25 10:18:50 +08008639 return;
8640
Paolo Bonzini35754c92015-07-29 12:05:37 +02008641 if (!cpu_need_tpr_shadow(vcpu))
Yang Zhang8d146952013-01-25 10:18:50 +08008642 return;
8643
8644 sec_exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
8645
8646 if (set) {
8647 sec_exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
8648 sec_exec_control |= SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
8649 } else {
8650 sec_exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
8651 sec_exec_control |= SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
Jim Mattsonfb6c8192017-03-16 13:53:59 -07008652 vmx_flush_tlb_ept_only(vcpu);
Yang Zhang8d146952013-01-25 10:18:50 +08008653 }
8654 vmcs_write32(SECONDARY_VM_EXEC_CONTROL, sec_exec_control);
8655
8656 vmx_set_msr_bitmap(vcpu);
8657}
8658
Tang Chen38b99172014-09-24 15:57:54 +08008659static void vmx_set_apic_access_page_addr(struct kvm_vcpu *vcpu, hpa_t hpa)
8660{
8661 struct vcpu_vmx *vmx = to_vmx(vcpu);
8662
8663 /*
8664 * Currently we do not handle the nested case where L2 has an
8665 * APIC access page of its own; that page is still pinned.
8666 * Hence, we skip the case where the VCPU is in guest mode _and_
8667 * L1 prepared an APIC access page for L2.
8668 *
8669 * For the case where L1 and L2 share the same APIC access page
8670 * (flexpriority=Y but SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES clear
8671 * in the vmcs12), this function will only update either the vmcs01
8672 * or the vmcs02. If the former, the vmcs02 will be updated by
8673 * prepare_vmcs02. If the latter, the vmcs01 will be updated in
8674 * the next L2->L1 exit.
8675 */
8676 if (!is_guest_mode(vcpu) ||
David Matlack4f2777b2016-07-13 17:16:37 -07008677 !nested_cpu_has2(get_vmcs12(&vmx->vcpu),
Jim Mattsonfb6c8192017-03-16 13:53:59 -07008678 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) {
Tang Chen38b99172014-09-24 15:57:54 +08008679 vmcs_write64(APIC_ACCESS_ADDR, hpa);
Jim Mattsonfb6c8192017-03-16 13:53:59 -07008680 vmx_flush_tlb_ept_only(vcpu);
8681 }
Tang Chen38b99172014-09-24 15:57:54 +08008682}
8683
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +02008684static void vmx_hwapic_isr_update(struct kvm_vcpu *vcpu, int max_isr)
Yang Zhangc7c9c562013-01-25 10:18:51 +08008685{
8686 u16 status;
8687 u8 old;
8688
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +02008689 if (max_isr == -1)
8690 max_isr = 0;
Yang Zhangc7c9c562013-01-25 10:18:51 +08008691
8692 status = vmcs_read16(GUEST_INTR_STATUS);
8693 old = status >> 8;
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +02008694 if (max_isr != old) {
Yang Zhangc7c9c562013-01-25 10:18:51 +08008695 status &= 0xff;
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +02008696 status |= max_isr << 8;
Yang Zhangc7c9c562013-01-25 10:18:51 +08008697 vmcs_write16(GUEST_INTR_STATUS, status);
8698 }
8699}
8700
8701static void vmx_set_rvi(int vector)
8702{
8703 u16 status;
8704 u8 old;
8705
Wei Wang4114c272014-11-05 10:53:43 +08008706 if (vector == -1)
8707 vector = 0;
8708
Yang Zhangc7c9c562013-01-25 10:18:51 +08008709 status = vmcs_read16(GUEST_INTR_STATUS);
8710 old = (u8)status & 0xff;
8711 if ((u8)vector != old) {
8712 status &= ~0xff;
8713 status |= (u8)vector;
8714 vmcs_write16(GUEST_INTR_STATUS, status);
8715 }
8716}
8717
8718static void vmx_hwapic_irr_update(struct kvm_vcpu *vcpu, int max_irr)
8719{
Wanpeng Li963fee12014-07-17 19:03:00 +08008720 if (!is_guest_mode(vcpu)) {
8721 vmx_set_rvi(max_irr);
8722 return;
8723 }
8724
Wei Wang4114c272014-11-05 10:53:43 +08008725 if (max_irr == -1)
8726 return;
8727
Wanpeng Li963fee12014-07-17 19:03:00 +08008728 /*
Wei Wang4114c272014-11-05 10:53:43 +08008729 * In guest mode. If a vmexit is needed, vmx_check_nested_events
8730 * handles it.
8731 */
8732 if (nested_exit_on_intr(vcpu))
8733 return;
8734
8735 /*
8736 * Else, fall back to pre-APICv interrupt injection since L2
Wanpeng Li963fee12014-07-17 19:03:00 +08008737 * is run without virtual interrupt delivery.
8738 */
8739 if (!kvm_event_needs_reinjection(vcpu) &&
8740 vmx_interrupt_allowed(vcpu)) {
8741 kvm_queue_interrupt(vcpu, max_irr, false);
8742 vmx_inject_irq(vcpu);
8743 }
Yang Zhangc7c9c562013-01-25 10:18:51 +08008744}
8745
Paolo Bonzini76dfafd52016-12-19 17:17:11 +01008746static int vmx_sync_pir_to_irr(struct kvm_vcpu *vcpu)
Paolo Bonzini810e6de2016-12-19 13:05:46 +01008747{
8748 struct vcpu_vmx *vmx = to_vmx(vcpu);
Paolo Bonzini76dfafd52016-12-19 17:17:11 +01008749 int max_irr;
Paolo Bonzini810e6de2016-12-19 13:05:46 +01008750
Paolo Bonzini76dfafd52016-12-19 17:17:11 +01008751 WARN_ON(!vcpu->arch.apicv_active);
8752 if (pi_test_on(&vmx->pi_desc)) {
8753 pi_clear_on(&vmx->pi_desc);
8754 /*
8755 * IOMMU can write to PIR.ON, so the barrier matters even on UP.
8756 * But on x86 this is just a compiler barrier anyway.
8757 */
8758 smp_mb__after_atomic();
8759 max_irr = kvm_apic_update_irr(vcpu, vmx->pi_desc.pir);
8760 } else {
8761 max_irr = kvm_lapic_find_highest_irr(vcpu);
8762 }
8763 vmx_hwapic_irr_update(vcpu, max_irr);
8764 return max_irr;
Paolo Bonzini810e6de2016-12-19 13:05:46 +01008765}
8766
Andrey Smetanin63086302015-11-10 15:36:32 +03008767static void vmx_load_eoi_exitmap(struct kvm_vcpu *vcpu, u64 *eoi_exit_bitmap)
Yang Zhangc7c9c562013-01-25 10:18:51 +08008768{
Andrey Smetanind62caab2015-11-10 15:36:33 +03008769 if (!kvm_vcpu_apicv_active(vcpu))
Yang Zhang3d81bc72013-04-11 19:25:13 +08008770 return;
8771
Yang Zhangc7c9c562013-01-25 10:18:51 +08008772 vmcs_write64(EOI_EXIT_BITMAP0, eoi_exit_bitmap[0]);
8773 vmcs_write64(EOI_EXIT_BITMAP1, eoi_exit_bitmap[1]);
8774 vmcs_write64(EOI_EXIT_BITMAP2, eoi_exit_bitmap[2]);
8775 vmcs_write64(EOI_EXIT_BITMAP3, eoi_exit_bitmap[3]);
8776}
8777
Paolo Bonzini967235d2016-12-19 14:03:45 +01008778static void vmx_apicv_post_state_restore(struct kvm_vcpu *vcpu)
8779{
8780 struct vcpu_vmx *vmx = to_vmx(vcpu);
8781
8782 pi_clear_on(&vmx->pi_desc);
8783 memset(vmx->pi_desc.pir, 0, sizeof(vmx->pi_desc.pir));
8784}
8785
Avi Kivity51aa01d2010-07-20 14:31:20 +03008786static void vmx_complete_atomic_exit(struct vcpu_vmx *vmx)
Avi Kivitycf393f72008-07-01 16:20:21 +03008787{
Jim Mattson48ae0fb2017-05-22 09:48:33 -07008788 u32 exit_intr_info = 0;
8789 u16 basic_exit_reason = (u16)vmx->exit_reason;
Avi Kivity00eba012011-03-07 17:24:54 +02008790
Jim Mattson48ae0fb2017-05-22 09:48:33 -07008791 if (!(basic_exit_reason == EXIT_REASON_MCE_DURING_VMENTRY
8792 || basic_exit_reason == EXIT_REASON_EXCEPTION_NMI))
Avi Kivity00eba012011-03-07 17:24:54 +02008793 return;
8794
Jim Mattson48ae0fb2017-05-22 09:48:33 -07008795 if (!(vmx->exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY))
8796 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
8797 vmx->exit_intr_info = exit_intr_info;
Andi Kleena0861c02009-06-08 17:37:09 +08008798
Wanpeng Li1261bfa2017-07-13 18:30:40 -07008799 /* if exit due to PF check for async PF */
8800 if (is_page_fault(exit_intr_info))
8801 vmx->vcpu.arch.apf.host_apf_reason = kvm_read_and_reset_pf_reason();
8802
Andi Kleena0861c02009-06-08 17:37:09 +08008803 /* Handle machine checks before interrupts are enabled */
Jim Mattson48ae0fb2017-05-22 09:48:33 -07008804 if (basic_exit_reason == EXIT_REASON_MCE_DURING_VMENTRY ||
8805 is_machine_check(exit_intr_info))
Andi Kleena0861c02009-06-08 17:37:09 +08008806 kvm_machine_check();
8807
Gleb Natapov20f65982009-05-11 13:35:55 +03008808 /* We need to handle NMIs before interrupts are enabled */
Jim Mattsonef85b672016-12-12 11:01:37 -08008809 if (is_nmi(exit_intr_info)) {
Zhang, Yanminff9d07a2010-04-19 13:32:45 +08008810 kvm_before_handle_nmi(&vmx->vcpu);
Gleb Natapov20f65982009-05-11 13:35:55 +03008811 asm("int $2");
Zhang, Yanminff9d07a2010-04-19 13:32:45 +08008812 kvm_after_handle_nmi(&vmx->vcpu);
8813 }
Avi Kivity51aa01d2010-07-20 14:31:20 +03008814}
Gleb Natapov20f65982009-05-11 13:35:55 +03008815
Yang Zhanga547c6d2013-04-11 19:25:10 +08008816static void vmx_handle_external_intr(struct kvm_vcpu *vcpu)
8817{
8818 u32 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
Chris J Arges3f62de52016-01-22 15:44:38 -06008819 register void *__sp asm(_ASM_SP);
Yang Zhanga547c6d2013-04-11 19:25:10 +08008820
Yang Zhanga547c6d2013-04-11 19:25:10 +08008821 if ((exit_intr_info & (INTR_INFO_VALID_MASK | INTR_INFO_INTR_TYPE_MASK))
8822 == (INTR_INFO_VALID_MASK | INTR_TYPE_EXT_INTR)) {
8823 unsigned int vector;
8824 unsigned long entry;
8825 gate_desc *desc;
8826 struct vcpu_vmx *vmx = to_vmx(vcpu);
8827#ifdef CONFIG_X86_64
8828 unsigned long tmp;
8829#endif
8830
8831 vector = exit_intr_info & INTR_INFO_VECTOR_MASK;
8832 desc = (gate_desc *)vmx->host_idt_base + vector;
8833 entry = gate_offset(*desc);
8834 asm volatile(
8835#ifdef CONFIG_X86_64
8836 "mov %%" _ASM_SP ", %[sp]\n\t"
8837 "and $0xfffffffffffffff0, %%" _ASM_SP "\n\t"
8838 "push $%c[ss]\n\t"
8839 "push %[sp]\n\t"
8840#endif
8841 "pushf\n\t"
Yang Zhanga547c6d2013-04-11 19:25:10 +08008842 __ASM_SIZE(push) " $%c[cs]\n\t"
8843 "call *%[entry]\n\t"
8844 :
8845#ifdef CONFIG_X86_64
Chris J Arges3f62de52016-01-22 15:44:38 -06008846 [sp]"=&r"(tmp),
Yang Zhanga547c6d2013-04-11 19:25:10 +08008847#endif
Chris J Arges3f62de52016-01-22 15:44:38 -06008848 "+r"(__sp)
Yang Zhanga547c6d2013-04-11 19:25:10 +08008849 :
8850 [entry]"r"(entry),
8851 [ss]"i"(__KERNEL_DS),
8852 [cs]"i"(__KERNEL_CS)
8853 );
Paolo Bonzinif2485b32016-06-15 15:23:11 +02008854 }
Yang Zhanga547c6d2013-04-11 19:25:10 +08008855}
Josh Poimboeufc207aee2017-06-28 10:11:06 -05008856STACK_FRAME_NON_STANDARD(vmx_handle_external_intr);
Yang Zhanga547c6d2013-04-11 19:25:10 +08008857
Paolo Bonzini6d396b52015-04-01 14:25:33 +02008858static bool vmx_has_high_real_mode_segbase(void)
8859{
8860 return enable_unrestricted_guest || emulate_invalid_guest_state;
8861}
8862
Liu, Jinsongda8999d2014-02-24 10:55:46 +00008863static bool vmx_mpx_supported(void)
8864{
8865 return (vmcs_config.vmexit_ctrl & VM_EXIT_CLEAR_BNDCFGS) &&
8866 (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_BNDCFGS);
8867}
8868
Wanpeng Li55412b22014-12-02 19:21:30 +08008869static bool vmx_xsaves_supported(void)
8870{
8871 return vmcs_config.cpu_based_2nd_exec_ctrl &
8872 SECONDARY_EXEC_XSAVES;
8873}
8874
Avi Kivity51aa01d2010-07-20 14:31:20 +03008875static void vmx_recover_nmi_blocking(struct vcpu_vmx *vmx)
8876{
Avi Kivityc5ca8e52011-03-07 17:37:37 +02008877 u32 exit_intr_info;
Avi Kivity51aa01d2010-07-20 14:31:20 +03008878 bool unblock_nmi;
8879 u8 vector;
8880 bool idtv_info_valid;
8881
8882 idtv_info_valid = vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK;
Gleb Natapov20f65982009-05-11 13:35:55 +03008883
Paolo Bonzini4c4a6f72017-07-14 13:36:11 +02008884 if (vmx->loaded_vmcs->nmi_known_unmasked)
Paolo Bonzini2c828782017-03-27 14:37:28 +02008885 return;
8886 /*
8887 * Can't use vmx->exit_intr_info since we're not sure what
8888 * the exit reason is.
8889 */
8890 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
8891 unblock_nmi = (exit_intr_info & INTR_INFO_UNBLOCK_NMI) != 0;
8892 vector = exit_intr_info & INTR_INFO_VECTOR_MASK;
8893 /*
8894 * SDM 3: 27.7.1.2 (September 2008)
8895 * Re-set bit "block by NMI" before VM entry if vmexit caused by
8896 * a guest IRET fault.
8897 * SDM 3: 23.2.2 (September 2008)
8898 * Bit 12 is undefined in any of the following cases:
8899 * If the VM exit sets the valid bit in the IDT-vectoring
8900 * information field.
8901 * If the VM exit is due to a double fault.
8902 */
8903 if ((exit_intr_info & INTR_INFO_VALID_MASK) && unblock_nmi &&
8904 vector != DF_VECTOR && !idtv_info_valid)
8905 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
8906 GUEST_INTR_STATE_NMI);
8907 else
Paolo Bonzini4c4a6f72017-07-14 13:36:11 +02008908 vmx->loaded_vmcs->nmi_known_unmasked =
Paolo Bonzini2c828782017-03-27 14:37:28 +02008909 !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO)
8910 & GUEST_INTR_STATE_NMI);
Avi Kivity51aa01d2010-07-20 14:31:20 +03008911}
8912
Jan Kiszka3ab66e82013-02-20 14:03:24 +01008913static void __vmx_complete_interrupts(struct kvm_vcpu *vcpu,
Avi Kivity83422e12010-07-20 14:43:23 +03008914 u32 idt_vectoring_info,
8915 int instr_len_field,
8916 int error_code_field)
Avi Kivity51aa01d2010-07-20 14:31:20 +03008917{
Avi Kivity51aa01d2010-07-20 14:31:20 +03008918 u8 vector;
8919 int type;
8920 bool idtv_info_valid;
8921
8922 idtv_info_valid = idt_vectoring_info & VECTORING_INFO_VALID_MASK;
Avi Kivity668f6122008-07-02 09:28:55 +03008923
Jan Kiszka3ab66e82013-02-20 14:03:24 +01008924 vcpu->arch.nmi_injected = false;
8925 kvm_clear_exception_queue(vcpu);
8926 kvm_clear_interrupt_queue(vcpu);
Gleb Natapov37b96e92009-03-30 16:03:13 +03008927
8928 if (!idtv_info_valid)
8929 return;
8930
Jan Kiszka3ab66e82013-02-20 14:03:24 +01008931 kvm_make_request(KVM_REQ_EVENT, vcpu);
Avi Kivity3842d132010-07-27 12:30:24 +03008932
Avi Kivity668f6122008-07-02 09:28:55 +03008933 vector = idt_vectoring_info & VECTORING_INFO_VECTOR_MASK;
8934 type = idt_vectoring_info & VECTORING_INFO_TYPE_MASK;
Gleb Natapov37b96e92009-03-30 16:03:13 +03008935
Gleb Natapov64a7ec02009-03-30 16:03:29 +03008936 switch (type) {
Gleb Natapov37b96e92009-03-30 16:03:13 +03008937 case INTR_TYPE_NMI_INTR:
Jan Kiszka3ab66e82013-02-20 14:03:24 +01008938 vcpu->arch.nmi_injected = true;
Avi Kivity668f6122008-07-02 09:28:55 +03008939 /*
Gleb Natapov7b4a25c2009-03-30 16:03:08 +03008940 * SDM 3: 27.7.1.2 (September 2008)
Gleb Natapov37b96e92009-03-30 16:03:13 +03008941 * Clear bit "block by NMI" before VM entry if a NMI
8942 * delivery faulted.
Avi Kivity668f6122008-07-02 09:28:55 +03008943 */
Jan Kiszka3ab66e82013-02-20 14:03:24 +01008944 vmx_set_nmi_mask(vcpu, false);
Gleb Natapov37b96e92009-03-30 16:03:13 +03008945 break;
Gleb Natapov37b96e92009-03-30 16:03:13 +03008946 case INTR_TYPE_SOFT_EXCEPTION:
Jan Kiszka3ab66e82013-02-20 14:03:24 +01008947 vcpu->arch.event_exit_inst_len = vmcs_read32(instr_len_field);
Gleb Natapov66fd3f72009-05-11 13:35:50 +03008948 /* fall through */
8949 case INTR_TYPE_HARD_EXCEPTION:
Avi Kivity35920a32008-07-03 14:50:12 +03008950 if (idt_vectoring_info & VECTORING_INFO_DELIVER_CODE_MASK) {
Avi Kivity83422e12010-07-20 14:43:23 +03008951 u32 err = vmcs_read32(error_code_field);
Gleb Natapov851eb6672013-09-25 12:51:34 +03008952 kvm_requeue_exception_e(vcpu, vector, err);
Avi Kivity35920a32008-07-03 14:50:12 +03008953 } else
Gleb Natapov851eb6672013-09-25 12:51:34 +03008954 kvm_requeue_exception(vcpu, vector);
Gleb Natapov37b96e92009-03-30 16:03:13 +03008955 break;
Gleb Natapov66fd3f72009-05-11 13:35:50 +03008956 case INTR_TYPE_SOFT_INTR:
Jan Kiszka3ab66e82013-02-20 14:03:24 +01008957 vcpu->arch.event_exit_inst_len = vmcs_read32(instr_len_field);
Gleb Natapov66fd3f72009-05-11 13:35:50 +03008958 /* fall through */
Gleb Natapov37b96e92009-03-30 16:03:13 +03008959 case INTR_TYPE_EXT_INTR:
Jan Kiszka3ab66e82013-02-20 14:03:24 +01008960 kvm_queue_interrupt(vcpu, vector, type == INTR_TYPE_SOFT_INTR);
Gleb Natapov37b96e92009-03-30 16:03:13 +03008961 break;
8962 default:
8963 break;
Avi Kivityf7d92382008-07-03 16:14:28 +03008964 }
Avi Kivitycf393f72008-07-01 16:20:21 +03008965}
8966
Avi Kivity83422e12010-07-20 14:43:23 +03008967static void vmx_complete_interrupts(struct vcpu_vmx *vmx)
8968{
Jan Kiszka3ab66e82013-02-20 14:03:24 +01008969 __vmx_complete_interrupts(&vmx->vcpu, vmx->idt_vectoring_info,
Avi Kivity83422e12010-07-20 14:43:23 +03008970 VM_EXIT_INSTRUCTION_LEN,
8971 IDT_VECTORING_ERROR_CODE);
8972}
8973
Avi Kivityb463a6f2010-07-20 15:06:17 +03008974static void vmx_cancel_injection(struct kvm_vcpu *vcpu)
8975{
Jan Kiszka3ab66e82013-02-20 14:03:24 +01008976 __vmx_complete_interrupts(vcpu,
Avi Kivityb463a6f2010-07-20 15:06:17 +03008977 vmcs_read32(VM_ENTRY_INTR_INFO_FIELD),
8978 VM_ENTRY_INSTRUCTION_LEN,
8979 VM_ENTRY_EXCEPTION_ERROR_CODE);
8980
8981 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0);
8982}
8983
Gleb Natapovd7cd9792011-10-05 14:01:23 +02008984static void atomic_switch_perf_msrs(struct vcpu_vmx *vmx)
8985{
8986 int i, nr_msrs;
8987 struct perf_guest_switch_msr *msrs;
8988
8989 msrs = perf_guest_get_msrs(&nr_msrs);
8990
8991 if (!msrs)
8992 return;
8993
8994 for (i = 0; i < nr_msrs; i++)
8995 if (msrs[i].host == msrs[i].guest)
8996 clear_atomic_switch_msr(vmx, msrs[i].msr);
8997 else
8998 add_atomic_switch_msr(vmx, msrs[i].msr, msrs[i].guest,
8999 msrs[i].host);
9000}
9001
Jiang Biao33365e72016-11-03 15:03:37 +08009002static void vmx_arm_hv_timer(struct kvm_vcpu *vcpu)
Yunhong Jiang64672c92016-06-13 14:19:59 -07009003{
9004 struct vcpu_vmx *vmx = to_vmx(vcpu);
9005 u64 tscl;
9006 u32 delta_tsc;
9007
9008 if (vmx->hv_deadline_tsc == -1)
9009 return;
9010
9011 tscl = rdtsc();
9012 if (vmx->hv_deadline_tsc > tscl)
9013 /* sure to be 32 bit only because checked on set_hv_timer */
9014 delta_tsc = (u32)((vmx->hv_deadline_tsc - tscl) >>
9015 cpu_preemption_timer_multi);
9016 else
9017 delta_tsc = 0;
9018
9019 vmcs_write32(VMX_PREEMPTION_TIMER_VALUE, delta_tsc);
9020}
9021
Lai Jiangshana3b5ba42011-02-11 14:29:40 +08009022static void __noclone vmx_vcpu_run(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08009023{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04009024 struct vcpu_vmx *vmx = to_vmx(vcpu);
Andy Lutomirskid6e41f12017-05-28 10:00:17 -07009025 unsigned long debugctlmsr, cr3, cr4;
Avi Kivity104f2262010-11-18 13:12:52 +02009026
Avi Kivity104f2262010-11-18 13:12:52 +02009027 /* Don't enter VMX if guest state is invalid, let the exit handler
9028 start emulation until we arrive back to a valid state */
Gleb Natapov14168782013-01-21 15:36:49 +02009029 if (vmx->emulation_required)
Avi Kivity104f2262010-11-18 13:12:52 +02009030 return;
9031
Radim Krčmářa7653ec2014-08-21 18:08:07 +02009032 if (vmx->ple_window_dirty) {
9033 vmx->ple_window_dirty = false;
9034 vmcs_write32(PLE_WINDOW, vmx->ple_window);
9035 }
9036
Abel Gordon012f83c2013-04-18 14:39:25 +03009037 if (vmx->nested.sync_shadow_vmcs) {
9038 copy_vmcs12_to_shadow(vmx);
9039 vmx->nested.sync_shadow_vmcs = false;
9040 }
9041
Avi Kivity104f2262010-11-18 13:12:52 +02009042 if (test_bit(VCPU_REGS_RSP, (unsigned long *)&vcpu->arch.regs_dirty))
9043 vmcs_writel(GUEST_RSP, vcpu->arch.regs[VCPU_REGS_RSP]);
9044 if (test_bit(VCPU_REGS_RIP, (unsigned long *)&vcpu->arch.regs_dirty))
9045 vmcs_writel(GUEST_RIP, vcpu->arch.regs[VCPU_REGS_RIP]);
9046
Andy Lutomirskid6e41f12017-05-28 10:00:17 -07009047 cr3 = __get_current_cr3_fast();
9048 if (unlikely(cr3 != vmx->host_state.vmcs_host_cr3)) {
9049 vmcs_writel(HOST_CR3, cr3);
9050 vmx->host_state.vmcs_host_cr3 = cr3;
9051 }
9052
Andy Lutomirski1e02ce42014-10-24 15:58:08 -07009053 cr4 = cr4_read_shadow();
Andy Lutomirskid974baa2014-10-08 09:02:13 -07009054 if (unlikely(cr4 != vmx->host_state.vmcs_host_cr4)) {
9055 vmcs_writel(HOST_CR4, cr4);
9056 vmx->host_state.vmcs_host_cr4 = cr4;
9057 }
9058
Avi Kivity104f2262010-11-18 13:12:52 +02009059 /* When single-stepping over STI and MOV SS, we must clear the
9060 * corresponding interruptibility bits in the guest state. Otherwise
9061 * vmentry fails as it then expects bit 14 (BS) in pending debug
9062 * exceptions being set, but that's not correct for the guest debugging
9063 * case. */
9064 if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP)
9065 vmx_set_interrupt_shadow(vcpu, 0);
9066
Xiao Guangrong1be0e612016-03-22 16:51:18 +08009067 if (vmx->guest_pkru_valid)
9068 __write_pkru(vmx->guest_pkru);
9069
Gleb Natapovd7cd9792011-10-05 14:01:23 +02009070 atomic_switch_perf_msrs(vmx);
Gleb Natapov2a7921b2012-08-12 16:12:29 +03009071 debugctlmsr = get_debugctlmsr();
Gleb Natapovd7cd9792011-10-05 14:01:23 +02009072
Yunhong Jiang64672c92016-06-13 14:19:59 -07009073 vmx_arm_hv_timer(vcpu);
9074
Nadav Har'Eld462b812011-05-24 15:26:10 +03009075 vmx->__launched = vmx->loaded_vmcs->launched;
Avi Kivity104f2262010-11-18 13:12:52 +02009076 asm(
Avi Kivity6aa8b732006-12-10 02:21:36 -08009077 /* Store host registers */
Avi Kivityb188c81f2012-09-16 15:10:58 +03009078 "push %%" _ASM_DX "; push %%" _ASM_BP ";"
9079 "push %%" _ASM_CX " \n\t" /* placeholder for guest rcx */
9080 "push %%" _ASM_CX " \n\t"
9081 "cmp %%" _ASM_SP ", %c[host_rsp](%0) \n\t"
Avi Kivity313dbd42008-07-17 18:04:30 +03009082 "je 1f \n\t"
Avi Kivityb188c81f2012-09-16 15:10:58 +03009083 "mov %%" _ASM_SP ", %c[host_rsp](%0) \n\t"
Avi Kivity4ecac3f2008-05-13 13:23:38 +03009084 __ex(ASM_VMX_VMWRITE_RSP_RDX) "\n\t"
Avi Kivity313dbd42008-07-17 18:04:30 +03009085 "1: \n\t"
Avi Kivityd3edefc2009-06-16 12:33:56 +03009086 /* Reload cr2 if changed */
Avi Kivityb188c81f2012-09-16 15:10:58 +03009087 "mov %c[cr2](%0), %%" _ASM_AX " \n\t"
9088 "mov %%cr2, %%" _ASM_DX " \n\t"
9089 "cmp %%" _ASM_AX ", %%" _ASM_DX " \n\t"
Avi Kivityd3edefc2009-06-16 12:33:56 +03009090 "je 2f \n\t"
Avi Kivityb188c81f2012-09-16 15:10:58 +03009091 "mov %%" _ASM_AX", %%cr2 \n\t"
Avi Kivityd3edefc2009-06-16 12:33:56 +03009092 "2: \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08009093 /* Check if vmlaunch of vmresume is needed */
Avi Kivitye08aa782007-11-15 18:06:18 +02009094 "cmpl $0, %c[launched](%0) \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08009095 /* Load guest registers. Don't clobber flags. */
Avi Kivityb188c81f2012-09-16 15:10:58 +03009096 "mov %c[rax](%0), %%" _ASM_AX " \n\t"
9097 "mov %c[rbx](%0), %%" _ASM_BX " \n\t"
9098 "mov %c[rdx](%0), %%" _ASM_DX " \n\t"
9099 "mov %c[rsi](%0), %%" _ASM_SI " \n\t"
9100 "mov %c[rdi](%0), %%" _ASM_DI " \n\t"
9101 "mov %c[rbp](%0), %%" _ASM_BP " \n\t"
Avi Kivity05b3e0c2006-12-13 00:33:45 -08009102#ifdef CONFIG_X86_64
Avi Kivitye08aa782007-11-15 18:06:18 +02009103 "mov %c[r8](%0), %%r8 \n\t"
9104 "mov %c[r9](%0), %%r9 \n\t"
9105 "mov %c[r10](%0), %%r10 \n\t"
9106 "mov %c[r11](%0), %%r11 \n\t"
9107 "mov %c[r12](%0), %%r12 \n\t"
9108 "mov %c[r13](%0), %%r13 \n\t"
9109 "mov %c[r14](%0), %%r14 \n\t"
9110 "mov %c[r15](%0), %%r15 \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08009111#endif
Avi Kivityb188c81f2012-09-16 15:10:58 +03009112 "mov %c[rcx](%0), %%" _ASM_CX " \n\t" /* kills %0 (ecx) */
Avi Kivityc8019492008-07-14 14:44:59 +03009113
Avi Kivity6aa8b732006-12-10 02:21:36 -08009114 /* Enter guest mode */
Avi Kivity83287ea422012-09-16 15:10:57 +03009115 "jne 1f \n\t"
Avi Kivity4ecac3f2008-05-13 13:23:38 +03009116 __ex(ASM_VMX_VMLAUNCH) "\n\t"
Avi Kivity83287ea422012-09-16 15:10:57 +03009117 "jmp 2f \n\t"
9118 "1: " __ex(ASM_VMX_VMRESUME) "\n\t"
9119 "2: "
Avi Kivity6aa8b732006-12-10 02:21:36 -08009120 /* Save guest registers, load host registers, keep flags */
Avi Kivityb188c81f2012-09-16 15:10:58 +03009121 "mov %0, %c[wordsize](%%" _ASM_SP ") \n\t"
Avi Kivity40712fa2011-01-06 18:09:12 +02009122 "pop %0 \n\t"
Avi Kivityb188c81f2012-09-16 15:10:58 +03009123 "mov %%" _ASM_AX ", %c[rax](%0) \n\t"
9124 "mov %%" _ASM_BX ", %c[rbx](%0) \n\t"
9125 __ASM_SIZE(pop) " %c[rcx](%0) \n\t"
9126 "mov %%" _ASM_DX ", %c[rdx](%0) \n\t"
9127 "mov %%" _ASM_SI ", %c[rsi](%0) \n\t"
9128 "mov %%" _ASM_DI ", %c[rdi](%0) \n\t"
9129 "mov %%" _ASM_BP ", %c[rbp](%0) \n\t"
Avi Kivity05b3e0c2006-12-13 00:33:45 -08009130#ifdef CONFIG_X86_64
Avi Kivitye08aa782007-11-15 18:06:18 +02009131 "mov %%r8, %c[r8](%0) \n\t"
9132 "mov %%r9, %c[r9](%0) \n\t"
9133 "mov %%r10, %c[r10](%0) \n\t"
9134 "mov %%r11, %c[r11](%0) \n\t"
9135 "mov %%r12, %c[r12](%0) \n\t"
9136 "mov %%r13, %c[r13](%0) \n\t"
9137 "mov %%r14, %c[r14](%0) \n\t"
9138 "mov %%r15, %c[r15](%0) \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08009139#endif
Avi Kivityb188c81f2012-09-16 15:10:58 +03009140 "mov %%cr2, %%" _ASM_AX " \n\t"
9141 "mov %%" _ASM_AX ", %c[cr2](%0) \n\t"
Avi Kivityc8019492008-07-14 14:44:59 +03009142
Avi Kivityb188c81f2012-09-16 15:10:58 +03009143 "pop %%" _ASM_BP "; pop %%" _ASM_DX " \n\t"
Avi Kivitye08aa782007-11-15 18:06:18 +02009144 "setbe %c[fail](%0) \n\t"
Avi Kivity83287ea422012-09-16 15:10:57 +03009145 ".pushsection .rodata \n\t"
9146 ".global vmx_return \n\t"
9147 "vmx_return: " _ASM_PTR " 2b \n\t"
9148 ".popsection"
Avi Kivitye08aa782007-11-15 18:06:18 +02009149 : : "c"(vmx), "d"((unsigned long)HOST_RSP),
Nadav Har'Eld462b812011-05-24 15:26:10 +03009150 [launched]"i"(offsetof(struct vcpu_vmx, __launched)),
Avi Kivitye08aa782007-11-15 18:06:18 +02009151 [fail]"i"(offsetof(struct vcpu_vmx, fail)),
Avi Kivity313dbd42008-07-17 18:04:30 +03009152 [host_rsp]"i"(offsetof(struct vcpu_vmx, host_rsp)),
Zhang Xiantaoad312c72007-12-13 23:50:52 +08009153 [rax]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RAX])),
9154 [rbx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBX])),
9155 [rcx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RCX])),
9156 [rdx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDX])),
9157 [rsi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RSI])),
9158 [rdi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDI])),
9159 [rbp]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBP])),
Avi Kivity05b3e0c2006-12-13 00:33:45 -08009160#ifdef CONFIG_X86_64
Zhang Xiantaoad312c72007-12-13 23:50:52 +08009161 [r8]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R8])),
9162 [r9]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R9])),
9163 [r10]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R10])),
9164 [r11]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R11])),
9165 [r12]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R12])),
9166 [r13]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R13])),
9167 [r14]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R14])),
9168 [r15]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R15])),
Avi Kivity6aa8b732006-12-10 02:21:36 -08009169#endif
Avi Kivity40712fa2011-01-06 18:09:12 +02009170 [cr2]"i"(offsetof(struct vcpu_vmx, vcpu.arch.cr2)),
9171 [wordsize]"i"(sizeof(ulong))
Laurent Vivierc2036302007-10-25 14:18:52 +02009172 : "cc", "memory"
9173#ifdef CONFIG_X86_64
Avi Kivityb188c81f2012-09-16 15:10:58 +03009174 , "rax", "rbx", "rdi", "rsi"
Laurent Vivierc2036302007-10-25 14:18:52 +02009175 , "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15"
Avi Kivityb188c81f2012-09-16 15:10:58 +03009176#else
9177 , "eax", "ebx", "edi", "esi"
Laurent Vivierc2036302007-10-25 14:18:52 +02009178#endif
9179 );
Avi Kivity6aa8b732006-12-10 02:21:36 -08009180
Gleb Natapov2a7921b2012-08-12 16:12:29 +03009181 /* MSR_IA32_DEBUGCTLMSR is zeroed on vmexit. Restore it if needed */
9182 if (debugctlmsr)
9183 update_debugctlmsr(debugctlmsr);
9184
Avi Kivityaa67f602012-08-01 16:48:03 +03009185#ifndef CONFIG_X86_64
9186 /*
9187 * The sysexit path does not restore ds/es, so we must set them to
9188 * a reasonable value ourselves.
9189 *
9190 * We can't defer this to vmx_load_host_state() since that function
9191 * may be executed in interrupt context, which saves and restore segments
9192 * around it, nullifying its effect.
9193 */
9194 loadsegment(ds, __USER_DS);
9195 loadsegment(es, __USER_DS);
9196#endif
9197
Avi Kivity6de4f3a2009-05-31 22:58:47 +03009198 vcpu->arch.regs_avail = ~((1 << VCPU_REGS_RIP) | (1 << VCPU_REGS_RSP)
Avi Kivity6de12732011-03-07 12:51:22 +02009199 | (1 << VCPU_EXREG_RFLAGS)
Avi Kivityaff48ba2010-12-05 18:56:11 +02009200 | (1 << VCPU_EXREG_PDPTR)
Avi Kivity2fb92db2011-04-27 19:42:18 +03009201 | (1 << VCPU_EXREG_SEGMENTS)
Avi Kivityaff48ba2010-12-05 18:56:11 +02009202 | (1 << VCPU_EXREG_CR3));
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03009203 vcpu->arch.regs_dirty = 0;
9204
Avi Kivity1155f762007-11-22 11:30:47 +02009205 vmx->idt_vectoring_info = vmcs_read32(IDT_VECTORING_INFO_FIELD);
9206
Nadav Har'Eld462b812011-05-24 15:26:10 +03009207 vmx->loaded_vmcs->launched = 1;
Avi Kivity1b6269d2007-10-09 12:12:19 +02009208
Avi Kivity51aa01d2010-07-20 14:31:20 +03009209 vmx->exit_reason = vmcs_read32(VM_EXIT_REASON);
Avi Kivity51aa01d2010-07-20 14:31:20 +03009210
Gleb Natapove0b890d2013-09-25 12:51:33 +03009211 /*
Xiao Guangrong1be0e612016-03-22 16:51:18 +08009212 * eager fpu is enabled if PKEY is supported and CR4 is switched
9213 * back on host, so it is safe to read guest PKRU from current
9214 * XSAVE.
9215 */
9216 if (boot_cpu_has(X86_FEATURE_OSPKE)) {
9217 vmx->guest_pkru = __read_pkru();
9218 if (vmx->guest_pkru != vmx->host_pkru) {
9219 vmx->guest_pkru_valid = true;
9220 __write_pkru(vmx->host_pkru);
9221 } else
9222 vmx->guest_pkru_valid = false;
9223 }
9224
9225 /*
Gleb Natapove0b890d2013-09-25 12:51:33 +03009226 * the KVM_REQ_EVENT optimization bit is only on for one entry, and if
9227 * we did not inject a still-pending event to L1 now because of
9228 * nested_run_pending, we need to re-enable this bit.
9229 */
9230 if (vmx->nested.nested_run_pending)
9231 kvm_make_request(KVM_REQ_EVENT, vcpu);
9232
9233 vmx->nested.nested_run_pending = 0;
9234
Avi Kivity51aa01d2010-07-20 14:31:20 +03009235 vmx_complete_atomic_exit(vmx);
9236 vmx_recover_nmi_blocking(vmx);
Avi Kivitycf393f72008-07-01 16:20:21 +03009237 vmx_complete_interrupts(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08009238}
Josh Poimboeufc207aee2017-06-28 10:11:06 -05009239STACK_FRAME_NON_STANDARD(vmx_vcpu_run);
Avi Kivity6aa8b732006-12-10 02:21:36 -08009240
David Hildenbrand1279a6b12017-03-20 10:00:08 +01009241static void vmx_switch_vmcs(struct kvm_vcpu *vcpu, struct loaded_vmcs *vmcs)
Paolo Bonzini4fa77342014-07-17 12:25:16 +02009242{
9243 struct vcpu_vmx *vmx = to_vmx(vcpu);
9244 int cpu;
9245
David Hildenbrand1279a6b12017-03-20 10:00:08 +01009246 if (vmx->loaded_vmcs == vmcs)
Paolo Bonzini4fa77342014-07-17 12:25:16 +02009247 return;
9248
9249 cpu = get_cpu();
David Hildenbrand1279a6b12017-03-20 10:00:08 +01009250 vmx->loaded_vmcs = vmcs;
Paolo Bonzini4fa77342014-07-17 12:25:16 +02009251 vmx_vcpu_put(vcpu);
9252 vmx_vcpu_load(vcpu, cpu);
9253 vcpu->cpu = cpu;
9254 put_cpu();
9255}
9256
Jim Mattson2f1fe812016-07-08 15:36:06 -07009257/*
9258 * Ensure that the current vmcs of the logical processor is the
9259 * vmcs01 of the vcpu before calling free_nested().
9260 */
9261static void vmx_free_vcpu_nested(struct kvm_vcpu *vcpu)
9262{
9263 struct vcpu_vmx *vmx = to_vmx(vcpu);
9264 int r;
9265
9266 r = vcpu_load(vcpu);
9267 BUG_ON(r);
David Hildenbrand1279a6b12017-03-20 10:00:08 +01009268 vmx_switch_vmcs(vcpu, &vmx->vmcs01);
Jim Mattson2f1fe812016-07-08 15:36:06 -07009269 free_nested(vmx);
9270 vcpu_put(vcpu);
9271}
9272
Avi Kivity6aa8b732006-12-10 02:21:36 -08009273static void vmx_free_vcpu(struct kvm_vcpu *vcpu)
9274{
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009275 struct vcpu_vmx *vmx = to_vmx(vcpu);
9276
Kai Huang843e4332015-01-28 10:54:28 +08009277 if (enable_pml)
Kai Huanga3eaa862015-11-04 13:46:05 +08009278 vmx_destroy_pml_buffer(vmx);
Wanpeng Li991e7a02015-09-16 17:30:05 +08009279 free_vpid(vmx->vpid);
Paolo Bonzini4fa77342014-07-17 12:25:16 +02009280 leave_guest_mode(vcpu);
Jim Mattson2f1fe812016-07-08 15:36:06 -07009281 vmx_free_vcpu_nested(vcpu);
Paolo Bonzini4fa77342014-07-17 12:25:16 +02009282 free_loaded_vmcs(vmx->loaded_vmcs);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009283 kfree(vmx->guest_msrs);
9284 kvm_vcpu_uninit(vcpu);
Rusty Russella4770342007-08-01 14:46:11 +10009285 kmem_cache_free(kvm_vcpu_cache, vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08009286}
9287
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009288static struct kvm_vcpu *vmx_create_vcpu(struct kvm *kvm, unsigned int id)
Avi Kivity6aa8b732006-12-10 02:21:36 -08009289{
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009290 int err;
Rusty Russellc16f8622007-07-30 21:12:19 +10009291 struct vcpu_vmx *vmx = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
Avi Kivity15ad7142007-07-11 18:17:21 +03009292 int cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08009293
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04009294 if (!vmx)
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009295 return ERR_PTR(-ENOMEM);
9296
Wanpeng Li991e7a02015-09-16 17:30:05 +08009297 vmx->vpid = allocate_vpid();
Sheng Yang2384d2b2008-01-17 15:14:33 +08009298
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009299 err = kvm_vcpu_init(&vmx->vcpu, kvm, id);
9300 if (err)
9301 goto free_vcpu;
Ingo Molnar965b58a2007-01-05 16:36:23 -08009302
Peter Feiner4e595162016-07-07 14:49:58 -07009303 err = -ENOMEM;
9304
9305 /*
9306 * If PML is turned on, failure on enabling PML just results in failure
9307 * of creating the vcpu, therefore we can simplify PML logic (by
9308 * avoiding dealing with cases, such as enabling PML partially on vcpus
9309 * for the guest, etc.
9310 */
9311 if (enable_pml) {
9312 vmx->pml_pg = alloc_page(GFP_KERNEL | __GFP_ZERO);
9313 if (!vmx->pml_pg)
9314 goto uninit_vcpu;
9315 }
9316
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04009317 vmx->guest_msrs = kmalloc(PAGE_SIZE, GFP_KERNEL);
Paolo Bonzini03916db2014-07-24 14:21:57 +02009318 BUILD_BUG_ON(ARRAY_SIZE(vmx_msr_index) * sizeof(vmx->guest_msrs[0])
9319 > PAGE_SIZE);
Nadav Amit0123be42014-07-24 15:06:56 +03009320
Peter Feiner4e595162016-07-07 14:49:58 -07009321 if (!vmx->guest_msrs)
9322 goto free_pml;
Ingo Molnar965b58a2007-01-05 16:36:23 -08009323
Nadav Har'Eld462b812011-05-24 15:26:10 +03009324 vmx->loaded_vmcs = &vmx->vmcs01;
9325 vmx->loaded_vmcs->vmcs = alloc_vmcs();
Jim Mattson355f4fb2016-10-28 08:29:39 -07009326 vmx->loaded_vmcs->shadow_vmcs = NULL;
Nadav Har'Eld462b812011-05-24 15:26:10 +03009327 if (!vmx->loaded_vmcs->vmcs)
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009328 goto free_msrs;
Nadav Har'Eld462b812011-05-24 15:26:10 +03009329 loaded_vmcs_init(vmx->loaded_vmcs);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04009330
Avi Kivity15ad7142007-07-11 18:17:21 +03009331 cpu = get_cpu();
9332 vmx_vcpu_load(&vmx->vcpu, cpu);
Zachary Amsdene48672f2010-08-19 22:07:23 -10009333 vmx->vcpu.cpu = cpu;
Rusty Russell8b9cf982007-07-30 16:31:43 +10009334 err = vmx_vcpu_setup(vmx);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009335 vmx_vcpu_put(&vmx->vcpu);
Avi Kivity15ad7142007-07-11 18:17:21 +03009336 put_cpu();
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009337 if (err)
9338 goto free_vmcs;
Paolo Bonzini35754c92015-07-29 12:05:37 +02009339 if (cpu_need_virtualize_apic_accesses(&vmx->vcpu)) {
Jan Kiszkabe6d05c2011-04-13 01:27:55 +02009340 err = alloc_apic_access_page(kvm);
9341 if (err)
Marcelo Tosatti5e4a0b32008-02-14 21:21:43 -02009342 goto free_vmcs;
Jan Kiszkaa63cb562013-04-08 11:07:46 +02009343 }
Ingo Molnar965b58a2007-01-05 16:36:23 -08009344
Sheng Yangb927a3c2009-07-21 10:42:48 +08009345 if (enable_ept) {
9346 if (!kvm->arch.ept_identity_map_addr)
9347 kvm->arch.ept_identity_map_addr =
9348 VMX_EPT_IDENTITY_PAGETABLE_ADDR;
Tang Chenf51770e2014-09-16 18:41:59 +08009349 err = init_rmode_identity_map(kvm);
9350 if (err)
Gleb Natapov93ea5382011-02-21 12:07:59 +02009351 goto free_vmcs;
Sheng Yangb927a3c2009-07-21 10:42:48 +08009352 }
Sheng Yangb7ebfb02008-04-25 21:44:52 +08009353
Wanpeng Li5c614b32015-10-13 09:18:36 -07009354 if (nested) {
Wincy Vanb9c237b2015-02-03 23:56:30 +08009355 nested_vmx_setup_ctls_msrs(vmx);
Wanpeng Li5c614b32015-10-13 09:18:36 -07009356 vmx->nested.vpid02 = allocate_vpid();
9357 }
Wincy Vanb9c237b2015-02-03 23:56:30 +08009358
Wincy Van705699a2015-02-03 23:58:17 +08009359 vmx->nested.posted_intr_nv = -1;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +03009360 vmx->nested.current_vmptr = -1ull;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +03009361
Haozhong Zhang37e4c992016-06-22 14:59:55 +08009362 vmx->msr_ia32_feature_control_valid_bits = FEATURE_CONTROL_LOCKED;
9363
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009364 return &vmx->vcpu;
Ingo Molnar965b58a2007-01-05 16:36:23 -08009365
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009366free_vmcs:
Wanpeng Li5c614b32015-10-13 09:18:36 -07009367 free_vpid(vmx->nested.vpid02);
Xiao Guangrong5f3fbc32012-05-14 14:58:58 +08009368 free_loaded_vmcs(vmx->loaded_vmcs);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009369free_msrs:
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009370 kfree(vmx->guest_msrs);
Peter Feiner4e595162016-07-07 14:49:58 -07009371free_pml:
9372 vmx_destroy_pml_buffer(vmx);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009373uninit_vcpu:
9374 kvm_vcpu_uninit(&vmx->vcpu);
9375free_vcpu:
Wanpeng Li991e7a02015-09-16 17:30:05 +08009376 free_vpid(vmx->vpid);
Rusty Russella4770342007-08-01 14:46:11 +10009377 kmem_cache_free(kvm_vcpu_cache, vmx);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009378 return ERR_PTR(err);
Avi Kivity6aa8b732006-12-10 02:21:36 -08009379}
9380
Yang, Sheng002c7f72007-07-31 14:23:01 +03009381static void __init vmx_check_processor_compat(void *rtn)
9382{
9383 struct vmcs_config vmcs_conf;
9384
9385 *(int *)rtn = 0;
9386 if (setup_vmcs_config(&vmcs_conf) < 0)
9387 *(int *)rtn = -EIO;
9388 if (memcmp(&vmcs_config, &vmcs_conf, sizeof(struct vmcs_config)) != 0) {
9389 printk(KERN_ERR "kvm: CPU %d feature inconsistency!\n",
9390 smp_processor_id());
9391 *(int *)rtn = -EIO;
9392 }
9393}
9394
Sheng Yang67253af2008-04-25 10:20:22 +08009395static int get_ept_level(void)
9396{
9397 return VMX_EPT_DEFAULT_GAW + 1;
9398}
9399
Sheng Yang4b12f0d2009-04-27 20:35:42 +08009400static u64 vmx_get_mt_mask(struct kvm_vcpu *vcpu, gfn_t gfn, bool is_mmio)
Sheng Yang64d4d522008-10-09 16:01:57 +08009401{
Xiao Guangrongb18d5432015-06-15 16:55:21 +08009402 u8 cache;
9403 u64 ipat = 0;
Sheng Yang4b12f0d2009-04-27 20:35:42 +08009404
Sheng Yang522c68c2009-04-27 20:35:43 +08009405 /* For VT-d and EPT combination
Paolo Bonzini606decd2015-10-01 13:12:47 +02009406 * 1. MMIO: always map as UC
Sheng Yang522c68c2009-04-27 20:35:43 +08009407 * 2. EPT with VT-d:
9408 * a. VT-d without snooping control feature: can't guarantee the
Paolo Bonzini606decd2015-10-01 13:12:47 +02009409 * result, try to trust guest.
Sheng Yang522c68c2009-04-27 20:35:43 +08009410 * b. VT-d with snooping control feature: snooping control feature of
9411 * VT-d engine can guarantee the cache correctness. Just set it
9412 * to WB to keep consistent with host. So the same as item 3.
Sheng Yanga19a6d12010-02-09 16:41:53 +08009413 * 3. EPT without VT-d: always map as WB and set IPAT=1 to keep
Sheng Yang522c68c2009-04-27 20:35:43 +08009414 * consistent with host MTRR
9415 */
Paolo Bonzini606decd2015-10-01 13:12:47 +02009416 if (is_mmio) {
9417 cache = MTRR_TYPE_UNCACHABLE;
9418 goto exit;
9419 }
9420
9421 if (!kvm_arch_has_noncoherent_dma(vcpu->kvm)) {
Xiao Guangrongb18d5432015-06-15 16:55:21 +08009422 ipat = VMX_EPT_IPAT_BIT;
9423 cache = MTRR_TYPE_WRBACK;
9424 goto exit;
9425 }
9426
9427 if (kvm_read_cr0(vcpu) & X86_CR0_CD) {
9428 ipat = VMX_EPT_IPAT_BIT;
Paolo Bonzini0da029e2015-07-23 08:24:42 +02009429 if (kvm_check_has_quirk(vcpu->kvm, KVM_X86_QUIRK_CD_NW_CLEARED))
Xiao Guangrongfb2799502015-07-16 03:25:56 +08009430 cache = MTRR_TYPE_WRBACK;
9431 else
9432 cache = MTRR_TYPE_UNCACHABLE;
Xiao Guangrongb18d5432015-06-15 16:55:21 +08009433 goto exit;
9434 }
9435
Xiao Guangrongff536042015-06-15 16:55:22 +08009436 cache = kvm_mtrr_get_guest_memory_type(vcpu, gfn);
Xiao Guangrongb18d5432015-06-15 16:55:21 +08009437
9438exit:
9439 return (cache << VMX_EPT_MT_EPTE_SHIFT) | ipat;
Sheng Yang64d4d522008-10-09 16:01:57 +08009440}
9441
Sheng Yang17cc3932010-01-05 19:02:27 +08009442static int vmx_get_lpage_level(void)
Joerg Roedel344f4142009-07-27 16:30:48 +02009443{
Sheng Yang878403b2010-01-05 19:02:29 +08009444 if (enable_ept && !cpu_has_vmx_ept_1g_page())
9445 return PT_DIRECTORY_LEVEL;
9446 else
9447 /* For shadow and EPT supported 1GB page */
9448 return PT_PDPE_LEVEL;
Joerg Roedel344f4142009-07-27 16:30:48 +02009449}
9450
Xiao Guangrongfeda8052015-09-09 14:05:55 +08009451static void vmcs_set_secondary_exec_control(u32 new_ctl)
9452{
9453 /*
9454 * These bits in the secondary execution controls field
9455 * are dynamic, the others are mostly based on the hypervisor
9456 * architecture and the guest's CPUID. Do not touch the
9457 * dynamic bits.
9458 */
9459 u32 mask =
9460 SECONDARY_EXEC_SHADOW_VMCS |
9461 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
9462 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
9463
9464 u32 cur_ctl = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
9465
9466 vmcs_write32(SECONDARY_VM_EXEC_CONTROL,
9467 (new_ctl & ~mask) | (cur_ctl & mask));
9468}
9469
David Matlack8322ebb2016-11-29 18:14:09 -08009470/*
9471 * Generate MSR_IA32_VMX_CR{0,4}_FIXED1 according to CPUID. Only set bits
9472 * (indicating "allowed-1") if they are supported in the guest's CPUID.
9473 */
9474static void nested_vmx_cr_fixed1_bits_update(struct kvm_vcpu *vcpu)
9475{
9476 struct vcpu_vmx *vmx = to_vmx(vcpu);
9477 struct kvm_cpuid_entry2 *entry;
9478
9479 vmx->nested.nested_vmx_cr0_fixed1 = 0xffffffff;
9480 vmx->nested.nested_vmx_cr4_fixed1 = X86_CR4_PCE;
9481
9482#define cr4_fixed1_update(_cr4_mask, _reg, _cpuid_mask) do { \
9483 if (entry && (entry->_reg & (_cpuid_mask))) \
9484 vmx->nested.nested_vmx_cr4_fixed1 |= (_cr4_mask); \
9485} while (0)
9486
9487 entry = kvm_find_cpuid_entry(vcpu, 0x1, 0);
9488 cr4_fixed1_update(X86_CR4_VME, edx, bit(X86_FEATURE_VME));
9489 cr4_fixed1_update(X86_CR4_PVI, edx, bit(X86_FEATURE_VME));
9490 cr4_fixed1_update(X86_CR4_TSD, edx, bit(X86_FEATURE_TSC));
9491 cr4_fixed1_update(X86_CR4_DE, edx, bit(X86_FEATURE_DE));
9492 cr4_fixed1_update(X86_CR4_PSE, edx, bit(X86_FEATURE_PSE));
9493 cr4_fixed1_update(X86_CR4_PAE, edx, bit(X86_FEATURE_PAE));
9494 cr4_fixed1_update(X86_CR4_MCE, edx, bit(X86_FEATURE_MCE));
9495 cr4_fixed1_update(X86_CR4_PGE, edx, bit(X86_FEATURE_PGE));
9496 cr4_fixed1_update(X86_CR4_OSFXSR, edx, bit(X86_FEATURE_FXSR));
9497 cr4_fixed1_update(X86_CR4_OSXMMEXCPT, edx, bit(X86_FEATURE_XMM));
9498 cr4_fixed1_update(X86_CR4_VMXE, ecx, bit(X86_FEATURE_VMX));
9499 cr4_fixed1_update(X86_CR4_SMXE, ecx, bit(X86_FEATURE_SMX));
9500 cr4_fixed1_update(X86_CR4_PCIDE, ecx, bit(X86_FEATURE_PCID));
9501 cr4_fixed1_update(X86_CR4_OSXSAVE, ecx, bit(X86_FEATURE_XSAVE));
9502
9503 entry = kvm_find_cpuid_entry(vcpu, 0x7, 0);
9504 cr4_fixed1_update(X86_CR4_FSGSBASE, ebx, bit(X86_FEATURE_FSGSBASE));
9505 cr4_fixed1_update(X86_CR4_SMEP, ebx, bit(X86_FEATURE_SMEP));
9506 cr4_fixed1_update(X86_CR4_SMAP, ebx, bit(X86_FEATURE_SMAP));
9507 cr4_fixed1_update(X86_CR4_PKE, ecx, bit(X86_FEATURE_PKU));
9508 /* TODO: Use X86_CR4_UMIP and X86_FEATURE_UMIP macros */
9509 cr4_fixed1_update(bit(11), ecx, bit(2));
9510
9511#undef cr4_fixed1_update
9512}
9513
Sheng Yang0e851882009-12-18 16:48:46 +08009514static void vmx_cpuid_update(struct kvm_vcpu *vcpu)
9515{
Sheng Yang4e47c7a2009-12-18 16:48:47 +08009516 struct vcpu_vmx *vmx = to_vmx(vcpu);
Xiao Guangrongfeda8052015-09-09 14:05:55 +08009517 u32 secondary_exec_ctl = vmx_secondary_exec_control(vmx);
Sheng Yang4e47c7a2009-12-18 16:48:47 +08009518
Sheng Yang4e47c7a2009-12-18 16:48:47 +08009519 if (vmx_rdtscp_supported()) {
Xiao Guangrong1cea0ce2015-09-09 14:05:57 +08009520 bool rdtscp_enabled = guest_cpuid_has_rdtscp(vcpu);
9521 if (!rdtscp_enabled)
Xiao Guangrongfeda8052015-09-09 14:05:55 +08009522 secondary_exec_ctl &= ~SECONDARY_EXEC_RDTSCP;
Xiao Guangrongf36201e2015-09-09 14:05:53 +08009523
Paolo Bonzini8b972652015-09-15 17:34:42 +02009524 if (nested) {
Xiao Guangrong1cea0ce2015-09-09 14:05:57 +08009525 if (rdtscp_enabled)
Paolo Bonzini8b972652015-09-15 17:34:42 +02009526 vmx->nested.nested_vmx_secondary_ctls_high |=
9527 SECONDARY_EXEC_RDTSCP;
9528 else
9529 vmx->nested.nested_vmx_secondary_ctls_high &=
9530 ~SECONDARY_EXEC_RDTSCP;
9531 }
Sheng Yang4e47c7a2009-12-18 16:48:47 +08009532 }
Mao, Junjiead756a12012-07-02 01:18:48 +00009533
Paolo Bonzini90a2db62017-07-27 13:22:13 +02009534 if (vmx_invpcid_supported()) {
9535 /* Exposing INVPCID only when PCID is exposed */
9536 struct kvm_cpuid_entry2 *best = kvm_find_cpuid_entry(vcpu, 0x7, 0);
9537 bool invpcid_enabled =
9538 best && best->ebx & bit(X86_FEATURE_INVPCID) &&
9539 guest_cpuid_has_pcid(vcpu);
Xiao Guangrong29541bb2015-09-09 14:05:54 +08009540
Paolo Bonzini90a2db62017-07-27 13:22:13 +02009541 if (!invpcid_enabled) {
9542 secondary_exec_ctl &= ~SECONDARY_EXEC_ENABLE_INVPCID;
9543 if (best)
9544 best->ebx &= ~bit(X86_FEATURE_INVPCID);
9545 }
9546
9547 if (nested) {
9548 if (invpcid_enabled)
9549 vmx->nested.nested_vmx_secondary_ctls_high |=
9550 SECONDARY_EXEC_ENABLE_INVPCID;
9551 else
9552 vmx->nested.nested_vmx_secondary_ctls_high &=
9553 ~SECONDARY_EXEC_ENABLE_INVPCID;
9554 }
Mao, Junjiead756a12012-07-02 01:18:48 +00009555 }
Xiao Guangrong8b3e34e2015-09-09 14:05:51 +08009556
Huaitong Han45bdbcf2016-01-12 16:04:20 +08009557 if (cpu_has_secondary_exec_ctrls())
9558 vmcs_set_secondary_exec_control(secondary_exec_ctl);
Xiao Guangrongfeda8052015-09-09 14:05:55 +08009559
Haozhong Zhang37e4c992016-06-22 14:59:55 +08009560 if (nested_vmx_allowed(vcpu))
9561 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits |=
9562 FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
9563 else
9564 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits &=
9565 ~FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
David Matlack8322ebb2016-11-29 18:14:09 -08009566
9567 if (nested_vmx_allowed(vcpu))
9568 nested_vmx_cr_fixed1_bits_update(vcpu);
Sheng Yang0e851882009-12-18 16:48:46 +08009569}
9570
Joerg Roedeld4330ef2010-04-22 12:33:11 +02009571static void vmx_set_supported_cpuid(u32 func, struct kvm_cpuid_entry2 *entry)
9572{
Nadav Har'El7b8050f2011-05-25 23:16:10 +03009573 if (func == 1 && nested)
9574 entry->ecx |= bit(X86_FEATURE_VMX);
Joerg Roedeld4330ef2010-04-22 12:33:11 +02009575}
9576
Yang Zhang25d92082013-08-06 12:00:32 +03009577static void nested_ept_inject_page_fault(struct kvm_vcpu *vcpu,
9578 struct x86_exception *fault)
9579{
Jan Kiszka533558b2014-01-04 18:47:20 +01009580 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Bandan Dasc5f983f2017-05-05 15:25:14 -04009581 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jan Kiszka533558b2014-01-04 18:47:20 +01009582 u32 exit_reason;
Bandan Dasc5f983f2017-05-05 15:25:14 -04009583 unsigned long exit_qualification = vcpu->arch.exit_qualification;
Yang Zhang25d92082013-08-06 12:00:32 +03009584
Bandan Dasc5f983f2017-05-05 15:25:14 -04009585 if (vmx->nested.pml_full) {
9586 exit_reason = EXIT_REASON_PML_FULL;
9587 vmx->nested.pml_full = false;
9588 exit_qualification &= INTR_INFO_UNBLOCK_NMI;
9589 } else if (fault->error_code & PFERR_RSVD_MASK)
Jan Kiszka533558b2014-01-04 18:47:20 +01009590 exit_reason = EXIT_REASON_EPT_MISCONFIG;
Yang Zhang25d92082013-08-06 12:00:32 +03009591 else
Jan Kiszka533558b2014-01-04 18:47:20 +01009592 exit_reason = EXIT_REASON_EPT_VIOLATION;
Bandan Dasc5f983f2017-05-05 15:25:14 -04009593
9594 nested_vmx_vmexit(vcpu, exit_reason, 0, exit_qualification);
Yang Zhang25d92082013-08-06 12:00:32 +03009595 vmcs12->guest_physical_address = fault->address;
9596}
9597
Peter Feiner995f00a2017-06-30 17:26:32 -07009598static bool nested_ept_ad_enabled(struct kvm_vcpu *vcpu)
9599{
9600 return nested_ept_get_cr3(vcpu) & VMX_EPT_AD_ENABLE_BIT;
9601}
9602
Nadav Har'El155a97a2013-08-05 11:07:16 +03009603/* Callbacks for nested_ept_init_mmu_context: */
9604
9605static unsigned long nested_ept_get_cr3(struct kvm_vcpu *vcpu)
9606{
9607 /* return the page table to be shadowed - in our case, EPT12 */
9608 return get_vmcs12(vcpu)->ept_pointer;
9609}
9610
Paolo Bonziniae1e2d12017-03-30 11:55:30 +02009611static int nested_ept_init_mmu_context(struct kvm_vcpu *vcpu)
Nadav Har'El155a97a2013-08-05 11:07:16 +03009612{
Peter Feiner995f00a2017-06-30 17:26:32 -07009613 bool wants_ad;
Paolo Bonziniae1e2d12017-03-30 11:55:30 +02009614
Paolo Bonziniad896af2013-10-02 16:56:14 +02009615 WARN_ON(mmu_is_nested(vcpu));
Peter Feiner995f00a2017-06-30 17:26:32 -07009616 wants_ad = nested_ept_ad_enabled(vcpu);
9617 if (wants_ad && !enable_ept_ad_bits)
Paolo Bonziniae1e2d12017-03-30 11:55:30 +02009618 return 1;
9619
9620 kvm_mmu_unload(vcpu);
Paolo Bonziniad896af2013-10-02 16:56:14 +02009621 kvm_init_shadow_ept_mmu(vcpu,
Wincy Vanb9c237b2015-02-03 23:56:30 +08009622 to_vmx(vcpu)->nested.nested_vmx_ept_caps &
Paolo Bonziniae1e2d12017-03-30 11:55:30 +02009623 VMX_EPT_EXECUTE_ONLY_BIT,
Peter Feiner995f00a2017-06-30 17:26:32 -07009624 wants_ad);
Nadav Har'El155a97a2013-08-05 11:07:16 +03009625 vcpu->arch.mmu.set_cr3 = vmx_set_cr3;
9626 vcpu->arch.mmu.get_cr3 = nested_ept_get_cr3;
9627 vcpu->arch.mmu.inject_page_fault = nested_ept_inject_page_fault;
9628
9629 vcpu->arch.walk_mmu = &vcpu->arch.nested_mmu;
Paolo Bonziniae1e2d12017-03-30 11:55:30 +02009630 return 0;
Nadav Har'El155a97a2013-08-05 11:07:16 +03009631}
9632
9633static void nested_ept_uninit_mmu_context(struct kvm_vcpu *vcpu)
9634{
9635 vcpu->arch.walk_mmu = &vcpu->arch.mmu;
9636}
9637
Eugene Korenevsky19d5f102014-12-16 22:35:53 +03009638static bool nested_vmx_is_page_fault_vmexit(struct vmcs12 *vmcs12,
9639 u16 error_code)
9640{
9641 bool inequality, bit;
9642
9643 bit = (vmcs12->exception_bitmap & (1u << PF_VECTOR)) != 0;
9644 inequality =
9645 (error_code & vmcs12->page_fault_error_code_mask) !=
9646 vmcs12->page_fault_error_code_match;
9647 return inequality ^ bit;
9648}
9649
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +03009650static void vmx_inject_page_fault_nested(struct kvm_vcpu *vcpu,
9651 struct x86_exception *fault)
9652{
9653 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
9654
9655 WARN_ON(!is_guest_mode(vcpu));
9656
Paolo Bonzini7313c692017-07-27 10:31:25 +02009657 if (nested_vmx_is_page_fault_vmexit(vmcs12, fault->error_code)) {
Paolo Bonzinib96fb432017-07-27 12:29:32 +02009658 vmcs12->vm_exit_intr_error_code = fault->error_code;
9659 nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI,
9660 PF_VECTOR | INTR_TYPE_HARD_EXCEPTION |
9661 INTR_INFO_DELIVER_CODE_MASK | INTR_INFO_VALID_MASK,
9662 fault->address);
Paolo Bonzini7313c692017-07-27 10:31:25 +02009663 } else {
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +03009664 kvm_inject_page_fault(vcpu, fault);
Paolo Bonzini7313c692017-07-27 10:31:25 +02009665 }
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +03009666}
9667
Jim Mattson6beb7bd2016-11-30 12:03:45 -08009668static inline bool nested_vmx_merge_msr_bitmap(struct kvm_vcpu *vcpu,
9669 struct vmcs12 *vmcs12);
9670
9671static void nested_get_vmcs12_pages(struct kvm_vcpu *vcpu,
Wanpeng Lia2bcba52014-08-21 19:46:49 +08009672 struct vmcs12 *vmcs12)
9673{
9674 struct vcpu_vmx *vmx = to_vmx(vcpu);
David Hildenbrand5e2f30b2017-08-03 18:11:04 +02009675 struct page *page;
Jim Mattson6beb7bd2016-11-30 12:03:45 -08009676 u64 hpa;
Wanpeng Lia2bcba52014-08-21 19:46:49 +08009677
9678 if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) {
Wanpeng Lia2bcba52014-08-21 19:46:49 +08009679 /*
9680 * Translate L1 physical address to host physical
9681 * address for vmcs02. Keep the page pinned, so this
9682 * physical address remains valid. We keep a reference
9683 * to it so we can release it later.
9684 */
David Hildenbrand5e2f30b2017-08-03 18:11:04 +02009685 if (vmx->nested.apic_access_page) { /* shouldn't happen */
David Hildenbrand53a70da2017-08-03 18:11:05 +02009686 kvm_release_page_dirty(vmx->nested.apic_access_page);
David Hildenbrand5e2f30b2017-08-03 18:11:04 +02009687 vmx->nested.apic_access_page = NULL;
9688 }
9689 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->apic_access_addr);
Jim Mattson6beb7bd2016-11-30 12:03:45 -08009690 /*
9691 * If translation failed, no matter: This feature asks
9692 * to exit when accessing the given address, and if it
9693 * can never be accessed, this feature won't do
9694 * anything anyway.
9695 */
David Hildenbrand5e2f30b2017-08-03 18:11:04 +02009696 if (!is_error_page(page)) {
9697 vmx->nested.apic_access_page = page;
Jim Mattson6beb7bd2016-11-30 12:03:45 -08009698 hpa = page_to_phys(vmx->nested.apic_access_page);
9699 vmcs_write64(APIC_ACCESS_ADDR, hpa);
9700 } else {
9701 vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL,
9702 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES);
9703 }
9704 } else if (!(nested_cpu_has_virt_x2apic_mode(vmcs12)) &&
9705 cpu_need_virtualize_apic_accesses(&vmx->vcpu)) {
9706 vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL,
9707 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES);
9708 kvm_vcpu_reload_apic_access_page(vcpu);
Wanpeng Lia2bcba52014-08-21 19:46:49 +08009709 }
Wanpeng Lia7c0b072014-08-21 19:46:50 +08009710
9711 if (nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW)) {
David Hildenbrand5e2f30b2017-08-03 18:11:04 +02009712 if (vmx->nested.virtual_apic_page) { /* shouldn't happen */
David Hildenbrand53a70da2017-08-03 18:11:05 +02009713 kvm_release_page_dirty(vmx->nested.virtual_apic_page);
David Hildenbrand5e2f30b2017-08-03 18:11:04 +02009714 vmx->nested.virtual_apic_page = NULL;
9715 }
9716 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->virtual_apic_page_addr);
Wanpeng Lia7c0b072014-08-21 19:46:50 +08009717
9718 /*
Jim Mattson6beb7bd2016-11-30 12:03:45 -08009719 * If translation failed, VM entry will fail because
9720 * prepare_vmcs02 set VIRTUAL_APIC_PAGE_ADDR to -1ull.
9721 * Failing the vm entry is _not_ what the processor
9722 * does but it's basically the only possibility we
9723 * have. We could still enter the guest if CR8 load
9724 * exits are enabled, CR8 store exits are enabled, and
9725 * virtualize APIC access is disabled; in this case
9726 * the processor would never use the TPR shadow and we
9727 * could simply clear the bit from the execution
9728 * control. But such a configuration is useless, so
9729 * let's keep the code simple.
Wanpeng Lia7c0b072014-08-21 19:46:50 +08009730 */
David Hildenbrand5e2f30b2017-08-03 18:11:04 +02009731 if (!is_error_page(page)) {
9732 vmx->nested.virtual_apic_page = page;
Jim Mattson6beb7bd2016-11-30 12:03:45 -08009733 hpa = page_to_phys(vmx->nested.virtual_apic_page);
9734 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, hpa);
9735 }
Wanpeng Lia7c0b072014-08-21 19:46:50 +08009736 }
9737
Wincy Van705699a2015-02-03 23:58:17 +08009738 if (nested_cpu_has_posted_intr(vmcs12)) {
Wincy Van705699a2015-02-03 23:58:17 +08009739 if (vmx->nested.pi_desc_page) { /* shouldn't happen */
9740 kunmap(vmx->nested.pi_desc_page);
David Hildenbrand53a70da2017-08-03 18:11:05 +02009741 kvm_release_page_dirty(vmx->nested.pi_desc_page);
David Hildenbrand5e2f30b2017-08-03 18:11:04 +02009742 vmx->nested.pi_desc_page = NULL;
Wincy Van705699a2015-02-03 23:58:17 +08009743 }
David Hildenbrand5e2f30b2017-08-03 18:11:04 +02009744 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->posted_intr_desc_addr);
9745 if (is_error_page(page))
Jim Mattson6beb7bd2016-11-30 12:03:45 -08009746 return;
David Hildenbrand5e2f30b2017-08-03 18:11:04 +02009747 vmx->nested.pi_desc_page = page;
9748 vmx->nested.pi_desc = kmap(vmx->nested.pi_desc_page);
Wincy Van705699a2015-02-03 23:58:17 +08009749 vmx->nested.pi_desc =
9750 (struct pi_desc *)((void *)vmx->nested.pi_desc +
9751 (unsigned long)(vmcs12->posted_intr_desc_addr &
9752 (PAGE_SIZE - 1)));
Jim Mattson6beb7bd2016-11-30 12:03:45 -08009753 vmcs_write64(POSTED_INTR_DESC_ADDR,
9754 page_to_phys(vmx->nested.pi_desc_page) +
9755 (unsigned long)(vmcs12->posted_intr_desc_addr &
9756 (PAGE_SIZE - 1)));
Wincy Van705699a2015-02-03 23:58:17 +08009757 }
Jim Mattson6beb7bd2016-11-30 12:03:45 -08009758 if (cpu_has_vmx_msr_bitmap() &&
9759 nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS) &&
9760 nested_vmx_merge_msr_bitmap(vcpu, vmcs12))
9761 ;
9762 else
9763 vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL,
9764 CPU_BASED_USE_MSR_BITMAPS);
Wanpeng Lia2bcba52014-08-21 19:46:49 +08009765}
9766
Jan Kiszkaf41245002014-03-07 20:03:13 +01009767static void vmx_start_preemption_timer(struct kvm_vcpu *vcpu)
9768{
9769 u64 preemption_timeout = get_vmcs12(vcpu)->vmx_preemption_timer_value;
9770 struct vcpu_vmx *vmx = to_vmx(vcpu);
9771
9772 if (vcpu->arch.virtual_tsc_khz == 0)
9773 return;
9774
9775 /* Make sure short timeouts reliably trigger an immediate vmexit.
9776 * hrtimer_start does not guarantee this. */
9777 if (preemption_timeout <= 1) {
9778 vmx_preemption_timer_fn(&vmx->nested.preemption_timer);
9779 return;
9780 }
9781
9782 preemption_timeout <<= VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE;
9783 preemption_timeout *= 1000000;
9784 do_div(preemption_timeout, vcpu->arch.virtual_tsc_khz);
9785 hrtimer_start(&vmx->nested.preemption_timer,
9786 ns_to_ktime(preemption_timeout), HRTIMER_MODE_REL);
9787}
9788
Jim Mattson56a20512017-07-06 16:33:06 -07009789static int nested_vmx_check_io_bitmap_controls(struct kvm_vcpu *vcpu,
9790 struct vmcs12 *vmcs12)
9791{
9792 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_IO_BITMAPS))
9793 return 0;
9794
9795 if (!page_address_valid(vcpu, vmcs12->io_bitmap_a) ||
9796 !page_address_valid(vcpu, vmcs12->io_bitmap_b))
9797 return -EINVAL;
9798
9799 return 0;
9800}
9801
Wincy Van3af18d92015-02-03 23:49:31 +08009802static int nested_vmx_check_msr_bitmap_controls(struct kvm_vcpu *vcpu,
9803 struct vmcs12 *vmcs12)
9804{
Wincy Van3af18d92015-02-03 23:49:31 +08009805 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS))
9806 return 0;
9807
Jim Mattson5fa99cb2017-07-06 16:33:07 -07009808 if (!page_address_valid(vcpu, vmcs12->msr_bitmap))
Wincy Van3af18d92015-02-03 23:49:31 +08009809 return -EINVAL;
9810
9811 return 0;
9812}
9813
9814/*
9815 * Merge L0's and L1's MSR bitmap, return false to indicate that
9816 * we do not use the hardware.
9817 */
9818static inline bool nested_vmx_merge_msr_bitmap(struct kvm_vcpu *vcpu,
9819 struct vmcs12 *vmcs12)
9820{
Wincy Van82f0dd42015-02-03 23:57:18 +08009821 int msr;
Wincy Vanf2b93282015-02-03 23:56:03 +08009822 struct page *page;
Radim Krčmářd048c092016-08-08 20:16:22 +02009823 unsigned long *msr_bitmap_l1;
9824 unsigned long *msr_bitmap_l0 = to_vmx(vcpu)->nested.msr_bitmap;
Wincy Vanf2b93282015-02-03 23:56:03 +08009825
Radim Krčmářd048c092016-08-08 20:16:22 +02009826 /* This shortcut is ok because we support only x2APIC MSRs so far. */
Wincy Vanf2b93282015-02-03 23:56:03 +08009827 if (!nested_cpu_has_virt_x2apic_mode(vmcs12))
9828 return false;
9829
David Hildenbrand5e2f30b2017-08-03 18:11:04 +02009830 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->msr_bitmap);
9831 if (is_error_page(page))
Wincy Vanf2b93282015-02-03 23:56:03 +08009832 return false;
Radim Krčmářd048c092016-08-08 20:16:22 +02009833 msr_bitmap_l1 = (unsigned long *)kmap(page);
Wincy Vanf2b93282015-02-03 23:56:03 +08009834
Radim Krčmářd048c092016-08-08 20:16:22 +02009835 memset(msr_bitmap_l0, 0xff, PAGE_SIZE);
9836
Wincy Vanf2b93282015-02-03 23:56:03 +08009837 if (nested_cpu_has_virt_x2apic_mode(vmcs12)) {
Wincy Van82f0dd42015-02-03 23:57:18 +08009838 if (nested_cpu_has_apic_reg_virt(vmcs12))
9839 for (msr = 0x800; msr <= 0x8ff; msr++)
9840 nested_vmx_disable_intercept_for_msr(
Radim Krčmářd048c092016-08-08 20:16:22 +02009841 msr_bitmap_l1, msr_bitmap_l0,
Wincy Van82f0dd42015-02-03 23:57:18 +08009842 msr, MSR_TYPE_R);
Radim Krčmářd048c092016-08-08 20:16:22 +02009843
9844 nested_vmx_disable_intercept_for_msr(
9845 msr_bitmap_l1, msr_bitmap_l0,
Wincy Vanf2b93282015-02-03 23:56:03 +08009846 APIC_BASE_MSR + (APIC_TASKPRI >> 4),
9847 MSR_TYPE_R | MSR_TYPE_W);
Radim Krčmářd048c092016-08-08 20:16:22 +02009848
Wincy Van608406e2015-02-03 23:57:51 +08009849 if (nested_cpu_has_vid(vmcs12)) {
Wincy Van608406e2015-02-03 23:57:51 +08009850 nested_vmx_disable_intercept_for_msr(
Radim Krčmářd048c092016-08-08 20:16:22 +02009851 msr_bitmap_l1, msr_bitmap_l0,
Wincy Van608406e2015-02-03 23:57:51 +08009852 APIC_BASE_MSR + (APIC_EOI >> 4),
9853 MSR_TYPE_W);
9854 nested_vmx_disable_intercept_for_msr(
Radim Krčmářd048c092016-08-08 20:16:22 +02009855 msr_bitmap_l1, msr_bitmap_l0,
Wincy Van608406e2015-02-03 23:57:51 +08009856 APIC_BASE_MSR + (APIC_SELF_IPI >> 4),
9857 MSR_TYPE_W);
9858 }
Wincy Van82f0dd42015-02-03 23:57:18 +08009859 }
Wincy Vanf2b93282015-02-03 23:56:03 +08009860 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +02009861 kvm_release_page_clean(page);
Wincy Vanf2b93282015-02-03 23:56:03 +08009862
9863 return true;
9864}
9865
9866static int nested_vmx_check_apicv_controls(struct kvm_vcpu *vcpu,
9867 struct vmcs12 *vmcs12)
9868{
Wincy Van82f0dd42015-02-03 23:57:18 +08009869 if (!nested_cpu_has_virt_x2apic_mode(vmcs12) &&
Wincy Van608406e2015-02-03 23:57:51 +08009870 !nested_cpu_has_apic_reg_virt(vmcs12) &&
Wincy Van705699a2015-02-03 23:58:17 +08009871 !nested_cpu_has_vid(vmcs12) &&
9872 !nested_cpu_has_posted_intr(vmcs12))
Wincy Vanf2b93282015-02-03 23:56:03 +08009873 return 0;
9874
9875 /*
9876 * If virtualize x2apic mode is enabled,
9877 * virtualize apic access must be disabled.
9878 */
Wincy Van82f0dd42015-02-03 23:57:18 +08009879 if (nested_cpu_has_virt_x2apic_mode(vmcs12) &&
9880 nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES))
Wincy Vanf2b93282015-02-03 23:56:03 +08009881 return -EINVAL;
9882
Wincy Van608406e2015-02-03 23:57:51 +08009883 /*
9884 * If virtual interrupt delivery is enabled,
9885 * we must exit on external interrupts.
9886 */
9887 if (nested_cpu_has_vid(vmcs12) &&
9888 !nested_exit_on_intr(vcpu))
9889 return -EINVAL;
9890
Wincy Van705699a2015-02-03 23:58:17 +08009891 /*
9892 * bits 15:8 should be zero in posted_intr_nv,
9893 * the descriptor address has been already checked
9894 * in nested_get_vmcs12_pages.
9895 */
9896 if (nested_cpu_has_posted_intr(vmcs12) &&
9897 (!nested_cpu_has_vid(vmcs12) ||
9898 !nested_exit_intr_ack_set(vcpu) ||
9899 vmcs12->posted_intr_nv & 0xff00))
9900 return -EINVAL;
9901
Wincy Vanf2b93282015-02-03 23:56:03 +08009902 /* tpr shadow is needed by all apicv features. */
9903 if (!nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW))
9904 return -EINVAL;
9905
9906 return 0;
Wincy Van3af18d92015-02-03 23:49:31 +08009907}
9908
Eugene Korenevskye9ac0332014-12-11 08:53:27 +03009909static int nested_vmx_check_msr_switch(struct kvm_vcpu *vcpu,
9910 unsigned long count_field,
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +03009911 unsigned long addr_field)
Wincy Vanff651cb2014-12-11 08:52:58 +03009912{
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +03009913 int maxphyaddr;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +03009914 u64 count, addr;
9915
9916 if (vmcs12_read_any(vcpu, count_field, &count) ||
9917 vmcs12_read_any(vcpu, addr_field, &addr)) {
9918 WARN_ON(1);
9919 return -EINVAL;
9920 }
9921 if (count == 0)
9922 return 0;
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +03009923 maxphyaddr = cpuid_maxphyaddr(vcpu);
Eugene Korenevskye9ac0332014-12-11 08:53:27 +03009924 if (!IS_ALIGNED(addr, 16) || addr >> maxphyaddr ||
9925 (addr + count * sizeof(struct vmx_msr_entry) - 1) >> maxphyaddr) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +02009926 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +03009927 "nVMX: invalid MSR switch (0x%lx, %d, %llu, 0x%08llx)",
9928 addr_field, maxphyaddr, count, addr);
9929 return -EINVAL;
9930 }
9931 return 0;
9932}
9933
9934static int nested_vmx_check_msr_switch_controls(struct kvm_vcpu *vcpu,
9935 struct vmcs12 *vmcs12)
9936{
Eugene Korenevskye9ac0332014-12-11 08:53:27 +03009937 if (vmcs12->vm_exit_msr_load_count == 0 &&
9938 vmcs12->vm_exit_msr_store_count == 0 &&
9939 vmcs12->vm_entry_msr_load_count == 0)
9940 return 0; /* Fast path */
Eugene Korenevskye9ac0332014-12-11 08:53:27 +03009941 if (nested_vmx_check_msr_switch(vcpu, VM_EXIT_MSR_LOAD_COUNT,
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +03009942 VM_EXIT_MSR_LOAD_ADDR) ||
Eugene Korenevskye9ac0332014-12-11 08:53:27 +03009943 nested_vmx_check_msr_switch(vcpu, VM_EXIT_MSR_STORE_COUNT,
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +03009944 VM_EXIT_MSR_STORE_ADDR) ||
Eugene Korenevskye9ac0332014-12-11 08:53:27 +03009945 nested_vmx_check_msr_switch(vcpu, VM_ENTRY_MSR_LOAD_COUNT,
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +03009946 VM_ENTRY_MSR_LOAD_ADDR))
Wincy Vanff651cb2014-12-11 08:52:58 +03009947 return -EINVAL;
9948 return 0;
9949}
9950
Bandan Dasc5f983f2017-05-05 15:25:14 -04009951static int nested_vmx_check_pml_controls(struct kvm_vcpu *vcpu,
9952 struct vmcs12 *vmcs12)
9953{
9954 u64 address = vmcs12->pml_address;
9955 int maxphyaddr = cpuid_maxphyaddr(vcpu);
9956
9957 if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_PML)) {
9958 if (!nested_cpu_has_ept(vmcs12) ||
9959 !IS_ALIGNED(address, 4096) ||
9960 address >> maxphyaddr)
9961 return -EINVAL;
9962 }
9963
9964 return 0;
9965}
9966
Eugene Korenevskye9ac0332014-12-11 08:53:27 +03009967static int nested_vmx_msr_check_common(struct kvm_vcpu *vcpu,
9968 struct vmx_msr_entry *e)
9969{
9970 /* x2APIC MSR accesses are not allowed */
Jan Kiszka8a9781f2015-05-04 08:32:32 +02009971 if (vcpu->arch.apic_base & X2APIC_ENABLE && e->index >> 8 == 0x8)
Eugene Korenevskye9ac0332014-12-11 08:53:27 +03009972 return -EINVAL;
9973 if (e->index == MSR_IA32_UCODE_WRITE || /* SDM Table 35-2 */
9974 e->index == MSR_IA32_UCODE_REV)
9975 return -EINVAL;
9976 if (e->reserved != 0)
9977 return -EINVAL;
9978 return 0;
9979}
9980
9981static int nested_vmx_load_msr_check(struct kvm_vcpu *vcpu,
9982 struct vmx_msr_entry *e)
Wincy Vanff651cb2014-12-11 08:52:58 +03009983{
9984 if (e->index == MSR_FS_BASE ||
9985 e->index == MSR_GS_BASE ||
Eugene Korenevskye9ac0332014-12-11 08:53:27 +03009986 e->index == MSR_IA32_SMM_MONITOR_CTL || /* SMM is not supported */
9987 nested_vmx_msr_check_common(vcpu, e))
9988 return -EINVAL;
9989 return 0;
9990}
9991
9992static int nested_vmx_store_msr_check(struct kvm_vcpu *vcpu,
9993 struct vmx_msr_entry *e)
9994{
9995 if (e->index == MSR_IA32_SMBASE || /* SMM is not supported */
9996 nested_vmx_msr_check_common(vcpu, e))
Wincy Vanff651cb2014-12-11 08:52:58 +03009997 return -EINVAL;
9998 return 0;
9999}
10000
10001/*
10002 * Load guest's/host's msr at nested entry/exit.
10003 * return 0 for success, entry index for failure.
10004 */
10005static u32 nested_vmx_load_msr(struct kvm_vcpu *vcpu, u64 gpa, u32 count)
10006{
10007 u32 i;
10008 struct vmx_msr_entry e;
10009 struct msr_data msr;
10010
10011 msr.host_initiated = false;
10012 for (i = 0; i < count; i++) {
Paolo Bonzini54bf36a2015-04-08 15:39:23 +020010013 if (kvm_vcpu_read_guest(vcpu, gpa + i * sizeof(e),
10014 &e, sizeof(e))) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020010015 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010016 "%s cannot read MSR entry (%u, 0x%08llx)\n",
10017 __func__, i, gpa + i * sizeof(e));
Wincy Vanff651cb2014-12-11 08:52:58 +030010018 goto fail;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010019 }
10020 if (nested_vmx_load_msr_check(vcpu, &e)) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020010021 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010022 "%s check failed (%u, 0x%x, 0x%x)\n",
10023 __func__, i, e.index, e.reserved);
10024 goto fail;
10025 }
Wincy Vanff651cb2014-12-11 08:52:58 +030010026 msr.index = e.index;
10027 msr.data = e.value;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010028 if (kvm_set_msr(vcpu, &msr)) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020010029 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010030 "%s cannot write MSR (%u, 0x%x, 0x%llx)\n",
10031 __func__, i, e.index, e.value);
Wincy Vanff651cb2014-12-11 08:52:58 +030010032 goto fail;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010033 }
Wincy Vanff651cb2014-12-11 08:52:58 +030010034 }
10035 return 0;
10036fail:
10037 return i + 1;
10038}
10039
10040static int nested_vmx_store_msr(struct kvm_vcpu *vcpu, u64 gpa, u32 count)
10041{
10042 u32 i;
10043 struct vmx_msr_entry e;
10044
10045 for (i = 0; i < count; i++) {
Paolo Bonzini609e36d2015-04-08 15:30:38 +020010046 struct msr_data msr_info;
Paolo Bonzini54bf36a2015-04-08 15:39:23 +020010047 if (kvm_vcpu_read_guest(vcpu,
10048 gpa + i * sizeof(e),
10049 &e, 2 * sizeof(u32))) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020010050 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010051 "%s cannot read MSR entry (%u, 0x%08llx)\n",
10052 __func__, i, gpa + i * sizeof(e));
Wincy Vanff651cb2014-12-11 08:52:58 +030010053 return -EINVAL;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010054 }
10055 if (nested_vmx_store_msr_check(vcpu, &e)) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020010056 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010057 "%s check failed (%u, 0x%x, 0x%x)\n",
10058 __func__, i, e.index, e.reserved);
Wincy Vanff651cb2014-12-11 08:52:58 +030010059 return -EINVAL;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010060 }
Paolo Bonzini609e36d2015-04-08 15:30:38 +020010061 msr_info.host_initiated = false;
10062 msr_info.index = e.index;
10063 if (kvm_get_msr(vcpu, &msr_info)) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020010064 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010065 "%s cannot read MSR (%u, 0x%x)\n",
10066 __func__, i, e.index);
10067 return -EINVAL;
10068 }
Paolo Bonzini54bf36a2015-04-08 15:39:23 +020010069 if (kvm_vcpu_write_guest(vcpu,
10070 gpa + i * sizeof(e) +
10071 offsetof(struct vmx_msr_entry, value),
10072 &msr_info.data, sizeof(msr_info.data))) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020010073 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010074 "%s cannot write MSR (%u, 0x%x, 0x%llx)\n",
Paolo Bonzini609e36d2015-04-08 15:30:38 +020010075 __func__, i, e.index, msr_info.data);
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010076 return -EINVAL;
10077 }
Wincy Vanff651cb2014-12-11 08:52:58 +030010078 }
10079 return 0;
10080}
10081
Ladi Prosek1dc35da2016-11-30 16:03:11 +010010082static bool nested_cr3_valid(struct kvm_vcpu *vcpu, unsigned long val)
10083{
10084 unsigned long invalid_mask;
10085
10086 invalid_mask = (~0ULL) << cpuid_maxphyaddr(vcpu);
10087 return (val & invalid_mask) == 0;
10088}
10089
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010090/*
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010010091 * Load guest's/host's cr3 at nested entry/exit. nested_ept is true if we are
10092 * emulating VM entry into a guest with EPT enabled.
10093 * Returns 0 on success, 1 on failure. Invalid state exit qualification code
10094 * is assigned to entry_failure_code on failure.
10095 */
10096static int nested_vmx_load_cr3(struct kvm_vcpu *vcpu, unsigned long cr3, bool nested_ept,
Jim Mattsonca0bde22016-11-30 12:03:46 -080010097 u32 *entry_failure_code)
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010010098{
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010010099 if (cr3 != kvm_read_cr3(vcpu) || (!nested_ept && pdptrs_changed(vcpu))) {
Ladi Prosek1dc35da2016-11-30 16:03:11 +010010100 if (!nested_cr3_valid(vcpu, cr3)) {
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010010101 *entry_failure_code = ENTRY_FAIL_DEFAULT;
10102 return 1;
10103 }
10104
10105 /*
10106 * If PAE paging and EPT are both on, CR3 is not used by the CPU and
10107 * must not be dereferenced.
10108 */
10109 if (!is_long_mode(vcpu) && is_pae(vcpu) && is_paging(vcpu) &&
10110 !nested_ept) {
10111 if (!load_pdptrs(vcpu, vcpu->arch.walk_mmu, cr3)) {
10112 *entry_failure_code = ENTRY_FAIL_PDPTE;
10113 return 1;
10114 }
10115 }
10116
10117 vcpu->arch.cr3 = cr3;
10118 __set_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail);
10119 }
10120
10121 kvm_mmu_reset_context(vcpu);
10122 return 0;
10123}
10124
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010125/*
10126 * prepare_vmcs02 is called when the L1 guest hypervisor runs its nested
10127 * L2 guest. L1 has a vmcs for L2 (vmcs12), and this function "merges" it
Tiejun Chenb4619662014-09-22 10:31:38 +080010128 * 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 +030010129 * guest in a way that will both be appropriate to L1's requests, and our
10130 * needs. In addition to modifying the active vmcs (which is vmcs02), this
10131 * function also has additional necessary side-effects, like setting various
10132 * vcpu->arch fields.
Ladi Prosekee146c12016-11-30 16:03:09 +010010133 * Returns 0 on success, 1 on failure. Invalid state exit qualification code
10134 * is assigned to entry_failure_code on failure.
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010135 */
Ladi Prosekee146c12016-11-30 16:03:09 +010010136static int prepare_vmcs02(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12,
Jim Mattsonca0bde22016-11-30 12:03:46 -080010137 bool from_vmentry, u32 *entry_failure_code)
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010138{
10139 struct vcpu_vmx *vmx = to_vmx(vcpu);
Bandan Das03efce62017-05-05 15:25:15 -040010140 u32 exec_control, vmcs12_exec_ctrl;
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010141
10142 vmcs_write16(GUEST_ES_SELECTOR, vmcs12->guest_es_selector);
10143 vmcs_write16(GUEST_CS_SELECTOR, vmcs12->guest_cs_selector);
10144 vmcs_write16(GUEST_SS_SELECTOR, vmcs12->guest_ss_selector);
10145 vmcs_write16(GUEST_DS_SELECTOR, vmcs12->guest_ds_selector);
10146 vmcs_write16(GUEST_FS_SELECTOR, vmcs12->guest_fs_selector);
10147 vmcs_write16(GUEST_GS_SELECTOR, vmcs12->guest_gs_selector);
10148 vmcs_write16(GUEST_LDTR_SELECTOR, vmcs12->guest_ldtr_selector);
10149 vmcs_write16(GUEST_TR_SELECTOR, vmcs12->guest_tr_selector);
10150 vmcs_write32(GUEST_ES_LIMIT, vmcs12->guest_es_limit);
10151 vmcs_write32(GUEST_CS_LIMIT, vmcs12->guest_cs_limit);
10152 vmcs_write32(GUEST_SS_LIMIT, vmcs12->guest_ss_limit);
10153 vmcs_write32(GUEST_DS_LIMIT, vmcs12->guest_ds_limit);
10154 vmcs_write32(GUEST_FS_LIMIT, vmcs12->guest_fs_limit);
10155 vmcs_write32(GUEST_GS_LIMIT, vmcs12->guest_gs_limit);
10156 vmcs_write32(GUEST_LDTR_LIMIT, vmcs12->guest_ldtr_limit);
10157 vmcs_write32(GUEST_TR_LIMIT, vmcs12->guest_tr_limit);
10158 vmcs_write32(GUEST_GDTR_LIMIT, vmcs12->guest_gdtr_limit);
10159 vmcs_write32(GUEST_IDTR_LIMIT, vmcs12->guest_idtr_limit);
10160 vmcs_write32(GUEST_ES_AR_BYTES, vmcs12->guest_es_ar_bytes);
10161 vmcs_write32(GUEST_CS_AR_BYTES, vmcs12->guest_cs_ar_bytes);
10162 vmcs_write32(GUEST_SS_AR_BYTES, vmcs12->guest_ss_ar_bytes);
10163 vmcs_write32(GUEST_DS_AR_BYTES, vmcs12->guest_ds_ar_bytes);
10164 vmcs_write32(GUEST_FS_AR_BYTES, vmcs12->guest_fs_ar_bytes);
10165 vmcs_write32(GUEST_GS_AR_BYTES, vmcs12->guest_gs_ar_bytes);
10166 vmcs_write32(GUEST_LDTR_AR_BYTES, vmcs12->guest_ldtr_ar_bytes);
10167 vmcs_write32(GUEST_TR_AR_BYTES, vmcs12->guest_tr_ar_bytes);
10168 vmcs_writel(GUEST_ES_BASE, vmcs12->guest_es_base);
10169 vmcs_writel(GUEST_CS_BASE, vmcs12->guest_cs_base);
10170 vmcs_writel(GUEST_SS_BASE, vmcs12->guest_ss_base);
10171 vmcs_writel(GUEST_DS_BASE, vmcs12->guest_ds_base);
10172 vmcs_writel(GUEST_FS_BASE, vmcs12->guest_fs_base);
10173 vmcs_writel(GUEST_GS_BASE, vmcs12->guest_gs_base);
10174 vmcs_writel(GUEST_LDTR_BASE, vmcs12->guest_ldtr_base);
10175 vmcs_writel(GUEST_TR_BASE, vmcs12->guest_tr_base);
10176 vmcs_writel(GUEST_GDTR_BASE, vmcs12->guest_gdtr_base);
10177 vmcs_writel(GUEST_IDTR_BASE, vmcs12->guest_idtr_base);
10178
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080010179 if (from_vmentry &&
10180 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS)) {
Jan Kiszka2996fca2014-06-16 13:59:43 +020010181 kvm_set_dr(vcpu, 7, vmcs12->guest_dr7);
10182 vmcs_write64(GUEST_IA32_DEBUGCTL, vmcs12->guest_ia32_debugctl);
10183 } else {
10184 kvm_set_dr(vcpu, 7, vcpu->arch.dr7);
10185 vmcs_write64(GUEST_IA32_DEBUGCTL, vmx->nested.vmcs01_debugctl);
10186 }
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080010187 if (from_vmentry) {
10188 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
10189 vmcs12->vm_entry_intr_info_field);
10190 vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE,
10191 vmcs12->vm_entry_exception_error_code);
10192 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
10193 vmcs12->vm_entry_instruction_len);
10194 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO,
10195 vmcs12->guest_interruptibility_info);
Wanpeng Li2d6144e2017-07-25 03:40:46 -070010196 vmx->loaded_vmcs->nmi_known_unmasked =
10197 !(vmcs12->guest_interruptibility_info & GUEST_INTR_STATE_NMI);
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080010198 } else {
10199 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0);
10200 }
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010201 vmcs_write32(GUEST_SYSENTER_CS, vmcs12->guest_sysenter_cs);
Gleb Natapov63fbf592013-07-28 18:31:06 +030010202 vmx_set_rflags(vcpu, vmcs12->guest_rflags);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010203 vmcs_writel(GUEST_PENDING_DBG_EXCEPTIONS,
10204 vmcs12->guest_pending_dbg_exceptions);
10205 vmcs_writel(GUEST_SYSENTER_ESP, vmcs12->guest_sysenter_esp);
10206 vmcs_writel(GUEST_SYSENTER_EIP, vmcs12->guest_sysenter_eip);
10207
Wanpeng Li81dc01f2014-12-04 19:11:07 +080010208 if (nested_cpu_has_xsaves(vmcs12))
10209 vmcs_write64(XSS_EXIT_BITMAP, vmcs12->xss_exit_bitmap);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010210 vmcs_write64(VMCS_LINK_POINTER, -1ull);
10211
Jan Kiszkaf41245002014-03-07 20:03:13 +010010212 exec_control = vmcs12->pin_based_vm_exec_control;
Wincy Van705699a2015-02-03 23:58:17 +080010213
Paolo Bonzini9314006db2016-07-06 13:23:51 +020010214 /* Preemption timer setting is only taken from vmcs01. */
10215 exec_control &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
10216 exec_control |= vmcs_config.pin_based_exec_ctrl;
10217 if (vmx->hv_deadline_tsc == -1)
10218 exec_control &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
10219
10220 /* Posted interrupts setting is only taken from vmcs12. */
Wincy Van705699a2015-02-03 23:58:17 +080010221 if (nested_cpu_has_posted_intr(vmcs12)) {
Wincy Van705699a2015-02-03 23:58:17 +080010222 vmx->nested.posted_intr_nv = vmcs12->posted_intr_nv;
10223 vmx->nested.pi_pending = false;
Wincy Van06a55242017-04-28 13:13:59 +080010224 vmcs_write16(POSTED_INTR_NV, POSTED_INTR_NESTED_VECTOR);
Jim Mattson6beb7bd2016-11-30 12:03:45 -080010225 } else {
Wincy Van705699a2015-02-03 23:58:17 +080010226 exec_control &= ~PIN_BASED_POSTED_INTR;
Jim Mattson6beb7bd2016-11-30 12:03:45 -080010227 }
Wincy Van705699a2015-02-03 23:58:17 +080010228
Jan Kiszkaf41245002014-03-07 20:03:13 +010010229 vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, exec_control);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010230
Jan Kiszkaf41245002014-03-07 20:03:13 +010010231 vmx->nested.preemption_timer_expired = false;
10232 if (nested_cpu_has_preemption_timer(vmcs12))
10233 vmx_start_preemption_timer(vcpu);
Jan Kiszka0238ea92013-03-13 11:31:24 +010010234
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010235 /*
10236 * Whether page-faults are trapped is determined by a combination of
10237 * 3 settings: PFEC_MASK, PFEC_MATCH and EXCEPTION_BITMAP.PF.
10238 * If enable_ept, L0 doesn't care about page faults and we should
10239 * set all of these to L1's desires. However, if !enable_ept, L0 does
10240 * care about (at least some) page faults, and because it is not easy
10241 * (if at all possible?) to merge L0 and L1's desires, we simply ask
10242 * to exit on each and every L2 page fault. This is done by setting
10243 * MASK=MATCH=0 and (see below) EB.PF=1.
10244 * Note that below we don't need special code to set EB.PF beyond the
10245 * "or"ing of the EB of vmcs01 and vmcs12, because when enable_ept,
10246 * vmcs01's EB.PF is 0 so the "or" will take vmcs12's value, and when
10247 * !enable_ept, EB.PF is 1, so the "or" will always be 1.
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010248 */
10249 vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK,
10250 enable_ept ? vmcs12->page_fault_error_code_mask : 0);
10251 vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH,
10252 enable_ept ? vmcs12->page_fault_error_code_match : 0);
10253
10254 if (cpu_has_secondary_exec_ctrls()) {
Jan Kiszkaf41245002014-03-07 20:03:13 +010010255 exec_control = vmx_secondary_exec_control(vmx);
Xiao Guangronge2821622015-09-09 14:05:52 +080010256
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010257 /* Take the following fields only from vmcs12 */
Paolo Bonzini696dfd92014-05-07 11:20:54 +020010258 exec_control &= ~(SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
Paolo Bonzini90a2db62017-07-27 13:22:13 +020010259 SECONDARY_EXEC_ENABLE_INVPCID |
Jan Kiszkab3a2a902015-03-23 19:27:19 +010010260 SECONDARY_EXEC_RDTSCP |
Paolo Bonzini696dfd92014-05-07 11:20:54 +020010261 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY |
Bandan Das27c42a12017-08-03 15:54:42 -040010262 SECONDARY_EXEC_APIC_REGISTER_VIRT |
10263 SECONDARY_EXEC_ENABLE_VMFUNC);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010264 if (nested_cpu_has(vmcs12,
Bandan Das03efce62017-05-05 15:25:15 -040010265 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS)) {
10266 vmcs12_exec_ctrl = vmcs12->secondary_vm_exec_control &
10267 ~SECONDARY_EXEC_ENABLE_PML;
10268 exec_control |= vmcs12_exec_ctrl;
10269 }
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010270
Bandan Das27c42a12017-08-03 15:54:42 -040010271 /* All VMFUNCs are currently emulated through L0 vmexits. */
10272 if (exec_control & SECONDARY_EXEC_ENABLE_VMFUNC)
10273 vmcs_write64(VM_FUNCTION_CONTROL, 0);
10274
Wincy Van608406e2015-02-03 23:57:51 +080010275 if (exec_control & SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY) {
10276 vmcs_write64(EOI_EXIT_BITMAP0,
10277 vmcs12->eoi_exit_bitmap0);
10278 vmcs_write64(EOI_EXIT_BITMAP1,
10279 vmcs12->eoi_exit_bitmap1);
10280 vmcs_write64(EOI_EXIT_BITMAP2,
10281 vmcs12->eoi_exit_bitmap2);
10282 vmcs_write64(EOI_EXIT_BITMAP3,
10283 vmcs12->eoi_exit_bitmap3);
10284 vmcs_write16(GUEST_INTR_STATUS,
10285 vmcs12->guest_intr_status);
10286 }
10287
Jim Mattson6beb7bd2016-11-30 12:03:45 -080010288 /*
10289 * Write an illegal value to APIC_ACCESS_ADDR. Later,
10290 * nested_get_vmcs12_pages will either fix it up or
10291 * remove the VM execution control.
10292 */
10293 if (exec_control & SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)
10294 vmcs_write64(APIC_ACCESS_ADDR, -1ull);
10295
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010296 vmcs_write32(SECONDARY_VM_EXEC_CONTROL, exec_control);
10297 }
10298
10299
10300 /*
10301 * Set host-state according to L0's settings (vmcs12 is irrelevant here)
10302 * Some constant fields are set here by vmx_set_constant_host_state().
10303 * Other fields are different per CPU, and will be set later when
10304 * vmx_vcpu_load() is called, and when vmx_save_host_state() is called.
10305 */
Yang Zhanga547c6d2013-04-11 19:25:10 +080010306 vmx_set_constant_host_state(vmx);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010307
10308 /*
Jim Mattson83bafef2016-10-04 10:48:38 -070010309 * Set the MSR load/store lists to match L0's settings.
10310 */
10311 vmcs_write32(VM_EXIT_MSR_STORE_COUNT, 0);
10312 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, vmx->msr_autoload.nr);
10313 vmcs_write64(VM_EXIT_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.host));
10314 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, vmx->msr_autoload.nr);
10315 vmcs_write64(VM_ENTRY_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.guest));
10316
10317 /*
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010318 * HOST_RSP is normally set correctly in vmx_vcpu_run() just before
10319 * entry, but only if the current (host) sp changed from the value
10320 * we wrote last (vmx->host_rsp). This cache is no longer relevant
10321 * if we switch vmcs, and rather than hold a separate cache per vmcs,
10322 * here we just force the write to happen on entry.
10323 */
10324 vmx->host_rsp = 0;
10325
10326 exec_control = vmx_exec_control(vmx); /* L0's desires */
10327 exec_control &= ~CPU_BASED_VIRTUAL_INTR_PENDING;
10328 exec_control &= ~CPU_BASED_VIRTUAL_NMI_PENDING;
10329 exec_control &= ~CPU_BASED_TPR_SHADOW;
10330 exec_control |= vmcs12->cpu_based_vm_exec_control;
Wanpeng Lia7c0b072014-08-21 19:46:50 +080010331
Jim Mattson6beb7bd2016-11-30 12:03:45 -080010332 /*
10333 * Write an illegal value to VIRTUAL_APIC_PAGE_ADDR. Later, if
10334 * nested_get_vmcs12_pages can't fix it up, the illegal value
10335 * will result in a VM entry failure.
10336 */
Wanpeng Lia7c0b072014-08-21 19:46:50 +080010337 if (exec_control & CPU_BASED_TPR_SHADOW) {
Jim Mattson6beb7bd2016-11-30 12:03:45 -080010338 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, -1ull);
Wanpeng Lia7c0b072014-08-21 19:46:50 +080010339 vmcs_write32(TPR_THRESHOLD, vmcs12->tpr_threshold);
10340 }
10341
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010342 /*
Wincy Van3af18d92015-02-03 23:49:31 +080010343 * Merging of IO bitmap not currently supported.
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010344 * Rather, exit every time.
10345 */
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010346 exec_control &= ~CPU_BASED_USE_IO_BITMAPS;
10347 exec_control |= CPU_BASED_UNCOND_IO_EXITING;
10348
10349 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, exec_control);
10350
10351 /* EXCEPTION_BITMAP and CR0_GUEST_HOST_MASK should basically be the
10352 * bitwise-or of what L1 wants to trap for L2, and what we want to
10353 * trap. Note that CR0.TS also needs updating - we do this later.
10354 */
10355 update_exception_bitmap(vcpu);
10356 vcpu->arch.cr0_guest_owned_bits &= ~vmcs12->cr0_guest_host_mask;
10357 vmcs_writel(CR0_GUEST_HOST_MASK, ~vcpu->arch.cr0_guest_owned_bits);
10358
Nadav Har'El8049d652013-08-05 11:07:06 +030010359 /* L2->L1 exit controls are emulated - the hardware exit is to L0 so
10360 * we should use its exit controls. Note that VM_EXIT_LOAD_IA32_EFER
10361 * bits are further modified by vmx_set_efer() below.
10362 */
Jan Kiszkaf41245002014-03-07 20:03:13 +010010363 vmcs_write32(VM_EXIT_CONTROLS, vmcs_config.vmexit_ctrl);
Nadav Har'El8049d652013-08-05 11:07:06 +030010364
10365 /* vmcs12's VM_ENTRY_LOAD_IA32_EFER and VM_ENTRY_IA32E_MODE are
10366 * emulated by vmx_set_efer(), below.
10367 */
Gleb Natapov2961e8762013-11-25 15:37:13 +020010368 vm_entry_controls_init(vmx,
Nadav Har'El8049d652013-08-05 11:07:06 +030010369 (vmcs12->vm_entry_controls & ~VM_ENTRY_LOAD_IA32_EFER &
10370 ~VM_ENTRY_IA32E_MODE) |
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010371 (vmcs_config.vmentry_ctrl & ~VM_ENTRY_IA32E_MODE));
10372
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080010373 if (from_vmentry &&
10374 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_PAT)) {
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010375 vmcs_write64(GUEST_IA32_PAT, vmcs12->guest_ia32_pat);
Jan Kiszka44811c02013-08-04 17:17:27 +020010376 vcpu->arch.pat = vmcs12->guest_ia32_pat;
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080010377 } else if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) {
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010378 vmcs_write64(GUEST_IA32_PAT, vmx->vcpu.arch.pat);
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080010379 }
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010380
10381 set_cr4_guest_host_mask(vmx);
10382
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080010383 if (from_vmentry &&
10384 vmcs12->vm_entry_controls & VM_ENTRY_LOAD_BNDCFGS)
Paolo Bonzini36be0b92014-02-24 12:30:04 +010010385 vmcs_write64(GUEST_BNDCFGS, vmcs12->guest_bndcfgs);
10386
Nadav Har'El27fc51b2011-08-02 15:54:52 +030010387 if (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING)
10388 vmcs_write64(TSC_OFFSET,
Paolo Bonziniea26e4e2016-11-01 00:39:48 +010010389 vcpu->arch.tsc_offset + vmcs12->tsc_offset);
Nadav Har'El27fc51b2011-08-02 15:54:52 +030010390 else
Paolo Bonziniea26e4e2016-11-01 00:39:48 +010010391 vmcs_write64(TSC_OFFSET, vcpu->arch.tsc_offset);
Peter Feinerc95ba922016-08-17 09:36:47 -070010392 if (kvm_has_tsc_control)
10393 decache_tsc_multiplier(vmx);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010394
10395 if (enable_vpid) {
10396 /*
Wanpeng Li5c614b32015-10-13 09:18:36 -070010397 * There is no direct mapping between vpid02 and vpid12, the
10398 * vpid02 is per-vCPU for L0 and reused while the value of
10399 * vpid12 is changed w/ one invvpid during nested vmentry.
10400 * The vpid12 is allocated by L1 for L2, so it will not
10401 * influence global bitmap(for vpid01 and vpid02 allocation)
10402 * even if spawn a lot of nested vCPUs.
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010403 */
Wanpeng Li5c614b32015-10-13 09:18:36 -070010404 if (nested_cpu_has_vpid(vmcs12) && vmx->nested.vpid02) {
10405 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->nested.vpid02);
10406 if (vmcs12->virtual_processor_id != vmx->nested.last_vpid) {
10407 vmx->nested.last_vpid = vmcs12->virtual_processor_id;
10408 __vmx_flush_tlb(vcpu, to_vmx(vcpu)->nested.vpid02);
10409 }
10410 } else {
10411 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid);
10412 vmx_flush_tlb(vcpu);
10413 }
10414
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010415 }
10416
Ladi Prosek1fb883b2017-04-04 14:18:53 +020010417 if (enable_pml) {
10418 /*
10419 * Conceptually we want to copy the PML address and index from
10420 * vmcs01 here, and then back to vmcs01 on nested vmexit. But,
10421 * since we always flush the log on each vmexit, this happens
10422 * to be equivalent to simply resetting the fields in vmcs02.
10423 */
10424 ASSERT(vmx->pml_pg);
10425 vmcs_write64(PML_ADDRESS, page_to_phys(vmx->pml_pg));
10426 vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1);
10427 }
10428
Nadav Har'El155a97a2013-08-05 11:07:16 +030010429 if (nested_cpu_has_ept(vmcs12)) {
Paolo Bonziniae1e2d12017-03-30 11:55:30 +020010430 if (nested_ept_init_mmu_context(vcpu)) {
10431 *entry_failure_code = ENTRY_FAIL_DEFAULT;
10432 return 1;
10433 }
Jim Mattsonfb6c8192017-03-16 13:53:59 -070010434 } else if (nested_cpu_has2(vmcs12,
10435 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) {
10436 vmx_flush_tlb_ept_only(vcpu);
Nadav Har'El155a97a2013-08-05 11:07:16 +030010437 }
10438
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010439 /*
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -080010440 * This sets GUEST_CR0 to vmcs12->guest_cr0, possibly modifying those
10441 * bits which we consider mandatory enabled.
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010442 * The CR0_READ_SHADOW is what L2 should have expected to read given
10443 * the specifications by L1; It's not enough to take
10444 * vmcs12->cr0_read_shadow because on our cr0_guest_host_mask we we
10445 * have more bits than L1 expected.
10446 */
10447 vmx_set_cr0(vcpu, vmcs12->guest_cr0);
10448 vmcs_writel(CR0_READ_SHADOW, nested_read_cr0(vmcs12));
10449
10450 vmx_set_cr4(vcpu, vmcs12->guest_cr4);
10451 vmcs_writel(CR4_READ_SHADOW, nested_read_cr4(vmcs12));
10452
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080010453 if (from_vmentry &&
10454 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_EFER))
David Matlack5a6a9742016-11-29 18:14:10 -080010455 vcpu->arch.efer = vmcs12->guest_ia32_efer;
10456 else if (vmcs12->vm_entry_controls & VM_ENTRY_IA32E_MODE)
10457 vcpu->arch.efer |= (EFER_LMA | EFER_LME);
10458 else
10459 vcpu->arch.efer &= ~(EFER_LMA | EFER_LME);
10460 /* Note: modifies VM_ENTRY/EXIT_CONTROLS and GUEST/HOST_IA32_EFER */
10461 vmx_set_efer(vcpu, vcpu->arch.efer);
10462
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010010463 /* Shadow page tables on either EPT or shadow page tables. */
Ladi Prosek7ad658b2017-03-23 07:18:08 +010010464 if (nested_vmx_load_cr3(vcpu, vmcs12->guest_cr3, nested_cpu_has_ept(vmcs12),
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010010465 entry_failure_code))
10466 return 1;
Ladi Prosek7ca29de2016-11-30 16:03:08 +010010467
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +030010468 if (!enable_ept)
10469 vcpu->arch.walk_mmu->inject_page_fault = vmx_inject_page_fault_nested;
10470
Nadav Har'El3633cfc2013-08-05 11:07:07 +030010471 /*
10472 * L1 may access the L2's PDPTR, so save them to construct vmcs12
10473 */
10474 if (enable_ept) {
10475 vmcs_write64(GUEST_PDPTR0, vmcs12->guest_pdptr0);
10476 vmcs_write64(GUEST_PDPTR1, vmcs12->guest_pdptr1);
10477 vmcs_write64(GUEST_PDPTR2, vmcs12->guest_pdptr2);
10478 vmcs_write64(GUEST_PDPTR3, vmcs12->guest_pdptr3);
10479 }
10480
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010481 kvm_register_write(vcpu, VCPU_REGS_RSP, vmcs12->guest_rsp);
10482 kvm_register_write(vcpu, VCPU_REGS_RIP, vmcs12->guest_rip);
Ladi Prosekee146c12016-11-30 16:03:09 +010010483 return 0;
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010484}
10485
Jim Mattsonca0bde22016-11-30 12:03:46 -080010486static int check_vmentry_prereqs(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
10487{
10488 struct vcpu_vmx *vmx = to_vmx(vcpu);
10489
10490 if (vmcs12->guest_activity_state != GUEST_ACTIVITY_ACTIVE &&
10491 vmcs12->guest_activity_state != GUEST_ACTIVITY_HLT)
10492 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
10493
Jim Mattson56a20512017-07-06 16:33:06 -070010494 if (nested_vmx_check_io_bitmap_controls(vcpu, vmcs12))
10495 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
10496
Jim Mattsonca0bde22016-11-30 12:03:46 -080010497 if (nested_vmx_check_msr_bitmap_controls(vcpu, vmcs12))
10498 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
10499
10500 if (nested_vmx_check_apicv_controls(vcpu, vmcs12))
10501 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
10502
10503 if (nested_vmx_check_msr_switch_controls(vcpu, vmcs12))
10504 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
10505
Bandan Dasc5f983f2017-05-05 15:25:14 -040010506 if (nested_vmx_check_pml_controls(vcpu, vmcs12))
10507 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
10508
Jim Mattsonca0bde22016-11-30 12:03:46 -080010509 if (!vmx_control_verify(vmcs12->cpu_based_vm_exec_control,
10510 vmx->nested.nested_vmx_procbased_ctls_low,
10511 vmx->nested.nested_vmx_procbased_ctls_high) ||
Jim Mattson2e5b0bd2017-05-04 11:51:58 -070010512 (nested_cpu_has(vmcs12, CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) &&
10513 !vmx_control_verify(vmcs12->secondary_vm_exec_control,
10514 vmx->nested.nested_vmx_secondary_ctls_low,
10515 vmx->nested.nested_vmx_secondary_ctls_high)) ||
Jim Mattsonca0bde22016-11-30 12:03:46 -080010516 !vmx_control_verify(vmcs12->pin_based_vm_exec_control,
10517 vmx->nested.nested_vmx_pinbased_ctls_low,
10518 vmx->nested.nested_vmx_pinbased_ctls_high) ||
10519 !vmx_control_verify(vmcs12->vm_exit_controls,
10520 vmx->nested.nested_vmx_exit_ctls_low,
10521 vmx->nested.nested_vmx_exit_ctls_high) ||
10522 !vmx_control_verify(vmcs12->vm_entry_controls,
10523 vmx->nested.nested_vmx_entry_ctls_low,
10524 vmx->nested.nested_vmx_entry_ctls_high))
10525 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
10526
Bandan Das27c42a12017-08-03 15:54:42 -040010527 if (nested_cpu_has_vmfunc(vmcs12) &&
10528 (vmcs12->vm_function_control &
10529 ~vmx->nested.nested_vmx_vmfunc_controls))
10530 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
10531
Jim Mattsonc7c2c7092017-05-05 11:28:09 -070010532 if (vmcs12->cr3_target_count > nested_cpu_vmx_misc_cr3_count(vcpu))
10533 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
10534
Jim Mattsonca0bde22016-11-30 12:03:46 -080010535 if (!nested_host_cr0_valid(vcpu, vmcs12->host_cr0) ||
10536 !nested_host_cr4_valid(vcpu, vmcs12->host_cr4) ||
10537 !nested_cr3_valid(vcpu, vmcs12->host_cr3))
10538 return VMXERR_ENTRY_INVALID_HOST_STATE_FIELD;
10539
10540 return 0;
10541}
10542
10543static int check_vmentry_postreqs(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12,
10544 u32 *exit_qual)
10545{
10546 bool ia32e;
10547
10548 *exit_qual = ENTRY_FAIL_DEFAULT;
10549
10550 if (!nested_guest_cr0_valid(vcpu, vmcs12->guest_cr0) ||
10551 !nested_guest_cr4_valid(vcpu, vmcs12->guest_cr4))
10552 return 1;
10553
10554 if (!nested_cpu_has2(vmcs12, SECONDARY_EXEC_SHADOW_VMCS) &&
10555 vmcs12->vmcs_link_pointer != -1ull) {
10556 *exit_qual = ENTRY_FAIL_VMCS_LINK_PTR;
10557 return 1;
10558 }
10559
10560 /*
10561 * If the load IA32_EFER VM-entry control is 1, the following checks
10562 * are performed on the field for the IA32_EFER MSR:
10563 * - Bits reserved in the IA32_EFER MSR must be 0.
10564 * - Bit 10 (corresponding to IA32_EFER.LMA) must equal the value of
10565 * the IA-32e mode guest VM-exit control. It must also be identical
10566 * to bit 8 (LME) if bit 31 in the CR0 field (corresponding to
10567 * CR0.PG) is 1.
10568 */
10569 if (to_vmx(vcpu)->nested.nested_run_pending &&
10570 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_EFER)) {
10571 ia32e = (vmcs12->vm_entry_controls & VM_ENTRY_IA32E_MODE) != 0;
10572 if (!kvm_valid_efer(vcpu, vmcs12->guest_ia32_efer) ||
10573 ia32e != !!(vmcs12->guest_ia32_efer & EFER_LMA) ||
10574 ((vmcs12->guest_cr0 & X86_CR0_PG) &&
10575 ia32e != !!(vmcs12->guest_ia32_efer & EFER_LME)))
10576 return 1;
10577 }
10578
10579 /*
10580 * If the load IA32_EFER VM-exit control is 1, bits reserved in the
10581 * IA32_EFER MSR must be 0 in the field for that register. In addition,
10582 * the values of the LMA and LME bits in the field must each be that of
10583 * the host address-space size VM-exit control.
10584 */
10585 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_EFER) {
10586 ia32e = (vmcs12->vm_exit_controls &
10587 VM_EXIT_HOST_ADDR_SPACE_SIZE) != 0;
10588 if (!kvm_valid_efer(vcpu, vmcs12->host_ia32_efer) ||
10589 ia32e != !!(vmcs12->host_ia32_efer & EFER_LMA) ||
10590 ia32e != !!(vmcs12->host_ia32_efer & EFER_LME))
10591 return 1;
10592 }
10593
10594 return 0;
10595}
10596
Jim Mattson858e25c2016-11-30 12:03:47 -080010597static int enter_vmx_non_root_mode(struct kvm_vcpu *vcpu, bool from_vmentry)
10598{
10599 struct vcpu_vmx *vmx = to_vmx(vcpu);
10600 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
10601 struct loaded_vmcs *vmcs02;
Jim Mattson858e25c2016-11-30 12:03:47 -080010602 u32 msr_entry_idx;
10603 u32 exit_qual;
10604
10605 vmcs02 = nested_get_current_vmcs02(vmx);
10606 if (!vmcs02)
10607 return -ENOMEM;
10608
10609 enter_guest_mode(vcpu);
10610
10611 if (!(vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS))
10612 vmx->nested.vmcs01_debugctl = vmcs_read64(GUEST_IA32_DEBUGCTL);
10613
David Hildenbrand1279a6b12017-03-20 10:00:08 +010010614 vmx_switch_vmcs(vcpu, vmcs02);
Jim Mattson858e25c2016-11-30 12:03:47 -080010615 vmx_segment_cache_clear(vmx);
10616
10617 if (prepare_vmcs02(vcpu, vmcs12, from_vmentry, &exit_qual)) {
10618 leave_guest_mode(vcpu);
David Hildenbrand1279a6b12017-03-20 10:00:08 +010010619 vmx_switch_vmcs(vcpu, &vmx->vmcs01);
Jim Mattson858e25c2016-11-30 12:03:47 -080010620 nested_vmx_entry_failure(vcpu, vmcs12,
10621 EXIT_REASON_INVALID_STATE, exit_qual);
10622 return 1;
10623 }
10624
10625 nested_get_vmcs12_pages(vcpu, vmcs12);
10626
10627 msr_entry_idx = nested_vmx_load_msr(vcpu,
10628 vmcs12->vm_entry_msr_load_addr,
10629 vmcs12->vm_entry_msr_load_count);
10630 if (msr_entry_idx) {
10631 leave_guest_mode(vcpu);
David Hildenbrand1279a6b12017-03-20 10:00:08 +010010632 vmx_switch_vmcs(vcpu, &vmx->vmcs01);
Jim Mattson858e25c2016-11-30 12:03:47 -080010633 nested_vmx_entry_failure(vcpu, vmcs12,
10634 EXIT_REASON_MSR_LOAD_FAIL, msr_entry_idx);
10635 return 1;
10636 }
10637
Jim Mattson858e25c2016-11-30 12:03:47 -080010638 /*
10639 * Note no nested_vmx_succeed or nested_vmx_fail here. At this point
10640 * we are no longer running L1, and VMLAUNCH/VMRESUME has not yet
10641 * returned as far as L1 is concerned. It will only return (and set
10642 * the success flag) when L2 exits (see nested_vmx_vmexit()).
10643 */
10644 return 0;
10645}
10646
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030010647/*
10648 * nested_vmx_run() handles a nested entry, i.e., a VMLAUNCH or VMRESUME on L1
10649 * for running an L2 nested guest.
10650 */
10651static int nested_vmx_run(struct kvm_vcpu *vcpu, bool launch)
10652{
10653 struct vmcs12 *vmcs12;
10654 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jim Mattsonb3f1dfb2017-07-17 12:00:34 -070010655 u32 interrupt_shadow = vmx_get_interrupt_shadow(vcpu);
Jim Mattsonca0bde22016-11-30 12:03:46 -080010656 u32 exit_qual;
10657 int ret;
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030010658
Kyle Hueyeb277562016-11-29 12:40:39 -080010659 if (!nested_vmx_check_permission(vcpu))
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030010660 return 1;
10661
Kyle Hueyeb277562016-11-29 12:40:39 -080010662 if (!nested_vmx_check_vmcs12(vcpu))
10663 goto out;
10664
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030010665 vmcs12 = get_vmcs12(vcpu);
10666
Abel Gordon012f83c2013-04-18 14:39:25 +030010667 if (enable_shadow_vmcs)
10668 copy_shadow_to_vmcs12(vmx);
10669
Nadav Har'El7c177932011-05-25 23:12:04 +030010670 /*
10671 * The nested entry process starts with enforcing various prerequisites
10672 * on vmcs12 as required by the Intel SDM, and act appropriately when
10673 * they fail: As the SDM explains, some conditions should cause the
10674 * instruction to fail, while others will cause the instruction to seem
10675 * to succeed, but return an EXIT_REASON_INVALID_STATE.
10676 * To speed up the normal (success) code path, we should avoid checking
10677 * for misconfigurations which will anyway be caught by the processor
10678 * when using the merged vmcs02.
10679 */
Jim Mattsonb3f1dfb2017-07-17 12:00:34 -070010680 if (interrupt_shadow & KVM_X86_SHADOW_INT_MOV_SS) {
10681 nested_vmx_failValid(vcpu,
10682 VMXERR_ENTRY_EVENTS_BLOCKED_BY_MOV_SS);
10683 goto out;
10684 }
10685
Nadav Har'El7c177932011-05-25 23:12:04 +030010686 if (vmcs12->launch_state == launch) {
10687 nested_vmx_failValid(vcpu,
10688 launch ? VMXERR_VMLAUNCH_NONCLEAR_VMCS
10689 : VMXERR_VMRESUME_NONLAUNCHED_VMCS);
Kyle Hueyeb277562016-11-29 12:40:39 -080010690 goto out;
Nadav Har'El7c177932011-05-25 23:12:04 +030010691 }
10692
Jim Mattsonca0bde22016-11-30 12:03:46 -080010693 ret = check_vmentry_prereqs(vcpu, vmcs12);
10694 if (ret) {
10695 nested_vmx_failValid(vcpu, ret);
Kyle Hueyeb277562016-11-29 12:40:39 -080010696 goto out;
Paolo Bonzini26539bd2013-04-15 15:00:27 +020010697 }
10698
Nadav Har'El7c177932011-05-25 23:12:04 +030010699 /*
Jim Mattsonca0bde22016-11-30 12:03:46 -080010700 * After this point, the trap flag no longer triggers a singlestep trap
10701 * on the vm entry instructions; don't call kvm_skip_emulated_instruction.
10702 * This is not 100% correct; for performance reasons, we delegate most
10703 * of the checks on host state to the processor. If those fail,
10704 * the singlestep trap is missed.
Jan Kiszka384bb782013-04-20 10:52:36 +020010705 */
Jim Mattsonca0bde22016-11-30 12:03:46 -080010706 skip_emulated_instruction(vcpu);
Jan Kiszka384bb782013-04-20 10:52:36 +020010707
Jim Mattsonca0bde22016-11-30 12:03:46 -080010708 ret = check_vmentry_postreqs(vcpu, vmcs12, &exit_qual);
10709 if (ret) {
10710 nested_vmx_entry_failure(vcpu, vmcs12,
10711 EXIT_REASON_INVALID_STATE, exit_qual);
10712 return 1;
Jan Kiszka384bb782013-04-20 10:52:36 +020010713 }
10714
10715 /*
Nadav Har'El7c177932011-05-25 23:12:04 +030010716 * We're finally done with prerequisite checking, and can start with
10717 * the nested entry.
10718 */
10719
Jim Mattson858e25c2016-11-30 12:03:47 -080010720 ret = enter_vmx_non_root_mode(vcpu, true);
10721 if (ret)
10722 return ret;
Wincy Vanff651cb2014-12-11 08:52:58 +030010723
Jan Kiszka6dfacad2013-12-04 08:58:54 +010010724 if (vmcs12->guest_activity_state == GUEST_ACTIVITY_HLT)
Joel Schopp5cb56052015-03-02 13:43:31 -060010725 return kvm_vcpu_halt(vcpu);
Jan Kiszka6dfacad2013-12-04 08:58:54 +010010726
Jan Kiszka7af40ad32014-01-04 18:47:23 +010010727 vmx->nested.nested_run_pending = 1;
10728
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030010729 return 1;
Kyle Hueyeb277562016-11-29 12:40:39 -080010730
10731out:
Kyle Huey6affcbe2016-11-29 12:40:40 -080010732 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030010733}
10734
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010735/*
10736 * On a nested exit from L2 to L1, vmcs12.guest_cr0 might not be up-to-date
10737 * because L2 may have changed some cr0 bits directly (CRO_GUEST_HOST_MASK).
10738 * This function returns the new value we should put in vmcs12.guest_cr0.
10739 * It's not enough to just return the vmcs02 GUEST_CR0. Rather,
10740 * 1. Bits that neither L0 nor L1 trapped, were set directly by L2 and are now
10741 * available in vmcs02 GUEST_CR0. (Note: It's enough to check that L0
10742 * didn't trap the bit, because if L1 did, so would L0).
10743 * 2. Bits that L1 asked to trap (and therefore L0 also did) could not have
10744 * been modified by L2, and L1 knows it. So just leave the old value of
10745 * the bit from vmcs12.guest_cr0. Note that the bit from vmcs02 GUEST_CR0
10746 * isn't relevant, because if L0 traps this bit it can set it to anything.
10747 * 3. Bits that L1 didn't trap, but L0 did. L1 believes the guest could have
10748 * changed these bits, and therefore they need to be updated, but L0
10749 * didn't necessarily allow them to be changed in GUEST_CR0 - and rather
10750 * put them in vmcs02 CR0_READ_SHADOW. So take these bits from there.
10751 */
10752static inline unsigned long
10753vmcs12_guest_cr0(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
10754{
10755 return
10756 /*1*/ (vmcs_readl(GUEST_CR0) & vcpu->arch.cr0_guest_owned_bits) |
10757 /*2*/ (vmcs12->guest_cr0 & vmcs12->cr0_guest_host_mask) |
10758 /*3*/ (vmcs_readl(CR0_READ_SHADOW) & ~(vmcs12->cr0_guest_host_mask |
10759 vcpu->arch.cr0_guest_owned_bits));
10760}
10761
10762static inline unsigned long
10763vmcs12_guest_cr4(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
10764{
10765 return
10766 /*1*/ (vmcs_readl(GUEST_CR4) & vcpu->arch.cr4_guest_owned_bits) |
10767 /*2*/ (vmcs12->guest_cr4 & vmcs12->cr4_guest_host_mask) |
10768 /*3*/ (vmcs_readl(CR4_READ_SHADOW) & ~(vmcs12->cr4_guest_host_mask |
10769 vcpu->arch.cr4_guest_owned_bits));
10770}
10771
Jan Kiszka5f3d5792013-04-14 12:12:46 +020010772static void vmcs12_save_pending_event(struct kvm_vcpu *vcpu,
10773 struct vmcs12 *vmcs12)
10774{
10775 u32 idt_vectoring;
10776 unsigned int nr;
10777
Gleb Natapov851eb6672013-09-25 12:51:34 +030010778 if (vcpu->arch.exception.pending && vcpu->arch.exception.reinject) {
Jan Kiszka5f3d5792013-04-14 12:12:46 +020010779 nr = vcpu->arch.exception.nr;
10780 idt_vectoring = nr | VECTORING_INFO_VALID_MASK;
10781
10782 if (kvm_exception_is_soft(nr)) {
10783 vmcs12->vm_exit_instruction_len =
10784 vcpu->arch.event_exit_inst_len;
10785 idt_vectoring |= INTR_TYPE_SOFT_EXCEPTION;
10786 } else
10787 idt_vectoring |= INTR_TYPE_HARD_EXCEPTION;
10788
10789 if (vcpu->arch.exception.has_error_code) {
10790 idt_vectoring |= VECTORING_INFO_DELIVER_CODE_MASK;
10791 vmcs12->idt_vectoring_error_code =
10792 vcpu->arch.exception.error_code;
10793 }
10794
10795 vmcs12->idt_vectoring_info_field = idt_vectoring;
Jan Kiszkacd2633c2013-10-23 17:42:15 +010010796 } else if (vcpu->arch.nmi_injected) {
Jan Kiszka5f3d5792013-04-14 12:12:46 +020010797 vmcs12->idt_vectoring_info_field =
10798 INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK | NMI_VECTOR;
10799 } else if (vcpu->arch.interrupt.pending) {
10800 nr = vcpu->arch.interrupt.nr;
10801 idt_vectoring = nr | VECTORING_INFO_VALID_MASK;
10802
10803 if (vcpu->arch.interrupt.soft) {
10804 idt_vectoring |= INTR_TYPE_SOFT_INTR;
10805 vmcs12->vm_entry_instruction_len =
10806 vcpu->arch.event_exit_inst_len;
10807 } else
10808 idt_vectoring |= INTR_TYPE_EXT_INTR;
10809
10810 vmcs12->idt_vectoring_info_field = idt_vectoring;
10811 }
10812}
10813
Jan Kiszkab6b8a142014-03-07 20:03:12 +010010814static int vmx_check_nested_events(struct kvm_vcpu *vcpu, bool external_intr)
10815{
10816 struct vcpu_vmx *vmx = to_vmx(vcpu);
10817
Wanpeng Liacc9ab62017-02-27 04:24:39 -080010818 if (vcpu->arch.exception.pending ||
10819 vcpu->arch.nmi_injected ||
10820 vcpu->arch.interrupt.pending)
10821 return -EBUSY;
10822
Jan Kiszkaf41245002014-03-07 20:03:13 +010010823 if (nested_cpu_has_preemption_timer(get_vmcs12(vcpu)) &&
10824 vmx->nested.preemption_timer_expired) {
10825 if (vmx->nested.nested_run_pending)
10826 return -EBUSY;
10827 nested_vmx_vmexit(vcpu, EXIT_REASON_PREEMPTION_TIMER, 0, 0);
10828 return 0;
10829 }
10830
Jan Kiszkab6b8a142014-03-07 20:03:12 +010010831 if (vcpu->arch.nmi_pending && nested_exit_on_nmi(vcpu)) {
Wanpeng Liacc9ab62017-02-27 04:24:39 -080010832 if (vmx->nested.nested_run_pending)
Jan Kiszkab6b8a142014-03-07 20:03:12 +010010833 return -EBUSY;
10834 nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI,
10835 NMI_VECTOR | INTR_TYPE_NMI_INTR |
10836 INTR_INFO_VALID_MASK, 0);
10837 /*
10838 * The NMI-triggered VM exit counts as injection:
10839 * clear this one and block further NMIs.
10840 */
10841 vcpu->arch.nmi_pending = 0;
10842 vmx_set_nmi_mask(vcpu, true);
10843 return 0;
10844 }
10845
10846 if ((kvm_cpu_has_interrupt(vcpu) || external_intr) &&
10847 nested_exit_on_intr(vcpu)) {
10848 if (vmx->nested.nested_run_pending)
10849 return -EBUSY;
10850 nested_vmx_vmexit(vcpu, EXIT_REASON_EXTERNAL_INTERRUPT, 0, 0);
Wincy Van705699a2015-02-03 23:58:17 +080010851 return 0;
Jan Kiszkab6b8a142014-03-07 20:03:12 +010010852 }
10853
David Hildenbrand6342c502017-01-25 11:58:58 +010010854 vmx_complete_nested_posted_interrupt(vcpu);
10855 return 0;
Jan Kiszkab6b8a142014-03-07 20:03:12 +010010856}
10857
Jan Kiszkaf41245002014-03-07 20:03:13 +010010858static u32 vmx_get_preemption_timer_value(struct kvm_vcpu *vcpu)
10859{
10860 ktime_t remaining =
10861 hrtimer_get_remaining(&to_vmx(vcpu)->nested.preemption_timer);
10862 u64 value;
10863
10864 if (ktime_to_ns(remaining) <= 0)
10865 return 0;
10866
10867 value = ktime_to_ns(remaining) * vcpu->arch.virtual_tsc_khz;
10868 do_div(value, 1000000);
10869 return value >> VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE;
10870}
10871
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010872/*
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080010873 * Update the guest state fields of vmcs12 to reflect changes that
10874 * occurred while L2 was running. (The "IA-32e mode guest" bit of the
10875 * VM-entry controls is also updated, since this is really a guest
10876 * state bit.)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010877 */
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080010878static void sync_vmcs12(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010879{
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010880 vmcs12->guest_cr0 = vmcs12_guest_cr0(vcpu, vmcs12);
10881 vmcs12->guest_cr4 = vmcs12_guest_cr4(vcpu, vmcs12);
10882
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010883 vmcs12->guest_rsp = kvm_register_read(vcpu, VCPU_REGS_RSP);
10884 vmcs12->guest_rip = kvm_register_read(vcpu, VCPU_REGS_RIP);
10885 vmcs12->guest_rflags = vmcs_readl(GUEST_RFLAGS);
10886
10887 vmcs12->guest_es_selector = vmcs_read16(GUEST_ES_SELECTOR);
10888 vmcs12->guest_cs_selector = vmcs_read16(GUEST_CS_SELECTOR);
10889 vmcs12->guest_ss_selector = vmcs_read16(GUEST_SS_SELECTOR);
10890 vmcs12->guest_ds_selector = vmcs_read16(GUEST_DS_SELECTOR);
10891 vmcs12->guest_fs_selector = vmcs_read16(GUEST_FS_SELECTOR);
10892 vmcs12->guest_gs_selector = vmcs_read16(GUEST_GS_SELECTOR);
10893 vmcs12->guest_ldtr_selector = vmcs_read16(GUEST_LDTR_SELECTOR);
10894 vmcs12->guest_tr_selector = vmcs_read16(GUEST_TR_SELECTOR);
10895 vmcs12->guest_es_limit = vmcs_read32(GUEST_ES_LIMIT);
10896 vmcs12->guest_cs_limit = vmcs_read32(GUEST_CS_LIMIT);
10897 vmcs12->guest_ss_limit = vmcs_read32(GUEST_SS_LIMIT);
10898 vmcs12->guest_ds_limit = vmcs_read32(GUEST_DS_LIMIT);
10899 vmcs12->guest_fs_limit = vmcs_read32(GUEST_FS_LIMIT);
10900 vmcs12->guest_gs_limit = vmcs_read32(GUEST_GS_LIMIT);
10901 vmcs12->guest_ldtr_limit = vmcs_read32(GUEST_LDTR_LIMIT);
10902 vmcs12->guest_tr_limit = vmcs_read32(GUEST_TR_LIMIT);
10903 vmcs12->guest_gdtr_limit = vmcs_read32(GUEST_GDTR_LIMIT);
10904 vmcs12->guest_idtr_limit = vmcs_read32(GUEST_IDTR_LIMIT);
10905 vmcs12->guest_es_ar_bytes = vmcs_read32(GUEST_ES_AR_BYTES);
10906 vmcs12->guest_cs_ar_bytes = vmcs_read32(GUEST_CS_AR_BYTES);
10907 vmcs12->guest_ss_ar_bytes = vmcs_read32(GUEST_SS_AR_BYTES);
10908 vmcs12->guest_ds_ar_bytes = vmcs_read32(GUEST_DS_AR_BYTES);
10909 vmcs12->guest_fs_ar_bytes = vmcs_read32(GUEST_FS_AR_BYTES);
10910 vmcs12->guest_gs_ar_bytes = vmcs_read32(GUEST_GS_AR_BYTES);
10911 vmcs12->guest_ldtr_ar_bytes = vmcs_read32(GUEST_LDTR_AR_BYTES);
10912 vmcs12->guest_tr_ar_bytes = vmcs_read32(GUEST_TR_AR_BYTES);
10913 vmcs12->guest_es_base = vmcs_readl(GUEST_ES_BASE);
10914 vmcs12->guest_cs_base = vmcs_readl(GUEST_CS_BASE);
10915 vmcs12->guest_ss_base = vmcs_readl(GUEST_SS_BASE);
10916 vmcs12->guest_ds_base = vmcs_readl(GUEST_DS_BASE);
10917 vmcs12->guest_fs_base = vmcs_readl(GUEST_FS_BASE);
10918 vmcs12->guest_gs_base = vmcs_readl(GUEST_GS_BASE);
10919 vmcs12->guest_ldtr_base = vmcs_readl(GUEST_LDTR_BASE);
10920 vmcs12->guest_tr_base = vmcs_readl(GUEST_TR_BASE);
10921 vmcs12->guest_gdtr_base = vmcs_readl(GUEST_GDTR_BASE);
10922 vmcs12->guest_idtr_base = vmcs_readl(GUEST_IDTR_BASE);
10923
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010924 vmcs12->guest_interruptibility_info =
10925 vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
10926 vmcs12->guest_pending_dbg_exceptions =
10927 vmcs_readl(GUEST_PENDING_DBG_EXCEPTIONS);
Jan Kiszka3edf1e62014-01-04 18:47:24 +010010928 if (vcpu->arch.mp_state == KVM_MP_STATE_HALTED)
10929 vmcs12->guest_activity_state = GUEST_ACTIVITY_HLT;
10930 else
10931 vmcs12->guest_activity_state = GUEST_ACTIVITY_ACTIVE;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010932
Jan Kiszkaf41245002014-03-07 20:03:13 +010010933 if (nested_cpu_has_preemption_timer(vmcs12)) {
10934 if (vmcs12->vm_exit_controls &
10935 VM_EXIT_SAVE_VMX_PREEMPTION_TIMER)
10936 vmcs12->vmx_preemption_timer_value =
10937 vmx_get_preemption_timer_value(vcpu);
10938 hrtimer_cancel(&to_vmx(vcpu)->nested.preemption_timer);
10939 }
Arthur Chunqi Li7854cbc2013-09-16 16:11:44 +080010940
Nadav Har'El3633cfc2013-08-05 11:07:07 +030010941 /*
10942 * In some cases (usually, nested EPT), L2 is allowed to change its
10943 * own CR3 without exiting. If it has changed it, we must keep it.
10944 * Of course, if L0 is using shadow page tables, GUEST_CR3 was defined
10945 * by L0, not L1 or L2, so we mustn't unconditionally copy it to vmcs12.
10946 *
10947 * Additionally, restore L2's PDPTR to vmcs12.
10948 */
10949 if (enable_ept) {
Paolo Bonzinif3531052015-12-03 15:49:56 +010010950 vmcs12->guest_cr3 = vmcs_readl(GUEST_CR3);
Nadav Har'El3633cfc2013-08-05 11:07:07 +030010951 vmcs12->guest_pdptr0 = vmcs_read64(GUEST_PDPTR0);
10952 vmcs12->guest_pdptr1 = vmcs_read64(GUEST_PDPTR1);
10953 vmcs12->guest_pdptr2 = vmcs_read64(GUEST_PDPTR2);
10954 vmcs12->guest_pdptr3 = vmcs_read64(GUEST_PDPTR3);
10955 }
10956
Jim Mattsond281e132017-06-01 12:44:46 -070010957 vmcs12->guest_linear_address = vmcs_readl(GUEST_LINEAR_ADDRESS);
Jan Dakinevich119a9c02016-09-04 21:22:47 +030010958
Wincy Van608406e2015-02-03 23:57:51 +080010959 if (nested_cpu_has_vid(vmcs12))
10960 vmcs12->guest_intr_status = vmcs_read16(GUEST_INTR_STATUS);
10961
Jan Kiszkac18911a2013-03-13 16:06:41 +010010962 vmcs12->vm_entry_controls =
10963 (vmcs12->vm_entry_controls & ~VM_ENTRY_IA32E_MODE) |
Gleb Natapov2961e8762013-11-25 15:37:13 +020010964 (vm_entry_controls_get(to_vmx(vcpu)) & VM_ENTRY_IA32E_MODE);
Jan Kiszkac18911a2013-03-13 16:06:41 +010010965
Jan Kiszka2996fca2014-06-16 13:59:43 +020010966 if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_DEBUG_CONTROLS) {
10967 kvm_get_dr(vcpu, 7, (unsigned long *)&vmcs12->guest_dr7);
10968 vmcs12->guest_ia32_debugctl = vmcs_read64(GUEST_IA32_DEBUGCTL);
10969 }
10970
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010971 /* TODO: These cannot have changed unless we have MSR bitmaps and
10972 * the relevant bit asks not to trap the change */
Jan Kiszkab8c07d52013-04-06 13:51:21 +020010973 if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_IA32_PAT)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010974 vmcs12->guest_ia32_pat = vmcs_read64(GUEST_IA32_PAT);
Jan Kiszka10ba54a2013-08-08 16:26:31 +020010975 if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_IA32_EFER)
10976 vmcs12->guest_ia32_efer = vcpu->arch.efer;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010977 vmcs12->guest_sysenter_cs = vmcs_read32(GUEST_SYSENTER_CS);
10978 vmcs12->guest_sysenter_esp = vmcs_readl(GUEST_SYSENTER_ESP);
10979 vmcs12->guest_sysenter_eip = vmcs_readl(GUEST_SYSENTER_EIP);
Paolo Bonzinia87036a2016-03-08 09:52:13 +010010980 if (kvm_mpx_supported())
Paolo Bonzini36be0b92014-02-24 12:30:04 +010010981 vmcs12->guest_bndcfgs = vmcs_read64(GUEST_BNDCFGS);
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080010982}
10983
10984/*
10985 * prepare_vmcs12 is part of what we need to do when the nested L2 guest exits
10986 * and we want to prepare to run its L1 parent. L1 keeps a vmcs for L2 (vmcs12),
10987 * and this function updates it to reflect the changes to the guest state while
10988 * L2 was running (and perhaps made some exits which were handled directly by L0
10989 * without going back to L1), and to reflect the exit reason.
10990 * Note that we do not have to copy here all VMCS fields, just those that
10991 * could have changed by the L2 guest or the exit - i.e., the guest-state and
10992 * exit-information fields only. Other fields are modified by L1 with VMWRITE,
10993 * which already writes to vmcs12 directly.
10994 */
10995static void prepare_vmcs12(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12,
10996 u32 exit_reason, u32 exit_intr_info,
10997 unsigned long exit_qualification)
10998{
10999 /* update guest state fields: */
11000 sync_vmcs12(vcpu, vmcs12);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011001
11002 /* update exit information fields: */
11003
Jan Kiszka533558b2014-01-04 18:47:20 +010011004 vmcs12->vm_exit_reason = exit_reason;
11005 vmcs12->exit_qualification = exit_qualification;
Jan Kiszka533558b2014-01-04 18:47:20 +010011006 vmcs12->vm_exit_intr_info = exit_intr_info;
Paolo Bonzini7313c692017-07-27 10:31:25 +020011007
Jan Kiszka5f3d5792013-04-14 12:12:46 +020011008 vmcs12->idt_vectoring_info_field = 0;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011009 vmcs12->vm_exit_instruction_len = vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
11010 vmcs12->vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
11011
Jan Kiszka5f3d5792013-04-14 12:12:46 +020011012 if (!(vmcs12->vm_exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY)) {
Jim Mattson7cdc2d62017-07-06 16:33:05 -070011013 vmcs12->launch_state = 1;
11014
Jan Kiszka5f3d5792013-04-14 12:12:46 +020011015 /* vm_entry_intr_info_field is cleared on exit. Emulate this
11016 * instead of reading the real value. */
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011017 vmcs12->vm_entry_intr_info_field &= ~INTR_INFO_VALID_MASK;
Jan Kiszka5f3d5792013-04-14 12:12:46 +020011018
11019 /*
11020 * Transfer the event that L0 or L1 may wanted to inject into
11021 * L2 to IDT_VECTORING_INFO_FIELD.
11022 */
11023 vmcs12_save_pending_event(vcpu, vmcs12);
11024 }
11025
11026 /*
11027 * Drop what we picked up for L2 via vmx_complete_interrupts. It is
11028 * preserved above and would only end up incorrectly in L1.
11029 */
11030 vcpu->arch.nmi_injected = false;
11031 kvm_clear_exception_queue(vcpu);
11032 kvm_clear_interrupt_queue(vcpu);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011033}
11034
11035/*
11036 * A part of what we need to when the nested L2 guest exits and we want to
11037 * run its L1 parent, is to reset L1's guest state to the host state specified
11038 * in vmcs12.
11039 * This function is to be called not only on normal nested exit, but also on
11040 * a nested entry failure, as explained in Intel's spec, 3B.23.7 ("VM-Entry
11041 * Failures During or After Loading Guest State").
11042 * This function should be called when the active VMCS is L1's (vmcs01).
11043 */
Jan Kiszka733568f2013-02-23 15:07:47 +010011044static void load_vmcs12_host_state(struct kvm_vcpu *vcpu,
11045 struct vmcs12 *vmcs12)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011046{
Arthur Chunqi Li21feb4e2013-07-15 16:04:08 +080011047 struct kvm_segment seg;
Jim Mattsonca0bde22016-11-30 12:03:46 -080011048 u32 entry_failure_code;
Arthur Chunqi Li21feb4e2013-07-15 16:04:08 +080011049
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011050 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_EFER)
11051 vcpu->arch.efer = vmcs12->host_ia32_efer;
Jan Kiszkad1fa0352013-04-14 12:44:54 +020011052 else if (vmcs12->vm_exit_controls & VM_EXIT_HOST_ADDR_SPACE_SIZE)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011053 vcpu->arch.efer |= (EFER_LMA | EFER_LME);
11054 else
11055 vcpu->arch.efer &= ~(EFER_LMA | EFER_LME);
11056 vmx_set_efer(vcpu, vcpu->arch.efer);
11057
11058 kvm_register_write(vcpu, VCPU_REGS_RSP, vmcs12->host_rsp);
11059 kvm_register_write(vcpu, VCPU_REGS_RIP, vmcs12->host_rip);
H. Peter Anvin1adfa762013-04-27 16:10:11 -070011060 vmx_set_rflags(vcpu, X86_EFLAGS_FIXED);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011061 /*
11062 * Note that calling vmx_set_cr0 is important, even if cr0 hasn't
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -080011063 * actually changed, because vmx_set_cr0 refers to efer set above.
11064 *
11065 * CR0_GUEST_HOST_MASK is already set in the original vmcs01
11066 * (KVM doesn't change it);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011067 */
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -080011068 vcpu->arch.cr0_guest_owned_bits = X86_CR0_TS;
Jan Kiszka9e3e4dbf2013-09-03 21:11:45 +020011069 vmx_set_cr0(vcpu, vmcs12->host_cr0);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011070
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -080011071 /* Same as above - no reason to call set_cr4_guest_host_mask(). */
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011072 vcpu->arch.cr4_guest_owned_bits = ~vmcs_readl(CR4_GUEST_HOST_MASK);
11073 kvm_set_cr4(vcpu, vmcs12->host_cr4);
11074
Jan Kiszka29bf08f2013-12-28 16:31:52 +010011075 nested_ept_uninit_mmu_context(vcpu);
Nadav Har'El155a97a2013-08-05 11:07:16 +030011076
Ladi Prosek1dc35da2016-11-30 16:03:11 +010011077 /*
11078 * Only PDPTE load can fail as the value of cr3 was checked on entry and
11079 * couldn't have changed.
11080 */
11081 if (nested_vmx_load_cr3(vcpu, vmcs12->host_cr3, false, &entry_failure_code))
11082 nested_vmx_abort(vcpu, VMX_ABORT_LOAD_HOST_PDPTE_FAIL);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011083
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +030011084 if (!enable_ept)
11085 vcpu->arch.walk_mmu->inject_page_fault = kvm_inject_page_fault;
11086
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011087 if (enable_vpid) {
11088 /*
11089 * Trivially support vpid by letting L2s share their parent
11090 * L1's vpid. TODO: move to a more elaborate solution, giving
11091 * each L2 its own vpid and exposing the vpid feature to L1.
11092 */
11093 vmx_flush_tlb(vcpu);
11094 }
Wincy Van06a55242017-04-28 13:13:59 +080011095 /* Restore posted intr vector. */
11096 if (nested_cpu_has_posted_intr(vmcs12))
11097 vmcs_write16(POSTED_INTR_NV, POSTED_INTR_VECTOR);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011098
11099 vmcs_write32(GUEST_SYSENTER_CS, vmcs12->host_ia32_sysenter_cs);
11100 vmcs_writel(GUEST_SYSENTER_ESP, vmcs12->host_ia32_sysenter_esp);
11101 vmcs_writel(GUEST_SYSENTER_EIP, vmcs12->host_ia32_sysenter_eip);
11102 vmcs_writel(GUEST_IDTR_BASE, vmcs12->host_idtr_base);
11103 vmcs_writel(GUEST_GDTR_BASE, vmcs12->host_gdtr_base);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011104
Paolo Bonzini36be0b92014-02-24 12:30:04 +010011105 /* If not VM_EXIT_CLEAR_BNDCFGS, the L2 value propagates to L1. */
11106 if (vmcs12->vm_exit_controls & VM_EXIT_CLEAR_BNDCFGS)
11107 vmcs_write64(GUEST_BNDCFGS, 0);
11108
Jan Kiszka44811c02013-08-04 17:17:27 +020011109 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PAT) {
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011110 vmcs_write64(GUEST_IA32_PAT, vmcs12->host_ia32_pat);
Jan Kiszka44811c02013-08-04 17:17:27 +020011111 vcpu->arch.pat = vmcs12->host_ia32_pat;
11112 }
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011113 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL)
11114 vmcs_write64(GUEST_IA32_PERF_GLOBAL_CTRL,
11115 vmcs12->host_ia32_perf_global_ctrl);
Jan Kiszka503cd0c2013-03-03 13:05:44 +010011116
Arthur Chunqi Li21feb4e2013-07-15 16:04:08 +080011117 /* Set L1 segment info according to Intel SDM
11118 27.5.2 Loading Host Segment and Descriptor-Table Registers */
11119 seg = (struct kvm_segment) {
11120 .base = 0,
11121 .limit = 0xFFFFFFFF,
11122 .selector = vmcs12->host_cs_selector,
11123 .type = 11,
11124 .present = 1,
11125 .s = 1,
11126 .g = 1
11127 };
11128 if (vmcs12->vm_exit_controls & VM_EXIT_HOST_ADDR_SPACE_SIZE)
11129 seg.l = 1;
11130 else
11131 seg.db = 1;
11132 vmx_set_segment(vcpu, &seg, VCPU_SREG_CS);
11133 seg = (struct kvm_segment) {
11134 .base = 0,
11135 .limit = 0xFFFFFFFF,
11136 .type = 3,
11137 .present = 1,
11138 .s = 1,
11139 .db = 1,
11140 .g = 1
11141 };
11142 seg.selector = vmcs12->host_ds_selector;
11143 vmx_set_segment(vcpu, &seg, VCPU_SREG_DS);
11144 seg.selector = vmcs12->host_es_selector;
11145 vmx_set_segment(vcpu, &seg, VCPU_SREG_ES);
11146 seg.selector = vmcs12->host_ss_selector;
11147 vmx_set_segment(vcpu, &seg, VCPU_SREG_SS);
11148 seg.selector = vmcs12->host_fs_selector;
11149 seg.base = vmcs12->host_fs_base;
11150 vmx_set_segment(vcpu, &seg, VCPU_SREG_FS);
11151 seg.selector = vmcs12->host_gs_selector;
11152 seg.base = vmcs12->host_gs_base;
11153 vmx_set_segment(vcpu, &seg, VCPU_SREG_GS);
11154 seg = (struct kvm_segment) {
Gleb Natapov205befd2013-08-04 15:08:06 +030011155 .base = vmcs12->host_tr_base,
Arthur Chunqi Li21feb4e2013-07-15 16:04:08 +080011156 .limit = 0x67,
11157 .selector = vmcs12->host_tr_selector,
11158 .type = 11,
11159 .present = 1
11160 };
11161 vmx_set_segment(vcpu, &seg, VCPU_SREG_TR);
11162
Jan Kiszka503cd0c2013-03-03 13:05:44 +010011163 kvm_set_dr(vcpu, 7, 0x400);
11164 vmcs_write64(GUEST_IA32_DEBUGCTL, 0);
Wincy Vanff651cb2014-12-11 08:52:58 +030011165
Wincy Van3af18d92015-02-03 23:49:31 +080011166 if (cpu_has_vmx_msr_bitmap())
11167 vmx_set_msr_bitmap(vcpu);
11168
Wincy Vanff651cb2014-12-11 08:52:58 +030011169 if (nested_vmx_load_msr(vcpu, vmcs12->vm_exit_msr_load_addr,
11170 vmcs12->vm_exit_msr_load_count))
11171 nested_vmx_abort(vcpu, VMX_ABORT_LOAD_HOST_MSR_FAIL);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011172}
11173
11174/*
11175 * Emulate an exit from nested guest (L2) to L1, i.e., prepare to run L1
11176 * and modify vmcs12 to make it see what it would expect to see there if
11177 * L2 was its real guest. Must only be called when in L2 (is_guest_mode())
11178 */
Jan Kiszka533558b2014-01-04 18:47:20 +010011179static void nested_vmx_vmexit(struct kvm_vcpu *vcpu, u32 exit_reason,
11180 u32 exit_intr_info,
11181 unsigned long exit_qualification)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011182{
11183 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011184 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Jim Mattsoncf3215d2016-09-06 09:33:21 -070011185 u32 vm_inst_error = 0;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011186
Jan Kiszka5f3d5792013-04-14 12:12:46 +020011187 /* trying to cancel vmlaunch/vmresume is a bug */
11188 WARN_ON_ONCE(vmx->nested.nested_run_pending);
11189
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011190 leave_guest_mode(vcpu);
Jan Kiszka533558b2014-01-04 18:47:20 +010011191 prepare_vmcs12(vcpu, vmcs12, exit_reason, exit_intr_info,
11192 exit_qualification);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011193
Wincy Vanff651cb2014-12-11 08:52:58 +030011194 if (nested_vmx_store_msr(vcpu, vmcs12->vm_exit_msr_store_addr,
11195 vmcs12->vm_exit_msr_store_count))
11196 nested_vmx_abort(vcpu, VMX_ABORT_SAVE_GUEST_MSR_FAIL);
11197
Jim Mattsoncf3215d2016-09-06 09:33:21 -070011198 if (unlikely(vmx->fail))
11199 vm_inst_error = vmcs_read32(VM_INSTRUCTION_ERROR);
11200
David Hildenbrand1279a6b12017-03-20 10:00:08 +010011201 vmx_switch_vmcs(vcpu, &vmx->vmcs01);
Wanpeng Lif3380ca52014-08-05 12:42:23 +080011202
Wanpeng Li6550c4d2017-07-31 19:25:27 -070011203 /*
11204 * TODO: SDM says that with acknowledge interrupt on exit, bit 31 of
11205 * the VM-exit interrupt information (valid interrupt) is always set to
11206 * 1 on EXIT_REASON_EXTERNAL_INTERRUPT, so we shouldn't need
11207 * kvm_cpu_has_interrupt(). See the commit message for details.
11208 */
11209 if (nested_exit_intr_ack_set(vcpu) &&
11210 exit_reason == EXIT_REASON_EXTERNAL_INTERRUPT &&
11211 kvm_cpu_has_interrupt(vcpu)) {
Bandan Das77b0f5d2014-04-19 18:17:45 -040011212 int irq = kvm_cpu_get_interrupt(vcpu);
11213 WARN_ON(irq < 0);
11214 vmcs12->vm_exit_intr_info = irq |
11215 INTR_INFO_VALID_MASK | INTR_TYPE_EXT_INTR;
11216 }
11217
Jan Kiszka542060e2014-01-04 18:47:21 +010011218 trace_kvm_nested_vmexit_inject(vmcs12->vm_exit_reason,
11219 vmcs12->exit_qualification,
11220 vmcs12->idt_vectoring_info_field,
11221 vmcs12->vm_exit_intr_info,
11222 vmcs12->vm_exit_intr_error_code,
11223 KVM_ISA_VMX);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011224
Paolo Bonzini8391ce42016-07-07 14:58:33 +020011225 vm_entry_controls_reset_shadow(vmx);
11226 vm_exit_controls_reset_shadow(vmx);
Jan Kiszka36c3cc42013-02-23 22:35:37 +010011227 vmx_segment_cache_clear(vmx);
11228
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011229 /* if no vmcs02 cache requested, remove the one we used */
11230 if (VMCS02_POOL_SIZE == 0)
11231 nested_free_vmcs02(vmx, vmx->nested.current_vmptr);
11232
11233 load_vmcs12_host_state(vcpu, vmcs12);
11234
Paolo Bonzini9314006db2016-07-06 13:23:51 +020011235 /* Update any VMCS fields that might have changed while L2 ran */
Jim Mattson83bafef2016-10-04 10:48:38 -070011236 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, vmx->msr_autoload.nr);
11237 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, vmx->msr_autoload.nr);
Paolo Bonziniea26e4e2016-11-01 00:39:48 +010011238 vmcs_write64(TSC_OFFSET, vcpu->arch.tsc_offset);
Paolo Bonzini9314006db2016-07-06 13:23:51 +020011239 if (vmx->hv_deadline_tsc == -1)
11240 vmcs_clear_bits(PIN_BASED_VM_EXEC_CONTROL,
11241 PIN_BASED_VMX_PREEMPTION_TIMER);
11242 else
11243 vmcs_set_bits(PIN_BASED_VM_EXEC_CONTROL,
11244 PIN_BASED_VMX_PREEMPTION_TIMER);
Peter Feinerc95ba922016-08-17 09:36:47 -070011245 if (kvm_has_tsc_control)
11246 decache_tsc_multiplier(vmx);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011247
Radim Krčmářdccbfcf2016-08-08 20:16:23 +020011248 if (vmx->nested.change_vmcs01_virtual_x2apic_mode) {
11249 vmx->nested.change_vmcs01_virtual_x2apic_mode = false;
11250 vmx_set_virtual_x2apic_mode(vcpu,
11251 vcpu->arch.apic_base & X2APIC_ENABLE);
Jim Mattsonfb6c8192017-03-16 13:53:59 -070011252 } else if (!nested_cpu_has_ept(vmcs12) &&
11253 nested_cpu_has2(vmcs12,
11254 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) {
11255 vmx_flush_tlb_ept_only(vcpu);
Radim Krčmářdccbfcf2016-08-08 20:16:23 +020011256 }
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011257
11258 /* This is needed for same reason as it was needed in prepare_vmcs02 */
11259 vmx->host_rsp = 0;
11260
11261 /* Unpin physical memory we referred to in vmcs02 */
11262 if (vmx->nested.apic_access_page) {
David Hildenbrand53a70da2017-08-03 18:11:05 +020011263 kvm_release_page_dirty(vmx->nested.apic_access_page);
Paolo Bonzini48d89b92014-08-26 13:27:46 +020011264 vmx->nested.apic_access_page = NULL;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011265 }
Wanpeng Lia7c0b072014-08-21 19:46:50 +080011266 if (vmx->nested.virtual_apic_page) {
David Hildenbrand53a70da2017-08-03 18:11:05 +020011267 kvm_release_page_dirty(vmx->nested.virtual_apic_page);
Paolo Bonzini48d89b92014-08-26 13:27:46 +020011268 vmx->nested.virtual_apic_page = NULL;
Wanpeng Lia7c0b072014-08-21 19:46:50 +080011269 }
Wincy Van705699a2015-02-03 23:58:17 +080011270 if (vmx->nested.pi_desc_page) {
11271 kunmap(vmx->nested.pi_desc_page);
David Hildenbrand53a70da2017-08-03 18:11:05 +020011272 kvm_release_page_dirty(vmx->nested.pi_desc_page);
Wincy Van705699a2015-02-03 23:58:17 +080011273 vmx->nested.pi_desc_page = NULL;
11274 vmx->nested.pi_desc = NULL;
11275 }
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011276
11277 /*
Tang Chen38b99172014-09-24 15:57:54 +080011278 * We are now running in L2, mmu_notifier will force to reload the
11279 * page's hpa for L2 vmcs. Need to reload it for L1 before entering L1.
11280 */
Wanpeng Lic83b6d12016-09-06 17:20:33 +080011281 kvm_make_request(KVM_REQ_APIC_PAGE_RELOAD, vcpu);
Tang Chen38b99172014-09-24 15:57:54 +080011282
11283 /*
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011284 * Exiting from L2 to L1, we're now back to L1 which thinks it just
11285 * finished a VMLAUNCH or VMRESUME instruction, so we need to set the
11286 * success or failure flag accordingly.
11287 */
11288 if (unlikely(vmx->fail)) {
11289 vmx->fail = 0;
Jim Mattsoncf3215d2016-09-06 09:33:21 -070011290 nested_vmx_failValid(vcpu, vm_inst_error);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011291 } else
11292 nested_vmx_succeed(vcpu);
Abel Gordon012f83c2013-04-18 14:39:25 +030011293 if (enable_shadow_vmcs)
11294 vmx->nested.sync_shadow_vmcs = true;
Jan Kiszkab6b8a142014-03-07 20:03:12 +010011295
11296 /* in case we halted in L2 */
11297 vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011298}
11299
Nadav Har'El7c177932011-05-25 23:12:04 +030011300/*
Jan Kiszka42124922014-01-04 18:47:19 +010011301 * Forcibly leave nested mode in order to be able to reset the VCPU later on.
11302 */
11303static void vmx_leave_nested(struct kvm_vcpu *vcpu)
11304{
Wanpeng Li2f707d92017-03-06 04:03:28 -080011305 if (is_guest_mode(vcpu)) {
11306 to_vmx(vcpu)->nested.nested_run_pending = 0;
Jan Kiszka533558b2014-01-04 18:47:20 +010011307 nested_vmx_vmexit(vcpu, -1, 0, 0);
Wanpeng Li2f707d92017-03-06 04:03:28 -080011308 }
Jan Kiszka42124922014-01-04 18:47:19 +010011309 free_nested(to_vmx(vcpu));
11310}
11311
11312/*
Nadav Har'El7c177932011-05-25 23:12:04 +030011313 * L1's failure to enter L2 is a subset of a normal exit, as explained in
11314 * 23.7 "VM-entry failures during or after loading guest state" (this also
11315 * lists the acceptable exit-reason and exit-qualification parameters).
11316 * It should only be called before L2 actually succeeded to run, and when
11317 * vmcs01 is current (it doesn't leave_guest_mode() or switch vmcss).
11318 */
11319static void nested_vmx_entry_failure(struct kvm_vcpu *vcpu,
11320 struct vmcs12 *vmcs12,
11321 u32 reason, unsigned long qualification)
11322{
11323 load_vmcs12_host_state(vcpu, vmcs12);
11324 vmcs12->vm_exit_reason = reason | VMX_EXIT_REASONS_FAILED_VMENTRY;
11325 vmcs12->exit_qualification = qualification;
11326 nested_vmx_succeed(vcpu);
Abel Gordon012f83c2013-04-18 14:39:25 +030011327 if (enable_shadow_vmcs)
11328 to_vmx(vcpu)->nested.sync_shadow_vmcs = true;
Nadav Har'El7c177932011-05-25 23:12:04 +030011329}
11330
Joerg Roedel8a76d7f2011-04-04 12:39:27 +020011331static int vmx_check_intercept(struct kvm_vcpu *vcpu,
11332 struct x86_instruction_info *info,
11333 enum x86_intercept_stage stage)
11334{
11335 return X86EMUL_CONTINUE;
11336}
11337
Yunhong Jiang64672c92016-06-13 14:19:59 -070011338#ifdef CONFIG_X86_64
11339/* (a << shift) / divisor, return 1 if overflow otherwise 0 */
11340static inline int u64_shl_div_u64(u64 a, unsigned int shift,
11341 u64 divisor, u64 *result)
11342{
11343 u64 low = a << shift, high = a >> (64 - shift);
11344
11345 /* To avoid the overflow on divq */
11346 if (high >= divisor)
11347 return 1;
11348
11349 /* Low hold the result, high hold rem which is discarded */
11350 asm("divq %2\n\t" : "=a" (low), "=d" (high) :
11351 "rm" (divisor), "0" (low), "1" (high));
11352 *result = low;
11353
11354 return 0;
11355}
11356
11357static int vmx_set_hv_timer(struct kvm_vcpu *vcpu, u64 guest_deadline_tsc)
11358{
11359 struct vcpu_vmx *vmx = to_vmx(vcpu);
Paolo Bonzini9175d2e2016-06-27 15:08:01 +020011360 u64 tscl = rdtsc();
11361 u64 guest_tscl = kvm_read_l1_tsc(vcpu, tscl);
11362 u64 delta_tsc = max(guest_deadline_tsc, guest_tscl) - guest_tscl;
Yunhong Jiang64672c92016-06-13 14:19:59 -070011363
11364 /* Convert to host delta tsc if tsc scaling is enabled */
11365 if (vcpu->arch.tsc_scaling_ratio != kvm_default_tsc_scaling_ratio &&
11366 u64_shl_div_u64(delta_tsc,
11367 kvm_tsc_scaling_ratio_frac_bits,
11368 vcpu->arch.tsc_scaling_ratio,
11369 &delta_tsc))
11370 return -ERANGE;
11371
11372 /*
11373 * If the delta tsc can't fit in the 32 bit after the multi shift,
11374 * we can't use the preemption timer.
11375 * It's possible that it fits on later vmentries, but checking
11376 * on every vmentry is costly so we just use an hrtimer.
11377 */
11378 if (delta_tsc >> (cpu_preemption_timer_multi + 32))
11379 return -ERANGE;
11380
11381 vmx->hv_deadline_tsc = tscl + delta_tsc;
11382 vmcs_set_bits(PIN_BASED_VM_EXEC_CONTROL,
11383 PIN_BASED_VMX_PREEMPTION_TIMER);
Wanpeng Lic8533542017-06-29 06:28:09 -070011384
11385 return delta_tsc == 0;
Yunhong Jiang64672c92016-06-13 14:19:59 -070011386}
11387
11388static void vmx_cancel_hv_timer(struct kvm_vcpu *vcpu)
11389{
11390 struct vcpu_vmx *vmx = to_vmx(vcpu);
11391 vmx->hv_deadline_tsc = -1;
11392 vmcs_clear_bits(PIN_BASED_VM_EXEC_CONTROL,
11393 PIN_BASED_VMX_PREEMPTION_TIMER);
11394}
11395#endif
11396
Paolo Bonzini48d89b92014-08-26 13:27:46 +020011397static void vmx_sched_in(struct kvm_vcpu *vcpu, int cpu)
Radim Krčmářae97a3b2014-08-21 18:08:06 +020011398{
Radim Krčmářb4a2d312014-08-21 18:08:08 +020011399 if (ple_gap)
11400 shrink_ple_window(vcpu);
Radim Krčmářae97a3b2014-08-21 18:08:06 +020011401}
11402
Kai Huang843e4332015-01-28 10:54:28 +080011403static void vmx_slot_enable_log_dirty(struct kvm *kvm,
11404 struct kvm_memory_slot *slot)
11405{
11406 kvm_mmu_slot_leaf_clear_dirty(kvm, slot);
11407 kvm_mmu_slot_largepage_remove_write_access(kvm, slot);
11408}
11409
11410static void vmx_slot_disable_log_dirty(struct kvm *kvm,
11411 struct kvm_memory_slot *slot)
11412{
11413 kvm_mmu_slot_set_dirty(kvm, slot);
11414}
11415
11416static void vmx_flush_log_dirty(struct kvm *kvm)
11417{
11418 kvm_flush_pml_buffers(kvm);
11419}
11420
Bandan Dasc5f983f2017-05-05 15:25:14 -040011421static int vmx_write_pml_buffer(struct kvm_vcpu *vcpu)
11422{
11423 struct vmcs12 *vmcs12;
11424 struct vcpu_vmx *vmx = to_vmx(vcpu);
11425 gpa_t gpa;
11426 struct page *page = NULL;
11427 u64 *pml_address;
11428
11429 if (is_guest_mode(vcpu)) {
11430 WARN_ON_ONCE(vmx->nested.pml_full);
11431
11432 /*
11433 * Check if PML is enabled for the nested guest.
11434 * Whether eptp bit 6 is set is already checked
11435 * as part of A/D emulation.
11436 */
11437 vmcs12 = get_vmcs12(vcpu);
11438 if (!nested_cpu_has_pml(vmcs12))
11439 return 0;
11440
Dan Carpenter47698862017-05-10 22:43:17 +030011441 if (vmcs12->guest_pml_index >= PML_ENTITY_NUM) {
Bandan Dasc5f983f2017-05-05 15:25:14 -040011442 vmx->nested.pml_full = true;
11443 return 1;
11444 }
11445
11446 gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS) & ~0xFFFull;
11447
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020011448 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->pml_address);
11449 if (is_error_page(page))
Bandan Dasc5f983f2017-05-05 15:25:14 -040011450 return 0;
11451
11452 pml_address = kmap(page);
11453 pml_address[vmcs12->guest_pml_index--] = gpa;
11454 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +020011455 kvm_release_page_clean(page);
Bandan Dasc5f983f2017-05-05 15:25:14 -040011456 }
11457
11458 return 0;
11459}
11460
Kai Huang843e4332015-01-28 10:54:28 +080011461static void vmx_enable_log_dirty_pt_masked(struct kvm *kvm,
11462 struct kvm_memory_slot *memslot,
11463 gfn_t offset, unsigned long mask)
11464{
11465 kvm_mmu_clear_dirty_pt_masked(kvm, memslot, offset, mask);
11466}
11467
Feng Wuefc64402015-09-18 22:29:51 +080011468/*
Feng Wubf9f6ac2015-09-18 22:29:55 +080011469 * This routine does the following things for vCPU which is going
11470 * to be blocked if VT-d PI is enabled.
11471 * - Store the vCPU to the wakeup list, so when interrupts happen
11472 * we can find the right vCPU to wake up.
11473 * - Change the Posted-interrupt descriptor as below:
11474 * 'NDST' <-- vcpu->pre_pcpu
11475 * 'NV' <-- POSTED_INTR_WAKEUP_VECTOR
11476 * - If 'ON' is set during this process, which means at least one
11477 * interrupt is posted for this vCPU, we cannot block it, in
11478 * this case, return 1, otherwise, return 0.
11479 *
11480 */
Yunhong Jiangbc225122016-06-13 14:19:58 -070011481static int pi_pre_block(struct kvm_vcpu *vcpu)
Feng Wubf9f6ac2015-09-18 22:29:55 +080011482{
11483 unsigned long flags;
11484 unsigned int dest;
11485 struct pi_desc old, new;
11486 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
11487
11488 if (!kvm_arch_has_assigned_device(vcpu->kvm) ||
Yang Zhanga0052192016-06-13 09:56:56 +080011489 !irq_remapping_cap(IRQ_POSTING_CAP) ||
11490 !kvm_vcpu_apicv_active(vcpu))
Feng Wubf9f6ac2015-09-18 22:29:55 +080011491 return 0;
11492
11493 vcpu->pre_pcpu = vcpu->cpu;
11494 spin_lock_irqsave(&per_cpu(blocked_vcpu_on_cpu_lock,
11495 vcpu->pre_pcpu), flags);
11496 list_add_tail(&vcpu->blocked_vcpu_list,
11497 &per_cpu(blocked_vcpu_on_cpu,
11498 vcpu->pre_pcpu));
11499 spin_unlock_irqrestore(&per_cpu(blocked_vcpu_on_cpu_lock,
11500 vcpu->pre_pcpu), flags);
11501
11502 do {
11503 old.control = new.control = pi_desc->control;
11504
11505 /*
11506 * We should not block the vCPU if
11507 * an interrupt is posted for it.
11508 */
11509 if (pi_test_on(pi_desc) == 1) {
11510 spin_lock_irqsave(&per_cpu(blocked_vcpu_on_cpu_lock,
11511 vcpu->pre_pcpu), flags);
11512 list_del(&vcpu->blocked_vcpu_list);
11513 spin_unlock_irqrestore(
11514 &per_cpu(blocked_vcpu_on_cpu_lock,
11515 vcpu->pre_pcpu), flags);
11516 vcpu->pre_pcpu = -1;
11517
11518 return 1;
11519 }
11520
11521 WARN((pi_desc->sn == 1),
11522 "Warning: SN field of posted-interrupts "
11523 "is set before blocking\n");
11524
11525 /*
11526 * Since vCPU can be preempted during this process,
11527 * vcpu->cpu could be different with pre_pcpu, we
11528 * need to set pre_pcpu as the destination of wakeup
11529 * notification event, then we can find the right vCPU
11530 * to wakeup in wakeup handler if interrupts happen
11531 * when the vCPU is in blocked state.
11532 */
11533 dest = cpu_physical_id(vcpu->pre_pcpu);
11534
11535 if (x2apic_enabled())
11536 new.ndst = dest;
11537 else
11538 new.ndst = (dest << 8) & 0xFF00;
11539
11540 /* set 'NV' to 'wakeup vector' */
11541 new.nv = POSTED_INTR_WAKEUP_VECTOR;
11542 } while (cmpxchg(&pi_desc->control, old.control,
11543 new.control) != old.control);
11544
11545 return 0;
11546}
11547
Yunhong Jiangbc225122016-06-13 14:19:58 -070011548static int vmx_pre_block(struct kvm_vcpu *vcpu)
11549{
11550 if (pi_pre_block(vcpu))
11551 return 1;
11552
Yunhong Jiang64672c92016-06-13 14:19:59 -070011553 if (kvm_lapic_hv_timer_in_use(vcpu))
11554 kvm_lapic_switch_to_sw_timer(vcpu);
11555
Yunhong Jiangbc225122016-06-13 14:19:58 -070011556 return 0;
11557}
11558
11559static void pi_post_block(struct kvm_vcpu *vcpu)
Feng Wubf9f6ac2015-09-18 22:29:55 +080011560{
11561 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
11562 struct pi_desc old, new;
11563 unsigned int dest;
11564 unsigned long flags;
11565
11566 if (!kvm_arch_has_assigned_device(vcpu->kvm) ||
Yang Zhanga0052192016-06-13 09:56:56 +080011567 !irq_remapping_cap(IRQ_POSTING_CAP) ||
11568 !kvm_vcpu_apicv_active(vcpu))
Feng Wubf9f6ac2015-09-18 22:29:55 +080011569 return;
11570
11571 do {
11572 old.control = new.control = pi_desc->control;
11573
11574 dest = cpu_physical_id(vcpu->cpu);
11575
11576 if (x2apic_enabled())
11577 new.ndst = dest;
11578 else
11579 new.ndst = (dest << 8) & 0xFF00;
11580
11581 /* Allow posting non-urgent interrupts */
11582 new.sn = 0;
11583
11584 /* set 'NV' to 'notification vector' */
11585 new.nv = POSTED_INTR_VECTOR;
11586 } while (cmpxchg(&pi_desc->control, old.control,
11587 new.control) != old.control);
11588
11589 if(vcpu->pre_pcpu != -1) {
11590 spin_lock_irqsave(
11591 &per_cpu(blocked_vcpu_on_cpu_lock,
11592 vcpu->pre_pcpu), flags);
11593 list_del(&vcpu->blocked_vcpu_list);
11594 spin_unlock_irqrestore(
11595 &per_cpu(blocked_vcpu_on_cpu_lock,
11596 vcpu->pre_pcpu), flags);
11597 vcpu->pre_pcpu = -1;
11598 }
11599}
11600
Yunhong Jiangbc225122016-06-13 14:19:58 -070011601static void vmx_post_block(struct kvm_vcpu *vcpu)
11602{
Yunhong Jiang64672c92016-06-13 14:19:59 -070011603 if (kvm_x86_ops->set_hv_timer)
11604 kvm_lapic_switch_to_hv_timer(vcpu);
11605
Yunhong Jiangbc225122016-06-13 14:19:58 -070011606 pi_post_block(vcpu);
11607}
11608
Feng Wubf9f6ac2015-09-18 22:29:55 +080011609/*
Feng Wuefc64402015-09-18 22:29:51 +080011610 * vmx_update_pi_irte - set IRTE for Posted-Interrupts
11611 *
11612 * @kvm: kvm
11613 * @host_irq: host irq of the interrupt
11614 * @guest_irq: gsi of the interrupt
11615 * @set: set or unset PI
11616 * returns 0 on success, < 0 on failure
11617 */
11618static int vmx_update_pi_irte(struct kvm *kvm, unsigned int host_irq,
11619 uint32_t guest_irq, bool set)
11620{
11621 struct kvm_kernel_irq_routing_entry *e;
11622 struct kvm_irq_routing_table *irq_rt;
11623 struct kvm_lapic_irq irq;
11624 struct kvm_vcpu *vcpu;
11625 struct vcpu_data vcpu_info;
11626 int idx, ret = -EINVAL;
11627
11628 if (!kvm_arch_has_assigned_device(kvm) ||
Yang Zhanga0052192016-06-13 09:56:56 +080011629 !irq_remapping_cap(IRQ_POSTING_CAP) ||
11630 !kvm_vcpu_apicv_active(kvm->vcpus[0]))
Feng Wuefc64402015-09-18 22:29:51 +080011631 return 0;
11632
11633 idx = srcu_read_lock(&kvm->irq_srcu);
11634 irq_rt = srcu_dereference(kvm->irq_routing, &kvm->irq_srcu);
11635 BUG_ON(guest_irq >= irq_rt->nr_rt_entries);
11636
11637 hlist_for_each_entry(e, &irq_rt->map[guest_irq], link) {
11638 if (e->type != KVM_IRQ_ROUTING_MSI)
11639 continue;
11640 /*
11641 * VT-d PI cannot support posting multicast/broadcast
11642 * interrupts to a vCPU, we still use interrupt remapping
11643 * for these kind of interrupts.
11644 *
11645 * For lowest-priority interrupts, we only support
11646 * those with single CPU as the destination, e.g. user
11647 * configures the interrupts via /proc/irq or uses
11648 * irqbalance to make the interrupts single-CPU.
11649 *
11650 * We will support full lowest-priority interrupt later.
11651 */
11652
Radim Krčmář371313132016-07-12 22:09:27 +020011653 kvm_set_msi_irq(kvm, e, &irq);
Feng Wu23a1c252016-01-25 16:53:32 +080011654 if (!kvm_intr_is_single_vcpu(kvm, &irq, &vcpu)) {
11655 /*
11656 * Make sure the IRTE is in remapped mode if
11657 * we don't handle it in posted mode.
11658 */
11659 ret = irq_set_vcpu_affinity(host_irq, NULL);
11660 if (ret < 0) {
11661 printk(KERN_INFO
11662 "failed to back to remapped mode, irq: %u\n",
11663 host_irq);
11664 goto out;
11665 }
11666
Feng Wuefc64402015-09-18 22:29:51 +080011667 continue;
Feng Wu23a1c252016-01-25 16:53:32 +080011668 }
Feng Wuefc64402015-09-18 22:29:51 +080011669
11670 vcpu_info.pi_desc_addr = __pa(vcpu_to_pi_desc(vcpu));
11671 vcpu_info.vector = irq.vector;
11672
Feng Wub6ce9782016-01-25 16:53:35 +080011673 trace_kvm_pi_irte_update(vcpu->vcpu_id, host_irq, e->gsi,
Feng Wuefc64402015-09-18 22:29:51 +080011674 vcpu_info.vector, vcpu_info.pi_desc_addr, set);
11675
11676 if (set)
11677 ret = irq_set_vcpu_affinity(host_irq, &vcpu_info);
11678 else {
11679 /* suppress notification event before unposting */
11680 pi_set_sn(vcpu_to_pi_desc(vcpu));
11681 ret = irq_set_vcpu_affinity(host_irq, NULL);
11682 pi_clear_sn(vcpu_to_pi_desc(vcpu));
11683 }
11684
11685 if (ret < 0) {
11686 printk(KERN_INFO "%s: failed to update PI IRTE\n",
11687 __func__);
11688 goto out;
11689 }
11690 }
11691
11692 ret = 0;
11693out:
11694 srcu_read_unlock(&kvm->irq_srcu, idx);
11695 return ret;
11696}
11697
Ashok Rajc45dcc72016-06-22 14:59:56 +080011698static void vmx_setup_mce(struct kvm_vcpu *vcpu)
11699{
11700 if (vcpu->arch.mcg_cap & MCG_LMCE_P)
11701 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits |=
11702 FEATURE_CONTROL_LMCE;
11703 else
11704 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits &=
11705 ~FEATURE_CONTROL_LMCE;
11706}
11707
Kees Cook404f6aa2016-08-08 16:29:06 -070011708static struct kvm_x86_ops vmx_x86_ops __ro_after_init = {
Avi Kivity6aa8b732006-12-10 02:21:36 -080011709 .cpu_has_kvm_support = cpu_has_kvm_support,
11710 .disabled_by_bios = vmx_disabled_by_bios,
11711 .hardware_setup = hardware_setup,
11712 .hardware_unsetup = hardware_unsetup,
Yang, Sheng002c7f72007-07-31 14:23:01 +030011713 .check_processor_compatibility = vmx_check_processor_compat,
Avi Kivity6aa8b732006-12-10 02:21:36 -080011714 .hardware_enable = hardware_enable,
11715 .hardware_disable = hardware_disable,
Sheng Yang04547152009-04-01 15:52:31 +080011716 .cpu_has_accelerated_tpr = report_flexpriority,
Paolo Bonzini6d396b52015-04-01 14:25:33 +020011717 .cpu_has_high_real_mode_segbase = vmx_has_high_real_mode_segbase,
Avi Kivity6aa8b732006-12-10 02:21:36 -080011718
11719 .vcpu_create = vmx_create_vcpu,
11720 .vcpu_free = vmx_free_vcpu,
Avi Kivity04d2cc72007-09-10 18:10:54 +030011721 .vcpu_reset = vmx_vcpu_reset,
Avi Kivity6aa8b732006-12-10 02:21:36 -080011722
Avi Kivity04d2cc72007-09-10 18:10:54 +030011723 .prepare_guest_switch = vmx_save_host_state,
Avi Kivity6aa8b732006-12-10 02:21:36 -080011724 .vcpu_load = vmx_vcpu_load,
11725 .vcpu_put = vmx_vcpu_put,
11726
Paolo Bonzinia96036b2015-11-10 11:55:36 +010011727 .update_bp_intercept = update_exception_bitmap,
Avi Kivity6aa8b732006-12-10 02:21:36 -080011728 .get_msr = vmx_get_msr,
11729 .set_msr = vmx_set_msr,
11730 .get_segment_base = vmx_get_segment_base,
11731 .get_segment = vmx_get_segment,
11732 .set_segment = vmx_set_segment,
Izik Eidus2e4d2652008-03-24 19:38:34 +020011733 .get_cpl = vmx_get_cpl,
Avi Kivity6aa8b732006-12-10 02:21:36 -080011734 .get_cs_db_l_bits = vmx_get_cs_db_l_bits,
Avi Kivitye8467fd2009-12-29 18:43:06 +020011735 .decache_cr0_guest_bits = vmx_decache_cr0_guest_bits,
Avi Kivityaff48ba2010-12-05 18:56:11 +020011736 .decache_cr3 = vmx_decache_cr3,
Anthony Liguori25c4c272007-04-27 09:29:21 +030011737 .decache_cr4_guest_bits = vmx_decache_cr4_guest_bits,
Avi Kivity6aa8b732006-12-10 02:21:36 -080011738 .set_cr0 = vmx_set_cr0,
Avi Kivity6aa8b732006-12-10 02:21:36 -080011739 .set_cr3 = vmx_set_cr3,
11740 .set_cr4 = vmx_set_cr4,
Avi Kivity6aa8b732006-12-10 02:21:36 -080011741 .set_efer = vmx_set_efer,
Avi Kivity6aa8b732006-12-10 02:21:36 -080011742 .get_idt = vmx_get_idt,
11743 .set_idt = vmx_set_idt,
11744 .get_gdt = vmx_get_gdt,
11745 .set_gdt = vmx_set_gdt,
Jan Kiszka73aaf249e2014-01-04 18:47:16 +010011746 .get_dr6 = vmx_get_dr6,
11747 .set_dr6 = vmx_set_dr6,
Gleb Natapov020df072010-04-13 10:05:23 +030011748 .set_dr7 = vmx_set_dr7,
Paolo Bonzini81908bf2014-02-21 10:32:27 +010011749 .sync_dirty_debug_regs = vmx_sync_dirty_debug_regs,
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -030011750 .cache_reg = vmx_cache_reg,
Avi Kivity6aa8b732006-12-10 02:21:36 -080011751 .get_rflags = vmx_get_rflags,
11752 .set_rflags = vmx_set_rflags,
Huaitong Hanbe94f6b2016-03-22 16:51:20 +080011753
11754 .get_pkru = vmx_get_pkru,
11755
Avi Kivity6aa8b732006-12-10 02:21:36 -080011756 .tlb_flush = vmx_flush_tlb,
Avi Kivity6aa8b732006-12-10 02:21:36 -080011757
Avi Kivity6aa8b732006-12-10 02:21:36 -080011758 .run = vmx_vcpu_run,
Avi Kivity6062d012009-03-23 17:35:17 +020011759 .handle_exit = vmx_handle_exit,
Avi Kivity6aa8b732006-12-10 02:21:36 -080011760 .skip_emulated_instruction = skip_emulated_instruction,
Glauber Costa2809f5d2009-05-12 16:21:05 -040011761 .set_interrupt_shadow = vmx_set_interrupt_shadow,
11762 .get_interrupt_shadow = vmx_get_interrupt_shadow,
Ingo Molnar102d8322007-02-19 14:37:47 +020011763 .patch_hypercall = vmx_patch_hypercall,
Eddie Dong2a8067f2007-08-06 16:29:07 +030011764 .set_irq = vmx_inject_irq,
Gleb Natapov95ba8273132009-04-21 17:45:08 +030011765 .set_nmi = vmx_inject_nmi,
Avi Kivity298101d2007-11-25 13:41:11 +020011766 .queue_exception = vmx_queue_exception,
Avi Kivityb463a6f2010-07-20 15:06:17 +030011767 .cancel_injection = vmx_cancel_injection,
Gleb Natapov78646122009-03-23 12:12:11 +020011768 .interrupt_allowed = vmx_interrupt_allowed,
Gleb Natapov95ba8273132009-04-21 17:45:08 +030011769 .nmi_allowed = vmx_nmi_allowed,
Jan Kiszka3cfc3092009-11-12 01:04:25 +010011770 .get_nmi_mask = vmx_get_nmi_mask,
11771 .set_nmi_mask = vmx_set_nmi_mask,
Gleb Natapov95ba8273132009-04-21 17:45:08 +030011772 .enable_nmi_window = enable_nmi_window,
11773 .enable_irq_window = enable_irq_window,
11774 .update_cr8_intercept = update_cr8_intercept,
Yang Zhang8d146952013-01-25 10:18:50 +080011775 .set_virtual_x2apic_mode = vmx_set_virtual_x2apic_mode,
Tang Chen38b99172014-09-24 15:57:54 +080011776 .set_apic_access_page_addr = vmx_set_apic_access_page_addr,
Andrey Smetanind62caab2015-11-10 15:36:33 +030011777 .get_enable_apicv = vmx_get_enable_apicv,
11778 .refresh_apicv_exec_ctrl = vmx_refresh_apicv_exec_ctrl,
Yang Zhangc7c9c562013-01-25 10:18:51 +080011779 .load_eoi_exitmap = vmx_load_eoi_exitmap,
Paolo Bonzini967235d2016-12-19 14:03:45 +010011780 .apicv_post_state_restore = vmx_apicv_post_state_restore,
Yang Zhangc7c9c562013-01-25 10:18:51 +080011781 .hwapic_irr_update = vmx_hwapic_irr_update,
11782 .hwapic_isr_update = vmx_hwapic_isr_update,
Yang Zhanga20ed542013-04-11 19:25:15 +080011783 .sync_pir_to_irr = vmx_sync_pir_to_irr,
11784 .deliver_posted_interrupt = vmx_deliver_posted_interrupt,
Gleb Natapov95ba8273132009-04-21 17:45:08 +030011785
Izik Eiduscbc94022007-10-25 00:29:55 +020011786 .set_tss_addr = vmx_set_tss_addr,
Sheng Yang67253af2008-04-25 10:20:22 +080011787 .get_tdp_level = get_ept_level,
Sheng Yang4b12f0d2009-04-27 20:35:42 +080011788 .get_mt_mask = vmx_get_mt_mask,
Marcelo Tosatti229456f2009-06-17 09:22:14 -030011789
Avi Kivity586f9602010-11-18 13:09:54 +020011790 .get_exit_info = vmx_get_exit_info,
Avi Kivity586f9602010-11-18 13:09:54 +020011791
Sheng Yang17cc3932010-01-05 19:02:27 +080011792 .get_lpage_level = vmx_get_lpage_level,
Sheng Yang0e851882009-12-18 16:48:46 +080011793
11794 .cpuid_update = vmx_cpuid_update,
Sheng Yang4e47c7a2009-12-18 16:48:47 +080011795
11796 .rdtscp_supported = vmx_rdtscp_supported,
Mao, Junjiead756a12012-07-02 01:18:48 +000011797 .invpcid_supported = vmx_invpcid_supported,
Joerg Roedeld4330ef2010-04-22 12:33:11 +020011798
11799 .set_supported_cpuid = vmx_set_supported_cpuid,
Sheng Yangf5f48ee2010-06-30 12:25:15 +080011800
11801 .has_wbinvd_exit = cpu_has_vmx_wbinvd_exit,
Zachary Amsden99e3e302010-08-19 22:07:17 -100011802
11803 .write_tsc_offset = vmx_write_tsc_offset,
Joerg Roedel1c97f0a2010-09-10 17:30:41 +020011804
11805 .set_tdp_cr3 = vmx_set_cr3,
Joerg Roedel8a76d7f2011-04-04 12:39:27 +020011806
11807 .check_intercept = vmx_check_intercept,
Yang Zhanga547c6d2013-04-11 19:25:10 +080011808 .handle_external_intr = vmx_handle_external_intr,
Liu, Jinsongda8999d2014-02-24 10:55:46 +000011809 .mpx_supported = vmx_mpx_supported,
Wanpeng Li55412b22014-12-02 19:21:30 +080011810 .xsaves_supported = vmx_xsaves_supported,
Jan Kiszkab6b8a142014-03-07 20:03:12 +010011811
11812 .check_nested_events = vmx_check_nested_events,
Radim Krčmářae97a3b2014-08-21 18:08:06 +020011813
11814 .sched_in = vmx_sched_in,
Kai Huang843e4332015-01-28 10:54:28 +080011815
11816 .slot_enable_log_dirty = vmx_slot_enable_log_dirty,
11817 .slot_disable_log_dirty = vmx_slot_disable_log_dirty,
11818 .flush_log_dirty = vmx_flush_log_dirty,
11819 .enable_log_dirty_pt_masked = vmx_enable_log_dirty_pt_masked,
Bandan Dasc5f983f2017-05-05 15:25:14 -040011820 .write_log_dirty = vmx_write_pml_buffer,
Wei Huang25462f72015-06-19 15:45:05 +020011821
Feng Wubf9f6ac2015-09-18 22:29:55 +080011822 .pre_block = vmx_pre_block,
11823 .post_block = vmx_post_block,
11824
Wei Huang25462f72015-06-19 15:45:05 +020011825 .pmu_ops = &intel_pmu_ops,
Feng Wuefc64402015-09-18 22:29:51 +080011826
11827 .update_pi_irte = vmx_update_pi_irte,
Yunhong Jiang64672c92016-06-13 14:19:59 -070011828
11829#ifdef CONFIG_X86_64
11830 .set_hv_timer = vmx_set_hv_timer,
11831 .cancel_hv_timer = vmx_cancel_hv_timer,
11832#endif
Ashok Rajc45dcc72016-06-22 14:59:56 +080011833
11834 .setup_mce = vmx_setup_mce,
Avi Kivity6aa8b732006-12-10 02:21:36 -080011835};
11836
11837static int __init vmx_init(void)
11838{
Tiejun Chen34a1cd62014-10-28 10:14:48 +080011839 int r = kvm_init(&vmx_x86_ops, sizeof(struct vcpu_vmx),
11840 __alignof__(struct vcpu_vmx), THIS_MODULE);
He, Qingfdef3ad2007-04-30 09:45:24 +030011841 if (r)
Tiejun Chen34a1cd62014-10-28 10:14:48 +080011842 return r;
Sheng Yang25c5f222008-03-28 13:18:56 +080011843
Dave Young2965faa2015-09-09 15:38:55 -070011844#ifdef CONFIG_KEXEC_CORE
Zhang Yanfei8f536b72012-12-06 23:43:34 +080011845 rcu_assign_pointer(crash_vmclear_loaded_vmcss,
11846 crash_vmclear_local_loaded_vmcss);
11847#endif
11848
He, Qingfdef3ad2007-04-30 09:45:24 +030011849 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -080011850}
11851
11852static void __exit vmx_exit(void)
11853{
Dave Young2965faa2015-09-09 15:38:55 -070011854#ifdef CONFIG_KEXEC_CORE
Monam Agarwal3b63a432014-03-22 12:28:10 +053011855 RCU_INIT_POINTER(crash_vmclear_loaded_vmcss, NULL);
Zhang Yanfei8f536b72012-12-06 23:43:34 +080011856 synchronize_rcu();
11857#endif
11858
Zhang Xiantaocb498ea2007-11-14 20:39:31 +080011859 kvm_exit();
Avi Kivity6aa8b732006-12-10 02:21:36 -080011860}
11861
11862module_init(vmx_init)
11863module_exit(vmx_exit)