blob: 65747d1194acf361cf9ee68b138bc2465cf9da50 [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>
Dan Williams085331d2018-01-31 17:47:03 -080037#include <linux/nospec.h>
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -030038#include "kvm_cache_regs.h"
Avi Kivity35920a32008-07-03 14:50:12 +030039#include "x86.h"
Avi Kivitye4956062007-06-28 14:15:57 -040040
Feng Wu28b835d2015-09-18 22:29:54 +080041#include <asm/cpu.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080042#include <asm/io.h>
Anthony Liguori3b3be0d2006-12-13 00:33:43 -080043#include <asm/desc.h>
Eduardo Habkost13673a92008-11-17 19:03:13 -020044#include <asm/vmx.h>
Eduardo Habkost6210e372008-11-17 19:03:16 -020045#include <asm/virtext.h>
Andi Kleena0861c02009-06-08 17:37:09 +080046#include <asm/mce.h>
Ingo Molnar952f07e2015-04-26 16:56:05 +020047#include <asm/fpu/internal.h>
Gleb Natapovd7cd9792011-10-05 14:01:23 +020048#include <asm/perf_event.h>
Paolo Bonzini81908bf2014-02-21 10:32:27 +010049#include <asm/debugreg.h>
Zhang Yanfei8f536b72012-12-06 23:43:34 +080050#include <asm/kexec.h>
Radim Krčmářdab20872015-02-09 22:44:07 +010051#include <asm/apic.h>
Feng Wuefc64402015-09-18 22:29:51 +080052#include <asm/irq_remapping.h>
Andy Lutomirskid6e41f12017-05-28 10:00:17 -070053#include <asm/mmu_context.h>
Paolo Bonziniecb586b2018-02-22 16:43:17 +010054#include <asm/microcode.h>
David Woodhouse117cc7a2018-01-12 11:11:27 +000055#include <asm/nospec-branch.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080056
Marcelo Tosatti229456f2009-06-17 09:22:14 -030057#include "trace.h"
Wei Huang25462f72015-06-19 15:45:05 +020058#include "pmu.h"
Marcelo Tosatti229456f2009-06-17 09:22:14 -030059
Avi Kivity4ecac3f2008-05-13 13:23:38 +030060#define __ex(x) __kvm_handle_fault_on_reboot(x)
Avi Kivity5e520e62011-05-15 10:13:12 -040061#define __ex_clear(x, reg) \
62 ____kvm_handle_fault_on_reboot(x, "xor " reg " , " reg)
Avi Kivity4ecac3f2008-05-13 13:23:38 +030063
Avi Kivity6aa8b732006-12-10 02:21:36 -080064MODULE_AUTHOR("Qumranet");
65MODULE_LICENSE("GPL");
66
Josh Triplette9bda3b2012-03-20 23:33:51 -070067static const struct x86_cpu_id vmx_cpu_id[] = {
68 X86_FEATURE_MATCH(X86_FEATURE_VMX),
69 {}
70};
71MODULE_DEVICE_TABLE(x86cpu, vmx_cpu_id);
72
Rusty Russell476bc002012-01-13 09:32:18 +103073static bool __read_mostly enable_vpid = 1;
Avi Kivity736caef2009-03-23 17:39:48 +020074module_param_named(vpid, enable_vpid, bool, 0444);
Sheng Yang2384d2b2008-01-17 15:14:33 +080075
Paolo Bonzinid02fcf52017-11-06 13:31:13 +010076static bool __read_mostly enable_vnmi = 1;
77module_param_named(vnmi, enable_vnmi, bool, S_IRUGO);
78
Rusty Russell476bc002012-01-13 09:32:18 +103079static bool __read_mostly flexpriority_enabled = 1;
Avi Kivity736caef2009-03-23 17:39:48 +020080module_param_named(flexpriority, flexpriority_enabled, bool, S_IRUGO);
Avi Kivity4c9fc8e2008-03-24 18:15:14 +020081
Rusty Russell476bc002012-01-13 09:32:18 +103082static bool __read_mostly enable_ept = 1;
Avi Kivity736caef2009-03-23 17:39:48 +020083module_param_named(ept, enable_ept, bool, S_IRUGO);
Sheng Yangd56f5462008-04-25 10:13:16 +080084
Rusty Russell476bc002012-01-13 09:32:18 +103085static bool __read_mostly enable_unrestricted_guest = 1;
Nitin A Kamble3a624e22009-06-08 11:34:16 -070086module_param_named(unrestricted_guest,
87 enable_unrestricted_guest, bool, S_IRUGO);
88
Xudong Hao83c3a332012-05-28 19:33:35 +080089static bool __read_mostly enable_ept_ad_bits = 1;
90module_param_named(eptad, enable_ept_ad_bits, bool, S_IRUGO);
91
Avi Kivitya27685c2012-06-12 20:30:18 +030092static bool __read_mostly emulate_invalid_guest_state = true;
Avi Kivityc1f8bc02009-03-23 15:41:17 +020093module_param(emulate_invalid_guest_state, bool, S_IRUGO);
Mohammed Gamal04fa4d32008-08-17 16:39:48 +030094
Rusty Russell476bc002012-01-13 09:32:18 +103095static bool __read_mostly fasteoi = 1;
Kevin Tian58fbbf22011-08-30 13:56:17 +030096module_param(fasteoi, bool, S_IRUGO);
97
Yang Zhang5a717852013-04-11 19:25:16 +080098static bool __read_mostly enable_apicv = 1;
Yang Zhang01e439b2013-04-11 19:25:12 +080099module_param(enable_apicv, bool, S_IRUGO);
Yang Zhang83d4c282013-01-25 10:18:49 +0800100
Abel Gordonabc4fc52013-04-18 14:35:25 +0300101static bool __read_mostly enable_shadow_vmcs = 1;
102module_param_named(enable_shadow_vmcs, enable_shadow_vmcs, bool, S_IRUGO);
Nadav Har'El801d3422011-05-25 23:02:23 +0300103/*
104 * If nested=1, nested virtualization is supported, i.e., guests may use
105 * VMX and be a hypervisor for its own guests. If nested=0, guests may not
106 * use VMX instructions.
107 */
Rusty Russell476bc002012-01-13 09:32:18 +1030108static bool __read_mostly nested = 0;
Nadav Har'El801d3422011-05-25 23:02:23 +0300109module_param(nested, bool, S_IRUGO);
110
Wanpeng Li20300092014-12-02 19:14:59 +0800111static u64 __read_mostly host_xss;
112
Kai Huang843e4332015-01-28 10:54:28 +0800113static bool __read_mostly enable_pml = 1;
114module_param_named(pml, enable_pml, bool, S_IRUGO);
115
Paolo Bonzini904e14f2018-01-16 16:51:18 +0100116#define MSR_TYPE_R 1
117#define MSR_TYPE_W 2
118#define MSR_TYPE_RW 3
119
120#define MSR_BITMAP_MODE_X2APIC 1
121#define MSR_BITMAP_MODE_X2APIC_APICV 2
122#define MSR_BITMAP_MODE_LM 4
123
Haozhong Zhang64903d62015-10-20 15:39:09 +0800124#define KVM_VMX_TSC_MULTIPLIER_MAX 0xffffffffffffffffULL
125
Yunhong Jiang64672c92016-06-13 14:19:59 -0700126/* Guest_tsc -> host_tsc conversion requires 64-bit division. */
127static int __read_mostly cpu_preemption_timer_multi;
128static bool __read_mostly enable_preemption_timer = 1;
129#ifdef CONFIG_X86_64
130module_param_named(preemption_timer, enable_preemption_timer, bool, S_IRUGO);
131#endif
132
Gleb Natapov50378782013-02-04 16:00:28 +0200133#define KVM_GUEST_CR0_MASK (X86_CR0_NW | X86_CR0_CD)
Sean Christopherson1706bd02018-03-05 12:04:38 -0800134#define KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST X86_CR0_NE
135#define KVM_VM_CR0_ALWAYS_ON \
136 (KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST | \
137 X86_CR0_WP | X86_CR0_PG | X86_CR0_PE)
Avi Kivity4c386092009-12-07 12:26:18 +0200138#define KVM_CR4_GUEST_OWNED_BITS \
139 (X86_CR4_PVI | X86_CR4_DE | X86_CR4_PCE | X86_CR4_OSFXSR \
Yu Zhangfd8cb432017-08-24 20:27:56 +0800140 | X86_CR4_OSXMMEXCPT | X86_CR4_LA57 | X86_CR4_TSD)
Avi Kivity4c386092009-12-07 12:26:18 +0200141
Sean Christopherson5dc1f042018-03-05 12:04:39 -0800142#define KVM_VM_CR4_ALWAYS_ON_UNRESTRICTED_GUEST X86_CR4_VMXE
Avi Kivitycdc0e242009-12-06 17:21:14 +0200143#define KVM_PMODE_VM_CR4_ALWAYS_ON (X86_CR4_PAE | X86_CR4_VMXE)
144#define KVM_RMODE_VM_CR4_ALWAYS_ON (X86_CR4_VME | X86_CR4_PAE | X86_CR4_VMXE)
145
Avi Kivity78ac8b42010-04-08 18:19:35 +0300146#define RMODE_GUEST_OWNED_EFLAGS_BITS (~(X86_EFLAGS_IOPL | X86_EFLAGS_VM))
147
Jan Kiszkaf41245002014-03-07 20:03:13 +0100148#define VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE 5
149
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +0800150/*
Jan Dakinevich16c2aec2016-10-28 07:00:30 +0300151 * Hyper-V requires all of these, so mark them as supported even though
152 * they are just treated the same as all-context.
153 */
154#define VMX_VPID_EXTENT_SUPPORTED_MASK \
155 (VMX_VPID_EXTENT_INDIVIDUAL_ADDR_BIT | \
156 VMX_VPID_EXTENT_SINGLE_CONTEXT_BIT | \
157 VMX_VPID_EXTENT_GLOBAL_CONTEXT_BIT | \
158 VMX_VPID_EXTENT_SINGLE_NON_GLOBAL_BIT)
159
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +0800160/*
161 * These 2 parameters are used to config the controls for Pause-Loop Exiting:
162 * ple_gap: upper bound on the amount of time between two successive
163 * executions of PAUSE in a loop. Also indicate if ple enabled.
Rik van Riel00c25bc2011-01-04 09:51:33 -0500164 * According to test, this time is usually smaller than 128 cycles.
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +0800165 * ple_window: upper bound on the amount of time a guest is allowed to execute
166 * in a PAUSE loop. Tests indicate that most spinlocks are held for
167 * less than 2^12 cycles
168 * Time is measured based on a counter that runs at the same rate as the TSC,
169 * refer SDM volume 3b section 21.6.13 & 22.1.3.
170 */
Radim Krčmářb4a2d312014-08-21 18:08:08 +0200171#define KVM_VMX_DEFAULT_PLE_GAP 128
172#define KVM_VMX_DEFAULT_PLE_WINDOW 4096
173#define KVM_VMX_DEFAULT_PLE_WINDOW_GROW 2
174#define KVM_VMX_DEFAULT_PLE_WINDOW_SHRINK 0
175#define KVM_VMX_DEFAULT_PLE_WINDOW_MAX \
176 INT_MAX / KVM_VMX_DEFAULT_PLE_WINDOW_GROW
177
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +0800178static int ple_gap = KVM_VMX_DEFAULT_PLE_GAP;
179module_param(ple_gap, int, S_IRUGO);
180
181static int ple_window = KVM_VMX_DEFAULT_PLE_WINDOW;
182module_param(ple_window, int, S_IRUGO);
183
Radim Krčmářb4a2d312014-08-21 18:08:08 +0200184/* Default doubles per-vcpu window every exit. */
185static int ple_window_grow = KVM_VMX_DEFAULT_PLE_WINDOW_GROW;
186module_param(ple_window_grow, int, S_IRUGO);
187
188/* Default resets per-vcpu window every exit to ple_window. */
189static int ple_window_shrink = KVM_VMX_DEFAULT_PLE_WINDOW_SHRINK;
190module_param(ple_window_shrink, int, S_IRUGO);
191
192/* Default is to compute the maximum so we can never overflow. */
193static int ple_window_actual_max = KVM_VMX_DEFAULT_PLE_WINDOW_MAX;
194static int ple_window_max = KVM_VMX_DEFAULT_PLE_WINDOW_MAX;
195module_param(ple_window_max, int, S_IRUGO);
196
Avi Kivity83287ea422012-09-16 15:10:57 +0300197extern const ulong vmx_return;
198
Gleb Natapov8bf00a52011-10-05 14:01:22 +0200199#define NR_AUTOLOAD_MSRS 8
Avi Kivity61d2ef22010-04-28 16:40:38 +0300200
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400201struct vmcs {
202 u32 revision_id;
203 u32 abort;
204 char data[0];
205};
206
Nadav Har'Eld462b812011-05-24 15:26:10 +0300207/*
208 * Track a VMCS that may be loaded on a certain CPU. If it is (cpu!=-1), also
209 * remember whether it was VMLAUNCHed, and maintain a linked list of all VMCSs
210 * loaded on this CPU (so we can clear them if the CPU goes down).
211 */
212struct loaded_vmcs {
213 struct vmcs *vmcs;
Jim Mattson355f4fb2016-10-28 08:29:39 -0700214 struct vmcs *shadow_vmcs;
Nadav Har'Eld462b812011-05-24 15:26:10 +0300215 int cpu;
Paolo Bonzini4c4a6f72017-07-14 13:36:11 +0200216 bool launched;
217 bool nmi_known_unmasked;
Ladi Prosek44889942017-09-22 07:53:15 +0200218 unsigned long vmcs_host_cr3; /* May not match real cr3 */
219 unsigned long vmcs_host_cr4; /* May not match real cr4 */
Paolo Bonzini8a1b4392017-11-06 13:31:12 +0100220 /* Support for vnmi-less CPUs */
221 int soft_vnmi_blocked;
222 ktime_t entry_time;
223 s64 vnmi_blocked_time;
Paolo Bonzini904e14f2018-01-16 16:51:18 +0100224 unsigned long *msr_bitmap;
Nadav Har'Eld462b812011-05-24 15:26:10 +0300225 struct list_head loaded_vmcss_on_cpu_link;
226};
227
Avi Kivity26bb0982009-09-07 11:14:12 +0300228struct shared_msr_entry {
229 unsigned index;
230 u64 data;
Avi Kivityd5696722009-12-02 12:28:47 +0200231 u64 mask;
Avi Kivity26bb0982009-09-07 11:14:12 +0300232};
233
Nadav Har'Elec378ae2011-05-25 23:02:54 +0300234/*
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300235 * struct vmcs12 describes the state that our guest hypervisor (L1) keeps for a
236 * single nested guest (L2), hence the name vmcs12. Any VMX implementation has
237 * a VMCS structure, and vmcs12 is our emulated VMX's VMCS. This structure is
238 * stored in guest memory specified by VMPTRLD, but is opaque to the guest,
239 * which must access it using VMREAD/VMWRITE/VMCLEAR instructions.
240 * More than one of these structures may exist, if L1 runs multiple L2 guests.
Jim Mattsonde3a0022017-11-27 17:22:25 -0600241 * nested_vmx_run() will use the data here to build the vmcs02: a VMCS for the
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300242 * underlying hardware which will be used to run L2.
243 * This structure is packed to ensure that its layout is identical across
244 * machines (necessary for live migration).
245 * If there are changes in this struct, VMCS12_REVISION must be changed.
246 */
Nadav Har'El22bd0352011-05-25 23:05:57 +0300247typedef u64 natural_width;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300248struct __packed vmcs12 {
249 /* According to the Intel spec, a VMCS region must start with the
250 * following two fields. Then follow implementation-specific data.
251 */
252 u32 revision_id;
253 u32 abort;
Nadav Har'El22bd0352011-05-25 23:05:57 +0300254
Nadav Har'El27d6c862011-05-25 23:06:59 +0300255 u32 launch_state; /* set to 0 by VMCLEAR, to 1 by VMLAUNCH */
256 u32 padding[7]; /* room for future expansion */
257
Nadav Har'El22bd0352011-05-25 23:05:57 +0300258 u64 io_bitmap_a;
259 u64 io_bitmap_b;
260 u64 msr_bitmap;
261 u64 vm_exit_msr_store_addr;
262 u64 vm_exit_msr_load_addr;
263 u64 vm_entry_msr_load_addr;
264 u64 tsc_offset;
265 u64 virtual_apic_page_addr;
266 u64 apic_access_addr;
Wincy Van705699a2015-02-03 23:58:17 +0800267 u64 posted_intr_desc_addr;
Bandan Das27c42a12017-08-03 15:54:42 -0400268 u64 vm_function_control;
Nadav Har'El22bd0352011-05-25 23:05:57 +0300269 u64 ept_pointer;
Wincy Van608406e2015-02-03 23:57:51 +0800270 u64 eoi_exit_bitmap0;
271 u64 eoi_exit_bitmap1;
272 u64 eoi_exit_bitmap2;
273 u64 eoi_exit_bitmap3;
Bandan Das41ab9372017-08-03 15:54:43 -0400274 u64 eptp_list_address;
Wanpeng Li81dc01f2014-12-04 19:11:07 +0800275 u64 xss_exit_bitmap;
Nadav Har'El22bd0352011-05-25 23:05:57 +0300276 u64 guest_physical_address;
277 u64 vmcs_link_pointer;
Bandan Dasc5f983f2017-05-05 15:25:14 -0400278 u64 pml_address;
Nadav Har'El22bd0352011-05-25 23:05:57 +0300279 u64 guest_ia32_debugctl;
280 u64 guest_ia32_pat;
281 u64 guest_ia32_efer;
282 u64 guest_ia32_perf_global_ctrl;
283 u64 guest_pdptr0;
284 u64 guest_pdptr1;
285 u64 guest_pdptr2;
286 u64 guest_pdptr3;
Paolo Bonzini36be0b92014-02-24 12:30:04 +0100287 u64 guest_bndcfgs;
Nadav Har'El22bd0352011-05-25 23:05:57 +0300288 u64 host_ia32_pat;
289 u64 host_ia32_efer;
290 u64 host_ia32_perf_global_ctrl;
291 u64 padding64[8]; /* room for future expansion */
292 /*
293 * To allow migration of L1 (complete with its L2 guests) between
294 * machines of different natural widths (32 or 64 bit), we cannot have
295 * unsigned long fields with no explict size. We use u64 (aliased
296 * natural_width) instead. Luckily, x86 is little-endian.
297 */
298 natural_width cr0_guest_host_mask;
299 natural_width cr4_guest_host_mask;
300 natural_width cr0_read_shadow;
301 natural_width cr4_read_shadow;
302 natural_width cr3_target_value0;
303 natural_width cr3_target_value1;
304 natural_width cr3_target_value2;
305 natural_width cr3_target_value3;
306 natural_width exit_qualification;
307 natural_width guest_linear_address;
308 natural_width guest_cr0;
309 natural_width guest_cr3;
310 natural_width guest_cr4;
311 natural_width guest_es_base;
312 natural_width guest_cs_base;
313 natural_width guest_ss_base;
314 natural_width guest_ds_base;
315 natural_width guest_fs_base;
316 natural_width guest_gs_base;
317 natural_width guest_ldtr_base;
318 natural_width guest_tr_base;
319 natural_width guest_gdtr_base;
320 natural_width guest_idtr_base;
321 natural_width guest_dr7;
322 natural_width guest_rsp;
323 natural_width guest_rip;
324 natural_width guest_rflags;
325 natural_width guest_pending_dbg_exceptions;
326 natural_width guest_sysenter_esp;
327 natural_width guest_sysenter_eip;
328 natural_width host_cr0;
329 natural_width host_cr3;
330 natural_width host_cr4;
331 natural_width host_fs_base;
332 natural_width host_gs_base;
333 natural_width host_tr_base;
334 natural_width host_gdtr_base;
335 natural_width host_idtr_base;
336 natural_width host_ia32_sysenter_esp;
337 natural_width host_ia32_sysenter_eip;
338 natural_width host_rsp;
339 natural_width host_rip;
340 natural_width paddingl[8]; /* room for future expansion */
341 u32 pin_based_vm_exec_control;
342 u32 cpu_based_vm_exec_control;
343 u32 exception_bitmap;
344 u32 page_fault_error_code_mask;
345 u32 page_fault_error_code_match;
346 u32 cr3_target_count;
347 u32 vm_exit_controls;
348 u32 vm_exit_msr_store_count;
349 u32 vm_exit_msr_load_count;
350 u32 vm_entry_controls;
351 u32 vm_entry_msr_load_count;
352 u32 vm_entry_intr_info_field;
353 u32 vm_entry_exception_error_code;
354 u32 vm_entry_instruction_len;
355 u32 tpr_threshold;
356 u32 secondary_vm_exec_control;
357 u32 vm_instruction_error;
358 u32 vm_exit_reason;
359 u32 vm_exit_intr_info;
360 u32 vm_exit_intr_error_code;
361 u32 idt_vectoring_info_field;
362 u32 idt_vectoring_error_code;
363 u32 vm_exit_instruction_len;
364 u32 vmx_instruction_info;
365 u32 guest_es_limit;
366 u32 guest_cs_limit;
367 u32 guest_ss_limit;
368 u32 guest_ds_limit;
369 u32 guest_fs_limit;
370 u32 guest_gs_limit;
371 u32 guest_ldtr_limit;
372 u32 guest_tr_limit;
373 u32 guest_gdtr_limit;
374 u32 guest_idtr_limit;
375 u32 guest_es_ar_bytes;
376 u32 guest_cs_ar_bytes;
377 u32 guest_ss_ar_bytes;
378 u32 guest_ds_ar_bytes;
379 u32 guest_fs_ar_bytes;
380 u32 guest_gs_ar_bytes;
381 u32 guest_ldtr_ar_bytes;
382 u32 guest_tr_ar_bytes;
383 u32 guest_interruptibility_info;
384 u32 guest_activity_state;
385 u32 guest_sysenter_cs;
386 u32 host_ia32_sysenter_cs;
Jan Kiszka0238ea92013-03-13 11:31:24 +0100387 u32 vmx_preemption_timer_value;
388 u32 padding32[7]; /* room for future expansion */
Nadav Har'El22bd0352011-05-25 23:05:57 +0300389 u16 virtual_processor_id;
Wincy Van705699a2015-02-03 23:58:17 +0800390 u16 posted_intr_nv;
Nadav Har'El22bd0352011-05-25 23:05:57 +0300391 u16 guest_es_selector;
392 u16 guest_cs_selector;
393 u16 guest_ss_selector;
394 u16 guest_ds_selector;
395 u16 guest_fs_selector;
396 u16 guest_gs_selector;
397 u16 guest_ldtr_selector;
398 u16 guest_tr_selector;
Wincy Van608406e2015-02-03 23:57:51 +0800399 u16 guest_intr_status;
Bandan Dasc5f983f2017-05-05 15:25:14 -0400400 u16 guest_pml_index;
Nadav Har'El22bd0352011-05-25 23:05:57 +0300401 u16 host_es_selector;
402 u16 host_cs_selector;
403 u16 host_ss_selector;
404 u16 host_ds_selector;
405 u16 host_fs_selector;
406 u16 host_gs_selector;
407 u16 host_tr_selector;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300408};
409
410/*
411 * VMCS12_REVISION is an arbitrary id that should be changed if the content or
412 * layout of struct vmcs12 is changed. MSR_IA32_VMX_BASIC returns this id, and
413 * VMPTRLD verifies that the VMCS region that L1 is loading contains this id.
414 */
415#define VMCS12_REVISION 0x11e57ed0
416
417/*
418 * VMCS12_SIZE is the number of bytes L1 should allocate for the VMXON region
419 * and any VMCS region. Although only sizeof(struct vmcs12) are used by the
420 * current implementation, 4K are reserved to avoid future complications.
421 */
422#define VMCS12_SIZE 0x1000
423
424/*
Jim Mattson5b157062017-12-22 12:11:12 -0800425 * VMCS12_MAX_FIELD_INDEX is the highest index value used in any
426 * supported VMCS12 field encoding.
427 */
428#define VMCS12_MAX_FIELD_INDEX 0x17
429
Paolo Bonzini6677f3d2018-02-26 13:40:08 +0100430struct nested_vmx_msrs {
431 /*
432 * We only store the "true" versions of the VMX capability MSRs. We
433 * generate the "non-true" versions by setting the must-be-1 bits
434 * according to the SDM.
435 */
436 u32 procbased_ctls_low;
437 u32 procbased_ctls_high;
438 u32 secondary_ctls_low;
439 u32 secondary_ctls_high;
440 u32 pinbased_ctls_low;
441 u32 pinbased_ctls_high;
442 u32 exit_ctls_low;
443 u32 exit_ctls_high;
444 u32 entry_ctls_low;
445 u32 entry_ctls_high;
446 u32 misc_low;
447 u32 misc_high;
448 u32 ept_caps;
449 u32 vpid_caps;
450 u64 basic;
451 u64 cr0_fixed0;
452 u64 cr0_fixed1;
453 u64 cr4_fixed0;
454 u64 cr4_fixed1;
455 u64 vmcs_enum;
456 u64 vmfunc_controls;
457};
458
Jim Mattson5b157062017-12-22 12:11:12 -0800459/*
Nadav Har'Elec378ae2011-05-25 23:02:54 +0300460 * The nested_vmx structure is part of vcpu_vmx, and holds information we need
461 * for correct emulation of VMX (i.e., nested VMX) on this vcpu.
462 */
463struct nested_vmx {
464 /* Has the level1 guest done vmxon? */
465 bool vmxon;
Bandan Das3573e222014-05-06 02:19:16 -0400466 gpa_t vmxon_ptr;
Bandan Dasc5f983f2017-05-05 15:25:14 -0400467 bool pml_full;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300468
469 /* The guest-physical address of the current VMCS L1 keeps for L2 */
470 gpa_t current_vmptr;
David Matlack4f2777b2016-07-13 17:16:37 -0700471 /*
472 * Cache of the guest's VMCS, existing outside of guest memory.
473 * Loaded from guest memory during VMPTRLD. Flushed to guest
David Matlack8ca44e82017-08-01 14:00:39 -0700474 * memory during VMCLEAR and VMPTRLD.
David Matlack4f2777b2016-07-13 17:16:37 -0700475 */
476 struct vmcs12 *cached_vmcs12;
Abel Gordon012f83c2013-04-18 14:39:25 +0300477 /*
478 * Indicates if the shadow vmcs must be updated with the
479 * data hold by vmcs12
480 */
481 bool sync_shadow_vmcs;
Paolo Bonzini74a497f2017-12-20 13:55:39 +0100482 bool dirty_vmcs12;
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +0300483
Radim Krčmářdccbfcf2016-08-08 20:16:23 +0200484 bool change_vmcs01_virtual_x2apic_mode;
Nadav Har'El644d7112011-05-25 23:12:35 +0300485 /* L2 must run next, and mustn't decide to exit to L1. */
486 bool nested_run_pending;
Jim Mattsonde3a0022017-11-27 17:22:25 -0600487
488 struct loaded_vmcs vmcs02;
489
Nadav Har'Elfe3ef052011-05-25 23:10:02 +0300490 /*
Jim Mattsonde3a0022017-11-27 17:22:25 -0600491 * Guest pages referred to in the vmcs02 with host-physical
492 * pointers, so we must keep them pinned while L2 runs.
Nadav Har'Elfe3ef052011-05-25 23:10:02 +0300493 */
494 struct page *apic_access_page;
Wanpeng Lia7c0b072014-08-21 19:46:50 +0800495 struct page *virtual_apic_page;
Wincy Van705699a2015-02-03 23:58:17 +0800496 struct page *pi_desc_page;
497 struct pi_desc *pi_desc;
498 bool pi_pending;
499 u16 posted_intr_nv;
Jan Kiszkaf41245002014-03-07 20:03:13 +0100500
501 struct hrtimer preemption_timer;
502 bool preemption_timer_expired;
Jan Kiszka2996fca2014-06-16 13:59:43 +0200503
504 /* to migrate it to L2 if VM_ENTRY_LOAD_DEBUG_CONTROLS is off */
505 u64 vmcs01_debugctl;
Wincy Vanb9c237b2015-02-03 23:56:30 +0800506
Wanpeng Li5c614b32015-10-13 09:18:36 -0700507 u16 vpid02;
508 u16 last_vpid;
509
Paolo Bonzini6677f3d2018-02-26 13:40:08 +0100510 struct nested_vmx_msrs msrs;
Ladi Prosek72e9cbd2017-10-11 16:54:43 +0200511
512 /* SMM related state */
513 struct {
514 /* in VMX operation on SMM entry? */
515 bool vmxon;
516 /* in guest mode on SMM entry? */
517 bool guest_mode;
518 } smm;
Nadav Har'Elec378ae2011-05-25 23:02:54 +0300519};
520
Yang Zhang01e439b2013-04-11 19:25:12 +0800521#define POSTED_INTR_ON 0
Feng Wuebbfc762015-09-18 22:29:46 +0800522#define POSTED_INTR_SN 1
523
Yang Zhang01e439b2013-04-11 19:25:12 +0800524/* Posted-Interrupt Descriptor */
525struct pi_desc {
526 u32 pir[8]; /* Posted interrupt requested */
Feng Wu6ef15222015-09-18 22:29:45 +0800527 union {
528 struct {
529 /* bit 256 - Outstanding Notification */
530 u16 on : 1,
531 /* bit 257 - Suppress Notification */
532 sn : 1,
533 /* bit 271:258 - Reserved */
534 rsvd_1 : 14;
535 /* bit 279:272 - Notification Vector */
536 u8 nv;
537 /* bit 287:280 - Reserved */
538 u8 rsvd_2;
539 /* bit 319:288 - Notification Destination */
540 u32 ndst;
541 };
542 u64 control;
543 };
544 u32 rsvd[6];
Yang Zhang01e439b2013-04-11 19:25:12 +0800545} __aligned(64);
546
Yang Zhanga20ed542013-04-11 19:25:15 +0800547static bool pi_test_and_set_on(struct pi_desc *pi_desc)
548{
549 return test_and_set_bit(POSTED_INTR_ON,
550 (unsigned long *)&pi_desc->control);
551}
552
553static bool pi_test_and_clear_on(struct pi_desc *pi_desc)
554{
555 return test_and_clear_bit(POSTED_INTR_ON,
556 (unsigned long *)&pi_desc->control);
557}
558
559static int pi_test_and_set_pir(int vector, struct pi_desc *pi_desc)
560{
561 return test_and_set_bit(vector, (unsigned long *)pi_desc->pir);
562}
563
Feng Wuebbfc762015-09-18 22:29:46 +0800564static inline void pi_clear_sn(struct pi_desc *pi_desc)
565{
566 return clear_bit(POSTED_INTR_SN,
567 (unsigned long *)&pi_desc->control);
568}
569
570static inline void pi_set_sn(struct pi_desc *pi_desc)
571{
572 return set_bit(POSTED_INTR_SN,
573 (unsigned long *)&pi_desc->control);
574}
575
Paolo Bonziniad361092016-09-20 16:15:05 +0200576static inline void pi_clear_on(struct pi_desc *pi_desc)
577{
578 clear_bit(POSTED_INTR_ON,
579 (unsigned long *)&pi_desc->control);
580}
581
Feng Wuebbfc762015-09-18 22:29:46 +0800582static inline int pi_test_on(struct pi_desc *pi_desc)
583{
584 return test_bit(POSTED_INTR_ON,
585 (unsigned long *)&pi_desc->control);
586}
587
588static inline int pi_test_sn(struct pi_desc *pi_desc)
589{
590 return test_bit(POSTED_INTR_SN,
591 (unsigned long *)&pi_desc->control);
592}
593
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400594struct vcpu_vmx {
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000595 struct kvm_vcpu vcpu;
Avi Kivity313dbd42008-07-17 18:04:30 +0300596 unsigned long host_rsp;
Avi Kivity29bd8a72007-09-10 17:27:03 +0300597 u8 fail;
Paolo Bonzini904e14f2018-01-16 16:51:18 +0100598 u8 msr_bitmap_mode;
Avi Kivity51aa01d2010-07-20 14:31:20 +0300599 u32 exit_intr_info;
Avi Kivity1155f762007-11-22 11:30:47 +0200600 u32 idt_vectoring_info;
Avi Kivity6de12732011-03-07 12:51:22 +0200601 ulong rflags;
Avi Kivity26bb0982009-09-07 11:14:12 +0300602 struct shared_msr_entry *guest_msrs;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400603 int nmsrs;
604 int save_nmsrs;
Yang Zhanga547c6d2013-04-11 19:25:10 +0800605 unsigned long host_idt_base;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400606#ifdef CONFIG_X86_64
Avi Kivity44ea2b12009-09-06 15:55:37 +0300607 u64 msr_host_kernel_gs_base;
608 u64 msr_guest_kernel_gs_base;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400609#endif
Ashok Raj15d45072018-02-01 22:59:43 +0100610
KarimAllah Ahmed28c1c9f2018-02-01 22:59:44 +0100611 u64 arch_capabilities;
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +0100612 u64 spec_ctrl;
KarimAllah Ahmed28c1c9f2018-02-01 22:59:44 +0100613
Gleb Natapov2961e8762013-11-25 15:37:13 +0200614 u32 vm_entry_controls_shadow;
615 u32 vm_exit_controls_shadow;
Paolo Bonzini80154d72017-08-24 13:55:35 +0200616 u32 secondary_exec_control;
617
Nadav Har'Eld462b812011-05-24 15:26:10 +0300618 /*
619 * loaded_vmcs points to the VMCS currently used in this vcpu. For a
620 * non-nested (L1) guest, it always points to vmcs01. For a nested
621 * guest (L2), it points to a different VMCS.
622 */
623 struct loaded_vmcs vmcs01;
624 struct loaded_vmcs *loaded_vmcs;
625 bool __launched; /* temporary, used in vmx_vcpu_run */
Avi Kivity61d2ef22010-04-28 16:40:38 +0300626 struct msr_autoload {
627 unsigned nr;
628 struct vmx_msr_entry guest[NR_AUTOLOAD_MSRS];
629 struct vmx_msr_entry host[NR_AUTOLOAD_MSRS];
630 } msr_autoload;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400631 struct {
632 int loaded;
633 u16 fs_sel, gs_sel, ldt_sel;
Avi Kivityb2da15a2012-05-13 19:53:24 +0300634#ifdef CONFIG_X86_64
635 u16 ds_sel, es_sel;
636#endif
Laurent Vivier152d3f22007-08-23 16:33:11 +0200637 int gs_ldt_reload_needed;
638 int fs_reload_needed;
Liu, Jinsongda8999d2014-02-24 10:55:46 +0000639 u64 msr_host_bndcfgs;
Mike Dayd77c26f2007-10-08 09:02:08 -0400640 } host_state;
Avi Kivity9c8cba32007-11-22 11:42:59 +0200641 struct {
Avi Kivity7ffd92c2009-06-09 14:10:45 +0300642 int vm86_active;
Avi Kivity78ac8b42010-04-08 18:19:35 +0300643 ulong save_rflags;
Avi Kivityf5f7b2f2012-08-21 17:07:00 +0300644 struct kvm_segment segs[8];
645 } rmode;
646 struct {
647 u32 bitmask; /* 4 bits per segment (1 bit per field) */
Avi Kivity7ffd92c2009-06-09 14:10:45 +0300648 struct kvm_save_segment {
649 u16 selector;
650 unsigned long base;
651 u32 limit;
652 u32 ar;
Avi Kivityf5f7b2f2012-08-21 17:07:00 +0300653 } seg[8];
Avi Kivity2fb92db2011-04-27 19:42:18 +0300654 } segment_cache;
Sheng Yang2384d2b2008-01-17 15:14:33 +0800655 int vpid;
Mohammed Gamal04fa4d32008-08-17 16:39:48 +0300656 bool emulation_required;
Jan Kiszka3b86cd92008-09-26 09:30:57 +0200657
Andi Kleena0861c02009-06-08 17:37:09 +0800658 u32 exit_reason;
Sheng Yang4e47c7a2009-12-18 16:48:47 +0800659
Yang Zhang01e439b2013-04-11 19:25:12 +0800660 /* Posted interrupt descriptor */
661 struct pi_desc pi_desc;
662
Nadav Har'Elec378ae2011-05-25 23:02:54 +0300663 /* Support for a guest hypervisor (nested VMX) */
664 struct nested_vmx nested;
Radim Krčmářa7653ec2014-08-21 18:08:07 +0200665
666 /* Dynamic PLE window. */
667 int ple_window;
668 bool ple_window_dirty;
Kai Huang843e4332015-01-28 10:54:28 +0800669
670 /* Support for PML */
671#define PML_ENTITY_NUM 512
672 struct page *pml_pg;
Owen Hofmann2680d6d2016-03-01 13:36:13 -0800673
Yunhong Jiang64672c92016-06-13 14:19:59 -0700674 /* apic deadline value in host tsc */
675 u64 hv_deadline_tsc;
676
Owen Hofmann2680d6d2016-03-01 13:36:13 -0800677 u64 current_tsc_ratio;
Xiao Guangrong1be0e612016-03-22 16:51:18 +0800678
Xiao Guangrong1be0e612016-03-22 16:51:18 +0800679 u32 host_pkru;
Haozhong Zhang3b840802016-06-22 14:59:54 +0800680
Wanpeng Li74c55932017-11-29 01:31:20 -0800681 unsigned long host_debugctlmsr;
682
Haozhong Zhang37e4c992016-06-22 14:59:55 +0800683 /*
684 * Only bits masked by msr_ia32_feature_control_valid_bits can be set in
685 * msr_ia32_feature_control. FEATURE_CONTROL_LOCKED is always included
686 * in msr_ia32_feature_control_valid_bits.
687 */
Haozhong Zhang3b840802016-06-22 14:59:54 +0800688 u64 msr_ia32_feature_control;
Haozhong Zhang37e4c992016-06-22 14:59:55 +0800689 u64 msr_ia32_feature_control_valid_bits;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400690};
691
Avi Kivity2fb92db2011-04-27 19:42:18 +0300692enum segment_cache_field {
693 SEG_FIELD_SEL = 0,
694 SEG_FIELD_BASE = 1,
695 SEG_FIELD_LIMIT = 2,
696 SEG_FIELD_AR = 3,
697
698 SEG_FIELD_NR = 4
699};
700
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400701static inline struct vcpu_vmx *to_vmx(struct kvm_vcpu *vcpu)
702{
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000703 return container_of(vcpu, struct vcpu_vmx, vcpu);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400704}
705
Feng Wuefc64402015-09-18 22:29:51 +0800706static struct pi_desc *vcpu_to_pi_desc(struct kvm_vcpu *vcpu)
707{
708 return &(to_vmx(vcpu)->pi_desc);
709}
710
Jim Mattson58e9ffa2017-12-22 12:13:13 -0800711#define ROL16(val, n) ((u16)(((u16)(val) << (n)) | ((u16)(val) >> (16 - (n)))))
Nadav Har'El22bd0352011-05-25 23:05:57 +0300712#define VMCS12_OFFSET(x) offsetof(struct vmcs12, x)
Jim Mattson58e9ffa2017-12-22 12:13:13 -0800713#define FIELD(number, name) [ROL16(number, 6)] = VMCS12_OFFSET(name)
714#define FIELD64(number, name) \
715 FIELD(number, name), \
716 [ROL16(number##_HIGH, 6)] = VMCS12_OFFSET(name) + sizeof(u32)
Nadav Har'El22bd0352011-05-25 23:05:57 +0300717
Abel Gordon4607c2d2013-04-18 14:35:55 +0300718
Paolo Bonzini44900ba2017-12-13 12:58:02 +0100719static u16 shadow_read_only_fields[] = {
Paolo Bonzinic9e9dea2017-12-20 13:16:29 +0100720#define SHADOW_FIELD_RO(x) x,
721#include "vmx_shadow_fields.h"
Abel Gordon4607c2d2013-04-18 14:35:55 +0300722};
Bandan Dasfe2b2012014-04-21 15:20:14 -0400723static int max_shadow_read_only_fields =
Abel Gordon4607c2d2013-04-18 14:35:55 +0300724 ARRAY_SIZE(shadow_read_only_fields);
725
Paolo Bonzini44900ba2017-12-13 12:58:02 +0100726static u16 shadow_read_write_fields[] = {
Paolo Bonzinic9e9dea2017-12-20 13:16:29 +0100727#define SHADOW_FIELD_RW(x) x,
728#include "vmx_shadow_fields.h"
Abel Gordon4607c2d2013-04-18 14:35:55 +0300729};
Bandan Dasfe2b2012014-04-21 15:20:14 -0400730static int max_shadow_read_write_fields =
Abel Gordon4607c2d2013-04-18 14:35:55 +0300731 ARRAY_SIZE(shadow_read_write_fields);
732
Mathias Krause772e0312012-08-30 01:30:19 +0200733static const unsigned short vmcs_field_to_offset_table[] = {
Nadav Har'El22bd0352011-05-25 23:05:57 +0300734 FIELD(VIRTUAL_PROCESSOR_ID, virtual_processor_id),
Wincy Van705699a2015-02-03 23:58:17 +0800735 FIELD(POSTED_INTR_NV, posted_intr_nv),
Nadav Har'El22bd0352011-05-25 23:05:57 +0300736 FIELD(GUEST_ES_SELECTOR, guest_es_selector),
737 FIELD(GUEST_CS_SELECTOR, guest_cs_selector),
738 FIELD(GUEST_SS_SELECTOR, guest_ss_selector),
739 FIELD(GUEST_DS_SELECTOR, guest_ds_selector),
740 FIELD(GUEST_FS_SELECTOR, guest_fs_selector),
741 FIELD(GUEST_GS_SELECTOR, guest_gs_selector),
742 FIELD(GUEST_LDTR_SELECTOR, guest_ldtr_selector),
743 FIELD(GUEST_TR_SELECTOR, guest_tr_selector),
Wincy Van608406e2015-02-03 23:57:51 +0800744 FIELD(GUEST_INTR_STATUS, guest_intr_status),
Bandan Dasc5f983f2017-05-05 15:25:14 -0400745 FIELD(GUEST_PML_INDEX, guest_pml_index),
Nadav Har'El22bd0352011-05-25 23:05:57 +0300746 FIELD(HOST_ES_SELECTOR, host_es_selector),
747 FIELD(HOST_CS_SELECTOR, host_cs_selector),
748 FIELD(HOST_SS_SELECTOR, host_ss_selector),
749 FIELD(HOST_DS_SELECTOR, host_ds_selector),
750 FIELD(HOST_FS_SELECTOR, host_fs_selector),
751 FIELD(HOST_GS_SELECTOR, host_gs_selector),
752 FIELD(HOST_TR_SELECTOR, host_tr_selector),
753 FIELD64(IO_BITMAP_A, io_bitmap_a),
754 FIELD64(IO_BITMAP_B, io_bitmap_b),
755 FIELD64(MSR_BITMAP, msr_bitmap),
756 FIELD64(VM_EXIT_MSR_STORE_ADDR, vm_exit_msr_store_addr),
757 FIELD64(VM_EXIT_MSR_LOAD_ADDR, vm_exit_msr_load_addr),
758 FIELD64(VM_ENTRY_MSR_LOAD_ADDR, vm_entry_msr_load_addr),
759 FIELD64(TSC_OFFSET, tsc_offset),
760 FIELD64(VIRTUAL_APIC_PAGE_ADDR, virtual_apic_page_addr),
761 FIELD64(APIC_ACCESS_ADDR, apic_access_addr),
Wincy Van705699a2015-02-03 23:58:17 +0800762 FIELD64(POSTED_INTR_DESC_ADDR, posted_intr_desc_addr),
Bandan Das27c42a12017-08-03 15:54:42 -0400763 FIELD64(VM_FUNCTION_CONTROL, vm_function_control),
Nadav Har'El22bd0352011-05-25 23:05:57 +0300764 FIELD64(EPT_POINTER, ept_pointer),
Wincy Van608406e2015-02-03 23:57:51 +0800765 FIELD64(EOI_EXIT_BITMAP0, eoi_exit_bitmap0),
766 FIELD64(EOI_EXIT_BITMAP1, eoi_exit_bitmap1),
767 FIELD64(EOI_EXIT_BITMAP2, eoi_exit_bitmap2),
768 FIELD64(EOI_EXIT_BITMAP3, eoi_exit_bitmap3),
Bandan Das41ab9372017-08-03 15:54:43 -0400769 FIELD64(EPTP_LIST_ADDRESS, eptp_list_address),
Wanpeng Li81dc01f2014-12-04 19:11:07 +0800770 FIELD64(XSS_EXIT_BITMAP, xss_exit_bitmap),
Nadav Har'El22bd0352011-05-25 23:05:57 +0300771 FIELD64(GUEST_PHYSICAL_ADDRESS, guest_physical_address),
772 FIELD64(VMCS_LINK_POINTER, vmcs_link_pointer),
Bandan Dasc5f983f2017-05-05 15:25:14 -0400773 FIELD64(PML_ADDRESS, pml_address),
Nadav Har'El22bd0352011-05-25 23:05:57 +0300774 FIELD64(GUEST_IA32_DEBUGCTL, guest_ia32_debugctl),
775 FIELD64(GUEST_IA32_PAT, guest_ia32_pat),
776 FIELD64(GUEST_IA32_EFER, guest_ia32_efer),
777 FIELD64(GUEST_IA32_PERF_GLOBAL_CTRL, guest_ia32_perf_global_ctrl),
778 FIELD64(GUEST_PDPTR0, guest_pdptr0),
779 FIELD64(GUEST_PDPTR1, guest_pdptr1),
780 FIELD64(GUEST_PDPTR2, guest_pdptr2),
781 FIELD64(GUEST_PDPTR3, guest_pdptr3),
Paolo Bonzini36be0b92014-02-24 12:30:04 +0100782 FIELD64(GUEST_BNDCFGS, guest_bndcfgs),
Nadav Har'El22bd0352011-05-25 23:05:57 +0300783 FIELD64(HOST_IA32_PAT, host_ia32_pat),
784 FIELD64(HOST_IA32_EFER, host_ia32_efer),
785 FIELD64(HOST_IA32_PERF_GLOBAL_CTRL, host_ia32_perf_global_ctrl),
786 FIELD(PIN_BASED_VM_EXEC_CONTROL, pin_based_vm_exec_control),
787 FIELD(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control),
788 FIELD(EXCEPTION_BITMAP, exception_bitmap),
789 FIELD(PAGE_FAULT_ERROR_CODE_MASK, page_fault_error_code_mask),
790 FIELD(PAGE_FAULT_ERROR_CODE_MATCH, page_fault_error_code_match),
791 FIELD(CR3_TARGET_COUNT, cr3_target_count),
792 FIELD(VM_EXIT_CONTROLS, vm_exit_controls),
793 FIELD(VM_EXIT_MSR_STORE_COUNT, vm_exit_msr_store_count),
794 FIELD(VM_EXIT_MSR_LOAD_COUNT, vm_exit_msr_load_count),
795 FIELD(VM_ENTRY_CONTROLS, vm_entry_controls),
796 FIELD(VM_ENTRY_MSR_LOAD_COUNT, vm_entry_msr_load_count),
797 FIELD(VM_ENTRY_INTR_INFO_FIELD, vm_entry_intr_info_field),
798 FIELD(VM_ENTRY_EXCEPTION_ERROR_CODE, vm_entry_exception_error_code),
799 FIELD(VM_ENTRY_INSTRUCTION_LEN, vm_entry_instruction_len),
800 FIELD(TPR_THRESHOLD, tpr_threshold),
801 FIELD(SECONDARY_VM_EXEC_CONTROL, secondary_vm_exec_control),
802 FIELD(VM_INSTRUCTION_ERROR, vm_instruction_error),
803 FIELD(VM_EXIT_REASON, vm_exit_reason),
804 FIELD(VM_EXIT_INTR_INFO, vm_exit_intr_info),
805 FIELD(VM_EXIT_INTR_ERROR_CODE, vm_exit_intr_error_code),
806 FIELD(IDT_VECTORING_INFO_FIELD, idt_vectoring_info_field),
807 FIELD(IDT_VECTORING_ERROR_CODE, idt_vectoring_error_code),
808 FIELD(VM_EXIT_INSTRUCTION_LEN, vm_exit_instruction_len),
809 FIELD(VMX_INSTRUCTION_INFO, vmx_instruction_info),
810 FIELD(GUEST_ES_LIMIT, guest_es_limit),
811 FIELD(GUEST_CS_LIMIT, guest_cs_limit),
812 FIELD(GUEST_SS_LIMIT, guest_ss_limit),
813 FIELD(GUEST_DS_LIMIT, guest_ds_limit),
814 FIELD(GUEST_FS_LIMIT, guest_fs_limit),
815 FIELD(GUEST_GS_LIMIT, guest_gs_limit),
816 FIELD(GUEST_LDTR_LIMIT, guest_ldtr_limit),
817 FIELD(GUEST_TR_LIMIT, guest_tr_limit),
818 FIELD(GUEST_GDTR_LIMIT, guest_gdtr_limit),
819 FIELD(GUEST_IDTR_LIMIT, guest_idtr_limit),
820 FIELD(GUEST_ES_AR_BYTES, guest_es_ar_bytes),
821 FIELD(GUEST_CS_AR_BYTES, guest_cs_ar_bytes),
822 FIELD(GUEST_SS_AR_BYTES, guest_ss_ar_bytes),
823 FIELD(GUEST_DS_AR_BYTES, guest_ds_ar_bytes),
824 FIELD(GUEST_FS_AR_BYTES, guest_fs_ar_bytes),
825 FIELD(GUEST_GS_AR_BYTES, guest_gs_ar_bytes),
826 FIELD(GUEST_LDTR_AR_BYTES, guest_ldtr_ar_bytes),
827 FIELD(GUEST_TR_AR_BYTES, guest_tr_ar_bytes),
828 FIELD(GUEST_INTERRUPTIBILITY_INFO, guest_interruptibility_info),
829 FIELD(GUEST_ACTIVITY_STATE, guest_activity_state),
830 FIELD(GUEST_SYSENTER_CS, guest_sysenter_cs),
831 FIELD(HOST_IA32_SYSENTER_CS, host_ia32_sysenter_cs),
Jan Kiszka0238ea92013-03-13 11:31:24 +0100832 FIELD(VMX_PREEMPTION_TIMER_VALUE, vmx_preemption_timer_value),
Nadav Har'El22bd0352011-05-25 23:05:57 +0300833 FIELD(CR0_GUEST_HOST_MASK, cr0_guest_host_mask),
834 FIELD(CR4_GUEST_HOST_MASK, cr4_guest_host_mask),
835 FIELD(CR0_READ_SHADOW, cr0_read_shadow),
836 FIELD(CR4_READ_SHADOW, cr4_read_shadow),
837 FIELD(CR3_TARGET_VALUE0, cr3_target_value0),
838 FIELD(CR3_TARGET_VALUE1, cr3_target_value1),
839 FIELD(CR3_TARGET_VALUE2, cr3_target_value2),
840 FIELD(CR3_TARGET_VALUE3, cr3_target_value3),
841 FIELD(EXIT_QUALIFICATION, exit_qualification),
842 FIELD(GUEST_LINEAR_ADDRESS, guest_linear_address),
843 FIELD(GUEST_CR0, guest_cr0),
844 FIELD(GUEST_CR3, guest_cr3),
845 FIELD(GUEST_CR4, guest_cr4),
846 FIELD(GUEST_ES_BASE, guest_es_base),
847 FIELD(GUEST_CS_BASE, guest_cs_base),
848 FIELD(GUEST_SS_BASE, guest_ss_base),
849 FIELD(GUEST_DS_BASE, guest_ds_base),
850 FIELD(GUEST_FS_BASE, guest_fs_base),
851 FIELD(GUEST_GS_BASE, guest_gs_base),
852 FIELD(GUEST_LDTR_BASE, guest_ldtr_base),
853 FIELD(GUEST_TR_BASE, guest_tr_base),
854 FIELD(GUEST_GDTR_BASE, guest_gdtr_base),
855 FIELD(GUEST_IDTR_BASE, guest_idtr_base),
856 FIELD(GUEST_DR7, guest_dr7),
857 FIELD(GUEST_RSP, guest_rsp),
858 FIELD(GUEST_RIP, guest_rip),
859 FIELD(GUEST_RFLAGS, guest_rflags),
860 FIELD(GUEST_PENDING_DBG_EXCEPTIONS, guest_pending_dbg_exceptions),
861 FIELD(GUEST_SYSENTER_ESP, guest_sysenter_esp),
862 FIELD(GUEST_SYSENTER_EIP, guest_sysenter_eip),
863 FIELD(HOST_CR0, host_cr0),
864 FIELD(HOST_CR3, host_cr3),
865 FIELD(HOST_CR4, host_cr4),
866 FIELD(HOST_FS_BASE, host_fs_base),
867 FIELD(HOST_GS_BASE, host_gs_base),
868 FIELD(HOST_TR_BASE, host_tr_base),
869 FIELD(HOST_GDTR_BASE, host_gdtr_base),
870 FIELD(HOST_IDTR_BASE, host_idtr_base),
871 FIELD(HOST_IA32_SYSENTER_ESP, host_ia32_sysenter_esp),
872 FIELD(HOST_IA32_SYSENTER_EIP, host_ia32_sysenter_eip),
873 FIELD(HOST_RSP, host_rsp),
874 FIELD(HOST_RIP, host_rip),
875};
Nadav Har'El22bd0352011-05-25 23:05:57 +0300876
877static inline short vmcs_field_to_offset(unsigned long field)
878{
Dan Williams085331d2018-01-31 17:47:03 -0800879 const size_t size = ARRAY_SIZE(vmcs_field_to_offset_table);
880 unsigned short offset;
Jim Mattson58e9ffa2017-12-22 12:13:13 -0800881 unsigned index;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +0100882
Jim Mattson58e9ffa2017-12-22 12:13:13 -0800883 if (field >> 15)
Andrew Honig75f139a2018-01-10 10:12:03 -0800884 return -ENOENT;
885
Jim Mattson58e9ffa2017-12-22 12:13:13 -0800886 index = ROL16(field, 6);
Linus Torvalds15303ba2018-02-10 13:16:35 -0800887 if (index >= size)
Andrew Honig75f139a2018-01-10 10:12:03 -0800888 return -ENOENT;
889
Linus Torvalds15303ba2018-02-10 13:16:35 -0800890 index = array_index_nospec(index, size);
891 offset = vmcs_field_to_offset_table[index];
Dan Williams085331d2018-01-31 17:47:03 -0800892 if (offset == 0)
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +0100893 return -ENOENT;
Dan Williams085331d2018-01-31 17:47:03 -0800894 return offset;
Nadav Har'El22bd0352011-05-25 23:05:57 +0300895}
896
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300897static inline struct vmcs12 *get_vmcs12(struct kvm_vcpu *vcpu)
898{
David Matlack4f2777b2016-07-13 17:16:37 -0700899 return to_vmx(vcpu)->nested.cached_vmcs12;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300900}
901
Peter Feiner995f00a2017-06-30 17:26:32 -0700902static bool nested_ept_ad_enabled(struct kvm_vcpu *vcpu);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +0300903static unsigned long nested_ept_get_cr3(struct kvm_vcpu *vcpu);
Peter Feiner995f00a2017-06-30 17:26:32 -0700904static u64 construct_eptp(struct kvm_vcpu *vcpu, unsigned long root_hpa);
Wanpeng Lif53cd632014-12-02 19:14:58 +0800905static bool vmx_xsaves_supported(void);
Orit Wassermanb246dd52012-05-31 14:49:22 +0300906static void vmx_set_segment(struct kvm_vcpu *vcpu,
907 struct kvm_segment *var, int seg);
908static void vmx_get_segment(struct kvm_vcpu *vcpu,
909 struct kvm_segment *var, int seg);
Gleb Natapovd99e4152012-12-20 16:57:45 +0200910static bool guest_state_valid(struct kvm_vcpu *vcpu);
911static u32 vmx_segment_access_rights(struct kvm_segment *var);
Abel Gordon16f5b902013-04-18 14:38:25 +0300912static void copy_shadow_to_vmcs12(struct vcpu_vmx *vmx);
Paolo Bonzinib96fb432017-07-27 12:29:32 +0200913static bool vmx_get_nmi_mask(struct kvm_vcpu *vcpu);
914static void vmx_set_nmi_mask(struct kvm_vcpu *vcpu, bool masked);
915static bool nested_vmx_is_page_fault_vmexit(struct vmcs12 *vmcs12,
916 u16 error_code);
Paolo Bonzini904e14f2018-01-16 16:51:18 +0100917static void vmx_update_msr_bitmap(struct kvm_vcpu *vcpu);
Ashok Raj15d45072018-02-01 22:59:43 +0100918static void __always_inline vmx_disable_intercept_for_msr(unsigned long *msr_bitmap,
919 u32 msr, int type);
Avi Kivity75880a02007-06-20 11:20:04 +0300920
Avi Kivity6aa8b732006-12-10 02:21:36 -0800921static DEFINE_PER_CPU(struct vmcs *, vmxarea);
922static DEFINE_PER_CPU(struct vmcs *, current_vmcs);
Nadav Har'Eld462b812011-05-24 15:26:10 +0300923/*
924 * We maintain a per-CPU linked-list of VMCS loaded on that CPU. This is needed
925 * when a CPU is brought down, and we need to VMCLEAR all VMCSs loaded on it.
926 */
927static DEFINE_PER_CPU(struct list_head, loaded_vmcss_on_cpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800928
Feng Wubf9f6ac2015-09-18 22:29:55 +0800929/*
930 * We maintian a per-CPU linked-list of vCPU, so in wakeup_handler() we
931 * can find which vCPU should be waken up.
932 */
933static DEFINE_PER_CPU(struct list_head, blocked_vcpu_on_cpu);
934static DEFINE_PER_CPU(spinlock_t, blocked_vcpu_on_cpu_lock);
935
Radim Krčmář23611332016-09-29 22:41:33 +0200936enum {
Radim Krčmář23611332016-09-29 22:41:33 +0200937 VMX_VMREAD_BITMAP,
938 VMX_VMWRITE_BITMAP,
939 VMX_BITMAP_NR
940};
941
942static unsigned long *vmx_bitmap[VMX_BITMAP_NR];
943
Radim Krčmář23611332016-09-29 22:41:33 +0200944#define vmx_vmread_bitmap (vmx_bitmap[VMX_VMREAD_BITMAP])
945#define vmx_vmwrite_bitmap (vmx_bitmap[VMX_VMWRITE_BITMAP])
He, Qingfdef3ad2007-04-30 09:45:24 +0300946
Avi Kivity110312c2010-12-21 12:54:20 +0200947static bool cpu_has_load_ia32_efer;
Gleb Natapov8bf00a52011-10-05 14:01:22 +0200948static bool cpu_has_load_perf_global_ctrl;
Avi Kivity110312c2010-12-21 12:54:20 +0200949
Sheng Yang2384d2b2008-01-17 15:14:33 +0800950static DECLARE_BITMAP(vmx_vpid_bitmap, VMX_NR_VPIDS);
951static DEFINE_SPINLOCK(vmx_vpid_lock);
952
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +0300953static struct vmcs_config {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800954 int size;
955 int order;
Jan Dakinevich9ac7e3e2016-09-04 21:23:15 +0300956 u32 basic_cap;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800957 u32 revision_id;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +0300958 u32 pin_based_exec_ctrl;
959 u32 cpu_based_exec_ctrl;
Sheng Yangf78e0e22007-10-29 09:40:42 +0800960 u32 cpu_based_2nd_exec_ctrl;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +0300961 u32 vmexit_ctrl;
962 u32 vmentry_ctrl;
Paolo Bonzini13893092018-02-26 13:40:09 +0100963 struct nested_vmx_msrs nested;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +0300964} vmcs_config;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800965
Hannes Ederefff9e52008-11-28 17:02:06 +0100966static struct vmx_capability {
Sheng Yangd56f5462008-04-25 10:13:16 +0800967 u32 ept;
968 u32 vpid;
969} vmx_capability;
970
Avi Kivity6aa8b732006-12-10 02:21:36 -0800971#define VMX_SEGMENT_FIELD(seg) \
972 [VCPU_SREG_##seg] = { \
973 .selector = GUEST_##seg##_SELECTOR, \
974 .base = GUEST_##seg##_BASE, \
975 .limit = GUEST_##seg##_LIMIT, \
976 .ar_bytes = GUEST_##seg##_AR_BYTES, \
977 }
978
Mathias Krause772e0312012-08-30 01:30:19 +0200979static const struct kvm_vmx_segment_field {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800980 unsigned selector;
981 unsigned base;
982 unsigned limit;
983 unsigned ar_bytes;
984} kvm_vmx_segment_fields[] = {
985 VMX_SEGMENT_FIELD(CS),
986 VMX_SEGMENT_FIELD(DS),
987 VMX_SEGMENT_FIELD(ES),
988 VMX_SEGMENT_FIELD(FS),
989 VMX_SEGMENT_FIELD(GS),
990 VMX_SEGMENT_FIELD(SS),
991 VMX_SEGMENT_FIELD(TR),
992 VMX_SEGMENT_FIELD(LDTR),
993};
994
Avi Kivity26bb0982009-09-07 11:14:12 +0300995static u64 host_efer;
996
Avi Kivity6de4f3a2009-05-31 22:58:47 +0300997static void ept_save_pdptrs(struct kvm_vcpu *vcpu);
998
Avi Kivity4d56c8a2007-04-19 14:28:44 +0300999/*
Brian Gerst8c065852010-07-17 09:03:26 -04001000 * Keep MSR_STAR at the end, as setup_msrs() will try to optimize it
Avi Kivity4d56c8a2007-04-19 14:28:44 +03001001 * away by decrementing the array size.
1002 */
Avi Kivity6aa8b732006-12-10 02:21:36 -08001003static const u32 vmx_msr_index[] = {
Avi Kivity05b3e0c2006-12-13 00:33:45 -08001004#ifdef CONFIG_X86_64
Avi Kivity44ea2b12009-09-06 15:55:37 +03001005 MSR_SYSCALL_MASK, MSR_LSTAR, MSR_CSTAR,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001006#endif
Brian Gerst8c065852010-07-17 09:03:26 -04001007 MSR_EFER, MSR_TSC_AUX, MSR_STAR,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001008};
Avi Kivity6aa8b732006-12-10 02:21:36 -08001009
Jan Kiszka5bb16012016-02-09 20:14:21 +01001010static inline bool is_exception_n(u32 intr_info, u8 vector)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001011{
1012 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
1013 INTR_INFO_VALID_MASK)) ==
Jan Kiszka5bb16012016-02-09 20:14:21 +01001014 (INTR_TYPE_HARD_EXCEPTION | vector | INTR_INFO_VALID_MASK);
1015}
1016
Jan Kiszka6f054852016-02-09 20:15:18 +01001017static inline bool is_debug(u32 intr_info)
1018{
1019 return is_exception_n(intr_info, DB_VECTOR);
1020}
1021
1022static inline bool is_breakpoint(u32 intr_info)
1023{
1024 return is_exception_n(intr_info, BP_VECTOR);
1025}
1026
Jan Kiszka5bb16012016-02-09 20:14:21 +01001027static inline bool is_page_fault(u32 intr_info)
1028{
1029 return is_exception_n(intr_info, PF_VECTOR);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001030}
1031
Gui Jianfeng31299942010-03-15 17:29:09 +08001032static inline bool is_no_device(u32 intr_info)
Anthony Liguori2ab455c2007-04-27 09:29:49 +03001033{
Jan Kiszka5bb16012016-02-09 20:14:21 +01001034 return is_exception_n(intr_info, NM_VECTOR);
Anthony Liguori2ab455c2007-04-27 09:29:49 +03001035}
1036
Gui Jianfeng31299942010-03-15 17:29:09 +08001037static inline bool is_invalid_opcode(u32 intr_info)
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001038{
Jan Kiszka5bb16012016-02-09 20:14:21 +01001039 return is_exception_n(intr_info, UD_VECTOR);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001040}
1041
Liran Alon9e869482018-03-12 13:12:51 +02001042static inline bool is_gp_fault(u32 intr_info)
1043{
1044 return is_exception_n(intr_info, GP_VECTOR);
1045}
1046
Gui Jianfeng31299942010-03-15 17:29:09 +08001047static inline bool is_external_interrupt(u32 intr_info)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001048{
1049 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK))
1050 == (INTR_TYPE_EXT_INTR | INTR_INFO_VALID_MASK);
1051}
1052
Gui Jianfeng31299942010-03-15 17:29:09 +08001053static inline bool is_machine_check(u32 intr_info)
Andi Kleena0861c02009-06-08 17:37:09 +08001054{
1055 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
1056 INTR_INFO_VALID_MASK)) ==
1057 (INTR_TYPE_HARD_EXCEPTION | MC_VECTOR | INTR_INFO_VALID_MASK);
1058}
1059
Gui Jianfeng31299942010-03-15 17:29:09 +08001060static inline bool cpu_has_vmx_msr_bitmap(void)
Sheng Yang25c5f222008-03-28 13:18:56 +08001061{
Sheng Yang04547152009-04-01 15:52:31 +08001062 return vmcs_config.cpu_based_exec_ctrl & CPU_BASED_USE_MSR_BITMAPS;
Sheng Yang25c5f222008-03-28 13:18:56 +08001063}
1064
Gui Jianfeng31299942010-03-15 17:29:09 +08001065static inline bool cpu_has_vmx_tpr_shadow(void)
Yang, Sheng6e5d8652007-09-12 18:03:11 +08001066{
Sheng Yang04547152009-04-01 15:52:31 +08001067 return vmcs_config.cpu_based_exec_ctrl & CPU_BASED_TPR_SHADOW;
Yang, Sheng6e5d8652007-09-12 18:03:11 +08001068}
1069
Paolo Bonzini35754c92015-07-29 12:05:37 +02001070static inline bool cpu_need_tpr_shadow(struct kvm_vcpu *vcpu)
Yang, Sheng6e5d8652007-09-12 18:03:11 +08001071{
Paolo Bonzini35754c92015-07-29 12:05:37 +02001072 return cpu_has_vmx_tpr_shadow() && lapic_in_kernel(vcpu);
Yang, Sheng6e5d8652007-09-12 18:03:11 +08001073}
1074
Gui Jianfeng31299942010-03-15 17:29:09 +08001075static inline bool cpu_has_secondary_exec_ctrls(void)
Sheng Yangf78e0e22007-10-29 09:40:42 +08001076{
Sheng Yang04547152009-04-01 15:52:31 +08001077 return vmcs_config.cpu_based_exec_ctrl &
1078 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
Sheng Yangf78e0e22007-10-29 09:40:42 +08001079}
1080
Avi Kivity774ead32007-12-26 13:57:04 +02001081static inline bool cpu_has_vmx_virtualize_apic_accesses(void)
Sheng Yangf78e0e22007-10-29 09:40:42 +08001082{
Sheng Yang04547152009-04-01 15:52:31 +08001083 return vmcs_config.cpu_based_2nd_exec_ctrl &
1084 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
1085}
1086
Yang Zhang8d146952013-01-25 10:18:50 +08001087static inline bool cpu_has_vmx_virtualize_x2apic_mode(void)
1088{
1089 return vmcs_config.cpu_based_2nd_exec_ctrl &
1090 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
1091}
1092
Yang Zhang83d4c282013-01-25 10:18:49 +08001093static inline bool cpu_has_vmx_apic_register_virt(void)
1094{
1095 return vmcs_config.cpu_based_2nd_exec_ctrl &
1096 SECONDARY_EXEC_APIC_REGISTER_VIRT;
1097}
1098
Yang Zhangc7c9c562013-01-25 10:18:51 +08001099static inline bool cpu_has_vmx_virtual_intr_delivery(void)
1100{
1101 return vmcs_config.cpu_based_2nd_exec_ctrl &
1102 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY;
1103}
1104
Yunhong Jiang64672c92016-06-13 14:19:59 -07001105/*
1106 * Comment's format: document - errata name - stepping - processor name.
1107 * Refer from
1108 * https://www.virtualbox.org/svn/vbox/trunk/src/VBox/VMM/VMMR0/HMR0.cpp
1109 */
1110static u32 vmx_preemption_cpu_tfms[] = {
1111/* 323344.pdf - BA86 - D0 - Xeon 7500 Series */
11120x000206E6,
1113/* 323056.pdf - AAX65 - C2 - Xeon L3406 */
1114/* 322814.pdf - AAT59 - C2 - i7-600, i5-500, i5-400 and i3-300 Mobile */
1115/* 322911.pdf - AAU65 - C2 - i5-600, i3-500 Desktop and Pentium G6950 */
11160x00020652,
1117/* 322911.pdf - AAU65 - K0 - i5-600, i3-500 Desktop and Pentium G6950 */
11180x00020655,
1119/* 322373.pdf - AAO95 - B1 - Xeon 3400 Series */
1120/* 322166.pdf - AAN92 - B1 - i7-800 and i5-700 Desktop */
1121/*
1122 * 320767.pdf - AAP86 - B1 -
1123 * i7-900 Mobile Extreme, i7-800 and i7-700 Mobile
1124 */
11250x000106E5,
1126/* 321333.pdf - AAM126 - C0 - Xeon 3500 */
11270x000106A0,
1128/* 321333.pdf - AAM126 - C1 - Xeon 3500 */
11290x000106A1,
1130/* 320836.pdf - AAJ124 - C0 - i7-900 Desktop Extreme and i7-900 Desktop */
11310x000106A4,
1132 /* 321333.pdf - AAM126 - D0 - Xeon 3500 */
1133 /* 321324.pdf - AAK139 - D0 - Xeon 5500 */
1134 /* 320836.pdf - AAJ124 - D0 - i7-900 Extreme and i7-900 Desktop */
11350x000106A5,
1136};
1137
1138static inline bool cpu_has_broken_vmx_preemption_timer(void)
1139{
1140 u32 eax = cpuid_eax(0x00000001), i;
1141
1142 /* Clear the reserved bits */
1143 eax &= ~(0x3U << 14 | 0xfU << 28);
Wei Yongjun03f6a222016-07-04 15:13:07 +00001144 for (i = 0; i < ARRAY_SIZE(vmx_preemption_cpu_tfms); i++)
Yunhong Jiang64672c92016-06-13 14:19:59 -07001145 if (eax == vmx_preemption_cpu_tfms[i])
1146 return true;
1147
1148 return false;
1149}
1150
1151static inline bool cpu_has_vmx_preemption_timer(void)
1152{
Yunhong Jiang64672c92016-06-13 14:19:59 -07001153 return vmcs_config.pin_based_exec_ctrl &
1154 PIN_BASED_VMX_PREEMPTION_TIMER;
1155}
1156
Yang Zhang01e439b2013-04-11 19:25:12 +08001157static inline bool cpu_has_vmx_posted_intr(void)
1158{
Paolo Bonzinid6a858d2015-09-28 11:58:14 +02001159 return IS_ENABLED(CONFIG_X86_LOCAL_APIC) &&
1160 vmcs_config.pin_based_exec_ctrl & PIN_BASED_POSTED_INTR;
Yang Zhang01e439b2013-04-11 19:25:12 +08001161}
1162
1163static inline bool cpu_has_vmx_apicv(void)
1164{
1165 return cpu_has_vmx_apic_register_virt() &&
1166 cpu_has_vmx_virtual_intr_delivery() &&
1167 cpu_has_vmx_posted_intr();
1168}
1169
Sheng Yang04547152009-04-01 15:52:31 +08001170static inline bool cpu_has_vmx_flexpriority(void)
1171{
1172 return cpu_has_vmx_tpr_shadow() &&
1173 cpu_has_vmx_virtualize_apic_accesses();
Sheng Yangf78e0e22007-10-29 09:40:42 +08001174}
1175
Marcelo Tosattie7997942009-06-11 12:07:40 -03001176static inline bool cpu_has_vmx_ept_execute_only(void)
1177{
Gui Jianfeng31299942010-03-15 17:29:09 +08001178 return vmx_capability.ept & VMX_EPT_EXECUTE_ONLY_BIT;
Marcelo Tosattie7997942009-06-11 12:07:40 -03001179}
1180
Marcelo Tosattie7997942009-06-11 12:07:40 -03001181static inline bool cpu_has_vmx_ept_2m_page(void)
1182{
Gui Jianfeng31299942010-03-15 17:29:09 +08001183 return vmx_capability.ept & VMX_EPT_2MB_PAGE_BIT;
Marcelo Tosattie7997942009-06-11 12:07:40 -03001184}
1185
Sheng Yang878403b2010-01-05 19:02:29 +08001186static inline bool cpu_has_vmx_ept_1g_page(void)
1187{
Gui Jianfeng31299942010-03-15 17:29:09 +08001188 return vmx_capability.ept & VMX_EPT_1GB_PAGE_BIT;
Sheng Yang878403b2010-01-05 19:02:29 +08001189}
1190
Sheng Yang4bc9b982010-06-02 14:05:24 +08001191static inline bool cpu_has_vmx_ept_4levels(void)
1192{
1193 return vmx_capability.ept & VMX_EPT_PAGE_WALK_4_BIT;
1194}
1195
David Hildenbrand42aa53b2017-08-10 23:15:29 +02001196static inline bool cpu_has_vmx_ept_mt_wb(void)
1197{
1198 return vmx_capability.ept & VMX_EPTP_WB_BIT;
1199}
1200
Yu Zhang855feb62017-08-24 20:27:55 +08001201static inline bool cpu_has_vmx_ept_5levels(void)
1202{
1203 return vmx_capability.ept & VMX_EPT_PAGE_WALK_5_BIT;
1204}
1205
Xudong Hao83c3a332012-05-28 19:33:35 +08001206static inline bool cpu_has_vmx_ept_ad_bits(void)
1207{
1208 return vmx_capability.ept & VMX_EPT_AD_BIT;
1209}
1210
Gui Jianfeng31299942010-03-15 17:29:09 +08001211static inline bool cpu_has_vmx_invept_context(void)
Sheng Yangd56f5462008-04-25 10:13:16 +08001212{
Gui Jianfeng31299942010-03-15 17:29:09 +08001213 return vmx_capability.ept & VMX_EPT_EXTENT_CONTEXT_BIT;
Sheng Yangd56f5462008-04-25 10:13:16 +08001214}
1215
Gui Jianfeng31299942010-03-15 17:29:09 +08001216static inline bool cpu_has_vmx_invept_global(void)
Sheng Yangd56f5462008-04-25 10:13:16 +08001217{
Gui Jianfeng31299942010-03-15 17:29:09 +08001218 return vmx_capability.ept & VMX_EPT_EXTENT_GLOBAL_BIT;
Sheng Yangd56f5462008-04-25 10:13:16 +08001219}
1220
Gui Jianfeng518c8ae2010-06-04 08:51:39 +08001221static inline bool cpu_has_vmx_invvpid_single(void)
1222{
1223 return vmx_capability.vpid & VMX_VPID_EXTENT_SINGLE_CONTEXT_BIT;
1224}
1225
Gui Jianfengb9d762f2010-06-07 10:32:29 +08001226static inline bool cpu_has_vmx_invvpid_global(void)
1227{
1228 return vmx_capability.vpid & VMX_VPID_EXTENT_GLOBAL_CONTEXT_BIT;
1229}
1230
Wanpeng Li08d839c2017-03-23 05:30:08 -07001231static inline bool cpu_has_vmx_invvpid(void)
1232{
1233 return vmx_capability.vpid & VMX_VPID_INVVPID_BIT;
1234}
1235
Gui Jianfeng31299942010-03-15 17:29:09 +08001236static inline bool cpu_has_vmx_ept(void)
Sheng Yangd56f5462008-04-25 10:13:16 +08001237{
Sheng Yang04547152009-04-01 15:52:31 +08001238 return vmcs_config.cpu_based_2nd_exec_ctrl &
1239 SECONDARY_EXEC_ENABLE_EPT;
Sheng Yangd56f5462008-04-25 10:13:16 +08001240}
1241
Gui Jianfeng31299942010-03-15 17:29:09 +08001242static inline bool cpu_has_vmx_unrestricted_guest(void)
Nitin A Kamble3a624e22009-06-08 11:34:16 -07001243{
1244 return vmcs_config.cpu_based_2nd_exec_ctrl &
1245 SECONDARY_EXEC_UNRESTRICTED_GUEST;
1246}
1247
Gui Jianfeng31299942010-03-15 17:29:09 +08001248static inline bool cpu_has_vmx_ple(void)
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08001249{
1250 return vmcs_config.cpu_based_2nd_exec_ctrl &
1251 SECONDARY_EXEC_PAUSE_LOOP_EXITING;
1252}
1253
Jan Dakinevich9ac7e3e2016-09-04 21:23:15 +03001254static inline bool cpu_has_vmx_basic_inout(void)
1255{
1256 return (((u64)vmcs_config.basic_cap << 32) & VMX_BASIC_INOUT);
1257}
1258
Paolo Bonzini35754c92015-07-29 12:05:37 +02001259static inline bool cpu_need_virtualize_apic_accesses(struct kvm_vcpu *vcpu)
Sheng Yangf78e0e22007-10-29 09:40:42 +08001260{
Paolo Bonzini35754c92015-07-29 12:05:37 +02001261 return flexpriority_enabled && lapic_in_kernel(vcpu);
Sheng Yangf78e0e22007-10-29 09:40:42 +08001262}
1263
Gui Jianfeng31299942010-03-15 17:29:09 +08001264static inline bool cpu_has_vmx_vpid(void)
Sheng Yang2384d2b2008-01-17 15:14:33 +08001265{
Sheng Yang04547152009-04-01 15:52:31 +08001266 return vmcs_config.cpu_based_2nd_exec_ctrl &
1267 SECONDARY_EXEC_ENABLE_VPID;
Sheng Yang2384d2b2008-01-17 15:14:33 +08001268}
1269
Gui Jianfeng31299942010-03-15 17:29:09 +08001270static inline bool cpu_has_vmx_rdtscp(void)
Sheng Yang4e47c7a2009-12-18 16:48:47 +08001271{
1272 return vmcs_config.cpu_based_2nd_exec_ctrl &
1273 SECONDARY_EXEC_RDTSCP;
1274}
1275
Mao, Junjiead756a12012-07-02 01:18:48 +00001276static inline bool cpu_has_vmx_invpcid(void)
1277{
1278 return vmcs_config.cpu_based_2nd_exec_ctrl &
1279 SECONDARY_EXEC_ENABLE_INVPCID;
1280}
1281
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01001282static inline bool cpu_has_virtual_nmis(void)
1283{
1284 return vmcs_config.pin_based_exec_ctrl & PIN_BASED_VIRTUAL_NMIS;
1285}
1286
Sheng Yangf5f48ee2010-06-30 12:25:15 +08001287static inline bool cpu_has_vmx_wbinvd_exit(void)
1288{
1289 return vmcs_config.cpu_based_2nd_exec_ctrl &
1290 SECONDARY_EXEC_WBINVD_EXITING;
1291}
1292
Abel Gordonabc4fc52013-04-18 14:35:25 +03001293static inline bool cpu_has_vmx_shadow_vmcs(void)
1294{
1295 u64 vmx_msr;
1296 rdmsrl(MSR_IA32_VMX_MISC, vmx_msr);
1297 /* check if the cpu supports writing r/o exit information fields */
1298 if (!(vmx_msr & MSR_IA32_VMX_MISC_VMWRITE_SHADOW_RO_FIELDS))
1299 return false;
1300
1301 return vmcs_config.cpu_based_2nd_exec_ctrl &
1302 SECONDARY_EXEC_SHADOW_VMCS;
1303}
1304
Kai Huang843e4332015-01-28 10:54:28 +08001305static inline bool cpu_has_vmx_pml(void)
1306{
1307 return vmcs_config.cpu_based_2nd_exec_ctrl & SECONDARY_EXEC_ENABLE_PML;
1308}
1309
Haozhong Zhang64903d62015-10-20 15:39:09 +08001310static inline bool cpu_has_vmx_tsc_scaling(void)
1311{
1312 return vmcs_config.cpu_based_2nd_exec_ctrl &
1313 SECONDARY_EXEC_TSC_SCALING;
1314}
1315
Bandan Das2a499e42017-08-03 15:54:41 -04001316static inline bool cpu_has_vmx_vmfunc(void)
1317{
1318 return vmcs_config.cpu_based_2nd_exec_ctrl &
1319 SECONDARY_EXEC_ENABLE_VMFUNC;
1320}
1321
Sheng Yang04547152009-04-01 15:52:31 +08001322static inline bool report_flexpriority(void)
1323{
1324 return flexpriority_enabled;
1325}
1326
Jim Mattsonc7c2c7092017-05-05 11:28:09 -07001327static inline unsigned nested_cpu_vmx_misc_cr3_count(struct kvm_vcpu *vcpu)
1328{
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01001329 return vmx_misc_cr3_count(to_vmx(vcpu)->nested.msrs.misc_low);
Jim Mattsonc7c2c7092017-05-05 11:28:09 -07001330}
1331
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03001332static inline bool nested_cpu_has(struct vmcs12 *vmcs12, u32 bit)
1333{
1334 return vmcs12->cpu_based_vm_exec_control & bit;
1335}
1336
1337static inline bool nested_cpu_has2(struct vmcs12 *vmcs12, u32 bit)
1338{
1339 return (vmcs12->cpu_based_vm_exec_control &
1340 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) &&
1341 (vmcs12->secondary_vm_exec_control & bit);
1342}
1343
Jan Kiszkaf41245002014-03-07 20:03:13 +01001344static inline bool nested_cpu_has_preemption_timer(struct vmcs12 *vmcs12)
1345{
1346 return vmcs12->pin_based_vm_exec_control &
1347 PIN_BASED_VMX_PREEMPTION_TIMER;
1348}
1349
Krish Sadhukhan0c7f6502018-02-20 21:24:39 -05001350static inline bool nested_cpu_has_nmi_exiting(struct vmcs12 *vmcs12)
1351{
1352 return vmcs12->pin_based_vm_exec_control & PIN_BASED_NMI_EXITING;
1353}
1354
1355static inline bool nested_cpu_has_virtual_nmis(struct vmcs12 *vmcs12)
1356{
1357 return vmcs12->pin_based_vm_exec_control & PIN_BASED_VIRTUAL_NMIS;
1358}
1359
Nadav Har'El155a97a2013-08-05 11:07:16 +03001360static inline int nested_cpu_has_ept(struct vmcs12 *vmcs12)
1361{
1362 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_EPT);
1363}
1364
Wanpeng Li81dc01f2014-12-04 19:11:07 +08001365static inline bool nested_cpu_has_xsaves(struct vmcs12 *vmcs12)
1366{
Paolo Bonzini3db13482017-08-24 14:48:03 +02001367 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_XSAVES);
Wanpeng Li81dc01f2014-12-04 19:11:07 +08001368}
1369
Bandan Dasc5f983f2017-05-05 15:25:14 -04001370static inline bool nested_cpu_has_pml(struct vmcs12 *vmcs12)
1371{
1372 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_PML);
1373}
1374
Wincy Vanf2b93282015-02-03 23:56:03 +08001375static inline bool nested_cpu_has_virt_x2apic_mode(struct vmcs12 *vmcs12)
1376{
1377 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE);
1378}
1379
Wanpeng Li5c614b32015-10-13 09:18:36 -07001380static inline bool nested_cpu_has_vpid(struct vmcs12 *vmcs12)
1381{
1382 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_VPID);
1383}
1384
Wincy Van82f0dd42015-02-03 23:57:18 +08001385static inline bool nested_cpu_has_apic_reg_virt(struct vmcs12 *vmcs12)
1386{
1387 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_APIC_REGISTER_VIRT);
1388}
1389
Wincy Van608406e2015-02-03 23:57:51 +08001390static inline bool nested_cpu_has_vid(struct vmcs12 *vmcs12)
1391{
1392 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
1393}
1394
Wincy Van705699a2015-02-03 23:58:17 +08001395static inline bool nested_cpu_has_posted_intr(struct vmcs12 *vmcs12)
1396{
1397 return vmcs12->pin_based_vm_exec_control & PIN_BASED_POSTED_INTR;
1398}
1399
Bandan Das27c42a12017-08-03 15:54:42 -04001400static inline bool nested_cpu_has_vmfunc(struct vmcs12 *vmcs12)
1401{
1402 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_VMFUNC);
1403}
1404
Bandan Das41ab9372017-08-03 15:54:43 -04001405static inline bool nested_cpu_has_eptp_switching(struct vmcs12 *vmcs12)
1406{
1407 return nested_cpu_has_vmfunc(vmcs12) &&
1408 (vmcs12->vm_function_control &
1409 VMX_VMFUNC_EPTP_SWITCHING);
1410}
1411
Jim Mattsonef85b672016-12-12 11:01:37 -08001412static inline bool is_nmi(u32 intr_info)
Nadav Har'El644d7112011-05-25 23:12:35 +03001413{
1414 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK))
Jim Mattsonef85b672016-12-12 11:01:37 -08001415 == (INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK);
Nadav Har'El644d7112011-05-25 23:12:35 +03001416}
1417
Jan Kiszka533558b2014-01-04 18:47:20 +01001418static void nested_vmx_vmexit(struct kvm_vcpu *vcpu, u32 exit_reason,
1419 u32 exit_intr_info,
1420 unsigned long exit_qualification);
Nadav Har'El7c177932011-05-25 23:12:04 +03001421static void nested_vmx_entry_failure(struct kvm_vcpu *vcpu,
1422 struct vmcs12 *vmcs12,
1423 u32 reason, unsigned long qualification);
1424
Rusty Russell8b9cf982007-07-30 16:31:43 +10001425static int __find_msr_index(struct vcpu_vmx *vmx, u32 msr)
Avi Kivity7725f0b2006-12-13 00:34:01 -08001426{
1427 int i;
1428
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001429 for (i = 0; i < vmx->nmsrs; ++i)
Avi Kivity26bb0982009-09-07 11:14:12 +03001430 if (vmx_msr_index[vmx->guest_msrs[i].index] == msr)
Eddie Donga75beee2007-05-17 18:55:15 +03001431 return i;
1432 return -1;
1433}
1434
Sheng Yang2384d2b2008-01-17 15:14:33 +08001435static inline void __invvpid(int ext, u16 vpid, gva_t gva)
1436{
1437 struct {
1438 u64 vpid : 16;
1439 u64 rsvd : 48;
1440 u64 gva;
1441 } operand = { vpid, 0, gva };
1442
Avi Kivity4ecac3f2008-05-13 13:23:38 +03001443 asm volatile (__ex(ASM_VMX_INVVPID)
Sheng Yang2384d2b2008-01-17 15:14:33 +08001444 /* CF==1 or ZF==1 --> rc = -1 */
1445 "; ja 1f ; ud2 ; 1:"
1446 : : "a"(&operand), "c"(ext) : "cc", "memory");
1447}
1448
Sheng Yang14394422008-04-28 12:24:45 +08001449static inline void __invept(int ext, u64 eptp, gpa_t gpa)
1450{
1451 struct {
1452 u64 eptp, gpa;
1453 } operand = {eptp, gpa};
1454
Avi Kivity4ecac3f2008-05-13 13:23:38 +03001455 asm volatile (__ex(ASM_VMX_INVEPT)
Sheng Yang14394422008-04-28 12:24:45 +08001456 /* CF==1 or ZF==1 --> rc = -1 */
1457 "; ja 1f ; ud2 ; 1:\n"
1458 : : "a" (&operand), "c" (ext) : "cc", "memory");
1459}
1460
Avi Kivity26bb0982009-09-07 11:14:12 +03001461static struct shared_msr_entry *find_msr_entry(struct vcpu_vmx *vmx, u32 msr)
Eddie Donga75beee2007-05-17 18:55:15 +03001462{
1463 int i;
1464
Rusty Russell8b9cf982007-07-30 16:31:43 +10001465 i = __find_msr_index(vmx, msr);
Eddie Donga75beee2007-05-17 18:55:15 +03001466 if (i >= 0)
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001467 return &vmx->guest_msrs[i];
Al Viro8b6d44c2007-02-09 16:38:40 +00001468 return NULL;
Avi Kivity7725f0b2006-12-13 00:34:01 -08001469}
1470
Avi Kivity6aa8b732006-12-10 02:21:36 -08001471static void vmcs_clear(struct vmcs *vmcs)
1472{
1473 u64 phys_addr = __pa(vmcs);
1474 u8 error;
1475
Avi Kivity4ecac3f2008-05-13 13:23:38 +03001476 asm volatile (__ex(ASM_VMX_VMCLEAR_RAX) "; setna %0"
Avi Kivity16d8f722010-12-21 16:51:50 +02001477 : "=qm"(error) : "a"(&phys_addr), "m"(phys_addr)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001478 : "cc", "memory");
1479 if (error)
1480 printk(KERN_ERR "kvm: vmclear fail: %p/%llx\n",
1481 vmcs, phys_addr);
1482}
1483
Nadav Har'Eld462b812011-05-24 15:26:10 +03001484static inline void loaded_vmcs_init(struct loaded_vmcs *loaded_vmcs)
1485{
1486 vmcs_clear(loaded_vmcs->vmcs);
Jim Mattson355f4fb2016-10-28 08:29:39 -07001487 if (loaded_vmcs->shadow_vmcs && loaded_vmcs->launched)
1488 vmcs_clear(loaded_vmcs->shadow_vmcs);
Nadav Har'Eld462b812011-05-24 15:26:10 +03001489 loaded_vmcs->cpu = -1;
1490 loaded_vmcs->launched = 0;
1491}
1492
Dongxiao Xu7725b892010-05-11 18:29:38 +08001493static void vmcs_load(struct vmcs *vmcs)
1494{
1495 u64 phys_addr = __pa(vmcs);
1496 u8 error;
1497
1498 asm volatile (__ex(ASM_VMX_VMPTRLD_RAX) "; setna %0"
Avi Kivity16d8f722010-12-21 16:51:50 +02001499 : "=qm"(error) : "a"(&phys_addr), "m"(phys_addr)
Dongxiao Xu7725b892010-05-11 18:29:38 +08001500 : "cc", "memory");
1501 if (error)
Nadav Har'El2844d842011-05-25 23:16:40 +03001502 printk(KERN_ERR "kvm: vmptrld %p/%llx failed\n",
Dongxiao Xu7725b892010-05-11 18:29:38 +08001503 vmcs, phys_addr);
1504}
1505
Dave Young2965faa2015-09-09 15:38:55 -07001506#ifdef CONFIG_KEXEC_CORE
Zhang Yanfei8f536b72012-12-06 23:43:34 +08001507/*
1508 * This bitmap is used to indicate whether the vmclear
1509 * operation is enabled on all cpus. All disabled by
1510 * default.
1511 */
1512static cpumask_t crash_vmclear_enabled_bitmap = CPU_MASK_NONE;
1513
1514static inline void crash_enable_local_vmclear(int cpu)
1515{
1516 cpumask_set_cpu(cpu, &crash_vmclear_enabled_bitmap);
1517}
1518
1519static inline void crash_disable_local_vmclear(int cpu)
1520{
1521 cpumask_clear_cpu(cpu, &crash_vmclear_enabled_bitmap);
1522}
1523
1524static inline int crash_local_vmclear_enabled(int cpu)
1525{
1526 return cpumask_test_cpu(cpu, &crash_vmclear_enabled_bitmap);
1527}
1528
1529static void crash_vmclear_local_loaded_vmcss(void)
1530{
1531 int cpu = raw_smp_processor_id();
1532 struct loaded_vmcs *v;
1533
1534 if (!crash_local_vmclear_enabled(cpu))
1535 return;
1536
1537 list_for_each_entry(v, &per_cpu(loaded_vmcss_on_cpu, cpu),
1538 loaded_vmcss_on_cpu_link)
1539 vmcs_clear(v->vmcs);
1540}
1541#else
1542static inline void crash_enable_local_vmclear(int cpu) { }
1543static inline void crash_disable_local_vmclear(int cpu) { }
Dave Young2965faa2015-09-09 15:38:55 -07001544#endif /* CONFIG_KEXEC_CORE */
Zhang Yanfei8f536b72012-12-06 23:43:34 +08001545
Nadav Har'Eld462b812011-05-24 15:26:10 +03001546static void __loaded_vmcs_clear(void *arg)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001547{
Nadav Har'Eld462b812011-05-24 15:26:10 +03001548 struct loaded_vmcs *loaded_vmcs = arg;
Ingo Molnard3b2c332007-01-05 16:36:23 -08001549 int cpu = raw_smp_processor_id();
Avi Kivity6aa8b732006-12-10 02:21:36 -08001550
Nadav Har'Eld462b812011-05-24 15:26:10 +03001551 if (loaded_vmcs->cpu != cpu)
1552 return; /* vcpu migration can race with cpu offline */
1553 if (per_cpu(current_vmcs, cpu) == loaded_vmcs->vmcs)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001554 per_cpu(current_vmcs, cpu) = NULL;
Zhang Yanfei8f536b72012-12-06 23:43:34 +08001555 crash_disable_local_vmclear(cpu);
Nadav Har'Eld462b812011-05-24 15:26:10 +03001556 list_del(&loaded_vmcs->loaded_vmcss_on_cpu_link);
Xiao Guangrong5a560f82012-11-28 20:54:14 +08001557
1558 /*
1559 * we should ensure updating loaded_vmcs->loaded_vmcss_on_cpu_link
1560 * is before setting loaded_vmcs->vcpu to -1 which is done in
1561 * loaded_vmcs_init. Otherwise, other cpu can see vcpu = -1 fist
1562 * then adds the vmcs into percpu list before it is deleted.
1563 */
1564 smp_wmb();
1565
Nadav Har'Eld462b812011-05-24 15:26:10 +03001566 loaded_vmcs_init(loaded_vmcs);
Zhang Yanfei8f536b72012-12-06 23:43:34 +08001567 crash_enable_local_vmclear(cpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001568}
1569
Nadav Har'Eld462b812011-05-24 15:26:10 +03001570static void loaded_vmcs_clear(struct loaded_vmcs *loaded_vmcs)
Avi Kivity8d0be2b2007-02-12 00:54:46 -08001571{
Xiao Guangronge6c7d322012-11-28 20:53:15 +08001572 int cpu = loaded_vmcs->cpu;
1573
1574 if (cpu != -1)
1575 smp_call_function_single(cpu,
1576 __loaded_vmcs_clear, loaded_vmcs, 1);
Avi Kivity8d0be2b2007-02-12 00:54:46 -08001577}
1578
Wanpeng Lidd5f5342015-09-23 18:26:57 +08001579static inline void vpid_sync_vcpu_single(int vpid)
Sheng Yang2384d2b2008-01-17 15:14:33 +08001580{
Wanpeng Lidd5f5342015-09-23 18:26:57 +08001581 if (vpid == 0)
Sheng Yang2384d2b2008-01-17 15:14:33 +08001582 return;
1583
Gui Jianfeng518c8ae2010-06-04 08:51:39 +08001584 if (cpu_has_vmx_invvpid_single())
Wanpeng Lidd5f5342015-09-23 18:26:57 +08001585 __invvpid(VMX_VPID_EXTENT_SINGLE_CONTEXT, vpid, 0);
Sheng Yang2384d2b2008-01-17 15:14:33 +08001586}
1587
Gui Jianfengb9d762f2010-06-07 10:32:29 +08001588static inline void vpid_sync_vcpu_global(void)
1589{
1590 if (cpu_has_vmx_invvpid_global())
1591 __invvpid(VMX_VPID_EXTENT_ALL_CONTEXT, 0, 0);
1592}
1593
Wanpeng Lidd5f5342015-09-23 18:26:57 +08001594static inline void vpid_sync_context(int vpid)
Gui Jianfengb9d762f2010-06-07 10:32:29 +08001595{
1596 if (cpu_has_vmx_invvpid_single())
Wanpeng Lidd5f5342015-09-23 18:26:57 +08001597 vpid_sync_vcpu_single(vpid);
Gui Jianfengb9d762f2010-06-07 10:32:29 +08001598 else
1599 vpid_sync_vcpu_global();
1600}
1601
Sheng Yang14394422008-04-28 12:24:45 +08001602static inline void ept_sync_global(void)
1603{
David Hildenbrandf5f51582017-08-24 20:51:30 +02001604 __invept(VMX_EPT_EXTENT_GLOBAL, 0, 0);
Sheng Yang14394422008-04-28 12:24:45 +08001605}
1606
1607static inline void ept_sync_context(u64 eptp)
1608{
David Hildenbrand0e1252d2017-08-24 20:51:28 +02001609 if (cpu_has_vmx_invept_context())
1610 __invept(VMX_EPT_EXTENT_CONTEXT, eptp, 0);
1611 else
1612 ept_sync_global();
Sheng Yang14394422008-04-28 12:24:45 +08001613}
1614
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001615static __always_inline void vmcs_check16(unsigned long field)
1616{
1617 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2000,
1618 "16-bit accessor invalid for 64-bit field");
1619 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2001,
1620 "16-bit accessor invalid for 64-bit high field");
1621 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x4000,
1622 "16-bit accessor invalid for 32-bit high field");
1623 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x6000,
1624 "16-bit accessor invalid for natural width field");
1625}
1626
1627static __always_inline void vmcs_check32(unsigned long field)
1628{
1629 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0,
1630 "32-bit accessor invalid for 16-bit field");
1631 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x6000,
1632 "32-bit accessor invalid for natural width field");
1633}
1634
1635static __always_inline void vmcs_check64(unsigned long field)
1636{
1637 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0,
1638 "64-bit accessor invalid for 16-bit field");
1639 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2001,
1640 "64-bit accessor invalid for 64-bit high field");
1641 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x4000,
1642 "64-bit accessor invalid for 32-bit field");
1643 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x6000,
1644 "64-bit accessor invalid for natural width field");
1645}
1646
1647static __always_inline void vmcs_checkl(unsigned long field)
1648{
1649 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0,
1650 "Natural width accessor invalid for 16-bit field");
1651 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2000,
1652 "Natural width accessor invalid for 64-bit field");
1653 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2001,
1654 "Natural width accessor invalid for 64-bit high field");
1655 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x4000,
1656 "Natural width accessor invalid for 32-bit field");
1657}
1658
1659static __always_inline unsigned long __vmcs_readl(unsigned long field)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001660{
Avi Kivity5e520e62011-05-15 10:13:12 -04001661 unsigned long value;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001662
Avi Kivity5e520e62011-05-15 10:13:12 -04001663 asm volatile (__ex_clear(ASM_VMX_VMREAD_RDX_RAX, "%0")
1664 : "=a"(value) : "d"(field) : "cc");
Avi Kivity6aa8b732006-12-10 02:21:36 -08001665 return value;
1666}
1667
Avi Kivity96304212011-05-15 10:13:13 -04001668static __always_inline u16 vmcs_read16(unsigned long field)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001669{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001670 vmcs_check16(field);
1671 return __vmcs_readl(field);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001672}
1673
Avi Kivity96304212011-05-15 10:13:13 -04001674static __always_inline u32 vmcs_read32(unsigned long field)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001675{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001676 vmcs_check32(field);
1677 return __vmcs_readl(field);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001678}
1679
Avi Kivity96304212011-05-15 10:13:13 -04001680static __always_inline u64 vmcs_read64(unsigned long field)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001681{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001682 vmcs_check64(field);
Avi Kivity05b3e0c2006-12-13 00:33:45 -08001683#ifdef CONFIG_X86_64
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001684 return __vmcs_readl(field);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001685#else
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001686 return __vmcs_readl(field) | ((u64)__vmcs_readl(field+1) << 32);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001687#endif
1688}
1689
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001690static __always_inline unsigned long vmcs_readl(unsigned long field)
1691{
1692 vmcs_checkl(field);
1693 return __vmcs_readl(field);
1694}
1695
Avi Kivitye52de1b2007-01-05 16:36:56 -08001696static noinline void vmwrite_error(unsigned long field, unsigned long value)
1697{
1698 printk(KERN_ERR "vmwrite error: reg %lx value %lx (err %d)\n",
1699 field, value, vmcs_read32(VM_INSTRUCTION_ERROR));
1700 dump_stack();
1701}
1702
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001703static __always_inline void __vmcs_writel(unsigned long field, unsigned long value)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001704{
1705 u8 error;
1706
Avi Kivity4ecac3f2008-05-13 13:23:38 +03001707 asm volatile (__ex(ASM_VMX_VMWRITE_RAX_RDX) "; setna %0"
Mike Dayd77c26f2007-10-08 09:02:08 -04001708 : "=q"(error) : "a"(value), "d"(field) : "cc");
Avi Kivitye52de1b2007-01-05 16:36:56 -08001709 if (unlikely(error))
1710 vmwrite_error(field, value);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001711}
1712
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001713static __always_inline void vmcs_write16(unsigned long field, u16 value)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001714{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001715 vmcs_check16(field);
1716 __vmcs_writel(field, value);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001717}
1718
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001719static __always_inline void vmcs_write32(unsigned long field, u32 value)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001720{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001721 vmcs_check32(field);
1722 __vmcs_writel(field, value);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001723}
1724
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001725static __always_inline void vmcs_write64(unsigned long field, u64 value)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001726{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001727 vmcs_check64(field);
1728 __vmcs_writel(field, value);
Avi Kivity7682f2d2008-05-12 19:25:43 +03001729#ifndef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08001730 asm volatile ("");
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001731 __vmcs_writel(field+1, value >> 32);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001732#endif
1733}
1734
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001735static __always_inline void vmcs_writel(unsigned long field, unsigned long value)
Anthony Liguori2ab455c2007-04-27 09:29:49 +03001736{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001737 vmcs_checkl(field);
1738 __vmcs_writel(field, value);
Anthony Liguori2ab455c2007-04-27 09:29:49 +03001739}
1740
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001741static __always_inline void vmcs_clear_bits(unsigned long field, u32 mask)
Anthony Liguori2ab455c2007-04-27 09:29:49 +03001742{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001743 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x2000,
1744 "vmcs_clear_bits does not support 64-bit fields");
1745 __vmcs_writel(field, __vmcs_readl(field) & ~mask);
1746}
1747
1748static __always_inline void vmcs_set_bits(unsigned long field, u32 mask)
1749{
1750 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x2000,
1751 "vmcs_set_bits does not support 64-bit fields");
1752 __vmcs_writel(field, __vmcs_readl(field) | mask);
Anthony Liguori2ab455c2007-04-27 09:29:49 +03001753}
1754
Paolo Bonzini8391ce42016-07-07 14:58:33 +02001755static inline void vm_entry_controls_reset_shadow(struct vcpu_vmx *vmx)
1756{
1757 vmx->vm_entry_controls_shadow = vmcs_read32(VM_ENTRY_CONTROLS);
1758}
1759
Gleb Natapov2961e8762013-11-25 15:37:13 +02001760static inline void vm_entry_controls_init(struct vcpu_vmx *vmx, u32 val)
1761{
1762 vmcs_write32(VM_ENTRY_CONTROLS, val);
1763 vmx->vm_entry_controls_shadow = val;
1764}
1765
1766static inline void vm_entry_controls_set(struct vcpu_vmx *vmx, u32 val)
1767{
1768 if (vmx->vm_entry_controls_shadow != val)
1769 vm_entry_controls_init(vmx, val);
1770}
1771
1772static inline u32 vm_entry_controls_get(struct vcpu_vmx *vmx)
1773{
1774 return vmx->vm_entry_controls_shadow;
1775}
1776
1777
1778static inline void vm_entry_controls_setbit(struct vcpu_vmx *vmx, u32 val)
1779{
1780 vm_entry_controls_set(vmx, vm_entry_controls_get(vmx) | val);
1781}
1782
1783static inline void vm_entry_controls_clearbit(struct vcpu_vmx *vmx, u32 val)
1784{
1785 vm_entry_controls_set(vmx, vm_entry_controls_get(vmx) & ~val);
1786}
1787
Paolo Bonzini8391ce42016-07-07 14:58:33 +02001788static inline void vm_exit_controls_reset_shadow(struct vcpu_vmx *vmx)
1789{
1790 vmx->vm_exit_controls_shadow = vmcs_read32(VM_EXIT_CONTROLS);
1791}
1792
Gleb Natapov2961e8762013-11-25 15:37:13 +02001793static inline void vm_exit_controls_init(struct vcpu_vmx *vmx, u32 val)
1794{
1795 vmcs_write32(VM_EXIT_CONTROLS, val);
1796 vmx->vm_exit_controls_shadow = val;
1797}
1798
1799static inline void vm_exit_controls_set(struct vcpu_vmx *vmx, u32 val)
1800{
1801 if (vmx->vm_exit_controls_shadow != val)
1802 vm_exit_controls_init(vmx, val);
1803}
1804
1805static inline u32 vm_exit_controls_get(struct vcpu_vmx *vmx)
1806{
1807 return vmx->vm_exit_controls_shadow;
1808}
1809
1810
1811static inline void vm_exit_controls_setbit(struct vcpu_vmx *vmx, u32 val)
1812{
1813 vm_exit_controls_set(vmx, vm_exit_controls_get(vmx) | val);
1814}
1815
1816static inline void vm_exit_controls_clearbit(struct vcpu_vmx *vmx, u32 val)
1817{
1818 vm_exit_controls_set(vmx, vm_exit_controls_get(vmx) & ~val);
1819}
1820
Avi Kivity2fb92db2011-04-27 19:42:18 +03001821static void vmx_segment_cache_clear(struct vcpu_vmx *vmx)
1822{
1823 vmx->segment_cache.bitmask = 0;
1824}
1825
1826static bool vmx_segment_cache_test_set(struct vcpu_vmx *vmx, unsigned seg,
1827 unsigned field)
1828{
1829 bool ret;
1830 u32 mask = 1 << (seg * SEG_FIELD_NR + field);
1831
1832 if (!(vmx->vcpu.arch.regs_avail & (1 << VCPU_EXREG_SEGMENTS))) {
1833 vmx->vcpu.arch.regs_avail |= (1 << VCPU_EXREG_SEGMENTS);
1834 vmx->segment_cache.bitmask = 0;
1835 }
1836 ret = vmx->segment_cache.bitmask & mask;
1837 vmx->segment_cache.bitmask |= mask;
1838 return ret;
1839}
1840
1841static u16 vmx_read_guest_seg_selector(struct vcpu_vmx *vmx, unsigned seg)
1842{
1843 u16 *p = &vmx->segment_cache.seg[seg].selector;
1844
1845 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_SEL))
1846 *p = vmcs_read16(kvm_vmx_segment_fields[seg].selector);
1847 return *p;
1848}
1849
1850static ulong vmx_read_guest_seg_base(struct vcpu_vmx *vmx, unsigned seg)
1851{
1852 ulong *p = &vmx->segment_cache.seg[seg].base;
1853
1854 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_BASE))
1855 *p = vmcs_readl(kvm_vmx_segment_fields[seg].base);
1856 return *p;
1857}
1858
1859static u32 vmx_read_guest_seg_limit(struct vcpu_vmx *vmx, unsigned seg)
1860{
1861 u32 *p = &vmx->segment_cache.seg[seg].limit;
1862
1863 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_LIMIT))
1864 *p = vmcs_read32(kvm_vmx_segment_fields[seg].limit);
1865 return *p;
1866}
1867
1868static u32 vmx_read_guest_seg_ar(struct vcpu_vmx *vmx, unsigned seg)
1869{
1870 u32 *p = &vmx->segment_cache.seg[seg].ar;
1871
1872 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_AR))
1873 *p = vmcs_read32(kvm_vmx_segment_fields[seg].ar_bytes);
1874 return *p;
1875}
1876
Avi Kivityabd3f2d2007-05-02 17:57:40 +03001877static void update_exception_bitmap(struct kvm_vcpu *vcpu)
1878{
1879 u32 eb;
1880
Jan Kiszkafd7373c2010-01-20 18:20:20 +01001881 eb = (1u << PF_VECTOR) | (1u << UD_VECTOR) | (1u << MC_VECTOR) |
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -08001882 (1u << DB_VECTOR) | (1u << AC_VECTOR);
Liran Alon9e869482018-03-12 13:12:51 +02001883 /*
1884 * Guest access to VMware backdoor ports could legitimately
1885 * trigger #GP because of TSS I/O permission bitmap.
1886 * We intercept those #GP and allow access to them anyway
1887 * as VMware does.
1888 */
1889 if (enable_vmware_backdoor)
1890 eb |= (1u << GP_VECTOR);
Jan Kiszkafd7373c2010-01-20 18:20:20 +01001891 if ((vcpu->guest_debug &
1892 (KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_SW_BP)) ==
1893 (KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_SW_BP))
1894 eb |= 1u << BP_VECTOR;
Avi Kivity7ffd92c2009-06-09 14:10:45 +03001895 if (to_vmx(vcpu)->rmode.vm86_active)
Avi Kivityabd3f2d2007-05-02 17:57:40 +03001896 eb = ~0;
Avi Kivity089d0342009-03-23 18:26:32 +02001897 if (enable_ept)
Sheng Yang14394422008-04-28 12:24:45 +08001898 eb &= ~(1u << PF_VECTOR); /* bypass_guest_pf = 0 */
Nadav Har'El36cf24e2011-05-25 23:15:08 +03001899
1900 /* When we are running a nested L2 guest and L1 specified for it a
1901 * certain exception bitmap, we must trap the same exceptions and pass
1902 * them to L1. When running L2, we will only handle the exceptions
1903 * specified above if L1 did not want them.
1904 */
1905 if (is_guest_mode(vcpu))
1906 eb |= get_vmcs12(vcpu)->exception_bitmap;
1907
Avi Kivityabd3f2d2007-05-02 17:57:40 +03001908 vmcs_write32(EXCEPTION_BITMAP, eb);
1909}
1910
Ashok Raj15d45072018-02-01 22:59:43 +01001911/*
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01001912 * Check if MSR is intercepted for currently loaded MSR bitmap.
1913 */
1914static bool msr_write_intercepted(struct kvm_vcpu *vcpu, u32 msr)
1915{
1916 unsigned long *msr_bitmap;
1917 int f = sizeof(unsigned long);
1918
1919 if (!cpu_has_vmx_msr_bitmap())
1920 return true;
1921
1922 msr_bitmap = to_vmx(vcpu)->loaded_vmcs->msr_bitmap;
1923
1924 if (msr <= 0x1fff) {
1925 return !!test_bit(msr, msr_bitmap + 0x800 / f);
1926 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
1927 msr &= 0x1fff;
1928 return !!test_bit(msr, msr_bitmap + 0xc00 / f);
1929 }
1930
1931 return true;
1932}
1933
1934/*
Ashok Raj15d45072018-02-01 22:59:43 +01001935 * Check if MSR is intercepted for L01 MSR bitmap.
1936 */
1937static bool msr_write_intercepted_l01(struct kvm_vcpu *vcpu, u32 msr)
1938{
1939 unsigned long *msr_bitmap;
1940 int f = sizeof(unsigned long);
1941
1942 if (!cpu_has_vmx_msr_bitmap())
1943 return true;
1944
1945 msr_bitmap = to_vmx(vcpu)->vmcs01.msr_bitmap;
1946
1947 if (msr <= 0x1fff) {
1948 return !!test_bit(msr, msr_bitmap + 0x800 / f);
1949 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
1950 msr &= 0x1fff;
1951 return !!test_bit(msr, msr_bitmap + 0xc00 / f);
1952 }
1953
1954 return true;
1955}
1956
Gleb Natapov2961e8762013-11-25 15:37:13 +02001957static void clear_atomic_switch_msr_special(struct vcpu_vmx *vmx,
1958 unsigned long entry, unsigned long exit)
Gleb Natapov8bf00a52011-10-05 14:01:22 +02001959{
Gleb Natapov2961e8762013-11-25 15:37:13 +02001960 vm_entry_controls_clearbit(vmx, entry);
1961 vm_exit_controls_clearbit(vmx, exit);
Gleb Natapov8bf00a52011-10-05 14:01:22 +02001962}
1963
Avi Kivity61d2ef22010-04-28 16:40:38 +03001964static void clear_atomic_switch_msr(struct vcpu_vmx *vmx, unsigned msr)
1965{
1966 unsigned i;
1967 struct msr_autoload *m = &vmx->msr_autoload;
1968
Gleb Natapov8bf00a52011-10-05 14:01:22 +02001969 switch (msr) {
1970 case MSR_EFER:
1971 if (cpu_has_load_ia32_efer) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02001972 clear_atomic_switch_msr_special(vmx,
1973 VM_ENTRY_LOAD_IA32_EFER,
Gleb Natapov8bf00a52011-10-05 14:01:22 +02001974 VM_EXIT_LOAD_IA32_EFER);
1975 return;
1976 }
1977 break;
1978 case MSR_CORE_PERF_GLOBAL_CTRL:
1979 if (cpu_has_load_perf_global_ctrl) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02001980 clear_atomic_switch_msr_special(vmx,
Gleb Natapov8bf00a52011-10-05 14:01:22 +02001981 VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL,
1982 VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL);
1983 return;
1984 }
1985 break;
Avi Kivity110312c2010-12-21 12:54:20 +02001986 }
1987
Avi Kivity61d2ef22010-04-28 16:40:38 +03001988 for (i = 0; i < m->nr; ++i)
1989 if (m->guest[i].index == msr)
1990 break;
1991
1992 if (i == m->nr)
1993 return;
1994 --m->nr;
1995 m->guest[i] = m->guest[m->nr];
1996 m->host[i] = m->host[m->nr];
1997 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, m->nr);
1998 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, m->nr);
1999}
2000
Gleb Natapov2961e8762013-11-25 15:37:13 +02002001static void add_atomic_switch_msr_special(struct vcpu_vmx *vmx,
2002 unsigned long entry, unsigned long exit,
2003 unsigned long guest_val_vmcs, unsigned long host_val_vmcs,
2004 u64 guest_val, u64 host_val)
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002005{
2006 vmcs_write64(guest_val_vmcs, guest_val);
2007 vmcs_write64(host_val_vmcs, host_val);
Gleb Natapov2961e8762013-11-25 15:37:13 +02002008 vm_entry_controls_setbit(vmx, entry);
2009 vm_exit_controls_setbit(vmx, exit);
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002010}
2011
Avi Kivity61d2ef22010-04-28 16:40:38 +03002012static void add_atomic_switch_msr(struct vcpu_vmx *vmx, unsigned msr,
2013 u64 guest_val, u64 host_val)
2014{
2015 unsigned i;
2016 struct msr_autoload *m = &vmx->msr_autoload;
2017
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002018 switch (msr) {
2019 case MSR_EFER:
2020 if (cpu_has_load_ia32_efer) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02002021 add_atomic_switch_msr_special(vmx,
2022 VM_ENTRY_LOAD_IA32_EFER,
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002023 VM_EXIT_LOAD_IA32_EFER,
2024 GUEST_IA32_EFER,
2025 HOST_IA32_EFER,
2026 guest_val, host_val);
2027 return;
2028 }
2029 break;
2030 case MSR_CORE_PERF_GLOBAL_CTRL:
2031 if (cpu_has_load_perf_global_ctrl) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02002032 add_atomic_switch_msr_special(vmx,
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002033 VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL,
2034 VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL,
2035 GUEST_IA32_PERF_GLOBAL_CTRL,
2036 HOST_IA32_PERF_GLOBAL_CTRL,
2037 guest_val, host_val);
2038 return;
2039 }
2040 break;
Radim Krčmář7099e2e2016-03-04 15:08:42 +01002041 case MSR_IA32_PEBS_ENABLE:
2042 /* PEBS needs a quiescent period after being disabled (to write
2043 * a record). Disabling PEBS through VMX MSR swapping doesn't
2044 * provide that period, so a CPU could write host's record into
2045 * guest's memory.
2046 */
2047 wrmsrl(MSR_IA32_PEBS_ENABLE, 0);
Avi Kivity110312c2010-12-21 12:54:20 +02002048 }
2049
Avi Kivity61d2ef22010-04-28 16:40:38 +03002050 for (i = 0; i < m->nr; ++i)
2051 if (m->guest[i].index == msr)
2052 break;
2053
Gleb Natapove7fc6f93b2011-10-05 14:01:24 +02002054 if (i == NR_AUTOLOAD_MSRS) {
Michael S. Tsirkin60266202013-10-31 00:34:56 +02002055 printk_once(KERN_WARNING "Not enough msr switch entries. "
Gleb Natapove7fc6f93b2011-10-05 14:01:24 +02002056 "Can't add msr %x\n", msr);
2057 return;
2058 } else if (i == m->nr) {
Avi Kivity61d2ef22010-04-28 16:40:38 +03002059 ++m->nr;
2060 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, m->nr);
2061 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, m->nr);
2062 }
2063
2064 m->guest[i].index = msr;
2065 m->guest[i].value = guest_val;
2066 m->host[i].index = msr;
2067 m->host[i].value = host_val;
2068}
2069
Avi Kivity92c0d902009-10-29 11:00:16 +02002070static bool update_transition_efer(struct vcpu_vmx *vmx, int efer_offset)
Eddie Dong2cc51562007-05-21 07:28:09 +03002071{
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002072 u64 guest_efer = vmx->vcpu.arch.efer;
2073 u64 ignore_bits = 0;
Eddie Dong2cc51562007-05-21 07:28:09 +03002074
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002075 if (!enable_ept) {
2076 /*
2077 * NX is needed to handle CR0.WP=1, CR4.SMEP=1. Testing
2078 * host CPUID is more efficient than testing guest CPUID
2079 * or CR4. Host SMEP is anyway a requirement for guest SMEP.
2080 */
2081 if (boot_cpu_has(X86_FEATURE_SMEP))
2082 guest_efer |= EFER_NX;
2083 else if (!(guest_efer & EFER_NX))
2084 ignore_bits |= EFER_NX;
2085 }
Roel Kluin3a34a882009-08-04 02:08:45 -07002086
Avi Kivity51c6cf62007-08-29 03:48:05 +03002087 /*
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002088 * LMA and LME handled by hardware; SCE meaningless outside long mode.
Avi Kivity51c6cf62007-08-29 03:48:05 +03002089 */
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002090 ignore_bits |= EFER_SCE;
Avi Kivity51c6cf62007-08-29 03:48:05 +03002091#ifdef CONFIG_X86_64
2092 ignore_bits |= EFER_LMA | EFER_LME;
2093 /* SCE is meaningful only in long mode on Intel */
2094 if (guest_efer & EFER_LMA)
2095 ignore_bits &= ~(u64)EFER_SCE;
2096#endif
Avi Kivity84ad33e2010-04-28 16:42:29 +03002097
2098 clear_atomic_switch_msr(vmx, MSR_EFER);
Andy Lutomirskif6577a5f2014-11-07 18:25:18 -08002099
2100 /*
2101 * On EPT, we can't emulate NX, so we must switch EFER atomically.
2102 * On CPUs that support "load IA32_EFER", always switch EFER
2103 * atomically, since it's faster than switching it manually.
2104 */
2105 if (cpu_has_load_ia32_efer ||
2106 (enable_ept && ((vmx->vcpu.arch.efer ^ host_efer) & EFER_NX))) {
Avi Kivity84ad33e2010-04-28 16:42:29 +03002107 if (!(guest_efer & EFER_LMA))
2108 guest_efer &= ~EFER_LME;
Andy Lutomirski54b98bf2014-11-10 11:19:15 -08002109 if (guest_efer != host_efer)
2110 add_atomic_switch_msr(vmx, MSR_EFER,
2111 guest_efer, host_efer);
Avi Kivity84ad33e2010-04-28 16:42:29 +03002112 return false;
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002113 } else {
2114 guest_efer &= ~ignore_bits;
2115 guest_efer |= host_efer & ignore_bits;
Avi Kivity84ad33e2010-04-28 16:42:29 +03002116
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002117 vmx->guest_msrs[efer_offset].data = guest_efer;
2118 vmx->guest_msrs[efer_offset].mask = ~ignore_bits;
2119
2120 return true;
2121 }
Avi Kivity51c6cf62007-08-29 03:48:05 +03002122}
2123
Andy Lutomirskie28baea2017-02-20 08:56:11 -08002124#ifdef CONFIG_X86_32
2125/*
2126 * On 32-bit kernels, VM exits still load the FS and GS bases from the
2127 * VMCS rather than the segment table. KVM uses this helper to figure
2128 * out the current bases to poke them into the VMCS before entry.
2129 */
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002130static unsigned long segment_base(u16 selector)
2131{
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08002132 struct desc_struct *table;
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002133 unsigned long v;
2134
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08002135 if (!(selector & ~SEGMENT_RPL_MASK))
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002136 return 0;
2137
Thomas Garnier45fc8752017-03-14 10:05:08 -07002138 table = get_current_gdt_ro();
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002139
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08002140 if ((selector & SEGMENT_TI_MASK) == SEGMENT_LDT) {
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002141 u16 ldt_selector = kvm_read_ldt();
2142
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08002143 if (!(ldt_selector & ~SEGMENT_RPL_MASK))
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002144 return 0;
2145
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08002146 table = (struct desc_struct *)segment_base(ldt_selector);
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002147 }
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08002148 v = get_desc_base(&table[selector >> 3]);
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002149 return v;
2150}
Andy Lutomirskie28baea2017-02-20 08:56:11 -08002151#endif
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002152
Avi Kivity04d2cc72007-09-10 18:10:54 +03002153static void vmx_save_host_state(struct kvm_vcpu *vcpu)
Avi Kivity33ed6322007-05-02 16:54:03 +03002154{
Avi Kivity04d2cc72007-09-10 18:10:54 +03002155 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity26bb0982009-09-07 11:14:12 +03002156 int i;
Avi Kivity04d2cc72007-09-10 18:10:54 +03002157
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002158 if (vmx->host_state.loaded)
Avi Kivity33ed6322007-05-02 16:54:03 +03002159 return;
2160
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002161 vmx->host_state.loaded = 1;
Avi Kivity33ed6322007-05-02 16:54:03 +03002162 /*
2163 * Set host fs and gs selectors. Unfortunately, 22.2.3 does not
2164 * allow segment selectors with cpl > 0 or ti == 1.
2165 */
Avi Kivityd6e88ae2008-07-10 16:53:33 +03002166 vmx->host_state.ldt_sel = kvm_read_ldt();
Laurent Vivier152d3f22007-08-23 16:33:11 +02002167 vmx->host_state.gs_ldt_reload_needed = vmx->host_state.ldt_sel;
Avi Kivity9581d442010-10-19 16:46:55 +02002168 savesegment(fs, vmx->host_state.fs_sel);
Laurent Vivier152d3f22007-08-23 16:33:11 +02002169 if (!(vmx->host_state.fs_sel & 7)) {
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002170 vmcs_write16(HOST_FS_SELECTOR, vmx->host_state.fs_sel);
Laurent Vivier152d3f22007-08-23 16:33:11 +02002171 vmx->host_state.fs_reload_needed = 0;
2172 } else {
Avi Kivity33ed6322007-05-02 16:54:03 +03002173 vmcs_write16(HOST_FS_SELECTOR, 0);
Laurent Vivier152d3f22007-08-23 16:33:11 +02002174 vmx->host_state.fs_reload_needed = 1;
Avi Kivity33ed6322007-05-02 16:54:03 +03002175 }
Avi Kivity9581d442010-10-19 16:46:55 +02002176 savesegment(gs, vmx->host_state.gs_sel);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002177 if (!(vmx->host_state.gs_sel & 7))
2178 vmcs_write16(HOST_GS_SELECTOR, vmx->host_state.gs_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +03002179 else {
2180 vmcs_write16(HOST_GS_SELECTOR, 0);
Laurent Vivier152d3f22007-08-23 16:33:11 +02002181 vmx->host_state.gs_ldt_reload_needed = 1;
Avi Kivity33ed6322007-05-02 16:54:03 +03002182 }
2183
2184#ifdef CONFIG_X86_64
Avi Kivityb2da15a2012-05-13 19:53:24 +03002185 savesegment(ds, vmx->host_state.ds_sel);
2186 savesegment(es, vmx->host_state.es_sel);
2187#endif
2188
2189#ifdef CONFIG_X86_64
Avi Kivity33ed6322007-05-02 16:54:03 +03002190 vmcs_writel(HOST_FS_BASE, read_msr(MSR_FS_BASE));
2191 vmcs_writel(HOST_GS_BASE, read_msr(MSR_GS_BASE));
2192#else
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002193 vmcs_writel(HOST_FS_BASE, segment_base(vmx->host_state.fs_sel));
2194 vmcs_writel(HOST_GS_BASE, segment_base(vmx->host_state.gs_sel));
Avi Kivity33ed6322007-05-02 16:54:03 +03002195#endif
Avi Kivity707c0872007-05-02 17:33:43 +03002196
2197#ifdef CONFIG_X86_64
Avi Kivityc8770e72010-11-11 12:37:26 +02002198 rdmsrl(MSR_KERNEL_GS_BASE, vmx->msr_host_kernel_gs_base);
2199 if (is_long_mode(&vmx->vcpu))
Avi Kivity44ea2b12009-09-06 15:55:37 +03002200 wrmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base);
Avi Kivity707c0872007-05-02 17:33:43 +03002201#endif
Liu, Jinsongda8999d2014-02-24 10:55:46 +00002202 if (boot_cpu_has(X86_FEATURE_MPX))
2203 rdmsrl(MSR_IA32_BNDCFGS, vmx->host_state.msr_host_bndcfgs);
Avi Kivity26bb0982009-09-07 11:14:12 +03002204 for (i = 0; i < vmx->save_nmsrs; ++i)
2205 kvm_set_shared_msr(vmx->guest_msrs[i].index,
Avi Kivityd5696722009-12-02 12:28:47 +02002206 vmx->guest_msrs[i].data,
2207 vmx->guest_msrs[i].mask);
Avi Kivity33ed6322007-05-02 16:54:03 +03002208}
2209
Avi Kivitya9b21b62008-06-24 11:48:49 +03002210static void __vmx_load_host_state(struct vcpu_vmx *vmx)
Avi Kivity33ed6322007-05-02 16:54:03 +03002211{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002212 if (!vmx->host_state.loaded)
Avi Kivity33ed6322007-05-02 16:54:03 +03002213 return;
2214
Avi Kivitye1beb1d2007-11-18 13:50:24 +02002215 ++vmx->vcpu.stat.host_state_reload;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002216 vmx->host_state.loaded = 0;
Avi Kivityc8770e72010-11-11 12:37:26 +02002217#ifdef CONFIG_X86_64
2218 if (is_long_mode(&vmx->vcpu))
2219 rdmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base);
2220#endif
Laurent Vivier152d3f22007-08-23 16:33:11 +02002221 if (vmx->host_state.gs_ldt_reload_needed) {
Avi Kivityd6e88ae2008-07-10 16:53:33 +03002222 kvm_load_ldt(vmx->host_state.ldt_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +03002223#ifdef CONFIG_X86_64
Avi Kivity9581d442010-10-19 16:46:55 +02002224 load_gs_index(vmx->host_state.gs_sel);
Avi Kivity9581d442010-10-19 16:46:55 +02002225#else
2226 loadsegment(gs, vmx->host_state.gs_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +03002227#endif
Avi Kivity33ed6322007-05-02 16:54:03 +03002228 }
Avi Kivity0a77fe42010-10-19 18:48:35 +02002229 if (vmx->host_state.fs_reload_needed)
2230 loadsegment(fs, vmx->host_state.fs_sel);
Avi Kivityb2da15a2012-05-13 19:53:24 +03002231#ifdef CONFIG_X86_64
2232 if (unlikely(vmx->host_state.ds_sel | vmx->host_state.es_sel)) {
2233 loadsegment(ds, vmx->host_state.ds_sel);
2234 loadsegment(es, vmx->host_state.es_sel);
2235 }
Avi Kivityb2da15a2012-05-13 19:53:24 +03002236#endif
Andy Lutomirskib7ffc442017-02-20 08:56:14 -08002237 invalidate_tss_limit();
Avi Kivity44ea2b12009-09-06 15:55:37 +03002238#ifdef CONFIG_X86_64
Avi Kivityc8770e72010-11-11 12:37:26 +02002239 wrmsrl(MSR_KERNEL_GS_BASE, vmx->msr_host_kernel_gs_base);
Avi Kivity44ea2b12009-09-06 15:55:37 +03002240#endif
Liu, Jinsongda8999d2014-02-24 10:55:46 +00002241 if (vmx->host_state.msr_host_bndcfgs)
2242 wrmsrl(MSR_IA32_BNDCFGS, vmx->host_state.msr_host_bndcfgs);
Thomas Garnier45fc8752017-03-14 10:05:08 -07002243 load_fixmap_gdt(raw_smp_processor_id());
Avi Kivity33ed6322007-05-02 16:54:03 +03002244}
2245
Avi Kivitya9b21b62008-06-24 11:48:49 +03002246static void vmx_load_host_state(struct vcpu_vmx *vmx)
2247{
2248 preempt_disable();
2249 __vmx_load_host_state(vmx);
2250 preempt_enable();
2251}
2252
Feng Wu28b835d2015-09-18 22:29:54 +08002253static void vmx_vcpu_pi_load(struct kvm_vcpu *vcpu, int cpu)
2254{
2255 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
2256 struct pi_desc old, new;
2257 unsigned int dest;
2258
Paolo Bonzini31afb2e2017-06-06 12:57:06 +02002259 /*
2260 * In case of hot-plug or hot-unplug, we may have to undo
2261 * vmx_vcpu_pi_put even if there is no assigned device. And we
2262 * always keep PI.NDST up to date for simplicity: it makes the
2263 * code easier, and CPU migration is not a fast path.
2264 */
2265 if (!pi_test_sn(pi_desc) && vcpu->cpu == cpu)
Feng Wu28b835d2015-09-18 22:29:54 +08002266 return;
2267
Paolo Bonzini31afb2e2017-06-06 12:57:06 +02002268 /*
2269 * First handle the simple case where no cmpxchg is necessary; just
2270 * allow posting non-urgent interrupts.
2271 *
2272 * If the 'nv' field is POSTED_INTR_WAKEUP_VECTOR, do not change
2273 * PI.NDST: pi_post_block will do it for us and the wakeup_handler
2274 * expects the VCPU to be on the blocked_vcpu_list that matches
2275 * PI.NDST.
2276 */
2277 if (pi_desc->nv == POSTED_INTR_WAKEUP_VECTOR ||
2278 vcpu->cpu == cpu) {
2279 pi_clear_sn(pi_desc);
2280 return;
2281 }
2282
2283 /* The full case. */
Feng Wu28b835d2015-09-18 22:29:54 +08002284 do {
2285 old.control = new.control = pi_desc->control;
2286
Paolo Bonzini31afb2e2017-06-06 12:57:06 +02002287 dest = cpu_physical_id(cpu);
Feng Wu28b835d2015-09-18 22:29:54 +08002288
Paolo Bonzini31afb2e2017-06-06 12:57:06 +02002289 if (x2apic_enabled())
2290 new.ndst = dest;
2291 else
2292 new.ndst = (dest << 8) & 0xFF00;
Feng Wu28b835d2015-09-18 22:29:54 +08002293
Feng Wu28b835d2015-09-18 22:29:54 +08002294 new.sn = 0;
Paolo Bonzinic0a16662017-09-28 17:58:41 +02002295 } while (cmpxchg64(&pi_desc->control, old.control,
2296 new.control) != old.control);
Feng Wu28b835d2015-09-18 22:29:54 +08002297}
Xiao Guangrong1be0e612016-03-22 16:51:18 +08002298
Peter Feinerc95ba922016-08-17 09:36:47 -07002299static void decache_tsc_multiplier(struct vcpu_vmx *vmx)
2300{
2301 vmx->current_tsc_ratio = vmx->vcpu.arch.tsc_scaling_ratio;
2302 vmcs_write64(TSC_MULTIPLIER, vmx->current_tsc_ratio);
2303}
2304
Avi Kivity6aa8b732006-12-10 02:21:36 -08002305/*
2306 * Switches to specified vcpu, until a matching vcpu_put(), but assumes
2307 * vcpu mutex is already taken.
2308 */
Avi Kivity15ad7142007-07-11 18:17:21 +03002309static void vmx_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002310{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002311 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jim Mattsonb80c76e2016-07-29 18:56:53 -07002312 bool already_loaded = vmx->loaded_vmcs->cpu == cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002313
Jim Mattsonb80c76e2016-07-29 18:56:53 -07002314 if (!already_loaded) {
David Hildenbrandfe0e80b2017-03-10 12:47:13 +01002315 loaded_vmcs_clear(vmx->loaded_vmcs);
Dongxiao Xu92fe13b2010-05-11 18:29:42 +08002316 local_irq_disable();
Zhang Yanfei8f536b72012-12-06 23:43:34 +08002317 crash_disable_local_vmclear(cpu);
Xiao Guangrong5a560f82012-11-28 20:54:14 +08002318
2319 /*
2320 * Read loaded_vmcs->cpu should be before fetching
2321 * loaded_vmcs->loaded_vmcss_on_cpu_link.
2322 * See the comments in __loaded_vmcs_clear().
2323 */
2324 smp_rmb();
2325
Nadav Har'Eld462b812011-05-24 15:26:10 +03002326 list_add(&vmx->loaded_vmcs->loaded_vmcss_on_cpu_link,
2327 &per_cpu(loaded_vmcss_on_cpu, cpu));
Zhang Yanfei8f536b72012-12-06 23:43:34 +08002328 crash_enable_local_vmclear(cpu);
Dongxiao Xu92fe13b2010-05-11 18:29:42 +08002329 local_irq_enable();
Jim Mattsonb80c76e2016-07-29 18:56:53 -07002330 }
2331
2332 if (per_cpu(current_vmcs, cpu) != vmx->loaded_vmcs->vmcs) {
2333 per_cpu(current_vmcs, cpu) = vmx->loaded_vmcs->vmcs;
2334 vmcs_load(vmx->loaded_vmcs->vmcs);
Ashok Raj15d45072018-02-01 22:59:43 +01002335 indirect_branch_prediction_barrier();
Jim Mattsonb80c76e2016-07-29 18:56:53 -07002336 }
2337
2338 if (!already_loaded) {
Andy Lutomirski59c58ceb2017-03-22 14:32:33 -07002339 void *gdt = get_current_gdt_ro();
Jim Mattsonb80c76e2016-07-29 18:56:53 -07002340 unsigned long sysenter_esp;
2341
2342 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
Dongxiao Xu92fe13b2010-05-11 18:29:42 +08002343
Avi Kivity6aa8b732006-12-10 02:21:36 -08002344 /*
2345 * Linux uses per-cpu TSS and GDT, so set these when switching
Andy Lutomirskie0c23062017-02-20 08:56:10 -08002346 * processors. See 22.2.4.
Avi Kivity6aa8b732006-12-10 02:21:36 -08002347 */
Andy Lutomirskie0c23062017-02-20 08:56:10 -08002348 vmcs_writel(HOST_TR_BASE,
Andy Lutomirski72f5e082017-12-04 15:07:20 +01002349 (unsigned long)&get_cpu_entry_area(cpu)->tss.x86_tss);
Andy Lutomirski59c58ceb2017-03-22 14:32:33 -07002350 vmcs_writel(HOST_GDTR_BASE, (unsigned long)gdt); /* 22.2.4 */
Avi Kivity6aa8b732006-12-10 02:21:36 -08002351
Andy Lutomirskib7ffc442017-02-20 08:56:14 -08002352 /*
2353 * VM exits change the host TR limit to 0x67 after a VM
2354 * exit. This is okay, since 0x67 covers everything except
2355 * the IO bitmap and have have code to handle the IO bitmap
2356 * being lost after a VM exit.
2357 */
2358 BUILD_BUG_ON(IO_BITMAP_OFFSET - 1 != 0x67);
2359
Avi Kivity6aa8b732006-12-10 02:21:36 -08002360 rdmsrl(MSR_IA32_SYSENTER_ESP, sysenter_esp);
2361 vmcs_writel(HOST_IA32_SYSENTER_ESP, sysenter_esp); /* 22.2.3 */
Haozhong Zhangff2c3a12015-10-20 15:39:10 +08002362
Nadav Har'Eld462b812011-05-24 15:26:10 +03002363 vmx->loaded_vmcs->cpu = cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002364 }
Feng Wu28b835d2015-09-18 22:29:54 +08002365
Owen Hofmann2680d6d2016-03-01 13:36:13 -08002366 /* Setup TSC multiplier */
2367 if (kvm_has_tsc_control &&
Peter Feinerc95ba922016-08-17 09:36:47 -07002368 vmx->current_tsc_ratio != vcpu->arch.tsc_scaling_ratio)
2369 decache_tsc_multiplier(vmx);
Owen Hofmann2680d6d2016-03-01 13:36:13 -08002370
Feng Wu28b835d2015-09-18 22:29:54 +08002371 vmx_vcpu_pi_load(vcpu, cpu);
Xiao Guangrong1be0e612016-03-22 16:51:18 +08002372 vmx->host_pkru = read_pkru();
Wanpeng Li74c55932017-11-29 01:31:20 -08002373 vmx->host_debugctlmsr = get_debugctlmsr();
Feng Wu28b835d2015-09-18 22:29:54 +08002374}
2375
2376static void vmx_vcpu_pi_put(struct kvm_vcpu *vcpu)
2377{
2378 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
2379
2380 if (!kvm_arch_has_assigned_device(vcpu->kvm) ||
Yang Zhanga0052192016-06-13 09:56:56 +08002381 !irq_remapping_cap(IRQ_POSTING_CAP) ||
2382 !kvm_vcpu_apicv_active(vcpu))
Feng Wu28b835d2015-09-18 22:29:54 +08002383 return;
2384
2385 /* Set SN when the vCPU is preempted */
2386 if (vcpu->preempted)
2387 pi_set_sn(pi_desc);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002388}
2389
2390static void vmx_vcpu_put(struct kvm_vcpu *vcpu)
2391{
Feng Wu28b835d2015-09-18 22:29:54 +08002392 vmx_vcpu_pi_put(vcpu);
2393
Avi Kivitya9b21b62008-06-24 11:48:49 +03002394 __vmx_load_host_state(to_vmx(vcpu));
Avi Kivity6aa8b732006-12-10 02:21:36 -08002395}
2396
Wanpeng Lif244dee2017-07-20 01:11:54 -07002397static bool emulation_required(struct kvm_vcpu *vcpu)
2398{
2399 return emulate_invalid_guest_state && !guest_state_valid(vcpu);
2400}
2401
Avi Kivityedcafe32009-12-30 18:07:40 +02002402static void vmx_decache_cr0_guest_bits(struct kvm_vcpu *vcpu);
2403
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03002404/*
2405 * Return the cr0 value that a nested guest would read. This is a combination
2406 * of the real cr0 used to run the guest (guest_cr0), and the bits shadowed by
2407 * its hypervisor (cr0_read_shadow).
2408 */
2409static inline unsigned long nested_read_cr0(struct vmcs12 *fields)
2410{
2411 return (fields->guest_cr0 & ~fields->cr0_guest_host_mask) |
2412 (fields->cr0_read_shadow & fields->cr0_guest_host_mask);
2413}
2414static inline unsigned long nested_read_cr4(struct vmcs12 *fields)
2415{
2416 return (fields->guest_cr4 & ~fields->cr4_guest_host_mask) |
2417 (fields->cr4_read_shadow & fields->cr4_guest_host_mask);
2418}
2419
Avi Kivity6aa8b732006-12-10 02:21:36 -08002420static unsigned long vmx_get_rflags(struct kvm_vcpu *vcpu)
2421{
Avi Kivity78ac8b42010-04-08 18:19:35 +03002422 unsigned long rflags, save_rflags;
Avi Kivity345dcaa2009-08-12 15:29:37 +03002423
Avi Kivity6de12732011-03-07 12:51:22 +02002424 if (!test_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail)) {
2425 __set_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail);
2426 rflags = vmcs_readl(GUEST_RFLAGS);
2427 if (to_vmx(vcpu)->rmode.vm86_active) {
2428 rflags &= RMODE_GUEST_OWNED_EFLAGS_BITS;
2429 save_rflags = to_vmx(vcpu)->rmode.save_rflags;
2430 rflags |= save_rflags & ~RMODE_GUEST_OWNED_EFLAGS_BITS;
2431 }
2432 to_vmx(vcpu)->rflags = rflags;
Avi Kivity78ac8b42010-04-08 18:19:35 +03002433 }
Avi Kivity6de12732011-03-07 12:51:22 +02002434 return to_vmx(vcpu)->rflags;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002435}
2436
2437static void vmx_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags)
2438{
Wanpeng Lif244dee2017-07-20 01:11:54 -07002439 unsigned long old_rflags = vmx_get_rflags(vcpu);
2440
Avi Kivity6de12732011-03-07 12:51:22 +02002441 __set_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail);
2442 to_vmx(vcpu)->rflags = rflags;
Avi Kivity78ac8b42010-04-08 18:19:35 +03002443 if (to_vmx(vcpu)->rmode.vm86_active) {
2444 to_vmx(vcpu)->rmode.save_rflags = rflags;
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +01002445 rflags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM;
Avi Kivity78ac8b42010-04-08 18:19:35 +03002446 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002447 vmcs_writel(GUEST_RFLAGS, rflags);
Wanpeng Lif244dee2017-07-20 01:11:54 -07002448
2449 if ((old_rflags ^ to_vmx(vcpu)->rflags) & X86_EFLAGS_VM)
2450 to_vmx(vcpu)->emulation_required = emulation_required(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002451}
2452
Paolo Bonzini37ccdcb2014-05-20 14:29:47 +02002453static u32 vmx_get_interrupt_shadow(struct kvm_vcpu *vcpu)
Glauber Costa2809f5d2009-05-12 16:21:05 -04002454{
2455 u32 interruptibility = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
2456 int ret = 0;
2457
2458 if (interruptibility & GUEST_INTR_STATE_STI)
Jan Kiszka48005f62010-02-19 19:38:07 +01002459 ret |= KVM_X86_SHADOW_INT_STI;
Glauber Costa2809f5d2009-05-12 16:21:05 -04002460 if (interruptibility & GUEST_INTR_STATE_MOV_SS)
Jan Kiszka48005f62010-02-19 19:38:07 +01002461 ret |= KVM_X86_SHADOW_INT_MOV_SS;
Glauber Costa2809f5d2009-05-12 16:21:05 -04002462
Paolo Bonzini37ccdcb2014-05-20 14:29:47 +02002463 return ret;
Glauber Costa2809f5d2009-05-12 16:21:05 -04002464}
2465
2466static void vmx_set_interrupt_shadow(struct kvm_vcpu *vcpu, int mask)
2467{
2468 u32 interruptibility_old = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
2469 u32 interruptibility = interruptibility_old;
2470
2471 interruptibility &= ~(GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS);
2472
Jan Kiszka48005f62010-02-19 19:38:07 +01002473 if (mask & KVM_X86_SHADOW_INT_MOV_SS)
Glauber Costa2809f5d2009-05-12 16:21:05 -04002474 interruptibility |= GUEST_INTR_STATE_MOV_SS;
Jan Kiszka48005f62010-02-19 19:38:07 +01002475 else if (mask & KVM_X86_SHADOW_INT_STI)
Glauber Costa2809f5d2009-05-12 16:21:05 -04002476 interruptibility |= GUEST_INTR_STATE_STI;
2477
2478 if ((interruptibility != interruptibility_old))
2479 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, interruptibility);
2480}
2481
Avi Kivity6aa8b732006-12-10 02:21:36 -08002482static void skip_emulated_instruction(struct kvm_vcpu *vcpu)
2483{
2484 unsigned long rip;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002485
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002486 rip = kvm_rip_read(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002487 rip += vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002488 kvm_rip_write(vcpu, rip);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002489
Glauber Costa2809f5d2009-05-12 16:21:05 -04002490 /* skipping an emulated instruction also counts */
2491 vmx_set_interrupt_shadow(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002492}
2493
Paolo Bonzinib96fb432017-07-27 12:29:32 +02002494static void nested_vmx_inject_exception_vmexit(struct kvm_vcpu *vcpu,
2495 unsigned long exit_qual)
2496{
2497 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
2498 unsigned int nr = vcpu->arch.exception.nr;
2499 u32 intr_info = nr | INTR_INFO_VALID_MASK;
2500
2501 if (vcpu->arch.exception.has_error_code) {
2502 vmcs12->vm_exit_intr_error_code = vcpu->arch.exception.error_code;
2503 intr_info |= INTR_INFO_DELIVER_CODE_MASK;
2504 }
2505
2506 if (kvm_exception_is_soft(nr))
2507 intr_info |= INTR_TYPE_SOFT_EXCEPTION;
2508 else
2509 intr_info |= INTR_TYPE_HARD_EXCEPTION;
2510
2511 if (!(vmcs12->idt_vectoring_info_field & VECTORING_INFO_VALID_MASK) &&
2512 vmx_get_nmi_mask(vcpu))
2513 intr_info |= INTR_INFO_UNBLOCK_NMI;
2514
2515 nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI, intr_info, exit_qual);
2516}
2517
Nadav Har'El0b6ac342011-05-25 23:13:36 +03002518/*
2519 * KVM wants to inject page-faults which it got to the guest. This function
2520 * checks whether in a nested guest, we need to inject them to L1 or L2.
Nadav Har'El0b6ac342011-05-25 23:13:36 +03002521 */
Wanpeng Libfcf83b2017-08-24 03:35:11 -07002522static int nested_vmx_check_exception(struct kvm_vcpu *vcpu, unsigned long *exit_qual)
Nadav Har'El0b6ac342011-05-25 23:13:36 +03002523{
2524 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Wanpeng Liadfe20f2017-07-13 18:30:41 -07002525 unsigned int nr = vcpu->arch.exception.nr;
Nadav Har'El0b6ac342011-05-25 23:13:36 +03002526
Paolo Bonzinib96fb432017-07-27 12:29:32 +02002527 if (nr == PF_VECTOR) {
2528 if (vcpu->arch.exception.nested_apf) {
Wanpeng Libfcf83b2017-08-24 03:35:11 -07002529 *exit_qual = vcpu->arch.apf.nested_apf_token;
Paolo Bonzinib96fb432017-07-27 12:29:32 +02002530 return 1;
2531 }
2532 /*
2533 * FIXME: we must not write CR2 when L1 intercepts an L2 #PF exception.
2534 * The fix is to add the ancillary datum (CR2 or DR6) to structs
2535 * kvm_queued_exception and kvm_vcpu_events, so that CR2 and DR6
2536 * can be written only when inject_pending_event runs. This should be
2537 * conditional on a new capability---if the capability is disabled,
2538 * kvm_multiple_exception would write the ancillary information to
2539 * CR2 or DR6, for backwards ABI-compatibility.
2540 */
2541 if (nested_vmx_is_page_fault_vmexit(vmcs12,
2542 vcpu->arch.exception.error_code)) {
Wanpeng Libfcf83b2017-08-24 03:35:11 -07002543 *exit_qual = vcpu->arch.cr2;
Paolo Bonzinib96fb432017-07-27 12:29:32 +02002544 return 1;
2545 }
2546 } else {
Paolo Bonzinib96fb432017-07-27 12:29:32 +02002547 if (vmcs12->exception_bitmap & (1u << nr)) {
Wanpeng Libfcf83b2017-08-24 03:35:11 -07002548 if (nr == DB_VECTOR)
2549 *exit_qual = vcpu->arch.dr6;
2550 else
2551 *exit_qual = 0;
Paolo Bonzinib96fb432017-07-27 12:29:32 +02002552 return 1;
2553 }
Wanpeng Liadfe20f2017-07-13 18:30:41 -07002554 }
2555
Paolo Bonzinib96fb432017-07-27 12:29:32 +02002556 return 0;
Nadav Har'El0b6ac342011-05-25 23:13:36 +03002557}
2558
Wanpeng Licaa057a2018-03-12 04:53:03 -07002559static void vmx_clear_hlt(struct kvm_vcpu *vcpu)
2560{
2561 /*
2562 * Ensure that we clear the HLT state in the VMCS. We don't need to
2563 * explicitly skip the instruction because if the HLT state is set,
2564 * then the instruction is already executing and RIP has already been
2565 * advanced.
2566 */
2567 if (kvm_hlt_in_guest(vcpu->kvm) &&
2568 vmcs_read32(GUEST_ACTIVITY_STATE) == GUEST_ACTIVITY_HLT)
2569 vmcs_write32(GUEST_ACTIVITY_STATE, GUEST_ACTIVITY_ACTIVE);
2570}
2571
Wanpeng Licfcd20e2017-07-13 18:30:39 -07002572static void vmx_queue_exception(struct kvm_vcpu *vcpu)
Avi Kivity298101d2007-11-25 13:41:11 +02002573{
Jan Kiszka77ab6db2008-07-14 12:28:51 +02002574 struct vcpu_vmx *vmx = to_vmx(vcpu);
Wanpeng Licfcd20e2017-07-13 18:30:39 -07002575 unsigned nr = vcpu->arch.exception.nr;
2576 bool has_error_code = vcpu->arch.exception.has_error_code;
Wanpeng Licfcd20e2017-07-13 18:30:39 -07002577 u32 error_code = vcpu->arch.exception.error_code;
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01002578 u32 intr_info = nr | INTR_INFO_VALID_MASK;
Jan Kiszka77ab6db2008-07-14 12:28:51 +02002579
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01002580 if (has_error_code) {
Jan Kiszka77ab6db2008-07-14 12:28:51 +02002581 vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE, error_code);
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01002582 intr_info |= INTR_INFO_DELIVER_CODE_MASK;
2583 }
Jan Kiszka77ab6db2008-07-14 12:28:51 +02002584
Avi Kivity7ffd92c2009-06-09 14:10:45 +03002585 if (vmx->rmode.vm86_active) {
Serge E. Hallyn71f98332011-04-13 09:12:54 -05002586 int inc_eip = 0;
2587 if (kvm_exception_is_soft(nr))
2588 inc_eip = vcpu->arch.event_exit_inst_len;
2589 if (kvm_inject_realmode_interrupt(vcpu, nr, inc_eip) != EMULATE_DONE)
Mohammed Gamala92601b2010-09-19 14:34:07 +02002590 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Jan Kiszka77ab6db2008-07-14 12:28:51 +02002591 return;
2592 }
2593
Gleb Natapov66fd3f72009-05-11 13:35:50 +03002594 if (kvm_exception_is_soft(nr)) {
2595 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
2596 vmx->vcpu.arch.event_exit_inst_len);
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01002597 intr_info |= INTR_TYPE_SOFT_EXCEPTION;
2598 } else
2599 intr_info |= INTR_TYPE_HARD_EXCEPTION;
2600
2601 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr_info);
Wanpeng Licaa057a2018-03-12 04:53:03 -07002602
2603 vmx_clear_hlt(vcpu);
Avi Kivity298101d2007-11-25 13:41:11 +02002604}
2605
Sheng Yang4e47c7a2009-12-18 16:48:47 +08002606static bool vmx_rdtscp_supported(void)
2607{
2608 return cpu_has_vmx_rdtscp();
2609}
2610
Mao, Junjiead756a12012-07-02 01:18:48 +00002611static bool vmx_invpcid_supported(void)
2612{
2613 return cpu_has_vmx_invpcid() && enable_ept;
2614}
2615
Avi Kivity6aa8b732006-12-10 02:21:36 -08002616/*
Eddie Donga75beee2007-05-17 18:55:15 +03002617 * Swap MSR entry in host/guest MSR entry array.
2618 */
Rusty Russell8b9cf982007-07-30 16:31:43 +10002619static void move_msr_up(struct vcpu_vmx *vmx, int from, int to)
Eddie Donga75beee2007-05-17 18:55:15 +03002620{
Avi Kivity26bb0982009-09-07 11:14:12 +03002621 struct shared_msr_entry tmp;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002622
2623 tmp = vmx->guest_msrs[to];
2624 vmx->guest_msrs[to] = vmx->guest_msrs[from];
2625 vmx->guest_msrs[from] = tmp;
Eddie Donga75beee2007-05-17 18:55:15 +03002626}
2627
2628/*
Avi Kivitye38aea32007-04-19 13:22:48 +03002629 * Set up the vmcs to automatically save and restore system
2630 * msrs. Don't touch the 64-bit msrs if the guest is in legacy
2631 * mode, as fiddling with msrs is very expensive.
2632 */
Rusty Russell8b9cf982007-07-30 16:31:43 +10002633static void setup_msrs(struct vcpu_vmx *vmx)
Avi Kivitye38aea32007-04-19 13:22:48 +03002634{
Avi Kivity26bb0982009-09-07 11:14:12 +03002635 int save_nmsrs, index;
Avi Kivitye38aea32007-04-19 13:22:48 +03002636
Eddie Donga75beee2007-05-17 18:55:15 +03002637 save_nmsrs = 0;
Avi Kivity4d56c8a2007-04-19 14:28:44 +03002638#ifdef CONFIG_X86_64
Rusty Russell8b9cf982007-07-30 16:31:43 +10002639 if (is_long_mode(&vmx->vcpu)) {
Rusty Russell8b9cf982007-07-30 16:31:43 +10002640 index = __find_msr_index(vmx, MSR_SYSCALL_MASK);
Eddie Donga75beee2007-05-17 18:55:15 +03002641 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +10002642 move_msr_up(vmx, index, save_nmsrs++);
2643 index = __find_msr_index(vmx, MSR_LSTAR);
Eddie Donga75beee2007-05-17 18:55:15 +03002644 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +10002645 move_msr_up(vmx, index, save_nmsrs++);
2646 index = __find_msr_index(vmx, MSR_CSTAR);
Eddie Donga75beee2007-05-17 18:55:15 +03002647 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +10002648 move_msr_up(vmx, index, save_nmsrs++);
Sheng Yang4e47c7a2009-12-18 16:48:47 +08002649 index = __find_msr_index(vmx, MSR_TSC_AUX);
Radim Krčmářd6321d42017-08-05 00:12:49 +02002650 if (index >= 0 && guest_cpuid_has(&vmx->vcpu, X86_FEATURE_RDTSCP))
Sheng Yang4e47c7a2009-12-18 16:48:47 +08002651 move_msr_up(vmx, index, save_nmsrs++);
Eddie Donga75beee2007-05-17 18:55:15 +03002652 /*
Brian Gerst8c065852010-07-17 09:03:26 -04002653 * MSR_STAR is only needed on long mode guests, and only
Eddie Donga75beee2007-05-17 18:55:15 +03002654 * if efer.sce is enabled.
2655 */
Brian Gerst8c065852010-07-17 09:03:26 -04002656 index = __find_msr_index(vmx, MSR_STAR);
Avi Kivityf6801df2010-01-21 15:31:50 +02002657 if ((index >= 0) && (vmx->vcpu.arch.efer & EFER_SCE))
Rusty Russell8b9cf982007-07-30 16:31:43 +10002658 move_msr_up(vmx, index, save_nmsrs++);
Avi Kivity4d56c8a2007-04-19 14:28:44 +03002659 }
Eddie Donga75beee2007-05-17 18:55:15 +03002660#endif
Avi Kivity92c0d902009-10-29 11:00:16 +02002661 index = __find_msr_index(vmx, MSR_EFER);
2662 if (index >= 0 && update_transition_efer(vmx, index))
Avi Kivity26bb0982009-09-07 11:14:12 +03002663 move_msr_up(vmx, index, save_nmsrs++);
Avi Kivity4d56c8a2007-04-19 14:28:44 +03002664
Avi Kivity26bb0982009-09-07 11:14:12 +03002665 vmx->save_nmsrs = save_nmsrs;
Avi Kivity58972972009-02-24 22:26:47 +02002666
Yang Zhang8d146952013-01-25 10:18:50 +08002667 if (cpu_has_vmx_msr_bitmap())
Paolo Bonzini904e14f2018-01-16 16:51:18 +01002668 vmx_update_msr_bitmap(&vmx->vcpu);
Avi Kivitye38aea32007-04-19 13:22:48 +03002669}
2670
2671/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08002672 * reads and returns guest's timestamp counter "register"
Haozhong Zhangbe7b2632015-10-20 15:39:11 +08002673 * guest_tsc = (host_tsc * tsc multiplier) >> 48 + tsc_offset
2674 * -- Intel TSC Scaling for Virtualization White Paper, sec 1.3
Avi Kivity6aa8b732006-12-10 02:21:36 -08002675 */
Haozhong Zhangbe7b2632015-10-20 15:39:11 +08002676static u64 guest_read_tsc(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002677{
2678 u64 host_tsc, tsc_offset;
2679
Andy Lutomirski4ea16362015-06-25 18:44:07 +02002680 host_tsc = rdtsc();
Avi Kivity6aa8b732006-12-10 02:21:36 -08002681 tsc_offset = vmcs_read64(TSC_OFFSET);
Haozhong Zhangbe7b2632015-10-20 15:39:11 +08002682 return kvm_scale_tsc(vcpu, host_tsc) + tsc_offset;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002683}
2684
2685/*
Zachary Amsden99e3e302010-08-19 22:07:17 -10002686 * writes 'offset' into guest's timestamp counter offset register
Avi Kivity6aa8b732006-12-10 02:21:36 -08002687 */
Zachary Amsden99e3e302010-08-19 22:07:17 -10002688static void vmx_write_tsc_offset(struct kvm_vcpu *vcpu, u64 offset)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002689{
Nadav Har'El27fc51b2011-08-02 15:54:52 +03002690 if (is_guest_mode(vcpu)) {
Nadav Har'El79918252011-05-25 23:15:39 +03002691 /*
Nadav Har'El27fc51b2011-08-02 15:54:52 +03002692 * We're here if L1 chose not to trap WRMSR to TSC. According
2693 * to the spec, this should set L1's TSC; The offset that L1
2694 * set for L2 remains unchanged, and still needs to be added
2695 * to the newly set TSC to get L2's TSC.
Nadav Har'El79918252011-05-25 23:15:39 +03002696 */
Nadav Har'El27fc51b2011-08-02 15:54:52 +03002697 struct vmcs12 *vmcs12;
Nadav Har'El27fc51b2011-08-02 15:54:52 +03002698 /* recalculate vmcs02.TSC_OFFSET: */
2699 vmcs12 = get_vmcs12(vcpu);
2700 vmcs_write64(TSC_OFFSET, offset +
2701 (nested_cpu_has(vmcs12, CPU_BASED_USE_TSC_OFFSETING) ?
2702 vmcs12->tsc_offset : 0));
2703 } else {
Yoshihiro YUNOMAE489223e2013-06-12 16:43:44 +09002704 trace_kvm_write_tsc_offset(vcpu->vcpu_id,
2705 vmcs_read64(TSC_OFFSET), offset);
Nadav Har'El27fc51b2011-08-02 15:54:52 +03002706 vmcs_write64(TSC_OFFSET, offset);
2707 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002708}
2709
Nadav Har'El801d3422011-05-25 23:02:23 +03002710/*
2711 * nested_vmx_allowed() checks whether a guest should be allowed to use VMX
2712 * instructions and MSRs (i.e., nested VMX). Nested VMX is disabled for
2713 * all guests if the "nested" module option is off, and can also be disabled
2714 * for a single guest by disabling its VMX cpuid bit.
2715 */
2716static inline bool nested_vmx_allowed(struct kvm_vcpu *vcpu)
2717{
Radim Krčmářd6321d42017-08-05 00:12:49 +02002718 return nested && guest_cpuid_has(vcpu, X86_FEATURE_VMX);
Nadav Har'El801d3422011-05-25 23:02:23 +03002719}
2720
Avi Kivity6aa8b732006-12-10 02:21:36 -08002721/*
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002722 * nested_vmx_setup_ctls_msrs() sets up variables containing the values to be
2723 * returned for the various VMX controls MSRs when nested VMX is enabled.
2724 * The same values should also be used to verify that vmcs12 control fields are
2725 * valid during nested entry from L1 to L2.
2726 * Each of these control msrs has a low and high 32-bit half: A low bit is on
2727 * if the corresponding bit in the (32-bit) control field *must* be on, and a
2728 * bit in the high half is on if the corresponding bit in the control field
2729 * may be on. See also vmx_control_verify().
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002730 */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01002731static void nested_vmx_setup_ctls_msrs(struct nested_vmx_msrs *msrs, bool apicv)
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002732{
Paolo Bonzini13893092018-02-26 13:40:09 +01002733 if (!nested) {
2734 memset(msrs, 0, sizeof(*msrs));
2735 return;
2736 }
2737
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002738 /*
2739 * Note that as a general rule, the high half of the MSRs (bits in
2740 * the control fields which may be 1) should be initialized by the
2741 * intersection of the underlying hardware's MSR (i.e., features which
2742 * can be supported) and the list of features we want to expose -
2743 * because they are known to be properly supported in our code.
2744 * Also, usually, the low half of the MSRs (bits which must be 1) can
2745 * be set to 0, meaning that L1 may turn off any of these bits. The
2746 * reason is that if one of these bits is necessary, it will appear
2747 * in vmcs01 and prepare_vmcs02, when it bitwise-or's the control
2748 * fields of vmcs01 and vmcs02, will turn these bits off - and
Paolo Bonzini7313c692017-07-27 10:31:25 +02002749 * nested_vmx_exit_reflected() will not pass related exits to L1.
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002750 * These rules have exceptions below.
2751 */
2752
2753 /* pin-based controls */
Jan Kiszkaeabeaac2013-03-13 11:30:50 +01002754 rdmsr(MSR_IA32_VMX_PINBASED_CTLS,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01002755 msrs->pinbased_ctls_low,
2756 msrs->pinbased_ctls_high);
2757 msrs->pinbased_ctls_low |=
Wincy Vanb9c237b2015-02-03 23:56:30 +08002758 PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01002759 msrs->pinbased_ctls_high &=
Wincy Vanb9c237b2015-02-03 23:56:30 +08002760 PIN_BASED_EXT_INTR_MASK |
2761 PIN_BASED_NMI_EXITING |
Paolo Bonzini13893092018-02-26 13:40:09 +01002762 PIN_BASED_VIRTUAL_NMIS |
2763 (apicv ? PIN_BASED_POSTED_INTR : 0);
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01002764 msrs->pinbased_ctls_high |=
Wincy Vanb9c237b2015-02-03 23:56:30 +08002765 PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR |
Jan Kiszka0238ea92013-03-13 11:31:24 +01002766 PIN_BASED_VMX_PREEMPTION_TIMER;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002767
Jan Kiszka3dbcd8d2014-06-16 13:59:40 +02002768 /* exit controls */
Arthur Chunqi Lic0dfee52013-08-06 18:41:45 +08002769 rdmsr(MSR_IA32_VMX_EXIT_CTLS,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01002770 msrs->exit_ctls_low,
2771 msrs->exit_ctls_high);
2772 msrs->exit_ctls_low =
Wincy Vanb9c237b2015-02-03 23:56:30 +08002773 VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR;
Bandan Dase0ba1a62014-04-19 18:17:46 -04002774
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01002775 msrs->exit_ctls_high &=
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002776#ifdef CONFIG_X86_64
Arthur Chunqi Lic0dfee52013-08-06 18:41:45 +08002777 VM_EXIT_HOST_ADDR_SPACE_SIZE |
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002778#endif
Jan Kiszkaf41245002014-03-07 20:03:13 +01002779 VM_EXIT_LOAD_IA32_PAT | VM_EXIT_SAVE_IA32_PAT;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01002780 msrs->exit_ctls_high |=
Wincy Vanb9c237b2015-02-03 23:56:30 +08002781 VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR |
Jan Kiszkaf41245002014-03-07 20:03:13 +01002782 VM_EXIT_LOAD_IA32_EFER | VM_EXIT_SAVE_IA32_EFER |
Bandan Dase0ba1a62014-04-19 18:17:46 -04002783 VM_EXIT_SAVE_VMX_PREEMPTION_TIMER | VM_EXIT_ACK_INTR_ON_EXIT;
2784
Paolo Bonzinia87036a2016-03-08 09:52:13 +01002785 if (kvm_mpx_supported())
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01002786 msrs->exit_ctls_high |= VM_EXIT_CLEAR_BNDCFGS;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002787
Jan Kiszka2996fca2014-06-16 13:59:43 +02002788 /* We support free control of debug control saving. */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01002789 msrs->exit_ctls_low &= ~VM_EXIT_SAVE_DEBUG_CONTROLS;
Jan Kiszka2996fca2014-06-16 13:59:43 +02002790
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002791 /* entry controls */
2792 rdmsr(MSR_IA32_VMX_ENTRY_CTLS,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01002793 msrs->entry_ctls_low,
2794 msrs->entry_ctls_high);
2795 msrs->entry_ctls_low =
Wincy Vanb9c237b2015-02-03 23:56:30 +08002796 VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01002797 msrs->entry_ctls_high &=
Jan Kiszka57435342013-08-06 10:39:56 +02002798#ifdef CONFIG_X86_64
2799 VM_ENTRY_IA32E_MODE |
2800#endif
2801 VM_ENTRY_LOAD_IA32_PAT;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01002802 msrs->entry_ctls_high |=
Wincy Vanb9c237b2015-02-03 23:56:30 +08002803 (VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR | VM_ENTRY_LOAD_IA32_EFER);
Paolo Bonzinia87036a2016-03-08 09:52:13 +01002804 if (kvm_mpx_supported())
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01002805 msrs->entry_ctls_high |= VM_ENTRY_LOAD_BNDCFGS;
Jan Kiszka57435342013-08-06 10:39:56 +02002806
Jan Kiszka2996fca2014-06-16 13:59:43 +02002807 /* We support free control of debug control loading. */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01002808 msrs->entry_ctls_low &= ~VM_ENTRY_LOAD_DEBUG_CONTROLS;
Jan Kiszka2996fca2014-06-16 13:59:43 +02002809
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002810 /* cpu-based controls */
2811 rdmsr(MSR_IA32_VMX_PROCBASED_CTLS,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01002812 msrs->procbased_ctls_low,
2813 msrs->procbased_ctls_high);
2814 msrs->procbased_ctls_low =
Wincy Vanb9c237b2015-02-03 23:56:30 +08002815 CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01002816 msrs->procbased_ctls_high &=
Jan Kiszkaa294c9b2013-10-23 17:43:09 +01002817 CPU_BASED_VIRTUAL_INTR_PENDING |
2818 CPU_BASED_VIRTUAL_NMI_PENDING | CPU_BASED_USE_TSC_OFFSETING |
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002819 CPU_BASED_HLT_EXITING | CPU_BASED_INVLPG_EXITING |
2820 CPU_BASED_MWAIT_EXITING | CPU_BASED_CR3_LOAD_EXITING |
2821 CPU_BASED_CR3_STORE_EXITING |
2822#ifdef CONFIG_X86_64
2823 CPU_BASED_CR8_LOAD_EXITING | CPU_BASED_CR8_STORE_EXITING |
2824#endif
2825 CPU_BASED_MOV_DR_EXITING | CPU_BASED_UNCOND_IO_EXITING |
Mihai Donțu5f3d45e2015-07-05 20:08:57 +03002826 CPU_BASED_USE_IO_BITMAPS | CPU_BASED_MONITOR_TRAP_FLAG |
2827 CPU_BASED_MONITOR_EXITING | CPU_BASED_RDPMC_EXITING |
2828 CPU_BASED_RDTSC_EXITING | CPU_BASED_PAUSE_EXITING |
2829 CPU_BASED_TPR_SHADOW | CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002830 /*
2831 * We can allow some features even when not supported by the
2832 * hardware. For example, L1 can specify an MSR bitmap - and we
2833 * can use it to avoid exits to L1 - even when L0 runs L2
2834 * without MSR bitmaps.
2835 */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01002836 msrs->procbased_ctls_high |=
Wincy Vanb9c237b2015-02-03 23:56:30 +08002837 CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR |
Jan Kiszka560b7ee2014-06-16 13:59:42 +02002838 CPU_BASED_USE_MSR_BITMAPS;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002839
Jan Kiszka3dcdf3ec2014-06-16 13:59:41 +02002840 /* We support free control of CR3 access interception. */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01002841 msrs->procbased_ctls_low &=
Jan Kiszka3dcdf3ec2014-06-16 13:59:41 +02002842 ~(CPU_BASED_CR3_LOAD_EXITING | CPU_BASED_CR3_STORE_EXITING);
2843
Paolo Bonzini80154d72017-08-24 13:55:35 +02002844 /*
2845 * secondary cpu-based controls. Do not include those that
2846 * depend on CPUID bits, they are added later by vmx_cpuid_update.
2847 */
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002848 rdmsr(MSR_IA32_VMX_PROCBASED_CTLS2,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01002849 msrs->secondary_ctls_low,
2850 msrs->secondary_ctls_high);
2851 msrs->secondary_ctls_low = 0;
2852 msrs->secondary_ctls_high &=
Jan Kiszkad6851fb2013-02-23 22:34:39 +01002853 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
Paolo Bonzini1b073042016-10-25 16:06:30 +02002854 SECONDARY_EXEC_DESC |
Wincy Vanf2b93282015-02-03 23:56:03 +08002855 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
Wincy Van82f0dd42015-02-03 23:57:18 +08002856 SECONDARY_EXEC_APIC_REGISTER_VIRT |
Wincy Van608406e2015-02-03 23:57:51 +08002857 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY |
Paolo Bonzini3db13482017-08-24 14:48:03 +02002858 SECONDARY_EXEC_WBINVD_EXITING;
Jan Kiszkac18911a2013-03-13 16:06:41 +01002859
Nadav Har'Elafa61f7522013-08-07 14:59:22 +02002860 if (enable_ept) {
2861 /* nested EPT: emulate EPT also to L1 */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01002862 msrs->secondary_ctls_high |=
Radim Krčmář0790ec12015-03-17 14:02:32 +01002863 SECONDARY_EXEC_ENABLE_EPT;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01002864 msrs->ept_caps = VMX_EPT_PAGE_WALK_4_BIT |
Paolo Bonzini7db74262017-03-08 10:49:19 +01002865 VMX_EPTP_WB_BIT | VMX_EPT_INVEPT_BIT;
Bandan Das02120c42016-07-12 18:18:52 -04002866 if (cpu_has_vmx_ept_execute_only())
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01002867 msrs->ept_caps |=
Bandan Das02120c42016-07-12 18:18:52 -04002868 VMX_EPT_EXECUTE_ONLY_BIT;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01002869 msrs->ept_caps &= vmx_capability.ept;
2870 msrs->ept_caps |= VMX_EPT_EXTENT_GLOBAL_BIT |
Paolo Bonzini7db74262017-03-08 10:49:19 +01002871 VMX_EPT_EXTENT_CONTEXT_BIT | VMX_EPT_2MB_PAGE_BIT |
2872 VMX_EPT_1GB_PAGE_BIT;
Bandan Das03efce62017-05-05 15:25:15 -04002873 if (enable_ept_ad_bits) {
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01002874 msrs->secondary_ctls_high |=
Bandan Das03efce62017-05-05 15:25:15 -04002875 SECONDARY_EXEC_ENABLE_PML;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01002876 msrs->ept_caps |= VMX_EPT_AD_BIT;
Bandan Das03efce62017-05-05 15:25:15 -04002877 }
David Hildenbrand1c13bff2017-08-24 20:51:33 +02002878 }
Nadav Har'Elafa61f7522013-08-07 14:59:22 +02002879
Bandan Das27c42a12017-08-03 15:54:42 -04002880 if (cpu_has_vmx_vmfunc()) {
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01002881 msrs->secondary_ctls_high |=
Bandan Das27c42a12017-08-03 15:54:42 -04002882 SECONDARY_EXEC_ENABLE_VMFUNC;
Bandan Das41ab9372017-08-03 15:54:43 -04002883 /*
2884 * Advertise EPTP switching unconditionally
2885 * since we emulate it
2886 */
Wanpeng Li575b3a22017-10-19 07:00:34 +08002887 if (enable_ept)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01002888 msrs->vmfunc_controls =
Wanpeng Li575b3a22017-10-19 07:00:34 +08002889 VMX_VMFUNC_EPTP_SWITCHING;
Bandan Das27c42a12017-08-03 15:54:42 -04002890 }
2891
Paolo Bonzinief697a72016-03-18 16:58:38 +01002892 /*
2893 * Old versions of KVM use the single-context version without
2894 * checking for support, so declare that it is supported even
2895 * though it is treated as global context. The alternative is
2896 * not failing the single-context invvpid, and it is worse.
2897 */
Wanpeng Li63cb6d52017-03-20 21:18:53 -07002898 if (enable_vpid) {
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01002899 msrs->secondary_ctls_high |=
Wanpeng Li63cb6d52017-03-20 21:18:53 -07002900 SECONDARY_EXEC_ENABLE_VPID;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01002901 msrs->vpid_caps = VMX_VPID_INVVPID_BIT |
Jan Dakinevichbcdde302016-10-28 07:00:30 +03002902 VMX_VPID_EXTENT_SUPPORTED_MASK;
David Hildenbrand1c13bff2017-08-24 20:51:33 +02002903 }
Wanpeng Li99b83ac2015-10-13 09:12:21 -07002904
Radim Krčmář0790ec12015-03-17 14:02:32 +01002905 if (enable_unrestricted_guest)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01002906 msrs->secondary_ctls_high |=
Radim Krčmář0790ec12015-03-17 14:02:32 +01002907 SECONDARY_EXEC_UNRESTRICTED_GUEST;
2908
Jan Kiszkac18911a2013-03-13 16:06:41 +01002909 /* miscellaneous data */
Wincy Vanb9c237b2015-02-03 23:56:30 +08002910 rdmsr(MSR_IA32_VMX_MISC,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01002911 msrs->misc_low,
2912 msrs->misc_high);
2913 msrs->misc_low &= VMX_MISC_SAVE_EFER_LMA;
2914 msrs->misc_low |=
Wincy Vanb9c237b2015-02-03 23:56:30 +08002915 VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE |
Jan Kiszkaf41245002014-03-07 20:03:13 +01002916 VMX_MISC_ACTIVITY_HLT;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01002917 msrs->misc_high = 0;
David Matlack62cc6b9d2016-11-29 18:14:07 -08002918
2919 /*
2920 * This MSR reports some information about VMX support. We
2921 * should return information about the VMX we emulate for the
2922 * guest, and the VMCS structure we give it - not about the
2923 * VMX support of the underlying hardware.
2924 */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01002925 msrs->basic =
David Matlack62cc6b9d2016-11-29 18:14:07 -08002926 VMCS12_REVISION |
2927 VMX_BASIC_TRUE_CTLS |
2928 ((u64)VMCS12_SIZE << VMX_BASIC_VMCS_SIZE_SHIFT) |
2929 (VMX_BASIC_MEM_TYPE_WB << VMX_BASIC_MEM_TYPE_SHIFT);
2930
2931 if (cpu_has_vmx_basic_inout())
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01002932 msrs->basic |= VMX_BASIC_INOUT;
David Matlack62cc6b9d2016-11-29 18:14:07 -08002933
2934 /*
David Matlack8322ebb2016-11-29 18:14:09 -08002935 * These MSRs specify bits which the guest must keep fixed on
David Matlack62cc6b9d2016-11-29 18:14:07 -08002936 * while L1 is in VMXON mode (in L1's root mode, or running an L2).
2937 * We picked the standard core2 setting.
2938 */
2939#define VMXON_CR0_ALWAYSON (X86_CR0_PE | X86_CR0_PG | X86_CR0_NE)
2940#define VMXON_CR4_ALWAYSON X86_CR4_VMXE
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01002941 msrs->cr0_fixed0 = VMXON_CR0_ALWAYSON;
2942 msrs->cr4_fixed0 = VMXON_CR4_ALWAYSON;
David Matlack8322ebb2016-11-29 18:14:09 -08002943
2944 /* These MSRs specify bits which the guest must keep fixed off. */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01002945 rdmsrl(MSR_IA32_VMX_CR0_FIXED1, msrs->cr0_fixed1);
2946 rdmsrl(MSR_IA32_VMX_CR4_FIXED1, msrs->cr4_fixed1);
David Matlack62cc6b9d2016-11-29 18:14:07 -08002947
2948 /* highest index: VMX_PREEMPTION_TIMER_VALUE */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01002949 msrs->vmcs_enum = VMCS12_MAX_FIELD_INDEX << 1;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002950}
2951
David Matlack38991522016-11-29 18:14:08 -08002952/*
2953 * if fixed0[i] == 1: val[i] must be 1
2954 * if fixed1[i] == 0: val[i] must be 0
2955 */
2956static inline bool fixed_bits_valid(u64 val, u64 fixed0, u64 fixed1)
2957{
2958 return ((val & fixed1) | fixed0) == val;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002959}
2960
2961static inline bool vmx_control_verify(u32 control, u32 low, u32 high)
2962{
David Matlack38991522016-11-29 18:14:08 -08002963 return fixed_bits_valid(control, low, high);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002964}
2965
2966static inline u64 vmx_control_msr(u32 low, u32 high)
2967{
2968 return low | ((u64)high << 32);
2969}
2970
David Matlack62cc6b9d2016-11-29 18:14:07 -08002971static bool is_bitwise_subset(u64 superset, u64 subset, u64 mask)
2972{
2973 superset &= mask;
2974 subset &= mask;
2975
2976 return (superset | subset) == superset;
2977}
2978
2979static int vmx_restore_vmx_basic(struct vcpu_vmx *vmx, u64 data)
2980{
2981 const u64 feature_and_reserved =
2982 /* feature (except bit 48; see below) */
2983 BIT_ULL(49) | BIT_ULL(54) | BIT_ULL(55) |
2984 /* reserved */
2985 BIT_ULL(31) | GENMASK_ULL(47, 45) | GENMASK_ULL(63, 56);
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01002986 u64 vmx_basic = vmx->nested.msrs.basic;
David Matlack62cc6b9d2016-11-29 18:14:07 -08002987
2988 if (!is_bitwise_subset(vmx_basic, data, feature_and_reserved))
2989 return -EINVAL;
2990
2991 /*
2992 * KVM does not emulate a version of VMX that constrains physical
2993 * addresses of VMX structures (e.g. VMCS) to 32-bits.
2994 */
2995 if (data & BIT_ULL(48))
2996 return -EINVAL;
2997
2998 if (vmx_basic_vmcs_revision_id(vmx_basic) !=
2999 vmx_basic_vmcs_revision_id(data))
3000 return -EINVAL;
3001
3002 if (vmx_basic_vmcs_size(vmx_basic) > vmx_basic_vmcs_size(data))
3003 return -EINVAL;
3004
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003005 vmx->nested.msrs.basic = data;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003006 return 0;
3007}
3008
3009static int
3010vmx_restore_control_msr(struct vcpu_vmx *vmx, u32 msr_index, u64 data)
3011{
3012 u64 supported;
3013 u32 *lowp, *highp;
3014
3015 switch (msr_index) {
3016 case MSR_IA32_VMX_TRUE_PINBASED_CTLS:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003017 lowp = &vmx->nested.msrs.pinbased_ctls_low;
3018 highp = &vmx->nested.msrs.pinbased_ctls_high;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003019 break;
3020 case MSR_IA32_VMX_TRUE_PROCBASED_CTLS:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003021 lowp = &vmx->nested.msrs.procbased_ctls_low;
3022 highp = &vmx->nested.msrs.procbased_ctls_high;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003023 break;
3024 case MSR_IA32_VMX_TRUE_EXIT_CTLS:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003025 lowp = &vmx->nested.msrs.exit_ctls_low;
3026 highp = &vmx->nested.msrs.exit_ctls_high;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003027 break;
3028 case MSR_IA32_VMX_TRUE_ENTRY_CTLS:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003029 lowp = &vmx->nested.msrs.entry_ctls_low;
3030 highp = &vmx->nested.msrs.entry_ctls_high;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003031 break;
3032 case MSR_IA32_VMX_PROCBASED_CTLS2:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003033 lowp = &vmx->nested.msrs.secondary_ctls_low;
3034 highp = &vmx->nested.msrs.secondary_ctls_high;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003035 break;
3036 default:
3037 BUG();
3038 }
3039
3040 supported = vmx_control_msr(*lowp, *highp);
3041
3042 /* Check must-be-1 bits are still 1. */
3043 if (!is_bitwise_subset(data, supported, GENMASK_ULL(31, 0)))
3044 return -EINVAL;
3045
3046 /* Check must-be-0 bits are still 0. */
3047 if (!is_bitwise_subset(supported, data, GENMASK_ULL(63, 32)))
3048 return -EINVAL;
3049
3050 *lowp = data;
3051 *highp = data >> 32;
3052 return 0;
3053}
3054
3055static int vmx_restore_vmx_misc(struct vcpu_vmx *vmx, u64 data)
3056{
3057 const u64 feature_and_reserved_bits =
3058 /* feature */
3059 BIT_ULL(5) | GENMASK_ULL(8, 6) | BIT_ULL(14) | BIT_ULL(15) |
3060 BIT_ULL(28) | BIT_ULL(29) | BIT_ULL(30) |
3061 /* reserved */
3062 GENMASK_ULL(13, 9) | BIT_ULL(31);
3063 u64 vmx_misc;
3064
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003065 vmx_misc = vmx_control_msr(vmx->nested.msrs.misc_low,
3066 vmx->nested.msrs.misc_high);
David Matlack62cc6b9d2016-11-29 18:14:07 -08003067
3068 if (!is_bitwise_subset(vmx_misc, data, feature_and_reserved_bits))
3069 return -EINVAL;
3070
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003071 if ((vmx->nested.msrs.pinbased_ctls_high &
David Matlack62cc6b9d2016-11-29 18:14:07 -08003072 PIN_BASED_VMX_PREEMPTION_TIMER) &&
3073 vmx_misc_preemption_timer_rate(data) !=
3074 vmx_misc_preemption_timer_rate(vmx_misc))
3075 return -EINVAL;
3076
3077 if (vmx_misc_cr3_count(data) > vmx_misc_cr3_count(vmx_misc))
3078 return -EINVAL;
3079
3080 if (vmx_misc_max_msr(data) > vmx_misc_max_msr(vmx_misc))
3081 return -EINVAL;
3082
3083 if (vmx_misc_mseg_revid(data) != vmx_misc_mseg_revid(vmx_misc))
3084 return -EINVAL;
3085
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003086 vmx->nested.msrs.misc_low = data;
3087 vmx->nested.msrs.misc_high = data >> 32;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003088 return 0;
3089}
3090
3091static int vmx_restore_vmx_ept_vpid_cap(struct vcpu_vmx *vmx, u64 data)
3092{
3093 u64 vmx_ept_vpid_cap;
3094
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003095 vmx_ept_vpid_cap = vmx_control_msr(vmx->nested.msrs.ept_caps,
3096 vmx->nested.msrs.vpid_caps);
David Matlack62cc6b9d2016-11-29 18:14:07 -08003097
3098 /* Every bit is either reserved or a feature bit. */
3099 if (!is_bitwise_subset(vmx_ept_vpid_cap, data, -1ULL))
3100 return -EINVAL;
3101
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003102 vmx->nested.msrs.ept_caps = data;
3103 vmx->nested.msrs.vpid_caps = data >> 32;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003104 return 0;
3105}
3106
3107static int vmx_restore_fixed0_msr(struct vcpu_vmx *vmx, u32 msr_index, u64 data)
3108{
3109 u64 *msr;
3110
3111 switch (msr_index) {
3112 case MSR_IA32_VMX_CR0_FIXED0:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003113 msr = &vmx->nested.msrs.cr0_fixed0;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003114 break;
3115 case MSR_IA32_VMX_CR4_FIXED0:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003116 msr = &vmx->nested.msrs.cr4_fixed0;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003117 break;
3118 default:
3119 BUG();
3120 }
3121
3122 /*
3123 * 1 bits (which indicates bits which "must-be-1" during VMX operation)
3124 * must be 1 in the restored value.
3125 */
3126 if (!is_bitwise_subset(data, *msr, -1ULL))
3127 return -EINVAL;
3128
3129 *msr = data;
3130 return 0;
3131}
3132
3133/*
3134 * Called when userspace is restoring VMX MSRs.
3135 *
3136 * Returns 0 on success, non-0 otherwise.
3137 */
3138static int vmx_set_vmx_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 data)
3139{
3140 struct vcpu_vmx *vmx = to_vmx(vcpu);
3141
3142 switch (msr_index) {
3143 case MSR_IA32_VMX_BASIC:
3144 return vmx_restore_vmx_basic(vmx, data);
3145 case MSR_IA32_VMX_PINBASED_CTLS:
3146 case MSR_IA32_VMX_PROCBASED_CTLS:
3147 case MSR_IA32_VMX_EXIT_CTLS:
3148 case MSR_IA32_VMX_ENTRY_CTLS:
3149 /*
3150 * The "non-true" VMX capability MSRs are generated from the
3151 * "true" MSRs, so we do not support restoring them directly.
3152 *
3153 * If userspace wants to emulate VMX_BASIC[55]=0, userspace
3154 * should restore the "true" MSRs with the must-be-1 bits
3155 * set according to the SDM Vol 3. A.2 "RESERVED CONTROLS AND
3156 * DEFAULT SETTINGS".
3157 */
3158 return -EINVAL;
3159 case MSR_IA32_VMX_TRUE_PINBASED_CTLS:
3160 case MSR_IA32_VMX_TRUE_PROCBASED_CTLS:
3161 case MSR_IA32_VMX_TRUE_EXIT_CTLS:
3162 case MSR_IA32_VMX_TRUE_ENTRY_CTLS:
3163 case MSR_IA32_VMX_PROCBASED_CTLS2:
3164 return vmx_restore_control_msr(vmx, msr_index, data);
3165 case MSR_IA32_VMX_MISC:
3166 return vmx_restore_vmx_misc(vmx, data);
3167 case MSR_IA32_VMX_CR0_FIXED0:
3168 case MSR_IA32_VMX_CR4_FIXED0:
3169 return vmx_restore_fixed0_msr(vmx, msr_index, data);
3170 case MSR_IA32_VMX_CR0_FIXED1:
3171 case MSR_IA32_VMX_CR4_FIXED1:
3172 /*
3173 * These MSRs are generated based on the vCPU's CPUID, so we
3174 * do not support restoring them directly.
3175 */
3176 return -EINVAL;
3177 case MSR_IA32_VMX_EPT_VPID_CAP:
3178 return vmx_restore_vmx_ept_vpid_cap(vmx, data);
3179 case MSR_IA32_VMX_VMCS_ENUM:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003180 vmx->nested.msrs.vmcs_enum = data;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003181 return 0;
3182 default:
3183 /*
3184 * The rest of the VMX capability MSRs do not support restore.
3185 */
3186 return -EINVAL;
3187 }
3188}
3189
Jan Kiszkacae50132014-01-04 18:47:22 +01003190/* Returns 0 on success, non-0 otherwise. */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003191static int vmx_get_vmx_msr(struct nested_vmx_msrs *msrs, u32 msr_index, u64 *pdata)
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003192{
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003193 switch (msr_index) {
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003194 case MSR_IA32_VMX_BASIC:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003195 *pdata = msrs->basic;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003196 break;
3197 case MSR_IA32_VMX_TRUE_PINBASED_CTLS:
3198 case MSR_IA32_VMX_PINBASED_CTLS:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003199 *pdata = vmx_control_msr(
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003200 msrs->pinbased_ctls_low,
3201 msrs->pinbased_ctls_high);
David Matlack0115f9c2016-11-29 18:14:06 -08003202 if (msr_index == MSR_IA32_VMX_PINBASED_CTLS)
3203 *pdata |= PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003204 break;
3205 case MSR_IA32_VMX_TRUE_PROCBASED_CTLS:
3206 case MSR_IA32_VMX_PROCBASED_CTLS:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003207 *pdata = vmx_control_msr(
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003208 msrs->procbased_ctls_low,
3209 msrs->procbased_ctls_high);
David Matlack0115f9c2016-11-29 18:14:06 -08003210 if (msr_index == MSR_IA32_VMX_PROCBASED_CTLS)
3211 *pdata |= CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003212 break;
3213 case MSR_IA32_VMX_TRUE_EXIT_CTLS:
3214 case MSR_IA32_VMX_EXIT_CTLS:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003215 *pdata = vmx_control_msr(
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003216 msrs->exit_ctls_low,
3217 msrs->exit_ctls_high);
David Matlack0115f9c2016-11-29 18:14:06 -08003218 if (msr_index == MSR_IA32_VMX_EXIT_CTLS)
3219 *pdata |= VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003220 break;
3221 case MSR_IA32_VMX_TRUE_ENTRY_CTLS:
3222 case MSR_IA32_VMX_ENTRY_CTLS:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003223 *pdata = vmx_control_msr(
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003224 msrs->entry_ctls_low,
3225 msrs->entry_ctls_high);
David Matlack0115f9c2016-11-29 18:14:06 -08003226 if (msr_index == MSR_IA32_VMX_ENTRY_CTLS)
3227 *pdata |= VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003228 break;
3229 case MSR_IA32_VMX_MISC:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003230 *pdata = vmx_control_msr(
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003231 msrs->misc_low,
3232 msrs->misc_high);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003233 break;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003234 case MSR_IA32_VMX_CR0_FIXED0:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003235 *pdata = msrs->cr0_fixed0;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003236 break;
3237 case MSR_IA32_VMX_CR0_FIXED1:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003238 *pdata = msrs->cr0_fixed1;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003239 break;
3240 case MSR_IA32_VMX_CR4_FIXED0:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003241 *pdata = msrs->cr4_fixed0;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003242 break;
3243 case MSR_IA32_VMX_CR4_FIXED1:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003244 *pdata = msrs->cr4_fixed1;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003245 break;
3246 case MSR_IA32_VMX_VMCS_ENUM:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003247 *pdata = msrs->vmcs_enum;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003248 break;
3249 case MSR_IA32_VMX_PROCBASED_CTLS2:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003250 *pdata = vmx_control_msr(
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003251 msrs->secondary_ctls_low,
3252 msrs->secondary_ctls_high);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003253 break;
3254 case MSR_IA32_VMX_EPT_VPID_CAP:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003255 *pdata = msrs->ept_caps |
3256 ((u64)msrs->vpid_caps << 32);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003257 break;
Bandan Das27c42a12017-08-03 15:54:42 -04003258 case MSR_IA32_VMX_VMFUNC:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003259 *pdata = msrs->vmfunc_controls;
Bandan Das27c42a12017-08-03 15:54:42 -04003260 break;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003261 default:
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003262 return 1;
Nadav Har'Elb3897a42013-07-08 19:12:35 +08003263 }
3264
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003265 return 0;
3266}
3267
Haozhong Zhang37e4c992016-06-22 14:59:55 +08003268static inline bool vmx_feature_control_msr_valid(struct kvm_vcpu *vcpu,
3269 uint64_t val)
3270{
3271 uint64_t valid_bits = to_vmx(vcpu)->msr_ia32_feature_control_valid_bits;
3272
3273 return !(val & ~valid_bits);
3274}
3275
Tom Lendacky801e4592018-02-21 13:39:51 -06003276static int vmx_get_msr_feature(struct kvm_msr_entry *msr)
3277{
Paolo Bonzini13893092018-02-26 13:40:09 +01003278 switch (msr->index) {
3279 case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC:
3280 if (!nested)
3281 return 1;
3282 return vmx_get_vmx_msr(&vmcs_config.nested, msr->index, &msr->data);
3283 default:
3284 return 1;
3285 }
3286
3287 return 0;
Tom Lendacky801e4592018-02-21 13:39:51 -06003288}
3289
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003290/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08003291 * Reads an msr value (of 'msr_index') into 'pdata'.
3292 * Returns 0 on success, non-0 otherwise.
3293 * Assumes vcpu_load() was already called.
3294 */
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003295static int vmx_get_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003296{
Borislav Petkova6cb0992017-12-20 12:50:28 +01003297 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity26bb0982009-09-07 11:14:12 +03003298 struct shared_msr_entry *msr;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003299
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003300 switch (msr_info->index) {
Avi Kivity05b3e0c2006-12-13 00:33:45 -08003301#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08003302 case MSR_FS_BASE:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003303 msr_info->data = vmcs_readl(GUEST_FS_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003304 break;
3305 case MSR_GS_BASE:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003306 msr_info->data = vmcs_readl(GUEST_GS_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003307 break;
Avi Kivity44ea2b12009-09-06 15:55:37 +03003308 case MSR_KERNEL_GS_BASE:
Borislav Petkova6cb0992017-12-20 12:50:28 +01003309 vmx_load_host_state(vmx);
3310 msr_info->data = vmx->msr_guest_kernel_gs_base;
Avi Kivity44ea2b12009-09-06 15:55:37 +03003311 break;
Avi Kivity26bb0982009-09-07 11:14:12 +03003312#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -08003313 case MSR_EFER:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003314 return kvm_get_msr_common(vcpu, msr_info);
Jaswinder Singh Rajputaf24a4e2009-05-15 18:42:05 +05303315 case MSR_IA32_TSC:
Haozhong Zhangbe7b2632015-10-20 15:39:11 +08003316 msr_info->data = guest_read_tsc(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003317 break;
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01003318 case MSR_IA32_SPEC_CTRL:
3319 if (!msr_info->host_initiated &&
3320 !guest_cpuid_has(vcpu, X86_FEATURE_IBRS) &&
3321 !guest_cpuid_has(vcpu, X86_FEATURE_SPEC_CTRL))
3322 return 1;
3323
3324 msr_info->data = to_vmx(vcpu)->spec_ctrl;
3325 break;
KarimAllah Ahmed28c1c9f2018-02-01 22:59:44 +01003326 case MSR_IA32_ARCH_CAPABILITIES:
3327 if (!msr_info->host_initiated &&
3328 !guest_cpuid_has(vcpu, X86_FEATURE_ARCH_CAPABILITIES))
3329 return 1;
3330 msr_info->data = to_vmx(vcpu)->arch_capabilities;
3331 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003332 case MSR_IA32_SYSENTER_CS:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003333 msr_info->data = vmcs_read32(GUEST_SYSENTER_CS);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003334 break;
3335 case MSR_IA32_SYSENTER_EIP:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003336 msr_info->data = vmcs_readl(GUEST_SYSENTER_EIP);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003337 break;
3338 case MSR_IA32_SYSENTER_ESP:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003339 msr_info->data = vmcs_readl(GUEST_SYSENTER_ESP);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003340 break;
Liu, Jinsong0dd376e2014-02-24 10:56:53 +00003341 case MSR_IA32_BNDCFGS:
Haozhong Zhang691bd432017-07-04 10:27:41 +08003342 if (!kvm_mpx_supported() ||
Radim Krčmářd6321d42017-08-05 00:12:49 +02003343 (!msr_info->host_initiated &&
3344 !guest_cpuid_has(vcpu, X86_FEATURE_MPX)))
Paolo Bonzini93c4adc2014-03-05 23:19:52 +01003345 return 1;
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003346 msr_info->data = vmcs_read64(GUEST_BNDCFGS);
Liu, Jinsong0dd376e2014-02-24 10:56:53 +00003347 break;
Ashok Rajc45dcc72016-06-22 14:59:56 +08003348 case MSR_IA32_MCG_EXT_CTL:
3349 if (!msr_info->host_initiated &&
Borislav Petkova6cb0992017-12-20 12:50:28 +01003350 !(vmx->msr_ia32_feature_control &
Ashok Rajc45dcc72016-06-22 14:59:56 +08003351 FEATURE_CONTROL_LMCE))
Jan Kiszkacae50132014-01-04 18:47:22 +01003352 return 1;
Ashok Rajc45dcc72016-06-22 14:59:56 +08003353 msr_info->data = vcpu->arch.mcg_ext_ctl;
3354 break;
Jan Kiszkacae50132014-01-04 18:47:22 +01003355 case MSR_IA32_FEATURE_CONTROL:
Borislav Petkova6cb0992017-12-20 12:50:28 +01003356 msr_info->data = vmx->msr_ia32_feature_control;
Jan Kiszkacae50132014-01-04 18:47:22 +01003357 break;
3358 case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC:
3359 if (!nested_vmx_allowed(vcpu))
3360 return 1;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003361 return vmx_get_vmx_msr(&vmx->nested.msrs, msr_info->index,
3362 &msr_info->data);
Wanpeng Li20300092014-12-02 19:14:59 +08003363 case MSR_IA32_XSS:
3364 if (!vmx_xsaves_supported())
3365 return 1;
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003366 msr_info->data = vcpu->arch.ia32_xss;
Wanpeng Li20300092014-12-02 19:14:59 +08003367 break;
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003368 case MSR_TSC_AUX:
Radim Krčmářd6321d42017-08-05 00:12:49 +02003369 if (!msr_info->host_initiated &&
3370 !guest_cpuid_has(vcpu, X86_FEATURE_RDTSCP))
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003371 return 1;
3372 /* Otherwise falls through */
Avi Kivity6aa8b732006-12-10 02:21:36 -08003373 default:
Borislav Petkova6cb0992017-12-20 12:50:28 +01003374 msr = find_msr_entry(vmx, msr_info->index);
Avi Kivity3bab1f52006-12-29 16:49:48 -08003375 if (msr) {
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003376 msr_info->data = msr->data;
Avi Kivity3bab1f52006-12-29 16:49:48 -08003377 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003378 }
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003379 return kvm_get_msr_common(vcpu, msr_info);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003380 }
3381
Avi Kivity6aa8b732006-12-10 02:21:36 -08003382 return 0;
3383}
3384
Jan Kiszkacae50132014-01-04 18:47:22 +01003385static void vmx_leave_nested(struct kvm_vcpu *vcpu);
3386
Avi Kivity6aa8b732006-12-10 02:21:36 -08003387/*
3388 * Writes msr value into into the appropriate "register".
3389 * Returns 0 on success, non-0 otherwise.
3390 * Assumes vcpu_load() was already called.
3391 */
Will Auld8fe8ab42012-11-29 12:42:12 -08003392static int vmx_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003393{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003394 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity26bb0982009-09-07 11:14:12 +03003395 struct shared_msr_entry *msr;
Eddie Dong2cc51562007-05-21 07:28:09 +03003396 int ret = 0;
Will Auld8fe8ab42012-11-29 12:42:12 -08003397 u32 msr_index = msr_info->index;
3398 u64 data = msr_info->data;
Eddie Dong2cc51562007-05-21 07:28:09 +03003399
Avi Kivity6aa8b732006-12-10 02:21:36 -08003400 switch (msr_index) {
Avi Kivity3bab1f52006-12-29 16:49:48 -08003401 case MSR_EFER:
Will Auld8fe8ab42012-11-29 12:42:12 -08003402 ret = kvm_set_msr_common(vcpu, msr_info);
Eddie Dong2cc51562007-05-21 07:28:09 +03003403 break;
Avi Kivity16175a72009-03-23 22:13:44 +02003404#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08003405 case MSR_FS_BASE:
Avi Kivity2fb92db2011-04-27 19:42:18 +03003406 vmx_segment_cache_clear(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003407 vmcs_writel(GUEST_FS_BASE, data);
3408 break;
3409 case MSR_GS_BASE:
Avi Kivity2fb92db2011-04-27 19:42:18 +03003410 vmx_segment_cache_clear(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003411 vmcs_writel(GUEST_GS_BASE, data);
3412 break;
Avi Kivity44ea2b12009-09-06 15:55:37 +03003413 case MSR_KERNEL_GS_BASE:
3414 vmx_load_host_state(vmx);
3415 vmx->msr_guest_kernel_gs_base = data;
3416 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003417#endif
3418 case MSR_IA32_SYSENTER_CS:
3419 vmcs_write32(GUEST_SYSENTER_CS, data);
3420 break;
3421 case MSR_IA32_SYSENTER_EIP:
Avi Kivityf5b42c32007-03-06 12:05:53 +02003422 vmcs_writel(GUEST_SYSENTER_EIP, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003423 break;
3424 case MSR_IA32_SYSENTER_ESP:
Avi Kivityf5b42c32007-03-06 12:05:53 +02003425 vmcs_writel(GUEST_SYSENTER_ESP, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003426 break;
Liu, Jinsong0dd376e2014-02-24 10:56:53 +00003427 case MSR_IA32_BNDCFGS:
Haozhong Zhang691bd432017-07-04 10:27:41 +08003428 if (!kvm_mpx_supported() ||
Radim Krčmářd6321d42017-08-05 00:12:49 +02003429 (!msr_info->host_initiated &&
3430 !guest_cpuid_has(vcpu, X86_FEATURE_MPX)))
Paolo Bonzini93c4adc2014-03-05 23:19:52 +01003431 return 1;
Yu Zhangfd8cb432017-08-24 20:27:56 +08003432 if (is_noncanonical_address(data & PAGE_MASK, vcpu) ||
Jim Mattson45316622017-05-23 11:52:54 -07003433 (data & MSR_IA32_BNDCFGS_RSVD))
Avi Kivity6aa8b732006-12-10 02:21:36 -08003434 return 1;
Sheng Yang468d4722008-10-09 16:01:55 +08003435 vmcs_write64(GUEST_BNDCFGS, data);
3436 break;
3437 case MSR_IA32_TSC:
3438 kvm_write_tsc(vcpu, msr_info);
3439 break;
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01003440 case MSR_IA32_SPEC_CTRL:
3441 if (!msr_info->host_initiated &&
3442 !guest_cpuid_has(vcpu, X86_FEATURE_IBRS) &&
3443 !guest_cpuid_has(vcpu, X86_FEATURE_SPEC_CTRL))
3444 return 1;
3445
3446 /* The STIBP bit doesn't fault even if it's not advertised */
3447 if (data & ~(SPEC_CTRL_IBRS | SPEC_CTRL_STIBP))
3448 return 1;
3449
3450 vmx->spec_ctrl = data;
3451
3452 if (!data)
3453 break;
3454
3455 /*
3456 * For non-nested:
3457 * When it's written (to non-zero) for the first time, pass
3458 * it through.
3459 *
3460 * For nested:
3461 * The handling of the MSR bitmap for L2 guests is done in
3462 * nested_vmx_merge_msr_bitmap. We should not touch the
3463 * vmcs02.msr_bitmap here since it gets completely overwritten
3464 * in the merging. We update the vmcs01 here for L1 as well
3465 * since it will end up touching the MSR anyway now.
3466 */
3467 vmx_disable_intercept_for_msr(vmx->vmcs01.msr_bitmap,
3468 MSR_IA32_SPEC_CTRL,
3469 MSR_TYPE_RW);
3470 break;
Ashok Raj15d45072018-02-01 22:59:43 +01003471 case MSR_IA32_PRED_CMD:
3472 if (!msr_info->host_initiated &&
3473 !guest_cpuid_has(vcpu, X86_FEATURE_IBPB) &&
3474 !guest_cpuid_has(vcpu, X86_FEATURE_SPEC_CTRL))
3475 return 1;
3476
3477 if (data & ~PRED_CMD_IBPB)
3478 return 1;
3479
3480 if (!data)
3481 break;
3482
3483 wrmsrl(MSR_IA32_PRED_CMD, PRED_CMD_IBPB);
3484
3485 /*
3486 * For non-nested:
3487 * When it's written (to non-zero) for the first time, pass
3488 * it through.
3489 *
3490 * For nested:
3491 * The handling of the MSR bitmap for L2 guests is done in
3492 * nested_vmx_merge_msr_bitmap. We should not touch the
3493 * vmcs02.msr_bitmap here since it gets completely overwritten
3494 * in the merging.
3495 */
3496 vmx_disable_intercept_for_msr(vmx->vmcs01.msr_bitmap, MSR_IA32_PRED_CMD,
3497 MSR_TYPE_W);
3498 break;
KarimAllah Ahmed28c1c9f2018-02-01 22:59:44 +01003499 case MSR_IA32_ARCH_CAPABILITIES:
3500 if (!msr_info->host_initiated)
3501 return 1;
3502 vmx->arch_capabilities = data;
3503 break;
Sheng Yang468d4722008-10-09 16:01:55 +08003504 case MSR_IA32_CR_PAT:
3505 if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) {
Nadav Amit45666542014-09-18 22:39:44 +03003506 if (!kvm_mtrr_valid(vcpu, MSR_IA32_CR_PAT, data))
3507 return 1;
Sheng Yang468d4722008-10-09 16:01:55 +08003508 vmcs_write64(GUEST_IA32_PAT, data);
3509 vcpu->arch.pat = data;
3510 break;
3511 }
Will Auld8fe8ab42012-11-29 12:42:12 -08003512 ret = kvm_set_msr_common(vcpu, msr_info);
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003513 break;
Will Auldba904632012-11-29 12:42:50 -08003514 case MSR_IA32_TSC_ADJUST:
3515 ret = kvm_set_msr_common(vcpu, msr_info);
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003516 break;
Ashok Rajc45dcc72016-06-22 14:59:56 +08003517 case MSR_IA32_MCG_EXT_CTL:
3518 if ((!msr_info->host_initiated &&
3519 !(to_vmx(vcpu)->msr_ia32_feature_control &
3520 FEATURE_CONTROL_LMCE)) ||
3521 (data & ~MCG_EXT_CTL_LMCE_EN))
3522 return 1;
3523 vcpu->arch.mcg_ext_ctl = data;
3524 break;
Jan Kiszkacae50132014-01-04 18:47:22 +01003525 case MSR_IA32_FEATURE_CONTROL:
Haozhong Zhang37e4c992016-06-22 14:59:55 +08003526 if (!vmx_feature_control_msr_valid(vcpu, data) ||
Haozhong Zhang3b840802016-06-22 14:59:54 +08003527 (to_vmx(vcpu)->msr_ia32_feature_control &
Jan Kiszkacae50132014-01-04 18:47:22 +01003528 FEATURE_CONTROL_LOCKED && !msr_info->host_initiated))
3529 return 1;
Haozhong Zhang3b840802016-06-22 14:59:54 +08003530 vmx->msr_ia32_feature_control = data;
Jan Kiszkacae50132014-01-04 18:47:22 +01003531 if (msr_info->host_initiated && data == 0)
3532 vmx_leave_nested(vcpu);
3533 break;
3534 case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC:
David Matlack62cc6b9d2016-11-29 18:14:07 -08003535 if (!msr_info->host_initiated)
3536 return 1; /* they are read-only */
3537 if (!nested_vmx_allowed(vcpu))
3538 return 1;
3539 return vmx_set_vmx_msr(vcpu, msr_index, data);
Wanpeng Li20300092014-12-02 19:14:59 +08003540 case MSR_IA32_XSS:
3541 if (!vmx_xsaves_supported())
3542 return 1;
3543 /*
3544 * The only supported bit as of Skylake is bit 8, but
3545 * it is not supported on KVM.
3546 */
3547 if (data != 0)
3548 return 1;
3549 vcpu->arch.ia32_xss = data;
3550 if (vcpu->arch.ia32_xss != host_xss)
3551 add_atomic_switch_msr(vmx, MSR_IA32_XSS,
3552 vcpu->arch.ia32_xss, host_xss);
3553 else
3554 clear_atomic_switch_msr(vmx, MSR_IA32_XSS);
3555 break;
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003556 case MSR_TSC_AUX:
Radim Krčmářd6321d42017-08-05 00:12:49 +02003557 if (!msr_info->host_initiated &&
3558 !guest_cpuid_has(vcpu, X86_FEATURE_RDTSCP))
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003559 return 1;
3560 /* Check reserved bit, higher 32 bits should be zero */
3561 if ((data >> 32) != 0)
3562 return 1;
3563 /* Otherwise falls through */
Avi Kivity6aa8b732006-12-10 02:21:36 -08003564 default:
Rusty Russell8b9cf982007-07-30 16:31:43 +10003565 msr = find_msr_entry(vmx, msr_index);
Avi Kivity3bab1f52006-12-29 16:49:48 -08003566 if (msr) {
Andy Honig8b3c3102014-08-27 11:16:44 -07003567 u64 old_msr_data = msr->data;
Avi Kivity3bab1f52006-12-29 16:49:48 -08003568 msr->data = data;
Avi Kivity2225fd52012-04-18 15:03:04 +03003569 if (msr - vmx->guest_msrs < vmx->save_nmsrs) {
3570 preempt_disable();
Andy Honig8b3c3102014-08-27 11:16:44 -07003571 ret = kvm_set_shared_msr(msr->index, msr->data,
3572 msr->mask);
Avi Kivity2225fd52012-04-18 15:03:04 +03003573 preempt_enable();
Andy Honig8b3c3102014-08-27 11:16:44 -07003574 if (ret)
3575 msr->data = old_msr_data;
Avi Kivity2225fd52012-04-18 15:03:04 +03003576 }
Avi Kivity3bab1f52006-12-29 16:49:48 -08003577 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003578 }
Will Auld8fe8ab42012-11-29 12:42:12 -08003579 ret = kvm_set_msr_common(vcpu, msr_info);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003580 }
3581
Eddie Dong2cc51562007-05-21 07:28:09 +03003582 return ret;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003583}
3584
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003585static void vmx_cache_reg(struct kvm_vcpu *vcpu, enum kvm_reg reg)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003586{
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003587 __set_bit(reg, (unsigned long *)&vcpu->arch.regs_avail);
3588 switch (reg) {
3589 case VCPU_REGS_RSP:
3590 vcpu->arch.regs[VCPU_REGS_RSP] = vmcs_readl(GUEST_RSP);
3591 break;
3592 case VCPU_REGS_RIP:
3593 vcpu->arch.regs[VCPU_REGS_RIP] = vmcs_readl(GUEST_RIP);
3594 break;
Avi Kivity6de4f3a2009-05-31 22:58:47 +03003595 case VCPU_EXREG_PDPTR:
3596 if (enable_ept)
3597 ept_save_pdptrs(vcpu);
3598 break;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003599 default:
3600 break;
3601 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08003602}
3603
Avi Kivity6aa8b732006-12-10 02:21:36 -08003604static __init int cpu_has_kvm_support(void)
3605{
Eduardo Habkost6210e372008-11-17 19:03:16 -02003606 return cpu_has_vmx();
Avi Kivity6aa8b732006-12-10 02:21:36 -08003607}
3608
3609static __init int vmx_disabled_by_bios(void)
3610{
3611 u64 msr;
3612
3613 rdmsrl(MSR_IA32_FEATURE_CONTROL, msr);
Shane Wangcafd6652010-04-29 12:09:01 -04003614 if (msr & FEATURE_CONTROL_LOCKED) {
Joseph Cihula23f3e992011-02-08 11:45:56 -08003615 /* launched w/ TXT and VMX disabled */
Shane Wangcafd6652010-04-29 12:09:01 -04003616 if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX)
3617 && tboot_enabled())
3618 return 1;
Joseph Cihula23f3e992011-02-08 11:45:56 -08003619 /* launched w/o TXT and VMX only enabled w/ TXT */
Shane Wangcafd6652010-04-29 12:09:01 -04003620 if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX)
Joseph Cihula23f3e992011-02-08 11:45:56 -08003621 && (msr & FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX)
Shane Wangf9335af2010-11-17 11:40:17 +08003622 && !tboot_enabled()) {
3623 printk(KERN_WARNING "kvm: disable TXT in the BIOS or "
Joseph Cihula23f3e992011-02-08 11:45:56 -08003624 "activate TXT before enabling KVM\n");
Shane Wangcafd6652010-04-29 12:09:01 -04003625 return 1;
Shane Wangf9335af2010-11-17 11:40:17 +08003626 }
Joseph Cihula23f3e992011-02-08 11:45:56 -08003627 /* launched w/o TXT and VMX disabled */
3628 if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX)
3629 && !tboot_enabled())
3630 return 1;
Shane Wangcafd6652010-04-29 12:09:01 -04003631 }
3632
3633 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003634}
3635
Dongxiao Xu7725b892010-05-11 18:29:38 +08003636static void kvm_cpu_vmxon(u64 addr)
3637{
David Hildenbrandfe0e80b2017-03-10 12:47:13 +01003638 cr4_set_bits(X86_CR4_VMXE);
Alexander Shishkin1c5ac212016-03-29 17:43:10 +03003639 intel_pt_handle_vmx(1);
3640
Dongxiao Xu7725b892010-05-11 18:29:38 +08003641 asm volatile (ASM_VMX_VMXON_RAX
3642 : : "a"(&addr), "m"(addr)
3643 : "memory", "cc");
3644}
3645
Radim Krčmář13a34e02014-08-28 15:13:03 +02003646static int hardware_enable(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003647{
3648 int cpu = raw_smp_processor_id();
3649 u64 phys_addr = __pa(per_cpu(vmxarea, cpu));
Shane Wangcafd6652010-04-29 12:09:01 -04003650 u64 old, test_bits;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003651
Andy Lutomirski1e02ce42014-10-24 15:58:08 -07003652 if (cr4_read_shadow() & X86_CR4_VMXE)
Alexander Graf10474ae2009-09-15 11:37:46 +02003653 return -EBUSY;
3654
Nadav Har'Eld462b812011-05-24 15:26:10 +03003655 INIT_LIST_HEAD(&per_cpu(loaded_vmcss_on_cpu, cpu));
Feng Wubf9f6ac2015-09-18 22:29:55 +08003656 INIT_LIST_HEAD(&per_cpu(blocked_vcpu_on_cpu, cpu));
3657 spin_lock_init(&per_cpu(blocked_vcpu_on_cpu_lock, cpu));
Zhang Yanfei8f536b72012-12-06 23:43:34 +08003658
3659 /*
3660 * Now we can enable the vmclear operation in kdump
3661 * since the loaded_vmcss_on_cpu list on this cpu
3662 * has been initialized.
3663 *
3664 * Though the cpu is not in VMX operation now, there
3665 * is no problem to enable the vmclear operation
3666 * for the loaded_vmcss_on_cpu list is empty!
3667 */
3668 crash_enable_local_vmclear(cpu);
3669
Avi Kivity6aa8b732006-12-10 02:21:36 -08003670 rdmsrl(MSR_IA32_FEATURE_CONTROL, old);
Shane Wangcafd6652010-04-29 12:09:01 -04003671
3672 test_bits = FEATURE_CONTROL_LOCKED;
3673 test_bits |= FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
3674 if (tboot_enabled())
3675 test_bits |= FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX;
3676
3677 if ((old & test_bits) != test_bits) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003678 /* enable and lock */
Shane Wangcafd6652010-04-29 12:09:01 -04003679 wrmsrl(MSR_IA32_FEATURE_CONTROL, old | test_bits);
3680 }
David Hildenbrandfe0e80b2017-03-10 12:47:13 +01003681 kvm_cpu_vmxon(phys_addr);
David Hildenbrandfdf288b2017-08-24 20:51:29 +02003682 if (enable_ept)
3683 ept_sync_global();
Alexander Graf10474ae2009-09-15 11:37:46 +02003684
3685 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003686}
3687
Nadav Har'Eld462b812011-05-24 15:26:10 +03003688static void vmclear_local_loaded_vmcss(void)
Avi Kivity543e4242008-05-13 16:22:47 +03003689{
3690 int cpu = raw_smp_processor_id();
Nadav Har'Eld462b812011-05-24 15:26:10 +03003691 struct loaded_vmcs *v, *n;
Avi Kivity543e4242008-05-13 16:22:47 +03003692
Nadav Har'Eld462b812011-05-24 15:26:10 +03003693 list_for_each_entry_safe(v, n, &per_cpu(loaded_vmcss_on_cpu, cpu),
3694 loaded_vmcss_on_cpu_link)
3695 __loaded_vmcs_clear(v);
Avi Kivity543e4242008-05-13 16:22:47 +03003696}
3697
Eduardo Habkost710ff4a2008-11-17 19:03:18 -02003698
3699/* Just like cpu_vmxoff(), but with the __kvm_handle_fault_on_reboot()
3700 * tricks.
3701 */
3702static void kvm_cpu_vmxoff(void)
3703{
3704 asm volatile (__ex(ASM_VMX_VMXOFF) : : : "cc");
Alexander Shishkin1c5ac212016-03-29 17:43:10 +03003705
3706 intel_pt_handle_vmx(0);
David Hildenbrandfe0e80b2017-03-10 12:47:13 +01003707 cr4_clear_bits(X86_CR4_VMXE);
Eduardo Habkost710ff4a2008-11-17 19:03:18 -02003708}
3709
Radim Krčmář13a34e02014-08-28 15:13:03 +02003710static void hardware_disable(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003711{
David Hildenbrandfe0e80b2017-03-10 12:47:13 +01003712 vmclear_local_loaded_vmcss();
3713 kvm_cpu_vmxoff();
Avi Kivity6aa8b732006-12-10 02:21:36 -08003714}
3715
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003716static __init int adjust_vmx_controls(u32 ctl_min, u32 ctl_opt,
Mike Dayd77c26f2007-10-08 09:02:08 -04003717 u32 msr, u32 *result)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003718{
3719 u32 vmx_msr_low, vmx_msr_high;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003720 u32 ctl = ctl_min | ctl_opt;
3721
3722 rdmsr(msr, vmx_msr_low, vmx_msr_high);
3723
3724 ctl &= vmx_msr_high; /* bit == 0 in high word ==> must be zero */
3725 ctl |= vmx_msr_low; /* bit == 1 in low word ==> must be one */
3726
3727 /* Ensure minimum (required) set of control bits are supported. */
3728 if (ctl_min & ~ctl)
Yang, Sheng002c7f72007-07-31 14:23:01 +03003729 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003730
3731 *result = ctl;
3732 return 0;
3733}
3734
Avi Kivity110312c2010-12-21 12:54:20 +02003735static __init bool allow_1_setting(u32 msr, u32 ctl)
3736{
3737 u32 vmx_msr_low, vmx_msr_high;
3738
3739 rdmsr(msr, vmx_msr_low, vmx_msr_high);
3740 return vmx_msr_high & ctl;
3741}
3742
Yang, Sheng002c7f72007-07-31 14:23:01 +03003743static __init int setup_vmcs_config(struct vmcs_config *vmcs_conf)
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003744{
3745 u32 vmx_msr_low, vmx_msr_high;
Sheng Yangd56f5462008-04-25 10:13:16 +08003746 u32 min, opt, min2, opt2;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003747 u32 _pin_based_exec_control = 0;
3748 u32 _cpu_based_exec_control = 0;
Sheng Yangf78e0e22007-10-29 09:40:42 +08003749 u32 _cpu_based_2nd_exec_control = 0;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003750 u32 _vmexit_control = 0;
3751 u32 _vmentry_control = 0;
3752
Paolo Bonzini13893092018-02-26 13:40:09 +01003753 memset(vmcs_conf, 0, sizeof(*vmcs_conf));
Raghavendra K T10166742012-02-07 23:19:20 +05303754 min = CPU_BASED_HLT_EXITING |
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003755#ifdef CONFIG_X86_64
3756 CPU_BASED_CR8_LOAD_EXITING |
3757 CPU_BASED_CR8_STORE_EXITING |
3758#endif
Sheng Yangd56f5462008-04-25 10:13:16 +08003759 CPU_BASED_CR3_LOAD_EXITING |
3760 CPU_BASED_CR3_STORE_EXITING |
Quan Xu8eb73e2d2017-12-12 16:44:21 +08003761 CPU_BASED_UNCOND_IO_EXITING |
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003762 CPU_BASED_MOV_DR_EXITING |
Marcelo Tosattia7052892008-09-23 13:18:35 -03003763 CPU_BASED_USE_TSC_OFFSETING |
Wanpeng Li4d5422c2018-03-12 04:53:02 -07003764 CPU_BASED_MWAIT_EXITING |
3765 CPU_BASED_MONITOR_EXITING |
Avi Kivityfee84b02011-11-10 14:57:25 +02003766 CPU_BASED_INVLPG_EXITING |
3767 CPU_BASED_RDPMC_EXITING;
Anthony Liguori443381a2010-12-06 10:53:38 -06003768
Sheng Yangf78e0e22007-10-29 09:40:42 +08003769 opt = CPU_BASED_TPR_SHADOW |
Sheng Yang25c5f222008-03-28 13:18:56 +08003770 CPU_BASED_USE_MSR_BITMAPS |
Sheng Yangf78e0e22007-10-29 09:40:42 +08003771 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003772 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PROCBASED_CTLS,
3773 &_cpu_based_exec_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03003774 return -EIO;
Yang, Sheng6e5d8652007-09-12 18:03:11 +08003775#ifdef CONFIG_X86_64
3776 if ((_cpu_based_exec_control & CPU_BASED_TPR_SHADOW))
3777 _cpu_based_exec_control &= ~CPU_BASED_CR8_LOAD_EXITING &
3778 ~CPU_BASED_CR8_STORE_EXITING;
3779#endif
Sheng Yangf78e0e22007-10-29 09:40:42 +08003780 if (_cpu_based_exec_control & CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) {
Sheng Yangd56f5462008-04-25 10:13:16 +08003781 min2 = 0;
3782 opt2 = SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
Yang Zhang8d146952013-01-25 10:18:50 +08003783 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
Sheng Yang2384d2b2008-01-17 15:14:33 +08003784 SECONDARY_EXEC_WBINVD_EXITING |
Sheng Yangd56f5462008-04-25 10:13:16 +08003785 SECONDARY_EXEC_ENABLE_VPID |
Nitin A Kamble3a624e22009-06-08 11:34:16 -07003786 SECONDARY_EXEC_ENABLE_EPT |
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08003787 SECONDARY_EXEC_UNRESTRICTED_GUEST |
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003788 SECONDARY_EXEC_PAUSE_LOOP_EXITING |
Paolo Bonzini0367f202016-07-12 10:44:55 +02003789 SECONDARY_EXEC_DESC |
Mao, Junjiead756a12012-07-02 01:18:48 +00003790 SECONDARY_EXEC_RDTSCP |
Yang Zhang83d4c282013-01-25 10:18:49 +08003791 SECONDARY_EXEC_ENABLE_INVPCID |
Yang Zhangc7c9c562013-01-25 10:18:51 +08003792 SECONDARY_EXEC_APIC_REGISTER_VIRT |
Abel Gordonabc4fc52013-04-18 14:35:25 +03003793 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY |
Wanpeng Li20300092014-12-02 19:14:59 +08003794 SECONDARY_EXEC_SHADOW_VMCS |
Kai Huang843e4332015-01-28 10:54:28 +08003795 SECONDARY_EXEC_XSAVES |
David Hildenbrand736fdf72017-08-24 20:51:37 +02003796 SECONDARY_EXEC_RDSEED_EXITING |
3797 SECONDARY_EXEC_RDRAND_EXITING |
Xiao Guangrong8b3e34e2015-09-09 14:05:51 +08003798 SECONDARY_EXEC_ENABLE_PML |
Bandan Das2a499e42017-08-03 15:54:41 -04003799 SECONDARY_EXEC_TSC_SCALING |
3800 SECONDARY_EXEC_ENABLE_VMFUNC;
Sheng Yangd56f5462008-04-25 10:13:16 +08003801 if (adjust_vmx_controls(min2, opt2,
3802 MSR_IA32_VMX_PROCBASED_CTLS2,
Sheng Yangf78e0e22007-10-29 09:40:42 +08003803 &_cpu_based_2nd_exec_control) < 0)
3804 return -EIO;
3805 }
3806#ifndef CONFIG_X86_64
3807 if (!(_cpu_based_2nd_exec_control &
3808 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES))
3809 _cpu_based_exec_control &= ~CPU_BASED_TPR_SHADOW;
3810#endif
Yang Zhang83d4c282013-01-25 10:18:49 +08003811
3812 if (!(_cpu_based_exec_control & CPU_BASED_TPR_SHADOW))
3813 _cpu_based_2nd_exec_control &= ~(
Yang Zhang8d146952013-01-25 10:18:50 +08003814 SECONDARY_EXEC_APIC_REGISTER_VIRT |
Yang Zhangc7c9c562013-01-25 10:18:51 +08003815 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
3816 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
Yang Zhang83d4c282013-01-25 10:18:49 +08003817
Wanpeng Li61f1dd92017-10-18 16:02:19 -07003818 rdmsr_safe(MSR_IA32_VMX_EPT_VPID_CAP,
3819 &vmx_capability.ept, &vmx_capability.vpid);
3820
Sheng Yangd56f5462008-04-25 10:13:16 +08003821 if (_cpu_based_2nd_exec_control & SECONDARY_EXEC_ENABLE_EPT) {
Marcelo Tosattia7052892008-09-23 13:18:35 -03003822 /* CR3 accesses and invlpg don't need to cause VM Exits when EPT
3823 enabled */
Gleb Natapov5fff7d22009-08-27 18:41:30 +03003824 _cpu_based_exec_control &= ~(CPU_BASED_CR3_LOAD_EXITING |
3825 CPU_BASED_CR3_STORE_EXITING |
3826 CPU_BASED_INVLPG_EXITING);
Wanpeng Li61f1dd92017-10-18 16:02:19 -07003827 } else if (vmx_capability.ept) {
3828 vmx_capability.ept = 0;
3829 pr_warn_once("EPT CAP should not exist if not support "
3830 "1-setting enable EPT VM-execution control\n");
3831 }
3832 if (!(_cpu_based_2nd_exec_control & SECONDARY_EXEC_ENABLE_VPID) &&
3833 vmx_capability.vpid) {
3834 vmx_capability.vpid = 0;
3835 pr_warn_once("VPID CAP should not exist if not support "
3836 "1-setting enable VPID VM-execution control\n");
Sheng Yangd56f5462008-04-25 10:13:16 +08003837 }
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003838
Paolo Bonzini91fa0f82016-06-15 20:55:08 +02003839 min = VM_EXIT_SAVE_DEBUG_CONTROLS | VM_EXIT_ACK_INTR_ON_EXIT;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003840#ifdef CONFIG_X86_64
3841 min |= VM_EXIT_HOST_ADDR_SPACE_SIZE;
3842#endif
Yang Zhanga547c6d2013-04-11 19:25:10 +08003843 opt = VM_EXIT_SAVE_IA32_PAT | VM_EXIT_LOAD_IA32_PAT |
Paolo Bonzini91fa0f82016-06-15 20:55:08 +02003844 VM_EXIT_CLEAR_BNDCFGS;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003845 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_EXIT_CTLS,
3846 &_vmexit_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03003847 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003848
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01003849 min = PIN_BASED_EXT_INTR_MASK | PIN_BASED_NMI_EXITING;
3850 opt = PIN_BASED_VIRTUAL_NMIS | PIN_BASED_POSTED_INTR |
3851 PIN_BASED_VMX_PREEMPTION_TIMER;
Yang Zhang01e439b2013-04-11 19:25:12 +08003852 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PINBASED_CTLS,
3853 &_pin_based_exec_control) < 0)
3854 return -EIO;
3855
Paolo Bonzini1c17c3e2016-07-08 11:53:38 +02003856 if (cpu_has_broken_vmx_preemption_timer())
3857 _pin_based_exec_control &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
Yang Zhang01e439b2013-04-11 19:25:12 +08003858 if (!(_cpu_based_2nd_exec_control &
Paolo Bonzini91fa0f82016-06-15 20:55:08 +02003859 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY))
Yang Zhang01e439b2013-04-11 19:25:12 +08003860 _pin_based_exec_control &= ~PIN_BASED_POSTED_INTR;
3861
Paolo Bonzinic845f9c2014-02-21 10:55:44 +01003862 min = VM_ENTRY_LOAD_DEBUG_CONTROLS;
Liu, Jinsongda8999d2014-02-24 10:55:46 +00003863 opt = VM_ENTRY_LOAD_IA32_PAT | VM_ENTRY_LOAD_BNDCFGS;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003864 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_ENTRY_CTLS,
3865 &_vmentry_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03003866 return -EIO;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003867
Nguyen Anh Quynhc68876f2006-12-29 16:49:54 -08003868 rdmsr(MSR_IA32_VMX_BASIC, vmx_msr_low, vmx_msr_high);
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003869
3870 /* IA-32 SDM Vol 3B: VMCS size is never greater than 4kB. */
3871 if ((vmx_msr_high & 0x1fff) > PAGE_SIZE)
Yang, Sheng002c7f72007-07-31 14:23:01 +03003872 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003873
3874#ifdef CONFIG_X86_64
3875 /* IA-32 SDM Vol 3B: 64-bit CPUs always have VMX_BASIC_MSR[48]==0. */
3876 if (vmx_msr_high & (1u<<16))
Yang, Sheng002c7f72007-07-31 14:23:01 +03003877 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003878#endif
3879
3880 /* Require Write-Back (WB) memory type for VMCS accesses. */
3881 if (((vmx_msr_high >> 18) & 15) != 6)
Yang, Sheng002c7f72007-07-31 14:23:01 +03003882 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003883
Yang, Sheng002c7f72007-07-31 14:23:01 +03003884 vmcs_conf->size = vmx_msr_high & 0x1fff;
Paolo Bonzini16cb0252016-09-05 15:57:00 +02003885 vmcs_conf->order = get_order(vmcs_conf->size);
Jan Dakinevich9ac7e3e2016-09-04 21:23:15 +03003886 vmcs_conf->basic_cap = vmx_msr_high & ~0x1fff;
Yang, Sheng002c7f72007-07-31 14:23:01 +03003887 vmcs_conf->revision_id = vmx_msr_low;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003888
Yang, Sheng002c7f72007-07-31 14:23:01 +03003889 vmcs_conf->pin_based_exec_ctrl = _pin_based_exec_control;
3890 vmcs_conf->cpu_based_exec_ctrl = _cpu_based_exec_control;
Sheng Yangf78e0e22007-10-29 09:40:42 +08003891 vmcs_conf->cpu_based_2nd_exec_ctrl = _cpu_based_2nd_exec_control;
Yang, Sheng002c7f72007-07-31 14:23:01 +03003892 vmcs_conf->vmexit_ctrl = _vmexit_control;
3893 vmcs_conf->vmentry_ctrl = _vmentry_control;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003894
Avi Kivity110312c2010-12-21 12:54:20 +02003895 cpu_has_load_ia32_efer =
3896 allow_1_setting(MSR_IA32_VMX_ENTRY_CTLS,
3897 VM_ENTRY_LOAD_IA32_EFER)
3898 && allow_1_setting(MSR_IA32_VMX_EXIT_CTLS,
3899 VM_EXIT_LOAD_IA32_EFER);
3900
Gleb Natapov8bf00a52011-10-05 14:01:22 +02003901 cpu_has_load_perf_global_ctrl =
3902 allow_1_setting(MSR_IA32_VMX_ENTRY_CTLS,
3903 VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL)
3904 && allow_1_setting(MSR_IA32_VMX_EXIT_CTLS,
3905 VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL);
3906
3907 /*
3908 * Some cpus support VM_ENTRY_(LOAD|SAVE)_IA32_PERF_GLOBAL_CTRL
Andrea Gelminibb3541f2016-05-21 14:14:44 +02003909 * but due to errata below it can't be used. Workaround is to use
Gleb Natapov8bf00a52011-10-05 14:01:22 +02003910 * msr load mechanism to switch IA32_PERF_GLOBAL_CTRL.
3911 *
3912 * VM Exit May Incorrectly Clear IA32_PERF_GLOBAL_CTRL [34:32]
3913 *
3914 * AAK155 (model 26)
3915 * AAP115 (model 30)
3916 * AAT100 (model 37)
3917 * BC86,AAY89,BD102 (model 44)
3918 * BA97 (model 46)
3919 *
3920 */
3921 if (cpu_has_load_perf_global_ctrl && boot_cpu_data.x86 == 0x6) {
3922 switch (boot_cpu_data.x86_model) {
3923 case 26:
3924 case 30:
3925 case 37:
3926 case 44:
3927 case 46:
3928 cpu_has_load_perf_global_ctrl = false;
3929 printk_once(KERN_WARNING"kvm: VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL "
3930 "does not work properly. Using workaround\n");
3931 break;
3932 default:
3933 break;
3934 }
3935 }
3936
Borislav Petkov782511b2016-04-04 22:25:03 +02003937 if (boot_cpu_has(X86_FEATURE_XSAVES))
Wanpeng Li20300092014-12-02 19:14:59 +08003938 rdmsrl(MSR_IA32_XSS, host_xss);
3939
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003940 return 0;
Nguyen Anh Quynhc68876f2006-12-29 16:49:54 -08003941}
Avi Kivity6aa8b732006-12-10 02:21:36 -08003942
3943static struct vmcs *alloc_vmcs_cpu(int cpu)
3944{
3945 int node = cpu_to_node(cpu);
3946 struct page *pages;
3947 struct vmcs *vmcs;
3948
Vlastimil Babka96db8002015-09-08 15:03:50 -07003949 pages = __alloc_pages_node(node, GFP_KERNEL, vmcs_config.order);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003950 if (!pages)
3951 return NULL;
3952 vmcs = page_address(pages);
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003953 memset(vmcs, 0, vmcs_config.size);
3954 vmcs->revision_id = vmcs_config.revision_id; /* vmcs revision id */
Avi Kivity6aa8b732006-12-10 02:21:36 -08003955 return vmcs;
3956}
3957
Avi Kivity6aa8b732006-12-10 02:21:36 -08003958static void free_vmcs(struct vmcs *vmcs)
3959{
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003960 free_pages((unsigned long)vmcs, vmcs_config.order);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003961}
3962
Nadav Har'Eld462b812011-05-24 15:26:10 +03003963/*
3964 * Free a VMCS, but before that VMCLEAR it on the CPU where it was last loaded
3965 */
3966static void free_loaded_vmcs(struct loaded_vmcs *loaded_vmcs)
3967{
3968 if (!loaded_vmcs->vmcs)
3969 return;
3970 loaded_vmcs_clear(loaded_vmcs);
3971 free_vmcs(loaded_vmcs->vmcs);
3972 loaded_vmcs->vmcs = NULL;
Paolo Bonzini904e14f2018-01-16 16:51:18 +01003973 if (loaded_vmcs->msr_bitmap)
3974 free_page((unsigned long)loaded_vmcs->msr_bitmap);
Jim Mattson355f4fb2016-10-28 08:29:39 -07003975 WARN_ON(loaded_vmcs->shadow_vmcs != NULL);
Nadav Har'Eld462b812011-05-24 15:26:10 +03003976}
3977
Paolo Bonzinif21f1652018-01-11 12:16:15 +01003978static struct vmcs *alloc_vmcs(void)
3979{
3980 return alloc_vmcs_cpu(raw_smp_processor_id());
3981}
3982
3983static int alloc_loaded_vmcs(struct loaded_vmcs *loaded_vmcs)
3984{
3985 loaded_vmcs->vmcs = alloc_vmcs();
3986 if (!loaded_vmcs->vmcs)
3987 return -ENOMEM;
3988
3989 loaded_vmcs->shadow_vmcs = NULL;
3990 loaded_vmcs_init(loaded_vmcs);
Paolo Bonzini904e14f2018-01-16 16:51:18 +01003991
3992 if (cpu_has_vmx_msr_bitmap()) {
3993 loaded_vmcs->msr_bitmap = (unsigned long *)__get_free_page(GFP_KERNEL);
3994 if (!loaded_vmcs->msr_bitmap)
3995 goto out_vmcs;
3996 memset(loaded_vmcs->msr_bitmap, 0xff, PAGE_SIZE);
3997 }
Paolo Bonzinif21f1652018-01-11 12:16:15 +01003998 return 0;
Paolo Bonzini904e14f2018-01-16 16:51:18 +01003999
4000out_vmcs:
4001 free_loaded_vmcs(loaded_vmcs);
4002 return -ENOMEM;
Paolo Bonzinif21f1652018-01-11 12:16:15 +01004003}
4004
Sam Ravnborg39959582007-06-01 00:47:13 -07004005static void free_kvm_area(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004006{
4007 int cpu;
4008
Zachary Amsden3230bb42009-09-29 11:38:37 -10004009 for_each_possible_cpu(cpu) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08004010 free_vmcs(per_cpu(vmxarea, cpu));
Zachary Amsden3230bb42009-09-29 11:38:37 -10004011 per_cpu(vmxarea, cpu) = NULL;
4012 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08004013}
4014
Jim Mattsond37f4262017-12-22 12:12:16 -08004015enum vmcs_field_width {
4016 VMCS_FIELD_WIDTH_U16 = 0,
4017 VMCS_FIELD_WIDTH_U64 = 1,
4018 VMCS_FIELD_WIDTH_U32 = 2,
4019 VMCS_FIELD_WIDTH_NATURAL_WIDTH = 3
Jim Mattson85fd5142017-07-07 12:51:41 -07004020};
4021
Jim Mattsond37f4262017-12-22 12:12:16 -08004022static inline int vmcs_field_width(unsigned long field)
Jim Mattson85fd5142017-07-07 12:51:41 -07004023{
4024 if (0x1 & field) /* the *_HIGH fields are all 32 bit */
Jim Mattsond37f4262017-12-22 12:12:16 -08004025 return VMCS_FIELD_WIDTH_U32;
Jim Mattson85fd5142017-07-07 12:51:41 -07004026 return (field >> 13) & 0x3 ;
4027}
4028
4029static inline int vmcs_field_readonly(unsigned long field)
4030{
4031 return (((field >> 10) & 0x3) == 1);
4032}
4033
Bandan Dasfe2b2012014-04-21 15:20:14 -04004034static void init_vmcs_shadow_fields(void)
4035{
4036 int i, j;
4037
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004038 for (i = j = 0; i < max_shadow_read_only_fields; i++) {
4039 u16 field = shadow_read_only_fields[i];
Jim Mattsond37f4262017-12-22 12:12:16 -08004040 if (vmcs_field_width(field) == VMCS_FIELD_WIDTH_U64 &&
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004041 (i + 1 == max_shadow_read_only_fields ||
4042 shadow_read_only_fields[i + 1] != field + 1))
4043 pr_err("Missing field from shadow_read_only_field %x\n",
4044 field + 1);
4045
4046 clear_bit(field, vmx_vmread_bitmap);
4047#ifdef CONFIG_X86_64
4048 if (field & 1)
4049 continue;
4050#endif
4051 if (j < i)
4052 shadow_read_only_fields[j] = field;
4053 j++;
4054 }
4055 max_shadow_read_only_fields = j;
Bandan Dasfe2b2012014-04-21 15:20:14 -04004056
4057 for (i = j = 0; i < max_shadow_read_write_fields; i++) {
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004058 u16 field = shadow_read_write_fields[i];
Jim Mattsond37f4262017-12-22 12:12:16 -08004059 if (vmcs_field_width(field) == VMCS_FIELD_WIDTH_U64 &&
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004060 (i + 1 == max_shadow_read_write_fields ||
4061 shadow_read_write_fields[i + 1] != field + 1))
4062 pr_err("Missing field from shadow_read_write_field %x\n",
4063 field + 1);
4064
Paolo Bonzinic5d167b2017-12-13 11:05:19 +01004065 /*
4066 * PML and the preemption timer can be emulated, but the
4067 * processor cannot vmwrite to fields that don't exist
4068 * on bare metal.
4069 */
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004070 switch (field) {
Paolo Bonzinic5d167b2017-12-13 11:05:19 +01004071 case GUEST_PML_INDEX:
4072 if (!cpu_has_vmx_pml())
4073 continue;
4074 break;
4075 case VMX_PREEMPTION_TIMER_VALUE:
4076 if (!cpu_has_vmx_preemption_timer())
4077 continue;
4078 break;
4079 case GUEST_INTR_STATUS:
4080 if (!cpu_has_vmx_apicv())
Bandan Dasfe2b2012014-04-21 15:20:14 -04004081 continue;
4082 break;
4083 default:
4084 break;
4085 }
4086
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004087 clear_bit(field, vmx_vmwrite_bitmap);
4088 clear_bit(field, vmx_vmread_bitmap);
4089#ifdef CONFIG_X86_64
4090 if (field & 1)
4091 continue;
4092#endif
Bandan Dasfe2b2012014-04-21 15:20:14 -04004093 if (j < i)
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004094 shadow_read_write_fields[j] = field;
Bandan Dasfe2b2012014-04-21 15:20:14 -04004095 j++;
4096 }
4097 max_shadow_read_write_fields = j;
Bandan Dasfe2b2012014-04-21 15:20:14 -04004098}
4099
Avi Kivity6aa8b732006-12-10 02:21:36 -08004100static __init int alloc_kvm_area(void)
4101{
4102 int cpu;
4103
Zachary Amsden3230bb42009-09-29 11:38:37 -10004104 for_each_possible_cpu(cpu) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08004105 struct vmcs *vmcs;
4106
4107 vmcs = alloc_vmcs_cpu(cpu);
4108 if (!vmcs) {
4109 free_kvm_area();
4110 return -ENOMEM;
4111 }
4112
4113 per_cpu(vmxarea, cpu) = vmcs;
4114 }
4115 return 0;
4116}
4117
Gleb Natapov91b0aa22013-01-21 15:36:47 +02004118static void fix_pmode_seg(struct kvm_vcpu *vcpu, int seg,
Gleb Natapovd99e4152012-12-20 16:57:45 +02004119 struct kvm_segment *save)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004120{
Gleb Natapovd99e4152012-12-20 16:57:45 +02004121 if (!emulate_invalid_guest_state) {
4122 /*
4123 * CS and SS RPL should be equal during guest entry according
4124 * to VMX spec, but in reality it is not always so. Since vcpu
4125 * is in the middle of the transition from real mode to
4126 * protected mode it is safe to assume that RPL 0 is a good
4127 * default value.
4128 */
4129 if (seg == VCPU_SREG_CS || seg == VCPU_SREG_SS)
Nadav Amitb32a9912015-03-29 16:33:04 +03004130 save->selector &= ~SEGMENT_RPL_MASK;
4131 save->dpl = save->selector & SEGMENT_RPL_MASK;
Gleb Natapovd99e4152012-12-20 16:57:45 +02004132 save->s = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004133 }
Gleb Natapovd99e4152012-12-20 16:57:45 +02004134 vmx_set_segment(vcpu, save, seg);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004135}
4136
4137static void enter_pmode(struct kvm_vcpu *vcpu)
4138{
4139 unsigned long flags;
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03004140 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004141
Gleb Natapovd99e4152012-12-20 16:57:45 +02004142 /*
4143 * Update real mode segment cache. It may be not up-to-date if sement
4144 * register was written while vcpu was in a guest mode.
4145 */
4146 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_ES], VCPU_SREG_ES);
4147 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_DS], VCPU_SREG_DS);
4148 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_FS], VCPU_SREG_FS);
4149 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_GS], VCPU_SREG_GS);
4150 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_SS], VCPU_SREG_SS);
4151 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_CS], VCPU_SREG_CS);
4152
Avi Kivity7ffd92c2009-06-09 14:10:45 +03004153 vmx->rmode.vm86_active = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004154
Avi Kivity2fb92db2011-04-27 19:42:18 +03004155 vmx_segment_cache_clear(vmx);
4156
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03004157 vmx_set_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_TR], VCPU_SREG_TR);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004158
4159 flags = vmcs_readl(GUEST_RFLAGS);
Avi Kivity78ac8b42010-04-08 18:19:35 +03004160 flags &= RMODE_GUEST_OWNED_EFLAGS_BITS;
4161 flags |= vmx->rmode.save_rflags & ~RMODE_GUEST_OWNED_EFLAGS_BITS;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004162 vmcs_writel(GUEST_RFLAGS, flags);
4163
Rusty Russell66aee912007-07-17 23:34:16 +10004164 vmcs_writel(GUEST_CR4, (vmcs_readl(GUEST_CR4) & ~X86_CR4_VME) |
4165 (vmcs_readl(CR4_READ_SHADOW) & X86_CR4_VME));
Avi Kivity6aa8b732006-12-10 02:21:36 -08004166
4167 update_exception_bitmap(vcpu);
4168
Gleb Natapov91b0aa22013-01-21 15:36:47 +02004169 fix_pmode_seg(vcpu, VCPU_SREG_CS, &vmx->rmode.segs[VCPU_SREG_CS]);
4170 fix_pmode_seg(vcpu, VCPU_SREG_SS, &vmx->rmode.segs[VCPU_SREG_SS]);
4171 fix_pmode_seg(vcpu, VCPU_SREG_ES, &vmx->rmode.segs[VCPU_SREG_ES]);
4172 fix_pmode_seg(vcpu, VCPU_SREG_DS, &vmx->rmode.segs[VCPU_SREG_DS]);
4173 fix_pmode_seg(vcpu, VCPU_SREG_FS, &vmx->rmode.segs[VCPU_SREG_FS]);
4174 fix_pmode_seg(vcpu, VCPU_SREG_GS, &vmx->rmode.segs[VCPU_SREG_GS]);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004175}
4176
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03004177static void fix_rmode_seg(int seg, struct kvm_segment *save)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004178{
Mathias Krause772e0312012-08-30 01:30:19 +02004179 const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
Gleb Natapovd99e4152012-12-20 16:57:45 +02004180 struct kvm_segment var = *save;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004181
Gleb Natapovd99e4152012-12-20 16:57:45 +02004182 var.dpl = 0x3;
4183 if (seg == VCPU_SREG_CS)
4184 var.type = 0x3;
4185
4186 if (!emulate_invalid_guest_state) {
4187 var.selector = var.base >> 4;
4188 var.base = var.base & 0xffff0;
4189 var.limit = 0xffff;
4190 var.g = 0;
4191 var.db = 0;
4192 var.present = 1;
4193 var.s = 1;
4194 var.l = 0;
4195 var.unusable = 0;
4196 var.type = 0x3;
4197 var.avl = 0;
4198 if (save->base & 0xf)
4199 printk_once(KERN_WARNING "kvm: segment base is not "
4200 "paragraph aligned when entering "
4201 "protected mode (seg=%d)", seg);
4202 }
4203
4204 vmcs_write16(sf->selector, var.selector);
Chao Peng96794e42017-02-21 03:50:01 -05004205 vmcs_writel(sf->base, var.base);
Gleb Natapovd99e4152012-12-20 16:57:45 +02004206 vmcs_write32(sf->limit, var.limit);
4207 vmcs_write32(sf->ar_bytes, vmx_segment_access_rights(&var));
Avi Kivity6aa8b732006-12-10 02:21:36 -08004208}
4209
4210static void enter_rmode(struct kvm_vcpu *vcpu)
4211{
4212 unsigned long flags;
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03004213 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004214
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03004215 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_TR], VCPU_SREG_TR);
4216 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_ES], VCPU_SREG_ES);
4217 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_DS], VCPU_SREG_DS);
4218 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_FS], VCPU_SREG_FS);
4219 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_GS], VCPU_SREG_GS);
Gleb Natapovc6ad11532012-12-12 19:10:51 +02004220 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_SS], VCPU_SREG_SS);
4221 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_CS], VCPU_SREG_CS);
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03004222
Avi Kivity7ffd92c2009-06-09 14:10:45 +03004223 vmx->rmode.vm86_active = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004224
Gleb Natapov776e58e2011-03-13 12:34:27 +02004225 /*
4226 * Very old userspace does not call KVM_SET_TSS_ADDR before entering
Jan Kiszka4918c6c2013-03-15 08:38:56 +01004227 * vcpu. Warn the user that an update is overdue.
Gleb Natapov776e58e2011-03-13 12:34:27 +02004228 */
Jan Kiszka4918c6c2013-03-15 08:38:56 +01004229 if (!vcpu->kvm->arch.tss_addr)
Gleb Natapov776e58e2011-03-13 12:34:27 +02004230 printk_once(KERN_WARNING "kvm: KVM_SET_TSS_ADDR need to be "
4231 "called before entering vcpu\n");
Gleb Natapov776e58e2011-03-13 12:34:27 +02004232
Avi Kivity2fb92db2011-04-27 19:42:18 +03004233 vmx_segment_cache_clear(vmx);
4234
Jan Kiszka4918c6c2013-03-15 08:38:56 +01004235 vmcs_writel(GUEST_TR_BASE, vcpu->kvm->arch.tss_addr);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004236 vmcs_write32(GUEST_TR_LIMIT, RMODE_TSS_SIZE - 1);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004237 vmcs_write32(GUEST_TR_AR_BYTES, 0x008b);
4238
4239 flags = vmcs_readl(GUEST_RFLAGS);
Avi Kivity78ac8b42010-04-08 18:19:35 +03004240 vmx->rmode.save_rflags = flags;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004241
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +01004242 flags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004243
4244 vmcs_writel(GUEST_RFLAGS, flags);
Rusty Russell66aee912007-07-17 23:34:16 +10004245 vmcs_writel(GUEST_CR4, vmcs_readl(GUEST_CR4) | X86_CR4_VME);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004246 update_exception_bitmap(vcpu);
4247
Gleb Natapovd99e4152012-12-20 16:57:45 +02004248 fix_rmode_seg(VCPU_SREG_SS, &vmx->rmode.segs[VCPU_SREG_SS]);
4249 fix_rmode_seg(VCPU_SREG_CS, &vmx->rmode.segs[VCPU_SREG_CS]);
4250 fix_rmode_seg(VCPU_SREG_ES, &vmx->rmode.segs[VCPU_SREG_ES]);
4251 fix_rmode_seg(VCPU_SREG_DS, &vmx->rmode.segs[VCPU_SREG_DS]);
4252 fix_rmode_seg(VCPU_SREG_GS, &vmx->rmode.segs[VCPU_SREG_GS]);
4253 fix_rmode_seg(VCPU_SREG_FS, &vmx->rmode.segs[VCPU_SREG_FS]);
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03004254
Eddie Dong8668a3c2007-10-10 14:26:45 +08004255 kvm_mmu_reset_context(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004256}
4257
Amit Shah401d10d2009-02-20 22:53:37 +05304258static void vmx_set_efer(struct kvm_vcpu *vcpu, u64 efer)
4259{
4260 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity26bb0982009-09-07 11:14:12 +03004261 struct shared_msr_entry *msr = find_msr_entry(vmx, MSR_EFER);
4262
4263 if (!msr)
4264 return;
Amit Shah401d10d2009-02-20 22:53:37 +05304265
Avi Kivity44ea2b12009-09-06 15:55:37 +03004266 /*
4267 * Force kernel_gs_base reloading before EFER changes, as control
4268 * of this msr depends on is_long_mode().
4269 */
4270 vmx_load_host_state(to_vmx(vcpu));
Avi Kivityf6801df2010-01-21 15:31:50 +02004271 vcpu->arch.efer = efer;
Amit Shah401d10d2009-02-20 22:53:37 +05304272 if (efer & EFER_LMA) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02004273 vm_entry_controls_setbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
Amit Shah401d10d2009-02-20 22:53:37 +05304274 msr->data = efer;
4275 } else {
Gleb Natapov2961e8762013-11-25 15:37:13 +02004276 vm_entry_controls_clearbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
Amit Shah401d10d2009-02-20 22:53:37 +05304277
4278 msr->data = efer & ~EFER_LME;
4279 }
4280 setup_msrs(vmx);
4281}
4282
Avi Kivity05b3e0c2006-12-13 00:33:45 -08004283#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08004284
4285static void enter_lmode(struct kvm_vcpu *vcpu)
4286{
4287 u32 guest_tr_ar;
4288
Avi Kivity2fb92db2011-04-27 19:42:18 +03004289 vmx_segment_cache_clear(to_vmx(vcpu));
4290
Avi Kivity6aa8b732006-12-10 02:21:36 -08004291 guest_tr_ar = vmcs_read32(GUEST_TR_AR_BYTES);
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07004292 if ((guest_tr_ar & VMX_AR_TYPE_MASK) != VMX_AR_TYPE_BUSY_64_TSS) {
Jan Kiszkabd801582011-09-12 11:26:22 +02004293 pr_debug_ratelimited("%s: tss fixup for long mode. \n",
4294 __func__);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004295 vmcs_write32(GUEST_TR_AR_BYTES,
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07004296 (guest_tr_ar & ~VMX_AR_TYPE_MASK)
4297 | VMX_AR_TYPE_BUSY_64_TSS);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004298 }
Avi Kivityda38f432010-07-06 11:30:49 +03004299 vmx_set_efer(vcpu, vcpu->arch.efer | EFER_LMA);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004300}
4301
4302static void exit_lmode(struct kvm_vcpu *vcpu)
4303{
Gleb Natapov2961e8762013-11-25 15:37:13 +02004304 vm_entry_controls_clearbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
Avi Kivityda38f432010-07-06 11:30:49 +03004305 vmx_set_efer(vcpu, vcpu->arch.efer & ~EFER_LMA);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004306}
4307
4308#endif
4309
Wanpeng Lic2ba05c2017-12-12 17:33:03 -08004310static inline void __vmx_flush_tlb(struct kvm_vcpu *vcpu, int vpid,
4311 bool invalidate_gpa)
Sheng Yang2384d2b2008-01-17 15:14:33 +08004312{
Wanpeng Lic2ba05c2017-12-12 17:33:03 -08004313 if (enable_ept && (invalidate_gpa || !enable_vpid)) {
Xiao Guangrongdd180b32010-07-03 16:02:42 +08004314 if (!VALID_PAGE(vcpu->arch.mmu.root_hpa))
4315 return;
Peter Feiner995f00a2017-06-30 17:26:32 -07004316 ept_sync_context(construct_eptp(vcpu, vcpu->arch.mmu.root_hpa));
Jim Mattsonf0b98c02017-03-15 07:56:11 -07004317 } else {
4318 vpid_sync_context(vpid);
Xiao Guangrongdd180b32010-07-03 16:02:42 +08004319 }
Sheng Yang2384d2b2008-01-17 15:14:33 +08004320}
4321
Wanpeng Lic2ba05c2017-12-12 17:33:03 -08004322static void vmx_flush_tlb(struct kvm_vcpu *vcpu, bool invalidate_gpa)
Wanpeng Lidd5f5342015-09-23 18:26:57 +08004323{
Wanpeng Lic2ba05c2017-12-12 17:33:03 -08004324 __vmx_flush_tlb(vcpu, to_vmx(vcpu)->vpid, invalidate_gpa);
Wanpeng Lidd5f5342015-09-23 18:26:57 +08004325}
4326
Jim Mattsonfb6c8192017-03-16 13:53:59 -07004327static void vmx_flush_tlb_ept_only(struct kvm_vcpu *vcpu)
4328{
4329 if (enable_ept)
Wanpeng Lic2ba05c2017-12-12 17:33:03 -08004330 vmx_flush_tlb(vcpu, true);
Jim Mattsonfb6c8192017-03-16 13:53:59 -07004331}
4332
Avi Kivitye8467fd2009-12-29 18:43:06 +02004333static void vmx_decache_cr0_guest_bits(struct kvm_vcpu *vcpu)
4334{
4335 ulong cr0_guest_owned_bits = vcpu->arch.cr0_guest_owned_bits;
4336
4337 vcpu->arch.cr0 &= ~cr0_guest_owned_bits;
4338 vcpu->arch.cr0 |= vmcs_readl(GUEST_CR0) & cr0_guest_owned_bits;
4339}
4340
Avi Kivityaff48ba2010-12-05 18:56:11 +02004341static void vmx_decache_cr3(struct kvm_vcpu *vcpu)
4342{
Sean Christophersonb4d18512018-03-05 12:04:40 -08004343 if (enable_unrestricted_guest || (enable_ept && is_paging(vcpu)))
Avi Kivityaff48ba2010-12-05 18:56:11 +02004344 vcpu->arch.cr3 = vmcs_readl(GUEST_CR3);
4345 __set_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail);
4346}
4347
Anthony Liguori25c4c272007-04-27 09:29:21 +03004348static void vmx_decache_cr4_guest_bits(struct kvm_vcpu *vcpu)
Avi Kivity399badf2007-01-05 16:36:38 -08004349{
Avi Kivityfc78f512009-12-07 12:16:48 +02004350 ulong cr4_guest_owned_bits = vcpu->arch.cr4_guest_owned_bits;
4351
4352 vcpu->arch.cr4 &= ~cr4_guest_owned_bits;
4353 vcpu->arch.cr4 |= vmcs_readl(GUEST_CR4) & cr4_guest_owned_bits;
Avi Kivity399badf2007-01-05 16:36:38 -08004354}
4355
Sheng Yang14394422008-04-28 12:24:45 +08004356static void ept_load_pdptrs(struct kvm_vcpu *vcpu)
4357{
Gleb Natapovd0d538b2013-10-09 19:13:19 +03004358 struct kvm_mmu *mmu = vcpu->arch.walk_mmu;
4359
Avi Kivity6de4f3a2009-05-31 22:58:47 +03004360 if (!test_bit(VCPU_EXREG_PDPTR,
4361 (unsigned long *)&vcpu->arch.regs_dirty))
4362 return;
4363
Sheng Yang14394422008-04-28 12:24:45 +08004364 if (is_paging(vcpu) && is_pae(vcpu) && !is_long_mode(vcpu)) {
Gleb Natapovd0d538b2013-10-09 19:13:19 +03004365 vmcs_write64(GUEST_PDPTR0, mmu->pdptrs[0]);
4366 vmcs_write64(GUEST_PDPTR1, mmu->pdptrs[1]);
4367 vmcs_write64(GUEST_PDPTR2, mmu->pdptrs[2]);
4368 vmcs_write64(GUEST_PDPTR3, mmu->pdptrs[3]);
Sheng Yang14394422008-04-28 12:24:45 +08004369 }
4370}
4371
Avi Kivity8f5d5492009-05-31 18:41:29 +03004372static void ept_save_pdptrs(struct kvm_vcpu *vcpu)
4373{
Gleb Natapovd0d538b2013-10-09 19:13:19 +03004374 struct kvm_mmu *mmu = vcpu->arch.walk_mmu;
4375
Avi Kivity8f5d5492009-05-31 18:41:29 +03004376 if (is_paging(vcpu) && is_pae(vcpu) && !is_long_mode(vcpu)) {
Gleb Natapovd0d538b2013-10-09 19:13:19 +03004377 mmu->pdptrs[0] = vmcs_read64(GUEST_PDPTR0);
4378 mmu->pdptrs[1] = vmcs_read64(GUEST_PDPTR1);
4379 mmu->pdptrs[2] = vmcs_read64(GUEST_PDPTR2);
4380 mmu->pdptrs[3] = vmcs_read64(GUEST_PDPTR3);
Avi Kivity8f5d5492009-05-31 18:41:29 +03004381 }
Avi Kivity6de4f3a2009-05-31 22:58:47 +03004382
4383 __set_bit(VCPU_EXREG_PDPTR,
4384 (unsigned long *)&vcpu->arch.regs_avail);
4385 __set_bit(VCPU_EXREG_PDPTR,
4386 (unsigned long *)&vcpu->arch.regs_dirty);
Avi Kivity8f5d5492009-05-31 18:41:29 +03004387}
4388
David Matlack38991522016-11-29 18:14:08 -08004389static bool nested_guest_cr0_valid(struct kvm_vcpu *vcpu, unsigned long val)
4390{
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01004391 u64 fixed0 = to_vmx(vcpu)->nested.msrs.cr0_fixed0;
4392 u64 fixed1 = to_vmx(vcpu)->nested.msrs.cr0_fixed1;
David Matlack38991522016-11-29 18:14:08 -08004393 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
4394
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01004395 if (to_vmx(vcpu)->nested.msrs.secondary_ctls_high &
David Matlack38991522016-11-29 18:14:08 -08004396 SECONDARY_EXEC_UNRESTRICTED_GUEST &&
4397 nested_cpu_has2(vmcs12, SECONDARY_EXEC_UNRESTRICTED_GUEST))
4398 fixed0 &= ~(X86_CR0_PE | X86_CR0_PG);
4399
4400 return fixed_bits_valid(val, fixed0, fixed1);
4401}
4402
4403static bool nested_host_cr0_valid(struct kvm_vcpu *vcpu, unsigned long val)
4404{
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01004405 u64 fixed0 = to_vmx(vcpu)->nested.msrs.cr0_fixed0;
4406 u64 fixed1 = to_vmx(vcpu)->nested.msrs.cr0_fixed1;
David Matlack38991522016-11-29 18:14:08 -08004407
4408 return fixed_bits_valid(val, fixed0, fixed1);
4409}
4410
4411static bool nested_cr4_valid(struct kvm_vcpu *vcpu, unsigned long val)
4412{
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01004413 u64 fixed0 = to_vmx(vcpu)->nested.msrs.cr4_fixed0;
4414 u64 fixed1 = to_vmx(vcpu)->nested.msrs.cr4_fixed1;
David Matlack38991522016-11-29 18:14:08 -08004415
4416 return fixed_bits_valid(val, fixed0, fixed1);
4417}
4418
4419/* No difference in the restrictions on guest and host CR4 in VMX operation. */
4420#define nested_guest_cr4_valid nested_cr4_valid
4421#define nested_host_cr4_valid nested_cr4_valid
4422
Nadav Har'El5e1746d2011-05-25 23:03:24 +03004423static int vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4);
Sheng Yang14394422008-04-28 12:24:45 +08004424
4425static void ept_update_paging_mode_cr0(unsigned long *hw_cr0,
4426 unsigned long cr0,
4427 struct kvm_vcpu *vcpu)
4428{
Marcelo Tosatti5233dd52011-06-06 14:27:47 -03004429 if (!test_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail))
4430 vmx_decache_cr3(vcpu);
Sheng Yang14394422008-04-28 12:24:45 +08004431 if (!(cr0 & X86_CR0_PG)) {
4432 /* From paging/starting to nonpaging */
4433 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL,
Sheng Yang65267ea2008-06-18 14:43:38 +08004434 vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) |
Sheng Yang14394422008-04-28 12:24:45 +08004435 (CPU_BASED_CR3_LOAD_EXITING |
4436 CPU_BASED_CR3_STORE_EXITING));
4437 vcpu->arch.cr0 = cr0;
Avi Kivityfc78f512009-12-07 12:16:48 +02004438 vmx_set_cr4(vcpu, kvm_read_cr4(vcpu));
Sheng Yang14394422008-04-28 12:24:45 +08004439 } else if (!is_paging(vcpu)) {
4440 /* From nonpaging to paging */
4441 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL,
Sheng Yang65267ea2008-06-18 14:43:38 +08004442 vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) &
Sheng Yang14394422008-04-28 12:24:45 +08004443 ~(CPU_BASED_CR3_LOAD_EXITING |
4444 CPU_BASED_CR3_STORE_EXITING));
4445 vcpu->arch.cr0 = cr0;
Avi Kivityfc78f512009-12-07 12:16:48 +02004446 vmx_set_cr4(vcpu, kvm_read_cr4(vcpu));
Sheng Yang14394422008-04-28 12:24:45 +08004447 }
Sheng Yang95eb84a2009-08-19 09:52:18 +08004448
4449 if (!(cr0 & X86_CR0_WP))
4450 *hw_cr0 &= ~X86_CR0_WP;
Sheng Yang14394422008-04-28 12:24:45 +08004451}
4452
Avi Kivity6aa8b732006-12-10 02:21:36 -08004453static void vmx_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
4454{
Avi Kivity7ffd92c2009-06-09 14:10:45 +03004455 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nitin A Kamble3a624e22009-06-08 11:34:16 -07004456 unsigned long hw_cr0;
4457
Gleb Natapov50378782013-02-04 16:00:28 +02004458 hw_cr0 = (cr0 & ~KVM_GUEST_CR0_MASK);
Nitin A Kamble3a624e22009-06-08 11:34:16 -07004459 if (enable_unrestricted_guest)
Gleb Natapov50378782013-02-04 16:00:28 +02004460 hw_cr0 |= KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST;
Gleb Natapov218e7632013-01-21 15:36:45 +02004461 else {
Gleb Natapov50378782013-02-04 16:00:28 +02004462 hw_cr0 |= KVM_VM_CR0_ALWAYS_ON;
Sheng Yang14394422008-04-28 12:24:45 +08004463
Gleb Natapov218e7632013-01-21 15:36:45 +02004464 if (vmx->rmode.vm86_active && (cr0 & X86_CR0_PE))
4465 enter_pmode(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004466
Gleb Natapov218e7632013-01-21 15:36:45 +02004467 if (!vmx->rmode.vm86_active && !(cr0 & X86_CR0_PE))
4468 enter_rmode(vcpu);
4469 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08004470
Avi Kivity05b3e0c2006-12-13 00:33:45 -08004471#ifdef CONFIG_X86_64
Avi Kivityf6801df2010-01-21 15:31:50 +02004472 if (vcpu->arch.efer & EFER_LME) {
Rusty Russell707d92fa2007-07-17 23:19:08 +10004473 if (!is_paging(vcpu) && (cr0 & X86_CR0_PG))
Avi Kivity6aa8b732006-12-10 02:21:36 -08004474 enter_lmode(vcpu);
Rusty Russell707d92fa2007-07-17 23:19:08 +10004475 if (is_paging(vcpu) && !(cr0 & X86_CR0_PG))
Avi Kivity6aa8b732006-12-10 02:21:36 -08004476 exit_lmode(vcpu);
4477 }
4478#endif
4479
Sean Christophersonb4d18512018-03-05 12:04:40 -08004480 if (enable_ept && !enable_unrestricted_guest)
Sheng Yang14394422008-04-28 12:24:45 +08004481 ept_update_paging_mode_cr0(&hw_cr0, cr0, vcpu);
4482
Avi Kivity6aa8b732006-12-10 02:21:36 -08004483 vmcs_writel(CR0_READ_SHADOW, cr0);
Sheng Yang14394422008-04-28 12:24:45 +08004484 vmcs_writel(GUEST_CR0, hw_cr0);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08004485 vcpu->arch.cr0 = cr0;
Gleb Natapov14168782013-01-21 15:36:49 +02004486
4487 /* depends on vcpu->arch.cr0 to be set to a new value */
4488 vmx->emulation_required = emulation_required(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004489}
4490
Yu Zhang855feb62017-08-24 20:27:55 +08004491static int get_ept_level(struct kvm_vcpu *vcpu)
4492{
4493 if (cpu_has_vmx_ept_5levels() && (cpuid_maxphyaddr(vcpu) > 48))
4494 return 5;
4495 return 4;
4496}
4497
Peter Feiner995f00a2017-06-30 17:26:32 -07004498static u64 construct_eptp(struct kvm_vcpu *vcpu, unsigned long root_hpa)
Sheng Yang14394422008-04-28 12:24:45 +08004499{
Yu Zhang855feb62017-08-24 20:27:55 +08004500 u64 eptp = VMX_EPTP_MT_WB;
Sheng Yang14394422008-04-28 12:24:45 +08004501
Yu Zhang855feb62017-08-24 20:27:55 +08004502 eptp |= (get_ept_level(vcpu) == 5) ? VMX_EPTP_PWL_5 : VMX_EPTP_PWL_4;
Sheng Yang14394422008-04-28 12:24:45 +08004503
Peter Feiner995f00a2017-06-30 17:26:32 -07004504 if (enable_ept_ad_bits &&
4505 (!is_guest_mode(vcpu) || nested_ept_ad_enabled(vcpu)))
David Hildenbrandbb97a012017-08-10 23:15:28 +02004506 eptp |= VMX_EPTP_AD_ENABLE_BIT;
Sheng Yang14394422008-04-28 12:24:45 +08004507 eptp |= (root_hpa & PAGE_MASK);
4508
4509 return eptp;
4510}
4511
Avi Kivity6aa8b732006-12-10 02:21:36 -08004512static void vmx_set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3)
4513{
Sheng Yang14394422008-04-28 12:24:45 +08004514 unsigned long guest_cr3;
4515 u64 eptp;
4516
4517 guest_cr3 = cr3;
Avi Kivity089d0342009-03-23 18:26:32 +02004518 if (enable_ept) {
Peter Feiner995f00a2017-06-30 17:26:32 -07004519 eptp = construct_eptp(vcpu, cr3);
Sheng Yang14394422008-04-28 12:24:45 +08004520 vmcs_write64(EPT_POINTER, eptp);
Sean Christophersone90008d2018-03-05 12:04:37 -08004521 if (enable_unrestricted_guest || is_paging(vcpu) ||
4522 is_guest_mode(vcpu))
Jan Kiszka59ab5a82013-08-08 16:26:29 +02004523 guest_cr3 = kvm_read_cr3(vcpu);
4524 else
4525 guest_cr3 = vcpu->kvm->arch.ept_identity_map_addr;
Marcelo Tosatti7c93be442009-10-26 16:48:33 -02004526 ept_load_pdptrs(vcpu);
Sheng Yang14394422008-04-28 12:24:45 +08004527 }
4528
Wanpeng Lic2ba05c2017-12-12 17:33:03 -08004529 vmx_flush_tlb(vcpu, true);
Sheng Yang14394422008-04-28 12:24:45 +08004530 vmcs_writel(GUEST_CR3, guest_cr3);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004531}
4532
Nadav Har'El5e1746d2011-05-25 23:03:24 +03004533static int vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004534{
Ben Serebrin085e68e2015-04-16 11:58:05 -07004535 /*
4536 * Pass through host's Machine Check Enable value to hw_cr4, which
4537 * is in force while we are in guest mode. Do not let guests control
4538 * this bit, even if host CR4.MCE == 0.
4539 */
Sean Christopherson5dc1f042018-03-05 12:04:39 -08004540 unsigned long hw_cr4;
4541
4542 hw_cr4 = (cr4_read_shadow() & X86_CR4_MCE) | (cr4 & ~X86_CR4_MCE);
4543 if (enable_unrestricted_guest)
4544 hw_cr4 |= KVM_VM_CR4_ALWAYS_ON_UNRESTRICTED_GUEST;
4545 else if (to_vmx(vcpu)->rmode.vm86_active)
4546 hw_cr4 |= KVM_RMODE_VM_CR4_ALWAYS_ON;
4547 else
4548 hw_cr4 |= KVM_PMODE_VM_CR4_ALWAYS_ON;
Sheng Yang14394422008-04-28 12:24:45 +08004549
Paolo Bonzini0367f202016-07-12 10:44:55 +02004550 if ((cr4 & X86_CR4_UMIP) && !boot_cpu_has(X86_FEATURE_UMIP)) {
4551 vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL,
4552 SECONDARY_EXEC_DESC);
4553 hw_cr4 &= ~X86_CR4_UMIP;
Radim Krčmář99158242018-01-31 18:12:50 +01004554 } else if (!is_guest_mode(vcpu) ||
4555 !nested_cpu_has2(get_vmcs12(vcpu), SECONDARY_EXEC_DESC))
Paolo Bonzini0367f202016-07-12 10:44:55 +02004556 vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL,
4557 SECONDARY_EXEC_DESC);
4558
Nadav Har'El5e1746d2011-05-25 23:03:24 +03004559 if (cr4 & X86_CR4_VMXE) {
4560 /*
4561 * To use VMXON (and later other VMX instructions), a guest
4562 * must first be able to turn on cr4.VMXE (see handle_vmon()).
4563 * So basically the check on whether to allow nested VMX
4564 * is here.
4565 */
4566 if (!nested_vmx_allowed(vcpu))
4567 return 1;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01004568 }
David Matlack38991522016-11-29 18:14:08 -08004569
4570 if (to_vmx(vcpu)->nested.vmxon && !nested_cr4_valid(vcpu, cr4))
Nadav Har'El5e1746d2011-05-25 23:03:24 +03004571 return 1;
4572
Zhang Xiantaoad312c72007-12-13 23:50:52 +08004573 vcpu->arch.cr4 = cr4;
Sheng Yang14394422008-04-28 12:24:45 +08004574
Sean Christopherson5dc1f042018-03-05 12:04:39 -08004575 if (!enable_unrestricted_guest) {
4576 if (enable_ept) {
4577 if (!is_paging(vcpu)) {
4578 hw_cr4 &= ~X86_CR4_PAE;
4579 hw_cr4 |= X86_CR4_PSE;
4580 } else if (!(cr4 & X86_CR4_PAE)) {
4581 hw_cr4 &= ~X86_CR4_PAE;
4582 }
4583 }
4584
Radim Krčmář656ec4a2015-11-02 22:20:00 +01004585 /*
Huaitong Handdba2622016-03-22 16:51:15 +08004586 * SMEP/SMAP/PKU is disabled if CPU is in non-paging mode in
4587 * hardware. To emulate this behavior, SMEP/SMAP/PKU needs
4588 * to be manually disabled when guest switches to non-paging
4589 * mode.
4590 *
4591 * If !enable_unrestricted_guest, the CPU is always running
4592 * with CR0.PG=1 and CR4 needs to be modified.
4593 * If enable_unrestricted_guest, the CPU automatically
4594 * disables SMEP/SMAP/PKU when the guest sets CR0.PG=0.
Radim Krčmář656ec4a2015-11-02 22:20:00 +01004595 */
Sean Christopherson5dc1f042018-03-05 12:04:39 -08004596 if (!is_paging(vcpu))
4597 hw_cr4 &= ~(X86_CR4_SMEP | X86_CR4_SMAP | X86_CR4_PKE);
4598 }
Radim Krčmář656ec4a2015-11-02 22:20:00 +01004599
Sheng Yang14394422008-04-28 12:24:45 +08004600 vmcs_writel(CR4_READ_SHADOW, cr4);
4601 vmcs_writel(GUEST_CR4, hw_cr4);
Nadav Har'El5e1746d2011-05-25 23:03:24 +03004602 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004603}
4604
Avi Kivity6aa8b732006-12-10 02:21:36 -08004605static void vmx_get_segment(struct kvm_vcpu *vcpu,
4606 struct kvm_segment *var, int seg)
4607{
Avi Kivitya9179492011-01-03 14:28:52 +02004608 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004609 u32 ar;
4610
Gleb Natapovc6ad11532012-12-12 19:10:51 +02004611 if (vmx->rmode.vm86_active && seg != VCPU_SREG_LDTR) {
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03004612 *var = vmx->rmode.segs[seg];
Avi Kivitya9179492011-01-03 14:28:52 +02004613 if (seg == VCPU_SREG_TR
Avi Kivity2fb92db2011-04-27 19:42:18 +03004614 || var->selector == vmx_read_guest_seg_selector(vmx, seg))
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03004615 return;
Avi Kivity1390a282012-08-21 17:07:08 +03004616 var->base = vmx_read_guest_seg_base(vmx, seg);
4617 var->selector = vmx_read_guest_seg_selector(vmx, seg);
4618 return;
Avi Kivitya9179492011-01-03 14:28:52 +02004619 }
Avi Kivity2fb92db2011-04-27 19:42:18 +03004620 var->base = vmx_read_guest_seg_base(vmx, seg);
4621 var->limit = vmx_read_guest_seg_limit(vmx, seg);
4622 var->selector = vmx_read_guest_seg_selector(vmx, seg);
4623 ar = vmx_read_guest_seg_ar(vmx, seg);
Gleb Natapov03617c12013-06-28 13:17:18 +03004624 var->unusable = (ar >> 16) & 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004625 var->type = ar & 15;
4626 var->s = (ar >> 4) & 1;
4627 var->dpl = (ar >> 5) & 3;
Gleb Natapov03617c12013-06-28 13:17:18 +03004628 /*
4629 * Some userspaces do not preserve unusable property. Since usable
4630 * segment has to be present according to VMX spec we can use present
4631 * property to amend userspace bug by making unusable segment always
4632 * nonpresent. vmx_segment_access_rights() already marks nonpresent
4633 * segment as unusable.
4634 */
4635 var->present = !var->unusable;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004636 var->avl = (ar >> 12) & 1;
4637 var->l = (ar >> 13) & 1;
4638 var->db = (ar >> 14) & 1;
4639 var->g = (ar >> 15) & 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004640}
4641
Avi Kivitya9179492011-01-03 14:28:52 +02004642static u64 vmx_get_segment_base(struct kvm_vcpu *vcpu, int seg)
4643{
Avi Kivitya9179492011-01-03 14:28:52 +02004644 struct kvm_segment s;
4645
4646 if (to_vmx(vcpu)->rmode.vm86_active) {
4647 vmx_get_segment(vcpu, &s, seg);
4648 return s.base;
4649 }
Avi Kivity2fb92db2011-04-27 19:42:18 +03004650 return vmx_read_guest_seg_base(to_vmx(vcpu), seg);
Avi Kivitya9179492011-01-03 14:28:52 +02004651}
4652
Marcelo Tosattib09408d2013-01-07 19:27:06 -02004653static int vmx_get_cpl(struct kvm_vcpu *vcpu)
Izik Eidus2e4d2652008-03-24 19:38:34 +02004654{
Marcelo Tosattib09408d2013-01-07 19:27:06 -02004655 struct vcpu_vmx *vmx = to_vmx(vcpu);
4656
Paolo Bonziniae9fedc2014-05-14 09:39:49 +02004657 if (unlikely(vmx->rmode.vm86_active))
Izik Eidus2e4d2652008-03-24 19:38:34 +02004658 return 0;
Paolo Bonziniae9fedc2014-05-14 09:39:49 +02004659 else {
4660 int ar = vmx_read_guest_seg_ar(vmx, VCPU_SREG_SS);
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07004661 return VMX_AR_DPL(ar);
Avi Kivity69c73022011-03-07 15:26:44 +02004662 }
Avi Kivity69c73022011-03-07 15:26:44 +02004663}
4664
Avi Kivity653e3102007-05-07 10:55:37 +03004665static u32 vmx_segment_access_rights(struct kvm_segment *var)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004666{
Avi Kivity6aa8b732006-12-10 02:21:36 -08004667 u32 ar;
4668
Avi Kivityf0495f92012-06-07 17:06:10 +03004669 if (var->unusable || !var->present)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004670 ar = 1 << 16;
4671 else {
4672 ar = var->type & 15;
4673 ar |= (var->s & 1) << 4;
4674 ar |= (var->dpl & 3) << 5;
4675 ar |= (var->present & 1) << 7;
4676 ar |= (var->avl & 1) << 12;
4677 ar |= (var->l & 1) << 13;
4678 ar |= (var->db & 1) << 14;
4679 ar |= (var->g & 1) << 15;
4680 }
Avi Kivity653e3102007-05-07 10:55:37 +03004681
4682 return ar;
4683}
4684
4685static void vmx_set_segment(struct kvm_vcpu *vcpu,
4686 struct kvm_segment *var, int seg)
4687{
Avi Kivity7ffd92c2009-06-09 14:10:45 +03004688 struct vcpu_vmx *vmx = to_vmx(vcpu);
Mathias Krause772e0312012-08-30 01:30:19 +02004689 const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
Avi Kivity653e3102007-05-07 10:55:37 +03004690
Avi Kivity2fb92db2011-04-27 19:42:18 +03004691 vmx_segment_cache_clear(vmx);
4692
Gleb Natapov1ecd50a2012-12-12 19:10:54 +02004693 if (vmx->rmode.vm86_active && seg != VCPU_SREG_LDTR) {
4694 vmx->rmode.segs[seg] = *var;
4695 if (seg == VCPU_SREG_TR)
4696 vmcs_write16(sf->selector, var->selector);
4697 else if (var->s)
4698 fix_rmode_seg(seg, &vmx->rmode.segs[seg]);
Gleb Natapovd99e4152012-12-20 16:57:45 +02004699 goto out;
Avi Kivity653e3102007-05-07 10:55:37 +03004700 }
Gleb Natapov1ecd50a2012-12-12 19:10:54 +02004701
Avi Kivity653e3102007-05-07 10:55:37 +03004702 vmcs_writel(sf->base, var->base);
4703 vmcs_write32(sf->limit, var->limit);
4704 vmcs_write16(sf->selector, var->selector);
Nitin A Kamble3a624e22009-06-08 11:34:16 -07004705
4706 /*
4707 * Fix the "Accessed" bit in AR field of segment registers for older
4708 * qemu binaries.
4709 * IA32 arch specifies that at the time of processor reset the
4710 * "Accessed" bit in the AR field of segment registers is 1. And qemu
Guo Chao0fa06072012-06-28 15:16:19 +08004711 * is setting it to 0 in the userland code. This causes invalid guest
Nitin A Kamble3a624e22009-06-08 11:34:16 -07004712 * state vmexit when "unrestricted guest" mode is turned on.
4713 * Fix for this setup issue in cpu_reset is being pushed in the qemu
4714 * tree. Newer qemu binaries with that qemu fix would not need this
4715 * kvm hack.
4716 */
4717 if (enable_unrestricted_guest && (seg != VCPU_SREG_LDTR))
Gleb Natapovf924d662012-12-12 19:10:55 +02004718 var->type |= 0x1; /* Accessed */
Nitin A Kamble3a624e22009-06-08 11:34:16 -07004719
Gleb Natapovf924d662012-12-12 19:10:55 +02004720 vmcs_write32(sf->ar_bytes, vmx_segment_access_rights(var));
Gleb Natapovd99e4152012-12-20 16:57:45 +02004721
4722out:
Paolo Bonzini98eb2f82014-03-27 09:51:52 +01004723 vmx->emulation_required = emulation_required(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004724}
4725
Avi Kivity6aa8b732006-12-10 02:21:36 -08004726static void vmx_get_cs_db_l_bits(struct kvm_vcpu *vcpu, int *db, int *l)
4727{
Avi Kivity2fb92db2011-04-27 19:42:18 +03004728 u32 ar = vmx_read_guest_seg_ar(to_vmx(vcpu), VCPU_SREG_CS);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004729
4730 *db = (ar >> 14) & 1;
4731 *l = (ar >> 13) & 1;
4732}
4733
Gleb Natapov89a27f42010-02-16 10:51:48 +02004734static void vmx_get_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004735{
Gleb Natapov89a27f42010-02-16 10:51:48 +02004736 dt->size = vmcs_read32(GUEST_IDTR_LIMIT);
4737 dt->address = vmcs_readl(GUEST_IDTR_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004738}
4739
Gleb Natapov89a27f42010-02-16 10:51:48 +02004740static void vmx_set_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004741{
Gleb Natapov89a27f42010-02-16 10:51:48 +02004742 vmcs_write32(GUEST_IDTR_LIMIT, dt->size);
4743 vmcs_writel(GUEST_IDTR_BASE, dt->address);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004744}
4745
Gleb Natapov89a27f42010-02-16 10:51:48 +02004746static void vmx_get_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004747{
Gleb Natapov89a27f42010-02-16 10:51:48 +02004748 dt->size = vmcs_read32(GUEST_GDTR_LIMIT);
4749 dt->address = vmcs_readl(GUEST_GDTR_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004750}
4751
Gleb Natapov89a27f42010-02-16 10:51:48 +02004752static void vmx_set_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004753{
Gleb Natapov89a27f42010-02-16 10:51:48 +02004754 vmcs_write32(GUEST_GDTR_LIMIT, dt->size);
4755 vmcs_writel(GUEST_GDTR_BASE, dt->address);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004756}
4757
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004758static bool rmode_segment_valid(struct kvm_vcpu *vcpu, int seg)
4759{
4760 struct kvm_segment var;
4761 u32 ar;
4762
4763 vmx_get_segment(vcpu, &var, seg);
Gleb Natapov07f42f52012-12-12 19:10:49 +02004764 var.dpl = 0x3;
Gleb Natapov0647f4a2012-12-12 19:10:50 +02004765 if (seg == VCPU_SREG_CS)
4766 var.type = 0x3;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004767 ar = vmx_segment_access_rights(&var);
4768
4769 if (var.base != (var.selector << 4))
4770 return false;
Gleb Natapov89efbed2012-12-20 16:57:44 +02004771 if (var.limit != 0xffff)
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004772 return false;
Gleb Natapov07f42f52012-12-12 19:10:49 +02004773 if (ar != 0xf3)
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004774 return false;
4775
4776 return true;
4777}
4778
4779static bool code_segment_valid(struct kvm_vcpu *vcpu)
4780{
4781 struct kvm_segment cs;
4782 unsigned int cs_rpl;
4783
4784 vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
Nadav Amitb32a9912015-03-29 16:33:04 +03004785 cs_rpl = cs.selector & SEGMENT_RPL_MASK;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004786
Avi Kivity1872a3f2009-01-04 23:26:52 +02004787 if (cs.unusable)
4788 return false;
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07004789 if (~cs.type & (VMX_AR_TYPE_CODE_MASK|VMX_AR_TYPE_ACCESSES_MASK))
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004790 return false;
4791 if (!cs.s)
4792 return false;
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07004793 if (cs.type & VMX_AR_TYPE_WRITEABLE_MASK) {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004794 if (cs.dpl > cs_rpl)
4795 return false;
Avi Kivity1872a3f2009-01-04 23:26:52 +02004796 } else {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004797 if (cs.dpl != cs_rpl)
4798 return false;
4799 }
4800 if (!cs.present)
4801 return false;
4802
4803 /* TODO: Add Reserved field check, this'll require a new member in the kvm_segment_field structure */
4804 return true;
4805}
4806
4807static bool stack_segment_valid(struct kvm_vcpu *vcpu)
4808{
4809 struct kvm_segment ss;
4810 unsigned int ss_rpl;
4811
4812 vmx_get_segment(vcpu, &ss, VCPU_SREG_SS);
Nadav Amitb32a9912015-03-29 16:33:04 +03004813 ss_rpl = ss.selector & SEGMENT_RPL_MASK;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004814
Avi Kivity1872a3f2009-01-04 23:26:52 +02004815 if (ss.unusable)
4816 return true;
4817 if (ss.type != 3 && ss.type != 7)
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004818 return false;
4819 if (!ss.s)
4820 return false;
4821 if (ss.dpl != ss_rpl) /* DPL != RPL */
4822 return false;
4823 if (!ss.present)
4824 return false;
4825
4826 return true;
4827}
4828
4829static bool data_segment_valid(struct kvm_vcpu *vcpu, int seg)
4830{
4831 struct kvm_segment var;
4832 unsigned int rpl;
4833
4834 vmx_get_segment(vcpu, &var, seg);
Nadav Amitb32a9912015-03-29 16:33:04 +03004835 rpl = var.selector & SEGMENT_RPL_MASK;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004836
Avi Kivity1872a3f2009-01-04 23:26:52 +02004837 if (var.unusable)
4838 return true;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004839 if (!var.s)
4840 return false;
4841 if (!var.present)
4842 return false;
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07004843 if (~var.type & (VMX_AR_TYPE_CODE_MASK|VMX_AR_TYPE_WRITEABLE_MASK)) {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004844 if (var.dpl < rpl) /* DPL < RPL */
4845 return false;
4846 }
4847
4848 /* TODO: Add other members to kvm_segment_field to allow checking for other access
4849 * rights flags
4850 */
4851 return true;
4852}
4853
4854static bool tr_valid(struct kvm_vcpu *vcpu)
4855{
4856 struct kvm_segment tr;
4857
4858 vmx_get_segment(vcpu, &tr, VCPU_SREG_TR);
4859
Avi Kivity1872a3f2009-01-04 23:26:52 +02004860 if (tr.unusable)
4861 return false;
Nadav Amitb32a9912015-03-29 16:33:04 +03004862 if (tr.selector & SEGMENT_TI_MASK) /* TI = 1 */
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004863 return false;
Avi Kivity1872a3f2009-01-04 23:26:52 +02004864 if (tr.type != 3 && tr.type != 11) /* TODO: Check if guest is in IA32e mode */
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004865 return false;
4866 if (!tr.present)
4867 return false;
4868
4869 return true;
4870}
4871
4872static bool ldtr_valid(struct kvm_vcpu *vcpu)
4873{
4874 struct kvm_segment ldtr;
4875
4876 vmx_get_segment(vcpu, &ldtr, VCPU_SREG_LDTR);
4877
Avi Kivity1872a3f2009-01-04 23:26:52 +02004878 if (ldtr.unusable)
4879 return true;
Nadav Amitb32a9912015-03-29 16:33:04 +03004880 if (ldtr.selector & SEGMENT_TI_MASK) /* TI = 1 */
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004881 return false;
4882 if (ldtr.type != 2)
4883 return false;
4884 if (!ldtr.present)
4885 return false;
4886
4887 return true;
4888}
4889
4890static bool cs_ss_rpl_check(struct kvm_vcpu *vcpu)
4891{
4892 struct kvm_segment cs, ss;
4893
4894 vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
4895 vmx_get_segment(vcpu, &ss, VCPU_SREG_SS);
4896
Nadav Amitb32a9912015-03-29 16:33:04 +03004897 return ((cs.selector & SEGMENT_RPL_MASK) ==
4898 (ss.selector & SEGMENT_RPL_MASK));
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004899}
4900
4901/*
4902 * Check if guest state is valid. Returns true if valid, false if
4903 * not.
4904 * We assume that registers are always usable
4905 */
4906static bool guest_state_valid(struct kvm_vcpu *vcpu)
4907{
Gleb Natapovc5e97c82013-01-21 15:36:43 +02004908 if (enable_unrestricted_guest)
4909 return true;
4910
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004911 /* real mode guest state checks */
Gleb Natapovf13882d2013-04-14 16:07:37 +03004912 if (!is_protmode(vcpu) || (vmx_get_rflags(vcpu) & X86_EFLAGS_VM)) {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004913 if (!rmode_segment_valid(vcpu, VCPU_SREG_CS))
4914 return false;
4915 if (!rmode_segment_valid(vcpu, VCPU_SREG_SS))
4916 return false;
4917 if (!rmode_segment_valid(vcpu, VCPU_SREG_DS))
4918 return false;
4919 if (!rmode_segment_valid(vcpu, VCPU_SREG_ES))
4920 return false;
4921 if (!rmode_segment_valid(vcpu, VCPU_SREG_FS))
4922 return false;
4923 if (!rmode_segment_valid(vcpu, VCPU_SREG_GS))
4924 return false;
4925 } else {
4926 /* protected mode guest state checks */
4927 if (!cs_ss_rpl_check(vcpu))
4928 return false;
4929 if (!code_segment_valid(vcpu))
4930 return false;
4931 if (!stack_segment_valid(vcpu))
4932 return false;
4933 if (!data_segment_valid(vcpu, VCPU_SREG_DS))
4934 return false;
4935 if (!data_segment_valid(vcpu, VCPU_SREG_ES))
4936 return false;
4937 if (!data_segment_valid(vcpu, VCPU_SREG_FS))
4938 return false;
4939 if (!data_segment_valid(vcpu, VCPU_SREG_GS))
4940 return false;
4941 if (!tr_valid(vcpu))
4942 return false;
4943 if (!ldtr_valid(vcpu))
4944 return false;
4945 }
4946 /* TODO:
4947 * - Add checks on RIP
4948 * - Add checks on RFLAGS
4949 */
4950
4951 return true;
4952}
4953
Jim Mattson5fa99cb2017-07-06 16:33:07 -07004954static bool page_address_valid(struct kvm_vcpu *vcpu, gpa_t gpa)
4955{
4956 return PAGE_ALIGNED(gpa) && !(gpa >> cpuid_maxphyaddr(vcpu));
4957}
4958
Mike Dayd77c26f2007-10-08 09:02:08 -04004959static int init_rmode_tss(struct kvm *kvm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004960{
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08004961 gfn_t fn;
Izik Eidus195aefd2007-10-01 22:14:18 +02004962 u16 data = 0;
Paolo Bonzini1f755a82014-09-16 13:37:40 +02004963 int idx, r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004964
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08004965 idx = srcu_read_lock(&kvm->srcu);
Jan Kiszka4918c6c2013-03-15 08:38:56 +01004966 fn = kvm->arch.tss_addr >> PAGE_SHIFT;
Izik Eidus195aefd2007-10-01 22:14:18 +02004967 r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE);
4968 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05004969 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02004970 data = TSS_BASE_SIZE + TSS_REDIRECTION_SIZE;
Sheng Yang464d17c2008-08-13 14:10:33 +08004971 r = kvm_write_guest_page(kvm, fn++, &data,
4972 TSS_IOPB_BASE_OFFSET, sizeof(u16));
Izik Eidus195aefd2007-10-01 22:14:18 +02004973 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05004974 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02004975 r = kvm_clear_guest_page(kvm, fn++, 0, PAGE_SIZE);
4976 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05004977 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02004978 r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE);
4979 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05004980 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02004981 data = ~0;
Marcelo Tosatti10589a42007-12-20 19:18:22 -05004982 r = kvm_write_guest_page(kvm, fn, &data,
4983 RMODE_TSS_SIZE - 2 * PAGE_SIZE - 1,
4984 sizeof(u8));
Marcelo Tosatti10589a42007-12-20 19:18:22 -05004985out:
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08004986 srcu_read_unlock(&kvm->srcu, idx);
Paolo Bonzini1f755a82014-09-16 13:37:40 +02004987 return r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004988}
4989
Sheng Yangb7ebfb02008-04-25 21:44:52 +08004990static int init_rmode_identity_map(struct kvm *kvm)
4991{
Tang Chenf51770e2014-09-16 18:41:59 +08004992 int i, idx, r = 0;
Dan Williamsba049e92016-01-15 16:56:11 -08004993 kvm_pfn_t identity_map_pfn;
Sheng Yangb7ebfb02008-04-25 21:44:52 +08004994 u32 tmp;
4995
Tang Chena255d472014-09-16 18:41:58 +08004996 /* Protect kvm->arch.ept_identity_pagetable_done. */
4997 mutex_lock(&kvm->slots_lock);
4998
Tang Chenf51770e2014-09-16 18:41:59 +08004999 if (likely(kvm->arch.ept_identity_pagetable_done))
Tang Chena255d472014-09-16 18:41:58 +08005000 goto out2;
Tang Chena255d472014-09-16 18:41:58 +08005001
David Hildenbrandd8a6e362017-08-24 20:51:34 +02005002 if (!kvm->arch.ept_identity_map_addr)
5003 kvm->arch.ept_identity_map_addr = VMX_EPT_IDENTITY_PAGETABLE_ADDR;
Sheng Yangb927a3c2009-07-21 10:42:48 +08005004 identity_map_pfn = kvm->arch.ept_identity_map_addr >> PAGE_SHIFT;
Tang Chena255d472014-09-16 18:41:58 +08005005
David Hildenbrandd8a6e362017-08-24 20:51:34 +02005006 r = __x86_set_memory_region(kvm, IDENTITY_PAGETABLE_PRIVATE_MEMSLOT,
5007 kvm->arch.ept_identity_map_addr, PAGE_SIZE);
Tang Chenf51770e2014-09-16 18:41:59 +08005008 if (r < 0)
Tang Chena255d472014-09-16 18:41:58 +08005009 goto out2;
5010
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08005011 idx = srcu_read_lock(&kvm->srcu);
Sheng Yangb7ebfb02008-04-25 21:44:52 +08005012 r = kvm_clear_guest_page(kvm, identity_map_pfn, 0, PAGE_SIZE);
5013 if (r < 0)
5014 goto out;
5015 /* Set up identity-mapping pagetable for EPT in real mode */
5016 for (i = 0; i < PT32_ENT_PER_PAGE; i++) {
5017 tmp = (i << 22) + (_PAGE_PRESENT | _PAGE_RW | _PAGE_USER |
5018 _PAGE_ACCESSED | _PAGE_DIRTY | _PAGE_PSE);
5019 r = kvm_write_guest_page(kvm, identity_map_pfn,
5020 &tmp, i * sizeof(tmp), sizeof(tmp));
5021 if (r < 0)
5022 goto out;
5023 }
5024 kvm->arch.ept_identity_pagetable_done = true;
Tang Chenf51770e2014-09-16 18:41:59 +08005025
Sheng Yangb7ebfb02008-04-25 21:44:52 +08005026out:
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08005027 srcu_read_unlock(&kvm->srcu, idx);
Tang Chena255d472014-09-16 18:41:58 +08005028
5029out2:
5030 mutex_unlock(&kvm->slots_lock);
Tang Chenf51770e2014-09-16 18:41:59 +08005031 return r;
Sheng Yangb7ebfb02008-04-25 21:44:52 +08005032}
5033
Avi Kivity6aa8b732006-12-10 02:21:36 -08005034static void seg_setup(int seg)
5035{
Mathias Krause772e0312012-08-30 01:30:19 +02005036 const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
Nitin A Kamble3a624e22009-06-08 11:34:16 -07005037 unsigned int ar;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005038
5039 vmcs_write16(sf->selector, 0);
5040 vmcs_writel(sf->base, 0);
5041 vmcs_write32(sf->limit, 0xffff);
Gleb Natapovd54d07b2012-12-20 16:57:46 +02005042 ar = 0x93;
5043 if (seg == VCPU_SREG_CS)
5044 ar |= 0x08; /* code segment */
Nitin A Kamble3a624e22009-06-08 11:34:16 -07005045
5046 vmcs_write32(sf->ar_bytes, ar);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005047}
5048
Sheng Yangf78e0e22007-10-29 09:40:42 +08005049static int alloc_apic_access_page(struct kvm *kvm)
5050{
Xiao Guangrong44841412012-09-07 14:14:20 +08005051 struct page *page;
Sheng Yangf78e0e22007-10-29 09:40:42 +08005052 int r = 0;
5053
Marcelo Tosatti79fac952009-12-23 14:35:26 -02005054 mutex_lock(&kvm->slots_lock);
Tang Chenc24ae0d2014-09-24 15:57:58 +08005055 if (kvm->arch.apic_access_page_done)
Sheng Yangf78e0e22007-10-29 09:40:42 +08005056 goto out;
Paolo Bonzini1d8007b2015-10-12 13:38:32 +02005057 r = __x86_set_memory_region(kvm, APIC_ACCESS_PAGE_PRIVATE_MEMSLOT,
5058 APIC_DEFAULT_PHYS_BASE, PAGE_SIZE);
Sheng Yangf78e0e22007-10-29 09:40:42 +08005059 if (r)
5060 goto out;
Izik Eidus72dc67a2008-02-10 18:04:15 +02005061
Tang Chen73a6d942014-09-11 13:38:00 +08005062 page = gfn_to_page(kvm, APIC_DEFAULT_PHYS_BASE >> PAGE_SHIFT);
Xiao Guangrong44841412012-09-07 14:14:20 +08005063 if (is_error_page(page)) {
5064 r = -EFAULT;
5065 goto out;
5066 }
5067
Tang Chenc24ae0d2014-09-24 15:57:58 +08005068 /*
5069 * Do not pin the page in memory, so that memory hot-unplug
5070 * is able to migrate it.
5071 */
5072 put_page(page);
5073 kvm->arch.apic_access_page_done = true;
Sheng Yangf78e0e22007-10-29 09:40:42 +08005074out:
Marcelo Tosatti79fac952009-12-23 14:35:26 -02005075 mutex_unlock(&kvm->slots_lock);
Sheng Yangf78e0e22007-10-29 09:40:42 +08005076 return r;
5077}
5078
Wanpeng Li991e7a02015-09-16 17:30:05 +08005079static int allocate_vpid(void)
Sheng Yang2384d2b2008-01-17 15:14:33 +08005080{
5081 int vpid;
5082
Avi Kivity919818a2009-03-23 18:01:29 +02005083 if (!enable_vpid)
Wanpeng Li991e7a02015-09-16 17:30:05 +08005084 return 0;
Sheng Yang2384d2b2008-01-17 15:14:33 +08005085 spin_lock(&vmx_vpid_lock);
5086 vpid = find_first_zero_bit(vmx_vpid_bitmap, VMX_NR_VPIDS);
Wanpeng Li991e7a02015-09-16 17:30:05 +08005087 if (vpid < VMX_NR_VPIDS)
Sheng Yang2384d2b2008-01-17 15:14:33 +08005088 __set_bit(vpid, vmx_vpid_bitmap);
Wanpeng Li991e7a02015-09-16 17:30:05 +08005089 else
5090 vpid = 0;
Sheng Yang2384d2b2008-01-17 15:14:33 +08005091 spin_unlock(&vmx_vpid_lock);
Wanpeng Li991e7a02015-09-16 17:30:05 +08005092 return vpid;
Sheng Yang2384d2b2008-01-17 15:14:33 +08005093}
5094
Wanpeng Li991e7a02015-09-16 17:30:05 +08005095static void free_vpid(int vpid)
Lai Jiangshancdbecfc2010-04-17 16:41:47 +08005096{
Wanpeng Li991e7a02015-09-16 17:30:05 +08005097 if (!enable_vpid || vpid == 0)
Lai Jiangshancdbecfc2010-04-17 16:41:47 +08005098 return;
5099 spin_lock(&vmx_vpid_lock);
Wanpeng Li991e7a02015-09-16 17:30:05 +08005100 __clear_bit(vpid, vmx_vpid_bitmap);
Lai Jiangshancdbecfc2010-04-17 16:41:47 +08005101 spin_unlock(&vmx_vpid_lock);
5102}
5103
Paolo Bonzini904e14f2018-01-16 16:51:18 +01005104static void __always_inline vmx_disable_intercept_for_msr(unsigned long *msr_bitmap,
5105 u32 msr, int type)
Sheng Yang25c5f222008-03-28 13:18:56 +08005106{
Avi Kivity3e7c73e2009-02-24 21:46:19 +02005107 int f = sizeof(unsigned long);
Sheng Yang25c5f222008-03-28 13:18:56 +08005108
5109 if (!cpu_has_vmx_msr_bitmap())
5110 return;
5111
5112 /*
5113 * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
5114 * have the write-low and read-high bitmap offsets the wrong way round.
5115 * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
5116 */
Sheng Yang25c5f222008-03-28 13:18:56 +08005117 if (msr <= 0x1fff) {
Yang Zhang8d146952013-01-25 10:18:50 +08005118 if (type & MSR_TYPE_R)
5119 /* read-low */
5120 __clear_bit(msr, msr_bitmap + 0x000 / f);
5121
5122 if (type & MSR_TYPE_W)
5123 /* write-low */
5124 __clear_bit(msr, msr_bitmap + 0x800 / f);
5125
Sheng Yang25c5f222008-03-28 13:18:56 +08005126 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
5127 msr &= 0x1fff;
Yang Zhang8d146952013-01-25 10:18:50 +08005128 if (type & MSR_TYPE_R)
5129 /* read-high */
5130 __clear_bit(msr, msr_bitmap + 0x400 / f);
5131
5132 if (type & MSR_TYPE_W)
5133 /* write-high */
5134 __clear_bit(msr, msr_bitmap + 0xc00 / f);
5135
5136 }
5137}
5138
Paolo Bonzini904e14f2018-01-16 16:51:18 +01005139static void __always_inline vmx_enable_intercept_for_msr(unsigned long *msr_bitmap,
5140 u32 msr, int type)
5141{
5142 int f = sizeof(unsigned long);
5143
5144 if (!cpu_has_vmx_msr_bitmap())
5145 return;
5146
5147 /*
5148 * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
5149 * have the write-low and read-high bitmap offsets the wrong way round.
5150 * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
5151 */
5152 if (msr <= 0x1fff) {
5153 if (type & MSR_TYPE_R)
5154 /* read-low */
5155 __set_bit(msr, msr_bitmap + 0x000 / f);
5156
5157 if (type & MSR_TYPE_W)
5158 /* write-low */
5159 __set_bit(msr, msr_bitmap + 0x800 / f);
5160
5161 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
5162 msr &= 0x1fff;
5163 if (type & MSR_TYPE_R)
5164 /* read-high */
5165 __set_bit(msr, msr_bitmap + 0x400 / f);
5166
5167 if (type & MSR_TYPE_W)
5168 /* write-high */
5169 __set_bit(msr, msr_bitmap + 0xc00 / f);
5170
5171 }
5172}
5173
5174static void __always_inline vmx_set_intercept_for_msr(unsigned long *msr_bitmap,
5175 u32 msr, int type, bool value)
5176{
5177 if (value)
5178 vmx_enable_intercept_for_msr(msr_bitmap, msr, type);
5179 else
5180 vmx_disable_intercept_for_msr(msr_bitmap, msr, type);
5181}
5182
Wincy Vanf2b93282015-02-03 23:56:03 +08005183/*
5184 * If a msr is allowed by L0, we should check whether it is allowed by L1.
5185 * The corresponding bit will be cleared unless both of L0 and L1 allow it.
5186 */
5187static void nested_vmx_disable_intercept_for_msr(unsigned long *msr_bitmap_l1,
5188 unsigned long *msr_bitmap_nested,
5189 u32 msr, int type)
5190{
5191 int f = sizeof(unsigned long);
5192
Wincy Vanf2b93282015-02-03 23:56:03 +08005193 /*
5194 * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
5195 * have the write-low and read-high bitmap offsets the wrong way round.
5196 * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
5197 */
5198 if (msr <= 0x1fff) {
5199 if (type & MSR_TYPE_R &&
5200 !test_bit(msr, msr_bitmap_l1 + 0x000 / f))
5201 /* read-low */
5202 __clear_bit(msr, msr_bitmap_nested + 0x000 / f);
5203
5204 if (type & MSR_TYPE_W &&
5205 !test_bit(msr, msr_bitmap_l1 + 0x800 / f))
5206 /* write-low */
5207 __clear_bit(msr, msr_bitmap_nested + 0x800 / f);
5208
5209 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
5210 msr &= 0x1fff;
5211 if (type & MSR_TYPE_R &&
5212 !test_bit(msr, msr_bitmap_l1 + 0x400 / f))
5213 /* read-high */
5214 __clear_bit(msr, msr_bitmap_nested + 0x400 / f);
5215
5216 if (type & MSR_TYPE_W &&
5217 !test_bit(msr, msr_bitmap_l1 + 0xc00 / f))
5218 /* write-high */
5219 __clear_bit(msr, msr_bitmap_nested + 0xc00 / f);
5220
5221 }
5222}
5223
Paolo Bonzini904e14f2018-01-16 16:51:18 +01005224static u8 vmx_msr_bitmap_mode(struct kvm_vcpu *vcpu)
Avi Kivity58972972009-02-24 22:26:47 +02005225{
Paolo Bonzini904e14f2018-01-16 16:51:18 +01005226 u8 mode = 0;
5227
5228 if (cpu_has_secondary_exec_ctrls() &&
5229 (vmcs_read32(SECONDARY_VM_EXEC_CONTROL) &
5230 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE)) {
5231 mode |= MSR_BITMAP_MODE_X2APIC;
5232 if (enable_apicv && kvm_vcpu_apicv_active(vcpu))
5233 mode |= MSR_BITMAP_MODE_X2APIC_APICV;
5234 }
5235
5236 if (is_long_mode(vcpu))
5237 mode |= MSR_BITMAP_MODE_LM;
5238
5239 return mode;
Yang Zhang8d146952013-01-25 10:18:50 +08005240}
5241
Paolo Bonzini904e14f2018-01-16 16:51:18 +01005242#define X2APIC_MSR(r) (APIC_BASE_MSR + ((r) >> 4))
5243
5244static void vmx_update_msr_bitmap_x2apic(unsigned long *msr_bitmap,
5245 u8 mode)
Yang Zhang8d146952013-01-25 10:18:50 +08005246{
Paolo Bonzini904e14f2018-01-16 16:51:18 +01005247 int msr;
5248
5249 for (msr = 0x800; msr <= 0x8ff; msr += BITS_PER_LONG) {
5250 unsigned word = msr / BITS_PER_LONG;
5251 msr_bitmap[word] = (mode & MSR_BITMAP_MODE_X2APIC_APICV) ? 0 : ~0;
5252 msr_bitmap[word + (0x800 / sizeof(long))] = ~0;
Wanpeng Lif6e90f92016-09-22 07:43:25 +08005253 }
Paolo Bonzini904e14f2018-01-16 16:51:18 +01005254
5255 if (mode & MSR_BITMAP_MODE_X2APIC) {
5256 /*
5257 * TPR reads and writes can be virtualized even if virtual interrupt
5258 * delivery is not in use.
5259 */
5260 vmx_disable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_TASKPRI), MSR_TYPE_RW);
5261 if (mode & MSR_BITMAP_MODE_X2APIC_APICV) {
5262 vmx_enable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_TMCCT), MSR_TYPE_R);
5263 vmx_disable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_EOI), MSR_TYPE_W);
5264 vmx_disable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_SELF_IPI), MSR_TYPE_W);
5265 }
5266 }
5267}
5268
5269static void vmx_update_msr_bitmap(struct kvm_vcpu *vcpu)
5270{
5271 struct vcpu_vmx *vmx = to_vmx(vcpu);
5272 unsigned long *msr_bitmap = vmx->vmcs01.msr_bitmap;
5273 u8 mode = vmx_msr_bitmap_mode(vcpu);
5274 u8 changed = mode ^ vmx->msr_bitmap_mode;
5275
5276 if (!changed)
5277 return;
5278
5279 vmx_set_intercept_for_msr(msr_bitmap, MSR_KERNEL_GS_BASE, MSR_TYPE_RW,
5280 !(mode & MSR_BITMAP_MODE_LM));
5281
5282 if (changed & (MSR_BITMAP_MODE_X2APIC | MSR_BITMAP_MODE_X2APIC_APICV))
5283 vmx_update_msr_bitmap_x2apic(msr_bitmap, mode);
5284
5285 vmx->msr_bitmap_mode = mode;
Avi Kivity58972972009-02-24 22:26:47 +02005286}
5287
Suravee Suthikulpanitb2a05fe2017-09-12 10:42:41 -05005288static bool vmx_get_enable_apicv(struct kvm_vcpu *vcpu)
Paolo Bonzinid50ab6c2015-07-29 11:49:59 +02005289{
Andrey Smetanind62caab2015-11-10 15:36:33 +03005290 return enable_apicv;
Paolo Bonzinid50ab6c2015-07-29 11:49:59 +02005291}
5292
David Matlackc9f04402017-08-01 14:00:40 -07005293static void nested_mark_vmcs12_pages_dirty(struct kvm_vcpu *vcpu)
5294{
5295 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
5296 gfn_t gfn;
5297
5298 /*
5299 * Don't need to mark the APIC access page dirty; it is never
5300 * written to by the CPU during APIC virtualization.
5301 */
5302
5303 if (nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW)) {
5304 gfn = vmcs12->virtual_apic_page_addr >> PAGE_SHIFT;
5305 kvm_vcpu_mark_page_dirty(vcpu, gfn);
5306 }
5307
5308 if (nested_cpu_has_posted_intr(vmcs12)) {
5309 gfn = vmcs12->posted_intr_desc_addr >> PAGE_SHIFT;
5310 kvm_vcpu_mark_page_dirty(vcpu, gfn);
5311 }
5312}
5313
5314
David Hildenbrand6342c502017-01-25 11:58:58 +01005315static void vmx_complete_nested_posted_interrupt(struct kvm_vcpu *vcpu)
Wincy Van705699a2015-02-03 23:58:17 +08005316{
5317 struct vcpu_vmx *vmx = to_vmx(vcpu);
5318 int max_irr;
5319 void *vapic_page;
5320 u16 status;
5321
David Matlackc9f04402017-08-01 14:00:40 -07005322 if (!vmx->nested.pi_desc || !vmx->nested.pi_pending)
5323 return;
Wincy Van705699a2015-02-03 23:58:17 +08005324
David Matlackc9f04402017-08-01 14:00:40 -07005325 vmx->nested.pi_pending = false;
5326 if (!pi_test_and_clear_on(vmx->nested.pi_desc))
5327 return;
Wincy Van705699a2015-02-03 23:58:17 +08005328
David Matlackc9f04402017-08-01 14:00:40 -07005329 max_irr = find_last_bit((unsigned long *)vmx->nested.pi_desc->pir, 256);
5330 if (max_irr != 256) {
Wincy Van705699a2015-02-03 23:58:17 +08005331 vapic_page = kmap(vmx->nested.virtual_apic_page);
Liran Alone7387b02017-12-24 18:12:54 +02005332 __kvm_apic_update_irr(vmx->nested.pi_desc->pir,
5333 vapic_page, &max_irr);
Wincy Van705699a2015-02-03 23:58:17 +08005334 kunmap(vmx->nested.virtual_apic_page);
5335
5336 status = vmcs_read16(GUEST_INTR_STATUS);
5337 if ((u8)max_irr > ((u8)status & 0xff)) {
5338 status &= ~0xff;
5339 status |= (u8)max_irr;
5340 vmcs_write16(GUEST_INTR_STATUS, status);
5341 }
5342 }
David Matlackc9f04402017-08-01 14:00:40 -07005343
5344 nested_mark_vmcs12_pages_dirty(vcpu);
Wincy Van705699a2015-02-03 23:58:17 +08005345}
5346
Wincy Van06a55242017-04-28 13:13:59 +08005347static inline bool kvm_vcpu_trigger_posted_interrupt(struct kvm_vcpu *vcpu,
5348 bool nested)
Radim Krčmář21bc8dc2015-02-16 15:36:33 +01005349{
5350#ifdef CONFIG_SMP
Wincy Van06a55242017-04-28 13:13:59 +08005351 int pi_vec = nested ? POSTED_INTR_NESTED_VECTOR : POSTED_INTR_VECTOR;
5352
Radim Krčmář21bc8dc2015-02-16 15:36:33 +01005353 if (vcpu->mode == IN_GUEST_MODE) {
Feng Wu28b835d2015-09-18 22:29:54 +08005354 /*
Haozhong Zhang5753743f2017-09-18 09:56:50 +08005355 * The vector of interrupt to be delivered to vcpu had
5356 * been set in PIR before this function.
Feng Wu28b835d2015-09-18 22:29:54 +08005357 *
Haozhong Zhang5753743f2017-09-18 09:56:50 +08005358 * Following cases will be reached in this block, and
5359 * we always send a notification event in all cases as
5360 * explained below.
5361 *
5362 * Case 1: vcpu keeps in non-root mode. Sending a
5363 * notification event posts the interrupt to vcpu.
5364 *
5365 * Case 2: vcpu exits to root mode and is still
5366 * runnable. PIR will be synced to vIRR before the
5367 * next vcpu entry. Sending a notification event in
5368 * this case has no effect, as vcpu is not in root
5369 * mode.
5370 *
5371 * Case 3: vcpu exits to root mode and is blocked.
5372 * vcpu_block() has already synced PIR to vIRR and
5373 * never blocks vcpu if vIRR is not cleared. Therefore,
5374 * a blocked vcpu here does not wait for any requested
5375 * interrupts in PIR, and sending a notification event
5376 * which has no effect is safe here.
Feng Wu28b835d2015-09-18 22:29:54 +08005377 */
Feng Wu28b835d2015-09-18 22:29:54 +08005378
Wincy Van06a55242017-04-28 13:13:59 +08005379 apic->send_IPI_mask(get_cpu_mask(vcpu->cpu), pi_vec);
Radim Krčmář21bc8dc2015-02-16 15:36:33 +01005380 return true;
5381 }
5382#endif
5383 return false;
5384}
5385
Wincy Van705699a2015-02-03 23:58:17 +08005386static int vmx_deliver_nested_posted_interrupt(struct kvm_vcpu *vcpu,
5387 int vector)
5388{
5389 struct vcpu_vmx *vmx = to_vmx(vcpu);
5390
5391 if (is_guest_mode(vcpu) &&
5392 vector == vmx->nested.posted_intr_nv) {
Wincy Van705699a2015-02-03 23:58:17 +08005393 /*
5394 * If a posted intr is not recognized by hardware,
5395 * we will accomplish it in the next vmentry.
5396 */
5397 vmx->nested.pi_pending = true;
5398 kvm_make_request(KVM_REQ_EVENT, vcpu);
Liran Alon6b697712017-11-09 20:27:20 +02005399 /* the PIR and ON have been set by L1. */
5400 if (!kvm_vcpu_trigger_posted_interrupt(vcpu, true))
5401 kvm_vcpu_kick(vcpu);
Wincy Van705699a2015-02-03 23:58:17 +08005402 return 0;
5403 }
5404 return -1;
5405}
Avi Kivity6aa8b732006-12-10 02:21:36 -08005406/*
Yang Zhanga20ed542013-04-11 19:25:15 +08005407 * Send interrupt to vcpu via posted interrupt way.
5408 * 1. If target vcpu is running(non-root mode), send posted interrupt
5409 * notification to vcpu and hardware will sync PIR to vIRR atomically.
5410 * 2. If target vcpu isn't running(root mode), kick it to pick up the
5411 * interrupt from PIR in next vmentry.
5412 */
5413static void vmx_deliver_posted_interrupt(struct kvm_vcpu *vcpu, int vector)
5414{
5415 struct vcpu_vmx *vmx = to_vmx(vcpu);
5416 int r;
5417
Wincy Van705699a2015-02-03 23:58:17 +08005418 r = vmx_deliver_nested_posted_interrupt(vcpu, vector);
5419 if (!r)
5420 return;
5421
Yang Zhanga20ed542013-04-11 19:25:15 +08005422 if (pi_test_and_set_pir(vector, &vmx->pi_desc))
5423 return;
5424
Paolo Bonzinib95234c2016-12-19 13:57:33 +01005425 /* If a previous notification has sent the IPI, nothing to do. */
5426 if (pi_test_and_set_on(&vmx->pi_desc))
5427 return;
5428
Wincy Van06a55242017-04-28 13:13:59 +08005429 if (!kvm_vcpu_trigger_posted_interrupt(vcpu, false))
Yang Zhanga20ed542013-04-11 19:25:15 +08005430 kvm_vcpu_kick(vcpu);
5431}
5432
Avi Kivity6aa8b732006-12-10 02:21:36 -08005433/*
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005434 * Set up the vmcs's constant host-state fields, i.e., host-state fields that
5435 * will not change in the lifetime of the guest.
5436 * Note that host-state that does change is set elsewhere. E.g., host-state
5437 * that is set differently for each CPU is set in vmx_vcpu_load(), not here.
5438 */
Yang Zhanga547c6d2013-04-11 19:25:10 +08005439static void vmx_set_constant_host_state(struct vcpu_vmx *vmx)
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005440{
5441 u32 low32, high32;
5442 unsigned long tmpl;
5443 struct desc_ptr dt;
Andy Lutomirskid6e41f12017-05-28 10:00:17 -07005444 unsigned long cr0, cr3, cr4;
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005445
Andy Lutomirski04ac88a2016-10-31 15:18:45 -07005446 cr0 = read_cr0();
5447 WARN_ON(cr0 & X86_CR0_TS);
5448 vmcs_writel(HOST_CR0, cr0); /* 22.2.3 */
Andy Lutomirskid6e41f12017-05-28 10:00:17 -07005449
5450 /*
5451 * Save the most likely value for this task's CR3 in the VMCS.
5452 * We can't use __get_current_cr3_fast() because we're not atomic.
5453 */
Andy Lutomirski6c690ee2017-06-12 10:26:14 -07005454 cr3 = __read_cr3();
Andy Lutomirskid6e41f12017-05-28 10:00:17 -07005455 vmcs_writel(HOST_CR3, cr3); /* 22.2.3 FIXME: shadow tables */
Ladi Prosek44889942017-09-22 07:53:15 +02005456 vmx->loaded_vmcs->vmcs_host_cr3 = cr3;
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005457
Andy Lutomirskid974baa2014-10-08 09:02:13 -07005458 /* Save the most likely value for this task's CR4 in the VMCS. */
Andy Lutomirski1e02ce42014-10-24 15:58:08 -07005459 cr4 = cr4_read_shadow();
Andy Lutomirskid974baa2014-10-08 09:02:13 -07005460 vmcs_writel(HOST_CR4, cr4); /* 22.2.3, 22.2.5 */
Ladi Prosek44889942017-09-22 07:53:15 +02005461 vmx->loaded_vmcs->vmcs_host_cr4 = cr4;
Andy Lutomirskid974baa2014-10-08 09:02:13 -07005462
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005463 vmcs_write16(HOST_CS_SELECTOR, __KERNEL_CS); /* 22.2.4 */
Avi Kivityb2da15a2012-05-13 19:53:24 +03005464#ifdef CONFIG_X86_64
5465 /*
5466 * Load null selectors, so we can avoid reloading them in
5467 * __vmx_load_host_state(), in case userspace uses the null selectors
5468 * too (the expected case).
5469 */
5470 vmcs_write16(HOST_DS_SELECTOR, 0);
5471 vmcs_write16(HOST_ES_SELECTOR, 0);
5472#else
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005473 vmcs_write16(HOST_DS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
5474 vmcs_write16(HOST_ES_SELECTOR, __KERNEL_DS); /* 22.2.4 */
Avi Kivityb2da15a2012-05-13 19:53:24 +03005475#endif
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005476 vmcs_write16(HOST_SS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
5477 vmcs_write16(HOST_TR_SELECTOR, GDT_ENTRY_TSS*8); /* 22.2.4 */
5478
Juergen Gross87930012017-09-04 12:25:27 +02005479 store_idt(&dt);
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005480 vmcs_writel(HOST_IDTR_BASE, dt.address); /* 22.2.4 */
Yang Zhanga547c6d2013-04-11 19:25:10 +08005481 vmx->host_idt_base = dt.address;
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005482
Avi Kivity83287ea422012-09-16 15:10:57 +03005483 vmcs_writel(HOST_RIP, vmx_return); /* 22.2.5 */
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005484
5485 rdmsr(MSR_IA32_SYSENTER_CS, low32, high32);
5486 vmcs_write32(HOST_IA32_SYSENTER_CS, low32);
5487 rdmsrl(MSR_IA32_SYSENTER_EIP, tmpl);
5488 vmcs_writel(HOST_IA32_SYSENTER_EIP, tmpl); /* 22.2.3 */
5489
5490 if (vmcs_config.vmexit_ctrl & VM_EXIT_LOAD_IA32_PAT) {
5491 rdmsr(MSR_IA32_CR_PAT, low32, high32);
5492 vmcs_write64(HOST_IA32_PAT, low32 | ((u64) high32 << 32));
5493 }
5494}
5495
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005496static void set_cr4_guest_host_mask(struct vcpu_vmx *vmx)
5497{
5498 vmx->vcpu.arch.cr4_guest_owned_bits = KVM_CR4_GUEST_OWNED_BITS;
5499 if (enable_ept)
5500 vmx->vcpu.arch.cr4_guest_owned_bits |= X86_CR4_PGE;
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03005501 if (is_guest_mode(&vmx->vcpu))
5502 vmx->vcpu.arch.cr4_guest_owned_bits &=
5503 ~get_vmcs12(&vmx->vcpu)->cr4_guest_host_mask;
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005504 vmcs_writel(CR4_GUEST_HOST_MASK, ~vmx->vcpu.arch.cr4_guest_owned_bits);
5505}
5506
Yang Zhang01e439b2013-04-11 19:25:12 +08005507static u32 vmx_pin_based_exec_ctrl(struct vcpu_vmx *vmx)
5508{
5509 u32 pin_based_exec_ctrl = vmcs_config.pin_based_exec_ctrl;
5510
Andrey Smetanind62caab2015-11-10 15:36:33 +03005511 if (!kvm_vcpu_apicv_active(&vmx->vcpu))
Yang Zhang01e439b2013-04-11 19:25:12 +08005512 pin_based_exec_ctrl &= ~PIN_BASED_POSTED_INTR;
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01005513
5514 if (!enable_vnmi)
5515 pin_based_exec_ctrl &= ~PIN_BASED_VIRTUAL_NMIS;
5516
Yunhong Jiang64672c92016-06-13 14:19:59 -07005517 /* Enable the preemption timer dynamically */
5518 pin_based_exec_ctrl &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
Yang Zhang01e439b2013-04-11 19:25:12 +08005519 return pin_based_exec_ctrl;
5520}
5521
Andrey Smetanind62caab2015-11-10 15:36:33 +03005522static void vmx_refresh_apicv_exec_ctrl(struct kvm_vcpu *vcpu)
5523{
5524 struct vcpu_vmx *vmx = to_vmx(vcpu);
5525
5526 vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, vmx_pin_based_exec_ctrl(vmx));
Roman Kagan3ce424e2016-05-18 17:48:20 +03005527 if (cpu_has_secondary_exec_ctrls()) {
5528 if (kvm_vcpu_apicv_active(vcpu))
5529 vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL,
5530 SECONDARY_EXEC_APIC_REGISTER_VIRT |
5531 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
5532 else
5533 vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL,
5534 SECONDARY_EXEC_APIC_REGISTER_VIRT |
5535 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
5536 }
5537
5538 if (cpu_has_vmx_msr_bitmap())
Paolo Bonzini904e14f2018-01-16 16:51:18 +01005539 vmx_update_msr_bitmap(vcpu);
Andrey Smetanind62caab2015-11-10 15:36:33 +03005540}
5541
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005542static u32 vmx_exec_control(struct vcpu_vmx *vmx)
5543{
5544 u32 exec_control = vmcs_config.cpu_based_exec_ctrl;
Paolo Bonzinid16c2932014-02-21 10:36:37 +01005545
5546 if (vmx->vcpu.arch.switch_db_regs & KVM_DEBUGREG_WONT_EXIT)
5547 exec_control &= ~CPU_BASED_MOV_DR_EXITING;
5548
Paolo Bonzini35754c92015-07-29 12:05:37 +02005549 if (!cpu_need_tpr_shadow(&vmx->vcpu)) {
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005550 exec_control &= ~CPU_BASED_TPR_SHADOW;
5551#ifdef CONFIG_X86_64
5552 exec_control |= CPU_BASED_CR8_STORE_EXITING |
5553 CPU_BASED_CR8_LOAD_EXITING;
5554#endif
5555 }
5556 if (!enable_ept)
5557 exec_control |= CPU_BASED_CR3_STORE_EXITING |
5558 CPU_BASED_CR3_LOAD_EXITING |
5559 CPU_BASED_INVLPG_EXITING;
Wanpeng Li4d5422c2018-03-12 04:53:02 -07005560 if (kvm_mwait_in_guest(vmx->vcpu.kvm))
5561 exec_control &= ~(CPU_BASED_MWAIT_EXITING |
5562 CPU_BASED_MONITOR_EXITING);
Wanpeng Licaa057a2018-03-12 04:53:03 -07005563 if (kvm_hlt_in_guest(vmx->vcpu.kvm))
5564 exec_control &= ~CPU_BASED_HLT_EXITING;
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005565 return exec_control;
5566}
5567
Jim Mattson45ec3682017-08-23 16:32:04 -07005568static bool vmx_rdrand_supported(void)
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005569{
Jim Mattson45ec3682017-08-23 16:32:04 -07005570 return vmcs_config.cpu_based_2nd_exec_ctrl &
David Hildenbrand736fdf72017-08-24 20:51:37 +02005571 SECONDARY_EXEC_RDRAND_EXITING;
Jim Mattson45ec3682017-08-23 16:32:04 -07005572}
5573
Jim Mattson75f4fc82017-08-23 16:32:03 -07005574static bool vmx_rdseed_supported(void)
5575{
5576 return vmcs_config.cpu_based_2nd_exec_ctrl &
David Hildenbrand736fdf72017-08-24 20:51:37 +02005577 SECONDARY_EXEC_RDSEED_EXITING;
Jim Mattson75f4fc82017-08-23 16:32:03 -07005578}
5579
Paolo Bonzini80154d72017-08-24 13:55:35 +02005580static void vmx_compute_secondary_exec_control(struct vcpu_vmx *vmx)
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005581{
Paolo Bonzini80154d72017-08-24 13:55:35 +02005582 struct kvm_vcpu *vcpu = &vmx->vcpu;
5583
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005584 u32 exec_control = vmcs_config.cpu_based_2nd_exec_ctrl;
Paolo Bonzini0367f202016-07-12 10:44:55 +02005585
Paolo Bonzini80154d72017-08-24 13:55:35 +02005586 if (!cpu_need_virtualize_apic_accesses(vcpu))
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005587 exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
5588 if (vmx->vpid == 0)
5589 exec_control &= ~SECONDARY_EXEC_ENABLE_VPID;
5590 if (!enable_ept) {
5591 exec_control &= ~SECONDARY_EXEC_ENABLE_EPT;
5592 enable_unrestricted_guest = 0;
Mao, Junjiead756a12012-07-02 01:18:48 +00005593 /* Enable INVPCID for non-ept guests may cause performance regression. */
5594 exec_control &= ~SECONDARY_EXEC_ENABLE_INVPCID;
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005595 }
5596 if (!enable_unrestricted_guest)
5597 exec_control &= ~SECONDARY_EXEC_UNRESTRICTED_GUEST;
5598 if (!ple_gap)
5599 exec_control &= ~SECONDARY_EXEC_PAUSE_LOOP_EXITING;
Paolo Bonzini80154d72017-08-24 13:55:35 +02005600 if (!kvm_vcpu_apicv_active(vcpu))
Yang Zhangc7c9c562013-01-25 10:18:51 +08005601 exec_control &= ~(SECONDARY_EXEC_APIC_REGISTER_VIRT |
5602 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
Yang Zhang8d146952013-01-25 10:18:50 +08005603 exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
Paolo Bonzini0367f202016-07-12 10:44:55 +02005604
5605 /* SECONDARY_EXEC_DESC is enabled/disabled on writes to CR4.UMIP,
5606 * in vmx_set_cr4. */
5607 exec_control &= ~SECONDARY_EXEC_DESC;
5608
Abel Gordonabc4fc52013-04-18 14:35:25 +03005609 /* SECONDARY_EXEC_SHADOW_VMCS is enabled when L1 executes VMPTRLD
5610 (handle_vmptrld).
5611 We can NOT enable shadow_vmcs here because we don't have yet
5612 a current VMCS12
5613 */
5614 exec_control &= ~SECONDARY_EXEC_SHADOW_VMCS;
Kai Huanga3eaa862015-11-04 13:46:05 +08005615
5616 if (!enable_pml)
5617 exec_control &= ~SECONDARY_EXEC_ENABLE_PML;
Kai Huang843e4332015-01-28 10:54:28 +08005618
Paolo Bonzini3db13482017-08-24 14:48:03 +02005619 if (vmx_xsaves_supported()) {
5620 /* Exposing XSAVES only when XSAVE is exposed */
5621 bool xsaves_enabled =
5622 guest_cpuid_has(vcpu, X86_FEATURE_XSAVE) &&
5623 guest_cpuid_has(vcpu, X86_FEATURE_XSAVES);
5624
5625 if (!xsaves_enabled)
5626 exec_control &= ~SECONDARY_EXEC_XSAVES;
5627
5628 if (nested) {
5629 if (xsaves_enabled)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01005630 vmx->nested.msrs.secondary_ctls_high |=
Paolo Bonzini3db13482017-08-24 14:48:03 +02005631 SECONDARY_EXEC_XSAVES;
5632 else
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01005633 vmx->nested.msrs.secondary_ctls_high &=
Paolo Bonzini3db13482017-08-24 14:48:03 +02005634 ~SECONDARY_EXEC_XSAVES;
5635 }
5636 }
5637
Paolo Bonzini80154d72017-08-24 13:55:35 +02005638 if (vmx_rdtscp_supported()) {
5639 bool rdtscp_enabled = guest_cpuid_has(vcpu, X86_FEATURE_RDTSCP);
5640 if (!rdtscp_enabled)
5641 exec_control &= ~SECONDARY_EXEC_RDTSCP;
5642
5643 if (nested) {
5644 if (rdtscp_enabled)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01005645 vmx->nested.msrs.secondary_ctls_high |=
Paolo Bonzini80154d72017-08-24 13:55:35 +02005646 SECONDARY_EXEC_RDTSCP;
5647 else
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01005648 vmx->nested.msrs.secondary_ctls_high &=
Paolo Bonzini80154d72017-08-24 13:55:35 +02005649 ~SECONDARY_EXEC_RDTSCP;
5650 }
5651 }
5652
5653 if (vmx_invpcid_supported()) {
5654 /* Exposing INVPCID only when PCID is exposed */
5655 bool invpcid_enabled =
5656 guest_cpuid_has(vcpu, X86_FEATURE_INVPCID) &&
5657 guest_cpuid_has(vcpu, X86_FEATURE_PCID);
5658
5659 if (!invpcid_enabled) {
5660 exec_control &= ~SECONDARY_EXEC_ENABLE_INVPCID;
5661 guest_cpuid_clear(vcpu, X86_FEATURE_INVPCID);
5662 }
5663
5664 if (nested) {
5665 if (invpcid_enabled)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01005666 vmx->nested.msrs.secondary_ctls_high |=
Paolo Bonzini80154d72017-08-24 13:55:35 +02005667 SECONDARY_EXEC_ENABLE_INVPCID;
5668 else
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01005669 vmx->nested.msrs.secondary_ctls_high &=
Paolo Bonzini80154d72017-08-24 13:55:35 +02005670 ~SECONDARY_EXEC_ENABLE_INVPCID;
5671 }
5672 }
5673
Jim Mattson45ec3682017-08-23 16:32:04 -07005674 if (vmx_rdrand_supported()) {
5675 bool rdrand_enabled = guest_cpuid_has(vcpu, X86_FEATURE_RDRAND);
5676 if (rdrand_enabled)
David Hildenbrand736fdf72017-08-24 20:51:37 +02005677 exec_control &= ~SECONDARY_EXEC_RDRAND_EXITING;
Jim Mattson45ec3682017-08-23 16:32:04 -07005678
5679 if (nested) {
5680 if (rdrand_enabled)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01005681 vmx->nested.msrs.secondary_ctls_high |=
David Hildenbrand736fdf72017-08-24 20:51:37 +02005682 SECONDARY_EXEC_RDRAND_EXITING;
Jim Mattson45ec3682017-08-23 16:32:04 -07005683 else
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01005684 vmx->nested.msrs.secondary_ctls_high &=
David Hildenbrand736fdf72017-08-24 20:51:37 +02005685 ~SECONDARY_EXEC_RDRAND_EXITING;
Jim Mattson45ec3682017-08-23 16:32:04 -07005686 }
5687 }
5688
Jim Mattson75f4fc82017-08-23 16:32:03 -07005689 if (vmx_rdseed_supported()) {
5690 bool rdseed_enabled = guest_cpuid_has(vcpu, X86_FEATURE_RDSEED);
5691 if (rdseed_enabled)
David Hildenbrand736fdf72017-08-24 20:51:37 +02005692 exec_control &= ~SECONDARY_EXEC_RDSEED_EXITING;
Jim Mattson75f4fc82017-08-23 16:32:03 -07005693
5694 if (nested) {
5695 if (rdseed_enabled)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01005696 vmx->nested.msrs.secondary_ctls_high |=
David Hildenbrand736fdf72017-08-24 20:51:37 +02005697 SECONDARY_EXEC_RDSEED_EXITING;
Jim Mattson75f4fc82017-08-23 16:32:03 -07005698 else
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01005699 vmx->nested.msrs.secondary_ctls_high &=
David Hildenbrand736fdf72017-08-24 20:51:37 +02005700 ~SECONDARY_EXEC_RDSEED_EXITING;
Jim Mattson75f4fc82017-08-23 16:32:03 -07005701 }
5702 }
5703
Paolo Bonzini80154d72017-08-24 13:55:35 +02005704 vmx->secondary_exec_control = exec_control;
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005705}
5706
Xiao Guangrongce88dec2011-07-12 03:33:44 +08005707static void ept_set_mmio_spte_mask(void)
5708{
5709 /*
5710 * EPT Misconfigurations can be generated if the value of bits 2:0
5711 * of an EPT paging-structure entry is 110b (write/execute).
Xiao Guangrongce88dec2011-07-12 03:33:44 +08005712 */
Peter Feinerdcdca5f2017-06-30 17:26:30 -07005713 kvm_mmu_set_mmio_spte_mask(VMX_EPT_RWX_MASK,
5714 VMX_EPT_MISCONFIG_WX_VALUE);
Xiao Guangrongce88dec2011-07-12 03:33:44 +08005715}
5716
Wanpeng Lif53cd632014-12-02 19:14:58 +08005717#define VMX_XSS_EXIT_BITMAP 0
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005718/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08005719 * Sets up the vmcs for emulated real mode.
5720 */
David Hildenbrand12d79912017-08-24 20:51:26 +02005721static void vmx_vcpu_setup(struct vcpu_vmx *vmx)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005722{
Jan Kiszka2e4ce7f2011-06-01 12:57:30 +02005723#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08005724 unsigned long a;
Jan Kiszka2e4ce7f2011-06-01 12:57:30 +02005725#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -08005726 int i;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005727
Abel Gordon4607c2d2013-04-18 14:35:55 +03005728 if (enable_shadow_vmcs) {
5729 vmcs_write64(VMREAD_BITMAP, __pa(vmx_vmread_bitmap));
5730 vmcs_write64(VMWRITE_BITMAP, __pa(vmx_vmwrite_bitmap));
5731 }
Sheng Yang25c5f222008-03-28 13:18:56 +08005732 if (cpu_has_vmx_msr_bitmap())
Paolo Bonzini904e14f2018-01-16 16:51:18 +01005733 vmcs_write64(MSR_BITMAP, __pa(vmx->vmcs01.msr_bitmap));
Sheng Yang25c5f222008-03-28 13:18:56 +08005734
Avi Kivity6aa8b732006-12-10 02:21:36 -08005735 vmcs_write64(VMCS_LINK_POINTER, -1ull); /* 22.3.1.5 */
5736
Avi Kivity6aa8b732006-12-10 02:21:36 -08005737 /* Control */
Yang Zhang01e439b2013-04-11 19:25:12 +08005738 vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, vmx_pin_based_exec_ctrl(vmx));
Yunhong Jiang64672c92016-06-13 14:19:59 -07005739 vmx->hv_deadline_tsc = -1;
Yang, Sheng6e5d8652007-09-12 18:03:11 +08005740
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005741 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, vmx_exec_control(vmx));
Avi Kivity6aa8b732006-12-10 02:21:36 -08005742
Dan Williamsdfa169b2016-06-02 11:17:24 -07005743 if (cpu_has_secondary_exec_ctrls()) {
Paolo Bonzini80154d72017-08-24 13:55:35 +02005744 vmx_compute_secondary_exec_control(vmx);
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005745 vmcs_write32(SECONDARY_VM_EXEC_CONTROL,
Paolo Bonzini80154d72017-08-24 13:55:35 +02005746 vmx->secondary_exec_control);
Dan Williamsdfa169b2016-06-02 11:17:24 -07005747 }
Sheng Yangf78e0e22007-10-29 09:40:42 +08005748
Andrey Smetanind62caab2015-11-10 15:36:33 +03005749 if (kvm_vcpu_apicv_active(&vmx->vcpu)) {
Yang Zhangc7c9c562013-01-25 10:18:51 +08005750 vmcs_write64(EOI_EXIT_BITMAP0, 0);
5751 vmcs_write64(EOI_EXIT_BITMAP1, 0);
5752 vmcs_write64(EOI_EXIT_BITMAP2, 0);
5753 vmcs_write64(EOI_EXIT_BITMAP3, 0);
5754
5755 vmcs_write16(GUEST_INTR_STATUS, 0);
Yang Zhang01e439b2013-04-11 19:25:12 +08005756
Li RongQing0bcf2612015-12-03 13:29:34 +08005757 vmcs_write16(POSTED_INTR_NV, POSTED_INTR_VECTOR);
Yang Zhang01e439b2013-04-11 19:25:12 +08005758 vmcs_write64(POSTED_INTR_DESC_ADDR, __pa((&vmx->pi_desc)));
Yang Zhangc7c9c562013-01-25 10:18:51 +08005759 }
5760
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08005761 if (ple_gap) {
5762 vmcs_write32(PLE_GAP, ple_gap);
Radim Krčmářa7653ec2014-08-21 18:08:07 +02005763 vmx->ple_window = ple_window;
5764 vmx->ple_window_dirty = true;
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08005765 }
5766
Xiao Guangrongc3707952011-07-12 03:28:04 +08005767 vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK, 0);
5768 vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005769 vmcs_write32(CR3_TARGET_COUNT, 0); /* 22.2.1 */
5770
Avi Kivity9581d442010-10-19 16:46:55 +02005771 vmcs_write16(HOST_FS_SELECTOR, 0); /* 22.2.4 */
5772 vmcs_write16(HOST_GS_SELECTOR, 0); /* 22.2.4 */
Yang Zhanga547c6d2013-04-11 19:25:10 +08005773 vmx_set_constant_host_state(vmx);
Avi Kivity05b3e0c2006-12-13 00:33:45 -08005774#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08005775 rdmsrl(MSR_FS_BASE, a);
5776 vmcs_writel(HOST_FS_BASE, a); /* 22.2.4 */
5777 rdmsrl(MSR_GS_BASE, a);
5778 vmcs_writel(HOST_GS_BASE, a); /* 22.2.4 */
5779#else
5780 vmcs_writel(HOST_FS_BASE, 0); /* 22.2.4 */
5781 vmcs_writel(HOST_GS_BASE, 0); /* 22.2.4 */
5782#endif
5783
Bandan Das2a499e42017-08-03 15:54:41 -04005784 if (cpu_has_vmx_vmfunc())
5785 vmcs_write64(VM_FUNCTION_CONTROL, 0);
5786
Eddie Dong2cc51562007-05-21 07:28:09 +03005787 vmcs_write32(VM_EXIT_MSR_STORE_COUNT, 0);
5788 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, 0);
Avi Kivity61d2ef22010-04-28 16:40:38 +03005789 vmcs_write64(VM_EXIT_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.host));
Eddie Dong2cc51562007-05-21 07:28:09 +03005790 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, 0);
Avi Kivity61d2ef22010-04-28 16:40:38 +03005791 vmcs_write64(VM_ENTRY_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.guest));
Avi Kivity6aa8b732006-12-10 02:21:36 -08005792
Radim Krčmář74545702015-04-27 15:11:25 +02005793 if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT)
5794 vmcs_write64(GUEST_IA32_PAT, vmx->vcpu.arch.pat);
Sheng Yang468d4722008-10-09 16:01:55 +08005795
Paolo Bonzini03916db2014-07-24 14:21:57 +02005796 for (i = 0; i < ARRAY_SIZE(vmx_msr_index); ++i) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08005797 u32 index = vmx_msr_index[i];
5798 u32 data_low, data_high;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04005799 int j = vmx->nmsrs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005800
5801 if (rdmsr_safe(index, &data_low, &data_high) < 0)
5802 continue;
Avi Kivity432bd6c2007-01-31 23:48:13 -08005803 if (wrmsr_safe(index, data_low, data_high) < 0)
5804 continue;
Avi Kivity26bb0982009-09-07 11:14:12 +03005805 vmx->guest_msrs[j].index = i;
5806 vmx->guest_msrs[j].data = 0;
Avi Kivityd5696722009-12-02 12:28:47 +02005807 vmx->guest_msrs[j].mask = -1ull;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04005808 ++vmx->nmsrs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005809 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08005810
KarimAllah Ahmed28c1c9f2018-02-01 22:59:44 +01005811 if (boot_cpu_has(X86_FEATURE_ARCH_CAPABILITIES))
5812 rdmsrl(MSR_IA32_ARCH_CAPABILITIES, vmx->arch_capabilities);
Gleb Natapov2961e8762013-11-25 15:37:13 +02005813
5814 vm_exit_controls_init(vmx, vmcs_config.vmexit_ctrl);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005815
5816 /* 22.2.1, 20.8.1 */
Gleb Natapov2961e8762013-11-25 15:37:13 +02005817 vm_entry_controls_init(vmx, vmcs_config.vmentry_ctrl);
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03005818
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -08005819 vmx->vcpu.arch.cr0_guest_owned_bits = X86_CR0_TS;
5820 vmcs_writel(CR0_GUEST_HOST_MASK, ~X86_CR0_TS);
5821
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005822 set_cr4_guest_host_mask(vmx);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005823
Wanpeng Lif53cd632014-12-02 19:14:58 +08005824 if (vmx_xsaves_supported())
5825 vmcs_write64(XSS_EXIT_BITMAP, VMX_XSS_EXIT_BITMAP);
5826
Peter Feiner4e595162016-07-07 14:49:58 -07005827 if (enable_pml) {
5828 ASSERT(vmx->pml_pg);
5829 vmcs_write64(PML_ADDRESS, page_to_phys(vmx->pml_pg));
5830 vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1);
5831 }
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005832}
5833
Nadav Amitd28bc9d2015-04-13 14:34:08 +03005834static void vmx_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event)
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005835{
5836 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jan Kiszka58cb6282014-01-24 16:48:44 +01005837 struct msr_data apic_base_msr;
Nadav Amitd28bc9d2015-04-13 14:34:08 +03005838 u64 cr0;
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005839
Avi Kivity7ffd92c2009-06-09 14:10:45 +03005840 vmx->rmode.vm86_active = 0;
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01005841 vmx->spec_ctrl = 0;
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005842
Wanpeng Li518e7b92018-02-28 14:03:31 +08005843 vcpu->arch.microcode_version = 0x100000000ULL;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08005844 vmx->vcpu.arch.regs[VCPU_REGS_RDX] = get_rdx_init_val();
Nadav Amitd28bc9d2015-04-13 14:34:08 +03005845 kvm_set_cr8(vcpu, 0);
5846
5847 if (!init_event) {
5848 apic_base_msr.data = APIC_DEFAULT_PHYS_BASE |
5849 MSR_IA32_APICBASE_ENABLE;
5850 if (kvm_vcpu_is_reset_bsp(vcpu))
5851 apic_base_msr.data |= MSR_IA32_APICBASE_BSP;
5852 apic_base_msr.host_initiated = true;
5853 kvm_set_apic_base(vcpu, &apic_base_msr);
5854 }
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005855
Avi Kivity2fb92db2011-04-27 19:42:18 +03005856 vmx_segment_cache_clear(vmx);
5857
Avi Kivity5706be02008-08-20 15:07:31 +03005858 seg_setup(VCPU_SREG_CS);
Jan Kiszka66450a22013-03-13 12:42:34 +01005859 vmcs_write16(GUEST_CS_SELECTOR, 0xf000);
Paolo Bonzinif3531052015-12-03 15:49:56 +01005860 vmcs_writel(GUEST_CS_BASE, 0xffff0000ul);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005861
5862 seg_setup(VCPU_SREG_DS);
5863 seg_setup(VCPU_SREG_ES);
5864 seg_setup(VCPU_SREG_FS);
5865 seg_setup(VCPU_SREG_GS);
5866 seg_setup(VCPU_SREG_SS);
5867
5868 vmcs_write16(GUEST_TR_SELECTOR, 0);
5869 vmcs_writel(GUEST_TR_BASE, 0);
5870 vmcs_write32(GUEST_TR_LIMIT, 0xffff);
5871 vmcs_write32(GUEST_TR_AR_BYTES, 0x008b);
5872
5873 vmcs_write16(GUEST_LDTR_SELECTOR, 0);
5874 vmcs_writel(GUEST_LDTR_BASE, 0);
5875 vmcs_write32(GUEST_LDTR_LIMIT, 0xffff);
5876 vmcs_write32(GUEST_LDTR_AR_BYTES, 0x00082);
5877
Nadav Amitd28bc9d2015-04-13 14:34:08 +03005878 if (!init_event) {
5879 vmcs_write32(GUEST_SYSENTER_CS, 0);
5880 vmcs_writel(GUEST_SYSENTER_ESP, 0);
5881 vmcs_writel(GUEST_SYSENTER_EIP, 0);
5882 vmcs_write64(GUEST_IA32_DEBUGCTL, 0);
5883 }
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005884
Wanpeng Lic37c2872017-11-20 14:52:21 -08005885 kvm_set_rflags(vcpu, X86_EFLAGS_FIXED);
Jan Kiszka66450a22013-03-13 12:42:34 +01005886 kvm_rip_write(vcpu, 0xfff0);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005887
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005888 vmcs_writel(GUEST_GDTR_BASE, 0);
5889 vmcs_write32(GUEST_GDTR_LIMIT, 0xffff);
5890
5891 vmcs_writel(GUEST_IDTR_BASE, 0);
5892 vmcs_write32(GUEST_IDTR_LIMIT, 0xffff);
5893
Anthony Liguori443381a2010-12-06 10:53:38 -06005894 vmcs_write32(GUEST_ACTIVITY_STATE, GUEST_ACTIVITY_ACTIVE);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005895 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, 0);
Paolo Bonzinif3531052015-12-03 15:49:56 +01005896 vmcs_writel(GUEST_PENDING_DBG_EXCEPTIONS, 0);
Wanpeng Lia554d202017-10-11 05:10:19 -07005897 if (kvm_mpx_supported())
5898 vmcs_write64(GUEST_BNDCFGS, 0);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005899
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005900 setup_msrs(vmx);
5901
Avi Kivity6aa8b732006-12-10 02:21:36 -08005902 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0); /* 22.2.1 */
5903
Nadav Amitd28bc9d2015-04-13 14:34:08 +03005904 if (cpu_has_vmx_tpr_shadow() && !init_event) {
Sheng Yangf78e0e22007-10-29 09:40:42 +08005905 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, 0);
Paolo Bonzini35754c92015-07-29 12:05:37 +02005906 if (cpu_need_tpr_shadow(vcpu))
Sheng Yangf78e0e22007-10-29 09:40:42 +08005907 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR,
Nadav Amitd28bc9d2015-04-13 14:34:08 +03005908 __pa(vcpu->arch.apic->regs));
Sheng Yangf78e0e22007-10-29 09:40:42 +08005909 vmcs_write32(TPR_THRESHOLD, 0);
5910 }
5911
Paolo Bonzinia73896c2014-11-02 07:54:30 +01005912 kvm_make_request(KVM_REQ_APIC_PAGE_RELOAD, vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005913
Sheng Yang2384d2b2008-01-17 15:14:33 +08005914 if (vmx->vpid != 0)
5915 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid);
5916
Nadav Amitd28bc9d2015-04-13 14:34:08 +03005917 cr0 = X86_CR0_NW | X86_CR0_CD | X86_CR0_ET;
Nadav Amitd28bc9d2015-04-13 14:34:08 +03005918 vmx->vcpu.arch.cr0 = cr0;
Bruce Rogersf2463242016-04-28 14:49:21 -06005919 vmx_set_cr0(vcpu, cr0); /* enter rmode */
Nadav Amitd28bc9d2015-04-13 14:34:08 +03005920 vmx_set_cr4(vcpu, 0);
Paolo Bonzini56908912015-10-19 11:30:19 +02005921 vmx_set_efer(vcpu, 0);
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -08005922
Nadav Amitd28bc9d2015-04-13 14:34:08 +03005923 update_exception_bitmap(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005924
Wanpeng Lidd5f5342015-09-23 18:26:57 +08005925 vpid_sync_context(vmx->vpid);
Wanpeng Licaa057a2018-03-12 04:53:03 -07005926 if (init_event)
5927 vmx_clear_hlt(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005928}
5929
Nadav Har'Elb6f12502011-05-25 23:13:06 +03005930/*
5931 * In nested virtualization, check if L1 asked to exit on external interrupts.
5932 * For most existing hypervisors, this will always return true.
5933 */
5934static bool nested_exit_on_intr(struct kvm_vcpu *vcpu)
5935{
5936 return get_vmcs12(vcpu)->pin_based_vm_exec_control &
5937 PIN_BASED_EXT_INTR_MASK;
5938}
5939
Bandan Das77b0f5d2014-04-19 18:17:45 -04005940/*
5941 * In nested virtualization, check if L1 has set
5942 * VM_EXIT_ACK_INTR_ON_EXIT
5943 */
5944static bool nested_exit_intr_ack_set(struct kvm_vcpu *vcpu)
5945{
5946 return get_vmcs12(vcpu)->vm_exit_controls &
5947 VM_EXIT_ACK_INTR_ON_EXIT;
5948}
5949
Jan Kiszkaea8ceb82013-04-14 21:04:26 +02005950static bool nested_exit_on_nmi(struct kvm_vcpu *vcpu)
5951{
Krish Sadhukhan0c7f6502018-02-20 21:24:39 -05005952 return nested_cpu_has_nmi_exiting(get_vmcs12(vcpu));
Jan Kiszkaea8ceb82013-04-14 21:04:26 +02005953}
5954
Jan Kiszkac9a79532014-03-07 20:03:15 +01005955static void enable_irq_window(struct kvm_vcpu *vcpu)
Jan Kiszka3b86cd92008-09-26 09:30:57 +02005956{
Paolo Bonzini47c01522016-12-19 11:44:07 +01005957 vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL,
5958 CPU_BASED_VIRTUAL_INTR_PENDING);
Jan Kiszka3b86cd92008-09-26 09:30:57 +02005959}
5960
Jan Kiszkac9a79532014-03-07 20:03:15 +01005961static void enable_nmi_window(struct kvm_vcpu *vcpu)
Jan Kiszka3b86cd92008-09-26 09:30:57 +02005962{
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01005963 if (!enable_vnmi ||
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01005964 vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & GUEST_INTR_STATE_STI) {
Jan Kiszkac9a79532014-03-07 20:03:15 +01005965 enable_irq_window(vcpu);
5966 return;
5967 }
Jan Kiszka03b28f82013-04-29 16:46:42 +02005968
Paolo Bonzini47c01522016-12-19 11:44:07 +01005969 vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL,
5970 CPU_BASED_VIRTUAL_NMI_PENDING);
Jan Kiszka3b86cd92008-09-26 09:30:57 +02005971}
5972
Gleb Natapov66fd3f72009-05-11 13:35:50 +03005973static void vmx_inject_irq(struct kvm_vcpu *vcpu)
Eddie Dong85f455f2007-07-06 12:20:49 +03005974{
Avi Kivity9c8cba32007-11-22 11:42:59 +02005975 struct vcpu_vmx *vmx = to_vmx(vcpu);
Gleb Natapov66fd3f72009-05-11 13:35:50 +03005976 uint32_t intr;
5977 int irq = vcpu->arch.interrupt.nr;
Avi Kivity9c8cba32007-11-22 11:42:59 +02005978
Marcelo Tosatti229456f2009-06-17 09:22:14 -03005979 trace_kvm_inj_virq(irq);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04005980
Avi Kivityfa89a812008-09-01 15:57:51 +03005981 ++vcpu->stat.irq_injections;
Avi Kivity7ffd92c2009-06-09 14:10:45 +03005982 if (vmx->rmode.vm86_active) {
Serge E. Hallyn71f98332011-04-13 09:12:54 -05005983 int inc_eip = 0;
5984 if (vcpu->arch.interrupt.soft)
5985 inc_eip = vcpu->arch.event_exit_inst_len;
5986 if (kvm_inject_realmode_interrupt(vcpu, irq, inc_eip) != EMULATE_DONE)
Mohammed Gamala92601b2010-09-19 14:34:07 +02005987 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Eddie Dong85f455f2007-07-06 12:20:49 +03005988 return;
5989 }
Gleb Natapov66fd3f72009-05-11 13:35:50 +03005990 intr = irq | INTR_INFO_VALID_MASK;
5991 if (vcpu->arch.interrupt.soft) {
5992 intr |= INTR_TYPE_SOFT_INTR;
5993 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
5994 vmx->vcpu.arch.event_exit_inst_len);
5995 } else
5996 intr |= INTR_TYPE_EXT_INTR;
5997 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr);
Wanpeng Licaa057a2018-03-12 04:53:03 -07005998
5999 vmx_clear_hlt(vcpu);
Eddie Dong85f455f2007-07-06 12:20:49 +03006000}
6001
Sheng Yangf08864b2008-05-15 18:23:25 +08006002static void vmx_inject_nmi(struct kvm_vcpu *vcpu)
6003{
Jan Kiszka66a5a342008-09-26 09:30:51 +02006004 struct vcpu_vmx *vmx = to_vmx(vcpu);
6005
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01006006 if (!enable_vnmi) {
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01006007 /*
6008 * Tracking the NMI-blocked state in software is built upon
6009 * finding the next open IRQ window. This, in turn, depends on
6010 * well-behaving guests: They have to keep IRQs disabled at
6011 * least as long as the NMI handler runs. Otherwise we may
6012 * cause NMI nesting, maybe breaking the guest. But as this is
6013 * highly unlikely, we can live with the residual risk.
6014 */
6015 vmx->loaded_vmcs->soft_vnmi_blocked = 1;
6016 vmx->loaded_vmcs->vnmi_blocked_time = 0;
6017 }
6018
Paolo Bonzini4c4a6f72017-07-14 13:36:11 +02006019 ++vcpu->stat.nmi_injections;
6020 vmx->loaded_vmcs->nmi_known_unmasked = false;
Jan Kiszka3b86cd92008-09-26 09:30:57 +02006021
Avi Kivity7ffd92c2009-06-09 14:10:45 +03006022 if (vmx->rmode.vm86_active) {
Serge E. Hallyn71f98332011-04-13 09:12:54 -05006023 if (kvm_inject_realmode_interrupt(vcpu, NMI_VECTOR, 0) != EMULATE_DONE)
Mohammed Gamala92601b2010-09-19 14:34:07 +02006024 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Jan Kiszka66a5a342008-09-26 09:30:51 +02006025 return;
6026 }
Wanpeng Lic5a6d5f2016-09-22 17:55:54 +08006027
Sheng Yangf08864b2008-05-15 18:23:25 +08006028 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
6029 INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK | NMI_VECTOR);
Wanpeng Licaa057a2018-03-12 04:53:03 -07006030
6031 vmx_clear_hlt(vcpu);
Sheng Yangf08864b2008-05-15 18:23:25 +08006032}
6033
Jan Kiszka3cfc3092009-11-12 01:04:25 +01006034static bool vmx_get_nmi_mask(struct kvm_vcpu *vcpu)
6035{
Paolo Bonzini4c4a6f72017-07-14 13:36:11 +02006036 struct vcpu_vmx *vmx = to_vmx(vcpu);
6037 bool masked;
6038
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01006039 if (!enable_vnmi)
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01006040 return vmx->loaded_vmcs->soft_vnmi_blocked;
Paolo Bonzini4c4a6f72017-07-14 13:36:11 +02006041 if (vmx->loaded_vmcs->nmi_known_unmasked)
Avi Kivity9d58b932011-03-07 16:52:07 +02006042 return false;
Paolo Bonzini4c4a6f72017-07-14 13:36:11 +02006043 masked = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & GUEST_INTR_STATE_NMI;
6044 vmx->loaded_vmcs->nmi_known_unmasked = !masked;
6045 return masked;
Jan Kiszka3cfc3092009-11-12 01:04:25 +01006046}
6047
6048static void vmx_set_nmi_mask(struct kvm_vcpu *vcpu, bool masked)
6049{
6050 struct vcpu_vmx *vmx = to_vmx(vcpu);
6051
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01006052 if (!enable_vnmi) {
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01006053 if (vmx->loaded_vmcs->soft_vnmi_blocked != masked) {
6054 vmx->loaded_vmcs->soft_vnmi_blocked = masked;
6055 vmx->loaded_vmcs->vnmi_blocked_time = 0;
6056 }
6057 } else {
6058 vmx->loaded_vmcs->nmi_known_unmasked = !masked;
6059 if (masked)
6060 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
6061 GUEST_INTR_STATE_NMI);
6062 else
6063 vmcs_clear_bits(GUEST_INTERRUPTIBILITY_INFO,
6064 GUEST_INTR_STATE_NMI);
6065 }
Jan Kiszka3cfc3092009-11-12 01:04:25 +01006066}
6067
Jan Kiszka2505dc92013-04-14 12:12:47 +02006068static int vmx_nmi_allowed(struct kvm_vcpu *vcpu)
6069{
Jan Kiszkab6b8a142014-03-07 20:03:12 +01006070 if (to_vmx(vcpu)->nested.nested_run_pending)
6071 return 0;
Jan Kiszkaea8ceb82013-04-14 21:04:26 +02006072
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01006073 if (!enable_vnmi &&
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01006074 to_vmx(vcpu)->loaded_vmcs->soft_vnmi_blocked)
6075 return 0;
6076
Jan Kiszka2505dc92013-04-14 12:12:47 +02006077 return !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) &
6078 (GUEST_INTR_STATE_MOV_SS | GUEST_INTR_STATE_STI
6079 | GUEST_INTR_STATE_NMI));
6080}
6081
Gleb Natapov78646122009-03-23 12:12:11 +02006082static int vmx_interrupt_allowed(struct kvm_vcpu *vcpu)
6083{
Jan Kiszkab6b8a142014-03-07 20:03:12 +01006084 return (!to_vmx(vcpu)->nested.nested_run_pending &&
6085 vmcs_readl(GUEST_RFLAGS) & X86_EFLAGS_IF) &&
Gleb Natapovc4282df2009-04-21 17:45:07 +03006086 !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) &
6087 (GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS));
Gleb Natapov78646122009-03-23 12:12:11 +02006088}
6089
Izik Eiduscbc94022007-10-25 00:29:55 +02006090static int vmx_set_tss_addr(struct kvm *kvm, unsigned int addr)
6091{
6092 int ret;
Izik Eiduscbc94022007-10-25 00:29:55 +02006093
Sean Christophersonf7eaeb02018-03-05 12:04:36 -08006094 if (enable_unrestricted_guest)
6095 return 0;
6096
Paolo Bonzini1d8007b2015-10-12 13:38:32 +02006097 ret = x86_set_memory_region(kvm, TSS_PRIVATE_MEMSLOT, addr,
6098 PAGE_SIZE * 3);
Izik Eiduscbc94022007-10-25 00:29:55 +02006099 if (ret)
6100 return ret;
Zhang Xiantaobfc6d222007-12-14 10:20:16 +08006101 kvm->arch.tss_addr = addr;
Paolo Bonzini1f755a82014-09-16 13:37:40 +02006102 return init_rmode_tss(kvm);
Izik Eiduscbc94022007-10-25 00:29:55 +02006103}
6104
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02006105static bool rmode_exception(struct kvm_vcpu *vcpu, int vec)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006106{
Jan Kiszka77ab6db2008-07-14 12:28:51 +02006107 switch (vec) {
Jan Kiszka77ab6db2008-07-14 12:28:51 +02006108 case BP_VECTOR:
Jan Kiszkac573cd222010-02-23 17:47:53 +01006109 /*
6110 * Update instruction length as we may reinject the exception
6111 * from user space while in guest debugging mode.
6112 */
6113 to_vmx(vcpu)->vcpu.arch.event_exit_inst_len =
6114 vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
Jan Kiszkad0bfb942008-12-15 13:52:10 +01006115 if (vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02006116 return false;
6117 /* fall through */
6118 case DB_VECTOR:
6119 if (vcpu->guest_debug &
6120 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))
6121 return false;
Jan Kiszkad0bfb942008-12-15 13:52:10 +01006122 /* fall through */
6123 case DE_VECTOR:
Jan Kiszka77ab6db2008-07-14 12:28:51 +02006124 case OF_VECTOR:
6125 case BR_VECTOR:
6126 case UD_VECTOR:
6127 case DF_VECTOR:
6128 case SS_VECTOR:
6129 case GP_VECTOR:
6130 case MF_VECTOR:
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02006131 return true;
6132 break;
Jan Kiszka77ab6db2008-07-14 12:28:51 +02006133 }
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02006134 return false;
6135}
6136
6137static int handle_rmode_exception(struct kvm_vcpu *vcpu,
6138 int vec, u32 err_code)
6139{
6140 /*
6141 * Instruction with address size override prefix opcode 0x67
6142 * Cause the #SS fault with 0 error code in VM86 mode.
6143 */
6144 if (((vec == GP_VECTOR) || (vec == SS_VECTOR)) && err_code == 0) {
6145 if (emulate_instruction(vcpu, 0) == EMULATE_DONE) {
6146 if (vcpu->arch.halt_request) {
6147 vcpu->arch.halt_request = 0;
Joel Schopp5cb56052015-03-02 13:43:31 -06006148 return kvm_vcpu_halt(vcpu);
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02006149 }
6150 return 1;
6151 }
6152 return 0;
6153 }
6154
6155 /*
6156 * Forward all other exceptions that are valid in real mode.
6157 * FIXME: Breaks guest debugging in real mode, needs to be fixed with
6158 * the required debugging infrastructure rework.
6159 */
6160 kvm_queue_exception(vcpu, vec);
6161 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006162}
6163
Andi Kleena0861c02009-06-08 17:37:09 +08006164/*
6165 * Trigger machine check on the host. We assume all the MSRs are already set up
6166 * by the CPU and that we still run on the same CPU as the MCE occurred on.
6167 * We pass a fake environment to the machine check handler because we want
6168 * the guest to be always treated like user space, no matter what context
6169 * it used internally.
6170 */
6171static void kvm_machine_check(void)
6172{
6173#if defined(CONFIG_X86_MCE) && defined(CONFIG_X86_64)
6174 struct pt_regs regs = {
6175 .cs = 3, /* Fake ring 3 no matter what the guest ran on */
6176 .flags = X86_EFLAGS_IF,
6177 };
6178
6179 do_machine_check(&regs, 0);
6180#endif
6181}
6182
Avi Kivity851ba692009-08-24 11:10:17 +03006183static int handle_machine_check(struct kvm_vcpu *vcpu)
Andi Kleena0861c02009-06-08 17:37:09 +08006184{
6185 /* already handled by vcpu_run */
6186 return 1;
6187}
6188
Avi Kivity851ba692009-08-24 11:10:17 +03006189static int handle_exception(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006190{
Avi Kivity1155f762007-11-22 11:30:47 +02006191 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity851ba692009-08-24 11:10:17 +03006192 struct kvm_run *kvm_run = vcpu->run;
Jan Kiszkad0bfb942008-12-15 13:52:10 +01006193 u32 intr_info, ex_no, error_code;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006194 unsigned long cr2, rip, dr6;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006195 u32 vect_info;
6196 enum emulation_result er;
6197
Avi Kivity1155f762007-11-22 11:30:47 +02006198 vect_info = vmx->idt_vectoring_info;
Avi Kivity88786472011-03-07 17:39:45 +02006199 intr_info = vmx->exit_intr_info;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006200
Andi Kleena0861c02009-06-08 17:37:09 +08006201 if (is_machine_check(intr_info))
Avi Kivity851ba692009-08-24 11:10:17 +03006202 return handle_machine_check(vcpu);
Andi Kleena0861c02009-06-08 17:37:09 +08006203
Jim Mattsonef85b672016-12-12 11:01:37 -08006204 if (is_nmi(intr_info))
Avi Kivity1b6269d2007-10-09 12:12:19 +02006205 return 1; /* already handled by vmx_vcpu_run() */
Anthony Liguori2ab455c2007-04-27 09:29:49 +03006206
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05006207 if (is_invalid_opcode(intr_info)) {
Andre Przywara51d8b662010-12-21 11:12:02 +01006208 er = emulate_instruction(vcpu, EMULTYPE_TRAP_UD);
Liran Alon61cb57c2017-11-05 16:56:32 +02006209 if (er == EMULATE_USER_EXIT)
6210 return 0;
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05006211 if (er != EMULATE_DONE)
Avi Kivity7ee5d9402007-11-25 15:22:50 +02006212 kvm_queue_exception(vcpu, UD_VECTOR);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05006213 return 1;
6214 }
6215
Avi Kivity6aa8b732006-12-10 02:21:36 -08006216 error_code = 0;
Ryan Harper2e113842008-02-11 10:26:38 -06006217 if (intr_info & INTR_INFO_DELIVER_CODE_MASK)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006218 error_code = vmcs_read32(VM_EXIT_INTR_ERROR_CODE);
Xiao Guangrongbf4ca232012-10-17 13:48:06 +08006219
Liran Alon9e869482018-03-12 13:12:51 +02006220 if (!vmx->rmode.vm86_active && is_gp_fault(intr_info)) {
6221 WARN_ON_ONCE(!enable_vmware_backdoor);
6222 er = emulate_instruction(vcpu,
6223 EMULTYPE_VMWARE | EMULTYPE_NO_UD_ON_FAIL);
6224 if (er == EMULATE_USER_EXIT)
6225 return 0;
6226 else if (er != EMULATE_DONE)
6227 kvm_queue_exception_e(vcpu, GP_VECTOR, error_code);
6228 return 1;
6229 }
6230
Xiao Guangrongbf4ca232012-10-17 13:48:06 +08006231 /*
6232 * The #PF with PFEC.RSVD = 1 indicates the guest is accessing
6233 * MMIO, it is better to report an internal error.
6234 * See the comments in vmx_handle_exit.
6235 */
6236 if ((vect_info & VECTORING_INFO_VALID_MASK) &&
6237 !(is_page_fault(intr_info) && !(error_code & PFERR_RSVD_MASK))) {
6238 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
6239 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_SIMUL_EX;
Radim Krčmář80f0e952015-04-02 21:11:05 +02006240 vcpu->run->internal.ndata = 3;
Xiao Guangrongbf4ca232012-10-17 13:48:06 +08006241 vcpu->run->internal.data[0] = vect_info;
6242 vcpu->run->internal.data[1] = intr_info;
Radim Krčmář80f0e952015-04-02 21:11:05 +02006243 vcpu->run->internal.data[2] = error_code;
Xiao Guangrongbf4ca232012-10-17 13:48:06 +08006244 return 0;
6245 }
6246
Avi Kivity6aa8b732006-12-10 02:21:36 -08006247 if (is_page_fault(intr_info)) {
6248 cr2 = vmcs_readl(EXIT_QUALIFICATION);
Wanpeng Li1261bfa2017-07-13 18:30:40 -07006249 /* EPT won't cause page fault directly */
6250 WARN_ON_ONCE(!vcpu->arch.apf.host_apf_reason && enable_ept);
Paolo Bonzinid0006532017-08-11 18:36:43 +02006251 return kvm_handle_page_fault(vcpu, error_code, cr2, NULL, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006252 }
6253
Jan Kiszkad0bfb942008-12-15 13:52:10 +01006254 ex_no = intr_info & INTR_INFO_VECTOR_MASK;
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02006255
6256 if (vmx->rmode.vm86_active && rmode_exception(vcpu, ex_no))
6257 return handle_rmode_exception(vcpu, ex_no, error_code);
6258
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006259 switch (ex_no) {
Eric Northup54a20552015-11-03 18:03:53 +01006260 case AC_VECTOR:
6261 kvm_queue_exception_e(vcpu, AC_VECTOR, error_code);
6262 return 1;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006263 case DB_VECTOR:
6264 dr6 = vmcs_readl(EXIT_QUALIFICATION);
6265 if (!(vcpu->guest_debug &
6266 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))) {
Jan Kiszka8246bf52014-01-04 18:47:17 +01006267 vcpu->arch.dr6 &= ~15;
Nadav Amit6f43ed02014-07-15 17:37:46 +03006268 vcpu->arch.dr6 |= dr6 | DR6_RTM;
Huw Daviesfd2a4452014-04-16 10:02:51 +01006269 if (!(dr6 & ~DR6_RESERVED)) /* icebp */
6270 skip_emulated_instruction(vcpu);
6271
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006272 kvm_queue_exception(vcpu, DB_VECTOR);
6273 return 1;
6274 }
6275 kvm_run->debug.arch.dr6 = dr6 | DR6_FIXED_1;
6276 kvm_run->debug.arch.dr7 = vmcs_readl(GUEST_DR7);
6277 /* fall through */
6278 case BP_VECTOR:
Jan Kiszkac573cd222010-02-23 17:47:53 +01006279 /*
6280 * Update instruction length as we may reinject #BP from
6281 * user space while in guest debugging mode. Reading it for
6282 * #DB as well causes no harm, it is not used in that case.
6283 */
6284 vmx->vcpu.arch.event_exit_inst_len =
6285 vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006286 kvm_run->exit_reason = KVM_EXIT_DEBUG;
Avi Kivity0a434bb2011-04-28 15:59:33 +03006287 rip = kvm_rip_read(vcpu);
Jan Kiszkad0bfb942008-12-15 13:52:10 +01006288 kvm_run->debug.arch.pc = vmcs_readl(GUEST_CS_BASE) + rip;
6289 kvm_run->debug.arch.exception = ex_no;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006290 break;
6291 default:
Jan Kiszkad0bfb942008-12-15 13:52:10 +01006292 kvm_run->exit_reason = KVM_EXIT_EXCEPTION;
6293 kvm_run->ex.exception = ex_no;
6294 kvm_run->ex.error_code = error_code;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006295 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006296 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08006297 return 0;
6298}
6299
Avi Kivity851ba692009-08-24 11:10:17 +03006300static int handle_external_interrupt(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006301{
Avi Kivity1165f5f2007-04-19 17:27:43 +03006302 ++vcpu->stat.irq_exits;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006303 return 1;
6304}
6305
Avi Kivity851ba692009-08-24 11:10:17 +03006306static int handle_triple_fault(struct kvm_vcpu *vcpu)
Avi Kivity988ad742007-02-12 00:54:36 -08006307{
Avi Kivity851ba692009-08-24 11:10:17 +03006308 vcpu->run->exit_reason = KVM_EXIT_SHUTDOWN;
Wanpeng Libbeac282017-08-09 22:33:12 -07006309 vcpu->mmio_needed = 0;
Avi Kivity988ad742007-02-12 00:54:36 -08006310 return 0;
6311}
Avi Kivity6aa8b732006-12-10 02:21:36 -08006312
Avi Kivity851ba692009-08-24 11:10:17 +03006313static int handle_io(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006314{
He, Qingbfdaab02007-09-12 14:18:28 +08006315 unsigned long exit_qualification;
Sean Christophersondca7f122018-03-08 08:57:27 -08006316 int size, in, string;
Avi Kivity039576c2007-03-20 12:46:50 +02006317 unsigned port;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006318
He, Qingbfdaab02007-09-12 14:18:28 +08006319 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Avi Kivity039576c2007-03-20 12:46:50 +02006320 string = (exit_qualification & 16) != 0;
Laurent Viviere70669a2007-08-05 10:36:40 +03006321
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02006322 ++vcpu->stat.io_exits;
6323
Sean Christopherson432baf62018-03-08 08:57:26 -08006324 if (string)
Andre Przywara51d8b662010-12-21 11:12:02 +01006325 return emulate_instruction(vcpu, 0) == EMULATE_DONE;
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02006326
6327 port = exit_qualification >> 16;
6328 size = (exit_qualification & 7) + 1;
Sean Christopherson432baf62018-03-08 08:57:26 -08006329 in = (exit_qualification & 8) != 0;
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02006330
Sean Christophersondca7f122018-03-08 08:57:27 -08006331 return kvm_fast_pio(vcpu, size, port, in);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006332}
6333
Ingo Molnar102d8322007-02-19 14:37:47 +02006334static void
6335vmx_patch_hypercall(struct kvm_vcpu *vcpu, unsigned char *hypercall)
6336{
6337 /*
6338 * Patch in the VMCALL instruction:
6339 */
6340 hypercall[0] = 0x0f;
6341 hypercall[1] = 0x01;
6342 hypercall[2] = 0xc1;
Ingo Molnar102d8322007-02-19 14:37:47 +02006343}
6344
Guo Chao0fa06072012-06-28 15:16:19 +08006345/* called to set cr0 as appropriate for a mov-to-cr0 exit. */
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006346static int handle_set_cr0(struct kvm_vcpu *vcpu, unsigned long val)
6347{
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006348 if (is_guest_mode(vcpu)) {
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006349 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
6350 unsigned long orig_val = val;
6351
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006352 /*
6353 * We get here when L2 changed cr0 in a way that did not change
6354 * any of L1's shadowed bits (see nested_vmx_exit_handled_cr),
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006355 * but did change L0 shadowed bits. So we first calculate the
6356 * effective cr0 value that L1 would like to write into the
6357 * hardware. It consists of the L2-owned bits from the new
6358 * value combined with the L1-owned bits from L1's guest_cr0.
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006359 */
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006360 val = (val & ~vmcs12->cr0_guest_host_mask) |
6361 (vmcs12->guest_cr0 & vmcs12->cr0_guest_host_mask);
6362
David Matlack38991522016-11-29 18:14:08 -08006363 if (!nested_guest_cr0_valid(vcpu, val))
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006364 return 1;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006365
6366 if (kvm_set_cr0(vcpu, val))
6367 return 1;
6368 vmcs_writel(CR0_READ_SHADOW, orig_val);
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006369 return 0;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006370 } else {
6371 if (to_vmx(vcpu)->nested.vmxon &&
David Matlack38991522016-11-29 18:14:08 -08006372 !nested_host_cr0_valid(vcpu, val))
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006373 return 1;
David Matlack38991522016-11-29 18:14:08 -08006374
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006375 return kvm_set_cr0(vcpu, val);
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006376 }
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006377}
6378
6379static int handle_set_cr4(struct kvm_vcpu *vcpu, unsigned long val)
6380{
6381 if (is_guest_mode(vcpu)) {
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006382 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
6383 unsigned long orig_val = val;
6384
6385 /* analogously to handle_set_cr0 */
6386 val = (val & ~vmcs12->cr4_guest_host_mask) |
6387 (vmcs12->guest_cr4 & vmcs12->cr4_guest_host_mask);
6388 if (kvm_set_cr4(vcpu, val))
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006389 return 1;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006390 vmcs_writel(CR4_READ_SHADOW, orig_val);
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006391 return 0;
6392 } else
6393 return kvm_set_cr4(vcpu, val);
6394}
6395
Paolo Bonzini0367f202016-07-12 10:44:55 +02006396static int handle_desc(struct kvm_vcpu *vcpu)
6397{
6398 WARN_ON(!(vcpu->arch.cr4 & X86_CR4_UMIP));
6399 return emulate_instruction(vcpu, 0) == EMULATE_DONE;
6400}
6401
Avi Kivity851ba692009-08-24 11:10:17 +03006402static int handle_cr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006403{
Marcelo Tosatti229456f2009-06-17 09:22:14 -03006404 unsigned long exit_qualification, val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006405 int cr;
6406 int reg;
Avi Kivity49a9b072010-06-10 17:02:14 +03006407 int err;
Kyle Huey6affcbe2016-11-29 12:40:40 -08006408 int ret;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006409
He, Qingbfdaab02007-09-12 14:18:28 +08006410 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006411 cr = exit_qualification & 15;
6412 reg = (exit_qualification >> 8) & 15;
6413 switch ((exit_qualification >> 4) & 3) {
6414 case 0: /* mov to cr */
Nadav Amit1e32c072014-06-18 17:19:25 +03006415 val = kvm_register_readl(vcpu, reg);
Marcelo Tosatti229456f2009-06-17 09:22:14 -03006416 trace_kvm_cr_write(cr, val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006417 switch (cr) {
6418 case 0:
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006419 err = handle_set_cr0(vcpu, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08006420 return kvm_complete_insn_gp(vcpu, err);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006421 case 3:
Sean Christophersone1de91c2018-03-05 12:04:41 -08006422 WARN_ON_ONCE(enable_unrestricted_guest);
Avi Kivity23902182010-06-10 17:02:16 +03006423 err = kvm_set_cr3(vcpu, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08006424 return kvm_complete_insn_gp(vcpu, err);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006425 case 4:
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006426 err = handle_set_cr4(vcpu, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08006427 return kvm_complete_insn_gp(vcpu, err);
Gleb Natapov0a5fff192009-04-21 17:45:06 +03006428 case 8: {
6429 u8 cr8_prev = kvm_get_cr8(vcpu);
Nadav Amit1e32c072014-06-18 17:19:25 +03006430 u8 cr8 = (u8)val;
Andre Przywaraeea1cff2010-12-21 11:12:00 +01006431 err = kvm_set_cr8(vcpu, cr8);
Kyle Huey6affcbe2016-11-29 12:40:40 -08006432 ret = kvm_complete_insn_gp(vcpu, err);
Paolo Bonzini35754c92015-07-29 12:05:37 +02006433 if (lapic_in_kernel(vcpu))
Kyle Huey6affcbe2016-11-29 12:40:40 -08006434 return ret;
Gleb Natapov0a5fff192009-04-21 17:45:06 +03006435 if (cr8_prev <= cr8)
Kyle Huey6affcbe2016-11-29 12:40:40 -08006436 return ret;
6437 /*
6438 * TODO: we might be squashing a
6439 * KVM_GUESTDBG_SINGLESTEP-triggered
6440 * KVM_EXIT_DEBUG here.
6441 */
Avi Kivity851ba692009-08-24 11:10:17 +03006442 vcpu->run->exit_reason = KVM_EXIT_SET_TPR;
Gleb Natapov0a5fff192009-04-21 17:45:06 +03006443 return 0;
6444 }
Peter Senna Tschudin4b8073e2012-09-18 18:36:14 +02006445 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08006446 break;
Anthony Liguori25c4c272007-04-27 09:29:21 +03006447 case 2: /* clts */
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -08006448 WARN_ONCE(1, "Guest should always own CR0.TS");
6449 vmx_set_cr0(vcpu, kvm_read_cr0_bits(vcpu, ~X86_CR0_TS));
Avi Kivity4d4ec082009-12-29 18:07:30 +02006450 trace_kvm_cr_write(0, kvm_read_cr0(vcpu));
Kyle Huey6affcbe2016-11-29 12:40:40 -08006451 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006452 case 1: /*mov from cr*/
6453 switch (cr) {
6454 case 3:
Sean Christophersone1de91c2018-03-05 12:04:41 -08006455 WARN_ON_ONCE(enable_unrestricted_guest);
Avi Kivity9f8fe502010-12-05 17:30:00 +02006456 val = kvm_read_cr3(vcpu);
6457 kvm_register_write(vcpu, reg, val);
6458 trace_kvm_cr_read(cr, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08006459 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006460 case 8:
Marcelo Tosatti229456f2009-06-17 09:22:14 -03006461 val = kvm_get_cr8(vcpu);
6462 kvm_register_write(vcpu, reg, val);
6463 trace_kvm_cr_read(cr, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08006464 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006465 }
6466 break;
6467 case 3: /* lmsw */
Avi Kivitya1f83a72009-12-29 17:33:58 +02006468 val = (exit_qualification >> LMSW_SOURCE_DATA_SHIFT) & 0x0f;
Avi Kivity4d4ec082009-12-29 18:07:30 +02006469 trace_kvm_cr_write(0, (kvm_read_cr0(vcpu) & ~0xful) | val);
Avi Kivitya1f83a72009-12-29 17:33:58 +02006470 kvm_lmsw(vcpu, val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006471
Kyle Huey6affcbe2016-11-29 12:40:40 -08006472 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006473 default:
6474 break;
6475 }
Avi Kivity851ba692009-08-24 11:10:17 +03006476 vcpu->run->exit_reason = 0;
Christoffer Dalla737f252012-06-03 21:17:48 +03006477 vcpu_unimpl(vcpu, "unhandled control register: op %d cr %d\n",
Avi Kivity6aa8b732006-12-10 02:21:36 -08006478 (int)(exit_qualification >> 4) & 3, cr);
6479 return 0;
6480}
6481
Avi Kivity851ba692009-08-24 11:10:17 +03006482static int handle_dr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006483{
He, Qingbfdaab02007-09-12 14:18:28 +08006484 unsigned long exit_qualification;
Nadav Amit16f8a6f2014-10-03 01:10:05 +03006485 int dr, dr7, reg;
6486
6487 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
6488 dr = exit_qualification & DEBUG_REG_ACCESS_NUM;
6489
6490 /* First, if DR does not exist, trigger UD */
6491 if (!kvm_require_dr(vcpu, dr))
6492 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006493
Jan Kiszkaf2483412010-01-20 18:20:20 +01006494 /* Do not handle if the CPL > 0, will trigger GP on re-entry */
Avi Kivity0a79b002009-09-01 12:03:25 +03006495 if (!kvm_require_cpl(vcpu, 0))
6496 return 1;
Nadav Amit16f8a6f2014-10-03 01:10:05 +03006497 dr7 = vmcs_readl(GUEST_DR7);
6498 if (dr7 & DR7_GD) {
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006499 /*
6500 * As the vm-exit takes precedence over the debug trap, we
6501 * need to emulate the latter, either for the host or the
6502 * guest debugging itself.
6503 */
6504 if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP) {
Avi Kivity851ba692009-08-24 11:10:17 +03006505 vcpu->run->debug.arch.dr6 = vcpu->arch.dr6;
Nadav Amit16f8a6f2014-10-03 01:10:05 +03006506 vcpu->run->debug.arch.dr7 = dr7;
Nadav Amit82b32772014-11-02 11:54:45 +02006507 vcpu->run->debug.arch.pc = kvm_get_linear_rip(vcpu);
Avi Kivity851ba692009-08-24 11:10:17 +03006508 vcpu->run->debug.arch.exception = DB_VECTOR;
6509 vcpu->run->exit_reason = KVM_EXIT_DEBUG;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006510 return 0;
6511 } else {
Nadav Amit7305eb52014-11-02 11:54:44 +02006512 vcpu->arch.dr6 &= ~15;
Nadav Amit6f43ed02014-07-15 17:37:46 +03006513 vcpu->arch.dr6 |= DR6_BD | DR6_RTM;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006514 kvm_queue_exception(vcpu, DB_VECTOR);
6515 return 1;
6516 }
6517 }
6518
Paolo Bonzini81908bf2014-02-21 10:32:27 +01006519 if (vcpu->guest_debug == 0) {
Paolo Bonzini8f223722016-02-26 12:09:49 +01006520 vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL,
6521 CPU_BASED_MOV_DR_EXITING);
Paolo Bonzini81908bf2014-02-21 10:32:27 +01006522
6523 /*
6524 * No more DR vmexits; force a reload of the debug registers
6525 * and reenter on this instruction. The next vmexit will
6526 * retrieve the full state of the debug registers.
6527 */
6528 vcpu->arch.switch_db_regs |= KVM_DEBUGREG_WONT_EXIT;
6529 return 1;
6530 }
6531
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006532 reg = DEBUG_REG_ACCESS_REG(exit_qualification);
6533 if (exit_qualification & TYPE_MOV_FROM_DR) {
Gleb Natapov020df072010-04-13 10:05:23 +03006534 unsigned long val;
Jan Kiszka4c4d5632013-12-18 19:16:24 +01006535
6536 if (kvm_get_dr(vcpu, dr, &val))
6537 return 1;
6538 kvm_register_write(vcpu, reg, val);
Gleb Natapov020df072010-04-13 10:05:23 +03006539 } else
Nadav Amit57773922014-06-18 17:19:23 +03006540 if (kvm_set_dr(vcpu, dr, kvm_register_readl(vcpu, reg)))
Jan Kiszka4c4d5632013-12-18 19:16:24 +01006541 return 1;
6542
Kyle Huey6affcbe2016-11-29 12:40:40 -08006543 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006544}
6545
Jan Kiszka73aaf249e2014-01-04 18:47:16 +01006546static u64 vmx_get_dr6(struct kvm_vcpu *vcpu)
6547{
6548 return vcpu->arch.dr6;
6549}
6550
6551static void vmx_set_dr6(struct kvm_vcpu *vcpu, unsigned long val)
6552{
6553}
6554
Paolo Bonzini81908bf2014-02-21 10:32:27 +01006555static void vmx_sync_dirty_debug_regs(struct kvm_vcpu *vcpu)
6556{
Paolo Bonzini81908bf2014-02-21 10:32:27 +01006557 get_debugreg(vcpu->arch.db[0], 0);
6558 get_debugreg(vcpu->arch.db[1], 1);
6559 get_debugreg(vcpu->arch.db[2], 2);
6560 get_debugreg(vcpu->arch.db[3], 3);
6561 get_debugreg(vcpu->arch.dr6, 6);
6562 vcpu->arch.dr7 = vmcs_readl(GUEST_DR7);
6563
6564 vcpu->arch.switch_db_regs &= ~KVM_DEBUGREG_WONT_EXIT;
Paolo Bonzini8f223722016-02-26 12:09:49 +01006565 vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL, CPU_BASED_MOV_DR_EXITING);
Paolo Bonzini81908bf2014-02-21 10:32:27 +01006566}
6567
Gleb Natapov020df072010-04-13 10:05:23 +03006568static void vmx_set_dr7(struct kvm_vcpu *vcpu, unsigned long val)
6569{
6570 vmcs_writel(GUEST_DR7, val);
6571}
6572
Avi Kivity851ba692009-08-24 11:10:17 +03006573static int handle_cpuid(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006574{
Kyle Huey6a908b62016-11-29 12:40:37 -08006575 return kvm_emulate_cpuid(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006576}
6577
Avi Kivity851ba692009-08-24 11:10:17 +03006578static int handle_rdmsr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006579{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08006580 u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX];
Paolo Bonzini609e36d2015-04-08 15:30:38 +02006581 struct msr_data msr_info;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006582
Paolo Bonzini609e36d2015-04-08 15:30:38 +02006583 msr_info.index = ecx;
6584 msr_info.host_initiated = false;
6585 if (vmx_get_msr(vcpu, &msr_info)) {
Avi Kivity59200272010-01-25 19:47:02 +02006586 trace_kvm_msr_read_ex(ecx);
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02006587 kvm_inject_gp(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006588 return 1;
6589 }
6590
Paolo Bonzini609e36d2015-04-08 15:30:38 +02006591 trace_kvm_msr_read(ecx, msr_info.data);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04006592
Avi Kivity6aa8b732006-12-10 02:21:36 -08006593 /* FIXME: handling of bits 32:63 of rax, rdx */
Paolo Bonzini609e36d2015-04-08 15:30:38 +02006594 vcpu->arch.regs[VCPU_REGS_RAX] = msr_info.data & -1u;
6595 vcpu->arch.regs[VCPU_REGS_RDX] = (msr_info.data >> 32) & -1u;
Kyle Huey6affcbe2016-11-29 12:40:40 -08006596 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006597}
6598
Avi Kivity851ba692009-08-24 11:10:17 +03006599static int handle_wrmsr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006600{
Will Auld8fe8ab42012-11-29 12:42:12 -08006601 struct msr_data msr;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08006602 u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX];
6603 u64 data = (vcpu->arch.regs[VCPU_REGS_RAX] & -1u)
6604 | ((u64)(vcpu->arch.regs[VCPU_REGS_RDX] & -1u) << 32);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006605
Will Auld8fe8ab42012-11-29 12:42:12 -08006606 msr.data = data;
6607 msr.index = ecx;
6608 msr.host_initiated = false;
Nadav Amit854e8bb2014-09-16 03:24:05 +03006609 if (kvm_set_msr(vcpu, &msr) != 0) {
Avi Kivity59200272010-01-25 19:47:02 +02006610 trace_kvm_msr_write_ex(ecx, data);
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02006611 kvm_inject_gp(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006612 return 1;
6613 }
6614
Avi Kivity59200272010-01-25 19:47:02 +02006615 trace_kvm_msr_write(ecx, data);
Kyle Huey6affcbe2016-11-29 12:40:40 -08006616 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006617}
6618
Avi Kivity851ba692009-08-24 11:10:17 +03006619static int handle_tpr_below_threshold(struct kvm_vcpu *vcpu)
Yang, Sheng6e5d8652007-09-12 18:03:11 +08006620{
Paolo Bonzinieb90f342016-12-18 14:02:21 +01006621 kvm_apic_update_ppr(vcpu);
Yang, Sheng6e5d8652007-09-12 18:03:11 +08006622 return 1;
6623}
6624
Avi Kivity851ba692009-08-24 11:10:17 +03006625static int handle_interrupt_window(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006626{
Paolo Bonzini47c01522016-12-19 11:44:07 +01006627 vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL,
6628 CPU_BASED_VIRTUAL_INTR_PENDING);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04006629
Avi Kivity3842d132010-07-27 12:30:24 +03006630 kvm_make_request(KVM_REQ_EVENT, vcpu);
6631
Jan Kiszkaa26bf122008-09-26 09:30:45 +02006632 ++vcpu->stat.irq_window_exits;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006633 return 1;
6634}
6635
Avi Kivity851ba692009-08-24 11:10:17 +03006636static int handle_halt(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006637{
Avi Kivityd3bef152007-06-05 15:53:05 +03006638 return kvm_emulate_halt(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006639}
6640
Avi Kivity851ba692009-08-24 11:10:17 +03006641static int handle_vmcall(struct kvm_vcpu *vcpu)
Ingo Molnarc21415e2007-02-19 14:37:47 +02006642{
Andrey Smetanin0d9c0552016-02-11 16:44:59 +03006643 return kvm_emulate_hypercall(vcpu);
Ingo Molnarc21415e2007-02-19 14:37:47 +02006644}
6645
Gleb Natapovec25d5e2010-11-01 15:35:01 +02006646static int handle_invd(struct kvm_vcpu *vcpu)
6647{
Andre Przywara51d8b662010-12-21 11:12:02 +01006648 return emulate_instruction(vcpu, 0) == EMULATE_DONE;
Gleb Natapovec25d5e2010-11-01 15:35:01 +02006649}
6650
Avi Kivity851ba692009-08-24 11:10:17 +03006651static int handle_invlpg(struct kvm_vcpu *vcpu)
Marcelo Tosattia7052892008-09-23 13:18:35 -03006652{
Sheng Yangf9c617f2009-03-25 10:08:52 +08006653 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Marcelo Tosattia7052892008-09-23 13:18:35 -03006654
6655 kvm_mmu_invlpg(vcpu, exit_qualification);
Kyle Huey6affcbe2016-11-29 12:40:40 -08006656 return kvm_skip_emulated_instruction(vcpu);
Marcelo Tosattia7052892008-09-23 13:18:35 -03006657}
6658
Avi Kivityfee84b02011-11-10 14:57:25 +02006659static int handle_rdpmc(struct kvm_vcpu *vcpu)
6660{
6661 int err;
6662
6663 err = kvm_rdpmc(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08006664 return kvm_complete_insn_gp(vcpu, err);
Avi Kivityfee84b02011-11-10 14:57:25 +02006665}
6666
Avi Kivity851ba692009-08-24 11:10:17 +03006667static int handle_wbinvd(struct kvm_vcpu *vcpu)
Eddie Donge5edaa02007-11-11 12:28:35 +02006668{
Kyle Huey6affcbe2016-11-29 12:40:40 -08006669 return kvm_emulate_wbinvd(vcpu);
Eddie Donge5edaa02007-11-11 12:28:35 +02006670}
6671
Dexuan Cui2acf9232010-06-10 11:27:12 +08006672static int handle_xsetbv(struct kvm_vcpu *vcpu)
6673{
6674 u64 new_bv = kvm_read_edx_eax(vcpu);
6675 u32 index = kvm_register_read(vcpu, VCPU_REGS_RCX);
6676
6677 if (kvm_set_xcr(vcpu, index, new_bv) == 0)
Kyle Huey6affcbe2016-11-29 12:40:40 -08006678 return kvm_skip_emulated_instruction(vcpu);
Dexuan Cui2acf9232010-06-10 11:27:12 +08006679 return 1;
6680}
6681
Wanpeng Lif53cd632014-12-02 19:14:58 +08006682static int handle_xsaves(struct kvm_vcpu *vcpu)
6683{
Kyle Huey6affcbe2016-11-29 12:40:40 -08006684 kvm_skip_emulated_instruction(vcpu);
Wanpeng Lif53cd632014-12-02 19:14:58 +08006685 WARN(1, "this should never happen\n");
6686 return 1;
6687}
6688
6689static int handle_xrstors(struct kvm_vcpu *vcpu)
6690{
Kyle Huey6affcbe2016-11-29 12:40:40 -08006691 kvm_skip_emulated_instruction(vcpu);
Wanpeng Lif53cd632014-12-02 19:14:58 +08006692 WARN(1, "this should never happen\n");
6693 return 1;
6694}
6695
Avi Kivity851ba692009-08-24 11:10:17 +03006696static int handle_apic_access(struct kvm_vcpu *vcpu)
Sheng Yangf78e0e22007-10-29 09:40:42 +08006697{
Kevin Tian58fbbf22011-08-30 13:56:17 +03006698 if (likely(fasteoi)) {
6699 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
6700 int access_type, offset;
6701
6702 access_type = exit_qualification & APIC_ACCESS_TYPE;
6703 offset = exit_qualification & APIC_ACCESS_OFFSET;
6704 /*
6705 * Sane guest uses MOV to write EOI, with written value
6706 * not cared. So make a short-circuit here by avoiding
6707 * heavy instruction emulation.
6708 */
6709 if ((access_type == TYPE_LINEAR_APIC_INST_WRITE) &&
6710 (offset == APIC_EOI)) {
6711 kvm_lapic_set_eoi(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08006712 return kvm_skip_emulated_instruction(vcpu);
Kevin Tian58fbbf22011-08-30 13:56:17 +03006713 }
6714 }
Andre Przywara51d8b662010-12-21 11:12:02 +01006715 return emulate_instruction(vcpu, 0) == EMULATE_DONE;
Sheng Yangf78e0e22007-10-29 09:40:42 +08006716}
6717
Yang Zhangc7c9c562013-01-25 10:18:51 +08006718static int handle_apic_eoi_induced(struct kvm_vcpu *vcpu)
6719{
6720 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
6721 int vector = exit_qualification & 0xff;
6722
6723 /* EOI-induced VM exit is trap-like and thus no need to adjust IP */
6724 kvm_apic_set_eoi_accelerated(vcpu, vector);
6725 return 1;
6726}
6727
Yang Zhang83d4c282013-01-25 10:18:49 +08006728static int handle_apic_write(struct kvm_vcpu *vcpu)
6729{
6730 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
6731 u32 offset = exit_qualification & 0xfff;
6732
6733 /* APIC-write VM exit is trap-like and thus no need to adjust IP */
6734 kvm_apic_write_nodecode(vcpu, offset);
6735 return 1;
6736}
6737
Avi Kivity851ba692009-08-24 11:10:17 +03006738static int handle_task_switch(struct kvm_vcpu *vcpu)
Izik Eidus37817f22008-03-24 23:14:53 +02006739{
Jan Kiszka60637aa2008-09-26 09:30:47 +02006740 struct vcpu_vmx *vmx = to_vmx(vcpu);
Izik Eidus37817f22008-03-24 23:14:53 +02006741 unsigned long exit_qualification;
Jan Kiszkae269fb22010-04-14 15:51:09 +02006742 bool has_error_code = false;
6743 u32 error_code = 0;
Izik Eidus37817f22008-03-24 23:14:53 +02006744 u16 tss_selector;
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01006745 int reason, type, idt_v, idt_index;
Gleb Natapov64a7ec02009-03-30 16:03:29 +03006746
6747 idt_v = (vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK);
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01006748 idt_index = (vmx->idt_vectoring_info & VECTORING_INFO_VECTOR_MASK);
Gleb Natapov64a7ec02009-03-30 16:03:29 +03006749 type = (vmx->idt_vectoring_info & VECTORING_INFO_TYPE_MASK);
Izik Eidus37817f22008-03-24 23:14:53 +02006750
6751 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
6752
6753 reason = (u32)exit_qualification >> 30;
Gleb Natapov64a7ec02009-03-30 16:03:29 +03006754 if (reason == TASK_SWITCH_GATE && idt_v) {
6755 switch (type) {
6756 case INTR_TYPE_NMI_INTR:
6757 vcpu->arch.nmi_injected = false;
Avi Kivity654f06f2011-03-23 15:02:47 +02006758 vmx_set_nmi_mask(vcpu, true);
Gleb Natapov64a7ec02009-03-30 16:03:29 +03006759 break;
6760 case INTR_TYPE_EXT_INTR:
Gleb Natapov66fd3f72009-05-11 13:35:50 +03006761 case INTR_TYPE_SOFT_INTR:
Gleb Natapov64a7ec02009-03-30 16:03:29 +03006762 kvm_clear_interrupt_queue(vcpu);
6763 break;
6764 case INTR_TYPE_HARD_EXCEPTION:
Jan Kiszkae269fb22010-04-14 15:51:09 +02006765 if (vmx->idt_vectoring_info &
6766 VECTORING_INFO_DELIVER_CODE_MASK) {
6767 has_error_code = true;
6768 error_code =
6769 vmcs_read32(IDT_VECTORING_ERROR_CODE);
6770 }
6771 /* fall through */
Gleb Natapov64a7ec02009-03-30 16:03:29 +03006772 case INTR_TYPE_SOFT_EXCEPTION:
6773 kvm_clear_exception_queue(vcpu);
6774 break;
6775 default:
6776 break;
6777 }
Jan Kiszka60637aa2008-09-26 09:30:47 +02006778 }
Izik Eidus37817f22008-03-24 23:14:53 +02006779 tss_selector = exit_qualification;
6780
Gleb Natapov64a7ec02009-03-30 16:03:29 +03006781 if (!idt_v || (type != INTR_TYPE_HARD_EXCEPTION &&
6782 type != INTR_TYPE_EXT_INTR &&
6783 type != INTR_TYPE_NMI_INTR))
6784 skip_emulated_instruction(vcpu);
6785
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01006786 if (kvm_task_switch(vcpu, tss_selector,
6787 type == INTR_TYPE_SOFT_INTR ? idt_index : -1, reason,
6788 has_error_code, error_code) == EMULATE_FAIL) {
Gleb Natapovacb54512010-04-15 21:03:50 +03006789 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
6790 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
6791 vcpu->run->internal.ndata = 0;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006792 return 0;
Gleb Natapovacb54512010-04-15 21:03:50 +03006793 }
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006794
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006795 /*
6796 * TODO: What about debug traps on tss switch?
6797 * Are we supposed to inject them and update dr6?
6798 */
6799
6800 return 1;
Izik Eidus37817f22008-03-24 23:14:53 +02006801}
6802
Avi Kivity851ba692009-08-24 11:10:17 +03006803static int handle_ept_violation(struct kvm_vcpu *vcpu)
Sheng Yang14394422008-04-28 12:24:45 +08006804{
Sheng Yangf9c617f2009-03-25 10:08:52 +08006805 unsigned long exit_qualification;
Sheng Yang14394422008-04-28 12:24:45 +08006806 gpa_t gpa;
Paolo Bonzinieebed242016-11-28 14:39:58 +01006807 u64 error_code;
Sheng Yang14394422008-04-28 12:24:45 +08006808
Sheng Yangf9c617f2009-03-25 10:08:52 +08006809 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Sheng Yang14394422008-04-28 12:24:45 +08006810
Gleb Natapov0be9c7a2013-09-15 11:07:23 +03006811 /*
6812 * EPT violation happened while executing iret from NMI,
6813 * "blocked by NMI" bit has to be set before next VM entry.
6814 * There are errata that may cause this bit to not be set:
6815 * AAK134, BY25.
6816 */
Gleb Natapovbcd1c292013-09-25 10:58:22 +03006817 if (!(to_vmx(vcpu)->idt_vectoring_info & VECTORING_INFO_VALID_MASK) &&
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01006818 enable_vnmi &&
Gleb Natapovbcd1c292013-09-25 10:58:22 +03006819 (exit_qualification & INTR_INFO_UNBLOCK_NMI))
Gleb Natapov0be9c7a2013-09-15 11:07:23 +03006820 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO, GUEST_INTR_STATE_NMI);
6821
Sheng Yang14394422008-04-28 12:24:45 +08006822 gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS);
Marcelo Tosatti229456f2009-06-17 09:22:14 -03006823 trace_kvm_page_fault(gpa, exit_qualification);
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08006824
Junaid Shahid27959a42016-12-06 16:46:10 -08006825 /* Is it a read fault? */
Junaid Shahidab22a472016-12-21 20:29:28 -08006826 error_code = (exit_qualification & EPT_VIOLATION_ACC_READ)
Junaid Shahid27959a42016-12-06 16:46:10 -08006827 ? PFERR_USER_MASK : 0;
6828 /* Is it a write fault? */
Junaid Shahidab22a472016-12-21 20:29:28 -08006829 error_code |= (exit_qualification & EPT_VIOLATION_ACC_WRITE)
Junaid Shahid27959a42016-12-06 16:46:10 -08006830 ? PFERR_WRITE_MASK : 0;
6831 /* Is it a fetch fault? */
Junaid Shahidab22a472016-12-21 20:29:28 -08006832 error_code |= (exit_qualification & EPT_VIOLATION_ACC_INSTR)
Junaid Shahid27959a42016-12-06 16:46:10 -08006833 ? PFERR_FETCH_MASK : 0;
6834 /* ept page table entry is present? */
6835 error_code |= (exit_qualification &
6836 (EPT_VIOLATION_READABLE | EPT_VIOLATION_WRITABLE |
6837 EPT_VIOLATION_EXECUTABLE))
6838 ? PFERR_PRESENT_MASK : 0;
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08006839
Paolo Bonzinieebed242016-11-28 14:39:58 +01006840 error_code |= (exit_qualification & 0x100) != 0 ?
6841 PFERR_GUEST_FINAL_MASK : PFERR_GUEST_PAGE_MASK;
Yang Zhang25d92082013-08-06 12:00:32 +03006842
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08006843 vcpu->arch.exit_qualification = exit_qualification;
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08006844 return kvm_mmu_page_fault(vcpu, gpa, error_code, NULL, 0);
Sheng Yang14394422008-04-28 12:24:45 +08006845}
6846
Avi Kivity851ba692009-08-24 11:10:17 +03006847static int handle_ept_misconfig(struct kvm_vcpu *vcpu)
Marcelo Tosatti68f89402009-06-11 12:07:43 -03006848{
Xiao Guangrongf735d4a2015-08-05 12:04:27 +08006849 int ret;
Marcelo Tosatti68f89402009-06-11 12:07:43 -03006850 gpa_t gpa;
6851
Paolo Bonzini9034e6e2017-08-17 18:36:58 +02006852 /*
6853 * A nested guest cannot optimize MMIO vmexits, because we have an
6854 * nGPA here instead of the required GPA.
6855 */
Marcelo Tosatti68f89402009-06-11 12:07:43 -03006856 gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS);
Paolo Bonzini9034e6e2017-08-17 18:36:58 +02006857 if (!is_guest_mode(vcpu) &&
6858 !kvm_io_bus_write(vcpu, KVM_FAST_MMIO_BUS, gpa, 0, NULL)) {
Jason Wang931c33b2015-09-15 14:41:58 +08006859 trace_kvm_fast_mmio(gpa);
Vitaly Kuznetsovd391f122018-01-25 16:37:07 +01006860 /*
6861 * Doing kvm_skip_emulated_instruction() depends on undefined
6862 * behavior: Intel's manual doesn't mandate
6863 * VM_EXIT_INSTRUCTION_LEN to be set in VMCS when EPT MISCONFIG
6864 * occurs and while on real hardware it was observed to be set,
6865 * other hypervisors (namely Hyper-V) don't set it, we end up
6866 * advancing IP with some random value. Disable fast mmio when
6867 * running nested and keep it for real hardware in hope that
6868 * VM_EXIT_INSTRUCTION_LEN will always be set correctly.
6869 */
6870 if (!static_cpu_has(X86_FEATURE_HYPERVISOR))
6871 return kvm_skip_emulated_instruction(vcpu);
6872 else
6873 return x86_emulate_instruction(vcpu, gpa, EMULTYPE_SKIP,
6874 NULL, 0) == EMULATE_DONE;
Michael S. Tsirkin68c3b4d2014-03-31 21:50:44 +03006875 }
Marcelo Tosatti68f89402009-06-11 12:07:43 -03006876
Paolo Bonzinie08d26f2017-08-17 18:36:56 +02006877 ret = kvm_mmu_page_fault(vcpu, gpa, PFERR_RSVD_MASK, NULL, 0);
6878 if (ret >= 0)
6879 return ret;
Xiao Guangrongce88dec2011-07-12 03:33:44 +08006880
6881 /* It is the real ept misconfig */
Xiao Guangrongf735d4a2015-08-05 12:04:27 +08006882 WARN_ON(1);
Marcelo Tosatti68f89402009-06-11 12:07:43 -03006883
Avi Kivity851ba692009-08-24 11:10:17 +03006884 vcpu->run->exit_reason = KVM_EXIT_UNKNOWN;
6885 vcpu->run->hw.hardware_exit_reason = EXIT_REASON_EPT_MISCONFIG;
Marcelo Tosatti68f89402009-06-11 12:07:43 -03006886
6887 return 0;
6888}
6889
Avi Kivity851ba692009-08-24 11:10:17 +03006890static int handle_nmi_window(struct kvm_vcpu *vcpu)
Sheng Yangf08864b2008-05-15 18:23:25 +08006891{
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01006892 WARN_ON_ONCE(!enable_vnmi);
Paolo Bonzini47c01522016-12-19 11:44:07 +01006893 vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL,
6894 CPU_BASED_VIRTUAL_NMI_PENDING);
Sheng Yangf08864b2008-05-15 18:23:25 +08006895 ++vcpu->stat.nmi_window_exits;
Avi Kivity3842d132010-07-27 12:30:24 +03006896 kvm_make_request(KVM_REQ_EVENT, vcpu);
Sheng Yangf08864b2008-05-15 18:23:25 +08006897
6898 return 1;
6899}
6900
Mohammed Gamal80ced182009-09-01 12:48:18 +02006901static int handle_invalid_guest_state(struct kvm_vcpu *vcpu)
Mohammed Gamalea953ef2008-08-17 16:47:05 +03006902{
Avi Kivity8b3079a2009-01-05 12:10:54 +02006903 struct vcpu_vmx *vmx = to_vmx(vcpu);
6904 enum emulation_result err = EMULATE_DONE;
Mohammed Gamal80ced182009-09-01 12:48:18 +02006905 int ret = 1;
Avi Kivity49e9d552010-09-19 14:34:08 +02006906 u32 cpu_exec_ctrl;
6907 bool intr_window_requested;
Avi Kivityb8405c12012-06-07 17:08:48 +03006908 unsigned count = 130;
Avi Kivity49e9d552010-09-19 14:34:08 +02006909
Sean Christopherson2bb8caf2018-03-12 10:56:13 -07006910 /*
6911 * We should never reach the point where we are emulating L2
6912 * due to invalid guest state as that means we incorrectly
6913 * allowed a nested VMEntry with an invalid vmcs12.
6914 */
6915 WARN_ON_ONCE(vmx->emulation_required && vmx->nested.nested_run_pending);
6916
Avi Kivity49e9d552010-09-19 14:34:08 +02006917 cpu_exec_ctrl = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
6918 intr_window_requested = cpu_exec_ctrl & CPU_BASED_VIRTUAL_INTR_PENDING;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03006919
Paolo Bonzini98eb2f82014-03-27 09:51:52 +01006920 while (vmx->emulation_required && count-- != 0) {
Avi Kivitybdea48e2012-06-10 18:07:57 +03006921 if (intr_window_requested && vmx_interrupt_allowed(vcpu))
Avi Kivity49e9d552010-09-19 14:34:08 +02006922 return handle_interrupt_window(&vmx->vcpu);
6923
Radim Krčmář72875d82017-04-26 22:32:19 +02006924 if (kvm_test_request(KVM_REQ_EVENT, vcpu))
Avi Kivityde87dcdd2012-06-12 20:21:38 +03006925 return 1;
6926
Liran Alon9b8ae632017-11-05 16:56:34 +02006927 err = emulate_instruction(vcpu, 0);
Mohammed Gamalea953ef2008-08-17 16:47:05 +03006928
Paolo Bonziniac0a48c2013-06-25 18:24:41 +02006929 if (err == EMULATE_USER_EXIT) {
Paolo Bonzini94452b92013-08-27 15:41:42 +02006930 ++vcpu->stat.mmio_exits;
Mohammed Gamal80ced182009-09-01 12:48:18 +02006931 ret = 0;
6932 goto out;
6933 }
Guillaume Thouvenin1d5a4d92008-10-29 09:39:42 +01006934
Avi Kivityde5f70e2012-06-12 20:22:28 +03006935 if (err != EMULATE_DONE) {
6936 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
6937 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
6938 vcpu->run->internal.ndata = 0;
Gleb Natapov6d77dbf2010-05-10 11:16:56 +03006939 return 0;
Avi Kivityde5f70e2012-06-12 20:22:28 +03006940 }
Mohammed Gamalea953ef2008-08-17 16:47:05 +03006941
Gleb Natapov8d76c492013-05-08 18:38:44 +03006942 if (vcpu->arch.halt_request) {
6943 vcpu->arch.halt_request = 0;
Joel Schopp5cb56052015-03-02 13:43:31 -06006944 ret = kvm_vcpu_halt(vcpu);
Gleb Natapov8d76c492013-05-08 18:38:44 +03006945 goto out;
6946 }
6947
Mohammed Gamalea953ef2008-08-17 16:47:05 +03006948 if (signal_pending(current))
Mohammed Gamal80ced182009-09-01 12:48:18 +02006949 goto out;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03006950 if (need_resched())
6951 schedule();
6952 }
6953
Mohammed Gamal80ced182009-09-01 12:48:18 +02006954out:
6955 return ret;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03006956}
6957
Radim Krčmářb4a2d312014-08-21 18:08:08 +02006958static int __grow_ple_window(int val)
6959{
6960 if (ple_window_grow < 1)
6961 return ple_window;
6962
6963 val = min(val, ple_window_actual_max);
6964
6965 if (ple_window_grow < ple_window)
6966 val *= ple_window_grow;
6967 else
6968 val += ple_window_grow;
6969
6970 return val;
6971}
6972
6973static int __shrink_ple_window(int val, int modifier, int minimum)
6974{
6975 if (modifier < 1)
6976 return ple_window;
6977
6978 if (modifier < ple_window)
6979 val /= modifier;
6980 else
6981 val -= modifier;
6982
6983 return max(val, minimum);
6984}
6985
6986static void grow_ple_window(struct kvm_vcpu *vcpu)
6987{
6988 struct vcpu_vmx *vmx = to_vmx(vcpu);
6989 int old = vmx->ple_window;
6990
6991 vmx->ple_window = __grow_ple_window(old);
6992
6993 if (vmx->ple_window != old)
6994 vmx->ple_window_dirty = true;
Radim Krčmář7b462682014-08-21 18:08:09 +02006995
6996 trace_kvm_ple_window_grow(vcpu->vcpu_id, vmx->ple_window, old);
Radim Krčmářb4a2d312014-08-21 18:08:08 +02006997}
6998
6999static void shrink_ple_window(struct kvm_vcpu *vcpu)
7000{
7001 struct vcpu_vmx *vmx = to_vmx(vcpu);
7002 int old = vmx->ple_window;
7003
7004 vmx->ple_window = __shrink_ple_window(old,
7005 ple_window_shrink, ple_window);
7006
7007 if (vmx->ple_window != old)
7008 vmx->ple_window_dirty = true;
Radim Krčmář7b462682014-08-21 18:08:09 +02007009
7010 trace_kvm_ple_window_shrink(vcpu->vcpu_id, vmx->ple_window, old);
Radim Krčmářb4a2d312014-08-21 18:08:08 +02007011}
7012
7013/*
7014 * ple_window_actual_max is computed to be one grow_ple_window() below
7015 * ple_window_max. (See __grow_ple_window for the reason.)
7016 * This prevents overflows, because ple_window_max is int.
7017 * ple_window_max effectively rounded down to a multiple of ple_window_grow in
7018 * this process.
7019 * ple_window_max is also prevented from setting vmx->ple_window < ple_window.
7020 */
7021static void update_ple_window_actual_max(void)
7022{
7023 ple_window_actual_max =
7024 __shrink_ple_window(max(ple_window_max, ple_window),
7025 ple_window_grow, INT_MIN);
7026}
7027
Feng Wubf9f6ac2015-09-18 22:29:55 +08007028/*
7029 * Handler for POSTED_INTERRUPT_WAKEUP_VECTOR.
7030 */
7031static void wakeup_handler(void)
7032{
7033 struct kvm_vcpu *vcpu;
7034 int cpu = smp_processor_id();
7035
7036 spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, cpu));
7037 list_for_each_entry(vcpu, &per_cpu(blocked_vcpu_on_cpu, cpu),
7038 blocked_vcpu_list) {
7039 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
7040
7041 if (pi_test_on(pi_desc) == 1)
7042 kvm_vcpu_kick(vcpu);
7043 }
7044 spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, cpu));
7045}
7046
Junaid Shahidf160c7b2016-12-06 16:46:16 -08007047void vmx_enable_tdp(void)
7048{
7049 kvm_mmu_set_mask_ptes(VMX_EPT_READABLE_MASK,
7050 enable_ept_ad_bits ? VMX_EPT_ACCESS_BIT : 0ull,
7051 enable_ept_ad_bits ? VMX_EPT_DIRTY_BIT : 0ull,
7052 0ull, VMX_EPT_EXECUTABLE_MASK,
7053 cpu_has_vmx_ept_execute_only() ? 0ull : VMX_EPT_READABLE_MASK,
Tom Lendackyd0ec49d2017-07-17 16:10:27 -05007054 VMX_EPT_RWX_MASK, 0ull);
Junaid Shahidf160c7b2016-12-06 16:46:16 -08007055
7056 ept_set_mmio_spte_mask();
7057 kvm_enable_tdp();
7058}
7059
Tiejun Chenf2c76482014-10-28 10:14:47 +08007060static __init int hardware_setup(void)
7061{
Paolo Bonzini904e14f2018-01-16 16:51:18 +01007062 int r = -ENOMEM, i;
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007063
7064 rdmsrl_safe(MSR_EFER, &host_efer);
7065
7066 for (i = 0; i < ARRAY_SIZE(vmx_msr_index); ++i)
7067 kvm_define_shared_msr(i, vmx_msr_index[i]);
7068
Radim Krčmář23611332016-09-29 22:41:33 +02007069 for (i = 0; i < VMX_BITMAP_NR; i++) {
7070 vmx_bitmap[i] = (unsigned long *)__get_free_page(GFP_KERNEL);
7071 if (!vmx_bitmap[i])
7072 goto out;
7073 }
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007074
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007075 memset(vmx_vmread_bitmap, 0xff, PAGE_SIZE);
7076 memset(vmx_vmwrite_bitmap, 0xff, PAGE_SIZE);
7077
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007078 if (setup_vmcs_config(&vmcs_config) < 0) {
7079 r = -EIO;
Radim Krčmář23611332016-09-29 22:41:33 +02007080 goto out;
Tiejun Chenbaa03522014-12-23 16:21:11 +08007081 }
Tiejun Chenf2c76482014-10-28 10:14:47 +08007082
7083 if (boot_cpu_has(X86_FEATURE_NX))
7084 kvm_enable_efer_bits(EFER_NX);
7085
Wanpeng Li08d839c2017-03-23 05:30:08 -07007086 if (!cpu_has_vmx_vpid() || !cpu_has_vmx_invvpid() ||
7087 !(cpu_has_vmx_invvpid_single() || cpu_has_vmx_invvpid_global()))
Tiejun Chenf2c76482014-10-28 10:14:47 +08007088 enable_vpid = 0;
Wanpeng Li08d839c2017-03-23 05:30:08 -07007089
Tiejun Chenf2c76482014-10-28 10:14:47 +08007090 if (!cpu_has_vmx_ept() ||
David Hildenbrand42aa53b2017-08-10 23:15:29 +02007091 !cpu_has_vmx_ept_4levels() ||
David Hildenbrandf5f51582017-08-24 20:51:30 +02007092 !cpu_has_vmx_ept_mt_wb() ||
Wanpeng Li8ad81822017-10-09 15:51:53 -07007093 !cpu_has_vmx_invept_global())
Tiejun Chenf2c76482014-10-28 10:14:47 +08007094 enable_ept = 0;
Tiejun Chenf2c76482014-10-28 10:14:47 +08007095
Wanpeng Lifce6ac42017-05-11 02:58:56 -07007096 if (!cpu_has_vmx_ept_ad_bits() || !enable_ept)
Tiejun Chenf2c76482014-10-28 10:14:47 +08007097 enable_ept_ad_bits = 0;
7098
Wanpeng Li8ad81822017-10-09 15:51:53 -07007099 if (!cpu_has_vmx_unrestricted_guest() || !enable_ept)
Tiejun Chenf2c76482014-10-28 10:14:47 +08007100 enable_unrestricted_guest = 0;
7101
Paolo Bonziniad15a292015-01-30 16:18:49 +01007102 if (!cpu_has_vmx_flexpriority())
Tiejun Chenf2c76482014-10-28 10:14:47 +08007103 flexpriority_enabled = 0;
7104
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01007105 if (!cpu_has_virtual_nmis())
7106 enable_vnmi = 0;
7107
Paolo Bonziniad15a292015-01-30 16:18:49 +01007108 /*
7109 * set_apic_access_page_addr() is used to reload apic access
7110 * page upon invalidation. No need to do anything if not
7111 * using the APIC_ACCESS_ADDR VMCS field.
7112 */
7113 if (!flexpriority_enabled)
Tiejun Chenf2c76482014-10-28 10:14:47 +08007114 kvm_x86_ops->set_apic_access_page_addr = NULL;
Tiejun Chenf2c76482014-10-28 10:14:47 +08007115
7116 if (!cpu_has_vmx_tpr_shadow())
7117 kvm_x86_ops->update_cr8_intercept = NULL;
7118
7119 if (enable_ept && !cpu_has_vmx_ept_2m_page())
7120 kvm_disable_largepages();
7121
Wanpeng Li0f107682017-09-28 18:06:24 -07007122 if (!cpu_has_vmx_ple()) {
Tiejun Chenf2c76482014-10-28 10:14:47 +08007123 ple_gap = 0;
Wanpeng Li0f107682017-09-28 18:06:24 -07007124 ple_window = 0;
7125 ple_window_grow = 0;
7126 ple_window_max = 0;
7127 ple_window_shrink = 0;
7128 }
Tiejun Chenf2c76482014-10-28 10:14:47 +08007129
Paolo Bonzini76dfafd52016-12-19 17:17:11 +01007130 if (!cpu_has_vmx_apicv()) {
Tiejun Chenf2c76482014-10-28 10:14:47 +08007131 enable_apicv = 0;
Paolo Bonzini76dfafd52016-12-19 17:17:11 +01007132 kvm_x86_ops->sync_pir_to_irr = NULL;
7133 }
Tiejun Chenf2c76482014-10-28 10:14:47 +08007134
Haozhong Zhang64903d62015-10-20 15:39:09 +08007135 if (cpu_has_vmx_tsc_scaling()) {
7136 kvm_has_tsc_control = true;
7137 kvm_max_tsc_scaling_ratio = KVM_VMX_TSC_MULTIPLIER_MAX;
7138 kvm_tsc_scaling_ratio_frac_bits = 48;
7139 }
7140
Wanpeng Li04bb92e2015-09-16 19:31:11 +08007141 set_bit(0, vmx_vpid_bitmap); /* 0 is reserved for host */
7142
Junaid Shahidf160c7b2016-12-06 16:46:16 -08007143 if (enable_ept)
7144 vmx_enable_tdp();
7145 else
Tiejun Chenbaa03522014-12-23 16:21:11 +08007146 kvm_disable_tdp();
7147
7148 update_ple_window_actual_max();
7149
Kai Huang843e4332015-01-28 10:54:28 +08007150 /*
7151 * Only enable PML when hardware supports PML feature, and both EPT
7152 * and EPT A/D bit features are enabled -- PML depends on them to work.
7153 */
7154 if (!enable_ept || !enable_ept_ad_bits || !cpu_has_vmx_pml())
7155 enable_pml = 0;
7156
7157 if (!enable_pml) {
7158 kvm_x86_ops->slot_enable_log_dirty = NULL;
7159 kvm_x86_ops->slot_disable_log_dirty = NULL;
7160 kvm_x86_ops->flush_log_dirty = NULL;
7161 kvm_x86_ops->enable_log_dirty_pt_masked = NULL;
7162 }
7163
Yunhong Jiang64672c92016-06-13 14:19:59 -07007164 if (cpu_has_vmx_preemption_timer() && enable_preemption_timer) {
7165 u64 vmx_msr;
7166
7167 rdmsrl(MSR_IA32_VMX_MISC, vmx_msr);
7168 cpu_preemption_timer_multi =
7169 vmx_msr & VMX_MISC_PREEMPTION_TIMER_RATE_MASK;
7170 } else {
7171 kvm_x86_ops->set_hv_timer = NULL;
7172 kvm_x86_ops->cancel_hv_timer = NULL;
7173 }
7174
Paolo Bonzinic5d167b2017-12-13 11:05:19 +01007175 if (!cpu_has_vmx_shadow_vmcs())
7176 enable_shadow_vmcs = 0;
7177 if (enable_shadow_vmcs)
7178 init_vmcs_shadow_fields();
7179
Feng Wubf9f6ac2015-09-18 22:29:55 +08007180 kvm_set_posted_intr_wakeup_handler(wakeup_handler);
Paolo Bonzini13893092018-02-26 13:40:09 +01007181 nested_vmx_setup_ctls_msrs(&vmcs_config.nested, enable_apicv);
Feng Wubf9f6ac2015-09-18 22:29:55 +08007182
Ashok Rajc45dcc72016-06-22 14:59:56 +08007183 kvm_mce_cap_supported |= MCG_LMCE_P;
7184
Tiejun Chenf2c76482014-10-28 10:14:47 +08007185 return alloc_kvm_area();
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007186
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007187out:
Radim Krčmář23611332016-09-29 22:41:33 +02007188 for (i = 0; i < VMX_BITMAP_NR; i++)
7189 free_page((unsigned long)vmx_bitmap[i]);
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007190
7191 return r;
Tiejun Chenf2c76482014-10-28 10:14:47 +08007192}
7193
7194static __exit void hardware_unsetup(void)
7195{
Radim Krčmář23611332016-09-29 22:41:33 +02007196 int i;
7197
7198 for (i = 0; i < VMX_BITMAP_NR; i++)
7199 free_page((unsigned long)vmx_bitmap[i]);
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007200
Tiejun Chenf2c76482014-10-28 10:14:47 +08007201 free_kvm_area();
7202}
7203
Avi Kivity6aa8b732006-12-10 02:21:36 -08007204/*
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08007205 * Indicate a busy-waiting vcpu in spinlock. We do not enable the PAUSE
7206 * exiting, so only get here on cpu with PAUSE-Loop-Exiting.
7207 */
Marcelo Tosatti9fb41ba2009-10-12 19:37:31 -03007208static int handle_pause(struct kvm_vcpu *vcpu)
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08007209{
Radim Krčmářb4a2d312014-08-21 18:08:08 +02007210 if (ple_gap)
7211 grow_ple_window(vcpu);
7212
Longpeng(Mike)de63ad42017-08-08 12:05:33 +08007213 /*
7214 * Intel sdm vol3 ch-25.1.3 says: The "PAUSE-loop exiting"
7215 * VM-execution control is ignored if CPL > 0. OTOH, KVM
7216 * never set PAUSE_EXITING and just set PLE if supported,
7217 * so the vcpu must be CPL=0 if it gets a PAUSE exit.
7218 */
7219 kvm_vcpu_on_spin(vcpu, true);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007220 return kvm_skip_emulated_instruction(vcpu);
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08007221}
7222
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04007223static int handle_nop(struct kvm_vcpu *vcpu)
Sheng Yang59708672009-12-15 13:29:54 +08007224{
Kyle Huey6affcbe2016-11-29 12:40:40 -08007225 return kvm_skip_emulated_instruction(vcpu);
Sheng Yang59708672009-12-15 13:29:54 +08007226}
7227
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04007228static int handle_mwait(struct kvm_vcpu *vcpu)
7229{
7230 printk_once(KERN_WARNING "kvm: MWAIT instruction emulated as NOP!\n");
7231 return handle_nop(vcpu);
7232}
7233
Jim Mattson45ec3682017-08-23 16:32:04 -07007234static int handle_invalid_op(struct kvm_vcpu *vcpu)
7235{
7236 kvm_queue_exception(vcpu, UD_VECTOR);
7237 return 1;
7238}
7239
Mihai Donțu5f3d45e2015-07-05 20:08:57 +03007240static int handle_monitor_trap(struct kvm_vcpu *vcpu)
7241{
7242 return 1;
7243}
7244
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04007245static int handle_monitor(struct kvm_vcpu *vcpu)
7246{
7247 printk_once(KERN_WARNING "kvm: MONITOR instruction emulated as NOP!\n");
7248 return handle_nop(vcpu);
7249}
7250
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08007251/*
Arthur Chunqi Li0658fba2013-07-04 15:03:32 +08007252 * The following 3 functions, nested_vmx_succeed()/failValid()/failInvalid(),
7253 * set the success or error code of an emulated VMX instruction, as specified
7254 * by Vol 2B, VMX Instruction Reference, "Conventions".
7255 */
7256static void nested_vmx_succeed(struct kvm_vcpu *vcpu)
7257{
7258 vmx_set_rflags(vcpu, vmx_get_rflags(vcpu)
7259 & ~(X86_EFLAGS_CF | X86_EFLAGS_PF | X86_EFLAGS_AF |
7260 X86_EFLAGS_ZF | X86_EFLAGS_SF | X86_EFLAGS_OF));
7261}
7262
7263static void nested_vmx_failInvalid(struct kvm_vcpu *vcpu)
7264{
7265 vmx_set_rflags(vcpu, (vmx_get_rflags(vcpu)
7266 & ~(X86_EFLAGS_PF | X86_EFLAGS_AF | X86_EFLAGS_ZF |
7267 X86_EFLAGS_SF | X86_EFLAGS_OF))
7268 | X86_EFLAGS_CF);
7269}
7270
Abel Gordon145c28d2013-04-18 14:36:55 +03007271static void nested_vmx_failValid(struct kvm_vcpu *vcpu,
Arthur Chunqi Li0658fba2013-07-04 15:03:32 +08007272 u32 vm_instruction_error)
7273{
7274 if (to_vmx(vcpu)->nested.current_vmptr == -1ull) {
7275 /*
7276 * failValid writes the error number to the current VMCS, which
7277 * can't be done there isn't a current VMCS.
7278 */
7279 nested_vmx_failInvalid(vcpu);
7280 return;
7281 }
7282 vmx_set_rflags(vcpu, (vmx_get_rflags(vcpu)
7283 & ~(X86_EFLAGS_CF | X86_EFLAGS_PF | X86_EFLAGS_AF |
7284 X86_EFLAGS_SF | X86_EFLAGS_OF))
7285 | X86_EFLAGS_ZF);
7286 get_vmcs12(vcpu)->vm_instruction_error = vm_instruction_error;
7287 /*
7288 * We don't need to force a shadow sync because
7289 * VM_INSTRUCTION_ERROR is not shadowed
7290 */
7291}
Abel Gordon145c28d2013-04-18 14:36:55 +03007292
Wincy Vanff651cb2014-12-11 08:52:58 +03007293static void nested_vmx_abort(struct kvm_vcpu *vcpu, u32 indicator)
7294{
7295 /* TODO: not to reset guest simply here. */
7296 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Paolo Bonzinibbe41b92016-08-19 17:51:20 +02007297 pr_debug_ratelimited("kvm: nested vmx abort, indicator %d\n", indicator);
Wincy Vanff651cb2014-12-11 08:52:58 +03007298}
7299
Jan Kiszkaf41245002014-03-07 20:03:13 +01007300static enum hrtimer_restart vmx_preemption_timer_fn(struct hrtimer *timer)
7301{
7302 struct vcpu_vmx *vmx =
7303 container_of(timer, struct vcpu_vmx, nested.preemption_timer);
7304
7305 vmx->nested.preemption_timer_expired = true;
7306 kvm_make_request(KVM_REQ_EVENT, &vmx->vcpu);
7307 kvm_vcpu_kick(&vmx->vcpu);
7308
7309 return HRTIMER_NORESTART;
7310}
7311
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +03007312/*
Bandan Das19677e32014-05-06 02:19:15 -04007313 * Decode the memory-address operand of a vmx instruction, as recorded on an
7314 * exit caused by such an instruction (run by a guest hypervisor).
7315 * On success, returns 0. When the operand is invalid, returns 1 and throws
7316 * #UD or #GP.
7317 */
7318static int get_vmx_mem_address(struct kvm_vcpu *vcpu,
7319 unsigned long exit_qualification,
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007320 u32 vmx_instruction_info, bool wr, gva_t *ret)
Bandan Das19677e32014-05-06 02:19:15 -04007321{
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007322 gva_t off;
7323 bool exn;
7324 struct kvm_segment s;
7325
Bandan Das19677e32014-05-06 02:19:15 -04007326 /*
7327 * According to Vol. 3B, "Information for VM Exits Due to Instruction
7328 * Execution", on an exit, vmx_instruction_info holds most of the
7329 * addressing components of the operand. Only the displacement part
7330 * is put in exit_qualification (see 3B, "Basic VM-Exit Information").
7331 * For how an actual address is calculated from all these components,
7332 * refer to Vol. 1, "Operand Addressing".
7333 */
7334 int scaling = vmx_instruction_info & 3;
7335 int addr_size = (vmx_instruction_info >> 7) & 7;
7336 bool is_reg = vmx_instruction_info & (1u << 10);
7337 int seg_reg = (vmx_instruction_info >> 15) & 7;
7338 int index_reg = (vmx_instruction_info >> 18) & 0xf;
7339 bool index_is_valid = !(vmx_instruction_info & (1u << 22));
7340 int base_reg = (vmx_instruction_info >> 23) & 0xf;
7341 bool base_is_valid = !(vmx_instruction_info & (1u << 27));
7342
7343 if (is_reg) {
7344 kvm_queue_exception(vcpu, UD_VECTOR);
7345 return 1;
7346 }
7347
7348 /* Addr = segment_base + offset */
7349 /* offset = base + [index * scale] + displacement */
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007350 off = exit_qualification; /* holds the displacement */
Bandan Das19677e32014-05-06 02:19:15 -04007351 if (base_is_valid)
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007352 off += kvm_register_read(vcpu, base_reg);
Bandan Das19677e32014-05-06 02:19:15 -04007353 if (index_is_valid)
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007354 off += kvm_register_read(vcpu, index_reg)<<scaling;
7355 vmx_get_segment(vcpu, &s, seg_reg);
7356 *ret = s.base + off;
Bandan Das19677e32014-05-06 02:19:15 -04007357
7358 if (addr_size == 1) /* 32 bit */
7359 *ret &= 0xffffffff;
7360
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007361 /* Checks for #GP/#SS exceptions. */
7362 exn = false;
Quentin Casasnovasff30ef42016-06-18 11:01:05 +02007363 if (is_long_mode(vcpu)) {
7364 /* Long mode: #GP(0)/#SS(0) if the memory address is in a
7365 * non-canonical form. This is the only check on the memory
7366 * destination for long mode!
7367 */
Yu Zhangfd8cb432017-08-24 20:27:56 +08007368 exn = is_noncanonical_address(*ret, vcpu);
Quentin Casasnovasff30ef42016-06-18 11:01:05 +02007369 } else if (is_protmode(vcpu)) {
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007370 /* Protected mode: apply checks for segment validity in the
7371 * following order:
7372 * - segment type check (#GP(0) may be thrown)
7373 * - usability check (#GP(0)/#SS(0))
7374 * - limit check (#GP(0)/#SS(0))
7375 */
7376 if (wr)
7377 /* #GP(0) if the destination operand is located in a
7378 * read-only data segment or any code segment.
7379 */
7380 exn = ((s.type & 0xa) == 0 || (s.type & 8));
7381 else
7382 /* #GP(0) if the source operand is located in an
7383 * execute-only code segment
7384 */
7385 exn = ((s.type & 0xa) == 8);
Quentin Casasnovasff30ef42016-06-18 11:01:05 +02007386 if (exn) {
7387 kvm_queue_exception_e(vcpu, GP_VECTOR, 0);
7388 return 1;
7389 }
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007390 /* Protected mode: #GP(0)/#SS(0) if the segment is unusable.
7391 */
7392 exn = (s.unusable != 0);
7393 /* Protected mode: #GP(0)/#SS(0) if the memory
7394 * operand is outside the segment limit.
7395 */
7396 exn = exn || (off + sizeof(u64) > s.limit);
7397 }
7398 if (exn) {
7399 kvm_queue_exception_e(vcpu,
7400 seg_reg == VCPU_SREG_SS ?
7401 SS_VECTOR : GP_VECTOR,
7402 0);
7403 return 1;
7404 }
7405
Bandan Das19677e32014-05-06 02:19:15 -04007406 return 0;
7407}
7408
Radim Krčmářcbf71272017-05-19 15:48:51 +02007409static int nested_vmx_get_vmptr(struct kvm_vcpu *vcpu, gpa_t *vmpointer)
Bandan Das3573e222014-05-06 02:19:16 -04007410{
7411 gva_t gva;
Bandan Das3573e222014-05-06 02:19:16 -04007412 struct x86_exception e;
Bandan Das3573e222014-05-06 02:19:16 -04007413
7414 if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007415 vmcs_read32(VMX_INSTRUCTION_INFO), false, &gva))
Bandan Das3573e222014-05-06 02:19:16 -04007416 return 1;
7417
Radim Krčmářcbf71272017-05-19 15:48:51 +02007418 if (kvm_read_guest_virt(&vcpu->arch.emulate_ctxt, gva, vmpointer,
7419 sizeof(*vmpointer), &e)) {
Bandan Das3573e222014-05-06 02:19:16 -04007420 kvm_inject_page_fault(vcpu, &e);
7421 return 1;
7422 }
7423
Bandan Das3573e222014-05-06 02:19:16 -04007424 return 0;
7425}
7426
Jim Mattsone29acc52016-11-30 12:03:43 -08007427static int enter_vmx_operation(struct kvm_vcpu *vcpu)
7428{
7429 struct vcpu_vmx *vmx = to_vmx(vcpu);
7430 struct vmcs *shadow_vmcs;
Paolo Bonzinif21f1652018-01-11 12:16:15 +01007431 int r;
Jim Mattsone29acc52016-11-30 12:03:43 -08007432
Paolo Bonzinif21f1652018-01-11 12:16:15 +01007433 r = alloc_loaded_vmcs(&vmx->nested.vmcs02);
7434 if (r < 0)
Jim Mattsonde3a0022017-11-27 17:22:25 -06007435 goto out_vmcs02;
Jim Mattsone29acc52016-11-30 12:03:43 -08007436
7437 vmx->nested.cached_vmcs12 = kmalloc(VMCS12_SIZE, GFP_KERNEL);
7438 if (!vmx->nested.cached_vmcs12)
7439 goto out_cached_vmcs12;
7440
7441 if (enable_shadow_vmcs) {
7442 shadow_vmcs = alloc_vmcs();
7443 if (!shadow_vmcs)
7444 goto out_shadow_vmcs;
7445 /* mark vmcs as shadow */
7446 shadow_vmcs->revision_id |= (1u << 31);
7447 /* init shadow vmcs */
7448 vmcs_clear(shadow_vmcs);
7449 vmx->vmcs01.shadow_vmcs = shadow_vmcs;
7450 }
7451
Jim Mattsone29acc52016-11-30 12:03:43 -08007452 hrtimer_init(&vmx->nested.preemption_timer, CLOCK_MONOTONIC,
7453 HRTIMER_MODE_REL_PINNED);
7454 vmx->nested.preemption_timer.function = vmx_preemption_timer_fn;
7455
7456 vmx->nested.vmxon = true;
7457 return 0;
7458
7459out_shadow_vmcs:
7460 kfree(vmx->nested.cached_vmcs12);
7461
7462out_cached_vmcs12:
Jim Mattsonde3a0022017-11-27 17:22:25 -06007463 free_loaded_vmcs(&vmx->nested.vmcs02);
Jim Mattsone29acc52016-11-30 12:03:43 -08007464
Jim Mattsonde3a0022017-11-27 17:22:25 -06007465out_vmcs02:
Jim Mattsone29acc52016-11-30 12:03:43 -08007466 return -ENOMEM;
7467}
7468
Bandan Das3573e222014-05-06 02:19:16 -04007469/*
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007470 * Emulate the VMXON instruction.
7471 * Currently, we just remember that VMX is active, and do not save or even
7472 * inspect the argument to VMXON (the so-called "VMXON pointer") because we
7473 * do not currently need to store anything in that guest-allocated memory
7474 * region. Consequently, VMCLEAR and VMPTRLD also do not verify that the their
7475 * argument is different from the VMXON pointer (which the spec says they do).
7476 */
7477static int handle_vmon(struct kvm_vcpu *vcpu)
7478{
Jim Mattsone29acc52016-11-30 12:03:43 -08007479 int ret;
Radim Krčmářcbf71272017-05-19 15:48:51 +02007480 gpa_t vmptr;
7481 struct page *page;
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007482 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'Elb3897a42013-07-08 19:12:35 +08007483 const u64 VMXON_NEEDED_FEATURES = FEATURE_CONTROL_LOCKED
7484 | FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007485
Jim Mattson70f3aac2017-04-26 08:53:46 -07007486 /*
7487 * The Intel VMX Instruction Reference lists a bunch of bits that are
7488 * prerequisite to running VMXON, most notably cr4.VMXE must be set to
7489 * 1 (see vmx_set_cr4() for when we allow the guest to set this).
7490 * Otherwise, we should fail with #UD. But most faulting conditions
7491 * have already been checked by hardware, prior to the VM-exit for
7492 * VMXON. We do test guest cr4.VMXE because processor CR4 always has
7493 * that bit set to 1 in non-root mode.
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007494 */
Jim Mattson70f3aac2017-04-26 08:53:46 -07007495 if (!kvm_read_cr4_bits(vcpu, X86_CR4_VMXE)) {
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007496 kvm_queue_exception(vcpu, UD_VECTOR);
7497 return 1;
7498 }
7499
Abel Gordon145c28d2013-04-18 14:36:55 +03007500 if (vmx->nested.vmxon) {
7501 nested_vmx_failValid(vcpu, VMXERR_VMXON_IN_VMX_ROOT_OPERATION);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007502 return kvm_skip_emulated_instruction(vcpu);
Abel Gordon145c28d2013-04-18 14:36:55 +03007503 }
Nadav Har'Elb3897a42013-07-08 19:12:35 +08007504
Haozhong Zhang3b840802016-06-22 14:59:54 +08007505 if ((vmx->msr_ia32_feature_control & VMXON_NEEDED_FEATURES)
Nadav Har'Elb3897a42013-07-08 19:12:35 +08007506 != VMXON_NEEDED_FEATURES) {
7507 kvm_inject_gp(vcpu, 0);
7508 return 1;
7509 }
7510
Radim Krčmářcbf71272017-05-19 15:48:51 +02007511 if (nested_vmx_get_vmptr(vcpu, &vmptr))
Jim Mattson21e7fbe2016-12-22 15:49:55 -08007512 return 1;
Radim Krčmářcbf71272017-05-19 15:48:51 +02007513
7514 /*
7515 * SDM 3: 24.11.5
7516 * The first 4 bytes of VMXON region contain the supported
7517 * VMCS revision identifier
7518 *
7519 * Note - IA32_VMX_BASIC[48] will never be 1 for the nested case;
7520 * which replaces physical address width with 32
7521 */
7522 if (!PAGE_ALIGNED(vmptr) || (vmptr >> cpuid_maxphyaddr(vcpu))) {
7523 nested_vmx_failInvalid(vcpu);
7524 return kvm_skip_emulated_instruction(vcpu);
7525 }
7526
David Hildenbrand5e2f30b2017-08-03 18:11:04 +02007527 page = kvm_vcpu_gpa_to_page(vcpu, vmptr);
7528 if (is_error_page(page)) {
Radim Krčmářcbf71272017-05-19 15:48:51 +02007529 nested_vmx_failInvalid(vcpu);
7530 return kvm_skip_emulated_instruction(vcpu);
7531 }
7532 if (*(u32 *)kmap(page) != VMCS12_REVISION) {
7533 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +02007534 kvm_release_page_clean(page);
Radim Krčmářcbf71272017-05-19 15:48:51 +02007535 nested_vmx_failInvalid(vcpu);
7536 return kvm_skip_emulated_instruction(vcpu);
7537 }
7538 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +02007539 kvm_release_page_clean(page);
Radim Krčmářcbf71272017-05-19 15:48:51 +02007540
7541 vmx->nested.vmxon_ptr = vmptr;
Jim Mattsone29acc52016-11-30 12:03:43 -08007542 ret = enter_vmx_operation(vcpu);
7543 if (ret)
7544 return ret;
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007545
Arthur Chunqi Lia25eb112013-07-04 15:03:33 +08007546 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007547 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007548}
7549
7550/*
7551 * Intel's VMX Instruction Reference specifies a common set of prerequisites
7552 * for running VMX instructions (except VMXON, whose prerequisites are
7553 * slightly different). It also specifies what exception to inject otherwise.
Jim Mattson70f3aac2017-04-26 08:53:46 -07007554 * Note that many of these exceptions have priority over VM exits, so they
7555 * don't have to be checked again here.
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007556 */
7557static int nested_vmx_check_permission(struct kvm_vcpu *vcpu)
7558{
Jim Mattson70f3aac2017-04-26 08:53:46 -07007559 if (!to_vmx(vcpu)->nested.vmxon) {
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007560 kvm_queue_exception(vcpu, UD_VECTOR);
7561 return 0;
7562 }
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007563 return 1;
7564}
7565
David Matlack8ca44e82017-08-01 14:00:39 -07007566static void vmx_disable_shadow_vmcs(struct vcpu_vmx *vmx)
7567{
7568 vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL, SECONDARY_EXEC_SHADOW_VMCS);
7569 vmcs_write64(VMCS_LINK_POINTER, -1ull);
7570}
7571
Abel Gordone7953d72013-04-18 14:37:55 +03007572static inline void nested_release_vmcs12(struct vcpu_vmx *vmx)
7573{
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02007574 if (vmx->nested.current_vmptr == -1ull)
7575 return;
7576
Abel Gordon012f83c2013-04-18 14:39:25 +03007577 if (enable_shadow_vmcs) {
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02007578 /* copy to memory all shadowed fields in case
7579 they were modified */
7580 copy_shadow_to_vmcs12(vmx);
7581 vmx->nested.sync_shadow_vmcs = false;
David Matlack8ca44e82017-08-01 14:00:39 -07007582 vmx_disable_shadow_vmcs(vmx);
Abel Gordon012f83c2013-04-18 14:39:25 +03007583 }
Wincy Van705699a2015-02-03 23:58:17 +08007584 vmx->nested.posted_intr_nv = -1;
David Matlack4f2777b2016-07-13 17:16:37 -07007585
7586 /* Flush VMCS12 to guest memory */
Paolo Bonzini9f744c52017-07-27 15:54:46 +02007587 kvm_vcpu_write_guest_page(&vmx->vcpu,
7588 vmx->nested.current_vmptr >> PAGE_SHIFT,
7589 vmx->nested.cached_vmcs12, 0, VMCS12_SIZE);
David Matlack4f2777b2016-07-13 17:16:37 -07007590
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02007591 vmx->nested.current_vmptr = -1ull;
Abel Gordone7953d72013-04-18 14:37:55 +03007592}
7593
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007594/*
7595 * Free whatever needs to be freed from vmx->nested when L1 goes down, or
7596 * just stops using VMX.
7597 */
7598static void free_nested(struct vcpu_vmx *vmx)
7599{
Wanpeng Lib7455822017-11-22 14:04:00 -08007600 if (!vmx->nested.vmxon && !vmx->nested.smm.vmxon)
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007601 return;
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02007602
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007603 vmx->nested.vmxon = false;
Wanpeng Lib7455822017-11-22 14:04:00 -08007604 vmx->nested.smm.vmxon = false;
Wanpeng Li5c614b32015-10-13 09:18:36 -07007605 free_vpid(vmx->nested.vpid02);
David Matlack8ca44e82017-08-01 14:00:39 -07007606 vmx->nested.posted_intr_nv = -1;
7607 vmx->nested.current_vmptr = -1ull;
Jim Mattson355f4fb2016-10-28 08:29:39 -07007608 if (enable_shadow_vmcs) {
David Matlack8ca44e82017-08-01 14:00:39 -07007609 vmx_disable_shadow_vmcs(vmx);
Jim Mattson355f4fb2016-10-28 08:29:39 -07007610 vmcs_clear(vmx->vmcs01.shadow_vmcs);
7611 free_vmcs(vmx->vmcs01.shadow_vmcs);
7612 vmx->vmcs01.shadow_vmcs = NULL;
7613 }
David Matlack4f2777b2016-07-13 17:16:37 -07007614 kfree(vmx->nested.cached_vmcs12);
Jim Mattsonde3a0022017-11-27 17:22:25 -06007615 /* Unpin physical memory we referred to in the vmcs02 */
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03007616 if (vmx->nested.apic_access_page) {
David Hildenbrand53a70da2017-08-03 18:11:05 +02007617 kvm_release_page_dirty(vmx->nested.apic_access_page);
Paolo Bonzini48d89b92014-08-26 13:27:46 +02007618 vmx->nested.apic_access_page = NULL;
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03007619 }
Wanpeng Lia7c0b072014-08-21 19:46:50 +08007620 if (vmx->nested.virtual_apic_page) {
David Hildenbrand53a70da2017-08-03 18:11:05 +02007621 kvm_release_page_dirty(vmx->nested.virtual_apic_page);
Paolo Bonzini48d89b92014-08-26 13:27:46 +02007622 vmx->nested.virtual_apic_page = NULL;
Wanpeng Lia7c0b072014-08-21 19:46:50 +08007623 }
Wincy Van705699a2015-02-03 23:58:17 +08007624 if (vmx->nested.pi_desc_page) {
7625 kunmap(vmx->nested.pi_desc_page);
David Hildenbrand53a70da2017-08-03 18:11:05 +02007626 kvm_release_page_dirty(vmx->nested.pi_desc_page);
Wincy Van705699a2015-02-03 23:58:17 +08007627 vmx->nested.pi_desc_page = NULL;
7628 vmx->nested.pi_desc = NULL;
7629 }
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +03007630
Jim Mattsonde3a0022017-11-27 17:22:25 -06007631 free_loaded_vmcs(&vmx->nested.vmcs02);
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007632}
7633
7634/* Emulate the VMXOFF instruction */
7635static int handle_vmoff(struct kvm_vcpu *vcpu)
7636{
7637 if (!nested_vmx_check_permission(vcpu))
7638 return 1;
7639 free_nested(to_vmx(vcpu));
Arthur Chunqi Lia25eb112013-07-04 15:03:33 +08007640 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007641 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007642}
7643
Nadav Har'El27d6c862011-05-25 23:06:59 +03007644/* Emulate the VMCLEAR instruction */
7645static int handle_vmclear(struct kvm_vcpu *vcpu)
7646{
7647 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jim Mattson587d7e722017-03-02 12:41:48 -08007648 u32 zero = 0;
Nadav Har'El27d6c862011-05-25 23:06:59 +03007649 gpa_t vmptr;
Nadav Har'El27d6c862011-05-25 23:06:59 +03007650
7651 if (!nested_vmx_check_permission(vcpu))
7652 return 1;
7653
Radim Krčmářcbf71272017-05-19 15:48:51 +02007654 if (nested_vmx_get_vmptr(vcpu, &vmptr))
Nadav Har'El27d6c862011-05-25 23:06:59 +03007655 return 1;
7656
Radim Krčmářcbf71272017-05-19 15:48:51 +02007657 if (!PAGE_ALIGNED(vmptr) || (vmptr >> cpuid_maxphyaddr(vcpu))) {
7658 nested_vmx_failValid(vcpu, VMXERR_VMCLEAR_INVALID_ADDRESS);
7659 return kvm_skip_emulated_instruction(vcpu);
7660 }
7661
7662 if (vmptr == vmx->nested.vmxon_ptr) {
7663 nested_vmx_failValid(vcpu, VMXERR_VMCLEAR_VMXON_POINTER);
7664 return kvm_skip_emulated_instruction(vcpu);
7665 }
7666
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02007667 if (vmptr == vmx->nested.current_vmptr)
Abel Gordone7953d72013-04-18 14:37:55 +03007668 nested_release_vmcs12(vmx);
Nadav Har'El27d6c862011-05-25 23:06:59 +03007669
Jim Mattson587d7e722017-03-02 12:41:48 -08007670 kvm_vcpu_write_guest(vcpu,
7671 vmptr + offsetof(struct vmcs12, launch_state),
7672 &zero, sizeof(zero));
Nadav Har'El27d6c862011-05-25 23:06:59 +03007673
Nadav Har'El27d6c862011-05-25 23:06:59 +03007674 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007675 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El27d6c862011-05-25 23:06:59 +03007676}
7677
Nadav Har'Elcd232ad2011-05-25 23:10:33 +03007678static int nested_vmx_run(struct kvm_vcpu *vcpu, bool launch);
7679
7680/* Emulate the VMLAUNCH instruction */
7681static int handle_vmlaunch(struct kvm_vcpu *vcpu)
7682{
7683 return nested_vmx_run(vcpu, true);
7684}
7685
7686/* Emulate the VMRESUME instruction */
7687static int handle_vmresume(struct kvm_vcpu *vcpu)
7688{
7689
7690 return nested_vmx_run(vcpu, false);
7691}
7692
Nadav Har'El49f705c2011-05-25 23:08:30 +03007693/*
7694 * Read a vmcs12 field. Since these can have varying lengths and we return
7695 * one type, we chose the biggest type (u64) and zero-extend the return value
7696 * to that size. Note that the caller, handle_vmread, might need to use only
7697 * some of the bits we return here (e.g., on 32-bit guests, only 32 bits of
7698 * 64-bit fields are to be returned).
7699 */
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007700static inline int vmcs12_read_any(struct kvm_vcpu *vcpu,
7701 unsigned long field, u64 *ret)
Nadav Har'El49f705c2011-05-25 23:08:30 +03007702{
7703 short offset = vmcs_field_to_offset(field);
7704 char *p;
7705
7706 if (offset < 0)
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007707 return offset;
Nadav Har'El49f705c2011-05-25 23:08:30 +03007708
7709 p = ((char *)(get_vmcs12(vcpu))) + offset;
7710
Jim Mattsond37f4262017-12-22 12:12:16 -08007711 switch (vmcs_field_width(field)) {
7712 case VMCS_FIELD_WIDTH_NATURAL_WIDTH:
Nadav Har'El49f705c2011-05-25 23:08:30 +03007713 *ret = *((natural_width *)p);
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007714 return 0;
Jim Mattsond37f4262017-12-22 12:12:16 -08007715 case VMCS_FIELD_WIDTH_U16:
Nadav Har'El49f705c2011-05-25 23:08:30 +03007716 *ret = *((u16 *)p);
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007717 return 0;
Jim Mattsond37f4262017-12-22 12:12:16 -08007718 case VMCS_FIELD_WIDTH_U32:
Nadav Har'El49f705c2011-05-25 23:08:30 +03007719 *ret = *((u32 *)p);
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007720 return 0;
Jim Mattsond37f4262017-12-22 12:12:16 -08007721 case VMCS_FIELD_WIDTH_U64:
Nadav Har'El49f705c2011-05-25 23:08:30 +03007722 *ret = *((u64 *)p);
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007723 return 0;
Nadav Har'El49f705c2011-05-25 23:08:30 +03007724 default:
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007725 WARN_ON(1);
7726 return -ENOENT;
Nadav Har'El49f705c2011-05-25 23:08:30 +03007727 }
7728}
7729
Abel Gordon20b97fe2013-04-18 14:36:25 +03007730
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007731static inline int vmcs12_write_any(struct kvm_vcpu *vcpu,
7732 unsigned long field, u64 field_value){
Abel Gordon20b97fe2013-04-18 14:36:25 +03007733 short offset = vmcs_field_to_offset(field);
7734 char *p = ((char *) get_vmcs12(vcpu)) + offset;
7735 if (offset < 0)
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007736 return offset;
Abel Gordon20b97fe2013-04-18 14:36:25 +03007737
Jim Mattsond37f4262017-12-22 12:12:16 -08007738 switch (vmcs_field_width(field)) {
7739 case VMCS_FIELD_WIDTH_U16:
Abel Gordon20b97fe2013-04-18 14:36:25 +03007740 *(u16 *)p = field_value;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007741 return 0;
Jim Mattsond37f4262017-12-22 12:12:16 -08007742 case VMCS_FIELD_WIDTH_U32:
Abel Gordon20b97fe2013-04-18 14:36:25 +03007743 *(u32 *)p = field_value;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007744 return 0;
Jim Mattsond37f4262017-12-22 12:12:16 -08007745 case VMCS_FIELD_WIDTH_U64:
Abel Gordon20b97fe2013-04-18 14:36:25 +03007746 *(u64 *)p = field_value;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007747 return 0;
Jim Mattsond37f4262017-12-22 12:12:16 -08007748 case VMCS_FIELD_WIDTH_NATURAL_WIDTH:
Abel Gordon20b97fe2013-04-18 14:36:25 +03007749 *(natural_width *)p = field_value;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007750 return 0;
Abel Gordon20b97fe2013-04-18 14:36:25 +03007751 default:
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007752 WARN_ON(1);
7753 return -ENOENT;
Abel Gordon20b97fe2013-04-18 14:36:25 +03007754 }
7755
7756}
7757
Abel Gordon16f5b902013-04-18 14:38:25 +03007758static void copy_shadow_to_vmcs12(struct vcpu_vmx *vmx)
7759{
7760 int i;
7761 unsigned long field;
7762 u64 field_value;
Jim Mattson355f4fb2016-10-28 08:29:39 -07007763 struct vmcs *shadow_vmcs = vmx->vmcs01.shadow_vmcs;
Paolo Bonzini44900ba2017-12-13 12:58:02 +01007764 const u16 *fields = shadow_read_write_fields;
Mathias Krausec2bae892013-06-26 20:36:21 +02007765 const int num_fields = max_shadow_read_write_fields;
Abel Gordon16f5b902013-04-18 14:38:25 +03007766
Jan Kiszka282da872014-10-08 18:05:39 +02007767 preempt_disable();
7768
Abel Gordon16f5b902013-04-18 14:38:25 +03007769 vmcs_load(shadow_vmcs);
7770
7771 for (i = 0; i < num_fields; i++) {
7772 field = fields[i];
Paolo Bonzini44900ba2017-12-13 12:58:02 +01007773 field_value = __vmcs_readl(field);
Abel Gordon16f5b902013-04-18 14:38:25 +03007774 vmcs12_write_any(&vmx->vcpu, field, field_value);
7775 }
7776
7777 vmcs_clear(shadow_vmcs);
7778 vmcs_load(vmx->loaded_vmcs->vmcs);
Jan Kiszka282da872014-10-08 18:05:39 +02007779
7780 preempt_enable();
Abel Gordon16f5b902013-04-18 14:38:25 +03007781}
7782
Abel Gordonc3114422013-04-18 14:38:55 +03007783static void copy_vmcs12_to_shadow(struct vcpu_vmx *vmx)
7784{
Paolo Bonzini44900ba2017-12-13 12:58:02 +01007785 const u16 *fields[] = {
Mathias Krausec2bae892013-06-26 20:36:21 +02007786 shadow_read_write_fields,
7787 shadow_read_only_fields
Abel Gordonc3114422013-04-18 14:38:55 +03007788 };
Mathias Krausec2bae892013-06-26 20:36:21 +02007789 const int max_fields[] = {
Abel Gordonc3114422013-04-18 14:38:55 +03007790 max_shadow_read_write_fields,
7791 max_shadow_read_only_fields
7792 };
7793 int i, q;
7794 unsigned long field;
7795 u64 field_value = 0;
Jim Mattson355f4fb2016-10-28 08:29:39 -07007796 struct vmcs *shadow_vmcs = vmx->vmcs01.shadow_vmcs;
Abel Gordonc3114422013-04-18 14:38:55 +03007797
7798 vmcs_load(shadow_vmcs);
7799
Mathias Krausec2bae892013-06-26 20:36:21 +02007800 for (q = 0; q < ARRAY_SIZE(fields); q++) {
Abel Gordonc3114422013-04-18 14:38:55 +03007801 for (i = 0; i < max_fields[q]; i++) {
7802 field = fields[q][i];
7803 vmcs12_read_any(&vmx->vcpu, field, &field_value);
Paolo Bonzini44900ba2017-12-13 12:58:02 +01007804 __vmcs_writel(field, field_value);
Abel Gordonc3114422013-04-18 14:38:55 +03007805 }
7806 }
7807
7808 vmcs_clear(shadow_vmcs);
7809 vmcs_load(vmx->loaded_vmcs->vmcs);
7810}
7811
Nadav Har'El49f705c2011-05-25 23:08:30 +03007812/*
7813 * VMX instructions which assume a current vmcs12 (i.e., that VMPTRLD was
7814 * used before) all generate the same failure when it is missing.
7815 */
7816static int nested_vmx_check_vmcs12(struct kvm_vcpu *vcpu)
7817{
7818 struct vcpu_vmx *vmx = to_vmx(vcpu);
7819 if (vmx->nested.current_vmptr == -1ull) {
7820 nested_vmx_failInvalid(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03007821 return 0;
7822 }
7823 return 1;
7824}
7825
7826static int handle_vmread(struct kvm_vcpu *vcpu)
7827{
7828 unsigned long field;
7829 u64 field_value;
7830 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7831 u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
7832 gva_t gva = 0;
7833
Kyle Hueyeb277562016-11-29 12:40:39 -08007834 if (!nested_vmx_check_permission(vcpu))
Nadav Har'El49f705c2011-05-25 23:08:30 +03007835 return 1;
7836
Kyle Huey6affcbe2016-11-29 12:40:40 -08007837 if (!nested_vmx_check_vmcs12(vcpu))
7838 return kvm_skip_emulated_instruction(vcpu);
Kyle Hueyeb277562016-11-29 12:40:39 -08007839
Nadav Har'El49f705c2011-05-25 23:08:30 +03007840 /* Decode instruction info and find the field to read */
Nadav Amit27e6fb52014-06-18 17:19:26 +03007841 field = kvm_register_readl(vcpu, (((vmx_instruction_info) >> 28) & 0xf));
Nadav Har'El49f705c2011-05-25 23:08:30 +03007842 /* Read the field, zero-extended to a u64 field_value */
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007843 if (vmcs12_read_any(vcpu, field, &field_value) < 0) {
Nadav Har'El49f705c2011-05-25 23:08:30 +03007844 nested_vmx_failValid(vcpu, VMXERR_UNSUPPORTED_VMCS_COMPONENT);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007845 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03007846 }
7847 /*
7848 * Now copy part of this value to register or memory, as requested.
7849 * Note that the number of bits actually copied is 32 or 64 depending
7850 * on the guest's mode (32 or 64 bit), not on the given field's length.
7851 */
7852 if (vmx_instruction_info & (1u << 10)) {
Nadav Amit27e6fb52014-06-18 17:19:26 +03007853 kvm_register_writel(vcpu, (((vmx_instruction_info) >> 3) & 0xf),
Nadav Har'El49f705c2011-05-25 23:08:30 +03007854 field_value);
7855 } else {
7856 if (get_vmx_mem_address(vcpu, exit_qualification,
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007857 vmx_instruction_info, true, &gva))
Nadav Har'El49f705c2011-05-25 23:08:30 +03007858 return 1;
Jim Mattson70f3aac2017-04-26 08:53:46 -07007859 /* _system ok, as hardware has verified cpl=0 */
Nadav Har'El49f705c2011-05-25 23:08:30 +03007860 kvm_write_guest_virt_system(&vcpu->arch.emulate_ctxt, gva,
7861 &field_value, (is_long_mode(vcpu) ? 8 : 4), NULL);
7862 }
7863
7864 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007865 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03007866}
7867
7868
7869static int handle_vmwrite(struct kvm_vcpu *vcpu)
7870{
7871 unsigned long field;
7872 gva_t gva;
Paolo Bonzini74a497f2017-12-20 13:55:39 +01007873 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03007874 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7875 u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
Paolo Bonzini74a497f2017-12-20 13:55:39 +01007876
Nadav Har'El49f705c2011-05-25 23:08:30 +03007877 /* The value to write might be 32 or 64 bits, depending on L1's long
7878 * mode, and eventually we need to write that into a field of several
7879 * possible lengths. The code below first zero-extends the value to 64
Adam Buchbinder6a6256f2016-02-23 15:34:30 -08007880 * bit (field_value), and then copies only the appropriate number of
Nadav Har'El49f705c2011-05-25 23:08:30 +03007881 * bits into the vmcs12 field.
7882 */
7883 u64 field_value = 0;
7884 struct x86_exception e;
7885
Kyle Hueyeb277562016-11-29 12:40:39 -08007886 if (!nested_vmx_check_permission(vcpu))
Nadav Har'El49f705c2011-05-25 23:08:30 +03007887 return 1;
7888
Kyle Huey6affcbe2016-11-29 12:40:40 -08007889 if (!nested_vmx_check_vmcs12(vcpu))
7890 return kvm_skip_emulated_instruction(vcpu);
Kyle Hueyeb277562016-11-29 12:40:39 -08007891
Nadav Har'El49f705c2011-05-25 23:08:30 +03007892 if (vmx_instruction_info & (1u << 10))
Nadav Amit27e6fb52014-06-18 17:19:26 +03007893 field_value = kvm_register_readl(vcpu,
Nadav Har'El49f705c2011-05-25 23:08:30 +03007894 (((vmx_instruction_info) >> 3) & 0xf));
7895 else {
7896 if (get_vmx_mem_address(vcpu, exit_qualification,
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007897 vmx_instruction_info, false, &gva))
Nadav Har'El49f705c2011-05-25 23:08:30 +03007898 return 1;
7899 if (kvm_read_guest_virt(&vcpu->arch.emulate_ctxt, gva,
Nadav Amit27e6fb52014-06-18 17:19:26 +03007900 &field_value, (is_64_bit_mode(vcpu) ? 8 : 4), &e)) {
Nadav Har'El49f705c2011-05-25 23:08:30 +03007901 kvm_inject_page_fault(vcpu, &e);
7902 return 1;
7903 }
7904 }
7905
7906
Nadav Amit27e6fb52014-06-18 17:19:26 +03007907 field = kvm_register_readl(vcpu, (((vmx_instruction_info) >> 28) & 0xf));
Nadav Har'El49f705c2011-05-25 23:08:30 +03007908 if (vmcs_field_readonly(field)) {
7909 nested_vmx_failValid(vcpu,
7910 VMXERR_VMWRITE_READ_ONLY_VMCS_COMPONENT);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007911 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03007912 }
7913
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007914 if (vmcs12_write_any(vcpu, field, field_value) < 0) {
Nadav Har'El49f705c2011-05-25 23:08:30 +03007915 nested_vmx_failValid(vcpu, VMXERR_UNSUPPORTED_VMCS_COMPONENT);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007916 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03007917 }
7918
Paolo Bonzini74a497f2017-12-20 13:55:39 +01007919 switch (field) {
7920#define SHADOW_FIELD_RW(x) case x:
7921#include "vmx_shadow_fields.h"
7922 /*
7923 * The fields that can be updated by L1 without a vmexit are
7924 * always updated in the vmcs02, the others go down the slow
7925 * path of prepare_vmcs02.
7926 */
7927 break;
7928 default:
7929 vmx->nested.dirty_vmcs12 = true;
7930 break;
7931 }
7932
Nadav Har'El49f705c2011-05-25 23:08:30 +03007933 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007934 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03007935}
7936
Jim Mattsona8bc2842016-11-30 12:03:44 -08007937static void set_current_vmptr(struct vcpu_vmx *vmx, gpa_t vmptr)
7938{
7939 vmx->nested.current_vmptr = vmptr;
7940 if (enable_shadow_vmcs) {
7941 vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL,
7942 SECONDARY_EXEC_SHADOW_VMCS);
7943 vmcs_write64(VMCS_LINK_POINTER,
7944 __pa(vmx->vmcs01.shadow_vmcs));
7945 vmx->nested.sync_shadow_vmcs = true;
7946 }
Paolo Bonzini74a497f2017-12-20 13:55:39 +01007947 vmx->nested.dirty_vmcs12 = true;
Jim Mattsona8bc2842016-11-30 12:03:44 -08007948}
7949
Nadav Har'El63846662011-05-25 23:07:29 +03007950/* Emulate the VMPTRLD instruction */
7951static int handle_vmptrld(struct kvm_vcpu *vcpu)
7952{
7953 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'El63846662011-05-25 23:07:29 +03007954 gpa_t vmptr;
Nadav Har'El63846662011-05-25 23:07:29 +03007955
7956 if (!nested_vmx_check_permission(vcpu))
7957 return 1;
7958
Radim Krčmářcbf71272017-05-19 15:48:51 +02007959 if (nested_vmx_get_vmptr(vcpu, &vmptr))
Nadav Har'El63846662011-05-25 23:07:29 +03007960 return 1;
7961
Radim Krčmářcbf71272017-05-19 15:48:51 +02007962 if (!PAGE_ALIGNED(vmptr) || (vmptr >> cpuid_maxphyaddr(vcpu))) {
7963 nested_vmx_failValid(vcpu, VMXERR_VMPTRLD_INVALID_ADDRESS);
7964 return kvm_skip_emulated_instruction(vcpu);
7965 }
7966
7967 if (vmptr == vmx->nested.vmxon_ptr) {
7968 nested_vmx_failValid(vcpu, VMXERR_VMPTRLD_VMXON_POINTER);
7969 return kvm_skip_emulated_instruction(vcpu);
7970 }
7971
Nadav Har'El63846662011-05-25 23:07:29 +03007972 if (vmx->nested.current_vmptr != vmptr) {
7973 struct vmcs12 *new_vmcs12;
7974 struct page *page;
David Hildenbrand5e2f30b2017-08-03 18:11:04 +02007975 page = kvm_vcpu_gpa_to_page(vcpu, vmptr);
7976 if (is_error_page(page)) {
Nadav Har'El63846662011-05-25 23:07:29 +03007977 nested_vmx_failInvalid(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007978 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El63846662011-05-25 23:07:29 +03007979 }
7980 new_vmcs12 = kmap(page);
7981 if (new_vmcs12->revision_id != VMCS12_REVISION) {
7982 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +02007983 kvm_release_page_clean(page);
Nadav Har'El63846662011-05-25 23:07:29 +03007984 nested_vmx_failValid(vcpu,
7985 VMXERR_VMPTRLD_INCORRECT_VMCS_REVISION_ID);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007986 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El63846662011-05-25 23:07:29 +03007987 }
Nadav Har'El63846662011-05-25 23:07:29 +03007988
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02007989 nested_release_vmcs12(vmx);
David Matlack4f2777b2016-07-13 17:16:37 -07007990 /*
7991 * Load VMCS12 from guest memory since it is not already
7992 * cached.
7993 */
Paolo Bonzini9f744c52017-07-27 15:54:46 +02007994 memcpy(vmx->nested.cached_vmcs12, new_vmcs12, VMCS12_SIZE);
7995 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +02007996 kvm_release_page_clean(page);
Paolo Bonzini9f744c52017-07-27 15:54:46 +02007997
Jim Mattsona8bc2842016-11-30 12:03:44 -08007998 set_current_vmptr(vmx, vmptr);
Nadav Har'El63846662011-05-25 23:07:29 +03007999 }
8000
8001 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008002 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El63846662011-05-25 23:07:29 +03008003}
8004
Nadav Har'El6a4d7552011-05-25 23:08:00 +03008005/* Emulate the VMPTRST instruction */
8006static int handle_vmptrst(struct kvm_vcpu *vcpu)
8007{
8008 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
8009 u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
8010 gva_t vmcs_gva;
8011 struct x86_exception e;
8012
8013 if (!nested_vmx_check_permission(vcpu))
8014 return 1;
8015
8016 if (get_vmx_mem_address(vcpu, exit_qualification,
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00008017 vmx_instruction_info, true, &vmcs_gva))
Nadav Har'El6a4d7552011-05-25 23:08:00 +03008018 return 1;
Jim Mattson70f3aac2017-04-26 08:53:46 -07008019 /* ok to use *_system, as hardware has verified cpl=0 */
Nadav Har'El6a4d7552011-05-25 23:08:00 +03008020 if (kvm_write_guest_virt_system(&vcpu->arch.emulate_ctxt, vmcs_gva,
8021 (void *)&to_vmx(vcpu)->nested.current_vmptr,
8022 sizeof(u64), &e)) {
8023 kvm_inject_page_fault(vcpu, &e);
8024 return 1;
8025 }
8026 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008027 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El6a4d7552011-05-25 23:08:00 +03008028}
8029
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008030/* Emulate the INVEPT instruction */
8031static int handle_invept(struct kvm_vcpu *vcpu)
8032{
Wincy Vanb9c237b2015-02-03 23:56:30 +08008033 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008034 u32 vmx_instruction_info, types;
8035 unsigned long type;
8036 gva_t gva;
8037 struct x86_exception e;
8038 struct {
8039 u64 eptp, gpa;
8040 } operand;
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008041
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01008042 if (!(vmx->nested.msrs.secondary_ctls_high &
Wincy Vanb9c237b2015-02-03 23:56:30 +08008043 SECONDARY_EXEC_ENABLE_EPT) ||
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01008044 !(vmx->nested.msrs.ept_caps & VMX_EPT_INVEPT_BIT)) {
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008045 kvm_queue_exception(vcpu, UD_VECTOR);
8046 return 1;
8047 }
8048
8049 if (!nested_vmx_check_permission(vcpu))
8050 return 1;
8051
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008052 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
Nadav Amit27e6fb52014-06-18 17:19:26 +03008053 type = kvm_register_readl(vcpu, (vmx_instruction_info >> 28) & 0xf);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008054
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01008055 types = (vmx->nested.msrs.ept_caps >> VMX_EPT_EXTENT_SHIFT) & 6;
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008056
Jim Mattson85c856b2016-10-26 08:38:38 -07008057 if (type >= 32 || !(types & (1 << type))) {
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008058 nested_vmx_failValid(vcpu,
8059 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008060 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008061 }
8062
8063 /* According to the Intel VMX instruction reference, the memory
8064 * operand is read even if it isn't needed (e.g., for type==global)
8065 */
8066 if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00008067 vmx_instruction_info, false, &gva))
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008068 return 1;
8069 if (kvm_read_guest_virt(&vcpu->arch.emulate_ctxt, gva, &operand,
8070 sizeof(operand), &e)) {
8071 kvm_inject_page_fault(vcpu, &e);
8072 return 1;
8073 }
8074
8075 switch (type) {
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008076 case VMX_EPT_EXTENT_GLOBAL:
Bandan Das45e11812016-08-02 16:32:36 -04008077 /*
8078 * TODO: track mappings and invalidate
8079 * single context requests appropriately
8080 */
8081 case VMX_EPT_EXTENT_CONTEXT:
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008082 kvm_mmu_sync_roots(vcpu);
Liang Chen77c39132014-09-18 12:38:37 -04008083 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008084 nested_vmx_succeed(vcpu);
8085 break;
8086 default:
8087 BUG_ON(1);
8088 break;
8089 }
8090
Kyle Huey6affcbe2016-11-29 12:40:40 -08008091 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008092}
8093
Petr Matouseka642fc32014-09-23 20:22:30 +02008094static int handle_invvpid(struct kvm_vcpu *vcpu)
8095{
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008096 struct vcpu_vmx *vmx = to_vmx(vcpu);
8097 u32 vmx_instruction_info;
8098 unsigned long type, types;
8099 gva_t gva;
8100 struct x86_exception e;
Jim Mattson40352602017-06-28 09:37:37 -07008101 struct {
8102 u64 vpid;
8103 u64 gla;
8104 } operand;
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008105
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01008106 if (!(vmx->nested.msrs.secondary_ctls_high &
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008107 SECONDARY_EXEC_ENABLE_VPID) ||
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01008108 !(vmx->nested.msrs.vpid_caps & VMX_VPID_INVVPID_BIT)) {
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008109 kvm_queue_exception(vcpu, UD_VECTOR);
8110 return 1;
8111 }
8112
8113 if (!nested_vmx_check_permission(vcpu))
8114 return 1;
8115
8116 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
8117 type = kvm_register_readl(vcpu, (vmx_instruction_info >> 28) & 0xf);
8118
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01008119 types = (vmx->nested.msrs.vpid_caps &
Jan Dakinevichbcdde302016-10-28 07:00:30 +03008120 VMX_VPID_EXTENT_SUPPORTED_MASK) >> 8;
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008121
Jim Mattson85c856b2016-10-26 08:38:38 -07008122 if (type >= 32 || !(types & (1 << type))) {
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008123 nested_vmx_failValid(vcpu,
8124 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008125 return kvm_skip_emulated_instruction(vcpu);
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008126 }
8127
8128 /* according to the intel vmx instruction reference, the memory
8129 * operand is read even if it isn't needed (e.g., for type==global)
8130 */
8131 if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
8132 vmx_instruction_info, false, &gva))
8133 return 1;
Jim Mattson40352602017-06-28 09:37:37 -07008134 if (kvm_read_guest_virt(&vcpu->arch.emulate_ctxt, gva, &operand,
8135 sizeof(operand), &e)) {
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008136 kvm_inject_page_fault(vcpu, &e);
8137 return 1;
8138 }
Jim Mattson40352602017-06-28 09:37:37 -07008139 if (operand.vpid >> 16) {
8140 nested_vmx_failValid(vcpu,
8141 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
8142 return kvm_skip_emulated_instruction(vcpu);
8143 }
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008144
8145 switch (type) {
Jan Dakinevichbcdde302016-10-28 07:00:30 +03008146 case VMX_VPID_EXTENT_INDIVIDUAL_ADDR:
Yu Zhangfd8cb432017-08-24 20:27:56 +08008147 if (is_noncanonical_address(operand.gla, vcpu)) {
Jim Mattson40352602017-06-28 09:37:37 -07008148 nested_vmx_failValid(vcpu,
8149 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
8150 return kvm_skip_emulated_instruction(vcpu);
8151 }
8152 /* fall through */
Paolo Bonzinief697a72016-03-18 16:58:38 +01008153 case VMX_VPID_EXTENT_SINGLE_CONTEXT:
Jan Dakinevichbcdde302016-10-28 07:00:30 +03008154 case VMX_VPID_EXTENT_SINGLE_NON_GLOBAL:
Jim Mattson40352602017-06-28 09:37:37 -07008155 if (!operand.vpid) {
Jan Dakinevichbcdde302016-10-28 07:00:30 +03008156 nested_vmx_failValid(vcpu,
8157 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008158 return kvm_skip_emulated_instruction(vcpu);
Jan Dakinevichbcdde302016-10-28 07:00:30 +03008159 }
8160 break;
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008161 case VMX_VPID_EXTENT_ALL_CONTEXT:
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008162 break;
8163 default:
Jan Dakinevichbcdde302016-10-28 07:00:30 +03008164 WARN_ON_ONCE(1);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008165 return kvm_skip_emulated_instruction(vcpu);
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008166 }
8167
Wanpeng Lic2ba05c2017-12-12 17:33:03 -08008168 __vmx_flush_tlb(vcpu, vmx->nested.vpid02, true);
Jan Dakinevichbcdde302016-10-28 07:00:30 +03008169 nested_vmx_succeed(vcpu);
8170
Kyle Huey6affcbe2016-11-29 12:40:40 -08008171 return kvm_skip_emulated_instruction(vcpu);
Petr Matouseka642fc32014-09-23 20:22:30 +02008172}
8173
Kai Huang843e4332015-01-28 10:54:28 +08008174static int handle_pml_full(struct kvm_vcpu *vcpu)
8175{
8176 unsigned long exit_qualification;
8177
8178 trace_kvm_pml_full(vcpu->vcpu_id);
8179
8180 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
8181
8182 /*
8183 * PML buffer FULL happened while executing iret from NMI,
8184 * "blocked by NMI" bit has to be set before next VM entry.
8185 */
8186 if (!(to_vmx(vcpu)->idt_vectoring_info & VECTORING_INFO_VALID_MASK) &&
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01008187 enable_vnmi &&
Kai Huang843e4332015-01-28 10:54:28 +08008188 (exit_qualification & INTR_INFO_UNBLOCK_NMI))
8189 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
8190 GUEST_INTR_STATE_NMI);
8191
8192 /*
8193 * PML buffer already flushed at beginning of VMEXIT. Nothing to do
8194 * here.., and there's no userspace involvement needed for PML.
8195 */
8196 return 1;
8197}
8198
Yunhong Jiang64672c92016-06-13 14:19:59 -07008199static int handle_preemption_timer(struct kvm_vcpu *vcpu)
8200{
8201 kvm_lapic_expired_hv_timer(vcpu);
8202 return 1;
8203}
8204
Bandan Das41ab9372017-08-03 15:54:43 -04008205static bool valid_ept_address(struct kvm_vcpu *vcpu, u64 address)
8206{
8207 struct vcpu_vmx *vmx = to_vmx(vcpu);
Bandan Das41ab9372017-08-03 15:54:43 -04008208 int maxphyaddr = cpuid_maxphyaddr(vcpu);
8209
8210 /* Check for memory type validity */
David Hildenbrandbb97a012017-08-10 23:15:28 +02008211 switch (address & VMX_EPTP_MT_MASK) {
8212 case VMX_EPTP_MT_UC:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01008213 if (!(vmx->nested.msrs.ept_caps & VMX_EPTP_UC_BIT))
Bandan Das41ab9372017-08-03 15:54:43 -04008214 return false;
8215 break;
David Hildenbrandbb97a012017-08-10 23:15:28 +02008216 case VMX_EPTP_MT_WB:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01008217 if (!(vmx->nested.msrs.ept_caps & VMX_EPTP_WB_BIT))
Bandan Das41ab9372017-08-03 15:54:43 -04008218 return false;
8219 break;
8220 default:
8221 return false;
8222 }
8223
David Hildenbrandbb97a012017-08-10 23:15:28 +02008224 /* only 4 levels page-walk length are valid */
8225 if ((address & VMX_EPTP_PWL_MASK) != VMX_EPTP_PWL_4)
Bandan Das41ab9372017-08-03 15:54:43 -04008226 return false;
8227
8228 /* Reserved bits should not be set */
8229 if (address >> maxphyaddr || ((address >> 7) & 0x1f))
8230 return false;
8231
8232 /* AD, if set, should be supported */
David Hildenbrandbb97a012017-08-10 23:15:28 +02008233 if (address & VMX_EPTP_AD_ENABLE_BIT) {
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01008234 if (!(vmx->nested.msrs.ept_caps & VMX_EPT_AD_BIT))
Bandan Das41ab9372017-08-03 15:54:43 -04008235 return false;
8236 }
8237
8238 return true;
8239}
8240
8241static int nested_vmx_eptp_switching(struct kvm_vcpu *vcpu,
8242 struct vmcs12 *vmcs12)
8243{
8244 u32 index = vcpu->arch.regs[VCPU_REGS_RCX];
8245 u64 address;
8246 bool accessed_dirty;
8247 struct kvm_mmu *mmu = vcpu->arch.walk_mmu;
8248
8249 if (!nested_cpu_has_eptp_switching(vmcs12) ||
8250 !nested_cpu_has_ept(vmcs12))
8251 return 1;
8252
8253 if (index >= VMFUNC_EPTP_ENTRIES)
8254 return 1;
8255
8256
8257 if (kvm_vcpu_read_guest_page(vcpu, vmcs12->eptp_list_address >> PAGE_SHIFT,
8258 &address, index * 8, 8))
8259 return 1;
8260
David Hildenbrandbb97a012017-08-10 23:15:28 +02008261 accessed_dirty = !!(address & VMX_EPTP_AD_ENABLE_BIT);
Bandan Das41ab9372017-08-03 15:54:43 -04008262
8263 /*
8264 * If the (L2) guest does a vmfunc to the currently
8265 * active ept pointer, we don't have to do anything else
8266 */
8267 if (vmcs12->ept_pointer != address) {
8268 if (!valid_ept_address(vcpu, address))
8269 return 1;
8270
8271 kvm_mmu_unload(vcpu);
8272 mmu->ept_ad = accessed_dirty;
8273 mmu->base_role.ad_disabled = !accessed_dirty;
8274 vmcs12->ept_pointer = address;
8275 /*
8276 * TODO: Check what's the correct approach in case
8277 * mmu reload fails. Currently, we just let the next
8278 * reload potentially fail
8279 */
8280 kvm_mmu_reload(vcpu);
8281 }
8282
8283 return 0;
8284}
8285
Bandan Das2a499e42017-08-03 15:54:41 -04008286static int handle_vmfunc(struct kvm_vcpu *vcpu)
8287{
Bandan Das27c42a12017-08-03 15:54:42 -04008288 struct vcpu_vmx *vmx = to_vmx(vcpu);
8289 struct vmcs12 *vmcs12;
8290 u32 function = vcpu->arch.regs[VCPU_REGS_RAX];
8291
8292 /*
8293 * VMFUNC is only supported for nested guests, but we always enable the
8294 * secondary control for simplicity; for non-nested mode, fake that we
8295 * didn't by injecting #UD.
8296 */
8297 if (!is_guest_mode(vcpu)) {
8298 kvm_queue_exception(vcpu, UD_VECTOR);
8299 return 1;
8300 }
8301
8302 vmcs12 = get_vmcs12(vcpu);
8303 if ((vmcs12->vm_function_control & (1 << function)) == 0)
8304 goto fail;
Bandan Das41ab9372017-08-03 15:54:43 -04008305
8306 switch (function) {
8307 case 0:
8308 if (nested_vmx_eptp_switching(vcpu, vmcs12))
8309 goto fail;
8310 break;
8311 default:
8312 goto fail;
8313 }
8314 return kvm_skip_emulated_instruction(vcpu);
Bandan Das27c42a12017-08-03 15:54:42 -04008315
8316fail:
8317 nested_vmx_vmexit(vcpu, vmx->exit_reason,
8318 vmcs_read32(VM_EXIT_INTR_INFO),
8319 vmcs_readl(EXIT_QUALIFICATION));
Bandan Das2a499e42017-08-03 15:54:41 -04008320 return 1;
8321}
8322
Nadav Har'El0140cae2011-05-25 23:06:28 +03008323/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08008324 * The exit handlers return 1 if the exit was handled fully and guest execution
8325 * may resume. Otherwise they set the kvm_run parameter to indicate what needs
8326 * to be done to userspace and return 0.
8327 */
Mathias Krause772e0312012-08-30 01:30:19 +02008328static int (*const kvm_vmx_exit_handlers[])(struct kvm_vcpu *vcpu) = {
Avi Kivity6aa8b732006-12-10 02:21:36 -08008329 [EXIT_REASON_EXCEPTION_NMI] = handle_exception,
8330 [EXIT_REASON_EXTERNAL_INTERRUPT] = handle_external_interrupt,
Avi Kivity988ad742007-02-12 00:54:36 -08008331 [EXIT_REASON_TRIPLE_FAULT] = handle_triple_fault,
Sheng Yangf08864b2008-05-15 18:23:25 +08008332 [EXIT_REASON_NMI_WINDOW] = handle_nmi_window,
Avi Kivity6aa8b732006-12-10 02:21:36 -08008333 [EXIT_REASON_IO_INSTRUCTION] = handle_io,
Avi Kivity6aa8b732006-12-10 02:21:36 -08008334 [EXIT_REASON_CR_ACCESS] = handle_cr,
8335 [EXIT_REASON_DR_ACCESS] = handle_dr,
8336 [EXIT_REASON_CPUID] = handle_cpuid,
8337 [EXIT_REASON_MSR_READ] = handle_rdmsr,
8338 [EXIT_REASON_MSR_WRITE] = handle_wrmsr,
8339 [EXIT_REASON_PENDING_INTERRUPT] = handle_interrupt_window,
8340 [EXIT_REASON_HLT] = handle_halt,
Gleb Natapovec25d5e2010-11-01 15:35:01 +02008341 [EXIT_REASON_INVD] = handle_invd,
Marcelo Tosattia7052892008-09-23 13:18:35 -03008342 [EXIT_REASON_INVLPG] = handle_invlpg,
Avi Kivityfee84b02011-11-10 14:57:25 +02008343 [EXIT_REASON_RDPMC] = handle_rdpmc,
Ingo Molnarc21415e2007-02-19 14:37:47 +02008344 [EXIT_REASON_VMCALL] = handle_vmcall,
Nadav Har'El27d6c862011-05-25 23:06:59 +03008345 [EXIT_REASON_VMCLEAR] = handle_vmclear,
Nadav Har'Elcd232ad2011-05-25 23:10:33 +03008346 [EXIT_REASON_VMLAUNCH] = handle_vmlaunch,
Nadav Har'El63846662011-05-25 23:07:29 +03008347 [EXIT_REASON_VMPTRLD] = handle_vmptrld,
Nadav Har'El6a4d7552011-05-25 23:08:00 +03008348 [EXIT_REASON_VMPTRST] = handle_vmptrst,
Nadav Har'El49f705c2011-05-25 23:08:30 +03008349 [EXIT_REASON_VMREAD] = handle_vmread,
Nadav Har'Elcd232ad2011-05-25 23:10:33 +03008350 [EXIT_REASON_VMRESUME] = handle_vmresume,
Nadav Har'El49f705c2011-05-25 23:08:30 +03008351 [EXIT_REASON_VMWRITE] = handle_vmwrite,
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008352 [EXIT_REASON_VMOFF] = handle_vmoff,
8353 [EXIT_REASON_VMON] = handle_vmon,
Sheng Yangf78e0e22007-10-29 09:40:42 +08008354 [EXIT_REASON_TPR_BELOW_THRESHOLD] = handle_tpr_below_threshold,
8355 [EXIT_REASON_APIC_ACCESS] = handle_apic_access,
Yang Zhang83d4c282013-01-25 10:18:49 +08008356 [EXIT_REASON_APIC_WRITE] = handle_apic_write,
Yang Zhangc7c9c562013-01-25 10:18:51 +08008357 [EXIT_REASON_EOI_INDUCED] = handle_apic_eoi_induced,
Eddie Donge5edaa02007-11-11 12:28:35 +02008358 [EXIT_REASON_WBINVD] = handle_wbinvd,
Dexuan Cui2acf9232010-06-10 11:27:12 +08008359 [EXIT_REASON_XSETBV] = handle_xsetbv,
Izik Eidus37817f22008-03-24 23:14:53 +02008360 [EXIT_REASON_TASK_SWITCH] = handle_task_switch,
Andi Kleena0861c02009-06-08 17:37:09 +08008361 [EXIT_REASON_MCE_DURING_VMENTRY] = handle_machine_check,
Paolo Bonzini0367f202016-07-12 10:44:55 +02008362 [EXIT_REASON_GDTR_IDTR] = handle_desc,
8363 [EXIT_REASON_LDTR_TR] = handle_desc,
Marcelo Tosatti68f89402009-06-11 12:07:43 -03008364 [EXIT_REASON_EPT_VIOLATION] = handle_ept_violation,
8365 [EXIT_REASON_EPT_MISCONFIG] = handle_ept_misconfig,
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08008366 [EXIT_REASON_PAUSE_INSTRUCTION] = handle_pause,
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04008367 [EXIT_REASON_MWAIT_INSTRUCTION] = handle_mwait,
Mihai Donțu5f3d45e2015-07-05 20:08:57 +03008368 [EXIT_REASON_MONITOR_TRAP_FLAG] = handle_monitor_trap,
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04008369 [EXIT_REASON_MONITOR_INSTRUCTION] = handle_monitor,
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008370 [EXIT_REASON_INVEPT] = handle_invept,
Petr Matouseka642fc32014-09-23 20:22:30 +02008371 [EXIT_REASON_INVVPID] = handle_invvpid,
Jim Mattson45ec3682017-08-23 16:32:04 -07008372 [EXIT_REASON_RDRAND] = handle_invalid_op,
Jim Mattson75f4fc82017-08-23 16:32:03 -07008373 [EXIT_REASON_RDSEED] = handle_invalid_op,
Wanpeng Lif53cd632014-12-02 19:14:58 +08008374 [EXIT_REASON_XSAVES] = handle_xsaves,
8375 [EXIT_REASON_XRSTORS] = handle_xrstors,
Kai Huang843e4332015-01-28 10:54:28 +08008376 [EXIT_REASON_PML_FULL] = handle_pml_full,
Bandan Das2a499e42017-08-03 15:54:41 -04008377 [EXIT_REASON_VMFUNC] = handle_vmfunc,
Yunhong Jiang64672c92016-06-13 14:19:59 -07008378 [EXIT_REASON_PREEMPTION_TIMER] = handle_preemption_timer,
Avi Kivity6aa8b732006-12-10 02:21:36 -08008379};
8380
8381static const int kvm_vmx_max_exit_handlers =
Robert P. J. Day50a34852007-06-03 13:35:29 -04008382 ARRAY_SIZE(kvm_vmx_exit_handlers);
Avi Kivity6aa8b732006-12-10 02:21:36 -08008383
Jan Kiszka908a7bd2013-02-18 11:21:16 +01008384static bool nested_vmx_exit_handled_io(struct kvm_vcpu *vcpu,
8385 struct vmcs12 *vmcs12)
8386{
8387 unsigned long exit_qualification;
8388 gpa_t bitmap, last_bitmap;
8389 unsigned int port;
8390 int size;
8391 u8 b;
8392
Jan Kiszka908a7bd2013-02-18 11:21:16 +01008393 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_IO_BITMAPS))
Zhihui Zhang2f0a6392013-12-30 15:56:29 -05008394 return nested_cpu_has(vmcs12, CPU_BASED_UNCOND_IO_EXITING);
Jan Kiszka908a7bd2013-02-18 11:21:16 +01008395
8396 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
8397
8398 port = exit_qualification >> 16;
8399 size = (exit_qualification & 7) + 1;
8400
8401 last_bitmap = (gpa_t)-1;
8402 b = -1;
8403
8404 while (size > 0) {
8405 if (port < 0x8000)
8406 bitmap = vmcs12->io_bitmap_a;
8407 else if (port < 0x10000)
8408 bitmap = vmcs12->io_bitmap_b;
8409 else
Joe Perches1d804d02015-03-30 16:46:09 -07008410 return true;
Jan Kiszka908a7bd2013-02-18 11:21:16 +01008411 bitmap += (port & 0x7fff) / 8;
8412
8413 if (last_bitmap != bitmap)
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02008414 if (kvm_vcpu_read_guest(vcpu, bitmap, &b, 1))
Joe Perches1d804d02015-03-30 16:46:09 -07008415 return true;
Jan Kiszka908a7bd2013-02-18 11:21:16 +01008416 if (b & (1 << (port & 7)))
Joe Perches1d804d02015-03-30 16:46:09 -07008417 return true;
Jan Kiszka908a7bd2013-02-18 11:21:16 +01008418
8419 port++;
8420 size--;
8421 last_bitmap = bitmap;
8422 }
8423
Joe Perches1d804d02015-03-30 16:46:09 -07008424 return false;
Jan Kiszka908a7bd2013-02-18 11:21:16 +01008425}
8426
Nadav Har'El644d7112011-05-25 23:12:35 +03008427/*
8428 * Return 1 if we should exit from L2 to L1 to handle an MSR access access,
8429 * rather than handle it ourselves in L0. I.e., check whether L1 expressed
8430 * disinterest in the current event (read or write a specific MSR) by using an
8431 * MSR bitmap. This may be the case even when L0 doesn't use MSR bitmaps.
8432 */
8433static bool nested_vmx_exit_handled_msr(struct kvm_vcpu *vcpu,
8434 struct vmcs12 *vmcs12, u32 exit_reason)
8435{
8436 u32 msr_index = vcpu->arch.regs[VCPU_REGS_RCX];
8437 gpa_t bitmap;
8438
Jan Kiszkacbd29cb2013-02-11 12:19:28 +01008439 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS))
Joe Perches1d804d02015-03-30 16:46:09 -07008440 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008441
8442 /*
8443 * The MSR_BITMAP page is divided into four 1024-byte bitmaps,
8444 * for the four combinations of read/write and low/high MSR numbers.
8445 * First we need to figure out which of the four to use:
8446 */
8447 bitmap = vmcs12->msr_bitmap;
8448 if (exit_reason == EXIT_REASON_MSR_WRITE)
8449 bitmap += 2048;
8450 if (msr_index >= 0xc0000000) {
8451 msr_index -= 0xc0000000;
8452 bitmap += 1024;
8453 }
8454
8455 /* Then read the msr_index'th bit from this bitmap: */
8456 if (msr_index < 1024*8) {
8457 unsigned char b;
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02008458 if (kvm_vcpu_read_guest(vcpu, bitmap + msr_index/8, &b, 1))
Joe Perches1d804d02015-03-30 16:46:09 -07008459 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008460 return 1 & (b >> (msr_index & 7));
8461 } else
Joe Perches1d804d02015-03-30 16:46:09 -07008462 return true; /* let L1 handle the wrong parameter */
Nadav Har'El644d7112011-05-25 23:12:35 +03008463}
8464
8465/*
8466 * Return 1 if we should exit from L2 to L1 to handle a CR access exit,
8467 * rather than handle it ourselves in L0. I.e., check if L1 wanted to
8468 * intercept (via guest_host_mask etc.) the current event.
8469 */
8470static bool nested_vmx_exit_handled_cr(struct kvm_vcpu *vcpu,
8471 struct vmcs12 *vmcs12)
8472{
8473 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
8474 int cr = exit_qualification & 15;
Jan H. Schönherre1d39b12017-05-20 13:22:56 +02008475 int reg;
8476 unsigned long val;
Nadav Har'El644d7112011-05-25 23:12:35 +03008477
8478 switch ((exit_qualification >> 4) & 3) {
8479 case 0: /* mov to cr */
Jan H. Schönherre1d39b12017-05-20 13:22:56 +02008480 reg = (exit_qualification >> 8) & 15;
8481 val = kvm_register_readl(vcpu, reg);
Nadav Har'El644d7112011-05-25 23:12:35 +03008482 switch (cr) {
8483 case 0:
8484 if (vmcs12->cr0_guest_host_mask &
8485 (val ^ vmcs12->cr0_read_shadow))
Joe Perches1d804d02015-03-30 16:46:09 -07008486 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008487 break;
8488 case 3:
8489 if ((vmcs12->cr3_target_count >= 1 &&
8490 vmcs12->cr3_target_value0 == val) ||
8491 (vmcs12->cr3_target_count >= 2 &&
8492 vmcs12->cr3_target_value1 == val) ||
8493 (vmcs12->cr3_target_count >= 3 &&
8494 vmcs12->cr3_target_value2 == val) ||
8495 (vmcs12->cr3_target_count >= 4 &&
8496 vmcs12->cr3_target_value3 == val))
Joe Perches1d804d02015-03-30 16:46:09 -07008497 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03008498 if (nested_cpu_has(vmcs12, CPU_BASED_CR3_LOAD_EXITING))
Joe Perches1d804d02015-03-30 16:46:09 -07008499 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008500 break;
8501 case 4:
8502 if (vmcs12->cr4_guest_host_mask &
8503 (vmcs12->cr4_read_shadow ^ val))
Joe Perches1d804d02015-03-30 16:46:09 -07008504 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008505 break;
8506 case 8:
8507 if (nested_cpu_has(vmcs12, CPU_BASED_CR8_LOAD_EXITING))
Joe Perches1d804d02015-03-30 16:46:09 -07008508 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008509 break;
8510 }
8511 break;
8512 case 2: /* clts */
8513 if ((vmcs12->cr0_guest_host_mask & X86_CR0_TS) &&
8514 (vmcs12->cr0_read_shadow & X86_CR0_TS))
Joe Perches1d804d02015-03-30 16:46:09 -07008515 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008516 break;
8517 case 1: /* mov from cr */
8518 switch (cr) {
8519 case 3:
8520 if (vmcs12->cpu_based_vm_exec_control &
8521 CPU_BASED_CR3_STORE_EXITING)
Joe Perches1d804d02015-03-30 16:46:09 -07008522 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008523 break;
8524 case 8:
8525 if (vmcs12->cpu_based_vm_exec_control &
8526 CPU_BASED_CR8_STORE_EXITING)
Joe Perches1d804d02015-03-30 16:46:09 -07008527 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008528 break;
8529 }
8530 break;
8531 case 3: /* lmsw */
8532 /*
8533 * lmsw can change bits 1..3 of cr0, and only set bit 0 of
8534 * cr0. Other attempted changes are ignored, with no exit.
8535 */
Jan H. Schönherre1d39b12017-05-20 13:22:56 +02008536 val = (exit_qualification >> LMSW_SOURCE_DATA_SHIFT) & 0x0f;
Nadav Har'El644d7112011-05-25 23:12:35 +03008537 if (vmcs12->cr0_guest_host_mask & 0xe &
8538 (val ^ vmcs12->cr0_read_shadow))
Joe Perches1d804d02015-03-30 16:46:09 -07008539 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008540 if ((vmcs12->cr0_guest_host_mask & 0x1) &&
8541 !(vmcs12->cr0_read_shadow & 0x1) &&
8542 (val & 0x1))
Joe Perches1d804d02015-03-30 16:46:09 -07008543 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008544 break;
8545 }
Joe Perches1d804d02015-03-30 16:46:09 -07008546 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03008547}
8548
8549/*
8550 * Return 1 if we should exit from L2 to L1 to handle an exit, or 0 if we
8551 * should handle it ourselves in L0 (and then continue L2). Only call this
8552 * when in is_guest_mode (L2).
8553 */
Paolo Bonzini7313c692017-07-27 10:31:25 +02008554static bool nested_vmx_exit_reflected(struct kvm_vcpu *vcpu, u32 exit_reason)
Nadav Har'El644d7112011-05-25 23:12:35 +03008555{
Nadav Har'El644d7112011-05-25 23:12:35 +03008556 u32 intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
8557 struct vcpu_vmx *vmx = to_vmx(vcpu);
8558 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
8559
Jim Mattson4f350c62017-09-14 16:31:44 -07008560 if (vmx->nested.nested_run_pending)
8561 return false;
8562
8563 if (unlikely(vmx->fail)) {
8564 pr_info_ratelimited("%s failed vm entry %x\n", __func__,
8565 vmcs_read32(VM_INSTRUCTION_ERROR));
8566 return true;
8567 }
Jan Kiszka542060e2014-01-04 18:47:21 +01008568
David Matlackc9f04402017-08-01 14:00:40 -07008569 /*
8570 * The host physical addresses of some pages of guest memory
Jim Mattsonde3a0022017-11-27 17:22:25 -06008571 * are loaded into the vmcs02 (e.g. vmcs12's Virtual APIC
8572 * Page). The CPU may write to these pages via their host
8573 * physical address while L2 is running, bypassing any
8574 * address-translation-based dirty tracking (e.g. EPT write
8575 * protection).
David Matlackc9f04402017-08-01 14:00:40 -07008576 *
8577 * Mark them dirty on every exit from L2 to prevent them from
8578 * getting out of sync with dirty tracking.
8579 */
8580 nested_mark_vmcs12_pages_dirty(vcpu);
8581
Jim Mattson4f350c62017-09-14 16:31:44 -07008582 trace_kvm_nested_vmexit(kvm_rip_read(vcpu), exit_reason,
8583 vmcs_readl(EXIT_QUALIFICATION),
8584 vmx->idt_vectoring_info,
8585 intr_info,
8586 vmcs_read32(VM_EXIT_INTR_ERROR_CODE),
8587 KVM_ISA_VMX);
Nadav Har'El644d7112011-05-25 23:12:35 +03008588
8589 switch (exit_reason) {
8590 case EXIT_REASON_EXCEPTION_NMI:
Jim Mattsonef85b672016-12-12 11:01:37 -08008591 if (is_nmi(intr_info))
Joe Perches1d804d02015-03-30 16:46:09 -07008592 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03008593 else if (is_page_fault(intr_info))
Wanpeng Li52a5c152017-07-13 18:30:42 -07008594 return !vmx->vcpu.arch.apf.host_apf_reason && enable_ept;
Anthoine Bourgeoise504c902013-11-13 11:45:37 +01008595 else if (is_no_device(intr_info) &&
Paolo Bonziniccf98442014-02-27 22:54:11 +01008596 !(vmcs12->guest_cr0 & X86_CR0_TS))
Joe Perches1d804d02015-03-30 16:46:09 -07008597 return false;
Jan Kiszka6f054852016-02-09 20:15:18 +01008598 else if (is_debug(intr_info) &&
8599 vcpu->guest_debug &
8600 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))
8601 return false;
8602 else if (is_breakpoint(intr_info) &&
8603 vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
8604 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03008605 return vmcs12->exception_bitmap &
8606 (1u << (intr_info & INTR_INFO_VECTOR_MASK));
8607 case EXIT_REASON_EXTERNAL_INTERRUPT:
Joe Perches1d804d02015-03-30 16:46:09 -07008608 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03008609 case EXIT_REASON_TRIPLE_FAULT:
Joe Perches1d804d02015-03-30 16:46:09 -07008610 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008611 case EXIT_REASON_PENDING_INTERRUPT:
Jan Kiszka3b656cf2013-04-14 12:12:45 +02008612 return nested_cpu_has(vmcs12, CPU_BASED_VIRTUAL_INTR_PENDING);
Nadav Har'El644d7112011-05-25 23:12:35 +03008613 case EXIT_REASON_NMI_WINDOW:
Jan Kiszka3b656cf2013-04-14 12:12:45 +02008614 return nested_cpu_has(vmcs12, CPU_BASED_VIRTUAL_NMI_PENDING);
Nadav Har'El644d7112011-05-25 23:12:35 +03008615 case EXIT_REASON_TASK_SWITCH:
Joe Perches1d804d02015-03-30 16:46:09 -07008616 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008617 case EXIT_REASON_CPUID:
Joe Perches1d804d02015-03-30 16:46:09 -07008618 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008619 case EXIT_REASON_HLT:
8620 return nested_cpu_has(vmcs12, CPU_BASED_HLT_EXITING);
8621 case EXIT_REASON_INVD:
Joe Perches1d804d02015-03-30 16:46:09 -07008622 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008623 case EXIT_REASON_INVLPG:
8624 return nested_cpu_has(vmcs12, CPU_BASED_INVLPG_EXITING);
8625 case EXIT_REASON_RDPMC:
8626 return nested_cpu_has(vmcs12, CPU_BASED_RDPMC_EXITING);
Paolo Bonzinia5f46452017-03-30 11:55:32 +02008627 case EXIT_REASON_RDRAND:
David Hildenbrand736fdf72017-08-24 20:51:37 +02008628 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_RDRAND_EXITING);
Paolo Bonzinia5f46452017-03-30 11:55:32 +02008629 case EXIT_REASON_RDSEED:
David Hildenbrand736fdf72017-08-24 20:51:37 +02008630 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_RDSEED_EXITING);
Jan Kiszkab3a2a902015-03-23 19:27:19 +01008631 case EXIT_REASON_RDTSC: case EXIT_REASON_RDTSCP:
Nadav Har'El644d7112011-05-25 23:12:35 +03008632 return nested_cpu_has(vmcs12, CPU_BASED_RDTSC_EXITING);
8633 case EXIT_REASON_VMCALL: case EXIT_REASON_VMCLEAR:
8634 case EXIT_REASON_VMLAUNCH: case EXIT_REASON_VMPTRLD:
8635 case EXIT_REASON_VMPTRST: case EXIT_REASON_VMREAD:
8636 case EXIT_REASON_VMRESUME: case EXIT_REASON_VMWRITE:
8637 case EXIT_REASON_VMOFF: case EXIT_REASON_VMON:
Petr Matouseka642fc32014-09-23 20:22:30 +02008638 case EXIT_REASON_INVEPT: case EXIT_REASON_INVVPID:
Nadav Har'El644d7112011-05-25 23:12:35 +03008639 /*
8640 * VMX instructions trap unconditionally. This allows L1 to
8641 * emulate them for its L2 guest, i.e., allows 3-level nesting!
8642 */
Joe Perches1d804d02015-03-30 16:46:09 -07008643 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008644 case EXIT_REASON_CR_ACCESS:
8645 return nested_vmx_exit_handled_cr(vcpu, vmcs12);
8646 case EXIT_REASON_DR_ACCESS:
8647 return nested_cpu_has(vmcs12, CPU_BASED_MOV_DR_EXITING);
8648 case EXIT_REASON_IO_INSTRUCTION:
Jan Kiszka908a7bd2013-02-18 11:21:16 +01008649 return nested_vmx_exit_handled_io(vcpu, vmcs12);
Paolo Bonzini1b073042016-10-25 16:06:30 +02008650 case EXIT_REASON_GDTR_IDTR: case EXIT_REASON_LDTR_TR:
8651 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_DESC);
Nadav Har'El644d7112011-05-25 23:12:35 +03008652 case EXIT_REASON_MSR_READ:
8653 case EXIT_REASON_MSR_WRITE:
8654 return nested_vmx_exit_handled_msr(vcpu, vmcs12, exit_reason);
8655 case EXIT_REASON_INVALID_STATE:
Joe Perches1d804d02015-03-30 16:46:09 -07008656 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008657 case EXIT_REASON_MWAIT_INSTRUCTION:
8658 return nested_cpu_has(vmcs12, CPU_BASED_MWAIT_EXITING);
Mihai Donțu5f3d45e2015-07-05 20:08:57 +03008659 case EXIT_REASON_MONITOR_TRAP_FLAG:
8660 return nested_cpu_has(vmcs12, CPU_BASED_MONITOR_TRAP_FLAG);
Nadav Har'El644d7112011-05-25 23:12:35 +03008661 case EXIT_REASON_MONITOR_INSTRUCTION:
8662 return nested_cpu_has(vmcs12, CPU_BASED_MONITOR_EXITING);
8663 case EXIT_REASON_PAUSE_INSTRUCTION:
8664 return nested_cpu_has(vmcs12, CPU_BASED_PAUSE_EXITING) ||
8665 nested_cpu_has2(vmcs12,
8666 SECONDARY_EXEC_PAUSE_LOOP_EXITING);
8667 case EXIT_REASON_MCE_DURING_VMENTRY:
Joe Perches1d804d02015-03-30 16:46:09 -07008668 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03008669 case EXIT_REASON_TPR_BELOW_THRESHOLD:
Wanpeng Lia7c0b072014-08-21 19:46:50 +08008670 return nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW);
Nadav Har'El644d7112011-05-25 23:12:35 +03008671 case EXIT_REASON_APIC_ACCESS:
8672 return nested_cpu_has2(vmcs12,
8673 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES);
Wincy Van82f0dd42015-02-03 23:57:18 +08008674 case EXIT_REASON_APIC_WRITE:
Wincy Van608406e2015-02-03 23:57:51 +08008675 case EXIT_REASON_EOI_INDUCED:
8676 /* apic_write and eoi_induced should exit unconditionally. */
Joe Perches1d804d02015-03-30 16:46:09 -07008677 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008678 case EXIT_REASON_EPT_VIOLATION:
Nadav Har'El2b1be672013-08-05 11:07:19 +03008679 /*
8680 * L0 always deals with the EPT violation. If nested EPT is
8681 * used, and the nested mmu code discovers that the address is
8682 * missing in the guest EPT table (EPT12), the EPT violation
8683 * will be injected with nested_ept_inject_page_fault()
8684 */
Joe Perches1d804d02015-03-30 16:46:09 -07008685 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03008686 case EXIT_REASON_EPT_MISCONFIG:
Nadav Har'El2b1be672013-08-05 11:07:19 +03008687 /*
8688 * L2 never uses directly L1's EPT, but rather L0's own EPT
8689 * table (shadow on EPT) or a merged EPT table that L0 built
8690 * (EPT on EPT). So any problems with the structure of the
8691 * table is L0's fault.
8692 */
Joe Perches1d804d02015-03-30 16:46:09 -07008693 return false;
Paolo Bonzini90a2db62017-07-27 13:22:13 +02008694 case EXIT_REASON_INVPCID:
8695 return
8696 nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_INVPCID) &&
8697 nested_cpu_has(vmcs12, CPU_BASED_INVLPG_EXITING);
Nadav Har'El644d7112011-05-25 23:12:35 +03008698 case EXIT_REASON_WBINVD:
8699 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_WBINVD_EXITING);
8700 case EXIT_REASON_XSETBV:
Joe Perches1d804d02015-03-30 16:46:09 -07008701 return true;
Wanpeng Li81dc01f2014-12-04 19:11:07 +08008702 case EXIT_REASON_XSAVES: case EXIT_REASON_XRSTORS:
8703 /*
8704 * This should never happen, since it is not possible to
8705 * set XSS to a non-zero value---neither in L1 nor in L2.
8706 * If if it were, XSS would have to be checked against
8707 * the XSS exit bitmap in vmcs12.
8708 */
8709 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_XSAVES);
Wanpeng Li55123e32016-07-06 18:29:58 +08008710 case EXIT_REASON_PREEMPTION_TIMER:
8711 return false;
Ladi Prosekab007cc2017-03-31 10:19:26 +02008712 case EXIT_REASON_PML_FULL:
Bandan Das03efce62017-05-05 15:25:15 -04008713 /* We emulate PML support to L1. */
Ladi Prosekab007cc2017-03-31 10:19:26 +02008714 return false;
Bandan Das2a499e42017-08-03 15:54:41 -04008715 case EXIT_REASON_VMFUNC:
8716 /* VM functions are emulated through L2->L0 vmexits. */
8717 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03008718 default:
Joe Perches1d804d02015-03-30 16:46:09 -07008719 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008720 }
8721}
8722
Paolo Bonzini7313c692017-07-27 10:31:25 +02008723static int nested_vmx_reflect_vmexit(struct kvm_vcpu *vcpu, u32 exit_reason)
8724{
8725 u32 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
8726
8727 /*
8728 * At this point, the exit interruption info in exit_intr_info
8729 * is only valid for EXCEPTION_NMI exits. For EXTERNAL_INTERRUPT
8730 * we need to query the in-kernel LAPIC.
8731 */
8732 WARN_ON(exit_reason == EXIT_REASON_EXTERNAL_INTERRUPT);
8733 if ((exit_intr_info &
8734 (INTR_INFO_VALID_MASK | INTR_INFO_DELIVER_CODE_MASK)) ==
8735 (INTR_INFO_VALID_MASK | INTR_INFO_DELIVER_CODE_MASK)) {
8736 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
8737 vmcs12->vm_exit_intr_error_code =
8738 vmcs_read32(VM_EXIT_INTR_ERROR_CODE);
8739 }
8740
8741 nested_vmx_vmexit(vcpu, exit_reason, exit_intr_info,
8742 vmcs_readl(EXIT_QUALIFICATION));
8743 return 1;
8744}
8745
Avi Kivity586f9602010-11-18 13:09:54 +02008746static void vmx_get_exit_info(struct kvm_vcpu *vcpu, u64 *info1, u64 *info2)
8747{
8748 *info1 = vmcs_readl(EXIT_QUALIFICATION);
8749 *info2 = vmcs_read32(VM_EXIT_INTR_INFO);
8750}
8751
Kai Huanga3eaa862015-11-04 13:46:05 +08008752static void vmx_destroy_pml_buffer(struct vcpu_vmx *vmx)
Kai Huang843e4332015-01-28 10:54:28 +08008753{
Kai Huanga3eaa862015-11-04 13:46:05 +08008754 if (vmx->pml_pg) {
8755 __free_page(vmx->pml_pg);
8756 vmx->pml_pg = NULL;
8757 }
Kai Huang843e4332015-01-28 10:54:28 +08008758}
8759
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02008760static void vmx_flush_pml_buffer(struct kvm_vcpu *vcpu)
Kai Huang843e4332015-01-28 10:54:28 +08008761{
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02008762 struct vcpu_vmx *vmx = to_vmx(vcpu);
Kai Huang843e4332015-01-28 10:54:28 +08008763 u64 *pml_buf;
8764 u16 pml_idx;
8765
8766 pml_idx = vmcs_read16(GUEST_PML_INDEX);
8767
8768 /* Do nothing if PML buffer is empty */
8769 if (pml_idx == (PML_ENTITY_NUM - 1))
8770 return;
8771
8772 /* PML index always points to next available PML buffer entity */
8773 if (pml_idx >= PML_ENTITY_NUM)
8774 pml_idx = 0;
8775 else
8776 pml_idx++;
8777
8778 pml_buf = page_address(vmx->pml_pg);
8779 for (; pml_idx < PML_ENTITY_NUM; pml_idx++) {
8780 u64 gpa;
8781
8782 gpa = pml_buf[pml_idx];
8783 WARN_ON(gpa & (PAGE_SIZE - 1));
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02008784 kvm_vcpu_mark_page_dirty(vcpu, gpa >> PAGE_SHIFT);
Kai Huang843e4332015-01-28 10:54:28 +08008785 }
8786
8787 /* reset PML index */
8788 vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1);
8789}
8790
8791/*
8792 * Flush all vcpus' PML buffer and update logged GPAs to dirty_bitmap.
8793 * Called before reporting dirty_bitmap to userspace.
8794 */
8795static void kvm_flush_pml_buffers(struct kvm *kvm)
8796{
8797 int i;
8798 struct kvm_vcpu *vcpu;
8799 /*
8800 * We only need to kick vcpu out of guest mode here, as PML buffer
8801 * is flushed at beginning of all VMEXITs, and it's obvious that only
8802 * vcpus running in guest are possible to have unflushed GPAs in PML
8803 * buffer.
8804 */
8805 kvm_for_each_vcpu(i, vcpu, kvm)
8806 kvm_vcpu_kick(vcpu);
8807}
8808
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02008809static void vmx_dump_sel(char *name, uint32_t sel)
8810{
8811 pr_err("%s sel=0x%04x, attr=0x%05x, limit=0x%08x, base=0x%016lx\n",
Chao Peng96794e42017-02-21 03:50:01 -05008812 name, vmcs_read16(sel),
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02008813 vmcs_read32(sel + GUEST_ES_AR_BYTES - GUEST_ES_SELECTOR),
8814 vmcs_read32(sel + GUEST_ES_LIMIT - GUEST_ES_SELECTOR),
8815 vmcs_readl(sel + GUEST_ES_BASE - GUEST_ES_SELECTOR));
8816}
8817
8818static void vmx_dump_dtsel(char *name, uint32_t limit)
8819{
8820 pr_err("%s limit=0x%08x, base=0x%016lx\n",
8821 name, vmcs_read32(limit),
8822 vmcs_readl(limit + GUEST_GDTR_BASE - GUEST_GDTR_LIMIT));
8823}
8824
8825static void dump_vmcs(void)
8826{
8827 u32 vmentry_ctl = vmcs_read32(VM_ENTRY_CONTROLS);
8828 u32 vmexit_ctl = vmcs_read32(VM_EXIT_CONTROLS);
8829 u32 cpu_based_exec_ctrl = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
8830 u32 pin_based_exec_ctrl = vmcs_read32(PIN_BASED_VM_EXEC_CONTROL);
8831 u32 secondary_exec_control = 0;
8832 unsigned long cr4 = vmcs_readl(GUEST_CR4);
Paolo Bonzinif3531052015-12-03 15:49:56 +01008833 u64 efer = vmcs_read64(GUEST_IA32_EFER);
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02008834 int i, n;
8835
8836 if (cpu_has_secondary_exec_ctrls())
8837 secondary_exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
8838
8839 pr_err("*** Guest State ***\n");
8840 pr_err("CR0: actual=0x%016lx, shadow=0x%016lx, gh_mask=%016lx\n",
8841 vmcs_readl(GUEST_CR0), vmcs_readl(CR0_READ_SHADOW),
8842 vmcs_readl(CR0_GUEST_HOST_MASK));
8843 pr_err("CR4: actual=0x%016lx, shadow=0x%016lx, gh_mask=%016lx\n",
8844 cr4, vmcs_readl(CR4_READ_SHADOW), vmcs_readl(CR4_GUEST_HOST_MASK));
8845 pr_err("CR3 = 0x%016lx\n", vmcs_readl(GUEST_CR3));
8846 if ((secondary_exec_control & SECONDARY_EXEC_ENABLE_EPT) &&
8847 (cr4 & X86_CR4_PAE) && !(efer & EFER_LMA))
8848 {
Paolo Bonzini845c5b402015-12-03 15:51:00 +01008849 pr_err("PDPTR0 = 0x%016llx PDPTR1 = 0x%016llx\n",
8850 vmcs_read64(GUEST_PDPTR0), vmcs_read64(GUEST_PDPTR1));
8851 pr_err("PDPTR2 = 0x%016llx PDPTR3 = 0x%016llx\n",
8852 vmcs_read64(GUEST_PDPTR2), vmcs_read64(GUEST_PDPTR3));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02008853 }
8854 pr_err("RSP = 0x%016lx RIP = 0x%016lx\n",
8855 vmcs_readl(GUEST_RSP), vmcs_readl(GUEST_RIP));
8856 pr_err("RFLAGS=0x%08lx DR7 = 0x%016lx\n",
8857 vmcs_readl(GUEST_RFLAGS), vmcs_readl(GUEST_DR7));
8858 pr_err("Sysenter RSP=%016lx CS:RIP=%04x:%016lx\n",
8859 vmcs_readl(GUEST_SYSENTER_ESP),
8860 vmcs_read32(GUEST_SYSENTER_CS), vmcs_readl(GUEST_SYSENTER_EIP));
8861 vmx_dump_sel("CS: ", GUEST_CS_SELECTOR);
8862 vmx_dump_sel("DS: ", GUEST_DS_SELECTOR);
8863 vmx_dump_sel("SS: ", GUEST_SS_SELECTOR);
8864 vmx_dump_sel("ES: ", GUEST_ES_SELECTOR);
8865 vmx_dump_sel("FS: ", GUEST_FS_SELECTOR);
8866 vmx_dump_sel("GS: ", GUEST_GS_SELECTOR);
8867 vmx_dump_dtsel("GDTR:", GUEST_GDTR_LIMIT);
8868 vmx_dump_sel("LDTR:", GUEST_LDTR_SELECTOR);
8869 vmx_dump_dtsel("IDTR:", GUEST_IDTR_LIMIT);
8870 vmx_dump_sel("TR: ", GUEST_TR_SELECTOR);
8871 if ((vmexit_ctl & (VM_EXIT_SAVE_IA32_PAT | VM_EXIT_SAVE_IA32_EFER)) ||
8872 (vmentry_ctl & (VM_ENTRY_LOAD_IA32_PAT | VM_ENTRY_LOAD_IA32_EFER)))
Paolo Bonzini845c5b402015-12-03 15:51:00 +01008873 pr_err("EFER = 0x%016llx PAT = 0x%016llx\n",
8874 efer, vmcs_read64(GUEST_IA32_PAT));
8875 pr_err("DebugCtl = 0x%016llx DebugExceptions = 0x%016lx\n",
8876 vmcs_read64(GUEST_IA32_DEBUGCTL),
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02008877 vmcs_readl(GUEST_PENDING_DBG_EXCEPTIONS));
8878 if (vmentry_ctl & VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL)
Paolo Bonzini845c5b402015-12-03 15:51:00 +01008879 pr_err("PerfGlobCtl = 0x%016llx\n",
8880 vmcs_read64(GUEST_IA32_PERF_GLOBAL_CTRL));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02008881 if (vmentry_ctl & VM_ENTRY_LOAD_BNDCFGS)
Paolo Bonzini845c5b402015-12-03 15:51:00 +01008882 pr_err("BndCfgS = 0x%016llx\n", vmcs_read64(GUEST_BNDCFGS));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02008883 pr_err("Interruptibility = %08x ActivityState = %08x\n",
8884 vmcs_read32(GUEST_INTERRUPTIBILITY_INFO),
8885 vmcs_read32(GUEST_ACTIVITY_STATE));
8886 if (secondary_exec_control & SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY)
8887 pr_err("InterruptStatus = %04x\n",
8888 vmcs_read16(GUEST_INTR_STATUS));
8889
8890 pr_err("*** Host State ***\n");
8891 pr_err("RIP = 0x%016lx RSP = 0x%016lx\n",
8892 vmcs_readl(HOST_RIP), vmcs_readl(HOST_RSP));
8893 pr_err("CS=%04x SS=%04x DS=%04x ES=%04x FS=%04x GS=%04x TR=%04x\n",
8894 vmcs_read16(HOST_CS_SELECTOR), vmcs_read16(HOST_SS_SELECTOR),
8895 vmcs_read16(HOST_DS_SELECTOR), vmcs_read16(HOST_ES_SELECTOR),
8896 vmcs_read16(HOST_FS_SELECTOR), vmcs_read16(HOST_GS_SELECTOR),
8897 vmcs_read16(HOST_TR_SELECTOR));
8898 pr_err("FSBase=%016lx GSBase=%016lx TRBase=%016lx\n",
8899 vmcs_readl(HOST_FS_BASE), vmcs_readl(HOST_GS_BASE),
8900 vmcs_readl(HOST_TR_BASE));
8901 pr_err("GDTBase=%016lx IDTBase=%016lx\n",
8902 vmcs_readl(HOST_GDTR_BASE), vmcs_readl(HOST_IDTR_BASE));
8903 pr_err("CR0=%016lx CR3=%016lx CR4=%016lx\n",
8904 vmcs_readl(HOST_CR0), vmcs_readl(HOST_CR3),
8905 vmcs_readl(HOST_CR4));
8906 pr_err("Sysenter RSP=%016lx CS:RIP=%04x:%016lx\n",
8907 vmcs_readl(HOST_IA32_SYSENTER_ESP),
8908 vmcs_read32(HOST_IA32_SYSENTER_CS),
8909 vmcs_readl(HOST_IA32_SYSENTER_EIP));
8910 if (vmexit_ctl & (VM_EXIT_LOAD_IA32_PAT | VM_EXIT_LOAD_IA32_EFER))
Paolo Bonzini845c5b402015-12-03 15:51:00 +01008911 pr_err("EFER = 0x%016llx PAT = 0x%016llx\n",
8912 vmcs_read64(HOST_IA32_EFER),
8913 vmcs_read64(HOST_IA32_PAT));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02008914 if (vmexit_ctl & VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL)
Paolo Bonzini845c5b402015-12-03 15:51:00 +01008915 pr_err("PerfGlobCtl = 0x%016llx\n",
8916 vmcs_read64(HOST_IA32_PERF_GLOBAL_CTRL));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02008917
8918 pr_err("*** Control State ***\n");
8919 pr_err("PinBased=%08x CPUBased=%08x SecondaryExec=%08x\n",
8920 pin_based_exec_ctrl, cpu_based_exec_ctrl, secondary_exec_control);
8921 pr_err("EntryControls=%08x ExitControls=%08x\n", vmentry_ctl, vmexit_ctl);
8922 pr_err("ExceptionBitmap=%08x PFECmask=%08x PFECmatch=%08x\n",
8923 vmcs_read32(EXCEPTION_BITMAP),
8924 vmcs_read32(PAGE_FAULT_ERROR_CODE_MASK),
8925 vmcs_read32(PAGE_FAULT_ERROR_CODE_MATCH));
8926 pr_err("VMEntry: intr_info=%08x errcode=%08x ilen=%08x\n",
8927 vmcs_read32(VM_ENTRY_INTR_INFO_FIELD),
8928 vmcs_read32(VM_ENTRY_EXCEPTION_ERROR_CODE),
8929 vmcs_read32(VM_ENTRY_INSTRUCTION_LEN));
8930 pr_err("VMExit: intr_info=%08x errcode=%08x ilen=%08x\n",
8931 vmcs_read32(VM_EXIT_INTR_INFO),
8932 vmcs_read32(VM_EXIT_INTR_ERROR_CODE),
8933 vmcs_read32(VM_EXIT_INSTRUCTION_LEN));
8934 pr_err(" reason=%08x qualification=%016lx\n",
8935 vmcs_read32(VM_EXIT_REASON), vmcs_readl(EXIT_QUALIFICATION));
8936 pr_err("IDTVectoring: info=%08x errcode=%08x\n",
8937 vmcs_read32(IDT_VECTORING_INFO_FIELD),
8938 vmcs_read32(IDT_VECTORING_ERROR_CODE));
Paolo Bonzini845c5b402015-12-03 15:51:00 +01008939 pr_err("TSC Offset = 0x%016llx\n", vmcs_read64(TSC_OFFSET));
Haozhong Zhang8cfe9862015-10-20 15:39:12 +08008940 if (secondary_exec_control & SECONDARY_EXEC_TSC_SCALING)
Paolo Bonzini845c5b402015-12-03 15:51:00 +01008941 pr_err("TSC Multiplier = 0x%016llx\n",
8942 vmcs_read64(TSC_MULTIPLIER));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02008943 if (cpu_based_exec_ctrl & CPU_BASED_TPR_SHADOW)
8944 pr_err("TPR Threshold = 0x%02x\n", vmcs_read32(TPR_THRESHOLD));
8945 if (pin_based_exec_ctrl & PIN_BASED_POSTED_INTR)
8946 pr_err("PostedIntrVec = 0x%02x\n", vmcs_read16(POSTED_INTR_NV));
8947 if ((secondary_exec_control & SECONDARY_EXEC_ENABLE_EPT))
Paolo Bonzini845c5b402015-12-03 15:51:00 +01008948 pr_err("EPT pointer = 0x%016llx\n", vmcs_read64(EPT_POINTER));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02008949 n = vmcs_read32(CR3_TARGET_COUNT);
8950 for (i = 0; i + 1 < n; i += 4)
8951 pr_err("CR3 target%u=%016lx target%u=%016lx\n",
8952 i, vmcs_readl(CR3_TARGET_VALUE0 + i * 2),
8953 i + 1, vmcs_readl(CR3_TARGET_VALUE0 + i * 2 + 2));
8954 if (i < n)
8955 pr_err("CR3 target%u=%016lx\n",
8956 i, vmcs_readl(CR3_TARGET_VALUE0 + i * 2));
8957 if (secondary_exec_control & SECONDARY_EXEC_PAUSE_LOOP_EXITING)
8958 pr_err("PLE Gap=%08x Window=%08x\n",
8959 vmcs_read32(PLE_GAP), vmcs_read32(PLE_WINDOW));
8960 if (secondary_exec_control & SECONDARY_EXEC_ENABLE_VPID)
8961 pr_err("Virtual processor ID = 0x%04x\n",
8962 vmcs_read16(VIRTUAL_PROCESSOR_ID));
8963}
8964
Avi Kivity6aa8b732006-12-10 02:21:36 -08008965/*
8966 * The guest has exited. See if we can fix it or if we need userspace
8967 * assistance.
8968 */
Avi Kivity851ba692009-08-24 11:10:17 +03008969static int vmx_handle_exit(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08008970{
Avi Kivity29bd8a72007-09-10 17:27:03 +03008971 struct vcpu_vmx *vmx = to_vmx(vcpu);
Andi Kleena0861c02009-06-08 17:37:09 +08008972 u32 exit_reason = vmx->exit_reason;
Avi Kivity1155f762007-11-22 11:30:47 +02008973 u32 vectoring_info = vmx->idt_vectoring_info;
Avi Kivity29bd8a72007-09-10 17:27:03 +03008974
Paolo Bonzini8b89fe12015-12-10 18:37:32 +01008975 trace_kvm_exit(exit_reason, vcpu, KVM_ISA_VMX);
8976
Kai Huang843e4332015-01-28 10:54:28 +08008977 /*
8978 * Flush logged GPAs PML buffer, this will make dirty_bitmap more
8979 * updated. Another good is, in kvm_vm_ioctl_get_dirty_log, before
8980 * querying dirty_bitmap, we only need to kick all vcpus out of guest
8981 * mode as if vcpus is in root mode, the PML buffer must has been
8982 * flushed already.
8983 */
8984 if (enable_pml)
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02008985 vmx_flush_pml_buffer(vcpu);
Kai Huang843e4332015-01-28 10:54:28 +08008986
Mohammed Gamal80ced182009-09-01 12:48:18 +02008987 /* If guest state is invalid, start emulating */
Gleb Natapov14168782013-01-21 15:36:49 +02008988 if (vmx->emulation_required)
Mohammed Gamal80ced182009-09-01 12:48:18 +02008989 return handle_invalid_guest_state(vcpu);
Guillaume Thouvenin1d5a4d92008-10-29 09:39:42 +01008990
Paolo Bonzini7313c692017-07-27 10:31:25 +02008991 if (is_guest_mode(vcpu) && nested_vmx_exit_reflected(vcpu, exit_reason))
8992 return nested_vmx_reflect_vmexit(vcpu, exit_reason);
Nadav Har'El644d7112011-05-25 23:12:35 +03008993
Mohammed Gamal51207022010-05-31 22:40:54 +03008994 if (exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY) {
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02008995 dump_vmcs();
Mohammed Gamal51207022010-05-31 22:40:54 +03008996 vcpu->run->exit_reason = KVM_EXIT_FAIL_ENTRY;
8997 vcpu->run->fail_entry.hardware_entry_failure_reason
8998 = exit_reason;
8999 return 0;
9000 }
9001
Avi Kivity29bd8a72007-09-10 17:27:03 +03009002 if (unlikely(vmx->fail)) {
Avi Kivity851ba692009-08-24 11:10:17 +03009003 vcpu->run->exit_reason = KVM_EXIT_FAIL_ENTRY;
9004 vcpu->run->fail_entry.hardware_entry_failure_reason
Avi Kivity29bd8a72007-09-10 17:27:03 +03009005 = vmcs_read32(VM_INSTRUCTION_ERROR);
9006 return 0;
9007 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08009008
Xiao Guangrongb9bf6882012-10-17 13:46:52 +08009009 /*
9010 * Note:
9011 * Do not try to fix EXIT_REASON_EPT_MISCONFIG if it caused by
9012 * delivery event since it indicates guest is accessing MMIO.
9013 * The vm-exit can be triggered again after return to guest that
9014 * will cause infinite loop.
9015 */
Mike Dayd77c26f2007-10-08 09:02:08 -04009016 if ((vectoring_info & VECTORING_INFO_VALID_MASK) &&
Sheng Yang14394422008-04-28 12:24:45 +08009017 (exit_reason != EXIT_REASON_EXCEPTION_NMI &&
Jan Kiszka60637aa2008-09-26 09:30:47 +02009018 exit_reason != EXIT_REASON_EPT_VIOLATION &&
Cao, Leib244c9f2016-07-15 13:54:04 +00009019 exit_reason != EXIT_REASON_PML_FULL &&
Xiao Guangrongb9bf6882012-10-17 13:46:52 +08009020 exit_reason != EXIT_REASON_TASK_SWITCH)) {
9021 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
9022 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_DELIVERY_EV;
Paolo Bonzini70bcd702017-07-05 12:38:06 +02009023 vcpu->run->internal.ndata = 3;
Xiao Guangrongb9bf6882012-10-17 13:46:52 +08009024 vcpu->run->internal.data[0] = vectoring_info;
9025 vcpu->run->internal.data[1] = exit_reason;
Paolo Bonzini70bcd702017-07-05 12:38:06 +02009026 vcpu->run->internal.data[2] = vcpu->arch.exit_qualification;
9027 if (exit_reason == EXIT_REASON_EPT_MISCONFIG) {
9028 vcpu->run->internal.ndata++;
9029 vcpu->run->internal.data[3] =
9030 vmcs_read64(GUEST_PHYSICAL_ADDRESS);
9031 }
Xiao Guangrongb9bf6882012-10-17 13:46:52 +08009032 return 0;
9033 }
Jan Kiszka3b86cd92008-09-26 09:30:57 +02009034
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01009035 if (unlikely(!enable_vnmi &&
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01009036 vmx->loaded_vmcs->soft_vnmi_blocked)) {
9037 if (vmx_interrupt_allowed(vcpu)) {
9038 vmx->loaded_vmcs->soft_vnmi_blocked = 0;
9039 } else if (vmx->loaded_vmcs->vnmi_blocked_time > 1000000000LL &&
9040 vcpu->arch.nmi_pending) {
9041 /*
9042 * This CPU don't support us in finding the end of an
9043 * NMI-blocked window if the guest runs with IRQs
9044 * disabled. So we pull the trigger after 1 s of
9045 * futile waiting, but inform the user about this.
9046 */
9047 printk(KERN_WARNING "%s: Breaking out of NMI-blocked "
9048 "state on VCPU %d after 1 s timeout\n",
9049 __func__, vcpu->vcpu_id);
9050 vmx->loaded_vmcs->soft_vnmi_blocked = 0;
9051 }
9052 }
9053
Avi Kivity6aa8b732006-12-10 02:21:36 -08009054 if (exit_reason < kvm_vmx_max_exit_handlers
9055 && kvm_vmx_exit_handlers[exit_reason])
Avi Kivity851ba692009-08-24 11:10:17 +03009056 return kvm_vmx_exit_handlers[exit_reason](vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08009057 else {
Radim Krčmář6c6c5e02017-01-13 18:59:04 +01009058 vcpu_unimpl(vcpu, "vmx: unexpected exit reason 0x%x\n",
9059 exit_reason);
Michael S. Tsirkin2bc19dc2014-09-18 16:21:16 +03009060 kvm_queue_exception(vcpu, UD_VECTOR);
9061 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08009062 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08009063}
9064
Gleb Natapov95ba8273132009-04-21 17:45:08 +03009065static void update_cr8_intercept(struct kvm_vcpu *vcpu, int tpr, int irr)
Yang, Sheng6e5d8652007-09-12 18:03:11 +08009066{
Wanpeng Lia7c0b072014-08-21 19:46:50 +08009067 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
9068
9069 if (is_guest_mode(vcpu) &&
9070 nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW))
9071 return;
9072
Gleb Natapov95ba8273132009-04-21 17:45:08 +03009073 if (irr == -1 || tpr < irr) {
Yang, Sheng6e5d8652007-09-12 18:03:11 +08009074 vmcs_write32(TPR_THRESHOLD, 0);
9075 return;
9076 }
9077
Gleb Natapov95ba8273132009-04-21 17:45:08 +03009078 vmcs_write32(TPR_THRESHOLD, irr);
Yang, Sheng6e5d8652007-09-12 18:03:11 +08009079}
9080
Yang Zhang8d146952013-01-25 10:18:50 +08009081static void vmx_set_virtual_x2apic_mode(struct kvm_vcpu *vcpu, bool set)
9082{
9083 u32 sec_exec_control;
9084
Radim Krčmářdccbfcf2016-08-08 20:16:23 +02009085 /* Postpone execution until vmcs01 is the current VMCS. */
9086 if (is_guest_mode(vcpu)) {
9087 to_vmx(vcpu)->nested.change_vmcs01_virtual_x2apic_mode = true;
9088 return;
9089 }
9090
Wanpeng Lif6e90f92016-09-22 07:43:25 +08009091 if (!cpu_has_vmx_virtualize_x2apic_mode())
Yang Zhang8d146952013-01-25 10:18:50 +08009092 return;
9093
Paolo Bonzini35754c92015-07-29 12:05:37 +02009094 if (!cpu_need_tpr_shadow(vcpu))
Yang Zhang8d146952013-01-25 10:18:50 +08009095 return;
9096
9097 sec_exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
9098
9099 if (set) {
9100 sec_exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
9101 sec_exec_control |= SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
9102 } else {
9103 sec_exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
9104 sec_exec_control |= SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
Jim Mattsonfb6c8192017-03-16 13:53:59 -07009105 vmx_flush_tlb_ept_only(vcpu);
Yang Zhang8d146952013-01-25 10:18:50 +08009106 }
9107 vmcs_write32(SECONDARY_VM_EXEC_CONTROL, sec_exec_control);
9108
Paolo Bonzini904e14f2018-01-16 16:51:18 +01009109 vmx_update_msr_bitmap(vcpu);
Yang Zhang8d146952013-01-25 10:18:50 +08009110}
9111
Tang Chen38b99172014-09-24 15:57:54 +08009112static void vmx_set_apic_access_page_addr(struct kvm_vcpu *vcpu, hpa_t hpa)
9113{
9114 struct vcpu_vmx *vmx = to_vmx(vcpu);
9115
9116 /*
9117 * Currently we do not handle the nested case where L2 has an
9118 * APIC access page of its own; that page is still pinned.
9119 * Hence, we skip the case where the VCPU is in guest mode _and_
9120 * L1 prepared an APIC access page for L2.
9121 *
9122 * For the case where L1 and L2 share the same APIC access page
9123 * (flexpriority=Y but SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES clear
9124 * in the vmcs12), this function will only update either the vmcs01
9125 * or the vmcs02. If the former, the vmcs02 will be updated by
9126 * prepare_vmcs02. If the latter, the vmcs01 will be updated in
9127 * the next L2->L1 exit.
9128 */
9129 if (!is_guest_mode(vcpu) ||
David Matlack4f2777b2016-07-13 17:16:37 -07009130 !nested_cpu_has2(get_vmcs12(&vmx->vcpu),
Jim Mattsonfb6c8192017-03-16 13:53:59 -07009131 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) {
Tang Chen38b99172014-09-24 15:57:54 +08009132 vmcs_write64(APIC_ACCESS_ADDR, hpa);
Jim Mattsonfb6c8192017-03-16 13:53:59 -07009133 vmx_flush_tlb_ept_only(vcpu);
9134 }
Tang Chen38b99172014-09-24 15:57:54 +08009135}
9136
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +02009137static void vmx_hwapic_isr_update(struct kvm_vcpu *vcpu, int max_isr)
Yang Zhangc7c9c562013-01-25 10:18:51 +08009138{
9139 u16 status;
9140 u8 old;
9141
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +02009142 if (max_isr == -1)
9143 max_isr = 0;
Yang Zhangc7c9c562013-01-25 10:18:51 +08009144
9145 status = vmcs_read16(GUEST_INTR_STATUS);
9146 old = status >> 8;
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +02009147 if (max_isr != old) {
Yang Zhangc7c9c562013-01-25 10:18:51 +08009148 status &= 0xff;
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +02009149 status |= max_isr << 8;
Yang Zhangc7c9c562013-01-25 10:18:51 +08009150 vmcs_write16(GUEST_INTR_STATUS, status);
9151 }
9152}
9153
9154static void vmx_set_rvi(int vector)
9155{
9156 u16 status;
9157 u8 old;
9158
Wei Wang4114c272014-11-05 10:53:43 +08009159 if (vector == -1)
9160 vector = 0;
9161
Yang Zhangc7c9c562013-01-25 10:18:51 +08009162 status = vmcs_read16(GUEST_INTR_STATUS);
9163 old = (u8)status & 0xff;
9164 if ((u8)vector != old) {
9165 status &= ~0xff;
9166 status |= (u8)vector;
9167 vmcs_write16(GUEST_INTR_STATUS, status);
9168 }
9169}
9170
9171static void vmx_hwapic_irr_update(struct kvm_vcpu *vcpu, int max_irr)
9172{
Liran Alon851c1a182017-12-24 18:12:56 +02009173 /*
9174 * When running L2, updating RVI is only relevant when
9175 * vmcs12 virtual-interrupt-delivery enabled.
9176 * However, it can be enabled only when L1 also
9177 * intercepts external-interrupts and in that case
9178 * we should not update vmcs02 RVI but instead intercept
9179 * interrupt. Therefore, do nothing when running L2.
9180 */
9181 if (!is_guest_mode(vcpu))
Wanpeng Li963fee12014-07-17 19:03:00 +08009182 vmx_set_rvi(max_irr);
Yang Zhangc7c9c562013-01-25 10:18:51 +08009183}
9184
Paolo Bonzini76dfafd52016-12-19 17:17:11 +01009185static int vmx_sync_pir_to_irr(struct kvm_vcpu *vcpu)
Paolo Bonzini810e6de2016-12-19 13:05:46 +01009186{
9187 struct vcpu_vmx *vmx = to_vmx(vcpu);
Paolo Bonzini76dfafd52016-12-19 17:17:11 +01009188 int max_irr;
Liran Alonf27a85c2017-12-24 18:12:55 +02009189 bool max_irr_updated;
Paolo Bonzini810e6de2016-12-19 13:05:46 +01009190
Paolo Bonzini76dfafd52016-12-19 17:17:11 +01009191 WARN_ON(!vcpu->arch.apicv_active);
9192 if (pi_test_on(&vmx->pi_desc)) {
9193 pi_clear_on(&vmx->pi_desc);
9194 /*
9195 * IOMMU can write to PIR.ON, so the barrier matters even on UP.
9196 * But on x86 this is just a compiler barrier anyway.
9197 */
9198 smp_mb__after_atomic();
Liran Alonf27a85c2017-12-24 18:12:55 +02009199 max_irr_updated =
9200 kvm_apic_update_irr(vcpu, vmx->pi_desc.pir, &max_irr);
9201
9202 /*
9203 * If we are running L2 and L1 has a new pending interrupt
9204 * which can be injected, we should re-evaluate
9205 * what should be done with this new L1 interrupt.
Liran Alon851c1a182017-12-24 18:12:56 +02009206 * If L1 intercepts external-interrupts, we should
9207 * exit from L2 to L1. Otherwise, interrupt should be
9208 * delivered directly to L2.
Liran Alonf27a85c2017-12-24 18:12:55 +02009209 */
Liran Alon851c1a182017-12-24 18:12:56 +02009210 if (is_guest_mode(vcpu) && max_irr_updated) {
9211 if (nested_exit_on_intr(vcpu))
9212 kvm_vcpu_exiting_guest_mode(vcpu);
9213 else
9214 kvm_make_request(KVM_REQ_EVENT, vcpu);
9215 }
Paolo Bonzini76dfafd52016-12-19 17:17:11 +01009216 } else {
9217 max_irr = kvm_lapic_find_highest_irr(vcpu);
9218 }
9219 vmx_hwapic_irr_update(vcpu, max_irr);
9220 return max_irr;
Paolo Bonzini810e6de2016-12-19 13:05:46 +01009221}
9222
Andrey Smetanin63086302015-11-10 15:36:32 +03009223static void vmx_load_eoi_exitmap(struct kvm_vcpu *vcpu, u64 *eoi_exit_bitmap)
Yang Zhangc7c9c562013-01-25 10:18:51 +08009224{
Andrey Smetanind62caab2015-11-10 15:36:33 +03009225 if (!kvm_vcpu_apicv_active(vcpu))
Yang Zhang3d81bc72013-04-11 19:25:13 +08009226 return;
9227
Yang Zhangc7c9c562013-01-25 10:18:51 +08009228 vmcs_write64(EOI_EXIT_BITMAP0, eoi_exit_bitmap[0]);
9229 vmcs_write64(EOI_EXIT_BITMAP1, eoi_exit_bitmap[1]);
9230 vmcs_write64(EOI_EXIT_BITMAP2, eoi_exit_bitmap[2]);
9231 vmcs_write64(EOI_EXIT_BITMAP3, eoi_exit_bitmap[3]);
9232}
9233
Paolo Bonzini967235d2016-12-19 14:03:45 +01009234static void vmx_apicv_post_state_restore(struct kvm_vcpu *vcpu)
9235{
9236 struct vcpu_vmx *vmx = to_vmx(vcpu);
9237
9238 pi_clear_on(&vmx->pi_desc);
9239 memset(vmx->pi_desc.pir, 0, sizeof(vmx->pi_desc.pir));
9240}
9241
Avi Kivity51aa01d2010-07-20 14:31:20 +03009242static void vmx_complete_atomic_exit(struct vcpu_vmx *vmx)
Avi Kivitycf393f72008-07-01 16:20:21 +03009243{
Jim Mattson48ae0fb2017-05-22 09:48:33 -07009244 u32 exit_intr_info = 0;
9245 u16 basic_exit_reason = (u16)vmx->exit_reason;
Avi Kivity00eba012011-03-07 17:24:54 +02009246
Jim Mattson48ae0fb2017-05-22 09:48:33 -07009247 if (!(basic_exit_reason == EXIT_REASON_MCE_DURING_VMENTRY
9248 || basic_exit_reason == EXIT_REASON_EXCEPTION_NMI))
Avi Kivity00eba012011-03-07 17:24:54 +02009249 return;
9250
Jim Mattson48ae0fb2017-05-22 09:48:33 -07009251 if (!(vmx->exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY))
9252 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
9253 vmx->exit_intr_info = exit_intr_info;
Andi Kleena0861c02009-06-08 17:37:09 +08009254
Wanpeng Li1261bfa2017-07-13 18:30:40 -07009255 /* if exit due to PF check for async PF */
9256 if (is_page_fault(exit_intr_info))
9257 vmx->vcpu.arch.apf.host_apf_reason = kvm_read_and_reset_pf_reason();
9258
Andi Kleena0861c02009-06-08 17:37:09 +08009259 /* Handle machine checks before interrupts are enabled */
Jim Mattson48ae0fb2017-05-22 09:48:33 -07009260 if (basic_exit_reason == EXIT_REASON_MCE_DURING_VMENTRY ||
9261 is_machine_check(exit_intr_info))
Andi Kleena0861c02009-06-08 17:37:09 +08009262 kvm_machine_check();
9263
Gleb Natapov20f65982009-05-11 13:35:55 +03009264 /* We need to handle NMIs before interrupts are enabled */
Jim Mattsonef85b672016-12-12 11:01:37 -08009265 if (is_nmi(exit_intr_info)) {
Zhang, Yanminff9d07a2010-04-19 13:32:45 +08009266 kvm_before_handle_nmi(&vmx->vcpu);
Gleb Natapov20f65982009-05-11 13:35:55 +03009267 asm("int $2");
Zhang, Yanminff9d07a2010-04-19 13:32:45 +08009268 kvm_after_handle_nmi(&vmx->vcpu);
9269 }
Avi Kivity51aa01d2010-07-20 14:31:20 +03009270}
Gleb Natapov20f65982009-05-11 13:35:55 +03009271
Yang Zhanga547c6d2013-04-11 19:25:10 +08009272static void vmx_handle_external_intr(struct kvm_vcpu *vcpu)
9273{
9274 u32 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
9275
Yang Zhanga547c6d2013-04-11 19:25:10 +08009276 if ((exit_intr_info & (INTR_INFO_VALID_MASK | INTR_INFO_INTR_TYPE_MASK))
9277 == (INTR_INFO_VALID_MASK | INTR_TYPE_EXT_INTR)) {
9278 unsigned int vector;
9279 unsigned long entry;
9280 gate_desc *desc;
9281 struct vcpu_vmx *vmx = to_vmx(vcpu);
9282#ifdef CONFIG_X86_64
9283 unsigned long tmp;
9284#endif
9285
9286 vector = exit_intr_info & INTR_INFO_VECTOR_MASK;
9287 desc = (gate_desc *)vmx->host_idt_base + vector;
Thomas Gleixner64b163f2017-08-28 08:47:37 +02009288 entry = gate_offset(desc);
Yang Zhanga547c6d2013-04-11 19:25:10 +08009289 asm volatile(
9290#ifdef CONFIG_X86_64
9291 "mov %%" _ASM_SP ", %[sp]\n\t"
9292 "and $0xfffffffffffffff0, %%" _ASM_SP "\n\t"
9293 "push $%c[ss]\n\t"
9294 "push %[sp]\n\t"
9295#endif
9296 "pushf\n\t"
Yang Zhanga547c6d2013-04-11 19:25:10 +08009297 __ASM_SIZE(push) " $%c[cs]\n\t"
Peter Zijlstrac940a3f2018-01-25 10:58:14 +01009298 CALL_NOSPEC
Yang Zhanga547c6d2013-04-11 19:25:10 +08009299 :
9300#ifdef CONFIG_X86_64
Chris J Arges3f62de52016-01-22 15:44:38 -06009301 [sp]"=&r"(tmp),
Yang Zhanga547c6d2013-04-11 19:25:10 +08009302#endif
Josh Poimboeuff5caf622017-09-20 16:24:33 -05009303 ASM_CALL_CONSTRAINT
Yang Zhanga547c6d2013-04-11 19:25:10 +08009304 :
Peter Zijlstrac940a3f2018-01-25 10:58:14 +01009305 THUNK_TARGET(entry),
Yang Zhanga547c6d2013-04-11 19:25:10 +08009306 [ss]"i"(__KERNEL_DS),
9307 [cs]"i"(__KERNEL_CS)
9308 );
Paolo Bonzinif2485b32016-06-15 15:23:11 +02009309 }
Yang Zhanga547c6d2013-04-11 19:25:10 +08009310}
Josh Poimboeufc207aee2017-06-28 10:11:06 -05009311STACK_FRAME_NON_STANDARD(vmx_handle_external_intr);
Yang Zhanga547c6d2013-04-11 19:25:10 +08009312
Paolo Bonzini6d396b52015-04-01 14:25:33 +02009313static bool vmx_has_high_real_mode_segbase(void)
9314{
9315 return enable_unrestricted_guest || emulate_invalid_guest_state;
9316}
9317
Liu, Jinsongda8999d2014-02-24 10:55:46 +00009318static bool vmx_mpx_supported(void)
9319{
9320 return (vmcs_config.vmexit_ctrl & VM_EXIT_CLEAR_BNDCFGS) &&
9321 (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_BNDCFGS);
9322}
9323
Wanpeng Li55412b22014-12-02 19:21:30 +08009324static bool vmx_xsaves_supported(void)
9325{
9326 return vmcs_config.cpu_based_2nd_exec_ctrl &
9327 SECONDARY_EXEC_XSAVES;
9328}
9329
Paolo Bonzini66336ca2016-07-12 10:36:41 +02009330static bool vmx_umip_emulated(void)
9331{
Paolo Bonzini0367f202016-07-12 10:44:55 +02009332 return vmcs_config.cpu_based_2nd_exec_ctrl &
9333 SECONDARY_EXEC_DESC;
Paolo Bonzini66336ca2016-07-12 10:36:41 +02009334}
9335
Avi Kivity51aa01d2010-07-20 14:31:20 +03009336static void vmx_recover_nmi_blocking(struct vcpu_vmx *vmx)
9337{
Avi Kivityc5ca8e52011-03-07 17:37:37 +02009338 u32 exit_intr_info;
Avi Kivity51aa01d2010-07-20 14:31:20 +03009339 bool unblock_nmi;
9340 u8 vector;
9341 bool idtv_info_valid;
9342
9343 idtv_info_valid = vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK;
Gleb Natapov20f65982009-05-11 13:35:55 +03009344
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01009345 if (enable_vnmi) {
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01009346 if (vmx->loaded_vmcs->nmi_known_unmasked)
9347 return;
9348 /*
9349 * Can't use vmx->exit_intr_info since we're not sure what
9350 * the exit reason is.
9351 */
9352 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
9353 unblock_nmi = (exit_intr_info & INTR_INFO_UNBLOCK_NMI) != 0;
9354 vector = exit_intr_info & INTR_INFO_VECTOR_MASK;
9355 /*
9356 * SDM 3: 27.7.1.2 (September 2008)
9357 * Re-set bit "block by NMI" before VM entry if vmexit caused by
9358 * a guest IRET fault.
9359 * SDM 3: 23.2.2 (September 2008)
9360 * Bit 12 is undefined in any of the following cases:
9361 * If the VM exit sets the valid bit in the IDT-vectoring
9362 * information field.
9363 * If the VM exit is due to a double fault.
9364 */
9365 if ((exit_intr_info & INTR_INFO_VALID_MASK) && unblock_nmi &&
9366 vector != DF_VECTOR && !idtv_info_valid)
9367 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
9368 GUEST_INTR_STATE_NMI);
9369 else
9370 vmx->loaded_vmcs->nmi_known_unmasked =
9371 !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO)
9372 & GUEST_INTR_STATE_NMI);
9373 } else if (unlikely(vmx->loaded_vmcs->soft_vnmi_blocked))
9374 vmx->loaded_vmcs->vnmi_blocked_time +=
9375 ktime_to_ns(ktime_sub(ktime_get(),
9376 vmx->loaded_vmcs->entry_time));
Avi Kivity51aa01d2010-07-20 14:31:20 +03009377}
9378
Jan Kiszka3ab66e82013-02-20 14:03:24 +01009379static void __vmx_complete_interrupts(struct kvm_vcpu *vcpu,
Avi Kivity83422e12010-07-20 14:43:23 +03009380 u32 idt_vectoring_info,
9381 int instr_len_field,
9382 int error_code_field)
Avi Kivity51aa01d2010-07-20 14:31:20 +03009383{
Avi Kivity51aa01d2010-07-20 14:31:20 +03009384 u8 vector;
9385 int type;
9386 bool idtv_info_valid;
9387
9388 idtv_info_valid = idt_vectoring_info & VECTORING_INFO_VALID_MASK;
Avi Kivity668f6122008-07-02 09:28:55 +03009389
Jan Kiszka3ab66e82013-02-20 14:03:24 +01009390 vcpu->arch.nmi_injected = false;
9391 kvm_clear_exception_queue(vcpu);
9392 kvm_clear_interrupt_queue(vcpu);
Gleb Natapov37b96e92009-03-30 16:03:13 +03009393
9394 if (!idtv_info_valid)
9395 return;
9396
Jan Kiszka3ab66e82013-02-20 14:03:24 +01009397 kvm_make_request(KVM_REQ_EVENT, vcpu);
Avi Kivity3842d132010-07-27 12:30:24 +03009398
Avi Kivity668f6122008-07-02 09:28:55 +03009399 vector = idt_vectoring_info & VECTORING_INFO_VECTOR_MASK;
9400 type = idt_vectoring_info & VECTORING_INFO_TYPE_MASK;
Gleb Natapov37b96e92009-03-30 16:03:13 +03009401
Gleb Natapov64a7ec02009-03-30 16:03:29 +03009402 switch (type) {
Gleb Natapov37b96e92009-03-30 16:03:13 +03009403 case INTR_TYPE_NMI_INTR:
Jan Kiszka3ab66e82013-02-20 14:03:24 +01009404 vcpu->arch.nmi_injected = true;
Avi Kivity668f6122008-07-02 09:28:55 +03009405 /*
Gleb Natapov7b4a25c2009-03-30 16:03:08 +03009406 * SDM 3: 27.7.1.2 (September 2008)
Gleb Natapov37b96e92009-03-30 16:03:13 +03009407 * Clear bit "block by NMI" before VM entry if a NMI
9408 * delivery faulted.
Avi Kivity668f6122008-07-02 09:28:55 +03009409 */
Jan Kiszka3ab66e82013-02-20 14:03:24 +01009410 vmx_set_nmi_mask(vcpu, false);
Gleb Natapov37b96e92009-03-30 16:03:13 +03009411 break;
Gleb Natapov37b96e92009-03-30 16:03:13 +03009412 case INTR_TYPE_SOFT_EXCEPTION:
Jan Kiszka3ab66e82013-02-20 14:03:24 +01009413 vcpu->arch.event_exit_inst_len = vmcs_read32(instr_len_field);
Gleb Natapov66fd3f72009-05-11 13:35:50 +03009414 /* fall through */
9415 case INTR_TYPE_HARD_EXCEPTION:
Avi Kivity35920a32008-07-03 14:50:12 +03009416 if (idt_vectoring_info & VECTORING_INFO_DELIVER_CODE_MASK) {
Avi Kivity83422e12010-07-20 14:43:23 +03009417 u32 err = vmcs_read32(error_code_field);
Gleb Natapov851eb6672013-09-25 12:51:34 +03009418 kvm_requeue_exception_e(vcpu, vector, err);
Avi Kivity35920a32008-07-03 14:50:12 +03009419 } else
Gleb Natapov851eb6672013-09-25 12:51:34 +03009420 kvm_requeue_exception(vcpu, vector);
Gleb Natapov37b96e92009-03-30 16:03:13 +03009421 break;
Gleb Natapov66fd3f72009-05-11 13:35:50 +03009422 case INTR_TYPE_SOFT_INTR:
Jan Kiszka3ab66e82013-02-20 14:03:24 +01009423 vcpu->arch.event_exit_inst_len = vmcs_read32(instr_len_field);
Gleb Natapov66fd3f72009-05-11 13:35:50 +03009424 /* fall through */
Gleb Natapov37b96e92009-03-30 16:03:13 +03009425 case INTR_TYPE_EXT_INTR:
Jan Kiszka3ab66e82013-02-20 14:03:24 +01009426 kvm_queue_interrupt(vcpu, vector, type == INTR_TYPE_SOFT_INTR);
Gleb Natapov37b96e92009-03-30 16:03:13 +03009427 break;
9428 default:
9429 break;
Avi Kivityf7d92382008-07-03 16:14:28 +03009430 }
Avi Kivitycf393f72008-07-01 16:20:21 +03009431}
9432
Avi Kivity83422e12010-07-20 14:43:23 +03009433static void vmx_complete_interrupts(struct vcpu_vmx *vmx)
9434{
Jan Kiszka3ab66e82013-02-20 14:03:24 +01009435 __vmx_complete_interrupts(&vmx->vcpu, vmx->idt_vectoring_info,
Avi Kivity83422e12010-07-20 14:43:23 +03009436 VM_EXIT_INSTRUCTION_LEN,
9437 IDT_VECTORING_ERROR_CODE);
9438}
9439
Avi Kivityb463a6f2010-07-20 15:06:17 +03009440static void vmx_cancel_injection(struct kvm_vcpu *vcpu)
9441{
Jan Kiszka3ab66e82013-02-20 14:03:24 +01009442 __vmx_complete_interrupts(vcpu,
Avi Kivityb463a6f2010-07-20 15:06:17 +03009443 vmcs_read32(VM_ENTRY_INTR_INFO_FIELD),
9444 VM_ENTRY_INSTRUCTION_LEN,
9445 VM_ENTRY_EXCEPTION_ERROR_CODE);
9446
9447 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0);
9448}
9449
Gleb Natapovd7cd9792011-10-05 14:01:23 +02009450static void atomic_switch_perf_msrs(struct vcpu_vmx *vmx)
9451{
9452 int i, nr_msrs;
9453 struct perf_guest_switch_msr *msrs;
9454
9455 msrs = perf_guest_get_msrs(&nr_msrs);
9456
9457 if (!msrs)
9458 return;
9459
9460 for (i = 0; i < nr_msrs; i++)
9461 if (msrs[i].host == msrs[i].guest)
9462 clear_atomic_switch_msr(vmx, msrs[i].msr);
9463 else
9464 add_atomic_switch_msr(vmx, msrs[i].msr, msrs[i].guest,
9465 msrs[i].host);
9466}
9467
Jiang Biao33365e72016-11-03 15:03:37 +08009468static void vmx_arm_hv_timer(struct kvm_vcpu *vcpu)
Yunhong Jiang64672c92016-06-13 14:19:59 -07009469{
9470 struct vcpu_vmx *vmx = to_vmx(vcpu);
9471 u64 tscl;
9472 u32 delta_tsc;
9473
9474 if (vmx->hv_deadline_tsc == -1)
9475 return;
9476
9477 tscl = rdtsc();
9478 if (vmx->hv_deadline_tsc > tscl)
9479 /* sure to be 32 bit only because checked on set_hv_timer */
9480 delta_tsc = (u32)((vmx->hv_deadline_tsc - tscl) >>
9481 cpu_preemption_timer_multi);
9482 else
9483 delta_tsc = 0;
9484
9485 vmcs_write32(VMX_PREEMPTION_TIMER_VALUE, delta_tsc);
9486}
9487
Lai Jiangshana3b5ba42011-02-11 14:29:40 +08009488static void __noclone vmx_vcpu_run(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08009489{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04009490 struct vcpu_vmx *vmx = to_vmx(vcpu);
Wanpeng Li74c55932017-11-29 01:31:20 -08009491 unsigned long cr3, cr4;
Avi Kivity104f2262010-11-18 13:12:52 +02009492
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01009493 /* Record the guest's net vcpu time for enforced NMI injections. */
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01009494 if (unlikely(!enable_vnmi &&
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01009495 vmx->loaded_vmcs->soft_vnmi_blocked))
9496 vmx->loaded_vmcs->entry_time = ktime_get();
9497
Avi Kivity104f2262010-11-18 13:12:52 +02009498 /* Don't enter VMX if guest state is invalid, let the exit handler
9499 start emulation until we arrive back to a valid state */
Gleb Natapov14168782013-01-21 15:36:49 +02009500 if (vmx->emulation_required)
Avi Kivity104f2262010-11-18 13:12:52 +02009501 return;
9502
Radim Krčmářa7653ec2014-08-21 18:08:07 +02009503 if (vmx->ple_window_dirty) {
9504 vmx->ple_window_dirty = false;
9505 vmcs_write32(PLE_WINDOW, vmx->ple_window);
9506 }
9507
Abel Gordon012f83c2013-04-18 14:39:25 +03009508 if (vmx->nested.sync_shadow_vmcs) {
9509 copy_vmcs12_to_shadow(vmx);
9510 vmx->nested.sync_shadow_vmcs = false;
9511 }
9512
Avi Kivity104f2262010-11-18 13:12:52 +02009513 if (test_bit(VCPU_REGS_RSP, (unsigned long *)&vcpu->arch.regs_dirty))
9514 vmcs_writel(GUEST_RSP, vcpu->arch.regs[VCPU_REGS_RSP]);
9515 if (test_bit(VCPU_REGS_RIP, (unsigned long *)&vcpu->arch.regs_dirty))
9516 vmcs_writel(GUEST_RIP, vcpu->arch.regs[VCPU_REGS_RIP]);
9517
Andy Lutomirskid6e41f12017-05-28 10:00:17 -07009518 cr3 = __get_current_cr3_fast();
Ladi Prosek44889942017-09-22 07:53:15 +02009519 if (unlikely(cr3 != vmx->loaded_vmcs->vmcs_host_cr3)) {
Andy Lutomirskid6e41f12017-05-28 10:00:17 -07009520 vmcs_writel(HOST_CR3, cr3);
Ladi Prosek44889942017-09-22 07:53:15 +02009521 vmx->loaded_vmcs->vmcs_host_cr3 = cr3;
Andy Lutomirskid6e41f12017-05-28 10:00:17 -07009522 }
9523
Andy Lutomirski1e02ce42014-10-24 15:58:08 -07009524 cr4 = cr4_read_shadow();
Ladi Prosek44889942017-09-22 07:53:15 +02009525 if (unlikely(cr4 != vmx->loaded_vmcs->vmcs_host_cr4)) {
Andy Lutomirskid974baa2014-10-08 09:02:13 -07009526 vmcs_writel(HOST_CR4, cr4);
Ladi Prosek44889942017-09-22 07:53:15 +02009527 vmx->loaded_vmcs->vmcs_host_cr4 = cr4;
Andy Lutomirskid974baa2014-10-08 09:02:13 -07009528 }
9529
Avi Kivity104f2262010-11-18 13:12:52 +02009530 /* When single-stepping over STI and MOV SS, we must clear the
9531 * corresponding interruptibility bits in the guest state. Otherwise
9532 * vmentry fails as it then expects bit 14 (BS) in pending debug
9533 * exceptions being set, but that's not correct for the guest debugging
9534 * case. */
9535 if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP)
9536 vmx_set_interrupt_shadow(vcpu, 0);
9537
Paolo Bonzinib9dd21e2017-08-23 23:14:38 +02009538 if (static_cpu_has(X86_FEATURE_PKU) &&
9539 kvm_read_cr4_bits(vcpu, X86_CR4_PKE) &&
9540 vcpu->arch.pkru != vmx->host_pkru)
9541 __write_pkru(vcpu->arch.pkru);
Xiao Guangrong1be0e612016-03-22 16:51:18 +08009542
Gleb Natapovd7cd9792011-10-05 14:01:23 +02009543 atomic_switch_perf_msrs(vmx);
9544
Yunhong Jiang64672c92016-06-13 14:19:59 -07009545 vmx_arm_hv_timer(vcpu);
9546
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01009547 /*
9548 * If this vCPU has touched SPEC_CTRL, restore the guest's value if
9549 * it's non-zero. Since vmentry is serialising on affected CPUs, there
9550 * is no need to worry about the conditional branch over the wrmsr
9551 * being speculatively taken.
9552 */
9553 if (vmx->spec_ctrl)
Paolo Bonziniecb586b2018-02-22 16:43:17 +01009554 native_wrmsrl(MSR_IA32_SPEC_CTRL, vmx->spec_ctrl);
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01009555
Nadav Har'Eld462b812011-05-24 15:26:10 +03009556 vmx->__launched = vmx->loaded_vmcs->launched;
Avi Kivity104f2262010-11-18 13:12:52 +02009557 asm(
Avi Kivity6aa8b732006-12-10 02:21:36 -08009558 /* Store host registers */
Avi Kivityb188c81f2012-09-16 15:10:58 +03009559 "push %%" _ASM_DX "; push %%" _ASM_BP ";"
9560 "push %%" _ASM_CX " \n\t" /* placeholder for guest rcx */
9561 "push %%" _ASM_CX " \n\t"
9562 "cmp %%" _ASM_SP ", %c[host_rsp](%0) \n\t"
Avi Kivity313dbd42008-07-17 18:04:30 +03009563 "je 1f \n\t"
Avi Kivityb188c81f2012-09-16 15:10:58 +03009564 "mov %%" _ASM_SP ", %c[host_rsp](%0) \n\t"
Avi Kivity4ecac3f2008-05-13 13:23:38 +03009565 __ex(ASM_VMX_VMWRITE_RSP_RDX) "\n\t"
Avi Kivity313dbd42008-07-17 18:04:30 +03009566 "1: \n\t"
Avi Kivityd3edefc2009-06-16 12:33:56 +03009567 /* Reload cr2 if changed */
Avi Kivityb188c81f2012-09-16 15:10:58 +03009568 "mov %c[cr2](%0), %%" _ASM_AX " \n\t"
9569 "mov %%cr2, %%" _ASM_DX " \n\t"
9570 "cmp %%" _ASM_AX ", %%" _ASM_DX " \n\t"
Avi Kivityd3edefc2009-06-16 12:33:56 +03009571 "je 2f \n\t"
Avi Kivityb188c81f2012-09-16 15:10:58 +03009572 "mov %%" _ASM_AX", %%cr2 \n\t"
Avi Kivityd3edefc2009-06-16 12:33:56 +03009573 "2: \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08009574 /* Check if vmlaunch of vmresume is needed */
Avi Kivitye08aa782007-11-15 18:06:18 +02009575 "cmpl $0, %c[launched](%0) \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08009576 /* Load guest registers. Don't clobber flags. */
Avi Kivityb188c81f2012-09-16 15:10:58 +03009577 "mov %c[rax](%0), %%" _ASM_AX " \n\t"
9578 "mov %c[rbx](%0), %%" _ASM_BX " \n\t"
9579 "mov %c[rdx](%0), %%" _ASM_DX " \n\t"
9580 "mov %c[rsi](%0), %%" _ASM_SI " \n\t"
9581 "mov %c[rdi](%0), %%" _ASM_DI " \n\t"
9582 "mov %c[rbp](%0), %%" _ASM_BP " \n\t"
Avi Kivity05b3e0c2006-12-13 00:33:45 -08009583#ifdef CONFIG_X86_64
Avi Kivitye08aa782007-11-15 18:06:18 +02009584 "mov %c[r8](%0), %%r8 \n\t"
9585 "mov %c[r9](%0), %%r9 \n\t"
9586 "mov %c[r10](%0), %%r10 \n\t"
9587 "mov %c[r11](%0), %%r11 \n\t"
9588 "mov %c[r12](%0), %%r12 \n\t"
9589 "mov %c[r13](%0), %%r13 \n\t"
9590 "mov %c[r14](%0), %%r14 \n\t"
9591 "mov %c[r15](%0), %%r15 \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08009592#endif
Avi Kivityb188c81f2012-09-16 15:10:58 +03009593 "mov %c[rcx](%0), %%" _ASM_CX " \n\t" /* kills %0 (ecx) */
Avi Kivityc8019492008-07-14 14:44:59 +03009594
Avi Kivity6aa8b732006-12-10 02:21:36 -08009595 /* Enter guest mode */
Avi Kivity83287ea422012-09-16 15:10:57 +03009596 "jne 1f \n\t"
Avi Kivity4ecac3f2008-05-13 13:23:38 +03009597 __ex(ASM_VMX_VMLAUNCH) "\n\t"
Avi Kivity83287ea422012-09-16 15:10:57 +03009598 "jmp 2f \n\t"
9599 "1: " __ex(ASM_VMX_VMRESUME) "\n\t"
9600 "2: "
Avi Kivity6aa8b732006-12-10 02:21:36 -08009601 /* Save guest registers, load host registers, keep flags */
Avi Kivityb188c81f2012-09-16 15:10:58 +03009602 "mov %0, %c[wordsize](%%" _ASM_SP ") \n\t"
Avi Kivity40712fa2011-01-06 18:09:12 +02009603 "pop %0 \n\t"
Jim Mattson0cb5b302018-01-03 14:31:38 -08009604 "setbe %c[fail](%0)\n\t"
Avi Kivityb188c81f2012-09-16 15:10:58 +03009605 "mov %%" _ASM_AX ", %c[rax](%0) \n\t"
9606 "mov %%" _ASM_BX ", %c[rbx](%0) \n\t"
9607 __ASM_SIZE(pop) " %c[rcx](%0) \n\t"
9608 "mov %%" _ASM_DX ", %c[rdx](%0) \n\t"
9609 "mov %%" _ASM_SI ", %c[rsi](%0) \n\t"
9610 "mov %%" _ASM_DI ", %c[rdi](%0) \n\t"
9611 "mov %%" _ASM_BP ", %c[rbp](%0) \n\t"
Avi Kivity05b3e0c2006-12-13 00:33:45 -08009612#ifdef CONFIG_X86_64
Avi Kivitye08aa782007-11-15 18:06:18 +02009613 "mov %%r8, %c[r8](%0) \n\t"
9614 "mov %%r9, %c[r9](%0) \n\t"
9615 "mov %%r10, %c[r10](%0) \n\t"
9616 "mov %%r11, %c[r11](%0) \n\t"
9617 "mov %%r12, %c[r12](%0) \n\t"
9618 "mov %%r13, %c[r13](%0) \n\t"
9619 "mov %%r14, %c[r14](%0) \n\t"
9620 "mov %%r15, %c[r15](%0) \n\t"
Jim Mattson0cb5b302018-01-03 14:31:38 -08009621 "xor %%r8d, %%r8d \n\t"
9622 "xor %%r9d, %%r9d \n\t"
9623 "xor %%r10d, %%r10d \n\t"
9624 "xor %%r11d, %%r11d \n\t"
9625 "xor %%r12d, %%r12d \n\t"
9626 "xor %%r13d, %%r13d \n\t"
9627 "xor %%r14d, %%r14d \n\t"
9628 "xor %%r15d, %%r15d \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08009629#endif
Avi Kivityb188c81f2012-09-16 15:10:58 +03009630 "mov %%cr2, %%" _ASM_AX " \n\t"
9631 "mov %%" _ASM_AX ", %c[cr2](%0) \n\t"
Avi Kivityc8019492008-07-14 14:44:59 +03009632
Jim Mattson0cb5b302018-01-03 14:31:38 -08009633 "xor %%eax, %%eax \n\t"
9634 "xor %%ebx, %%ebx \n\t"
9635 "xor %%esi, %%esi \n\t"
9636 "xor %%edi, %%edi \n\t"
Avi Kivityb188c81f2012-09-16 15:10:58 +03009637 "pop %%" _ASM_BP "; pop %%" _ASM_DX " \n\t"
Avi Kivity83287ea422012-09-16 15:10:57 +03009638 ".pushsection .rodata \n\t"
9639 ".global vmx_return \n\t"
9640 "vmx_return: " _ASM_PTR " 2b \n\t"
9641 ".popsection"
Avi Kivitye08aa782007-11-15 18:06:18 +02009642 : : "c"(vmx), "d"((unsigned long)HOST_RSP),
Nadav Har'Eld462b812011-05-24 15:26:10 +03009643 [launched]"i"(offsetof(struct vcpu_vmx, __launched)),
Avi Kivitye08aa782007-11-15 18:06:18 +02009644 [fail]"i"(offsetof(struct vcpu_vmx, fail)),
Avi Kivity313dbd42008-07-17 18:04:30 +03009645 [host_rsp]"i"(offsetof(struct vcpu_vmx, host_rsp)),
Zhang Xiantaoad312c72007-12-13 23:50:52 +08009646 [rax]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RAX])),
9647 [rbx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBX])),
9648 [rcx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RCX])),
9649 [rdx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDX])),
9650 [rsi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RSI])),
9651 [rdi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDI])),
9652 [rbp]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBP])),
Avi Kivity05b3e0c2006-12-13 00:33:45 -08009653#ifdef CONFIG_X86_64
Zhang Xiantaoad312c72007-12-13 23:50:52 +08009654 [r8]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R8])),
9655 [r9]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R9])),
9656 [r10]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R10])),
9657 [r11]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R11])),
9658 [r12]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R12])),
9659 [r13]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R13])),
9660 [r14]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R14])),
9661 [r15]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R15])),
Avi Kivity6aa8b732006-12-10 02:21:36 -08009662#endif
Avi Kivity40712fa2011-01-06 18:09:12 +02009663 [cr2]"i"(offsetof(struct vcpu_vmx, vcpu.arch.cr2)),
9664 [wordsize]"i"(sizeof(ulong))
Laurent Vivierc2036302007-10-25 14:18:52 +02009665 : "cc", "memory"
9666#ifdef CONFIG_X86_64
Avi Kivityb188c81f2012-09-16 15:10:58 +03009667 , "rax", "rbx", "rdi", "rsi"
Laurent Vivierc2036302007-10-25 14:18:52 +02009668 , "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15"
Avi Kivityb188c81f2012-09-16 15:10:58 +03009669#else
9670 , "eax", "ebx", "edi", "esi"
Laurent Vivierc2036302007-10-25 14:18:52 +02009671#endif
9672 );
Avi Kivity6aa8b732006-12-10 02:21:36 -08009673
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01009674 /*
9675 * We do not use IBRS in the kernel. If this vCPU has used the
9676 * SPEC_CTRL MSR it may have left it on; save the value and
9677 * turn it off. This is much more efficient than blindly adding
9678 * it to the atomic save/restore list. Especially as the former
9679 * (Saving guest MSRs on vmexit) doesn't even exist in KVM.
9680 *
9681 * For non-nested case:
9682 * If the L01 MSR bitmap does not intercept the MSR, then we need to
9683 * save it.
9684 *
9685 * For nested case:
9686 * If the L02 MSR bitmap does not intercept the MSR, then we need to
9687 * save it.
9688 */
Paolo Bonzini946fbbc2018-02-22 16:43:18 +01009689 if (unlikely(!msr_write_intercepted(vcpu, MSR_IA32_SPEC_CTRL)))
Paolo Bonziniecb586b2018-02-22 16:43:17 +01009690 vmx->spec_ctrl = native_read_msr(MSR_IA32_SPEC_CTRL);
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01009691
9692 if (vmx->spec_ctrl)
Paolo Bonziniecb586b2018-02-22 16:43:17 +01009693 native_wrmsrl(MSR_IA32_SPEC_CTRL, 0);
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01009694
David Woodhouse117cc7a2018-01-12 11:11:27 +00009695 /* Eliminate branch target predictions from guest mode */
9696 vmexit_fill_RSB();
9697
Gleb Natapov2a7921b2012-08-12 16:12:29 +03009698 /* MSR_IA32_DEBUGCTLMSR is zeroed on vmexit. Restore it if needed */
Wanpeng Li74c55932017-11-29 01:31:20 -08009699 if (vmx->host_debugctlmsr)
9700 update_debugctlmsr(vmx->host_debugctlmsr);
Gleb Natapov2a7921b2012-08-12 16:12:29 +03009701
Avi Kivityaa67f602012-08-01 16:48:03 +03009702#ifndef CONFIG_X86_64
9703 /*
9704 * The sysexit path does not restore ds/es, so we must set them to
9705 * a reasonable value ourselves.
9706 *
9707 * We can't defer this to vmx_load_host_state() since that function
9708 * may be executed in interrupt context, which saves and restore segments
9709 * around it, nullifying its effect.
9710 */
9711 loadsegment(ds, __USER_DS);
9712 loadsegment(es, __USER_DS);
9713#endif
9714
Avi Kivity6de4f3a2009-05-31 22:58:47 +03009715 vcpu->arch.regs_avail = ~((1 << VCPU_REGS_RIP) | (1 << VCPU_REGS_RSP)
Avi Kivity6de12732011-03-07 12:51:22 +02009716 | (1 << VCPU_EXREG_RFLAGS)
Avi Kivityaff48ba2010-12-05 18:56:11 +02009717 | (1 << VCPU_EXREG_PDPTR)
Avi Kivity2fb92db2011-04-27 19:42:18 +03009718 | (1 << VCPU_EXREG_SEGMENTS)
Avi Kivityaff48ba2010-12-05 18:56:11 +02009719 | (1 << VCPU_EXREG_CR3));
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03009720 vcpu->arch.regs_dirty = 0;
9721
Gleb Natapove0b890d2013-09-25 12:51:33 +03009722 /*
Xiao Guangrong1be0e612016-03-22 16:51:18 +08009723 * eager fpu is enabled if PKEY is supported and CR4 is switched
9724 * back on host, so it is safe to read guest PKRU from current
9725 * XSAVE.
9726 */
Paolo Bonzinib9dd21e2017-08-23 23:14:38 +02009727 if (static_cpu_has(X86_FEATURE_PKU) &&
9728 kvm_read_cr4_bits(vcpu, X86_CR4_PKE)) {
9729 vcpu->arch.pkru = __read_pkru();
9730 if (vcpu->arch.pkru != vmx->host_pkru)
Xiao Guangrong1be0e612016-03-22 16:51:18 +08009731 __write_pkru(vmx->host_pkru);
Xiao Guangrong1be0e612016-03-22 16:51:18 +08009732 }
9733
9734 /*
Gleb Natapove0b890d2013-09-25 12:51:33 +03009735 * the KVM_REQ_EVENT optimization bit is only on for one entry, and if
9736 * we did not inject a still-pending event to L1 now because of
9737 * nested_run_pending, we need to re-enable this bit.
9738 */
9739 if (vmx->nested.nested_run_pending)
9740 kvm_make_request(KVM_REQ_EVENT, vcpu);
9741
9742 vmx->nested.nested_run_pending = 0;
Jim Mattsonb060ca32017-09-14 16:31:42 -07009743 vmx->idt_vectoring_info = 0;
9744
9745 vmx->exit_reason = vmx->fail ? 0xdead : vmcs_read32(VM_EXIT_REASON);
9746 if (vmx->fail || (vmx->exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY))
9747 return;
9748
9749 vmx->loaded_vmcs->launched = 1;
9750 vmx->idt_vectoring_info = vmcs_read32(IDT_VECTORING_INFO_FIELD);
Gleb Natapove0b890d2013-09-25 12:51:33 +03009751
Avi Kivity51aa01d2010-07-20 14:31:20 +03009752 vmx_complete_atomic_exit(vmx);
9753 vmx_recover_nmi_blocking(vmx);
Avi Kivitycf393f72008-07-01 16:20:21 +03009754 vmx_complete_interrupts(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08009755}
Josh Poimboeufc207aee2017-06-28 10:11:06 -05009756STACK_FRAME_NON_STANDARD(vmx_vcpu_run);
Avi Kivity6aa8b732006-12-10 02:21:36 -08009757
David Hildenbrand1279a6b12017-03-20 10:00:08 +01009758static void vmx_switch_vmcs(struct kvm_vcpu *vcpu, struct loaded_vmcs *vmcs)
Paolo Bonzini4fa77342014-07-17 12:25:16 +02009759{
9760 struct vcpu_vmx *vmx = to_vmx(vcpu);
9761 int cpu;
9762
David Hildenbrand1279a6b12017-03-20 10:00:08 +01009763 if (vmx->loaded_vmcs == vmcs)
Paolo Bonzini4fa77342014-07-17 12:25:16 +02009764 return;
9765
9766 cpu = get_cpu();
David Hildenbrand1279a6b12017-03-20 10:00:08 +01009767 vmx->loaded_vmcs = vmcs;
Paolo Bonzini4fa77342014-07-17 12:25:16 +02009768 vmx_vcpu_put(vcpu);
9769 vmx_vcpu_load(vcpu, cpu);
Paolo Bonzini4fa77342014-07-17 12:25:16 +02009770 put_cpu();
9771}
9772
Jim Mattson2f1fe812016-07-08 15:36:06 -07009773/*
9774 * Ensure that the current vmcs of the logical processor is the
9775 * vmcs01 of the vcpu before calling free_nested().
9776 */
9777static void vmx_free_vcpu_nested(struct kvm_vcpu *vcpu)
9778{
9779 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jim Mattson2f1fe812016-07-08 15:36:06 -07009780
Christoffer Dallec7660c2017-12-04 21:35:23 +01009781 vcpu_load(vcpu);
David Hildenbrand1279a6b12017-03-20 10:00:08 +01009782 vmx_switch_vmcs(vcpu, &vmx->vmcs01);
Jim Mattson2f1fe812016-07-08 15:36:06 -07009783 free_nested(vmx);
9784 vcpu_put(vcpu);
9785}
9786
Avi Kivity6aa8b732006-12-10 02:21:36 -08009787static void vmx_free_vcpu(struct kvm_vcpu *vcpu)
9788{
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009789 struct vcpu_vmx *vmx = to_vmx(vcpu);
9790
Kai Huang843e4332015-01-28 10:54:28 +08009791 if (enable_pml)
Kai Huanga3eaa862015-11-04 13:46:05 +08009792 vmx_destroy_pml_buffer(vmx);
Wanpeng Li991e7a02015-09-16 17:30:05 +08009793 free_vpid(vmx->vpid);
Paolo Bonzini4fa77342014-07-17 12:25:16 +02009794 leave_guest_mode(vcpu);
Jim Mattson2f1fe812016-07-08 15:36:06 -07009795 vmx_free_vcpu_nested(vcpu);
Paolo Bonzini4fa77342014-07-17 12:25:16 +02009796 free_loaded_vmcs(vmx->loaded_vmcs);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009797 kfree(vmx->guest_msrs);
9798 kvm_vcpu_uninit(vcpu);
Rusty Russella4770342007-08-01 14:46:11 +10009799 kmem_cache_free(kvm_vcpu_cache, vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08009800}
9801
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009802static struct kvm_vcpu *vmx_create_vcpu(struct kvm *kvm, unsigned int id)
Avi Kivity6aa8b732006-12-10 02:21:36 -08009803{
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009804 int err;
Rusty Russellc16f8622007-07-30 21:12:19 +10009805 struct vcpu_vmx *vmx = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
Paolo Bonzini904e14f2018-01-16 16:51:18 +01009806 unsigned long *msr_bitmap;
Avi Kivity15ad7142007-07-11 18:17:21 +03009807 int cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08009808
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04009809 if (!vmx)
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009810 return ERR_PTR(-ENOMEM);
9811
Wanpeng Li991e7a02015-09-16 17:30:05 +08009812 vmx->vpid = allocate_vpid();
Sheng Yang2384d2b2008-01-17 15:14:33 +08009813
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009814 err = kvm_vcpu_init(&vmx->vcpu, kvm, id);
9815 if (err)
9816 goto free_vcpu;
Ingo Molnar965b58a2007-01-05 16:36:23 -08009817
Peter Feiner4e595162016-07-07 14:49:58 -07009818 err = -ENOMEM;
9819
9820 /*
9821 * If PML is turned on, failure on enabling PML just results in failure
9822 * of creating the vcpu, therefore we can simplify PML logic (by
9823 * avoiding dealing with cases, such as enabling PML partially on vcpus
9824 * for the guest, etc.
9825 */
9826 if (enable_pml) {
9827 vmx->pml_pg = alloc_page(GFP_KERNEL | __GFP_ZERO);
9828 if (!vmx->pml_pg)
9829 goto uninit_vcpu;
9830 }
9831
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04009832 vmx->guest_msrs = kmalloc(PAGE_SIZE, GFP_KERNEL);
Paolo Bonzini03916db2014-07-24 14:21:57 +02009833 BUILD_BUG_ON(ARRAY_SIZE(vmx_msr_index) * sizeof(vmx->guest_msrs[0])
9834 > PAGE_SIZE);
Nadav Amit0123be42014-07-24 15:06:56 +03009835
Peter Feiner4e595162016-07-07 14:49:58 -07009836 if (!vmx->guest_msrs)
9837 goto free_pml;
Ingo Molnar965b58a2007-01-05 16:36:23 -08009838
Paolo Bonzinif21f1652018-01-11 12:16:15 +01009839 err = alloc_loaded_vmcs(&vmx->vmcs01);
9840 if (err < 0)
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009841 goto free_msrs;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04009842
Paolo Bonzini904e14f2018-01-16 16:51:18 +01009843 msr_bitmap = vmx->vmcs01.msr_bitmap;
9844 vmx_disable_intercept_for_msr(msr_bitmap, MSR_FS_BASE, MSR_TYPE_RW);
9845 vmx_disable_intercept_for_msr(msr_bitmap, MSR_GS_BASE, MSR_TYPE_RW);
9846 vmx_disable_intercept_for_msr(msr_bitmap, MSR_KERNEL_GS_BASE, MSR_TYPE_RW);
9847 vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_SYSENTER_CS, MSR_TYPE_RW);
9848 vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_SYSENTER_ESP, MSR_TYPE_RW);
9849 vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_SYSENTER_EIP, MSR_TYPE_RW);
9850 vmx->msr_bitmap_mode = 0;
9851
Paolo Bonzinif21f1652018-01-11 12:16:15 +01009852 vmx->loaded_vmcs = &vmx->vmcs01;
Avi Kivity15ad7142007-07-11 18:17:21 +03009853 cpu = get_cpu();
9854 vmx_vcpu_load(&vmx->vcpu, cpu);
Zachary Amsdene48672f2010-08-19 22:07:23 -10009855 vmx->vcpu.cpu = cpu;
David Hildenbrand12d79912017-08-24 20:51:26 +02009856 vmx_vcpu_setup(vmx);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009857 vmx_vcpu_put(&vmx->vcpu);
Avi Kivity15ad7142007-07-11 18:17:21 +03009858 put_cpu();
Paolo Bonzini35754c92015-07-29 12:05:37 +02009859 if (cpu_need_virtualize_apic_accesses(&vmx->vcpu)) {
Jan Kiszkabe6d05c2011-04-13 01:27:55 +02009860 err = alloc_apic_access_page(kvm);
9861 if (err)
Marcelo Tosatti5e4a0b32008-02-14 21:21:43 -02009862 goto free_vmcs;
Jan Kiszkaa63cb562013-04-08 11:07:46 +02009863 }
Ingo Molnar965b58a2007-01-05 16:36:23 -08009864
Sean Christophersone90008d2018-03-05 12:04:37 -08009865 if (enable_ept && !enable_unrestricted_guest) {
Tang Chenf51770e2014-09-16 18:41:59 +08009866 err = init_rmode_identity_map(kvm);
9867 if (err)
Gleb Natapov93ea5382011-02-21 12:07:59 +02009868 goto free_vmcs;
Sheng Yangb927a3c2009-07-21 10:42:48 +08009869 }
Sheng Yangb7ebfb02008-04-25 21:44:52 +08009870
Wanpeng Li5c614b32015-10-13 09:18:36 -07009871 if (nested) {
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01009872 nested_vmx_setup_ctls_msrs(&vmx->nested.msrs,
9873 kvm_vcpu_apicv_active(&vmx->vcpu));
Wanpeng Li5c614b32015-10-13 09:18:36 -07009874 vmx->nested.vpid02 = allocate_vpid();
9875 }
Wincy Vanb9c237b2015-02-03 23:56:30 +08009876
Wincy Van705699a2015-02-03 23:58:17 +08009877 vmx->nested.posted_intr_nv = -1;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +03009878 vmx->nested.current_vmptr = -1ull;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +03009879
Haozhong Zhang37e4c992016-06-22 14:59:55 +08009880 vmx->msr_ia32_feature_control_valid_bits = FEATURE_CONTROL_LOCKED;
9881
Paolo Bonzini31afb2e2017-06-06 12:57:06 +02009882 /*
9883 * Enforce invariant: pi_desc.nv is always either POSTED_INTR_VECTOR
9884 * or POSTED_INTR_WAKEUP_VECTOR.
9885 */
9886 vmx->pi_desc.nv = POSTED_INTR_VECTOR;
9887 vmx->pi_desc.sn = 1;
9888
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009889 return &vmx->vcpu;
Ingo Molnar965b58a2007-01-05 16:36:23 -08009890
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009891free_vmcs:
Wanpeng Li5c614b32015-10-13 09:18:36 -07009892 free_vpid(vmx->nested.vpid02);
Xiao Guangrong5f3fbc32012-05-14 14:58:58 +08009893 free_loaded_vmcs(vmx->loaded_vmcs);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009894free_msrs:
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009895 kfree(vmx->guest_msrs);
Peter Feiner4e595162016-07-07 14:49:58 -07009896free_pml:
9897 vmx_destroy_pml_buffer(vmx);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009898uninit_vcpu:
9899 kvm_vcpu_uninit(&vmx->vcpu);
9900free_vcpu:
Wanpeng Li991e7a02015-09-16 17:30:05 +08009901 free_vpid(vmx->vpid);
Rusty Russella4770342007-08-01 14:46:11 +10009902 kmem_cache_free(kvm_vcpu_cache, vmx);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009903 return ERR_PTR(err);
Avi Kivity6aa8b732006-12-10 02:21:36 -08009904}
9905
Yang, Sheng002c7f72007-07-31 14:23:01 +03009906static void __init vmx_check_processor_compat(void *rtn)
9907{
9908 struct vmcs_config vmcs_conf;
9909
9910 *(int *)rtn = 0;
9911 if (setup_vmcs_config(&vmcs_conf) < 0)
9912 *(int *)rtn = -EIO;
Paolo Bonzini13893092018-02-26 13:40:09 +01009913 nested_vmx_setup_ctls_msrs(&vmcs_conf.nested, enable_apicv);
Yang, Sheng002c7f72007-07-31 14:23:01 +03009914 if (memcmp(&vmcs_config, &vmcs_conf, sizeof(struct vmcs_config)) != 0) {
9915 printk(KERN_ERR "kvm: CPU %d feature inconsistency!\n",
9916 smp_processor_id());
9917 *(int *)rtn = -EIO;
9918 }
9919}
9920
Sheng Yang4b12f0d2009-04-27 20:35:42 +08009921static u64 vmx_get_mt_mask(struct kvm_vcpu *vcpu, gfn_t gfn, bool is_mmio)
Sheng Yang64d4d522008-10-09 16:01:57 +08009922{
Xiao Guangrongb18d5432015-06-15 16:55:21 +08009923 u8 cache;
9924 u64 ipat = 0;
Sheng Yang4b12f0d2009-04-27 20:35:42 +08009925
Sheng Yang522c68c2009-04-27 20:35:43 +08009926 /* For VT-d and EPT combination
Paolo Bonzini606decd2015-10-01 13:12:47 +02009927 * 1. MMIO: always map as UC
Sheng Yang522c68c2009-04-27 20:35:43 +08009928 * 2. EPT with VT-d:
9929 * a. VT-d without snooping control feature: can't guarantee the
Paolo Bonzini606decd2015-10-01 13:12:47 +02009930 * result, try to trust guest.
Sheng Yang522c68c2009-04-27 20:35:43 +08009931 * b. VT-d with snooping control feature: snooping control feature of
9932 * VT-d engine can guarantee the cache correctness. Just set it
9933 * to WB to keep consistent with host. So the same as item 3.
Sheng Yanga19a6d12010-02-09 16:41:53 +08009934 * 3. EPT without VT-d: always map as WB and set IPAT=1 to keep
Sheng Yang522c68c2009-04-27 20:35:43 +08009935 * consistent with host MTRR
9936 */
Paolo Bonzini606decd2015-10-01 13:12:47 +02009937 if (is_mmio) {
9938 cache = MTRR_TYPE_UNCACHABLE;
9939 goto exit;
9940 }
9941
9942 if (!kvm_arch_has_noncoherent_dma(vcpu->kvm)) {
Xiao Guangrongb18d5432015-06-15 16:55:21 +08009943 ipat = VMX_EPT_IPAT_BIT;
9944 cache = MTRR_TYPE_WRBACK;
9945 goto exit;
9946 }
9947
9948 if (kvm_read_cr0(vcpu) & X86_CR0_CD) {
9949 ipat = VMX_EPT_IPAT_BIT;
Paolo Bonzini0da029e2015-07-23 08:24:42 +02009950 if (kvm_check_has_quirk(vcpu->kvm, KVM_X86_QUIRK_CD_NW_CLEARED))
Xiao Guangrongfb2799502015-07-16 03:25:56 +08009951 cache = MTRR_TYPE_WRBACK;
9952 else
9953 cache = MTRR_TYPE_UNCACHABLE;
Xiao Guangrongb18d5432015-06-15 16:55:21 +08009954 goto exit;
9955 }
9956
Xiao Guangrongff536042015-06-15 16:55:22 +08009957 cache = kvm_mtrr_get_guest_memory_type(vcpu, gfn);
Xiao Guangrongb18d5432015-06-15 16:55:21 +08009958
9959exit:
9960 return (cache << VMX_EPT_MT_EPTE_SHIFT) | ipat;
Sheng Yang64d4d522008-10-09 16:01:57 +08009961}
9962
Sheng Yang17cc3932010-01-05 19:02:27 +08009963static int vmx_get_lpage_level(void)
Joerg Roedel344f4142009-07-27 16:30:48 +02009964{
Sheng Yang878403b2010-01-05 19:02:29 +08009965 if (enable_ept && !cpu_has_vmx_ept_1g_page())
9966 return PT_DIRECTORY_LEVEL;
9967 else
9968 /* For shadow and EPT supported 1GB page */
9969 return PT_PDPE_LEVEL;
Joerg Roedel344f4142009-07-27 16:30:48 +02009970}
9971
Xiao Guangrongfeda8052015-09-09 14:05:55 +08009972static void vmcs_set_secondary_exec_control(u32 new_ctl)
9973{
9974 /*
9975 * These bits in the secondary execution controls field
9976 * are dynamic, the others are mostly based on the hypervisor
9977 * architecture and the guest's CPUID. Do not touch the
9978 * dynamic bits.
9979 */
9980 u32 mask =
9981 SECONDARY_EXEC_SHADOW_VMCS |
9982 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
Paolo Bonzini0367f202016-07-12 10:44:55 +02009983 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
9984 SECONDARY_EXEC_DESC;
Xiao Guangrongfeda8052015-09-09 14:05:55 +08009985
9986 u32 cur_ctl = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
9987
9988 vmcs_write32(SECONDARY_VM_EXEC_CONTROL,
9989 (new_ctl & ~mask) | (cur_ctl & mask));
9990}
9991
David Matlack8322ebb2016-11-29 18:14:09 -08009992/*
9993 * Generate MSR_IA32_VMX_CR{0,4}_FIXED1 according to CPUID. Only set bits
9994 * (indicating "allowed-1") if they are supported in the guest's CPUID.
9995 */
9996static void nested_vmx_cr_fixed1_bits_update(struct kvm_vcpu *vcpu)
9997{
9998 struct vcpu_vmx *vmx = to_vmx(vcpu);
9999 struct kvm_cpuid_entry2 *entry;
10000
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010010001 vmx->nested.msrs.cr0_fixed1 = 0xffffffff;
10002 vmx->nested.msrs.cr4_fixed1 = X86_CR4_PCE;
David Matlack8322ebb2016-11-29 18:14:09 -080010003
10004#define cr4_fixed1_update(_cr4_mask, _reg, _cpuid_mask) do { \
10005 if (entry && (entry->_reg & (_cpuid_mask))) \
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010010006 vmx->nested.msrs.cr4_fixed1 |= (_cr4_mask); \
David Matlack8322ebb2016-11-29 18:14:09 -080010007} while (0)
10008
10009 entry = kvm_find_cpuid_entry(vcpu, 0x1, 0);
10010 cr4_fixed1_update(X86_CR4_VME, edx, bit(X86_FEATURE_VME));
10011 cr4_fixed1_update(X86_CR4_PVI, edx, bit(X86_FEATURE_VME));
10012 cr4_fixed1_update(X86_CR4_TSD, edx, bit(X86_FEATURE_TSC));
10013 cr4_fixed1_update(X86_CR4_DE, edx, bit(X86_FEATURE_DE));
10014 cr4_fixed1_update(X86_CR4_PSE, edx, bit(X86_FEATURE_PSE));
10015 cr4_fixed1_update(X86_CR4_PAE, edx, bit(X86_FEATURE_PAE));
10016 cr4_fixed1_update(X86_CR4_MCE, edx, bit(X86_FEATURE_MCE));
10017 cr4_fixed1_update(X86_CR4_PGE, edx, bit(X86_FEATURE_PGE));
10018 cr4_fixed1_update(X86_CR4_OSFXSR, edx, bit(X86_FEATURE_FXSR));
10019 cr4_fixed1_update(X86_CR4_OSXMMEXCPT, edx, bit(X86_FEATURE_XMM));
10020 cr4_fixed1_update(X86_CR4_VMXE, ecx, bit(X86_FEATURE_VMX));
10021 cr4_fixed1_update(X86_CR4_SMXE, ecx, bit(X86_FEATURE_SMX));
10022 cr4_fixed1_update(X86_CR4_PCIDE, ecx, bit(X86_FEATURE_PCID));
10023 cr4_fixed1_update(X86_CR4_OSXSAVE, ecx, bit(X86_FEATURE_XSAVE));
10024
10025 entry = kvm_find_cpuid_entry(vcpu, 0x7, 0);
10026 cr4_fixed1_update(X86_CR4_FSGSBASE, ebx, bit(X86_FEATURE_FSGSBASE));
10027 cr4_fixed1_update(X86_CR4_SMEP, ebx, bit(X86_FEATURE_SMEP));
10028 cr4_fixed1_update(X86_CR4_SMAP, ebx, bit(X86_FEATURE_SMAP));
10029 cr4_fixed1_update(X86_CR4_PKE, ecx, bit(X86_FEATURE_PKU));
Paolo Bonzinic4ad77e2017-11-13 14:23:59 +010010030 cr4_fixed1_update(X86_CR4_UMIP, ecx, bit(X86_FEATURE_UMIP));
David Matlack8322ebb2016-11-29 18:14:09 -080010031
10032#undef cr4_fixed1_update
10033}
10034
Sheng Yang0e851882009-12-18 16:48:46 +080010035static void vmx_cpuid_update(struct kvm_vcpu *vcpu)
10036{
Sheng Yang4e47c7a2009-12-18 16:48:47 +080010037 struct vcpu_vmx *vmx = to_vmx(vcpu);
Sheng Yang4e47c7a2009-12-18 16:48:47 +080010038
Paolo Bonzini80154d72017-08-24 13:55:35 +020010039 if (cpu_has_secondary_exec_ctrls()) {
10040 vmx_compute_secondary_exec_control(vmx);
10041 vmcs_set_secondary_exec_control(vmx->secondary_exec_control);
Sheng Yang4e47c7a2009-12-18 16:48:47 +080010042 }
Mao, Junjiead756a12012-07-02 01:18:48 +000010043
Haozhong Zhang37e4c992016-06-22 14:59:55 +080010044 if (nested_vmx_allowed(vcpu))
10045 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits |=
10046 FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
10047 else
10048 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits &=
10049 ~FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
David Matlack8322ebb2016-11-29 18:14:09 -080010050
10051 if (nested_vmx_allowed(vcpu))
10052 nested_vmx_cr_fixed1_bits_update(vcpu);
Sheng Yang0e851882009-12-18 16:48:46 +080010053}
10054
Joerg Roedeld4330ef2010-04-22 12:33:11 +020010055static void vmx_set_supported_cpuid(u32 func, struct kvm_cpuid_entry2 *entry)
10056{
Nadav Har'El7b8050f2011-05-25 23:16:10 +030010057 if (func == 1 && nested)
10058 entry->ecx |= bit(X86_FEATURE_VMX);
Joerg Roedeld4330ef2010-04-22 12:33:11 +020010059}
10060
Yang Zhang25d92082013-08-06 12:00:32 +030010061static void nested_ept_inject_page_fault(struct kvm_vcpu *vcpu,
10062 struct x86_exception *fault)
10063{
Jan Kiszka533558b2014-01-04 18:47:20 +010010064 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Bandan Dasc5f983f2017-05-05 15:25:14 -040010065 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jan Kiszka533558b2014-01-04 18:47:20 +010010066 u32 exit_reason;
Bandan Dasc5f983f2017-05-05 15:25:14 -040010067 unsigned long exit_qualification = vcpu->arch.exit_qualification;
Yang Zhang25d92082013-08-06 12:00:32 +030010068
Bandan Dasc5f983f2017-05-05 15:25:14 -040010069 if (vmx->nested.pml_full) {
10070 exit_reason = EXIT_REASON_PML_FULL;
10071 vmx->nested.pml_full = false;
10072 exit_qualification &= INTR_INFO_UNBLOCK_NMI;
10073 } else if (fault->error_code & PFERR_RSVD_MASK)
Jan Kiszka533558b2014-01-04 18:47:20 +010010074 exit_reason = EXIT_REASON_EPT_MISCONFIG;
Yang Zhang25d92082013-08-06 12:00:32 +030010075 else
Jan Kiszka533558b2014-01-04 18:47:20 +010010076 exit_reason = EXIT_REASON_EPT_VIOLATION;
Bandan Dasc5f983f2017-05-05 15:25:14 -040010077
10078 nested_vmx_vmexit(vcpu, exit_reason, 0, exit_qualification);
Yang Zhang25d92082013-08-06 12:00:32 +030010079 vmcs12->guest_physical_address = fault->address;
10080}
10081
Peter Feiner995f00a2017-06-30 17:26:32 -070010082static bool nested_ept_ad_enabled(struct kvm_vcpu *vcpu)
10083{
David Hildenbrandbb97a012017-08-10 23:15:28 +020010084 return nested_ept_get_cr3(vcpu) & VMX_EPTP_AD_ENABLE_BIT;
Peter Feiner995f00a2017-06-30 17:26:32 -070010085}
10086
Nadav Har'El155a97a2013-08-05 11:07:16 +030010087/* Callbacks for nested_ept_init_mmu_context: */
10088
10089static unsigned long nested_ept_get_cr3(struct kvm_vcpu *vcpu)
10090{
10091 /* return the page table to be shadowed - in our case, EPT12 */
10092 return get_vmcs12(vcpu)->ept_pointer;
10093}
10094
Paolo Bonziniae1e2d12017-03-30 11:55:30 +020010095static int nested_ept_init_mmu_context(struct kvm_vcpu *vcpu)
Nadav Har'El155a97a2013-08-05 11:07:16 +030010096{
Paolo Bonziniad896af2013-10-02 16:56:14 +020010097 WARN_ON(mmu_is_nested(vcpu));
David Hildenbranda057e0e2017-08-10 23:36:54 +020010098 if (!valid_ept_address(vcpu, nested_ept_get_cr3(vcpu)))
Paolo Bonziniae1e2d12017-03-30 11:55:30 +020010099 return 1;
10100
10101 kvm_mmu_unload(vcpu);
Paolo Bonziniad896af2013-10-02 16:56:14 +020010102 kvm_init_shadow_ept_mmu(vcpu,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010010103 to_vmx(vcpu)->nested.msrs.ept_caps &
Paolo Bonziniae1e2d12017-03-30 11:55:30 +020010104 VMX_EPT_EXECUTE_ONLY_BIT,
David Hildenbranda057e0e2017-08-10 23:36:54 +020010105 nested_ept_ad_enabled(vcpu));
Nadav Har'El155a97a2013-08-05 11:07:16 +030010106 vcpu->arch.mmu.set_cr3 = vmx_set_cr3;
10107 vcpu->arch.mmu.get_cr3 = nested_ept_get_cr3;
10108 vcpu->arch.mmu.inject_page_fault = nested_ept_inject_page_fault;
10109
10110 vcpu->arch.walk_mmu = &vcpu->arch.nested_mmu;
Paolo Bonziniae1e2d12017-03-30 11:55:30 +020010111 return 0;
Nadav Har'El155a97a2013-08-05 11:07:16 +030010112}
10113
10114static void nested_ept_uninit_mmu_context(struct kvm_vcpu *vcpu)
10115{
10116 vcpu->arch.walk_mmu = &vcpu->arch.mmu;
10117}
10118
Eugene Korenevsky19d5f102014-12-16 22:35:53 +030010119static bool nested_vmx_is_page_fault_vmexit(struct vmcs12 *vmcs12,
10120 u16 error_code)
10121{
10122 bool inequality, bit;
10123
10124 bit = (vmcs12->exception_bitmap & (1u << PF_VECTOR)) != 0;
10125 inequality =
10126 (error_code & vmcs12->page_fault_error_code_mask) !=
10127 vmcs12->page_fault_error_code_match;
10128 return inequality ^ bit;
10129}
10130
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +030010131static void vmx_inject_page_fault_nested(struct kvm_vcpu *vcpu,
10132 struct x86_exception *fault)
10133{
10134 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
10135
10136 WARN_ON(!is_guest_mode(vcpu));
10137
Wanpeng Li305d0ab2017-09-28 18:16:44 -070010138 if (nested_vmx_is_page_fault_vmexit(vmcs12, fault->error_code) &&
10139 !to_vmx(vcpu)->nested.nested_run_pending) {
Paolo Bonzinib96fb432017-07-27 12:29:32 +020010140 vmcs12->vm_exit_intr_error_code = fault->error_code;
10141 nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI,
10142 PF_VECTOR | INTR_TYPE_HARD_EXCEPTION |
10143 INTR_INFO_DELIVER_CODE_MASK | INTR_INFO_VALID_MASK,
10144 fault->address);
Paolo Bonzini7313c692017-07-27 10:31:25 +020010145 } else {
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +030010146 kvm_inject_page_fault(vcpu, fault);
Paolo Bonzini7313c692017-07-27 10:31:25 +020010147 }
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +030010148}
10149
Paolo Bonzinic9923842017-12-13 14:16:30 +010010150static inline bool nested_vmx_prepare_msr_bitmap(struct kvm_vcpu *vcpu,
10151 struct vmcs12 *vmcs12);
Jim Mattson6beb7bd2016-11-30 12:03:45 -080010152
10153static void nested_get_vmcs12_pages(struct kvm_vcpu *vcpu,
Wanpeng Lia2bcba52014-08-21 19:46:49 +080010154 struct vmcs12 *vmcs12)
10155{
10156 struct vcpu_vmx *vmx = to_vmx(vcpu);
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020010157 struct page *page;
Jim Mattson6beb7bd2016-11-30 12:03:45 -080010158 u64 hpa;
Wanpeng Lia2bcba52014-08-21 19:46:49 +080010159
10160 if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) {
Wanpeng Lia2bcba52014-08-21 19:46:49 +080010161 /*
10162 * Translate L1 physical address to host physical
10163 * address for vmcs02. Keep the page pinned, so this
10164 * physical address remains valid. We keep a reference
10165 * to it so we can release it later.
10166 */
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020010167 if (vmx->nested.apic_access_page) { /* shouldn't happen */
David Hildenbrand53a70da2017-08-03 18:11:05 +020010168 kvm_release_page_dirty(vmx->nested.apic_access_page);
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020010169 vmx->nested.apic_access_page = NULL;
10170 }
10171 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->apic_access_addr);
Jim Mattson6beb7bd2016-11-30 12:03:45 -080010172 /*
10173 * If translation failed, no matter: This feature asks
10174 * to exit when accessing the given address, and if it
10175 * can never be accessed, this feature won't do
10176 * anything anyway.
10177 */
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020010178 if (!is_error_page(page)) {
10179 vmx->nested.apic_access_page = page;
Jim Mattson6beb7bd2016-11-30 12:03:45 -080010180 hpa = page_to_phys(vmx->nested.apic_access_page);
10181 vmcs_write64(APIC_ACCESS_ADDR, hpa);
10182 } else {
10183 vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL,
10184 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES);
10185 }
10186 } else if (!(nested_cpu_has_virt_x2apic_mode(vmcs12)) &&
10187 cpu_need_virtualize_apic_accesses(&vmx->vcpu)) {
10188 vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL,
10189 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES);
10190 kvm_vcpu_reload_apic_access_page(vcpu);
Wanpeng Lia2bcba52014-08-21 19:46:49 +080010191 }
Wanpeng Lia7c0b072014-08-21 19:46:50 +080010192
10193 if (nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW)) {
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020010194 if (vmx->nested.virtual_apic_page) { /* shouldn't happen */
David Hildenbrand53a70da2017-08-03 18:11:05 +020010195 kvm_release_page_dirty(vmx->nested.virtual_apic_page);
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020010196 vmx->nested.virtual_apic_page = NULL;
10197 }
10198 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->virtual_apic_page_addr);
Wanpeng Lia7c0b072014-08-21 19:46:50 +080010199
10200 /*
Jim Mattson6beb7bd2016-11-30 12:03:45 -080010201 * If translation failed, VM entry will fail because
10202 * prepare_vmcs02 set VIRTUAL_APIC_PAGE_ADDR to -1ull.
10203 * Failing the vm entry is _not_ what the processor
10204 * does but it's basically the only possibility we
10205 * have. We could still enter the guest if CR8 load
10206 * exits are enabled, CR8 store exits are enabled, and
10207 * virtualize APIC access is disabled; in this case
10208 * the processor would never use the TPR shadow and we
10209 * could simply clear the bit from the execution
10210 * control. But such a configuration is useless, so
10211 * let's keep the code simple.
Wanpeng Lia7c0b072014-08-21 19:46:50 +080010212 */
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020010213 if (!is_error_page(page)) {
10214 vmx->nested.virtual_apic_page = page;
Jim Mattson6beb7bd2016-11-30 12:03:45 -080010215 hpa = page_to_phys(vmx->nested.virtual_apic_page);
10216 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, hpa);
10217 }
Wanpeng Lia7c0b072014-08-21 19:46:50 +080010218 }
10219
Wincy Van705699a2015-02-03 23:58:17 +080010220 if (nested_cpu_has_posted_intr(vmcs12)) {
Wincy Van705699a2015-02-03 23:58:17 +080010221 if (vmx->nested.pi_desc_page) { /* shouldn't happen */
10222 kunmap(vmx->nested.pi_desc_page);
David Hildenbrand53a70da2017-08-03 18:11:05 +020010223 kvm_release_page_dirty(vmx->nested.pi_desc_page);
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020010224 vmx->nested.pi_desc_page = NULL;
Wincy Van705699a2015-02-03 23:58:17 +080010225 }
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020010226 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->posted_intr_desc_addr);
10227 if (is_error_page(page))
Jim Mattson6beb7bd2016-11-30 12:03:45 -080010228 return;
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020010229 vmx->nested.pi_desc_page = page;
10230 vmx->nested.pi_desc = kmap(vmx->nested.pi_desc_page);
Wincy Van705699a2015-02-03 23:58:17 +080010231 vmx->nested.pi_desc =
10232 (struct pi_desc *)((void *)vmx->nested.pi_desc +
10233 (unsigned long)(vmcs12->posted_intr_desc_addr &
10234 (PAGE_SIZE - 1)));
Jim Mattson6beb7bd2016-11-30 12:03:45 -080010235 vmcs_write64(POSTED_INTR_DESC_ADDR,
10236 page_to_phys(vmx->nested.pi_desc_page) +
10237 (unsigned long)(vmcs12->posted_intr_desc_addr &
10238 (PAGE_SIZE - 1)));
Wincy Van705699a2015-02-03 23:58:17 +080010239 }
Linus Torvaldsd4667ca2018-02-14 17:02:15 -080010240 if (nested_vmx_prepare_msr_bitmap(vcpu, vmcs12))
KarimAllah Ahmed3712caeb2018-02-10 23:39:26 +000010241 vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL,
10242 CPU_BASED_USE_MSR_BITMAPS);
Jim Mattson6beb7bd2016-11-30 12:03:45 -080010243 else
10244 vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL,
10245 CPU_BASED_USE_MSR_BITMAPS);
Wanpeng Lia2bcba52014-08-21 19:46:49 +080010246}
10247
Jan Kiszkaf41245002014-03-07 20:03:13 +010010248static void vmx_start_preemption_timer(struct kvm_vcpu *vcpu)
10249{
10250 u64 preemption_timeout = get_vmcs12(vcpu)->vmx_preemption_timer_value;
10251 struct vcpu_vmx *vmx = to_vmx(vcpu);
10252
10253 if (vcpu->arch.virtual_tsc_khz == 0)
10254 return;
10255
10256 /* Make sure short timeouts reliably trigger an immediate vmexit.
10257 * hrtimer_start does not guarantee this. */
10258 if (preemption_timeout <= 1) {
10259 vmx_preemption_timer_fn(&vmx->nested.preemption_timer);
10260 return;
10261 }
10262
10263 preemption_timeout <<= VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE;
10264 preemption_timeout *= 1000000;
10265 do_div(preemption_timeout, vcpu->arch.virtual_tsc_khz);
10266 hrtimer_start(&vmx->nested.preemption_timer,
10267 ns_to_ktime(preemption_timeout), HRTIMER_MODE_REL);
10268}
10269
Jim Mattson56a20512017-07-06 16:33:06 -070010270static int nested_vmx_check_io_bitmap_controls(struct kvm_vcpu *vcpu,
10271 struct vmcs12 *vmcs12)
10272{
10273 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_IO_BITMAPS))
10274 return 0;
10275
10276 if (!page_address_valid(vcpu, vmcs12->io_bitmap_a) ||
10277 !page_address_valid(vcpu, vmcs12->io_bitmap_b))
10278 return -EINVAL;
10279
10280 return 0;
10281}
10282
Wincy Van3af18d92015-02-03 23:49:31 +080010283static int nested_vmx_check_msr_bitmap_controls(struct kvm_vcpu *vcpu,
10284 struct vmcs12 *vmcs12)
10285{
Wincy Van3af18d92015-02-03 23:49:31 +080010286 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS))
10287 return 0;
10288
Jim Mattson5fa99cb2017-07-06 16:33:07 -070010289 if (!page_address_valid(vcpu, vmcs12->msr_bitmap))
Wincy Van3af18d92015-02-03 23:49:31 +080010290 return -EINVAL;
10291
10292 return 0;
10293}
10294
Jim Mattson712b12d2017-08-24 13:24:47 -070010295static int nested_vmx_check_tpr_shadow_controls(struct kvm_vcpu *vcpu,
10296 struct vmcs12 *vmcs12)
10297{
10298 if (!nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW))
10299 return 0;
10300
10301 if (!page_address_valid(vcpu, vmcs12->virtual_apic_page_addr))
10302 return -EINVAL;
10303
10304 return 0;
10305}
10306
Wincy Van3af18d92015-02-03 23:49:31 +080010307/*
10308 * Merge L0's and L1's MSR bitmap, return false to indicate that
10309 * we do not use the hardware.
10310 */
Paolo Bonzinic9923842017-12-13 14:16:30 +010010311static inline bool nested_vmx_prepare_msr_bitmap(struct kvm_vcpu *vcpu,
10312 struct vmcs12 *vmcs12)
Wincy Van3af18d92015-02-03 23:49:31 +080010313{
Wincy Van82f0dd42015-02-03 23:57:18 +080010314 int msr;
Wincy Vanf2b93282015-02-03 23:56:03 +080010315 struct page *page;
Radim Krčmářd048c092016-08-08 20:16:22 +020010316 unsigned long *msr_bitmap_l1;
Paolo Bonzini904e14f2018-01-16 16:51:18 +010010317 unsigned long *msr_bitmap_l0 = to_vmx(vcpu)->nested.vmcs02.msr_bitmap;
Ashok Raj15d45072018-02-01 22:59:43 +010010318 /*
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010010319 * pred_cmd & spec_ctrl are trying to verify two things:
Ashok Raj15d45072018-02-01 22:59:43 +010010320 *
10321 * 1. L0 gave a permission to L1 to actually passthrough the MSR. This
10322 * ensures that we do not accidentally generate an L02 MSR bitmap
10323 * from the L12 MSR bitmap that is too permissive.
10324 * 2. That L1 or L2s have actually used the MSR. This avoids
10325 * unnecessarily merging of the bitmap if the MSR is unused. This
10326 * works properly because we only update the L01 MSR bitmap lazily.
10327 * So even if L0 should pass L1 these MSRs, the L01 bitmap is only
10328 * updated to reflect this when L1 (or its L2s) actually write to
10329 * the MSR.
10330 */
KarimAllah Ahmed206587a2018-02-10 23:39:25 +000010331 bool pred_cmd = !msr_write_intercepted_l01(vcpu, MSR_IA32_PRED_CMD);
10332 bool spec_ctrl = !msr_write_intercepted_l01(vcpu, MSR_IA32_SPEC_CTRL);
Wincy Vanf2b93282015-02-03 23:56:03 +080010333
Paolo Bonzinic9923842017-12-13 14:16:30 +010010334 /* Nothing to do if the MSR bitmap is not in use. */
10335 if (!cpu_has_vmx_msr_bitmap() ||
10336 !nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS))
10337 return false;
10338
Ashok Raj15d45072018-02-01 22:59:43 +010010339 if (!nested_cpu_has_virt_x2apic_mode(vmcs12) &&
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010010340 !pred_cmd && !spec_ctrl)
Wincy Vanf2b93282015-02-03 23:56:03 +080010341 return false;
10342
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020010343 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->msr_bitmap);
10344 if (is_error_page(page))
Wincy Vanf2b93282015-02-03 23:56:03 +080010345 return false;
Paolo Bonzinic9923842017-12-13 14:16:30 +010010346
Radim Krčmářd048c092016-08-08 20:16:22 +020010347 msr_bitmap_l1 = (unsigned long *)kmap(page);
Paolo Bonzinic9923842017-12-13 14:16:30 +010010348 if (nested_cpu_has_apic_reg_virt(vmcs12)) {
10349 /*
10350 * L0 need not intercept reads for MSRs between 0x800 and 0x8ff, it
10351 * just lets the processor take the value from the virtual-APIC page;
10352 * take those 256 bits directly from the L1 bitmap.
10353 */
10354 for (msr = 0x800; msr <= 0x8ff; msr += BITS_PER_LONG) {
10355 unsigned word = msr / BITS_PER_LONG;
10356 msr_bitmap_l0[word] = msr_bitmap_l1[word];
10357 msr_bitmap_l0[word + (0x800 / sizeof(long))] = ~0;
Wincy Van608406e2015-02-03 23:57:51 +080010358 }
Paolo Bonzinic9923842017-12-13 14:16:30 +010010359 } else {
10360 for (msr = 0x800; msr <= 0x8ff; msr += BITS_PER_LONG) {
10361 unsigned word = msr / BITS_PER_LONG;
10362 msr_bitmap_l0[word] = ~0;
10363 msr_bitmap_l0[word + (0x800 / sizeof(long))] = ~0;
10364 }
10365 }
10366
10367 nested_vmx_disable_intercept_for_msr(
10368 msr_bitmap_l1, msr_bitmap_l0,
Paolo Bonzinid7231e72017-12-21 00:47:55 +010010369 X2APIC_MSR(APIC_TASKPRI),
Paolo Bonzinic9923842017-12-13 14:16:30 +010010370 MSR_TYPE_W);
10371
10372 if (nested_cpu_has_vid(vmcs12)) {
10373 nested_vmx_disable_intercept_for_msr(
10374 msr_bitmap_l1, msr_bitmap_l0,
Paolo Bonzinid7231e72017-12-21 00:47:55 +010010375 X2APIC_MSR(APIC_EOI),
Paolo Bonzinic9923842017-12-13 14:16:30 +010010376 MSR_TYPE_W);
10377 nested_vmx_disable_intercept_for_msr(
10378 msr_bitmap_l1, msr_bitmap_l0,
Paolo Bonzinid7231e72017-12-21 00:47:55 +010010379 X2APIC_MSR(APIC_SELF_IPI),
Paolo Bonzinic9923842017-12-13 14:16:30 +010010380 MSR_TYPE_W);
Wincy Van82f0dd42015-02-03 23:57:18 +080010381 }
Ashok Raj15d45072018-02-01 22:59:43 +010010382
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010010383 if (spec_ctrl)
10384 nested_vmx_disable_intercept_for_msr(
10385 msr_bitmap_l1, msr_bitmap_l0,
10386 MSR_IA32_SPEC_CTRL,
10387 MSR_TYPE_R | MSR_TYPE_W);
10388
Ashok Raj15d45072018-02-01 22:59:43 +010010389 if (pred_cmd)
10390 nested_vmx_disable_intercept_for_msr(
10391 msr_bitmap_l1, msr_bitmap_l0,
10392 MSR_IA32_PRED_CMD,
10393 MSR_TYPE_W);
10394
Wincy Vanf2b93282015-02-03 23:56:03 +080010395 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +020010396 kvm_release_page_clean(page);
Wincy Vanf2b93282015-02-03 23:56:03 +080010397
10398 return true;
10399}
10400
10401static int nested_vmx_check_apicv_controls(struct kvm_vcpu *vcpu,
10402 struct vmcs12 *vmcs12)
10403{
Wincy Van82f0dd42015-02-03 23:57:18 +080010404 if (!nested_cpu_has_virt_x2apic_mode(vmcs12) &&
Wincy Van608406e2015-02-03 23:57:51 +080010405 !nested_cpu_has_apic_reg_virt(vmcs12) &&
Wincy Van705699a2015-02-03 23:58:17 +080010406 !nested_cpu_has_vid(vmcs12) &&
10407 !nested_cpu_has_posted_intr(vmcs12))
Wincy Vanf2b93282015-02-03 23:56:03 +080010408 return 0;
10409
10410 /*
10411 * If virtualize x2apic mode is enabled,
10412 * virtualize apic access must be disabled.
10413 */
Wincy Van82f0dd42015-02-03 23:57:18 +080010414 if (nested_cpu_has_virt_x2apic_mode(vmcs12) &&
10415 nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES))
Wincy Vanf2b93282015-02-03 23:56:03 +080010416 return -EINVAL;
10417
Wincy Van608406e2015-02-03 23:57:51 +080010418 /*
10419 * If virtual interrupt delivery is enabled,
10420 * we must exit on external interrupts.
10421 */
10422 if (nested_cpu_has_vid(vmcs12) &&
10423 !nested_exit_on_intr(vcpu))
10424 return -EINVAL;
10425
Wincy Van705699a2015-02-03 23:58:17 +080010426 /*
10427 * bits 15:8 should be zero in posted_intr_nv,
10428 * the descriptor address has been already checked
10429 * in nested_get_vmcs12_pages.
10430 */
10431 if (nested_cpu_has_posted_intr(vmcs12) &&
10432 (!nested_cpu_has_vid(vmcs12) ||
10433 !nested_exit_intr_ack_set(vcpu) ||
10434 vmcs12->posted_intr_nv & 0xff00))
10435 return -EINVAL;
10436
Wincy Vanf2b93282015-02-03 23:56:03 +080010437 /* tpr shadow is needed by all apicv features. */
10438 if (!nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW))
10439 return -EINVAL;
10440
10441 return 0;
Wincy Van3af18d92015-02-03 23:49:31 +080010442}
10443
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010444static int nested_vmx_check_msr_switch(struct kvm_vcpu *vcpu,
10445 unsigned long count_field,
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030010446 unsigned long addr_field)
Wincy Vanff651cb2014-12-11 08:52:58 +030010447{
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030010448 int maxphyaddr;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010449 u64 count, addr;
10450
10451 if (vmcs12_read_any(vcpu, count_field, &count) ||
10452 vmcs12_read_any(vcpu, addr_field, &addr)) {
10453 WARN_ON(1);
10454 return -EINVAL;
10455 }
10456 if (count == 0)
10457 return 0;
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030010458 maxphyaddr = cpuid_maxphyaddr(vcpu);
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010459 if (!IS_ALIGNED(addr, 16) || addr >> maxphyaddr ||
10460 (addr + count * sizeof(struct vmx_msr_entry) - 1) >> maxphyaddr) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020010461 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010462 "nVMX: invalid MSR switch (0x%lx, %d, %llu, 0x%08llx)",
10463 addr_field, maxphyaddr, count, addr);
10464 return -EINVAL;
10465 }
10466 return 0;
10467}
10468
10469static int nested_vmx_check_msr_switch_controls(struct kvm_vcpu *vcpu,
10470 struct vmcs12 *vmcs12)
10471{
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010472 if (vmcs12->vm_exit_msr_load_count == 0 &&
10473 vmcs12->vm_exit_msr_store_count == 0 &&
10474 vmcs12->vm_entry_msr_load_count == 0)
10475 return 0; /* Fast path */
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010476 if (nested_vmx_check_msr_switch(vcpu, VM_EXIT_MSR_LOAD_COUNT,
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030010477 VM_EXIT_MSR_LOAD_ADDR) ||
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010478 nested_vmx_check_msr_switch(vcpu, VM_EXIT_MSR_STORE_COUNT,
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030010479 VM_EXIT_MSR_STORE_ADDR) ||
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010480 nested_vmx_check_msr_switch(vcpu, VM_ENTRY_MSR_LOAD_COUNT,
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030010481 VM_ENTRY_MSR_LOAD_ADDR))
Wincy Vanff651cb2014-12-11 08:52:58 +030010482 return -EINVAL;
10483 return 0;
10484}
10485
Bandan Dasc5f983f2017-05-05 15:25:14 -040010486static int nested_vmx_check_pml_controls(struct kvm_vcpu *vcpu,
10487 struct vmcs12 *vmcs12)
10488{
10489 u64 address = vmcs12->pml_address;
10490 int maxphyaddr = cpuid_maxphyaddr(vcpu);
10491
10492 if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_PML)) {
10493 if (!nested_cpu_has_ept(vmcs12) ||
10494 !IS_ALIGNED(address, 4096) ||
10495 address >> maxphyaddr)
10496 return -EINVAL;
10497 }
10498
10499 return 0;
10500}
10501
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010502static int nested_vmx_msr_check_common(struct kvm_vcpu *vcpu,
10503 struct vmx_msr_entry *e)
10504{
10505 /* x2APIC MSR accesses are not allowed */
Jan Kiszka8a9781f2015-05-04 08:32:32 +020010506 if (vcpu->arch.apic_base & X2APIC_ENABLE && e->index >> 8 == 0x8)
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010507 return -EINVAL;
10508 if (e->index == MSR_IA32_UCODE_WRITE || /* SDM Table 35-2 */
10509 e->index == MSR_IA32_UCODE_REV)
10510 return -EINVAL;
10511 if (e->reserved != 0)
10512 return -EINVAL;
10513 return 0;
10514}
10515
10516static int nested_vmx_load_msr_check(struct kvm_vcpu *vcpu,
10517 struct vmx_msr_entry *e)
Wincy Vanff651cb2014-12-11 08:52:58 +030010518{
10519 if (e->index == MSR_FS_BASE ||
10520 e->index == MSR_GS_BASE ||
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010521 e->index == MSR_IA32_SMM_MONITOR_CTL || /* SMM is not supported */
10522 nested_vmx_msr_check_common(vcpu, e))
10523 return -EINVAL;
10524 return 0;
10525}
10526
10527static int nested_vmx_store_msr_check(struct kvm_vcpu *vcpu,
10528 struct vmx_msr_entry *e)
10529{
10530 if (e->index == MSR_IA32_SMBASE || /* SMM is not supported */
10531 nested_vmx_msr_check_common(vcpu, e))
Wincy Vanff651cb2014-12-11 08:52:58 +030010532 return -EINVAL;
10533 return 0;
10534}
10535
10536/*
10537 * Load guest's/host's msr at nested entry/exit.
10538 * return 0 for success, entry index for failure.
10539 */
10540static u32 nested_vmx_load_msr(struct kvm_vcpu *vcpu, u64 gpa, u32 count)
10541{
10542 u32 i;
10543 struct vmx_msr_entry e;
10544 struct msr_data msr;
10545
10546 msr.host_initiated = false;
10547 for (i = 0; i < count; i++) {
Paolo Bonzini54bf36a2015-04-08 15:39:23 +020010548 if (kvm_vcpu_read_guest(vcpu, gpa + i * sizeof(e),
10549 &e, sizeof(e))) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020010550 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010551 "%s cannot read MSR entry (%u, 0x%08llx)\n",
10552 __func__, i, gpa + i * sizeof(e));
Wincy Vanff651cb2014-12-11 08:52:58 +030010553 goto fail;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010554 }
10555 if (nested_vmx_load_msr_check(vcpu, &e)) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020010556 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010557 "%s check failed (%u, 0x%x, 0x%x)\n",
10558 __func__, i, e.index, e.reserved);
10559 goto fail;
10560 }
Wincy Vanff651cb2014-12-11 08:52:58 +030010561 msr.index = e.index;
10562 msr.data = e.value;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010563 if (kvm_set_msr(vcpu, &msr)) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020010564 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010565 "%s cannot write MSR (%u, 0x%x, 0x%llx)\n",
10566 __func__, i, e.index, e.value);
Wincy Vanff651cb2014-12-11 08:52:58 +030010567 goto fail;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010568 }
Wincy Vanff651cb2014-12-11 08:52:58 +030010569 }
10570 return 0;
10571fail:
10572 return i + 1;
10573}
10574
10575static int nested_vmx_store_msr(struct kvm_vcpu *vcpu, u64 gpa, u32 count)
10576{
10577 u32 i;
10578 struct vmx_msr_entry e;
10579
10580 for (i = 0; i < count; i++) {
Paolo Bonzini609e36d2015-04-08 15:30:38 +020010581 struct msr_data msr_info;
Paolo Bonzini54bf36a2015-04-08 15:39:23 +020010582 if (kvm_vcpu_read_guest(vcpu,
10583 gpa + i * sizeof(e),
10584 &e, 2 * sizeof(u32))) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020010585 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010586 "%s cannot read MSR entry (%u, 0x%08llx)\n",
10587 __func__, i, gpa + i * sizeof(e));
Wincy Vanff651cb2014-12-11 08:52:58 +030010588 return -EINVAL;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010589 }
10590 if (nested_vmx_store_msr_check(vcpu, &e)) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020010591 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010592 "%s check failed (%u, 0x%x, 0x%x)\n",
10593 __func__, i, e.index, e.reserved);
Wincy Vanff651cb2014-12-11 08:52:58 +030010594 return -EINVAL;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010595 }
Paolo Bonzini609e36d2015-04-08 15:30:38 +020010596 msr_info.host_initiated = false;
10597 msr_info.index = e.index;
10598 if (kvm_get_msr(vcpu, &msr_info)) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020010599 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010600 "%s cannot read MSR (%u, 0x%x)\n",
10601 __func__, i, e.index);
10602 return -EINVAL;
10603 }
Paolo Bonzini54bf36a2015-04-08 15:39:23 +020010604 if (kvm_vcpu_write_guest(vcpu,
10605 gpa + i * sizeof(e) +
10606 offsetof(struct vmx_msr_entry, value),
10607 &msr_info.data, sizeof(msr_info.data))) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020010608 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010609 "%s cannot write MSR (%u, 0x%x, 0x%llx)\n",
Paolo Bonzini609e36d2015-04-08 15:30:38 +020010610 __func__, i, e.index, msr_info.data);
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010611 return -EINVAL;
10612 }
Wincy Vanff651cb2014-12-11 08:52:58 +030010613 }
10614 return 0;
10615}
10616
Ladi Prosek1dc35da2016-11-30 16:03:11 +010010617static bool nested_cr3_valid(struct kvm_vcpu *vcpu, unsigned long val)
10618{
10619 unsigned long invalid_mask;
10620
10621 invalid_mask = (~0ULL) << cpuid_maxphyaddr(vcpu);
10622 return (val & invalid_mask) == 0;
10623}
10624
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010625/*
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010010626 * Load guest's/host's cr3 at nested entry/exit. nested_ept is true if we are
10627 * emulating VM entry into a guest with EPT enabled.
10628 * Returns 0 on success, 1 on failure. Invalid state exit qualification code
10629 * is assigned to entry_failure_code on failure.
10630 */
10631static int nested_vmx_load_cr3(struct kvm_vcpu *vcpu, unsigned long cr3, bool nested_ept,
Jim Mattsonca0bde22016-11-30 12:03:46 -080010632 u32 *entry_failure_code)
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010010633{
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010010634 if (cr3 != kvm_read_cr3(vcpu) || (!nested_ept && pdptrs_changed(vcpu))) {
Ladi Prosek1dc35da2016-11-30 16:03:11 +010010635 if (!nested_cr3_valid(vcpu, cr3)) {
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010010636 *entry_failure_code = ENTRY_FAIL_DEFAULT;
10637 return 1;
10638 }
10639
10640 /*
10641 * If PAE paging and EPT are both on, CR3 is not used by the CPU and
10642 * must not be dereferenced.
10643 */
10644 if (!is_long_mode(vcpu) && is_pae(vcpu) && is_paging(vcpu) &&
10645 !nested_ept) {
10646 if (!load_pdptrs(vcpu, vcpu->arch.walk_mmu, cr3)) {
10647 *entry_failure_code = ENTRY_FAIL_PDPTE;
10648 return 1;
10649 }
10650 }
10651
10652 vcpu->arch.cr3 = cr3;
10653 __set_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail);
10654 }
10655
10656 kvm_mmu_reset_context(vcpu);
10657 return 0;
10658}
10659
Paolo Bonzini74a497f2017-12-20 13:55:39 +010010660static void prepare_vmcs02_full(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12,
10661 bool from_vmentry)
10662{
Paolo Bonzini8665c3f2017-12-20 13:56:53 +010010663 struct vcpu_vmx *vmx = to_vmx(vcpu);
10664
10665 vmcs_write16(GUEST_ES_SELECTOR, vmcs12->guest_es_selector);
10666 vmcs_write16(GUEST_SS_SELECTOR, vmcs12->guest_ss_selector);
10667 vmcs_write16(GUEST_DS_SELECTOR, vmcs12->guest_ds_selector);
10668 vmcs_write16(GUEST_FS_SELECTOR, vmcs12->guest_fs_selector);
10669 vmcs_write16(GUEST_GS_SELECTOR, vmcs12->guest_gs_selector);
10670 vmcs_write16(GUEST_LDTR_SELECTOR, vmcs12->guest_ldtr_selector);
10671 vmcs_write16(GUEST_TR_SELECTOR, vmcs12->guest_tr_selector);
10672 vmcs_write32(GUEST_ES_LIMIT, vmcs12->guest_es_limit);
10673 vmcs_write32(GUEST_SS_LIMIT, vmcs12->guest_ss_limit);
10674 vmcs_write32(GUEST_DS_LIMIT, vmcs12->guest_ds_limit);
10675 vmcs_write32(GUEST_FS_LIMIT, vmcs12->guest_fs_limit);
10676 vmcs_write32(GUEST_GS_LIMIT, vmcs12->guest_gs_limit);
10677 vmcs_write32(GUEST_LDTR_LIMIT, vmcs12->guest_ldtr_limit);
10678 vmcs_write32(GUEST_TR_LIMIT, vmcs12->guest_tr_limit);
10679 vmcs_write32(GUEST_GDTR_LIMIT, vmcs12->guest_gdtr_limit);
10680 vmcs_write32(GUEST_IDTR_LIMIT, vmcs12->guest_idtr_limit);
10681 vmcs_write32(GUEST_ES_AR_BYTES, vmcs12->guest_es_ar_bytes);
10682 vmcs_write32(GUEST_SS_AR_BYTES, vmcs12->guest_ss_ar_bytes);
10683 vmcs_write32(GUEST_DS_AR_BYTES, vmcs12->guest_ds_ar_bytes);
10684 vmcs_write32(GUEST_FS_AR_BYTES, vmcs12->guest_fs_ar_bytes);
10685 vmcs_write32(GUEST_GS_AR_BYTES, vmcs12->guest_gs_ar_bytes);
10686 vmcs_write32(GUEST_LDTR_AR_BYTES, vmcs12->guest_ldtr_ar_bytes);
10687 vmcs_write32(GUEST_TR_AR_BYTES, vmcs12->guest_tr_ar_bytes);
10688 vmcs_writel(GUEST_SS_BASE, vmcs12->guest_ss_base);
10689 vmcs_writel(GUEST_DS_BASE, vmcs12->guest_ds_base);
10690 vmcs_writel(GUEST_FS_BASE, vmcs12->guest_fs_base);
10691 vmcs_writel(GUEST_GS_BASE, vmcs12->guest_gs_base);
10692 vmcs_writel(GUEST_LDTR_BASE, vmcs12->guest_ldtr_base);
10693 vmcs_writel(GUEST_TR_BASE, vmcs12->guest_tr_base);
10694 vmcs_writel(GUEST_GDTR_BASE, vmcs12->guest_gdtr_base);
10695 vmcs_writel(GUEST_IDTR_BASE, vmcs12->guest_idtr_base);
Paolo Bonzini25a2e4f2017-12-20 14:05:21 +010010696
10697 vmcs_write32(GUEST_SYSENTER_CS, vmcs12->guest_sysenter_cs);
10698 vmcs_writel(GUEST_PENDING_DBG_EXCEPTIONS,
10699 vmcs12->guest_pending_dbg_exceptions);
10700 vmcs_writel(GUEST_SYSENTER_ESP, vmcs12->guest_sysenter_esp);
10701 vmcs_writel(GUEST_SYSENTER_EIP, vmcs12->guest_sysenter_eip);
10702
10703 if (nested_cpu_has_xsaves(vmcs12))
10704 vmcs_write64(XSS_EXIT_BITMAP, vmcs12->xss_exit_bitmap);
10705 vmcs_write64(VMCS_LINK_POINTER, -1ull);
10706
10707 if (cpu_has_vmx_posted_intr())
10708 vmcs_write16(POSTED_INTR_NV, POSTED_INTR_NESTED_VECTOR);
10709
10710 /*
10711 * Whether page-faults are trapped is determined by a combination of
10712 * 3 settings: PFEC_MASK, PFEC_MATCH and EXCEPTION_BITMAP.PF.
10713 * If enable_ept, L0 doesn't care about page faults and we should
10714 * set all of these to L1's desires. However, if !enable_ept, L0 does
10715 * care about (at least some) page faults, and because it is not easy
10716 * (if at all possible?) to merge L0 and L1's desires, we simply ask
10717 * to exit on each and every L2 page fault. This is done by setting
10718 * MASK=MATCH=0 and (see below) EB.PF=1.
10719 * Note that below we don't need special code to set EB.PF beyond the
10720 * "or"ing of the EB of vmcs01 and vmcs12, because when enable_ept,
10721 * vmcs01's EB.PF is 0 so the "or" will take vmcs12's value, and when
10722 * !enable_ept, EB.PF is 1, so the "or" will always be 1.
10723 */
10724 vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK,
10725 enable_ept ? vmcs12->page_fault_error_code_mask : 0);
10726 vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH,
10727 enable_ept ? vmcs12->page_fault_error_code_match : 0);
10728
10729 /* All VMFUNCs are currently emulated through L0 vmexits. */
10730 if (cpu_has_vmx_vmfunc())
10731 vmcs_write64(VM_FUNCTION_CONTROL, 0);
10732
10733 if (cpu_has_vmx_apicv()) {
10734 vmcs_write64(EOI_EXIT_BITMAP0, vmcs12->eoi_exit_bitmap0);
10735 vmcs_write64(EOI_EXIT_BITMAP1, vmcs12->eoi_exit_bitmap1);
10736 vmcs_write64(EOI_EXIT_BITMAP2, vmcs12->eoi_exit_bitmap2);
10737 vmcs_write64(EOI_EXIT_BITMAP3, vmcs12->eoi_exit_bitmap3);
10738 }
10739
10740 /*
10741 * Set host-state according to L0's settings (vmcs12 is irrelevant here)
10742 * Some constant fields are set here by vmx_set_constant_host_state().
10743 * Other fields are different per CPU, and will be set later when
10744 * vmx_vcpu_load() is called, and when vmx_save_host_state() is called.
10745 */
10746 vmx_set_constant_host_state(vmx);
10747
10748 /*
10749 * Set the MSR load/store lists to match L0's settings.
10750 */
10751 vmcs_write32(VM_EXIT_MSR_STORE_COUNT, 0);
10752 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, vmx->msr_autoload.nr);
10753 vmcs_write64(VM_EXIT_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.host));
10754 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, vmx->msr_autoload.nr);
10755 vmcs_write64(VM_ENTRY_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.guest));
10756
10757 set_cr4_guest_host_mask(vmx);
10758
10759 if (vmx_mpx_supported())
10760 vmcs_write64(GUEST_BNDCFGS, vmcs12->guest_bndcfgs);
10761
10762 if (enable_vpid) {
10763 if (nested_cpu_has_vpid(vmcs12) && vmx->nested.vpid02)
10764 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->nested.vpid02);
10765 else
10766 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid);
10767 }
10768
10769 /*
10770 * L1 may access the L2's PDPTR, so save them to construct vmcs12
10771 */
10772 if (enable_ept) {
10773 vmcs_write64(GUEST_PDPTR0, vmcs12->guest_pdptr0);
10774 vmcs_write64(GUEST_PDPTR1, vmcs12->guest_pdptr1);
10775 vmcs_write64(GUEST_PDPTR2, vmcs12->guest_pdptr2);
10776 vmcs_write64(GUEST_PDPTR3, vmcs12->guest_pdptr3);
10777 }
Radim Krčmář80132f42018-02-02 18:26:58 +010010778
10779 if (cpu_has_vmx_msr_bitmap())
10780 vmcs_write64(MSR_BITMAP, __pa(vmx->nested.vmcs02.msr_bitmap));
Paolo Bonzini74a497f2017-12-20 13:55:39 +010010781}
10782
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010783/*
10784 * prepare_vmcs02 is called when the L1 guest hypervisor runs its nested
10785 * L2 guest. L1 has a vmcs for L2 (vmcs12), and this function "merges" it
Tiejun Chenb4619662014-09-22 10:31:38 +080010786 * 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 +030010787 * guest in a way that will both be appropriate to L1's requests, and our
10788 * needs. In addition to modifying the active vmcs (which is vmcs02), this
10789 * function also has additional necessary side-effects, like setting various
10790 * vcpu->arch fields.
Ladi Prosekee146c12016-11-30 16:03:09 +010010791 * Returns 0 on success, 1 on failure. Invalid state exit qualification code
10792 * is assigned to entry_failure_code on failure.
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010793 */
Ladi Prosekee146c12016-11-30 16:03:09 +010010794static int prepare_vmcs02(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12,
Jim Mattsonca0bde22016-11-30 12:03:46 -080010795 bool from_vmentry, u32 *entry_failure_code)
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010796{
10797 struct vcpu_vmx *vmx = to_vmx(vcpu);
Bandan Das03efce62017-05-05 15:25:15 -040010798 u32 exec_control, vmcs12_exec_ctrl;
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010799
Paolo Bonzini8665c3f2017-12-20 13:56:53 +010010800 /*
10801 * First, the fields that are shadowed. This must be kept in sync
10802 * with vmx_shadow_fields.h.
10803 */
10804
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010805 vmcs_write16(GUEST_CS_SELECTOR, vmcs12->guest_cs_selector);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010806 vmcs_write32(GUEST_CS_LIMIT, vmcs12->guest_cs_limit);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010807 vmcs_write32(GUEST_CS_AR_BYTES, vmcs12->guest_cs_ar_bytes);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010808 vmcs_writel(GUEST_ES_BASE, vmcs12->guest_es_base);
10809 vmcs_writel(GUEST_CS_BASE, vmcs12->guest_cs_base);
Paolo Bonzini8665c3f2017-12-20 13:56:53 +010010810
10811 /*
10812 * Not in vmcs02: GUEST_PML_INDEX, HOST_FS_SELECTOR, HOST_GS_SELECTOR,
10813 * HOST_FS_BASE, HOST_GS_BASE.
10814 */
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010815
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080010816 if (from_vmentry &&
10817 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS)) {
Jan Kiszka2996fca2014-06-16 13:59:43 +020010818 kvm_set_dr(vcpu, 7, vmcs12->guest_dr7);
10819 vmcs_write64(GUEST_IA32_DEBUGCTL, vmcs12->guest_ia32_debugctl);
10820 } else {
10821 kvm_set_dr(vcpu, 7, vcpu->arch.dr7);
10822 vmcs_write64(GUEST_IA32_DEBUGCTL, vmx->nested.vmcs01_debugctl);
10823 }
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080010824 if (from_vmentry) {
10825 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
10826 vmcs12->vm_entry_intr_info_field);
10827 vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE,
10828 vmcs12->vm_entry_exception_error_code);
10829 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
10830 vmcs12->vm_entry_instruction_len);
10831 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO,
10832 vmcs12->guest_interruptibility_info);
Wanpeng Li2d6144e2017-07-25 03:40:46 -070010833 vmx->loaded_vmcs->nmi_known_unmasked =
10834 !(vmcs12->guest_interruptibility_info & GUEST_INTR_STATE_NMI);
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080010835 } else {
10836 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0);
10837 }
Gleb Natapov63fbf592013-07-28 18:31:06 +030010838 vmx_set_rflags(vcpu, vmcs12->guest_rflags);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010839
Jan Kiszkaf41245002014-03-07 20:03:13 +010010840 exec_control = vmcs12->pin_based_vm_exec_control;
Wincy Van705699a2015-02-03 23:58:17 +080010841
Paolo Bonzini9314006db2016-07-06 13:23:51 +020010842 /* Preemption timer setting is only taken from vmcs01. */
10843 exec_control &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
10844 exec_control |= vmcs_config.pin_based_exec_ctrl;
10845 if (vmx->hv_deadline_tsc == -1)
10846 exec_control &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
10847
10848 /* Posted interrupts setting is only taken from vmcs12. */
Wincy Van705699a2015-02-03 23:58:17 +080010849 if (nested_cpu_has_posted_intr(vmcs12)) {
Wincy Van705699a2015-02-03 23:58:17 +080010850 vmx->nested.posted_intr_nv = vmcs12->posted_intr_nv;
10851 vmx->nested.pi_pending = false;
Jim Mattson6beb7bd2016-11-30 12:03:45 -080010852 } else {
Wincy Van705699a2015-02-03 23:58:17 +080010853 exec_control &= ~PIN_BASED_POSTED_INTR;
Jim Mattson6beb7bd2016-11-30 12:03:45 -080010854 }
Wincy Van705699a2015-02-03 23:58:17 +080010855
Jan Kiszkaf41245002014-03-07 20:03:13 +010010856 vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, exec_control);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010857
Jan Kiszkaf41245002014-03-07 20:03:13 +010010858 vmx->nested.preemption_timer_expired = false;
10859 if (nested_cpu_has_preemption_timer(vmcs12))
10860 vmx_start_preemption_timer(vcpu);
Jan Kiszka0238ea92013-03-13 11:31:24 +010010861
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010862 if (cpu_has_secondary_exec_ctrls()) {
Paolo Bonzini80154d72017-08-24 13:55:35 +020010863 exec_control = vmx->secondary_exec_control;
Xiao Guangronge2821622015-09-09 14:05:52 +080010864
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010865 /* Take the following fields only from vmcs12 */
Paolo Bonzini696dfd92014-05-07 11:20:54 +020010866 exec_control &= ~(SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
Paolo Bonzini90a2db62017-07-27 13:22:13 +020010867 SECONDARY_EXEC_ENABLE_INVPCID |
Jan Kiszkab3a2a902015-03-23 19:27:19 +010010868 SECONDARY_EXEC_RDTSCP |
Paolo Bonzini3db13482017-08-24 14:48:03 +020010869 SECONDARY_EXEC_XSAVES |
Paolo Bonzini696dfd92014-05-07 11:20:54 +020010870 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY |
Bandan Das27c42a12017-08-03 15:54:42 -040010871 SECONDARY_EXEC_APIC_REGISTER_VIRT |
10872 SECONDARY_EXEC_ENABLE_VMFUNC);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010873 if (nested_cpu_has(vmcs12,
Bandan Das03efce62017-05-05 15:25:15 -040010874 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS)) {
10875 vmcs12_exec_ctrl = vmcs12->secondary_vm_exec_control &
10876 ~SECONDARY_EXEC_ENABLE_PML;
10877 exec_control |= vmcs12_exec_ctrl;
10878 }
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010879
Paolo Bonzini25a2e4f2017-12-20 14:05:21 +010010880 if (exec_control & SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY)
Wincy Van608406e2015-02-03 23:57:51 +080010881 vmcs_write16(GUEST_INTR_STATUS,
10882 vmcs12->guest_intr_status);
Wincy Van608406e2015-02-03 23:57:51 +080010883
Jim Mattson6beb7bd2016-11-30 12:03:45 -080010884 /*
10885 * Write an illegal value to APIC_ACCESS_ADDR. Later,
10886 * nested_get_vmcs12_pages will either fix it up or
10887 * remove the VM execution control.
10888 */
10889 if (exec_control & SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)
10890 vmcs_write64(APIC_ACCESS_ADDR, -1ull);
10891
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010892 vmcs_write32(SECONDARY_VM_EXEC_CONTROL, exec_control);
10893 }
10894
Jim Mattson83bafef2016-10-04 10:48:38 -070010895 /*
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010896 * HOST_RSP is normally set correctly in vmx_vcpu_run() just before
10897 * entry, but only if the current (host) sp changed from the value
10898 * we wrote last (vmx->host_rsp). This cache is no longer relevant
10899 * if we switch vmcs, and rather than hold a separate cache per vmcs,
10900 * here we just force the write to happen on entry.
10901 */
10902 vmx->host_rsp = 0;
10903
10904 exec_control = vmx_exec_control(vmx); /* L0's desires */
10905 exec_control &= ~CPU_BASED_VIRTUAL_INTR_PENDING;
10906 exec_control &= ~CPU_BASED_VIRTUAL_NMI_PENDING;
10907 exec_control &= ~CPU_BASED_TPR_SHADOW;
10908 exec_control |= vmcs12->cpu_based_vm_exec_control;
Wanpeng Lia7c0b072014-08-21 19:46:50 +080010909
Jim Mattson6beb7bd2016-11-30 12:03:45 -080010910 /*
10911 * Write an illegal value to VIRTUAL_APIC_PAGE_ADDR. Later, if
10912 * nested_get_vmcs12_pages can't fix it up, the illegal value
10913 * will result in a VM entry failure.
10914 */
Wanpeng Lia7c0b072014-08-21 19:46:50 +080010915 if (exec_control & CPU_BASED_TPR_SHADOW) {
Jim Mattson6beb7bd2016-11-30 12:03:45 -080010916 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, -1ull);
Wanpeng Lia7c0b072014-08-21 19:46:50 +080010917 vmcs_write32(TPR_THRESHOLD, vmcs12->tpr_threshold);
Jim Mattson51aa68e2017-09-12 13:02:54 -070010918 } else {
10919#ifdef CONFIG_X86_64
10920 exec_control |= CPU_BASED_CR8_LOAD_EXITING |
10921 CPU_BASED_CR8_STORE_EXITING;
10922#endif
Wanpeng Lia7c0b072014-08-21 19:46:50 +080010923 }
10924
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010925 /*
Quan Xu8eb73e2d2017-12-12 16:44:21 +080010926 * A vmexit (to either L1 hypervisor or L0 userspace) is always needed
10927 * for I/O port accesses.
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010928 */
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010929 exec_control &= ~CPU_BASED_USE_IO_BITMAPS;
10930 exec_control |= CPU_BASED_UNCOND_IO_EXITING;
10931
10932 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, exec_control);
10933
10934 /* EXCEPTION_BITMAP and CR0_GUEST_HOST_MASK should basically be the
10935 * bitwise-or of what L1 wants to trap for L2, and what we want to
10936 * trap. Note that CR0.TS also needs updating - we do this later.
10937 */
10938 update_exception_bitmap(vcpu);
10939 vcpu->arch.cr0_guest_owned_bits &= ~vmcs12->cr0_guest_host_mask;
10940 vmcs_writel(CR0_GUEST_HOST_MASK, ~vcpu->arch.cr0_guest_owned_bits);
10941
Nadav Har'El8049d652013-08-05 11:07:06 +030010942 /* L2->L1 exit controls are emulated - the hardware exit is to L0 so
10943 * we should use its exit controls. Note that VM_EXIT_LOAD_IA32_EFER
10944 * bits are further modified by vmx_set_efer() below.
10945 */
Jan Kiszkaf41245002014-03-07 20:03:13 +010010946 vmcs_write32(VM_EXIT_CONTROLS, vmcs_config.vmexit_ctrl);
Nadav Har'El8049d652013-08-05 11:07:06 +030010947
10948 /* vmcs12's VM_ENTRY_LOAD_IA32_EFER and VM_ENTRY_IA32E_MODE are
10949 * emulated by vmx_set_efer(), below.
10950 */
Gleb Natapov2961e8762013-11-25 15:37:13 +020010951 vm_entry_controls_init(vmx,
Nadav Har'El8049d652013-08-05 11:07:06 +030010952 (vmcs12->vm_entry_controls & ~VM_ENTRY_LOAD_IA32_EFER &
10953 ~VM_ENTRY_IA32E_MODE) |
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010954 (vmcs_config.vmentry_ctrl & ~VM_ENTRY_IA32E_MODE));
10955
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080010956 if (from_vmentry &&
10957 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_PAT)) {
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010958 vmcs_write64(GUEST_IA32_PAT, vmcs12->guest_ia32_pat);
Jan Kiszka44811c02013-08-04 17:17:27 +020010959 vcpu->arch.pat = vmcs12->guest_ia32_pat;
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080010960 } else if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) {
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010961 vmcs_write64(GUEST_IA32_PAT, vmx->vcpu.arch.pat);
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080010962 }
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010963
Nadav Har'El27fc51b2011-08-02 15:54:52 +030010964 if (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING)
10965 vmcs_write64(TSC_OFFSET,
Paolo Bonziniea26e4e2016-11-01 00:39:48 +010010966 vcpu->arch.tsc_offset + vmcs12->tsc_offset);
Nadav Har'El27fc51b2011-08-02 15:54:52 +030010967 else
Paolo Bonziniea26e4e2016-11-01 00:39:48 +010010968 vmcs_write64(TSC_OFFSET, vcpu->arch.tsc_offset);
Peter Feinerc95ba922016-08-17 09:36:47 -070010969 if (kvm_has_tsc_control)
10970 decache_tsc_multiplier(vmx);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010971
10972 if (enable_vpid) {
10973 /*
Wanpeng Li5c614b32015-10-13 09:18:36 -070010974 * There is no direct mapping between vpid02 and vpid12, the
10975 * vpid02 is per-vCPU for L0 and reused while the value of
10976 * vpid12 is changed w/ one invvpid during nested vmentry.
10977 * The vpid12 is allocated by L1 for L2, so it will not
10978 * influence global bitmap(for vpid01 and vpid02 allocation)
10979 * even if spawn a lot of nested vCPUs.
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010980 */
Wanpeng Li5c614b32015-10-13 09:18:36 -070010981 if (nested_cpu_has_vpid(vmcs12) && vmx->nested.vpid02) {
Wanpeng Li5c614b32015-10-13 09:18:36 -070010982 if (vmcs12->virtual_processor_id != vmx->nested.last_vpid) {
10983 vmx->nested.last_vpid = vmcs12->virtual_processor_id;
Wanpeng Lic2ba05c2017-12-12 17:33:03 -080010984 __vmx_flush_tlb(vcpu, to_vmx(vcpu)->nested.vpid02, true);
Wanpeng Li5c614b32015-10-13 09:18:36 -070010985 }
10986 } else {
Wanpeng Lic2ba05c2017-12-12 17:33:03 -080010987 vmx_flush_tlb(vcpu, true);
Wanpeng Li5c614b32015-10-13 09:18:36 -070010988 }
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010989 }
10990
Ladi Prosek1fb883b2017-04-04 14:18:53 +020010991 if (enable_pml) {
10992 /*
10993 * Conceptually we want to copy the PML address and index from
10994 * vmcs01 here, and then back to vmcs01 on nested vmexit. But,
10995 * since we always flush the log on each vmexit, this happens
10996 * to be equivalent to simply resetting the fields in vmcs02.
10997 */
10998 ASSERT(vmx->pml_pg);
10999 vmcs_write64(PML_ADDRESS, page_to_phys(vmx->pml_pg));
11000 vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1);
11001 }
11002
Nadav Har'El155a97a2013-08-05 11:07:16 +030011003 if (nested_cpu_has_ept(vmcs12)) {
Paolo Bonziniae1e2d12017-03-30 11:55:30 +020011004 if (nested_ept_init_mmu_context(vcpu)) {
11005 *entry_failure_code = ENTRY_FAIL_DEFAULT;
11006 return 1;
11007 }
Jim Mattsonfb6c8192017-03-16 13:53:59 -070011008 } else if (nested_cpu_has2(vmcs12,
11009 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) {
11010 vmx_flush_tlb_ept_only(vcpu);
Nadav Har'El155a97a2013-08-05 11:07:16 +030011011 }
11012
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011013 /*
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -080011014 * This sets GUEST_CR0 to vmcs12->guest_cr0, possibly modifying those
11015 * bits which we consider mandatory enabled.
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011016 * The CR0_READ_SHADOW is what L2 should have expected to read given
11017 * the specifications by L1; It's not enough to take
11018 * vmcs12->cr0_read_shadow because on our cr0_guest_host_mask we we
11019 * have more bits than L1 expected.
11020 */
11021 vmx_set_cr0(vcpu, vmcs12->guest_cr0);
11022 vmcs_writel(CR0_READ_SHADOW, nested_read_cr0(vmcs12));
11023
11024 vmx_set_cr4(vcpu, vmcs12->guest_cr4);
11025 vmcs_writel(CR4_READ_SHADOW, nested_read_cr4(vmcs12));
11026
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080011027 if (from_vmentry &&
11028 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_EFER))
David Matlack5a6a9742016-11-29 18:14:10 -080011029 vcpu->arch.efer = vmcs12->guest_ia32_efer;
11030 else if (vmcs12->vm_entry_controls & VM_ENTRY_IA32E_MODE)
11031 vcpu->arch.efer |= (EFER_LMA | EFER_LME);
11032 else
11033 vcpu->arch.efer &= ~(EFER_LMA | EFER_LME);
11034 /* Note: modifies VM_ENTRY/EXIT_CONTROLS and GUEST/HOST_IA32_EFER */
11035 vmx_set_efer(vcpu, vcpu->arch.efer);
11036
Paolo Bonzini74a497f2017-12-20 13:55:39 +010011037 if (vmx->nested.dirty_vmcs12) {
11038 prepare_vmcs02_full(vcpu, vmcs12, from_vmentry);
11039 vmx->nested.dirty_vmcs12 = false;
11040 }
11041
Sean Christopherson2bb8caf2018-03-12 10:56:13 -070011042 /*
11043 * Guest state is invalid and unrestricted guest is disabled,
11044 * which means L1 attempted VMEntry to L2 with invalid state.
11045 * Fail the VMEntry.
11046 */
11047 if (vmx->emulation_required)
11048 return 1;
11049
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010011050 /* Shadow page tables on either EPT or shadow page tables. */
Ladi Prosek7ad658b2017-03-23 07:18:08 +010011051 if (nested_vmx_load_cr3(vcpu, vmcs12->guest_cr3, nested_cpu_has_ept(vmcs12),
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010011052 entry_failure_code))
11053 return 1;
Ladi Prosek7ca29de2016-11-30 16:03:08 +010011054
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +030011055 if (!enable_ept)
11056 vcpu->arch.walk_mmu->inject_page_fault = vmx_inject_page_fault_nested;
11057
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011058 kvm_register_write(vcpu, VCPU_REGS_RSP, vmcs12->guest_rsp);
11059 kvm_register_write(vcpu, VCPU_REGS_RIP, vmcs12->guest_rip);
Ladi Prosekee146c12016-11-30 16:03:09 +010011060 return 0;
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011061}
11062
Krish Sadhukhan0c7f6502018-02-20 21:24:39 -050011063static int nested_vmx_check_nmi_controls(struct vmcs12 *vmcs12)
11064{
11065 if (!nested_cpu_has_nmi_exiting(vmcs12) &&
11066 nested_cpu_has_virtual_nmis(vmcs12))
11067 return -EINVAL;
11068
11069 if (!nested_cpu_has_virtual_nmis(vmcs12) &&
11070 nested_cpu_has(vmcs12, CPU_BASED_VIRTUAL_NMI_PENDING))
11071 return -EINVAL;
11072
11073 return 0;
11074}
11075
Jim Mattsonca0bde22016-11-30 12:03:46 -080011076static int check_vmentry_prereqs(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
11077{
11078 struct vcpu_vmx *vmx = to_vmx(vcpu);
11079
11080 if (vmcs12->guest_activity_state != GUEST_ACTIVITY_ACTIVE &&
11081 vmcs12->guest_activity_state != GUEST_ACTIVITY_HLT)
11082 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11083
Jim Mattson56a20512017-07-06 16:33:06 -070011084 if (nested_vmx_check_io_bitmap_controls(vcpu, vmcs12))
11085 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11086
Jim Mattsonca0bde22016-11-30 12:03:46 -080011087 if (nested_vmx_check_msr_bitmap_controls(vcpu, vmcs12))
11088 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11089
Jim Mattson712b12d2017-08-24 13:24:47 -070011090 if (nested_vmx_check_tpr_shadow_controls(vcpu, vmcs12))
11091 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11092
Jim Mattsonca0bde22016-11-30 12:03:46 -080011093 if (nested_vmx_check_apicv_controls(vcpu, vmcs12))
11094 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11095
11096 if (nested_vmx_check_msr_switch_controls(vcpu, vmcs12))
11097 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11098
Bandan Dasc5f983f2017-05-05 15:25:14 -040011099 if (nested_vmx_check_pml_controls(vcpu, vmcs12))
11100 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11101
Jim Mattsonca0bde22016-11-30 12:03:46 -080011102 if (!vmx_control_verify(vmcs12->cpu_based_vm_exec_control,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010011103 vmx->nested.msrs.procbased_ctls_low,
11104 vmx->nested.msrs.procbased_ctls_high) ||
Jim Mattson2e5b0bd2017-05-04 11:51:58 -070011105 (nested_cpu_has(vmcs12, CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) &&
11106 !vmx_control_verify(vmcs12->secondary_vm_exec_control,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010011107 vmx->nested.msrs.secondary_ctls_low,
11108 vmx->nested.msrs.secondary_ctls_high)) ||
Jim Mattsonca0bde22016-11-30 12:03:46 -080011109 !vmx_control_verify(vmcs12->pin_based_vm_exec_control,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010011110 vmx->nested.msrs.pinbased_ctls_low,
11111 vmx->nested.msrs.pinbased_ctls_high) ||
Jim Mattsonca0bde22016-11-30 12:03:46 -080011112 !vmx_control_verify(vmcs12->vm_exit_controls,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010011113 vmx->nested.msrs.exit_ctls_low,
11114 vmx->nested.msrs.exit_ctls_high) ||
Jim Mattsonca0bde22016-11-30 12:03:46 -080011115 !vmx_control_verify(vmcs12->vm_entry_controls,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010011116 vmx->nested.msrs.entry_ctls_low,
11117 vmx->nested.msrs.entry_ctls_high))
Jim Mattsonca0bde22016-11-30 12:03:46 -080011118 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11119
Krish Sadhukhan0c7f6502018-02-20 21:24:39 -050011120 if (nested_vmx_check_nmi_controls(vmcs12))
11121 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11122
Bandan Das41ab9372017-08-03 15:54:43 -040011123 if (nested_cpu_has_vmfunc(vmcs12)) {
11124 if (vmcs12->vm_function_control &
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010011125 ~vmx->nested.msrs.vmfunc_controls)
Bandan Das41ab9372017-08-03 15:54:43 -040011126 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11127
11128 if (nested_cpu_has_eptp_switching(vmcs12)) {
11129 if (!nested_cpu_has_ept(vmcs12) ||
11130 !page_address_valid(vcpu, vmcs12->eptp_list_address))
11131 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11132 }
11133 }
Bandan Das27c42a12017-08-03 15:54:42 -040011134
Jim Mattsonc7c2c7092017-05-05 11:28:09 -070011135 if (vmcs12->cr3_target_count > nested_cpu_vmx_misc_cr3_count(vcpu))
11136 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11137
Jim Mattsonca0bde22016-11-30 12:03:46 -080011138 if (!nested_host_cr0_valid(vcpu, vmcs12->host_cr0) ||
11139 !nested_host_cr4_valid(vcpu, vmcs12->host_cr4) ||
11140 !nested_cr3_valid(vcpu, vmcs12->host_cr3))
11141 return VMXERR_ENTRY_INVALID_HOST_STATE_FIELD;
11142
11143 return 0;
11144}
11145
11146static int check_vmentry_postreqs(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12,
11147 u32 *exit_qual)
11148{
11149 bool ia32e;
11150
11151 *exit_qual = ENTRY_FAIL_DEFAULT;
11152
11153 if (!nested_guest_cr0_valid(vcpu, vmcs12->guest_cr0) ||
11154 !nested_guest_cr4_valid(vcpu, vmcs12->guest_cr4))
11155 return 1;
11156
11157 if (!nested_cpu_has2(vmcs12, SECONDARY_EXEC_SHADOW_VMCS) &&
11158 vmcs12->vmcs_link_pointer != -1ull) {
11159 *exit_qual = ENTRY_FAIL_VMCS_LINK_PTR;
11160 return 1;
11161 }
11162
11163 /*
11164 * If the load IA32_EFER VM-entry control is 1, the following checks
11165 * are performed on the field for the IA32_EFER MSR:
11166 * - Bits reserved in the IA32_EFER MSR must be 0.
11167 * - Bit 10 (corresponding to IA32_EFER.LMA) must equal the value of
11168 * the IA-32e mode guest VM-exit control. It must also be identical
11169 * to bit 8 (LME) if bit 31 in the CR0 field (corresponding to
11170 * CR0.PG) is 1.
11171 */
11172 if (to_vmx(vcpu)->nested.nested_run_pending &&
11173 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_EFER)) {
11174 ia32e = (vmcs12->vm_entry_controls & VM_ENTRY_IA32E_MODE) != 0;
11175 if (!kvm_valid_efer(vcpu, vmcs12->guest_ia32_efer) ||
11176 ia32e != !!(vmcs12->guest_ia32_efer & EFER_LMA) ||
11177 ((vmcs12->guest_cr0 & X86_CR0_PG) &&
11178 ia32e != !!(vmcs12->guest_ia32_efer & EFER_LME)))
11179 return 1;
11180 }
11181
11182 /*
11183 * If the load IA32_EFER VM-exit control is 1, bits reserved in the
11184 * IA32_EFER MSR must be 0 in the field for that register. In addition,
11185 * the values of the LMA and LME bits in the field must each be that of
11186 * the host address-space size VM-exit control.
11187 */
11188 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_EFER) {
11189 ia32e = (vmcs12->vm_exit_controls &
11190 VM_EXIT_HOST_ADDR_SPACE_SIZE) != 0;
11191 if (!kvm_valid_efer(vcpu, vmcs12->host_ia32_efer) ||
11192 ia32e != !!(vmcs12->host_ia32_efer & EFER_LMA) ||
11193 ia32e != !!(vmcs12->host_ia32_efer & EFER_LME))
11194 return 1;
11195 }
11196
Wanpeng Lif1b026a2017-11-05 16:54:48 -080011197 if ((vmcs12->vm_entry_controls & VM_ENTRY_LOAD_BNDCFGS) &&
11198 (is_noncanonical_address(vmcs12->guest_bndcfgs & PAGE_MASK, vcpu) ||
11199 (vmcs12->guest_bndcfgs & MSR_IA32_BNDCFGS_RSVD)))
11200 return 1;
11201
Jim Mattsonca0bde22016-11-30 12:03:46 -080011202 return 0;
11203}
11204
Jim Mattson858e25c2016-11-30 12:03:47 -080011205static int enter_vmx_non_root_mode(struct kvm_vcpu *vcpu, bool from_vmentry)
11206{
11207 struct vcpu_vmx *vmx = to_vmx(vcpu);
11208 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Jim Mattson858e25c2016-11-30 12:03:47 -080011209 u32 msr_entry_idx;
11210 u32 exit_qual;
11211
Jim Mattson858e25c2016-11-30 12:03:47 -080011212 enter_guest_mode(vcpu);
11213
11214 if (!(vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS))
11215 vmx->nested.vmcs01_debugctl = vmcs_read64(GUEST_IA32_DEBUGCTL);
11216
Jim Mattsonde3a0022017-11-27 17:22:25 -060011217 vmx_switch_vmcs(vcpu, &vmx->nested.vmcs02);
Jim Mattson858e25c2016-11-30 12:03:47 -080011218 vmx_segment_cache_clear(vmx);
11219
11220 if (prepare_vmcs02(vcpu, vmcs12, from_vmentry, &exit_qual)) {
11221 leave_guest_mode(vcpu);
David Hildenbrand1279a6b12017-03-20 10:00:08 +010011222 vmx_switch_vmcs(vcpu, &vmx->vmcs01);
Jim Mattson858e25c2016-11-30 12:03:47 -080011223 nested_vmx_entry_failure(vcpu, vmcs12,
11224 EXIT_REASON_INVALID_STATE, exit_qual);
11225 return 1;
11226 }
11227
11228 nested_get_vmcs12_pages(vcpu, vmcs12);
11229
11230 msr_entry_idx = nested_vmx_load_msr(vcpu,
11231 vmcs12->vm_entry_msr_load_addr,
11232 vmcs12->vm_entry_msr_load_count);
11233 if (msr_entry_idx) {
11234 leave_guest_mode(vcpu);
David Hildenbrand1279a6b12017-03-20 10:00:08 +010011235 vmx_switch_vmcs(vcpu, &vmx->vmcs01);
Jim Mattson858e25c2016-11-30 12:03:47 -080011236 nested_vmx_entry_failure(vcpu, vmcs12,
11237 EXIT_REASON_MSR_LOAD_FAIL, msr_entry_idx);
11238 return 1;
11239 }
11240
Jim Mattson858e25c2016-11-30 12:03:47 -080011241 /*
11242 * Note no nested_vmx_succeed or nested_vmx_fail here. At this point
11243 * we are no longer running L1, and VMLAUNCH/VMRESUME has not yet
11244 * returned as far as L1 is concerned. It will only return (and set
11245 * the success flag) when L2 exits (see nested_vmx_vmexit()).
11246 */
11247 return 0;
11248}
11249
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030011250/*
11251 * nested_vmx_run() handles a nested entry, i.e., a VMLAUNCH or VMRESUME on L1
11252 * for running an L2 nested guest.
11253 */
11254static int nested_vmx_run(struct kvm_vcpu *vcpu, bool launch)
11255{
11256 struct vmcs12 *vmcs12;
11257 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jim Mattsonb3f1dfb2017-07-17 12:00:34 -070011258 u32 interrupt_shadow = vmx_get_interrupt_shadow(vcpu);
Jim Mattsonca0bde22016-11-30 12:03:46 -080011259 u32 exit_qual;
11260 int ret;
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030011261
Kyle Hueyeb277562016-11-29 12:40:39 -080011262 if (!nested_vmx_check_permission(vcpu))
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030011263 return 1;
11264
Kyle Hueyeb277562016-11-29 12:40:39 -080011265 if (!nested_vmx_check_vmcs12(vcpu))
11266 goto out;
11267
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030011268 vmcs12 = get_vmcs12(vcpu);
11269
Abel Gordon012f83c2013-04-18 14:39:25 +030011270 if (enable_shadow_vmcs)
11271 copy_shadow_to_vmcs12(vmx);
11272
Nadav Har'El7c177932011-05-25 23:12:04 +030011273 /*
11274 * The nested entry process starts with enforcing various prerequisites
11275 * on vmcs12 as required by the Intel SDM, and act appropriately when
11276 * they fail: As the SDM explains, some conditions should cause the
11277 * instruction to fail, while others will cause the instruction to seem
11278 * to succeed, but return an EXIT_REASON_INVALID_STATE.
11279 * To speed up the normal (success) code path, we should avoid checking
11280 * for misconfigurations which will anyway be caught by the processor
11281 * when using the merged vmcs02.
11282 */
Jim Mattsonb3f1dfb2017-07-17 12:00:34 -070011283 if (interrupt_shadow & KVM_X86_SHADOW_INT_MOV_SS) {
11284 nested_vmx_failValid(vcpu,
11285 VMXERR_ENTRY_EVENTS_BLOCKED_BY_MOV_SS);
11286 goto out;
11287 }
11288
Nadav Har'El7c177932011-05-25 23:12:04 +030011289 if (vmcs12->launch_state == launch) {
11290 nested_vmx_failValid(vcpu,
11291 launch ? VMXERR_VMLAUNCH_NONCLEAR_VMCS
11292 : VMXERR_VMRESUME_NONLAUNCHED_VMCS);
Kyle Hueyeb277562016-11-29 12:40:39 -080011293 goto out;
Nadav Har'El7c177932011-05-25 23:12:04 +030011294 }
11295
Jim Mattsonca0bde22016-11-30 12:03:46 -080011296 ret = check_vmentry_prereqs(vcpu, vmcs12);
11297 if (ret) {
11298 nested_vmx_failValid(vcpu, ret);
Kyle Hueyeb277562016-11-29 12:40:39 -080011299 goto out;
Paolo Bonzini26539bd2013-04-15 15:00:27 +020011300 }
11301
Nadav Har'El7c177932011-05-25 23:12:04 +030011302 /*
Jim Mattsonca0bde22016-11-30 12:03:46 -080011303 * After this point, the trap flag no longer triggers a singlestep trap
11304 * on the vm entry instructions; don't call kvm_skip_emulated_instruction.
11305 * This is not 100% correct; for performance reasons, we delegate most
11306 * of the checks on host state to the processor. If those fail,
11307 * the singlestep trap is missed.
Jan Kiszka384bb782013-04-20 10:52:36 +020011308 */
Jim Mattsonca0bde22016-11-30 12:03:46 -080011309 skip_emulated_instruction(vcpu);
Jan Kiszka384bb782013-04-20 10:52:36 +020011310
Jim Mattsonca0bde22016-11-30 12:03:46 -080011311 ret = check_vmentry_postreqs(vcpu, vmcs12, &exit_qual);
11312 if (ret) {
11313 nested_vmx_entry_failure(vcpu, vmcs12,
11314 EXIT_REASON_INVALID_STATE, exit_qual);
11315 return 1;
Jan Kiszka384bb782013-04-20 10:52:36 +020011316 }
11317
11318 /*
Nadav Har'El7c177932011-05-25 23:12:04 +030011319 * We're finally done with prerequisite checking, and can start with
11320 * the nested entry.
11321 */
11322
Jim Mattson858e25c2016-11-30 12:03:47 -080011323 ret = enter_vmx_non_root_mode(vcpu, true);
11324 if (ret)
11325 return ret;
Wincy Vanff651cb2014-12-11 08:52:58 +030011326
Chao Gao135a06c2018-02-11 10:06:30 +080011327 /*
11328 * If we're entering a halted L2 vcpu and the L2 vcpu won't be woken
11329 * by event injection, halt vcpu.
11330 */
11331 if ((vmcs12->guest_activity_state == GUEST_ACTIVITY_HLT) &&
11332 !(vmcs12->vm_entry_intr_info_field & INTR_INFO_VALID_MASK))
Joel Schopp5cb56052015-03-02 13:43:31 -060011333 return kvm_vcpu_halt(vcpu);
Jan Kiszka6dfacad2013-12-04 08:58:54 +010011334
Jan Kiszka7af40ad32014-01-04 18:47:23 +010011335 vmx->nested.nested_run_pending = 1;
11336
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030011337 return 1;
Kyle Hueyeb277562016-11-29 12:40:39 -080011338
11339out:
Kyle Huey6affcbe2016-11-29 12:40:40 -080011340 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030011341}
11342
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011343/*
11344 * On a nested exit from L2 to L1, vmcs12.guest_cr0 might not be up-to-date
11345 * because L2 may have changed some cr0 bits directly (CRO_GUEST_HOST_MASK).
11346 * This function returns the new value we should put in vmcs12.guest_cr0.
11347 * It's not enough to just return the vmcs02 GUEST_CR0. Rather,
11348 * 1. Bits that neither L0 nor L1 trapped, were set directly by L2 and are now
11349 * available in vmcs02 GUEST_CR0. (Note: It's enough to check that L0
11350 * didn't trap the bit, because if L1 did, so would L0).
11351 * 2. Bits that L1 asked to trap (and therefore L0 also did) could not have
11352 * been modified by L2, and L1 knows it. So just leave the old value of
11353 * the bit from vmcs12.guest_cr0. Note that the bit from vmcs02 GUEST_CR0
11354 * isn't relevant, because if L0 traps this bit it can set it to anything.
11355 * 3. Bits that L1 didn't trap, but L0 did. L1 believes the guest could have
11356 * changed these bits, and therefore they need to be updated, but L0
11357 * didn't necessarily allow them to be changed in GUEST_CR0 - and rather
11358 * put them in vmcs02 CR0_READ_SHADOW. So take these bits from there.
11359 */
11360static inline unsigned long
11361vmcs12_guest_cr0(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
11362{
11363 return
11364 /*1*/ (vmcs_readl(GUEST_CR0) & vcpu->arch.cr0_guest_owned_bits) |
11365 /*2*/ (vmcs12->guest_cr0 & vmcs12->cr0_guest_host_mask) |
11366 /*3*/ (vmcs_readl(CR0_READ_SHADOW) & ~(vmcs12->cr0_guest_host_mask |
11367 vcpu->arch.cr0_guest_owned_bits));
11368}
11369
11370static inline unsigned long
11371vmcs12_guest_cr4(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
11372{
11373 return
11374 /*1*/ (vmcs_readl(GUEST_CR4) & vcpu->arch.cr4_guest_owned_bits) |
11375 /*2*/ (vmcs12->guest_cr4 & vmcs12->cr4_guest_host_mask) |
11376 /*3*/ (vmcs_readl(CR4_READ_SHADOW) & ~(vmcs12->cr4_guest_host_mask |
11377 vcpu->arch.cr4_guest_owned_bits));
11378}
11379
Jan Kiszka5f3d5792013-04-14 12:12:46 +020011380static void vmcs12_save_pending_event(struct kvm_vcpu *vcpu,
11381 struct vmcs12 *vmcs12)
11382{
11383 u32 idt_vectoring;
11384 unsigned int nr;
11385
Wanpeng Li664f8e22017-08-24 03:35:09 -070011386 if (vcpu->arch.exception.injected) {
Jan Kiszka5f3d5792013-04-14 12:12:46 +020011387 nr = vcpu->arch.exception.nr;
11388 idt_vectoring = nr | VECTORING_INFO_VALID_MASK;
11389
11390 if (kvm_exception_is_soft(nr)) {
11391 vmcs12->vm_exit_instruction_len =
11392 vcpu->arch.event_exit_inst_len;
11393 idt_vectoring |= INTR_TYPE_SOFT_EXCEPTION;
11394 } else
11395 idt_vectoring |= INTR_TYPE_HARD_EXCEPTION;
11396
11397 if (vcpu->arch.exception.has_error_code) {
11398 idt_vectoring |= VECTORING_INFO_DELIVER_CODE_MASK;
11399 vmcs12->idt_vectoring_error_code =
11400 vcpu->arch.exception.error_code;
11401 }
11402
11403 vmcs12->idt_vectoring_info_field = idt_vectoring;
Jan Kiszkacd2633c2013-10-23 17:42:15 +010011404 } else if (vcpu->arch.nmi_injected) {
Jan Kiszka5f3d5792013-04-14 12:12:46 +020011405 vmcs12->idt_vectoring_info_field =
11406 INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK | NMI_VECTOR;
11407 } else if (vcpu->arch.interrupt.pending) {
11408 nr = vcpu->arch.interrupt.nr;
11409 idt_vectoring = nr | VECTORING_INFO_VALID_MASK;
11410
11411 if (vcpu->arch.interrupt.soft) {
11412 idt_vectoring |= INTR_TYPE_SOFT_INTR;
11413 vmcs12->vm_entry_instruction_len =
11414 vcpu->arch.event_exit_inst_len;
11415 } else
11416 idt_vectoring |= INTR_TYPE_EXT_INTR;
11417
11418 vmcs12->idt_vectoring_info_field = idt_vectoring;
11419 }
11420}
11421
Jan Kiszkab6b8a142014-03-07 20:03:12 +010011422static int vmx_check_nested_events(struct kvm_vcpu *vcpu, bool external_intr)
11423{
11424 struct vcpu_vmx *vmx = to_vmx(vcpu);
Wanpeng Libfcf83b2017-08-24 03:35:11 -070011425 unsigned long exit_qual;
Liran Alon917dc602017-11-05 16:07:43 +020011426 bool block_nested_events =
11427 vmx->nested.nested_run_pending || kvm_event_needs_reinjection(vcpu);
Wanpeng Liacc9ab62017-02-27 04:24:39 -080011428
Wanpeng Libfcf83b2017-08-24 03:35:11 -070011429 if (vcpu->arch.exception.pending &&
11430 nested_vmx_check_exception(vcpu, &exit_qual)) {
Liran Alon917dc602017-11-05 16:07:43 +020011431 if (block_nested_events)
Wanpeng Libfcf83b2017-08-24 03:35:11 -070011432 return -EBUSY;
11433 nested_vmx_inject_exception_vmexit(vcpu, exit_qual);
Wanpeng Libfcf83b2017-08-24 03:35:11 -070011434 return 0;
11435 }
11436
Jan Kiszkaf41245002014-03-07 20:03:13 +010011437 if (nested_cpu_has_preemption_timer(get_vmcs12(vcpu)) &&
11438 vmx->nested.preemption_timer_expired) {
Liran Alon917dc602017-11-05 16:07:43 +020011439 if (block_nested_events)
Jan Kiszkaf41245002014-03-07 20:03:13 +010011440 return -EBUSY;
11441 nested_vmx_vmexit(vcpu, EXIT_REASON_PREEMPTION_TIMER, 0, 0);
11442 return 0;
11443 }
11444
Jan Kiszkab6b8a142014-03-07 20:03:12 +010011445 if (vcpu->arch.nmi_pending && nested_exit_on_nmi(vcpu)) {
Liran Alon917dc602017-11-05 16:07:43 +020011446 if (block_nested_events)
Jan Kiszkab6b8a142014-03-07 20:03:12 +010011447 return -EBUSY;
11448 nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI,
11449 NMI_VECTOR | INTR_TYPE_NMI_INTR |
11450 INTR_INFO_VALID_MASK, 0);
11451 /*
11452 * The NMI-triggered VM exit counts as injection:
11453 * clear this one and block further NMIs.
11454 */
11455 vcpu->arch.nmi_pending = 0;
11456 vmx_set_nmi_mask(vcpu, true);
11457 return 0;
11458 }
11459
11460 if ((kvm_cpu_has_interrupt(vcpu) || external_intr) &&
11461 nested_exit_on_intr(vcpu)) {
Liran Alon917dc602017-11-05 16:07:43 +020011462 if (block_nested_events)
Jan Kiszkab6b8a142014-03-07 20:03:12 +010011463 return -EBUSY;
11464 nested_vmx_vmexit(vcpu, EXIT_REASON_EXTERNAL_INTERRUPT, 0, 0);
Wincy Van705699a2015-02-03 23:58:17 +080011465 return 0;
Jan Kiszkab6b8a142014-03-07 20:03:12 +010011466 }
11467
David Hildenbrand6342c502017-01-25 11:58:58 +010011468 vmx_complete_nested_posted_interrupt(vcpu);
11469 return 0;
Jan Kiszkab6b8a142014-03-07 20:03:12 +010011470}
11471
Jan Kiszkaf41245002014-03-07 20:03:13 +010011472static u32 vmx_get_preemption_timer_value(struct kvm_vcpu *vcpu)
11473{
11474 ktime_t remaining =
11475 hrtimer_get_remaining(&to_vmx(vcpu)->nested.preemption_timer);
11476 u64 value;
11477
11478 if (ktime_to_ns(remaining) <= 0)
11479 return 0;
11480
11481 value = ktime_to_ns(remaining) * vcpu->arch.virtual_tsc_khz;
11482 do_div(value, 1000000);
11483 return value >> VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE;
11484}
11485
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011486/*
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080011487 * Update the guest state fields of vmcs12 to reflect changes that
11488 * occurred while L2 was running. (The "IA-32e mode guest" bit of the
11489 * VM-entry controls is also updated, since this is really a guest
11490 * state bit.)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011491 */
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080011492static void sync_vmcs12(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011493{
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011494 vmcs12->guest_cr0 = vmcs12_guest_cr0(vcpu, vmcs12);
11495 vmcs12->guest_cr4 = vmcs12_guest_cr4(vcpu, vmcs12);
11496
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011497 vmcs12->guest_rsp = kvm_register_read(vcpu, VCPU_REGS_RSP);
11498 vmcs12->guest_rip = kvm_register_read(vcpu, VCPU_REGS_RIP);
11499 vmcs12->guest_rflags = vmcs_readl(GUEST_RFLAGS);
11500
11501 vmcs12->guest_es_selector = vmcs_read16(GUEST_ES_SELECTOR);
11502 vmcs12->guest_cs_selector = vmcs_read16(GUEST_CS_SELECTOR);
11503 vmcs12->guest_ss_selector = vmcs_read16(GUEST_SS_SELECTOR);
11504 vmcs12->guest_ds_selector = vmcs_read16(GUEST_DS_SELECTOR);
11505 vmcs12->guest_fs_selector = vmcs_read16(GUEST_FS_SELECTOR);
11506 vmcs12->guest_gs_selector = vmcs_read16(GUEST_GS_SELECTOR);
11507 vmcs12->guest_ldtr_selector = vmcs_read16(GUEST_LDTR_SELECTOR);
11508 vmcs12->guest_tr_selector = vmcs_read16(GUEST_TR_SELECTOR);
11509 vmcs12->guest_es_limit = vmcs_read32(GUEST_ES_LIMIT);
11510 vmcs12->guest_cs_limit = vmcs_read32(GUEST_CS_LIMIT);
11511 vmcs12->guest_ss_limit = vmcs_read32(GUEST_SS_LIMIT);
11512 vmcs12->guest_ds_limit = vmcs_read32(GUEST_DS_LIMIT);
11513 vmcs12->guest_fs_limit = vmcs_read32(GUEST_FS_LIMIT);
11514 vmcs12->guest_gs_limit = vmcs_read32(GUEST_GS_LIMIT);
11515 vmcs12->guest_ldtr_limit = vmcs_read32(GUEST_LDTR_LIMIT);
11516 vmcs12->guest_tr_limit = vmcs_read32(GUEST_TR_LIMIT);
11517 vmcs12->guest_gdtr_limit = vmcs_read32(GUEST_GDTR_LIMIT);
11518 vmcs12->guest_idtr_limit = vmcs_read32(GUEST_IDTR_LIMIT);
11519 vmcs12->guest_es_ar_bytes = vmcs_read32(GUEST_ES_AR_BYTES);
11520 vmcs12->guest_cs_ar_bytes = vmcs_read32(GUEST_CS_AR_BYTES);
11521 vmcs12->guest_ss_ar_bytes = vmcs_read32(GUEST_SS_AR_BYTES);
11522 vmcs12->guest_ds_ar_bytes = vmcs_read32(GUEST_DS_AR_BYTES);
11523 vmcs12->guest_fs_ar_bytes = vmcs_read32(GUEST_FS_AR_BYTES);
11524 vmcs12->guest_gs_ar_bytes = vmcs_read32(GUEST_GS_AR_BYTES);
11525 vmcs12->guest_ldtr_ar_bytes = vmcs_read32(GUEST_LDTR_AR_BYTES);
11526 vmcs12->guest_tr_ar_bytes = vmcs_read32(GUEST_TR_AR_BYTES);
11527 vmcs12->guest_es_base = vmcs_readl(GUEST_ES_BASE);
11528 vmcs12->guest_cs_base = vmcs_readl(GUEST_CS_BASE);
11529 vmcs12->guest_ss_base = vmcs_readl(GUEST_SS_BASE);
11530 vmcs12->guest_ds_base = vmcs_readl(GUEST_DS_BASE);
11531 vmcs12->guest_fs_base = vmcs_readl(GUEST_FS_BASE);
11532 vmcs12->guest_gs_base = vmcs_readl(GUEST_GS_BASE);
11533 vmcs12->guest_ldtr_base = vmcs_readl(GUEST_LDTR_BASE);
11534 vmcs12->guest_tr_base = vmcs_readl(GUEST_TR_BASE);
11535 vmcs12->guest_gdtr_base = vmcs_readl(GUEST_GDTR_BASE);
11536 vmcs12->guest_idtr_base = vmcs_readl(GUEST_IDTR_BASE);
11537
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011538 vmcs12->guest_interruptibility_info =
11539 vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
11540 vmcs12->guest_pending_dbg_exceptions =
11541 vmcs_readl(GUEST_PENDING_DBG_EXCEPTIONS);
Jan Kiszka3edf1e62014-01-04 18:47:24 +010011542 if (vcpu->arch.mp_state == KVM_MP_STATE_HALTED)
11543 vmcs12->guest_activity_state = GUEST_ACTIVITY_HLT;
11544 else
11545 vmcs12->guest_activity_state = GUEST_ACTIVITY_ACTIVE;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011546
Jan Kiszkaf41245002014-03-07 20:03:13 +010011547 if (nested_cpu_has_preemption_timer(vmcs12)) {
11548 if (vmcs12->vm_exit_controls &
11549 VM_EXIT_SAVE_VMX_PREEMPTION_TIMER)
11550 vmcs12->vmx_preemption_timer_value =
11551 vmx_get_preemption_timer_value(vcpu);
11552 hrtimer_cancel(&to_vmx(vcpu)->nested.preemption_timer);
11553 }
Arthur Chunqi Li7854cbc2013-09-16 16:11:44 +080011554
Nadav Har'El3633cfc2013-08-05 11:07:07 +030011555 /*
11556 * In some cases (usually, nested EPT), L2 is allowed to change its
11557 * own CR3 without exiting. If it has changed it, we must keep it.
11558 * Of course, if L0 is using shadow page tables, GUEST_CR3 was defined
11559 * by L0, not L1 or L2, so we mustn't unconditionally copy it to vmcs12.
11560 *
11561 * Additionally, restore L2's PDPTR to vmcs12.
11562 */
11563 if (enable_ept) {
Paolo Bonzinif3531052015-12-03 15:49:56 +010011564 vmcs12->guest_cr3 = vmcs_readl(GUEST_CR3);
Nadav Har'El3633cfc2013-08-05 11:07:07 +030011565 vmcs12->guest_pdptr0 = vmcs_read64(GUEST_PDPTR0);
11566 vmcs12->guest_pdptr1 = vmcs_read64(GUEST_PDPTR1);
11567 vmcs12->guest_pdptr2 = vmcs_read64(GUEST_PDPTR2);
11568 vmcs12->guest_pdptr3 = vmcs_read64(GUEST_PDPTR3);
11569 }
11570
Jim Mattsond281e132017-06-01 12:44:46 -070011571 vmcs12->guest_linear_address = vmcs_readl(GUEST_LINEAR_ADDRESS);
Jan Dakinevich119a9c02016-09-04 21:22:47 +030011572
Wincy Van608406e2015-02-03 23:57:51 +080011573 if (nested_cpu_has_vid(vmcs12))
11574 vmcs12->guest_intr_status = vmcs_read16(GUEST_INTR_STATUS);
11575
Jan Kiszkac18911a2013-03-13 16:06:41 +010011576 vmcs12->vm_entry_controls =
11577 (vmcs12->vm_entry_controls & ~VM_ENTRY_IA32E_MODE) |
Gleb Natapov2961e8762013-11-25 15:37:13 +020011578 (vm_entry_controls_get(to_vmx(vcpu)) & VM_ENTRY_IA32E_MODE);
Jan Kiszkac18911a2013-03-13 16:06:41 +010011579
Jan Kiszka2996fca2014-06-16 13:59:43 +020011580 if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_DEBUG_CONTROLS) {
11581 kvm_get_dr(vcpu, 7, (unsigned long *)&vmcs12->guest_dr7);
11582 vmcs12->guest_ia32_debugctl = vmcs_read64(GUEST_IA32_DEBUGCTL);
11583 }
11584
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011585 /* TODO: These cannot have changed unless we have MSR bitmaps and
11586 * the relevant bit asks not to trap the change */
Jan Kiszkab8c07d52013-04-06 13:51:21 +020011587 if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_IA32_PAT)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011588 vmcs12->guest_ia32_pat = vmcs_read64(GUEST_IA32_PAT);
Jan Kiszka10ba54a2013-08-08 16:26:31 +020011589 if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_IA32_EFER)
11590 vmcs12->guest_ia32_efer = vcpu->arch.efer;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011591 vmcs12->guest_sysenter_cs = vmcs_read32(GUEST_SYSENTER_CS);
11592 vmcs12->guest_sysenter_esp = vmcs_readl(GUEST_SYSENTER_ESP);
11593 vmcs12->guest_sysenter_eip = vmcs_readl(GUEST_SYSENTER_EIP);
Paolo Bonzinia87036a2016-03-08 09:52:13 +010011594 if (kvm_mpx_supported())
Paolo Bonzini36be0b92014-02-24 12:30:04 +010011595 vmcs12->guest_bndcfgs = vmcs_read64(GUEST_BNDCFGS);
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080011596}
11597
11598/*
11599 * prepare_vmcs12 is part of what we need to do when the nested L2 guest exits
11600 * and we want to prepare to run its L1 parent. L1 keeps a vmcs for L2 (vmcs12),
11601 * and this function updates it to reflect the changes to the guest state while
11602 * L2 was running (and perhaps made some exits which were handled directly by L0
11603 * without going back to L1), and to reflect the exit reason.
11604 * Note that we do not have to copy here all VMCS fields, just those that
11605 * could have changed by the L2 guest or the exit - i.e., the guest-state and
11606 * exit-information fields only. Other fields are modified by L1 with VMWRITE,
11607 * which already writes to vmcs12 directly.
11608 */
11609static void prepare_vmcs12(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12,
11610 u32 exit_reason, u32 exit_intr_info,
11611 unsigned long exit_qualification)
11612{
11613 /* update guest state fields: */
11614 sync_vmcs12(vcpu, vmcs12);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011615
11616 /* update exit information fields: */
11617
Jan Kiszka533558b2014-01-04 18:47:20 +010011618 vmcs12->vm_exit_reason = exit_reason;
11619 vmcs12->exit_qualification = exit_qualification;
Jan Kiszka533558b2014-01-04 18:47:20 +010011620 vmcs12->vm_exit_intr_info = exit_intr_info;
Paolo Bonzini7313c692017-07-27 10:31:25 +020011621
Jan Kiszka5f3d5792013-04-14 12:12:46 +020011622 vmcs12->idt_vectoring_info_field = 0;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011623 vmcs12->vm_exit_instruction_len = vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
11624 vmcs12->vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
11625
Jan Kiszka5f3d5792013-04-14 12:12:46 +020011626 if (!(vmcs12->vm_exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY)) {
Jim Mattson7cdc2d62017-07-06 16:33:05 -070011627 vmcs12->launch_state = 1;
11628
Jan Kiszka5f3d5792013-04-14 12:12:46 +020011629 /* vm_entry_intr_info_field is cleared on exit. Emulate this
11630 * instead of reading the real value. */
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011631 vmcs12->vm_entry_intr_info_field &= ~INTR_INFO_VALID_MASK;
Jan Kiszka5f3d5792013-04-14 12:12:46 +020011632
11633 /*
11634 * Transfer the event that L0 or L1 may wanted to inject into
11635 * L2 to IDT_VECTORING_INFO_FIELD.
11636 */
11637 vmcs12_save_pending_event(vcpu, vmcs12);
11638 }
11639
11640 /*
11641 * Drop what we picked up for L2 via vmx_complete_interrupts. It is
11642 * preserved above and would only end up incorrectly in L1.
11643 */
11644 vcpu->arch.nmi_injected = false;
11645 kvm_clear_exception_queue(vcpu);
11646 kvm_clear_interrupt_queue(vcpu);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011647}
11648
Wanpeng Li5af41572017-11-05 16:54:49 -080011649static void load_vmcs12_mmu_host_state(struct kvm_vcpu *vcpu,
11650 struct vmcs12 *vmcs12)
11651{
11652 u32 entry_failure_code;
11653
11654 nested_ept_uninit_mmu_context(vcpu);
11655
11656 /*
11657 * Only PDPTE load can fail as the value of cr3 was checked on entry and
11658 * couldn't have changed.
11659 */
11660 if (nested_vmx_load_cr3(vcpu, vmcs12->host_cr3, false, &entry_failure_code))
11661 nested_vmx_abort(vcpu, VMX_ABORT_LOAD_HOST_PDPTE_FAIL);
11662
11663 if (!enable_ept)
11664 vcpu->arch.walk_mmu->inject_page_fault = kvm_inject_page_fault;
11665}
11666
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011667/*
11668 * A part of what we need to when the nested L2 guest exits and we want to
11669 * run its L1 parent, is to reset L1's guest state to the host state specified
11670 * in vmcs12.
11671 * This function is to be called not only on normal nested exit, but also on
11672 * a nested entry failure, as explained in Intel's spec, 3B.23.7 ("VM-Entry
11673 * Failures During or After Loading Guest State").
11674 * This function should be called when the active VMCS is L1's (vmcs01).
11675 */
Jan Kiszka733568f2013-02-23 15:07:47 +010011676static void load_vmcs12_host_state(struct kvm_vcpu *vcpu,
11677 struct vmcs12 *vmcs12)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011678{
Arthur Chunqi Li21feb4e2013-07-15 16:04:08 +080011679 struct kvm_segment seg;
11680
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011681 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_EFER)
11682 vcpu->arch.efer = vmcs12->host_ia32_efer;
Jan Kiszkad1fa0352013-04-14 12:44:54 +020011683 else if (vmcs12->vm_exit_controls & VM_EXIT_HOST_ADDR_SPACE_SIZE)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011684 vcpu->arch.efer |= (EFER_LMA | EFER_LME);
11685 else
11686 vcpu->arch.efer &= ~(EFER_LMA | EFER_LME);
11687 vmx_set_efer(vcpu, vcpu->arch.efer);
11688
11689 kvm_register_write(vcpu, VCPU_REGS_RSP, vmcs12->host_rsp);
11690 kvm_register_write(vcpu, VCPU_REGS_RIP, vmcs12->host_rip);
H. Peter Anvin1adfa762013-04-27 16:10:11 -070011691 vmx_set_rflags(vcpu, X86_EFLAGS_FIXED);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011692 /*
11693 * Note that calling vmx_set_cr0 is important, even if cr0 hasn't
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -080011694 * actually changed, because vmx_set_cr0 refers to efer set above.
11695 *
11696 * CR0_GUEST_HOST_MASK is already set in the original vmcs01
11697 * (KVM doesn't change it);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011698 */
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -080011699 vcpu->arch.cr0_guest_owned_bits = X86_CR0_TS;
Jan Kiszka9e3e4dbf2013-09-03 21:11:45 +020011700 vmx_set_cr0(vcpu, vmcs12->host_cr0);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011701
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -080011702 /* Same as above - no reason to call set_cr4_guest_host_mask(). */
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011703 vcpu->arch.cr4_guest_owned_bits = ~vmcs_readl(CR4_GUEST_HOST_MASK);
Haozhong Zhang8eb3f872017-10-10 15:01:22 +080011704 vmx_set_cr4(vcpu, vmcs12->host_cr4);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011705
Wanpeng Li5af41572017-11-05 16:54:49 -080011706 load_vmcs12_mmu_host_state(vcpu, vmcs12);
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +030011707
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011708 if (enable_vpid) {
11709 /*
11710 * Trivially support vpid by letting L2s share their parent
11711 * L1's vpid. TODO: move to a more elaborate solution, giving
11712 * each L2 its own vpid and exposing the vpid feature to L1.
11713 */
Wanpeng Lic2ba05c2017-12-12 17:33:03 -080011714 vmx_flush_tlb(vcpu, true);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011715 }
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011716
11717 vmcs_write32(GUEST_SYSENTER_CS, vmcs12->host_ia32_sysenter_cs);
11718 vmcs_writel(GUEST_SYSENTER_ESP, vmcs12->host_ia32_sysenter_esp);
11719 vmcs_writel(GUEST_SYSENTER_EIP, vmcs12->host_ia32_sysenter_eip);
11720 vmcs_writel(GUEST_IDTR_BASE, vmcs12->host_idtr_base);
11721 vmcs_writel(GUEST_GDTR_BASE, vmcs12->host_gdtr_base);
Ladi Prosek21f2d552017-10-11 16:54:42 +020011722 vmcs_write32(GUEST_IDTR_LIMIT, 0xFFFF);
11723 vmcs_write32(GUEST_GDTR_LIMIT, 0xFFFF);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011724
Paolo Bonzini36be0b92014-02-24 12:30:04 +010011725 /* If not VM_EXIT_CLEAR_BNDCFGS, the L2 value propagates to L1. */
11726 if (vmcs12->vm_exit_controls & VM_EXIT_CLEAR_BNDCFGS)
11727 vmcs_write64(GUEST_BNDCFGS, 0);
11728
Jan Kiszka44811c02013-08-04 17:17:27 +020011729 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PAT) {
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011730 vmcs_write64(GUEST_IA32_PAT, vmcs12->host_ia32_pat);
Jan Kiszka44811c02013-08-04 17:17:27 +020011731 vcpu->arch.pat = vmcs12->host_ia32_pat;
11732 }
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011733 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL)
11734 vmcs_write64(GUEST_IA32_PERF_GLOBAL_CTRL,
11735 vmcs12->host_ia32_perf_global_ctrl);
Jan Kiszka503cd0c2013-03-03 13:05:44 +010011736
Arthur Chunqi Li21feb4e2013-07-15 16:04:08 +080011737 /* Set L1 segment info according to Intel SDM
11738 27.5.2 Loading Host Segment and Descriptor-Table Registers */
11739 seg = (struct kvm_segment) {
11740 .base = 0,
11741 .limit = 0xFFFFFFFF,
11742 .selector = vmcs12->host_cs_selector,
11743 .type = 11,
11744 .present = 1,
11745 .s = 1,
11746 .g = 1
11747 };
11748 if (vmcs12->vm_exit_controls & VM_EXIT_HOST_ADDR_SPACE_SIZE)
11749 seg.l = 1;
11750 else
11751 seg.db = 1;
11752 vmx_set_segment(vcpu, &seg, VCPU_SREG_CS);
11753 seg = (struct kvm_segment) {
11754 .base = 0,
11755 .limit = 0xFFFFFFFF,
11756 .type = 3,
11757 .present = 1,
11758 .s = 1,
11759 .db = 1,
11760 .g = 1
11761 };
11762 seg.selector = vmcs12->host_ds_selector;
11763 vmx_set_segment(vcpu, &seg, VCPU_SREG_DS);
11764 seg.selector = vmcs12->host_es_selector;
11765 vmx_set_segment(vcpu, &seg, VCPU_SREG_ES);
11766 seg.selector = vmcs12->host_ss_selector;
11767 vmx_set_segment(vcpu, &seg, VCPU_SREG_SS);
11768 seg.selector = vmcs12->host_fs_selector;
11769 seg.base = vmcs12->host_fs_base;
11770 vmx_set_segment(vcpu, &seg, VCPU_SREG_FS);
11771 seg.selector = vmcs12->host_gs_selector;
11772 seg.base = vmcs12->host_gs_base;
11773 vmx_set_segment(vcpu, &seg, VCPU_SREG_GS);
11774 seg = (struct kvm_segment) {
Gleb Natapov205befd2013-08-04 15:08:06 +030011775 .base = vmcs12->host_tr_base,
Arthur Chunqi Li21feb4e2013-07-15 16:04:08 +080011776 .limit = 0x67,
11777 .selector = vmcs12->host_tr_selector,
11778 .type = 11,
11779 .present = 1
11780 };
11781 vmx_set_segment(vcpu, &seg, VCPU_SREG_TR);
11782
Jan Kiszka503cd0c2013-03-03 13:05:44 +010011783 kvm_set_dr(vcpu, 7, 0x400);
11784 vmcs_write64(GUEST_IA32_DEBUGCTL, 0);
Wincy Vanff651cb2014-12-11 08:52:58 +030011785
Wincy Van3af18d92015-02-03 23:49:31 +080011786 if (cpu_has_vmx_msr_bitmap())
Paolo Bonzini904e14f2018-01-16 16:51:18 +010011787 vmx_update_msr_bitmap(vcpu);
Wincy Van3af18d92015-02-03 23:49:31 +080011788
Wincy Vanff651cb2014-12-11 08:52:58 +030011789 if (nested_vmx_load_msr(vcpu, vmcs12->vm_exit_msr_load_addr,
11790 vmcs12->vm_exit_msr_load_count))
11791 nested_vmx_abort(vcpu, VMX_ABORT_LOAD_HOST_MSR_FAIL);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011792}
11793
11794/*
11795 * Emulate an exit from nested guest (L2) to L1, i.e., prepare to run L1
11796 * and modify vmcs12 to make it see what it would expect to see there if
11797 * L2 was its real guest. Must only be called when in L2 (is_guest_mode())
11798 */
Jan Kiszka533558b2014-01-04 18:47:20 +010011799static void nested_vmx_vmexit(struct kvm_vcpu *vcpu, u32 exit_reason,
11800 u32 exit_intr_info,
11801 unsigned long exit_qualification)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011802{
11803 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011804 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
11805
Jan Kiszka5f3d5792013-04-14 12:12:46 +020011806 /* trying to cancel vmlaunch/vmresume is a bug */
11807 WARN_ON_ONCE(vmx->nested.nested_run_pending);
11808
Wanpeng Li6550c4d2017-07-31 19:25:27 -070011809 /*
Jim Mattson4f350c62017-09-14 16:31:44 -070011810 * The only expected VM-instruction error is "VM entry with
11811 * invalid control field(s)." Anything else indicates a
11812 * problem with L0.
Wanpeng Li6550c4d2017-07-31 19:25:27 -070011813 */
Jim Mattson4f350c62017-09-14 16:31:44 -070011814 WARN_ON_ONCE(vmx->fail && (vmcs_read32(VM_INSTRUCTION_ERROR) !=
11815 VMXERR_ENTRY_INVALID_CONTROL_FIELD));
11816
11817 leave_guest_mode(vcpu);
11818
11819 if (likely(!vmx->fail)) {
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020011820 if (exit_reason == -1)
11821 sync_vmcs12(vcpu, vmcs12);
11822 else
11823 prepare_vmcs12(vcpu, vmcs12, exit_reason, exit_intr_info,
11824 exit_qualification);
Jim Mattson4f350c62017-09-14 16:31:44 -070011825
11826 if (nested_vmx_store_msr(vcpu, vmcs12->vm_exit_msr_store_addr,
11827 vmcs12->vm_exit_msr_store_count))
11828 nested_vmx_abort(vcpu, VMX_ABORT_SAVE_GUEST_MSR_FAIL);
Bandan Das77b0f5d2014-04-19 18:17:45 -040011829 }
11830
Jim Mattson4f350c62017-09-14 16:31:44 -070011831 vmx_switch_vmcs(vcpu, &vmx->vmcs01);
Paolo Bonzini8391ce42016-07-07 14:58:33 +020011832 vm_entry_controls_reset_shadow(vmx);
11833 vm_exit_controls_reset_shadow(vmx);
Jan Kiszka36c3cc42013-02-23 22:35:37 +010011834 vmx_segment_cache_clear(vmx);
11835
Paolo Bonzini9314006db2016-07-06 13:23:51 +020011836 /* Update any VMCS fields that might have changed while L2 ran */
Jim Mattson83bafef2016-10-04 10:48:38 -070011837 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, vmx->msr_autoload.nr);
11838 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, vmx->msr_autoload.nr);
Paolo Bonziniea26e4e2016-11-01 00:39:48 +010011839 vmcs_write64(TSC_OFFSET, vcpu->arch.tsc_offset);
Paolo Bonzini9314006db2016-07-06 13:23:51 +020011840 if (vmx->hv_deadline_tsc == -1)
11841 vmcs_clear_bits(PIN_BASED_VM_EXEC_CONTROL,
11842 PIN_BASED_VMX_PREEMPTION_TIMER);
11843 else
11844 vmcs_set_bits(PIN_BASED_VM_EXEC_CONTROL,
11845 PIN_BASED_VMX_PREEMPTION_TIMER);
Peter Feinerc95ba922016-08-17 09:36:47 -070011846 if (kvm_has_tsc_control)
11847 decache_tsc_multiplier(vmx);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011848
Radim Krčmářdccbfcf2016-08-08 20:16:23 +020011849 if (vmx->nested.change_vmcs01_virtual_x2apic_mode) {
11850 vmx->nested.change_vmcs01_virtual_x2apic_mode = false;
11851 vmx_set_virtual_x2apic_mode(vcpu,
11852 vcpu->arch.apic_base & X2APIC_ENABLE);
Jim Mattsonfb6c8192017-03-16 13:53:59 -070011853 } else if (!nested_cpu_has_ept(vmcs12) &&
11854 nested_cpu_has2(vmcs12,
11855 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) {
11856 vmx_flush_tlb_ept_only(vcpu);
Radim Krčmářdccbfcf2016-08-08 20:16:23 +020011857 }
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011858
11859 /* This is needed for same reason as it was needed in prepare_vmcs02 */
11860 vmx->host_rsp = 0;
11861
11862 /* Unpin physical memory we referred to in vmcs02 */
11863 if (vmx->nested.apic_access_page) {
David Hildenbrand53a70da2017-08-03 18:11:05 +020011864 kvm_release_page_dirty(vmx->nested.apic_access_page);
Paolo Bonzini48d89b92014-08-26 13:27:46 +020011865 vmx->nested.apic_access_page = NULL;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011866 }
Wanpeng Lia7c0b072014-08-21 19:46:50 +080011867 if (vmx->nested.virtual_apic_page) {
David Hildenbrand53a70da2017-08-03 18:11:05 +020011868 kvm_release_page_dirty(vmx->nested.virtual_apic_page);
Paolo Bonzini48d89b92014-08-26 13:27:46 +020011869 vmx->nested.virtual_apic_page = NULL;
Wanpeng Lia7c0b072014-08-21 19:46:50 +080011870 }
Wincy Van705699a2015-02-03 23:58:17 +080011871 if (vmx->nested.pi_desc_page) {
11872 kunmap(vmx->nested.pi_desc_page);
David Hildenbrand53a70da2017-08-03 18:11:05 +020011873 kvm_release_page_dirty(vmx->nested.pi_desc_page);
Wincy Van705699a2015-02-03 23:58:17 +080011874 vmx->nested.pi_desc_page = NULL;
11875 vmx->nested.pi_desc = NULL;
11876 }
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011877
11878 /*
Tang Chen38b99172014-09-24 15:57:54 +080011879 * We are now running in L2, mmu_notifier will force to reload the
11880 * page's hpa for L2 vmcs. Need to reload it for L1 before entering L1.
11881 */
Wanpeng Lic83b6d12016-09-06 17:20:33 +080011882 kvm_make_request(KVM_REQ_APIC_PAGE_RELOAD, vcpu);
Tang Chen38b99172014-09-24 15:57:54 +080011883
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020011884 if (enable_shadow_vmcs && exit_reason != -1)
Abel Gordon012f83c2013-04-18 14:39:25 +030011885 vmx->nested.sync_shadow_vmcs = true;
Jan Kiszkab6b8a142014-03-07 20:03:12 +010011886
11887 /* in case we halted in L2 */
11888 vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
Jim Mattson4f350c62017-09-14 16:31:44 -070011889
11890 if (likely(!vmx->fail)) {
11891 /*
11892 * TODO: SDM says that with acknowledge interrupt on
11893 * exit, bit 31 of the VM-exit interrupt information
11894 * (valid interrupt) is always set to 1 on
11895 * EXIT_REASON_EXTERNAL_INTERRUPT, so we shouldn't
11896 * need kvm_cpu_has_interrupt(). See the commit
11897 * message for details.
11898 */
11899 if (nested_exit_intr_ack_set(vcpu) &&
11900 exit_reason == EXIT_REASON_EXTERNAL_INTERRUPT &&
11901 kvm_cpu_has_interrupt(vcpu)) {
11902 int irq = kvm_cpu_get_interrupt(vcpu);
11903 WARN_ON(irq < 0);
11904 vmcs12->vm_exit_intr_info = irq |
11905 INTR_INFO_VALID_MASK | INTR_TYPE_EXT_INTR;
11906 }
11907
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020011908 if (exit_reason != -1)
11909 trace_kvm_nested_vmexit_inject(vmcs12->vm_exit_reason,
11910 vmcs12->exit_qualification,
11911 vmcs12->idt_vectoring_info_field,
11912 vmcs12->vm_exit_intr_info,
11913 vmcs12->vm_exit_intr_error_code,
11914 KVM_ISA_VMX);
Jim Mattson4f350c62017-09-14 16:31:44 -070011915
11916 load_vmcs12_host_state(vcpu, vmcs12);
11917
11918 return;
11919 }
11920
11921 /*
11922 * After an early L2 VM-entry failure, we're now back
11923 * in L1 which thinks it just finished a VMLAUNCH or
11924 * VMRESUME instruction, so we need to set the failure
11925 * flag and the VM-instruction error field of the VMCS
11926 * accordingly.
11927 */
11928 nested_vmx_failValid(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD);
Wanpeng Li5af41572017-11-05 16:54:49 -080011929
11930 load_vmcs12_mmu_host_state(vcpu, vmcs12);
11931
Jim Mattson4f350c62017-09-14 16:31:44 -070011932 /*
11933 * The emulated instruction was already skipped in
11934 * nested_vmx_run, but the updated RIP was never
11935 * written back to the vmcs01.
11936 */
11937 skip_emulated_instruction(vcpu);
11938 vmx->fail = 0;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011939}
11940
Nadav Har'El7c177932011-05-25 23:12:04 +030011941/*
Jan Kiszka42124922014-01-04 18:47:19 +010011942 * Forcibly leave nested mode in order to be able to reset the VCPU later on.
11943 */
11944static void vmx_leave_nested(struct kvm_vcpu *vcpu)
11945{
Wanpeng Li2f707d92017-03-06 04:03:28 -080011946 if (is_guest_mode(vcpu)) {
11947 to_vmx(vcpu)->nested.nested_run_pending = 0;
Jan Kiszka533558b2014-01-04 18:47:20 +010011948 nested_vmx_vmexit(vcpu, -1, 0, 0);
Wanpeng Li2f707d92017-03-06 04:03:28 -080011949 }
Jan Kiszka42124922014-01-04 18:47:19 +010011950 free_nested(to_vmx(vcpu));
11951}
11952
11953/*
Nadav Har'El7c177932011-05-25 23:12:04 +030011954 * L1's failure to enter L2 is a subset of a normal exit, as explained in
11955 * 23.7 "VM-entry failures during or after loading guest state" (this also
11956 * lists the acceptable exit-reason and exit-qualification parameters).
11957 * It should only be called before L2 actually succeeded to run, and when
11958 * vmcs01 is current (it doesn't leave_guest_mode() or switch vmcss).
11959 */
11960static void nested_vmx_entry_failure(struct kvm_vcpu *vcpu,
11961 struct vmcs12 *vmcs12,
11962 u32 reason, unsigned long qualification)
11963{
11964 load_vmcs12_host_state(vcpu, vmcs12);
11965 vmcs12->vm_exit_reason = reason | VMX_EXIT_REASONS_FAILED_VMENTRY;
11966 vmcs12->exit_qualification = qualification;
11967 nested_vmx_succeed(vcpu);
Abel Gordon012f83c2013-04-18 14:39:25 +030011968 if (enable_shadow_vmcs)
11969 to_vmx(vcpu)->nested.sync_shadow_vmcs = true;
Nadav Har'El7c177932011-05-25 23:12:04 +030011970}
11971
Joerg Roedel8a76d7f2011-04-04 12:39:27 +020011972static int vmx_check_intercept(struct kvm_vcpu *vcpu,
11973 struct x86_instruction_info *info,
11974 enum x86_intercept_stage stage)
11975{
Paolo Bonzinifb6d4d32016-07-12 11:04:26 +020011976 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
11977 struct x86_emulate_ctxt *ctxt = &vcpu->arch.emulate_ctxt;
11978
11979 /*
11980 * RDPID causes #UD if disabled through secondary execution controls.
11981 * Because it is marked as EmulateOnUD, we need to intercept it here.
11982 */
11983 if (info->intercept == x86_intercept_rdtscp &&
11984 !nested_cpu_has2(vmcs12, SECONDARY_EXEC_RDTSCP)) {
11985 ctxt->exception.vector = UD_VECTOR;
11986 ctxt->exception.error_code_valid = false;
11987 return X86EMUL_PROPAGATE_FAULT;
11988 }
11989
11990 /* TODO: check more intercepts... */
Joerg Roedel8a76d7f2011-04-04 12:39:27 +020011991 return X86EMUL_CONTINUE;
11992}
11993
Yunhong Jiang64672c92016-06-13 14:19:59 -070011994#ifdef CONFIG_X86_64
11995/* (a << shift) / divisor, return 1 if overflow otherwise 0 */
11996static inline int u64_shl_div_u64(u64 a, unsigned int shift,
11997 u64 divisor, u64 *result)
11998{
11999 u64 low = a << shift, high = a >> (64 - shift);
12000
12001 /* To avoid the overflow on divq */
12002 if (high >= divisor)
12003 return 1;
12004
12005 /* Low hold the result, high hold rem which is discarded */
12006 asm("divq %2\n\t" : "=a" (low), "=d" (high) :
12007 "rm" (divisor), "0" (low), "1" (high));
12008 *result = low;
12009
12010 return 0;
12011}
12012
12013static int vmx_set_hv_timer(struct kvm_vcpu *vcpu, u64 guest_deadline_tsc)
12014{
12015 struct vcpu_vmx *vmx = to_vmx(vcpu);
Paolo Bonzini9175d2e2016-06-27 15:08:01 +020012016 u64 tscl = rdtsc();
12017 u64 guest_tscl = kvm_read_l1_tsc(vcpu, tscl);
12018 u64 delta_tsc = max(guest_deadline_tsc, guest_tscl) - guest_tscl;
Yunhong Jiang64672c92016-06-13 14:19:59 -070012019
12020 /* Convert to host delta tsc if tsc scaling is enabled */
12021 if (vcpu->arch.tsc_scaling_ratio != kvm_default_tsc_scaling_ratio &&
12022 u64_shl_div_u64(delta_tsc,
12023 kvm_tsc_scaling_ratio_frac_bits,
12024 vcpu->arch.tsc_scaling_ratio,
12025 &delta_tsc))
12026 return -ERANGE;
12027
12028 /*
12029 * If the delta tsc can't fit in the 32 bit after the multi shift,
12030 * we can't use the preemption timer.
12031 * It's possible that it fits on later vmentries, but checking
12032 * on every vmentry is costly so we just use an hrtimer.
12033 */
12034 if (delta_tsc >> (cpu_preemption_timer_multi + 32))
12035 return -ERANGE;
12036
12037 vmx->hv_deadline_tsc = tscl + delta_tsc;
12038 vmcs_set_bits(PIN_BASED_VM_EXEC_CONTROL,
12039 PIN_BASED_VMX_PREEMPTION_TIMER);
Wanpeng Lic8533542017-06-29 06:28:09 -070012040
12041 return delta_tsc == 0;
Yunhong Jiang64672c92016-06-13 14:19:59 -070012042}
12043
12044static void vmx_cancel_hv_timer(struct kvm_vcpu *vcpu)
12045{
12046 struct vcpu_vmx *vmx = to_vmx(vcpu);
12047 vmx->hv_deadline_tsc = -1;
12048 vmcs_clear_bits(PIN_BASED_VM_EXEC_CONTROL,
12049 PIN_BASED_VMX_PREEMPTION_TIMER);
12050}
12051#endif
12052
Paolo Bonzini48d89b92014-08-26 13:27:46 +020012053static void vmx_sched_in(struct kvm_vcpu *vcpu, int cpu)
Radim Krčmářae97a3b2014-08-21 18:08:06 +020012054{
Radim Krčmářb4a2d312014-08-21 18:08:08 +020012055 if (ple_gap)
12056 shrink_ple_window(vcpu);
Radim Krčmářae97a3b2014-08-21 18:08:06 +020012057}
12058
Kai Huang843e4332015-01-28 10:54:28 +080012059static void vmx_slot_enable_log_dirty(struct kvm *kvm,
12060 struct kvm_memory_slot *slot)
12061{
12062 kvm_mmu_slot_leaf_clear_dirty(kvm, slot);
12063 kvm_mmu_slot_largepage_remove_write_access(kvm, slot);
12064}
12065
12066static void vmx_slot_disable_log_dirty(struct kvm *kvm,
12067 struct kvm_memory_slot *slot)
12068{
12069 kvm_mmu_slot_set_dirty(kvm, slot);
12070}
12071
12072static void vmx_flush_log_dirty(struct kvm *kvm)
12073{
12074 kvm_flush_pml_buffers(kvm);
12075}
12076
Bandan Dasc5f983f2017-05-05 15:25:14 -040012077static int vmx_write_pml_buffer(struct kvm_vcpu *vcpu)
12078{
12079 struct vmcs12 *vmcs12;
12080 struct vcpu_vmx *vmx = to_vmx(vcpu);
12081 gpa_t gpa;
12082 struct page *page = NULL;
12083 u64 *pml_address;
12084
12085 if (is_guest_mode(vcpu)) {
12086 WARN_ON_ONCE(vmx->nested.pml_full);
12087
12088 /*
12089 * Check if PML is enabled for the nested guest.
12090 * Whether eptp bit 6 is set is already checked
12091 * as part of A/D emulation.
12092 */
12093 vmcs12 = get_vmcs12(vcpu);
12094 if (!nested_cpu_has_pml(vmcs12))
12095 return 0;
12096
Dan Carpenter47698862017-05-10 22:43:17 +030012097 if (vmcs12->guest_pml_index >= PML_ENTITY_NUM) {
Bandan Dasc5f983f2017-05-05 15:25:14 -040012098 vmx->nested.pml_full = true;
12099 return 1;
12100 }
12101
12102 gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS) & ~0xFFFull;
12103
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020012104 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->pml_address);
12105 if (is_error_page(page))
Bandan Dasc5f983f2017-05-05 15:25:14 -040012106 return 0;
12107
12108 pml_address = kmap(page);
12109 pml_address[vmcs12->guest_pml_index--] = gpa;
12110 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +020012111 kvm_release_page_clean(page);
Bandan Dasc5f983f2017-05-05 15:25:14 -040012112 }
12113
12114 return 0;
12115}
12116
Kai Huang843e4332015-01-28 10:54:28 +080012117static void vmx_enable_log_dirty_pt_masked(struct kvm *kvm,
12118 struct kvm_memory_slot *memslot,
12119 gfn_t offset, unsigned long mask)
12120{
12121 kvm_mmu_clear_dirty_pt_masked(kvm, memslot, offset, mask);
12122}
12123
Paolo Bonzinicd39e112017-06-06 12:57:04 +020012124static void __pi_post_block(struct kvm_vcpu *vcpu)
12125{
12126 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
12127 struct pi_desc old, new;
12128 unsigned int dest;
Paolo Bonzinicd39e112017-06-06 12:57:04 +020012129
12130 do {
12131 old.control = new.control = pi_desc->control;
Paolo Bonzini8b306e22017-06-06 12:57:05 +020012132 WARN(old.nv != POSTED_INTR_WAKEUP_VECTOR,
12133 "Wakeup handler not enabled while the VCPU is blocked\n");
Paolo Bonzinicd39e112017-06-06 12:57:04 +020012134
12135 dest = cpu_physical_id(vcpu->cpu);
12136
12137 if (x2apic_enabled())
12138 new.ndst = dest;
12139 else
12140 new.ndst = (dest << 8) & 0xFF00;
12141
Paolo Bonzinicd39e112017-06-06 12:57:04 +020012142 /* set 'NV' to 'notification vector' */
12143 new.nv = POSTED_INTR_VECTOR;
Paolo Bonzinic0a16662017-09-28 17:58:41 +020012144 } while (cmpxchg64(&pi_desc->control, old.control,
12145 new.control) != old.control);
Paolo Bonzinicd39e112017-06-06 12:57:04 +020012146
Paolo Bonzini8b306e22017-06-06 12:57:05 +020012147 if (!WARN_ON_ONCE(vcpu->pre_pcpu == -1)) {
12148 spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
Paolo Bonzinicd39e112017-06-06 12:57:04 +020012149 list_del(&vcpu->blocked_vcpu_list);
Paolo Bonzini8b306e22017-06-06 12:57:05 +020012150 spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
Paolo Bonzinicd39e112017-06-06 12:57:04 +020012151 vcpu->pre_pcpu = -1;
12152 }
12153}
12154
Feng Wuefc64402015-09-18 22:29:51 +080012155/*
Feng Wubf9f6ac2015-09-18 22:29:55 +080012156 * This routine does the following things for vCPU which is going
12157 * to be blocked if VT-d PI is enabled.
12158 * - Store the vCPU to the wakeup list, so when interrupts happen
12159 * we can find the right vCPU to wake up.
12160 * - Change the Posted-interrupt descriptor as below:
12161 * 'NDST' <-- vcpu->pre_pcpu
12162 * 'NV' <-- POSTED_INTR_WAKEUP_VECTOR
12163 * - If 'ON' is set during this process, which means at least one
12164 * interrupt is posted for this vCPU, we cannot block it, in
12165 * this case, return 1, otherwise, return 0.
12166 *
12167 */
Yunhong Jiangbc225122016-06-13 14:19:58 -070012168static int pi_pre_block(struct kvm_vcpu *vcpu)
Feng Wubf9f6ac2015-09-18 22:29:55 +080012169{
Feng Wubf9f6ac2015-09-18 22:29:55 +080012170 unsigned int dest;
12171 struct pi_desc old, new;
12172 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
12173
12174 if (!kvm_arch_has_assigned_device(vcpu->kvm) ||
Yang Zhanga0052192016-06-13 09:56:56 +080012175 !irq_remapping_cap(IRQ_POSTING_CAP) ||
12176 !kvm_vcpu_apicv_active(vcpu))
Feng Wubf9f6ac2015-09-18 22:29:55 +080012177 return 0;
12178
Paolo Bonzini8b306e22017-06-06 12:57:05 +020012179 WARN_ON(irqs_disabled());
12180 local_irq_disable();
12181 if (!WARN_ON_ONCE(vcpu->pre_pcpu != -1)) {
12182 vcpu->pre_pcpu = vcpu->cpu;
12183 spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
12184 list_add_tail(&vcpu->blocked_vcpu_list,
12185 &per_cpu(blocked_vcpu_on_cpu,
12186 vcpu->pre_pcpu));
12187 spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
12188 }
Feng Wubf9f6ac2015-09-18 22:29:55 +080012189
12190 do {
12191 old.control = new.control = pi_desc->control;
12192
Feng Wubf9f6ac2015-09-18 22:29:55 +080012193 WARN((pi_desc->sn == 1),
12194 "Warning: SN field of posted-interrupts "
12195 "is set before blocking\n");
12196
12197 /*
12198 * Since vCPU can be preempted during this process,
12199 * vcpu->cpu could be different with pre_pcpu, we
12200 * need to set pre_pcpu as the destination of wakeup
12201 * notification event, then we can find the right vCPU
12202 * to wakeup in wakeup handler if interrupts happen
12203 * when the vCPU is in blocked state.
12204 */
12205 dest = cpu_physical_id(vcpu->pre_pcpu);
12206
12207 if (x2apic_enabled())
12208 new.ndst = dest;
12209 else
12210 new.ndst = (dest << 8) & 0xFF00;
12211
12212 /* set 'NV' to 'wakeup vector' */
12213 new.nv = POSTED_INTR_WAKEUP_VECTOR;
Paolo Bonzinic0a16662017-09-28 17:58:41 +020012214 } while (cmpxchg64(&pi_desc->control, old.control,
12215 new.control) != old.control);
Feng Wubf9f6ac2015-09-18 22:29:55 +080012216
Paolo Bonzini8b306e22017-06-06 12:57:05 +020012217 /* We should not block the vCPU if an interrupt is posted for it. */
12218 if (pi_test_on(pi_desc) == 1)
12219 __pi_post_block(vcpu);
12220
12221 local_irq_enable();
12222 return (vcpu->pre_pcpu == -1);
Feng Wubf9f6ac2015-09-18 22:29:55 +080012223}
12224
Yunhong Jiangbc225122016-06-13 14:19:58 -070012225static int vmx_pre_block(struct kvm_vcpu *vcpu)
12226{
12227 if (pi_pre_block(vcpu))
12228 return 1;
12229
Yunhong Jiang64672c92016-06-13 14:19:59 -070012230 if (kvm_lapic_hv_timer_in_use(vcpu))
12231 kvm_lapic_switch_to_sw_timer(vcpu);
12232
Yunhong Jiangbc225122016-06-13 14:19:58 -070012233 return 0;
12234}
12235
12236static void pi_post_block(struct kvm_vcpu *vcpu)
Feng Wubf9f6ac2015-09-18 22:29:55 +080012237{
Paolo Bonzini8b306e22017-06-06 12:57:05 +020012238 if (vcpu->pre_pcpu == -1)
Feng Wubf9f6ac2015-09-18 22:29:55 +080012239 return;
12240
Paolo Bonzini8b306e22017-06-06 12:57:05 +020012241 WARN_ON(irqs_disabled());
12242 local_irq_disable();
Paolo Bonzinicd39e112017-06-06 12:57:04 +020012243 __pi_post_block(vcpu);
Paolo Bonzini8b306e22017-06-06 12:57:05 +020012244 local_irq_enable();
Feng Wubf9f6ac2015-09-18 22:29:55 +080012245}
12246
Yunhong Jiangbc225122016-06-13 14:19:58 -070012247static void vmx_post_block(struct kvm_vcpu *vcpu)
12248{
Yunhong Jiang64672c92016-06-13 14:19:59 -070012249 if (kvm_x86_ops->set_hv_timer)
12250 kvm_lapic_switch_to_hv_timer(vcpu);
12251
Yunhong Jiangbc225122016-06-13 14:19:58 -070012252 pi_post_block(vcpu);
12253}
12254
Feng Wubf9f6ac2015-09-18 22:29:55 +080012255/*
Feng Wuefc64402015-09-18 22:29:51 +080012256 * vmx_update_pi_irte - set IRTE for Posted-Interrupts
12257 *
12258 * @kvm: kvm
12259 * @host_irq: host irq of the interrupt
12260 * @guest_irq: gsi of the interrupt
12261 * @set: set or unset PI
12262 * returns 0 on success, < 0 on failure
12263 */
12264static int vmx_update_pi_irte(struct kvm *kvm, unsigned int host_irq,
12265 uint32_t guest_irq, bool set)
12266{
12267 struct kvm_kernel_irq_routing_entry *e;
12268 struct kvm_irq_routing_table *irq_rt;
12269 struct kvm_lapic_irq irq;
12270 struct kvm_vcpu *vcpu;
12271 struct vcpu_data vcpu_info;
Jan H. Schönherr3a8b0672017-09-07 19:02:30 +010012272 int idx, ret = 0;
Feng Wuefc64402015-09-18 22:29:51 +080012273
12274 if (!kvm_arch_has_assigned_device(kvm) ||
Yang Zhanga0052192016-06-13 09:56:56 +080012275 !irq_remapping_cap(IRQ_POSTING_CAP) ||
12276 !kvm_vcpu_apicv_active(kvm->vcpus[0]))
Feng Wuefc64402015-09-18 22:29:51 +080012277 return 0;
12278
12279 idx = srcu_read_lock(&kvm->irq_srcu);
12280 irq_rt = srcu_dereference(kvm->irq_routing, &kvm->irq_srcu);
Jan H. Schönherr3a8b0672017-09-07 19:02:30 +010012281 if (guest_irq >= irq_rt->nr_rt_entries ||
12282 hlist_empty(&irq_rt->map[guest_irq])) {
12283 pr_warn_once("no route for guest_irq %u/%u (broken user space?)\n",
12284 guest_irq, irq_rt->nr_rt_entries);
12285 goto out;
12286 }
Feng Wuefc64402015-09-18 22:29:51 +080012287
12288 hlist_for_each_entry(e, &irq_rt->map[guest_irq], link) {
12289 if (e->type != KVM_IRQ_ROUTING_MSI)
12290 continue;
12291 /*
12292 * VT-d PI cannot support posting multicast/broadcast
12293 * interrupts to a vCPU, we still use interrupt remapping
12294 * for these kind of interrupts.
12295 *
12296 * For lowest-priority interrupts, we only support
12297 * those with single CPU as the destination, e.g. user
12298 * configures the interrupts via /proc/irq or uses
12299 * irqbalance to make the interrupts single-CPU.
12300 *
12301 * We will support full lowest-priority interrupt later.
12302 */
12303
Radim Krčmář371313132016-07-12 22:09:27 +020012304 kvm_set_msi_irq(kvm, e, &irq);
Feng Wu23a1c252016-01-25 16:53:32 +080012305 if (!kvm_intr_is_single_vcpu(kvm, &irq, &vcpu)) {
12306 /*
12307 * Make sure the IRTE is in remapped mode if
12308 * we don't handle it in posted mode.
12309 */
12310 ret = irq_set_vcpu_affinity(host_irq, NULL);
12311 if (ret < 0) {
12312 printk(KERN_INFO
12313 "failed to back to remapped mode, irq: %u\n",
12314 host_irq);
12315 goto out;
12316 }
12317
Feng Wuefc64402015-09-18 22:29:51 +080012318 continue;
Feng Wu23a1c252016-01-25 16:53:32 +080012319 }
Feng Wuefc64402015-09-18 22:29:51 +080012320
12321 vcpu_info.pi_desc_addr = __pa(vcpu_to_pi_desc(vcpu));
12322 vcpu_info.vector = irq.vector;
12323
Feng Wub6ce9782016-01-25 16:53:35 +080012324 trace_kvm_pi_irte_update(vcpu->vcpu_id, host_irq, e->gsi,
Feng Wuefc64402015-09-18 22:29:51 +080012325 vcpu_info.vector, vcpu_info.pi_desc_addr, set);
12326
12327 if (set)
12328 ret = irq_set_vcpu_affinity(host_irq, &vcpu_info);
Haozhong Zhangdc91f2e2017-09-18 09:56:49 +080012329 else
Feng Wuefc64402015-09-18 22:29:51 +080012330 ret = irq_set_vcpu_affinity(host_irq, NULL);
Feng Wuefc64402015-09-18 22:29:51 +080012331
12332 if (ret < 0) {
12333 printk(KERN_INFO "%s: failed to update PI IRTE\n",
12334 __func__);
12335 goto out;
12336 }
12337 }
12338
12339 ret = 0;
12340out:
12341 srcu_read_unlock(&kvm->irq_srcu, idx);
12342 return ret;
12343}
12344
Ashok Rajc45dcc72016-06-22 14:59:56 +080012345static void vmx_setup_mce(struct kvm_vcpu *vcpu)
12346{
12347 if (vcpu->arch.mcg_cap & MCG_LMCE_P)
12348 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits |=
12349 FEATURE_CONTROL_LMCE;
12350 else
12351 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits &=
12352 ~FEATURE_CONTROL_LMCE;
12353}
12354
Ladi Prosek72d7b372017-10-11 16:54:41 +020012355static int vmx_smi_allowed(struct kvm_vcpu *vcpu)
12356{
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020012357 /* we need a nested vmexit to enter SMM, postpone if run is pending */
12358 if (to_vmx(vcpu)->nested.nested_run_pending)
12359 return 0;
Ladi Prosek72d7b372017-10-11 16:54:41 +020012360 return 1;
12361}
12362
Ladi Prosek0234bf82017-10-11 16:54:40 +020012363static int vmx_pre_enter_smm(struct kvm_vcpu *vcpu, char *smstate)
12364{
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020012365 struct vcpu_vmx *vmx = to_vmx(vcpu);
12366
12367 vmx->nested.smm.guest_mode = is_guest_mode(vcpu);
12368 if (vmx->nested.smm.guest_mode)
12369 nested_vmx_vmexit(vcpu, -1, 0, 0);
12370
12371 vmx->nested.smm.vmxon = vmx->nested.vmxon;
12372 vmx->nested.vmxon = false;
Wanpeng Licaa057a2018-03-12 04:53:03 -070012373 vmx_clear_hlt(vcpu);
Ladi Prosek0234bf82017-10-11 16:54:40 +020012374 return 0;
12375}
12376
12377static int vmx_pre_leave_smm(struct kvm_vcpu *vcpu, u64 smbase)
12378{
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020012379 struct vcpu_vmx *vmx = to_vmx(vcpu);
12380 int ret;
12381
12382 if (vmx->nested.smm.vmxon) {
12383 vmx->nested.vmxon = true;
12384 vmx->nested.smm.vmxon = false;
12385 }
12386
12387 if (vmx->nested.smm.guest_mode) {
12388 vcpu->arch.hflags &= ~HF_SMM_MASK;
12389 ret = enter_vmx_non_root_mode(vcpu, false);
12390 vcpu->arch.hflags |= HF_SMM_MASK;
12391 if (ret)
12392 return ret;
12393
12394 vmx->nested.smm.guest_mode = false;
12395 }
Ladi Prosek0234bf82017-10-11 16:54:40 +020012396 return 0;
12397}
12398
Ladi Prosekcc3d9672017-10-17 16:02:39 +020012399static int enable_smi_window(struct kvm_vcpu *vcpu)
12400{
12401 return 0;
12402}
12403
Kees Cook404f6aa2016-08-08 16:29:06 -070012404static struct kvm_x86_ops vmx_x86_ops __ro_after_init = {
Avi Kivity6aa8b732006-12-10 02:21:36 -080012405 .cpu_has_kvm_support = cpu_has_kvm_support,
12406 .disabled_by_bios = vmx_disabled_by_bios,
12407 .hardware_setup = hardware_setup,
12408 .hardware_unsetup = hardware_unsetup,
Yang, Sheng002c7f72007-07-31 14:23:01 +030012409 .check_processor_compatibility = vmx_check_processor_compat,
Avi Kivity6aa8b732006-12-10 02:21:36 -080012410 .hardware_enable = hardware_enable,
12411 .hardware_disable = hardware_disable,
Sheng Yang04547152009-04-01 15:52:31 +080012412 .cpu_has_accelerated_tpr = report_flexpriority,
Paolo Bonzini6d396b52015-04-01 14:25:33 +020012413 .cpu_has_high_real_mode_segbase = vmx_has_high_real_mode_segbase,
Avi Kivity6aa8b732006-12-10 02:21:36 -080012414
12415 .vcpu_create = vmx_create_vcpu,
12416 .vcpu_free = vmx_free_vcpu,
Avi Kivity04d2cc72007-09-10 18:10:54 +030012417 .vcpu_reset = vmx_vcpu_reset,
Avi Kivity6aa8b732006-12-10 02:21:36 -080012418
Avi Kivity04d2cc72007-09-10 18:10:54 +030012419 .prepare_guest_switch = vmx_save_host_state,
Avi Kivity6aa8b732006-12-10 02:21:36 -080012420 .vcpu_load = vmx_vcpu_load,
12421 .vcpu_put = vmx_vcpu_put,
12422
Paolo Bonzinia96036b2015-11-10 11:55:36 +010012423 .update_bp_intercept = update_exception_bitmap,
Tom Lendacky801e4592018-02-21 13:39:51 -060012424 .get_msr_feature = vmx_get_msr_feature,
Avi Kivity6aa8b732006-12-10 02:21:36 -080012425 .get_msr = vmx_get_msr,
12426 .set_msr = vmx_set_msr,
12427 .get_segment_base = vmx_get_segment_base,
12428 .get_segment = vmx_get_segment,
12429 .set_segment = vmx_set_segment,
Izik Eidus2e4d2652008-03-24 19:38:34 +020012430 .get_cpl = vmx_get_cpl,
Avi Kivity6aa8b732006-12-10 02:21:36 -080012431 .get_cs_db_l_bits = vmx_get_cs_db_l_bits,
Avi Kivitye8467fd2009-12-29 18:43:06 +020012432 .decache_cr0_guest_bits = vmx_decache_cr0_guest_bits,
Avi Kivityaff48ba2010-12-05 18:56:11 +020012433 .decache_cr3 = vmx_decache_cr3,
Anthony Liguori25c4c272007-04-27 09:29:21 +030012434 .decache_cr4_guest_bits = vmx_decache_cr4_guest_bits,
Avi Kivity6aa8b732006-12-10 02:21:36 -080012435 .set_cr0 = vmx_set_cr0,
Avi Kivity6aa8b732006-12-10 02:21:36 -080012436 .set_cr3 = vmx_set_cr3,
12437 .set_cr4 = vmx_set_cr4,
Avi Kivity6aa8b732006-12-10 02:21:36 -080012438 .set_efer = vmx_set_efer,
Avi Kivity6aa8b732006-12-10 02:21:36 -080012439 .get_idt = vmx_get_idt,
12440 .set_idt = vmx_set_idt,
12441 .get_gdt = vmx_get_gdt,
12442 .set_gdt = vmx_set_gdt,
Jan Kiszka73aaf249e2014-01-04 18:47:16 +010012443 .get_dr6 = vmx_get_dr6,
12444 .set_dr6 = vmx_set_dr6,
Gleb Natapov020df072010-04-13 10:05:23 +030012445 .set_dr7 = vmx_set_dr7,
Paolo Bonzini81908bf2014-02-21 10:32:27 +010012446 .sync_dirty_debug_regs = vmx_sync_dirty_debug_regs,
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -030012447 .cache_reg = vmx_cache_reg,
Avi Kivity6aa8b732006-12-10 02:21:36 -080012448 .get_rflags = vmx_get_rflags,
12449 .set_rflags = vmx_set_rflags,
Huaitong Hanbe94f6b2016-03-22 16:51:20 +080012450
Avi Kivity6aa8b732006-12-10 02:21:36 -080012451 .tlb_flush = vmx_flush_tlb,
Avi Kivity6aa8b732006-12-10 02:21:36 -080012452
Avi Kivity6aa8b732006-12-10 02:21:36 -080012453 .run = vmx_vcpu_run,
Avi Kivity6062d012009-03-23 17:35:17 +020012454 .handle_exit = vmx_handle_exit,
Avi Kivity6aa8b732006-12-10 02:21:36 -080012455 .skip_emulated_instruction = skip_emulated_instruction,
Glauber Costa2809f5d2009-05-12 16:21:05 -040012456 .set_interrupt_shadow = vmx_set_interrupt_shadow,
12457 .get_interrupt_shadow = vmx_get_interrupt_shadow,
Ingo Molnar102d8322007-02-19 14:37:47 +020012458 .patch_hypercall = vmx_patch_hypercall,
Eddie Dong2a8067f2007-08-06 16:29:07 +030012459 .set_irq = vmx_inject_irq,
Gleb Natapov95ba8273132009-04-21 17:45:08 +030012460 .set_nmi = vmx_inject_nmi,
Avi Kivity298101d2007-11-25 13:41:11 +020012461 .queue_exception = vmx_queue_exception,
Avi Kivityb463a6f2010-07-20 15:06:17 +030012462 .cancel_injection = vmx_cancel_injection,
Gleb Natapov78646122009-03-23 12:12:11 +020012463 .interrupt_allowed = vmx_interrupt_allowed,
Gleb Natapov95ba8273132009-04-21 17:45:08 +030012464 .nmi_allowed = vmx_nmi_allowed,
Jan Kiszka3cfc3092009-11-12 01:04:25 +010012465 .get_nmi_mask = vmx_get_nmi_mask,
12466 .set_nmi_mask = vmx_set_nmi_mask,
Gleb Natapov95ba8273132009-04-21 17:45:08 +030012467 .enable_nmi_window = enable_nmi_window,
12468 .enable_irq_window = enable_irq_window,
12469 .update_cr8_intercept = update_cr8_intercept,
Yang Zhang8d146952013-01-25 10:18:50 +080012470 .set_virtual_x2apic_mode = vmx_set_virtual_x2apic_mode,
Tang Chen38b99172014-09-24 15:57:54 +080012471 .set_apic_access_page_addr = vmx_set_apic_access_page_addr,
Andrey Smetanind62caab2015-11-10 15:36:33 +030012472 .get_enable_apicv = vmx_get_enable_apicv,
12473 .refresh_apicv_exec_ctrl = vmx_refresh_apicv_exec_ctrl,
Yang Zhangc7c9c562013-01-25 10:18:51 +080012474 .load_eoi_exitmap = vmx_load_eoi_exitmap,
Paolo Bonzini967235d2016-12-19 14:03:45 +010012475 .apicv_post_state_restore = vmx_apicv_post_state_restore,
Yang Zhangc7c9c562013-01-25 10:18:51 +080012476 .hwapic_irr_update = vmx_hwapic_irr_update,
12477 .hwapic_isr_update = vmx_hwapic_isr_update,
Yang Zhanga20ed542013-04-11 19:25:15 +080012478 .sync_pir_to_irr = vmx_sync_pir_to_irr,
12479 .deliver_posted_interrupt = vmx_deliver_posted_interrupt,
Gleb Natapov95ba8273132009-04-21 17:45:08 +030012480
Izik Eiduscbc94022007-10-25 00:29:55 +020012481 .set_tss_addr = vmx_set_tss_addr,
Sheng Yang67253af2008-04-25 10:20:22 +080012482 .get_tdp_level = get_ept_level,
Sheng Yang4b12f0d2009-04-27 20:35:42 +080012483 .get_mt_mask = vmx_get_mt_mask,
Marcelo Tosatti229456f2009-06-17 09:22:14 -030012484
Avi Kivity586f9602010-11-18 13:09:54 +020012485 .get_exit_info = vmx_get_exit_info,
Avi Kivity586f9602010-11-18 13:09:54 +020012486
Sheng Yang17cc3932010-01-05 19:02:27 +080012487 .get_lpage_level = vmx_get_lpage_level,
Sheng Yang0e851882009-12-18 16:48:46 +080012488
12489 .cpuid_update = vmx_cpuid_update,
Sheng Yang4e47c7a2009-12-18 16:48:47 +080012490
12491 .rdtscp_supported = vmx_rdtscp_supported,
Mao, Junjiead756a12012-07-02 01:18:48 +000012492 .invpcid_supported = vmx_invpcid_supported,
Joerg Roedeld4330ef2010-04-22 12:33:11 +020012493
12494 .set_supported_cpuid = vmx_set_supported_cpuid,
Sheng Yangf5f48ee2010-06-30 12:25:15 +080012495
12496 .has_wbinvd_exit = cpu_has_vmx_wbinvd_exit,
Zachary Amsden99e3e302010-08-19 22:07:17 -100012497
12498 .write_tsc_offset = vmx_write_tsc_offset,
Joerg Roedel1c97f0a2010-09-10 17:30:41 +020012499
12500 .set_tdp_cr3 = vmx_set_cr3,
Joerg Roedel8a76d7f2011-04-04 12:39:27 +020012501
12502 .check_intercept = vmx_check_intercept,
Yang Zhanga547c6d2013-04-11 19:25:10 +080012503 .handle_external_intr = vmx_handle_external_intr,
Liu, Jinsongda8999d2014-02-24 10:55:46 +000012504 .mpx_supported = vmx_mpx_supported,
Wanpeng Li55412b22014-12-02 19:21:30 +080012505 .xsaves_supported = vmx_xsaves_supported,
Paolo Bonzini66336ca2016-07-12 10:36:41 +020012506 .umip_emulated = vmx_umip_emulated,
Jan Kiszkab6b8a142014-03-07 20:03:12 +010012507
12508 .check_nested_events = vmx_check_nested_events,
Radim Krčmářae97a3b2014-08-21 18:08:06 +020012509
12510 .sched_in = vmx_sched_in,
Kai Huang843e4332015-01-28 10:54:28 +080012511
12512 .slot_enable_log_dirty = vmx_slot_enable_log_dirty,
12513 .slot_disable_log_dirty = vmx_slot_disable_log_dirty,
12514 .flush_log_dirty = vmx_flush_log_dirty,
12515 .enable_log_dirty_pt_masked = vmx_enable_log_dirty_pt_masked,
Bandan Dasc5f983f2017-05-05 15:25:14 -040012516 .write_log_dirty = vmx_write_pml_buffer,
Wei Huang25462f72015-06-19 15:45:05 +020012517
Feng Wubf9f6ac2015-09-18 22:29:55 +080012518 .pre_block = vmx_pre_block,
12519 .post_block = vmx_post_block,
12520
Wei Huang25462f72015-06-19 15:45:05 +020012521 .pmu_ops = &intel_pmu_ops,
Feng Wuefc64402015-09-18 22:29:51 +080012522
12523 .update_pi_irte = vmx_update_pi_irte,
Yunhong Jiang64672c92016-06-13 14:19:59 -070012524
12525#ifdef CONFIG_X86_64
12526 .set_hv_timer = vmx_set_hv_timer,
12527 .cancel_hv_timer = vmx_cancel_hv_timer,
12528#endif
Ashok Rajc45dcc72016-06-22 14:59:56 +080012529
12530 .setup_mce = vmx_setup_mce,
Ladi Prosek0234bf82017-10-11 16:54:40 +020012531
Ladi Prosek72d7b372017-10-11 16:54:41 +020012532 .smi_allowed = vmx_smi_allowed,
Ladi Prosek0234bf82017-10-11 16:54:40 +020012533 .pre_enter_smm = vmx_pre_enter_smm,
12534 .pre_leave_smm = vmx_pre_leave_smm,
Ladi Prosekcc3d9672017-10-17 16:02:39 +020012535 .enable_smi_window = enable_smi_window,
Avi Kivity6aa8b732006-12-10 02:21:36 -080012536};
12537
12538static int __init vmx_init(void)
12539{
Tiejun Chen34a1cd62014-10-28 10:14:48 +080012540 int r = kvm_init(&vmx_x86_ops, sizeof(struct vcpu_vmx),
12541 __alignof__(struct vcpu_vmx), THIS_MODULE);
He, Qingfdef3ad2007-04-30 09:45:24 +030012542 if (r)
Tiejun Chen34a1cd62014-10-28 10:14:48 +080012543 return r;
Sheng Yang25c5f222008-03-28 13:18:56 +080012544
Dave Young2965faa2015-09-09 15:38:55 -070012545#ifdef CONFIG_KEXEC_CORE
Zhang Yanfei8f536b72012-12-06 23:43:34 +080012546 rcu_assign_pointer(crash_vmclear_loaded_vmcss,
12547 crash_vmclear_local_loaded_vmcss);
12548#endif
12549
He, Qingfdef3ad2007-04-30 09:45:24 +030012550 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -080012551}
12552
12553static void __exit vmx_exit(void)
12554{
Dave Young2965faa2015-09-09 15:38:55 -070012555#ifdef CONFIG_KEXEC_CORE
Monam Agarwal3b63a432014-03-22 12:28:10 +053012556 RCU_INIT_POINTER(crash_vmclear_loaded_vmcss, NULL);
Zhang Yanfei8f536b72012-12-06 23:43:34 +080012557 synchronize_rcu();
12558#endif
12559
Zhang Xiantaocb498ea2007-11-14 20:39:31 +080012560 kvm_exit();
Avi Kivity6aa8b732006-12-10 02:21:36 -080012561}
12562
12563module_init(vmx_init)
12564module_exit(vmx_exit)